Skip to content

Commit 0062bcb

Browse files
committed
Add support for PageHandle in AnyPage
1 parent fe186d2 commit 0062bcb

File tree

7 files changed

+420
-14
lines changed

7 files changed

+420
-14
lines changed

packages/core/lib/v3/types/public/agent.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
22
import { ToolSet } from "ai";
33
import { LogLine } from "./logs";
4-
import { Page as PlaywrightPage } from "playwright-core";
5-
import { Page as PuppeteerPage } from "puppeteer-core";
6-
import { Page as PatchrightPage } from "patchright-core";
7-
import { Page } from "../../understudy/page";
4+
import { AnyPage } from "./page";
85

96
export interface AgentAction {
107
type: string;
@@ -37,7 +34,7 @@ export interface AgentResult {
3734
export interface AgentExecuteOptions {
3835
instruction: string;
3936
maxSteps?: number;
40-
page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
37+
page?: AnyPage;
4138
highlightCursor?: boolean;
4239
}
4340
export type AgentType = "openai" | "anthropic" | "google";

packages/core/lib/v3/types/public/methods.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { Page as PatchrightPage } from "patchright-core";
2-
import { Page as PlaywrightPage } from "playwright-core";
3-
import { Page as PuppeteerPage } from "puppeteer-core";
41
import { z } from "zod";
52
import type { InferStagehandSchema, StagehandZodSchema } from "../../zodCompat";
6-
import { Page } from "../../understudy/page";
73
import { ModelConfiguration } from "../public/model";
4+
import { AnyPage } from "./page";
85

96
export interface ActOptions {
107
model?: ModelConfiguration;
118
variables?: Record<string, string>;
129
timeout?: number;
13-
page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
10+
page?: AnyPage;
1411
}
1512

1613
export interface ActResult {
@@ -41,7 +38,7 @@ export interface ExtractOptions {
4138
model?: ModelConfiguration;
4239
timeout?: number;
4340
selector?: string;
44-
page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
41+
page?: AnyPage;
4542
}
4643

4744
export const defaultExtractSchema = z.object({
@@ -56,7 +53,7 @@ export interface ObserveOptions {
5653
model?: ModelConfiguration;
5754
timeout?: number;
5855
selector?: string;
59-
page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
56+
page?: AnyPage;
6057
}
6158

6259
export enum V3FunctionName {

packages/core/lib/v3/types/public/page.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
import { z } from "zod";
12
import { Page } from "../../understudy/page";
23
import { Page as PlaywrightPage } from "playwright-core";
34
import { Page as PatchrightPage } from "patchright-core";
45
import { Page as PuppeteerPage } from "puppeteer-core";
56

67
export type { PlaywrightPage, PatchrightPage, PuppeteerPage, Page };
7-
export type AnyPage = PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
8+
9+
export const pageHandleSchema = z.object({
10+
pageId: z.string().min(1),
11+
targetId: z.string().min(1),
12+
});
13+
14+
export interface PageHandle {
15+
pageId: string;
16+
targetId: string;
17+
}
18+
19+
export type AnyPage =
20+
| PlaywrightPage
21+
| PuppeteerPage
22+
| PatchrightPage
23+
| Page
24+
| PageHandle;
825

926
export { ConsoleMessage } from "../../understudy/consoleMessage";
1027
export type { ConsoleListener } from "../../understudy/consoleMessage";

packages/core/lib/v3/understudy/context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export class V3Context {
229229
return targetId ? this.pagesByTarget.get(targetId) : undefined;
230230
}
231231

232+
resolvePageByTargetId(targetId: string): Page | undefined {
233+
return this.pagesByTarget.get(targetId);
234+
}
235+
232236
/**
233237
* Serialize the full frame tree for a given top-level main frame id.
234238
*/

packages/core/lib/v3/v3.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
LocalBrowserLaunchOptions,
5757
V3Options,
5858
AnyPage,
59+
PageHandle,
5960
PatchrightPage,
6061
PlaywrightPage,
6162
PuppeteerPage,
@@ -1389,6 +1390,15 @@ export class V3 {
13891390
);
13901391
}
13911392

1393+
private hasTargetId(p: unknown): p is PageHandle {
1394+
return (
1395+
typeof p === "object" &&
1396+
p !== null &&
1397+
typeof (p as PageHandle).pageId === "string" &&
1398+
typeof (p as PageHandle).targetId === "string"
1399+
);
1400+
}
1401+
13921402
private isPlaywrightPage(p: unknown): p is PlaywrightPage {
13931403
return (
13941404
typeof p === "object" &&
@@ -1413,6 +1423,20 @@ export class V3 {
14131423
);
14141424
}
14151425

1426+
private resolvePageFromHandle(handle: PageHandle): Page {
1427+
const ctx = this.ctx;
1428+
if (!ctx) {
1429+
throw new StagehandNotInitializedError("resolvePage()");
1430+
}
1431+
const page = ctx.resolvePageByTargetId(handle.targetId);
1432+
if (!page) {
1433+
throw new StagehandInitError(
1434+
`Failed to resolve V3 Page from handle targetId=${handle.targetId}.`,
1435+
);
1436+
}
1437+
return page;
1438+
}
1439+
14161440
/** Resolve an external page reference or fall back to the active V3 page. */
14171441
private async resolvePage(page?: AnyPage): Promise<Page> {
14181442
if (page) {
@@ -1426,6 +1450,9 @@ export class V3 {
14261450
}
14271451

14281452
private async normalizeToV3Page(input: AnyPage): Promise<Page> {
1453+
if (this.hasTargetId(input)) {
1454+
return this.resolvePageFromHandle(input);
1455+
}
14291456
if (input instanceof (await import("./understudy/page")).Page) {
14301457
return input as Page;
14311458
}

0 commit comments

Comments
 (0)