|
| 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 * as l10n from '@vscode/l10n'; |
| 7 | +import { CancellationToken } from '../../../../util/vs/base/common/cancellation'; |
| 8 | +import { IInstantiationService, createDecorator } from '../../../../util/vs/platform/instantiation/common/instantiation'; |
| 9 | +import { LanguageModelTextPart } from '../../../../vscodeTypes'; |
| 10 | +import { ToolName } from '../../../tools/common/toolNames'; |
| 11 | +import { IToolsService } from '../../../tools/common/toolsService'; |
| 12 | +import { |
| 13 | + ClaudeToolPermissionContext, |
| 14 | + ClaudeToolPermissionResult, |
| 15 | + IClaudeToolConfirmationParams, |
| 16 | + IClaudeToolPermissionHandler |
| 17 | +} from './claudeToolPermission'; |
| 18 | +import { getToolPermissionHandlerRegistry } from './claudeToolPermissionRegistry'; |
| 19 | +import { ClaudeToolInputMap, ClaudeToolNames } from './claudeTools'; |
| 20 | + |
| 21 | +export const IClaudeToolPermissionService = createDecorator<IClaudeToolPermissionService>('claudeToolPermissionService'); |
| 22 | + |
| 23 | +export interface IClaudeToolPermissionService { |
| 24 | + readonly _serviceBrand: undefined; |
| 25 | + |
| 26 | + /** |
| 27 | + * Check if a tool can be used, showing confirmation if needed. |
| 28 | + * @param toolName The name of the Claude tool |
| 29 | + * @param input The tool input parameters |
| 30 | + * @param context Context including the tool invocation token |
| 31 | + * @returns Permission result (allow with updated input, or deny with message) |
| 32 | + */ |
| 33 | + canUseTool( |
| 34 | + toolName: string, |
| 35 | + input: Record<string, unknown>, |
| 36 | + context: ClaudeToolPermissionContext |
| 37 | + ): Promise<ClaudeToolPermissionResult>; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Default deny message when user declines a tool |
| 42 | + */ |
| 43 | +const DenyToolMessage = 'The user declined to run the tool'; |
| 44 | + |
| 45 | +export class ClaudeToolPermissionService implements IClaudeToolPermissionService { |
| 46 | + declare readonly _serviceBrand: undefined; |
| 47 | + |
| 48 | + private readonly _handlerCache = new Map<ClaudeToolNames, IClaudeToolPermissionHandler>(); |
| 49 | + |
| 50 | + constructor( |
| 51 | + @IInstantiationService private readonly instantiationService: IInstantiationService, |
| 52 | + @IToolsService private readonly toolsService: IToolsService, |
| 53 | + ) { } |
| 54 | + |
| 55 | + public async canUseTool( |
| 56 | + toolName: string, |
| 57 | + input: Record<string, unknown>, |
| 58 | + context: ClaudeToolPermissionContext |
| 59 | + ): Promise<ClaudeToolPermissionResult> { |
| 60 | + const handler = this._getHandler(toolName as ClaudeToolNames); |
| 61 | + |
| 62 | + // If handler has full custom implementation, use it |
| 63 | + if (handler?.handle) { |
| 64 | + return handler.handle(toolName as ClaudeToolNames, input as ClaudeToolInputMap[ClaudeToolNames], context); |
| 65 | + } |
| 66 | + |
| 67 | + // Check auto-approve |
| 68 | + if (handler?.canAutoApprove) { |
| 69 | + const canAutoApprove = await handler.canAutoApprove(toolName as ClaudeToolNames, input as ClaudeToolInputMap[ClaudeToolNames], context); |
| 70 | + if (canAutoApprove) { |
| 71 | + return { behavior: 'allow', updatedInput: input }; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Get confirmation params (custom or default) |
| 76 | + const confirmationParams = handler?.getConfirmationParams |
| 77 | + ? handler.getConfirmationParams(toolName as ClaudeToolNames, input as ClaudeToolInputMap[ClaudeToolNames]) |
| 78 | + : this._getDefaultConfirmationParams(toolName, input); |
| 79 | + |
| 80 | + // Show confirmation dialog |
| 81 | + return this._showConfirmation(confirmationParams, input, context); |
| 82 | + } |
| 83 | + |
| 84 | + private _getHandler(toolName: ClaudeToolNames): IClaudeToolPermissionHandler | undefined { |
| 85 | + // Check cache first |
| 86 | + if (this._handlerCache.has(toolName)) { |
| 87 | + return this._handlerCache.get(toolName); |
| 88 | + } |
| 89 | + |
| 90 | + // Find registration for this tool |
| 91 | + const registration = getToolPermissionHandlerRegistry().find(r => r.toolNames.includes(toolName)); |
| 92 | + if (!registration) { |
| 93 | + return undefined; |
| 94 | + } |
| 95 | + |
| 96 | + // Create handler instance |
| 97 | + const handler = this.instantiationService.createInstance(registration.ctor); |
| 98 | + // Cache for all tool names this handler supports |
| 99 | + for (const name of registration.toolNames) { |
| 100 | + this._handlerCache.set(name, handler); |
| 101 | + } |
| 102 | + |
| 103 | + return handler; |
| 104 | + } |
| 105 | + |
| 106 | + private _getDefaultConfirmationParams(toolName: string, input: Record<string, unknown>): IClaudeToolConfirmationParams { |
| 107 | + return { |
| 108 | + title: l10n.t('Use {0}?', toolName), |
| 109 | + message: `\`\`\`\n${JSON.stringify(input, null, 2)}\n\`\`\``, |
| 110 | + confirmationType: 'basic' |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + private async _showConfirmation( |
| 115 | + params: IClaudeToolConfirmationParams, |
| 116 | + input: Record<string, unknown>, |
| 117 | + context: ClaudeToolPermissionContext |
| 118 | + ): Promise<ClaudeToolPermissionResult> { |
| 119 | + try { |
| 120 | + const result = await this.toolsService.invokeTool(ToolName.CoreConfirmationTool, { |
| 121 | + input: params, |
| 122 | + toolInvocationToken: context.toolInvocationToken, |
| 123 | + }, CancellationToken.None); |
| 124 | + |
| 125 | + const firstResultPart = result.content.at(0); |
| 126 | + if (firstResultPart instanceof LanguageModelTextPart && firstResultPart.value === 'yes') { |
| 127 | + return { |
| 128 | + behavior: 'allow', |
| 129 | + updatedInput: input |
| 130 | + }; |
| 131 | + } |
| 132 | + } catch { } |
| 133 | + |
| 134 | + return { |
| 135 | + behavior: 'deny', |
| 136 | + message: DenyToolMessage |
| 137 | + }; |
| 138 | + } |
| 139 | +} |
0 commit comments