diff --git a/public/src/chat.js b/public/src/chat.js index e2e37f9..dfd869e 100644 --- a/public/src/chat.js +++ b/public/src/chat.js @@ -5,9 +5,9 @@ import { API_KEY, getAPIConfig } from "./config.js"; import { getSelectedModel, getSelectedModelEndpoint, getSelectedModelMaxTokens, getSelectedModelMaxCompletionTokens } from "./models.js"; import { groqAwareStreamingFetch } from "./groq-fetch.js"; -// Rough token estimation (3 chars = 1 token to be conservative) +// Rough token estimation (4 chars = 1 token average for English) function estimateTokens(text) { - return Math.ceil(text.length / 3); + return Math.ceil(text.length / 4); } // Calculate available output tokens diff --git a/public/src/storage.js b/public/src/storage.js index 292741a..f51f089 100644 --- a/public/src/storage.js +++ b/public/src/storage.js @@ -34,21 +34,31 @@ export function loadConversationHistory() { return []; } +// Get just the custom prompt for attention echo (not full system message with page content) +function getCustomPromptEcho() { + if (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) { + return window.UNCLOSEAI_SYSTEM_PROMPT; + } + return null; +} + export async function clearConversationHistory() { localStorage.removeItem(getPageSpecificKey("hermes-conversation-history")); const systemContent = await getSystemMessage(); - return [ - { role: "system", content: systemContent }, - { role: "assistant", content: systemContent }, - ]; + const echo = getCustomPromptEcho(); + const history = [{ role: "system", content: systemContent }]; + if (echo) history.push({ role: "assistant", content: echo }); + return history; } // Initialize chat history export async function initializeChatHistory() { const systemContent = await getSystemMessage(); - return [ + const echo = getCustomPromptEcho(); + const history = [ { role: "system", content: systemContent }, ...loadConversationHistory(), - { role: "assistant", content: systemContent }, ]; + if (echo) history.push({ role: "assistant", content: echo }); + return history; } diff --git a/public/src/translation.js b/public/src/translation.js index 1410176..58efa0b 100644 --- a/public/src/translation.js +++ b/public/src/translation.js @@ -75,7 +75,7 @@ async function* sendMessageWithHistory(messageHistory) { // Calculate available tokens for output based on current prompt const modelMaxTokens = getSelectedModelMaxTokens(); const inputText = messageHistory.map(msg => msg.content).join(''); - const inputTokens = Math.ceil(inputText.length / 3); // Conservative token estimation + const inputTokens = Math.ceil(inputText.length / 4); // Simple token estimation const outputReserve = Math.floor(modelMaxTokens * 0.4); // Reserve 40% for output let availableTokens = Math.max(512, modelMaxTokens - inputTokens - outputReserve); diff --git a/public/src/tts.js b/public/src/tts.js index 1c42518..0bdb657 100644 --- a/public/src/tts.js +++ b/public/src/tts.js @@ -143,7 +143,7 @@ Your task: 7. Start with the article title, then the main content Return only the article text ready for TTS, nothing else.`; - const inputTokens = Math.ceil((systemPrompt + preCleanedContent).length / 3); + const inputTokens = Math.ceil((systemPrompt + preCleanedContent).length / 4); const buffer = Math.max(2048, Math.floor(inputTokens * 1.5)); // Use 1.5x input tokens as buffer, minimum 2048 const calculatedAvailable = Math.max(100, modelMaxTokens - inputTokens - buffer); diff --git a/public/src/ui.js b/public/src/ui.js index c1a02ed..b2ba880 100644 --- a/public/src/ui.js +++ b/public/src/ui.js @@ -141,9 +141,9 @@ async function* sendMessageWithCustomHistory(messageHistory) { const { getSelectedModelMaxTokens, getSelectedModelMaxCompletionTokens } = await import("./models.js"); const modelMaxTokens = getSelectedModelMaxTokens(); - // Estimate input tokens for intro generation (3 chars = 1 token to be conservative) + // Estimate input tokens for intro generation const inputText = messageHistory.map(msg => msg.content).join(''); - const inputTokens = Math.ceil(inputText.length / 3); + const inputTokens = Math.ceil(inputText.length / 4); // For intro generation, be more generous - we want complete intros // With large context models, we have plenty of room diff --git a/public/src/uncloseai-embed-modal.js b/public/src/uncloseai-embed-modal.js index 5cf9f36..67b9dfb 100644 --- a/public/src/uncloseai-embed-modal.js +++ b/public/src/uncloseai-embed-modal.js @@ -1499,10 +1499,11 @@ You have complete knowledge of this page content and can reference any details, // Create new history with updated system message and loaded conversation const systemContent = await getSystemMessage(); + const echo = (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) ? window.UNCLOSEAI_SYSTEM_PROMPT : null; const newHistory = [ { role: "system", content: systemContent }, ...history, - { role: "assistant", content: systemContent }, + ...(echo ? [{ role: "assistant", content: echo }] : []), ]; // Update the chat.js module's history @@ -1648,10 +1649,11 @@ You have complete knowledge of this page content and can reference any details, const { updateChatHistory } = await import("./chat.js"); const { getSystemMessage } = await import("./config.js"); const systemContent = await getSystemMessage(); + const echo = (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) ? window.UNCLOSEAI_SYSTEM_PROMPT : null; const newHistory = [ { role: "system", content: systemContent }, ...currentHistory, - { role: "assistant", content: systemContent }, + ...(echo ? [{ role: "assistant", content: echo }] : []), ]; await updateChatHistory(newHistory); } @@ -1967,10 +1969,11 @@ You have complete knowledge of this page content and can reference any details, // Create new history with system message, intro, and echo for attention const systemContent = await getSystemMessage(); + const echo = (typeof window !== "undefined" && window.UNCLOSEAI_SYSTEM_PROMPT) ? window.UNCLOSEAI_SYSTEM_PROMPT : null; const newHistory = [ { role: "system", content: systemContent }, { role: "assistant", content: response }, - { role: "assistant", content: systemContent }, + ...(echo ? [{ role: "assistant", content: echo }] : []), ]; // Update the chat.js module's history