session

package
v0.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 7 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"`
	// NoteEntries and TodoEntries are durable session state: unlike a tool result, they are
	// not messages, so compaction cannot take them. The fields are named apart from the
	// Notes/Todos accessors that implement agent.NoteStore; the JSON keys are the plain names.
	NoteEntries []agent.Note `json:"notes,omitempty"`
	TodoEntries []agent.Todo `json:"todos,omitempty"`
	// AdditionalSystemPrompt records the extra system-prompt text this session actually ran
	// under. It is a record — for the trace, and so a resumed session does not silently lose
	// its identity when the flag that set it is not repeated — not the authority. Whoever
	// assembles a run resolves it afresh, and the last writer wins.
	AdditionalSystemPrompt string `json:"additional_system_prompt,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. The summary is expected to subsume any earlier one, so replacing is correct. 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.

func (*Session) Notes

func (s *Session) Notes() []agent.Note

Notes returns the session's durable notes. Implements agent.NoteStore.

func (*Session) PriorSummary

func (s *Session) PriorSummary() string

PriorSummary returns the summary stored by the most recent compaction, or "" when this session has never been compacted. Implements agent.CompactionHistory so RunLoop can feed the previous summary back into the next compaction instead of discarding what it covered.

func (*Session) SetNotes

func (s *Session) SetNotes(notes []agent.Note, iter int)

SetNotes replaces the session's notes, preserving the age of entries whose text is unchanged so a rewrite of the list does not make every entry look new. Implements agent.NoteStore.

func (*Session) SetTodos

func (s *Session) SetTodos(todos []agent.Todo, iter int)

SetTodos replaces the session's task list, preserving the age of entries whose content and status are both unchanged. Implements agent.NoteStore.

func (*Session) Todos

func (s *Session) Todos() []agent.Todo

Todos returns the session's durable task list. Implements agent.NoteStore.

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