|
| 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 vscode from 'vscode'; |
| 7 | +import { CancellationToken } from '../../../util/vs/base/common/cancellation'; |
| 8 | +import { LanguageModelTextPart, LanguageModelToolResult, MarkdownString } from '../../../vscodeTypes'; |
| 9 | +import { ToolName } from '../common/toolNames'; |
| 10 | +import { ICopilotTool, ToolRegistry } from '../common/toolsRegistry'; |
| 11 | + |
| 12 | +interface ISwitchAgentParams { |
| 13 | + agentName: string; |
| 14 | +} |
| 15 | + |
| 16 | +export class SwitchAgentTool implements ICopilotTool<ISwitchAgentParams> { |
| 17 | + public static readonly toolName = ToolName.SwitchAgent; |
| 18 | + |
| 19 | + async invoke(options: vscode.LanguageModelToolInvocationOptions<ISwitchAgentParams>, token: CancellationToken): Promise<vscode.LanguageModelToolResult> { |
| 20 | + const { agentName } = options.input; |
| 21 | + |
| 22 | + // Only 'Plan' is supported |
| 23 | + if (agentName !== 'Plan') { |
| 24 | + throw new Error(vscode.l10n.t('Only "Plan" agent is supported')); |
| 25 | + } |
| 26 | + |
| 27 | + // Execute command to switch agent |
| 28 | + await vscode.commands.executeCommand('workbench.action.chat.toggleAgentMode', { |
| 29 | + modeId: agentName, |
| 30 | + sessionResource: options.chatSessionResource ? vscode.Uri.parse(options.chatSessionResource) : undefined |
| 31 | + }); |
| 32 | + |
| 33 | + return new LanguageModelToolResult([ |
| 34 | + new LanguageModelTextPart(JSON.stringify({ success: true, agentName })) |
| 35 | + ]); |
| 36 | + } |
| 37 | + |
| 38 | + prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<ISwitchAgentParams>, token: vscode.CancellationToken): vscode.ProviderResult<vscode.PreparedToolInvocation> { |
| 39 | + const { agentName } = options.input; |
| 40 | + |
| 41 | + if (agentName !== 'Plan') { |
| 42 | + throw new Error(vscode.l10n.t('Only "Plan" agent is supported. Received: "{0}"', agentName)); |
| 43 | + } |
| 44 | + |
| 45 | + return { |
| 46 | + invocationMessage: new MarkdownString(vscode.l10n.t('Switching to {0} agent', agentName)), |
| 47 | + pastTenseMessage: new MarkdownString(vscode.l10n.t('Switched to {0} agent', agentName)) |
| 48 | + }; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +ToolRegistry.registerTool(SwitchAgentTool); |
0 commit comments