diff --git a/src/ui.js b/src/ui.js index 9e0d46f..eb3bfd5 100644 --- a/src/ui.js +++ b/src/ui.js @@ -3,6 +3,7 @@ import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js"; import hljs from "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/es/highlight.min.js"; import { extractWebpageContent } from "./content.js"; import { speakText } from "./tts.js"; +import { TTS_API_URL, API_KEY } from "./config.js"; import { handleFileUpload, uploadFile, @@ -1033,18 +1034,49 @@ async function openHermesModalNew() { ttsBtn.disabled = true; try { - const result = await speakText(response, selectedVoice, 1.0); - console.log("TTS completed successfully"); + console.log("🔊 CHAT TTS: Calling direct TTS for chat message"); + + // Use direct TTS without extractSpokenTokens preprocessing for chat messages + const directTTSResponse = await fetch(TTS_API_URL, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${API_KEY}`, + }, + body: JSON.stringify({ + model: "tts-1", + voice: selectedVoice, + input: response, + }), + }); + + if (!directTTSResponse.ok) { + throw new Error(`HTTP error! status: ${directTTSResponse.status}`); + } + + const audioBlob = await directTTSResponse.blob(); + const audioUrl = URL.createObjectURL(audioBlob); + const audio = new Audio(audioUrl); + audio.playbackRate = 1.0; + + const result = { audio, blob: audioBlob }; + console.log("🔊 CHAT TTS: Direct TTS completed successfully", result); + // Auto-play the generated audio - try { - await result.audio.play(); - console.log("TTS auto-playing from standalone button"); - } catch (playError) { - console.log("Auto-play blocked, user can manually play"); + if (result && result.audio) { + try { + await result.audio.play(); + console.log("🔊 CHAT TTS: auto-playing from standalone button"); + } catch (playError) { + console.log("🔊 CHAT TTS: auto-play blocked:", playError.message); + } + } else { + console.error("🔊 CHAT TTS: No audio returned from speakText"); } } catch (error) { - console.error("TTS failed:", error); - alert("TTS failed: " + error.message); + console.error("🔊 CHAT TTS: Failed with error:", error); + console.error("🔊 CHAT TTS: Error stack:", error.stack); + alert("Chat TTS failed: " + error.message); } finally { ttsBtn.textContent = originalText; ttsBtn.disabled = false; @@ -1182,18 +1214,49 @@ async function openHermesModalNew() { ttsBtn.disabled = true; try { - const result = await speakText(msg.content, selectedVoice, 1.0); - console.log("Historical TTS completed successfully"); + console.log("🔊 HISTORICAL TTS: Calling direct TTS for historical message"); + + // Use direct TTS without extractSpokenTokens preprocessing for historical messages + const directTTSResponse = await fetch(TTS_API_URL, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${API_KEY}`, + }, + body: JSON.stringify({ + model: "tts-1", + voice: selectedVoice, + input: msg.content, + }), + }); + + if (!directTTSResponse.ok) { + throw new Error(`HTTP error! status: ${directTTSResponse.status}`); + } + + const audioBlob = await directTTSResponse.blob(); + const audioUrl = URL.createObjectURL(audioBlob); + const audio = new Audio(audioUrl); + audio.playbackRate = 1.0; + + const result = { audio, blob: audioBlob }; + console.log("🔊 HISTORICAL TTS: Direct TTS completed successfully", result); + // Auto-play the generated audio - try { - await result.audio.play(); - console.log("Historical TTS auto-playing"); - } catch (playError) { - console.log("Auto-play blocked for historical message"); + if (result && result.audio) { + try { + await result.audio.play(); + console.log("🔊 HISTORICAL TTS: auto-playing"); + } catch (playError) { + console.log("🔊 HISTORICAL TTS: auto-play blocked:", playError.message); + } + } else { + console.error("🔊 HISTORICAL TTS: No audio returned from speakText"); } } catch (error) { - console.error("Historical TTS failed:", error); - alert("TTS failed: " + error.message); + console.error("🔊 HISTORICAL TTS: Failed with error:", error); + console.error("🔊 HISTORICAL TTS: Error stack:", error.stack); + alert("Historical TTS failed: " + error.message); } finally { ttsBtn.textContent = originalText; ttsBtn.disabled = false;