session

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package session owns the session core: the two facts every interaction policy depends on, and the identity and media plumbing a live session needs.

The duplex state is the reason this package exists. Before it, "is the user speaking" lived in the gateway's VAD, "is the agent speaking" lived in the speech scheduler's active phase, and a state machine that modelled both sat unwired in a third package. Three answers to two questions is three chances to disagree, and every overlap decision depends on getting them right.

Index

Constants

This section is empty.

Variables

View Source
var ErrMediaExpired = errors.New("media handle is no longer retained")

ErrMediaExpired means a handle named media that retention has released. It is an ordinary outcome rather than a failure: the persistent text an observer committed is what survives, and images are an optional attachment with a bounded window.

Functions

This section is empty.

Types

type Duplex

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

Duplex is the single authoritative owner of the two facts.

func NewDuplex

func NewDuplex(config DuplexConfig) *Duplex

NewDuplex creates a duplex state in the listening phase.

func (*Duplex) AgentAudioHandedOff

func (duplex *Duplex) AgentAudioHandedOff(utteranceID string, duration time.Duration) error

AgentAudioHandedOff records that audio of the given duration has been handed to the client for an utterance.

This is the only way agent-speaking becomes true, which is the point: it is called by the code that puts frames on the wire, not by the code that decides to speak. The horizon extends from whichever is later - now, or the end of audio already handed off - so a stream that runs ahead of real time still reports playback ending when the user will actually stop hearing it.

func (*Duplex) AgentAudioStopped

func (duplex *Duplex) AgentAudioStopped(atNS uint64)

AgentAudioStopped ends agent playback immediately, whatever the horizon said. Barge-in cancellation uses it: frames that were never sent are not audio the user is hearing.

func (*Duplex) Close

func (duplex *Duplex) Close()

Close stops the playout watcher. It does not publish a transition: a closed session has no conversational state to report.

func (*Duplex) Observe

func (duplex *Duplex) Observe(observer Observer) func()

Observe registers an observer and returns a function that removes it.

func (*Duplex) Snapshot

func (duplex *Duplex) Snapshot() Snapshot

Snapshot returns the current state.

func (*Duplex) UserSpeechStarted

func (duplex *Duplex) UserSpeechStarted(atNS uint64)

UserSpeechStarted records that the user began speaking. The source is the acoustic gate, or a model-native signal where the binding provides one. Repeated starts without an intervening stop are idempotent.

func (*Duplex) UserSpeechStopped

func (duplex *Duplex) UserSpeechStopped(atNS uint64)

UserSpeechStopped records that the user stopped speaking.

type DuplexConfig

type DuplexConfig struct {
	// Scheduler defaults to the system clock. Tests pass a manual one so a
	// playout horizon can be crossed deterministically.
	Scheduler clock.Scheduler
}

DuplexConfig supplies the scheduler used to notice that playback finished.

type Media

type Media struct {
	Ref   trajectory.MediaRef
	Bytes []byte
}

Media is one retained frame or attachment.

type MediaConfig

type MediaConfig struct {
	// MaxItems bounds how many attachments are retained at once.
	MaxItems int
	// MaxBytes bounds total retained size.
	MaxBytes int
	// Window bounds age. Zero means age is not a bound.
	Window    time.Duration
	Scheduler clock.Scheduler
}

MediaConfig bounds retention. Every bound is enforced; the first one that binds is the one that evicts.

type MediaMetrics

type MediaMetrics struct {
	Retained     uint64 `json:"retained"`
	Evicted      uint64 `json:"evicted"`
	Misses       uint64 `json:"misses"`
	LiveItems    int    `json:"live_items"`
	LiveBytes    int    `json:"live_bytes"`
	MaxItems     int    `json:"max_items"`
	MaxBytes     int    `json:"max_bytes"`
	WindowMillis int64  `json:"window_ms"`
}

MediaMetrics is operational telemetry with no media content in it.

type MediaStore

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

MediaStore holds attachment bytes outside the trajectory.

The trajectory is copied for every continuation request, so it carries handles and never bytes. This is where the bytes live, under bounds a deployment sets, and a handle that has aged out simply stops resolving.

func NewMediaStore

func NewMediaStore(config MediaConfig) (*MediaStore, error)

NewMediaStore creates a bounded attachment store.

