sessions

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sessions provides persistent session management for the agent.

Index

Constants

View Source
const (

	// DefaultSessionTTL is the default TTL for sessions (7 days).
	DefaultSessionTTL = 7 * 24 * time.Hour
)

Variables

View Source
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

func NewSession(id string) *Session

NewSession creates a new session with the given ID.

func (*Session) AddMessage

func (s *Session) AddMessage(role provider.Role, content string)

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

func (s *Session) AddToolResult(toolCallID, content string)

AddToolResult adds a tool result message to the session.

func (*Session) Clear

func (s *Session) Clear()

Clear removes all messages from the session.

func (*Session) GetMessages

func (s *Session) GetMessages() []provider.Message

GetMessages returns all messages in the session.

func (*Session) GetMetadata

func (s *Session) GetMetadata(key string) (any, bool)

GetMetadata gets a metadata value.

func (*Session) MarshalJSON

func (s *Session) MarshalJSON() ([]byte, error)

MarshalJSON serializes the session to JSON.

func (*Session) SetMetadata

func (s *Session) SetMetadata(key string, value any)

SetMetadata sets a metadata value.

func (*Session) Trim

func (s *Session) Trim(n int)

Trim keeps only the last n messages.

func (*Session) UnmarshalJSON

func (s *Session) UnmarshalJSON(data []byte) error

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) Close

func (s *Skill) Close() error

Close releases resources.

func (*Skill) Description

func (s *Skill) Description() string

Description returns a human-readable description.

func (*Skill) Init

func (s *Skill) Init(ctx context.Context) error

Init initializes the skill.

func (*Skill) Name

func (s *Skill) Name() string

Name returns the skill identifier.

func (*Skill) SetStorage

func (s *Skill) SetStorage(backend kvs.Store)

SetStorage implements compiled.StorageAware.

func (*Skill) Tools

func (s *Skill) Tools() []skill.Tool

Tools returns the tools provided by this skill.

type Store

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

Store manages persistent session storage.

func NewStore

func NewStore(config StoreConfig) *Store

NewStore creates a new session store.

func (*Store) ClearCache

func (s *Store) ClearCache()

Clear removes all cached sessions. This does not delete sessions from the backend.

func (*Store) Close

func (s *Store) Close() error

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) Delete

func (s *Store) Delete(ctx context.Context, id string) error

Delete removes a session.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (*Session, error)

Get retrieves a session by ID. If the session doesn't exist, it creates a new one.

func (*Store) GetIfExists

func (s *Store) GetIfExists(ctx context.Context, id string) (*Session, error)

GetIfExists retrieves a session by ID only if it exists. Returns ErrSessionNotFound if the session doesn't exist.

func (*Store) List

func (s *Store) List(ctx context.Context) ([]string, error)

List returns all session IDs. This requires the backend to implement kvs.ListableStore.

func (*Store) Save

func (s *Store) Save(ctx context.Context, session *Session) error

Save persists a session to storage.

func (*Store) Touch

func (s *Store) Touch(ctx context.Context, id string) error

Touch updates the session's TTL without modifying its content.

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).

Jump to

Keyboard shortcuts

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