nimblebrain 0.3.0 → 0.5.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 +10 -4
- package/dist/nimblebrain.js +836 -314
- package/package.json +1 -1
package/dist/nimblebrain.js
CHANGED
|
@@ -5,10 +5,12 @@ import { writeSync } from "node:fs";
|
|
|
5
5
|
|
|
6
6
|
// src/deps.ts
|
|
7
7
|
import { spawn } from "node:child_process";
|
|
8
|
-
import {
|
|
8
|
+
import { constants } from "node:fs";
|
|
9
|
+
import { access, chmod, mkdir, readFile, rm, rmdir, writeFile } from "node:fs/promises";
|
|
9
10
|
import { createServer } from "node:net";
|
|
10
11
|
import { homedir } from "node:os";
|
|
11
|
-
import { dirname, join, resolve } from "node:path";
|
|
12
|
+
import { delimiter, dirname, join, resolve } from "node:path";
|
|
13
|
+
var LAUNCH_WAIT_MS = 2000;
|
|
12
14
|
function realDeps() {
|
|
13
15
|
return {
|
|
14
16
|
exec(cmd, args, opts = {}) {
|
|
@@ -35,6 +37,41 @@ function realDeps() {
|
|
|
35
37
|
child.on("close", (code) => resolve({ code, stdout, stderr, missing }));
|
|
36
38
|
});
|
|
37
39
|
},
|
|
40
|
+
launch(cmd, args) {
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
let child;
|
|
43
|
+
try {
|
|
44
|
+
child = spawn(cmd, args, { stdio: "ignore", detached: true });
|
|
45
|
+
} catch {
|
|
46
|
+
resolve(false);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
child.unref();
|
|
51
|
+
resolve(true);
|
|
52
|
+
}, LAUNCH_WAIT_MS);
|
|
53
|
+
child.once("error", () => {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
resolve(false);
|
|
56
|
+
});
|
|
57
|
+
child.once("exit", (code) => {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
resolve(code === 0);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
async which(cmd) {
|
|
64
|
+
const exts = process.platform === "win32" ? (process.env.PATHEXT ?? ".EXE").split(";") : [""];
|
|
65
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter).filter(Boolean)) {
|
|
66
|
+
for (const ext of exts) {
|
|
67
|
+
try {
|
|
68
|
+
await access(join(dir, cmd + ext), constants.X_OK);
|
|
69
|
+
return true;
|
|
70
|
+
} catch {}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
},
|
|
38
75
|
portFree(port) {
|
|
39
76
|
return new Promise((resolve) => {
|
|
40
77
|
const server = createServer();
|
|
@@ -78,6 +115,7 @@ function realDeps() {
|
|
|
78
115
|
`);
|
|
79
116
|
},
|
|
80
117
|
env: process.env,
|
|
118
|
+
platform: process.platform,
|
|
81
119
|
home: homedir(),
|
|
82
120
|
cwd: process.cwd()
|
|
83
121
|
};
|
|
@@ -154,6 +192,19 @@ function typeHidden(value, chunk) {
|
|
|
154
192
|
function installHome(deps, flag) {
|
|
155
193
|
return resolve(deps.cwd, flag ?? deps.env.NIMBLEBRAIN_HOME ?? join(deps.home, ".nimblebrain-local"));
|
|
156
194
|
}
|
|
195
|
+
var BARE_CLI = "npx nimblebrain";
|
|
196
|
+
function invocation(deps, home) {
|
|
197
|
+
const fallback = resolve(deps.cwd, join(deps.home, ".nimblebrain-local"));
|
|
198
|
+
if (home === fallback && installHome(deps) === fallback)
|
|
199
|
+
return BARE_CLI;
|
|
200
|
+
return `${BARE_CLI} --home ${shellQuote(home)}`;
|
|
201
|
+
}
|
|
202
|
+
function shellQuote(s) {
|
|
203
|
+
return /^[A-Za-z0-9_./,:@%+=-]+$/.test(s) ? s : `'${s.replaceAll("'", `'\\''`)}'`;
|
|
204
|
+
}
|
|
205
|
+
function forInstall(text, cli) {
|
|
206
|
+
return text.replaceAll(BARE_CLI, () => cli);
|
|
207
|
+
}
|
|
157
208
|
|
|
158
209
|
// src/args.ts
|
|
159
210
|
import { parseArgs } from "node:util";
|
|
@@ -196,59 +247,327 @@ var ERRORS = {
|
|
|
196
247
|
NAME_IS_CATALOG_ENTRY: "Pass another name with --name; this one belongs to a catalog connector.",
|
|
197
248
|
REQUEST_FAILED: "Run `npx nimblebrain logs` and look at the last error line, then run this again."
|
|
198
249
|
};
|
|
250
|
+
var FIX_VARIANTS = {
|
|
251
|
+
LOCAL_MODEL_UNREACHABLE: {
|
|
252
|
+
brew_install: "Install Ollama with `brew install ollama`, then run this again; it offers to start Ollama for you.",
|
|
253
|
+
app: "Open the Ollama app, then run this again.",
|
|
254
|
+
brew: "Run `brew services start ollama` (it then also starts at login), then run this again.",
|
|
255
|
+
systemd: "Run `sudo systemctl start ollama`, then run this again.",
|
|
256
|
+
serve: "Run `ollama serve` in another terminal, then run this again."
|
|
257
|
+
}
|
|
258
|
+
};
|
|
199
259
|
|
|
200
260
|
class CliError extends Error {
|
|
201
261
|
code;
|
|
202
262
|
fix;
|
|
203
263
|
detail;
|
|
204
|
-
|
|
264
|
+
variant;
|
|
265
|
+
constructor(code, message, detail = {}, variant) {
|
|
205
266
|
super(message ?? code);
|
|
206
267
|
this.code = code;
|
|
207
|
-
|
|
268
|
+
const variants = FIX_VARIANTS[code];
|
|
269
|
+
this.fix = variant && variants?.[variant] || ERRORS[code];
|
|
270
|
+
this.variant = variant;
|
|
208
271
|
this.detail = detail;
|
|
209
272
|
}
|
|
273
|
+
with(detail, message = this.message) {
|
|
274
|
+
return new CliError(this.code, message, { ...this.detail, ...detail }, this.variant);
|
|
275
|
+
}
|
|
210
276
|
}
|
|
211
277
|
function failure(err) {
|
|
212
278
|
return { ...err.detail, ok: false, error: err.code, message: err.message, fix: err.fix };
|
|
213
279
|
}
|
|
214
280
|
function exitCodeFor(result) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
281
|
+
return result.ok ? 0 : exitCodeOf(result.error);
|
|
282
|
+
}
|
|
283
|
+
function exitCodeOf(code) {
|
|
284
|
+
return code === "USAGE" ? 2 : 1;
|
|
218
285
|
}
|
|
219
286
|
|
|
220
|
-
// src/
|
|
221
|
-
var
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
"
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
"
|
|
233
|
-
|
|
287
|
+
// src/local-model.ts
|
|
288
|
+
var OLLAMA_URL = "http://127.0.0.1:11434";
|
|
289
|
+
var OLLAMA_FROM_CONTAINER = "http://host.docker.internal:11434/v1";
|
|
290
|
+
var DEFAULT_LOCAL_MODEL = "gemma4:e4b";
|
|
291
|
+
var LOCAL_CONTEXT = 32768;
|
|
292
|
+
var PULL_TIMEOUT_MS = 60 * 60 * 1000;
|
|
293
|
+
var OLLAMA_START_TIMEOUT_MS = 30000;
|
|
294
|
+
var CREATE_TIMEOUT_MS = 2 * 60 * 1000;
|
|
295
|
+
function takesLocalModel(composeText) {
|
|
296
|
+
return composeText.includes("NB_LOCAL_MODEL_URL");
|
|
297
|
+
}
|
|
298
|
+
function nimblebrainModelName(model) {
|
|
299
|
+
return `nimblebrain-${model.replace(/[^A-Za-z0-9._-]+/g, "-")}`;
|
|
300
|
+
}
|
|
301
|
+
function localModelOf(envText) {
|
|
302
|
+
const line = envText.split(`
|
|
303
|
+
`).find((l) => l.startsWith("NB_LOCAL_MODEL="));
|
|
304
|
+
const value = line?.slice("NB_LOCAL_MODEL=".length).trim();
|
|
305
|
+
return value ? value : undefined;
|
|
306
|
+
}
|
|
307
|
+
async function show(deps, model) {
|
|
308
|
+
const res = await deps.postJson(`${OLLAMA_URL}/api/show`, { model });
|
|
309
|
+
return res?.status === 200 ? res.body : undefined;
|
|
310
|
+
}
|
|
311
|
+
function numCtxOf(parameters) {
|
|
312
|
+
const match = parameters?.match(/^num_ctx\s+(\d+)/m);
|
|
313
|
+
return match ? Number(match[1]) : undefined;
|
|
314
|
+
}
|
|
315
|
+
async function ollamaVersion(deps) {
|
|
316
|
+
const res = await deps.getJson(`${OLLAMA_URL}/api/version`);
|
|
317
|
+
const version = res?.body?.version;
|
|
318
|
+
return res?.status === 200 && typeof version === "string" ? version : undefined;
|
|
319
|
+
}
|
|
320
|
+
var SYSTEMD_UNITS = [
|
|
321
|
+
"/etc/systemd/system/ollama.service",
|
|
322
|
+
"/usr/lib/systemd/system/ollama.service"
|
|
234
323
|
];
|
|
324
|
+
async function findOllama(deps) {
|
|
325
|
+
const apps = ["/Applications", `${deps.home}/Applications`].map((dir) => `${dir}/Ollama.app/Contents/Info.plist`);
|
|
326
|
+
if (deps.platform === "darwin" && await anyFile(deps, apps))
|
|
327
|
+
return { kind: "app", start: ["open", "-g", "-a", "Ollama"], fix: "app" };
|
|
328
|
+
if (deps.platform === "linux" && await anyFile(deps, SYSTEMD_UNITS))
|
|
329
|
+
return {
|
|
330
|
+
kind: "systemd",
|
|
331
|
+
start: ["systemctl", "start", "--no-ask-password", "ollama"],
|
|
332
|
+
fix: "systemd"
|
|
333
|
+
};
|
|
334
|
+
const brew = await deps.exec("brew", ["list", "--versions", "ollama"]);
|
|
335
|
+
if (brew.code === 0 && brew.stdout.trim())
|
|
336
|
+
return { kind: "brew", start: ["brew", "services", "start", "ollama"], fix: "brew" };
|
|
337
|
+
if (await deps.which("ollama"))
|
|
338
|
+
return { kind: "binary", fix: "serve" };
|
|
339
|
+
return { kind: "none", fix: brew.missing ? undefined : "brew_install" };
|
|
340
|
+
}
|
|
341
|
+
async function anyFile(deps, files) {
|
|
342
|
+
for (const file of files)
|
|
343
|
+
if (await deps.readText(file) !== undefined)
|
|
344
|
+
return true;
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
async function ensureOllama(deps, confirm) {
|
|
348
|
+
const running = await ollamaVersion(deps);
|
|
349
|
+
if (running)
|
|
350
|
+
return { version: running };
|
|
351
|
+
const install = await findOllama(deps);
|
|
352
|
+
const down = (message, extra = {}) => new CliError("LOCAL_MODEL_UNREACHABLE", message, { check: "ollama", ollama: install.kind, ...extra }, install.fix);
|
|
353
|
+
if (!install.start) {
|
|
354
|
+
throw down(install.kind === "none" ? `Ollama does not answer at ${OLLAMA_URL}, and it does not look installed.` : `Ollama does not answer at ${OLLAMA_URL}.`);
|
|
355
|
+
}
|
|
356
|
+
const command = install.start.join(" ");
|
|
357
|
+
if (!await confirm(command)) {
|
|
358
|
+
throw down(`Ollama is installed but not running at ${OLLAMA_URL}; it was not started.`);
|
|
359
|
+
}
|
|
360
|
+
deps.say(`Starting Ollama: ${command}`);
|
|
361
|
+
const [cmd = "", ...args] = install.start;
|
|
362
|
+
const res = await deps.exec(cmd, args, { timeoutMs: 60000 });
|
|
363
|
+
if (res.code !== 0 || res.missing) {
|
|
364
|
+
const why = res.stderr.trim().split(`
|
|
365
|
+
`)[0];
|
|
366
|
+
throw down(`\`${command}\` did not start Ollama${why ? `: ${why}` : "."}`);
|
|
367
|
+
}
|
|
368
|
+
for (let waited = 0;waited < OLLAMA_START_TIMEOUT_MS; waited += 1000) {
|
|
369
|
+
await deps.sleep(1000);
|
|
370
|
+
const version = await ollamaVersion(deps);
|
|
371
|
+
if (version) {
|
|
372
|
+
deps.say(`Ollama ${version} is running.`);
|
|
373
|
+
if (deps.platform === "linux")
|
|
374
|
+
deps.say("On Linux, NimbleBrain's container reaches Ollama only when it listens beyond 127.0.0.1 (OLLAMA_HOST=0.0.0.0).");
|
|
375
|
+
return { version, started: command };
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
throw down(`Ollama was started with \`${command}\` but did not answer at ${OLLAMA_URL} within 30 seconds.`, { ollama_started: command });
|
|
379
|
+
}
|
|
380
|
+
async function setupLocalModel(deps, model, asks) {
|
|
381
|
+
const { started } = await ensureOllama(deps, asks.start);
|
|
382
|
+
try {
|
|
383
|
+
return { values: await prepareModel(deps, model, asks.pull), started };
|
|
384
|
+
} catch (err) {
|
|
385
|
+
if (err instanceof CliError && started)
|
|
386
|
+
throw err.with({ ollama_started: started });
|
|
387
|
+
throw err;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
async function prepareModel(deps, model, pull) {
|
|
391
|
+
let found = await show(deps, model);
|
|
392
|
+
if (!found) {
|
|
393
|
+
if (!await pull(model)) {
|
|
394
|
+
throw new CliError("LOCAL_MODEL_MISSING", `Ollama has no model "${model}".`, {
|
|
395
|
+
check: "model"
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
deps.say(`Downloading ${model} with Ollama. A model is several GB; this can take a while.`);
|
|
399
|
+
const pulled = await deps.postJson(`${OLLAMA_URL}/api/pull`, { model, stream: false }, PULL_TIMEOUT_MS);
|
|
400
|
+
found = pulled?.status === 200 ? await show(deps, model) : undefined;
|
|
401
|
+
if (!found) {
|
|
402
|
+
throw new CliError("LOCAL_MODEL_MISSING", `Ollama could not download "${model}".`, {
|
|
403
|
+
check: "model"
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (!found.capabilities?.includes("tools")) {
|
|
408
|
+
throw new CliError("LOCAL_MODEL_NO_TOOLS", `${model} does not support tool calling, which NimbleBrain's agents need.`, { check: "tools" });
|
|
409
|
+
}
|
|
410
|
+
const name = nimblebrainModelName(model);
|
|
411
|
+
const created = await deps.postJson(`${OLLAMA_URL}/api/create`, { model: name, from: model, parameters: { num_ctx: LOCAL_CONTEXT }, stream: false }, CREATE_TIMEOUT_MS);
|
|
412
|
+
if (created?.status !== 200) {
|
|
413
|
+
throw new CliError("LOCAL_MODEL_MISSING", `Ollama could not prepare ${name} from ${model}.`, {
|
|
414
|
+
check: "model"
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
return {
|
|
418
|
+
NB_LOCAL_MODEL_URL: OLLAMA_FROM_CONTAINER,
|
|
419
|
+
NB_LOCAL_MODEL: name,
|
|
420
|
+
NB_LOCAL_MODEL_CONTEXT: String(LOCAL_CONTEXT)
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
async function checkLocalModel(deps, model, start) {
|
|
424
|
+
const checks = [];
|
|
425
|
+
const { version, started } = await ensureOllama(deps, start).catch((err) => {
|
|
426
|
+
if (!(err instanceof CliError))
|
|
427
|
+
throw err;
|
|
428
|
+
checks.push({ check: "ollama", ok: false, detail: err.message });
|
|
429
|
+
throw err.with({ check: "ollama", checks });
|
|
430
|
+
});
|
|
431
|
+
checks.push({
|
|
432
|
+
check: "ollama",
|
|
433
|
+
ok: true,
|
|
434
|
+
detail: started ? `${version} (started: ${started})` : version
|
|
435
|
+
});
|
|
436
|
+
const found = await show(deps, model);
|
|
437
|
+
const fail = (check, code, detail) => {
|
|
438
|
+
checks.push({ check, ok: false, detail });
|
|
439
|
+
throw new CliError(code, detail, { check, checks, ollama_started: started });
|
|
440
|
+
};
|
|
441
|
+
if (!found)
|
|
442
|
+
fail("model", "LOCAL_MODEL_MISSING", `Ollama has no model "${model}".`);
|
|
443
|
+
checks.push({ check: "model", ok: true, detail: model });
|
|
444
|
+
if (!found?.capabilities?.includes("tools"))
|
|
445
|
+
fail("tools", "LOCAL_MODEL_NO_TOOLS", `${model} does not support tool calling.`);
|
|
446
|
+
checks.push({ check: "tools", ok: true, detail: "tool calling" });
|
|
447
|
+
const ctx = numCtxOf(found?.parameters);
|
|
448
|
+
if (ctx === undefined || ctx < LOCAL_CONTEXT)
|
|
449
|
+
fail("context", "LOCAL_CONTEXT_SMALL", `${model} has a context window of ${ctx ?? "Ollama's default"}, below ${LOCAL_CONTEXT}.`);
|
|
450
|
+
checks.push({ check: "context", ok: true, detail: `${ctx} tokens` });
|
|
451
|
+
return { checks, started };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// src/args.ts
|
|
455
|
+
var COMMAND_SPECS = {
|
|
456
|
+
init: {
|
|
457
|
+
args: [],
|
|
458
|
+
description: "Check this machine, start NimbleBrain, connect your coding agent (default)"
|
|
459
|
+
},
|
|
460
|
+
doctor: {
|
|
461
|
+
args: [],
|
|
462
|
+
description: "Check Docker, Compose, architecture, memory and the port; installs nothing"
|
|
463
|
+
},
|
|
464
|
+
status: { args: [], description: "Version, health, URL and each workspace's MCP URL" },
|
|
465
|
+
open: { args: [], description: "Open NimbleBrain's web UI in the default browser" },
|
|
466
|
+
key: { args: [], description: "Enter the model key at a hidden prompt" },
|
|
467
|
+
"local-model": {
|
|
468
|
+
args: ["[model]"],
|
|
469
|
+
description: `Run NimbleBrain's agents on a model served by Ollama (default ${DEFAULT_LOCAL_MODEL})`
|
|
470
|
+
},
|
|
471
|
+
telemetry: {
|
|
472
|
+
args: ["[on|off|status]"],
|
|
473
|
+
description: "Whether this install sends anonymous usage data"
|
|
474
|
+
},
|
|
475
|
+
up: { args: [], description: "Start NimbleBrain" },
|
|
476
|
+
down: { args: [], description: "Stop NimbleBrain; data survives down" },
|
|
477
|
+
logs: { args: [], description: "Recent runtime log lines" },
|
|
478
|
+
update: {
|
|
479
|
+
args: [],
|
|
480
|
+
description: "Move to the newest release, keeping data, and refresh the connected agent's nimblebrain skill"
|
|
481
|
+
},
|
|
482
|
+
uninstall: { args: [], description: "Remove NimbleBrain and its data" },
|
|
483
|
+
mcp: {
|
|
484
|
+
args: ["<add|list|remove>"],
|
|
485
|
+
description: "MCP servers in development; see mcp add, list and remove"
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
var COMMANDS = Object.keys(COMMAND_SPECS);
|
|
489
|
+
var TELEMETRY_ARGS = ["on", "off", "status"];
|
|
235
490
|
var AGENTS = ["claude-code", "cursor", "codex", "none"];
|
|
236
491
|
var DEFAULT_PORT = 27246;
|
|
237
|
-
var
|
|
238
|
-
json: {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
492
|
+
var FLAG_SPECS = {
|
|
493
|
+
json: {
|
|
494
|
+
type: "boolean",
|
|
495
|
+
description: "One JSON object on stdout; progress and notices go to stderr"
|
|
496
|
+
},
|
|
497
|
+
yes: {
|
|
498
|
+
type: "boolean",
|
|
499
|
+
short: "y",
|
|
500
|
+
description: "Take every default; implied when no terminal is attached"
|
|
501
|
+
},
|
|
502
|
+
agent: {
|
|
503
|
+
type: "string",
|
|
504
|
+
takes: "name",
|
|
505
|
+
description: `The coding agent to connect: ${AGENTS.slice(0, -1).join(", ")} or ${AGENTS.at(-1)}`,
|
|
506
|
+
commands: ["init"]
|
|
507
|
+
},
|
|
508
|
+
port: {
|
|
509
|
+
type: "string",
|
|
510
|
+
takes: "n",
|
|
511
|
+
description: `Local port (default ${DEFAULT_PORT})`,
|
|
512
|
+
commands: ["init", "doctor"]
|
|
513
|
+
},
|
|
514
|
+
home: {
|
|
515
|
+
type: "string",
|
|
516
|
+
takes: "dir",
|
|
517
|
+
description: "Where the install lives (default ~/.nimblebrain-local, or NIMBLEBRAIN_HOME); a home with another folder name is a separate install"
|
|
518
|
+
},
|
|
519
|
+
provider: {
|
|
520
|
+
type: "string",
|
|
521
|
+
takes: "name",
|
|
522
|
+
description: "The model provider for key (default anthropic)",
|
|
523
|
+
commands: ["key"]
|
|
524
|
+
},
|
|
525
|
+
"local-model": {
|
|
526
|
+
type: "string",
|
|
527
|
+
takes: "model",
|
|
528
|
+
description: "Run on this Ollama model instead of a hosted key",
|
|
529
|
+
commands: ["init"]
|
|
530
|
+
},
|
|
531
|
+
runtime: {
|
|
532
|
+
type: "string",
|
|
533
|
+
takes: "tag",
|
|
534
|
+
description: "The NimbleBrain release to install or update to (default the newest)",
|
|
535
|
+
commands: ["init", "update"]
|
|
536
|
+
},
|
|
537
|
+
"no-open": {
|
|
538
|
+
type: "boolean",
|
|
539
|
+
description: "Do not open the web UI in the browser when init finishes",
|
|
540
|
+
commands: ["init"]
|
|
541
|
+
},
|
|
542
|
+
telemetry: {
|
|
543
|
+
type: "string",
|
|
544
|
+
takes: "on|off",
|
|
545
|
+
description: "The person's answer to sending anonymous usage data; without it, init asks or leaves it undecided",
|
|
546
|
+
commands: ["init"]
|
|
547
|
+
},
|
|
548
|
+
name: {
|
|
549
|
+
type: "string",
|
|
550
|
+
takes: "name",
|
|
551
|
+
description: "The server name (default from the server's card, else its URL)",
|
|
552
|
+
commands: ["mcp add"]
|
|
553
|
+
},
|
|
554
|
+
workspace: {
|
|
555
|
+
type: "string",
|
|
556
|
+
takes: "id",
|
|
557
|
+
description: "The workspace (default the one init connected, the first in status)",
|
|
558
|
+
commands: ["mcp add", "mcp list", "mcp remove"]
|
|
559
|
+
},
|
|
560
|
+
help: {
|
|
561
|
+
type: "boolean",
|
|
562
|
+
short: "h",
|
|
563
|
+
description: "The commands and flags; with --json, these and every error code as JSON"
|
|
564
|
+
},
|
|
565
|
+
version: { type: "boolean", short: "v", description: "This CLI's version" }
|
|
251
566
|
};
|
|
567
|
+
var options = Object.fromEntries(Object.entries(FLAG_SPECS).map(([name, f]) => [
|
|
568
|
+
name,
|
|
569
|
+
f.short ? { type: f.type, short: f.short } : { type: f.type }
|
|
570
|
+
]));
|
|
252
571
|
function parse(argv) {
|
|
253
572
|
const { values, positionals } = parseStrict(argv);
|
|
254
573
|
const json = values.json === true;
|
|
@@ -274,6 +593,7 @@ function parse(argv) {
|
|
|
274
593
|
localModel: values["local-model"],
|
|
275
594
|
runtime: values.runtime,
|
|
276
595
|
telemetry,
|
|
596
|
+
noOpen: values["no-open"] === true,
|
|
277
597
|
name: values.name,
|
|
278
598
|
workspace: values.workspace
|
|
279
599
|
};
|
|
@@ -297,7 +617,11 @@ function telemetryFlag(value) {
|
|
|
297
617
|
return value;
|
|
298
618
|
throw new CliError("USAGE", `--telemetry must be on or off, not "${value}".`);
|
|
299
619
|
}
|
|
300
|
-
var MCP_SUBCOMMANDS = {
|
|
620
|
+
var MCP_SUBCOMMANDS = {
|
|
621
|
+
add: { operand: "url", description: "Add an MCP server you run on this machine to a workspace" },
|
|
622
|
+
list: { operand: null, description: "The servers in a workspace" },
|
|
623
|
+
remove: { operand: "serverName", description: "Remove a server from a workspace" }
|
|
624
|
+
};
|
|
301
625
|
function command(name, positionals) {
|
|
302
626
|
if (!COMMANDS.includes(name))
|
|
303
627
|
throw new CliError("USAGE", `Unknown command "${name}".`);
|
|
@@ -307,7 +631,7 @@ function command(name, positionals) {
|
|
|
307
631
|
if (positionals.length > maxArgs)
|
|
308
632
|
throw new CliError("USAGE", `Unexpected argument "${positionals[maxArgs]}".`);
|
|
309
633
|
const arg = positionals[1];
|
|
310
|
-
if (name === "telemetry" && arg !== undefined && !
|
|
634
|
+
if (name === "telemetry" && arg !== undefined && !TELEMETRY_ARGS.includes(arg))
|
|
311
635
|
throw new CliError("USAGE", `telemetry takes on, off or status, not "${arg}".`);
|
|
312
636
|
return name;
|
|
313
637
|
}
|
|
@@ -315,7 +639,7 @@ function mcpCommand(positionals) {
|
|
|
315
639
|
const sub = positionals[1];
|
|
316
640
|
if (sub === undefined || !Object.hasOwn(MCP_SUBCOMMANDS, sub))
|
|
317
641
|
throw new CliError("USAGE", `mcp takes add <url>, list or remove <serverName>${sub === undefined ? "" : `, not "${sub}"`}.`);
|
|
318
|
-
const operand = MCP_SUBCOMMANDS[sub];
|
|
642
|
+
const { operand } = MCP_SUBCOMMANDS[sub];
|
|
319
643
|
const maxArgs = operand === null ? 2 : 3;
|
|
320
644
|
if (positionals.length > maxArgs)
|
|
321
645
|
throw new CliError("USAGE", `Unexpected argument "${positionals[maxArgs]}".`);
|
|
@@ -537,6 +861,75 @@ function gib(bytes) {
|
|
|
537
861
|
return `${(bytes / 1024 ** 3).toFixed(1)} GB`;
|
|
538
862
|
}
|
|
539
863
|
|
|
864
|
+
// src/help.ts
|
|
865
|
+
var HELP_COMMAND = {
|
|
866
|
+
name: "help",
|
|
867
|
+
args: [],
|
|
868
|
+
description: "These commands and flags; with --json, these and every error code as JSON"
|
|
869
|
+
};
|
|
870
|
+
function commands() {
|
|
871
|
+
const list = Object.keys(COMMAND_SPECS).flatMap((name) => {
|
|
872
|
+
if (name === "mcp") {
|
|
873
|
+
return Object.entries(MCP_SUBCOMMANDS).map(([sub, spec]) => ({
|
|
874
|
+
name: `mcp ${sub}`,
|
|
875
|
+
args: spec.operand ? [`<${spec.operand}>`] : [],
|
|
876
|
+
description: spec.description
|
|
877
|
+
}));
|
|
878
|
+
}
|
|
879
|
+
const spec = COMMAND_SPECS[name];
|
|
880
|
+
return [{ name, args: [...spec.args], description: spec.description }];
|
|
881
|
+
});
|
|
882
|
+
return [...list, HELP_COMMAND];
|
|
883
|
+
}
|
|
884
|
+
function flags() {
|
|
885
|
+
return Object.entries(FLAG_SPECS).map(([name, spec]) => {
|
|
886
|
+
const f = {
|
|
887
|
+
name: `--${name}`,
|
|
888
|
+
takes: "takes" in spec ? spec.takes : null,
|
|
889
|
+
description: spec.description
|
|
890
|
+
};
|
|
891
|
+
if ("short" in spec)
|
|
892
|
+
f.short = `-${spec.short}`;
|
|
893
|
+
if ("commands" in spec)
|
|
894
|
+
f.commands = [...spec.commands];
|
|
895
|
+
return f;
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
function errors() {
|
|
899
|
+
return Object.keys(ERRORS).map((code) => ({
|
|
900
|
+
code,
|
|
901
|
+
fix: ERRORS[code],
|
|
902
|
+
exit: exitCodeOf(code)
|
|
903
|
+
}));
|
|
904
|
+
}
|
|
905
|
+
function describe(version) {
|
|
906
|
+
return { ok: true, cli_version: version, commands: commands(), flags: flags(), errors: errors() };
|
|
907
|
+
}
|
|
908
|
+
function helpText(version) {
|
|
909
|
+
const cmds = commands().map((c) => [[c.name, ...c.args].join(" "), c.description]);
|
|
910
|
+
const fls = flags().map((f) => [
|
|
911
|
+
[f.takes ? `${f.name} <${f.takes}>` : f.name, f.short].filter(Boolean).join(", "),
|
|
912
|
+
f.commands ? `${f.description}; for ${f.commands.join(", ")}` : f.description
|
|
913
|
+
]);
|
|
914
|
+
const width = Math.max(...[...cmds, ...fls].map(([left]) => left.length)) + 2;
|
|
915
|
+
const row = ([left, right]) => ` ${left.padEnd(width)}${right}`;
|
|
916
|
+
return [
|
|
917
|
+
`nimblebrain ${version}: install and run NimbleBrain on this machine`,
|
|
918
|
+
"",
|
|
919
|
+
"Usage: npx nimblebrain [command] [flags]",
|
|
920
|
+
"",
|
|
921
|
+
"Commands:",
|
|
922
|
+
...cmds.map(row),
|
|
923
|
+
"",
|
|
924
|
+
"Flags:",
|
|
925
|
+
...fls.map(row),
|
|
926
|
+
"",
|
|
927
|
+
"Every error code and its fix: npx nimblebrain help --json",
|
|
928
|
+
""
|
|
929
|
+
].join(`
|
|
930
|
+
`);
|
|
931
|
+
}
|
|
932
|
+
|
|
540
933
|
// src/agents.ts
|
|
541
934
|
import { join as join3 } from "node:path";
|
|
542
935
|
var SERVER_NAME = "nimblebrain";
|
|
@@ -601,6 +994,20 @@ async function installSkill(deps, agent) {
|
|
|
601
994
|
"-y"
|
|
602
995
|
], { timeoutMs: 120000 });
|
|
603
996
|
}
|
|
997
|
+
async function refreshSkill(deps, agent) {
|
|
998
|
+
if (!await skillInstalled(deps, agent))
|
|
999
|
+
return "not_installed";
|
|
1000
|
+
try {
|
|
1001
|
+
const res = await deps.exec("npx", ["-y", "skills", "update", SKILL_NAME, "-g", "-y"], {
|
|
1002
|
+
timeoutMs: 120000
|
|
1003
|
+
});
|
|
1004
|
+
if (res.code !== 0)
|
|
1005
|
+
return "failed";
|
|
1006
|
+
return /Updated \d+ skill/.test(res.stdout) ? "updated" : "unchanged";
|
|
1007
|
+
} catch {
|
|
1008
|
+
return "failed";
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
604
1011
|
var SKILL_NAME = "nimblebrain";
|
|
605
1012
|
function globalSkillsDir(deps, agent) {
|
|
606
1013
|
if (agent === "claude-code")
|
|
@@ -618,167 +1025,6 @@ async function skillInstalled(deps, agent) {
|
|
|
618
1025
|
return await deps.readText(join3(dir, SKILL_NAME, "SKILL.md")) === undefined ? null : SKILL_NAME;
|
|
619
1026
|
}
|
|
620
1027
|
|
|
621
|
-
// src/local-model.ts
|
|
622
|
-
var OLLAMA_URL = "http://127.0.0.1:11434";
|
|
623
|
-
var OLLAMA_FROM_CONTAINER = "http://host.docker.internal:11434/v1";
|
|
624
|
-
var DEFAULT_LOCAL_MODEL = "gemma4:e4b";
|
|
625
|
-
var LOCAL_CONTEXT = 32768;
|
|
626
|
-
var PULL_TIMEOUT_MS = 60 * 60 * 1000;
|
|
627
|
-
var CREATE_TIMEOUT_MS = 2 * 60 * 1000;
|
|
628
|
-
function takesLocalModel(composeText) {
|
|
629
|
-
return composeText.includes("NB_LOCAL_MODEL_URL");
|
|
630
|
-
}
|
|
631
|
-
function nimblebrainModelName(model) {
|
|
632
|
-
return `nimblebrain-${model.replace(/[^A-Za-z0-9._-]+/g, "-")}`;
|
|
633
|
-
}
|
|
634
|
-
function localModelOf(envText) {
|
|
635
|
-
const line = envText.split(`
|
|
636
|
-
`).find((l) => l.startsWith("NB_LOCAL_MODEL="));
|
|
637
|
-
const value = line?.slice("NB_LOCAL_MODEL=".length).trim();
|
|
638
|
-
return value ? value : undefined;
|
|
639
|
-
}
|
|
640
|
-
async function show(deps, model) {
|
|
641
|
-
const res = await deps.postJson(`${OLLAMA_URL}/api/show`, { model });
|
|
642
|
-
return res?.status === 200 ? res.body : undefined;
|
|
643
|
-
}
|
|
644
|
-
function numCtxOf(parameters) {
|
|
645
|
-
const match = parameters?.match(/^num_ctx\s+(\d+)/m);
|
|
646
|
-
return match ? Number(match[1]) : undefined;
|
|
647
|
-
}
|
|
648
|
-
async function requireOllama(deps) {
|
|
649
|
-
const res = await deps.getJson(`${OLLAMA_URL}/api/version`);
|
|
650
|
-
const version = res?.body?.version;
|
|
651
|
-
if (res?.status !== 200 || typeof version !== "string") {
|
|
652
|
-
throw new CliError("LOCAL_MODEL_UNREACHABLE", `Ollama does not answer at ${OLLAMA_URL}.`, {
|
|
653
|
-
check: "ollama"
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
|
-
return version;
|
|
657
|
-
}
|
|
658
|
-
async function setupLocalModel(deps, model, pull) {
|
|
659
|
-
await requireOllama(deps);
|
|
660
|
-
let found = await show(deps, model);
|
|
661
|
-
if (!found) {
|
|
662
|
-
if (!await pull(model)) {
|
|
663
|
-
throw new CliError("LOCAL_MODEL_MISSING", `Ollama has no model "${model}".`, {
|
|
664
|
-
check: "model"
|
|
665
|
-
});
|
|
666
|
-
}
|
|
667
|
-
deps.say(`Downloading ${model} with Ollama. A model is several GB; this can take a while.`);
|
|
668
|
-
const pulled = await deps.postJson(`${OLLAMA_URL}/api/pull`, { model, stream: false }, PULL_TIMEOUT_MS);
|
|
669
|
-
found = pulled?.status === 200 ? await show(deps, model) : undefined;
|
|
670
|
-
if (!found) {
|
|
671
|
-
throw new CliError("LOCAL_MODEL_MISSING", `Ollama could not download "${model}".`, {
|
|
672
|
-
check: "model"
|
|
673
|
-
});
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
if (!found.capabilities?.includes("tools")) {
|
|
677
|
-
throw new CliError("LOCAL_MODEL_NO_TOOLS", `${model} does not support tool calling, which NimbleBrain's agents need.`, { check: "tools" });
|
|
678
|
-
}
|
|
679
|
-
const name = nimblebrainModelName(model);
|
|
680
|
-
const created = await deps.postJson(`${OLLAMA_URL}/api/create`, { model: name, from: model, parameters: { num_ctx: LOCAL_CONTEXT }, stream: false }, CREATE_TIMEOUT_MS);
|
|
681
|
-
if (created?.status !== 200) {
|
|
682
|
-
throw new CliError("LOCAL_MODEL_MISSING", `Ollama could not prepare ${name} from ${model}.`, {
|
|
683
|
-
check: "model"
|
|
684
|
-
});
|
|
685
|
-
}
|
|
686
|
-
return {
|
|
687
|
-
NB_LOCAL_MODEL_URL: OLLAMA_FROM_CONTAINER,
|
|
688
|
-
NB_LOCAL_MODEL: name,
|
|
689
|
-
NB_LOCAL_MODEL_CONTEXT: String(LOCAL_CONTEXT)
|
|
690
|
-
};
|
|
691
|
-
}
|
|
692
|
-
async function checkLocalModel(deps, model) {
|
|
693
|
-
const checks = [];
|
|
694
|
-
const version = await requireOllama(deps).catch((err) => {
|
|
695
|
-
checks.push({ check: "ollama", ok: false, detail: err.message });
|
|
696
|
-
throw new CliError(err.code, err.message, { check: "ollama", checks });
|
|
697
|
-
});
|
|
698
|
-
checks.push({ check: "ollama", ok: true, detail: version });
|
|
699
|
-
const found = await show(deps, model);
|
|
700
|
-
const fail = (check, code, detail) => {
|
|
701
|
-
checks.push({ check, ok: false, detail });
|
|
702
|
-
throw new CliError(code, detail, { check, checks });
|
|
703
|
-
};
|
|
704
|
-
if (!found)
|
|
705
|
-
fail("model", "LOCAL_MODEL_MISSING", `Ollama has no model "${model}".`);
|
|
706
|
-
checks.push({ check: "model", ok: true, detail: model });
|
|
707
|
-
if (!found?.capabilities?.includes("tools"))
|
|
708
|
-
fail("tools", "LOCAL_MODEL_NO_TOOLS", `${model} does not support tool calling.`);
|
|
709
|
-
checks.push({ check: "tools", ok: true, detail: "tool calling" });
|
|
710
|
-
const ctx = numCtxOf(found?.parameters);
|
|
711
|
-
if (ctx === undefined || ctx < LOCAL_CONTEXT)
|
|
712
|
-
fail("context", "LOCAL_CONTEXT_SMALL", `${model} has a context window of ${ctx ?? "Ollama's default"}, below ${LOCAL_CONTEXT}.`);
|
|
713
|
-
checks.push({ check: "context", ok: true, detail: `${ctx} tokens` });
|
|
714
|
-
return checks;
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
// src/release.ts
|
|
718
|
-
var REFS_URL = "https://github.com/NimbleBrainInc/nimblebrain.git/info/refs?service=git-upload-pack";
|
|
719
|
-
var VERSION = /^v\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;
|
|
720
|
-
var MIN_RUNTIME = "v0.73.0";
|
|
721
|
-
async function runtimeTag(deps, requested) {
|
|
722
|
-
const pinned = requested ?? deps.env.NIMBLEBRAIN_TAG;
|
|
723
|
-
let tag;
|
|
724
|
-
if (pinned) {
|
|
725
|
-
if (!VERSION.test(pinned)) {
|
|
726
|
-
throw new CliError("USAGE", `"${pinned}" is not a runtime release tag like v1.2.3.`);
|
|
727
|
-
}
|
|
728
|
-
tag = pinned;
|
|
729
|
-
} else {
|
|
730
|
-
tag = await newestRuntime(deps);
|
|
731
|
-
}
|
|
732
|
-
if (compareVersions(tag, MIN_RUNTIME) < 0) {
|
|
733
|
-
throw new CliError("RUNTIME_UNSUPPORTED", `This CLI installs NimbleBrain ${MIN_RUNTIME} or newer, not ${tag}.`);
|
|
734
|
-
}
|
|
735
|
-
return tag;
|
|
736
|
-
}
|
|
737
|
-
async function newestRuntime(deps) {
|
|
738
|
-
const res = await deps.getText(REFS_URL, 1e4);
|
|
739
|
-
if (res && res.status !== 200) {
|
|
740
|
-
throw new CliError("NETWORK", `GitHub answered ${res.status} when asked for the newest NimbleBrain release. Pass --runtime vX.Y.Z to skip the lookup.`);
|
|
741
|
-
}
|
|
742
|
-
const names = [...(res?.body ?? "").matchAll(/refs\/tags\/([^\s\0^]+)/g)].map((m) => m[1] ?? "");
|
|
743
|
-
const newest = newestRelease(names);
|
|
744
|
-
if (!newest)
|
|
745
|
-
throw new CliError("NETWORK", "Could not find the newest NimbleBrain release.");
|
|
746
|
-
return newest;
|
|
747
|
-
}
|
|
748
|
-
function newestRelease(tags) {
|
|
749
|
-
const plain = tags.filter((t) => /^v\d+\.\d+\.\d+$/.test(t));
|
|
750
|
-
return plain.sort(compareVersions).pop();
|
|
751
|
-
}
|
|
752
|
-
function compareVersions(a, b) {
|
|
753
|
-
const [coreA = "", preA] = a.replace(/^v/, "").split(/-(.*)/s);
|
|
754
|
-
const [coreB = "", preB] = b.replace(/^v/, "").split(/-(.*)/s);
|
|
755
|
-
const core = compareIdentifiers(coreA.split("."), coreB.split("."));
|
|
756
|
-
if (core !== 0 || !preA && !preB)
|
|
757
|
-
return core;
|
|
758
|
-
if (!preA || !preB)
|
|
759
|
-
return preA ? -1 : 1;
|
|
760
|
-
return compareIdentifiers(preA.split("."), preB.split("."));
|
|
761
|
-
}
|
|
762
|
-
function compareIdentifiers(a, b) {
|
|
763
|
-
for (let i = 0;i < Math.min(a.length, b.length); i++) {
|
|
764
|
-
const order = compareIdentifier(a[i] ?? "", b[i] ?? "");
|
|
765
|
-
if (order !== 0)
|
|
766
|
-
return order;
|
|
767
|
-
}
|
|
768
|
-
return a.length - b.length;
|
|
769
|
-
}
|
|
770
|
-
function compareIdentifier(x, y) {
|
|
771
|
-
if (x === y)
|
|
772
|
-
return 0;
|
|
773
|
-
const xn = /^\d+$/.test(x);
|
|
774
|
-
const yn = /^\d+$/.test(y);
|
|
775
|
-
if (xn && yn)
|
|
776
|
-
return Number(x) - Number(y);
|
|
777
|
-
if (xn !== yn)
|
|
778
|
-
return xn ? -1 : 1;
|
|
779
|
-
return x < y ? -1 : 1;
|
|
780
|
-
}
|
|
781
|
-
|
|
782
1028
|
// src/status.ts
|
|
783
1029
|
import { join as join5 } from "node:path";
|
|
784
1030
|
|
|
@@ -793,19 +1039,22 @@ var RELEASED = true;
|
|
|
793
1039
|
function telemetryKey(deps) {
|
|
794
1040
|
return deps.env.NIMBLEBRAIN_TELEMETRY_KEY ?? (RELEASED ? TELEMETRY_KEY : "");
|
|
795
1041
|
}
|
|
1042
|
+
function doNotTrack(deps) {
|
|
1043
|
+
return deps.env.DO_NOT_TRACK === "1";
|
|
1044
|
+
}
|
|
796
1045
|
function lockedHere(deps) {
|
|
797
|
-
if (deps
|
|
1046
|
+
if (doNotTrack(deps) || deps.env.NB_TELEMETRY_DISABLED === "1")
|
|
798
1047
|
return "do_not_track";
|
|
799
1048
|
if (!telemetryKey(deps))
|
|
800
1049
|
return "no_destination";
|
|
801
1050
|
return;
|
|
802
1051
|
}
|
|
803
|
-
function field(state, locked) {
|
|
1052
|
+
function field(state, cli, locked) {
|
|
804
1053
|
if (locked)
|
|
805
1054
|
return { state: state === "undecided" ? "off" : state, locked };
|
|
806
1055
|
if (state !== "undecided")
|
|
807
1056
|
return { state };
|
|
808
|
-
return { state, ask: QUESTION, set:
|
|
1057
|
+
return { state, ask: QUESTION, set: `${cli} telemetry on|off` };
|
|
809
1058
|
}
|
|
810
1059
|
function recorder2(deps, version, id) {
|
|
811
1060
|
const events = [];
|
|
@@ -886,7 +1135,7 @@ async function currentTelemetry(deps, home, url) {
|
|
|
886
1135
|
const rt = await runtimeTelemetry(deps, url);
|
|
887
1136
|
const state = rt?.consent ?? seededConsent(await deps.readText(join4(home, ".env")) ?? "");
|
|
888
1137
|
const lock = here ?? (rt?.locked === "operator" || rt?.locked === "no_destination" ? rt.locked : undefined);
|
|
889
|
-
return field(state, lock);
|
|
1138
|
+
return field(state, invocation(deps, home), lock);
|
|
890
1139
|
}
|
|
891
1140
|
async function settleWaiting(deps, home, version) {
|
|
892
1141
|
try {
|
|
@@ -934,7 +1183,7 @@ async function setRuntimeTelemetry(deps, url, consent) {
|
|
|
934
1183
|
// package.json
|
|
935
1184
|
var package_default = {
|
|
936
1185
|
name: "nimblebrain",
|
|
937
|
-
version: "0.
|
|
1186
|
+
version: "0.5.0",
|
|
938
1187
|
description: "Install, run and connect NimbleBrain, the open-source runtime for agents and MCP apps, on this machine.",
|
|
939
1188
|
license: "Apache-2.0",
|
|
940
1189
|
type: "module",
|
|
@@ -959,7 +1208,7 @@ var package_default = {
|
|
|
959
1208
|
};
|
|
960
1209
|
|
|
961
1210
|
// src/version.ts
|
|
962
|
-
var
|
|
1211
|
+
var VERSION = package_default.version;
|
|
963
1212
|
|
|
964
1213
|
// src/status.ts
|
|
965
1214
|
async function readInstall(deps, home) {
|
|
@@ -980,7 +1229,7 @@ async function status(deps, home) {
|
|
|
980
1229
|
const install = await readInstall(deps, home);
|
|
981
1230
|
if (!install)
|
|
982
1231
|
throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
|
|
983
|
-
const url =
|
|
1232
|
+
const url = installUrl(install);
|
|
984
1233
|
const up = await isUp(deps, url);
|
|
985
1234
|
const workspaces = up ? await readWorkspaces(deps, url) : [];
|
|
986
1235
|
const composeText = await deps.readText(join5(home, "docker-compose.yml")) ?? "";
|
|
@@ -990,7 +1239,7 @@ async function status(deps, home) {
|
|
|
990
1239
|
return {
|
|
991
1240
|
ok: true,
|
|
992
1241
|
version: install.version,
|
|
993
|
-
cli_version:
|
|
1242
|
+
cli_version: VERSION,
|
|
994
1243
|
health: up ? "ok" : "down",
|
|
995
1244
|
url,
|
|
996
1245
|
workspaces,
|
|
@@ -1004,6 +1253,9 @@ async function status(deps, home) {
|
|
|
1004
1253
|
telemetry: await currentTelemetry(deps, home, url)
|
|
1005
1254
|
};
|
|
1006
1255
|
}
|
|
1256
|
+
function installUrl(install) {
|
|
1257
|
+
return `http://127.0.0.1:${install.port}`;
|
|
1258
|
+
}
|
|
1007
1259
|
async function isUp(deps, url) {
|
|
1008
1260
|
const health = await deps.getJson(`${url}/v1/health`);
|
|
1009
1261
|
return health?.status === 200 && health.body?.status === "ok";
|
|
@@ -1016,10 +1268,112 @@ async function readWorkspaces(deps, url) {
|
|
|
1016
1268
|
return (body.workspaces ?? []).filter((w) => typeof w.id === "string" && typeof w.name === "string").map((w) => ({ id: w.id, name: w.name, mcp: `${url}/mcp/${w.id}` }));
|
|
1017
1269
|
}
|
|
1018
1270
|
|
|
1271
|
+
// src/open.ts
|
|
1272
|
+
function browserCommand(deps, url) {
|
|
1273
|
+
const { env } = deps;
|
|
1274
|
+
if (env.SSH_CONNECTION || env.SSH_TTY)
|
|
1275
|
+
return;
|
|
1276
|
+
if (deps.platform === "darwin")
|
|
1277
|
+
return { cmd: "open", args: [url] };
|
|
1278
|
+
if (deps.platform === "linux" && (env.DISPLAY || env.WAYLAND_DISPLAY))
|
|
1279
|
+
return { cmd: "xdg-open", args: [url] };
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
async function openBrowser(deps, url) {
|
|
1283
|
+
const command = browserCommand(deps, url);
|
|
1284
|
+
if (!command)
|
|
1285
|
+
return false;
|
|
1286
|
+
return deps.launch(command.cmd, command.args);
|
|
1287
|
+
}
|
|
1288
|
+
async function open(ctx) {
|
|
1289
|
+
const { deps, home } = ctx;
|
|
1290
|
+
const install = await readInstall(deps, home);
|
|
1291
|
+
if (!install)
|
|
1292
|
+
throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
|
|
1293
|
+
const url = installUrl(install);
|
|
1294
|
+
if (!await isUp(deps, url))
|
|
1295
|
+
throw new CliError("NOT_RUNNING", "NimbleBrain is not running.", { url });
|
|
1296
|
+
return { ok: true, url, opened: await openBrowser(deps, url) };
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
// src/release.ts
|
|
1300
|
+
var REFS_URL = "https://github.com/NimbleBrainInc/nimblebrain.git/info/refs?service=git-upload-pack";
|
|
1301
|
+
var VERSION2 = /^v\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;
|
|
1302
|
+
var MIN_RUNTIME = "v0.73.0";
|
|
1303
|
+
async function runtimeTag(deps, requested) {
|
|
1304
|
+
const pinned = requested ?? deps.env.NIMBLEBRAIN_TAG;
|
|
1305
|
+
let tag;
|
|
1306
|
+
if (pinned) {
|
|
1307
|
+
if (!VERSION2.test(pinned)) {
|
|
1308
|
+
throw new CliError("USAGE", `"${pinned}" is not a runtime release tag like v1.2.3.`);
|
|
1309
|
+
}
|
|
1310
|
+
tag = pinned;
|
|
1311
|
+
} else {
|
|
1312
|
+
tag = await newestRuntime(deps);
|
|
1313
|
+
}
|
|
1314
|
+
if (compareVersions(tag, MIN_RUNTIME) < 0) {
|
|
1315
|
+
throw new CliError("RUNTIME_UNSUPPORTED", `This CLI installs NimbleBrain ${MIN_RUNTIME} or newer, not ${tag}.`);
|
|
1316
|
+
}
|
|
1317
|
+
return tag;
|
|
1318
|
+
}
|
|
1319
|
+
async function newestRuntime(deps, timeoutMs = 1e4) {
|
|
1320
|
+
const res = await deps.getText(REFS_URL, timeoutMs);
|
|
1321
|
+
if (res && res.status !== 200) {
|
|
1322
|
+
throw new CliError("NETWORK", `GitHub answered ${res.status} when asked for the newest NimbleBrain release. Pass --runtime vX.Y.Z to skip the lookup.`);
|
|
1323
|
+
}
|
|
1324
|
+
const names = [...(res?.body ?? "").matchAll(/refs\/tags\/([^\s\0^]+)/g)].map((m) => m[1] ?? "");
|
|
1325
|
+
const newest = newestRelease(names);
|
|
1326
|
+
if (!newest)
|
|
1327
|
+
throw new CliError("NETWORK", "Could not find the newest NimbleBrain release.");
|
|
1328
|
+
return newest;
|
|
1329
|
+
}
|
|
1330
|
+
function newestRelease(tags) {
|
|
1331
|
+
const plain = tags.filter((t) => /^v\d+\.\d+\.\d+$/.test(t));
|
|
1332
|
+
return plain.sort(compareVersions).pop();
|
|
1333
|
+
}
|
|
1334
|
+
function compareVersions(a, b) {
|
|
1335
|
+
const [coreA = "", preA] = a.replace(/^v/, "").split(/-(.*)/s);
|
|
1336
|
+
const [coreB = "", preB] = b.replace(/^v/, "").split(/-(.*)/s);
|
|
1337
|
+
const core = compareIdentifiers(coreA.split("."), coreB.split("."));
|
|
1338
|
+
if (core !== 0 || !preA && !preB)
|
|
1339
|
+
return core;
|
|
1340
|
+
if (!preA || !preB)
|
|
1341
|
+
return preA ? -1 : 1;
|
|
1342
|
+
return compareIdentifiers(preA.split("."), preB.split("."));
|
|
1343
|
+
}
|
|
1344
|
+
function compareIdentifiers(a, b) {
|
|
1345
|
+
for (let i = 0;i < Math.min(a.length, b.length); i++) {
|
|
1346
|
+
const order = compareIdentifier(a[i] ?? "", b[i] ?? "");
|
|
1347
|
+
if (order !== 0)
|
|
1348
|
+
return order;
|
|
1349
|
+
}
|
|
1350
|
+
return a.length - b.length;
|
|
1351
|
+
}
|
|
1352
|
+
function compareIdentifier(x, y) {
|
|
1353
|
+
if (x === y)
|
|
1354
|
+
return 0;
|
|
1355
|
+
const xn = /^\d+$/.test(x);
|
|
1356
|
+
const yn = /^\d+$/.test(y);
|
|
1357
|
+
if (xn && yn)
|
|
1358
|
+
return Number(x) - Number(y);
|
|
1359
|
+
if (xn !== yn)
|
|
1360
|
+
return xn ? -1 : 1;
|
|
1361
|
+
return x < y ? -1 : 1;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1019
1364
|
// src/lifecycle.ts
|
|
1020
1365
|
var START_TIMEOUT_MS = 15 * 60000;
|
|
1021
1366
|
var HEALTH_TIMEOUT_MS = 3 * 60000;
|
|
1022
1367
|
var asks = ({ deps, flags }) => deps.interactive && !flags.yes && !flags.json;
|
|
1368
|
+
var yes = (answer) => /^(y|yes)?$/i.test(answer.trim());
|
|
1369
|
+
function confirmOllamaStart(ctx, unasked) {
|
|
1370
|
+
return async (command) => {
|
|
1371
|
+
if (!asks(ctx))
|
|
1372
|
+
return unasked;
|
|
1373
|
+
const login = command.startsWith("brew services") ? " It then also starts at login." : "";
|
|
1374
|
+
return yes(await ctx.deps.prompt(`Ollama isn't running. Start it with \`${command}\`?${login} [Y/n] `));
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1023
1377
|
async function init(ctx) {
|
|
1024
1378
|
const { deps, flags, home } = ctx;
|
|
1025
1379
|
if (await readInstall(deps, home))
|
|
@@ -1029,18 +1383,23 @@ async function init(ctx) {
|
|
|
1029
1383
|
const begun = Date.now();
|
|
1030
1384
|
rec.record("cli.init_started", { agent: flags.agent ?? null, interactive: deps.interactive });
|
|
1031
1385
|
try {
|
|
1032
|
-
return await install(ctx, consent, rec, begun);
|
|
1386
|
+
return await openWhenDone(ctx, await install(ctx, consent, rec, begun));
|
|
1033
1387
|
} catch (err) {
|
|
1034
1388
|
if (!(err instanceof CliError))
|
|
1035
1389
|
throw err;
|
|
1036
1390
|
if (err.code !== "CANCELLED" || consent !== "undecided")
|
|
1037
1391
|
await settleFailure(rec, home, consent, err);
|
|
1038
|
-
throw
|
|
1039
|
-
...err.detail,
|
|
1040
|
-
telemetry: field(consent, lockedHere(deps))
|
|
1041
|
-
});
|
|
1392
|
+
throw err.with({ telemetry: field(consent, invocation(deps, home), lockedHere(deps)) });
|
|
1042
1393
|
}
|
|
1043
1394
|
}
|
|
1395
|
+
async function openWhenDone(ctx, result) {
|
|
1396
|
+
const { deps, flags } = ctx;
|
|
1397
|
+
if (!deps.interactive || flags.json || flags.noOpen)
|
|
1398
|
+
return result;
|
|
1399
|
+
if (result.health !== "ok" || typeof result.url !== "string")
|
|
1400
|
+
return result;
|
|
1401
|
+
return { ...result, opened: await openBrowser(deps, result.url) };
|
|
1402
|
+
}
|
|
1044
1403
|
async function settleFailure(rec, home, consent, err) {
|
|
1045
1404
|
if (err.code !== "MODEL_KEY_MISSING") {
|
|
1046
1405
|
const check = typeof err.detail.check === "string" ? err.detail.check : undefined;
|
|
@@ -1056,7 +1415,8 @@ async function chooseConsent(ctx) {
|
|
|
1056
1415
|
if (!asks(ctx))
|
|
1057
1416
|
return "undecided";
|
|
1058
1417
|
const consent = parseAnswer(await ctx.deps.prompt(`${QUESTION} [Y/n] `));
|
|
1059
|
-
|
|
1418
|
+
const cli = invocation(ctx.deps, ctx.home);
|
|
1419
|
+
ctx.deps.say(consent === "on" ? `Sending anonymous usage data. Turn it off any time: ${cli} telemetry off` : `No usage data will be sent. Turn it on any time: ${cli} telemetry on`);
|
|
1060
1420
|
return consent;
|
|
1061
1421
|
}
|
|
1062
1422
|
async function install(ctx, consent, rec, begun) {
|
|
@@ -1070,36 +1430,43 @@ async function install(ctx, consent, rec, begun) {
|
|
|
1070
1430
|
const envText = await deps.readText(paths(home).env) ?? "";
|
|
1071
1431
|
const hasModel = hasKey(envText, keyVars(composeText)) || localModelOf(envText) !== undefined;
|
|
1072
1432
|
const local = flags.localModel ?? (!hasModel && asks(ctx) && takesLocalModel(composeText) ? await chooseModelSource(deps) : undefined);
|
|
1433
|
+
let ollamaStarted;
|
|
1073
1434
|
if (local)
|
|
1074
|
-
await useLocalModel(ctx, composeText, local);
|
|
1435
|
+
ollamaStarted = (await useLocalModel(ctx, composeText, local)).started;
|
|
1075
1436
|
else if (!hasModel && asks(ctx))
|
|
1076
1437
|
await enterKey(ctx, false);
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1438
|
+
return carryingStarted(ollamaStarted, async () => {
|
|
1439
|
+
deps.say(`Starting NimbleBrain ${tag}. The first start downloads about 2 GB of images.`);
|
|
1440
|
+
await start(deps, home, flags.port);
|
|
1441
|
+
const state = {
|
|
1442
|
+
version: tag.replace(/^v/, ""),
|
|
1443
|
+
port: flags.port,
|
|
1444
|
+
project
|
|
1445
|
+
};
|
|
1446
|
+
await writeState(deps, home, state);
|
|
1447
|
+
const manual = await connectChosenAgent(ctx, state);
|
|
1448
|
+
if (state.agent)
|
|
1449
|
+
rec.record("cli.agent_connected", { agent: state.agent });
|
|
1450
|
+
const result = {
|
|
1451
|
+
...await status(deps, home),
|
|
1452
|
+
manual,
|
|
1453
|
+
ollama_started: ollamaStarted
|
|
1454
|
+
};
|
|
1455
|
+
const finalEnv = await deps.readText(paths(home).env) ?? "";
|
|
1456
|
+
rec.record("cli.init_completed", {
|
|
1457
|
+
duration_ms: Date.now() - begun,
|
|
1458
|
+
model_key: result.model_key,
|
|
1459
|
+
provider: localModelOf(finalEnv) ? "ollama" : keyProvider(finalEnv, composeText),
|
|
1460
|
+
agent: state.agent ?? null
|
|
1100
1461
|
});
|
|
1101
|
-
|
|
1102
|
-
|
|
1462
|
+
await rec.settle(home, consent);
|
|
1463
|
+
if (result.model_key === "missing" && !result.local_model) {
|
|
1464
|
+
throw new CliError("MODEL_KEY_MISSING", "NimbleBrain is running; chat needs a model key.", {
|
|
1465
|
+
...result
|
|
1466
|
+
});
|
|
1467
|
+
}
|
|
1468
|
+
return { ...result };
|
|
1469
|
+
});
|
|
1103
1470
|
}
|
|
1104
1471
|
async function confirmStart(ctx, tag) {
|
|
1105
1472
|
if (!asks(ctx))
|
|
@@ -1125,10 +1492,25 @@ async function connectChosenAgent(ctx, state) {
|
|
|
1125
1492
|
return conn.manual;
|
|
1126
1493
|
}
|
|
1127
1494
|
async function up(ctx) {
|
|
1495
|
+
const { deps, home } = ctx;
|
|
1128
1496
|
const install = await requireInstall(ctx);
|
|
1129
|
-
await
|
|
1130
|
-
|
|
1497
|
+
const local = localModelOf(await deps.readText(paths(home).env) ?? "");
|
|
1498
|
+
const ollama = local ? await ensureOllama(deps, confirmOllamaStart(ctx, true)).catch((err) => {
|
|
1499
|
+
if (err instanceof CliError)
|
|
1500
|
+
return err;
|
|
1501
|
+
throw err;
|
|
1502
|
+
}) : undefined;
|
|
1503
|
+
const started = ollama instanceof CliError ? ollama.detail.ollama_started : ollama?.started;
|
|
1504
|
+
const result = await carryingStarted(started, async () => {
|
|
1505
|
+
await start(deps, home, install.port);
|
|
1506
|
+
return status(deps, home);
|
|
1507
|
+
});
|
|
1508
|
+
if (ollama instanceof CliError) {
|
|
1509
|
+
throw ollama.with({ ...result }, `NimbleBrain is running, but ${lowerFirst(ollama.message)}`);
|
|
1510
|
+
}
|
|
1511
|
+
return { ...result, ollama_started: ollama?.started };
|
|
1131
1512
|
}
|
|
1513
|
+
var lowerFirst = (s) => s.charAt(0).toLowerCase() + s.slice(1);
|
|
1132
1514
|
async function down(ctx) {
|
|
1133
1515
|
await requireInstall(ctx);
|
|
1134
1516
|
const res = await compose(ctx.deps, ctx.home, ["down"]);
|
|
@@ -1156,8 +1538,10 @@ async function update(ctx) {
|
|
|
1156
1538
|
const tag = await runtimeTag(deps, ctx.flags.runtime);
|
|
1157
1539
|
const from = `v${install.version}`;
|
|
1158
1540
|
const order = compareVersions(tag, from);
|
|
1159
|
-
if (order === 0)
|
|
1160
|
-
|
|
1541
|
+
if (order === 0) {
|
|
1542
|
+
const skill_update = await refreshAgentSkill(deps, install);
|
|
1543
|
+
return { ...await status(deps, home), already: true, ...skill_update };
|
|
1544
|
+
}
|
|
1161
1545
|
if (order < 0) {
|
|
1162
1546
|
throw new CliError("USAGE", `${tag} is older than the installed ${from}; NimbleBrain does not move backwards.`);
|
|
1163
1547
|
}
|
|
@@ -1169,7 +1553,13 @@ async function update(ctx) {
|
|
|
1169
1553
|
throw startError(pull);
|
|
1170
1554
|
await start(deps, home, install.port);
|
|
1171
1555
|
await writeState(deps, home, { ...install, version: tag.replace(/^v/, "") });
|
|
1172
|
-
|
|
1556
|
+
const skill_update = await refreshAgentSkill(deps, install);
|
|
1557
|
+
return { ...await status(deps, home), from, ...skill_update };
|
|
1558
|
+
}
|
|
1559
|
+
async function refreshAgentSkill(deps, install) {
|
|
1560
|
+
if (!install.agent || install.agent === "none")
|
|
1561
|
+
return {};
|
|
1562
|
+
return { skill_update: await refreshSkill(deps, install.agent) };
|
|
1173
1563
|
}
|
|
1174
1564
|
async function uninstall(ctx) {
|
|
1175
1565
|
const { deps, flags, home } = ctx;
|
|
@@ -1213,7 +1603,7 @@ async function telemetry(ctx) {
|
|
|
1213
1603
|
await deps.writeText(p.env, setEnvLines(await deps.readText(p.env) ?? "", { NB_TELEMETRY_CONSENT: ctx.arg }), 384);
|
|
1214
1604
|
const rec = recorder2(deps, ctx.version, await reportId(deps, home));
|
|
1215
1605
|
await rec.settle(home, ctx.arg);
|
|
1216
|
-
return { ok: true, telemetry: field(ctx.arg) };
|
|
1606
|
+
return { ok: true, telemetry: field(ctx.arg, invocation(deps, home)) };
|
|
1217
1607
|
}
|
|
1218
1608
|
async function key(ctx) {
|
|
1219
1609
|
if (!ctx.deps.interactive) {
|
|
@@ -1250,9 +1640,18 @@ async function localModel(ctx) {
|
|
|
1250
1640
|
const install = await requireInstall(ctx);
|
|
1251
1641
|
const composeText = await ctx.deps.readText(paths(ctx.home).compose) ?? "";
|
|
1252
1642
|
const model = ctx.arg ?? ctx.flags.localModel ?? DEFAULT_LOCAL_MODEL;
|
|
1253
|
-
const name = await useLocalModel(ctx, composeText, model);
|
|
1254
|
-
await start(ctx.deps, ctx.home, install.port);
|
|
1255
|
-
return { ok: true, local_model: name };
|
|
1643
|
+
const { name, started } = await useLocalModel(ctx, composeText, model);
|
|
1644
|
+
await carryingStarted(started, () => start(ctx.deps, ctx.home, install.port));
|
|
1645
|
+
return { ok: true, local_model: name, ollama_started: started };
|
|
1646
|
+
}
|
|
1647
|
+
async function carryingStarted(started, next) {
|
|
1648
|
+
try {
|
|
1649
|
+
return await next();
|
|
1650
|
+
} catch (err) {
|
|
1651
|
+
if (err instanceof CliError && started)
|
|
1652
|
+
throw err.with({ ollama_started: started });
|
|
1653
|
+
throw err;
|
|
1654
|
+
}
|
|
1256
1655
|
}
|
|
1257
1656
|
async function chooseModelSource(deps) {
|
|
1258
1657
|
deps.say("NimbleBrain's own agents need a model: a hosted one with your API key, or a local one through Ollama (free, slower; https://docs.nimblebrain.ai/config/local-models/).");
|
|
@@ -1264,16 +1663,18 @@ async function useLocalModel(ctx, composeText, model) {
|
|
|
1264
1663
|
if (!takesLocalModel(composeText)) {
|
|
1265
1664
|
throw new CliError("RUNTIME_UNSUPPORTED", "This release's runtime takes no local model; a newer release does.");
|
|
1266
1665
|
}
|
|
1267
|
-
const values = await setupLocalModel(deps, model,
|
|
1666
|
+
const { values, started } = await setupLocalModel(deps, model, {
|
|
1667
|
+
start: confirmOllamaStart(ctx, true),
|
|
1668
|
+
pull: (m) => confirmPull(ctx, m)
|
|
1669
|
+
});
|
|
1268
1670
|
const p = paths(home);
|
|
1269
1671
|
await deps.writeText(p.env, setEnvLines(await deps.readText(p.env) ?? "", values), 384);
|
|
1270
|
-
return values.NB_LOCAL_MODEL ?? model;
|
|
1672
|
+
return { name: values.NB_LOCAL_MODEL ?? model, started };
|
|
1271
1673
|
}
|
|
1272
1674
|
async function confirmPull(ctx, model) {
|
|
1273
1675
|
if (!asks(ctx))
|
|
1274
1676
|
return true;
|
|
1275
|
-
|
|
1276
|
-
return answer === "" || answer === "y" || answer === "yes";
|
|
1677
|
+
return yes(await ctx.deps.prompt(`Download ${model} with Ollama? It is several GB. [Y/n] `));
|
|
1277
1678
|
}
|
|
1278
1679
|
async function chooseAgent(deps) {
|
|
1279
1680
|
deps.say("A coding agent you connect can use NimbleBrain's tools. NimbleBrain's own agents still run on the model key above.");
|
|
@@ -1460,71 +1861,143 @@ async function call(t, args, timeoutMs) {
|
|
|
1460
1861
|
return body.structuredContent ?? {};
|
|
1461
1862
|
}
|
|
1462
1863
|
|
|
1463
|
-
// src/
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1864
|
+
// src/updates.ts
|
|
1865
|
+
import { join as join6 } from "node:path";
|
|
1866
|
+
var NPM_LATEST = "https://registry.npmjs.org/nimblebrain/latest";
|
|
1867
|
+
var FRESH_MS = 24 * 60 * 60000;
|
|
1868
|
+
var LOOKUP_TIMEOUT_MS = 5000;
|
|
1869
|
+
var NOTICE_WAIT_MS = 1000;
|
|
1870
|
+
var SEMVER = /^v?\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
|
|
1871
|
+
function cachePath(deps) {
|
|
1872
|
+
const xdg = deps.env.XDG_CACHE_HOME?.trim();
|
|
1873
|
+
const base = xdg?.startsWith("/") ? xdg : join6(deps.home, ".cache");
|
|
1874
|
+
return join6(base, "nimblebrain", "update-check.json");
|
|
1875
|
+
}
|
|
1876
|
+
function updateCheckOff(deps) {
|
|
1877
|
+
const ci = deps.env.CI?.trim().toLowerCase();
|
|
1878
|
+
return ci !== undefined && ci !== "" && ci !== "false" && ci !== "0" || deps.env.NIMBLEBRAIN_NO_UPDATE_CHECK === "1" || doNotTrack(deps);
|
|
1879
|
+
}
|
|
1880
|
+
function startUpdateCheck(deps) {
|
|
1881
|
+
if (updateCheckOff(deps))
|
|
1882
|
+
return;
|
|
1883
|
+
const cached = readCache(deps);
|
|
1884
|
+
let settled = false;
|
|
1885
|
+
const refreshed = cached.then(async (old) => {
|
|
1886
|
+
if (old && Date.now() - old.checked_at < FRESH_MS)
|
|
1887
|
+
return old;
|
|
1888
|
+
return await lookup(deps, old) ?? old;
|
|
1889
|
+
});
|
|
1890
|
+
refreshed.then(() => {
|
|
1891
|
+
settled = true;
|
|
1892
|
+
});
|
|
1893
|
+
return {
|
|
1894
|
+
async known(waitMs) {
|
|
1895
|
+
const old = await cached;
|
|
1896
|
+
if (settled)
|
|
1897
|
+
return refreshed;
|
|
1898
|
+
const deadline = waitMs > 0 ? deps.sleep(waitMs).then(() => old) : Promise.resolve(old);
|
|
1899
|
+
return Promise.race([refreshed, deadline]);
|
|
1900
|
+
}
|
|
1901
|
+
};
|
|
1902
|
+
}
|
|
1903
|
+
async function readCache(deps) {
|
|
1904
|
+
try {
|
|
1905
|
+
const raw = JSON.parse(await deps.readText(cachePath(deps)) ?? "null");
|
|
1906
|
+
if (typeof raw?.checked_at !== "number")
|
|
1907
|
+
return;
|
|
1908
|
+
return {
|
|
1909
|
+
checked_at: raw.checked_at,
|
|
1910
|
+
cli: version(raw.cli),
|
|
1911
|
+
runtime: version(raw.runtime)
|
|
1912
|
+
};
|
|
1913
|
+
} catch {
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
async function lookup(deps, old) {
|
|
1918
|
+
try {
|
|
1919
|
+
const [npm, runtime] = await Promise.all([
|
|
1920
|
+
deps.getJson(NPM_LATEST, LOOKUP_TIMEOUT_MS),
|
|
1921
|
+
newestRuntime(deps, LOOKUP_TIMEOUT_MS).catch(() => {
|
|
1922
|
+
return;
|
|
1923
|
+
})
|
|
1924
|
+
]);
|
|
1925
|
+
const cli = npm?.status === 200 ? version(npm.body?.version) : undefined;
|
|
1926
|
+
if (!cli && !runtime)
|
|
1927
|
+
return;
|
|
1928
|
+
const known = {
|
|
1929
|
+
checked_at: Date.now(),
|
|
1930
|
+
cli: cli ?? old?.cli,
|
|
1931
|
+
runtime: runtime ?? old?.runtime
|
|
1932
|
+
};
|
|
1933
|
+
await deps.writeText(cachePath(deps), `${JSON.stringify(known)}
|
|
1934
|
+
`);
|
|
1935
|
+
return known;
|
|
1936
|
+
} catch {
|
|
1937
|
+
return;
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
function version(value) {
|
|
1941
|
+
return typeof value === "string" && SEMVER.test(value) ? value : undefined;
|
|
1942
|
+
}
|
|
1943
|
+
function updatesField(known, cliVersion, runtimeVersion) {
|
|
1944
|
+
const updates = {};
|
|
1945
|
+
if (known?.cli)
|
|
1946
|
+
updates.cli = { current: cliVersion, latest: known.cli };
|
|
1947
|
+
if (known?.runtime && runtimeVersion) {
|
|
1948
|
+
updates.runtime = { current: runtimeVersion, latest: known.runtime.replace(/^v/, "") };
|
|
1949
|
+
}
|
|
1950
|
+
return updates.cli || updates.runtime ? updates : undefined;
|
|
1951
|
+
}
|
|
1952
|
+
function noticeLines(updates, withRuntime, run = "npx nimblebrain") {
|
|
1953
|
+
const lines = [];
|
|
1954
|
+
const cli = updates?.cli;
|
|
1955
|
+
if (cli && compareVersions(cli.latest, cli.current) > 0) {
|
|
1956
|
+
lines.push(`nimblebrain ${cli.latest} is available (you have ${cli.current}): npx nimblebrain@latest`);
|
|
1957
|
+
}
|
|
1958
|
+
const rt = updates?.runtime;
|
|
1959
|
+
if (withRuntime && rt && compareVersions(rt.latest, rt.current) > 0) {
|
|
1960
|
+
lines.push(`NimbleBrain v${rt.latest} is available (you have v${rt.current}): ${run} update`);
|
|
1961
|
+
}
|
|
1962
|
+
return lines;
|
|
1963
|
+
}
|
|
1482
1964
|
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
--yes, -y Take every default; implied when no terminal is attached
|
|
1486
|
-
--agent <name> claude-code, cursor, codex or none
|
|
1487
|
-
--port <n> Local port (default 27246)
|
|
1488
|
-
--home <dir> Where the install lives (default ~/.nimblebrain-local, or NIMBLEBRAIN_HOME);
|
|
1489
|
-
a home with another folder name is a separate install
|
|
1490
|
-
--provider <name> The model provider for key (default anthropic)
|
|
1491
|
-
--local-model <model> init: run on this Ollama model instead of a hosted key
|
|
1492
|
-
--runtime <tag> The NimbleBrain release to install or update to (default the newest)
|
|
1493
|
-
--telemetry <on|off> The person's answer to sending anonymous usage data; without it, init asks or leaves it undecided
|
|
1494
|
-
--name <name> For mcp add: the server name (default from the server's card, else its URL)
|
|
1495
|
-
--workspace <id> For mcp: the workspace (default the one init connected, the first in status)
|
|
1496
|
-
--help, --version
|
|
1497
|
-
`;
|
|
1965
|
+
// src/main.ts
|
|
1966
|
+
var RUNTIME_NOTICE = ["status", "up", "init"];
|
|
1498
1967
|
async function run(argv, deps) {
|
|
1499
1968
|
const json = wantsJson(argv);
|
|
1500
1969
|
let parsed;
|
|
1501
1970
|
try {
|
|
1502
1971
|
parsed = parse(argv);
|
|
1503
1972
|
} catch (err) {
|
|
1504
|
-
return render(toFailure(err), json);
|
|
1973
|
+
return render(toFailure(err), json, BARE_CLI);
|
|
1974
|
+
}
|
|
1975
|
+
if (parsed.kind === "help") {
|
|
1976
|
+
return parsed.flags.json ? render(describe(VERSION), true, BARE_CLI) : { stdout: helpText(VERSION), code: 0 };
|
|
1505
1977
|
}
|
|
1506
|
-
if (parsed.kind === "help")
|
|
1507
|
-
return { stdout: HELP, code: 0 };
|
|
1508
1978
|
if (parsed.kind === "version") {
|
|
1509
|
-
return parsed.flags.json ? render({ ok: true, version:
|
|
1979
|
+
return parsed.flags.json ? render({ ok: true, version: VERSION }, true, BARE_CLI) : { stdout: `${VERSION}
|
|
1510
1980
|
`, code: 0 };
|
|
1511
1981
|
}
|
|
1512
1982
|
const { command, flags, arg, operand } = parsed;
|
|
1983
|
+
const check = startUpdateCheck(deps);
|
|
1513
1984
|
const home = installHome(deps, flags.home);
|
|
1985
|
+
const cli = invocation(deps, home);
|
|
1514
1986
|
let result;
|
|
1515
1987
|
try {
|
|
1516
1988
|
switch (command) {
|
|
1517
1989
|
case "doctor": {
|
|
1518
1990
|
const install = await readInstall(deps, home);
|
|
1519
1991
|
const ours = install !== undefined && install.port === flags.port && await isHealthy(deps, flags.port);
|
|
1520
|
-
|
|
1992
|
+
const startOllama = confirmOllamaStart({ deps, flags }, false);
|
|
1993
|
+
result = { ...await doctorInstall(deps, home, flags.port, ours, startOllama) };
|
|
1521
1994
|
break;
|
|
1522
1995
|
}
|
|
1523
1996
|
case "status":
|
|
1524
1997
|
result = { ...await status(deps, home) };
|
|
1525
1998
|
break;
|
|
1526
1999
|
default: {
|
|
1527
|
-
const ctx = { deps, flags, home, version:
|
|
2000
|
+
const ctx = { deps, flags, home, version: VERSION, arg, operand };
|
|
1528
2001
|
const commands = {
|
|
1529
2002
|
init,
|
|
1530
2003
|
up,
|
|
@@ -1535,7 +2008,8 @@ async function run(argv, deps) {
|
|
|
1535
2008
|
key,
|
|
1536
2009
|
telemetry,
|
|
1537
2010
|
"local-model": localModel,
|
|
1538
|
-
mcp
|
|
2011
|
+
mcp,
|
|
2012
|
+
open
|
|
1539
2013
|
};
|
|
1540
2014
|
result = await commands[command](ctx);
|
|
1541
2015
|
}
|
|
@@ -1543,26 +2017,50 @@ async function run(argv, deps) {
|
|
|
1543
2017
|
} catch (err) {
|
|
1544
2018
|
result = toFailure(err);
|
|
1545
2019
|
}
|
|
2020
|
+
if (!result.ok)
|
|
2021
|
+
result = { ...result, fix: forInstall(result.fix, cli) };
|
|
1546
2022
|
if (!["init", "telemetry", "uninstall", "doctor"].includes(command)) {
|
|
1547
|
-
await settleWaiting(deps, home,
|
|
1548
|
-
}
|
|
1549
|
-
return
|
|
2023
|
+
await settleWaiting(deps, home, VERSION);
|
|
2024
|
+
}
|
|
2025
|
+
return answerWithUpdates(result, command, flags.json, check, cli);
|
|
2026
|
+
}
|
|
2027
|
+
async function answerWithUpdates(result, command, json, check, cli) {
|
|
2028
|
+
if (!check)
|
|
2029
|
+
return render(result, json, cli, command);
|
|
2030
|
+
const runtime = isStatusShaped(result) ? result.version : undefined;
|
|
2031
|
+
const updates = updatesField(await check.known(0), VERSION, runtime);
|
|
2032
|
+
const answer = render(updates && runtime !== undefined ? { ...result, updates } : result, json, cli, command);
|
|
2033
|
+
const notices = async () => {
|
|
2034
|
+
const known = await check.known(NOTICE_WAIT_MS);
|
|
2035
|
+
if (json)
|
|
2036
|
+
return;
|
|
2037
|
+
const lines = noticeLines(updatesField(known, VERSION, runtime), RUNTIME_NOTICE.includes(command), cli);
|
|
2038
|
+
return lines.length > 0 ? `${lines.join(`
|
|
2039
|
+
`)}
|
|
2040
|
+
` : undefined;
|
|
2041
|
+
};
|
|
2042
|
+
return { ...answer, notices };
|
|
1550
2043
|
}
|
|
1551
|
-
|
|
2044
|
+
function isStatusShaped(result) {
|
|
2045
|
+
return typeof result.version === "string" && "health" in result && "url" in result && "workspaces" in result;
|
|
2046
|
+
}
|
|
2047
|
+
async function doctorInstall(deps, home, port, ours, startOllama) {
|
|
1552
2048
|
const machine = await doctor(deps, port, ours);
|
|
1553
2049
|
const model = localModelOf(await deps.readText(paths(home).env) ?? "");
|
|
1554
2050
|
if (!model)
|
|
1555
2051
|
return machine;
|
|
1556
2052
|
try {
|
|
1557
|
-
|
|
2053
|
+
const local = await checkLocalModel(deps, model, startOllama);
|
|
2054
|
+
return {
|
|
2055
|
+
...machine,
|
|
2056
|
+
checks: [...machine.checks, ...local.checks],
|
|
2057
|
+
ollama_started: local.started
|
|
2058
|
+
};
|
|
1558
2059
|
} catch (err) {
|
|
1559
2060
|
if (!(err instanceof CliError))
|
|
1560
2061
|
throw err;
|
|
1561
2062
|
const local = err.detail.checks ?? [];
|
|
1562
|
-
throw
|
|
1563
|
-
...err.detail,
|
|
1564
|
-
checks: [...machine.checks, ...local]
|
|
1565
|
-
});
|
|
2063
|
+
throw err.with({ checks: [...machine.checks, ...local] });
|
|
1566
2064
|
}
|
|
1567
2065
|
}
|
|
1568
2066
|
async function isHealthy(deps, port) {
|
|
@@ -1574,7 +2072,7 @@ function toFailure(err) {
|
|
|
1574
2072
|
return failure(err);
|
|
1575
2073
|
return failure(new CliError("USAGE", err instanceof Error ? err.message : String(err)));
|
|
1576
2074
|
}
|
|
1577
|
-
function render(result, json, command) {
|
|
2075
|
+
function render(result, json, cli, command) {
|
|
1578
2076
|
const code = exitCodeFor(result);
|
|
1579
2077
|
if (json)
|
|
1580
2078
|
return { stdout: `${JSON.stringify(result)}
|
|
@@ -1583,7 +2081,7 @@ function render(result, json, command) {
|
|
|
1583
2081
|
return { stdout: `error: ${result.message}
|
|
1584
2082
|
fix: ${result.fix}
|
|
1585
2083
|
`, code };
|
|
1586
|
-
const lines = isStatus(result) ? statusLines(result, command) : genericLines(result);
|
|
2084
|
+
const lines = isStatus(result) ? statusLines(result, cli, command) : command === "open" ? openLines(result) : genericLines(result);
|
|
1587
2085
|
return { stdout: `${lines.join(`
|
|
1588
2086
|
`)}
|
|
1589
2087
|
`, code };
|
|
@@ -1596,25 +2094,31 @@ var AGENT_NAMES = {
|
|
|
1596
2094
|
cursor: "Cursor",
|
|
1597
2095
|
codex: "Codex"
|
|
1598
2096
|
};
|
|
1599
|
-
function statusLines(r, command) {
|
|
2097
|
+
function statusLines(r, cli, command) {
|
|
1600
2098
|
const lines = [
|
|
1601
2099
|
...changeLines(r),
|
|
1602
2100
|
` NimbleBrain ${r.version.startsWith("v") ? r.version : `v${r.version}`}: ${r.health === "ok" ? "running" : "stopped"} at ${r.url}`,
|
|
1603
2101
|
...r.workspaces.map((w) => ` workspace ${w.name}: ${w.mcp}`),
|
|
1604
|
-
r.local_model ? ` model: ${r.local_model} on Ollama (NimbleBrain's own agents use it)` : ` model key: ${r.model_key === "set" ? "set (NimbleBrain's own agents use it)" :
|
|
2102
|
+
r.local_model ? ` model: ${r.local_model} on Ollama (NimbleBrain's own agents use it)` : ` model key: ${r.model_key === "set" ? "set (NimbleBrain's own agents use it)" : `missing (run ${cli} key)`}`
|
|
1605
2103
|
];
|
|
1606
2104
|
const agent = r.agent.connected;
|
|
1607
2105
|
lines.push(...agentLines(r));
|
|
1608
2106
|
if (!r.host_loopback)
|
|
1609
|
-
lines.push(
|
|
2107
|
+
lines.push(` servers on this machine: not reachable until ${cli} update`);
|
|
1610
2108
|
lines.push(telemetryLine(r.telemetry));
|
|
1611
2109
|
lines.push(` CLI ${r.cli_version}`);
|
|
1612
2110
|
if (command === "init" && !r.already && r.health === "ok") {
|
|
1613
2111
|
const ask = agent ? `, or ask ${AGENT_NAMES[agent] ?? agent} what NimbleBrain can do` : "";
|
|
1614
|
-
|
|
2112
|
+
const use = r.opened ? `use NimbleBrain in your browser at ${r.url}` : `open ${r.url}`;
|
|
2113
|
+
lines.push("", `Next: ${use}${ask}. Stop it with ${cli} down.`);
|
|
1615
2114
|
}
|
|
1616
2115
|
return lines;
|
|
1617
2116
|
}
|
|
2117
|
+
function openLines(r) {
|
|
2118
|
+
return [
|
|
2119
|
+
r.opened ? ` Opened ${String(r.url)} in your browser.` : ` Open ${String(r.url)} in your browser; none could be started here.`
|
|
2120
|
+
];
|
|
2121
|
+
}
|
|
1618
2122
|
function agentLines(r) {
|
|
1619
2123
|
const agent = r.agent.connected;
|
|
1620
2124
|
if (!agent)
|
|
@@ -1623,10 +2127,19 @@ function agentLines(r) {
|
|
|
1623
2127
|
const lines = [
|
|
1624
2128
|
` ${name} can use NimbleBrain's tools (it loads them when its next session starts)`
|
|
1625
2129
|
];
|
|
2130
|
+
const refresh = r.skill_update && SKILL_REFRESH_LINES[r.skill_update];
|
|
2131
|
+
if (refresh)
|
|
2132
|
+
lines.push(` skill: ${refresh}`);
|
|
1626
2133
|
if (!r.agent.skill)
|
|
1627
2134
|
lines.push(` skill: not installed; add it with npx skills add nimblebraininc/skills --skill nimblebrain --agent ${agent} -g`);
|
|
1628
2135
|
return lines;
|
|
1629
2136
|
}
|
|
2137
|
+
var SKILL_REFRESH_LINES = {
|
|
2138
|
+
updated: "updated to the newest nimblebrain skill",
|
|
2139
|
+
unchanged: "the nimblebrain skill is current",
|
|
2140
|
+
failed: "could not refresh the nimblebrain skill; try npx skills update nimblebrain -g",
|
|
2141
|
+
not_installed: undefined
|
|
2142
|
+
};
|
|
1630
2143
|
function changeLines(r) {
|
|
1631
2144
|
if (r.already)
|
|
1632
2145
|
return [" already installed"];
|
|
@@ -1659,18 +2172,24 @@ function field2(key, value) {
|
|
|
1659
2172
|
}
|
|
1660
2173
|
return [` ${key}: ${String(value)}`];
|
|
1661
2174
|
}
|
|
1662
|
-
function interrupted(argv, exitCode) {
|
|
2175
|
+
function interrupted(argv, exitCode, deps) {
|
|
1663
2176
|
const err = new CliError("INTERRUPTED", `The CLI stopped before it answered (exit code ${exitCode}).`, { exit_code: exitCode });
|
|
1664
|
-
|
|
2177
|
+
let cli = BARE_CLI;
|
|
2178
|
+
try {
|
|
2179
|
+
cli = invocation(deps, installHome(deps, parse(argv).flags.home));
|
|
2180
|
+
} catch {}
|
|
2181
|
+
const result = failure(err);
|
|
2182
|
+
return render({ ...result, fix: forInstall(result.fix, cli) }, wantsJson(argv), cli);
|
|
1665
2183
|
}
|
|
1666
2184
|
|
|
1667
2185
|
// src/bin.ts
|
|
1668
2186
|
var argv = process.argv.slice(2);
|
|
2187
|
+
var deps = realDeps();
|
|
1669
2188
|
var answered = false;
|
|
1670
2189
|
process.on("exit", (code) => {
|
|
1671
2190
|
if (answered)
|
|
1672
2191
|
return;
|
|
1673
|
-
const out = interrupted(argv, code);
|
|
2192
|
+
const out = interrupted(argv, code, deps);
|
|
1674
2193
|
writeSync(1, out.stdout);
|
|
1675
2194
|
writeSync(2, `nimblebrain: stopped before answering (exit code ${code})
|
|
1676
2195
|
`);
|
|
@@ -1683,7 +2202,10 @@ for (const [signal, n] of [
|
|
|
1683
2202
|
]) {
|
|
1684
2203
|
process.on(signal, () => process.exit(128 + n));
|
|
1685
2204
|
}
|
|
1686
|
-
var
|
|
1687
|
-
writeSync(1, stdout);
|
|
2205
|
+
var answer = await run(argv, deps);
|
|
2206
|
+
writeSync(1, answer.stdout);
|
|
1688
2207
|
answered = true;
|
|
1689
|
-
|
|
2208
|
+
var notices = await answer.notices?.();
|
|
2209
|
+
if (notices)
|
|
2210
|
+
writeSync(2, notices);
|
|
2211
|
+
process.exit(answer.code);
|