From ce3c2068c60d01364b82cac99660058b2e375edb Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Sat, 23 Aug 2025 01:15:15 -0700 Subject: [PATCH 1/8] chore: refactor cn box styling, mcp selector, update clearer arrow icon --- .../cli/src/ui/FreeTrialTransitionUI.tsx | 77 +++++++------------ extensions/cli/src/ui/MCPSelector.tsx | 31 ++++---- extensions/cli/src/ui/Selector.tsx | 20 ++--- extensions/cli/src/ui/ServiceDebugger.tsx | 9 +-- extensions/cli/src/ui/SessionSelector.tsx | 20 +++-- extensions/cli/src/ui/SlashCommandUI.tsx | 2 +- .../__tests__/FreeTrialTransitionUI.test.tsx | 16 ++-- .../ui/components/ToolPermissionSelector.tsx | 9 +-- extensions/cli/src/ui/styles.ts | 17 ++++ 9 files changed, 89 insertions(+), 112 deletions(-) create mode 100644 extensions/cli/src/ui/styles.ts diff --git a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx index 5a4638e2e..d23cfceba 100644 --- a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx +++ b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx @@ -13,8 +13,8 @@ import { isValidAnthropicApiKey, } from "../util/apiKeyValidation.js"; import { updateAnthropicModelInYaml } from "../util/yamlConfigUpdater.js"; - import { useNavigation } from "./context/NavigationContext.js"; +import { defaultBoxStyles } from "./styles.js"; const CONFIG_PATH = path.join(env.continueHome, "config.yaml"); @@ -215,39 +215,38 @@ const FreeTrialTransitionUI: React.FC = ({ }; if (currentStep === "choice") { + const options = [ + { num: 1, text: "💳 Sign up for models add-on (recommended)" }, + { num: 2, text: "🔑 Enter your Anthropic API key" }, + { num: 3, text: "⚙️ Switch to a different configuration" }, + ]; + + if (hasOrganizations) { + options.push({ + num: 4, + text: "🏢 Switch to organization configuration", + }); + } + return ( - + 🚀 Free trial limit reached! Choose how you'd like to continue: - - {selectedOption === 1 ? "▶ " : " "}1. 💳 Sign up for models add-on - (recommended) - - - {selectedOption === 2 ? "▶ " : " "}2. 🔑 Enter your Anthropic API - key - - - {selectedOption === 3 ? "▶ " : " "}3. ⚙️ Switch to a different - configuration - - {hasOrganizations && ( - - {selectedOption === 4 ? "▶ " : " "}4. 🏢 Switch to organization - configuration + {options.map((option) => ( + + {selectedOption === option.num ? "➤ " : " "} + {option.num}. {option.text} - )} + ))} - Use ↑↓ arrows or {hasOrganizations ? "1/2/3/4" : "1/2/3"} to select, + ↑/↓ to navigate or {hasOrganizations ? "1/2/3/4" : "1/2/3"} to select, Enter to confirm @@ -256,12 +255,7 @@ const FreeTrialTransitionUI: React.FC = ({ if (currentStep === "enterApiKey") { return ( - + Enter your Anthropic API key @@ -278,12 +272,7 @@ const FreeTrialTransitionUI: React.FC = ({ if (currentStep === "processing") { return ( - + Processing... @@ -294,12 +283,7 @@ const FreeTrialTransitionUI: React.FC = ({ if (currentStep === "success") { return ( - + Success! @@ -316,12 +300,7 @@ const FreeTrialTransitionUI: React.FC = ({ if (currentStep === "error") { return ( - + Error diff --git a/extensions/cli/src/ui/MCPSelector.tsx b/extensions/cli/src/ui/MCPSelector.tsx index fb84a4c60..80c52f6e5 100644 --- a/extensions/cli/src/ui/MCPSelector.tsx +++ b/extensions/cli/src/ui/MCPSelector.tsx @@ -3,11 +3,17 @@ import React, { useState } from "react"; import { quote } from "shell-quote"; import { useServices } from "../hooks/useService.js"; -import { MCPServiceState, SERVICE_NAMES } from "../services/types.js"; +import { + MCPConnectionInfo, + MCPServerStatus, + MCPServiceState, + SERVICE_NAMES, +} from "../services/types.js"; import { logger } from "../util/logger.js"; +import { defaultBoxStyles } from "./styles.js"; // Utility function to get status icon and color based on server connection -const getServerStatusDisplay = (conn: any) => { +const getServerStatusDisplay = (conn: MCPConnectionInfo) => { let icon = "⚪️"; // note, white circle causes extra blank line bug let color: "green" | "yellow" | "red" | "white" | "dim" = "white"; let statusText = conn.status; @@ -22,7 +28,7 @@ const getServerStatusDisplay = (conn: any) => { if (conn.warnings && conn.warnings.length > 0) { icon = "🟡"; color = "yellow"; - statusText = "connected (with warnings)"; + statusText = "connected (with warnings)" as MCPServerStatus; } else { icon = "🟢"; color = "green"; @@ -170,7 +176,7 @@ export const MCPSelector: React.FC = ({ onCancel }) => { {/* Add prompts if any */} {serverInfo.prompts.length > 0 && ( - 📝 Prompts: {serverInfo.prompts.length} + 📝 Prompts: {serverInfo.prompts.length} {serverInfo.prompts.map((prompt, index) => ( • {prompt.name} @@ -182,7 +188,7 @@ export const MCPSelector: React.FC = ({ onCancel }) => { {/* Add tools if any */} {serverInfo.tools.length > 0 && ( - 🔧 Tools: {serverInfo.tools.length} + 🔧 Tools: {serverInfo.tools.length} {serverInfo.tools.map((tool, index) => ( • {tool.name} @@ -302,7 +308,7 @@ export const MCPSelector: React.FC = ({ onCancel }) => { const renderHeader = () => { return ( - + {menuState === "server-detail" && selectedServer ? `Server: ${selectedServer}` : "MCP Servers"} @@ -336,13 +342,7 @@ export const MCPSelector: React.FC = ({ onCancel }) => { }; return ( - + {renderHeader()} {message && ( @@ -369,9 +369,8 @@ export const MCPSelector: React.FC = ({ onCancel }) => { key={`${item.value}-${index}`} color={isSelected ? "blue" : "white"} bold={isSelected} - inverse={isSelected} > - {isSelected ? "> " : " "} + {isSelected ? "➤ " : " "} {item.label} ); @@ -381,7 +380,7 @@ export const MCPSelector: React.FC = ({ onCancel }) => { - Use ↑/↓ to navigate, Enter to select, Esc to go back + ↑/↓ to navigate, Enter to select, Esc to go back diff --git a/extensions/cli/src/ui/Selector.tsx b/extensions/cli/src/ui/Selector.tsx index 6d00660f0..3c76446df 100644 --- a/extensions/cli/src/ui/Selector.tsx +++ b/extensions/cli/src/ui/Selector.tsx @@ -1,20 +1,14 @@ import { Box, Text, useInput } from "ink"; import React, { ReactNode } from "react"; +import { defaultBoxStyles } from "./styles.js"; + export interface SelectorOption { id: string; name: string; displaySuffix?: string; } -const boxStyles: React.ComponentProps = { - flexDirection: "column", - paddingX: 2, - paddingY: 1, - borderStyle: "round", - borderColor: "blue", -}; - interface SelectorProps { title: string; options: T[]; @@ -70,7 +64,7 @@ export function Selector({ if (loading) { return ( - + {title} @@ -84,7 +78,7 @@ export function Selector({ if (error) { return ( - + Error @@ -106,7 +100,7 @@ export function Selector({ color={isSelected ? "blue" : isCurrent ? "green" : "white"} bold={isSelected} > - {isSelected ? "→ " : " "} + {isSelected ? "➤ " : " "} {option.name} {option.displaySuffix || ""} @@ -119,7 +113,7 @@ export function Selector({ ); return ( - + {title} @@ -136,7 +130,7 @@ export function Selector({ - ↑/↓ to navigate, Enter to select, Escape to cancel + ↑/↓ to navigate, Enter to select, Esc to cancel diff --git a/extensions/cli/src/ui/ServiceDebugger.tsx b/extensions/cli/src/ui/ServiceDebugger.tsx index 1f121bd0a..4b03a525a 100644 --- a/extensions/cli/src/ui/ServiceDebugger.tsx +++ b/extensions/cli/src/ui/ServiceDebugger.tsx @@ -1,6 +1,7 @@ import { Box, Text } from "ink"; import React from "react"; +import { defaultBoxStyles } from "./styles.js"; import { ApiClientServiceState, AuthServiceState, @@ -60,13 +61,7 @@ const ServiceDebugger: React.FC = ({ }; return ( - + 🔧 Service Debug Info diff --git a/extensions/cli/src/ui/SessionSelector.tsx b/extensions/cli/src/ui/SessionSelector.tsx index 4e085f197..fa32a4680 100644 --- a/extensions/cli/src/ui/SessionSelector.tsx +++ b/extensions/cli/src/ui/SessionSelector.tsx @@ -1,10 +1,10 @@ -import { format, isToday, isYesterday, isThisWeek, isThisYear } from "date-fns"; +import { format, isThisWeek, isThisYear, isToday, isYesterday } from "date-fns"; import { Box, Text, useInput } from "ink"; import React, { useMemo, useState } from "react"; import { SessionMetadata } from "../session.js"; - import { useTerminalSize } from "./hooks/useTerminalSize.js"; +import { defaultBoxStyles } from "./styles.js"; interface SessionSelectorProps { sessions: SessionMetadata[]; @@ -70,33 +70,31 @@ export function SessionSelector({ if (sessions.length === 0) { return ( - + No previous sessions found. Start a new conversation with: cn - Press ESC to exit + Press Esc to exit ); } return ( - + Recent Sessions - - Use ↑↓ arrows to navigate, Enter to select, ESC to exit - + ↑/↓ to navigate, Enter to select, Esc to exit {displaySessions.map((session, index) => { const isSelected = index === selectedIndex; - const indicator = isSelected ? "► " : " "; - const color = isSelected ? "cyan" : "white"; + const indicator = isSelected ? "➤ " : " "; + const color = isSelected ? "blue" : "white"; return ( - + {indicator} {formatMessage(session.firstUserMessage)} diff --git a/extensions/cli/src/ui/SlashCommandUI.tsx b/extensions/cli/src/ui/SlashCommandUI.tsx index b4852933f..15d88bf29 100644 --- a/extensions/cli/src/ui/SlashCommandUI.tsx +++ b/extensions/cli/src/ui/SlashCommandUI.tsx @@ -73,7 +73,7 @@ const SlashCommandUI: React.FC = ({ {" "} {paddedCommandName} - {" "} + {" "} {command.description} diff --git a/extensions/cli/src/ui/__tests__/FreeTrialTransitionUI.test.tsx b/extensions/cli/src/ui/__tests__/FreeTrialTransitionUI.test.tsx index 1fea1b3fb..dfb3b59ce 100644 --- a/extensions/cli/src/ui/__tests__/FreeTrialTransitionUI.test.tsx +++ b/extensions/cli/src/ui/__tests__/FreeTrialTransitionUI.test.tsx @@ -37,7 +37,7 @@ describe("FreeTrialTransitionUI - Rendering and Props Tests", () => { expect(frame).toContain("1. 💳 Sign up for models add-on"); expect(frame).toContain("2. 🔑 Enter your Anthropic API key"); expect(frame).toContain("3. ⚙️ Switch to a different configuration"); - expect(frame).toContain("Use ↑↓ arrows or 1/2/3 to select"); + expect(frame).toContain("↑/↓ to navigate or 1/2/3 to select"); }); it("highlights first option by default", () => { @@ -46,10 +46,10 @@ describe("FreeTrialTransitionUI - Rendering and Props Tests", () => { ); const frame = lastFrame(); - expect(frame).toContain("▶ 1. 💳 Sign up for models add-on"); - expect(frame).not.toContain("▶ 2. 🔑 Enter your Anthropic API key"); + expect(frame).toContain("➤ 1. 💳 Sign up for models add-on"); + expect(frame).not.toContain("➤ 2. 🔑 Enter your Anthropic API key"); expect(frame).not.toContain( - "▶ 3. ⚙️ Switch to a different configuration", + "➤ 3. ⚙️ Switch to a different configuration", ); }); @@ -85,7 +85,7 @@ describe("FreeTrialTransitionUI - Rendering and Props Tests", () => { const frame = lastFrame(); expect(frame).toContain("Choose how you'd like to continue:"); expect(frame).toContain( - "Use ↑↓ arrows or 1/2/3 to select, Enter to confirm", + "↑/↓ to navigate or 1/2/3 to select, Enter to confirm", ); }); @@ -155,9 +155,9 @@ describe("FreeTrialTransitionUI - Rendering and Props Tests", () => { line.includes("Switch to a different configuration"), ); - expect(modelsLine).toContain("▶"); - expect(apiKeyLine).not.toContain("▶"); - expect(configLine).not.toContain("▶"); + expect(modelsLine).toContain("➤"); + expect(apiKeyLine).not.toContain("➤"); + expect(configLine).not.toContain("➤"); }); }); diff --git a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx index fed7c2203..c4b93ab35 100644 --- a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx +++ b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx @@ -3,7 +3,7 @@ import React, { useState } from "react"; import { formatToolCall } from "../../tools/index.js"; import { ToolCallPreview } from "../../tools/types.js"; - +import { defaultBoxStyles } from "../styles.js"; import { ToolPreview } from "./ToolPreview.js"; interface PermissionOption { @@ -104,12 +104,7 @@ export const ToolPermissionSelector: React.FC = ({ const toolCallDisplay = formatToolCall(toolName, toolArgs); return ( - + {toolCallDisplay} diff --git a/extensions/cli/src/ui/styles.ts b/extensions/cli/src/ui/styles.ts new file mode 100644 index 000000000..4e767b0b8 --- /dev/null +++ b/extensions/cli/src/ui/styles.ts @@ -0,0 +1,17 @@ +import { Box } from "ink"; + +export const baseBoxStyles: React.ComponentProps = { + flexDirection: "column", + paddingX: 2, + paddingY: 1, + borderStyle: "round", +}; + +export const defaultBoxStyles = ( + borderColor: string, + overrides?: Partial>, +): React.ComponentProps => ({ + ...baseBoxStyles, + borderColor, + ...overrides, +}); From 2c95e2c3223d9fee7269ca958aa28b78a8646a0e Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Sat, 23 Aug 2025 02:49:20 -0700 Subject: [PATCH 2/8] feat: ctrl+c once to clear input, twice to exit --- extensions/cli/spec/ctrl-c-behavior.md | 28 +++++ .../__tests__/CtrlCProcessHandling.test.ts | 12 ++ extensions/cli/src/index.ts | 64 +++++++++- .../cli/src/ui/FreeTrialTransitionUI.tsx | 12 ++ extensions/cli/src/ui/MCPSelector.tsx | 7 +- extensions/cli/src/ui/Selector.tsx | 16 ++- extensions/cli/src/ui/UserInput.tsx | 8 ++ .../__tests__/BottomStatusBarCtrlC.test.tsx | 63 ++++++++++ .../cli/src/ui/__tests__/CtrlC.test.tsx | 119 ++++++++++++++++++ .../ui/__tests__/CtrlCInputClearing.test.tsx | 63 ++++++++++ .../cli/src/ui/components/BottomStatusBar.tsx | 94 ++++++++------ .../ui/components/ToolPermissionSelector.tsx | 4 +- extensions/cli/src/ui/hooks/useUserInput.ts | 17 ++- extensions/cli/src/ui/index.ts | 21 ++-- 14 files changed, 464 insertions(+), 64 deletions(-) create mode 100644 extensions/cli/spec/ctrl-c-behavior.md create mode 100644 extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts create mode 100644 extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx create mode 100644 extensions/cli/src/ui/__tests__/CtrlC.test.tsx create mode 100644 extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx diff --git a/extensions/cli/spec/ctrl-c-behavior.md b/extensions/cli/spec/ctrl-c-behavior.md new file mode 100644 index 000000000..72fd49b17 --- /dev/null +++ b/extensions/cli/spec/ctrl-c-behavior.md @@ -0,0 +1,28 @@ +# Ctrl+C Exit Behavior + +## Overview + +Ctrl+C operates on two levels: +1. **Menus/Selectors**: Immediate cancellation and return to previous state +2. **Main Application**: Two-stage exit requiring double Ctrl+C within 1 second + +## Two-Stage Exit (Main Application) + +### First Ctrl+C +- Shows temporary status message "ctrl+c to exit" in bottom-left status bar +- Message automatically disappears after 1 second +- Clears any text in the input field (if focused) +- Does **not** exit the application + +### Second Ctrl+C (within 1 second) +- Exits the application immediately +- Performs graceful shutdown + +### Second Ctrl+C (after 1 second) +- Treated as a new "first Ctrl+C" +- Shows exit message again +- User must press Ctrl+C twice within 1 second window to actually exit + +## Menu/Selector Cancellation + +Ctrl+C immediately cancels and returns to the previous state in all interactive menus and selectors. diff --git a/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts b/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts new file mode 100644 index 000000000..31782b653 --- /dev/null +++ b/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it, vi } from "vitest"; + +describe("Process SIGINT handling functions", () => { + it("exports the necessary functions for exit message handling", async () => { + const indexModule = await import("../index.js"); + + // Verify the functions exist + expect(typeof indexModule.shouldShowExitMessage).toBe("function"); + expect(typeof indexModule.setExitMessageCallback).toBe("function"); + expect(typeof indexModule.setTUIUnmount).toBe("function"); + }); +}); \ No newline at end of file diff --git a/extensions/cli/src/index.ts b/extensions/cli/src/index.ts index cf3040968..78d10729e 100644 --- a/extensions/cli/src/index.ts +++ b/extensions/cli/src/index.ts @@ -23,6 +23,35 @@ import { logger } from "./util/logger.js"; import { readStdinSync } from "./util/stdin.js"; import { getVersion } from "./version.js"; +// TUI lifecycle and two-stage exit state management +let tuiUnmount: (() => void) | null; +let showExitMessage: boolean; +let exitMessageCallback: (() => void) | null; +let lastCtrlCTime: number; + +// Initialize state immediately to avoid temporal dead zone issues with exported functions +(function initializeTUIState() { + tuiUnmount = null; + showExitMessage = false; + exitMessageCallback = null; + lastCtrlCTime = 0; +})(); + +// Register TUI cleanup function for graceful shutdown +export function setTUIUnmount(unmount: () => void) { + tuiUnmount = unmount; +} + +// Register callback to trigger UI updates when exit message state changes +export function setExitMessageCallback(callback: () => void) { + exitMessageCallback = callback; +} + +// Check if "ctrl+c to exit" message should be displayed +export function shouldShowExitMessage(): boolean { + return showExitMessage; +} + // Add global error handlers to prevent uncaught errors from crashing the process process.on("unhandledRejection", (reason, promise) => { logger.error("Unhandled Rejection at:", { promise, reason }); @@ -262,10 +291,39 @@ try { process.exit(1); } -// Graceful shutdown - flush Sentry data +// Graceful shutdown with two-stage Ctrl+C exit + +// Remove all existing SIGINT listeners first +process.removeAllListeners("SIGINT"); + process.on("SIGINT", async () => { - await sentryService.flush(); - process.exit(0); + const now = Date.now(); + const timeSinceLastCtrlC = now - lastCtrlCTime; + + if (timeSinceLastCtrlC <= 1000 && lastCtrlCTime !== 0) { + // Second Ctrl+C within 1 second - exit + showExitMessage = false; + if (tuiUnmount) { + tuiUnmount(); + } + await sentryService.flush(); + process.exit(0); + } else { + // First Ctrl+C or too much time elapsed - show exit message + lastCtrlCTime = now; + showExitMessage = true; + if (exitMessageCallback) { + exitMessageCallback(); + } + + // Hide message after 1 second + setTimeout(() => { + showExitMessage = false; + if (exitMessageCallback) { + exitMessageCallback(); + } + }, 1000); + } }); process.on("SIGTERM", async () => { diff --git a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx index d23cfceba..19fc8ca5d 100644 --- a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx +++ b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx @@ -130,6 +130,18 @@ const FreeTrialTransitionUI: React.FC = ({ const hasOrganizations = organizations && organizations.length > 0; useInput((input, key) => { + // Allow Escape to cancel immediately, but Ctrl+C uses two-stage exit + if (key.escape) { + closeCurrentScreen(); + return; + } + + // Handle Ctrl+C with two-stage exit (delegates to main process) + if (key.ctrl && input === "c") { + process.kill(process.pid, "SIGINT"); + return; + } + if (currentStep === "choice") { handleChoiceInput({ input, diff --git a/extensions/cli/src/ui/MCPSelector.tsx b/extensions/cli/src/ui/MCPSelector.tsx index 80c52f6e5..a06372fec 100644 --- a/extensions/cli/src/ui/MCPSelector.tsx +++ b/extensions/cli/src/ui/MCPSelector.tsx @@ -216,13 +216,14 @@ export const MCPSelector: React.FC = ({ onCancel }) => { const maxIndex = Math.max(0, items.length - 1); useInput(async (input, key) => { - if (isLoading) return; - - if (key.escape) { + // Always allow escape/Ctrl+C so users can cancel even when loading + if (key.escape || (key.ctrl && input === "c")) { handleBack(); return; } + if (isLoading) return; + if (key.upArrow) { setSelectedIndex(selectedIndex <= 0 ? maxIndex : selectedIndex - 1); return; diff --git a/extensions/cli/src/ui/Selector.tsx b/extensions/cli/src/ui/Selector.tsx index 3c76446df..b8ba5f4c4 100644 --- a/extensions/cli/src/ui/Selector.tsx +++ b/extensions/cli/src/ui/Selector.tsx @@ -40,8 +40,20 @@ export function Selector({ onNavigate, renderOption, }: SelectorProps) { + // Handle input for loading and error states useInput((input, key) => { - if (key.escape) { + if (loading || error) { + if (key.escape || (key.ctrl && input === "c")) { + onCancel(); + return; + } + } + }); + + useInput((input, key) => { + if (loading || error) return; // Skip normal input handling during loading/error + + if (key.escape || (key.ctrl && input === "c")) { onCancel(); return; } @@ -84,7 +96,7 @@ export function Selector({ {error} - Press Escape to cancel + Press Esc to cancel ); diff --git a/extensions/cli/src/ui/UserInput.tsx b/extensions/cli/src/ui/UserInput.tsx index 0ebfc2858..466d8b421 100644 --- a/extensions/cli/src/ui/UserInput.tsx +++ b/extensions/cli/src/ui/UserInput.tsx @@ -62,6 +62,13 @@ const UserInput: React.FC = ({ const [inputText, setInputText] = useState(""); const [cursorPosition, setCursorPosition] = useState(0); const [showSlashCommands, setShowSlashCommands] = useState(false); + + // Clear input function for Ctrl+C + const clearInput = useCallback(() => { + textBuffer.clear(); + setInputText(""); + setCursorPosition(0); + }, [textBuffer]); const [slashCommandFilter, setSlashCommandFilter] = useState(""); const [selectedCommandIndex, setSelectedCommandIndex] = useState(0); const [showFileSearch, setShowFileSearch] = useState(false); @@ -427,6 +434,7 @@ const UserInput: React.FC = ({ showSlashCommands, showFileSearch, cycleModes, + clearInput, }) ) { return; diff --git a/extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx b/extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx new file mode 100644 index 000000000..8088b4aa5 --- /dev/null +++ b/extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx @@ -0,0 +1,63 @@ +import { render } from "ink-testing-library"; +import React from "react"; +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; + +import { BottomStatusBar } from "../components/BottomStatusBar.js"; + +// Mock the index module functions +vi.mock("../../index.js", () => ({ + shouldShowExitMessage: vi.fn(() => false), + setExitMessageCallback: vi.fn(), +})); + +describe("BottomStatusBar Ctrl+C message", () => { + const defaultProps = { + currentMode: "normal" as const, + repoURLText: "github.com/user/repo", + isRemoteMode: false, + services: { model: { model: null } }, + navState: { currentScreen: "chat" }, + navigateTo: vi.fn(), + closeCurrentScreen: vi.fn(), + contextPercentage: undefined, + }; + + beforeEach(async () => { + vi.clearAllMocks(); + // Reset mocks to default state + const indexModule = await vi.importMock("../../index.js") as any; + indexModule.shouldShowExitMessage.mockReturnValue(false); + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it("shows repo URL when not in exit message mode", async () => { + const indexModule = await vi.importMock("../../index.js") as any; + indexModule.shouldShowExitMessage.mockReturnValue(false); + + const { lastFrame } = render(); + + expect(lastFrame()).toContain("github.com/user/repo"); + expect(lastFrame()).not.toContain("ctrl+c to exit"); + }); + + it("shows exit message when shouldShowExitMessage returns true", async () => { + const indexModule = await vi.importMock("../../index.js") as any; + indexModule.shouldShowExitMessage.mockReturnValue(true); + + const { lastFrame } = render(); + + expect(lastFrame()).toContain("ctrl+c to exit"); + expect(lastFrame()).not.toContain("github.com/user/repo"); + }); + + it("registers exit message callback on mount", async () => { + const indexModule = await vi.importMock("../../index.js") as any; + + render(); + + expect(indexModule.setExitMessageCallback).toHaveBeenCalledWith(expect.any(Function)); + }); +}); \ No newline at end of file diff --git a/extensions/cli/src/ui/__tests__/CtrlC.test.tsx b/extensions/cli/src/ui/__tests__/CtrlC.test.tsx new file mode 100644 index 000000000..9051d3849 --- /dev/null +++ b/extensions/cli/src/ui/__tests__/CtrlC.test.tsx @@ -0,0 +1,119 @@ +import { render } from "ink-testing-library"; +import React from "react"; +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; + +import { Selector } from "../Selector.js"; +import { MCPSelector } from "../MCPSelector.js"; +import { ConfigSelector } from "../ConfigSelector.js"; + +describe("Ctrl+C behavior", () => { + let mockOnCancel: ReturnType; + let mockOnSelect: ReturnType; + let mockProcess: any; + + beforeEach(() => { + mockOnCancel = vi.fn(); + mockOnSelect = vi.fn(); + + // Mock process.exit for testing + mockProcess = { + exit: vi.fn(), + kill: vi.fn(), + }; + vi.stubGlobal("process", mockProcess); + }); + + afterEach(() => { + vi.clearAllMocks(); + vi.unstubAllGlobals(); + }); + + describe("Selector component", () => { + it("calls onCancel when Ctrl+C is pressed", () => { + const options = [ + { id: "1", name: "Option 1" }, + { id: "2", name: "Option 2" } + ]; + + const { stdin } = render( + + ); + + // Simulate Ctrl+C + stdin.write("\u0003"); + + expect(mockOnCancel).toHaveBeenCalledTimes(1); + expect(mockOnSelect).not.toHaveBeenCalled(); + }); + + it("calls onCancel when Ctrl+C is pressed during loading", () => { + const options: any[] = []; + + const { stdin } = render( + + ); + + // Simulate Ctrl+C during loading + stdin.write("\u0003"); + + expect(mockOnCancel).toHaveBeenCalledTimes(1); + }); + + it("calls onCancel when Ctrl+C is pressed during error state", () => { + const options: any[] = []; + + const { stdin } = render( + + ); + + // Simulate Ctrl+C during error + stdin.write("\u0003"); + + expect(mockOnCancel).toHaveBeenCalledTimes(1); + }); + }); + + describe("ConfigSelector component", () => { + it("calls onCancel when Ctrl+C is pressed", () => { + const { stdin } = render( + + ); + + // Simulate Ctrl+C + stdin.write("\u0003"); + + expect(mockOnCancel).toHaveBeenCalledTimes(1); + }); + }); +}); \ No newline at end of file diff --git a/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx b/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx new file mode 100644 index 000000000..d79c49306 --- /dev/null +++ b/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx @@ -0,0 +1,63 @@ +import { render } from "ink-testing-library"; +import React from "react"; +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; + +import { UserInput } from "../UserInput.js"; + +describe("Ctrl+C input clearing", () => { + let mockOnSubmit: ReturnType; + let mockProcess: any; + + beforeEach(() => { + mockOnSubmit = vi.fn(); + + // Mock process.kill for SIGINT simulation + mockProcess = { + kill: vi.fn(), + pid: 12345, + }; + vi.stubGlobal("process", mockProcess); + }); + + afterEach(() => { + vi.clearAllMocks(); + vi.unstubAllGlobals(); + }); + + it("sends SIGINT when Ctrl+C is pressed (input clearing tested implicitly)", () => { + const { stdin } = render( + + ); + + // Type some text first + stdin.write("hello world"); + + // Simulate Ctrl+C + stdin.write("\u0003"); + + // Should send SIGINT to process (which handles the two-stage exit) + expect(mockProcess.kill).toHaveBeenCalledWith(12345, "SIGINT"); + }); + + it("sends SIGINT to main process for two-stage exit handling", () => { + const { stdin } = render( + + ); + + // Simulate Ctrl+C + stdin.write("\u0003"); + + // Should delegate to main process SIGINT handler + expect(mockProcess.kill).toHaveBeenCalledWith(12345, "SIGINT"); + }); +}); \ No newline at end of file diff --git a/extensions/cli/src/ui/components/BottomStatusBar.tsx b/extensions/cli/src/ui/components/BottomStatusBar.tsx index c46bd2059..da4094b08 100644 --- a/extensions/cli/src/ui/components/BottomStatusBar.tsx +++ b/extensions/cli/src/ui/components/BottomStatusBar.tsx @@ -1,6 +1,7 @@ import { Box, Text } from "ink"; -import React from "react"; +import React, { useEffect, useState } from "react"; +import { setExitMessageCallback, shouldShowExitMessage } from "../../index.js"; import type { PermissionMode } from "../../permissions/types.js"; import type { NavigationScreen } from "../context/NavigationContext.js"; import { FreeTrialStatus } from "../FreeTrialStatus.js"; @@ -29,42 +30,57 @@ export const BottomStatusBar: React.FC = ({ navigateTo, closeCurrentScreen, contextPercentage, -}) => ( - - - {currentMode === "normal" && ( - - - {repoURLText} - - - - )} - - {contextPercentage !== undefined && contextPercentage > 75 && ( - - • - - - )} +}) => { + const [refreshTrigger, setRefreshTrigger] = useState(0); + + useEffect(() => { + setExitMessageCallback(() => { + setRefreshTrigger((prev) => prev + 1); + }); + }, []); + + const showingExitMessage = shouldShowExitMessage(); + + return ( + + + {currentMode === "normal" && ( + + + {showingExitMessage ? "ctrl+c to exit" : repoURLText} + + + + )} + + {contextPercentage !== undefined && contextPercentage > 75 && ( + + • + + + )} + + + {!isRemoteMode && services.model?.model && ( + { + if (shouldShow && navState.currentScreen === "chat") { + navigateTo("free-trial"); + } else if ( + !shouldShow && + navState.currentScreen === "free-trial" + ) { + closeCurrentScreen(); + } + }} + /> + )} + + + + - - {!isRemoteMode && services.model?.model && ( - { - if (shouldShow && navState.currentScreen === "chat") { - navigateTo("free-trial"); - } else if (!shouldShow && navState.currentScreen === "free-trial") { - closeCurrentScreen(); - } - }} - /> - )} - - - - - -); + ); +}; diff --git a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx index c4b93ab35..15154b7b4 100644 --- a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx +++ b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx @@ -79,8 +79,8 @@ export const ToolPermissionSelector: React.FC = ({ return; } - // Escape to reject with stop stream - if (key.escape) { + // Escape or Ctrl+C to reject with stop stream + if (key.escape || (key.ctrl && input === "c")) { onResponse(requestId, false, false, true); return; } diff --git a/extensions/cli/src/ui/hooks/useUserInput.ts b/extensions/cli/src/ui/hooks/useUserInput.ts index ae0dea719..3aa349f85 100644 --- a/extensions/cli/src/ui/hooks/useUserInput.ts +++ b/extensions/cli/src/ui/hooks/useUserInput.ts @@ -9,14 +9,25 @@ interface ControlKeysOptions { showSlashCommands: boolean; showFileSearch: boolean; cycleModes: () => Promise; + clearInput?: () => void; } export function handleControlKeys(options: ControlKeysOptions): boolean { - const { input, key, exit, showSlashCommands, showFileSearch, cycleModes } = + const { input, key, exit, showSlashCommands, showFileSearch, cycleModes, clearInput } = options; - // Handle Ctrl+C and Ctrl+D - if (key.ctrl && (input === "c" || input === "d")) { + // Handle Ctrl+C with two-stage exit, Ctrl+D immediately exits + if (key.ctrl && input === "c") { + // Clear input box if clearInput function is provided + if (clearInput) { + clearInput(); + } + // Let the main process SIGINT handler handle Ctrl+C logic + process.kill(process.pid, "SIGINT"); + return true; + } + + if (key.ctrl && input === "d") { exit(); return true; } diff --git a/extensions/cli/src/ui/index.ts b/extensions/cli/src/ui/index.ts index e75f9aa91..55dc236eb 100644 --- a/extensions/cli/src/ui/index.ts +++ b/extensions/cli/src/ui/index.ts @@ -1,9 +1,10 @@ -import { render } from "ink"; +import { render, RenderOptions } from "ink"; import React from "react"; import { PermissionMode } from "../permissions/types.js"; import { initializeServices } from "../services/index.js"; import { ServiceContainerProvider } from "../services/ServiceContainerContext.js"; +import { setTUIUnmount } from "../index.js"; import { AppRoot } from "./AppRoot.js"; @@ -52,7 +53,9 @@ export async function startTUIChat( // Use static imports since we're always loading TUI when there's piped input // Start the TUI immediately - it will handle loading states - const renderOptions: any = {}; + const renderOptions: RenderOptions = { + exitOnCtrlC: false, // Disable Ink's default Ctrl+C handling so we can implement two-stage exit + }; if (customStdin) { renderOptions.stdin = customStdin; } @@ -69,11 +72,8 @@ export async function startTUIChat( renderOptions, ); - // Handle cleanup - process.on("SIGINT", () => { - unmount(); - process.exit(0); - }); + // Register unmount function with main process for two-stage Ctrl+C exit + setTUIUnmount(unmount); return { unmount }; } @@ -92,11 +92,8 @@ export async function startRemoteTUIChat( }), ); - // Handle cleanup - process.on("SIGINT", () => { - unmount(); - process.exit(0); - }); + // Register unmount function with main process for two-stage Ctrl+C exit + setTUIUnmount(unmount); return { unmount }; } From fa9af3778a1574439376df2544a73d892c89fa0e Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Sun, 24 Aug 2025 02:36:26 -0700 Subject: [PATCH 3/8] fix: slash command search uses complete text regardless of cursor position --- extensions/cli/src/ui/UserInput.tsx | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/extensions/cli/src/ui/UserInput.tsx b/extensions/cli/src/ui/UserInput.tsx index 466d8b421..0352cf83a 100644 --- a/extensions/cli/src/ui/UserInput.tsx +++ b/extensions/cli/src/ui/UserInput.tsx @@ -135,13 +135,8 @@ const UserInput: React.FC = ({ return; } - const afterSlash = beforeCursor.slice(1); - - // Check if we have whitespace in the command - if (afterSlash.includes(" ") || afterSlash.includes("\n")) { - setShowSlashCommands(false); - return; - } + // Get the complete slash command, not just the part before cursor + const afterSlash = trimmedText.slice(1).split(/[\s\n]/)[0]; // We're in a slash command context - check if it's a complete command const allCommands = getSlashCommands(); From 6ed85c19b085d51aad5ec121228283f4cfaf1434 Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Sun, 24 Aug 2025 03:12:54 -0700 Subject: [PATCH 4/8] fix: ConfigSelector showing incorrect green checkmark for first-time users --- extensions/cli/src/ui/ConfigSelector.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extensions/cli/src/ui/ConfigSelector.tsx b/extensions/cli/src/ui/ConfigSelector.tsx index 652671c53..23ec70724 100644 --- a/extensions/cli/src/ui/ConfigSelector.tsx +++ b/extensions/cli/src/ui/ConfigSelector.tsx @@ -12,6 +12,7 @@ import { } from "../auth/workos.js"; import { getApiClient } from "../config.js"; import { env } from "../env.js"; +import { services } from "../services/index.js"; import { Selector, SelectorOption } from "./Selector.js"; @@ -115,8 +116,10 @@ const ConfigSelector: React.FC = ({ displaySuffix: " (opens web)", }); - // Determine current config by checking auth config + // Determine current config by checking both auth config and service state const assistantSlug = getAssistantSlug(authConfig); + const currentConfigState = services.config.getState(); + if (assistantSlug) { // Find the matching config by slug and organization const matchingConfig = options.find( @@ -126,8 +129,9 @@ const ConfigSelector: React.FC = ({ opt.organizationId === currentOrganizationId, ); currentId = matchingConfig?.id || null; - } else if (fs.existsSync(CONFIG_PATH)) { - // No assistant slug means local config is current + } else if (currentConfigState.configPath === CONFIG_PATH && fs.existsSync(CONFIG_PATH)) { + // Only mark local config as current if it's actually the active config path in the service + // This ensures we don't show a green checkmark for first-time users just because the file exists currentId = "local"; } From bf11e16b8495051b5fd4a5151927ac2d854acc25 Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Sun, 24 Aug 2025 13:42:59 -0700 Subject: [PATCH 5/8] fix lint --- extensions/cli/spec/ctrl-c-behavior.md | 4 ++++ .../src/__tests__/CtrlCProcessHandling.test.ts | 2 +- extensions/cli/src/ui/ConfigSelector.tsx | 7 +++++-- extensions/cli/src/ui/FreeTrialTransitionUI.tsx | 2 +- extensions/cli/src/ui/Selector.tsx | 2 +- .../ui/__tests__/BottomStatusBarCtrlC.test.tsx | 16 +++++++++------- extensions/cli/src/ui/__tests__/CtrlC.test.tsx | 17 +++++++---------- .../ui/__tests__/CtrlCInputClearing.test.tsx | 12 ++++++------ extensions/cli/src/ui/hooks/useUserInput.ts | 11 +++++++++-- 9 files changed, 43 insertions(+), 30 deletions(-) diff --git a/extensions/cli/spec/ctrl-c-behavior.md b/extensions/cli/spec/ctrl-c-behavior.md index 72fd49b17..9d11f26ab 100644 --- a/extensions/cli/spec/ctrl-c-behavior.md +++ b/extensions/cli/spec/ctrl-c-behavior.md @@ -3,22 +3,26 @@ ## Overview Ctrl+C operates on two levels: + 1. **Menus/Selectors**: Immediate cancellation and return to previous state 2. **Main Application**: Two-stage exit requiring double Ctrl+C within 1 second ## Two-Stage Exit (Main Application) ### First Ctrl+C + - Shows temporary status message "ctrl+c to exit" in bottom-left status bar - Message automatically disappears after 1 second - Clears any text in the input field (if focused) - Does **not** exit the application ### Second Ctrl+C (within 1 second) + - Exits the application immediately - Performs graceful shutdown ### Second Ctrl+C (after 1 second) + - Treated as a new "first Ctrl+C" - Shows exit message again - User must press Ctrl+C twice within 1 second window to actually exit diff --git a/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts b/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts index 31782b653..a50f425c9 100644 --- a/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts +++ b/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts @@ -9,4 +9,4 @@ describe("Process SIGINT handling functions", () => { expect(typeof indexModule.setExitMessageCallback).toBe("function"); expect(typeof indexModule.setTUIUnmount).toBe("function"); }); -}); \ No newline at end of file +}); diff --git a/extensions/cli/src/ui/ConfigSelector.tsx b/extensions/cli/src/ui/ConfigSelector.tsx index 23ec70724..01b1ca928 100644 --- a/extensions/cli/src/ui/ConfigSelector.tsx +++ b/extensions/cli/src/ui/ConfigSelector.tsx @@ -119,7 +119,7 @@ const ConfigSelector: React.FC = ({ // Determine current config by checking both auth config and service state const assistantSlug = getAssistantSlug(authConfig); const currentConfigState = services.config.getState(); - + if (assistantSlug) { // Find the matching config by slug and organization const matchingConfig = options.find( @@ -129,7 +129,10 @@ const ConfigSelector: React.FC = ({ opt.organizationId === currentOrganizationId, ); currentId = matchingConfig?.id || null; - } else if (currentConfigState.configPath === CONFIG_PATH && fs.existsSync(CONFIG_PATH)) { + } else if ( + currentConfigState.configPath === CONFIG_PATH && + fs.existsSync(CONFIG_PATH) + ) { // Only mark local config as current if it's actually the active config path in the service // This ensures we don't show a green checkmark for first-time users just because the file exists currentId = "local"; diff --git a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx index 19fc8ca5d..0a8fceb24 100644 --- a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx +++ b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx @@ -135,7 +135,7 @@ const FreeTrialTransitionUI: React.FC = ({ closeCurrentScreen(); return; } - + // Handle Ctrl+C with two-stage exit (delegates to main process) if (key.ctrl && input === "c") { process.kill(process.pid, "SIGINT"); diff --git a/extensions/cli/src/ui/Selector.tsx b/extensions/cli/src/ui/Selector.tsx index b8ba5f4c4..3e01a37e2 100644 --- a/extensions/cli/src/ui/Selector.tsx +++ b/extensions/cli/src/ui/Selector.tsx @@ -52,7 +52,7 @@ export function Selector({ useInput((input, key) => { if (loading || error) return; // Skip normal input handling during loading/error - + if (key.escape || (key.ctrl && input === "c")) { onCancel(); return; diff --git a/extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx b/extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx index 8088b4aa5..897228a96 100644 --- a/extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx +++ b/extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx @@ -25,7 +25,7 @@ describe("BottomStatusBar Ctrl+C message", () => { beforeEach(async () => { vi.clearAllMocks(); // Reset mocks to default state - const indexModule = await vi.importMock("../../index.js") as any; + const indexModule = (await vi.importMock("../../index.js")) as any; indexModule.shouldShowExitMessage.mockReturnValue(false); }); @@ -34,7 +34,7 @@ describe("BottomStatusBar Ctrl+C message", () => { }); it("shows repo URL when not in exit message mode", async () => { - const indexModule = await vi.importMock("../../index.js") as any; + const indexModule = (await vi.importMock("../../index.js")) as any; indexModule.shouldShowExitMessage.mockReturnValue(false); const { lastFrame } = render(); @@ -44,7 +44,7 @@ describe("BottomStatusBar Ctrl+C message", () => { }); it("shows exit message when shouldShowExitMessage returns true", async () => { - const indexModule = await vi.importMock("../../index.js") as any; + const indexModule = (await vi.importMock("../../index.js")) as any; indexModule.shouldShowExitMessage.mockReturnValue(true); const { lastFrame } = render(); @@ -54,10 +54,12 @@ describe("BottomStatusBar Ctrl+C message", () => { }); it("registers exit message callback on mount", async () => { - const indexModule = await vi.importMock("../../index.js") as any; - + const indexModule = (await vi.importMock("../../index.js")) as any; + render(); - expect(indexModule.setExitMessageCallback).toHaveBeenCalledWith(expect.any(Function)); + expect(indexModule.setExitMessageCallback).toHaveBeenCalledWith( + expect.any(Function), + ); }); -}); \ No newline at end of file +}); diff --git a/extensions/cli/src/ui/__tests__/CtrlC.test.tsx b/extensions/cli/src/ui/__tests__/CtrlC.test.tsx index 9051d3849..47c72f9ce 100644 --- a/extensions/cli/src/ui/__tests__/CtrlC.test.tsx +++ b/extensions/cli/src/ui/__tests__/CtrlC.test.tsx @@ -14,7 +14,7 @@ describe("Ctrl+C behavior", () => { beforeEach(() => { mockOnCancel = vi.fn(); mockOnSelect = vi.fn(); - + // Mock process.exit for testing mockProcess = { exit: vi.fn(), @@ -32,7 +32,7 @@ describe("Ctrl+C behavior", () => { it("calls onCancel when Ctrl+C is pressed", () => { const options = [ { id: "1", name: "Option 1" }, - { id: "2", name: "Option 2" } + { id: "2", name: "Option 2" }, ]; const { stdin } = render( @@ -45,7 +45,7 @@ describe("Ctrl+C behavior", () => { onSelect={mockOnSelect} onCancel={mockOnCancel} onNavigate={vi.fn()} - /> + />, ); // Simulate Ctrl+C @@ -69,7 +69,7 @@ describe("Ctrl+C behavior", () => { onSelect={mockOnSelect} onCancel={mockOnCancel} onNavigate={vi.fn()} - /> + />, ); // Simulate Ctrl+C during loading @@ -91,7 +91,7 @@ describe("Ctrl+C behavior", () => { onSelect={mockOnSelect} onCancel={mockOnCancel} onNavigate={vi.fn()} - /> + />, ); // Simulate Ctrl+C during error @@ -104,10 +104,7 @@ describe("Ctrl+C behavior", () => { describe("ConfigSelector component", () => { it("calls onCancel when Ctrl+C is pressed", () => { const { stdin } = render( - + , ); // Simulate Ctrl+C @@ -116,4 +113,4 @@ describe("Ctrl+C behavior", () => { expect(mockOnCancel).toHaveBeenCalledTimes(1); }); }); -}); \ No newline at end of file +}); diff --git a/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx b/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx index d79c49306..bf48e2506 100644 --- a/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx +++ b/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx @@ -10,7 +10,7 @@ describe("Ctrl+C input clearing", () => { beforeEach(() => { mockOnSubmit = vi.fn(); - + // Mock process.kill for SIGINT simulation mockProcess = { kill: vi.fn(), @@ -28,10 +28,10 @@ describe("Ctrl+C input clearing", () => { const { stdin } = render( + />, ); // Type some text first @@ -48,10 +48,10 @@ describe("Ctrl+C input clearing", () => { const { stdin } = render( + />, ); // Simulate Ctrl+C @@ -60,4 +60,4 @@ describe("Ctrl+C input clearing", () => { // Should delegate to main process SIGINT handler expect(mockProcess.kill).toHaveBeenCalledWith(12345, "SIGINT"); }); -}); \ No newline at end of file +}); diff --git a/extensions/cli/src/ui/hooks/useUserInput.ts b/extensions/cli/src/ui/hooks/useUserInput.ts index 3aa349f85..8ecdd50ca 100644 --- a/extensions/cli/src/ui/hooks/useUserInput.ts +++ b/extensions/cli/src/ui/hooks/useUserInput.ts @@ -13,8 +13,15 @@ interface ControlKeysOptions { } export function handleControlKeys(options: ControlKeysOptions): boolean { - const { input, key, exit, showSlashCommands, showFileSearch, cycleModes, clearInput } = - options; + const { + input, + key, + exit, + showSlashCommands, + showFileSearch, + cycleModes, + clearInput, + } = options; // Handle Ctrl+C with two-stage exit, Ctrl+D immediately exits if (key.ctrl && input === "c") { From a7a212f78f861b9c1b44ab8c6e87991c7daba9da Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Sun, 24 Aug 2025 14:24:07 -0700 Subject: [PATCH 6/8] fix linting --- extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts | 2 +- extensions/cli/src/ui/FreeTrialTransitionUI.tsx | 1 + extensions/cli/src/ui/MCPSelector.tsx | 1 + extensions/cli/src/ui/ServiceDebugger.tsx | 3 ++- extensions/cli/src/ui/SessionSelector.tsx | 1 + extensions/cli/src/ui/__tests__/CtrlC.test.tsx | 3 +-- extensions/cli/src/ui/components/BottomStatusBar.tsx | 2 +- extensions/cli/src/ui/components/ToolPermissionSelector.tsx | 1 + extensions/cli/src/ui/index.ts | 2 +- 9 files changed, 10 insertions(+), 6 deletions(-) diff --git a/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts b/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts index a50f425c9..563efbbe9 100644 --- a/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts +++ b/extensions/cli/src/__tests__/CtrlCProcessHandling.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from "vitest"; +import { describe, expect, it } from "vitest"; describe("Process SIGINT handling functions", () => { it("exports the necessary functions for exit message handling", async () => { diff --git a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx index 0a8fceb24..0943d8015 100644 --- a/extensions/cli/src/ui/FreeTrialTransitionUI.tsx +++ b/extensions/cli/src/ui/FreeTrialTransitionUI.tsx @@ -13,6 +13,7 @@ import { isValidAnthropicApiKey, } from "../util/apiKeyValidation.js"; import { updateAnthropicModelInYaml } from "../util/yamlConfigUpdater.js"; + import { useNavigation } from "./context/NavigationContext.js"; import { defaultBoxStyles } from "./styles.js"; diff --git a/extensions/cli/src/ui/MCPSelector.tsx b/extensions/cli/src/ui/MCPSelector.tsx index a06372fec..f9db017ef 100644 --- a/extensions/cli/src/ui/MCPSelector.tsx +++ b/extensions/cli/src/ui/MCPSelector.tsx @@ -10,6 +10,7 @@ import { SERVICE_NAMES, } from "../services/types.js"; import { logger } from "../util/logger.js"; + import { defaultBoxStyles } from "./styles.js"; // Utility function to get status icon and color based on server connection diff --git a/extensions/cli/src/ui/ServiceDebugger.tsx b/extensions/cli/src/ui/ServiceDebugger.tsx index 4b03a525a..e10786dc5 100644 --- a/extensions/cli/src/ui/ServiceDebugger.tsx +++ b/extensions/cli/src/ui/ServiceDebugger.tsx @@ -1,7 +1,6 @@ import { Box, Text } from "ink"; import React from "react"; -import { defaultBoxStyles } from "./styles.js"; import { ApiClientServiceState, AuthServiceState, @@ -10,6 +9,8 @@ import { ModelServiceState, } from "../services/types.js"; +import { defaultBoxStyles } from "./styles.js"; + interface ServiceDebuggerProps { services: { auth?: AuthServiceState; diff --git a/extensions/cli/src/ui/SessionSelector.tsx b/extensions/cli/src/ui/SessionSelector.tsx index fa32a4680..d1a34a2d2 100644 --- a/extensions/cli/src/ui/SessionSelector.tsx +++ b/extensions/cli/src/ui/SessionSelector.tsx @@ -3,6 +3,7 @@ import { Box, Text, useInput } from "ink"; import React, { useMemo, useState } from "react"; import { SessionMetadata } from "../session.js"; + import { useTerminalSize } from "./hooks/useTerminalSize.js"; import { defaultBoxStyles } from "./styles.js"; diff --git a/extensions/cli/src/ui/__tests__/CtrlC.test.tsx b/extensions/cli/src/ui/__tests__/CtrlC.test.tsx index 47c72f9ce..f05154977 100644 --- a/extensions/cli/src/ui/__tests__/CtrlC.test.tsx +++ b/extensions/cli/src/ui/__tests__/CtrlC.test.tsx @@ -2,9 +2,8 @@ import { render } from "ink-testing-library"; import React from "react"; import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; -import { Selector } from "../Selector.js"; -import { MCPSelector } from "../MCPSelector.js"; import { ConfigSelector } from "../ConfigSelector.js"; +import { Selector } from "../Selector.js"; describe("Ctrl+C behavior", () => { let mockOnCancel: ReturnType; diff --git a/extensions/cli/src/ui/components/BottomStatusBar.tsx b/extensions/cli/src/ui/components/BottomStatusBar.tsx index da4094b08..94bb81fef 100644 --- a/extensions/cli/src/ui/components/BottomStatusBar.tsx +++ b/extensions/cli/src/ui/components/BottomStatusBar.tsx @@ -31,7 +31,7 @@ export const BottomStatusBar: React.FC = ({ closeCurrentScreen, contextPercentage, }) => { - const [refreshTrigger, setRefreshTrigger] = useState(0); + const [_refreshTrigger, setRefreshTrigger] = useState(0); useEffect(() => { setExitMessageCallback(() => { diff --git a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx index 15154b7b4..77790af00 100644 --- a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx +++ b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx @@ -4,6 +4,7 @@ import React, { useState } from "react"; import { formatToolCall } from "../../tools/index.js"; import { ToolCallPreview } from "../../tools/types.js"; import { defaultBoxStyles } from "../styles.js"; + import { ToolPreview } from "./ToolPreview.js"; interface PermissionOption { diff --git a/extensions/cli/src/ui/index.ts b/extensions/cli/src/ui/index.ts index 55dc236eb..848a74fe7 100644 --- a/extensions/cli/src/ui/index.ts +++ b/extensions/cli/src/ui/index.ts @@ -1,10 +1,10 @@ import { render, RenderOptions } from "ink"; import React from "react"; +import { setTUIUnmount } from "../index.js"; import { PermissionMode } from "../permissions/types.js"; import { initializeServices } from "../services/index.js"; import { ServiceContainerProvider } from "../services/ServiceContainerContext.js"; -import { setTUIUnmount } from "../index.js"; import { AppRoot } from "./AppRoot.js"; From 4356d12e6409bf19ffaa87bec30151de4157e422 Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Tue, 26 Aug 2025 10:57:03 -0700 Subject: [PATCH 7/8] fix --- extensions/cli/src/ui/UserInput.tsx | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/extensions/cli/src/ui/UserInput.tsx b/extensions/cli/src/ui/UserInput.tsx index 8a9da83c5..0352cf83a 100644 --- a/extensions/cli/src/ui/UserInput.tsx +++ b/extensions/cli/src/ui/UserInput.tsx @@ -414,16 +414,6 @@ const UserInput: React.FC = ({ return false; }; - // Clear input function - const clearInput = () => { - textBuffer.clear(); - setInputText(""); - setCursorPosition(0); - setShowSlashCommands(false); - setShowFileSearch(false); - inputHistory.resetNavigation(); - }; - useInput((input, key) => { // Don't handle any input when disabled if (disabled) { From 862c3fc665b82aa04b574f5a8c58d684dfa0f35e Mon Sep 17 00:00:00 2001 From: Ting-Wai To <40179554+tingwai@users.noreply.github.com> Date: Wed, 3 Sep 2025 11:01:40 -0700 Subject: [PATCH 8/8] fix: lint --- extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx b/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx index ceb882590..f40dd1414 100644 --- a/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx +++ b/extensions/cli/src/ui/__tests__/CtrlCInputClearing.test.tsx @@ -6,13 +6,13 @@ import { UserInput } from "../UserInput.js"; describe("Ctrl+C input clearing", () => { let mockOnSubmit: ReturnType; - let mockProcessKill: ReturnType; + let mockProcessKill: any; beforeEach(() => { mockOnSubmit = vi.fn(); // Mock process.kill using vi.spyOn - mockProcessKill = vi.spyOn(process, 'kill').mockImplementation(() => { + mockProcessKill = vi.spyOn(process, "kill").mockImplementation(() => { return true; }); });