@nodetool-ai/cli 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/README.md +4 -1
  2. package/dist/.nodetool-build-stamp +1 -1
  3. package/dist/affected/affected.d.ts +7 -12
  4. package/dist/affected/affected.d.ts.map +1 -1
  5. package/dist/affected/affected.js +42 -0
  6. package/dist/affected/affected.js.map +1 -1
  7. package/dist/app-debug/app-target.d.ts +1 -1
  8. package/dist/app-debug/app-target.d.ts.map +1 -1
  9. package/dist/app-debug/harness.d.ts +3 -1
  10. package/dist/app-debug/harness.d.ts.map +1 -1
  11. package/dist/app-debug/harness.js +23 -0
  12. package/dist/app-debug/harness.js.map +1 -1
  13. package/dist/app.d.ts.map +1 -1
  14. package/dist/app.js +2 -1
  15. package/dist/app.js.map +1 -1
  16. package/dist/chat-codeact.d.ts +8 -0
  17. package/dist/chat-codeact.d.ts.map +1 -1
  18. package/dist/chat-codeact.js +59 -21
  19. package/dist/chat-codeact.js.map +1 -1
  20. package/dist/chat-context.d.ts.map +1 -1
  21. package/dist/chat-context.js +7 -2
  22. package/dist/chat-context.js.map +1 -1
  23. package/dist/commands/__tests__/timeline-render-audio.test.js +1 -1
  24. package/dist/commands/__tests__/timeline-render-audio.test.js.map +1 -1
  25. package/dist/commands/__tests__/timeline-render-completeness.test.d.ts +2 -0
  26. package/dist/commands/__tests__/timeline-render-completeness.test.d.ts.map +1 -0
  27. package/dist/commands/__tests__/timeline-render-completeness.test.js +117 -0
  28. package/dist/commands/__tests__/timeline-render-completeness.test.js.map +1 -0
  29. package/dist/commands/__tests__/timeline-score.test.d.ts +2 -0
  30. package/dist/commands/__tests__/timeline-score.test.d.ts.map +1 -0
  31. package/dist/commands/__tests__/timeline-score.test.js +100 -0
  32. package/dist/commands/__tests__/timeline-score.test.js.map +1 -0
  33. package/dist/commands/agent.d.ts.map +1 -1
  34. package/dist/commands/agent.js +2 -1
  35. package/dist/commands/agent.js.map +1 -1
  36. package/dist/commands/app.d.ts.map +1 -1
  37. package/dist/commands/app.js +29 -5
  38. package/dist/commands/app.js.map +1 -1
  39. package/dist/commands/debug-summary-output.d.ts +22 -0
  40. package/dist/commands/debug-summary-output.d.ts.map +1 -0
  41. package/dist/commands/debug-summary-output.js +28 -0
  42. package/dist/commands/debug-summary-output.js.map +1 -0
  43. package/dist/commands/errors.d.ts +11 -0
  44. package/dist/commands/errors.d.ts.map +1 -0
  45. package/dist/commands/errors.js +177 -0
  46. package/dist/commands/errors.js.map +1 -0
  47. package/dist/commands/eval.d.ts.map +1 -1
  48. package/dist/commands/eval.js +2 -0
  49. package/dist/commands/eval.js.map +1 -1
  50. package/dist/commands/extension.d.ts +12 -0
  51. package/dist/commands/extension.d.ts.map +1 -0
  52. package/dist/commands/extension.js +30 -0
  53. package/dist/commands/extension.js.map +1 -0
  54. package/dist/commands/game-benchmark.d.ts +34 -0
  55. package/dist/commands/game-benchmark.d.ts.map +1 -0
  56. package/dist/commands/game-benchmark.js +95 -0
  57. package/dist/commands/game-benchmark.js.map +1 -0
  58. package/dist/commands/game-smoke.d.ts +3 -0
  59. package/dist/commands/game-smoke.d.ts.map +1 -0
  60. package/dist/commands/game-smoke.js +23 -0
  61. package/dist/commands/game-smoke.js.map +1 -0
  62. package/dist/commands/game.d.ts.map +1 -1
  63. package/dist/commands/game.js +247 -18
  64. package/dist/commands/game.js.map +1 -1
  65. package/dist/commands/harness.d.ts +50 -0
  66. package/dist/commands/harness.d.ts.map +1 -1
  67. package/dist/commands/harness.js +284 -70
  68. package/dist/commands/harness.js.map +1 -1
  69. package/dist/commands/js-script.d.ts.map +1 -1
  70. package/dist/commands/js-script.js +6 -22
  71. package/dist/commands/js-script.js.map +1 -1
  72. package/dist/commands/mcp.d.ts +21 -0
  73. package/dist/commands/mcp.d.ts.map +1 -0
  74. package/dist/commands/mcp.js +164 -0
  75. package/dist/commands/mcp.js.map +1 -0
  76. package/dist/commands/package.d.ts.map +1 -1
  77. package/dist/commands/package.js +12 -12
  78. package/dist/commands/package.js.map +1 -1
  79. package/dist/commands/runs.d.ts +7 -0
  80. package/dist/commands/runs.d.ts.map +1 -0
  81. package/dist/commands/runs.js +302 -0
  82. package/dist/commands/runs.js.map +1 -0
  83. package/dist/commands/sketch.d.ts.map +1 -1
  84. package/dist/commands/sketch.js +6 -22
  85. package/dist/commands/sketch.js.map +1 -1
  86. package/dist/commands/timeline-render.d.ts +6 -1
  87. package/dist/commands/timeline-render.d.ts.map +1 -1
  88. package/dist/commands/timeline-render.js +13 -4
  89. package/dist/commands/timeline-render.js.map +1 -1
  90. package/dist/commands/timeline-score.d.ts +16 -0
  91. package/dist/commands/timeline-score.d.ts.map +1 -0
  92. package/dist/commands/timeline-score.js +89 -0
  93. package/dist/commands/timeline-score.js.map +1 -0
  94. package/dist/commands/timeline.d.ts.map +1 -1
  95. package/dist/commands/timeline.js +8 -22
  96. package/dist/commands/timeline.js.map +1 -1
  97. package/dist/commands/worker.js +1 -1
  98. package/dist/commands/worker.js.map +1 -1
  99. package/dist/debug/server-runner.d.ts +2 -0
  100. package/dist/debug/server-runner.d.ts.map +1 -1
  101. package/dist/debug/server-runner.js +37 -4
  102. package/dist/debug/server-runner.js.map +1 -1
  103. package/dist/harness/capability-table.d.ts.map +1 -1
  104. package/dist/harness/capability-table.js +295 -27
  105. package/dist/harness/capability-table.js.map +1 -1
  106. package/dist/harness/changed-files.d.ts +2 -0
  107. package/dist/harness/changed-files.d.ts.map +1 -1
  108. package/dist/harness/changed-files.js +14 -0
  109. package/dist/harness/changed-files.js.map +1 -1
  110. package/dist/harness/registry.d.ts +32 -4
  111. package/dist/harness/registry.d.ts.map +1 -1
  112. package/dist/harness/registry.js +327 -31
  113. package/dist/harness/registry.js.map +1 -1
  114. package/dist/index.js +1 -1
  115. package/dist/index.js.map +1 -1
  116. package/dist/interaction-script.d.ts +23 -0
  117. package/dist/interaction-script.d.ts.map +1 -1
  118. package/dist/interaction-script.js +50 -1
  119. package/dist/interaction-script.js.map +1 -1
  120. package/dist/js-script-debug/harness.d.ts +11 -1
  121. package/dist/js-script-debug/harness.d.ts.map +1 -1
  122. package/dist/js-script-debug/harness.js +17 -39
  123. package/dist/js-script-debug/harness.js.map +1 -1
  124. package/dist/local-model-interfaces.d.ts.map +1 -1
  125. package/dist/local-model-interfaces.js +1 -24
  126. package/dist/local-model-interfaces.js.map +1 -1
  127. package/dist/node-registry.d.ts +14 -0
  128. package/dist/node-registry.d.ts.map +1 -1
  129. package/dist/node-registry.js +15 -18
  130. package/dist/node-registry.js.map +1 -1
  131. package/dist/nodetool-main.d.ts +15 -0
  132. package/dist/nodetool-main.d.ts.map +1 -0
  133. package/dist/nodetool-main.js +1368 -0
  134. package/dist/nodetool-main.js.map +1 -0
  135. package/dist/nodetool.d.ts +0 -13
  136. package/dist/nodetool.d.ts.map +1 -1
  137. package/dist/nodetool.js +17 -1683
  138. package/dist/nodetool.js.map +1 -1
  139. package/dist/providers.d.ts +1 -1
  140. package/dist/providers.d.ts.map +1 -1
  141. package/dist/providers.js +11 -1
  142. package/dist/providers.js.map +1 -1
  143. package/dist/run-dsl.d.ts +6 -0
  144. package/dist/run-dsl.d.ts.map +1 -1
  145. package/dist/run-dsl.js +4 -1
  146. package/dist/run-dsl.js.map +1 -1
  147. package/dist/sandbox-catalog.d.ts +12 -0
  148. package/dist/sandbox-catalog.d.ts.map +1 -1
  149. package/dist/sandbox-catalog.js +51 -0
  150. package/dist/sandbox-catalog.js.map +1 -1
  151. package/dist/sketch-debug/harness.d.ts.map +1 -1
  152. package/dist/sketch-debug/harness.js +2 -35
  153. package/dist/sketch-debug/harness.js.map +1 -1
  154. package/dist/stdin.d.ts.map +1 -1
  155. package/dist/stdin.js +2 -1
  156. package/dist/stdin.js.map +1 -1
  157. package/dist/timeline-debug/harness.d.ts.map +1 -1
  158. package/dist/timeline-debug/harness.js +2 -35
  159. package/dist/timeline-debug/harness.js.map +1 -1
  160. package/package.json +38 -35
