diff --git a/cptr/frontend/src/lib/components/chat/ChatInput.svelte b/cptr/frontend/src/lib/components/chat/ChatInput.svelte index 0549419..1a4cb6c 100644 --- a/cptr/frontend/src/lib/components/chat/ChatInput.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatInput.svelte @@ -7,6 +7,7 @@ import Placeholder from '@tiptap/extension-placeholder'; import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'; import { all, createLowlight } from 'lowlight'; + import { toast } from 'svelte-sonner'; import { createFileMention, extractMentionedFiles, type FileMentionAttrs } from './FileMention'; import { createSkillMention, extractMentionedSkills, type SkillMentionAttrs } from './SkillMention'; @@ -583,12 +584,12 @@ }, delay); } - function stopVoiceRecognition() { + function stopVoiceRecognition(stopCapture = true) { clearVoiceRestartTimer(); const recognition = voiceRecognition; voiceRecognition = null; voiceStopRequested = true; - stopVoiceCapture(); + if (stopCapture) stopVoiceCapture(); try { recognition?.stop(); } catch {} @@ -635,9 +636,9 @@ const contentType = voiceCaptureMimeType || 'audio/webm'; voiceCaptureRecorder = null; voiceCaptureStream = null; - voiceCaptureChunks = []; if (!recorder) { + voiceCaptureChunks = []; stream?.getTracks().forEach((track) => track.stop()); return null; } @@ -652,6 +653,7 @@ } }); stream?.getTracks().forEach((track) => track.stop()); + voiceCaptureChunks = []; if (!chunks.length) return null; const ext = contentType.includes('mp4') ? 'm4a' : 'webm'; return { @@ -687,7 +689,14 @@ form.append('file', capture.blob, capture.filename); form.append('workspace', workspace); const res = await fetch('/api/audio/transcribe', { method: 'POST', body: form }); - if (!res.ok) throw new Error('transcription failed'); + if (!res.ok) { + let detail = `${res.status}`; + try { + const data = await res.json(); + detail = data?.detail || data?.error || detail; + } catch {} + throw new Error(detail); + } const data = await res.json(); return (data?.text || '').trim(); } @@ -735,15 +744,19 @@ voiceWaitingForResponse = true; voiceSawStreaming = false; const capturePromise = stopVoiceCapture(); - stopVoiceRecognition(); + stopVoiceRecognition(false); const capture = await capturePromise; let text = browserText; if ($voiceModeSttMode === 'provider' && capture) { try { text = (await transcribeVoiceModeCapture(capture)) || browserText; - } catch { + } catch (err: any) { + const detail = err?.message ? ` ${err.message}` : ''; + toast.error(`${$t('chat.voiceProviderSttFallback')}${detail}`); text = browserText; } + } else if ($voiceModeSttMode === 'provider') { + toast.error($t('chat.voiceProviderSttNoAudio')); } else if (capture) { void saveVoiceModeSttCapture(capture, browserText); } diff --git a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte index aacc89d..61250ca 100644 --- a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte @@ -50,6 +50,12 @@ import { toast } from 'svelte-sonner'; import { t } from '$lib/i18n'; + type PreparedTtsAudio = { + promise: Promise; + controller: AbortController; + generation: number; + }; + interface Props { workspace: string; chatId?: string; @@ -74,7 +80,6 @@ let ttsInsideCodeFence = false; let ttsPlaying = false; let ttsGeneration = 0; - let ttsAbort: AbortController | null = null; let ttsAudio: HTMLAudioElement | null = null; let ttsObjectUrl: string | null = null; let ttsErrorShown = false; @@ -85,6 +90,7 @@ // for cross-session reuse and the workspace data flywheel. let ttsAudioCacheBytes = 0; const ttsAudioCache = new Map(); + const ttsPreparedAudio = new Map(); const TTS_AUDIO_CACHE_LIMIT_BYTES = 20 * 1024 * 1024; // ── Windowed rendering ────────────────────────────────────── @@ -951,8 +957,8 @@ ttsGeneration += 1; ttsQueue = []; resetTtsBuffer(); - ttsAbort?.abort(); - ttsAbort = null; + for (const pending of ttsPreparedAudio.values()) pending.controller.abort(); + ttsPreparedAudio.clear(); if (ttsAudio) { ttsAudio.pause(); ttsAudio.src = ''; @@ -1035,6 +1041,7 @@ function enqueueSpeech(text: string) { ttsQueue = [...ttsQueue, text]; + void prepareTtsAudio(text, ttsGeneration); if (!ttsPlaying) void playTtsQueue(ttsGeneration); } @@ -1106,7 +1113,58 @@ ttsAudioCacheBytes += blob.size; } + async function readTtsError(response: Response): Promise { + try { + const data = await response.json(); + return data?.detail || data?.error || `${response.status}`; + } catch { + return `${response.status}`; + } + } + + function showTtsFailure(detail: string) { + if (!ttsErrorShown) { + ttsErrorShown = true; + toast.error($t('admin.audio.tts') + ': ' + detail); + } + ttsPlaybackEnabled.set(false); + voiceModeEnabled.set(false); + } + + function prepareTtsAudio(text: string, generation: number): Promise { + const cacheKey = ttsCacheKey(text); + const cached = getCachedTtsAudio(cacheKey); + if (cached) return Promise.resolve(cached); + + const existing = ttsPreparedAudio.get(cacheKey); + if (existing && existing.generation === generation) return existing.promise; + if (existing) existing.controller.abort(); + + const controller = new AbortController(); + let promise!: Promise; + promise = fetch('/api/audio/speech', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ text, voice: $ttsVoice, workspace }), + signal: controller.signal + }) + .then(async (response) => { + if (!response.ok) throw new Error(await readTtsError(response)); + const blob = await response.blob(); + cacheTtsAudio(cacheKey, blob); + return blob; + }) + .finally(() => { + const current = ttsPreparedAudio.get(cacheKey); + if (current?.promise === promise) ttsPreparedAudio.delete(cacheKey); + }); + void promise.catch(() => {}); + ttsPreparedAudio.set(cacheKey, { promise, controller, generation }); + return promise; + } + async function playTtsQueue(generation: number) { + ttsStopRequested = false; ttsPlaying = true; try { while (generation === ttsGeneration && ttsQueue.length > 0) { @@ -1114,33 +1172,7 @@ const text = ttsQueue.shift(); if (!text) continue; - const cacheKey = ttsCacheKey(text); - let blob = getCachedTtsAudio(cacheKey); - if (!blob) { - ttsAbort = new AbortController(); - const response = await fetch('/api/audio/speech', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ text, voice: $ttsVoice, workspace }), - signal: ttsAbort.signal - }); - if (!response.ok) { - if (!ttsErrorShown) { - ttsErrorShown = true; - let detail = `${response.status}`; - try { - const data = await response.json(); - detail = data?.detail || data?.error || detail; - } catch {} - toast.error($t('admin.audio.tts') + ': ' + detail); - } - ttsPlaybackEnabled.set(false); - voiceModeEnabled.set(false); - break; - } - blob = await response.blob(); - cacheTtsAudio(cacheKey, blob); - } + const blob = await prepareTtsAudio(text, generation); if (generation !== ttsGeneration) break; if (ttsObjectUrl) URL.revokeObjectURL(ttsObjectUrl); @@ -1161,12 +1193,10 @@ ttsObjectUrl = null; } ttsAudio = null; - ttsAbort = null; } } catch (err: any) { if (!ttsStopRequested && err?.name !== 'AbortError' && !ttsErrorShown) { - ttsErrorShown = true; - toast.error($t('admin.audio.tts') + ': playback failed'); + showTtsFailure(err?.message || 'playback failed'); } } finally { if (generation === ttsGeneration) ttsPlaying = false; diff --git a/cptr/frontend/src/lib/i18n/locales/de.json b/cptr/frontend/src/lib/i18n/locales/de.json index 9922753..cc99577 100644 --- a/cptr/frontend/src/lib/i18n/locales/de.json +++ b/cptr/frontend/src/lib/i18n/locales/de.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Sprachmodus aktiv", "chat.voiceListening": "Hört zu...", "chat.voiceWaiting": "Wartet auf Antwort...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech ist nicht aktiviert oder konfiguriert.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/en.json b/cptr/frontend/src/lib/i18n/locales/en.json index 91ecd10..11969a1 100644 --- a/cptr/frontend/src/lib/i18n/locales/en.json +++ b/cptr/frontend/src/lib/i18n/locales/en.json @@ -702,6 +702,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/es.json b/cptr/frontend/src/lib/i18n/locales/es.json index b16b218..99e55b1 100644 --- a/cptr/frontend/src/lib/i18n/locales/es.json +++ b/cptr/frontend/src/lib/i18n/locales/es.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/fr.json b/cptr/frontend/src/lib/i18n/locales/fr.json index 566d833..6a527fb 100644 --- a/cptr/frontend/src/lib/i18n/locales/fr.json +++ b/cptr/frontend/src/lib/i18n/locales/fr.json @@ -573,6 +573,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/ja.json b/cptr/frontend/src/lib/i18n/locales/ja.json index e895cfa..b7aef2f 100644 --- a/cptr/frontend/src/lib/i18n/locales/ja.json +++ b/cptr/frontend/src/lib/i18n/locales/ja.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/ko.json b/cptr/frontend/src/lib/i18n/locales/ko.json index 3d4786b..4823eba 100644 --- a/cptr/frontend/src/lib/i18n/locales/ko.json +++ b/cptr/frontend/src/lib/i18n/locales/ko.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/pt-BR.json b/cptr/frontend/src/lib/i18n/locales/pt-BR.json index 7c414e7..efc1dd6 100644 --- a/cptr/frontend/src/lib/i18n/locales/pt-BR.json +++ b/cptr/frontend/src/lib/i18n/locales/pt-BR.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/ru.json b/cptr/frontend/src/lib/i18n/locales/ru.json index 5fe4eb8..0be1266 100644 --- a/cptr/frontend/src/lib/i18n/locales/ru.json +++ b/cptr/frontend/src/lib/i18n/locales/ru.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/zh-CN.json b/cptr/frontend/src/lib/i18n/locales/zh-CN.json index 4d9c1fe..4dfeeb9 100644 --- a/cptr/frontend/src/lib/i18n/locales/zh-CN.json +++ b/cptr/frontend/src/lib/i18n/locales/zh-CN.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking", diff --git a/cptr/frontend/src/lib/i18n/locales/zh-TW.json b/cptr/frontend/src/lib/i18n/locales/zh-TW.json index a9b337c..8830152 100644 --- a/cptr/frontend/src/lib/i18n/locales/zh-TW.json +++ b/cptr/frontend/src/lib/i18n/locales/zh-TW.json @@ -574,6 +574,8 @@ "chat.voiceModeOn": "Voice mode on", "chat.voiceListening": "Listening...", "chat.voiceWaiting": "Waiting for response...", + "chat.voiceProviderSttFallback": "Configured STT failed. Used browser transcript.", + "chat.voiceProviderSttNoAudio": "Configured STT needs microphone audio, but no voice-mode recording was captured.", "chat.ttsNotConfigured": "Text-to-Speech is not enabled or configured.", "chat.speakResponses": "Speak responses", "chat.stopSpeaking": "Stop speaking",