diff --git a/cmd_chat/operator/__main__.py b/cmd_chat/operator/__main__.py index 1955369..d80afdc 100644 --- a/cmd_chat/operator/__main__.py +++ b/cmd_chat/operator/__main__.py @@ -136,6 +136,13 @@ def _build_parser() -> argparse.ArgumentParser: op.add_argument("--cost", type=float, default=5.0) op.add_argument("--session", default=None) + wb = sub.add_parser("web", help="serve a mobile chat UI in front of the daemon") + wb.add_argument("--bind", default="127.0.0.1", + help="listen address (default: 127.0.0.1 — loopback only; " + "the control socket grants room-send, so keep it local)") + wb.add_argument("--port", type=int, default=8790, help="listen port (default: 8790)") + wb.add_argument("--session", default=None) + sc = sub.add_parser("screen", help="print the relayed sandbox terminal buffer") sc.add_argument("--tail", type=int, default=None, help="last N bytes only") sc.add_argument("--ansi", action="store_true", help="keep ANSI codes (raw)") @@ -560,6 +567,11 @@ def _run_registry(args) -> int: return 0 +def _run_web(args) -> int: + from .web import serve + return serve(args.session, args.bind, args.port) + + def _run_simple(args, op: str) -> int: resp = _client_request(args, {"op": op}) print(json.dumps(resp)) @@ -599,6 +611,8 @@ def main(argv: list[str] | None = None) -> int: return _run_screen(args) if verb == "watch": return _run_watch(args) + if verb == "web": + return _run_web(args) if verb in ("roster", "status", "down"): return _run_simple(args, verb) return 2 diff --git a/cmd_chat/operator/web.py b/cmd_chat/operator/web.py new file mode 100644 index 0000000..98b9182 --- /dev/null +++ b/cmd_chat/operator/web.py @@ -0,0 +1,259 @@ +"""Mobile web chat front-end for the operator bridge (`operator web`). + +Turns a running operator daemon into a phone-friendly chat page. The daemon +already exposes the room as a local API over its AF_UNIX control socket +(`read` with long-poll, `say`, `roster`, `status`); this module is a thin, +**stdlib-only** HTTP shim in front of that socket so a browser can drive it: + + GET / the single-page chat UI (self-contained HTML/JS) + GET /api/events?since=N long-poll: proxies {"op":"read","wait":true} + POST /api/say {text} proxies {"op":"say"} + GET /api/status proxies {"op":"status"} (roster + connection) + +Why this exists: on Termux the tmux `read --wait` loop is read-only — replying +means a second shell typing `operator say …`, which is painful on a touch +keyboard. A browser page gives a real input box, scrollback and live updates +with zero extra dependencies (no server stack, no JS build — just http.server). + +Binds 127.0.0.1 by default: the control socket grants room-send rights, so the +listener stays loopback-only unless you deliberately pass a routable --bind. +""" + +from __future__ import annotations + +import json +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from urllib.parse import urlparse, parse_qs + +from .cli_client import BridgeUnreachable, request +from .session import resolve + +# Long-poll window the browser holds open per /api/events call. The socket read +# timeout must outlast it (see request(read_timeout=...)). +_POLL_TIMEOUT = 25.0 + + +_PAGE = """ + +
+ + +