nanoodle 0.13.0 → 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 +65 -2
- package/bin/nanoodle.mjs +14 -4
- package/package.json +4 -4
- package/src/branches.mjs +55 -0
- package/src/browser.mjs +1 -0
- package/src/client.mjs +190 -84
- package/src/decide.mjs +12 -1
- package/src/errors.mjs +1 -1
- package/src/estimate.mjs +171 -29
- package/src/graph.mjs +7 -2
- package/src/index.mjs +1 -0
- package/src/io.mjs +14 -13
- package/src/local-media.mjs +19 -26
- package/src/media.mjs +66 -19
- package/src/nodes.mjs +50 -27
- package/src/workflow.mjs +57 -29
- package/src/x402.mjs +9 -2
- package/templates/starter-graph.json +1 -1
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,
|
|
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
|
}
|
|
@@ -20,6 +25,16 @@ export function httpError(status, bodyText) {
|
|
|
20
25
|
return new NanoodleError(status + ": " + String(bodyText).slice(0, 160), { code: "http", status });
|
|
21
26
|
}
|
|
22
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
|
+
|
|
23
38
|
/**
|
|
24
39
|
* Cost extraction (mirrors the app's costFromJson). USD priority:
|
|
25
40
|
* j.cost (>0) → x_nanogpt_pricing.(costUsd|cost|amount) → metadata.cost → j.cost even when 0.
|
|
@@ -81,15 +96,17 @@ export function sleep(ms, signal) {
|
|
|
81
96
|
* pollIntervals / timeouts are per-media-kind knobs (ms).
|
|
82
97
|
*/
|
|
83
98
|
/**
|
|
84
|
-
*
|
|
99
|
+
* Model URL from a 3D status poll. A completed payload is 3D only when
|
|
85
100
|
* output.kind is "3d" or output.format is "glb" — a bare video url is not a model.
|
|
86
|
-
*
|
|
101
|
+
* Idol's model_url is only a point-cloud preview: its real Gaussian splat is
|
|
102
|
+
* model_mesh.url. Other models retain model_url, model.url, video.url, videoUrls[0].
|
|
87
103
|
*/
|
|
88
|
-
export function model3dStatusUrl(s) {
|
|
104
|
+
export function model3dStatusUrl(s, model) {
|
|
89
105
|
const out = (s && s.data && s.data.output) || (s && s.output) || {};
|
|
90
106
|
const kind = String(out.kind || "").toLowerCase();
|
|
91
107
|
const format = String(out.format || "").toLowerCase();
|
|
92
108
|
if (kind !== "3d" && format !== "glb") return "";
|
|
109
|
+
if (model === "speridlabs/idol") return (out.model_mesh && out.model_mesh.url) || "";
|
|
93
110
|
const list = Array.isArray(out.videoUrls) ? out.videoUrls : [];
|
|
94
111
|
const first = list.length ? list[0] : null;
|
|
95
112
|
const fromList = typeof first === "string" ? first : (first && first.url) || "";
|
|
@@ -128,17 +145,14 @@ export class NanoClient {
|
|
|
128
145
|
* at most once; a second 402 after settling is an error, never a second send.
|
|
129
146
|
*/
|
|
130
147
|
async _paidFetch(url, init, signal) {
|
|
148
|
+
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
131
149
|
const r = await this.fetch(url, init);
|
|
132
150
|
if (r.status !== 402 || !this.payment || this.apiKey) return r;
|
|
133
151
|
const settled = await this._settle402(r, signal);
|
|
134
152
|
if (settled.response) return settled.response; // complete replayed the stored request
|
|
153
|
+
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
135
154
|
const r2 = await this.fetch(url, { ...init, headers: { ...init.headers, "x-x402-payment-id": settled.paymentId } });
|
|
136
|
-
if (r2.
|
|
137
|
-
throw new NanoodleError(
|
|
138
|
-
"payment " + settled.paymentId + " settled, but the API still answered 402 on retry — " +
|
|
139
|
-
"check " + (settled.statusUrl || "the payment status") + " before paying again",
|
|
140
|
-
{ code: "x402", status: 402, paymentId: settled.paymentId });
|
|
141
|
-
}
|
|
155
|
+
if (!r2.ok) throw await paidResponseError(r2, settled.paymentId, "replay");
|
|
142
156
|
return r2;
|
|
143
157
|
}
|
|
144
158
|
|
|
@@ -151,10 +165,15 @@ export class NanoClient {
|
|
|
151
165
|
"payment required, but the 402 response offered no usable Nano option" +
|
|
152
166
|
(body ? " — " + JSON.stringify(body).slice(0, 200) : ""), { code: "x402", status: 402 });
|
|
153
167
|
}
|
|
168
|
+
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
169
|
+
if (invoice.expiresAt && invoice.expiresAt <= Date.now()) {
|
|
170
|
+
throw new NanoodleError("payment window expired before payment (" + invoice.paymentId + ", to " + invoice.payTo + ") — request a fresh invoice", { code: "x402-expired", paymentId: invoice.paymentId });
|
|
171
|
+
}
|
|
154
172
|
await this.payment(invoice); // ← the callback does the actual XNO send
|
|
155
173
|
// The complete endpoint doubles as the poll: 402 = not seen on-chain yet.
|
|
156
174
|
const deadline = invoice.expiresAt || Date.now() + 15 * 60 * 1000;
|
|
157
175
|
while (true) {
|
|
176
|
+
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
158
177
|
const cr = await this.fetch(invoice.completeUrl, {
|
|
159
178
|
method: "POST", headers: { "Content-Type": "application/json", "x-x402": "true" }, body: "{}", signal,
|
|
160
179
|
});
|
|
@@ -182,7 +201,13 @@ export class NanoClient {
|
|
|
182
201
|
{ code: "x402", status: cr.status, paymentId: invoice.paymentId });
|
|
183
202
|
}
|
|
184
203
|
let cj = null;
|
|
185
|
-
if (
|
|
204
|
+
if (mime.includes("json")) { try { cj = await cr.json(); } catch { /* fail closed below */ } }
|
|
205
|
+
if (!cj || typeof cj !== "object") {
|
|
206
|
+
throw new NanoodleError(
|
|
207
|
+
"payment " + invoice.paymentId + " was sent, but completion returned no readable JSON receipt — check " +
|
|
208
|
+
(invoice.statusUrl || "the payment status") + " before paying again",
|
|
209
|
+
{ code: "x402", paymentId: invoice.paymentId });
|
|
210
|
+
}
|
|
186
211
|
if (looksLikeResult(cj)) {
|
|
187
212
|
// wrap so call sites keep their Response contract (ok/json/text/headers)
|
|
188
213
|
const response = { ok: true, status: 200, headers: cr.headers, json: async () => cj, text: async () => JSON.stringify(cj) };
|
|
@@ -190,7 +215,7 @@ export class NanoClient {
|
|
|
190
215
|
}
|
|
191
216
|
return { paymentId: invoice.paymentId, statusUrl: invoice.statusUrl };
|
|
192
217
|
}
|
|
193
|
-
if (cr.status !== 402) throw
|
|
218
|
+
if (cr.status !== 402) throw await paidResponseError(cr, invoice.paymentId, "completion");
|
|
194
219
|
await cr.text().catch(() => {}); // drain the "not verified yet" body
|
|
195
220
|
if (Date.now() >= deadline) {
|
|
196
221
|
throw new NanoodleError(
|
|
@@ -215,6 +240,35 @@ export class NanoClient {
|
|
|
215
240
|
}, signal);
|
|
216
241
|
}
|
|
217
242
|
|
|
243
|
+
async _pollJson(path, signal, remainingMs) {
|
|
244
|
+
const ac = new AbortController();
|
|
245
|
+
const onAbort = () => ac.abort(abortError(signal.reason));
|
|
246
|
+
if (signal) {
|
|
247
|
+
if (signal.aborted) onAbort();
|
|
248
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
249
|
+
}
|
|
250
|
+
const timer = Number.isFinite(remainingMs) ? setTimeout(() => ac.abort(
|
|
251
|
+
new NanoodleError("poll deadline reached", { code: "poll-timeout" })), Math.max(0, remainingMs)) : null;
|
|
252
|
+
let rejectAbort;
|
|
253
|
+
const aborted = new Promise((_, reject) => {
|
|
254
|
+
rejectAbort = () => reject(ac.signal.reason);
|
|
255
|
+
ac.signal.addEventListener("abort", rejectAbort, { once: true });
|
|
256
|
+
if (ac.signal.aborted) rejectAbort();
|
|
257
|
+
});
|
|
258
|
+
const work = Promise.resolve().then(async () => {
|
|
259
|
+
if (ac.signal.aborted) throw ac.signal.reason;
|
|
260
|
+
const r = await this._get(path, ac.signal);
|
|
261
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
262
|
+
return r.json();
|
|
263
|
+
});
|
|
264
|
+
try { return await Promise.race([work, aborted]); }
|
|
265
|
+
finally {
|
|
266
|
+
if (timer) clearTimeout(timer);
|
|
267
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
268
|
+
ac.signal.removeEventListener("abort", rejectAbort);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
218
272
|
async _get(path, signal) {
|
|
219
273
|
return this.fetch(this.baseUrl + path, { headers: this._auth(), signal });
|
|
220
274
|
}
|
|
@@ -222,12 +276,19 @@ export class NanoClient {
|
|
|
222
276
|
/** POST /api/v1/chat/completions (non-streaming). Returns the assistant text. */
|
|
223
277
|
async chat(messages, model, opts = {}, { onCost, signal } = {}) {
|
|
224
278
|
const body = { model, messages };
|
|
225
|
-
|
|
226
|
-
if (
|
|
279
|
+
// Carbon has no sampling-temperature control; omit it rather than restoring a default.
|
|
280
|
+
if (model !== "google/gemini-4-carbon") body.temperature = opts.temperature != null && opts.temperature !== "" ? +opts.temperature : 0.8;
|
|
227
281
|
if (opts.response_format) body.response_format = opts.response_format;
|
|
228
|
-
|
|
282
|
+
// Carbon's selected NanoGPT provider rejects explicit thinking controls (HTTP 400).
|
|
283
|
+
// Let the provider use its default rather than forwarding reasoning_effort.
|
|
284
|
+
if (model !== "google/gemini-4-carbon" && opts.reasoning_effort) body.reasoning_effort = opts.reasoning_effort;
|
|
285
|
+
// max_tokens is the one token budget forwarded, and only when a runner
|
|
286
|
+
// explicitly opts in via opts (the graph's maxTokens field, D1 ruling).
|
|
287
|
+
// The legacy spellings (max_completion_tokens, max_output_tokens,
|
|
288
|
+
// maxTokens) are never read here, so they can never leak into the body.
|
|
289
|
+
if (opts.max_tokens != null) body.max_tokens = opts.max_tokens;
|
|
229
290
|
const r = await this._postJson("/api/v1/chat/completions", body, signal);
|
|
230
|
-
if (!r.ok) throw httpError(r.status, await r.text());
|
|
291
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
231
292
|
const j = await r.json();
|
|
232
293
|
if (onCost) onCost(costWithHeaders(j, r));
|
|
233
294
|
const msg = (j.choices && j.choices[0] && j.choices[0].message) || {};
|
|
@@ -248,29 +309,42 @@ export class NanoClient {
|
|
|
248
309
|
*/
|
|
249
310
|
async decide(body, { onCost, signal } = {}) {
|
|
250
311
|
const r = await this._postJson("/api/v1/decisions", body, signal);
|
|
251
|
-
if (!r.ok) throw httpError(r.status, await r.text());
|
|
312
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
252
313
|
const j = await r.json();
|
|
253
314
|
const c = j && j.usage && typeof j.usage.cost === "number" ? j.usage.cost : null;
|
|
254
315
|
if (onCost) onCost(costWithHeaders(c != null ? { ...j, cost: c } : j, r));
|
|
255
316
|
return j;
|
|
256
317
|
}
|
|
257
318
|
|
|
258
|
-
/** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s).
|
|
259
|
-
|
|
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 } = {}) {
|
|
260
325
|
const body = { model, size: size || "1024x1024", n, response_format: "b64_json" };
|
|
261
326
|
if (prompt) body.prompt = prompt; // omit when blank — upscalers run with no instruction
|
|
262
327
|
if (imageDataUrl) body.imageDataUrl = imageDataUrl; // string OR array (edit multi-reference)
|
|
263
328
|
if (maskDataUrl) body.maskDataUrl = maskDataUrl; // white = repaint
|
|
264
329
|
Object.assign(body, imageModelOpts(extra));
|
|
265
330
|
const r = await this._postJson("/v1/images/generations", body, signal);
|
|
266
|
-
if (!r.ok) throw httpError(r.status, await r.text());
|
|
331
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
267
332
|
const j = await r.json();
|
|
268
|
-
const
|
|
269
|
-
.map((d) =>
|
|
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
|
+
})
|
|
270
338
|
.filter(Boolean);
|
|
339
|
+
const urls = pairs.map((p) => p.url);
|
|
271
340
|
if (!urls.length) throw new NanoodleError("no image in response");
|
|
272
341
|
if (onCost) onCost(costWithHeaders(j, r));
|
|
273
|
-
|
|
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;
|
|
274
348
|
}
|
|
275
349
|
|
|
276
350
|
/** POST /api/generate-video then poll GET /api/video/status?requestId= until COMPLETED. */
|
|
@@ -309,7 +383,7 @@ export class NanoClient {
|
|
|
309
383
|
let runId = resume || null;
|
|
310
384
|
if (!runId) {
|
|
311
385
|
const r = await this._postJson("/api/generate-video", body, signal);
|
|
312
|
-
if (!r.ok) throw httpError(r.status, await r.text());
|
|
386
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
313
387
|
const j = await r.json();
|
|
314
388
|
if (onCost) onCost(costWithHeaders(j, r));
|
|
315
389
|
runId = j.runId || j.id;
|
|
@@ -317,39 +391,50 @@ export class NanoClient {
|
|
|
317
391
|
if (onRunId) onRunId(runId);
|
|
318
392
|
}
|
|
319
393
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
s
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
394
|
+
try {
|
|
395
|
+
const t0 = Date.now();
|
|
396
|
+
const videoCap = is3d ? this.timeouts.model3d : this.timeouts.video;
|
|
397
|
+
const pollEvery = is3d ? (this.pollIntervals.model3d || this.pollIntervals.video) : this.pollIntervals.video;
|
|
398
|
+
// Infinity/non-finite = wait forever (default). Finite ms = headless/CI cap.
|
|
399
|
+
while (!Number.isFinite(videoCap) || Date.now() - t0 < videoCap) {
|
|
400
|
+
await sleep(Number.isFinite(videoCap) ? Math.min(pollEvery, Math.max(0, videoCap - (Date.now() - t0))) : pollEvery, signal);
|
|
401
|
+
if (Number.isFinite(videoCap) && Date.now() - t0 >= videoCap) break;
|
|
402
|
+
let s;
|
|
403
|
+
try {
|
|
404
|
+
s = await this._pollJson("/api/video/status?requestId=" + encodeURIComponent(runId), signal, videoCap - (Date.now() - t0));
|
|
405
|
+
} catch (e) {
|
|
406
|
+
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
407
|
+
if (e.code === "poll-timeout") break;
|
|
408
|
+
if (e.status >= 400 && e.status < 500 && ![408, 429].includes(e.status)) throw e;
|
|
409
|
+
continue; // transient poll failure — keep polling until complete or optional cap
|
|
410
|
+
}
|
|
411
|
+
const st = String((s.data && s.data.status) || s.status || "").toUpperCase();
|
|
412
|
+
if (onPoll) onPoll({ status: st, elapsedMs: Date.now() - t0, runId });
|
|
413
|
+
if (st === "COMPLETED" || st === "SUCCEEDED") {
|
|
414
|
+
if (is3d) {
|
|
415
|
+
const url = model3dStatusUrl(s, model);
|
|
416
|
+
if (!url) throw new NanoodleError("completed but no model url");
|
|
417
|
+
return url;
|
|
418
|
+
}
|
|
419
|
+
const out = (s.data && s.data.output) || s.output || {};
|
|
420
|
+
const url = (out.video && out.video.url) || out.url || (Array.isArray(out.video) ? out.video[0] && out.video[0].url : null);
|
|
421
|
+
if (!url) throw new NanoodleError("completed but no video url");
|
|
339
422
|
return url;
|
|
340
423
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
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);
|
|
424
|
+
if (["FAILED", "ERROR", "CANCELED", "CANCELLED", "CONTENT_POLICY_VIOLATION"].includes(st)) {
|
|
425
|
+
const why = (s.data && s.data.error) || st;
|
|
426
|
+
throw new NanoodleError((is3d ? "3D failed: " : "video failed: ") + why);
|
|
427
|
+
}
|
|
349
428
|
}
|
|
429
|
+
const label = is3d ? "3D" : "video";
|
|
430
|
+
throw new NanoodleError(`${label} timed out (${Math.round(videoCap / 1000)}s) — the job may still be running on NanoGPT's side`, { code: "timeout" });
|
|
431
|
+
} catch (e) {
|
|
432
|
+
if (signal && signal.aborted) throw e;
|
|
433
|
+
throw new NanoodleError(e && e.message ? e.message : String(e), {
|
|
434
|
+
...(e && e.code ? { code: e.code } : {}),
|
|
435
|
+
...(e && e.status ? { status: e.status } : {}), runId, cause: e,
|
|
436
|
+
});
|
|
350
437
|
}
|
|
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
438
|
}
|
|
354
439
|
|
|
355
440
|
/**
|
|
@@ -374,7 +459,7 @@ export class NanoClient {
|
|
|
374
459
|
delete body.prompt;
|
|
375
460
|
}
|
|
376
461
|
const r = await this._postJson("/api/v1/audio/speech", body, signal);
|
|
377
|
-
if (!r.ok) throw httpError(r.status, await r.text());
|
|
462
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
378
463
|
const ct = (r.headers && r.headers.get && r.headers.get("content-type")) || "";
|
|
379
464
|
if (ct.includes("application/json")) {
|
|
380
465
|
const j = await r.json();
|
|
@@ -405,12 +490,15 @@ export class NanoClient {
|
|
|
405
490
|
if (j.isApiRequest != null) qs.set("isApiRequest", String(j.isApiRequest));
|
|
406
491
|
const t0 = Date.now();
|
|
407
492
|
while (Date.now() - t0 < this.timeouts.audio) {
|
|
408
|
-
await sleep(this.pollIntervals.audio, signal);
|
|
493
|
+
await sleep(Math.min(this.pollIntervals.audio, Math.max(0, this.timeouts.audio - (Date.now() - t0))), signal);
|
|
494
|
+
if (Date.now() - t0 >= this.timeouts.audio) break;
|
|
409
495
|
let s;
|
|
410
496
|
try {
|
|
411
|
-
s = await
|
|
497
|
+
s = await this._pollJson("/api/tts/status?" + qs, signal, this.timeouts.audio - (Date.now() - t0));
|
|
412
498
|
} catch (e) {
|
|
413
499
|
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
500
|
+
if (e.code === "poll-timeout") break;
|
|
501
|
+
if (e.status >= 400 && e.status < 500 && ![408, 429].includes(e.status)) throw e;
|
|
414
502
|
continue;
|
|
415
503
|
}
|
|
416
504
|
const st = String(s.status || "").toLowerCase();
|
|
@@ -420,7 +508,7 @@ export class NanoClient {
|
|
|
420
508
|
if (!url) throw new NanoodleError("completed but no audio url");
|
|
421
509
|
return url;
|
|
422
510
|
}
|
|
423
|
-
if (["error", "failed", "content_policy_violation"].includes(st)) {
|
|
511
|
+
if (["error", "failed", "canceled", "cancelled", "content_policy_violation"].includes(st)) {
|
|
424
512
|
throw new NanoodleError("audio failed: " + (s.error || s.message || st));
|
|
425
513
|
}
|
|
426
514
|
}
|
|
@@ -433,15 +521,9 @@ export class NanoClient {
|
|
|
433
521
|
if (/^data:/i.test(audioUrl)) {
|
|
434
522
|
({ bytes, mime } = dataUrlBytes(audioUrl));
|
|
435
523
|
} else {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
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());
|
|
524
|
+
const downloaded = await downloadMedia(audioUrl, { fetch: this.fetch, signal, what: "the audio to transcribe" });
|
|
525
|
+
const r = downloaded.response;
|
|
526
|
+
bytes = downloaded.bytes;
|
|
445
527
|
mime = ((r.headers && r.headers.get && r.headers.get("content-type")) || "audio/mpeg").split(";")[0];
|
|
446
528
|
}
|
|
447
529
|
if (bytes.length > 3.5 * 1024 * 1024) {
|
|
@@ -454,7 +536,7 @@ export class NanoClient {
|
|
|
454
536
|
if (language) fd.append("language", language);
|
|
455
537
|
// no explicit Content-Type — fetch sets the multipart boundary
|
|
456
538
|
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());
|
|
539
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
458
540
|
const j = await r.json();
|
|
459
541
|
if (onCost) onCost(costWithHeaders(j, r));
|
|
460
542
|
const txt = j.transcription != null ? j.transcription
|
|
@@ -469,15 +551,7 @@ export class NanoClient {
|
|
|
469
551
|
* Used when a chat audio part needs base64 bytes but the upstream node produced an https URL.
|
|
470
552
|
*/
|
|
471
553
|
async fetchMediaDataUrl(url, { signal } = {}) {
|
|
472
|
-
|
|
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());
|
|
554
|
+
const { bytes, response: r } = await downloadMedia(url, { fetch: this.fetch, signal, what: "media to inline" });
|
|
481
555
|
const ct = (((r.headers && r.headers.get && r.headers.get("content-type")) || "").split(";")[0] || "").trim().toLowerCase();
|
|
482
556
|
const mime = ct && ct !== "application/octet-stream" && ct !== "binary/octet-stream" ? ct : undefined;
|
|
483
557
|
return bytesToDataUrl(bytes, mime); // sniffs magic bytes when the CDN's content-type is generic
|
|
@@ -486,9 +560,12 @@ export class NanoClient {
|
|
|
486
560
|
/**
|
|
487
561
|
* Resolve a provider media URL before it is handed to another node or returned
|
|
488
562
|
* as a node output. Relative content paths (Grok Imagine's
|
|
489
|
-
* `/api/generate-video/content?...`) and same-origin
|
|
490
|
-
* joined onto `baseUrl` and fetched with the API key,
|
|
491
|
-
* key is never forwarded to the file host.
|
|
563
|
+
* `/api/generate-video/content?...`) and the exact same-origin content paths
|
|
564
|
+
* in CONTENT_API_PATHS are joined onto `baseUrl` and fetched with the API key,
|
|
565
|
+
* redirects manual so the key is never forwarded to the file host. Any other
|
|
566
|
+
* same-origin `/api` or `/v1` URL, including a user-supplied upload URL, is
|
|
567
|
+
* refused and not fetched — a 200 from those paths must not be inlined with
|
|
568
|
+
* the caller's credentials. A redirect Location becomes the
|
|
492
569
|
* absolute file URL (presigned queries stay on that URL — the next provider
|
|
493
570
|
* needs them — and are never put in errors). A direct 200 is inlined as a
|
|
494
571
|
* data URL only when it fits MEDIA_INLINE_MAX; an over-cap clip must come
|
|
@@ -507,17 +584,38 @@ export class NanoClient {
|
|
|
507
584
|
/** Optional helper: POST /api/check-balance → { usd_balance }. */
|
|
508
585
|
async checkBalance(signal) {
|
|
509
586
|
const r = await this._postJson("/api/check-balance", {}, signal);
|
|
510
|
-
if (!r.ok) throw httpError(r.status, await r.text());
|
|
587
|
+
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
511
588
|
return r.json();
|
|
512
589
|
}
|
|
513
590
|
}
|
|
514
591
|
|
|
515
|
-
/**
|
|
592
|
+
/**
|
|
593
|
+
* Exact content paths that return provider media. Credentials are sent only here.
|
|
594
|
+
* A prefix (`/api/…`, `/v1/…`) is not enough: `/api/v1/models`, an upload URL, or
|
|
595
|
+
* `/api/generate-video/content/extra` would otherwise be fetched with the key and
|
|
596
|
+
* a 200 body inlined into the graph output.
|
|
597
|
+
*/
|
|
598
|
+
const CONTENT_API_PATHS = new Set([
|
|
599
|
+
"/api/generate-video/content",
|
|
600
|
+
"/api/images/content",
|
|
601
|
+
"/v1/audio/content",
|
|
602
|
+
]);
|
|
603
|
+
|
|
516
604
|
function isContentApiPath(pathname) {
|
|
605
|
+
return CONTENT_API_PATHS.has(pathname);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** Same-origin API surface. These are not fetched unless the path is allowlisted. */
|
|
609
|
+
function isSameOriginApiPath(pathname) {
|
|
517
610
|
return pathname === "/api" || pathname.startsWith("/api/")
|
|
518
611
|
|| pathname === "/v1" || pathname.startsWith("/v1/");
|
|
519
612
|
}
|
|
520
613
|
|
|
614
|
+
function refuseNonContentApi(pathname) {
|
|
615
|
+
if (!isSameOriginApiPath(pathname) || isContentApiPath(pathname)) return;
|
|
616
|
+
throw new NanoodleError("that URL is not a provider content file, so it was not fetched");
|
|
617
|
+
}
|
|
618
|
+
|
|
521
619
|
function apiBase(baseUrl) {
|
|
522
620
|
const base = String(baseUrl || "https://nano-gpt.com").replace(/\/+$/, "");
|
|
523
621
|
const origin = new URL(base + "/").origin;
|
|
@@ -583,15 +681,20 @@ export async function resolveProviderMediaUrl(url, { baseUrl, fetch: fetchImpl,
|
|
|
583
681
|
try {
|
|
584
682
|
if (/^https?:/i.test(s)) {
|
|
585
683
|
const abs = new URL(s);
|
|
586
|
-
if (abs.origin !== origin
|
|
684
|
+
if (abs.origin !== origin) return s;
|
|
685
|
+
refuseNonContentApi(abs.pathname);
|
|
686
|
+
if (!isContentApiPath(abs.pathname)) return s;
|
|
587
687
|
start = abs;
|
|
588
688
|
} else if (s.startsWith("/") && !s.startsWith("//")) {
|
|
589
689
|
start = new URL(s, base + "/");
|
|
690
|
+
refuseNonContentApi(start.pathname);
|
|
590
691
|
if (!isContentApiPath(start.pathname)) return start.href;
|
|
591
692
|
} else {
|
|
592
693
|
return s;
|
|
593
694
|
}
|
|
594
|
-
} catch {
|
|
695
|
+
} catch (e) {
|
|
696
|
+
// A refused /api or /v1 URL must not be swallowed as "leave it unchanged".
|
|
697
|
+
if (e && e.name === "NanoodleError") throw e;
|
|
595
698
|
return s;
|
|
596
699
|
}
|
|
597
700
|
|
|
@@ -622,6 +725,9 @@ export async function resolveProviderMediaUrl(url, { baseUrl, fetch: fetchImpl,
|
|
|
622
725
|
catch { throw new NanoodleError("provider media redirect had no usable location"); }
|
|
623
726
|
// File host or a non-API path: hand back the absolute URL. Do not fetch it
|
|
624
727
|
// (that would pull a multi-megabyte clip and might attach the key).
|
|
728
|
+
// A same-origin /api or /v1 hop that is not itself a content file is refused
|
|
729
|
+
// outright — its body must never be inlined with the caller's credentials.
|
|
730
|
+
if (next.origin === origin) refuseNonContentApi(next.pathname);
|
|
625
731
|
if (next.origin !== origin || !isContentApiPath(next.pathname)) return next.href;
|
|
626
732
|
current = next.href;
|
|
627
733
|
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
|
|
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);
|