memory

package
v0.12.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const DefaultCompactionThreshold = 10
View Source
const MainBranchID = "main"

Variables

This section is empty.

Functions

func DefaultShouldTriggerCompaction

func DefaultShouldTriggerCompaction(context CompactionDecisionContext) bool

DefaultShouldTriggerCompaction compacts when candidate count reaches threshold.

func IsOpenAIModelName

func IsOpenAIModelName(model string) bool

IsOpenAIModelName validates model names accepted for compaction.

func IsOpenAIResponsesCompactionAwareSession

func IsOpenAIResponsesCompactionAwareSession(session Session) bool

IsOpenAIResponsesCompactionAwareSession returns true if session supports compaction.

func ResolveSessionLimit added in v0.9.2

func ResolveSessionLimit(explicit *int, settings *SessionSettings) *int

ResolveSessionLimit returns the effective limit based on an explicit value and optional session settings.

func StartOpenAIConversationsSession

func StartOpenAIConversationsSession(ctx context.Context, client *openai.Client) (string, error)

StartOpenAIConversationsSession creates a conversation and returns its ID. When client is nil, it uses the default client.

Types

type AdvancedBranchInfo

type AdvancedBranchInfo struct {
	BranchID     string `json:"branch_id"`
	MessageCount int    `json:"message_count"`
	UserTurns    int    `json:"user_turns"`
	IsCurrent    bool   `json:"is_current"`
	CreatedAt    string `json:"created_at"`
}

type AdvancedConversationTurn

type AdvancedConversationTurn struct {
	Turn        int    `json:"turn"`
	Content     string `json:"content"`
	FullContent string `json:"full_content"`
	Timestamp   string `json:"timestamp"`
	CanBranch   bool   `json:"can_branch"`
}

type AdvancedConversationTurnItem

type AdvancedConversationTurnItem struct {
	Type     string `json:"type"`
	ToolName string `json:"tool_name,omitempty"`
}

type AdvancedSQLiteSession

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

AdvancedSQLiteSession adds branch-aware conversation history management on top of SQLiteSession.

func (*AdvancedSQLiteSession) AddItems

func (s *AdvancedSQLiteSession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*AdvancedSQLiteSession) ClearSession

func (s *AdvancedSQLiteSession) ClearSession(ctx context.Context) error

func (*AdvancedSQLiteSession) Close

func (s *AdvancedSQLiteSession) Close() error

func (*AdvancedSQLiteSession) CreateBranchFromContent

func (s *AdvancedSQLiteSession) CreateBranchFromContent(ctx context.Context, searchTerm, branchID string) (string, error)

CreateBranchFromContent creates a new branch from the first user turn containing searchTerm. When branchID is empty, a timestamped name is generated.

func (*AdvancedSQLiteSession) CreateBranchFromTurn

func (s *AdvancedSQLiteSession) CreateBranchFromTurn(ctx context.Context, turn int, branchID string) (string, error)

func (*AdvancedSQLiteSession) CurrentBranchID

func (s *AdvancedSQLiteSession) CurrentBranchID() string

func (*AdvancedSQLiteSession) DeleteBranch

func (s *AdvancedSQLiteSession) DeleteBranch(ctx context.Context, branchID string) error

func (*AdvancedSQLiteSession) DeleteBranchForce

func (s *AdvancedSQLiteSession) DeleteBranchForce(ctx context.Context, branchID string) error

DeleteBranchForce deletes branchID even if it is currently selected. When deleting the active branch, the current branch is switched to `main`.

func (*AdvancedSQLiteSession) FindTurnsByContent

func (s *AdvancedSQLiteSession) FindTurnsByContent(ctx context.Context, searchTerm, branchID string) ([]AdvancedConversationTurn, error)

FindTurnsByContent returns user turns containing searchTerm for the selected branch. If branchID is empty, the current branch is used.

func (*AdvancedSQLiteSession) GetConversationByTurns

