memory

package
v1.0.0-alpha.36 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compactor

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

func NewCompactor

func NewCompactor(cfg Config, logger *zap.Logger) *Compactor

func (*Compactor) Compact

func (c *Compactor) Compact(
	ctx context.Context, model fantasy.LanguageModel,
	store *Store, sessionID string,
) error

Compact narrows the LLM-facing message window when it grows past the configured caps. Non-destructive: the raw history stays on disk for the UI's ListMessages view. The summarize strategy generates a one-paragraph recap and inserts it as a system row; the slide strategy just hides the older rows without a recap.

Both paths flip visible_to_model = 0 on older rows. The next GetMessages call (model history load) returns only the recent window + the synthetic summary, but ListMessages still shows everything — the operator can scroll back through the full transcript with a "collapsed" hint on hidden rows.

type Config

type Config struct {
	Strategy    string
	MaxMessages int
	MaxTokens   int
}

type SessionInfo

type SessionInfo struct {
	ID           string
	MessageCount int
	LastActive   int64
}

type Store

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

func NewStore

func NewStore(ctx context.Context, db *sql.DB) (*Store, error)

func (*Store) AppendAssistantStub

func (s *Store) AppendAssistantStub(ctx context.Context, sessionID string) (int64, error)

AppendAssistantStub inserts an empty assistant row up front and returns its id. The streaming path updates the row via UpdateBranches after every callback event (tool call, tool result, text delta, step finish) — so a runtime crash mid-stream (self_reload, kill, OOM) leaves a partial-but-loadable assistant turn in the daemon's message store instead of a ghost row that only carries the user prompt.

Content defaults to "[]" so the StoredPart array deserialises cleanly even if the runtime dies before the first event fires.

func (*Store) CountMessages

func (s *Store) CountMessages(ctx context.Context, sessionID string) (int, error)

func (*Store) DeleteSession

func (s *Store) DeleteSession(ctx context.Context, sessionID string) error

func (*Store) GetMessages

func (s *Store) GetMessages(ctx context.Context, sessionID string) ([]fantasy.Message, error)

func (*Store) HideAndAppendSummary

func (s *Store) HideAndAppendSummary(ctx context.Context, sessionID, summary string) error

HideAndAppendSummary marks every currently-visible row in the session as hidden from the model, then inserts a synthetic system-role row carrying the summary. Used by the compactor to shrink the model's prompt window WITHOUT destroying the raw history the UI reads via ListMessages.

The summary row is visible (and is the only "old context" the model sees on subsequent turns). Plain-summary content goes in as-is; the UI renders it like any other system message.

Idempotent in the sense that re-running with the same summary hides nothing new (everything's already hidden) and inserts a second summary row — callers control the cadence via the compactor's shouldCompact check.

func (*Store) HideOlder

func (s *Store) HideOlder(ctx context.Context, sessionID string, keep int) error

HideOlder marks all but the N most recent visible rows as hidden. Used by the sliding-window compaction strategy when summarization isn't desired (or fails to fall through). UI keeps every row; only the model's GetMessages window narrows.

func (*Store) LastAssistantMessage

func (s *Store) LastAssistantMessage(ctx context.Context, sessionID string) (StoredMessage, error)

LastAssistantMessage returns the most recent assistant row for sessionID, or sql.ErrNoRows if none exists. Used by the regenerate path to append a branch onto the prior turn instead of inserting a new row.

func (*Store) ListMessages

func (s *Store) ListMessages(ctx context.Context, sessionID string) ([]StoredMessage, error)

ListMessages returns the persisted messages for sessionID with their stored timestamps. Order ASC, capped at 50 rows like GetMessages.

func (*Store) ListSessions

func (s *Store) ListSessions(ctx context.Context) ([]SessionInfo, error)

func (*Store) ReplaceMessages

func (s *Store) ReplaceMessages(ctx context.Context, sessionID string, messages []fantasy.Message) error

func (*Store) SaveMessage

func (s *Store) SaveMessage(ctx context.Context, sessionID, role, content string) error

func (*Store) UpdateBranches

func (s *Store) UpdateBranches(ctx context.Context, id int64, content, branchesJSON string, active int) error

UpdateBranches rewrites a single message's content + branches + active index. Used by the regenerate path: the old content slides into branches, the new parts become the new content, and active flips to point at it.

type StoredMessage

type StoredMessage struct {
	ID           int64
	Role         string
	Content      string
	BranchesJSON string
	ActiveBranch int
	CreatedAt    time.Time
	// VisibleToModel is false when compaction has hidden the row
	// from the LLM's GetMessages window. The UI still renders the
	// row (with a "collapsed" marker) so the operator can see the
	// full conversation history regardless of compaction state.
	VisibleToModel bool
}

StoredMessage is the timestamp-bearing shape used by ListMessages. Used by the runtime's gRPC ListSessionMessages path so the dashboard can render real wall-clock timestamps on hydrated history.

Content shapes (alpha — no retro compat):

  • user role: plain prompt text.
  • assistant role: JSON-encoded array of "parts" representing the active branch (text + tool blocks). BranchesJSON, when non-empty, is a JSON-encoded array of alternative parts arrays produced by Regenerate.

Jump to

Keyboard shortcuts

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