Documentation
¶
Index ¶
- type BranchInfo
- type Cache
- func (c *Cache) Clear()
- func (c *Cache) Close()
- func (c *Cache) Contains(key string) bool
- func (c *Cache) Delete(key string)
- func (c *Cache) Get(key string) (*Session, bool)
- func (c *Cache) GetNewest() (*CachedSession, bool)
- func (c *Cache) GetOldest() (*CachedSession, bool)
- func (c *Cache) GetSession(key string) (*Session, bool)
- func (c *Cache) HitRate() float64
- func (c *Cache) Keys() []string
- func (c *Cache) PruneByAccessCount(minAccessCount int) int
- func (c *Cache) PruneBySize(maxTotalSize int) int
- func (c *Cache) RefreshTTL(key string, ttl time.Duration) bool
- func (c *Cache) Set(key string, session *Session)
- func (c *Cache) SetWithTTL(key string, session *Session, ttl time.Duration)
- func (c *Cache) Size() int
- func (c *Cache) Stats() CacheStats
- type CacheConfig
- type CacheStats
- type CachedSession
- type Manager
- type Media
- type Message
- type PruneConfig
- type PruneStats
- type PruneStrategy
- type Pruner
- func (p *Pruner) Cleanup() error
- func (p *Pruner) CompactSession(sessionKey string) error
- func (p *Pruner) EstimateMessages(sessionKey string) int
- func (p *Pruner) GetConfig() PruneConfig
- func (p *Pruner) GetStats() PruneStats
- func (p *Pruner) PruneByType(sessionKey string, isDM bool) error
- func (p *Pruner) PruneMessages(sessionKey string, preserveCount int) error
- func (p *Pruner) PruneMessagesByTTL(sessionKey string) error
- func (p *Pruner) PruneSessions() error
- func (p *Pruner) SetConfig(config PruneConfig)
- func (p *Pruner) ShouldCompact(sessionKey string, estimatedTokens int) bool
- type Session
- type SessionDiff
- type SessionNode
- type SessionTree
- func (t *SessionTree) CompareSessions(id1, id2 string) (*SessionDiff, error)
- func (t *SessionTree) CountNodes() int
- func (t *SessionTree) CreateBranch(parentID string, branchSession *Session, branchName string, createdBy string) (string, error)
- func (t *SessionTree) DeleteNode(id string, recursive bool) error
- func (t *SessionTree) FindNodesByBranchName(name string) []*SessionNode
- func (t *SessionTree) GetBranches() []*SessionNode
- func (t *SessionTree) GetChildren(id string) ([]*SessionNode, error)
- func (t *SessionTree) GetMaxDepth() int
- func (t *SessionTree) GetNode(id string) (*SessionNode, error)
- func (t *SessionTree) GetPath(id string) ([]*SessionNode, error)
- func (t *SessionTree) GetRoot() (*SessionNode, error)
- func (t *SessionTree) GetStatistics() *TreeStatistics
- func (t *SessionTree) ListNodes() []*SessionNode
- func (t *SessionTree) MergeBranch(branchID string) error
- func (t *SessionTree) SetMaxDepth(depth int)
- func (t *SessionTree) SwitchBranch(fromID, toID string) (*Session, error)
- type ToolCall
- type TreeStatistics
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BranchInfo ¶
type BranchInfo struct {
Name string `json:"name"` // Human-readable branch name
Description string `json:"description"` // Branch description
IsMain bool `json:"is_main"` // Whether this is the main branch
IsMerged bool `json:"is_merged"` // Whether this branch was merged back
MergedFrom []string `json:"merged_from"` // List of branch IDs that were merged
CreatedAt time.Time `json:"created_at"`
CreatedBy string `json:"created_by"` // What created this branch (user, system, auto)
}
BranchInfo contains metadata about a branch
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache implements an LRU cache with TTL for sessions
func (*Cache) GetNewest ¶
func (c *Cache) GetNewest() (*CachedSession, bool)
GetNewest returns the newest cached session
func (*Cache) GetOldest ¶
func (c *Cache) GetOldest() (*CachedSession, bool)
GetOldest returns the oldest cached session
func (*Cache) GetSession ¶
GetSession retrieves cached session without updating access time
func (*Cache) PruneByAccessCount ¶
PruneByAccessCount removes sessions with access count below threshold
func (*Cache) PruneBySize ¶
PruneBySize removes sessions until total size is under the limit
func (*Cache) RefreshTTL ¶
RefreshTTL refreshes the TTL for a cached session
func (*Cache) SetWithTTL ¶
SetWithTTL adds or updates a session with a specific TTL
type CacheConfig ¶
type CacheConfig struct {
MaxSize int // Maximum number of sessions to cache
DefaultTTL time.Duration // Default TTL for cache entries
CleanupIntvl time.Duration // Interval for cleanup routine
}
CacheConfig configures the session cache
func DefaultCacheConfig ¶
func DefaultCacheConfig() CacheConfig
DefaultCacheConfig returns default cache configuration
type CacheStats ¶
type CacheStats struct {
Hits int64
Misses int64
Evictions int64
Expirations int64
Size int
MaxSize int
}
CacheStats contains cache statistics
type CachedSession ¶
type CachedSession struct {
Session *Session
AccessCount int
CreatedAt time.Time
LastAccessed time.Time
ExpiresAt time.Time
Size int // Estimated size in bytes
}
CachedSession represents a cached session with metadata
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 会话管理器
func (*Manager) GetOrCreate ¶
GetOrCreate 获取或创建会话
type Media ¶
type Media struct {
Type string `json:"type"` // image, video, audio, document
URL string `json:"url"` // 文件URL
Base64 string `json:"base64,omitempty"` // Base64编码内容
MimeType string `json:"mimetype"` // MIME类型
}
Media 媒体文件
type Message ¶
type Message struct {
Role string `json:"role"` // user, assistant, system, tool
Content string `json:"content"`
Media []Media `json:"media,omitempty"`
Timestamp time.Time `json:"timestamp"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"` // For tool role
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // For assistant role
}
Message 消息
type PruneConfig ¶
type PruneConfig struct {
Strategy PruneStrategy
MaxTotalSessions int
MaxTotalMessages int
MaxTotalTokens int
DefaultMessageTTL time.Duration
DMPreserveCount int // Minimum messages to preserve in DM
GroupPreserveCount int // Minimum messages to preserve in group
}
PruneConfig configures session pruning behavior
func DefaultPruneConfig ¶
func DefaultPruneConfig() PruneConfig
DefaultPruneConfig returns default pruning configuration
type PruneStats ¶
type PruneStats struct {
TotalPrunes int64
MessagesPruned int64
SessionsPruned int64
TokensReclaimed int64
LastPruneAt time.Time
}
PruneStats contains pruning statistics
type PruneStrategy ¶
type PruneStrategy string
PruneStrategy defines how sessions should be pruned
const ( // PruneStrategyLRU removes least recently used sessions PruneStrategyLRU PruneStrategy = "lru" // PruneStrategyLFU removes least frequently used sessions PruneStrategyLFU PruneStrategy = "lfu" // PruneStrategyTTL removes sessions past their TTL PruneStrategyTTL PruneStrategy = "ttl" // PruneStrategySemantic uses semantic similarity to deduplicate PruneStrategySemantic PruneStrategy = "semantic" // PruneStrategySize removes largest sessions first PruneStrategySize PruneStrategy = "size" )
type Pruner ¶
type Pruner struct {
// contains filtered or unexported fields
}
Pruner manages session pruning operations
func NewPruner ¶
func NewPruner(manager *Manager, config PruneConfig) *Pruner
NewPruner creates a new session pruner
func (*Pruner) CompactSession ¶
CompactSession compacts a session by summarizing older messages
func (*Pruner) EstimateMessages ¶
EstimateTokens estimates the token count for messages
func (*Pruner) GetConfig ¶
func (p *Pruner) GetConfig() PruneConfig
GetConfig returns the current prune configuration
func (*Pruner) GetStats ¶
func (p *Pruner) GetStats() PruneStats
GetStats returns pruning statistics
func (*Pruner) PruneByType ¶
PruneByType prunes messages based on session type (DM vs Group)
func (*Pruner) PruneMessages ¶
PruneMessages prunes messages within a session based on TTL
func (*Pruner) PruneMessagesByTTL ¶
PruneMessagesByTTL prunes messages based on their time-to-live
func (*Pruner) PruneSessions ¶
PruneSessions prunes sessions based on the configured strategy
func (*Pruner) SetConfig ¶
func (p *Pruner) SetConfig(config PruneConfig)
SetConfig updates the prune configuration
type Session ¶
type Session struct {
Key string `json:"key"`
Messages []Message `json:"messages"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
// contains filtered or unexported fields
}
Session 会话
func (*Session) GetHistory ¶
GetHistory 获取历史消息
func (*Session) GetHistorySafe ¶ added in v0.3.1
GetHistorySafe 获取历史消息,确保不会在工具调用中间截断 这样可以保证消息的完整性和顺序
type SessionDiff ¶
type SessionDiff struct {
ID1 string `json:"id1"`
ID2 string `json:"id2"`
Messages1 int `json:"messages1"`
Messages2 int `json:"messages2"`
AddedMessages int `json:"added_messages"`
RemovedMessages int `json:"removed_messages"`
AddedContent []Message `json:"added_content,omitempty"`
RemovedContent []Message `json:"removed_content,omitempty"`
}
SessionDiff represents differences between two sessions
type SessionNode ¶
type SessionNode struct {
ID string `json:"id"`
Session *Session `json:"session"`
ParentID string `json:"parent_id,omitempty"`
ChildIDs []string `json:"child_ids,omitempty"`
BranchInfo *BranchInfo `json:"branch_info,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
SessionNode represents a node in the session tree
type SessionTree ¶
type SessionTree struct {
// contains filtered or unexported fields
}
SessionTree manages a tree structure of sessions with branching
func NewSessionTree ¶
func NewSessionTree(rootSession *Session) (*SessionTree, error)
NewSessionTree creates a new session tree
func (*SessionTree) CompareSessions ¶
func (t *SessionTree) CompareSessions(id1, id2 string) (*SessionDiff, error)
CompareSessions compares two sessions and returns differences
func (*SessionTree) CountNodes ¶
func (t *SessionTree) CountNodes() int
CountNodes returns the total number of nodes
func (*SessionTree) CreateBranch ¶
func (t *SessionTree) CreateBranch(parentID string, branchSession *Session, branchName string, createdBy string) (string, error)
CreateBranch creates a new branch from the current session state
func (*SessionTree) DeleteNode ¶
func (t *SessionTree) DeleteNode(id string, recursive bool) error
DeleteNode removes a node and its subtree
func (*SessionTree) FindNodesByBranchName ¶
func (t *SessionTree) FindNodesByBranchName(name string) []*SessionNode
FindNodesByBranchName finds nodes with matching branch name
func (*SessionTree) GetBranches ¶
func (t *SessionTree) GetBranches() []*SessionNode
GetBranches returns all branches (non-main nodes)
func (*SessionTree) GetChildren ¶
func (t *SessionTree) GetChildren(id string) ([]*SessionNode, error)
GetChildren returns all children of a node
func (*SessionTree) GetMaxDepth ¶
func (t *SessionTree) GetMaxDepth() int
GetMaxDepth returns the maximum branch depth
func (*SessionTree) GetNode ¶
func (t *SessionTree) GetNode(id string) (*SessionNode, error)
GetNode retrieves a node by ID
func (*SessionTree) GetPath ¶
func (t *SessionTree) GetPath(id string) ([]*SessionNode, error)
GetPath returns the path from root to the specified node
func (*SessionTree) GetRoot ¶
func (t *SessionTree) GetRoot() (*SessionNode, error)
GetRoot returns the root node
func (*SessionTree) GetStatistics ¶
func (t *SessionTree) GetStatistics() *TreeStatistics
GetStatistics returns statistics about the session tree
func (*SessionTree) ListNodes ¶
func (t *SessionTree) ListNodes() []*SessionNode
ListNodes returns all nodes in the tree
func (*SessionTree) MergeBranch ¶
func (t *SessionTree) MergeBranch(branchID string) error
MergeBranch merges a branch back into its parent
func (*SessionTree) SetMaxDepth ¶
func (t *SessionTree) SetMaxDepth(depth int)
SetMaxDepth sets the maximum branch depth
func (*SessionTree) SwitchBranch ¶
func (t *SessionTree) SwitchBranch(fromID, toID string) (*Session, error)
SwitchBranch switches the active branch to a different branch