func (s *AdvancedSQLiteSession) GetConversationByTurns(
	ctx context.Context, branchID string,
) (map[int][]AdvancedConversationTurnItem, error)

GetConversationByTurns groups item metadata by user turn number for the selected branch. If branchID is empty, the current branch is used.

func (*AdvancedSQLiteSession) GetConversationTurns

func (s *AdvancedSQLiteSession) GetConversationTurns(ctx context.Context, branchID string) ([]AdvancedConversationTurn, error)

GetConversationTurns returns the user turns in order for the selected branch. If branchID is empty, the current branch is used.

func (*AdvancedSQLiteSession) GetItems

func (s *AdvancedSQLiteSession) GetItems(ctx context.Context, limit int) ([]TResponseInputItem, error)

func (*AdvancedSQLiteSession) GetSessionUsage

func (s *AdvancedSQLiteSession) GetSessionUsage(ctx context.Context, branchID string) (*AdvancedSessionUsage, error)

GetSessionUsage returns cumulative usage for all branches or one specific branch. If branchID is empty, usage is aggregated across all branches.

func (*AdvancedSQLiteSession) GetToolUsage

func (s *AdvancedSQLiteSession) GetToolUsage(ctx context.Context, branchID string) ([]AdvancedToolUsage, error)

GetToolUsage returns tool usage count grouped by tool name and user turn. If branchID is empty, the current branch is used.

func (*AdvancedSQLiteSession) GetTurnUsage

func (s *AdvancedSQLiteSession) GetTurnUsage(
	ctx context.Context, branchID string, userTurnNumber ...int,
) ([]AdvancedTurnUsage, error)

GetTurnUsage returns usage for the selected branch. If userTurnNumber is omitted, all turns are returned in ascending order. If branchID is empty, the current branch is used.

func (*AdvancedSQLiteSession) ListBranchInfos

func (s *AdvancedSQLiteSession) ListBranchInfos(ctx context.Context) ([]AdvancedBranchInfo, error)

ListBranchInfos returns branch metadata similar to Python's list_branches output.

func (*AdvancedSQLiteSession) ListBranches

func (s *AdvancedSQLiteSession) ListBranches() []string

func (*AdvancedSQLiteSession) PopItem

func (*AdvancedSQLiteSession) SessionID

func (*AdvancedSQLiteSession) SessionSettings added in v0.9.2

func (s *AdvancedSQLiteSession) SessionSettings() *SessionSettings

func (*AdvancedSQLiteSession) StoreRunUsage

func (s *AdvancedSQLiteSession) StoreRunUsage(ctx context.Context, runUsage *usage.Usage) error

StoreRunUsage aggregates usage data for the current user turn in the active branch.

func (*AdvancedSQLiteSession) SwitchToBranch

func (s *AdvancedSQLiteSession) SwitchToBranch(branchID string) error

type AdvancedSQLiteSessionParams

type AdvancedSQLiteSessionParams struct {
	SessionID string

	// Optional database data source name. Defaults to `file::memory:?cache=shared`.
	DBDataSourceName string

	// Optional table names (shared by all branches).
	SessionTable   string
	MessagesTable  string
	TurnUsageTable string

	// Optional session settings (e.g., default history limit).
	SessionSettings *SessionSettings
}

type AdvancedSessionUsage

type AdvancedSessionUsage struct {
	Requests     uint64 `json:"requests"`
	InputTokens  uint64 `json:"input_tokens"`
	OutputTokens uint64 `json:"output_tokens"`
	TotalTokens  uint64 `json:"total_tokens"`
	TotalTurns   int    `json:"total_turns"`
}

type AdvancedToolUsage

type AdvancedToolUsage struct {
	ToolName   string `json:"tool_name"`
	UsageCount int    `json:"usage_count"`
	Turn       int    `json:"turn"`
}

type AdvancedTurnUsage

