Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v1.41.2 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package session provides session management for Hector v2.

Sessions represent a series of interactions between a user and agents. Each session has:

  • A unique identifier
  • Associated app and user
  • State (key-value store with scope prefixes)
  • Event history

Index

Constants

View Source
const (
	// KeyPrefixApp is for app-level state (shared across all users/sessions).
	KeyPrefixApp = "app:"

	// KeyPrefixUser is for user-level state (shared across sessions for a user).
	KeyPrefixUser = "user:"

	// KeyPrefixTemp is for temporary state (discarded after invocation).
	KeyPrefixTemp = "temp:"
)

State prefixes for scoping state keys.

Variables

View Source
var ErrSessionNotFound = errors.New("session not found")

ErrSessionNotFound is returned when a session doesn't exist.

View Source
var ErrStaleSession = fmt.Errorf("stale session: session has been modified since it was loaded")

ErrStaleSession is returned when attempting to modify a session that has been updated elsewhere since it was loaded.

View Source
var ErrStateKeyNotExist = errors.New("state key does not exist")

ErrStateKeyNotExist is returned when a state key doesn't exist.

Functions

This section is empty.

Types

type CreateRequest

type CreateRequest struct {
	AppName   string
	UserID    string
	SessionID string // Optional - generated if empty
	State     map[string]any
}

CreateRequest contains parameters for creating a session.

type CreateResponse

type CreateResponse struct {
	Session Session
}

CreateResponse contains the created session.

type DeleteRequest

type DeleteRequest struct {
	AppName   string
	UserID    string
	SessionID string
}

DeleteRequest contains parameters for deleting a session.

type GetRequest

type GetRequest struct {
	AppName   string
	UserID    string
	SessionID string

	// NumRecentEvents returns at most N most recent events.
	// Optional: if zero, returns all events.
	NumRecentEvents int

	// AfterEventID returns events after the event with this ID (by sequence_num).
	// Optional: if empty, returns all events.
	AfterEventID string
}

GetRequest contains parameters for retrieving a session.

type GetResponse

type GetResponse struct {
	Session Session
}

GetResponse contains the retrieved session.

type ListRequest

type ListRequest struct {
	AppName   string
	UserID    string
	PageSize  int
	PageToken string
}

ListRequest contains parameters for listing sessions.

type ListResponse

type ListResponse struct {
	Sessions      []Session
	NextPageToken string
}

ListResponse contains the list of sessions.

type SQLSessionService

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

SQLSessionService implements Service using a SQL database. Concurrency is handled by database-level locking (transactions).

func NewSQLSessionService

func NewSQLSessionService(db *sql.DB, dialect string) (*SQLSessionService, error)

NewSQLSessionService creates a new SQL-based session service.

func (*SQLSessionService) AppendEvent

func (s *SQLSessionService) AppendEvent(ctx context.Context, session Session, event *agent.Event) error

AppendEvent adds an event to the session history. Uses optimistic concurrency control to detect stale sessions.

func (*SQLSessionService) Close

func (s *SQLSessionService) Close() error

Close closes the database connection.

func (*SQLSessionService) Create

Create creates a new session.

func (*SQLSessionService) Delete

func (s *SQLSessionService) Delete(ctx context.Context, req *DeleteRequest) error

Delete removes a session.

func (*SQLSessionService) DeleteByApp added in v1.21.0

func (s *SQLSessionService) DeleteByApp(ctx context.Context, appName string) error

DeleteByApp removes all sessions and associated data for a specific app.

func (*SQLSessionService) Get

Get retrieves an existing session.

func (*SQLSessionService) List

List returns sessions matching the filter criteria.

func (*SQLSessionService) ReplaceEvents added in v1.21.0

func (s *SQLSessionService) ReplaceEvents(ctx context.Context, session Session, events []*agent.Event) error

ReplaceEvents replaces all events in the session with the given events. This is used by the summary_buffer strategy to compact the session after summarization. The operation is atomic - either all events are replaced or none are.

type Service

type Service interface {
	// Get retrieves an existing session.
	Get(ctx context.Context, req *GetRequest) (*GetResponse, error)

	// Create creates a new session.
	Create(ctx context.Context, req *CreateRequest) (*CreateResponse, error)

	// AppendEvent adds an event to the session history.
	AppendEvent(ctx context.Context, session Session, event *agent.Event) error

	// ReplaceEvents replaces all events in the session with the given events.
	// This is used by summary_buffer strategy to compact the session after summarization.
	ReplaceEvents(ctx context.Context, session Session, events []*agent.Event) error

	// List returns sessions matching the filter criteria.
	List(ctx context.Context, req *ListRequest) (*ListResponse, error)

	// Delete removes a session.
	Delete(ctx context.Context, req *DeleteRequest) error

	// DeleteByApp removes all sessions and associated data for a specific app.
	DeleteByApp(ctx context.Context, appName string) error
}

Service manages session lifecycle and persistence.

func InMemoryService

func InMemoryService() Service

InMemoryService returns an in-memory session service. Useful for testing and development.

func NewSessionService added in v1.21.0

func NewSessionService(pool *config.DBPool, databaseDSN string) (Service, error)

NewSessionService creates a session Service using the provided database configuration. This is the new clean interface that accepts separated configs.

type Session

type Session interface {
	// ID returns the unique session identifier.
	ID() string

	// AppName returns the application name.
	AppName() string

	// UserID returns the user identifier.
	UserID() string

	// State returns the session state store.
	State() agent.State

	// Events returns the session event history.
	Events() agent.Events

	// LastUpdateTime returns when the session was last modified.
	LastUpdateTime() time.Time
}

Session represents a conversation session between user and agents.

Jump to

Keyboard shortcuts

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