@charivo/tts 0.6.9 → 0.6.11

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
@@ -66,7 +66,11 @@ When connected, the manager emits:
66
66
  - `stop()` — stops active playback. If it interrupts an in-flight `speak()`
67
67
  call, that call's promise settles as part of the stop instead of being left
68
68
  pending: a deliberate stop is treated as a cancellation, not a failure, so
69
- it resolves rather than rejects.
69
+ it resolves rather than rejects. `stop()` also cancels a `speak()` call still
70
+ starting up — the pre-speech stop, or audio synthesis — the same way: that
71
+ call resolves silently and never begins playback, whether or not it had
72
+ already opened an audio session, so a resolved `speak()` is not proof audio
73
+ played. A newer `speak()` cancels a still-starting older one the same way.
70
74
  - `prepareAudio?.()` — creates the lip-sync `AudioContext` up front. Call it
71
75
  from a user-gesture handler before the first `speak()` so mobile browsers
72
76
  that require audio to start from a gesture do not block playback.
package/dist/index.js CHANGED
@@ -108,6 +108,8 @@ function supportsGenerateAudio(player) {
108
108
  }
109
109
 
110
110
  // src/tts-manager.ts
111
+ var STARTUP_CANCELLED = Symbol("charivo.tts.startupCancelled");
112
+ var noop = () => void 0;
111
113
  var TTSManagerImpl = class {
112
114
  ttsPlayer;
113
115
  eventEmitter;
@@ -137,6 +139,24 @@ var TTSManagerImpl = class {
137
139
  // still matches the current one, so a stale completion can never act on
138
140
  // a session that isn't its own.
139
141
  webSpeechGeneration = 0;
142
+ // Claim counter for speak() calls. A call claims its generation
143
+ // synchronously at entry, before its first await, so any stop() that lands
144
+ // afterwards can invalidate it -- even while it is still starting up and
145
+ // has no playback for stop() to cancel.
146
+ speakGeneration = 0;
147
+ // Every speak() generation up to and including this one is invalidated: it
148
+ // must not start playback, even if its startup operation succeeds. Raised
149
+ // by stop() (through the current claim) and by a newer speak() (through its
150
+ // predecessor's claim).
151
+ cancelledThrough = 0;
152
+ // Startup handles of the speak() calls that have claimed a generation and
153
+ // have not finished yet. Resolving one releases a speak() that would
154
+ // otherwise stay parked on a startup operation that may hang forever.
155
+ startupCancellations = /* @__PURE__ */ new Set();
156
+ // The stop currently running, if any. Stops are single-flight: overlapping
157
+ // requests share one player stop and one cleanup, and no new utterance can
158
+ // dispatch until it has fully settled.
159
+ pendingStopPlayback = null;
140
160
  // Only the Web Speech lip-sync simulation is needed
141
161
  webSimulator;
142
162
  // Reads the emitter at emit time, so a later setEventEmitter() still applies.
@@ -162,26 +182,79 @@ var TTSManagerImpl = class {
162
182
  this.webSimulator = new WebSpeechLipSyncSimulator(eventEmitter);
163
183
  }
164
184
  /**
165
- * Convert text to speech and play it
185
+ * Convert text to speech and play it.
186
+ *
187
+ * Resolves when the utterance finishes -- or silently, without speaking, if
188
+ * stop() or a newer speak() lands while this call is still starting up (the
189
+ * pre-speech stop, or synthesis). A cancelled call never begins playback,
190
+ * whether or not it had already opened an audio session (a reentrant stop
191
+ * from the tts:audio:start listener still sees the session close), so a
192
+ * resolved speak() is not proof audio played.
166
193
  */
167
194
  async speak(text, options) {
195
+ const generation = ++this.speakGeneration;
196
+ this.cancelStartupsThrough(generation - 1);
197
+ const startup = this.createSpeakStartup(generation);
198
+ this.startupCancellations.add(startup);
168
199
  try {
169
- await this.stop().catch((error) => {
170
- throw (0, import_core.toCharivoError)("provider", error, "Failed to stop active TTS");
171
- });
200
+ const stopped = await this.raceStartup(
201
+ this.stopPlayback().catch((error) => {
202
+ throw (0, import_core.toCharivoError)("provider", error, "Failed to stop active TTS");
203
+ }),
204
+ startup.cancelled,
205
+ startup.isCancelled
206
+ );
207
+ if (stopped === STARTUP_CANCELLED) {
208
+ return;
209
+ }
172
210
  if (this.playbackMode === "web-speech") {
173
- return await this.handleWebSpeech(text, options);
211
+ return await this.handleWebSpeech(text, options, startup);
174
212
  } else {
175
- return await this.handleStatelessAudio(text, options);
213
+ return await this.handleStatelessAudio(text, options, startup);
176
214
  }
177
215
  } catch (error) {
178
216
  throw (0, import_core.toCharivoError)("provider", error, "Failed to speak text");
217
+ } finally {
218
+ this.startupCancellations.delete(startup);
179
219
  }
180
220
  }
181
221
  /**
182
- * Stop the currently playing speech
222
+ * Stop the currently playing speech, and cancel every speak() still
223
+ * starting up -- those calls resolve silently instead of speaking after
224
+ * this stop.
183
225
  */
184
226
  async stop() {
227
+ this.cancelStartupsThrough(this.speakGeneration);
228
+ await this.stopPlayback();
229
+ }
230
+ /**
231
+ * Stop playback, single-flight: a request issued while another stop is
232
+ * still running joins it instead of starting a second one.
233
+ *
234
+ * Coalescing is safe because no new playback can be established while a
235
+ * stop is in flight: speak() waits on this same promise before it can
236
+ * dispatch, so the running stop already covers whatever the second request
237
+ * would have stopped. Serializing also keeps a late-settling player stop
238
+ * (the TTSPlayer contract allows the player-side cancellation to land when
239
+ * stop() settles, not when it was called) from cancelling a newer
240
+ * utterance, and keeps the cleanup in runStopPlayback() from acting on
241
+ * state that a newer utterance has since taken over.
242
+ */
243
+ stopPlayback() {
244
+ if (this.pendingStopPlayback) {
245
+ return this.pendingStopPlayback;
246
+ }
247
+ const stopping = this.runStopPlayback().finally(() => {
248
+ this.pendingStopPlayback = null;
249
+ });
250
+ this.pendingStopPlayback = stopping;
251
+ return stopping;
252
+ }
253
+ /**
254
+ * The actual stop: cancels the player, then cleans up this manager's own
255
+ * playback state. Resolves only once both have completed.
256
+ */
257
+ async runStopPlayback() {
185
258
  this.webSimulator.stopSimulation();
186
259
  try {
187
260
  await this.ttsPlayer.stop();
@@ -244,21 +317,96 @@ var TTSManagerImpl = class {
244
317
  );
245
318
  }
246
319
  }
320
+ createSpeakStartup(generation) {
321
+ let cancel;
322
+ const cancelled = new Promise((resolve) => {
323
+ cancel = resolve;
324
+ });
325
+ return {
326
+ cancelled,
327
+ cancel,
328
+ isCancelled: () => generation <= this.cancelledThrough
329
+ };
330
+ }
331
+ /**
332
+ * Invalidate every speak() claimed through `generation` and release the
333
+ * ones still starting up. Synchronous on purpose: callers must raise the
334
+ * watermark before they await anything. Every registered handle belongs to
335
+ * a generation at or below the caller's own claim, so all of them are
336
+ * cancelled here.
337
+ */
338
+ cancelStartupsThrough(generation) {
339
+ this.cancelledThrough = generation;
340
+ const cancelled = [...this.startupCancellations];
341
+ this.startupCancellations.clear();
342
+ for (const startup of cancelled) {
343
+ startup.cancel();
344
+ }
345
+ }
346
+ /**
347
+ * Await one of speak()'s startup operations, but give up as soon as this
348
+ * call's startup is cancelled -- the operation may hang forever, and its
349
+ * result must never start playback the caller no longer wants.
350
+ */
351
+ async raceStartup(operation, cancelled, isCancelled) {
352
+ const abandon = () => {
353
+ operation.then(noop, noop);
354
+ return { cancelled: true };
355
+ };
356
+ const outcome = await Promise.race([
357
+ cancelled.then(abandon),
358
+ operation.then(
359
+ // A stop() can land synchronously between the operation settling and
360
+ // this continuation running, so re-check before handing the result
361
+ // back. An uncancelled failure still propagates unchanged.
362
+ (value) => isCancelled() ? abandon() : { cancelled: false, value },
363
+ (error) => {
364
+ if (isCancelled()) {
365
+ return abandon();
366
+ }
367
+ throw error;
368
+ }
369
+ )
370
+ ]);
371
+ return outcome.cancelled ? STARTUP_CANCELLED : outcome.value;
372
+ }
373
+ /**
374
+ * Establish an utterance right before it dispatches speech: (1) register
375
+ * the callback stop() uses to settle it, (2) publish tts:audio:start, then
376
+ * (3) report whether the utterance may still dispatch.
377
+ *
378
+ * Registration precedes the emission because startAudioSession() emits
379
+ * synchronously: a listener may call stop() re-entrantly, and that stop
380
+ * must be able to settle this utterance deterministically. The re-check
381
+ * afterwards keeps the utterance from dispatching speech that the stop no
382
+ * longer controls.
383
+ */
384
+ beginUtterance(registerPendingStop, startup) {
385
+ registerPendingStop();
386
+ this.startAudioSession();
387
+ return !startup.isCancelled();
388
+ }
247
389
  /**
248
390
  * Handle the Web Speech API (simulated lip-sync)
249
391
  */
250
- async handleWebSpeech(text, options) {
392
+ async handleWebSpeech(text, options, startup) {
251
393
  const generation = ++this.webSpeechGeneration;
252
- this.startAudioSession();
253
- const effectiveRate = options?.rate !== void 0 ? Math.max(0.1, Math.min(10, options.rate)) : 1;
254
- this.webSimulator.startSimulation(text, effectiveRate);
255
394
  let settleThis;
395
+ let failThis;
396
+ const completion = new Promise((resolve, reject) => {
397
+ settleThis = resolve;
398
+ failThis = reject;
399
+ });
256
400
  try {
257
- await new Promise((resolve, reject) => {
258
- settleThis = resolve;
259
- this.pendingWebSpeechStop = resolve;
260
- this.ttsPlayer.speak(text, options).then(resolve, reject);
261
- });
401
+ const dispatch = this.beginUtterance(() => {
402
+ this.pendingWebSpeechStop = settleThis;
403
+ }, startup);
404
+ if (dispatch) {
405
+ const effectiveRate = options?.rate !== void 0 ? Math.max(0.1, Math.min(10, options.rate)) : 1;
406
+ this.webSimulator.startSimulation(text, effectiveRate);
407
+ this.ttsPlayer.speak(text, options).then(settleThis, failThis);
408
+ await completion;
409
+ }
262
410
  } finally {
263
411
  if (this.pendingWebSpeechStop === settleThis) {
264
412
  this.pendingWebSpeechStop = null;
@@ -272,12 +420,19 @@ var TTSManagerImpl = class {
272
420
  /**
273
421
  * Stateless audio handling
274
422
  */
275
- async handleStatelessAudio(text, options) {
276
- const audioData = await this.ttsPlayer.generateAudio(text, options).catch(
277
- (error) => Promise.reject(
278
- (0, import_core.toCharivoError)("provider", error, "Failed to generate TTS audio")
279
- )
423
+ async handleStatelessAudio(text, options, startup) {
424
+ const audioData = await this.raceStartup(
425
+ this.ttsPlayer.generateAudio(text, options).catch(
426
+ (error) => Promise.reject(
427
+ (0, import_core.toCharivoError)("provider", error, "Failed to generate TTS audio")
428
+ )
429
+ ),
430
+ startup.cancelled,
431
+ startup.isCancelled
280
432
  );
433
+ if (audioData === STARTUP_CANCELLED) {
434
+ return;
435
+ }
281
436
  const mimeType = getTTSAudioMimeType(this.ttsPlayer);
282
437
  const blob = new Blob([audioData], { type: mimeType });
283
438
  const audioUrl = URL.createObjectURL(blob);
@@ -290,7 +445,6 @@ var TTSManagerImpl = class {
290
445
  }
291
446
  this.ensureLifecycleBound();
292
447
  this.lipSync.attachMediaElement(audio);
293
- this.startAudioSession();
294
448
  let isFinalized = false;
295
449
  const finalize = (next) => {
296
450
  if (isFinalized) return;
@@ -305,13 +459,19 @@ var TTSManagerImpl = class {
305
459
  this.endAudioSession();
306
460
  next();
307
461
  };
308
- this.pendingStatelessStop = () => finalize(resolve);
309
462
  audio.onended = () => {
310
463
  finalize(resolve);
311
464
  };
312
465
  audio.onerror = () => {
313
466
  finalize(() => reject(new Error("Audio playback failed")));
314
467
  };
468
+ const dispatch = this.beginUtterance(() => {
469
+ this.pendingStatelessStop = () => finalize(resolve);
470
+ }, startup);
471
+ if (!dispatch) {
472
+ finalize(resolve);
473
+ return;
474
+ }
315
475
  audio.play().catch((error) => {
316
476
  finalize(
317
477
  () => reject(
package/dist/index.mjs CHANGED
@@ -87,6 +87,8 @@ function supportsGenerateAudio(player) {
87
87
  }
88
88
 
89
89
  // src/tts-manager.ts
90
+ var STARTUP_CANCELLED = Symbol("charivo.tts.startupCancelled");
91
+ var noop = () => void 0;
90
92
  var TTSManagerImpl = class {
91
93
  ttsPlayer;
92
94
  eventEmitter;
@@ -116,6 +118,24 @@ var TTSManagerImpl = class {
116
118
  // still matches the current one, so a stale completion can never act on
117
119
  // a session that isn't its own.
118
120
  webSpeechGeneration = 0;
121
+ // Claim counter for speak() calls. A call claims its generation
122
+ // synchronously at entry, before its first await, so any stop() that lands
123
+ // afterwards can invalidate it -- even while it is still starting up and
124
+ // has no playback for stop() to cancel.
125
+ speakGeneration = 0;
126
+ // Every speak() generation up to and including this one is invalidated: it
127
+ // must not start playback, even if its startup operation succeeds. Raised
128
+ // by stop() (through the current claim) and by a newer speak() (through its
129
+ // predecessor's claim).
130
+ cancelledThrough = 0;
131
+ // Startup handles of the speak() calls that have claimed a generation and
132
+ // have not finished yet. Resolving one releases a speak() that would
133
+ // otherwise stay parked on a startup operation that may hang forever.
134
+ startupCancellations = /* @__PURE__ */ new Set();
135
+ // The stop currently running, if any. Stops are single-flight: overlapping
136
+ // requests share one player stop and one cleanup, and no new utterance can
137
+ // dispatch until it has fully settled.
138
+ pendingStopPlayback = null;
119
139
  // Only the Web Speech lip-sync simulation is needed
120
140
  webSimulator;
121
141
  // Reads the emitter at emit time, so a later setEventEmitter() still applies.
@@ -141,26 +161,79 @@ var TTSManagerImpl = class {
141
161
  this.webSimulator = new WebSpeechLipSyncSimulator(eventEmitter);
142
162
  }
143
163
  /**
144
- * Convert text to speech and play it
164
+ * Convert text to speech and play it.
165
+ *
166
+ * Resolves when the utterance finishes -- or silently, without speaking, if
167
+ * stop() or a newer speak() lands while this call is still starting up (the
168
+ * pre-speech stop, or synthesis). A cancelled call never begins playback,
169
+ * whether or not it had already opened an audio session (a reentrant stop
170
+ * from the tts:audio:start listener still sees the session close), so a
171
+ * resolved speak() is not proof audio played.
145
172
  */
146
173
  async speak(text, options) {
174
+ const generation = ++this.speakGeneration;
175
+ this.cancelStartupsThrough(generation - 1);
176
+ const startup = this.createSpeakStartup(generation);
177
+ this.startupCancellations.add(startup);
147
178
  try {
148
- await this.stop().catch((error) => {
149
- throw toCharivoError("provider", error, "Failed to stop active TTS");
150
- });
179
+ const stopped = await this.raceStartup(
180
+ this.stopPlayback().catch((error) => {
181
+ throw toCharivoError("provider", error, "Failed to stop active TTS");
182
+ }),
183
+ startup.cancelled,
184
+ startup.isCancelled
185
+ );
186
+ if (stopped === STARTUP_CANCELLED) {
187
+ return;
188
+ }
151
189
  if (this.playbackMode === "web-speech") {
152
- return await this.handleWebSpeech(text, options);
190
+ return await this.handleWebSpeech(text, options, startup);
153
191
  } else {
154
- return await this.handleStatelessAudio(text, options);
192
+ return await this.handleStatelessAudio(text, options, startup);
155
193
  }
156
194
  } catch (error) {
157
195
  throw toCharivoError("provider", error, "Failed to speak text");
196
+ } finally {
197
+ this.startupCancellations.delete(startup);
158
198
  }
159
199
  }
160
200
  /**
161
- * Stop the currently playing speech
201
+ * Stop the currently playing speech, and cancel every speak() still
202
+ * starting up -- those calls resolve silently instead of speaking after
203
+ * this stop.
162
204
  */
163
205
  async stop() {
206
+ this.cancelStartupsThrough(this.speakGeneration);
207
+ await this.stopPlayback();
208
+ }
209
+ /**
210
+ * Stop playback, single-flight: a request issued while another stop is
211
+ * still running joins it instead of starting a second one.
212
+ *
213
+ * Coalescing is safe because no new playback can be established while a
214
+ * stop is in flight: speak() waits on this same promise before it can
215
+ * dispatch, so the running stop already covers whatever the second request
216
+ * would have stopped. Serializing also keeps a late-settling player stop
217
+ * (the TTSPlayer contract allows the player-side cancellation to land when
218
+ * stop() settles, not when it was called) from cancelling a newer
219
+ * utterance, and keeps the cleanup in runStopPlayback() from acting on
220
+ * state that a newer utterance has since taken over.
221
+ */
222
+ stopPlayback() {
223
+ if (this.pendingStopPlayback) {
224
+ return this.pendingStopPlayback;
225
+ }
226
+ const stopping = this.runStopPlayback().finally(() => {
227
+ this.pendingStopPlayback = null;
228
+ });
229
+ this.pendingStopPlayback = stopping;
230
+ return stopping;
231
+ }
232
+ /**
233
+ * The actual stop: cancels the player, then cleans up this manager's own
234
+ * playback state. Resolves only once both have completed.
235
+ */
236
+ async runStopPlayback() {
164
237
  this.webSimulator.stopSimulation();
165
238
  try {
166
239
  await this.ttsPlayer.stop();
@@ -223,21 +296,96 @@ var TTSManagerImpl = class {
223
296
  );
224
297
  }
225
298
  }
299
+ createSpeakStartup(generation) {
300
+ let cancel;
301
+ const cancelled = new Promise((resolve) => {
302
+ cancel = resolve;
303
+ });
304
+ return {
305
+ cancelled,
306
+ cancel,
307
+ isCancelled: () => generation <= this.cancelledThrough
308
+ };
309
+ }
310
+ /**
311
+ * Invalidate every speak() claimed through `generation` and release the
312
+ * ones still starting up. Synchronous on purpose: callers must raise the
313
+ * watermark before they await anything. Every registered handle belongs to
314
+ * a generation at or below the caller's own claim, so all of them are
315
+ * cancelled here.
316
+ */
317
+ cancelStartupsThrough(generation) {
318
+ this.cancelledThrough = generation;
319
+ const cancelled = [...this.startupCancellations];
320
+ this.startupCancellations.clear();
321
+ for (const startup of cancelled) {
322
+ startup.cancel();
323
+ }
324
+ }
325
+ /**
326
+ * Await one of speak()'s startup operations, but give up as soon as this
327
+ * call's startup is cancelled -- the operation may hang forever, and its
328
+ * result must never start playback the caller no longer wants.
329
+ */
330
+ async raceStartup(operation, cancelled, isCancelled) {
331
+ const abandon = () => {
332
+ operation.then(noop, noop);
333
+ return { cancelled: true };
334
+ };
335
+ const outcome = await Promise.race([
336
+ cancelled.then(abandon),
337
+ operation.then(
338
+ // A stop() can land synchronously between the operation settling and
339
+ // this continuation running, so re-check before handing the result
340
+ // back. An uncancelled failure still propagates unchanged.
341
+ (value) => isCancelled() ? abandon() : { cancelled: false, value },
342
+ (error) => {
343
+ if (isCancelled()) {
344
+ return abandon();
345
+ }
346
+ throw error;
347
+ }
348
+ )
349
+ ]);
350
+ return outcome.cancelled ? STARTUP_CANCELLED : outcome.value;
351
+ }
352
+ /**
353
+ * Establish an utterance right before it dispatches speech: (1) register
354
+ * the callback stop() uses to settle it, (2) publish tts:audio:start, then
355
+ * (3) report whether the utterance may still dispatch.
356
+ *
357
+ * Registration precedes the emission because startAudioSession() emits
358
+ * synchronously: a listener may call stop() re-entrantly, and that stop
359
+ * must be able to settle this utterance deterministically. The re-check
360
+ * afterwards keeps the utterance from dispatching speech that the stop no
361
+ * longer controls.
362
+ */
363
+ beginUtterance(registerPendingStop, startup) {
364
+ registerPendingStop();
365
+ this.startAudioSession();
366
+ return !startup.isCancelled();
367
+ }
226
368
  /**
227
369
  * Handle the Web Speech API (simulated lip-sync)
228
370
  */
229
- async handleWebSpeech(text, options) {
371
+ async handleWebSpeech(text, options, startup) {
230
372
  const generation = ++this.webSpeechGeneration;
231
- this.startAudioSession();
232
- const effectiveRate = options?.rate !== void 0 ? Math.max(0.1, Math.min(10, options.rate)) : 1;
233
- this.webSimulator.startSimulation(text, effectiveRate);
234
373
  let settleThis;
374
+ let failThis;
375
+ const completion = new Promise((resolve, reject) => {
376
+ settleThis = resolve;
377
+ failThis = reject;
378
+ });
235
379
  try {
236
- await new Promise((resolve, reject) => {
237
- settleThis = resolve;
238
- this.pendingWebSpeechStop = resolve;
239
- this.ttsPlayer.speak(text, options).then(resolve, reject);
240
- });
380
+ const dispatch = this.beginUtterance(() => {
381
+ this.pendingWebSpeechStop = settleThis;
382
+ }, startup);
383
+ if (dispatch) {
384
+ const effectiveRate = options?.rate !== void 0 ? Math.max(0.1, Math.min(10, options.rate)) : 1;
385
+ this.webSimulator.startSimulation(text, effectiveRate);
386
+ this.ttsPlayer.speak(text, options).then(settleThis, failThis);
387
+ await completion;
388
+ }
241
389
  } finally {
242
390
  if (this.pendingWebSpeechStop === settleThis) {
243
391
  this.pendingWebSpeechStop = null;
@@ -251,12 +399,19 @@ var TTSManagerImpl = class {
251
399
  /**
252
400
  * Stateless audio handling
253
401
  */
254
- async handleStatelessAudio(text, options) {
255
- const audioData = await this.ttsPlayer.generateAudio(text, options).catch(
256
- (error) => Promise.reject(
257
- toCharivoError("provider", error, "Failed to generate TTS audio")
258
- )
402
+ async handleStatelessAudio(text, options, startup) {
403
+ const audioData = await this.raceStartup(
404
+ this.ttsPlayer.generateAudio(text, options).catch(
405
+ (error) => Promise.reject(
406
+ toCharivoError("provider", error, "Failed to generate TTS audio")
407
+ )
408
+ ),
409
+ startup.cancelled,
410
+ startup.isCancelled
259
411
  );
412
+ if (audioData === STARTUP_CANCELLED) {
413
+ return;
414
+ }
260
415
  const mimeType = getTTSAudioMimeType(this.ttsPlayer);
261
416
  const blob = new Blob([audioData], { type: mimeType });
262
417
  const audioUrl = URL.createObjectURL(blob);
@@ -269,7 +424,6 @@ var TTSManagerImpl = class {
269
424
  }
270
425
  this.ensureLifecycleBound();
271
426
  this.lipSync.attachMediaElement(audio);
272
- this.startAudioSession();
273
427
  let isFinalized = false;
274
428
  const finalize = (next) => {
275
429
  if (isFinalized) return;
@@ -284,13 +438,19 @@ var TTSManagerImpl = class {
284
438
  this.endAudioSession();
285
439
  next();
286
440
  };
287
- this.pendingStatelessStop = () => finalize(resolve);
288
441
  audio.onended = () => {
289
442
  finalize(resolve);
290
443
  };
291
444
  audio.onerror = () => {
292
445
  finalize(() => reject(new Error("Audio playback failed")));
293
446
  };
447
+ const dispatch = this.beginUtterance(() => {
448
+ this.pendingStatelessStop = () => finalize(resolve);
449
+ }, startup);
450
+ if (!dispatch) {
451
+ finalize(resolve);
452
+ return;
453
+ }
294
454
  audio.play().catch((error) => {
295
455
  finalize(
296
456
  () => reject(
@@ -24,7 +24,6 @@ __export(remote_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(remote_exports);
26
26
  var import_core = require("@charivo/core");
27
- var REQUEST_TIMEOUT_MS = 3e4;
28
27
  var RemoteTTSPlayer = class {
29
28
  playbackMode = "audio";
30
29
  audioMimeType = "audio/wav";
@@ -38,7 +37,7 @@ var RemoteTTSPlayer = class {
38
37
  * Stateless audio generation (used by the TTS Manager)
39
38
  */
40
39
  async generateAudio(text, options) {
41
- const response = await fetchWithTimeout(
40
+ const response = await (0, import_core.fetchWithTimeout)(
42
41
  this.apiEndpoint,
43
42
  {
44
43
  method: "POST",
@@ -50,7 +49,10 @@ var RemoteTTSPlayer = class {
50
49
  format: "wav"
51
50
  })
52
51
  },
53
- `TTS request timed out after ${REQUEST_TIMEOUT_MS}ms`
52
+ {
53
+ timeoutMessage: `TTS request timed out after ${import_core.DEFAULT_FETCH_TIMEOUT_MS}ms`,
54
+ failureMessage: "TTS request failed"
55
+ }
54
56
  );
55
57
  if (!response.ok) {
56
58
  throw new import_core.CharivoProviderError(`TTS API failed: ${response.statusText}`);
@@ -92,28 +94,6 @@ var RemoteTTSPlayer = class {
92
94
  function createRemoteTTSPlayer(config) {
93
95
  return new RemoteTTSPlayer(config);
94
96
  }
95
- async function fetchWithTimeout(input, init, timeoutMessage) {
96
- const controller = new AbortController();
97
- const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
98
- try {
99
- return await fetch(input, {
100
- ...init,
101
- signal: controller.signal
102
- });
103
- } catch (error) {
104
- if (isAbortError(error)) {
105
- throw new import_core.CharivoTimeoutError(timeoutMessage, { cause: error });
106
- }
107
- throw new import_core.CharivoTransportError("TTS request failed", {
108
- cause: error instanceof Error ? error : void 0
109
- });
110
- } finally {
111
- clearTimeout(timeoutId);
112
- }
113
- }
114
- function isAbortError(error) {
115
- return error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
116
- }
117
97
  // Annotate the CommonJS export names for ESM import in node:
118
98
  0 && (module.exports = {
119
99
  createRemoteTTSPlayer
@@ -1,10 +1,10 @@
1
1
  // src/remote/index.ts
2
2
  import {
3
3
  CharivoProviderError,
4
- CharivoTimeoutError,
5
- CharivoTransportError
4
+ CharivoTransportError,
5
+ DEFAULT_FETCH_TIMEOUT_MS,
6
+ fetchWithTimeout
6
7
  } from "@charivo/core";
7
- var REQUEST_TIMEOUT_MS = 3e4;
8
8
  var RemoteTTSPlayer = class {
9
9
  playbackMode = "audio";
10
10
  audioMimeType = "audio/wav";
@@ -30,7 +30,10 @@ var RemoteTTSPlayer = class {
30
30
  format: "wav"
31
31
  })
32
32
  },
33
- `TTS request timed out after ${REQUEST_TIMEOUT_MS}ms`
33
+ {
34
+ timeoutMessage: `TTS request timed out after ${DEFAULT_FETCH_TIMEOUT_MS}ms`,
35
+ failureMessage: "TTS request failed"
36
+ }
34
37
  );
35
38
  if (!response.ok) {
36
39
  throw new CharivoProviderError(`TTS API failed: ${response.statusText}`);
@@ -72,28 +75,6 @@ var RemoteTTSPlayer = class {
72
75
  function createRemoteTTSPlayer(config) {
73
76
  return new RemoteTTSPlayer(config);
74
77
  }
75
- async function fetchWithTimeout(input, init, timeoutMessage) {
76
- const controller = new AbortController();
77
- const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
78
- try {
79
- return await fetch(input, {
80
- ...init,
81
- signal: controller.signal
82
- });
83
- } catch (error) {
84
- if (isAbortError(error)) {
85
- throw new CharivoTimeoutError(timeoutMessage, { cause: error });
86
- }
87
- throw new CharivoTransportError("TTS request failed", {
88
- cause: error instanceof Error ? error : void 0
89
- });
90
- } finally {
91
- clearTimeout(timeoutId);
92
- }
93
- }
94
- function isAbortError(error) {
95
- return error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
96
- }
97
78
  export {
98
79
  createRemoteTTSPlayer
99
80
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charivo/tts",
3
- "version": "0.6.9",
3
+ "version": "0.6.11",
4
4
  "description": "TTS manager and browser adapters for Charivo",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -36,7 +36,7 @@
36
36
  ],
37
37
  "dependencies": {
38
38
  "openai": "^4.47.1",
39
- "@charivo/core": "^0.25.0"
39
+ "@charivo/core": "^0.27.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "tsup": "^8.5.0",