Documentation
¶
Index ¶
- func CompactIfNeeded(ctx context.Context, compactor *ConversationCompactor, ...) ([]schema.Message, int, bool, error)
- func DefaultMessageScorer(messages []schema.Message, index int) float64
- func DefaultTokenEstimator(msg schema.Message) int
- func EstimateTextTokens(text string) int
- type ArchiveFunc
- type Archiver
- type BatchStore
- type ChainCompressor
- type CompressFunc
- type ContextCompressor
- type ConversationCompactor
- func (c *ConversationCompactor) Compact(ctx context.Context, messages []schema.Message) ([]schema.Message, int, error)
- func (c *ConversationCompactor) EstimateTokens(messages []schema.Message) int
- func (c *ConversationCompactor) Summarizer() Summarizer
- func (c *ConversationCompactor) WithMaxInputTokens(n int) *ConversationCompactor
- func (c *ConversationCompactor) WithTokenEstimator(est TokenEstimator) *ConversationCompactor
- type Entry
- type ImportanceRankingCompressor
- type Manager
- type ManagerOption
- type MapStore
- func (s *MapStore) BatchGet(ctx context.Context, keys []string) (map[string]any, error)
- func (s *MapStore) BatchSet(_ context.Context, entries map[string]any, ttl int64) error
- func (s *MapStore) Clear(_ context.Context) error
- func (s *MapStore) Delete(_ context.Context, key string) error
- func (s *MapStore) Get(_ context.Context, key string) (any, bool, error)
- func (s *MapStore) List(_ context.Context, prefix string) ([]StoreEntry, error)
- func (s *MapStore) Set(_ context.Context, key string, value any, ttl int64) error
- func (s *MapStore) SetCreatedAtForTest(key string, t time.Time)
- type Memory
- type MessageScorer
- type PersistentMemory
- func (m *PersistentMemory) BatchGet(ctx context.Context, keys []string) (map[string]any, error)
- func (m *PersistentMemory) BatchSet(ctx context.Context, entries map[string]any, ttl int64) error
- func (m *PersistentMemory) Clear(ctx context.Context) error
- func (m *PersistentMemory) Delete(ctx context.Context, key string) error
- func (m *PersistentMemory) Get(ctx context.Context, key string) (any, error)
- func (m *PersistentMemory) List(ctx context.Context, prefix string) ([]Entry, error)
- func (m *PersistentMemory) Set(ctx context.Context, key string, value any, ttl int64) error
- type PromoteFunc
- type Promoter
- type Scope
- type SessionMemory
- func (m *SessionMemory) BatchGet(ctx context.Context, keys []string) (map[string]any, error)
- func (m *SessionMemory) BatchSet(ctx context.Context, entries map[string]any, ttl int64) error
- func (m *SessionMemory) Clear(ctx context.Context) error
- func (m *SessionMemory) Delete(ctx context.Context, key string) error
- func (m *SessionMemory) Get(ctx context.Context, key string) (any, error)
- func (m *SessionMemory) List(ctx context.Context, prefix string) ([]Entry, error)
- func (m *SessionMemory) Set(ctx context.Context, key string, value any, ttl int64) error
- type SlidingWindowCompressor
- type Store
- type StoreEntry
- type SummarizeAndTruncCompressor
- type SummarizeAndTruncOption
- type Summarizer
- type TokenBudgetCompressor
- type TokenEstimator
- type WorkingMemory
- func (m *WorkingMemory) BatchGet(ctx context.Context, keys []string) (map[string]any, error)
- func (m *WorkingMemory) BatchSet(ctx context.Context, entries map[string]any, ttl int64) error
- func (m *WorkingMemory) Clear(ctx context.Context) error
- func (m *WorkingMemory) Delete(ctx context.Context, key string) error
- func (m *WorkingMemory) Get(ctx context.Context, key string) (any, error)
- func (m *WorkingMemory) List(ctx context.Context, prefix string) ([]Entry, error)
- func (m *WorkingMemory) Set(ctx context.Context, key string, value any, ttl int64) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompactIfNeeded ¶ added in v0.4.0
func CompactIfNeeded( ctx context.Context, compactor *ConversationCompactor, messages []schema.Message, threshold int, ) ([]schema.Message, int, bool, error)
CompactIfNeeded checks the estimated token count and compresses if above the threshold. Returns the (possibly compressed) messages, updated token estimate, whether compaction occurred, and any error. This is a convenience for callers (CLI, HTTP handler) to avoid duplicating the check logic.
func DefaultMessageScorer ¶
DefaultMessageScorer provides a heuristic scoring function. System messages score highest (1000), followed by tool messages and assistant messages with tool calls (100), user messages (50), and plain assistant messages (10). A proportional recency bonus of up to 5% of the base score is added, so recency matters within the same role but does not override role priority.
func DefaultTokenEstimator ¶
DefaultTokenEstimator returns an approximate token count for a message. Uses a simple heuristic: len(text) / 4, with a minimum of 1 for non-empty content.
The minimum-of-1 rule prevents zero-token messages from bypassing budget checks. This means messages with 1-3 characters all estimate as 1 token, which slightly overcounts short messages. This is acceptable for a rough heuristic.
Known limitation: only the text portion of Content is considered. Multimodal content parts (images, etc.) are not accounted for in the estimate.
func EstimateTextTokens ¶ added in v0.4.0
EstimateTextTokens returns the estimated token count for a plain text string. Uses the same heuristic as DefaultTokenEstimator: len(text) / 4.
Types ¶
type ArchiveFunc ¶
ArchiveFunc is a function adapter for Archiver.
type Archiver ¶
Archiver decides which session memory entries are archived to store.
func ArchiveAll ¶
func ArchiveAll() Archiver
ArchiveAll returns an archiver that archives all entries.
func ArchiveNone ¶
func ArchiveNone() Archiver
ArchiveNone returns an archiver that archives no entries.
type BatchStore ¶
type BatchStore interface {
BatchGet(ctx context.Context, keys []string) (map[string]any, error)
BatchSet(ctx context.Context, entries map[string]any, ttl int64) error
}
BatchStore is an optional interface that Store implementations can provide for optimised bulk operations.
type ChainCompressor ¶
type ChainCompressor struct {
// contains filtered or unexported fields
}
ChainCompressor applies multiple compressors in sequence, passing the output of each compressor as input to the next.
func NewChainCompressor ¶
func NewChainCompressor(compressors ...ContextCompressor) *ChainCompressor
NewChainCompressor creates a ChainCompressor that applies the given compressors in order.
type CompressFunc ¶
type CompressFunc func(ctx context.Context, messages []schema.Message, maxTokens int) ([]schema.Message, error)
CompressFunc is a function adapter for ContextCompressor.
type ContextCompressor ¶
type ContextCompressor interface {
Compress(ctx context.Context, messages []schema.Message, maxTokens int) ([]schema.Message, error)
}
ContextCompressor compresses a message history to fit within a token budget.
type ConversationCompactor ¶ added in v0.4.0
type ConversationCompactor struct {
// contains filtered or unexported fields
}
ConversationCompactor compresses a conversation history by summarizing older messages while preserving protected messages (system prompt and recent turn pairs).
ConversationCompactor is safe for concurrent use; it holds no mutable state.
func NewConversationCompactor ¶ added in v0.4.0
func NewConversationCompactor(summarizer Summarizer, protectedTurns int) *ConversationCompactor
NewConversationCompactor creates a ConversationCompactor. protectedTurns is the number of recent turn pairs (user + assistant) to keep verbatim.
func (*ConversationCompactor) Compact ¶ added in v0.4.0
func (c *ConversationCompactor) Compact(ctx context.Context, messages []schema.Message) ([]schema.Message, int, error)
Compact compresses messages by summarizing eligible (non-protected) messages. Returns the compressed message slice and the estimated token count of the result.
Protected messages: the first message if it is a system message, plus the last protectedTurns user/assistant exchange pairs.
The summary replaces all eligible messages with a single system-role message carrying metadata {"compressed": true, "source_count": N, "strategy": "conversation_compact"}.
func (*ConversationCompactor) EstimateTokens ¶ added in v0.4.0
func (c *ConversationCompactor) EstimateTokens(messages []schema.Message) int
EstimateTokens returns the total estimated token count for a message slice.
func (*ConversationCompactor) Summarizer ¶ added in v0.4.0
func (c *ConversationCompactor) Summarizer() Summarizer
Summarizer returns the underlying summarizer function.
func (*ConversationCompactor) WithMaxInputTokens ¶ added in v0.4.0
func (c *ConversationCompactor) WithMaxInputTokens(n int) *ConversationCompactor
WithMaxInputTokens sets the maximum token count for the summarizer input. If the eligible messages exceed this, the input is truncated with an omission marker. This prevents the summarization call itself from exceeding the context window.
func (*ConversationCompactor) WithTokenEstimator ¶ added in v0.4.0
func (c *ConversationCompactor) WithTokenEstimator(est TokenEstimator) *ConversationCompactor
WithTokenEstimator sets a custom token estimator.
type Entry ¶
type Entry struct {
Key string `json:"key"`
Value any `json:"value"`
Scope Scope `json:"scope"`
AgentID string `json:"agent_id,omitempty"`
SessionID string `json:"session_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
TTL int64 `json:"ttl,omitempty"` // seconds; 0 means no expiry
}
Entry is a single memory item stored in any tier.
type ImportanceRankingCompressor ¶
type ImportanceRankingCompressor struct {
// contains filtered or unexported fields
}
ImportanceRankingCompressor keeps the most important messages within a token budget, using a scorer function to rank messages by importance. The most important message is always retained regardless of token budget.
func NewImportanceRankingCompressor ¶
func NewImportanceRankingCompressor(scorer MessageScorer) *ImportanceRankingCompressor
NewImportanceRankingCompressor creates an ImportanceRankingCompressor with the given scorer. Panics if scorer is nil.
func NewImportanceRankingCompressorWithDefaults ¶
func NewImportanceRankingCompressorWithDefaults() *ImportanceRankingCompressor
NewImportanceRankingCompressorWithDefaults creates a compressor using DefaultMessageScorer.
func (*ImportanceRankingCompressor) Compress ¶
func (c *ImportanceRankingCompressor) Compress(ctx context.Context, messages []schema.Message, maxTokens int) ([]schema.Message, error)
Compress keeps the most important messages that fit within maxTokens. If maxTokens is 0 or negative, all messages are returned unchanged. The highest-scored message is always retained regardless of budget. Output messages are in chronological order.
func (*ImportanceRankingCompressor) WithTokenEstimator ¶
func (c *ImportanceRankingCompressor) WithTokenEstimator(est TokenEstimator) *ImportanceRankingCompressor
WithTokenEstimator sets a custom token estimator. Nil values are ignored.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager orchestrates the three-tier memory system.
func NewManager ¶
func NewManager(opts ...ManagerOption) *Manager
NewManager creates a Manager with the given options. Defaults: PromoteAll promoter, ArchiveNone archiver.
func (*Manager) ArchiveToStore ¶
ArchiveToStore archives entries from session memory to store memory. Returns an error if archival fails, but callers should not fail the Run.
func (*Manager) Compressor ¶
func (mgr *Manager) Compressor() ContextCompressor
Compressor returns the context compressor, or nil if not configured.
func (*Manager) PromoteToSession ¶
PromoteToSession promotes entries from working memory to session memory. Returns an error if promotion fails, but callers should not fail the Run.
type ManagerOption ¶
type ManagerOption func(*Manager)
ManagerOption configures a Manager.
func WithArchiver ¶
func WithArchiver(a Archiver) ManagerOption
WithArchiver sets the archival strategy.
func WithCompressor ¶
func WithCompressor(c ContextCompressor) ManagerOption
WithCompressor sets the context compression strategy.
func WithPromoter ¶
func WithPromoter(p Promoter) ManagerOption
WithPromoter sets the promotion strategy.
func WithSession ¶
func WithSession(m Memory) ManagerOption
WithSession sets the session-tier memory.
type MapStore ¶
type MapStore struct {
// contains filtered or unexported fields
}
MapStore is an in-memory Store backed by a plain Go map. It is not safe for concurrent use; callers must provide their own locking.
type Memory ¶
type Memory interface {
// Get returns the value for the given key, or nil if not found.
Get(ctx context.Context, key string) (any, error)
// Set stores a value under the given key with an optional TTL in seconds.
Set(ctx context.Context, key string, value any, ttl int64) error
// Delete removes the entry for the given key.
Delete(ctx context.Context, key string) error
// List returns all entries whose keys have the given prefix.
List(ctx context.Context, prefix string) ([]Entry, error)
// Clear removes all entries.
Clear(ctx context.Context) error
// BatchGet returns the values for the given keys.
BatchGet(ctx context.Context, keys []string) (map[string]any, error)
// BatchSet stores multiple key-value pairs with the given TTL.
BatchSet(ctx context.Context, entries map[string]any, ttl int64) error
}
Memory is the core interface for a memory tier.
type MessageScorer ¶
MessageScorer assigns an importance score to a message. Higher scores indicate more important messages. The full message slice is provided so scorers can make context-aware decisions (e.g., keeping tool-call/tool-result pairs together). Parameters: the full messages slice and the index of the message to score.
type PersistentMemory ¶
type PersistentMemory struct {
// contains filtered or unexported fields
}
PersistentMemory is a cross-session persistent in-process memory store. It is safe for concurrent use.
func NewPersistentMemory ¶
func NewPersistentMemory() *PersistentMemory
NewPersistentMemory creates a new PersistentMemory backed by an in-memory MapStore.
func NewPersistentMemoryWithStore ¶
func NewPersistentMemoryWithStore(store Store) *PersistentMemory
NewPersistentMemoryWithStore creates a new PersistentMemory backed by the given Store.
type PromoteFunc ¶
PromoteFunc is a function adapter for Promoter.
type Promoter ¶
Promoter decides which working memory entries are promoted to session memory.
func PromoteAll ¶
func PromoteAll() Promoter
PromoteAll returns a promoter that promotes all entries.
func PromoteNone ¶
func PromoteNone() Promoter
PromoteNone returns a promoter that promotes no entries.
type SessionMemory ¶
type SessionMemory struct {
// contains filtered or unexported fields
}
SessionMemory is a per-session in-process memory store. It is safe for concurrent use.
func NewSessionMemory ¶
func NewSessionMemory(agentID, sessionID string) *SessionMemory
NewSessionMemory creates a new SessionMemory backed by an in-memory MapStore.
func NewSessionMemoryWithStore ¶
func NewSessionMemoryWithStore(store Store, agentID, sessionID string) *SessionMemory
NewSessionMemoryWithStore creates a new SessionMemory backed by the given Store.
type SlidingWindowCompressor ¶
type SlidingWindowCompressor struct {
// contains filtered or unexported fields
}
SlidingWindowCompressor keeps the last N messages, then delegates to TokenBudgetCompressor for token-based trimming within the window.
func NewSlidingWindowCompressor ¶
func NewSlidingWindowCompressor(windowSize int) *SlidingWindowCompressor
NewSlidingWindowCompressor creates a compressor that keeps the last windowSize messages. Panics if windowSize is not positive.
func (*SlidingWindowCompressor) Compress ¶
func (c *SlidingWindowCompressor) Compress(ctx context.Context, messages []schema.Message, maxTokens int) ([]schema.Message, error)
Compress returns the last windowSize messages, optionally trimmed to fit within maxTokens. If maxTokens is 0 or negative, no token budget is applied.
func (*SlidingWindowCompressor) WithTokenEstimator ¶
func (c *SlidingWindowCompressor) WithTokenEstimator(est TokenEstimator) *SlidingWindowCompressor
WithTokenEstimator sets a custom token estimator for the internal token budget check.
type Store ¶
type Store interface {
Get(ctx context.Context, key string) (any, bool, error)
Set(ctx context.Context, key string, value any, ttl int64) error
Delete(ctx context.Context, key string) error
List(ctx context.Context, prefix string) ([]StoreEntry, error)
Clear(ctx context.Context) error
}
Store is the low-level interface for raw key-value storage backends. Implementations must be safe for use by a single goroutine; the memory tiers provide their own concurrency control when needed.
type StoreEntry ¶
StoreEntry is a single record returned by Store.List.
type SummarizeAndTruncCompressor ¶
type SummarizeAndTruncCompressor struct {
// contains filtered or unexported fields
}
SummarizeAndTruncCompressor splits messages into older and recent parts, summarizes the older messages, and prepends the summary to the recent ones. The keepLastN parameter controls how many recent messages to keep verbatim. When maxTokens > 0, the summary text is truncated to fit the remaining token budget after accounting for the recent messages.
func NewSummarizeAndTruncCompressor ¶
func NewSummarizeAndTruncCompressor(summarizer Summarizer, keepLastN int, opts ...SummarizeAndTruncOption) *SummarizeAndTruncCompressor
NewSummarizeAndTruncCompressor creates a new SummarizeAndTruncCompressor. Panics if summarizer is nil or keepLastN <= 0.
func (*SummarizeAndTruncCompressor) Compress ¶
func (c *SummarizeAndTruncCompressor) Compress(ctx context.Context, messages []schema.Message, maxTokens int) ([]schema.Message, error)
Compress summarizes older messages and keeps the last keepLastN messages verbatim. When maxTokens > 0, the summary text is truncated proportionally to fit the remaining token budget after accounting for recent messages.
func (*SummarizeAndTruncCompressor) WithTokenEstimator ¶
func (c *SummarizeAndTruncCompressor) WithTokenEstimator(est TokenEstimator) *SummarizeAndTruncCompressor
WithTokenEstimator sets a custom token estimator. Nil values are ignored.
type SummarizeAndTruncOption ¶
type SummarizeAndTruncOption func(*SummarizeAndTruncCompressor)
SummarizeAndTruncOption configures a SummarizeAndTruncCompressor.
func WithSummaryRole ¶
func WithSummaryRole(role aimodel.Role) SummarizeAndTruncOption
WithSummaryRole sets the role assigned to the summary message. Defaults to aimodel.RoleUser.
type Summarizer ¶
Summarizer is a function that summarizes messages into a single text string.
type TokenBudgetCompressor ¶
type TokenBudgetCompressor struct {
// contains filtered or unexported fields
}
TokenBudgetCompressor keeps the most recent messages that fit within a token budget. It uses a simple reverse-iteration strategy: starting from the newest message, it accumulates messages until the budget is exhausted.
func NewTokenBudgetCompressor ¶
func NewTokenBudgetCompressor() *TokenBudgetCompressor
NewTokenBudgetCompressor creates a new TokenBudgetCompressor with DefaultTokenEstimator.
func (*TokenBudgetCompressor) Compress ¶
func (c *TokenBudgetCompressor) Compress(ctx context.Context, messages []schema.Message, maxTokens int) ([]schema.Message, error)
Compress keeps the most recent messages that fit within maxTokens. If maxTokens is 0 or negative, all messages are returned unchanged. At least one message (the most recent) is always returned for non-empty input.
func (*TokenBudgetCompressor) WithTokenEstimator ¶
func (c *TokenBudgetCompressor) WithTokenEstimator(est TokenEstimator) *TokenBudgetCompressor
WithTokenEstimator sets a custom token estimator. Nil values are ignored.
type TokenEstimator ¶
TokenEstimator estimates the token count for a message. Implementations can use simple heuristics or real tokenizers depending on accuracy needs.
type WorkingMemory ¶
type WorkingMemory struct {
// contains filtered or unexported fields
}
WorkingMemory is a per-Run in-process memory store. It is not safe for concurrent use (single goroutine per Run).
func NewWorkingMemory ¶
func NewWorkingMemory(agentID, sessionID string) *WorkingMemory
NewWorkingMemory creates a new WorkingMemory backed by an in-memory MapStore.
func NewWorkingMemoryWithStore ¶
func NewWorkingMemoryWithStore(store Store, agentID, sessionID string) *WorkingMemory
NewWorkingMemoryWithStore creates a new WorkingMemory backed by the given Store.
func (*WorkingMemory) Delete ¶
func (m *WorkingMemory) Delete(ctx context.Context, key string) error