🐫 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
chore: enhance non visual browser (#2705)
Puzhen Zhange0e01912b0ad
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlindex f867e4eec..0b2220972 100644--- a/.github/ISSUE_TEMPLATE/bug_report.yml+++ b/.github/ISSUE_TEMPLATE/bug_report.yml@@ -26,7 +26,7 @@ body:attributes:label: What version of camel are you using?description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.- placeholder: E.g., 0.2.69a2+ placeholder: E.g., 0.2.69a3validations:required: truediff --git a/camel/__init__.py b/camel/__init__.pyindex f24a99e0d..3d9963505 100644--- a/camel/__init__.py+++ b/camel/__init__.py@@ -14,7 +14,7 @@from camel.logger import disable_logging, enable_logging, set_log_level-__version__ = '0.2.69a2'+__version__ = '0.2.69a3'__all__ = ['__version__',diff --git a/camel/toolkits/non_visual_browser_toolkit/browser_non_visual_toolkit.py b/camel/toolkits/non_visual_browser_toolkit/browser_non_visual_toolkit.pyindex b513c18e6..cae8dd274 100644--- a/camel/toolkits/non_visual_browser_toolkit/browser_non_visual_toolkit.py+++ b/camel/toolkits/non_visual_browser_toolkit/browser_non_visual_toolkit.py@@ -15,15 +15,16 @@ from __future__ import annotationsfrom typing import Any, Dict, List, Optional+from camel.logger import get_loggerfrom camel.models import BaseModelBackendfrom camel.toolkits.base import BaseToolkitfrom camel.toolkits.function_tool import FunctionToolfrom .agent import PlaywrightLLMAgent--# session wrapperfrom .nv_browser_session import NVBrowserSession+logger = get_logger(__name__)+class BrowserNonVisualToolkit(BaseToolkit):r"""A lightweight, *non-visual* browser toolkit exposing primitive@@ -52,24 +53,50 @@ class BrowserNonVisualToolkit(BaseToolkit):self._agent: Optional[PlaywrightLLMAgent] = Nonedef __del__(self):- r"""Ensure cleanup when toolkit is garbage collected."""- # Note: __del__ cannot be async, so we schedule cleanup if needed- import asyncio-+ r"""Best-effort cleanup when toolkit is garbage collected.++ 1. We *avoid* running during the Python interpreter shutdown phase+ (`sys.is_finalizing()`), because the import machinery and/or event+ loop may already be torn down which leads to noisy exceptions such+ as `ImportError: sys.meta_path is None` or+ `RuntimeError: Event loop is closed`.+ 2. We protect all imports and event-loop operations with defensive+ `try/except` blocks. This ensures that, even if cleanup cannot be+ carried out, we silently ignore the failure instead of polluting+ stderr on program exit.+ """try:- loop = asyncio.get_event_loop()+ import sys++ if getattr(sys, "is_finalizing", lambda: False)():+ return # Skip cleanup during interpreter shutdown++ import asyncio++ try:+ loop = asyncio.get_event_loop()+ except RuntimeError:+ # No event loop in current thread → nothing to clean+ return++ if loop.is_closed():+ # Event loop already closed → cannot run async cleanup+ return+if loop.is_running():- task = loop.create_task(self.close_browser())- # Don't wait for completion to avoid blocking- del task+ try:+ task = loop.create_task(self.close_browser())+ del task # Fire-and-forget+ except RuntimeError:+ # Loop is running but not in this thread → ignore+ passelse:+ # Own the loop → safe to runasyncio.run(self.close_browser())except Exception:- pass # Don't fail during garbage collection+ # Suppress *all* errors during garbage collection+ pass- # ------------------------------------------------------------------- # Internal helpers- # ------------------------------------------------------------------async def _ensure_browser(self):await self._session.ensure_browser()@@ -78,28 +105,26 @@ class BrowserNonVisualToolkit(BaseToolkit):return await self._session.get_page()def _validate_ref(self, ref: str, method_name: str) -> None:- """Validate that ref parameter is a non-empty string."""+ r"""Validate that ref parameter is a non-empty string."""if not ref or not isinstance(ref, str):- raise ValueError(+ logger.error(f"{method_name}(): 'ref' must be a non-empty string, "f"got: {ref}")- # ------------------------------------------------------------------- # Tool implementations- # ------------------------------------------------------------------async def open_browser(self, start_url: Optional[str] = None) -> Dict[str, str]:r"""Launch a Playwright browser session.Args:- start_url (Optional[str]): If provided, the page will navigate to- this URL immediately after the browser launches.+ start_url (Optional[str]): Optional URL to navigate to immediately+ after the browser launches. If not provided, the browser will+ not navigate to any URL. (default: :obj:`None`)Returns:Dict[str, str]: Keys: ``result`` for action outcome,- ``snapshot`` for full DOM snapshot.+ ``snapshot`` for full DOM snapshot."""await self._session.ensure_browser()if start_url:@@ -128,16 +153,15 @@ class BrowserNonVisualToolkit(BaseToolkit):await self._session.close()return "Browser session closed."- # Navigation / page state ------------------------------------------------async def visit_page(self, url: str) -> Dict[str, str]:- """Navigate the current page to the specified URL.+ r"""Navigate the current page to the specified URL.Args:url (str): The destination URL.Returns:Dict[str, str]: Keys: ``result`` for action outcome,- ``snapshot`` for full DOM snapshot.+ ``snapshot`` for full DOM snapshot."""if not url or not isinstance(url, str):raise ValueError("visit_page(): 'url' must be a non-empty string")@@ -155,10 +179,10 @@ class BrowserNonVisualToolkit(BaseToolkit):Args:force_refresh (bool): When ``True`` always re-generate the- snapshot even- if the URL has not changed.- diff_only (bool): If ``True`` return only the diff relative to the- previous snapshot.+ snapshot even if the URL has not changed. (default:+ :obj:`False`)+ diff_only (bool): When ``True`` return only the diff relative to+ the previous snapshot. (default: :obj:`False`)Returns:str: Formatted snapshot string.@@ -167,13 +191,12 @@ class BrowserNonVisualToolkit(BaseToolkit):force_refresh=force_refresh, diff_only=diff_only)- # Element-level wrappers -------------------------------------------------async def click(self, *, ref: str) -> Dict[str, str]:r"""Click an element identified by ``ref``Args:- ref (str): Element reference ID extracted from snapshot (e.g.- ``"e3"``).+ ref (str): Element reference ID extracted from snapshot+ (e.g.``"e3"``).Returns:Dict[str, str]: Result message from ``ActionExecutor``.@@ -187,8 +210,8 @@ class BrowserNonVisualToolkit(BaseToolkit):r"""Type text into an input or textarea element.Args:- ref (str): Element reference ID extracted from snapshot (e.g.- ``"e3"``).+ ref (str): Element reference ID extracted from snapshot.+ (e.g.``"e3"``).text (str): The text to enter.Returns:@@ -218,67 +241,70 @@ class BrowserNonVisualToolkit(BaseToolkit):r"""Scroll the page.Args:- direction (str): ``"down"`` or ``"up"``.+ direction (str): Scroll direction, should be ``"down"`` or+ ``"up"``.amount (int): Pixel distance to scroll.Returns:Dict[str, str]: Execution result message."""if direction not in ("up", "down"):- raise ValueError("scroll(): 'direction' must be 'up' or 'down'")+ logger.error("scroll(): 'direction' must be 'up' or 'down'")+ return {+ "result": "scroll() Error: 'direction' must be 'up' or 'down'"+ }action = {"type": "scroll", "direction": direction, "amount": amount}return await self._exec_with_snapshot(action)- async def wait(- self, *, timeout_ms: int | None = None, selector: str | None = None- ) -> Dict[str, str]:- r"""Explicit wait utility.+ async def enter(self, *, ref: str) -> Dict[str, str]:+ r"""Press the Enter key.Args:- timeout_ms (Optional[int]): Milliseconds to sleep.- selector (Optional[str]): Wait until this CSS selector appears- in DOM.+ ref (str): Element reference ID to focus before pressing.Returns:Dict[str, str]: Execution result message."""- # Default to 1 000 ms sleep when no arguments provided- if timeout_ms is None and selector is None:- timeout_ms = 1000-- action: Dict[str, Any] = {"type": "wait"}- if timeout_ms is not None:- action["timeout"] = timeout_ms- if selector is not None:- action["selector"] = selector+ self._validate_ref(ref, "enter")++ action: Dict[str, Any] = {"type": "enter", "ref": ref}return await self._exec_with_snapshot(action)- async def extract(self, *, ref: str) -> Dict[str, str]:- r"""Extract text content from an element.+ async def wait_user(self, *, seconds: float = 1.0) -> Dict[str, str]:+ r"""Pause execution for a given amount of *real* time and then+ return a *full* page snapshot.++ This is a convenience wrapper around the existing wait action for+ scenarios where you encounter a CAPTCHA or need to pause for manual+ user input, and want to retrieve the complete DOM snapshot afterward.Args:- ref (str): Element reference ID obtained from snapshot.+ seconds (float): How long to sleep, expressed in seconds. Must+ be a positive number. (default: :obj:`1.0`)Returns:- Dict[str, str]: Extracted text or error message.+ Dict[str, str]: Keys ``result`` and ``snapshot``."""- self._validate_ref(ref, "extract")- return await self._exec_with_snapshot({"type": "extract", "ref": ref})+ if seconds is None or seconds <= 0:+ logger.error("wait_time(): 'seconds' must be a positive number")+ return {+ "result": "wait_time(): 'seconds' must be a positive number"+ }- async def enter(self, *, ref: str) -> Dict[str, str]:- r"""Press the Enter key.+ # Reuse underlying ActionExecutor's ``wait`` implementation (expects+ # ms)+ timeout_ms = int(seconds * 1000)+ action: Dict[str, Any] = {"type": "wait", "timeout": timeout_ms}- Args:- ref (str): Element reference ID to focus before pressing.+ # Execute the sleep via ActionExecutor (no snapshot diff expected)+ result = await self._exec(action)- Returns:- Dict[str, str]: Execution result message.- """- self._validate_ref(ref, "enter")-- action: Dict[str, Any] = {"type": "enter", "ref": ref}- return await self._exec_with_snapshot(action)+ # Always return a *full* snapshot after the pause+ snapshot = await self._session.get_snapshot(+ force_refresh=True, diff_only=False+ )+ return {"result": result, "snapshot": snapshot}# Helper to run through ActionExecutorasync def _exec(self, action: Dict[str, Any]) -> str:@@ -308,9 +334,6 @@ class BrowserNonVisualToolkit(BaseToolkit):return {"result": result, "snapshot": diff}- # ------------------------------------------------------------------- # Optional PlaywrightLLMAgent helpers- # ------------------------------------------------------------------def _ensure_agent(self) -> PlaywrightLLMAgent:r"""Create PlaywrightLLMAgent on first use if `web_agent_model`provided."""@@ -332,29 +355,34 @@ class BrowserNonVisualToolkit(BaseToolkit):self, task_prompt: str, start_url: str, max_steps: int = 15) -> str:r"""Use LLM agent to autonomously complete the task (requires- `web_agent_model`)."""+ `web_agent_model`).++ Args:+ task_prompt (str): The task prompt to complete.+ start_url (str): The URL to navigate to.+ max_steps (int): The maximum number of steps to take.+ (default: :obj:`15`)++ Returns:+ str: The result of the task.+ """agent = self._ensure_agent()await agent.navigate(start_url)await agent.process_command(task_prompt, max_steps=max_steps)return "Task processing finished - see stdout for detailed trace."- # ------------------------------------------------------------------- # Toolkit registration- # ------------------------------------------------------------------def get_tools(self) -> List[FunctionTool]:base_tools = [FunctionTool(self.open_browser),FunctionTool(self.close_browser),FunctionTool(self.visit_page),- FunctionTool(self.get_page_snapshot),FunctionTool(self.click),FunctionTool(self.type),FunctionTool(self.select),FunctionTool(self.scroll),- FunctionTool(self.wait),- FunctionTool(self.extract),FunctionTool(self.enter),+ FunctionTool(self.wait_user),]if self.web_agent_model is not None:diff --git a/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py b/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.pyindex 8a64dcad0..7310721ba 100644--- a/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py+++ b/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py@@ -55,10 +55,23 @@ class NVBrowserSession:self.snapshot: Optional[PageSnapshot] = Noneself.executor: Optional[ActionExecutor] = None+ # Protect browser initialisation against concurrent calls+ import asyncio++ self._ensure_lock: "asyncio.Lock" = asyncio.Lock()+# ------------------------------------------------------------------# Browser lifecycle helpers# ------------------------------------------------------------------async def ensure_browser(self) -> None:+ # Serialise initialisation to avoid race conditions where multiple+ # concurrent coroutine calls create multiple browser instances for+ # the same NVBrowserSession.+ async with self._ensure_lock:+ await self._ensure_browser_inner()++ # Moved original logic to helper+ async def _ensure_browser_inner(self) -> None:from playwright.async_api import async_playwrightif self._page is not None:@@ -80,12 +93,27 @@ class NVBrowserSession:self._browser = await pl.chromium.launch(headless=self._headless)self._context = await self._browser.new_context()+ from camel.logger import get_logger++ _dbg_logger = get_logger(__name__)+# Reuse an already open page (persistent context may restore last# session)if self._context.pages:self._page = self._context.pages[0]else:self._page = await self._context.new_page()++ # Debug information to help trace concurrency issues+ _dbg_logger.debug(+ "Session %s created browser=%s context=%s page=%s (url=%s)",+ hex(id(self)),+ hex(id(self._browser)) if self._browser else None,+ hex(id(self._context)) if self._context else None,+ hex(id(self._page)),+ self._page.url if self._page else "<none>",+ )+# helpersself.snapshot = PageSnapshot(self._page)self.executor = ActionExecutor(self._page)diff --git a/camel/toolkits/non_visual_browser_toolkit/snapshot.py b/camel/toolkits/non_visual_browser_toolkit/snapshot.pyindex 0c3e8a1e4..1c6fb0301 100644--- a/camel/toolkits/non_visual_browser_toolkit/snapshot.py+++ b/camel/toolkits/non_visual_browser_toolkit/snapshot.py@@ -99,16 +99,65 @@ class PageSnapshot:_snapshot_js_cache: Optional[str] = None # class-level cacheasync def _get_snapshot_direct(self) -> Optional[str]:- try:- if PageSnapshot._snapshot_js_cache is None:- js_path = Path(__file__).parent / "snapshot.js"- PageSnapshot._snapshot_js_cache = js_path.read_text(- encoding="utf-8"+ r"""Evaluate the snapshot-extraction JS with simple retry logic.++ Playwright throws *Execution context was destroyed* when a new page+ navigation happens between scheduling and evaluating the JS. In that+ case we retry a few times after waiting for the next DOMContentLoaded+ event; for all other exceptions we abort immediately.+ """++ # Load JS once and cache it at class level+ if PageSnapshot._snapshot_js_cache is None:+ js_path = Path(__file__).parent / "snapshot.js"+ PageSnapshot._snapshot_js_cache = js_path.read_text(+ encoding="utf-8"+ )++ js_code = PageSnapshot._snapshot_js_cache++ retries: int = 3+ while retries > 0:+ try:+ return await self.page.evaluate(js_code)+ except Exception as e:+ msg = str(e)++ # Typical error when navigation happens between calls+ nav_err = "Execution context was destroyed"++ if (+ nav_err in msg+ or "Most likely because of a navigation" in msg+ ):+ retries -= 1+ logger.debug(+ "Snapshot evaluate failed due to navigation; "+ "retrying (%d left)…",+ retries,+ )++ # Wait for next DOM stability before retrying+ try:+ await self.page.wait_for_load_state(+ "domcontentloaded", timeout=self.MAX_TIMEOUT_MS+ )+ except Exception:+ # Even if waiting fails, attempt retry to give it+ # one more chance+ pass++ continue # retry the evaluate()++ # Any other exception → abort+ logger.warning(+ "Failed to execute snapshot JavaScript: %s",+ e,)- return await self.page.evaluate(PageSnapshot._snapshot_js_cache)- except Exception as e:- logger.warning("Failed to execute snapshot JavaScript: %s", e)- return None+ return None++ logger.warning("Failed to execute snapshot JavaScript after retries")+ return None@staticmethoddef _format_snapshot(text: str) -> str:diff --git a/docs/conf.py b/docs/conf.pyindex 62b66d027..ef9ef3157 100644--- a/docs/conf.py+++ b/docs/conf.py@@ -27,7 +27,7 @@ sys.path.insert(0, os.path.abspath('..'))project = 'CAMEL'copyright = '2024, CAMEL-AI.org'author = 'CAMEL-AI.org'-release = '0.2.69a2'+release = '0.2.69a3'html_favicon = ('https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'diff --git a/examples/toolkits/browser_non_visual_toolkit_example.py b/examples/toolkits/browser_non_visual_toolkit_example.pyindex 38025169d..bdffe362c 100644--- a/examples/toolkits/browser_non_visual_toolkit_example.py+++ b/examples/toolkits/browser_non_visual_toolkit_example.py@@ -11,6 +11,7 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import asyncioimport loggingfrom camel.agents import ChatAgent@@ -30,15 +31,14 @@ logging.basicConfig(logging.getLogger('camel.agents').setLevel(logging.INFO)logging.getLogger('camel.models').setLevel(logging.INFO)-USER_DATA_DIR = r"User_Data"+USER_DATA_DIR = r"D:/User_Data"model_backend = ModelFactory.create(model_platform=ModelPlatformType.OPENAI,- model_type=ModelType.GPT_4O,+ model_type=ModelType.GPT_4_1,model_config_dict={"temperature": 0.0, "top_p": 1},)-web_toolkit = BrowserNonVisualToolkit(headless=False,user_data_dir=USER_DATA_DIR,@@ -51,13 +51,25 @@ agent = ChatAgent(tools=[*web_toolkit.get_tools()],)+TASK_PROMPT = """Click "Sign in to Google Account" on Google.+If a CAPTCHA appears requiring user input, call the wait_user function —+wait 5 seconds each time and keep waiting until you detect that the snapshot+no longer contains content requiring user action.+Make sure not to help the user fill in their account or CAPTCHA information.+Before the task is completed, you must call the tool in every session and+use the wait_user function whenever needed.++"""+++async def main() -> None:+ response = await agent.astep(TASK_PROMPT)-TASK_PROMPT = """Find the cheapest keyboard in Amazon,-open the product page and add it to cart."""+ print("Task:", TASK_PROMPT)+ print(f"Using user data directory: {USER_DATA_DIR}\n")+ print("Response from agent:")+ print(response.msgs[0].content if response.msgs else "<no response>")-response = agent.step(TASK_PROMPT)-print("Task:", TASK_PROMPT)-print(f"Using user data directory: {USER_DATA_DIR}\n")-print("Response from agent:")-print(response.msgs[0].content if response.msgs else "<no response>")+if __name__ == "__main__":+ asyncio.run(main())diff --git a/pyproject.toml b/pyproject.tomlindex 27dbe2fff..498a9a4e9 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "camel-ai"-version = "0.2.69a2"+version = "0.2.69a3"description = "Communicative Agents for AI Society Study"authors = [{ name = "CAMEL-AI.org" }]requires-python = ">=3.10,<3.13"diff --git a/uv.lock b/uv.lockindex 2cd7bcde2..188c501e8 100644--- a/uv.lock+++ b/uv.lock@@ -560,7 +560,7 @@ wheels = [[[package]]name = "camel-ai"-version = "0.2.69a2"+version = "0.2.69a3"source = { editable = "." }dependencies = [{ name = "colorama" },