memory

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeScope added in v0.7.0

func NormalizeScope(scope string) string

NormalizeScope cleans a scope path: lowercases, removes trailing slash (except for root), and collapses double slashes.

Types

type EntityMemory

type EntityMemory struct {
	ShortTerm
	// contains filtered or unexported fields
}

EntityMemory extends ShortTerm with entity-specific fact storage.

func NewEntityMemory

func NewEntityMemory(limit int) *EntityMemory

NewEntityMemory creates a new EntityMemory with the given entry limit.

func (*EntityMemory) GetAllEntities

func (e *EntityMemory) GetAllEntities(_ context.Context) ([]string, error)

GetAllEntities returns all entity names that have stored facts.

func (*EntityMemory) GetFacts

func (e *EntityMemory) GetFacts(_ context.Context, entity string) ([]string, error)

GetFacts retrieves all facts stored for an entity.

func (*EntityMemory) SaveFact

func (e *EntityMemory) SaveFact(_ context.Context, entity string, fact string) error

SaveFact stores a fact about an entity.

type EntityStore

type EntityStore interface {
	Store
	SaveFact(ctx context.Context, entity string, fact string) error
	GetFacts(ctx context.Context, entity string) ([]string, error)
	GetAllEntities(ctx context.Context) ([]string, error)
}

EntityStore extends Store with entity-specific fact storage and retrieval.

type Entry

type Entry struct {
	Role     string         `json:"role"`
	Content  string         `json:"content"`
	Metadata map[string]any `json:"metadata,omitempty"`
}

Entry represents a single memory entry with role, content, and metadata.

type MemVectorStore added in v0.7.0

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

MemVectorStore is an in-memory StorageBackend using brute-force cosine similarity.

func NewMemVectorStore added in v0.7.0

func NewMemVectorStore() *MemVectorStore

NewMemVectorStore creates an empty in-memory vector store.

func (*MemVectorStore) Clear added in v0.7.0

func (s *MemVectorStore) Clear(_ context.Context) error

func (*MemVectorStore) Close added in v0.7.0

func (s *MemVectorStore) Close() error

func (*MemVectorStore) Delete added in v0.7.0

func (s *MemVectorStore) Delete(_ context.Context, scope string) error

func (*MemVectorStore) Save added in v0.7.0

func (s *MemVectorStore) Save(_ context.Context, scope, content string, embedding []float64, metadata map[string]any) (string, error)

func (*MemVectorStore) Search added in v0.7.0

func (s *MemVectorStore) Search(_ context.Context, queryEmbedding []float64, limit int, scopeFilter string) ([]ScoredMemory, error)

type MemoryScope added in v0.7.0

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

MemoryScope represents a node in the hierarchical memory scope tree.

func GetScope added in v0.7.0

func GetScope(path string) *MemoryScope

GetScope returns the MemoryScope node for the given path, creating intermediate nodes as needed. Thread-safe.

func (*MemoryScope) Child added in v0.7.0

func (s *MemoryScope) Child(name string) *MemoryScope

Child returns the named child scope, creating it if it doesn't exist.

func (*MemoryScope) Children added in v0.7.0

func (s *MemoryScope) Children() []*MemoryScope

Children returns a copy of the direct child scopes.

func (*MemoryScope) DeleteSubtree added in v0.7.0

func (s *MemoryScope) DeleteSubtree()

DeleteSubtree removes this scope and all descendants from the parent's children map. Does nothing for the root scope.

func (*MemoryScope) Parent added in v0.7.0

func (s *MemoryScope) Parent() *MemoryScope

Parent returns the parent scope node, or nil for root.

func (*MemoryScope) Path added in v0.7.0

func (s *MemoryScope) Path() string

Path returns the normalized scope path (always ends with / except root).

type RecallOption added in v0.7.0

type RecallOption interface {
	// contains filtered or unexported methods
}

RecallOption configures a Recall call.

func WithLimit added in v0.7.0

func WithLimit(limit int) RecallOption

WithLimit sets the maximum number of results (default 10).

func WithScope added in v0.7.0

func WithScope(scope string) RecallOption

WithScope restricts recall to entries within the given scope subtree.

type RememberOption added in v0.7.0

type RememberOption interface {
	// contains filtered or unexported methods
}

RememberOption configures a Remember call.

func WithImportance added in v0.7.0

func WithImportance(importance float64) RememberOption

WithImportance sets the importance score for the memory entry (0.0 to 1.0).

type SQLiteVecOption added in v0.7.0

type SQLiteVecOption func(*SQLiteVecStore)

SQLiteVecOption configures the SQLiteVecStore.

func WithDBPath added in v0.7.0

func WithDBPath(path string) SQLiteVecOption

WithDBPath sets the SQLite database file path.

type SQLiteVecStore added in v0.7.0

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

SQLiteVecStore is a StorageBackend backed by a SQLite database with brute-force cosine similarity search.

func NewSQLiteVecStore added in v0.7.0

func NewSQLiteVecStore(opts ...SQLiteVecOption) (*SQLiteVecStore, error)

NewSQLiteVecStore creates a SQLiteVecStore, opening or creating the database.

func (*SQLiteVecStore) Clear added in v0.7.0

func (s *SQLiteVecStore) Clear(_ context.Context) error

func (*SQLiteVecStore) Close added in v0.7.0

func (s *SQLiteVecStore) Close() error

func (*SQLiteVecStore) Delete added in v0.7.0

func (s *SQLiteVecStore) Delete(_ context.Context, scope string) error

