Add an opt-in `web` plugin to `clai2` that serves the session to a browser, with `/web` and `clai2 --web`

Merged
MP
2mo ago
+2801-6

Part 2 of 3 of a browser UI for clai2 (stack: #10261 → this PR → #10263). Based on #10261; the UI itself lands in #10263, so / answers 503 "not built" until then. No linked issue: built at the CLAI maintainer's request.

Why. Drive a CLAI session from a browser as well as the terminal, without a second agent loop or session store.

New public surface

  • Built-in plugin web (pydantic_clai2.builtin_plugins.web), off by default, with /web [start|stop|status] and settings host, port, open_browser, ask_user.
  • clai2 --web (also uvx pydantic-clai2 --web): CLAI with no terminal prompt, web loaded for that launch only.
  • fastapi and uvicorn>=0.38 become dependencies. An extra would break uvx pydantic-clai2 --web. uvicorn and starlette already come with mcp.

How it reuses CLAI. Every browser action goes through host.send_input (prompts, /commands, /resume ID, /new, /model NAME), so the terminal and the browser drive one session in one order. The stream comes from a capability's on_event, filtered to the held conversation, and turn and command results come from CLAI's moments. The sidebar reads host.sessions. ask_user is answered in the browser and the terminal picker at once (a compatibility row keeps the ask_user plugin off meanwhile).

Security. Binds 127.0.0.1 and rejects wildcard hosts. Every request must name the server's own Host (no DNS rebinding) and carry a per-launch token: in a header for the API, in the URL for the page and assets, in the query plus a matching Origin for the WebSocket. No cookie, since cookies are shared across ports. The browser opens through a 0600 launcher file, so the token is never on a command line. All handlers are async on CLAI's loop, and uvicorn's signal handlers are disabled.

Verification. tests/clai2/test_web.py drives a real shell with TestModel through FastAPI's TestClient on one loop: streamed turn and tool cards over the WebSocket, commands, ask_user (picked, typed, declined), interrupt, every session action, every access check, the slow-browser resync. A real uvicorn server is checked with a foreign Host and a WebSocket. tests/clai2/test_web_parts.py covers event translation, history, the answerer race, and --web. Both files reach 100% branch coverage of the new code.

What changes for existing users. Nothing until they enable web or pass --web.

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.