nanoodle 0.13.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/src/client.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { imageModelOpts } from "./image-options.mjs";
2
2
  import { NanoodleError } from "./errors.mjs";
3
- import { b64ImageMime, bytesToDataUrl, dataUrlBytes, redactUrl, sniffMime, MEDIA_INLINE_MAX } from "./media.mjs";
3
+ import { b64ImageMime, bytesToDataUrl, dataUrlBytes, downloadMedia, sniffMime, MEDIA_INLINE_MAX } from "./media.mjs";
4
4
  import { assertPaymentOption, parseNanoInvoice, looksLikeResult } from "./x402.mjs";
5
5
 
6
6
  const AUDIO_MIME = { mp3: "audio/mpeg", opus: "audio/ogg", aac: "audio/aac", flac: "audio/flac", wav: "audio/wav", pcm: "audio/wav" };
@@ -10,7 +10,12 @@ const AUDIO_PROMPT_MODEL_RE = /(?:^|\/)(?:prompt-to-song|generate-bgm)$|(?:^|\/)
10
10
  const AUDIO_STYLE_MODEL_RE = /yue2-3b\/(?:text|music)-to-music$/i;
11
11
 
12
12
  /** Map an HTTP failure to an actionable error (mirrors the app's httpRunError). Never leaks the key. */
