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 ¶
- Variables
- type Duplex
- func (duplex *Duplex) AgentAudioHandedOff(utteranceID string, duration time.Duration) error
- func (duplex *Duplex) AgentAudioStopped(atNS uint64)
- func (duplex *Duplex) Close()
- func (duplex *Duplex) Observe(observer Observer) func()
- func (duplex *Duplex) Snapshot() Snapshot
- func (duplex *Duplex) UserSpeechStarted(atNS uint64)
- func (duplex *Duplex) UserSpeechStopped(atNS uint64)
- type DuplexConfig
- type Media
- type MediaConfig
- type MediaMetrics
- type MediaStore
- type Observer
- type Phase
- type Snapshot
- type Transition
- type TransitionKind
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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) UserSpeechStarted ¶
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 ¶
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 ¶
Overlapping reports whether both parties are producing audio.
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" )