session

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package session defines the persistence model of sessions: sessions, session messages and the store interface.

It only describes what is stored and where, not how runs work; when to store and what to store is up to the runner. A custom storage backend only needs to depend on this package.

Index

Constants

View Source
const (
	SourceUserInput = "user_input"
	SourceSystem    = "system"
	SourceAgent     = "agent"
	SourceTool      = "tool"

	KindNormal     = "normal"
	KindToolResult = "tool_result"
	// KindTaskResult is the result notice sent back to the session when a background task (sub-agent, background command and so on) ends; Details is a tool.Task.
	KindTaskResult = "task_result"
)

Common Source and Kind values. Both are free-form strings; the business layer may use its own values.

Variables

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

ErrNotFound means the session does not exist.

Functions

This section is empty.

Types

type MemoryStore

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

MemoryStore is an in-memory Store, suited to tests and lightweight single-process use.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates an in-memory session store.

func (*MemoryStore) AppendMessages

func (s *MemoryStore) AppendMessages(_ context.Context, sessionID string, messages ...Message) error

func (*MemoryStore) EnsureSession

func (s *MemoryStore) EnsureSession(_ context.Context, session Session) error

func (*MemoryStore) GetSession

func (s *MemoryStore) GetSession(_ context.Context, sessionID string) (Session, error)

func (*MemoryStore) ListMessages

func (s *MemoryStore) ListMessages(_ context.Context, sessionID string) ([]Message, error)

type Message

type Message struct {
	ID        string      `json:"id"`
	SessionID string      `json:"session_id"`
	Type      MessageType `json:"type"`
	// RunID is the run during which the message was written to history, for retrieving all messages of a run.
	// Background task results written directly to history while the session is idle belong to no run and have an empty RunID.
	RunID string `json:"run_id,omitempty"`
	// ChatMessage is the single source of the message content. User messages, business context and summaries only use its Content;
	// assistant and tool messages are the raw messages produced by the model, with thinking, tool calls, signatures and usage, replayed as is.
	ChatMessage chat.Message `json:"chat_message"`

	// Visible reports whether the message should be shown to the user.
	Visible bool `json:"visible"`
	// Source and Kind describe where the message came from and what it is, for the business layer to classify; the framework does not depend on their values.
	Source string `json:"source"`
	Kind   string `json:"kind"`
	Scope  Scope  `json:"scope"`
	// Details is structured data attached by the business layer and never sent to the model.
	Details any `json:"details,omitempty"`

	// FirstKeptID is only used by summary messages: messages before this ID are replaced by the summary and no longer enter the model context.
	FirstKeptID string    `json:"first_kept_id,omitempty"`
	CreatedAt   time.Time `json:"created_at"`
}

Message is a complete session message, as persisted and shown in a UI.

func CloneMessages

func CloneMessages(in []Message) []Message

CloneMessages returns a deep copy of a list of messages.

func ContextMessage

func ContextMessage(content []chat.ContentBlock, base Message) Message

ContextMessage creates a business context message, taking the other fields from base.

func DisplayMessages

func DisplayMessages(messages []Message) []Message

DisplayMessages returns the messages to show to the user (those with Visible set).

func FromChat

func FromChat(msg chat.Message) Message

FromChat turns an assistant or tool message produced by the model into a session message. An assistant message is visible only when it has displayable text; tool results are hidden by default.

func SummaryMessage

func SummaryMessage(summary, firstKeptID string) Message

SummaryMessage creates a summary message; firstKeptID is the first message kept after the summary.

func UserMessage

func UserMessage(content []chat.ContentBlock, details any) Message

UserMessage creates a user input message.

func (Message) Clone

func (m Message) Clone() Message

Clone returns a deep copy of the message.

func (Message) Content

func (m Message) Content() []chat.ContentBlock

Content returns the content blocks of the message.

func (Message) Text

func (m Message) Text() string

Text returns the display text of the message: text blocks only, media as placeholders, no thinking or tool calls.

type MessageType

type MessageType string

MessageType is the kind of a session message. It is not the same as ChatMessage.Role, the role used when sending to the model: business context and summaries are sent to the model with the user role.

const (
	TypeUser      MessageType = "user"
	TypeAssistant MessageType = "assistant"
	TypeTool      MessageType = "tool"
	// TypeContext is business context injected for the model, not something the user said.
	TypeContext MessageType = "context"
	// TypeSummary is a summary produced by compaction; it replaces the history before FirstKeptID in the model context.
	TypeSummary MessageType = "summary"
)

type Scope

type Scope string

Scope is the lifetime of a message in the session.

const (
	// ScopeRun messages only enter the model context of one run and are not stored, such as temporary context.
	ScopeRun Scope = "run"
	// ScopePersistent messages are stored and stay visible to later runs.
	ScopePersistent Scope = "persistent"
)

type Session

type Session struct {
	ID string
	// ParentID is the parent session that spawned this one. Each sub-agent task is stored in a session of its own;
	// it is not a conversation the user created, and listings of the user's sessions should usually exclude it.
	ParentID  string
	CreatedAt time.Time
}

Session holds session metadata.

type Store

type Store interface {
	// EnsureSession creates the session if it does not exist and does nothing otherwise.
	EnsureSession(ctx context.Context, session Session) error
	GetSession(ctx context.Context, sessionID string) (Session, error)
	// AppendMessages appends messages in order. Message IDs are generated by the caller.
	// The runner calls it with a ctx that has no cancellation or deadline (messages already produced must still be written
	// after a run is canceled); an implementation that needs to bound write time should set its own timeout.
	AppendMessages(ctx context.Context, sessionID string, messages ...Message) error
	// ListMessages returns all messages of the session in append order.
	ListMessages(ctx context.Context, sessionID string) ([]Message, error)
}

Store is the session store interface, which can be backed by a database and so on. Its methods are called concurrently for many sessions.

Jump to

Keyboard shortcuts

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