session

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

func NewContext[S any](ctx context.Context, s *Session[S]) context.Context

NewContext returns a new context with the session attached.

func StateFromContext

func StateFromContext(ctx context.Context) any

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 Data

type Data[S any] struct {
	ID    string `json:"id"`
	State S      `json:"state,omitempty"`
}

Data is the serializable session state persisted by Store.

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.

func (*InMemoryStore[S]) Get

func (s *InMemoryStore[S]) Get(_ context.Context, sessionID string) (*Data[S], error)

Get retrieves session data by ID.

func (*InMemoryStore[S]) Save

func (s *InMemoryStore[S]) Save(_ context.Context, sessionID string, data *Data[S]) error

Save persists session data.

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 WithID

func WithID[S any](id string) Option[S]

WithID sets a custom session ID. If not provided, a UUID is generated.

func WithInitialState

func WithInitialState[S any](state S) Option[S]

WithInitialState sets the initial state for a new session.

func WithStore

func WithStore[S any](store Store[S]) Option[S]

WithStore sets the persistence backend for the session. If not provided, the session is not persisted and exists only in memory.

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

func FromContext[S any](ctx context.Context) *Session[S]

FromContext retrieves the current session from context. Returns nil if no session is in context or if the type doesn't match.

func Load

func Load[S any](ctx context.Context, store Store[S], sessionID string) (*Session[S], error)

Load loads an existing session from the store. Returns an error if the session is not found or if loading fails.

func New

func New[S any](ctx context.Context, opts ...Option[S]) (*Session[S], error)

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.

func (*Session[S]) ID

func (s *Session[S]) ID() string

ID returns the session's unique identifier.

func (*Session[S]) State

func (s *Session[S]) State() S

State returns the current session state. The returned value is a copy; modifications do not affect the session.

func (*Session[S]) UpdateState

func (s *Session[S]) UpdateState(ctx context.Context, state S) error

UpdateState updates the session state and persists it to the store (if configured).

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.

Jump to

Keyboard shortcuts

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