session

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// PermissionModeAsk requires user confirmation each time the tool is called
	PermissionModeAsk = "ask"
	// PermissionModeAlwaysAllow auto-approves the tool without user confirmation
	PermissionModeAlwaysAllow = "always_allow"
)

Permission mode constants

View Source
const (
	// MaxToolCallTokens is the maximum number of tokens to keep from tool call
	// arguments and results. Older tool calls beyond this budget will have their
	// content replaced with a placeholder. Tokens are approximated as len/4.
	MaxToolCallTokens = 40000
)

Variables

View Source
var (
	ErrEmptyID  = errors.New("session ID cannot be empty")
	ErrNotFound = errors.New("session not found")
)

Functions

This section is empty.

Types

type InMemorySessionStore added in v1.8.2

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

func (*InMemorySessionStore) AddSession added in v1.8.2

func (s *InMemorySessionStore) AddSession(_ context.Context, session *Session) error

func (*InMemorySessionStore) DeleteSession added in v1.8.2

func (s *InMemorySessionStore) DeleteSession(_ context.Context, id string) error

func (*InMemorySessionStore) GetSession added in v1.8.2

func (s *InMemorySessionStore) GetSession(_ context.Context, id string) (*Session, error)

func (*InMemorySessionStore) GetSessionSummaries added in v1.8.2

func (s *InMemorySessionStore) GetSessionSummaries(_ context.Context) ([]Summary, error)

func (*InMemorySessionStore) GetSessions added in v1.8.2

func (s *InMemorySessionStore) GetSessions(_ context.Context) ([]*Session, error)

func (*InMemorySessionStore) SetSessionStarred added in v1.18.4

func (s *InMemorySessionStore) SetSessionStarred(_ context.Context, id string, starred bool) error

SetSessionStarred sets the starred status of a session.

func (*InMemorySessionStore) UpdateSession added in v1.8.2

func (s *InMemorySessionStore) UpdateSession(_ context.Context, session *Session) error

UpdateSession updates an existing session, or creates it if it doesn't exist (upsert). This enables lazy session persistence - sessions are only stored when they have content.

type Item

type Item struct {
	// Message holds a regular conversation message
	Message *Message `json:"message,omitempty"`

	// SubSession holds a complete sub-session from task transfers
	SubSession *Session `json:"sub_session,omitempty"`

	// Summary is a summary of the session up until this point
	Summary string `json:"summary,omitempty"`
}

Item represents either a message or a sub-session

func NewMessageItem

func NewMessageItem(msg *Message) Item

NewMessageItem creates a SessionItem containing a message

func NewSubSessionItem

func NewSubSessionItem(subSession *Session) Item

NewSubSessionItem creates a SessionItem containing a sub-session

func (*Item) IsMessage

func (si *Item) IsMessage() bool

IsMessage returns true if this item contains a message

func (*Item) IsSubSession

func (si *Item) IsSubSession() bool

IsSubSession returns true if this item contains a sub-session

type Message

type Message struct {
	AgentName string       `json:"agentName"` // TODO: rename to agent_name
	Message   chat.Message `json:"message"`
	// Implicit is an optional field to indicate if the message shouldn't be shown to the user. It's needed for special  situations
	// like when an agent transfers a task to another agent - new session is created with a default user message, but this shouldn't be shown to the user.
	// Such messages should be marked as true
	Implicit bool `json:"implicit,omitempty"`
}

Message is a message from an agent

func ImplicitUserMessage added in v1.6.3

func ImplicitUserMessage(content string) *Message

func NewAgentMessage

func NewAgentMessage(a *agent.Agent, message *chat.Message) *Message

func SystemMessage

func SystemMessage(content string) *Message

func UserMessage

func UserMessage(content string, multiContent ...chat.MessagePart) *Message

type Migration

type Migration struct {
	ID          int
	Name        string
	Description string
	UpSQL       string
	DownSQL     string
	AppliedAt   time.Time
}

Migration represents a database migration

type MigrationManager

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

MigrationManager handles database migrations

func NewMigrationManager

func NewMigrationManager(db *sql.DB) *MigrationManager

NewMigrationManager creates a new migration manager

func (*MigrationManager) GetAppliedMigrations

func (m *MigrationManager) GetAppliedMigrations(ctx context.Context) ([]Migration, error)

GetAppliedMigrations returns a list of applied migrations

func (*MigrationManager) InitializeMigrations

func (m *MigrationManager) InitializeMigrations(ctx context.Context) error

