Documentation
¶
Overview ¶
Package gptlive presents OpenAI's GPT-Live API as a Realtime endpoint.
GPT-Live shares a vendor with the Realtime API and almost nothing else. It is full duplex - it listens while it speaks - so it has no turn loop to drive: there is no input-buffer commit, no response.create that starts a spoken turn, and, the difference that shapes this whole package, no event marking the end of anything. Transcripts arrive as fragments with timestamps and stop arriving; the protocol says in as many words that they "do not define complete turns or include a transcript-done event". So this is a translator rather than a profile, and most of it is the boundary synthesis in turns.go.
Its second difference is an opportunity rather than a cost. GPT-Live does not reason or call tools itself: it delegates that work and carries on talking while it waits. With delegation.type "client" the endpoint asks *this* process for help, which is precisely the seam the upstream binding exists to fill. So the binding's background reasoner is not bolted onto the side of a model that would rather answer by itself - it is the backend the vendor's own design expects, and the hand-off goes back through session.commentary.append, the channel built for exactly that.
Four of the endpoint's constraints are load-bearing here:
- The model, instruction, voice, and audio format are settable only in the opening session.start. So the handshake is deferred until the caller's first session.update arrives, and anything sent before that is held rather than dropped. Later session.updates cannot change those fields and are accepted and dropped - an append is capped at 500 tokens and would reject a full instruction anyway.
- Nothing is sent until session.started arrives, which the vendor requires and which this package enforces rather than trusting call order.
- There are no turn boundaries, so they are synthesised. See turns.go.
- A completed answer is spoken by appending commentary, not by creating a conversation item. The catalogue still declares the portable hand-off; this translator is what makes it mean something here, which is the same arrangement Gemini Live has.
Index ¶
- Constants
- func Recording(ctx context.Context, config Config, sessionID string) ([]byte, error)
- type Client
- func (client *Client) Close() error
- func (client *Client) Err() error
- func (client *Client) Events() <-chan realtimeclient.Event
- func (client *Client) ResponsesDelegation() bool
- func (client *Client) Send(ctx context.Context, value any) error
- func (client *Client) SessionID() string
- func (client *Client) Wait()
- type Config
Constants ¶
const ( // DefaultURL is the Live session WebSocket. It takes no query parameters: // the model travels inside session.start, not in the URL, which is the // opposite of every Realtime endpoint in the catalogue. DefaultURL = "wss://api.openai.com/v1/live/sessions" // DefaultModel is the Live model. Like every default model in this project // it is a hint; gpt-live-1 serves only the v1/live/sessions endpoint. DefaultModel = "gpt-live-1" // DefaultVoice is the endpoint's own default, named here so that what this // package sends is visible rather than implied. DefaultVoice = "marin" // FormatPCM, FormatMuLaw and FormatALaw are the session audio formats a // Live WebSocket accepts. PCM is 16-bit at 24 or 16 kHz; the two G.711 laws // are one byte per sample at 8 kHz, which is what a telephone call is. FormatPCM = "audio/pcm" FormatMuLaw = "audio/pcmu" FormatALaw = "audio/pcma" // TelephonySampleRateHz is the only rate G.711 comes in. TelephonySampleRateHz = uint32(8_000) // WireSampleRateHz is what the Realtime wire carries, and what this // package asks the Live session for. Live accepts 16 kHz as well, so the // rate is configurable - but choosing the wire's rate means the audio is // passed through in both directions rather than resampled twice for // nothing. WireSampleRateHz = uint32(24_000) // AlternateSampleRateHz is the other PCM rate a Live session accepts. AlternateSampleRateHz = uint32(16_000) )
const ( // EventContext carries evidence the voice should know without saying. EventContext = "openrealtime.upstream.context" // EventSteer carries an application instruction that changes behaviour // now, including interrupting speech in progress. EventSteer = "openrealtime.upstream.steer" // EventMute holds or resumes the endpoint's hearing. EventMute = "openrealtime.upstream.mute" )
Internal event types the binding may send. No Realtime endpoint ever sees them: the binding translates them itself before an ordinary client, and only a translator like this one receives them by name.
Variables ¶
This section is empty.
Functions ¶
func Recording ¶
Recording downloads a stored session's audio once the vendor has finalised it: stereo WAV, the caller in the left channel and the voice in the right.
It needs a session opened with Store and closed, and a data policy that permits persistence; under Zero Data Retention there is nothing to download and the endpoint says so. The address is derived from the WebSocket one, because the vendor serves both from the same path.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a Live connection wearing the Realtime protocol.
func Attach ¶
Attach opens a sideband onto a running session.
The session is already running, so there is no handshake: nothing is held, nothing is started, and the first events arrive at once. Audio sent through this client is refused rather than forwarded, because the endpoint forbids it and the caller is better told than silently ignored.
func (*Client) Close ¶
Close ends the session and then the connection.
The vendor's own guidance is that session.closed is the only event that establishes finalisation and carries final usage, and that dropping the socket first prevents its delivery. So the close command is sent, the event is waited for within a bound, and the socket is released either way - a vendor that has stopped answering must not hold a session shutdown open.
func (*Client) Events ¶
func (client *Client) Events() <-chan realtimeclient.Event
Events yields translated Realtime server events.
func (*Client) ResponsesDelegation ¶
ResponsesDelegation reports whether this session hands its backend work to the vendor's managed Responses loop rather than to this process.
func (*Client) Send ¶
Send translates one Realtime client event into Live protocol messages.
An event with no Live equivalent is accepted and dropped rather than refused, for the reason the Gemini translator gives: the caller is speaking a protocol this endpoint does not implement, and failing its ordinary traffic would take down a session over a message that changes nothing. Here that covers the commit and truncate halves of a turn loop a full-duplex endpoint does not run.
type Config ¶
type Config struct {
// URL is the Live session endpoint. Empty selects DefaultURL.
URL string
// APIKey is sent as Authorization: Bearer.
APIKey string
// Model is the Live model. Empty selects DefaultModel.
Model string
// Voice is the Live voice. Empty selects the endpoint's own default.
Voice string
// Header carries additional request headers.
Header http.Header
// SessionFormat is the audio format the session is opened with. Empty
// selects PCM. The G.711 laws fix the rate at 8 kHz and are for a session
// whose other end is a telephone: the caller's 24 kHz is resampled and
// companded on the way in, and expanded and resampled on the way out.
SessionFormat string
// SessionSampleRateHz is the PCM rate the Live session is opened with.
// Empty selects WireSampleRateHz, which needs no resampling. Ignored for
// G.711, which has one rate.
SessionSampleRateHz uint32
// ForkOf names a stored session to continue from instead of starting a
// new one. The connection goes to that session's fork endpoint, the
// start carries only the audio format, and everything else - model,
// instruction, voice, delegation, history - is inherited from the vendor's
// recording. The fork gets a session id of its own.
ForkOf string
// Reconnect forks the session when the connection drops, which needs
// Store and a session that has started. Nil selects on when Store is set.
// A reconnect is the vendor's own recovery guidance: fork the stored
// session rather than start cold, then reconcile what was pending.
Reconnect *bool
// MaxReconnects bounds how many forks one Client will attempt. Zero
// selects three.
MaxReconnects int
// MaxRestarts bounds how many times a session may be abandoned and
// reopened to honour a configuration the endpoint fixes at start. Zero
// selects two; negative refuses to restart at all.
MaxRestarts int
// CallerSampleRateHz is the rate the caller sends and expects audio at.
// Empty selects the Realtime wire's 24 kHz.
CallerSampleRateHz uint32
// FrameInterval is how often input audio must reach the endpoint for its
// clock to advance. Zero selects the default; a negative value turns the
// gap filler off, which only a caller that guarantees its own continuous
// stream should do. See frames.go for why this exists at all.
FrameInterval time.Duration
// InputTurnGap is the silence after a user transcript fragment that ends
// the user's turn. Zero selects the default; see turns.go for why this
// number exists at all and what it is trading off.
InputTurnGap time.Duration
// OutputTurnGap is the equivalent for the assistant's own speech.
OutputTurnGap time.Duration
// SilenceHangover is how much of the endpoint's silent carrier is passed
// on after real speech before the stream is allowed to go quiet. Zero
// selects the default; negative forwards none at all.
//
// It exists because the carrier is not speech and must not be reported as
// it. Live emits a frame every 100 ms whether or not anyone is talking, so
// forwarding it for as long as an utterance is open tells everything
// downstream that the agent is still speaking when it has stopped - which
// erases the silence a barge-in policy, a duplex state, and any
// full-duplex measurement all read. Measured against the real endpoint on
// one Full-Duplex-Bench interruption, forwarding the whole carrier put the
// agent's apparent stop 16.4 s after the user took the floor; the audio
// had in fact stopped, and this side was inventing the rest. A short
// hangover keeps genuine pauses between words intact without inventing
// anything after the speech ends.
SilenceHangover time.Duration
// DialTimeout bounds connection establishment.
DialTimeout time.Duration
// WriteTimeout bounds one send on an established connection. Zero selects
// the shipped default; a negative value removes the bound.
WriteTimeout time.Duration
// CloseTimeout bounds the wait for session.closed during Close. Zero
// selects the default; a negative value does not wait.
CloseTimeout time.Duration
// ResponsesModel selects the vendor's own managed backend instead of this
// process. Empty keeps client delegation, where the upstream binding's
// reasoner is the backend.
//
// The two modes are exclusive and fixed at session start. Client
// delegation is what this binding is for; Responses delegation exists
// because it is half of the endpoint's surface, and a deployment may
// legitimately want the vendor's managed loop - with OpenRealtime still
// mirroring the conversation, running observers, and holding the floor.
ResponsesModel string
// ResponsesInstructions is the backend prompt for Responses delegation.
ResponsesInstructions string
// ResponsesTools are the tools the managed backend may call.
ResponsesTools []map[string]any
// Store asks the endpoint to keep a resumable recording, which is what
// makes a session forkable after a dropped connection and downloadable
// afterwards. Off by default, as it is at the vendor; under Zero Data
// Retention the endpoint treats it as false whatever is sent.
Store bool
// StallTimeout is how long a started session may send nothing before a
// stall is reported. Zero selects the default; negative disables it.
StallTimeout time.Duration
// ReadLimit bounds one inbound message.
ReadLimit int64
// HTTPClient dials the WebSocket.
HTTPClient *http.Client
// Scheduler drives the turn-boundary timers. Empty selects the wall
// clock; a test supplies a manual one so that boundaries - which are the
// whole difficulty of this adapter - are checked deterministically rather
// than by sleeping.
Scheduler clock.Scheduler
}
Config configures one translated connection.