Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/toolPermissionHandlers/bashToolHandler.ts
13406 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { BashInput } from '@anthropic-ai/claude-agent-sdk/sdk-tools';6import { CancellationToken } from '../../../../../util/vs/base/common/cancellation';7import { LanguageModelTextPart } from '../../../../../vscodeTypes';8import { ToolName } from '../../../../tools/common/toolNames';9import { IToolsService } from '../../../../tools/common/toolsService';10import { ClaudeToolPermissionContext, ClaudeToolPermissionResult, IClaudeToolPermissionHandler } from '../claudeToolPermission';11import { registerToolPermissionHandler } from '../claudeToolPermissionRegistry';12import { ClaudeToolNames } from '../claudeTools';1314/**15* Default deny message when user declines a tool16*/17const DenyToolMessage = 'The user declined to run the tool';1819/**20* Handler for the Bash tool.21* Uses terminal-style confirmation with the command highlighted.22* See: src/extension/agents/copilotcli/node/permissionHelpers.ts#L126-L12723*/24export class BashToolHandler implements IClaudeToolPermissionHandler<ClaudeToolNames.Bash> {25public readonly toolNames = [ClaudeToolNames.Bash] as const;2627constructor(28@IToolsService private readonly toolsService: IToolsService,29) { }3031public async handle(32_toolName: ClaudeToolNames.Bash,33input: BashInput,34context: ClaudeToolPermissionContext35): Promise<ClaudeToolPermissionResult> {36try {37const result = await this.toolsService.invokeTool(ToolName.CoreTerminalConfirmationTool, {38input: {39message: input.description || input.command,40command: input.command,41isBackground: input.run_in_background ?? false42},43toolInvocationToken: context.toolInvocationToken,44}, CancellationToken.None);4546const firstResultPart = result.content.at(0);47if (firstResultPart instanceof LanguageModelTextPart && firstResultPart.value === 'yes') {48return {49behavior: 'allow',50updatedInput: input as unknown as Record<string, unknown>51};52}53} catch { }5455return {56behavior: 'deny',57message: DenyToolMessage58};59}60}6162// Self-register the handler63registerToolPermissionHandler(64[ClaudeToolNames.Bash],65BashToolHandler66);676869