From fc24deb115aafba0929abd6a6140a51480ebbd11 Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Wed, 10 Sep 2025 17:44:03 -0700 Subject: [PATCH] feat: improve compaction UI, show compaction message as status message Introduces ActionStatus component and refactor normal loading message into ActionStatus --- .../streamChatResponse.autoCompaction.test.ts | 2 +- .../streamChatResponse.autoCompaction.ts | 2 +- extensions/cli/src/ui/TUIChat.tsx | 34 +-- .../__tests__/TUIChat.actionStatus.test.tsx | 214 ++++++++++++++++++ .../cli/src/ui/components/ActionStatus.tsx | 37 +++ .../cli/src/ui/hooks/useChat.compaction.ts | 39 ++-- extensions/cli/src/ui/hooks/useChat.ts | 21 +- 7 files changed, 304 insertions(+), 45 deletions(-) create mode 100644 extensions/cli/src/ui/__tests__/TUIChat.actionStatus.test.tsx create mode 100644 extensions/cli/src/ui/components/ActionStatus.tsx diff --git a/extensions/cli/src/stream/streamChatResponse.autoCompaction.test.ts b/extensions/cli/src/stream/streamChatResponse.autoCompaction.test.ts index 5960a952a..528e28be2 100644 --- a/extensions/cli/src/stream/streamChatResponse.autoCompaction.test.ts +++ b/extensions/cli/src/stream/streamChatResponse.autoCompaction.test.ts @@ -147,7 +147,7 @@ describe("handleAutoCompaction", () => { "Auto-compacting...", ); expect(mockCallbacks.onSystemMessage).toHaveBeenCalledWith( - "✓ Chat history auto-compacted successfully.", + "Chat history auto-compacted successfully.", ); expect(result).toEqual({ diff --git a/extensions/cli/src/stream/streamChatResponse.autoCompaction.ts b/extensions/cli/src/stream/streamChatResponse.autoCompaction.ts index 4e55a2cd5..797cfba2b 100644 --- a/extensions/cli/src/stream/streamChatResponse.autoCompaction.ts +++ b/extensions/cli/src/stream/streamChatResponse.autoCompaction.ts @@ -55,7 +55,7 @@ function handleCompactionSuccess( ) { if (isHeadless) return; - const successMessage = "✓ Chat history auto-compacted successfully."; + const successMessage = "Chat history auto-compacted successfully."; if (callbacks?.onSystemMessage) { callbacks.onSystemMessage(successMessage); diff --git a/extensions/cli/src/ui/TUIChat.tsx b/extensions/cli/src/ui/TUIChat.tsx index a60ec85b4..3d5195346 100644 --- a/extensions/cli/src/ui/TUIChat.tsx +++ b/extensions/cli/src/ui/TUIChat.tsx @@ -1,4 +1,4 @@ -import { Box, Text } from "ink"; +import { Box } from "ink"; import React, { useCallback, useEffect, @@ -17,6 +17,7 @@ import { } from "../services/types.js"; import { logger } from "../util/logger.js"; +import { ActionStatus } from "./components/ActionStatus.js"; import { BottomStatusBar } from "./components/BottomStatusBar.js"; import { ResourceDebugBar } from "./components/ResourceDebugBar.js"; import { ScreenContent } from "./components/ScreenContent.js"; @@ -31,8 +32,6 @@ import { useLoginHandlers, useSelectors, } from "./hooks/useTUIChatHooks.js"; -import { LoadingAnimation } from "./LoadingAnimation.js"; -import { Timer } from "./Timer.js"; interface TUIChatProps { // Remote mode props @@ -185,6 +184,8 @@ const TUIChat: React.FC = ({ setChatHistory, isWaitingForResponse, responseStartTime, + isCompacting, + compactionStartTime, inputMode, activePermissionRequest, wasInterrupted, @@ -290,18 +291,21 @@ const TUIChat: React.FC = ({ {/* Fixed bottom section */} {/* Status */} - {isWaitingForResponse && responseStartTime && ( - - - - ( - - - - • esc to interrupt ) - - - )} + + + {/* Compaction Status */} + {/* All screen-specific content */} ({ + useChat: () => mockUseChat(), +})); + +describe("TUIChat - ActionStatus", () => { + let context: any; + + beforeEach(() => { + context = createUITestContext({ + allServicesReady: true, + serviceState: "ready", + }); + + // Default mock return value + mockUseChat.mockReturnValue({ + chatHistory: [], + setChatHistory: vi.fn(), + isWaitingForResponse: false, + responseStartTime: null, + isCompacting: false, + compactionStartTime: null, + inputMode: true, + attachedFiles: [], + activePermissionRequest: null, + wasInterrupted: false, + handleUserMessage: vi.fn(), + handleInterrupt: vi.fn(), + handleFileAttached: vi.fn(), + resetChatHistory: vi.fn(), + handleToolPermissionResponse: vi.fn(), + }); + }); + + afterEach(() => { + context.cleanup(); + vi.clearAllMocks(); + }); + + it("shows only response status when waiting for response", () => { + const responseStartTime = Date.now(); + + mockUseChat.mockReturnValue({ + chatHistory: [], + setChatHistory: vi.fn(), + isWaitingForResponse: true, + responseStartTime, + isCompacting: false, + compactionStartTime: null, + inputMode: false, + attachedFiles: [], + activePermissionRequest: null, + wasInterrupted: false, + handleUserMessage: vi.fn(), + handleInterrupt: vi.fn(), + handleFileAttached: vi.fn(), + resetChatHistory: vi.fn(), + handleToolPermissionResponse: vi.fn(), + }); + + const { lastFrame, unmount } = render(React.createElement(AppRoot, {})); + + try { + const frame = lastFrame(); + expect(frame).toBeDefined(); + + // Should show spinner/response indicator but not compaction message + expect(frame).toContain("esc to interrupt"); + expect(frame).not.toContain("Compacting history"); + } finally { + unmount(); + } + }); + + it("shows only compaction status when compacting", () => { + const compactionStartTime = Date.now(); + + mockUseChat.mockReturnValue({ + chatHistory: [], + setChatHistory: vi.fn(), + isWaitingForResponse: false, + responseStartTime: null, + isCompacting: true, + compactionStartTime, + inputMode: true, + attachedFiles: [], + activePermissionRequest: null, + wasInterrupted: false, + handleUserMessage: vi.fn(), + handleInterrupt: vi.fn(), + handleFileAttached: vi.fn(), + resetChatHistory: vi.fn(), + handleToolPermissionResponse: vi.fn(), + }); + + const { lastFrame, unmount } = render(React.createElement(AppRoot, {})); + + try { + const frame = lastFrame(); + expect(frame).toBeDefined(); + + // Should show compaction message but not spinner + expect(frame).toContain("Compacting history"); + expect(frame).toContain("esc to interrupt"); + } finally { + unmount(); + } + }); + + it("shows neither status when both states are false", () => { + mockUseChat.mockReturnValue({ + chatHistory: [], + setChatHistory: vi.fn(), + isWaitingForResponse: false, + responseStartTime: null, + isCompacting: false, + compactionStartTime: null, + inputMode: true, + attachedFiles: [], + activePermissionRequest: null, + wasInterrupted: false, + handleUserMessage: vi.fn(), + handleInterrupt: vi.fn(), + handleFileAttached: vi.fn(), + resetChatHistory: vi.fn(), + handleToolPermissionResponse: vi.fn(), + }); + + const { lastFrame, unmount } = render(React.createElement(AppRoot, {})); + + try { + const frame = lastFrame(); + expect(frame).toBeDefined(); + + // Should not show any status messages + expect(frame).not.toContain("Compacting history"); + // The basic UI should still be there + expect(frame).toContain("Ask anything"); + } finally { + unmount(); + } + }); + + it("handles state transitions correctly", () => { + const { lastFrame, rerender, unmount } = render( + React.createElement(AppRoot, {}), + ); + + // Start with compaction + const compactionStartTime = Date.now(); + mockUseChat.mockReturnValue({ + chatHistory: [], + setChatHistory: vi.fn(), + isWaitingForResponse: false, + responseStartTime: null, + isCompacting: true, + compactionStartTime, + inputMode: true, + attachedFiles: [], + activePermissionRequest: null, + wasInterrupted: false, + handleUserMessage: vi.fn(), + handleInterrupt: vi.fn(), + handleFileAttached: vi.fn(), + resetChatHistory: vi.fn(), + handleToolPermissionResponse: vi.fn(), + }); + + try { + rerender(React.createElement(AppRoot, {})); + + let frame = lastFrame(); + expect(frame).toContain("Compacting history"); + expect(frame).not.toContain("⠀⠁⠃⠇⠏⠟⠿⣿"); // No spinner chars when only compacting + + // Transition to response waiting + const responseStartTime = Date.now(); + mockUseChat.mockReturnValue({ + chatHistory: [], + setChatHistory: vi.fn(), + isWaitingForResponse: true, + responseStartTime, + isCompacting: false, + compactionStartTime: null, + inputMode: false, + attachedFiles: [], + activePermissionRequest: null, + wasInterrupted: false, + handleUserMessage: vi.fn(), + handleInterrupt: vi.fn(), + handleFileAttached: vi.fn(), + resetChatHistory: vi.fn(), + handleToolPermissionResponse: vi.fn(), + }); + + rerender(React.createElement(AppRoot, {})); + + frame = lastFrame(); + expect(frame).not.toContain("Compacting history"); + expect(frame).toContain("esc to interrupt"); + } finally { + unmount(); + } + }); +}); diff --git a/extensions/cli/src/ui/components/ActionStatus.tsx b/extensions/cli/src/ui/components/ActionStatus.tsx new file mode 100644 index 000000000..ce4542cb6 --- /dev/null +++ b/extensions/cli/src/ui/components/ActionStatus.tsx @@ -0,0 +1,37 @@ +import { Box, Text } from "ink"; +import React from "react"; + +import { LoadingAnimation } from "../LoadingAnimation.js"; +import { Timer } from "../Timer.js"; + +interface ActionStatusProps { + visible: boolean; + startTime: number; + message: string; + showSpinner?: boolean; + color?: string; + loadingColor?: string; +} + +const ActionStatus: React.FC = ({ + visible, + startTime, + message, + showSpinner = false, + color = "gray", + loadingColor = "green", +}) => { + if (!visible) return null; + + return ( + + {showSpinner && } + {message} + ( + + • esc to interrupt ) + + ); +}; + +export { ActionStatus }; diff --git a/extensions/cli/src/ui/hooks/useChat.compaction.ts b/extensions/cli/src/ui/hooks/useChat.compaction.ts index 8952f7643..adfa175de 100644 --- a/extensions/cli/src/ui/hooks/useChat.compaction.ts +++ b/extensions/cli/src/ui/hooks/useChat.compaction.ts @@ -116,18 +116,6 @@ export async function handleAutoCompaction({ logger.info("Auto-compaction triggered for TUI mode"); - // Add compacting message - setChatHistory((prev) => [ - ...prev, - { - message: { - role: "system", - content: "Auto-compacting chat history...", - }, - contextItems: [], - }, - ]); - // Compact the unified history const result = await compactChatHistory(chatHistory, model, llmApi); @@ -151,23 +139,22 @@ export async function handleAutoCompaction({ ? [systemMessage, compactedMessage] : [compactedMessage]; - setChatHistory(updatedHistory); + // Add success message to the history that will be returned + const successMessage: ChatHistoryItem = { + message: { + role: "system", + content: "Chat history auto-compacted successfully.", + }, + contextItems: [], + }; + + const finalHistory = [...updatedHistory, successMessage]; + + setChatHistory(finalHistory); setCompactionIndex(updatedHistory.length - 1); - // Add success message - setChatHistory((prev) => [ - ...prev, - { - message: { - role: "system", - content: "✓ Chat history auto-compacted successfully.", - }, - contextItems: [], - }, - ]); - return { - currentChatHistory: updatedHistory, + currentChatHistory: finalHistory, currentCompactionIndex: updatedHistory.length - 1, }; } catch (error) { diff --git a/extensions/cli/src/ui/hooks/useChat.ts b/extensions/cli/src/ui/hooks/useChat.ts index 9b9cc5871..bc0d8dffd 100644 --- a/extensions/cli/src/ui/hooks/useChat.ts +++ b/extensions/cli/src/ui/hooks/useChat.ts @@ -138,6 +138,10 @@ export function useChat({ const [responseStartTime, setResponseStartTime] = useState( null, ); + const [isCompacting, setIsCompacting] = useState(false); + const [compactionStartTime, setCompactionStartTime] = useState( + null, + ); const [inputMode, setInputMode] = useState(true); const [abortController, setAbortController] = useState(null); @@ -470,8 +474,12 @@ export function useChat({ } // Check if auto-compacting is needed BEFORE adding user message - const { currentChatHistory, currentCompactionIndex } = - await handleAutoCompaction({ + setIsCompacting(true); + setCompactionStartTime(Date.now()); + + let currentChatHistory, currentCompactionIndex; + try { + const result = await handleAutoCompaction({ chatHistory, model, llmApi, @@ -479,6 +487,13 @@ export function useChat({ setChatHistory: setChatHistory, setCompactionIndex, }); + + currentChatHistory = result.currentChatHistory; + currentCompactionIndex = result.currentCompactionIndex; + } finally { + setIsCompacting(false); + setCompactionStartTime(null); + } // Add the formatted user message to history const newHistory = [...currentChatHistory, newUserMessage]; @@ -610,6 +625,8 @@ export function useChat({ setChatHistory: setChatHistory, isWaitingForResponse, responseStartTime, + isCompacting, + compactionStartTime, inputMode, attachedFiles, activePermissionRequest,