func (*MediaStore) Metrics

func (store *MediaStore) Metrics() MediaMetrics

func (*MediaStore) Release

func (store *MediaStore) Release(handle string)

Release drops one handle early, for a caller that knows it is finished.

func (*MediaStore) Resolve

func (store *MediaStore) Resolve(handle string) (Media, error)

Resolve returns retained media, or ErrMediaExpired once retention released it. A provider adapter that cannot use media never calls this at all.

func (*MediaStore) Retain

func (store *MediaStore) Retain(reference trajectory.MediaRef, payload []byte) (trajectory.MediaRef, error)

Retain stores bytes and returns the handle that names them. The caller keeps the returned reference and puts it on a trajectory item; it must not keep the bytes.

type Observer

type Observer func(Transition)

Observer receives transitions. It is called without the duplex lock held, in transition order, and must not block: the wake-ups it drives are signals, not work.

type Phase

type Phase string

Phase is the conversational configuration derived from the two facts. It is derived rather than stored, so it can never disagree with them.

const (
	// PhaseListening: neither party is producing audio.
	PhaseListening Phase = "listening"
	// PhaseUserSpeaking: the user is speaking and the agent is not.
	PhaseUserSpeaking Phase = "user_speaking"
	// PhaseAgentSpeaking: agent audio is reaching the user.
	PhaseAgentSpeaking Phase = "agent_speaking"
	// PhaseOverlap: both are producing audio. Whether that overlap is an
	// interruption, a backchannel, or side speech is an interaction policy
	// judgement, not a fact, and is decided elsewhere.
	PhaseOverlap Phase = "overlap"
)

type Snapshot

type Snapshot struct {
	Phase         Phase `json:"phase"`
	UserSpeaking  bool  `json:"user_speaking"`
	AgentSpeaking bool  `json:"agent_speaking"`

	UserSpeechStartedNS uint64 `json:"user_speech_started_ns,omitempty"`
	UserSpeechEndedNS   uint64 `json:"user_speech_ended_ns,omitempty"`
	AgentAudioStartedNS uint64 `json:"agent_audio_started_ns,omitempty"`
	// PlayoutHorizonNS is when the audio already handed to the client finishes
	// playing. It is the server's honest estimate of what the user is hearing:
	// audio is on the wire and its duration has not yet elapsed.
	PlayoutHorizonNS uint64 `json:"playout_horizon_ns,omitempty"`
	AgentUtteranceID string `json:"agent_utterance_id,omitempty"`
	// Generation increments on every transition, so a policy can tell whether
	// the state it decided against is still the current one.
	Generation uint64 `json:"generation"`
}

Snapshot is the complete duplex state at one instant.

AgentSpeaking means audio is reaching the user right now. It does not mean a continuation is generating, and it does not mean speech was enqueued. Synthesised audio takes real time to play, and conflating the decision to speak with the user hearing us gets every overlap decision wrong.

func (Snapshot) Overlapping

func (snapshot Snapshot) Overlapping() bool

Overlapping reports whether both parties are producing audio.

func (Snapshot) Silent

func (snapshot Snapshot) Silent() bool

Silent reports whether neither party is producing audio. It is the condition under which deferred work can run without talking over anyone.

func (Snapshot) String

func (snapshot Snapshot) String() string

String renders the state for logs.

type Transition

type Transition struct {
	Kind   TransitionKind `json:"kind"`
	AtNS   uint64         `json:"at_ns"`
	Before Snapshot       `json:"before"`
	After  Snapshot       `json:"after"`
}

Transition is one edge, with the state on both sides of it.

Every deferral condition an interaction policy can express is a predicate over Snapshot, so every one of them clears on some transition here. That is what makes "every deferral has a wake-up" mechanically checkable rather than a discipline each policy has to remember.

func (Transition) Reason

func (transition Transition) Reason() string

Reason renders a transition as a wake-up reason.

type TransitionKind

type TransitionKind string

TransitionKind names the four edges of the duplex state.

const (
	UserSpeechStarted TransitionKind = "user_speech_started"
	UserSpeechStopped TransitionKind = "user_speech_stopped"
	AgentAudioStarted TransitionKind = "agent_audio_started"
	AgentAudioStopped TransitionKind = "agent_audio_stopped"
)

Jump to

Keyboard shortcuts

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