feat: improve compaction UI, show compaction message as status message

Introduces ActionStatus component and refactor
normal loading message into ActionStatus
This commit is contained in:
Ting-Wai To 2025-09-10 17:44:03 -07:00
parent 91478d9133
commit fc24deb115
7 changed files with 304 additions and 45 deletions

View file

@ -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({

View file

@ -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);

View file

@ -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<TUIChatProps> = ({
setChatHistory,
isWaitingForResponse,
responseStartTime,
isCompacting,
compactionStartTime,
inputMode,
activePermissionRequest,
wasInterrupted,
@ -290,18 +291,21 @@ const TUIChat: React.FC<TUIChatProps> = ({
{/* Fixed bottom section */}
<Box flexDirection="column" flexShrink={0}>
{/* Status */}
{isWaitingForResponse && responseStartTime && (
<Box paddingX={1} flexDirection="row" gap={1}>
<LoadingAnimation visible={isWaitingForResponse} />
<Text key="loading-start" color="gray">
(
</Text>
<Timer startTime={responseStartTime} />
<Text key="loading-end" color="gray">
• esc to interrupt )
</Text>
</Box>
)}
<ActionStatus
visible={isWaitingForResponse && !!responseStartTime}
startTime={responseStartTime || 0}
message=""
showSpinner={true}
/>
{/* Compaction Status */}
<ActionStatus
visible={isCompacting && !!compactionStartTime}
startTime={compactionStartTime || 0}
message="Compacting history"
showSpinner={true}
loadingColor="grey"
/>
{/* All screen-specific content */}
<ScreenContent

View file

@ -0,0 +1,214 @@
import { render } from "ink-testing-library";
import React from "react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createUITestContext } from "../../test-helpers/ui-test-context.js";
import { AppRoot } from "../AppRoot.js";
// Mock the useChat hook to control state
const mockUseChat = vi.fn();
vi.mock("../hooks/useChat.js", () => ({
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();
}
});
});

View file

@ -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<ActionStatusProps> = ({
visible,
startTime,
message,
showSpinner = false,
color = "gray",
loadingColor = "green",
}) => {
if (!visible) return null;
return (
<Box paddingX={1} flexDirection="row" gap={1}>
{showSpinner && <LoadingAnimation color={loadingColor} visible={true} />}
<Text color={color}>{message}</Text>
<Text color="gray">(</Text>
<Timer startTime={startTime} />
<Text color="gray">• esc to interrupt )</Text>
</Box>
);
};
export { ActionStatus };

View file

@ -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) {

View file

@ -138,6 +138,10 @@ export function useChat({
const [responseStartTime, setResponseStartTime] = useState<number | null>(
null,
);
const [isCompacting, setIsCompacting] = useState(false);
const [compactionStartTime, setCompactionStartTime] = useState<number | null>(
null,
);
const [inputMode, setInputMode] = useState(true);
const [abortController, setAbortController] =
useState<AbortController | null>(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,