UNPKG

phonic

Version:

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FPhonic-Co%2Fphonic-node) [![npm shield](htt

77 lines (76 loc) 3.51 kB
import * as core from "../core/index.js"; import type * as Phonic from "../api/index.js"; import { ConversationsSocket } from "../api/resources/conversations/client/Socket.js"; export interface ReconnectableConversationsSocketArgs { /** Called on 1006 to create a new socket with reconnect_conv_id. May be async (e.g. fresh auth). */ createReconnectSocket: (conversationId: string) => core.ReconnectingWebSocket | Promise<core.ReconnectingWebSocket>; /** Initial socket for the first connection. */ socket: core.ReconnectingWebSocket; /** If provided, reconnection stops when the signal is aborted. */ abortSignal?: AbortSignal; } type EventHandlers = { open?: () => void; message?: (message: ConversationsSocket.Response) => void; close?: (event: core.CloseEvent) => void; error?: (error: Error) => void; }; /** * Wraps ConversationsSocket with automatic reconnection on 1006. * * On abnormal closure, creates a brand new ReconnectingWebSocket (via the * createReconnectSocket factory) and wraps it in a fresh ConversationsSocket, * forwarding all events to user-registered handlers. * * Retries reconnection with exponential backoff until the server responds * with a terminal close code (4800 session expired, 4801 invalid state), * the safety cap is reached, or the user calls close(). * * Uses composition rather than inheritance to avoid coupling to the parent's * private event handler registration or ReconnectingWebSocket internals. */ export declare class ReconnectableConversationsSocket { private _conversationId; private _inner; private readonly _createReconnectSocket; private readonly _handlers; private _reconnectAttempts; private _isClosed; private readonly _abortSignal; private _cleanupWireListeners; private _pendingReconnect; private _pendingReplacement; constructor(args: ReconnectableConversationsSocketArgs); /** The conversation ID captured from the server's conversation_created message. */ get conversationId(): string | null; get socket(): core.ReconnectingWebSocket; get readyState(): number; on<T extends keyof EventHandlers>(event: T, callback: EventHandlers[T]): void; /** Drop outbound sends when we cannot talk to a live socket (no queue). */ private _safeSend; sendConfig(message: Phonic.ConfigPayload): void; sendAudioChunk(message: Phonic.AudioChunkPayload): void; sendToolCallOutput(message: Phonic.ToolCallOutputPayload): void; sendUpdateSystemPrompt(message: Phonic.UpdateSystemPromptPayload): void; sendAddSystemMessage(message: Phonic.AddSystemMessagePayload): void; sendSetExternalId(message: Phonic.SetExternalIdPayload): void; sendGenerateReply(message: Phonic.GenerateReplyPayload): void; sendSay(message: Phonic.SayPayload): void; sendReset(message: Phonic.ResetPayload): void; sendMute(message: Phonic.MutePayload): void; sendUnmute(message: Phonic.UnmutePayload): void; /** * Not supported — reconnection after 1006 is handled automatically. * To start a new conversation, create a new socket via client.conversations.connect(). */ connect(): never; close(): void; waitForOpen(): Promise<core.ReconnectingWebSocket>; private _getReconnectDelay; /** Schedule a reconnection attempt after backoff delay. */ private _scheduleReconnect; /** Perform the actual reconnection attempt. */ private _doReconnect; private _wireInner; } export {};