type AdvancedTurnUsage struct {
	UserTurnNumber      int                                        `json:"user_turn_number"`
	Requests            uint64                                     `json:"requests"`
	InputTokens         uint64                                     `json:"input_tokens"`
	OutputTokens        uint64                                     `json:"output_tokens"`
	TotalTokens         uint64                                     `json:"total_tokens"`
	InputTokensDetails  responses.ResponseUsageInputTokensDetails  `json:"input_tokens_details"`
	OutputTokensDetails responses.ResponseUsageOutputTokensDetails `json:"output_tokens_details"`
}

type AsyncSQLiteSession

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

AsyncSQLiteSession is a SQLite-based session implementation mirroring the async Python session semantics.

func NewAsyncSQLiteSession

func NewAsyncSQLiteSession(ctx context.Context, params AsyncSQLiteSessionParams) (_ *AsyncSQLiteSession, err error)

NewAsyncSQLiteSession initializes the async SQLite session.

func (*AsyncSQLiteSession) AddItems

func (s *AsyncSQLiteSession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*AsyncSQLiteSession) ClearSession

func (s *AsyncSQLiteSession) ClearSession(ctx context.Context) error

func (*AsyncSQLiteSession) Close

func (s *AsyncSQLiteSession) Close() error

Close the database connection.

func (*AsyncSQLiteSession) GetItems

func (s *AsyncSQLiteSession) GetItems(ctx context.Context, limit int) (_ []TResponseInputItem, err error)

func (*AsyncSQLiteSession) PopItem

func (*AsyncSQLiteSession) SessionID

func (s *AsyncSQLiteSession) SessionID(context.Context) string

func (*AsyncSQLiteSession) SessionSettings added in v0.9.2

func (s *AsyncSQLiteSession) SessionSettings() *SessionSettings

type AsyncSQLiteSessionParams

type AsyncSQLiteSessionParams struct {
	// Unique identifier for the conversation session.
	SessionID string

	// Optional database data source name. Defaults to `file::memory:?cache=shared`.
	DBDataSourceName string

	// Optional name of the table to store session metadata.
	SessionTable string

	// Optional name of the table to store message data.
	MessagesTable string

	// Optional session settings (e.g., default history limit).
	SessionSettings *SessionSettings
}

type CompactionDecisionContext

type CompactionDecisionContext struct {
	ResponseID               string
	CompactionMode           string
	CompactionCandidateItems []TResponseInputItem
	SessionItems             []TResponseInputItem
}

CompactionDecisionContext is passed to ShouldTriggerCompaction.

type DaprConcurrencyMode

type DaprConcurrencyMode string
const (
	DaprConcurrencyFirstWrite DaprConcurrencyMode = "first_write"
)

type DaprConsistencyLevel

type DaprConsistencyLevel string
const (
	DaprConsistencyEventual DaprConsistencyLevel = "eventual"
	DaprConsistencyStrong   DaprConsistencyLevel = "strong"
)

type DaprSession

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

DaprSession is a Dapr State Store-backed implementation of Session.

func NewDaprSession

func NewDaprSession(params DaprSessionParams) (*DaprSession, error)

func (*DaprSession) AddItems

func (s *DaprSession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*DaprSession) ClearSession

func (s *DaprSession) ClearSession(ctx context.Context) error

func (*DaprSession) Close

func (s *DaprSession) Close() error

func (*DaprSession) GetItems

func (s *DaprSession) GetItems(ctx context.Context, limit int) ([]TResponseInputItem, error)

func (*DaprSession) Ping

func (s *DaprSession) Ping(ctx context.Context) bool

func (*DaprSession) PopItem

func (s *DaprSession) PopItem(ctx context.Context) (*TResponseInputItem, error)

func (*DaprSession) SessionID

func (s *DaprSession) SessionID(context.Context) string

func (*DaprSession) SessionSettings added in v0.9.2

func (s *DaprSession) SessionSettings() *SessionSettings

type DaprSessionParams

type DaprSessionParams struct {
	SessionID       string
	StateStoreName  string
	Client          DaprStateClient
	TTL             time.Duration
	Consistency     DaprConsistencyLevel
	OwnsClient      bool
	SessionSettings *SessionSettings
}

