session

package
v0.0.212 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("session: not found")

ErrNotFound is returned when a lookup or update targets a row that does not exist (e.g., UpdateMessage against a deleted/never-persisted message ID).

Functions

func ExpandShortID added in v0.0.52

func ExpandShortID(shortID string) string

ExpandShortID converts a short ID to a SQL LIKE pattern for prefix matching. Example: "240115-1430" -> "20240115-1430%"

func ExportToMarkdown added in v0.0.56

func ExportToMarkdown(sess *Session, messages []Message, opts ExportOptions) string

ExportToMarkdown exports a session and its messages to a pretty markdown format.

func GetDBPath

func GetDBPath() (string, error)

GetDBPath returns the path to the sessions database.

func GetDataDir

func GetDataDir() (string, error)

GetDataDir returns the XDG data directory for term-llm. Uses $XDG_DATA_HOME if set, otherwise ~/.local/share

func GetHandoverDir added in v0.0.146

func GetHandoverDir(cwd string) (string, error)

GetHandoverDir returns the handover directory for the given working directory. The path is XDG_DATA_HOME/term-llm/handover/<basename>-<sha256[:6]>/ where the hash is computed from the absolute cwd to avoid collisions.

func GetHandoverPath added in v0.0.146

func GetHandoverPath(cwd, date string) (string, error)

GetHandoverPath returns a full handover file path with a deterministic random name like "2026-04-03-amber-creek-bloom.md". The random slug is generated once per call; callers should cache the result for the session.

func IsRandomHandoverName added in v0.0.146

func IsRandomHandoverName(name string) bool

IsRandomHandoverName returns true if the filename matches the random 3-word pattern generated by GetHandoverPath and all three words are from the handoverWords list. This prevents LLM-generated descriptive slugs (e.g. "fix-auth-bug") from being treated as random and renamed repeatedly.

func MaybeRenameHandover added in v0.0.146

func MaybeRenameHandover(ctx context.Context, path string, slugGen HandoverSlugGenerator) error

MaybeRenameHandover checks if the given handover file should be renamed. It renames when the file has a random-word name and contains >= 1000 bytes. After renaming, a symlink is created from the old path to the new file so that the system prompt path remains valid (preserving LLM cache).

slugGen is called to produce the new slug; if nil or if it returns an error, the rename is silently skipped.

func NewID

func NewID() string

NewID generates a unique session ID using a timestamp prefix and random suffix. Format: YYYYMMDD-HHMMSS-RANDOM (e.g., "20240115-143052-a1b2c3") This format:

  • Sorts chronologically by default
  • Is human-readable for debugging
  • Has enough randomness to prevent collisions

func ParseIDTime

func ParseIDTime(id string) time.Time

ParseIDTime extracts the timestamp from a session ID. Returns zero time if parsing fails.

func RandomHandoverSlug added in v0.0.146

func RandomHandoverSlug() string

RandomHandoverSlug returns a string like "amber-creek-bloom" using crypto/rand for word selection.

func ResolveDBPath added in v0.0.85

func ResolveDBPath(pathOverride string) (string, error)

ResolveDBPath resolves an optional DB path override. Empty path uses the default XDG location. Supports :memory: for ephemeral in-memory storage.

func ShortID

func ShortID(id string) string

ShortID returns a shortened version of the session ID for display. Example: "20240115-143052-a1b2c3" -> "240115-1430"

func TruncateSummary

func TruncateSummary(content string) string

TruncateSummary returns the first line of content, truncated to 100 chars.

Types

type Config

type Config struct {
	Enabled    bool   `mapstructure:"enabled"`      // Master switch
	MaxAgeDays int    `mapstructure:"max_age_days"` // Auto-delete after N days (0=never)
	MaxCount   int    `mapstructure:"max_count"`    // Keep at most N sessions (0=unlimited)
	Path       string `mapstructure:"path"`         // Optional DB path override (supports :memory:)
	ReadOnly   bool   `mapstructure:"-"`            // Open DB in read-only mode (skip schema init/cleanup)
}

Config holds session storage configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default session configuration.

type ExportOptions added in v0.0.56

type ExportOptions struct {
	IncludeSystem bool // Include system prompt in export
}

ExportOptions configures session export.

type HandoverSlugGenerator added in v0.0.146

