tz-voice-chat 1.2.1 → 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.
@@ -18,7 +18,7 @@
18
18
  }
19
19
  }
20
20
  </script>
21
- <script type="module" crossorigin src="/assets/index-Wcfqx97N.js"></script>
21
+ <script type="module" crossorigin src="/assets/index-KxBxd4q9.js"></script>
22
22
  <link rel="stylesheet" crossorigin href="/assets/index-Bcmhe9G_.css">
23
23
  </head>
24
24
  <body>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tz-voice-chat",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "Voice assistant: React hook + UI and a ready-to-run server/CLI. VAD → Whisper STT → LLM → Kokoro/OpenAI/WebSpeech TTS, configurable from a Settings page.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
package/server/index.js CHANGED
@@ -30,6 +30,7 @@ import { createSettingsStore } from './settings.js';
30
30
  import { streamOpenAI, streamAnthropic, streamOpenAICompatible } from './chat.js';
31
31
  import { transcribeElevenLabs, synthesizeElevenLabs } from './elevenlabs.js';
32
32
  import { transcribeOpenAI } from './openai-stt.js';
33
+ import { synthesizeOpenAICompatible, parseBaseUrl } from './openai-tts.js';
33
34
 
34
35
  /**
35
36
  * @param {object} [options]
@@ -93,6 +94,8 @@ export function createVoiceAssistantRouter({ settingsFile, defaults = {} } = {})
93
94
  kokoroVoice: o.kokoroVoice,
94
95
  openaiTtsModel: o.openaiTtsModel,
95
96
  openaiTtsVoice: o.openaiTtsVoice,
97
+ ttsCompatModel: o.ttsCompatModel,
98
+ ttsCompatVoice: o.ttsCompatVoice,
96
99
  webSpeechVoice: o.webSpeechVoice,
97
100
  webSpeechRate: o.webSpeechRate,
98
101
  webSpeechPitch: o.webSpeechPitch,
@@ -274,6 +277,28 @@ export function createVoiceAssistantRouter({ settingsFile, defaults = {} } = {})
274
277
  return res.end();
275
278
  }
276
279
 
280
+ if (o.ttsProvider === 'openai-compatible') {
281
+ const base = parseBaseUrl(o.ttsCompatBaseUrl);
282
+ if (!base) return res.status(400).send('The OpenAI-compatible TTS server URL is missing or invalid — fix it in Settings');
283
+ let out;
284
+ try {
285
+ out = await synthesizeOpenAICompatible({
286
+ baseUrl: base.href,
287
+ apiKey: o.ttsCompatApiKey,
288
+ model: o.ttsCompatModel || 'tts-1',
289
+ voice: voice || o.ttsCompatVoice || 'alloy',
290
+ format: o.ttsCompatFormat,
291
+ text,
292
+ });
293
+ } catch (err) {
294
+ return res.status(502).send(`Could not reach the OpenAI-compatible TTS server at ${base.origin}: ${err.cause?.code || err.message}`);
295
+ }
296
+ if (!out.ok) return res.status(out.status).send(out.body);
297
+ res.setHeader('Content-Type', out.contentType);
298
+ res.setHeader('Content-Length', String(out.audio.length));
299
+ return res.end(out.audio);
300
+ }
301
+
277
302
  if (o.ttsProvider === 'elevenlabs') {
278
303
  if (!o.elevenlabsApiKey) return res.status(500).send('ElevenLabs API key is missing — set it in Settings');
279
304
  const out = await synthesizeElevenLabs({
@@ -0,0 +1,39 @@
1
+ /**
2
+ * OpenAI-compatible text-to-speech: POST <baseUrl>/audio/speech with { model, input, voice, response_format }.
3
+ * Works with OpenAI itself and with any server that speaks the same protocol (LocalAI, openedai-speech,
4
+ * Kokoro-FastAPI, Speaches, an on-premise gateway…), e.g. to run Piper voices behind it.
5
+ */
6
+
7
+ const MIME = { mp3: 'audio/mpeg', wav: 'audio/wav', opus: 'audio/ogg', aac: 'audio/aac', flac: 'audio/flac' };
8
+
9
+ /** @returns {URL | null} the parsed http(s) base URL, or null when it is not usable */
10
+ export function parseBaseUrl(value) {
11
+ try {
12
+ const url = new URL(value);
13
+ return /^https?:$/.test(url.protocol) ? url : null;
14
+ } catch (_) {
15
+ return null;
16
+ }
17
+ }
18
+
19
+ /**
20
+ * @returns {Promise<{ok: true, audio: Buffer, contentType: string} | {ok: false, status: number, body: string}>}
21
+ */
22
+ export async function synthesizeOpenAICompatible({ baseUrl, apiKey, model, voice, format, text }) {
23
+ const body = { model, input: text, voice };
24
+ if (format) body.response_format = format;
25
+ const headers = { 'Content-Type': 'application/json' };
26
+ if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
27
+
28
+ const res = await fetch(`${baseUrl.replace(/\/$/, '')}/audio/speech`, {
29
+ method: 'POST',
30
+ headers,
31
+ body: JSON.stringify(body),
32
+ });
33
+ if (!res.ok) return { ok: false, status: res.status, body: await res.text() };
34
+ const audio = Buffer.from(await res.arrayBuffer());
35
+ // Trust the server's Content-Type unless it is generic; otherwise derive it from the requested format.
36
+ const upstream = res.headers.get('content-type') || '';
37
+ const contentType = /^audio\//.test(upstream) ? upstream : MIME[format] || 'audio/mpeg';
38
+ return { ok: true, audio, contentType };
39
+ }
@@ -105,6 +105,7 @@ export const SETTINGS_SCHEMA = [
105
105
  ['kokoro', 'Kokoro in the browser', LOCAL],
106
106
  ['server-kokoro', 'Kokoro on the server (needs: npm install kokoro-js)', LOCAL],
107
107
  ['webspeech', 'Browser Web Speech API (built-in voices)', LOCAL],
108
+ ['openai-compatible', 'OpenAI-compatible server (Piper, LocalAI, Kokoro-FastAPI… any /audio/speech)', LOCAL],
108
109
  ['openai', 'OpenAI TTS', CLOUD],
109
110
  ['elevenlabs', 'ElevenLabs', CLOUD],
110
111
  ],
