@fbp/evaluator 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/evaluate.js +104 -33
- package/esm/validate.js +13 -10
- package/evaluate.d.ts +19 -1
- package/evaluate.js +104 -32
- package/package.json +2 -2
- package/validate.js +11 -8
package/esm/evaluate.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, GRAPH_OUTPUT, GRAPH_PROP, hoistDefinitions, isRef, joinPath, parsePath, resolveRef, SUBNET, typeKey } from '@fbp/spec';
|
|
1
|
+
import { assertTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, GRAPH_OUTPUT, GRAPH_PROP, boundaryName, hoistDefinitions, isLiteral, isRef, joinPath, parsePath, plainValue, resolveRef, SUBNET, typeKey, unwiredInput } from '@fbp/spec';
|
|
2
2
|
import { findCycle, formatCycle } from './cycles';
|
|
3
3
|
/**
|
|
4
4
|
* Evaluate a graph starting from the specified output node/port.
|
|
@@ -26,32 +26,84 @@ export async function evaluate(graph, options) {
|
|
|
26
26
|
/**
|
|
27
27
|
* A `{ ref }` to a sibling node's value is a dependency, like an edge.
|
|
28
28
|
* `scopePath` is where `nodes` live, so absolute references are recognised too.
|
|
29
|
+
*
|
|
30
|
+
* A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
|
|
31
|
+
* its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
|
|
32
|
+
* the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
|
|
29
33
|
*/
|
|
30
|
-
export function refEdges(nodes, scopePath = []) {
|
|
34
|
+
export function refEdges(nodes, scopePath = [], wiring) {
|
|
31
35
|
const names = new Set(nodes.map(n => n.name));
|
|
32
36
|
const scope = joinPath(scopePath);
|
|
33
37
|
const edges = [];
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
const find = (segments) => {
|
|
39
|
+
if (segments.length <= scopePath.length || scopePath.some((part, i) => segments[i] !== part))
|
|
40
|
+
return undefined;
|
|
41
|
+
let level = nodes;
|
|
42
|
+
let found;
|
|
43
|
+
for (const part of segments.slice(scopePath.length)) {
|
|
44
|
+
found = level.find(n => n.name === part);
|
|
45
|
+
if (!found)
|
|
46
|
+
return undefined;
|
|
47
|
+
level = found.nodes ?? [];
|
|
48
|
+
}
|
|
49
|
+
return found;
|
|
50
|
+
};
|
|
51
|
+
// The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
|
|
52
|
+
const follow = (value, from, seen) => {
|
|
53
|
+
if (!isRef(value))
|
|
54
|
+
return [];
|
|
55
|
+
let target;
|
|
56
|
+
try {
|
|
57
|
+
target = resolveRef(joinPath(from), value.ref);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
const segments = parsePath(target.path);
|
|
63
|
+
const owner = segments.slice(0, -1);
|
|
64
|
+
if (target.prop === undefined) {
|
|
65
|
+
const sibling = segments[segments.length - 1];
|
|
66
|
+
return sibling !== undefined && names.has(sibling) && joinPath(owner) === scope ? [sibling] : [];
|
|
67
|
+
}
|
|
68
|
+
const key = `${target.path}.${target.prop}`;
|
|
69
|
+
if (seen.includes(key))
|
|
70
|
+
return [];
|
|
71
|
+
return follow(find(segments)?.props?.find(p => p.name === target.prop)?.value, owner, [...seen, key]);
|
|
72
|
+
};
|
|
73
|
+
const collect = (holder, holderScope, dependent, wires, context) => {
|
|
74
|
+
for (const prop of holder.props ?? []) {
|
|
36
75
|
if (!isRef(prop.value))
|
|
37
76
|
continue;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
catch {
|
|
77
|
+
// A typed value on an input that is wired is never read, unless the type also declares a prop of that name.
|
|
78
|
+
if (wiring && holder.type !== SUBNET &&
|
|
79
|
+
wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
|
|
80
|
+
!wiring.declaresProp(holder.context || context, holder.type, prop.name))
|
|
43
81
|
continue;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const sibling = segments[segments.length - 1];
|
|
47
|
-
if (target.prop === undefined && sibling !== undefined && names.has(sibling) &&
|
|
48
|
-
joinPath(segments.slice(0, -1)) === scope) {
|
|
49
|
-
edges.push({ src: { node: sibling, port: 'value' }, dst: { node: node.name, port: prop.name } });
|
|
82
|
+
for (const sibling of follow(prop.value, holderScope, [])) {
|
|
83
|
+
edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
|
|
50
84
|
}
|
|
51
85
|
}
|
|
52
|
-
|
|
86
|
+
if (holder.type !== SUBNET)
|
|
87
|
+
return;
|
|
88
|
+
for (const child of holder.nodes ?? []) {
|
|
89
|
+
collect(child, [...holderScope, holder.name], dependent, holder.edges ?? [], holder.context || context);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
for (const node of nodes)
|
|
93
|
+
collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
|
|
53
94
|
return edges;
|
|
54
95
|
}
|
|
96
|
+
/** `Wiring` for a scope, answered from its definitions. */
|
|
97
|
+
export function wiringOf(edges, context, definitions) {
|
|
98
|
+
return {
|
|
99
|
+
edges,
|
|
100
|
+
context,
|
|
101
|
+
declaresProp: (ctx, type, prop) => {
|
|
102
|
+
const definition = definitions.get(typeKey(ctx, type));
|
|
103
|
+
return !definition || (definition.props ?? []).some(def => def.name === prop);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
55
107
|
/**
|
|
56
108
|
* @param assetPath - definition keys of the digital assets being evaluated,
|
|
57
109
|
* outermost first, so an asset that contains itself throws instead of
|
|
@@ -62,7 +114,11 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
62
114
|
const { definitions } = options;
|
|
63
115
|
// Reject cycles up front, as the validator and the SQL gate do: pulling
|
|
64
116
|
// through a loop would otherwise recurse until the stack overflows.
|
|
65
|
-
const
|
|
117
|
+
const defMap = new Map();
|
|
118
|
+
for (const def of definitions) {
|
|
119
|
+
defMap.set(definitionKey(def), def);
|
|
120
|
+
}
|
|
121
|
+
const cycle = findCycle(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))]);
|
|
66
122
|
if (cycle) {
|
|
67
123
|
throw new Error(`Graph "${graph.name}" contains a cycle: ${formatCycle(cycle)}`);
|
|
68
124
|
}
|
|
@@ -71,10 +127,6 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
71
127
|
for (const node of graph.nodes) {
|
|
72
128
|
nodeMap.set(node.name, node);
|
|
73
129
|
}
|
|
74
|
-
const defMap = new Map();
|
|
75
|
-
for (const def of definitions) {
|
|
76
|
-
defMap.set(definitionKey(def), def);
|
|
77
|
-
}
|
|
78
130
|
// Graph-level context used as default for node lookups
|
|
79
131
|
const graphContext = graph.context || '';
|
|
80
132
|
// Build edge lookup: destination node -> destination port -> edges (in array order)
|
|
@@ -96,9 +148,12 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
96
148
|
/**
|
|
97
149
|
* Resolve a prop value: a `{ ref }` takes the value of the node it points at
|
|
98
150
|
* (its `value` output, or the named prop for a '.prop' reference). The target
|
|
99
|
-
* is in this scope or one that encloses it.
|
|
151
|
+
* is in this scope or one that encloses it. A `{ literal }` is its content,
|
|
152
|
+
* verbatim.
|
|
100
153
|
*/
|
|
101
154
|
async function resolve(value, holder, chain = []) {
|
|
155
|
+
if (isLiteral(value))
|
|
156
|
+
return value.literal;
|
|
102
157
|
if (!isRef(value))
|
|
103
158
|
return value;
|
|
104
159
|
const target = resolveRef(joinPath(scopePath), value.ref);
|
|
@@ -145,9 +200,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
145
200
|
// Handle system nodes (graphInput, graphOutput, graphProp)
|
|
146
201
|
// System nodes are identified by their type name — reserved, no definition lookup needed
|
|
147
202
|
if (node.type === GRAPH_INPUT) {
|
|
148
|
-
|
|
149
|
-
const portNameProp = node.props?.find(p => p.name === 'portName');
|
|
150
|
-
const inputName = portNameProp?.value || nodeName;
|
|
203
|
+
const inputName = boundaryName(node);
|
|
151
204
|
// A wired input whose arm was not taken stays dark, default or not, as it would in the flat graph.
|
|
152
205
|
if (absent?.has(inputName)) {
|
|
153
206
|
cache.set(nodeName, {});
|
|
@@ -157,19 +210,17 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
157
210
|
let value = inputs[inputName];
|
|
158
211
|
if (value === undefined) {
|
|
159
212
|
const defaultProp = node.props?.find(p => p.name === 'default');
|
|
160
|
-
value = defaultProp?.value;
|
|
213
|
+
value = plainValue(defaultProp?.value);
|
|
161
214
|
}
|
|
162
215
|
const result = { value };
|
|
163
216
|
cache.set(nodeName, result);
|
|
164
217
|
return result;
|
|
165
218
|
}
|
|
166
219
|
if (node.type === GRAPH_PROP) {
|
|
167
|
-
|
|
168
|
-
const propNameProp = node.props?.find(p => p.name === 'propName');
|
|
169
|
-
const propName = propNameProp?.value || nodeName;
|
|
220
|
+
const propName = boundaryName(node);
|
|
170
221
|
let value = props[propName];
|
|
171
222
|
if (value === undefined) {
|
|
172
|
-
value = node.props?.find(p => p.name === 'default')?.value;
|
|
223
|
+
value = plainValue(node.props?.find(p => p.name === 'default')?.value);
|
|
173
224
|
}
|
|
174
225
|
const result = { value };
|
|
175
226
|
cache.set(nodeName, result);
|
|
@@ -217,6 +268,14 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
217
268
|
}
|
|
218
269
|
}
|
|
219
270
|
}
|
|
271
|
+
// An unwired input takes the subnet's prop of the same name; the graphInput's default comes after.
|
|
272
|
+
for (const inputPort of inputPorts) {
|
|
273
|
+
if (inputPort.multi || (subnetPortEdges?.get(inputPort.name) ?? []).length > 0)
|
|
274
|
+
continue;
|
|
275
|
+
const stored = unwiredInput(node, inputPort.name);
|
|
276
|
+
if (stored !== undefined)
|
|
277
|
+
subnetInputs[inputPort.name] = await resolve(stored, nodeName);
|
|
278
|
+
}
|
|
220
279
|
// Promoted parameters: the value set on the subnet node, else the graphProp's default.
|
|
221
280
|
const subnetProps = {};
|
|
222
281
|
for (const propDef of derivePropDefinitions(node)) {
|
|
@@ -229,7 +288,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
229
288
|
const pull = openGraph({ name: `${nodeName}_subnet`, context: node.context || graphContext || undefined, nodes: children, edges: node.edges ?? [] }, { definitions, inputs: subnetInputs, props: subnetProps, absent: absentInputs }, assetPath, [...scopePath, nodeName], scope);
|
|
230
289
|
const subnetOutputs = {};
|
|
231
290
|
for (const outputPort of outputPorts) {
|
|
232
|
-
const outputBoundaryNode = children.find(n => n.type === GRAPH_OUTPUT && n
|
|
291
|
+
const outputBoundaryNode = children.find(n => n.type === GRAPH_OUTPUT && boundaryName(n) === outputPort.name);
|
|
233
292
|
if (!outputBoundaryNode) {
|
|
234
293
|
throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
|
|
235
294
|
}
|
|
@@ -325,6 +384,19 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
325
384
|
}
|
|
326
385
|
}
|
|
327
386
|
}
|
|
387
|
+
// Unwired: the node's prop of the same name, else the definition's default for it. Read once
|
|
388
|
+
// the wires are known to have arrived, so a node on an arm not taken resolves nothing.
|
|
389
|
+
const ports = [...(definition.inputs ?? []), ...(definition.graph ? derivePorts(definition.graph).inputs : [])];
|
|
390
|
+
const multi = new Set(ports.filter(port => port.multi).map(port => port.name));
|
|
391
|
+
for (const port of new Set(ports.map(port => port.name))) {
|
|
392
|
+
if (multi.has(port))
|
|
393
|
+
continue;
|
|
394
|
+
if ((portEdges?.get(port) ?? []).length > 0)
|
|
395
|
+
continue;
|
|
396
|
+
const stored = unwiredInput(node, port, definition.props);
|
|
397
|
+
if (stored !== undefined)
|
|
398
|
+
nodeInputs[port] = await resolve(stored, nodeName);
|
|
399
|
+
}
|
|
328
400
|
// Get props from node instance (override definition defaults)
|
|
329
401
|
const nodeProps = {};
|
|
330
402
|
if (definition.props) {
|
|
@@ -348,10 +420,9 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
348
420
|
const pull = openGraph({ name: `${nodeName}_asset`, context: assetGraph.context || definition.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, inputs: nodeInputs, props: nodeProps, absent: absentInputs }, [...assetPath, key]);
|
|
349
421
|
const assetOutputs = {};
|
|
350
422
|
for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type === GRAPH_OUTPUT)) {
|
|
351
|
-
const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
|
|
352
423
|
const result = await pull(outputBoundaryNode.name);
|
|
353
424
|
if ('value' in result)
|
|
354
|
-
assetOutputs[
|
|
425
|
+
assetOutputs[boundaryName(outputBoundaryNode)] = result.value;
|
|
355
426
|
}
|
|
356
427
|
cache.set(nodeName, assetOutputs);
|
|
357
428
|
return assetOutputs;
|
package/esm/validate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { checkTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, hoistDefinitions, isBoundaryType, isNodeType, SUBNET, typeKey } from '@fbp/spec';
|
|
1
|
+
import { boundaryName, checkTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, hoistDefinitions, isBoundaryType, isNodeType, plainValue, SUBNET, typeKey, unwiredInput } from '@fbp/spec';
|
|
2
2
|
import { findCycle, formatCycle } from './cycles';
|
|
3
|
-
import { refEdges } from './evaluate';
|
|
3
|
+
import { refEdges, wiringOf } from './evaluate';
|
|
4
4
|
/** Build "dst node -> dst port -> has at least one incoming edge". */
|
|
5
5
|
function connectedPorts(edges) {
|
|
6
6
|
const map = new Map();
|
|
@@ -15,8 +15,8 @@ function connectedPorts(edges) {
|
|
|
15
15
|
return map;
|
|
16
16
|
}
|
|
17
17
|
function nodeHasProp(node, propName) {
|
|
18
|
-
const
|
|
19
|
-
return
|
|
18
|
+
const value = plainValue(node.props?.find(pr => pr.name === propName)?.value);
|
|
19
|
+
return value !== undefined && value !== null;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* Statically validate a flow graph before publish/execution.
|
|
@@ -83,7 +83,10 @@ export function validateGraph(graph, options = {}) {
|
|
|
83
83
|
const visit = (g, pathPrefix, scope = []) => {
|
|
84
84
|
const graphContext = g.context || '';
|
|
85
85
|
const connected = connectedPorts(g.edges ?? []);
|
|
86
|
-
const cycle = findCycle((g.nodes ?? []).map(n => n.name), [
|
|
86
|
+
const cycle = findCycle((g.nodes ?? []).map(n => n.name), [
|
|
87
|
+
...(g.edges ?? []),
|
|
88
|
+
...refEdges(g.nodes ?? [], scope, wiringOf(g.edges ?? [], graphContext, defMap))
|
|
89
|
+
]);
|
|
87
90
|
if (cycle) {
|
|
88
91
|
const path = cycle.map(name => qualify(pathPrefix, name));
|
|
89
92
|
errors.push({
|
|
@@ -103,16 +106,16 @@ export function validateGraph(graph, options = {}) {
|
|
|
103
106
|
const children = node.nodes ?? [];
|
|
104
107
|
const defaulted = new Set(children
|
|
105
108
|
.filter(n => n.type === GRAPH_INPUT && nodeHasProp(n, 'default'))
|
|
106
|
-
.map(
|
|
109
|
+
.map(boundaryName));
|
|
107
110
|
for (const port of derivePorts(node).inputs) {
|
|
108
111
|
if (port.optional === true || port.multi === true || defaulted.has(port.name))
|
|
109
112
|
continue;
|
|
110
|
-
if (!nodeConnected.has(port.name)) {
|
|
113
|
+
if (!nodeConnected.has(port.name) && plainValue(unwiredInput(node, port.name)) == null) {
|
|
111
114
|
errors.push({
|
|
112
115
|
code: 'MISSING_REQUIRED_INPUT',
|
|
113
116
|
node: qualifiedName,
|
|
114
117
|
port: port.name,
|
|
115
|
-
message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected`,
|
|
118
|
+
message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected or set`,
|
|
116
119
|
});
|
|
117
120
|
}
|
|
118
121
|
}
|
|
@@ -142,12 +145,12 @@ export function validateGraph(graph, options = {}) {
|
|
|
142
145
|
for (const port of definition.inputs ?? []) {
|
|
143
146
|
if (port.optional === true || port.multi === true)
|
|
144
147
|
continue;
|
|
145
|
-
if (!nodeConnected.has(port.name)) {
|
|
148
|
+
if (!nodeConnected.has(port.name) && plainValue(unwiredInput(node, port.name, definition.props)) == null) {
|
|
146
149
|
errors.push({
|
|
147
150
|
code: 'MISSING_REQUIRED_INPUT',
|
|
148
151
|
node: qualifiedName,
|
|
149
152
|
port: port.name,
|
|
150
|
-
message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected`,
|
|
153
|
+
message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected or set`,
|
|
151
154
|
});
|
|
152
155
|
}
|
|
153
156
|
}
|
package/evaluate.d.ts
CHANGED
|
@@ -15,5 +15,23 @@ export declare function evaluate(graph: Graph, options: EvaluateOptions): Promis
|
|
|
15
15
|
/**
|
|
16
16
|
* A `{ ref }` to a sibling node's value is a dependency, like an edge.
|
|
17
17
|
* `scopePath` is where `nodes` live, so absolute references are recognised too.
|
|
18
|
+
*
|
|
19
|
+
* A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
|
|
20
|
+
* its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
|
|
21
|
+
* the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
|
|
18
22
|
*/
|
|
19
|
-
export declare function refEdges(nodes: Node[], scopePath?: string[]): Edge[];
|
|
23
|
+
export declare function refEdges(nodes: Node[], scopePath?: string[], wiring?: Wiring): Edge[];
|
|
24
|
+
/** What `refEdges` needs to tell a typed value that a wire overrides from one that is read. */
|
|
25
|
+
export interface Wiring {
|
|
26
|
+
/** The edges and context of the scope the nodes live in. */
|
|
27
|
+
edges: Edge[];
|
|
28
|
+
context: string;
|
|
29
|
+
/** Whether the type declares a prop of this name; unknown types should answer `true`. */
|
|
30
|
+
declaresProp(context: string, type: string, prop: string): boolean;
|
|
31
|
+
}
|
|
32
|
+
/** `Wiring` for a scope, answered from its definitions. */
|
|
33
|
+
export declare function wiringOf(edges: Edge[], context: string, definitions: Map<string, {
|
|
34
|
+
props?: {
|
|
35
|
+
name: string;
|
|
36
|
+
}[];
|
|
37
|
+
}>): Wiring;
|
package/evaluate.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.evaluate = evaluate;
|
|
4
4
|
exports.refEdges = refEdges;
|
|
5
|
+
exports.wiringOf = wiringOf;
|
|
5
6
|
const spec_1 = require("@fbp/spec");
|
|
6
7
|
const cycles_1 = require("./cycles");
|
|
7
8
|
/**
|
|
@@ -30,32 +31,84 @@ async function evaluate(graph, options) {
|
|
|
30
31
|
/**
|
|
31
32
|
* A `{ ref }` to a sibling node's value is a dependency, like an edge.
|
|
32
33
|
* `scopePath` is where `nodes` live, so absolute references are recognised too.
|
|
34
|
+
*
|
|
35
|
+
* A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
|
|
36
|
+
* its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
|
|
37
|
+
* the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
|
|
33
38
|
*/
|
|
34
|
-
function refEdges(nodes, scopePath = []) {
|
|
39
|
+
function refEdges(nodes, scopePath = [], wiring) {
|
|
35
40
|
const names = new Set(nodes.map(n => n.name));
|
|
36
41
|
const scope = (0, spec_1.joinPath)(scopePath);
|
|
37
42
|
const edges = [];
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
const find = (segments) => {
|
|
44
|
+
if (segments.length <= scopePath.length || scopePath.some((part, i) => segments[i] !== part))
|
|
45
|
+
return undefined;
|
|
46
|
+
let level = nodes;
|
|
47
|
+
let found;
|
|
48
|
+
for (const part of segments.slice(scopePath.length)) {
|
|
49
|
+
found = level.find(n => n.name === part);
|
|
50
|
+
if (!found)
|
|
51
|
+
return undefined;
|
|
52
|
+
level = found.nodes ?? [];
|
|
53
|
+
}
|
|
54
|
+
return found;
|
|
55
|
+
};
|
|
56
|
+
// The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
|
|
57
|
+
const follow = (value, from, seen) => {
|
|
58
|
+
if (!(0, spec_1.isRef)(value))
|
|
59
|
+
return [];
|
|
60
|
+
let target;
|
|
61
|
+
try {
|
|
62
|
+
target = (0, spec_1.resolveRef)((0, spec_1.joinPath)(from), value.ref);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const segments = (0, spec_1.parsePath)(target.path);
|
|
68
|
+
const owner = segments.slice(0, -1);
|
|
69
|
+
if (target.prop === undefined) {
|
|
70
|
+
const sibling = segments[segments.length - 1];
|
|
71
|
+
return sibling !== undefined && names.has(sibling) && (0, spec_1.joinPath)(owner) === scope ? [sibling] : [];
|
|
72
|
+
}
|
|
73
|
+
const key = `${target.path}.${target.prop}`;
|
|
74
|
+
if (seen.includes(key))
|
|
75
|
+
return [];
|
|
76
|
+
return follow(find(segments)?.props?.find(p => p.name === target.prop)?.value, owner, [...seen, key]);
|
|
77
|
+
};
|
|
78
|
+
const collect = (holder, holderScope, dependent, wires, context) => {
|
|
79
|
+
for (const prop of holder.props ?? []) {
|
|
40
80
|
if (!(0, spec_1.isRef)(prop.value))
|
|
41
81
|
continue;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
catch {
|
|
82
|
+
// A typed value on an input that is wired is never read, unless the type also declares a prop of that name.
|
|
83
|
+
if (wiring && holder.type !== spec_1.SUBNET &&
|
|
84
|
+
wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
|
|
85
|
+
!wiring.declaresProp(holder.context || context, holder.type, prop.name))
|
|
47
86
|
continue;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const sibling = segments[segments.length - 1];
|
|
51
|
-
if (target.prop === undefined && sibling !== undefined && names.has(sibling) &&
|
|
52
|
-
(0, spec_1.joinPath)(segments.slice(0, -1)) === scope) {
|
|
53
|
-
edges.push({ src: { node: sibling, port: 'value' }, dst: { node: node.name, port: prop.name } });
|
|
87
|
+
for (const sibling of follow(prop.value, holderScope, [])) {
|
|
88
|
+
edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
|
|
54
89
|
}
|
|
55
90
|
}
|
|
56
|
-
|
|
91
|
+
if (holder.type !== spec_1.SUBNET)
|
|
92
|
+
return;
|
|
93
|
+
for (const child of holder.nodes ?? []) {
|
|
94
|
+
collect(child, [...holderScope, holder.name], dependent, holder.edges ?? [], holder.context || context);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
for (const node of nodes)
|
|
98
|
+
collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
|
|
57
99
|
return edges;
|
|
58
100
|
}
|
|
101
|
+
/** `Wiring` for a scope, answered from its definitions. */
|
|
102
|
+
function wiringOf(edges, context, definitions) {
|
|
103
|
+
return {
|
|
104
|
+
edges,
|
|
105
|
+
context,
|
|
106
|
+
declaresProp: (ctx, type, prop) => {
|
|
107
|
+
const definition = definitions.get((0, spec_1.typeKey)(ctx, type));
|
|
108
|
+
return !definition || (definition.props ?? []).some(def => def.name === prop);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
59
112
|
/**
|
|
60
113
|
* @param assetPath - definition keys of the digital assets being evaluated,
|
|
61
114
|
* outermost first, so an asset that contains itself throws instead of
|
|
@@ -66,7 +119,11 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
66
119
|
const { definitions } = options;
|
|
67
120
|
// Reject cycles up front, as the validator and the SQL gate do: pulling
|
|
68
121
|
// through a loop would otherwise recurse until the stack overflows.
|
|
69
|
-
const
|
|
122
|
+
const defMap = new Map();
|
|
123
|
+
for (const def of definitions) {
|
|
124
|
+
defMap.set((0, spec_1.definitionKey)(def), def);
|
|
125
|
+
}
|
|
126
|
+
const cycle = (0, cycles_1.findCycle)(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))]);
|
|
70
127
|
if (cycle) {
|
|
71
128
|
throw new Error(`Graph "${graph.name}" contains a cycle: ${(0, cycles_1.formatCycle)(cycle)}`);
|
|
72
129
|
}
|
|
@@ -75,10 +132,6 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
75
132
|
for (const node of graph.nodes) {
|
|
76
133
|
nodeMap.set(node.name, node);
|
|
77
134
|
}
|
|
78
|
-
const defMap = new Map();
|
|
79
|
-
for (const def of definitions) {
|
|
80
|
-
defMap.set((0, spec_1.definitionKey)(def), def);
|
|
81
|
-
}
|
|
82
135
|
// Graph-level context used as default for node lookups
|
|
83
136
|
const graphContext = graph.context || '';
|
|
84
137
|
// Build edge lookup: destination node -> destination port -> edges (in array order)
|
|
@@ -100,9 +153,12 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
100
153
|
/**
|
|
101
154
|
* Resolve a prop value: a `{ ref }` takes the value of the node it points at
|
|
102
155
|
* (its `value` output, or the named prop for a '.prop' reference). The target
|
|
103
|
-
* is in this scope or one that encloses it.
|
|
156
|
+
* is in this scope or one that encloses it. A `{ literal }` is its content,
|
|
157
|
+
* verbatim.
|
|
104
158
|
*/
|
|
105
159
|
async function resolve(value, holder, chain = []) {
|
|
160
|
+
if ((0, spec_1.isLiteral)(value))
|
|
161
|
+
return value.literal;
|
|
106
162
|
if (!(0, spec_1.isRef)(value))
|
|
107
163
|
return value;
|
|
108
164
|
const target = (0, spec_1.resolveRef)((0, spec_1.joinPath)(scopePath), value.ref);
|
|
@@ -149,9 +205,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
149
205
|
// Handle system nodes (graphInput, graphOutput, graphProp)
|
|
150
206
|
// System nodes are identified by their type name — reserved, no definition lookup needed
|
|
151
207
|
if (node.type === spec_1.GRAPH_INPUT) {
|
|
152
|
-
|
|
153
|
-
const portNameProp = node.props?.find(p => p.name === 'portName');
|
|
154
|
-
const inputName = portNameProp?.value || nodeName;
|
|
208
|
+
const inputName = (0, spec_1.boundaryName)(node);
|
|
155
209
|
// A wired input whose arm was not taken stays dark, default or not, as it would in the flat graph.
|
|
156
210
|
if (absent?.has(inputName)) {
|
|
157
211
|
cache.set(nodeName, {});
|
|
@@ -161,19 +215,17 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
161
215
|
let value = inputs[inputName];
|
|
162
216
|
if (value === undefined) {
|
|
163
217
|
const defaultProp = node.props?.find(p => p.name === 'default');
|
|
164
|
-
value = defaultProp?.value;
|
|
218
|
+
value = (0, spec_1.plainValue)(defaultProp?.value);
|
|
165
219
|
}
|
|
166
220
|
const result = { value };
|
|
167
221
|
cache.set(nodeName, result);
|
|
168
222
|
return result;
|
|
169
223
|
}
|
|
170
224
|
if (node.type === spec_1.GRAPH_PROP) {
|
|
171
|
-
|
|
172
|
-
const propNameProp = node.props?.find(p => p.name === 'propName');
|
|
173
|
-
const propName = propNameProp?.value || nodeName;
|
|
225
|
+
const propName = (0, spec_1.boundaryName)(node);
|
|
174
226
|
let value = props[propName];
|
|
175
227
|
if (value === undefined) {
|
|
176
|
-
value = node.props?.find(p => p.name === 'default')?.value;
|
|
228
|
+
value = (0, spec_1.plainValue)(node.props?.find(p => p.name === 'default')?.value);
|
|
177
229
|
}
|
|
178
230
|
const result = { value };
|
|
179
231
|
cache.set(nodeName, result);
|
|
@@ -221,6 +273,14 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
221
273
|
}
|
|
222
274
|
}
|
|
223
275
|
}
|
|
276
|
+
// An unwired input takes the subnet's prop of the same name; the graphInput's default comes after.
|
|
277
|
+
for (const inputPort of inputPorts) {
|
|
278
|
+
if (inputPort.multi || (subnetPortEdges?.get(inputPort.name) ?? []).length > 0)
|
|
279
|
+
continue;
|
|
280
|
+
const stored = (0, spec_1.unwiredInput)(node, inputPort.name);
|
|
281
|
+
if (stored !== undefined)
|
|
282
|
+
subnetInputs[inputPort.name] = await resolve(stored, nodeName);
|
|
283
|
+
}
|
|
224
284
|
// Promoted parameters: the value set on the subnet node, else the graphProp's default.
|
|
225
285
|
const subnetProps = {};
|
|
226
286
|
for (const propDef of (0, spec_1.derivePropDefinitions)(node)) {
|
|
@@ -233,7 +293,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
233
293
|
const pull = openGraph({ name: `${nodeName}_subnet`, context: node.context || graphContext || undefined, nodes: children, edges: node.edges ?? [] }, { definitions, inputs: subnetInputs, props: subnetProps, absent: absentInputs }, assetPath, [...scopePath, nodeName], scope);
|
|
234
294
|
const subnetOutputs = {};
|
|
235
295
|
for (const outputPort of outputPorts) {
|
|
236
|
-
const outputBoundaryNode = children.find(n => n.type === spec_1.GRAPH_OUTPUT &&
|
|
296
|
+
const outputBoundaryNode = children.find(n => n.type === spec_1.GRAPH_OUTPUT && (0, spec_1.boundaryName)(n) === outputPort.name);
|
|
237
297
|
if (!outputBoundaryNode) {
|
|
238
298
|
throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
|
|
239
299
|
}
|
|
@@ -329,6 +389,19 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
329
389
|
}
|
|
330
390
|
}
|
|
331
391
|
}
|
|
392
|
+
// Unwired: the node's prop of the same name, else the definition's default for it. Read once
|
|
393
|
+
// the wires are known to have arrived, so a node on an arm not taken resolves nothing.
|
|
394
|
+
const ports = [...(definition.inputs ?? []), ...(definition.graph ? (0, spec_1.derivePorts)(definition.graph).inputs : [])];
|
|
395
|
+
const multi = new Set(ports.filter(port => port.multi).map(port => port.name));
|
|
396
|
+
for (const port of new Set(ports.map(port => port.name))) {
|
|
397
|
+
if (multi.has(port))
|
|
398
|
+
continue;
|
|
399
|
+
if ((portEdges?.get(port) ?? []).length > 0)
|
|
400
|
+
continue;
|
|
401
|
+
const stored = (0, spec_1.unwiredInput)(node, port, definition.props);
|
|
402
|
+
if (stored !== undefined)
|
|
403
|
+
nodeInputs[port] = await resolve(stored, nodeName);
|
|
404
|
+
}
|
|
332
405
|
// Get props from node instance (override definition defaults)
|
|
333
406
|
const nodeProps = {};
|
|
334
407
|
if (definition.props) {
|
|
@@ -352,10 +425,9 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
352
425
|
const pull = openGraph({ name: `${nodeName}_asset`, context: assetGraph.context || definition.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, inputs: nodeInputs, props: nodeProps, absent: absentInputs }, [...assetPath, key]);
|
|
353
426
|
const assetOutputs = {};
|
|
354
427
|
for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type === spec_1.GRAPH_OUTPUT)) {
|
|
355
|
-
const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
|
|
356
428
|
const result = await pull(outputBoundaryNode.name);
|
|
357
429
|
if ('value' in result)
|
|
358
|
-
assetOutputs[
|
|
430
|
+
assetOutputs[(0, spec_1.boundaryName)(outputBoundaryNode)] = result.value;
|
|
359
431
|
}
|
|
360
432
|
cache.set(nodeName, assetOutputs);
|
|
361
433
|
return assetOutputs;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fbp/evaluator",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "FBP graph evaluator — node definitions, graph evaluation and validation",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"directory": "dist"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@fbp/spec": "2.
|
|
15
|
+
"@fbp/spec": "2.1.0"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/jest": "^30.0.0",
|
package/validate.js
CHANGED
|
@@ -18,8 +18,8 @@ function connectedPorts(edges) {
|
|
|
18
18
|
return map;
|
|
19
19
|
}
|
|
20
20
|
function nodeHasProp(node, propName) {
|
|
21
|
-
const
|
|
22
|
-
return
|
|
21
|
+
const value = (0, spec_1.plainValue)(node.props?.find(pr => pr.name === propName)?.value);
|
|
22
|
+
return value !== undefined && value !== null;
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
25
|
* Statically validate a flow graph before publish/execution.
|
|
@@ -86,7 +86,10 @@ function validateGraph(graph, options = {}) {
|
|
|
86
86
|
const visit = (g, pathPrefix, scope = []) => {
|
|
87
87
|
const graphContext = g.context || '';
|
|
88
88
|
const connected = connectedPorts(g.edges ?? []);
|
|
89
|
-
const cycle = (0, cycles_1.findCycle)((g.nodes ?? []).map(n => n.name), [
|
|
89
|
+
const cycle = (0, cycles_1.findCycle)((g.nodes ?? []).map(n => n.name), [
|
|
90
|
+
...(g.edges ?? []),
|
|
91
|
+
...(0, evaluate_1.refEdges)(g.nodes ?? [], scope, (0, evaluate_1.wiringOf)(g.edges ?? [], graphContext, defMap))
|
|
92
|
+
]);
|
|
90
93
|
if (cycle) {
|
|
91
94
|
const path = cycle.map(name => qualify(pathPrefix, name));
|
|
92
95
|
errors.push({
|
|
@@ -106,16 +109,16 @@ function validateGraph(graph, options = {}) {
|
|
|
106
109
|
const children = node.nodes ?? [];
|
|
107
110
|
const defaulted = new Set(children
|
|
108
111
|
.filter(n => n.type === spec_1.GRAPH_INPUT && nodeHasProp(n, 'default'))
|
|
109
|
-
.map(
|
|
112
|
+
.map(spec_1.boundaryName));
|
|
110
113
|
for (const port of (0, spec_1.derivePorts)(node).inputs) {
|
|
111
114
|
if (port.optional === true || port.multi === true || defaulted.has(port.name))
|
|
112
115
|
continue;
|
|
113
|
-
if (!nodeConnected.has(port.name)) {
|
|
116
|
+
if (!nodeConnected.has(port.name) && (0, spec_1.plainValue)((0, spec_1.unwiredInput)(node, port.name)) == null) {
|
|
114
117
|
errors.push({
|
|
115
118
|
code: 'MISSING_REQUIRED_INPUT',
|
|
116
119
|
node: qualifiedName,
|
|
117
120
|
port: port.name,
|
|
118
|
-
message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected`,
|
|
121
|
+
message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected or set`,
|
|
119
122
|
});
|
|
120
123
|
}
|
|
121
124
|
}
|
|
@@ -145,12 +148,12 @@ function validateGraph(graph, options = {}) {
|
|
|
145
148
|
for (const port of definition.inputs ?? []) {
|
|
146
149
|
if (port.optional === true || port.multi === true)
|
|
147
150
|
continue;
|
|
148
|
-
if (!nodeConnected.has(port.name)) {
|
|
151
|
+
if (!nodeConnected.has(port.name) && (0, spec_1.plainValue)((0, spec_1.unwiredInput)(node, port.name, definition.props)) == null) {
|
|
149
152
|
errors.push({
|
|
150
153
|
code: 'MISSING_REQUIRED_INPUT',
|
|
151
154
|
node: qualifiedName,
|
|
152
155
|
port: port.name,
|
|
153
|
-
message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected`,
|
|
156
|
+
message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected or set`,
|
|
154
157
|
});
|
|
155
158
|
}
|
|
156
159
|
}
|