@infernetprotocol/cli 0.1.22
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/LICENSE +15 -0
- package/commands/chat.js +268 -0
- package/commands/console.js +229 -0
- package/commands/debug.js +222 -0
- package/commands/deploy.js +524 -0
- package/commands/doctor.js +501 -0
- package/commands/firewall.js +39 -0
- package/commands/gpu.js +130 -0
- package/commands/help.js +54 -0
- package/commands/init.js +304 -0
- package/commands/login.js +305 -0
- package/commands/logs.js +93 -0
- package/commands/model.js +359 -0
- package/commands/payments.js +92 -0
- package/commands/payout.js +128 -0
- package/commands/pubkey.js +116 -0
- package/commands/register.js +304 -0
- package/commands/remove.js +230 -0
- package/commands/service.js +255 -0
- package/commands/setup.js +869 -0
- package/commands/start.js +903 -0
- package/commands/stats.js +85 -0
- package/commands/status.js +116 -0
- package/commands/stop.js +81 -0
- package/commands/train.js +563 -0
- package/commands/tui.js +285 -0
- package/commands/uncensored.js +222 -0
- package/commands/update.js +80 -0
- package/commands/upgrade.js +2 -0
- package/examples/app.js +153 -0
- package/index.js +207 -0
- package/lib/chat-executor.js +232 -0
- package/lib/config.js +148 -0
- package/lib/daemonize.js +46 -0
- package/lib/firewall.js +190 -0
- package/lib/hf-model.js +166 -0
- package/lib/identity.js +18 -0
- package/lib/ipc.js +114 -0
- package/lib/model-fit.js +67 -0
- package/lib/model-key.js +52 -0
- package/lib/network.js +240 -0
- package/lib/nip44.js +130 -0
- package/lib/node-client.js +152 -0
- package/lib/peers.js +133 -0
- package/lib/prompt.js +49 -0
- package/lib/remote-chat.js +115 -0
- package/lib/route.js +44 -0
- package/lib/runtime-config.js +130 -0
- package/lib/version.js +32 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Infernet Protocol
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/commands/chat.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `infernet chat` — run an inference, either through the P2P network
|
|
3
|
+
* (default when a control plane is configured) or against the local
|
|
4
|
+
* engine (Ollama/Mojo/stub).
|
|
5
|
+
*
|
|
6
|
+
* infernet chat "what is 2+2" # auto: P2P if configured, else local
|
|
7
|
+
* infernet chat --remote "..." # force network — control plane required
|
|
8
|
+
* infernet chat --local "..." # force local — no control plane call
|
|
9
|
+
*
|
|
10
|
+
* P2P path:
|
|
11
|
+
* POST <url>/api/chat → control plane creates job, picks
|
|
12
|
+
* a provider (or NIM fallback)
|
|
13
|
+
* GET <url>/api/chat/stream/<jobId> → SSE: job | meta | token | done | error
|
|
14
|
+
*
|
|
15
|
+
* Local path:
|
|
16
|
+
* createEngine() → Ollama / Mojo / stub → tokens straight to stdout.
|
|
17
|
+
*
|
|
18
|
+
* Exits 0 on `done`, non-zero on `error`.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { createEngine, MSG } from "@infernetprotocol/engine";
|
|
22
|
+
import { loadConfig } from "../lib/config.js";
|
|
23
|
+
import { submitChatJob, streamChatEvents } from "../lib/remote-chat.js";
|
|
24
|
+
|
|
25
|
+
const HELP = `infernet chat — run a chat inference (P2P network or local)
|
|
26
|
+
|
|
27
|
+
Usage:
|
|
28
|
+
infernet chat [flags] [prompt]
|
|
29
|
+
echo "..." | infernet chat [flags]
|
|
30
|
+
|
|
31
|
+
Routing:
|
|
32
|
+
--remote Force the P2P network (control plane required).
|
|
33
|
+
--local Force the local engine (Ollama / Mojo / stub).
|
|
34
|
+
(default) Use the network if config.controlPlane.url is
|
|
35
|
+
set; otherwise fall back to the local engine.
|
|
36
|
+
|
|
37
|
+
Common flags:
|
|
38
|
+
--model <name> Model id (e.g. qwen2.5:7b). $INFERNET_ENGINE_MODEL fallback.
|
|
39
|
+
--system <text> Prepend a system message.
|
|
40
|
+
--temperature <num> Sampling temperature.
|
|
41
|
+
--max-tokens <num> Cap on generated tokens.
|
|
42
|
+
--json Emit raw events as NDJSON instead of token stream.
|
|
43
|
+
-h, --help Show this help.
|
|
44
|
+
|
|
45
|
+
Network-only flags:
|
|
46
|
+
--url <url> Control-plane URL override (default: config.controlPlane.url).
|
|
47
|
+
|
|
48
|
+
Local-only flags:
|
|
49
|
+
--backend <kind> ollama | mojo | stub. Default: auto.
|
|
50
|
+
--host <url> Override Ollama host. Default: \$OLLAMA_HOST or localhost:11434.
|
|
51
|
+
`;
|
|
52
|
+
|
|
53
|
+
async function readStdin() {
|
|
54
|
+
if (process.stdin.isTTY) return "";
|
|
55
|
+
let buf = "";
|
|
56
|
+
process.stdin.setEncoding("utf8");
|
|
57
|
+
for await (const chunk of process.stdin) buf += chunk;
|
|
58
|
+
return buf;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function emitJson(obj) {
|
|
62
|
+
process.stdout.write(JSON.stringify(obj) + "\n");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function runRemote({ baseUrl, messages, model, temperature, maxTokens, jsonMode }) {
|
|
66
|
+
const ctrl = new AbortController();
|
|
67
|
+
const onSigint = () => {
|
|
68
|
+
process.stderr.write("\n[cancelled]\n");
|
|
69
|
+
ctrl.abort();
|
|
70
|
+
};
|
|
71
|
+
process.on("SIGINT", onSigint);
|
|
72
|
+
|
|
73
|
+
let exitCode = 0;
|
|
74
|
+
try {
|
|
75
|
+
let job;
|
|
76
|
+
try {
|
|
77
|
+
job = await submitChatJob(baseUrl, {
|
|
78
|
+
messages,
|
|
79
|
+
model,
|
|
80
|
+
maxTokens,
|
|
81
|
+
temperature,
|
|
82
|
+
signal: ctrl.signal
|
|
83
|
+
});
|
|
84
|
+
} catch (err) {
|
|
85
|
+
process.stderr.write(`error: failed to submit job to ${baseUrl}: ${err?.message ?? err}\n`);
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!process.stdout.isTTY || jsonMode) {
|
|
90
|
+
// No-op: in JSON / piped mode, header is omitted.
|
|
91
|
+
} else {
|
|
92
|
+
const provHint = job.provider
|
|
93
|
+
? `provider=${job.provider.name ?? job.provider.nodeId ?? job.provider.id}`
|
|
94
|
+
: "no provider";
|
|
95
|
+
process.stderr.write(
|
|
96
|
+
`[infernet] job=${job.jobId} via=${job.source} ${provHint}\n`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (jsonMode) emitJson({ type: "job", ...job });
|
|
101
|
+
|
|
102
|
+
for await (const ev of streamChatEvents(baseUrl, job.streamUrl, { signal: ctrl.signal })) {
|
|
103
|
+
if (jsonMode) {
|
|
104
|
+
emitJson(ev);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
switch (ev.event) {
|
|
108
|
+
case "token":
|
|
109
|
+
process.stdout.write(ev.data?.text ?? "");
|
|
110
|
+
break;
|
|
111
|
+
case "done":
|
|
112
|
+
if (process.stdout.isTTY) process.stdout.write("\n");
|
|
113
|
+
break;
|
|
114
|
+
case "error":
|
|
115
|
+
process.stderr.write(`\nerror: ${ev.data?.message ?? "remote error"}\n`);
|
|
116
|
+
exitCode = 1;
|
|
117
|
+
break;
|
|
118
|
+
default:
|
|
119
|
+
// job / meta / unknown — silent in plain mode
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} finally {
|
|
124
|
+
process.off("SIGINT", onSigint);
|
|
125
|
+
}
|
|
126
|
+
return exitCode;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function runLocal({ engineOpts, messages, model, temperature, maxTokens, jsonMode }) {
|
|
130
|
+
let engine;
|
|
131
|
+
try {
|
|
132
|
+
engine = await createEngine(engineOpts);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
process.stderr.write(`engine init failed: ${err?.message ?? err}\n`);
|
|
135
|
+
return 1;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const generation = engine.generate({
|
|
139
|
+
messages,
|
|
140
|
+
model,
|
|
141
|
+
temperature,
|
|
142
|
+
max_tokens: maxTokens
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const onSigint = () => {
|
|
146
|
+
process.stderr.write("\n[cancelled]\n");
|
|
147
|
+
generation.cancel();
|
|
148
|
+
};
|
|
149
|
+
process.on("SIGINT", onSigint);
|
|
150
|
+
|
|
151
|
+
let exitCode = 0;
|
|
152
|
+
try {
|
|
153
|
+
for await (const ev of generation.stream) {
|
|
154
|
+
if (jsonMode) {
|
|
155
|
+
emitJson(ev);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
switch (ev.type) {
|
|
159
|
+
case MSG.TOKEN:
|
|
160
|
+
process.stdout.write(ev.text ?? "");
|
|
161
|
+
break;
|
|
162
|
+
case MSG.DONE:
|
|
163
|
+
if (process.stdout.isTTY) process.stdout.write("\n");
|
|
164
|
+
break;
|
|
165
|
+
case MSG.ERROR:
|
|
166
|
+
process.stderr.write(`\nerror: ${ev.message ?? "engine error"}\n`);
|
|
167
|
+
exitCode = 1;
|
|
168
|
+
break;
|
|
169
|
+
default:
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} finally {
|
|
174
|
+
process.off("SIGINT", onSigint);
|
|
175
|
+
try { await engine.shutdown(); } catch { /* best-effort */ }
|
|
176
|
+
}
|
|
177
|
+
return exitCode;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export default async function chat(args) {
|
|
181
|
+
if (args.has("help") || args.has("h")) {
|
|
182
|
+
process.stdout.write(HELP);
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Reclaim values eaten by the greedy parser for known-boolean flags.
|
|
187
|
+
const reclaimed = [];
|
|
188
|
+
for (const name of ["json", "remote", "local"]) {
|
|
189
|
+
const v = args.get(name);
|
|
190
|
+
if (typeof v === "string") reclaimed.push(v);
|
|
191
|
+
}
|
|
192
|
+
const positional = [...reclaimed, ...(args.positional ?? [])];
|
|
193
|
+
let prompt = positional.join(" ").trim();
|
|
194
|
+
if (!prompt) prompt = (await readStdin()).trim();
|
|
195
|
+
if (!prompt) {
|
|
196
|
+
process.stderr.write("error: no prompt — pass as argument or pipe via stdin\n\n");
|
|
197
|
+
process.stderr.write(HELP);
|
|
198
|
+
return 2;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const messages = [];
|
|
202
|
+
const sys = args.get("system");
|
|
203
|
+
if (sys) messages.push({ role: "system", content: String(sys) });
|
|
204
|
+
messages.push({ role: "user", content: prompt });
|
|
205
|
+
|
|
206
|
+
const config = (await loadConfig()) ?? {};
|
|
207
|
+
const cfgEngine = config.engine ?? {};
|
|
208
|
+
const cfgUrl = config.controlPlane?.url ?? null;
|
|
209
|
+
|
|
210
|
+
// Mode resolution.
|
|
211
|
+
const wantRemote = args.has("remote");
|
|
212
|
+
const wantLocal = args.has("local");
|
|
213
|
+
if (wantRemote && wantLocal) {
|
|
214
|
+
process.stderr.write("error: --remote and --local are mutually exclusive\n");
|
|
215
|
+
return 2;
|
|
216
|
+
}
|
|
217
|
+
const urlArg = args.get("url");
|
|
218
|
+
const remoteUrl = (urlArg ? String(urlArg) : null) ?? cfgUrl;
|
|
219
|
+
let mode;
|
|
220
|
+
if (wantLocal) mode = "local";
|
|
221
|
+
else if (wantRemote) mode = "remote";
|
|
222
|
+
else mode = remoteUrl ? "remote" : "local";
|
|
223
|
+
|
|
224
|
+
const model =
|
|
225
|
+
args.get("model") ?? process.env.INFERNET_ENGINE_MODEL ?? cfgEngine.model ?? null;
|
|
226
|
+
const temperature =
|
|
227
|
+
args.get("temperature") != null ? Number(args.get("temperature")) : undefined;
|
|
228
|
+
const maxTokens =
|
|
229
|
+
args.get("max-tokens") != null ? Number(args.get("max-tokens")) : undefined;
|
|
230
|
+
const jsonMode = args.has("json");
|
|
231
|
+
|
|
232
|
+
if (mode === "remote") {
|
|
233
|
+
if (!remoteUrl) {
|
|
234
|
+
process.stderr.write(
|
|
235
|
+
"error: --remote requested but no control-plane URL configured.\n" +
|
|
236
|
+
" set one with `infernet init` or pass --url <https://...>\n"
|
|
237
|
+
);
|
|
238
|
+
return 2;
|
|
239
|
+
}
|
|
240
|
+
return runRemote({
|
|
241
|
+
baseUrl: remoteUrl,
|
|
242
|
+
messages,
|
|
243
|
+
model,
|
|
244
|
+
temperature,
|
|
245
|
+
maxTokens,
|
|
246
|
+
jsonMode
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Local path
|
|
251
|
+
const backendOpt =
|
|
252
|
+
args.get("backend") ?? process.env.INFERNET_ENGINE_BACKEND ?? cfgEngine.backend ?? null;
|
|
253
|
+
const host =
|
|
254
|
+
args.get("host") ?? process.env.OLLAMA_HOST ?? cfgEngine.ollamaHost ?? null;
|
|
255
|
+
const engineOpts = {};
|
|
256
|
+
if (backendOpt && backendOpt !== "auto") engineOpts.backend = String(backendOpt);
|
|
257
|
+
if (host) engineOpts.host = String(host);
|
|
258
|
+
if (model) engineOpts.defaultModel = model;
|
|
259
|
+
|
|
260
|
+
return runLocal({
|
|
261
|
+
engineOpts,
|
|
262
|
+
messages,
|
|
263
|
+
model,
|
|
264
|
+
temperature,
|
|
265
|
+
maxTokens,
|
|
266
|
+
jsonMode
|
|
267
|
+
});
|
|
268
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `infernet` (no args) → interactive console.
|
|
3
|
+
*
|
|
4
|
+
* Modeled on `claude` / `codex` / `hermes` with no args: drop into a
|
|
5
|
+
* REPL where the operator can chat, inspect, and tweak settings via
|
|
6
|
+
* slash commands. Plain text → one-shot chat against the configured
|
|
7
|
+
* model/node. Slash commands operate on the session.
|
|
8
|
+
*
|
|
9
|
+
* Slash commands:
|
|
10
|
+
* /help show this list
|
|
11
|
+
* /model show the active model
|
|
12
|
+
* /model <name> switch to a different model
|
|
13
|
+
* /model <name>@<pubkey> pin a specific provider node
|
|
14
|
+
* /models same as /models list
|
|
15
|
+
* /models list list models served on the network
|
|
16
|
+
* /status dump daemon + control plane status
|
|
17
|
+
* /clear clear the screen
|
|
18
|
+
* /quit | /exit | Ctrl-D leave the console
|
|
19
|
+
*
|
|
20
|
+
* If no config exists yet, the console invites the operator to run
|
|
21
|
+
* `infernet setup` and exits cleanly — never silently fails.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import readline from "node:readline";
|
|
25
|
+
import { loadConfig } from "../lib/config.js";
|
|
26
|
+
import { submitChatJob, streamChatEvents } from "../lib/remote-chat.js";
|
|
27
|
+
|
|
28
|
+
const RESET = "\x1b[0m";
|
|
29
|
+
const BOLD = "\x1b[1m";
|
|
30
|
+
const DIM = "\x1b[2m";
|
|
31
|
+
const ACCENT = "\x1b[36m";
|
|
32
|
+
|
|
33
|
+
export default async function consoleCmd(_args, ctx) {
|
|
34
|
+
const config = ctx?.config ?? (await loadConfig());
|
|
35
|
+
|
|
36
|
+
if (!config) {
|
|
37
|
+
process.stdout.write("\nNo Infernet config found.\n\n");
|
|
38
|
+
process.stdout.write("Run setup to install the daemon + register your node:\n");
|
|
39
|
+
process.stdout.write(` ${BOLD}infernet setup${RESET}\n\n`);
|
|
40
|
+
process.stdout.write("Or for a one-shot inference without a daemon:\n");
|
|
41
|
+
process.stdout.write(` ${BOLD}infernet chat --local "your prompt"${RESET}\n\n`);
|
|
42
|
+
return 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const session = {
|
|
46
|
+
model: config?.engine?.model ?? null,
|
|
47
|
+
node: null, // optional pinned-provider pubkey
|
|
48
|
+
controlPlaneUrl: config?.controlPlane?.url ?? "https://infernetprotocol.com"
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
showWelcome(session);
|
|
52
|
+
|
|
53
|
+
const rl = readline.createInterface({
|
|
54
|
+
input: process.stdin,
|
|
55
|
+
output: process.stdout,
|
|
56
|
+
prompt: prompt(session),
|
|
57
|
+
terminal: true,
|
|
58
|
+
historySize: 200
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
rl.prompt();
|
|
62
|
+
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
rl.on("line", async (line) => {
|
|
65
|
+
const input = line.trim();
|
|
66
|
+
if (!input) { rl.prompt(); return; }
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
if (input === "/quit" || input === "/exit") {
|
|
70
|
+
rl.close();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (input === "/help" || input === "/?") {
|
|
74
|
+
showHelp();
|
|
75
|
+
} else if (input === "/clear") {
|
|
76
|
+
process.stdout.write("\x1b[2J\x1b[H");
|
|
77
|
+
} else if (input === "/status") {
|
|
78
|
+
await printStatus(session);
|
|
79
|
+
} else if (input === "/model") {
|
|
80
|
+
process.stdout.write(`Active model: ${session.model ?? "(none — set with /model <name>)"}\n`);
|
|
81
|
+
if (session.node) process.stdout.write(`Pinned node: ${session.node}\n`);
|
|
82
|
+
} else if (input.startsWith("/model ")) {
|
|
83
|
+
setModel(session, input.slice("/model ".length).trim());
|
|
84
|
+
rl.setPrompt(prompt(session));
|
|
85
|
+
} else if (input === "/models" || input === "/models list") {
|
|
86
|
+
await listModels(session);
|
|
87
|
+
} else if (input.startsWith("/")) {
|
|
88
|
+
process.stderr.write(`Unknown slash command: ${input}. Try /help\n`);
|
|
89
|
+
} else {
|
|
90
|
+
// Plain text → one-shot chat.
|
|
91
|
+
await runChatOnce(session, input);
|
|
92
|
+
}
|
|
93
|
+
} catch (err) {
|
|
94
|
+
process.stderr.write(`error: ${err?.message ?? err}\n`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
rl.prompt();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
rl.on("close", () => {
|
|
101
|
+
process.stdout.write("\nbye.\n");
|
|
102
|
+
resolve(0);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Ctrl-C → cancel current line, don't exit
|
|
106
|
+
rl.on("SIGINT", () => {
|
|
107
|
+
process.stdout.write("\n(use /quit or Ctrl-D to leave)\n");
|
|
108
|
+
rl.prompt();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function prompt(session) {
|
|
114
|
+
const tag = session.model ? `${session.model}` : "no-model";
|
|
115
|
+
const pin = session.node ? `@${shortPubkey(session.node)}` : "";
|
|
116
|
+
return `${ACCENT}infernet${RESET} ${DIM}(${tag}${pin})${RESET} > `;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function shortPubkey(pk) {
|
|
120
|
+
if (!pk) return "";
|
|
121
|
+
if (pk.length <= 12) return pk;
|
|
122
|
+
return pk.slice(0, 6) + "…" + pk.slice(-4);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function showWelcome(session) {
|
|
126
|
+
process.stdout.write(`\n${BOLD}Infernet console${RESET} ${DIM}(/help for commands, /quit to leave)${RESET}\n`);
|
|
127
|
+
process.stdout.write(` control plane: ${session.controlPlaneUrl}\n`);
|
|
128
|
+
process.stdout.write(` model: ${session.model ?? "(none — /model <name> to set)"}\n\n`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function showHelp() {
|
|
132
|
+
process.stdout.write(`
|
|
133
|
+
${BOLD}Slash commands${RESET}
|
|
134
|
+
/help show this list
|
|
135
|
+
/model show the active model
|
|
136
|
+
/model <name> switch to a different model (e.g. qwen2.5:7b)
|
|
137
|
+
/model <name>@<pubkey> pin a specific provider node
|
|
138
|
+
/models list models served on the network
|
|
139
|
+
/models list same as above
|
|
140
|
+
/status daemon + control plane status
|
|
141
|
+
/clear clear the screen
|
|
142
|
+
/quit, /exit, Ctrl-D leave the console
|
|
143
|
+
|
|
144
|
+
${BOLD}Anything else${RESET} → sent as a chat message to the active model.
|
|
145
|
+
`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function setModel(session, arg) {
|
|
149
|
+
const at = arg.lastIndexOf("@");
|
|
150
|
+
if (at !== -1) {
|
|
151
|
+
session.model = arg.slice(0, at).trim();
|
|
152
|
+
session.node = arg.slice(at + 1).trim() || null;
|
|
153
|
+
} else {
|
|
154
|
+
session.model = arg;
|
|
155
|
+
session.node = null;
|
|
156
|
+
}
|
|
157
|
+
process.stdout.write(`✓ active model: ${session.model}${session.node ? `@${shortPubkey(session.node)}` : ""}\n`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function listModels(session) {
|
|
161
|
+
try {
|
|
162
|
+
const res = await fetch(`${session.controlPlaneUrl}/api/models`);
|
|
163
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
164
|
+
const body = await res.json();
|
|
165
|
+
const models = Array.isArray(body?.data) ? body.data : Array.isArray(body) ? body : [];
|
|
166
|
+
if (models.length === 0) {
|
|
167
|
+
process.stdout.write("No models advertised on the network right now.\n");
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
process.stdout.write(`\n${BOLD}Models served on the network${RESET}\n`);
|
|
171
|
+
for (const m of models) {
|
|
172
|
+
const name = m.name ?? m.model ?? "(unknown)";
|
|
173
|
+
const provs = m.providers ?? m.provider_count ?? "?";
|
|
174
|
+
const seen = m.freshest_seen ?? m.last_seen ?? "";
|
|
175
|
+
process.stdout.write(` ${name.padEnd(32)} ${String(provs).padEnd(4)} providers ${DIM}${seen}${RESET}\n`);
|
|
176
|
+
}
|
|
177
|
+
process.stdout.write("\n");
|
|
178
|
+
} catch (err) {
|
|
179
|
+
process.stderr.write(`/models failed: ${err?.message ?? err}\n`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function printStatus(session) {
|
|
184
|
+
try {
|
|
185
|
+
const res = await fetch(`${session.controlPlaneUrl}/api/overview`);
|
|
186
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
187
|
+
const body = await res.json();
|
|
188
|
+
process.stdout.write(`\nControl plane: ${session.controlPlaneUrl}\n`);
|
|
189
|
+
const cards = body?.cards ?? body?.data?.cards ?? [];
|
|
190
|
+
for (const c of cards) {
|
|
191
|
+
process.stdout.write(` ${String(c.label ?? c.title ?? "?").padEnd(20)} ${c.value ?? c.count ?? ""}\n`);
|
|
192
|
+
}
|
|
193
|
+
process.stdout.write("\n");
|
|
194
|
+
} catch (err) {
|
|
195
|
+
process.stderr.write(`/status failed: ${err?.message ?? err}\n`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function runChatOnce(session, prompt) {
|
|
200
|
+
if (!session.model) {
|
|
201
|
+
process.stderr.write("No model selected. Use /model <name> first (e.g. /model qwen2.5:7b).\n");
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const messages = [{ role: "user", content: prompt }];
|
|
205
|
+
const job = await submitChatJob({
|
|
206
|
+
url: session.controlPlaneUrl,
|
|
207
|
+
messages,
|
|
208
|
+
model: session.model,
|
|
209
|
+
provider_pubkey: session.node ?? undefined
|
|
210
|
+
});
|
|
211
|
+
if (!job?.jobId) {
|
|
212
|
+
process.stderr.write(`submit failed${job?.error ? `: ${job.error}` : ""}\n`);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
let any = false;
|
|
216
|
+
for await (const ev of streamChatEvents({ url: session.controlPlaneUrl, jobId: job.jobId })) {
|
|
217
|
+
if (ev.type === "token") {
|
|
218
|
+
process.stdout.write(ev.data?.text ?? "");
|
|
219
|
+
any = true;
|
|
220
|
+
} else if (ev.type === "done") {
|
|
221
|
+
if (any) process.stdout.write("\n");
|
|
222
|
+
return;
|
|
223
|
+
} else if (ev.type === "error") {
|
|
224
|
+
process.stderr.write(`\nerror: ${ev.data?.message ?? "unknown"}\n`);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (any) process.stdout.write("\n");
|
|
229
|
+
}
|