Skip to content

Commit 3ea4178

Browse files
committed
pass stagehand, instead of stagehandPage to agent
1 parent 7f38b3a commit 3ea4178

File tree

11 files changed

+47
-50
lines changed

11 files changed

+47
-50
lines changed

lib/agent/tools/act.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44
import { buildActObservePrompt } from "../../prompt";
55
import { SupportedPlaywrightAction } from "@/types/act";
6-
export const createActTool = (
7-
stagehandPage: StagehandPage,
8-
executionModel?: string,
9-
) =>
6+
export const createActTool = (stagehand: Stagehand, executionModel?: string) =>
107
tool({
118
description: "Perform an action on the page (click, type)",
129
parameters: z.object({
@@ -34,7 +31,7 @@ export const createActTool = (
3431
instruction: builtPrompt,
3532
};
3633

37-
const observeResults = await stagehandPage.page.observe(observeOptions);
34+
const observeResults = await stagehand.page.observe(observeOptions);
3835

3936
if (!observeResults || observeResults.length === 0) {
4037
return {
@@ -60,7 +57,7 @@ export const createActTool = (
6057
};
6158

6259
const iframeObserveResults =
63-
await stagehandPage.page.observe(iframeObserveOptions);
60+
await stagehand.page.observe(iframeObserveOptions);
6461

6562
if (!iframeObserveResults || iframeObserveResults.length === 0) {
6663
return {
@@ -71,7 +68,7 @@ export const createActTool = (
7168
}
7269

7370
const iframeObserveResult = iframeObserveResults[0];
74-
const fallback = await stagehandPage.page.act(iframeObserveResult);
71+
const fallback = await stagehand.page.act(iframeObserveResult);
7572

7673
return {
7774
success: fallback.success,
@@ -86,7 +83,7 @@ export const createActTool = (
8683
};
8784
}
8885

89-
const result = await stagehandPage.page.act(observeResult);
86+
const result = await stagehand.page.act(observeResult);
9087
const playwrightArguments = {
9188
description: observeResult.description,
9289
method: observeResult.method,

lib/agent/tools/ariaTree.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44

5-
export const createAriaTreeTool = (stagehandPage: StagehandPage) =>
5+
export const createAriaTreeTool = (stagehand: Stagehand) =>
66
tool({
77
description:
88
"gets the accessibility (ARIA) tree from the current page. this is useful for understanding the page structure and accessibility features. it should provide full context of what is on the page",
99
parameters: z.object({}),
1010
execute: async () => {
11-
const { page_text } = await stagehandPage.page.extract();
12-
const pageUrl = stagehandPage.page.url();
11+
const { page_text } = await stagehand.page.extract();
12+
const pageUrl = stagehand.page.url();
1313

1414
let content = page_text;
1515
const MAX_CHARACTERS = 70000;

lib/agent/tools/extract.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44
import { LogLine } from "@/types/log";
55

66
/**
@@ -33,7 +33,7 @@ function evaluateZodSchema(
3333
}
3434

3535
export const createExtractTool = (
36-
stagehandPage: StagehandPage,
36+
stagehand: Stagehand,
3737
executionModel?: string,
3838
logger?: (message: LogLine) => void,
3939
) =>
@@ -80,7 +80,7 @@ export const createExtractTool = (
8080
: z.object({ result: zodSchema });
8181

8282
// Extract with the schema - only pass modelName if executionModel is explicitly provided
83-
const result = await stagehandPage.page.extract({
83+
const result = await stagehand.page.extract({
8484
instruction,
8585
schema: schemaObject,
8686
...(executionModel && { modelName: executionModel }),

lib/agent/tools/fillform.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44

55
export const createFillFormTool = (
6-
stagehandPage: StagehandPage,
6+
stagehand: Stagehand,
77
executionModel?: string,
88
) =>
99
tool({
@@ -54,15 +54,15 @@ export const createFillFormTool = (
5454
.join(", ")}`;
5555

5656
const observeResults = executionModel
57-
? await stagehandPage.page.observe({
57+
? await stagehand.page.observe({
5858
instruction,
5959
modelName: executionModel,
6060
})
61-
: await stagehandPage.page.observe(instruction);
61+
: await stagehand.page.observe(instruction);
6262

6363
const completedActions = [];
6464
for (const result of observeResults) {
65-
const action = await stagehandPage.page.act(result);
65+
const action = await stagehand.page.act(result);
6666
completedActions.push(action);
6767
}
6868

lib/agent/tools/goto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44

5-
export const createGotoTool = (stagehandPage: StagehandPage) =>
5+
export const createGotoTool = (stagehand: Stagehand) =>
66
tool({
77
description: "Navigate to a specific URL",
88
parameters: z.object({
99
url: z.string().describe("The URL to navigate to"),
1010
}),
1111
execute: async ({ url }) => {
1212
try {
13-
await stagehandPage.page.goto(url, { waitUntil: "load" });
13+
await stagehand.page.goto(url, { waitUntil: "load" });
1414
return { success: true, url };
1515
} catch (error) {
1616
return { success: false, error: error.message };

lib/agent/tools/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createCloseTool } from "./close";
77
import { createAriaTreeTool } from "./ariaTree";
88
import { createFillFormTool } from "./fillform";
99
import { createScrollTool } from "./scroll";
10-
import { StagehandPage } from "../../StagehandPage";
10+
import { Stagehand } from "../../index";
1111
import { LogLine } from "@/types/log";
1212
import { createExtractTool } from "./extract";
1313

@@ -17,21 +17,21 @@ export interface AgentToolOptions {
1717
}
1818

1919
export function createAgentTools(
20-
stagehandPage: StagehandPage,
20+
stagehand: Stagehand,
2121
options?: AgentToolOptions,
2222
) {
2323
const executionModel = options?.executionModel;
2424

2525
return {
26-
act: createActTool(stagehandPage, executionModel),
27-
ariaTree: createAriaTreeTool(stagehandPage),
26+
act: createActTool(stagehand, executionModel),
27+
ariaTree: createAriaTreeTool(stagehand),
2828
close: createCloseTool(),
29-
extract: createExtractTool(stagehandPage, executionModel, options?.logger),
30-
fillForm: createFillFormTool(stagehandPage, executionModel),
31-
goto: createGotoTool(stagehandPage),
32-
navback: createNavBackTool(stagehandPage),
33-
screenshot: createScreenshotTool(stagehandPage),
34-
scroll: createScrollTool(stagehandPage),
29+
extract: createExtractTool(stagehand, executionModel, options?.logger),
30+
fillForm: createFillFormTool(stagehand, executionModel),
31+
goto: createGotoTool(stagehand),
32+
navback: createNavBackTool(stagehand),
33+
screenshot: createScreenshotTool(stagehand),
34+
scroll: createScrollTool(stagehand),
3535
wait: createWaitTool(),
3636
};
3737
}

lib/agent/tools/navback.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44

5-
export const createNavBackTool = (stagehandPage: StagehandPage) =>
5+
export const createNavBackTool = (stagehand: Stagehand) =>
66
tool({
77
description: "Navigate back to the previous page",
88
parameters: z.object({
99
reasoning: z.string().describe("Why you're going back"),
1010
}),
1111
execute: async () => {
12-
await stagehandPage.page.goBack();
12+
await stagehand.page.goBack();
1313
return { success: true };
1414
},
1515
});

lib/agent/tools/screenshot.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44

5-
export const createScreenshotTool = (stagehandPage: StagehandPage) =>
5+
export const createScreenshotTool = (stagehand: Stagehand) =>
66
tool({
77
description:
88
"Takes a screenshot of the current page. Use this tool to learn where you are on the page, or to get context of elements on the page",
99
parameters: z.object({}),
1010
execute: async () => {
11-
const screenshotBuffer = await stagehandPage.page.screenshot({
11+
const screenshotBuffer = await stagehand.page.screenshot({
1212
fullPage: false,
1313
type: "jpeg",
1414
});
15-
const pageUrl = stagehandPage.page.url();
15+
const pageUrl = stagehand.page.url();
1616

1717
return {
1818
base64: screenshotBuffer.toString("base64"),

lib/agent/tools/scroll.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { tool } from "ai";
22
import { z } from "zod/v3";
3-
import { StagehandPage } from "../../StagehandPage";
3+
import { Stagehand } from "../../index";
44

5-
export const createScrollTool = (stagehandPage: StagehandPage) =>
5+
export const createScrollTool = (stagehand: Stagehand) =>
66
tool({
77
description: "Scroll the page",
88
parameters: z.object({
99
pixels: z.number().describe("Number of pixels to scroll up or down"),
1010
direction: z.enum(["up", "down"]).describe("Direction to scroll"),
1111
}),
1212
execute: async ({ pixels, direction }) => {
13-
await stagehandPage.page.mouse.wheel(
13+
await stagehand.page.mouse.wheel(
1414
0,
1515
direction === "up" ? -pixels : pixels,
1616
);

lib/handlers/stagehandAgentHandler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@ import {
55
ActToolResult,
66
} from "@/types/agent";
77
import { LogLine } from "@/types/log";
8-
import { StagehandPage } from "../StagehandPage";
98
import { LLMClient } from "../llm/LLMClient";
109
import { CoreMessage, wrapLanguageModel } from "ai";
1110
import { LanguageModel } from "ai";
1211
import { processMessages } from "../agent/utils/messageProcessing";
1312
import { createAgentTools } from "../agent/tools";
1413
import { ToolSet } from "ai";
14+
import { Stagehand } from "../index";
1515

1616
export class StagehandAgentHandler {
17-
private stagehandPage: StagehandPage;
17+
private stagehand: Stagehand;
1818
private logger: (message: LogLine) => void;
1919
private llmClient: LLMClient;
2020
private executionModel?: string;
2121
private systemInstructions?: string;
2222
private mcpTools?: ToolSet;
2323

2424
constructor(
25-
stagehandPage: StagehandPage,
25+
stagehand: Stagehand,
2626
logger: (message: LogLine) => void,
2727
llmClient: LLMClient,
2828
executionModel?: string,
2929
systemInstructions?: string,
3030
mcpTools?: ToolSet,
3131
) {
32-
this.stagehandPage = stagehandPage;
32+
this.stagehand = stagehand;
3333
this.logger = logger;
3434
this.llmClient = llmClient;
3535
this.executionModel = executionModel;
@@ -239,7 +239,7 @@ For each action, provide clear reasoning about why you're taking that step.`;
239239
}
240240

241241
private createTools() {
242-
return createAgentTools(this.stagehandPage, {
242+
return createAgentTools(this.stagehand, {
243243
executionModel: this.executionModel,
244244
logger: this.logger,
245245
});

0 commit comments

Comments
 (0)