TwilioRealtimeTransportLayer
An adapter to connect a websocket that is receiving messages from Twilio’s Media Streams API to the OpenAI Realtime API via WebSocket.
It automatically handles setting the right audio format for the input and output audio, passing
the data along and handling the timing for interruptions using Twilio’s mark
events.
It does require you to run your own WebSocket server that is receiving connection requests from Twilio.
It will emit all Twilio received messages as twilio_message
type messages on the *
handler.
If you are using a RealtimeSession
you can listen to the transport_event
.
Example
Section titled “Example”const transport = new TwilioRealtimeTransportLayer({ twilioWebSocket: twilioWebSocket,});
transport.on('*', (event) => { if (event.type === 'twilio_message') { console.log('Twilio message:', event.data); }});
Extends
Section titled “Extends”OpenAIRealtimeWebSocket
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new TwilioRealtimeTransportLayer(options): TwilioRealtimeTransportLayer
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
Returns
Section titled “Returns”TwilioRealtimeTransportLayer
Overrides
Section titled “Overrides”OpenAIRealtimeWebSocket.constructor
Accessors
Section titled “Accessors”_tracingConfig
Section titled “_tracingConfig”Set Signature
Section titled “Set Signature”set _tracingConfig(tracingConfig): void
Sets the internal tracing config. This is used to track the tracing config that has been set during the session.create event.
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket._tracingConfig
connectionState
Section titled “connectionState”Get Signature
Section titled “Get Signature”get connectionState(): WebSocketState
The current connection state of the WebSocket connection.
Returns
Section titled “Returns”WebSocketState
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.connectionState
currentModel
Section titled “currentModel”Get Signature
Section titled “Get Signature”get currentModel(): OpenAIRealtimeModels
The current model that is being used by the transport layer.
Returns
Section titled “Returns”OpenAIRealtimeModels
Set Signature
Section titled “Set Signature”set currentModel(model): void
The current model that is being used by the transport layer. Note: The model cannot be changed mid conversation.
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.currentModel
Get Signature
Section titled “Get Signature”get muted(): null
Always returns null
as the WebSocket transport layer does not handle muting. Instead,
this should be handled by the client by not triggering the sendAudio
method.
Returns
Section titled “Returns”null
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.muted
status
Section titled “status”Get Signature
Section titled “Get Signature”get status(): "connected" | "disconnected" | "connecting"
The current status of the WebSocket connection.
Returns
Section titled “Returns”"connected"
| "disconnected"
| "connecting"
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.status
Methods
Section titled “Methods”_cancelResponse()
Section titled “_cancelResponse()”_cancelResponse(): void
Send a cancel response event to the Realtime API. This is used to cancel an ongoing response that the model is currently generating.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket._cancelResponse
_interrupt()
Section titled “_interrupt()”_interrupt(_elapsedTime): void
Do NOT call this method directly. Call interrupt()
instead for proper interruption handling.
This method is used to send the right events to the API to inform the model that the user has
interrupted the response. It might be overridden/extended by an extended transport layer. See
the TwilioRealtimeTransportLayer
for an example.
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”void
Overrides
Section titled “Overrides”OpenAIRealtimeWebSocket._interrupt
_setInputAndOutputAudioFormat()
Section titled “_setInputAndOutputAudioFormat()”_setInputAndOutputAudioFormat(partialConfig?): Partial<RealtimeSessionConfig>
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”Partial
<RealtimeSessionConfig
>
close()
Section titled “close()”close(): void
Close the WebSocket connection.
This will also reset any internal connection tracking used for interruption handling.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.close
connect()
Section titled “connect()”connect(options): Promise<void>
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”Promise
<void
>
Overrides
Section titled “Overrides”OpenAIRealtimeWebSocket.connect
emit()
Section titled “emit()”emit<K>(type, ...args): boolean
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
… |
|
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.emit
interrupt()
Section titled “interrupt()”interrupt(): void
Interrupt the ongoing response. This method is triggered automatically by the client when voice activity detection (VAD) is enabled (default) as well as when an output guardrail got triggered.
You can also call this method directly if you want to interrupt the conversation for example based on an event in the client.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.interrupt
mute()
Section titled “mute()”mute(_muted): never
Will throw an error as the WebSocket transport layer does not support muting.
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”never
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.mute
off<K>(type, listener): EventEmitter<EventTypes>
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
|
(… |
Returns
Section titled “Returns”EventEmitter
<EventTypes
>
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.off
on<K>(type, listener): EventEmitter<EventTypes>
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
|
(… |
Returns
Section titled “Returns”EventEmitter
<EventTypes
>
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.on
once()
Section titled “once()”once<K>(type, listener): EventEmitter<EventTypes>
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
|
(… |
Returns
Section titled “Returns”EventEmitter
<EventTypes
>
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.once
resetHistory()
Section titled “resetHistory()”resetHistory(oldHistory, newHistory): void
Reset the history of the conversation. This will create a diff between the old and new history and send the necessary events to the Realtime API to update the history.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
The old history of the conversation. |
|
|
The new history of the conversation. |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.resetHistory
sendAudio()
Section titled “sendAudio()”sendAudio(audio, options?): void
Send an audio buffer to the Realtime API. This is used for your client to send audio to the model to respond.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
The audio buffer to send. |
|
{ |
The options for the audio buffer. |
|
|
‐ |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.sendAudio
sendEvent()
Section titled “sendEvent()”sendEvent(event): void
Send an event to the Realtime API. This will stringify the event and send it directly to the API. This can be used if you want to take control over the connection and send events manually.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
The event to send. |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.sendEvent
sendFunctionCallOutput()
Section titled “sendFunctionCallOutput()”sendFunctionCallOutput( toolCall, output, startResponse?): void
Send the output of a function call to the Realtime API.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
The tool call to send the output for. |
|
|
The output of the function call. |
|
|
Whether to start a new response after sending the output. |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.sendFunctionCallOutput
sendMessage()
Section titled “sendMessage()”sendMessage(message, otherEventData): void
Send a message to the Realtime API. This will create a new item in the conversation and trigger a response.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
The message to send. |
|
|
Additional event data to send. |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.sendMessage
updateSessionConfig()
Section titled “updateSessionConfig()”updateSessionConfig(config): void
Updates the session config. This will merge it with the current session config with the default values and send it to the Realtime API.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
The session config to update. |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”OpenAIRealtimeWebSocket.updateSessionConfig