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
- Variables
- type MemoryStore
- func (s *MemoryStore) AppendMessages(_ context.Context, sessionID string, messages ...Message) error
- func (s *MemoryStore) EnsureSession(_ context.Context, session Session) error
- func (s *MemoryStore) GetSession(_ context.Context, sessionID string) (Session, error)
- func (s *MemoryStore) ListMessages(_ context.Context, sessionID string) ([]Message, error)
- func (s *MemoryStore) ListTail(_ context.Context, sessionID string) ([]Message, error)
- type Message
- func CloneMessages(in []Message) []Message
- func ContextMessage(content []chat.ContentBlock, base Message) Message
- func DisplayMessages(messages []Message) []Message
- func FromChat(msg chat.Message) Message
- func SummaryMessage(summary, firstKeptID string) Message
- func Tail(all []Message) []Message
- func UserMessage(content []chat.ContentBlock, details any) Message
- type MessageType
- type Scope
- type Session
- type Store
- type TailLister
Constants ¶
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 ¶
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 (*MemoryStore) EnsureSession ¶
func (s *MemoryStore) EnsureSession(_ context.Context, session Session) error
func (*MemoryStore) GetSession ¶
func (*MemoryStore) ListMessages ¶
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 ¶
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 ¶
DisplayMessages returns the messages to show to the user (those with Visible set).
func FromChat ¶
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 ¶
SummaryMessage creates a summary message; firstKeptID is the first message kept after the summary.
func Tail ¶ added in v0.2.3
Tail returns the messages still used after the latest compaction: from the first message the latest summary keeps (Message.FirstKeptID) to the end, or from that summary when the kept message is not found. Without a summary it returns all.
func UserMessage ¶
func UserMessage(content []chat.ContentBlock, details any) Message
UserMessage creates a user input message.
func (Message) Content ¶
func (m Message) Content() []chat.ContentBlock
Content returns the content blocks of the message.
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 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. It does not gain methods: further capabilities are optional interfaces such as TailLister. The storetest package checks an implementation.
type TailLister ¶ added in v0.2.3
type TailLister interface {
// ListTail returns what Tail returns for the session's messages, in append order.
ListTail(ctx context.Context, sessionID string) ([]Message, error)
}
TailLister is an optional interface for stores that can read a session without the part compaction has replaced. The runner uses it when the store implements it and falls back to ListMessages otherwise, so it only saves reading: a long session is then loaded from its latest summary instead of from its first message.
A store wrapped by another type (for logging, metrics and so on) only keeps it when the wrapper forwards it.