Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

enhance: hybrid browser (#2810)

Puzhen Zhang9a0c1c9e3940
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index 15d6fa876..df812e51d 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.71a5
+ placeholder: E.g., 0.2.71a6
validations:
required: true
diff --git a/camel/__init__.py b/camel/__init__.py
index 5cf2a765e..deb3957c4 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.71a5'
+__version__ = '0.2.71a6'
__all__ = [
'__version__',
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 171b1f7c3..1d2696f14 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -3815,7 +3815,7 @@ class ChatAgent(BaseAgent):
self.memory.get_context_creator(), "token_limit", None
),
output_language=self._output_language,
- tools=[tool.func for tool in self._internal_tools.values()],
+ tools=self._clone_tools(),
external_tools=[
schema for schema in self._external_tool_schemas.values()
],
@@ -3839,6 +3839,56 @@ class ChatAgent(BaseAgent):
return new_agent
+ def _clone_tools(self) -> List[Union[FunctionTool, Callable]]:
+ r"""Clone tools for new agent instance,
+ handling stateful toolkits properly."""
+ cloned_tools = []
+ hybrid_browser_toolkits = {} # Cache for created toolkits
+
+ for tool in self._internal_tools.values():
+ # Check if this is a HybridBrowserToolkit method
+ if (
+ hasattr(tool.func, '__self__')
+ and tool.func.__self__.__class__.__name__
+ == 'HybridBrowserToolkit'
+ ):
+ toolkit_instance = tool.func.__self__
+ toolkit_id = id(toolkit_instance)
+
+ # Check if we already created a clone for this toolkit
+ if toolkit_id not in hybrid_browser_toolkits:
+ try:
+ import uuid
+
+ new_session_id = str(uuid.uuid4())[:8]
+ new_toolkit = toolkit_instance.clone_for_new_session(
+ new_session_id
+ )
+ hybrid_browser_toolkits[toolkit_id] = new_toolkit
+ except Exception as e:
+ logger.warning(
+ f"Failed to clone HybridBrowserToolkit: {e}"
+ )
+ # Fallback to original function
+ cloned_tools.append(tool.func)
+ continue
+
+ # Get the corresponding method from the cloned toolkit
+ new_toolkit = hybrid_browser_toolkits[toolkit_id]
+ method_name = tool.func.__name__
+ if hasattr(new_toolkit, method_name):
+ new_method = getattr(new_toolkit, method_name)
+ cloned_tools.append(new_method)
+ else:
+ # Fallback to original function
+ cloned_tools.append(tool.func)
+ else:
+ # Regular tool or other stateless toolkit
+ # just use the original function
+ cloned_tools.append(tool.func)
+
+ return cloned_tools
+
def __repr__(self) -> str:
r"""Returns a string representation of the :obj:`ChatAgent`.
diff --git a/camel/tasks/task.py b/camel/tasks/task.py
index 17621cdb5..da57fe88a 100644
--- a/camel/tasks/task.py
+++ b/camel/tasks/task.py
@@ -184,7 +184,7 @@ def parse_response(
tasks = []
if task_id is None:
task_id = "0"
- for i, content in enumerate(tasks_content):
+ for i, content in enumerate(tasks_content, 1):
stripped_content = content.strip()
# validate subtask content before creating the task
if validate_task_content(stripped_content, f"{task_id}.{i}"):
diff --git a/camel/toolkits/hybrid_browser_toolkit/actions.py b/camel/toolkits/hybrid_browser_toolkit/actions.py
index 1054ccfa7..7b87e898f 100644
--- a/camel/toolkits/hybrid_browser_toolkit/actions.py
+++ b/camel/toolkits/hybrid_browser_toolkit/actions.py
@@ -12,7 +12,7 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import asyncio
-from typing import TYPE_CHECKING, Any, Dict
+from typing import TYPE_CHECKING, Any, Dict, Optional
if TYPE_CHECKING:
from playwright.async_api import Page
@@ -26,24 +26,33 @@ class ActionExecutor:
SHORT_TIMEOUT = 2000 # 2 seconds
MAX_SCROLL_AMOUNT = 5000 # Maximum scroll distance in pixels
- def __init__(self, page: "Page"):
+ def __init__(self, page: "Page", session: Optional[Any] = None):
self.page = page
+ self.session = session # HybridBrowserSession instance
# ------------------------------------------------------------------
# Public helpers
# ------------------------------------------------------------------
- async def execute(self, action: Dict[str, Any]) -> str:
- r"""Execute an action and return the result description."""
+ async def execute(self, action: Dict[str, Any]) -> Dict[str, Any]:
+ r"""Execute an action and return detailed result information."""
if not action:
- return "No action to execute"
+ return {
+ "success": False,
+ "message": "No action to execute",
+ "details": {},
+ }
action_type = action.get("type")
if not action_type:
- return "Error: action has no type"
+ return {
+ "success": False,
+ "message": "Error: action has no type",
+ "details": {},
+ }
try:
# small helper to ensure basic stability
- await self._wait_dom_stable()
+ # await self._wait_dom_stable()
handler = {
"click": self._click,
@@ -56,24 +65,41 @@ class ActionExecutor:
}.get(action_type)
if handler is None:
- return f"Error: Unknown action type '{action_type}'"
+ return {
+ "success": False,
+ "message": f"Error: Unknown action type '{action_type}'",
+ "details": {"action_type": action_type},
+ }
- return await handler(action)
+ result = await handler(action)
+ return {
+ "success": True,
+ "message": result["message"],
+ "details": result.get("details", {}),
+ }
except Exception as exc:
- return f"Error executing {action_type}: {exc}"
+ return {
+ "success": False,
+ "message": f"Error executing {action_type}: {exc}",
+ "details": {"action_type": action_type, "error": str(exc)},
+ }
# ------------------------------------------------------------------
# Internal handlers
# ------------------------------------------------------------------
- async def _click(self, action: Dict[str, Any]) -> str:
- r"""Handle click actions with multiple fallback strategies."""
+ async def _click(self, action: Dict[str, Any]) -> Dict[str, Any]:
+ r"""Handle click actions with new tab support for any clickable
+ element."""
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"
+ return {
+ "message": "Error: click requires ref/text/selector",
+ "details": {"error": "missing_selector"},
+ }
- # Build strategies in priority order: ref > selector > text
+ # Build strategies in priority order
strategies = []
if ref:
strategies.append(f"[aria-ref='{ref}']")
@@ -82,90 +108,230 @@ class ActionExecutor:
if text:
strategies.append(f'text="{text}"')
- # Strategy 1: Try Playwright force click for each selector
- for sel in strategies:
- try:
- if await self.page.locator(sel).count() > 0:
- await self.page.click(
- sel, timeout=self.DEFAULT_TIMEOUT, force=True
- )
- return f"Clicked element via force: {sel}"
- except Exception:
- continue
+ details: Dict[str, Any] = {
+ "ref": ref,
+ "selector": selector,
+ "text": text,
+ "strategies_tried": [],
+ "successful_strategy": None,
+ "click_method": None,
+ "new_tab_created": False,
+ }
- # Strategy 2: Try JavaScript click as fallback
+ # Find the first valid selector
+ found_selector = None
for sel in strategies:
- try:
- await self.page.locator(sel).first.evaluate("el => el.click()")
- await asyncio.sleep(0.1) # Brief wait for effects
- return f"Clicked element via JS: {sel}"
- except Exception:
- continue
+ if await self.page.locator(sel).count() > 0:
+ found_selector = sel
+ break
+
+ if not found_selector:
+ details['error'] = "Element not found with any strategy"
+ return {
+ "message": "Error: Click failed, element not found",
+ "details": details,
+ }
+
+ element = self.page.locator(found_selector).first
+ details['successful_strategy'] = found_selector
+
+ # Attempt ctrl+click first (always)
+ try:
+ if self.session:
+ async with self.page.context.expect_page(
+ timeout=self.SHORT_TIMEOUT
+ ) as new_page_info:
+ await element.click(modifiers=["ControlOrMeta"])
+ new_page = await new_page_info.value
+ await new_page.wait_for_load_state('domcontentloaded')
+ new_tab_index = await self.session.register_page(new_page)
+ if new_tab_index is not None:
+ await self.session.switch_to_tab(new_tab_index)
+ self.page = new_page
+ details.update(
+ {
+ "click_method": "ctrl_click_new_tab",
+ "new_tab_created": True,
+ "new_tab_index": new_tab_index,
+ }
+ )
+ return {
+ "message": f"Clicked element (ctrl click), opened in new "
+ f"tab {new_tab_index}",
+ "details": details,
+ }
+ else:
+ await element.click(modifiers=["ControlOrMeta"])
+ details["click_method"] = "ctrl_click_no_session"
+ return {
+ "message": f"Clicked element (ctrl click, no"
+ f" session): {found_selector}",
+ "details": details,
+ }
+ except asyncio.TimeoutError:
+ # No new tab was opened, click may have still worked
+ details["click_method"] = "ctrl_click_same_tab"
+ return {
+ "message": f"Clicked element (ctrl click, "
+ f"same tab): {found_selector}",
+ "details": details,
+ }
+ except Exception as e:
+ details['strategies_tried'].append(
+ {
+ 'selector': found_selector,
+ 'method': 'ctrl_click',
+ 'error': str(e),
+ }
+ )
+ # Fall through to fallback
- return "Error: All click strategies failed"
+ # Fallback to normal force click if ctrl+click fails
+ try:
+ await element.click(force=True, timeout=self.DEFAULT_TIMEOUT)
+ details["click_method"] = "playwright_force_click"
+ return {
+ "message": f"Fallback clicked element: {found_selector}",
+ "details": details,
+ }
+ except Exception as e:
+ details["click_method"] = "playwright_force_click_failed"
+ details["error"] = str(e)
+ return {
+ "message": f"Error: All click strategies "
+ f"failed for {found_selector}",
+ "details": details,
+ }
- async def _type(self, action: Dict[str, Any]) -> str:
+ async def _type(self, action: Dict[str, Any]) -> Dict[str, Any]:
r"""Handle typing text into input fields."""
ref = action.get("ref")
selector = action.get("selector")
text = action.get("text", "")
if not (ref or selector):
- return "Error: type requires ref/selector"
+ return {
+ "message": "Error: type requires ref/selector",
+ "details": {"error": "missing_selector"},
+ }
+
target = selector or f"[aria-ref='{ref}']"
+ details = {
+ "ref": ref,
+ "selector": selector,
+ "target": target,
+ "text": text,
+ "text_length": len(text),
+ }
+
try:
await self.page.fill(target, text, timeout=self.SHORT_TIMEOUT)
- return f"Typed '{text}' into {target}"
+ return {
+ "message": f"Typed '{text}' into {target}",
+ "details": details,
+ }
except Exception as exc:
- return f"Type failed: {exc}"
+ details["error"] = str(exc)
+ return {"message": f"Type failed: {exc}", "details": details}
- async def _select(self, action: Dict[str, Any]) -> str:
+ async def _select(self, action: Dict[str, Any]) -> Dict[str, Any]:
r"""Handle selecting options from dropdowns."""
ref = action.get("ref")
selector = action.get("selector")
value = action.get("value", "")
if not (ref or selector):
- return "Error: select requires ref/selector"
+ return {
+ "message": "Error: select requires ref/selector",
+ "details": {"error": "missing_selector"},
+ }
+
target = selector or f"[aria-ref='{ref}']"
+ details = {
+ "ref": ref,
+ "selector": selector,
+ "target": target,
+ "value": value,
+ }
+
try:
await self.page.select_option(
target, value, timeout=self.DEFAULT_TIMEOUT
)
- return f"Selected '{value}' in {target}"
+ return {
+ "message": f"Selected '{value}' in {target}",
+ "details": details,
+ }
except Exception as exc:
- return f"Select failed: {exc}"
+ details["error"] = str(exc)
+ return {"message": f"Select failed: {exc}", "details": details}
- async def _wait(self, action: Dict[str, Any]) -> str:
+ async def _wait(self, action: Dict[str, Any]) -> Dict[str, Any]:
r"""Handle wait actions."""
+ details: Dict[str, Any] = {
+ "wait_type": None,
+ "timeout": None,
+ "selector": None,
+ }
+
if "timeout" in action:
ms = int(action["timeout"])
+ details["wait_type"] = "timeout"
+ details["timeout"] = ms
await asyncio.sleep(ms / 1000)
- return f"Waited {ms}ms"
+ return {"message": f"Waited {ms}ms", "details": details}
if "selector" in action:
sel = action["selector"]
+ details["wait_type"] = "selector"
+ details["selector"] = sel
await self.page.wait_for_selector(
sel, timeout=self.DEFAULT_TIMEOUT
)
- return f"Waited for {sel}"
- return "Error: wait requires timeout/selector"
+ return {"message": f"Waited for {sel}", "details": details}
+ return {
+ "message": "Error: wait requires timeout/selector",
+ "details": details,
+ }
- async def _extract(self, action: Dict[str, Any]) -> str:
+ async def _extract(self, action: Dict[str, Any]) -> Dict[str, Any]:
r"""Handle text extraction from elements."""
ref = action.get("ref")
if not ref:
- return "Error: extract requires ref"
+ return {
+ "message": "Error: extract requires ref",
+ "details": {"error": "missing_ref"},
+ }
+
target = f"[aria-ref='{ref}']"
+ details = {"ref": ref, "target": target}
+
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:
+ details["extracted_text"] = txt
+ details["text_length"] = len(txt) if txt else 0
+
+ return {
+ "message": f"Extracted: {txt[:100] if txt else 'None'}",
+ "details": details,
+ }
+
+ async def _scroll(self, action: Dict[str, Any]) -> Dict[str, Any]:
r"""Handle page scrolling with safe parameter validation."""
direction = action.get("direction", "down")
amount = action.get("amount", 300)
+ details = {
+ "direction": direction,
+ "requested_amount": amount,
+ "actual_amount": None,
+ "scroll_offset": None,
+ }
+
# Validate inputs to prevent injection
if direction not in ("up", "down"):
- return "Error: direction must be 'up' or 'down'"
+ return {
+ "message": "Error: direction must be 'up' or 'down'",
+ "details": details,
+ }
try:
# Safely convert amount to integer and clamp to reasonable range
@@ -174,28 +340,37 @@ class ActionExecutor:
-self.MAX_SCROLL_AMOUNT,
min(self.MAX_SCROLL_AMOUNT, amount_int),
) # Clamp to MAX_SCROLL_AMOUNT range
+ details["actual_amount"] = amount_int
except (ValueError, TypeError):
- return "Error: amount must be a valid number"
+ return {
+ "message": "Error: amount must be a valid number",
+ "details": details,
+ }
# Use safe evaluation with bound parameters
scroll_offset = amount_int if direction == "down" else -amount_int
+ details["scroll_offset"] = scroll_offset
+
await self.page.evaluate(
"offset => window.scrollBy(0, offset)", scroll_offset
)
await asyncio.sleep(0.5)
- return f"Scrolled {direction} by {abs(amount_int)}px"
+ return {
+ "message": f"Scrolled {direction} by {abs(amount_int)}px",
+ "details": details,
+ }
- async def _enter(self, action: Dict[str, Any]) -> str:
- r"""Handle Enter key press actions."""
- 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)
+ async def _enter(self, action: Dict[str, Any]) -> Dict[str, Any]:
+ r"""Handle Enter key press on the currently focused element."""
+ details = {"action_type": "enter", "target": "focused_element"}
+
+ # Press Enter on whatever element currently has focus
await self.page.keyboard.press("Enter")
await asyncio.sleep(0.3)
- return "Pressed Enter"
+ return {
+ "message": "Pressed Enter on focused element",
+ "details": details,
+ }
# utilities
async def _wait_dom_stable(self) -> None:
diff --git a/camel/toolkits/hybrid_browser_toolkit/agent.py b/camel/toolkits/hybrid_browser_toolkit/agent.py
index c7b5bb68a..cd402d2d8 100644
--- a/camel/toolkits/hybrid_browser_toolkit/agent.py
+++ b/camel/toolkits/hybrid_browser_toolkit/agent.py
@@ -13,14 +13,14 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import json
import re
-from typing import TYPE_CHECKING, Any, Dict, List, Optional
+from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from camel.logger import get_logger
from camel.models import BaseModelBackend, ModelFactory
from camel.types import ModelPlatformType, ModelType
from .actions import ActionExecutor
-from .browser_session import NVBrowserSession
+from .browser_session import HybridBrowserSession
if TYPE_CHECKING:
from camel.agents import ChatAgent
@@ -79,10 +79,11 @@ what was accomplished
*,
user_data_dir: Optional[str] = None,
headless: bool = False,
+ stealth: bool = False,
model_backend: Optional[BaseModelBackend] = None,
):
- self._session = NVBrowserSession(
- headless=headless, user_data_dir=user_data_dir
+ self._session = HybridBrowserSession(
+ headless=headless, user_data_dir=user_data_dir, stealth=stealth
)
from camel.agents import ChatAgent
@@ -101,7 +102,7 @@ what was accomplished
async def navigate(self, url: str) -> str:
r"""Navigate to a URL and return the snapshot."""
try:
- # NVBrowserSession handles waits internally
+ # HybridBrowserSession handles waits internally
logger.debug("Navigated to URL: %s", url)
await self._session.visit(url)
return await self._session.get_snapshot(force_refresh=True)
@@ -240,11 +241,25 @@ what was accomplished
result = await self._run_action(action)
logger.debug("Executed action: %s | Result: %s", action, result)
+ success = False
+ result_for_history = ""
+
+ if isinstance(result, str):
+ success = "Error" not in result
+ result_for_history = result
+ elif isinstance(result, dict):
+ success = result.get('success', False)
+ result_for_history = result.get('message', str(result))
+ else:
+ # Fallback case
+ success = False
+ result_for_history = str(result)
+
self.action_history.append(
{
"action": action,
- "result": result,
- "success": "Error" not in result,
+ "result": result_for_history,
+ "success": success,
}
)
@@ -277,7 +292,9 @@ what was accomplished
logger.info("Process completed with %d steps", steps)
- async def _run_action(self, action: Dict[str, Any]) -> str:
+ async def _run_action(
+ self, action: Dict[str, Any]
+ ) -> Union[str, Dict[str, Any]]:
r"""Execute a single action and return the result."""
if action.get("type") == "navigate":
return await self.navigate(action.get("url", ""))
diff --git a/camel/toolkits/hybrid_browser_toolkit/browser_session.py b/camel/toolkits/hybrid_browser_toolkit/browser_session.py
index cafcfaf3b..d918354f8 100644
--- a/camel/toolkits/hybrid_browser_toolkit/browser_session.py
+++ b/camel/toolkits/hybrid_browser_toolkit/browser_session.py
@@ -14,13 +14,13 @@
from __future__ import annotations
import asyncio
-from pathlib import Path
-from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional
+from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Tuple
from camel.logger import get_logger
from .actions import ActionExecutor
from .snapshot import PageSnapshot
+from .stealth_config import StealthConfig
if TYPE_CHECKING:
from playwright.async_api import (
@@ -30,42 +30,102 @@ if TYPE_CHECKING:
Playwright,
)
-
logger = get_logger(__name__)
-class NVBrowserSession:
- """Lightweight wrapper around Playwright for non-visual (headless)
- browsing.
+class HybridBrowserSession:
+ """Lightweight wrapper around Playwright for
+ browsing with multi-tab support.
- It provides a single *Page* instance plus helper utilities (snapshot &
+ It provides multiple *Page* instances plus helper utilities (snapshot &
executor). Multiple toolkits or agents can reuse this class without
duplicating Playwright setup code.
- This class is a singleton per event-loop.
+ 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
- _sessions: ClassVar[
- Dict[asyncio.AbstractEventLoop, "NVBrowserSession"]
- ] = {}
+ # Class-level registry for singleton instances
+ # Format: {(loop_id, session_id): HybridBrowserSession}
+ _instances: ClassVar[Dict[Tuple[Any, str], "HybridBrowserSession"]] = {}
+ _instances_lock: ClassVar[asyncio.Lock] = asyncio.Lock()
_initialized: bool
+ _creation_params: Dict[str, Any]
def __new__(
- cls, *, headless: bool = True, user_data_dir: Optional[str] = None
- ) -> "NVBrowserSession":
- # Defer event loop lookup until we actually need it
- # This allows creation outside of async context
+ cls,
+ *,
+ headless: bool = True,
+ user_data_dir: Optional[str] = None,
+ stealth: bool = False,
+ session_id: Optional[str] = None,
+ ) -> "HybridBrowserSession":
+ # Create a unique key for this event loop and session combination
+ # We defer the event loop lookup to avoid issues with creation
+ # outside async context
instance = super().__new__(cls)
instance._initialized = False
+ instance._session_id = session_id or "default"
+ instance._creation_params = {
+ "headless": headless,
+ "user_data_dir": user_data_dir,
+ "stealth": stealth,
+ "session_id": session_id,
+ }
return instance
+ @classmethod
+ async def _get_or_create_instance(
+ cls,
+ instance: "HybridBrowserSession",
+ ) -> "HybridBrowserSession":
+ """Get or create singleton instance for the current event loop and
+ session."""
+ try:
+ loop = asyncio.get_running_loop()
+ loop_id = str(id(loop))
+ except RuntimeError:
+ # No event loop running, use a unique identifier for sync context
+ import threading
+
+ loop_id = f"sync_{threading.current_thread().ident}"
+
+ # Ensure session_id is never None for the key
+ session_id = (
+ instance._session_id
+ if instance._session_id is not None
+ else "default"
+ )
+ session_key = (loop_id, session_id)
+
+ # Use class-level lock to protect the instances registry
+ async with cls._instances_lock:
+ if session_key in cls._instances:
+ existing_instance = cls._instances[session_key]
+ logger.debug(
+ f"Reusing existing browser session for session_id: "
+ f"{session_id}"
+ )
+ return existing_instance
+
+ # Register this new instance
+ cls._instances[session_key] = instance
+ logger.debug(
+ f"Created new browser session for session_id: {session_id}"
+ )
+ return instance
+
def __init__(
- self, *, headless: bool = True, user_data_dir: Optional[str] = None
+ self,
+ *,
+ headless: bool = True,
+ user_data_dir: Optional[str] = None,
+ stealth: bool = False,
+ session_id: Optional[str] = None,
):
if self._initialized:
return
@@ -73,66 +133,324 @@ class NVBrowserSession:
self._headless = headless
self._user_data_dir = user_data_dir
+ self._stealth = stealth
+ self._session_id = session_id or "default"
+
+ # Initialize _creation_params to fix linter error
+ self._creation_params = {
+ "headless": headless,
+ "user_data_dir": user_data_dir,
+ "stealth": stealth,
+ "session_id": session_id,
+ }
self._playwright: Optional[Playwright] = None
self._browser: Optional[Browser] = None
self._context: Optional[BrowserContext] = None
self._page: Optional[Page] = None
+ # Multi-tab support
+ self._pages: List[Page] = [] # All tabs
+ self._current_tab_index: int = 0 # Current active tab index
+
self.snapshot: Optional[PageSnapshot] = None
self.executor: Optional[ActionExecutor] = None
# Protect browser initialisation against concurrent calls
self._ensure_lock: "asyncio.Lock" = asyncio.Lock()
+ # Load stealth script and config on initialization
+ self._stealth_script: Optional[str] = None
+ self._stealth_config: Optional[Dict[str, Any]] = None
+ if self._stealth:
+ self._stealth_script = self._load_stealth_script()
+ self._stealth_config = StealthConfig.get_all_config()
+
+ def _load_stealth_script(self) -> str:
+ r"""Load the stealth JavaScript script from file."""
+ import os
+
+ script_path = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)), "stealth_script.js"
+ )
+
+ try:
+ with open(
+ script_path, "r", encoding='utf-8', errors='replace'
+ ) as f:
+ script_content = f.read()
+
+ if not script_content.strip():
+ raise ValueError(f"Stealth script is empty: {script_path}")
+
+ logger.debug(
+ f"Loaded stealth script ({len(script_content)} chars)"
+ )
+ return script_content
+ except FileNotFoundError:
+ logger.error(f"Stealth script not found: {script_path}")
+ raise FileNotFoundError(f"Stealth script not found: {script_path}")
+ except Exception as e:
+ logger.error(f"Error loading stealth script: {e}")
+ raise RuntimeError(f"Failed to load stealth script: {e}") from e
+
# ------------------------------------------------------------------
- # Browser lifecycle helpers
+ # Multi-tab management methods
# ------------------------------------------------------------------
- async def ensure_browser(self) -> None:
- r"""Ensure browser is ready, implementing singleton pattern per event
- loop.
+ async def create_new_tab(self, url: Optional[str] = None) -> int:
+ r"""Create a new tab and optionally navigate to a URL.
+
+ Args:
+ url: Optional URL to navigate to in the new tab
+
+ Returns:
+ int: Index of the newly created tab
"""
- # Check if we need to reuse or create a session for this event loop
+ await self.ensure_browser()
+
+ if self._context is None:
+ raise RuntimeError("Browser context is not available")
+
+ # Create new page
+ new_page = await self._context.new_page()
+
+ # Apply stealth modifications if enabled
+ if self._stealth and self._stealth_script:
+ try:
+ await new_page.add_init_script(self._stealth_script)
+ logger.debug("Applied stealth script to new tab")
+ except Exception as e:
+ logger.warning(
+ f"Failed to apply stealth script to new tab: {e}"
+ )
+
+ # Add to our pages list
+ self._pages.append(new_page)
+ new_tab_index = len(self._pages) - 1
+
+ # Navigate if URL provided
+ if url:
+ try:
+ await new_page.goto(
+ url, timeout=self.DEFAULT_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}")
+
+ logger.info(
+ f"Created new tab {new_tab_index}, total tabs: {len(self._pages)}"
+ )
+ return new_tab_index
+
+ async def register_page(self, new_page: "Page") -> int:
+ r"""Register a page that was created externally (e.g., by a click).
+
+ Args:
+ new_page (Page): The new page object to register.
+
+ Returns:
+ int: The index of the (newly) registered tab.
+ """
+ if new_page in self._pages:
+ try:
+ # Page is already known, just return its index
+ return self._pages.index(new_page)
+ except ValueError:
+ # Should not happen if `in` check passed, but handle anyway
+ pass
+
+ # Add new page to our list
+ self._pages.append(new_page)
+ new_tab_index = len(self._pages) - 1
+ logger.info(
+ f"Registered new tab {new_tab_index} (opened by user action). "
+ f"Total tabs: {len(self._pages)}"
+ )
+ return new_tab_index
+
+ async def switch_to_tab(self, tab_index: int) -> bool:
+ r"""Switch to a specific tab by index.
+
+ Args:
+ tab_index: Index of the tab to switch to
+
+ Returns:
+ bool: True if successful, False if tab index is invalid
+ """
+ # Use a more robust bounds check to prevent race conditions
try:
- loop = asyncio.get_running_loop()
- except RuntimeError as e:
- raise RuntimeError(
- "ensure_browser() must be called from within an async context"
- ) from e
-
- # Check if there's already a session for this loop
- if loop in self._sessions and self._sessions[loop] is not self:
- # Copy the existing session's browser resources
- existing = self._sessions[loop]
- # Wait for existing session to be fully initialized
- async with existing._ensure_lock:
+ if not self._pages:
+ logger.warning("No tabs available")
+ return False
+
+ # Capture current state to avoid race conditions
+ current_pages = self._pages.copy()
+ pages_count = len(current_pages)
+
+ if tab_index < 0 or tab_index >= pages_count:
+ logger.warning(
+ f"Invalid tab index {tab_index}, available "
+ f"tabs: {pages_count}"
+ )
+ return False
+
+ # Check if the page is still valid
+ page = current_pages[tab_index]
+ if page.is_closed():
+ logger.warning(
+ f"Tab {tab_index} is closed, removing from list"
+ )
+ # Remove closed page from original list
if (
- existing._initialized
- and existing._page is not None
- and existing._playwright is not None
+ tab_index < len(self._pages)
+ and self._pages[tab_index] is page
):
- try:
- # Verify the page is still responsive
- await existing._page.title()
- self._playwright = existing._playwright
- self._browser = existing._browser
- self._context = existing._context
- self._page = existing._page
- self.snapshot = existing.snapshot
- self.executor = existing.executor
- self._initialized = True
- return
- except Exception:
- # Existing session is broken, continue with new
- # initialization
- pass
-
- # Register this instance for the current loop
- self._sessions[loop] = self
+ self._pages.pop(tab_index)
+ # Adjust current tab index if necessary
+ if self._current_tab_index >= len(self._pages):
+ self._current_tab_index = max(0, len(self._pages) - 1)
+ return False
+
+ self._current_tab_index = tab_index
+ self._page = page
+
+ # Bring the tab to the front in the browser window
+ await self._page.bring_to_front()
+
+ # Update executor and snapshot for new tab
+ self.executor = ActionExecutor(self._page, self)
+ self.snapshot = PageSnapshot(self._page)
+
+ logger.info(f"Switched to tab {tab_index}")
+ return True
+
+ except Exception as e:
+ logger.warning(f"Error switching to tab {tab_index}: {e}")
+ return False
+
+ async def close_tab(self, tab_index: int) -> bool:
+ r"""Close a specific tab.
+
+ Args:
+ tab_index: Index of the tab to close
+
+ Returns:
+ bool: True if successful, False if tab index is invalid
+ """
+ if not self._pages or tab_index < 0 or tab_index >= len(self._pages):
+ return False
+
+ try:
+ page = self._pages[tab_index]
+ if not page.is_closed():
+ await page.close()
+
+ # Remove from our list
+ self._pages.pop(tab_index)
+
+ # If we closed the current tab, switch to another one
+ if tab_index == self._current_tab_index:
+ if self._pages:
+ # Switch to the previous tab, or first tab if we closed
+ # the first one
+ new_index = max(
+ 0, min(tab_index - 1, len(self._pages) - 1)
+ )
+ await self.switch_to_tab(new_index)
+ else:
+ # No tabs left
+ self._current_tab_index = 0
+ self._page = None
+ self.executor = None
+ self.snapshot = None
+ elif tab_index < self._current_tab_index:
+ # Adjust current tab index since we removed a tab before it
+ self._current_tab_index -= 1
+
+ logger.info(
+ f"Closed tab {tab_index}, remaining tabs: {len(self._pages)}"
+ )
+ return True
+
+ except Exception as e:
+ logger.warning(f"Error closing tab {tab_index}: {e}")
+ return False
+
+ async def get_tab_info(self) -> List[Dict[str, Any]]:
+ r"""Get information about all open tabs.
+
+ Returns:
+ List of dictionaries containing tab information
+ """
+ tab_info = []
+ for i, page in enumerate(self._pages):
+ try:
+ if not page.is_closed():
+ title = await page.title()
+ url = page.url
+ is_current = i == self._current_tab_index
+ tab_info.append(
+ {
+ "index": i,
+ "title": title,
+ "url": url,
+ "is_current": is_current,
+ }
+ )
+ else:
+ # Mark closed tab for removal
+ tab_info.append(
+ {
+ "index": i,
+ "title": "[CLOSED]",
+ "url": "",
+ "is_current": False,
+ }
+ )
+ except Exception as e:
+ logger.warning(f"Error getting info for tab {i}: {e}")
+ tab_info.append(
+ {
+ "index": i,
+ "title": "[ERROR]",
+ "url": "",
+ "is_current": False,
+ }
+ )
+
+ return tab_info
+
+ async def get_current_tab_index(self) -> int:
+ r"""Get the index of the current active tab."""
+ return self._current_tab_index
+
+ # ------------------------------------------------------------------
+ # Browser lifecycle helpers
+ # ------------------------------------------------------------------
+ async def ensure_browser(self) -> None:
+ r"""Ensure browser is ready. Each session_id gets its own browser
+ instance."""
+ # First, get the singleton instance for this session
+ singleton_instance = await self._get_or_create_instance(self)
+
+ # If this isn't the singleton instance, delegate to the singleton
+ if singleton_instance is not self:
+ await singleton_instance.ensure_browser()
+ # Copy the singleton's browser state to this instance
+ self._playwright = singleton_instance._playwright
+ self._browser = singleton_instance._browser
+ self._context = singleton_instance._context
+ self._page = singleton_instance._page
+ self._pages = singleton_instance._pages
+ self._current_tab_index = singleton_instance._current_tab_index
+ self.snapshot = singleton_instance.snapshot
+ self.executor = singleton_instance.executor
+ return
# Serialise initialisation to avoid race conditions where multiple
# concurrent coroutine calls create multiple browser instances for
- # the same NVBrowserSession.
+ # the same HybridBrowserSession.
async with self._ensure_lock:
await self._ensure_browser_inner()
@@ -145,155 +463,247 @@ class NVBrowserSession:
return
self._playwright = await async_playwright().start()
+
+ # Prepare stealth options
+ launch_options: Dict[str, Any] = {"headless": self._headless}
+ context_options: Dict[str, Any] = {}
+ if self._stealth and self._stealth_config:
+ # Use preloaded stealth configuration
+ launch_options['args'] = self._stealth_config['launch_args']
+ context_options.update(self._stealth_config['context_options'])
+
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,
+ context = (
+ await self._playwright.chromium.launch_persistent_context(
+ user_data_dir=self._user_data_dir,
+ **launch_options,
+ **context_options,
+ )
)
- 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]
+ self._context = context
+ # Get the first (default) page
+ pages = context.pages
+ if pages:
+ self._page = pages[0]
+ self._pages = list(pages)
+ else:
+ self._page = await context.new_page()
+ self._pages = [self._page]
else:
+ self._browser = await self._playwright.chromium.launch(
+ **launch_options
+ )
+ self._context = await self._browser.new_context(**context_options)
self._page = await self._context.new_page()
+ self._pages = [self._page]
- # Debug information to help trace concurrency issues
- 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>",
+ # Apply stealth modifications if enabled
+ if self._stealth and self._stealth_script:
+ try:
+ await self._page.add_init_script(self._stealth_script)
+ logger.debug("Applied stealth script to main page")
+ except Exception as e:
+ 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)
- # helpers
+ # Initialize utilities
self.snapshot = PageSnapshot(self._page)
- self.executor = ActionExecutor(self._page)
+ self.executor = ActionExecutor(self._page, self)
+ self._current_tab_index = 0
+
+ logger.info("Browser session initialized successfully")
async def close(self) -> None:
- r"""Close all browser resources, ensuring cleanup even if some
- operations fail.
- """
- # Remove this session from the sessions dict and close resources
- try:
- loop = asyncio.get_running_loop()
- if loop in self._sessions and self._sessions[loop] is self:
- del self._sessions[loop]
- except RuntimeError:
- pass # No running loop, that's okay
+ r"""Close browser session and clean up resources."""
+ if self._page is None:
+ return
- # Clean up any stale loop references
- stale_loops = [loop for loop in self._sessions if loop.is_closed()]
- for loop in stale_loops:
- del self._sessions[loop]
+ try:
+ logger.debug("Closing browser session...")
+ await self._close_session()
- await self._close_session()
+ # Remove from singleton registry
+ try:
+ try:
+ loop = asyncio.get_running_loop()
+ loop_id = str(id(loop))
+ except RuntimeError:
+ # Use same logic as _get_or_create_instance
+ import threading
+
+ loop_id = f"sync_{threading.current_thread().ident}"
+
+ session_id = (
+ self._session_id
+ if self._session_id is not None
+ else "default"
+ )
+ session_key = (loop_id, session_id)
+
+ async with self._instances_lock:
+ if (
+ session_key in self._instances
+ and self._instances[session_key] is self
+ ):
+ del self._instances[session_key]
+ logger.debug(
+ f"Removed session {session_id} from registry"
+ )
+
+ except Exception as registry_error:
+ logger.warning(f"Error cleaning up registry: {registry_error}")
+
+ logger.debug("Browser session closed successfully")
+ except Exception as e:
+ logger.error(f"Error during browser session close: {e}")
+ finally:
+ self._page = None
+ self._pages = []
+ self._current_tab_index = 0
+ self.snapshot = None
+ self.executor = None
async def _close_session(self) -> None:
- r"""Internal session cleanup with comprehensive error handling."""
- errors: list[str] = []
+ r"""Internal session close logic with thorough cleanup."""
+ try:
+ # Close all pages first
+ pages_to_close = self._pages.copy()
+ for page in pages_to_close:
+ try:
+ if not page.is_closed():
+ await page.close()
+ logger.debug(
+ f"Closed page: "
+ f"{page.url if hasattr(page, 'url') else 'unknown'}" # noqa: E501
+ )
+ except Exception as e:
+ logger.warning(f"Error closing page: {e}")
- # 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}")
+ # Clear the pages list
+ self._pages.clear()
- # Close browser
- if self._browser is not None:
- try:
- await self._browser.close()
- except Exception as e:
- errors.append(f"Browser close error: {e}")
+ # Close context with explicit wait
+ if self._context:
+ try:
+ await self._context.close()
+ logger.debug("Browser context closed")
+ except Exception as e:
+ logger.warning(f"Error closing context: {e}")
+ finally:
+ self._context = None
- # Stop playwright
- if self._playwright is not None:
- try:
- await self._playwright.stop()
- except Exception as e:
- errors.append(f"Playwright stop error: {e}")
+ # Close browser with explicit wait
+ if self._browser:
+ try:
+ await self._browser.close()
+ logger.debug("Browser instance closed")
+ except Exception as e:
+ logger.warning(f"Error closing browser: {e}")
+ finally:
+ self._browser = None
- # Reset all references
- self._playwright = self._browser = self._context = self._page = None
- self.snapshot = self.executor = None
+ # Stop playwright with increased delay for cleanup
+ if self._playwright:
+ try:
+ await self._playwright.stop()
+ logger.debug("Playwright stopped")
- # Log errors if any occurred during cleanup
- if errors:
- logger.warning(
- "Errors during browser session cleanup: %s", "; ".join(errors)
- )
+ # Give more time for complete subprocess cleanup
+ import asyncio
+
+ await asyncio.sleep(0.5)
+
+ except Exception as e:
+ logger.warning(f"Error stopping playwright: {e}")
+ finally:
+ self._playwright = None
+
+ except Exception as e:
+ logger.error(f"Error during session cleanup: {e}")
+ finally:
+ # Ensure all attributes are cleared regardless of errors
+ self._page = None
+ self._pages = []
+ self._context = None
+ self._browser = None
+ self._playwright = None
@classmethod
async def close_all_sessions(cls) -> None:
- r"""Iterate over all stored sessions and close them."""
- for loop, session in cls._sessions.items():
- if loop.is_running():
- await session._close_session()
- else:
- try:
- if not loop.is_closed():
- loop.run_until_complete(session._close_session())
- except Exception as e:
- logger.warning(
- "Failed to close session for loop %s: %s",
- hex(id(loop)),
- e,
- )
- cls._sessions.clear()
+ r"""Close all browser sessions and clean up the singleton registry."""
+ logger.debug("Closing all browser sessions...")
+ async with cls._instances_lock:
+ # Close all active sessions
+ instances_to_close = list(cls._instances.values())
+ cls._instances.clear()
+ logger.debug(f"Closing {len(instances_to_close)} sessions.")
+
+ # Close sessions outside the lock to avoid deadlock
+ for instance in instances_to_close:
+ try:
+ await instance._close_session()
+ logger.debug(f"Closed session: {instance._session_id}")
+ except Exception as e:
+ logger.error(
+ f"Error closing session {instance._session_id}: {e}"
+ )
+
+ logger.debug("All browser sessions closed and registry cleared")
+
+ @classmethod
+ async def close_all(cls) -> None:
+ """Alias for close_all_sessions for backward compatibility."""
+ await cls.close_all_sessions()
# ------------------------------------------------------------------
- # Convenience wrappers around common actions
+ # Page interaction
# ------------------------------------------------------------------
async def visit(self, url: str) -> str:
- r"""Navigate to a URL with proper error handling."""
+ r"""Navigate current tab to URL."""
await self.ensure_browser()
- assert self._page is not None
+ page = await self.get_page()
+
+ await page.goto(url, timeout=self.DEFAULT_NAVIGATION_TIMEOUT)
+ await page.wait_for_load_state('domcontentloaded')
+ # Try to wait for network idle
try:
- await self._page.goto(
- url,
- wait_until="domcontentloaded",
- timeout=self.DEFAULT_NAVIGATION_TIMEOUT,
+ await page.wait_for_load_state(
+ 'networkidle', timeout=self.NETWORK_IDLE_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}"
+ except Exception:
+ logger.debug("Network idle timeout - continuing anyway")
+
+ return f"Navigated to {url}"
async def get_snapshot(
self, *, force_refresh: bool = False, diff_only: bool = False
) -> str:
- await self.ensure_browser()
- assert self.snapshot is not None
+ r"""Get snapshot for current tab."""
+ if not self.snapshot:
+ return "<empty>"
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
+ async def exec_action(self, action: Dict[str, Any]) -> Dict[str, Any]:
+ r"""Execute action on current tab."""
+ if not self.executor:
+ return {
+ "success": False,
+ "message": "No executor available",
+ "details": {},
+ }
return await self.executor.execute(action)
- # Low-level accessors -------------------------------------------------
async def get_page(self) -> "Page":
+ r"""Get current active page."""
await self.ensure_browser()
- assert self._page is not None
+ if self._page is None:
+ raise RuntimeError("No active page available")
return self._page
diff --git a/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py b/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py
index f1407c553..2f9c36735 100644
--- a/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py
+++ b/camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py
@@ -15,9 +15,11 @@
import base64
import datetime
import io
+import json
import os
import time
import urllib.parse
+from functools import wraps
from typing import Any, Callable, ClassVar, Dict, List, Optional, cast
from camel.logger import get_logger
@@ -26,9 +28,10 @@ from camel.toolkits.base import BaseToolkit
from camel.toolkits.function_tool import FunctionTool
from camel.utils import sanitize_filename
from camel.utils.commons import dependencies_required
+from camel.utils.tool_result import ToolResult
from .agent import PlaywrightLLMAgent
-from .browser_session import NVBrowserSession
+from .browser_session import HybridBrowserSession
logger = get_logger(__name__)
@@ -54,9 +57,11 @@ class HybridBrowserToolkit(BaseToolkit):
"open_browser",
"close_browser",
"visit_page",
+ "back",
+ "forward",
"click",
"type",
- "enter",
+ "switch_tab",
]
# All available tools
@@ -64,6 +69,8 @@ class HybridBrowserToolkit(BaseToolkit):
"open_browser",
"close_browser",
"visit_page",
+ "back",
+ "forward",
"get_page_snapshot",
"get_som_screenshot",
"get_page_links",
@@ -74,6 +81,9 @@ class HybridBrowserToolkit(BaseToolkit):
"enter",
"wait_user",
"solve_task",
+ "switch_tab",
+ "close_tab",
+ "get_tab_info",
]
def __init__(
@@ -81,9 +91,13 @@ class HybridBrowserToolkit(BaseToolkit):
*,
headless: bool = True,
user_data_dir: Optional[str] = None,
+ stealth: bool = False,
web_agent_model: Optional[BaseModelBackend] = None,
cache_dir: str = "tmp/",
enabled_tools: Optional[List[str]] = None,
+ browser_log_to_file: bool = False,
+ session_id: Optional[str] = None,
+ default_start_url: str = "https://google.com/",
) -> None:
r"""Initialize the HybridBrowserToolkit.
@@ -94,6 +108,12 @@ class HybridBrowserToolkit(BaseToolkit):
browser data like cookies and local storage. Useful for
maintaining sessions across runs. Defaults to `None` (a
temporary directory is used).
+ stealth (bool): Whether to run the browser in stealth mode to avoid
+ bot detection. When enabled, hides WebDriver characteristics,
+ spoofs navigator properties, and implements various
+ anti-detection
+ measures. Highly recommended for production use and when
+ accessing sites with bot detection. Defaults to `False`.
web_agent_model (Optional[BaseModelBackend]): The language model
backend to use for the high-level `solve_task` agent. This is
required only if you plan to use `solve_task`.
@@ -102,18 +122,57 @@ class HybridBrowserToolkit(BaseToolkit):
screenshots. Defaults to `"tmp/"`.
enabled_tools (Optional[List[str]]): List of tool names to enable.
If None, uses DEFAULT_TOOLS. Available tools: open_browser,
- close_browser, visit_page, get_page_snapshot,
+ close_browser, visit_page, back, forward, get_page_snapshot,
get_som_screenshot, get_page_links, click, type, select,
scroll, enter, wait_user, solve_task.
Defaults to `None`.
+ browser_log_to_file (bool): Whether to save detailed browser
+ action logs to file.
+ When enabled, logs action inputs/outputs, execution times,
+ and page loading times.
+ Logs are saved to an auto-generated timestamped file.
+ Defaults to `False`.
+ session_id (Optional[str]): A unique identifier for this browser
+ session. When multiple HybridBrowserToolkit instances are used
+ concurrently, different session IDs prevent them from sharing
+ the same browser session and causing conflicts. If None, a
+ default session will be used. Defaults to `None`.
+ default_start_url (str): The default URL to navigate to when
+ open_browser() is called without a start_url parameter or with
+ None. Defaults to `"https://google.com/"`.
"""
super().__init__()
self._headless = headless
self._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)
+ # Logging configuration - fixed values for simplicity
+ self.enable_action_logging = True
+ self.enable_timing_logging = True
+ self.enable_page_loading_logging = True
+ self.log_to_console = False # Always disabled for cleaner output
+ self.log_to_file = browser_log_to_file
+ self.max_log_length = None # No truncation for file logs
+
+ # Set up log file if needed
+ if self.log_to_file:
+ # Create log directory if it doesn't exist
+ log_dir = "browser_log"
+ os.makedirs(log_dir, exist_ok=True)
+
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
+ self.log_file_path: Optional[str] = os.path.join(
+ log_dir, f"hybrid_browser_toolkit_{timestamp}_{session_id}.log"
+ )
+ else:
+ self.log_file_path = None
+
+ # Initialize log buffer for in-memory storage
+ self.log_buffer: List[Dict[str, Any]] = []
+
# Configure enabled tools
if enabled_tools is None:
self.enabled_tools = self.DEFAULT_TOOLS.copy()
@@ -131,10 +190,23 @@ class HybridBrowserToolkit(BaseToolkit):
logger.info(f"Enabled tools: {self.enabled_tools}")
+ # Log initialization if file logging is enabled
+ if self.log_to_file:
+ logger.info(
+ "HybridBrowserToolkit initialized with file logging enabled"
+ )
+ logger.info(f"Log file path: {self.log_file_path}")
+
# Core components
- self._session = NVBrowserSession(
- headless=headless, user_data_dir=user_data_dir
+ temp_session = HybridBrowserSession(
+ headless=headless,
+ user_data_dir=user_data_dir,
+ stealth=stealth,
+ session_id=session_id,
)
+ # Use the session directly - singleton logic is handled in
+ # ensure_browser
+ self._session = temp_session
self._agent: Optional[PlaywrightLLMAgent] = None
self._unified_script = self._load_unified_analyzer()
@@ -151,7 +223,13 @@ class HybridBrowserToolkit(BaseToolkit):
try:
loop = asyncio.get_event_loop()
if not loop.is_closed() and not loop.is_running():
- loop.run_until_complete(self.close_browser())
+ # Try to close browser with a timeout to prevent hanging
+ try:
+ loop.run_until_complete(
+ asyncio.wait_for(self.close_browser(), timeout=2.0)
+ )
+ except asyncio.TimeoutError:
+ pass # Skip cleanup if it takes too long
except (RuntimeError, ImportError):
pass # Event loop unavailable, skip cleanup
except Exception:
@@ -186,12 +264,176 @@ class HybridBrowserToolkit(BaseToolkit):
f"{method_name}: 'ref' must be a non-empty string"
)
+ def _truncate_if_needed(self, content: Any) -> str:
+ r"""Truncate content if max_log_length is set."""
+ content_str = str(content)
+ if (
+ self.max_log_length is not None
+ and len(content_str) > self.max_log_length
+ ):
+ return content_str[: self.max_log_length] + "... [TRUNCATED]"
+ return content_str
+
+ async def _get_current_url(self) -> Optional[str]:
+ r"""Safely get the current URL of the active page."""
+ try:
+ page = await self._session.get_page()
+ if page and not page.is_closed():
+ return page.url
+ return None # Return None if page is closed
+ except Exception:
+ # This can happen if browser is not open.
+ return None
+
+ async def _log_action(
+ self,
+ action_name: str,
+ inputs: Dict[str, Any],
+ outputs: Any,
+ execution_time: float,
+ page_load_time: Optional[float] = None,
+ error: Optional[str] = None,
+ ) -> None:
+ r"""Log action details with comprehensive information."""
+ if not (self.enable_action_logging or self.enable_timing_logging):
+ return
+
+ current_url = await self._get_current_url()
+
+ log_entry: Dict[str, Any] = {
+ "timestamp": datetime.datetime.now().isoformat(),
+ "action": action_name,
+ "url": current_url,
+ "execution_time_ms": round(execution_time * 1000, 2),
+ }
+
+ if self.enable_action_logging:
+ log_entry["inputs"] = inputs
+ if error:
+ log_entry["error"] = str(error)
+ elif isinstance(outputs, dict):
+ # Unpack dictionary items into the log entry
+ log_entry.update(outputs)
+ elif isinstance(outputs, ToolResult):
+ log_entry["outputs"] = {
+ "text": outputs.text,
+ "images": outputs.images,
+ }
+ else:
+ # For non-dict outputs, assign to 'outputs' key
+ log_entry["outputs"] = outputs
+
+ if page_load_time is not None and self.enable_page_loading_logging:
+ log_entry["page_load_time_ms"] = round(page_load_time * 1000, 2)
+
+ # Add to buffer
+ self.log_buffer.append(log_entry)
+
+ # Console logging
+ if self.log_to_console:
+ log_msg = f"[BROWSER ACTION] {action_name}"
+ if self.enable_timing_logging:
+ log_msg += f" | Execution: {log_entry['execution_time_ms']}ms"
+ if page_load_time is not None and self.enable_page_loading_logging:
+ log_msg += f" | Page Load: {log_entry['page_load_time_ms']}ms"
+ if error:
+ log_msg += f" | ERROR: {error}"
+
+ logger.info(log_msg)
+
+ if self.enable_action_logging:
+ logger.info(f" Inputs: {self._truncate_if_needed(inputs)}")
+ if not error:
+ if isinstance(outputs, dict):
+ for key, value in outputs.items():
+ logger.info(
+ f" - {key}: "
+ f"{self._truncate_if_needed(value)}"
+ )
+ else:
+ logger.info(
+ f" Outputs: {self._truncate_if_needed(outputs)}"
+ )
+
+ # File logging
+ if self.log_to_file and self.log_file_path:
+ try:
+ with open(self.log_file_path, 'a', encoding='utf-8') as f:
+ # Write full log entry to file without truncation
+ f.write(
+ json.dumps(log_entry, ensure_ascii=False, indent=2)
+ + '\n'
+ )
+ except Exception as e:
+ logger.error(f"Failed to write to log file: {e}")
+
+ @staticmethod
+ def action_logger(func: Callable[..., Any]) -> Callable[..., Any]:
+ r"""Decorator to add logging to action methods."""
+
+ @wraps(func)
+ async def wrapper(self, *args, **kwargs):
+ action_name = func.__name__
+ start_time = time.time()
+
+ # Log inputs
+ inputs = {
+ "args": args, # Don't skip self since it's already handled
+ "kwargs": kwargs,
+ }
+
+ try:
+ # Execute the original function
+ result = await func(self, *args, **kwargs)
+ execution_time = time.time() - start_time
+
+ # Log success
+ await self._log_action(
+ action_name=action_name,
+ inputs=inputs,
+ outputs=result,
+ execution_time=execution_time,
+ )
+
+ return result
+
+ except Exception as e:
+ execution_time = time.time() - start_time
+ error_msg = f"{type(e).__name__}: {e!s}"
+
+ # Log error
+ await self._log_action(
+ action_name=action_name,
+ inputs=inputs,
+ outputs=None,
+ execution_time=execution_time,
+ error=error_msg,
+ )
+
+ raise
+
+ return wrapper
+
+ async def _get_session(self) -> "HybridBrowserSession":
+ """Get the correct singleton session instance."""
+ singleton = await HybridBrowserSession._get_or_create_instance(
+ self._session
+ )
+ if singleton is not self._session:
+ logger.debug("Updating to singleton session instance")
+ self._session = singleton
+ return self._session
+
async def _ensure_browser(self):
- await self._session.ensure_browser()
+ # Get singleton instance and update self._session if needed
+ session = await self._get_session()
+ await session.ensure_browser()
async def _require_page(self):
- await self._session.ensure_browser()
- return await self._session.get_page()
+ # Get singleton instance and update self._session if needed
+ session = await self._get_session()
+ await session.ensure_browser()
+ return await session.get_page()
async def _wait_for_page_stability(self):
r"""Wait for page to become stable after actions that might trigger
@@ -370,8 +612,108 @@ class HybridBrowserToolkit(BaseToolkit):
return "\n".join(lines)
+ async def _get_tab_info_for_output(self) -> Dict[str, Any]:
+ r"""Get tab information to include in action outputs."""
+ try:
+ # Ensure we have the correct singleton session instance first
+ session = await self._get_session()
+
+ # Add debug info for tab info retrieval
+ logger.debug("Attempting to get tab info from session...")
+ tab_info = await session.get_tab_info()
+ current_tab_index = await session.get_current_tab_index()
+
+ # Debug log the successful retrieval
+ logger.debug(
+ f"Successfully retrieved {len(tab_info)} tabs, current: "
+ f"{current_tab_index}"
+ )
+
+ return {
+ "tabs": tab_info,
+ "current_tab": current_tab_index,
+ "total_tabs": len(tab_info),
+ }
+ except Exception as e:
+ logger.warning(
+ f"Failed to get tab info from session: {type(e).__name__}: {e}"
+ )
+
+ # Try to get actual tab count from session pages directly
+ try:
+ # Get the correct session instance for fallback
+ fallback_session = await self._get_session()
+
+ # Check browser session state
+ session_state = {
+ "has_session": fallback_session is not None,
+ "has_pages_attr": hasattr(fallback_session, '_pages'),
+ "pages_count": len(fallback_session._pages)
+ if hasattr(fallback_session, '_pages')
+ else "unknown",
+ "has_page": hasattr(fallback_session, '_page')
+ and fallback_session._page is not None,
+ "session_id": getattr(
+ fallback_session, '_session_id', 'unknown'
+ ),
+ }
+ logger.debug(f"Browser session state: {session_state}")
+
+ actual_tab_count = 0
+ if (
+ hasattr(fallback_session, '_pages')
+ and fallback_session._pages
+ ):
+ actual_tab_count = len(fallback_session._pages)
+ # Also try to filter out closed pages
+ try:
+ open_pages = [
+ p
+ for p in fallback_session._pages
+ if not p.is_closed()
+ ]
+ actual_tab_count = len(open_pages)
+ logger.debug(
+ f"Found {actual_tab_count} open tabs out of "
+ f"{len(fallback_session._pages)} total"
+ )
+ except Exception:
+ # Keep the original count if we can't check page status
+ pass
+
+ if actual_tab_count == 0:
+ # If no pages, check if browser is even initialized
+ if (
+ hasattr(fallback_session, '_page')
+ and fallback_session._page is not None
+ ):
+ actual_tab_count = 1
+ logger.debug(
+ "No pages in list but main page exists, assuming "
+ "1 tab"
+ )
+ else:
+ actual_tab_count = 1
+ logger.debug("No pages found, defaulting to 1 tab")
+
+ logger.debug(f"Using fallback tab count: {actual_tab_count}")
+ return {
+ "tabs": [],
+ "current_tab": 0,
+ "total_tabs": actual_tab_count,
+ }
+
+ except Exception as fallback_error:
+ logger.warning(
+ f"Fallback tab count also failed: "
+ f"{type(fallback_error).__name__}: {fallback_error}"
+ )
+ return {"tabs": [], "current_tab": 0, "total_tabs": 1}
+
async def _exec_with_snapshot(
- self, action: Dict[str, Any]
+ self,
+ action: Dict[str, Any],
+ element_details: Optional[Dict[str, Any]] = None,
) -> Dict[str, str]:
r"""Execute action and return result with snapshot comparison."""
@@ -379,72 +721,166 @@ class HybridBrowserToolkit(BaseToolkit):
action_type = action.get("type", "unknown")
logger.info(f"Executing action: {action_type}")
- # Get before snapshot
- logger.info("Capturing pre-action snapshot...")
- snapshot_start = time.time()
- before_snapshot = await self._session.get_snapshot(
- force_refresh=True, diff_only=False
- )
- snapshot_time = time.time() - snapshot_start
- logger.info(f"Pre-action snapshot captured in {snapshot_time:.2f}s")
+ action_start_time = time.time()
+ inputs: Dict[str, Any] = {"action": action}
+ page_load_time = None
- # Execute action
- logger.info(f"Executing {action_type} action...")
- action_start = time.time()
- result = await self._session.exec_action(action)
- action_time = time.time() - action_start
- logger.info(f"Action {action_type} completed in {action_time:.2f}s")
-
- # Wait for page stability after action (especially important for click)
- if action_type in ["click", "type", "select", "enter"]:
- logger.info(
- f"Waiting for page stability " f"after {action_type}..."
+ try:
+ # Get before snapshot
+ logger.info("Capturing pre-action snapshot...")
+ snapshot_start = time.time()
+ before_snapshot = await self._session.get_snapshot(
+ force_refresh=True, diff_only=False
)
- stability_start = time.time()
- await self._wait_for_page_stability()
- stability_time = time.time() - stability_start
+ snapshot_time = time.time() - snapshot_start
logger.info(
- f"Page stability wait " f"completed in {stability_time:.2f}s"
+ f"Pre-action snapshot captured in {snapshot_time:.2f}s"
)
- # Get after snapshot
- logger.info("Capturing post-action snapshot...")
- snapshot_start = time.time()
- after_snapshot = await self._session.get_snapshot(
- force_refresh=True, diff_only=False
- )
- snapshot_time = time.time() - snapshot_start
- logger.info(
- f"Post-action snapshot " f"captured in {snapshot_time:.2f}s"
- )
-
- # Check for snapshot quality and log warnings
- if before_snapshot == after_snapshot:
- snapshot = "snapshot not changed"
- logger.debug("Page snapshot unchanged after action")
- else:
- snapshot = after_snapshot
- # Check if snapshot is empty or problematic
- if "<empty>" in after_snapshot:
- logger.warning(
- f"Action {action_type} resulted "
- f"in empty snapshot - "
- f"page may still be loading"
+ # Execute action
+ logger.info(f"Executing {action_type} action...")
+ exec_start = time.time()
+ exec_result = await self._session.exec_action(action)
+ exec_time = time.time() - exec_start
+ logger.info(f"Action {action_type} completed in {exec_time:.2f}s")
+
+ # Parse the detailed result from ActionExecutor
+ if isinstance(exec_result, dict):
+ result_message = exec_result.get("message", str(exec_result))
+ action_details = exec_result.get("details", {})
+ success = exec_result.get("success", True)
+ else:
+ result_message = str(exec_result)
+ action_details = {}
+ success = True
+
+ # Wait for page stability after action (especially important for
+ # click)
+ stability_time: float = 0.0
+ if action_type in ["click", "type", "select", "enter"]:
+ logger.info(
+ f"Waiting for page stability " f"after {action_type}..."
)
- elif len(after_snapshot.strip()) < 50:
- logger.warning(
- f"Action {action_type} resulted "
- f"in very short snapshot:"
- f" {len(after_snapshot)} chars"
+ stability_start = time.time()
+ await self._wait_for_page_stability()
+ stability_time = time.time() - stability_start
+ logger.info(
+ f"Page stability wait "
+ f"completed in "
+ f"{stability_time:.2f}s"
)
+ page_load_time = stability_time
+
+ # Enhanced logging for page loading times
+ if self.enable_page_loading_logging and self.log_to_console:
+ logger.info(
+ f"[PAGE LOADING] Page stability for {action_type}: "
+ f"{round(stability_time * 1000, 2)}ms"
+ )
+
+ # Get after snapshot
+ logger.info("Capturing post-action snapshot...")
+ snapshot_start = time.time()
+ after_snapshot = await self._session.get_snapshot(
+ force_refresh=True, diff_only=False
+ )
+ snapshot_time = time.time() - snapshot_start
+ logger.info(
+ f"Post-action snapshot " f"captured in {snapshot_time:.2f}s"
+ )
+
+ # Check for snapshot quality and log warnings
+ if before_snapshot == after_snapshot:
+ snapshot = "snapshot not changed"
+ logger.debug("Page snapshot unchanged after action")
else:
+ snapshot = after_snapshot
+ # Check if snapshot is empty or problematic
+ if "<empty>" in after_snapshot:
+ logger.warning(
+ f"Action {action_type} resulted "
+ f"in empty snapshot - "
+ f"page may still be loading"
+ )
+ elif len(after_snapshot.strip()) < 50:
+ logger.warning(
+ f"Action {action_type} resulted "
+ f"in very short snapshot:"
+ f" {len(after_snapshot)} chars"
+ )
+ else:
+ logger.debug(
+ f"Action {action_type} resulted "
+ f"in updated snapshot: "
+ f"{len(after_snapshot)} chars"
+ )
+
+ # Get tab information for output
+ tab_info = await self._get_tab_info_for_output()
+
+ # Create comprehensive output for logging
+ execution_time = time.time() - action_start_time
+ outputs = {
+ "result": result_message,
+ "snapshot": snapshot,
+ "success": success,
+ "action_details": action_details,
+ "execution_stats": {
+ "exec_time_ms": round(exec_time * 1000, 2),
+ "stability_time_ms": round(stability_time * 1000, 2)
+ if stability_time > 0
+ else None,
+ "total_time_ms": round(execution_time * 1000, 2),
+ },
+ **tab_info, # Include tab information
+ }
+
+ # If snapshot is unchanged after click, add element details to log
+ if (
+ snapshot == "snapshot not changed"
+ and action_type == "click"
+ and element_details
+ ):
logger.debug(
- f"Action {action_type} resulted "
- f"in updated snapshot: "
- f"{len(after_snapshot)} chars"
+ "Snapshot unchanged after click. "
+ "Adding element details to log."
+ )
+ outputs["clicked_element_tag"] = element_details.get(
+ "tagName", "N/A"
)
+ outputs["clicked_element_content"] = element_details.get(
+ "name", ""
+ )
+ outputs["clicked_element_type"] = element_details.get(
+ "role", "generic"
+ )
+
+ # Log the action with all details
+ await self._log_action(
+ action_name=f"_exec_with_snapshot_{action_type}",
+ inputs=inputs,
+ outputs=outputs,
+ execution_time=execution_time,
+ page_load_time=page_load_time,
+ )
+
+ return {"result": result_message, "snapshot": snapshot}
+
+ except Exception as e:
+ execution_time = time.time() - action_start_time
+ error_msg = f"{type(e).__name__}: {e!s}"
+
+ # Log error
+ await self._log_action(
+ action_name=f"_exec_with_snapshot_{action_type}",
+ inputs=inputs,
+ outputs=None,
+ execution_time=execution_time,
+ page_load_time=page_load_time,
+ error=error_msg,
+ )
- return {"result": result, "snapshot": snapshot}
+ raise
async def _extract_links_by_refs(
self, snapshot: str, page, refs: List[str]
@@ -509,29 +945,31 @@ class HybridBrowserToolkit(BaseToolkit):
# Public API Methods
- async def open_browser(
- self, start_url: Optional[str] = "https://search.brave.com/"
- ) -> Dict[str, str]:
- r"""Launches a new browser session, making it ready for web automation.
-
- This method initializes the underlying browser instance. If a
- `start_url` is provided, it will also navigate to that URL. If you
- don't have a specific URL to start with, you can use a search engine
- like 'https://search.brave.com/'.
+ async def open_browser(self) -> Dict[str, Any]:
+ r"""Launches a new browser session and navigates to the configured
+ default page.
- Args:
- start_url (Optional[str]): The initial URL to navigate to after the
- browser is launched. If not provided, the browser will start
- with a blank page. (default: :obj:`https://search.brave.com/`)
+ 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.
Returns:
- Dict[str, str]: A dictionary containing:
+ Dict[str, Any]: A dictionary containing:
- "result": A string confirming that the browser session has
- started.
+ started and the default page has been loaded.
- "snapshot": A textual representation of the current page's
interactive elements. This snapshot is crucial for
identifying elements for subsequent actions.
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
"""
+ # Add logging if enabled
+ action_start = time.time()
+ inputs: Dict[str, Any] = {} # No input parameters for agents
+
logger.info("Starting browser session...")
browser_start = time.time()
@@ -539,20 +977,42 @@ class HybridBrowserToolkit(BaseToolkit):
browser_time = time.time() - browser_start
logger.info(f"Browser session started in {browser_time:.2f}s")
- if start_url:
- logger.info(f"Auto-navigating to start URL: {start_url}")
- return await self.visit_page(start_url)
+ try:
+ # Always use the configured default start URL
+ start_url = self.default_start_url
+ logger.info(f"Navigating to configured default page: {start_url}")
+
+ result = await self.visit_page(start_url)
+
+ # Log success
+ if self.enable_action_logging or self.enable_timing_logging:
+ execution_time = time.time() - action_start
+ await self._log_action(
+ action_name="open_browser",
+ inputs=inputs,
+ outputs={
+ "result": "Browser opened and navigated to default "
+ "page."
+ },
+ execution_time=execution_time,
+ )
- logger.info("Capturing initial browser snapshot...")
- 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"Initial snapshot captured in {snapshot_time:.2f}s")
+ return result
- return {"result": "Browser session started.", "snapshot": snapshot}
+ except Exception as e:
+ # Log error
+ if self.enable_action_logging or self.enable_timing_logging:
+ execution_time = time.time() - action_start
+ await self._log_action(
+ action_name="open_browser",
+ inputs=inputs,
+ outputs=None,
+ execution_time=execution_time,
+ error=f"{type(e).__name__}: {e!s}",
+ )
+ raise
+ @action_logger
async def close_browser(self) -> str:
r"""Closes the current browser session and releases all associated
resources.
@@ -573,7 +1033,8 @@ class HybridBrowserToolkit(BaseToolkit):
await self._session.close()
return "Browser session closed."
- async def visit_page(self, url: str) -> Dict[str, str]:
+ @action_logger
+ async def visit_page(self, url: str) -> Dict[str, Any]:
r"""Navigates the current browser page to a specified URL.
Args:
@@ -581,16 +1042,22 @@ class HybridBrowserToolkit(BaseToolkit):
valid URL.
Returns:
- Dict[str, str]: A dictionary containing:
+ 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.
"""
if not url or not isinstance(url, str):
return {
"result": "Error: 'url' must be a non-empty string",
"snapshot": "",
+ "tabs": [],
+ "current_tab": 0,
+ "total_tabs": 1,
}
if '://' not in url:
@@ -613,8 +1080,142 @@ class HybridBrowserToolkit(BaseToolkit):
snapshot_time = time.time() - snapshot_start
logger.info(f"Navigation snapshot captured in {snapshot_time:.2f}s")
- return {"result": nav_result, "snapshot": snapshot}
+ # Get tab information
+ tab_info = await self._get_tab_info_for_output()
+
+ return {"result": nav_result, "snapshot": snapshot, **tab_info}
+
+ @action_logger
+ async def back(self) -> Dict[str, Any]:
+ r"""Navigates the browser back to the previous page in history.
+
+ This function simulates clicking the browser's back button, taking
+ you to the previously visited page if one exists in the browser
+ history.
+ Returns:
+ Dict[str, Any]: A dictionary containing:
+ - "result": A message indicating the outcome of the back
+ navigation, e.g., "Back navigation successful." or an error
+ message if no previous page exists.
+ - "snapshot": A new textual snapshot of the page after
+ navigation. If the snapshot is unchanged, it will be the
+ string "snapshot not changed".
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
+ """
+ page = await self._require_page()
+
+ try:
+ logger.info("Navigating back in browser history...")
+ nav_start = time.time()
+ await page.go_back(wait_until="domcontentloaded", timeout=30000)
+ nav_time = time.time() - nav_start
+ logger.info(f"Back navigation completed in {nav_time:.2f}s")
+
+ # Wait for page stability
+ await self._wait_for_page_stability()
+
+ # Get snapshot
+ logger.info("Capturing page snapshot after back 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"Back navigation snapshot captured in {snapshot_time:.2f}s"
+ )
+
+ # Get tab information
+ tab_info = await self._get_tab_info_for_output()
+
+ return {
+ "result": "Back navigation successful.",
+ "snapshot": snapshot,
+ **tab_info,
+ }
+
+ except Exception as e:
+ logger.warning(f"Back navigation failed: {e}")
+ # Get current snapshot even if navigation failed
+ snapshot = await self._session.get_snapshot(
+ force_refresh=True, diff_only=False
+ )
+ tab_info = await self._get_tab_info_for_output()
+ return {
+ "result": f"Back navigation failed: {e!s}",
+ "snapshot": snapshot,
+ **tab_info,
+ }
+
+ @action_logger
+ async def forward(self) -> Dict[str, Any]:
+ r"""Navigates the browser forward to the next page in history.
+
+ This function simulates clicking the browser's forward button, taking
+ you to the next page in the browser history if one exists (i.e.,
+ if you have previously navigated back).
+
+ Returns:
+ Dict[str, Any]: A dictionary containing:
+ - "result": A message indicating the outcome of the forward
+ navigation, e.g., "Forward navigation successful." or an
+ error message if no next page exists.
+ - "snapshot": A new textual snapshot of the page after
+ navigation. If the snapshot is unchanged, it will be the
+ string "snapshot not changed".
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
+ """
+ page = await self._require_page()
+
+ try:
+ logger.info("Navigating forward in browser history...")
+ nav_start = time.time()
+ await page.go_forward(wait_until="domcontentloaded", timeout=30000)
+ nav_time = time.time() - nav_start
+ logger.info(f"Forward navigation completed in {nav_time:.2f}s")
+
+ # Wait for page stability
+ await self._wait_for_page_stability()
+
+ # Get snapshot
+ logger.info("Capturing page snapshot after forward 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"Forward navigation snapshot captured in {snapshot_time:.2f}s"
+ )
+
+ # Get tab information
+ tab_info = await self._get_tab_info_for_output()
+
+ return {
+ "result": "Forward navigation successful.",
+ "snapshot": snapshot,
+ **tab_info,
+ }
+
+ except Exception as e:
+ logger.warning(f"Forward navigation failed: {e}")
+ # Get current snapshot even if navigation failed
+ snapshot = await self._session.get_snapshot(
+ force_refresh=True, diff_only=False
+ )
+ tab_info = await self._get_tab_info_for_output()
+ return {
+ "result": f"Forward navigation failed: {e!s}",
+ "snapshot": snapshot,
+ **tab_info,
+ }
+
+ @action_logger
async def get_page_snapshot(self) -> str:
r"""Captures a textual representation of the current page's content.
@@ -650,6 +1251,7 @@ class HybridBrowserToolkit(BaseToolkit):
)
@dependencies_required('PIL')
+ @action_logger
async def get_som_screenshot(self):
r"""Captures a screenshot of the current webpage and visually marks all
interactive elements. "SoM" stands for "Set of Marks".
@@ -733,7 +1335,7 @@ class HybridBrowserToolkit(BaseToolkit):
return ToolResult(text=text_result, images=[img_data_url])
- async def click(self, *, ref: str) -> Dict[str, str]:
+ async def click(self, *, ref: str) -> Dict[str, Any]:
r"""Clicks on an interactive element on the page.
Args:
@@ -742,12 +1344,15 @@ class HybridBrowserToolkit(BaseToolkit):
`get_som_screenshot`).
Returns:
- Dict[str, str]: A dictionary containing:
+ Dict[str, Any]: A dictionary containing:
- "result": A message confirming the click action.
- "snapshot": A new textual snapshot of the page after the
click, which may have changed as a result of the action. If
the snapshot is unchanged, it will be the string "snapshot
not changed".
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
"""
self._validate_ref(ref, "click")
@@ -755,19 +1360,30 @@ class HybridBrowserToolkit(BaseToolkit):
elements = analysis.get("elements", {})
if ref not in elements:
available_refs = list(elements.keys())
- logger.error(
- f"Error: Element reference '{ref}' not found. "
- f"Available refs: {available_refs}"
- )
+ logger.error(f"Error: Element reference '{ref}' not found. ")
+ # Added snapshot to give more context on failure
+ snapshot = self._format_snapshot_from_analysis(analysis)
+ tab_info = await self._get_tab_info_for_output()
return {
"result": f"Error: Element reference '{ref}' not found. "
- f"Available refs: {available_refs}"
+ f"Available refs: {available_refs}",
+ "snapshot": snapshot,
+ **tab_info,
}
+ element_details = elements.get(ref)
action = {"type": "click", "ref": ref}
- return await self._exec_with_snapshot(action)
+ result = await self._exec_with_snapshot(
+ action, element_details=element_details
+ )
+
+ # Add tab information to the result
+ tab_info = await self._get_tab_info_for_output()
+ result.update(tab_info)
- async def type(self, *, ref: str, text: str) -> Dict[str, str]:
+ return result
+
+ async def type(self, *, ref: str, text: str) -> Dict[str, Any]:
r"""Types text into an input field, such as a textbox or search bar.
Args:
@@ -775,18 +1391,27 @@ class HybridBrowserToolkit(BaseToolkit):
text (str): The text to be typed into the element.
Returns:
- Dict[str, str]: A dictionary containing:
+ Dict[str, Any]: A dictionary containing:
- "result": A message confirming the type action.
- "snapshot": A new textual snapshot of the page after the
text has been entered.
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
"""
self._validate_ref(ref, "type")
await self._get_unified_analysis() # Ensure aria-ref attributes
action = {"type": "type", "ref": ref, "text": text}
- return await self._exec_with_snapshot(action)
+ result = await self._exec_with_snapshot(action)
+
+ # Add tab information to the result
+ tab_info = await self._get_tab_info_for_output()
+ result.update(tab_info)
+
+ return result
- async def select(self, *, ref: str, value: str) -> Dict[str, str]:
+ async def select(self, *, ref: str, value: str) -> Dict[str, Any]:
r"""Selects an option from a dropdown (`<select>`) element.
Args:
@@ -796,17 +1421,26 @@ class HybridBrowserToolkit(BaseToolkit):
visible text.
Returns:
- Dict[str, str]: A dictionary containing:
+ Dict[str, Any]: A dictionary containing:
- "result": A message confirming the select action.
- "snapshot": A new snapshot of the page after the selection.
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
"""
self._validate_ref(ref, "select")
await self._get_unified_analysis()
action = {"type": "select", "ref": ref, "value": value}
- return await self._exec_with_snapshot(action)
+ result = await self._exec_with_snapshot(action)
+
+ # Add tab information to the result
+ tab_info = await self._get_tab_info_for_output()
+ result.update(tab_info)
- async def scroll(self, *, direction: str, amount: int) -> Dict[str, str]:
+ return result
+
+ async def scroll(self, *, direction: str, amount: int) -> Dict[str, Any]:
r"""Scrolls the page window up or down by a specified amount.
Args:
@@ -815,40 +1449,68 @@ class HybridBrowserToolkit(BaseToolkit):
amount (int): The number of pixels to scroll.
Returns:
- Dict[str, str]: A dictionary containing:
+ Dict[str, Any]: A dictionary containing:
- "result": A confirmation of the scroll action.
- "snapshot": A new snapshot of the page after scrolling.
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
"""
if direction not in ("up", "down"):
+ tab_info = await self._get_tab_info_for_output()
return {
"result": "Error: direction must be 'up' or 'down'",
"snapshot": "",
+ **tab_info,
}
action = {"type": "scroll", "direction": direction, "amount": amount}
- return await self._exec_with_snapshot(action)
+ result = await self._exec_with_snapshot(action)
- async def enter(self, *, ref: str) -> Dict[str, str]:
- r"""Simulates pressing the Enter key on a specific element.
+ # Add tab information to the result
+ tab_info = await self._get_tab_info_for_output()
+ result.update(tab_info)
- This is often used to submit forms after filling them out.
+ return result
- Args:
- ref (str): The reference ID of the element to press Enter on.
+ async def enter(self) -> Dict[str, Any]:
+ r"""Simulates pressing the Enter key on the currently focused element.
+
+ This tool is used to execute or confirm an action after interacting
+ with
+ an element, such as:
+ - Submitting a search query after typing in a search box.
+ - Confirming a form submission.
+ - Executing a command in a text input field.
+
+ The common usage pattern is to first use the 'type' tool to input
+ text, which sets the focus, and then call 'enter' without any
+ parameters to trigger the action.
Returns:
- Dict[str, str]: A dictionary containing:
- - "result": A confirmation of the action.
+ Dict[str, Any]: A dictionary containing:
+ - "result": A confirmation of the Enter key action.
- "snapshot": A new page snapshot, as this action often
triggers navigation or page updates.
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
"""
- self._validate_ref(ref, "enter")
- action = {"type": "enter", "ref": ref}
- return await self._exec_with_snapshot(action)
+ # Always press Enter on the currently focused element
+ action = {"type": "enter"}
+
+ result = await self._exec_with_snapshot(action)
+
+ # Add tab information to the result
+ tab_info = await self._get_tab_info_for_output()
+ result.update(tab_info)
+
+ return result
+ @action_logger
async def wait_user(
self, timeout_sec: Optional[float] = None
- ) -> Dict[str, str]:
+ ) -> Dict[str, Any]:
r"""Pauses the agent's execution and waits for human intervention.
This is useful for tasks that require manual steps, like solving a
@@ -861,10 +1523,13 @@ class HybridBrowserToolkit(BaseToolkit):
automatically. If `None`, it will wait indefinitely.
Returns:
- Dict[str, str]: A dictionary containing:
+ Dict[str, Any]: A dictionary containing:
- "result": A message indicating how the wait ended (e.g.,
"User resumed." or "Timeout... reached, auto-resumed.").
- "snapshot": The current page snapshot after the wait.
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
"""
import asyncio
@@ -905,8 +1570,11 @@ class HybridBrowserToolkit(BaseToolkit):
snapshot = await self._session.get_snapshot(
force_refresh=True, diff_only=False
)
- return {"result": result_msg, "snapshot": snapshot}
+ tab_info = await self._get_tab_info_for_output()
+
+ return {"result": result_msg, "snapshot": snapshot, **tab_info}
+ @action_logger
async def get_page_links(self, *, ref: List[str]) -> Dict[str, Any]:
r"""Retrieves the full URLs for a given list of link reference IDs.
@@ -938,6 +1606,7 @@ class HybridBrowserToolkit(BaseToolkit):
return {"links": links}
+ @action_logger
async def solve_task(
self, task_prompt: str, start_url: str, max_steps: int = 15
) -> str:
@@ -967,6 +1636,48 @@ class HybridBrowserToolkit(BaseToolkit):
await agent.process_command(task_prompt, max_steps=max_steps)
return "Task processing finished - see stdout for detailed trace."
+ def get_log_summary(self) -> Dict[str, Any]:
+ r"""Get a summary of logged actions."""
+ if not self.log_buffer:
+ return {"total_actions": 0, "summary": "No actions logged"}
+
+ total_actions = len(self.log_buffer)
+ total_execution_time = sum(
+ entry.get("execution_time_ms", 0) for entry in self.log_buffer
+ )
+ total_page_load_time = sum(
+ entry.get("page_load_time_ms", 0)
+ for entry in self.log_buffer
+ if "page_load_time_ms" in entry
+ )
+
+ action_counts: Dict[str, int] = {}
+ error_count = 0
+
+ for entry in self.log_buffer:
+ action = entry["action"]
+ action_counts[action] = action_counts.get(action, 0) + 1
+ if "error" in entry:
+ error_count += 1
+
+ return {
+ "total_actions": total_actions,
+ "total_execution_time_ms": round(total_execution_time, 2),
+ "total_page_load_time_ms": round(total_page_load_time, 2),
+ "action_counts": action_counts,
+ "error_count": error_count,
+ "success_rate": round(
+ (total_actions - error_count) / total_actions * 100, 2
+ )
+ if total_actions > 0
+ else 0,
+ }
+
+ def clear_logs(self) -> None:
+ r"""Clear the log buffer."""
+ self.log_buffer.clear()
+ logger.info("Log buffer cleared")
+
def get_tools(self) -> List[FunctionTool]:
r"""Get available function tools
based on enabled_tools configuration."""
@@ -975,6 +1686,8 @@ class HybridBrowserToolkit(BaseToolkit):
"open_browser": self.open_browser,
"close_browser": self.close_browser,
"visit_page": self.visit_page,
+ "back": self.back,
+ "forward": self.forward,
"get_page_snapshot": self.get_page_snapshot,
"get_som_screenshot": self.get_som_screenshot,
"get_page_links": self.get_page_links,
@@ -985,6 +1698,9 @@ class HybridBrowserToolkit(BaseToolkit):
"enter": self.enter,
"wait_user": self.wait_user,
"solve_task": self.solve_task,
+ "switch_tab": self.switch_tab,
+ "close_tab": self.close_tab,
+ "get_tab_info": self.get_tab_info,
}
enabled_tools = []
@@ -998,11 +1714,165 @@ class HybridBrowserToolkit(BaseToolkit):
continue
if tool_name in tool_map:
- enabled_tools.append(
- FunctionTool(cast(Callable, tool_map[tool_name]))
+ tool = FunctionTool(
+ cast(Callable[..., Any], tool_map[tool_name])
)
+ enabled_tools.append(tool)
else:
logger.warning(f"Unknown tool name: {tool_name}")
logger.info(f"Returning {len(enabled_tools)} enabled tools")
return enabled_tools
+
+ def clone_for_new_session(
+ self, new_session_id: Optional[str] = None
+ ) -> "HybridBrowserToolkit":
+ r"""Create a new instance of HybridBrowserToolkit with a unique
+ session.
+
+ Args:
+ new_session_id: Optional new session ID. If None, a UUID will be
+ generated.
+
+ Returns:
+ A new HybridBrowserToolkit instance with the same configuration
+ but a different session.
+ """
+ import uuid
+
+ if new_session_id is None:
+ new_session_id = str(uuid.uuid4())[:8]
+
+ 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}/",
+ enabled_tools=self.enabled_tools.copy(),
+ browser_log_to_file=self.log_to_file,
+ session_id=new_session_id,
+ default_start_url=self.default_start_url,
+ )
+
+ @action_logger
+ async def switch_tab(self, *, tab_index: int) -> Dict[str, Any]:
+ r"""Switches to a specific browser tab by its index.
+
+ This allows you to control which tab is currently active. After
+ switching, all subsequent browser actions will operate on the newly
+ selected tab.
+
+ Args:
+ tab_index (int): The zero-based index of the tab to switch to.
+ Use `get_tab_info` to see available tabs and their indices.
+
+ Returns:
+ Dict[str, Any]: A dictionary containing:
+ - "result": A message indicating success or failure of the
+ tab switch.
+ - "snapshot": A textual snapshot of the newly active tab's
+ content.
+ - "tabs": List of all open tabs with their information.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of open tabs.
+ """
+ await self._ensure_browser()
+ session = await self._get_session()
+
+ success = await session.switch_to_tab(tab_index)
+
+ if success:
+ snapshot = await session.get_snapshot(
+ force_refresh=True, diff_only=False
+ )
+ tab_info = await self._get_tab_info_for_output()
+
+ result = {
+ "result": f"Successfully switched to tab {tab_index}",
+ "snapshot": snapshot,
+ **tab_info,
+ }
+ else:
+ tab_info = await self._get_tab_info_for_output()
+ result = {
+ "result": f"Failed to switch to tab {tab_index}. Tab may not "
+ f"exist.",
+ "snapshot": "",
+ **tab_info,
+ }
+
+ return result
+
+ @action_logger
+ async def close_tab(self, *, tab_index: int) -> Dict[str, Any]:
+ r"""Closes a specific browser tab by its index.
+
+ After closing a tab, the browser will automatically switch to another
+ available tab. If the closed tab was the only one open, the browser
+ session will remain active but without any pages.
+
+ Args:
+ tab_index (int): The zero-based index of the tab to close.
+
+ Returns:
+ Dict[str, Any]: A dictionary containing:
+ - "result": A message indicating success or failure of the
+ tab closure.
+ - "snapshot": A textual snapshot of the currently active tab
+ after the closure (empty if no tabs remain).
+ - "tabs": List of remaining open tabs.
+ - "current_tab": Index of the currently active tab.
+ - "total_tabs": Total number of remaining open tabs.
+ """
+ await self._ensure_browser()
+ session = await self._get_session()
+
+ success = await session.close_tab(tab_index)
+
+ if success:
+ # Get current state after closing the tab
+ try:
+ snapshot = await session.get_snapshot(
+ force_refresh=True, diff_only=False
+ )
+ except Exception:
+ snapshot = "" # No active tab
+
+ tab_info = await self._get_tab_info_for_output()
+
+ result = {
+ "result": f"Successfully closed tab {tab_index}",
+ "snapshot": snapshot,
+ **tab_info,
+ }
+ else:
+ tab_info = await self._get_tab_info_for_output()
+ result = {
+ "result": f"Failed to close tab {tab_index}. Tab may not "
+ f"exist.",
+ "snapshot": "",
+ **tab_info,
+ }
+
+ return result
+
+ @action_logger
+ async def get_tab_info(self) -> Dict[str, Any]:
+ r"""Retrieves information about all currently open browser tabs.
+
+ This provides a comprehensive overview of the browser state, including
+ all open tabs, their titles, URLs, and which one is currently active.
+
+ Returns:
+ Dict[str, Any]: A dictionary containing:
+ - "tabs": A list of dictionaries, each representing a tab with:
+ - "index": The zero-based index of the tab
+ - "title": The page title
+ - "url": The current URL
+ - "is_current": Whether this is the currently active tab
+ - "current_tab": Index of the currently active tab
+ - "total_tabs": Total number of open tabs
+ """
+ await self._ensure_browser()
+ return await self._get_tab_info_for_output()
diff --git a/camel/toolkits/hybrid_browser_toolkit/stealth_config.py b/camel/toolkits/hybrid_browser_toolkit/stealth_config.py
new file mode 100644
index 000000000..7da275773
--- /dev/null
+++ b/camel/toolkits/hybrid_browser_toolkit/stealth_config.py
@@ -0,0 +1,116 @@
+# ========= 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/stealth_script.js b/camel/toolkits/hybrid_browser_toolkit/stealth_script.js
new file mode 100644
index 000000000..ba9296cf5
Binary files /dev/null and b/camel/toolkits/hybrid_browser_toolkit/stealth_script.js differ
diff --git a/camel/toolkits/note_taking_toolkit.py b/camel/toolkits/note_taking_toolkit.py
index 61c33d6a3..e5bb91602 100644
--- a/camel/toolkits/note_taking_toolkit.py
+++ b/camel/toolkits/note_taking_toolkit.py
@@ -41,27 +41,21 @@ class NoteTakingToolkit(BaseToolkit):
self.note_file_path = Path(note_file_path)
self.note_file_path.parent.mkdir(parents=True, exist_ok=True)
- def take_note(self, content: str, update: bool = False) -> str:
- r"""Takes a note and saves it to the note file.
+ def append_note(self, content: str) -> str:
+ r"""Appends a note to the note file.
Args:
- content (str): The content of the note to be saved.
- update (bool): If True, the existing note file will be
- overwritten with the new content. If False, the new content
- will be appended to the end of the file.
- (default: :obj:`False`)
+ content (str): The content of the note to be appended.
Returns:
str: A message indicating the result of the operation.
"""
- mode = "w" if update else "a"
try:
- with self.note_file_path.open(mode, encoding="utf-8") as f:
+ with self.note_file_path.open("a", encoding="utf-8") as f:
f.write(content + "\n")
- action = "updated" if update else "appended to"
- return f"Note successfully {action} in {self.note_file_path}."
+ return f"Note successfully appended to in {self.note_file_path}."
except Exception as e:
- return f"Error taking note: {e}"
+ return f"Error appending note: {e}"
def read_note(self) -> str:
r"""Reads the content of the note file.
@@ -85,6 +79,6 @@ class NoteTakingToolkit(BaseToolkit):
List[FunctionTool]: A list of FunctionTool objects.
"""
return [
- FunctionTool(self.take_note),
+ FunctionTool(self.append_note),
FunctionTool(self.read_note),
]
diff --git a/docs/conf.py b/docs/conf.py
index c5c1c2ec5..44e50012a 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.71a5'
+release = '0.2.71a6'
html_favicon = (
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'
diff --git a/docs/mintlify/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.mdx b/docs/mintlify/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.mdx
index 8b806536c..733f64835 100644
--- a/docs/mintlify/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.mdx
+++ b/docs/mintlify/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.mdx
@@ -1,11 +1,11 @@
<a id="camel.toolkits.hybrid_browser_toolkit.browser_session"></a>
-<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession"></a>
+<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession"></a>
-## NVBrowserSession
+## HybridBrowserSession
```python
-class NVBrowserSession:
+class HybridBrowserSession:
```
Lightweight wrapper around Playwright for non-visual (headless)
@@ -17,7 +17,7 @@ duplicating Playwright setup code.
This class is a singleton per event-loop.
-<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession.__new__"></a>
+<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession.__new__"></a>
### __new__
@@ -25,7 +25,7 @@ This class is a singleton per event-loop.
def __new__(cls):
```
-<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession.__init__"></a>
+<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession.__init__"></a>
### __init__
diff --git a/docs/mintlify/reference/camel.toolkits.non_visual_browser_toolkit.nv_browser_session.mdx b/docs/mintlify/reference/camel.toolkits.non_visual_browser_toolkit.nv_browser_session.mdx
index bd74fa5f9..112068572 100644
--- a/docs/mintlify/reference/camel.toolkits.non_visual_browser_toolkit.nv_browser_session.mdx
+++ b/docs/mintlify/reference/camel.toolkits.non_visual_browser_toolkit.nv_browser_session.mdx
@@ -1,11 +1,11 @@
<a id="camel.toolkits.non_visual_browser_toolkit.nv_browser_session"></a>
-<a id="camel.toolkits.non_visual_browser_toolkit.nv_browser_session.NVBrowserSession"></a>
+<a id="camel.toolkits.non_visual_browser_toolkit.nv_browser_session.HybridBrowserSession"></a>
-## NVBrowserSession
+## HybridBrowserSession
```python
-class NVBrowserSession:
+class HybridBrowserSession:
```
Lightweight wrapper around Playwright for non-visual (headless)
@@ -17,7 +17,7 @@ duplicating Playwright setup code.
This class is a singleton per event-loop.
-<a id="camel.toolkits.non_visual_browser_toolkit.nv_browser_session.NVBrowserSession.__new__"></a>
+<a id="camel.toolkits.non_visual_browser_toolkit.nv_browser_session.HybridBrowserSession.__new__"></a>
### __new__
@@ -25,7 +25,7 @@ This class is a singleton per event-loop.
def __new__(cls):
```
-<a id="camel.toolkits.non_visual_browser_toolkit.nv_browser_session.NVBrowserSession.__init__"></a>
+<a id="camel.toolkits.non_visual_browser_toolkit.nv_browser_session.HybridBrowserSession.__init__"></a>
### __init__
diff --git a/docs/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.md b/docs/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.md
index 8b806536c..733f64835 100644
--- a/docs/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.md
+++ b/docs/reference/camel.toolkits.hybrid_browser_toolkit.browser_session.md
@@ -1,11 +1,11 @@
<a id="camel.toolkits.hybrid_browser_toolkit.browser_session"></a>
-<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession"></a>
+<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession"></a>
-## NVBrowserSession
+## HybridBrowserSession
```python
-class NVBrowserSession:
+class HybridBrowserSession:
```
Lightweight wrapper around Playwright for non-visual (headless)
@@ -17,7 +17,7 @@ duplicating Playwright setup code.
This class is a singleton per event-loop.
-<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession.__new__"></a>
+<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession.__new__"></a>
### __new__
@@ -25,7 +25,7 @@ This class is a singleton per event-loop.
def __new__(cls):
```
-<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession.__init__"></a>
+<a id="camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession.__init__"></a>
### __init__
diff --git a/examples/toolkits/hybrid_browser_toolkit_example.py b/examples/toolkits/hybrid_browser_toolkit_example.py
index 22b635b62..eb5df7d62 100644
--- a/examples/toolkits/hybrid_browser_toolkit_example.py
+++ b/examples/toolkits/hybrid_browser_toolkit_example.py
@@ -13,10 +13,11 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import asyncio
import logging
+import os
from camel.agents import ChatAgent
from camel.models import ModelFactory
-from camel.toolkits import HybridBrowserToolkit
+from camel.toolkits import FileWriteToolkit, HybridBrowserToolkit
from camel.types import ModelPlatformType, ModelType
logging.basicConfig(
@@ -61,29 +62,53 @@ custom_tools = [
"open_browser",
"close_browser",
"visit_page",
- "get_som_screenshot", # Add screenshot capability
"click",
"type",
+ "back",
+ "forward",
+ "switch_tab",
]
web_toolkit_custom = HybridBrowserToolkit(
- headless=False, user_data_dir=USER_DATA_DIR, enabled_tools=custom_tools
+ headless=False,
+ user_data_dir=USER_DATA_DIR,
+ enabled_tools=custom_tools,
+ browser_log_to_file=True, # generate detailed log file in ./browser_log
+ stealth=True, # Using stealth mode during browser operation
)
print(f"Custom tools: {web_toolkit_custom.enabled_tools}")
+output_dir = "./file_write_outputs"
+os.makedirs(output_dir, exist_ok=True)
+# Initialize the FileWriteToolkit with the output directory
+file_toolkit = FileWriteToolkit(output_dir=output_dir)
+
+# Get the tools from the toolkit
+tools_list = file_toolkit.get_tools()
# Use the custom toolkit for the actual task
agent = ChatAgent(
model=model_backend,
- tools=[*web_toolkit_custom.get_tools()],
+ tools=[*web_toolkit_custom.get_tools(), *file_toolkit.get_tools()],
max_iteration=10,
)
TASK_PROMPT = r"""
-Open Amazon.com, search for a beautiful keyboard,
-click it and add it to the cart
-and use get_som_screenshot to see the final
-picture of keyboard and describe its appearance
+Use Google Search to search for news in Munich today, and click on relevant
+websites to get the news and write it in markdown.
+
+I mean you need to browse multiple websites. After visiting each website,
+write the news in markdown, then return to the Google search results page
+and click on other websites.
+
+If you encounter "snapshot": "snapshot not changed" when clicking, it might
+be because you've clicked on plain text that doesn't lead to another page.
+Don't be discouraged—keep trying to click on different links (but do not
+click the same reference more than once). Note that in the Google search
+results, the text inside the heading tags is clickable and will lead to the
+result page.
+
+
"""
@@ -95,8 +120,17 @@ async def main() -> None:
print("\nResponse from agent:")
print(response.msgs[0].content if response.msgs else "<no response>")
+ # Ensure proper cleanup
try:
await web_toolkit_custom.close_browser()
+ # Close all browser sessions to ensure complete cleanup
+ from camel.toolkits.hybrid_browser_toolkit.browser_session import (
+ HybridBrowserSession,
+ )
+
+ await HybridBrowserSession.close_all_sessions()
+ # Give Playwright time to fully shut down subprocesses
+ await asyncio.sleep(0.5)
except Exception as err:
logging.warning(
"Failed to close " "browser session explicitly: %s", err
diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.py
index d1ea522bb..5a9cb8efa 100644
--- a/examples/workforce/eigent.py
+++ b/examples/workforce/eigent.py
@@ -13,6 +13,7 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import asyncio
+import uuid
from camel.agents.chat_agent import ChatAgent
from camel.logger import get_logger
@@ -114,7 +115,6 @@ def developer_agent_factory(
- Communicating with other agents using messaging tools. You can use
`list_available_agents` to see available team members and `send_message`
to coordinate with them for complex tasks requiring collaboration.
-
Remember to manage your terminal sessions. You can create new sessions
and run commands in them.
"""
@@ -137,24 +137,47 @@ def search_agent_factory(
r"""Factory for creating a search agent, based on user-provided code
structure.
"""
+ # Generate a unique identifier for this agent instance
+ agent_id = str(uuid.uuid4())[:8]
+
+ custom_tools = [
+ "open_browser",
+ "close_browser",
+ "click",
+ "type",
+ "back",
+ "forward",
+ "switch_tab",
+ "enter",
+ "get_som_screenshot",
+ ]
+ web_toolkit_custom = HybridBrowserToolkit(
+ headless=False,
+ enabled_tools=custom_tools,
+ browser_log_to_file=True,
+ stealth=True,
+ session_id=agent_id, # unique session ID
+ default_start_url="https://bing.com/",
+ )
+
tools = [
- *HybridBrowserToolkit(headless=False).get_tools(),
+ *web_toolkit_custom.get_tools(),
*TerminalToolkit().get_tools(),
send_message_to_user,
HumanToolkit().ask_human_via_console,
- NoteTakingToolkit().take_note,
+ NoteTakingToolkit().append_note,
*Crawl4AIToolkit().get_tools(),
]
system_message = """You are a helpful assistant that can search the web,
extract webpage content, simulate browser actions, and provide relevant
information to solve the given task.
-
+
You MUST use the `send_message_to_user` tool to inform the user of every
decision and action you take. Your message must include a short title
and a one-sentence description. This is a mandatory part of your
workflow.
-
+
### 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.
@@ -183,7 +206,7 @@ def search_agent_factory(
or document processing capabilities.
### Note Taking
- - As you find information, you MUST use the `take_note` tool to record
+ - As you find information, you MUST use the `append_note` tool to record
your findings in a structured way.
- Append new information to the notes. Do not overwrite the note file
unless you are summarizing or restructuring the content.
@@ -244,7 +267,7 @@ def document_agent_factory(
the `send_message_to_user` tool to inform the user of every decision and
action you take. Your message must include a short title and a
one-sentence description. This is a mandatory part of your workflow.
-
+
Your capabilities include:
1. Information Gathering:
@@ -334,7 +357,7 @@ def multi_modal_agent_factory(model: BaseModelBackend, task_id: str):
the `send_message_to_user` tool to inform the user of every decision and
action you take. Your message must include a short title and a
one-sentence description. This is a mandatory part of your workflow.
-
+
Your capabilities include:
1. Video & Audio Analysis:
@@ -475,7 +498,7 @@ async def main():
# Create a single model backend for all agents
model_backend = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
- model_type=ModelType.GPT_4_1_MINI,
+ model_type=ModelType.GPT_4_1,
# model_config_dict={
# "max_tokens": 32768,
# },
@@ -483,7 +506,7 @@ async def main():
model_backend_reason = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
- model_type=ModelType.GPT_4_1_MINI,
+ model_type=ModelType.GPT_4_1,
# model_config_dict={
# "max_tokens": 32768,
# },
diff --git a/pyproject.toml b/pyproject.toml
index c221a1666..7cfeb86b6 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "camel-ai"
-version = "0.2.71a5"
+version = "0.2.71a6"
description = "Communicative Agents for AI Society Study"
authors = [{ name = "CAMEL-AI.org" }]
requires-python = ">=3.10,<3.13"
diff --git a/test/toolkits/test_hybrid_browser_toolkit.py b/test/toolkits/test_hybrid_browser_toolkit.py
index 4f290e01e..64dc966d1 100644
--- a/test/toolkits/test_hybrid_browser_toolkit.py
+++ b/test/toolkits/test_hybrid_browser_toolkit.py
@@ -143,7 +143,7 @@ async def browser_toolkit_fixture():
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -219,7 +219,7 @@ def sync_browser_toolkit():
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -244,7 +244,7 @@ class TestHybridBrowserToolkit:
with (
patch(
'camel.toolkits.hybrid_browser_toolkit.browser_session'
- '.NVBrowserSession',
+ '.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -272,7 +272,7 @@ class TestHybridBrowserToolkit:
with (
patch(
'camel.toolkits.hybrid_browser_toolkit.browser_session'
- '.NVBrowserSession',
+ '.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -300,7 +300,7 @@ class TestHybridBrowserToolkit:
mock_session = AsyncMock()
with patch(
'camel.toolkits.hybrid_browser_toolkit.browser_session'
- '.NVBrowserSession',
+ '.HybridBrowserSession',
return_value=mock_session,
):
with patch('builtins.open', create=True) as mock_file:
@@ -319,7 +319,7 @@ class TestHybridBrowserToolkit:
mock_session = AsyncMock()
with patch(
'camel.toolkits.hybrid_browser_toolkit.browser_session'
- '.NVBrowserSession',
+ '.HybridBrowserSession',
return_value=mock_session,
):
with (
@@ -336,7 +336,7 @@ class TestHybridBrowserToolkit:
with (
patch(
'camel.toolkits.hybrid_browser_toolkit.browser_session'
- '.NVBrowserSession',
+ '.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -356,7 +356,7 @@ class TestHybridBrowserToolkit:
with (
patch(
'camel.toolkits.hybrid_browser_toolkit.browser_session'
- '.NVBrowserSession',
+ '.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -374,7 +374,7 @@ class TestHybridBrowserToolkit:
@pytest.mark.asyncio
async def test_open_browser_no_start_url(self):
- """Test opening browser without start URL."""
+ """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)
@@ -389,7 +389,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -410,8 +410,8 @@ class TestHybridBrowserToolkit:
assert "snapshot" in result
@pytest.mark.asyncio
- async def test_open_browser_with_start_url(self):
- """Test opening browser with start URL."""
+ 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)
@@ -426,7 +426,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -438,7 +438,10 @@ class TestHybridBrowserToolkit:
mock_open(read_data="console.log('mock script');"),
),
):
- toolkit = HybridBrowserToolkit(headless=True)
+ # Create toolkit with custom default URL
+ toolkit = HybridBrowserToolkit(
+ headless=True, default_start_url=TEST_URL
+ )
toolkit._session = mock_session
# Mock visit_page method
@@ -450,8 +453,9 @@ class TestHybridBrowserToolkit:
"snapshot": "test",
}
- await toolkit.open_browser(start_url=TEST_URL)
+ await toolkit.open_browser()
+ # Should call visit_page with the configured default URL
mock_visit.assert_called_once_with(TEST_URL)
@pytest.mark.asyncio
@@ -471,7 +475,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -507,7 +511,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -544,7 +548,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -582,7 +586,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -627,7 +631,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -690,7 +694,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -742,7 +746,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -785,7 +789,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -820,7 +824,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -859,7 +863,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -898,7 +902,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -935,7 +939,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -974,7 +978,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -1018,7 +1022,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -1053,7 +1057,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -1092,7 +1096,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
@@ -1124,17 +1128,20 @@ class TestHybridBrowserToolkit:
tools = toolkit.get_tools()
- # Default tools should be 6 tools (updated default configuration)
+ # Default tools should be 5 tools (updated default configuration)
+ assert len(tools) == 8
- assert len(tools) == 6
tool_names = [tool.func.__name__ for tool in tools]
expected_default_tools = [
- 'open_browser',
- 'close_browser',
- 'visit_page',
- 'click',
- 'type',
+ "open_browser",
+ "close_browser",
+ "visit_page",
+ "back",
+ "forward",
+ "click",
+ "type",
+ "switch_tab",
]
for expected_tool in expected_default_tools:
@@ -1152,7 +1159,7 @@ class TestHybridBrowserToolkit:
# Default tools should be 6, even with web_agent_model set
# unless solve_task is explicitly enabled
- assert len(tools) == 6
+ assert len(tools) == 8
tool_names = [tool.func.__name__ for tool in tools]
# Should not automatically include solve_task unless explicitly enabled
@@ -1164,7 +1171,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=AsyncMock(),
),
patch(
@@ -1193,16 +1200,19 @@ class TestHybridBrowserToolkit:
def test_get_tools_custom_selection(self):
"""Test getting tools with custom tool selection."""
custom_tools = [
- 'open_browser',
- 'visit_page',
- 'get_som_screenshot',
- 'click',
- 'scroll',
+ "open_browser",
+ "close_browser",
+ "visit_page",
+ "back",
+ "forward",
+ "click",
+ "type",
+ "switch_tab",
]
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=AsyncMock(),
),
patch(
@@ -1221,7 +1231,7 @@ class TestHybridBrowserToolkit:
tools = toolkit.get_tools()
tool_names = [tool.func.__name__ for tool in tools]
- assert len(tools) == 5
+ assert len(tools) == 8
for expected_tool in custom_tools:
assert expected_tool in tool_names
@@ -1327,7 +1337,7 @@ class TestHybridBrowserToolkit:
with (
patch(
- 'camel.toolkits.hybrid_browser_toolkit.browser_session.NVBrowserSession',
+ 'camel.toolkits.hybrid_browser_toolkit.browser_session.HybridBrowserSession',
return_value=mock_session,
),
patch(
diff --git a/test/toolkits/test_note_taking_toolkit.py b/test/toolkits/test_note_taking_toolkit.py
index c453be425..c001b8b36 100644
--- a/test/toolkits/test_note_taking_toolkit.py
+++ b/test/toolkits/test_note_taking_toolkit.py
@@ -24,17 +24,17 @@ def note_taking_toolkit():
return NoteTakingToolkit(note_file_path="test_notes.md")
-def test_take_note_append(note_taking_toolkit):
+def test_append_note(note_taking_toolkit):
# Test appending a note
note_file = Path("test_notes.md")
if note_file.exists():
note_file.unlink()
- result = note_taking_toolkit.take_note("First note.")
+ result = note_taking_toolkit.append_note("First note.")
assert "appended" in result
assert note_file.read_text() == "First note.\n"
- result = note_taking_toolkit.take_note("Second note.")
+ result = note_taking_toolkit.append_note("Second note.")
assert "appended" in result
assert note_file.read_text() == "First note.\nSecond note.\n"
@@ -43,22 +43,6 @@ def test_take_note_append(note_taking_toolkit):
note_file.unlink()
-def test_take_note_update(note_taking_toolkit):
- # Test updating a note
- note_file = Path("test_notes.md")
- if note_file.exists():
- note_file.unlink()
-
- note_taking_toolkit.take_note("Initial content.")
- result = note_taking_toolkit.take_note("Updated content.", update=True)
- assert "updated" in result
- assert note_file.read_text() == "Updated content.\n"
-
- # Clean up the test file
- if note_file.exists():
- note_file.unlink()
-
-
def test_read_note(note_taking_toolkit):
# Test reading a note
note_file = Path("test_notes.md")
@@ -68,7 +52,7 @@ def test_read_note(note_taking_toolkit):
# Test reading non-existent file
assert "does not exist" in note_taking_toolkit.read_note()
- note_taking_toolkit.take_note("Hello, world!")
+ note_taking_toolkit.append_note("Hello, world!")
assert note_taking_toolkit.read_note() == "Hello, world!\n"
# Clean up the test file
diff --git a/uv.lock b/uv.lock
index 2d6b22312..b5c9ec5a2 100644
--- a/uv.lock
+++ b/uv.lock
@@ -21,7 +21,7 @@ wheels = [
[[package]]
name = "aci-sdk"
-version = "1.0.0b2"
+version = "1.0.0b3"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "griffe" },
@@ -30,9 +30,9 @@ dependencies = [
{ name = "tenacity" },
{ name = "typing-extensions" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/68/57/bd2b54e6ee84db1606e9959cabdde5fc6dace3e34dd360a62663d353e868/aci_sdk-1.0.0b2.tar.gz", hash = "sha256:b5f6b97c65adbacdf835bbba9d691dd55ad4aeae941eae68d5fb05ea01da802a", size = 63622, upload-time = "2025-05-06T14:33:12.312Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/c1/d4/7b7e723e935f6bc0e6c9c6c274536a8aefdbce1c5ff88f0a7123715cf2d5/aci_sdk-1.0.0b3.tar.gz", hash = "sha256:f4ff452419c9baac3c86787f3ae85123712706668397fe1b7780ffb85b1b06b1", size = 64225, upload-time = "2025-07-05T22:53:32.213Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/5e/57/943ce077b823bce9a9d8f223cde70c12d9fbe923392f62d89cabea7cd7e1/aci_sdk-1.0.0b2-py3-none-any.whl", hash = "sha256:5ff09d074e1e146b536fe19334ea7ab03e62c75657ba0628bfb8778d376a7140", size = 31876, upload-time = "2025-05-06T14:33:10.871Z" },
+ { url = "https://files.pythonhosted.org/packages/4f/95/231ce4f25f78bf8f3e35d2d9e3603895e162104c33450a08b5912303f512/aci_sdk-1.0.0b3-py3-none-any.whl", hash = "sha256:828bd478a633dea0aa7ffe8857e5bc2339e1eb06b47155ed74372d7dbd46f24f", size = 32225, upload-time = "2025-07-05T22:53:30.895Z" },
]
[[package]]
@@ -647,7 +647,7 @@ wheels = [
[[package]]
name = "camel-ai"
-version = "0.2.71a5"
+version = "0.2.71a6"
source = { editable = "." }
dependencies = [
{ name = "colorama" },
@@ -7493,14 +7493,14 @@ wheels = [
[[package]]
name = "sse-starlette"
-version = "2.3.6"
+version = "2.4.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "anyio" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/8c/f4/989bc70cb8091eda43a9034ef969b25145291f3601703b82766e5172dfed/sse_starlette-2.3.6.tar.gz", hash = "sha256:0382336f7d4ec30160cf9ca0518962905e1b69b72d6c1c995131e0a703b436e3", size = 18284, upload-time = "2025-05-30T13:34:12.914Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/07/3e/eae74d8d33e3262bae0a7e023bb43d8bdd27980aa3557333f4632611151f/sse_starlette-2.4.1.tar.gz", hash = "sha256:7c8a800a1ca343e9165fc06bbda45c78e4c6166320707ae30b416c42da070926", size = 18635, upload-time = "2025-07-06T09:41:33.631Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/81/05/78850ac6e79af5b9508f8841b0f26aa9fd329a1ba00bf65453c2d312bcc8/sse_starlette-2.3.6-py3-none-any.whl", hash = "sha256:d49a8285b182f6e2228e2609c350398b2ca2c36216c2675d875f81e93548f760", size = 10606, upload-time = "2025-05-30T13:34:11.703Z" },
+ { url = "https://files.pythonhosted.org/packages/e4/f1/6c7eaa8187ba789a6dd6d74430307478d2a91c23a5452ab339b6fbe15a08/sse_starlette-2.4.1-py3-none-any.whl", hash = "sha256:08b77ea898ab1a13a428b2b6f73cfe6d0e607a7b4e15b9bb23e4a37b087fd39a", size = 10824, upload-time = "2025-07-06T09:41:32.321Z" },
]
[[package]]