nanoodle 0.6.2 → 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,13 +170,43 @@ 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 |
176
206
  |---|---|
177
207
  | local | text, upload (image/audio/video), choice, join, comment |
178
208
  | local media† | resize, vframes, combine, soundtrack, trim, extractaudio |
179
- | NanoGPT | llm (incl. vision + audio input), image, draw, edit, inpaint*, vision, tvideo, ivideo, vedit, lipsync, music, remix, tts, transcribe |
209
+ | NanoGPT | llm (incl. vision + audio input), image, edit, inpaint*, vision, tvideo, ivideo, vedit, lipsync, music, remix, tts, transcribe |
180
210
 
181
211
  † **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.
182
212
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoodle",
3
- "version": "0.6.2",
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/client.mjs CHANGED
@@ -196,26 +196,6 @@ export class NanoClient {
196
196
  return out;
197
197
  }
198
198
 
199
- /** Draw-node twin of chat(): the model answers with images in message.images[]. */
200
- async chatImage(messages, model, opts = {}, { onCost, signal } = {}) {
201
- const body = { model, messages };
202
- body.temperature = opts.temperature != null && opts.temperature !== "" ? +opts.temperature : 0.8;
203
- const r = await this._postJson("/api/v1/chat/completions", body, signal);
204
- if (!r.ok) throw httpError(r.status, await r.text());
205
- const j = await r.json();
206
- if (onCost) onCost(costWithHeaders(j, r));
207
- const msg = (j.choices && j.choices[0] && j.choices[0].message) || {};
208
- const images = (msg.images || [])
209
- .map((im) => (im && im.image_url && im.image_url.url) || (im && im.url) || (typeof im === "string" ? im : null))
210
- .filter(Boolean);
211
- const text = typeof msg.content === "string" ? msg.content
212
- : Array.isArray(msg.content) ? msg.content.map((p) => p.text || "").join("") : "";
213
- if (!images.length) {
214
- throw new NanoodleError(text ? "this model replied with text, not an image — pick an image-output model" : "no image in response");
215
- }
216
- return { images, text, reasoning: msg.reasoning || "" };
217
- }
218
-
219
199
  /** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s). */
