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 streamed output

Magnus Müllercfa280bf9c62
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index f9f0979bf..214d42f69 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -639,10 +639,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This keeps completed Rust-backed histories consistent for `history.last_action()`, `history.action_names()`, `history.model_actions_filtered(['done'])`, and action-history consumers that expect a final Browser Use completion action.
- Proof: `test_rust_history_synthesizes_done_action_from_terminal_completion`.
+126. Rust terminal streamed model output reconstruction
+ - Terminal `model.stream_delta`/`model.delta` events are now reconstructed into Browser Use `AgentOutput.memory`, and `model.thinking_delta` events are reconstructed into `AgentOutput.thinking`.
+ - The reducer mirrors terminal current-main retry semantics by clearing stale streamed text after `model.turn.request`, `model.turn.retry`, and `model.turn.error`, and de-duplicates prefix-style deltas before exposing `history.model_outputs()` and `history.model_thoughts()`.
+ - Proof: `test_rust_history_reconstructs_terminal_streamed_model_thoughts`.
+
## 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` (97 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (98 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 7621f4b06..b5551b0f0 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -881,7 +881,37 @@ def _tool_result_text(payload: dict[str, Any]) -> str | None:
return None
-def _model_output_from_tool_calls(tool_calls: list[dict[str, Any]]) -> AgentOutput | None:
+def _append_streaming_text_delta(current: str, incoming: str) -> str:
+ if not incoming:
+ return current
+ if not current:
+ return incoming
+ if incoming == current or incoming.strip() == current.strip():
+ return current
+ if incoming.startswith(current):
+ return current + incoming[len(current) :]
+ if len(incoming) >= 24 and current.endswith(incoming):
+ return current
+ return current + incoming
+
+
+def _streaming_text_from_events(events: list[dict[str, Any]], stream_event_types: tuple[str, ...]) -> str | None:
+ text = ''
+ for event in events:
+ event_type = _event_type(event)
+ 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 isinstance(incoming, str):
+ text = _append_streaming_text_delta(text, incoming)
+ return text if text.strip() else None
+
+
+def _model_output_from_tool_calls(tool_calls: list[dict[str, Any]], events: list[dict[str, Any]]) -> AgentOutput | None:
if not tool_calls:
return None
action_names = list(dict.fromkeys(call['name'] for call in tool_calls if isinstance(call.get('name'), str)))
@@ -898,9 +928,12 @@ def _model_output_from_tool_calls(tool_calls: list[dict[str, Any]]) -> AgentOutp
continue
if not actions:
return None
+ streamed_text = _streaming_text_from_events(events, ('model.delta', 'model.stream_delta'))
+ thinking_text = _streaming_text_from_events(events, ('model.thinking_delta',))
return recovered_output_model(
+ thinking=thinking_text,
evaluation_previous_goal='',
- memory='',
+ memory=streamed_text or '',
next_goal='',
action=actions,
)
@@ -1126,7 +1159,7 @@ def _history_from_events(
is_done=is_done,
)
history = AgentHistory(
- model_output=_model_output_from_tool_calls(tool_calls),
+ model_output=_model_output_from_tool_calls(tool_calls, events),
result=_action_results_from_tool_calls(
tool_calls,
events,
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 27a28b88a..81b057716 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -408,6 +408,48 @@ def test_rust_history_synthesizes_done_action_from_terminal_completion():
assert history.action_history()[0][-1]['result'] == 'Example Domain'
+def test_rust_history_reconstructs_terminal_streamed_model_thoughts():
+ 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': 'stale text'}},
+ {'event_type': 'model.thinking_delta', 'payload': {'text': 'stale thinking'}},
+ {'event_type': 'model.turn.error', 'payload': {'transient': True, 'error': 'retry'}},
+ {'event_type': 'model.turn.retry', 'payload': {'attempt': 1, 'max_retries': 3}},
+ {'event_type': 'model.thinking_delta', 'payload': {'text': 'checking '}},
+ {'event_type': 'model.thinking_delta', 'payload': {'text': 'checking page'}},
+ {'event_type': 'model.stream_delta', 'payload': {'text': 'Opening '}},
+ {'event_type': 'model.stream_delta', 'payload': {'text': 'Opening Example Domain'}},
+ {
+ 'type': 'tool.started',
+ 'payload': {
+ 'name': 'browser_script',
+ 'tool_call_id': 'call-browser',
+ 'arguments': {'code': "goto_url('https://example.com')"},
+ },
+ },
+ {'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 == 'Opening Example Domain'
+ assert thought.thinking == 'checking page'
+ assert thought.memory == 'Opening Example Domain'
+ assert 'stale' not in thought.memory
+ assert 'stale' not in (thought.thinking or '')
+
+
def test_rust_history_reconstructs_terminal_token_count_usage():
from browser_use.rust.service import _history_from_events