fix progress calculation to show actual tokens including preservation and chunking

This commit is contained in:
Russell Ballestrini 2025-07-11 21:21:58 -04:00
parent cfc6dec1f4
commit 854d22ee79

View file

@ -333,9 +333,43 @@ export function openTranslateModal() {
? `${rawContent.substring(0, maxLength)}...`
: rawContent;
// Just estimate tokens without preservation for progress calculation
const tokenInfo = await countTokensWithUpstream(contentToProcess, getSelectedModel());
const estimatedSeconds = estimateProcessingTime(tokenInfo.totalTokens);
// Calculate actual tokens that will be sent to AI (including preservation and chunking)
let totalTokens = 0;
// Simulate the same chunking logic that translateHTML will use
const maxTokens = getSelectedModelMaxTokens?.() || 8192;
const reserveTokens = 1000;
const availableTokens = Math.max(maxTokens - reserveTokens, 2000);
const contentTokens = await countTokensWithUpstream(contentToProcess, getSelectedModel());
const estimatedOutputTokens = Math.floor(contentTokens.totalTokens * 1.8);
const totalEstimatedTokens = contentTokens.totalTokens + estimatedOutputTokens;
if (totalEstimatedTokens <= availableTokens) {
// Single request - calculate preserved content tokens
const { preservedText } = preserveSpecialContent(contentToProcess);
const prompt = `Translate the following HTML content into ${SUPPORTED_LANGUAGES[targetLanguage]}. The input is in English and formatted as HTML, which includes formatting, code blocks, and technical content. Ensure the output preserves the structure, syntax, and formatting of the original. Do not translate or modify placeholders like __CODE_0__, __URI_0__, __HTML_0__, etc., as they represent code blocks, URLs, or HTML elements that should remain unchanged. Only translate the surrounding text. Here is the text to translate:
${preservedText}`;
const promptTokens = await countTokensWithUpstream(prompt, getSelectedModel());
totalTokens = promptTokens.totalTokens;
} else {
// Multiple chunks - estimate tokens per chunk and multiply
const maxChunkInputTokens = Math.floor(availableTokens / 2.8);
const estimatedChunks = Math.ceil(contentTokens.totalTokens / maxChunkInputTokens);
const avgTokensPerChunk = Math.floor(contentTokens.totalTokens / estimatedChunks);
// Estimate tokens for one chunk with preservation and prompt
const sampleChunk = contentToProcess.substring(0, Math.floor(contentToProcess.length / estimatedChunks));
const { preservedText: samplePreserved } = preserveSpecialContent(sampleChunk);
const samplePrompt = `Translate the following HTML content into ${SUPPORTED_LANGUAGES[targetLanguage]}. The input is in English and formatted as HTML, which includes formatting, code blocks, and technical content. Ensure the output preserves the structure, syntax, and formatting of the original. Do not translate or modify placeholders like __CODE_0__, __URI_0__, __HTML_0__, etc., as they represent code blocks, URLs, or HTML elements that should remain unchanged. Only translate the surrounding text. Here is the text to translate:
${samplePreserved}`;
const sampleTokens = await countTokensWithUpstream(samplePrompt, getSelectedModel());
totalTokens = sampleTokens.totalTokens * estimatedChunks;
}
const estimatedSeconds = estimateProcessingTime(totalTokens);
// Create progress indicator
const progressContainer = document.createElement("div");
@ -343,7 +377,7 @@ export function openTranslateModal() {
const progressText = document.createElement("div");
progressText.className = "translate-progress-text";
progressText.textContent = getUIText("processingTokens", { tokens: tokenInfo.totalTokens });
progressText.textContent = getUIText("processingTokens", { tokens: totalTokens });
const etaText = document.createElement("div");
etaText.className = "translate-eta-text";