Fix echo to only use custom prompt, not full system message with page content

This commit is contained in:
Russell Ballestrini 2026-01-01 12:24:50 -05:00
parent 1ec196275f
commit e55d7d414c
6 changed files with 28 additions and 15 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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);

View file

@ -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);

View file

@ -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

View file

@ -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