nanoodle 0.2.0 → 0.4.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 +72 -9
- package/bin/nanoodle.mjs +33 -5
- package/package.json +5 -1
- package/src/browser.mjs +38 -0
- package/src/catalog.mjs +22 -0
- package/src/client.mjs +116 -17
- package/src/graph.mjs +25 -9
- package/src/index.mjs +7 -2
- package/src/io.mjs +42 -2
- package/src/local-media.mjs +998 -0
- package/src/media.mjs +68 -16
- package/src/mp4cat.mjs +284 -0
- package/src/nodes.mjs +253 -37
- package/src/qr.mjs +385 -0
- package/src/share.mjs +20 -14
- package/src/workflow.mjs +102 -18
- package/src/x402.mjs +77 -0
- package/src/zlib.mjs +44 -0
package/README.md
CHANGED
|
@@ -9,6 +9,19 @@ Zero runtime dependencies. Library + CLI in one install.
|
|
|
9
9
|
|
|
10
10
|
Looking for Python? → **[nanoodle-py](https://github.com/nanoodlecom/nanoodle-py)**
|
|
11
11
|
|
|
12
|
+
## Which repo do I want?
|
|
13
|
+
|
|
14
|
+
| I want to… | Go to |
|
|
15
|
+
|---|---|
|
|
16
|
+
| Design workflows in the browser | [nanoodle.com](https://nanoodle.com) |
|
|
17
|
+
| Run graphs from Node code or the CLI | **this package** |
|
|
18
|
+
| Run graphs from Python | [nanoodle-py](https://github.com/nanoodlecom/nanoodle-py) |
|
|
19
|
+
| Serve saved graphs as MCP tools | [nanoodle-mcp](https://github.com/nanoodlecom/nanoodle-mcp) |
|
|
20
|
+
| Run graphs in GitHub Actions CI | [run-noodle-action](https://github.com/nanoodlecom/run-noodle-action) |
|
|
21
|
+
| Teach an agent to *build* graphs | [nanoodle-skill](https://github.com/nanoodlecom/nanoodle-skill) |
|
|
22
|
+
| Prebuilt one-task agent skills | [noodle-skills](https://github.com/nanoodlecom/noodle-skills) |
|
|
23
|
+
| Browse ready-to-open graphs | [awesome-noodles](https://github.com/nanoodlecom/awesome-noodles) |
|
|
24
|
+
|
|
12
25
|
## At a glance
|
|
13
26
|
|
|
14
27
|

|
|
@@ -33,6 +46,10 @@ npm install nanoodle # library + CLI
|
|
|
33
46
|
npx nanoodle --help # or run the CLI without installing
|
|
34
47
|
```
|
|
35
48
|
|
|
49
|
+
Latest on npm is **0.2.0**; accountless `--pay` (x402) and the local-media
|
|
50
|
+
nodes below need 0.4 — until it ships, install from this repo
|
|
51
|
+
(`npm install github:nanoodlecom/nanoodle-js`).
|
|
52
|
+
|
|
36
53
|
## Quickstart (library)
|
|
37
54
|
|
|
38
55
|
```js
|
|
@@ -73,6 +90,7 @@ progress lines to stderr; exit code `0` on success, `1` on failure.
|
|
|
73
90
|
- `--input k=@path` — read a file (media as media; `.txt` / `.md` / `.json` as text)
|
|
74
91
|
- `--set k=v` — override a setting (`n3.model=flux-pro`)
|
|
75
92
|
- `--out dir` — where media outputs land (default `./noodle-out`)
|
|
93
|
+
- `--timeout ms` — overall run timeout
|
|
76
94
|
- `--json` — quiet mode: skip the stderr progress lines (the JSON summary is printed either way)
|
|
77
95
|
- `--env-file path` — load `NANOGPT_API_KEY` from a `.env`-style file (`--key` wins if both are set)
|
|
78
96
|
|
|
@@ -135,23 +153,22 @@ const result = await wf.run(
|
|
|
135
153
|
`run()` rejects with `RunError` when an output (sink) node fails —
|
|
136
154
|
`err.result` still has partial results, per-node statuses, and cost so far.
|
|
137
155
|
Failures in lanes no output depends on only appear in `result.errors`.
|
|
138
|
-
Unknown
|
|
139
|
-
|
|
156
|
+
Unknown node types, missing required inputs, bad keys, and a missing API key
|
|
157
|
+
all fail **before** anything is spent.
|
|
140
158
|
|
|
141
159
|
## Supported nodes
|
|
142
160
|
|
|
143
161
|
| runs | node types |
|
|
144
162
|
|---|---|
|
|
145
163
|
| local | text, upload (image/audio/video), choice, join, comment |
|
|
164
|
+
| local media† | resize, vframes, combine, soundtrack, trim, extractaudio |
|
|
146
165
|
| NanoGPT | llm (incl. vision + audio input), image, draw, edit, inpaint*, vision, tvideo, ivideo, vedit, lipsync, music, remix, tts, transcribe |
|
|
147
|
-
| **not supported** (browser-only media processing) | resize, vframes, combine, soundtrack, trim, extractaudio |
|
|
148
166
|
|
|
149
|
-
|
|
150
|
-
`run()` with `UnsupportedNodeError` — before any network call.
|
|
167
|
+
† **local media** (0.4, unreleased — see [Install](#install)) prefers a pure-JS path that matches the browser (lossless mp4 remux, PCM-WAV trim, PNG resize). **ffmpeg** on `PATH` is the fallback for everything else (soft dependency — not an npm package); clear error if it’s required and missing.
|
|
151
168
|
|
|
152
|
-
\* **inpaint:** the
|
|
153
|
-
|
|
154
|
-
|
|
169
|
+
\* **inpaint:** the mask is composited onto black at the source image’s pixel
|
|
170
|
+
size before send (same as the browser app) — white = repaint, black = keep.
|
|
171
|
+
Brush-style white-on-transparent masks and plain B/W masks both work.
|
|
155
172
|
|
|
156
173
|
## Use it as an agent skill
|
|
157
174
|
|
|
@@ -159,7 +176,8 @@ A saved workflow plus a short `SKILL.md` playbook is a skill any coding agent
|
|
|
159
176
|
can run — Claude Code, Cursor, Grok, or anything that reads markdown and runs
|
|
160
177
|
shell. Recipe and template: [docs/agent-skills.md](docs/agent-skills.md).
|
|
161
178
|
|
|
162
|
-
**Example skill** (idea → LLM prompt → poster image)
|
|
179
|
+
**Example skill** (idea → LLM prompt → poster image), installed with the
|
|
180
|
+
[`skills` CLI](https://www.npmjs.com/package/skills):
|
|
163
181
|
|
|
164
182
|
```bash
|
|
165
183
|
npx skills add nanoodlecom/nanoodle-js@poster-generator -g -y
|
|
@@ -184,6 +202,51 @@ A price of `0` means known-included (subscription), not unknown. `inspect`
|
|
|
184
202
|
and loading/validating workflows never call the API. No telemetry, no
|
|
185
203
|
analytics; the API key is never logged.
|
|
186
204
|
|
|
205
|
+
## No account at all: pay per run in Nano (x402) — 0.4, unreleased
|
|
206
|
+
|
|
207
|
+
NanoGPT supports [x402](https://nano-gpt.com) accountless payments: call the
|
|
208
|
+
API with no key, get an HTTP 402 invoice, settle it in Nano (XNO — instant,
|
|
209
|
+
feeless), and the call completes. nanoodle wires that up end to end:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
# each paid call prints a scannable QR + nano: URI on stderr and waits for the deposit
|
|
213
|
+
nanoodle run "https://nanoodle.com/#g=..." --input Text="hello" --pay
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
const wf = await Workflow.load(url, {
|
|
218
|
+
payment: async (invoice) => {
|
|
219
|
+
// invoice: { payTo, amountRaw, amount, amountUsd, uri, expiresAt, explorerUrl, ... }
|
|
220
|
+
await myWallet.send(invoice.payTo, invoice.amountRaw); // YOUR signer does the send
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
The library **never touches funds or keys**: `payment` must be a callback —
|
|
226
|
+
passing a seed or private key throws. Do the send inside the callback with
|
|
227
|
+
your own wallet/signer (e.g. a self-custody wallet, or print `invoice.uri`
|
|
228
|
+
for a human to scan — `qrTerminal(invoice.uri)` is exported). Each API call
|
|
229
|
+
pays at most once; graphs with several paid nodes produce one small invoice
|
|
230
|
+
per node. Chat and image nodes are live-verified; async video/audio job
|
|
231
|
+
polling under accountless mode is untested upstream.
|
|
232
|
+
|
|
233
|
+
Copy-paste scripts (CLI, QR callback, wallet stub, bare chat):
|
|
234
|
+
[examples/x402/](examples/x402/).
|
|
235
|
+
|
|
236
|
+
### Accountless image, start to finish
|
|
237
|
+
|
|
238
|
+
The starter graph (text → LLM prompt-writer → image), run with **no NanoGPT
|
|
239
|
+
account and no API key** — scan the Nano invoice, and the image node settles
|
|
240
|
+
for a few cents of XNO:
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
nanoodle run noodle-graph.json --input Text="a cozy ramen shop on a rainy night" --pay --out ./noodle-out
|
|
244
|
+
# → LLM node runs, image node prints a nano: QR, waits for the deposit,
|
|
245
|
+
# then writes noodle-out/Image.jpg
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+

|
|
249
|
+
|
|
187
250
|
## Specs and testing
|
|
188
251
|
|
|
189
252
|
Format and execution semantics live in [docs/](docs/):
|
package/bin/nanoodle.mjs
CHANGED
|
@@ -18,7 +18,7 @@ import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
|
18
18
|
import { join, extname } from "node:path";
|
|
19
19
|
import process from "node:process";
|
|
20
20
|
import { Workflow, RunError, mediaFromFile } from "../src/index.mjs";
|
|
21
|
-
import { MediaRef, extForMime } from "../src/media.mjs";
|
|
21
|
+
import { MediaRef, extForMime, sniffMime } from "../src/media.mjs";
|
|
22
22
|
|
|
23
23
|
const HELP = `nanoodle — run nanoodle.com visual AI workflows from the terminal
|
|
24
24
|
|
|
@@ -42,6 +42,9 @@ flags:
|
|
|
42
42
|
(the JSON run summary is always printed to stdout either way)
|
|
43
43
|
--key K NanoGPT API key (defaults to NANOGPT_API_KEY)
|
|
44
44
|
--env-file p read NANOGPT_API_KEY from a .env-style file (--key wins if both given)
|
|
45
|
+
--pay accountless run — no API key or account: each paid call prints a Nano (XNO)
|
|
46
|
+
invoice as a scannable QR + nano: URI on stderr and waits for the deposit
|
|
47
|
+
(x402; ignores any configured key; self-custody wallet does the send)
|
|
45
48
|
--timeout ms overall run timeout
|
|
46
49
|
|
|
47
50
|
examples:
|
|
@@ -59,6 +62,9 @@ examples:
|
|
|
59
62
|
nanoodle inspect "https://nanoodle.com/#g=..."
|
|
60
63
|
nanoodle run "https://nanoodle.com/play.html#a=..." --input Text="hello"
|
|
61
64
|
|
|
65
|
+
# no account at all: pay per run in Nano — scan the QR that appears in the terminal
|
|
66
|
+
nanoodle run "https://nanoodle.com/#g=..." --input Text="hello" --pay
|
|
67
|
+
|
|
62
68
|
Graphs are the noodle-graph.json files saved from the https://nanoodle.com editor (💾),
|
|
63
69
|
or any share link (#g=/#j=/#a= URLs, including da.gd/TinyURL short links) — quote it;
|
|
64
70
|
# starts a comment in most shells. Direct links decode offline; only short links fetch.
|
|
@@ -114,7 +120,7 @@ then: NANOGPT_API_KEY=... nanoodle run ${dest} --input Text="your idea"`);
|
|
|
114
120
|
|
|
115
121
|
if (cmd !== "run" && cmd !== "inspect") usage();
|
|
116
122
|
|
|
117
|
-
let graphPath = null, outDir = null, quiet = false, keyFlag = null, envFile = null, timeoutMs;
|
|
123
|
+
let graphPath = null, outDir = null, quiet = false, keyFlag = null, envFile = null, timeoutMs, pay = false;
|
|
118
124
|
const inputArgs = [], setArgs = [];
|
|
119
125
|
let i = 0;
|
|
120
126
|
const val = (flag) => { // a value-taking flag at end of argv is a usage error, not a TypeError
|
|
@@ -130,6 +136,7 @@ then: NANOGPT_API_KEY=... nanoodle run ${dest} --input Text="your idea"`);
|
|
|
130
136
|
else if (a === "--json") quiet = true;
|
|
131
137
|
else if (a === "--key") keyFlag = val("--key");
|
|
132
138
|
else if (a === "--env-file") envFile = val("--env-file");
|
|
139
|
+
else if (a === "--pay") pay = true;
|
|
133
140
|
else if (a === "--timeout") timeoutMs = +val("--timeout");
|
|
134
141
|
else if (a.startsWith("-")) { console.error("unknown flag: " + a); usage(); }
|
|
135
142
|
else if (!graphPath) graphPath = a;
|
|
@@ -148,7 +155,26 @@ then: NANOGPT_API_KEY=... nanoodle run ${dest} --input Text="your idea"`);
|
|
|
148
155
|
apiKey = m[1].trim();
|
|
149
156
|
}
|
|
150
157
|
|
|
151
|
-
|
|
158
|
+
// --pay = accountless x402: the key (flag or env) is deliberately ignored, each paid call
|
|
159
|
+
// prints a Nano invoice on stderr and the run resumes once the deposit is seen on-chain.
|
|
160
|
+
// The send happens in the user's own wallet — the CLI never asks for a seed or key.
|
|
161
|
+
let payment;
|
|
162
|
+
if (pay) {
|
|
163
|
+
apiKey = null; // null, not undefined — undefined would let Workflow's NANOGPT_API_KEY env fallback re-inject a key
|
|
164
|
+
const { qrTerminal } = await import("../src/qr.mjs");
|
|
165
|
+
payment = async (inv) => {
|
|
166
|
+
const mins = inv.expiresAt ? Math.max(1, Math.round((inv.expiresAt - Date.now()) / 60000)) : null;
|
|
167
|
+
console.error(`\n⚡ payment required: ${inv.amount || inv.amountRaw + " raw"}${inv.amountUsd != null ? ` (~$${inv.amountUsd})` : ""}`);
|
|
168
|
+
console.error(qrTerminal(inv.uri));
|
|
169
|
+
console.error("scan with your Nano wallet (dark terminals scan best), or send to:");
|
|
170
|
+
console.error(" " + inv.payTo);
|
|
171
|
+
console.error(" " + inv.uri);
|
|
172
|
+
if (inv.explorerUrl) console.error(" explorer: " + inv.explorerUrl);
|
|
173
|
+
console.error(`waiting for the deposit…${mins ? ` (invoice expires in ~${mins} min,` : " ("}Ctrl-C aborts)\n`);
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const wf = await Workflow.load(graphPath, { apiKey, payment, baseUrl: process.env.NANOGPT_BASE_URL || undefined });
|
|
152
178
|
|
|
153
179
|
if (cmd === "inspect") {
|
|
154
180
|
const pad = (s, n) => String(s).padEnd(n);
|
|
@@ -222,8 +248,10 @@ then: NANOGPT_API_KEY=... nanoodle run ${dest} --input Text="your idea"`);
|
|
|
222
248
|
if (value instanceof MediaRef) {
|
|
223
249
|
if (!dirMade) { await mkdir(dir, { recursive: true }); dirMade = true; }
|
|
224
250
|
const safe = o.key.replace(/[^\w.-]+/g, "_");
|
|
225
|
-
|
|
226
|
-
await value.
|
|
251
|
+
// fetch before naming: hosted media only reveals its mime via the response Content-Type
|
|
252
|
+
const data = await value.bytes();
|
|
253
|
+
const path = join(dir, safe + "." + extForMime(value.mime || sniffMime(data)));
|
|
254
|
+
await writeFile(path, data);
|
|
227
255
|
printable[o.key] = path;
|
|
228
256
|
if (!quiet) console.error(`${o.key}: saved ${path}`);
|
|
229
257
|
} else {
|
package/package.json
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nanoodle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Run nanoodle visual AI workflows from JavaScript — zero-dependency executor for saved noodle-graph.json files",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./src/index.mjs",
|
|
9
|
+
"./browser": "./src/browser.mjs",
|
|
9
10
|
"./package.json": "./package.json"
|
|
10
11
|
},
|
|
12
|
+
"browser": {
|
|
13
|
+
"./src/index.mjs": "./src/browser.mjs"
|
|
14
|
+
},
|
|
11
15
|
"sideEffects": false,
|
|
12
16
|
"bin": {
|
|
13
17
|
"nanoodle": "./bin/nanoodle.mjs"
|
package/src/browser.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-oriented package entry (`import … from "nanoodle/browser"`).
|
|
3
|
+
*
|
|
4
|
+
* Goal: one engine for play/export/CLI. No module on this surface top-level
|
|
5
|
+
* imports Node builtins (Phase D): the pure local-media paths (MP4CAT remux,
|
|
6
|
+
* PCM-WAV trim, PNG resize/mask composite) and share-link decoding run in a
|
|
7
|
+
* browser as-is — zlib work goes through Compression/DecompressionStream
|
|
8
|
+
* there. Only the ffmpeg fallback and file-path helpers dynamically import
|
|
9
|
+
* Node builtins when those code paths actually run.
|
|
10
|
+
* See docs/DESIGN.md § "Replacing the browser executor".
|
|
11
|
+
*
|
|
12
|
+
* Prefer `Workflow.fromJSON(obj, { apiKey, fetch, payment })` in the browser;
|
|
13
|
+
* `Workflow.load(path)` and `mediaFromFile` remain Node-only (dynamic `node:fs`).
|
|
14
|
+
*/
|
|
15
|
+
export { Workflow, RunResult } from "./workflow.mjs";
|
|
16
|
+
export { NanoodleError, UnsupportedNodeError, RunError } from "./errors.mjs";
|
|
17
|
+
export {
|
|
18
|
+
MediaRef,
|
|
19
|
+
MEDIA_INLINE_MAX,
|
|
20
|
+
coerceMediaInput,
|
|
21
|
+
assertInlineMediaSize,
|
|
22
|
+
bytesToDataUrl,
|
|
23
|
+
dataUrlBytes,
|
|
24
|
+
bytesToBase64,
|
|
25
|
+
base64ToBytes,
|
|
26
|
+
sniffMime,
|
|
27
|
+
b64ImageMime,
|
|
28
|
+
extForMime,
|
|
29
|
+
} from "./media.mjs";
|
|
30
|
+
export { NanoClient, httpError, costFromJson, costFromHeaders, costWithHeaders, sleep } from "./client.mjs";
|
|
31
|
+
export { NODE_TYPES, displayName, materialize, topoSort, wiredFramesFloor, MAX_FRAMES } from "./graph.mjs";
|
|
32
|
+
export { RUNNERS } from "./nodes.mjs"; // per-node executors — the play delegation shim drives these directly
|
|
33
|
+
export { catItem, chatModelCan } from "./catalog.mjs";
|
|
34
|
+
export { deriveInputs, deriveOutputs, deriveSettings, INPUT_SPECS, SETTING_SPECS } from "./io.mjs";
|
|
35
|
+
export { decodeShareUrl, decodeShareFragment, isShareRef } from "./share.mjs";
|
|
36
|
+
export { resizePlan, maskToSource, resizeCropImage, encodeWavMono } from "./local-media.mjs";
|
|
37
|
+
export { parseNanoInvoice } from "./x402.mjs";
|
|
38
|
+
export { qrTerminal, qrModules } from "./qr.mjs";
|
package/src/catalog.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opt-in model catalog (replace-prep: catalog gates behave like play RUNTIME_JS).
|
|
3
|
+
*
|
|
4
|
+
* Data-only: pass `{ catalog: { chat, image, video, audio } }` to Workflow with
|
|
5
|
+
* the raw arrays the NanoGPT public catalog endpoints return (/api/v1/models,
|
|
6
|
+
* /api/v1/image-models, …) — the library never fetches them itself. Every gate
|
|
7
|
+
* is permissive: no catalog, or a model absent from it, changes nothing, so
|
|
8
|
+
* authored graphs keep their behavior offline. Only a KNOWN-incapable model has
|
|
9
|
+
* the gated part/knob stripped (mirrors play's chatModelCan / rawCatItem).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function catItem(catalog, kind, id) {
|
|
13
|
+
if (!catalog || !id) return null;
|
|
14
|
+
const raw = catalog[kind];
|
|
15
|
+
return (Array.isArray(raw) && raw.find((m) => m && m.id === id)) || null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Permissive capability probe: true unless the model is in the catalog AND lacks the flag. */
|
|
19
|
+
export function chatModelCan(catalog, model, flag) {
|
|
20
|
+
const m = catItem(catalog, "chat", model);
|
|
21
|
+
return !m || !!((m.capabilities || {})[flag]);
|
|
22
|
+
}
|
package/src/client.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NanoodleError } from "./errors.mjs";
|
|
2
2
|
import { b64ImageMime, bytesToDataUrl, dataUrlBytes, MEDIA_INLINE_MAX } from "./media.mjs";
|
|
3
|
+
import { assertPaymentOption, parseNanoInvoice, looksLikeResult } from "./x402.mjs";
|
|
3
4
|
|
|
4
5
|
const AUDIO_MIME = { mp3: "audio/mpeg", opus: "audio/ogg", aac: "audio/aac", flac: "audio/flac", wav: "audio/wav", pcm: "audio/wav" };
|
|
5
6
|
|
|
@@ -75,29 +76,97 @@ export function sleep(ms, signal) {
|
|
|
75
76
|
* pollIntervals / timeouts are per-media-kind knobs (ms).
|
|
76
77
|
*/
|
|
77
78
|
export class NanoClient {
|
|
78
|
-
constructor({ apiKey, baseUrl = "https://nano-gpt.com", fetch = globalThis.fetch, pollIntervals = {}, timeouts = {} } = {}) {
|
|
79
|
+
constructor({ apiKey, baseUrl = "https://nano-gpt.com", fetch = globalThis.fetch, pollIntervals = {}, timeouts = {}, payment } = {}) {
|
|
79
80
|
// non-enumerable: console.log/util.inspect/JSON.stringify of a client (or a Workflow holding
|
|
80
81
|
// one) must never print the key
|
|
81
82
|
Object.defineProperty(this, "apiKey", { value: apiKey, writable: true, enumerable: false, configurable: true });
|
|
83
|
+
assertPaymentOption(payment); // a callback or nothing — never a seed/private key
|
|
84
|
+
this.payment = payment || null;
|
|
82
85
|
this.baseUrl = String(baseUrl).replace(/\/+$/, "");
|
|
83
86
|
this.fetch = fetch;
|
|
84
|
-
this.pollIntervals = { video: 5000, audio: 3000, ...pollIntervals };
|
|
87
|
+
this.pollIntervals = { video: 5000, audio: 3000, x402: 3000, ...pollIntervals };
|
|
85
88
|
this.timeouts = { video: 600000, audio: 300000, ...timeouts };
|
|
86
89
|
}
|
|
87
90
|
|
|
88
|
-
_auth() {
|
|
91
|
+
_auth() {
|
|
92
|
+
if (this.apiKey) return { Authorization: "Bearer " + this.apiKey, "x-api-key": this.apiKey };
|
|
93
|
+
if (this.payment) return { "x-x402": "true" }; // keyless: opt into accountless 402 invoices
|
|
94
|
+
return {};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* fetch that settles HTTP 402 via the x402 flow when running keyless with a
|
|
99
|
+
* `payment` callback: parse the Nano invoice → callback sends XNO (its own
|
|
100
|
+
* wallet/signer — the library never touches funds) → poll the complete URL
|
|
101
|
+
* until the deposit is seen → return the replayed result, or re-send the
|
|
102
|
+
* original request stamped with the settled payment id. Each API call pays
|
|
103
|
+
* at most once; a second 402 after settling is an error, never a second send.
|
|
104
|
+
*/
|
|
105
|
+
async _paidFetch(url, init, signal) {
|
|
106
|
+
const r = await this.fetch(url, init);
|
|
107
|
+
if (r.status !== 402 || !this.payment || this.apiKey) return r;
|
|
108
|
+
const settled = await this._settle402(r, signal);
|
|
109
|
+
if (settled.response) return settled.response; // complete replayed the stored request
|
|
110
|
+
const r2 = await this.fetch(url, { ...init, headers: { ...init.headers, "x-x402-payment-id": settled.paymentId } });
|
|
111
|
+
if (r2.status === 402) {
|
|
112
|
+
throw new NanoodleError(
|
|
113
|
+
"payment " + settled.paymentId + " settled, but the API still answered 402 on retry — " +
|
|
114
|
+
"check " + (settled.statusUrl || "the payment status") + " before paying again",
|
|
115
|
+
{ code: "x402", status: 402, paymentId: settled.paymentId });
|
|
116
|
+
}
|
|
117
|
+
return r2;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async _settle402(r, signal) {
|
|
121
|
+
let body = null;
|
|
122
|
+
try { body = await r.json(); } catch { /* fall through to the generic funds error */ }
|
|
123
|
+
const invoice = body && parseNanoInvoice(body, this.baseUrl);
|
|
124
|
+
if (!invoice || !invoice.paymentId || !invoice.completeUrl) {
|
|
125
|
+
throw new NanoodleError(
|
|
126
|
+
"payment required, but the 402 response offered no usable Nano option" +
|
|
127
|
+
(body ? " — " + JSON.stringify(body).slice(0, 200) : ""), { code: "x402", status: 402 });
|
|
128
|
+
}
|
|
129
|
+
await this.payment(invoice); // ← the callback does the actual XNO send
|
|
130
|
+
// The complete endpoint doubles as the poll: 402 = not seen on-chain yet.
|
|
131
|
+
const deadline = invoice.expiresAt || Date.now() + 15 * 60 * 1000;
|
|
132
|
+
while (true) {
|
|
133
|
+
const cr = await this.fetch(invoice.completeUrl, {
|
|
134
|
+
method: "POST", headers: { "Content-Type": "application/json", "x-x402": "true" }, body: "{}", signal,
|
|
135
|
+
});
|
|
136
|
+
if (cr.ok) {
|
|
137
|
+
const ct = ((cr.headers && cr.headers.get && cr.headers.get("content-type")) || "");
|
|
138
|
+
let cj = null;
|
|
139
|
+
if (ct.includes("json")) { try { cj = await cr.json(); } catch { /* treat as settle-only */ } }
|
|
140
|
+
if (looksLikeResult(cj)) {
|
|
141
|
+
// wrap so call sites keep their Response contract (ok/json/text/headers)
|
|
142
|
+
const response = { ok: true, status: 200, headers: cr.headers, json: async () => cj, text: async () => JSON.stringify(cj) };
|
|
143
|
+
return { paymentId: invoice.paymentId, statusUrl: invoice.statusUrl, response };
|
|
144
|
+
}
|
|
145
|
+
return { paymentId: invoice.paymentId, statusUrl: invoice.statusUrl };
|
|
146
|
+
}
|
|
147
|
+
if (cr.status !== 402) throw httpError(cr.status, await cr.text());
|
|
148
|
+
await cr.text().catch(() => {}); // drain the "not verified yet" body
|
|
149
|
+
if (Date.now() >= deadline) {
|
|
150
|
+
throw new NanoodleError(
|
|
151
|
+
"payment window expired before the Nano deposit was detected (payment " + invoice.paymentId + ", " +
|
|
152
|
+
(invoice.amount || invoice.amountRaw) + " to " + invoice.payTo + ") — if you already sent it, check " +
|
|
153
|
+
(invoice.explorerUrl || invoice.statusUrl), { code: "x402-expired", paymentId: invoice.paymentId });
|
|
154
|
+
}
|
|
155
|
+
await sleep(this.pollIntervals.x402, signal);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
89
158
|
|
|
90
159
|
async _postJson(path, body, signal) {
|
|
91
160
|
const payload = JSON.stringify(body);
|
|
92
161
|
if (payload.length > MEDIA_INLINE_MAX) {
|
|
93
162
|
throw new NanoodleError("request body is too large (~4 MB max) — nanoodle sends media inline as base64; use smaller/shorter media");
|
|
94
163
|
}
|
|
95
|
-
return this.
|
|
164
|
+
return this._paidFetch(this.baseUrl + path, {
|
|
96
165
|
method: "POST",
|
|
97
166
|
headers: { "Content-Type": "application/json", ...this._auth() },
|
|
98
167
|
body: payload,
|
|
99
168
|
signal,
|
|
100
|
-
});
|
|
169
|
+
}, signal);
|
|
101
170
|
}
|
|
102
171
|
|
|
103
172
|
async _get(path, signal) {
|
|
@@ -117,7 +186,9 @@ export class NanoClient {
|
|
|
117
186
|
if (onCost) onCost(costWithHeaders(j, r));
|
|
118
187
|
const msg = (j.choices && j.choices[0] && j.choices[0].message) || {};
|
|
119
188
|
const txt = msg.content;
|
|
120
|
-
|
|
189
|
+
// empty-string content is a billed-but-empty reply, not a protocol failure — return it
|
|
190
|
+
// (showThinking can still surface msg.reasoning), matching the editor/play built-in parsers
|
|
191
|
+
if (txt == null) throw new NanoodleError("no text in response");
|
|
121
192
|
let out = typeof txt === "string" ? txt : txt.map((p) => p.text || "").join("");
|
|
122
193
|
if (opts.showThinking && msg.reasoning) {
|
|
123
194
|
out = "```thinking\n" + msg.reasoning + "\n```\n\n" + out;
|
|
@@ -164,11 +235,19 @@ export class NanoClient {
|
|
|
164
235
|
}
|
|
165
236
|
|
|
166
237
|
/** POST /api/generate-video then poll GET /api/video/status?requestId= until COMPLETED. */
|
|
167
|
-
|
|
238
|
+
// io.onRunId(runId) fires the moment a FRESH submit returns its job id — a caller
|
|
239
|
+
// keeping a resume registry (play's PENDING_VIDEO) records it before any await.
|
|
240
|
+
// io.resume (a runId) skips the submit — and its charge — and goes straight to
|
|
241
|
+
// polling: how a timed-out job is picked back up without paying twice.
|
|
242
|
+
async video(model, prompt, opts = {}, imageDataUrl, { onCost, onPoll, onRunId, resume, signal } = {}) {
|
|
168
243
|
const body = { model, prompt };
|
|
169
244
|
if (opts.duration) body.duration = opts.duration;
|
|
170
245
|
if (opts.aspect_ratio) body.aspect_ratio = opts.aspect_ratio;
|
|
171
246
|
if (opts.resolution) body.resolution = opts.resolution;
|
|
247
|
+
// catalog-declared wire names (videoDims with an opt-in catalog): Sora-style seconds/orientation
|
|
248
|
+
if (opts.seconds) body.seconds = opts.seconds;
|
|
249
|
+
if (opts.orientation) body.orientation = opts.orientation;
|
|
250
|
+
if (opts.resolution_ratio) body.resolution_ratio = opts.resolution_ratio;
|
|
172
251
|
if (imageDataUrl) body.imageDataUrl = imageDataUrl;
|
|
173
252
|
if (opts.last_image) body.last_image = opts.last_image;
|
|
174
253
|
if (opts.videoUrl) body.videoUrl = opts.videoUrl;
|
|
@@ -181,13 +260,20 @@ export class NanoClient {
|
|
|
181
260
|
if (opts.duration) body.duration = opts.duration;
|
|
182
261
|
if (opts.aspect_ratio) body.aspect_ratio = opts.aspect_ratio;
|
|
183
262
|
if (opts.resolution) body.resolution = opts.resolution;
|
|
263
|
+
if (opts.seconds) body.seconds = opts.seconds;
|
|
264
|
+
if (opts.orientation) body.orientation = opts.orientation;
|
|
265
|
+
if (opts.resolution_ratio) body.resolution_ratio = opts.resolution_ratio;
|
|
184
266
|
if (opts.refImages && opts.refImages.length) body[opts.refKey || "reference_images"] = opts.refImages; // wired refs win last
|
|
185
|
-
|
|
186
|
-
if (!
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
267
|
+
let runId = resume || null;
|
|
268
|
+
if (!runId) {
|
|
269
|
+
const r = await this._postJson("/api/generate-video", body, signal);
|
|
270
|
+
if (!r.ok) throw httpError(r.status, await r.text());
|
|
271
|
+
const j = await r.json();
|
|
272
|
+
if (onCost) onCost(costWithHeaders(j, r));
|
|
273
|
+
runId = j.runId || j.id;
|
|
274
|
+
if (!runId) throw new NanoodleError("no runId returned");
|
|
275
|
+
if (onRunId) onRunId(runId);
|
|
276
|
+
}
|
|
191
277
|
|
|
192
278
|
const t0 = Date.now();
|
|
193
279
|
while (Date.now() - t0 < this.timeouts.video) {
|
|
@@ -214,8 +300,18 @@ export class NanoClient {
|
|
|
214
300
|
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
301
|
}
|
|
216
302
|
|
|
217
|
-
/**
|
|
218
|
-
|
|
303
|
+
/**
|
|
304
|
+
* POST /api/v1/audio/speech (music + tts + remix). Returns an audio URL (https or data:).
|
|
305
|
+
* io.onRunId(job) fires when an async job enters the poll branch (job = the submit
|
|
306
|
+
* response — runId + refund metadata the status poll needs); io.resume (that same job
|
|
307
|
+
* object) skips the submit + charge and resumes polling a prior run's job.
|
|
308
|
+
*/
|
|
309
|
+
async audio(model, input, extra = {}, { onCost, onPoll, onRunId, resume, signal } = {}) {
|
|
310
|
+
if (resume) {
|
|
311
|
+
const url = await this._pollAudio(model, resume, { onPoll, signal });
|
|
312
|
+
if (!url) throw new NanoodleError("no audio url in response");
|
|
313
|
+
return url;
|
|
314
|
+
}
|
|
219
315
|
const body = Object.assign({ model, input }, extra);
|
|
220
316
|
const r = await this._postJson("/api/v1/audio/speech", body, signal);
|
|
221
317
|
if (!r.ok) throw httpError(r.status, await r.text());
|
|
@@ -224,7 +320,10 @@ export class NanoClient {
|
|
|
224
320
|
const j = await r.json();
|
|
225
321
|
if (onCost) onCost(costWithHeaders(j, r));
|
|
226
322
|
let url = j.url || j.audioUrl || (j.data && j.data.url) || (j.data && j.data.audioUrl);
|
|
227
|
-
if (!url && (j.runId || j.id))
|
|
323
|
+
if (!url && (j.runId || j.id)) {
|
|
324
|
+
if (onRunId) onRunId(j);
|
|
325
|
+
url = await this._pollAudio(model, j, { onPoll, signal });
|
|
326
|
+
}
|
|
228
327
|
if (!url) throw new NanoodleError("no audio url in response");
|
|
229
328
|
return url;
|
|
230
329
|
}
|
|
@@ -288,7 +387,7 @@ export class NanoClient {
|
|
|
288
387
|
fd.append("model", model);
|
|
289
388
|
if (language) fd.append("language", language);
|
|
290
389
|
// no explicit Content-Type — fetch sets the multipart boundary
|
|
291
|
-
const r = await this.
|
|
390
|
+
const r = await this._paidFetch(this.baseUrl + "/api/v1/audio/transcriptions", { method: "POST", headers: this._auth(), body: fd, signal }, signal);
|
|
292
391
|
if (!r.ok) throw httpError(r.status, await r.text());
|
|
293
392
|
const j = await r.json();
|
|
294
393
|
if (onCost) onCost(costWithHeaders(j, r));
|
package/src/graph.mjs
CHANGED
|
@@ -9,13 +9,31 @@ export const VID_PORT_RE = /^vid\d+$/;
|
|
|
9
9
|
export const CLIP_PORT_RE = /^clip\d+$/; // combine clips
|
|
10
10
|
export const REF_PORT_RE = /^ref\d+$/; // tvideo reference images
|
|
11
11
|
export const FRAME_PORT_RE = /^frame\d+$/; // vframes outputs
|
|
12
|
+
export const MAX_FRAMES = 12;
|
|
12
13
|
|
|
13
14
|
const DYNAMIC_INPUT_RES = [IMG_PORT_RE, EDIT_IMG_RE, VID_PORT_RE, CLIP_PORT_RE, REF_PORT_RE];
|
|
14
15
|
const DYNAMIC_INPUT_NAMES = new Set(["audio", "endframe"]);
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Highest frameN port wired OUT of a vframes node. fields.frames is shape-affecting:
|
|
19
|
+
* run() emits frame1..frameN and downstream links read fixed frameK ports. A count below
|
|
20
|
+
* the highest wired port starves consumers mid-run (after upstream paid steps). Mirrors
|
|
21
|
+
* play.html wiredFramesFloor — floor is raised at run and in deriveSettings.
|
|
22
|
+
*/
|
|
23
|
+
export function wiredFramesFloor(graph, nodeId) {
|
|
24
|
+
let floor = 1;
|
|
25
|
+
for (const l of (graph && graph.links) || []) {
|
|
26
|
+
if (l.from.node !== nodeId) continue;
|
|
27
|
+
const m = /^frame(\d+)$/.exec(String(l.from.port));
|
|
28
|
+
if (m) floor = Math.max(floor, parseInt(m[1], 10) || 1);
|
|
29
|
+
}
|
|
30
|
+
return Math.min(floor, MAX_FRAMES);
|
|
31
|
+
}
|
|
32
|
+
|
|
16
33
|
/**
|
|
17
34
|
* Node-type registry (execution-relevant subset of the app's NODE_TYPES).
|
|
18
|
-
* flags: local (pure logic) | network (calls NanoGPT) |
|
|
35
|
+
* flags: local (pure logic / on-device media) | network (calls NanoGPT) | note.
|
|
36
|
+
* Local media: pure-JS first (MP4CAT / PCM-WAV / PNG); ffmpeg on PATH is the heavy fallback.
|
|
19
37
|
*/
|
|
20
38
|
export const NODE_TYPES = {
|
|
21
39
|
text: { title: "Text", inputs: [], outputs: [{ name: "text", type: "text" }], local: true },
|
|
@@ -29,20 +47,20 @@ export const NODE_TYPES = {
|
|
|
29
47
|
draw: { title: "Draw", inputs: [], outputs: [{ name: "image", type: "image" }, { name: "text", type: "text" }], network: true },
|
|
30
48
|
edit: { title: "Edit", inputs: [], outputs: [{ name: "image", type: "image" }], network: true },
|
|
31
49
|
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" }],
|
|
50
|
+
resize: { title: "Resize / crop", inputs: ["image"], outputs: [{ name: "image", type: "image" }], local: true },
|
|
33
51
|
vision: { title: "Vision", inputs: ["image"], outputs: [{ name: "text", type: "text" }], network: true },
|
|
34
52
|
tvideo: { title: "Text→Video", inputs: [], outputs: [{ name: "video", type: "video" }], network: true },
|
|
35
53
|
ivideo: { title: "Image→Video", inputs: ["image"], outputs: [{ name: "video", type: "video" }], network: true },
|
|
36
54
|
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" }],
|
|
38
|
-
combine: { title: "Combine videos", inputs: [], outputs: [{ name: "video", type: "video" }],
|
|
39
|
-
soundtrack: { title: "Soundtrack", inputs: ["video", "audio"], outputs: [{ name: "video", type: "video" }],
|
|
55
|
+
vframes: { title: "Video → frames", inputs: ["video"], outputs: [{ name: "frame1", type: "image" }], local: true, framesOut: true }, // dynamic frame1..N
|
|
56
|
+
combine: { title: "Combine videos", inputs: [], outputs: [{ name: "video", type: "video" }], local: true },
|
|
57
|
+
soundtrack: { title: "Soundtrack", inputs: ["video", "audio"], outputs: [{ name: "video", type: "video" }], local: true },
|
|
40
58
|
lipsync: { title: "Avatar / lipsync", inputs: ["image", "audio"], outputs: [{ name: "video", type: "video" }], network: true },
|
|
41
59
|
music: { title: "Music", inputs: [], outputs: [{ name: "audio", type: "audio" }], network: true },
|
|
42
60
|
remix: { title: "Remix audio", inputs: ["audio"], outputs: [{ name: "audio", type: "audio" }], network: true },
|
|
43
61
|
tts: { title: "Speech", inputs: [], outputs: [{ name: "audio", type: "audio" }], network: true },
|
|
44
|
-
trim: { title: "Trim audio", inputs: ["audio"], outputs: [{ name: "audio", type: "audio" }],
|
|
45
|
-
extractaudio: { title: "Extract audio", inputs: ["video"], outputs: [{ name: "audio", type: "audio" }],
|
|
62
|
+
trim: { title: "Trim audio", inputs: ["audio"], outputs: [{ name: "audio", type: "audio" }], local: true },
|
|
63
|
+
extractaudio: { title: "Extract audio", inputs: ["video"], outputs: [{ name: "audio", type: "audio" }], local: true },
|
|
46
64
|
transcribe: { title: "Transcribe", inputs: ["audio"], outputs: [{ name: "text", type: "text" }], network: true },
|
|
47
65
|
comment: { title: "Comment", inputs: [], outputs: [], note: true, local: true },
|
|
48
66
|
};
|
|
@@ -84,8 +102,6 @@ export function materialize(data) {
|
|
|
84
102
|
if (!NODE_TYPES[type]) {
|
|
85
103
|
n.unknown = true;
|
|
86
104
|
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
105
|
}
|
|
90
106
|
nodes.push(n);
|
|
91
107
|
}
|
package/src/index.mjs
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export { Workflow, RunResult } from "./workflow.mjs";
|
|
2
2
|
export { NanoodleError, UnsupportedNodeError, RunError } from "./errors.mjs";
|
|
3
|
-
export {
|
|
3
|
+
export {
|
|
4
|
+
MediaRef, mediaFromFile, MEDIA_INLINE_MAX, coerceMediaInput, assertInlineMediaSize,
|
|
5
|
+
bytesToDataUrl, dataUrlBytes, bytesToBase64, base64ToBytes, sniffMime, b64ImageMime, extForMime,
|
|
6
|
+
} from "./media.mjs";
|
|
4
7
|
export { NanoClient } from "./client.mjs";
|
|
5
|
-
export { NODE_TYPES, displayName, materialize, topoSort } from "./graph.mjs";
|
|
8
|
+
export { NODE_TYPES, displayName, materialize, topoSort, wiredFramesFloor, MAX_FRAMES } from "./graph.mjs";
|
|
6
9
|
export { deriveInputs, deriveOutputs, deriveSettings, INPUT_SPECS, SETTING_SPECS } from "./io.mjs";
|
|
7
10
|
export { decodeShareUrl, decodeShareFragment, isShareRef } from "./share.mjs";
|
|
11
|
+
export { parseNanoInvoice } from "./x402.mjs";
|
|
12
|
+
export { qrTerminal, qrModules } from "./qr.mjs";
|