Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
add cloud command and config utilities
ShawnPanabfa886afeac7
diff --git a/browser_use/skill_cli/commands/cloud.py b/browser_use/skill_cli/commands/cloud.pyindex d41832d0f..0886e0b70 100644--- a/browser_use/skill_cli/commands/cloud.py+++ b/browser_use/skill_cli/commands/cloud.py@@ -491,6 +491,11 @@ def handle_cloud_command(argv: list[str]) -> int:if subcmd in ('v2', 'v3'):return _cloud_versioned(argv[1:], subcmd)+ if subcmd == 'connect':+ # Normally intercepted by main.py before reaching here+ print('Error: cloud connect must be run via the main CLI (browser-use cloud connect)', file=sys.stderr)+ return 1+if subcmd in ('--help', 'help', '-h'):_print_cloud_usage()return 0@@ -504,6 +509,7 @@ def _print_cloud_usage() -> None:print('Usage: browser-use cloud <command>')print()print('Commands:')+ print(' connect Provision cloud browser and connect')print(' login <api-key> Save API key')print(' logout Remove API key')print(' v2 <METHOD> <path> [body] REST passthrough (API v2)')diff --git a/browser_use/skill_cli/utils.py b/browser_use/skill_cli/utils.pyindex 36baf129f..5e7c87eb5 100644--- a/browser_use/skill_cli/utils.py+++ b/browser_use/skill_cli/utils.py@@ -2,31 +2,77 @@import osimport platform+import reimport subprocessimport sysimport tempfile+import zlibfrom pathlib import Path-def get_socket_path() -> str:- """Get the fixed daemon socket path.+def validate_session_name(session: str) -> None:+ """Validate session name — reject path traversal and special characters.++ Raises ValueError on invalid name.+ """+ if not re.match(r'^[a-zA-Z0-9_-]+$', session):+ raise ValueError(f'Invalid session name {session!r}: only letters, digits, hyphens, and underscores allowed')+++def get_runtime_dir() -> Path:+ """Get runtime directory for daemon socket/PID files.++ Priority: BROWSER_USE_RUNTIME_DIR > XDG_RUNTIME_DIR/browser-use > ~/.browser-use/run > tempdir/browser-use+ """+ env_dir = os.environ.get('BROWSER_USE_RUNTIME_DIR')+ if env_dir:+ d = Path(env_dir)+ d.mkdir(parents=True, exist_ok=True)+ return d++ xdg = os.environ.get('XDG_RUNTIME_DIR')+ if xdg:+ d = Path(xdg) / 'browser-use'+ d.mkdir(parents=True, exist_ok=True)+ return d++ home_dir = Path.home() / '.browser-use' / 'run'+ try:+ home_dir.mkdir(parents=True, exist_ok=True)+ return home_dir+ except OSError:+ pass++ d = Path(tempfile.gettempdir()) / 'browser-use'+ d.mkdir(parents=True, exist_ok=True)+ return d+++def get_socket_path(session: str = 'default') -> str:+ """Get daemon socket path for a session.On Windows, returns a TCP address (tcp://127.0.0.1:PORT).On Unix, returns a Unix socket path."""if sys.platform == 'win32':- return 'tcp://127.0.0.1:49200'- return str(Path(tempfile.gettempdir()) / 'browser-use-cli.sock')+ port = 49152 + zlib.adler32(session.encode()) % 16383+ return f'tcp://127.0.0.1:{port}'+ return str(get_runtime_dir() / f'browser-use-{session}.sock')++def get_pid_path(session: str = 'default') -> Path:+ """Get PID file path for a session."""+ return get_runtime_dir() / f'browser-use-{session}.pid'-def is_daemon_alive() -> bool:++def is_daemon_alive(session: str = 'default') -> bool:"""Check daemon liveness by attempting socket connect.If socket file exists but nobody is listening, removes the stale file."""import socket- sock_path = get_socket_path()+ sock_path = get_socket_path(session)if sock_path.startswith('tcp://'):_, hostport = sock_path.split('://', 1)@@ -55,6 +101,51 @@ def is_daemon_alive() -> bool:return False+def list_sessions() -> list[dict]:+ """List active daemon sessions by scanning PID files.++ Returns list of {'name': str, 'pid': int, 'socket': str} for alive sessions.+ Cleans up stale PID/socket files for dead sessions.+ """+ runtime_dir = get_runtime_dir()+ sessions: list[dict] = []++ for pid_file in sorted(runtime_dir.glob('browser-use-*.pid')):+ # Extract session name from filename: browser-use-<session>.pid+ stem = pid_file.stem # browser-use-<session>+ session_name = stem[len('browser-use-') :]+ if not session_name:+ continue++ try:+ pid = int(pid_file.read_text().strip())+ except (OSError, ValueError):+ # Corrupt PID file — clean up+ pid_file.unlink(missing_ok=True)+ continue++ # Check if process is alive+ try:+ os.kill(pid, 0)+ except (OSError, ProcessLookupError):+ # Dead process — clean up stale files+ pid_file.unlink(missing_ok=True)+ sock_path = get_socket_path(session_name)+ if not sock_path.startswith('tcp://'):+ Path(sock_path).unlink(missing_ok=True)+ continue++ sessions.append(+ {+ 'name': session_name,+ 'pid': pid,+ 'socket': get_socket_path(session_name),+ }+ )++ return sessions++def get_log_path() -> Path:"""Get log file path for the daemon."""return Path(tempfile.gettempdir()) / 'browser-use-cli.log'diff --git a/tests/ci/test_cli_cloud_connect.py b/tests/ci/test_cli_cloud_connect.pynew file mode 100644index 000000000..6db2da79c--- /dev/null+++ b/tests/ci/test_cli_cloud_connect.py@@ -0,0 +1,48 @@+"""Tests for browser-use cloud connect CLI command."""++import subprocess+import sys+++def run_cli(*args: str, env_override: dict | None = None) -> subprocess.CompletedProcess:+ """Run the CLI as a subprocess, returning the result."""+ import os++ env = os.environ.copy()+ env.pop('BROWSER_USE_API_KEY', None)+ if env_override:+ env.update(env_override)++ return subprocess.run(+ [sys.executable, '-m', 'browser_use.skill_cli.main', *args],+ capture_output=True,+ text=True,+ env=env,+ timeout=15,+ )+++def test_cloud_connect_mutual_exclusivity_cdp_url():+ """cloud connect + --cdp-url should error."""+ result = run_cli('--cdp-url', 'http://localhost:9222', 'cloud', 'connect')+ assert result.returncode == 1+ assert 'mutually exclusive' in result.stderr.lower()+++def test_cloud_connect_mutual_exclusivity_profile():+ """cloud connect + --profile should error."""+ result = run_cli('--profile', 'Default', 'cloud', 'connect')+ assert result.returncode == 1+ assert 'mutually exclusive' in result.stderr.lower()+++def test_cloud_connect_shows_in_usage():+ """cloud help should list connect."""+ result = run_cli('cloud')+ assert 'connect' in result.stdout.lower()+++def test_cloud_connect_help_shows_in_epilog():+ """Main --help epilog should mention cloud connect."""+ result = run_cli('--help')+ assert 'cloud connect' in result.stdout.lower() or 'cloud' in result.stdout.lower()