13
- export function httpError(status, bodyText) {
13
+ export function httpError(status, bodyText, headers) {
14
+ if (status === 403 && (headers?.get?.("x-vercel-mitigated") === "challenge" ||
15
+ (/^\s*<(?:!doctype\s+html\b|html\b)/i.test(bodyText) &&
16
+ /<title>\s*Vercel Security Checkpoint\s*<\/title>/i.test(bodyText)))) {
17
+ return new NanoodleError("NanoGPT's automated traffic protection challenged this request (HTTP 403).", { code: "blocked", status });
18
+ }
14
19
  if (status === 401 || status === 403) {
15
20
  return new NanoodleError(`API key rejected (HTTP ${status}) — check your NanoGPT key / NANOGPT_API_KEY`, { code: "auth", status });
16
21
  }
@@ -81,15 +86,17 @@ export function sleep(ms, signal) {
81
86
  * pollIntervals / timeouts are per-media-kind knobs (ms).
82
87
  */
83
88
  /**
84
- * GLB URL from a 3D status poll. A completed payload is 3D only when
89
+ * Model URL from a 3D status poll. A completed payload is 3D only when
85
90
  * output.kind is "3d" or output.format is "glb" — a bare video url is not a model.
86
- * Prefers model_url, then model.url, video.url, then videoUrls[0].
91
+ * Idol's model_url is only a point-cloud preview: its real Gaussian splat is
92
+ * model_mesh.url. Other models retain model_url, model.url, video.url, videoUrls[0].
87
93
  */
88
- export function model3dStatusUrl(s) {
94
+ export function model3dStatusUrl(s, model) {
89
95
  const out = (s && s.data && s.data.output) || (s && s.output) || {};
90
96
  const kind = String(out.kind || "").toLowerCase();
91
97
  const format = String(out.format || "").toLowerCase();
92
98
  if (kind !== "3d" && format !== "glb") return "";
99
+ if (model === "speridlabs/idol") return (out.model_mesh && out.model_mesh.url) || "";
93
100
  const list = Array.isArray(out.videoUrls) ? out.videoUrls : [];
94
101
  const first = list.length ? list[0] : null;
95
102
  const fromList = typeof first === "string" ? first : (first && first.url) || "";
@@ -128,10 +135,12 @@ export class NanoClient {
128
135
  * at most once; a second 402 after settling is an error, never a second send.
129
136
  */
130
137
  async _paidFetch(url, init, signal) {
138
+ if (signal && signal.aborted) throw abortError(signal.reason);
131
139
  const r = await this.fetch(url, init);
132
140
  if (r.status !== 402 || !this.payment || this.apiKey) return r;
133
141
  const settled = await this._settle402(r, signal);
134
142
  if (settled.response) return settled.response; // complete replayed the stored request
143
+ if (signal && signal.aborted) throw abortError(signal.reason);
135
144
  const r2 = await this.fetch(url, { ...init, headers: { ...init.headers, "x-x402-payment-id": settled.paymentId } });
136
145
  if (r2.status === 402) {
137
146
  throw new NanoodleError(
@@ -151,10 +160,15 @@ export class NanoClient {
151
160
  "payment required, but the 402 response offered no usable Nano option" +
152
161
  (body ? " — " + JSON.stringify(body).slice(0, 200) : ""), { code: "x402", status: 402 });
153
162
  }
163
+ if (signal && signal.aborted) throw abortError(signal.reason);
164
+ if (invoice.expiresAt && invoice.expiresAt <= Date.now()) {
165
+ throw new NanoodleError("payment window expired before payment (" + invoice.paymentId + ", to " + invoice.payTo + ") — request a fresh invoice", { code: "x402-expired", paymentId: invoice.paymentId });
166
+ }
154
167
  await this.payment(invoice); // ← the callback does the actual XNO send
155
168
  // The complete endpoint doubles as the poll: 402 = not seen on-chain yet.
156
169
  const deadline = invoice.expiresAt || Date.now() + 15 * 60 * 1000;
157
170
  while (true) {
171
+ if (signal && signal.aborted) throw abortError(signal.reason);
158
172
  const cr = await this.fetch(invoice.completeUrl, {
159
173
  method: "POST", headers: { "Content-Type": "application/json", "x-x402": "true" }, body: "{}", signal,
160
174
  });
@@ -182,7 +196,13 @@ export class NanoClient {
182
196
  { code: "x402", status: cr.status, paymentId: invoice.paymentId });
183
197
  }
184
198
  let cj = null;
185
- if (ct.includes("json")) { try { cj = await cr.json(); } catch { /* treat as settle-only */ } }
199
+ if (mime.includes("json")) { try { cj = await cr.json(); } catch { /* fail closed below */ } }
200
+ if (!cj || typeof cj !== "object") {
201
+ throw new NanoodleError(
202
+ "payment " + invoice.paymentId + " was sent, but completion returned no readable JSON receipt — check " +
203
+ (invoice.statusUrl || "the payment status") + " before paying again",
204
+ { code: "x402", paymentId: invoice.paymentId });
205
+ }
186
206
  if (looksLikeResult(cj)) {
187
207
  // wrap so call sites keep their Response contract (ok/json/text/headers)
188
208
  const response = { ok: true, status: 200, headers: cr.headers, json: async () => cj, text: async () => JSON.stringify(cj) };
@@ -190,7 +210,7 @@ export class NanoClient {
190
210
  }
191
211
  return { paymentId: invoice.paymentId, statusUrl: invoice.statusUrl };
192
212
  }
193
- if (cr.status !== 402) throw httpError(cr.status, await cr.text());
213
+ if (cr.status !== 402) throw httpError(cr.status, await cr.text(), cr.headers);
194
214
  await cr.text().catch(() => {}); // drain the "not verified yet" body
195
215
  if (Date.now() >= deadline) {
196
216
  throw new NanoodleError(
@@ -215,6 +235,35 @@ export class NanoClient {
215
235
  }, signal);
216
236
  }
217
237
 
238
+ async _pollJson(path, signal, remainingMs) {
239
+ const ac = new AbortController();
240
+ const onAbort = () => ac.abort(abortError(signal.reason));
241
+ if (signal) {
242
+ if (signal.aborted) onAbort();
243
+ else signal.addEventListener("abort", onAbort, { once: true });
244
+ }
245
+ const timer = Number.isFinite(remainingMs) ? setTimeout(() => ac.abort(
246
+ new NanoodleError("poll deadline reached", { code: "poll-timeout" })), Math.max(0, remainingMs)) : null;
247
+ let rejectAbort;
248
+ const aborted = new Promise((_, reject) => {
249
+ rejectAbort = () => reject(ac.signal.reason);
250
+ ac.signal.addEventListener("abort", rejectAbort, { once: true });
251
+ if (ac.signal.aborted) rejectAbort();
252
+ });
253
+ const work = Promise.resolve().then(async () => {
254
+ if (ac.signal.aborted) throw ac.signal.reason;
255
+ const r = await this._get(path, ac.signal);
256
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
257
+ return r.json();
258
+ });
259
+ try { return await Promise.race([work, aborted]); }
260
+ finally {
261
+ if (timer) clearTimeout(timer);
262
+ if (signal) signal.removeEventListener("abort", onAbort);
263
+ ac.signal.removeEventListener("abort", rejectAbort);
264
+ }
265
+ }
266
+
218
267
  async _get(path, signal) {
219
268
  return this.fetch(this.baseUrl + path, { headers: this._auth(), signal });
220
269
  }
@@ -222,12 +271,19 @@ export class NanoClient {
222
271
  /** POST /api/v1/chat/completions (non-streaming). Returns the assistant text. */
223
272
  async chat(messages, model, opts = {}, { onCost, signal } = {}) {
224
273
  const body = { model, messages };
225
- body.temperature = opts.temperature != null && opts.temperature !== "" ? +opts.temperature : 0.8;
226
- if (opts.max_tokens) body.max_tokens = +opts.max_tokens;
274
+ // Carbon has no sampling-temperature control; omit it rather than restoring a default.
275
+ if (model !== "google/gemini-4-carbon") body.temperature = opts.temperature != null && opts.temperature !== "" ? +opts.temperature : 0.8;
227
276
  if (opts.response_format) body.response_format = opts.response_format;
228
- if (opts.reasoning_effort) body.reasoning_effort = opts.reasoning_effort;
277
+ // Carbon's selected NanoGPT provider rejects explicit thinking controls (HTTP 400).
278
+ // Let the provider use its default rather than forwarding reasoning_effort.
279
+ if (model !== "google/gemini-4-carbon" && opts.reasoning_effort) body.reasoning_effort = opts.reasoning_effort;
280
+ // max_tokens is the one token budget forwarded, and only when a runner
281
+ // explicitly opts in via opts (the graph's maxTokens field, D1 ruling).
282
+ // The legacy spellings (max_completion_tokens, max_output_tokens,
283
+ // maxTokens) are never read here, so they can never leak into the body.
284
+ if (opts.max_tokens != null) body.max_tokens = opts.max_tokens;
229
285
  const r = await this._postJson("/api/v1/chat/completions", body, signal);
230
- if (!r.ok) throw httpError(r.status, await r.text());
286
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
231
287
  const j = await r.json();
232
288
  if (onCost) onCost(costWithHeaders(j, r));
233
289
  const msg = (j.choices && j.choices[0] && j.choices[0].message) || {};
@@ -248,7 +304,7 @@ export class NanoClient {
248
304
  */
249
305
  async decide(body, { onCost, signal } = {}) {
250
306
  const r = await this._postJson("/api/v1/decisions", body, signal);
251
- if (!r.ok) throw httpError(r.status, await r.text());
307
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
252
308
  const j = await r.json();
253
309
  const c = j && j.usage && typeof j.usage.cost === "number" ? j.usage.cost : null;
254
310
  if (onCost) onCost(costWithHeaders(c != null ? { ...j, cost: c } : j, r));
@@ -263,7 +319,7 @@ export class NanoClient {
263
319
  if (maskDataUrl) body.maskDataUrl = maskDataUrl; // white = repaint
264
320
  Object.assign(body, imageModelOpts(extra));
265
321
  const r = await this._postJson("/v1/images/generations", body, signal);
266
- if (!r.ok) throw httpError(r.status, await r.text());
322
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
267
323
  const j = await r.json();
268
324
  const urls = (j.data || [])
269
325
  .map((d) => (d.b64_json ? "data:" + b64ImageMime(d.b64_json) + ";base64," + d.b64_json : d.url))
@@ -309,7 +365,7 @@ export class NanoClient {
309
365
  let runId = resume || null;
310
366
  if (!runId) {
311
367
  const r = await this._postJson("/api/generate-video", body, signal);
312
- if (!r.ok) throw httpError(r.status, await r.text());
368
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
313
369
  const j = await r.json();
314
370
  if (onCost) onCost(costWithHeaders(j, r));
315
371
  runId = j.runId || j.id;
@@ -317,39 +373,50 @@ export class NanoClient {
317
373
  if (onRunId) onRunId(runId);
318
374
  }
319
375
 
320
- const t0 = Date.now();
321
- const videoCap = is3d ? this.timeouts.model3d : this.timeouts.video;
322
- const pollEvery = is3d ? (this.pollIntervals.model3d || this.pollIntervals.video) : this.pollIntervals.video;
323
- // Infinity/non-finite = wait forever (default). Finite ms = headless/CI cap.
324
- while (!Number.isFinite(videoCap) || Date.now() - t0 < videoCap) {
325
- await sleep(pollEvery, signal);
326
- let s;
327
- try {
328
- s = await (await this._get("/api/video/status?requestId=" + encodeURIComponent(runId), signal)).json();
329
- } catch (e) {
330
- if (signal && signal.aborted) throw abortError(signal.reason);
331
- continue; // transient poll failure — keep polling until complete or optional cap
332
- }
333
- const st = String((s.data && s.data.status) || s.status || "").toUpperCase();
334
- if (onPoll) onPoll({ status: st, elapsedMs: Date.now() - t0, runId });
335
- if (st === "COMPLETED" || st === "SUCCEEDED") {
336
- if (is3d) {
337
- const url = model3dStatusUrl(s);
338
- if (!url) throw new NanoodleError("completed but no model url");
376
+ try {
377
+ const t0 = Date.now();
378
+ const videoCap = is3d ? this.timeouts.model3d : this.timeouts.video;
379
+ const pollEvery = is3d ? (this.pollIntervals.model3d || this.pollIntervals.video) : this.pollIntervals.video;
380
+ // Infinity/non-finite = wait forever (default). Finite ms = headless/CI cap.
381
+ while (!Number.isFinite(videoCap) || Date.now() - t0 < videoCap) {
382
+ await sleep(Number.isFinite(videoCap) ? Math.min(pollEvery, Math.max(0, videoCap - (Date.now() - t0))) : pollEvery, signal);
383
+ if (Number.isFinite(videoCap) && Date.now() - t0 >= videoCap) break;
384
+ let s;
385
+ try {
386
+ s = await this._pollJson("/api/video/status?requestId=" + encodeURIComponent(runId), signal, videoCap - (Date.now() - t0));
387
+ } catch (e) {
388
+ if (signal && signal.aborted) throw abortError(signal.reason);
389
+ if (e.code === "poll-timeout") break;
390
+ if (e.status >= 400 && e.status < 500 && ![408, 429].includes(e.status)) throw e;
391
+ continue; // transient poll failure — keep polling until complete or optional cap
392
+ }
393
+ const st = String((s.data && s.data.status) || s.status || "").toUpperCase();
394
+ if (onPoll) onPoll({ status: st, elapsedMs: Date.now() - t0, runId });
395
+ if (st === "COMPLETED" || st === "SUCCEEDED") {
396
+ if (is3d) {
397
+ const url = model3dStatusUrl(s, model);
398
+ if (!url) throw new NanoodleError("completed but no model url");
399
+ return url;
400
+ }
401
+ const out = (s.data && s.data.output) || s.output || {};
402
+ const url = (out.video && out.video.url) || out.url || (Array.isArray(out.video) ? out.video[0] && out.video[0].url : null);
403
+ if (!url) throw new NanoodleError("completed but no video url");
339
404
  return url;
340
405
  }
341
- const out = (s.data && s.data.output) || s.output || {};
342
- const url = (out.video && out.video.url) || out.url || (Array.isArray(out.video) ? out.video[0] && out.video[0].url : null);
343
- if (!url) throw new NanoodleError("completed but no video url");
344
- return url;
345
- }
346
- if (["FAILED", "ERROR", "CANCELED"].includes(st)) {
347
- const why = (s.data && s.data.error) || st;
348
- throw new NanoodleError((is3d ? "3D failed: " : "video failed: ") + why);
406
+ if (["FAILED", "ERROR", "CANCELED", "CANCELLED", "CONTENT_POLICY_VIOLATION"].includes(st)) {
407
+ const why = (s.data && s.data.error) || st;
408
+ throw new NanoodleError((is3d ? "3D failed: " : "video failed: ") + why);
409
+ }
349
410
  }
411
+ const label = is3d ? "3D" : "video";
412
+ throw new NanoodleError(`${label} timed out (${Math.round(videoCap / 1000)}s) — the job may still be running on NanoGPT's side`, { code: "timeout" });
413
+ } catch (e) {
414
+ if (signal && signal.aborted) throw e;
415
+ throw new NanoodleError(e && e.message ? e.message : String(e), {
416
+ ...(e && e.code ? { code: e.code } : {}),
417
+ ...(e && e.status ? { status: e.status } : {}), runId, cause: e,
418
+ });
350
419
  }
351
- const label = is3d ? "3D" : "video";
352
- throw new NanoodleError(`${label} timed out (${Math.round(videoCap / 1000)}s) — the job may still be running on NanoGPT's side`, { code: "timeout" });
353
420
  }
354
421
 
355
422
  /**
@@ -374,7 +441,7 @@ export class NanoClient {
374
441
  delete body.prompt;
375
442
  }
376
443
  const r = await this._postJson("/api/v1/audio/speech", body, signal);
377
- if (!r.ok) throw httpError(r.status, await r.text());
444
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
378
445
  const ct = (r.headers && r.headers.get && r.headers.get("content-type")) || "";
379
446
  if (ct.includes("application/json")) {
380
447
  const j = await r.json();
@@ -405,12 +472,15 @@ export class NanoClient {
405
472
  if (j.isApiRequest != null) qs.set("isApiRequest", String(j.isApiRequest));
406
473
  const t0 = Date.now();
407
474
  while (Date.now() - t0 < this.timeouts.audio) {
408
- await sleep(this.pollIntervals.audio, signal);
475
+ await sleep(Math.min(this.pollIntervals.audio, Math.max(0, this.timeouts.audio - (Date.now() - t0))), signal);
476
+ if (Date.now() - t0 >= this.timeouts.audio) break;
409
477
  let s;
410
478
  try {
411
- s = await (await this._get("/api/tts/status?" + qs, signal)).json();
479
+ s = await this._pollJson("/api/tts/status?" + qs, signal, this.timeouts.audio - (Date.now() - t0));
412
480
  } catch (e) {
413
481
  if (signal && signal.aborted) throw abortError(signal.reason);
482
+ if (e.code === "poll-timeout") break;
483
+ if (e.status >= 400 && e.status < 500 && ![408, 429].includes(e.status)) throw e;
414
484
  continue;
415
485
  }
416
486
  const st = String(s.status || "").toLowerCase();
@@ -420,7 +490,7 @@ export class NanoClient {
420
490
  if (!url) throw new NanoodleError("completed but no audio url");
421
491
  return url;
422
492
  }
423
- if (["error", "failed", "content_policy_violation"].includes(st)) {
493
+ if (["error", "failed", "canceled", "cancelled", "content_policy_violation"].includes(st)) {
424
494
  throw new NanoodleError("audio failed: " + (s.error || s.message || st));
425
495
  }
426
496
  }
@@ -433,15 +503,9 @@ export class NanoClient {
433
503
  if (/^data:/i.test(audioUrl)) {
434
504
  ({ bytes, mime } = dataUrlBytes(audioUrl));
435
505
  } else {
436
- let r;
437
- try { r = await this.fetch(audioUrl, { signal }); } // media CDN — no auth headers
438
- catch (e) {
439
- if (e && (e.name === "AbortError" || e.code === "aborted")) throw e;
440
- if (signal && signal.aborted) throw e;
441
- throw new NanoodleError("couldn't download the audio to transcribe: " + redactUrl(audioUrl));
442
- }
443
- if (!r.ok) throw new NanoodleError("couldn't download the audio to transcribe (" + r.status + "): " + redactUrl(audioUrl));
444
- bytes = new Uint8Array(await r.arrayBuffer());
506
+ const downloaded = await downloadMedia(audioUrl, { fetch: this.fetch, signal, what: "the audio to transcribe" });
507
+ const r = downloaded.response;
508
+ bytes = downloaded.bytes;
445
509
  mime = ((r.headers && r.headers.get && r.headers.get("content-type")) || "audio/mpeg").split(";")[0];
446
510
  }
447
511
  if (bytes.length > 3.5 * 1024 * 1024) {
@@ -454,7 +518,7 @@ export class NanoClient {
454
518
  if (language) fd.append("language", language);
455
519
  // no explicit Content-Type — fetch sets the multipart boundary
456
520
  const r = await this._paidFetch(this.baseUrl + "/api/v1/audio/transcriptions", { method: "POST", headers: this._auth(), body: fd, signal }, signal);
457
- if (!r.ok) throw httpError(r.status, await r.text());
521
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
458
522
  const j = await r.json();
459
523
  if (onCost) onCost(costWithHeaders(j, r));
460
524
  const txt = j.transcription != null ? j.transcription
@@ -469,15 +533,7 @@ export class NanoClient {
469
533
  * Used when a chat audio part needs base64 bytes but the upstream node produced an https URL.
470
534
  */
471
535
  async fetchMediaDataUrl(url, { signal } = {}) {
472
- let r;
473
- try { r = await this.fetch(url, { signal }); }
474
- catch (e) {
475
- if (e && (e.name === "AbortError" || e.code === "aborted")) throw e;
476
- if (signal && signal.aborted) throw e;
477
- throw new NanoodleError("couldn't download media to inline: " + redactUrl(url));
478
- }
479
- if (!r.ok) throw new NanoodleError("couldn't download media to inline (" + r.status + "): " + redactUrl(url));
480
- const bytes = new Uint8Array(await r.arrayBuffer());
536
+ const { bytes, response: r } = await downloadMedia(url, { fetch: this.fetch, signal, what: "media to inline" });
481
537
  const ct = (((r.headers && r.headers.get && r.headers.get("content-type")) || "").split(";")[0] || "").trim().toLowerCase();
482
538
  const mime = ct && ct !== "application/octet-stream" && ct !== "binary/octet-stream" ? ct : undefined;
483
539
  return bytesToDataUrl(bytes, mime); // sniffs magic bytes when the CDN's content-type is generic
@@ -486,9 +542,12 @@ export class NanoClient {
486
542
  /**
487
543
  * Resolve a provider media URL before it is handed to another node or returned
488
544
  * as a node output. Relative content paths (Grok Imagine's
489
- * `/api/generate-video/content?...`) and same-origin `/api` or `/v1` URLs are
490
- * joined onto `baseUrl` and fetched with the API key, redirects manual so the
491
- * key is never forwarded to the file host. A redirect Location becomes the
545
+ * `/api/generate-video/content?...`) and the exact same-origin content paths
546
+ * in CONTENT_API_PATHS are joined onto `baseUrl` and fetched with the API key,
547
+ * redirects manual so the key is never forwarded to the file host. Any other
548
+ * same-origin `/api` or `/v1` URL, including a user-supplied upload URL, is
549
+ * refused and not fetched — a 200 from those paths must not be inlined with
550
+ * the caller's credentials. A redirect Location becomes the
492
551
  * absolute file URL (presigned queries stay on that URL — the next provider
493
552
  * needs them — and are never put in errors). A direct 200 is inlined as a
494
553
  * data URL only when it fits MEDIA_INLINE_MAX; an over-cap clip must come
@@ -507,17 +566,38 @@ export class NanoClient {
507
566
  /** Optional helper: POST /api/check-balance → { usd_balance }. */
508
567
  async checkBalance(signal) {
509
568
  const r = await this._postJson("/api/check-balance", {}, signal);
510
- if (!r.ok) throw httpError(r.status, await r.text());
569
+ if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
511
570
  return r.json();
512
571
  }
513
572
  }
514
573
 
515
- /** Paths that still need the API key (a content endpoint), not a public file. */
574
+ /**
575
+ * Exact content paths that return provider media. Credentials are sent only here.
576
+ * A prefix (`/api/…`, `/v1/…`) is not enough: `/api/v1/models`, an upload URL, or
577
+ * `/api/generate-video/content/extra` would otherwise be fetched with the key and
578
+ * a 200 body inlined into the graph output.
579
+ */
580
+ const CONTENT_API_PATHS = new Set([
581
+ "/api/generate-video/content",
582
+ "/api/images/content",
583
+ "/v1/audio/content",
584
+ ]);
585
+
516
586
  function isContentApiPath(pathname) {
587
+ return CONTENT_API_PATHS.has(pathname);
588
+ }
589
+
590
+ /** Same-origin API surface. These are not fetched unless the path is allowlisted. */
591
+ function isSameOriginApiPath(pathname) {
517
592
  return pathname === "/api" || pathname.startsWith("/api/")
518
593
  || pathname === "/v1" || pathname.startsWith("/v1/");
519
594
  }
520
595
 
596
+ function refuseNonContentApi(pathname) {
597
+ if (!isSameOriginApiPath(pathname) || isContentApiPath(pathname)) return;
598
+ throw new NanoodleError("that URL is not a provider content file, so it was not fetched");
599
+ }
600
+
521
601
  function apiBase(baseUrl) {
522
602
  const base = String(baseUrl || "https://nano-gpt.com").replace(/\/+$/, "");
523
603
  const origin = new URL(base + "/").origin;
@@ -583,15 +663,20 @@ export async function resolveProviderMediaUrl(url, { baseUrl, fetch: fetchImpl,
583
663
  try {
584
664
  if (/^https?:/i.test(s)) {
585
665
  const abs = new URL(s);
586
- if (abs.origin !== origin || !isContentApiPath(abs.pathname)) return s;
666
+ if (abs.origin !== origin) return s;
667
+ refuseNonContentApi(abs.pathname);
668
+ if (!isContentApiPath(abs.pathname)) return s;
587
669
  start = abs;
588
670
  } else if (s.startsWith("/") && !s.startsWith("//")) {
589
671
  start = new URL(s, base + "/");
672
+ refuseNonContentApi(start.pathname);
590
673
  if (!isContentApiPath(start.pathname)) return start.href;
591
674
  } else {
592
675
  return s;
593
676
  }
594
- } catch {
677
+ } catch (e) {
678
+ // A refused /api or /v1 URL must not be swallowed as "leave it unchanged".
679
+ if (e && e.name === "NanoodleError") throw e;
595
680
  return s;
596
681
  }
597
682
 
@@ -622,6 +707,9 @@ export async function resolveProviderMediaUrl(url, { baseUrl, fetch: fetchImpl,
622
707
  catch { throw new NanoodleError("provider media redirect had no usable location"); }
623
708
  // File host or a non-API path: hand back the absolute URL. Do not fetch it
624
709
  // (that would pull a multi-megabyte clip and might attach the key).
710
+ // A same-origin /api or /v1 hop that is not itself a content file is refused
711
+ // outright — its body must never be inlined with the caller's credentials.
712
+ if (next.origin === origin) refuseNonContentApi(next.pathname);
625
713
  if (next.origin !== origin || !isContentApiPath(next.pathname)) return next.href;
626
714
  current = next.href;
627
715
  continue;
package/src/decide.mjs CHANGED
@@ -11,6 +11,7 @@ export const DECIDE_SCALE_DEFAULT = "poor\nokay\ngood\ngreat";
11
11
  export const DECIDE_IMG_FALLBACK = { maxImages: 4, maxDimension: 512, maxEncodedBytes: 240000 };
12
12
 
13
13
  export function decideMode(f) {
14
+ if (f?.branch === true) return "yesno";
14
15
  const m = f && f.mode;
15
16
  return (m === "choose" || m === "score" || m === "yesno") ? m : "pick";
16
17
  }
@@ -154,7 +155,17 @@ export async function decideRun(f, model, text, imgs, lim, send, fit) {
154
155
  } else {
155
156
  j = await send({ model, state: decideState(text, sent), questions: { answer: q } });
156
157
  }
157
- const out = decideOutputs(mode, j && j.answers && j.answers.answer, q, imgs, j && j.usage, model);
158
+ const answer = j?.answers?.answer;
159
+ const branching = f?.branch === true;
160
+ if (branching && (answer?.type !== "noul" || !Number.isFinite(answer.noul) || answer.noul < 0 || answer.noul > 1)) {
161
+ throw new NanoodleError("Decide needs a valid yes/no answer to select a branch", { code: "decide-branch" });
162
+ }
163
+ const out = decideOutputs(mode, answer, q, imgs, j && j.usage, model);
164
+ if (branching) {
165
+ out.decision.branch = out.text;
166
+ out[out.text] = text || out.text;
167
+ return out;
168
+ }
158
169
  if (mode === "yesno" && (f.gate === true || f.gate === "true") && out.decision.yes < 0.5) throw decideGateError(out.decision, out);
159
170
  return out;
160
171
  }
package/src/errors.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  export class NanoodleError extends Error {
3
3
  /**
4
4
  * @param {string} message
5
- * @param {object} [props] extra fields (e.g. { code: "auth" | "funds" | "http", status })
5
+ * @param {object} [props] extra fields (e.g. { code: "auth" | "blocked" | "funds" | "http", status })
6
6
  */
7
7
  constructor(message, props = {}) {
8
8
  super(message);