Skip to content

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

Dispatch Rust agent GIF output events

Magnus Müller270bf67aa8cf
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index c5ba24613..8c7e82331 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -831,10 +831,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Run event buses are stopped after completion and recreated before later runs when needed, matching Browser Use's per-run eventbus lifecycle without leaving pending no-handler events in local tests.
- Proof: `test_rust_agent_run_dispatches_browser_use_lifecycle_events`.
+164. Rust Agent GIF output-file event parity
+ - Terminal-backed `Agent.run()` and `Agent.follow_up()` now dispatch `CreateAgentOutputFileEvent` when Browser Use `generate_gif` creates an agent history GIF.
+ - The output-file event carries the generated GIF file name, `image/gif` content type, task ID, and base64 file content before the run eventbus is stopped.
+ - Proof: `test_rust_agent_dispatches_gif_output_file_event`.
+
## 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` (135 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (136 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 db97f3472..8215a072b 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -22,7 +22,12 @@ from pydantic import BaseModel, ValidationError, create_model
from typing_extensions import TypeVar
from uuid_extensions import uuid7str
-from browser_use.agent.cloud_events import CreateAgentSessionEvent, CreateAgentTaskEvent, UpdateAgentTaskEvent
+from browser_use.agent.cloud_events import (
+ CreateAgentOutputFileEvent,
+ CreateAgentSessionEvent,
+ CreateAgentTaskEvent,
+ UpdateAgentTaskEvent,
+)
from browser_use.agent.message_manager.utils import save_conversation
from browser_use.agent.message_manager.service import MessageManager
from browser_use.agent.prompts import SystemPrompt
@@ -2924,7 +2929,7 @@ class Agent(Generic[Context, 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()
+ await self._generate_gif_if_requested()
await self._stop_eventbus_after_run()
return self.history
@@ -2966,7 +2971,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
await self._save_conversation_if_requested()
await self._call_new_step_callback()
await self._call_done_callback()
- self._generate_gif_if_requested()
+ await self._generate_gif_if_requested()
await self._stop_eventbus_after_run()
return self.history
@@ -3876,7 +3881,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
encoding=self.settings.save_conversation_path_encoding or 'utf-8',
)
- def _generate_gif_if_requested(self) -> None:
+ async def _generate_gif_if_requested(self) -> None:
if not self.settings.generate_gif:
return
output_path = 'agent_history.gif'
@@ -3886,6 +3891,9 @@ class Agent(Generic[Context, AgentStructuredOutput]):
from browser_use.agent.gif import create_history_gif
create_history_gif(task=self.task, history=self.history, output_path=output_path)
+ if Path(output_path).exists():
+ output_event = await CreateAgentOutputFileEvent.from_agent_and_file(self, output_path)
+ self.eventbus.dispatch(output_event)
def _conversation_snapshot(self) -> dict[str, Any]:
return {
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 169cad4c9..9b0e75532 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3494,6 +3494,56 @@ async def test_rust_agent_generates_gif_after_done_callback(monkeypatch, tmp_pat
]
+async def test_rust_agent_dispatches_gif_output_file_event(monkeypatch, tmp_path):
+ import base64
+
+ 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')
+ output_path = tmp_path / 'rust-agent.gif'
+ dispatched = []
+
+ class EventBus:
+ def dispatch(self, event):
+ dispatched.append(event)
+ return event
+
+ def fake_create_history_gif(task, history, output_path):
+ Path(output_path).write_bytes(b'GIF89a')
+
+ monkeypatch.setattr(gif_module, 'create_history_gif', fake_create_history_gif)
+ agent = Agent(
+ task='Create a visual trace output event.',
+ llm=type('LLM', (), {'model': 'gpt-test'})(),
+ generate_gif=str(output_path),
+ )
+ agent.eventbus = EventBus()
+
+ 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 output event'}}]
+
+ agent._run_process = fake_run_process
+ agent._load_events = fake_load_events
+
+ await agent.run(max_steps=1)
+
+ assert [type(event).__name__ for event in dispatched] == [
+ 'CreateAgentSessionEvent',
+ 'CreateAgentTaskEvent',
+ 'UpdateAgentTaskEvent',
+ 'CreateAgentOutputFileEvent',
+ ]
+ output_event = dispatched[-1]
+ assert output_event.task_id == str(agent.task_id)
+ assert output_event.file_name == output_path.name
+ assert output_event.content_type == 'image/gif'
+ assert base64.b64decode(output_event.file_content) == b'GIF89a'
+
+
async def test_rust_agent_invokes_new_step_callback(monkeypatch):
from browser_use.rust import Agent