memory

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KindPreference  interfaces.MemoryKind = "preference"
	KindFact        interfaces.MemoryKind = "fact"
	KindDecision    interfaces.MemoryKind = "decision"
	KindInstruction interfaces.MemoryKind = "instruction"
	KindNote        interfaces.MemoryKind = "note"
)

Optional default kinds for general-purpose agents. Custom agents may define and use their own kind strings.

View Source
const (
	ScopeKeyTenantID = "tenant_id"
	ScopeKeyUserID   = "user_id"
	ScopeKeyAgentID  = "agent_id"
)

Default scope metadata keys for DefaultScopeConfig and ScopeMetadata.

View Source
const (
	TTLDecision    = 7 * 24 * time.Hour
	TTLFact        = 0
	TTLPreference  = 0
	TTLInstruction = 0
	TTLNote        = 48 * time.Hour
)

Default TTL values for DefaultTTLPolicy. Zero duration means no expiry.

Variables

This section is empty.

Functions

func ScopeMetadata

func ScopeMetadata(s interfaces.MemoryScope) map[string]string

ScopeMetadata returns a flat map of scope fields for vector-store or metadata filters. SDK default keys are defined in ScopeKeyTenantID, ScopeKeyUserID, and ScopeKeyAgentID. Struct fields take precedence over conflicting tag keys.

func WithContextAgentID

func WithContextAgentID(ctx context.Context, agentID string) context.Context

WithContextAgentID attaches agent ID for default scope resolution.

func WithContextTenantID

func WithContextTenantID(ctx context.Context, tenantID string) context.Context

WithContextTenantID attaches tenant ID for default scope resolution.

func WithContextUserID

func WithContextUserID(ctx context.Context, userID string) context.Context

WithContextUserID attaches user ID for default scope resolution.

Types

type Config

type Config struct {
	// Memory is the memory backend implementation. Required.
	// Shipped backends: pkg/memory/weaviate and pkg/memory/pgvector.
	Memory interfaces.Memory

	// ScopeConfig resolves [interfaces.MemoryScope] from request context.
	ScopeConfig ScopeConfig

	// TTLPolicy sets ExpiresAt at Store time from record kind.
	TTLPolicy TTLPolicy

	// Store controls when and how memories are written.
	Store StoreConfig

	// Recall controls Load behavior when the SDK recalls memories for a run.
	Recall RecallConfig
}

Config wires long-term memory for the agent SDK.

func DefaultConfig

func DefaultConfig(store interfaces.Memory) Config

DefaultConfig returns a Config with SDK defaults for scope, TTL, store, and recall policies.

func (Config) ExpiresAtForKind

func (c Config) ExpiresAtForKind(kind interfaces.MemoryKind, now time.Time) time.Time

ExpiresAtForKind returns expiry for kind at now using the config TTL policy.

func (Config) Validate

func (c Config) Validate() error

Validate checks the config. Call [WithDefaults] first.

func (Config) WithDefaults

func (c Config) WithDefaults() Config

WithDefaults fills zero policy fields with SDK defaults. Memory must be set separately.

type ExtractFunc

type ExtractFunc func(ctx context.Context, messages []interfaces.Message) ([]interfaces.MemoryRecord, error)

ExtractFunc extracts long-term memories from a completed run. Used only when StoreMode is StoreModeAlways. Nil uses the SDK default LLM extractor.

type RecallConfig

type RecallConfig struct {
	// Enabled recalls memories automatically for each run when true (default).
	// When false the SDK still stores memories after each run but skips Load.
	Enabled bool

	// Limit is the maximum number of memories to load. Zero or negative defaults to [defaultRecallLimit].
	Limit int

	// MinScore filters out entries below this relevance score when Score is applicable.
	MinScore float32

	// Kinds restricts recall to these kinds. Empty means all kinds.
	Kinds []interfaces.MemoryKind
}

RecallConfig controls SDK-initiated memory Load before or during a run.

func DefaultRecallConfig

func DefaultRecallConfig() RecallConfig

func (RecallConfig) LoadOptions

func (r RecallConfig) LoadOptions() []interfaces.LoadMemoryOption

LoadOptions builds interfaces.LoadMemoryOption values from recall settings.

func (RecallConfig) RecencyLoadOptions

func (r RecallConfig) RecencyLoadOptions() []interfaces.LoadMemoryOption

RecencyLoadOptions builds load options for scoped recency listing (no semantic min score).

