@livekit/agents 1.0.44 → 1.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ipc/supervised_proc.cjs +1 -1
- package/dist/ipc/supervised_proc.cjs.map +1 -1
- package/dist/ipc/supervised_proc.js +1 -1
- package/dist/ipc/supervised_proc.js.map +1 -1
- package/dist/llm/llm.cjs +1 -1
- package/dist/llm/llm.cjs.map +1 -1
- package/dist/llm/llm.js +1 -1
- package/dist/llm/llm.js.map +1 -1
- package/dist/log.cjs +13 -9
- package/dist/log.cjs.map +1 -1
- package/dist/log.d.cts +1 -1
- package/dist/log.d.ts +1 -1
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +13 -9
- package/dist/log.js.map +1 -1
- package/dist/stt/stt.cjs +2 -2
- package/dist/stt/stt.cjs.map +1 -1
- package/dist/stt/stt.js +2 -2
- package/dist/stt/stt.js.map +1 -1
- package/dist/tts/fallback_adapter.cjs +466 -0
- package/dist/tts/fallback_adapter.cjs.map +1 -0
- package/dist/tts/fallback_adapter.d.cts +110 -0
- package/dist/tts/fallback_adapter.d.ts +110 -0
- package/dist/tts/fallback_adapter.d.ts.map +1 -0
- package/dist/tts/fallback_adapter.js +442 -0
- package/dist/tts/fallback_adapter.js.map +1 -0
- package/dist/tts/index.cjs +3 -0
- package/dist/tts/index.cjs.map +1 -1
- package/dist/tts/index.d.cts +1 -0
- package/dist/tts/index.d.ts +1 -0
- package/dist/tts/index.d.ts.map +1 -1
- package/dist/tts/index.js +2 -0
- package/dist/tts/index.js.map +1 -1
- package/dist/tts/tts.cjs +2 -2
- package/dist/tts/tts.cjs.map +1 -1
- package/dist/tts/tts.js +2 -2
- package/dist/tts/tts.js.map +1 -1
- package/dist/vad.cjs +11 -10
- package/dist/vad.cjs.map +1 -1
- package/dist/vad.d.cts +5 -3
- package/dist/vad.d.ts +5 -3
- package/dist/vad.d.ts.map +1 -1
- package/dist/vad.js +11 -10
- package/dist/vad.js.map +1 -1
- package/dist/voice/room_io/_input.cjs +6 -3
- package/dist/voice/room_io/_input.cjs.map +1 -1
- package/dist/voice/room_io/_input.d.ts.map +1 -1
- package/dist/voice/room_io/_input.js +6 -3
- package/dist/voice/room_io/_input.js.map +1 -1
- package/package.json +1 -1
- package/src/ipc/supervised_proc.ts +1 -1
- package/src/llm/llm.ts +1 -1
- package/src/log.ts +22 -11
- package/src/stt/stt.ts +2 -2
- package/src/tts/fallback_adapter.ts +579 -0
- package/src/tts/index.ts +1 -0
- package/src/tts/tts.ts +2 -2
- package/src/vad.ts +12 -11
- package/src/voice/room_io/_input.ts +5 -3
package/dist/stt/stt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/stt/stt.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type AudioFrame, AudioResampler } from '@livekit/rtc-node';\nimport type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';\nimport { EventEmitter } from 'node:events';\nimport type { ReadableStream } from 'node:stream/web';\nimport { APIConnectionError, APIError } from '../_exceptions.js';\nimport { calculateAudioDurationSeconds } from '../audio.js';\nimport { log } from '../log.js';\nimport type { STTMetrics } from '../metrics/base.js';\nimport { DeferredReadableStream } from '../stream/deferred_stream.js';\nimport { type APIConnectOptions, DEFAULT_API_CONNECT_OPTIONS, intervalForRetry } from '../types.js';\nimport type { AudioBuffer } from '../utils.js';\nimport { AsyncIterableQueue, delay, startSoon, toError } from '../utils.js';\nimport type { TimedString } from '../voice/index.js';\n\n/** Indicates start/middle/end of speech */\nexport enum SpeechEventType {\n /**\n * Indicate the start of speech.\n * If the STT doesn't support this event, this will be emitted at the same time\n * as the first INTERIM_TRANSCRIPT.\n */\n START_OF_SPEECH = 0,\n /**\n * Interim transcript, useful for real-time transcription.\n */\n INTERIM_TRANSCRIPT = 1,\n /**\n * Final transcript, emitted when the STT is confident enough that a certain\n * portion of the speech will not change.\n */\n FINAL_TRANSCRIPT = 2,\n /**\n * Indicate the end of speech, emitted when the user stops speaking.\n * The first alternative is a combination of all the previous FINAL_TRANSCRIPT events.\n */\n END_OF_SPEECH = 3,\n /** Usage event, emitted periodically to indicate usage metrics. */\n RECOGNITION_USAGE = 4,\n /**\n * Preflight transcript, emitted before final transcript when STT has high confidence\n * but hasn't fully committed yet. Includes all pre-committed transcripts including\n * final transcript from the previous STT run.\n */\n PREFLIGHT_TRANSCRIPT = 5,\n}\n\n/** SpeechData contains metadata about this {@link SpeechEvent}. */\nexport interface SpeechData {\n /** Language code of the speech. */\n language: string;\n /** Transcribed text. */\n text: string;\n /** Start time of the speech segment in seconds. */\n startTime: number;\n /** End time of the speech segment in seconds. */\n endTime: number;\n /** Confidence score of the transcription (0-1). */\n confidence: number;\n /** Word-level timing information. */\n words?: TimedString[];\n}\n\nexport interface RecognitionUsage {\n /** Duration of the audio that was recognized in seconds. */\n audioDuration: number;\n}\n\n/** SpeechEvent is a packet of speech-to-text data. */\nexport interface SpeechEvent {\n type: SpeechEventType;\n alternatives?: [SpeechData, ...SpeechData[]];\n requestId?: string;\n recognitionUsage?: RecognitionUsage;\n}\n\n/**\n * Describes the capabilities of the STT provider.\n *\n * @remarks\n * At present, the framework only supports providers that have a streaming endpoint.\n */\nexport interface STTCapabilities {\n streaming: boolean;\n interimResults: boolean;\n /**\n * Whether this STT supports aligned transcripts with word/chunk timestamps.\n * - 'word': Provider returns word-level timestamps\n * - 'chunk': Provider returns chunk-level timestamps (e.g., sentence/phrase boundaries)\n * - false: Provider does not support aligned transcripts\n */\n alignedTranscript?: 'word' | 'chunk' | false;\n}\n\nexport interface STTError {\n type: 'stt_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport type STTCallbacks = {\n ['metrics_collected']: (metrics: STTMetrics) => void;\n ['error']: (error: STTError) => void;\n};\n\n/**\n * An instance of a speech-to-text adapter.\n *\n * @remarks\n * This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that\n * exports its own child STT class, which inherits this class's methods.\n */\nexport abstract class STT extends (EventEmitter as new () => TypedEmitter<STTCallbacks>) {\n abstract label: string;\n #capabilities: STTCapabilities;\n\n constructor(capabilities: STTCapabilities) {\n super();\n this.#capabilities = capabilities;\n }\n\n /** Returns this STT's capabilities */\n get capabilities(): STTCapabilities {\n return this.#capabilities;\n }\n\n /** Receives an audio buffer and returns transcription in the form of a {@link SpeechEvent} */\n async recognize(frame: AudioBuffer, abortSignal?: AbortSignal): Promise<SpeechEvent> {\n const startTime = process.hrtime.bigint();\n const event = await this._recognize(frame, abortSignal);\n const durationMs = Number((process.hrtime.bigint() - startTime) / BigInt(1000000));\n this.emit('metrics_collected', {\n type: 'stt_metrics',\n requestId: event.requestId ?? '',\n timestamp: Date.now(),\n durationMs,\n label: this.label,\n audioDurationMs: Math.round(calculateAudioDurationSeconds(frame) * 1000),\n streamed: false,\n });\n return event;\n }\n\n protected abstract _recognize(\n frame: AudioBuffer,\n abortSignal?: AbortSignal,\n ): Promise<SpeechEvent>;\n\n /**\n * Returns a {@link SpeechStream} that can be used to push audio frames and receive\n * transcriptions\n *\n * @param options - Optional configuration including connection options\n */\n abstract stream(options?: { connOptions?: APIConnectOptions }): SpeechStream;\n\n async close(): Promise<void> {\n return;\n }\n}\n\n/**\n * An instance of a speech-to-text stream, as an asynchronous iterable iterator.\n *\n * @example Looping through frames\n * ```ts\n * for await (const event of stream) {\n * if (event.type === SpeechEventType.FINAL_TRANSCRIPT) {\n * console.log(event.alternatives[0].text)\n * }\n * }\n * ```\n *\n * @remarks\n * This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that\n * exports its own child SpeechStream class, which inherits this class's methods.\n */\nexport abstract class SpeechStream implements AsyncIterableIterator<SpeechEvent> {\n protected static readonly FLUSH_SENTINEL = Symbol('FLUSH_SENTINEL');\n protected input = new AsyncIterableQueue<AudioFrame | typeof SpeechStream.FLUSH_SENTINEL>();\n protected output = new AsyncIterableQueue<SpeechEvent>();\n protected queue = new AsyncIterableQueue<SpeechEvent>();\n protected neededSampleRate?: number;\n protected resampler?: AudioResampler;\n abstract label: string;\n protected closed = false;\n #stt: STT;\n private deferredInputStream: DeferredReadableStream<AudioFrame>;\n private logger = log();\n private _connOptions: APIConnectOptions;\n private _startTimeOffset: number = 0;\n\n protected abortController = new AbortController();\n\n constructor(\n stt: STT,\n sampleRate?: number,\n connectionOptions: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,\n ) {\n this.#stt = stt;\n this._connOptions = connectionOptions;\n this.deferredInputStream = new DeferredReadableStream<AudioFrame>();\n this.neededSampleRate = sampleRate;\n this.monitorMetrics();\n this.pumpInput();\n\n // this is a hack to immitate asyncio.create_task so that mainTask\n // is run **after** the constructor has finished. Otherwise we get\n // runtime error when trying to access class variables in the\n // `run` method.\n startSoon(() => this.mainTask().finally(() => this.queue.close()));\n }\n\n private async mainTask() {\n for (let i = 0; i < this._connOptions.maxRetry + 1; i++) {\n try {\n return await this.run();\n } catch (error) {\n if (error instanceof APIError) {\n const retryInterval = intervalForRetry(this._connOptions, i);\n\n if (this._connOptions.maxRetry === 0 || !error.retryable) {\n this.emitError({ error, recoverable: false });\n throw error;\n } else if (i === this._connOptions.maxRetry) {\n this.emitError({ error, recoverable: false });\n throw new APIConnectionError({\n message: `failed to recognize speech after ${this._connOptions.maxRetry + 1} attempts`,\n options: { retryable: false },\n });\n } else {\n // Don't emit error event for recoverable errors during retry loop\n // to avoid ERR_UNHANDLED_ERROR or premature session termination\n this.logger.warn(\n { tts: this.#stt.label, attempt: i + 1, error },\n `failed to recognize speech, retrying in ${retryInterval}s`,\n );\n }\n\n if (retryInterval > 0) {\n await delay(retryInterval);\n }\n } else {\n this.emitError({ error: toError(error), recoverable: false });\n throw error;\n }\n }\n }\n }\n\n private emitError({ error, recoverable }: { error: Error; recoverable: boolean }) {\n this.#stt.emit('error', {\n type: 'stt_error',\n timestamp: Date.now(),\n label: this.#stt.label,\n error,\n recoverable,\n });\n }\n\n protected async pumpInput() {\n // TODO(AJS-35): Implement STT with webstreams API\n const inputStream = this.deferredInputStream.stream;\n const reader = inputStream.getReader();\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n this.pushFrame(value);\n }\n } catch (error) {\n this.logger.error('Error in STTStream mainTask:', error);\n } finally {\n reader.releaseLock();\n }\n }\n\n protected async monitorMetrics() {\n for await (const event of this.queue) {\n if (!this.output.closed) {\n try {\n this.output.put(event);\n } catch (e) {\n if (e instanceof Error && e.message.includes('Queue is closed')) {\n this.logger.warn(\n { err: e },\n 'Queue closed during transcript processing (expected during disconnect)',\n );\n }\n }\n }\n if (event.type !== SpeechEventType.RECOGNITION_USAGE) continue;\n const metrics: STTMetrics = {\n type: 'stt_metrics',\n timestamp: Date.now(),\n requestId: event.requestId!,\n durationMs: 0,\n label: this.#stt.label,\n audioDurationMs: Math.round(event.recognitionUsage!.audioDuration * 1000),\n streamed: true,\n };\n this.#stt.emit('metrics_collected', metrics);\n }\n if (!this.output.closed) {\n this.output.close();\n }\n }\n\n protected abstract run(): Promise<void>;\n\n protected get abortSignal(): AbortSignal {\n return this.abortController.signal;\n }\n\n get startTimeOffset(): number {\n return this._startTimeOffset;\n }\n\n set startTimeOffset(value: number) {\n if (value < 0) {\n throw new Error('startTimeOffset must be non-negative');\n }\n this._startTimeOffset = value;\n }\n\n updateInputStream(audioStream: ReadableStream<AudioFrame>) {\n this.deferredInputStream.setSource(audioStream);\n }\n\n detachInputStream() {\n this.deferredInputStream.detachSource();\n }\n\n /** Push an audio frame to the STT */\n pushFrame(frame: AudioFrame) {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n\n if (this.neededSampleRate && frame.sampleRate !== this.neededSampleRate) {\n if (!this.resampler) {\n this.resampler = new AudioResampler(frame.sampleRate, this.neededSampleRate);\n }\n }\n\n if (frame.samplesPerChannel === 0) {\n this.input.put(frame);\n return;\n }\n\n if (this.resampler) {\n const frames = this.resampler.push(frame);\n for (const frame of frames) {\n this.input.put(frame);\n }\n } else {\n this.input.put(frame);\n }\n }\n\n /** Flush the STT, causing it to process all pending text */\n flush() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.put(SpeechStream.FLUSH_SENTINEL);\n }\n\n /** Mark the input as ended and forbid additional pushes */\n endInput() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.close();\n }\n\n next(): Promise<IteratorResult<SpeechEvent>> {\n return this.output.next();\n }\n\n /** Close both the input and output of the STT stream */\n close() {\n if (!this.input.closed) this.input.close();\n if (!this.queue.closed) this.queue.close();\n if (!this.output.closed) this.output.close();\n if (!this.abortController.signal.aborted) this.abortController.abort();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): SpeechStream {\n return this;\n }\n}\n"],"mappings":"AAGA,SAA0B,sBAAsB;AAEhD,SAAS,oBAAoB;AAE7B,SAAS,oBAAoB,gBAAgB;AAC7C,SAAS,qCAAqC;AAC9C,SAAS,WAAW;AAEpB,SAAS,8BAA8B;AACvC,SAAiC,6BAA6B,wBAAwB;AAEtF,SAAS,oBAAoB,OAAO,WAAW,eAAe;AAIvD,IAAK,kBAAL,kBAAKA,qBAAL;AAML,EAAAA,kCAAA,qBAAkB,KAAlB;AAIA,EAAAA,kCAAA,wBAAqB,KAArB;AAKA,EAAAA,kCAAA,sBAAmB,KAAnB;AAKA,EAAAA,kCAAA,mBAAgB,KAAhB;AAEA,EAAAA,kCAAA,uBAAoB,KAApB;AAMA,EAAAA,kCAAA,0BAAuB,KAAvB;AA5BU,SAAAA;AAAA,GAAA;AAkGL,MAAe,YAAa,aAAsD;AAAA,EAEvF;AAAA,EAEA,YAAY,cAA+B;AACzC,UAAM;AACN,SAAK,gBAAgB;AAAA,EACvB;AAAA;AAAA,EAGA,IAAI,eAAgC;AAClC,WAAO,KAAK;AAAA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AAI,CAAC,KAAK,OAAO,OAAQ,MAAK,OAAO,MAAM;AAC3C,QAAI,CAAC,KAAK,gBAAgB,OAAO,QAAS,MAAK,gBAAgB,MAAM;AACrE,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,CAAC,OAAO,aAAa,IAAkB;AACrC,WAAO;AAAA,EACT;AACF;","names":["SpeechEventType","frame"]}
|
|
1
|
+
{"version":3,"sources":["../../src/stt/stt.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { type AudioFrame, AudioResampler } from '@livekit/rtc-node';\nimport type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';\nimport { EventEmitter } from 'node:events';\nimport type { ReadableStream } from 'node:stream/web';\nimport { APIConnectionError, APIError } from '../_exceptions.js';\nimport { calculateAudioDurationSeconds } from '../audio.js';\nimport { log } from '../log.js';\nimport type { STTMetrics } from '../metrics/base.js';\nimport { DeferredReadableStream } from '../stream/deferred_stream.js';\nimport { type APIConnectOptions, DEFAULT_API_CONNECT_OPTIONS, intervalForRetry } from '../types.js';\nimport type { AudioBuffer } from '../utils.js';\nimport { AsyncIterableQueue, delay, startSoon, toError } from '../utils.js';\nimport type { TimedString } from '../voice/index.js';\n\n/** Indicates start/middle/end of speech */\nexport enum SpeechEventType {\n /**\n * Indicate the start of speech.\n * If the STT doesn't support this event, this will be emitted at the same time\n * as the first INTERIM_TRANSCRIPT.\n */\n START_OF_SPEECH = 0,\n /**\n * Interim transcript, useful for real-time transcription.\n */\n INTERIM_TRANSCRIPT = 1,\n /**\n * Final transcript, emitted when the STT is confident enough that a certain\n * portion of the speech will not change.\n */\n FINAL_TRANSCRIPT = 2,\n /**\n * Indicate the end of speech, emitted when the user stops speaking.\n * The first alternative is a combination of all the previous FINAL_TRANSCRIPT events.\n */\n END_OF_SPEECH = 3,\n /** Usage event, emitted periodically to indicate usage metrics. */\n RECOGNITION_USAGE = 4,\n /**\n * Preflight transcript, emitted before final transcript when STT has high confidence\n * but hasn't fully committed yet. Includes all pre-committed transcripts including\n * final transcript from the previous STT run.\n */\n PREFLIGHT_TRANSCRIPT = 5,\n}\n\n/** SpeechData contains metadata about this {@link SpeechEvent}. */\nexport interface SpeechData {\n /** Language code of the speech. */\n language: string;\n /** Transcribed text. */\n text: string;\n /** Start time of the speech segment in seconds. */\n startTime: number;\n /** End time of the speech segment in seconds. */\n endTime: number;\n /** Confidence score of the transcription (0-1). */\n confidence: number;\n /** Word-level timing information. */\n words?: TimedString[];\n}\n\nexport interface RecognitionUsage {\n /** Duration of the audio that was recognized in seconds. */\n audioDuration: number;\n}\n\n/** SpeechEvent is a packet of speech-to-text data. */\nexport interface SpeechEvent {\n type: SpeechEventType;\n alternatives?: [SpeechData, ...SpeechData[]];\n requestId?: string;\n recognitionUsage?: RecognitionUsage;\n}\n\n/**\n * Describes the capabilities of the STT provider.\n *\n * @remarks\n * At present, the framework only supports providers that have a streaming endpoint.\n */\nexport interface STTCapabilities {\n streaming: boolean;\n interimResults: boolean;\n /**\n * Whether this STT supports aligned transcripts with word/chunk timestamps.\n * - 'word': Provider returns word-level timestamps\n * - 'chunk': Provider returns chunk-level timestamps (e.g., sentence/phrase boundaries)\n * - false: Provider does not support aligned transcripts\n */\n alignedTranscript?: 'word' | 'chunk' | false;\n}\n\nexport interface STTError {\n type: 'stt_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport type STTCallbacks = {\n ['metrics_collected']: (metrics: STTMetrics) => void;\n ['error']: (error: STTError) => void;\n};\n\n/**\n * An instance of a speech-to-text adapter.\n *\n * @remarks\n * This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that\n * exports its own child STT class, which inherits this class's methods.\n */\nexport abstract class STT extends (EventEmitter as new () => TypedEmitter<STTCallbacks>) {\n abstract label: string;\n #capabilities: STTCapabilities;\n\n constructor(capabilities: STTCapabilities) {\n super();\n this.#capabilities = capabilities;\n }\n\n /** Returns this STT's capabilities */\n get capabilities(): STTCapabilities {\n return this.#capabilities;\n }\n\n /** Receives an audio buffer and returns transcription in the form of a {@link SpeechEvent} */\n async recognize(frame: AudioBuffer, abortSignal?: AbortSignal): Promise<SpeechEvent> {\n const startTime = process.hrtime.bigint();\n const event = await this._recognize(frame, abortSignal);\n const durationMs = Number((process.hrtime.bigint() - startTime) / BigInt(1000000));\n this.emit('metrics_collected', {\n type: 'stt_metrics',\n requestId: event.requestId ?? '',\n timestamp: Date.now(),\n durationMs,\n label: this.label,\n audioDurationMs: Math.round(calculateAudioDurationSeconds(frame) * 1000),\n streamed: false,\n });\n return event;\n }\n\n protected abstract _recognize(\n frame: AudioBuffer,\n abortSignal?: AbortSignal,\n ): Promise<SpeechEvent>;\n\n /**\n * Returns a {@link SpeechStream} that can be used to push audio frames and receive\n * transcriptions\n *\n * @param options - Optional configuration including connection options\n */\n abstract stream(options?: { connOptions?: APIConnectOptions }): SpeechStream;\n\n async close(): Promise<void> {\n return;\n }\n}\n\n/**\n * An instance of a speech-to-text stream, as an asynchronous iterable iterator.\n *\n * @example Looping through frames\n * ```ts\n * for await (const event of stream) {\n * if (event.type === SpeechEventType.FINAL_TRANSCRIPT) {\n * console.log(event.alternatives[0].text)\n * }\n * }\n * ```\n *\n * @remarks\n * This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that\n * exports its own child SpeechStream class, which inherits this class's methods.\n */\nexport abstract class SpeechStream implements AsyncIterableIterator<SpeechEvent> {\n protected static readonly FLUSH_SENTINEL = Symbol('FLUSH_SENTINEL');\n protected input = new AsyncIterableQueue<AudioFrame | typeof SpeechStream.FLUSH_SENTINEL>();\n protected output = new AsyncIterableQueue<SpeechEvent>();\n protected queue = new AsyncIterableQueue<SpeechEvent>();\n protected neededSampleRate?: number;\n protected resampler?: AudioResampler;\n abstract label: string;\n protected closed = false;\n #stt: STT;\n private deferredInputStream: DeferredReadableStream<AudioFrame>;\n private logger = log();\n private _connOptions: APIConnectOptions;\n private _startTimeOffset: number = 0;\n\n protected abortController = new AbortController();\n\n constructor(\n stt: STT,\n sampleRate?: number,\n connectionOptions: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,\n ) {\n this.#stt = stt;\n this._connOptions = connectionOptions;\n this.deferredInputStream = new DeferredReadableStream<AudioFrame>();\n this.neededSampleRate = sampleRate;\n this.monitorMetrics();\n this.pumpInput();\n\n // this is a hack to immitate asyncio.create_task so that mainTask\n // is run **after** the constructor has finished. Otherwise we get\n // runtime error when trying to access class variables in the\n // `run` method.\n startSoon(() => this.mainTask().finally(() => this.queue.close()));\n }\n\n private async mainTask() {\n for (let i = 0; i < this._connOptions.maxRetry + 1; i++) {\n try {\n return await this.run();\n } catch (error) {\n if (error instanceof APIError) {\n const retryInterval = intervalForRetry(this._connOptions, i);\n\n if (this._connOptions.maxRetry === 0 || !error.retryable) {\n this.emitError({ error, recoverable: false });\n throw error;\n } else if (i === this._connOptions.maxRetry) {\n this.emitError({ error, recoverable: false });\n throw new APIConnectionError({\n message: `failed to recognize speech after ${this._connOptions.maxRetry + 1} attempts`,\n options: { retryable: false },\n });\n } else {\n // Don't emit error event for recoverable errors during retry loop\n // to avoid ERR_UNHANDLED_ERROR or premature session termination\n this.logger.warn(\n { stt: this.#stt.label, attempt: i + 1, error },\n `failed to recognize speech, retrying in ${retryInterval}ms`,\n );\n }\n\n if (retryInterval > 0) {\n await delay(retryInterval);\n }\n } else {\n this.emitError({ error: toError(error), recoverable: false });\n throw error;\n }\n }\n }\n }\n\n private emitError({ error, recoverable }: { error: Error; recoverable: boolean }) {\n this.#stt.emit('error', {\n type: 'stt_error',\n timestamp: Date.now(),\n label: this.#stt.label,\n error,\n recoverable,\n });\n }\n\n protected async pumpInput() {\n // TODO(AJS-35): Implement STT with webstreams API\n const inputStream = this.deferredInputStream.stream;\n const reader = inputStream.getReader();\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n this.pushFrame(value);\n }\n } catch (error) {\n this.logger.error('Error in STTStream mainTask:', error);\n } finally {\n reader.releaseLock();\n }\n }\n\n protected async monitorMetrics() {\n for await (const event of this.queue) {\n if (!this.output.closed) {\n try {\n this.output.put(event);\n } catch (e) {\n if (e instanceof Error && e.message.includes('Queue is closed')) {\n this.logger.warn(\n { err: e },\n 'Queue closed during transcript processing (expected during disconnect)',\n );\n }\n }\n }\n if (event.type !== SpeechEventType.RECOGNITION_USAGE) continue;\n const metrics: STTMetrics = {\n type: 'stt_metrics',\n timestamp: Date.now(),\n requestId: event.requestId!,\n durationMs: 0,\n label: this.#stt.label,\n audioDurationMs: Math.round(event.recognitionUsage!.audioDuration * 1000),\n streamed: true,\n };\n this.#stt.emit('metrics_collected', metrics);\n }\n if (!this.output.closed) {\n this.output.close();\n }\n }\n\n protected abstract run(): Promise<void>;\n\n protected get abortSignal(): AbortSignal {\n return this.abortController.signal;\n }\n\n get startTimeOffset(): number {\n return this._startTimeOffset;\n }\n\n set startTimeOffset(value: number) {\n if (value < 0) {\n throw new Error('startTimeOffset must be non-negative');\n }\n this._startTimeOffset = value;\n }\n\n updateInputStream(audioStream: ReadableStream<AudioFrame>) {\n this.deferredInputStream.setSource(audioStream);\n }\n\n detachInputStream() {\n this.deferredInputStream.detachSource();\n }\n\n /** Push an audio frame to the STT */\n pushFrame(frame: AudioFrame) {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n\n if (this.neededSampleRate && frame.sampleRate !== this.neededSampleRate) {\n if (!this.resampler) {\n this.resampler = new AudioResampler(frame.sampleRate, this.neededSampleRate);\n }\n }\n\n if (frame.samplesPerChannel === 0) {\n this.input.put(frame);\n return;\n }\n\n if (this.resampler) {\n const frames = this.resampler.push(frame);\n for (const frame of frames) {\n this.input.put(frame);\n }\n } else {\n this.input.put(frame);\n }\n }\n\n /** Flush the STT, causing it to process all pending text */\n flush() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.put(SpeechStream.FLUSH_SENTINEL);\n }\n\n /** Mark the input as ended and forbid additional pushes */\n endInput() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.close();\n }\n\n next(): Promise<IteratorResult<SpeechEvent>> {\n return this.output.next();\n }\n\n /** Close both the input and output of the STT stream */\n close() {\n if (!this.input.closed) this.input.close();\n if (!this.queue.closed) this.queue.close();\n if (!this.output.closed) this.output.close();\n if (!this.abortController.signal.aborted) this.abortController.abort();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): SpeechStream {\n return this;\n }\n}\n"],"mappings":"AAGA,SAA0B,sBAAsB;AAEhD,SAAS,oBAAoB;AAE7B,SAAS,oBAAoB,gBAAgB;AAC7C,SAAS,qCAAqC;AAC9C,SAAS,WAAW;AAEpB,SAAS,8BAA8B;AACvC,SAAiC,6BAA6B,wBAAwB;AAEtF,SAAS,oBAAoB,OAAO,WAAW,eAAe;AAIvD,IAAK,kBAAL,kBAAKA,qBAAL;AAML,EAAAA,kCAAA,qBAAkB,KAAlB;AAIA,EAAAA,kCAAA,wBAAqB,KAArB;AAKA,EAAAA,kCAAA,sBAAmB,KAAnB;AAKA,EAAAA,kCAAA,mBAAgB,KAAhB;AAEA,EAAAA,kCAAA,uBAAoB,KAApB;AAMA,EAAAA,kCAAA,0BAAuB,KAAvB;AA5BU,SAAAA;AAAA,GAAA;AAkGL,MAAe,YAAa,aAAsD;AAAA,EAEvF;AAAA,EAEA,YAAY,cAA+B;AACzC,UAAM;AACN,SAAK,gBAAgB;AAAA,EACvB;AAAA;AAAA,EAGA,IAAI,eAAgC;AAClC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,MAAM,UAAU,OAAoB,aAAiD;AACnF,UAAM,YAAY,QAAQ,OAAO,OAAO;AACxC,UAAM,QAAQ,MAAM,KAAK,WAAW,OAAO,WAAW;AACtD,UAAM,aAAa,QAAQ,QAAQ,OAAO,OAAO,IAAI,aAAa,OAAO,GAAO,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ACF;","names":["SpeechEventType","frame"]}
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var fallback_adapter_exports = {};
|
|
20
|
+
__export(fallback_adapter_exports, {
|
|
21
|
+
FallbackAdapter: () => FallbackAdapter
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(fallback_adapter_exports);
|
|
24
|
+
var import_rtc_node = require("@livekit/rtc-node");
|
|
25
|
+
var import_exceptions = require("../_exceptions.cjs");
|
|
26
|
+
var import_log = require("../log.cjs");
|
|
27
|
+
var import_tokenize = require("../tokenize/index.cjs");
|
|
28
|
+
var import_types = require("../types.cjs");
|
|
29
|
+
var import_utils = require("../utils.cjs");
|
|
30
|
+
var import_stream_adapter = require("./stream_adapter.cjs");
|
|
31
|
+
var import_tts = require("./tts.cjs");
|
|
32
|
+
const DEFAULT_FALLBACK_API_CONNECT_OPTIONS = {
|
|
33
|
+
maxRetry: 0,
|
|
34
|
+
timeoutMs: import_types.DEFAULT_API_CONNECT_OPTIONS.timeoutMs,
|
|
35
|
+
retryIntervalMs: import_types.DEFAULT_API_CONNECT_OPTIONS.retryIntervalMs
|
|
36
|
+
};
|
|
37
|
+
const FORWARD_POLL_MS = 10;
|
|
38
|
+
class FallbackAdapter extends import_tts.TTS {
|
|
39
|
+
/** The list of TTS instances used for fallback (in priority order). */
|
|
40
|
+
ttsInstances;
|
|
41
|
+
/** Number of retries per TTS instance before falling back to the next one. */
|
|
42
|
+
maxRetryPerTTS;
|
|
43
|
+
/** Delay in milliseconds before attempting to recover a failed TTS instance. */
|
|
44
|
+
recoveryDelayMs;
|
|
45
|
+
_status = [];
|
|
46
|
+
_logger = (0, import_log.log)();
|
|
47
|
+
_recoveryTimeouts = /* @__PURE__ */ new Map();
|
|
48
|
+
label = `tts.FallbackAdapter`;
|
|
49
|
+
constructor(opts) {
|
|
50
|
+
if (!opts.ttsInstances || opts.ttsInstances.length < 1) {
|
|
51
|
+
throw new Error("at least one TTS instance must be provided.");
|
|
52
|
+
}
|
|
53
|
+
const numChannels = opts.ttsInstances[0].numChannels;
|
|
54
|
+
const allNumChannelsMatch = opts.ttsInstances.every((tts) => tts.numChannels === numChannels);
|
|
55
|
+
if (!allNumChannelsMatch) {
|
|
56
|
+
throw new Error("All TTS instances should have the same number of channels");
|
|
57
|
+
}
|
|
58
|
+
const sampleRate = Math.max(...opts.ttsInstances.map((t) => t.sampleRate));
|
|
59
|
+
const capabilities = FallbackAdapter.aggregateCapabilities(opts.ttsInstances);
|
|
60
|
+
super(sampleRate, numChannels, capabilities);
|
|
61
|
+
this.ttsInstances = opts.ttsInstances;
|
|
62
|
+
this.maxRetryPerTTS = opts.maxRetryPerTTS ?? 2;
|
|
63
|
+
this.recoveryDelayMs = opts.recoveryDelayMs ?? 1e3;
|
|
64
|
+
this._status = opts.ttsInstances.map(() => ({
|
|
65
|
+
available: true,
|
|
66
|
+
recoveringTask: null
|
|
67
|
+
}));
|
|
68
|
+
this.setupEventForwarding();
|
|
69
|
+
}
|
|
70
|
+
static aggregateCapabilities(instances) {
|
|
71
|
+
const streaming = instances.some((tts) => tts.capabilities.streaming);
|
|
72
|
+
const alignedTranscript = instances.every((tts) => tts.capabilities.alignedTranscript === true);
|
|
73
|
+
return { streaming, alignedTranscript };
|
|
74
|
+
}
|
|
75
|
+
setupEventForwarding() {
|
|
76
|
+
this.ttsInstances.forEach((tts) => {
|
|
77
|
+
tts.on("metrics_collected", (metrics) => {
|
|
78
|
+
this.emit("metrics_collected", metrics);
|
|
79
|
+
});
|
|
80
|
+
tts.on("error", (error) => {
|
|
81
|
+
this.emit("error", error);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Returns the current status of all TTS instances, including availability and recovery state.
|
|
87
|
+
*/
|
|
88
|
+
get status() {
|
|
89
|
+
return this._status;
|
|
90
|
+
}
|
|
91
|
+
getStreamingInstance(index) {
|
|
92
|
+
const tts = this.ttsInstances[index];
|
|
93
|
+
if (tts.capabilities.streaming) {
|
|
94
|
+
return tts;
|
|
95
|
+
}
|
|
96
|
+
return new import_stream_adapter.StreamAdapter(tts, new import_tokenize.basic.SentenceTokenizer());
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Creates a new AudioResampler for the given TTS index if needed.
|
|
100
|
+
* Returns null if the TTS sample rate matches the adapter's output rate.
|
|
101
|
+
* Each stream should create its own resampler to avoid concurrency issues.
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
createResamplerForTTS(index) {
|
|
105
|
+
const tts = this.ttsInstances[index];
|
|
106
|
+
if (this.sampleRate !== tts.sampleRate) {
|
|
107
|
+
this._logger.debug(
|
|
108
|
+
`resampling ${tts.label} from ${tts.sampleRate}Hz to ${this.sampleRate}Hz`
|
|
109
|
+
);
|
|
110
|
+
return new import_rtc_node.AudioResampler(tts.sampleRate, this.sampleRate, tts.numChannels);
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
emitAvailabilityChanged(tts, available) {
|
|
115
|
+
const event = { tts, available };
|
|
116
|
+
this.emit(
|
|
117
|
+
"tts_availability_changed",
|
|
118
|
+
event
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
tryRecovery(index) {
|
|
122
|
+
const status = this._status[index];
|
|
123
|
+
const tts = this.ttsInstances[index];
|
|
124
|
+
if (status.recoveringTask && !status.recoveringTask.done) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
status.recoveringTask = import_utils.Task.from(async (controller) => {
|
|
128
|
+
try {
|
|
129
|
+
const testStream = tts.synthesize(
|
|
130
|
+
"Hello world, this is a recovery test.",
|
|
131
|
+
{
|
|
132
|
+
maxRetry: 0,
|
|
133
|
+
timeoutMs: 1e4,
|
|
134
|
+
retryIntervalMs: 1e3
|
|
135
|
+
},
|
|
136
|
+
controller.signal
|
|
137
|
+
);
|
|
138
|
+
let audioReceived = false;
|
|
139
|
+
for await (const _ of testStream) {
|
|
140
|
+
audioReceived = true;
|
|
141
|
+
}
|
|
142
|
+
if (!audioReceived) {
|
|
143
|
+
throw new Error("Recovery test completed but no audio was received");
|
|
144
|
+
}
|
|
145
|
+
status.available = true;
|
|
146
|
+
status.recoveringTask = null;
|
|
147
|
+
this._logger.info({ tts: tts.label }, "TTS recovered");
|
|
148
|
+
this.emitAvailabilityChanged(tts, true);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
status.recoveringTask = null;
|
|
151
|
+
if (controller.signal.aborted) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
this._logger.debug({ tts: tts.label, error }, "TTS recovery failed, will retry");
|
|
155
|
+
const timeoutId = setTimeout(() => {
|
|
156
|
+
this._recoveryTimeouts.delete(index);
|
|
157
|
+
this.tryRecovery(index);
|
|
158
|
+
}, this.recoveryDelayMs);
|
|
159
|
+
this._recoveryTimeouts.set(index, timeoutId);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
markUnAvailable(index) {
|
|
164
|
+
const status = this._status[index];
|
|
165
|
+
if (status.recoveringTask && !status.recoveringTask.done) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (status.available) {
|
|
169
|
+
status.available = false;
|
|
170
|
+
this.emitAvailabilityChanged(this.ttsInstances[index], false);
|
|
171
|
+
}
|
|
172
|
+
this.tryRecovery(index);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Receives text and returns synthesis in the form of a {@link ChunkedStream}
|
|
176
|
+
*/
|
|
177
|
+
synthesize(text, connOptions, abortSignal) {
|
|
178
|
+
return new FallbackChunkedStream(
|
|
179
|
+
this,
|
|
180
|
+
text,
|
|
181
|
+
connOptions ?? DEFAULT_FALLBACK_API_CONNECT_OPTIONS,
|
|
182
|
+
abortSignal
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Returns a {@link SynthesizeStream} that can be used to push text and receive audio data
|
|
187
|
+
*
|
|
188
|
+
* @param options - Optional configuration including connection options
|
|
189
|
+
*/
|
|
190
|
+
stream(options) {
|
|
191
|
+
return new FallbackSynthesizeStream(
|
|
192
|
+
this,
|
|
193
|
+
(options == null ? void 0 : options.connOptions) ?? DEFAULT_FALLBACK_API_CONNECT_OPTIONS
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Close the FallbackAdapter and all underlying TTS instances.
|
|
198
|
+
* This cancels any ongoing recovery tasks and cleans up resources.
|
|
199
|
+
*/
|
|
200
|
+
async close() {
|
|
201
|
+
this._recoveryTimeouts.forEach((timeoutId) => {
|
|
202
|
+
clearTimeout(timeoutId);
|
|
203
|
+
});
|
|
204
|
+
this._recoveryTimeouts.clear();
|
|
205
|
+
const recoveryTasks = this._status.map((s) => s.recoveringTask).filter((t) => t !== null);
|
|
206
|
+
if (recoveryTasks.length > 0) {
|
|
207
|
+
await (0, import_utils.cancelAndWait)(recoveryTasks, 1e3);
|
|
208
|
+
}
|
|
209
|
+
for (const tts of this.ttsInstances) {
|
|
210
|
+
tts.removeAllListeners("metrics_collected");
|
|
211
|
+
tts.removeAllListeners("error");
|
|
212
|
+
}
|
|
213
|
+
await Promise.all(this.ttsInstances.map((tts) => tts.close()));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
class FallbackChunkedStream extends import_tts.ChunkedStream {
|
|
217
|
+
adapter;
|
|
218
|
+
connOptions;
|
|
219
|
+
_logger = (0, import_log.log)();
|
|
220
|
+
label = "tts.FallbackChunkedStream";
|
|
221
|
+
constructor(adapter, text, connOptions, abortSignal) {
|
|
222
|
+
super(text, adapter, connOptions, abortSignal);
|
|
223
|
+
this.adapter = adapter;
|
|
224
|
+
this.connOptions = connOptions;
|
|
225
|
+
}
|
|
226
|
+
async run() {
|
|
227
|
+
const allTTSFailed = this.adapter.status.every((s) => !s.available);
|
|
228
|
+
let lastRequestId = "";
|
|
229
|
+
let lastSegmentId = "";
|
|
230
|
+
if (allTTSFailed) {
|
|
231
|
+
this._logger.warn("All fallback TTS instances failed, retrying from first...");
|
|
232
|
+
}
|
|
233
|
+
for (let i = 0; i < this.adapter.ttsInstances.length; i++) {
|
|
234
|
+
const tts = this.adapter.ttsInstances[i];
|
|
235
|
+
const status = this.adapter.status[i];
|
|
236
|
+
if (!status.available && !allTTSFailed) {
|
|
237
|
+
this.adapter.markUnAvailable(i);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
this._logger.debug({ tts: tts.label }, "attempting TTS synthesis");
|
|
242
|
+
const connOptions = {
|
|
243
|
+
...this.connOptions,
|
|
244
|
+
maxRetry: this.adapter.maxRetryPerTTS
|
|
245
|
+
};
|
|
246
|
+
const stream = tts.synthesize(this.inputText, connOptions, this.abortSignal);
|
|
247
|
+
let audioReceived = false;
|
|
248
|
+
const resampler = this.adapter.createResamplerForTTS(i);
|
|
249
|
+
for await (const audio of stream) {
|
|
250
|
+
if (this.abortController.signal.aborted) {
|
|
251
|
+
stream.close();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (resampler) {
|
|
255
|
+
for (const frame of resampler.push(audio.frame)) {
|
|
256
|
+
this.queue.put({
|
|
257
|
+
...audio,
|
|
258
|
+
frame
|
|
259
|
+
});
|
|
260
|
+
audioReceived = true;
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
this.queue.put(audio);
|
|
264
|
+
audioReceived = true;
|
|
265
|
+
}
|
|
266
|
+
lastRequestId = audio.requestId;
|
|
267
|
+
lastSegmentId = audio.segmentId;
|
|
268
|
+
}
|
|
269
|
+
if (resampler) {
|
|
270
|
+
for (const frame of resampler.flush()) {
|
|
271
|
+
this.queue.put({
|
|
272
|
+
requestId: lastRequestId || "",
|
|
273
|
+
segmentId: lastSegmentId || "",
|
|
274
|
+
frame,
|
|
275
|
+
final: true
|
|
276
|
+
});
|
|
277
|
+
audioReceived = true;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (!audioReceived) {
|
|
281
|
+
throw new import_exceptions.APIConnectionError({
|
|
282
|
+
message: "TTS synthesis completed but no audio was received"
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
this._logger.debug({ tts: tts.label }, "TTS synthesis succeeded");
|
|
286
|
+
return;
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if (error instanceof import_exceptions.APIError || error instanceof import_exceptions.APIConnectionError) {
|
|
289
|
+
this._logger.warn({ tts: tts.label, error }, "TTS failed, switching to next instance");
|
|
290
|
+
this.adapter.markUnAvailable(i);
|
|
291
|
+
} else {
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const labels = this.adapter.ttsInstances.map((t) => t.label).join(", ");
|
|
297
|
+
throw new import_exceptions.APIConnectionError({
|
|
298
|
+
message: `all TTS instances failed (${labels})`
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
class FallbackSynthesizeStream extends import_tts.SynthesizeStream {
|
|
303
|
+
adapter;
|
|
304
|
+
tokenBuffer = [];
|
|
305
|
+
audioPushed = false;
|
|
306
|
+
_logger = (0, import_log.log)();
|
|
307
|
+
label = "tts.FallbackSynthesizeStream";
|
|
308
|
+
constructor(adapter, connOptions) {
|
|
309
|
+
super(adapter, connOptions);
|
|
310
|
+
this.adapter = adapter;
|
|
311
|
+
}
|
|
312
|
+
async run() {
|
|
313
|
+
const allTTSFailed = this.adapter.status.every((s) => !s.available);
|
|
314
|
+
if (allTTSFailed) {
|
|
315
|
+
this._logger.warn("All fallback TTS instances failed, retrying from first...");
|
|
316
|
+
}
|
|
317
|
+
const readInputLLMStream = (async () => {
|
|
318
|
+
try {
|
|
319
|
+
for await (const input of this.input) {
|
|
320
|
+
if (this.abortController.signal.aborted) break;
|
|
321
|
+
this.tokenBuffer.push(input);
|
|
322
|
+
}
|
|
323
|
+
} catch (error) {
|
|
324
|
+
this._logger.debug({ error }, "Error reading input LLM stream");
|
|
325
|
+
throw error;
|
|
326
|
+
} finally {
|
|
327
|
+
this.tokenBuffer.push(import_tts.SynthesizeStream.END_OF_STREAM);
|
|
328
|
+
}
|
|
329
|
+
})();
|
|
330
|
+
for (let i = 0; i < this.adapter.ttsInstances.length; i++) {
|
|
331
|
+
const tts = this.adapter.getStreamingInstance(i);
|
|
332
|
+
const originalTts = this.adapter.ttsInstances[i];
|
|
333
|
+
const status = this.adapter.status[i];
|
|
334
|
+
let lastRequestId = "";
|
|
335
|
+
let lastSegmentId = "";
|
|
336
|
+
if (!status.available && !allTTSFailed) {
|
|
337
|
+
this.adapter.markUnAvailable(i);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
this._logger.debug({ tts: originalTts.label }, "attempting TTS stream");
|
|
342
|
+
const connOptions = {
|
|
343
|
+
...this.connOptions,
|
|
344
|
+
maxRetry: this.adapter.maxRetryPerTTS
|
|
345
|
+
};
|
|
346
|
+
const stream = tts.stream({ connOptions });
|
|
347
|
+
const resampler = this.adapter.createResamplerForTTS(i);
|
|
348
|
+
let bufferIndex = 0;
|
|
349
|
+
let streamOutputCompleted = false;
|
|
350
|
+
const forwardBufferToTTS = async () => {
|
|
351
|
+
while (true) {
|
|
352
|
+
while (bufferIndex < this.tokenBuffer.length) {
|
|
353
|
+
const token = this.tokenBuffer[bufferIndex++];
|
|
354
|
+
if (token === import_tts.SynthesizeStream.FLUSH_SENTINEL) {
|
|
355
|
+
stream.flush();
|
|
356
|
+
} else if (token === import_tts.SynthesizeStream.END_OF_STREAM) {
|
|
357
|
+
stream.endInput();
|
|
358
|
+
return;
|
|
359
|
+
} else {
|
|
360
|
+
stream.pushText(token);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
await new Promise((resolve) => setTimeout(resolve, FORWARD_POLL_MS));
|
|
364
|
+
if (this.abortController.signal.aborted || streamOutputCompleted) {
|
|
365
|
+
stream.endInput();
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
const processOutput = async () => {
|
|
371
|
+
try {
|
|
372
|
+
for await (const audio of stream) {
|
|
373
|
+
if (this.abortController.signal.aborted) {
|
|
374
|
+
stream.close();
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (audio === import_tts.SynthesizeStream.END_OF_STREAM) {
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (resampler) {
|
|
381
|
+
for (const frame of resampler.push(audio.frame)) {
|
|
382
|
+
this.queue.put({
|
|
383
|
+
...audio,
|
|
384
|
+
frame
|
|
385
|
+
});
|
|
386
|
+
this.audioPushed = true;
|
|
387
|
+
}
|
|
388
|
+
} else {
|
|
389
|
+
this.queue.put(audio);
|
|
390
|
+
this.audioPushed = true;
|
|
391
|
+
}
|
|
392
|
+
lastRequestId = audio.requestId;
|
|
393
|
+
lastSegmentId = audio.segmentId;
|
|
394
|
+
}
|
|
395
|
+
if (resampler) {
|
|
396
|
+
for (const frame of resampler.flush()) {
|
|
397
|
+
this.queue.put({
|
|
398
|
+
requestId: lastRequestId || "",
|
|
399
|
+
segmentId: lastSegmentId || "",
|
|
400
|
+
frame,
|
|
401
|
+
final: true
|
|
402
|
+
});
|
|
403
|
+
this.audioPushed = true;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
} finally {
|
|
407
|
+
streamOutputCompleted = true;
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
const [outputResult, forwardBufferResult] = await Promise.allSettled([
|
|
411
|
+
processOutput(),
|
|
412
|
+
forwardBufferToTTS().catch((err) => {
|
|
413
|
+
stream.close();
|
|
414
|
+
throw err;
|
|
415
|
+
})
|
|
416
|
+
]);
|
|
417
|
+
if (outputResult.status === "rejected") {
|
|
418
|
+
stream.close();
|
|
419
|
+
throw outputResult.reason;
|
|
420
|
+
}
|
|
421
|
+
if (forwardBufferResult.status === "rejected") {
|
|
422
|
+
stream.close();
|
|
423
|
+
throw forwardBufferResult.reason;
|
|
424
|
+
}
|
|
425
|
+
if (!this.audioPushed) {
|
|
426
|
+
throw new import_exceptions.APIConnectionError({
|
|
427
|
+
message: "TTS stream completed but no audio was received"
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
this.queue.put(import_tts.SynthesizeStream.END_OF_STREAM);
|
|
431
|
+
this._logger.debug({ tts: originalTts.label }, "TTS stream succeeded");
|
|
432
|
+
await readInputLLMStream.catch(() => {
|
|
433
|
+
});
|
|
434
|
+
return;
|
|
435
|
+
} catch (error) {
|
|
436
|
+
if (this.audioPushed) {
|
|
437
|
+
this._logger.error(
|
|
438
|
+
{ tts: originalTts.label },
|
|
439
|
+
"TTS failed after audio pushed, cannot fallback mid-utterance"
|
|
440
|
+
);
|
|
441
|
+
throw error;
|
|
442
|
+
}
|
|
443
|
+
if (error instanceof import_exceptions.APIError || error instanceof import_exceptions.APIConnectionError) {
|
|
444
|
+
this._logger.warn(
|
|
445
|
+
{ tts: originalTts.label, error },
|
|
446
|
+
"TTS failed, switching to next instance"
|
|
447
|
+
);
|
|
448
|
+
this.adapter.markUnAvailable(i);
|
|
449
|
+
} else {
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
await readInputLLMStream.catch(() => {
|
|
455
|
+
});
|
|
456
|
+
const labels = this.adapter.ttsInstances.map((t) => t.label).join(", ");
|
|
457
|
+
throw new import_exceptions.APIConnectionError({
|
|
458
|
+
message: `all TTS instances failed (${labels})`
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
463
|
+
0 && (module.exports = {
|
|
464
|
+
FallbackAdapter
|
|
465
|
+
});
|
|
466
|
+
//# sourceMappingURL=fallback_adapter.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/tts/fallback_adapter.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { AudioResampler } from '@livekit/rtc-node';\nimport { APIConnectionError, APIError } from '../_exceptions.js';\nimport { log } from '../log.js';\nimport { basic } from '../tokenize/index.js';\nimport { type APIConnectOptions, DEFAULT_API_CONNECT_OPTIONS } from '../types.js';\nimport { Task, cancelAndWait } from '../utils.js';\nimport { StreamAdapter } from './stream_adapter.js';\nimport { ChunkedStream, SynthesizeStream, TTS, type TTSCapabilities } from './tts.js';\n\n/**\n * Internal status tracking for each TTS instance.\n * @internal\n */\ninterface TTSStatus {\n available: boolean;\n recoveringTask: Task<void> | null;\n}\n\n/**\n * Options for creating a FallbackAdapter.\n */\nexport interface FallbackAdapterOptions {\n /** List of TTS instances to use for fallback (in priority order). At least one is required. */\n ttsInstances: TTS[];\n /** Number of internal retries per TTS instance before moving to the next one. Defaults to 2. */\n maxRetryPerTTS?: number;\n /** Delay in milliseconds before attempting to recover a failed TTS instance. Defaults to 1000. */\n recoveryDelayMs?: number;\n}\n\n/**\n * Event emitted when a TTS instance's availability changes.\n */\nexport interface AvailabilityChangedEvent {\n /** The TTS instance whose availability changed. */\n tts: TTS;\n /** Whether the TTS instance is now available. */\n available: boolean;\n}\n\nconst DEFAULT_FALLBACK_API_CONNECT_OPTIONS: APIConnectOptions = {\n maxRetry: 0,\n timeoutMs: DEFAULT_API_CONNECT_OPTIONS.timeoutMs,\n retryIntervalMs: DEFAULT_API_CONNECT_OPTIONS.retryIntervalMs,\n};\n\nconst FORWARD_POLL_MS = 10;\n\n/**\n * FallbackAdapter is a TTS wrapper that provides automatic failover between multiple TTS providers.\n *\n * When the primary TTS fails, it automatically switches to the next available provider in the list.\n * Failed providers are monitored in the background and restored when they recover.\n *\n * Features:\n * - Automatic failover to backup TTS providers on failure\n * - Background health checks to restore recovered providers\n * - Automatic audio resampling when TTS providers have different sample rates\n * - Support for both streaming and non-streaming TTS providers\n *\n * @example\n * ```typescript\n * import { FallbackAdapter } from '@livekit/agents';\n * import { TTS as OpenAITTS } from '@livekit/agents-plugin-openai';\n * import { TTS as ElevenLabsTTS } from '@livekit/agents-plugin-elevenlabs';\n *\n * const fallbackTTS = new FallbackAdapter({\n * ttsInstances: [\n * new OpenAITTS(), // Primary\n * new ElevenLabsTTS(), // Fallback\n * ],\n * maxRetryPerTTS: 2, // Retry each TTS twice before moving to next\n * recoveryDelayMs: 1000, // Check recovery every 1 second\n * });\n *\n * ```\n */\nexport class FallbackAdapter extends TTS {\n /** The list of TTS instances used for fallback (in priority order). */\n readonly ttsInstances: TTS[];\n /** Number of retries per TTS instance before falling back to the next one. */\n readonly maxRetryPerTTS: number;\n /** Delay in milliseconds before attempting to recover a failed TTS instance. */\n readonly recoveryDelayMs: number;\n\n private _status: TTSStatus[] = [];\n private _logger = log();\n private _recoveryTimeouts: Map<number, NodeJS.Timeout> = new Map();\n\n label: string = `tts.FallbackAdapter`;\n\n constructor(opts: FallbackAdapterOptions) {\n if (!opts.ttsInstances || opts.ttsInstances.length < 1) {\n throw new Error('at least one TTS instance must be provided.');\n }\n const numChannels = opts.ttsInstances[0]!.numChannels;\n const allNumChannelsMatch = opts.ttsInstances.every((tts) => tts.numChannels === numChannels);\n if (!allNumChannelsMatch) {\n throw new Error('All TTS instances should have the same number of channels');\n }\n const sampleRate = Math.max(...opts.ttsInstances.map((t) => t.sampleRate));\n const capabilities = FallbackAdapter.aggregateCapabilities(opts.ttsInstances);\n super(sampleRate, numChannels, capabilities);\n this.ttsInstances = opts.ttsInstances;\n this.maxRetryPerTTS = opts.maxRetryPerTTS ?? 2;\n this.recoveryDelayMs = opts.recoveryDelayMs ?? 1000;\n this._status = opts.ttsInstances.map(() => ({\n available: true,\n recoveringTask: null,\n }));\n this.setupEventForwarding();\n }\n private static aggregateCapabilities(instances: TTS[]): TTSCapabilities {\n const streaming = instances.some((tts) => tts.capabilities.streaming);\n const alignedTranscript = instances.every((tts) => tts.capabilities.alignedTranscript === true);\n return { streaming, alignedTranscript };\n }\n\n private setupEventForwarding(): void {\n this.ttsInstances.forEach((tts) => {\n tts.on('metrics_collected', (metrics) => {\n this.emit('metrics_collected', metrics);\n });\n tts.on('error', (error) => {\n this.emit('error', error);\n });\n });\n }\n\n /**\n * Returns the current status of all TTS instances, including availability and recovery state.\n */\n get status(): TTSStatus[] {\n return this._status;\n }\n\n getStreamingInstance(index: number): TTS {\n const tts = this.ttsInstances[index]!;\n if (tts.capabilities.streaming) {\n return tts;\n }\n // Wrap non-streaming TTS with StreamAdapter\n return new StreamAdapter(tts, new basic.SentenceTokenizer());\n }\n\n /**\n * Creates a new AudioResampler for the given TTS index if needed.\n * Returns null if the TTS sample rate matches the adapter's output rate.\n * Each stream should create its own resampler to avoid concurrency issues.\n * @internal\n */\n createResamplerForTTS(index: number): AudioResampler | null {\n const tts = this.ttsInstances[index]!;\n if (this.sampleRate !== tts.sampleRate) {\n this._logger.debug(\n `resampling ${tts.label} from ${tts.sampleRate}Hz to ${this.sampleRate}Hz`,\n );\n return new AudioResampler(tts.sampleRate, this.sampleRate, tts.numChannels);\n }\n return null;\n }\n\n private emitAvailabilityChanged(tts: TTS, available: boolean): void {\n const event: AvailabilityChangedEvent = { tts, available };\n (this as unknown as { emit: (event: string, data: AvailabilityChangedEvent) => void }).emit(\n 'tts_availability_changed',\n event,\n );\n }\n\n private tryRecovery(index: number): void {\n const status = this._status[index]!;\n const tts = this.ttsInstances[index]!;\n if (status.recoveringTask && !status.recoveringTask.done) {\n return;\n }\n status.recoveringTask = Task.from(async (controller) => {\n try {\n const testStream = tts.synthesize(\n 'Hello world, this is a recovery test.',\n {\n maxRetry: 0,\n timeoutMs: 10000,\n retryIntervalMs: 1000,\n },\n controller.signal,\n );\n let audioReceived = false;\n for await (const _ of testStream) {\n audioReceived = true;\n }\n if (!audioReceived) {\n throw new Error('Recovery test completed but no audio was received');\n }\n\n status.available = true;\n status.recoveringTask = null;\n this._logger.info({ tts: tts.label }, 'TTS recovered');\n this.emitAvailabilityChanged(tts, true);\n } catch (error) {\n status.recoveringTask = null;\n // Don't schedule retry if we're shutting down\n if (controller.signal.aborted) {\n return;\n }\n this._logger.debug({ tts: tts.label, error }, 'TTS recovery failed, will retry');\n // Retry recovery after delay (matches Python's retry behavior)\n const timeoutId = setTimeout(() => {\n this._recoveryTimeouts.delete(index);\n this.tryRecovery(index);\n }, this.recoveryDelayMs);\n this._recoveryTimeouts.set(index, timeoutId);\n }\n });\n }\n\n markUnAvailable(index: number): void {\n const status = this._status[index]!;\n if (status.recoveringTask && !status.recoveringTask.done) {\n return;\n }\n if (status.available) {\n status.available = false;\n this.emitAvailabilityChanged(this.ttsInstances[index]!, false);\n }\n this.tryRecovery(index);\n }\n\n /**\n * Receives text and returns synthesis in the form of a {@link ChunkedStream}\n */\n synthesize(\n text: string,\n connOptions?: APIConnectOptions,\n abortSignal?: AbortSignal,\n ): ChunkedStream {\n return new FallbackChunkedStream(\n this,\n text,\n connOptions ?? DEFAULT_FALLBACK_API_CONNECT_OPTIONS,\n abortSignal,\n );\n }\n\n /**\n * Returns a {@link SynthesizeStream} that can be used to push text and receive audio data\n *\n * @param options - Optional configuration including connection options\n */\n stream(options?: { connOptions?: APIConnectOptions }): SynthesizeStream {\n return new FallbackSynthesizeStream(\n this,\n options?.connOptions ?? DEFAULT_FALLBACK_API_CONNECT_OPTIONS,\n );\n }\n\n /**\n * Close the FallbackAdapter and all underlying TTS instances.\n * This cancels any ongoing recovery tasks and cleans up resources.\n */\n async close(): Promise<void> {\n // clear all recovery timeouts so that it does not cause issue\n this._recoveryTimeouts.forEach((timeoutId) => {\n clearTimeout(timeoutId);\n });\n this._recoveryTimeouts.clear();\n\n // Cancel all recovery tasks\n const recoveryTasks = this._status\n .map((s) => s.recoveringTask)\n .filter((t): t is Task<void> => t !== null);\n\n if (recoveryTasks.length > 0) {\n await cancelAndWait(recoveryTasks, 1000);\n }\n\n // Remove event listeners\n for (const tts of this.ttsInstances) {\n tts.removeAllListeners('metrics_collected');\n tts.removeAllListeners('error');\n }\n\n // Close all TTS instances\n await Promise.all(this.ttsInstances.map((tts) => tts.close()));\n }\n}\n\nclass FallbackChunkedStream extends ChunkedStream {\n private adapter: FallbackAdapter;\n private connOptions: APIConnectOptions;\n private _logger = log();\n\n label: string = 'tts.FallbackChunkedStream';\n\n constructor(\n adapter: FallbackAdapter,\n text: string,\n connOptions: APIConnectOptions,\n abortSignal?: AbortSignal,\n ) {\n super(text, adapter, connOptions, abortSignal);\n this.adapter = adapter;\n this.connOptions = connOptions;\n }\n\n protected async run(): Promise<void> {\n const allTTSFailed = this.adapter.status.every((s) => !s.available);\n let lastRequestId: string = '';\n let lastSegmentId: string = '';\n if (allTTSFailed) {\n this._logger.warn('All fallback TTS instances failed, retrying from first...');\n }\n for (let i = 0; i < this.adapter.ttsInstances.length; i++) {\n const tts = this.adapter.ttsInstances[i]!;\n const status = this.adapter.status[i]!;\n if (!status.available && !allTTSFailed) {\n this.adapter.markUnAvailable(i);\n continue;\n }\n try {\n this._logger.debug({ tts: tts.label }, 'attempting TTS synthesis');\n const connOptions: APIConnectOptions = {\n ...this.connOptions,\n maxRetry: this.adapter.maxRetryPerTTS,\n };\n const stream = tts.synthesize(this.inputText, connOptions, this.abortSignal);\n let audioReceived = false;\n const resampler = this.adapter.createResamplerForTTS(i);\n for await (const audio of stream) {\n if (this.abortController.signal.aborted) {\n stream.close();\n return;\n }\n\n if (resampler) {\n for (const frame of resampler.push(audio.frame)) {\n this.queue.put({\n ...audio,\n frame,\n });\n audioReceived = true;\n }\n } else {\n this.queue.put(audio);\n audioReceived = true;\n }\n lastRequestId = audio.requestId;\n lastSegmentId = audio.segmentId;\n }\n\n // Flush any remaining resampled frames\n if (resampler) {\n for (const frame of resampler.flush()) {\n this.queue.put({\n requestId: lastRequestId || '',\n segmentId: lastSegmentId || '',\n frame,\n final: true,\n });\n audioReceived = true;\n }\n }\n\n // Verify audio was actually received - silent failures should trigger fallback\n if (!audioReceived) {\n throw new APIConnectionError({\n message: 'TTS synthesis completed but no audio was received',\n });\n }\n\n this._logger.debug({ tts: tts.label }, 'TTS synthesis succeeded');\n return;\n } catch (error) {\n if (error instanceof APIError || error instanceof APIConnectionError) {\n this._logger.warn({ tts: tts.label, error }, 'TTS failed, switching to next instance');\n this.adapter.markUnAvailable(i);\n } else {\n throw error;\n }\n }\n }\n const labels = this.adapter.ttsInstances.map((t) => t.label).join(', ');\n throw new APIConnectionError({\n message: `all TTS instances failed (${labels})`,\n });\n }\n}\n\nclass FallbackSynthesizeStream extends SynthesizeStream {\n private adapter: FallbackAdapter;\n private tokenBuffer: (\n | string\n | typeof SynthesizeStream.FLUSH_SENTINEL\n | typeof SynthesizeStream.END_OF_STREAM\n )[] = [];\n private audioPushed = false;\n private _logger = log();\n\n label: string = 'tts.FallbackSynthesizeStream';\n\n constructor(adapter: FallbackAdapter, connOptions: APIConnectOptions) {\n super(adapter, connOptions);\n this.adapter = adapter;\n }\n\n protected async run(): Promise<void> {\n const allTTSFailed = this.adapter.status.every((s) => !s.available);\n if (allTTSFailed) {\n this._logger.warn('All fallback TTS instances failed, retrying from first...');\n }\n const readInputLLMStream = (async () => {\n try {\n for await (const input of this.input) {\n if (this.abortController.signal.aborted) break;\n this.tokenBuffer.push(input);\n }\n } catch (error) {\n this._logger.debug({ error }, 'Error reading input LLM stream');\n throw error;\n } finally {\n this.tokenBuffer.push(SynthesizeStream.END_OF_STREAM);\n }\n })();\n\n for (let i = 0; i < this.adapter.ttsInstances.length; i++) {\n const tts = this.adapter.getStreamingInstance(i);\n const originalTts = this.adapter.ttsInstances[i]!;\n const status = this.adapter.status[i]!;\n let lastRequestId: string = '';\n let lastSegmentId: string = '';\n\n if (!status.available && !allTTSFailed) {\n this.adapter.markUnAvailable(i);\n continue;\n }\n\n try {\n this._logger.debug({ tts: originalTts.label }, 'attempting TTS stream');\n\n const connOptions: APIConnectOptions = {\n ...this.connOptions,\n maxRetry: this.adapter.maxRetryPerTTS,\n };\n\n const stream = tts.stream({ connOptions });\n const resampler = this.adapter.createResamplerForTTS(i);\n let bufferIndex = 0;\n let streamOutputCompleted = false;\n const forwardBufferToTTS = async () => {\n while (true) {\n while (bufferIndex < this.tokenBuffer.length) {\n const token = this.tokenBuffer[bufferIndex++]!;\n if (token === SynthesizeStream.FLUSH_SENTINEL) {\n stream.flush();\n } else if (token === SynthesizeStream.END_OF_STREAM) {\n stream.endInput();\n return;\n } else {\n stream.pushText(token);\n }\n }\n await new Promise((resolve) => setTimeout(resolve, FORWARD_POLL_MS));\n if (this.abortController.signal.aborted || streamOutputCompleted) {\n stream.endInput();\n return;\n }\n }\n };\n\n const processOutput = async () => {\n try {\n for await (const audio of stream) {\n if (this.abortController.signal.aborted) {\n stream.close();\n return;\n }\n\n if (audio === SynthesizeStream.END_OF_STREAM) {\n // Don't forward END_OF_STREAM yet — only emit after we verify audio\n // was received. Otherwise a silent failure would signal completion\n // to consumers before fallback can try the next TTS.\n continue;\n }\n\n if (resampler) {\n for (const frame of resampler.push(audio.frame)) {\n this.queue.put({\n ...audio,\n frame,\n });\n this.audioPushed = true;\n }\n } else {\n this.queue.put(audio);\n this.audioPushed = true;\n }\n lastRequestId = audio.requestId;\n lastSegmentId = audio.segmentId;\n }\n\n // Flush resampler\n if (resampler) {\n for (const frame of resampler.flush()) {\n this.queue.put({\n requestId: lastRequestId || '',\n segmentId: lastSegmentId || '',\n frame,\n final: true,\n });\n this.audioPushed = true;\n }\n }\n } finally {\n // processOutput and forwardBufferToTTS run in parallel.\n // forwardBufferToTTS polls tokenBuffer and only exits when it sees END_OF_STREAM.\n // But END_OF_STREAM is only added when the LLM finishes streaming (line 417).\n // If the TTS fails while the LLM is still streaming, forwardBufferToTTS would\n // keep polling indefinitely, blocking fallback to the next TTS.\n // This flag tells it to exit early.\n streamOutputCompleted = true;\n }\n };\n const [outputResult, forwardBufferResult] = await Promise.allSettled([\n processOutput(),\n forwardBufferToTTS().catch((err) => {\n stream.close(); // Close stream so processOutput can exit\n throw err;\n }),\n ]);\n if (outputResult.status === 'rejected') {\n stream.close();\n throw outputResult.reason;\n }\n if (forwardBufferResult.status === 'rejected') {\n stream.close();\n throw forwardBufferResult.reason;\n }\n\n // Verify audio was actually received - if not, the TTS failed silently\n if (!this.audioPushed) {\n throw new APIConnectionError({\n message: 'TTS stream completed but no audio was received',\n });\n }\n\n this.queue.put(SynthesizeStream.END_OF_STREAM);\n this._logger.debug({ tts: originalTts.label }, 'TTS stream succeeded');\n await readInputLLMStream.catch(() => {});\n return;\n } catch (error) {\n if (this.audioPushed) {\n this._logger.error(\n { tts: originalTts.label },\n 'TTS failed after audio pushed, cannot fallback mid-utterance',\n );\n throw error;\n }\n\n if (error instanceof APIError || error instanceof APIConnectionError) {\n this._logger.warn(\n { tts: originalTts.label, error },\n 'TTS failed, switching to next instance',\n );\n this.adapter.markUnAvailable(i);\n } else {\n throw error;\n }\n }\n }\n await readInputLLMStream.catch(() => {});\n const labels = this.adapter.ttsInstances.map((t) => t.label).join(', ');\n throw new APIConnectionError({\n message: `all TTS instances failed (${labels})`,\n });\n }\n}\n"],"mappings":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