Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/claudeToolPermission.ts
13405 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 { PermissionMode } from '@anthropic-ai/claude-agent-sdk';
7
import type * as vscode from 'vscode';
8
import { ClaudeToolInputMap, ClaudeToolNames } from './claudeTools';
9
10
/**
11
* Result type for tool permission checks, matching the Claude SDK canUseTool return type
12
*/
13
export type ClaudeToolPermissionResult =
14
| { behavior: 'allow'; updatedInput: Record<string, unknown> }
15
| { behavior: 'deny'; message: string };
16
17
/**
18
* Context passed to tool permission handlers
19
*/
20
export interface ClaudeToolPermissionContext {
21
readonly toolInvocationToken: vscode.ChatParticipantToolToken;
22
readonly permissionMode?: PermissionMode;
23
readonly stream?: vscode.ChatResponseStream;
24
}
25
26
/**
27
* Parameters for showing a confirmation dialog to the user
28
*/
29
export interface IClaudeToolConfirmationParams {
30
readonly title: string;
31
readonly message: string;
32
}
33
34
/**
35
* Handler interface for tool permission checks.
36
* Implement any combination of these methods to customize behavior:
37
* - canAutoApprove: Return true to skip confirmation dialog
38
* - getConfirmationParams: Customize the confirmation dialog
39
* - handle: Full implementation that bypasses default confirmation flow
40
*
41
* @template TToolName The tool name(s) this handler supports
42
*/
43
export interface IClaudeToolPermissionHandler<TToolName extends ClaudeToolNames = ClaudeToolNames> {
44
/**
45
* The tool name(s) this handler is registered for
46
*/
47
readonly toolNames: readonly TToolName[];
48
49
/**
50
* Check if the tool can be auto-approved without user confirmation.
51
* If not implemented or returns false, continues to confirmation dialog.
52
*/
53
canAutoApprove?(
54
toolName: TToolName,
55
input: ClaudeToolInputMap[TToolName],
56
context: ClaudeToolPermissionContext
57
): Promise<boolean>;
58
59
/**
60
* Get custom confirmation dialog parameters.
61
* If not implemented, uses default confirmation params.
62
*/
63
getConfirmationParams?(
64
toolName: TToolName,
65
input: ClaudeToolInputMap[TToolName]
66
): IClaudeToolConfirmationParams;
67
68
/**
69
* Full custom handler implementation.
70
* If implemented, bypasses canAutoApprove and getConfirmationParams entirely.
71
* Use this for tools like AskUserQuestion that need complete custom behavior.
72
*/
73
handle?(
74
toolName: TToolName,
75
input: ClaudeToolInputMap[TToolName],
76
context: ClaudeToolPermissionContext
77
): Promise<ClaudeToolPermissionResult>;
78
}
79
80
/**
81
* Constructor type for tool permission handlers
82
*/
83
export type IClaudeToolPermissionHandlerCtor<TToolName extends ClaudeToolNames = ClaudeToolNames> =
84
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Constructor types for DI require 'any' for parameter compatibility
85
new (...args: any[]) => IClaudeToolPermissionHandler<TToolName>;
86
87