aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/ui/app.py
blob: a63e28f4f816a32e61c628f85914e236df30bb75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
MeshBay Node — local web UI (localhost:18000).

Minimal FastAPI app providing:
  GET /          → status page (HTML)
  GET /api/status → JSON status
  GET /api/files  → JSON file list from the group index
  GET /api/config → JSON config summary (no secrets)

Served only on 127.0.0.1 — not exposed to the network.
"""

import asyncio
import json
import logging
from typing import TYPE_CHECKING

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse

from meshbay_node import __version__

if TYPE_CHECKING:
    from meshbay_node.chat.store import ChatStore
    from meshbay_node.indexer import GroupIndex

log = logging.getLogger(__name__)


def create_ui_app(state: dict) -> FastAPI:
    """
    Create the UI FastAPI app.

    state dict is updated by the daemon and read by UI endpoints:
      state["status"]       : str  — "starting" | "running" | "error"
      state["group_id"]     : str
      state["group_name"]   : str
      state["hub_url"]      : str
      state["username"]     : str
      state["node_port"]    : int
      state["index"]        : GroupIndex | None
      state["endpoint_hint"]: str | None
    """
    app = FastAPI(
        title="MeshBay Node UI",
        version=__version__,
        docs_url=None,   # disable Swagger on local UI
        redoc_url=None,
    )

    @app.get("/api/status")
    async def api_status():
        index = state.get("index")
        return {
            "version":       __version__,
            "status":        state.get("status", "starting"),
            "hub_url":       state.get("hub_url", ""),
            "username":      state.get("username", ""),
            "group_id":      state.get("group_id", ""),
            "group_name":    state.get("group_name", ""),
            "node_port":     state.get("node_port", 0),
            "endpoint_hint": state.get("endpoint_hint"),
            "file_count":    index.count if index else 0,
            "index_version": index.version if index else 0,
        }

    @app.get("/api/files")
    async def api_files():
        index = state.get("index")
        if not index:
            return {"files": []}
        return {
            "files": [
                {
                    "id":       e.id[:16] + "…",
                    "name":     e.name,
                    "path":     e.path,
                    "size":     e.size,
                    "type":     e.type,
                    "duration": e.duration,
                }
                for e in index.entries
            ]
        }

    @app.get("/", response_class=HTMLResponse)
    async def root():
        index = state.get("index")
        status = state.get("status", "starting")
        file_count = index.count if index else 0
        status_color = {"running": "#22c55e", "error": "#ef4444"}.get(status, "#f59e0b")

        files_html = ""
        if index:
            rows = "".join(
                f"<tr><td>{e.name}</td><td>{e.type}</td>"
                f"<td>{e.size // 1024} KB</td><td>{e.path or '/'}</td></tr>"
                for e in index.entries
            )
            files_html = f"""
            <h2>Files ({file_count})</h2>
            <table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
              <thead><tr><th>Name</th><th>Type</th><th>Size</th><th>Path</th></tr></thead>
              <tbody>{rows}</tbody>
            </table>"""

        return f"""<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MeshBay Node</title>
  <style>
    body {{ font-family: monospace; max-width: 900px; margin: 40px auto; padding: 0 20px; }}
    .badge {{ display:inline-block; padding:3px 10px; border-radius:4px;
              color:#fff; background:{status_color}; font-weight:bold; }}
    table {{ font-size: 0.9em; }}
    th {{ background: #f3f4f6; }}
  </style>
  <meta http-equiv="refresh" content="10">
</head>
<body>
  <h1>🔗 MeshBay Node <span class="badge">{status}</span></h1>
  <p>
    <b>Hub:</b> {state.get("hub_url", "—")} &nbsp;|&nbsp;
    <b>User:</b> {state.get("username", "—")} &nbsp;|&nbsp;
    <b>Group:</b> {state.get("group_name") or state.get("group_id") or "—"} &nbsp;|&nbsp;
    <b>Port:</b> {state.get("node_port", "—")}
  </p>
  <p><b>Endpoint:</b> {state.get("endpoint_hint") or "unknown"}</p>
  {files_html}
  <hr>
  <small>MeshBay Node v{__version__} — <a href="/api/status">JSON status</a>
  — <a href="/api/files">JSON files</a> — <a href="/chat">Chat</a></small>
</body>
</html>"""

    # ── Chat endpoints ───────────────────────────────────────────────────────

    _chat_subscribers: list[WebSocket] = []

    @app.get("/api/chat/history")
    async def chat_history(since: float = 0, limit: int = 100):
        chat_store = state.get("chat_store")
        if not chat_store:
            return {"messages": []}
        msgs = await chat_store.get_messages(since=since, limit=limit)
        return {
            "messages": [
                {
                    "id": m.id,
                    "sender_id": m.sender_id,
                    "iteration": m.iteration,
                    "timestamp": m.timestamp,
                    "thread_id": m.thread_id,
                }
                for m in msgs
            ]
        }

    @app.websocket("/ws/chat")
    async def chat_websocket(ws: WebSocket):
        """WebSocket for real-time chat push to the local UI."""
        await ws.accept()
        _chat_subscribers.append(ws)
        try:
            while True:
                await ws.receive_text()
        except WebSocketDisconnect:
            pass
        finally:
            _chat_subscribers.remove(ws)

    async def broadcast_chat_to_ui(msg: dict) -> None:
        """Push a chat message to all connected UI WebSocket clients."""
        payload = json.dumps(msg)
        dead = []
        for ws in _chat_subscribers:
            try:
                await ws.send_text(payload)
            except Exception:
                dead.append(ws)
        for ws in dead:
            _chat_subscribers.remove(ws)

    app.broadcast_chat = broadcast_chat_to_ui

    @app.get("/chat", response_class=HTMLResponse)
    async def chat_page():
        return f"""<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MeshBay Chat</title>
  <style>
    body {{ font-family: monospace; max-width: 700px; margin: 40px auto; padding: 0 20px; }}
    #messages {{ border: 1px solid #ccc; padding: 10px; height: 400px; overflow-y: auto;
                 background: #fafafa; margin-bottom: 10px; }}
    .msg {{ margin: 4px 0; }}
    .sender {{ font-weight: bold; color: #2563eb; }}
    .time {{ color: #9ca3af; font-size: 0.8em; }}
  </style>
</head>
<body>
  <h1>MeshBay Chat</h1>
  <div id="messages"></div>
  <p><a href="/">Back to status</a></p>
  <script>
    const box = document.getElementById('messages');
    const ws = new WebSocket('ws://' + location.host + '/ws/chat');
    ws.onmessage = (e) => {{
      const msg = JSON.parse(e.data);
      const div = document.createElement('div');
      div.className = 'msg';
      const t = new Date(msg.timestamp * 1000).toLocaleTimeString();
      div.innerHTML = '<span class="time">' + t + '</span> '
        + '<span class="sender">' + msg.sender_id + '</span>: '
        + '(encrypted message #' + msg.iteration + ')';
      box.appendChild(div);
      box.scrollTop = box.scrollHeight;
    }};
  </script>
</body>
</html>"""

    return app