Documentation
¶
Index ¶
- type BufferMemory
- func (m *BufferMemory) Add(ctx context.Context, message core.Message) error
- func (m *BufferMemory) AddBatch(ctx context.Context, messages []core.Message) error
- func (m *BufferMemory) Clear(ctx context.Context) error
- func (m *BufferMemory) CurrentUsage() int
- func (m *BufferMemory) History(ctx context.Context) ([]core.Message, error)
- func (m *BufferMemory) HistoryLast(ctx context.Context, k int) ([]core.Message, error)
- func (m *BufferMemory) PruneRatio(ctx context.Context, ratio float64) (int, error)
- type CachedTokenCounter
- type Memory
- type RedisMemory
- func (r *RedisMemory) Add(ctx context.Context, message core.Message) error
- func (r *RedisMemory) AddBatch(ctx context.Context, messages []core.Message) error
- func (r *RedisMemory) Clear(ctx context.Context) error
- func (r *RedisMemory) History(ctx context.Context) ([]core.Message, error)
- func (r *RedisMemory) HistoryLast(ctx context.Context, k int) ([]core.Message, error)
- func (r *RedisMemory) PruneRatio(ctx context.Context, ratio float64) (int, error)
- type SimpleTokenCounter
- type TokenCounter
- type WordTokenCounter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferMemory ¶
type BufferMemory struct {
// contains filtered or unexported fields
}
BufferMemory implements an in-memory history storage with thread safety.
func NewTokenWindowMemory ¶
func NewTokenWindowMemory(maxTokens int, counter TokenCounter) *BufferMemory
NewTokenWindowMemory creates a memory with a specific token limit.
func (*BufferMemory) AddBatch ¶
AddBatch adds multiple messages and updates token count efficiently.
func (*BufferMemory) Clear ¶
func (m *BufferMemory) Clear(ctx context.Context) error
Clear wipes the memory.
func (*BufferMemory) CurrentUsage ¶
func (m *BufferMemory) CurrentUsage() int
CurrentUsage returns the current token count.
func (*BufferMemory) HistoryLast ¶
HistoryLast retrieves the last k messages safely.
func (*BufferMemory) PruneRatio ¶
PruneRatio implements the self-healing logic.
type CachedTokenCounter ¶
type CachedTokenCounter struct {
// contains filtered or unexported fields
}
func NewCachedTokenCounter ¶
func NewCachedTokenCounter(delegate TokenCounter) *CachedTokenCounter
func (*CachedTokenCounter) Count ¶
func (c *CachedTokenCounter) Count(text string) int
func (*CachedTokenCounter) CountBytes ¶
func (c *CachedTokenCounter) CountBytes(data []byte) int
func (*CachedTokenCounter) CountMessage ¶
func (c *CachedTokenCounter) CountMessage(msg core.Message) int
type Memory ¶
type Memory interface {
// Add appends a single message to the history.
Add(ctx context.Context, message core.Message) error
// AddBatch appends multiple messages at once (useful for initialization).
AddBatch(ctx context.Context, messages []core.Message) error
// History retrieves the current state of conversation.
// It allows optional fetching of limited entries (e.g., last N messages).
History(ctx context.Context) ([]core.Message, error)
// HistoryLast retrieves only the most recent k messages.
// This is O(k) optimized for Routing and Context Construction.
// If k <= 0, it behaves like History().
HistoryLast(ctx context.Context, k int) ([]core.Message, error) // <--- NEW
// PruneRatio reduces memory usage proportionally (0.0 - 1.0).
// Example: ratio=0.3 means deleting 30% of existing tokens (prioritizing the oldest messages).
// Returns the number of tokens freed.
PruneRatio(ctx context.Context, ratio float64) (int, error)
// Clear resets the memory state.
Clear(ctx context.Context) error
}
Memory defines the contract for storing conversation history. It follows the "State Control" pillar, allowing standardized access to agent state.
type RedisMemory ¶
type RedisMemory struct {
// contains filtered or unexported fields
}
RedisMemory implements memory.Memory interface using Redis lists.
func NewRedisMemory ¶
NewRedisMemory creates a new persistent memory instance.
func (*RedisMemory) Clear ¶
func (r *RedisMemory) Clear(ctx context.Context) error
Clear deletes the conversation history.
func (*RedisMemory) HistoryLast ¶
HistoryLast retrieves the last k messages efficiently.
func (*RedisMemory) PruneRatio ¶
PruneRatio removes the oldest messages to free up space.
type SimpleTokenCounter ¶
type SimpleTokenCounter struct {
// contains filtered or unexported fields
}
func NewSimpleTokenCounter ¶
func NewSimpleTokenCounter(averageCharsPerToken float64) *SimpleTokenCounter
NewSimpleTokenCounter creates a fast estimator. averageBytesPerToken: - ~3.0 - 3.5: Safe estimate for Mixed content (English + Vietnamese) - ~4.0: English only
func (*SimpleTokenCounter) Count ¶
func (c *SimpleTokenCounter) Count(text string) int
Count estimates tokens based on BYTE length.
func (*SimpleTokenCounter) CountBytes ¶
func (c *SimpleTokenCounter) CountBytes(data []byte) int
func (*SimpleTokenCounter) CountMessage ¶
func (c *SimpleTokenCounter) CountMessage(msg core.Message) int
CountMessage calculates overhead + content + tool calls
type TokenCounter ¶
type TokenCounter interface {
Count(text string) int
CountBytes(data []byte) int
CountMessage(msg core.Message) int
}
TokenCounter defines how to count tokens for a message.
type WordTokenCounter ¶
type WordTokenCounter struct{}
func NewWordTokenCounter ¶
func NewWordTokenCounter() *WordTokenCounter
func (*WordTokenCounter) Count ¶
func (c *WordTokenCounter) Count(text string) int
func (*WordTokenCounter) CountBytes ¶
func (c *WordTokenCounter) CountBytes(data []byte) int
func (*WordTokenCounter) CountMessage ¶
func (c *WordTokenCounter) CountMessage(msg core.Message) int