forked from microsoft/vscode-copilot-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleBrowserTool.tsx
More file actions
65 lines (54 loc) · 2.88 KB
/
Copy pathsimpleBrowserTool.tsx
File metadata and controls
65 lines (54 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import type * as vscode from 'vscode';
import { IRunCommandExecutionService } from '../../../platform/commands/common/runCommandExecutionService';
import { ResourceSet } from '../../../util/vs/base/common/map';
import { Schemas } from '../../../util/vs/base/common/network';
import { URI } from '../../../util/vs/base/common/uri';
import { LanguageModelTextPart, LanguageModelToolResult, MarkdownString } from '../../../vscodeTypes';
import { IBuildPromptContext } from '../../prompt/common/intents';
import { ToolName } from '../common/toolNames';
import { ICopilotTool, ToolRegistry } from '../common/toolsRegistry';
export interface ISimpleBrowserParams {
url: string;
}
export class SimpleBrowserTool implements ICopilotTool<ISimpleBrowserParams> {
public static toolName = ToolName.SimpleBrowser;
private _alreadyApprovedDomains = new ResourceSet();
constructor(
@IRunCommandExecutionService private readonly commandService: IRunCommandExecutionService,
) { }
async invoke(options: vscode.LanguageModelToolInvocationOptions<ISimpleBrowserParams>, token: vscode.CancellationToken) {
const uri = URI.parse(options.input.url);
this._alreadyApprovedDomains.add(uri);
this.commandService.executeCommand('simpleBrowser.show', options.input.url);
return new LanguageModelToolResult([
new LanguageModelTextPart(
l10n.t('Simple Browser opened at {0}', options.input.url),
)
]);
}
async resolveInput(input: ISimpleBrowserParams, promptContext: IBuildPromptContext): Promise<ISimpleBrowserParams> {
return input;
}
prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<ISimpleBrowserParams>, token: vscode.CancellationToken): vscode.ProviderResult<vscode.PreparedToolInvocation> {
const uri = URI.parse(options.input.url);
if (uri.scheme !== Schemas.http && uri.scheme !== Schemas.https) {
throw new Error(l10n.t('Invalid URL scheme. Only HTTP and HTTPS are supported.'));
}
const urlsNeedingConfirmation = !this._alreadyApprovedDomains.has(uri);
let confirmationMessages: vscode.LanguageModelToolConfirmationMessages | undefined;
if (urlsNeedingConfirmation) {
confirmationMessages = { title: l10n.t`Open untrusted web page?`, message: new MarkdownString(l10n.t`${options.input.url}`) };
}
return {
invocationMessage: new MarkdownString(l10n.t`Opening Simple Browser at ${options.input.url}`),
pastTenseMessage: new MarkdownString(l10n.t`Opened Simple Browser at ${options.input.url}`),
confirmationMessages
};
}
}
ToolRegistry.registerTool(SimpleBrowserTool);