Files
ai-agent/backend/plugins/chrome_bridge.py
gabrielvidalandClaude Opus 5.5 118b6db0df feat(plugins): the lab Chrome leaves the core for the lab-chrome plugin
Settings → Browser, the conversation's Browser dock (noVNC live view and
take-over) and the container's on-demand life move into the lab-chrome
live plugin (ai-agent-plugins). The core keeps the Chrome chip (the
harness capability chrome) and gains three generic hooks:

- sockets[] + aiAgentPlugin.openSocket(id): a frame's websocket, opened by
  the host with the reader's cookies and relayed over postMessage to
  WS /api/plugins/{pid}/sockets/{sid}, which bridges only to the declared
  upstream (owner only, the gate's handshake rules, never the guest host;
  operator approval). GET …/sockets/{sid}/probe → {up, open, lastActiveAt}
  for the plugin's handlers.
- provides chrome.lab (plugins/chrome_bridge.py): {op: status} → the chip
  default and whether guests' runs keep --chrome; {op: ensure} before a
  lab --chrome run launches (spawn and resume).
- ui.slots[].size on app.modal (full = near full screen, frame filling it);
  conversation.gizmos' update and hub/conversations rows carry chrome.

Gone: backend/chrome/, /api/chrome*, schemas/chrome.py, the SSE chrome
event, the lab.integrations permission, BrowserDock/BrowserSettings/store,
the Browser header button and eye switch, @novnc/novnc. /settings/browser
redirects to /settings/plugin/lab-chrome; carryover.chrome moves
/data/chrome.json into the plugin's config.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 00:18:31 +01:00

131 lines
4.7 KiB
Python

"""The browser a lab ``--chrome`` run drives, from a live plugin
(``provides: {"chrome.lab": "<action>"}`` — the ``lab-chrome`` plugin since
2026-11-26: the core's ``backend/chrome/`` — Settings → Browser, the noVNC
proxy, the on-demand life, the idle reaper — is gone).
The core keeps the *Chrome chip* (the harness capability ``chrome``: a run
launched with ``--chrome``, stamped ``meta.chrome``). Whatever browser such a
lab run drives is the provider's; the core asks it, as the owner:
* ``{"op": "status"}`` → ``{reachable, chipDefault, guests}`` — cached
:data:`STATUS_TTL_S` (dropped when the plugin runs a non-read action):
:func:`guests_allowed` is whether a guest's run may keep ``--chrome``
(``runs/routes.py``); the composer reads the same answer through the
plugin's own ``status`` read (``ui.providers``).
* ``{"op": "ensure", "sessionId", "resume"}`` — before a lab claude run
launches with ``--chrome`` (``runs/service.py``): the provider brings the
browser up and waits for it, so the extension is pairing by the time the
run's first browser call lands. Up to :data:`ENSURE_TIMEOUT_S`; a failure
is logged and the run launches anyway (its first browser call says what is
wrong).
No provider: no guest keeps ``--chrome``, and nothing is started before a run.
"""
from __future__ import annotations
import logging
import threading
import time
from typing import Any
from plugins.store import OWNER_UID
log = logging.getLogger(__name__)
PROVIDES = "chrome.lab"
STATUS_TTL_S = 10.0
ENSURE_TIMEOUT_S = 120
_CHIP_DEFAULTS = ("off", "running", "always")
_lock = threading.Lock()
_status: tuple[float, dict[str, Any] | None] | None = None
def invalidate() -> None:
global _status
with _lock:
_status = None
def provides_chrome(rec: dict[str, Any] | None) -> bool:
return PROVIDES in (((rec or {}).get("manifest") or {}).get("provides") or {})
def touched(deps, rec: dict[str, Any] | None) -> None:
"""The plugin ``rec`` just ran a non-read action (start, stop, a setting):
the next status is asked anew."""
if provides_chrome(rec):
invalidate()
def _provider(deps) -> tuple[dict[str, Any], dict[str, Any]] | None:
"""The owner's active plugin providing ``chrome.lab`` → (rec, its action)."""
from plugins.waiting import _providers_for
try:
found = _providers_for(deps, OWNER_UID, PROVIDES)
except Exception: # noqa: BLE001 — no plugin store (tests, early boot)
return None
for rec, _grant in found:
mf = rec.get("manifest") or {}
act = (mf.get("actions") or {}).get((mf.get("provides") or {}).get(PROVIDES))
if act:
return rec, act
return None
def _run(deps, op: str, payload: dict[str, Any] | None, timeout: int) -> dict[str, Any] | None:
hit = _provider(deps)
if hit is None:
return None
rec, act = hit
from plugins.runner import run_script
try:
res = run_script(deps, deps.plugin_store, OWNER_UID, rec, act["script"],
payload={**(payload or {}), "op": op, "trigger": PROVIDES},
timeout_sec=min(timeout, int(act.get("timeoutSec") or timeout)))
except Exception as e: # noqa: BLE001 — a broken plugin never breaks a launch
log.info("%s %s via %s failed: %s", PROVIDES, op, rec.get("id"), e)
return None
return res if isinstance(res, dict) else None
def normalize(raw: dict[str, Any] | None) -> dict[str, Any] | None:
if not isinstance(raw, dict):
return None
chip = raw.get("chipDefault")
return {
"reachable": bool(raw.get("reachable")),
"chipDefault": chip if chip in _CHIP_DEFAULTS else "off",
"guests": raw.get("guests") is True,
}
def status(deps) -> dict[str, Any] | None:
"""The provider's status (a copy), cached; ``None`` without one."""
global _status
now = time.monotonic()
with _lock:
c = _status
if c is not None and now - c[0] < STATUS_TTL_S:
return dict(c[1]) if c[1] else None
snap = normalize(_run(deps, "status", None, 15))
with _lock:
_status = (time.monotonic(), snap)
return dict(snap) if snap else None
def guests_allowed(deps) -> bool:
"""Whether a guest's lab run may launch with ``--chrome``."""
snap = status(deps)
return bool(snap and snap.get("guests"))
def ensure(deps, session_id: str | None = None, *, resume: bool = False) -> None:
"""Bring the lab browser up before a ``--chrome`` lab run launches.
Never raises; a no-op without a provider."""
if _provider(deps) is None:
return
_run(deps, "ensure", {"sessionId": session_id, "resume": bool(resume)}, ENSURE_TIMEOUT_S)
invalidate()