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 setup helper parity

Magnus Müllerb37111c2a917
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 2db314543..3b29965b8 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -481,10 +481,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- The helpers support Browser Use message content mutation plus recursive Pydantic/dict/list/tuple restoration of shortened URLs.
- Proof: `test_rust_agent_exposes_url_text_helper_methods`.
+95. Rust Agent setup helper parity
+ - The Rust-backed `Agent` now exposes Browser Use's `_set_file_system(...)`, `_set_screenshot_service()`, `_set_browser_use_version_and_source(...)`, and `_verify_and_setup_llm()` setup helpers.
+ - Construction routes through the helper methods, and direct calls support state restoration, explicit file-system initialization, screenshot storage setup, source overrides, and Rust-terminal LLM verification compatibility.
+ - Proof: `test_rust_agent_exposes_setup_helper_methods`.
+
## 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` (68 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (69 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 5044ffb98..3ab0456a8 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1117,14 +1117,14 @@ class Agent(Generic[AgentStructuredOutput]):
self.register_external_agent_status_raise_error_callback = register_external_agent_status_raise_error_callback
self.register_should_stop_callback = register_should_stop_callback
self.output_model_schema = output_model_schema
- self.version, self.source = _browser_use_version_and_source(source)
+ self._set_browser_use_version_and_source(source)
self.kwargs = kwargs
self.model = _model_name(llm)
self.state = injected_agent_state or AgentState(agent_id=self.id)
timestamp = int(time.time())
self.agent_directory = Path(tempfile.gettempdir()) / f'browser_use_agent_{self.id}_{timestamp}'
- self.file_system, self.file_system_path = _init_file_system(self.state, self.agent_directory, file_system_path)
- self.screenshot_service = ScreenshotService(self.agent_directory)
+ self._set_file_system(file_system_path)
+ self._set_screenshot_service()
self.settings = AgentSettings(
use_vision=use_vision,
vision_detail_level=vision_detail_level,
@@ -1233,6 +1233,32 @@ class Agent(Generic[AgentStructuredOutput]):
self._external_pause_event = asyncio.Event()
self._external_pause_event.set()
+ def _set_file_system(self, file_system_path: str | None = None) -> None:
+ """Initialize or restore Browser Use file-system state."""
+ self.file_system, self.file_system_path = _init_file_system(self.state, self.agent_directory, file_system_path)
+
+ def _set_screenshot_service(self) -> None:
+ """Initialize Browser Use screenshot storage under the agent directory."""
+ self.screenshot_service = ScreenshotService(self.agent_directory)
+
+ def _set_browser_use_version_and_source(self, source_override: str | None = None) -> None:
+ """Expose Browser Use version/source metadata on the Rust-backed wrapper."""
+ self.version, self.source = _browser_use_version_and_source(source_override)
+
+ def _verify_and_setup_llm(self) -> bool:
+ """Mirror Browser Use LLM verification state for callers that use the helper."""
+ if self.llm is None:
+ return True
+ try:
+ from browser_use.config import CONFIG
+
+ skip_verification = CONFIG.SKIP_LLM_API_KEY_VERIFICATION
+ except Exception:
+ skip_verification = False
+ if getattr(self.llm, '_verified_api_keys', None) is True or skip_verification:
+ setattr(self.llm, '_verified_api_keys', True)
+ return True
+
async def run(
self,
max_steps: int = 100,
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index ce28697a6..7dcbe43cb 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -973,6 +973,46 @@ def test_rust_agent_initializes_browser_use_session_and_file_system(tmp_path):
assert agent.state.file_system_state is not None
+def test_rust_agent_exposes_setup_helper_methods(tmp_path):
+ from browser_use.rust import Agent
+ from browser_use.screenshots.service import ScreenshotService
+
+ class LLM:
+ _verified_api_keys = True
+
+ initial_file_system_path = tmp_path / 'initial-files'
+ new_file_system_path = tmp_path / 'new-files'
+ agent = Agent(
+ task='Set up helpers.',
+ llm=LLM(),
+ file_system_path=str(initial_file_system_path),
+ source='constructor-source',
+ )
+
+ agent.file_system = None
+ agent.file_system_path = None
+ agent._set_file_system()
+ agent._set_screenshot_service()
+ agent._set_browser_use_version_and_source('helper-source')
+
+ assert agent.file_system_path == str(initial_file_system_path)
+ assert agent.file_system.base_dir == initial_file_system_path
+ assert isinstance(agent.screenshot_service, ScreenshotService)
+ assert agent.screenshot_service.agent_directory == agent.agent_directory
+ assert agent.source == 'helper-source'
+ assert agent.version
+ assert agent._verify_and_setup_llm() is True
+
+ with pytest.raises(ValueError, match='Cannot provide both file_system_state'):
+ agent._set_file_system(str(new_file_system_path))
+
+ agent.state.file_system_state = None
+ agent._set_file_system(str(new_file_system_path))
+
+ assert agent.file_system_path == str(new_file_system_path)
+ assert agent.file_system.base_dir == new_file_system_path
+
+
def test_rust_agent_browser_profile_property_tracks_session_profile():
from browser_use.rust import Agent