@micdrop/elevenlabs 1.1.4 → 1.2.1
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/README.md +43 -5
- package/dist/index.d.mts +34 -21
- package/dist/index.d.ts +34 -21
- package/dist/index.js +394 -29
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +392 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -32,27 +32,352 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
DEFAULT_MODEL_ID: () => DEFAULT_MODEL_ID,
|
|
34
34
|
DEFAULT_OUTPUT_FORMAT: () => DEFAULT_OUTPUT_FORMAT,
|
|
35
|
-
ElevenLabsTTS: () => ElevenLabsTTS
|
|
35
|
+
ElevenLabsTTS: () => ElevenLabsTTS,
|
|
36
|
+
isDialogueModel: () => isDialogueModel
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(index_exports);
|
|
38
39
|
|
|
39
40
|
// src/ElevenLabsTTS.ts
|
|
40
41
|
var import_server = require("@micdrop/server");
|
|
42
|
+
|
|
43
|
+
// src/DialogueWebSocket.ts
|
|
44
|
+
var import_ws2 = __toESM(require("ws"));
|
|
45
|
+
|
|
46
|
+
// src/ElevenLabsWebSocket.ts
|
|
41
47
|
var import_ws = __toESM(require("ws"));
|
|
48
|
+
function closeSocket(socket) {
|
|
49
|
+
socket.removeAllListeners();
|
|
50
|
+
socket.on("error", () => {
|
|
51
|
+
});
|
|
52
|
+
if (socket.readyState === import_ws.default.OPEN || socket.readyState === import_ws.default.CONNECTING) {
|
|
53
|
+
socket.close(1e3);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
42
56
|
|
|
43
57
|
// src/types.ts
|
|
44
58
|
var DEFAULT_MODEL_ID = "eleven_turbo_v2_5";
|
|
45
59
|
var DEFAULT_OUTPUT_FORMAT = "pcm_16000";
|
|
60
|
+
function isDialogueModel(modelId) {
|
|
61
|
+
return modelId.startsWith("eleven_v3") || modelId.startsWith("eleven_v4");
|
|
62
|
+
}
|
|
46
63
|
|
|
47
|
-
// src/
|
|
48
|
-
var
|
|
64
|
+
// src/DialogueWebSocket.ts
|
|
65
|
+
var KEEP_ALIVE_INTERVAL = 15e3;
|
|
49
66
|
var DEFAULT_CONNECTION_TIMEOUT = 5e3;
|
|
50
67
|
var DEFAULT_RETRY_DELAY = 1e3;
|
|
51
68
|
var DEFAULT_MAX_RETRY = 3;
|
|
52
|
-
var
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
var SENTENCE_END = /(?:[!?]+|(?<!\.)\.(?!\.))\s+/g;
|
|
70
|
+
var DialogueWebSocket = class {
|
|
71
|
+
// Whether the connection received any text
|
|
72
|
+
constructor(options, host) {
|
|
73
|
+
this.options = options;
|
|
74
|
+
this.host = host;
|
|
75
|
+
this.retryCount = 0;
|
|
76
|
+
this.destroyed = false;
|
|
77
|
+
// Bumped by every speak() and every cancel(), so a stream that starts late
|
|
78
|
+
// can tell whether it is still the one that should be heard.
|
|
79
|
+
this.generation = 0;
|
|
80
|
+
// Generation of the stream being spoken. A speak() that never carries a
|
|
81
|
+
// word leaves it in place, so the sentence it sends goes on to its end.
|
|
82
|
+
this.speaking = 0;
|
|
83
|
+
this.isProcessing = false;
|
|
84
|
+
this.textEnded = false;
|
|
85
|
+
// Whether the text stream has ended
|
|
86
|
+
this.textSent = "";
|
|
87
|
+
// Text of the utterance sent to ElevenLabs
|
|
88
|
+
this.textBuffer = "";
|
|
89
|
+
// End of the last chunk, held until its word is complete
|
|
90
|
+
this.unflushed = false;
|
|
91
|
+
// Whether text was sent since the last flush
|
|
92
|
+
this.pendingFlushes = 0;
|
|
93
|
+
// Flushes whose audio is not complete yet
|
|
94
|
+
this.newTurn = false;
|
|
95
|
+
// Whether the next input starts a new utterance
|
|
96
|
+
this.hasSpoken = false;
|
|
97
|
+
this.connect();
|
|
98
|
+
}
|
|
99
|
+
speak(textStream) {
|
|
100
|
+
const generation = ++this.generation;
|
|
101
|
+
let started = false;
|
|
102
|
+
const start = () => {
|
|
103
|
+
if (started) return this.speaking === generation;
|
|
104
|
+
if (this.generation !== generation) return false;
|
|
105
|
+
started = true;
|
|
106
|
+
if (this.isProcessing) this.restart();
|
|
107
|
+
this.speaking = generation;
|
|
108
|
+
this.isProcessing = true;
|
|
109
|
+
this.textEnded = false;
|
|
110
|
+
this.textSent = "";
|
|
111
|
+
this.textBuffer = "";
|
|
112
|
+
this.unflushed = false;
|
|
113
|
+
this.pendingFlushes = 0;
|
|
114
|
+
this.newTurn = true;
|
|
115
|
+
return true;
|
|
116
|
+
};
|
|
117
|
+
textStream.on("data", async (chunk) => {
|
|
118
|
+
if (!start()) return;
|
|
119
|
+
const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
|
|
120
|
+
await this.initPromise;
|
|
121
|
+
if (this.speaking !== generation) return;
|
|
122
|
+
const spaceIndex = text.lastIndexOf(" ");
|
|
123
|
+
if (spaceIndex === -1) {
|
|
124
|
+
this.textBuffer += text;
|
|
125
|
+
} else {
|
|
126
|
+
this.sendText(this.textBuffer + text.slice(0, spaceIndex + 1));
|
|
127
|
+
this.textBuffer = text.slice(spaceIndex + 1);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
textStream.on("error", (error) => {
|
|
131
|
+
if (!started || this.speaking !== generation) return;
|
|
132
|
+
this.log("Error in text stream, ending audio stream", error);
|
|
133
|
+
this.isProcessing = false;
|
|
134
|
+
});
|
|
135
|
+
textStream.on("end", async () => {
|
|
136
|
+
if (!started) return;
|
|
137
|
+
await this.initPromise;
|
|
138
|
+
if (this.speaking !== generation) return;
|
|
139
|
+
if (this.textBuffer.trim()) {
|
|
140
|
+
this.sendText(this.textBuffer + " ");
|
|
141
|
+
this.textBuffer = "";
|
|
142
|
+
}
|
|
143
|
+
this.textEnded = true;
|
|
144
|
+
this.flush();
|
|
145
|
+
this.checkEnded();
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
cancel() {
|
|
149
|
+
this.generation++;
|
|
150
|
+
this.speaking = 0;
|
|
151
|
+
if (!this.isProcessing) return;
|
|
152
|
+
this.log("Cancel");
|
|
153
|
+
this.isProcessing = false;
|
|
154
|
+
this.textSent = "";
|
|
155
|
+
this.textBuffer = "";
|
|
156
|
+
this.restart();
|
|
157
|
+
}
|
|
158
|
+
destroy() {
|
|
159
|
+
this.destroyed = true;
|
|
160
|
+
if (this.reconnectTimeout) {
|
|
161
|
+
clearTimeout(this.reconnectTimeout);
|
|
162
|
+
this.reconnectTimeout = void 0;
|
|
163
|
+
}
|
|
164
|
+
this.dropSocket();
|
|
165
|
+
this.isProcessing = false;
|
|
166
|
+
}
|
|
167
|
+
connect() {
|
|
168
|
+
this.initPromise = this.initWS().catch((error) => {
|
|
169
|
+
console.error("[ElevenLabsTTS] Connection error:", error);
|
|
170
|
+
this.reconnect();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** Drops the connection, with the synthesis running on it, for a new one */
|
|
174
|
+
restart() {
|
|
175
|
+
if (this.reconnectTimeout) {
|
|
176
|
+
clearTimeout(this.reconnectTimeout);
|
|
177
|
+
this.reconnectTimeout = void 0;
|
|
178
|
+
}
|
|
179
|
+
this.dropSocket();
|
|
180
|
+
this.connect();
|
|
181
|
+
}
|
|
182
|
+
/** Closes the connection without reconnecting */
|
|
183
|
+
dropSocket() {
|
|
184
|
+
if (this.connectionTimeout) {
|
|
185
|
+
clearTimeout(this.connectionTimeout);
|
|
186
|
+
this.connectionTimeout = void 0;
|
|
187
|
+
}
|
|
188
|
+
if (this.keepAliveInterval) {
|
|
189
|
+
clearInterval(this.keepAliveInterval);
|
|
190
|
+
this.keepAliveInterval = void 0;
|
|
191
|
+
}
|
|
192
|
+
if (this.socket) closeSocket(this.socket);
|
|
193
|
+
this.socket = void 0;
|
|
194
|
+
}
|
|
195
|
+
initWS() {
|
|
196
|
+
return new Promise((resolve, reject) => {
|
|
197
|
+
const params = new URLSearchParams();
|
|
198
|
+
params.append("model_id", this.options.modelId ?? DEFAULT_MODEL_ID);
|
|
199
|
+
params.append(
|
|
200
|
+
"output_format",
|
|
201
|
+
this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT
|
|
202
|
+
);
|
|
203
|
+
if (this.options.language) {
|
|
204
|
+
params.append("language_code", this.options.language);
|
|
205
|
+
}
|
|
206
|
+
const socket = new import_ws2.default(
|
|
207
|
+
`wss://api.elevenlabs.io/v1/text-to-dialogue/stream-input?${params.toString()}`,
|
|
208
|
+
{
|
|
209
|
+
headers: {
|
|
210
|
+
"xi-api-key": this.options.apiKey
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
this.socket = socket;
|
|
215
|
+
let opened = false;
|
|
216
|
+
const timeout = setTimeout(() => {
|
|
217
|
+
this.log("Connection timeout");
|
|
218
|
+
closeSocket(socket);
|
|
219
|
+
this.socket = void 0;
|
|
220
|
+
reject(new Error("WebSocket connection timeout"));
|
|
221
|
+
}, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
|
|
222
|
+
this.connectionTimeout = timeout;
|
|
223
|
+
socket.addEventListener("open", () => {
|
|
224
|
+
clearTimeout(timeout);
|
|
225
|
+
opened = true;
|
|
226
|
+
this.log("Connection opened");
|
|
227
|
+
const stability = this.options.voiceSettings?.stability;
|
|
228
|
+
this.send({
|
|
229
|
+
voices: [this.options.voiceId],
|
|
230
|
+
voice_settings: stability === void 0 ? void 0 : { stability }
|
|
231
|
+
});
|
|
232
|
+
this.hasSpoken = false;
|
|
233
|
+
this.keepAliveInterval = setInterval(() => {
|
|
234
|
+
this.send({ keep_alive: true });
|
|
235
|
+
this.retryCount = 0;
|
|
236
|
+
}, KEEP_ALIVE_INTERVAL);
|
|
237
|
+
resolve();
|
|
238
|
+
});
|
|
239
|
+
socket.addEventListener("error", (error) => {
|
|
240
|
+
clearTimeout(timeout);
|
|
241
|
+
this.log("WebSocket error:", error);
|
|
242
|
+
reject(new Error("WebSocket connection error"));
|
|
243
|
+
});
|
|
244
|
+
socket.addEventListener("message", (event) => {
|
|
245
|
+
try {
|
|
246
|
+
this.onMessage(JSON.parse(event.data.toString()));
|
|
247
|
+
} catch (error) {
|
|
248
|
+
this.log("message" in error ? error.message : error);
|
|
249
|
+
this.log("Event data during error:", event.data);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
socket.addEventListener("close", ({ code, reason }) => {
|
|
253
|
+
clearTimeout(timeout);
|
|
254
|
+
if (this.keepAliveInterval) {
|
|
255
|
+
clearInterval(this.keepAliveInterval);
|
|
256
|
+
this.keepAliveInterval = void 0;
|
|
257
|
+
}
|
|
258
|
+
this.socket?.removeAllListeners();
|
|
259
|
+
this.socket = void 0;
|
|
260
|
+
this.log("Connection closed", { code, reason });
|
|
261
|
+
if (opened) this.reconnect();
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
onMessage(message) {
|
|
266
|
+
if ("audio" in message && message.audio) {
|
|
267
|
+
const chunk = Buffer.from(message.audio, "base64");
|
|
268
|
+
this.log(`Received audio chunk (${chunk.length} bytes)`);
|
|
269
|
+
this.retryCount = 0;
|
|
270
|
+
this.host.audio(chunk);
|
|
271
|
+
}
|
|
272
|
+
if ("is_final_audio_for_turn" in message && message.is_final_audio_for_turn) {
|
|
273
|
+
this.pendingFlushes = Math.max(0, this.pendingFlushes - 1);
|
|
274
|
+
this.checkEnded();
|
|
275
|
+
}
|
|
276
|
+
if ("error" in message) {
|
|
277
|
+
console.error("[ElevenLabsTTS] Error:", message.error, message.message);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/** Sends text, flushing after each sentence it completes */
|
|
281
|
+
sendText(text) {
|
|
282
|
+
let start = 0;
|
|
283
|
+
for (const match of text.matchAll(SENTENCE_END)) {
|
|
284
|
+
const end = match.index + match[0].length;
|
|
285
|
+
this.sendInput(text.slice(start, end), true);
|
|
286
|
+
start = end;
|
|
287
|
+
}
|
|
288
|
+
if (start < text.length) {
|
|
289
|
+
this.sendInput(text.slice(start), false);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
sendInput(text, flush) {
|
|
293
|
+
this.textSent += text;
|
|
294
|
+
if (!text.trim()) return;
|
|
295
|
+
this.send({
|
|
296
|
+
inputs: [
|
|
297
|
+
{
|
|
298
|
+
text,
|
|
299
|
+
voice_id: this.options.voiceId,
|
|
300
|
+
// Resets the prosody between two utterances
|
|
301
|
+
new_turn: this.newTurn && this.hasSpoken || void 0
|
|
302
|
+
}
|
|
303
|
+
],
|
|
304
|
+
flush: flush || void 0
|
|
305
|
+
});
|
|
306
|
+
this.log(`Sent text${flush ? " and flushed" : ""}: "${text}"`);
|
|
307
|
+
this.newTurn = false;
|
|
308
|
+
this.hasSpoken = true;
|
|
309
|
+
if (flush) {
|
|
310
|
+
this.pendingFlushes++;
|
|
311
|
+
this.unflushed = false;
|
|
312
|
+
} else {
|
|
313
|
+
this.unflushed = true;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/** Generates the text held by the server, if any */
|
|
317
|
+
flush() {
|
|
318
|
+
if (!this.unflushed) return;
|
|
319
|
+
this.send({ flush: true });
|
|
320
|
+
this.pendingFlushes++;
|
|
321
|
+
this.unflushed = false;
|
|
322
|
+
this.log("Flushed text");
|
|
323
|
+
}
|
|
324
|
+
/** Ends the utterance once its text is over and all its audio came */
|
|
325
|
+
checkEnded() {
|
|
326
|
+
if (!this.isProcessing || !this.textEnded) return;
|
|
327
|
+
if (this.unflushed || this.pendingFlushes > 0) return;
|
|
328
|
+
this.log("Audio ended");
|
|
329
|
+
this.isProcessing = false;
|
|
330
|
+
this.textSent = "";
|
|
331
|
+
}
|
|
332
|
+
send(message) {
|
|
333
|
+
if (this.socket?.readyState !== import_ws2.default.OPEN) return;
|
|
334
|
+
this.socket.send(JSON.stringify(message));
|
|
335
|
+
}
|
|
336
|
+
reconnect() {
|
|
337
|
+
if (this.destroyed) return;
|
|
338
|
+
this.retryCount++;
|
|
339
|
+
if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
|
|
340
|
+
this.log("Max retries reached, giving up");
|
|
341
|
+
const unspoken = this.isProcessing ? this.textSent + this.textBuffer : "";
|
|
342
|
+
this.host.failed(unspoken.trim() ? [unspoken] : []);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
this.initPromise = new Promise((resolve) => {
|
|
346
|
+
this.log("Reconnecting...");
|
|
347
|
+
this.reconnectTimeout = setTimeout(() => {
|
|
348
|
+
this.reconnectTimeout = void 0;
|
|
349
|
+
this.initWS().then(() => {
|
|
350
|
+
if (this.isProcessing && this.textSent.trim()) {
|
|
351
|
+
this.log("Sending text again");
|
|
352
|
+
const text = this.textSent;
|
|
353
|
+
this.textSent = "";
|
|
354
|
+
this.unflushed = false;
|
|
355
|
+
this.pendingFlushes = 0;
|
|
356
|
+
this.sendText(text);
|
|
357
|
+
if (this.textEnded) this.flush();
|
|
358
|
+
}
|
|
359
|
+
}).then(resolve).catch((error) => {
|
|
360
|
+
this.log("Reconnection error:", error);
|
|
361
|
+
this.reconnect();
|
|
362
|
+
});
|
|
363
|
+
}, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
log(...message) {
|
|
367
|
+
this.host.log(...message);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
// src/SpeechWebSocket.ts
|
|
372
|
+
var import_ws3 = __toESM(require("ws"));
|
|
373
|
+
var WS_INACTIVITY_TIMEOUT = 180;
|
|
374
|
+
var DEFAULT_CONNECTION_TIMEOUT2 = 5e3;
|
|
375
|
+
var DEFAULT_RETRY_DELAY2 = 1e3;
|
|
376
|
+
var DEFAULT_MAX_RETRY2 = 3;
|
|
377
|
+
var SpeechWebSocket = class {
|
|
378
|
+
constructor(options, host) {
|
|
55
379
|
this.options = options;
|
|
380
|
+
this.host = host;
|
|
56
381
|
this.isProcessing = false;
|
|
57
382
|
this.textEnded = false;
|
|
58
383
|
// Whether the text stream has ended
|
|
@@ -62,6 +387,7 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
62
387
|
// Buffer of text to send to ElevenLabs
|
|
63
388
|
this.receivedAudioText = "";
|
|
64
389
|
this.retryCount = 0;
|
|
390
|
+
this.destroyed = false;
|
|
65
391
|
this.canceled = false;
|
|
66
392
|
// Bumped by every speak() and every cancel(), so a stream that starts late
|
|
67
393
|
// can tell whether it is still the one that should be heard.
|
|
@@ -125,26 +451,27 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
125
451
|
this.textSent = "";
|
|
126
452
|
this.receivedAudioText = "";
|
|
127
453
|
this.isProcessing = false;
|
|
128
|
-
if (this.socket?.readyState ===
|
|
454
|
+
if (this.socket?.readyState === import_ws3.default.OPEN) {
|
|
129
455
|
this.socket.send(JSON.stringify({ text: " ", flush: true }));
|
|
130
456
|
}
|
|
131
457
|
}
|
|
132
458
|
destroy() {
|
|
459
|
+
this.destroyed = true;
|
|
133
460
|
if (this.reconnectTimeout) {
|
|
134
461
|
clearTimeout(this.reconnectTimeout);
|
|
135
462
|
this.reconnectTimeout = void 0;
|
|
136
463
|
}
|
|
464
|
+
if (this.connectionTimeout) {
|
|
465
|
+
clearTimeout(this.connectionTimeout);
|
|
466
|
+
this.connectionTimeout = void 0;
|
|
467
|
+
}
|
|
137
468
|
if (this.keepAliveInterval) {
|
|
138
469
|
clearInterval(this.keepAliveInterval);
|
|
139
470
|
this.keepAliveInterval = void 0;
|
|
140
471
|
}
|
|
141
|
-
this.socket
|
|
142
|
-
if (this.socket?.readyState === import_ws.default.OPEN) {
|
|
143
|
-
this.socket?.close(1e3);
|
|
144
|
-
}
|
|
472
|
+
if (this.socket) closeSocket(this.socket);
|
|
145
473
|
this.socket = void 0;
|
|
146
474
|
this.isProcessing = false;
|
|
147
|
-
super.destroy();
|
|
148
475
|
}
|
|
149
476
|
initWS() {
|
|
150
477
|
return new Promise((resolve, reject) => {
|
|
@@ -155,15 +482,15 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
155
482
|
this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT
|
|
156
483
|
);
|
|
157
484
|
params.append("inactivity_timeout", WS_INACTIVITY_TIMEOUT.toString());
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
JSON.stringify(
|
|
161
|
-
|
|
485
|
+
const voiceSettings = toWireVoiceSettings(this.options.voiceSettings);
|
|
486
|
+
if (voiceSettings) {
|
|
487
|
+
params.append("voice_settings", JSON.stringify(voiceSettings));
|
|
488
|
+
}
|
|
162
489
|
if (this.options.language) {
|
|
163
490
|
params.append("language_code", this.options.language);
|
|
164
491
|
}
|
|
165
492
|
const uri = `wss://api.elevenlabs.io/v1/text-to-speech/${this.options.voiceId}/stream-input?${params.toString()}`;
|
|
166
|
-
const socket = new
|
|
493
|
+
const socket = new import_ws3.default(uri, {
|
|
167
494
|
headers: {
|
|
168
495
|
"xi-api-key": this.options.apiKey
|
|
169
496
|
}
|
|
@@ -171,25 +498,26 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
171
498
|
this.socket = socket;
|
|
172
499
|
const timeout = setTimeout(() => {
|
|
173
500
|
this.log("Connection timeout");
|
|
174
|
-
socket
|
|
175
|
-
socket.close();
|
|
501
|
+
closeSocket(socket);
|
|
176
502
|
this.socket = void 0;
|
|
177
503
|
reject(new Error("WebSocket connection timeout"));
|
|
178
|
-
}, this.options.connectionTimeout ??
|
|
504
|
+
}, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT2);
|
|
505
|
+
this.connectionTimeout = timeout;
|
|
179
506
|
socket.addEventListener("open", () => {
|
|
180
507
|
clearTimeout(timeout);
|
|
181
508
|
this.log("Connection opened");
|
|
182
509
|
this.socket?.send(
|
|
183
510
|
JSON.stringify({
|
|
184
511
|
text: " ",
|
|
185
|
-
voice_settings:
|
|
512
|
+
voice_settings: voiceSettings
|
|
186
513
|
})
|
|
187
514
|
);
|
|
188
515
|
this.keepAliveInterval = setInterval(
|
|
189
516
|
() => {
|
|
190
|
-
if (this.socket?.readyState ===
|
|
517
|
+
if (this.socket?.readyState === import_ws3.default.OPEN) {
|
|
191
518
|
this.log("Sending keep-alive message");
|
|
192
519
|
this.socket.send(JSON.stringify({ text: " " }));
|
|
520
|
+
this.retryCount = 0;
|
|
193
521
|
}
|
|
194
522
|
},
|
|
195
523
|
(WS_INACTIVITY_TIMEOUT - 1) * 1e3
|
|
@@ -234,11 +562,12 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
234
562
|
this.textSent = "";
|
|
235
563
|
}
|
|
236
564
|
if ("error" in message) {
|
|
237
|
-
|
|
565
|
+
console.error("[ElevenLabsTTS] Error:", message.error, message.message);
|
|
238
566
|
}
|
|
239
567
|
}
|
|
240
568
|
processAudioMessage(message) {
|
|
241
569
|
const chunk = Buffer.from(message.audio, "base64");
|
|
570
|
+
this.retryCount = 0;
|
|
242
571
|
if (message.alignment) {
|
|
243
572
|
const chunkText = message.alignment.chars.join("");
|
|
244
573
|
const receivedAudioText = this.receivedAudioText + chunkText;
|
|
@@ -250,7 +579,7 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
250
579
|
this.receivedAudioText = receivedAudioText;
|
|
251
580
|
this.fixIsFinal(message);
|
|
252
581
|
}
|
|
253
|
-
this.
|
|
582
|
+
this.host.audio(chunk);
|
|
254
583
|
}
|
|
255
584
|
fixIsFinal(message) {
|
|
256
585
|
if (message.isFinal || !this.textEnded) return;
|
|
@@ -263,10 +592,11 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
263
592
|
this.log(`Sent transcript: "${text}"`);
|
|
264
593
|
}
|
|
265
594
|
reconnect() {
|
|
595
|
+
if (this.destroyed) return;
|
|
266
596
|
this.retryCount++;
|
|
267
|
-
if (this.retryCount > (this.options.maxRetry ??
|
|
597
|
+
if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY2)) {
|
|
268
598
|
this.log("Max retries reached, giving up");
|
|
269
|
-
this.
|
|
599
|
+
this.host.failed(this.textSent.trim() ? [this.textSent] : []);
|
|
270
600
|
return;
|
|
271
601
|
}
|
|
272
602
|
this.initPromise = new Promise((resolve) => {
|
|
@@ -274,7 +604,6 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
274
604
|
this.reconnectTimeout = setTimeout(() => {
|
|
275
605
|
this.reconnectTimeout = void 0;
|
|
276
606
|
this.initWS().then(() => {
|
|
277
|
-
this.retryCount = 0;
|
|
278
607
|
if (this.textSent.length > 0) {
|
|
279
608
|
this.log("Sending text chunks again");
|
|
280
609
|
this.sendTranscript(this.textSent);
|
|
@@ -283,14 +612,50 @@ var ElevenLabsTTS = class extends import_server.TTS {
|
|
|
283
612
|
this.log("Reconnection error:", error);
|
|
284
613
|
this.reconnect();
|
|
285
614
|
});
|
|
286
|
-
}, this.options.retryDelay ??
|
|
615
|
+
}, this.options.retryDelay ?? DEFAULT_RETRY_DELAY2);
|
|
287
616
|
});
|
|
288
617
|
}
|
|
618
|
+
log(...message) {
|
|
619
|
+
this.host.log(...message);
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
function toWireVoiceSettings(settings) {
|
|
623
|
+
if (!settings) return void 0;
|
|
624
|
+
const { similarityBoost, useSpeakerBoost, ...rest } = settings;
|
|
625
|
+
return {
|
|
626
|
+
similarity_boost: similarityBoost,
|
|
627
|
+
use_speaker_boost: useSpeakerBoost,
|
|
628
|
+
...rest
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// src/ElevenLabsTTS.ts
|
|
633
|
+
var ElevenLabsTTS = class extends import_server.TTS {
|
|
634
|
+
constructor(options) {
|
|
635
|
+
super();
|
|
636
|
+
const host = {
|
|
637
|
+
audio: (chunk) => this.emit("Audio", chunk),
|
|
638
|
+
failed: (texts) => this.emit("Failed", texts),
|
|
639
|
+
log: (...message) => this.log(...message)
|
|
640
|
+
};
|
|
641
|
+
this.webSocket = isDialogueModel(options.modelId ?? DEFAULT_MODEL_ID) ? new DialogueWebSocket(options, host) : new SpeechWebSocket(options, host);
|
|
642
|
+
}
|
|
643
|
+
speak(textStream) {
|
|
644
|
+
this.webSocket.speak(textStream);
|
|
645
|
+
}
|
|
646
|
+
cancel() {
|
|
647
|
+
this.webSocket.cancel();
|
|
648
|
+
}
|
|
649
|
+
destroy() {
|
|
650
|
+
this.webSocket.destroy();
|
|
651
|
+
super.destroy();
|
|
652
|
+
}
|
|
289
653
|
};
|
|
290
654
|
// Annotate the CommonJS export names for ESM import in node:
|
|
291
655
|
0 && (module.exports = {
|
|
292
656
|
DEFAULT_MODEL_ID,
|
|
293
657
|
DEFAULT_OUTPUT_FORMAT,
|
|
294
|
-
ElevenLabsTTS
|
|
658
|
+
ElevenLabsTTS,
|
|
659
|
+
isDialogueModel
|
|
295
660
|
});
|
|
296
661
|
//# sourceMappingURL=index.js.map
|