Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Convert Rust agent initial actions

Magnus Müller4f8e08355f94
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index a2aad51c6..208197e19 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -2,7 +2,7 @@
Branch: `magnus/browser-use-rust-core-integration`
-Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `f036761` plus small CLI/browser-mode support commits.
+Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee3ce69` plus small CLI/browser-mode support commits.
## Built Features
@@ -411,6 +411,11 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `f0
- The wrapper now exposes Browser Use-style `ActionModel`, `DoneActionModel`, and `AgentOutput` classes from the configured tools registry so custom callers can inspect and build action models against the same interface.
- Proof: `test_rust_agent_initializes_tools_and_action_models`.
+81. Rust Agent initial action model conversion
+ - The Rust-backed `Agent` now converts dictionary `initial_actions` into Browser Use registry-backed action model instances while keeping ordered raw `initial_action_payloads` for Rust task context.
+ - Common Browser Use legacy aliases such as `go_to_url`, `open_tab`, `click_element_by_index`, and `input_text` normalize to the current `navigate`, `click`, and `input` actions when the local registry can validate them.
+ - Proof: `test_rust_agent_preserves_ordered_initial_actions_context` and `test_rust_agent_mirrors_direct_url_startup`.
+
## Current Verification
- `python3 -m py_compile browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index b79e51755..8cf3e45f1 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -983,6 +983,21 @@ def _actions_instruction(payloads: list[dict[str, Any]]) -> str:
)
+def _normalize_initial_action(action_name: str, params: Any) -> tuple[str, Any]:
+ if not isinstance(params, dict):
+ return action_name, params
+ if action_name in ('go_to_url', 'open_tab'):
+ normalized = dict(params)
+ if action_name == 'open_tab' and 'new_tab' not in normalized:
+ normalized['new_tab'] = True
+ return 'navigate', normalized
+ if action_name == 'click_element_by_index':
+ return 'click', params
+ if action_name == 'input_text':
+ return 'input', params
+ return action_name, params
+
+
class Agent(Generic[AgentStructuredOutput]):
"""Browser Use-style Agent backed by the Rust browser-use-terminal core."""
@@ -1123,12 +1138,13 @@ class Agent(Generic[AgentStructuredOutput]):
self.initial_url = _extract_start_url(task)
if self.initial_url:
initial_actions = [{'navigate': {'url': self.initial_url, 'new_tab': False}}]
- self.initial_actions = initial_actions
+ self.initial_action_payloads = list(initial_actions or [])
+ self.initial_actions = self._convert_initial_actions(self.initial_action_payloads) if self.initial_action_payloads else None
self.task = _task_with_schema(
_task_with_available_files(
_task_with_sensitive_data_context(
_task_with_domain_constraints(
- _task_with_initial_actions(task, initial_actions),
+ _task_with_initial_actions(task, self.initial_action_payloads),
self.allowed_domains,
self.prohibited_domains,
),
@@ -1274,6 +1290,32 @@ class Agent(Generic[AgentStructuredOutput]):
self.AgentOutput = AgentOutput.type_with_custom_actions_no_thinking(self.ActionModel)
self.DoneActionModel = create_action_model(include_actions=['done'], page_url=page_url)
+ def _convert_initial_actions(self, actions: list[dict[str, dict[str, Any]]]) -> list[Any]:
+ """Convert dictionary initial actions to Browser Use action model instances when possible."""
+ converted_actions: list[Any] = []
+ registry = getattr(getattr(self.tools, 'registry', None), 'registry', None)
+ registry_actions = getattr(registry, 'actions', {})
+ action_model = getattr(self, 'ActionModel', None)
+ if action_model is None:
+ return list(actions)
+ for action_dict in actions:
+ if not isinstance(action_dict, dict) or not action_dict:
+ converted_actions.append(action_dict)
+ continue
+ action_name = next(iter(action_dict))
+ params = action_dict[action_name]
+ normalized_name, normalized_params = _normalize_initial_action(action_name, params)
+ action_info = registry_actions.get(normalized_name)
+ if action_info is None:
+ converted_actions.append(action_dict)
+ continue
+ try:
+ validated_params = action_info.param_model(**(normalized_params if isinstance(normalized_params, dict) else {}))
+ converted_actions.append(action_model(**{normalized_name: validated_params}))
+ except (TypeError, ValueError, ValidationError):
+ converted_actions.append(action_dict)
+ return converted_actions
+
async def _check_and_update_downloads(self, context: str = '') -> None:
"""Mirror Browser Use's downloaded-file tracking for supplied sessions."""
if not self.has_downloads_path or self.browser_session is None:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index acb85d94d..c69a0f44e 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -712,7 +712,10 @@ def test_rust_agent_mirrors_direct_url_startup():
agent = Agent(task='Open example.com and report the title.')
assert agent.initial_url == 'https://example.com'
- assert agent.initial_actions == [{'navigate': {'url': 'https://example.com', 'new_tab': False}}]
+ assert agent.initial_action_payloads == [{'navigate': {'url': 'https://example.com', 'new_tab': False}}]
+ assert agent.initial_actions[0].model_dump(exclude_unset=True) == {
+ 'navigate': {'url': 'https://example.com', 'new_tab': False}
+ }
assert "First navigate to 'https://example.com'" in agent.task
@@ -726,7 +729,16 @@ def test_rust_agent_preserves_ordered_initial_actions_context():
{'click_element_by_index': {'index': 3}},
],
)
+ converted = [action.model_dump(exclude_unset=True) for action in agent.initial_actions]
+ assert agent.initial_action_payloads == [
+ {'navigate': {'url': 'https://example.com', 'new_tab': False}},
+ {'click_element_by_index': {'index': 3}},
+ ]
+ assert converted == [
+ {'navigate': {'url': 'https://example.com', 'new_tab': False}},
+ {'click': {'index': 3}},
+ ]
assert 'Browser Use initial actions in order' in agent.task
assert '"navigate"' in agent.task
assert '"click_element_by_index"' in agent.task