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