feat: store chat history in localStorage to preserve conversation on refresh

Co-authored-by: aider (openai//models/Qwen/Qwen2.5-Coder-32B-Instruct-GGUF/qwen2.5-coder-32b-instruct-q4_k_m.gguf) <aider@aider.chat>
This commit is contained in:
mike
2025-12-28 02:55:15 +01:00
parent 3c7c4d4233
commit db0811e8a7

View File

@@ -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)
}
addMessage('assistant', welcomeMessage, true)
} else {
// Re-add all messages from chatHistory
chatHistory.forEach(message => {
addMessage(message.role, message.content, message.markdown, message.messageId)
})
}
}