Path: blob/main/src/vs/workbench/contrib/browserView/electron-browser/tools/openBrowserToolNonAgentic.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 type { CancellationToken } from '../../../../../base/common/cancellation.js';6import { localize } from '../../../../../nls.js';7import { logBrowserOpen } from '../../../../../platform/browserView/common/browserViewTelemetry.js';8import { BrowserViewUri } from '../../../../../platform/browserView/common/browserViewUri.js';9import { generateUuid } from '../../../../../base/common/uuid.js';10import { ITelemetryService } from '../../../../../platform/telemetry/common/telemetry.js';11import { IEditorService } from '../../../../services/editor/common/editorService.js';12import { type CountTokensCallback, type IPreparedToolInvocation, type IToolData, type IToolImpl, type IToolInvocation, type IToolInvocationPreparationContext, type IToolResult, type ToolProgress } from '../../../chat/common/tools/languageModelToolsService.js';13import { IOpenBrowserToolParams, OpenBrowserToolData } from './openBrowserTool.js';14import { MarkdownString } from '../../../../../base/common/htmlContent.js';15import { createBrowserPageLink, findExistingPagesByHost, getExistingPagesResult } from './browserToolHelpers.js';16import { IBrowserViewWorkbenchService } from '../../common/browserView.js';1718export const OpenBrowserToolNonAgenticData: IToolData = {19...OpenBrowserToolData,20modelDescription: 'Open a new browser page in the integrated browser at the given URL.',21inputSchema: {22...OpenBrowserToolData.inputSchema,23required: ['url'],24$comment: undefined25}26};2728export class OpenBrowserToolNonAgentic implements IToolImpl {29constructor(30@ITelemetryService private readonly telemetryService: ITelemetryService,31@IEditorService private readonly editorService: IEditorService,32@IBrowserViewWorkbenchService private readonly browserViewService: IBrowserViewWorkbenchService,33) { }3435async prepareToolInvocation(context: IToolInvocationPreparationContext, _token: CancellationToken): Promise<IPreparedToolInvocation | undefined> {36const params = context.parameters as IOpenBrowserToolParams;3738if (!params.url) {39throw new Error('The "url" parameter is required.');40}41const parsed = URL.parse(params.url);42if (!parsed) {43throw new Error('You must provide a complete, valid URL.');44}4546return {47invocationMessage: localize('browser.open.nonAgentic.invocation', "Opening browser page at {0}", parsed.href),48pastTenseMessage: localize('browser.open.nonAgentic.past', "Opened browser page at {0}", parsed.href),49confirmationMessages: {50title: localize('browser.open.nonAgentic.confirmTitle', 'Open Browser Page?'),51message: 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),52allowAutoConfirm: true,53},54};55}5657async invoke(invocation: IToolInvocation, _countTokens: CountTokensCallback, _progress: ToolProgress, _token: CancellationToken): Promise<IToolResult> {58const params = invocation.parameters as IOpenBrowserToolParams;5960if (!params.forceNew) {61const existingPages = findExistingPagesByHost(this.browserViewService, params.url!);62const existingResult = await getExistingPagesResult(this.editorService, existingPages, { excludeIds: true });63if (existingResult) {64return existingResult;65}66}6768logBrowserOpen(this.telemetryService, 'chatTool');6970const browserUri = BrowserViewUri.forId(generateUuid());71await this.editorService.openEditor({ resource: browserUri, options: { pinned: true, preserveFocus: true, viewState: { url: params.url } } });7273return {74content: [{75kind: 'text',76value: `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.`,77}],78toolResultMessage: new MarkdownString(localize('browser.open.nonAgentic.result', "Opened {0}", createBrowserPageLink(browserUri)))79};80}81}828384