Skip to content

Commit a1ad06c

Browse files
authored
Additional agent logging (#1104)
# why To inform the user throughout the agent execution process # what changed Added logs to tool calls, and on the stagehand agent handler # test plan - [x] tested locally
1 parent a99aa48 commit a1ad06c

File tree

12 files changed

+143
-32
lines changed

12 files changed

+143
-32
lines changed

.changeset/fifty-windows-throw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
Fix logging for stagehand agent

lib/agent/tools/act.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,27 @@ export const createActTool = (stagehand: Stagehand, executionModel?: string) =>
88
description: "Perform an action on the page (click, type)",
99
parameters: z.object({
1010
action: z.string()
11-
.describe(`Describe what to click, or type within in a short, specific phrase that mentions the element type.
11+
.describe(`Describe the click, type, fill, scroll action within in a short, specific phrase that mentions the element type.
1212
Examples:
1313
- "click the Login button"
1414
- "click the language dropdown"
1515
- type "John" into the first name input
16-
- type "Doe" into the last name input`),
16+
- type "Doe" into the last name input.
17+
When attempting to fill a field you can say 'fill the field x with the value y'.`),
1718
}),
1819
execute: async ({ action }) => {
1920
try {
21+
stagehand.logger({
22+
category: "agent",
23+
message: `Agent calling tool: act`,
24+
level: 1,
25+
auxiliary: {
26+
arguments: {
27+
value: action,
28+
type: "string",
29+
},
30+
},
31+
});
2032
const builtPrompt = buildActObservePrompt(
2133
action,
2234
Object.values(SupportedPlaywrightAction),

lib/agent/tools/ariaTree.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export const createAriaTreeTool = (stagehand: Stagehand) =>
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+
stagehand.logger({
12+
category: "agent",
13+
message: `Agent calling tool: ariaTree`,
14+
level: 1,
15+
});
1116
const { page_text } = await stagehand.page.extract();
1217
const pageUrl = stagehand.page.url();
1318

lib/agent/tools/close.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export const createCloseTool = () =>
66
description: "Complete the task and close",
77
parameters: z.object({
88
reasoning: z.string().describe("Summary of what was accomplished"),
9-
taskComplete: z
9+
success: z
1010
.boolean()
11-
.describe("Whether the task was completed successfully"),
11+
.describe("Whether the full goal of the task was a success or not"),
1212
}),
13-
execute: async ({ reasoning, taskComplete }) => {
14-
return { success: true, reasoning, taskComplete };
13+
execute: async ({ reasoning, success }) => {
14+
return { success, reasoning };
1515
},
1616
});

lib/agent/tools/extract.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ export const createExtractTool = (
7070
}),
7171
execute: async ({ instruction, schema }) => {
7272
try {
73+
stagehand.logger({
74+
category: "agent",
75+
message: `Agent calling tool: extract`,
76+
level: 1,
77+
auxiliary: {
78+
arguments: {
79+
value: instruction,
80+
type: "string",
81+
},
82+
// TODO: check if we want to log this
83+
schema: {
84+
value: schema,
85+
type: "object",
86+
},
87+
},
88+
});
7389
// Evaluate the schema string to get the actual Zod schema
7490
const zodSchema = evaluateZodSchema(schema, logger);
7591

lib/agent/tools/fillform.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ export const createFillFormTool = (
4949
}),
5050

5151
execute: async ({ fields }) => {
52+
stagehand.logger({
53+
category: "agent",
54+
message: `Agent calling tool: fillForm`,
55+
level: 1,
56+
auxiliary: {
57+
arguments: {
58+
value: JSON.stringify(fields),
59+
type: "object",
60+
},
61+
},
62+
});
5263
const instruction = `Return observation results for the following actions: ${fields
5364
.map((field) => field.action)
5465
.join(", ")}`;

lib/agent/tools/goto.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ export const createGotoTool = (stagehand: Stagehand) =>
1010
}),
1111
execute: async ({ url }) => {
1212
try {
13+
stagehand.logger({
14+
category: "agent",
15+
message: `Agent calling tool: goto`,
16+
level: 1,
17+
auxiliary: {
18+
arguments: {
19+
value: url,
20+
type: "string",
21+
},
22+
},
23+
});
1324
await stagehand.page.goto(url, { waitUntil: "load" });
1425
return { success: true, url };
1526
} catch (error) {

lib/agent/tools/navback.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export const createNavBackTool = (stagehand: Stagehand) =>
99
reasoning: z.string().describe("Why you're going back"),
1010
}),
1111
execute: async () => {
12+
stagehand.logger({
13+
category: "agent",
14+
message: `Agent calling tool: navback`,
15+
level: 1,
16+
});
1217
await stagehand.page.goBack();
1318
return { success: true };
1419
},

lib/agent/tools/screenshot.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export const createScreenshotTool = (stagehand: Stagehand) =>
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+
stagehand.logger({
12+
category: "agent",
13+
message: `Agent calling tool: screenshot`,
14+
level: 1,
15+
});
1116
const screenshotBuffer = await stagehand.page.screenshot({
1217
fullPage: false,
1318
type: "jpeg",

lib/agent/tools/scroll.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ export const createScrollTool = (stagehand: Stagehand) =>
1010
direction: z.enum(["up", "down"]).describe("Direction to scroll"),
1111
}),
1212
execute: async ({ pixels, direction }) => {
13+
stagehand.logger({
14+
category: "agent",
15+
message: `Agent calling tool: scroll`,
16+
level: 1,
17+
auxiliary: {
18+
arguments: {
19+
value: JSON.stringify({ pixels, direction }),
20+
type: "object",
21+
},
22+
},
23+
});
1324
await stagehand.page.mouse.wheel(
1425
0,
1526
direction === "up" ? -pixels : pixels,

0 commit comments

Comments
 (0)