| |
| |
| |
| |
| |
|
|
| import type { PartListUnion } from '@google/genai'; |
| import { parseSlashCommand } from './utils/commands.js'; |
| import { |
| FatalInputError, |
| Logger, |
| uiTelemetryService, |
| type Config, |
| } from '@google/gemini-cli-core'; |
| import { CommandService } from './services/CommandService.js'; |
| import { BuiltinCommandLoader } from './services/BuiltinCommandLoader.js'; |
| import { FileCommandLoader } from './services/FileCommandLoader.js'; |
| import { McpPromptLoader } from './services/McpPromptLoader.js'; |
| import type { CommandContext } from './ui/commands/types.js'; |
| import { createNonInteractiveUI } from './ui/noninteractive/nonInteractiveUi.js'; |
| import type { LoadedSettings } from './config/settings.js'; |
| import type { SessionStatsState } from './ui/contexts/SessionContext.js'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export const handleSlashCommand = async ( |
| rawQuery: string, |
| abortController: AbortController, |
| config: Config, |
| settings: LoadedSettings, |
| ): Promise<PartListUnion | undefined> => { |
| const trimmed = rawQuery.trim(); |
| if (!trimmed.startsWith('/')) { |
| return; |
| } |
|
|
| const commandService = await CommandService.create( |
| [ |
| new BuiltinCommandLoader(config), |
| new McpPromptLoader(config), |
| new FileCommandLoader(config), |
| ], |
| abortController.signal, |
| ); |
| const commands = commandService.getCommands(); |
|
|
| const { commandToExecute, args } = parseSlashCommand(rawQuery, commands); |
|
|
| if (commandToExecute) { |
| if (commandToExecute.action) { |
| |
| const sessionStats: SessionStatsState = { |
| sessionId: config?.getSessionId(), |
| sessionStartTime: new Date(), |
| metrics: uiTelemetryService.getMetrics(), |
| lastPromptTokenCount: 0, |
| promptCount: 1, |
| }; |
|
|
| const logger = new Logger(config?.getSessionId() || '', config?.storage); |
|
|
| const commandContext: CommandContext = { |
| services: { |
| agentContext: config, |
| settings, |
| git: undefined, |
| logger, |
| }, |
| ui: createNonInteractiveUI(), |
| session: { |
| stats: sessionStats, |
| sessionShellAllowlist: new Set(), |
| }, |
| invocation: { |
| raw: trimmed, |
| name: commandToExecute.name, |
| args, |
| }, |
| }; |
|
|
| const result = await commandToExecute.action(commandContext, args); |
|
|
| if (result) { |
| switch (result.type) { |
| case 'submit_prompt': |
| return result.content; |
| case 'confirm_shell_commands': |
| |
| |
| |
| |
| |
| |
| throw new FatalInputError( |
| 'Exiting due to a confirmation prompt requested by the command.', |
| ); |
| default: |
| throw new FatalInputError( |
| 'Exiting due to command result that is not supported in non-interactive mode.', |
| ); |
| } |
| } |
| } |
| } |
|
|
| return; |
| }; |
|
|