InitializeMigrations sets up the migrations table and runs pending migrations

func (*MigrationManager) RunPendingMigrations

func (m *MigrationManager) RunPendingMigrations(ctx context.Context) error

RunPendingMigrations executes all migrations that haven't been applied yet

type Opt

type Opt func(s *Session)

func WithHideToolResults added in v1.8.2

func WithHideToolResults(hideToolResults bool) Opt

func WithImplicitUserMessage added in v1.6.3

func WithImplicitUserMessage(content string) Opt

func WithMaxIterations added in v1.3.5

func WithMaxIterations(maxIterations int) Opt

func WithPermissions added in v1.18.8

func WithPermissions(perms *PermissionsConfig) Opt

func WithSendUserMessage added in v1.8.2

func WithSendUserMessage(sendUserMessage bool) Opt

func WithSystemMessage

func WithSystemMessage(content string) Opt

func WithTitle added in v1.8.2

func WithTitle(title string) Opt

func WithToolsApproved added in v1.8.2

func WithToolsApproved(toolsApproved bool) Opt

func WithUserMessage

func WithUserMessage(content string) Opt

func WithWorkingDir added in v1.5.14

func WithWorkingDir(workingDir string) Opt

type PermissionsConfig added in v1.18.8

type PermissionsConfig struct {
	// Tools maps tool names to their permission settings.
	// Takes priority over Allow patterns when a tool is explicitly configured.
	Tools map[string]ToolPermission `json:"tools,omitempty"`
	// Allow lists tool name patterns that are auto-approved without user confirmation.
	// Used as fallback when tool is not in Tools map.
	Allow []string `json:"allow,omitempty"`
	// Deny lists tool name patterns that are always rejected.
	Deny []string `json:"deny,omitempty"`
}

PermissionsConfig defines session-level tool permission overrides. It supports both per-tool settings (Tools map) and pattern-based rules (Allow/Deny arrays).

func (*PermissionsConfig) GetToolMode added in v1.19.0

func (p *PermissionsConfig) GetToolMode(toolName string) string

GetToolMode returns the permission mode for a specific tool. Returns empty string if the tool is not in the Tools map. Returns the Mode value if set, otherwise returns PermissionModeAsk as default.

func (*PermissionsConfig) GetToolPermission added in v1.19.0

func (p *PermissionsConfig) GetToolPermission(toolName string) *ToolPermission

GetToolPermission returns the permission settings for a specific tool. Returns nil if the tool is not explicitly configured in the Tools map.

func (*PermissionsConfig) IsToolEnabled added in v1.19.0

func (p *PermissionsConfig) IsToolEnabled(toolName string) bool

IsToolEnabled checks if a tool is enabled based on the Tools map. Returns true if the tool is not in the map (not explicitly disabled). Returns the Enabled value if the tool is in the map.

type SQLiteSessionStore

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

SQLiteSessionStore implements Store using SQLite

func (*SQLiteSessionStore) AddSession

func (s *SQLiteSessionStore) AddSession(ctx context.Context, session *Session) error

AddSession adds a new session to the store

func (*SQLiteSessionStore) Close

func (s *SQLiteSessionStore) Close() error

Close closes the database connection

func (*SQLiteSessionStore) DeleteSession

func (s *SQLiteSessionStore) DeleteSession(ctx context.Context, id string) error

DeleteSession deletes a session by ID

func (*SQLiteSessionStore) GetSession

func (s *SQLiteSessionStore) GetSession(ctx context.Context, id string) (*Session, error)

GetSession retrieves a session by ID

func (*SQLiteSessionStore) GetSessionSummaries added in v1.8.2

func (s *SQLiteSessionStore) GetSessionSummaries(ctx context.Context) ([]Summary, error)

GetSessionSummaries retrieves lightweight session metadata for listing. This is much faster than GetSessions as it doesn't load message content.

func (*SQLiteSessionStore) GetSessions

func (s *SQLiteSessionStore) GetSessions(ctx context.Context) ([]*Session, error)

GetSessions retrieves all sessions

func (*SQLiteSessionStore) SetSessionStarred added in v1.18.4

func (s *SQLiteSessionStore) SetSessionStarred(ctx context.Context, id string, starred bool) error

SetSessionStarred sets the starred status of a session.

func (*SQLiteSessionStore) UpdateSession

func (s *SQLiteSessionStore) UpdateSession(ctx context.Context, session *Session) error

UpdateSession updates an existing session, or creates it if it doesn't exist (upsert). This enables lazy session persistence - sessions are only stored when they have content.

