nanoodle 0.8.0 → 0.11.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 CHANGED
@@ -170,6 +170,56 @@ Failures in lanes no output depends on only appear in `result.errors`.
170
170
  Unknown node types, missing required inputs, bad keys, and a missing API key
171
171
  all fail **before** anything is spent.
172
172
 
173
+ ### Gates (Decide said no)
174
+
175
+ A ⚖️ `decide` node in yes/no mode with **gate** on stops its branch when the
176
+ answer is no. That is a result, not an error, so `run()` resolves:
177
+
178
+ ```js
179
+ result.gated
180
+ // [{ nodeId: "n4", name: "Decide", yes: 0.12, skipped: ["n5", "n6"],
181
+ // message: "gate closed — the answer was no (yes 12%), so nothing downstream ran" }]
182
+ result.nodes.n4 // { status: "gated", out: { text: "no", decision }, gate: { yes, message }, costUsd, ... }
183
+ result.nodes.n5 // { status: "skipped", gatedBy: "n4", costUsd: null, ... } — never ran, never billed
184
+ result.get("Image") // throws NanoodleError code "gated": the gate "Decide" answered no
185
+ ```
186
+
187
+ `result.errors` stays empty, progress emits `node-gated` / `node-skipped`, and
188
+ the CLI exits 0 with `gated` in its JSON summary. A node fed by both a closed
189
+ gate and a real failure is still an error.
190
+
191
+ ### Per-node timeout, skip on failure, locked nodes
192
+
193
+ Three optional keys inside a node's `fields` (saved with the graph, no format change):
194
+
195
+ - **`timeoutMs`**: a deadline for this node alone. The node gets its own abort
196
+ signal, linked to the run's, and fails with a `NanoodleError` of code
197
+ `"timeout"` (`result.errors[i].code === "timeout"`). The run-level
198
+ `run(inputs, { timeoutMs })` still applies on top.
199
+ - **`onError: "skip"`**: if the node fails or times out, it gets
200
+ `status: "skipped"` (with `error`), the failure is still recorded in
201
+ `result.errors`, and progress emits `node-skipped` with `reason: "error"`.
202
+ Its ports come out `undefined`, so dependents just ignore it: a `join` keeps
203
+ the surviving inputs, and a decider reads only the writers that finished. A
204
+ node whose **every** input was skipped fails with "all inputs were skipped
205
+ after failing", and the run still fails if a non-skipped output fails or if
206
+ every output was skipped. A run-level abort is never skipped.
207
+ A network node that started but was skipped, timed out or aborted without
208
+ reporting a price makes `result.costExact` false. Once a node is finalized
209
+ (and once the run settles), late cost, progress or output callbacks from a
210
+ runner that ignored its abort signal are ignored, so `result.nodes` and the
211
+ cost totals don't change afterwards.
212
+ - **`locked: true`**: none of the node's fields become inputs (`wf.inputs`,
213
+ CLI `--input`, MCP tool args). An input key aimed at a locked field is
214
+ rejected, and so is any `run(…, { settings })` override that targets the
215
+ locked node (`NanoodleError` code `"locked"`). Inputs and settings can't
216
+ change a locked node's stored fields. Wires into its ports still deliver
217
+ data as usual, and editing the graph file itself still changes it.
218
+
219
+ ```js
220
+ { id: "w1", type: "llm", fields: { model: "…", system: "…", timeoutMs: 60000, onError: "skip", locked: true } }
221
+ ```
222
+
173
223
  ### Prompt length caps
174
224
 
175
225
  Many image and video models reject an over-long prompt at NanoGPT's route
@@ -204,9 +254,16 @@ The pieces are exported if you orchestrate graphs yourself:
204
254
 
205
255
  | runs | node types |
206
256
  |---|---|
207
- | local | text, upload (image/audio/video), choice, join, comment |
257
+ | local | text, upload (image/audio/video), mupload (3D .glb), choice, join, comment |
208
258
  | local media† | resize, vframes, combine, soundtrack, trim, extractaudio |
209
- | NanoGPT | llm (incl. vision + audio input), image, edit, inpaint*, vision, tvideo, ivideo, vedit, lipsync, music, remix, tts, transcribe |
259
+ | NanoGPT | llm (incl. vision + audio input), image, edit, inpaint*, vision, decide†, tvideo, ivideo, model3d‡, vedit, lipsync, music, remix, tts, transcribe, cleanvoice§ |
260
+ | custom URL | endpoint (POST to a URL in the graph; no NanoGPT key) |
261
+
262
+ † `decide` asks a NanoGPT decision model one typed question (`POST /api/v1/decisions`): pick the best of the wired `img1…` images, choose a label, score on a scale, or yes/no. Text-only decisions need nothing extra; wired images are shrunk to the model's limits with ffmpeg. A yes/no gate that answers no is not a failure: that node settles as `gated`, everything downstream is `skipped` unbilled, and the run succeeds (see [Gates](#gates-decide-said-no)).
263
+
264
+ ‡ `model3d` turns an image and/or a prompt into a GLB (`POST /api/generate-video`, then `GET /api/video/status`). The output port is `model`. A blank model id uses `tripo3d/v2.5` (image only). Polling stops at 25 minutes.
265
+
266
+ § `cleanvoice` strips noise and music from hosted audio or video (`POST /api/v1/audio/speech` with `elevenlabs/audio-isolation` or `veed/clean-audio`, then `GET /api/tts/status`). The source must be a public `http(s)` URL — `data:` and `blob:` are refused before any request. Priced from the source length (default model is $0.121/min).
210
267
 