type DaprStateClient

type DaprStateClient interface {
	GetState(ctx context.Context, storeName, key string, metadata map[string]string) (DaprStateItem, error)
	SaveState(ctx context.Context, storeName, key string, value []byte, etag string, metadata map[string]string, options DaprStateOptions) error
	DeleteState(ctx context.Context, storeName, key string, metadata map[string]string, options DaprStateOptions) error
	Close() error
}

DaprStateClient is the minimal state operations interface required by DaprSession.

type DaprStateItem

type DaprStateItem struct {
	Data []byte
	ETag string
}

DaprStateItem represents a state record returned from a Dapr state store.

type DaprStateOptions

type DaprStateOptions struct {
	Consistency DaprConsistencyLevel
	Concurrency DaprConcurrencyMode
}

DaprStateOptions represents write/read options for a Dapr state operation.

type EncryptedSession

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

EncryptedSession wraps any Session and transparently encrypts stored items.

Items are persisted as `compaction` input items containing encrypted payloads. During reads, valid encrypted items are decrypted back into their original input items. Expired or corrupted encrypted items are skipped.

func NewEncryptedSession

func NewEncryptedSession(params EncryptedSessionParams) (*EncryptedSession, error)

NewEncryptedSession creates a Session wrapper that encrypts/decrypts items around an underlying session backend.

func (*EncryptedSession) AddItems

func (s *EncryptedSession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*EncryptedSession) ClearSession

func (s *EncryptedSession) ClearSession(ctx context.Context) error

func (*EncryptedSession) Close

func (s *EncryptedSession) Close() error

Close delegates to the underlying session when it supports Close().

func (*EncryptedSession) GetItems

func (s *EncryptedSession) GetItems(ctx context.Context, limit int) ([]TResponseInputItem, error)

func (*EncryptedSession) IgnoreIDsForMatching added in v0.9.2

func (s *EncryptedSession) IgnoreIDsForMatching() bool

func (*EncryptedSession) PopItem

func (*EncryptedSession) SanitizeInputItemsForPersistence added in v0.9.2

func (s *EncryptedSession) SanitizeInputItemsForPersistence(items []TResponseInputItem) []TResponseInputItem

func (*EncryptedSession) SessionID

func (s *EncryptedSession) SessionID(context.Context) string

func (*EncryptedSession) SessionSettings added in v0.9.2

func (s *EncryptedSession) SessionSettings() *SessionSettings

type EncryptedSessionParams

type EncryptedSessionParams struct {
	// Optional session id. If omitted, it is derived from UnderlyingSession.SessionID.
	SessionID string

	// Underlying session implementation used for storage.
	UnderlyingSession Session

	// Master encryption key. Accepts either:
	// - a base64-url encoded 32-byte key
	// - any raw secret string
	EncryptionKey string

	// Token TTL. Expired encrypted entries are skipped when reading.
	// Defaults to 10 minutes when <= 0.
	TTL time.Duration

	// Optional clock source for deterministic testing.
	Now func() time.Time
}

type OpenAIConversationsSession

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

OpenAIConversationsSession stores chat history directly in OpenAI Conversations.

func NewOpenAIConversationsSession

func NewOpenAIConversationsSession(params OpenAIConversationsSessionParams) *OpenAIConversationsSession

NewOpenAIConversationsSession creates a Session backed by OpenAI Conversations.

func (*OpenAIConversationsSession) AddItems

func (*OpenAIConversationsSession) ClearSession

func (s *OpenAIConversationsSession) ClearSession(ctx context.Context) error

func (*OpenAIConversationsSession) GetItems

func (s *OpenAIConversationsSession) GetItems(
	ctx context.Context,
	limit int,
) ([]TResponseInputItem, error)

func (*OpenAIConversationsSession) IgnoreIDsForMatching added in v0.9.2

