From e1afdce07309b777f13d4f53e281736db8378c19 Mon Sep 17 00:00:00 2001 From: OpenBot Date: Tue, 13 Oct 2026 15:00:55 +0800 Subject: [PATCH] feat(admin): single-file dark dashboard UI on / and /ui Static HTML served by StatusHTTP (stdlib only, no build step): service health pills, channel connection states, agent processes, live bindings table; fetches existing /status + /sessions JSON, 10s auto-refresh. --- src/agenthub/admin_ui.py | 100 +++++++++++++++++++++++++++++++++++++ src/agenthub/http_admin.py | 16 +++++- tests/test_admin_ui.py | 74 +++++++++++++++++++++++++++ 3 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 src/agenthub/admin_ui.py create mode 100644 tests/test_admin_ui.py diff --git a/src/agenthub/admin_ui.py b/src/agenthub/admin_ui.py new file mode 100644 index 0000000..e4f3191 --- /dev/null +++ b/src/agenthub/admin_ui.py @@ -0,0 +1,100 @@ +"""Admin UI for agenthub — single-file dashboard served by StatusHTTP. + +Design: dark terminal-inspired theme, pure stdlib (no build step), auto-refresh. +Panels: service health, channels (connection state), agents (running procs), +live session bindings. All data comes from the existing /status + /sessions +endpoints — the UI is a static HTML string that fetches JSON client-side. +""" + +DARK_CSS = """ +:root{--bg:#0d1117;--panel:#161b22;--border:#30363d;--fg:#e6edf3;--muted:#8b949e; +--green:#3fb950;--red:#f85149;--amber:#d29922;--blue:#58a6ff;--mono:ui-monospace,'SF Mono','Cascadia Code',Menlo,monospace} +*{box-sizing:border-box;margin:0;padding:0} +body{background:var(--bg);color:var(--fg);font-family:var(--mono);font-size:14px;padding:24px;max-width:1200px;margin:0 auto} +h1{font-size:18px;font-weight:600;display:flex;align-items:center;gap:10px;margin-bottom:4px} +h1 .sub{font-size:12px;color:var(--muted);font-weight:400} +.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(340px,1fr));gap:16px;margin-top:18px} +.card{background:var(--panel);border:1px solid var(--border);border-radius:8px;padding:16px} +.card h2{font-size:12px;text-transform:uppercase;letter-spacing:.08em;color:var(--muted);margin-bottom:12px;display:flex;justify-content:space-between;align-items:center} +.pill{font-size:11px;padding:2px 8px;border-radius:10px;font-weight:600} +.pill.ok{background:#12351d;color:var(--green)} +.pill.bad{background:#3d1418;color:var(--red)} +.pill.warn{background:#33290c;color:var(--amber)} +.row{display:flex;justify-content:space-between;align-items:center;padding:8px 0;border-bottom:1px solid var(--border)} +.row:last-child{border-bottom:none} +.row .k{color:var(--muted)} +.dot{width:8px;height:8px;border-radius:50%;display:inline-block;margin-right:8px} +.dot.ok{background:var(--green)}.dot.bad{background:var(--red)}.dot.warn{background:var(--amber)}.dot.idle{background:var(--muted)} +.kv{font-size:22px;font-weight:600} +.meta{color:var(--muted);font-size:12px;margin-top:6px} +table{width:100%;border-collapse:collapse;font-size:13px} +th{color:var(--muted);text-align:left;font-weight:500;padding:6px 8px;border-bottom:1px solid var(--border);font-size:11px;text-transform:uppercase;letter-spacing:.06em} +td{padding:8px;border-bottom:1px solid var(--border)} +tr:last-child td{border-bottom:none} +code{background:#21262d;padding:1px 6px;border-radius:4px;font-size:12px;color:var(--blue)} +#refresh{color:var(--muted);font-size:11px;cursor:pointer;user-select:none} +#refresh:hover{color:var(--blue)} +.empty{color:var(--muted);font-style:italic;padding:12px 0;font-size:13px} +""" + +HTML = """ + +agenthub + +

⚡ agenthub ACP bridge · IM ⇄ NATS ⇄ agents

+
refreshed — · ↻ refresh · auto 10s
+ +
+

Service …

+
+
—
agents
+
—
sessions
+
—
uptime
+
+ +

Channels

+ +

Agents

+ +

Live Bindings

+
+""" + + +def render_index() -> str: + return HTML.replace("__CSS__", DARK_CSS) diff --git a/src/agenthub/http_admin.py b/src/agenthub/http_admin.py index 5234cd1..4c17a57 100644 --- a/src/agenthub/http_admin.py +++ b/src/agenthub/http_admin.py @@ -20,6 +20,8 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from typing import Any from urllib.parse import parse_qs, urlparse +from agenthub.admin_ui import render_index + class StatusHTTP: def __init__(self, mgr: Any, host: str = "127.0.0.1", port: int = 30990): @@ -107,8 +109,18 @@ class StatusHTTP: def do_GET(self): parsed = urlparse(self.path) - self._route_get(parsed.path.rstrip("/") or "/", parse_qs(parsed.query)) - + path = parsed.path.rstrip("/") or "/" + if path in ("/", "/ui", "/index.html"): + # Admin UI (browser-facing): the same nginx /agents/ prefix + # serves it; JSON endpoints remain for programmatic access. + data = render_index().encode() + self.send_response(200) + self.send_header("Content-Type", "text/html; charset=utf-8") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + return + self._route_get(path, parse_qs(parsed.query)) def do_POST(self): length = int(self.headers.get("Content-Length", 0)) self._route_post(urlparse(self.path).path.rstrip("/"), self.rfile.read(length)) diff --git a/tests/test_admin_ui.py b/tests/test_admin_ui.py new file mode 100644 index 0000000..7d4abde --- /dev/null +++ b/tests/test_admin_ui.py @@ -0,0 +1,74 @@ +"""Tests for the admin UI endpoint (served on / and /ui).""" + +import json +import threading +import time +import urllib.request +from unittest.mock import MagicMock + +import pytest + +from agenthub.http_admin import StatusHTTP + + +class FakeBinding: + def __init__(self, channel, chat_id, agent, acp_session_id): + self.channel = channel + self.chat_id = chat_id + self.agent = agent + self.acp_session_id = acp_session_id + self.last_active = time.time() + + +@pytest.fixture +def server(): + mgr = MagicMock() + mgr.list_agents.return_value = [ + {"name": "opencode", "cmd": "opencode acp", "running": True}, + {"name": "codex", "cmd": "codex acp", "running": False}, + ] + mgr._bindings = { + ("wecom", "c1"): FakeBinding("wecom", "c1", "opencode", "s1"), + } + mgr._lock = threading.Lock() + http = StatusHTTP(mgr, host="127.0.0.1", port=0) # port 0 = ephemeral + http.start() + port = http._server.server_address[1] + yield f"http://127.0.0.1:{port}" + http.stop() + + +def _get(url): + with urllib.request.urlopen(url, timeout=5) as r: + return r.status, r.read() + + +def test_root_serves_html(server): + code, body = _get(server + "/") + assert code == 200 + assert b"text/html" not in body # header not in body; content check below + assert b"agenthub" in body + assert b"" in body + + +def test_ui_alias(server): + code, body = _get(server + "/ui") + assert code == 200 and b"" in body + + +def test_json_endpoints_still_work(server): + code, body = _get(server + "/status") + d = json.loads(body) + assert d["service"] == "agenthub" + assert len(d["agents"]) == 2 + code, body = _get(server + "/sessions") + d = json.loads(body) + assert d["bindings"][0]["agent"] == "opencode" + + +def test_html_references_fetch_targets(server): + _, body = _get(server + "/") + # UI fetches status/sessions relatively — must not hardcode hosts + assert b"fetch('status')" in body + assert b"fetch('sessions')" in body + assert b"botflow_truncated" not in body # sanity: unrelated content absent