Files
Laith Weinberger 60c88343f9 fix: skill_cli mode handling, --json flag positioning, and code cleanup
- Auto-detect browser mode from existing session metadata instead of assuming default; error on explicit mode conflicts
- Make --json work after subcommands (not just before) via argparse SUPPRESS
- Deduplicate utils between main.py and utils.py (lazy portalocker import)
- Extract inline command handlers from main() into standalone functions
- Fix get_available_modes() to match is_mode_available() LOCAL_MODES logic
- Fix setup validation icons for boolean results
- Delete unused protocol.py
2026-02-19 17:59:38 -05:00

115 lines
3.2 KiB
Python

"""Install configuration - tracks which browser modes are available.
This module manages the installation configuration that determines which browser
modes (chromium, real, remote) are available based on how browser-use was installed.
Config file: ~/.browser-use/install-config.json
When no config file exists (e.g., pip install users), all modes are available by default.
"""
import json
from pathlib import Path
from typing import Literal
CONFIG_PATH = Path.home() / '.browser-use' / 'install-config.json'
ModeType = Literal['chromium', 'real', 'remote']
# Local modes (both require Chromium to be installed)
LOCAL_MODES: set[str] = {'chromium', 'real'}
def get_config() -> dict:
"""Read install config. Returns default if not found.
Default config enables all modes (for pip install users).
"""
if not CONFIG_PATH.exists():
return {
'installed_modes': ['chromium', 'real', 'remote'],
'default_mode': 'chromium',
}
try:
return json.loads(CONFIG_PATH.read_text())
except (json.JSONDecodeError, OSError):
# Config file corrupt, return default
return {
'installed_modes': ['chromium', 'real', 'remote'],
'default_mode': 'chromium',
}
def save_config(installed_modes: list[str], default_mode: str) -> None:
"""Save install config."""
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
CONFIG_PATH.write_text(
json.dumps(
{
'installed_modes': installed_modes,
'default_mode': default_mode,
},
indent=2,
)
)
def is_mode_available(mode: str) -> bool:
"""Check if a browser mode is available based on installation config.
Args:
mode: The browser mode to check ('chromium', 'real', or 'remote')
Returns:
True if the mode is available, False otherwise
"""
config = get_config()
installed = config.get('installed_modes', [])
# Map 'real' to same category as 'chromium' (both are local)
# If either local mode is installed, both are available
if mode in LOCAL_MODES:
return bool(LOCAL_MODES & set(installed))
return mode in installed
def get_default_mode() -> str:
"""Get the default browser mode based on installation config."""
return get_config().get('default_mode', 'chromium')
def get_available_modes() -> list[str]:
"""Get list of available browser modes.
If any local mode is installed, both chromium and real are available.
"""
installed = set(get_config().get('installed_modes', ['chromium', 'real', 'remote']))
# If any local mode is installed, both are available
if installed & LOCAL_MODES:
installed |= LOCAL_MODES
# Return in stable order
return [m for m in ('chromium', 'real', 'remote') if m in installed]
def get_mode_unavailable_error(mode: str) -> str:
"""Generate a helpful error message when a mode is not available.
Args:
mode: The unavailable mode that was requested
Returns:
A formatted error message with instructions for reinstalling
"""
available = get_available_modes()
mode_desc = 'Local browser mode' if mode in LOCAL_MODES else 'Remote browser mode'
return (
f"Error: {mode_desc} '{mode}' not installed.\n"
f'Available modes: {", ".join(available)}\n\n'
f'To install all modes, reinstall with:\n'
f' curl -fsSL https://browser-use.com/cli/install.sh | bash -s -- --full'
)