diff options
author | Grail Finder <wohilas@gmail.com> | 2024-11-20 20:47:49 +0300 |
---|---|---|
committer | Grail Finder <wohilas@gmail.com> | 2024-11-20 20:47:49 +0300 |
commit | 5ccad20bd680dc443b30f0decc8fca13427dc70d (patch) | |
tree | 5ed20ce680c09609a29f880cf41c976301f3e031 /storage/memory.go | |
parent | fc517c2c69d96501f1adc5a021b39b9eff22e4d7 (diff) |
Feat: add memory [wip]
Diffstat (limited to 'storage/memory.go')
-rw-r--r-- | storage/memory.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/storage/memory.go b/storage/memory.go new file mode 100644 index 0000000..a7bf8cc --- /dev/null +++ b/storage/memory.go @@ -0,0 +1,44 @@ +package storage + +import "elefant/models" + +type Memories interface { + Memorise(m *models.Memory) (*models.Memory, error) + Recall(agent, topic string) (string, error) + RecallTopics(agent string) ([]string, error) +} + +func (p ProviderSQL) Memorise(m *models.Memory) (*models.Memory, error) { + query := "INSERT INTO memories (agent, topic, mind) VALUES (:agent, :topic, :mind) RETURNING *;" + stmt, err := p.db.PrepareNamed(query) + if err != nil { + return nil, err + } + defer stmt.Close() + var memory models.Memory + err = stmt.Get(&memory, m) + if err != nil { + return nil, err + } + return &memory, nil +} + +func (p ProviderSQL) Recall(agent, topic string) (string, error) { + query := "SELECT mind FROM memories WHERE agent = $1 AND topic = $2" + var mind string + err := p.db.Get(&mind, query, agent, topic) + if err != nil { + return "", err + } + return mind, nil +} + +func (p ProviderSQL) RecallTopics(agent string) ([]string, error) { + query := "SELECT DISTINCT topic FROM memories WHERE agent = $1" + var topics []string + err := p.db.Select(&topics, query, agent) + if err != nil { + return nil, err + } + return topics, nil +} |