@charivo/tts 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -106,7 +106,11 @@ async function withTimeout(promise, timeoutMessage) {
106
106
  // src/openai/index.ts
107
107
  var OpenAITTSPlayer = class {
108
108
  playbackMode = "audio";
109
- audioMimeType = "audio/wav";
109
+ // The API answers with its mp3 default: `OpenAITTSProvider` sends
110
+ // `format: "wav"`, which is not the parameter the API reads -- that is
111
+ // `response_format` -- so it is carried in the body and ignored.
112
+ // `tests/live-tts` pins the container this actually yields.
113
+ audioMimeType = "audio/mpeg";
110
114
  provider;
111
115
  constructor(config) {
112
116
  this.provider = createOpenAITTSProvider({
@@ -125,7 +129,7 @@ var OpenAITTSPlayer = class {
125
129
  */
126
130
  async speak(text, options) {
127
131
  const audioBuffer = await this.generateAudio(text, options);
128
- const blob = new Blob([audioBuffer], { type: "audio/mp3" });
132
+ const blob = new Blob([audioBuffer], { type: this.audioMimeType });
129
133
  const audioUrl = URL.createObjectURL(blob);
130
134
  return new Promise((resolve, reject) => {
131
135
  const audio = new Audio(audioUrl);
@@ -72,7 +72,11 @@ async function withTimeout(promise, timeoutMessage) {
72
72
  // src/openai/index.ts
73
73
  var OpenAITTSPlayer = class {
74
74
  playbackMode = "audio";
75
- audioMimeType = "audio/wav";
75
+ // The API answers with its mp3 default: `OpenAITTSProvider` sends
76
+ // `format: "wav"`, which is not the parameter the API reads -- that is
77
+ // `response_format` -- so it is carried in the body and ignored.
78
+ // `tests/live-tts` pins the container this actually yields.
79
+ audioMimeType = "audio/mpeg";
76
80
  provider;
77
81
  constructor(config) {
78
82
  this.provider = createOpenAITTSProvider({
@@ -91,7 +95,7 @@ var OpenAITTSPlayer = class {
91
95
  */
92
96
  async speak(text, options) {
93
97
  const audioBuffer = await this.generateAudio(text, options);
94
- const blob = new Blob([audioBuffer], { type: "audio/mp3" });
98
+ const blob = new Blob([audioBuffer], { type: this.audioMimeType });
95
99
  const audioUrl = URL.createObjectURL(blob);
96
100
  return new Promise((resolve, reject) => {
97
101
  const audio = new Audio(audioUrl);
@@ -26,6 +26,12 @@ module.exports = __toCommonJS(remote_exports);
26
26
  var import_core = require("@charivo/core");
27
27
  var RemoteTTSPlayer = class {
28
28
  playbackMode = "audio";
29
+ /**
30
+ * Follows what the server said it sent, because the endpoint decides the
31
+ * container: the demo's OpenAI route answers with MPEG and its Gemini one
32
+ * with WAV. Naming either here would mislabel the other. Until a response
33
+ * has been seen this is WAV, matching the manager's own fallback.
34
+ */
29
35
  audioMimeType = "audio/wav";
30
36
  apiEndpoint;
31
37
  defaultVoice;
@@ -37,7 +43,7 @@ var RemoteTTSPlayer = class {
37
43
  * Stateless audio generation (used by the TTS Manager)
38
44
  */
39
45
  async generateAudio(text, options) {
40
- const response = await (0, import_core.fetchWithTimeout)(
46
+ return (0, import_core.fetchWithTimeout)(
41
47
  this.apiEndpoint,
42
48
  {
43
49
  method: "POST",
@@ -52,19 +58,30 @@ var RemoteTTSPlayer = class {
52
58
  {
53
59
  timeoutMessage: `TTS request timed out after ${import_core.DEFAULT_FETCH_TIMEOUT_MS}ms`,
54
60
  failureMessage: "TTS request failed"
61
+ },
62
+ // Consume the body while cancellation is still wired up, so a route that
63
+ // sends headers and then stalls -- on the error path too, where the
64
+ // message is read off the body -- still hits the deadline.
65
+ async (response) => {
66
+ if (!response.ok) {
67
+ throw new import_core.CharivoProviderError(
68
+ `TTS API failed: ${await (0, import_core.readResponseErrorMessage)(response)}`
69
+ );
70
+ }
71
+ const contentType = response.headers.get("content-type");
72
+ if (contentType) {
73
+ this.audioMimeType = contentType.split(";")[0].trim();
74
+ }
75
+ return response.arrayBuffer();
55
76
  }
56
77
  );
57
- if (!response.ok) {
58
- throw new import_core.CharivoProviderError(`TTS API failed: ${response.statusText}`);
59
- }
60
- return response.arrayBuffer();
61
78
  }
62
79
  /**
63
80
  * Legacy speak method (kept for compatibility)
64
81
  */
65
82
  async speak(text, options) {
66
83
  const audioBuffer = await this.generateAudio(text, options);
67
- const blob = new Blob([audioBuffer], { type: "audio/wav" });
84
+ const blob = new Blob([audioBuffer], { type: this.audioMimeType });
68
85
  const audioUrl = URL.createObjectURL(blob);
69
86
  return new Promise((resolve, reject) => {
70
87
  const audio = new Audio(audioUrl);
@@ -3,10 +3,17 @@ import {
3
3
  CharivoProviderError,
4
4
  CharivoTransportError,
5
5
  DEFAULT_FETCH_TIMEOUT_MS,
6
- fetchWithTimeout
6
+ fetchWithTimeout,
7
+ readResponseErrorMessage
7
8
  } from "@charivo/core";
8
9
  var RemoteTTSPlayer = class {
9
10
  playbackMode = "audio";
11
+ /**
12
+ * Follows what the server said it sent, because the endpoint decides the
13
+ * container: the demo's OpenAI route answers with MPEG and its Gemini one
14
+ * with WAV. Naming either here would mislabel the other. Until a response
15
+ * has been seen this is WAV, matching the manager's own fallback.
16
+ */
10
17
  audioMimeType = "audio/wav";
11
18
  apiEndpoint;
12
19
  defaultVoice;
@@ -18,7 +25,7 @@ var RemoteTTSPlayer = class {
18
25
  * Stateless audio generation (used by the TTS Manager)
19
26
  */
20
27
  async generateAudio(text, options) {
21
- const response = await fetchWithTimeout(
28
+ return fetchWithTimeout(
22
29
  this.apiEndpoint,
23
30
  {
24
31
  method: "POST",
@@ -33,19 +40,30 @@ var RemoteTTSPlayer = class {
33
40
  {
34
41
  timeoutMessage: `TTS request timed out after ${DEFAULT_FETCH_TIMEOUT_MS}ms`,
35
42
  failureMessage: "TTS request failed"
43
+ },
44
+ // Consume the body while cancellation is still wired up, so a route that
45
+ // sends headers and then stalls -- on the error path too, where the
46
+ // message is read off the body -- still hits the deadline.
47
+ async (response) => {
48
+ if (!response.ok) {
49
+ throw new CharivoProviderError(
50
+ `TTS API failed: ${await readResponseErrorMessage(response)}`
51
+ );
52
+ }
53
+ const contentType = response.headers.get("content-type");
54
+ if (contentType) {
55
+ this.audioMimeType = contentType.split(";")[0].trim();
56
+ }
57
+ return response.arrayBuffer();
36
58
  }
37
59
  );
38
- if (!response.ok) {
39
- throw new CharivoProviderError(`TTS API failed: ${response.statusText}`);
40
- }
41
- return response.arrayBuffer();
42
60
  }
43
61
  /**
44
62
  * Legacy speak method (kept for compatibility)
45
63
  */
46
64
  async speak(text, options) {
47
65
  const audioBuffer = await this.generateAudio(text, options);
48
- const blob = new Blob([audioBuffer], { type: "audio/wav" });
66
+ const blob = new Blob([audioBuffer], { type: this.audioMimeType });
49
67
  const audioUrl = URL.createObjectURL(blob);
50
68
  return new Promise((resolve, reject) => {
51
69
  const audio = new Audio(audioUrl);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charivo/tts",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "TTS manager and browser adapters for Charivo",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -40,8 +40,8 @@
40
40
  "lipsync"
41
41
  ],
42
42
  "dependencies": {
43
- "openai": "^4.47.1",
44
- "@charivo/core": "^0.33.0"
43
+ "@charivo/core": "^0.34.0",
44
+ "openai": "^4.47.1"
45
45
  },
46
46
  "devDependencies": {
47
47
  "tsup": "^8.5.0",