From cd00e28fd71207cfa8bd18d0a8e8ccf8f0f09f41 Mon Sep 17 00:00:00 2001 From: Christian Rute Date: Thu, 31 Oct 2024 15:02:07 +0100 Subject: [PATCH] add complete chat history --- client/css/styles.css | 3 +-- client/js/chat.js | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/client/css/styles.css b/client/css/styles.css index c69b4ff..5066408 100644 --- a/client/css/styles.css +++ b/client/css/styles.css @@ -310,8 +310,7 @@ main { #chatBox { display: flex; - /*flex-direction: column-reverse; /* flex-direction: column-reverse; */ - flex-direction: column; /* normale Richtung */ + flex-direction: column-reverse; overflow-y: auto; } diff --git a/client/js/chat.js b/client/js/chat.js index 0bc9bb7..b402be0 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -39,7 +39,7 @@ function createNewChat() { async function saveMessage(chatId, message) { const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || []; - chatHistory.push({ user: "user", text: message }); + chatHistory.push({ role: "user", content: message }); localStorage.setItem(`chatHistory-${chatId}`, JSON.stringify(chatHistory)); // Aktualisiere den Button-Text in der Sidebar @@ -51,7 +51,7 @@ async function saveMessage(chatId, message) { let a = await stream_api_open_ai(userMessage); console.log("Komplette Nachricht: " + a) const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || []; - chatHistory.push({ user: "bot", text: a }); + chatHistory.push({ role: "assistant", content: a }); localStorage.setItem(`chatHistory-${chatId}`, JSON.stringify(chatHistory)); } } @@ -61,7 +61,7 @@ function updateChatSessionButton(chatId, message) { if (button) { const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || []; // Hole die erste Nachricht oder lasse es leer, wenn keine vorhanden ist - const chatName = chatHistory.length > 0 ? chatHistory[0].text.slice(0, 10) : ''; // Nimm die ersten 10 Zeichen der ersten Nachricht + const chatName = chatHistory.length > 0 ? chatHistory[0].content.slice(0, 10) : ''; // Nimm die ersten 10 Zeichen der ersten Nachricht button.innerText = chatName + "..." || `Chat ${chatId + 1}`; // Falls leer, setze auf "Chat x" } } @@ -115,8 +115,8 @@ function loadChatHistory(chatId) { const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || []; chatHistory.forEach(entry => { const messageElement = document.createElement('div'); - messageElement.classList.add('chat-message', entry.user); - messageElement.innerText = entry.text; + messageElement.classList.add('chat-message', entry.role); + messageElement.innerText = entry.content; chatBox.prepend(messageElement); }); } @@ -208,16 +208,23 @@ async function getModels() { } } +function getAllCurrentChatMessages() { + const cookieData = JSON.parse(localStorage.getItem(`chatHistory-${currentChatId}`)) || []; + return cookieData +} + + function getLastUserMessage() { // Lade die Cookie-Daten (ersetze dies durch den tatsächlichen Weg zur Cookie-Datenstruktur) const cookieData = JSON.parse(localStorage.getItem(`chatHistory-${currentChatId}`)) || []; + // Filter nur die User-Nachrichten heraus - const userMessages = cookieData.filter(message => message.user === "user"); + const userMessages = cookieData.filter(message => message.role === "user"); // Falls es User-Nachrichten gibt, die letzte davon zurückgeben if (userMessages.length > 0) { - return userMessages[userMessages.length - 1].text; + return userMessages[userMessages.length - 1].content; } return ''; // Rückgabe eines leeren Strings, falls keine Nachricht gefunden wird @@ -240,7 +247,7 @@ async function stream_api_open_ai(userMessage) { model: await getModels(), messages: [ { role: 'system', content: 'You are a knowledgeable assistant.' }, - { role: 'user', content: userMessage } + ...getAllCurrentChatMessages() ], stream: true, temperature: 0.3