Path: blob/main/src/vs/workbench/contrib/browserView/electron-browser/tools/readBrowserTool.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 { Codicon } from '../../../../../base/common/codicons.js';7import { MarkdownString } from '../../../../../base/common/htmlContent.js';8import { localize } from '../../../../../nls.js';9import { IPlaywrightService } from '../../../../../platform/browserView/common/playwrightService.js';10import { ToolDataSource, type CountTokensCallback, type IPreparedToolInvocation, type IToolData, type IToolImpl, type IToolInvocation, type IToolInvocationPreparationContext, type IToolResult, type ToolProgress } from '../../../chat/common/tools/languageModelToolsService.js';11import { createBrowserPageLink, errorResult } from './browserToolHelpers.js';12import { BrowserChatToolReferenceName } from '../../common/browserChatToolReferenceNames.js';13import { OpenPageToolId } from './openBrowserTool.js';1415export const ReadBrowserToolData: IToolData = {16id: 'read_page',17toolReferenceName: BrowserChatToolReferenceName.ReadPage,18displayName: localize('readBrowserTool.displayName', 'Read Page'),19userDescription: localize('readBrowserTool.userDescription', 'Read the content of a browser page'),20modelDescription: 'Get a snapshot of the current browser page state. This is better than screenshot.',21icon: Codicon.fileText,22source: ToolDataSource.Internal,23inputSchema: {24type: 'object',25properties: {26pageId: {27type: 'string',28description: `The browser page ID to read, acquired from context or the open tool.`29},30},31required: ['pageId'],32},33};3435interface IReadBrowserToolParams {36pageId: string;37}3839export class ReadBrowserTool implements IToolImpl {40constructor(41@IPlaywrightService private readonly playwrightService: IPlaywrightService,42) { }4344async prepareToolInvocation(_context: IToolInvocationPreparationContext, _token: CancellationToken): Promise<IPreparedToolInvocation | undefined> {45const link = createBrowserPageLink(_context.parameters.pageId);46return {47invocationMessage: new MarkdownString(localize('browser.read.invocation', "Reading {0}", link)),48pastTenseMessage: new MarkdownString(localize('browser.read.past', "Read {0}", link)),49};50}5152async invoke(invocation: IToolInvocation, _countTokens: CountTokensCallback, _progress: ToolProgress, _token: CancellationToken): Promise<IToolResult> {53const params = invocation.parameters as IReadBrowserToolParams;5455if (!params.pageId) {56return errorResult(`No page ID provided. Use '${OpenPageToolId}' first.`);57}5859const summary = await this.playwrightService.getSummary(params.pageId);60if (!summary) {61return errorResult('No page summary available.');62}6364return {65content: [{66kind: 'text',67value: summary,68}],69};70}71}727374