From f66e5dc1a27a81860af552257434056f8d1a10c3 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 15 May 2025 23:55:35 +0800 Subject: [PATCH] upgrade to bu==0.1.47 --- Dockerfile | 19 +++++------ README.md | 6 ++-- docker-compose.yml | 2 +- requirements.txt | 2 +- src/agent/browser_use/browser_use_agent.py | 16 --------- .../deep_research/deep_research_agent.py | 6 +++- src/browser/custom_browser.py | 28 +++++++-------- src/browser/custom_context.py | 4 +-- src/webui/components/browser_use_agent_tab.py | 6 +++- tests/test_agents.py | 34 +++++++++++-------- tests/test_playwright.py | 2 +- 11 files changed, 61 insertions(+), 64 deletions(-) diff --git a/Dockerfile b/Dockerfile index 19c4b94..b412cd8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -67,24 +67,23 @@ WORKDIR /app # Copy requirements and install Python dependencies COPY requirements.txt . -# Ensure 'patchright' is in your requirements.txt or install it directly -# RUN pip install --no-cache-dir -r requirements.txt patchright # If not in requirements + RUN pip install --no-cache-dir -r requirements.txt -# Install Patchright browsers and dependencies -# Patchright documentation suggests PLAYWRIGHT_BROWSERS_PATH is still relevant -# or that Patchright installs to a similar default location that Playwright would. -# Let's assume Patchright respects PLAYWRIGHT_BROWSERS_PATH or its default install location is findable. +# Install playwright browsers and dependencies +# playwright documentation suggests PLAYWRIGHT_BROWSERS_PATH is still relevant +# or that playwright installs to a similar default location that Playwright would. +# Let's assume playwright respects PLAYWRIGHT_BROWSERS_PATH or its default install location is findable. ENV PLAYWRIGHT_BROWSERS_PATH=/ms-browsers RUN mkdir -p $PLAYWRIGHT_BROWSERS_PATH # Install recommended: Google Chrome (instead of just Chromium for better undetectability) -# The 'patchright install chrome' command might download and place it. -# The '--with-deps' equivalent for patchright install is to run 'patchright install-deps chrome' after. -# RUN patchright install chrome --with-deps +# The 'playwright install chrome' command might download and place it. +# The '--with-deps' equivalent for playwright install is to run 'playwright install-deps chrome' after. +# RUN playwright install chrome --with-deps # Alternative: Install Chromium if Google Chrome is problematic in certain environments -RUN patchright install chromium --with-deps +RUN playwright install chromium --with-deps # Copy the application code diff --git a/README.md b/README.md index b67a2ed..e5a24ea 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,13 @@ Install Python packages: uv pip install -r requirements.txt ``` -Install Browsers in Patchright. +Install Browsers in playwright. ```bash -patchright install --with-deps +playwright install --with-deps ``` Or you can install specific browsers by running: ```bash -patchright install chromium --with-deps +playwright install chromium --with-deps ``` #### Step 4: Configure Environment diff --git a/docker-compose.yml b/docker-compose.yml index b5051cb..c7e3f18 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -54,7 +54,7 @@ services: # Display Settings - DISPLAY=:99 - # This ENV is used by the Dockerfile during build time if Patchright respects it. + # This ENV is used by the Dockerfile during build time if playwright respects it. # It's not strictly needed at runtime by docker-compose unless your app or scripts also read it. - PLAYWRIGHT_BROWSERS_PATH=/ms-browsers # Matches Dockerfile ENV - RESOLUTION=${RESOLUTION:-1920x1080x24} diff --git a/requirements.txt b/requirements.txt index bc8de8c..f562733 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -browser-use==0.1.45 +browser-use==0.1.47 pyperclip==1.9.0 gradio==5.27.0 json-repair diff --git a/src/agent/browser_use/browser_use_agent.py b/src/agent/browser_use/browser_use_agent.py index d5cba0f..f7f6107 100644 --- a/src/agent/browser_use/browser_use_agent.py +++ b/src/agent/browser_use/browser_use_agent.py @@ -15,9 +15,6 @@ from browser_use.agent.views import ( ToolCallingMethod, ) from browser_use.browser.views import BrowserStateHistory -from browser_use.telemetry.views import ( - AgentEndTelemetryEvent, -) from browser_use.utils import time_execution_async from dotenv import load_dotenv from browser_use.agent.message_manager.utils import is_model_without_tool_support @@ -144,19 +141,6 @@ class BrowserUseAgent(Agent): # Unregister signal handlers before cleanup signal_handler.unregister() - self.telemetry.capture( - AgentEndTelemetryEvent( - agent_id=self.state.agent_id, - is_done=self.state.history.is_done(), - success=self.state.history.is_successful(), - steps=self.state.n_steps, - max_steps_reached=self.state.n_steps >= max_steps, - errors=self.state.history.errors(), - total_input_tokens=self.state.history.total_input_tokens(), - total_duration_seconds=self.state.history.total_duration_seconds(), - ) - ) - if self.settings.save_playwright_script_path: logger.info( f'Agent run finished. Attempting to save Playwright script to: {self.settings.save_playwright_script_path}' diff --git a/src/agent/deep_research/deep_research_agent.py b/src/agent/deep_research/deep_research_agent.py index b7a7a56..6981890 100644 --- a/src/agent/deep_research/deep_research_agent.py +++ b/src/agent/deep_research/deep_research_agent.py @@ -81,7 +81,7 @@ async def run_single_browser_task( bu_browser_context = None try: logger.info(f"Starting browser task for query: {task_query}") - extra_args = [f"--window-size={window_w},{window_h}"] + extra_args = [] if use_own_browser: browser_binary_path = os.getenv("BROWSER_PATH", None) or browser_binary_path if browser_binary_path == "": @@ -99,6 +99,10 @@ async def run_single_browser_task( extra_browser_args=extra_args, wss_url=wss_url, cdp_url=cdp_url, + new_context_config=BrowserContextConfig( + window_width=window_w, + window_height=window_h, + ) ) ) diff --git a/src/browser/custom_browser.py b/src/browser/custom_browser.py index 676ec49..1556959 100644 --- a/src/browser/custom_browser.py +++ b/src/browser/custom_browser.py @@ -1,17 +1,17 @@ import asyncio import pdb -from patchright.async_api import Browser as PlaywrightBrowser -from patchright.async_api import ( +from playwright.async_api import Browser as PlaywrightBrowser +from playwright.async_api import ( BrowserContext as PlaywrightBrowserContext, ) -from patchright.async_api import ( +from playwright.async_api import ( Playwright, async_playwright, ) from browser_use.browser.browser import Browser, IN_DOCKER from browser_use.browser.context import BrowserContext, BrowserContextConfig -from patchright.async_api import BrowserContext as PlaywrightBrowserContext +from playwright.async_api import BrowserContext as PlaywrightBrowserContext import logging from browser_use.browser.chrome import ( @@ -48,9 +48,13 @@ class CustomBrowser(Browser): if ( not self.config.headless and hasattr(self.config, 'new_context_config') - and hasattr(self.config.new_context_config, 'browser_window_size') + and hasattr(self.config.new_context_config, 'window_width') + and hasattr(self.config.new_context_config, 'window_height') ): - screen_size = self.config.new_context_config.browser_window_size.model_dump() + screen_size = { + 'width': self.config.new_context_config.window_width, + 'height': self.config.new_context_config.window_height, + } offset_x, offset_y = get_window_adjustments() elif self.config.headless: screen_size = {'width': 1920, 'height': 1080} @@ -67,17 +71,12 @@ class CustomBrowser(Browser): *(CHROME_DISABLE_SECURITY_ARGS if self.config.disable_security else []), *(CHROME_DETERMINISTIC_RENDERING_ARGS if self.config.deterministic_rendering else []), f'--window-position={offset_x},{offset_y}', + f'--window-size={screen_size["width"]},{screen_size["height"]}', *self.config.extra_browser_args, } - contain_window_size = False - for arg in self.config.extra_browser_args: - if "--window-size" in arg: - contain_window_size = True - break - if not contain_window_size: - chrome_args.add(f'--window-size={screen_size["width"]},{screen_size["height"]}') - # check if port 9222 is already taken, if so remove the remote-debugging-port arg to prevent conflicts + # check if chrome remote debugging port is already taken, + # if so remove the remote-debugging-port arg to prevent conflicts with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: if s.connect_ex(('localhost', self.config.chrome_remote_debugging_port)) == 0: chrome_args.remove(f'--remote-debugging-port={self.config.chrome_remote_debugging_port}') @@ -100,6 +99,7 @@ class CustomBrowser(Browser): } browser = await browser_class.launch( + channel='chromium', # https://github.com/microsoft/playwright/issues/33566 headless=self.config.headless, args=args[self.config.browser_class], proxy=self.config.proxy.model_dump() if self.config.proxy else None, diff --git a/src/browser/custom_context.py b/src/browser/custom_context.py index c146d34..674191a 100644 --- a/src/browser/custom_context.py +++ b/src/browser/custom_context.py @@ -4,8 +4,8 @@ import os from browser_use.browser.browser import Browser, IN_DOCKER from browser_use.browser.context import BrowserContext, BrowserContextConfig -from patchright.async_api import Browser as PlaywrightBrowser -from patchright.async_api import BrowserContext as PlaywrightBrowserContext +from playwright.async_api import Browser as PlaywrightBrowser +from playwright.async_api import BrowserContext as PlaywrightBrowserContext from typing import Optional from browser_use.browser.context import BrowserContextState diff --git a/src/webui/components/browser_use_agent_tab.py b/src/webui/components/browser_use_agent_tab.py index 1a292dd..a488e70 100644 --- a/src/webui/components/browser_use_agent_tab.py +++ b/src/webui/components/browser_use_agent_tab.py @@ -450,7 +450,7 @@ async def run_agent_task( # Create Browser if needed if not webui_manager.bu_browser: logger.info("Launching new browser instance.") - extra_args = [f"--window-size={window_w},{window_h}"] + extra_args = [] if use_own_browser: browser_binary_path = os.getenv("BROWSER_PATH", None) or browser_binary_path if browser_binary_path == "": @@ -469,6 +469,10 @@ async def run_agent_task( extra_browser_args=extra_args, wss_url=wss_url, cdp_url=cdp_url, + new_context_config=BrowserContextConfig( + window_width=window_w, + window_height=window_h, + ) ) ) diff --git a/tests/test_agents.py b/tests/test_agents.py index 1285167..a36561e 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -29,21 +29,19 @@ async def test_browser_use_agent(): from src.utils import llm_provider from src.agent.browser_use.browser_use_agent import BrowserUseAgent - # llm = utils.get_llm_model( - # provider="openai", - # model_name="gpt-4o", - # temperature=0.8, - # base_url=os.getenv("OPENAI_ENDPOINT", ""), - # api_key=os.getenv("OPENAI_API_KEY", ""), - # ) - llm = llm_provider.get_llm_model( - provider="google", - model_name="gemini-2.0-flash", - temperature=0.6, - api_key=os.getenv("GOOGLE_API_KEY", "") + provider="openai", + model_name="gpt-4o", + temperature=0.8, ) + # llm = llm_provider.get_llm_model( + # provider="google", + # model_name="gemini-2.0-flash", + # temperature=0.6, + # api_key=os.getenv("GOOGLE_API_KEY", "") + # ) + # llm = utils.get_llm_model( # provider="deepseek", # model_name="deepseek-reasoner", @@ -104,7 +102,7 @@ async def test_browser_use_agent(): browser_context = None try: - extra_browser_args = [f"--window-size={window_w},{window_h}"] + extra_browser_args = [] if use_own_browser: browser_binary_path = os.getenv("BROWSER_PATH", None) if browser_binary_path == "": @@ -119,6 +117,10 @@ async def test_browser_use_agent(): headless=False, browser_binary_path=browser_binary_path, extra_browser_args=extra_browser_args, + new_context_config=BrowserContextConfig( + window_width=window_w, + window_height=window_h, + ) ) ) browser_context = await browser.new_context( @@ -256,7 +258,7 @@ async def test_browser_use_parallel(): browser_context = None try: - extra_browser_args = [f"--window-size={window_w},{window_h}"] + extra_browser_args = [] if use_own_browser: browser_binary_path = os.getenv("BROWSER_PATH", None) if browser_binary_path == "": @@ -271,6 +273,10 @@ async def test_browser_use_parallel(): headless=False, browser_binary_path=browser_binary_path, extra_browser_args=extra_browser_args, + new_context_config=BrowserContextConfig( + window_width=window_w, + window_height=window_h, + ) ) ) browser_context = await browser.new_context( diff --git a/tests/test_playwright.py b/tests/test_playwright.py index 5a522fd..6704a02 100644 --- a/tests/test_playwright.py +++ b/tests/test_playwright.py @@ -6,7 +6,7 @@ load_dotenv() def test_connect_browser(): import os - from patchright.sync_api import sync_playwright + from playwright.sync_api import sync_playwright chrome_exe = os.getenv("CHROME_PATH", "") chrome_use_data = os.getenv("CHROME_USER_DATA", "")