@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.
- package/README.md +30 -0
- package/README_CN.md +27 -0
- package/package.json +1 -1
- package/src/agent.js +172 -43
- package/src/bootstrap.js +63 -56
- package/src/ci.js +247 -247
- package/src/colors.js +5 -1
- package/src/commands.js +121 -0
- package/src/composer.js +31 -1
- package/src/config.js +177 -45
- package/src/context.js +29 -0
- package/src/files.js +32 -0
- package/src/fx.js +19 -2
- package/src/i18n.js +180 -131
- package/src/index.js +558 -77
- package/src/permission-gate.js +103 -0
- package/src/plugin.js +221 -2
- package/src/prompt-presets.js +36 -0
- package/src/protocol.js +184 -0
- package/src/registry-browser.js +14 -3
- package/src/remote.js +117 -5
- package/src/schedule.js +148 -11
- package/src/serve.js +613 -0
- package/src/session-client.js +148 -0
- package/src/session-host.js +52 -0
- package/src/session.js +26 -3
- package/src/settings-schema.js +2 -2
- package/src/status-line.js +48 -0
- package/src/term.js +20 -1
- package/src/toml.js +18 -4
- package/src/tool-line.js +13 -6
- package/src/tool-status.js +45 -0
- package/src/tools/bash.js +43 -24
- package/src/tools/edit.js +10 -5
- package/src/tools/web-search.js +8 -5
- package/src/tools/write.js +28 -3
- package/src/tui.js +498 -1397
- package/src/ws.js +248 -0
- package/src/session-hub.js +0 -445
- package/src/web-auth.js +0 -98
- package/src/web-daemon-cli.js +0 -63
- package/src/web-daemon-client.js +0 -203
- package/src/web-daemon.js +0 -894
- package/src/web-live.js +0 -250
- package/src/web-public/app.css +0 -1524
- package/src/web-public/app.js +0 -2969
- package/src/web-public/complete.js +0 -154
- package/src/web-public/fonts/JetBrainsMono-Bold.woff2 +0 -0
- package/src/web-public/fonts/JetBrainsMono-Regular.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-100-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-101-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-102-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-103-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-104-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-105-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-106-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-107-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-108-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-109-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-110-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-111-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-112-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-113-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-114-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-115-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-116-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-117-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-118-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-119-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-21-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-22-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-23-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-24-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-25-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-26-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-27-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-28-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-29-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-30-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-31-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-32-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-33-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-34-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-35-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-36-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-37-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-38-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-39-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-4-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-40-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-41-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-42-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-43-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-44-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-45-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-46-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-47-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-48-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-49-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-5-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-50-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-51-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-52-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-53-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-54-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-55-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-56-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-57-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-58-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-59-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-6-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-60-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-61-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-62-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-63-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-64-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-65-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-66-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-67-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-68-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-69-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-70-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-71-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-72-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-73-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-74-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-75-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-76-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-77-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-78-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-79-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-80-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-81-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-82-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-83-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-84-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-85-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-86-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-87-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-88-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-89-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-90-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-91-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-97-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-98-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-99-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc.css +0 -775
- package/src/web-public/fonts.css +0 -31
- package/src/web-public/home.css +0 -222
- package/src/web-public/home.html +0 -33
- package/src/web-public/home.js +0 -160
- package/src/web-public/i18n.js +0 -983
- package/src/web-public/index.html +0 -196
- package/src/web-public/login.css +0 -123
- package/src/web-public/login.html +0 -25
- package/src/web-public/login.js +0 -46
- package/src/web-public/settings.js +0 -290
- package/src/web-ws.js +0 -226
- package/src/web.js +0 -430
package/src/web-daemon-client.js
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
// TUI side of the global web UI.
|
|
2
|
-
//
|
|
3
|
-
// The user's model is: "/web" opens ONE web UI for this machine. If a daemon is
|
|
4
|
-
// already running, this session simply registers with it and does NOT start a
|
|
5
|
-
// second server — the existing one already holds the fixed port and token, and a
|
|
6
|
-
// second one could not bind that port anyway.
|
|
7
|
-
//
|
|
8
|
-
// So the flow is: probe -> (spawn if absent) -> register -> heartbeat. The
|
|
9
|
-
// session's own per-session server (web.js) still exists, because that is where
|
|
10
|
-
// the transcript and the actions live; the daemon only routes to it.
|
|
11
|
-
|
|
12
|
-
import net from 'node:net';
|
|
13
|
-
import path from 'node:path';
|
|
14
|
-
import fs from 'node:fs';
|
|
15
|
-
import os from 'node:os';
|
|
16
|
-
import cp from 'node:child_process';
|
|
17
|
-
import { fileURLToPath } from 'node:url';
|
|
18
|
-
import { resolveConfig } from './config.js';
|
|
19
|
-
import { HEARTBEAT_MS, defaultDaemonPipe } from './web-daemon.js';
|
|
20
|
-
|
|
21
|
-
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
22
|
-
|
|
23
|
-
/** Send one line-JSON request over the daemon pipe. Never throws. */
|
|
24
|
-
export function daemonCall(req, opts = {}) {
|
|
25
|
-
const pipePath = opts.pipePath || defaultDaemonPipe();
|
|
26
|
-
const timeoutMs = opts.timeoutMs || 2000;
|
|
27
|
-
return new Promise((resolve) => {
|
|
28
|
-
let done = false, sock = null;
|
|
29
|
-
const finish = (r) => {
|
|
30
|
-
if (done) return;
|
|
31
|
-
done = true;
|
|
32
|
-
clearTimeout(timer);
|
|
33
|
-
try { if (sock) sock.destroy(); } catch { /* already gone */ }
|
|
34
|
-
resolve(r);
|
|
35
|
-
};
|
|
36
|
-
const timer = setTimeout(() => finish({ ok: false, error: 'timeout' }), timeoutMs);
|
|
37
|
-
try { sock = net.connect(pipePath); }
|
|
38
|
-
catch (e) { finish({ ok: false, error: e.message }); return; }
|
|
39
|
-
let buf = '';
|
|
40
|
-
sock.on('connect', () => {
|
|
41
|
-
try { sock.write(JSON.stringify(req) + '\n'); }
|
|
42
|
-
catch (e) { finish({ ok: false, error: e.message }); }
|
|
43
|
-
});
|
|
44
|
-
sock.on('data', (d) => {
|
|
45
|
-
buf += d.toString('utf8');
|
|
46
|
-
const i = buf.indexOf('\n');
|
|
47
|
-
if (i < 0) return;
|
|
48
|
-
let parsed = null;
|
|
49
|
-
try { parsed = JSON.parse(buf.slice(0, i)); } catch { /* malformed */ }
|
|
50
|
-
finish(parsed || { ok: false, error: 'bad reply' });
|
|
51
|
-
});
|
|
52
|
-
sock.on('error', (e) => finish({ ok: false, error: e.message }));
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Is a daemon already listening on the pipe? */
|
|
57
|
-
export async function daemonAlive(opts = {}) {
|
|
58
|
-
const r = await daemonCall({ op: 'ping' }, opts);
|
|
59
|
-
return r && r.ok === true;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The daemon's own address, as it reports it. Use this instead of assuming the
|
|
64
|
-
* configured port: when the config says `web_port = 0` the OS picks one, and a
|
|
65
|
-
* guessed URL would point nowhere.
|
|
66
|
-
*/
|
|
67
|
-
export async function daemonInfo(opts = {}) {
|
|
68
|
-
const r = await daemonCall({ op: 'ping' }, opts);
|
|
69
|
-
if (!r || !r.ok) return null;
|
|
70
|
-
const host = r.host || '127.0.0.1';
|
|
71
|
-
return { host, port: r.port, sessions: r.sessions, url: `http://${host}:${r.port}/` };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Ask the daemon to shut down and release its port.
|
|
76
|
-
*
|
|
77
|
-
* Needed because the daemon is started DETACHED on purpose — `child.kill()` from
|
|
78
|
-
* the spawning process does not reach it once that process has moved on (which is
|
|
79
|
-
* what left orphaned daemons behind in testing). This is the supported way to
|
|
80
|
-
* stop one from outside.
|
|
81
|
-
*/
|
|
82
|
-
export async function stopDaemon(opts = {}) {
|
|
83
|
-
const r = await daemonCall({ op: 'shutdown' }, opts);
|
|
84
|
-
return !!(r && r.ok);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Start the daemon as a DETACHED process.
|
|
89
|
-
*
|
|
90
|
-
* Detached and `unref`ed on purpose: the daemon must outlive the terminal that
|
|
91
|
-
* happened to start it. Its own idle rule (exit once the last session
|
|
92
|
-
* deregisters) is what stops it from lingering forever.
|
|
93
|
-
*
|
|
94
|
-
* Returns once it answers on the pipe, or throws with what went wrong — a silent
|
|
95
|
-
* failure here would leave /web reporting success with nothing serving it.
|
|
96
|
-
*/
|
|
97
|
-
export async function spawnDaemon(opts = {}) {
|
|
98
|
-
const cfg = resolveConfig();
|
|
99
|
-
const pipePath = opts.pipePath || defaultDaemonPipe();
|
|
100
|
-
// An explicit host (from `/web <ip>`) wins over the config value, so the same
|
|
101
|
-
// command that asks for a network-reachable session server also binds the
|
|
102
|
-
// daemon to the network. Without this the daemon kept 127.0.0.1 and the
|
|
103
|
-
// browser's connection to the listed URL died with "no data sent".
|
|
104
|
-
const daemonHost = opts.host || cfg.webHost || '127.0.0.1';
|
|
105
|
-
const logFile = path.join(os.homedir(), '.hncode', 'web-daemon.log');
|
|
106
|
-
try { fs.mkdirSync(path.dirname(logFile), { recursive: true }); } catch { /* exists */ }
|
|
107
|
-
let out = 'ignore';
|
|
108
|
-
try { out = fs.openSync(logFile, 'a'); } catch { out = 'ignore'; }
|
|
109
|
-
|
|
110
|
-
const entry = path.join(HERE, 'web-daemon-cli.js');
|
|
111
|
-
if (!fs.existsSync(entry)) throw new Error(`daemon entry not found: ${entry}`);
|
|
112
|
-
|
|
113
|
-
const child = cp.spawn(process.execPath, [entry], {
|
|
114
|
-
detached: true,
|
|
115
|
-
stdio: ['ignore', out, out],
|
|
116
|
-
windowsHide: true,
|
|
117
|
-
env: {
|
|
118
|
-
...process.env,
|
|
119
|
-
HNCODE_WEB_PIPE: pipePath,
|
|
120
|
-
HNCODE_WEB_HOST: daemonHost,
|
|
121
|
-
HNCODE_WEB_PORT: String(cfg.webPort == null ? 8765 : cfg.webPort),
|
|
122
|
-
},
|
|
123
|
-
});
|
|
124
|
-
child.unref();
|
|
125
|
-
|
|
126
|
-
// Wait for it to answer. A bind failure (port taken by something else) shows up
|
|
127
|
-
// here as a timeout, which is the honest report: the daemon is not serving.
|
|
128
|
-
const deadline = Date.now() + (opts.waitMs || 8000);
|
|
129
|
-
let lastErr = 'did not start';
|
|
130
|
-
while (Date.now() < deadline) {
|
|
131
|
-
await new Promise((r) => setTimeout(r, 120));
|
|
132
|
-
const ping = await daemonCall({ op: 'ping' }, { pipePath, timeoutMs: 800 });
|
|
133
|
-
if (ping && ping.ok) return { spawned: true, child, pipePath, logFile };
|
|
134
|
-
if (ping && ping.error && ping.error !== 'timeout') lastErr = ping.error;
|
|
135
|
-
}
|
|
136
|
-
throw new Error(`web daemon did not start (${lastErr}); see ${logFile}`);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Attach this session to the global daemon.
|
|
141
|
-
*
|
|
142
|
-
* @param {object} info { id, port, token, title, workspace }
|
|
143
|
-
* `port`/`token` describe THIS session's own web.js server, which the daemon
|
|
144
|
-
* proxies to. Without them the daemon can only serve the session read-only.
|
|
145
|
-
* @returns {Promise<{ ok, spawned, reused, pipePath, heartbeat }>}
|
|
146
|
-
*/
|
|
147
|
-
export async function attachToDaemon(info, opts = {}) {
|
|
148
|
-
const pipePath = opts.pipePath || defaultDaemonPipe();
|
|
149
|
-
let spawned = false;
|
|
150
|
-
|
|
151
|
-
if (!(await daemonAlive({ pipePath }))) {
|
|
152
|
-
await spawnDaemon({ pipePath, waitMs: opts.waitMs, host: info.host });
|
|
153
|
-
spawned = true;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const reg = await daemonCall({
|
|
157
|
-
op: 'register',
|
|
158
|
-
id: info.id,
|
|
159
|
-
port: info.port,
|
|
160
|
-
token: info.token,
|
|
161
|
-
title: info.title || '',
|
|
162
|
-
workspace: info.workspace || '',
|
|
163
|
-
pid: process.pid,
|
|
164
|
-
}, { pipePath, timeoutMs: 4000 });
|
|
165
|
-
|
|
166
|
-
if (!reg || !reg.ok) {
|
|
167
|
-
throw new Error(`could not register with the web daemon: ${(reg && reg.error) || 'no reply'}`);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Heartbeat so a killed process is not shown as live forever. `unref` keeps the
|
|
171
|
-
// timer from holding the event loop open on its own.
|
|
172
|
-
const hb = setInterval(() => {
|
|
173
|
-
daemonCall({ op: 'touch', id: info.id }, { pipePath, timeoutMs: 1500 }).catch(() => {});
|
|
174
|
-
}, Math.max(5000, HEARTBEAT_MS));
|
|
175
|
-
if (hb.unref) hb.unref();
|
|
176
|
-
|
|
177
|
-
const detach = () => {
|
|
178
|
-
clearInterval(hb);
|
|
179
|
-
daemonCall({ op: 'unregister', id: info.id }, { pipePath, timeoutMs: 1500 }).catch(() => {});
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
// Ask the daemon where it actually is, rather than trusting the configured
|
|
183
|
-
// port: `web_port = 0` means the OS chose one.
|
|
184
|
-
const addr = await daemonInfo({ pipePath, timeoutMs: 2000 });
|
|
185
|
-
const host = (addr && addr.host) || '127.0.0.1';
|
|
186
|
-
const port = (addr && addr.port) != null ? addr.port : null;
|
|
187
|
-
const url = port == null ? null : `http://${host}:${port}/s/${encodeURIComponent(info.id)}/`;
|
|
188
|
-
|
|
189
|
-
return {
|
|
190
|
-
ok: true,
|
|
191
|
-
spawned,
|
|
192
|
-
reused: !spawned,
|
|
193
|
-
pipePath,
|
|
194
|
-
host,
|
|
195
|
-
port,
|
|
196
|
-
// The per-SESSION URL the user should open. The daemon's root lists every
|
|
197
|
-
// session, so both are reported and the TUI can print whichever it wants.
|
|
198
|
-
url,
|
|
199
|
-
rootUrl: port == null ? null : `http://${host}:${port}/`,
|
|
200
|
-
heartbeat: hb,
|
|
201
|
-
detach,
|
|
202
|
-
};
|
|
203
|
-
}
|