func (s *OpenAIConversationsSession) IgnoreIDsForMatching() bool

func (*OpenAIConversationsSession) PopItem

func (*OpenAIConversationsSession) SanitizeInputItemsForPersistence added in v0.9.2

func (s *OpenAIConversationsSession) SanitizeInputItemsForPersistence(items []TResponseInputItem) []TResponseInputItem

func (*OpenAIConversationsSession) SessionID

func (*OpenAIConversationsSession) SessionSettings added in v0.9.2

func (s *OpenAIConversationsSession) SessionSettings() *SessionSettings

type OpenAIConversationsSessionParams

type OpenAIConversationsSessionParams struct {
	ConversationID string
	Client         *openai.Client

	// Optional session settings (e.g., default history limit).
	SessionSettings *SessionSettings
	// contains filtered or unexported fields
}

OpenAIConversationsSessionParams configures a conversation-backed Session.

type OpenAIResponsesCompactionArgs

type OpenAIResponsesCompactionArgs struct {
	ResponseID     string
	CompactionMode OpenAIResponsesCompactionMode
	Store          *bool
	Force          bool
}

OpenAIResponsesCompactionArgs contains optional run parameters for compaction.

type OpenAIResponsesCompactionAwareSession

type OpenAIResponsesCompactionAwareSession interface {
	Session
	RunCompaction(ctx context.Context, args *OpenAIResponsesCompactionArgs) error
}

OpenAIResponsesCompactionAwareSession marks sessions that support responses compaction.

type OpenAIResponsesCompactionMode

type OpenAIResponsesCompactionMode string

OpenAIResponsesCompactionMode controls how compaction history is provided.

const (
	OpenAIResponsesCompactionModePreviousResponseID OpenAIResponsesCompactionMode = "previous_response_id"
	OpenAIResponsesCompactionModeInput              OpenAIResponsesCompactionMode = "input"
	OpenAIResponsesCompactionModeAuto               OpenAIResponsesCompactionMode = "auto"
)

type OpenAIResponsesCompactionSession

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

OpenAIResponsesCompactionSession is a Session decorator that compacts long history.

func NewOpenAIResponsesCompactionSession

func NewOpenAIResponsesCompactionSession(
	params OpenAIResponsesCompactionSessionParams,
) (*OpenAIResponsesCompactionSession, error)

NewOpenAIResponsesCompactionSession creates a compaction-aware session decorator.

func (*OpenAIResponsesCompactionSession) AddItems

func (*OpenAIResponsesCompactionSession) ClearSession

func (*OpenAIResponsesCompactionSession) GetItems

func (*OpenAIResponsesCompactionSession) IgnoreIDsForMatching added in v0.9.2

func (s *OpenAIResponsesCompactionSession) IgnoreIDsForMatching() bool

func (*OpenAIResponsesCompactionSession) PopItem

func (*OpenAIResponsesCompactionSession) RunCompaction

RunCompaction runs compaction and replaces underlying history with compacted output.

func (*OpenAIResponsesCompactionSession) SanitizeInputItemsForPersistence added in v0.9.2

func (s *OpenAIResponsesCompactionSession) SanitizeInputItemsForPersistence(items []TResponseInputItem) []TResponseInputItem

func (*OpenAIResponsesCompactionSession) SessionID

func (*OpenAIResponsesCompactionSession) SessionSettings added in v0.9.2

func (s *OpenAIResponsesCompactionSession) SessionSettings() *SessionSettings

type OpenAIResponsesCompactionSessionParams

type OpenAIResponsesCompactionSessionParams struct {
	SessionID         string
	UnderlyingSession Session
	Client            *openai.Client
	Model             string
	CompactionMode    OpenAIResponsesCompactionMode

	ShouldTriggerCompaction shouldTriggerCompactionFunc

	// Internal dependency injection hook used in tests.
	CompactionService responseCompactionService
}

OpenAIResponsesCompactionSessionParams configures OpenAIResponsesCompactionSession.

type PgConnInterface

