Skip to content

Commit 499a72d

Browse files
Add a timeout to dom based act (#556)
* pls * pls * changeset * act evals * example * Update lib/StagehandPage.ts Co-authored-by: Sean McGuire <[email protected]> --------- Co-authored-by: Sean McGuire <[email protected]>
1 parent 5f1868b commit 499a72d

12 files changed

+109
-402
lines changed

Diff for: .changeset/small-zebras-jog.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": minor
3+
---
4+
5+
You can now set a timeout for dom-based stagehand act!

Diff for: .changeset/wild-hats-turn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@browserbasehq/stagehand": minor
33
---
44

5-
`act()` can now use `observe()` under the hood, resulting in significant performance improvements. To opt-in to this change, set `slowDomBasedAct: true` in `ActOptions`.
5+
`act()` can now use `observe()` under the hood, resulting in significant performance improvements. To opt-in to this change, set `slowDomBasedAct: false` in `ActOptions`.

Diff for: evals/evals.config.json

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"tasks": [
3+
{
4+
"name": "expect_act_timeout",
5+
"categories": ["act"]
6+
},
7+
{
8+
"name": "expect_act_timeout_global",
9+
"categories": ["act"]
10+
},
311
{
412
"name": "extract_repo_name",
513
"categories": ["extract"]

Diff for: evals/initStagehand.ts

+3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ export const initStagehand = async ({
5555
domSettleTimeoutMs,
5656
logger,
5757
configOverrides,
58+
actTimeoutMs,
5859
}: {
5960
modelName: AvailableModel;
6061
domSettleTimeoutMs?: number;
6162
logger: EvalLogger;
6263
configOverrides?: Partial<ConstructorParams>;
64+
actTimeoutMs?: number;
6365
}) => {
6466
let chosenApiKey: string | undefined = process.env.OPENAI_API_KEY;
6567
if (modelName.startsWith("claude")) {
@@ -73,6 +75,7 @@ export const initStagehand = async ({
7375
modelClientOptions: {
7476
apiKey: chosenApiKey,
7577
},
78+
actTimeoutMs,
7679
logger: (logLine: LogLine) => {
7780
logger.log(logLine);
7881
},

Diff for: evals/tasks/expect_act_timeout.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { initStagehand } from "@/evals/initStagehand";
2+
import { EvalFunction } from "@/types/evals";
3+
4+
export const expect_act_timeout: EvalFunction = async ({
5+
modelName,
6+
logger,
7+
}) => {
8+
const { stagehand, initResponse } = await initStagehand({
9+
modelName,
10+
logger,
11+
});
12+
13+
const { debugUrl, sessionUrl } = initResponse;
14+
15+
await stagehand.page.goto("https://docs.stagehand.dev");
16+
const result = await stagehand.page.act({
17+
action: "search for 'Stagehand'",
18+
timeoutMs: 1_000,
19+
slowDomBasedAct: true,
20+
});
21+
22+
await stagehand.close();
23+
24+
return {
25+
_success: !result.success,
26+
debugUrl,
27+
sessionUrl,
28+
logs: logger.getLogs(),
29+
};
30+
};

Diff for: evals/tasks/expect_act_timeout_global.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { initStagehand } from "@/evals/initStagehand";
2+
import { EvalFunction } from "@/types/evals";
3+
4+
export const expect_act_timeout_global: EvalFunction = async ({
5+
modelName,
6+
logger,
7+
}) => {
8+
const { stagehand, initResponse } = await initStagehand({
9+
modelName,
10+
logger,
11+
actTimeoutMs: 1_000,
12+
});
13+
14+
const { debugUrl, sessionUrl } = initResponse;
15+
16+
await stagehand.page.goto("https://docs.stagehand.dev");
17+
18+
const result = await stagehand.page.act({
19+
action: "search for 'Stagehand'",
20+
slowDomBasedAct: true,
21+
});
22+
console.log("RESULT", result);
23+
24+
await stagehand.close();
25+
26+
return {
27+
_success: !result.success,
28+
debugUrl,
29+
sessionUrl,
30+
logs: logger.getLogs(),
31+
};
32+
};

Diff for: examples/example.ts

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ async function example() {
1414
});
1515
await stagehand.init();
1616
await stagehand.page.goto("https://docs.stagehand.dev");
17+
/**
18+
* Add your code here!
19+
*/
20+
await stagehand.close();
1721
}
1822

1923
(async () => {

Diff for: lib/StagehandPage.ts

+2
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ export class StagehandPage {
488488
variables = {},
489489
domSettleTimeoutMs,
490490
slowDomBasedAct = true,
491+
timeoutMs = this.stagehand.actTimeoutMs,
491492
} = actionOrOptions;
492493

493494
if (typeof useVision !== "undefined") {
@@ -545,6 +546,7 @@ export class StagehandPage {
545546
previousSelectors: [],
546547
skipActionCacheForThisStep: false,
547548
domSettleTimeoutMs,
549+
timeoutMs,
548550
})
549551
.catch((e) => {
550552
this.stagehand.log({

0 commit comments

Comments
 (0)