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 item text

Magnus Mülleree3b0c652178
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index bf4b55da7..42da04dcf 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -674,10 +674,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- 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`.
+133. Rust terminal response-item model text reconstruction
+ - Terminal `model.response.output_item` assistant message records are now reconstructed into Browser Use `AgentOutput.memory`.
+ - 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`.
+
## 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` (104 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (105 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 4d1b98573..e89dde4c8 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1013,6 +1013,32 @@ 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')
+ if isinstance(content, str) and content.strip():
+ return content
+ if not isinstance(content, list):
+ return None
+ text_parts = []
+ for part in content:
+ if not isinstance(part, dict):
+ 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)
+ 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:
text = ''
for event in events:
@@ -1020,10 +1046,13 @@ def _streaming_text_from_events(events: list[dict[str, Any]], stream_event_types
if event_type in ('model.turn.request', 'model.turn.retry', 'model.turn.error'):
text = ''
continue
- if event_type not in stream_event_types:
- continue
payload = _event_payload(event)
- incoming = payload.get('text') or payload.get('delta')
+ if event_type == 'model.response.output_item' and event_type in stream_event_types:
+ incoming = _response_output_item_text(payload)
+ elif event_type in stream_event_types:
+ incoming = payload.get('text') or payload.get('delta')
+ else:
+ continue
if isinstance(incoming, str):
text = _append_streaming_text_delta(text, incoming)
return text if text.strip() else None
@@ -1046,7 +1075,7 @@ def _model_output_from_tool_calls(tool_calls: list[dict[str, Any]], events: list
continue
if not actions:
return None
- streamed_text = _streaming_text_from_events(events, ('model.delta', 'model.stream_delta'))
+ streamed_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',))
return recovered_output_model(
thinking=thinking_text,
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 262462038..b709b8da7 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -615,6 +615,58 @@ def test_rust_history_reconstructs_terminal_streamed_model_thoughts():
assert 'stale' not in (thought.thinking or '')
+def test_rust_history_reconstructs_terminal_response_item_model_text():
+ 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.stream_delta', 'payload': {'text': 'I will '}},
+ {
+ 'event_type': 'model.response.output_item',
+ 'payload': {
+ 'item': {
+ 'type': 'message',
+ 'role': 'assistant',
+ 'content': [
+ {'type': 'output_text', 'text': 'I will inspect the page, '},
+ {'type': 'text', 'text': 'then report the title.'},
+ ],
+ }
+ },
+ },
+ {
+ '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]
+
+ assert output.memory == 'I will inspect the page, then report the title.'
+ assert output.current_state.memory == 'I will inspect the page, then report the title.'
+ 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