🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
chore: move browser_toolkit_save_auth_cookie to utils (#2332)
Wendong-Fan9a7031a5e8f4
diff --git a/camel/utils/__init__.py b/camel/utils/__init__.pyindex 3800a6854..363ace51f 100644--- a/camel/utils/__init__.py+++ b/camel/utils/__init__.py@@ -17,6 +17,7 @@ from .commons import (BatchProcessor,agentops_decorator,api_keys_required,+ browser_toolkit_save_auth_cookie,check_server_running,create_chunks,dependencies_required,@@ -92,4 +93,5 @@ __all__ = ["with_timeout","MCPServer","sanitize_filename",+ "browser_toolkit_save_auth_cookie",]diff --git a/camel/utils/commons.py b/camel/utils/commons.pyindex 285d66785..6d7db4d3e 100644--- a/camel/utils/commons.py+++ b/camel/utils/commons.py@@ -1040,3 +1040,66 @@ def with_timeout(timeout=None):return decorator(func)return decorator+++def browser_toolkit_save_auth_cookie(+ cookie_json_path: str, url: str, wait_time: int = 60+):+ r"""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).+ wait_time (int): The time in seconds to wait for the user to manually+ authenticate.++ Usage:+ 1. The function opens a browser window and navigates to the specified+ URL+ 2. User manually logs in during the wait_time 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 wait_time sleep is intentional to give the user enough time to+ complete the manual authentication process before the storage state+ is captured.+ """+ from playwright.sync_api import sync_playwright++ 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+ storage_state = (+ cookie_json_path if os.path.exists(cookie_json_path) else None+ )++ # Create browser context with proper typing+ context = browser.new_context(+ accept_downloads=True, storage_state=storage_state+ )+ 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(wait_time) # 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 finisheddiff --git a/examples/toolkits/browser_toolkit_with_cookie.py b/examples/toolkits/browser_toolkit_with_cookie.pyindex fa11bbd01..b741137a3 100644--- a/examples/toolkits/browser_toolkit_with_cookie.py+++ b/examples/toolkits/browser_toolkit_with_cookie.py@@ -11,105 +11,18 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========--import asyncio-import gc-import os-import sys-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+from camel.utils import browser_toolkit_save_auth_cookie--def save_auth_cookie(cookie_json_path: str, url: str):- r"""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---def ensure_no_running_event_loops():- try:- loop = asyncio.get_event_loop()- if loop.is_running():- print("Detected running event loop, attempting to close it...")- loop.stop()- loop.close()- except RuntimeError:- # No running event loop- pass-- # Set new event loop policy to ensure clean state- if sys.platform == 'win32':- asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())- else:- asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())-- # Create a new event loop but don't start it- new_loop = asyncio.new_event_loop()- asyncio.set_event_loop(new_loop)-- # Force garbage collection to clean up any lingering references- gc.collect()---# Now we can safely proceed with synchronous code# NOTE: After saving the cookie, you need to comment out the following line# and rerun the script, otherwise you would get error due to asyncio event loop-save_auth_cookie(cookie_json_path="cookie.json", url="https://www.amazon.com/")+browser_toolkit_save_auth_cookie(+ cookie_json_path="cookie.json", url="https://www.amazon.com/", wait_time=10+)model = ModelFactory.create(model_platform=ModelPlatformType.OPENAI,