memory

package
v0.12.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package memory — manager.go provides an LLM-powered memory manager inspired by Agno's MemoryManager.

Package memory provides short-term and long-term memory APIs for agents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager uses an LLM to autonomously decide what to remember from conversations.

When a vector index is attached via WithVectorIndex, memories are additionally embedded on write and can be retrieved by semantic relevance via Recall. The vector index is optional: without it the Manager behaves exactly as before (write-through to the relational store, full-dump recall via GetUserMemories).

func NewManager

func NewManager(agentID, userID string, store *Store, provider model.Provider) *Manager

NewManager creates an LLM-powered memory manager. The underlying store is scoped to userID so that all reads and writes are isolated per tenant. An empty userID uses the global/tenantless bucket.

func (*Manager) CanRecall added in v0.10.0

func (m *Manager) CanRecall() bool

CanRecall reports whether a semantic index is attached (WithVectorIndex).

func (*Manager) ExtractMemories

func (m *Manager) ExtractMemories(ctx context.Context, messages []model.Message) error

ExtractMemories uses the LLM to identify memorable facts from messages and stores them.

func (*Manager) GetUserMemories

func (m *Manager) GetUserMemories(ctx context.Context) (string, error)

GetUserMemories returns all long-term memories, formatted for context injection.

func (*Manager) MemoryTools

func (m *Manager) MemoryTools() []MemoryTool

MemoryTools returns tool definitions that let the model manage memory directly (agentic memory).

func (*Manager) OptimizeMemories

func (m *Manager) OptimizeMemories(ctx context.Context) error

OptimizeMemories asks the LLM to compress/deduplicate existing long-term memories.

func (*Manager) Recall added in v0.10.0

func (m *Manager) Recall(ctx context.Context, query string, topK int) ([]RecalledMemory, error)

Recall semantically retrieves the top-k long-term memories most relevant to query, scoped to this manager's tenant, ranked by descending score. Scoping is delegated to the vector store via a metadata filter (storage.WithFilter), so top-k is computed within the tenant's subset of a shared per-agent collection. When no index is attached it returns (nil, nil). Search errors are propagated (wrapped); the agent's injection sites treat any error as "no memories", so recall stays best-effort at the boundary that owns that policy.

func (*Manager) UserID added in v0.4.0

func (m *Manager) UserID() string

UserID reports the tenant this manager is scoped to ("" means global).

func (*Manager) WithUserID added in v0.4.0

func (m *Manager) WithUserID(userID string) *Manager

WithUserID returns a copy of the manager scoped to a different tenant, re-scoping the underlying store as well. The original manager is unchanged, so a shared agent-level manager can be safely re-scoped per request.

func (*Manager) WithVectorIndex added in v0.10.0

func (m *Manager) WithVectorIndex(embedder model.EmbeddingsProvider, vstore storage.VectorStore, embedModel string, dimension int) *Manager

WithVectorIndex returns a copy of the manager with a semantic index attached, enabling embed-on-write and Recall. The collection is per-agent; tenant isolation within it is enforced by Recall's scope filter, not by the collection name. The original manager is unchanged, and the returned copy is safe to re-scope with WithUserID (the index fields survive the shallow copy), so a shared agent-level manager can be indexed once and re-scoped per request.

type MemoryTool

type MemoryTool struct {
	Name        string
	Description string
	Handler     func(ctx context.Context, args map[string]any) (any, error)
}

MemoryTool is a tool definition for agentic memory management.

type RecalledMemory added in v0.10.0

type RecalledMemory struct {
	Key     string  `json:"key"`
	Value   any     `json:"value"`
	Content string  `json:"content"`
	Score   float32 `json:"score"`
}

RecalledMemory is one semantically-retrieved long-term memory, ranked by Score (higher is more relevant). It is a structured candidate — never a pre-formatted string — so a caller (e.g. the agent's context budget) can rank, trim, or format it as needed.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store provides a high-level memory API on top of storage.Storage.

Long-term memory is multi-tenant: it is keyed by (agentID, userID) so that different users served by the same agent never observe each other's memories. When userID is empty, long-term memory falls back to a documented global/tenantless bucket.

func NewStore

func NewStore(agentID string, backend storage.Storage) *Store

NewStore creates a memory store for the given agent. The store is scoped to the global/tenantless long-term bucket; use NewStoreForUser or ForUser to scope it to a specific tenant.

func NewStoreForUser added in v0.4.0

func NewStoreForUser(agentID, userID string, backend storage.Storage) *Store

NewStoreForUser creates a memory store scoped to a specific user (tenant). An empty userID selects the global/tenantless long-term bucket.

func (*Store) DeleteLongTerm added in v0.4.0

func (s *Store) DeleteLongTerm(ctx context.Context, key string) error

DeleteLongTerm removes a long-term memory for the store's tenant by key.

func (*Store) ForUser added in v0.4.0

func (s *Store) ForUser(userID string) *Store

ForUser returns a shallow copy of the store scoped to the given user. The original store is left unchanged, so a shared agent-level store can be safely re-scoped per request. An empty userID selects the global/tenantless bucket.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string) (any, error)

Get retrieves a memory value by logical key. It resolves the tenant-scoped long-term key first and falls back to the raw key for short-term or legacy records.

func (*Store) ListLongTerm

func (s *Store) ListLongTerm(ctx context.Context) ([]*storage.MemoryRecord, error)

ListLongTerm returns the long-term memories that belong to the store's tenant. Records are returned with their logical (un-namespaced) key restored.

func (*Store) ListShortTerm

func (s *Store) ListShortTerm(ctx context.Context) ([]*storage.MemoryRecord, error)

ListShortTerm returns all short-term memories for this agent.

func (*Store) SetLongTerm

func (s *Store) SetLongTerm(ctx context.Context, key string, value any) error

SetLongTerm stores a value in cross-session persistent memory, scoped to the store's tenant (agentID, userID).

func (*Store) SetShortTerm

func (s *Store) SetShortTerm(ctx context.Context, sessionID, key string, value any) error

SetShortTerm stores a value in session-scoped working memory.

func (*Store) UserID added in v0.4.0

func (s *Store) UserID() string

UserID reports the tenant this store is scoped to ("" means global).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL