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