From e6b36ecf9d25a38b47e104afd51967a0b2b36646 Mon Sep 17 00:00:00 2001 From: tore Date: Mon, 14 Sep 2026 22:04:25 +0000 Subject: [PATCH] =?UTF-8?q?chat-=D0=BF=D0=BE=D0=BC=D0=BE=D1=89=D0=BD=D0=B8?= =?UTF-8?q?=D0=BA=20=D0=BF=D0=BE=20=D0=B7=D0=B0=D0=BA=D1=83=D0=BF=D0=BA?= =?UTF-8?q?=D0=B0=D0=BC:=20=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8=D0=B8,=20=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D1=81=20=D0=B4=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) 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 = `
${escapeHtml(msg.text)}
-
${time}
+ ${time ? `
${time}
` : ''} `; container.appendChild(div); });