diff --git a/src/ui.js b/src/ui.js index 23c854e..076ab77 100644 --- a/src/ui.js +++ b/src/ui.js @@ -1082,9 +1082,13 @@ async function openHermesModalNew() { aiMsg.innerHTML = marked.parse(response); } + // Get the updated chat history from chat.js module + const { getChatHistory } = await import('./chat.js'); + const updatedHistory = getChatHistory(); + // Save conversation history after successful response - saveConversationHistory(chatHistory); - console.log("Conversation history saved:", chatHistory.length, "messages"); + saveConversationHistory(updatedHistory); + console.log("Conversation history saved:", updatedHistory.length, "messages"); // Add TTS and delete buttons to AI message const messageActions = document.createElement("div"); @@ -1259,15 +1263,18 @@ You have complete knowledge of this page content and can reference any details, setSystemMessageAppend(conversationContextAppend); // Sync the loaded history with the chat.js module - // Clear existing non-system messages and add loaded history - const systemMsg = chatHistory.find(msg => msg.role === 'system'); - chatHistory.length = 0; // Clear array - if (systemMsg) { - const { getSystemMessage } = await import('./config.js'); - chatHistory.push({ role: 'system', content: getSystemMessage() }); - } - chatHistory.push(...history); - console.log("Chat history synced with loaded data:", chatHistory.length, "messages"); + const { updateChatHistory } = await import('./chat.js'); + const { getSystemMessage } = await import('./config.js'); + + // Create new history with updated system message and loaded conversation + const newHistory = [ + { role: 'system', content: getSystemMessage() }, + ...history + ]; + + // Update the chat.js module's history + updateChatHistory(newHistory); + console.log("Chat history synced with loaded data:", newHistory.length, "messages"); console.log("Current chatHistory contents:", chatHistory); console.log("History to display:", history); // Display previous conversation @@ -1487,15 +1494,18 @@ You have complete knowledge of this page content and can reference any details, setSystemMessageAppend(conversationContextAppend); - // Update the chat history system message for follow-up conversations - const systemMsg = chatHistory.find(msg => msg.role === 'system'); - if (systemMsg) { - const { getSystemMessage } = await import('./config.js'); - systemMsg.content = getSystemMessage(); - } + // Update the chat.js module with the intro message + const { updateChatHistory, getChatHistory } = await import('./chat.js'); + const { getSystemMessage } = await import('./config.js'); - // Add the intro message to chat history so it's not regenerated - chatHistory.push({ role: 'assistant', content: response }); + // Create new history with system message and intro + const newHistory = [ + { role: 'system', content: getSystemMessage() }, + { role: 'assistant', content: response } + ]; + + // Update the chat.js module's history + updateChatHistory(newHistory); console.log("Added intro message to chat history"); // Add TTS and delete buttons to intro message @@ -1605,7 +1615,7 @@ You have complete knowledge of this page content and can reference any details, introMsg.appendChild(messageActions); // Save this intro as part of the conversation - saveConversationHistory(chatHistory); + saveConversationHistory(getChatHistory()); } catch (error) { console.error("Failed to generate contextual intro:", error);