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/handleDialogBrowserTool.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 HandleDialogBrowserToolData: IToolData = {
17
id: 'handle_dialog',
18
toolReferenceName: BrowserChatToolReferenceName.HandleDialog,
19
displayName: localize('handleDialogBrowserTool.displayName', 'Handle Dialog'),
20
userDescription: localize('handleDialogBrowserTool.userDescription', 'Respond to a dialog in a browser page'),
21
modelDescription: 'Respond to a pending modal (alert, confirm, prompt) or file chooser dialog on a browser page.',
22
icon: Codicon.comment,
23
source: ToolDataSource.Internal,
24
inputSchema: {
25
type: 'object',
26
properties: {
27
pageId: {
28
type: 'string',
29
description: `The browser page ID, acquired from context or the open tool.`
30
},
31
acceptModal: {
32
type: 'boolean',
33
description: 'Whether to accept (true) or dismiss (false) a modal dialog.'
34
},
35
promptText: {
36
type: 'string',
37
description: 'Text to enter into a prompt dialog.'
38
},
39
selectFiles: {
40
type: 'array',
41
items: { type: 'string' },
42
description: 'Absolute paths of files to select, or empty to dismiss. Required for file chooser dialogs.'
43
},
44
},
45
required: ['pageId'],
46
},
47
};
48
49
interface IHandleDialogBrowserToolParams {
50
pageId: string;
51
acceptModal: boolean;
52
promptText?: string;
53
selectFiles?: string[];
54
}
55
56
export class HandleDialogBrowserTool implements IToolImpl {
57
constructor(
58
@IPlaywrightService private readonly playwrightService: IPlaywrightService,
59
) { }
60
61
async prepareToolInvocation(_context: IToolInvocationPreparationContext, _token: CancellationToken): Promise<IPreparedToolInvocation | undefined> {
62
const link = createBrowserPageLink(_context.parameters.pageId);
63
return {
64
invocationMessage: new MarkdownString(localize('browser.handleDialog.invocation', "Handling dialog in {0}", link)),
65
pastTenseMessage: new MarkdownString(localize('browser.handleDialog.past', "Handled dialog in {0}", link)),
66
};
67
}
68
69
async invoke(invocation: IToolInvocation, _countTokens: CountTokensCallback, _progress: ToolProgress, _token: CancellationToken): Promise<IToolResult> {
70
const params = invocation.parameters as IHandleDialogBrowserToolParams;
71
72
if (!params.pageId) {
73
return errorResult(`No page ID provided. Use '${OpenPageToolId}' first.`);
74
}
75
76
if (params.selectFiles !== undefined && (params.acceptModal !== undefined || params.promptText !== undefined)) {
77
return errorResult(`Invalid parameters. 'selectFiles' cannot be used with 'acceptModal' or 'promptText'.`);
78
}
79
80
if (!Array.isArray(params.selectFiles) && (params.acceptModal === undefined || params.acceptModal === null)) {
81
return errorResult(`Invalid parameters. Either 'selectFiles' or 'acceptModal' must be provided.`);
82
}
83
84
try {
85
let result;
86
if (params.selectFiles !== undefined) {
87
result = await this.playwrightService.replyToFileChooser(params.pageId, params.selectFiles);
88
} else {
89
result = await this.playwrightService.replyToDialog(params.pageId, params.acceptModal, params.promptText);
90
}
91
return { content: [{ kind: 'text', value: result.summary }] };
92
} catch (e) {
93
return errorResult(e instanceof Error ? e.message : String(e));
94
}
95
}
96
}
97
98