OpenAIAdapter
Defined in: packages/realtime-openai/src/openai-adapter.ts:191
A RealtimeProviderAdapter that connects the browser directly to
OpenAI gpt-realtime over WebRTC: mints an ephemeral client secret via the
injected mintToken, exchanges SDP with POST /v1/realtime/calls, and
drives tool calls / grounding pushes over the oai-events data channel.
The data-channel envelope → adapter-event mapping lives in parseOpenAIServerEvent; this class owns the provider-specific WebRTC, response lifecycle, and transcript-confirmed interruption bookkeeping around that mapping. The browser’s WebRTC implementation itself is not unit-tested.
Example
Section titled “Example”const adapter = new OpenAIAdapter({ mintToken: () => fetch('/api/realtime/mint', { method: 'POST', ... }).then(r => r.json()),});const session = new RealtimeSession({ adapter, instance, sessionId });await session.connect();Implements
Section titled “Implements”RealtimeProviderAdapterRealtimeGreetingAdapter
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new OpenAIAdapter(
options):OpenAIAdapter
Defined in: packages/realtime-openai/src/openai-adapter.ts:240
Parameters
Section titled “Parameters”options
Section titled “options”Returns
Section titled “Returns”OpenAIAdapter
Properties
Section titled “Properties”observations
Section titled “observations”
readonlyobservations:TranscriptObservationFeed
Defined in: packages/realtime-openai/src/openai-adapter.ts:217
Transcript fragments on a local receipt-time timeline, one per provider conversation item. The item’s final event revises its fragment with the full text. The fixed greeting appears as one final assistant fragment when its playback starts. Media evidence is not reported.
Implementation of
Section titled “Implementation of”RealtimeProviderAdapter.observations
Methods
Section titled “Methods”activateAudio()
Section titled “activateAudio()”activateAudio():
void
Defined in: packages/realtime-openai/src/openai-adapter.ts:565
Unlocks retained Web Audio activation in the initiating gesture.
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”RealtimeGreetingAdapter.activateAudio
connect()
Section titled “connect()”connect(
opts):Promise<void>
Defined in: packages/realtime-openai/src/openai-adapter.ts:317
Mints a token, captures the microphone, and opens the browser-to-OpenAI
WebRTC session. Capture requests AEC, noise suppression, and automatic gain
control as best-effort browser preferences; it does not promise that a
browser, device, or audio route applies them. After acquisition is confirmed
as current, enabled debug logging projects only applied booleans (or
unavailable) from the original track before attachment. Superseded
acquisitions are stopped without attachment or diagnostics, and missing or
throwing settings APIs do not fail connection.
Duplicate connections are rejected. Token, microphone, and SDP failures reject after this attempt’s resources are released. Unlike LiveKit, OpenAI owns turn detection and has no local VAD clone or publication gate.
Parameters
Section titled “Parameters”AdapterConnectOptions
Returns
Section titled “Returns”Promise<void>
Implementation of
Section titled “Implementation of”RealtimeProviderAdapter.connect
disconnect()
Section titled “disconnect()”disconnect():
Promise<void>
Defined in: packages/realtime-openai/src/openai-adapter.ts:485
Returns
Section titled “Returns”Promise<void>
Implementation of
Section titled “Implementation of”RealtimeProviderAdapter.disconnect
getAudioPlaybackStatus()
Section titled “getAudioPlaybackStatus()”getAudioPlaybackStatus():
OpenAIAudioPlaybackStatus
Defined in: packages/realtime-openai/src/openai-adapter.ts:573
Last observed remote media-element attempt for the current stream. Browser acceptance does not establish audible or continuous playback.
Returns
Section titled “Returns”getAudioStreams()
Section titled “getAudioStreams()”getAudioStreams():
AdapterAudioStreams
Defined in: packages/realtime-openai/src/openai-adapter.ts:489
Streams for UI metering. Never throws; absent streams are undefined.
Returns
Section titled “Returns”AdapterAudioStreams
Implementation of
Section titled “Implementation of”RealtimeProviderAdapter.getAudioStreams
injectGrounding()
Section titled “injectGrounding()”injectGrounding(
text):void
Defined in: packages/realtime-openai/src/openai-adapter.ts:521
Pushes a terse grounding string into the model’s context. The adapter
owns whatever latest-wins mechanics apply at the wire/session level
(e.g. overwriting session instructions rather than queuing utterances);
RealtimeSession additionally debounces calls into this method so the
adapter is never asked to collapse a rapid burst on its own.
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”RealtimeProviderAdapter.injectGrounding
on(
handler):Unsubscribe
Defined in: packages/realtime-openai/src/openai-adapter.ts:557
Parameters
Section titled “Parameters”handler
Section titled “handler”(event) => void
Returns
Section titled “Returns”Unsubscribe
Implementation of
Section titled “Implementation of”RealtimeProviderAdapter.on
resumeAudioPlayback()
Section titled “resumeAudioPlayback()”resumeAudioPlayback():
Promise<void>
Defined in: packages/realtime-openai/src/openai-adapter.ts:593
Retries the existing remote audio element within the caller’s user gesture.
Does not mint a grant, reconnect, acquire a microphone, or replay speech.
A current failure rejects with its original reason as cause; retired
attempts settle without changing current state. No attached stream is a no-op.
Acceptance is not proof of audibility.
Returns
Section titled “Returns”Promise<void>
sendToolResult()
Section titled “sendToolResult()”sendToolResult(
callId,output):Promise<void>
Defined in: packages/realtime-openai/src/openai-adapter.ts:493
Sends a tool call’s result back to the model.
Parameters
Section titled “Parameters”callId
Section titled “callId”ToolCallId
output
Section titled “output”ToolResult
Returns
Section titled “Returns”Promise<void>
Implementation of
Section titled “Implementation of”RealtimeProviderAdapter.sendToolResult
speakAssistantText()
Section titled “speakAssistantText()”speakAssistantText(
args):Promise<GreetingSpeechResult>
Defined in: packages/realtime-openai/src/openai-adapter.ts:601
Delegates provider handoff and local presentation to its package-private owner.
Parameters
Section titled “Parameters”MessageId
onPlaybackStart?
Section titled “onPlaybackStart?”() => void
signal
Section titled “signal”AbortSignal
string
Returns
Section titled “Returns”Promise<GreetingSpeechResult>
Implementation of
Section titled “Implementation of”RealtimeGreetingAdapter.speakAssistantText
subscribeAudioPlayback()
Section titled “subscribeAudioPlayback()”subscribeAudioPlayback(
handler):Unsubscribe
Defined in: packages/realtime-openai/src/openai-adapter.ts:582
Observes subsequent remote playback-attempt state changes. Read the current value with OpenAIAdapter.getAudioPlaybackStatus; subscription does not emit immediately. Observer failures do not affect playback or connection cleanup.
Parameters
Section titled “Parameters”handler
Section titled “handler”() => void
Returns
Section titled “Returns”Unsubscribe