func (*SQLiteVecStore) Save added in v0.7.0

func (s *SQLiteVecStore) Save(_ context.Context, scope, content string, embedding []float64, metadata map[string]any) (string, error)

func (*SQLiteVecStore) Search added in v0.7.0

func (s *SQLiteVecStore) Search(_ context.Context, queryEmbedding []float64, limit int, scopeFilter string) ([]ScoredMemory, error)

type ScoredMemory added in v0.7.0

type ScoredMemory struct {
	ID           string         `json:"id"`
	Scope        string         `json:"scope"`
	Content      string         `json:"content"`
	Score        float64        `json:"score"`
	MatchReasons []string       `json:"match_reasons,omitempty"`
	Metadata     map[string]any `json:"metadata,omitempty"`
	Importance   float64        `json:"importance"`
	CreatedAt    time.Time      `json:"created_at"`
	// contains filtered or unexported fields
}

ScoredMemory is a single retrieved memory result with composite score.

type ShortTerm

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

ShortTerm is an in-memory circular buffer store for recent conversation entries.

func NewShortTerm

func NewShortTerm(limit int) *ShortTerm

NewShortTerm creates a new ShortTerm store with the given entry limit.

func (*ShortTerm) Clear

func (s *ShortTerm) Clear(_ context.Context) error

Clear removes all entries from short-term memory.

func (*ShortTerm) Save

func (s *ShortTerm) Save(_ context.Context, key string, entry Entry) error

Save stores an entry in the short-term memory buffer.

func (*ShortTerm) Search

func (s *ShortTerm) Search(_ context.Context, key string, query string, limit int) ([]Entry, error)

Search retrieves the most recent entries from short-term memory.

type StorageBackend added in v0.7.0

type StorageBackend interface {

	// Save stores a memory entry with its embedding and metadata.
	Save(ctx context.Context, scope, content string, embedding []float64, metadata map[string]any) (id string, err error)

	// Search returns all entries under scopeFilter (subtree) scored by
	// brute-force cosine similarity against queryEmbedding. The returned
	// Score field is raw cosine similarity (not composite) — UnifiedMemory
	// applies composite scoring after Search.
	Search(ctx context.Context, queryEmbedding []float64, limit int, scopeFilter string) ([]ScoredMemory, error)

	// Delete removes all entries whose scope starts with scope (subtree).
	Delete(ctx context.Context, scope string) error

	// Clear removes all entries.
	Clear(ctx context.Context) error

	// Close releases backend resources.
	Close() error
}

StorageBackend is the pluggable persistence layer for UnifiedMemory.

type Store

type Store interface {
	Save(ctx context.Context, key string, entry Entry) error
	Search(ctx context.Context, key string, query string, limit int) ([]Entry, error)
	Clear(ctx context.Context) error
}

Store is the interface for basic memory storage and retrieval.

type UnifiedMemory added in v0.7.0

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

UnifiedMemory is a hierarchical, scored memory system with pluggable backends.

func NewUnifiedMemory added in v0.7.0

func NewUnifiedMemory(cfg UnifiedMemoryConfig) *UnifiedMemory

NewUnifiedMemory creates a UnifiedMemory with the given config.

func (*UnifiedMemory) AsStore added in v0.7.0

func (m *UnifiedMemory) AsStore() Store

AsStore returns a memory.Store adapter wrapping this UnifiedMemory.

func (*UnifiedMemory) Close added in v0.7.0

func (m *UnifiedMemory) Close() error

Close releases backend resources and prevents further use.

func (*UnifiedMemory) ExtractMemories added in v0.7.0

func (m *UnifiedMemory) ExtractMemories(_ context.Context, content string) ([]string, error)

ExtractMemories implements the UnifiedMemory method for splitting text into atomic factual sentences.

func (*UnifiedMemory) Forget added in v0.7.0

func (m *UnifiedMemory) Forget(ctx context.Context, scope string) error

Forget removes all memories under the given scope subtree.

func (*UnifiedMemory) Recall added in v0.7.0

func (m *UnifiedMemory) Recall(ctx context.Context, query string, opts ...RecallOption) (results []ScoredMemory, err error)

Recall retrieves scored memories matching the query, scoped and sorted by composite score.

func (*UnifiedMemory) Remember added in v0.7.0

func (m *UnifiedMemory) Remember(ctx context.Context, scope, content string, opts ...RememberOption) (err error)

Remember stores content with an optional scope and options.

type UnifiedMemoryConfig added in v0.7.0

type UnifiedMemoryConfig struct {

	// SemanticWeight is the weight for cosine-similarity score (default 0.4).
	SemanticWeight float64

	// RecencyWeight is the weight for recency-based score (default 0.35).
	RecencyWeight float64

	// ImportanceWeight is the weight for user-assigned importance (default 0.25).
	ImportanceWeight float64

	// RecencyHalfLifeDays controls how fast recency decays (default 7.0).
	RecencyHalfLifeDays float64

	// StorageBackend is the persistence layer. Defaults to MemVectorStore.
	StorageBackend StorageBackend

	// Embedder converts text to a float64 vector. When nil, Recall uses
	// recency + importance only (no semantic score).
	Embedder func(ctx context.Context, text string) ([]float64, error)

	// ScopeInference enables LLM-based scope inference when Remember is
	// called with an empty scope (default false).
	ScopeInference bool

	// Callback is invoked for memory lifecycle events.
	Callback callback.Callback
}

UnifiedMemoryConfig configures a UnifiedMemory instance.

Jump to

Keyboard shortcuts

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