upstream

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: 25 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 upstream puts a remote Realtime endpoint behind OpenRealtime's background reasoner.

The remote model owns perception, the fast voice, and action; it is a complete voice stack already. What it does not have, and cannot have, is a second model reasoning over the same conversation while it talks - a single-model server has no second model and no shared log to put one on.

So this binding mirrors the remote conversation into the canonical trajectory, runs the engine's slow provider over it, executes the tools that provider calls, and hands the result back for the remote to say. The hand-off is explicit rather than clever, which is what makes it work against any Realtime-compatible endpoint rather than against one vendor's internals.

Index

Constants

View Source
const DialectGPTLive = "gpt-live"

DialectGPTLive names OpenAI's Live protocol, the one endpoint in the catalogue that delegates. The string matches the provider catalogue's; it is repeated here because the catalogue imports this package.

Variables

View Source
var ErrAtCapacity = errors.New("upstream endpoint is at its concurrent session bound")

ErrAtCapacity reports a session refused because the endpoint's concurrent session bound has been reached.

Functions

This section is empty.

Types

type Binding

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

func New

func New(config Config) (*Binding, error)

New validates the configuration.

func (*Binding) Capabilities

func (bind *Binding) Capabilities() binding.Capabilities

Capabilities reports what an upstream session supports. Video and computer use depend on the remote, which the base protocol gives no way to ask about, so they are reported off rather than guessed at.

func (*Binding) Name

func (bind *Binding) Name() string

Name is the binding's stable identifier.

func (*Binding) Ownership

func (bind *Binding) Ownership() binding.Ownership

Ownership declares that the remote owns the voice stack and the engine owns the background reasoner.

func (*Binding) Start

func (bind *Binding) Start(ctx context.Context, options binding.Options) (binding.Runtime, error)

Start opens a session against the remote endpoint.

type Config

type Config struct {
	// URL is the remote Realtime WebSocket endpoint.
	URL string
	// Token is the remote credential.
	Token string
	// Model is the remote model identifier.
	Model string
	// Header carries provider-specific headers.
	Header http.Header
	// EventAliases renames inbound server events onto the ones the mirror
	// reads, for an endpoint that implements an older spelling of the spec.
	EventAliases map[string]string
	// Handoff selects how the reasoner's answer is given to the remote to
	// say. Empty selects the conversation-item form.
	Handoff Handoff
	// Dial opens the connection. Empty selects a Realtime WebSocket client,
	// which is what every endpoint that implements the protocol needs. An
	// endpoint that does not - Gemini Live - supplies a translator here.
	Dial Dialer

	// Slow is the engine's background reasoner. It is the whole point of this
	// binding and is therefore required, unlike every other component here.
	Slow          continuation.Provider
	SlowMaxTokens int
	// AgentInstruction is composed ahead of the slow phase instruction.
	AgentInstruction string

	// Policies overrides the interaction policy set. The default is
	// endpointed slow-only with voicing, because the remote already owns the
	// fast voice: running the engine's fast provider too would produce two
	// voices answering the same question.
	Policies interaction.Policies

	// FloorOwner declares who decides endpoints. The remote's own VAD is the
	// default because it is already running; declaring the engine here is the
	// F5 comparison this binding makes available.
	FloorOwner binding.Owner

	MaxPendingEvents int
	MediaRetention   session.MediaConfig
	Scheduler        clock.Scheduler
	// HandoffTimeout bounds how long a slow answer waits to be voiced.
	HandoffTimeout time.Duration
	// ClientToolTimeout bounds how long a client has to return results for
	// the tools it executes. Zero selects the default; negative disables the
	// deadline, which only a harness driving results by hand should do.
	ClientToolTimeout time.Duration

	// Dialect names the wire contract the remote speaks, from the provider
	// catalogue. Most of this binding is portable; the parts that are not are
	// decided here rather than by probing what the connection happens to do.
	Dialect string
	// DelegationGating decides whether the reasoner runs on every user turn
	// or only when the remote asks for help. Empty selects the dialect's
	// default: gated on an endpoint that delegates, because there the voice
	// answering by itself is the design and a reasoner answering "hello" is a
	// cost; ungated everywhere else, because nothing there ever asks.
	DelegationGating Gating
	// IdleTimeout closes a session that has heard no user speech for this
	// long. It exists because a full-duplex endpoint is billed by the second
	// and kept alive by this binding's own silence frames: without a bound an
	// abandoned tab is a running meter. Zero selects the dialect's default -
	// fifteen minutes on GPT-Live, none elsewhere - and negative disables it.
	IdleTimeout time.Duration
	// ContextPushDebounce coalesces observer evidence bound for the remote,
	// so a burst of screen changes becomes one summary of the latest state
	// rather than a summary per change. Zero selects 400ms.
	ContextPushDebounce time.Duration

	// Observers are the video observers a session may run. The remote owns
	// hearing; seeing is this side's, because no remote in the catalogue can
	// be sent a frame, and what an observer narrates reaches the remote as
	// context. Empty leaves video unsupported, as it always was.
	Observers []perception.Factory
	// DefaultObservers is the set a session runs when the client names none.
	// Empty selects every configured observer.
	DefaultObservers []string
	// Greeting is an application instruction sent once the session has
	// started, for a voice that should speak first. On GPT-Live it goes down
	// the instruction channel with audio already running, which is the
	// vendor's own recipe; on a Realtime endpoint it is context the first
	// response reads.
	Greeting string
	// MaxSessions bounds concurrent sessions on this endpoint, so the
	// vendor's own tier limit is met here with a reason rather than there
	// with a refusal mid-conversation. Zero is unbounded.
	MaxSessions int
	// Extraction notices when the user sets a policy out loud and steers
	// the remote with it. Nil leaves standing instructions to the remote.
	Extraction interaction.Extractor
	// Store asks the remote to keep a resumable recording of the session,
	// on the endpoints that can. It is what makes a dropped connection
	// recoverable by forking rather than starting cold.
	Store bool
	// AudioFormat is the wire format the remote is opened with, on the
	// endpoints that offer more than one. Empty selects PCM.
	AudioFormat string
	// BargeIn lets this binding's own barge-in policy stop the remote when the
	// user talks over it, and hold the remote's audio back until it does. Nil
	// selects off, which is the right default and was not the first answer.
	//
	// A full-duplex model handles being interrupted itself; that is most of
	// what full duplex is for, and GPT-Live is good at it. Measured against
	// the real endpoint on one Full-Duplex-Bench interruption it yielded in
	// 2.8 s with nothing helping it - after this side stopped cutting silence
	// into the audio on the way in, which had been costing 16.4 s and was a
	// defect here rather than a limit there.
	//
	// So this is an override, not a fix. It buys a sub-second yield - 0.26 s
	// on the same recording - by acting where the model cannot: the
	// instruction channel stops the model itself, slowly, and holding the
	// audio at this relay stops what the person hears at once, which is where
	// the vendor says to block output when an application needs speech to
	// stop. A deployment that must guarantee the floor within a second turns
	// it on and accepts that its own policy, not the model's judgement, is
	// deciding; everything else should leave the model to it.
	BargeIn *bool
	// contains filtered or unexported fields
}

