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