nanoodle 0.8.0 → 0.11.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/README.md +59 -2
- package/bin/nanoodle.mjs +7 -1
- package/package.json +1 -1
- package/src/browser.mjs +1 -1
- package/src/catalog.mjs +19 -0
- package/src/cleanvoice.mjs +104 -0
- package/src/client.mjs +87 -8
- package/src/decide.mjs +160 -0
- package/src/endpoint.mjs +406 -0
- package/src/estimate.mjs +141 -20
- package/src/graph.mjs +18 -2
- package/src/index.mjs +2 -0
- package/src/io.mjs +32 -1
- package/src/local-media.mjs +38 -0
- package/src/media.mjs +3 -0
- package/src/nodes.mjs +205 -21
- package/src/workflow.mjs +155 -18
package/src/endpoint.mjs
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🔌 Custom endpoint — POST a NanoGPT-shaped body to a URL the graph names.
|
|
3
|
+
* Twin of the editor/play helpers (index.html runEndpoint). The request goes
|
|
4
|
+
* straight to that URL: never the NanoGPT client, never the API key.
|
|
5
|
+
* Custom auth is only fields.auth. http is allowed for loopback and LAN;
|
|
6
|
+
* a public host must be https.
|
|
7
|
+
*/
|
|
8
|
+
import { NanoodleError } from "./errors.mjs";
|
|
9
|
+
import { b64ImageMime, bytesToDataUrl } from "./media.mjs";
|
|
10
|
+
|
|
11
|
+
export const ENDPOINT_DEF_URL = "http://127.0.0.1:8787/v1/chat/completions";
|
|
12
|
+
export const ENDPOINT_MODES = ["chat", "image", "video", "audio", "json"];
|
|
13
|
+
|
|
14
|
+
export function endpointMode(n) {
|
|
15
|
+
const m = String((n && n.fields && n.fields.mode) || "chat").toLowerCase();
|
|
16
|
+
return ENDPOINT_MODES.includes(m) ? m : "chat";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function endpointOutPort(n) {
|
|
20
|
+
const m = endpointMode(n);
|
|
21
|
+
if (m === "image") return { name: "image", type: "image" };
|
|
22
|
+
if (m === "video") return { name: "video", type: "video" };
|
|
23
|
+
if (m === "audio") return { name: "audio", type: "audio" };
|
|
24
|
+
return { name: "text", type: "text" };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function endpointIsLoopbackHost(host) {
|
|
28
|
+
const h = String(host || "").toLowerCase().replace(/^\[|\]$/g, "");
|
|
29
|
+
return h === "localhost" || h === "127.0.0.1" || h === "::1";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function endpointIsPrivateIPv4(host) {
|
|
33
|
+
const p = String(host || "").split(".");
|
|
34
|
+
if (p.length !== 4) return false;
|
|
35
|
+
if (!p.every((x) => /^\d+$/.test(x) && +x >= 0 && +x <= 255)) return false;
|
|
36
|
+
const a = +p[0], b = +p[1];
|
|
37
|
+
if (a === 10) return true;
|
|
38
|
+
if (a === 192 && b === 168) return true;
|
|
39
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
40
|
+
if (a === 169 && b === 254) return true;
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function endpointUrlOk(url) {
|
|
45
|
+
const s = String(url || "").trim();
|
|
46
|
+
if (!s) return "URL required — set the custom endpoint URL";
|
|
47
|
+
let u;
|
|
48
|
+
try { u = new URL(s); } catch {
|
|
49
|
+
return "that URL isn’t allowed — use http://localhost, 127.0.0.1, a LAN host, or https";
|
|
50
|
+
}
|
|
51
|
+
if (u.protocol !== "http:" && u.protocol !== "https:") {
|
|
52
|
+
return "that URL isn’t allowed — use http://localhost, 127.0.0.1, a LAN host, or https";
|
|
53
|
+
}
|
|
54
|
+
if (u.username || u.password) {
|
|
55
|
+
return "that URL isn’t allowed — don’t put credentials in the URL; use the Authorization field";
|
|
56
|
+
}
|
|
57
|
+
if (u.protocol === "https:") return true;
|
|
58
|
+
const host = u.hostname;
|
|
59
|
+
if (endpointIsLoopbackHost(host) || endpointIsPrivateIPv4(host) || /\.local$/i.test(host)) return true;
|
|
60
|
+
return "that URL isn’t allowed — http is only for localhost, 127.0.0.1, or a LAN host; use https for a public host";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function endpointHeaders(auth) {
|
|
64
|
+
const headers = { "Content-Type": "application/json" };
|
|
65
|
+
const a = String(auth || "").trim();
|
|
66
|
+
if (a) headers.Authorization = /\s/.test(a) ? a : ("Bearer " + a);
|
|
67
|
+
return headers;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function endpointPrompt(n, inp) {
|
|
71
|
+
inp = inp || {};
|
|
72
|
+
const v = inp.prompt != null ? inp.prompt : (inp.text != null ? inp.text : (n.fields && n.fields.prompt));
|
|
73
|
+
return String(v == null ? "" : v).trim();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function endpointIsPath(s) {
|
|
77
|
+
const raw = String(s == null ? "" : s).trim();
|
|
78
|
+
if (!raw || raw.charAt(0) !== "/") return false;
|
|
79
|
+
if (raw.length < 2) return false;
|
|
80
|
+
if (/[\s\u00b7|\u2022]/.test(raw)) return false;
|
|
81
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) return false;
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function endpointJoinPath(base, path) {
|
|
86
|
+
const u = new URL(String(base || "").trim());
|
|
87
|
+
let p = String(path || "").trim();
|
|
88
|
+
if (p.charAt(0) !== "/") p = "/" + p;
|
|
89
|
+
const q = p.indexOf("?");
|
|
90
|
+
if (q >= 0) { u.pathname = p.slice(0, q); u.search = p.slice(q); }
|
|
91
|
+
else { u.pathname = p; u.search = ""; }
|
|
92
|
+
return u.toString();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function endpointParseRoute(s) {
|
|
96
|
+
const raw = String(s == null ? "" : s).trim();
|
|
97
|
+
const out = {};
|
|
98
|
+
if (!raw) return out;
|
|
99
|
+
const low = raw.toLowerCase();
|
|
100
|
+
if (ENDPOINT_MODES.includes(low)) { out.mode = low; return out; }
|
|
101
|
+
let mode = "", rest = raw;
|
|
102
|
+
for (const prefix of ENDPOINT_MODES) {
|
|
103
|
+
if (!low.startsWith(prefix)) continue;
|
|
104
|
+
const after = raw.slice(prefix.length);
|
|
105
|
+
const ch = after.charAt(0);
|
|
106
|
+
if (ch === "\u00b7" || ch === "|" || ch === ":" || ch === "\u2022" || ch === "-" || /\s/.test(ch)) {
|
|
107
|
+
mode = prefix;
|
|
108
|
+
rest = after.replace(/^[\s\u00b7|\u2022:\-]+/, "");
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
rest = String(rest || "").replace(/\s+\([^)]*\)\s*$/, "").trim();
|
|
113
|
+
let url = "";
|
|
114
|
+
const m = rest.match(/https?:\/\/[^\s)]+/i);
|
|
115
|
+
if (m) url = m[0].replace(/[.,;]+$/, "");
|
|
116
|
+
if (mode) out.mode = mode;
|
|
117
|
+
if (url) out.url = url;
|
|
118
|
+
else if (endpointIsPath(rest)) out.path = rest;
|
|
119
|
+
else if (!mode && endpointIsPath(raw)) out.path = raw;
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function endpointResolveTarget(n, inp) {
|
|
124
|
+
inp = inp || {};
|
|
125
|
+
const hasFieldUrl = n && n.fields && Object.prototype.hasOwnProperty.call(n.fields, "url");
|
|
126
|
+
const fieldUrl = hasFieldUrl ? n.fields.url : ENDPOINT_DEF_URL;
|
|
127
|
+
const urlRaw = (inp.url != null && String(inp.url).trim() !== "") ? inp.url : fieldUrl;
|
|
128
|
+
const modeRaw = (inp.mode != null && String(inp.mode).trim() !== "") ? inp.mode : (n && n.fields && n.fields.mode);
|
|
129
|
+
const parsedUrl = endpointParseRoute(urlRaw);
|
|
130
|
+
const parsedMode = endpointParseRoute(modeRaw);
|
|
131
|
+
const parsedField = endpointParseRoute(fieldUrl);
|
|
132
|
+
const path = parsedUrl.path || parsedMode.path;
|
|
133
|
+
let resolvedUrl = parsedUrl;
|
|
134
|
+
if (path && !parsedUrl.url) {
|
|
135
|
+
const base = parsedField.url || String(fieldUrl == null ? "" : fieldUrl).trim();
|
|
136
|
+
if (base && !endpointIsPath(base)) {
|
|
137
|
+
try { resolvedUrl = { mode: parsedUrl.mode, url: endpointJoinPath(base, path) }; } catch { /* leave the raw path to fail urlOk */ }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
let url = resolvedUrl.url || String(urlRaw == null ? "" : urlRaw).trim();
|
|
141
|
+
let mode;
|
|
142
|
+
if (resolvedUrl.url && resolvedUrl.mode) mode = resolvedUrl.mode;
|
|
143
|
+
else if (parsedMode.url && parsedMode.mode) { url = parsedMode.url; mode = parsedMode.mode; }
|
|
144
|
+
else mode = parsedMode.mode || resolvedUrl.mode || (n && n.fields && n.fields.mode) || "chat";
|
|
145
|
+
mode = ENDPOINT_MODES.includes(String(mode).toLowerCase()) ? String(mode).toLowerCase() : "chat";
|
|
146
|
+
return { url, mode };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function endpointModel(n) {
|
|
150
|
+
const m = String((n.fields && n.fields.model) || "").trim();
|
|
151
|
+
return m || "local";
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function endpointRequestBody(mode, n, inp) {
|
|
155
|
+
inp = inp || {};
|
|
156
|
+
const model = endpointModel(n);
|
|
157
|
+
if (mode === "chat") {
|
|
158
|
+
const prompt = endpointPrompt(n, inp);
|
|
159
|
+
const messages = [];
|
|
160
|
+
const sys = String((n.fields && n.fields.system) || "").trim();
|
|
161
|
+
if (sys) messages.push({ role: "system", content: sys });
|
|
162
|
+
const imgs = inp.image ? (Array.isArray(inp.image) ? inp.image : [inp.image]) : [];
|
|
163
|
+
const aud = inp.audio
|
|
164
|
+
? { type: "input_audio", input_audio: { data: String(inp.audio).replace(/^data:[^,]*,/, ""), format: "wav" } }
|
|
165
|
+
: null;
|
|
166
|
+
if (imgs.length || aud) {
|
|
167
|
+
const parts = [{ type: "text", text: prompt || "" }];
|
|
168
|
+
for (const url of imgs) parts.push({ type: "image_url", image_url: { url } });
|
|
169
|
+
if (aud) parts.push(aud);
|
|
170
|
+
messages.push({ role: "user", content: parts });
|
|
171
|
+
} else {
|
|
172
|
+
messages.push({ role: "user", content: prompt });
|
|
173
|
+
}
|
|
174
|
+
return { model, messages, temperature: 0.8 };
|
|
175
|
+
}
|
|
176
|
+
if (mode === "image") {
|
|
177
|
+
const ib = { model, size: (n.fields && n.fields.size) || "1024x1024", n: 1, response_format: "b64_json" };
|
|
178
|
+
const ip = endpointPrompt(n, inp);
|
|
179
|
+
if (ip) ib.prompt = ip;
|
|
180
|
+
if (inp.image) ib.imageDataUrl = inp.image;
|
|
181
|
+
return ib;
|
|
182
|
+
}
|
|
183
|
+
if (mode === "video") {
|
|
184
|
+
const vb = { model, prompt: endpointPrompt(n, inp) };
|
|
185
|
+
if (inp.image) vb.imageDataUrl = inp.image;
|
|
186
|
+
if (inp.video) {
|
|
187
|
+
if (/^https?:/i.test(inp.video)) vb.videoUrl = inp.video;
|
|
188
|
+
else vb.videoDataUrl = inp.video;
|
|
189
|
+
}
|
|
190
|
+
return vb;
|
|
191
|
+
}
|
|
192
|
+
if (mode === "audio") {
|
|
193
|
+
const ab = { model, input: endpointPrompt(n, inp) };
|
|
194
|
+
if (inp.audio) {
|
|
195
|
+
if (/^https?:/i.test(inp.audio)) ab.audioUrl = inp.audio;
|
|
196
|
+
else ab.audioDataUrl = inp.audio;
|
|
197
|
+
}
|
|
198
|
+
return ab;
|
|
199
|
+
}
|
|
200
|
+
const jb = {};
|
|
201
|
+
if (inp.text != null && inp.text !== "") jb.text = inp.text;
|
|
202
|
+
else {
|
|
203
|
+
const tp = endpointPrompt(n, inp);
|
|
204
|
+
if (tp) jb.text = tp;
|
|
205
|
+
}
|
|
206
|
+
if (inp.image) jb.image = inp.image;
|
|
207
|
+
if (inp.video) jb.video = inp.video;
|
|
208
|
+
if (inp.audio) jb.audio = inp.audio;
|
|
209
|
+
return jb;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function endpointShapeHint(mode) {
|
|
213
|
+
if (mode === "chat") return "OpenAI chat JSON { choices:[{ message:{ content } }] }";
|
|
214
|
+
if (mode === "image") return "{ data:[{ b64_json }] } or { data:[{ url }] }";
|
|
215
|
+
if (mode === "video") return '{ "url" } or NanoGPT { output: { video: { url } } }';
|
|
216
|
+
if (mode === "audio") return '{ "url" } or a binary audio body';
|
|
217
|
+
return '{ "text" } or { "data": ... }';
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function endpointApiMessage(body) {
|
|
221
|
+
try {
|
|
222
|
+
const j = JSON.parse(String(body || "").replace(/^\d{3}:\s*/, ""));
|
|
223
|
+
const err = j && j.error;
|
|
224
|
+
let msg = (typeof err === "string" && err)
|
|
225
|
+
|| (err && typeof err.message === "string" && err.message)
|
|
226
|
+
|| (j && typeof j.message === "string" && j.message)
|
|
227
|
+
|| (j && typeof j.detail === "string" && j.detail)
|
|
228
|
+
|| (j && typeof j.title === "string" && j.title);
|
|
229
|
+
msg = String(msg || "").replace(/\s+/g, " ").trim();
|
|
230
|
+
return msg || null;
|
|
231
|
+
} catch { return null; }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function endpointHttpError(status, body) {
|
|
235
|
+
const extracted = endpointApiMessage(body);
|
|
236
|
+
let hint = "";
|
|
237
|
+
if (status === 404) hint = "check the custom endpoint URL";
|
|
238
|
+
else if (status === 401 || status === 403) hint = "check the Authorization field";
|
|
239
|
+
else if (status === 405) hint = "this endpoint must accept POST";
|
|
240
|
+
else if (status === 413) hint = "payload too large; send a smaller body";
|
|
241
|
+
else if (status === 415) hint = "send JSON (Content-Type: application/json)";
|
|
242
|
+
if (extracted) return extracted + (hint ? " — " + hint : "");
|
|
243
|
+
if (/<html|<body|<!doctype/i.test(String(body || ""))) {
|
|
244
|
+
return status + " — the URL returned a web page, not JSON; check the custom endpoint URL";
|
|
245
|
+
}
|
|
246
|
+
if (hint) return status + " — " + hint;
|
|
247
|
+
if (!String(body || "").replace(/\s+/g, "").trim()) {
|
|
248
|
+
return status + " — the endpoint returned an error with no body; check the URL and mode";
|
|
249
|
+
}
|
|
250
|
+
if (/^\s*[{\[]/.test(String(body || ""))) {
|
|
251
|
+
return status + " — the endpoint rejected the request (check URL, mode, and the posted JSON)";
|
|
252
|
+
}
|
|
253
|
+
return status + ": " + String(body).replace(/\s+/g, " ").trim().slice(0, 160);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function endpointNotJsonError(mode, ct, raw) {
|
|
257
|
+
const sample = String(raw || "").replace(/\s+/g, " ").trim().slice(0, 80);
|
|
258
|
+
if (/<html|<body|<!doctype/i.test(raw || "") || /text\/html/i.test(ct || "")) {
|
|
259
|
+
return "the endpoint returned a web page, not JSON — check the URL (it must POST " + endpointShapeHint(mode) + ")";
|
|
260
|
+
}
|
|
261
|
+
return "response is not JSON — return " + endpointShapeHint(mode) + (sample ? " (got: " + sample + ")" : "");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function endpointParseChat(j) {
|
|
265
|
+
const msg = (j && j.choices && j.choices[0] && j.choices[0].message) || {};
|
|
266
|
+
const txt = msg.content;
|
|
267
|
+
if (txt == null) throw new NanoodleError("no text in response — return " + endpointShapeHint("chat"));
|
|
268
|
+
return { text: typeof txt === "string" ? txt : txt.map((p) => p.text || "").join("") };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function endpointParseImage(j) {
|
|
272
|
+
const urls = ((j && j.data) || []).map((d) => {
|
|
273
|
+
if (d.b64_json) return "data:" + b64ImageMime(d.b64_json) + ";base64," + d.b64_json;
|
|
274
|
+
return d.url;
|
|
275
|
+
}).filter(Boolean);
|
|
276
|
+
if (!urls.length) throw new NanoodleError("no image in response — return " + endpointShapeHint("image"));
|
|
277
|
+
return { image: urls[0], images: urls };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function endpointParseVideo(j) {
|
|
281
|
+
const out = (j && j.data && j.data.output) || (j && j.output) || {};
|
|
282
|
+
const url = (j && (j.url || j.videoUrl))
|
|
283
|
+
|| (out.video && out.video.url)
|
|
284
|
+
|| out.url
|
|
285
|
+
|| (Array.isArray(out.video) ? (out.video[0] && out.video[0].url) : null)
|
|
286
|
+
|| (j && j.data && j.data.url);
|
|
287
|
+
if (!url) throw new NanoodleError('no video url in response — return { "url" } or NanoGPT { output: { video: { url } } }');
|
|
288
|
+
return { video: url };
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function endpointParseAudioJson(j) {
|
|
292
|
+
const url = (j && (j.url || j.audioUrl)) || (j && j.data && (j.data.url || j.data.audioUrl));
|
|
293
|
+
if (!url) throw new NanoodleError('no audio url in response — return { "url" } or a binary audio body');
|
|
294
|
+
return { audio: url };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function endpointLooksLikeEcho(j) {
|
|
298
|
+
if (!j || typeof j !== "object") return false;
|
|
299
|
+
const h = j.headers;
|
|
300
|
+
if (!h || typeof h !== "object" || Array.isArray(h)) return false;
|
|
301
|
+
if (!Object.prototype.hasOwnProperty.call(j, "data") && !Object.prototype.hasOwnProperty.call(j, "json")) return false;
|
|
302
|
+
return j.url != null || j.origin != null || j.method != null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function endpointJsonModeText(v) {
|
|
306
|
+
if (typeof v === "string") {
|
|
307
|
+
const s = v.trim();
|
|
308
|
+
if (s) {
|
|
309
|
+
try {
|
|
310
|
+
const p = JSON.parse(s);
|
|
311
|
+
if (p && typeof p === "object") return endpointJsonModeText(p);
|
|
312
|
+
} catch { /* plain text */ }
|
|
313
|
+
}
|
|
314
|
+
return v;
|
|
315
|
+
}
|
|
316
|
+
if (v && typeof v === "object") {
|
|
317
|
+
if (endpointLooksLikeEcho(v)) {
|
|
318
|
+
const inner = (Object.prototype.hasOwnProperty.call(v, "json") && v.json != null) ? v.json : v.data;
|
|
319
|
+
if (inner !== v) return endpointJsonModeText(inner);
|
|
320
|
+
}
|
|
321
|
+
if (v.text != null) return String(v.text);
|
|
322
|
+
try { return JSON.stringify(v, null, 2); } catch { return String(v); }
|
|
323
|
+
}
|
|
324
|
+
return v == null ? "" : String(v);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function endpointParseJsonMode(j) {
|
|
328
|
+
if (j && j.text != null && !endpointLooksLikeEcho(j)) return { text: String(j.text) };
|
|
329
|
+
if (endpointLooksLikeEcho(j)) {
|
|
330
|
+
return { text: endpointJsonModeText((Object.prototype.hasOwnProperty.call(j, "json") && j.json != null) ? j.json : j.data) };
|
|
331
|
+
}
|
|
332
|
+
if (j && Object.prototype.hasOwnProperty.call(j, "data")) return { text: endpointJsonModeText(j.data) };
|
|
333
|
+
throw new NanoodleError('json mode expected { "text" } or { "data": ... } — not a chat/completions wrapper');
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function endpointParseResponse(mode, r) {
|
|
337
|
+
const ct = (r.headers && r.headers.get && r.headers.get("content-type")) || "";
|
|
338
|
+
if (mode === "audio" && !/json/i.test(ct)) {
|
|
339
|
+
const bytes = new Uint8Array(await r.arrayBuffer());
|
|
340
|
+
let mime = ct.split(";")[0].trim().toLowerCase();
|
|
341
|
+
if (!mime || mime === "application/octet-stream" || mime === "binary/octet-stream") mime = "audio/mpeg";
|
|
342
|
+
return { audio: bytesToDataUrl(bytes, mime) };
|
|
343
|
+
}
|
|
344
|
+
let raw = "";
|
|
345
|
+
try { raw = await r.text(); } catch { raw = ""; }
|
|
346
|
+
if (!String(raw).trim()) {
|
|
347
|
+
throw new NanoodleError("empty response — the endpoint returned no body; check the URL and mode");
|
|
348
|
+
}
|
|
349
|
+
let j;
|
|
350
|
+
try { j = JSON.parse(raw); } catch {
|
|
351
|
+
throw new NanoodleError(endpointNotJsonError(mode, ct, raw));
|
|
352
|
+
}
|
|
353
|
+
if (mode === "chat") return endpointParseChat(j);
|
|
354
|
+
if (mode === "image") return endpointParseImage(j);
|
|
355
|
+
if (mode === "video") return endpointParseVideo(j);
|
|
356
|
+
if (mode === "audio") return endpointParseAudioJson(j);
|
|
357
|
+
return endpointParseJsonMode(j);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function endpointUrlIsLocal(url) {
|
|
361
|
+
try {
|
|
362
|
+
const u = new URL(String(url || "").trim());
|
|
363
|
+
const host = u.hostname;
|
|
364
|
+
return endpointIsLoopbackHost(host) || endpointIsPrivateIPv4(host) || /\.local$/i.test(host);
|
|
365
|
+
} catch { return false; }
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function endpointFetchIsOpaque(e) {
|
|
369
|
+
const m = ((e && e.message) || String(e)).trim();
|
|
370
|
+
if (/^(TypeError: )?(Failed to fetch|Load failed|NetworkError)/i.test(m)) return true;
|
|
371
|
+
return !!(e && e.name === "TypeError" && /fetch|network|cors/i.test(m));
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function endpointFetchError(e, url) {
|
|
375
|
+
if (!endpointFetchIsOpaque(e)) return (e && e.message) || String(e);
|
|
376
|
+
if (!endpointUrlIsLocal(url)) return "blocked by CORS — your server needs Access-Control-Allow-Origin";
|
|
377
|
+
return "blocked — CORS (Access-Control-Allow-Origin + OPTIONS) or Chrome local-network permission";
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* POST the mode's body and parse the reply.
|
|
382
|
+
* @param {{ fetch?: typeof fetch, signal?: AbortSignal }} [io]
|
|
383
|
+
*/
|
|
384
|
+
export async function runEndpoint(n, inp, io = {}) {
|
|
385
|
+
const target = endpointResolveTarget(n, inp || {});
|
|
386
|
+
const url = target.url;
|
|
387
|
+
const ok = endpointUrlOk(url);
|
|
388
|
+
if (ok !== true) throw new NanoodleError(ok);
|
|
389
|
+
const mode = target.mode;
|
|
390
|
+
const body = endpointRequestBody(mode, n, inp || {});
|
|
391
|
+
const headers = endpointHeaders(n.fields && n.fields.auth);
|
|
392
|
+
const doFetch = io.fetch || globalThis.fetch;
|
|
393
|
+
let r;
|
|
394
|
+
try {
|
|
395
|
+
r = await doFetch(url, { method: "POST", headers, body: JSON.stringify(body), signal: io.signal });
|
|
396
|
+
} catch (e) {
|
|
397
|
+
if (e && e.name === "AbortError") throw e;
|
|
398
|
+
throw new NanoodleError(endpointFetchError(e, url));
|
|
399
|
+
}
|
|
400
|
+
if (!r.ok) {
|
|
401
|
+
let errBody = "";
|
|
402
|
+
try { errBody = (await r.text()).slice(0, 800); } catch { /* ignore */ }
|
|
403
|
+
throw new NanoodleError(endpointHttpError(r.status, errBody));
|
|
404
|
+
}
|
|
405
|
+
return endpointParseResponse(mode, r);
|
|
406
|
+
}
|
package/src/estimate.mjs
CHANGED
|
@@ -17,15 +17,37 @@
|
|
|
17
17
|
// very first request, instead of a flat guess.
|
|
18
18
|
|
|
19
19
|
import { REF_PORT_RE } from "./graph.mjs";
|
|
20
|
+
import { pricingAdvertisesRefs } from "./catalog.mjs";
|
|
21
|
+
import { cleanVoiceEstSeconds, CLEANVOICE_DEFAULT_MODEL } from "./cleanvoice.mjs";
|
|
22
|
+
|
|
23
|
+
const MODEL3D_DEFAULT_MODEL = "tripo3d/v2.5";
|
|
24
|
+
|
|
25
|
+
/** Model id the run will actually send: the field, or the runner's default when the field is blank. */
|
|
26
|
+
function pricedModelId(node) {
|
|
27
|
+
const id = node.fields && String(node.fields.model || "").trim();
|
|
28
|
+
if (id) return id;
|
|
29
|
+
if (node.type === "model3d") return MODEL3D_DEFAULT_MODEL;
|
|
30
|
+
if (node.type === "cleanvoice") return CLEANVOICE_DEFAULT_MODEL;
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
20
33
|
|
|
21
34
|
// node type → pricing "kind" == which catalog it's priced from + which unit fn.
|
|
22
35
|
// Mirrors the app's SETTING_MODEL_KIND. This set is exactly the billable
|
|
23
36
|
// (network) node types; a type absent here is a free local node and costs $0.
|
|
37
|
+
//
|
|
38
|
+
// The retired `draw` type is deliberately absent. It priced as "chat" until
|
|
39
|
+
// 2026-07-22, when NanoGPT dropped the gemini-omni chat-image contract. The
|
|
40
|
+
// loader now treats `draw` as an unknown type: it warns on load and throws
|
|
41
|
+
// UnsupportedNodeError at run(), before any network call. So a `draw` node in a
|
|
42
|
+
// legacy graph never spends, and pricing it would quote a caller for a call
|
|
43
|
+
// that cannot happen. It contributes $0 and is not counted as `unpriced`,
|
|
44
|
+
// because "we could not price it" is the wrong report — it is not billable.
|
|
24
45
|
const PRICE_KIND = {
|
|
25
|
-
llm: "chat", vision: "chat",
|
|
46
|
+
llm: "chat", vision: "chat", decide: "chat",
|
|
26
47
|
image: "image", edit: "image", inpaint: "image",
|
|
27
48
|
tvideo: "video", ivideo: "video", vedit: "video", lipsync: "video",
|
|
28
|
-
music: "audio", remix: "audio", tts: "audio", transcribe: "audio",
|
|
49
|
+
music: "audio", remix: "audio", tts: "audio", transcribe: "audio", cleanvoice: "audio",
|
|
50
|
+
model3d: "model3d",
|
|
29
51
|
};
|
|
30
52
|
|
|
31
53
|
// Fallback assumptions when a run's real quantity isn't knowable up front.
|
|
@@ -58,18 +80,71 @@ function pickDur(p, raw, fields) {
|
|
|
58
80
|
return 5;
|
|
59
81
|
}
|
|
60
82
|
|
|
83
|
+
// A LoRA row counts only once it carries a URL. loraParams sends nothing for a
|
|
84
|
+
// blank row, so a blank row must not flip the estimate onto the LoRA tier.
|
|
85
|
+
function videoLoraOn(fields) {
|
|
86
|
+
const f = fields || {};
|
|
87
|
+
const rows = Array.isArray(f.loras) ? f.loras : (String(f.loraUrl || "").trim() ? [{ url: f.loraUrl }] : []);
|
|
88
|
+
return rows.some((r) => r && String(r.url || "").trim() !== "");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Untouched audio switch: modelOpts omits the key. The checkbox paints the catalog
|
|
92
|
+
// default and only writes it once the user flips it; the generate-video body omits
|
|
93
|
+
// it the same way. NanoGPT then bills that catalog default (42 models default on).
|
|
94
|
+
// An explicit false stays on the silent tier. No param descriptor stays off.
|
|
95
|
+
function videoAudioOn(fields, audioCtx) {
|
|
96
|
+
const f = fields || {};
|
|
97
|
+
const opts = (f.modelOpts && typeof f.modelOpts === "object") ? f.modelOpts : {};
|
|
98
|
+
let explicit = false;
|
|
99
|
+
for (const k in opts) {
|
|
100
|
+
if (/audio/i.test(k) && !/^(no|disable|without|mute)/i.test(k)) {
|
|
101
|
+
explicit = true;
|
|
102
|
+
const val = opts[k];
|
|
103
|
+
if (val === true || val === "true" || val === 1 || val === "1" || val === "on" || val === "yes") return true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (explicit) return false;
|
|
107
|
+
const ctx = audioCtx || {}, pp = ctx.params || {}, dflt = ctx.defaults || {};
|
|
108
|
+
for (const k of Object.keys(pp)) {
|
|
109
|
+
const d = pp[k];
|
|
110
|
+
if (!d || typeof d !== "object") continue;
|
|
111
|
+
if (d.type !== "switch" && d.type !== "boolean") continue;
|
|
112
|
+
if (!/audio/i.test(k) || /^(no|disable|without|mute)/i.test(k)) continue;
|
|
113
|
+
const def = dflt[k] != null ? dflt[k] : d.default;
|
|
114
|
+
return def === true || def === "true" || def === 1 || def === "1" || def === "on" || def === "yes";
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
|
|
61
119
|
// A video run bills a HIGHER tier when reference images are wired or the model's
|
|
62
120
|
// audio switch is on. refWired comes from the graph wiring; the audio switch /
|
|
63
|
-
// reference mode ride in fields.modelOpts.
|
|
64
|
-
|
|
121
|
+
// reference mode ride in fields.modelOpts. refWired may be a boolean (legacy
|
|
122
|
+
// "any refs?") or a count of wired refN ports. audioCtx is
|
|
123
|
+
// { params, defaults } from the catalog's supported_parameters — an untouched
|
|
124
|
+
// switch uses that default instead of assuming audio off.
|
|
125
|
+
function videoUnitUsd(pricing, fields, refWired, audioCtx) {
|
|
65
126
|
const p = pricing || {};
|
|
66
127
|
const f = fields || {};
|
|
67
128
|
const opts = (f.modelOpts && typeof f.modelOpts === "object") ? f.modelOpts : {};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const refOn =
|
|
71
|
-
|
|
72
|
-
if (usd != null && isFinite(usd) && audioOn && p.audio_multiplier != null)
|
|
129
|
+
const audioOn = videoAudioOn(f, audioCtx);
|
|
130
|
+
const refCount = typeof refWired === "number" ? Math.max(0, refWired | 0) : (refWired ? 1 : 0);
|
|
131
|
+
const refOn = refCount > 0 || /reference/i.test(String(opts.mode || ""));
|
|
132
|
+
let usd = videoBaseUsd(p, f, audioOn, refOn);
|
|
133
|
+
if (usd != null && isFinite(usd) && audioOn && p.audio_multiplier != null) usd *= (_num(p.audio_multiplier) ?? 1);
|
|
134
|
+
// MiniMax H3 LoRA: each wired reference image/audio adds
|
|
135
|
+
// pricing.lora.reference_image_or_audio ($0.02). This number is the quote,
|
|
136
|
+
// not the hold. x402 settlement on POST /api/generate-video (402
|
|
137
|
+
// payment.amountUsd, nothing runs) matches it with no added headroom:
|
|
138
|
+
// LoRA 5s 480p $0.25, +1 ref $0.32, +2 $0.34. GET /api/estimate-video-cost
|
|
139
|
+
// ignores lora/ref fields and stays on the non-LoRA floor ($0.65); this
|
|
140
|
+
// forecast does not. nanoodle-mcp's deposit (gate priceFor:
|
|
141
|
+
// ceil(basis × 1.2 × 2) for inexact video, 1.25× when exact) is applied by
|
|
142
|
+
// that caller on top of this usd and is intentionally left unchanged.
|
|
143
|
+
if (usd != null && isFinite(usd) && refCount > 0 && videoLoraOn(f)) {
|
|
144
|
+
const tier = (p.lora && typeof p.lora === "object") ? p.lora : null;
|
|
145
|
+
const per = tier && _num(tier.reference_image_or_audio);
|
|
146
|
+
if (per != null) usd += per * refCount;
|
|
147
|
+
}
|
|
73
148
|
return usd;
|
|
74
149
|
}
|
|
75
150
|
function videoBaseUsd(pricing, fields, audioOn, refOn) {
|
|
@@ -139,6 +214,18 @@ function videoBaseUsd(pricing, fields, audioOn, refOn) {
|
|
|
139
214
|
if (mp != null) return _num(raw.pricePerMegapixel) * mp * nf;
|
|
140
215
|
}
|
|
141
216
|
}
|
|
217
|
+
// MiniMax H3 with a LoRA URL. Without one, fall through: non-LoRA H3 stays
|
|
218
|
+
// on genericScanUsd (output_per_second × duration → 5s $0.65) and every
|
|
219
|
+
// other shape keeps its existing handler. A LoRA URL switches to
|
|
220
|
+
// lora.text_or_image_per_second by resolution, or lora.reference_per_second
|
|
221
|
+
// once refs are wired. The per-ref surcharge is added in videoUnitUsd.
|
|
222
|
+
if (p.output_per_second != null && p.lora && typeof p.lora === "object" && videoLoraOn(f)) {
|
|
223
|
+
const L = p.lora;
|
|
224
|
+
const billSecs = (d, lo, hi) => Math.min(_num(hi) ?? d, Math.max(_num(lo) ?? d, d));
|
|
225
|
+
const tbl = (refOn && L.reference_per_second) ? L.reference_per_second : L.text_or_image_per_second;
|
|
226
|
+
v = (tbl && typeof tbl === "object") ? rp(tbl) : _num(tbl);
|
|
227
|
+
if (v != null) return v * billSecs(dur, L.min_duration, L.max_duration);
|
|
228
|
+
}
|
|
142
229
|
return genericScanUsd(p, dur);
|
|
143
230
|
}
|
|
144
231
|
// Last resort so a never-seen pricing shape estimates SOMETHING rather than
|
|
@@ -168,6 +255,11 @@ function audioUnitUsd(pricing, chars, seconds) {
|
|
|
168
255
|
if (p.per_prompt_char_block != null) { const bs = _num(p.prompt_char_block_size) || 1; return Math.max(Math.ceil(c / bs) * _num(p.per_prompt_char_block), _num(p.minimum) || 0); }
|
|
169
256
|
if (p.per_generation != null) return _num(p.per_generation);
|
|
170
257
|
if (p.per_second != null) return Math.max(_num(p.per_second) * secs, _num(p.minimum) || 0);
|
|
258
|
+
// per started billing interval (ElevenLabs Music / VEED Clean Audio: $/started minute)
|
|
259
|
+
if (p.per_billing_interval != null) {
|
|
260
|
+
const iv = _num(p.billing_interval_seconds) || 60;
|
|
261
|
+
return Math.max(Math.ceil(secs / iv) * _num(p.per_billing_interval), _num(p.minimum) || 0);
|
|
262
|
+
}
|
|
171
263
|
if (p.per_minute != null) return _num(p.per_minute) * EST.sttMinutes;
|
|
172
264
|
return null;
|
|
173
265
|
}
|
|
@@ -199,28 +291,51 @@ export function graphModelKinds(graph) {
|
|
|
199
291
|
// only to detect wired reference-image ports (video ref tier).
|
|
200
292
|
function nodeUnitUsd(node, catItem, graph) {
|
|
201
293
|
const kind = PRICE_KIND[node.type]; if (!kind) return null;
|
|
202
|
-
const id = node
|
|
294
|
+
const id = pricedModelId(node); if (!id) return null;
|
|
203
295
|
const m = catItem; if (!m) return null;
|
|
204
296
|
const pricing = m.pricing, f = node.fields || {};
|
|
205
297
|
if (kind === "image") {
|
|
206
298
|
const sp = m.supported_parameters || {}, maxOut = sp.max_output_images || 1, fixed = sp.fixed_image_count || 0;
|
|
207
|
-
const count = fixed >
|
|
299
|
+
const count = fixed > 1 ? fixed : Math.min(maxOut, Math.max(1, parseInt(f.variations, 10) || 1)); // fixed>1 = the model dictates the count; fixed 1 falls through to the variations clamp
|
|
208
300
|
return imageUsd(pricing, f.size, count);
|
|
209
301
|
}
|
|
210
302
|
if (kind === "video") {
|
|
211
303
|
const sp = m.supported_parameters || {}, pp = sp.parameters || sp;
|
|
212
|
-
const modelHasRefs = ["reference_images", "reference_image_urls", "referenceImages"].some((k) => k in pp)
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
304
|
+
const modelHasRefs = ["reference_images", "reference_image_urls", "referenceImages"].some((k) => k in pp) ||
|
|
305
|
+
pricingAdvertisesRefs(pricing); // twin of modelRefSpec: refs the run WILL send must forecast at the ref tier
|
|
306
|
+
// Count, not a boolean: H3 LoRA adds reference_image_or_audio once per wired refN port.
|
|
307
|
+
const refCount = (modelHasRefs && graph && Array.isArray(graph.links))
|
|
308
|
+
? graph.links.filter((l) => l.to && l.to.node === node.id && REF_PORT_RE.test(l.to.port)).length
|
|
309
|
+
: 0;
|
|
310
|
+
const audioCtx = { params: (sp.parameters && typeof sp.parameters === "object") ? sp.parameters : {}, defaults: sp.defaults || {} };
|
|
311
|
+
const u = videoUnitUsd(pricing, f, refCount, audioCtx);
|
|
216
312
|
return (u != null && isFinite(u)) ? u : null;
|
|
217
313
|
}
|
|
314
|
+
if (kind === "model3d") return _num(pricing && pricing.per_run);
|
|
315
|
+
if (node.type === "decide") {
|
|
316
|
+
// input tokens only (twin of the editor's Decide estimate); pick asks twice (in order + reversed)
|
|
317
|
+
const imgs = (graph && Array.isArray(graph.links))
|
|
318
|
+
? graph.links.filter((l) => l.to && l.to.node === node.id && /^img\d+$/.test(l.to.port)).length : 0;
|
|
319
|
+
const inTok = 120 + Math.round(((f.question || "").length + (f.options || "").length + (f.levels || "").length) / 4) + 250 + imgs * 400;
|
|
320
|
+
const u = chatUnitUsd(pricing, inTok, 0);
|
|
321
|
+
const pick = !(f.mode === "choose" || f.mode === "score" || f.mode === "yesno");
|
|
322
|
+
return u == null ? null : u * (pick ? 2 : 1);
|
|
323
|
+
}
|
|
218
324
|
if (kind === "chat") {
|
|
219
325
|
const inTok = Math.max(200, Math.round(((f.system || "").length + (f.prompt || "").length) / 4) + 200);
|
|
220
326
|
const outTok = f.maxTokens ? +f.maxTokens : EST.llmOutTokens;
|
|
221
327
|
return chatUnitUsd(pricing, inTok, outTok);
|
|
222
328
|
}
|
|
223
329
|
if (kind === "audio") {
|
|
330
|
+
if (node.type === "cleanvoice") {
|
|
331
|
+
// Bills the wired source's duration knob (else 30 s). per_minute models ignore seconds.
|
|
332
|
+
const link = graph && Array.isArray(graph.links)
|
|
333
|
+
? graph.links.find((l) => l.to && l.to.node === node.id && (l.to.port === "audio" || l.to.port === "video"))
|
|
334
|
+
: null;
|
|
335
|
+
const src = link && Array.isArray(graph.nodes) ? graph.nodes.find((x) => x.id === link.from.node) : null;
|
|
336
|
+
const u = audioUnitUsd(pricing, undefined, cleanVoiceEstSeconds(src && src.fields));
|
|
337
|
+
return (u != null && isFinite(u)) ? u : null;
|
|
338
|
+
}
|
|
224
339
|
const chars = node.type === "transcribe" ? undefined : ((f.prompt || "").length || EST.ttsChars);
|
|
225
340
|
const secs = audioBilledSeconds(m.supported_parameters, f);
|
|
226
341
|
const u = audioUnitUsd(pricing, chars, secs);
|
|
@@ -233,7 +348,7 @@ function nodeUnitUsd(node, catItem, graph) {
|
|
|
233
348
|
* Forecast a graph's per-run cost from the public catalog.
|
|
234
349
|
*
|
|
235
350
|
* @param {{nodes:Array, links?:Array}} graph a materialized graph (Workflow#graph)
|
|
236
|
-
* @param {{chat?:Array, image?:Array, video?:Array, audio?:Array}} catalogs
|
|
351
|
+
* @param {{chat?:Array, image?:Array, video?:Array, audio?:Array, model3d?:Array}} catalogs
|
|
237
352
|
* raw catalog arrays (as /api/v1/*-models return them, `.data`), indexed here by model id
|
|
238
353
|
* @returns {{usd:number, exact:boolean, priced:number, unpriced:number}}
|
|
239
354
|
* usd = sum over priced billable nodes; exact = every priced node was image
|
|
@@ -242,7 +357,7 @@ function nodeUnitUsd(node, catItem, graph) {
|
|
|
242
357
|
export function estimateGraphCost(graph, catalogs = {}) {
|
|
243
358
|
const nodes = (graph && Array.isArray(graph.nodes)) ? graph.nodes : [];
|
|
244
359
|
const byKind = {};
|
|
245
|
-
for (const kind of ["chat", "image", "video", "audio"]) {
|
|
360
|
+
for (const kind of ["chat", "image", "video", "audio", "model3d"]) {
|
|
246
361
|
const arr = Array.isArray(catalogs[kind]) ? catalogs[kind] : [];
|
|
247
362
|
const map = new Map();
|
|
248
363
|
for (const m of arr) if (m && m.id != null) map.set(String(m.id), m);
|
|
@@ -251,12 +366,18 @@ export function estimateGraphCost(graph, catalogs = {}) {
|
|
|
251
366
|
let usd = 0, priced = 0, unpriced = 0, exact = true;
|
|
252
367
|
for (const n of nodes) {
|
|
253
368
|
const kind = PRICE_KIND[n.type]; if (!kind) continue; // free/local node
|
|
254
|
-
const id = n
|
|
255
|
-
const m = id
|
|
369
|
+
const id = pricedModelId(n);
|
|
370
|
+
const m = id ? byKind[kind].get(String(id)) : null;
|
|
256
371
|
const u = m ? nodeUnitUsd(n, m, graph) : null;
|
|
257
372
|
if (u == null || !isFinite(u)) { unpriced++; continue; }
|
|
258
373
|
usd += u; priced++;
|
|
259
|
-
if (kind
|
|
374
|
+
if (kind === "model3d") {
|
|
375
|
+
const by = m.pricing && m.pricing.per_run_by_variant;
|
|
376
|
+
if (by && typeof by === "object") {
|
|
377
|
+
const vals = Object.values(by).map(_num).filter((v) => v != null);
|
|
378
|
+
if (vals.length && Math.min(...vals) !== Math.max(...vals)) exact = false;
|
|
379
|
+
}
|
|
380
|
+
} else if (kind !== "image") exact = false;
|
|
260
381
|
}
|
|
261
382
|
return { usd, exact, priced, unpriced };
|
|
262
383
|
}
|