211
268
  † **local media** prefers a pure-JS path that matches the browser (lossless mp4 remux, PCM-WAV trim, PNG resize). **ffmpeg** on `PATH` is the fallback for everything else (soft dependency — not an npm package); clear error if it’s required and missing.
212
269
 
package/bin/nanoodle.mjs CHANGED
@@ -242,6 +242,8 @@ then: NANOGPT_API_KEY=... nanoodle run ${dest} --input Text="your idea"`);
242
242
  if (e.type === "node-start") console.error(`▶ ${e.name} (${e.nodeId})`);
243
243
  if (e.type === "node-done") console.error(`✔ ${e.name} (${e.nodeId}) ${e.ms}ms${e.costUsd != null ? ` $${e.costUsd}` : ""}`);
244
244
  if (e.type === "node-error") console.error(`✖ ${e.name} (${e.nodeId}): ${e.error}`);
245
+ if (e.type === "node-gated") console.error(`⛔ ${e.name} (${e.nodeId}) gated: ${e.message}${e.costUsd != null ? ` $${e.costUsd}` : ""}`);
246
+ if (e.type === "node-skipped") console.error(`⤼ ${e.name} (${e.nodeId}) skipped — gate ${e.gatedBy} said no`);
245
247
  },
246
248
  });
247
249
  } catch (e) {
@@ -284,7 +286,11 @@ then: NANOGPT_API_KEY=... nanoodle run ${dest} --input Text="your idea"`);
284
286
  costExact: result.costExact,
285
287
  remainingBalance: result.remainingBalance,
286
288
  errors: result.errors,
287
- nodes: Object.fromEntries(Object.entries(result.nodes).map(([id, r]) => [id, { status: r.status, ms: r.ms, costUsd: r.costUsd, error: r.error }])),
289
+ gated: result.gated || [],
290
+ nodes: Object.fromEntries(Object.entries(result.nodes).map(([id, r]) => [id, {
291
+ status: r.status, ms: r.ms, costUsd: r.costUsd, error: r.error,
292
+ ...(r.gate ? { gate: r.gate } : {}), ...(r.gatedBy ? { gatedBy: r.gatedBy } : {}),
293
+ }])),
288
294
  }, null, 2));
