Skip to content

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

Reconstruct Rust terminal response input tool results

Magnus Müller783c11819f9f
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 42da04dcf..8ef322a42 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -679,10 +679,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This preserves model commentary for replay/import logs that store assistant text as response items instead of live `model.stream_delta` events, while still de-duplicating overlapping stream prefixes.
- Proof: `test_rust_history_reconstructs_terminal_response_item_model_text`.
+134. Rust terminal response-input tool-result reconstruction
+ - Terminal `model.response.input_item` function-call-output records are now reconstructed into Browser Use `ActionResult` entries.
+ - This preserves tool result text for provider-shaped replay/import logs that store tool outputs as response input items instead of `tool.output` events.
+ - Proof: `test_rust_history_reconstructs_terminal_response_input_item_tool_results`.
+
## 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` (105 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (106 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.py
index e89dde4c8..1f1761a11 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -974,28 +974,63 @@ def _tool_results_by_call_id(events: list[dict[str, Any]]) -> dict[str, tuple[st
results: dict[str, tuple[str, dict[str, Any]]] = {}
for event in events:
event_type = _event_type(event)
- if event_type not in ('tool.output', 'tool.failed', 'tool.aborted'):
+ if event_type not in ('tool.output', 'tool.failed', 'tool.aborted', 'model.response.input_item'):
continue
payload = _event_payload(event)
+ if event_type == 'model.response.input_item':
+ payload = _response_input_item_tool_payload(payload)
call_id = payload.get('tool_call_id') or payload.get('call_id')
if call_id:
results[str(call_id)] = (event_type, payload)
return results
+def _response_input_item_tool_payload(payload: dict[str, Any]) -> dict[str, Any]:
+ item = payload.get('item')
+ if not isinstance(item, dict):
+ item = {}
+ call_id = item.get('call_id') or item.get('id') or payload.get('call_id') or payload.get('id')
+ result_payload: dict[str, Any] = dict(payload)
+ if call_id:
+ result_payload['tool_call_id'] = call_id
+ name = item.get('name') or payload.get('name')
+ if isinstance(name, str) and name:
+ result_payload['name'] = name
+ if 'output' in item:
+ result_payload['output'] = item.get('output')
+ elif 'content' in item:
+ result_payload['content'] = item.get('content')
+ return result_payload
+
+
+def _tool_output_text(value: Any) -> str | None:
+ if isinstance(value, str) and value.strip():
+ return value.strip()
+ if isinstance(value, list):
+ text_parts = []
+ for part in value:
+ if isinstance(part, str):
+ text_parts.append(part)
+ elif isinstance(part, dict):
+ text = part.get('text')
+ if isinstance(text, str):
+ text_parts.append(text)
+ text = ''.join(text_parts).strip()
+ return text or None
+ if isinstance(value, dict):
+ return json.dumps(value, ensure_ascii=False, default=str)
+ return None
+
+
def _tool_result_text(payload: dict[str, Any]) -> str | None:
for key in ('text', 'output', 'result'):
- value = payload.get(key)
- if isinstance(value, str) and value.strip():
- return value.strip()
+ text = _tool_output_text(payload.get(key))
+ if text:
+ return text
content = payload.get('content')
- if isinstance(content, list):
- for part in content:
- if not isinstance(part, dict):
- continue
- text = part.get('text')
- if isinstance(text, str) and text.strip():
- return text.strip()
+ text = _tool_output_text(content)
+ if text:
+ return text
return None
@@ -1110,8 +1145,8 @@ def _action_results_from_tool_calls(
action_results.append(
ActionResult(
error=error,
- extracted_content=text if event_type == 'tool.output' else None,
- long_term_memory=text if event_type == 'tool.output' else None,
+ extracted_content=text if event_type in ('tool.output', 'model.response.input_item') else None,
+ long_term_memory=text if event_type in ('tool.output', 'model.response.input_item') else None,
)
)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index b709b8da7..f7128d949 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -467,6 +467,53 @@ def test_rust_history_reconstructs_terminal_model_tool_call_actions():
assert history.last_action() == {'done': {'text': 'Example Domain', 'success': True}}
+def test_rust_history_reconstructs_terminal_response_input_item_tool_results():
+ 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': 'model.response.input_item',
+ 'payload': {
+ 'call_id': 'call-browser',
+ 'name': 'browser_script',
+ 'item': {
+ 'type': 'function_call_output',
+ 'call_id': 'call-browser',
+ 'output': [
+ {'type': 'input_text', 'text': 'Opened '},
+ {'type': 'input_text', 'text': 'Example Domain'},
+ {'type': 'input_image', 'image_url': 'data:image/png;base64,AAAA'},
+ ],
+ },
+ },
+ },
+ {'event_type': 'session.done', 'payload': {'result': 'Example Domain'}},
+ ],
+ model='gpt-test',
+ started=1.0,
+ finished=2.0,
+ output_model_schema=None,
+ process_error=None,
+ )
+
+ first_result = history.action_results()[0]
+
+ assert history.action_names() == ['browser_script', 'done']
+ assert first_result.extracted_content == 'Opened Example Domain'
+ assert first_result.long_term_memory == 'Opened Example Domain'
+ assert history.action_history()[0][0]['result'] == 'Opened Example Domain'
+ assert history.final_result() == 'Example Domain'
+
+
def test_rust_history_synthesizes_done_action_from_terminal_completion():
from browser_use.rust.service import _history_from_events