nanoodle 0.1.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/LICENSE +21 -0
- package/README.md +150 -0
- package/bin/nanoodle.mjs +195 -0
- package/package.json +52 -0
- package/src/client.mjs +321 -0
- package/src/errors.mjs +32 -0
- package/src/graph.mjs +134 -0
- package/src/index.mjs +6 -0
- package/src/io.mjs +312 -0
- package/src/media.mjs +157 -0
- package/src/nodes.mjs +441 -0
- package/src/workflow.mjs +307 -0
package/src/client.mjs
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { NanoodleError } from "./errors.mjs";
|
|
2
|
+
import { b64ImageMime, bytesToDataUrl, dataUrlBytes, MEDIA_INLINE_MAX } from "./media.mjs";
|
|
3
|
+
|
|
4
|
+
const AUDIO_MIME = { mp3: "audio/mpeg", opus: "audio/ogg", aac: "audio/aac", flac: "audio/flac", wav: "audio/wav", pcm: "audio/wav" };
|
|
5
|
+
|
|
6
|
+
/** Map an HTTP failure to an actionable error (mirrors the app's httpRunError). Never leaks the key. */
|
|
7
|
+
export function httpError(status, bodyText) {
|
|
8
|
+
if (status === 401 || status === 403) {
|
|
9
|
+
return new NanoodleError(`API key rejected (HTTP ${status}) — check your NanoGPT key / NANOGPT_API_KEY`, { code: "auth", status });
|
|
10
|
+
}
|
|
11
|
+
if (status === 402 || /insufficient|balance|funds|not enough|payment required/i.test(String(bodyText || ""))) {
|
|
12
|
+
return new NanoodleError("out of balance — this run needs more credit. Top up at nano-gpt.com, then run again.", { code: "funds", status });
|
|
13
|
+
}
|
|
14
|
+
return new NanoodleError(status + ": " + String(bodyText).slice(0, 160), { code: "http", status });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Cost extraction (mirrors the app's costFromJson). USD priority:
|
|
19
|
+
* j.cost (>0) → x_nanogpt_pricing.(costUsd|cost|amount) → metadata.cost → j.cost even when 0.
|
|
20
|
+
* Present-but-zero = known-free (kept); absent = unknown.
|
|
21
|
+
*/
|
|
22
|
+
export function costFromJson(j) {
|
|
23
|
+
if (!j) return { usd: null, balance: null };
|
|
24
|
+
const p = j.x_nanogpt_pricing;
|
|
25
|
+
const pUsd = p && (p.costUsd != null ? p.costUsd : p.cost != null ? p.cost : p.amount);
|
|
26
|
+
const mUsd = j.metadata && j.metadata.cost;
|
|
27
|
+
const usd = typeof j.cost === "number" && j.cost > 0 ? j.cost
|
|
28
|
+
: pUsd != null && isFinite(Number(pUsd)) ? Number(pUsd)
|
|
29
|
+
: mUsd != null && isFinite(Number(mUsd)) ? Number(mUsd)
|
|
30
|
+
: typeof j.cost === "number" ? j.cost
|
|
31
|
+
: null;
|
|
32
|
+
const balance = typeof j.remainingBalance === "number" ? j.remainingBalance
|
|
33
|
+
: p && typeof p.remainingBalance === "number" ? p.remainingBalance
|
|
34
|
+
: null;
|
|
35
|
+
return { usd, balance };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Header-borne cost/balance (binary audio path): x-cost / x-nano-cost, x-remaining-balance. */
|
|
39
|
+
export function costFromHeaders(r) {
|
|
40
|
+
const g = (k) => (r && r.headers && r.headers.get ? r.headers.get(k) : null);
|
|
41
|
+
const c = parseFloat(g("x-cost") || g("x-nano-cost") || "");
|
|
42
|
+
const b = parseFloat(g("x-remaining-balance") || "");
|
|
43
|
+
return { usd: isNaN(c) ? null : c, balance: isNaN(b) ? null : b };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** JSON cost wins for usd; the x-remaining-balance header wins for balance. */
|
|
47
|
+
export function costWithHeaders(j, r) {
|
|
48
|
+
const fromJson = costFromJson(j);
|
|
49
|
+
const fromHeaders = costFromHeaders(r);
|
|
50
|
+
return {
|
|
51
|
+
usd: fromJson.usd != null ? fromJson.usd : fromHeaders.usd,
|
|
52
|
+
balance: fromHeaders.balance != null ? fromHeaders.balance : fromJson.balance,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function abortError(reason) {
|
|
57
|
+
return reason instanceof Error ? reason : new NanoodleError(reason ? String(reason) : "run aborted", { code: "aborted" });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Abortable sleep. */
|
|
61
|
+
export function sleep(ms, signal) {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
if (signal && signal.aborted) return reject(abortError(signal.reason));
|
|
64
|
+
const t = setTimeout(() => {
|
|
65
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
66
|
+
resolve();
|
|
67
|
+
}, ms);
|
|
68
|
+
function onAbort() { clearTimeout(t); reject(abortError(signal.reason)); }
|
|
69
|
+
if (signal) signal.addEventListener("abort", onAbort, { once: true });
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* NanoGPT transport. `baseUrl` and `fetch` are injectable (that's how the offline test harness runs);
|
|
75
|
+
* pollIntervals / timeouts are per-media-kind knobs (ms).
|
|
76
|
+
*/
|
|
77
|
+
export class NanoClient {
|
|
78
|
+
constructor({ apiKey, baseUrl = "https://nano-gpt.com", fetch = globalThis.fetch, pollIntervals = {}, timeouts = {} } = {}) {
|
|
79
|
+
// non-enumerable: console.log/util.inspect/JSON.stringify of a client (or a Workflow holding
|
|
80
|
+
// one) must never print the key
|
|
81
|
+
Object.defineProperty(this, "apiKey", { value: apiKey, writable: true, enumerable: false, configurable: true });
|
|
82
|
+
this.baseUrl = String(baseUrl).replace(/\/+$/, "");
|
|
83
|
+
this.fetch = fetch;
|
|
84
|
+
this.pollIntervals = { video: 5000, audio: 3000, ...pollIntervals };
|
|
85
|
+
this.timeouts = { video: 600000, audio: 300000, ...timeouts };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_auth() { return { Authorization: "Bearer " + this.apiKey, "x-api-key": this.apiKey }; }
|
|
89
|
+
|
|
90
|
+
async _postJson(path, body, signal) {
|
|
91
|
+
const payload = JSON.stringify(body);
|
|
92
|
+
if (payload.length > MEDIA_INLINE_MAX) {
|
|
93
|
+
throw new NanoodleError("request body is too large (~4 MB max) — nanoodle sends media inline as base64; use smaller/shorter media");
|
|
94
|
+
}
|
|
95
|
+
return this.fetch(this.baseUrl + path, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers: { "Content-Type": "application/json", ...this._auth() },
|
|
98
|
+
body: payload,
|
|
99
|
+
signal,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async _get(path, signal) {
|
|
104
|
+
return this.fetch(this.baseUrl + path, { headers: this._auth(), signal });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** POST /api/v1/chat/completions (non-streaming). Returns the assistant text. */
|
|
108
|
+
async chat(messages, model, opts = {}, { onCost, signal } = {}) {
|
|
109
|
+
const body = { model, messages };
|
|
110
|
+
body.temperature = opts.temperature != null && opts.temperature !== "" ? +opts.temperature : 0.8;
|
|
111
|
+
if (opts.max_tokens) body.max_tokens = +opts.max_tokens;
|
|
112
|
+
if (opts.response_format) body.response_format = opts.response_format;
|
|
113
|
+
if (opts.reasoning_effort) body.reasoning_effort = opts.reasoning_effort;
|
|
114
|
+
const r = await this._postJson("/api/v1/chat/completions", body, signal);
|
|
115
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
116
|
+
const j = await r.json();
|
|
117
|
+
if (onCost) onCost(costWithHeaders(j, r));
|
|
118
|
+
const msg = (j.choices && j.choices[0] && j.choices[0].message) || {};
|
|
119
|
+
const txt = msg.content;
|
|
120
|
+
if (txt == null || txt === "") throw new NanoodleError("no text in response");
|
|
121
|
+
let out = typeof txt === "string" ? txt : txt.map((p) => p.text || "").join("");
|
|
122
|
+
if (opts.showThinking && msg.reasoning) {
|
|
123
|
+
out = "```thinking\n" + msg.reasoning + "\n```\n\n" + out;
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Draw-node twin of chat(): the model answers with images in message.images[]. */
|
|
129
|
+
async chatImage(messages, model, opts = {}, { onCost, signal } = {}) {
|
|
130
|
+
const body = { model, messages };
|
|
131
|
+
body.temperature = opts.temperature != null && opts.temperature !== "" ? +opts.temperature : 0.8;
|
|
132
|
+
const r = await this._postJson("/api/v1/chat/completions", body, signal);
|
|
133
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
134
|
+
const j = await r.json();
|
|
135
|
+
if (onCost) onCost(costWithHeaders(j, r));
|
|
136
|
+
const msg = (j.choices && j.choices[0] && j.choices[0].message) || {};
|
|
137
|
+
const images = (msg.images || [])
|
|
138
|
+
.map((im) => (im && im.image_url && im.image_url.url) || (im && im.url) || (typeof im === "string" ? im : null))
|
|
139
|
+
.filter(Boolean);
|
|
140
|
+
const text = typeof msg.content === "string" ? msg.content
|
|
141
|
+
: Array.isArray(msg.content) ? msg.content.map((p) => p.text || "").join("") : "";
|
|
142
|
+
if (!images.length) {
|
|
143
|
+
throw new NanoodleError(text ? "this model replied with text, not an image — pick an image-output model" : "no image in response");
|
|
144
|
+
}
|
|
145
|
+
return { images, text, reasoning: msg.reasoning || "" };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s). */
|
|
149
|
+
async image({ prompt, model, size, imageDataUrl, maskDataUrl, extra, n = 1, multi = false }, { onCost, signal } = {}) {
|
|
150
|
+
const body = { model, size: size || "1024x1024", n, response_format: "b64_json" };
|
|
151
|
+
if (prompt) body.prompt = prompt; // omit when blank — upscalers run with no instruction
|
|
152
|
+
if (imageDataUrl) body.imageDataUrl = imageDataUrl; // string OR array (edit multi-reference)
|
|
153
|
+
if (maskDataUrl) body.maskDataUrl = maskDataUrl; // white = repaint
|
|
154
|
+
if (extra) Object.assign(body, extra);
|
|
155
|
+
const r = await this._postJson("/v1/images/generations", body, signal);
|
|
156
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
157
|
+
const j = await r.json();
|
|
158
|
+
const urls = (j.data || [])
|
|
159
|
+
.map((d) => (d.b64_json ? "data:" + b64ImageMime(d.b64_json) + ";base64," + d.b64_json : d.url))
|
|
160
|
+
.filter(Boolean);
|
|
161
|
+
if (!urls.length) throw new NanoodleError("no image in response");
|
|
162
|
+
if (onCost) onCost(costWithHeaders(j, r));
|
|
163
|
+
return multi ? urls : urls[0];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** POST /api/generate-video then poll GET /api/video/status?requestId= until COMPLETED. */
|
|
167
|
+
async video(model, prompt, opts = {}, imageDataUrl, { onCost, onPoll, signal } = {}) {
|
|
168
|
+
const body = { model, prompt };
|
|
169
|
+
if (opts.duration) body.duration = opts.duration;
|
|
170
|
+
if (opts.aspect_ratio) body.aspect_ratio = opts.aspect_ratio;
|
|
171
|
+
if (opts.resolution) body.resolution = opts.resolution;
|
|
172
|
+
if (imageDataUrl) body.imageDataUrl = imageDataUrl;
|
|
173
|
+
if (opts.last_image) body.last_image = opts.last_image;
|
|
174
|
+
if (opts.videoUrl) body.videoUrl = opts.videoUrl;
|
|
175
|
+
if (opts.videoDataUrl) body.videoDataUrl = opts.videoDataUrl;
|
|
176
|
+
if (opts.audioUrl) body.audioUrl = opts.audioUrl;
|
|
177
|
+
if (opts.audioDataUrl) body.audioDataUrl = opts.audioDataUrl;
|
|
178
|
+
if (opts.lora) Object.assign(body, opts.lora); // LoRA params (lora_url_1.. for LTX video)
|
|
179
|
+
if (opts.extra) Object.assign(body, opts.extra); // fields.modelOpts — per-model knobs incl. seed
|
|
180
|
+
// node-owned dims win over stale modelOpts copies (twin of the app runtime)
|
|
181
|
+
if (opts.duration) body.duration = opts.duration;
|
|
182
|
+
if (opts.aspect_ratio) body.aspect_ratio = opts.aspect_ratio;
|
|
183
|
+
if (opts.resolution) body.resolution = opts.resolution;
|
|
184
|
+
if (opts.refImages && opts.refImages.length) body[opts.refKey || "reference_images"] = opts.refImages; // wired refs win last
|
|
185
|
+
const r = await this._postJson("/api/generate-video", body, signal);
|
|
186
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
187
|
+
const j = await r.json();
|
|
188
|
+
if (onCost) onCost(costWithHeaders(j, r));
|
|
189
|
+
const runId = j.runId || j.id;
|
|
190
|
+
if (!runId) throw new NanoodleError("no runId returned");
|
|
191
|
+
|
|
192
|
+
const t0 = Date.now();
|
|
193
|
+
while (Date.now() - t0 < this.timeouts.video) {
|
|
194
|
+
await sleep(this.pollIntervals.video, signal);
|
|
195
|
+
let s;
|
|
196
|
+
try {
|
|
197
|
+
s = await (await this._get("/api/video/status?requestId=" + encodeURIComponent(runId), signal)).json();
|
|
198
|
+
} catch (e) {
|
|
199
|
+
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
200
|
+
continue; // transient poll failure — keep polling until timeout
|
|
201
|
+
}
|
|
202
|
+
const st = String((s.data && s.data.status) || s.status || "").toUpperCase();
|
|
203
|
+
if (onPoll) onPoll({ status: st, elapsedMs: Date.now() - t0, runId });
|
|
204
|
+
if (st === "COMPLETED" || st === "SUCCEEDED") {
|
|
205
|
+
const out = (s.data && s.data.output) || s.output || {};
|
|
206
|
+
const url = (out.video && out.video.url) || out.url || (Array.isArray(out.video) ? out.video[0] && out.video[0].url : null);
|
|
207
|
+
if (!url) throw new NanoodleError("completed but no video url");
|
|
208
|
+
return url;
|
|
209
|
+
}
|
|
210
|
+
if (["FAILED", "ERROR", "CANCELED"].includes(st)) {
|
|
211
|
+
throw new NanoodleError("video failed: " + ((s.data && s.data.error) || st));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
throw new NanoodleError(`video timed out (${Math.round(this.timeouts.video / 1000)}s) — the job may still be running on NanoGPT's side`, { code: "timeout" });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** POST /api/v1/audio/speech (music + tts + remix). Returns an audio URL (https or data:). */
|
|
218
|
+
async audio(model, input, extra = {}, { onCost, onPoll, signal } = {}) {
|
|
219
|
+
const body = Object.assign({ model, input }, extra);
|
|
220
|
+
const r = await this._postJson("/api/v1/audio/speech", body, signal);
|
|
221
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
222
|
+
const ct = (r.headers && r.headers.get && r.headers.get("content-type")) || "";
|
|
223
|
+
if (ct.includes("application/json")) {
|
|
224
|
+
const j = await r.json();
|
|
225
|
+
if (onCost) onCost(costWithHeaders(j, r));
|
|
226
|
+
let url = j.url || j.audioUrl || (j.data && j.data.url) || (j.data && j.data.audioUrl);
|
|
227
|
+
if (!url && (j.runId || j.id)) url = await this._pollAudio(model, j, { onPoll, signal });
|
|
228
|
+
if (!url) throw new NanoodleError("no audio url in response");
|
|
229
|
+
return url;
|
|
230
|
+
}
|
|
231
|
+
// binary body → the audio bytes; MIME from content-type, pinned from the requested format when generic
|
|
232
|
+
if (onCost) onCost(costFromHeaders(r));
|
|
233
|
+
const bytes = new Uint8Array(await r.arrayBuffer());
|
|
234
|
+
let mime = ct.split(";")[0].trim().toLowerCase();
|
|
235
|
+
if (!mime || mime === "application/octet-stream" || mime === "binary/octet-stream") {
|
|
236
|
+
mime = AUDIO_MIME[extra.response_format || "mp3"] || "audio/mpeg";
|
|
237
|
+
}
|
|
238
|
+
return bytesToDataUrl(bytes, mime);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async _pollAudio(model, j, { onPoll, signal } = {}) {
|
|
242
|
+
const runId = j.runId || j.id;
|
|
243
|
+
const qs = new URLSearchParams({ runId: String(runId), model });
|
|
244
|
+
if (j.cost != null) qs.set("cost", String(j.cost)); // lets the server auto-refund on failure
|
|
245
|
+
if (j.paymentSource) qs.set("paymentSource", String(j.paymentSource));
|
|
246
|
+
if (j.isApiRequest != null) qs.set("isApiRequest", String(j.isApiRequest));
|
|
247
|
+
const t0 = Date.now();
|
|
248
|
+
while (Date.now() - t0 < this.timeouts.audio) {
|
|
249
|
+
await sleep(this.pollIntervals.audio, signal);
|
|
250
|
+
let s;
|
|
251
|
+
try {
|
|
252
|
+
s = await (await this._get("/api/tts/status?" + qs, signal)).json();
|
|
253
|
+
} catch (e) {
|
|
254
|
+
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const st = String(s.status || "").toLowerCase();
|
|
258
|
+
if (onPoll) onPoll({ status: st, elapsedMs: Date.now() - t0, runId, queuePosition: s.queuePosition });
|
|
259
|
+
if (st === "completed" || st === "succeeded") {
|
|
260
|
+
const url = s.audioUrl || s.url || (s.data && s.data.audioUrl) || (s.data && s.data.url);
|
|
261
|
+
if (!url) throw new NanoodleError("completed but no audio url");
|
|
262
|
+
return url;
|
|
263
|
+
}
|
|
264
|
+
if (["error", "failed", "content_policy_violation"].includes(st)) {
|
|
265
|
+
throw new NanoodleError("audio failed: " + (s.error || s.message || st));
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
throw new NanoodleError(`audio timed out (${Math.round(this.timeouts.audio / 1000)}s) — the job may still be running on NanoGPT's side`, { code: "timeout" });
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** POST /api/v1/audio/transcriptions (multipart; the audio form field MUST be "file"). */
|
|
272
|
+
async transcribe(model, audioUrl, language, { onCost, signal } = {}) {
|
|
273
|
+
let bytes, mime;
|
|
274
|
+
if (/^data:/i.test(audioUrl)) {
|
|
275
|
+
({ bytes, mime } = dataUrlBytes(audioUrl));
|
|
276
|
+
} else {
|
|
277
|
+
const r = await this.fetch(audioUrl, { signal }); // media CDN — no auth headers
|
|
278
|
+
if (!r.ok) throw new NanoodleError("couldn't download the audio to transcribe (" + r.status + ")");
|
|
279
|
+
bytes = new Uint8Array(await r.arrayBuffer());
|
|
280
|
+
mime = ((r.headers && r.headers.get && r.headers.get("content-type")) || "audio/mpeg").split(";")[0];
|
|
281
|
+
}
|
|
282
|
+
if (bytes.length > 3.5 * 1024 * 1024) {
|
|
283
|
+
throw new NanoodleError("this clip is too big to transcribe directly (~3.5 MB max) — nanoodle sends audio inline; use a shorter clip");
|
|
284
|
+
}
|
|
285
|
+
const ext = ((mime || "audio/mp3").split("/")[1] || "mp3").split(";")[0];
|
|
286
|
+
const fd = new FormData();
|
|
287
|
+
fd.append("file", new Blob([bytes], { type: mime || "audio/mpeg" }), "audio." + ext);
|
|
288
|
+
fd.append("model", model);
|
|
289
|
+
if (language) fd.append("language", language);
|
|
290
|
+
// no explicit Content-Type — fetch sets the multipart boundary
|
|
291
|
+
const r = await this.fetch(this.baseUrl + "/api/v1/audio/transcriptions", { method: "POST", headers: this._auth(), body: fd, signal });
|
|
292
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
293
|
+
const j = await r.json();
|
|
294
|
+
if (onCost) onCost(costWithHeaders(j, r));
|
|
295
|
+
const txt = j.transcription != null ? j.transcription
|
|
296
|
+
: j.text != null ? j.text
|
|
297
|
+
: j.data && (j.data.transcription != null ? j.data.transcription : j.data.text);
|
|
298
|
+
if (txt == null) throw new NanoodleError("no transcription in response");
|
|
299
|
+
return txt;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Download hosted media (CDN — no auth headers) and inline it as a data: URL.
|
|
304
|
+
* Used when a chat audio part needs base64 bytes but the upstream node produced an https URL.
|
|
305
|
+
*/
|
|
306
|
+
async fetchMediaDataUrl(url, { signal } = {}) {
|
|
307
|
+
const r = await this.fetch(url, { signal });
|
|
308
|
+
if (!r.ok) throw new NanoodleError("couldn't download media to inline (" + r.status + "): " + url);
|
|
309
|
+
const bytes = new Uint8Array(await r.arrayBuffer());
|
|
310
|
+
const ct = (((r.headers && r.headers.get && r.headers.get("content-type")) || "").split(";")[0] || "").trim().toLowerCase();
|
|
311
|
+
const mime = ct && ct !== "application/octet-stream" && ct !== "binary/octet-stream" ? ct : undefined;
|
|
312
|
+
return bytesToDataUrl(bytes, mime); // sniffs magic bytes when the CDN's content-type is generic
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Optional helper: POST /api/check-balance → { usd_balance }. */
|
|
316
|
+
async checkBalance(signal) {
|
|
317
|
+
const r = await this._postJson("/api/check-balance", {}, signal);
|
|
318
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
319
|
+
return r.json();
|
|
320
|
+
}
|
|
321
|
+
}
|
package/src/errors.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Base error for everything nanoodle throws deliberately. */
|
|
2
|
+
export class NanoodleError extends Error {
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} message
|
|
5
|
+
* @param {object} [props] extra fields (e.g. { code: "auth" | "funds" | "http", status })
|
|
6
|
+
*/
|
|
7
|
+
constructor(message, props = {}) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "NanoodleError";
|
|
10
|
+
Object.assign(this, props);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A node in the graph cannot be executed by this library (browser-only media op / unknown type). */
|
|
15
|
+
export class UnsupportedNodeError extends NanoodleError {
|
|
16
|
+
constructor(message, props = {}) {
|
|
17
|
+
super(message, props);
|
|
18
|
+
this.name = "UnsupportedNodeError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* run() rejects with this when a sink (output) node failed.
|
|
24
|
+
* `.result` carries the partial RunResult (successful lanes, per-node errors, cost so far).
|
|
25
|
+
*/
|
|
26
|
+
export class RunError extends NanoodleError {
|
|
27
|
+
constructor(message, result, props = {}) {
|
|
28
|
+
super(message, props);
|
|
29
|
+
this.name = "RunError";
|
|
30
|
+
this.result = result;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/graph.mjs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { NanoodleError } from "./errors.mjs";
|
|
2
|
+
|
|
3
|
+
/* Dynamic input-port families (mirrors the nanoodle app's runGraph). A wire landing on one of
|
|
4
|
+
these ports — or on a port declared in NODE_TYPES[type].inputs — is a data input; a wire
|
|
5
|
+
landing on ANY other port is a field override (wired prompt/system/lyrics/q/...). */
|
|
6
|
+
export const IMG_PORT_RE = /^img\d+$/; // llm / draw vision references
|
|
7
|
+
export const EDIT_IMG_RE = /^image\d*$/; // edit multi-reference: image, image2, ...
|
|
8
|
+
export const VID_PORT_RE = /^vid\d+$/;
|
|
9
|
+
export const CLIP_PORT_RE = /^clip\d+$/; // combine clips
|
|
10
|
+
export const REF_PORT_RE = /^ref\d+$/; // tvideo reference images
|
|
11
|
+
export const FRAME_PORT_RE = /^frame\d+$/; // vframes outputs
|
|
12
|
+
|
|
13
|
+
const DYNAMIC_INPUT_RES = [IMG_PORT_RE, EDIT_IMG_RE, VID_PORT_RE, CLIP_PORT_RE, REF_PORT_RE];
|
|
14
|
+
const DYNAMIC_INPUT_NAMES = new Set(["audio", "endframe"]);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Node-type registry (execution-relevant subset of the app's NODE_TYPES).
|
|
18
|
+
* flags: local (pure logic) | network (calls NanoGPT) | unsupported (browser-only media op) | note.
|
|
19
|
+
*/
|
|
20
|
+
export const NODE_TYPES = {
|
|
21
|
+
text: { title: "Text", inputs: [], outputs: [{ name: "text", type: "text" }], local: true },
|
|
22
|
+
upload: { title: "Image input", inputs: [], outputs: [{ name: "image", type: "image" }], local: true },
|
|
23
|
+
aupload: { title: "Audio input", inputs: [], outputs: [{ name: "audio", type: "audio" }], local: true },
|
|
24
|
+
vupload: { title: "Video input", inputs: [], outputs: [{ name: "video", type: "video" }], local: true },
|
|
25
|
+
choice: { title: "Choice", inputs: [], outputs: [{ name: "text", type: "text" }], local: true },
|
|
26
|
+
join: { title: "Join", inputs: ["a", "b"], outputs: [{ name: "text", type: "text" }], local: true },
|
|
27
|
+
llm: { title: "LLM", inputs: [], outputs: [{ name: "text", type: "text" }], network: true },
|
|
28
|
+
image: { title: "Image", inputs: [], outputs: [{ name: "image", type: "image" }], network: true },
|
|
29
|
+
draw: { title: "Draw", inputs: [], outputs: [{ name: "image", type: "image" }, { name: "text", type: "text" }], network: true },
|
|
30
|
+
edit: { title: "Edit", inputs: [], outputs: [{ name: "image", type: "image" }], network: true },
|
|
31
|
+
inpaint: { title: "Inpaint", inputs: ["image", "mask"], outputs: [{ name: "image", type: "image" }], network: true },
|
|
32
|
+
resize: { title: "Resize / crop", inputs: ["image"], outputs: [{ name: "image", type: "image" }], unsupported: true },
|
|
33
|
+
vision: { title: "Vision", inputs: ["image"], outputs: [{ name: "text", type: "text" }], network: true },
|
|
34
|
+
tvideo: { title: "Text→Video", inputs: [], outputs: [{ name: "video", type: "video" }], network: true },
|
|
35
|
+
ivideo: { title: "Image→Video", inputs: ["image"], outputs: [{ name: "video", type: "video" }], network: true },
|
|
36
|
+
vedit: { title: "Video edit", inputs: ["video"], outputs: [{ name: "video", type: "video" }], network: true },
|
|
37
|
+
vframes: { title: "Video → frames", inputs: ["video"], outputs: [{ name: "frame1", type: "image" }], unsupported: true },
|
|
38
|
+
combine: { title: "Combine videos", inputs: [], outputs: [{ name: "video", type: "video" }], unsupported: true },
|
|
39
|
+
soundtrack: { title: "Soundtrack", inputs: ["video", "audio"], outputs: [{ name: "video", type: "video" }], unsupported: true },
|
|
40
|
+
lipsync: { title: "Avatar / lipsync", inputs: ["image", "audio"], outputs: [{ name: "video", type: "video" }], network: true },
|
|
41
|
+
music: { title: "Music", inputs: [], outputs: [{ name: "audio", type: "audio" }], network: true },
|
|
42
|
+
remix: { title: "Remix audio", inputs: ["audio"], outputs: [{ name: "audio", type: "audio" }], network: true },
|
|
43
|
+
tts: { title: "Speech", inputs: [], outputs: [{ name: "audio", type: "audio" }], network: true },
|
|
44
|
+
trim: { title: "Trim audio", inputs: ["audio"], outputs: [{ name: "audio", type: "audio" }], unsupported: true },
|
|
45
|
+
extractaudio: { title: "Extract audio", inputs: ["video"], outputs: [{ name: "audio", type: "audio" }], unsupported: true },
|
|
46
|
+
transcribe: { title: "Transcribe", inputs: ["audio"], outputs: [{ name: "text", type: "text" }], network: true },
|
|
47
|
+
comment: { title: "Comment", inputs: [], outputs: [], note: true, local: true },
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** Display name: node.name (trimmed) → type title → type → "?". */
|
|
51
|
+
export function displayName(node) {
|
|
52
|
+
const nm = (node.name || "").trim();
|
|
53
|
+
if (nm) return nm;
|
|
54
|
+
const t = NODE_TYPES[node.type];
|
|
55
|
+
return (t && t.title) || node.type || "?";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Is a wire landing on `port` of `node` a data input (vs a field override)? */
|
|
59
|
+
export function isInputPort(node, port) {
|
|
60
|
+
const t = NODE_TYPES[node.type];
|
|
61
|
+
if (t && (t.inputs || []).includes(port)) return true;
|
|
62
|
+
if (DYNAMIC_INPUT_NAMES.has(port)) return true;
|
|
63
|
+
return DYNAMIC_INPUT_RES.some((re) => re.test(port));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Load raw parsed graph JSON into an executable graph (mirrors the app's applyGraphData):
|
|
68
|
+
* - `audio` type aliases to `tts` (legacy saves)
|
|
69
|
+
* - unknown node types are KEPT but flagged (`unknown: true`) + a warning; run() fails fast on them
|
|
70
|
+
* - links are kept only when both endpoints exist
|
|
71
|
+
* - links into music/tts port "text" migrate to "prompt"
|
|
72
|
+
* @returns {{ nodes, links, warnings: string[] }}
|
|
73
|
+
*/
|
|
74
|
+
export function materialize(data) {
|
|
75
|
+
if (!data || typeof data !== "object" || !Array.isArray(data.nodes)) {
|
|
76
|
+
throw new NanoodleError("not a nanoodle graph: expected an object with a nodes array (the noodle-graph.json save)");
|
|
77
|
+
}
|
|
78
|
+
const warnings = [];
|
|
79
|
+
const nodes = [];
|
|
80
|
+
for (const raw of data.nodes) {
|
|
81
|
+
if (!raw || raw.id == null || !raw.type) continue;
|
|
82
|
+
const type = raw.type === "audio" ? "tts" : raw.type;
|
|
83
|
+
const n = { id: String(raw.id), type, name: raw.name, fields: { ...(raw.fields || {}) } };
|
|
84
|
+
if (!NODE_TYPES[type]) {
|
|
85
|
+
n.unknown = true;
|
|
86
|
+
warnings.push(`unknown node type "${raw.type}" (node ${n.id}) — kept, but running this workflow will fail; you may need a newer nanoodle library`);
|
|
87
|
+
} else if (NODE_TYPES[type].unsupported) {
|
|
88
|
+
warnings.push(`node "${displayName(n)}" (${n.id}) has type '${type}', which does local media processing this library can't run — run() will refuse`);
|
|
89
|
+
}
|
|
90
|
+
nodes.push(n);
|
|
91
|
+
}
|
|
92
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
93
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
94
|
+
const links = [];
|
|
95
|
+
for (const l of data.links || []) {
|
|
96
|
+
if (!l || !l.from || !l.to) continue;
|
|
97
|
+
const from = { node: String(l.from.node), port: String(l.from.port) };
|
|
98
|
+
const to = { node: String(l.to.node), port: String(l.to.port) };
|
|
99
|
+
if (!ids.has(from.node) || !ids.has(to.node)) continue;
|
|
100
|
+
const toNode = byId.get(to.node);
|
|
101
|
+
if ((toNode.type === "music" || toNode.type === "tts") && to.port === "text") to.port = "prompt"; // legacy port migration
|
|
102
|
+
links.push({ id: l.id, from, to });
|
|
103
|
+
}
|
|
104
|
+
return { nodes, links, warnings };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Kahn topological sort. Throws naming the cyclic nodes.
|
|
109
|
+
* @returns node array in dependency order
|
|
110
|
+
*/
|
|
111
|
+
export function topoSort(graph) {
|
|
112
|
+
const indeg = new Map(graph.nodes.map((n) => [n.id, 0]));
|
|
113
|
+
const outAdj = new Map(graph.nodes.map((n) => [n.id, []]));
|
|
114
|
+
for (const l of graph.links) {
|
|
115
|
+
indeg.set(l.to.node, (indeg.get(l.to.node) || 0) + 1);
|
|
116
|
+
outAdj.get(l.from.node).push(l.to.node);
|
|
117
|
+
}
|
|
118
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
119
|
+
const queue = graph.nodes.filter((n) => indeg.get(n.id) === 0).map((n) => n.id);
|
|
120
|
+
const order = [];
|
|
121
|
+
while (queue.length) {
|
|
122
|
+
const id = queue.shift();
|
|
123
|
+
order.push(byId.get(id));
|
|
124
|
+
for (const next of outAdj.get(id)) {
|
|
125
|
+
indeg.set(next, indeg.get(next) - 1);
|
|
126
|
+
if (indeg.get(next) === 0) queue.push(next);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (order.length !== graph.nodes.length) {
|
|
130
|
+
const cyclic = graph.nodes.filter((n) => !order.includes(n)).map((n) => `${displayName(n)} (${n.id})`);
|
|
131
|
+
throw new NanoodleError("workflow has a cycle involving: " + cyclic.join(", "));
|
|
132
|
+
}
|
|
133
|
+
return order;
|
|
134
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Workflow, RunResult } from "./workflow.mjs";
|
|
2
|
+
export { NanoodleError, UnsupportedNodeError, RunError } from "./errors.mjs";
|
|
3
|
+
export { MediaRef, mediaFromFile, MEDIA_INLINE_MAX } from "./media.mjs";
|
|
4
|
+
export { NanoClient } from "./client.mjs";
|
|
5
|
+
export { NODE_TYPES, displayName, materialize, topoSort } from "./graph.mjs";
|
|
6
|
+
export { deriveInputs, deriveOutputs, deriveSettings, INPUT_SPECS, SETTING_SPECS } from "./io.mjs";
|