Help us shape the future of Go! Take the 2026 Go Developer Survey

memory

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MigrateFromJSON

func MigrateFromJSON(
	ctx context.Context, sessionsDir string, store Store,
) (int, error)

MigrateFromJSON reads legacy sessions/*.json files from sessionsDir, writes them into the Store, and renames each migrated file to .json.migrated as a backup. Returns the number of sessions migrated.

Files that fail to parse are logged and skipped. Already-migrated files (.json.migrated) are ignored, making the function idempotent.

Types

type JSONLStore

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

JSONLStore implements Store using append-only JSONL files.

Each session is stored as two files:

{sanitized_key}.jsonl      — one JSON-encoded message per line, append-only
{sanitized_key}.meta.json  — session metadata (summary, logical truncation offset)

Messages are never physically deleted from the JSONL file. Instead, TruncateHistory records a "skip" offset in the metadata file and GetHistory ignores lines before that offset. This keeps all writes append-only, which is both fast and crash-safe.

func NewJSONLStore

func NewJSONLStore(dir string) (*JSONLStore, error)

NewJSONLStore creates a new JSONL-backed store rooted at dir.

func (*JSONLStore) AddFullMessage

func (s *JSONLStore) AddFullMessage(
	_ context.Context, sessionKey string, msg providers.Message,
) error

func (*JSONLStore) AddMessage

func (s *JSONLStore) AddMessage(
	_ context.Context, sessionKey, role, content string,
) error

func (*JSONLStore) Close

func (s *JSONLStore) Close() error

func (*JSONLStore) Compact

func (s *JSONLStore) Compact(
	_ context.Context, sessionKey string,
) error

Compact physically rewrites the JSONL file, dropping all logically skipped lines. This reclaims disk space that accumulates after repeated TruncateHistory calls.

It is safe to call at any time; if there is nothing to compact (skip == 0) the method returns immediately.

func (*JSONLStore) GetHistory

func (s *JSONLStore) GetHistory(
	_ context.Context, sessionKey string,
) ([]providers.Message, error)

func (*JSONLStore) GetSessionMeta added in v0.2.7

func (s *JSONLStore) GetSessionMeta(_ context.Context, sessionKey string) (SessionMeta, error)

GetSessionMeta returns the current metadata snapshot for sessionKey.

func (*JSONLStore) GetSummary

func (s *JSONLStore) GetSummary(
	_ context.Context, sessionKey string,
) (string, error)

func (*JSONLStore) ListSessions added in v0.2.6

func (s *JSONLStore) ListSessions() []string

ListSessions returns all known session keys by reading .meta.json files.

func (*JSONLStore) PromoteAliasHistory added in v0.2.7

func (s *JSONLStore) PromoteAliasHistory(
	_ context.Context,
	sessionKey string,
	scope json.RawMessage,
	aliases []string,
) (bool, error)

PromoteAliasHistory atomically promotes the first non-empty alias session into the canonical session when the canonical session is still empty.

Main-session aliases (e.g. "agent:main:main" or its opaque form) are skipped during promotion. The main session is a shared global fallback and promoting its history into individual sessions would attach stale messages to every new Web UI session (issue #2972).

func (*JSONLStore) ResolveSessionKey added in v0.2.7

func (s *JSONLStore) ResolveSessionKey(_ context.Context, sessionKey string) (string, bool, error)

ResolveSessionKey returns the canonical session key for a candidate key. It short-circuits direct canonical keys when possible, then scans metadata once to resolve aliases or canonical metadata keys.

func (*JSONLStore) SetHistory

func (s *JSONLStore) SetHistory(
	_ context.Context,
	sessionKey string,
	history []providers.Message,
) error

func (*JSONLStore) SetSummary

func (s *JSONLStore) SetSummary(
	_ context.Context, sessionKey, summary string,
) error

func (*JSONLStore) TruncateHistory

func (s *JSONLStore) TruncateHistory(
	_ context.Context, sessionKey string, keepLast int,
) error

func (*JSONLStore) UpsertSessionMeta added in v0.2.7

func (s *JSONLStore) UpsertSessionMeta(
	_ context.Context,
	sessionKey string,
	scope json.RawMessage,
	aliases []string,
) error

UpsertSessionMeta stores structured session metadata while preserving summary/count/skip timestamps maintained by the core JSONL store.

type SessionMeta added in v0.2.7

type SessionMeta struct {
	Key       string          `json:"key"`
	Summary   string          `json:"summary"`
	Skip      int             `json:"skip"`
	Count     int             `json:"count"`
	CreatedAt time.Time       `json:"created_at"`
	UpdatedAt time.Time       `json:"updated_at"`
	Scope     json.RawMessage `json:"scope,omitempty"`
	Aliases   []string        `json:"aliases,omitempty"`
}

SessionMeta holds per-session metadata stored in a .meta.json file.

Scope is stored as raw JSON so pkg/memory can stay decoupled from the higher-level session package while still preserving structured scope data.

type Store

type Store interface {
	// AddMessage appends a simple text message to a session.
	AddMessage(ctx context.Context, sessionKey, role, content string) error

	// AddFullMessage appends a complete message (with tool calls, etc.) to a session.
	AddFullMessage(ctx context.Context, sessionKey string, msg providers.Message) error

	// GetHistory returns all messages for a session in insertion order.
	// Returns an empty slice (not nil) if the session does not exist.
	GetHistory(ctx context.Context, sessionKey string) ([]providers.Message, error)

	// GetSummary returns the conversation summary for a session.
	// Returns an empty string if no summary exists.
	GetSummary(ctx context.Context, sessionKey string) (string, error)

	// SetSummary updates the conversation summary for a session.
	SetSummary(ctx context.Context, sessionKey, summary string) error

	// TruncateHistory removes all but the last keepLast messages from a session.
	// If keepLast <= 0, all messages are removed.
	TruncateHistory(ctx context.Context, sessionKey string, keepLast int) error

	// SetHistory replaces all messages in a session with the provided history.
	SetHistory(ctx context.Context, sessionKey string, history []providers.Message) error

	// Compact reclaims storage by physically removing logically truncated
	// data. Backends that do not accumulate dead data may return nil.
	Compact(ctx context.Context, sessionKey string) error

	// ListSessions returns all known session keys.
	ListSessions() []string

	// Close releases any resources held by the store.
	Close() error
}

Store defines an interface for persistent session storage. Each method is an atomic operation — there is no separate Save() call.

Jump to

Keyboard shortcuts

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