220
200
  async image({ prompt, model, size, imageDataUrl, maskDataUrl, extra, n = 1, multi = false }, { onCost, signal } = {}) {
221
201
  const body = { model, size: size || "1024x1024", n, response_format: "b64_json" };
@@ -0,0 +1,262 @@
1
+ // Up-front run-cost estimate for a graph — a USD forecast computed from the
2
+ // public NanoGPT catalog BEFORE anything runs. Pure: it never fetches. The
3
+ // caller supplies the raw catalog arrays (the same ones the /api/v1/*-models
4
+ // endpoints return); this module walks the graph's billable nodes, prices each
5
+ // against its chosen model, and sums.
6
+ //
7
+ // This is the twin of the editor/play "~$X to run" chip (index.html /
8
+ // play.html): the resolver math is ported node-for-node so a hosted server and
9
+ // the app forecast the same number. Image is exact; chat/video/audio are
10
+ // estimates (marked `exact:false`) because their true cost depends on how many
11
+ // tokens/seconds the run actually produces. `unpriced` counts billable nodes we
12
+ // could not price (model missing from the catalog, or a pricing shape we don't
13
+ // recognise) — a nonzero count means the sum is a LOWER bound.
14
+ //
15
+ // Cost is only ever KNOWN after a run (the API reports it); this is the forecast
16
+ // that lets a paid server quote a deposit that actually covers the call on the
17
+ // very first request, instead of a flat guess.
18
+
19
+ import { REF_PORT_RE } from "./graph.mjs";
20
+
21
+ // node type → pricing "kind" == which catalog it's priced from + which unit fn.
22
+ // Mirrors the app's SETTING_MODEL_KIND. This set is exactly the billable
23
+ // (network) node types; a type absent here is a free local node and costs $0.
24
+ const PRICE_KIND = {
25
+ llm: "chat", vision: "chat", draw: "chat",
26
+ image: "image", edit: "image", inpaint: "image",
27
+ tvideo: "video", ivideo: "video", vedit: "video", lipsync: "video",
28
+ music: "audio", remix: "audio", tts: "audio", transcribe: "audio",
29
+ };
30
+
31
+ // Fallback assumptions when a run's real quantity isn't knowable up front.
32
+ const EST = { llmInTokens: 1000, llmOutTokens: 500, ttsChars: 600, audioSeconds: 30, sttMinutes: 1, videoFps: 30 };
33
+
34
+ const _num = (x) => (x != null && isFinite(+x)) ? +x : null;
35
+
36
+ function pickByRes(map, reqRes, defRes) {
37
+ if (!map || typeof map !== "object") return null;
38
+ if (reqRes != null && _num(map[reqRes]) != null) return _num(map[reqRes]);
39
+ if (defRes != null && _num(map[defRes]) != null) return _num(map[defRes]);
40
+ const vals = Object.values(map).map(_num).filter((v) => v != null);
41
+ return vals.length ? vals[0] : null;
42
+ }
43
+ function pickObjByRes(map, reqRes, defRes) {
44
+ if (!map || typeof map !== "object") return null;
45
+ if (reqRes != null && map[reqRes] && typeof map[reqRes] === "object") return map[reqRes];
46
+ if (defRes != null && map[defRes] && typeof map[defRes] === "object") return map[defRes];
47
+ const objs = Object.values(map).filter((v) => v && typeof v === "object");
48
+ return objs.length ? objs[0] : null;
49
+ }
50
+ function pickDur(p, raw, fields) {
51
+ const f = parseFloat(fields && fields.duration);
52
+ if (isFinite(f) && f > 0) return f;
53
+ const d = _num(p.default_duration) ?? _num(raw.defaultDuration);
54
+ if (d != null && d > 0) return d;
55
+ if (p.per_duration) { const k = Object.keys(p.per_duration)[0]; if (k && isFinite(+k)) return +k; }
56
+ if (Array.isArray(p.supported_durations) && p.supported_durations.length) return +p.supported_durations[0];
57
+ if (p.fixed_duration_seconds != null) return _num(p.fixed_duration_seconds) || 5;
58
+ return 5;
59
+ }
60
+
61
+ // A video run bills a HIGHER tier when reference images are wired or the model's
62
+ // audio switch is on. refWired comes from the graph wiring; the audio switch /
63
+ // reference mode ride in fields.modelOpts.
64
+ function videoUnitUsd(pricing, fields, refWired) {
65
+ const p = pricing || {};
66
+ const f = fields || {};
67
+ const opts = (f.modelOpts && typeof f.modelOpts === "object") ? f.modelOpts : {};
68
+ let audioOn = false;
69
+ for (const k in opts) { if (/audio/i.test(k) && !/^(no|disable|without|mute)/i.test(k)) { const val = opts[k]; if (val === true || val === "true" || val === 1 || val === "1" || val === "on" || val === "yes") { audioOn = true; break; } } }
70
+ const refOn = !!refWired || /reference/i.test(String(opts.mode || ""));
71
+ const usd = videoBaseUsd(p, f, audioOn, refOn);
72
+ if (usd != null && isFinite(usd) && audioOn && p.audio_multiplier != null) return usd * (_num(p.audio_multiplier) ?? 1);
73
+ return usd;
74
+ }
75
+ function videoBaseUsd(pricing, fields, audioOn, refOn) {
76
+ const p = pricing || {}, raw = (p.raw && typeof p.raw === "object") ? p.raw : {};
77
+ const f = fields || {};
78
+ const res = f.resolution, defRes = p.default_resolution ?? raw.defaultResolution;
79
+ const dur = pickDur(p, raw, f);
80
+ const rp = (m) => pickByRes(m, res, defRes);
81
+ let v;
82
+ if ((v = rp(p.per_second_by_resolution)) != null) return v * dur;
83
+ if ((v = rp(p.standard_prices_per_second)) != null) return v * dur;
84
+ if (p.per_second_by_mode) {
85
+ const mm = p.per_second_by_mode;
86
+ v = refOn ? (_num(mm.reference_to_video_image) ?? _num(mm.reference_to_video_video)) : null;
87
+ if (v == null) v = _num(mm.text_to_video) ?? pickByRes(mm);
88
+ if (v != null) return v * dur;
89
+ }
90
+ if (audioOn) {
91
+ for (const k of ["text_image_with_audio_per_second", "image_to_video_with_audio_per_second", "text_to_video_with_audio_per_second"]) {
92
+ if (p[k] != null) { v = (typeof p[k] === "object") ? rp(p[k]) : _num(p[k]); if (v != null) return v * dur; }
93
+ }
94
+ }
95
+ for (const k of ["text_to_video_per_second", "text_image_without_audio_per_second", "image_to_video_without_audio_per_second"]) {
96
+ if (p[k] != null) { v = (typeof p[k] === "object") ? rp(p[k]) : _num(p[k]); if (v != null) return v * dur; }
97
+ }
98
+ if (p.base_price_per_second != null) { const m = p.resolution_multipliers ? (rp(p.resolution_multipliers) ?? 1) : 1; v = _num(p.base_price_per_second); if (v != null) return v * dur * m; }
99
+ if (p.per_second != null) { v = _num(p.per_second); if (v != null) return v * dur; }
100
+ if (p.per_duration) { const dk = String(Math.round(dur)); v = _num(p.per_duration[dk]) ?? _num(p.per_duration[String(dur)]) ?? pickByRes(p.per_duration); if (v != null) return v; }
101
+ if (p.without_audio != null || p.with_audio != null) { v = audioOn ? (_num(p.with_audio) ?? _num(p.without_audio)) : (_num(p.without_audio) ?? _num(p.with_audio)); if (v != null) return v; }
102
+ if (p.base_prices_by_resolution) {
103
+ const dk = String(Math.round(dur));
104
+ if (p.duration_overrides && p.duration_overrides[dk]) { v = rp(p.duration_overrides[dk]); if (v != null) return v; }
105
+ const base = rp(p.base_prices_by_resolution);
106
+ let mult = 1;
107
+ if (p.duration_multiplier != null) mult = _num(p.duration_multiplier) ?? 1;
108
+ else if (p.duration_multipliers) mult = _num(p.duration_multipliers[dk]) ?? pickByRes(p.duration_multipliers) ?? 1;
109
+ if (base != null) return base * mult;
110
+ }
111
+ if ((v = rp(p.per_resolution)) != null) return v;
112
+ if (p.base_price != null && p.per_extra_second != null) { const bd = _num(p.base_duration) || 0; return _num(p.base_price) + Math.max(0, dur - bd) * _num(p.per_extra_second); }
113
+ if (p.per_video != null) { v = _num(p.per_video); if (v != null) return v; }
114
+ if (p.per_target_megapixel_second != null) { const mp = _num(p.default_target_megapixels) || 1; v = _num(p.per_target_megapixel_second) * mp * dur; return Math.max(v, _num(p.minimum_price) || 0); }
115
+ if (p.per_frame_unit != null) { const fpu = _num(p.frames_per_unit) || 1; return (dur * EST.videoFps / fpu) * _num(p.per_frame_unit); }
116
+ if (p.base_price != null) { v = _num(p.base_price); if (v != null) return v; }
117
+ if (Object.keys(raw).length) {
118
+ if (refOn) {
119
+ if (raw.referenceToVideoPrices) { const inner = pickObjByRes(raw.referenceToVideoPrices, res, defRes); if (inner) { const dk = String(Math.round(dur)); v = _num(inner[dk]) ?? pickByRes(inner); if (v != null) return v; } }
120
+ for (const k of ["textToVideoWithReferenceVideoPricesPerSecond", "standardTextToVideoWithReferenceVideoPricesPerSecond", "turboTextToVideoWithReferenceVideoPricesPerSecond",
121
+ "standardReferenceVideoPricesPerSecond", "fastReferenceVideoPricesPerSecond"]) {
122
+ if (raw[k]) { v = pickByRes(raw[k], res, defRes); if (v != null) return v * dur; }
123
+ }
124
+ }
125
+ if (audioOn) {
126
+ if (raw.withAudioPricesPerSecond) { v = pickByRes(raw.withAudioPricesPerSecond, res, defRes); if (v != null) return v * dur; }
127
+ if (raw.withAudioPrices) { const inner = pickObjByRes(raw.withAudioPrices, res, defRes); if (inner) { const dk = String(Math.round(dur)); v = _num(inner[dk]) ?? pickByRes(inner); if (v != null) return v; } }
128
+ }
129
+ for (const k of ["pricesPerSecond", "textToVideoPricesPerSecond", "withoutAudioPricesPerSecond",
130
+ "standardPricesPerSecond", "standardTextToVideoPricesPerSecond", "outputPricesPerSecond", "fastPricesPerSecond"]) {
131
+ if (raw[k]) { v = pickByRes(raw[k], res, defRes); if (v != null) return v * dur; }
132
+ }
133
+ for (const k of ["withoutAudioPrices", "withAudioPrices", "referenceToVideoPrices"]) {
134
+ if (raw[k]) { const inner = pickObjByRes(raw[k], res, defRes); if (inner) { const dk = String(Math.round(dur)); v = _num(inner[dk]) ?? pickByRes(inner); if (v != null) return v; } }
135
+ }
136
+ if (raw.pricePerMegapixel != null && raw.megapixelsByResolution) {
137
+ const mp = _num(raw.megapixelsByResolution[res]) ?? _num(raw.megapixelsByResolution[defRes]) ?? pickByRes(raw.megapixelsByResolution);
138
+ const nf = _num(raw.defaultNumFrames) || (dur * (_num(raw.defaultFramesPerSecond) || 24));
139
+ if (mp != null) return _num(raw.pricePerMegapixel) * mp * nf;
140
+ }
141
+ }
142
+ return genericScanUsd(p, dur);
143
+ }
144
+ // Last resort so a never-seen pricing shape estimates SOMETHING rather than
145
+ // blanking: the smallest per-second number scaled by duration, else the
146
+ // smallest flat number.
147
+ function genericScanUsd(p, dur) {
148
+ let perSec = null, flat = null;
149
+ (function walk(o, key) {
150
+ if (o == null) return;
151
+ if (typeof o === "number") { if (o > 0 && o < 500) { if (/second|persec/i.test(key)) perSec = Math.min(perSec ?? Infinity, o); else flat = Math.min(flat ?? Infinity, o); } return; }
152
+ if (typeof o === "object") for (const k of Object.keys(o)) walk(o[k], k);
153
+ })(p, "");
154
+ if (perSec != null) return perSec * dur;
155
+ if (flat != null) return flat;
156
+ return null;
157
+ }
158
+ function chatUnitUsd(pricing, inTok, outTok) {
159
+ if (!pricing) return null;
160
+ const pin = _num(pricing.prompt), pout = _num(pricing.completion); // $ per 1M tokens
161
+ if (pin == null && pout == null) return null; // omni "varies_by_modality" → unknown
162
+ return ((pin || 0) * (inTok ?? EST.llmInTokens) + (pout || 0) * (outTok ?? EST.llmOutTokens)) / 1e6;
163
+ }
164
+ function audioUnitUsd(pricing, chars, seconds) {
165
+ const p = pricing || {}, c = chars ?? EST.ttsChars;
166
+ const secs = (seconds != null && isFinite(seconds)) ? seconds : EST.audioSeconds;
167
+ if (p.per_thousand_chars != null) { let cost = _num(p.per_thousand_chars) * (c / 1000); if (p.per_generation != null) cost += _num(p.per_generation); return Math.max(cost, _num(p.minimum) || 0); }
168
+ if (p.per_prompt_char_block != null) { const bs = _num(p.prompt_char_block_size) || 1; return Math.max(Math.ceil(c / bs) * _num(p.per_prompt_char_block), _num(p.minimum) || 0); }
169
+ if (p.per_generation != null) return _num(p.per_generation);
170
+ if (p.per_second != null) return Math.max(_num(p.per_second) * secs, _num(p.minimum) || 0);
171
+ if (p.per_minute != null) return _num(p.per_minute) * EST.sttMinutes;
172
+ return null;
173
+ }
174
+ // Seconds a per_second audio model is billed for: the node's duration clamped to
175
+ // the model's advertised min/max_duration. null → audioUnitUsd uses EST.audioSeconds.
176
+ function audioBilledSeconds(sp, fields) {
177
+ const p = sp || {};
178
+ if (p.min_duration == null || p.max_duration == null) return null;
179
+ const d = parseFloat(fields && fields.duration);
180
+ if (!isFinite(d)) return null;
181
+ const lo = _num(p.min_duration), hi = _num(p.max_duration);
182
+ return Math.min(hi != null ? hi : d, Math.max(lo != null ? lo : d, d));
183
+ }
184
+ function imageUsd(pricing, size, count) {
185
+ const per = (pricing && pricing.per_image) || {};
186
+ const v = per[size] ?? per.square ?? per.square_hd ?? Object.values(per)[0];
187
+ return (v != null && isFinite(+v)) ? +v * (count || 1) : null;
188
+ }
189
+
190
+ /** Which pricing kinds a graph touches — so a caller fetches only the catalogs it needs. */
191
+ export function graphModelKinds(graph) {
192
+ const kinds = new Set();
193
+ for (const n of (graph && graph.nodes) || []) { const k = PRICE_KIND[n.type]; if (k) kinds.add(k); }
194
+ return kinds;
195
+ }
196
+
197
+ // One node's USD/run from its chosen model + settings. `catItem` is the raw
198
+ // catalog entry for node.fields.model (null → unpriceable). `graph` is needed
199
+ // only to detect wired reference-image ports (video ref tier).
200
+ function nodeUnitUsd(node, catItem, graph) {
201
+ const kind = PRICE_KIND[node.type]; if (!kind) return null;
202
+ const id = node.fields && node.fields.model; if (!id) return null;
203
+ const m = catItem; if (!m) return null;
204
+ const pricing = m.pricing, f = node.fields || {};
205
+ if (kind === "image") {
206
+ const sp = m.supported_parameters || {}, maxOut = sp.max_output_images || 1, fixed = sp.fixed_image_count || 0;
207
+ const count = fixed > 0 ? fixed : Math.min(maxOut, Math.max(1, parseInt(f.variations, 10) || 1));
208
+ return imageUsd(pricing, f.size, count);
209
+ }
210
+ if (kind === "video") {
211
+ const sp = m.supported_parameters || {}, pp = sp.parameters || sp;
212
+ const modelHasRefs = ["reference_images", "reference_image_urls", "referenceImages"].some((k) => k in pp);
213
+ const refWired = !!(modelHasRefs && graph && Array.isArray(graph.links) &&
214
+ graph.links.some((l) => l.to && l.to.node === node.id && REF_PORT_RE.test(l.to.port)));
215
+ const u = videoUnitUsd(pricing, f, refWired);
216
+ return (u != null && isFinite(u)) ? u : null;
217
+ }
218
+ if (kind === "chat") {
219
+ const inTok = Math.max(200, Math.round(((f.system || "").length + (f.prompt || "").length) / 4) + 200);
220
+ const outTok = f.maxTokens ? +f.maxTokens : EST.llmOutTokens;
221
+ return chatUnitUsd(pricing, inTok, outTok);
222
+ }
223
+ if (kind === "audio") {
224
+ const chars = node.type === "transcribe" ? undefined : ((f.prompt || "").length || EST.ttsChars);
225
+ const secs = audioBilledSeconds(m.supported_parameters, f);
226
+ const u = audioUnitUsd(pricing, chars, secs);
227
+ return (u != null && isFinite(u)) ? u : null; // one track — the sent song count is clamped to 1 at run
228
+ }
229
+ return null;
230
+ }
231
+
232
+ /**
233
+ * Forecast a graph's per-run cost from the public catalog.
234
+ *
235
+ * @param {{nodes:Array, links?:Array}} graph a materialized graph (Workflow#graph)
236
+ * @param {{chat?:Array, image?:Array, video?:Array, audio?:Array}} catalogs
237
+ * raw catalog arrays (as /api/v1/*-models return them, `.data`), indexed here by model id
238
+ * @returns {{usd:number, exact:boolean, priced:number, unpriced:number}}
239
+ * usd = sum over priced billable nodes; exact = every priced node was image
240
+ * (deterministic); priced/unpriced = billable node counts. unpriced>0 ⇒ usd is a lower bound.
241
+ */
242
+ export function estimateGraphCost(graph, catalogs = {}) {
243
+ const nodes = (graph && Array.isArray(graph.nodes)) ? graph.nodes : [];
244
+ const byKind = {};
245
+ for (const kind of ["chat", "image", "video", "audio"]) {
246
+ const arr = Array.isArray(catalogs[kind]) ? catalogs[kind] : [];
247
+ const map = new Map();
248
+ for (const m of arr) if (m && m.id != null) map.set(String(m.id), m);
249
+ byKind[kind] = map;
250
+ }
251
+ let usd = 0, priced = 0, unpriced = 0, exact = true;
252
+ for (const n of nodes) {
253
+ const kind = PRICE_KIND[n.type]; if (!kind) continue; // free/local node
254
+ const id = n.fields && n.fields.model;
255
+ const m = id != null ? byKind[kind].get(String(id)) : null;
256
+ const u = m ? nodeUnitUsd(n, m, graph) : null;
257
+ if (u == null || !isFinite(u)) { unpriced++; continue; }
258
+ usd += u; priced++;
259
+ if (kind !== "image") exact = false;
260
+ }
261
+ return { usd, exact, priced, unpriced };
262
+ }
package/src/graph.mjs CHANGED
@@ -3,7 +3,7 @@ import { NanoodleError } from "./errors.mjs";
3
3
  /* Dynamic input-port families (mirrors the nanoodle app's runGraph). A wire landing on one of
4
4
  these ports — or on a port declared in NODE_TYPES[type].inputs — is a data input; a wire
5
5
  landing on ANY other port is a field override (wired prompt/system/lyrics/q/...). */
6
- export const IMG_PORT_RE = /^img\d+$/; // llm / draw vision references
6
+ export const IMG_PORT_RE = /^img\d+$/; // llm vision references
7
7
  export const EDIT_IMG_RE = /^image\d*$/; // edit multi-reference: image, image2, ...
8
8
  export const VID_PORT_RE = /^vid\d+$/;
9
9
  export const CLIP_PORT_RE = /^clip\d+$/; // combine clips
@@ -44,7 +44,6 @@ export const NODE_TYPES = {
44
44
  join: { title: "Join", inputs: ["a", "b"], outputs: [{ name: "text", type: "text" }], local: true },
45
45
  llm: { title: "LLM", inputs: [], outputs: [{ name: "text", type: "text" }], network: true },
46
46
  image: { title: "Image", inputs: [], outputs: [{ name: "image", type: "image" }], network: true },
47
- draw: { title: "Draw", inputs: [], outputs: [{ name: "image", type: "image" }, { name: "text", type: "text" }], network: true },
48
47
  edit: { title: "Edit", inputs: [], outputs: [{ name: "image", type: "image" }], network: true },
49
48
  inpaint: { title: "Inpaint", inputs: ["image", "mask"], outputs: [{ name: "image", type: "image" }], network: true },
50
49
  resize: { title: "Resize / crop", inputs: ["image"], outputs: [{ name: "image", type: "image" }], local: true },
package/src/index.mjs CHANGED
@@ -7,6 +7,9 @@ export {
7
7
  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
+ 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";
10
13
  export { decodeShareUrl, decodeShareFragment, isShareRef } from "./share.mjs";
11
14
  export { parseNanoInvoice } from "./x402.mjs";
12
15
  export { qrTerminal, qrModules } from "./qr.mjs";
package/src/io.mjs CHANGED
@@ -12,8 +12,6 @@ export const INPUT_SPECS = {
12
12
  llm: [{ f: "prompt", label: "Prompt", kind: "textarea" },
13
13
  { f: "system", label: "System prompt", kind: "textarea", optional: true, def: "You are a helpful, concise assistant." }],
14
14
  image: [{ f: "prompt", label: "Image prompt", kind: "textarea" }],
15
- draw: [{ f: "prompt", label: "Prompt", kind: "textarea" },
16
- { f: "system", label: "System prompt", kind: "textarea", optional: true }],
17
15
  tvideo: [{ f: "prompt", label: "Video prompt", kind: "textarea" }],
18
16
  music: [{ f: "prompt", label: "Style / prompt", kind: "textarea" }],
19
17
  remix: [{ f: "prompt", label: "Style / direction", kind: "textarea" }],
@@ -201,10 +199,6 @@ export const SETTING_SPECS = {
201
199
  { f: "size", label: "Image size", kind: "select", options: SIZES, def: "1024x1024" },
202
200
  { f: "seed", label: "Seed", kind: "number" },
203
201
  ],
204
- draw: [
205
- { f: "model", label: "Model", kind: "model" },
206
- { f: "showThinking", label: "Show thinking", kind: "boolean", def: true },
207
- ],
208
202
  tvideo: [
209
203
  { f: "model", label: "Model", kind: "model" },
210
204
  { f: "resolution", label: "Resolution", kind: "select", def: "" },
package/src/nodes.mjs CHANGED
@@ -396,7 +396,7 @@ function guardRefsSize(imgs) {
396
396
  /**
397
397
  * Per-node executors. Each: async run(node, inp, ctx) → out map keyed by output port name.
398
398
  * `node.fields` already carries wired field overrides + user inputs + settings.
399
- * ctx = { chat, chatImage, image, video, audio, transcribe, progress } (cost/poll wired by the engine).
399
+ * ctx = { chat, image, video, audio, transcribe, progress } (cost/poll wired by the engine).
400
400
  */
401
401
  export const RUNNERS = {
402
402
  async text(n) { return { text: n.fields.text || "" }; },
@@ -570,20 +570,6 @@ export const RUNNERS = {
570
570
  return { image: await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: src, extra: imgExtra(n) }) };
571
571
  },
572
572
 
573
- async draw(n, inp, ctx) {
574
- const prompt = promptOf(n, inp, "no prompt");
575
- const imgs = await Promise.all(collectPorts(inp, IMG_PORT_RE).map((u) => fitImage(u, ctx, "wired image")));
576
- guardRefsSize(imgs);
577
- const messages = chatMessages(n, prompt, imgs, null);
578
- const res = await ctx.chatImage(messages, mdl(n), {});
579
- const sel = Math.min(Math.max(0, parseInt(n.fields.sel, 10) || 0), res.images.length - 1);
580
- const showThinking = n.fields.showThinking !== false && n.fields.showThinking !== "false";
581
- const text = showThinking && res.reasoning
582
- ? "```thinking\n" + res.reasoning + "\n```\n\n" + (res.text || "")
583
- : res.text;
584
- return { image: res.images[sel], images: res.images, text };
585
- },
586
-
587
573
  async inpaint(n, inp, ctx) {
588
574
  const source = inp.image != null ? inp.image : n.fields.image;
589
575
  const rawMask = inp.mask != null ? inp.mask : n.fields.mask;
@@ -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({
@@ -259,7 +266,6 @@ export class Workflow {
259
266
  const io = { onCost, onPoll, signal: ac.signal };
260
267
  return {
261
268
  chat: (messages, model, opts) => this.client.chat(messages, model, opts, io),
262
- chatImage: (messages, model, opts) => this.client.chatImage(messages, model, opts, io),
263
269
  image: (args) => this.client.image(args, io),
264
270
  video: (model, prompt, opts, imageDataUrl) => this.client.video(model, prompt, opts, imageDataUrl, io),
265
271
  audio: (model, input, extra) => this.client.audio(model, input, extra, io),
@@ -293,7 +299,19 @@ export class Workflow {
293
299
  }
294
300
  if (upstreamFail) throw new NanoodleError("upstream failed: " + upstreamFail);
295
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;
296
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
+ }
297
315
  const t0 = Date.now();
298
316
  const out = await RUNNERS[n.type]({ ...n, fields }, inp, ctxFor(n, rec));
299
317
  throwIfAborted(ac.signal);
@@ -303,6 +321,12 @@ export class Workflow {
303
321
  emit({ type: "node-done", nodeId: n.id, name: displayName(n), ms: rec.ms, costUsd: rec.costUsd });
304
322
  return out;
305
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
+ }
306
330
  rec.status = "error";
307
331
  rec.error = e.message;
308
332
  errors.push({ nodeId: n.id, name: displayName(n), message: e.message });