diff --git a/src/ui.js b/src/ui.js index f26fe7f..f77175b 100644 --- a/src/ui.js +++ b/src/ui.js @@ -5,6 +5,63 @@ import { extractWebpageContent } from "./content.js"; import { speakText } from "./tts.js"; import { TTS_API_URL, API_KEY, setSystemMessageAppend } from "./config.js"; +// Send message with custom history (for intro generation) +async function* sendMessageWithCustomHistory(messageHistory) { + const { getSelectedModel, getSelectedModelEndpoint } = await import('./models.js'); + const apiUrl = `${getSelectedModelEndpoint()}/chat/completions`; + + const response = await fetch(apiUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${API_KEY}`, + }, + body: JSON.stringify({ + model: getSelectedModel(), + messages: messageHistory, + temperature: 0.3, + max_tokens: 8192, + stream: true, + }), + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const reader = response.body.getReader(); + const decoder = new TextDecoder(); + let buffer = ""; + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + buffer += decoder.decode(value, { stream: true }); + const lines = buffer.split("\n"); + + for (let i = 0; i < lines.length - 1; i++) { + const line = lines[i].trim(); + if (line.startsWith("data: ")) { + const jsonData = line.slice(6); + if (jsonData === "[DONE]") continue; + + try { + const parsedData = JSON.parse(jsonData); + const content = parsedData.choices[0].delta.content; + if (content) { + yield content; + } + } catch (error) { + console.error("Error parsing JSON:", error); + } + } + } + + buffer = lines[lines.length - 1]; + } +} + // Simple TTS function for chat messages (no preprocessing needed) async function speakChatText(text, voice = "alloy", rate = 1.0) { try { @@ -1388,10 +1445,7 @@ Reference specific details from the page content to show understanding.`; // Generate intro with specialized system prompt let response = ''; - const { sendMessage: sendMessageWithHistory } = await import('./translation.js'); - - // Use the translation sendMessage which accepts custom history - for await (const chunk of sendMessageWithHistory(introHistory)) { + for await (const chunk of sendMessageWithCustomHistory(introHistory)) { response += chunk; introMsg.innerHTML = marked.parse(response); }