Documentation
¶
Index ¶
- func MarshalMessages(messages []llm.Message) ([]byte, error)
- func UnmarshalMessages(data []byte) ([]llm.Message, error)
- type CompactableContext
- type ContextManager
- type Memory
- type MemoryItem
- type SlidingWindowContext
- func (c *SlidingWindowContext) Add(msg llm.Message)
- func (c *SlidingWindowContext) Clear()
- func (c *SlidingWindowContext) Compact(l llm.LLM) error
- func (c *SlidingWindowContext) Messages(maxTokens int) []llm.Message
- func (c *SlidingWindowContext) NeedsCompaction(threshold int) bool
- func (c *SlidingWindowContext) TokenCount() int
- type TokenBudgetContext
- func (c *TokenBudgetContext) Add(msg llm.Message)
- func (c *TokenBudgetContext) Clear()
- func (c *TokenBudgetContext) Load(messages []llm.Message)
- func (c *TokenBudgetContext) Messages(maxTokens int) []llm.Message
- func (c *TokenBudgetContext) Snapshot() []llm.Message
- func (c *TokenBudgetContext) TokenCount() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalMessages ¶
MarshalMessages serializes messages to 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) 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.