@fbp/evaluator 1.1.0 → 1.5.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.
@@ -3,7 +3,7 @@
3
3
  * These nodes handle JSON manipulation, flow control, and string operations.
4
4
  */
5
5
  /**
6
- * core/json/select - Extract a value from JSON by path
6
+ * core/json:select - Extract a value from JSON by path
7
7
  *
8
8
  * Props:
9
9
  * - path: string (dot-path like "a.b.c" or "data.user.email")
@@ -57,7 +57,7 @@ export const jsonSelectDef = {
57
57
  }
58
58
  };
59
59
  /**
60
- * core/json/object - Build a JSON object from named inputs
60
+ * core/json:object - Build a JSON object from named inputs
61
61
  *
62
62
  * This node has dynamic inputs - any input wired to it becomes a key in the output object.
63
63
  * The implementation receives all inputs as a Record<string, any>.
@@ -85,13 +85,15 @@ export const jsonObjectDef = {
85
85
  }
86
86
  };
87
87
  /**
88
- * core/flow/guard - Stop the flow if a condition fails
88
+ * core/flow:guard - Stop the flow if a condition fails
89
89
  *
90
90
  * Inputs:
91
91
  * - ok: boolean (the condition to check)
92
92
  * - error?: json (optional error info)
93
93
  *
94
- * Outputs:
94
+ * Outputs (only the arm that was taken is emitted — a declared port absent
95
+ * from a completed output is a path not taken, and emitting both arms is what
96
+ * makes both arms run):
95
97
  * - pass: signal (emitted if ok is true)
96
98
  * - fail: signal (emitted if ok is false)
97
99
  * - error: json (the error if failed)
@@ -115,23 +117,316 @@ export const flowGuardDef = {
115
117
  impl: (inputs) => {
116
118
  const { ok, error } = inputs;
117
119
  if (ok) {
118
- return {
119
- pass: true,
120
- fail: false,
121
- error: null
122
- };
123
- }
124
- else {
125
- return {
126
- pass: false,
127
- fail: true,
128
- error: error || { message: 'Guard condition failed' }
129
- };
120
+ return { pass: true };
121
+ }
122
+ return {
123
+ fail: true,
124
+ error: error || { message: 'Guard condition failed' }
125
+ };
126
+ }
127
+ };
128
+ const COMPARISONS = ['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'truthy', 'empty', 'contains'];
129
+ /**
130
+ * The predicate flow:branch and flow:switch share: an operator plus an expected
131
+ * value, never an expression to evaluate — a graph definition is data, and data
132
+ * that gets executed is a remote-code-execution surface.
133
+ *
134
+ * Kept in step with the worker-side implementation in `@fbp/flow`, which is the
135
+ * same predicate for the same node types: the editor's local preview and a
136
+ * server run must agree on which arm a graph takes.
137
+ */
138
+ const compare = (value, operator, expected) => {
139
+ switch (operator) {
140
+ case 'eq': return value === expected;
141
+ case 'neq': return value !== expected;
142
+ case 'gt': return Number(value) > Number(expected);
143
+ case 'gte': return Number(value) >= Number(expected);
144
+ case 'lt': return Number(value) < Number(expected);
145
+ case 'lte': return Number(value) <= Number(expected);
146
+ case 'truthy': return Boolean(value);
147
+ case 'empty':
148
+ if (value === null || value === undefined)
149
+ return true;
150
+ if (Array.isArray(value) || typeof value === 'string')
151
+ return value.length === 0;
152
+ if (typeof value === 'object')
153
+ return Object.keys(value).length === 0;
154
+ return false;
155
+ case 'contains':
156
+ if (Array.isArray(value))
157
+ return value.includes(expected);
158
+ return String(value ?? '').includes(String(expected ?? ''));
159
+ }
160
+ };
161
+ /**
162
+ * core/flow:branch - Route a value down one of two paths
163
+ *
164
+ * Only the taken port carries a value; the other is absent rather than null, so
165
+ * a downstream node can tell "not taken" from "taken with a null value".
166
+ * `matched` always carries the verdict, for anything that wants the decision
167
+ * without sitting on an arm.
168
+ */
169
+ export const flowBranchDef = {
170
+ context: 'js',
171
+ name: 'branch',
172
+ category: 'flow',
173
+ icon: 'git-branch',
174
+ inputs: [
175
+ { name: 'value', type: 'any' },
176
+ // Wired only when the comparison value comes from the graph rather than the prop.
177
+ { name: 'expected', type: 'any', optional: true }
178
+ ],
179
+ outputs: [
180
+ { name: 'matched', type: 'boolean' },
181
+ { name: 'true', type: 'any' },
182
+ { name: 'false', type: 'any' }
183
+ ],
184
+ props: [
185
+ { name: 'operator', type: 'string', default: 'truthy' },
186
+ { name: 'expected', type: 'any' }
187
+ ],
188
+ description: 'Route a value down one of two paths by a declared predicate',
189
+ impl: (inputs, props) => {
190
+ const operator = String(props.operator ?? 'truthy');
191
+ if (!COMPARISONS.includes(operator)) {
192
+ throw new Error(`flow:branch: unknown operator "${operator}" (expected one of ${COMPARISONS.join(', ')})`);
193
+ }
194
+ const expected = 'expected' in inputs ? inputs.expected : props.expected;
195
+ const matched = compare(inputs.value, operator, expected);
196
+ return matched
197
+ ? { matched, true: inputs.value }
198
+ : { matched, false: inputs.value };
199
+ }
200
+ };
201
+ /** The number of case ports flow:switch declares. */
202
+ const SWITCH_CASE_PORTS = 6;
203
+ /**
204
+ * core/flow:switch - Route a value to the first matching case, or to `default`
205
+ *
206
+ * Cases are ordered and the first match wins. Case `i` emits on `case_<i>`;
207
+ * the ports are declared up front and fixed in number, because a port a graph
208
+ * cannot name at design time is a port nothing can be wired to.
209
+ */
210
+ export const flowSwitchDef = {
211
+ context: 'js',
212
+ name: 'switch',
213
+ category: 'flow',
214
+ icon: 'git-fork',
215
+ inputs: [
216
+ { name: 'value', type: 'any' }
217
+ ],
218
+ outputs: [
219
+ { name: 'matched_case', type: 'string' },
220
+ ...Array.from({ length: SWITCH_CASE_PORTS }, (_, i) => ({ name: `case_${i}`, type: 'any' })),
221
+ { name: 'default', type: 'any' }
222
+ ],
223
+ props: [
224
+ { name: 'cases', type: 'json', default: [] },
225
+ { name: 'operator', type: 'string', default: 'eq' }
226
+ ],
227
+ description: 'Route a value to the first matching case, or to default',
228
+ impl: (inputs, props) => {
229
+ const rawCases = props.cases ?? [];
230
+ if (!Array.isArray(rawCases)) {
231
+ throw new Error('flow:switch: props.cases must be an array of { operator, expected }');
232
+ }
233
+ if (rawCases.length > SWITCH_CASE_PORTS) {
234
+ throw new Error(`flow:switch: ${rawCases.length} cases exceeds the ${SWITCH_CASE_PORTS} declared case ports`);
130
235
  }
236
+ const fallbackOperator = String(props.operator ?? 'eq');
237
+ for (let i = 0; i < rawCases.length; i++) {
238
+ const branchCase = rawCases[i];
239
+ if (branchCase === null || typeof branchCase !== 'object') {
240
+ throw new Error(`flow:switch: case ${i} must be an object of { operator, expected }`);
241
+ }
242
+ const operator = String(branchCase.operator ?? fallbackOperator);
243
+ if (!COMPARISONS.includes(operator)) {
244
+ throw new Error(`flow:switch: case ${i} has unknown operator "${operator}" (expected one of ${COMPARISONS.join(', ')})`);
245
+ }
246
+ if (compare(inputs.value, operator, branchCase.expected)) {
247
+ return { matched_case: String(i), [`case_${i}`]: inputs.value };
248
+ }
249
+ }
250
+ return { matched_case: 'default', default: inputs.value };
131
251
  }
132
252
  };
