memory

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KindLeaf      = "leaf"
	KindCondensed = "condensed"
)

Summary kind constants.

View Source
const (
	RoleUser      = "user"
	RoleAssistant = "assistant"
	RoleTool      = "tool"
)

Message role constants.

View Source
const (
	ItemTypeMessage = "message"
	ItemTypeSummary = "summary"
)

Context item type constants.

View Source
const (
	EventTypeText       = "text"
	EventTypeMultimodal = "multimodal"
	EventTypeToolCall   = "tool_call"
	EventTypeToolResult = "tool_result"
)

Event type constants for the event_type column.

View Source
const (
	ScopeMessages  = "messages"
	ScopeSummaries = "summaries"
	ScopeBoth      = "both"
)

Search scope constants.

View Source
const (
	DefaultFreshTail        = 20
	DefaultContextThreshold = 0.75
	DefaultLeafChunkSize    = 10 // messages per leaf summary
)

Default configuration values.

Variables

This section is empty.

Functions

func AgentIDFromContext added in v0.8.0

func AgentIDFromContext(ctx context.Context) string

AgentIDFromContext extracts the agent ID from context.

func BuildPrompt

func BuildPrompt(text string, opts SummarizeOptions) string

BuildPrompt constructs the appropriate summarization prompt.

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens returns a rough token count (~4 chars per token). This matches the existing estimator in agent/store/store.go.

func FormatSummaryXML

func FormatSummaryXML(sum sqlc.CtxSummary, parents []sqlc.CtxSummary) string

FormatSummaryXML formats a summary as XML for model consumption.

func SessionIDFromContext

func SessionIDFromContext(ctx context.Context) string

SessionIDFromContext extracts the session ID from context.

func UserIDFromContext added in v0.8.0

func UserIDFromContext(ctx context.Context) int64

UserIDFromContext extracts the user ID from context.

func WithAgentID added in v0.8.0

func WithAgentID(ctx context.Context, agentID string) context.Context

WithAgentID attaches an agent ID to the context.

func WithSessionID

func WithSessionID(ctx context.Context, sessionID string) context.Context

WithSessionID attaches a session ID to the context.

func WithUserID added in v0.8.0

func WithUserID(ctx context.Context, userID int64) context.Context

WithUserID attaches a user ID to the context.

Types

type Assembler

type Assembler struct {
	// contains filtered or unexported fields
}

Assembler builds context for the model within a token budget.

func NewAssembler

func NewAssembler(q *sqlc.Queries) *Assembler

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. The engine owns the DB connection and will close it when Close() is called.

func NewEngineFromDB added in v0.8.0

func NewEngineFromDB(db *sql.DB, summarizer Summarizer, opts ...EngineOption) Engine

NewEngineFromDB creates a new memory engine using an existing *sql.DB. The engine does NOT own the DB connection — Close() will not close it. This is useful when multiple packages share a single database connection.

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

type ExpandChild struct {
	SummaryID string
	Kind      string
	Depth     int
	Content   string
}

ExpandChild is a child summary in an expand result.

type ExpandMessage

type ExpandMessage struct {
	MessageID int64
	Role      string
	Content   string
	CreatedAt time.Time
}

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
	AgentID    string // agent this session belongs to (empty for legacy sessions)
	UserID     int64  // user this session belongs to (0 for legacy sessions)
}

SessionInfo holds metadata about a session stored in the conversations table.

type StaticSummarizer

type StaticSummarizer struct {
	Response string
	Err      error
}

StaticSummarizer always returns a fixed response (for testing).

func (*StaticSummarizer) Summarize

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.

type UserMemoryStore added in v0.8.0

type UserMemoryStore struct {
	// contains filtered or unexported fields
}

UserMemoryStore provides per-user-per-agent memory access.

func NewUserMemoryStore added in v0.8.0

func NewUserMemoryStore(store config.Store) *UserMemoryStore

NewUserMemoryStore creates a new UserMemoryStore.

func (*UserMemoryStore) Get added in v0.8.0

func (s *UserMemoryStore) Get(ctx context.Context, userID int64, agentID string) (string, error)

Get returns the current memory content for a user-agent pair. Returns empty string if no memory exists.

func (*UserMemoryStore) Set added in v0.8.0

func (s *UserMemoryStore) Set(ctx context.Context, userID int64, agentID, content string) error

Set replaces the memory content for a user-agent pair.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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