From 600b11701c854eac462d15ec14ea1ff84348719f Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 26 Aug 2025 08:51:20 -0700 Subject: [PATCH] fix: autocompaction translation bug --- extensions/cli/src/streamChatResponse.ts | 2 - .../cli/src/ui/hooks/useChat.helpers.ts | 124 ++++++++++++------ extensions/cli/src/ui/hooks/useChat.ts | 2 +- 3 files changed, 86 insertions(+), 42 deletions(-) diff --git a/extensions/cli/src/streamChatResponse.ts b/extensions/cli/src/streamChatResponse.ts index 756651bc1..6a703200e 100644 --- a/extensions/cli/src/streamChatResponse.ts +++ b/extensions/cli/src/streamChatResponse.ts @@ -242,13 +242,11 @@ function processChunk(options: ProcessChunkOptions): { } = options; // Safety check: ensure chunk has the expected structure if (!chunk.choices || !chunk.choices[0]) { - logger.warn("Malformed chunk received - missing choices", { chunk }); return { aiResponse, shouldContinue: true }; } const choice = chunk.choices[0]; if (!choice.delta) { - logger.warn("Malformed chunk received - missing delta", { chunk }); return { aiResponse, shouldContinue: true }; } diff --git a/extensions/cli/src/ui/hooks/useChat.helpers.ts b/extensions/cli/src/ui/hooks/useChat.helpers.ts index 22b03609b..dc0dd0dec 100644 --- a/extensions/cli/src/ui/hooks/useChat.helpers.ts +++ b/extensions/cli/src/ui/hooks/useChat.helpers.ts @@ -1,15 +1,13 @@ import type { ChatHistoryItem, Session } from "../../../../../core/index.js"; import { initializeChatHistory } from "../../commands/chat.js"; import { compactChatHistory } from "../../compaction.js"; -import { - convertFromUnifiedHistory, - convertToUnifiedHistory, -} from "../../messageConversion.js"; +import { convertFromUnifiedHistory } from "../../messageConversion.js"; import { loadSession, saveSession } from "../../session.js"; import { posthogService } from "../../telemetry/posthogService.js"; import { telemetryService } from "../../telemetry/telemetryService.js"; import { formatError } from "../../util/formatError.js"; import { logger } from "../../util/logger.js"; +import { shouldAutoCompact } from "../../util/tokenizer.js"; import { SlashCommandResult } from "./useChat.types.js"; @@ -303,43 +301,91 @@ export async function handleAutoCompaction({ currentChatHistory: ChatHistoryItem[]; currentCompactionIndex: number | null; }> { - const { handleAutoCompaction: coreAutoCompaction } = await import( - "../../streamChatResponse.autoCompaction.js" - ); - - // Convert to legacy format for compaction + // Check if auto-compaction is needed + // Convert to legacy format for token counting const legacyHistory = convertFromUnifiedHistory(chatHistory); + if (!model || !shouldAutoCompact(legacyHistory, model)) { + return { + currentChatHistory: chatHistory, + currentCompactionIndex: _compactionIndex, + }; + } - const result = await coreAutoCompaction(legacyHistory, model, llmApi, { - isHeadless: false, - callbacks: { - setMessages: (updater: any) => { - // Convert messages updates to chat history updates - if (typeof updater === "function") { - setChatHistory((prev) => { - // For now, just return prev - compaction messages will be handled separately - return prev; - }); - } + try { + logger.info("Auto-compaction triggered for TUI mode"); + + // Add compacting message + setChatHistory((prev) => [ + ...prev, + { + message: { + role: "system", + content: "Auto-compacting chat history...", + }, + contextItems: [], }, - setChatHistory: (updater: any) => { - // Handle chat history updates - if (typeof updater === "function") { - setChatHistory((prev) => { - const legacyPrev = convertFromUnifiedHistory(prev); - const updated = updater(legacyPrev); - return convertToUnifiedHistory(updated); - }); - } else { - setChatHistory(convertToUnifiedHistory(updater)); - } - }, - setCompactionIndex, - }, - }); + ]); - return { - currentChatHistory: convertToUnifiedHistory(result.chatHistory), - currentCompactionIndex: result.compactionIndex, - }; + // Compact the unified history + const result = await compactChatHistory(chatHistory, model, llmApi); + + // Keep the system message and append the compaction summary + // This replaces the old messages with a summary to reduce context size + const systemMessage = chatHistory.find( + (item) => item.message.role === "system" + ); + + const compactedMessage: ChatHistoryItem = { + message: { + role: "assistant" as const, + content: result.compactionContent, + }, + contextItems: [], + conversationSummary: result.compactionContent, // Mark this as a summary + }; + + // Create new history with system message (if exists) and compaction summary + const updatedHistory: ChatHistoryItem[] = systemMessage + ? [systemMessage, compactedMessage] + : [compactedMessage]; + + setChatHistory(updatedHistory); + setCompactionIndex(updatedHistory.length - 1); + + // Add success message + setChatHistory((prev) => [ + ...prev, + { + message: { + role: "system", + content: "✓ Chat history auto-compacted successfully.", + }, + contextItems: [], + }, + ]); + + return { + currentChatHistory: updatedHistory, + currentCompactionIndex: updatedHistory.length - 1, + }; + } catch (error) { + logger.error("Auto-compaction failed:", error); + + // Add error message + setChatHistory((prev) => [ + ...prev, + { + message: { + role: "system", + content: `Auto-compaction failed: ${formatError(error)}. Continuing without compaction...`, + }, + contextItems: [], + }, + ]); + + return { + currentChatHistory: chatHistory, + currentCompactionIndex: null, + }; + } } diff --git a/extensions/cli/src/ui/hooks/useChat.ts b/extensions/cli/src/ui/hooks/useChat.ts index cdcb280f2..1f046d715 100644 --- a/extensions/cli/src/ui/hooks/useChat.ts +++ b/extensions/cli/src/ui/hooks/useChat.ts @@ -5,7 +5,7 @@ import type { ChatHistoryItem, Session } from "../../../../../core/index.js"; import { findCompactionIndex } from "../../compaction.js"; import { toolPermissionManager } from "../../permissions/permissionManager.js"; import { services } from "../../services/index.js"; -import { loadSession, saveSession, createSession } from "../../session.js"; +import { createSession, loadSession, saveSession } from "../../session.js"; import { handleSlashCommands } from "../../slashCommands.js"; import { telemetryService } from "../../telemetry/telemetryService.js"; import { formatError } from "../../util/formatError.js";