Config configures the binding.

type Dialer

type Dialer func(ctx context.Context, config Config) (RemoteConn, error)

Dialer opens a connection to a remote realtime endpoint.

It is a function rather than a dialect enum so that translating a foreign protocol into this one stays outside the binding. The binding's job is the background reasoner; which wire format the remote happens to speak is not something it should have opinions about.

type Gating added in v0.2.0

type Gating string

Gating is how the reasoner is triggered.

const (
	// GatingAuto selects the dialect's default.
	GatingAuto Gating = ""
	// GatingOn runs the reasoner only when the remote delegates.
	GatingOn Gating = "on"
	// GatingOff runs the reasoner on every user turn.
	GatingOff Gating = "off"
)

func ParseGating added in v0.2.0

func ParseGating(value string) (Gating, error)

ParseGating resolves a configured gating level.

type Handoff

type Handoff string

Handoff is how a completed answer reaches the remote's voice.

It exists because the base protocol turned out not to be as portable as it looked. Injecting a conversation item is the natural form and every endpoint modelled on OpenAI's own accepts it - but Alibaba's Qwen-Omni-Realtime accepts conversation.item.create only for tool results, and its response.create takes no per-response instructions. On that endpoint the session instruction is the only writable channel there is.

So the mechanism is a declared property of the endpoint rather than an assumption, in the same way a provider's reasoning switch is. An endpoint that supports neither cannot host this binding at all, and saying which one it supports is what makes that checkable.

const (
	// HandoffConversationItem appends a system message the remote reads as
	// context, then asks for a response. It is the portable form.
	HandoffConversationItem Handoff = "conversation-item"
	// HandoffSessionInstruction rewrites the session instruction to carry the
	// answer, asks for a response, and restores the instruction when the
	// response completes. It is for an endpoint whose only writable channel
	// is the session.
	HandoffSessionInstruction Handoff = "session-instruction"
)

type RemoteConn

type RemoteConn interface {
	Events() <-chan realtimeclient.Event
	Err() error
	Send(ctx context.Context, value any) error
	Close() error
}

RemoteConn is the connection to whatever is at the other end.

Jump to

Keyboard shortcuts

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