Show session summary on exit for ctrl+c x 2. Fix exit UI (#963)

This commit is contained in:
Abhi 2025-06-11 20:08:32 -04:00 committed by GitHub
parent e02a035ab4
commit dd53e5c96a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 70 additions and 19 deletions

View File

@ -89,6 +89,9 @@ const App = ({ config, settings, startupWarnings = [] }: AppProps) => {
const [showToolDescriptions, setShowToolDescriptions] = const [showToolDescriptions, setShowToolDescriptions] =
useState<boolean>(false); useState<boolean>(false);
const [ctrlCPressedOnce, setCtrlCPressedOnce] = useState(false); const [ctrlCPressedOnce, setCtrlCPressedOnce] = useState(false);
const [quittingMessages, setQuittingMessages] = useState<
HistoryItem[] | null
>(null);
const ctrlCTimerRef = useRef<NodeJS.Timeout | null>(null); const ctrlCTimerRef = useRef<NodeJS.Timeout | null>(null);
const errorCount = useMemo( const errorCount = useMemo(
@ -162,6 +165,7 @@ const App = ({ config, settings, startupWarnings = [] }: AppProps) => {
performMemoryRefresh, performMemoryRefresh,
toggleCorgiMode, toggleCorgiMode,
showToolDescriptions, showToolDescriptions,
setQuittingMessages,
); );
useInput((input: string, key: InkKeyType) => { useInput((input: string, key: InkKeyType) => {
@ -185,7 +189,14 @@ const App = ({ config, settings, startupWarnings = [] }: AppProps) => {
if (ctrlCTimerRef.current) { if (ctrlCTimerRef.current) {
clearTimeout(ctrlCTimerRef.current); clearTimeout(ctrlCTimerRef.current);
} }
process.exit(0); const quitCommand = slashCommands.find(
(cmd) => cmd.name === 'quit' || cmd.altName === 'exit',
);
if (quitCommand) {
quitCommand.action('quit', '', '');
} else {
process.exit(0);
}
} else { } else {
setCtrlCPressedOnce(true); setCtrlCPressedOnce(true);
ctrlCTimerRef.current = setTimeout(() => { ctrlCTimerRef.current = setTimeout(() => {
@ -338,6 +349,22 @@ const App = ({ config, settings, startupWarnings = [] }: AppProps) => {
const branchName = useGitBranchName(config.getTargetDir()); const branchName = useGitBranchName(config.getTargetDir());
if (quittingMessages) {
return (
<Box flexDirection="column" marginBottom={1}>
{quittingMessages.map((item) => (
<HistoryItemDisplay
key={item.id}
availableTerminalHeight={availableTerminalHeight}
item={item}
isPending={false}
config={config}
/>
))}
</Box>
);
}
return ( return (
<StreamingContext.Provider value={streamingState}> <StreamingContext.Provider value={streamingState}>
<Box flexDirection="column" marginBottom={1} width="90%"> <Box flexDirection="column" marginBottom={1} width="90%">

View File

@ -98,6 +98,7 @@ describe('useSlashCommandProcessor', () => {
let mockOnDebugMessage: ReturnType<typeof vi.fn>; let mockOnDebugMessage: ReturnType<typeof vi.fn>;
let mockOpenThemeDialog: ReturnType<typeof vi.fn>; let mockOpenThemeDialog: ReturnType<typeof vi.fn>;
let mockPerformMemoryRefresh: ReturnType<typeof vi.fn>; let mockPerformMemoryRefresh: ReturnType<typeof vi.fn>;
let mockSetQuittingMessages: ReturnType<typeof vi.fn>;
let mockConfig: Config; let mockConfig: Config;
let mockCorgiMode: ReturnType<typeof vi.fn>; let mockCorgiMode: ReturnType<typeof vi.fn>;
const mockUseSessionStats = useSessionStats as Mock; const mockUseSessionStats = useSessionStats as Mock;
@ -111,6 +112,7 @@ describe('useSlashCommandProcessor', () => {
mockOnDebugMessage = vi.fn(); mockOnDebugMessage = vi.fn();
mockOpenThemeDialog = vi.fn(); mockOpenThemeDialog = vi.fn();
mockPerformMemoryRefresh = vi.fn().mockResolvedValue(undefined); mockPerformMemoryRefresh = vi.fn().mockResolvedValue(undefined);
mockSetQuittingMessages = vi.fn();
mockConfig = { mockConfig = {
getDebugMode: vi.fn(() => false), getDebugMode: vi.fn(() => false),
getSandbox: vi.fn(() => 'test-sandbox'), getSandbox: vi.fn(() => 'test-sandbox'),
@ -156,6 +158,7 @@ describe('useSlashCommandProcessor', () => {
mockPerformMemoryRefresh, mockPerformMemoryRefresh,
mockCorgiMode, mockCorgiMode,
showToolDescriptions, showToolDescriptions,
mockSetQuittingMessages,
), ),
); );
return result.current; return result.current;
@ -406,7 +409,7 @@ Add any other context about the problem here.
}); });
it.each([['/quit'], ['/exit']])( it.each([['/quit'], ['/exit']])(
'should handle %s, add a quit message, and exit the process', 'should handle %s, set quitting messages, and exit the process',
async (command) => { async (command) => {
const { handleSlashCommand } = getProcessor(); const { handleSlashCommand } = getProcessor();
const mockDate = new Date('2025-01-01T01:02:03.000Z'); const mockDate = new Date('2025-01-01T01:02:03.000Z');
@ -416,18 +419,25 @@ Add any other context about the problem here.
handleSlashCommand(command); handleSlashCommand(command);
}); });
expect(mockAddItem).toHaveBeenCalledTimes(2); expect(mockAddItem).not.toHaveBeenCalled();
expect(mockAddItem).toHaveBeenNthCalledWith( expect(mockSetQuittingMessages).toHaveBeenCalledWith([
2, {
expect.objectContaining({ type: 'user',
type: MessageType.QUIT, text: command,
id: expect.any(Number),
},
{
type: 'quit',
stats: expect.any(Object),
duration: '1h 2m 3s', duration: '1h 2m 3s',
}), id: expect.any(Number),
expect.any(Number), },
); ]);
// Fast-forward timers to trigger process.exit // Fast-forward timers to trigger process.exit
vi.advanceTimersByTime(100); await act(async () => {
vi.advanceTimersByTime(100);
});
expect(mockProcessExit).toHaveBeenCalledWith(0); expect(mockProcessExit).toHaveBeenCalledWith(0);
}, },
); );

View File

@ -69,6 +69,7 @@ export const useSlashCommandProcessor = (
performMemoryRefresh: () => Promise<void>, performMemoryRefresh: () => Promise<void>,
toggleCorgiMode: () => void, toggleCorgiMode: () => void,
showToolDescriptions: boolean = false, showToolDescriptions: boolean = false,
setQuittingMessages: (message: HistoryItem[]) => void,
) => { ) => {
const session = useSessionStats(); const session = useSessionStats();
const gitService = useMemo(() => { const gitService = useMemo(() => {
@ -608,17 +609,24 @@ Add any other context about the problem here.
name: 'quit', name: 'quit',
altName: 'exit', altName: 'exit',
description: 'exit the cli', description: 'exit the cli',
action: async (_mainCommand, _subCommand, _args) => { action: async (mainCommand, _subCommand, _args) => {
const now = new Date(); const now = new Date();
const { sessionStartTime, cumulative } = session.stats; const { sessionStartTime, cumulative } = session.stats;
const wallDuration = now.getTime() - sessionStartTime.getTime(); const wallDuration = now.getTime() - sessionStartTime.getTime();
addMessage({ setQuittingMessages([
type: MessageType.QUIT, {
stats: cumulative, type: 'user',
duration: formatDuration(wallDuration), text: `/${mainCommand}`,
timestamp: new Date(), id: now.getTime() - 1,
}); },
{
type: 'quit',
stats: cumulative,
duration: formatDuration(wallDuration),
id: now.getTime(),
},
]);
setTimeout(() => { setTimeout(() => {
process.exit(0); process.exit(0);
@ -749,6 +757,7 @@ Add any other context about the problem here.
gitService, gitService,
loadHistory, loadHistory,
addItem, addItem,
setQuittingMessages,
]); ]);
const handleSlashCommand = useCallback( const handleSlashCommand = useCallback(
@ -763,7 +772,12 @@ Add any other context about the problem here.
return false; return false;
} }
const userMessageTimestamp = Date.now(); const userMessageTimestamp = Date.now();
addItem({ type: MessageType.USER, text: trimmed }, userMessageTimestamp); if (trimmed !== '/quit' && trimmed !== '/exit') {
addItem(
{ type: MessageType.USER, text: trimmed },
userMessageTimestamp,
);
}
let subCommand: string | undefined; let subCommand: string | undefined;
let args: string | undefined; let args: string | undefined;