diff --git a/chatv2.js b/chatv2.js index be84359..61622ea 100644 --- a/chatv2.js +++ b/chatv2.js @@ -27,7 +27,7 @@ let currentModel = null let availableModels = [] let currentStreamController = null let isStreaming = false -let chatHistory = [] // Stores conversation history +let chatHistory = JSON.parse(localStorage.getItem('chatHistory')) || [] // Stores conversation history // Get current API URL based on selected backend function getApiUrl() { @@ -99,6 +99,8 @@ function trimChatHistory() { if (chatHistory.length > MAX_CHAT_HISTORY) { chatHistory = chatHistory.slice(-MAX_CHAT_HISTORY) console.log(`Chat history trimmed to ${ MAX_CHAT_HISTORY } messages`) + // Save chat history to localStorage + localStorage.setItem('chatHistory', JSON.stringify(chatHistory)) } } @@ -207,6 +209,9 @@ function addMessage(role, content, markdown = false, messageId = null) { // Trim history if it gets too long trimChatHistory() + // Save chat history to localStorage + localStorage.setItem('chatHistory', JSON.stringify(chatHistory)) + return contentDiv } @@ -598,8 +603,10 @@ window.onload = () => { userInput.focus() fetchModels() - // Add welcome message (this will be the first entry in chatHistory) - const welcomeMessage = `# Welcome to LM Studio Chat! ? + // Load chat history from localStorage + if (chatHistory.length === 0) { + // Add welcome message (this will be the first entry in chatHistory) + const welcomeMessage = `# Welcome to LM Studio Chat! ? I now support **full conversation history**, **real-time streaming responses** and **dark, readable text formatting**. @@ -618,5 +625,11 @@ I now support **full conversation history**, **real-time streaming responses** a > *Tip: You can toggle streaming and markdown using the checkboxes below.*` - addMessage('assistant', welcomeMessage, true) -} \ No newline at end of file + addMessage('assistant', welcomeMessage, true) + } else { + // Re-add all messages from chatHistory + chatHistory.forEach(message => { + addMessage(message.role, message.content, message.markdown, message.messageId) + }) + } +}