type ScopeConfig

type ScopeConfig struct {
	TenantIDResolver ScopeResolver
	UserIDResolver   ScopeResolver
	AgentIDResolver  ScopeResolver

	// ExtraKeys lists tag keys always resolved on Store/Load/Clear (e.g. "project_id", "env").
	// Each key must have a matching entry in TagResolvers.
	ExtraKeys []string

	// TagResolvers supplies values for [ExtraKeys].
	TagResolvers map[string]ScopeResolver
}

ScopeConfig controls how the agent SDK builds interfaces.MemoryScope from context. Configured via Config.ScopeConfig and agent [WithMemory].

Defaults resolve tenant_id, user_id, and agent_id from context values set by WithContextTenantID, WithContextUserID, and WithContextAgentID. Override any field with a custom resolver when your auth or tenancy model differs.

func DefaultScopeConfig

func DefaultScopeConfig() ScopeConfig

DefaultScopeConfig returns SDK defaults: tenant_id, user_id, and agent_id from context.

func (ScopeConfig) Resolve

Resolve builds a interfaces.MemoryScope from context using configured resolvers.

func (ScopeConfig) Validate

func (c ScopeConfig) Validate() error

Validate checks that every ScopeConfig.ExtraKeys entry has a TagResolver.

type ScopeResolver

type ScopeResolver func(ctx context.Context) string

ScopeResolver extracts one scope field value from the request context.

type StoreConfig

type StoreConfig struct {
	// Mode controls when memories are written. Default [StoreModeOnDemand].
	Mode StoreMode

	// DedupMinScore is the semantic similarity floor for upserting an existing memory instead of appending.
	// Zero defaults to [defaultDedupMinScore].
	DedupMinScore float32

	// Extract overrides run-end memory extraction when [Mode] is [StoreModeAlways].
	// Must be nil when [Mode] is [StoreModeOnDemand].
	Extract ExtractFunc

	// DefaultKind is used when the record kind is empty. Zero defaults to [KindNote].
	DefaultKind interfaces.MemoryKind

	// AllowedKinds restricts stored kinds when non-empty.
	AllowedKinds []interfaces.MemoryKind
}

StoreConfig controls store timing, extraction, deduplication, and kind policy.

func DefaultStoreConfig

func DefaultStoreConfig() StoreConfig

DefaultStoreConfig returns SDK defaults for store behavior.

func (StoreConfig) ResolveKind

ResolveKind returns the kind to store, applying default and allowlist policy.

func (StoreConfig) Validate

func (s StoreConfig) Validate() error

Validate checks store configuration.

func (StoreConfig) WithDefaults

func (s StoreConfig) WithDefaults() StoreConfig

WithDefaults fills zero store fields with SDK defaults.

type StoreMode

type StoreMode string

StoreMode selects when the SDK persists long-term memories.

const (
	// StoreModeOnDemand registers the save_memory tool; the LLM stores via tool calls (default).
	StoreModeOnDemand StoreMode = "ondemand"
	// StoreModeAlways extracts memories at run end and stores them automatically.
	StoreModeAlways StoreMode = "always"
)

func DefaultStoreMode

func DefaultStoreMode() StoreMode

DefaultStoreMode returns the default store mode (StoreModeOnDemand).

type TTLPolicy

type TTLPolicy map[interfaces.MemoryKind]time.Duration

TTLPolicy maps memory kind strings to time-to-live. Zero duration means no expiry for that kind. Opt-in via agent config; not enforced by interfaces.Memory.

func DefaultTTLPolicy

func DefaultTTLPolicy() TTLPolicy

DefaultTTLPolicy returns a convenience TTL map for general-purpose agents.

func (TTLPolicy) ExpiresAt

func (p TTLPolicy) ExpiresAt(kind interfaces.MemoryKind, now time.Time) time.Time

ExpiresAt returns the expiry timestamp for kind at now. Empty or unknown kinds return zero time (no expiry) unless present in the policy map.

Directories

Path Synopsis
Package pgvector provides a interfaces.Memory implementation backed by PostgreSQL with pgvector.
Package pgvector provides a interfaces.Memory implementation backed by PostgreSQL with pgvector.
Package weaviate provides a interfaces.Memory implementation backed by Weaviate.
Package weaviate provides a interfaces.Memory implementation backed by Weaviate.

Jump to

Keyboard shortcuts

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