@hncode/hncode 0.8.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/README.md +30 -0
  2. package/README_CN.md +27 -0
  3. package/package.json +1 -1
  4. package/src/agent.js +172 -43
  5. package/src/bootstrap.js +63 -56
  6. package/src/ci.js +247 -247
  7. package/src/colors.js +5 -1
  8. package/src/commands.js +121 -0
  9. package/src/composer.js +31 -1
  10. package/src/config.js +177 -45
  11. package/src/context.js +29 -0
  12. package/src/files.js +32 -0
  13. package/src/fx.js +19 -2
  14. package/src/i18n.js +180 -131
  15. package/src/index.js +558 -77
  16. package/src/permission-gate.js +103 -0
  17. package/src/plugin.js +221 -2
  18. package/src/prompt-presets.js +36 -0
  19. package/src/protocol.js +184 -0
  20. package/src/registry-browser.js +14 -3
  21. package/src/remote.js +117 -5
  22. package/src/schedule.js +148 -11
  23. package/src/serve.js +613 -0
  24. package/src/session-client.js +148 -0
  25. package/src/session-host.js +52 -0
  26. package/src/session.js +26 -3
  27. package/src/settings-schema.js +2 -2
  28. package/src/status-line.js +48 -0
  29. package/src/term.js +20 -1
  30. package/src/toml.js +18 -4
  31. package/src/tool-line.js +13 -6
  32. package/src/tool-status.js +45 -0
  33. package/src/tools/bash.js +43 -24
  34. package/src/tools/edit.js +10 -5
  35. package/src/tools/web-search.js +8 -5
  36. package/src/tools/write.js +28 -3
  37. package/src/tui.js +498 -1397
  38. package/src/ws.js +248 -0
  39. package/src/session-hub.js +0 -445
  40. package/src/web-auth.js +0 -98
  41. package/src/web-daemon-cli.js +0 -63
  42. package/src/web-daemon-client.js +0 -203
  43. package/src/web-daemon.js +0 -894
  44. package/src/web-live.js +0 -250
  45. package/src/web-public/app.css +0 -1524
  46. package/src/web-public/app.js +0 -2969
  47. package/src/web-public/complete.js +0 -154
  48. package/src/web-public/fonts/JetBrainsMono-Bold.woff2 +0 -0
  49. package/src/web-public/fonts/JetBrainsMono-Regular.woff2 +0 -0
  50. package/src/web-public/fonts/noto-sans-sc-100-400-normal.woff2 +0 -0
  51. package/src/web-public/fonts/noto-sans-sc-101-400-normal.woff2 +0 -0
  52. package/src/web-public/fonts/noto-sans-sc-102-400-normal.woff2 +0 -0
  53. package/src/web-public/fonts/noto-sans-sc-103-400-normal.woff2 +0 -0
  54. package/src/web-public/fonts/noto-sans-sc-104-400-normal.woff2 +0 -0
  55. package/src/web-public/fonts/noto-sans-sc-105-400-normal.woff2 +0 -0
  56. package/src/web-public/fonts/noto-sans-sc-106-400-normal.woff2 +0 -0
  57. package/src/web-public/fonts/noto-sans-sc-107-400-normal.woff2 +0 -0
  58. package/src/web-public/fonts/noto-sans-sc-108-400-normal.woff2 +0 -0
  59. package/src/web-public/fonts/noto-sans-sc-109-400-normal.woff2 +0 -0
  60. package/src/web-public/fonts/noto-sans-sc-110-400-normal.woff2 +0 -0
  61. package/src/web-public/fonts/noto-sans-sc-111-400-normal.woff2 +0 -0
  62. package/src/web-public/fonts/noto-sans-sc-112-400-normal.woff2 +0 -0
  63. package/src/web-public/fonts/noto-sans-sc-113-400-normal.woff2 +0 -0
  64. package/src/web-public/fonts/noto-sans-sc-114-400-normal.woff2 +0 -0
  65. package/src/web-public/fonts/noto-sans-sc-115-400-normal.woff2 +0 -0
  66. package/src/web-public/fonts/noto-sans-sc-116-400-normal.woff2 +0 -0
  67. package/src/web-public/fonts/noto-sans-sc-117-400-normal.woff2 +0 -0
  68. package/src/web-public/fonts/noto-sans-sc-118-400-normal.woff2 +0 -0
  69. package/src/web-public/fonts/noto-sans-sc-119-400-normal.woff2 +0 -0
  70. package/src/web-public/fonts/noto-sans-sc-21-400-normal.woff2 +0 -0
  71. package/src/web-public/fonts/noto-sans-sc-22-400-normal.woff2 +0 -0
  72. package/src/web-public/fonts/noto-sans-sc-23-400-normal.woff2 +0 -0
  73. package/src/web-public/fonts/noto-sans-sc-24-400-normal.woff2 +0 -0
  74. package/src/web-public/fonts/noto-sans-sc-25-400-normal.woff2 +0 -0
  75. package/src/web-public/fonts/noto-sans-sc-26-400-normal.woff2 +0 -0
  76. package/src/web-public/fonts/noto-sans-sc-27-400-normal.woff2 +0 -0
  77. package/src/web-public/fonts/noto-sans-sc-28-400-normal.woff2 +0 -0
  78. package/src/web-public/fonts/noto-sans-sc-29-400-normal.woff2 +0 -0
  79. package/src/web-public/fonts/noto-sans-sc-30-400-normal.woff2 +0 -0
  80. package/src/web-public/fonts/noto-sans-sc-31-400-normal.woff2 +0 -0
  81. package/src/web-public/fonts/noto-sans-sc-32-400-normal.woff2 +0 -0
  82. package/src/web-public/fonts/noto-sans-sc-33-400-normal.woff2 +0 -0
  83. package/src/web-public/fonts/noto-sans-sc-34-400-normal.woff2 +0 -0
  84. package/src/web-public/fonts/noto-sans-sc-35-400-normal.woff2 +0 -0
  85. package/src/web-public/fonts/noto-sans-sc-36-400-normal.woff2 +0 -0
  86. package/src/web-public/fonts/noto-sans-sc-37-400-normal.woff2 +0 -0
  87. package/src/web-public/fonts/noto-sans-sc-38-400-normal.woff2 +0 -0
  88. package/src/web-public/fonts/noto-sans-sc-39-400-normal.woff2 +0 -0
  89. package/src/web-public/fonts/noto-sans-sc-4-400-normal.woff2 +0 -0
  90. package/src/web-public/fonts/noto-sans-sc-40-400-normal.woff2 +0 -0
  91. package/src/web-public/fonts/noto-sans-sc-41-400-normal.woff2 +0 -0
  92. package/src/web-public/fonts/noto-sans-sc-42-400-normal.woff2 +0 -0
  93. package/src/web-public/fonts/noto-sans-sc-43-400-normal.woff2 +0 -0
  94. package/src/web-public/fonts/noto-sans-sc-44-400-normal.woff2 +0 -0
  95. package/src/web-public/fonts/noto-sans-sc-45-400-normal.woff2 +0 -0
  96. package/src/web-public/fonts/noto-sans-sc-46-400-normal.woff2 +0 -0
  97. package/src/web-public/fonts/noto-sans-sc-47-400-normal.woff2 +0 -0
  98. package/src/web-public/fonts/noto-sans-sc-48-400-normal.woff2 +0 -0
  99. package/src/web-public/fonts/noto-sans-sc-49-400-normal.woff2 +0 -0
  100. package/src/web-public/fonts/noto-sans-sc-5-400-normal.woff2 +0 -0
  101. package/src/web-public/fonts/noto-sans-sc-50-400-normal.woff2 +0 -0
  102. package/src/web-public/fonts/noto-sans-sc-51-400-normal.woff2 +0 -0
  103. package/src/web-public/fonts/noto-sans-sc-52-400-normal.woff2 +0 -0
  104. package/src/web-public/fonts/noto-sans-sc-53-400-normal.woff2 +0 -0
  105. package/src/web-public/fonts/noto-sans-sc-54-400-normal.woff2 +0 -0
  106. package/src/web-public/fonts/noto-sans-sc-55-400-normal.woff2 +0 -0
  107. package/src/web-public/fonts/noto-sans-sc-56-400-normal.woff2 +0 -0
  108. package/src/web-public/fonts/noto-sans-sc-57-400-normal.woff2 +0 -0
  109. package/src/web-public/fonts/noto-sans-sc-58-400-normal.woff2 +0 -0
  110. package/src/web-public/fonts/noto-sans-sc-59-400-normal.woff2 +0 -0
  111. package/src/web-public/fonts/noto-sans-sc-6-400-normal.woff2 +0 -0
  112. package/src/web-public/fonts/noto-sans-sc-60-400-normal.woff2 +0 -0
  113. package/src/web-public/fonts/noto-sans-sc-61-400-normal.woff2 +0 -0
  114. package/src/web-public/fonts/noto-sans-sc-62-400-normal.woff2 +0 -0
  115. package/src/web-public/fonts/noto-sans-sc-63-400-normal.woff2 +0 -0
  116. package/src/web-public/fonts/noto-sans-sc-64-400-normal.woff2 +0 -0
  117. package/src/web-public/fonts/noto-sans-sc-65-400-normal.woff2 +0 -0
  118. package/src/web-public/fonts/noto-sans-sc-66-400-normal.woff2 +0 -0
  119. package/src/web-public/fonts/noto-sans-sc-67-400-normal.woff2 +0 -0
  120. package/src/web-public/fonts/noto-sans-sc-68-400-normal.woff2 +0 -0
  121. package/src/web-public/fonts/noto-sans-sc-69-400-normal.woff2 +0 -0
  122. package/src/web-public/fonts/noto-sans-sc-70-400-normal.woff2 +0 -0
  123. package/src/web-public/fonts/noto-sans-sc-71-400-normal.woff2 +0 -0
  124. package/src/web-public/fonts/noto-sans-sc-72-400-normal.woff2 +0 -0
  125. package/src/web-public/fonts/noto-sans-sc-73-400-normal.woff2 +0 -0
  126. package/src/web-public/fonts/noto-sans-sc-74-400-normal.woff2 +0 -0
  127. package/src/web-public/fonts/noto-sans-sc-75-400-normal.woff2 +0 -0
  128. package/src/web-public/fonts/noto-sans-sc-76-400-normal.woff2 +0 -0
  129. package/src/web-public/fonts/noto-sans-sc-77-400-normal.woff2 +0 -0
  130. package/src/web-public/fonts/noto-sans-sc-78-400-normal.woff2 +0 -0
  131. package/src/web-public/fonts/noto-sans-sc-79-400-normal.woff2 +0 -0
  132. package/src/web-public/fonts/noto-sans-sc-80-400-normal.woff2 +0 -0
  133. package/src/web-public/fonts/noto-sans-sc-81-400-normal.woff2 +0 -0
  134. package/src/web-public/fonts/noto-sans-sc-82-400-normal.woff2 +0 -0
  135. package/src/web-public/fonts/noto-sans-sc-83-400-normal.woff2 +0 -0
  136. package/src/web-public/fonts/noto-sans-sc-84-400-normal.woff2 +0 -0
  137. package/src/web-public/fonts/noto-sans-sc-85-400-normal.woff2 +0 -0
  138. package/src/web-public/fonts/noto-sans-sc-86-400-normal.woff2 +0 -0
  139. package/src/web-public/fonts/noto-sans-sc-87-400-normal.woff2 +0 -0
  140. package/src/web-public/fonts/noto-sans-sc-88-400-normal.woff2 +0 -0
  141. package/src/web-public/fonts/noto-sans-sc-89-400-normal.woff2 +0 -0
  142. package/src/web-public/fonts/noto-sans-sc-90-400-normal.woff2 +0 -0
  143. package/src/web-public/fonts/noto-sans-sc-91-400-normal.woff2 +0 -0
  144. package/src/web-public/fonts/noto-sans-sc-97-400-normal.woff2 +0 -0
  145. package/src/web-public/fonts/noto-sans-sc-98-400-normal.woff2 +0 -0
  146. package/src/web-public/fonts/noto-sans-sc-99-400-normal.woff2 +0 -0
  147. package/src/web-public/fonts/noto-sans-sc.css +0 -775
  148. package/src/web-public/fonts.css +0 -31
  149. package/src/web-public/home.css +0 -222
  150. package/src/web-public/home.html +0 -33
  151. package/src/web-public/home.js +0 -160
  152. package/src/web-public/i18n.js +0 -983
  153. package/src/web-public/index.html +0 -196
  154. package/src/web-public/login.css +0 -123
  155. package/src/web-public/login.html +0 -25
  156. package/src/web-public/login.js +0 -46
  157. package/src/web-public/settings.js +0 -290
  158. package/src/web-ws.js +0 -226
  159. package/src/web.js +0 -430
