Skip to content

Commit 2a27e1c

Browse files
fixed new tabs opened for cua models (#616)
* fixed new tabs opened for cua models * reverted example * cleaned example, added changeset * reverting screenshots to playwright * Update lib/handlers/agentHandler.ts Co-authored-by: Sean McGuire <[email protected]> * cdp screenshot for bb env * add params back --------- Co-authored-by: Sean McGuire <[email protected]>
1 parent 8ec69f1 commit 2a27e1c

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

Diff for: .changeset/giant-tires-sleep.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
Fixed new opened tab handling for CUA models

Diff for: examples/cua-example.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function main() {
2727

2828
const agent = stagehand.agent({
2929
provider: "openai",
30-
model: "computer-use-preview-2025-02-04",
30+
model: "computer-use-preview",
3131
instructions: `You are a helpful assistant that can use a web browser.
3232
You are currently on the following page: ${page.url()}.
3333
Do not ask follow up questions, the user will trust your judgement.`,
@@ -40,7 +40,6 @@ async function main() {
4040
await stagehand.page.goto("https://www.google.com");
4141
console.log(`${chalk.green("✓")} Loaded: ${chalk.dim(page.url())}`);
4242

43-
// Execute the agent again with a different instruction
4443
const firstInstruction =
4544
"Search for openai news on google and extract the name of the first 3 results";
4645
console.log(
@@ -62,7 +61,7 @@ async function main() {
6261
console.log(`${chalk.green("✓")} Loaded: ${chalk.dim(page.url())}`);
6362

6463
const instruction =
65-
"Apply for the full-stack engineer position with mock data. Don't submit the form.";
64+
"Apply for the first engineer position with mock data. Don't submit the form.";
6665
console.log(`${chalk.cyan("↳")} Instruction: ${chalk.white(instruction)}`);
6766

6867
const result = await agent.execute({

Diff for: lib/StagehandPage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export class StagehandPage {
261261
}
262262

263263
// Handle screenshots with CDP
264-
if (prop === "screenshot") {
264+
if (prop === "screenshot" && this.stagehand.env === "BROWSERBASE") {
265265
return async (
266266
options: {
267267
type?: "png" | "jpeg";

Diff for: lib/handlers/agentHandler.ts

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AgentProvider } from "../agent/AgentProvider";
33
import { StagehandAgent } from "../agent/StagehandAgent";
44
import { AgentClient } from "../agent/AgentClient";
55
import { LogLine } from "../../types/log";
6+
import { Page } from "playwright";
67
import {
78
AgentExecuteOptions,
89
AgentAction,
@@ -190,6 +191,28 @@ export class StagehandAgentHandler {
190191
await this.stagehandPage.page.mouse.click(x as number, y as number, {
191192
button: button as "left" | "right",
192193
});
194+
const newOpenedTab = await Promise.race([
195+
new Promise<Page | null>((resolve) => {
196+
this.stagehandPage.context.once("page", (page) => resolve(page));
197+
setTimeout(() => resolve(null), 1500);
198+
}),
199+
]);
200+
if (newOpenedTab) {
201+
this.logger({
202+
category: "action",
203+
message: `New page detected (new tab) with URL. Opening on current page...`,
204+
level: 1,
205+
auxiliary: {
206+
url: {
207+
value: newOpenedTab.url(),
208+
type: "string",
209+
},
210+
},
211+
});
212+
await newOpenedTab.close();
213+
await this.stagehandPage.page.goto(newOpenedTab.url());
214+
await this.stagehandPage.page.waitForURL(newOpenedTab.url());
215+
}
193216
return { success: true };
194217
}
195218

0 commit comments

Comments
 (0)