From 8b0fe6d2ac2f68cbe5d5aeb6771d55854ce0f6de Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Fri, 11 Jul 2025 15:25:02 -0400 Subject: [PATCH] apply 2048 token buffer to all upstream API requests: translation and TTS now calculate available tokens properly --- src/translation.js | 11 ++++++++--- src/tts.js | 24 +++++++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/translation.js b/src/translation.js index bc88806..9fd7547 100644 --- a/src/translation.js +++ b/src/translation.js @@ -70,10 +70,15 @@ async function* sendMessageWithHistory(messageHistory) { model = getSelectedModel(); } - const maxTokens = getSelectedModelMaxTokens(); + // Calculate available tokens based on input length + const modelMaxTokens = getSelectedModelMaxTokens(); + const inputText = messageHistory.map(msg => msg.content).join(''); + const inputTokens = Math.ceil(inputText.length / 4); // Simple token estimation + const availableTokens = Math.max(100, modelMaxTokens - inputTokens - 2048); // 2048 token buffer + console.log("Translation using endpoint:", apiUrl); console.log("Translation using model:", model); - console.log("Translation using max_tokens:", maxTokens); + console.log("Translation token calculation: max=" + modelMaxTokens + ", input≈" + inputTokens + ", available≈" + availableTokens); const response = await fetch(apiUrl, { method: "POST", @@ -82,7 +87,7 @@ async function* sendMessageWithHistory(messageHistory) { model: model, messages: messageHistory, temperature: 0.3, // Lower temperature for more consistent translation - max_tokens: maxTokens, + max_tokens: availableTokens, stream: true, }), }); diff --git a/src/tts.js b/src/tts.js index 946f18f..70725a6 100644 --- a/src/tts.js +++ b/src/tts.js @@ -1,6 +1,6 @@ // Text-to-speech functionality import { API_KEY, MODEL, setLastTTS, TTS_API_URL } from "./config.js"; -import { getSelectedModel, getSelectedModelEndpoint } from "./models.js"; +import { getSelectedModel, getSelectedModelEndpoint, getSelectedModelMaxTokens } from "./models.js"; // Simple JavaScript text cleaning for TTS function cleanTextForTTS(text) { @@ -129,12 +129,9 @@ export async function extractSpokenTokens(content) { .replace(/\s+/g, ' ') .trim(); - const payload = { - model: getSelectedModel(), - messages: [ - { - role: "system", - content: `You are extracting the main article content for text-to-speech. The input may be markdown or pre-cleaned HTML. + // Calculate available tokens for TTS processing + const modelMaxTokens = getSelectedModelMaxTokens(); + const systemPrompt = `You are extracting the main article content for text-to-speech. The input may be markdown or pre-cleaned HTML. Your task: 1. Find and extract the main article content (title, subtitle, body text) @@ -145,7 +142,16 @@ Your task: 6. Remove any remaining HTML tags but keep the text inside them 7. Start with the article title, then the main content -Return only the article text ready for TTS, nothing else.`, +Return only the article text ready for TTS, nothing else.`; + const inputTokens = Math.ceil((systemPrompt + preCleanedContent).length / 4); + const availableTokens = Math.max(100, modelMaxTokens - inputTokens - 2048); + + const payload = { + model: getSelectedModel(), + messages: [ + { + role: "system", + content: systemPrompt, }, { role: "user", @@ -153,7 +159,7 @@ Return only the article text ready for TTS, nothing else.`, }, ], temperature: 0, - max_tokens: 76000, + max_tokens: availableTokens, }; console.log("Processing page content for TTS...");