@micdrop/cartesia 1.2.0 → 1.3.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/README.md CHANGED
@@ -54,10 +54,10 @@ tts.speak(Readable.from(['Hello! ', 'What can I do for you?']))
54
54
 
55
55
  ### Events
56
56
 
57
- | Event | Payload | Description |
58
- | -------- | ---------- | ------------------------------------------------------------------------ |
59
- | `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, 16 kHz, mono, ready to be played. |
60
- | `Failed` | `string[]` | Synthesis gave up after its retries, with the text that stayed unspoken. |
57
+ | Event | Payload | Description |
58
+ | -------- | ---------- | --------------------------------------------------------------------------------------------------------- |
59
+ | `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, 16 kHz, mono, ready to be played. |
60
+ | `Failed` | `string[]` | Every reconnection attempt failed, with the text that stayed unspoken. The next utterance connects again. |
61
61
 
62
62
  See the [TTS interface](https://micdrop.dev/docs/ai-integration/custom-integrations/custom-tts) for the full contract.
63
63
 
package/dist/index.d.mts CHANGED
@@ -99,29 +99,33 @@ interface CartesiaTTSOptions {
99
99
  /** @deprecated Sonic 3 models read `generationConfig.speed` instead */
100
100
  speed?: 'fast' | 'normal' | 'slow';
101
101
  generationConfig?: CartesiaGenerationConfig;
102
+ /** How long a connection attempt may take, 5000 ms by default */
102
103
  connectionTimeout?: number;
104
+ /** How long to wait before a new attempt, 1000 ms by default */
103
105
  retryDelay?: number;
106
+ /**
107
+ * How many attempts in a row may fail before giving up and emitting
108
+ * `Failed`, 3 by default. The next utterance tries to connect again.
109
+ */
104
110
  maxRetry?: number;
105
111
  }
106
112
  declare class CartesiaTTS extends TTS {
107
113
  private readonly options;
108
- private socket?;
109
- private initPromise;
114
+ private socket;
110
115
  private counter;
111
116
  private generation;
112
- private isProcessing;
113
- private reconnectTimeout?;
114
- private connectionTimeout?;
115
- private textSent;
116
- private retryCount;
117
- private destroyed;
117
+ private context?;
118
118
  constructor(options: CartesiaTTSOptions);
119
119
  speak(textStream: Readable): void;
120
120
  cancel(): void;
121
121
  destroy(): void;
122
+ /** Asks Cartesia to drop the current context, and forgets it */
123
+ private stopContext;
124
+ private sendText;
122
125
  private getConfig;
123
- private initWS;
124
- private reconnect;
126
+ private onOpen;
127
+ private onMessage;
128
+ private onFailed;
125
129
  }
126
130
 
127
131
  export { type CartesiaCancelPayload, type CartesiaChunkResponse, type CartesiaDoneResponse, type CartesiaEmotion, type CartesiaErrorResponse, type CartesiaFlushDoneResponse, type CartesiaGenerationConfig, type CartesiaLanguage, type CartesiaPayload, type CartesiaPhonemeTimestampsResponse, type CartesiaResponse, CartesiaTTS, type CartesiaTTSOptions, type CartesiaTimestampsResponse, type CartesiaTranscriptPayload };
package/dist/index.d.ts CHANGED
@@ -99,29 +99,33 @@ interface CartesiaTTSOptions {
99
99
  /** @deprecated Sonic 3 models read `generationConfig.speed` instead */
100
100
  speed?: 'fast' | 'normal' | 'slow';
101
101
  generationConfig?: CartesiaGenerationConfig;
102
+ /** How long a connection attempt may take, 5000 ms by default */
102
103
  connectionTimeout?: number;
104
+ /** How long to wait before a new attempt, 1000 ms by default */
103
105
  retryDelay?: number;
106
+ /**
107
+ * How many attempts in a row may fail before giving up and emitting
108
+ * `Failed`, 3 by default. The next utterance tries to connect again.
109
+ */
104
110
  maxRetry?: number;
105
111
  }
106
112
  declare class CartesiaTTS extends TTS {
107
113
  private readonly options;
108
- private socket?;
109
- private initPromise;
114
+ private socket;
110
115
  private counter;
111
116
  private generation;
112
- private isProcessing;
113
- private reconnectTimeout?;
114
- private connectionTimeout?;
115
- private textSent;
116
- private retryCount;
117
- private destroyed;
117
+ private context?;
118
118
  constructor(options: CartesiaTTSOptions);
119
119
  speak(textStream: Readable): void;
120
120
  cancel(): void;
121
121
  destroy(): void;
122
+ /** Asks Cartesia to drop the current context, and forgets it */
123
+ private stopContext;
124
+ private sendText;
122
125
  private getConfig;
123
- private initWS;
124
- private reconnect;
126
+ private onOpen;
127
+ private onMessage;
128
+ private onFailed;
125
129
  }
126
130
 
127
131
  export { type CartesiaCancelPayload, type CartesiaChunkResponse, type CartesiaDoneResponse, type CartesiaEmotion, type CartesiaErrorResponse, type CartesiaFlushDoneResponse, type CartesiaGenerationConfig, type CartesiaLanguage, type CartesiaPayload, type CartesiaPhonemeTimestampsResponse, type CartesiaResponse, CartesiaTTS, type CartesiaTTSOptions, type CartesiaTimestampsResponse, type CartesiaTranscriptPayload };
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
@@ -36,108 +26,131 @@ module.exports = __toCommonJS(index_exports);
36
26
 
37
27
  // src/CartesiaTTS.ts
38
28
  var import_server = require("@micdrop/server");
39
- var import_ws = __toESM(require("ws"));
40
- var DEFAULT_CONNECTION_TIMEOUT = 5e3;
41
- var DEFAULT_RETRY_DELAY = 1e3;
42
- var DEFAULT_MAX_RETRY = 3;
43
29
  var CartesiaTTS = class extends import_server.TTS {
44
30
  constructor(options) {
45
31
  super();
46
32
  this.options = options;
33
+ // Numbers the audio contexts, and only moves when there is something to say
47
34
  this.counter = 0;
48
35
  // Bumped by every speak() and every cancel(), so a context claimed late can
49
- // tell whether it is still the one that should be heard. Kept apart from
50
- // `counter`, which numbers the audio contexts and must only move when there
51
- // is something to synthesize.
36
+ // tell whether it is still the one that should be heard
52
37
  this.generation = 0;
53
- this.isProcessing = false;
54
- this.textSent = "";
55
- // Store text chunks to send them again if reconnecting
56
- this.retryCount = 0;
57
- this.destroyed = false;
58
- this.initPromise = this.initWS().catch((error) => {
59
- console.error("[CartesiaTTS] Connection error:", error);
60
- this.reconnect();
38
+ // A new connection knows nothing of the utterance under way: the text read
39
+ // before the first connection opened, or lost with the previous one, goes out
40
+ // again whole
41
+ this.onOpen = () => {
42
+ const context = this.context;
43
+ if (!context) return;
44
+ if (context.text) {
45
+ this.log("Sending text again");
46
+ this.sendText(context.id, context.text, true);
47
+ }
48
+ if (context.ended) this.sendText(context.id, "", false);
49
+ };
50
+ this.onMessage = (data) => {
51
+ let message;
52
+ try {
53
+ message = JSON.parse(data.toString());
54
+ } catch {
55
+ this.log("Error parsing message", data);
56
+ return;
57
+ }
58
+ if (!this.context || this.context.id !== message.context_id) return;
59
+ switch (message.type) {
60
+ case "chunk":
61
+ const chunk = Buffer.from(message.data, "base64");
62
+ this.log(`Received audio chunk (${chunk.length} bytes)`);
63
+ this.emit("Audio", chunk);
64
+ break;
65
+ case "done":
66
+ this.log("Audio ended");
67
+ this.context = void 0;
68
+ break;
69
+ case "error":
70
+ this.log("Error", message.error);
71
+ this.context = void 0;
72
+ break;
73
+ }
74
+ };
75
+ this.onFailed = () => {
76
+ const text = this.context?.text;
77
+ this.context = void 0;
78
+ this.emit("Failed", text ? [text] : []);
79
+ };
80
+ this.socket = new import_server.ReconnectingSocket({
81
+ url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,
82
+ connectionTimeout: options.connectionTimeout,
83
+ retryDelay: options.retryDelay,
84
+ maxRetry: options.maxRetry,
85
+ log: (...message) => this.log(...message)
61
86
  });
87
+ this.socket.on("Open", this.onOpen);
88
+ this.socket.on("Message", this.onMessage);
89
+ this.socket.on("Failed", this.onFailed);
90
+ this.socket.connect();
62
91
  }
63
92
  speak(textStream) {
64
93
  const generation = ++this.generation;
65
- let counter = 0;
66
- let context_id = "";
94
+ let context;
67
95
  const claimContext = () => {
68
- if (counter) return true;
69
- if (this.generation !== generation) return false;
70
- this.counter++;
71
- counter = this.counter;
72
- context_id = counter.toString();
73
- this.isProcessing = true;
74
- this.textSent = "";
75
- return true;
96
+ if (context) return context;
97
+ if (this.generation !== generation) return;
98
+ context = { id: (++this.counter).toString(), text: "", ended: false };
99
+ this.context = context;
100
+ this.socket.connect();
101
+ return context;
76
102
  };
77
- textStream.on("data", async (chunk) => {
78
- if (!claimContext()) return;
79
- if (counter !== this.counter) return;
103
+ textStream.on("data", (chunk) => {
104
+ const context2 = claimContext();
105
+ if (!context2 || this.context !== context2) return;
80
106
  const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
81
- this.textSent += text;
82
- await this.initPromise;
83
- this.socket?.send(
84
- JSON.stringify({
85
- ...this.getConfig(),
86
- transcript: text,
87
- context_id,
88
- continue: true
89
- })
90
- );
91
- this.log(`Sent transcript: "${text}"`);
107
+ context2.text += text;
108
+ if (this.sendText(context2.id, text, true)) {
109
+ this.log(`Sent transcript: "${text}"`);
110
+ }
92
111
  });
93
112
  textStream.on("error", (error) => {
94
- if (!counter) return;
95
- this.log("Error in text stream, ending audio stream", error);
96
- this.isProcessing = false;
113
+ if (!context || this.context !== context) return;
114
+ this.log("Error in text stream, cancelling the audio", error);
115
+ this.stopContext();
97
116
  });
98
- textStream.on("end", async () => {
99
- if (!counter || counter !== this.counter) return;
100
- await this.initPromise;
101
- this.socket?.send(
102
- JSON.stringify({
103
- ...this.getConfig(),
104
- transcript: "",
105
- context_id,
106
- continue: false
107
- })
108
- );
117
+ textStream.on("end", () => {
118
+ if (!context || this.context !== context) return;
119
+ context.ended = true;
120
+ this.sendText(context.id, "", false);
109
121
  });
110
122
  }
111
123
  cancel() {
112
124
  this.generation++;
113
- if (!this.isProcessing) return;
125
+ if (!this.context) return;
114
126
  this.log("Cancel");
115
- this.isProcessing = false;
116
- this.textSent = "";
117
- if (this.socket?.readyState === import_ws.default.OPEN) {
118
- this.socket.send(
119
- JSON.stringify({
120
- context_id: this.counter.toString(),
121
- cancel: true
122
- })
123
- );
124
- }
125
- this.counter++;
127
+ this.stopContext();
126
128
  }
127
129
  destroy() {
128
130
  super.destroy();
129
- this.destroyed = true;
130
- if (this.reconnectTimeout) {
131
- clearTimeout(this.reconnectTimeout);
132
- this.reconnectTimeout = void 0;
133
- }
134
- if (this.connectionTimeout) {
135
- clearTimeout(this.connectionTimeout);
136
- this.connectionTimeout = void 0;
137
- }
138
- if (this.socket) closeSocket(this.socket);
139
- this.socket = void 0;
140
- this.isProcessing = false;
131
+ this.socket.destroy();
132
+ this.context = void 0;
133
+ }
134
+ /** Asks Cartesia to drop the current context, and forgets it */
135
+ stopContext() {
136
+ if (!this.context) return;
137
+ this.socket.send(
138
+ JSON.stringify({
139
+ context_id: this.context.id,
140
+ cancel: true
141
+ })
142
+ );
143
+ this.context = void 0;
144
+ }
145
+ sendText(contextId, transcript, more) {
146
+ return this.socket.send(
147
+ JSON.stringify({
148
+ ...this.getConfig(),
149
+ transcript,
150
+ context_id: contextId,
151
+ continue: more
152
+ })
153
+ );
141
154
  }
142
155
  getConfig() {
143
156
  return {
@@ -156,104 +169,7 @@ var CartesiaTTS = class extends import_server.TTS {
156
169
  generation_config: this.options.generationConfig
157
170
  };
158
171
  }
159
- // Connect to Cartesia
160
- async initWS() {
161
- return new Promise((resolve, reject) => {
162
- const socket = new import_ws.default(
163
- `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`
164
- );
165
- this.socket = socket;
166
- const timeout = setTimeout(() => {
167
- this.log("Connection timeout");
168
- closeSocket(socket);
169
- this.socket = void 0;
170
- reject(new Error("WebSocket connection timeout"));
171
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
172
- this.connectionTimeout = timeout;
173
- socket.addEventListener("open", () => {
174
- clearTimeout(timeout);
175
- this.log("Connection opened");
176
- resolve();
177
- });
178
- socket.addEventListener("error", (error) => {
179
- clearTimeout(timeout);
180
- this.log("WebSocket error:", error);
181
- reject(new Error("WebSocket connection error"));
182
- });
183
- socket.addEventListener("close", ({ code, reason }) => {
184
- clearTimeout(timeout);
185
- this.socket?.removeAllListeners();
186
- this.socket = void 0;
187
- this.isProcessing = false;
188
- this.log("Connection closed", { code, reason });
189
- this.reconnect();
190
- });
191
- socket.addEventListener("message", (event) => {
192
- try {
193
- const message = JSON.parse(event.data.toString());
194
- if (this.counter.toString() !== message.context_id) return;
195
- switch (message.type) {
196
- case "chunk":
197
- const chunk = Buffer.from(message.data, "base64");
198
- this.log(`Received audio chunk (${chunk.length} bytes)`);
199
- this.emit("Audio", chunk);
200
- break;
201
- case "done":
202
- this.log("Audio ended");
203
- this.isProcessing = false;
204
- this.textSent = "";
205
- break;
206
- case "error":
207
- this.log("Error", message.error);
208
- break;
209
- }
210
- } catch {
211
- this.log("Error parsing message", event.data);
212
- }
213
- });
214
- });
215
- }
216
- reconnect() {
217
- if (this.destroyed) return;
218
- this.retryCount++;
219
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
220
- this.log("Max retries reached, giving up");
221
- this.emit("Failed", [this.textSent]);
222
- return;
223
- }
224
- this.initPromise = new Promise((resolve) => {
225
- this.log("Reconnecting...");
226
- this.reconnectTimeout = setTimeout(() => {
227
- this.reconnectTimeout = void 0;
228
- this.initWS().then(() => {
229
- this.retryCount = 0;
230
- if (this.textSent.length > 0) {
231
- this.log("Sending text chunks again");
232
- this.socket?.send(
233
- JSON.stringify({
234
- ...this.getConfig(),
235
- transcript: this.textSent,
236
- context_id: this.counter.toString(),
237
- continue: true
238
- })
239
- );
240
- }
241
- }).then(resolve).catch((error) => {
242
- this.log("Reconnection error:", error);
243
- this.reconnect();
244
- });
245
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
246
- });
247
- }
248
172
  };
249
- function closeSocket(socket) {
250
- socket.removeAllListeners();
251
- socket.on("error", () => {
252
- });
253
- if (socket.readyState === import_ws.default.OPEN || socket.readyState === import_ws.default.CONNECTING) {
254
- socket.close(1e3);
255
- }
256
- }
257
173
  // Annotate the CommonJS export names for ESM import in node:
258
174
  0 && (module.exports = {
259
175
  CartesiaTTS
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/CartesiaTTS.ts"],"sourcesContent":["export * from './CartesiaTTS'\nexport * from './types'\n","import { TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaGenerationConfig,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n /** @deprecated Sonic 3 models read `generationConfig.speed` instead */\n speed?: 'fast' | 'normal' | 'slow'\n generationConfig?: CartesiaGenerationConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\nexport class CartesiaTTS extends TTS {\n private socket?: WebSocket\n private initPromise: Promise<void>\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard. Kept apart from\n // `counter`, which numbers the audio contexts and must only move when there\n // is something to synthesize.\n private generation = 0\n private isProcessing = false\n private reconnectTimeout?: NodeJS.Timeout\n private connectionTimeout?: NodeJS.Timeout\n private textSent: string = '' // Store text chunks to send them again if reconnecting\n private retryCount = 0\n private destroyed = false\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n // Setup WebSocket connection\n this.initPromise = this.initWS().catch((error) => {\n console.error('[CartesiaTTS] Connection error:', error)\n this.reconnect()\n })\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let counter = 0\n let context_id = ''\n\n // Claiming the context is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (counter) return true\n // Cancelled, or superseded by another speak(), before the first word.\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n context_id = counter.toString()\n this.isProcessing = true\n this.textSent = ''\n return true\n }\n\n textStream.on('data', async (chunk: Buffer) => {\n if (!claimContext()) return\n if (counter !== this.counter) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n this.textSent += text\n\n await this.initPromise\n\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: text,\n context_id,\n continue: true,\n } satisfies CartesiaPayload)\n )\n this.log(`Sent transcript: \"${text}\"`)\n })\n\n textStream.on('error', (error) => {\n if (!counter) return\n this.log('Error in text stream, ending audio stream', error)\n this.isProcessing = false\n })\n\n textStream.on('end', async () => {\n // Nothing was ever said, so there is no context to close.\n if (!counter || counter !== this.counter) return\n await this.initPromise\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: '',\n context_id,\n continue: false,\n } satisfies CartesiaPayload)\n )\n })\n }\n\n cancel() {\n this.generation++\n if (!this.isProcessing) return\n this.log('Cancel')\n this.isProcessing = false\n this.textSent = ''\n\n // Signal Cartesia to stop sending data\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket.send(\n JSON.stringify({\n context_id: this.counter.toString(),\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n }\n\n // Increment counter to avoid processing messages from previous calls\n this.counter++\n }\n\n destroy() {\n super.destroy()\n this.destroyed = true\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n if (this.connectionTimeout) {\n clearTimeout(this.connectionTimeout)\n this.connectionTimeout = undefined\n }\n if (this.socket) closeSocket(this.socket)\n this.socket = undefined\n this.isProcessing = false\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n generation_config: this.options.generationConfig,\n } as const\n }\n\n // Connect to Cartesia\n private async initWS() {\n return new Promise<void>((resolve, reject) => {\n const socket = new WebSocket(\n `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`\n )\n this.socket = socket\n\n const timeout = setTimeout(() => {\n this.log('Connection timeout')\n closeSocket(socket)\n this.socket = undefined\n reject(new Error('WebSocket connection timeout'))\n }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT)\n this.connectionTimeout = timeout\n\n socket.addEventListener('open', () => {\n clearTimeout(timeout)\n this.log('Connection opened')\n resolve()\n })\n\n socket.addEventListener('error', (error) => {\n clearTimeout(timeout)\n this.log('WebSocket error:', error)\n reject(new Error('WebSocket connection error'))\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n clearTimeout(timeout)\n this.socket?.removeAllListeners()\n this.socket = undefined\n this.isProcessing = false\n\n // Always reconnect: cancel() never closes the socket (it cancels\n // in-band), and destroy() detaches its listeners before closing, so any\n // close reaching this handler is server-initiated. Reconnecting even on\n // a graceful 1000 close keeps the connection ready, otherwise the next\n // speak() would send on an undefined socket and silently no-op.\n this.log('Connection closed', { code, reason })\n this.reconnect()\n })\n\n socket.addEventListener('message', (event) => {\n try {\n const message: CartesiaResponse = JSON.parse(event.data.toString())\n\n // Ignore messages from previous calls\n if (this.counter.toString() !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.isProcessing = false\n this.textSent = ''\n break\n case 'error':\n this.log('Error', message.error)\n break\n }\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private reconnect() {\n if (this.destroyed) return\n this.retryCount++\n if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {\n this.log('Max retries reached, giving up')\n this.emit('Failed', [this.textSent])\n return\n }\n\n this.initPromise = new Promise((resolve) => {\n this.log('Reconnecting...')\n this.reconnectTimeout = setTimeout(() => {\n this.reconnectTimeout = undefined\n this.initWS()\n .then(() => {\n this.retryCount = 0\n\n // Send text chunks again if reconnecting\n if (this.textSent.length > 0) {\n this.log('Sending text chunks again')\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: this.textSent,\n context_id: this.counter.toString(),\n continue: true,\n } satisfies CartesiaPayload)\n )\n }\n })\n .then(resolve)\n .catch((error) => {\n this.log('Reconnection error:', error)\n this.reconnect()\n })\n }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY)\n })\n }\n}\n\n/**\n * Closes a socket without hearing from it again.\n *\n * A socket closed while still connecting emits an error, and an error nobody\n * listens to crashes the process, so a listener that ignores it stays.\n */\nfunction closeSocket(socket: WebSocket) {\n socket.removeAllListeners()\n socket.on('error', () => {})\n if (\n socket.readyState === WebSocket.OPEN ||\n socket.readyState === WebSocket.CONNECTING\n ) {\n socket.close(1000)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAoB;AAEpB,gBAAsB;AAsBtB,IAAM,6BAA6B;AACnC,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AACnB,IAAM,cAAN,cAA0B,kBAAI;AAAA,EAg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B;AACF;","names":["WebSocket"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/CartesiaTTS.ts"],"sourcesContent":["export * from './CartesiaTTS'\nexport * from './types'\n","import { ReconnectingSocket, TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaGenerationConfig,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n /** @deprecated Sonic 3 models read `generationConfig.speed` instead */\n speed?: 'fast' | 'normal' | 'slow'\n generationConfig?: CartesiaGenerationConfig\n /** How long a connection attempt may take, 5000 ms by default */\n connectionTimeout?: number\n /** How long to wait before a new attempt, 1000 ms by default */\n retryDelay?: number\n /**\n * How many attempts in a row may fail before giving up and emitting\n * `Failed`, 3 by default. The next utterance tries to connect again.\n */\n maxRetry?: number\n}\n\n/**\n * The utterance Cartesia is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of an utterance\n * (done, server error, cancel, text stream error, give up) clears it, so\n * nothing about it outlives it.\n */\ninterface CartesiaContext {\n id: string\n /** Every word received so far, sent again whole on a new connection */\n text: string\n /** The text stream ended, so Cartesia was told no more text follows */\n ended: boolean\n}\n\nexport class CartesiaTTS extends TTS {\n private socket: ReconnectingSocket\n // Numbers the audio contexts, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private context?: CartesiaContext\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n this.socket = new ReconnectingSocket({\n url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let context: CartesiaContext | undefined\n\n // Claiming the context is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (context) return context\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n context = { id: (++this.counter).toString(), text: '', ended: false }\n this.context = context\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return context\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const context = claimContext()\n if (!context || this.context !== context) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n context.text += text\n // Without a connection the text waits, and goes out whole on open\n if (this.sendText(context.id, text, true)) {\n this.log(`Sent transcript: \"${text}\"`)\n }\n })\n\n textStream.on('error', (error) => {\n if (!context || this.context !== context) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopContext()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no context to close\n if (!context || this.context !== context) return\n context.ended = true\n this.sendText(context.id, '', false)\n })\n }\n\n cancel() {\n this.generation++\n if (!this.context) return\n this.log('Cancel')\n this.stopContext()\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.context = undefined\n }\n\n /** Asks Cartesia to drop the current context, and forgets it */\n private stopContext() {\n if (!this.context) return\n this.socket.send(\n JSON.stringify({\n context_id: this.context.id,\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n this.context = undefined\n }\n\n private sendText(contextId: string, transcript: string, more: boolean) {\n return this.socket.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript,\n context_id: contextId,\n continue: more,\n } satisfies CartesiaPayload)\n )\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n generation_config: this.options.generationConfig,\n } as const\n }\n\n // A new connection knows nothing of the utterance under way: the text read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole\n private onOpen = () => {\n const context = this.context\n if (!context) return\n if (context.text) {\n this.log('Sending text again')\n this.sendText(context.id, context.text, true)\n }\n if (context.ended) this.sendText(context.id, '', false)\n }\n\n private onMessage = (data: RawData) => {\n let message: CartesiaResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n // Ignore messages from previous contexts\n if (!this.context || this.context.id !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.context = undefined\n break\n case 'error':\n // Cartesia sends nothing more for a context it failed\n this.log('Error', message.error)\n this.context = undefined\n break\n }\n }\n\n private onFailed = () => {\n const text = this.context?.text\n this.context = undefined\n this.emit('Failed', text ? [text] : [])\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAwC;AA4CjC,IAAM,cAAN,cAA0B,kBAAI;AAAA,EASnC,YAA6B,SAA6B;AACxD,UAAM;AADqB;AAN7B;AAAA,SAAQ,UAAU;AAGlB;AAAA;AAAA,SAAQ,aAAa;AA2HrB;AAAA;AAAA;AAAA,SAAQ,SAAS,MAAM;AACrB,YAAM,UAAU,KAAK;AACrB,UAAI,CAAC,QAAS;AACd,UAAI,QAAQ,MAAM;AAChB,aAAK,IAAI,oBAAoB;AAC7B,aAAK,SAAS,QAAQ,IAAI,QAAQ,MAAM,IAAI;AAAA,MAC9C;AACA,UAAI,QAAQ,MAAO,MAAK,SAAS,QAAQ,IAAI,IAAI,KAAK;AAAA,IACxD;A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,SAAS,WAAmB,YAAoB,MAAe;AACrE,WAAO,KAAK,OAAO;AAAA,MACjB,KAAK,UAAU;AAAA,QACb,GAAG,KAAK,UAAU;AAAA,QAClB;AAAA,QACA,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ,CAA2B;AAAA,IAC7B;AAAA,EACF;AAAA,EAEQ,YAAY;AAClB,WAAO;AAAA,MACL,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO;AAAA,QACL,MAAM;AAAA,QACN,IAAI,KAAK,QAAQ;AAAA,MACnB;AAAA,MACA,eAAe;AAAA,QACb,WAAW;AAAA,QACX,UAAU;AAAA,QACV,aAAa;AAAA,MACf;AAAA,MACA,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO,KAAK,QAAQ;AAAA,MACpB,mBAAmB,KAAK,QAAQ;AAAA,IAClC;AAAA,EACF;AAkDF;","names":["context"]}
package/dist/index.mjs CHANGED
@@ -1,107 +1,130 @@
1
1
  // src/CartesiaTTS.ts
2
- import { TTS } from "@micdrop/server";
3
- import WebSocket from "ws";
4
- var DEFAULT_CONNECTION_TIMEOUT = 5e3;
5
- var DEFAULT_RETRY_DELAY = 1e3;
6
- var DEFAULT_MAX_RETRY = 3;
2
+ import { ReconnectingSocket, TTS } from "@micdrop/server";
7
3
  var CartesiaTTS = class extends TTS {
8
4
  constructor(options) {
9
5
  super();
10
6
  this.options = options;
7
+ // Numbers the audio contexts, and only moves when there is something to say
11
8
  this.counter = 0;
12
9
  // Bumped by every speak() and every cancel(), so a context claimed late can
13
- // tell whether it is still the one that should be heard. Kept apart from
14
- // `counter`, which numbers the audio contexts and must only move when there
15
- // is something to synthesize.
10
+ // tell whether it is still the one that should be heard
16
11
  this.generation = 0;
17
- this.isProcessing = false;
18
- this.textSent = "";
19
- // Store text chunks to send them again if reconnecting
20
- this.retryCount = 0;
21
- this.destroyed = false;
22
- this.initPromise = this.initWS().catch((error) => {
23
- console.error("[CartesiaTTS] Connection error:", error);
24
- this.reconnect();
12
+ // A new connection knows nothing of the utterance under way: the text read
13
+ // before the first connection opened, or lost with the previous one, goes out
14
+ // again whole
15
+ this.onOpen = () => {
16
+ const context = this.context;
17
+ if (!context) return;
18
+ if (context.text) {
19
+ this.log("Sending text again");
20
+ this.sendText(context.id, context.text, true);
21
+ }
22
+ if (context.ended) this.sendText(context.id, "", false);
23
+ };
24
+ this.onMessage = (data) => {
25
+ let message;
26
+ try {
27
+ message = JSON.parse(data.toString());
28
+ } catch {
29
+ this.log("Error parsing message", data);
30
+ return;
31
+ }
32
+ if (!this.context || this.context.id !== message.context_id) return;
33
+ switch (message.type) {
34
+ case "chunk":
35
+ const chunk = Buffer.from(message.data, "base64");
36
+ this.log(`Received audio chunk (${chunk.length} bytes)`);
37
+ this.emit("Audio", chunk);
38
+ break;
39
+ case "done":
40
+ this.log("Audio ended");
41
+ this.context = void 0;
42
+ break;
43
+ case "error":
44
+ this.log("Error", message.error);
45
+ this.context = void 0;
46
+ break;
47
+ }
48
+ };
49
+ this.onFailed = () => {
50
+ const text = this.context?.text;
51
+ this.context = void 0;
52
+ this.emit("Failed", text ? [text] : []);
53
+ };
54
+ this.socket = new ReconnectingSocket({
55
+ url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,
56
+ connectionTimeout: options.connectionTimeout,
57
+ retryDelay: options.retryDelay,
58
+ maxRetry: options.maxRetry,
59
+ log: (...message) => this.log(...message)
25
60
  });
61
+ this.socket.on("Open", this.onOpen);
62
+ this.socket.on("Message", this.onMessage);
63
+ this.socket.on("Failed", this.onFailed);
64
+ this.socket.connect();
26
65
  }
27
66
  speak(textStream) {
28
67
  const generation = ++this.generation;
29
- let counter = 0;
30
- let context_id = "";
68
+ let context;
31
69
  const claimContext = () => {
32
- if (counter) return true;
33
- if (this.generation !== generation) return false;
34
- this.counter++;
35
- counter = this.counter;
36
- context_id = counter.toString();
37
- this.isProcessing = true;
38
- this.textSent = "";
39
- return true;
70
+ if (context) return context;
71
+ if (this.generation !== generation) return;
72
+ context = { id: (++this.counter).toString(), text: "", ended: false };
73
+ this.context = context;
74
+ this.socket.connect();
75
+ return context;
40
76
  };
41
- textStream.on("data", async (chunk) => {
42
- if (!claimContext()) return;
43
- if (counter !== this.counter) return;
77
+ textStream.on("data", (chunk) => {
78
+ const context2 = claimContext();
79
+ if (!context2 || this.context !== context2) return;
44
80
  const text = chunk.toString("utf-8").replace(/[\r\n ]+/g, " ");
45
- this.textSent += text;
46
- await this.initPromise;
47
- this.socket?.send(
48
- JSON.stringify({
49
- ...this.getConfig(),
50
- transcript: text,
51
- context_id,
52
- continue: true
53
- })
54
- );
55
- this.log(`Sent transcript: "${text}"`);
81
+ context2.text += text;
82
+ if (this.sendText(context2.id, text, true)) {
83
+ this.log(`Sent transcript: "${text}"`);
84
+ }
56
85
  });
57
86
  textStream.on("error", (error) => {
58
- if (!counter) return;
59
- this.log("Error in text stream, ending audio stream", error);
60
- this.isProcessing = false;
87
+ if (!context || this.context !== context) return;
88
+ this.log("Error in text stream, cancelling the audio", error);
89
+ this.stopContext();
61
90
  });
62
- textStream.on("end", async () => {
63
- if (!counter || counter !== this.counter) return;
64
- await this.initPromise;
65
- this.socket?.send(
66
- JSON.stringify({
67
- ...this.getConfig(),
68
- transcript: "",
69
- context_id,
70
- continue: false
71
- })
72
- );
91
+ textStream.on("end", () => {
92
+ if (!context || this.context !== context) return;
93
+ context.ended = true;
94
+ this.sendText(context.id, "", false);
73
95
  });
74
96
  }
75
97
  cancel() {
76
98
  this.generation++;
77
- if (!this.isProcessing) return;
99
+ if (!this.context) return;
78
100
  this.log("Cancel");
79
- this.isProcessing = false;
80
- this.textSent = "";
81
- if (this.socket?.readyState === WebSocket.OPEN) {
82
- this.socket.send(
83
- JSON.stringify({
84
- context_id: this.counter.toString(),
85
- cancel: true
86
- })
87
- );
88
- }
89
- this.counter++;
101
+ this.stopContext();
90
102
  }
91
103
  destroy() {
92
104
  super.destroy();
93
- this.destroyed = true;
94
- if (this.reconnectTimeout) {
95
- clearTimeout(this.reconnectTimeout);
96
- this.reconnectTimeout = void 0;
97
- }
98
- if (this.connectionTimeout) {
99
- clearTimeout(this.connectionTimeout);
100
- this.connectionTimeout = void 0;
101
- }
102
- if (this.socket) closeSocket(this.socket);
103
- this.socket = void 0;
104
- this.isProcessing = false;
105
+ this.socket.destroy();
106
+ this.context = void 0;
107
+ }
108
+ /** Asks Cartesia to drop the current context, and forgets it */
109
+ stopContext() {
110
+ if (!this.context) return;
111
+ this.socket.send(
112
+ JSON.stringify({
113
+ context_id: this.context.id,
114
+ cancel: true
115
+ })
116
+ );
117
+ this.context = void 0;
118
+ }
119
+ sendText(contextId, transcript, more) {
120
+ return this.socket.send(
121
+ JSON.stringify({
122
+ ...this.getConfig(),
123
+ transcript,
124
+ context_id: contextId,
125
+ continue: more
126
+ })
127
+ );
105
128
  }
106
129
  getConfig() {
107
130
  return {
@@ -120,104 +143,7 @@ var CartesiaTTS = class extends TTS {
120
143
  generation_config: this.options.generationConfig
121
144
  };
122
145
  }
123
- // Connect to Cartesia
124
- async initWS() {
125
- return new Promise((resolve, reject) => {
126
- const socket = new WebSocket(
127
- `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`
128
- );
129
- this.socket = socket;
130
- const timeout = setTimeout(() => {
131
- this.log("Connection timeout");
132
- closeSocket(socket);
133
- this.socket = void 0;
134
- reject(new Error("WebSocket connection timeout"));
135
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
136
- this.connectionTimeout = timeout;
137
- socket.addEventListener("open", () => {
138
- clearTimeout(timeout);
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
- this.isProcessing = false;
152
- this.log("Connection closed", { code, reason });
153
- this.reconnect();
154
- });
155
- socket.addEventListener("message", (event) => {
156
- try {
157
- const message = JSON.parse(event.data.toString());
158
- if (this.counter.toString() !== message.context_id) return;
159
- switch (message.type) {
160
- case "chunk":
161
- const chunk = Buffer.from(message.data, "base64");
162
- this.log(`Received audio chunk (${chunk.length} bytes)`);
163
- this.emit("Audio", chunk);
164
- break;
165
- case "done":
166
- this.log("Audio ended");
167
- this.isProcessing = false;
168
- this.textSent = "";
169
- break;
170
- case "error":
171
- this.log("Error", message.error);
172
- break;
173
- }
174
- } catch {
175
- this.log("Error parsing message", event.data);
176
- }
177
- });
178
- });
179
- }
180
- reconnect() {
181
- if (this.destroyed) return;
182
- this.retryCount++;
183
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {
184
- this.log("Max retries reached, giving up");
185
- this.emit("Failed", [this.textSent]);
186
- return;
187
- }
188
- this.initPromise = new Promise((resolve) => {
189
- this.log("Reconnecting...");
190
- this.reconnectTimeout = setTimeout(() => {
191
- this.reconnectTimeout = void 0;
192
- this.initWS().then(() => {
193
- this.retryCount = 0;
194
- if (this.textSent.length > 0) {
195
- this.log("Sending text chunks again");
196
- this.socket?.send(
197
- JSON.stringify({
198
- ...this.getConfig(),
199
- transcript: this.textSent,
200
- context_id: this.counter.toString(),
201
- continue: true
202
- })
203
- );
204
- }
205
- }).then(resolve).catch((error) => {
206
- this.log("Reconnection error:", error);
207
- this.reconnect();
208
- });
209
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY);
210
- });
211
- }
212
146
  };
213
- function closeSocket(socket) {
214
- socket.removeAllListeners();
215
- socket.on("error", () => {
216
- });
217
- if (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING) {
218
- socket.close(1e3);
219
- }
220
- }
221
147
  export {
222
148
  CartesiaTTS
223
149
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/CartesiaTTS.ts"],"sourcesContent":["import { TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaGenerationConfig,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n /** @deprecated Sonic 3 models read `generationConfig.speed` instead */\n speed?: 'fast' | 'normal' | 'slow'\n generationConfig?: CartesiaGenerationConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\nexport class CartesiaTTS extends TTS {\n private socket?: WebSocket\n private initPromise: Promise<void>\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard. Kept apart from\n // `counter`, which numbers the audio contexts and must only move when there\n // is something to synthesize.\n private generation = 0\n private isProcessing = false\n private reconnectTimeout?: NodeJS.Timeout\n private connectionTimeout?: NodeJS.Timeout\n private textSent: string = '' // Store text chunks to send them again if reconnecting\n private retryCount = 0\n private destroyed = false\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n // Setup WebSocket connection\n this.initPromise = this.initWS().catch((error) => {\n console.error('[CartesiaTTS] Connection error:', error)\n this.reconnect()\n })\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let counter = 0\n let context_id = ''\n\n // Claiming the context is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (counter) return true\n // Cancelled, or superseded by another speak(), before the first word.\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n context_id = counter.toString()\n this.isProcessing = true\n this.textSent = ''\n return true\n }\n\n textStream.on('data', async (chunk: Buffer) => {\n if (!claimContext()) return\n if (counter !== this.counter) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n this.textSent += text\n\n await this.initPromise\n\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: text,\n context_id,\n continue: true,\n } satisfies CartesiaPayload)\n )\n this.log(`Sent transcript: \"${text}\"`)\n })\n\n textStream.on('error', (error) => {\n if (!counter) return\n this.log('Error in text stream, ending audio stream', error)\n this.isProcessing = false\n })\n\n textStream.on('end', async () => {\n // Nothing was ever said, so there is no context to close.\n if (!counter || counter !== this.counter) return\n await this.initPromise\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: '',\n context_id,\n continue: false,\n } satisfies CartesiaPayload)\n )\n })\n }\n\n cancel() {\n this.generation++\n if (!this.isProcessing) return\n this.log('Cancel')\n this.isProcessing = false\n this.textSent = ''\n\n // Signal Cartesia to stop sending data\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket.send(\n JSON.stringify({\n context_id: this.counter.toString(),\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n }\n\n // Increment counter to avoid processing messages from previous calls\n this.counter++\n }\n\n destroy() {\n super.destroy()\n this.destroyed = true\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n if (this.connectionTimeout) {\n clearTimeout(this.connectionTimeout)\n this.connectionTimeout = undefined\n }\n if (this.socket) closeSocket(this.socket)\n this.socket = undefined\n this.isProcessing = false\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n generation_config: this.options.generationConfig,\n } as const\n }\n\n // Connect to Cartesia\n private async initWS() {\n return new Promise<void>((resolve, reject) => {\n const socket = new WebSocket(\n `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`\n )\n this.socket = socket\n\n const timeout = setTimeout(() => {\n this.log('Connection timeout')\n closeSocket(socket)\n this.socket = undefined\n reject(new Error('WebSocket connection timeout'))\n }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT)\n this.connectionTimeout = timeout\n\n socket.addEventListener('open', () => {\n clearTimeout(timeout)\n this.log('Connection opened')\n resolve()\n })\n\n socket.addEventListener('error', (error) => {\n clearTimeout(timeout)\n this.log('WebSocket error:', error)\n reject(new Error('WebSocket connection error'))\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n clearTimeout(timeout)\n this.socket?.removeAllListeners()\n this.socket = undefined\n this.isProcessing = false\n\n // Always reconnect: cancel() never closes the socket (it cancels\n // in-band), and destroy() detaches its listeners before closing, so any\n // close reaching this handler is server-initiated. Reconnecting even on\n // a graceful 1000 close keeps the connection ready, otherwise the next\n // speak() would send on an undefined socket and silently no-op.\n this.log('Connection closed', { code, reason })\n this.reconnect()\n })\n\n socket.addEventListener('message', (event) => {\n try {\n const message: CartesiaResponse = JSON.parse(event.data.toString())\n\n // Ignore messages from previous calls\n if (this.counter.toString() !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.isProcessing = false\n this.textSent = ''\n break\n case 'error':\n this.log('Error', message.error)\n break\n }\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private reconnect() {\n if (this.destroyed) return\n this.retryCount++\n if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {\n this.log('Max retries reached, giving up')\n this.emit('Failed', [this.textSent])\n return\n }\n\n this.initPromise = new Promise((resolve) => {\n this.log('Reconnecting...')\n this.reconnectTimeout = setTimeout(() => {\n this.reconnectTimeout = undefined\n this.initWS()\n .then(() => {\n this.retryCount = 0\n\n // Send text chunks again if reconnecting\n if (this.textSent.length > 0) {\n this.log('Sending text chunks again')\n this.socket?.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript: this.textSent,\n context_id: this.counter.toString(),\n continue: true,\n } satisfies CartesiaPayload)\n )\n }\n })\n .then(resolve)\n .catch((error) => {\n this.log('Reconnection error:', error)\n this.reconnect()\n })\n }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY)\n })\n }\n}\n\n/**\n * Closes a socket without hearing from it again.\n *\n * A socket closed while still connecting emits an error, and an error nobody\n * listens to crashes the process, so a listener that ignores it stays.\n */\nfunction closeSocket(socket: WebSocket) {\n socket.removeAllListeners()\n socket.on('error', () => {})\n if (\n socket.readyState === WebSocket.OPEN ||\n socket.readyState === WebSocket.CONNECTING\n ) {\n socket.close(1000)\n }\n}\n"],"mappings":";AAAA,SAAS,WAAW;AAEpB,OAAO,eAAe;AAsBtB,IAAM,6BAA6B;AACnC,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AACnB,IAAM,cAAN,cAA0B,IAAI;AAAA,EAgBnC,YAA6B,SAA6B;AACxD,UAAM;AADqB;AAb7B,SAAQ,UAAU;AAKlB;AAAA;AAAA;AAAA;AAAA,SAAQ,aAAa;AACrB,SAAQ,eAAe;AAGvB,SAAQ,WAAmB;AAC3B;AAAA,SAAQ,aAAa;AACrB,SAAQ,YAAY;AAMlB,SAAK,cAAc,KAAK,OAAO,EAAE,MAAM,CAAC,UAAU;AAChD,cAAQ,MAAM,mCAAmC,KAAK;AACtD,WAAK,UAAU;AAAA,IACjB,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,YAAsB;AAC1B,UAAM,aAAa,EAAE,KAAK;AAC1B,QAAI,UAAU;AACd,QAAI,aAAa;AASjB,UAAM,eAAe,MAAM;AACzB,UAAI,QAAS,QAAO;AAEpB,UAAI,KAAK,eAAe,WAAY,QAAO;AAC3C,WAAK;AACL,gBAAU,KAAK;AACf,mBAAa,QAAQ,SAAS;AAC9B,WAAK,eAAe;AACpB,WAA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,iBAAK,IAAI,2BAA2B;AACpC,iBAAK,QAAQ;AAAA,cACX,KAAK,UAAU;AAAA,gBACb,GAAG,KAAK,UAAU;AAAA,gBAClB,YAAY,KAAK;AAAA,gBACjB,YAAY,KAAK,QAAQ,SAAS;AAAA,gBAClC,UAAU;AAAA,cACZ,CAA2B;AAAA,YAC7B;AAAA,UACF;AAAA,QACF,CAAC,EACA,KAAK,OAAO,EACZ,MAAM,CAAC,UAAU;AAChB,eAAK,IAAI,uBAAuB,KAAK;AACrC,eAAK,UAAU;AAAA,QACjB,CAAC;AAAA,MACL,GAAG,KAAK,QAAQ,cAAc,mBAAmB;AAAA,IACnD,CAAC;AAAA,EACH;AACF;AAQA,SAAS,YAAY,QAAmB;AACtC,SAAO,mBAAmB;AAC1B,SAAO,GAAG,SAAS,MAAM;AAAA,EAAC,CAAC;AAC3B,MACE,OAAO,eAAe,UAAU,QAChC,OAAO,eAAe,UAAU,YAChC;AACA,WAAO,MAAM,GAAI;AAAA,EACnB;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/CartesiaTTS.ts"],"sourcesContent":["import { ReconnectingSocket, TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n CartesiaCancelPayload,\n CartesiaGenerationConfig,\n CartesiaLanguage,\n CartesiaPayload,\n CartesiaResponse,\n} from './types'\n\nexport interface CartesiaTTSOptions {\n apiKey: string\n modelId: string\n voiceId: string\n language?: CartesiaLanguage\n /** @deprecated Sonic 3 models read `generationConfig.speed` instead */\n speed?: 'fast' | 'normal' | 'slow'\n generationConfig?: CartesiaGenerationConfig\n /** How long a connection attempt may take, 5000 ms by default */\n connectionTimeout?: number\n /** How long to wait before a new attempt, 1000 ms by default */\n retryDelay?: number\n /**\n * How many attempts in a row may fail before giving up and emitting\n * `Failed`, 3 by default. The next utterance tries to connect again.\n */\n maxRetry?: number\n}\n\n/**\n * The utterance Cartesia is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of an utterance\n * (done, server error, cancel, text stream error, give up) clears it, so\n * nothing about it outlives it.\n */\ninterface CartesiaContext {\n id: string\n /** Every word received so far, sent again whole on a new connection */\n text: string\n /** The text stream ended, so Cartesia was told no more text follows */\n ended: boolean\n}\n\nexport class CartesiaTTS extends TTS {\n private socket: ReconnectingSocket\n // Numbers the audio contexts, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a context claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private context?: CartesiaContext\n\n constructor(private readonly options: CartesiaTTSOptions) {\n super()\n\n this.socket = new ReconnectingSocket({\n url: `wss://api.cartesia.ai/tts/websocket?api_key=${this.options.apiKey}&cartesia_version=2025-04-16`,\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let context: CartesiaContext | undefined\n\n // Claiming the context is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current context and\n // anything older is dropped. A stream that never carries a word, which is\n // what an answer skipped by a tool or by onBeforeAnswer hands over, would\n // then cut the assistant off mid-sentence for nothing.\n const claimContext = () => {\n if (context) return context\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n context = { id: (++this.counter).toString(), text: '', ended: false }\n this.context = context\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return context\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const context = claimContext()\n if (!context || this.context !== context) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n context.text += text\n // Without a connection the text waits, and goes out whole on open\n if (this.sendText(context.id, text, true)) {\n this.log(`Sent transcript: \"${text}\"`)\n }\n })\n\n textStream.on('error', (error) => {\n if (!context || this.context !== context) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopContext()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no context to close\n if (!context || this.context !== context) return\n context.ended = true\n this.sendText(context.id, '', false)\n })\n }\n\n cancel() {\n this.generation++\n if (!this.context) return\n this.log('Cancel')\n this.stopContext()\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.context = undefined\n }\n\n /** Asks Cartesia to drop the current context, and forgets it */\n private stopContext() {\n if (!this.context) return\n this.socket.send(\n JSON.stringify({\n context_id: this.context.id,\n cancel: true,\n } satisfies CartesiaCancelPayload)\n )\n this.context = undefined\n }\n\n private sendText(contextId: string, transcript: string, more: boolean) {\n return this.socket.send(\n JSON.stringify({\n ...this.getConfig(),\n transcript,\n context_id: contextId,\n continue: more,\n } satisfies CartesiaPayload)\n )\n }\n\n private getConfig() {\n return {\n model_id: this.options.modelId,\n voice: {\n mode: 'id',\n id: this.options.voiceId,\n },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: 16000,\n },\n language: this.options.language,\n speed: this.options.speed,\n generation_config: this.options.generationConfig,\n } as const\n }\n\n // A new connection knows nothing of the utterance under way: the text read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole\n private onOpen = () => {\n const context = this.context\n if (!context) return\n if (context.text) {\n this.log('Sending text again')\n this.sendText(context.id, context.text, true)\n }\n if (context.ended) this.sendText(context.id, '', false)\n }\n\n private onMessage = (data: RawData) => {\n let message: CartesiaResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n // Ignore messages from previous contexts\n if (!this.context || this.context.id !== message.context_id) return\n\n switch (message.type) {\n case 'chunk':\n const chunk = Buffer.from(message.data, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n break\n case 'done':\n this.log('Audio ended')\n this.context = undefined\n break\n case 'error':\n // Cartesia sends nothing more for a context it failed\n this.log('Error', message.error)\n this.context = undefined\n break\n }\n }\n\n private on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AY,QAAS;AAC1C,cAAQ,QAAQ;AAChB,WAAK,SAAS,QAAQ,IAAI,IAAI,KAAK;AAAA,IACrC,CAAC;AAAA,EACH;AAAA,EAEA,SAAS;AACP,SAAK;AACL,QAAI,CAAC,KAAK,QAAS;AACnB,SAAK,IAAI,QAAQ;AACjB,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,UAAU;AACR,UAAM,QAAQ;AACd,SAAK,OAAO,QAAQ;AACpB,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGQ,cAAc;AACpB,QAAI,CAAC,KAAK,QAAS;AACnB,SAAK,OAAO;AAAA,MACV,KAAK,UAAU;AAAA,QACb,YAAY,KAAK,QAAQ;AAAA,QACzB,QAAQ;AAAA,MACV,CAAiC;AAAA,IACnC;AACA,SAAK,UAAU;AAAA,EACjB;AAAA,EAEQ,SAAS,WAAmB,YAAoB,MAAe;AACrE,WAAO,KAAK,OAAO;AAAA,MACjB,KAAK,UAAU;AAAA,QACb,GAAG,KAAK,UAAU;AAAA,QAClB;AAAA,QACA,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ,CAA2B;AAAA,IAC7B;AAAA,EACF;AAAA,EAEQ,YAAY;AAClB,WAAO;AAAA,MACL,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO;AAAA,QACL,MAAM;AAAA,QACN,IAAI,KAAK,QAAQ;AAAA,MACnB;AAAA,MACA,eAAe;AAAA,QACb,WAAW;AAAA,QACX,UAAU;AAAA,QACV,aAAa;AAAA,MACf;AAAA,MACA,UAAU,KAAK,QAAQ;AAAA,MACvB,OAAO,KAAK,QAAQ;AAAA,MACpB,mBAAmB,KAAK,QAAQ;AAAA,IAClC;AAAA,EACF;AAkDF;","names":["context"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micdrop/cartesia",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Cartesia implementation for @micdrop/server",
5
5
  "author": "Godefroy de Compreignac",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "ws": "^8.19.0"
39
39
  },
40
40
  "peerDependencies": {
41
- "@micdrop/server": "*"
41
+ "@micdrop/server": ">=2.10.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/ws": "^8.18.1",