type HandoverSlugGenerator func(ctx context.Context, content string) (string, error)

HandoverSlugGenerator produces a short filesystem-safe slug from document content.

type ListOptions

type ListOptions struct {
	Name           string        // Filter by name
	Provider       string        // Filter by provider
	Model          string        // Filter by model
	Mode           SessionMode   // Filter by mode (chat, ask, plan, exec)
	Status         SessionStatus // Filter by status
	Tag            string        // Filter by tag (substring match)
	Categories     []string      // Sidebar/web categories (all, chat, web, ask, plan, exec)
	Limit          int           // Max results (0 = use default)
	Offset         int           // Pagination offset
	Archived       bool          // Include archived sessions
	SortByActivity bool          // Sort by last_message_at (web sidebar); defaults to last_user_message_at
}

ListOptions configures session listing.

type LoggingStore added in v0.0.41

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

LoggingStore wraps a Store and logs errors instead of silently discarding them. This preserves the best-effort semantics (operations don't fail the caller) while providing visibility into persistence issues.

func NewLoggingStore added in v0.0.41

func NewLoggingStore(store Store, warnFunc WarnFunc) *LoggingStore

NewLoggingStore creates a new LoggingStore wrapper. The warnFunc is called when persistence operations fail.

func (*LoggingStore) AddMessage added in v0.0.41

func (s *LoggingStore) AddMessage(ctx context.Context, sessionID string, msg *Message) error

AddMessage wraps Store.AddMessage with error logging.

func (*LoggingStore) Create added in v0.0.41

func (s *LoggingStore) Create(ctx context.Context, sess *Session) error

Create wraps Store.Create with error logging.

func (*LoggingStore) IncrementUserTurns added in v0.0.41

func (s *LoggingStore) IncrementUserTurns(ctx context.Context, id string) error

IncrementUserTurns wraps Store.IncrementUserTurns with error logging.

func (*LoggingStore) SetCurrent added in v0.0.41

func (s *LoggingStore) SetCurrent(ctx context.Context, sessionID string) error

SetCurrent wraps Store.SetCurrent with error logging.

func (*LoggingStore) Update added in v0.0.41

func (s *LoggingStore) Update(ctx context.Context, sess *Session) error

Update wraps Store.Update with error logging.

func (*LoggingStore) UpdateContextEstimate added in v0.0.161

func (s *LoggingStore) UpdateContextEstimate(ctx context.Context, id string, lastTotalTokens, lastMessageCount int) error

UpdateContextEstimate wraps Store.UpdateContextEstimate with error logging.

func (*LoggingStore) UpdateMessage added in v0.0.174

func (s *LoggingStore) UpdateMessage(ctx context.Context, sessionID string, msg *Message) error

UpdateMessage wraps Store.UpdateMessage with error logging. ErrNotFound is returned to the caller verbatim (no logging) so upsert callers can fall back to AddMessage without noise.

func (*LoggingStore) UpdateMetrics added in v0.0.41

func (s *LoggingStore) UpdateMetrics(ctx context.Context, id string, llmTurns, toolCalls, inputTokens, outputTokens, cachedInputTokens, cacheWriteTokens int) error

UpdateMetrics wraps Store.UpdateMetrics with error logging.

func (*LoggingStore) UpdateStatus added in v0.0.41

func (s *LoggingStore) UpdateStatus(ctx context.Context, id string, status SessionStatus) error

UpdateStatus wraps Store.UpdateStatus with error logging.

type Message

type Message struct {
	ID          int64      `json:"id"`
	SessionID   string     `json:"session_id"`
	Role        llm.Role   `json:"role"`
	Parts       []llm.Part `json:"parts"`        // Full parts array
	TextContent string     `json:"text_content"` // Extracted text for display/FTS
	DurationMs  int64      `json:"duration_ms,omitempty"`
	CreatedAt   time.Time  `json:"created_at"`
	Sequence    int        `json:"sequence"`
}

Message represents a message in a session. The Parts field stores the full llm.Message.Parts as JSON to preserve tool calls and results exactly.

func NewMessage

func NewMessage(sessionID string, msg llm.Message, sequence int) *Message

NewMessage creates a new Message from an llm.Message with the given session ID and sequence.

func (*Message) ExtractTextContent

func (m *Message) ExtractTextContent() string

ExtractTextContent extracts and concatenates all text parts from the message.

func (*Message) PartsJSON

func (m *Message) PartsJSON() (string, error)

PartsJSON returns the Parts field serialized to JSON for database storage.

func (*Message) SetPartsFromJSON

func (m *Message) SetPartsFromJSON(data string) error

SetPartsFromJSON deserializes JSON into the Parts field.

func (*Message) ToLLMMessage

func (m *Message) ToLLMMessage() llm.Message

ToLLMMessage converts a Message back to an llm.Message.

type NoopStore

type NoopStore struct{}

NoopStore is a no-op implementation of Store used when sessions are disabled. It silently discards all writes and returns empty results for reads.

func (*NoopStore) AddMessage

func (s *NoopStore) AddMessage(ctx context.Context, sessionID string, msg *Message) error

func (*NoopStore) ClearCurrent

func (s *NoopStore) ClearCurrent(ctx context.Context) error

func (*NoopStore) Close

func (s *NoopStore) Close() error

func (*NoopStore) CompactMessages added in v0.0.115

func (s *NoopStore) CompactMessages(ctx context.Context, sessionID string, messages []Message) error

func (*NoopStore) Create

func (s *NoopStore) Create(ctx context.Context, sess *Session) error

func (*NoopStore) Delete

func (s *NoopStore) Delete(ctx context.Context, id string) error

func (*NoopStore) DeletePushSubscription added in v0.0.114

func (s *NoopStore) DeletePushSubscription(ctx context.Context, endpoint string) error

func (*NoopStore) Get

func (s *NoopStore) Get(ctx context.Context, id string) (*Session, error)

func (*NoopStore) GetByNumber added in v0.0.56

func (s *NoopStore) GetByNumber(ctx context.Context, number int64) (*Session, error)

func (*NoopStore) GetByPrefix added in v0.0.52

func (s *NoopStore) GetByPrefix(ctx context.Context, prefix string) (*Session, error)

func (*NoopStore) GetCurrent

func (s *NoopStore) GetCurrent(ctx context.Context) (*Session, error)

func (*NoopStore) GetMessages

func (s *NoopStore) GetMessages(ctx context.Context, sessionID string, limit, offset int) ([]Message, error)

func (*NoopStore) GetMessagesFrom added in v0.0.115

func (s *NoopStore) GetMessagesFrom(ctx context.Context, sessionID string, fromSeq int) ([]Message, error)

func (*NoopStore) IncrementUserTurns added in v0.0.41

func (s *NoopStore) IncrementUserTurns(ctx context.Context, id string) error

func (*NoopStore) List

func (s *NoopStore) List(ctx context.Context, opts ListOptions) ([]SessionSummary, error)

func (*NoopStore) ListPushSubscriptions added in v0.0.114

func (s *NoopStore) ListPushSubscriptions(ctx context.Context) ([]PushSubscription, error)

func (*NoopStore) MarkTitleSkipped added in v0.0.131

func (s *NoopStore) MarkTitleSkipped(ctx context.Context, id string, t time.Time) error

func (*NoopStore) ReplaceMessages added in v0.0.80

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

func (*NoopStore) SavePushSubscription added in v0.0.114

func (s *NoopStore) SavePushSubscription(ctx context.Context, sub *PushSubscription) error

func (*NoopStore) Search

func (s *NoopStore) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

func (*NoopStore) SetCurrent

func (s *NoopStore) SetCurrent(ctx context.Context, sessionID string) error

func (*NoopStore) Update

func (s *NoopStore) Update(ctx context.Context, sess *Session) error

func (*NoopStore) UpdateContextEstimate added in v0.0.161

func (s *NoopStore) UpdateContextEstimate(ctx context.Context, id string, lastTotalTokens, lastMessageCount int) error

func (*NoopStore) UpdateMessage added in v0.0.174

func (s *NoopStore) UpdateMessage(ctx context.Context, sessionID string, msg *Message) error

func (*NoopStore) UpdateMetrics added in v0.0.41

func (s *NoopStore) UpdateMetrics(ctx context.Context, id string, llmTurns, toolCalls, inputTokens, outputTokens, cachedInputTokens, cacheWriteTokens int) error

func (*NoopStore) UpdateStatus added in v0.0.41

func (s *NoopStore) UpdateStatus(ctx context.Context, id string, status SessionStatus) error

type PushSubscription added in v0.0.114

type PushSubscription struct {
	ID        string
	Endpoint  string
	KeyP256DH string
	KeyAuth   string
}

PushSubscription represents a Web Push subscription stored in the database.

type SQLiteStore

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

SQLiteStore implements Store using SQLite.

func NewSQLiteStore

func NewSQLiteStore(cfg Config) (*SQLiteStore, error)

NewSQLiteStore creates a new SQLite-based session store.

func (*SQLiteStore) AddMessage

func (s *SQLiteStore) AddMessage(ctx context.Context, sessionID string, msg *Message) error

AddMessage adds a message to a session. If msg.Sequence < 0, the sequence number is auto-allocated atomically.

func (*SQLiteStore) ClearCurrent

func (s *SQLiteStore) ClearCurrent(ctx context.Context) error

ClearCurrent removes the current session marker.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close closes the database connection.

func (*SQLiteStore) CompactMessages added in v0.0.115

func (s *SQLiteStore) CompactMessages(ctx context.Context, sessionID string, messages []Message) error

CompactMessages appends compacted messages to the session, preserving old history, and updates compaction_seq so that resume loads only post-compaction messages. Old messages remain in the database for scrollback/history.

func (*SQLiteStore) Create

func (s *SQLiteStore) Create(ctx context.Context, sess *Session) error

Create inserts a new session.

func (*SQLiteStore) Delete

func (s *SQLiteStore) Delete(ctx context.Context, id string) error

Delete removes a session and its messages.

func (*SQLiteStore) DeletePushSubscription added in v0.0.114

func (s *SQLiteStore) DeletePushSubscription(ctx context.Context, endpoint string) error

DeletePushSubscription removes a Web Push subscription by endpoint.

func (*SQLiteStore) Get

func (s *SQLiteStore) Get(ctx context.Context, id string) (*Session, error)

Get retrieves a session by ID.

func (*SQLiteStore) GetByNumber added in v0.0.56

func (s *SQLiteStore) GetByNumber(ctx context.Context, number int64) (*Session, error)

GetByNumber retrieves a session by its sequential number.

func (*SQLiteStore) GetByPrefix added in v0.0.52

func (s *SQLiteStore) GetByPrefix(ctx context.Context, prefix string) (*Session, error)

GetByPrefix retrieves a session by number (with # prefix), exact ID, or by short ID prefix match. It tries in order: #number (e.g., #42), exact ID match, short ID prefix match.

func (*SQLiteStore) GetCurrent

func (s *SQLiteStore) GetCurrent(ctx context.Context) (*Session, error)

GetCurrent retrieves the current session.

func (*SQLiteStore) GetMessages

func (s *SQLiteStore) GetMessages(ctx context.Context, sessionID string, limit, offset int) ([]Message, error)

GetMessages retrieves messages for a session.

func (*SQLiteStore) GetMessagesFrom added in v0.0.115

func (s *SQLiteStore) GetMessagesFrom(ctx context.Context, sessionID string, fromSeq int) ([]Message, error)

GetMessagesFrom retrieves messages for a session starting from a given sequence number. Used on resume to load only post-compaction messages.

func (*SQLiteStore) IncrementUserTurns added in v0.0.41

func (s *SQLiteStore) IncrementUserTurns(ctx context.Context, id string) error

IncrementUserTurns increments the user turn count and updates last_user_message_at.

func (*SQLiteStore) List

func (s *SQLiteStore) List(ctx context.Context, opts ListOptions) ([]SessionSummary, error)

List returns sessions matching the options.

func (*SQLiteStore) ListPushSubscriptions added in v0.0.114

func (s *SQLiteStore) ListPushSubscriptions(ctx context.Context) ([]PushSubscription, error)

ListPushSubscriptions returns all stored Web Push subscriptions.

func (*SQLiteStore) MarkTitleSkipped added in v0.0.131

func (s *SQLiteStore) MarkTitleSkipped(ctx context.Context, id string, t time.Time) error

MarkTitleSkipped sets title_skipped_at on a session without bumping updated_at. This lets the autotitle job skip trivial sessions until real new messages arrive.

func (*SQLiteStore) ReplaceMessages added in v0.0.80

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

ReplaceMessages deletes all existing messages for the session and inserts the new set in a single transaction. Used after context compaction.

func (*SQLiteStore) SavePushSubscription added in v0.0.114

func (s *SQLiteStore) SavePushSubscription(ctx context.Context, sub *PushSubscription) error

SavePushSubscription upserts a Web Push subscription.

func (*SQLiteStore) Search

func (s *SQLiteStore) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

Search finds sessions containing the query text using FTS5.

func (*SQLiteStore) SetCurrent

func (s *SQLiteStore) SetCurrent(ctx context.Context, sessionID string) error

SetCurrent marks a session as the current one.

func (*SQLiteStore) Update

func (s *SQLiteStore) Update(ctx context.Context, sess *Session) error

Update modifies an existing session's metadata fields. Token metrics (input_tokens, cached_input_tokens, cache_write_tokens, output_tokens) and turn counters (llm_turns, tool_calls) are intentionally excluded — they are managed exclusively by UpdateMetrics (which uses atomic increments) to prevent stale in-memory values from clobbering accumulated totals.

func (*SQLiteStore) UpdateContextEstimate added in v0.0.161

func (s *SQLiteStore) UpdateContextEstimate(ctx context.Context, id string, lastTotalTokens, lastMessageCount int) error

UpdateContextEstimate persists the last observed provider context estimate so resumed sessions can display a realistic context meter before the next turn.

func (*SQLiteStore) UpdateMessage added in v0.0.174

func (s *SQLiteStore) UpdateMessage(ctx context.Context, sessionID string, msg *Message) error

UpdateMessage replaces the content of an existing message (keyed by msg.ID within sessionID). Returns ErrNotFound if no row matches. Used by the "persist as we go" upsert path: the caller first calls AddMessage to stamp an ID, then subsequent snapshots call UpdateMessage with the same ID.

func (*SQLiteStore) UpdateMetrics added in v0.0.41

func (s *SQLiteStore) UpdateMetrics(ctx context.Context, id string, llmTurns, toolCalls, inputTokens, outputTokens, cachedInputTokens, cacheWriteTokens int) error

UpdateMetrics atomically increments the metrics fields for a session. All token counters use += to avoid clobbering concurrent accumulation.

func (*SQLiteStore) UpdateStatus added in v0.0.41

func (s *SQLiteStore) UpdateStatus(ctx context.Context, id string, status SessionStatus) error

UpdateStatus updates just the session status.

type SearchResult

type SearchResult struct {
	SessionID     string    `json:"session_id"`
	SessionNumber int64     `json:"session_number"` // Sequential session number
	MessageID     int64     `json:"message_id"`
	SessionName   string    `json:"session_name"`
	Summary       string    `json:"summary"`
	Snippet       string    `json:"snippet"` // Matched text snippet
	Provider      string    `json:"provider"`
	Model         string    `json:"model"`
	CreatedAt     time.Time `json:"created_at"`
}

SearchResult represents a search match.

type Session

type Session struct {
	ID      string `json:"id"`
	Number  int64  `json:"number,omitempty"` // Sequential session number (1, 2, 3...)
	Name    string `json:"name,omitempty"`
	Summary string `json:"summary,omitempty"` // First user message or auto-generated

	GeneratedShortTitle string             `json:"generated_short_title,omitempty"`
	GeneratedLongTitle  string             `json:"generated_long_title,omitempty"`
	TitleSource         SessionTitleSource `json:"title_source,omitempty"`
	TitleGeneratedAt    time.Time          `json:"title_generated_at,omitempty"`
	TitleBasisMsgSeq    int                `json:"title_basis_msg_seq,omitempty"`
	TitleSkippedAt      time.Time          `json:"title_skipped_at,omitempty"` // Set when autotitle considers session untitlable; cleared when session is updated

	Provider        string        `json:"provider"`               // Provider display label
	ProviderKey     string        `json:"provider_key,omitempty"` // Canonical provider key (e.g. openai, chatgpt, custom alias)
	Model           string        `json:"model"`
	ReasoningEffort string        `json:"reasoning_effort,omitempty"` // Reasoning effort pinned at session creation (web only)
	Mode            SessionMode   `json:"mode,omitempty"`             // Session mode (chat, ask, plan, exec)
	Origin          SessionOrigin `json:"origin,omitempty"`           // Session surface/origin (tui, web, telegram)
	Agent           string        `json:"agent,omitempty"`            // Agent name used for this session
	CWD             string        `json:"cwd,omitempty"`              // Working directory at session start
	CreatedAt       time.Time     `json:"created_at"`
	UpdatedAt       time.Time     `json:"updated_at"`
	Archived        bool          `json:"archived,omitempty"`
	Pinned          bool          `json:"pinned,omitempty"`
	ParentID        string        `json:"parent_id,omitempty"`   // For session branching
	IsSubagent      bool          `json:"is_subagent,omitempty"` // True if this is a subagent session

	// Session settings (restored on resume unless overridden)
	Search bool   `json:"search,omitempty"` // Web search enabled
	Tools  string `json:"tools,omitempty"`  // Enabled tools (comma-separated)
	MCP    string `json:"mcp,omitempty"`    // Enabled MCP servers (comma-separated)

	// Session metrics
	UserTurns         int           `json:"user_turns,omitempty"`          // Number of user messages
	LLMTurns          int           `json:"llm_turns,omitempty"`           // Number of LLM API round-trips
	ToolCalls         int           `json:"tool_calls,omitempty"`          // Total tool executions
	InputTokens       int           `json:"input_tokens,omitempty"`        // Total non-cached, non-cache-write input tokens
	CachedInputTokens int           `json:"cached_input_tokens,omitempty"` // Total cached input tokens read (cache hits)
	CacheWriteTokens  int           `json:"cache_write_tokens,omitempty"`  // Total tokens written to cache (cache misses)
	OutputTokens      int           `json:"output_tokens,omitempty"`       // Total output tokens used
	LastTotalTokens   int           `json:"last_total_tokens,omitempty"`   // Last observed request context size (input+cached+output)
	LastMessageCount  int           `json:"last_message_count,omitempty"`  // Message count at time LastTotalTokens was observed
	Status            SessionStatus `json:"status,omitempty"`              // Session status
	Tags              string        `json:"tags,omitempty"`                // Comma-separated tags
	CompactionSeq     int           `json:"compaction_seq,omitempty"`      // Sequence of first post-compaction message (-1 = none)
}

Session represents a chat session stored in the database.

func (Session) PreferredLongTitle added in v0.0.120

func (s Session) PreferredLongTitle() string

PreferredLongTitle returns the best long descriptive title available for the session.

func (Session) PreferredShortTitle added in v0.0.120

func (s Session) PreferredShortTitle() string

PreferredShortTitle returns the best short title available for the session.

type SessionMode added in v0.0.55

type SessionMode string

SessionMode represents the type/context of a session.

const (
	ModeChat SessionMode = "chat" // Interactive chat TUI
	ModeAsk  SessionMode = "ask"  // One-shot ask command
	ModePlan SessionMode = "plan" // Collaborative planning TUI
	ModeExec SessionMode = "exec" // Command suggestion/execution
)

type SessionOrigin added in v0.0.130

type SessionOrigin string
const (
	OriginTUI      SessionOrigin = "tui"
	OriginWeb      SessionOrigin = "web"
	OriginTelegram SessionOrigin = "telegram"
)

type SessionStatus added in v0.0.41

type SessionStatus string

SessionStatus represents the current state of a session.

const (
	StatusActive      SessionStatus = "active"      // Session is open/current (may or may not be streaming)
	StatusComplete    SessionStatus = "complete"    // Session finished normally
	StatusError       SessionStatus = "error"       // Session ended with an error
	StatusInterrupted SessionStatus = "interrupted" // Session was cancelled by user
)

type SessionSummary

type SessionSummary struct {
	ID                  string             `json:"id"`
	Number              int64              `json:"number,omitempty"` // Sequential session number
	Name                string             `json:"name,omitempty"`
	Summary             string             `json:"summary,omitempty"`
	GeneratedShortTitle string             `json:"generated_short_title,omitempty"`
	GeneratedLongTitle  string             `json:"generated_long_title,omitempty"`
	TitleSource         SessionTitleSource `json:"title_source,omitempty"`
	Provider            string             `json:"provider"`
	ProviderKey         string             `json:"provider_key,omitempty"`
	Model               string             `json:"model"`
	Mode                SessionMode        `json:"mode,omitempty"`
	Origin              SessionOrigin      `json:"origin,omitempty"`
	Archived            bool               `json:"archived,omitempty"`
	Pinned              bool               `json:"pinned,omitempty"`
	MessageCount        int                `json:"message_count"`
	UserTurns           int                `json:"user_turns,omitempty"`
	LLMTurns            int                `json:"llm_turns,omitempty"`
	ToolCalls           int                `json:"tool_calls,omitempty"`
	InputTokens         int                `json:"input_tokens,omitempty"`
	CachedInputTokens   int                `json:"cached_input_tokens,omitempty"`
	CacheWriteTokens    int                `json:"cache_write_tokens,omitempty"`
	OutputTokens        int                `json:"output_tokens,omitempty"`
	Status              SessionStatus      `json:"status,omitempty"`
	Tags                string             `json:"tags,omitempty"`
	CreatedAt           time.Time          `json:"created_at"`
	UpdatedAt           time.Time          `json:"updated_at"`
	LastMessageAt       time.Time          `json:"last_message_at,omitempty"`
}

SessionSummary is a lightweight view of a session for listing.

func (SessionSummary) PreferredLongTitle added in v0.0.120

func (s SessionSummary) PreferredLongTitle() string

PreferredLongTitle returns the best long descriptive title available for the summary.

func (SessionSummary) PreferredShortTitle added in v0.0.120

func (s SessionSummary) PreferredShortTitle() string

PreferredShortTitle returns the best short title available for the summary.

type SessionTitleSource added in v0.0.120

type SessionTitleSource string
const (
	TitleSourceNone      SessionTitleSource = ""
	TitleSourceUser      SessionTitleSource = "user"
	TitleSourceGenerated SessionTitleSource = "generated"
)

type Store

type Store interface {
	// Session CRUD
	Create(ctx context.Context, s *Session) error
	Get(ctx context.Context, id string) (*Session, error)
	GetByNumber(ctx context.Context, number int64) (*Session, error)
	GetByPrefix(ctx context.Context, prefix string) (*Session, error)
	Update(ctx context.Context, s *Session) error
	MarkTitleSkipped(ctx context.Context, id string, t time.Time) error
	Delete(ctx context.Context, id string) error

	// Listing and search
	List(ctx context.Context, opts ListOptions) ([]SessionSummary, error)
	Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

	// Message operations - stores full llm.Message with Parts
	AddMessage(ctx context.Context, sessionID string, msg *Message) error
	// UpdateMessage replaces the content of an existing message (by msg.ID) with
	// the supplied msg (role, parts, text, duration, sequence are updated in
	// place). Used for "persist as we go" upserts of an in-progress assistant
	// message during streaming. Returns ErrNotFound if the row does not exist.
	UpdateMessage(ctx context.Context, sessionID string, msg *Message) error
	GetMessages(ctx context.Context, sessionID string, limit, offset int) ([]Message, error)
	GetMessagesFrom(ctx context.Context, sessionID string, fromSeq int) ([]Message, error)
	ReplaceMessages(ctx context.Context, sessionID string, messages []Message) error
	CompactMessages(ctx context.Context, sessionID string, messages []Message) error

	// Metrics operations (for incremental session saving)
	UpdateMetrics(ctx context.Context, id string, llmTurns, toolCalls, inputTokens, outputTokens, cachedInputTokens, cacheWriteTokens int) error
	UpdateContextEstimate(ctx context.Context, id string, lastTotalTokens, lastMessageCount int) error
	UpdateStatus(ctx context.Context, id string, status SessionStatus) error
	IncrementUserTurns(ctx context.Context, id string) error

	// Current session tracking (for auto-resume)
	SetCurrent(ctx context.Context, sessionID string) error
	GetCurrent(ctx context.Context) (*Session, error)
	ClearCurrent(ctx context.Context) error

	// Push subscription management (for web push notifications)
	SavePushSubscription(ctx context.Context, sub *PushSubscription) error
	DeletePushSubscription(ctx context.Context, endpoint string) error
	ListPushSubscriptions(ctx context.Context) ([]PushSubscription, error)

	// Lifecycle
	Close() error
}

Store is the interface for session persistence.

func NewStore

func NewStore(cfg Config) (Store, error)

NewStore creates a new Store based on the configuration. If sessions are disabled, returns a no-op store.

type WarnFunc added in v0.0.41

type WarnFunc func(format string, args ...any)

WarnFunc is a function that logs warnings.

Jump to

Keyboard shortcuts

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