Documentation
¶
Overview ¶
Package session provides experimental session management APIs for Genkit.
A session encapsulates a stateful execution environment with strongly-typed state that can be persisted across requests. Sessions are useful for maintaining user preferences, conversation context, or any application state that needs to survive between interactions.
APIs in this package are under active development and may change in any minor version release. Use with caution in production environments.
When these APIs stabilize, they will be moved to the core package and these exports will be deprecated.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContext ¶
NewContext returns a new context with the session attached.
func StateFromContext ¶
StateFromContext retrieves the current session state from context without requiring knowledge of the state type. This is useful for template rendering where the state type is not known at compile time. Returns nil if no session is in context.
Types ¶
type InMemoryStore ¶
type InMemoryStore[S any] struct { // contains filtered or unexported fields }
InMemoryStore is a thread-safe in-memory implementation of Store. Useful for testing or single-instance deployments where persistence is not required.
func NewInMemoryStore ¶
func NewInMemoryStore[S any]() *InMemoryStore[S]
NewInMemoryStore creates a new in-memory session store.
type NotFoundError ¶
type NotFoundError struct {
SessionID string
}
NotFoundError is returned when a session cannot be found in the store.
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
type Option ¶
type Option[S any] interface { // contains filtered or unexported methods }
Option configures a Session during creation.
func WithInitialState ¶
WithInitialState sets the initial state for a new session.
type Session ¶
type Session[S any] struct { // contains filtered or unexported fields }
Session represents a stateful environment with typed state. The type parameter S defines the shape of the session state and must be JSON-serializable for persistence.
func FromContext ¶
FromContext retrieves the current session from context. Returns nil if no session is in context or if the type doesn't match.
func Load ¶
Load loads an existing session from the store. Returns an error if the session is not found or if loading fails.
func New ¶
New creates a new session with the provided options. If a store is provided via WithStore, the session is persisted immediately. If no store is provided, the session exists only in memory for the current request and can be propagated via context using NewContext. If no ID is provided, a new UUID is generated. If no initial state is provided, the session is created with an empty state.
type Store ¶
type Store[S any] interface { // Get retrieves session data by ID. Returns nil if not found. Get(ctx context.Context, sessionID string) (*Data[S], error) // Save persists session data, creating or updating as needed. Save(ctx context.Context, sessionID string, data *Data[S]) error }
Store persists session data to a backend (database, file, memory, etc). Implementations must be safe for concurrent use.