Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/browserView/common/playwrightService.ts
13397 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 { Event } from '../../../base/common/event.js';
7
import { createDecorator } from '../../instantiation/common/instantiation.js';
8
9
export const IPlaywrightService = createDecorator<IPlaywrightService>('playwrightService');
10
11
export interface IInvokeFunctionResult {
12
result?: unknown;
13
error?: string;
14
summary: string;
15
/** When present the function did not complete within the timeout. Pass this ID to {@link IPlaywrightService.waitForDeferredResult} to keep waiting. */
16
deferredResultId?: string;
17
}
18
19
/**
20
* A service for using Playwright to connect to and automate the integrated browser.
21
*
22
* Pages must be explicitly tracked via {@link startTrackingPage} (or implicitly via
23
* {@link openPage}) before they can be interacted with.
24
*/
25
export interface IPlaywrightService {
26
readonly _serviceBrand: undefined;
27
28
/**
29
* Fires when the set of tracked pages changes.
30
* The event value is the full list of currently tracked view IDs.
31
*/
32
readonly onDidChangeTrackedPages: Event<readonly string[]>;
33
34
/**
35
* Start tracking an existing browser view so that agent
36
* tools can interact with it.
37
* @param viewId The browser view identifier.
38
*/
39
startTrackingPage(viewId: string): Promise<void>;
40
41
/**
42
* Stop tracking a browser view.
43
* @param viewId The browser view identifier.
44
*/
45
stopTrackingPage(viewId: string): Promise<void>;
46
47
/**
48
* Whether the given page is currently tracked by the service.
49
*/
50
isPageTracked(viewId: string): Promise<boolean>;
51
52
/**
53
* Get the list of currently tracked page IDs.
54
*/
55
getTrackedPages(): Promise<readonly string[]>;
56
57
/**
58
* Opens a new page in the browser and returns its associated view ID.
59
* The page is automatically added to the tracked pages.
60
* @param url The URL to open in the new page.
61
* @returns An object containing the new page's view ID and a summary of its initial state.
62
*/
63
openPage(url: string): Promise<{ pageId: string; summary: string }>;
64
65
/**
66
* Gets a summary of the page's current state, including its DOM and visual representation.
67
* @param pageId The browser view ID identifying the page to read.
68
* @returns The summary of the page's current state.
69
*/
70
getSummary(pageId: string): Promise<string>;
71
72
/**
73
* Run a function with access to a Playwright page and return its raw result, or throw an error.
74
* The first function argument is always the Playwright `page` object, and additional arguments can be passed after.
75
* @param pageId The browser view ID identifying the page to operate on.
76
* @param fnDef The function code to execute. Should contain the function definition but not its invocation, e.g. `async (page, arg1, arg2) => { ... }`.
77
* @param args Additional arguments to pass to the function after the `page` object.
78
* @returns The result of the function execution.
79
*/
80
invokeFunctionRaw<T>(pageId: string, fnDef: string, ...args: unknown[]): Promise<T>;
81
82
/**
83
* Run a function with access to a Playwright page and return a result for tool output, including error handling.
84
* The first function argument is always the Playwright `page` object, and additional arguments can be passed after.
85
*
86
* When {@link timeoutMs} is provided, the call races against that timeout.
87
* If the timeout fires before the function completes, or the function is otherwise interrupted,
88
* the in-flight promise is stored as a *deferred result* and the returned object includes a
89
* {@link deferredResultId} that can be passed to {@link waitForDeferredResult} to resume waiting.
90
* When {@link timeoutMs} is omitted the function runs to completion with no deferral.
91
*
92
* @param pageId The browser view ID identifying the page to operate on.
93
* @param fnDef The function code to execute. Should contain the function definition but not its invocation, e.g. `async (page, arg1, arg2) => { ... }`.
94
* @param args Additional arguments to pass to the function after the `page` object.
95
* @param timeoutMs Maximum time (in ms) to wait for the function to complete before deferring. When omitted the call awaits indefinitely.
96
* @returns The result of the function execution, including a page summary and optionally a deferredResultId if the call did not complete.
97
*/
98
invokeFunction(pageId: string, fnDef: string, args?: unknown[], timeoutMs?: number): Promise<IInvokeFunctionResult>;
99
100
/**
101
* Continue waiting for a previously deferred function invocation.
102
*
103
* @param deferredResultId The ID returned from a timed-out {@link invokeFunction} call.
104
* @param timeoutMs Maximum time (in ms) to wait before returning a deferred result again.
105
* @returns The same shape as {@link invokeFunction}. If the result is still not
106
* available after the timeout, {@link deferredResultId} is returned again.
107
*/
108
waitForDeferredResult(deferredResultId: string, timeoutMs: number): Promise<IInvokeFunctionResult>;
109
110
/**
111
* Responds to a file chooser dialog on the given page.
112
* @param pageId The browser view ID identifying the page.
113
* @param files The list of files to select in the file chooser. Empty to dismiss the dialog without selecting files.
114
* @returns An object with the page summary afterwards.
115
*/
116
replyToFileChooser(pageId: string, files: string[]): Promise<{ summary: string }>;
117
118
/**
119
* Responds to a dialog (alert, confirm, prompt) on the given page.
120
* @param pageId The browser view ID identifying the page.
121
* @param accept Whether to accept or dismiss the dialog.
122
* @param promptText Optional text to enter into a prompt dialog.
123
* @returns An object with the page summary afterwards.
124
*/
125
replyToDialog(pageId: string, accept: boolean, promptText?: string): Promise<{ summary: string }>;
126
}
127
128