type PgConnInterface interface {
	Query(ctx context.Context, sql string, args ...any) (PgRowsInterface, error)
	QueryRow(ctx context.Context, sql string, args ...any) PgRowInterface
	Exec(ctx context.Context, sql string, args ...any) (any, error)
	Close(ctx context.Context) error
}

PgConnInterface abstracts the database operations needed by PgSession. This allows for easy mocking in tests.

type PgConnWrapper

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

PgConnWrapper wraps a real pgx.Conn to implement PgConnInterface

func (*PgConnWrapper) Close

func (w *PgConnWrapper) Close(ctx context.Context) error

func (*PgConnWrapper) Exec

func (w *PgConnWrapper) Exec(ctx context.Context, sql string, args ...any) (any, error)

func (*PgConnWrapper) Query

func (w *PgConnWrapper) Query(ctx context.Context, sql string, args ...any) (PgRowsInterface, error)

func (*PgConnWrapper) QueryRow

func (w *PgConnWrapper) QueryRow(ctx context.Context, sql string, args ...any) PgRowInterface

type PgRowInterface

type PgRowInterface interface {
	Scan(dest ...any) error
}

PgRowInterface abstracts the row operations for easier mocking

type PgRowWrapper

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

PgRowWrapper wraps pgx.Row to implement PgRowInterface

func (*PgRowWrapper) Scan

func (w *PgRowWrapper) Scan(dest ...any) error

type PgRowsInterface

type PgRowsInterface interface {
	Next() bool
	Scan(dest ...any) error
	Err() error
	Close()
}

PgRowsInterface abstracts the rows operations for easier mocking

type PgRowsWrapper

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

PgRowsWrapper wraps pgx.Rows to implement PgRowsInterface

func (*PgRowsWrapper) Close

func (w *PgRowsWrapper) Close()

func (*PgRowsWrapper) Err

func (w *PgRowsWrapper) Err() error

func (*PgRowsWrapper) Next

func (w *PgRowsWrapper) Next() bool

func (*PgRowsWrapper) Scan

func (w *PgRowsWrapper) Scan(dest ...any) error

type PgSession

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

PgSession is a PostgreSQL-based implementation of Session storage.

This implementation stores conversation history in a PostgreSQL database. Requires a valid PostgreSQL connection string.

func NewPgSession

func NewPgSession(ctx context.Context, params PgSessionParams) (_ *PgSession, err error)

NewPgSession initializes the PostgreSQL session.

func (*PgSession) AddItems

func (s *PgSession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*PgSession) ClearSession

func (s *PgSession) ClearSession(ctx context.Context) error

func (*PgSession) Close

func (s *PgSession) Close(ctx context.Context) error

Close the database connection.

func (*PgSession) GetItems

func (s *PgSession) GetItems(ctx context.Context, limit int) (_ []TResponseInputItem, err error)

func (*PgSession) PopItem

func (s *PgSession) PopItem(ctx context.Context) (*TResponseInputItem, error)

func (*PgSession) SessionID

func (s *PgSession) SessionID(context.Context) string

func (*PgSession) SessionSettings added in v0.9.2

func (s *PgSession) SessionSettings() *SessionSettings

type PgSessionParams

type PgSessionParams struct {
	// Unique identifier for the conversation session
	SessionID string

	// PostgreSQL connection string.
	// Example: "postgres://user:password@localhost:5432/database"
	ConnectionString string

	// Optional name of the table to store session metadata.
	// Defaults to "agent_sessions".
	SessionTable string

	// Optional name of the table to store message data.
	// Defaults to "agent_messages".
	MessagesTable string

	// Optional connection interface for dependency injection (mainly for testing)
	Conn PgConnInterface

	// Optional session settings (e.g., default history limit).
	SessionSettings *SessionSettings
}

type RedisSession

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

RedisSession is a Redis-backed implementation of Session storage.

func NewRedisSession

func NewRedisSession(ctx context.Context, params RedisSessionParams) (*RedisSession, error)

