nanoodle 0.11.0 → 0.13.1
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 +64 -1
- package/bin/nanoodle.mjs +14 -4
- package/package.json +2 -2
- package/src/branches.mjs +55 -0
- package/src/browser.mjs +1 -0
- package/src/client.mjs +317 -56
- package/src/decide.mjs +12 -1
- package/src/errors.mjs +1 -1
- package/src/estimate.mjs +173 -31
- package/src/graph.mjs +7 -2
- package/src/image-options.mjs +11 -0
- package/src/index.mjs +1 -0
- package/src/io.mjs +14 -13
- package/src/local-media.mjs +19 -21
- package/src/media.mjs +90 -15
- package/src/nodes.mjs +61 -21
- package/src/workflow.mjs +128 -25
- package/src/x402.mjs +9 -2
package/src/media.mjs
CHANGED
|
@@ -33,28 +33,50 @@ export function b64ImageMime(b64) {
|
|
|
33
33
|
if (b64.startsWith("iVBOR")) return "image/png";
|
|
34
34
|
if (b64.startsWith("R0lG")) return "image/gif";
|
|
35
35
|
if (b64.startsWith("UklG")) return "image/webp";
|
|
36
|
+
// SVG may start with a BOM, XML declaration, or comments. Inspect a bounded
|
|
37
|
+
// prefix as text only; no XML parsing or execution.
|
|
38
|
+
try {
|
|
39
|
+
const head = atob(b64.slice(0, 8192));
|
|
40
|
+
if (/^(?:\xef\xbb\xbf)?\s*(?:<\?xml[^>]*>\s*)?(?:<!--[\s\S]*?-->\s*)*<svg(?:\s|\/?>)/i.test(head)) return "image/svg+xml";
|
|
41
|
+
} catch { /* malformed/unknown base64 keeps the raster fallback */ }
|
|
36
42
|
return "image/png";
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
const EXT_MIME = {
|
|
40
46
|
".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif",
|
|
41
|
-
".webp": "image/webp", ".bmp": "image/bmp",
|
|
47
|
+
".webp": "image/webp", ".bmp": "image/bmp", ".svg": "image/svg+xml",
|
|
42
48
|
".mp3": "audio/mpeg", ".wav": "audio/wav", ".ogg": "audio/ogg", ".opus": "audio/ogg",
|
|
43
49
|
".aac": "audio/aac", ".flac": "audio/flac", ".m4a": "audio/mp4",
|
|
44
50
|
".mp4": "video/mp4", ".webm": "video/webm", ".mov": "video/quicktime", ".mkv": "video/x-matroska",
|
|
45
|
-
".glb": "model/gltf-binary",
|
|
51
|
+
".glb": "model/gltf-binary", ".ply": "model/ply",
|
|
46
52
|
".txt": "text/plain", ".json": "application/json",
|
|
47
53
|
};
|
|
48
54
|
|
|
49
55
|
const MIME_EXT = {
|
|
50
|
-
"image/png": "png", "image/jpeg": "jpg", "image/gif": "gif", "image/webp": "webp",
|
|
56
|
+
"image/svg+xml": "svg", "image/png": "png", "image/jpeg": "jpg", "image/gif": "gif", "image/webp": "webp",
|
|
51
57
|
"audio/mpeg": "mp3", "audio/wav": "wav", "audio/ogg": "ogg", "audio/aac": "aac",
|
|
52
58
|
"audio/flac": "flac", "audio/mp4": "m4a",
|
|
53
59
|
"video/mp4": "mp4", "video/webm": "webm", "video/quicktime": "mov",
|
|
54
|
-
"model/gltf-binary": "glb",
|
|
60
|
+
"model/gltf-binary": "glb", "model/ply": "ply", "application/ply": "ply", "application/x-ply": "ply",
|
|
55
61
|
"text/plain": "txt", "application/json": "json",
|
|
56
62
|
};
|
|
57
63
|
|
|
64
|
+
/**
|
|
65
|
+
* A URL safe to put in an error or log line: origin + path only.
|
|
66
|
+
* Presigned query strings (and any other search/fragment) are dropped, so a
|
|
67
|
+
* signature or a key that rode along in the query cannot leak via an error.
|
|
68
|
+
*/
|
|
69
|
+
export function redactUrl(url) {
|
|
70
|
+
const s = String(url || "");
|
|
71
|
+
try {
|
|
72
|
+
const u = new URL(s);
|
|
73
|
+
return u.origin + u.pathname;
|
|
74
|
+
} catch {
|
|
75
|
+
const cut = s.search(/[?#]/);
|
|
76
|
+
return (cut >= 0 ? s.slice(0, cut) : s).slice(0, 120);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
58
80
|
/** Best-effort file extension for a MIME type (used by the CLI --out saver). */
|
|
59
81
|
export function extForMime(mime) {
|
|
60
82
|
return MIME_EXT[String(mime || "").split(";")[0].trim().toLowerCase()] || "bin";
|
|
@@ -83,6 +105,7 @@ export function sniffMime(bytes) {
|
|
|
83
105
|
if (b.length >= 12 && ascii(4, "ftyp")) return "video/mp4";
|
|
84
106
|
if (b.length >= 4 && b[0] === 0x1a && b[1] === 0x45 && b[2] === 0xdf && b[3] === 0xa3) return "video/webm";
|
|
85
107
|
if (b.length >= 4 && ascii(0, "glTF")) return "model/gltf-binary";
|
|
108
|
+
if (/^ply\r?\nformat (?:ascii|binary_little_endian|binary_big_endian) 1\.0\r?\n/.test(new TextDecoder().decode(b.subarray(0, 64)))) return "model/ply";
|
|
86
109
|
return "application/octet-stream";
|
|
87
110
|
}
|
|
88
111
|
|
|
@@ -105,6 +128,47 @@ export function dataUrlBytes(url) {
|
|
|
105
128
|
return { bytes, mime };
|
|
106
129
|
}
|
|
107
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Internal hosted-media GET. Retry connection/body interruptions from scratch,
|
|
133
|
+
* never generation POSTs. Keep errors redacted even when fetch includes a signed
|
|
134
|
+
* URL in its exception. HTTP rejections and deliberate cancellation are final.
|
|
135
|
+
*/
|
|
136
|
+
export async function downloadMedia(url, { fetch: fetchFn = globalThis.fetch, signal, what = "media" } = {}) {
|
|
137
|
+
const checkAbort = () => {
|
|
138
|
+
if (signal && signal.aborted) throw signal.reason || new NanoodleError("download aborted", { code: "aborted" });
|
|
139
|
+
};
|
|
140
|
+
for (let attempt = 1; attempt <= 3; attempt++) {
|
|
141
|
+
checkAbort();
|
|
142
|
+
try {
|
|
143
|
+
if (typeof fetchFn !== "function") throw new NanoodleError("can't download media: no fetch available");
|
|
144
|
+
const r = await fetchFn(url, { signal }); // CDN: no credentials or API auth headers
|
|
145
|
+
if (!r.ok) throw new NanoodleError(`couldn't download ${what} (${r.status}): ${redactUrl(url)}`, { status: r.status });
|
|
146
|
+
const bytes = new Uint8Array(await r.arrayBuffer());
|
|
147
|
+
checkAbort();
|
|
148
|
+
return { bytes, response: r };
|
|
149
|
+
} catch (e) {
|
|
150
|
+
checkAbort();
|
|
151
|
+
if (e && (e.name === "AbortError" || e.code === "aborted")) throw e;
|
|
152
|
+
if (e instanceof NanoodleError) throw e;
|
|
153
|
+
if (attempt === 3) throw new NanoodleError(`couldn't download ${what}: ${redactUrl(url)}`, { code: "download", attempts: attempt });
|
|
154
|
+
}
|
|
155
|
+
// Short, bounded backoff; cancellation also interrupts the retry delay.
|
|
156
|
+
await new Promise((resolve, reject) => {
|
|
157
|
+
const done = () => { if (signal) signal.removeEventListener("abort", cancel); resolve(); };
|
|
158
|
+
const timer = setTimeout(done, 100 * attempt);
|
|
159
|
+
const cancel = () => {
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
signal.removeEventListener("abort", cancel);
|
|
162
|
+
reject(signal.reason || new NanoodleError("download aborted", { code: "aborted" }));
|
|
163
|
+
};
|
|
164
|
+
if (signal) {
|
|
165
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
166
|
+
if (signal.aborted) cancel();
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
108
172
|
/**
|
|
109
173
|
* A media output value: a data: or https URL plus lazy byte access.
|
|
110
174
|
* String-coerces to the URL so it drops into templates / JSON naturally.
|
|
@@ -131,22 +195,33 @@ export class MediaRef {
|
|
|
131
195
|
toString() { return this.url; }
|
|
132
196
|
toJSON() { return this.url; }
|
|
133
197
|
|
|
134
|
-
/**
|
|
135
|
-
async bytes() {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
198
|
+
/** Read the raw media bytes, optionally cancelling the download with a signal. */
|
|
199
|
+
async bytes({ signal } = {}) {
|
|
200
|
+
signal?.throwIfAborted();
|
|
201
|
+
let bytes;
|
|
202
|
+
if (this.url.startsWith("data:")) bytes = dataUrlBytes(this.url).bytes;
|
|
203
|
+
else {
|
|
204
|
+
const downloaded = await downloadMedia(this.url, { fetch: this._fetch, signal });
|
|
205
|
+
bytes = downloaded.bytes;
|
|
206
|
+
if (!this._mime) {
|
|
207
|
+
const r = downloaded.response;
|
|
208
|
+
const ct = r.headers && r.headers.get && r.headers.get("content-type");
|
|
209
|
+
if (ct) this._mime = ct.split(";")[0].trim();
|
|
210
|
+
}
|
|
142
211
|
}
|
|
143
|
-
|
|
212
|
+
// CDNs commonly serve model assets as generic binary. Sniff their actual
|
|
213
|
+
// format before the CLI or save() chooses a filename extension.
|
|
214
|
+
if (!this._mime || /^(?:application|binary)\/octet-stream$/i.test(this._mime)) this._mime = sniffMime(bytes);
|
|
215
|
+
return bytes;
|
|
144
216
|
}
|
|
145
217
|
|
|
146
|
-
/** Write
|
|
218
|
+
/** Write bytes (Node only); append a media extension if absent, and return the saved path. */
|
|
147
219
|
async save(path) {
|
|
148
220
|
const { writeFile } = await import("node:fs/promises");
|
|
149
|
-
|
|
221
|
+
const { extname } = await import("node:path");
|
|
222
|
+
const bytes = await this.bytes();
|
|
223
|
+
if (typeof path === "string" && !extname(path)) path += "." + extForMime(this.mime);
|
|
224
|
+
await writeFile(path, bytes);
|
|
150
225
|
return path;
|
|
151
226
|
}
|
|
152
227
|
}
|
package/src/nodes.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { imageModelOpts } from "./image-options.mjs";
|
|
1
2
|
import { NanoodleError } from "./errors.mjs";
|
|
2
3
|
import { catItem, chatModelCan, pricingAdvertisesRefs } from "./catalog.mjs";
|
|
3
4
|
import { IMG_PORT_RE, EDIT_IMG_RE, REF_PORT_RE, CLIP_PORT_RE, VID_PORT_RE, optionalNode } from "./graph.mjs";
|
|
@@ -128,7 +129,10 @@ function audioInputPart(url) {
|
|
|
128
129
|
function llmOpts(n) {
|
|
129
130
|
const f = n.fields, o = {};
|
|
130
131
|
if (f.temperature != null && f.temperature !== "") o.temperature = +f.temperature;
|
|
131
|
-
|
|
132
|
+
// maxTokens is opt-in (D1 ruling, 2026-11-18): absent by default, honored
|
|
133
|
+
// exactly when the node sets it — twin of nanoodle-py _run_llm (truthy +
|
|
134
|
+
// numeric → int(float(v)); non-numeric degrades gracefully, sends nothing).
|
|
135
|
+
if (f.maxTokens && Number.isFinite(+f.maxTokens)) o.max_tokens = Math.trunc(+f.maxTokens);
|
|
132
136
|
if (f.format === "JSON") o.response_format = { type: "json_object" };
|
|
133
137
|
if (f.reasoningEffort && f.reasoningEffort !== "default") o.reasoning_effort = f.reasoningEffort;
|
|
134
138
|
if (f.showThinking === true || f.showThinking === "true") o.showThinking = true;
|
|
@@ -164,6 +168,7 @@ function normalizeLoraUrl(raw) {
|
|
|
164
168
|
function loraFamily(model) {
|
|
165
169
|
const m = String(model || "");
|
|
166
170
|
if (/spicy/i.test(m)) return null;
|
|
171
|
+
if (/p-h3-image/i.test(m)) return "ph3"; // wavespeed p-h3-image: lora_url_1..3 (ids lack "lora")
|
|
167
172
|
if (/p-image/i.test(m)) return "pimage";
|
|
168
173
|
if (/klein/i.test(m)) return "flux2klein";
|
|
169
174
|
if (/flux-2/i.test(m)) return "flux2dev";
|
|
@@ -181,10 +186,13 @@ function loraKind(type) { return (type === "image" || type === "edit" || type ==
|
|
|
181
186
|
function imageTakesLora(id) {
|
|
182
187
|
id = String(id || "");
|
|
183
188
|
if (/inpaint/i.test(id)) return false;
|
|
184
|
-
// flux-2-klein-*
|
|
185
|
-
//
|
|
189
|
+
// twin of the app's imageTakesLora (nanoodle #732, #764): flux-2-klein-* is lora-capable;
|
|
190
|
+
// FLUX.3 Klein text-to-image (base + distilled) gained lora_url_1..3 in the catalog on
|
|
191
|
+
// 2026-10-27, but FLUX.3 Klein edit-image still takes no lora_url
|
|
186
192
|
if (/flux-2-klein/i.test(id)) return true;
|
|
193
|
+
if (/flux-3\/klein\/(base\/)?text-to-image/i.test(id)) return true;
|
|
187
194
|
if (/minimax-h3\/(text-to-image|image-edit)/i.test(id)) return true;
|
|
195
|
+
if (/p-h3-image/i.test(id)) return true; // Pruna P-H3 Image (2026-11-05): lora_url_1..3
|
|
188
196
|
return /(^|[-\/])lora($|[-\/])/i.test(id);
|
|
189
197
|
}
|
|
190
198
|
|
|
@@ -198,7 +206,7 @@ function modelTakesLora(kind, id) {
|
|
|
198
206
|
function loraCap(model) {
|
|
199
207
|
switch (loraFamily(model)) {
|
|
200
208
|
case "flux2dev": return 4;
|
|
201
|
-
case "flux2klein": case "zimage": case "ltx": case "krea": case "h3": case "anima": case "qwen21": return 3;
|
|
209
|
+
case "flux2klein": case "zimage": case "ltx": case "krea": case "h3": case "anima": case "qwen21": case "ph3": return 3;
|
|
202
210
|
default: return 1; // flux-lora, pimage — single slot
|
|
203
211
|
}
|
|
204
212
|
}
|
|
@@ -214,7 +222,7 @@ function nodeLoras(n) {
|
|
|
214
222
|
function loraBodyFor(model, items) {
|
|
215
223
|
const fam = loraFamily(model), sc = (v) => (isNaN(v) ? 1 : v);
|
|
216
224
|
if (fam === "pimage") return { lora_weights: items[0].url, lora_scale: sc(items[0].scale) };
|
|
217
|
-
if (fam === "flux2dev" || fam === "flux2klein" || fam === "zimage" || fam === "ltx" || fam === "krea" || fam === "h3" || fam === "anima" || fam === "qwen21") {
|
|
225
|
+
if (fam === "flux2dev" || fam === "flux2klein" || fam === "zimage" || fam === "ltx" || fam === "krea" || fam === "h3" || fam === "anima" || fam === "qwen21" || fam === "ph3") {
|
|
218
226
|
const b = {};
|
|
219
227
|
items.forEach((it, i) => { b["lora_url_" + (i + 1)] = it.url; b["lora_scale_" + (i + 1)] = sc(it.scale); });
|
|
220
228
|
return b;
|
|
@@ -268,7 +276,7 @@ function airModelTakesNegative(model) {
|
|
|
268
276
|
|
|
269
277
|
/** Per-call image extras: LoRA params + fixed seed (when numeric) + custom-civitai AIR. */
|
|
270
278
|
function imgExtra(n) {
|
|
271
|
-
const e = loraParams(n);
|
|
279
|
+
const e = { ...imageModelOpts(n.fields.modelOpts), ...loraParams(n) };
|
|
272
280
|
const s = n.fields.seed;
|
|
273
281
|
if (s != null && String(s).trim() !== "" && !isNaN(Number(s))) e.seed = Number(s);
|
|
274
282
|
if (n.fields.model === "custom-civitai") {
|
|
@@ -333,6 +341,7 @@ function refMaxFor(model, pricing) {
|
|
|
333
341
|
const id = String(model || "");
|
|
334
342
|
if (/seedance/i.test(id)) return 9;
|
|
335
343
|
if (/minimax-h3/i.test(id)) return 9; // upstream cap; the first 5 are included, 6-9 bill extra_reference_image each
|
|
344
|
+
if (/gemini-omni-flash\/v1\.2/i.test(id)) return 6;
|
|
336
345
|
if (/luma|ray/i.test(id)) return 4;
|
|
337
346
|
const inc = pricing && +pricing.included_reference_images;
|
|
338
347
|
if (inc > 0) return inc; // no family entry: the included count is the only number the catalog gives us
|
|
@@ -349,8 +358,12 @@ function modelRefSpec(model, ctx) {
|
|
|
349
358
|
const m = catItem(ctx && ctx.catalog, "video", model);
|
|
350
359
|
if (!m) return { key: "reference_images", cap: refMaxFor(model) };
|
|
351
360
|
const sp = m.supported_parameters || {}, pp = sp.parameters || sp;
|
|
352
|
-
const
|
|
353
|
-
|
|
361
|
+
const modeOptions = pp.mode && pp.mode.options;
|
|
362
|
+
const referenceMode = Array.isArray(modeOptions) && modeOptions.some((o) =>
|
|
363
|
+
o && /^reference\b/i.test(String(o.value != null ? o.value : "")));
|
|
364
|
+
const key = keys.find((k) => k in pp) ||
|
|
365
|
+
(referenceMode || pricingAdvertisesRefs(m.pricing) ? "reference_images" : null);
|
|
366
|
+
if (!key) return null; // known model with no ref-image param, reference mode, or ref pricing
|
|
354
367
|
const d = pp[key];
|
|
355
368
|
let cap = null;
|
|
356
369
|
if (d && typeof d === "object") {
|
|
@@ -675,7 +688,8 @@ export const RUNNERS = {
|
|
|
675
688
|
}
|
|
676
689
|
}
|
|
677
690
|
const prompt = promptOf(n, inp);
|
|
678
|
-
if (!prompt && !/upscal/i.test(n.fields.model || "")
|
|
691
|
+
if (!prompt && !/upscal/i.test(n.fields.model || "")
|
|
692
|
+
&& !(n.fields.model === "ideogram/vector" && n.fields.modelOpts?.vector_operation === "convert")) throw new NanoodleError("no edit instruction");
|
|
679
693
|
imgs = await Promise.all(imgs.map((u) => fitImage(u, ctx, "source image")));
|
|
680
694
|
guardRefsSize(imgs);
|
|
681
695
|
const src = imgs.length > 1 ? imgs : imgs[0]; // array → multi-image composite; string → single edit
|
|
@@ -717,7 +731,12 @@ export const RUNNERS = {
|
|
|
717
731
|
const prompt = promptOf(n, inp);
|
|
718
732
|
const opts = { ...videoSourceOpts(inp.video), ...videoDims(n, ctx), lora: loraParams(n), extra: n.fields.modelOpts || {} };
|
|
719
733
|
applyRefs(opts, collectPorts(inp, REF_PORT_RE), n, ctx); // ref wires (seedance video-edit family) — same key/cap resolution as tvideo
|
|
720
|
-
|
|
734
|
+
// Character image (image+video->video motion-control / replace models, e.g. Wan 2.2 Animate):
|
|
735
|
+
// the wired still rides as imageDataUrl next to the driver video. Gated on the catalog so a
|
|
736
|
+
// stale wire left after a swap to a plain v2v model is never sent; catalog miss → send (the
|
|
737
|
+
// wire was authored deliberately, same permissive-ON stance as ivideo's endframe).
|
|
738
|
+
const image = inp.image && vEditTakesImage(ctx && ctx.catalog, n.fields.model) ? await fitImage(inp.image, ctx, "character image") : null;
|
|
739
|
+
return { video: await ctx.video(mdl(n), prompt, opts, image) };
|
|
721
740
|
},
|
|
722
741
|
|
|
723
742
|
async lipsync(n, inp, ctx) {
|
|
@@ -765,7 +784,7 @@ export const RUNNERS = {
|
|
|
765
784
|
} catch (e) {
|
|
766
785
|
if (attempt > 0) throw e;
|
|
767
786
|
const msg = (e && e.message) || "";
|
|
768
|
-
if (/^video failed:/i.test(msg)) throw e; // post-submit poll failure: already charged — no second job
|
|
787
|
+
if ((e && e.runId != null) || /^video failed:/i.test(msg)) throw e; // post-submit poll failure: already charged — no second job
|
|
769
788
|
const cap = /up to\s+(\d+(?:\.\d+)?)\s*second/i.exec(msg);
|
|
770
789
|
if (cap && /INVALID_AUDIO_DURATION|audio.{0,15}duration/i.test(msg)) {
|
|
771
790
|
trimSec = Math.min(60, Math.max(1, parseFloat(cap[1]) - 0.1)); // the model told us its real cap
|
|
@@ -814,24 +833,33 @@ export const RUNNERS = {
|
|
|
814
833
|
return cleanVoiceRun(model, inp, n.fields, (m, extra) => ctx.audio(m, "", extra));
|
|
815
834
|
},
|
|
816
835
|
|
|
817
|
-
// 🧊 3D model: image and/or text →
|
|
836
|
+
// 🧊 3D model: image and/or text → model asset via the video submit/poll API.
|
|
837
|
+
// Mesh-processing models (e.g. Hyper3D Rigging) instead take an existing
|
|
838
|
+
// model on the `model` wire and receive it as the `model_url` parameter.
|
|
818
839
|
async model3d(n, inp, ctx) {
|
|
819
840
|
const model = String((n.fields && n.fields.model) || "").trim() || MODEL3D_IMAGE_DEFAULT;
|
|
820
841
|
const mods = model3dInputMods(ctx && ctx.catalog, model);
|
|
821
842
|
const prompt = promptOf(n, inp);
|
|
822
843
|
let image = mods.image ? (inp.image || "") : "";
|
|
844
|
+
const mesh = mods.mesh ? (inp.model || "") : "";
|
|
823
845
|
if (inp.image && !mods.image && ctx && ctx.progress) {
|
|
824
846
|
ctx.progress("This model takes text only — the image wire stays, but this run ignores it.");
|
|
825
847
|
}
|
|
848
|
+
if (inp.model && !mods.mesh && ctx && ctx.progress) {
|
|
849
|
+
ctx.progress("This model generates from scratch — the model wire stays, but this run ignores it.");
|
|
850
|
+
}
|
|
826
851
|
if (prompt && !mods.text && ctx && ctx.progress) {
|
|
827
852
|
ctx.progress("This model takes an image only — the prompt stays, but this run ignores it.");
|
|
828
853
|
}
|
|
829
|
-
if (mods.
|
|
830
|
-
if (mods.
|
|
831
|
-
if (mods.
|
|
854
|
+
if (mods.mesh && !mesh) throw new NanoodleError("Connect a model first.");
|
|
855
|
+
if (!mods.mesh && mods.image && !mods.text && !image) throw new NanoodleError("Connect an image first.");
|
|
856
|
+
if (!mods.mesh && mods.text && !mods.image && !prompt) throw new NanoodleError("Add a prompt first.");
|
|
857
|
+
if (!mods.mesh && mods.image && mods.text && !image && !prompt) throw new NanoodleError("Add a prompt or connect an image first.");
|
|
832
858
|
if (image) image = await fitImage(image, ctx, "source image");
|
|
859
|
+
const extra = { ...(n.fields.modelOpts || {}) };
|
|
860
|
+
if (mesh) extra.model_url = mesh; // the wire wins over a static modelOpts copy
|
|
833
861
|
const url = await ctx.video(model, mods.text ? prompt : "", {
|
|
834
|
-
extra
|
|
862
|
+
extra,
|
|
835
863
|
mediaKind: "model3d",
|
|
836
864
|
}, image || null);
|
|
837
865
|
return { model: url };
|
|
@@ -849,6 +877,18 @@ export const RUNNERS = {
|
|
|
849
877
|
* HeyGen Avatar V). Catalog miss → true (the long-standing portrait requirement).
|
|
850
878
|
* Mirrors play's built-in lipsync runner: raw `capabilities.image_to_video` or a normalized `i2v`.
|
|
851
879
|
*/
|
|
880
|
+
// Video-edit models that take a character/reference still AND a driver video but no text prompt
|
|
881
|
+
// (architecture image+video->video: Wan 2.2 Animate replace/animate, Kling / PixVerse motion
|
|
882
|
+
// control, P-Video Animate). Text+image+video "unified" models are excluded: for them an image
|
|
883
|
+
// usually switches the job to image→video and drops the source clip. Catalog miss → true.
|
|
884
|
+
export function vEditTakesImage(catalog, model) {
|
|
885
|
+
const m = catItem(catalog, "video", model);
|
|
886
|
+
if (!m) return true;
|
|
887
|
+
const a = m.architecture || {};
|
|
888
|
+
const mods = Array.isArray(a.input_modalities) ? a.input_modalities : String(a.modality || "").split("->")[0].split("+");
|
|
889
|
+
return mods.includes("image") && mods.includes("video") && !mods.includes("text");
|
|
890
|
+
}
|
|
891
|
+
|
|
852
892
|
function avatarNeedsPortrait(catalog, model) {
|
|
853
893
|
const m = catItem(catalog, "video", model);
|
|
854
894
|
return !m || !!((m.capabilities || {}).image_to_video) || !!m.i2v;
|
|
@@ -866,10 +906,10 @@ function model3dInputMods(catalog, id) {
|
|
|
866
906
|
|| (it.architecture && Array.isArray(it.architecture.input_modalities) && it.architecture.input_modalities)
|
|
867
907
|
);
|
|
868
908
|
if (raw && raw.length) {
|
|
869
|
-
const mods = raw.filter((x) => x === "image" || x === "text");
|
|
870
|
-
if (mods.length) return { image: mods.includes("image"), text: mods.includes("text") };
|
|
909
|
+
const mods = raw.filter((x) => x === "image" || x === "text" || x === "3d");
|
|
910
|
+
if (mods.length) return { image: mods.includes("image"), text: mods.includes("text"), mesh: mods.includes("3d") };
|
|
871
911
|
}
|
|
872
|
-
if (id === MODEL3D_IMAGE_DEFAULT) return { image: true, text: false };
|
|
873
|
-
if (id === MODEL3D_TEXT_DEFAULT) return { image: true, text: true };
|
|
874
|
-
return { image: true, text: true };
|
|
912
|
+
if (id === MODEL3D_IMAGE_DEFAULT) return { image: true, text: false, mesh: false };
|
|
913
|
+
if (id === MODEL3D_TEXT_DEFAULT) return { image: true, text: true, mesh: false };
|
|
914
|
+
return { image: true, text: true, mesh: false };
|
|
875
915
|
}
|
package/src/workflow.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { NanoodleError, RunError, UnsupportedNodeError } from "./errors.mjs";
|
|
2
2
|
import { NODE_TYPES, displayName, isInputPort, lockedNode, materialize, topoSort, wiredFramesFloor, MAX_FRAMES } from "./graph.mjs";
|
|
3
|
+
import { endpointOutPort } from "./endpoint.mjs";
|
|
3
4
|
import { deriveInputs, deriveOutputs, deriveSettings, resolveInputKey, resolveSettingKey } from "./io.mjs";
|
|
4
5
|
import { NanoClient } from "./client.mjs";
|
|
5
6
|
import { MediaRef, coerceMediaInput } from "./media.mjs";
|
|
6
7
|
import { RUNNERS } from "./nodes.mjs";
|
|
7
8
|
import { learnPromptCap, withFittedPrompt } from "./prompt-caps.mjs";
|
|
8
9
|
import { decodeShareUrl, isShareRef } from "./share.mjs";
|
|
10
|
+
import { isBranchDecider, planBranches } from "./branches.mjs";
|
|
9
11
|
|
|
10
12
|
/** Env / process access — safe when `process` is missing (browsers, some workers). */
|
|
11
13
|
function envApiKey() {
|
|
@@ -27,6 +29,68 @@ function warnGraph(msg) {
|
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
const MEDIA_KINDS = new Set(["image", "audio", "video", "inpaint"]);
|
|
32
|
+
/** Ports whose value is media a later node will fetch or inline. */
|
|
33
|
+
const HANDED_MEDIA_PORT = /^(?:video|audio|image|mask|endframe|model)$|^(?:img|image|vid|clip|ref|frame)\d+$/;
|
|
34
|
+
const MEDIA_OUT_TYPES = new Set(["image", "audio", "video", "model3d"]);
|
|
35
|
+
|
|
36
|
+
function mediaOutputKeys(node, out) {
|
|
37
|
+
const keys = [];
|
|
38
|
+
const add = (k) => { if (k && !keys.includes(k)) keys.push(k); };
|
|
39
|
+
if (!out || typeof out !== "object") return keys;
|
|
40
|
+
if (node.type === "endpoint") {
|
|
41
|
+
const p = endpointOutPort(node);
|
|
42
|
+
if (p && MEDIA_OUT_TYPES.has(p.type)) add(p.name);
|
|
43
|
+
} else if (node.type === "vframes") {
|
|
44
|
+
for (const k of Object.keys(out)) if (/^frame\d+$/.test(k)) add(k);
|
|
45
|
+
} else {
|
|
46
|
+
const spec = NODE_TYPES[node.type];
|
|
47
|
+
for (const p of (spec && spec.outputs) || []) {
|
|
48
|
+
if (MEDIA_OUT_TYPES.has(p.type)) add(p.name);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (Array.isArray(out.images)) add("images");
|
|
52
|
+
return keys;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Resolve provider-relative content URLs on a node's media outputs before they
|
|
57
|
+
* are stored or wired onward. One fetch per distinct URL (image + images[]).
|
|
58
|
+
*/
|
|
59
|
+
async function publishNodeMedia(client, node, out, signal) {
|
|
60
|
+
if (!out || typeof out !== "object" || Array.isArray(out)) return out;
|
|
61
|
+
const keys = mediaOutputKeys(node, out);
|
|
62
|
+
if (!keys.length) return out;
|
|
63
|
+
const cache = new Map();
|
|
64
|
+
const resolve = async (value) => {
|
|
65
|
+
if (typeof value === "string") {
|
|
66
|
+
if (!value) return value;
|
|
67
|
+
if (cache.has(value)) return cache.get(value);
|
|
68
|
+
const resolved = await client.resolveContentUrl(value, { signal });
|
|
69
|
+
cache.set(value, resolved);
|
|
70
|
+
return resolved;
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(value)) {
|
|
73
|
+
let changed = false;
|
|
74
|
+
const next = [];
|
|
75
|
+
for (const item of value) {
|
|
76
|
+
const r = await resolve(item);
|
|
77
|
+
if (r !== item) changed = true;
|
|
78
|
+
next.push(r);
|
|
79
|
+
}
|
|
80
|
+
return changed ? next : value;
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
};
|
|
84
|
+
let copy = null;
|
|
85
|
+
for (const k of keys) {
|
|
86
|
+
const resolved = await resolve(out[k]);
|
|
87
|
+
if (resolved !== out[k]) {
|
|
88
|
+
if (!copy) copy = { ...out };
|
|
89
|
+
copy[k] = resolved;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return copy || out;
|
|
93
|
+
}
|
|
30
94
|
|
|
31
95
|
function abortReason(signal) {
|
|
32
96
|
const r = signal && signal.reason;
|
|
@@ -38,6 +102,18 @@ function throwIfAborted(signal) {
|
|
|
38
102
|
if (signal && signal.aborted) throw abortReason(signal);
|
|
39
103
|
}
|
|
40
104
|
|
|
105
|
+
/** Bound an async stage even when injected I/O ignores AbortSignal. Always observe late rejection. */
|
|
106
|
+
function withSignal(work, signal) {
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
const onAbort = () => reject(abortReason(signal));
|
|
109
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
110
|
+
Promise.resolve().then(() => {
|
|
111
|
+
throwIfAborted(signal);
|
|
112
|
+
return work();
|
|
113
|
+
}).then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort));
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
41
117
|
/** The outcome of Workflow.run(). Media values are MediaRef; text values plain strings. */
|
|
42
118
|
/** Internal: thrown by a node skipped behind a closed gate, so ITS dependents skip too. Never surfaced. */
|
|
43
119
|
class GateSkip extends Error {
|
|
@@ -49,7 +125,9 @@ export class RunResult {
|
|
|
49
125
|
/** { [friendlyKey | nodeId]: value } — sink node primary outputs (a sink skipped by a closed gate has none) */
|
|
50
126
|
this.outputs = outputs;
|
|
51
127
|
/**
|
|
52
|
-
* per-node { status: "done"|"error"|"gated"|"skipped", out, error, costUsd, ms }.
|
|
128
|
+
* per-node { status: "done"|"error"|"gated"|"skipped", out, error, costUsd, ms, runId }.
|
|
129
|
+
* runId: the provider's async video/3D/audio job ID, or null when none was returned.
|
|
130
|
+
* Retained on failure/timeout too, so an accepted job can be traced without resubmitting.
|
|
53
131
|
* "gated": a ⚖️ Decide yes/no gate that answered no — it ran and billed, `out` holds its answer
|
|
54
132
|
* ({ text: "no", decision }), and it carries `gate: { yes, message }`. Not an error.
|
|
55
133
|
* "skipped" + `gatedBy: <decide node id>`: downstream of a closed gate — never ran, never billed.
|
|
@@ -215,6 +293,7 @@ export class Workflow {
|
|
|
215
293
|
}
|
|
216
294
|
|
|
217
295
|
const order = topoSort(graph); // throws naming cyclic nodes
|
|
296
|
+
const branches = planBranches(graph, order.map(n => n.id));
|
|
218
297
|
|
|
219
298
|
// Local-only graphs never POST media — skip the ~4 MB inline cap on inputs.
|
|
220
299
|
// Mixed/network graphs keep the cap so we fail before spending on an oversize body.
|
|
@@ -273,7 +352,7 @@ export class Workflow {
|
|
|
273
352
|
}
|
|
274
353
|
|
|
275
354
|
const emit = (evt) => { if (onProgress) { try { onProgress(evt); } catch { /* listener errors never kill the run */ } } };
|
|
276
|
-
const nodesRec =
|
|
355
|
+
const nodesRec = Object.fromEntries(graph.nodes.map(n => [n.id, null]));
|
|
277
356
|
const errors = [];
|
|
278
357
|
const cost = { total: 0, exact: true, balance: null };
|
|
279
358
|
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
@@ -292,7 +371,14 @@ export class Workflow {
|
|
|
292
371
|
if (c.balance != null) cost.balance = c.balance;
|
|
293
372
|
};
|
|
294
373
|
const onPoll = (info) => frozen(rec) ? undefined : emit({ type: "poll", nodeId: node.id, name: displayName(node), ...info });
|
|
295
|
-
const
|
|
374
|
+
const onRunId = (job) => {
|
|
375
|
+
if (frozen(rec)) return;
|
|
376
|
+
const runId = job && typeof job === "object" ? (job.runId || job.id) : job;
|
|
377
|
+
if (runId == null) return;
|
|
378
|
+
rec.runId = runId;
|
|
379
|
+
emit({ type: "node-run-id", nodeId: node.id, name: displayName(node), runId });
|
|
380
|
+
};
|
|
381
|
+
const io = { onCost, onPoll, onRunId, signal: nodeSignal };
|
|
296
382
|
return {
|
|
297
383
|
chat: (messages, model, opts) => this.client.chat(messages, model, opts, io),
|
|
298
384
|
decide: (body) => this.client.decide(body, io),
|
|
@@ -323,7 +409,17 @@ export class Workflow {
|
|
|
323
409
|
let nodeTimer = null, unlinkNode = null;
|
|
324
410
|
try {
|
|
325
411
|
throwIfAborted(ac.signal);
|
|
326
|
-
|
|
412
|
+
let inbound = graph.links.filter((l) => l.to.node === n.id);
|
|
413
|
+
if (branches) {
|
|
414
|
+
await Promise.allSettled(inbound.map(l => promises.get(l.from.node)));
|
|
415
|
+
inbound = branches.activeLinks(n.id, id => nodesRec[id]?.status === "done" ? nodesRec[id].out : null);
|
|
416
|
+
if (inbound === null) {
|
|
417
|
+
rec.status = "skipped";
|
|
418
|
+
rec.skipReason = "branch";
|
|
419
|
+
emit({ type: "node-skipped", nodeId: n.id, name: displayName(n), reason: "branch" });
|
|
420
|
+
return {};
|
|
421
|
+
}
|
|
422
|
+
}
|
|
327
423
|
const inp = {};
|
|
328
424
|
let fields = effFields.get(n.id);
|
|
329
425
|
let upstreamFail = null, gatedBy = null, skippedIn = 0;
|
|
@@ -339,7 +435,14 @@ export class Workflow {
|
|
|
339
435
|
continue;
|
|
340
436
|
}
|
|
341
437
|
const v = srcOut[l.from.port];
|
|
342
|
-
if (isInputPort(n, l.to.port))
|
|
438
|
+
if (isInputPort(n, l.to.port)) {
|
|
439
|
+
// Node boundary: a relative content URL from the upstream provider is
|
|
440
|
+
// resolved (authed redirect → absolute file URL, or inlined bytes)
|
|
441
|
+
// before this node builds videoUrl / videoDataUrl / audio / image.
|
|
442
|
+
inp[l.to.port] = (typeof v === "string" && v && HANDED_MEDIA_PORT.test(l.to.port))
|
|
443
|
+
? await withSignal(() => this.client.resolveContentUrl(v, { signal: ac.signal }), ac.signal)
|
|
444
|
+
: v;
|
|
445
|
+
}
|
|
343
446
|
// Custom endpoint url/mode: a Choice path like "/post" must ride inp so
|
|
344
447
|
// endpointResolveTarget can join it onto the typed host. Overwriting
|
|
345
448
|
// fields.url with "/post" rejects the joined URL.
|
|
@@ -371,6 +474,7 @@ export class Workflow {
|
|
|
371
474
|
// prompt at the route, and in a graph the prompt is WRITTEN by an upstream LLM — so a
|
|
372
475
|
// caller has nothing to shorten. Trim it to fit rather than send a request certain to 400,
|
|
373
476
|
// and report it below. The graph's own prompts are never rewritten to avoid this.
|
|
477
|
+
if (n.type === "decide" && fields.branch !== n.fields.branch) fields = { ...fields, branch: n.fields.branch };
|
|
374
478
|
const fit = withFittedPrompt(n, fields, { catalog: this.catalog, learned: this._promptCaps });
|
|
375
479
|
fields = fit.fields;
|
|
376
480
|
rec.started = true;
|
|
@@ -381,29 +485,28 @@ export class Workflow {
|
|
|
381
485
|
warnGraph(`node ${n.id}: prompt trimmed ${fit.trimmed.from} → ${fit.trimmed.to} characters — ` +
|
|
382
486
|
`${fields.model} rejects prompts over ${fit.trimmed.cap}`);
|
|
383
487
|
}
|
|
488
|
+
throwIfAborted(ac.signal);
|
|
384
489
|
const t0 = Date.now();
|
|
385
|
-
//
|
|
386
|
-
//
|
|
490
|
+
// The node deadline covers the runner AND publication of provider media.
|
|
491
|
+
// Race every active stage with cancellation so an injected fetch cannot hold the run open.
|
|
387
492
|
const timeoutMs = Number(fields.timeoutMs);
|
|
388
|
-
let nodeSignal = ac.signal
|
|
493
|
+
let nodeSignal = ac.signal;
|
|
389
494
|
if (Number.isFinite(timeoutMs) && timeoutMs > 0) {
|
|
390
495
|
const nac = new AbortController();
|
|
391
496
|
const onRunAbort = () => nac.abort(ac.signal.reason);
|
|
392
497
|
ac.signal.addEventListener("abort", onRunAbort, { once: true });
|
|
393
498
|
unlinkNode = () => ac.signal.removeEventListener("abort", onRunAbort);
|
|
394
499
|
nodeSignal = nac.signal;
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
const err = new NanoodleError(`node timed out after ${timeoutMs}ms`, { code: "timeout" });
|
|
398
|
-
nac.abort(err);
|
|
399
|
-
reject(err);
|
|
400
|
-
}, timeoutMs);
|
|
401
|
-
});
|
|
402
|
-
deadline.catch(() => {});
|
|
500
|
+
nodeTimer = setTimeout(() => nac.abort(new NanoodleError(
|
|
501
|
+
`node timed out after ${timeoutMs}ms`, { code: "timeout" })), timeoutMs);
|
|
403
502
|
}
|
|
404
|
-
const
|
|
405
|
-
const out =
|
|
406
|
-
|
|
503
|
+
const nodeForRun = { ...n, fields };
|
|
504
|
+
const out = await withSignal(async () => {
|
|
505
|
+
const rawOut = await RUNNERS[n.type](nodeForRun, inp, ctxFor(n, rec, nodeSignal));
|
|
506
|
+
throwIfAborted(nodeSignal);
|
|
507
|
+
return publishNodeMedia(this.client, nodeForRun, rawOut, nodeSignal);
|
|
508
|
+
}, nodeSignal);
|
|
509
|
+
throwIfAborted(nodeSignal);
|
|
407
510
|
throwIfAborted(ac.signal);
|
|
408
511
|
rec.final = true;
|
|
409
512
|
rec.status = "done";
|
|
@@ -434,7 +537,7 @@ export class Workflow {
|
|
|
434
537
|
// fields.onError:"skip" — a failed (or timed-out) node is recorded and skipped; its
|
|
435
538
|
// dependents see undefined ports. A run-level abort/timeout is never skipped.
|
|
436
539
|
const onError = (effFields.get(n.id) || {}).onError;
|
|
437
|
-
if (onError === "skip" && !ac.signal.aborted) {
|
|
540
|
+
if (onError === "skip" && !isBranchDecider(n) && !ac.signal.aborted) {
|
|
438
541
|
rec.status = "skipped";
|
|
439
542
|
rec.skippedOnError = true;
|
|
440
543
|
rec.out = {};
|
|
@@ -452,8 +555,8 @@ export class Workflow {
|
|
|
452
555
|
|
|
453
556
|
try {
|
|
454
557
|
for (const n of order) {
|
|
455
|
-
if (NODE_TYPES[n.type].note) { nodesRec[n.id] = { status: "skipped", out: null, error: null, costUsd: null, ms: null }; continue; }
|
|
456
|
-
nodesRec[n.id] = { status: "pending", out: null, error: null, costUsd: null, ms: null };
|
|
558
|
+
if (NODE_TYPES[n.type].note) { nodesRec[n.id] = { status: "skipped", out: null, error: null, costUsd: null, ms: null, runId: null }; continue; }
|
|
559
|
+
nodesRec[n.id] = { status: "pending", out: null, error: null, costUsd: null, ms: null, runId: null };
|
|
457
560
|
promises.set(n.id, execNode(n)); // siblings run concurrently; a node starts when ITS deps finish
|
|
458
561
|
}
|
|
459
562
|
await Promise.allSettled([...promises.values()]);
|
|
@@ -472,15 +575,15 @@ export class Workflow {
|
|
|
472
575
|
cost.exact = false;
|
|
473
576
|
}
|
|
474
577
|
// ---- result ----
|
|
475
|
-
const
|
|
578
|
+
const outputEntries = [];
|
|
476
579
|
for (const o of this.outputs) {
|
|
477
580
|
const rec = nodesRec[o.nodeId];
|
|
478
581
|
if (!rec || !(rec.status === "done" || (rec.status === "gated" && rec.out))) continue;
|
|
479
582
|
const primary = o.ports[0];
|
|
480
583
|
const value = this._wrapValue(rec.out[primary.name], primary.type);
|
|
481
|
-
|
|
482
|
-
outputsMap[o.nodeId] = value;
|
|
584
|
+
outputEntries.push([o.key, value], [o.nodeId, value]);
|
|
483
585
|
}
|
|
586
|
+
const outputsMap = Object.fromEntries(outputEntries);
|
|
484
587
|
const result = new RunResult({
|
|
485
588
|
outputs: outputsMap,
|
|
486
589
|
nodes: nodesRec,
|
package/src/x402.mjs
CHANGED
|
@@ -71,7 +71,14 @@ export function parseNanoInvoice(body, baseUrl) {
|
|
|
71
71
|
|
|
72
72
|
const RESULT_KEYS = ["choices", "data", "output", "runId", "url", "audioUrl", "transcription", "text"];
|
|
73
73
|
|
|
74
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* Does a complete-endpoint body already carry the replayed API result?
|
|
76
|
+
* A paid decision comes back as { answers, usage }. That is success. Treating
|
|
77
|
+
* it as a bare receipt makes the client resend the original request, and the
|
|
78
|
+
* API answers 402 because the payment was already consumed.
|
|
79
|
+
*/
|
|
75
80
|
export function looksLikeResult(j) {
|
|
76
|
-
|
|
81
|
+
if (!j || typeof j !== "object") return false;
|
|
82
|
+
if (RESULT_KEYS.some((k) => j[k] != null)) return true;
|
|
83
|
+
return j.answers != null && j.usage != null && typeof j.usage === "object";
|
|
77
84
|
}
|