session

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionNotFound  = errors.New("session not found")
	ErrSessionExpired   = errors.New("session expired")
	ErrDuplicateSession = errors.New("duplicate session")
)

Functions

func SessionKeyFromContext

func SessionKeyFromContext(ctx context.Context) string

SessionKeyFromContext extracts the session key from context.

func WithSessionKey

func WithSessionKey(ctx context.Context, key string) context.Context

WithSessionKey adds a session key to the context.

Types

type EntStore

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

EntStore implements Store using entgo.io

func NewEntStore

func NewEntStore(dbPath string, opts ...StoreOption) (*EntStore, error)

NewEntStore creates a new ent-backed session store

func NewEntStoreWithClient

func NewEntStoreWithClient(client *ent.Client, opts ...StoreOption) *EntStore

NewEntStoreWithClient creates a new ent-backed session store using an existing ent.Client. This avoids opening a second database connection when the client is already available (e.g., from the bootstrap process). Schema migration is assumed to be already complete.

func (*EntStore) AppendMessage

func (s *EntStore) AppendMessage(key string, msg Message) error

AppendMessage adds a message to session history

func (*EntStore) Client

func (s *EntStore) Client() *ent.Client

Client returns the ent client

func (*EntStore) Close

func (s *EntStore) Close() error

Close closes the ent client and underlying database connection. When the client was provided externally via NewEntStoreWithClient, only the ent client is closed; the raw DB connection is managed by the caller.

func (*EntStore) CompactMessages

func (s *EntStore) CompactMessages(key string, upToIndex int, summary string) error

CompactMessages replaces messages up to (and including) upToIndex with a single summary message. This achieves compaction: the original messages are removed and replaced by a condensed version, preserving recent context.

func (*EntStore) Create

func (s *EntStore) Create(session *Session) error

Create creates a new session

func (*EntStore) Delete

func (s *EntStore) Delete(key string) error

Delete removes a session

func (*EntStore) Get

func (s *EntStore) Get(key string) (*Session, error)

Get retrieves a session by key

func (*EntStore) GetChecksum

func (s *EntStore) GetChecksum(name string) ([]byte, error)

GetChecksum retrieves the passphrase checksum by name

func (*EntStore) GetSalt

func (s *EntStore) GetSalt(name string) ([]byte, error)

GetSalt retrieves the encryption salt by name

func (*EntStore) MigrateSecrets

func (s *EntStore) MigrateSecrets(ctx context.Context, reencryptFn func([]byte) ([]byte, error), newSalt, newChecksum []byte) (err error)

MigrateSecrets performs the secret migration using callbacks to avoid import cycles. reencryptFn typically decrypts with old key and encrypts with new key.

func (*EntStore) SetChecksum

func (s *EntStore) SetChecksum(name string, checksum []byte) error

SetChecksum stores the passphrase checksum by name

func (*EntStore) SetSalt

func (s *EntStore) SetSalt(name string, salt []byte) error

SetSalt stores the encryption salt by name

func (*EntStore) Update

func (s *EntStore) Update(session *Session) error

Update updates an existing session

type Message

type Message struct {
	Role      types.MessageRole `json:"role"` // "user", "assistant", "tool"
	Content   string            `json:"content"`
	Timestamp time.Time         `json:"timestamp"`
	ToolCalls []ToolCall        `json:"toolCalls,omitempty"`
	Author    string            `json:"author,omitempty"` // ADK agent name for multi-agent routing
}

Message represents a single message in conversation history

type Session

type Session struct {
	Key         string            `json:"key"`
	AgentID     string            `json:"agentId,omitempty"`
	ChannelType string            `json:"channelType,omitempty"`
	ChannelID   string            `json:"channelId,omitempty"`
	History     []Message         `json:"history"`
	Metadata    map[string]string `json:"metadata,omitempty"`
	Model       string            `json:"model,omitempty"`
	CreatedAt   time.Time         `json:"createdAt"`
	UpdatedAt   time.Time         `json:"updatedAt"`
}

Session represents a conversation session

type Store

type Store interface {
	// Create creates a new session
	Create(session *Session) error
	// Get retrieves a session by key
	Get(key string) (*Session, error)
	// Update updates an existing session
	Update(session *Session) error
	// Delete removes a session
	Delete(key string) error
	// AppendMessage adds a message to session history
	AppendMessage(key string, msg Message) error
	// Close closes the store
	Close() error

	// GetSalt retrieves the encryption salt for LocalCryptoProvider
	GetSalt(name string) ([]byte, error)
	// SetSalt stores the encryption salt for LocalCryptoProvider
	SetSalt(name string, salt []byte) error
}

Store defines the interface for session storage

type StoreOption

type StoreOption func(*EntStore)

StoreOption defines the functional option pattern for EntStore

func WithMaxHistoryTurns

func WithMaxHistoryTurns(n int) StoreOption

WithMaxHistoryTurns limits the number of messages kept per session.

func WithPassphrase

func WithPassphrase(passphrase string) StoreOption

WithPassphrase sets the encryption passphrase for the database.

func WithTTL

func WithTTL(d time.Duration) StoreOption

WithTTL sets the session time-to-live.

type ToolCall

type ToolCall struct {
	ID     string `json:"id"`
	Name   string `json:"name"`
	Input  string `json:"input"`
	Output string `json:"output,omitempty"`
}

ToolCall represents a tool invocation

Jump to

Keyboard shortcuts

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