Documentation
¶
Overview ¶
Package sessions provides persistent session management for the agent.
Index ¶
- Constants
- Variables
- type RolloverPolicy
- type RolloverReason
- type Session
- func (s *Session) AddMessage(role provider.Role, content string)
- func (s *Session) AddMessageWithToolCalls(role provider.Role, content string, toolCalls []provider.ToolCall)
- func (s *Session) AddToolResult(toolCallID, content string)
- func (s *Session) Clear()
- func (s *Session) GetMessages() []provider.Message
- func (s *Session) GetMetadata(key string) (any, bool)
- func (s *Session) MarshalJSON() ([]byte, error)
- func (s *Session) SetMetadata(key string, value any)
- func (s *Session) Trim(n int)
- func (s *Session) UnmarshalJSON(data []byte) error
- type Skill
- type Store
- func (s *Store) ClearCache()
- func (s *Store) Close() error
- func (s *Store) Delete(ctx context.Context, id string) error
- func (s *Store) Get(ctx context.Context, id string) (*Session, error)
- func (s *Store) GetIfExists(ctx context.Context, id string) (*Session, error)
- func (s *Store) List(ctx context.Context) ([]string, error)
- func (s *Store) Save(ctx context.Context, session *Session) error
- func (s *Store) Touch(ctx context.Context, id string) error
- type StoreConfig
- type ToolOverrides
Constants ¶
const ( // DefaultSessionTTL is the default TTL for sessions (7 days). DefaultSessionTTL = 7 * 24 * time.Hour )
Variables ¶
var ErrSessionNotFound = errors.New("session not found")
ErrSessionNotFound is returned when a session is not found.
Functions ¶
This section is empty.
Types ¶
type RolloverPolicy ¶ added in v0.16.0
type RolloverPolicy struct {
// IdleTimeout rolls a session over when more than this duration has
// passed since its last update. Zero disables idle rollover.
IdleTimeout time.Duration
// Daily rolls a session over when a calendar-day boundary has been
// crossed since its last update.
Daily bool
// Location resolves the day boundary for Daily rollovers. Nil falls
// back to the caller's timezone (the agent uses its configured user
// timezone, defaulting to UTC).
Location *time.Location
}
RolloverPolicy decides when a session automatically rolls over. A rollover ends the session's current conversation (its context can be persisted to memory by a hook) and starts fresh under the same session ID.
func (*RolloverPolicy) ShouldRollover ¶ added in v0.16.0
func (p *RolloverPolicy) ShouldRollover(session *Session, now time.Time, fallback *time.Location) (RolloverReason, bool)
ShouldRollover reports whether the session should roll over at now, and why. Sessions with no messages never roll over — there is nothing to end. Idle takes precedence over daily when both apply.
type RolloverReason ¶ added in v0.16.0
type RolloverReason string
RolloverReason identifies why a session automatically rolled over.
const ( // RolloverReasonIdle marks a rollover caused by the idle timeout. RolloverReasonIdle RolloverReason = "idle" // RolloverReasonDaily marks a rollover caused by crossing a calendar-day // boundary in the configured timezone. RolloverReasonDaily RolloverReason = "daily" )
type Session ¶
type Session struct {
ID string `json:"id"`
AgentID string `json:"agent_id,omitempty"`
Messages []provider.Message `json:"messages"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Metadata map[string]any `json:"metadata,omitempty"`
// ToolOverrides scopes the tool set for this session's turns.
// Nil means no overrides. Changes take effect on the next turn.
ToolOverrides *ToolOverrides `json:"tool_overrides,omitempty"`
// Model overrides the agent's default model for this session's turns.
// Empty uses the agent default. Changes take effect on the next turn.
Model string `json:"model,omitempty"`
}
Session represents a conversation session.
func NewSession ¶
NewSession creates a new session with the given ID.
func (*Session) AddMessage ¶
AddMessage adds a message to the session.
func (*Session) AddMessageWithToolCalls ¶
func (s *Session) AddMessageWithToolCalls(role provider.Role, content string, toolCalls []provider.ToolCall)
AddMessageWithToolCalls adds a message with tool calls to the session.
func (*Session) AddToolResult ¶
AddToolResult adds a tool result message to the session.
func (*Session) GetMessages ¶
GetMessages returns all messages in the session.
func (*Session) GetMetadata ¶
GetMetadata gets a metadata value.
func (*Session) MarshalJSON ¶
MarshalJSON serializes the session to JSON.
func (*Session) SetMetadata ¶
SetMetadata sets a metadata value.
func (*Session) UnmarshalJSON ¶
UnmarshalJSON deserializes the session from JSON.
type Skill ¶
type Skill struct {
// contains filtered or unexported fields
}
Skill implements the compiled.Skill interface for session management.
func NewSkill ¶
func NewSkill() *Skill
NewSkill creates a new session management skill. The store must be set before Init is called.
func (*Skill) Description ¶
Description returns a human-readable description.
func (*Skill) SetStorage ¶
SetStorage implements compiled.StorageAware.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store manages persistent session storage.
func (*Store) ClearCache ¶
func (s *Store) ClearCache()
Clear removes all cached sessions. This does not delete sessions from the backend.
func (*Store) Close ¶
Close releases the in-memory session cache and closes the underlying backend. Releasing the cache ensures a closed store does not retain references to cached sessions for as long as the Store value is reachable.
func (*Store) Get ¶
Get retrieves a session by ID. If the session doesn't exist, it creates a new one.
func (*Store) GetIfExists ¶
GetIfExists retrieves a session by ID only if it exists. Returns ErrSessionNotFound if the session doesn't exist.
func (*Store) List ¶
List returns all session IDs. This requires the backend to implement kvs.ListableStore.
type StoreConfig ¶
type StoreConfig struct {
// Backend is the KVS storage backend.
Backend kvs.Store
// TTL is the session time-to-live. Zero means no expiration.
TTL time.Duration
}
StoreConfig configures the session store.
type ToolOverrides ¶ added in v0.16.0
type ToolOverrides struct {
// Tools maps individual tool names to enabled (true) or disabled
// (false). Tools absent from the map keep their default availability.
// This covers skill-provided tools and built-ins (e.g. web search) —
// they are all addressed by registered tool name.
Tools map[string]bool `json:"tools,omitempty"`
// MCPServers maps MCP server names to enabled/disabled. Disabling a
// server removes all of its tools for this session.
MCPServers map[string]bool `json:"mcp_servers,omitempty"`
// MCPToolsDeny lists denied tool names per MCP server, using the
// tool's original name on that server.
MCPToolsDeny map[string][]string `json:"mcp_tools_deny,omitempty"`
}
ToolOverrides holds per-session tool scoping. The agent applies these when building the tool set for a session's turns; the shared tool registry is never mutated, so concurrent sessions with different overrides get independent tool sets.
func (*ToolOverrides) Denies ¶ added in v0.16.0
func (o *ToolOverrides) Denies(name, source, sourceName, sourceTool string) bool
Denies reports whether the overrides deny the given tool. The source fields describe the tool's provenance: source is the origin kind (e.g. "mcp"), sourceName the originating server/skill, and sourceTool the tool's original name at its source. Non-MCP tools pass empty provenance.
func (*ToolOverrides) IsZero ¶ added in v0.16.0
func (o *ToolOverrides) IsZero() bool
IsZero reports whether no overrides are set (including a nil receiver).