session

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSessionNotFound is returned when a session is not found
	ErrSessionNotFound = errors.New("session not found")

	// ErrInvalidSessionID is returned when a session ID is invalid
	ErrInvalidSessionID = errors.New("invalid session ID")
)

Functions

This section is empty.

Types

type HistoryOption added in v1.4.0

type HistoryOption func(*HistoryProvider)

HistoryOption configures a HistoryProvider.

func WithMaxContentLen added in v1.4.0

func WithMaxContentLen(n int) HistoryOption

WithMaxContentLen sets the maximum character length for each run summary.

type HistoryProvider added in v1.4.0

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

HistoryProvider implements agent.HistoryProvider by reading session history from a Storage backend and formatting it as a context string.

func NewHistoryProvider added in v1.4.0

func NewHistoryProvider(storage Storage, opts ...HistoryOption) *HistoryProvider

NewHistoryProvider creates a new HistoryProvider wrapping the given Storage.

func (*HistoryProvider) GetHistory added in v1.4.0

func (h *HistoryProvider) GetHistory(ctx context.Context, sessionID string, maxRuns int) (string, error)

GetHistory returns a formatted summary of previous runs in the session. maxRuns limits how many recent runs to include.

type MemoryStorage

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

MemoryStorage implements in-memory session storage

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

NewMemoryStorage creates a new in-memory storage

func (*MemoryStorage) Close

func (m *MemoryStorage) Close() error

Close closes the storage (no-op for memory storage)

func (*MemoryStorage) Create

func (m *MemoryStorage) Create(ctx context.Context, session *Session) error

Create creates a new session

func (*MemoryStorage) Delete

func (m *MemoryStorage) Delete(ctx context.Context, sessionID string) error

Delete deletes a session by ID

func (*MemoryStorage) Get

func (m *MemoryStorage) Get(ctx context.Context, sessionID string) (*Session, error)

Get retrieves a session by ID

func (*MemoryStorage) List

func (m *MemoryStorage) List(ctx context.Context, filters map[string]interface{}) ([]*Session, error)

List lists all sessions (with optional filters)

func (*MemoryStorage) ListByAgent

func (m *MemoryStorage) ListByAgent(ctx context.Context, agentID string) ([]*Session, error)

ListByAgent lists all sessions for a specific agent

func (*MemoryStorage) ListByUser

func (m *MemoryStorage) ListByUser(ctx context.Context, userID string) ([]*Session, error)

ListByUser lists all sessions for a specific user

func (*MemoryStorage) Update

func (m *MemoryStorage) Update(ctx context.Context, session *Session) error

Update updates an existing session

type Persister added in v1.4.0

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

Persister adapts a session.Storage to the agent.SessionPersister interface, allowing the agent to persist run outputs without importing the session package.

func NewPersister added in v1.4.0

func NewPersister(storage Storage) *Persister

NewPersister creates a new Persister wrapping the given Storage.

func (*Persister) PersistRun added in v1.4.0

func (p *Persister) PersistRun(ctx context.Context, sessionID, agentID, userID string, output *agent.RunOutput) error

PersistRun saves a completed run output to the session. If the session doesn't exist, it creates a new one. Concurrent calls are serialized via a mutex to prevent lost updates.

type Session

type Session struct {
	// Unique session identifier
	SessionID string `json:"session_id"`

	// Agent/Team/Workflow identifiers
	AgentID    string `json:"agent_id,omitempty"`
	TeamID     string `json:"team_id,omitempty"`
	WorkflowID string `json:"workflow_id,omitempty"`

	// User identifier
	UserID string `json:"user_id,omitempty"`

	// Session metadata
	Name     string                 `json:"name,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`

	// Session state
	State map[string]interface{} `json:"state,omitempty"`

	// Agent data (for reference)
	AgentData map[string]interface{} `json:"agent_data,omitempty"`

	// Run history
	Runs []*agent.RunOutput `json:"runs,omitempty"`

	// Summary (for long sessions)
	Summary *SessionSummary `json:"summary,omitempty"`

	// Timestamps
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

Session represents a conversation session with an agent

func NewSession

func NewSession(sessionID string, agentID string) *Session

NewSession creates a new session

func (*Session) AddRun

func (s *Session) AddRun(run *agent.RunOutput)

AddRun adds a run output to the session

func (*Session) CalculateTotalTokens

func (s *Session) CalculateTotalTokens() int

CalculateTotalTokens calculates total tokens used across all runs Note: Currently RunOutput doesn't track metrics, so this returns 0 This is a placeholder for future enhancements

func (*Session) GenerateSummary

func (s *Session) GenerateSummary(content string)

GenerateSummary creates a summary of the session

func (*Session) GetLastRun

func (s *Session) GetLastRun() *agent.RunOutput

GetLastRun returns the most recent run

func (*Session) GetRunCount

func (s *Session) GetRunCount() int

GetRunCount returns the number of runs in this session

type SessionSummary

type SessionSummary struct {
	// Summary text
	Content string `json:"content"`

	// Number of runs in this session
	RunCount int `json:"run_count"`

	// Total tokens used
	TotalTokens int `json:"total_tokens"`

	// Created at timestamp
	CreatedAt time.Time `json:"created_at"`
}

SessionSummary contains a summary of a session

type Storage

type Storage interface {
	// Create creates a new session
	Create(ctx context.Context, session *Session) error

	// Get retrieves a session by ID
	Get(ctx context.Context, sessionID string) (*Session, error)

	// Update updates an existing session
	Update(ctx context.Context, session *Session) error

	// Delete deletes a session by ID
	Delete(ctx context.Context, sessionID string) error

	// List lists all sessions (with optional filters)
	List(ctx context.Context, filters map[string]interface{}) ([]*Session, error)

	// ListByAgent lists all sessions for a specific agent
	ListByAgent(ctx context.Context, agentID string) ([]*Session, error)

	// ListByUser lists all sessions for a specific user
	ListByUser(ctx context.Context, userID string) ([]*Session, error)

	// Close closes the storage connection
	Close() error
}

Storage defines the interface for session storage

type SummaryManager

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

SummaryManager 负责对 Session 生成摘要。

func NewSummaryManager

func NewSummaryManager(opts ...SummaryOption) *SummaryManager

NewSummaryManager 构造 SummaryManager。

func (*SummaryManager) Generate

func (m *SummaryManager) Generate(ctx context.Context, sess *Session) (*SessionSummary, error)

Generate 基于会话记录生成摘要。

func (*SummaryManager) OperationTimeout

func (m *SummaryManager) OperationTimeout() time.Duration

OperationTimeout 返回当前设置的超时时间。

type SummaryOption

type SummaryOption func(*SummaryManager)

SummaryOption 自定义 SummaryManager 行为。

func WithSummaryLogger

func WithSummaryLogger(logger *slog.Logger) SummaryOption

WithSummaryLogger 自定义日志记录器。

func WithSummaryModel

func WithSummaryModel(model models.Model) SummaryOption

WithSummaryModel 使用指定模型生成摘要。

func WithSummaryTimeout

func WithSummaryTimeout(timeout time.Duration) SummaryOption

WithSummaryTimeout 设置摘要生成超时时间。

Jump to

Keyboard shortcuts

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