Implementation:Microsoft Playwright PageAgentDispatcher
| Knowledge Sources | |
|---|---|
| Domains | Dispatcher, Agent |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for exposing the AI-driven page agent capabilities over the Playwright RPC protocol provided by the Playwright library.
Description
The `PageAgentDispatcher` class extends `Dispatcher` and implements `channels.PageAgentChannel` to bridge the server-side page agent functionality to remote clients. The page agent provides AI-powered automation through three core methods: `perform` (execute a task described in natural language), `expect` (assert an expectation against the page state), and `extract` (extract structured data from the page). Each method delegates to corresponding functions (`pageAgentPerform`, `pageAgentExpect`, `pageAgentExtract`) from the `pageAgent` module, tracks usage statistics (turns, input/output tokens), and maintains a history of operations via a `Context` object. The dispatcher tracks a `Usage` type with token counts.
Usage
Use PageAgentDispatcher when exposing Playwright's AI page agent capabilities over the protocol, enabling natural language interaction with web pages.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/dispatchers/pageAgentDispatcher.ts
Signature
export class PageAgentDispatcher extends Dispatcher<SdkObject, channels.PageAgentChannel, DispatcherScope> implements channels.PageAgentChannel {
_type_PageAgent: boolean;
_type_EventTarget: boolean;
constructor(scope: PageDispatcher, options: channels.PageAgentParams);
async perform(params: channels.PageAgentPerformParams, progress: Progress): Promise<channels.PageAgentPerformResult>;
async expect(params: channels.PageAgentExpectParams, progress: Progress): Promise<channels.PageAgentExpectResult>;
async extract(params: channels.PageAgentExtractParams, progress: Progress): Promise<channels.PageAgentExtractResult>;
}
Import
import { PageAgentDispatcher } from '../server/dispatchers/pageAgentDispatcher';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| scope | PageDispatcher | Yes | Parent page dispatcher scope |
| options | channels.PageAgentParams | Yes | Agent configuration options |
| params.task | string | Yes (perform) | Natural language task description |
| params.expectation | string | Yes (expect) | Natural language expectation to verify |
| params.instruction | string | Yes (extract) | Natural language extraction instruction |
Outputs
| Name | Type | Description |
|---|---|---|
| usage | Usage | Token usage statistics (turns, inputTokens, outputTokens) |
| data | string | Extracted data (for extract method) |
Usage Examples
import { PageAgentDispatcher } from '../server/dispatchers/pageAgentDispatcher';
const agent = new PageAgentDispatcher(pageScope, agentOptions);
const { usage } = await agent.perform({ task: 'Click the login button' }, progress);
const { usage: expectUsage } = await agent.expect({ expectation: 'The page shows a welcome message' }, progress);
const { data } = await agent.extract({ instruction: 'Get all product names' }, progress);