From 299539dc868841b6682080c2630a2553b6e73df6 Mon Sep 17 00:00:00 2001 From: YasinOnm08 Date: Wed, 9 Oct 2024 15:56:06 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=AB=F0=9F=A4=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/backend/InputOutputHandler.tsx | 20 ++++++++++++-------- app/hooks/useChatHistory.tsx | 3 +-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/backend/InputOutputHandler.tsx b/app/backend/InputOutputHandler.tsx index 1a4817b..e56bbe9 100644 --- a/app/backend/InputOutputHandler.tsx +++ b/app/backend/InputOutputHandler.tsx @@ -21,7 +21,7 @@ const InputOutputBackend: React.FC = () => { const [preferredMeasurement, setPreferredMeasurement] = useState("metric"); const [timeZone, setTimeZone] = useState("GMT"); const [dateFormat, setDateFormat] = useState("DD-MM-YYYY"); - const [messages, setMessages] = useState(chatHistory.chats[chatHistory.selectedIndex]?.messages || []); + const [messages, setMessages] = useState(chatHistory.chats[chatHistory.selectedIndex].messages || []); const [myBoolean, setMyBoolean] = useState(false); const [systemMessage, setSystemMessage] = useState("You are a helpful assistant") const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_create") @@ -31,22 +31,26 @@ const InputOutputBackend: React.FC = () => { apiURL.hostname = "localhost" } + console.log(setSelectedIndex) - useEffect(() => { +useEffect(() => { console.log("History", chatHistory); console.log("Messages", messages); // Get the current chat's messages - const currentMessages = chatHistory.chats[chatHistory.selectedIndex]?.messages || []; + const currentMessages = chatHistory.chats[chatHistory.selectedIndex].messages || []; - // If currentMessages is not empty, update messages only if it's not the same - if (currentMessages.length > 0 && JSON.stringify(currentMessages) !== JSON.stringify(messages)) { + // If the selected chat has messages, set them + if (currentMessages.length > 0) { setMessages(currentMessages); - } else if (messages.length === 0) { - setMessages([{ role: "system", content: systemMessage }, { role: "assistant", content: "Hello! How can I help you?" }]); + } else if (currentMessages.length === 0) { + // When creating a new chat and no messages exist yet, set default messages + addMessage("system", systemMessage) + addMessage("assistant", "Hello! How can I help you?") + console.log(systemMessage) } -}, [chatHistory, setSelectedIndex]); +}, [chatHistory, chatHistory.selectedIndex, systemMessage]); // Update messages when any of the settings change useEffect(() => { diff --git a/app/hooks/useChatHistory.tsx b/app/hooks/useChatHistory.tsx index 29f9b97..b2aceb0 100644 --- a/app/hooks/useChatHistory.tsx +++ b/app/hooks/useChatHistory.tsx @@ -19,8 +19,7 @@ interface GlobalChatHistory { let globalChatHistory: GlobalChatHistory = { chats: [ - { name: "Chat 1", messages: [{role:"system",content:"you are a helpful assistant"},{role:"assistant",content:"how can i help you"}], timestamp: 4 }, - { name: "Chat 2", messages: [{role:"system",content:"you are a helpful assistant"},{role:"assistant",content:"how can i help you"}], timestamp: 4 }, + { name: "Welcome!", messages: [{role:"system",content:"you are a helpful assistant"},{role:"assistant",content:"Hello! How can I help you?"}], timestamp: 4 }, ], selectedIndex:0 }