diff --git a/index.html b/index.html index 89a2670..a0cea60 100644 --- a/index.html +++ b/index.html @@ -96,8 +96,16 @@ const knowledgeBase = { } }; -// === История чата в localStorage === -let chatHistory = JSON.parse(localStorage.getItem('chatHistory') || '[]'); +// === История чата в localStorage (ключ уникальный для этого чата) === +const STORAGE_KEY = 'zakupkiChatHistory_v1'; +let chatHistory = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]'); + +// Миграция: если есть старая история без ключа версии — очищаем +const oldHistory = localStorage.getItem('chatHistory'); +if (oldHistory) { + localStorage.removeItem('chatHistory'); + chatHistory = []; +} // === Инициализация === function init() { @@ -124,7 +132,7 @@ function sendMessage() { const userMsg = { role: 'user', text: question, - time: new Date().toISOString() + time: Date.now() }; chatHistory.push(userMsg); @@ -133,12 +141,12 @@ function sendMessage() { const assistantMsg = { role: 'assistant', text: response, - time: new Date().toISOString() + time: Date.now() }; chatHistory.push(assistantMsg); // Сохраняем и рисуем - localStorage.setItem('chatHistory', JSON.stringify(chatHistory)); + localStorage.setItem(STORAGE_KEY, JSON.stringify(chatHistory)); input.value = ''; renderMessages(); scrollToBottom(); @@ -183,14 +191,18 @@ function renderMessages() { } chatHistory.forEach(msg => { + // Проверка на битые сообщения + if (!msg || !msg.text || !msg.time) return; + const div = document.createElement('div'); div.className = `chat-message ${msg.role}`; - const time = new Date(msg.time).toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' }); + const date = new Date(msg.time); + const time = isNaN(date.getTime()) ? '' : date.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' }); div.innerHTML = `