openai
Version:
The official TypeScript library for the OpenAI API
99 lines • 4.25 kB
text/typescript
import { SidebandEmitter, SidebandStreamMessage } from "./internal-base.mjs";
import type { WebSocketLike } from "../../../internal/ws-adapter.mjs";
import { type WebSocketStreamOptions, type RawWebSocketData, type ReconnectingEvent, type ReconnectingOverrides } from "../../../internal/ws.mjs";
import * as SidebandAPI from "./sideband.mjs";
import { OpenAI } from "../../../client.mjs";
export interface SidebandWSParameters extends Record<string, unknown> {
/**
* Opt in to the graceful WebSocket closing handshake when the session ends. The
* server may also enable this behavior by default.
*/
graceful_close?: boolean;
session_id: string;
}
export interface SidebandWSReconnectOptions {
/**
* Called before each reconnect attempt. Return an object with
* `parameters` to override query parameters for the next connection.
*/
onReconnecting(event: ReconnectingEvent<SidebandWSParameters>): ReconnectingOverrides<SidebandWSParameters> | void;
/**
* Maximum number of reconnection attempts. Default: 5.
* Set to 0 to disable reconnection entirely.
*/
maxRetries?: number;
/**
* Initial backoff delay in milliseconds. Default: 500.
*/
initialDelay?: number;
/**
* Maximum backoff delay in milliseconds. Default: 8000.
*/
maxDelay?: number;
}
export interface SidebandWSBaseOptions {
/**
* Options for automatic reconnection on recoverable close codes.
* Automatic reconnection is only enabled when this has a non-null value.
*/
reconnect?: SidebandWSReconnectOptions | null | undefined;
/**
* Byte budget for outgoing messages queued while the socket is connecting
* or reconnecting. An empty queue accepts one message even if it exceeds
* this budget. Further messages are discarded and an `error` event is emitted
* if the total queued size would exceed the budget.
* Default: 1 MiB (1,048,576 bytes).
*/
maxQueueSize?: number | undefined;
}
export declare abstract class SidebandWSBase<TSocket extends WebSocketLike> extends SidebandEmitter {
#private;
url: URL;
socket: TSocket;
protected _client: OpenAI;
protected _parameters: SidebandWSParameters | null | undefined;
constructor(client: OpenAI, parameters: SidebandWSParameters, options?: SidebandWSBaseOptions | undefined);
/** Establishes the initial WebSocket connection. */
protected _connectInitial(): void;
/** Creates a platform-specific WebSocket for the given URL and auth headers. */
protected abstract _createSocket(url: URL, authHeaders: Record<string, string>): TSocket;
send(event: SidebandAPI.ConnectClientEvent): void;
sendRaw(data: RawWebSocketData): void;
close(props?: {
code: number;
reason: string;
}): void;
/**
* Returns an async iterator over WebSocket lifecycle and message events,
* providing an alternative to the event-based `.on()` API.
* The iterator will exit if the socket closes but exiting the iterator
* does not close the socket.
*
* Pass `maxBufferedEvents` to limit queued records for this iterator, including
* lifecycle events. Overflow discards its backlog and rejects `next()` with a
* WebSocketError; the shared socket and other iterators remain active.
* Omitted means unlimited. This is an event-count limit, not a byte limit.
*
* @example
* ```ts
* for await (const event of client.stream()) {
* switch (event.type) {
* case 'message':
* console.log('received:', event.message);
* break;
* case 'error':
* console.error(event.error);
* break;
* case 'close':
* console.log('connection closed');
* break;
* }
* }
* ```
*/
stream(options?: WebSocketStreamOptions): AsyncIterableIterator<SidebandStreamMessage>;
[Symbol.asyncIterator](options?: WebSocketStreamOptions): AsyncIterableIterator<SidebandStreamMessage>;
protected _prepareReconnectSocket(): Promise<(url: URL) => TSocket>;
protected _authHeaders(apiKey?: string | null): Record<string, string>;
}
//# sourceMappingURL=ws-base.d.mts.map