From b816e0925ec8bccea8857bd75898309f969fad47 Mon Sep 17 00:00:00 2001 From: Zacharias-Brohn Date: Wed, 14 Jan 2026 20:39:53 +0100 Subject: [PATCH] changes --- components/Chat/ChatLayout.tsx | 38 +++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/components/Chat/ChatLayout.tsx b/components/Chat/ChatLayout.tsx index 3aa4ccd..bb477ba 100644 --- a/components/Chat/ChatLayout.tsx +++ b/components/Chat/ChatLayout.tsx @@ -186,7 +186,43 @@ export default function ChatLayout() { role: 'assistant', content: result.message.content, }; - setMessages([...newMessages, responseMessage]); + const finalMessages = [...newMessages, responseMessage]; + setMessages(finalMessages); + + // Save both user message and assistant response to database + try { + // Save user message + const userSaveRes = await fetch('/api/chats', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + chatId: activeChatId, + messages: [userMessage], + }), + }); + const userSaveData = await userSaveRes.json(); + const savedChatId = userSaveData.chatId; + + // Update activeChatId if this was a new chat + if (!activeChatId && savedChatId) { + setActiveChatId(savedChatId); + } + + // Save assistant response + await fetch('/api/chats', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + chatId: savedChatId, + messages: [responseMessage], + }), + }); + + // Refresh chat list + fetchChats(); + } catch (saveError) { + console.error('Failed to save messages:', saveError); + } } else { // Error handling const errorMessage: Message = {