nanoodle 0.7.0 → 0.8.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,36 @@ 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
+ ### Prompt length caps
174
+
175
+ Many image and video models reject an over-long prompt at NanoGPT's route
176
+ (`400 prompt_too_long`) — `qwen-image-3` stops at 800 characters,
177
+ `step-image-edit-2` at 512, 45 of 183 probed image models cap somewhere. The
178
+ catalogs don't advertise any of it, and in a graph the prompt is written by an
179
+ upstream LLM, so there's nothing for a caller to shorten. So `run()`:
180
+
181
+ 1. **trims** a prompt over the cap, at a sentence boundary (never mid-word,
182
+ never below 70% of the cap), rather than sending a request certain to fail;
183
+ 2. **reports** every trim — a `prompt-trimmed` progress event
184
+ (`{ nodeId, name, from, to, cap }`) and a process warning. Never silent;
185
+ 3. **learns** a cap from a live 400 and keeps it on the `Workflow` instance, so
186
+ a retry of an unknown or newly-added model trims up front instead of failing
187
+ again.
188
+
189
+ It does **not** rewrite the graph's own prompts to avoid a trim: an `llm` node's
190
+ system prompt goes to the model exactly as its author wrote it. Expect the trim
191
+ to fire routinely on tight models — measured against five chat models writing an
192
+ image prompt, all five overshot an 800-char cap and the trim kept 29–62%. Handle
193
+ `prompt-trimmed` if that matters to you.
194
+
195
+ Audio caps come from `supported_parameters.max_chars` when you pass a
196
+ [catalog](#supported-nodes); image/video caps come from a probed table. A model
197
+ with no known cap is left completely alone.
198
+
199
+ The pieces are exported if you orchestrate graphs yourself:
200
+ `promptCap`, `fitPromptText`, `isPromptTooLong`, `promptCapFromError`,
201
+ `PROMPT_CAPS`.
202
+
173
203
  ## Supported nodes
174
204
 
175
205
  | runs | node types |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoodle",
3
- "version": "0.7.0",
3
+ "version": "0.8.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/index.mjs CHANGED
@@ -8,6 +8,8 @@ export { NanoClient } from "./client.mjs";
8
8
  export { NODE_TYPES, displayName, materialize, topoSort, wiredFramesFloor, MAX_FRAMES } from "./graph.mjs";
9
9
  export { deriveInputs, deriveOutputs, deriveSettings, INPUT_SPECS, SETTING_SPECS } from "./io.mjs";
10
10
  export { estimateGraphCost, graphModelKinds } from "./estimate.mjs";
11
+ // prompt length caps — a caller doing its own orchestration needs the same trim-and-say-so
12
+ export { PROMPT_CAPS, promptCap, fitPromptText, isPromptTooLong, promptCapFromError } from "./prompt-caps.mjs";
11
13
  export { decodeShareUrl, decodeShareFragment, isShareRef } from "./share.mjs";
12
14
  export { parseNanoInvoice } from "./x402.mjs";
13
15
  export { qrTerminal, qrModules } from "./qr.mjs";
@@ -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). */
@@ -84,6 +85,12 @@ export class Workflow {
84
85
  const { nodes, links, warnings } = materialize(graphData);
85
86
  this.graph = { nodes, links };
86
87
  this.catalog = opts.catalog || null;
88
+ /**
89
+ * Prompt character caps learned from live 400s ("kind:id" → cap), on top of the probed table in
90
+ * prompt-caps.mjs. Per-instance and in-memory: a run that hits an unknown model's limit teaches
91
+ * the next run of the same Workflow, which is where a retry actually happens.
92
+ */
93
+ this._promptCaps = new Map();
87
94
  /** Load-time warnings (unknown / unsupported node types). load() only warns; run() fails fast. */
88
95
  this.warnings = warnings;
89
96
  this.client = new NanoClient({
@@ -292,7 +299,19 @@ export class Workflow {
292
299
  }
293
300
  if (upstreamFail) throw new NanoodleError("upstream failed: " + upstreamFail);
294
301
  throwIfAborted(ac.signal);
302
+ // Prompt length caps (see prompt-caps.mjs). Many image/video models reject an over-long
303
+ // prompt at the route, and in a graph the prompt is WRITTEN by an upstream LLM — so a
304
+ // caller has nothing to shorten. Trim it to fit rather than send a request certain to 400,
305
+ // and report it below. The graph's own prompts are never rewritten to avoid this.
306
+ const fit = withFittedPrompt(n, fields, { catalog: this.catalog, learned: this._promptCaps });
307
+ fields = fit.fields;
295
308
  emit({ type: "node-start", nodeId: n.id, name: displayName(n) });
309
+ // A fitted prompt changed what this run pays for — never silent, even headless.
310
+ if (fit.trimmed) {
311
+ emit({ type: "prompt-trimmed", nodeId: n.id, name: displayName(n), ...fit.trimmed });
312
+ warnGraph(`node ${n.id}: prompt trimmed ${fit.trimmed.from} → ${fit.trimmed.to} characters — ` +
313
+ `${fields.model} rejects prompts over ${fit.trimmed.cap}`);
314
+ }
296
315
  const t0 = Date.now();
297
316
  const out = await RUNNERS[n.type]({ ...n, fields }, inp, ctxFor(n, rec));
298
317
  throwIfAborted(ac.signal);
@@ -302,6 +321,12 @@ export class Workflow {
302
321
  emit({ type: "node-done", nodeId: n.id, name: displayName(n), ms: rec.ms, costUsd: rec.costUsd });
303
322
  return out;
304
323
  } catch (e) {
324
+ // A prompt-length rejection is free (nothing generated) and, once banked, preventable: the
325
+ // next run budgets the LLM above this node and fits whatever is left. Say that, rather than
326
+ // relaying "please shorten it" about a prompt the caller never wrote.
327
+ if (learnPromptCap(this._promptCaps, n, e.message)) {
328
+ e.message = e.message + " — noted: nanoodle keeps this model's prompt inside that limit from now on, so running again should succeed";
329
+ }
305
330
  rec.status = "error";
306
331
  rec.error = e.message;
307
332
  errors.push({ nodeId: n.id, name: displayName(n), message: e.message });