From 6ea17d54c62389afc0fa29cf422255480c44268c Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Tue, 22 Jul 2025 21:21:49 +0530 Subject: [PATCH] feat(chat-window): fix wrong history while rewriting --- src/components/ChatWindow.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/ChatWindow.tsx b/src/components/ChatWindow.tsx index 67a5d0c..6ff7d22 100644 --- a/src/components/ChatWindow.tsx +++ b/src/components/ChatWindow.tsx @@ -354,7 +354,11 @@ const ChatWindow = ({ id }: { id?: string }) => { } }, [isMessagesLoaded, isConfigReady]); - const sendMessage = async (message: string, messageId?: string) => { + const sendMessage = async ( + message: string, + messageId?: string, + rewrite = false, + ) => { if (loading) return; if (!isConfigReady) { toast.error('Cannot send message before the configuration is ready'); @@ -482,6 +486,8 @@ const ChatWindow = ({ id }: { id?: string }) => { } }; + const messageIndex = messages.findIndex((m) => m.messageId === messageId); + const res = await fetch('/api/chat', { method: 'POST', headers: { @@ -498,7 +504,9 @@ const ChatWindow = ({ id }: { id?: string }) => { files: fileIds, focusMode: focusMode, optimizationMode: optimizationMode, - history: chatHistory, + history: rewrite + ? chatHistory.slice(0, messageIndex === -1 ? undefined : messageIndex) + : chatHistory, chatModel: { name: chatModelProvider.name, provider: chatModelProvider.provider, @@ -552,7 +560,7 @@ const ChatWindow = ({ id }: { id?: string }) => { return [...prev.slice(0, messages.length > 2 ? index - 1 : 0)]; }); - sendMessage(message.content, message.messageId); + sendMessage(message.content, message.messageId, true); }; useEffect(() => {