refactor(tts): create dedicated speakChatText function for chat messages

- Add speakChatText function that skips extractSpokenTokens preprocessing
- Chat messages are already clean text that don't need markdown/HTML cleanup
- Simplify chat TTS button handlers to use clean direct TTS approach
- Separate chat TTS from webpage TTS which needs content preprocessing
- Much faster and more reliable for chat conversations
This commit is contained in:
Russell Ballestrini 2025-07-02 07:58:24 -04:00
parent 59aacd7954
commit bab44f680b

View file

@ -4,6 +4,38 @@ import hljs from "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/es/
import { extractWebpageContent } from "./content.js";
import { speakText } from "./tts.js";
import { TTS_API_URL, API_KEY } from "./config.js";
// Simple TTS function for chat messages (no preprocessing needed)
async function speakChatText(text, voice = "alloy", rate = 1.0) {
try {
const response = await fetch(TTS_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${API_KEY}`,
},
body: JSON.stringify({
model: "tts-1",
voice: voice,
input: text,
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const audioBlob = await response.blob();
const audioUrl = URL.createObjectURL(audioBlob);
const audio = new Audio(audioUrl);
audio.playbackRate = rate;
return { audio, blob: audioBlob };
} catch (error) {
console.error("Error in chat TTS:", error);
throw error;
}
}
import {
handleFileUpload,
uploadFile,
@ -1034,33 +1066,9 @@ async function openHermesModalNew() {
ttsBtn.disabled = true;
try {
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);
console.log("🔊 CHAT TTS: Using simple chat TTS (no preprocessing)");
const result = await speakChatText(response, selectedVoice, 1.0);
console.log("🔊 CHAT TTS: completed successfully");
// Auto-play the generated audio
if (result && result.audio) {
@ -1214,33 +1222,9 @@ async function openHermesModalNew() {
ttsBtn.disabled = true;
try {
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);
console.log("🔊 HISTORICAL TTS: Using simple chat TTS (no preprocessing)");
const result = await speakChatText(msg.content, selectedVoice, 1.0);
console.log("🔊 HISTORICAL TTS: completed successfully");
// Auto-play the generated audio
if (result && result.audio) {