session

package
v0.97.12 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup(store Store, maxAge time.Duration, archiveFn func(*Session) error) int

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

func NeedsMemoryContext(text string) bool

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.

func (*Compactor) Compact

func (c *Compactor) Compact(ctx context.Context, key string)

Compact compacts the session identified by key.

type Fact

type Fact struct {
	Content     string    `json:"content"`
	ExtractedAt time.Time `json:"extracted_at"`
}

Fact is a single extracted fact from a compacted conversation segment.

type FileStore

type FileStore struct {
	*InMemoryStore
	// contains filtered or unexported fields
}

FileStore wraps InMemoryStore with JSON file persistence.

func (*FileStore) Delete

func (f *FileStore) Delete(key string) error

Delete removes from memory and disk.

func (*FileStore) Save

func (f *FileStore) Save(key string) error

Save persists a session to disk atomically.

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

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

func NewSession(key string) *Session

NewSession creates a new session with the given key.

func (*Session) AddFacts

func (s *Session) AddFacts(facts []Fact)

AddFacts appends facts to the session.

func (*Session) AddMessage

func (s *Session) AddMessage(msg Message)

AddMessage appends a message and updates the timestamp.

func (*Session) Clear

func (s *Session) Clear()

Clear resets messages, summary, and facts.

func (*Session) CompactMessages

func (s *Session) CompactMessages(keepLast int) []Message

CompactMessages extracts the oldest messages, keeping the last keepLast. Returns nil if there are fewer messages than keepLast.

func (*Session) GetFacts

func (s *Session) GetFacts() []Fact

GetFacts returns a copy of the facts slice.

func (*Session) MessageCount

func (s *Session) MessageCount() int

MessageCount returns the number of messages.

func (*Session) TruncateHistory

func (s *Session) TruncateHistory(keepLast int)

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

func NewFileStore(dir string, opts Options) Store

NewFileStore creates a file-backed store, loading existing sessions.

type SummarizeFn

type SummarizeFn func(ctx context.Context, prompt string) (string, error)

SummarizeFn is called by the compactor to get an LLM summary.

type ToolCall

type ToolCall struct {
	ID       string        `json:"id"`
	Name     string        `json:"name,omitempty"`
	Args     string        `json:"arguments,omitempty"`
	Function *FunctionCall `json:"function,omitempty"`
}

ToolCall represents a single tool invocation in a message.

Directories

Path Synopsis
Package redis provides a Redis-backed session store.
Package redis provides a Redis-backed session store.

Jump to

Keyboard shortcuts

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