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
Reconstruct Rust terminal model tool calls
Magnus Müller6cd73cefc832
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 150f6dc83..bf4b55da7 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -669,10 +669,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- Terminal `tool.aborted` events now surface action-level abort messages and final history errors when no terminal completion result is available.- Proof: `test_rust_history_surfaces_terminal_cancellation_and_tool_abort_messages`.+132. Rust terminal model tool-call reconstruction+ - Terminal `model.tool_call` and provider `model.response.output_item` function-call records are now reconstructed into Browser Use action models.+ - This preserves model action history for terminal replay/import logs that contain model call records instead of, or in addition to, `tool.started`, and de-duplicates repeated call ids.+ - Proof: `test_rust_history_reconstructs_terminal_model_tool_call_actions`.+## Current Verification- `python3 -m py_compile browser_use/agent/service.py 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` (103 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (104 tests)- `cargo build -q -p browser-use-cli` on terminal branch `magnus/browser-use-rust-main-integration`- `cargo test -q -p browser-use-cli run_codex_session_command_accepts_task_id_and_model -- --nocapture`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex ce6ef8fbb..4d1b98573 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -886,28 +886,63 @@ def _safe_tool_action_name(value: Any) -> str | None:def _tool_arguments(value: Any) -> dict[str, Any]:if isinstance(value, dict):return value+ if isinstance(value, str):+ try:+ parsed = json.loads(value)+ except json.JSONDecodeError:+ return {'value': value}+ return parsed if isinstance(parsed, dict) else {'value': parsed}if value is None:return {}return {'value': value}+def _tool_call_from_payload(payload: dict[str, Any], fallback_id: str) -> dict[str, Any] | None:+ function = payload.get('function')+ if not isinstance(function, dict):+ function = {}+ name = _safe_tool_action_name(payload.get('name') or function.get('name'))+ if name is None:+ return None+ call_id = payload.get('tool_call_id') or payload.get('call_id') or payload.get('id') or fallback_id+ arguments = payload.get('arguments')+ if arguments is None:+ arguments = function.get('arguments')+ return {+ 'name': name,+ 'tool_call_id': str(call_id),+ 'arguments': _tool_arguments(arguments),+ }+++def _tool_call_from_response_item(payload: dict[str, Any], fallback_id: str) -> dict[str, Any] | None:+ item = payload.get('item')+ if not isinstance(item, dict):+ return None+ if item.get('type') not in ('function_call', 'custom_tool_call'):+ return None+ return _tool_call_from_payload(item, fallback_id)++def _tool_started_calls(events: list[dict[str, Any]]) -> list[dict[str, Any]]:tool_calls: list[dict[str, Any]] = []+ seen_call_ids: set[str] = set()for event in events:- if _event_type(event) != 'tool.started':- continue+ event_type = _event_type(event)payload = _event_payload(event)- name = _safe_tool_action_name(payload.get('name'))- if name is None:+ if event_type in ('tool.started', 'model.tool_call'):+ tool_call = _tool_call_from_payload(payload, f'tool-{len(tool_calls)}')+ elif event_type == 'model.response.output_item':+ tool_call = _tool_call_from_response_item(payload, f'tool-{len(tool_calls)}')+ else:continue- call_id = payload.get('tool_call_id') or payload.get('call_id') or f'tool-{len(tool_calls)}'- tool_calls.append(- {- 'name': name,- 'tool_call_id': str(call_id),- 'arguments': _tool_arguments(payload.get('arguments')),- }- )+ if tool_call is None:+ continue+ call_id = tool_call['tool_call_id']+ if call_id in seen_call_ids:+ continue+ seen_call_ids.add(call_id)+ tool_calls.append(tool_call)return tool_callsdiff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex f894b8c38..262462038 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -409,6 +409,64 @@ def test_rust_history_reconstructs_terminal_tool_call_actions():assert history.action_results()[-1].is_done is True+def test_rust_history_reconstructs_terminal_model_tool_call_actions():+ from browser_use.rust.service import _history_from_events++ history = _history_from_events(+ [+ {+ 'type': 'model.tool_call',+ 'payload': {+ 'id': 'call-browser',+ 'name': 'browser_script',+ 'arguments': {'code': "goto_url('https://example.com')"},+ },+ },+ {+ 'type': 'tool.started',+ 'payload': {+ 'name': 'browser_script',+ 'tool_call_id': 'call-browser',+ 'arguments': {'code': "duplicate_should_not_replace_original()"},+ },+ },+ {+ 'type': 'tool.output',+ 'payload': {+ 'name': 'browser_script',+ 'tool_call_id': 'call-browser',+ 'text': 'Opened Example Domain',+ },+ },+ {+ 'type': 'model.response.output_item',+ 'payload': {+ 'item': {+ 'type': 'function_call',+ 'call_id': 'call-done',+ 'name': 'done',+ 'arguments': '{"text": "Example Domain", "success": true}',+ }+ },+ },+ {'event_type': 'session.done', 'payload': {'result': 'Example Domain'}},+ ],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ actions = history.model_actions()++ assert history.action_names() == ['browser_script', 'done']+ assert actions[0]['browser_script']['code'] == "goto_url('https://example.com')"+ assert actions[1]['done'] == {'text': 'Example Domain', 'success': True}+ assert history.action_history()[0][0]['result'] == 'Opened Example Domain'+ assert history.last_action() == {'done': {'text': 'Example Domain', 'success': True}}++def test_rust_history_synthesizes_done_action_from_terminal_completion():from browser_use.rust.service import _history_from_events