@fbp/evaluator 1.0.0 → 1.3.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 +4 -84
- package/definitions/core.d.ts +60 -6
- package/definitions/core.js +432 -29
- package/definitions/math.d.ts +1 -0
- package/definitions/math.js +21 -5
- package/definitions/net.js +2 -2
- package/definitions/ui.js +13 -13
- package/esm/definitions/core.js +431 -28
- package/esm/definitions/math.js +20 -4
- package/esm/definitions/net.js +2 -2
- package/esm/definitions/ui.js +13 -13
- package/esm/evaluate.js +100 -22
- package/esm/index.js +5 -4
- package/esm/validate.js +147 -0
- package/evaluate.js +100 -22
- package/index.d.ts +7 -5
- package/index.js +31 -18
- package/package.json +20 -26
- package/validate.d.ts +54 -0
- package/validate.js +150 -0
- package/esm/definitions/core.d.ts +0 -72
- package/esm/definitions/math.d.ts +0 -8
- package/esm/definitions/net.d.ts +0 -25
- package/esm/definitions/ui.d.ts +0 -10
- package/esm/evaluate.d.ts +0 -12
- package/esm/index.d.ts +0 -6
- package/esm/types.d.ts +0 -38
package/esm/definitions/net.js
CHANGED
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
* - ok: boolean (true if request succeeded without errors)
|
|
22
22
|
*/
|
|
23
23
|
export const graphqlRequestDef = {
|
|
24
|
-
context: '
|
|
24
|
+
context: 'js',
|
|
25
|
+
name: 'request',
|
|
25
26
|
category: 'graphql',
|
|
26
|
-
type: 'net/graphql/request',
|
|
27
27
|
icon: 'graphql',
|
|
28
28
|
inputs: [
|
|
29
29
|
{ name: 'variables', type: 'json' },
|
package/esm/definitions/ui.js
CHANGED
|
@@ -22,8 +22,8 @@ function coerceValue(value, valueType) {
|
|
|
22
22
|
// Boundary node definitions for graph inputs/outputs
|
|
23
23
|
export const graphInputDef = {
|
|
24
24
|
context: 'core',
|
|
25
|
+
name: 'graphInput',
|
|
25
26
|
category: 'graph',
|
|
26
|
-
type: 'core/graph/input',
|
|
27
27
|
icon: 'arrow-right',
|
|
28
28
|
inputs: [],
|
|
29
29
|
outputs: [{ name: 'value', type: 'any' }],
|
|
@@ -36,8 +36,8 @@ export const graphInputDef = {
|
|
|
36
36
|
};
|
|
37
37
|
export const graphOutputDef = {
|
|
38
38
|
context: 'core',
|
|
39
|
+
name: 'graphOutput',
|
|
39
40
|
category: 'graph',
|
|
40
|
-
type: 'core/graph/output',
|
|
41
41
|
icon: 'arrow-left',
|
|
42
42
|
inputs: [{ name: 'value', type: 'any' }],
|
|
43
43
|
outputs: [],
|
|
@@ -49,8 +49,8 @@ export const graphOutputDef = {
|
|
|
49
49
|
};
|
|
50
50
|
export const graphPropDef = {
|
|
51
51
|
context: 'core',
|
|
52
|
+
name: 'graphProp',
|
|
52
53
|
category: 'graph',
|
|
53
|
-
type: 'core/graph/prop',
|
|
54
54
|
icon: 'settings',
|
|
55
55
|
inputs: [],
|
|
56
56
|
outputs: [{ name: 'value', type: 'any' }],
|
|
@@ -62,9 +62,9 @@ export const graphPropDef = {
|
|
|
62
62
|
impl: (_inputs, props) => ({ value: coerceValue(props?.value ?? props?.default, props?.valueType ?? 'any') })
|
|
63
63
|
};
|
|
64
64
|
export const pageDef = {
|
|
65
|
-
context: '
|
|
65
|
+
context: 'js',
|
|
66
|
+
name: 'Page',
|
|
66
67
|
category: 'layout',
|
|
67
|
-
type: 'ui/layout/Page',
|
|
68
68
|
icon: 'file',
|
|
69
69
|
inputs: [
|
|
70
70
|
{ name: 'children', type: 'Element[]', multi: true }
|
|
@@ -85,9 +85,9 @@ export const pageDef = {
|
|
|
85
85
|
})
|
|
86
86
|
};
|
|
87
87
|
export const formDef = {
|
|
88
|
-
context: '
|
|
88
|
+
context: 'js',
|
|
89
|
+
name: 'Form',
|
|
89
90
|
category: 'form',
|
|
90
|
-
type: 'ui/form/Form',
|
|
91
91
|
icon: 'file-text',
|
|
92
92
|
inputs: [
|
|
93
93
|
{ name: 'children', type: 'Element[]', multi: true }
|
|
@@ -108,9 +108,9 @@ export const formDef = {
|
|
|
108
108
|
})
|
|
109
109
|
};
|
|
110
110
|
export const inputDef = {
|
|
111
|
-
context: '
|
|
111
|
+
context: 'js',
|
|
112
|
+
name: 'Input',
|
|
112
113
|
category: 'form',
|
|
113
|
-
type: 'ui/form/Input',
|
|
114
114
|
icon: 'text-cursor',
|
|
115
115
|
inputs: [],
|
|
116
116
|
outputs: [{ name: 'element', type: 'Element' }],
|
|
@@ -134,9 +134,9 @@ export const inputDef = {
|
|
|
134
134
|
})
|
|
135
135
|
};
|
|
136
136
|
export const buttonDef = {
|
|
137
|
-
context: '
|
|
137
|
+
context: 'js',
|
|
138
|
+
name: 'Button',
|
|
138
139
|
category: 'form',
|
|
139
|
-
type: 'ui/form/Button',
|
|
140
140
|
icon: 'square',
|
|
141
141
|
inputs: [],
|
|
142
142
|
outputs: [{ name: 'element', type: 'Element' }],
|
|
@@ -158,9 +158,9 @@ export const buttonDef = {
|
|
|
158
158
|
})
|
|
159
159
|
};
|
|
160
160
|
export const textDef = {
|
|
161
|
-
context: '
|
|
161
|
+
context: 'js',
|
|
162
|
+
name: 'Text',
|
|
162
163
|
category: 'content',
|
|
163
|
-
type: 'ui/content/Text',
|
|
164
164
|
icon: 'type',
|
|
165
165
|
inputs: [],
|
|
166
166
|
outputs: [{ name: 'element', type: 'Element' }],
|
package/esm/evaluate.js
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build a definition map key from context, category, and name.
|
|
3
|
+
*/
|
|
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];
|
|
20
|
+
}
|
|
1
21
|
/**
|
|
2
22
|
* Evaluate a graph starting from the specified output node/port.
|
|
3
23
|
* Uses lazy evaluation - only evaluates nodes that are needed for the output.
|
|
@@ -16,8 +36,10 @@ export async function evaluate(graph, options) {
|
|
|
16
36
|
}
|
|
17
37
|
const defMap = new Map();
|
|
18
38
|
for (const def of definitions) {
|
|
19
|
-
defMap.set(def.
|
|
39
|
+
defMap.set(defKey(def.context, def.category, def.name), def);
|
|
20
40
|
}
|
|
41
|
+
// Graph-level context used as default for node lookups
|
|
42
|
+
const graphContext = graph.context || '';
|
|
21
43
|
// Build edge lookup: destination node -> destination port -> edges (in array order)
|
|
22
44
|
const edgesByDst = new Map();
|
|
23
45
|
for (const edge of graph.edges) {
|
|
@@ -47,8 +69,8 @@ export async function evaluate(graph, options) {
|
|
|
47
69
|
if (!node) {
|
|
48
70
|
throw new Error(`Node not found: ${nodeName}`);
|
|
49
71
|
}
|
|
50
|
-
// Handle
|
|
51
|
-
//
|
|
72
|
+
// Handle system nodes (graphInput, graphOutput, graphProp)
|
|
73
|
+
// System nodes are identified by their type name — reserved, no definition lookup needed
|
|
52
74
|
if (node.type === 'graphInput') {
|
|
53
75
|
// Get the port name from the portName property
|
|
54
76
|
const portNameProp = node.props?.find(p => p.name === 'portName');
|
|
@@ -86,8 +108,8 @@ export async function evaluate(graph, options) {
|
|
|
86
108
|
cache.set(nodeName, result);
|
|
87
109
|
return result;
|
|
88
110
|
}
|
|
89
|
-
// Handle subnet nodes (
|
|
90
|
-
if (node.
|
|
111
|
+
// Handle subnet nodes (type: 'subnet' with inline nodes/edges)
|
|
112
|
+
if (node.type === 'subnet' && node.nodes && node.edges) {
|
|
91
113
|
// Collect inputs for the subnet by evaluating upstream nodes
|
|
92
114
|
const subnetInputs = {};
|
|
93
115
|
const subnetPortEdges = edgesByDst.get(nodeName);
|
|
@@ -118,6 +140,7 @@ export async function evaluate(graph, options) {
|
|
|
118
140
|
// Recursively evaluate the subnet's internal graph
|
|
119
141
|
const subnetResult = await evaluate({
|
|
120
142
|
name: `${nodeName}_subnet`,
|
|
143
|
+
context: graphContext || undefined,
|
|
121
144
|
nodes: node.nodes,
|
|
122
145
|
edges: node.edges
|
|
123
146
|
}, {
|
|
@@ -132,56 +155,111 @@ export async function evaluate(graph, options) {
|
|
|
132
155
|
cache.set(nodeName, subnetOutputs);
|
|
133
156
|
return subnetOutputs;
|
|
134
157
|
}
|
|
135
|
-
// Get the definition for this node
|
|
136
|
-
const
|
|
158
|
+
// Get the definition for this node — resolve context from node override or graph default
|
|
159
|
+
const nodeContext = node.context || graphContext;
|
|
160
|
+
const [nodeCategory, nodeDefName] = parseTaskIdentifier(node.type);
|
|
161
|
+
const definition = defMap.get(defKey(nodeContext, nodeCategory, nodeDefName));
|
|
137
162
|
if (!definition) {
|
|
138
|
-
throw new Error(`No definition found for node
|
|
139
|
-
}
|
|
140
|
-
if (!definition.impl) {
|
|
141
|
-
throw new Error(`No implementation found for node type: ${node.type}`);
|
|
163
|
+
throw new Error(`No definition found for node: ${nodeContext}:${node.type}`);
|
|
142
164
|
}
|
|
143
165
|
// Collect inputs by evaluating upstream nodes
|
|
144
166
|
const nodeInputs = {};
|
|
145
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();
|
|
146
181
|
if (definition.inputs) {
|
|
147
182
|
for (const inputPort of definition.inputs) {
|
|
183
|
+
declaredPortNames.add(inputPort.name);
|
|
148
184
|
const edges = portEdges?.get(inputPort.name) ?? [];
|
|
149
185
|
if (inputPort.multi) {
|
|
150
|
-
|
|
151
|
-
const values = await Promise.all(edges.map(async (edge) => {
|
|
186
|
+
const arrived = await Promise.all(edges.map(async (edge) => {
|
|
152
187
|
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
153
|
-
return upstreamOutputs
|
|
188
|
+
return notTaken(upstreamOutputs, edge.src.port)
|
|
189
|
+
? undefined
|
|
190
|
+
: { value: upstreamOutputs[edge.src.port] };
|
|
154
191
|
}));
|
|
155
|
-
|
|
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);
|
|
156
198
|
}
|
|
157
199
|
else {
|
|
158
|
-
// Single input: take first edge if exists
|
|
159
200
|
if (edges.length > 0) {
|
|
160
201
|
const edge = edges[0];
|
|
161
202
|
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
162
|
-
|
|
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
|
+
}
|
|
163
212
|
}
|
|
164
213
|
}
|
|
165
214
|
}
|
|
166
215
|
}
|
|
167
|
-
//
|
|
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;
|
|
225
|
+
}
|
|
226
|
+
nodeInputs[portName] = upstreamOutputs[edge.src.port];
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// Get props from node instance (override definition defaults)
|
|
168
231
|
const nodeProps = {};
|
|
169
232
|
if (definition.props) {
|
|
170
233
|
for (const propDef of definition.props) {
|
|
171
|
-
// First check node instance props
|
|
172
234
|
const instanceProp = node.props?.find(p => p.name === propDef.name);
|
|
173
235
|
if (instanceProp !== undefined && instanceProp.value !== undefined) {
|
|
174
236
|
nodeProps[propDef.name] = instanceProp.value;
|
|
175
237
|
}
|
|
176
238
|
else if (propDef.default !== undefined) {
|
|
177
|
-
// Fall back to definition default
|
|
178
239
|
nodeProps[propDef.name] = propDef.default;
|
|
179
240
|
}
|
|
180
241
|
}
|
|
181
242
|
}
|
|
182
|
-
//
|
|
243
|
+
// Digital asset: definition has an internal graph — evaluate it recursively
|
|
244
|
+
if (definition.graph) {
|
|
245
|
+
const assetGraph = definition.graph;
|
|
246
|
+
// Find all output boundary nodes in the asset's graph
|
|
247
|
+
const assetOutputs = {};
|
|
248
|
+
const outputBoundaryNodes = assetGraph.nodes.filter(n => n.type === 'graphOutput');
|
|
249
|
+
for (const outputBoundaryNode of outputBoundaryNodes) {
|
|
250
|
+
const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
|
|
251
|
+
const portName = portNameProp?.value || outputBoundaryNode.name;
|
|
252
|
+
const result = await evaluate({ name: `${nodeName}_asset`, context: assetGraph.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, outputNode: outputBoundaryNode.name, outputPort: 'value', inputs: nodeInputs, props: nodeProps });
|
|
253
|
+
assetOutputs[portName] = result;
|
|
254
|
+
}
|
|
255
|
+
cache.set(nodeName, assetOutputs);
|
|
256
|
+
return assetOutputs;
|
|
257
|
+
}
|
|
258
|
+
// Leaf node: call the implementation function
|
|
259
|
+
if (!definition.impl) {
|
|
260
|
+
throw new Error(`No implementation found for node: ${nodeContext}:${node.type}`);
|
|
261
|
+
}
|
|
183
262
|
const outputs = await definition.impl(nodeInputs, nodeProps);
|
|
184
|
-
// Cache and return
|
|
185
263
|
cache.set(nodeName, outputs);
|
|
186
264
|
return outputs;
|
|
187
265
|
}
|
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 {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
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';
|
package/esm/validate.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Boundary node types. These are satisfied by the runtime (graphInput/graphProp
|
|
3
|
+
* receive values from the execution payload; graphOutput is a pass-through), so
|
|
4
|
+
* they are never checked for required inputs/props.
|
|
5
|
+
*/
|
|
6
|
+
const BOUNDARY_TYPES = new Set(['graphInput', 'graphOutput', 'graphProp']);
|
|
7
|
+
function defKey(context, category, name) {
|
|
8
|
+
return `${context}:${category}:${name}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Parse a node type (e.g., 'math:add' or 'const/string') into [category, name].
|
|
12
|
+
* Mirrors the evaluator's resolution so pre-flight and evaluation stay aligned.
|
|
13
|
+
*/
|
|
14
|
+
function parseTaskIdentifier(type) {
|
|
15
|
+
const colonIdx = type.indexOf(':');
|
|
16
|
+
if (colonIdx !== -1)
|
|
17
|
+
return [type.slice(0, colonIdx), type.slice(colonIdx + 1)];
|
|
18
|
+
const slashIdx = type.indexOf('/');
|
|
19
|
+
if (slashIdx !== -1)
|
|
20
|
+
return [type.slice(0, slashIdx), type.slice(slashIdx + 1)];
|
|
21
|
+
return ['core', type];
|
|
22
|
+
}
|
|
23
|
+
/** Build "dst node -> dst port -> has at least one incoming edge". */
|
|
24
|
+
function connectedPorts(edges) {
|
|
25
|
+
const map = new Map();
|
|
26
|
+
for (const edge of edges) {
|
|
27
|
+
let ports = map.get(edge.dst.node);
|
|
28
|
+
if (!ports) {
|
|
29
|
+
ports = new Set();
|
|
30
|
+
map.set(edge.dst.node, ports);
|
|
31
|
+
}
|
|
32
|
+
ports.add(edge.dst.port);
|
|
33
|
+
}
|
|
34
|
+
return map;
|
|
35
|
+
}
|
|
36
|
+
function nodeHasProp(node, propName) {
|
|
37
|
+
const p = node.props?.find(pr => pr.name === propName);
|
|
38
|
+
return p !== undefined && p.value !== undefined && p.value !== null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Statically validate a flow graph before publish/execution.
|
|
42
|
+
*
|
|
43
|
+
* Heuristic (intentionally not a proof — see the caveats below) that mirrors the
|
|
44
|
+
* SQL `validate_function_graph` definition-aware pass:
|
|
45
|
+
*
|
|
46
|
+
* - For every non-boundary node whose definition resolves, each **declared**
|
|
47
|
+
* input port that is required (`optional !== true`) and non-`multi` must have
|
|
48
|
+
* at least one incoming edge, else `MISSING_REQUIRED_INPUT`.
|
|
49
|
+
* - Each definition prop that is `required` and has no `default` must be
|
|
50
|
+
* supplied by a node instance prop value, else `MISSING_REQUIRED_PROP`.
|
|
51
|
+
* - Inline `subnet` nodes are checked against their declared interface inputs
|
|
52
|
+
* and recursed into.
|
|
53
|
+
* - Digital-asset definitions (definitions carrying an internal `graph`) are
|
|
54
|
+
* recursed into.
|
|
55
|
+
*
|
|
56
|
+
* Deliberately NOT flagged (kept consistent with the evaluator + SQL):
|
|
57
|
+
* - `graphInput` / `graphProp` boundaries — supplied at runtime.
|
|
58
|
+
* - `multi` inputs — zero-or-more, never required.
|
|
59
|
+
* - Dynamic/undeclared input ports — the evaluator accepts them, so requiredness
|
|
60
|
+
* is only enforced for declared ports.
|
|
61
|
+
* - Nodes with no resolvable definition — treated as opaque (may be a runtime
|
|
62
|
+
* handler), exactly as the SQL validator and evaluator do.
|
|
63
|
+
*
|
|
64
|
+
* @returns the list of validation errors; an empty array means the graph is
|
|
65
|
+
* statically valid. Never throws.
|
|
66
|
+
*/
|
|
67
|
+
export function validateGraph(graph, options = {}) {
|
|
68
|
+
const errors = [];
|
|
69
|
+
const defMap = new Map();
|
|
70
|
+
const registerDefs = (defs) => {
|
|
71
|
+
if (!defs)
|
|
72
|
+
return;
|
|
73
|
+
for (const def of defs) {
|
|
74
|
+
const category = def.category ?? 'core';
|
|
75
|
+
defMap.set(defKey(def.context, category, def.name), def);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
registerDefs(options.definitions);
|
|
79
|
+
const visit = (g, pathPrefix) => {
|
|
80
|
+
// Definitions can be embedded per-graph (digital assets carry their own).
|
|
81
|
+
registerDefs(g.definitions);
|
|
82
|
+
const graphContext = g.context || '';
|
|
83
|
+
const connected = connectedPorts(g.edges ?? []);
|
|
84
|
+
for (const node of g.nodes ?? []) {
|
|
85
|
+
if (BOUNDARY_TYPES.has(node.type))
|
|
86
|
+
continue;
|
|
87
|
+
const qualifiedName = pathPrefix ? `${pathPrefix}/${node.name}` : node.name;
|
|
88
|
+
const nodeConnected = connected.get(node.name) ?? new Set();
|
|
89
|
+
// Inline subnet: validate against its declared interface, then recurse.
|
|
90
|
+
if (node.type === 'subnet' && node.nodes) {
|
|
91
|
+
for (const port of node.inputs ?? []) {
|
|
92
|
+
if (port.optional === true || port.multi === true)
|
|
93
|
+
continue;
|
|
94
|
+
if (!nodeConnected.has(port.name)) {
|
|
95
|
+
errors.push({
|
|
96
|
+
code: 'MISSING_REQUIRED_INPUT',
|
|
97
|
+
node: qualifiedName,
|
|
98
|
+
port: port.name,
|
|
99
|
+
message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected`,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
visit({ name: `${node.name}_subnet`, context: graphContext || undefined, nodes: node.nodes, edges: node.edges ?? [] }, qualifiedName);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const nodeContext = node.context || graphContext;
|
|
107
|
+
const [category, name] = parseTaskIdentifier(node.type);
|
|
108
|
+
const definition = defMap.get(defKey(nodeContext, category, name));
|
|
109
|
+
// Unknown definition: opaque node (possibly a runtime handler). Skip —
|
|
110
|
+
// matches the SQL validator + evaluator, which do not require a definition.
|
|
111
|
+
if (!definition)
|
|
112
|
+
continue;
|
|
113
|
+
for (const port of definition.inputs ?? []) {
|
|
114
|
+
if (port.optional === true || port.multi === true)
|
|
115
|
+
continue;
|
|
116
|
+
if (!nodeConnected.has(port.name)) {
|
|
117
|
+
errors.push({
|
|
118
|
+
code: 'MISSING_REQUIRED_INPUT',
|
|
119
|
+
node: qualifiedName,
|
|
120
|
+
port: port.name,
|
|
121
|
+
message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected`,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
for (const prop of definition.props ?? []) {
|
|
126
|
+
if (prop.required !== true)
|
|
127
|
+
continue;
|
|
128
|
+
if (prop.default !== undefined)
|
|
129
|
+
continue;
|
|
130
|
+
if (!nodeHasProp(node, prop.name)) {
|
|
131
|
+
errors.push({
|
|
132
|
+
code: 'MISSING_REQUIRED_PROP',
|
|
133
|
+
node: qualifiedName,
|
|
134
|
+
prop: prop.name,
|
|
135
|
+
message: `Required prop "${prop.name}" of node "${qualifiedName}" (${node.type}) is not set`,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// Digital asset: recurse into the definition's internal graph.
|
|
140
|
+
if (definition.graph) {
|
|
141
|
+
visit(definition.graph, qualifiedName);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
visit(graph, '');
|
|
146
|
+
return errors;
|
|
147
|
+
}
|
package/evaluate.js
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.evaluate = evaluate;
|
|
4
|
+
/**
|
|
5
|
+
* Build a definition map key from context, category, and name.
|
|
6
|
+
*/
|
|
7
|
+
function defKey(context, category, name) {
|
|
8
|
+
return `${context}:${category}:${name}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Parse a node type (e.g., 'math:add' or 'const/string') into [category, name].
|
|
12
|
+
* Colon is the canonical separator (category:name); slash is checked as fallback
|
|
13
|
+
* for legacy graph files. Boundary nodes (no separator) default to ['core', type].
|
|
14
|
+
*/
|
|
15
|
+
function parseTaskIdentifier(type) {
|
|
16
|
+
const colonIdx = type.indexOf(':');
|
|
17
|
+
if (colonIdx !== -1)
|
|
18
|
+
return [type.slice(0, colonIdx), type.slice(colonIdx + 1)];
|
|
19
|
+
const slashIdx = type.indexOf('/');
|
|
20
|
+
if (slashIdx !== -1)
|
|
21
|
+
return [type.slice(0, slashIdx), type.slice(slashIdx + 1)];
|
|
22
|
+
return ['core', type];
|
|
23
|
+
}
|
|
4
24
|
/**
|
|
5
25
|
* Evaluate a graph starting from the specified output node/port.
|
|
6
26
|
* Uses lazy evaluation - only evaluates nodes that are needed for the output.
|
|
@@ -19,8 +39,10 @@ async function evaluate(graph, options) {
|
|
|
19
39
|
}
|
|
20
40
|
const defMap = new Map();
|
|
21
41
|
for (const def of definitions) {
|
|
22
|
-
defMap.set(def.
|
|
42
|
+
defMap.set(defKey(def.context, def.category, def.name), def);
|
|
23
43
|
}
|
|
44
|
+
// Graph-level context used as default for node lookups
|
|
45
|
+
const graphContext = graph.context || '';
|
|
24
46
|
// Build edge lookup: destination node -> destination port -> edges (in array order)
|
|
25
47
|
const edgesByDst = new Map();
|
|
26
48
|
for (const edge of graph.edges) {
|
|
@@ -50,8 +72,8 @@ async function evaluate(graph, options) {
|
|
|
50
72
|
if (!node) {
|
|
51
73
|
throw new Error(`Node not found: ${nodeName}`);
|
|
52
74
|
}
|
|
53
|
-
// Handle
|
|
54
|
-
//
|
|
75
|
+
// Handle system nodes (graphInput, graphOutput, graphProp)
|
|
76
|
+
// System nodes are identified by their type name — reserved, no definition lookup needed
|
|
55
77
|
if (node.type === 'graphInput') {
|
|
56
78
|
// Get the port name from the portName property
|
|
57
79
|
const portNameProp = node.props?.find(p => p.name === 'portName');
|
|
@@ -89,8 +111,8 @@ async function evaluate(graph, options) {
|
|
|
89
111
|
cache.set(nodeName, result);
|
|
90
112
|
return result;
|
|
91
113
|
}
|
|
92
|
-
// Handle subnet nodes (
|
|
93
|
-
if (node.
|
|
114
|
+
// Handle subnet nodes (type: 'subnet' with inline nodes/edges)
|
|
115
|
+
if (node.type === 'subnet' && node.nodes && node.edges) {
|
|
94
116
|
// Collect inputs for the subnet by evaluating upstream nodes
|
|
95
117
|
const subnetInputs = {};
|
|
96
118
|
const subnetPortEdges = edgesByDst.get(nodeName);
|
|
@@ -121,6 +143,7 @@ async function evaluate(graph, options) {
|
|
|
121
143
|
// Recursively evaluate the subnet's internal graph
|
|
122
144
|
const subnetResult = await evaluate({
|
|
123
145
|
name: `${nodeName}_subnet`,
|
|
146
|
+
context: graphContext || undefined,
|
|
124
147
|
nodes: node.nodes,
|
|
125
148
|
edges: node.edges
|
|
126
149
|
}, {
|
|
@@ -135,56 +158,111 @@ async function evaluate(graph, options) {
|
|
|
135
158
|
cache.set(nodeName, subnetOutputs);
|
|
136
159
|
return subnetOutputs;
|
|
137
160
|
}
|
|
138
|
-
// Get the definition for this node
|
|
139
|
-
const
|
|
161
|
+
// Get the definition for this node — resolve context from node override or graph default
|
|
162
|
+
const nodeContext = node.context || graphContext;
|
|
163
|
+
const [nodeCategory, nodeDefName] = parseTaskIdentifier(node.type);
|
|
164
|
+
const definition = defMap.get(defKey(nodeContext, nodeCategory, nodeDefName));
|
|
140
165
|
if (!definition) {
|
|
141
|
-
throw new Error(`No definition found for node
|
|
142
|
-
}
|
|
143
|
-
if (!definition.impl) {
|
|
144
|
-
throw new Error(`No implementation found for node type: ${node.type}`);
|
|
166
|
+
throw new Error(`No definition found for node: ${nodeContext}:${node.type}`);
|
|
145
167
|
}
|
|
146
168
|
// Collect inputs by evaluating upstream nodes
|
|
147
169
|
const nodeInputs = {};
|
|
148
170
|
const portEdges = edgesByDst.get(nodeName);
|
|
171
|
+
/**
|
|
172
|
+
* A branch emits only the port it took, so an edge out of the arm it did not
|
|
173
|
+
* take is an edge whose source port is *absent* — not one carrying
|
|
174
|
+
* `undefined`, which a node may legitimately produce. A node fed by such an
|
|
175
|
+
* edge on a required input is not runnable, and the preview says so the way
|
|
176
|
+
* the runtime does: the node produces no outputs at all, which leaves the
|
|
177
|
+
* arm dark for everything downstream of it and lets a `flow:merge` further
|
|
178
|
+
* on run on whichever arm did arrive. Optional inputs absorb the absence
|
|
179
|
+
* instead, which is what makes reconvergence expressible.
|
|
180
|
+
*/
|
|
181
|
+
const notTaken = (outputs, port) => !(port in outputs);
|
|
182
|
+
const skipped = {};
|
|
183
|
+
const declaredPortNames = new Set();
|
|
149
184
|
if (definition.inputs) {
|
|
150
185
|
for (const inputPort of definition.inputs) {
|
|
186
|
+
declaredPortNames.add(inputPort.name);
|
|
151
187
|
const edges = portEdges?.get(inputPort.name) ?? [];
|
|
152
188
|
if (inputPort.multi) {
|
|
153
|
-
|
|
154
|
-
const values = await Promise.all(edges.map(async (edge) => {
|
|
189
|
+
const arrived = await Promise.all(edges.map(async (edge) => {
|
|
155
190
|
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
156
|
-
return upstreamOutputs
|
|
191
|
+
return notTaken(upstreamOutputs, edge.src.port)
|
|
192
|
+
? undefined
|
|
193
|
+
: { value: upstreamOutputs[edge.src.port] };
|
|
157
194
|
}));
|
|
158
|
-
|
|
195
|
+
const values = arrived.filter((slot) => slot !== undefined);
|
|
196
|
+
if (edges.length > 0 && values.length === 0 && !inputPort.optional) {
|
|
197
|
+
cache.set(nodeName, skipped);
|
|
198
|
+
return skipped;
|
|
199
|
+
}
|
|
200
|
+
nodeInputs[inputPort.name] = values.map((slot) => slot.value);
|
|
159
201
|
}
|
|
160
202
|
else {
|
|
161
|
-
// Single input: take first edge if exists
|
|
162
203
|
if (edges.length > 0) {
|
|
163
204
|
const edge = edges[0];
|
|
164
205
|
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
165
|
-
|
|
206
|
+
if (notTaken(upstreamOutputs, edge.src.port)) {
|
|
207
|
+
if (!inputPort.optional) {
|
|
208
|
+
cache.set(nodeName, skipped);
|
|
209
|
+
return skipped;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
nodeInputs[inputPort.name] = upstreamOutputs[edge.src.port];
|
|
214
|
+
}
|
|
166
215
|
}
|
|
167
216
|
}
|
|
168
217
|
}
|
|
169
218
|
}
|
|
170
|
-
//
|
|
219
|
+
// Dynamic inputs: collect any edges targeting ports not declared in definition.inputs
|
|
220
|
+
if (portEdges) {
|
|
221
|
+
for (const [portName, edges] of portEdges) {
|
|
222
|
+
if (!declaredPortNames.has(portName) && edges.length > 0) {
|
|
223
|
+
const edge = edges[0];
|
|
224
|
+
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
225
|
+
if (notTaken(upstreamOutputs, edge.src.port)) {
|
|
226
|
+
cache.set(nodeName, skipped);
|
|
227
|
+
return skipped;
|
|
228
|
+
}
|
|
229
|
+
nodeInputs[portName] = upstreamOutputs[edge.src.port];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
// Get props from node instance (override definition defaults)
|
|
171
234
|
const nodeProps = {};
|
|
172
235
|
if (definition.props) {
|
|
173
236
|
for (const propDef of definition.props) {
|
|
174
|
-
// First check node instance props
|
|
175
237
|
const instanceProp = node.props?.find(p => p.name === propDef.name);
|
|
176
238
|
if (instanceProp !== undefined && instanceProp.value !== undefined) {
|
|
177
239
|
nodeProps[propDef.name] = instanceProp.value;
|
|
178
240
|
}
|
|
179
241
|
else if (propDef.default !== undefined) {
|
|
180
|
-
// Fall back to definition default
|
|
181
242
|
nodeProps[propDef.name] = propDef.default;
|
|
182
243
|
}
|
|
183
244
|
}
|
|
184
245
|
}
|
|
185
|
-
//
|
|
246
|
+
// Digital asset: definition has an internal graph — evaluate it recursively
|
|
247
|
+
if (definition.graph) {
|
|
248
|
+
const assetGraph = definition.graph;
|
|
249
|
+
// Find all output boundary nodes in the asset's graph
|
|
250
|
+
const assetOutputs = {};
|
|
251
|
+
const outputBoundaryNodes = assetGraph.nodes.filter(n => n.type === 'graphOutput');
|
|
252
|
+
for (const outputBoundaryNode of outputBoundaryNodes) {
|
|
253
|
+
const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
|
|
254
|
+
const portName = portNameProp?.value || outputBoundaryNode.name;
|
|
255
|
+
const result = await evaluate({ name: `${nodeName}_asset`, context: assetGraph.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, outputNode: outputBoundaryNode.name, outputPort: 'value', inputs: nodeInputs, props: nodeProps });
|
|
256
|
+
assetOutputs[portName] = result;
|
|
257
|
+
}
|
|
258
|
+
cache.set(nodeName, assetOutputs);
|
|
259
|
+
return assetOutputs;
|
|
260
|
+
}
|
|
261
|
+
// Leaf node: call the implementation function
|
|
262
|
+
if (!definition.impl) {
|
|
263
|
+
throw new Error(`No implementation found for node: ${nodeContext}:${node.type}`);
|
|
264
|
+
}
|
|
186
265
|
const outputs = await definition.impl(nodeInputs, nodeProps);
|
|
187
|
-
// Cache and return
|
|
188
266
|
cache.set(nodeName, outputs);
|
|
189
267
|
return outputs;
|
|
190
268
|
}
|