memory

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalMessages

func MarshalMessages(messages []llm.Message) ([]byte, error)

MarshalMessages serializes messages to JSON.

func UnmarshalMessages

func UnmarshalMessages(data []byte) ([]llm.Message, error)

UnmarshalMessages deserializes messages from JSON.

Types

type CompactableContext

type CompactableContext interface {
	ContextManager

	// Compact summarizes older messages to reduce token count.
	// The LLM is used to generate summaries.
	Compact(l llm.LLM) error

	// NeedsCompaction returns true if the context exceeds the threshold.
	NeedsCompaction(threshold int) bool
}

CompactableContext extends ContextManager with compaction support. Compaction summarizes old messages to reduce token usage while preserving context.

type ContextManager

type ContextManager interface {
	// Add appends a message to the context
	Add(msg llm.Message)

	// Messages returns messages that fit within maxTokens
	Messages(maxTokens int) []llm.Message

	// Clear resets the context
	Clear()

	// TokenCount returns current token usage
	TokenCount() int
}

ContextManager manages conversation history and token budgets.

type Memory

type Memory interface {
	// Store saves a value with a key
	Store(key string, value any, metadata map[string]any) error

	// Retrieve performs semantic search and returns top-k results
	Retrieve(query string, k int) ([]MemoryItem, error)

	// Get retrieves a specific item by key
	Get(key string) (MemoryItem, error)

	// Delete removes an item
	Delete(key string) error
}

Memory provides persistent storage for agent knowledge.

type MemoryItem

type MemoryItem struct {
	Key       string
	Value     any
	Metadata  map[string]any
	CreatedAt time.Time
	UpdatedAt time.Time
	Score     float64 // Relevance score for Retrieve
}

MemoryItem is a stored memory entry.

type SlidingWindowContext

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

SlidingWindowContext implements ContextManager with a sliding window and optional compaction.

func NewSlidingWindowContext

func NewSlidingWindowContext(maxMessages int) *SlidingWindowContext

NewSlidingWindowContext creates a context manager with a sliding window. maxMessages is the number of recent messages to keep (0 = unlimited).

func (*SlidingWindowContext) Add

func (c *SlidingWindowContext) Add(msg llm.Message)

Add appends a message to the context.

func (*SlidingWindowContext) Clear

func (c *SlidingWindowContext) Clear()

Clear resets the context.

func (*SlidingWindowContext) Compact

func (c *SlidingWindowContext) Compact(l llm.LLM) error

Compact summarizes older messages using the provided LLM.

func (*SlidingWindowContext) Messages

func (c *SlidingWindowContext) Messages(maxTokens int) []llm.Message

Messages returns messages that fit within maxTokens.

func (*SlidingWindowContext) NeedsCompaction

func (c *SlidingWindowContext) NeedsCompaction(threshold int) bool

NeedsCompaction returns true if token count exceeds threshold.

func (*SlidingWindowContext) TokenCount

func (c *SlidingWindowContext) TokenCount() int

TokenCount returns an estimated token count (roughly 4 chars per token).

type TokenBudgetContext

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

TokenBudgetContext manages conversation history within a token budget. When adding messages would exceed the budget, oldest messages are removed.

func NewTokenBudgetContext

func NewTokenBudgetContext(maxTokens int) *TokenBudgetContext

NewTokenBudgetContext creates a context that keeps messages within a token budget. Uses ~4 chars per token as a rough estimate.

func (*TokenBudgetContext) Add

func (c *TokenBudgetContext) Add(msg llm.Message)

Add appends a message to the context. If the new message would exceed the budget, oldest messages are removed.

func (*TokenBudgetContext) Clear

func (c *TokenBudgetContext) Clear()

Clear resets the context.

func (*TokenBudgetContext) Load

func (c *TokenBudgetContext) Load(messages []llm.Message)

Load initializes the context with existing messages. This is useful for restoring conversation history from persistence. If the loaded messages exceed the budget, oldest messages are trimmed.

func (*TokenBudgetContext) Messages

func (c *TokenBudgetContext) Messages(maxTokens int) []llm.Message

Messages returns messages that fit within maxTokens. If the requested maxTokens is lower than our budget, we return fewer messages.

func (*TokenBudgetContext) Snapshot

func (c *TokenBudgetContext) Snapshot() []llm.Message

Snapshot returns a copy of all messages for persistence.

func (*TokenBudgetContext) TokenCount

func (c *TokenBudgetContext) TokenCount() int

TokenCount returns current token usage.

Jump to

Keyboard shortcuts

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