fix(browser): implement manual Chrome launch with CDP for remote debugging

When REMOTE_DEBUGGING_ENABLED is true, launch Chrome manually via subprocess and connect via CDP instead of using BrowserProfile. This improves container compatibility and provides better control over browser lifecycle.
This commit is contained in:
nafer committed 2026-03-05 23:54:53 +03:00
1 parent fdf98f6004
commit 4e8160ae85
2 files changed
+42 -22

No files matched your search

Binary file not shown.
+42 -22
View File
@@ -74,31 +74,51 @@ async def run_browser_task(task_id: str, task: str, max_steps: int, model: str):
api_key=api_key
)
# Настройки браузера через BrowserProfile
# Если включен remote debugging - запускаем в не-headless режиме
is_headless = not REMOTE_DEBUGGING_ENABLED
browser_profile = BrowserProfile(
user_data_dir="/app/browser-data",
profile_directory="Default",
headless=is_headless,
keep_alive=True, # Сохраняем сессию между задачами
)
# Добавляем remote debugging если включено
extra_args = []
# Настройки браузера
# Если включен remote debugging - запускаем Chrome вручную и подключаемся через CDP
if REMOTE_DEBUGGING_ENABLED:
extra_args = [
import subprocess
import tempfile
# Создаем временную директорию для профиля
user_data_dir = "/app/browser-data"
# Запускаем Chrome с remote debugging
chrome_cmd = [
"chromium",
f"--remote-debugging-port={REMOTE_DEBUGGING_PORT}",
"--remote-debugging-address=0.0.0.0",
"--no-sandbox", # Важно для работы в контейнере
"--disable-dev-shm-usage"
f"--user-data-dir={user_data_dir}",
"--no-first-run",
"--no-default-browser-check",
"--disable-extensions",
"--disable-popup-blocking",
"--disable-gpu",
"--no-sandbox",
"about:blank"
]
browser = Browser(
browser_profile=browser_profile,
args=extra_args
)
# Запускаем процесс
process = subprocess.Popen(
chrome_cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
# Даем браузеру время запуститься
await asyncio.sleep(3)
# Подключаемся через CDP
cdp_url = f"http://localhost:{REMOTE_DEBUGGING_PORT}"
browser = Browser(cdp_url=cdp_url)
else:
# Обычный запуск
browser_profile = BrowserProfile(
user_data_dir="/app/browser-data",
profile_directory="Default",
headless=True,
keep_alive=True,
)
browser = Browser(browser_profile=browser_profile)
# Создание агента
agent = Agent(