nanoodle 0.1.1 → 0.4.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 +103 -16
- package/bin/nanoodle.mjs +122 -39
- package/package.json +11 -1
- package/src/browser.mjs +38 -0
- package/src/catalog.mjs +22 -0
- package/src/client.mjs +116 -17
- package/src/graph.mjs +25 -9
- package/src/index.mjs +8 -2
- package/src/io.mjs +42 -2
- package/src/local-media.mjs +998 -0
- package/src/media.mjs +68 -16
- package/src/mp4cat.mjs +284 -0
- package/src/nodes.mjs +253 -37
- package/src/qr.mjs +385 -0
- package/src/share.mjs +135 -0
- package/src/workflow.mjs +116 -21
- package/src/x402.mjs +77 -0
- package/src/zlib.mjs +44 -0
- package/templates/starter-graph.json +48 -0
package/src/nodes.mjs
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { NanoodleError
|
|
2
|
-
import {
|
|
1
|
+
import { NanoodleError } from "./errors.mjs";
|
|
2
|
+
import { catItem, chatModelCan } from "./catalog.mjs";
|
|
3
|
+
import { IMG_PORT_RE, EDIT_IMG_RE, REF_PORT_RE, CLIP_PORT_RE, VID_PORT_RE } from "./graph.mjs";
|
|
3
4
|
import { MEDIA_INLINE_MAX } from "./media.mjs";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
`node "${displayName(node)}" (${node.id}): node type '${node.type}' does local media processing that requires ` +
|
|
9
|
-
"the nanoodle browser app; not supported by this library yet",
|
|
10
|
-
{ nodeId: node.id, nodeType: node.type });
|
|
11
|
-
}
|
|
5
|
+
import {
|
|
6
|
+
resizeCropImage, trimAudioToWav, extractAudioToWav,
|
|
7
|
+
extractVideoFrames, concatVideos, muxSoundtrack, maskToSource,
|
|
8
|
+
} from "./local-media.mjs";
|
|
12
9
|
|
|
13
10
|
function mdl(n) {
|
|
14
11
|
const m = String((n.fields && n.fields.model) || "").trim();
|
|
@@ -16,6 +13,15 @@ function mdl(n) {
|
|
|
16
13
|
return m; // model strings pass through VERBATIM — endpoint choice is by node TYPE
|
|
17
14
|
}
|
|
18
15
|
|
|
16
|
+
/** Local-media opts from the workflow ctx (custom fetch + AbortSignal). */
|
|
17
|
+
function mediaOpts(ctx) {
|
|
18
|
+
if (!ctx) return {};
|
|
19
|
+
return {
|
|
20
|
+
...(ctx.fetch ? { fetch: ctx.fetch } : {}),
|
|
21
|
+
...(ctx.signal ? { signal: ctx.signal } : {}),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
19
25
|
function portIdx(name) {
|
|
20
26
|
const m = /(\d+)$/.exec(name);
|
|
21
27
|
return m ? +m[1] : 1;
|
|
@@ -198,16 +204,77 @@ function imgExtra(n) {
|
|
|
198
204
|
return e;
|
|
199
205
|
}
|
|
200
206
|
|
|
201
|
-
/**
|
|
202
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Video dims. Standard wire names by default; with a catalog, the chosen model's
|
|
209
|
+
* declared param names win (aspect → aspect_ratio | orientation | resolution_ratio,
|
|
210
|
+
* duration → duration | seconds) and blank fields backfill from the catalog default —
|
|
211
|
+
* mirrors play's videoDimParams so Sora/WAN-style models honour the chosen dims.
|
|
212
|
+
*/
|
|
213
|
+
function videoDims(n, ctx) {
|
|
203
214
|
const out = {};
|
|
204
215
|
const f = n.fields;
|
|
216
|
+
const m = catItem(ctx && ctx.catalog, "video", f.model);
|
|
217
|
+
const p = (m && m.supported_parameters && m.supported_parameters.parameters) || {};
|
|
218
|
+
const aP = p.aspect_ratio || p.orientation || p.resolution_ratio;
|
|
219
|
+
const aWire = p.aspect_ratio ? "aspect_ratio" : p.orientation ? "orientation" : p.resolution_ratio ? "resolution_ratio" : "aspect_ratio";
|
|
220
|
+
const dP = p.duration || p.seconds;
|
|
221
|
+
const dWire = p.duration ? "duration" : p.seconds ? "seconds" : "duration";
|
|
222
|
+
let asp = f.aspect, dur = f.duration;
|
|
223
|
+
if ((asp == null || asp === "") && aP && aP.default != null) asp = aP.default;
|
|
224
|
+
if ((dur == null || dur === "") && dP && dP.default != null) dur = dP.default;
|
|
205
225
|
if (f.resolution != null && f.resolution !== "") out.resolution = f.resolution;
|
|
206
|
-
if (
|
|
207
|
-
if (
|
|
226
|
+
if (asp != null && asp !== "") out[aWire] = asp;
|
|
227
|
+
if (dur != null && dur !== "") out[dWire] = dur;
|
|
208
228
|
return out;
|
|
209
229
|
}
|
|
210
230
|
|
|
231
|
+
/* ---------- reference-image wire key + cap (mirrors play's modelAllowsRefs) ----------
|
|
232
|
+
Video models disagree on the ref-array param name AND its size limit; sending the wrong
|
|
233
|
+
key silently degrades to a plain video, sending too many can over-bill. Resolve the
|
|
234
|
+
model's REAL key from the catalog and clamp to its declared max. */
|
|
235
|
+
function refMaxFor(model) {
|
|
236
|
+
const id = String(model || "");
|
|
237
|
+
if (/seedance/i.test(id)) return 9;
|
|
238
|
+
if (/luma|ray/i.test(id)) return 4;
|
|
239
|
+
return 4;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* {key, cap} for the model's reference-image param, or null when the model is KNOWN
|
|
244
|
+
* not to take refs. Catalog-absent / no-catalog models honor authored wires under the
|
|
245
|
+
* most common spelling (a wrong guess degrades the render, it never double-charges).
|
|
246
|
+
*/
|
|
247
|
+
function modelRefSpec(model, ctx) {
|
|
248
|
+
const keys = ["reference_images", "reference_image_urls", "referenceImages"];
|
|
249
|
+
const m = catItem(ctx && ctx.catalog, "video", model);
|
|
250
|
+
if (!m) return { key: "reference_images", cap: refMaxFor(model) };
|
|
251
|
+
const sp = m.supported_parameters || {}, pp = sp.parameters || sp;
|
|
252
|
+
const key = keys.find((k) => k in pp);
|
|
253
|
+
if (!key) return null; // known model with no ref-image param
|
|
254
|
+
const d = pp[key];
|
|
255
|
+
let cap = null;
|
|
256
|
+
if (d && typeof d === "object") {
|
|
257
|
+
const mx = d.max != null ? d.max : d.maxItems != null ? d.maxItems : d.max_items;
|
|
258
|
+
if (mx != null && +mx > 0) cap = +mx;
|
|
259
|
+
}
|
|
260
|
+
return { key, cap: cap != null ? cap : refMaxFor(model) };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Attach wired refs to video opts under the model's real key, clamped to its cap (twin of the app runtimes: say so, never silently discard). */
|
|
264
|
+
function applyRefs(opts, refs, n, ctx) {
|
|
265
|
+
if (!refs.length) return;
|
|
266
|
+
const spec = modelRefSpec(mdl(n), ctx);
|
|
267
|
+
if (spec && spec.key) {
|
|
268
|
+
opts.refImages = refs.slice(0, spec.cap);
|
|
269
|
+
opts.refKey = spec.key;
|
|
270
|
+
if (refs.length > spec.cap && ctx && ctx.progress) {
|
|
271
|
+
ctx.progress("dropped " + (refs.length - spec.cap) + " reference image(s) over this model's limit of " + spec.cap);
|
|
272
|
+
}
|
|
273
|
+
} else if (ctx && ctx.progress) {
|
|
274
|
+
ctx.progress("reference image(s) ignored — this model doesn't support them");
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
211
278
|
function videoSourceOpts(url) {
|
|
212
279
|
return /^https?:/i.test(url) ? { videoUrl: url } : { videoDataUrl: url };
|
|
213
280
|
}
|
|
@@ -220,26 +287,35 @@ const nonEmpty = (v) => v != null && String(v).trim() !== "";
|
|
|
220
287
|
|
|
221
288
|
/**
|
|
222
289
|
* Faithful to the app's collectAudioParams: only-when-nonempty, defaults omitted,
|
|
223
|
-
* then fields.extraJson merged verbatim last.
|
|
290
|
+
* then fields.extraJson merged verbatim last. With an opt-in catalog, the cat:*
|
|
291
|
+
* applies gates match play: a param is dropped only when the chosen model is IN
|
|
292
|
+
* the catalog and doesn't advertise it (duration needs a min/max_duration range;
|
|
293
|
+
* remix duration additionally needs per-second pricing; tts voice needs a voice
|
|
294
|
+
* list). No catalog → send-everything fallback, exactly like an offline export.
|
|
224
295
|
*/
|
|
225
|
-
function audioParams(n) {
|
|
296
|
+
function audioParams(n, ctx) {
|
|
226
297
|
const f = n.fields, body = {};
|
|
227
298
|
const num = (v) => { const x = Number(v); return isNaN(x) ? null : x; };
|
|
299
|
+
const m = catItem(ctx && ctx.catalog, "audio", f.model);
|
|
300
|
+
const sp = (m && m.supported_parameters) || null;
|
|
301
|
+
const durOk = !sp || (sp.min_duration != null && sp.max_duration != null); // cat:duration
|
|
302
|
+
const secDurOk = !sp || (sp.min_duration != null && sp.max_duration != null && +((m.pricing || {}).per_second) > 0); // cat:secduration
|
|
303
|
+
const voiceOk = !sp || (Array.isArray(sp.voices) && sp.voices.length > 0); // cat:voices
|
|
228
304
|
if (n.type === "music") {
|
|
229
305
|
if (nonEmpty(f.lyrics)) body.lyrics = f.lyrics;
|
|
230
306
|
if (f.instrumental === true || f.instrumental === "true") body.instrumental = true;
|
|
231
|
-
if (nonEmpty(f.duration) && num(f.duration) != null) body.duration = num(f.duration);
|
|
307
|
+
if (nonEmpty(f.duration) && num(f.duration) != null && durOk) body.duration = num(f.duration);
|
|
232
308
|
if (nonEmpty(f.negative_prompt)) body.negative_prompt = f.negative_prompt;
|
|
233
309
|
if (nonEmpty(f.seed) && num(f.seed) != null) body.seed = num(f.seed);
|
|
234
310
|
if (nonEmpty(f.response_format) && f.response_format !== "mp3") body.response_format = f.response_format;
|
|
235
311
|
} else if (n.type === "tts") {
|
|
236
|
-
if (nonEmpty(f.voice)) body.voice = f.voice;
|
|
312
|
+
if (nonEmpty(f.voice) && voiceOk) body.voice = f.voice;
|
|
237
313
|
if (nonEmpty(f.speed) && num(f.speed) != null && num(f.speed) !== 1) body.speed = num(f.speed); // omit when 1
|
|
238
314
|
if (nonEmpty(f.instructions)) body.instructions = f.instructions;
|
|
239
315
|
if (nonEmpty(f.response_format) && f.response_format !== "mp3") body.response_format = f.response_format;
|
|
240
316
|
} else if (n.type === "remix") {
|
|
241
317
|
if (nonEmpty(f.lyrics)) body.lyrics = f.lyrics;
|
|
242
|
-
if (nonEmpty(f.duration) && num(f.duration) != null) body.duration = num(f.duration);
|
|
318
|
+
if (nonEmpty(f.duration) && num(f.duration) != null && secDurOk) body.duration = num(f.duration);
|
|
243
319
|
if (nonEmpty(f.response_format) && f.response_format !== "mp3") body.response_format = f.response_format;
|
|
244
320
|
}
|
|
245
321
|
if ((f.extraJson || "").trim()) {
|
|
@@ -315,15 +391,93 @@ export const RUNNERS = {
|
|
|
315
391
|
return { text: [inp.a, inp.b].filter((v) => v != null && v !== "").join(sep) };
|
|
316
392
|
},
|
|
317
393
|
|
|
394
|
+
// ---- local media (pure-JS first like the browser; ffmpeg soft fallback) ----
|
|
395
|
+
|
|
396
|
+
async resize(n, inp, ctx) {
|
|
397
|
+
if (!inp.image) throw new NanoodleError("no image input");
|
|
398
|
+
const media = mediaOpts(ctx);
|
|
399
|
+
return {
|
|
400
|
+
image: await resizeCropImage(inp.image, n.fields.mode || "fit", n.fields.width, n.fields.height, media),
|
|
401
|
+
};
|
|
402
|
+
},
|
|
403
|
+
|
|
404
|
+
async vframes(n, inp, ctx) {
|
|
405
|
+
if (!inp.video) throw new NanoodleError("no video input");
|
|
406
|
+
const media = mediaOpts(ctx);
|
|
407
|
+
return extractVideoFrames(inp.video, {
|
|
408
|
+
count: n.fields.frames,
|
|
409
|
+
gap: n.fields.gap,
|
|
410
|
+
dir: n.fields.dir || "end",
|
|
411
|
+
...media,
|
|
412
|
+
onProgress: ctx && ctx.progress,
|
|
413
|
+
});
|
|
414
|
+
},
|
|
415
|
+
|
|
416
|
+
async combine(n, inp, ctx) {
|
|
417
|
+
// Browser wires vid1..; some docs/saves use clip1.. — accept both, ordered by port number
|
|
418
|
+
// (not CLIP-then-VID, which reorders mixed graphs).
|
|
419
|
+
const keys = Object.keys(inp)
|
|
420
|
+
.filter((k) => CLIP_PORT_RE.test(k) || VID_PORT_RE.test(k))
|
|
421
|
+
.sort((a, b) => portIdx(a) - portIdx(b) || a.localeCompare(b));
|
|
422
|
+
const clips = [];
|
|
423
|
+
const seen = new Set();
|
|
424
|
+
for (const k of keys) {
|
|
425
|
+
const v = inp[k];
|
|
426
|
+
if (!v || seen.has(v)) continue;
|
|
427
|
+
seen.add(v);
|
|
428
|
+
clips.push(v);
|
|
429
|
+
}
|
|
430
|
+
if (clips.length < 2) throw new NanoodleError("wire at least two clips to combine");
|
|
431
|
+
const dedup = n.fields.dedup == null ? true
|
|
432
|
+
: !(n.fields.dedup === false || n.fields.dedup === "false" || n.fields.dedup === 0 || n.fields.dedup === "0");
|
|
433
|
+
const media = mediaOpts(ctx);
|
|
434
|
+
return { video: await concatVideos(clips, dedup, { ...media, onProgress: ctx && ctx.progress }) };
|
|
435
|
+
},
|
|
436
|
+
|
|
437
|
+
async soundtrack(n, inp, ctx) {
|
|
438
|
+
if (!inp.video) throw new NanoodleError("no video input");
|
|
439
|
+
if (!inp.audio) throw new NanoodleError("no audio input");
|
|
440
|
+
const loop = n.fields.loop === true || n.fields.loop === "true" || n.fields.loop === 1 || n.fields.loop === "1";
|
|
441
|
+
const media = mediaOpts(ctx);
|
|
442
|
+
return { video: await muxSoundtrack(inp.video, inp.audio, loop, { ...media, onProgress: ctx && ctx.progress }) };
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
async trim(n, inp, ctx) {
|
|
446
|
+
if (!inp.audio) throw new NanoodleError("no audio input");
|
|
447
|
+
const start = parseFloat(n.fields.start) || 0;
|
|
448
|
+
const length = parseFloat(n.fields.length);
|
|
449
|
+
return { audio: await trimAudioToWav(inp.audio, start, Number.isFinite(length) ? length : 30, 16000, mediaOpts(ctx)) };
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
async extractaudio(n, inp, ctx) {
|
|
453
|
+
if (!inp.video) throw new NanoodleError("no video input");
|
|
454
|
+
const start = parseFloat(n.fields.start) || 0;
|
|
455
|
+
const lenRaw = parseFloat(n.fields.length);
|
|
456
|
+
const length = (Number.isFinite(lenRaw) && lenRaw > 0) ? lenRaw : 0;
|
|
457
|
+
return { audio: await extractAudioToWav(inp.video, start, length, 16000, mediaOpts(ctx)) };
|
|
458
|
+
},
|
|
459
|
+
|
|
318
460
|
async llm(n, inp, ctx) {
|
|
319
461
|
const prompt = promptOf(n, inp, "no prompt");
|
|
320
462
|
const imgs = collectPorts(inp, IMG_PORT_RE);
|
|
321
463
|
// hosted audio (music/tts nodes return https CDN URLs verbatim) → download + inline as base64:
|
|
322
464
|
// the chat input_audio part carries bytes, never a URL
|
|
323
|
-
|
|
324
|
-
|
|
465
|
+
let audioPart = null;
|
|
466
|
+
if (inp.audio) {
|
|
467
|
+
// a KNOWN text-only model can't hear the (large, still-billed) input_audio part — drop it
|
|
468
|
+
// and note it; permissive for catalog-absent models (mirrors play's chatModelCan gate)
|
|
469
|
+
if (chatModelCan(ctx.catalog, mdl(n), "audio_input")) {
|
|
470
|
+
const audioSrc = /^https?:/i.test(inp.audio) ? await ctx.fetchMedia(inp.audio) : inp.audio;
|
|
471
|
+
audioPart = audioInputPart(audioSrc);
|
|
472
|
+
} else {
|
|
473
|
+
ctx.progress("audio ignored — this model is text-only");
|
|
474
|
+
}
|
|
475
|
+
}
|
|
325
476
|
const messages = chatMessages(n, prompt, imgs, audioPart);
|
|
326
|
-
|
|
477
|
+
// JSON response_format on a non-structured_output model bills but returns empty — strip it
|
|
478
|
+
const opts = llmOpts(n);
|
|
479
|
+
if (opts.response_format && !chatModelCan(ctx.catalog, mdl(n), "structured_output")) delete opts.response_format;
|
|
480
|
+
return { text: await ctx.chat(messages, mdl(n), opts) };
|
|
327
481
|
},
|
|
328
482
|
|
|
329
483
|
async vision(n, inp, ctx) {
|
|
@@ -338,15 +492,30 @@ export const RUNNERS = {
|
|
|
338
492
|
|
|
339
493
|
async image(n, inp, ctx) {
|
|
340
494
|
const prompt = promptOf(n, inp, "no prompt");
|
|
341
|
-
|
|
495
|
+
let want = Math.max(1, parseInt(n.fields.variations, 10) || 1);
|
|
496
|
+
// clamp to the model's real max output (catalog item present but silent → 1, the
|
|
497
|
+
// conservative default; absent → unclamped) so we never bill for surplus images
|
|
498
|
+
const catIt = catItem(ctx.catalog, "image", mdl(n));
|
|
499
|
+
if (catIt) want = Math.min(want, (catIt.supported_parameters && catIt.supported_parameters.max_output_images) || 1);
|
|
342
500
|
const urls = await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", extra: imgExtra(n), n: want, multi: true });
|
|
343
501
|
const sel = Math.min(Math.max(0, parseInt(n.fields.sel, 10) || 0), urls.length - 1);
|
|
344
502
|
return { image: urls[sel], images: urls };
|
|
345
503
|
},
|
|
346
504
|
|
|
347
505
|
async edit(n, inp, ctx) {
|
|
348
|
-
|
|
506
|
+
let imgs = collectPorts(inp, EDIT_IMG_RE);
|
|
349
507
|
if (!imgs.length) throw new NanoodleError("no image input");
|
|
508
|
+
// cap to the model's max_input_images (item present but silent → 1; absent → no cap):
|
|
509
|
+
// a baked graph can carry more refs than a later-swapped model composites
|
|
510
|
+
const m = catItem(ctx.catalog, "image", n.fields.model);
|
|
511
|
+
if (m) {
|
|
512
|
+
const mi = m.supported_parameters && m.supported_parameters.max_input_images;
|
|
513
|
+
const cap = mi > 0 ? mi : 1;
|
|
514
|
+
if (imgs.length > cap) {
|
|
515
|
+
ctx.progress(`dropped ${imgs.length - cap} image(s) over this model's limit`);
|
|
516
|
+
imgs = imgs.slice(0, cap);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
350
519
|
const prompt = promptOf(n, inp);
|
|
351
520
|
if (!prompt && !/upscal/i.test(n.fields.model || "")) throw new NanoodleError("no edit instruction");
|
|
352
521
|
guardRefsSize(imgs);
|
|
@@ -370,26 +539,28 @@ export const RUNNERS = {
|
|
|
370
539
|
|
|
371
540
|
async inpaint(n, inp, ctx) {
|
|
372
541
|
const source = inp.image != null ? inp.image : n.fields.image;
|
|
373
|
-
const
|
|
542
|
+
const rawMask = inp.mask != null ? inp.mask : n.fields.mask;
|
|
374
543
|
if (!source) throw new NanoodleError("no image — supply the image to repaint");
|
|
375
|
-
if (!
|
|
544
|
+
if (!rawMask) throw new NanoodleError("no mask — supply a B/W mask (white = repaint)");
|
|
376
545
|
const prompt = promptOf(n, inp, "no prompt — say what to paint into the masked area");
|
|
377
|
-
//
|
|
546
|
+
// Match play.html maskToSource: composite mask onto black at the source's pixel size.
|
|
547
|
+
// ctx.maskToSource lets a browser host inject its canvas compositor (handles JPEG/WebP
|
|
548
|
+
// sources the pure-PNG path can't, where ffmpeg isn't an option).
|
|
549
|
+
const mask = await (ctx.maskToSource || maskToSource)(rawMask, source, mediaOpts(ctx));
|
|
378
550
|
return { image: await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: source, maskDataUrl: mask, extra: imgExtra(n) }) };
|
|
379
551
|
},
|
|
380
552
|
|
|
381
553
|
async tvideo(n, inp, ctx) {
|
|
382
554
|
const prompt = promptOf(n, inp, "no prompt");
|
|
383
|
-
const opts = { ...videoDims(n), lora: loraParams(n), extra: n.fields.modelOpts || {} };
|
|
384
|
-
|
|
385
|
-
if (refs.length) { opts.refImages = refs; opts.refKey = "reference_images"; }
|
|
555
|
+
const opts = { ...videoDims(n, ctx), lora: loraParams(n), extra: n.fields.modelOpts || {} };
|
|
556
|
+
applyRefs(opts, collectPorts(inp, REF_PORT_RE), n, ctx);
|
|
386
557
|
return { video: await ctx.video(mdl(n), prompt, opts, null) };
|
|
387
558
|
},
|
|
388
559
|
|
|
389
560
|
async ivideo(n, inp, ctx) {
|
|
390
561
|
if (!inp.image) throw new NanoodleError("no image input");
|
|
391
562
|
const prompt = promptOf(n, inp);
|
|
392
|
-
const opts = { ...videoDims(n), lora: loraParams(n), extra: n.fields.modelOpts || {} };
|
|
563
|
+
const opts = { ...videoDims(n, ctx), lora: loraParams(n), extra: n.fields.modelOpts || {} };
|
|
393
564
|
if (inp.endframe) opts.last_image = inp.endframe;
|
|
394
565
|
return { video: await ctx.video(mdl(n), prompt, opts, inp.image) };
|
|
395
566
|
},
|
|
@@ -397,7 +568,8 @@ export const RUNNERS = {
|
|
|
397
568
|
async vedit(n, inp, ctx) {
|
|
398
569
|
if (!inp.video) throw new NanoodleError("no video input");
|
|
399
570
|
const prompt = promptOf(n, inp);
|
|
400
|
-
const opts = { ...videoSourceOpts(inp.video), ...videoDims(n), lora: loraParams(n), extra: n.fields.modelOpts || {} };
|
|
571
|
+
const opts = { ...videoSourceOpts(inp.video), ...videoDims(n, ctx), lora: loraParams(n), extra: n.fields.modelOpts || {} };
|
|
572
|
+
applyRefs(opts, collectPorts(inp, REF_PORT_RE), n, ctx); // ref wires (seedance video-edit family) — same key/cap resolution as tvideo
|
|
401
573
|
return { video: await ctx.video(mdl(n), prompt, opts, null) };
|
|
402
574
|
},
|
|
403
575
|
|
|
@@ -405,24 +577,68 @@ export const RUNNERS = {
|
|
|
405
577
|
if (!inp.image) throw new NanoodleError("no image input");
|
|
406
578
|
if (!inp.audio) throw new NanoodleError("no audio input");
|
|
407
579
|
const prompt = promptOf(n, inp);
|
|
408
|
-
|
|
409
|
-
|
|
580
|
+
// Avatar models cap audio length (LongCat = 30s) and the cap isn't reliably in the catalog,
|
|
581
|
+
// so submit the audio as-is first (a remote song rides full-length as a url). If the model
|
|
582
|
+
// REJECTS the submit (HTTP error — not yet charged), read its real cap from the error, trim
|
|
583
|
+
// to fit (mono WAV) and retry ONCE; an oversize local clip that can't inline trims to 15s.
|
|
584
|
+
// NEVER auto-retry after a post-submit job failure ("video failed: …"): that path already
|
|
585
|
+
// reserved credits, and a second submit would double-charge. (Twin of the app runtimes.)
|
|
586
|
+
// ctx.trimAudio lets a browser host inject its Web Audio trimmer so the retry bytes match
|
|
587
|
+
// its built-in runner exactly; default is the local-media trimmer (pure-JS WAV, ffmpeg).
|
|
588
|
+
const trim = ctx.trimAudio || ((url, start, len, rate) => trimAudioToWav(url, start, len, rate, mediaOpts(ctx)));
|
|
589
|
+
let trimSec = null;
|
|
590
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
591
|
+
let opts;
|
|
592
|
+
if (trimSec != null) {
|
|
593
|
+
if (ctx.progress) ctx.progress("trimming audio to " + Math.round(trimSec) + "s…");
|
|
594
|
+
let trimmed;
|
|
595
|
+
try { trimmed = await trim(inp.audio, 0, trimSec, 24000); }
|
|
596
|
+
catch (e) {
|
|
597
|
+
// a hosted song's CDN may refuse byte downloads (browser CORS) — surface the model's
|
|
598
|
+
// cap and the way out instead of a bare "Failed to fetch"
|
|
599
|
+
if (/^https?:/i.test(inp.audio)) {
|
|
600
|
+
throw new NanoodleError("This avatar model accepts about " + Math.round(trimSec) + "s of audio, but the source track can't be downloaded to trim (the provider's audio CDN blocks it). Shorten it at the source — e.g. set the Music node's length to " + Math.round(trimSec) + "s or less — or use a Speech node (its audio can be trimmed).");
|
|
601
|
+
}
|
|
602
|
+
throw e;
|
|
603
|
+
}
|
|
604
|
+
opts = { audioDataUrl: trimmed };
|
|
605
|
+
} else {
|
|
606
|
+
opts = audioSourceOpts(inp.audio);
|
|
607
|
+
}
|
|
608
|
+
Object.assign(opts, videoDims(n, ctx));
|
|
609
|
+
opts.extra = n.fields.modelOpts || {};
|
|
610
|
+
try {
|
|
611
|
+
return { video: await ctx.video(mdl(n), prompt, opts, inp.image) };
|
|
612
|
+
} catch (e) {
|
|
613
|
+
if (attempt > 0) throw e;
|
|
614
|
+
const msg = (e && e.message) || "";
|
|
615
|
+
if (/^video failed:/i.test(msg)) throw e; // post-submit poll failure: already charged — no second job
|
|
616
|
+
const cap = /up to\s+(\d+(?:\.\d+)?)\s*second/i.exec(msg);
|
|
617
|
+
if (cap && /INVALID_AUDIO_DURATION|audio.{0,15}duration/i.test(msg)) {
|
|
618
|
+
trimSec = Math.min(60, Math.max(1, parseFloat(cap[1]) - 0.1)); // the model told us its real cap
|
|
619
|
+
} else if (/\blarge\b|MEDIA_INLINE|~4 MB|inline/i.test(msg)) {
|
|
620
|
+
trimSec = 15; // oversize local clip → safe default (a 30s guess can re-trip a 30s-cap avatar)
|
|
621
|
+
} else if (/left.{0,6}audio|right.{0,6}audio|left and right/i.test(msg)) {
|
|
622
|
+
throw new NanoodleError("This avatar model needs two separate audio tracks (multi-speaker). Pick a single-speaker avatar model.");
|
|
623
|
+
} else throw e;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
410
626
|
},
|
|
411
627
|
|
|
412
628
|
async music(n, inp, ctx) {
|
|
413
629
|
const text = promptOf(n, inp, "no prompt — describe the track");
|
|
414
|
-
return { audio: await ctx.audio(mdl(n), text, audioParams(n)) };
|
|
630
|
+
return { audio: await ctx.audio(mdl(n), text, audioParams(n, ctx)) };
|
|
415
631
|
},
|
|
416
632
|
|
|
417
633
|
async tts(n, inp, ctx) {
|
|
418
634
|
const text = promptOf(n, inp, "no text — give the Speech node something to say");
|
|
419
|
-
return { audio: await ctx.audio(mdl(n), text, audioParams(n)) };
|
|
635
|
+
return { audio: await ctx.audio(mdl(n), text, audioParams(n, ctx)) };
|
|
420
636
|
},
|
|
421
637
|
|
|
422
638
|
async remix(n, inp, ctx) {
|
|
423
639
|
if (!inp.audio) throw new NanoodleError("no audio — wire a source track into the audio port");
|
|
424
640
|
const text = promptOf(n, inp, "no prompt — describe the cover / extension first");
|
|
425
|
-
const params = audioParams(n);
|
|
641
|
+
const params = audioParams(n, ctx);
|
|
426
642
|
// https source rides as-is (providers take hosted URLs); local data: is inlined
|
|
427
643
|
if (/^https?:/i.test(inp.audio)) params.audio = inp.audio;
|
|
428
644
|
else {
|