🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Cache capability for browser toolkit (#2185)
sunchengxuanivyc998ea39042c
diff --git a/camel/toolkits/browser_toolkit.py b/camel/toolkits/browser_toolkit.pyindex 1aa7d034c..d88168ebb 100644--- a/camel/toolkits/browser_toolkit.py+++ b/camel/toolkits/browser_toolkit.py@@ -141,7 +141,7 @@ def _get_str(d: Any, k: str) -> str:if isinstance(val, str):return valraise TypeError(- f"Expected a string for key '{k}', " f"but got {type(val).__name__}"+ f"Expected a string for key '{k}', but got {type(val).__name__}")@@ -162,7 +162,7 @@ def _get_bool(d: Any, k: str) -> bool:if isinstance(val, bool):return valraise TypeError(- f"Expected a boolean for key '{k}', " f"but got {type(val).__name__}"+ f"Expected a boolean for key '{k}', but got {type(val).__name__}")@@ -436,6 +436,7 @@ class BaseBrowser:headless=True,cache_dir: Optional[str] = None,channel: Literal["chrome", "msedge", "chromium"] = "chromium",+ cookie_json_path: Optional[str] = None,):r"""Initialize the WebBrowser instance.@@ -445,6 +446,10 @@ class BaseBrowser:channel (Literal["chrome", "msedge", "chromium"]): The browserchannel 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.Returns:None@@ -459,6 +464,7 @@ class BaseBrowser:self._ensure_browser_installed()self.playwright = sync_playwright().start()self.page_history: list = [] # stores the history of visited pages+ self.cookie_json_path = cookie_json_path# Set the cache directoryself.cache_dir = "tmp/" if cache_dir is None else cache_dir@@ -483,8 +489,18 @@ class BaseBrowser:self.browser = self.playwright.chromium.launch(headless=self.headless, channel=self.channel)- # Create a new context- self.context = self.browser.new_context(accept_downloads=True)++ # 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 = self.browser.new_context(+ accept_downloads=True, storage_state=self.cookie_json_path+ )+ else:+ self.context = self.browser.new_context(+ accept_downloads=True,+ )# Create a new pageself.page = self.context.new_page()@@ -993,6 +1009,7 @@ class BrowserToolkit(BaseToolkit):web_agent_model: Optional[BaseModelBackend] = None,planning_agent_model: Optional[BaseModelBackend] = None,output_language: str = "en",+ cookie_json_path: Optional[str] = None,):r"""Initialize the BrowserToolkit instance.@@ -1010,10 +1027,18 @@ class BrowserToolkit(BaseToolkit):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`)"""self.browser = BaseBrowser(- headless=headless, cache_dir=cache_dir, channel=channel+ headless=headless,+ cache_dir=cache_dir,+ channel=channel,+ cookie_json_path=cookie_json_path,)self.history_window = history_window@@ -1105,7 +1130,7 @@ Here are the current available browser functions you can use:Here are the latest {self.history_window} trajectory (at most) you have taken:<history>-{self.history[-self.history_window:]}+{self.history[-self.history_window :]}</history>Your output should be in json format, including the following fields:@@ -1368,7 +1393,7 @@ In order to solve the task, we made a detailed plan previously. Here is the deta<detailed plan>{detailed_plan}</detailed plan>According to the task above, we have made a series of observations, reasonings, and actions. Here are the latest {self.history_window} trajectory (at most) we have taken:-<history>{self.history[-self.history_window:]}</history>+<history>{self.history[-self.history_window :]}</history>However, the task is not completed yet. As the task is partially observable, we may need to replan the task based on the current state of the browser if necessary.Now please carefully examine the current task planning schema, and our history actions, and then judge whether the task needs to be fundamentally replanned. If so, please provide a detailed replanned schema (including the restated overall task).@@ -1464,7 +1489,7 @@ Your output should be in json format, including the following fields:if not task_completed:simulation_result = f"""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:diff --git a/examples/toolkits/browser_toolkit.py b/examples/toolkits/browser_toolkit.pyindex 0b81626e8..c566f1cf7 100644--- a/examples/toolkits/browser_toolkit.py+++ b/examples/toolkits/browser_toolkit.py@@ -12,12 +12,74 @@# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import os+import time++from playwright.sync_api import sync_playwright+from camel.agents import ChatAgentfrom camel.configs import ChatGPTConfigfrom camel.models import ModelFactoryfrom camel.toolkits import BrowserToolkitfrom camel.types import ModelPlatformType, ModelType++def save_auth_cookie(cookie_json_path: str, url: str):+ """+ Saves authentication cookies and browser storage state to a JSON file.++ This function launches a browser window and navigates to the specified URL,+ allowing the user to manually authenticate (log in) during a 60-second+ wait period.After authentication, it saves all cookies, localStorage, and+ sessionStorage data to the specified JSON file path, which can be used+ later to maintain authenticated sessions without requiring manual login.++ Args:+ cookie_json_path (str): Path where the authentication cookies and+ storage state will be saved as a JSON file.+ If the file already exists, it will be loaded+ first and then overwritten with updated state.+ The function checks if this file exists before+ attempting to use it.+ url (str): The URL to navigate to for authentication+ (e.g., a login page).++ Usage:+ 1. The function opens a browser window and navigates to the+ specified URL+ 2. User manually logs in during the 60-second wait period+ 3. Browser storage state (including auth cookies) is saved to the+ specified file+ 4. The saved state can be used in subsequent browser sessions to+ maintain authentication++ Note:+ The 60-second sleep is intentional to give the user enough time to+ complete the manual authentication process before the storage state+ is captured.+ """+ playwright = sync_playwright().start()++ # Launch visible browser window using Chromium+ browser = playwright.chromium.launch(headless=False, channel="chromium")++ # Check if cookie file exists before using it+ context_params = {"accept_downloads": True}+ if os.path.exists(cookie_json_path):+ context_params["storage_state"] = cookie_json_path++ context = browser.new_context(**context_params)+ page = context.new_page()+ page.goto(url) # Navigate to the authentication URL+ # Wait for page to fully load+ page.wait_for_load_state("load", timeout=1000)+ time.sleep(60) # Wait 60 seconds for user to manually authenticate+ # Save browser storage state (cookies, localStorage, etc.) to JSON file+ context.storage_state(path=cookie_json_path)++ browser.close() # Close the browser when finished++model = ModelFactory.create(model_platform=ModelPlatformType.OPENAI,model_type=ModelType.GPT_4O_MINI,@@ -42,6 +104,19 @@ planning_agent_model = ModelFactory.create().as_dict(),)+# Example of saving authentication cookies for Amazon+# Uncomment to run the authentication process+# save_auth_cookie(cookie_json_path="cookie.json", url="https://www.amazon.com/")++# example to let toolkit pickup the cookie.+# web_toolkit = BrowserToolkit(+# headless=False,+# web_agent_model=web_agent_model,+# planning_agent_model=planning_agent_model,+# channel="chromium",+# cookie_json_path="cookie.json"+# )+web_toolkit = BrowserToolkit(headless=False,web_agent_model=web_agent_model,