nanoodle 0.13.1 → 0.13.3

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
@@ -115,7 +115,7 @@ unrecoverable.
115
115
  ```js
116
116
  wf.inputs // [{ key: "Text", nodeId: "n1", field: "text", kind: "textarea", optional: false, def: "..." }]
117
117
  wf.outputs // [{ key: "Image", nodeId: "n3", type: "image", ports: [{ name: "image", type: "image" }] }]
118
- wf.settings // [{ key: "n3.model", kind: "model", def: "nano-banana-2-lite" }, ...]
118
+ wf.settings // [{ key: "n3.model", kind: "model", def: "nano-banana-2-fast" }, ...]
119
119
  ```
120
120
 
121
121
  Input keys are flexible (case-insensitive): the node’s custom name (`"Text"`),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nanoodle",
3
- "version": "0.13.1",
4
- "description": "Run nanoodle visual AI workflows from JavaScript — zero-dependency executor for saved noodle-graph.json files",
3
+ "version": "0.13.3",
4
+ "description": "Run nanoodle visual AI workflows from JavaScript \u2014 zero-dependency executor for saved noodle-graph.json files",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
7
7
  "exports": {
@@ -59,4 +59,4 @@
59
59
  "bugs": {
60
60
  "url": "https://github.com/nanoodlecom/nanoodle-js/issues"
61
61
  }
62
- }
62
+ }
package/src/client.mjs CHANGED
@@ -25,6 +25,16 @@ export function httpError(status, bodyText, headers) {
25
25
  return new NanoodleError(status + ": " + String(bodyText).slice(0, 160), { code: "http", status });
26
26
  }
27
27
 