133
253
  /**
134
- * core/string/template - Build a string from a template with placeholders
254
+ * core/flow:merge - Rejoin two branch arms into one path
255
+ *
256
+ * The counterpart of a lazy branch: whichever arm produced a value wins, and
257
+ * `source` names it so a graph can tell which path it is now carrying. `a` wins
258
+ * a tie, which only happens when the inputs are not exclusive arms at all.
259
+ */
260
+ export const flowMergeDef = {
261
+ context: 'js',
262
+ name: 'merge',
263
+ category: 'flow',
264
+ icon: 'git-merge',
265
+ inputs: [
266
+ // Optional on purpose: a merge runs on whichever arm arrived, and a branch
267
+ // arm that was not taken never produces one.
268
+ { name: 'a', type: 'any', optional: true },
269
+ { name: 'b', type: 'any', optional: true }
270
+ ],
271
+ outputs: [
272
+ { name: 'value', type: 'any' },
273
+ { name: 'source', type: 'string' }
274
+ ],
275
+ props: [],
276
+ description: 'Rejoin two branch arms into one path',
277
+ impl: (inputs) => {
278
+ if (inputs.a !== undefined) {
279
+ return { value: inputs.a, source: 'a' };
280
+ }
281
+ if (inputs.b !== undefined) {
282
+ return { value: inputs.b, source: 'b' };
283
+ }
284
+ throw new Error('flow:merge: neither input arrived — nothing to merge');
285
+ }
286
+ };
287
+ /** Read a dot-path out of one collection element, for the nodes that key on a field. */
288
+ const selectPath = (item, path) => {
289
+ if (path === '')
290
+ return item;
291
+ return path.split('.').reduce((acc, segment) => {
292
+ if (acc === null || typeof acc !== 'object')
293
+ return undefined;
294
+ return acc[segment];
295
+ }, item);
296
+ };
297
+ const requireArray = (node, items) => {
298
+ if (!Array.isArray(items)) {
299
+ throw new Error(`${node}: items must be an array (received ${items === null ? 'null' : typeof items})`);
300
+ }
301
+ return items;
302
+ };
303
+ /**
304
+ * core/flow:filter - Narrow a collection by a declared predicate
305
+ *
306
+ * Same predicate vocabulary as flow:branch, applied per element — `path`
307
+ * compares a field of each element so a list of rows can be filtered without a
308
+ * per-item node. `items`, `dropped` and `empty` are lazy ports: a survivor lane
309
+ * only runs when something survived and a rejects lane only when something was
310
+ * rejected, so the "nothing to do" case is a path rather than a length test.
311
+ */
312
+ export const flowFilterDef = {
313
+ context: 'js',
314
+ name: 'filter',
315
+ category: 'flow',
316
+ icon: 'filter',
317
+ inputs: [
318
+ { name: 'items', type: 'json' },
319
+ // Wired only when the comparison value comes from the graph rather than the prop.
320
+ { name: 'expected', type: 'any', optional: true }
321
+ ],
322
+ outputs: [
323
+ { name: 'items', type: 'json' },
324
+ { name: 'dropped', type: 'json' },
325
+ { name: 'empty', type: 'boolean' },
326
+ { name: 'kept_count', type: 'number' },
327
+ { name: 'dropped_count', type: 'number' }
328
+ ],
329
+ props: [
330
+ { name: 'operator', type: 'string', default: 'truthy' },
331
+ { name: 'expected', type: 'any' },
332
+ { name: 'path', type: 'string', default: '' }
333
+ ],
334
+ description: 'Narrow a collection by a declared predicate',
335
+ impl: (inputs, props) => {
336
+ const operator = String(props.operator ?? 'truthy');
337
+ if (!COMPARISONS.includes(operator)) {
338
+ throw new Error(`flow:filter: unknown operator "${operator}" (expected one of ${COMPARISONS.join(', ')})`);
339
+ }
340
+ const items = requireArray('flow:filter', inputs.items);
341
+ const path = String(props.path ?? '');
342
+ const expected = 'expected' in inputs ? inputs.expected : props.expected;
343
+ const kept = [];
344
+ const dropped = [];
345
+ for (const item of items) {
346
+ if (compare(selectPath(item, path), operator, expected)) {
347
+ kept.push(item);
348
+ }
349
+ else {
350
+ dropped.push(item);
351
+ }
352
+ }
353
+ const outputs = {
354
+ kept_count: kept.length,
355
+ dropped_count: dropped.length
356
+ };
357
+ if (kept.length > 0)
358
+ outputs.items = kept;
359
+ else
360
+ outputs.empty = true;
361
+ if (dropped.length > 0)
362
+ outputs.dropped = dropped;
363
+ return outputs;
364
+ }
365
+ };
366
+ /** The ceiling flow:for_each refuses to plan beyond unless a graph raises it. */
367
+ const DEFAULT_MAX_SHARDS = 64;
368
+ /**
369
+ * core/flow:for_each - Turn a collection into an ordered, keyed fan-out plan
370
+ *
371
+ * Each element becomes a shard descriptor `{ index, total, item, key }`. The key
372
+ * is a declared field of the element (falling back to its index) because a
373
+ * shard's identity is what lets a retry land on the same shard; duplicate or
374
+ * missing keys fail the node rather than producing an unaddressable plan, and so
375
+ * does a collection larger than `max_shards` — a truncated plan is work that
376
+ * never ran with nothing saying so. `shards` is absent for an empty collection,
377
+ * where `empty` is present instead, so a graph never fans out over nothing.
378
+ */
379
+ export const flowForEachDef = {
380
+ context: 'js',
381
+ name: 'for_each',
382
+ category: 'flow',
383
+ icon: 'list-ordered',
384
+ inputs: [
385
+ { name: 'items', type: 'json' }
386
+ ],
387
+ outputs: [
388
+ { name: 'shards', type: 'json' },
389
+ { name: 'empty', type: 'boolean' },
390
+ { name: 'total', type: 'number' }
391
+ ],
392
+ props: [
393
+ { name: 'key', type: 'string', default: '' },
394
+ { name: 'max_shards', type: 'number', default: DEFAULT_MAX_SHARDS }
395
+ ],
396
+ description: 'Turn a collection into an ordered, keyed fan-out plan',
397
+ impl: (inputs, props) => {
398
+ const items = requireArray('flow:for_each', inputs.items);
399
+ const maxShards = Number(props.max_shards ?? DEFAULT_MAX_SHARDS);
400
+ if (!Number.isFinite(maxShards) || maxShards < 1) {
401
+ throw new Error(`flow:for_each: max_shards must be a positive number (received ${String(props.max_shards)})`);
402
+ }
403
+ if (items.length > maxShards) {
404
+ throw new Error(`flow:for_each: ${items.length} items exceeds max_shards ${maxShards} — raise the ceiling or filter the collection first`);
405
+ }
406
+ const keyPath = String(props.key ?? '');
407
+ const total = items.length;
408
+ const shards = items.map((item, index) => {
409
+ if (keyPath === '') {
410
+ return { index, total, item, key: String(index) };
411
+ }
412
+ const keyValue = selectPath(item, keyPath);
413
+ if (keyValue === null || keyValue === undefined || keyValue === '') {
414
+ throw new Error(`flow:for_each: item ${index} has no value at key path "${keyPath}" — a shard without a stable key cannot be retried`);
415
+ }
416
+ return { index, total, item, key: String(keyValue) };
417
+ });
418
+ const keys = shards.map((shard) => shard.key);
419
+ const duplicate = keys.find((key, index) => keys.indexOf(key) !== index);
420
+ if (duplicate !== undefined) {
421
+ throw new Error(`flow:for_each: key "${duplicate}" appears more than once at key path "${keyPath}" — shard keys must be unique`);
422
+ }
423
+ return total === 0
424
+ ? { total, empty: true }
425
+ : { total, shards };
426
+ }
427
+ };
428
+ /**
429
+ * core/string:template - Build a string from a template with placeholders
135
430
  *
136
431
  * Props:
137
432
  * - template: string (template with {{placeholder}} syntax)
@@ -172,7 +467,7 @@ export const stringTemplateDef = {
172
467
  }
173
468
  };
174
469
  /**
175
- * core/string/concat - Concatenate strings with an optional separator
470
+ * core/string:concat - Concatenate strings with an optional separator
176
471
  *
177
472
  * Props:
178
473
  * - prefix: string (optional prefix)
@@ -206,10 +501,118 @@ export const stringConcatDef = {
206
501
  return { value: `${prefix}${value}${suffix}` };
207
502
  }
208
503
  };
504
+ export const constStringDef = {
505
+ context: 'js',
506
+ name: 'string',
507
+ category: 'const',
508
+ icon: 'type',
509
+ outputs: [{ name: 'value', type: 'string' }],
510
+ props: [{ name: 'value', type: 'string', default: '' }],
511
+ description: 'Outputs a constant string value',
512
+ impl: (_inputs, props) => ({
513
+ value: props.value ?? ''
514
+ })
515
+ };
516
+ export const constBooleanDef = {
517
+ context: 'js',
518
+ name: 'boolean',
519
+ category: 'const',
520
+ icon: 'toggle',
521
+ outputs: [{ name: 'value', type: 'boolean' }],
522
+ props: [{ name: 'value', type: 'boolean', default: false }],
523
+ description: 'Outputs a constant boolean value',
524
+ impl: (_inputs, props) => ({
525
+ value: props.value ?? false
526
+ })
527
+ };
528
+ export const jsonMergeDef = {
529
+ context: 'js',
530
+ name: 'merge',
531
+ category: 'json',
532
+ icon: 'git-merge',
533
+ inputs: [
534
+ { name: 'a', type: 'json' },
535
+ { name: 'b', type: 'json' }
536
+ ],
537
+ outputs: [
538
+ { name: 'value', type: 'json' }
539
+ ],
540
+ description: 'Merge two JSON objects',
541
+ impl: (inputs) => ({
542
+ value: { ...(inputs.a || {}), ...(inputs.b || {}) }
543
+ })
544
+ };
545
+ export const jsonSplitDef = {
546
+ context: 'js',
547
+ name: 'split',
548
+ category: 'json',
549
+ icon: 'scissors',
550
+ inputs: [
551
+ { name: 'obj', type: 'json' }
552
+ ],
553
+ outputs: [
554
+ { name: 'selected', type: 'json' },
555
+ { name: 'rest', type: 'json' }
556
+ ],
557
+ props: [
558
+ { name: 'keys', type: 'string', default: '' }
559
+ ],
560
+ description: 'Split a JSON object by key list',
561
+ impl: (inputs, props) => {
562
+ const obj = inputs.obj || {};
563
+ const keyList = (props.keys || '').split(',').map((k) => k.trim()).filter(Boolean);
564
+ const keySet = new Set(keyList);
565
+ const selected = {};
566
+ const rest = {};
567
+ for (const [k, v] of Object.entries(obj)) {
568
+ if (keySet.has(k))
569
+ selected[k] = v;
570
+ else
571
+ rest[k] = v;
572
+ }
573
+ return { selected, rest };
574
+ }
575
+ };
576
+ export const flowCoerceDef = {
577
+ context: 'js',
578
+ name: 'coerce',
579
+ category: 'flow',
580
+ icon: 'repeat',
581
+ inputs: [
582
+ { name: 'value', type: 'any' }
583
+ ],
584
+ outputs: [
585
+ { name: 'value', type: 'any' }
586
+ ],
587
+ props: [
588
+ { name: 'type', type: 'string', default: 'string' }
589
+ ],
590
+ description: 'Convert a value to a different type',
591
+ impl: (inputs, props) => {
592
+ const { value } = inputs;
593
+ switch (props.type) {
594
+ case 'number': return { value: Number(value) || 0 };
595
+ case 'string': return { value: String(value ?? '') };
596
+ case 'boolean': return { value: Boolean(value) };
597
+ case 'json': return { value: typeof value === 'string' ? JSON.parse(value) : value };
598
+ default: return { value };
599
+ }
600
+ }
601
+ };
209
602
  export const coreDefinitions = [
603
+ constStringDef,
604
+ constBooleanDef,
210
605
  jsonSelectDef,
211
606
  jsonObjectDef,
607
+ jsonMergeDef,
608
+ jsonSplitDef,
212
609
  flowGuardDef,
610
+ flowBranchDef,
611
+ flowSwitchDef,
612
+ flowMergeDef,
613
+ flowFilterDef,
614
+ flowForEachDef,
615
+ flowCoerceDef,
213
616
  stringTemplateDef,
214
617
  stringConcatDef
215
618
  ];
@@ -43,8 +43,24 @@ export const multiplyDef = {
43
43
  product: (inputs.a ?? 0) * (inputs.b ?? 0)
44
44
  })
45
45
  };
46
+ export const subtractDef = {
47
+ context: 'js',
48
+ name: 'subtract',
49
+ category: 'math',
50
+ icon: 'minus',
51
+ inputs: [
52
+ { name: 'a', type: 'number' },
53
+ { name: 'b', type: 'number' }
54
+ ],
55
+ outputs: [{ name: 'difference', type: 'number' }],
56
+ description: 'Subtracts b from a',
57
+ impl: (inputs) => ({
58
+ difference: (inputs.a ?? 0) - (inputs.b ?? 0)
59
+ })
60
+ };
46
61
  export const mathDefinitions = [
47
62
  constNumberDef,
48
63
  addDef,
49
- multiplyDef
64
+ multiplyDef,
65
+ subtractDef
50
66
  ];
package/esm/evaluate.js CHANGED
@@ -1,8 +1,22 @@
1
1
  /**
2
- * Build a definition map key from context and name.
2
+ * Build a definition map key from context, category, and name.
3
3
  */
