Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Add Rust agent step action compatibility
Magnus Müller89ceb23efe0c
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 5ee0d9138..6b6a7334b 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -391,10 +391,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `f8- The helpers collect deduplicated row/file detail actions, recommend fanout when enough independent actions are present, and return `fanout_tasks` with per-item instructions and spawn messages for child-agent processing.- Proof: `browser_script_grid_rows_builds_fanout_manifests`; paginated coverage: `browser_script_extract_paginated_grid_rows_collects_pages`.+77. Rust Agent step/action compatibility+ - The Rust-backed `Agent` now exposes Browser Use-style `take_step(step_info=None)` and `multi_act(actions)`.+ - `take_step` drives one Rust terminal turn and returns `(is_done, is_valid)`; `multi_act` preserves local `done` action semantics and routes non-done Browser Use action batches through the active Rust session or a bounded one-step run.+ - Proof: `test_rust_agent_take_step_runs_one_terminal_turn`, `test_rust_agent_multi_act_preserves_done_action`, and `test_rust_agent_multi_act_routes_actions_to_followup`.+## 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`-- `uv run pytest -q tests/ci/test_rust_agent.py` (49 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (52 tests)- `cargo build -q -p browser-use-cli`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent bare_browser_connect_resolves_to_selected_managed_mode_with_launch_args -- --nocapture`diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 32ee4ab33..c1d5dc5e7 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -16,7 +16,7 @@ from pydantic import BaseModel, ValidationErrorfrom typing_extensions import TypeVarfrom uuid_extensions import uuid7str-from browser_use.agent.views import ActionResult, AgentHistory, AgentHistoryList, AgentSettings, AgentState, StepMetadata+from browser_use.agent.views import ActionResult, AgentHistory, AgentHistoryList, AgentSettings, AgentState, AgentStepInfo, StepMetadatafrom browser_use.browser import BrowserProfile, BrowserSessionfrom browser_use.browser.profile import CHROME_DETERMINISTIC_RENDERING_ARGS, CHROME_DISABLE_SECURITY_ARGS, CHROME_DOCKER_ARGSfrom browser_use.browser.views import BrowserStateHistory, TabInfo@@ -863,6 +863,56 @@ def _load_rust_history(file_path: str | Path) -> AgentHistoryList:return AgentHistoryList.model_validate(data)+def _action_payload(action: Any) -> dict[str, Any]:+ """Serialize a Browser Use action model into JSON-like data for Rust task context."""+ if isinstance(action, dict):+ return action+ if hasattr(action, 'model_dump'):+ try:+ dumped = action.model_dump(exclude_unset=True, mode='json')+ except TypeError:+ dumped = action.model_dump(exclude_unset=True)+ if isinstance(dumped, dict):+ return dumped+ return {'action': str(action)}+++def _done_action_result(payload: dict[str, Any]) -> ActionResult | None:+ done_payload = payload.get('done')+ if done_payload is None:+ return None+ if hasattr(done_payload, 'model_dump'):+ done_payload = done_payload.model_dump(exclude_unset=True, mode='json')+ if not isinstance(done_payload, dict):+ done_payload = {}+ success = done_payload.get('success')+ if not isinstance(success, bool):+ success = True+ text = done_payload.get('text')+ if not isinstance(text, str) and 'data' in done_payload:+ text = json.dumps(done_payload['data'], ensure_ascii=False, default=str)+ if not isinstance(text, str):+ text = ''+ files = done_payload.get('files_to_display')+ attachments = [str(item) for item in files if isinstance(item, (str, os.PathLike))] if isinstance(files, list) else None+ return ActionResult(+ is_done=True,+ success=success,+ extracted_content=text,+ long_term_memory=f'Task completed. Success Status: {success}',+ attachments=attachments or None,+ )+++def _actions_instruction(payloads: list[dict[str, Any]]) -> str:+ actions_json = json.dumps(payloads, indent=2, ensure_ascii=False, default=str)+ return (+ 'Execute these Browser Use action models in order using the current browser page/session. '+ 'Return a concise result for the executed actions.\n\n'+ f'Actions:\n{actions_json}'+ )++class Agent(Generic[AgentStructuredOutput]):"""Browser Use-style Agent backed by the Rust browser-use-terminal core."""@@ -1119,6 +1169,41 @@ class Agent(Generic[AgentStructuredOutput]):def usage(self) -> UsageSummary | None:return self.history.usage+ async def take_step(self, step_info: AgentStepInfo | None = None) -> tuple[bool, bool]:+ """Take one Rust terminal turn and return Browser Use-style step status."""+ if step_info is not None:+ self.state.n_steps = max(self.state.n_steps, step_info.step_number)+ history = await self.run(max_steps=1)+ is_valid = not history.has_errors()+ return history.is_done(), is_valid++ async def multi_act(self, actions: list[Any]) -> list[ActionResult]:+ """Execute Browser Use action models through the Rust-backed session.++ The Rust terminal owns browser actions, so non-`done` action batches are+ serialized as a follow-up instruction for the active Rust session. A+ standalone `done` action preserves Browser Use's local completion semantics.+ """+ payloads = [_action_payload(action) for action in actions]+ if not payloads:+ return []+ if len(payloads) == 1:+ done_result = _done_action_result(payloads[0])+ if done_result is not None:+ return [done_result]+ instruction = _actions_instruction(payloads)+ max_steps = max(1, len(payloads))+ if self.session_id:+ history = await self.follow_up(instruction, max_steps=max_steps)+ return history.action_results()+ original_task = self.task+ self.task = f'{self.task}\n\n{instruction}'+ try:+ history = await self.run(max_steps=max_steps)+ finally:+ self.task = original_task+ return history.action_results()+def add_new_task(self, new_task: str) -> None:"""Add a follow-up task while keeping the same Browser Use-style agent object."""self.task = _task_with_schema(new_task, self.output_model_schema)diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 574b7d3f2..b24d47e19 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -981,6 +981,82 @@ async def test_rust_agent_load_and_rerun_loads_saved_rust_history(tmp_path):assert results[0].extracted_content == 'loaded answer'+async def test_rust_agent_take_step_runs_one_terminal_turn():+ from browser_use.rust import Agent+ from browser_use.rust.service import _history_from_events++ agent = Agent(task='step once')+ seen = []++ async def fake_run(max_steps=100, on_step_start=None, on_step_end=None):+ seen.append(max_steps)+ return _history_from_events(+ [{'event_type': 'session.done', 'payload': {'result': 'single step answer'}}],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ agent.run = fake_run++ is_done, is_valid = await agent.take_step()++ assert seen == [1]+ assert is_done is True+ assert is_valid is True+++async def test_rust_agent_multi_act_preserves_done_action():+ from browser_use.rust import Agent++ agent = Agent(task='finish manually')++ results = await agent.multi_act([{'done': {'text': 'manual answer', 'success': False, 'files_to_display': ['report.txt']}}])++ assert len(results) == 1+ assert results[0].is_done is True+ assert results[0].success is False+ assert results[0].extracted_content == 'manual answer'+ assert results[0].attachments == ['report.txt']+++async def test_rust_agent_multi_act_routes_actions_to_followup():+ from browser_use.rust import Agent+ from browser_use.rust.service import _history_from_events++ agent = Agent(task='act on current page')+ agent.session_id = '12345678-1234-1234-1234-123456789abc'+ seen = []++ async def fake_follow_up(task, max_steps=None):+ seen.append((task, max_steps))+ return _history_from_events(+ [{'event_type': 'session.done', 'payload': {'result': 'actions applied'}}],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ agent.follow_up = fake_follow_up++ results = await agent.multi_act(+ [+ {'click_element': {'index': 2}},+ {'input_text': {'index': 3, 'text': 'hello'}},+ ]+ )++ assert seen[0][1] == 2+ assert 'Browser Use action models' in seen[0][0]+ assert '"click_element"' in seen[0][0]+ assert '"input_text"' in seen[0][0]+ assert results[0].extracted_content == 'actions applied'++async def test_rust_agent_trace_and_cloud_auth_helpers():from browser_use.rust import Agentfrom browser_use.rust.service import _history_from_events