import { type AbortSignalLike, type EventTargetLike, type TimerPlatform, type Utf8Codec } from './transport-utils.js'; import type { RealtimeClientEvent, RealtimeSessionConfig, RealtimeTransport, RealtimeTransportEvents, RealtimeTransportState } from './types.js'; /** * The subset of a `WebSocket` the transport uses. Structural, so the `ws` package, a browser * socket, or a test double all fit. */ export interface RealtimeWebSocketLike extends EventTargetLike { /** Connection state, as the WebSocket standard numbers it. */ readonly readyState: number; /** How binary messages are delivered. */ binaryType?: string; /** Sends one message. */ send(data: string | ArrayBuffer | Uint8Array): void; /** Closes the socket. */ close(code?: number, reason?: string): void; } /** What a WebSocket factory receives besides the URL. */ export interface OpenAIWebSocketFactoryOptions { /** Headers to send on the upgrade request, including authorization. */ headers: Record; /** Subprotocols to request. */ protocols?: string[]; } /** * Opens a WebSocket. Supply one for a runtime whose `WebSocket` cannot set headers, such as the * `ws` package in Node. */ export type OpenAIWebSocketFactory = (url: string, options: OpenAIWebSocketFactoryOptions) => RealtimeWebSocketLike; /** Fetches a short-lived token for a session, instead of using a long-lived API key. */ export type OpenAIEphemeralTokenProvider = (config: RealtimeSessionConfig, signal?: AbortSignalLike) => string | Promise; /** Options for the server-side WebSocket transport. */ export interface OpenAIWebSocketTransportOptions { /** Realtime endpoint. Defaults to OpenAI's production URL. */ url?: string; /** * Attaches to a call OpenAI is already carrying, such as an accepted SIP call, by its id, instead * of starting a session on a model. */ callId?: string; /** API key. Keep it on a server; a browser should use WebRTC with an ephemeral token. */ apiKey?: string; /** A token already issued. */ ephemeralToken?: string; /** Fetches a token when connecting. */ ephemeralTokenProvider?: OpenAIEphemeralTokenProvider; /** Headers added to the upgrade request. */ headers?: Record; /** Subprotocols to request. */ protocols?: string[]; /** Opens the socket. */ webSocketFactory?: OpenAIWebSocketFactory; /** * Fails the connection if it is not open within this many milliseconds. Defaults to 10 seconds. */ connectTimeoutMs?: number; /** * How long `disconnect()` waits for the socket to close cleanly, in milliseconds. Defaults to 1 * second. */ disconnectTimeoutMs?: number; /** Largest audio chunk `sendAudio()` accepts, in bytes. Defaults to 15 MiB. */ maxAudioChunkBytes?: number; /** Decodes text frames, for runtimes without `TextDecoder`. */ codec?: Utf8Codec; /** Replaces timers, for tests. */ timers?: TimerPlatform; /** Replaces the system clock, for tests. */ now?: () => number; } /** OpenAI Realtime WebSocket transport. Inject a factory such as `ws` for server-side authenticated use. */ export declare class OpenAIWebSocketTransport implements RealtimeTransport { private readonly options; /** Always `websocket`. */ readonly kind: "websocket"; /** Current connection state. */ state: RealtimeTransportState; private readonly emitter; private readonly cleanups; private socket?; private generation; private expectedClose; private disconnectEmitted; private connectAbort?; constructor(options?: OpenAIWebSocketTransportOptions); /** Subscribes to a transport event. Returns a function that unsubscribes. */ on(event: Event, listener: (payload: RealtimeTransportEvents[Event]) => void): () => void; /** Opens the socket, configures the session, and resolves once it is ready. */ connect(config: RealtimeSessionConfig): Promise; /** * Sends a chunk of audio as an `input_audio_buffer.append` event. Throws for a chunk over * `maxAudioChunkBytes`. */ sendAudio(chunk: ArrayBuffer): void; /** Sends a raw realtime event. */ sendEvent(event: RealtimeClientEvent): void; /** Cancels the model's current response. */ interrupt(): void; /** Closes the socket. Resolves once closed or after `disconnectTimeoutMs`. */ disconnect(): Promise; private awaitOpen; private handleMessage; private requireOpenSocket; /** The token to connect with, and whether it is a key: a minted token, the tenant's key, or the configured one. */ private resolveToken; private createSocket; private browserProtocols; private url; private closeDetails; private safeRawError; private finishDisconnect; private closeSocket; private cleanupListeners; private invalidState; private abortError; private timers; private now; }