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

feat: Support persistent context and stealth mode (#2291)

Puzhen Zhange5a7d0a52839
diff --git a/camel/toolkits/async_browser_toolkit.py b/camel/toolkits/async_browser_toolkit.py
index 8a6cf29c8..0858b374e 100644
--- a/camel/toolkits/async_browser_toolkit.py
+++ b/camel/toolkits/async_browser_toolkit.py
@@ -123,6 +123,7 @@ class AsyncBaseBrowser:
cache_dir: Optional[str] = None,
channel: Literal["chrome", "msedge", "chromium"] = "chromium",
cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None,
):
r"""
Initialize the asynchronous browser core.
@@ -136,7 +137,11 @@ class AsyncBaseBrowser:
cookie_json_path (Optional[str]): Path to a JSON file containing
authentication cookies and browser storage state. If provided
and the file exists, the browser will load this state to
- maintain authenticated sessions without requiring manual login.
+ maintain authenticated sessions. This is primarily used when
+ `user_data_dir` is not set.
+ user_data_dir (Optional[str]): The directory to store user data
+ for persistent context. If None, a fresh browser instance
+ is used without saving data. (default: :obj:`None`)
Returns:
None
@@ -151,6 +156,7 @@ class AsyncBaseBrowser:
self.playwright = async_playwright()
self.page_history: list[Any] = []
self.cookie_json_path = cookie_json_path
+ self.user_data_dir = user_data_dir
self.playwright_server: Any = None
self.playwright_started: bool = False
self.browser: Any = None
@@ -163,6 +169,10 @@ class AsyncBaseBrowser:
self.cache_dir = "tmp/" if cache_dir is None else cache_dir
os.makedirs(self.cache_dir, exist_ok=True)
+ # Create user data directory only if specified
+ if self.user_data_dir:
+ os.makedirs(self.user_data_dir, exist_ok=True)
+
# Load the page script
abs_dir_path = os.path.dirname(os.path.abspath(__file__))
page_script_path = os.path.join(abs_dir_path, "page_script.js")
@@ -183,23 +193,56 @@ class AsyncBaseBrowser:
await self._ensure_browser_installed()
self.playwright_server = await self.playwright.start()
self.playwright_started = True
- # Launch the browser asynchronously.
- self.browser = await self.playwright_server.chromium.launch(
- headless=self.headless, channel=self.channel
+
+ browser_launch_args = [
+ "--disable-blink-features=AutomationControlled", # Basic stealth
+ ]
+
+ user_agent_string = (
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
+ "Chrome/91.0.4472.124 Safari/537.36"
)
- # Check if cookie file exists before using it to maintain
- # authenticated sessions. This prevents errors when the cookie file
- # doesn't exist
- if self.cookie_json_path and os.path.exists(self.cookie_json_path):
- self.context = await self.browser.new_context(
- accept_downloads=True, storage_state=self.cookie_json_path
+
+ if self.user_data_dir:
+ self.context = await (
+ self.playwright_server.chromium.launch_persistent_context(
+ user_data_dir=self.user_data_dir,
+ headless=self.headless,
+ channel=self.channel,
+ accept_downloads=True,
+ user_agent=user_agent_string,
+ java_script_enabled=True,
+ args=browser_launch_args,
+ )
)
+ self.browser = None # Not using a separate browser instance
+ if len(self.context.pages) > 0: # Persistent context might
+ # reopen pages
+ self.page = self.context.pages[0]
+ else:
+ self.page = await self.context.new_page()
else:
- self.context = await self.browser.new_context(
- accept_downloads=True,
+ # Launch a fresh browser instance
+ self.browser = await self.playwright_server.chromium.launch(
+ headless=self.headless,
+ channel=self.channel,
+ args=browser_launch_args,
)
- # Create a new page asynchronously.
- self.page = await self.context.new_page()
+
+ new_context_kwargs: Dict[str, Any] = {
+ "accept_downloads": True,
+ "user_agent": user_agent_string,
+ "java_script_enabled": True,
+ }
+ if self.cookie_json_path and os.path.exists(self.cookie_json_path):
+ new_context_kwargs["storage_state"] = self.cookie_json_path
+
+ self.context = await self.browser.new_context(**new_context_kwargs)
+ self.page = await self.context.new_page()
+
+ assert self.context is not None
+ assert self.page is not None
def init(self) -> Coroutine[Any, Any, None]:
r"""Initialize the browser asynchronously."""
@@ -827,7 +870,14 @@ class AsyncBaseBrowser:
async def async_close(self) -> None:
r"""Asynchronously close the browser."""
- await self.browser.close()
+ if self.context is not None:
+ await self.context.close()
+ if self.browser is not None: # Only close browser if it was
+ # launched separately
+ await self.browser.close()
+ if self.playwright_server and self.playwright_started:
+ await self.playwright_server.stop()
+ self.playwright_started = False
def close(self) -> Coroutine[Any, Any, None]:
r"""Close the browser."""
@@ -943,6 +993,7 @@ class AsyncBrowserToolkit(BaseToolkit):
planning_agent_model: Optional[BaseModelBackend] = None,
output_language: str = "en",
cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None,
):
r"""Initialize the BrowserToolkit instance.
@@ -966,6 +1017,8 @@ class AsyncBrowserToolkit(BaseToolkit):
maintain authenticated sessions without requiring manual
login.
(default: :obj:`None`)
+ user_data_dir (Optional[str]): The directory to store user data
+ for persistent context. (default: :obj:`"user_data_dir/"`)
"""
super().__init__()
self.browser = AsyncBaseBrowser(
@@ -973,6 +1026,7 @@ class AsyncBrowserToolkit(BaseToolkit):
cache_dir=cache_dir,
channel=channel,
cookie_json_path=cookie_json_path,
+ user_data_dir=user_data_dir,
)
self.history_window = history_window
@@ -991,7 +1045,7 @@ class AsyncBrowserToolkit(BaseToolkit):
os.makedirs(self.browser.cache_dir, exist_ok=True)
def _initialize_agent(self) -> Tuple["ChatAgent", "ChatAgent"]:
- r"""Initialize the agent."""
+ r"""Initialize the planning and web agents."""
from camel.agents.chat_agent import ChatAgent
if self.web_agent_model is None:
@@ -1060,7 +1114,7 @@ Here is a plan about how to solve the task step-by-step which you must follow:
)
# Reset the history message of web_agent.
self.web_agent.reset()
- resp = self.web_agent.step(message)
+ resp = await self.web_agent.astep(message)
resp_content = resp.msgs[0].content
@@ -1196,43 +1250,29 @@ Here is a plan about how to solve the task step-by-step which you must follow:
f"correct identifier.",
)
- def _get_final_answer(self, task_prompt: str) -> str:
- r"""Get the final answer based on the task prompt and current browser
- state. It is used when the agent thinks that the task can be completed
- without any further action, and answer can be directly found in the
- current viewport.
- """
-
- prompt = GET_FINAL_ANSWER_PROMPT_TEMPLATE.format(
- history=self.history, task_prompt=task_prompt
- )
-
- message = BaseMessage.make_user_message(
- role_name='user',
- content=prompt,
+ async def _async_get_final_answer(self, task_prompt: str) -> str:
+ r"""Generate the final answer based on the task prompt."""
+ final_answer_prompt = GET_FINAL_ANSWER_PROMPT_TEMPLATE.format(
+ task_prompt=task_prompt, history=self.history
)
+ response = await self.planning_agent.astep(final_answer_prompt)
+ if response.msgs is None or len(response.msgs) == 0:
+ raise RuntimeError("Got empty final answer from planning agent.")
+ return response.msgs[0].content
- resp = self.web_agent.step(message)
- return resp.msgs[0].content
-
- def _task_planning(self, task_prompt: str, start_url: str) -> str:
- r"""Plan the task based on the given task prompt."""
-
- # Here are the available browser functions we can
- # use: {AVAILABLE_ACTIONS_PROMPT}
-
+ async def _async_task_planning(
+ self, task_prompt: str, start_url: str
+ ) -> str:
+ r"""Generate a detailed plan for the given task."""
planning_prompt = TASK_PLANNING_PROMPT_TEMPLATE.format(
task_prompt=task_prompt, start_url=start_url
)
+ response = await self.planning_agent.astep(planning_prompt)
+ if response.msgs is None or len(response.msgs) == 0:
+ raise RuntimeError("Got empty plan from planning agent.")
+ return response.msgs[0].content
- message = BaseMessage.make_user_message(
- role_name='user', content=planning_prompt
- )
-
- resp = self.planning_agent.step(message)
- return resp.msgs[0].content
-
- def _task_replanning(
+ async def _async_task_replanning(
self, task_prompt: str, detailed_plan: str
) -> Tuple[bool, str]:
r"""Replan the task based on the given task prompt.
@@ -1252,12 +1292,11 @@ Here is a plan about how to solve the task step-by-step which you must follow:
replanning_prompt = TASK_REPLANNING_PROMPT_TEMPLATE.format(
task_prompt=task_prompt,
detailed_plan=detailed_plan,
- history_window=self.history_window,
history=self.history[-self.history_window :],
)
# Reset the history message of planning_agent.
self.planning_agent.reset()
- resp = self.planning_agent.step(replanning_prompt)
+ resp = await self.planning_agent.astep(replanning_prompt)
resp_dict = _parse_json_output(resp.msgs[0].content, logger)
if_need_replan = resp_dict.get("if_need_replan", False)
@@ -1287,7 +1326,7 @@ Here is a plan about how to solve the task step-by-step which you must follow:
self._reset()
task_completed = False
- detailed_plan = self._task_planning(task_prompt, start_url)
+ detailed_plan = await self._async_task_planning(task_prompt, start_url)
logger.debug(f"Detailed plan: {detailed_plan}")
await self.browser.async_init()
@@ -1331,7 +1370,11 @@ Here is a plan about how to solve the task step-by-step which you must follow:
self.history.append(trajectory_info)
# replan the task if necessary
- if_need_replan, replanned_schema = self._task_replanning(
+ (
+ if_need_replan,
+ replanned_schema,
+ # ruff: noqa: E501
+ ) = await self._async_task_replanning(
task_prompt, detailed_plan
)
if if_need_replan:
@@ -1343,11 +1386,11 @@ Here is a plan about how to solve the task step-by-step which you must follow:
The task is not completed within the round limit. Please check
the last round {self.history_window} information to see if
there is any useful information:
- <history>{self.history[-self.history_window :]}</history>
+ <history>{self.history[-self.history_window:]}</history>
"""
else:
- simulation_result = self._get_final_answer(task_prompt)
+ simulation_result = await self._async_get_final_answer(task_prompt)
await self.browser.close()
return simulation_result
diff --git a/camel/toolkits/browser_toolkit.py b/camel/toolkits/browser_toolkit.py
index 1f98ffc2d..a85156564 100644
--- a/camel/toolkits/browser_toolkit.py
+++ b/camel/toolkits/browser_toolkit.py
@@ -125,6 +125,7 @@ class BaseBrowser:
cache_dir: Optional[str] = None,
channel: Literal["chrome", "msedge", "chromium"] = "chromium",
cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None,
):
r"""Initialize the WebBrowser instance.
@@ -137,8 +138,11 @@ class BaseBrowser:
cookie_json_path (Optional[str]): Path to a JSON file containing
authentication cookies and browser storage state. If provided
and the file exists, the browser will load this state to
- maintain
- authenticated sessions without requiring manual login.
+ maintain authenticated sessions. This is primarily used when
+ `user_data_dir` is not set.
+ user_data_dir (Optional[str]): The directory to store user data
+ for persistent context. If None, a fresh browser instance
+ is used without saving data. (default: :obj:`None`)
Returns:
None
@@ -156,11 +160,16 @@ class BaseBrowser:
str
] = [] # stores the history of visited pages
self.cookie_json_path = cookie_json_path
+ self.user_data_dir = user_data_dir
# Set the cache directory
self.cache_dir = "tmp/" if cache_dir is None else cache_dir
os.makedirs(self.cache_dir, exist_ok=True)
+ # Create user data directory only if specified
+ if self.user_data_dir:
+ os.makedirs(self.user_data_dir, exist_ok=True)
+
# Load the page script
abs_dir_path = os.path.dirname(os.path.abspath(__file__))
page_script_path = os.path.join(abs_dir_path, "page_script.js")
@@ -183,27 +192,56 @@ class BaseBrowser:
def init(self) -> None:
r"""Initialize the browser."""
- # Launch the browser, if headless is False, the browser will display
assert self.playwright is not None
- self.browser = self.playwright.chromium.launch(
- headless=self.headless, channel=self.channel
+
+ browser_launch_args = [
+ "--disable-blink-features=AutomationControlled", # Basic stealth
+ ]
+
+ user_agent_string = (
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
+ "Chrome/91.0.4472.124 Safari/537.36"
)
- # Check if cookie file exists before using it to maintain
- # authenticated sessions. This prevents errors when the cookie file
- # doesn't exist
- assert self.browser is not None
- if self.cookie_json_path and os.path.exists(self.cookie_json_path):
- self.context = self.browser.new_context(
- accept_downloads=True, storage_state=self.cookie_json_path
+ if self.user_data_dir:
+ self.context = self.playwright.chromium.launch_persistent_context(
+ user_data_dir=self.user_data_dir,
+ headless=self.headless,
+ channel=self.channel,
+ accept_downloads=True,
+ user_agent=user_agent_string,
+ java_script_enabled=True,
+ args=browser_launch_args,
)
+ self.browser = None # Not using a separate browser instance
+ if (
+ len(self.context.pages) > 0
+ ): # Persistent context might reopen pages
+ self.page = self.context.pages[0]
+ else:
+ self.page = self.context.new_page()
else:
- self.context = self.browser.new_context(
- accept_downloads=True,
+ # Launch a fresh browser instance
+ self.browser = self.playwright.chromium.launch(
+ headless=self.headless,
+ channel=self.channel,
+ args=browser_launch_args,
)
- # Create a new page
+
+ new_context_kwargs: Dict[str, Any] = {
+ "accept_downloads": True,
+ "user_agent": user_agent_string,
+ "java_script_enabled": True,
+ }
+ if self.cookie_json_path and os.path.exists(self.cookie_json_path):
+ new_context_kwargs["storage_state"] = self.cookie_json_path
+
+ self.context = self.browser.new_context(**new_context_kwargs)
+ self.page = self.context.new_page()
+
assert self.context is not None
- self.page = self.context.new_page()
+ assert self.page is not None
def clean_cache(self) -> None:
r"""Delete the cache directory and its contents."""
@@ -690,8 +728,12 @@ class BaseBrowser:
self._wait_for_load()
def close(self):
- assert self.browser is not None
- self.browser.close()
+ if self.context is not None:
+ self.context.close()
+ if (
+ self.browser is not None
+ ): # Only close browser if it was launched separately
+ self.browser.close()
if self.playwright:
self.playwright.stop() # Stop playwright instance
@@ -781,6 +823,7 @@ class BrowserToolkit(BaseToolkit):
planning_agent_model: Optional[BaseModelBackend] = None,
output_language: str = "en",
cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None,
):
r"""Initialize the BrowserToolkit instance.
@@ -804,6 +847,9 @@ class BrowserToolkit(BaseToolkit):
maintain
authenticated sessions without requiring manual login.
(default: :obj:`None`)
+ user_data_dir (Optional[str]): The directory to store user data
+ for persistent context. If None, a fresh browser instance
+ is used without saving data. (default: :obj:`None`)
"""
super().__init__() # Call to super().__init__() added
self.browser = BaseBrowser(
@@ -811,6 +857,7 @@ class BrowserToolkit(BaseToolkit):
cache_dir=cache_dir,
channel=channel,
cookie_json_path=cookie_json_path,
+ user_data_dir=user_data_dir,
)
self.browser.web_agent_model = web_agent_model # Pass model to
# BaseBrowser instance
diff --git a/docs/mintlify/reference/camel.toolkits.async_browser_toolkit.mdx b/docs/mintlify/reference/camel.toolkits.async_browser_toolkit.mdx
index 3f5d71cdb..50c966fb1 100644
--- a/docs/mintlify/reference/camel.toolkits.async_browser_toolkit.mdx
+++ b/docs/mintlify/reference/camel.toolkits.async_browser_toolkit.mdx
@@ -37,7 +37,8 @@ def __init__(
headless = True,
cache_dir: Optional[str] = None,
channel: Literal['chrome', 'msedge', 'chromium'] = 'chromium',
- cookie_json_path: Optional[str] = None
+ cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None
):
```
@@ -48,7 +49,8 @@ Initialize the asynchronous browser core.
- **headless** (bool): Whether to run the browser in headless mode.
- **cache_dir** (Union[str, None]): The directory to store cache files.
- **channel** (`Literal["chrome", "msedge", "chromium"]`): The browser channel to use. Must be one of "chrome", "msedge", or "chromium".
-- **cookie_json_path** (Optional[str]): Path to a JSON file containing authentication cookies and browser storage state. If provided and the file exists, the browser will load this state to maintain authenticated sessions without requiring manual login.
+- **cookie_json_path** (Optional[str]): Path to a JSON file containing authentication cookies and browser storage state. If provided and the file exists, the browser will load this state to maintain authenticated sessions. This is primarily used when `user_data_dir` is not set.
+- **user_data_dir** (Optional[str]): The directory to store user data for persistent context. If None, a fresh browser instance is used without saving data. (default: :obj:`None`)
**Returns:**
@@ -406,7 +408,8 @@ def __init__(
web_agent_model: Optional[BaseModelBackend] = None,
planning_agent_model: Optional[BaseModelBackend] = None,
output_language: str = 'en',
- cookie_json_path: Optional[str] = None
+ cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None
):
```
@@ -422,6 +425,7 @@ Initialize the BrowserToolkit instance.
- **planning_agent_model** (Optional[BaseModelBackend]): The model backend for the planning agent.
- **output_language** (str): The language to use for output. (default: :obj:`"en`")
- **cookie_json_path** (Optional[str]): Path to a JSON file containing authentication cookies and browser storage state. If provided and the file exists, the browser will load this state to maintain authenticated sessions without requiring manual login. (default: :obj:`None`)
+- **user_data_dir** (Optional[str]): The directory to store user data for persistent context. (default: :obj:`"user_data_dir/"`)
<a id="camel.toolkits.async_browser_toolkit.AsyncBrowserToolkit._reset"></a>
diff --git a/docs/mintlify/reference/camel.toolkits.browser_toolkit.mdx b/docs/mintlify/reference/camel.toolkits.browser_toolkit.mdx
index f75b54c42..d5cddef5f 100644
--- a/docs/mintlify/reference/camel.toolkits.browser_toolkit.mdx
+++ b/docs/mintlify/reference/camel.toolkits.browser_toolkit.mdx
@@ -48,7 +48,8 @@ def __init__(
headless = True,
cache_dir: Optional[str] = None,
channel: Literal['chrome', 'msedge', 'chromium'] = 'chromium',
- cookie_json_path: Optional[str] = None
+ cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None
):
```
@@ -59,7 +60,8 @@ Initialize the WebBrowser instance.
- **headless** (bool): Whether to run the browser in headless mode.
- **cache_dir** (Union[str, None]): The directory to store cache files.
- **channel** (`Literal["chrome", "msedge", "chromium"]`): The browser channel to use. Must be one of "chrome", "msedge", or "chromium".
-- **cookie_json_path** (Optional[str]): Path to a JSON file containing authentication cookies and browser storage state. If provided and the file exists, the browser will load this state to maintain authenticated sessions without requiring manual login.
+- **cookie_json_path** (Optional[str]): Path to a JSON file containing authentication cookies and browser storage state. If provided and the file exists, the browser will load this state to maintain authenticated sessions. This is primarily used when `user_data_dir` is not set.
+- **user_data_dir** (Optional[str]): The directory to store user data for persistent context. If None, a fresh browser instance is used without saving data. (default: :obj:`None`)
**Returns:**
@@ -425,7 +427,8 @@ def __init__(
web_agent_model: Optional[BaseModelBackend] = None,
planning_agent_model: Optional[BaseModelBackend] = None,
output_language: str = 'en',
- cookie_json_path: Optional[str] = None
+ cookie_json_path: Optional[str] = None,
+ user_data_dir: Optional[str] = None
):
```
@@ -441,6 +444,7 @@ Initialize the BrowserToolkit instance.
- **planning_agent_model** (Optional[BaseModelBackend]): The model backend for the planning agent.
- **output_language** (str): The language to use for output. (default: :obj:`"en`")
- **cookie_json_path** (Optional[str]): Path to a JSON file containing authentication cookies and browser storage state. If provided and the file exists, the browser will load this state to maintain authenticated sessions without requiring manual login. (default: :obj:`None`)
+- **user_data_dir** (Optional[str]): The directory to store user data for persistent context. If None, a fresh browser instance is used without saving data. (default: :obj:`None`)
<a id="camel.toolkits.browser_toolkit.BrowserToolkit._reset"></a>
diff --git a/examples/toolkits/browser_toolkit_with_user_data_dir.py b/examples/toolkits/browser_toolkit_with_user_data_dir.py
new file mode 100644
index 000000000..c6b02c491
--- /dev/null
+++ b/examples/toolkits/browser_toolkit_with_user_data_dir.py
@@ -0,0 +1,112 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+from camel.agents import ChatAgent
+from camel.configs import ChatGPTConfig
+from camel.models import ModelFactory
+from camel.toolkits import BrowserToolkit
+from camel.types import ModelPlatformType, ModelType
+
+# This example demonstrates using BrowserToolkit with a persistent user data
+# directory.
+# The browser will store data (cookies, local storage, etc.) in this directory,
+# allowing sessions to persist across runs.
+
+# NOTE:
+# 1. If you run this script for the first time, a directory named
+# "my_browser_profile"
+# (or whatever you set user_data_dir to) will be created.
+# 2. Since headless=False, a browser window will open. If the task involves
+# a website
+# that requires login (e.g., Amazon for personalized content or purchase
+# history),
+# you may need to manually log in through the browser window during the
+# first run.
+# 3. On subsequent runs, the browser will use the data stored in
+# "my_browser_profile",
+# potentially remembering your login session, preferences, etc.
+
+USER_DATA_DIR = "my_browser_profile" # Directory to store persistent
+# browser data
+
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4O_MINI,
+ model_config_dict=ChatGPTConfig(
+ temperature=0.0,
+ ).as_dict(),
+)
+
+web_agent_model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4O_MINI,
+ model_config_dict=ChatGPTConfig(
+ temperature=0.0,
+ ).as_dict(),
+)
+
+planning_agent_model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4O_MINI,
+ model_config_dict=ChatGPTConfig(
+ temperature=0.0,
+ ).as_dict(),
+)
+
+# Create the BrowserToolkit with a user_data_dir for persistence
+web_toolkit = BrowserToolkit(
+ headless=False, # Set to False to see the browser and interact if needed
+ web_agent_model=web_agent_model,
+ planning_agent_model=planning_agent_model,
+ channel="chromium",
+ user_data_dir=USER_DATA_DIR,
+ # Specify the user data directory. If set to None, a fresh browser
+ # profile will be launched (no cache, no history, no logged-in sessions,
+ # etc.).
+)
+
+agent = ChatAgent(
+ system_message="You are a helpful assistant.",
+ model=model,
+ tools=[*web_toolkit.get_tools()],
+)
+
+# Example task:
+# If you manually log into Amazon in the browser window that opens,
+# subsequent runs might show personalized content or keep you logged in.
+response = agent.step(
+ "Navigate to Amazon.com and identify the current #1 best-selling product"
+ " in the 'Home & Kitchen' category. Please provide the product name, "
+ "price,"
+ " and rating if available.",
+)
+
+print(
+ "Task: Navigate to Amazon.com and find the #1 best-seller in 'Home & "
+ "Kitchen'."
+)
+print(f"Using user data directory: {USER_DATA_DIR}")
+print("\nResponse from agent:")
+print(response.msgs[0].content)
+
+# Example of expected output structure (actual product will vary):
+# """
+# ==========================================================================
+# The current #1 best-selling product in the Home & Kitchen category on
+# Amazon is
+# the **XYZ Smart Coffee Maker**.
+#
+# - **Price:** $79.99
+# - **Rating:** 4.6 stars based on 12,345 ratings.
+# ==========================================================================
+# """
diff --git a/test/toolkits/test_async_browser_toolkit.py b/test/toolkits/test_async_browser_toolkit.py
index 7296f1f13..d7ce5d3b3 100644
--- a/test/toolkits/test_async_browser_toolkit.py
+++ b/test/toolkits/test_async_browser_toolkit.py
@@ -13,12 +13,15 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import io
import os
+import shutil
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import pytest_asyncio
from PIL import Image
+from camel.messages import BaseMessage
+from camel.responses import ChatAgentResponse
from camel.toolkits.async_browser_toolkit import (
AsyncBaseBrowser,
AsyncBrowserToolkit,
@@ -27,20 +30,43 @@ from camel.toolkits.async_browser_toolkit import (
TEST_URL = "https://example.com"
-def dummy_playwright_setup():
- dummy_instance = MagicMock()
+class DummyMessage:
+ def __init__(self, content):
+ self.content = content
+
+
+class DummyResp:
+ def __init__(self, value):
+ self._value = value
+
+ def __str__(self):
+ return self._value
+
+ @property
+ def text(self):
+ return self._value
+
+@pytest_asyncio.fixture
+async def dummy_playwright_setup():
+ dummy_instance = MagicMock()
dummy_browser = MagicMock()
+ dummy_context = MagicMock()
+ dummy_page = MagicMock()
+
dummy_instance.chromium = MagicMock()
dummy_instance.chromium.launch = AsyncMock(return_value=dummy_browser)
+ dummy_instance.chromium.launch_persistent_context = AsyncMock(
+ return_value=dummy_context
+ )
+ dummy_instance.stop = AsyncMock()
- dummy_context = MagicMock()
dummy_browser.new_context = AsyncMock(return_value=dummy_context)
dummy_browser.close = AsyncMock()
- dummy_page = MagicMock()
dummy_context.new_page = AsyncMock(return_value=dummy_page)
+ dummy_context.pages = []
+ dummy_context.close = AsyncMock()
dummy_playwright = MagicMock()
dummy_playwright.start = AsyncMock(return_value=dummy_instance)
@@ -49,34 +75,32 @@ def dummy_playwright_setup():
@pytest_asyncio.fixture(scope="function")
-async def async_base_browser_fixture(dummy_playwright_setup):
+async def async_base_browser_fixture(tmp_path, dummy_playwright_setup):
with patch(
'playwright.async_api.async_playwright',
return_value=dummy_playwright_setup,
):
- browser = AsyncBaseBrowser(headless=True, cache_dir="test_cache")
+ browser = AsyncBaseBrowser(headless=True, cache_dir=str(tmp_path))
yield browser
- if os.path.exists("test_cache"):
- os.rmdir("test_cache")
+ shutil.rmtree(str(tmp_path), ignore_errors=True)
@pytest_asyncio.fixture(scope="function")
-async def async_browser_toolkit_fixture(dummy_playwright_setup):
+async def async_browser_toolkit_fixture(tmp_path, dummy_playwright_setup):
with patch(
'playwright.async_api.async_playwright',
return_value=dummy_playwright_setup,
):
- toolkit = AsyncBrowserToolkit(headless=True, cache_dir="test_cache")
+ toolkit = AsyncBrowserToolkit(headless=True, cache_dir=str(tmp_path))
yield toolkit
- if os.path.exists("test_cache"):
- os.rmdir("test_cache")
+ shutil.rmtree(str(tmp_path), ignore_errors=True)
@pytest.mark.asyncio
async def test_async_base_browser_init(async_base_browser_fixture):
browser = async_base_browser_fixture
assert browser.headless is True
- assert browser.cache_dir == "test_cache"
+ assert isinstance(browser.cache_dir, str)
assert isinstance(browser.history, list)
assert isinstance(browser.page_history, list)
@@ -86,25 +110,15 @@ async def test_async_base_browser_initialization_order_async(
async_base_browser_fixture,
):
browser = async_base_browser_fixture
-
assert browser.browser is None
assert browser.page is None
-
await browser.async_init()
-
assert browser.browser is not None
assert browser.page is not None
@pytest.mark.asyncio
- "channel",
- [
- "chrome",
- "msedge",
- "chromium",
- ],
-)
[email protected]("channel", ["chrome", "msedge", "chromium"])
async def test_async_browser_channel_selection(
channel, dummy_playwright_setup
):
@@ -120,13 +134,9 @@ async def test_async_browser_channel_selection(
async def test_async_browser_visit_page(async_base_browser_fixture):
browser = async_base_browser_fixture
await browser.async_init()
-
- browser_page = MagicMock()
- browser.page.goto = AsyncMock(return_value=browser_page)
+ browser.page.goto = AsyncMock()
browser.page.wait_for_load_state = AsyncMock()
-
await browser.visit_page(TEST_URL)
-
browser.page.goto.assert_called_once_with(TEST_URL)
browser.page.wait_for_load_state.assert_called_once()
@@ -135,15 +145,11 @@ async def test_async_browser_visit_page(async_base_browser_fixture):
async def test_async_browser_get_screenshot(async_base_browser_fixture):
browser = async_base_browser_fixture
await browser.async_init()
-
- # Create a dummy image and its byte representation
mock_image = Image.new('RGB', (100, 100))
img_byte_arr = io.BytesIO()
mock_image.save(img_byte_arr, format='PNG')
img_bytes = img_byte_arr.getvalue()
-
browser.page.screenshot = AsyncMock(return_value=img_bytes)
-
image, file_path = await browser.async_get_screenshot(save_image=False)
assert isinstance(image, Image.Image)
assert file_path is None
@@ -152,24 +158,36 @@ async def test_async_browser_get_screenshot(async_base_browser_fixture):
@pytest.mark.asyncio
async def test_async_browser_toolkit_browse_url(async_browser_toolkit_fixture):
toolkit = async_browser_toolkit_fixture
-
- class DummyMessage:
- def __init__(self, content):
- self.content = content
-
- class DummyResp:
- def __init__(self, content):
- self.msgs = [DummyMessage(content)]
-
- toolkit.planning_agent.step = MagicMock(
- return_value=DummyResp("1. Restate the task\n2. Make a plan")
+ mock_response_plan = ChatAgentResponse(
+ msgs=[
+ BaseMessage(
+ role_name="assistant",
+ role_type="assistant",
+ meta_dict=None,
+ content="1. Restate the task\n2. Make a plan",
+ )
+ ],
+ terminated=False,
+ info={},
+ )
+ mock_response_final = ChatAgentResponse(
+ msgs=[
+ BaseMessage(
+ role_name="assistant",
+ role_type="assistant",
+ meta_dict=None,
+ content="Task completed",
+ )
+ ],
+ terminated=False,
+ info={},
+ )
+ toolkit.planning_agent.astep = AsyncMock(
+ side_effect=[mock_response_plan, mock_response_final]
)
-
toolkit.browser.visit_page = AsyncMock()
toolkit.async_observe = AsyncMock(return_value=("obs", "reason", "stop()"))
- toolkit._get_final_answer = MagicMock(return_value="Task completed")
- # 4) 调用并断言
result = await toolkit.browse_url(
task_prompt="Test task", start_url=TEST_URL, round_limit=1
)
@@ -180,12 +198,10 @@ async def test_async_browser_toolkit_browse_url(async_browser_toolkit_fixture):
@pytest.mark.asyncio
async def test_async_browser_clean_cache(async_base_browser_fixture):
browser = async_base_browser_fixture
-
os.makedirs(browser.cache_dir, exist_ok=True)
test_file = os.path.join(browser.cache_dir, "test.txt")
with open(test_file, "w") as f:
f.write("test")
-
browser.clean_cache()
assert not os.path.exists(browser.cache_dir)
@@ -194,12 +210,9 @@ async def test_async_browser_clean_cache(async_base_browser_fixture):
async def test_async_browser_click_blank_area(async_base_browser_fixture):
browser = async_base_browser_fixture
await browser.async_init()
-
browser.page.mouse.click = AsyncMock()
browser.page.wait_for_load_state = AsyncMock()
-
await browser.click_blank_area()
-
browser.page.mouse.click.assert_called_once_with(0, 0)
browser.page.wait_for_load_state.assert_called_once()
@@ -208,7 +221,6 @@ async def test_async_browser_click_blank_area(async_base_browser_fixture):
async def test_async_browser_get_url(async_base_browser_fixture):
browser = async_base_browser_fixture
await browser.async_init()
-
browser.page.url = TEST_URL
assert browser.get_url() == TEST_URL
@@ -217,11 +229,8 @@ async def test_async_browser_get_url(async_base_browser_fixture):
async def test_async_browser_back_navigation(async_base_browser_fixture):
browser = async_base_browser_fixture
await browser.async_init()
-
browser.page.go_back = AsyncMock()
browser.page.wait_for_load_state = AsyncMock()
-
await browser.back()
-
browser.page.go_back.assert_called_once()
browser.page.wait_for_load_state.assert_called_once()
diff --git a/test/toolkits/test_browser_toolkit.py b/test/toolkits/test_browser_toolkit.py
index 293c9f27e..b564b7aca 100644
--- a/test/toolkits/test_browser_toolkit.py
+++ b/test/toolkits/test_browser_toolkit.py
@@ -14,6 +14,7 @@
import io
import os
+import shutil
from unittest.mock import MagicMock, patch
import pytest
@@ -37,7 +38,7 @@ def base_browser_fixture():
yield browser
# Cleanup
if os.path.exists("test_cache"):
- os.rmdir("test_cache")
+ shutil.rmtree("test_cache", ignore_errors=True)
@pytest.fixture(scope="function")
@@ -47,7 +48,7 @@ def browser_toolkit_fixture():
yield toolkit
# Cleanup
if os.path.exists("test_cache"):
- os.rmdir("test_cache")
+ shutil.rmtree("test_cache", ignore_errors=True)
def test_base_browser_init(base_browser_fixture):
@@ -56,6 +57,31 @@ def test_base_browser_init(base_browser_fixture):
assert browser.cache_dir == "test_cache"
assert isinstance(browser.history, list)
assert isinstance(browser.page_history, list)
+ assert (
+ browser.user_data_dir is None
+ ) # Added assertion for default user_data_dir
+
+
+def test_base_browser_init_with_user_data_dir():
+ test_user_dir = "test_user_data_dir_sync"
+ try:
+ with patch('playwright.sync_api.sync_playwright'):
+ # Mock _ensure_browser_installed to prevent
+ # actual browser checks during this specific init test
+ with patch.object(
+ BaseBrowser, '_ensure_browser_installed', MagicMock()
+ ) as mock_ensure_installed:
+ browser = BaseBrowser(
+ headless=True, user_data_dir=test_user_dir
+ )
+ mock_ensure_installed.assert_called_once()
+ assert browser.user_data_dir == test_user_dir
+ assert os.path.exists(
+ test_user_dir
+ ) # Check if __init__ created it
+ finally:
+ if os.path.exists(test_user_dir):
+ shutil.rmtree(test_user_dir)
def test_base_browser_initialization_order():
@@ -65,10 +91,74 @@ def test_base_browser_initialization_order():
assert browser.page is None # page not created yet
browser.init() # explicit init() call
+ # This assertion depends on user_data_dir being None. If it were set,
+ # browser.browser would be None. For default (None), it's not None.
assert browser.browser is not None # browser launched
assert browser.page is not None # page created
+def test_base_browser_initialization_order_with_user_data_dir():
+ test_init_dir = "test_init_user_data_sync"
+
+ with (
+ patch(
+ 'playwright.sync_api.sync_playwright'
+ ) as mock_sync_playwright_entry,
+ patch.object(
+ BaseBrowser, '_ensure_browser_installed', MagicMock()
+ ) as mock_ensure_installed_in_init,
+ ):
+ # Configure the mock for sync_playwright().start()
+ mock_playwright_api = mock_sync_playwright_entry.return_value
+ mock_playwright_started_instance = (
+ mock_playwright_api.start.return_value
+ )
+
+ # Configure the mock for launch_persistent_context
+ mock_context = MagicMock(name="PersistentContextMock")
+ mock_page = MagicMock(name="PageMock")
+ # ruff: noqa: E501
+ mock_playwright_started_instance.chromium.launch_persistent_context.return_value = mock_context
+ mock_context.pages = [] # Simulate no pre-existing pages initially
+ mock_context.new_page.return_value = mock_page
+
+ try:
+ browser = BaseBrowser(headless=True, user_data_dir=test_init_dir)
+ # Assertions for __init__ behaviour
+ mock_ensure_installed_in_init.assert_called_once()
+ assert os.path.exists(
+ test_init_dir
+ ) # __init__ should have created it
+ assert browser.browser is None # Before init
+ assert browser.page is None # Before init
+ assert browser.context is None # Before init
+
+ browser.init()
+ # Action: This calls
+ # self.playwright.chromium.launch_persistent_context
+
+ # Assertions for init() behaviour with user_data_dir
+ # ruff: noqa: E501
+ mock_playwright_started_instance.chromium.launch_persistent_context.assert_called_once()
+ # ruff: noqa: E501
+ call_kwargs = mock_playwright_started_instance.chromium.launch_persistent_context.call_args.kwargs
+ assert call_kwargs['user_data_dir'] == test_init_dir
+ assert call_kwargs['headless'] is True
+ assert call_kwargs['channel'] == "chromium" # Default channel
+ assert call_kwargs['accept_downloads'] is True
+ assert isinstance(call_kwargs['user_agent'], str)
+ assert call_kwargs['java_script_enabled'] is True
+ assert isinstance(call_kwargs['args'], list)
+
+ assert browser.browser is None # Key assertion: No separate
+ # browser object with persistent context
+ assert browser.context == mock_context
+ assert browser.page == mock_page
+ finally:
+ if os.path.exists(test_init_dir):
+ shutil.rmtree(test_init_dir)
+
+
@pytest.mark.parametrize(
"channel",
[