memory

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeFact       = "fact"
	TypePreference = "preference"
	TypeEvent      = "event"
	TypeNote       = "note"

	DefaultMaxAge                  = 180 * 24 * time.Hour
	DefaultPruneThreshold          = 0.1
	DefaultHalfLifeFact            = 90 * 24 * time.Hour
	DefaultHalfLifeEvent           = 30 * 24 * time.Hour
	DefaultHalfLifeNote            = 7 * 24 * time.Hour
	DefaultSimilarityHighThreshold = 0.85 // MinHash >= this -> update existing in place
	DefaultSimilarityMidThreshold  = 0.50 // MinHash >= this -> ask LLM if available

)
View Source
const MemoryLibraryName = "scriptling.ai.memory"

Variables

This section is empty.

Functions

func Register

func Register(registrar interface{ RegisterLibrary(*object.Library) }, log ...logger.Logger)

Register registers the scriptling.ai.memory library.

Types

type Memory

type Memory struct {
	ID         string    `msgpack:"id"`
	Content    string    `msgpack:"content"`
	Type       string    `msgpack:"type"`
	Importance float64   `msgpack:"importance"`
	CreatedAt  time.Time `msgpack:"created_at"`
	AccessedAt time.Time `msgpack:"accessed_at"`
	MinHash    []uint32  `msgpack:"min_hash"` // pre-computed MinHash signature for similarity
}

Memory is a single stored memory entry.

type Option

type Option func(*storeConfig)

Option is a functional option for Store.

func WithAIClient

func WithAIClient(client mcpai.Client, model string) Option

WithAIClient enables LLM-based similarity resolution for compaction. If client is nil, only rule-based pruning and auto-merging are performed.

func WithHalfLifeEvent

func WithHalfLifeEvent(d time.Duration) Option

func WithHalfLifeFact

func WithHalfLifeFact(d time.Duration) Option

func WithHalfLifeNote

func WithHalfLifeNote(d time.Duration) Option

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger sets the logger for the store.

func WithMaxAge

func WithMaxAge(d time.Duration) Option

func WithNoBackgroundPrune added in v0.5.0

func WithNoBackgroundPrune() Option

WithNoBackgroundPrune disables the background prune goroutine. Useful for tests that need deterministic prune behavior.

func WithPruneThreshold

func WithPruneThreshold(f float64) Option

func WithSimilarityMergeRange

func WithSimilarityMergeRange(mid, high float64) Option

WithSimilarityMergeRange sets the Jaccard similarity thresholds for pre-flight deduplication at write time. high: score >= high -> update existing memory in place (no LLM needed). mid: score >= mid -> ask LLM to merge or keep separate (if LLM configured).

type Store

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

Store is a memory store backed by a snapshotkv DB. It does not own the DB - the caller manages its lifecycle.

func New

func New(db *snapshotkv.DB, opts ...Option) *Store

New creates a Store using the provided DB and optional functional options.

func (*Store) Compact

func (s *Store) Compact() int

Compact runs compaction synchronously (prune + deduplicate). Returns the number of memories remaining after compaction.

func (*Store) Count

func (s *Store) Count() int

Count returns the total number of stored memories.

func (*Store) Forget

func (s *Store) Forget(id string) bool

Forget removes a memory by ID.

func (*Store) Recall

func (s *Store) Recall(query string, limit int, typeFilter string) []*Memory

Recall searches memories by keyword and semantic similarity, returning up to limit results ranked by score. Use limit=-1 for unlimited results. Use typeFilter="!xxx" to exclude a type (e.g., "!preference" returns all non-preferences).

func (*Store) Remember

func (s *Store) Remember(content, memType string, importance float64) (*Memory, error)

Remember stores a memory and returns it with a UUIDv7 ID. Before saving, performs a pre-flight similarity check against existing memories of the same type:

  • score >= similarityHighThreshold -> update the existing memory in place
  • score >= similarityMidThreshold -> ask LLM to merge or keep (if configured)

After saving, checks whether background compaction should be triggered.

Jump to

Keyboard shortcuts

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