NewRedisSession initializes a Redis session.

func (*RedisSession) AddItems

func (s *RedisSession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*RedisSession) ClearSession

func (s *RedisSession) ClearSession(ctx context.Context) error

func (*RedisSession) Close

func (s *RedisSession) Close() error

Close closes the Redis client if this session owns it.

func (*RedisSession) GetItems

func (s *RedisSession) GetItems(ctx context.Context, limit int) ([]TResponseInputItem, error)

func (*RedisSession) Ping

func (s *RedisSession) Ping(ctx context.Context) bool

Ping checks Redis connectivity.

func (*RedisSession) PopItem

func (s *RedisSession) PopItem(ctx context.Context) (*TResponseInputItem, error)

func (*RedisSession) SessionID

func (s *RedisSession) SessionID(context.Context) string

func (*RedisSession) SessionSettings added in v0.9.2

func (s *RedisSession) SessionSettings() *SessionSettings

type RedisSessionParams

type RedisSessionParams struct {
	// Unique identifier for the conversation session.
	SessionID string

	// Existing Redis client. When provided, this session does not close the client.
	Client redis.UniversalClient

	// Redis URL used to create a dedicated client when Client is nil.
	// Example: redis://localhost:6379/0
	URL string

	// Optional key prefix for all session keys.
	// Defaults to "agents:session".
	KeyPrefix string

	// Optional TTL for session keys. Zero means no expiration.
	TTL time.Duration

	// Optional session settings (e.g., default history limit).
	SessionSettings *SessionSettings
}

type RunUsageTrackingAwareSession

type RunUsageTrackingAwareSession interface {
	Session
	StoreRunUsage(ctx context.Context, runUsage *usage.Usage) error
}

RunUsageTrackingAwareSession extends Session with usage tracking support. Sessions implementing this interface can persist token and request usage per turn.

type SQLAlchemySession

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

SQLAlchemySession is a compatibility session wrapper that maps SQLAlchemy-like URLs to Go session implementations.

Supported URL schemes: - PostgreSQL: postgresql+asyncpg://..., postgresql://..., postgres://... - SQLite: sqlite+aiosqlite://..., sqlite://...

func NewSQLAlchemySession

func NewSQLAlchemySession(ctx context.Context, params SQLAlchemySessionParams) (*SQLAlchemySession, error)

func (*SQLAlchemySession) AddItems

func (s *SQLAlchemySession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*SQLAlchemySession) ClearSession

func (s *SQLAlchemySession) ClearSession(ctx context.Context) error

func (*SQLAlchemySession) Close

func (s *SQLAlchemySession) Close(ctx context.Context) error

func (*SQLAlchemySession) GetItems

func (s *SQLAlchemySession) GetItems(ctx context.Context, limit int) ([]TResponseInputItem, error)

func (*SQLAlchemySession) PopItem

func (*SQLAlchemySession) SessionID

func (s *SQLAlchemySession) SessionID(ctx context.Context) string

func (*SQLAlchemySession) SessionSettings added in v0.9.2

func (s *SQLAlchemySession) SessionSettings() *SessionSettings

type SQLAlchemySessionParams

type SQLAlchemySessionParams struct {
	// Optional pre-configured backend session.
	Session Session

	// SQLAlchemy-like connection URL.
	URL string

	// Session id used when URL-based construction is used.
	SessionID string

	// Optional table overrides used when URL-based construction is used.
	SessionsTable string
	MessagesTable string

	// Optional session settings (e.g., default history limit).
	SessionSettings *SessionSettings
}

type SQLiteSession

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

SQLiteSession is a SQLite-based implementation of Session storage.

This implementation stores conversation history in a SQLite database. By default, uses an in-memory database that is lost when the process ends. For persistent storage, provide a file path.

func NewSQLiteSession

func NewSQLiteSession(ctx context.Context, params SQLiteSessionParams) (_ *SQLiteSession, err error)

