Skip to content

Commit a8811fe

Browse files
need to add timeoutMs to Act in local mode
1 parent f5366ff commit a8811fe

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

stagehand/handlers/act_handler.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import traceback
2+
import asyncio
23
from typing import Any, Optional, Union
34

45
from stagehand.handlers.act_handler_utils import (
@@ -46,6 +47,29 @@ async def act(self, options: Union[ActOptions, ObserveResult]) -> ActResult:
4647
options, self.stagehand.dom_settle_timeout_ms
4748
)
4849

50+
# Extract timeout_ms from options (check both snake_case and camelCase)
51+
timeout_ms = options.get("timeout_ms") or options.get("timeoutMs")
52+
53+
# If timeout is specified, wrap the entire act operation with asyncio.wait_for
54+
if timeout_ms:
55+
try:
56+
return await asyncio.wait_for(
57+
self._perform_act_with_timeout(options),
58+
timeout=timeout_ms / 1000.0 # Convert ms to seconds
59+
)
60+
except asyncio.TimeoutError:
61+
action_task = options.get("action")
62+
return ActResult(
63+
success=False,
64+
message=f"Action timed out after {timeout_ms}ms",
65+
action=action_task,
66+
)
67+
else:
68+
# No timeout specified, use existing behavior
69+
return await self._perform_act_with_timeout(options)
70+
71+
async def _perform_act_with_timeout(self, options) -> ActResult:
72+
"""Extract the main act logic into a separate method for timeout handling"""
4973
# Start inference timer if available
5074
if hasattr(self.stagehand, "start_inference_timer"):
5175
self.stagehand.start_inference_timer()

0 commit comments

Comments
 (0)