nanoodle 0.8.0 → 0.9.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/src/workflow.mjs CHANGED
@@ -39,14 +39,30 @@ function throwIfAborted(signal) {
39
39
  }
40
40
 
41
41
  /** The outcome of Workflow.run(). Media values are MediaRef; text values plain strings. */
42
+ /** Internal: thrown by a node skipped behind a closed gate, so ITS dependents skip too. Never surfaced. */
43
+ class GateSkip extends Error {
44
+ constructor(gatedBy) { super("skipped — a gate upstream said no"); this.gatedBy = gatedBy; }
45
+ }
46
+
42
47
  export class RunResult {
43
- constructor({ outputs, nodes, errors, costUsd, costExact, remainingBalance }) {
44
- /** { [friendlyKey | nodeId]: value } — sink node primary outputs */
48
+ constructor({ outputs, nodes, errors, gated = [], costUsd, costExact, remainingBalance }) {
49
+ /** { [friendlyKey | nodeId]: value } — sink node primary outputs (a sink skipped by a closed gate has none) */
45
50
  this.outputs = outputs;
46
- /** per-node { status: "done"|"error"|"skipped", out, error, costUsd, ms } */
51
+ /**
52
+ * per-node { status: "done"|"error"|"gated"|"skipped", out, error, costUsd, ms }.
53
+ * "gated": a ⚖️ Decide yes/no gate that answered no — it ran and billed, `out` holds its answer
54
+ * ({ text: "no", decision }), and it carries `gate: { yes, message }`. Not an error.
55
+ * "skipped" + `gatedBy: <decide node id>`: downstream of a closed gate — never ran, never billed.
56
+ * (Annotation notes are "skipped" with no gatedBy.)
57
+ */
47
58
  this.nodes = nodes;
48
- /** [{ nodeId, name, message }] for every node that failed (incl. non-sink warnings) */
59
+ /** [{ nodeId, name, message }] for every node that failed (incl. non-sink warnings) — never gates */
49
60
  this.errors = errors;
61
+ /**
62
+ * [{ nodeId, name, message, yes, skipped: [nodeId…] }] — every Decide gate that closed this run.
63
+ * A closed gate is a deliberate, successful outcome: run() resolves, it never throws for one.
64
+ */
65
+ this.gated = gated;
50
66
  /** summed USD cost of all calls that reported one */
51
67
  this.costUsd = costUsd;
52
68
  /** false when any network call omitted its price (total is a floor) */
@@ -63,6 +79,8 @@ export class RunResult {
63
79
  for (const k of Object.keys(this.outputs)) {
64
80
  if (k.toLowerCase() === norm) return this.outputs[k];
65
81
  }
82
+ const g = (this._gatedOutputs || []).find((o) => o.key.toLowerCase() === norm || o.nodeId.toLowerCase() === norm);
83
+ if (g) throw new NanoodleError(`output "${g.key}" was skipped — the gate "${g.gateName}" answered no, so it never ran (see result.gated)`, { code: "gated" });
66
84
  throw new NanoodleError(`no output "${key}" — available outputs: ${Object.keys(this.outputs).map((k) => `"${k}"`).join(", ") || "(none)"}`);
67
85
  }
68
86
  }
@@ -266,6 +284,7 @@ export class Workflow {
266
284
  const io = { onCost, onPoll, signal: ac.signal };
267
285
  return {
268
286
  chat: (messages, model, opts) => this.client.chat(messages, model, opts, io),
287
+ decide: (body) => this.client.decide(body, io),
269
288
  image: (args) => this.client.image(args, io),
270
289
  video: (model, prompt, opts, imageDataUrl) => this.client.video(model, prompt, opts, imageDataUrl, io),
271
290
  audio: (model, input, extra) => this.client.audio(model, input, extra, io),
@@ -279,6 +298,15 @@ export class Workflow {
279
298
  };
280
299
  };
281
300
 
301
+ const gated = [];
302
+ // a node whose upstream settled as a closed gate (or was itself skipped by one) → that gate's id
303
+ const gateOf = (id) => {
304
+ const r = nodesRec[id];
305
+ if (!r) return null;
306
+ if (r.status === "gated") return id;
307
+ if (r.status === "skipped" && r.gatedBy) return r.gatedBy;
308
+ return null;
309
+ };
282
310
  const execNode = async (n) => {
283
311
  const rec = nodesRec[n.id];
284
312
  try {
@@ -286,18 +314,37 @@ export class Workflow {
286
314
  const inbound = graph.links.filter((l) => l.to.node === n.id);
287
315
  const inp = {};
288
316
  let fields = effFields.get(n.id);
289
- let upstreamFail = null;
317
+ let upstreamFail = null, gatedBy = null;
290
318
  for (const l of inbound) {
291
319
  let srcOut;
292
320
  try { srcOut = await promises.get(l.from.node); }
293
- catch { if (!upstreamFail) upstreamFail = displayName(byId.get(l.from.node)); continue; }
321
+ catch {
322
+ const g = gateOf(l.from.node);
323
+ if (g) { if (!gatedBy) gatedBy = g; } else if (!upstreamFail) upstreamFail = displayName(byId.get(l.from.node));
324
+ continue;
325
+ }
294
326
  const v = srcOut[l.from.port];
295
327
  if (isInputPort(n, l.to.port)) inp[l.to.port] = v;
328
+ // Custom endpoint url/mode: a Choice path like "/post" must ride inp so
329
+ // endpointResolveTarget can join it onto the typed host. Overwriting
330
+ // fields.url with "/post" rejects the joined URL.
331
+ else if (n.type === "endpoint" && (l.to.port === "url" || l.to.port === "mode")) {
332
+ if (v != null) inp[l.to.port] = v;
333
+ }
296
334
  // wired textarea port = field override; a missing upstream port (degraded save) must
297
335
  // NOT clobber the typed field with undefined — the app only applies v != null
298
336
  else if (v != null) fields = { ...fields, [l.to.port]: v };
299
337
  }
338
+ // a real failure upstream wins over a gate (the browser runners agree): that is an error
300
339
  if (upstreamFail) throw new NanoodleError("upstream failed: " + upstreamFail);
340
+ if (gatedBy) {
341
+ rec.status = "skipped";
342
+ rec.gatedBy = gatedBy;
343
+ const gr = gated.find((x) => x.nodeId === gatedBy);
344
+ if (gr) gr.skipped.push(n.id);
345
+ emit({ type: "node-skipped", nodeId: n.id, name: displayName(n), reason: "gated", gatedBy });
346
+ throw new GateSkip(gatedBy);
347
+ }
301
348
  throwIfAborted(ac.signal);
302
349
  // Prompt length caps (see prompt-caps.mjs). Many image/video models reject an over-long
303
350
  // prompt at the route, and in a graph the prompt is WRITTEN by an upstream LLM — so a
@@ -321,6 +368,16 @@ export class Workflow {
321
368
  emit({ type: "node-done", nodeId: n.id, name: displayName(n), ms: rec.ms, costUsd: rec.costUsd });
322
369
  return out;
323
370
  } catch (e) {
371
+ if (e instanceof GateSkip) throw e;
372
+ // ⚖️ Decide gate answered no: a settled, billed decision — not an error. Downstream skips.
373
+ if (e && e.gate === true && !ac.signal.aborted) {
374
+ rec.status = "gated";
375
+ rec.out = e.out || null;
376
+ rec.gate = { yes: e.decision && typeof e.decision.yes === "number" ? e.decision.yes : null, message: e.message };
377
+ gated.push({ nodeId: n.id, name: displayName(n), message: e.message, yes: rec.gate.yes, skipped: [] });
378
+ emit({ type: "node-gated", nodeId: n.id, name: displayName(n), message: e.message, yes: rec.gate.yes, costUsd: rec.costUsd });
379
+ throw e;
380
+ }
324
381
  // A prompt-length rejection is free (nothing generated) and, once banked, preventable: the
325
382
  // next run budgets the LLM above this node and fits whatever is left. Say that, rather than
326
383
  // relaying "please shorten it" about a prompt the caller never wrote.
@@ -351,7 +408,7 @@ export class Workflow {
351
408
  const outputsMap = {};
352
409
  for (const o of this.outputs) {
353
410
  const rec = nodesRec[o.nodeId];
354
- if (!rec || rec.status !== "done") continue;
411
+ if (!rec || !(rec.status === "done" || (rec.status === "gated" && rec.out))) continue;
355
412
  const primary = o.ports[0];
356
413
  const value = this._wrapValue(rec.out[primary.name], primary.type);
357
414
  outputsMap[o.key] = value;
@@ -361,10 +418,17 @@ export class Workflow {
361
418
  outputs: outputsMap,
362
419
  nodes: nodesRec,
363
420
  errors,
421
+ gated,
364
422
  costUsd: cost.total,
365
423
  costExact: cost.exact,
366
424
  remainingBalance: cost.balance,
367
425
  });
426
+ // sinks a closed gate skipped: get() names the gate instead of "no output"
427
+ Object.defineProperty(result, "_gatedOutputs", {
428
+ enumerable: false,
429
+ value: this.outputs.filter((o) => nodesRec[o.nodeId] && nodesRec[o.nodeId].gatedBy)
430
+ .map((o) => ({ key: o.key, nodeId: o.nodeId, gateName: displayName(byId.get(nodesRec[o.nodeId].gatedBy)) })),
431
+ });
368
432
 
369
433
  // timeout/abort must fail the run even when local media finished after the deadline
370
434
  // (or nodes never observed the signal). Prefer the abort reason when present.