import EventEmitter from "eventemitter3"; import type { TranscriptionAdapter } from "../sessionPlan/TranscriptionService"; import type { SofyaGatewayTimings } from "../sofyaGatewayConfig"; import type { TelemetryRow, TelemetrySnapshot } from "../metrics/types"; /** * Sofya STT gateway adapter (`provider: "sofya_gateway"`): binary PCM16 LE * 16 kHz frames on a plain WebSocket, on the shared PCM capture. One * single-use key per WebSocket open, carried only as the `x-api-key.` * subprotocol. A drop reconnects with a fresh key while the audio waits in a * bounded in-memory queue; the close code decides between reconnecting and * ending the session (gateway close-code table, PBI 4243). * * Every captured chunk is also written under an independent local session id, * with the Whisper retention (expiry 30 min after the stop). The * stop resolves with that audio as WAV; it is never replayed to the gateway. */ export type SofyaGatewayTransportFactory = (url: string, protocols: string[]) => WebSocket; export interface SofyaGatewayAdapterConfig { /** Gateway endpoint with its query (`transcription_language`); no credential. */ endpoint: string; language?: string; /** Stable id of the capture; generated once when absent. */ externalId?: string; /** Forwarded as the `record` query parameter (already in `endpoint`); logged in debug. */ record?: boolean; auth: { type: "single_use_token"; getToken: () => Promise; }; timings: SofyaGatewayTimings; debug?: boolean | { enabled?: boolean; }; transportFactory?: SofyaGatewayTransportFactory; } export declare class SofyaGatewayTranscriptionAdapter extends EventEmitter implements TranscriptionAdapter { private readonly config; readonly externalId: string; private readonly url; private readonly capture; private ws; private wsOpen; private wsOpenedAt; /** Reconnection attempts since the last stable socket. */ private attempt; private invalidKeyStreak; /** Bumped to cancel pending attempts. */ private generation; private reconnectTimer; /** Runs the scheduled attempt at once; registered only while one is scheduled. */ private onlineListener; private openTimer; private ended; private stopping; private finishSent; private stopPromise; private openWaiter; private closeWaiter; private readonly sessionIds; private readonly durable; private durableStarted; private readonly metrics; private durableResult; /** Open time of the first connection; cleared once the first partial is measured. */ private firstOpenAt; private firstPartialMeasured; private reconnectEpisode; constructor(config: SofyaGatewayAdapterConfig); /** Resolves once the first connection is opening; rejects when getToken fails. */ startTranscription(mediaStream: MediaStream): Promise; /** Builds the capture once the first key is in hand; false when a stop raced it. */ private prepareCapture; /** * One WebSocket open with its own fresh key (never reused). The socket is * opened only when `stillWanted()` holds once the key arrives; a getToken * failure rejects. */ private connectWithFreshKey; /** Stops feeding audio; the connection stays as it is. */ pauseTranscription(): void; resumeTranscription(): void; stopTranscription(): Promise; getSttSessionId(): string | null; getSttSessionIds(): string[]; /** Requests one key with a timeout; never reuses one. */ private fetchKey; /** Debug output from an explicit allowlist; never the auth or the query. */ private logConfig; private openSocket; private handleOpen; private handleClose; private scheduleReconnect; private addOnlineListener; private removeOnlineListener; /** * One reconnection attempt. A getToken failure (rejection, timeout or * invalid value) counts as one failed attempt, like a failed socket; only * the first open ends on TOKEN_PROVIDER_FAILED. */ private runAttempt; /** What a reconnection attempt has before its socket: a fresh key or a failure reason. */ private prepareAttempt; private handleMessage; private canSend; private sendAudio; /** Counts a captured chunk and writes it to the durable store (live or queued alike). */ private produceChunk; /** Latency from the first connection open to the first partial, once per session. */ private measureFirstPartial; /** * Finalizes the durable session (expiry 30 min after now) and returns its * audio as WAV through the recovery API, which purges expired audio before * any read. Includes in-memory quota fallback; null when no audio is available. */ private finalizeDurable; private runFinalizeDurable; getTelemetrySnapshot(): TelemetrySnapshot; getTelemetryRows(): TelemetryRow[]; clearTelemetryRows(): void; resetTelemetry(): void; private clearOpenTimer; /** Forgets the current socket (returned, not closed). */ private detachSocket; private cancelReconnect; /** * Ends the session on a definitive failure: `error` then `disconnected`, * once. Outside a stop, the pipeline is released and `stopped` follows. */ private endSession; private releaseWaiters; private waitFor; private sendFinishAndWait; /** * Normal stop: remaining audio, finish, final + 1000 (bounded). During a * reconnection with queued audio, waits (bounded) for the next socket to * drain it; otherwise ends at once. */ private runStop; } //# sourceMappingURL=SofyaGatewayTranscriptionAdapter.d.ts.map