Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Merge remote-tracking branch 'upstream/main' into magnus/browser-use-rust-core-integration
Magnus Müller1474c60da127
diff --cc browser_use/__init__.pyindex cb9d43300,946ceba12..cd217aaf0--- a/browser_use/__init__.py+++ b/browser_use/__init__.py@@@ -61,15 -64,15 +62,14 @@@ if TYPE_CHECKINGfrom browser_use.llm.oci_raw.chat import ChatOCIRawfrom browser_use.llm.ollama.chat import ChatOllamafrom browser_use.llm.openai.chat import ChatOpenAI+ from browser_use.llm.vercel.chat import ChatVercel+ from browser_use.sandbox import sandboxfrom browser_use.tools.service import Controller, Tools-- # Lazy imports mapping - only import when actually accessed+ # Lazy imports mapping - only import when actually accessed_LAZY_IMPORTS = {# Agent service (heavy due to dependencies)- # 'Agent': ('browser_use.agent.service', 'Agent'),'Agent': ('browser_use.agent.service', 'Agent'),- # Code-use agent (Jupyter notebook-like execution)- 'CodeAgent': ('browser_use.code_use.service', 'CodeAgent'),# System prompt (moderate weight due to agent.views imports)'SystemPrompt': ('browser_use.agent.prompts', 'SystemPrompt'),# Agent views (very heavy - over 1 second!)diff --cc browser_use/agent/service.pyindex 4d18660ab,d70a107bd..3409fcc5d--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@@ -2316,38 -4041,92 +4048,128 @@@ class Agent(Generic[Context, AgentStrucreturn asyncio.run(self.run(max_steps=max_steps, on_step_start=on_step_start, on_step_end=on_step_end))+ def detect_variables(self) -> dict[str, DetectedVariable]:+ """Detect reusable variables in agent history"""+ from browser_use.agent.variable_detector import detect_variables_in_history++ return detect_variables_in_history(self.history)++ def _substitute_variables_in_history(self, history: AgentHistoryList, variables: dict[str, str]) -> AgentHistoryList:+ """Substitute variables in history with new values for rerunning with different data"""+ from browser_use.agent.variable_detector import detect_variables_in_history++ # Detect variables in the history+ detected_vars = detect_variables_in_history(history)++ # Build a mapping of original values to new values+ value_replacements: dict[str, str] = {}+ for var_name, new_value in variables.items():+ if var_name in detected_vars:+ old_value = detected_vars[var_name].original_value+ value_replacements[old_value] = new_value+ else:+ self.logger.warning(f'Variable "{var_name}" not found in history, skipping substitution')++ if not value_replacements:+ self.logger.info('No variables to substitute')+ return history++ # Create a deep copy of history to avoid modifying the original+ import copy++ modified_history = copy.deepcopy(history)++ # Substitute values in all actions+ substitution_count = 0+ for history_item in modified_history.history:+ if not history_item.model_output or not history_item.model_output.action:+ continue++ for action in history_item.model_output.action:+ # Handle both Pydantic models and dicts+ if hasattr(action, 'model_dump'):+ action_dict = action.model_dump()+ elif isinstance(action, dict):+ action_dict = action+ else:+ action_dict = vars(action) if hasattr(action, '__dict__') else {}++ # Substitute in all string fields+ substitution_count += self._substitute_in_dict(action_dict, value_replacements)++ # Update the action with modified values+ if hasattr(action, 'model_dump'):+ # For Pydantic RootModel, we need to recreate from the modified dict+ if hasattr(action, 'root'):+ # This is a RootModel - recreate it from the modified dict+ new_action = type(action).model_validate(action_dict)+ # Replace the root field in-place using object.__setattr__ to bypass Pydantic's immutability+ object.__setattr__(action, 'root', getattr(new_action, 'root'))+ else:+ # Regular Pydantic model - update fields in-place+ for key, val in action_dict.items():+ if hasattr(action, key):+ setattr(action, key, val)+ elif isinstance(action, dict):+ action.update(action_dict)++ self.logger.info(f'Substituted {substitution_count} value(s) in {len(value_replacements)} variable type(s) in history')+ return modified_history++ def _substitute_in_dict(self, data: dict, replacements: dict[str, str]) -> int:+ """Recursively substitute values in a dictionary, returns count of substitutions made"""+ count = 0+ for key, value in data.items():+ if isinstance(value, str):+ # Replace if exact match+ if value in replacements:+ data[key] = replacements[value]+ count += 1+ elif isinstance(value, dict):+ # Recurse into nested dicts+ count += self._substitute_in_dict(value, replacements)+ elif isinstance(value, list):+ # Handle lists+ for i, item in enumerate(value):+ if isinstance(item, str) and item in replacements:+ value[i] = replacements[item]+ count += 1+ elif isinstance(item, dict):+ count += self._substitute_in_dict(item, replacements)+ return count++++_PythonAgent = Agent+++def _align_rust_agent_signatures() -> None:+ from browser_use.rust.service import Agent as RustAgent++ for name, browser_use_method in vars(_PythonAgent).items():+ if name.startswith('__') and name != '__init__':+ continue+ rust_method = getattr(RustAgent, name, None)+ if rust_method is None or not callable(browser_use_method) or not callable(rust_method):+ continue+ try:+ rust_method.__signature__ = inspect.signature(browser_use_method)+ rust_method.__annotations__ = dict(getattr(browser_use_method, '__annotations__', {}))+ except (TypeError, ValueError, AttributeError):+ continue+ try:+ RustAgent.__signature__ = inspect.signature(_PythonAgent)+ except (TypeError, ValueError, AttributeError):+ pass+ agent_hook_func = Callable[[_PythonAgent], Awaitable[None]]+ for name in ('run', 'run_sync'):+ rust_method = getattr(RustAgent, name, None)+ if rust_method is None:+ continue+ try:+ rust_method.__annotations__['on_step_start'] = agent_hook_func | None+ rust_method.__annotations__['on_step_end'] = agent_hook_func | None+ except (TypeError, AttributeError, KeyError):+ continue+++_align_rust_agent_signatures()diff --cc browser_use/browser/watchdogs/downloads_watchdog.pyindex 176e95b1f,d4127e899..c3ec80453--- a/browser_use/browser/watchdogs/downloads_watchdog.py+++ b/browser_use/browser/watchdogs/downloads_watchdog.py@@@ -414,18 -557,14 +627,26 @@@ class DownloadsWatchdog(BaseWatchdog)if not (is_pdf or is_download_attachment):return+ # Extract filename from Content-Disposition if available+ suggested_filename = _filename_from_content_disposition(content_disposition)++ if not _should_auto_download_network_response(+ url=url,+ content_type=content_type,+ is_pdf=is_pdf,+ is_download_attachment=is_download_attachment,+ suggested_filename=suggested_filename,+ ):+ return++ # If already downloaded this URL and file still exists, do nothing+ existing_path = self._session_pdf_urls.get(url)+ if existing_path:+ if os.path.exists(existing_path):+ return+ # Stale cache entry, allow re-download+ del self._session_pdf_urls[url]+# Check if we've already processed this URL in this sessionif url in self._detected_downloads:self.logger.debug(f'[DownloadsWatchdog] Already detected download: {url[:80]}...')diff --cc browser_use/llm/models.pyindex 14d021026,11ebaa799..21ccdfd35--- a/browser_use/llm/models.py+++ b/browser_use/llm/models.py@@@ -64,11 -65,12 +65,16 @@@ google_gemini_2_0_pro: 'BaseChatModelgoogle_gemini_2_5_pro: 'BaseChatModel'google_gemini_2_5_flash: 'BaseChatModel'google_gemini_2_5_flash_lite: 'BaseChatModel'+ mistral_large: 'BaseChatModel'+ mistral_medium: 'BaseChatModel'+ mistral_small: 'BaseChatModel'+ codestral: 'BaseChatModel'+ pixtral_large: 'BaseChatModel'+anthropic_claude_sonnet_4_0: 'BaseChatModel'+anthropic_claude_3_5_sonnet_latest: 'BaseChatModel'+anthropic_claude_3_5_haiku_latest: 'BaseChatModel'+cerebras_llama3_1_8b: 'BaseChatModel'cerebras_llama3_3_70b: 'BaseChatModel'cerebras_gpt_oss_120b: 'BaseChatModel'@@@ -161,13 -177,21 +181,28 @@@ def get_llm_by_name(model_name: str)api_key = os.getenv('GOOGLE_API_KEY')return ChatGoogle(model=model, api_key=api_key)+ # Anthropic Models+ elif provider == 'anthropic':+ from browser_use.llm.anthropic.chat import ChatAnthropic++ api_key = os.getenv('ANTHROPIC_API_KEY')+ return ChatAnthropic(model=model, api_key=api_key)++ # Mistral Models+ elif provider == 'mistral':+ api_key = os.getenv('MISTRAL_API_KEY')+ base_url = os.getenv('MISTRAL_BASE_URL', 'https://api.mistral.ai/v1')+ mistral_map = {+ 'large': 'mistral-large-latest',+ 'medium': 'mistral-medium-latest',+ 'small': 'mistral-small-latest',+ 'codestral': 'codestral-latest',+ 'pixtral-large': 'pixtral-large-latest',+ }+ normalized_model_part = model_part.replace('_', '-')+ resolved_model = mistral_map.get(normalized_model_part, model.replace('_', '-'))+ return ChatMistral(model=resolved_model, api_key=api_key, base_url=base_url)+# OCI Modelselif provider == 'oci':# OCI requires more complex configuration that can't be easily inferred from env vars@@@ -187,7 -211,8 +222,7 @@@return ChatBrowserUse(model=model, api_key=api_key)else:- available_providers = ['openai', 'azure', 'google', 'anthropic', 'oci', 'cerebras', 'bu']- available_providers = ['openai', 'azure', 'google', 'oci', 'cerebras', 'bu']-++ available_providers = ['openai', 'azure', 'google', 'anthropic', 'mistral', 'oci', 'cerebras', 'bu']raise ValueError(f"Unknown provider: '{provider}'. Available providers: {', '.join(available_providers)}")@@@ -263,10 -293,12 +303,16 @@@ __all__ +='google_gemini_2_5_pro','google_gemini_2_5_flash','google_gemini_2_5_flash_lite',+ # Anthropic instances - created on demand+ 'anthropic_claude_sonnet_4_0',+ 'anthropic_claude_3_5_sonnet_latest',+ 'anthropic_claude_3_5_haiku_latest',+ # Mistral instances - created on demand+ 'mistral_large',+ 'mistral_medium',+ 'mistral_small',+ 'codestral',+ 'pixtral_large',# Cerebras instances - created on demand'cerebras_llama3_1_8b','cerebras_llama3_3_70b',diff --cc browser_use/tokens/custom_pricing.pyindex 752d95612,ff8adb147..cab127103--- a/browser_use/tokens/custom_pricing.py+++ b/browser_use/tokens/custom_pricing.py@@@ -27,40 -27,7 +27,43 @@@ CUSTOM_MODEL_PRICING: dict[str, dict[st'max_input_tokens': None, # Not specified'max_output_tokens': None, # Not specified},+ 'claude-sonnet-4-6': {+ 'input_cost_per_token': 3.00 / 1_000_000,+ 'output_cost_per_token': 15.00 / 1_000_000,+ 'cache_read_input_token_cost': 0.30 / 1_000_000,+ 'cache_creation_input_token_cost': 3.75 / 1_000_000,+ 'max_tokens': None,+ 'max_input_tokens': None,+ 'max_output_tokens': None,+ },+ 'anthropic/claude-sonnet-4.6': {+ 'input_cost_per_token': 3.00 / 1_000_000,+ 'output_cost_per_token': 15.00 / 1_000_000,+ 'cache_read_input_token_cost': 0.30 / 1_000_000,+ 'cache_creation_input_token_cost': 3.75 / 1_000_000,+ 'max_tokens': None,+ 'max_input_tokens': None,+ 'max_output_tokens': None,+ },+ 'claude-opus-4-6': {+ 'input_cost_per_token': 5.00 / 1_000_000,+ 'output_cost_per_token': 25.00 / 1_000_000,+ 'cache_read_input_token_cost': 0.50 / 1_000_000,+ 'cache_creation_input_token_cost': 6.25 / 1_000_000,+ 'max_tokens': None,+ 'max_input_tokens': None,+ 'max_output_tokens': None,+ },+ 'anthropic/claude-opus-4.6': {+ 'input_cost_per_token': 5.00 / 1_000_000,+ 'output_cost_per_token': 25.00 / 1_000_000,+ 'cache_read_input_token_cost': 0.50 / 1_000_000,+ 'cache_creation_input_token_cost': 6.25 / 1_000_000,+ 'max_tokens': None,+ 'max_input_tokens': None,+ 'max_output_tokens': None,+ },}+ CUSTOM_MODEL_PRICING['bu-latest'] = CUSTOM_MODEL_PRICING['bu-2-0']++ CUSTOM_MODEL_PRICING['smart'] = CUSTOM_MODEL_PRICING['bu-2-0']diff --cc browser_use/tokens/service.pyindex 901a08552,928c091c1..349e51b89--- a/browser_use/tokens/service.py+++ b/browser_use/tokens/service.py@@@ -174,10 -193,11 +189,15 @@@ class TokenCostcache_creation_input_token_cost=data.get('cache_creation_input_token_cost'),)+ # Ensure we're initialized before checking remote LiteLLM pricing.+ if not self._initialized:+ await self.initialize()++ if is_openrouter_pricing_model(model_name):+ openrouter_pricing = await get_openrouter_model_pricing(model_name)+ if openrouter_pricing is not None:+ return openrouter_pricing+# Map model name to LiteLLM model name if neededlitellm_model_name = MODEL_TO_LITELLM.get(model_name, model_name)diff --cc browser_use/utils.pyindex 5e878bc9a,a949aa77d..d768066f4--- a/browser_use/utils.py+++ b/browser_use/utils.py@@@ -24,41 -24,61 +24,86 @@@ URL_PATTERN = re.compile(r'https?://[^\logger = logging.getLogger(__name__)++def is_placeholder_url(url: str) -> bool:+ """Return True for mock placeholder hostnames like https://XXX.XX."""+ parsed_url = urlparse(url if '://' in url else f'https://{url}')+ hostname = (parsed_url.hostname or '').strip('.').lower()+ if not hostname:+ return False++ labels = [label for label in hostname.split('.') if label]+ if labels and labels[0] == 'www':+ labels = labels[1:]++ return len(labels) >= 2 and all(re.fullmatch(r'x+', label) for label in labels)+++def sanitize_url_candidate(url: str) -> str:+ """Normalize a URL candidate captured from prose before auto-navigation."""+ candidate = url.strip()+ # Some benchmark tasks arrive with escaped newlines in prose, e.g.+ # "https://example.com/search.\\n2. Next step". Those are task text,+ # not part of the URL.+ candidate = re.split(r'\\[nrt]', candidate, maxsplit=1)[0]+ return re.sub(r'[.,;:!?()\[\]]+$', '', candidate)++- # Import error types - these may need to be adjusted based on actual import paths- try:- from openai import BadRequestError as OpenAIBadRequestError- except ImportError:- OpenAIBadRequestError = None+ # Lazy import for error types+ # Use sentinel to avoid retrying import when package is not installed+ _IMPORT_NOT_FOUND: type = type('_ImportNotFound', (), {})+ _openai_bad_request_error: type | None = None+ _groq_bad_request_error: type | None = None- try:- from groq import BadRequestError as GroqBadRequestError # type: ignore[import-not-found]- except ImportError:- GroqBadRequestError = None++ def collect_sensitive_data_values(sensitive_data: dict[str, str | dict[str, str]] | None) -> dict[str, str]:+ """Flatten legacy and domain-scoped sensitive data into placeholder -> value mappings."""+ if not sensitive_data:+ return {}++ sensitive_values: dict[str, str] = {}+ for key_or_domain, content in sensitive_data.items():+ if isinstance(content, dict):+ for key, val in content.items():+ if val:+ sensitive_values[key] = val+ elif content:+ sensitive_values[key_or_domain] = content++ return sensitive_values+++ def redact_sensitive_string(value: str, sensitive_values: dict[str, str]) -> str:+ """Replace sensitive values with placeholders, longest matches first to avoid partial leaks."""+ for key, secret in sorted(sensitive_values.items(), key=lambda item: len(item[1]), reverse=True):+ value = value.replace(secret, f'<secret>{key}</secret>')+ return value+++ def _get_openai_bad_request_error() -> type | None:+ """Lazy loader for OpenAI BadRequestError."""+ global _openai_bad_request_error+ if _openai_bad_request_error is None:+ try:+ from openai import BadRequestError++ _openai_bad_request_error = BadRequestError+ except ImportError:+ _openai_bad_request_error = _IMPORT_NOT_FOUND+ return _openai_bad_request_error if _openai_bad_request_error is not _IMPORT_NOT_FOUND else None+++ def _get_groq_bad_request_error() -> type | None:+ """Lazy loader for Groq BadRequestError."""+ global _groq_bad_request_error+ if _groq_bad_request_error is None:+ try:+ from groq import BadRequestError # type: ignore[import-not-found]++ _groq_bad_request_error = BadRequestError+ except ImportError:+ _groq_bad_request_error = _IMPORT_NOT_FOUND+ return _groq_bad_request_error if _groq_bad_request_error is not _IMPORT_NOT_FOUND else None# Global flag to prevent duplicate exit messages