289
295
  if (!quiet) {
290
296
  const approx = result.costExact ? "" : "≥ ";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoodle",
3
- "version": "0.8.0",
3
+ "version": "0.11.0",
4
4
  "description": "Run nanoodle visual AI workflows from JavaScript — zero-dependency executor for saved noodle-graph.json files",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
package/src/browser.mjs CHANGED
@@ -29,7 +29,7 @@ export {
29
29
  } from "./media.mjs";
30
30
  export { NanoClient, httpError, costFromJson, costFromHeaders, costWithHeaders, sleep } from "./client.mjs";
31
31
  export { NODE_TYPES, displayName, materialize, topoSort, wiredFramesFloor, MAX_FRAMES } from "./graph.mjs";
32
- export { RUNNERS } from "./nodes.mjs"; // per-node executors — the play delegation shim drives these directly
32
+ export { RUNNERS, IMG_INPUT_ROLES } from "./nodes.mjs"; // per-node executors — the play delegation shim drives these directly
33
33
  export { catItem, chatModelCan } from "./catalog.mjs";
34
34
  export { deriveInputs, deriveOutputs, deriveSettings, INPUT_SPECS, SETTING_SPECS } from "./io.mjs";
35
35
  export { decodeShareUrl, decodeShareFragment, isShareRef } from "./share.mjs";
package/src/catalog.mjs CHANGED
@@ -15,6 +15,25 @@ export function catItem(catalog, kind, id) {
15
15
  return (Array.isArray(raw) && raw.find((m) => m && m.id === id)) || null;
16
16
  }
17
17
 
18
+ /**
19
+ * True when a video model's PRICING block prices reference images even though its
20
+ * supported_parameters never names the param (minimax-h3 ships that way). A billing
21
+ * line for refs is the catalog stating the model takes them, so the ref gates accept
22
+ * it as evidence; models with neither the param nor the pricing stay "no refs",
23
+ * because an ignored-but-sent ref array is still charged.
24
+ *
25
+ * Also true for a billed `reference_to_video` MODE under per_second_by_mode or
26
+ * per_second_by_mode_and_resolution (Gemini Omni Flash v1 / v1.1) — same class of
27
+ * evidence as extra_reference_image. Exact key only; kling-o1's
28
+ * reference_to_video_image / _video stay on the mode gate (different shape).
29
+ */
30
+ export function pricingAdvertisesRefs(pricing) {
31
+ if (!pricing) return false;
32
+ if (pricing.included_reference_images != null || pricing.extra_reference_image != null) return true;
33
+ const mm = pricing.per_second_by_mode || pricing.per_second_by_mode_and_resolution;
34
+ return !!(mm && mm.reference_to_video != null);
35
+ }
36
+
18
37
  /** Permissive capability probe: true unless the model is in the catalog AND lacks the flag. */
19
38
  export function chatModelCan(catalog, model, flag) {
20
39
  const m = catItem(catalog, "chat", model);
@@ -0,0 +1,104 @@
1
+ /**
2
+ * 🎧 Clean voice — strip background noise / music, keep the voice.
3
+ * Twin of the editor/play block (nanoodle #709). NanoGPT's noise_reduction
4
+ * models (ElevenLabs Audio Isolation, VEED Clean Audio) ride
5
+ * POST /api/v1/audio/speech with { model, audio:<public URL>, duration }
6
+ * and answer 202 + runId (charged up front) → GET /api/tts/status → hosted audio.
7
+ * The source MUST be a public http(s) URL: data: and blob: are refused before
8
+ * any request. duration is only a quote; the server re-measures and bills that.
9
+ */
10
+ import { NanoodleError } from "./errors.mjs";
11
+
12
+ export const CLEANVOICE_DEFAULT_MODEL = "elevenlabs/audio-isolation"; // $0.121/min
13
+ export const CLEANVOICE_FALLBACK_SECS = 60; // length unknown → quote a minute
14
+ const CLEANVOICE_VIDEO_EXT = /\.(mp4|m4v|mov|webm|mkv|avi)(\?|#|$)/i;
15
+
16
+ /** Which file to clean: a wired audio clip, else a wired video, else the pasted link. */
17
+ export function cleanVoiceSource(inp, f) {
18
+ const a = inp && typeof inp.audio === "string" ? inp.audio.trim() : "";
19
+ const v = inp && typeof inp.video === "string" ? inp.video.trim() : "";
20
+ const link = String((f && f.url) || "").trim();
21
+ const url = a || v || link;
22
+ const video = a ? false : (v ? true : CLEANVOICE_VIDEO_EXT.test(link));
23
+ if (!url) throw new NanoodleError("no audio — wire audio or a video in, or paste a public link to the recording");
24
+ if (!/^https?:\/\//i.test(url)) {
25
+ throw new NanoodleError(
26
+ "Clean voice needs a hosted file — NanoGPT downloads it from a public link, and a clip uploaded or made in your browser has none. Wire a generated video or track, or paste a public https link to the recording.");
27
+ }
28
+ return { url, video };
29
+ }
30
+
31
+ /** Duration quote NanoGPT requires (0.6–3600 s); unknown → CLEANVOICE_FALLBACK_SECS. */
32
+ export function cleanVoiceSeconds(secs) {
33
+ const s = +secs;
34
+ if (!isFinite(s) || s <= 0) return CLEANVOICE_FALLBACK_SECS;
35
+ return Math.min(3600, Math.max(0.6, Math.round(s * 100) / 100));
36
+ }
37
+
38
+ export function cleanVoiceExtra(url, secs) {
39
+ return { audio: url, duration: cleanVoiceSeconds(secs) };
40
+ }
41
+
42
+ /** Seconds the cost forecast assumes: the wired source node's duration knob, else 30 s. */
43
+ export function cleanVoiceEstSeconds(srcFields) {
44
+ const d = parseFloat(srcFields && srcFields.duration);
45
+ return (isFinite(d) && d > 0) ? d : 30;
46
+ }
47
+
48
+ /** NanoGPT's 400s, reworded into the fix. Unrelated errors (and aborts) pass through. */
49
+ export function cleanVoiceError(e) {
50
+ if (e && (e.name === "AbortError" || e.code === "aborted")) return e;
51
+ const m = String((e && e.message) || e || "");
52
+ if (/verify the source (audio )?duration/i.test(m)) {
53
+ return new NanoodleError("NanoGPT couldn't read this file's length — use MP3, WAV, M4A/AAC or MP4 (OGG isn't accepted).");
54
+ }
55
+ if (/unable to download/i.test(m)) {
56
+ return new NanoodleError("NanoGPT couldn't download the file — the link must be public (no sign-in, not expired).");
57
+ }
58
+ if (/public http\(?s?\)? source/i.test(m)) {
59
+ return new NanoodleError("Clean voice needs a public https link — clips uploaded in the browser can't reach it.");
60
+ }
61
+ return e;
62
+ }
63
+
64
+ /**
65
+ * The source's length from media metadata. Browsers can read it without CORS;
66
+ * Node has no media element, so this resolves null and the quote falls back to
67
+ * CLEANVOICE_FALLBACK_SECS (the server re-measures before billing).
68
+ */
69
+ export function cleanVoiceMediaSeconds(url, isVideo) {
70
+ return new Promise((res) => {
71
+ const doc = globalThis.document;
72
+ if (!doc || typeof doc.createElement !== "function") { res(null); return; }
73
+ let el = null;
74
+ let done = false;
75
+ const fin = (v) => {
76
+ if (done) return;
77
+ done = true;
78
+ try { el.removeAttribute("src"); el.load(); } catch { /* element may already be gone */ }
79
+ res(v);
80
+ };
81
+ try { el = doc.createElement(isVideo ? "video" : "audio"); }
82
+ catch { res(null); return; }
83
+ el.preload = "metadata";
84
+ el.muted = true;
85
+ el.onloadedmetadata = () => {
86
+ const d = el.duration;
87
+ fin(isFinite(d) && d > 0 ? d : null);
88
+ };
89
+ el.onerror = () => fin(null);
90
+ setTimeout(() => fin(null), 10000);
91
+ el.src = url;
92
+ });
93
+ }
94
+
95
+ /** send(model, extra) → audio URL (the runtime's audio submit + poll). */
96
+ export async function cleanVoiceRun(model, inp, f, send) {
97
+ const src = cleanVoiceSource(inp, f);
98
+ const secs = await cleanVoiceMediaSeconds(src.url, src.video);
99
+ try {
100
+ return { audio: await send(model || CLEANVOICE_DEFAULT_MODEL, cleanVoiceExtra(src.url, secs)) };
101
+ } catch (e) {
102
+ throw cleanVoiceError(e);
103
+ }
104
+ }
package/src/client.mjs CHANGED
@@ -3,6 +3,10 @@ import { b64ImageMime, bytesToDataUrl, dataUrlBytes, MEDIA_INLINE_MAX } from "./
3
3
  import { assertPaymentOption, parseNanoInvoice, looksLikeResult } from "./x402.mjs";
4
4
 
5
5
  const AUDIO_MIME = { mp3: "audio/mpeg", opus: "audio/ogg", aac: "audio/aac", flac: "audio/flac", wav: "audio/wav", pcm: "audio/wav" };
6
+ // Native music endpoints use prompt for musical direction; lyrics remain separate.
7
+ const AUDIO_PROMPT_MODEL_RE = /(?:^|\/)(?:prompt-to-song|generate-bgm)$|(?:^|\/)mureka-ai\/[^/]+\/generate-song$|^minimax\/music-3$/i;
8
+ // Yue2 catalogs take `style` + `lyrics` (+ `audio` for music-to-music), not OpenAI-style `input`.
9
+ const AUDIO_STYLE_MODEL_RE = /yue2-3b\/(?:text|music)-to-music$/i;
6
10
 
7
11
  /** Map an HTTP failure to an actionable error (mirrors the app's httpRunError). Never leaks the key. */
8
12
  export function httpError(status, bodyText) {
@@ -75,6 +79,22 @@ export function sleep(ms, signal) {
75
79
  * NanoGPT transport. `baseUrl` and `fetch` are injectable (that's how the offline test harness runs);
76
80
  * pollIntervals / timeouts are per-media-kind knobs (ms).
77
81
  */
82
+ /**
83
+ * GLB URL from a 3D status poll. A completed payload is 3D only when
84
+ * output.kind is "3d" or output.format is "glb" — a bare video url is not a model.
85
+ * Prefers model_url, then model.url, video.url, then videoUrls[0].
86
+ */
87
+ export function model3dStatusUrl(s) {
88
+ const out = (s && s.data && s.data.output) || (s && s.output) || {};
89
+ const kind = String(out.kind || "").toLowerCase();
90
+ const format = String(out.format || "").toLowerCase();
91
+ if (kind !== "3d" && format !== "glb") return "";
92
+ const list = Array.isArray(out.videoUrls) ? out.videoUrls : [];
93
+ const first = list.length ? list[0] : null;
94
+ const fromList = typeof first === "string" ? first : (first && first.url) || "";
95
+ return out.model_url || (out.model && out.model.url) || (out.video && out.video.url) || fromList || "";
96
+ }
97
+
78
98
  export class NanoClient {
79
99
  constructor({ apiKey, baseUrl = "https://nano-gpt.com", fetch = globalThis.fetch, pollIntervals = {}, timeouts = {}, payment } = {}) {
80
100
  // non-enumerable: console.log/util.inspect/JSON.stringify of a client (or a Workflow holding
@@ -85,7 +105,11 @@ export class NanoClient {
85
105
  this.baseUrl = String(baseUrl).replace(/\/+$/, "");
86
106
  this.fetch = fetch;
87
107
  this.pollIntervals = { video: 5000, audio: 3000, x402: 3000, ...pollIntervals };
88
- this.timeouts = { video: 600000, audio: 300000, ...timeouts };
108
+ // video: no default deadline — NanoGPT jobs keep running server-side and long renders
109
+ // routinely exceed 10 min. Pass timeouts.video (ms) to cap a headless/CI run. Audio keeps
110
+ // a 5-min default (shorter jobs; still overridable). 3D stops at 25 min (the editor's
111
+ // MODEL3D_DEADLINE_MS); the job may still be running after that.
112
+ this.timeouts = { video: Infinity, audio: 300000, model3d: 25 * 60 * 1000, ...timeouts };
89
113
  }
90
114
 
91
115
  _auth() {
@@ -135,6 +159,27 @@ export class NanoClient {
135
159
  });
136
160
  if (cr.ok) {
137
161
  const ct = ((cr.headers && cr.headers.get && cr.headers.get("content-type")) || "");
162
+ const mime = ct.split(";")[0].trim().toLowerCase();
163
+ // A binary 2xx IS the replayed result: TTS (/api/v1/audio/speech) replays raw audio bytes,
164
+ // typed audio/* or a generic application/octet-stream. It's the only paid call that answers
165
+ // binary — images, video, chat and decisions all answer JSON — so image/* and video/* are
166
+ // not accepted here. The payment is now completed and can't be reused, so dropping this
167
+ // body and re-POSTing only gets a 402.
168
+ if (mime.startsWith("audio/") || mime === "application/octet-stream") {
169
+ return { paymentId: invoice.paymentId, statusUrl: invoice.statusUrl, response: cr };
170
+ }
171
+ // Any other non-JSON 2xx (a text/plain settlement ack, a text/html error page) is neither
172
+ // the result nor a settle receipt we can act on. The complete call may already have
173
+ // consumed the payment, so re-POSTing could get a second 402 and paying again would
174
+ // double-charge: fail with the payment id instead. No second payment, no re-POST.
175
+ if (mime && !mime.includes("json")) {
176
+ const snippet = String(await cr.text().catch(() => "")).replace(/\s+/g, " ").trim().slice(0, 160);
177
+ throw new NanoodleError(
178
+ "payment " + invoice.paymentId + " was sent, but the complete call answered HTTP " + cr.status +
179
+ " with " + mime + " instead of a result" + (snippet ? " (" + snippet + ")" : "") +
180
+ " — check " + (invoice.statusUrl || "the payment status") + " before paying again",
181
+ { code: "x402", status: cr.status, paymentId: invoice.paymentId });
182
+ }
138
183
  let cj = null;
139
184
  if (ct.includes("json")) { try { cj = await cr.json(); } catch { /* treat as settle-only */ } }
140
185
  if (looksLikeResult(cj)) {
@@ -196,6 +241,19 @@ export class NanoClient {
196
241
  return out;
197
242
  }
198
243
 
244
+ /**
245
+ * POST /api/v1/decisions (⚖️ Decide). Returns the JSON ({ answers, usage:{ input_tokens, output_tokens, cost } }).
246
+ * Decision models bill input tokens only; the real price rides usage.cost.
247
+ */
248
+ async decide(body, { onCost, signal } = {}) {
249
+ const r = await this._postJson("/api/v1/decisions", body, signal);
250
+ if (!r.ok) throw httpError(r.status, await r.text());
251
+ const j = await r.json();
252
+ const c = j && j.usage && typeof j.usage.cost === "number" ? j.usage.cost : null;
253
+ if (onCost) onCost(costWithHeaders(c != null ? { ...j, cost: c } : j, r));
254
+ return j;
255
+ }
256
+
199
257
  /** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s). */
200
258
  async image({ prompt, model, size, imageDataUrl, maskDataUrl, extra, n = 1, multi = false }, { onCost, signal } = {}) {
201
259
  const body = { model, size: size || "1024x1024", n, response_format: "b64_json" };
@@ -220,7 +278,10 @@ export class NanoClient {
220
278
  // io.resume (a runId) skips the submit — and its charge — and goes straight to
221
279
  // polling: how a timed-out job is picked back up without paying twice.
222
280
  async video(model, prompt, opts = {}, imageDataUrl, { onCost, onPoll, onRunId, resume, signal } = {}) {
223
- const body = { model, prompt };
281
+ const is3d = opts.mediaKind === "model3d";
282
+ const body = { model };
283
+ // 3D omits an empty prompt (image-only models). Video still sends a blank prompt.
284
+ if (!(is3d && !String(prompt || "").trim())) body.prompt = prompt;
224
285
  if (opts.duration) body.duration = opts.duration;
225
286
  if (opts.aspect_ratio) body.aspect_ratio = opts.aspect_ratio;
226
287
  if (opts.resolution) body.resolution = opts.resolution;
@@ -256,28 +317,38 @@ export class NanoClient {
256
317
  }
257
318
 
258
319
  const t0 = Date.now();
259
- while (Date.now() - t0 < this.timeouts.video) {
260
- await sleep(this.pollIntervals.video, signal);
320
+ const videoCap = is3d ? this.timeouts.model3d : this.timeouts.video;
321
+ const pollEvery = is3d ? (this.pollIntervals.model3d || this.pollIntervals.video) : this.pollIntervals.video;
322
+ // Infinity/non-finite = wait forever (default). Finite ms = headless/CI cap.
323
+ while (!Number.isFinite(videoCap) || Date.now() - t0 < videoCap) {
324
+ await sleep(pollEvery, signal);
261
325
  let s;
262
326
  try {
263
327
  s = await (await this._get("/api/video/status?requestId=" + encodeURIComponent(runId), signal)).json();
264
328
  } catch (e) {
265
329
  if (signal && signal.aborted) throw abortError(signal.reason);
266
- continue; // transient poll failure — keep polling until timeout
330
+ continue; // transient poll failure — keep polling until complete or optional cap
267
331
  }
268
332
  const st = String((s.data && s.data.status) || s.status || "").toUpperCase();
269
333
  if (onPoll) onPoll({ status: st, elapsedMs: Date.now() - t0, runId });
270
334
  if (st === "COMPLETED" || st === "SUCCEEDED") {
335
+ if (is3d) {
336
+ const url = model3dStatusUrl(s);
337
+ if (!url) throw new NanoodleError("completed but no model url");
338
+ return url;
339
+ }
271
340
  const out = (s.data && s.data.output) || s.output || {};
272
341
  const url = (out.video && out.video.url) || out.url || (Array.isArray(out.video) ? out.video[0] && out.video[0].url : null);
273
342
  if (!url) throw new NanoodleError("completed but no video url");
274
343
  return url;
275
344
  }
276
345
  if (["FAILED", "ERROR", "CANCELED"].includes(st)) {
277
- throw new NanoodleError("video failed: " + ((s.data && s.data.error) || st));
346
+ const why = (s.data && s.data.error) || st;
347
+ throw new NanoodleError((is3d ? "3D failed: " : "video failed: ") + why);
278
348
  }
279
349
  }
280
- throw new NanoodleError(`video timed out (${Math.round(this.timeouts.video / 1000)}s) — the job may still be running on NanoGPT's side`, { code: "timeout" });
350
+ const label = is3d ? "3D" : "video";
351
+ throw new NanoodleError(`${label} timed out (${Math.round(videoCap / 1000)}s) — the job may still be running on NanoGPT's side`, { code: "timeout" });
281
352
  }
282
353
 
283
354
  /**
@@ -293,6 +364,14 @@ export class NanoClient {
293
364
  return url;
294
365
  }
295
366
  const body = Object.assign({ model, input }, extra);
367
+ if (AUDIO_PROMPT_MODEL_RE.test(String(model || "").trim())) {
368
+ if (body.prompt == null || String(body.prompt).trim() === "") body.prompt = input;
369
+ delete body.input;
370
+ } else if (AUDIO_STYLE_MODEL_RE.test(String(model || "").trim())) {
371
+ if (body.style == null || String(body.style).trim() === "") body.style = input;
372
+ delete body.input;
373
+ delete body.prompt;
374
+ }
296
375
  const r = await this._postJson("/api/v1/audio/speech", body, signal);
297
376
  if (!r.ok) throw httpError(r.status, await r.text());
298
377
  const ct = (r.headers && r.headers.get && r.headers.get("content-type")) || "";
@@ -312,7 +391,7 @@ export class NanoClient {
312
391
  const bytes = new Uint8Array(await r.arrayBuffer());
313
392
  let mime = ct.split(";")[0].trim().toLowerCase();
314
393
  if (!mime || mime === "application/octet-stream" || mime === "binary/octet-stream") {
315
- mime = AUDIO_MIME[extra.response_format || "mp3"] || "audio/mpeg";
394
+ mime = AUDIO_MIME[(extra && (extra.output_format || extra.response_format)) || "mp3"] || "audio/mpeg";
316
395
  }
317
396
  return bytesToDataUrl(bytes, mime);
318
397
  }
package/src/decide.mjs ADDED
@@ -0,0 +1,160 @@
1
+ // ⚖️ Decide — typed judgments from NanoGPT decision models (POST /api/v1/decisions).
2
+ // Port of the editor/play twin block (nanoodle index.html / play.html "⚖️ DECIDE"): same
3
+ // question shapes, same image state parts, same in-order + reversed pick debias, same
4
+ // outputs. Only the on-device image shrink differs: browsers use canvas, this library
5
+ // uses ffmpeg (fitImageJpeg) — text-only decisions need no ffmpeg at all.
6
+ import { NanoodleError } from "./errors.mjs";
7
+
8
+ export const DECIDE_DEFAULT_MODEL = "perplexity/pplx-decider-v1.1-27b";
9
+ export const DECIDE_SCALE_DEFAULT = "poor\nokay\ngood\ngreat";
10
+ /** The live limits of every image-capable decision model at launch (catalog decision_input.image_limits). */
11
+ export const DECIDE_IMG_FALLBACK = { maxImages: 4, maxDimension: 512, maxEncodedBytes: 240000 };
12
+
13
+ export function decideMode(f) {
14
+ const m = f && f.mode;
15
+ return (m === "choose" || m === "score" || m === "yesno") ? m : "pick";
16
+ }
17
+ export function decideLines(s) {
18
+ return String(s == null ? "" : s).split("\n").map((x) => x.trim()).filter(Boolean);
19
+ }
20
+ export function decideDefaultQuestion(mode) {
21
+ if (mode === "pick") return "Which image best matches the brief?";
22
+ if (mode === "choose") return "Which label fits best?";
23
+ if (mode === "score") return "How good is it?";
24
+ return "Is it good enough to use?";
25
+ }
26
+ /** Image limits from a catalog decision_input; known=false (absent / offline) → permissive launch limits; known text-only → null. */
27
+ export function decideImageLimits(di, known) {
28
+ if (!known) return DECIDE_IMG_FALLBACK;
29
+ if (!di || !di.image_input) return null;
30
+ const l = di.image_limits || {};
31
+ return {
32
+ maxImages: l.maxImages > 0 ? l.maxImages : DECIDE_IMG_FALLBACK.maxImages,
33
+ maxDimension: l.maxDimension > 0 ? l.maxDimension : DECIDE_IMG_FALLBACK.maxDimension,
34
+ maxEncodedBytes: l.maxEncodedBytes > 0 ? l.maxEncodedBytes : DECIDE_IMG_FALLBACK.maxEncodedBytes,
35
+ };
36
+ }
37
+ /** The one typed question this node asks. Throws (before any request) on an unanswerable setup. */
38
+ export function decideQuestionFor(mode, f, nImgs) {
39
+ const q = String((f && f.question) || "").trim() || decideDefaultQuestion(mode);
40
+ if (mode === "pick") {
41
+ if (nImgs < 2) throw new NanoodleError("pick needs at least two images — wire 2 or more into the image ports");
42
+ const c = {};
43
+ for (let i = 1; i <= nImgs; i++) c["image_" + i] = "image " + i;
44
+ return { type: "choice", instructions: q, criteria: c };
45
+ }
46
+ if (mode === "choose") {
47
+ const labels = decideLines(f && f.options).filter((x, i, a) => a.indexOf(x) === i);
48
+ if (labels.length < 2) throw new NanoodleError("add at least two labels to choose from (one per line)");
49
+ if (labels.length > 255) throw new NanoodleError("too many labels — 255 at most");
50
+ const cc = {};
51
+ labels.forEach((x) => { cc[x] = null; });
52
+ return { type: "choice", instructions: q, criteria: cc };
53
+ }
54
+ if (mode === "score") {
55
+ const lv = decideLines((f && f.levels != null && String(f.levels).trim()) ? f.levels : DECIDE_SCALE_DEFAULT);
56
+ if (lv.length < 2 || lv.length > 10) throw new NanoodleError("the scale needs 2 to 10 levels, worst first (one per line)");
57
+ return { type: "score", instructions: q, criteria: lv };
58
+ }
59
+ return { type: "noul", instructions: q };
60
+ }
61
+ /** state: plain text, or (with images) text + labelled inline image_url parts. */
62
+ export function decideState(text, imgs) {
63
+ if (!imgs.length) return text;
64
+ const s = [];
65
+ if (text) s.push(text);
66
+ imgs.forEach((u, i) => {
67
+ if (imgs.length > 1) s.push("image_" + (i + 1) + ":");
68
+ s.push({ type: "image_url", image_url: { url: u } });
69
+ });
70
+ return s;
71
+ }
72
+ /** API answer → node outputs: text, image (pick: the winner; else the first image, passed through), decision detail. */
73
+ export function decideOutputs(mode, ans, q, imgs, usage, model) {
74
+ if (!ans || !ans.type) throw new NanoodleError("the decision model returned no answer");
75
+ const d = { mode, model, cost: (usage && typeof usage.cost === "number") ? usage.cost : null, rows: [] };
76
+ const probs = ans.probabilities || {};
77
+ if (mode === "pick") {
78
+ const k = String(ans.choice || ""), idx = parseInt(k.replace(/^image_/, ""), 10) || 1;
79
+ d.pick = idx; d.confidence = ans.confidence;
80
+ d.rows = Object.keys(q.criteria).map((key, i) => ({ label: "image " + (i + 1), p: +probs[key] || 0, win: key === k }));
81
+ return { text: "image " + idx, image: imgs[idx - 1] || "", decision: d };
82
+ }
83
+ if (mode === "choose") {
84
+ d.confidence = ans.confidence;
85
+ d.rows = Object.keys(q.criteria).map((key) => ({ label: key, p: +probs[key] || 0, win: key === ans.choice }));
86
+ return { text: String(ans.choice || ""), image: imgs[0] || "", decision: d };
87
+ }
88
+ if (mode === "score") {
89
+ const sc = (+ans.score || 0) + 1; // 1 = the first (worst) level
90
+ d.score = sc; d.levels = q.criteria.length; d.confidence = ans.confidence;
91
+ let top = 0;
92
+ q.criteria.forEach((x, i) => { if ((+probs[String(i)] || 0) > (+probs[String(top)] || 0)) top = i; });
93
+ d.rows = q.criteria.map((x, i) => ({ label: (i + 1) + " · " + x, p: +probs[String(i)] || 0, win: i === top }));
94
+ return { text: String(Math.round(sc * 100) / 100), image: imgs[0] || "", decision: d };
95
+ }
96
+ const py = Math.max(0, Math.min(1, +ans.noul || 0));
97
+ d.yes = py;
98
+ d.rows = [{ label: "yes", p: py, win: py >= 0.5 }, { label: "no", p: 1 - py, win: py < 0.5 }];
99
+ return { text: py >= 0.5 ? "yes" : "no", image: imgs[0] || "", decision: d };
100
+ }
101
+ /**
102
+ * A closed yes/no gate: a deliberate stop, not a failure. The runner throws this (so the browser
103
+ * twin and any direct RUNNERS caller see err.gate), and Workflow.run() settles it as node status
104
+ * "gated" — the decision ran and billed, `out` carries its answer, everything downstream is
105
+ * "skipped" unbilled, and the run itself succeeds.
106
+ */
107
+ export function decideGateError(d, out) {
108
+ return new NanoodleError(
109
+ "gate closed — the answer was no (yes " + Math.round(d.yes * 100) + "%), so nothing downstream ran",
110
+ { code: "decide-gate", gate: true, decision: d, out: out || null });
111
+ }
112
+ /** Average pick probabilities across runs that saw the candidates in different orders; costs add up. */
113
+ export function decideMergeOrders(js, orders) {
114
+ const n = orders[0].length, sum = new Array(n).fill(0);
115
+ let cost = 0, costKnown = true;
116
+ js.forEach((j, r) => {
117
+ const a = j && j.answers && j.answers.answer;
118
+ if (!a || !a.probabilities) throw new NanoodleError("the decision model returned no answer");
119
+ orders[r].forEach((orig, k) => { sum[orig] += (+a.probabilities["image_" + (k + 1)] || 0) / js.length; });
120
+ if (j.usage && typeof j.usage.cost === "number") cost += j.usage.cost; else costKnown = false;
121
+ });
122
+ let best = 0;
123
+ const probs = {};
124
+ sum.forEach((p, i) => { probs["image_" + (i + 1)] = p; if (p > sum[best]) best = i; });
125
+ return {
126
+ answers: { answer: { type: "choice", choice: "image_" + (best + 1), confidence: sum[best], probabilities: probs } },
127
+ usage: { cost: costKnown ? cost : null }, runs: js.length,
128
+ };
129
+ }
130
+ /**
131
+ * The whole run. send(body) POSTs /api/v1/decisions and returns the JSON; fit(url, maxDim, budget)
132
+ * returns a data: URL that fits (inlining remote URLs as needed).
133
+ */
134
+ export async function decideRun(f, model, text, imgs, lim, send, fit) {
135
+ const mode = decideMode(f);
136
+ text = String(text == null ? "" : text).trim();
137
+ imgs = (imgs || []).filter(Boolean);
138
+ if (imgs.length && !lim) throw new NanoodleError("this decision model can’t see images — pick one that can (e.g. PPLX Decider or Clef)");
139
+ if (lim && imgs.length > lim.maxImages) throw new NanoodleError("this decision model takes at most " + lim.maxImages + " images — unwire the extras");
140
+ const q = decideQuestionFor(mode, f, imgs.length);
141
+ if (!text && !imgs.length && !String((f && f.question) || "").trim()) throw new NanoodleError("nothing to judge — wire text or an image into Decide");
142
+ const sent = [];
143
+ if (imgs.length) {
144
+ const budget = Math.floor(lim.maxEncodedBytes * 0.95 / imgs.length);
145
+ for (const u of imgs) sent.push(await fit(u, lim.maxDimension, budget));
146
+ }
147
+ let j;
148
+ if (mode === "pick") {
149
+ // decision models lean toward the first image they see: ask in order and reversed, average
150
+ const fwd = sent.map((x, i) => i), rev = fwd.slice().reverse();
151
+ const both = await Promise.all([fwd, rev].map((ord) =>
152
+ send({ model, state: decideState(text, ord.map((i) => sent[i])), questions: { answer: q } })));
153
+ j = decideMergeOrders(both, [fwd, rev]);
154
+ } else {
155
+ j = await send({ model, state: decideState(text, sent), questions: { answer: q } });
156
+ }
157
+ const out = decideOutputs(mode, j && j.answers && j.answers.answer, q, imgs, j && j.usage, model);
158
+ if (mode === "yesno" && (f.gate === true || f.gate === "true") && out.decision.yes < 0.5) throw decideGateError(out.decision, out);
159
+ return out;
160
+ }