@livekit/agents-plugin-deepgram 0.0.0-next-20260624041820
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/LICENSE +201 -0
- package/README.md +17 -0
- package/dist/_utils.cjs +56 -0
- package/dist/_utils.cjs.map +1 -0
- package/dist/_utils.d.cts +12 -0
- package/dist/_utils.d.ts +12 -0
- package/dist/_utils.d.ts.map +1 -0
- package/dist/_utils.js +32 -0
- package/dist/_utils.js.map +1 -0
- package/dist/index.cjs +38 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/models.cjs +17 -0
- package/dist/models.cjs.map +1 -0
- package/dist/models.d.cts +6 -0
- package/dist/models.d.ts +6 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +1 -0
- package/dist/models.js.map +1 -0
- package/dist/stt.cjs +496 -0
- package/dist/stt.cjs.map +1 -0
- package/dist/stt.d.cts +53 -0
- package/dist/stt.d.ts +53 -0
- package/dist/stt.d.ts.map +1 -0
- package/dist/stt.js +483 -0
- package/dist/stt.js.map +1 -0
- package/dist/stt.test.cjs +28 -0
- package/dist/stt.test.cjs.map +1 -0
- package/dist/stt.test.d.cts +2 -0
- package/dist/stt.test.d.ts +2 -0
- package/dist/stt.test.d.ts.map +1 -0
- package/dist/stt.test.js +27 -0
- package/dist/stt.test.js.map +1 -0
- package/dist/stt_v2.cjs +413 -0
- package/dist/stt_v2.cjs.map +1 -0
- package/dist/stt_v2.d.cts +97 -0
- package/dist/stt_v2.d.ts +97 -0
- package/dist/stt_v2.d.ts.map +1 -0
- package/dist/stt_v2.js +387 -0
- package/dist/stt_v2.js.map +1 -0
- package/dist/stt_v2.test.cjs +17 -0
- package/dist/stt_v2.test.cjs.map +1 -0
- package/dist/stt_v2.test.d.cts +2 -0
- package/dist/stt_v2.test.d.ts +2 -0
- package/dist/stt_v2.test.d.ts.map +1 -0
- package/dist/stt_v2.test.js +16 -0
- package/dist/stt_v2.test.js.map +1 -0
- package/dist/tts.cjs +385 -0
- package/dist/tts.cjs.map +1 -0
- package/dist/tts.d.cts +43 -0
- package/dist/tts.d.ts +43 -0
- package/dist/tts.d.ts.map +1 -0
- package/dist/tts.js +369 -0
- package/dist/tts.js.map +1 -0
- package/dist/tts.test.cjs +17 -0
- package/dist/tts.test.cjs.map +1 -0
- package/dist/tts.test.d.cts +2 -0
- package/dist/tts.test.d.ts +2 -0
- package/dist/tts.test.d.ts.map +1 -0
- package/dist/tts.test.js +16 -0
- package/dist/tts.test.js.map +1 -0
- package/package.json +54 -0
- package/src/_utils.ts +50 -0
- package/src/index.ts +20 -0
- package/src/models.ts +93 -0
- package/src/stt.test.ts +36 -0
- package/src/stt.ts +612 -0
- package/src/stt_v2.test.ts +19 -0
- package/src/stt_v2.ts +560 -0
- package/src/tts.test.ts +19 -0
- package/src/tts.ts +450 -0
package/src/tts.ts
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import {
|
|
5
|
+
type APIConnectOptions,
|
|
6
|
+
APIConnectionError,
|
|
7
|
+
APIError,
|
|
8
|
+
APIStatusError,
|
|
9
|
+
APITimeoutError,
|
|
10
|
+
AudioByteStream,
|
|
11
|
+
log,
|
|
12
|
+
shortuuid,
|
|
13
|
+
tokenize,
|
|
14
|
+
tts,
|
|
15
|
+
} from '@livekit/agents';
|
|
16
|
+
import type { AudioFrame } from '@livekit/rtc-node';
|
|
17
|
+
import { request } from 'node:https';
|
|
18
|
+
import { type RawData, WebSocket } from 'ws';
|
|
19
|
+
import type { TTSEncoding, TTSModels } from './models.js';
|
|
20
|
+
|
|
21
|
+
const AUTHORIZATION_HEADER = 'Authorization';
|
|
22
|
+
const NUM_CHANNELS = 1;
|
|
23
|
+
const MIN_SENTENCE_LENGTH = 8;
|
|
24
|
+
|
|
25
|
+
export interface TTSOptions {
|
|
26
|
+
model: TTSModels | string;
|
|
27
|
+
encoding: TTSEncoding;
|
|
28
|
+
sampleRate: number;
|
|
29
|
+
speed?: number;
|
|
30
|
+
apiKey?: string;
|
|
31
|
+
baseUrl?: string;
|
|
32
|
+
sentenceTokenizer: tokenize.SentenceTokenizer;
|
|
33
|
+
capabilities: tts.TTSCapabilities;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const defaultTTSOptions: TTSOptions = {
|
|
37
|
+
model: 'aura-asteria-en',
|
|
38
|
+
encoding: 'linear16',
|
|
39
|
+
sampleRate: 24000,
|
|
40
|
+
apiKey: process.env.DEEPGRAM_API_KEY,
|
|
41
|
+
baseUrl: 'https://api.deepgram.com',
|
|
42
|
+
capabilities: {
|
|
43
|
+
streaming: true,
|
|
44
|
+
},
|
|
45
|
+
sentenceTokenizer: new tokenize.basic.SentenceTokenizer({
|
|
46
|
+
minSentenceLength: MIN_SENTENCE_LENGTH,
|
|
47
|
+
}),
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export class TTS extends tts.TTS {
|
|
51
|
+
private opts: TTSOptions;
|
|
52
|
+
label = 'deepgram.TTS';
|
|
53
|
+
|
|
54
|
+
get model(): string {
|
|
55
|
+
return this.opts.model;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get provider(): string {
|
|
59
|
+
return 'Deepgram';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
constructor(opts: Partial<TTSOptions> = {}) {
|
|
63
|
+
super(opts.sampleRate || defaultTTSOptions.sampleRate, NUM_CHANNELS, {
|
|
64
|
+
streaming: opts.capabilities?.streaming ?? defaultTTSOptions.capabilities.streaming,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
this.opts = {
|
|
68
|
+
...defaultTTSOptions,
|
|
69
|
+
...opts,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
if (this.opts.apiKey === undefined) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
'Deepgram API key is required, whether as an argument or as $DEEPGRAM_API_KEY',
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (this.opts.speed !== undefined && (this.opts.speed < 0.7 || this.opts.speed > 1.5)) {
|
|
79
|
+
throw new Error(`Deepgram TTS speed must be between 0.7 and 1.5, got ${this.opts.speed}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
synthesize(
|
|
84
|
+
text: string,
|
|
85
|
+
connOptions?: APIConnectOptions,
|
|
86
|
+
abortSignal?: AbortSignal,
|
|
87
|
+
): tts.ChunkedStream {
|
|
88
|
+
return new ChunkedStream(this, text, this.opts, connOptions, abortSignal);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
stream(options?: { connOptions?: APIConnectOptions }): tts.SynthesizeStream {
|
|
92
|
+
return new SynthesizeStream(this, this.opts, options?.connOptions);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class ChunkedStream extends tts.ChunkedStream {
|
|
97
|
+
label = 'deepgram.ChunkedStream';
|
|
98
|
+
#logger = log();
|
|
99
|
+
private opts: TTSOptions;
|
|
100
|
+
private text: string;
|
|
101
|
+
|
|
102
|
+
constructor(
|
|
103
|
+
tts: TTS,
|
|
104
|
+
text: string,
|
|
105
|
+
opts: TTSOptions,
|
|
106
|
+
connOptions?: APIConnectOptions,
|
|
107
|
+
abortSignal?: AbortSignal,
|
|
108
|
+
) {
|
|
109
|
+
super(text, tts, connOptions, abortSignal);
|
|
110
|
+
this.text = text;
|
|
111
|
+
this.opts = opts;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
protected async run() {
|
|
115
|
+
const requestId = shortuuid();
|
|
116
|
+
const bstream = new AudioByteStream(this.opts.sampleRate, NUM_CHANNELS);
|
|
117
|
+
const json = { text: this.text };
|
|
118
|
+
const url = new URL(`${this.opts.baseUrl!}/v1/speak`);
|
|
119
|
+
url.searchParams.append('sample_rate', this.opts.sampleRate.toString());
|
|
120
|
+
url.searchParams.append('model', this.opts.model);
|
|
121
|
+
url.searchParams.append('encoding', this.opts.encoding);
|
|
122
|
+
if (this.opts.speed !== undefined) {
|
|
123
|
+
url.searchParams.append('speed', this.opts.speed.toString());
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
await new Promise<void>((resolve, reject) => {
|
|
127
|
+
let settled = false;
|
|
128
|
+
const settle = (fn: () => void) => {
|
|
129
|
+
if (!settled) {
|
|
130
|
+
settled = true;
|
|
131
|
+
fn();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const req = request(
|
|
136
|
+
{
|
|
137
|
+
hostname: url.hostname,
|
|
138
|
+
port: 443,
|
|
139
|
+
path: url.pathname + url.search,
|
|
140
|
+
method: 'POST',
|
|
141
|
+
headers: {
|
|
142
|
+
[AUTHORIZATION_HEADER]: `Token ${this.opts.apiKey!}`,
|
|
143
|
+
'Content-Type': 'application/json',
|
|
144
|
+
},
|
|
145
|
+
signal: this.abortSignal,
|
|
146
|
+
},
|
|
147
|
+
(res) => {
|
|
148
|
+
if (res.statusCode !== 200) {
|
|
149
|
+
settle(() =>
|
|
150
|
+
reject(
|
|
151
|
+
new Error(
|
|
152
|
+
`Deepgram TTS HTTP request failed: ${res.statusCode} ${res.statusMessage}`,
|
|
153
|
+
),
|
|
154
|
+
),
|
|
155
|
+
);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
res.on('data', (chunk) => {
|
|
160
|
+
for (const frame of bstream.write(chunk)) {
|
|
161
|
+
if (!this.queue.closed) {
|
|
162
|
+
this.queue.put({
|
|
163
|
+
requestId,
|
|
164
|
+
frame,
|
|
165
|
+
final: false,
|
|
166
|
+
segmentId: requestId,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
res.on('error', (err) => {
|
|
173
|
+
if (err.message === 'aborted') return;
|
|
174
|
+
this.#logger.error({ err }, 'Deepgram TTS response error');
|
|
175
|
+
settle(() => reject(err));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
res.on('close', () => {
|
|
179
|
+
for (const frame of bstream.flush()) {
|
|
180
|
+
if (!this.queue.closed) {
|
|
181
|
+
this.queue.put({
|
|
182
|
+
requestId,
|
|
183
|
+
frame,
|
|
184
|
+
final: false,
|
|
185
|
+
segmentId: requestId,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!this.queue.closed) {
|
|
190
|
+
this.queue.close();
|
|
191
|
+
}
|
|
192
|
+
settle(() => resolve());
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
req.on('error', (err) => {
|
|
198
|
+
if (err.name === 'AbortError') return;
|
|
199
|
+
this.#logger.error({ err }, 'Deepgram TTS request error');
|
|
200
|
+
settle(() => reject(err));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
req.on('close', () => settle(() => resolve()));
|
|
204
|
+
req.write(JSON.stringify(json));
|
|
205
|
+
req.end();
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export class SynthesizeStream extends tts.SynthesizeStream {
|
|
211
|
+
private opts: TTSOptions;
|
|
212
|
+
private tokenizer: tokenize.SentenceStream;
|
|
213
|
+
#logger = log();
|
|
214
|
+
label = 'deepgram.SynthesizeStream';
|
|
215
|
+
|
|
216
|
+
private static readonly FLUSH_MSG = JSON.stringify({ type: 'Flush' });
|
|
217
|
+
private static readonly CLOSE_MSG = JSON.stringify({ type: 'Close' });
|
|
218
|
+
|
|
219
|
+
constructor(tts: TTS, opts: TTSOptions, connOptions?: APIConnectOptions) {
|
|
220
|
+
super(tts, connOptions);
|
|
221
|
+
this.opts = opts;
|
|
222
|
+
this.tokenizer = opts.sentenceTokenizer.stream();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private async closeWebSocket(ws: WebSocket): Promise<void> {
|
|
226
|
+
try {
|
|
227
|
+
// Send Flush and Close messages to ensure Deepgram processes all remaining audio
|
|
228
|
+
// and properly terminates the session, preventing lingering TTS sessions
|
|
229
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
230
|
+
ws.send(SynthesizeStream.FLUSH_MSG);
|
|
231
|
+
ws.send(SynthesizeStream.CLOSE_MSG);
|
|
232
|
+
|
|
233
|
+
// Wait for server acknowledgment to prevent race conditions and ensure
|
|
234
|
+
// proper cleanup, avoiding 429 Too Many Requests errors from lingering sessions
|
|
235
|
+
try {
|
|
236
|
+
await new Promise<void>((resolve, _reject) => {
|
|
237
|
+
const timeout = setTimeout(() => {
|
|
238
|
+
resolve();
|
|
239
|
+
}, 1000);
|
|
240
|
+
|
|
241
|
+
ws.once('message', () => {
|
|
242
|
+
clearTimeout(timeout);
|
|
243
|
+
resolve();
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
ws.once('close', () => {
|
|
247
|
+
clearTimeout(timeout);
|
|
248
|
+
resolve();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
ws.once('error', () => {
|
|
252
|
+
clearTimeout(timeout);
|
|
253
|
+
resolve();
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
} catch (e) {
|
|
257
|
+
// Ignore timeout or other errors during close sequence
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
} catch (e) {
|
|
261
|
+
console.warn(`Error during WebSocket close sequence: ${e}`);
|
|
262
|
+
} finally {
|
|
263
|
+
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
|
|
264
|
+
ws.close();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
protected async run() {
|
|
270
|
+
const requestId = shortuuid();
|
|
271
|
+
const segmentId = shortuuid();
|
|
272
|
+
|
|
273
|
+
const wsUrl = this.opts.baseUrl!.replace(/^http/, 'ws');
|
|
274
|
+
const url = new URL(`${wsUrl}/v1/speak`);
|
|
275
|
+
url.searchParams.append('sample_rate', this.opts.sampleRate.toString());
|
|
276
|
+
url.searchParams.append('model', this.opts.model);
|
|
277
|
+
url.searchParams.append('encoding', this.opts.encoding);
|
|
278
|
+
if (this.opts.speed !== undefined) {
|
|
279
|
+
url.searchParams.append('speed', this.opts.speed.toString());
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const ws = new WebSocket(url, {
|
|
283
|
+
headers: {
|
|
284
|
+
[AUTHORIZATION_HEADER]: `Token ${this.opts.apiKey!}`,
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
await new Promise((resolve, reject) => {
|
|
289
|
+
ws.on('open', resolve);
|
|
290
|
+
ws.on('error', (error) => reject(error));
|
|
291
|
+
ws.on('close', (code) => reject(`WebSocket returned ${code}`));
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
const inputTask = async () => {
|
|
295
|
+
for await (const data of this.input) {
|
|
296
|
+
if (data === SynthesizeStream.FLUSH_SENTINEL) {
|
|
297
|
+
this.tokenizer.flush();
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
this.tokenizer.pushText(data);
|
|
301
|
+
}
|
|
302
|
+
this.tokenizer.endInput();
|
|
303
|
+
this.tokenizer.close();
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
let markInputSent: () => void = () => {};
|
|
307
|
+
const inputSent = new Promise<void>((resolve) => {
|
|
308
|
+
markInputSent = resolve;
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const sendTask = async () => {
|
|
312
|
+
try {
|
|
313
|
+
for await (const event of this.tokenizer) {
|
|
314
|
+
if (this.abortController.signal.aborted) break;
|
|
315
|
+
|
|
316
|
+
let text = event.token;
|
|
317
|
+
if (!text.endsWith(' ')) {
|
|
318
|
+
text += ' ';
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const message = JSON.stringify({
|
|
322
|
+
type: 'Speak',
|
|
323
|
+
text: text,
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
ws.send(message);
|
|
327
|
+
markInputSent();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (!this.abortController.signal.aborted) {
|
|
331
|
+
ws.send(SynthesizeStream.FLUSH_MSG);
|
|
332
|
+
markInputSent();
|
|
333
|
+
}
|
|
334
|
+
} finally {
|
|
335
|
+
markInputSent();
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const recvTask = async () => {
|
|
340
|
+
const bstream = new AudioByteStream(this.opts.sampleRate, NUM_CHANNELS);
|
|
341
|
+
let finalReceived = false;
|
|
342
|
+
let timeout: NodeJS.Timeout | null = null;
|
|
343
|
+
let lastFrame: AudioFrame | undefined;
|
|
344
|
+
|
|
345
|
+
const sendLastFrame = (segmentId: string, final: boolean) => {
|
|
346
|
+
if (lastFrame && !this.queue.closed) {
|
|
347
|
+
this.queue.put({ requestId, segmentId, frame: lastFrame, final });
|
|
348
|
+
lastFrame = undefined;
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const clearMessageTimeout = () => {
|
|
353
|
+
if (timeout) {
|
|
354
|
+
clearTimeout(timeout);
|
|
355
|
+
timeout = null;
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const resetMessageTimeout = (reject: (reason?: unknown) => void) => {
|
|
360
|
+
clearMessageTimeout();
|
|
361
|
+
timeout = setTimeout(() => {
|
|
362
|
+
reject(new APITimeoutError({ message: 'Deepgram TTS recv idle timeout' }));
|
|
363
|
+
}, this.connOptions.timeoutMs);
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
await inputSent;
|
|
367
|
+
if (this.abortController.signal.aborted) return;
|
|
368
|
+
|
|
369
|
+
return new Promise<void>((resolve, reject) => {
|
|
370
|
+
resetMessageTimeout(reject);
|
|
371
|
+
|
|
372
|
+
ws.on('message', (data: RawData, isBinary: boolean) => {
|
|
373
|
+
clearMessageTimeout();
|
|
374
|
+
|
|
375
|
+
if (!isBinary) {
|
|
376
|
+
const message = JSON.parse(data.toString());
|
|
377
|
+
if (message.type === 'Flushed') {
|
|
378
|
+
finalReceived = true;
|
|
379
|
+
for (const frame of bstream.flush()) {
|
|
380
|
+
sendLastFrame(segmentId, false);
|
|
381
|
+
lastFrame = frame;
|
|
382
|
+
}
|
|
383
|
+
sendLastFrame(segmentId, true);
|
|
384
|
+
|
|
385
|
+
if (!this.queue.closed) {
|
|
386
|
+
this.queue.put(SynthesizeStream.END_OF_STREAM);
|
|
387
|
+
}
|
|
388
|
+
resolve();
|
|
389
|
+
return;
|
|
390
|
+
} else if (message.type === 'Warning') {
|
|
391
|
+
this.#logger.warn(`Deepgram warning: ${message.warn_msg}`);
|
|
392
|
+
} else if (message.type === 'Error' || message.type === 'error') {
|
|
393
|
+
reject(new APIError('Deepgram TTS returned error', { body: message }));
|
|
394
|
+
return;
|
|
395
|
+
} else if (message.type !== 'Metadata') {
|
|
396
|
+
this.#logger.warn({ message }, 'Unknown Deepgram message type');
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
resetMessageTimeout(reject);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const buffer =
|
|
404
|
+
data instanceof Buffer
|
|
405
|
+
? data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength)
|
|
406
|
+
: (data as ArrayBuffer);
|
|
407
|
+
for (const frame of bstream.write(buffer as ArrayBuffer)) {
|
|
408
|
+
sendLastFrame(segmentId, false);
|
|
409
|
+
lastFrame = frame;
|
|
410
|
+
}
|
|
411
|
+
resetMessageTimeout(reject);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
ws.on('close', (code, reason) => {
|
|
415
|
+
clearMessageTimeout();
|
|
416
|
+
if (!finalReceived) {
|
|
417
|
+
reject(
|
|
418
|
+
new APIStatusError({
|
|
419
|
+
message: 'Deepgram websocket connection closed unexpectedly',
|
|
420
|
+
options: {
|
|
421
|
+
statusCode: code || -1,
|
|
422
|
+
body: { reason: reason.toString() },
|
|
423
|
+
},
|
|
424
|
+
}),
|
|
425
|
+
);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
resolve();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
ws.on('error', (error) => {
|
|
432
|
+
clearMessageTimeout();
|
|
433
|
+
reject(error);
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
try {
|
|
439
|
+
await Promise.all([inputTask(), sendTask(), recvTask()]);
|
|
440
|
+
} catch (e) {
|
|
441
|
+
if (this.abortController.signal.aborted) return;
|
|
442
|
+
if (e instanceof APIError) throw e;
|
|
443
|
+
throw new APIConnectionError({
|
|
444
|
+
message: `Deepgram TTS WebSocket failed: ${(e as Error).message ?? 'unknown error'}`,
|
|
445
|
+
});
|
|
446
|
+
} finally {
|
|
447
|
+
await this.closeWebSocket(ws);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|