Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/toolPermissionHandlers/bashToolHandler.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { BashInput } from '@anthropic-ai/claude-agent-sdk/sdk-tools';
7
import { CancellationToken } from '../../../../../util/vs/base/common/cancellation';
8
import { LanguageModelTextPart } from '../../../../../vscodeTypes';
9
import { ToolName } from '../../../../tools/common/toolNames';
10
import { IToolsService } from '../../../../tools/common/toolsService';
11
import { ClaudeToolPermissionContext, ClaudeToolPermissionResult, IClaudeToolPermissionHandler } from '../claudeToolPermission';
12
import { registerToolPermissionHandler } from '../claudeToolPermissionRegistry';
13
import { ClaudeToolNames } from '../claudeTools';
14
15
/**
16
* Default deny message when user declines a tool
17
*/
18
const DenyToolMessage = 'The user declined to run the tool';
19
20
/**
21
* Handler for the Bash tool.
22
* Uses terminal-style confirmation with the command highlighted.
23
* See: src/extension/agents/copilotcli/node/permissionHelpers.ts#L126-L127
24
*/
25
export class BashToolHandler implements IClaudeToolPermissionHandler<ClaudeToolNames.Bash> {
26
public readonly toolNames = [ClaudeToolNames.Bash] as const;
27
28
constructor(
29
@IToolsService private readonly toolsService: IToolsService,
30
) { }
31
32
public async handle(
33
_toolName: ClaudeToolNames.Bash,
34
input: BashInput,
35
context: ClaudeToolPermissionContext
36
): Promise<ClaudeToolPermissionResult> {
37
try {
38
const result = await this.toolsService.invokeTool(ToolName.CoreTerminalConfirmationTool, {
39
input: {
40
message: input.description || input.command,
41
command: input.command,
42
isBackground: input.run_in_background ?? false
43
},
44
toolInvocationToken: context.toolInvocationToken,
45
}, CancellationToken.None);
46
47
const firstResultPart = result.content.at(0);
48
if (firstResultPart instanceof LanguageModelTextPart && firstResultPart.value === 'yes') {
49
return {
50
behavior: 'allow',
51
updatedInput: input as unknown as Record<string, unknown>
52
};
53
}
54
} catch { }
55
56
return {
57
behavior: 'deny',
58
message: DenyToolMessage
59
};
60
}
61
}
62
63
// Self-register the handler
64
registerToolPermissionHandler(
65
[ClaudeToolNames.Bash],
66
BashToolHandler
67
);
68
69