diff options
Diffstat (limited to 'packages/cli/src/ui/hooks')
| -rw-r--r-- | packages/cli/src/ui/hooks/slashCommandProcessor.test.ts | 37 | ||||
| -rw-r--r-- | packages/cli/src/ui/hooks/slashCommandProcessor.ts | 31 |
2 files changed, 68 insertions, 0 deletions
diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts index 221893a2..3fcdff97 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts @@ -61,10 +61,15 @@ import { MCPServerStatus, getMCPServerStatus, } from '@gemini-cli/core'; +import { useSession } from '../contexts/SessionContext.js'; import * as ShowMemoryCommandModule from './useShowMemoryCommand.js'; import { GIT_COMMIT_INFO } from '../../generated/git-commit.js'; +vi.mock('../contexts/SessionContext.js', () => ({ + useSession: vi.fn(), +})); + vi.mock('./useShowMemoryCommand.js', () => ({ SHOW_MEMORY_COMMAND_NAME: '/memory show', createShowMemoryAction: vi.fn(() => vi.fn()), @@ -84,6 +89,7 @@ describe('useSlashCommandProcessor', () => { let mockPerformMemoryRefresh: ReturnType<typeof vi.fn>; let mockConfig: Config; let mockCorgiMode: ReturnType<typeof vi.fn>; + const mockUseSession = useSession as Mock; beforeEach(() => { mockAddItem = vi.fn(); @@ -99,6 +105,9 @@ describe('useSlashCommandProcessor', () => { getModel: vi.fn(() => 'test-model'), } as unknown as Config; mockCorgiMode = vi.fn(); + mockUseSession.mockReturnValue({ + startTime: new Date('2025-01-01T00:00:00.000Z'), + }); (open as Mock).mockClear(); mockProcessExit.mockClear(); @@ -230,6 +239,34 @@ describe('useSlashCommandProcessor', () => { }); }); + describe('/stats command', () => { + it('should show the session duration', async () => { + const { handleSlashCommand } = getProcessor(); + let commandResult: SlashCommandActionReturn | boolean = false; + + // Mock current time + const mockDate = new Date('2025-01-01T00:01:05.000Z'); + vi.setSystemTime(mockDate); + + await act(async () => { + commandResult = handleSlashCommand('/stats'); + }); + + expect(mockAddItem).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ + type: MessageType.INFO, + text: 'Session duration: 1m 5s', + }), + expect.any(Number), + ); + expect(commandResult).toBe(true); + + // Restore system time + vi.useRealTimers(); + }); + }); + describe('Other commands', () => { it('/help should open help and return true', async () => { const { handleSlashCommand } = getProcessor(); diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.ts index 38fdddba..85ae825e 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.ts @@ -11,6 +11,7 @@ import process from 'node:process'; import { UseHistoryManagerReturn } from './useHistoryManager.js'; import { Config, MCPServerStatus, getMCPServerStatus } from '@gemini-cli/core'; import { Message, MessageType, HistoryItemWithoutId } from '../types.js'; +import { useSession } from '../contexts/SessionContext.js'; import { createShowMemoryAction } from './useShowMemoryCommand.js'; import { GIT_COMMIT_INFO } from '../../generated/git-commit.js'; import { formatMemoryUsage } from '../utils/formatters.js'; @@ -49,6 +50,8 @@ export const useSlashCommandProcessor = ( toggleCorgiMode: () => void, showToolDescriptions: boolean = false, ) => { + const session = useSession(); + const addMessage = useCallback( (message: Message) => { // Convert Message to HistoryItemWithoutId @@ -139,6 +142,33 @@ export const useSlashCommandProcessor = ( }, }, { + name: 'stats', + altName: 'usage', + description: 'check session stats', + action: (_mainCommand, _subCommand, _args) => { + const now = new Date(); + const duration = now.getTime() - session.startTime.getTime(); + const durationInSeconds = Math.floor(duration / 1000); + const hours = Math.floor(durationInSeconds / 3600); + const minutes = Math.floor((durationInSeconds % 3600) / 60); + const seconds = durationInSeconds % 60; + + const durationString = [ + hours > 0 ? `${hours}h` : '', + minutes > 0 ? `${minutes}m` : '', + `${seconds}s`, + ] + .filter(Boolean) + .join(' '); + + addMessage({ + type: MessageType.INFO, + content: `Session duration: ${durationString}`, + timestamp: new Date(), + }); + }, + }, + { name: 'mcp', description: 'list configured MCP servers and tools', action: async (_mainCommand, _subCommand, _args) => { @@ -447,6 +477,7 @@ Add any other context about the problem here. toggleCorgiMode, config, showToolDescriptions, + session.startTime, ], ); |