4
- function defKey(context, name) {
5
- return `${context}:${name}`;
4
+ function defKey(context, category, name) {
5
+ return `${context}:${category}:${name}`;
6
+ }
7
+ /**
8
+ * Parse a node type (e.g., 'math:add' or 'const/string') into [category, name].
9
+ * Colon is the canonical separator (category:name); slash is checked as fallback
10
+ * for legacy graph files. Boundary nodes (no separator) default to ['core', type].
11
+ */
12
+ function parseTaskIdentifier(type) {
13
+ const colonIdx = type.indexOf(':');
14
+ if (colonIdx !== -1)
15
+ return [type.slice(0, colonIdx), type.slice(colonIdx + 1)];
16
+ const slashIdx = type.indexOf('/');
17
+ if (slashIdx !== -1)
18
+ return [type.slice(0, slashIdx), type.slice(slashIdx + 1)];
19
+ return ['core', type];
6
20
  }
7
21
  /**
8
22
  * Evaluate a graph starting from the specified output node/port.
@@ -22,7 +36,7 @@ export async function evaluate(graph, options) {
22
36
  }
23
37
  const defMap = new Map();
24
38
  for (const def of definitions) {
25
- defMap.set(defKey(def.context, def.name), def);
39
+ defMap.set(defKey(def.context, def.category, def.name), def);
26
40
  }
27
41
  // Graph-level context used as default for node lookups
28
42
  const graphContext = graph.context || '';
@@ -143,29 +157,73 @@ export async function evaluate(graph, options) {
143
157
  }
144
158
  // Get the definition for this node — resolve context from node override or graph default
145
159
  const nodeContext = node.context || graphContext;
146
- const definition = defMap.get(defKey(nodeContext, node.type));
160
+ const [nodeCategory, nodeDefName] = parseTaskIdentifier(node.type);
161
+ const definition = defMap.get(defKey(nodeContext, nodeCategory, nodeDefName));
147
162
  if (!definition) {
148
163
  throw new Error(`No definition found for node: ${nodeContext}:${node.type}`);
149
164
  }
150
165
  // Collect inputs by evaluating upstream nodes
151
166
  const nodeInputs = {};
152
167
  const portEdges = edgesByDst.get(nodeName);
168
+ /**
169
+ * A branch emits only the port it took, so an edge out of the arm it did not
170
+ * take is an edge whose source port is *absent* — not one carrying
171
+ * `undefined`, which a node may legitimately produce. A node fed by such an
172
+ * edge on a required input is not runnable, and the preview says so the way
173
+ * the runtime does: the node produces no outputs at all, which leaves the
174
+ * arm dark for everything downstream of it and lets a `flow:merge` further
175
+ * on run on whichever arm did arrive. Optional inputs absorb the absence
176
+ * instead, which is what makes reconvergence expressible.
177
+ */
178
+ const notTaken = (outputs, port) => !(port in outputs);
179
+ const skipped = {};
180
+ const declaredPortNames = new Set();
153
181
  if (definition.inputs) {
154
182
  for (const inputPort of definition.inputs) {
183
+ declaredPortNames.add(inputPort.name);
155
184
  const edges = portEdges?.get(inputPort.name) ?? [];
156
185
  if (inputPort.multi) {
157
- const values = await Promise.all(edges.map(async (edge) => {
186
+ const arrived = await Promise.all(edges.map(async (edge) => {
158
187
  const upstreamOutputs = await evaluateNode(edge.src.node);
159
- return upstreamOutputs[edge.src.port];
188
+ return notTaken(upstreamOutputs, edge.src.port)
189
+ ? undefined
190
+ : { value: upstreamOutputs[edge.src.port] };
160
191
  }));
161
- nodeInputs[inputPort.name] = values;
192
+ const values = arrived.filter((slot) => slot !== undefined);
193
+ if (edges.length > 0 && values.length === 0 && !inputPort.optional) {
194
+ cache.set(nodeName, skipped);
195
+ return skipped;
196
+ }
197
+ nodeInputs[inputPort.name] = values.map((slot) => slot.value);
162
198
  }
163
199
  else {
164
200
  if (edges.length > 0) {
165
201
  const edge = edges[0];
166
202
  const upstreamOutputs = await evaluateNode(edge.src.node);
167
- nodeInputs[inputPort.name] = upstreamOutputs[edge.src.port];
203
+ if (notTaken(upstreamOutputs, edge.src.port)) {
204
+ if (!inputPort.optional) {
205
+ cache.set(nodeName, skipped);
206
+ return skipped;
207
+ }
208
+ }
209
+ else {
210
+ nodeInputs[inputPort.name] = upstreamOutputs[edge.src.port];
211
+ }
212
+ }
213
+ }
214
+ }
215
+ }
216
+ // Dynamic inputs: collect any edges targeting ports not declared in definition.inputs
217
+ if (portEdges) {
218
+ for (const [portName, edges] of portEdges) {
219
+ if (!declaredPortNames.has(portName) && edges.length > 0) {
220
+ const edge = edges[0];
221
+ const upstreamOutputs = await evaluateNode(edge.src.node);
222
+ if (notTaken(upstreamOutputs, edge.src.port)) {
223
+ cache.set(nodeName, skipped);
224
+ return skipped;
168
225
  }
226
+ nodeInputs[portName] = upstreamOutputs[edge.src.port];
169
227
  }
170
228
  }
171
229
  }
package/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { evaluate } from './evaluate';
2
+ export { validateGraph } from './validate';
2
3
  // Re-export node definitions for convenience
3
- export { mathDefinitions, constNumberDef, addDef, multiplyDef } from './definitions/math';
4
- export { uiDefinitions, pageDef, formDef, inputDef, buttonDef, textDef, graphInputDef, graphOutputDef, graphPropDef } from './definitions/ui';
5
- export { coreDefinitions, jsonSelectDef, jsonObjectDef, flowGuardDef, stringTemplateDef, stringConcatDef } from './definitions/core';
6
- export { netDefinitions, graphqlRequestDef } from './definitions/net';
4
+ export { constBooleanDef, constStringDef, coreDefinitions, flowBranchDef, flowCoerceDef, flowFilterDef, flowForEachDef, flowGuardDef, flowMergeDef, flowSwitchDef, jsonMergeDef, jsonObjectDef, jsonSelectDef, jsonSplitDef, stringConcatDef, stringTemplateDef } from './definitions/core';
5
+ export { addDef, constNumberDef, mathDefinitions, multiplyDef, subtractDef } from './definitions/math';
6
+ export { graphqlRequestDef, netDefinitions } from './definitions/net';
7
+ export { buttonDef, formDef, graphInputDef, graphOutputDef, graphPropDef, inputDef, pageDef, textDef, uiDefinitions } from './definitions/ui';