nanoodle 0.7.0 → 0.9.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.
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Prompt length caps — the wired prompt nobody can shorten.
3
+ *
4
+ * Many image and video models reject an over-long prompt at NanoGPT's route, before the provider is
5
+ * called and before anything is charged:
6
+ *
7
+ * 400 {"error":"Your prompt is too long for Qwen Image 3. Please shorten it to 800 characters
8
+ * or less (current: 831 characters).","code":"prompt_too_long"}
9
+ *
10
+ * Free, but a dead end. In a graph the prompt is generated, not typed — an LLM node writes it and a
11
+ * wire carries it into the image node — so "please shorten it" names something no caller controls
12
+ * directly. For a headless or agent-driven run that's the whole workflow lost to a limit nobody was
13
+ * told about.
14
+ *
15
+ * So: trim it to fit at a sentence boundary (fitPromptText) and REPORT it — a `prompt-trimmed`
16
+ * progress event plus a process warning. A silent trim would be a bug; the report is what makes the
17
+ * trim defensible. A cap we didn't know is learned from the live 400 (promptCapFromError) and kept
18
+ * on the Workflow, so a retry states the limit instead of rediscovering it.
19
+ *
20
+ * What this deliberately does NOT do is edit the graph's own prompts to avoid the trim. An llm
21
+ * node's system prompt goes to the model exactly as its author wrote it — injecting a "keep it
22
+ * under N characters" directive on their behalf would silently change what their workflow produces.
23
+ *
24
+ * The image/video catalogs advertise no prompt limit at all (checked live 2026-07-26), hence the
25
+ * probed table below. Audio's caps ARE catalog metadata (supported_parameters.max_chars) and are
26
+ * read from the opt-in catalog instead — never listed here. Absent id = no cap found, or never
27
+ * probed: unknown stays unconstrained, matching this library's permissive-gate rule everywhere else.
28
+ *
29
+ * Table generated by nanoodle's scripts/probe-prompt-caps.mjs; keep it in step with the copies in
30
+ * nanoodle's index.html / play.html.
31
+ */
32
+ import { catItem } from "./catalog.mjs";
33
+
34
+ export const PROMPT_CAPS = {
35
+ generated: "2026-07-26",
36
+ image: {
37
+ "step-image-edit-2": 512,
38
+ "qwen-image-3": 800,
39
+ "z-image-turbo": 1200,
40
+ "z-image-turbo-image-to-image": 1200,
41
+ "z-image-turbo-lora": 1200,
42
+ "2dn-pony-v2": 3000,
43
+ "aniflatmix-anime": 3000,
44
+ "aniflatmix-anime-sfwnsfw": 3000,
45
+ "animagine-xl-31": 3000,
46
+ "artiwaifu-diffusion": 3000,
47
+ "atomix-xl": 3000,
48
+ "boltning": 3000,
49
+ "crystal-clear-xl": 3000,
50
+ "crystal-clear-xlightning": 3000,
51
+ "cyberrealistic-pony-v9": 3000,
52
+ "cyberrealistic-xl": 3000,
53
+ "dreamshaper-v1": 3000,
54
+ "dreamshaper-xl": 3000,
55
+ "fluently-xl": 3000,
56
+ "flux-schnell": 3000,
57
+ "hassaku-hentai": 3000,
58
+ "hidream": 3000,
59
+ "hidream-i1-fast": 3000,
60
+ "ideogram:4@0": 3000,
61
+ "infinite-illustrious": 3000,
62
+ "juggernaut-lightning-flux": 3000,
63
+ "juggernaut-pro-flux": 3000,
64
+ "juggernaut-z": 3000,
65
+ "miusmius-xl": 3000,
66
+ "nsfw-gen-illustrious": 3000,
67
+ "persona:376130@2456367": 3000,
68
+ "pixelwave": 3000,
69
+ "proteus": 3000,
70
+ "prunaai:1@1": 3000,
71
+ "qwen-image": 3000,
72
+ "realpony-xl": 3000,
73
+ "rev-animated": 3000,
74
+ "riverflow-2-fast": 3000,
75
+ "riverflow-2-max": 3000,
76
+ "riverflow-2-standard": 3000,
77
+ "runware:106@1": 3000,
78
+ "runware:107@1": 3000,
79
+ "stable-diffusion-xl-turbo": 3000,
80
+ "stoiqo-new-reality": 3000,
81
+ "wai-illustrious-sdxl": 3000,
82
+ },
83
+ video: {
84
+ "minimax-hailuo-02": 2000,
85
+ "lightricks-ltx-2-fast": 3000,
86
+ },
87
+ };
88
+
89
+ // node type → which catalog its model comes from. Same split as estimate.mjs's PRICE_KIND, minus
90
+ // the types that take no prompt (transcribe) — chat is present so promptCap can say "no" for it:
91
+ // an LLM's own limit is tokens, and it is never the node being fitted.
92
+ const CAP_KIND = {
93
+ llm: "chat", vision: "chat",
94
+ image: "image", edit: "image", inpaint: "image",
95
+ tvideo: "video", ivideo: "video", vedit: "video", lipsync: "video",
96
+ music: "audio", remix: "audio", tts: "audio",
97
+ };
98
+
99
+ /**
100
+ * The prompt character cap for a node, or null when the model has none / we don't know one.
101
+ * `learned` (a Map of "kind:id" → cap) wins over the table: it came from this account hitting this
102
+ * model today, and the table is a snapshot.
103
+ */
104
+ export function promptCap(node, { catalog, learned } = {}) {
105
+ const kind = CAP_KIND[node && node.type];
106
+ const id = node && node.fields && node.fields.model;
107
+ if (!kind || !id || kind === "chat") return null;
108
+ const hit = learned && learned.get(kind + ":" + id);
109
+ if (hit) return hit;
110
+ if (kind === "audio") {
111
+ const m = catItem(catalog, "audio", id);
112
+ return (m && m.supported_parameters && m.supported_parameters.max_chars) || null;
113
+ }
114
+ return (PROMPT_CAPS[kind] && PROMPT_CAPS[kind][id]) || null;
115
+ }
116
+
117
+ /**
118
+ * Cut to the last sentence end inside the cap, then the last word — never mid-word, and never below
119
+ * 70% of the cap, where honouring a boundary would throw away more than the overflow did.
120
+ */
121
+ export function fitPromptText(s, cap) {
122
+ if (s.length <= cap) return s;
123
+ const head = s.slice(0, cap), floor = Math.floor(cap * 0.7);
124
+ const sent = Math.max(head.lastIndexOf(". "), head.lastIndexOf("! "), head.lastIndexOf("? "), head.lastIndexOf("\n"));
125
+ if (sent >= floor) return head.slice(0, sent + 1).trim();
126
+ const word = head.lastIndexOf(" ");
127
+ return (word >= floor ? head.slice(0, word) : head).trim();
128
+ }
129
+
130
+ /**
131
+ * Fit the prompt rather than lose the run to a 400 the caller can't act on. Returns
132
+ * { fields, trimmed } — `trimmed` is non-null exactly when the caller owes the user an explanation,
133
+ * and reporting it is not optional.
134
+ */
135
+ export function withFittedPrompt(node, fields, opts = {}) {
136
+ const cap = promptCap({ ...node, fields }, opts);
137
+ const p = fields && fields.prompt;
138
+ if (!cap || typeof p !== "string" || p.length <= cap) return { fields, trimmed: null };
139
+ const prompt = fitPromptText(p, cap);
140
+ return { fields: { ...fields, prompt }, trimmed: { from: p.length, to: prompt.length, cap } };
141
+ }
142
+
143
+ /**
144
+ * NanoGPT phrases the rejection three ways (all live-verified 2026-07-26):
145
+ * {"error":"…Please shorten it to 800 characters or less (current: 831 characters).","code":"prompt_too_long"}
146
+ * {"error":{"message":"…512 characters or less…","code":"prompt_too_long"}} ← nested
147
+ * {"error":"Prompt is too long. Please keep it under 3000 characters."} ← no code at all
148
+ * so match the sentence, not the envelope. "current: N" is deliberately unmatched — that's the
149
+ * length we SENT, and learning it as the cap would grow the limit with every failure.
150
+ */
151
+ const PROMPT_TOO_LONG_RE = /(?:shorten (?:it )?to|keep it under|at most|maximum of|limit of)\s*([\d,]{2,})\s*characters/i;
152
+
153
+ export const isPromptTooLong = (msg) => /prompt_too_long/i.test(String(msg || "")) || /prompt is too long/i.test(String(msg || ""));
154
+
155
+ export function promptCapFromError(msg) {
156
+ const m = PROMPT_TOO_LONG_RE.exec(String(msg || ""));
157
+ const v = m ? +m[1].replace(/,/g, "") : 0;
158
+ return v > 0 ? v : null;
159
+ }
160
+
161
+ /** Bank a cap learned from a live rejection. Returns true when it's new information. */
162
+ export function learnPromptCap(learned, node, msg) {
163
+ const cap = promptCapFromError(msg);
164
+ const kind = CAP_KIND[node && node.type];
165
+ const id = node && node.fields && node.fields.model;
166
+ if (!cap || !kind || !id || learned.get(kind + ":" + id) === cap) return false;
167
+ learned.set(kind + ":" + id, cap);
168
+ return true;
169
+ }
package/src/workflow.mjs CHANGED
@@ -4,6 +4,7 @@ import { deriveInputs, deriveOutputs, deriveSettings, resolveInputKey, resolveSe
4
4
  import { NanoClient } from "./client.mjs";
5
5
  import { MediaRef, coerceMediaInput } from "./media.mjs";
6
6
  import { RUNNERS } from "./nodes.mjs";
7
+ import { learnPromptCap, withFittedPrompt } from "./prompt-caps.mjs";
7
8
  import { decodeShareUrl, isShareRef } from "./share.mjs";
8
9
 
9
10
  /** Env / process access — safe when `process` is missing (browsers, some workers). */
@@ -38,14 +39,30 @@ function throwIfAborted(signal) {
38
39
  }
39
40
 
40
41
  /** The outcome of Workflow.run(). Media values are MediaRef; text values plain strings. */
42
+ /** Internal: thrown by a node skipped behind a closed gate, so ITS dependents skip too. Never surfaced. */
43
+ class GateSkip extends Error {
44
+ constructor(gatedBy) { super("skipped — a gate upstream said no"); this.gatedBy = gatedBy; }
45
+ }
46
+
41
47
  export class RunResult {
42
- constructor({ outputs, nodes, errors, costUsd, costExact, remainingBalance }) {
43
- /** { [friendlyKey | nodeId]: value } — sink node primary outputs */
48
+ constructor({ outputs, nodes, errors, gated = [], costUsd, costExact, remainingBalance }) {
49
+ /** { [friendlyKey | nodeId]: value } — sink node primary outputs (a sink skipped by a closed gate has none) */
44
50
  this.outputs = outputs;
45
- /** per-node { status: "done"|"error"|"skipped", out, error, costUsd, ms } */
51
+ /**
52
+ * per-node { status: "done"|"error"|"gated"|"skipped", out, error, costUsd, ms }.
53
+ * "gated": a ⚖️ Decide yes/no gate that answered no — it ran and billed, `out` holds its answer
54
+ * ({ text: "no", decision }), and it carries `gate: { yes, message }`. Not an error.
55
+ * "skipped" + `gatedBy: <decide node id>`: downstream of a closed gate — never ran, never billed.
56
+ * (Annotation notes are "skipped" with no gatedBy.)
57
+ */
46
58
  this.nodes = nodes;
47
- /** [{ nodeId, name, message }] for every node that failed (incl. non-sink warnings) */
59
+ /** [{ nodeId, name, message }] for every node that failed (incl. non-sink warnings) — never gates */
48
60
  this.errors = errors;
61
+ /**
62
+ * [{ nodeId, name, message, yes, skipped: [nodeId…] }] — every Decide gate that closed this run.
63
+ * A closed gate is a deliberate, successful outcome: run() resolves, it never throws for one.
64
+ */
65
+ this.gated = gated;
49
66
  /** summed USD cost of all calls that reported one */
50
67
  this.costUsd = costUsd;
51
68
  /** false when any network call omitted its price (total is a floor) */
@@ -62,6 +79,8 @@ export class RunResult {
62
79
  for (const k of Object.keys(this.outputs)) {
63
80
  if (k.toLowerCase() === norm) return this.outputs[k];
64
81
  }
82
+ const g = (this._gatedOutputs || []).find((o) => o.key.toLowerCase() === norm || o.nodeId.toLowerCase() === norm);
83
+ if (g) throw new NanoodleError(`output "${g.key}" was skipped — the gate "${g.gateName}" answered no, so it never ran (see result.gated)`, { code: "gated" });
65
84
  throw new NanoodleError(`no output "${key}" — available outputs: ${Object.keys(this.outputs).map((k) => `"${k}"`).join(", ") || "(none)"}`);
66
85
  }
67
86
  }
@@ -84,6 +103,12 @@ export class Workflow {
84
103
  const { nodes, links, warnings } = materialize(graphData);
85
104
  this.graph = { nodes, links };
86
105
  this.catalog = opts.catalog || null;
106
+ /**
107
+ * Prompt character caps learned from live 400s ("kind:id" → cap), on top of the probed table in
108
+ * prompt-caps.mjs. Per-instance and in-memory: a run that hits an unknown model's limit teaches
109
+ * the next run of the same Workflow, which is where a retry actually happens.
110
+ */
111
+ this._promptCaps = new Map();
87
112
  /** Load-time warnings (unknown / unsupported node types). load() only warns; run() fails fast. */
88
113
  this.warnings = warnings;
89
114
  this.client = new NanoClient({
@@ -259,6 +284,7 @@ export class Workflow {
259
284
  const io = { onCost, onPoll, signal: ac.signal };
260
285
  return {
261
286
  chat: (messages, model, opts) => this.client.chat(messages, model, opts, io),
287
+ decide: (body) => this.client.decide(body, io),
262
288
  image: (args) => this.client.image(args, io),
263
289
  video: (model, prompt, opts, imageDataUrl) => this.client.video(model, prompt, opts, imageDataUrl, io),
264
290
  audio: (model, input, extra) => this.client.audio(model, input, extra, io),
@@ -272,6 +298,15 @@ export class Workflow {
272
298
  };
273
299
  };
274
300
 
301
+ const gated = [];
302
+ // a node whose upstream settled as a closed gate (or was itself skipped by one) → that gate's id
303
+ const gateOf = (id) => {
304
+ const r = nodesRec[id];
305
+ if (!r) return null;
306
+ if (r.status === "gated") return id;
307
+ if (r.status === "skipped" && r.gatedBy) return r.gatedBy;
308
+ return null;
309
+ };
275
310
  const execNode = async (n) => {
276
311
  const rec = nodesRec[n.id];
277
312
  try {
@@ -279,20 +314,51 @@ export class Workflow {
279
314
  const inbound = graph.links.filter((l) => l.to.node === n.id);
280
315
  const inp = {};
281
316
  let fields = effFields.get(n.id);
282
- let upstreamFail = null;
317
+ let upstreamFail = null, gatedBy = null;
283
318
  for (const l of inbound) {
284
319
  let srcOut;
285
320
  try { srcOut = await promises.get(l.from.node); }
286
- catch { if (!upstreamFail) upstreamFail = displayName(byId.get(l.from.node)); continue; }
321
+ catch {
322
+ const g = gateOf(l.from.node);
323
+ if (g) { if (!gatedBy) gatedBy = g; } else if (!upstreamFail) upstreamFail = displayName(byId.get(l.from.node));
324
+ continue;
325
+ }
287
326
  const v = srcOut[l.from.port];
288
327
  if (isInputPort(n, l.to.port)) inp[l.to.port] = v;
328
+ // Custom endpoint url/mode: a Choice path like "/post" must ride inp so
329
+ // endpointResolveTarget can join it onto the typed host. Overwriting
330
+ // fields.url with "/post" rejects the joined URL.
331
+ else if (n.type === "endpoint" && (l.to.port === "url" || l.to.port === "mode")) {
332
+ if (v != null) inp[l.to.port] = v;
333
+ }
289
334
  // wired textarea port = field override; a missing upstream port (degraded save) must
290
335
  // NOT clobber the typed field with undefined — the app only applies v != null
291
336
  else if (v != null) fields = { ...fields, [l.to.port]: v };
292
337
  }
338
+ // a real failure upstream wins over a gate (the browser runners agree): that is an error
293
339
  if (upstreamFail) throw new NanoodleError("upstream failed: " + upstreamFail);
340
+ if (gatedBy) {
341
+ rec.status = "skipped";
342
+ rec.gatedBy = gatedBy;
343
+ const gr = gated.find((x) => x.nodeId === gatedBy);
344
+ if (gr) gr.skipped.push(n.id);
345
+ emit({ type: "node-skipped", nodeId: n.id, name: displayName(n), reason: "gated", gatedBy });
346
+ throw new GateSkip(gatedBy);
347
+ }
294
348
  throwIfAborted(ac.signal);
349
+ // Prompt length caps (see prompt-caps.mjs). Many image/video models reject an over-long
350
+ // prompt at the route, and in a graph the prompt is WRITTEN by an upstream LLM — so a
351
+ // caller has nothing to shorten. Trim it to fit rather than send a request certain to 400,
352
+ // and report it below. The graph's own prompts are never rewritten to avoid this.
353
+ const fit = withFittedPrompt(n, fields, { catalog: this.catalog, learned: this._promptCaps });
354
+ fields = fit.fields;
295
355
  emit({ type: "node-start", nodeId: n.id, name: displayName(n) });
356
+ // A fitted prompt changed what this run pays for — never silent, even headless.
357
+ if (fit.trimmed) {
358
+ emit({ type: "prompt-trimmed", nodeId: n.id, name: displayName(n), ...fit.trimmed });
359
+ warnGraph(`node ${n.id}: prompt trimmed ${fit.trimmed.from} → ${fit.trimmed.to} characters — ` +
360
+ `${fields.model} rejects prompts over ${fit.trimmed.cap}`);
361
+ }
296
362
  const t0 = Date.now();
297
363
  const out = await RUNNERS[n.type]({ ...n, fields }, inp, ctxFor(n, rec));
298
364
  throwIfAborted(ac.signal);
@@ -302,6 +368,22 @@ export class Workflow {
302
368
  emit({ type: "node-done", nodeId: n.id, name: displayName(n), ms: rec.ms, costUsd: rec.costUsd });
303
369
  return out;
304
370
  } catch (e) {
371
+ if (e instanceof GateSkip) throw e;
372
+ // ⚖️ Decide gate answered no: a settled, billed decision — not an error. Downstream skips.
373
+ if (e && e.gate === true && !ac.signal.aborted) {
374
+ rec.status = "gated";
375
+ rec.out = e.out || null;
376
+ rec.gate = { yes: e.decision && typeof e.decision.yes === "number" ? e.decision.yes : null, message: e.message };
377
+ gated.push({ nodeId: n.id, name: displayName(n), message: e.message, yes: rec.gate.yes, skipped: [] });
378
+ emit({ type: "node-gated", nodeId: n.id, name: displayName(n), message: e.message, yes: rec.gate.yes, costUsd: rec.costUsd });
379
+ throw e;
380
+ }
381
+ // A prompt-length rejection is free (nothing generated) and, once banked, preventable: the
382
+ // next run budgets the LLM above this node and fits whatever is left. Say that, rather than
383
+ // relaying "please shorten it" about a prompt the caller never wrote.
384
+ if (learnPromptCap(this._promptCaps, n, e.message)) {
385
+ e.message = e.message + " — noted: nanoodle keeps this model's prompt inside that limit from now on, so running again should succeed";
386
+ }
305
387
  rec.status = "error";
306
388
  rec.error = e.message;
307
389
  errors.push({ nodeId: n.id, name: displayName(n), message: e.message });
@@ -326,7 +408,7 @@ export class Workflow {
326
408
  const outputsMap = {};
327
409
  for (const o of this.outputs) {
328
410
  const rec = nodesRec[o.nodeId];
329
- if (!rec || rec.status !== "done") continue;
411
+ if (!rec || !(rec.status === "done" || (rec.status === "gated" && rec.out))) continue;
330
412
  const primary = o.ports[0];
331
413
  const value = this._wrapValue(rec.out[primary.name], primary.type);
332
414
  outputsMap[o.key] = value;
@@ -336,10 +418,17 @@ export class Workflow {
336
418
  outputs: outputsMap,
337
419
  nodes: nodesRec,
338
420
  errors,
421
+ gated,
339
422
  costUsd: cost.total,
340
423
  costExact: cost.exact,
341
424
  remainingBalance: cost.balance,
342
425
  });
426
+ // sinks a closed gate skipped: get() names the gate instead of "no output"
427
+ Object.defineProperty(result, "_gatedOutputs", {
428
+ enumerable: false,
429
+ value: this.outputs.filter((o) => nodesRec[o.nodeId] && nodesRec[o.nodeId].gatedBy)
430
+ .map((o) => ({ key: o.key, nodeId: o.nodeId, gateName: displayName(byId.get(nodesRec[o.nodeId].gatedBy)) })),
431
+ });
343
432
 
344
433
  // timeout/abort must fail the run even when local media finished after the deadline
345
434
  // (or nodes never observed the signal). Prefer the abort reason when present.