@micdrop/gradium 1.1.2 → 1.2.0

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/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -42,18 +32,6 @@ module.exports = __toCommonJS(index_exports);
42
32
 
43
33
  // src/GradiumSTT.ts
44
34
  var import_server = require("@micdrop/server");
45
- var import_ws2 = __toESM(require("ws"));
46
-
47
- // src/closeSocket.ts
48
- var import_ws = __toESM(require("ws"));
49
- function closeSocket(socket) {
50
- socket.removeAllListeners();
51
- socket.on("error", () => {
52
- });
53
- if (socket.readyState === import_ws.default.OPEN || socket.readyState === import_ws.default.CONNECTING) {
54
- socket.close(1e3);
55
- }
56
- }
57
35
 
58
36
  // src/types.ts
59
37
  var DEFAULT_MODEL_NAME = "default";
@@ -63,124 +41,164 @@ var DEFAULT_REGION = "eu";
63
41
  var DEFAULT_LANGUAGE = "any";
64
42
 
65
43
  // src/GradiumSTT.ts
66
- var DEFAULT_CONNECTION_TIMEOUT = 5e3;
67
44
  var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
68
- var DEFAULT_RETRY_DELAY = 1e3;
69
- var DEFAULT_MAX_RETRY = 3;
70
45
  var FRAME_DURATION_MS = 80;
71
46
  var DEFAULT_DELAY_IN_FRAMES = 10;
