|
|
|
|
@@ -27,7 +27,15 @@ 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
|
|
|
|
|
|
|
|
|
|
// Ensure chatHistory is an array of objects with the correct structure
|
|
|
|
|
chatHistory = chatHistory.map(message => ({
|
|
|
|
|
role : message.role || 'user',
|
|
|
|
|
content : message.content || '',
|
|
|
|
|
markdown : message.markdown || false,
|
|
|
|
|
messageId: message.messageId || `msg-${ Date.now() }`
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
// Get current API URL based on selected backend
|
|
|
|
|
function getApiUrl() {
|
|
|
|
|
@@ -99,6 +107,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 +217,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 +611,24 @@ 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 event listener for reset button
|
|
|
|
|
document.getElementById('resetBtn').addEventListener('click', resetChat)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset chat history
|
|
|
|
|
function resetChat() {
|
|
|
|
|
// Clear chat history
|
|
|
|
|
chatHistory = []
|
|
|
|
|
|
|
|
|
|
// Clear chat log
|
|
|
|
|
chatLog.innerHTML = ''
|
|
|
|
|
|
|
|
|
|
// Add welcome message
|
|
|
|
|
const welcomeMessage = `# Welcome to LM Studio Chat!
|
|
|
|
|
|
|
|
|
|
I now support **full conversation history**, **real-time streaming responses** and **dark, readable text formatting**.
|
|
|
|
|
|
|
|
|
|
@@ -619,4 +648,7 @@ 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save chat history to localStorage
|
|
|
|
|
localStorage.setItem('chatHistory', JSON.stringify(chatHistory))
|
|
|
|
|
}
|
|
|
|
|
|