package/src/web-ws.js DELETED
@@ -1,226 +0,0 @@
1
- // Zero-dependency WebSocket server for the local web UI.
2
- //
3
- // The project ships with NO runtime dependencies (see package.json), so a
4
- // WebSocket upgrade cannot use the `ws` package. RFC 6455 is implemented here by
5
- // hand: the SHA-1 handshake uses node:crypto, and the data framing (text frames,
6
- // ~7-byte masks, 16/64-bit lengths) is a small loop.
7
- //
8
- // This is deliberately the smallest subset the web UI needs: a server that
9
- // accepts one text-message stream per connection, echoes nothing, and lets the
10
- // caller push string messages. No ping/pong, no fragmentation (the browser never
11
- // fragments), no binary. Compression is NOT negotiated, so the browser sends
12
- // plain frames — enough for our JSON events.
13
-
14
- import http from 'node:http';
15
- import crypto from 'node:crypto';
16
- import fs from 'node:fs';
17
- import os from 'node:os';
18
- import path from 'node:path';
19
- const KEEPALIVE_MS = 25000;
20
-
21
- const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
22
- // Hard cap on a single frame's payload. A malicious/formed peer can declare a
23
- // 64-bit length; allocating that much would throw RangeError and crash the
24
- // process, and buffering a slow trickle of an oversized frame would OOM.
25
- const MAX_FRAME = 64 * 1024 * 1024;
26
-
27
- /** A single accepted, open WebSocket connection. */
28
- export class WsConn {
29
- constructor(socket, onClose) {
30
- this.socket = socket;
31
- this._done = onClose; // internal close sink, fired once when the socket dies
32
- this.open = true;
33
- // The caller registers a message handler.
34
- this.onMessage = null;
35
- socket.on('data', (d) => this._onData(d));
36
- socket.on('close', () => this._close());
37
- socket.on('error', () => this._close());
38
- // Server->client keepalive: idle proxies (EasyTier, the daemon, nginx)
39
- // drop a silent WebSocket, the front-end reconnects and re-reads the whole
40
- // snapshot (page appears to reload itself). A repeated ping keeps the path
41
- // alive; the timer dies with the socket in _close().
42
- this._ka = setInterval(function () { try { this.ping(); } catch { /* closing */ } }.bind(this), KEEPALIVE_MS);
43
- if (this._ka.unref) this._ka.unref();
44
- }
45
-
46
- _buffer = Buffer.alloc(0);
47
-
48
- _onData(chunk) {
49
- this._buffer = Buffer.concat([this._buffer, chunk]);
50
- // Guard against a peer trickling an oversized frame: the incomplete-frame
51
- // path would otherwise keep appending to the buffer without bound.
52
- if (this._buffer.length > MAX_FRAME) { try { this._close(); } catch {} return; }
53
- // Try to parse as many complete frames as the buffer holds.
54
- for (;;) {
55
- const parsed = this._parseFrame(this._buffer);
56
- if (!parsed) return;
57
- const { consumed, data } = parsed;
58
- this._buffer = this._buffer.subarray(consumed);
59
- if (parsed.fatal) { try { this._close(); } catch {} return; }
60
- if (this.onMessage) {
61
- try { this.onMessage(data); } catch { /* a handler must not kill the conn */ }
62
- }
63
- }
64
- }
65
-
66
- /** Parse one WS frame from the front of `buf`. Returns null when the frame is
67
- incomplete; otherwise `{ consumed, data }` with `data` the UTF-8 string. */
68
- _parseFrame(buf) {
69
- if (buf.length < 2) return null;
70
- const fin = (buf[0] & 0x80) !== 0;
71
- const opcode = buf[0] & 0x0f;
72
- const masked = (buf[1] & 0x80) !== 0;
73
- let len = buf[1] & 0x7f;
74
- let off = 2;
75
- if (len === 126) {
76
- if (buf.length < off + 2) return null;
77
- len = buf.readUInt16BE(off);
78
- off += 2;
79
- } else if (len === 127) {
80
- if (buf.length < off + 8) return null;
81
- const hi = buf.readUInt32BE(off);
82
- const lo = buf.readUInt32BE(off + 4);
83
- len = hi * 4294967296 + lo;
84
- off += 8;
85
- }
86
- if (len > MAX_FRAME) return { consumed: 1, data: '', fatal: true };
87
- // Only masked text frames from a browser; ignore anything else gracefully.
88
- if (!masked) {
89
- // Not masked is a protocol error. A browser always masks, so this means
90
- // the stream is corrupted — flag it fatal and let _onData drop the
91
- // connection instead of guessing a frame length (which could swallow
92
- // following frames) or spinning forever (consumed:0 froze the server).
93
- return { consumed: 1, data: '', fatal: true };
94
- }
95
- const maskLen = 4;
96
- if (buf.length < off + maskLen) return null;
97
- const mask = buf.subarray(off, off + maskLen);
98
- off += maskLen;
99
- if (buf.length < off + len) return null;
100
- const payload = Buffer.alloc(len);
101
- for (let i = 0; i < len; i++) payload[i] = buf[off + i] ^ mask[i & 3];
102
- // opcode 0x9 = ping, 0xA = pong, 0x8 = close. We answer pings; skip the rest.
103
- if (!fin) return { consumed: off + len, data: '' }; // no fragmentation support
104
- let text = '';
105
- if (opcode === 0x1) { // text frame
106
- text = payload.toString('utf8');
107
- } else if (opcode === 0x9) { // ping -> pong
108
- this._writeFrame(0xA, payload);
109
- } else if (opcode === 0x8) { // close -> close back
110
- this._writeFrame(0x8, Buffer.alloc(0));
111
- this._close();
112
- }
113
- return { consumed: off + len, data: text };
114
- }
115
-
116
- /** Send a text message as an unmasked frame, per RFC 6455 (server->client
117
- frames are never masked). */
118
- send(text) {
119
- if (!this.open) return;
120
- this._writeFrame(0x1, Buffer.from(String(text), 'utf8'));
121
- }
122
-
123
- /** Send a ping control frame (opcode 0x9). The peer answers with a pong,
124
- keeping the socket and any proxy between us and the browser alive. */
125
- ping() {
126
- if (!this.open) return;
127
- this._writeFrame(0x9, Buffer.alloc(0));
128
- }
129
-
130
- _writeFrame(opcode, payload) {
131
- const len = payload.length;
132
- const header = [0x80 | opcode];
133
- if (len <= 125) {
134
- header.push(len);
135
- } else if (len <= 0xffff) {
136
- // RFC 6455: opcode+mask bit, then 126, then a 16-bit big-endian length.
137
- header.push(126);
138
- const b = Buffer.alloc(2);
139
- b.writeUInt16BE(len, 0);
140
- header.push(b[0], b[1]);
141
- } else {
142
- // 127, then a 64-bit big-endian length.
143
- header.push(127);
144
- const b = Buffer.alloc(8);
145
- b.writeUInt32BE(Math.floor(len / 4294967296), 0);
146
- b.writeUInt32BE(len >>> 0, 4);
147
- header.push(...b);
148
- }
149
- const buf = Buffer.concat([Buffer.from(header), payload]);
150
- try { this.socket.write(buf); } catch { /* closing */ }
151
- }
152
-
153
- close() {
154
- if (!this.open) return;
155
- try { this._writeFrame(0x8, Buffer.alloc(0)); } catch { /* closing */ }
156
- this._close();
157
- }
158
-
159
- _close() {
160
- if (this._ka) { clearInterval(this._ka); this._ka = null; }
161
- if (!this.open) return;
162
- this.open = false;
163
- try { this.socket.destroy(); } catch { /* already gone */ }
164
- if (this._done) { try { this._done(); } catch { /* best-effort */ } }
165
- }
166
- }
167
-
168
- /**
169
- * Attach a WebSocket upgrade handler to an http.Server.
170
- *
171
- * onConnection(conn) — called once a connection is accepted and open. `conn` is
172
- * a WsConn with `.send(text)` and `.close()`; set `conn.onMessage` to receive
173
- * client frames.
174
- * onUpgrade(req) — optional; return a string to reject (sent as the HTTP status
175
- * for the upgrade), or null to accept.
176
- */
177
- export function attachWebSocket(server, opts = {}) {
178
- const { path = '/api/ws', onConnection, onUpgrade } = opts;
179
- server.on('upgrade', (req, socket, head) => {
180
- const url = req.url || '/';
181
- const p = url.split('?')[0];
182
- if (p !== path) { socket.destroy(); return; }
183
- if (onUpgrade) {
184
- const reject = onUpgrade(req);
185
- if (reject) {
186
- socket.write(
187
- 'HTTP/1.1 403 Forbidden\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n' + reject,
188
- );
189
- socket.destroy();
190
- return;
191
- }
192
- }
193
- const key = req.headers['sec-websocket-key'];
194
- if (!key) { socket.destroy(); return; }
195
- const accept = crypto
196
- .createHash('sha1')
197
- .update(key + WS_MAGIC)
198
- .digest('base64');
199
- socket.write(
200
- 'HTTP/1.1 101 Switching Protocols\r\n'
201
- + 'Upgrade: websocket\r\n'
202
- + 'Connection: Upgrade\r\n'
203
- + `Sec-WebSocket-Accept: ${accept}\r\n\r\n`,
204
- );
205
- try {
206
- const conn = new WsConn(socket, () => {
207
- // The caller decides what to do when a connection drops.
208
- if (opts.onClose) opts.onClose(conn);
209
- });
210
- if (onConnection) onConnection(conn);
211
- } catch (e) {
212
- // A connection that fails to construct must not kill the process; log it.
213
- try { fs.appendFileSync(path.join(os.tmpdir(), 'hncode-web-ws.log'), '[ws] onConnection threw: ' + (e && e.message) + '\n'); } catch { /* best-effort */ }
214
- }
215
- });
216
- }
217
-
218
- /**
219
- * Convenience: is the incoming `req` a WebSocket upgrade for our path? Lets an
220
- * http server's normal request handler skip it (the upgrade event is handled
221
- * separately, so this is informational).
222
- */
223
- export function isUpgradeRequest(req) {
224
- const up = String(req.headers.upgrade || '').toLowerCase();
225
- return up === 'websocket';
226
- }
package/src/web.js DELETED
@@ -1,430 +0,0 @@
1
- // Local web UI server (/web) — serves the browser client and bridges it to a
2
- // running session through the SessionHub.
3
- //
4
- // Zero dependencies, like the rest of the project: node:http, a hand-written
5
- // router, Server-Sent Events for the server->browser direction and small JSON
6
- // POSTs for the other. SSE rather than WebSockets because the traffic is
7
- // one-directional and heavy (streamed tokens, tool output), it survives proxies,
8
- // and it needs no upgrade handshake or framing code.
9
- //
10
- // AUTH is enforced in ONE place — `handle()` — so a new route cannot accidentally
11
- // ship unauthenticated. The only unauthenticated routes are the login page itself
12
- // and the health check (which reveals nothing but "a server is here").
13
-
14
- import http from 'node:http';
15
- import fs from 'node:fs';
16
- import path from 'node:path';
17
- import { fileURLToPath } from 'node:url';
18
- import { newToken, isAuthed, isLoopback, sameOriginOk, safeEqual } from './web-auth.js';
19
- import { t, LANGUAGES, DEFAULT_LANG } from './i18n.js';
20
- // The session directory lives in web-daemon.js (it is the daemon that serves the
21
- // landing page listing every session). The session page needs the SAME list for
22
- // its left rail, so it imports the reader rather than growing a second one.
23
- import { listSavedSessions } from './web-daemon.js';
24
- import { attachWebSocket } from './web-ws.js';
25
- const HERE = path.dirname(fileURLToPath(import.meta.url));
26
- const PUBLIC_DIR = path.join(HERE, 'web-public');
27
- const COOKIE = 'hncode_web';
28
- const MAX_BODY = 1 << 20; // 1 MiB — a prompt, not a file upload
29
- const LOGIN_DELAY_MS = 400; // slows online guessing; costs a human nothing
30
-
31
- // Actions a browser may invoke. Shared by the HTTP /api/action handler AND the
32
- // WebSocket command channel, so registering a hub handler is never enough on its
33
- // own to expose it, and neither transport can drift from the other.
34
- const ALLOWED_ACTIONS = [
35
- // conversation
36
- 'submit', 'dispatch', 'interrupt', 'approve', 'answerPlan', 'answerQuestion', 'stopTask',
37
- 'setMode', 'shell', 'steer', 'editQueued', 'dropQueued',
38
- // Answering a prompt and moving a command to the background: the browser's Ctrl+G and Ctrl+B.
39
- 'stdinLine', 'detach',
40
- // files
41
- 'readFile', 'writeFile',
42
- // session settings
43
- 'setTitle', 'getConfig', 'setConfig',
44
- // providers and models
45
- 'listProviders', 'addProvider', 'removeProvider', 'discoverModels',
46
- 'listKnownProviders', 'importKnownProvider',
47
- ];
48
-
49
- /**
50
- * Pick a language for a server-rendered string. The browser's own choice lives in
51
- * localStorage (client side), so the server cannot read it; `Accept-Language` is
52
- * the next best signal and is what a curl client or a direct visit sends. An
53
- * explicit `?lang=` wins, so a link can pin the language.
54
- */
55
- function pickLang(req, url) {
56
- const q = url && url.searchParams.get('lang');
57
- if (q && LANGUAGES.some((l) => l.id === q)) return q;
58
- const header = String(req.headers['accept-language'] || '');
59
- for (const part of header.split(',')) {
60
- const id = part.split(';')[0].trim().toLowerCase().split('-')[0];
61
- if (LANGUAGES.some((l) => l.id === id)) return id;
62
- }
63
- return DEFAULT_LANG;
64
- }
65
-
66
- /** Read a static asset from web-public/. Returns null when missing/unsafe. */
67
- function readAsset(urlPath) {
68
- // Strip the query, decode, and REFUSE anything that escapes the directory.
69
- // Serving `../src/tui.js` here would hand out the source to an authed client —
70
- // not catastrophic, but a path traversal is a bug wherever it appears.
71
- let rel = decodeURIComponent(String(urlPath || '/').split('?')[0]);
72
- if (rel === '/' || rel === '') rel = '/index.html';
73
- const full = path.resolve(PUBLIC_DIR, '.' + rel);
74
- if (!full.startsWith(PUBLIC_DIR + path.sep)) return null;
75
- try {
76
- const st = fs.statSync(full);
77
- if (!st.isFile()) return null;
78
- return { body: fs.readFileSync(full), type: contentType(full), mtime: st.mtimeMs };
79
- } catch { return null; }
80
- }
81
-
82
- function contentType(file) {
83
- const ext = path.extname(file).toLowerCase();
84
- return ({
85
- '.html': 'text/html; charset=utf-8',
86
- '.css': 'text/css; charset=utf-8',
87
- '.js': 'text/javascript; charset=utf-8',
88
- '.json': 'application/json; charset=utf-8',
89
- '.svg': 'image/svg+xml',
90
- '.ico': 'image/x-icon',
91
- // The self-hosted faces under web-public/fonts. Without these the browser
92
- // receives `application/octet-stream` and refuses to apply the font.
93
- '.woff2': 'font/woff2',
94
- '.woff': 'font/woff',
95
- '.ttf': 'font/ttf',
96
- })[ext] || 'application/octet-stream';
97
- }
98
-
99
- function send(res, code, body, headers = {}) {
100
- const buf = Buffer.isBuffer(body) ? body : Buffer.from(String(body), 'utf8');
101
- res.writeHead(code, { 'content-length': buf.length, ...headers });
102
- res.end(buf);
103
- }
104
-
105
- function json(res, code, obj) {
106
- send(res, code, JSON.stringify(obj), { 'content-type': 'application/json; charset=utf-8' });
107
- }
108
-
109
- async function readBody(req) {
110
- return new Promise((resolve, reject) => {
111
- let size = 0;
112
- const chunks = [];
113
- req.on('data', (d) => {
114
- size += d.length;
115
- if (size > MAX_BODY) { reject(new Error('body too large')); req.destroy(); return; }
116
- chunks.push(d);
117
- });
118
- req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
119
- req.on('error', reject);
120
- });
121
- }
122
-
123
- /**
124
- * Start the web server.
125
- *
126
- * hub SessionHub — the shared session state
127
- * bindIp string — address to listen on (default 127.0.0.1)
128
- * port number — 0 asks the OS for a free port
129
- * token string — pre-set token (tests); otherwise generated
130
- * storeDir string — session store for /api/sessions (default: the user's)
131
- * onNotice (msg, kind) => void — surface startup info in the TUI transcript
132
- * @returns {Promise<{server, port, host, url, token, close}>}
133
- */
134
- export async function startWebServer(opts = {}) {
135
- const hub = opts.hub;
136
- if (!hub) throw new Error('startWebServer: hub is required');
137
- const bindIp = opts.bindIp || '127.0.0.1';
138
- const token = opts.token || newToken();
139
- const loopback = isLoopback(bindIp);
140
- // Hosts we accept in Origin. A non-loopback bind may be reached under any name
141
- // the client used, so the listen port is matched rather than a fixed hostname.
142
- let expectedHosts = [];
143
- const onNotice = typeof opts.onNotice === 'function' ? opts.onNotice : () => {};
144
-
145
- const server = http.createServer((req, res) => {
146
- handle(req, res).catch((e) => {
147
- try { json(res, 500, { ok: false, error: (e && e.message) || String(e) }); } catch { /* headers sent */ }
148
- });
149
- });
150
-
151
- // Live events over WebSocket (RFC 6455, zero deps). A client that upgrades to
152
- // /api/ws gets the current snapshot, then every hub event pushed as a JSON
153
- // line. Auth is checked in onUpgrade (cookie), so an unauthorised handshake is
154
- // rejected before a connection is made.
155
- const wsTargets = new Set();
156
- const wsBroadcast = (obj) => {
157
- const line = JSON.stringify(obj);
158
- for (const conn of wsTargets) { try { conn.send(line); } catch { /* closing */ } }
159
- };
160
- const wsOff = hub.subscribe(wsBroadcast);
161
- attachWebSocket(server, {
162
- onUpgrade: (req) => {
163
- // The browser carries the session cookie (standalone /web), while the
164
- // daemon reverse-proxy injects `Authorization: Bearer <session token>` —
165
- // isAuthed reads both, so a handshake through either path is accepted.
166
- if (!isAuthed(req, token, COOKIE)) return 'unauthorized';
167
- return null;
168
- },
169
- onConnection: (conn) => {
170
- wsTargets.add(conn);
171
- conn.send(JSON.stringify({ type: 'snapshot', ...hub.snapshot() }));
172
- // Bidirectional: a client text frame carries {action, args} and is run
173
- // through the SAME hub dispatch the HTTP /api/action path uses. This is
174
- // how the UI sends a message WITHOUT waiting on a blocking HTTP round
175
- // trip. The handler is run side-by-side (not awaited here) so the socket
176
- // never stalls on a long action; the result, if the caller wants one,
177
- // is pushed back as a matching frame below.
178
- conn.onMessage = (text) => {
179
- let req;
180
- try { req = JSON.parse(text); } catch { return; }
181
- const name = String((req && req.action) || '');
182
- if (!name) return;
183
- // Same allowlist as the HTTP path: a hub handler is not exposed just by
184
- // existing.
185
- if (!ALLOWED_ACTIONS.includes(name)) {
186
- try { conn.send(JSON.stringify({ type: 'ack', action: name, ok: false, error: `unknown action: ${name}` })); } catch {}
187
- return;
188
- }
189
- const args = Array.isArray(req && req.args) ? req.args : [];
190
- Promise.resolve(hub.call(name, ...args))
191
- .then((r) => {
192
- try { conn.send(JSON.stringify({ type: 'ack', action: name, ...(r || {}) })); } catch {}
193
- })
194
- .catch((err) => {
195
- try { conn.send(JSON.stringify({ type: 'ack', action: name, ok: false, error: (err && err.message) || String(err) })); } catch {}
196
- });
197
- };
198
- },
199
- onClose: (conn) => {
200
- wsTargets.delete(conn);
201
- },
202
- });
203
- async function handle(req, res) {
204
- const url = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`);
205
- const p = url.pathname;
206
- const method = (req.method || 'GET').toUpperCase();
207
-
208
- // --- unauthenticated ------------------------------------------------------
209
- // The login page and its assets must load before a token exists, and the login
210
- // script imports the message table — so i18n.js is served here too. NONE of
211
- // these files contain session data: they are the page, its styles, its script
212
- // and the UI strings.
213
- if (p === '/login' || p === '/login.html' || p === '/login.css'
214
- || p === '/login.js' || p === '/i18n.js'
215
- // The self-hosted faces. Neither file carries session data, and the login
216
- // page cannot render without them.
217
- || p === '/fonts.css' || p.startsWith('/fonts/')) {
218
- const asset = readAsset(p === '/login' ? '/login.html' : p);
219
- if (!asset) return send(res, 404, t(pickLang(req, url), 'http.notFound'));
220
- return send(res, 200, asset.body, { 'content-type': asset.type, 'cache-control': 'no-store' });
221
- }
222
- // Health check: reveals only that a server exists. Used by the TUI to confirm
223
- // the bind worked, and by the user to test reachability from another machine.
224
- if (p === '/healthz') return json(res, 200, { ok: true, service: 'hncode-web' });
225
-
226
-
227
- // --- token exchange ------------------------------------------------------
228
- // POST /api/login { token } -> sets an HttpOnly session cookie. The token is
229
- // accepted in the BODY, never the query string, so it does not land in the
230
- // server log or the browser history.
231
- if (p === '/api/login' && method === 'POST') {
232
- let body = {};
233
- try { body = JSON.parse(await readBody(req) || '{}'); } catch { /* handled below */ }
234
- if (!safeEqual(String(body.token || ''), token)) {
235
- await new Promise((r) => setTimeout(r, LOGIN_DELAY_MS));
236
- return json(res, 401, { ok: false, error: 'invalid token' });
237
- }
238
- const secure = !loopback && String(req.headers['x-forwarded-proto'] || '') === 'https';
239
- const cookie = [
240
- `${COOKIE}=${encodeURIComponent(token)}`,
241
- 'HttpOnly',
242
- 'SameSite=Strict',
243
- 'Path=/',
244
- // Not `Secure` on loopback: the browser would refuse to send it over http
245
- // and login would appear to succeed then fail on every later request.
246
- secure ? 'Secure' : '',
247
- `Max-Age=${60 * 60 * 12}`,
248
- ].filter(Boolean).join('; ');
249
- return send(res, 200, JSON.stringify({ ok: true }), {
250
- 'content-type': 'application/json; charset=utf-8',
251
- 'set-cookie': cookie,
252
- });
253
- }
254
-
255
- // --- everything below requires the token ---------------------------------
256
- if (!isAuthed(req, token, COOKIE)) {
257
- if (p.startsWith('/api/')) return json(res, 401, { ok: false, error: 'unauthorized' });
258
- // An unauthenticated page request goes to the login form, not a 401 body.
259
- if (method === 'GET') {
260
- res.writeHead(302, { location: '/login' });
261
- return res.end();
262
- }
263
- return send(res, 401, t(pickLang(req, url), 'http.unauthorized'));
264
- }
265
-
266
- // CSRF: a state-changing request must come from a page we served. Same-origin
267
- // requests carry a matching Origin; a missing Origin means a non-browser
268
- // client (curl, a test) that already proved it holds the token.
269
- if (method !== 'GET' && method !== 'HEAD') {
270
- const ok = sameOriginOk(req, expectedHosts);
271
- if (ok === false) return json(res, 403, { ok: false, error: 'cross-origin request refused' });
272
- }
273
-
274
- // --- API ------------------------------------------------------------------
275
- if (p === '/api/state') {
276
- // WHAT TO SEND, decided by the caller.
277
- //
278
- // `/api/state` used to answer with one fixed shape: every row, every tool result, the whole
279
- // transcript. That is what a browser wants and the wrong answer for a script asking "what did the
280
- // last turn do", which pays for the entire session to read the last few rows.
281
- //
282
- // ?detail=full every row, tool results included (the default: unchanged behaviour)
283
- // ?detail=summary drop tool RESULTS, keep the "Used Bash (cmd)" rows and the assistant text
284
- // ?detail=minimal assistant and user text only — no tool rows at all
285
- // ?turn=last only the most recent turn (a turn starts at a user message)
286
- // ?turn=<n> only turn n, counting from 1
287
- // ?limit=<n> at most n rows, taken from the end
288
- //
289
- // All of them compose, so `/api/state?detail=summary&turn=last` is one request for "what just
290
- // happened, and with which tools" without the payload of the results themselves.
291
- const detail = String(url.searchParams.get('detail') || 'full');
292
- const turn = url.searchParams.get('turn');
293
- const limit = Number(url.searchParams.get('limit'));
294
- const snap = hub.snapshot();
295
- let rows = snap.rows;
296
- // A turn begins at a user message; splitting on those is what makes "the last turn" mean the same
297
- // thing here as it does in the transcript.
298
- if (turn === 'last' || (turn && /^\d+$/.test(turn))) {
299
- const starts = [];
300
- rows.forEach((r, idx) => { if (r.role === 'user') starts.push(idx); });
301
- const which = turn === 'last' ? starts.length - 1 : Number(turn) - 1;
302
- if (which >= 0 && which < starts.length) {
303
- const from = starts[which];
304
- const to = which + 1 < starts.length ? starts[which + 1] : rows.length;
305
- rows = rows.slice(from, to);
306
- } else {
307
- rows = []; // no such turn: an empty list is the honest answer
308
- }
309
- }
310
- if (detail === 'summary' || detail === 'minimal') {
311
- // `summary` keeps the tool ROWS but clears their output: the caller wants to know which tools
312
- // ran, not what they printed. `minimal` drops the rows entirely.
313
- rows = rows.filter((r) => r.role !== 'tool' || detail === 'summary');
314
- if (detail === 'summary') {
315
- rows = rows.map((r) => (r.role === 'tool' ? { ...r, text: '', liveOutput: '', diff: undefined } : r));
316
- }
317
- }
318
- // Any row carrying a `card` is an interactive prompt; those are never trimmed by `minimal`.
319
- if (Number.isFinite(limit) && limit > 0) rows = rows.slice(-Math.floor(limit));
320
- return json(res, 200, { ok: true, ...snap, rows, hidden: snap.totalRows - rows.length, detail, turn: turn || null });
321
- }
322
-
323
- // The session list for the left rail. `listSavedSessions` already powers the
324
- // daemon's landing page, so a session page shows the same names in the same
325
- // order whether it was opened standalone or through the daemon. Entries are
326
- // metadata only — no transcript — so this stays cheap to poll.
327
- if (p === '/api/sessions') {
328
- const saved = listSavedSessions(opts.storeDir);
329
- // Which row is THIS page. It may not be on disk yet (a brand-new session
330
- // that has not been saved), so the current id is reported separately
331
- // instead of being inferred from the list.
332
- const current = String((hub.snapshot().status || {}).session || '');
333
- return json(res, 200, { ok: true, current, sessions: saved });
334
- }
335
-
336
-
337
- if (p === '/api/events') return sse(req, res, hub);
338
-
339
- if (p === '/api/action' && method === 'POST') {
340
- let body = {};
341
- try { body = JSON.parse((await readBody(req)) || '{}'); } catch {
342
- return json(res, 400, { ok: false, error: 'invalid JSON body' });
343
- }
344
- const name = String(body.action || '');
345
- const args = Array.isArray(body.args) ? body.args : [];
346
- // The security boundary: only these may be invoked from a browser. Kept as
347
- // an explicit list rather than derived from `hub.actions`, so registering a
348
- // handler on the hub is never enough on its own to expose it over HTTP.
349
- const ALLOWED = ALLOWED_ACTIONS;
350
- if (!ALLOWED.includes(name)) return json(res, 400, { ok: false, error: `unknown action: ${name}` });
351
- const result = await hub.call(name, ...args);
352
- return json(res, result.ok ? 200 : 409, result);
353
- }
354
-
355
- // A standalone /web serves exactly ONE session (this process's). The left
356
- // rail still links to `/s/<id>/` so the SAME page markup works behind the
357
- // daemon; standalone those links have no route and 404'd. Redirect them back
358
- // to the root, which is this session's page.
359
- if (p.startsWith('/s/')) {
360
- res.writeHead(302, { location: '/' });
361
- return res.end();
362
- }
363
-
364
- // --- static client ---------------------------------------------------------
365
- if (method === 'GET' || method === 'HEAD') {
366
- const asset = readAsset(p);
367
- if (!asset) return send(res, 404, t(pickLang(req, url), 'http.notFound'));
368
- return send(res, 200, method === 'HEAD' ? '' : asset.body, {
369
- 'content-type': asset.type,
370
- // The client is a handful of files served from disk; a no-store keeps the
371
- // browser from running a stale UI against a newer server (the mismatch
372
- // that makes a UI look broken after an update).
373
- 'cache-control': 'no-store',
374
- });
375
- }
376
- return send(res, 405, t(pickLang(req, url), 'http.badMethod'));
377
- }
378
-
379
- // --- Server-Sent Events -----------------------------------------------------
380
- function sse(req, res, hubRef) {
381
- res.writeHead(200, {
382
- 'content-type': 'text/event-stream; charset=utf-8',
383
- 'cache-control': 'no-store',
384
- connection: 'keep-alive',
385
- // Without this a proxy may buffer the whole stream and the UI looks frozen.
386
- 'x-accel-buffering': 'no',
387
- });
388
- const write = (obj) => {
389
- try { res.write(`data: ${JSON.stringify(obj)}\n\n`); } catch { /* closing */ }
390
- };
391
- // A late subscriber needs the current view FIRST, then the live stream.
392
- write({ type: 'snapshot', ...hubRef.snapshot() });
393
- const off = hubRef.subscribe(write);
394
- // Comment frames keep the connection alive through idle proxies and let the
395
- // server notice a dead peer on its own.
396
- const ping = setInterval(() => { try { res.write(': ping\n\n'); } catch { /* closing */ } }, 20000);
397
- const done = () => { clearInterval(ping); off(); try { res.end(); } catch { /* already gone */ } };
398
- req.on('close', done);
399
- req.on('error', done);
400
- }
401
-
402
- // --- listen -----------------------------------------------------------------
403
- const port = await new Promise((resolve, reject) => {
404
- server.once('error', reject);
405
- server.listen(opts.port == null ? 0 : opts.port, bindIp, () => resolve(server.address().port));
406
- }).catch((e) => {
407
- throw new Error(`could not bind ${bindIp}:${opts.port || 0} — ${e.message}`);
408
- });
409
-
410
- const hostForUrl = bindIp.includes(':') ? `[${bindIp}]` : bindIp;
411
- const url = `http://${hostForUrl}:${port}/`;
412
- expectedHosts = [`${bindIp}:${port}`, `localhost:${port}`, `127.0.0.1:${port}`];
413
- if (bindIp === '0.0.0.0' || bindIp === '::') expectedHosts.push(`0.0.0.0:${port}`);
414
-
415
- return {
416
- server,
417
- port,
418
- host: bindIp,
419
- url,
420
- token,
421
- loopback,
422
- close() {
423
- if (typeof wsOff === 'function') { try { wsOff(); } catch { /* best-effort */ } }
424
- for (const conn of wsTargets) { try { conn.close(); } catch { /* already gone */ } }
425
- try { server.close(); } catch { /* already closed */ }
426
- },
427
- };
428
- }
429
-
430
- export { PUBLIC_DIR };