Documentation
¶
Index ¶
- Constants
- func BuildPrompt(text string, opts SummarizeOptions) string
- func EstimateTokens(text string) int
- func FormatSummaryXML(sum sqlc.Summary, parents []sqlc.Summary) string
- func SessionIDFromContext(ctx context.Context) string
- func WithSessionID(ctx context.Context, sessionID string) context.Context
- type Assembler
- type CompactionEngine
- type CompactionMode
- type CompactionResult
- type DescribeResult
- type Engine
- type EngineOption
- type ExpandChild
- type ExpandMessage
- type ExpandResult
- type GrepResult
- type LLMSummarizer
- type RetrievalEngine
- func (r *RetrievalEngine) Describe(ctx context.Context, summaryID string) (*DescribeResult, error)
- func (r *RetrievalEngine) Expand(ctx context.Context, summaryID string, tokenCap int) (*ExpandResult, error)
- func (r *RetrievalEngine) Grep(ctx context.Context, convID int64, pattern string, scope string, limit int) ([]GrepResult, error)
- func (r *RetrievalEngine) GrepBySession(ctx context.Context, sessionID string, pattern string, scope string, limit int) ([]GrepResult, error)
- type SessionInfo
- type StaticSummarizer
- type SummarizeOptions
- type Summarizer
Constants ¶
const ( KindLeaf = "leaf" KindCondensed = "condensed" )
Summary kind constants.
const ( RoleUser = "user" RoleAssistant = "assistant" RoleTool = "tool" )
Message role constants.
const ( ItemTypeMessage = "message" ItemTypeSummary = "summary" )
Context item type constants.
const ( EventTypeText = "text" EventTypeMultimodal = "multimodal" EventTypeToolCall = "tool_call" EventTypeToolResult = "tool_result" )
Event type constants for the event_type column.
const ( ScopeMessages = "messages" ScopeSummaries = "summaries" ScopeBoth = "both" )
Search scope constants.
const ( DefaultFreshTail = 20 DefaultContextThreshold = 0.75 DefaultLeafChunkSize = 10 // messages per leaf summary )
Default configuration values.
Variables ¶
This section is empty.
Functions ¶
func BuildPrompt ¶
func BuildPrompt(text string, opts SummarizeOptions) string
BuildPrompt constructs the appropriate summarization prompt.
func EstimateTokens ¶
EstimateTokens returns a rough token count (~4 chars per token). This matches the existing estimator in agent/store/store.go.
func FormatSummaryXML ¶
FormatSummaryXML formats a summary as XML for model consumption.
func SessionIDFromContext ¶
SessionIDFromContext extracts the session ID from context.
Types ¶
type Assembler ¶
type Assembler struct {
// contains filtered or unexported fields
}
Assembler builds context for the model within a token budget.
func NewAssembler ¶
NewAssembler creates a new context assembler.
func (*Assembler) Assemble ¶
func (a *Assembler) Assemble(ctx context.Context, convID int64, budget int, freshTail int) ([]ai.Message, error)
Assemble builds a context window from context_items, respecting the token budget. It protects the freshTail most recent raw messages from being excluded. Returns ai.Messages for direct use by the engine/runner pipeline.
type CompactionEngine ¶
type CompactionEngine struct {
// contains filtered or unexported fields
}
CompactionEngine runs leaf and condensed compaction passes.
func NewCompactionEngine ¶
func NewCompactionEngine(db *sql.DB, q *sqlc.Queries, summarizer Summarizer, freshTail int) *CompactionEngine
NewCompactionEngine creates a new compaction engine.
func (*CompactionEngine) Compact ¶
func (c *CompactionEngine) Compact(ctx context.Context, convID int64, mode CompactionMode) (*CompactionResult, error)
Compact runs compaction on a conversation based on the given mode.
type CompactionMode ¶
type CompactionMode int
CompactionMode controls compaction behavior.
const ( // CompactionIncremental runs a single leaf pass + optional condensation. CompactionIncremental CompactionMode = iota // CompactionFull runs repeated passes until no more compaction is possible. CompactionFull )
func (CompactionMode) String ¶
func (m CompactionMode) String() string
type CompactionResult ¶
type CompactionResult struct {
LeafSummariesCreated int
CondensedSummariesCreated int
MessagesCompacted int
TokensBefore int
TokensAfter int
Duration time.Duration
}
CompactionResult reports what a compaction cycle accomplished.
type DescribeResult ¶
type DescribeResult struct {
SummaryID string
Kind string
Depth int
Content string
EarliestAt *time.Time
LatestAt *time.Time
DescendantCount int
ParentIDs []string
ChildIDs []string
}
DescribeResult represents summary metadata from memory_describe.
type Engine ¶
type Engine interface {
// Bootstrap reconciles session state on startup.
Bootstrap(ctx context.Context, sessionID string) error
// Ingest persists a message and appends to context.
Ingest(ctx context.Context, sessionID string, msg ai.Message) error
// IngestBatch persists multiple messages.
IngestBatch(ctx context.Context, sessionID string, msgs []ai.Message) error
// Assemble builds context for the model within token budget.
Assemble(ctx context.Context, sessionID string, budget int, freshTail int) ([]ai.Message, error)
// Compact runs compaction passes (leaf + optional condensation).
Compact(ctx context.Context, sessionID string, mode CompactionMode) (*CompactionResult, error)
// NeedsCompaction checks if compaction should run based on token threshold.
NeedsCompaction(ctx context.Context, sessionID string, threshold float64) bool
// SaveInfo persists session metadata (upsert).
SaveInfo(ctx context.Context, info SessionInfo) error
// LoadInfo retrieves session metadata by session ID.
LoadInfo(ctx context.Context, sessionID string) (SessionInfo, error)
// ListInfo lists session metadata. If includeArchived is false, archived sessions are excluded.
ListInfo(ctx context.Context, includeArchived bool) ([]SessionInfo, error)
// Load returns the full event history for a session.
Load(ctx context.Context, sessionID string) ([]ai.Message, error)
// Retrieval returns the retrieval engine for tools.
Retrieval() *RetrievalEngine
// Close releases database resources.
Close() error
}
Engine is the main interface for lossless context management.
func NewEngine ¶
func NewEngine(dbPath string, summarizer Summarizer, opts ...EngineOption) (Engine, error)
NewEngine creates a new memory engine backed by a SQLite database at dbPath.
type EngineOption ¶
type EngineOption func(*engine)
EngineOption configures the engine.
func WithFreshTail ¶
func WithFreshTail(n int) EngineOption
WithFreshTail sets the number of recent messages protected from compaction.
func WithLogger ¶
func WithLogger(log *slog.Logger) EngineOption
WithLogger sets the logger for the engine.
type ExpandChild ¶
ExpandChild is a child summary in an expand result.
type ExpandMessage ¶
ExpandMessage is a source message in an expand result.
type ExpandResult ¶
type ExpandResult struct {
SummaryID string
Children []ExpandChild
Messages []ExpandMessage
}
ExpandResult represents drill-down results from memory_expand.
type GrepResult ¶
type GrepResult struct {
SourceType string // "message" or "summary"
SourceID string
Content string
Relevance float64 // reserved for future scoring; currently 0
Timestamp time.Time
}
GrepResult represents a single search hit from memory_grep.
type LLMSummarizer ¶
type LLMSummarizer struct {
// Generate calls the LLM with the given prompt and returns the response text.
Generate func(ctx context.Context, prompt string) (string, error)
}
LLMSummarizer generates summaries using an LLM via a callback function.
func (*LLMSummarizer) Summarize ¶
func (s *LLMSummarizer) Summarize(ctx context.Context, text string, opts SummarizeOptions) (string, error)
Summarize generates a summary with escalation: normal → aggressive → deterministic fallback.
type RetrievalEngine ¶
type RetrievalEngine struct {
// contains filtered or unexported fields
}
RetrievalEngine provides search and exploration of compacted history.
func NewRetrievalEngine ¶
func NewRetrievalEngine(q *sqlc.Queries) *RetrievalEngine
NewRetrievalEngine creates a RetrievalEngine from a Queries instance.
func (*RetrievalEngine) Describe ¶
func (r *RetrievalEngine) Describe(ctx context.Context, summaryID string) (*DescribeResult, error)
Describe returns metadata and lineage for a summary.
func (*RetrievalEngine) Expand ¶
func (r *RetrievalEngine) Expand(ctx context.Context, summaryID string, tokenCap int) (*ExpandResult, error)
Expand drills into a summary, returning either source messages (leaf) or child summaries (condensed), up to tokenCap tokens.
func (*RetrievalEngine) Grep ¶
func (r *RetrievalEngine) Grep(ctx context.Context, convID int64, pattern string, scope string, limit int) ([]GrepResult, error)
Grep searches messages and/or summaries for a LIKE pattern. scope is "messages", "summaries", or "both" (default).
func (*RetrievalEngine) GrepBySession ¶
func (r *RetrievalEngine) GrepBySession(ctx context.Context, sessionID string, pattern string, scope string, limit int) ([]GrepResult, error)
GrepBySession searches messages and/or summaries for a LIKE pattern, resolving the conversation ID from a session ID.
type SessionInfo ¶
type SessionInfo struct {
ID string
Channel string
Title string
CreatedAt time.Time
LastActive time.Time
Archived bool
}
SessionInfo holds metadata about a session stored in the conversations table.
type StaticSummarizer ¶
StaticSummarizer always returns a fixed response (for testing).
func (*StaticSummarizer) Summarize ¶
func (s *StaticSummarizer) Summarize(_ context.Context, _ string, _ SummarizeOptions) (string, error)
type SummarizeOptions ¶
type SummarizeOptions struct {
IsCondensed bool
Depth int
Aggressive bool
Previous string // previous summary for continuity
TargetTokens int
}
SummarizeOptions controls summarization behavior.
type Summarizer ¶
type Summarizer interface {
Summarize(ctx context.Context, text string, opts SummarizeOptions) (string, error)
}
Summarizer generates summaries from content.