Skip to content

Commit 8be56ce

Browse files
committed
fixes, use firefox
1 parent 630569a commit 8be56ce

File tree

6 files changed

+64
-10
lines changed

6 files changed

+64
-10
lines changed

src/agentlab/actions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from bgym import AbstractActionSet
2+
from tapeagents.tool_calling import FunctionCall, ToolCallAction, ToolSpec
3+
4+
from agentlab.llm.llm_utils import parse_html_tags_raise
5+
6+
7+
class ToolsActionSet(AbstractActionSet):
8+
def __init__(self, actions:list[ToolSpec]):
9+
self.actions = actions
10+
11+
def describe(self, with_long_description: bool = True, with_examples: bool = True) -> str:
12+
tools_description = "\n".join([action.description() for action in self.actions])
13+
return tools_description
14+
15+
def example_action(self, abstract: bool) -> str:
16+
if abstract:
17+
return """<action>
18+
{
19+
"name": "<action_name>",
20+
"arguments": {
21+
"<argument_name_1>": "<argument_value_1>",
22+
"<argument_name_2>": "<argument_value_2>",
23+
...
24+
}
25+
}
26+
</action>
27+
"""
28+
else:
29+
return """<action>
30+
{
31+
"name": "browser_navigate",
32+
"arguments": {
33+
"url": "https://www.google.com"
34+
}
35+
}
36+
</action>
37+
"""
38+
@classmethod
39+
def parse_action(cls, llm_output: str) -> ToolCallAction:
40+
content_dict, valid, retry_message = parse_html_tags_raise(llm_output, keys=["action"])
41+
if not valid or "action" not in content_dict:
42+
raise ValueError(f"Invalid action: llm_output: {llm_output}, retry_message: {retry_message}")
43+
action_str = content_dict["action"]
44+
return ToolCallAction(function=FunctionCall(name=action_str["name"], arguments=action_str["arguments"]))
45+
46+
def to_python_code(self, action) -> str:
47+
return action.model_dump_json(indent=2)

src/agentlab/agents/tapeagent/agent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def load_config(config_name: str) -> DictConfig:
4141
class TapeAgentArgs(AgentArgs):
4242
config: DictConfig = None # type: ignore
4343

44-
def make_agent(self, known_actions: tuple[ToolSpec, ...] | None) -> bgym.Agent:
45-
if known_actions is None:
44+
def make_agent(self, actions: tuple[ToolSpec, ...] | None) -> bgym.Agent:
45+
if actions is None:
4646
agent = hydra.utils.instantiate(self.config.agent)
4747
else:
48-
tools_description = "\n".join([action.description() for action in known_actions])
49-
agent = hydra.utils.instantiate(self.config.agent, known_actions=known_actions, tools_description=tools_description)
48+
tools_description = "\n".join([action.description() for action in actions])
49+
agent = hydra.utils.instantiate(self.config.agent, known_actions=actions, tools_description=tools_description)
5050
return TapeAgent(agent=agent)
5151

5252

src/agentlab/backends/browser/env.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from tapeagents.core import Action, Observation, StopStep
88
from tapeagents.tool_calling import ToolCallAction, ToolSpec
99

10+
from agentlab.actions import ToolsActionSet
1011
from agentlab.backends.browser.base import BrowserBackend
1112
from agentlab.benchmarks.abstract_env import AbstractEnv, AbstractEnvArgs
1213
from agentlab.benchmarks.miniwob.task import AbstractWebTask
@@ -41,7 +42,9 @@ def reset(self, seed: int):
4142
logger.info(f"Task reset result: {js_result_str}")
4243
return [GoalObservation(goal=js_result_str), PageObservation(content=page_content)], {}
4344

44-
def step(self, action: ToolCallAction) -> tuple[Observation, float, bool, bool, dict]:
45+
def step(self, action: ToolCallAction | str) -> tuple[Observation, float, bool, bool, dict]:
46+
if isinstance(action, str):
47+
action = ToolsActionSet.parse_action(action)
4548
logger.info(f"BrowserEnv.step() called with action {action.function.name}")
4649

4750
action_exec_start = time.time()

src/agentlab/backends/browser/mcp_playwright.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"args": [
66
"@playwright/mcp@latest",
77
"--browser",
8-
"chromium",
8+
"firefox",
99
"--headless",
1010
"--isolated"
1111
],

src/agentlab/backends/browser/mcp_playwright.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ class MCPPlaywright(MCPBrowserBackend):
1414

1515
def run_js(self, js: str):
1616
raw_response = self.call_tool("browser_evaluate", {"function": js})
17-
_, half_response = raw_response.split("### Result", maxsplit=1)
18-
result_str, _ = half_response.split("\n### Ran", maxsplit=1)
19-
result_str = result_str.strip()
17+
try:
18+
_, half_response = raw_response.split("### Result", maxsplit=1)
19+
result_str, _ = half_response.split("\n### Ran", maxsplit=1)
20+
result_str = result_str.strip()
21+
except Exception as e:
22+
logger.error(f"Error parsing JS result: {e}. Raw result: {raw_response}")
23+
raise e
2024
return result_str
2125

2226
def step(self, action: ToolCallAction) -> str:

src/agentlab/experiments/loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def run(self):
419419
if isinstance(self.env_args, BrowserEnvArgs):
420420
env = self.env_args.make_env(exp_dir=self.exp_dir)
421421
logger.debug("Environment created.")
422-
agent = self.agent_args.make_agent(known_actions=env.actions())
422+
agent = self.agent_args.make_agent(actions=env.actions())
423423
logger.debug(f"Agent created with actions: {env.actions()}")
424424
else:
425425
agent = self.agent_args.make_agent()

0 commit comments

Comments
 (0)