Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/claudeToolPermission.ts
13405 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 { PermissionMode } from '@anthropic-ai/claude-agent-sdk';6import type * as vscode from 'vscode';7import { ClaudeToolInputMap, ClaudeToolNames } from './claudeTools';89/**10* Result type for tool permission checks, matching the Claude SDK canUseTool return type11*/12export type ClaudeToolPermissionResult =13| { behavior: 'allow'; updatedInput: Record<string, unknown> }14| { behavior: 'deny'; message: string };1516/**17* Context passed to tool permission handlers18*/19export interface ClaudeToolPermissionContext {20readonly toolInvocationToken: vscode.ChatParticipantToolToken;21readonly permissionMode?: PermissionMode;22readonly stream?: vscode.ChatResponseStream;23}2425/**26* Parameters for showing a confirmation dialog to the user27*/28export interface IClaudeToolConfirmationParams {29readonly title: string;30readonly message: string;31}3233/**34* Handler interface for tool permission checks.35* Implement any combination of these methods to customize behavior:36* - canAutoApprove: Return true to skip confirmation dialog37* - getConfirmationParams: Customize the confirmation dialog38* - handle: Full implementation that bypasses default confirmation flow39*40* @template TToolName The tool name(s) this handler supports41*/42export interface IClaudeToolPermissionHandler<TToolName extends ClaudeToolNames = ClaudeToolNames> {43/**44* The tool name(s) this handler is registered for45*/46readonly toolNames: readonly TToolName[];4748/**49* Check if the tool can be auto-approved without user confirmation.50* If not implemented or returns false, continues to confirmation dialog.51*/52canAutoApprove?(53toolName: TToolName,54input: ClaudeToolInputMap[TToolName],55context: ClaudeToolPermissionContext56): Promise<boolean>;5758/**59* Get custom confirmation dialog parameters.60* If not implemented, uses default confirmation params.61*/62getConfirmationParams?(63toolName: TToolName,64input: ClaudeToolInputMap[TToolName]65): IClaudeToolConfirmationParams;6667/**68* Full custom handler implementation.69* If implemented, bypasses canAutoApprove and getConfirmationParams entirely.70* Use this for tools like AskUserQuestion that need complete custom behavior.71*/72handle?(73toolName: TToolName,74input: ClaudeToolInputMap[TToolName],75context: ClaudeToolPermissionContext76): Promise<ClaudeToolPermissionResult>;77}7879/**80* Constructor type for tool permission handlers81*/82export type IClaudeToolPermissionHandlerCtor<TToolName extends ClaudeToolNames = ClaudeToolNames> =83// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Constructor types for DI require 'any' for parameter compatibility84new (...args: any[]) => IClaudeToolPermissionHandler<TToolName>;858687