Skip to content

Commit f426ba5

Browse files
authored
add pageurl & timestamp to agent actions (#1123)
# why currently, we have no sense of what url an action was taken on and what time it was taken when using agent this is useful to have, because in the dashboard it will allow us to filter the agents actions by url, and display timestamps of the individual actions # what changed - added pageUrl to every action - added timestamp to every action the url is grabbed prior to the action being taken. This is because if we do it after the action is taken, there is a chance the action could have caused a navigation, which would result in the incorrect url for the action # test plan tested locally
1 parent 438f5af commit f426ba5

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

.changeset/upset-ideas-fall.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+
Add pageUrl & timestamp to agent actions

lib/handlers/cuaAgentHandler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export class CuaAgentHandler {
7272

7373
// Set up action handler for any client type
7474
this.agentClient.setActionHandler(async (action) => {
75+
action.pageUrl = this.page.url();
76+
7577
// Default delay between actions (1 second if not specified)
7678
const defaultDelay = 1000;
7779
// Use specified delay or default
@@ -80,6 +82,8 @@ export class CuaAgentHandler {
8082
defaultDelay;
8183

8284
try {
85+
this.updateClientUrl();
86+
8387
// Try to inject cursor before each action if enabled
8488
if (this.highlightCursor) {
8589
try {
@@ -95,6 +99,8 @@ export class CuaAgentHandler {
9599
// Execute the action
96100
await this.executeAction(action);
97101

102+
action.timestamp = Date.now();
103+
98104
// Add a delay after the action for better visibility
99105
await new Promise((resolve) => setTimeout(resolve, waitBetweenActions));
100106

lib/handlers/stagehandAgentHandler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export class StagehandAgentHandler {
5454
let completed = false;
5555
const collectedReasoning: string[] = [];
5656

57+
let currentPageUrl = this.stagehand.page.url();
58+
5759
this.logger({
5860
category: "agent",
5961
message: `Executing agent task: ${options.instruction}`,
@@ -184,16 +186,19 @@ export class StagehandAgentHandler {
184186
const action: AgentAction = {
185187
type: toolCall.toolName,
186188
reasoning: event.text || undefined,
189+
pageUrl: currentPageUrl,
187190
taskCompleted:
188191
toolCall.toolName === "close"
189192
? (args?.success as boolean)
190193
: false,
194+
timestamp: Date.now(),
191195
...args,
192196
...getPlaywrightArguments(),
193197
};
194198

195199
actions.push(action);
196200
}
201+
currentPageUrl = this.stagehand.page.url();
197202
}
198203
},
199204
});

types/agent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface AgentAction {
1717
// Tool-specific fields
1818
timeMs?: number; // wait tool
1919
pageText?: string; // ariaTree tool
20+
timestamp?: number;
2021
pageUrl?: string; // ariaTree tool
2122
instruction?: string; // various tools
2223
playwrightArguments?: ObserveResult | null; // act tool

0 commit comments

Comments
 (0)