"""Hermes Web UI -- startup helpers.""" from __future__ import annotations import os, subprocess, sys from pathlib import Path def _agent_dir() -> Path | None: hermes_home = Path(os.environ.get('HERMES_HOME', str(Path.home() / '.hermes'))) for raw in [os.environ.get('HERMES_WEBUI_AGENT_DIR', '').strip(), str(hermes_home / 'hermes-agent')]: if not raw: continue p = Path(raw).expanduser() if p.is_dir(): return p.resolve() return None def auto_install_agent_deps() -> bool: agent_dir = _agent_dir() if agent_dir is None: print('[!!] Auto-install skipped: agent directory not found.', flush=True) return False req_file = agent_dir / 'requirements.txt' pyproject = agent_dir / 'pyproject.toml' if req_file.exists(): install_args = [sys.executable, '-m', 'pip', 'install', '--quiet', '-r', str(req_file)] print(f' Installing from {req_file} ...', flush=True) elif pyproject.exists(): install_args = [sys.executable, '-m', 'pip', 'install', '--quiet', str(agent_dir)] print(f' Installing from {agent_dir} (pyproject.toml) ...', flush=True) else: print('[!!] Auto-install skipped: no requirements.txt or pyproject.toml in agent dir.', flush=True) return False try: result = subprocess.run(install_args, capture_output=True, text=True, timeout=120) if result.returncode != 0: print(f'[!!] pip install failed (exit {result.returncode}):', flush=True) for line in (result.stderr or '').splitlines()[-10:]: print(f' {line}', flush=True) return False print('[ok] pip install completed.', flush=True) return True except subprocess.TimeoutExpired: print('[!!] Auto-install timed out after 120s.', flush=True) return False except Exception as e: print(f'[!!] Auto-install error: {e}', flush=True) return False