From 73135ffdc57206bb75cef923a11da1221878fdcf Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 5 Aug 2025 10:23:48 -0400 Subject: [PATCH] fix: address feedback --- src/commands/chat.ts | 9 +++++---- src/ui/hooks/useChat.ts | 41 ++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/commands/chat.ts b/src/commands/chat.ts index 92d6dd207..b4d4ca68e 100644 --- a/src/commands/chat.ts +++ b/src/commands/chat.ts @@ -168,10 +168,7 @@ async function processMessage( // Track user prompt telemetryService.logUserPrompt(userInput.length, userInput); - // Add user message to history - chatHistory.push({ role: "user", content: userInput }); - - // Check if auto-compacting is needed (always enabled) + // Check if auto-compacting is needed BEFORE adding user message if (shouldAutoCompact(chatHistory, model)) { logger.info("Auto-compacting triggered due to context limit"); @@ -196,6 +193,7 @@ async function processMessage( } try { + // Compact the history WITHOUT the current user message const result = await compactChatHistory(chatHistory, model, llmApi); // Replace chat history with compacted version @@ -241,6 +239,9 @@ async function processMessage( } } + // Add user message to history AFTER potential compaction + chatHistory.push({ role: "user", content: userInput }); + // Get AI response with potential tool usage if (!isHeadless) { console.info(`\n${chalk.bold.blue("Assistant:")}`); diff --git a/src/ui/hooks/useChat.ts b/src/ui/hooks/useChat.ts index f4581b33e..b9a099329 100644 --- a/src/ui/hooks/useChat.ts +++ b/src/ui/hooks/useChat.ts @@ -477,17 +477,11 @@ export function useChat({ return; } - // Local mode: process message normally - const newUserMessage: ChatCompletionMessageParam = { - role: "user", - content: messageContent, - }; - const newHistory = [...chatHistory, newUserMessage]; - setChatHistory(newHistory); - setMessages((prev) => [...prev, { role: "user", content: message }]); - - // Check if auto-compacting is needed (always enabled) - if (model && shouldAutoCompact(newHistory, model)) { + // Check if auto-compacting is needed BEFORE adding user message + let currentCompactionIndex = compactionIndex; + let currentChatHistory = chatHistory; + + if (model && shouldAutoCompact(chatHistory, model)) { logger.info("Auto-compacting triggered in TUI mode"); setMessages((prev) => [ @@ -500,11 +494,14 @@ export function useChat({ ]); try { - const result = await compactChatHistory(newHistory, model, llmApi!); + // Compact the history WITHOUT the current user message + const result = await compactChatHistory(chatHistory, model, llmApi!); - // Replace chat history with compacted version - newHistory.length = 0; - newHistory.push(...result.compactedHistory); + // Update local variables immediately + currentChatHistory = result.compactedHistory; + currentCompactionIndex = result.compactionIndex; + + // Update state setChatHistory(result.compactedHistory); setCompactionIndex(result.compactionIndex); @@ -536,6 +533,15 @@ export function useChat({ } } + // NOW add user message to history and UI + const newUserMessage: ChatCompletionMessageParam = { + role: "user", + content: messageContent, + }; + const newHistory = [...currentChatHistory, newUserMessage]; + setChatHistory(newHistory); + setMessages((prev) => [...prev, { role: "user", content: message }]); + // Start streaming response const controller = new AbortController(); setAbortController(controller); @@ -648,9 +654,10 @@ export function useChat({ // streamChatResponse modifies the history array in place, so we need to handle this carefully if (model && llmApi) { - if (compactionIndex !== null && compactionIndex !== undefined) { + // Use currentCompactionIndex which has the updated value after potential auto-compaction + if (currentCompactionIndex !== null && currentCompactionIndex !== undefined) { // When using compaction, we need to send a subset but capture the full history - const historyForLLM = getHistoryForLLM(newHistory, compactionIndex); + const historyForLLM = getHistoryForLLM(newHistory, currentCompactionIndex); const originalLength = historyForLLM.length; await streamChatResponse(