session

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package session provides the core session model: id, title, created_at, and conversation history. File-based persistence lives in internal/agentapp (SessionStore).

Index

Constants

This section is empty.

Variables

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

ErrSessionNotFound is returned when a session file does not exist.

Functions

func CtxWithSessionID

func CtxWithSessionID(ctx context.Context, id string) context.Context

CtxWithSessionID returns a context that carries the given session ID.

func EnsureTitleFromFirstUserMessage

func EnsureTitleFromFirstUserMessage(s *Session, maxLen int)

EnsureTitleFromFirstUserMessage sets the session title from the first user message if the title is empty, truncated to maxLen runes. No-op otherwise.

func NewID

func NewID() string

NewID returns a new session ID.

func SessionIDFromContext

func SessionIDFromContext(ctx context.Context) (string, bool)

SessionIDFromContext returns the session ID from ctx, or ("", false) if not set.

Types

type Session

type Session struct {
	ID               string        `json:"id"`
	Title            string        `json:"title,omitempty"`
	CreatedAt        time.Time     `json:"created_at"`
	Messages         []llm.Message `json:"messages,omitempty"`
	PromptTokens     int           `json:"prompt_tokens,omitempty"`
	CompletionTokens int           `json:"completion_tokens,omitempty"`
	// CompactionIdx is the index into Messages where the latest compaction boundary falls.
	// Messages before this index have been summarized into CompactionSummary.
	// Zero means no compaction has occurred.
	CompactionIdx     int    `json:"compaction_idx,omitempty"`
	CompactionSummary string `json:"compaction_summary,omitempty"`
}

Session holds conversation history (user, assistant, tool messages) and metadata. The system message is not stored; it is prepended at call time by the agent. JSON tags match the on-disk session file format (snake_case).

func NewSession

func NewSession(title string) *Session

NewSession creates a new session with a generated UUID, the given title, created_at set to the current time, and empty history. Title may be empty.

func NewSessionFromData

func NewSessionFromData(id, title string, createdAt time.Time, messages []llm.Message, promptTokens, completionTokens int) *Session

NewSessionFromData constructs a Session from persisted data.

func (*Session) AddCompaction

func (s *Session) AddCompaction(summary string, summarizedCount int)

AddCompaction advances the compaction boundary by summarizedCount messages and stores the summary. Implements agent.CompactionHistory so RunLoop can persist the boundary across turns.

func (*Session) Append

func (s *Session) Append(msg llm.Message) error

Append adds one message to the session's history.

func (*Session) HistoryMessages

func (s *Session) HistoryMessages() []llm.Message

HistoryMessages returns the LLM-facing message slice. When a compaction boundary exists, only messages from CompactionIdx onward are returned; earlier messages have been summarized.

type SessionItem

type SessionItem struct {
	ID        string `json:"id"`
	Title     string `json:"title,omitempty"`
	Workspace string `json:"workspace,omitempty"`
	CreatedAt string `json:"created_at"` // RFC3339
	Pinned    bool   `json:"pinned,omitempty"`
}

SessionItem is one session's metadata in the session index file (sessions.json).

Jump to

Keyboard shortcuts

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