🐫 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
feat:non visual browser method (#2674)
Puzhen Zhang30409cd516b4
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex b12e5e09d..d1e5354af 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -77,6 +77,7 @@ from .aci_toolkit import ACIToolkitfrom .playwright_mcp_toolkit import PlaywrightMCPToolkitfrom .wolfram_alpha_toolkit import WolframAlphaToolkitfrom .task_planning_toolkit import TaskPlanningToolkit+from .non_visual_browser_toolkit import BrowserNonVisualToolkit__all__ = [@@ -142,4 +143,5 @@ __all__ = ['WolframAlphaToolkit','BohriumToolkit','TaskPlanningToolkit',+ 'BrowserNonVisualToolkit',]diff --git a/camel/toolkits/non_visual_browser_toolkit/__init__.py b/camel/toolkits/non_visual_browser_toolkit/__init__.pynew file mode 100644index 000000000..0fff38941--- /dev/null+++ b/camel/toolkits/non_visual_browser_toolkit/__init__.py@@ -0,0 +1,18 @@+# ========= 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. =========+from .browser_non_visual_toolkit import BrowserNonVisualToolkit++__all__ = [+ "BrowserNonVisualToolkit",+]diff --git a/camel/toolkits/non_visual_browser_toolkit/actions.py b/camel/toolkits/non_visual_browser_toolkit/actions.pynew file mode 100644index 000000000..4c5cb3799--- /dev/null+++ b/camel/toolkits/non_visual_browser_toolkit/actions.py@@ -0,0 +1,196 @@+# ========= 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. =========+import asyncio+from typing import TYPE_CHECKING, Any, Dict++if TYPE_CHECKING:+ from playwright.async_api import Page+++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++ def __init__(self, page: "Page"):+ self.page = page++ # ------------------------------------------------------------------+ # Public helpers+ # ------------------------------------------------------------------+ async def execute(self, action: Dict[str, Any]) -> str:+ if not action:+ return "No action to execute"++ action_type = action.get("type")+ if not action_type:+ return "Error: action has no type"++ try:+ # small helper to ensure basic stability+ await self._wait_dom_stable()++ handler = {+ "click": self._click,+ "type": self._type,+ "select": self._select,+ "wait": self._wait,+ "extract": self._extract,+ "scroll": self._scroll,+ "enter": self._enter,+ }.get(action_type)++ if handler is None:+ return f"Error: Unknown action type '{action_type}'"++ return await handler(action)+ except Exception as exc:+ return f"Error executing {action_type}: {exc}"++ # ------------------------------------------------------------------+ # Internal handlers+ # ------------------------------------------------------------------+ async def _click(self, action: Dict[str, Any]) -> str:+ ref = action.get("ref")+ text = action.get("text")+ selector = action.get("selector")+ if not (ref or text or selector):+ return "Error: click requires ref/text/selector"++ strategies = []+ if selector:+ strategies.append(selector)+ if text:+ strategies.append(f'text="{text}"')+ if ref:+ strategies.append(f"[aria-ref='{ref}']")++ for sel in strategies:+ try:+ if await self.page.locator(sel).count() > 0:+ await self.page.click(+ sel, timeout=self.SHORT_TIMEOUT, force=True+ )+ return f"Clicked element via {sel}"+ except Exception:+ pass+ return "Error: Could not click element"++ async def _type(self, action: Dict[str, Any]) -> str:+ ref = action.get("ref")+ selector = action.get("selector")+ text = action.get("text", "")+ if not (ref or selector):+ return "Error: type requires ref/selector"+ target = selector or f"[aria-ref='{ref}']"+ try:+ await self.page.fill(target, text, timeout=self.SHORT_TIMEOUT)+ return f"Typed '{text}' into {target}"+ except Exception as exc:+ return f"Type failed: {exc}"++ async def _select(self, action: Dict[str, Any]) -> str:+ ref = action.get("ref")+ selector = action.get("selector")+ value = action.get("value", "")+ if not (ref or selector):+ return "Error: select requires ref/selector"+ target = selector or f"[aria-ref='{ref}']"+ try:+ await self.page.select_option(+ target, value, timeout=self.DEFAULT_TIMEOUT+ )+ return f"Selected '{value}' in {target}"+ except Exception as exc:+ return f"Select failed: {exc}"++ async def _wait(self, action: Dict[str, Any]) -> str:+ if "timeout" in action:+ ms = action["timeout"]+ await asyncio.sleep(ms / 1000)+ return f"Waited {ms}ms"+ if "selector" in action:+ sel = action["selector"]+ await self.page.wait_for_selector(+ sel, timeout=self.DEFAULT_TIMEOUT+ )+ return f"Waited for {sel}"+ return "Error: wait requires timeout/selector"++ async def _extract(self, action: Dict[str, Any]) -> str:+ ref = action.get("ref")+ if not ref:+ return "Error: extract requires ref"+ target = f"[aria-ref='{ref}']"+ await self.page.wait_for_selector(target, timeout=self.DEFAULT_TIMEOUT)+ txt = await self.page.text_content(target)+ return f"Extracted: {txt[:100] if txt else 'None'}"++ async def _scroll(self, action: Dict[str, Any]) -> str:+ direction = action.get("direction", "down")+ amount = action.get("amount", 300)++ # Validate inputs to prevent injection+ if direction not in ("up", "down"):+ return "Error: direction must be 'up' or 'down'"++ try:+ # Safely convert amount to integer and clamp to reasonable range+ amount_int = int(amount)+ amount_int = max(+ -5000, min(5000, amount_int)+ ) # Clamp between -5000 and 5000+ except (ValueError, TypeError):+ return "Error: amount must be a valid number"++ # Use safe evaluation with bound parameters+ scroll_offset = amount_int if direction == "down" else -amount_int+ await self.page.evaluate(f"window.scrollBy(0, {scroll_offset})")+ await asyncio.sleep(0.5)+ return f"Scrolled {direction} by {abs(amount_int)}px"++ async def _enter(self, action: Dict[str, Any]) -> str:+ ref = action.get("ref")+ selector = action.get("selector")+ if ref:+ await self.page.focus(f"[aria-ref='{ref}']")+ elif selector:+ await self.page.focus(selector)+ await self.page.keyboard.press("Enter")+ await asyncio.sleep(0.3)+ return "Pressed Enter"++ # utilities+ async def _wait_dom_stable(self) -> None:+ try:+ await self.page.wait_for_load_state(+ 'domcontentloaded', timeout=self.SHORT_TIMEOUT+ )+ except Exception:+ pass++ # static helpers+ @staticmethod+ def should_update_snapshot(action: Dict[str, Any]) -> bool:+ change_types = {+ "click",+ "type",+ "select",+ "scroll",+ "navigate",+ "enter",+ }+ return action.get("type") in change_typesdiff --git a/camel/toolkits/non_visual_browser_toolkit/agent.py b/camel/toolkits/non_visual_browser_toolkit/agent.pynew file mode 100644index 000000000..1e166b0cf--- /dev/null+++ b/camel/toolkits/non_visual_browser_toolkit/agent.py@@ -0,0 +1,278 @@+# ========= 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. =========+import json+import logging+import re+from typing import TYPE_CHECKING, Any, Dict, List, Optional++from camel.models import BaseModelBackend, ModelFactory+from camel.types import ModelPlatformType, ModelType++from .actions import ActionExecutor+from .nv_browser_session import NVBrowserSession++if TYPE_CHECKING:+ from camel.agents import ChatAgent++logger = logging.getLogger(__name__)+++class PlaywrightLLMAgent:+ """High-level orchestration: snapshot ↔ LLM ↔ action executor."""++ # System prompt as class constant to avoid recreation+ SYSTEM_PROMPT = """+You are a web automation assistant.++" Analyse the page snapshot and create a short high-level plan, "+"then output the FIRST action to start with.\n\n"+"Return a JSON object in *exactly* this shape:\n"+"Action format json_object examples:\n"+"{\n \"plan\": [\"Step 1\", \"Step 2\"],\n \"action\": {\n \"type\":+\"click\",\n \"ref\": \"e1\"\n }\n}\n\n"+"If task is already complete:\n"+"{\n \"plan\": [],\n \"action\": {\n \"type\": \"finish\",+\n \"ref\": null,\n \"summary\": \"Task was already completed. Summary+of what was found...\"\n }\n}"++Available action types:+- 'click': {"type": "click", "ref": "e1"} or {"type": "click", "text":+"Button Text"} or {"type": "click", "selector": "button"}+- 'type': {"type": "type", "ref": "e1", "text": "search text"} or {"type":+"type", "selector": "input", "text": "search text"}+- 'select': {"type": "select", "ref": "e1", "value": "option"} or {"type":+"select", "selector": "select", "value": "option"}+- 'wait': {"type": "wait", "timeout": 2000} or {"type": "wait", "selector":+"#element"}+- 'scroll': {"type": "scroll", "direction": "down", "amount": 300}+- 'enter': {"type": "enter", "ref": "e1"} or {"type": "enter", "selector":+"input[name=q]"} or {"type": "enter"}+- 'navigate': {"type": "navigate", "url": "https://example.com"}+- 'finish': {"type": "finish", "ref": null, "summary": "task completion+summary"}++IMPORTANT:+- For 'click': Use 'ref' from snapshot, or 'text' for visible text,+or 'selector' for CSS selectors+- For 'type'/'select': Use 'ref' from snapshot or 'selector' for CSS selectors+- Only use 'ref' values that exist in the snapshot (e.g., ref=e1, ref=e2, etc.)+- Use 'finish' when the task is completed successfully with a summary of+what was accomplished+- Use 'enter' to press the Enter key (optionally focus an element first)+- Use 'navigate' to open a new URL before interacting further+- click can choose radio, checkbox...+ """++ def __init__(+ self,+ *,+ user_data_dir: Optional[str] = None,+ headless: bool = False,+ model_backend: Optional[BaseModelBackend] = None,+ ):+ self._session = NVBrowserSession(+ headless=headless, user_data_dir=user_data_dir+ )+ from camel.agents import ChatAgent++ # Populated lazily after first page load+ self.action_history: List[Dict[str, Any]] = []+ if model_backend is None:+ model_backend = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAI,+ model_type=ModelType.GPT_4O_MINI,+ model_config_dict={"temperature": 0, "top_p": 1},+ )+ self.model_backend = model_backend+ # Reuse ChatAgent instance to avoid recreation overhead+ self._chat_agent: Optional[ChatAgent] = None++ async def navigate(self, url: str) -> str:+ try:+ # NVBrowserSession handles waits internally+ logger.debug("Navigated to URL: %s", url)+ await self._session.visit(url)+ return await self._session.get_snapshot(force_refresh=True)+ except Exception as exc:+ return f"Error: could not navigate - {exc}"++ def _get_chat_agent(self) -> "ChatAgent":+ """Get or create the ChatAgent instance."""+ from camel.agents import ChatAgent++ if self._chat_agent is None:+ self._chat_agent = ChatAgent(+ system_message=self.SYSTEM_PROMPT, model=self.model_backend+ )+ return self._chat_agent++ def _safe_parse_json(self, content: str) -> Dict[str, Any]:+ r"""Safely parse JSON from LLM response with multiple fallback+ strategies.+ """+ # First attempt: direct parsing+ try:+ return json.loads(content)+ except json.JSONDecodeError:+ pass++ # Second attempt: extract JSON-like block using regex+ # Look for content between outermost braces+ json_pattern = re.compile(+ r'\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}', re.DOTALL+ )+ json_matches = json_pattern.findall(content)++ for match in json_matches:+ try:+ return json.loads(match)+ except json.JSONDecodeError:+ continue++ # Third attempt: try to find and parse line by line+ lines = content.split('\n')+ json_lines = []+ in_json = False++ for line in lines:+ line = line.strip()+ if line.startswith('{'):+ in_json = True+ json_lines = [line]+ elif in_json:+ json_lines.append(line)+ if line.endswith('}'):+ try:+ json_text = '\n'.join(json_lines)+ return json.loads(json_text)+ except json.JSONDecodeError:+ pass+ in_json = False+ json_lines = []++ # Fallback: return default structure+ logger.warning(+ "Could not parse JSON from LLM response: %s", content[:200]+ )+ return {+ "plan": ["Could not parse response"],+ "action": {+ "type": "finish",+ "ref": None,+ "summary": "Parsing error",+ },+ }++ def _llm_call(+ self,+ prompt: str,+ snapshot: str,+ is_initial: bool,+ history: Optional[List[Dict[str, Any]]] = None,+ ) -> Dict[str, Any]:+ """Call the LLM (via CAMEL ChatAgent) to get plan & next action."""+ # Build user message+ if is_initial:+ user_content = f"Snapshot:\n{snapshot}\n\nTask: {prompt}"+ else:+ hist_lines = [+ (+ f"{i + 1}. {'✅' if h['success'] else '❌'} "+ f"{h['action']['type']} -> {h['result']}"+ )+ for i, h in enumerate(history or [])+ ]+ user_content = (+ f"Snapshot:\n{snapshot}\n\nHistory:\n"+ + "\n".join(hist_lines)+ + f"\n\nTask: {prompt}"+ )++ # Run ChatAgent+ chat_agent = self._get_chat_agent()+ response = chat_agent.step(user_content)+ content = response.msgs[0].content if response.msgs else "{}"++ # Safely parse JSON response+ return self._safe_parse_json(content)++ async def process_command(self, prompt: str, max_steps: int = 15):+ # initial full snapshot+ full_snapshot = await self._session.get_snapshot()+ assert self._session.snapshot is not None+ meta = self._session.snapshot.last_info+ logger.info("Initial snapshot priorities=%s", meta["priorities"])+ logger.debug("Full snapshot:\n%s", full_snapshot)++ plan_resp = self._llm_call(+ prompt, full_snapshot or "", is_initial=True+ )+ plan = plan_resp.get("plan", [])+ action = plan_resp.get("action")++ logger.info("Plan generated: %s", json.dumps(plan, ensure_ascii=False))++ steps = 0+ while action and steps < max_steps:+ if action.get("type") == "finish":+ logger.info("Task finished: %s", action.get("summary", "Done"))+ break++ result = await self._run_action(action)+ logger.debug("Executed action: %s | Result: %s", action, result)++ self.action_history.append(+ {+ "action": action,+ "result": result,+ "success": "Error" not in result,+ }+ )++ diff_snapshot = await self._session.get_snapshot(+ force_refresh=ActionExecutor.should_update_snapshot(action),+ diff_only=True,+ )+ assert self._session.snapshot is not None+ meta = self._session.snapshot.last_info+ logger.debug(+ "Snapshot after action (diff=%s):\n%s",+ meta["is_diff"],+ diff_snapshot,+ )++ # Update full snapshot if page changed+ if meta["is_diff"] and not diff_snapshot.startswith(+ "- Page Snapshot (no structural changes)"+ ):+ assert self._session.snapshot is not None+ full_snapshot = self._session.snapshot.snapshot_data or ""++ action = self._llm_call(+ prompt,+ full_snapshot or "",+ is_initial=False,+ history=self.action_history,+ ).get("action")+ steps += 1++ logger.info("Process completed with %d steps", steps)++ async def _run_action(self, action: Dict[str, Any]) -> str:+ if action.get("type") == "navigate":+ return await self.navigate(action.get("url", ""))+ return await self._session.exec_action(action)++ async def close(self):+ await self._session.close()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.pynew file mode 100644index 000000000..b513c18e6--- /dev/null+++ b/camel/toolkits/non_visual_browser_toolkit/browser_non_visual_toolkit.py@@ -0,0 +1,363 @@+# ========= 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. =========+from __future__ import annotations++from typing import Any, Dict, List, Optional++from camel.models import BaseModelBackend+from camel.toolkits.base import BaseToolkit+from camel.toolkits.function_tool import FunctionTool++from .agent import PlaywrightLLMAgent++# session wrapper+from .nv_browser_session import NVBrowserSession+++class BrowserNonVisualToolkit(BaseToolkit):+ r"""A lightweight, *non-visual* browser toolkit exposing primitive+ Playwright actions as CAMEL `FunctionTool`s.+ """++ def __init__(+ self,+ *,+ headless: bool = True,+ user_data_dir: Optional[str] = None,+ web_agent_model: Optional[BaseModelBackend] = None,+ ) -> None:+ super().__init__()+ self._headless = headless+ self._user_data_dir = user_data_dir+ self.web_agent_model = web_agent_model # Currently unused but kept+ # for compatibility++ # Encapsulated browser session+ self._session = NVBrowserSession(+ headless=headless, user_data_dir=user_data_dir+ )++ # Optional higher-level agent (only if user supplies model)+ self._agent: Optional[PlaywrightLLMAgent] = None++ def __del__(self):+ r"""Ensure cleanup when toolkit is garbage collected."""+ # Note: __del__ cannot be async, so we schedule cleanup if needed+ import asyncio++ try:+ loop = asyncio.get_event_loop()+ if loop.is_running():+ task = loop.create_task(self.close_browser())+ # Don't wait for completion to avoid blocking+ del task+ else:+ asyncio.run(self.close_browser())+ except Exception:+ pass # Don't fail during garbage collection++ # ------------------------------------------------------------------+ # Internal helpers+ # ------------------------------------------------------------------+ async def _ensure_browser(self):+ await self._session.ensure_browser()++ async def _require_page(self):+ await self._session.ensure_browser()+ 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."""+ if not ref or not isinstance(ref, str):+ raise ValueError(+ 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.++ Returns:+ Dict[str, str]: Keys: ``result`` for action outcome,+ ``snapshot`` for full DOM snapshot.+ """+ await self._session.ensure_browser()+ if start_url:+ return await self.visit_page(start_url)+ # If no start_url provided, still capture initial snapshot+ snapshot = await self._session.get_snapshot(+ force_refresh=True, diff_only=False+ )+ return {"result": "Browser session started.", "snapshot": snapshot}++ async def close_browser(self) -> str:+ r"""Terminate the current browser session and free all resources.++ Returns:+ str: Confirmation message.+ """+ # Close agent if it exists+ if self._agent is not None:+ try:+ await self._agent.close()+ except Exception:+ pass # Don't fail if agent cleanup fails+ self._agent = None++ # Close session+ 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.++ Args:+ url (str): The destination URL.++ Returns:+ Dict[str, str]: Keys: ``result`` for action outcome,+ ``snapshot`` for full DOM snapshot.+ """+ if not url or not isinstance(url, str):+ raise ValueError("visit_page(): 'url' must be a non-empty string")++ nav_result = await self._session.visit(url)+ snapshot = await self._session.get_snapshot(+ force_refresh=True, diff_only=False+ )+ return {"result": nav_result, "snapshot": snapshot}++ async def get_page_snapshot(+ self, *, force_refresh: bool = False, diff_only: bool = False+ ) -> str:+ r"""Capture a YAML-like structural snapshot of the DOM.++ 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.++ Returns:+ str: Formatted snapshot string.+ """+ return await self._session.get_snapshot(+ 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"``).++ Returns:+ Dict[str, str]: Result message from ``ActionExecutor``.+ """+ self._validate_ref(ref, "click")++ action: Dict[str, Any] = {"type": "click", "ref": ref}+ return await self._exec_with_snapshot(action)++ async def type(self, *, ref: str, text: str) -> Dict[str, str]:+ r"""Type text into an input or textarea element.++ Args:+ ref (str): Element reference ID extracted from snapshot (e.g.+ ``"e3"``).+ text (str): The text to enter.++ Returns:+ Dict[str, str]: Execution result message.+ """+ self._validate_ref(ref, "type")++ action: Dict[str, Any] = {"type": "type", "ref": ref, "text": text}+ return await self._exec_with_snapshot(action)++ async def select(self, *, ref: str, value: str) -> Dict[str, str]:+ r"""Select an option in a ``<select>`` element.++ Args:+ ref (str): Element reference ID.+ value (str): The value / option to select.++ Returns:+ Dict[str, str]: Execution result message.+ """+ self._validate_ref(ref, "select")++ action: Dict[str, Any] = {"type": "select", "ref": ref, "value": value}+ return await self._exec_with_snapshot(action)++ async def scroll(self, *, direction: str, amount: int) -> Dict[str, str]:+ r"""Scroll the page.++ Args:+ direction (str): ``"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'")++ 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.++ Args:+ timeout_ms (Optional[int]): Milliseconds to sleep.+ selector (Optional[str]): Wait until this CSS selector appears+ in DOM.++ 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+ return await self._exec_with_snapshot(action)++ async def extract(self, *, ref: str) -> Dict[str, str]:+ r"""Extract text content from an element.++ Args:+ ref (str): Element reference ID obtained from snapshot.++ Returns:+ Dict[str, str]: Extracted text or error message.+ """+ self._validate_ref(ref, "extract")+ return await self._exec_with_snapshot({"type": "extract", "ref": ref})++ async def enter(self, *, ref: str) -> Dict[str, str]:+ r"""Press the Enter key.++ Args:+ ref (str): Element reference ID to focus before pressing.++ 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)++ # Helper to run through ActionExecutor+ async def _exec(self, action: Dict[str, Any]) -> str:+ return await self._session.exec_action(action)++ async def _exec_with_snapshot(+ self, action: Dict[str, Any]+ ) -> Dict[str, str]:+ r"""Execute action and, if DOM structure changed, include snapshot+ diff.+ """+ result = await self._session.exec_action(action)++ # Only capture diff if action type typically changes DOM+ from .actions import ActionExecutor++ if not ActionExecutor.should_update_snapshot(action):+ return {"result": result}++ # Capture structural diff to previous snapshot+ diff = await self._session.get_snapshot(+ force_refresh=True, diff_only=True+ )++ if diff.startswith("- Page Snapshot (no structural changes)"):+ return {"result": result}++ return {"result": result, "snapshot": diff}++ # ------------------------------------------------------------------+ # Optional PlaywrightLLMAgent helpers+ # ------------------------------------------------------------------+ def _ensure_agent(self) -> PlaywrightLLMAgent:+ r"""Create PlaywrightLLMAgent on first use if `web_agent_model`+ provided."""+ if self.web_agent_model is None:+ raise RuntimeError(+ "web_agent_model not supplied - high-level task planning is "+ "unavailable."+ )++ if self._agent is None:+ self._agent = PlaywrightLLMAgent(+ headless=self._headless,+ user_data_dir=self._user_data_dir,+ model_backend=self.web_agent_model,+ )+ return self._agent++ async def solve_task(+ 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`)."""++ 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),+ ]++ if self.web_agent_model is not None:+ base_tools.append(FunctionTool(self.solve_task))++ return base_toolsdiff --git a/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py b/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.pynew file mode 100644index 000000000..3d08170f8--- /dev/null+++ b/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py@@ -0,0 +1,181 @@+# ========= 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. =========+from __future__ import annotations++from pathlib import Path+from typing import TYPE_CHECKING, Any, Optional++from playwright.async_api import (+ Browser,+ BrowserContext,+ Page,+)++from .actions import ActionExecutor+from .snapshot import PageSnapshot++if TYPE_CHECKING:+ from playwright.async_api import (+ Browser,+ BrowserContext,+ Page,+ Playwright,+ )+++class NVBrowserSession:+ """Lightweight wrapper around Playwright for non-visual (headless)+ browsing.++ It provides a single *Page* instance plus helper utilities (snapshot &+ executor). Multiple toolkits or agents can reuse this class without+ duplicating Playwright setup code.+ """++ # Configuration constants+ DEFAULT_NAVIGATION_TIMEOUT = 10000 # 10 seconds+ NETWORK_IDLE_TIMEOUT = 5000 # 5 seconds++ def __init__(+ self, *, headless: bool = True, user_data_dir: Optional[str] = None+ ):+ self._headless = headless+ self._user_data_dir = user_data_dir++ self._playwright: Optional[Playwright] = None+ self._browser: Optional[Browser] = None+ self._context: Optional[BrowserContext] = None+ self._page: Optional[Page] = None++ self.snapshot: Optional[PageSnapshot] = None+ self.executor: Optional[ActionExecutor] = None++ # ------------------------------------------------------------------+ # Browser lifecycle helpers+ # ------------------------------------------------------------------+ async def ensure_browser(self) -> None:+ from playwright.async_api import async_playwright++ if self._page is not None:+ return++ self._playwright = await async_playwright().start()+ if self._user_data_dir:+ Path(self._user_data_dir).mkdir(parents=True, exist_ok=True)+ pl = self._playwright+ assert pl is not None+ self._context = await pl.chromium.launch_persistent_context(+ user_data_dir=self._user_data_dir,+ headless=self._headless,+ )+ self._browser = self._context.browser+ else:+ pl = self._playwright+ assert pl is not None+ self._browser = await pl.chromium.launch(headless=self._headless)+ self._context = await self._browser.new_context()++ # 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()+ # helpers+ self.snapshot = PageSnapshot(self._page)+ self.executor = ActionExecutor(self._page)++ async def close(self) -> None:+ r"""Close all browser resources, ensuring cleanup even if some+ operations fail.+ """+ errors: list[str] = []++ # Close context first (which closes pages)+ if self._context is not None:+ try:+ await self._context.close()+ except Exception as e:+ errors.append(f"Context close error: {e}")++ # Close browser+ if self._browser is not None:+ try:+ await self._browser.close()+ except Exception as e:+ errors.append(f"Browser close error: {e}")++ # Stop playwright+ if self._playwright is not None:+ try:+ await self._playwright.stop()+ except Exception as e:+ errors.append(f"Playwright stop error: {e}")++ # Reset all references+ self._playwright = self._browser = self._context = self._page = None+ self.snapshot = self.executor = None++ # Log errors if any occurred during cleanup+ if errors:+ from camel.logger import get_logger++ logger = get_logger(__name__)+ logger.warning(+ "Errors during browser session cleanup: %s", "; ".join(errors)+ )++ # ------------------------------------------------------------------+ # Convenience wrappers around common actions+ # ------------------------------------------------------------------+ async def visit(self, url: str) -> str:+ await self.ensure_browser()+ assert self._page is not None++ try:+ await self._page.goto(+ url,+ wait_until="domcontentloaded",+ timeout=self.DEFAULT_NAVIGATION_TIMEOUT,+ )+ # Try to wait for network idle, but don't fail if it times out+ try:+ await self._page.wait_for_load_state(+ "networkidle", timeout=self.NETWORK_IDLE_TIMEOUT+ )+ except Exception:+ pass # Network idle timeout is not critical+ return f"Visited {url}"+ except Exception as e:+ return f"Error visiting {url}: {e}"++ async def get_snapshot(+ self, *, force_refresh: bool = False, diff_only: bool = False+ ) -> str:+ await self.ensure_browser()+ assert self.snapshot is not None+ return await self.snapshot.capture(+ force_refresh=force_refresh, diff_only=diff_only+ )++ async def exec_action(self, action: dict[str, Any]) -> str:+ await self.ensure_browser()+ assert self.executor is not None+ return await self.executor.execute(action)++ # Low-level accessors -------------------------------------------------+ async def get_page(self) -> "Page":+ await self.ensure_browser()+ assert self._page is not None+ return self._pagediff --git a/camel/toolkits/non_visual_browser_toolkit/snapshot.js b/camel/toolkits/non_visual_browser_toolkit/snapshot.jsnew file mode 100644index 000000000..844d1432b--- /dev/null+++ b/camel/toolkits/non_visual_browser_toolkit/snapshot.js@@ -0,0 +1,188 @@+(() => {+ // Store each element as {text, priority, depth}+ const elements = [];++ // Maximum lines allowed before we start dropping lower-priority nodes+ const MAX_LINES = 400;++ // Priority helper – lower number = higher priority+ function getPriority(tag, role, text) {+ // 1. Interactive elements+ if (["input", "button", "a", "select", "textarea"].includes(tag)) return 1;+ if (["checkbox", "radio"].includes(role)) return 1;++ // 2. Labels / descriptive adjacent text (label elements)+ if (tag === "label") return 2;++ // 3. General visible text+ if (text) return 3;++ // 4. Low-value structural nodes+ return 4;+ }++ function isVisible(node) {+ const rect = node.getBoundingClientRect();+ if (rect.width === 0 || rect.height === 0) return false;++ const style = window.getComputedStyle(node);+ if (style.display === 'none' || style.visibility === 'hidden') return false;++ return true;+ }++ function getRole(node) {+ const tag = node.tagName.toLowerCase();+ const type = node.getAttribute('type');++ if (node.getAttribute('role')) return node.getAttribute('role');++ if (tag === 'input') {+ if (type === 'checkbox') return 'checkbox';+ if (type === 'radio') return 'radio';+ return 'input';+ }++ if (tag === 'button') return 'button';+ if (tag === 'a') return 'link';+ if (tag === 'select') return 'select';+ if (tag === 'textarea') return 'textarea';+ if (tag === 'p') return 'paragraph';+ if (tag === 'span') return 'text';++ return 'generic';+ }++ function getAccessibleName(node) {+ if (node.hasAttribute('aria-label')) {+ return node.getAttribute('aria-label');+ }+ if (node.hasAttribute('aria-labelledby')) {+ const id = node.getAttribute('aria-labelledby');+ const labelEl = document.getElementById(id);+ if (labelEl) return labelEl.textContent.trim();+ }+ if (node.hasAttribute('title')) {+ return node.getAttribute('title');+ }++ const tagName = node.tagName?.toLowerCase();+ if (['style', 'script', 'meta', 'noscript', 'svg'].includes(tagName)) {+ return '';+ }++ const text = node.textContent?.trim() || '';++ // Ignore styles, tokens, or long CSS-like expressions+ if (/^[.#]?[a-zA-Z0-9\-_]+\s*\{[^}]*\}/.test(text)) return '';+ if ((text.match(/[;:{}]/g)?.length || 0) > 2) return '';++ return text.replace(/[^\w\u4e00-\u9fa5\s\-.,?!'"()()]/g, '').trim();+ }++ let refCounter = 1;++ function traverse(node, depth) {+ if (node.nodeType !== Node.ELEMENT_NODE) return;+ if (!isVisible(node)) return;++ const tagName = node.tagName.toLowerCase();+ const text = getAccessibleName(node).slice(0, 50);++ // Skip unlabeled links (anchors without any accessible name)+ if (tagName === 'a' && !text) {+ // Skip unlabeled links; process children if any+ for (const child of node.children) {+ traverse(child, depth + 1);+ }+ return;+ }++ const hasRoleOrText = ['button', 'a', 'input', 'select', 'textarea', 'p', 'span'].includes(tagName) ||+ node.getAttribute('role') || text;++ if (hasRoleOrText) {+ const role = getRole(node);+ const ref = `e${refCounter++}`;+ const label = text ? `"${text}"` : '';++ // Raw line (without indent) – we will apply indentation later once we know+ // which ancestor lines survive filtering so that indentation always reflects+ // the visible hierarchy.+ const lineText = `- ${role}${label ? ` ${label}` : ''} [ref=${ref}]`;+ const priority = getPriority(tagName, role, text);++ elements.push({ text: lineText, priority, depth });++ // Always inject ref so Playwright can still locate the element even if line is later filtered out.+ node.setAttribute('aria-ref', ref);+ }++ for (const child of node.children) {+ traverse(child, depth + 1);+ }+ }++ function processDocument(doc, depth = 0) {+ try {+ traverse(doc.body, depth);+ } catch (e) {+ // Handle docs without body (e.g., about:blank)+ }++ const frames = doc.querySelectorAll('iframe');+ for (const frame of frames) {+ try {+ if (frame.contentDocument) {+ processDocument(frame.contentDocument, depth + 1);+ }+ } catch (e) {+ // Skip cross-origin iframes+ }+ }+ }++ processDocument(document);++ // Always drop priority-4 nodes (low-value structural or invisible)+ let finalElements = elements.filter(el => el.priority <= 3);++ // Additional size condensation when still exceeding MAX_LINES+ if (finalElements.length > MAX_LINES) {+ const filterBy = (maxPriority) => finalElements.filter(el => el.priority <= maxPriority);++ // Progressively tighten: keep 1-3, then 1-2, finally only 1+ for (const limit of [3, 2, 1]) {+ const candidate = filterBy(limit);+ if (candidate.length <= MAX_LINES || limit === 1) {+ finalElements = candidate;+ break;+ }+ }+ }++ // ------------------------------------------------------------------+ // Re-apply indentation so that it matches the *visible* hierarchy only.+ // Whenever an ancestor element is removed due to priority rules, its+ // children will be re-indented one level up so the structure remains+ // intuitive.+ // ------------------------------------------------------------------+ const outputLines = [];+ const depthStack = []; // keeps track of kept original depths++ for (const el of finalElements) {+ // Pop depths that are not ancestors of current element+ while (depthStack.length && depthStack[depthStack.length - 1] >= el.depth) {+ depthStack.pop();+ }++ // Push the current depth so future descendants know their ancestor chain+ depthStack.push(el.depth);++ const compressedDepth = depthStack.length - 1; // root level has zero indent+ const indent = '\t'.repeat(compressedDepth);+ outputLines.push(indent + el.text);+ }++ return outputLines.join('\n');+})();diff --git a/camel/toolkits/non_visual_browser_toolkit/snapshot.py b/camel/toolkits/non_visual_browser_toolkit/snapshot.pynew file mode 100644index 000000000..0c3e8a1e4--- /dev/null+++ b/camel/toolkits/non_visual_browser_toolkit/snapshot.py@@ -0,0 +1,164 @@+# ========= 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. =========+from pathlib import Path+from typing import TYPE_CHECKING, Dict, List, Optional++if TYPE_CHECKING:+ from playwright.async_api import Page++# Logging support+from camel.logger import get_logger++logger = get_logger(__name__)+++class PageSnapshot:+ """Utility for capturing YAML-like page snapshots and diff-only+ variants."""++ MAX_TIMEOUT_MS = 5000 # wait_for_load_state timeout++ def __init__(self, page: "Page"):+ self.page = page+ self.snapshot_data: Optional[str] = None # last full snapshot+ self._last_url: Optional[str] = None+ self.last_info: Dict[str, List[int] | bool] = {+ "is_diff": False,+ "priorities": [1, 2, 3],+ }++ # ---------------------------------------------------------------------+ # Public API+ # ---------------------------------------------------------------------+ async def capture(+ self, *, force_refresh: bool = False, diff_only: bool = False+ ) -> str:+ """Return current snapshot or just the diff to previous one."""+ try:+ current_url = self.page.url++ # Serve cached copy (unless diff requested)+ if (+ not force_refresh+ and current_url == self._last_url+ and self.snapshot_data+ and not diff_only+ ):+ return self.snapshot_data++ # ensure DOM stability+ await self.page.wait_for_load_state(+ 'domcontentloaded', timeout=self.MAX_TIMEOUT_MS+ )++ logger.debug("Capturing page snapshot …")+ snapshot_text = await self._get_snapshot_direct()+ formatted = self._format_snapshot(snapshot_text or "<empty>")++ output = formatted+ if diff_only and self.snapshot_data:+ output = self._compute_diff(self.snapshot_data, formatted)++ # update cache with *full* snapshot (not diff)+ self._last_url = current_url+ self.snapshot_data = formatted++ # analyse priorities present (only for non-diff)+ priorities_included = self._detect_priorities(+ formatted if not diff_only else self.snapshot_data or formatted+ )+ self.last_info = {+ "is_diff": diff_only and self.snapshot_data is not None,+ "priorities": priorities_included,+ }++ logger.debug(+ "Snapshot captured. Diff_only=%s, priorities=%s",+ diff_only,+ self.last_info["priorities"],+ )+ return output+ except Exception as exc:+ logger.error("Snapshot capture failed: %s", exc)+ return f"Error: Could not capture page snapshot {exc}"++ # ------------------------------------------------------------------+ # Internal helpers+ # ------------------------------------------------------------------+ _snapshot_js_cache: Optional[str] = None # class-level cache++ async 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"+ )+ return await self.page.evaluate(PageSnapshot._snapshot_js_cache)+ except Exception as e:+ logger.warning("Failed to execute snapshot JavaScript: %s", e)+ return None++ @staticmethod+ def _format_snapshot(text: str) -> str:+ return "\n".join(["- Page Snapshot", "```yaml", text, "```"])++ @staticmethod+ def _compute_diff(old: str, new: str) -> str:+ if not old or not new:+ return "- Page Snapshot (error: missing data for diff)"++ import difflib++ diff = list(+ difflib.unified_diff(+ old.splitlines(False),+ new.splitlines(False),+ fromfile='prev',+ tofile='curr',+ lineterm='',+ )+ )+ if not diff:+ return "- Page Snapshot (no structural changes)"+ return "\n".join(["- Page Snapshot (diff)", "```diff", *diff, "```"])++ # ------------------------------------------------------------------+ def _detect_priorities(self, snapshot_yaml: str) -> List[int]:+ """Return sorted list of priorities present (1,2,3)."""+ priorities = set()+ for line in snapshot_yaml.splitlines():+ if '[ref=' not in line:+ continue+ lower_line = line.lower()+ if any(+ r in lower_line+ for r in (+ "input",+ "button",+ "select",+ "textarea",+ "checkbox",+ "radio",+ "link",+ )+ ):+ priorities.add(1)+ elif 'label' in lower_line:+ priorities.add(2)+ else:+ priorities.add(3)+ if not priorities:+ priorities.add(3)+ return sorted(priorities)diff --git a/examples/toolkits/browser_non_visual_toolkit_example.py b/examples/toolkits/browser_non_visual_toolkit_example.pynew file mode 100644index 000000000..38025169d--- /dev/null+++ b/examples/toolkits/browser_non_visual_toolkit_example.py@@ -0,0 +1,63 @@+# ========= 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. =========+import logging++from camel.agents import ChatAgent+from camel.models import ModelFactory+from camel.toolkits import BrowserNonVisualToolkit+from camel.types import ModelPlatformType, ModelType++logging.basicConfig(+ level=logging.DEBUG,+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',+ handlers=[+ logging.StreamHandler(), # Console output+ logging.FileHandler('browser_dom_debug.log'), # File output+ ],+)++logging.getLogger('camel.agents').setLevel(logging.INFO)+logging.getLogger('camel.models').setLevel(logging.INFO)++USER_DATA_DIR = r"User_Data"++model_backend = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAI,+ model_type=ModelType.GPT_4O,+ model_config_dict={"temperature": 0.0, "top_p": 1},+)+++web_toolkit = BrowserNonVisualToolkit(+ headless=False,+ user_data_dir=USER_DATA_DIR,+)++agent = ChatAgent(+ system_message="""+ """,+ model=model_backend,+ tools=[*web_toolkit.get_tools()],+)+++TASK_PROMPT = """Find the cheapest keyboard in Amazon,+open the product page and add it to cart."""++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>")diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.pyindex 630fb0755..4c204f7da 100644--- a/examples/workforce/eigent.py+++ b/examples/workforce/eigent.py@@ -21,6 +21,7 @@ from camel.societies.workforce import Workforcefrom camel.tasks.task import Taskfrom camel.toolkits import (AudioAnalysisToolkit,+ BrowserNonVisualToolkit,CodeExecutionToolkit,DalleToolkit,FileWriteToolkit,@@ -29,7 +30,6 @@ from camel.toolkits import (ImageAnalysisToolkit,LinkedInToolkit,NotionToolkit,- PlaywrightMCPToolkit,PPTXToolkit,RedditToolkit,SearchToolkit,@@ -75,23 +75,23 @@ def developer_agent_factory(model: BaseModelBackend, task_id: str):)-async def search_agent_factory(+def search_agent_factory(model: BaseModelBackend,task_id: str,- playwright_toolkit: PlaywrightMCPToolkit,):r"""Factory for creating a search agent, based on user-provided codestructure."""search_toolkits = SearchToolkit()# browser_toolkits = AsyncBrowserToolkit()+ browser_toolkit = BrowserNonVisualToolkit(headless=False)terminal_toolkits = TerminalToolkit()human_toolkits = HumanToolkit()tools = [# FunctionTool(search_toolkits.search_wiki),- FunctionTool(search_toolkits.search_google),+ FunctionTool(search_toolkits.search_exa),# FunctionTool(search_toolkits.search_bing),# FunctionTool(search_toolkits.search_baidu),- *playwright_toolkit.get_tools(),+ *browser_toolkit.get_tools(),# *browser_toolkits.get_tools(),*terminal_toolkits.get_tools(),human_toolkits.ask_human_via_console,@@ -337,107 +337,94 @@ When assisting users, always:async def main():- playwright_toolkit = PlaywrightMCPToolkit(timeout=60) # Create instance- try:- await playwright_toolkit.connect() # Connect the instance-- # Create a single model backend for all agents- model_backend = ModelFactory.create(- model_platform=ModelPlatformType.OPENAI,- model_type=ModelType.GPT_4_1,- # model_config_dict={- # "max_tokens": 32768,- # },- )-- model_backend_reason = ModelFactory.create(- model_platform=ModelPlatformType.OPENAI,- model_type=ModelType.GPT_4_1,- # model_config_dict={- # "max_tokens": 32768,- # },- )-- task_id = 'workforce_task'-- # Create agents using factory functions- search_agent = await search_agent_factory(- model_backend, task_id, playwright_toolkit- )- developer_agent = developer_agent_factory(model_backend, task_id)- document_agent = document_agent_factory(model_backend, task_id)- multi_modal_agent = multi_modal_agent_factory(model_backend, task_id)- # social_medium_agent = social_medium_agent_factory(model_backend,- # task_id)-- # Configure kwargs for all agents to use the same model_backend- coordinator_agent_kwargs = {"model": model_backend_reason}- task_agent_kwargs = {"model": model_backend_reason}- new_worker_agent_kwargs = {"model": model_backend}-- workforce = Workforce(- 'A workforce',- graceful_shutdown_timeout=30.0, # 30 seconds for debugging- share_memory=False,- coordinator_agent_kwargs=coordinator_agent_kwargs,- task_agent_kwargs=task_agent_kwargs,- new_worker_agent_kwargs=new_worker_agent_kwargs,- )-- workforce.add_single_agent_worker(- "Search Agent: Can search the web, extract webpage content, "- "simulate browser actions, and provide relevant information to "- "solve the given task.",- worker=search_agent,- ).add_single_agent_worker(- "Developer Agent: A skilled coding assistant that can write and "- "execute code, run terminal commands, and verify solutions to "- "complete tasks.",- worker=developer_agent,- ).add_single_agent_worker(- "Document Agent: A document processing assistant for creating, "- "modifying, and managing various document formats, including "- "presentations.",- worker=document_agent,- ).add_single_agent_worker(- "Multi-Modal Agent: A multi-modal processing assistant for "- "analyzing, and generating media content like audio and images.",- worker=multi_modal_agent,- )-- # specify the task to be solved- human_task = Task(- content=(- """- I want to read papers about GUI Agent. Please help me find- ten papers, check the detailed content of the papers and help- me write a comparison report, then create a nice slides(pptx)- to introduce the latest research progress of GUI Agent. The- slides should be very comprehensive and- professional.- """- ),- id='0',- )-- # Use the async version directly to avoid hanging with async tools- await workforce.process_task_async(human_task)-- # Test WorkforceLogger features- print("\n--- Workforce Log Tree ---")- print(workforce.get_workforce_log_tree())-- print("\n--- Workforce KPIs ---")- kpis = workforce.get_workforce_kpis()- for key, value in kpis.items():- print(f"{key}: {value}")-- log_file_path = "eigent_logs.json"- print(f"\n--- Dumping Workforce Logs to {log_file_path} ---")- workforce.dump_workforce_logs(log_file_path)- print(f"Logs dumped. Please check the file: {log_file_path}")- finally:- await playwright_toolkit.disconnect() # Disconnect the same instance+ # Create a single model backend for all agents+ model_backend = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAI,+ model_type=ModelType.GPT_4_1,+ # model_config_dict={+ # "max_tokens": 32768,+ # },+ )++ model_backend_reason = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAI,+ model_type=ModelType.GPT_4_1,+ # model_config_dict={+ # "max_tokens": 32768,+ # },+ )++ task_id = 'workforce_task'++ # Create agents using factory functions+ search_agent = search_agent_factory(model_backend, task_id)+ developer_agent = developer_agent_factory(model_backend, task_id)+ document_agent = document_agent_factory(model_backend, task_id)+ multi_modal_agent = multi_modal_agent_factory(model_backend, task_id)+ # social_medium_agent = social_medium_agent_factory(model_backend,+ # task_id)++ # Configure kwargs for all agents to use the same model_backend+ coordinator_agent_kwargs = {"model": model_backend_reason}+ task_agent_kwargs = {"model": model_backend_reason}+ new_worker_agent_kwargs = {"model": model_backend}++ workforce = Workforce(+ 'A workforce',+ graceful_shutdown_timeout=30.0, # 30 seconds for debugging+ share_memory=False,+ coordinator_agent_kwargs=coordinator_agent_kwargs,+ task_agent_kwargs=task_agent_kwargs,+ new_worker_agent_kwargs=new_worker_agent_kwargs,+ )++ workforce.add_single_agent_worker(+ "Search Agent: Can search the web, extract webpage content, "+ "simulate browser actions, and provide relevant information to "+ "solve the given task.",+ worker=search_agent,+ ).add_single_agent_worker(+ "Developer Agent: A skilled coding assistant that can write and "+ "execute code, run terminal commands, and verify solutions to "+ "complete tasks.",+ worker=developer_agent,+ ).add_single_agent_worker(+ "Document Agent: A document processing assistant for creating, "+ "modifying, and managing various document formats, including "+ "presentations.",+ worker=document_agent,+ ).add_single_agent_worker(+ "Multi-Modal Agent: A multi-modal processing assistant for "+ "analyzing, and generating media content like audio and images.",+ worker=multi_modal_agent,+ )++ # specify the task to be solved+ human_task = Task(+ content=(+ """+Analyze the UK healthcare industry to support the planning of my next company. Provide a comprehensive market overview, including current trends, growth projections, and relevant regulations. Identify the top 5-10 major competitors in the space, including their names, website URLs, estimated market size or share, core services or products, key strengths, and notable weaknesses. Also highlight any significant opportunities, gaps, or underserved segments within the market. Present all findings in a well-structured, professional PDF report.+ """ # noqa: E501+ ),+ id='0',+ )++ # Use the async version directly to avoid hanging with async tools+ await workforce.process_task_async(human_task)++ # Test WorkforceLogger features+ print("\n--- Workforce Log Tree ---")+ print(workforce.get_workforce_log_tree())++ print("\n--- Workforce KPIs ---")+ kpis = workforce.get_workforce_kpis()+ for key, value in kpis.items():+ print(f"{key}: {value}")++ log_file_path = "eigent_logs.json"+ print(f"\n--- Dumping Workforce Logs to {log_file_path} ---")+ workforce.dump_workforce_logs(log_file_path)+ print(f"Logs dumped. Please check the file: {log_file_path}")if __name__ == "__main__":