- spawn takes codeMode (the composer's Code β chip): the sidecar runs it on its pty wrapper; Chrome is dropped (Code Mode has none); lean runs never. Guests too, unless their Access switch (users codeMode, on by default) is off. - what the CLI's banner said is stamped: meta.codeMode true, or false with codeModeOff = why — from the launch answer, else from /sessions on the exit watcher's tick. - the parser reads the transcript's repl-mode record (summary codeMode, PARSER_VERSION 44): a terminal session started in Code Mode shows it. - GET /api/code-mode: per runner (lab sidecar /health, workers — a proxied one can't: the CLI refuses a custom ANTHROPIC_BASE_URL) and whether the caller may use it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
3.2 KiB
Python
81 lines
3.2 KiB
Python
"""Where a conversation can start in Claude Code's Code Mode (beta).
|
|
|
|
Code Mode (``CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1``: the model writes
|
|
JavaScript its REPL tool runs, each `` $`…` `` / ``edit()`` in it an ordinary
|
|
tool call) needs, besides a supported model (Opus 5.5, Sonnet 5.5,
|
|
Haiku 5.5, Fable 5.2 — the composer's rule, ``lib/codeMode.ts``):
|
|
|
|
* a runner whose sidecar can run it — CLI 2.1.323 or later and the pty
|
|
wrapper (sidecar /health ``codeMode``: ``{ok, cli, reason}``);
|
|
* a login it calls the Anthropic API with **directly**: the CLI refuses Code
|
|
Mode behind a custom ``ANTHROPIC_BASE_URL``, so a worker that bills through
|
|
the hub's Claude proxy (``claudeMode: proxy`` — the personal Mac by
|
|
default, every guest's computer) can't.
|
|
|
|
``GET /api/code-mode`` answers that per runner for the composer's *Code β*
|
|
chip (greyed with the reason where it can't). The launch itself is the
|
|
sidecar's (launch.py "Code Mode"); runs/service.py stamps what happened.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
PROXIED = ("this computer bills through the hub's Claude proxy — Code Mode needs "
|
|
"a direct Claude login on it")
|
|
OLD_SIDECAR = "this runner's sidecar predates Code Mode — update it"
|
|
OFFLINE = "this runner is offline"
|
|
LAB_DOWN = "the lab's sidecar isn't answering"
|
|
|
|
_lab: list = [0.0, None] # [fetched at, {ok, cli, reason}]
|
|
LAB_TTL_S = 60.0
|
|
|
|
|
|
def _clean(h) -> dict | None:
|
|
if not isinstance(h, dict) or not isinstance(h.get("ok"), bool):
|
|
return None
|
|
return {"ok": h["ok"], "cli": h.get("cli") if isinstance(h.get("cli"), str) else None,
|
|
"reason": None if h["ok"] else str(h.get("reason") or "unavailable")[:300]}
|
|
|
|
|
|
def lab(get=None) -> dict:
|
|
"""The lab sidecar's ``codeMode`` (cached a minute)."""
|
|
if time.time() - _lab[0] < LAB_TTL_S and _lab[1] is not None:
|
|
return _lab[1]
|
|
if get is None:
|
|
from runs.sidecar_client import _sidecar_get as get
|
|
h = get("/health", timeout=3)
|
|
if h is None:
|
|
out = {"ok": False, "cli": None, "reason": LAB_DOWN}
|
|
else:
|
|
out = _clean(h.get("codeMode")) or {"ok": False, "cli": None, "reason": OLD_SIDECAR}
|
|
_lab[:] = [time.time(), out]
|
|
return out
|
|
|
|
|
|
def worker(w: dict, status: dict, mode: str) -> dict:
|
|
"""A paired worker: its sidecar's own answer, unless it bills through the
|
|
proxy (``mode`` — claudeproxy/service.py ``mode``) or is offline."""
|
|
h = _clean(status.get("codeMode"))
|
|
cli = h["cli"] if h else None
|
|
if mode != "own":
|
|
return {"ok": False, "cli": cli, "reason": PROXIED}
|
|
if status.get("status") not in (None, "online"):
|
|
return {"ok": False, "cli": cli, "reason": OFFLINE}
|
|
return h or {"ok": False, "cli": None, "reason": OLD_SIDECAR}
|
|
|
|
|
|
def runners(deps, *, owner: bool) -> dict[str, dict]:
|
|
"""``{"local": lab, <worker id>: …}`` — a guest sees the lab only (their
|
|
own computers always bill through the proxy)."""
|
|
out = {"local": lab()}
|
|
if not owner:
|
|
return out
|
|
from claudeproxy import service as proxy
|
|
for w in deps.worker_store.list():
|
|
try:
|
|
out[w["id"]] = worker(w, deps.worker_store.status(w["id"]), proxy.mode(w))
|
|
except Exception: # one odd record must not hide the rest
|
|
continue
|
|
return out
|