Skip to content

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

Add Rust agent GIF generation parity

Magnus Müller9a7d5f6bb190
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index b94b6c06d..b8fc7b440 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -441,10 +441,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- Completed Rust-backed runs now log task completion before invoking any registered done callback, matching the Python Agent lifecycle surface.
- Proof: `test_rust_agent_logs_completion_before_done_callback`.
+87. Rust Agent GIF generation parity
+ - The Rust-backed `Agent` now honors Browser Use's `generate_gif` setting after completed Rust terminal runs and follow-ups.
+ - GIF creation uses Browser Use's `create_history_gif(...)` with the same default `agent_history.gif` output path or caller-provided string path.
+ - Proof: `test_rust_agent_generates_gif_after_done_callback`.
+
## 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` (60 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (61 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`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent bare_browser_connect_resolves_to_selected_managed_mode_with_launch_args -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 2cee6233f..5b9f4fffa 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1296,6 +1296,7 @@ class Agent(Generic[AgentStructuredOutput]):
await self._call_new_step_callback()
await self._call_callback(on_step_end, self)
await self._call_done_callback()
+ self._generate_gif_if_requested()
return self.history
async def follow_up(self, task: str, max_steps: int | None = None) -> AgentHistoryList[AgentStructuredOutput]:
@@ -1332,6 +1333,7 @@ class Agent(Generic[AgentStructuredOutput]):
await self._save_conversation_if_requested()
await self._call_new_step_callback()
await self._call_done_callback()
+ self._generate_gif_if_requested()
return self.history
follow_up_task = follow_up
@@ -1713,6 +1715,17 @@ class Agent(Generic[AgentStructuredOutput]):
encoding=self.settings.save_conversation_path_encoding or 'utf-8',
)
+ def _generate_gif_if_requested(self) -> None:
+ if not self.settings.generate_gif:
+ return
+ output_path = 'agent_history.gif'
+ if isinstance(self.settings.generate_gif, str):
+ output_path = self.settings.generate_gif
+
+ from browser_use.agent.gif import create_history_gif
+
+ create_history_gif(task=self.task, history=self.history, output_path=output_path)
+
def _conversation_snapshot(self) -> dict[str, Any]:
return {
'agent': 'browser_use.rust.Agent',
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 9a77cc7cb..9704fa75a 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1060,6 +1060,45 @@ async def test_rust_agent_logs_completion_before_done_callback(monkeypatch):
assert 'Task completed successfully' in logged_messages
+async def test_rust_agent_generates_gif_after_done_callback(monkeypatch, tmp_path):
+ from browser_use.agent import gif as gif_module
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ seen = []
+ output_path = tmp_path / 'rust-agent.gif'
+
+ def done_callback(history):
+ seen.append(('done', history.final_result()))
+
+ def fake_create_history_gif(task, history, output_path):
+ seen.append(('gif', task, history.final_result(), output_path))
+
+ monkeypatch.setattr(gif_module, 'create_history_gif', fake_create_history_gif)
+ agent = Agent(
+ task='Create a visual trace.',
+ llm=type('LLM', (), {'model': 'gpt-test'})(),
+ register_done_callback=done_callback,
+ generate_gif=str(output_path),
+ )
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ return 0, 'Session: 12345678-1234-1234-1234-123456789abc\n', ''
+
+ async def fake_load_events():
+ return [{'event_type': 'session.done', 'payload': {'result': 'gif answer'}}]
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ await agent.run(max_steps=1)
+
+ assert seen == [
+ ('done', 'gif answer'),
+ ('gif', agent.task, 'gif answer', str(output_path)),
+ ]
+
+
async def test_rust_agent_invokes_new_step_callback(monkeypatch):
from browser_use.rust import Agent