Skip to content

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

Dispatch Rust finalize step events

Magnus Müllerd7d68fb1d161
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 9bb6f07ca..0d88c9dcd 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -946,6 +946,11 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- When `done` appears after earlier actions, the Rust wrapper stops before serializing that `done` payload into terminal follow-up/run instructions.
- Proof: `test_rust_agent_multi_act_ignores_later_done_actions`.
+187. Rust Agent step-event finalization parity
+ - Rust-backed `_finalize(...)` now dispatches Browser Use `CreateAgentStepEvent` when a browser state and model output are available.
+ - The event uses Browser Use's `CreateAgentStepEvent.from_agent_step(...)` factory, preserving action data, step metadata, URL, and screenshot payload semantics.
+ - Proof: `test_rust_agent_exposes_step_finalization_helper_methods`.
+
## 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`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 6729fdb5e..d9de3946e 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -25,6 +25,7 @@ from uuid_extensions import uuid7str
from browser_use.agent.cloud_events import (
CreateAgentOutputFileEvent,
CreateAgentSessionEvent,
+ CreateAgentStepEvent,
CreateAgentTaskEvent,
UpdateAgentTaskEvent,
)
@@ -3592,6 +3593,19 @@ class Agent(Generic[Context, AgentStructuredOutput]):
state_message=state_message,
)
self._log_step_completion_summary(step_start_time, self.state.last_result)
+ if self.state.last_model_output:
+ actions_data = []
+ for action in self.state.last_model_output.action or []:
+ action_dict = action.model_dump() if hasattr(action, 'model_dump') else {}
+ actions_data.append(action_dict)
+ step_event = CreateAgentStepEvent.from_agent_step(
+ self,
+ self.state.last_model_output,
+ self.state.last_result,
+ actions_data,
+ browser_state_summary,
+ )
+ self.eventbus.dispatch(step_event)
self.save_file_system_state()
self.state.n_steps += 1
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 3318b0042..1c3529c9c 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -4263,7 +4263,16 @@ async def test_rust_agent_exposes_step_finalization_helper_methods(tmp_path):
screenshot = base64.b64encode(b'png-bytes').decode('utf-8')
dom_state = DomState()
+ class EventBus:
+ def __init__(self):
+ self.dispatched = []
+
+ def dispatch(self, event):
+ self.dispatched.append(event)
+ return event
+
agent = Agent(task='Finalize helper parity.', file_system_path=str(tmp_path / 'agent-files'))
+ agent.eventbus = EventBus()
await agent._handle_step_error(ValueError('bad step'))
assert agent.state.consecutive_failures == 1
@@ -4279,6 +4288,12 @@ async def test_rust_agent_exposes_step_finalization_helper_methods(tmp_path):
await agent._post_process()
assert agent.state.consecutive_failures == 0
+ agent.state.last_model_output = agent.AgentOutput(
+ evaluation_previous_goal='Need answer',
+ memory='Have answer',
+ next_goal='Finish',
+ action=[agent.ActionModel(done={'text': 'final answer', 'success': True})],
+ )
agent.step_start_time = time.time() - 0.1
await agent._finalize(BrowserStateSummary())
@@ -4287,6 +4302,13 @@ async def test_rust_agent_exposes_step_finalization_helper_methods(tmp_path):
assert agent.history.final_result() == 'final answer'
assert agent.history.urls() == ['https://example.com/final']
assert Path(agent.history.history[0].state.screenshot_path).read_bytes() == b'png-bytes'
+ assert [type(event).__name__ for event in agent.eventbus.dispatched] == ['CreateAgentStepEvent']
+ assert agent.eventbus.dispatched[0].agent_task_id == str(agent.task_id)
+ assert agent.eventbus.dispatched[0].actions == [
+ {'done': {'text': 'final answer', 'success': True, 'files_to_display': []}}
+ ]
+ assert agent.eventbus.dispatched[0].url == 'https://example.com/final'
+ assert agent.eventbus.dispatched[0].screenshot_url.startswith('data:image/jpeg;base64,')
await agent._force_done_after_last_step(AgentStepInfo(step_number=2, max_steps=3))
assert agent.AgentOutput is agent.DoneAgentOutput