let messages = []; let currentChatTitle = ''; let chatIds = []; let currentChatId =…
- User
let messages = []; let currentChatTitle = ''; let chatIds = []; let currentChatId = null; document.addEventListener('DOMContentLoaded', function() { // Инициализируем чаты после загрузки DOM initializeChatHistory(); //+++++++++++ Работа с local storage ++++++++++++++++++++++
//Проверяет есть ли в local storag история сообщений function isChatHistoryExists() { // Проверяем наличие данных по ключу 'chatHistory' в local storage return localStorage.getItem(
chatHistory_${currentChatId}) !== null; }//Сохраняет историю чата в local storage (метка chatHistory_key) function saveChatHistory(mss) { let dialog = { id:
dialog_${currentChatId}, title: currentChatTitle, messages: mss }; localStorage.setItem(chatHistory_${currentChatId}, JSON.stringify(dialog));code// Обновление глобального массива ID бесед if (!chatIds.includes(currentChatId)) { chatIds.push(currentChatId); saveChatHistoryIds(chatIds); }}
//Считывает историю чата из local storage (метка chatHistory_key) function loadChatHistory() { const stored = localStorage.getItem(
chatHistory_${currentChatId}); return stored ? JSON.parse(stored).messages : []; }// Загружает массив с ID всех имеющихся бесед из local storage function loadChatHistoryIds() { const storedIds = localStorage.getItem('chatHistoryIds'); return storedIds ? JSON.parse(storedIds) : []; }
// Сохраняет массив с ID всех имеющихся бесед в local storage function saveChatHistoryIds(ids) { localStorage.setItem('chatHistoryIds', JSON.stringify(ids)); }
// Инициализация чата. function initializeChatHistory() {
code - Assistant