NewSQLiteSession initializes the SQLite session.

func (*SQLiteSession) AddItems

func (s *SQLiteSession) AddItems(ctx context.Context, items []TResponseInputItem) error

func (*SQLiteSession) ClearSession

func (s *SQLiteSession) ClearSession(ctx context.Context) error

func (*SQLiteSession) Close

func (s *SQLiteSession) Close() error

Close the database connection.

func (*SQLiteSession) GetItems

func (s *SQLiteSession) GetItems(ctx context.Context, limit int) (_ []TResponseInputItem, err error)

func (*SQLiteSession) PopItem

func (*SQLiteSession) SessionID

func (s *SQLiteSession) SessionID(context.Context) string

func (*SQLiteSession) SessionSettings added in v0.9.2

func (s *SQLiteSession) SessionSettings() *SessionSettings

type SQLiteSessionParams

type SQLiteSessionParams struct {
	// Unique identifier for the conversation session
	SessionID string

	// Optional database data source name.
	// Defaults to ':memory:' (in-memory database).
	DBDataSourceName string

	// Optional name of the table to store session metadata.
	// Defaults to "file::memory:?cache=shared".
	SessionTable string

	// Optional name of the table to store message data.
	// Defaults to "agent_messages".
	MessagesTable string

	// Optional session settings (e.g., default history limit).
	SessionSettings *SessionSettings
}

type Session

type Session interface {
	SessionID(context.Context) string

	// GetItems retrieves the conversation history for this session.
	//
	// `limit` is the maximum number of items to retrieve. If <= 0, retrieves all items.
	// When specified, returns the latest N items in chronological order.
	GetItems(ctx context.Context, limit int) ([]TResponseInputItem, error)

	// AddItems adds new items to the conversation history.
	AddItems(ctx context.Context, items []TResponseInputItem) error

	// PopItem removes and returns the most recent item from the session.
	// It returns nil if the session is empty.
	PopItem(context.Context) (*TResponseInputItem, error)

	// ClearSession clears all items for this session.
	ClearSession(context.Context) error
}

A Session stores conversation history for a specific session, allowing agents to maintain context without requiring explicit manual memory management.

type SessionIgnoreIDsProvider added in v0.9.2

type SessionIgnoreIDsProvider interface {
	IgnoreIDsForMatching() bool
}

SessionIgnoreIDsProvider indicates item IDs should be ignored when matching persisted items.

type SessionInputCallback added in v0.9.2

type SessionInputCallback func(history []TResponseInputItem, newInput []TResponseInputItem) ([]TResponseInputItem, error)

SessionInputCallback customizes how new input is merged with history. It should return the combined input items to send to the model.

type SessionItemSanitizer added in v0.9.2

type SessionItemSanitizer interface {
	SanitizeInputItemsForPersistence(items []TResponseInputItem) []TResponseInputItem
}

SessionItemSanitizer allows sessions to sanitize items before persistence.

type SessionSettings added in v0.9.2

type SessionSettings struct {
	// Limit sets the maximum number of items to retrieve. If nil, retrieves all items.
	Limit *int
}

SessionSettings holds optional configuration for session operations.

func (*SessionSettings) Resolve added in v0.9.2

func (s *SessionSettings) Resolve(override *SessionSettings) *SessionSettings

Resolve overlays non-nil values from override onto the receiver.

type SessionSettingsProvider added in v0.9.2

type SessionSettingsProvider interface {
	SessionSettings() *SessionSettings
}

SessionSettingsProvider exposes session-level defaults for history retrieval.

type TResponseInputItem

type TResponseInputItem = responses.ResponseInputItemUnionParam

TResponseInputItem is a type alias for the ResponseInputItemUnionParam type from the OpenAI SDK.

func SelectCompactionCandidateItems

func SelectCompactionCandidateItems(items []TResponseInputItem) []TResponseInputItem

SelectCompactionCandidateItems excludes user messages and prior compaction items.

Jump to

Keyboard shortcuts

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