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 response item reasoning
Magnus Müllera1ce3d7be427
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 258729921..aa8c009a8 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -694,10 +694,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- This preserves prompt, cached prompt, completion, cost, and invocation data for replay/import logs that use terminal's nested usage shape instead of the older flat usage payload.- Proof: `test_rust_history_reconstructs_terminal_nested_model_usage`.+137. Rust terminal response-item reasoning reconstruction+ - Terminal `model.response.output_item` reasoning records now populate Browser Use `AgentOutput.thinking`.+ - This preserves provider replay/import reasoning summaries, including OpenAI Responses `summary_text` parts, while keeping assistant message response items on the existing memory surface.+ - Proof: `test_rust_history_reconstructs_terminal_response_item_reasoning`.+## 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` (108 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (109 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 1df299664..fc5cf65dc 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -1060,33 +1060,60 @@ def _append_streaming_text_delta(current: str, incoming: str) -> str:return current + incoming-def _response_output_item_text(payload: dict[str, Any]) -> str | None:- item = payload.get('item')- if not isinstance(item, dict):- return None- if item.get('type') != 'message':- return None- if item.get('role', 'user') != 'assistant':- return None- content = item.get('content')+def _response_content_text(content: Any, part_types: tuple[str, ...]) -> str | None:if isinstance(content, str) and content.strip():return contentif not isinstance(content, list):return Nonetext_parts = []for part in content:- if not isinstance(part, dict):+ if isinstance(part, str):+ text_parts.append(part)continue- if part.get('type') not in ('output_text', 'text', 'input_text'):- continue- text = part.get('text')- if isinstance(text, str):- text_parts.append(text)+ if isinstance(part, dict):+ part_type = part.get('type')+ if part_type in part_types or (part_type is None and 'text' in part):+ text = part.get('text')+ if isinstance(text, str):+ text_parts.append(text)text = ''.join(text_parts)return text if text.strip() else None-def _streaming_text_from_events(events: list[dict[str, Any]], stream_event_types: tuple[str, ...]) -> str | None:+def _response_output_item_text(payload: dict[str, Any]) -> str | None:+ item = payload.get('item')+ if not isinstance(item, dict):+ return None+ if item.get('type') != 'message':+ return None+ if item.get('role', 'user') != 'assistant':+ return None+ return _response_content_text(item.get('content'), ('output_text', 'text', 'input_text'))+++def _response_output_item_reasoning_text(payload: dict[str, Any]) -> str | None:+ item = payload.get('item')+ if not isinstance(item, dict):+ return None+ for key in ('reasoning_content', 'thinking', 'reasoning'):+ text = item.get(key)+ if isinstance(text, str) and text.strip():+ return text+ if item.get('type') != 'reasoning':+ return None+ for key in ('text', 'content', 'summary'):+ text = _response_content_text(item.get(key), ('summary_text', 'text', 'output_text', 'input_text'))+ if text:+ return text+ return None+++def _streaming_text_from_events(+ events: list[dict[str, Any]],+ stream_event_types: tuple[str, ...],+ *,+ response_item_extractor: Callable[[dict[str, Any]], str | None] = _response_output_item_text,+) -> str | None:text = ''for event in events:event_type = _event_type(event)@@ -1095,7 +1122,7 @@ def _streaming_text_from_events(events: list[dict[str, Any]], stream_event_typescontinuepayload = _event_payload(event)if event_type == 'model.response.output_item' and event_type in stream_event_types:- incoming = _response_output_item_text(payload)+ incoming = response_item_extractor(payload)elif event_type in stream_event_types:incoming = payload.get('text') or payload.get('delta')else:@@ -1123,7 +1150,11 @@ def _model_output_from_tool_calls(tool_calls: list[dict[str, Any]], events: listif not actions:return Nonestreamed_text = _streaming_text_from_events(events, ('model.delta', 'model.stream_delta', 'model.response.output_item'))- thinking_text = _streaming_text_from_events(events, ('model.thinking_delta',))+ thinking_text = _streaming_text_from_events(+ events,+ ('model.thinking_delta', 'model.response.output_item'),+ response_item_extractor=_response_output_item_reasoning_text,+ )return recovered_output_model(thinking=thinking_text,evaluation_previous_goal='',diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 7ca6abbce..209a2fbb5 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -777,6 +777,71 @@ def test_rust_history_reconstructs_terminal_response_item_model_text():assert history.action_history()[0][0]['result'] == 'Opened Example Domain'+def test_rust_history_reconstructs_terminal_response_item_reasoning():+ from browser_use.rust.service import _history_from_events++ history = _history_from_events(+ [+ {'event_type': 'model.turn.request', 'payload': {'model': 'gpt-test'}},+ {'event_type': 'model.thinking_delta', 'payload': {'text': 'stale thinking'}},+ {'event_type': 'model.turn.retry', 'payload': {'attempt': 1, 'max_retries': 3}},+ {+ 'event_type': 'model.response.output_item',+ 'payload': {+ 'item': {+ 'type': 'reasoning',+ 'summary': [+ {'type': 'summary_text', 'text': 'checking '},+ {'type': 'summary_text', 'text': 'page'},+ ],+ }+ },+ },+ {+ 'event_type': 'model.response.output_item',+ 'payload': {+ 'item': {+ 'type': 'message',+ 'role': 'assistant',+ 'content': [{'type': 'output_text', 'text': 'I will inspect the page.'}],+ }+ },+ },+ {+ 'type': 'model.tool_call',+ 'payload': {+ 'id': 'call-browser',+ 'name': 'browser_script',+ 'arguments': {'code': "goto_url('https://example.com')"},+ },+ },+ {+ 'type': 'tool.output',+ 'payload': {+ 'name': 'browser_script',+ 'tool_call_id': 'call-browser',+ 'text': 'Opened Example Domain',+ },+ },+ {'event_type': 'session.done', 'payload': {'result': 'Example Domain'}},+ ],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ output = history.model_outputs()[0]+ thought = history.model_thoughts()[0]++ assert output.thinking == 'checking page'+ assert output.memory == 'I will inspect the page.'+ assert thought.thinking == 'checking page'+ assert 'stale' not in (thought.thinking or '')+ assert history.action_history()[0][0]['result'] == 'Opened Example Domain'++def test_rust_history_reconstructs_terminal_token_count_usage():from browser_use.rust.service import _history_from_events