Skip to content

Commit 596691a

Browse files
committed
Removed unnecessary pagehandle
1 parent d7bde37 commit 596691a

File tree

9 files changed

+39
-127
lines changed

9 files changed

+39
-127
lines changed

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
import { z } from "zod";
2-
import { Page } from "../../understudy/page";
3-
import { Page as PlaywrightPage } from "playwright-core";
41
import { Page as PatchrightPage } from "patchright-core";
2+
import { Page as PlaywrightPage } from "playwright-core";
53
import { Page as PuppeteerPage } from "puppeteer-core";
4+
import { Page } from "../../understudy/page";
65

76
export type { PlaywrightPage, PatchrightPage, PuppeteerPage, Page };
87

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;
8+
export type AnyPage = PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
259

2610
export { ConsoleMessage } from "../../understudy/consoleMessage";
2711
export type { ConsoleListener } from "../../understudy/consoleMessage";

packages/core/lib/v3/v3.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import {
5656
LocalBrowserLaunchOptions,
5757
V3Options,
5858
AnyPage,
59-
PageHandle,
6059
PatchrightPage,
6160
PlaywrightPage,
6261
PuppeteerPage,
@@ -1390,15 +1389,6 @@ export class V3 {
13901389
);
13911390
}
13921391

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-
14021392
private isPlaywrightPage(p: unknown): p is PlaywrightPage {
14031393
return (
14041394
typeof p === "object" &&
@@ -1423,20 +1413,6 @@ export class V3 {
14231413
);
14241414
}
14251415

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-
14401416
/** Resolve an external page reference or fall back to the active V3 page. */
14411417
private async resolvePage(page?: AnyPage): Promise<Page> {
14421418
if (page) {
@@ -1450,9 +1426,6 @@ export class V3 {
14501426
}
14511427

14521428
private async normalizeToV3Page(input: AnyPage): Promise<Page> {
1453-
if (this.hasTargetId(input)) {
1454-
return this.resolvePageFromHandle(input);
1455-
}
14561429
if (input instanceof (await import("./understudy/page")).Page) {
14571430
return input as Page;
14581431
}

packages/core/tests/page-boundary.test.ts

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,6 @@ function stubAgentCache(instance: Stagehand): void {
8383
});
8484
}
8585

86-
function installHandleContext(
87-
instance: Stagehand,
88-
targetId: string,
89-
page: Page,
90-
) {
91-
const resolvePageByTargetId = vi
92-
.fn()
93-
.mockImplementation((requestedTargetId: string) => {
94-
if (requestedTargetId === targetId) {
95-
return page;
96-
}
97-
return undefined;
98-
});
99-
Reflect.set(instance, "ctx", { resolvePageByTargetId });
100-
return resolvePageByTargetId;
101-
}
102-
10386
function createApiClient() {
10487
return {
10588
act: vi.fn().mockResolvedValue(defaultActResult),
@@ -143,29 +126,6 @@ describe("Page boundary contracts", () => {
143126
resolvePage.call(stagehand, {} as AnyPage),
144127
).rejects.toBeInstanceOf(StagehandInvalidArgumentError);
145128
});
146-
147-
it("maps PageHandle.targetId back to a V3 Page", async () => {
148-
const stagehand = createStagehand();
149-
const fakePage = createInternalPage("frame-handle");
150-
const resolvePageByTargetId = installHandleContext(
151-
stagehand,
152-
"target-handle",
153-
fakePage,
154-
);
155-
156-
const resolvePage = Reflect.get(stagehand, "resolvePage") as (
157-
page?: AnyPage,
158-
) => Promise<Page>;
159-
160-
const handle = {
161-
pageId: "page-handle",
162-
targetId: "target-handle",
163-
} as AnyPage;
164-
const resolved = await resolvePage.call(stagehand, handle);
165-
166-
expect(resolvePageByTargetId).toHaveBeenCalledWith("target-handle");
167-
expect(resolved).toBe(fakePage);
168-
});
169129
});
170130

171131
describe("normalizeToV3Page", () => {
@@ -263,16 +223,17 @@ describe("Page boundary contracts", () => {
263223
stubAgentCache(stagehand);
264224

265225
const frameId = "frame-act";
266-
const targetId = "target-act";
267226
const fakePage = createInternalPage(frameId);
268-
installHandleContext(stagehand, targetId, fakePage);
227+
Reflect.set(
228+
stagehand,
229+
"resolvePage",
230+
vi.fn().mockResolvedValue(fakePage),
231+
);
269232

270233
const apiClient = createApiClient();
271234
Reflect.set(stagehand, "apiClient", apiClient);
272235

273-
const options = {
274-
page: { pageId: "page-act", targetId } as AnyPage,
275-
};
236+
const options = {};
276237

277238
await stagehand.act("Click button", options);
278239

@@ -289,17 +250,18 @@ describe("Page boundary contracts", () => {
289250
stubAgentCache(stagehand);
290251

291252
const frameId = "frame-extract";
292-
const targetId = "target-extract";
293253
const fakePage = createInternalPage(frameId);
294-
installHandleContext(stagehand, targetId, fakePage);
254+
Reflect.set(
255+
stagehand,
256+
"resolvePage",
257+
vi.fn().mockResolvedValue(fakePage),
258+
);
295259

296260
const apiClient = createApiClient();
297261
Reflect.set(stagehand, "apiClient", apiClient);
298262

299263
const schema = z.object({ value: z.string() });
300-
const options = {
301-
page: { pageId: "page-extract", targetId } as AnyPage,
302-
};
264+
const options = {};
303265

304266
await stagehand.extract("Summarize", schema, options);
305267

@@ -317,16 +279,17 @@ describe("Page boundary contracts", () => {
317279
stubAgentCache(stagehand);
318280

319281
const frameId = "frame-observe";
320-
const targetId = "target-observe";
321282
const fakePage = createInternalPage(frameId);
322-
installHandleContext(stagehand, targetId, fakePage);
283+
Reflect.set(
284+
stagehand,
285+
"resolvePage",
286+
vi.fn().mockResolvedValue(fakePage),
287+
);
323288

324289
const apiClient = createApiClient();
325290
Reflect.set(stagehand, "apiClient", apiClient);
326291

327-
const options = {
328-
page: { pageId: "page-observe", targetId } as AnyPage,
329-
};
292+
const options = {};
330293

331294
await stagehand.observe("Check", options);
332295

packages/core/tests/public-api/public-types.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ describe("Stagehand public API types", () => {
9191
| Stagehand.PlaywrightPage
9292
| Stagehand.PuppeteerPage
9393
| Stagehand.PatchrightPage
94-
| Stagehand.Page
95-
| Stagehand.PageHandle;
94+
| Stagehand.Page;
9695

9796
it("matches expected type shape", () => {
9897
expectTypeOf<Stagehand.AnyPage>().toEqualTypeOf<ExpectedAnyPage>();

packages/sdk/src/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export const pageTextSchema = createSchemaStub({
4848
});
4949

5050
export function getZodType(schema: StagehandZodSchema): string {
51-
return (schema as unknown as { _def?: { typeName?: string } })?._def?.typeName ?? "unknown";
51+
return (
52+
(schema as unknown as { _def?: { typeName?: string } })?._def?.typeName ??
53+
"unknown"
54+
);
5255
}
5356

5457
export function transformSchema(

packages/sdk/src/runtime.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import type {
3737
ChatCompletionOptions,
3838
CreateChatCompletionOptions,
3939
LLMResponse,
40-
LLMUsage, AgentClient,
40+
LLMUsage,
41+
AgentClient,
4142
} from "./types";
4243
import {
4344
AnnotatedScreenshotText,
@@ -276,7 +277,10 @@ export class V3Evaluator {
276277
}
277278

278279
batchAsk(
279-
_instructions: Array<{ instruction: string; options?: Record<string, unknown> }>,
280+
_instructions: Array<{
281+
instruction: string;
282+
options?: Record<string, unknown>;
283+
}>,
280284
): Promise<unknown> {
281285
return rejectNotImplemented("V3Evaluator.batchAsk");
282286
}
@@ -287,11 +291,7 @@ export class V3Evaluator {
287291
}
288292

289293
export async function connectToMCPServer(
290-
_serverConfig:
291-
| string
292-
| URL
293-
| StdioServerConfig
294-
| ConnectToMCPServerOptions,
294+
_serverConfig: string | URL | StdioServerConfig | ConnectToMCPServerOptions,
295295
): Promise<MCPClient> {
296296
return rejectNotImplemented<MCPClient>("connectToMCPServer");
297297
}

packages/sdk/src/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,7 @@ export interface V3Options {
243243
browserbaseSessionCreateParams?: Record<string, unknown>;
244244
}
245245

246-
export interface PageHandle {
247-
targetId?: string;
248-
sessionId?: string;
249-
}
250-
251246
export type AnyPage =
252-
| PageHandle
253247
| {
254248
kind?: string;
255249
reference?: unknown;

tests/shared/exportSurfaceSuite.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { describe, expect, it } from "vitest";
22
import { buildPublicApiShape } from "./publicApiManifest";
33

4-
export function runExportSurfaceSuite<
5-
M extends Record<string, unknown>,
6-
D,
7-
>(
4+
export function runExportSurfaceSuite<M extends Record<string, unknown>, D>(
85
label: string,
96
moduleExports: M,
107
defaultExport: D,
@@ -13,9 +10,7 @@ export function runExportSurfaceSuite<
1310
const publicApiShape = buildPublicApiShape(moduleExports, defaultExport);
1411

1512
type PublicAPI = {
16-
[K in keyof typeof publicApiShape]: K extends "default"
17-
? D
18-
: M[K];
13+
[K in keyof typeof publicApiShape]: K extends "default" ? D : M[K];
1914
};
2015

2116
it("public API shape matches module exports", () => {

tests/shared/publicApiManifest.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ export const PUBLIC_API_EXPORT_KEYS = [
3636
export type PublicApiExportKey = (typeof PUBLIC_API_EXPORT_KEYS)[number];
3737

3838
export type PublicApiShape<M extends Record<string, unknown>, D> = {
39-
[K in PublicApiExportKey | PublicErrorTypeKey | "default"]: K extends "default"
40-
? D
41-
: M[K];
39+
[K in
40+
| PublicApiExportKey
41+
| PublicErrorTypeKey
42+
| "default"]: K extends "default" ? D : M[K];
4243
};
4344

4445
export function buildPublicApiShape<M extends Record<string, unknown>, D>(

0 commit comments

Comments
 (0)