"""Browser bridge: real browser automation (Playwright) behind HTTP. A codegen-authored skill body must never drive a browser directly, and it must never bundle a heavy, platform-specific automation stack. This bridge owns the browser — a single Chromium (or Firefox/WebKit) instance with a persistent profile — and exposes one uniform, token-guarded localhost JSON API for reading a page (text/links/screenshot) and acting on it (goto/click/fill/press/select/ wait/back/forward), so a body only reads `browser_bridge_url` and `browser_bridge_token` from `ctx.config` and calls a documented route. Unlike Nextcloud/Outlook this bridge needs a real optional dependency (`playwright` plus browser binaries), so it is **disabled by default**; install it with `scripts/bootstrap.sh --browser` (browsers land under `.runtime/browser`). The dependency and the browser are lazy: a host without them still runs every other bridge. `check_requirements()` deliberately always returns ok — `run_bridges()` aborts *all* bridges when one's requirements fail, and this dependency is optional, so a missing Playwright must degrade to a per-call 503 (with an install hint), never take down simplex/llm/nextcloud. The actual Playwright work lives in `browser_client.py`; the routes here validate input and map a real failure (`BrowserError`) to a 502, a missing dependency/browser (`BrowserUnavailable`) to a 503, and bad input to a 400 — never a fabricated success. """ from __future__ import annotations from pathlib import Path from .base import BridgeInfo, BridgeService from .browser_client import ( DEFAULT_MAX_TEXT_CHARS, DEFAULT_TIMEOUT, ENGINES, WAIT_UNTIL, BrowserDriver, BrowserError, BrowserUnavailable, ) #: The checkout root (`semif_agent/bridges/browser.py` -> repo root), used to #: anchor the browser profile, binaries, and screenshots under the gitignored #: `.runtime/` tree (the containment rule). _REPO_ROOT = Path(__file__).resolve().parents[2] DEFAULT_BROWSER_ROOT = str(_REPO_ROOT / ".runtime" / "browser") DEFAULT_PROFILE_DIR = str(Path(DEFAULT_BROWSER_ROOT) / "profile") DEFAULT_SCREENSHOT_DIR = str(Path(DEFAULT_BROWSER_ROOT) / "screenshots") class BrowserBridge(BridgeService): name = "browser" INFO = BridgeInfo( name="browser", service="browser", description=( "Drive a real web browser (Playwright): open a URL, read a page's " "text or links, take a screenshot, and click/fill/press/select to " "navigate a site and complete a flow. Keeps a persistent profile so " "logins and cookies survive across calls." ), url_config_var="browser_bridge_url", endpoints=( "GET /health -> 200 {\"ok\": true, \"platform\": \"browser\", " "\"engine\", \"headless\"} — the bridge is up", "GET /state -> 200 {\"url\", \"title\"} — the current page", "GET /content?format=text|html&max_chars= -> 200 {\"url\", " "\"title\", \"format\", \"content\", \"truncated\"} — the whole page's " "rendered text (format defaults to text, the default when you just " "want to read a page); content is truncated to max_chars", "GET /text?selector= -> 200 {\"url\", \"selector\", \"count\", " "\"matches\": [{\"text\"}]} — text of every element matching a CSS " "selector", "GET /links?query=&limit= -> 200 {\"url\", \"links\": " "[{\"text\", \"href\"}]} — anchors on the page, optionally filtered " "by a substring of the label or href", "GET /screenshot?full_page=&selector=&inline= -> 200 " "{\"ok\": true, \"path\", \"bytes\", \"url\", \"title\"} — saves a PNG " "under the checkout's .runtime/browser/screenshots (add inline=true " "for base64 in the reply)", "POST /goto {\"url\", \"wait_until\"?: commit|domcontentloaded|load|" "networkidle, \"timeout\"?: seconds} -> 200 {\"url\", \"title\", " "\"status\"} — navigates; a navigation error (bad host, TLS, timeout) " "is a 502", "POST /click {\"selector\"?| \"text\"?, \"index\"?: 0, \"timeout\"?} " "-> 200 {\"url\", \"title\"} — clicks by CSS selector or by visible " "text (index picks among several matches)", "POST /fill {\"selector\", \"value\", \"submit\"?: bool, " "\"timeout\"?} -> 200 {\"url\", \"title\"} — types into an input; " "submit=true presses Enter", "POST /press {\"key\", \"selector\"?, \"timeout\"?} -> 200 {\"url\", " "\"title\"} — presses a keyboard key (e.g. Enter, Escape, Tab)", "POST /select {\"selector\", \"value\"?|\"label\"?, \"timeout\"?} -> " "200 {\"url\", \"title\"} — chooses an option in a