diff options
| author | Jacob Richman <[email protected]> | 2025-08-13 11:13:18 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-13 18:13:18 +0000 |
| commit | e4473a9007e7555809ec9e4087be636e8aadf017 (patch) | |
| tree | 9273204712b0eaf922589a473128aa886e0c207b /packages/cli/src/ui/hooks/slashCommandProcessor.ts | |
| parent | 38876b738f4c9ef8bd1b839d5e33580486e9a089 (diff) | |
Revert "chore(cli/slashcommands): Add status enum to SlashCommandEvent telemetry" (#6161)
Diffstat (limited to 'packages/cli/src/ui/hooks/slashCommandProcessor.ts')
| -rw-r--r-- | packages/cli/src/ui/hooks/slashCommandProcessor.ts | 124 |
1 files changed, 56 insertions, 68 deletions
diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.ts index aaa2fbff..b4ce0d4d 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.ts @@ -14,8 +14,7 @@ import { GitService, Logger, logSlashCommand, - makeSlashCommandEvent, - SlashCommandStatus, + SlashCommandEvent, ToolConfirmationOutcome, } from '@google/gemini-cli-core'; import { useSessionStats } from '../contexts/SessionContext.js'; @@ -230,70 +229,76 @@ export const useSlashCommandProcessor = ( overwriteConfirmed?: boolean, ): Promise<SlashCommandProcessorResult | false> => { setIsProcessing(true); + try { + if (typeof rawQuery !== 'string') { + return false; + } - if (typeof rawQuery !== 'string') { - return false; - } - - const trimmed = rawQuery.trim(); - if (!trimmed.startsWith('/') && !trimmed.startsWith('?')) { - return false; - } + const trimmed = rawQuery.trim(); + if (!trimmed.startsWith('/') && !trimmed.startsWith('?')) { + return false; + } - const userMessageTimestamp = Date.now(); - addItem({ type: MessageType.USER, text: trimmed }, userMessageTimestamp); + const userMessageTimestamp = Date.now(); + addItem( + { type: MessageType.USER, text: trimmed }, + userMessageTimestamp, + ); - const parts = trimmed.substring(1).trim().split(/\s+/); - const commandPath = parts.filter((p) => p); // The parts of the command, e.g., ['memory', 'add'] + const parts = trimmed.substring(1).trim().split(/\s+/); + const commandPath = parts.filter((p) => p); // The parts of the command, e.g., ['memory', 'add'] - let currentCommands = commands; - let commandToExecute: SlashCommand | undefined; - let pathIndex = 0; - let hasError = false; - const canonicalPath: string[] = []; + let currentCommands = commands; + let commandToExecute: SlashCommand | undefined; + let pathIndex = 0; + const canonicalPath: string[] = []; - for (const part of commandPath) { - // TODO: For better performance and architectural clarity, this two-pass - // search could be replaced. A more optimal approach would be to - // pre-compute a single lookup map in `CommandService.ts` that resolves - // all name and alias conflicts during the initial loading phase. The - // processor would then perform a single, fast lookup on that map. + for (const part of commandPath) { + // TODO: For better performance and architectural clarity, this two-pass + // search could be replaced. A more optimal approach would be to + // pre-compute a single lookup map in `CommandService.ts` that resolves + // all name and alias conflicts during the initial loading phase. The + // processor would then perform a single, fast lookup on that map. - // First pass: check for an exact match on the primary command name. - let foundCommand = currentCommands.find((cmd) => cmd.name === part); + // First pass: check for an exact match on the primary command name. + let foundCommand = currentCommands.find((cmd) => cmd.name === part); - // Second pass: if no primary name matches, check for an alias. - if (!foundCommand) { - foundCommand = currentCommands.find((cmd) => - cmd.altNames?.includes(part), - ); - } + // Second pass: if no primary name matches, check for an alias. + if (!foundCommand) { + foundCommand = currentCommands.find((cmd) => + cmd.altNames?.includes(part), + ); + } - if (foundCommand) { - commandToExecute = foundCommand; - canonicalPath.push(foundCommand.name); - pathIndex++; - if (foundCommand.subCommands) { - currentCommands = foundCommand.subCommands; + if (foundCommand) { + commandToExecute = foundCommand; + canonicalPath.push(foundCommand.name); + pathIndex++; + if (foundCommand.subCommands) { + currentCommands = foundCommand.subCommands; + } else { + break; + } } else { break; } - } else { - break; } - } - - const resolvedCommandPath = canonicalPath; - const subcommand = - resolvedCommandPath.length > 1 - ? resolvedCommandPath.slice(1).join(' ') - : undefined; - try { if (commandToExecute) { const args = parts.slice(pathIndex).join(' '); if (commandToExecute.action) { + if (config) { + const resolvedCommandPath = canonicalPath; + const event = new SlashCommandEvent( + resolvedCommandPath[0], + resolvedCommandPath.length > 1 + ? resolvedCommandPath.slice(1).join(' ') + : undefined, + ); + logSlashCommand(config, event); + } + const fullCommandContext: CommandContext = { ...commandContext, invocation: { @@ -315,6 +320,7 @@ export const useSlashCommandProcessor = ( ]), }; } + const result = await commandToExecute.action( fullCommandContext, args, @@ -487,18 +493,8 @@ export const useSlashCommandProcessor = ( content: `Unknown command: ${trimmed}`, timestamp: new Date(), }); - return { type: 'handled' }; - } catch (e: unknown) { - hasError = true; - if (config) { - const event = makeSlashCommandEvent({ - command: resolvedCommandPath[0], - subcommand, - status: SlashCommandStatus.ERROR, - }); - logSlashCommand(config, event); - } + } catch (e) { addItem( { type: MessageType.ERROR, @@ -508,14 +504,6 @@ export const useSlashCommandProcessor = ( ); return { type: 'handled' }; } finally { - if (config && resolvedCommandPath[0] && !hasError) { - const event = makeSlashCommandEvent({ - command: resolvedCommandPath[0], - subcommand, - status: SlashCommandStatus.SUCCESS, - }); - logSlashCommand(config, event); - } setIsProcessing(false); } }, |