@@ -0,0 +1,1368 @@
1
+ /**
2
+ * nodetool — Top-level CLI mirroring the Python nodetool CLI.
3
+ *
4
+ * Usage:
5
+ * nodetool info # system/env info
6
+ * nodetool serve [--host] [--port] # start TS server
7
+ * nodetool chat [--url] [--provider] # interactive chat
8
+ * nodetool workflows list/get/run
9
+ * nodetool jobs list/get
10
+ * nodetool assets list/get
11
+ * nodetool secrets list/store <key>
12
+ * nodetool settings show
13
+ */
14
+ import { program } from "commander";
15
+ import { createRequire } from "node:module";
16
+ import { fileURLToPath } from "node:url";
17
+ import { dirname, resolve } from "node:path";
18
+ import { spawnSync } from "node:child_process";
19
+ import { createInterface } from "node:readline";
20
+ import { workflowToDsl } from "@nodetool-ai/dsl";
21
+ import { initDb, migrateSqliteDb, Workflow, Asset, Secret, getSecret } from "@nodetool-ai/models";
22
+ import { readCachedHfModels, searchCachedHfModels } from "@nodetool-ai/huggingface";
23
+ import { initMasterKey } from "@nodetool-ai/security";
24
+ import { installLocalModelInterfaces } from "./local-model-interfaces.js";
25
+ import { getDefaultDbPath, getDefaultAssetsPath } from "@nodetool-ai/config";
26
+ import { ExecutionSession, isExecutionPreflightError } from "@nodetool-ai/execution";
27
+ import { NodeRegistry, createGraphNodeTypeResolver } from "@nodetool-ai/node-sdk";
28
+ import { registerBuiltinPacks } from "@nodetool-ai/base-nodes/builtin-packs";
29
+ import { CLI_LOCAL_BUILTIN_PACK_POLICY, CLI_DSL_BUILTIN_PACK_POLICY } from "./node-registry.js";
30
+ import { ProcessingContext, createLocalWorkspace, initTelemetry, onPythonBridgeCreated } from "@nodetool-ai/runtime";
31
+ import { FileStorageAdapter } from "@nodetool-ai/storage";
32
+ import { mkdirSync } from "node:fs";
33
+ import { addSupervisorOptions, createSupervisorHandle, parseSupervisorFlags, printSupervisedSummary, recordSupervisorCost, streamInterventionLines } from "./supervisor.js";
34
+ import { registerPackageCommands } from "./commands/package.js";
35
+ import { registerPackCommands } from "./commands/packs.js";
36
+ import { registerDeployCommands } from "./commands/deploy.js";
37
+ import { registerHfCommands } from "./commands/models-hf.js";
38
+ import { registerWorkerCommands } from "./commands/worker.js";
39
+ import { installWorkerActivityHeartbeat } from "@nodetool-ai/compute";
40
+ import { registerRecommendedCommand } from "./commands/models-recommended.js";
41
+ import { registerAgentCommands } from "./commands/agent.js";
42
+ import { registerDbCommands } from "./commands/db.js";
43
+ import { registerDebugCommands } from "./commands/debug.js";
44
+ import { registerAppCommands } from "./commands/app.js";
45
+ import { registerAppsCommands } from "./commands/apps.js";
46
+ import { registerValidateCommand } from "./commands/validate.js";
47
+ import { registerTimelineCommands } from "./commands/timeline.js";
48
+ import { registerSketchCommands } from "./commands/sketch.js";
49
+ import { registerJsScriptCommands } from "./commands/js-script.js";
50
+ import { registerReliabilityCommands } from "./commands/reliability.js";
51
+ import { registerNodeCommands } from "./commands/node.js";
52
+ import { registerGenerateCommand } from "./commands/generate.js";
53
+ import { registerExtensionCommands } from "./commands/extension.js";
54
+ import { registerEvalCommand } from "./commands/eval.js";
55
+ import { registerJtbdCommand } from "./commands/jtbd.js";
56
+ import { registerAffectedCommand } from "./commands/affected.js";
57
+ import { registerMcpClientCommands } from "./commands/mcp.js";
58
+ import { registerHarnessCommands } from "./commands/harness.js";
59
+ import { registerCollectionCommands } from "./commands/collections.js";
60
+ import { registerCostsCommands } from "./commands/costs.js";
61
+ import { registerErrorsCommands } from "./commands/errors.js";
62
+ import { registerGenerationsCommands } from "./commands/generations.js";
63
+ import { registerRunsCommands } from "./commands/runs.js";
64
+ import { registerGameCommands } from "./commands/game.js";
65
+ import { registerAuthCommands } from "./commands/auth.js";
66
+ import { registerTelegramCommands } from "./commands/telegram.js";
67
+ import { registerResourceReadCommands } from "./commands/resource-read.js";
68
+ import { createApiClient } from "./api-client.js";
69
+ import { listAllModels, listConfiguredProviderInfo, listOllamaModels, listProviderModels } from "./providers.js";
70
+ import { isString } from "./predicates.js";
71
+ import { printTable, asJson } from "./commands/output.js";
72
+ import { printCommandError } from "./command-errors.js";
73
+ const __filename = fileURLToPath(import.meta.url);
74
+ const __dirname = dirname(__filename);
75
+ /**
76
+ * The CLI's own version, read from its package.json rather than repeated here.
77
+ * A literal drifts silently: it sat at "0.1.0" across every 0.7.0-rc release,
78
+ * so `nodetool --version` told users nothing about what they had installed.
79
+ */
80
+ const CLI_VERSION = createRequire(import.meta.url)("../package.json").version;
81
+ // ---------------------------------------------------------------------------
82
+ // DB setup (for secrets commands)
83
+ // ---------------------------------------------------------------------------
84
+ // The local single-user id every direct-DB command runs as, matching the
85
+ // `workflows run` / `secrets` commands and the stdio MCP server.
86
+ import { resolveLocalSecret } from "./local-secrets.js";
87
+ const LOCAL_USER_ID = "1";
88
+ // Open the local SQLite database for a read-only command. Unlike setupDb() this
89
+ // skips master-key unlocking (no secret decryption needed to list/read rows),
90
+ // so it never emits a keychain warning. Idempotent — initDb() is a no-op once
91
+ // a connection is open.
92
+ function ensureDb() {
93
+ initDb(getDefaultDbPath());
94
+ }
95
+ async function setupDb() {
96
+ ensureDb();
97
+ // Resolve the master encryption key from keychain / env / AWS so that
98
+ // both encryption (Secret.upsert) and decryption (Secret.getDecryptedValue)
99
+ // use the same persistent key. Without this, a fresh process would
100
+ // auto-generate a one-shot key inside `getMasterKey()`, encrypt with it,
101
+ // then lose it on exit — and the next launch could not decrypt.
102
+ try {
103
+ await initMasterKey();
104
+ }
105
+ catch (err) {
106
+ const msg = err instanceof Error ? err.message : String(err);
107
+ process.stderr.write(`Could not unlock the secret store: ${msg}\n` +
108
+ `Tip: set SECRETS_MASTER_KEY or grant keychain access.\n`);
109
+ }
110
+ }
111
+ // ---------------------------------------------------------------------------
112
+ // API clients
113
+ // ---------------------------------------------------------------------------
114
+ // REST GET (text) — used for the DSL export endpoint which stays REST
115
+ // because it returns a TypeScript source string rather than JSON.
116
+ async function apiGetText(apiUrl, path) {
117
+ const res = await fetch(`${apiUrl}${path}`);
118
+ if (!res.ok)
119
+ throw new Error(`HTTP ${res.status}: ${await res.text()}`);
120
+ return res.text();
121
+ }
122
+ // This CLI's package version, stamped into exported bundle manifests.
123
+ function cliVersion() {
124
+ try {
125
+ const req = createRequire(import.meta.url);
126
+ return req("../package.json").version;
127
+ }
128
+ catch {
129
+ // Version is best-effort metadata; omit it if the file can't be read.
130
+ return undefined;
131
+ }
132
+ }
133
+ // Download asset bytes from a running server, used by the export commands to
134
+ // resolve `asset://` / `/api/storage/` (and optionally http) refs into bytes.
135
+ function makeAssetFetcher(apiUrl) {
136
+ return async (ref) => {
137
+ let url = null;
138
+ if (ref.startsWith("asset://")) {
139
+ const id = ref.slice("asset://".length);
140
+ const bareId = id.replace(/\.[^.]+$/, "");
141
+ try {
142
+ const meta = await fetch(`${apiUrl}/api/assets/${encodeURIComponent(bareId)}`);
143
+ if (meta.ok) {
144
+ const j = (await meta.json());
145
+ if (j.get_url) {
146
+ url = j.get_url.startsWith("/")
147
+ ? `${apiUrl}${j.get_url}`
148
+ : j.get_url;
149
+ }
150
+ }
151
+ }
152
+ catch {
153
+ // Metadata lookup failed — fall back to the storage route below.
154
+ }
155
+ url ??= `${apiUrl}/api/storage/${encodeURIComponent(id)}`;
156
+ }
157
+ else if (ref.includes("/api/storage/")) {
158
+ url = /^https?:\/\//.test(ref)
159
+ ? ref
160
+ : `${apiUrl}${ref.startsWith("/") ? ref : `/${ref}`}`;
161
+ }
162
+ else if (/^https?:\/\//.test(ref)) {
163
+ url = ref;
164
+ }
165
+ if (!url)
166
+ return null;
167
+ const res = await fetch(url);
168
+ if (!res.ok)
169
+ return null;
170
+ return new Uint8Array(await res.arrayBuffer());
171
+ };
172
+ }
173
+ // Resolve asset bytes from the local assets directory, so the export commands
174
+ // work by id without a running server. `asset://<key>` is tried verbatim as a
175
+ // storage key first (that's how import-bundle writes refs), then via the DB
176
+ // record to derive `<id>.<ext>` from the content type. Remote http(s) refs
177
+ // still fetch over the network.
178
+ async function makeLocalAssetFetcher() {
179
+ const { getAssetFileName } = await import("@nodetool-ai/websocket");
180
+ const storage = new FileStorageAdapter(getDefaultAssetsPath());
181
+ return async (ref) => {
182
+ if (ref.startsWith("asset://")) {
183
+ const raw = ref.slice("asset://".length);
184
+ const direct = await storage.retrieve(`/api/storage/${raw}`);
185
+ if (direct)
186
+ return direct;
187
+ const bareId = raw.replace(/\.[^.]+$/, "");
188
+ const asset = await Asset.find(LOCAL_USER_ID, bareId);
189
+ if (!asset)
190
+ return null;
191
+ const key = getAssetFileName(asset.id, asset.content_type);
192
+ return storage.retrieve(`/api/storage/${key}`);
193
+ }
194
+ if (ref.includes("/api/storage/")) {
195
+ const local = await storage.retrieve(ref);
196
+ if (local)
197
+ return local;
198
+ }
199
+ if (/^https?:\/\//.test(ref)) {
200
+ const res = await fetch(ref);
201
+ if (!res.ok)
202
+ return null;
203
+ return new Uint8Array(await res.arrayBuffer());
204
+ }
205
+ return null;
206
+ };
207
+ }
208
+ // Asset and timeline persistence for every context any command builds, here
209
+ // rather than per-entrance: `debug` wired none while `workflows run` wired
210
+ // four, so one workflow saved an image under one command and threw under the
211
+ // other. The interfaces touch the database only when a node calls them, so
212
+ // installing them before `setupDb()` is safe.
213
+ await installLocalModelInterfaces();
214
+ /**
215
+ * What to print when a run command fails. A preflight refusal already reads as
216
+ * a complete explanation — printing the error class in front of it only buries
217
+ * the secret name the user has to go set.
218
+ */
219
+ function describeRunFailure(error) {
220
+ return isExecutionPreflightError(error) ? error.message : String(error);
221
+ }
222
+ // ---------------------------------------------------------------------------
223
+ // info
224
+ // ---------------------------------------------------------------------------
225
+ program
226
+ .name("nodetool")
227
+ .description("NodeTool CLI")
228
+ .version(CLI_VERSION)
229
+ .option("--trace-file <path>", "Append every LLM/agent/workflow span as JSONL to <path>")
230
+ .option("--trace-stdout [format]", "Stream spans to stdout: 'pretty' (default) or 'json' (JSONL)")
231
+ .option("--no-trace-stdout", "Disable stdout span output (overrides NODETOOL_TRACE_STDOUT)")
232
+ .hook("preAction", async (thisCommand, actionCommand) => {
233
+ // With `--trace`, the `debug` command owns telemetry: it routes spans into
234
+ // its bundle's trace.jsonl via its own initTelemetry call. Initializing here
235
+ // first would win the one-shot init and drop that file sink, so skip it.
236
+ // Without `--trace`, fall through so the global --trace-file/--trace-stdout
237
+ // flags still work for a debug run.
238
+ if (actionCommand?.name() === "debug" && actionCommand.opts()["trace"])
239
+ return;
240
+ const opts = thisCommand.opts();
241
+ await initTelemetry({
242
+ ...(opts.traceFile && { traceFile: opts.traceFile }),
243
+ ...(opts.traceStdout !== undefined && {
244
+ stdout: parseTraceStdout(opts.traceStdout)
245
+ })
246
+ });
247
+ });
248
+ function parseTraceStdout(v) {
249
+ if (v === false)
250
+ return false;
251
+ if (v === true)
252
+ return "pretty";
253
+ if (isString(v)) {
254
+ const lower = v.toLowerCase();
255
+ if (lower === "false" || lower === "0" || lower === "no")
256
+ return false;
257
+ if (lower === "json")
258
+ return "json";
259
+ if (lower === "pretty" || lower === "true" || lower === "1")
260
+ return "pretty";
261
+ }
262
+ throw new Error(`--trace-stdout must be 'pretty' or 'json' (got ${JSON.stringify(v)})`);
263
+ }
264
+ // ---------------------------------------------------------------------------
265
+ // run — execute a TypeScript/JavaScript DSL workflow file
266
+ // ---------------------------------------------------------------------------
267
+ addSupervisorOptions(program
268
+ .command("run <dsl-file>")
269
+ .description("Run a TypeScript/JavaScript DSL workflow file")
270
+ .option("--json", "Output results as JSON (default: pretty-print)")).action(async (dslFile, opts) => {
271
+ try {
272
+ const { resolve } = await import("node:path");
273
+ const { runDslFile } = await import("./run-dsl.js");
274
+ const { NodeRegistry } = await import("@nodetool-ai/node-sdk");
275
+ registerBuiltinPacks(NodeRegistry.global, CLI_DSL_BUILTIN_PACK_POLICY);
276
+ const supervisorConfig = parseSupervisorFlags(opts);
277
+ const absolutePath = resolve(dslFile);
278
+ let results;
279
+ let interventions = [];
280
+ if (supervisorConfig) {
281
+ // The supervisor's provider key comes from the secret store, and its
282
+ // spend is written to the local ledger — both need the DB.
283
+ await setupDb();
284
+ const { runDslFileSupervised } = await import("./run-dsl-supervised.js");
285
+ const run = await runDslFileSupervised(absolutePath, supervisorConfig, opts.json === true);
286
+ results = run.results;
287
+ interventions = run.interventions;
288
+ }
289
+ else {
290
+ results = await runDslFile(absolutePath);
291
+ }
292
+ const { printRunJson, previewJson } = await import("./run-json.js");
293
+ if (opts.json) {
294
+ // A supervised run reports what the supervisor did alongside the
295
+ // outputs; an unsupervised one keeps the bare results shape.
296
+ await printRunJson(supervisorConfig ? { results, interventions } : results);
297
+ }
298
+ else {
299
+ for (const [workflowName, outputs] of Object.entries(results)) {
300
+ console.log(`\n${workflowName}:`);
301
+ for (const [key, value] of Object.entries(outputs)) {
302
+ console.log(` ${key}: ${previewJson(value)}`);
303
+ }
304
+ }
305
+ }
306
+ }
307
+ catch (e) {
308
+ console.error(describeRunFailure(e));
309
+ process.exit(1);
310
+ }
311
+ });
312
+ program
313
+ .command("info")
314
+ .description("Display system and environment information")
315
+ .option("--json", "Output as JSON")
316
+ .action((opts) => {
317
+ const apiKeys = [
318
+ "ANTHROPIC_API_KEY",
319
+ "OPENAI_API_KEY",
320
+ "GEMINI_API_KEY",
321
+ "MISTRAL_API_KEY",
322
+ "GROQ_API_KEY",
323
+ "OLLAMA_API_URL",
324
+ "SERPAPI_API_KEY",
325
+ "HF_TOKEN"
326
+ ];
327
+ const data = {
328
+ version: CLI_VERSION,
329
+ node_version: process.version,
330
+ platform: process.platform,
331
+ arch: process.arch,
332
+ api_keys: Object.fromEntries(apiKeys.map((k) => [k, process.env[k] ? "configured" : "not set"])),
333
+ environment: {
334
+ ENV: process.env["ENV"] ?? "development",
335
+ LOG_LEVEL: process.env["LOG_LEVEL"] ?? "INFO",
336
+ PORT: process.env["PORT"] ?? "7777"
337
+ }
338
+ };
339
+ if (opts.json) {
340
+ asJson(data);
341
+ return;
342
+ }
343
+ console.log("\nNodeTool System Information\n");
344
+ printTable([
345
+ { property: "Version", value: data.version },
346
+ { property: "Node.js", value: data.node_version },
347
+ { property: "Platform", value: `${data.platform}/${data.arch}` }
348
+ ]);
349
+ console.log("\nAPI Keys:");
350
+ printTable(Object.entries(data.api_keys).map(([k, v]) => ({ key: k, status: v })));
351
+ console.log("\nEnvironment:");
352
+ printTable(Object.entries(data.environment).map(([k, v]) => ({
353
+ variable: k,
354
+ value: v
355
+ })));
356
+ console.log();
357
+ });
358
+ // ---------------------------------------------------------------------------
359
+ // serve
360
+ // ---------------------------------------------------------------------------
361
+ program
362
+ .command("serve")
363
+ .description("Start the NodeTool WebSocket + HTTP server")
364
+ .option("--host <host>", "Host to bind", "127.0.0.1")
365
+ .option("--port <port>", "Port to listen on", "7777")
366
+ .action((opts) => {
367
+ const serverPath = resolve(__dirname, "../../websocket/dist/server.js");
368
+ process.env["HOST"] = opts.host;
369
+ process.env["PORT"] = opts.port;
370
+ const result = spawnSync("node", [serverPath], {
371
+ stdio: "inherit",
372
+ env: { ...process.env }
373
+ });
374
+ process.exit(result.status ?? 0);
375
+ });
376
+ // ---------------------------------------------------------------------------
377
+ // chat
378
+ // ---------------------------------------------------------------------------
379
+ program
380
+ .command("chat")
381
+ .description("Start fullscreen chat")
382
+ .helpOption(false)
383
+ .option("-p, --provider <provider>", "LLM provider")
384
+ .option("-m, --model <model>", "Model ID")
385
+ .option("-a, --agent", "Agent mode")
386
+ .option("-u, --url <url>", "WebSocket server URL")
387
+ .option("-w, --workspace <path>", "Workspace directory")
388
+ .option("--tools <tools>", "Comma-separated enabled tools")
389
+ .allowUnknownOption()
390
+ .action((_opts, cmd) => {
391
+ const chatPath = resolve(__dirname, import.meta.url.endsWith(".ts") ? "index.ts" : "index.js");
392
+ const args = cmd.args;
393
+ const rawArgs = process.argv;
394
+ // Re-run with the chat entry point, forwarding all flags after "chat"
395
+ const chatIdx = rawArgs.indexOf("chat");
396
+ const forwarded = chatIdx >= 0 ? rawArgs.slice(chatIdx + 1) : args;
397
+ const result = spawnSync(process.execPath, [...process.execArgv, chatPath, ...forwarded], {
398
+ stdio: "inherit",
399
+ env: { ...process.env }
400
+ });
401
+ process.exit(result.status ?? 1);
402
+ });
403
+ // ---------------------------------------------------------------------------
404
+ // workflows
405
+ // ---------------------------------------------------------------------------
406
+ const workflows = program
407
+ .command("workflows")
408
+ .description("Workflow management");
409
+ addSupervisorOptions(workflows
410
+ .command("run <workflow_id_or_file>")
411
+ .description("Run a workflow by ID, JSON file, or TypeScript DSL file")
412
+ .option("--params <json>", "JSON params string")
413
+ .option("--json", "Output as JSON")
414
+ .option("--asset-output-mode <mode>", "How media outputs are materialized: native | raw | data_uri | workspace | storage_url | temp_url", "native")
415
+ .option("--workspace <dir>", "Workspace directory for workspace-mode asset output (default: ./nodetool-output)")
416
+ .option("--trigger-event <json>", 'Wake a trigger node as the scheduler/webhook adapter would: \'{"node_id":"watch","payload":{"path":"/tmp/a.csv","event":"created"}}\'. ' +
417
+ "Without it a webhook, file-watch or manual trigger has no event to " +
418
+ "emit and the run stalls until its timeout — so this is the only way " +
419
+ "to exercise a trigger-driven workflow from the CLI.")).action(async (idOrFile, opts) => {
420
+ try {
421
+ const supervisorConfig = parseSupervisorFlags(opts);
422
+ await setupDb();
423
+ let graph;
424
+ let workflowId = null;
425
+ const params = opts.params
426
+ ? JSON.parse(opts.params)
427
+ : {};
428
+ // A trigger node emits from `emitTriggerEvent`, not `genProcess`, and
429
+ // only when the run carries an event addressed to it. The kernel has
430
+ // always accepted one (`RunJobRequest.trigger_event`); nothing on the
431
+ // CLI could supply it, so a webhook or file-watch workflow could be
432
+ // validated but never executed. `input_id` defaults because every
433
+ // caller would otherwise invent the same throwaway id.
434
+ const triggerEvent = opts.triggerEvent
435
+ ? (() => {
436
+ const parsed = JSON.parse(opts.triggerEvent);
437
+ if (!parsed.node_id) {
438
+ throw new Error('--trigger-event needs a "node_id" naming the trigger node to wake.');
439
+ }
440
+ return {
441
+ node_id: parsed.node_id,
442
+ payload: parsed.payload ?? {},
443
+ input_id: parsed.input_id ?? `cli-${Date.now()}`
444
+ };
445
+ })()
446
+ : null;
447
+ // Determine if argument is a file path or workflow ID
448
+ if (idOrFile.endsWith(".json") ||
449
+ idOrFile.endsWith(".ts") ||
450
+ idOrFile.endsWith(".tsx") ||
451
+ idOrFile.includes("/") ||
452
+ idOrFile.includes("\\")) {
453
+ /** A DSL/JSON workflow file: the graph, optionally wrapped. */
454
+ let raw;
455
+ if (idOrFile.endsWith(".ts") || idOrFile.endsWith(".tsx")) {
456
+ // Execute DSL file via tsx and capture JSON output
457
+ const { execFileSync } = await import("node:child_process");
458
+ const output = execFileSync("npx", ["tsx", resolve(idOrFile)], {
459
+ encoding: "utf8",
460
+ cwd: dirname(resolve(idOrFile)),
461
+ timeout: 30000
462
+ });
463
+ raw = JSON.parse(output.trim());
464
+ }
465
+ else {
466
+ const { readFileSync } = await import("node:fs");
467
+ raw = JSON.parse(readFileSync(idOrFile, "utf8"));
468
+ }
469
+ graph = raw.graph ?? raw;
470
+ workflowId = raw.workflow_id ?? raw.id ?? null;
471
+ if (raw.params)
472
+ Object.assign(params, raw.params);
473
+ }
474
+ else {
475
+ const wf = await Workflow.get(idOrFile);
476
+ if (!wf)
477
+ throw new Error(`Workflow not found: ${idOrFile}`);
478
+ graph = wf.graph;
479
+ workflowId = idOrFile;
480
+ }
481
+ if (!graph?.nodes || !graph?.edges) {
482
+ throw new Error("Invalid workflow: missing nodes or edges");
483
+ }
484
+ // Editor-only nodes (Comment, Group, Reroute) and the data→properties
485
+ // rename are handled by `ExecutionSession.create` below, which runs
486
+ // the graph through `normalizeGraph` (@nodetool-ai/execution) — no
487
+ // need to duplicate that here.
488
+ const registry = new NodeRegistry();
489
+ registerBuiltinPacks(registry, CLI_LOCAL_BUILTIN_PACK_POLICY);
490
+ const jobId = `job-${Date.now()}`;
491
+ // Resolve asset URIs (e.g. /api/storage/<key>) against the local
492
+ // assets directory, so workflows referencing stored assets run the
493
+ // same decode path the server does.
494
+ // Apply the workflow's assigned workspace (if any) so file ops and
495
+ // workspace-driving nodes land in the same directory as a server run.
496
+ const { resolveWorkflowWorkspace } = await import("@nodetool-ai/websocket");
497
+ const assignedWorkspace = workflowId
498
+ ? await resolveWorkflowWorkspace(workflowId, "1")
499
+ : null;
500
+ const ASSET_OUTPUT_MODES = [
501
+ "native",
502
+ "raw",
503
+ "data_uri",
504
+ "workspace",
505
+ "storage_url",
506
+ "temp_url"
507
+ ];
508
+ const assetOutputMode = (opts.assetOutputMode ??
509
+ "native");
510
+ if (!ASSET_OUTPUT_MODES.includes(assetOutputMode)) {
511
+ throw new Error(`Unknown --asset-output-mode "${assetOutputMode}". ` +
512
+ `Expected one of: ${ASSET_OUTPUT_MODES.join(", ")}`);
513
+ }
514
+ // `--workspace <dir>` wins, then whatever the workflow is assigned.
515
+ // `workspace` asset mode writes each output into <workspace>/assets, so
516
+ // it needs somewhere to write even when the run has no saved workflow (a
517
+ // JSON or DSL file target). Fall back to ./nodetool-output so a bare
518
+ // `workflows run file.json --asset-output-mode workspace` produces files
519
+ // in the current directory rather than failing.
520
+ const overrideWorkspaceDir = opts.workspace ??
521
+ (assignedWorkspace
522
+ ? null
523
+ : assetOutputMode === "workspace"
524
+ ? resolve(process.cwd(), "nodetool-output")
525
+ : null);
526
+ if (overrideWorkspaceDir) {
527
+ mkdirSync(overrideWorkspaceDir, { recursive: true });
528
+ }
529
+ const runWorkspace = overrideWorkspaceDir
530
+ ? createLocalWorkspace(overrideWorkspaceDir)
531
+ : assignedWorkspace;
532
+ const assetStorage = new FileStorageAdapter(getDefaultAssetsPath());
533
+ const context = new ProcessingContext({
534
+ jobId,
535
+ workflowId,
536
+ userId: "1",
537
+ secretResolver: resolveLocalSecret,
538
+ storage: assetStorage,
539
+ assetOutputMode,
540
+ workspace: runWorkspace
541
+ });
542
+ const supervisor = supervisorConfig
543
+ ? await createSupervisorHandle({
544
+ config: supervisorConfig,
545
+ context
546
+ })
547
+ : null;
548
+ console.error(`Running workflow${workflowId ? ` ${workflowId}` : ""}` +
549
+ (supervisor
550
+ ? ` supervised by ${supervisor.providerId}/${supervisor.model}`
551
+ : "") +
552
+ "...");
553
+ // ExecutionSession owns graph hydration, Python-bridge connection
554
+ // (NODETOOL_WORKER_URL/NODETOOL_WORKER_TOKEN → remote worker; unset →
555
+ // local stdio worker; pure-TS graphs get none), and executor
556
+ // resolution (registry → Python bridge → throw).
557
+ const sessionOptions = {
558
+ graph,
559
+ registry,
560
+ // Registry alone hydrates node flags but not `propertyTypes`, and
561
+ // correlation analysis reads list-ness only from that map — so a
562
+ // saved workflow (which never carries it) had every list handle
563
+ // read as non-list, and legal fan-in into one was rejected before
564
+ // any node ran. The resolver fills it from node metadata.
565
+ resolveNodeType: createGraphNodeTypeResolver(registry).resolveNodeType,
566
+ jobId,
567
+ workflowId,
568
+ params,
569
+ context
570
+ };
571
+ if (triggerEvent) {
572
+ sessionOptions.triggerEvent = triggerEvent;
573
+ }
574
+ // Message capture is retention, so it stays off unless a supervised
575
+ // run needs the stream to print its `⛨` lines as they happen.
576
+ if (supervisor) {
577
+ sessionOptions.supervisor = supervisor.handle;
578
+ sessionOptions.captureMessages = true;
579
+ }
580
+ const session = await ExecutionSession.create(sessionOptions);
581
+ const interventionLines = supervisor
582
+ ? streamInterventionLines(session.messages)
583
+ : Promise.resolve();
584
+ const result = await session.result;
585
+ await interventionLines;
586
+ supervisor?.handle.close();
587
+ const interventions = result.interventions ?? [];
588
+ if (supervisor && interventions.length > 0) {
589
+ await recordSupervisorCost({
590
+ interventions,
591
+ jobId,
592
+ workflowId,
593
+ userId: "1",
594
+ providerId: supervisor.providerId,
595
+ model: supervisor.model
596
+ });
597
+ }
598
+ const { printRunJson, previewJson } = await import("./run-json.js");
599
+ if (opts.json) {
600
+ await printRunJson(result, {
601
+ outputDir: resolve(process.cwd(), "nodetool-output", jobId)
602
+ });
603
+ }
604
+ else {
605
+ console.log(`Status: ${result.status}`);
606
+ if (result.error)
607
+ console.error(`Error: ${result.error}`);
608
+ for (const [nodeId, outputs] of Object.entries(result.outputs ?? {})) {
609
+ if (Array.isArray(outputs) && outputs.length > 0) {
610
+ console.log(`\nNode ${nodeId}:`);
611
+ for (const out of outputs) {
612
+ console.log(` ${previewJson(out, 200)}`);
613
+ }
614
+ }
615
+ }
616
+ printSupervisedSummary(interventions);
617
+ }
618
+ process.exit(result.status === "completed" ? 0 : 1);
619
+ }
620
+ catch (e) {
621
+ console.error(describeRunFailure(e));
622
+ process.exit(1);
623
+ }
624
+ });
625
+ workflows
626
+ .command("export-dsl <workflow_id_or_file>")
627
+ .description("Export a workflow as TypeScript DSL code (local DB by id; --api-url for remote)")
628
+ .option("--api-url <url>", "Fetch the workflow from a remote server instead of the local database", process.env["NODETOOL_API_URL"])
629
+ .option("-o, --output <file>", "Write the exported DSL to a file")
630
+ .action(async (idOrFile, opts) => {
631
+ try {
632
+ let source;
633
+ if (idOrFile.endsWith(".json") ||
634
+ idOrFile.includes("/") ||
635
+ idOrFile.includes("\\")) {
636
+ const { readFileSync } = await import("node:fs");
637
+ const raw = JSON.parse(readFileSync(idOrFile, "utf8"));
638
+ const graph = (raw.graph ?? raw);
639
+ source = workflowToDsl(graph, {
640
+ workflowName: isString(raw.name) ? raw.name : null
641
+ });
642
+ }
643
+ else if (opts.apiUrl) {
644
+ source = await apiGetText(opts.apiUrl, `/api/workflows/${idOrFile}/dsl-export`);
645
+ }
646
+ else {
647
+ ensureDb();
648
+ const wf = await Workflow.find(LOCAL_USER_ID, idOrFile);
649
+ if (!wf)
650
+ throw new Error(`Workflow not found: ${idOrFile}`);
651
+ const graph = wf.getGraph();
652
+ source = workflowToDsl(graph, {
653
+ workflowName: isString(wf.name) ? wf.name : null
654
+ });
655
+ }
656
+ if (opts.output) {
657
+ const { writeFile } = await import("node:fs/promises");
658
+ await writeFile(opts.output, source, "utf8");
659
+ console.log(opts.output);
660
+ return;
661
+ }
662
+ console.log(source);
663
+ }
664
+ catch (e) {
665
+ printCommandError(e);
666
+ process.exit(1);
667
+ }
668
+ });
669
+ workflows
670
+ .command("export-example <workflow_id_or_file>")
671
+ .description("Export a workflow as a shipped template: materialize its assets into the " +
672
+ "package's constant asset dir (package:// refs) and write the example JSON")
673
+ .option("--api-url <url>", "Fetch the workflow and asset bytes from a remote server instead of the local DB/assets", process.env["NODETOOL_API_URL"])
674
+ .option("--package <name>", "Owning package", "nodetool-base")
675
+ .option("--assets-dir <dir>", "Root holding <package>/<file> constant assets (defaults to the monorepo base-nodes assets dir)")
676
+ .option("--examples-dir <dir>", "Directory the example JSON is written under, as <dir>/<package>/<name>.json")
677
+ .option("-o, --output <file>", "Write the example JSON to this exact path")
678
+ .option("--include-remote", "Also materialize http(s) and local-file refs (off by default)")
679
+ .action(async (idOrFile, opts) => {
680
+ try {
681
+ const { readFileSync } = await import("node:fs");
682
+ const { mkdir, writeFile } = await import("node:fs/promises");
683
+ const { join } = await import("node:path");
684
+ const { materializeWorkflowConstantAssets } = await import("@nodetool-ai/websocket");
685
+ let name = "";
686
+ let description = "";
687
+ let tags = [];
688
+ let graph;
689
+ const isFile = idOrFile.endsWith(".json") ||
690
+ idOrFile.includes("/") ||
691
+ idOrFile.includes("\\");
692
+ if (isFile) {
693
+ const raw = JSON.parse(readFileSync(idOrFile, "utf8"));
694
+ name = isString(raw.name) ? raw.name : "workflow";
695
+ description =
696
+ isString(raw.description) ? raw.description : "";
697
+ tags = Array.isArray(raw.tags)
698
+ ? raw.tags.filter((t) => typeof t === "string")
699
+ : [];
700
+ graph = (raw.graph ?? raw);
701
+ }
702
+ else if (opts.apiUrl) {
703
+ const client = createApiClient(opts.apiUrl);
704
+ const wf = (await client.workflows.get.query({
705
+ id: idOrFile
706
+ }));
707
+ name = isString(wf.name) ? wf.name : "workflow";
708
+ description =
709
+ isString(wf.description) ? wf.description : "";
710
+ tags = Array.isArray(wf.tags)
711
+ ? wf.tags.filter((t) => typeof t === "string")
712
+ : [];
713
+ graph = wf.graph;
714
+ }
715
+ else {
716
+ ensureDb();
717
+ const wf = await Workflow.find(LOCAL_USER_ID, idOrFile);
718
+ if (!wf)
719
+ throw new Error(`Workflow not found: ${idOrFile}`);
720
+ name = isString(wf.name) ? wf.name : "workflow";
721
+ description =
722
+ isString(wf.description) ? wf.description : "";
723
+ tags = Array.isArray(wf.tags)
724
+ ? wf.tags.filter((t) => typeof t === "string")
725
+ : [];
726
+ graph = wf.getGraph();
727
+ }
728
+ if (!graph?.nodes) {
729
+ throw new Error("Workflow has no graph to export");
730
+ }
731
+ const repoRoot = resolve(__dirname, "..", "..", "..");
732
+ const baseNodesDir = join(repoRoot, "packages", "base-nodes", "nodetool");
733
+ const assetsRoot = opts.assetsDir ?? join(baseNodesDir, "assets");
734
+ const examplesDir = opts.examplesDir ?? join(baseNodesDir, "examples");
735
+ const fetchAssetBytes = opts.apiUrl
736
+ ? makeAssetFetcher(opts.apiUrl.replace(/\/+$/, ""))
737
+ : await makeLocalAssetFetcher();
738
+ const materializeOptions = {
739
+ packageName: opts.package,
740
+ assetsRoot,
741
+ fetchAssetBytes
742
+ };
743
+ if (opts.includeRemote) {
744
+ materializeOptions.includeRemote = true;
745
+ }
746
+ const result = await materializeWorkflowConstantAssets(graph, materializeOptions);
747
+ const slug = name
748
+ .toLowerCase()
749
+ .replace(/[^a-z0-9]+/g, "-")
750
+ .replace(/^-+|-+$/g, "");
751
+ const example = {
752
+ id: slug || "workflow",
753
+ access: "public",
754
+ name,
755
+ description,
756
+ tags,
757
+ package_name: opts.package,
758
+ graph: result.graph
759
+ };
760
+ // Sanitize the on-disk filename separately from the display name so
761
+ // names with path separators can't escape the examples directory.
762
+ const safeFileName = name.replace(/[^A-Za-z0-9._-]+/g, "_").replace(/^[._]+/, "") ||
763
+ "workflow";
764
+ const outPath = opts.output ??
765
+ join(examplesDir, opts.package, `${safeFileName}.json`);
766
+ await mkdir(dirname(outPath), { recursive: true });
767
+ await writeFile(outPath, JSON.stringify(example, null, 2) + "\n", "utf8");
768
+ for (const a of result.exported) {
769
+ console.error(` asset ${a.source} → ${a.packageUri} (${a.byteLength} bytes)`);
770
+ }
771
+ for (const s of result.skipped) {
772
+ console.error(` warning: could not resolve asset ${s} (left as-is)`);
773
+ }
774
+ console.log(outPath);
775
+ }
776
+ catch (e) {
777
+ printCommandError(e);
778
+ process.exit(1);
779
+ }
780
+ });
781
+ workflows
782
+ .command("export-bundle <workflow_id_or_file...>")
783
+ .description("Export one or more workflows as a portable .nodetool bundle (zip): the " +
784
+ "graphs plus the bytes of every asset they reference, in a single file")
785
+ .option("--api-url <url>", "Fetch the workflow and asset bytes from a remote server instead of the local DB/assets", process.env["NODETOOL_API_URL"])
786
+ .option("-o, --output <file>", "Output path (default: <name>.nodetool)")
787
+ .option("--include-remote", "Also embed http(s) and local-file refs (off by default)")
788
+ .action(async (idsOrFiles, opts) => {
789
+ try {
790
+ const { readFileSync } = await import("node:fs");
791
+ const { writeFile } = await import("node:fs/promises");
792
+ const { packWorkflowsBundle } = await import("@nodetool-ai/websocket");
793
+ const toBundleWf = (raw) => ({
794
+ name: isString(raw.name) ? raw.name : "workflow",
795
+ description: isString(raw.description) ? raw.description : "",
796
+ tags: Array.isArray(raw.tags)
797
+ ? raw.tags.filter((t) => typeof t === "string")
798
+ : [],
799
+ run_mode: raw.run_mode ?? null,
800
+ settings: raw.settings ?? null,
801
+ graph: (raw.graph ?? raw)
802
+ });
803
+ const useServer = Boolean(opts.apiUrl);
804
+ const apiUrl = opts.apiUrl?.replace(/\/+$/, "") ?? "";
805
+ if (!useServer)
806
+ ensureDb();
807
+ const workflowsToPack = [];
808
+ for (const idOrFile of idsOrFiles) {
809
+ const isFile = idOrFile.endsWith(".json") ||
810
+ idOrFile.includes("/") ||
811
+ idOrFile.includes("\\");
812
+ let raw;
813
+ if (isFile) {
814
+ raw = JSON.parse(readFileSync(idOrFile, "utf8"));
815
+ }
816
+ else if (useServer) {
817
+ raw = (await createApiClient(apiUrl).workflows.get.query({
818
+ id: idOrFile
819
+ }));
820
+ }
821
+ else {
822
+ const found = await Workflow.find(LOCAL_USER_ID, idOrFile);
823
+ if (!found)
824
+ throw new Error(`Workflow not found: ${idOrFile}`);
825
+ raw = { ...found, graph: found.getGraph() };
826
+ }
827
+ const wf = toBundleWf(raw);
828
+ if (!wf.graph?.nodes) {
829
+ throw new Error(`Workflow '${idOrFile}' has no graph to export`);
830
+ }
831
+ workflowsToPack.push(wf);
832
+ }
833
+ const packOptions = {
834
+ workflows: workflowsToPack,
835
+ fetchAssetBytes: useServer
836
+ ? makeAssetFetcher(apiUrl)
837
+ : await makeLocalAssetFetcher(),
838
+ nodetoolVersion: cliVersion()
839
+ };
840
+ if (opts.includeRemote) {
841
+ packOptions.includeRemote = true;
842
+ }
843
+ const { bytes, manifest, skipped } = await packWorkflowsBundle(packOptions);
844
+ const defaultBase = workflowsToPack.length === 1
845
+ ? workflowsToPack[0].name.replace(/[^A-Za-z0-9._-]+/g, "_")
846
+ : `${workflowsToPack.length}-workflows`;
847
+ const outPath = opts.output ?? `${defaultBase}.nodetool`;
848
+ await writeFile(outPath, bytes);
849
+ for (const a of manifest.assets) {
850
+ console.error(` embedded ${a.file} (${a.bytes} bytes)`);
851
+ }
852
+ for (const s of skipped) {
853
+ console.error(` warning: could not resolve asset ${s} (left as-is)`);
854
+ }
855
+ console.error(` bundled ${manifest.workflows.length} workflow(s), ${manifest.assets.length} asset(s)`);
856
+ console.log(outPath);
857
+ }
858
+ catch (e) {
859
+ printCommandError(e);
860
+ process.exit(1);
861
+ }
862
+ });
863
+ workflows
864
+ .command("import-bundle <bundle_file>")
865
+ .description("Import a .nodetool bundle into the local library: store its assets and " +
866
+ "create the workflows with refs rewritten to the imported assets")
867
+ .option("--json", "Output the created workflows as JSON")
868
+ .action(async (bundleFile, opts) => {
869
+ try {
870
+ const { readFileSync } = await import("node:fs");
871
+ const { randomUUID } = await import("node:crypto");
872
+ const { extname } = await import("node:path");
873
+ const { importWorkflowBundle } = await import("@nodetool-ai/websocket");
874
+ await setupDb();
875
+ const storage = new FileStorageAdapter(getDefaultAssetsPath());
876
+ const zip = new Uint8Array(readFileSync(bundleFile));
877
+ const result = await importWorkflowBundle(zip, {
878
+ storeAsset: async ({ bytes, fileName, contentType }) => {
879
+ const id = randomUUID();
880
+ const key = `${id}${extname(fileName)}`;
881
+ await storage.store(key, bytes, contentType);
882
+ return { uri: `asset://${key}`, assetId: id };
883
+ }
884
+ });
885
+ const created = [];
886
+ for (const wf of result.workflows) {
887
+ created.push(await Workflow.create({
888
+ user_id: "1",
889
+ name: wf.name,
890
+ description: wf.description ?? "",
891
+ tags: wf.tags ?? [],
892
+ access: "private",
893
+ graph: wf.graph,
894
+ run_mode: wf.run_mode ?? "workflow",
895
+ settings: wf.settings ?? null
896
+ }));
897
+ }
898
+ for (const s of result.missing) {
899
+ console.error(` warning: bundle asset ${s} was missing from the archive`);
900
+ }
901
+ for (const m of result.checksumMismatches) {
902
+ console.error(` warning: ${m}`);
903
+ }
904
+ console.error(` imported ${created.length} workflow(s) and ${result.imported.length} asset(s) into local storage`);
905
+ if (opts.json) {
906
+ asJson(created);
907
+ }
908
+ else {
909
+ for (const c of created) {
910
+ console.log(String(c.id));
911
+ }
912
+ }
913
+ }
914
+ catch (e) {
915
+ printCommandError(e, opts.json);
916
+ process.exit(1);
917
+ }
918
+ });
919
+ // ---------------------------------------------------------------------------
920
+ // jobs / assets — the read commands are registered from commands/resource-read.ts
921
+ // ---------------------------------------------------------------------------
922
+ const jobs = program.command("jobs").description("Job management");
923
+ const assets = program.command("assets").description("Asset management");
924
+ registerResourceReadCommands({ workflows, jobs, assets }, { ensureDb, localUserId: LOCAL_USER_ID });
925
+ const models = program.command("models").description("Model management");
926
+ const modelKinds = [
927
+ "llm",
928
+ "image",
929
+ "tts",
930
+ "asr",
931
+ "video",
932
+ "embedding"
933
+ ];
934
+ function modelRow(m) {
935
+ return {
936
+ id: m["id"],
937
+ name: m["name"],
938
+ provider: m["provider"] ?? "",
939
+ type: m["type"] ?? "",
940
+ repo_id: m["repo_id"] ?? ""
941
+ };
942
+ }
943
+ models
944
+ .command("list")
945
+ .description("List all models (recommended + provider + HF cached); local by default, --api-url for remote")
946
+ .option("--api-url <url>", "Query a remote server instead of local providers/cache", process.env["NODETOOL_API_URL"])
947
+ .option("--json", "Output as JSON")
948
+ .action(async (opts) => {
949
+ try {
950
+ let rows;
951
+ if (opts.apiUrl) {
952
+ const client = createApiClient(opts.apiUrl);
953
+ rows = await client.models.all.query();
954
+ }
955
+ else {
956
+ await setupDb();
957
+ rows = await listAllModels();
958
+ }
959
+ if (opts.json) {
960
+ asJson(rows);
961
+ return;
962
+ }
963
+ printTable(rows.map(modelRow));
964
+ }
965
+ catch (e) {
966
+ printCommandError(e, opts.json);
967
+ process.exit(1);
968
+ }
969
+ });
970
+ models
971
+ .command("providers")
972
+ .description("List configured providers and their capabilities")
973
+ .option("--json", "Output as JSON")
974
+ .action(async (opts) => {
975
+ await setupDb();
976
+ try {
977
+ const data = await listConfiguredProviderInfo();
978
+ if (opts.json) {
979
+ asJson(data);
980
+ return;
981
+ }
982
+ printTable(data.map((p) => ({
983
+ provider: p.provider,
984
+ capabilities: p.capabilities.join(", ")
985
+ })));
986
+ }
987
+ catch (e) {
988
+ printCommandError(e, opts.json);
989
+ process.exit(1);
990
+ }
991
+ });
992
+ registerRecommendedCommand(models);
993
+ registerHfCommands(models);
994
+ models
995
+ .command("ollama")
996
+ .description("List Ollama models (queries the local daemon; --api-url for remote)")
997
+ .option("--api-url <url>", "Query a remote server instead of the local Ollama daemon", process.env["NODETOOL_API_URL"])
998
+ .option("--json", "Output as JSON")
999
+ .action(async (opts) => {
1000
+ try {
1001
+ let rows;
1002
+ if (opts.apiUrl) {
1003
+ const client = createApiClient(opts.apiUrl);
1004
+ rows = await client.models.ollama.query();
1005
+ }
1006
+ else {
1007
+ await setupDb();
1008
+ rows = await listOllamaModels();
1009
+ }
1010
+ if (opts.json) {
1011
+ asJson(rows);
1012
+ return;
1013
+ }
1014
+ printTable(rows.map(modelRow));
1015
+ }
1016
+ catch (e) {
1017
+ printCommandError(e, opts.json);
1018
+ process.exit(1);
1019
+ }
1020
+ });
1021
+ models
1022
+ .command("huggingface")
1023
+ .description("List HuggingFace cached models (scans the local cache; --api-url for remote)")
1024
+ .option("--api-url <url>", "Query a remote server instead of the local HuggingFace cache", process.env["NODETOOL_API_URL"])
1025
+ .option("--query <q>", "Search query")
1026
+ .option("--type <t>", "Filter by HF model type")
1027
+ .option("--json", "Output as JSON")
1028
+ .action(async (opts) => {
1029
+ try {
1030
+ let rows;
1031
+ if (opts.apiUrl) {
1032
+ const client = createApiClient(opts.apiUrl);
1033
+ if (opts.query || opts.type) {
1034
+ const search = {};
1035
+ if (opts.query) {
1036
+ search.query = String(opts.query);
1037
+ }
1038
+ if (opts.type) {
1039
+ search.type = String(opts.type);
1040
+ }
1041
+ rows = await client.models.huggingfaceSearch.query(search);
1042
+ }
1043
+ else {
1044
+ rows = await client.models.huggingfaceList.query();
1045
+ }
1046
+ }
1047
+ else if (opts.query || opts.type) {
1048
+ // Wrap a bare query in wildcards so it matches as a substring, the same
1049
+ // normalization the server's huggingfaceSearch applies.
1050
+ const rawQuery = opts.query ? String(opts.query) : undefined;
1051
+ const query = rawQuery && !rawQuery.includes("*") ? `*${rawQuery}*` : rawQuery;
1052
+ rows = await searchCachedHfModels(query ? [query] : undefined, opts.type ? [String(opts.type)] : undefined);
1053
+ }
1054
+ else {
1055
+ rows = await readCachedHfModels();
1056
+ }
1057
+ if (opts.json) {
1058
+ asJson(rows);
1059
+ return;
1060
+ }
1061
+ printTable(rows.map(modelRow));
1062
+ }
1063
+ catch (e) {
1064
+ printCommandError(e, opts.json);
1065
+ process.exit(1);
1066
+ }
1067
+ });
1068
+ models
1069
+ .command("by-provider <provider>")
1070
+ .description(`List models for a provider. --kind one of: ${modelKinds.join(", ")}`)
1071
+ .option("--kind <kind>", "Model kind", "llm")
1072
+ .option("--json", "Output as JSON")
1073
+ .action(async (provider, opts) => {
1074
+ const kind = opts.kind;
1075
+ if (!modelKinds.includes(kind)) {
1076
+ console.error(`Invalid --kind '${opts.kind}'. Must be one of: ${modelKinds.join(", ")}`);
1077
+ process.exit(1);
1078
+ }
1079
+ await setupDb();
1080
+ try {
1081
+ const rows = await listProviderModels(provider, kind);
1082
+ if (opts.json) {
1083
+ asJson(rows);
1084
+ return;
1085
+ }
1086
+ printTable(rows.map(modelRow));
1087
+ }
1088
+ catch (e) {
1089
+ printCommandError(e, opts.json);
1090
+ process.exit(1);
1091
+ }
1092
+ });
1093
+ // ---------------------------------------------------------------------------
1094
+ // storage
1095
+ // ---------------------------------------------------------------------------
1096
+ const storage = program
1097
+ .command("storage")
1098
+ .description("Maintain the asset storage backend");
1099
+ storage
1100
+ .command("migrate-keys")
1101
+ .description("Move asset objects under their owner's prefix (<userId>/<assetId>.<ext>)")
1102
+ .option("--dry-run", "Report what would move without writing anything")
1103
+ .option("--user-id <id>", "Migrate only this user's objects")
1104
+ .option("--json", "Output the report as JSON")
1105
+ .action(async (opts) => {
1106
+ await setupDb();
1107
+ const { migrateStorageKeys, formatMigrateKeysReport } = await import("./commands/storage.js");
1108
+ try {
1109
+ const migrateOptions = {
1110
+ dryRun: Boolean(opts.dryRun)
1111
+ };
1112
+ if (opts.userId) {
1113
+ migrateOptions.userId = opts.userId;
1114
+ }
1115
+ const report = await migrateStorageKeys(migrateOptions);
1116
+ if (opts.json) {
1117
+ console.log(JSON.stringify(report, null, 2));
1118
+ }
1119
+ else {
1120
+ console.log(formatMigrateKeysReport(report, Boolean(opts.dryRun)));
1121
+ }
1122
+ if (report.failed > 0)
1123
+ process.exit(1);
1124
+ }
1125
+ catch (e) {
1126
+ printCommandError(e, opts.json);
1127
+ process.exit(1);
1128
+ }
1129
+ });
1130
+ workflows
1131
+ .command("migrate-code-inputs")
1132
+ .description("Rewrite saved Code node bodies to read declared inputs off `inputs`")
1133
+ .option("--dry-run", "Report what would change without writing anything")
1134
+ .option("--user-id <id>", "Migrate only this user's workflows")
1135
+ .option("--json", "Output the report as JSON")
1136
+ .action(async (opts) => {
1137
+ await setupDb();
1138
+ const { migrateCodeInputs, formatMigrateCodeInputsReport } = await import("./commands/migrate-code-inputs.js");
1139
+ try {
1140
+ const migrateOptions = {
1141
+ dryRun: Boolean(opts.dryRun)
1142
+ };
1143
+ if (opts.userId) {
1144
+ migrateOptions.userId = opts.userId;
1145
+ }
1146
+ const report = await migrateCodeInputs(migrateOptions);
1147
+ if (opts.json) {
1148
+ console.log(JSON.stringify(report, null, 2));
1149
+ }
1150
+ else {
1151
+ console.log(formatMigrateCodeInputsReport(report, Boolean(opts.dryRun)));
1152
+ }
1153
+ if (report.failed > 0)
1154
+ process.exit(1);
1155
+ }
1156
+ catch (e) {
1157
+ printCommandError(e, opts.json);
1158
+ process.exit(1);
1159
+ }
1160
+ });
1161
+ // ---------------------------------------------------------------------------
1162
+ // secrets
1163
+ // ---------------------------------------------------------------------------
1164
+ const secrets = program
1165
+ .command("secrets")
1166
+ .description("Manage encrypted secrets");
1167
+ secrets
1168
+ .command("list")
1169
+ .description("List stored secret keys (values are never shown)")
1170
+ .option("--user-id <id>", "User ID", "1")
1171
+ .action(async (opts) => {
1172
+ await setupDb();
1173
+ try {
1174
+ const [items] = await Secret.listForUser(opts.userId, 100);
1175
+ if (items.length === 0) {
1176
+ console.log("No secrets stored.");
1177
+ return;
1178
+ }
1179
+ printTable(items.map((s) => ({ key: s.key, updated_at: s.updated_at ?? "" })));
1180
+ }
1181
+ catch (e) {
1182
+ printCommandError(e);
1183
+ process.exit(1);
1184
+ }
1185
+ });
1186
+ secrets
1187
+ .command("store <key>")
1188
+ .description("Store or update a secret (prompts for value)")
1189
+ .option("--user-id <id>", "User ID", "1")
1190
+ .option("--description <desc>", "Optional description")
1191
+ .action(async (key, opts) => {
1192
+ await setupDb();
1193
+ const rl = createInterface({
1194
+ input: process.stdin,
1195
+ output: process.stderr
1196
+ });
1197
+ const value = await new Promise((resolve) => {
1198
+ process.stderr.write(`Enter value for '${key}': `);
1199
+ rl.question("", (ans) => {
1200
+ rl.close();
1201
+ resolve(ans);
1202
+ });
1203
+ });
1204
+ try {
1205
+ await Secret.upsert({
1206
+ userId: opts.userId,
1207
+ key,
1208
+ value,
1209
+ description: opts.description
1210
+ });
1211
+ console.log(`Secret '${key}' stored.`);
1212
+ }
1213
+ catch (e) {
1214
+ printCommandError(e);
1215
+ process.exit(1);
1216
+ }
1217
+ });
1218
+ secrets
1219
+ .command("get <key>")
1220
+ .description("Retrieve and print a secret value")
1221
+ .option("--user-id <id>", "User ID", "1")
1222
+ .action(async (key, opts) => {
1223
+ await setupDb();
1224
+ try {
1225
+ const value = await getSecret(key, opts.userId);
1226
+ if (value == null) {
1227
+ console.error(`Secret '${key}' not found.`);
1228
+ process.exit(1);
1229
+ }
1230
+ console.log(value);
1231
+ }
1232
+ catch (e) {
1233
+ printCommandError(e);
1234
+ process.exit(1);
1235
+ }
1236
+ });
1237
+ // ---------------------------------------------------------------------------
1238
+ // settings
1239
+ // ---------------------------------------------------------------------------
1240
+ const settings = program
1241
+ .command("settings")
1242
+ .description("View configuration settings");
1243
+ settings
1244
+ .command("show")
1245
+ .description("Show current settings from environment")
1246
+ .option("--json", "Output as JSON")
1247
+ .action((opts) => {
1248
+ const vars = [
1249
+ "ENV",
1250
+ "LOG_LEVEL",
1251
+ "PORT",
1252
+ "HOST",
1253
+ "DB_PATH",
1254
+ "NODETOOL_API_URL",
1255
+ "ANTHROPIC_API_KEY",
1256
+ "OPENAI_API_KEY",
1257
+ "GEMINI_API_KEY",
1258
+ "MISTRAL_API_KEY",
1259
+ "GROQ_API_KEY",
1260
+ "OLLAMA_API_URL",
1261
+ "SERPAPI_API_KEY",
1262
+ "HF_TOKEN",
1263
+ "VECTORSTORE_DB_PATH",
1264
+ "ASSET_BUCKET",
1265
+ "S3_ENDPOINT_URL"
1266
+ ];
1267
+ const data = Object.fromEntries(vars.map((k) => [
1268
+ k,
1269
+ process.env[k]
1270
+ ? k.endsWith("KEY") || k.endsWith("TOKEN")
1271
+ ? "***"
1272
+ : process.env[k]
1273
+ : ""
1274
+ ]));
1275
+ if (opts.json) {
1276
+ asJson(data);
1277
+ return;
1278
+ }
1279
+ printTable(Object.entries(data).map(([variable, value]) => ({ variable, value })));
1280
+ });
1281
+ // ---------------------------------------------------------------------------
1282
+ // mcp
1283
+ // ---------------------------------------------------------------------------
1284
+ const mcp = program
1285
+ .command("mcp")
1286
+ .description("Use NodeTool as an MCP server from Claude Code, Codex, OpenCode, or any MCP client");
1287
+ registerMcpClientCommands(mcp);
1288
+ mcp
1289
+ .command("serve")
1290
+ .description("Run the NodeTool MCP server over stdio for an external agent " +
1291
+ "(Claude Code, Codex, …). All logging goes to stderr; stdout is the " +
1292
+ "MCP protocol channel.")
1293
+ .action(async () => {
1294
+ try {
1295
+ const { createMcpServer, createMcpStdioTransport, forwardMcpStdioToLocalServer } = await import("@nodetool-ai/websocket");
1296
+ if (await forwardMcpStdioToLocalServer()) {
1297
+ process.stderr.write("NodeTool MCP forwarding to the local API server.\n");
1298
+ return;
1299
+ }
1300
+ await setupDb();
1301
+ // A local TS-node registry powers list_nodes / search_nodes /
1302
+ // validate_workflow without spinning up a Python worker.
1303
+ const registry = new NodeRegistry();
1304
+ registerBuiltinPacks(registry, CLI_LOCAL_BUILTIN_PACK_POLICY);
1305
+ // stdio serves exactly one local user; "1" is the local single-user id.
1306
+ const server = createMcpServer({
1307
+ registry,
1308
+ agentToolsScope: { userId: "1", source: "stdio-local" }
1309
+ });
1310
+ const transport = createMcpStdioTransport();
1311
+ await server.connect(transport);
1312
+ process.stderr.write("NodeTool MCP server ready on stdio.\n");
1313
+ // The stdio transport keeps the event loop alive until the client
1314
+ // closes the connection.
1315
+ }
1316
+ catch (e) {
1317
+ printCommandError(e);
1318
+ process.exit(1);
1319
+ }
1320
+ });
1321
+ // ---------------------------------------------------------------------------
1322
+ // package
1323
+ // ---------------------------------------------------------------------------
1324
+ registerPackageCommands(program);
1325
+ registerPackCommands(program);
1326
+ registerWorkerCommands(program);
1327
+ // Any command that opens a remote Python bridge keeps the attached worker's
1328
+ // idle clock fresh, so the reaper doesn't pause a GPU that is mid-job.
1329
+ installWorkerActivityHeartbeat(onPythonBridgeCreated);
1330
+ // ---------------------------------------------------------------------------
1331
+ // deploy — registered from commands/deploy.ts
1332
+ // ---------------------------------------------------------------------------
1333
+ registerDeployCommands(program);
1334
+ registerAgentCommands(program);
1335
+ registerDbCommands(program);
1336
+ registerDebugCommands(program);
1337
+ registerAppCommands(program);
1338
+ registerAppsCommands(program);
1339
+ registerValidateCommand(program);
1340
+ registerTimelineCommands(program);
1341
+ registerSketchCommands(program);
1342
+ registerGameCommands(program);
1343
+ registerJsScriptCommands(program);
1344
+ registerReliabilityCommands(program);
1345
+ registerNodeCommands(program);
1346
+ registerGenerateCommand(program);
1347
+ registerExtensionCommands(program);
1348
+ registerEvalCommand(program);
1349
+ registerJtbdCommand(program, setupDb);
1350
+ registerAffectedCommand(program);
1351
+ registerHarnessCommands(program);
1352
+ registerCollectionCommands(program);
1353
+ registerCostsCommands(program);
1354
+ registerErrorsCommands(program);
1355
+ registerGenerationsCommands(program);
1356
+ registerRunsCommands(program, {
1357
+ ensureDb: async () => {
1358
+ const dbPath = getDefaultDbPath();
1359
+ await migrateSqliteDb(dbPath);
1360
+ initDb(dbPath);
1361
+ },
1362
+ localUserId: LOCAL_USER_ID
1363
+ });
1364
+ registerAuthCommands(program);
1365
+ registerTelegramCommands(program);
1366
+ // ---------------------------------------------------------------------------
1367
+ program.parse();
1368
+ //# sourceMappingURL=nodetool-main.js.map