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