@@ -124,6 +125,17 @@ export const SETTINGS_SCHEMA = [
124
125
  showWhen: { ttsProvider: 'openai' }, suggestions: ['tts-1', 'tts-1-hd', 'gpt-4o-mini-tts'] },
125
126
  { key: 'openaiTtsVoice', label: 'OpenAI voice', type: 'text', env: 'OPENAI_TTS_VOICE', default: 'alloy',
126
127
  showWhen: { ttsProvider: 'openai' }, suggestions: ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'] },
128
+ { key: 'ttsCompatBaseUrl', label: 'TTS server base URL', type: 'text', env: 'TTS_COMPAT_BASE_URL', default: 'http://localhost:8000/v1',
129
+ showWhen: { ttsProvider: 'openai-compatible' }, suggestions: ['http://localhost:8000/v1', 'http://localhost:8080/v1', 'http://localhost:8880/v1'],
130
+ hint: 'Must serve POST /audio/speech (the OpenAI text-to-speech protocol). From inside Docker use host.docker.internal instead of localhost.' },
131
+ { key: 'ttsCompatModel', label: 'TTS model name', type: 'text', env: 'TTS_COMPAT_MODEL', default: 'tts-1',
132
+ showWhen: { ttsProvider: 'openai-compatible' }, hint: 'Exactly as the server names it — e.g. tts-1, or a Piper voice such as en_US-lessac-medium' },
133
+ { key: 'ttsCompatVoice', label: 'TTS voice', type: 'text', env: 'TTS_COMPAT_VOICE', default: 'alloy',
134
+ showWhen: { ttsProvider: 'openai-compatible' }, hint: 'Voice name the server understands (alloy, af_bella, a Piper voice…)' },
135
+ { key: 'ttsCompatFormat', label: 'Audio format', type: 'select', env: 'TTS_COMPAT_FORMAT', default: '',
136
+ showWhen: { ttsProvider: 'openai-compatible' }, options: [['', 'Server default'], ['mp3', 'mp3'], ['wav', 'wav (no encoding delay)']] },
137
+ { key: 'ttsCompatApiKey', label: 'TTS API key (optional)', type: 'password', secret: true, env: 'TTS_COMPAT_API_KEY',
138
+ showWhen: { ttsProvider: 'openai-compatible' }, hint: 'Leave empty if the server needs no key (most local servers).' },
127
139
  { key: 'elevenlabsVoiceId', label: 'ElevenLabs voice ID', type: 'text', env: 'ELEVENLABS_VOICE_ID', default: '21m00Tcm4TlvDq8ikWAM',
128
140
  showWhen: { ttsProvider: 'elevenlabs' }, hint: 'Default is the premade "Rachel" voice. Copy another voice\'s ID from your ElevenLabs voice library.' },
129
141
  { key: 'elevenlabsTtsModel', label: 'ElevenLabs model', type: 'text', env: 'ELEVENLABS_TTS_MODEL', default: 'eleven_flash_v2_5',
@@ -185,6 +197,10 @@ export function computeMissing(values, secretsSet = {}) {
185
197
  if (!values.compatModel) missing.push({ key: 'compatModel', message: 'The model name for the OpenAI-compatible server is missing.', scope: 'chat' });
186
198
  }
187
199
 
200
+ if (values.ttsProvider === 'openai-compatible' && !values.ttsCompatBaseUrl) {
201
+ missing.push({ key: 'ttsCompatBaseUrl', message: 'The OpenAI-compatible text-to-speech server URL is missing.', scope: 'tts' });
202
+ }
203
+
188
204
  const needs = [
189
205
  { key: 'openrouterApiKey', name: 'OpenRouter API key', uses: [p === 'openrouter' && ['chat', 'chat']] },
190
206
  { key: 'anthropicApiKey', name: 'Anthropic API key', uses: [p === 'anthropic' && ['chat', 'chat']] },