type Session

type Session struct {
	// ID is the unique identifier for the session
	ID string `json:"id"`

	// Title is the title of the session, set by the runtime
	Title string `json:"title"`

	// Messages holds the conversation history (messages and sub-sessions)
	Messages []Item `json:"messages"`

	// CreatedAt is the time the session was created
	CreatedAt time.Time `json:"created_at"`

	// ToolsApproved is a flag to indicate if the tools have been approved
	ToolsApproved bool `json:"tools_approved"`

	// HideToolResults is a flag to indicate if tool results should be hidden
	HideToolResults bool `json:"hide_tool_results"`

	// WorkingDir is the base directory used for filesystem-aware tools
	WorkingDir string `json:"working_dir,omitempty"`

	// SendUserMessage is a flag to indicate if the user message should be sent
	SendUserMessage bool

	// MaxIterations is the maximum number of agentic loop iterations to prevent infinite loops
	// If 0, there is no limit
	MaxIterations int `json:"max_iterations"`

	// Starred indicates if this session has been starred by the user
	Starred bool `json:"starred"`

	InputTokens  int64   `json:"input_tokens"`
	OutputTokens int64   `json:"output_tokens"`
	Cost         float64 `json:"cost"`

	// Permissions holds session-level permission overrides.
	// When set, these are evaluated before team-level permissions.
	Permissions *PermissionsConfig `json:"permissions,omitempty"`

	// AgentModelOverrides stores per-agent model overrides for this session.
	// Key is the agent name, value is the model reference (e.g., "openai/gpt-4o" or a named model from config).
	// When a session is loaded, these overrides are reapplied to the runtime.
	AgentModelOverrides map[string]string `json:"agent_model_overrides,omitempty"`

	// CustomModelsUsed tracks custom models (provider/model format) used during this session.
	// These are shown in the model picker for easy re-selection.
	CustomModelsUsed []string `json:"custom_models_used,omitempty"`
}

Session represents the agent's state including conversation history and variables

func New

func New(opts ...Opt) *Session

New creates a new agent session

func (*Session) AddMessage

func (s *Session) AddMessage(msg *Message)

AddMessage adds a message to the session

func (*Session) AddSubSession

func (s *Session) AddSubSession(subSession *Session)

AddSubSession adds a sub-session to the session

func (*Session) AllowedDirectories added in v1.5.14

func (s *Session) AllowedDirectories() []string

AllowedDirectories returns the directories that should be considered safe for tools

func (*Session) Duration added in v1.19.0

func (s *Session) Duration() time.Duration

Duration calculates the duration of the session from message timestamps.

func (*Session) GetAllMessages

func (s *Session) GetAllMessages() []Message

GetAllMessages extracts all messages from the session, including from sub-sessions

func (*Session) GetLastAssistantMessageContent

func (s *Session) GetLastAssistantMessageContent() string

func (*Session) GetLastUserMessageContent added in v1.8.2

func (s *Session) GetLastUserMessageContent() string

func (*Session) GetMessages

func (s *Session) GetMessages(a *agent.Agent) []chat.Message

type Store

type Store interface {
	AddSession(ctx context.Context, session *Session) error
	GetSession(ctx context.Context, id string) (*Session, error)
	GetSessions(ctx context.Context) ([]*Session, error)
	GetSessionSummaries(ctx context.Context) ([]Summary, error)
	DeleteSession(ctx context.Context, id string) error
	UpdateSession(ctx context.Context, session *Session) error
	SetSessionStarred(ctx context.Context, id string, starred bool) error
}

Store defines the interface for session storage

func NewInMemorySessionStore added in v1.8.2

func NewInMemorySessionStore() Store

func NewSQLiteSessionStore

func NewSQLiteSessionStore(path string) (Store, error)

NewSQLiteSessionStore creates a new SQLite session store

type Summary added in v1.8.2

type Summary struct {
	ID        string
	Title     string
	CreatedAt time.Time
	Starred   bool
}

Summary contains lightweight session metadata for listing purposes. This is used instead of loading full Session objects with all messages.

type ToolPermission added in v1.19.0

type ToolPermission struct {
	// Enabled controls whether the tool is available (default: true if not set)
	Enabled *bool `json:"enabled,omitempty"`
	// Mode is the permission mode: "ask" (default) or "always_allow"
	Mode string `json:"mode,omitempty"`
}

ToolPermission defines permission settings for a single tool

Jump to

Keyboard shortcuts

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