Skip to content

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

Save Rust agent conversation transcripts

Magnus Müller87833eb72e69
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index ebedf5a39..b585a5212 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -60,10 +60,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- `get_trace_object()` returns Browser Use-style `trace` and `trace_details` dictionaries from the reconstructed history.
- Proof: `test_rust_agent_invokes_new_step_callback` and `test_rust_agent_trace_and_cloud_auth_helpers`.
+11. Rust terminal conversation transcript
+ - `save_conversation_path` now writes a structured Rust terminal transcript under the configured directory after a run.
+ - The transcript includes task/session metadata, final result, errors, URLs, token usage, stdout/stderr, and raw terminal events.
+ - Proof: `test_rust_agent_saves_terminal_conversation`.
+
## Current Verification
- `python3 -m py_compile 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` (18 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (19 tests)
- `cargo build -q -p browser-use-cli`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
- Managed-headless end-to-end:
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 752f94cbb..e8e24fbe8 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -508,6 +508,8 @@ class Agent(Generic[AgentStructuredOutput]):
process_error=process_error,
)
self.history = self.result
+ self._sync_state_from_history()
+ await self._save_conversation_if_requested()
await self._call_new_step_callback()
await self._call_callback(on_step_end, self)
await self._call_done_callback()
@@ -540,6 +542,8 @@ class Agent(Generic[AgentStructuredOutput]):
process_error=process_error,
)
self.history = self.result
+ self._sync_state_from_history()
+ await self._save_conversation_if_requested()
await self._call_new_step_callback()
await self._call_done_callback()
return self.history
@@ -717,6 +721,43 @@ class Agent(Generic[AgentStructuredOutput]):
if inspect.isawaitable(result):
await result
+ def _sync_state_from_history(self) -> None:
+ if not self.history.history:
+ return
+ metadata = self.history.history[-1].metadata
+ if metadata is not None:
+ self.state.n_steps = metadata.step_number
+
+ async def _save_conversation_if_requested(self) -> None:
+ if not self.settings.save_conversation_path:
+ return
+ conversation_dir = Path(self.settings.save_conversation_path).expanduser()
+ conversation_dir.mkdir(parents=True, exist_ok=True)
+ target = conversation_dir / f'conversation_{self.id}_{self.state.n_steps}.json'
+ target.write_text(
+ json.dumps(self._conversation_snapshot(), indent=2, default=str),
+ encoding=self.settings.save_conversation_path_encoding or 'utf-8',
+ )
+
+ def _conversation_snapshot(self) -> dict[str, Any]:
+ return {
+ 'agent': 'browser_use.rust.Agent',
+ 'task_id': self.task_id,
+ 'session_id': self.session_id,
+ 'model': self.model,
+ 'browser_mode': self._browser_mode(),
+ 'task': self.task,
+ 'final_result': self.history.final_result(),
+ 'is_done': self.history.is_done(),
+ 'is_successful': self.history.is_successful(),
+ 'errors': self.history.errors(),
+ 'urls': self.history.urls(),
+ 'usage': self.history.usage.model_dump() if self.history.usage else None,
+ 'events': self.last_events,
+ 'stdout': self.last_stdout,
+ 'stderr': self.last_stderr,
+ }
+
async def _should_stop_before_run(self) -> bool:
if self.state.stopped:
return True
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index e82a86210..47cf50c8d 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -332,6 +332,41 @@ async def test_rust_agent_trace_and_cloud_auth_helpers():
assert 'trace answer' in trace_object['trace_details']['complete_history']
+async def test_rust_agent_saves_terminal_conversation(tmp_path, monkeypatch):
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ agent = Agent(
+ task='start',
+ llm=type('LLM', (), {'model': 'gpt-test'})(),
+ task_id='task-1',
+ save_conversation_path=tmp_path,
+ )
+
+ async def fake_run_process(argv):
+ return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
+
+ async def fake_load_events():
+ return [
+ {'event_type': 'browser.state', 'payload': {'url': 'https://example.com', 'title': 'Example'}},
+ {'event_type': 'session.done', 'payload': {'result': 'saved transcript'}},
+ ]
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ await agent.run(max_steps=3)
+ files = list(tmp_path.glob('conversation_task-1_*.json'))
+
+ assert len(files) == 1
+ snapshot = json.loads(files[0].read_text(encoding='utf-8'))
+ assert snapshot['task_id'] == 'task-1'
+ assert snapshot['session_id'] == '12345678-1234-1234-1234-123456789abc'
+ assert snapshot['final_result'] == 'saved transcript'
+ assert snapshot['events'][0]['event_type'] == 'browser.state'
+ assert agent.state.n_steps == 2
+
+
def test_rust_history_marks_process_failure_not_done():
from browser_use.rust.service import _history_from_events