Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/browserView/electron-browser/tools/openBrowserToolNonAgentic.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 type { CancellationToken } from '../../../../../base/common/cancellation.js';
7
import { localize } from '../../../../../nls.js';
8
import { logBrowserOpen } from '../../../../../platform/browserView/common/browserViewTelemetry.js';
9
import { BrowserViewUri } from '../../../../../platform/browserView/common/browserViewUri.js';
10
import { generateUuid } from '../../../../../base/common/uuid.js';
11
import { ITelemetryService } from '../../../../../platform/telemetry/common/telemetry.js';
12
import { IEditorService } from '../../../../services/editor/common/editorService.js';
13
import { type CountTokensCallback, type IPreparedToolInvocation, type IToolData, type IToolImpl, type IToolInvocation, type IToolInvocationPreparationContext, type IToolResult, type ToolProgress } from '../../../chat/common/tools/languageModelToolsService.js';
14
import { IOpenBrowserToolParams, OpenBrowserToolData } from './openBrowserTool.js';
15
import { MarkdownString } from '../../../../../base/common/htmlContent.js';
16
import { createBrowserPageLink, findExistingPagesByHost, getExistingPagesResult } from './browserToolHelpers.js';
17
import { IBrowserViewWorkbenchService } from '../../common/browserView.js';
18
19
export const OpenBrowserToolNonAgenticData: IToolData = {
20
...OpenBrowserToolData,
21
modelDescription: 'Open a new browser page in the integrated browser at the given URL.',
22
inputSchema: {
23
...OpenBrowserToolData.inputSchema,
24
required: ['url'],
25
$comment: undefined
26
}
27
};
28
29
export class OpenBrowserToolNonAgentic implements IToolImpl {
30
constructor(
31
@ITelemetryService private readonly telemetryService: ITelemetryService,
32
@IEditorService private readonly editorService: IEditorService,
33
@IBrowserViewWorkbenchService private readonly browserViewService: IBrowserViewWorkbenchService,
34
) { }
35
36
async prepareToolInvocation(context: IToolInvocationPreparationContext, _token: CancellationToken): Promise<IPreparedToolInvocation | undefined> {
37
const params = context.parameters as IOpenBrowserToolParams;
38
39
if (!params.url) {
40
throw new Error('The "url" parameter is required.');
41
}
42
const parsed = URL.parse(params.url);
43
if (!parsed) {
44
throw new Error('You must provide a complete, valid URL.');
45
}
46
47
return {
48
invocationMessage: localize('browser.open.nonAgentic.invocation', "Opening browser page at {0}", parsed.href),
49
pastTenseMessage: localize('browser.open.nonAgentic.past', "Opened browser page at {0}", parsed.href),
50
confirmationMessages: {
51
title: localize('browser.open.nonAgentic.confirmTitle', 'Open Browser Page?'),
52
message: localize('browser.open.nonAgentic.confirmMessage', 'This will open {0} in the integrated browser. The agent will not be able to read its contents.', parsed.href),
53
allowAutoConfirm: true,
54
},
55
};
56
}
57
58
async invoke(invocation: IToolInvocation, _countTokens: CountTokensCallback, _progress: ToolProgress, _token: CancellationToken): Promise<IToolResult> {
59
const params = invocation.parameters as IOpenBrowserToolParams;
60
61
if (!params.forceNew) {
62
const existingPages = findExistingPagesByHost(this.browserViewService, params.url!);
63
const existingResult = await getExistingPagesResult(this.editorService, existingPages, { excludeIds: true });
64
if (existingResult) {
65
return existingResult;
66
}
67
}
68
69
logBrowserOpen(this.telemetryService, 'chatTool');
70
71
const browserUri = BrowserViewUri.forId(generateUuid());
72
await this.editorService.openEditor({ resource: browserUri, options: { pinned: true, preserveFocus: true, viewState: { url: params.url } } });
73
74
return {
75
content: [{
76
kind: 'text',
77
value: `Page opened successfully. Note that you do not have access to the page contents unless the user enables agentic tools via the \`workbench.browser.enableChatTools\` setting.`,
78
}],
79
toolResultMessage: new MarkdownString(localize('browser.open.nonAgentic.result', "Opened {0}", createBrowserPageLink(browserUri)))
80
};
81
}
82
}
83
84