72
47
  var GradiumSTT = class extends import_server.STT {
48
+ // Given by the server when ready
73
49
  constructor(options) {
74
50
  super();
75
51
  this.options = options;
76
- this.audioChunksPending = [];
77
- // Store audio chunks to send them again if reconnecting
78
- this.transcript = "";
79
- // Accumulated text segments for the current utterance
52
+ // Utterances in the order their audio went out. The server answers in that
53
+ // order too, so every text segment belongs to the first one not flushed yet
54
+ this.utterances = [];
80
55
  this.flushId = 0;
81
56
  this.delayInFrames = DEFAULT_DELAY_IN_FRAMES;
82
- // Given by the server when ready
83
- this.retryCount = 0;
84
- this.destroyed = false;
85
- this.initPromise = this.connect();
57
+ /** Sends the setup, and waits for the server to accept it */
58
+ this.handshake = {
59
+ start: (socket) => {
60
+ socket.send(
61
+ JSON.stringify({
62
+ type: "setup",
63
+ model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
64
+ input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,
65
+ json_config: this.buildJsonConfig()
66
+ })
67
+ );
68
+ this.log("Sent setup");
69
+ },
70
+ read: (data) => {
71
+ let message;
72
+ try {
73
+ message = JSON.parse(data.toString());
74
+ } catch {
75
+ this.log("Error parsing message", data);
76
+ return false;
77
+ }
78
+ if (message.type === "error") {
79
+ throw new Error(`Setup refused: ${message.message}`);
80
+ }
81
+ if (message.type !== "ready") return false;
82
+ this.log("Server ready");
83
+ if (typeof message.delay_in_frames === "number") {
84
+ this.delayInFrames = message.delay_in_frames;
85
+ }
86
+ return true;
87
+ }
88
+ };
89
+ // A new connection knows nothing of the utterances under way: the audio read
90
+ // before the first connection opened, or lost with the previous one, goes out
91
+ // again whole, and the server transcribes it from the start
92
+ this.onOpen = () => {
93
+ this.utterances = this.utterances.filter((utterance) => !utterance.done);
94
+ if (this.utterances.length === 0) return;
95
+ this.log("Sending audio chunks again");
96
+ for (const utterance of this.utterances) {
97
+ utterance.transcript = "";
98
+ if (utterance.flushId === void 0) {
99
+ utterance.audio.forEach((chunk) => this.sendAudioChunk(chunk));
100
+ } else {
101
+ utterance.audio.slice(0, -1).forEach((chunk) => this.sendAudioChunk(chunk));
102
+ this.sendEnd(utterance);
103
+ }
104
+ }
105
+ };
106
+ this.onMessage = (data) => {
107
+ let message;
108
+ try {
109
+ message = JSON.parse(data.toString());
110
+ } catch {
111
+ this.log("Error parsing message", data);
112
+ return;
113
+ }
114
+ switch (message.type) {
115
+ case "text": {
116
+ const utterance = this.utterances[0];
117
+ if (typeof message.text === "string" && utterance) {
118
+ utterance.transcript += (utterance.transcript ? " " : "") + message.text;
119
+ this.log(`Received text segment: "${message.text}"`);
120
+ }
121
+ break;
122
+ }
123
+ case "flushed": {
124
+ const index = this.utterances.findIndex(
125
+ (utterance) => utterance.flushId === message.flush_id
126
+ );
127
+ if (index === -1) break;
128
+ const flushed = this.utterances.splice(0, index + 1);
129
+ flushed.forEach((utterance) => this.emitTranscript(utterance));
130
+ break;
131
+ }
132
+ case "end_text":
133
+ break;
134
+ case "step":
135
+ break;
136
+ case "end_of_stream":
137
+ this.log("End of stream");
138
+ break;
139
+ case "error":
140
+ this.log("Error:", message.message, message.code);
141
+ break;
142
+ }
143
+ };
144
+ this.onFailed = () => {
145
+ const pending = this.utterances.filter((utterance) => !utterance.done);
146
+ this.utterances.forEach((utterance) => clearTimeout(utterance.timeout));
147
+ this.utterances = [];
148
+ this.emit(
149
+ "Failed",
150
+ pending.flatMap((utterance) => utterance.audio)
151
+ );
152
+ };
153
+ const region = options.region ?? DEFAULT_REGION;
154
+ this.socket = new import_server.ReconnectingSocket({
155
+ url: `wss://${region}.api.gradium.ai/api/speech/asr`,
156
+ clientOptions: { headers: { "x-api-key": options.apiKey } },
157
+ connectionTimeout: options.connectionTimeout,
158
+ retryDelay: options.retryDelay,
159
+ maxRetry: options.maxRetry,
160
+ // The server opens the connection before checking the setup, so the
161
+ // connection only counts as open once it answers ready
162
+ handshake: this.handshake,
163
+ log: (...message) => this.log(...message)
164
+ });
165
+ this.socket.on("Open", this.onOpen);
166
+ this.socket.on("Message", this.onMessage);
167
+ this.socket.on("Failed", this.onFailed);
168
+ this.socket.connect();
86
169
  }
87
170
  transcribe(audioStream) {
88
- let started = false;
89
- audioStream.on("data", async (chunk) => {
90
- if (!started) {
91
- started = true;
92
- this.transcript = "";
171
+ let utterance;
172
+ audioStream.on("data", (chunk) => {
173
+ if (!utterance) {
174
+ utterance = { audio: [], transcript: "", done: false };
175
+ this.utterances.push(utterance);
176
+ this.socket.connect();
177
+ }
178
+ if (utterance.done) return;
179
+ utterance.audio.push(chunk);
180
+ if (this.sendAudioChunk(chunk)) {
181
+ this.log(`Sent audio chunk (${chunk.byteLength} bytes)`);
93
182
  }
94
- this.audioChunksPending.push(chunk);
95
- await this.initPromise;
96
- this.sendAudioChunk(chunk);
97
- this.log(`Sent audio chunk (${chunk.byteLength} bytes)`);
98
183
  });
99
- audioStream.on("end", async () => {
100
- await this.initPromise;
101
- if (this.audioChunksPending.length === 0) return;
102
- this.sendTrailingSilence();
103
- const flushId = ++this.flushId;
104
- this.sendFlush(flushId);
105
- this.transcriptionTimeout = setTimeout(() => {
106
- this.transcriptionTimeout = void 0;
184
+ audioStream.on("end", () => {
185
+ if (!utterance || utterance.done) return;
186
+ const current = utterance;
187
+ current.audio.push(this.trailingSilence());
188
+ current.flushId = ++this.flushId;
189
+ if (this.socket.isOpen) this.sendEnd(current);
190
+ current.timeout = setTimeout(() => {
191
+ current.timeout = void 0;
107
192
  this.log("Transcription timeout");
108
- this.emitTranscript();
193
+ this.emitTranscript(current);
109
194
  }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT);
110
195
  });
111
196
  }
112
197
  destroy() {
113
198
  super.destroy();
114
- this.destroyed = true;
115
- if (this.reconnectTimeout) {
116
- clearTimeout(this.reconnectTimeout);
117
- this.reconnectTimeout = void 0;
118
- }
119
- if (this.transcriptionTimeout) {
120
- clearTimeout(this.transcriptionTimeout);
121
- this.transcriptionTimeout = void 0;
122
- }
123
- if (this.connectionTimeout) {
124
- clearTimeout(this.connectionTimeout);
125
- this.connectionTimeout = void 0;
126
- }
127
- if (this.socket) closeSocket(this.socket);
128
- this.socket = void 0;
129
- }
130
- connect() {
131
- return this.initWS().catch((error) => {
132
- console.error("[GradiumSTT] Connection error:", error);
133
- this.reconnect();
134
- });
135
- }
136
- getEndpoint() {
137
- const region = this.options.region ?? DEFAULT_REGION;
138
- return `wss://${region}.api.gradium.ai/api/speech/asr`;
139
- }
140
- async initWS() {
141
- return new Promise((resolve, reject) => {
142
- const socket = new import_ws2.default(this.getEndpoint(), {
143
- headers: {
144
- "x-api-key": this.options.apiKey
145
- }
146
- });
147
- this.socket = socket;
148
- const timeout = setTimeout(() => {
149
- this.log("Connection timeout");
150
- closeSocket(socket);
151
- this.socket = void 0;
152
- reject(new Error("WebSocket connection timeout"));
153
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
154
- this.connectionTimeout = timeout;
155
- socket.addEventListener("open", () => {
156
- clearTimeout(timeout);
157
- this.sendSetup();
158
- this.log("Connection opened");
159
- resolve();
160
- });
161
- socket.addEventListener("error", (error) => {
162
- clearTimeout(timeout);
163
- this.log("WebSocket error:", error);
164
- reject(new Error("WebSocket connection error"));
165
- });
166
- socket.addEventListener("close", ({ code, reason }) => {
167
- clearTimeout(timeout);
168
- this.socket?.removeAllListeners();
169
- this.socket = void 0;
170
- if (code !== 1e3) {
171
- this.reconnect();
172
- } else {
173
- this.log("Connection closed", { code, reason });
174
- }
175
- });
176
- socket.addEventListener("message", (event) => {
177
- try {
178
- this.handleMessage(JSON.parse(event.data.toString()));
179
- } catch {
180
- this.log("Error parsing message", event.data);
181
- }
182
- });
183
- });
199
+ this.socket.destroy();
200
+ this.utterances.forEach((utterance) => clearTimeout(utterance.timeout));
201
+ this.utterances = [];
184
202
  }
185
203
  buildJsonConfig() {
186
204
  const config = { ...this.options.jsonConfig };
@@ -189,19 +207,8 @@ var GradiumSTT = class extends import_server.STT {
189
207
  }
190
208
  return config;
191
209
  }
192
- sendSetup() {
193
- this.socket?.send(
194
- JSON.stringify({
195
- type: "setup",
196
- model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,
197
- input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,
198
- json_config: this.buildJsonConfig()
199
- })
200
- );
201
- this.log("Sent setup");
202
- }
203
210
  sendAudioChunk(chunk) {
204
- this.socket?.send(
211
+ return this.socket.send(
205
212
  JSON.stringify({
206
213
  type: "audio",
207
214
  audio: chunk.toString("base64")
@@ -209,98 +216,40 @@ var GradiumSTT = class extends import_server.STT {
209
216
  );
210
217
  }
211
218
  /**
212
- * Follows the speech with as much silence as the transcription lags behind.
219
+ * Silence as long as the transcription lags behind, to follow the speech.
213
220
  *
214
221
  * The model writes each word a few frames after hearing it, and a flush only
215
222
  * processes the audio already sent. With nothing after the last word, the
216
223
  * server confirms the flush before writing that word, which then opens the
217
224
  * next transcript.
218
225
  */
219
- sendTrailingSilence() {
226
+ trailingSilence() {
220
227
  const sampleRate = pcmSampleRate(
221
228
  this.options.inputFormat ?? DEFAULT_INPUT_FORMAT
222
229
  );
223
- if (!sampleRate) return;
230
+ if (!sampleRate) return Buffer.alloc(0);
224
231
  const durationMs = this.delayInFrames * FRAME_DURATION_MS;
225
232
  const samples = Math.round(sampleRate * durationMs / 1e3);
226
- const silence = Buffer.alloc(samples * 2);
227
- this.audioChunksPending.push(silence);
228
- this.sendAudioChunk(silence);
229
- this.log(`Sent ${durationMs}ms of silence`);
233
+ return Buffer.alloc(samples * 2);
230
234
  }
231
- sendFlush(flushId) {
232
- this.socket?.send(JSON.stringify({ type: "flush", flush_id: flushId }));
233
- this.log(`Sent flush (flush_id=${flushId})`);
235
+ /** Sends the trailing silence, already the last audio chunk, and the flush */
236
+ sendEnd(utterance) {
237
+ const silence = utterance.audio[utterance.audio.length - 1];
238
+ if (silence.length) this.sendAudioChunk(silence);
239
+ this.socket.send(
240
+ JSON.stringify({ type: "flush", flush_id: utterance.flushId })
241
+ );
242
+ this.log(`Sent flush (flush_id=${utterance.flushId})`);
234
243
  }
235
- emitTranscript() {
236
- const transcript = this.transcript.replace(/\s+/g, " ").trim();
244
+ emitTranscript(utterance) {
245
+ if (utterance.done) return;
246
+ utterance.done = true;
247
+ clearTimeout(utterance.timeout);
248
+ utterance.timeout = void 0;
249
+ utterance.audio = [];
250
+ const transcript = utterance.transcript.replace(/\s+/g, " ").trim();
237
251
  this.log(`Received transcript: "${transcript}"`);
238
252
  this.emit("Transcript", transcript);
239
- this.transcript = "";
240
- this.audioChunksPending.length = 0;
241
- if (this.transcriptionTimeout) {
242
- clearTimeout(this.transcriptionTimeout);
243
- this.transcriptionTimeout = void 0;
244
- }
245
- }
246
- handleMessage(message) {
247
- switch (message.type) {
248
- case "ready":
249
- this.log("Server ready");
250
- this.retryCount = 0;
251
- if (typeof message.delay_in_frames === "number") {
252
- this.delayInFrames = message.delay_in_frames;
253
- }
254
- break;
255
- case "text":
256
- if (typeof message.text === "string") {
257
- this.transcript += (this.transcript ? " " : "") + message.text;
258
- this.log(`Received text segment: "${message.text}"`);
259
- }
260
- break;
261
- case "end_text":
262
- break;
263
- case "flushed":
264
- if (message.flush_id === this.flushId) {
265
- this.emitTranscript();
266
- }
267
- break;
268
- case "step":
269
- break;
270
- case "end_of_stream":
271
- this.log("End of stream");
272
- break;
273
- case "error":
274
- this.log("Error:", message.message, message.code);
275
- break;
276
- }
277
- }
278
- reconnect() {
279
- if (this.destroyed) return;
280
- this.retryCount++;
281
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
282
- this.log("Max retries reached, giving up");
283
- this.emit("Failed", this.audioChunksPending);
284
- return;
285
- }
286
- this.initPromise = new Promise((resolve) => {
287
- this.log("Reconnecting...");
288
- this.reconnectTimeout = setTimeout(() => {
289
- this.reconnectTimeout = void 0;
290
- this.initWS().then(() => {
291
- if (this.audioChunksPending.length > 0) {
292
- this.log("Sending audio chunks again");
293
- this.transcript = "";
294
- this.audioChunksPending.forEach(
295
- (chunk) => this.sendAudioChunk(chunk)
296
- );
297
- }
298
- }).then(resolve).catch((error) => {
299
- this.log("Reconnection error:", error);
300
- this.reconnect();
301
- });
302
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
303
- });
304
253
  }
305
254
  };
306
255
  function pcmSampleRate(format) {
@@ -311,201 +260,138 @@ function pcmSampleRate(format) {
311
260
 
312
261
  // src/GradiumTTS.ts
313
262
  var import_server2 = require("@micdrop/server");
314
- var import_ws3 = __toESM(require("ws"));
315
- var DEFAULT_CONNECTION_TIMEOUT2 = 5e3;
316
- var DEFAULT_RETRY_DELAY2 = 1e3;
317
- var DEFAULT_MAX_RETRY2 = 3;
318
263
  var GradiumTTS = class extends import_server2.TTS {
319
264
  constructor(options) {
320
265
  super();
321
266
  this.options = options;
267
+ // Numbers the audio requests, and only moves when there is something to say
322
268
  this.counter = 0;
323
269
  // Bumped by every speak() and every cancel(), so a request claimed late can
324
- // tell whether it is still the one that should be heard. Kept apart from
325
- // `counter`, which numbers the audio requests and must only move when there
326
- // is something to synthesize.
270
+ // tell whether it is still the one that should be heard
327
271
  this.generation = 0;
328
- this.isProcessing = false;
329
- this.textSent = "";
330
- this.textBuffer = "";
331
- this.retryCount = 0;
332
- this.destroyed = false;
333
- this.connect();
334
- }
335
- connect() {
336
- if (this.destroyed) return;
337
- this.initPromise = this.initWS().catch((error) => {
338
- console.error("[GradiumTTS] Connection error:", error);
339
- this.reconnect();
272
+ // A new connection has no session for the request under way: its setup and
273
+ // its words go out again, the setup first, with the same client_req_id
274
+ this.onOpen = () => {
275
+ const request = this.request;
276
+ if (!request) return;
277
+ this.log("Sending the request again");
278
+ this.sendSetup(request.id);
279
+ if (request.words) this.sendTranscript(request.words, request.id);
280
+ if (request.ended) this.sendEnd(request);
281
+ };
282
+ this.onMessage = (data) => {
283
+ let message;
284
+ try {
285
+ message = JSON.parse(data.toString());
286
+ } catch {
287
+ this.log("Error parsing message", data);
288
+ return;
289
+ }
290
+ switch (message.type) {
291
+ case "ready":
292
+ this.log("Server ready", message.request_id ?? "");
293
+ break;
294
+ case "audio":
295
+ this.processAudioMessage(message);
296
+ break;
297
+ case "end_of_stream":
298
+ if (!this.request || message.client_req_id !== void 0 && message.client_req_id !== this.request.id)
299
+ return;
300
+ this.log("Audio ended");
301
+ this.request = void 0;
302
+ break;
303
+ case "error":
304
+ this.log("Error:", message.message, message.code);
305
+ if (!this.request) break;
306
+ this.onFailed();
307
+ this.socket.restart();
308
+ break;
309
+ }
310
+ };
311
+ this.onFailed = () => {
312
+ const request = this.request;
313
+ this.request = void 0;
314
+ const text = request ? request.words + request.partial : "";
315
+ this.emit("Failed", text ? [text] : []);
316
+ };
317
+ const region = options.region ?? DEFAULT_REGION;
318
+ this.socket = new import_server2.ReconnectingSocket({
319
+ url: `wss://${region}.api.gradium.ai/api/speech/tts`,
320
+ clientOptions: { headers: { "x-api-key": options.apiKey } },
321
+ connectionTimeout: options.connectionTimeout,
322
+ retryDelay: options.retryDelay,
323
+ maxRetry: options.maxRetry,
324
+ log: (...message) => this.log(...message)
340
325
  });
326
+ this.socket.on("Open", this.onOpen);
327
+ this.socket.on("Message", this.onMessage);
328
+ this.socket.on("Failed", this.onFailed);
329
+ this.socket.connect();
341
330
  }
342
331
  speak(textStream) {
343
332
  const generation = ++this.generation;
344
- let counter = 0;
345
- let clientReqId = "";
333
+ let request;
346
334
  const claimRequest = () => {
347
- if (counter) return true;
348
- if (this.generation !== generation) return false;
349
- this.counter++;
350
- counter = this.counter;
351
- clientReqId = counter.toString();
352
- this.isProcessing = true;
353
- this.textSent = "";
354
- this.textBuffer = "";
355
- this.initPromise.then(() => {
356
- if (counter !== this.counter) return;
357
- this.sendSetup(clientReqId);
358
- });
359
- return true;
335
+ if (request) return request;
336
+ if (this.generation !== generation) return;
337
+ request = {
338
+ id: (++this.counter).toString(),
339
+ words: "",
340
+ partial: "",
341
+ ended: false
342
+ };
343
+ this.request = request;
344
+ this.sendSetup(request.id);
345
+ this.socket.connect();
346
+ return request;
360
347
  };
361
- textStream.on("data", async (chunk) => {
362
- if (!claimRequest()) return;
363
- if (counter !== this.counter) return;
348
+ textStream.on("data", (chunk) => {
349
+ const request2 = claimRequest();
350
+ if (!request2 || this.request !== request2) return;
364
351
  const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
365
- this.textSent += text;
366
- await this.initPromise;
367
- if (counter !== this.counter) return;
368
352
  const spaceIndex = text.lastIndexOf(" ");
369
353
  if (spaceIndex === -1) {
370
- this.textBuffer += text;
371
- } else {
372
- this.sendTranscript(
373
- this.textBuffer + text.slice(0, spaceIndex + 1),
374
- clientReqId
375
- );
376
- this.textBuffer = text.slice(spaceIndex + 1);
354
+ request2.partial += text;
355
+ return;
377
356
  }
357
+ const words = request2.partial + text.slice(0, spaceIndex + 1);
358
+ request2.partial = text.slice(spaceIndex + 1);
359
+ request2.words += words;
360
+ this.sendTranscript(words, request2.id);
378
361
  });
379
362
  textStream.on("error", (error) => {
380
- if (!counter) return;
381
- this.log("Error in text stream, ending audio stream", error);
382
- this.isProcessing = false;
363
+ if (!request || this.request !== request) return;
364
+ this.log("Error in text stream, cancelling the audio", error);
365
+ this.stopRequest();
383
366
  });
384
- textStream.on("end", async () => {
385
- if (!counter || counter !== this.counter) return;
386
- await this.initPromise;
387
- if (counter !== this.counter) return;
388
- if (this.textBuffer.trim()) {
389
- this.sendTranscript(this.textBuffer, clientReqId);
390
- this.textBuffer = "";
391
- }
392
- this.socket?.send(
393
- JSON.stringify({
394
- type: "end_of_stream",
395
- client_req_id: clientReqId
396
- })
397
- );
398
- this.log("Sent end_of_stream");
367
+ textStream.on("end", () => {
368
+ if (!request || this.request !== request) return;
369
+ request.ended = true;
370
+ this.sendEnd(request);
399
371
  });
400
372
  }
373
+ /**
374
+ * Stops the current utterance. Gradium can only stop a synthesis by closing
375
+ * its connection, so a new one opens right away.
376
+ */
401
377
  cancel() {
402
378
  this.generation++;
403
- if (!this.isProcessing) return;
379
+ if (!this.request) return;
404
380
  this.log("Cancel");
405
- this.isProcessing = false;
406
- this.textSent = "";
407
- this.textBuffer = "";
408
- this.counter++;
409
- if (this.reconnectTimeout) {
410
- clearTimeout(this.reconnectTimeout);
411
- this.reconnectTimeout = void 0;
412
- }
413
- if (this.connectionTimeout) {
414
- clearTimeout(this.connectionTimeout);
415
- this.connectionTimeout = void 0;
416
- }
417
- const socket = this.socket;
418
- this.socket = void 0;
419
- if (socket) closeSocket(socket);
420
- this.connect();
381
+ this.stopRequest();
421
382
  }
422
383
  destroy() {
423
- this.destroyed = true;
384
+ this.socket.destroy();
424
385
  super.destroy();
425
- if (this.reconnectTimeout) {
426
- clearTimeout(this.reconnectTimeout);
427
- this.reconnectTimeout = void 0;
428
- }
429
- if (this.connectionTimeout) {
430
- clearTimeout(this.connectionTimeout);
431
- this.connectionTimeout = void 0;
432
- }
433
- if (this.socket) closeSocket(this.socket);
434
- this.socket = void 0;
435
- this.isProcessing = false;
436
- }
437
- getEndpoint() {
438
- const region = this.options.region ?? DEFAULT_REGION;
439
- return `wss://${region}.api.gradium.ai/api/speech/tts`;
386
+ this.request = void 0;
440
387
  }
441
- async initWS() {
442
- return new Promise((resolve, reject) => {
443
- const socket = new import_ws3.default(this.getEndpoint(), {
444
- headers: {
445
- "x-api-key": this.options.apiKey
446
- }
447
- });
448
- this.socket = socket;
449
- const timeout = setTimeout(() => {
450
- this.log("Connection timeout");
451
- closeSocket(socket);
452
- this.socket = void 0;
453
- reject(new Error("WebSocket connection timeout"));
454
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT2);
455
- this.connectionTimeout = timeout;
456
- socket.addEventListener("open", () => {
457
- clearTimeout(timeout);
458
- this.log("Connection opened");
459
- resolve();
460
- });
461
- socket.addEventListener("error", (error) => {
462
- clearTimeout(timeout);
463
- this.log("WebSocket error:", error);
464
- reject(new Error("WebSocket connection error"));
465
- });
466
- socket.addEventListener("close", ({ code, reason }) => {
467
- clearTimeout(timeout);
468
- this.socket?.removeAllListeners();
469
- this.socket = void 0;
470
- if (code !== 1e3) {
471
- this.reconnect();
472
- } else {
473
- this.log("Connection closed", { code, reason });
474
- if (!this.isProcessing) {
475
- this.reconnect();
476
- }
477
- }
478
- });
479
- socket.addEventListener("message", (event) => {
480
- try {
481
- const message = JSON.parse(event.data.toString());
482
- switch (message.type) {
483
- case "ready":
484
- this.log("Server ready", message.request_id ?? "");
485
- this.retryCount = 0;
486
- break;
487
- case "audio":
488
- this.processAudioMessage(message);
489
- break;
490
- case "end_of_stream":
491
- if ("client_req_id" in message && message.client_req_id !== this.counter.toString())
492
- return;
493
- this.log("Audio ended");
494
- this.isProcessing = false;
495
- this.textSent = "";
496
- break;
497
- case "error":
498
- this.log("Error:", message.message, message.code);
499
- break;
500
- }
501
- } catch {
502
- this.log("Error parsing message", event.data);
503
- }
504
- });
505
- });
388
+ /** Forgets the current request, and closes the connection that carries it */
389
+ stopRequest() {
390
+ this.request = void 0;
391
+ this.socket.restart();
506
392
  }
507
393
  sendSetup(clientReqId) {
508
- this.socket?.send(
394
+ const sent = this.socket.send(
509
395
  JSON.stringify({
510
396
  type: "setup",
511
397
  voice_id: this.options.voiceId,
@@ -516,50 +402,37 @@ var GradiumTTS = class extends import_server2.TTS {
516
402
  client_req_id: clientReqId
517
403
  })
518
404
  );
519
- this.log(`Sent setup (client_req_id=${clientReqId})`);
405
+ if (sent) this.log(`Sent setup (client_req_id=${clientReqId})`);
520
406
  }
521
407
  sendTranscript(text, clientReqId) {
522
- this.socket?.send(
408
+ const sent = this.socket.send(
523
409
  JSON.stringify({
524
410
  type: "text",
525
411
  text,
526
412
  client_req_id: clientReqId
527
413
  })
528
414
  );
529
- this.log(`Sent transcript: "${text}"`);
415
+ if (sent) this.log(`Sent transcript: "${text}"`);
416
+ }
417
+ /** Sends the last partial word, then the end of the text */
418
+ sendEnd(request) {
419
+ if (request.partial.trim()) {
420
+ this.sendTranscript(request.partial, request.id);
421
+ }
422
+ const sent = this.socket.send(
423
+ JSON.stringify({
424
+ type: "end_of_stream",
425
+ client_req_id: request.id
426
+ })
427
+ );
428
+ if (sent) this.log("Sent end_of_stream");
530
429
  }
531
430
  processAudioMessage(message) {
532
- if (message.client_req_id !== this.counter.toString()) return;
431
+ if (!this.request || message.client_req_id !== this.request.id) return;
533
432
  const chunk = Buffer.from(message.audio, "base64");
534
433
  this.log(`Received audio chunk (${chunk.length} bytes)`);
535
434
  this.emit("Audio", chunk);
536
435
  }
537
- reconnect() {
538
- if (this.destroyed) return;
539
- this.retryCount++;
540
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY2)) {
541
- this.log("Max retries reached, giving up");
542
- this.emit("Failed", [this.textSent]);
543
- return;
544
- }
545
- this.initPromise = new Promise((resolve) => {
546
- this.log("Reconnecting...");
547
- this.reconnectTimeout = setTimeout(() => {
548
- this.reconnectTimeout = void 0;
549
- this.initWS().then(() => {
550
- if (this.textSent.length > 0) {
551
- const clientReqId = this.counter.toString();
552
- this.log("Sending text chunks again");
553
- this.sendSetup(clientReqId);
554
- this.sendTranscript(this.textSent, clientReqId);
555
- }
556
- }).then(resolve).catch((error) => {
557
- this.log("Reconnection error:", error);
558
- this.reconnect();
559
- });
560
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY2);
561
- });
562
- }
563
436
  };
564
437
  // Annotate the CommonJS export names for ESM import in node:
565
438
  0 && (module.exports = {