28
+ async function paidResponseError(response, paymentId, stage) {
29
+ const requestId = response.headers?.get?.("x-request-id");
30
+ const reference = /^[a-z0-9][a-z0-9._:-]{0,127}$/i.test(requestId || "") ? requestId : null;
31
+ await response.text().catch(() => {});
32
+ return new NanoodleError(
33
+ `NanoGPT x402 ${stage} failed after payment was sent (HTTP ${response.status}${reference ? `, request ${reference}` : ""}). Check the payment status before paying again.`,
34
+ { code: "x402", status: response.status, paymentId, stage, requestId: reference },
35
+ );
36
+ }
37
+
28
38
  /**
29
39
  * Cost extraction (mirrors the app's costFromJson). USD priority:
30
40
  * j.cost (>0) → x_nanogpt_pricing.(costUsd|cost|amount) → metadata.cost → j.cost even when 0.
@@ -142,12 +152,7 @@ export class NanoClient {
142
152
  if (settled.response) return settled.response; // complete replayed the stored request
143
153
  if (signal && signal.aborted) throw abortError(signal.reason);
144
154
  const r2 = await this.fetch(url, { ...init, headers: { ...init.headers, "x-x402-payment-id": settled.paymentId } });
145
- if (r2.status === 402) {
146
- throw new NanoodleError(
147
- "payment " + settled.paymentId + " settled, but the API still answered 402 on retry — " +
148
- "check " + (settled.statusUrl || "the payment status") + " before paying again",
149
- { code: "x402", status: 402, paymentId: settled.paymentId });
150
- }
155
+ if (!r2.ok) throw await paidResponseError(r2, settled.paymentId, "replay");
151
156
  return r2;
152
157
  }
153
158
 
@@ -210,7 +215,7 @@ export class NanoClient {
210
215
  }
211
216
  return { paymentId: invoice.paymentId, statusUrl: invoice.statusUrl };
212
217
  }
213
- if (cr.status !== 402) throw httpError(cr.status, await cr.text(), cr.headers);
218
+ if (cr.status !== 402) throw await paidResponseError(cr, invoice.paymentId, "completion");
214
219
  await cr.text().catch(() => {}); // drain the "not verified yet" body
215
220
  if (Date.now() >= deadline) {
216
221
  throw new NanoodleError(
@@ -311,8 +316,12 @@ export class NanoClient {
311
316
  return j;
312
317
  }
313
318
 
314
- /** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s). */
315
- async image({ prompt, model, size, imageDataUrl, maskDataUrl, extra, n = 1, multi = false }, { onCost, signal } = {}) {
319
+ /** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s).
320
+ * With multi + items, returns { urls, layers } instead: layers is the per-item
321
+ * `layer` metadata aligned by index with urls (layer-decomposition models return
322
+ * { z_index, name, description, bounding_box } per item), or null when no item
323
+ * carries any. Items with neither b64_json nor url drop out of both arrays. */
324
+ async image({ prompt, model, size, imageDataUrl, maskDataUrl, extra, n = 1, multi = false, items = false }, { onCost, signal } = {}) {
316
325
  const body = { model, size: size || "1024x1024", n, response_format: "b64_json" };
317
326
  if (prompt) body.prompt = prompt; // omit when blank — upscalers run with no instruction
318
327
  if (imageDataUrl) body.imageDataUrl = imageDataUrl; // string OR array (edit multi-reference)
@@ -321,12 +330,21 @@ export class NanoClient {
321
330
  const r = await this._postJson("/v1/images/generations", body, signal);
322
331
  if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
323
332
  const j = await r.json();
324
- const urls = (j.data || [])
325
- .map((d) => (d.b64_json ? "data:" + b64ImageMime(d.b64_json) + ";base64," + d.b64_json : d.url))
333
+ const pairs = (j.data || [])
334
+ .map((d) => {
335
+ const url = d.b64_json ? "data:" + b64ImageMime(d.b64_json) + ";base64," + d.b64_json : d.url;
336
+ return url ? { url, layer: d.layer != null ? d.layer : null } : null;
337
+ })
326
338
  .filter(Boolean);
339
+ const urls = pairs.map((p) => p.url);
327
340
  if (!urls.length) throw new NanoodleError("no image in response");
328
341
  if (onCost) onCost(costWithHeaders(j, r));
329
- return multi ? urls : urls[0];
342
+ if (!multi) return urls[0];
343
+ if (items) {
344
+ const layers = pairs.map((p) => p.layer);
345
+ return { urls, layers: layers.some((l) => l != null) ? layers : null };
346
+ }
347
+ return urls;
330
348
  }
331
349
 
332
350
  /** POST /api/generate-video then poll GET /api/video/status?requestId= until COMPLETED. */
package/src/nodes.mjs CHANGED
@@ -86,14 +86,20 @@ function guardInputRoles(model, inp) {
86
86
  }
87
87
 
88
88
  /**
89
- * edit/inpaint have a single image output, but some models always bill (and return) a fixed
90
- * batch — midjourney/text-to-image and higgsfield-soul declare fixed_image_count: 4. Ask with
91
- * multi so we can SEE the extras and say we dropped them; the return shape stays one url.
89
+ * edit/inpaint surface EVERY image a model returns. `image` stays the first — the
90
+ * long-standing single-output contract, unchanged for existing graphs — `images`
91
+ * carries them all in response order (mirroring the image node's variations
92
+ * output), and when response items carry layer metadata (layer-decomposition
93
+ * models), `layers` carries it aligned by index with `images`. Fixed-batch
94
+ * models (midjourney fixed_image_count: 4) surface their variants the same way;
95
+ * nothing a model returns is dropped silently anymore.
92
96
  */
93
- function keptFirst(urls, ctx) {
94
- if (!Array.isArray(urls)) return urls; // host-injected ctx.image that ignores multi → already one url
95
- if (urls.length > 1 && ctx && ctx.progress) ctx.progress(`model returned ${urls.length} images, kept the first`);
96
- return urls[0];
97
+ function multiOut(res) {
98
+ if (typeof res === "string") return { image: res, images: [res] }; // a host-injected ctx.image that ignores multi/items → already one url
99
+ const urls = Array.isArray(res) ? res : res.urls; // …or one that honors multi but not `items` → bare urls
100
+ const out = { image: urls[0], images: urls };
101
+ if (!Array.isArray(res) && res.layers) out.layers = res.layers;
102
+ return out;
97
103
  }
98
104
 
99
105
  function promptOf(n, inp, errMsg) {
@@ -693,8 +699,7 @@ export const RUNNERS = {
693
699
  imgs = await Promise.all(imgs.map((u) => fitImage(u, ctx, "source image")));
694
700
  guardRefsSize(imgs);
695
701
  const src = imgs.length > 1 ? imgs : imgs[0]; // array → multi-image composite; string → single edit
696
- const urls = await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: src, extra: imgExtra(n), multi: true });
697
- return { image: keptFirst(urls, ctx) };
702
+ return multiOut(await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: src, extra: imgExtra(n), multi: true, items: true }));
698
703
  },
699
704
 
700
705
  async inpaint(n, inp, ctx) {
@@ -707,8 +712,7 @@ export const RUNNERS = {
707
712
  // ctx.maskToSource lets a browser host inject its canvas compositor (handles JPEG/WebP
708
713
  // sources the pure-PNG path can't, where ffmpeg isn't an option).
709
714
  const mask = await (ctx.maskToSource || maskToSource)(rawMask, source, mediaOpts(ctx));
710
- const urls = await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: source, maskDataUrl: mask, extra: imgExtra(n), multi: true });
711
- return { image: keptFirst(urls, ctx) };
715
+ return multiOut(await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: source, maskDataUrl: mask, extra: imgExtra(n), multi: true, items: true }));
712
716
  },
713
717
 
714
718
  async tvideo(n, inp, ctx) {
@@ -31,7 +31,7 @@
31
31
  "x": 650,
32
32
  "y": 100,
33
33
  "fields": {
34
- "model": "nano-banana-2-lite",
34
+ "model": "nano-banana-2-fast",
35
35
  "size": "1k",
36
36
  "variations": "1"
37
37
  },