Documentation
¶
Index ¶
- func Cleanup(store Store, maxAge time.Duration, archiveFn func(*Session) error) int
- func NeedsMemoryContext(text string) bool
- type Compactor
- type Fact
- type FileStore
- type FunctionCall
- type InMemoryStore
- func (m *InMemoryStore) AddFacts(key string, facts []Fact)
- func (m *InMemoryStore) AddMessage(key string, msg Message)
- func (m *InMemoryStore) Clear(key string)
- func (m *InMemoryStore) CompactMessages(key string, keepLast int) []Message
- func (m *InMemoryStore) Delete(key string) error
- func (m *InMemoryStore) GetFacts(key string) []Fact
- func (m *InMemoryStore) GetHistory(key string) []Message
- func (m *InMemoryStore) GetOrCreate(key string) *Session
- func (m *InMemoryStore) GetSummary(key string) string
- func (m *InMemoryStore) ListStale(maxAge time.Duration) []string
- func (m *InMemoryStore) MessageCount(key string) int
- func (m *InMemoryStore) Save(_ string) error
- func (m *InMemoryStore) SetSummary(key, summary string)
- func (m *InMemoryStore) TruncateHistory(key string, keepLast int)
- func (m *InMemoryStore) UpdateLastMessage(key string, content string)
- type Message
- type Options
- type Session
- type Store
- type SummarizeFn
- type ToolCall
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cleanup ¶
Cleanup removes stale sessions from the store. Sessions older than maxAge are archived (if archiveFn is provided) and then deleted. Returns the number of deleted sessions. If maxAge <= 0, cleanup is disabled.
func NeedsMemoryContext ¶
NeedsMemoryContext returns true if the text is substantive enough to warrant loading conversation memory from MemDB.
Types ¶
type Compactor ¶
type Compactor struct {
Store Store
Summarize SummarizeFn
Threshold int // trigger when MessageCount >= this
KeepLast int // messages to retain
ExtractFacts bool // true = parse "- " bullets as facts
MultiPart bool // split large histories before summarizing
MultiPartMin int // minimum messages for multi-part split (default: 10)
MaxTokensGuard int // skip messages with len(Content)/4 > this
}
Compactor manages session compaction.
type FileStore ¶
type FileStore struct {
*InMemoryStore
// contains filtered or unexported fields
}
FileStore wraps InMemoryStore with JSON file persistence.
type FunctionCall ¶
FunctionCall is the OpenAI-style nested function call format.
type InMemoryStore ¶
type InMemoryStore struct {
// contains filtered or unexported fields
}
InMemoryStore is a thread-safe in-memory session store. Uses per-key locking for better concurrency under multi-session load.
func NewInMemoryStore ¶
func NewInMemoryStore(opts Options) *InMemoryStore
NewInMemoryStore creates a new in-memory store.
func (*InMemoryStore) AddFacts ¶
func (m *InMemoryStore) AddFacts(key string, facts []Fact)
AddFacts appends facts to a session, enforcing MaxFacts.
func (*InMemoryStore) AddMessage ¶
func (m *InMemoryStore) AddMessage(key string, msg Message)
AddMessage appends a message, auto-creating the session if needed.
func (*InMemoryStore) Clear ¶
func (m *InMemoryStore) Clear(key string)
Clear resets a session's messages, summary, and facts.
func (*InMemoryStore) CompactMessages ¶
func (m *InMemoryStore) CompactMessages(key string, keepLast int) []Message
CompactMessages extracts oldest messages, keeping keepLast.
func (*InMemoryStore) Delete ¶
func (m *InMemoryStore) Delete(key string) error
Delete removes a session entirely.
func (*InMemoryStore) GetFacts ¶
func (m *InMemoryStore) GetFacts(key string) []Fact
GetFacts returns a copy of facts.
func (*InMemoryStore) GetHistory ¶
func (m *InMemoryStore) GetHistory(key string) []Message
GetHistory returns a copy of messages, or nil for unknown/expired keys.
func (*InMemoryStore) GetOrCreate ¶
func (m *InMemoryStore) GetOrCreate(key string) *Session
GetOrCreate returns an existing session or creates a new one.
func (*InMemoryStore) GetSummary ¶
func (m *InMemoryStore) GetSummary(key string) string
GetSummary returns the summary, or "" for unknown keys.
func (*InMemoryStore) ListStale ¶
func (m *InMemoryStore) ListStale(maxAge time.Duration) []string
ListStale returns keys where Updated is older than maxAge.
func (*InMemoryStore) MessageCount ¶
func (m *InMemoryStore) MessageCount(key string) int
MessageCount returns the number of messages.
func (*InMemoryStore) Save ¶
func (m *InMemoryStore) Save(_ string) error
Save is a no-op for in-memory store.
func (*InMemoryStore) SetSummary ¶
func (m *InMemoryStore) SetSummary(key, summary string)
SetSummary sets the compaction summary.
func (*InMemoryStore) TruncateHistory ¶
func (m *InMemoryStore) TruncateHistory(key string, keepLast int)
TruncateHistory removes oldest messages, keeping keepLast.
func (*InMemoryStore) UpdateLastMessage ¶
func (m *InMemoryStore) UpdateLastMessage(key string, content string)
UpdateLastMessage replaces the content of the most recent message. Useful for streaming: append chunks to the last assistant message. No-op if the session has no messages.
type Message ¶
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
// ChatTime is the message timestamp in RFC3339-UTC format.
// Use go-kit/llm.FormatChatTime(time.Time) to produce a value
// that round-trips cleanly across kitllm, MemDB, and this store.
ChatTime string `json:"chat_time,omitempty"`
// MessageID is a stable per-message identifier (MemDB dedup key).
MessageID string `json:"message_id,omitempty"`
// Name is an optional speaker label (OpenAI-native; MemDB-honoured).
Name string `json:"name,omitempty"`
}
Message is a provider-agnostic chat message.
ChatTime, MessageID, and Name mirror MemDB's ingest schema and go-kit/llm v0.43+ Message — keeping persisted history aligned with the format used in flight. All three are omitempty: existing session files written before this revision deserialise without changes.
type Options ¶
type Options struct {
TTL time.Duration // 0 = no expiry
MaxMessages int // 0 = unlimited
MaxContentSize int // truncate message content beyond this byte length; 0 = unlimited
MaxFacts int // rotate oldest facts when exceeded; 0 = unlimited
}
Options configures store behavior.
type Session ¶
type Session struct {
Key string `json:"key"`
Messages []Message `json:"messages"`
Summary string `json:"summary,omitempty"`
Facts []Fact `json:"facts,omitempty"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
Session holds conversation state for a single key.
func NewSession ¶
NewSession creates a new session with the given key.
func (*Session) AddMessage ¶
AddMessage appends a message and updates the timestamp.
func (*Session) CompactMessages ¶
CompactMessages extracts the oldest messages, keeping the last keepLast. Returns nil if there are fewer messages than keepLast.
func (*Session) MessageCount ¶
MessageCount returns the number of messages.
func (*Session) TruncateHistory ¶
TruncateHistory removes the oldest messages, keeping the last keepLast.
type Store ¶
type Store interface {
// GetOrCreate returns an existing session or creates a new one.
GetOrCreate(key string) *Session
// AddMessage appends a message to the session's history.
AddMessage(key string, msg Message)
// UpdateLastMessage replaces the content of the most recent message.
// Useful for streaming LLM responses: buffer chunks into the last message.
UpdateLastMessage(key string, content string)
// GetHistory returns an ordered copy of the session's messages.
GetHistory(key string) []Message
// GetSummary returns the compaction summary for a session.
GetSummary(key string) string
// SetSummary stores a compaction summary.
SetSummary(key, summary string)
// GetFacts returns a copy of extracted facts.
GetFacts(key string) []Fact
// AddFacts appends new facts to a session.
AddFacts(key string, facts []Fact)
// MessageCount returns the number of messages in a session.
MessageCount(key string) int
// CompactMessages extracts the oldest messages, keeping keepLast.
// Returns the extracted messages without modifying the caller's view.
CompactMessages(key string, keepLast int) []Message
// TruncateHistory removes the oldest messages, keeping keepLast.
TruncateHistory(key string, keepLast int)
// Clear resets a session's messages, summary, and facts.
Clear(key string)
// Delete removes a session entirely.
Delete(key string) error
// Save persists a session to the backend's storage.
Save(key string) error
// ListStale returns session keys where Updated is older than maxAge.
ListStale(maxAge time.Duration) []string
}
Store manages per-key conversation sessions with pluggable backends.
func NewFileStore ¶
NewFileStore creates a file-backed store, loading existing sessions.
type SummarizeFn ¶
SummarizeFn is called by the compactor to get an LLM summary.