dsh-netchat 0.0.0-stage → 0.3.19
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 +147 -2
- package/client/client.js +696 -0
- package/cordis.patch.yml +4 -0
- package/lib/host.mjs +234 -0
- package/lib/index.js +243 -0
- package/lib/runtime.mjs +9 -0
- package/lib/shared.mjs +28 -0
- package/package.json +34 -4
- package/shared/core/client.mjs +301 -0
- package/shared/core/consts.mjs +71 -0
- package/shared/core/datadir.mjs +92 -0
- package/shared/core/feishu-frame.mjs +190 -0
- package/shared/core/feishu.mjs +878 -0
- package/shared/core/hostbridge.mjs +115 -0
- package/shared/core/netchat.mjs +2542 -0
- package/shared/core/ops.mjs +803 -0
- package/shared/core/protocol.mjs +132 -0
- package/shared/core/util.mjs +184 -0
- package/shared/core/wsstream.mjs +204 -0
- package/shared/mcp/server.mjs +212 -0
- package/shared/skills/net-chat/SKILL.md +110 -0
- package/shared/ui/panel.html +2652 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Host capabilities over the loopback, so the one LAN service can act for either host.
|
|
2
|
+
//
|
|
3
|
+
// The ZCode plugin and the DeepSeek Harness plugin share one LAN service on a machine: whichever
|
|
4
|
+
// starts first owns it, and the other's panel borrows it. But "list my projects" and "open a
|
|
5
|
+
// session" are things only a host can do for itself — the owner's host would answer with the
|
|
6
|
+
// owner's projects and start sessions in the owner. So every process that has a host opens a
|
|
7
|
+
// small loopback endpoint exposing that host, and publishes its address under its label in the
|
|
8
|
+
// records directory. A call carries the caller's label; the service then uses that host —
|
|
9
|
+
// through this bridge when it is another process's — for everything bound to the task.
|
|
10
|
+
import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { createServer } from "node:http";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
|
|
14
|
+
import { fetchJson } from "./util.mjs";
|
|
15
|
+
|
|
16
|
+
/** The host methods the service uses; the bridge refuses anything else. */
|
|
17
|
+
export const BRIDGED_METHODS = ["listProjects", "listModels", "listSessions", "createSession", "renameSession", "openSession", "sendToSession", "readSession", "sessionInfo", "visibleSessions"];
|
|
18
|
+
|
|
19
|
+
const newToken = () => Array.from({ length: 32 }, () => Math.floor(Math.random() * 256).toString(16).padStart(2, "0")).join("");
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Serves a host on the loopback. `dataDir` is where the address is published, under
|
|
23
|
+
* `hosts/<label>.json`, so another process can find it by label alone — a task remembers the
|
|
24
|
+
* label, not the port, which changes with every restart.
|
|
25
|
+
*/
|
|
26
|
+
export async function serveHost({ host, label, dataDir, logger = console }) {
|
|
27
|
+
const token = newToken();
|
|
28
|
+
const server = createServer((req, res) => {
|
|
29
|
+
void (async () => {
|
|
30
|
+
const reply = (status, body) => {
|
|
31
|
+
res.writeHead(status, { "content-type": "application/json", connection: "close" });
|
|
32
|
+
res.end(JSON.stringify(body));
|
|
33
|
+
};
|
|
34
|
+
if (req.method !== "POST" || req.headers.authorization !== `Bearer ${token}`) return reply(403, { ok: false });
|
|
35
|
+
const method = decodeURIComponent((req.url ?? "").replace(/^\/__host\//, ""));
|
|
36
|
+
if (!BRIDGED_METHODS.includes(method) || typeof host[method] !== "function") return reply(404, { ok: false, error: `not bridged: ${method}` });
|
|
37
|
+
let raw = "";
|
|
38
|
+
for await (const chunk of req) raw += chunk;
|
|
39
|
+
if (raw.length > 8 * 1024 * 1024) return reply(413, { ok: false });
|
|
40
|
+
try {
|
|
41
|
+
const { args } = raw ? JSON.parse(raw) : {};
|
|
42
|
+
const result = await host[method](...(Array.isArray(args) ? args : []));
|
|
43
|
+
return reply(200, { ok: true, result });
|
|
44
|
+
} catch (error) {
|
|
45
|
+
// The error's code and extras travel too: callers turn them into NetChat errors as before.
|
|
46
|
+
const { code, message, ...extra } = error ?? {};
|
|
47
|
+
return reply(200, { ok: false, error: { code: code ?? "host_error", message: message ?? String(error), ...extra } });
|
|
48
|
+
}
|
|
49
|
+
})().catch((error) => logger.warn?.(`host bridge: ${error?.message ?? error}`));
|
|
50
|
+
});
|
|
51
|
+
await new Promise((resolve, reject) => {
|
|
52
|
+
server.once("error", reject);
|
|
53
|
+
server.listen(0, "127.0.0.1", resolve);
|
|
54
|
+
});
|
|
55
|
+
const { port } = server.address();
|
|
56
|
+
const dir = join(dataDir, "hosts");
|
|
57
|
+
mkdirSync(dir, { recursive: true });
|
|
58
|
+
const file = join(dir, `${label}.json`);
|
|
59
|
+
// What this host actually has, so a borrower's capabilities for it are true (a host without
|
|
60
|
+
// `listModels` must not show as offering a model picker, see capabilitiesOf in netchat.mjs).
|
|
61
|
+
writeFileSync(file, JSON.stringify({ port, token, label, pid: process.pid, methods: BRIDGED_METHODS.filter((method) => typeof host[method] === "function") }));
|
|
62
|
+
const close = () => new Promise((resolve) => {
|
|
63
|
+
server.close(() => resolve());
|
|
64
|
+
try {
|
|
65
|
+
// Only the file this process wrote is removed; a restarted process may have replaced it.
|
|
66
|
+
if (JSON.parse(readFileSync(file, "utf8")).pid === process.pid) unlinkSync(file);
|
|
67
|
+
} catch {
|
|
68
|
+
// Already gone.
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return { label, port, token, close };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A host reached through another process's bridge, with the same methods as a local one. Only
|
|
76
|
+
* the methods the other host said it has are put on (its address file lists them); one it lacks
|
|
77
|
+
* is absent here too, which is how the service tells what that host can do.
|
|
78
|
+
*/
|
|
79
|
+
export function bridgedHost({ label, port, token, methods }) {
|
|
80
|
+
const call = (method) => async (...args) => {
|
|
81
|
+
const outcome = await fetchJson(`http://127.0.0.1:${port}/__host/${method}`, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
body: JSON.stringify({ args }),
|
|
84
|
+
headers: { authorization: `Bearer ${token}` },
|
|
85
|
+
timeoutMs: 120_000,
|
|
86
|
+
}).catch((error) => {
|
|
87
|
+
throw Object.assign(new Error(`host "${label}" is not reachable: ${error?.message ?? error}`), { code: "host_unreachable" });
|
|
88
|
+
});
|
|
89
|
+
const data = outcome.data;
|
|
90
|
+
if (!data?.ok) {
|
|
91
|
+
// The served end refuses a method it does not carry with a plain string (`not bridged: x`).
|
|
92
|
+
const error = typeof data?.error === "string" ? { message: data.error } : (data?.error ?? {});
|
|
93
|
+
const { code, message, ...extra } = error;
|
|
94
|
+
throw Object.assign(new Error(message ?? `host "${label}" failed`), { code: code ?? "host_error", ...extra });
|
|
95
|
+
}
|
|
96
|
+
return data.result;
|
|
97
|
+
};
|
|
98
|
+
const offered = Array.isArray(methods) ? BRIDGED_METHODS.filter((method) => methods.includes(method)) : BRIDGED_METHODS;
|
|
99
|
+
return { label, bridged: true, ...Object.fromEntries(offered.map((method) => [method, call(method)])) };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The host named by a label: this process's own when it is, otherwise its bridge found by label.
|
|
104
|
+
* Returns null for a label that is not around any more, so the caller can say so.
|
|
105
|
+
*/
|
|
106
|
+
export function hostByLabel(label, { dataDir, ownLabel, ownHost }) {
|
|
107
|
+
if (!label) return ownHost;
|
|
108
|
+
if (label === ownLabel) return ownHost;
|
|
109
|
+
try {
|
|
110
|
+
const info = JSON.parse(readFileSync(join(dataDir, "hosts", `${label}.json`), "utf8"));
|
|
111
|
+
return Number.isInteger(info.port) && typeof info.token === "string" ? bridgedHost(info) : null;
|
|
112
|
+
} catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|