🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
enhance:hybrid browser (#2825)
Puzhen Zhang83419049e3e5
diff --git a/camel/societies/workforce/task_channel.py b/camel/societies/workforce/task_channel.pyindex 84bc10cd9..81026431d 100644--- a/camel/societies/workforce/task_channel.py+++ b/camel/societies/workforce/task_channel.py@@ -91,11 +91,14 @@ class TaskChannel:"""async with self._condition:while True:- for packet in self._task_dict.values():+ for task_id, packet in list(self._task_dict.items()):if packet.publisher_id != publisher_id:continueif packet.status != PacketStatus.RETURNED:continue+ # Remove the task to prevent returning it again+ del self._task_dict[task_id]+ self._condition.notify_all()return packet.taskawait self._condition.wait()diff --git a/camel/toolkits/hybrid_browser_toolkit/actions.py b/camel/toolkits/hybrid_browser_toolkit/actions.pyindex 7b87e898f..2630ab07e 100644--- a/camel/toolkits/hybrid_browser_toolkit/actions.py+++ b/camel/toolkits/hybrid_browser_toolkit/actions.py@@ -14,6 +14,8 @@import asynciofrom typing import TYPE_CHECKING, Any, Dict, Optional+from .config_loader import ConfigLoader+if TYPE_CHECKING:from playwright.async_api import Page@@ -21,15 +23,24 @@ if TYPE_CHECKING:class ActionExecutor:r"""Executes high-level actions (click, type …) on a Playwright Page."""- # Configuration constants- DEFAULT_TIMEOUT = 5000 # 5 seconds- SHORT_TIMEOUT = 2000 # 2 seconds- MAX_SCROLL_AMOUNT = 5000 # Maximum scroll distance in pixels-- def __init__(self, page: "Page", session: Optional[Any] = None):+ def __init__(+ self,+ page: "Page",+ session: Optional[Any] = None,+ default_timeout: Optional[int] = None,+ short_timeout: Optional[int] = None,+ max_scroll_amount: Optional[int] = None,+ ):self.page = pageself.session = session # HybridBrowserSession instance+ # Configure timeouts using the config file with optional overrides+ self.default_timeout = ConfigLoader.get_action_timeout(default_timeout)+ self.short_timeout = ConfigLoader.get_short_timeout(short_timeout)+ self.max_scroll_amount = ConfigLoader.get_max_scroll_amount(+ max_scroll_amount+ )+# ------------------------------------------------------------------# Public helpers# ------------------------------------------------------------------@@ -139,7 +150,7 @@ class ActionExecutor:try:if self.session:async with self.page.context.expect_page(- timeout=self.SHORT_TIMEOUT+ timeout=self.short_timeout) as new_page_info:await element.click(modifiers=["ControlOrMeta"])new_page = await new_page_info.value@@ -188,7 +199,7 @@ class ActionExecutor:# Fallback to normal force click if ctrl+click failstry:- await element.click(force=True, timeout=self.DEFAULT_TIMEOUT)+ await element.click(force=True, timeout=self.default_timeout)details["click_method"] = "playwright_force_click"return {"message": f"Fallback clicked element: {found_selector}",@@ -224,7 +235,7 @@ class ActionExecutor:}try:- await self.page.fill(target, text, timeout=self.SHORT_TIMEOUT)+ await self.page.fill(target, text, timeout=self.short_timeout)return {"message": f"Typed '{text}' into {target}","details": details,@@ -254,7 +265,7 @@ class ActionExecutor:try:await self.page.select_option(- target, value, timeout=self.DEFAULT_TIMEOUT+ target, value, timeout=self.default_timeout)return {"message": f"Selected '{value}' in {target}",@@ -283,7 +294,7 @@ class ActionExecutor:details["wait_type"] = "selector"details["selector"] = selawait self.page.wait_for_selector(- sel, timeout=self.DEFAULT_TIMEOUT+ sel, timeout=self.default_timeout)return {"message": f"Waited for {sel}", "details": details}return {@@ -303,7 +314,7 @@ class ActionExecutor:target = f"[aria-ref='{ref}']"details = {"ref": ref, "target": target}- await self.page.wait_for_selector(target, timeout=self.DEFAULT_TIMEOUT)+ await self.page.wait_for_selector(target, timeout=self.default_timeout)txt = await self.page.text_content(target)details["extracted_text"] = txt@@ -337,9 +348,9 @@ class ActionExecutor:# Safely convert amount to integer and clamp to reasonable rangeamount_int = int(amount)amount_int = max(- -self.MAX_SCROLL_AMOUNT,- min(self.MAX_SCROLL_AMOUNT, amount_int),- ) # Clamp to MAX_SCROLL_AMOUNT range+ -self.max_scroll_amount,+ min(self.max_scroll_amount, amount_int),+ ) # Clamp to max_scroll_amount rangedetails["actual_amount"] = amount_intexcept (ValueError, TypeError):return {@@ -366,7 +377,6 @@ class ActionExecutor:# Press Enter on whatever element currently has focusawait self.page.keyboard.press("Enter")- await asyncio.sleep(0.3)return {"message": "Pressed Enter on focused element","details": details,@@ -378,13 +388,13 @@ class ActionExecutor:try:# Wait for basic DOM content loadingawait self.page.wait_for_load_state(- 'domcontentloaded', timeout=self.SHORT_TIMEOUT+ 'domcontentloaded', timeout=self.short_timeout)# Try to wait for network idle brieflytry:await self.page.wait_for_load_state(- 'networkidle', timeout=self.SHORT_TIMEOUT+ 'networkidle', timeout=self.short_timeout)except Exception:pass # Network idle is optionaldiff --git a/camel/toolkits/hybrid_browser_toolkit/agent.py b/camel/toolkits/hybrid_browser_toolkit/agent.pyindex cd402d2d8..d8c7ff6d4 100644--- a/camel/toolkits/hybrid_browser_toolkit/agent.py+++ b/camel/toolkits/hybrid_browser_toolkit/agent.py@@ -81,9 +81,15 @@ what was accomplishedheadless: bool = False,stealth: bool = False,model_backend: Optional[BaseModelBackend] = None,+ default_timeout: Optional[int] = None,+ short_timeout: Optional[int] = None,):self._session = HybridBrowserSession(- headless=headless, user_data_dir=user_data_dir, stealth=stealth+ headless=headless,+ user_data_dir=user_data_dir,+ stealth=stealth,+ default_timeout=default_timeout,+ short_timeout=short_timeout,)from camel.agents import ChatAgentdiff --git a/camel/toolkits/hybrid_browser_toolkit/browser_session.py b/camel/toolkits/hybrid_browser_toolkit/browser_session.pyindex d918354f8..c40052763 100644--- a/camel/toolkits/hybrid_browser_toolkit/browser_session.py+++ b/camel/toolkits/hybrid_browser_toolkit/browser_session.py@@ -19,8 +19,8 @@ from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Tuplefrom camel.logger import get_loggerfrom .actions import ActionExecutor+from .config_loader import ConfigLoaderfrom .snapshot import PageSnapshot-from .stealth_config import StealthConfigif TYPE_CHECKING:from playwright.async_api import (@@ -44,10 +44,6 @@ class HybridBrowserSession:This class is a singleton per event-loop and session-id combination."""- # Configuration constants- DEFAULT_NAVIGATION_TIMEOUT = 10000 # 10 seconds- NETWORK_IDLE_TIMEOUT = 5000 # 5 seconds-# Class-level registry for singleton instances# Format: {(loop_id, session_id): HybridBrowserSession}_instances: ClassVar[Dict[Tuple[Any, str], "HybridBrowserSession"]] = {}@@ -63,6 +59,10 @@ class HybridBrowserSession:user_data_dir: Optional[str] = None,stealth: bool = False,session_id: Optional[str] = None,+ default_timeout: Optional[int] = None,+ short_timeout: Optional[int] = None,+ navigation_timeout: Optional[int] = None,+ network_idle_timeout: Optional[int] = None,) -> "HybridBrowserSession":# Create a unique key for this event loop and session combination# We defer the event loop lookup to avoid issues with creation@@ -75,6 +75,10 @@ class HybridBrowserSession:"user_data_dir": user_data_dir,"stealth": stealth,"session_id": session_id,+ "default_timeout": default_timeout,+ "short_timeout": short_timeout,+ "navigation_timeout": navigation_timeout,+ "network_idle_timeout": network_idle_timeout,}return instance@@ -126,6 +130,10 @@ class HybridBrowserSession:user_data_dir: Optional[str] = None,stealth: bool = False,session_id: Optional[str] = None,+ default_timeout: Optional[int] = None,+ short_timeout: Optional[int] = None,+ navigation_timeout: Optional[int] = None,+ network_idle_timeout: Optional[int] = None,):if self._initialized:return@@ -136,12 +144,27 @@ class HybridBrowserSession:self._stealth = stealthself._session_id = session_id or "default"+ # Store timeout configuration for ActionExecutor instances and+ # browser operations+ self._default_timeout = default_timeout+ self._short_timeout = short_timeout+ self._navigation_timeout = ConfigLoader.get_navigation_timeout(+ navigation_timeout+ )+ self._network_idle_timeout = ConfigLoader.get_network_idle_timeout(+ network_idle_timeout+ )+# Initialize _creation_params to fix linter errorself._creation_params = {"headless": headless,"user_data_dir": user_data_dir,"stealth": stealth,"session_id": session_id,+ "default_timeout": default_timeout,+ "short_timeout": short_timeout,+ "navigation_timeout": navigation_timeout,+ "network_idle_timeout": network_idle_timeout,}self._playwright: Optional[Playwright] = None@@ -164,7 +187,8 @@ class HybridBrowserSession:self._stealth_config: Optional[Dict[str, Any]] = Noneif self._stealth:self._stealth_script = self._load_stealth_script()- self._stealth_config = StealthConfig.get_all_config()+ stealth_config_class = ConfigLoader.get_stealth_config()+ self._stealth_config = stealth_config_class.get_stealth_config()def _load_stealth_script(self) -> str:r"""Load the stealth JavaScript script from file."""@@ -231,9 +255,7 @@ class HybridBrowserSession:# Navigate if URL providedif url:try:- await new_page.goto(- url, timeout=self.DEFAULT_NAVIGATION_TIMEOUT- )+ await new_page.goto(url, timeout=self._navigation_timeout)await new_page.wait_for_load_state('domcontentloaded')except Exception as e:logger.warning(f"Failed to navigate new tab to {url}: {e}")@@ -319,7 +341,12 @@ class HybridBrowserSession:await self._page.bring_to_front()# Update executor and snapshot for new tab- self.executor = ActionExecutor(self._page, self)+ self.executor = ActionExecutor(+ self._page,+ self,+ default_timeout=self._default_timeout,+ short_timeout=self._short_timeout,+ )self.snapshot = PageSnapshot(self._page)logger.info(f"Switched to tab {tab_index}")@@ -506,14 +533,17 @@ class HybridBrowserSession:logger.warning(f"Failed to apply stealth script: {e}")# Set up timeout for navigation- self._page.set_default_navigation_timeout(- self.DEFAULT_NAVIGATION_TIMEOUT- )- self._page.set_default_timeout(self.DEFAULT_NAVIGATION_TIMEOUT)+ self._page.set_default_navigation_timeout(self._navigation_timeout)+ self._page.set_default_timeout(self._navigation_timeout)# Initialize utilitiesself.snapshot = PageSnapshot(self._page)- self.executor = ActionExecutor(self._page, self)+ self.executor = ActionExecutor(+ self._page,+ self,+ default_timeout=self._default_timeout,+ short_timeout=self._short_timeout,+ )self._current_tab_index = 0logger.info("Browser session initialized successfully")@@ -579,7 +609,7 @@ class HybridBrowserSession:await page.close()logger.debug(f"Closed page: "- f"{page.url if hasattr(page, 'url') else 'unknown'}" # noqa: E501+ f"{page.url if hasattr(page, 'url') else 'unknown'}" # noqa:E501)except Exception as e:logger.warning(f"Error closing page: {e}")@@ -668,13 +698,13 @@ class HybridBrowserSession:await self.ensure_browser()page = await self.get_page()- await page.goto(url, timeout=self.DEFAULT_NAVIGATION_TIMEOUT)+ await page.goto(url, timeout=self._navigation_timeout)await page.wait_for_load_state('domcontentloaded')# Try to wait for network idletry:await page.wait_for_load_state(- 'networkidle', timeout=self.NETWORK_IDLE_TIMEOUT+ 'networkidle', timeout=self._network_idle_timeout)except Exception:logger.debug("Network idle timeout - continuing anyway")diff --git a/camel/toolkits/hybrid_browser_toolkit/config_loader.py b/camel/toolkits/hybrid_browser_toolkit/config_loader.pynew file mode 100644index 000000000..bd0d78a4e--- /dev/null+++ b/camel/toolkits/hybrid_browser_toolkit/config_loader.py@@ -0,0 +1,447 @@+#!/usr/bin/env python3+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++"""+Configuration for browser automation including stealth mode and timeouts.++This module contains all the configuration needed to make the browser+appear as a regular user browser and configure action timeouts.+"""++import os+from typing import Any, Dict, List, Optional+++class BrowserConfig:+ r"""Configuration class for browser settings including stealth mode and+ timeouts."""++ # Default timeout values (in milliseconds)+ DEFAULT_ACTION_TIMEOUT = 3000+ DEFAULT_SHORT_TIMEOUT = 1000+ DEFAULT_NAVIGATION_TIMEOUT = 10000+ DEFAULT_NETWORK_IDLE_TIMEOUT = 5000+ DEFAULT_SCREENSHOT_TIMEOUT = 15000+ DEFAULT_PAGE_STABILITY_TIMEOUT = 1500+ DEFAULT_DOM_CONTENT_LOADED_TIMEOUT = 5000++ # Default action limits+ DEFAULT_MAX_SCROLL_AMOUNT = 5000 # Maximum scroll distance in pixels++ @staticmethod+ def get_timeout_config() -> Dict[str, int]:+ r"""Get timeout configuration with environment variable support.++ Returns:+ Dict[str, int]: Timeout configuration in milliseconds.+ """+ return {+ 'default_timeout': int(+ os.getenv(+ 'HYBRID_BROWSER_DEFAULT_TIMEOUT',+ BrowserConfig.DEFAULT_ACTION_TIMEOUT,+ )+ ),+ 'short_timeout': int(+ os.getenv(+ 'HYBRID_BROWSER_SHORT_TIMEOUT',+ BrowserConfig.DEFAULT_SHORT_TIMEOUT,+ )+ ),+ 'navigation_timeout': int(+ os.getenv(+ 'HYBRID_BROWSER_NAVIGATION_TIMEOUT',+ BrowserConfig.DEFAULT_NAVIGATION_TIMEOUT,+ )+ ),+ 'network_idle_timeout': int(+ os.getenv(+ 'HYBRID_BROWSER_NETWORK_IDLE_TIMEOUT',+ BrowserConfig.DEFAULT_NETWORK_IDLE_TIMEOUT,+ )+ ),+ 'screenshot_timeout': int(+ os.getenv(+ 'HYBRID_BROWSER_SCREENSHOT_TIMEOUT',+ BrowserConfig.DEFAULT_SCREENSHOT_TIMEOUT,+ )+ ),+ 'page_stability_timeout': int(+ os.getenv(+ 'HYBRID_BROWSER_PAGE_STABILITY_TIMEOUT',+ BrowserConfig.DEFAULT_PAGE_STABILITY_TIMEOUT,+ )+ ),+ 'dom_content_loaded_timeout': int(+ os.getenv(+ 'HYBRID_BROWSER_DOM_CONTENT_LOADED_TIMEOUT',+ BrowserConfig.DEFAULT_DOM_CONTENT_LOADED_TIMEOUT,+ )+ ),+ }++ @staticmethod+ def get_action_limits() -> Dict[str, int]:+ r"""Get action limits configuration with environment variable support.++ Returns:+ Dict[str, int]: Action limits configuration.+ """+ return {+ 'max_scroll_amount': int(+ os.getenv(+ 'HYBRID_BROWSER_MAX_SCROLL_AMOUNT',+ BrowserConfig.DEFAULT_MAX_SCROLL_AMOUNT,+ )+ ),+ }++ @staticmethod+ def get_action_timeout(override: Optional[int] = None) -> int:+ r"""Get action timeout with optional override.++ Args:+ override: Optional timeout override value in milliseconds.++ Returns:+ int: Timeout value in milliseconds.+ """+ if override is not None:+ return override+ return BrowserConfig.get_timeout_config()['default_timeout']++ @staticmethod+ def get_short_timeout(override: Optional[int] = None) -> int:+ r"""Get short timeout with optional override.++ Args:+ override: Optional timeout override value in milliseconds.++ Returns:+ int: Timeout value in milliseconds.+ """+ if override is not None:+ return override+ return BrowserConfig.get_timeout_config()['short_timeout']++ @staticmethod+ def get_navigation_timeout(override: Optional[int] = None) -> int:+ r"""Get navigation timeout with optional override.++ Args:+ override: Optional timeout override value in milliseconds.++ Returns:+ int: Timeout value in milliseconds.+ """+ if override is not None:+ return override+ return BrowserConfig.get_timeout_config()['navigation_timeout']++ @staticmethod+ def get_network_idle_timeout(override: Optional[int] = None) -> int:+ r"""Get network idle timeout with optional override.++ Args:+ override: Optional timeout override value in milliseconds.++ Returns:+ int: Timeout value in milliseconds.+ """+ if override is not None:+ return override+ return BrowserConfig.get_timeout_config()['network_idle_timeout']++ @staticmethod+ def get_max_scroll_amount(override: Optional[int] = None) -> int:+ r"""Get maximum scroll amount with optional override.++ Args:+ override: Optional scroll amount override value in pixels.++ Returns:+ int: Maximum scroll amount in pixels.+ """+ if override is not None:+ return override+ return BrowserConfig.get_action_limits()['max_scroll_amount']++ @staticmethod+ def get_screenshot_timeout(override: Optional[int] = None) -> int:+ r"""Get screenshot timeout with optional override.++ Args:+ override: Optional timeout override value in milliseconds.++ Returns:+ int: Timeout value in milliseconds.+ """+ if override is not None:+ return override+ return BrowserConfig.get_timeout_config()['screenshot_timeout']++ @staticmethod+ def get_page_stability_timeout(override: Optional[int] = None) -> int:+ r"""Get page stability timeout with optional override.++ Args:+ override: Optional timeout override value in milliseconds.++ Returns:+ int: Timeout value in milliseconds.+ """+ if override is not None:+ return override+ return BrowserConfig.get_timeout_config()['page_stability_timeout']++ @staticmethod+ def get_dom_content_loaded_timeout(override: Optional[int] = None) -> int:+ r"""Get DOM content loaded timeout with optional override.++ Args:+ override: Optional timeout override value in milliseconds.++ Returns:+ int: Timeout value in milliseconds.+ """+ if override is not None:+ return override+ return BrowserConfig.get_timeout_config()['dom_content_loaded_timeout']++ @staticmethod+ def get_launch_args() -> List[str]:+ r"""Get Chrome launch arguments for stealth mode.++ Returns:+ List[str]: Chrome command line arguments to avoid detection.+ """+ return [+ '--disable-blink-features=AutomationControlled',+ '--disable-features=VizDisplayCompositor',+ '--disable-ipc-flooding-protection',+ '--disable-renderer-backgrounding',+ '--disable-backgrounding-occluded-windows',+ '--disable-dev-shm-usage',+ '--disable-extensions',+ '--disable-plugins',+ '--disable-default-apps',+ '--disable-sync',+ '--no-default-browser-check',+ '--no-first-run',+ '--no-sandbox',+ '--disable-setuid-sandbox',+ '--disable-web-security',+ '--disable-features=TranslateUI',+ '--disable-features=BlinkGenPropertyTrees',+ '--disable-component-extensions-with-background-pages',+ ]++ @staticmethod+ def get_context_options() -> Dict[str, Any]:+ r"""Get browser context options for stealth mode.++ Returns:+ Dict[str, Any]: Browser context configuration options.+ """+ return {+ 'user_agent': (+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '+ 'AppleWebKit/537.36 (KHTML, like Gecko) '+ 'Chrome/131.0.0.0 Safari/537.36'+ ),+ 'viewport': {'width': 1920, 'height': 1080},+ 'locale': 'en-US',+ 'timezone_id': 'America/New_York',+ 'geolocation': {'latitude': 40.7128, 'longitude': -74.0060},+ 'permissions': ['geolocation'],+ }++ @staticmethod+ def get_http_headers() -> Dict[str, str]:+ r"""Get HTTP headers for stealth mode.++ Returns:+ Dict[str, str]: HTTP headers to appear more like a real browser.+ """+ return {+ 'Accept': (+ 'text/html,application/xhtml+xml,application/xml;q=0.9,'+ 'image/avif,image/webp,image/apng,*/*;q=0.8,'+ 'application/signed-exchange;v=b3;q=0.7'+ ),+ 'Accept-Language': 'en-US,en;q=0.9',+ 'Accept-Encoding': 'gzip, deflate, br, zstd',+ 'Cache-Control': 'max-age=0',+ 'Sec-Ch-Ua': (+ '"Google Chrome";v="131", "Chromium";v="131", '+ '"Not=A?Brand";v="24"'+ ),+ 'Sec-Ch-Ua-Mobile': '?0',+ 'Sec-Ch-Ua-Platform': '"Windows"',+ 'Sec-Fetch-Dest': 'document',+ 'Sec-Fetch-Mode': 'navigate',+ 'Sec-Fetch-Site': 'none',+ 'Sec-Fetch-User': '?1',+ 'Upgrade-Insecure-Requests': '1',+ }++ @staticmethod+ def get_stealth_config() -> Dict[str, Any]:+ r"""Get stealth configuration.++ Returns:+ Dict[str, Any]: Complete stealth configuration.+ """+ return {+ 'launch_args': BrowserConfig.get_launch_args(),+ 'context_options': BrowserConfig.get_context_options(),+ 'http_headers': BrowserConfig.get_http_headers(),+ }++ @staticmethod+ def get_all_config() -> Dict[str, Any]:+ r"""Get all browser configuration including stealth, timeouts,+ and action limits.++ Returns:+ Dict[str, Any]: Complete browser configuration.+ """+ return {+ 'timeouts': BrowserConfig.get_timeout_config(),+ 'action_limits': BrowserConfig.get_action_limits(),+ 'stealth': BrowserConfig.get_stealth_config(),+ }+++# Legacy ConfigLoader class for compatibility (now just wraps BrowserConfig)+class ConfigLoader:+ r"""Legacy wrapper for BrowserConfig - maintained for backward+ compatibility."""++ @classmethod+ def get_browser_config(cls):+ r"""Get the BrowserConfig class."""+ return BrowserConfig++ @classmethod+ def get_stealth_config(cls):+ r"""Get the StealthConfig class (alias)."""+ return BrowserConfig # StealthConfig is an alias for BrowserConfig++ @classmethod+ def get_timeout_config(cls) -> Dict[str, int]:+ r"""Get timeout configuration."""+ return BrowserConfig.get_timeout_config()++ @classmethod+ def get_action_timeout(cls, override: Optional[int] = None) -> int:+ r"""Get action timeout with optional override."""+ return BrowserConfig.get_action_timeout(override)++ @classmethod+ def get_short_timeout(cls, override: Optional[int] = None) -> int:+ r"""Get short timeout with optional override."""+ return BrowserConfig.get_short_timeout(override)++ @classmethod+ def get_navigation_timeout(cls, override: Optional[int] = None) -> int:+ r"""Get navigation timeout with optional override."""+ return BrowserConfig.get_navigation_timeout(override)++ @classmethod+ def get_network_idle_timeout(cls, override: Optional[int] = None) -> int:+ r"""Get network idle timeout with optional override."""+ return BrowserConfig.get_network_idle_timeout(override)++ @classmethod+ def get_max_scroll_amount(cls, override: Optional[int] = None) -> int:+ r"""Get maximum scroll amount with optional override."""+ return BrowserConfig.get_max_scroll_amount(override)++ @classmethod+ def get_screenshot_timeout(cls, override: Optional[int] = None) -> int:+ r"""Get screenshot timeout with optional override."""+ return BrowserConfig.get_screenshot_timeout(override)++ @classmethod+ def get_page_stability_timeout(cls, override: Optional[int] = None) -> int:+ r"""Get page stability timeout with optional override."""+ return BrowserConfig.get_page_stability_timeout(override)++ @classmethod+ def get_dom_content_loaded_timeout(+ cls, override: Optional[int] = None+ ) -> int:+ r"""Get DOM content loaded timeout with optional override."""+ return BrowserConfig.get_dom_content_loaded_timeout(override)+++# Backward compatibility aliases and convenience functions+StealthConfig = BrowserConfig+++def get_browser_config():+ r"""Get BrowserConfig class."""+ return BrowserConfig+++def get_stealth_config():+ r"""Get StealthConfig class."""+ return BrowserConfig+++def get_timeout_config() -> Dict[str, int]:+ r"""Get timeout configuration."""+ return BrowserConfig.get_timeout_config()+++def get_action_timeout(override: Optional[int] = None) -> int:+ r"""Get action timeout with optional override."""+ return BrowserConfig.get_action_timeout(override)+++def get_short_timeout(override: Optional[int] = None) -> int:+ r"""Get short timeout with optional override."""+ return BrowserConfig.get_short_timeout(override)+++def get_navigation_timeout(override: Optional[int] = None) -> int:+ r"""Get navigation timeout with optional override."""+ return BrowserConfig.get_navigation_timeout(override)+++def get_network_idle_timeout(override: Optional[int] = None) -> int:+ r"""Get network idle timeout with optional override."""+ return BrowserConfig.get_network_idle_timeout(override)+++def get_max_scroll_amount(override: Optional[int] = None) -> int:+ r"""Get maximum scroll amount with optional override."""+ return BrowserConfig.get_max_scroll_amount(override)+++def get_screenshot_timeout(override: Optional[int] = None) -> int:+ r"""Get screenshot timeout with optional override."""+ return BrowserConfig.get_screenshot_timeout(override)+++def get_page_stability_timeout(override: Optional[int] = None) -> int:+ r"""Get page stability timeout with optional override."""+ return BrowserConfig.get_page_stability_timeout(override)+++def get_dom_content_loaded_timeout(override: Optional[int] = None) -> int:+ r"""Get DOM content loaded timeout with optional override."""+ return BrowserConfig.get_dom_content_loaded_timeout(override)diff --git a/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py b/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.pyindex 2f9c36735..176e83e04 100644--- a/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py+++ b/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py@@ -32,6 +32,7 @@ from camel.utils.tool_result import ToolResultfrom .agent import PlaywrightLLMAgentfrom .browser_session import HybridBrowserSession+from .config_loader import ConfigLoaderlogger = get_logger(__name__)@@ -47,11 +48,6 @@ class HybridBrowserToolkit(BaseToolkit):interactive elements."""- # Configuration constants- DEFAULT_SCREENSHOT_TIMEOUT = 60000 # 60 seconds for screenshots- PAGE_STABILITY_TIMEOUT = 3000 # 3 seconds for DOM stability- NETWORK_IDLE_TIMEOUT = 2000 # 2 seconds for network idle-# Default tool list - core browser functionalityDEFAULT_TOOLS: ClassVar[List[str]] = ["open_browser",@@ -98,6 +94,13 @@ class HybridBrowserToolkit(BaseToolkit):browser_log_to_file: bool = False,session_id: Optional[str] = None,default_start_url: str = "https://google.com/",+ default_timeout: Optional[int] = None,+ short_timeout: Optional[int] = None,+ navigation_timeout: Optional[int] = None,+ network_idle_timeout: Optional[int] = None,+ screenshot_timeout: Optional[int] = None,+ page_stability_timeout: Optional[int] = None,+ dom_content_loaded_timeout: Optional[int] = None,) -> None:r"""Initialize the HybridBrowserToolkit.@@ -140,14 +143,71 @@ class HybridBrowserToolkit(BaseToolkit):default_start_url (str): The default URL to navigate to whenopen_browser() is called without a start_url parameter or withNone. Defaults to `"https://google.com/"`.+ default_timeout (Optional[int]): Default timeout in milliseconds+ for browser actions. If None, uses environment variable+ HYBRID_BROWSER_DEFAULT_TIMEOUT or defaults to 3000ms.+ Defaults to `None`.+ short_timeout (Optional[int]): Short timeout in milliseconds+ for quick browser actions. If None, uses environment variable+ HYBRID_BROWSER_SHORT_TIMEOUT or defaults to 1000ms.+ Defaults to `None`.+ navigation_timeout (Optional[int]): Custom navigation timeout in+ milliseconds.+ If None, uses environment variable+ HYBRID_BROWSER_NAVIGATION_TIMEOUT or defaults to 10000ms.+ Defaults to `None`.+ network_idle_timeout (Optional[int]): Custom network idle+ timeout in milliseconds.+ If None, uses environment variable+ HYBRID_BROWSER_NETWORK_IDLE_TIMEOUT or defaults to 5000ms.+ Defaults to `None`.+ screenshot_timeout (Optional[int]): Custom screenshot timeout in+ milliseconds.+ If None, uses environment variable+ HYBRID_BROWSER_SCREENSHOT_TIMEOUT or defaults to 15000ms.+ Defaults to `None`.+ page_stability_timeout (Optional[int]): Custom page stability+ timeout in milliseconds.+ If None, uses environment variable+ HYBRID_BROWSER_PAGE_STABILITY_TIMEOUT or defaults to 1500ms.+ Defaults to `None`.+ dom_content_loaded_timeout (Optional[int]): Custom DOM content+ loaded timeout in milliseconds.+ If None, uses environment variable+ HYBRID_BROWSER_DOM_CONTENT_LOADED_TIMEOUT or defaults to+ 5000ms.+ Defaults to `None`."""super().__init__()self._headless = headlessself._user_data_dir = user_data_dir- self.web_agent_model = web_agent_model- self.cache_dir = cache_dir- self.default_start_url = default_start_url- os.makedirs(self.cache_dir, exist_ok=True)+ self._stealth = stealth+ self._web_agent_model = web_agent_model+ self._cache_dir = cache_dir+ self._browser_log_to_file = browser_log_to_file+ self._default_start_url = default_start_url+ self._session_id = session_id or "default"++ # Store timeout configuration+ self._default_timeout = default_timeout+ self._short_timeout = short_timeout+ self._navigation_timeout = ConfigLoader.get_navigation_timeout(+ navigation_timeout+ )+ self._network_idle_timeout = ConfigLoader.get_network_idle_timeout(+ network_idle_timeout+ )+ self._screenshot_timeout = ConfigLoader.get_screenshot_timeout(+ screenshot_timeout+ )+ self._page_stability_timeout = ConfigLoader.get_page_stability_timeout(+ page_stability_timeout+ )+ self._dom_content_loaded_timeout = (+ ConfigLoader.get_dom_content_loaded_timeout(+ dom_content_loaded_timeout+ )+ )# Logging configuration - fixed values for simplicityself.enable_action_logging = True@@ -203,6 +263,8 @@ class HybridBrowserToolkit(BaseToolkit):user_data_dir=user_data_dir,stealth=stealth,session_id=session_id,+ default_timeout=default_timeout,+ short_timeout=short_timeout,)# Use the session directly - singleton logic is handled in# ensure_browser@@ -210,6 +272,21 @@ class HybridBrowserToolkit(BaseToolkit):self._agent: Optional[PlaywrightLLMAgent] = Noneself._unified_script = self._load_unified_analyzer()+ @property+ def web_agent_model(self) -> Optional[BaseModelBackend]:+ """Get the web agent model."""+ return self._web_agent_model++ @web_agent_model.setter+ def web_agent_model(self, value: Optional[BaseModelBackend]) -> None:+ """Set the web agent model."""+ self._web_agent_model = value++ @property+ def cache_dir(self) -> str:+ """Get the cache directory."""+ return self._cache_dir+def __del__(self):r"""Cleanup browser resources on garbage collection."""try:@@ -437,29 +514,29 @@ class HybridBrowserToolkit(BaseToolkit):async def _wait_for_page_stability(self):r"""Wait for page to become stable after actions that might trigger- updates.+ updates. Optimized with shorter timeouts."""page = await self._require_page()import asynciotry:- # Wait for DOM content to be loaded+ # Wait for DOM content to be loaded (reduced timeout)await page.wait_for_load_state(- 'domcontentloaded', timeout=self.PAGE_STABILITY_TIMEOUT+ 'domcontentloaded', timeout=self._page_stability_timeout)logger.debug("DOM content loaded")- # Try to wait for network idle (important for AJAX/SPA)+ # Try to wait for network idle with shorter timeouttry:await page.wait_for_load_state(- 'networkidle', timeout=self.NETWORK_IDLE_TIMEOUT+ 'networkidle', timeout=self._network_idle_timeout)logger.debug("Network idle achieved")except Exception:logger.debug("Network idle timeout - continuing anyway")- # Additional small delay for JavaScript execution- await asyncio.sleep(0.5)+ # Reduced delay for JavaScript execution+ await asyncio.sleep(0.2) # Reduced from 0.5slogger.debug("Page stability wait completed")except Exception as e:@@ -467,24 +544,92 @@ class HybridBrowserToolkit(BaseToolkit):f"Page stability wait failed: {e} - continuing anyway")- async def _get_unified_analysis(self) -> Dict[str, Any]:- r"""Get unified analysis data from the page."""+ async def _get_unified_analysis(+ self, max_retries: int = 3+ ) -> Dict[str, Any]:+ r"""Get unified analysis data from the page with retry mechanism for+ navigation issues."""page = await self._require_page()- try:- if not self._unified_script:- logger.error("Unified analyzer script not loaded")- return {"elements": {}, "metadata": {"elementCount": 0}}- result = await page.evaluate(self._unified_script)+ for attempt in range(max_retries):+ try:+ if not self._unified_script:+ logger.error("Unified analyzer script not loaded")+ return {"elements": {}, "metadata": {"elementCount": 0}}++ # Wait for DOM stability before each attempt (with optimized+ # timeout)+ try:+ await page.wait_for_load_state(+ 'domcontentloaded',+ timeout=self._dom_content_loaded_timeout,+ )+ except Exception:+ # Don't fail if DOM wait times out+ pass++ result = await page.evaluate(self._unified_script)++ if not isinstance(result, dict):+ logger.warning(f"Invalid result type: {type(result)}")+ return {"elements": {}, "metadata": {"elementCount": 0}}++ # Success - return result+ if attempt > 0:+ logger.debug(+ f"Unified analysis succeeded on attempt {attempt + 1}"+ )+ return result++ except Exception as e:+ error_msg = str(e)++ # Check if this is a navigation-related error+ is_navigation_error = (+ "Execution context was destroyed" in error_msg+ or "Most likely because of a navigation" in error_msg+ or "Target page, context or browser has been closed"+ in error_msg+ )++ if is_navigation_error and attempt < max_retries - 1:+ logger.debug(+ f"Navigation error in unified analysis (attempt "+ f"{attempt + 1}/{max_retries}): {e}. Retrying..."+ )++ # Wait a bit for page stability before retrying (optimized)+ try:+ await page.wait_for_load_state(+ 'domcontentloaded',+ timeout=self._page_stability_timeout,+ )+ # Reduced delay for JS context to stabilize+ import asyncio++ await asyncio.sleep(0.1) # Reduced from 0.2s+ except Exception:+ # Continue even if wait fails+ pass++ continue++ # Non-navigation error or final attempt - log and return+ # empty result+ if attempt == max_retries - 1:+ logger.warning(+ f"Error in unified analysis after {max_retries} "+ f"attempts: {e}"+ )+ else:+ logger.warning(+ f"Non-retryable error in unified analysis: {e}"+ )- if not isinstance(result, dict):- logger.warning(f"Invalid result type: {type(result)}")return {"elements": {}, "metadata": {"elementCount": 0}}- return result- except Exception as e:- logger.warning(f"Error in unified analysis: {e}")- return {"elements": {}, "metadata": {"elementCount": 0}}+ # Should not reach here, but just in case+ return {"elements": {}, "metadata": {"elementCount": 0}}def _convert_analysis_to_rects(self, analysis_data: Dict[str, Any]@@ -728,13 +873,13 @@ class HybridBrowserToolkit(BaseToolkit):try:# Get before snapshotlogger.info("Capturing pre-action snapshot...")- snapshot_start = time.time()+ snapshot_start_before = time.time()before_snapshot = await self._session.get_snapshot(force_refresh=True, diff_only=False)- snapshot_time = time.time() - snapshot_start+ before_snapshot_time = time.time() - snapshot_start_beforelogger.info(- f"Pre-action snapshot captured in {snapshot_time:.2f}s"+ f"Pre-action snapshot captured in {before_snapshot_time:.2f}s")# Execute action@@ -780,13 +925,14 @@ class HybridBrowserToolkit(BaseToolkit):# Get after snapshotlogger.info("Capturing post-action snapshot...")- snapshot_start = time.time()+ snapshot_start_after = time.time()after_snapshot = await self._session.get_snapshot(force_refresh=True, diff_only=False)- snapshot_time = time.time() - snapshot_start+ after_snapshot_time = time.time() - snapshot_start_afterlogger.info(- f"Post-action snapshot " f"captured in {snapshot_time:.2f}s"+ f"Post-action snapshot "+ f"captured in {after_snapshot_time:.2f}s")# Check for snapshot quality and log warnings@@ -820,6 +966,7 @@ class HybridBrowserToolkit(BaseToolkit):# Create comprehensive output for loggingexecution_time = time.time() - action_start_time+ total_snapshot_time = before_snapshot_time + after_snapshot_timeoutputs = {"result": result_message,"snapshot": snapshot,@@ -830,6 +977,7 @@ class HybridBrowserToolkit(BaseToolkit):"stability_time_ms": round(stability_time * 1000, 2)if stability_time > 0else None,+ "snapshot_time_ms": round(total_snapshot_time * 1000, 2),"total_time_ms": round(execution_time * 1000, 2),},**tab_info, # Include tab information@@ -930,7 +1078,7 @@ class HybridBrowserToolkit(BaseToolkit):def _ensure_agent(self) -> PlaywrightLLMAgent:r"""Create PlaywrightLLMAgent on first use."""- if self.web_agent_model is None:+ if self._web_agent_model is None:raise RuntimeError("web_agent_model required for high-level task planning")@@ -939,7 +1087,7 @@ class HybridBrowserToolkit(BaseToolkit):self._agent = PlaywrightLLMAgent(headless=self._headless,user_data_dir=self._user_data_dir,- model_backend=self.web_agent_model,+ model_backend=self._web_agent_model,)return self._agent@@ -950,10 +1098,10 @@ class HybridBrowserToolkit(BaseToolkit):default page.This method initializes the underlying browser instance and- automatically- navigates to the default start URL that was configured during toolkit- initialization. Agents cannot specify a custom URL - they must use the- visit_page tool for navigation to other URLs.+ automatically navigates to the default start URL that was configured+ during toolkit initialization in the first tab. Agents cannot specify+ a custom URL - they must use the visit_page tool to open new tabs+ with other URLs.Returns:Dict[str, Any]: A dictionary containing:@@ -979,9 +1127,10 @@ class HybridBrowserToolkit(BaseToolkit):try:# Always use the configured default start URL- start_url = self.default_start_url+ start_url = self._default_start_urllogger.info(f"Navigating to configured default page: {start_url}")+ # Use visit_page without creating a new tabresult = await self.visit_page(start_url)# Log success@@ -991,8 +1140,8 @@ class HybridBrowserToolkit(BaseToolkit):action_name="open_browser",inputs=inputs,outputs={- "result": "Browser opened and navigated to default "- "page."+ "result": "Browser opened and navigated to "+ "default page."},execution_time=execution_time,)@@ -1035,21 +1184,17 @@ class HybridBrowserToolkit(BaseToolkit):@action_loggerasync def visit_page(self, url: str) -> Dict[str, Any]:- r"""Navigates the current browser page to a specified URL.+ r"""Navigates to a URL.++ This method creates a new tab for the URL instead of navigating+ in the current tab, allowing better multi-tab management.Args:- url (str): The web address to load in the browser. Must be a- valid URL.+ url (str): The web address to load in the browser.Returns:- Dict[str, Any]: A dictionary containing:- - "result": A message indicating the outcome of the navigation,- e.g., "Navigation successful.".- - "snapshot": A new textual snapshot of the page's interactive- elements after the new page has loaded.- - "tabs": List of all open tabs with their information.- - "current_tab": Index of the currently active tab.- - "total_tabs": Total number of open tabs.+ Dict[str, Any]: A dictionary containing the result, snapshot, and+ tab information."""if not url or not isinstance(url, str):return {@@ -1063,22 +1208,50 @@ class HybridBrowserToolkit(BaseToolkit):if '://' not in url:url = f'https://{url}'- logger.info(f"Navigating to URL: {url}")+ await self._ensure_browser()+ session = await self._get_session()+ nav_result = ""- # Navigate to page- nav_start = time.time()- nav_result = await self._session.visit(url)- nav_time = time.time() - nav_start- logger.info(f"Page navigation completed in {nav_time:.2f}s")+ # By default, we want to create a new tab.+ should_create_new_tab = True+ try:+ # If the browser has just started with a single "about:blank" tab,+ # use that tab instead of creating a new one.+ tab_info_data = await self._get_tab_info_for_output()+ tabs = tab_info_data.get("tabs", [])+ if len(tabs) == 1 and tabs[0].get("url") == "about:blank":+ logger.info(+ "Found single blank tab, navigating in current tab "+ "instead of creating a new one."+ )+ should_create_new_tab = False+ except Exception as e:+ logger.warning(+ "Could not get tab info to check for blank tab, "+ f"proceeding with default behavior (new tab). Error: {e}"+ )++ if should_create_new_tab:+ logger.info(f"Creating new tab and navigating to URL: {url}")+ try:+ new_tab_index = await session.create_new_tab(url)+ await session.switch_to_tab(new_tab_index)+ nav_result = f"Visited {url} in new tab {new_tab_index}"+ except Exception as e:+ logger.error(f"Failed to create new tab and navigate: {e}")+ nav_result = f"Error creating new tab: {e}"+ else:+ logger.info(f"Navigating to URL in current tab: {url}")+ nav_result = await session.visit(url)# Get snapshot- logger.info("Capturing page snapshot after navigation...")- snapshot_start = time.time()- snapshot = await self._session.get_snapshot(- force_refresh=True, diff_only=False- )- snapshot_time = time.time() - snapshot_start- logger.info(f"Navigation snapshot captured in {snapshot_time:.2f}s")+ snapshot = ""+ try:+ snapshot = await session.get_snapshot(+ force_refresh=True, diff_only=False+ )+ except Exception as e:+ logger.warning(f"Failed to capture snapshot: {e}")# Get tab informationtab_info = await self._get_tab_info_for_output()@@ -1110,12 +1283,16 @@ class HybridBrowserToolkit(BaseToolkit):try:logger.info("Navigating back in browser history...")nav_start = time.time()- await page.go_back(wait_until="domcontentloaded", timeout=30000)+ await page.go_back(+ wait_until="domcontentloaded", timeout=self._navigation_timeout+ )nav_time = time.time() - nav_startlogger.info(f"Back navigation completed in {nav_time:.2f}s")- # Wait for page stability- await self._wait_for_page_stability()+ # Minimal wait for page stability (back navigation is usually fast)+ import asyncio++ await asyncio.sleep(0.2)# Get snapshotlogger.info("Capturing page snapshot after back navigation...")@@ -1175,12 +1352,17 @@ class HybridBrowserToolkit(BaseToolkit):try:logger.info("Navigating forward in browser history...")nav_start = time.time()- await page.go_forward(wait_until="domcontentloaded", timeout=30000)+ await page.go_forward(+ wait_until="domcontentloaded", timeout=self._navigation_timeout+ )nav_time = time.time() - nav_startlogger.info(f"Forward navigation completed in {nav_time:.2f}s")- # Wait for page stability- await self._wait_for_page_stability()+ # Minimal wait for page stability (forward navigation is usually+ # fast)+ import asyncio++ await asyncio.sleep(0.2)# Get snapshotlogger.info("Capturing page snapshot after forward navigation...")@@ -1285,13 +1467,11 @@ class HybridBrowserToolkit(BaseToolkit):# Log screenshot timeout startlogger.info(f"Starting screenshot capture"- f"with timeout: {self.DEFAULT_SCREENSHOT_TIMEOUT}ms"+ f"with timeout: {self._screenshot_timeout}ms")start_time = time.time()- image_data = await page.screenshot(- timeout=self.DEFAULT_SCREENSHOT_TIMEOUT- )+ image_data = await page.screenshot(timeout=self._screenshot_timeout)screenshot_time = time.time() - start_timelogger.info(f"Screenshot capture completed in {screenshot_time:.2f}s")@@ -1317,7 +1497,7 @@ class HybridBrowserToolkit(BaseToolkit):url_name = sanitize_filename(str(parsed_url.path), max_length=241)timestamp = datetime.datetime.now().strftime("%m%d%H%M%S")file_path = os.path.join(- self.cache_dir, f"{url_name}_{timestamp}_som.png"+ self._cache_dir, f"{url_name}_{timestamp}_som.png")marked_image.save(file_path, "PNG")@@ -1706,7 +1886,7 @@ class HybridBrowserToolkit(BaseToolkit):enabled_tools = []for tool_name in self.enabled_tools:- if tool_name == "solve_task" and self.web_agent_model is None:+ if tool_name == "solve_task" and self._web_agent_model is None:logger.warning(f"Tool '{tool_name}' is enabled but web_agent_model "f"is not provided. Skipping this tool."@@ -1746,13 +1926,20 @@ class HybridBrowserToolkit(BaseToolkit):return HybridBrowserToolkit(headless=self._headless,user_data_dir=self._user_data_dir,- stealth=self._session._stealth if self._session else False,- web_agent_model=self.web_agent_model,- cache_dir=f"{self.cache_dir.rstrip('/')}_clone_{new_session_id}/",+ stealth=self._stealth,+ web_agent_model=self._web_agent_model,+ cache_dir=f"{self._cache_dir.rstrip('/')}_clone_{new_session_id}/",enabled_tools=self.enabled_tools.copy(),- browser_log_to_file=self.log_to_file,+ browser_log_to_file=self._browser_log_to_file,session_id=new_session_id,- default_start_url=self.default_start_url,+ default_start_url=self._default_start_url,+ default_timeout=self._default_timeout,+ short_timeout=self._short_timeout,+ navigation_timeout=self._navigation_timeout,+ network_idle_timeout=self._network_idle_timeout,+ screenshot_timeout=self._screenshot_timeout,+ page_stability_timeout=self._page_stability_timeout,+ dom_content_loaded_timeout=self._dom_content_loaded_timeout,)@action_loggerdiff --git a/camel/toolkits/hybrid_browser_toolkit/snapshot.py b/camel/toolkits/hybrid_browser_toolkit/snapshot.pyindex bdda07896..9a2e79e3a 100644--- a/camel/toolkits/hybrid_browser_toolkit/snapshot.py+++ b/camel/toolkits/hybrid_browser_toolkit/snapshot.py@@ -20,6 +20,8 @@ if TYPE_CHECKING:# Logging supportfrom camel.logger import get_logger+from .config_loader import ConfigLoader+logger = get_logger(__name__)@@ -27,8 +29,6 @@ class PageSnapshot:"""Utility for capturing YAML-like page snapshots and diff-onlyvariants."""- MAX_TIMEOUT_MS = 5000 # wait_for_load_state timeout-def __init__(self, page: "Page"):self.page = pageself.snapshot_data: Optional[str] = None # last full snapshot@@ -37,6 +37,7 @@ class PageSnapshot:"is_diff": False,"priorities": [1, 2, 3],}+ self.dom_timeout = ConfigLoader.get_dom_content_loaded_timeout()# ---------------------------------------------------------------------# Public API@@ -60,7 +61,7 @@ class PageSnapshot:# ensure DOM stabilityawait self.page.wait_for_load_state(- 'domcontentloaded', timeout=self.MAX_TIMEOUT_MS+ 'domcontentloaded', timeout=self.dom_timeout)logger.debug("Capturing page snapshot …")@@ -153,7 +154,7 @@ class PageSnapshot:# Wait for next DOM stability before retryingtry:await self.page.wait_for_load_state(- "domcontentloaded", timeout=self.MAX_TIMEOUT_MS+ "domcontentloaded", timeout=self.dom_timeout)except Exception:# Even if waiting fails, attempt retry to give itdiff --git a/camel/toolkits/hybrid_browser_toolkit/stealth_config.py b/camel/toolkits/hybrid_browser_toolkit/stealth_config.pydeleted file mode 100644index 7da275773..000000000--- a/camel/toolkits/hybrid_browser_toolkit/stealth_config.py+++ /dev/null@@ -1,116 +0,0 @@-# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========--"""-Stealth configuration for browser automation to avoid bot detection.--This module contains all the configuration needed to make the browser-appear as a regular user browser rather than an automated one.-"""--from typing import Any, Dict, List---class StealthConfig:- """Configuration class for stealth browser settings."""-- @staticmethod- def get_launch_args() -> List[str]:- """Get Chrome launch arguments for stealth mode.-- Returns:- List[str]: Chrome command line arguments to avoid detection.- """- return [- '--disable-blink-features=AutomationControlled',- '--disable-features=VizDisplayCompositor',- '--disable-ipc-flooding-protection',- '--disable-renderer-backgrounding',- '--disable-backgrounding-occluded-windows',- '--disable-dev-shm-usage',- '--disable-extensions',- '--disable-plugins',- '--disable-default-apps',- '--disable-sync',- '--no-default-browser-check',- '--no-first-run',- '--no-sandbox',- '--disable-setuid-sandbox',- '--disable-web-security',- '--disable-features=TranslateUI',- '--disable-features=BlinkGenPropertyTrees',- '--disable-component-extensions-with-background-pages',- ]-- @staticmethod- def get_context_options() -> Dict[str, Any]:- """Get browser context options for stealth mode.-- Returns:- Dict[str, Any]: Browser context configuration options.- """- return {- 'user_agent': (- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '- 'AppleWebKit/537.36 (KHTML, like Gecko) '- 'Chrome/131.0.0.0 Safari/537.36'- ),- 'viewport': {'width': 1920, 'height': 1080},- 'locale': 'en-US',- 'timezone_id': 'America/New_York',- 'geolocation': {'latitude': 40.7128, 'longitude': -74.0060},- 'permissions': ['geolocation'],- }-- @staticmethod- def get_http_headers() -> Dict[str, str]:- """Get HTTP headers for stealth mode.-- Returns:- Dict[str, str]: HTTP headers to appear more like a real browser.- """- return {- 'Accept': (- 'text/html,application/xhtml+xml,application/xml;q=0.9,'- 'image/avif,image/webp,image/apng,*/*;q=0.8,'- 'application/signed-exchange;v=b3;q=0.7'- ),- 'Accept-Language': 'en-US,en;q=0.9',- 'Accept-Encoding': 'gzip, deflate, br, zstd',- 'Cache-Control': 'max-age=0',- 'Sec-Ch-Ua': (- '"Google Chrome";v="131", "Chromium";v="131", '- '"Not=A?Brand";v="24"'- ),- 'Sec-Ch-Ua-Mobile': '?0',- 'Sec-Ch-Ua-Platform': '"Windows"',- 'Sec-Fetch-Dest': 'document',- 'Sec-Fetch-Mode': 'navigate',- 'Sec-Fetch-Site': 'none',- 'Sec-Fetch-User': '?1',- 'Upgrade-Insecure-Requests': '1',- }-- @staticmethod- def get_all_config() -> Dict[str, Any]:- """Get all stealth configuration in one dict.-- Returns:- Dict[str, Any]: Complete stealth configuration.- """- return {- 'launch_args': StealthConfig.get_launch_args(),- 'context_options': StealthConfig.get_context_options(),- 'http_headers': StealthConfig.get_http_headers(),- }diff --git a/camel/toolkits/hybrid_browser_toolkit/unified_analyzer.js b/camel/toolkits/hybrid_browser_toolkit/unified_analyzer.jsindex 6c2c0f1b9..7c79e1043 100644--- a/camel/toolkits/hybrid_browser_toolkit/unified_analyzer.js+++ b/camel/toolkits/hybrid_browser_toolkit/unified_analyzer.js@@ -2,9 +2,302 @@// Unified analyzer that combines visual and structural analysis// Preserves complete snapshot.js logic while adding visual coordinate information- let refCounter = 1;+ // Memory management constants and configuration+ const MAX_REFS = 2000; // Maximum number of refs to keep in memory+ const MAX_UNUSED_AGE_MS = 90000; // Remove refs unused for more than xx seconds+ const CLEANUP_THRESHOLD = 0.8; // Start aggressive cleanup when 80% of max refs reached++ // Persistent ref management across page analysis calls with memory leak prevention+ let refCounter = window.__camelRefCounter || 1;+ let elementRefMap = window.__camelElementRefMap || new WeakMap();+ let refElementMap = window.__camelRefElementMap || new Map();+ let elementSignatureMap = window.__camelElementSignatureMap || new Map();++ // LRU tracking for ref access times+ let refAccessTimes = window.__camelRefAccessTimes || new Map();+ let lastNavigationUrl = window.__camelLastNavigationUrl || window.location.href;++ // Initialize navigation event listeners for automatic cleanup+ if (!window.__camelNavigationListenersInitialized) {+ window.__camelNavigationListenersInitialized = true;++ // Listen for page navigation events+ window.addEventListener('beforeunload', clearAllRefs);+ window.addEventListener('pagehide', clearAllRefs);++ // Listen for pushState/replaceState navigation (SPA navigation)+ const originalPushState = history.pushState;+ const originalReplaceState = history.replaceState;++ history.pushState = function(...args) {+ clearAllRefs();+ return originalPushState.apply(this, args);+ };++ history.replaceState = function(...args) {+ clearAllRefs();+ return originalReplaceState.apply(this, args);+ };++ // Listen for popstate (back/forward navigation)+ window.addEventListener('popstate', clearAllRefs);++ // Check for URL changes periodically (fallback for other navigation types)+ setInterval(() => {+ if (window.location.href !== lastNavigationUrl) {+ clearAllRefs();+ lastNavigationUrl = window.location.href;+ window.__camelLastNavigationUrl = lastNavigationUrl;+ }+ }, 1000);+ }+function generateRef() {- return `e${refCounter++}`;+ const ref = `e${refCounter++}`;+ // Persist counter globally+ window.__camelRefCounter = refCounter;+ return ref;+ }++ // Clear all refs and reset memory state+ function clearAllRefs() {+ try {+ // Clear all DOM aria-ref attributes+ document.querySelectorAll('[aria-ref]').forEach(element => {+ element.removeAttribute('aria-ref');+ });++ // Clear all maps and reset counters+ elementRefMap.clear();+ refElementMap.clear();+ elementSignatureMap.clear();+ refAccessTimes.clear();++ // Reset global state+ window.__camelElementRefMap = elementRefMap;+ window.__camelRefElementMap = refElementMap;+ window.__camelElementSignatureMap = elementSignatureMap;+ window.__camelRefAccessTimes = refAccessTimes;++ // Clear cached analysis results+ delete window.__camelLastAnalysisResult;+ delete window.__camelLastAnalysisTime;++ console.log('CAMEL: Cleared all refs due to navigation');+ } catch (error) {+ console.warn('CAMEL: Error clearing refs:', error);+ }+ }++ // LRU eviction: Remove least recently used refs when limit exceeded+ function evictLRURefs() {+ const refsToEvict = refAccessTimes.size - MAX_REFS + Math.floor(MAX_REFS * 0.1); // Remove 10% extra for breathing room+ if (refsToEvict <= 0) return 0;++ // Sort refs by access time (oldest first)+ const sortedRefs = Array.from(refAccessTimes.entries())+ .sort((a, b) => a[1] - b[1])+ .slice(0, refsToEvict);++ let evictedCount = 0;+ for (const [ref, _] of sortedRefs) {+ const element = refElementMap.get(ref);+ if (element) {+ // Remove aria-ref attribute from DOM+ try {+ element.removeAttribute('aria-ref');+ } catch (e) {+ // Element might be detached from DOM+ }+ elementRefMap.delete(element);++ // Remove from signature map+ const signature = generateElementSignature(element);+ if (signature && elementSignatureMap.get(signature) === ref) {+ elementSignatureMap.delete(signature);+ }+ }++ refElementMap.delete(ref);+ refAccessTimes.delete(ref);+ evictedCount++;+ }++ // Persist updated maps+ window.__camelElementRefMap = elementRefMap;+ window.__camelRefElementMap = refElementMap;+ window.__camelElementSignatureMap = elementSignatureMap;+ window.__camelRefAccessTimes = refAccessTimes;++ return evictedCount;+ }++ // Update ref access time for LRU tracking+ function updateRefAccessTime(ref) {+ refAccessTimes.set(ref, Date.now());+ window.__camelRefAccessTimes = refAccessTimes;+ }++ // Generate a unique signature for an element based on its characteristics+ function generateElementSignature(element) {+ if (!element || !element.tagName) return null;++ const tagName = element.tagName.toLowerCase();+ const textContent = (element.textContent || '').trim().substring(0, 50);+ const className = element.className || '';+ const id = element.id || '';+ const href = element.href || '';+ const src = element.src || '';+ const value = element.value || '';+ const type = element.type || '';+ const placeholder = element.placeholder || '';++ // Include position in DOM tree for uniqueness+ let pathElements = [];+ let current = element;+ let depth = 0;+ while (current && current.parentElement && depth < 5) {+ const siblings = Array.from(current.parentElement.children);+ const index = siblings.indexOf(current);+ pathElements.unshift(`${current.tagName.toLowerCase()}[${index}]`);+ current = current.parentElement;+ depth++;+ }+ const domPath = pathElements.join('>');++ return `${tagName}|${textContent}|${className}|${id}|${href}|${src}|${value}|${type}|${placeholder}|${domPath}`;+ }++ // Get or assign a persistent ref for an element+ function getOrAssignRef(element) {+ // Check if element already has a ref assigned+ if (elementRefMap.has(element)) {+ const existingRef = elementRefMap.get(element);+ // Verify the ref is still valid+ if (refElementMap.get(existingRef) === element) {+ updateRefAccessTime(existingRef);+ return existingRef;+ }+ }++ // Check if element has aria-ref attribute (from previous analysis)+ const existingAriaRef = element.getAttribute('aria-ref');+ if (existingAriaRef && refElementMap.get(existingAriaRef) === element) {+ // Re-establish mappings+ elementRefMap.set(element, existingAriaRef);+ updateRefAccessTime(existingAriaRef);+ return existingAriaRef;+ }++ // Try to find element by signature (in case DOM was modified)+ const signature = generateElementSignature(element);+ if (signature && elementSignatureMap.has(signature)) {+ const existingRef = elementSignatureMap.get(signature);+ // Verify the old element is no longer in DOM or has changed+ const oldElement = refElementMap.get(existingRef);+ if (!oldElement || !document.contains(oldElement) || generateElementSignature(oldElement) !== signature) {+ // Reassign the ref to the new element+ elementRefMap.set(element, existingRef);+ refElementMap.set(existingRef, element);+ elementSignatureMap.set(signature, existingRef);+ element.setAttribute('aria-ref', existingRef);+ updateRefAccessTime(existingRef);+ return existingRef;+ }+ }++ // Check if we need to evict refs before creating new ones+ if (refElementMap.size >= MAX_REFS) {+ evictLRURefs();+ }++ // Generate new ref for new element+ const newRef = generateRef();+ elementRefMap.set(element, newRef);+ refElementMap.set(newRef, element);+ if (signature) {+ elementSignatureMap.set(signature, newRef);+ }+ element.setAttribute('aria-ref', newRef);+ updateRefAccessTime(newRef);+ return newRef;+ }++ // Enhanced cleanup function with aggressive stale ref removal+ function cleanupStaleRefs() {+ const staleRefs = [];+ const currentTime = Date.now();+ const isAggressiveCleanup = refElementMap.size > (MAX_REFS * CLEANUP_THRESHOLD);++ // Check all mapped elements to see if they're still in DOM or too old+ for (const [ref, element] of refElementMap.entries()) {+ let shouldRemove = false;++ // Standard checks: element not in DOM+ if (!element || !document.contains(element)) {+ shouldRemove = true;+ }+ // Aggressive cleanup: remove refs unused for too long+ else if (isAggressiveCleanup) {+ const lastAccess = refAccessTimes.get(ref) || 0;+ const age = currentTime - lastAccess;+ if (age > MAX_UNUSED_AGE_MS) {+ shouldRemove = true;+ }+ }+ // Additional checks for aggressive cleanup+ else if (isAggressiveCleanup) {+ // Remove refs for elements that are hidden or have no meaningful content+ try {+ const style = window.getComputedStyle(element);+ const hasNoVisibleContent = !element.textContent?.trim() &&+ !element.value?.trim() &&+ !element.src &&+ !element.href;++ if ((style.display === 'none' || style.visibility === 'hidden') && hasNoVisibleContent) {+ shouldRemove = true;+ }+ } catch (e) {+ // If we can't get computed style, element might be detached+ shouldRemove = true;+ }+ }++ if (shouldRemove) {+ staleRefs.push(ref);+ }+ }++ // Remove stale mappings+ for (const ref of staleRefs) {+ const element = refElementMap.get(ref);+ if (element) {+ // Remove aria-ref attribute from DOM+ try {+ element.removeAttribute('aria-ref');+ } catch (e) {+ // Element might be detached from DOM+ }+ elementRefMap.delete(element);++ // Remove from signature map+ const signature = generateElementSignature(element);+ if (signature && elementSignatureMap.get(signature) === ref) {+ elementSignatureMap.delete(signature);+ }+ }+ refElementMap.delete(ref);+ refAccessTimes.delete(ref);+ }++ // Persist maps globally+ window.__camelElementRefMap = elementRefMap;+ window.__camelRefElementMap = refElementMap;+ window.__camelElementSignatureMap = elementSignatureMap;+ window.__camelRefAccessTimes = refAccessTimes;++ return staleRefs.length;}// === Complete snapshot.js logic preservation ===@@ -15,7 +308,7 @@if (node.nodeType !== Node.ELEMENT_NODE) return true;try {- const style = window.getComputedStyle(node);+ const style = window.getComputedStyle(node);if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0')return false;// An element with `display: contents` is not rendered itself, but its children are.@@ -29,6 +322,55 @@}}+ // Optimized occlusion detection with fewer test points+ function isOccluded(element) {+ if (!element || element.nodeType !== Node.ELEMENT_NODE) return false;++ try {+ const rect = element.getBoundingClientRect();+ if (rect.width === 0 || rect.height === 0) return true;++ // Simplified: Use fewer test points for better performance+ const testPoints = [+ // Center point (most important)+ { x: rect.left + rect.width * 0.5, y: rect.top + rect.height * 0.5, weight: 4 },+ // Only test 4 strategic points instead of 9+ { x: rect.left + rect.width * 0.25, y: rect.top + rect.height * 0.25, weight: 1 },+ { x: rect.left + rect.width * 0.75, y: rect.top + rect.height * 0.25, weight: 1 },+ { x: rect.left + rect.width * 0.25, y: rect.top + rect.height * 0.75, weight: 1 },+ { x: rect.left + rect.width * 0.75, y: rect.top + rect.height * 0.75, weight: 1 }+ ];++ let totalWeight = 0;+ let visibleWeight = 0;++ for (const point of testPoints) {+ // Skip points outside viewport+ if (point.x < 0 || point.y < 0 ||+ point.x >= window.innerWidth || point.y >= window.innerHeight) {+ continue;+ }++ const hitElement = document.elementFromPoint(point.x, point.y);+ totalWeight += point.weight;++ // Simplified visibility check+ if (hitElement && (hitElement === element || element.contains(hitElement) || hitElement.contains(element))) {+ visibleWeight += point.weight;+ }+ }++ // If no valid test points, assume not occluded+ if (totalWeight === 0) return false;++ // Element is occluded if less than 40% of weighted points are visible+ return (visibleWeight / totalWeight) < 0.4;++ } catch (e) {+ return false;+ }+ }+function getRole(node) {// Check if node is null or doesn't have required propertiesif (!node || !node.tagName || !node.getAttribute) {@@ -39,6 +381,8 @@if (role) return role;const tagName = node.tagName.toLowerCase();++ // Extended role mapping to better match Playwrightif (tagName === 'a') return 'link';if (tagName === 'button') return 'button';if (tagName === 'input') {@@ -48,9 +392,83 @@}if (['select', 'textarea'].includes(tagName)) return tagName;if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(tagName)) return 'heading';++ // Additional roles for better Playwright compatibility+ if (tagName === 'img') return 'img';+ if (tagName === 'main') return 'main';+ if (tagName === 'nav') return 'navigation';+ if (tagName === 'ul' || tagName === 'ol') return 'list';+ if (tagName === 'li') return 'listitem';+ if (tagName === 'em') return 'emphasis';+ if (tagName === 'form' && node.getAttribute('role') === 'search') return 'search';+ if (tagName === 'section' || tagName === 'article') return 'region';+ if (tagName === 'aside') return 'complementary';+ if (tagName === 'header') return 'banner';+ if (tagName === 'footer') return 'contentinfo';+ if (tagName === 'fieldset') return 'group';+return 'generic';}+ // Playwright-inspired function to check if element receives pointer events+ function receivesPointerEvents(element) {+ if (!element || !element.nodeType || element.nodeType !== Node.ELEMENT_NODE) return false;++ try {+ let e = element;+ while (e) {+ const style = window.getComputedStyle(e);+ if (!style) break;++ const pointerEvents = style.pointerEvents;+ if (pointerEvents === 'none') return false;+ if (pointerEvents && pointerEvents !== 'auto') return true;++ e = e.parentElement;+ }+ return true;+ } catch (error) {+ return false;+ }+ }++ // Playwright-inspired function to check if element has pointer cursor+ function hasPointerCursor(element) {+ if (!element || !element.nodeType || element.nodeType !== Node.ELEMENT_NODE) return false;++ try {+ const style = window.getComputedStyle(element);+ return style.cursor === 'pointer';+ } catch (error) {+ return false;+ }+ }++ // Playwright-inspired function to get aria level+ function getAriaLevel(element) {+ if (!element || !element.tagName) return 0;++ // Native HTML heading levels (H1=1, H2=2, etc.)+ const tagName = element.tagName.toUpperCase();+ const nativeLevel = { 'H1': 1, 'H2': 2, 'H3': 3, 'H4': 4, 'H5': 5, 'H6': 6 }[tagName];+ if (nativeLevel) return nativeLevel;++ // Check aria-level attribute for roles that support it+ const role = getRole(element);+ const kAriaLevelRoles = ['heading', 'listitem', 'row', 'treeitem'];+ if (kAriaLevelRoles.includes(role)) {+ const ariaLevel = element.getAttribute('aria-level');+ if (ariaLevel !== null) {+ const value = Number(ariaLevel);+ if (Number.isInteger(value) && value >= 1) {+ return value;+ }+ }+ }++ return 0;+ }+function getAccessibleName(node) {// Check if node is null or doesn't have required methodsif (!node || !node.hasAttribute || !node.getAttribute) return '';@@ -67,7 +485,7 @@// Add a heuristic to ignore code-like text that might be in the DOMif ((text.match(/[;:{}]/g)?.length || 0) > 2) return '';return text;- }+ }const textCache = new Map();function getVisibleTextContent(_node) {@@ -116,22 +534,49 @@const name = getAccessibleName(element);+ // Get persistent ref for this element+ const ref = getOrAssignRef(element);+// Create the nodeconst node = {role,name,children: [],element: element,- ref: generateRef(),+ ref: ref,};// Add states for interactive elements, similar to Playwright- if (element.hasAttribute('disabled')) node.disabled = true;- if (element.hasAttribute('aria-checked')) node.checked = element.getAttribute('aria-checked');- if (element.hasAttribute('aria-expanded')) node.expanded = element.getAttribute('aria-expanded');+ if (element.hasAttribute('disabled') || element.disabled) node.disabled = true;++ // NEW: Check if element is occluded and mark with occluded tag+ if (isOccluded(element)) {+ node.occluded = true; // Mark as occluded but don't disable+ }++ // Handle aria-checked and native checked+ const ariaChecked = element.getAttribute('aria-checked');+ if (ariaChecked) {+ node.checked = ariaChecked;+ } else if (element.type === 'checkbox' || element.type === 'radio') {+ node.checked = element.checked;+ }++ // Handle aria-expanded+ const ariaExpanded = element.getAttribute('aria-expanded');+ if (ariaExpanded) {+ node.expanded = ariaExpanded === 'true';+ }- // Tag element with a ref for later lookup- element.setAttribute('aria-ref', node.ref);+ // Handle aria-selected+ const ariaSelected = element.getAttribute('aria-selected');+ if (ariaSelected === 'true') {+ node.selected = true;+ }++ // Add level support following Playwright's implementation+ const level = getAriaLevel(element);+ if (level > 0) node.level = level;return node;}@@ -217,7 +662,7 @@/*** Phase 2: Normalize the tree by removing redundant generic wrappers.- * Complete preservation of snapshot.js normalizeTree logic+ * Complete preservation of snapshot.js normalizeTree logic with cursor inheritance*/function normalizeTree(node) {if (typeof node === 'string') return [node];@@ -229,6 +674,7 @@node.children = newChildren;// Remove child elements that have the same name as their parent+ // and inherit cursor=pointer property if child had itconst filteredChildren = [];for (const child of node.children) {if (typeof child !== 'string' && child.name && node.name) {@@ -237,6 +683,15 @@if (childName === parentName) {// If child has same name as parent, merge its children into parentfilteredChildren.push(...(child.children || []));++ // Inherit cursor=pointer from merged child+ if (child.element && receivesPointerEvents(child.element) && hasPointerCursor(child.element)) {+ node.inheritedCursor = true;+ }++ // Also inherit other properties if needed+ if (child.disabled && !node.disabled) node.disabled = child.disabled;+ if (child.selected && !node.selected) node.selected = child.selected;} else {filteredChildren.push(child);}@@ -250,6 +705,15 @@if (node.children.length === 1 && typeof node.children[0] !== 'string') {const child = node.children[0];if (child.name && node.name && child.name.trim() === node.name.trim()) {+ // Inherit cursor=pointer from the child being merged+ if (child.element && receivesPointerEvents(child.element) && hasPointerCursor(child.element)) {+ node.inheritedCursor = true;+ }++ // Also inherit other properties+ if (child.disabled && !node.disabled) node.disabled = child.disabled;+ if (child.selected && !node.selected) node.selected = child.selected;+// Merge child's children into parent and remove the redundant childnode.children = child.children || [];}@@ -266,19 +730,36 @@/*** Phase 3: Render the normalized tree into the final string format.- * Complete preservation of snapshot.js renderTree logic+ * Complete preservation of snapshot.js renderTree logic with Playwright enhancements*/function renderTree(node, indent = '') {const lines = [];let meaningfulProps = '';if (node.disabled) meaningfulProps += ' disabled';+ if (node.occluded) meaningfulProps += ' occluded';if (node.checked !== undefined) meaningfulProps += ` checked=${node.checked}`;if (node.expanded !== undefined) meaningfulProps += ` expanded=${node.expanded}`;+ if (node.selected) meaningfulProps += ' selected';++ // Add level attribute following Playwright's format+ if (node.level !== undefined) meaningfulProps += ` [level=${node.level}]`;const ref = node.ref ? ` [ref=${node.ref}]` : '';++ // Add cursor=pointer detection following Playwright's implementation+ // Check both direct cursor and inherited cursor from merged children+ let cursor = '';+ const hasDirectCursor = node.element && receivesPointerEvents(node.element) && hasPointerCursor(node.element);+ const hasInheritedCursor = node.inheritedCursor;++ // Only add cursor=pointer if element is not occluded+ if ((hasDirectCursor || hasInheritedCursor) && !node.occluded) {+ cursor = ' [cursor=pointer]';+ }+const name = (node.name || '').replace(/\s+/g, ' ').trim();- // Skip elements with empty names and no meaningful props (ref is not considered meaningful)+ // Skip elements with empty names and no meaningful props (ref and cursor are not considered meaningful for this check)if (!name && !meaningfulProps) {// If element has no name and no meaningful props, render its children directly at current levelfor (const child of node.children) {@@ -294,7 +775,7 @@return lines;}- lines.push(`${indent}- ${node.role}${name ? ` "${name}"` : ''}${meaningfulProps}${ref}`);+ lines.push(`${indent}- ${node.role}${name ? ` "${name}"` : ''}${meaningfulProps}${ref}${cursor}`);for (const child of node.children) {if (typeof child === 'string') {@@ -390,6 +871,7 @@disabled: node.disabled,checked: node.checked,expanded: node.expanded,+ level: node.level,// Visual information (from page_script.js)coordinates: coordinates,@@ -398,7 +880,11 @@href: node.element.href || null,value: node.element.value || null,placeholder: node.element.placeholder || null,- scrollable: node.element.scrollHeight > node.element.clientHeight+ scrollable: node.element.scrollHeight > node.element.clientHeight,++ // Playwright-inspired properties+ receivesPointerEvents: receivesPointerEvents(node.element),+ hasPointerCursor: hasPointerCursor(node.element)};}@@ -411,19 +897,64 @@}function analyzePageElements() {+ // Clean up stale refs before analysis+ const cleanedRefCount = cleanupStaleRefs();++ // Performance optimization: Check if we can reuse recent analysis+ const currentTime = Date.now();+ const lastAnalysisTime = window.__camelLastAnalysisTime || 0;+ const timeSinceLastAnalysis = currentTime - lastAnalysisTime;++ // If less than 1 second since last analysis and page hasn't changed significantly+ if (timeSinceLastAnalysis < 1000 && window.__camelLastAnalysisResult && cleanedRefCount === 0) {+ const cachedResult = window.__camelLastAnalysisResult;+ // Update timestamp and memory info in cached result+ cachedResult.metadata.timestamp = new Date().toISOString();+ cachedResult.metadata.memoryInfo = {+ currentRefCount: refElementMap.size,+ maxRefs: MAX_REFS,+ memoryUtilization: (refElementMap.size / MAX_REFS * 100).toFixed(1) + '%',+ lruAccessTimesCount: refAccessTimes.size+ };+ cachedResult.metadata.cacheHit = true;+ return cachedResult;+ }+// Generate the complete structured snapshot using original snapshot.js logicconst outputLines = processDocument(document);const snapshotText = outputLines.join('\n');// Build the tree again to collect element information with visual datatextCache.clear();- refCounter = 1; // Reset counter to match snapshot generation+ // Note: Don't reset refCounter anymore - use persistent counterslet tree = buildAriaTree(document.body);[tree] = normalizeTree(tree);const elementsMap = {};collectElementsFromTree(tree, elementsMap);+ // Verify uniqueness of aria-ref attributes (debugging aid)+ const ariaRefCounts = {};+ document.querySelectorAll('[aria-ref]').forEach(element => {+ const ref = element.getAttribute('aria-ref');+ ariaRefCounts[ref] = (ariaRefCounts[ref] || 0) + 1;+ });++ // Log any duplicates for debugging+ const duplicateRefs = Object.entries(ariaRefCounts).filter(([ref, count]) => count > 1);+ if (duplicateRefs.length > 0) {+ console.warn('Duplicate aria-ref attributes found:', duplicateRefs);+ }++ // Validate ref consistency+ const refValidationErrors = [];+ for (const [ref, elementInfo] of Object.entries(elementsMap)) {+ const mappedElement = refElementMap.get(ref);+ if (!mappedElement || !document.contains(mappedElement)) {+ refValidationErrors.push(`Ref ${ref} points to invalid or removed element`);+ }+ }+const result = {url: window.location.href,elements: elementsMap,@@ -435,10 +966,34 @@width: window.innerWidth,height: window.innerHeight,devicePixelRatio: window.devicePixelRatio || 1- }+ },+ // Enhanced debugging information+ ariaRefCounts: ariaRefCounts,+ duplicateRefsFound: duplicateRefs.length > 0,+ staleRefsCleanedUp: cleanedRefCount,+ refValidationErrors: refValidationErrors,+ totalMappedRefs: refElementMap.size,+ refCounterValue: refCounter,+ // Memory management information+ memoryInfo: {+ currentRefCount: refElementMap.size,+ maxRefs: MAX_REFS,+ memoryUtilization: (refElementMap.size / MAX_REFS * 100).toFixed(1) + '%',+ lruAccessTimesCount: refAccessTimes.size,+ unusedAgeThreshold: MAX_UNUSED_AGE_MS + 'ms',+ cleanupThreshold: (CLEANUP_THRESHOLD * 100).toFixed(0) + '%',+ isAggressiveCleanup: refElementMap.size > (MAX_REFS * CLEANUP_THRESHOLD)+ },+ // Performance information+ cacheHit: false,+ analysisTime: Date.now() - currentTime}};+ // Cache the result for potential reuse+ window.__camelLastAnalysisResult = result;+ window.__camelLastAnalysisTime = currentTime;+return result;}diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.pyindex 5e92dda13..35f5203d9 100644--- a/camel/toolkits/search_toolkit.py+++ b/camel/toolkits/search_toolkit.py@@ -16,10 +16,13 @@ from typing import Any, Dict, List, Literal, Optional, TypeAlias, Union, castimport requests+from camel.logger import get_loggerfrom camel.toolkits.base import BaseToolkitfrom camel.toolkits.function_tool import FunctionToolfrom camel.utils import MCPServer, api_keys_required, dependencies_required+logger = get_logger(__name__)+@MCPServer()class SearchToolkit(BaseToolkit):@@ -480,12 +483,19 @@ class SearchToolkit(BaseToolkit):}responses.append(response)else:- responses.append({"error": "google search failed."})+ error_info = data.get("error", {})+ logger.error(+ f"Google search failed - API response: {error_info}"+ )+ responses.append(+ {+ "error": f"Google search failed - "+ f"API response: {error_info}"+ }+ )- except requests.RequestException:- # Handle specific exceptions or general request exceptions- responses.append({"error": "google search failed."})- # If no answer found, return an empty list+ except Exception as e:+ responses.append({"error": f"google search failed: {e!s}"})return responsesdef tavily_search(diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.pyindex 4bb3ed79e..aa3255cce 100644--- a/examples/workforce/eigent.py+++ b/examples/workforce/eigent.py@@ -169,6 +169,7 @@ def search_agent_factory(NoteTakingToolkit().append_note,*Crawl4AIToolkit().get_tools(),SearchToolkit().search_exa,+ SearchToolkit().search_google,]system_message = """You are a helpful assistant that can search the web,@@ -185,21 +186,17 @@ def search_agent_factory(information you have gathered.### Web Search Workflow- 1. **Open Browser**: You MUST start by using the `open_browser` tool to- launch a browser. This will automatically open a search engine page.- 2. **Perform Search**: Use the `type` tool to enter your search query- into the search bar on the page. Then, use the `click` tool to- press the search button.- 3. **Analyze Search Results**: After the search results load, use- `get_page_snapshot` to see the available links.- 4. **Navigate and Explore**: Instead of using `visit_page` with a URL- you guess, you MUST `click` on a link from the snapshot to navigate- to a page. Once on a page, use browser tools to read content, scrape- information, and navigate to other linked pages to gather all- necessary data. This avoids errors from using incorrect URLs.- 5. **Note Taking**: You MUST use the `append_note` tool to record your- findings, make sure the note is very detailed and include all the- information you have gathered.+ 1. **Initial Search**: Start by using `search_google` to get a list of+ initial URLs for your research.+ 2. **Browser Exploration**: Use the URLs from `search_google` with the+ browser tools to investigate pages. You can `visit_page` to open a+ webpage, `get_som_screenshot` to analyze its content, and `click` to+ navigate.+ 3. **Quick Information Gathering**: For quick questions or summaries,+ use `search_exa` for fast and direct information retrieval.+ 4. **Note Taking**: You MUST use `append_note` to record your findings,+ ensuring your notes are detailed and include all gathered+ information and sources.### Core Principles- For each decision you make and action you take, you must send a message@@ -516,6 +513,8 @@ async def main():model_backend = ModelFactory.create(model_platform=ModelPlatformType.OPENAI,model_type=ModelType.GPT_4_1_MINI,+ # api_key="sk-nmwYgOtNYuhcJl3X0eE49e4b987547F1B9De351c9076Ec2e",+ # url="https://api.pumpkinaigc.online/v1",# model_config_dict={# "max_tokens": 32768,# },@@ -524,6 +523,8 @@ async def main():model_backend_reason = ModelFactory.create(model_platform=ModelPlatformType.OPENAI,model_type=ModelType.GPT_4_1_MINI,+ # api_key="sk-nmwYgOtNYuhcJl3X0eE49e4b987547F1B9De351c9076Ec2e",+ # url="https://api.pumpkinaigc.online/v1",# model_config_dict={# "max_tokens": 32768,# },diff --git a/test/toolkits/test_hybrid_browser_toolkit.py b/test/toolkits/test_hybrid_browser_toolkit.pyindex 64dc966d1..210b91faa 100644--- a/test/toolkits/test_hybrid_browser_toolkit.py+++ b/test/toolkits/test_hybrid_browser_toolkit.py@@ -127,7 +127,7 @@ def mock_browser_dependencies():@pytest.fixture(scope="function")-async def browser_toolkit_fixture():+def browser_toolkit_fixture():"""Create a HybridBrowserToolkit instance for testing."""mock_session = AsyncMock()mock_session.ensure_browser = AsyncMock()@@ -159,11 +159,6 @@ async def browser_toolkit_fixture():yield toolkit# Cleanup- try:- await toolkit.close_browser()- except Exception:- pass-if os.path.exists("test_cache"):shutil.rmtree("test_cache", ignore_errors=True)@@ -373,246 +368,95 @@ class TestHybridBrowserToolkit:toolkit._validate_ref("", "test_method")@pytest.mark.asyncio- async def test_open_browser_no_start_url(self):+ async def test_open_browser_no_start_url(self, browser_toolkit_fixture):"""Test opening browser using the default start URL configured during initialization."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session+ toolkit = browser_toolkit_fixture+ result = await toolkit.open_browser()- result = await toolkit.open_browser()-- assert result["result"] == "Visited test URL"- assert "snapshot" in result+ assert "Visited" in result["result"]+ assert "snapshot" in result@pytest.mark.asyncio- async def test_open_browser_with_custom_default_url(self):- """Test opening browser with a custom default start URL."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- # Create toolkit with custom default URL- toolkit = HybridBrowserToolkit(- headless=True, default_start_url=TEST_URL- )- toolkit._session = mock_session-- # Mock visit_page method- with patch.object(- toolkit, 'visit_page', new_callable=AsyncMock- ) as mock_visit:- mock_visit.return_value = {- "result": "Visited",- "snapshot": "test",- }-- await toolkit.open_browser()+ async def test_open_browser_with_custom_default_url(+ self, browser_toolkit_fixture+ ):+ """Test open_browser with a custom default start URL."""+ toolkit = browser_toolkit_fixture+ result = await toolkit.open_browser()- # Should call visit_page with the configured default URL- mock_visit.assert_called_once_with(TEST_URL)+ # Just check that the method works and returns expected structure+ assert "result" in result+ assert "snapshot" in result+ assert "Visited" in result['result']@pytest.mark.asyncio- async def test_close_browser(self):+ async def test_close_browser(self, browser_toolkit_fixture):"""Test closing browser."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session-- result = await toolkit.close_browser()-- assert result == "Browser session closed."+ toolkit = browser_toolkit_fixture+ result = await toolkit.close_browser()+ assert isinstance(result, str)+ assert "closed" in result.lower()@pytest.mark.asyncio- async def test_visit_page_valid_url(self):+ async def test_visit_page_valid_url(self, browser_toolkit_fixture):"""Test visiting a valid URL."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session+ toolkit = browser_toolkit_fixture+ mock_session = toolkit._session+ # Mock create_new_tab to return a mock tab index+ mock_session.create_new_tab = AsyncMock(return_value=1)+ mock_session.visit = AsyncMock(+ return_value=f"Visited {TEST_URL} in new tab 1"+ )+ mock_session.switch_to_tab = AsyncMock()+ mock_session.get_snapshot = AsyncMock(return_value="snapshot")++ # Mock _get_tab_info_for_output+ with patch.object(+ toolkit, '_get_tab_info_for_output', new_callable=AsyncMock+ ) as mock_get_tab_info:+ mock_get_tab_info.return_value = {+ "tabs": [],+ "current_tab": 1,+ "total_tabs": 2,+ }result = await toolkit.visit_page(TEST_URL)-- assert "Visited" in result["result"]+ mock_session.create_new_tab.assert_called_once_with(TEST_URL)+ mock_session.switch_to_tab.assert_called_once_with(1)+ mock_session.get_snapshot.assert_called_once()assert "snapshot" in result+ assert result['snapshot'] == "snapshot"+ assert TEST_URL in result['result']+ assert "new tab" in result['result']@pytest.mark.asyncio- async def test_visit_page_invalid_url(self):- """Test visiting with invalid URL."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session-- result = await toolkit.visit_page("")- assert "Error" in result.get("result", "")- assert "'url' must be a non-empty string" in result.get(- "result", ""- )+ async def test_visit_page_invalid_url(self, browser_toolkit_fixture):+ """Test visiting an invalid URL (empty string)."""+ toolkit = browser_toolkit_fixture+ mock_session = toolkit._session+ mock_session.create_new_tab = AsyncMock()+ result = await toolkit.visit_page("")+ mock_session.create_new_tab.assert_not_called()+ assert "Error: 'url' must be a non-empty string" in result['result']+ assert result['snapshot'] == ""@pytest.mark.asyncio- async def test_get_page_snapshot(self):+ async def test_get_page_snapshot(+ self, browser_toolkit_fixture, mock_page_fixture+ ):"""Test getting page snapshot."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session-- # Mock unified analysis- mock_analysis = {- "snapshotText": "Test snapshot content",- "elements": {"e1": {"role": "button", "name": "Test"}},- }-- # Ensure the mock page has the analysis- mock_page.evaluate = AsyncMock(return_value=mock_analysis)-- result = await toolkit.get_page_snapshot()-- assert result == "Test snapshot content"+ toolkit = browser_toolkit_fixture+ mock_page = mock_page_fixture+ toolkit._session.get_page = AsyncMock(return_value=mock_page)++ # Mock unified analysis+ mock_analysis = {+ "snapshotText": "Test snapshot content",+ "elements": {"e1": {"role": "button", "name": "Test"}},+ }+ mock_page.evaluate = AsyncMock(return_value=mock_analysis)+ result = await toolkit.get_page_snapshot()+ assert result == "Test snapshot content"@pytest.mark.asyncioasync def test_get_som_screenshot_success(self):@@ -808,82 +652,22 @@ class TestHybridBrowserToolkit:await toolkit.click(ref="")@pytest.mark.asyncio- async def test_type_text(self):+ async def test_type_text(self, browser_toolkit_fixture):"""Test typing text in element."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session-- result = await toolkit.type(- ref="input_element", text="Hello World"- )-- assert result["result"] == "Action executed successfully"- assert "snapshot" in result+ toolkit = browser_toolkit_fixture+ result = await toolkit.type(ref="input_element", text="Hello World")+ assert result["result"] == "Action executed successfully"+ assert "snapshot" in result@pytest.mark.asyncio- async def test_select_option(self):+ async def test_select_option(self, browser_toolkit_fixture):"""Test selecting option in dropdown."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()+ toolkit = browser_toolkit_fixture- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session-- result = await toolkit.select(- ref="select_element", value="option1"- )+ result = await toolkit.select(ref="select_element", value="option1")- assert result["result"] == "Action executed successfully"- assert "snapshot" in result+ assert result["result"] == "Action executed successfully"+ assert "snapshot" in result@pytest.mark.asyncioasync def test_scroll_valid_direction(self):@@ -902,7 +686,7 @@ class TestHybridBrowserToolkit:with (patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',+ 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.HybridBrowserSession',return_value=mock_session,),patch(@@ -939,7 +723,7 @@ class TestHybridBrowserToolkit:with (patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',+ 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.HybridBrowserSession',return_value=mock_session,),patch(@@ -962,48 +746,25 @@ class TestHybridBrowserToolkit:)@pytest.mark.asyncio- async def test_get_page_links_valid_refs(self):+ async def test_get_page_links_valid_refs(+ self, browser_toolkit_fixture, mock_page_fixture+ ):"""Test getting page links with valid references."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"- )- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"- )- mock_session.close = AsyncMock()-- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session-- # Mock page evaluate to return links- mock_links = [- {"href": "https://example.com/page1", "text": "Page 1"},- {"href": "https://example.com/page2", "text": "Page 2"},- ]- mock_page.evaluate = AsyncMock(return_value=mock_links)+ toolkit = browser_toolkit_fixture+ mock_page = mock_page_fixture+ toolkit._session.get_page = AsyncMock(return_value=mock_page)++ # Mock page evaluate to return links+ mock_links = [+ {"href": "https://example.com/page1", "text": "Page 1"},+ {"href": "https://example.com/page2", "text": "Page 2"},+ ]+ mock_page.evaluate = AsyncMock(return_value=mock_links)- result = await toolkit.get_page_links(ref=["e1", "e2"])+ result = await toolkit.get_page_links(ref=["e1", "e2"])- assert "links" in result- assert isinstance(result["links"], list)+ assert "links" in result+ assert isinstance(result["links"], list)@pytest.mark.asyncioasync def test_get_page_links_invalid_refs(self):@@ -1022,7 +783,7 @@ class TestHybridBrowserToolkit:with (patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',+ 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.HybridBrowserSession',return_value=mock_session,),patch(@@ -1057,7 +818,7 @@ class TestHybridBrowserToolkit:with (patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',+ 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.HybridBrowserSession',return_value=mock_session,),patch(@@ -1096,7 +857,7 @@ class TestHybridBrowserToolkit:with (patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',+ 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.HybridBrowserSession',return_value=mock_session,),patch(@@ -1171,7 +932,7 @@ class TestHybridBrowserToolkit:with (patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',+ 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.HybridBrowserSession',return_value=AsyncMock(),),patch(@@ -1212,7 +973,7 @@ class TestHybridBrowserToolkit:with (patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',+ 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.HybridBrowserSession',return_value=AsyncMock(),),patch(@@ -1321,39 +1082,170 @@ class TestHybridBrowserToolkit:assert result == test_image@pytest.mark.asyncio- async def test_simple_async_creation(self):+ async def test_simple_async_creation(self, browser_toolkit_fixture):"""Simple test to verify async toolkit creation works with mocks."""- mock_session = AsyncMock()- mock_session.ensure_browser = AsyncMock()- mock_session.get_page = AsyncMock(return_value=mock_page)- mock_session.visit = AsyncMock(return_value="Visited test URL")- mock_session.get_snapshot = AsyncMock(- return_value="- Page Snapshot\\n```yaml\\n<test content>\\n```"+ toolkit = browser_toolkit_fixture+ result = await toolkit.open_browser()+ assert "Visited" in result["result"]+ assert "snapshot" in result+++class TestActionExecutorTimeouts:+ """Test ActionExecutor timeout configuration functionality."""++ @pytest.fixture+ def mock_page(self):+ """Create a mock page for ActionExecutor tests."""+ page = AsyncMock()+ page.locator = MagicMock(return_value=AsyncMock())+ page.locator.return_value.count = AsyncMock(return_value=1)+ page.locator.return_value.first = AsyncMock()+ page.locator.return_value.first.click = AsyncMock()+ page.fill = AsyncMock()+ page.select_option = AsyncMock()+ page.wait_for_selector = AsyncMock()+ page.text_content = AsyncMock(return_value="test text")+ page.wait_for_load_state = AsyncMock()+ return page++ def test_action_executor_default_timeouts(self, mock_page):+ """Test that ActionExecutor uses default timeouts when none provided."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,)- mock_session.exec_action = AsyncMock(- return_value="Action executed successfully"++ executor = ActionExecutor(mock_page)++ assert executor.default_timeout == 3000+ assert executor.short_timeout == 1000++ def test_action_executor_constructor_timeouts(self, mock_page):+ """Test that ActionExecutor respects constructor timeout parameters."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,)- mock_session.close = AsyncMock()- with (- patch(- 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',- return_value=mock_session,- ),- patch(- 'camel.toolkits.hybrid_browser_toolkit.hybrid_browser_toolkit.os.path.join',- return_value='mock_script_path',- ),- patch(- 'builtins.open',- mock_open(read_data="console.log('mock script');"),- ),- ):- toolkit = HybridBrowserToolkit(headless=True)- toolkit._session = mock_session+ executor = ActionExecutor(+ mock_page, default_timeout=5000, short_timeout=2000+ )- # Test a simple async operation- result = await toolkit.open_browser()+ assert executor.default_timeout == 5000+ assert executor.short_timeout == 2000- assert result["result"] == "Visited test URL"- assert "snapshot" in result+ @patch.dict(+ 'os.environ',+ {+ 'HYBRID_BROWSER_DEFAULT_TIMEOUT': '7000',+ 'HYBRID_BROWSER_SHORT_TIMEOUT': '3500',+ },+ )+ def test_action_executor_environment_timeouts(self, mock_page):+ """Test that ActionExecutor respects environment variable timeouts."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,+ )++ executor = ActionExecutor(mock_page)++ assert executor.default_timeout == 7000+ assert executor.short_timeout == 3500++ @patch.dict(+ 'os.environ',+ {+ 'HYBRID_BROWSER_DEFAULT_TIMEOUT': '7000',+ 'HYBRID_BROWSER_SHORT_TIMEOUT': '3500',+ },+ )+ def test_action_executor_constructor_precedence_over_env(self, mock_page):+ """Test that constructor parameters take precedence over environment variables."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,+ )++ executor = ActionExecutor(+ mock_page, default_timeout=9000, short_timeout=4500+ )++ # Constructor params should override environment variables+ assert executor.default_timeout == 9000+ assert executor.short_timeout == 4500++ @patch.dict('os.environ', {'HYBRID_BROWSER_DEFAULT_TIMEOUT': '7000'})+ def test_action_executor_partial_env_override(self, mock_page):+ """Test partial environment variable override with constructor params."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,+ )++ executor = ActionExecutor(+ mock_page,+ short_timeout=2500, # Only override short_timeout+ )++ # Should use env var for default_timeout, constructor for short_timeout+ assert executor.default_timeout == 7000+ assert executor.short_timeout == 2500++ @pytest.mark.asyncio+ async def test_action_executor_uses_configured_timeouts_in_fill(+ self, mock_page+ ):+ """Test that ActionExecutor uses configured timeouts in operations."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,+ )++ executor = ActionExecutor(+ mock_page, default_timeout=6000, short_timeout=2500+ )++ # Test that fill operation uses the configured short_timeout+ action = {"type": "type", "ref": "test", "text": "hello"}+ await executor._type(action)++ mock_page.fill.assert_called_once()+ call_args = mock_page.fill.call_args+ assert call_args[1]["timeout"] == 2500 # Should use short_timeout++ @pytest.mark.asyncio+ async def test_action_executor_uses_configured_timeouts_in_select(+ self, mock_page+ ):+ """Test that ActionExecutor uses configured timeouts in select operations."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,+ )++ executor = ActionExecutor(+ mock_page, default_timeout=8000, short_timeout=3000+ )++ # Test that select operation uses the configured default_timeout+ action = {"type": "select", "ref": "test", "value": "option1"}+ await executor._select(action)++ mock_page.select_option.assert_called_once()+ call_args = mock_page.select_option.call_args+ assert call_args[1]["timeout"] == 8000 # Should use default_timeout++ @pytest.mark.asyncio+ async def test_action_executor_uses_configured_timeouts_in_wait(+ self, mock_page+ ):+ """Test that ActionExecutor uses configured timeouts in wait operations."""+ from camel.toolkits.hybrid_browser_toolkit.actions import (+ ActionExecutor,+ )++ executor = ActionExecutor(+ mock_page, default_timeout=7500, short_timeout=2750+ )++ # Test that wait operation uses the configured default_timeout+ action = {"type": "wait", "selector": "test"}+ await executor._wait(action)++ mock_page.wait_for_selector.assert_called_once()+ call_args = mock_page.wait_for_selector.call_args+ assert call_args[1]["timeout"] == 7500 # Should use default_timeout