- sidecar: chrome flag on spawn/resume/fork → --chrome; /chrome status + start/stop/restart/reset-profile (docker) - backend: chrome/ package — status + settings (/api/chrome), owner-only websocket proxy to the container's websockify (/api/chrome/ws), ensure-start before a --chrome lab run, idle reaper; meta.chrome kept on resume - frontend: Chrome chip (trigger words via the chrome skill tag), ChromeBadge on resume, BrowserDock gizmo + take-over panel (@novnc/novnc), Settings → Browser page, visibility toggle, mock backend - docs: CLAUDE.md section
70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
"""Settings → Browser: a small JSON file, rewritten atomically."""
|
||
|
||
import json
|
||
import os
|
||
import pathlib
|
||
import threading
|
||
|
||
CHIP_DEFAULTS = ("off", "running", "always")
|
||
KEEP = ("always", "idle")
|
||
DEFAULTS = {
|
||
# Whether the composer's Chrome chip starts on: never, when the lab
|
||
# browser is up, or always.
|
||
"chipDefault": "off",
|
||
# Whether a guest's runs may launch with --chrome (they never see the live
|
||
# view, which is owner-only).
|
||
"guests": False,
|
||
# The container's life: "always" on, or "idle" — started by the first lab
|
||
# run launched with --chrome, stopped once no such run has been live and
|
||
# nobody has watched it for `idleMinutes` (chrome/lifecycle.py).
|
||
"keep": "idle",
|
||
"idleMinutes": 30,
|
||
}
|
||
|
||
|
||
def valid_settings(raw: dict) -> dict:
|
||
if not isinstance(raw, dict):
|
||
raise ValueError("settings must be an object")
|
||
out = dict(DEFAULTS)
|
||
if "chipDefault" in raw:
|
||
if raw["chipDefault"] not in CHIP_DEFAULTS:
|
||
raise ValueError(f"chipDefault must be one of {', '.join(CHIP_DEFAULTS)}")
|
||
out["chipDefault"] = raw["chipDefault"]
|
||
if "guests" in raw:
|
||
if not isinstance(raw["guests"], bool):
|
||
raise ValueError("guests must be a boolean")
|
||
out["guests"] = raw["guests"]
|
||
if "keep" in raw:
|
||
if raw["keep"] not in KEEP:
|
||
raise ValueError(f"keep must be one of {', '.join(KEEP)}")
|
||
out["keep"] = raw["keep"]
|
||
if "idleMinutes" in raw:
|
||
m = raw["idleMinutes"]
|
||
if not isinstance(m, int) or isinstance(m, bool) or not 1 <= m <= 24 * 60:
|
||
raise ValueError("idleMinutes must be a whole number of minutes (1–1440)")
|
||
out["idleMinutes"] = m
|
||
return out
|
||
|
||
|
||
class ChromeStore:
|
||
def __init__(self, path: str):
|
||
self.path = pathlib.Path(path)
|
||
self._lock = threading.Lock()
|
||
try:
|
||
self._cfg = valid_settings(json.loads(self.path.read_text(encoding="utf-8")))
|
||
except (OSError, ValueError):
|
||
self._cfg = dict(DEFAULTS)
|
||
|
||
def get(self) -> dict:
|
||
with self._lock:
|
||
return dict(self._cfg)
|
||
|
||
def set(self, raw: dict) -> dict:
|
||
cfg = valid_settings({**self.get(), **(raw or {})})
|
||
with self._lock:
|
||
tmp = self.path.with_suffix(".tmp")
|
||
tmp.write_text(json.dumps(cfg, indent=2), encoding="utf-8")
|
||
os.replace(tmp, self.path)
|
||
self._cfg = cfg
|
||
return dict(cfg)
|