Skip to content

Commit 963c999

Browse files
committed
better final step, pass loop backend init if needed, click timeouts
1 parent 8be928a commit 963c999

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/agentlab/backends/browser/env.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14+
def final_step():
15+
"""
16+
Finish the task execution.
17+
"""
18+
pass
19+
1420
class BrowserEnv(AbstractEnv):
1521
def __init__(
1622
self, task_name: str, task: AbstractWebTask, backend: BrowserBackend, seed: int = 0
@@ -117,6 +123,8 @@ def actions(self) -> list[ToolSpec]:
117123
logger.info(
118124
f"Filtered {len(filtered_actions)} actions out of {len(all_actions)} for task {self.task.dataset}"
119125
)
126+
final_step_action = ToolSpec.from_function(final_step)
127+
filtered_actions.append(final_step_action)
120128
return filtered_actions
121129

122130

src/agentlab/backends/browser/playwright.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def model_post_init(self, __context: Any):
2828
"browser_mouse_click_xy": self.browser_mouse_click_xy,
2929
}
3030

31-
def initialize(self):
32-
self._loop = asyncio.get_event_loop()
31+
def initialize(self, loop: asyncio.AbstractEventLoop | None = None):
32+
self._loop = loop or asyncio.get_event_loop()
3333
self._loop.run_until_complete(self.ainitialize())
3434

3535
async def ainitialize(self):
@@ -53,7 +53,7 @@ async def browser_click(self, selector: str):
5353
"""
5454
Click on a selector.
5555
"""
56-
await self._page.click(selector)
56+
await self._page.click(selector, timeout=3000, strict=True)
5757

5858
async def browser_drag(self, from_selector: str, to_selector: str):
5959
"""
@@ -71,7 +71,7 @@ async def browser_hover(self, selector: str):
7171
"""
7272
Hover over a given element.
7373
"""
74-
await self._page.hover(selector)
74+
await self._page.hover(selector, timeout=3000, strict=True)
7575

7676
async def browser_select_option(self, selector: str, value: str):
7777
"""
@@ -105,7 +105,11 @@ def page_axtree(self):
105105

106106
def step(self, action: ToolCallAction):
107107
fn = self._actions[action.function.name]
108-
action_result = self._loop.run_until_complete(fn(**action.function.arguments))
108+
try:
109+
action_result = self._loop.run_until_complete(fn(**action.function.arguments))
110+
except Exception as e:
111+
logger.error(f"Error executing action {action.function.name}: {e}")
112+
action_result = f"Error executing action {action.function.name}: {e}"
109113
html = self.page_html()
110114
screenshot = self.page_screenshot()
111115
axtree = self.page_axtree()

0 commit comments

Comments
 (0)