@fbp/evaluator 1.9.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/cycles.d.ts +1 -1
- package/definitions/math.d.ts +1 -0
- package/definitions/math.js +21 -2
- package/definitions/ui.js +6 -6
- package/esm/definitions/math.js +20 -1
- package/esm/definitions/ui.js +6 -6
- package/esm/evaluate.js +127 -72
- package/esm/index.js +1 -1
- package/esm/validate.js +51 -56
- package/evaluate.d.ts +22 -2
- package/evaluate.js +127 -71
- package/index.d.ts +2 -2
- package/index.js +2 -1
- package/package.json +9 -12
- package/types.d.ts +2 -10
- package/validate.d.ts +16 -12
- package/validate.js +49 -54
package/esm/validate.js
CHANGED
|
@@ -1,28 +1,6 @@
|
|
|
1
|
-
import { derivePorts, derivePropDefinitions } 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';
|
|
4
|
-
/**
|
|
5
|
-
* Boundary node types. These are satisfied by the runtime (graphInput/graphProp
|
|
6
|
-
* receive values from the execution payload; graphOutput is a pass-through), so
|
|
7
|
-
* they are never checked for required inputs/props.
|
|
8
|
-
*/
|
|
9
|
-
const BOUNDARY_TYPES = new Set(['graphInput', 'graphOutput', 'graphProp']);
|
|
10
|
-
function defKey(context, category, name) {
|
|
11
|
-
return `${context}:${category}:${name}`;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Parse a node type (e.g., 'math:add' or 'const/string') into [category, name].
|
|
15
|
-
* Mirrors the evaluator's resolution so pre-flight and evaluation stay aligned.
|
|
16
|
-
*/
|
|
17
|
-
function parseTaskIdentifier(type) {
|
|
18
|
-
const colonIdx = type.indexOf(':');
|
|
19
|
-
if (colonIdx !== -1)
|
|
20
|
-
return [type.slice(0, colonIdx), type.slice(colonIdx + 1)];
|
|
21
|
-
const slashIdx = type.indexOf('/');
|
|
22
|
-
if (slashIdx !== -1)
|
|
23
|
-
return [type.slice(0, slashIdx), type.slice(slashIdx + 1)];
|
|
24
|
-
return ['core', type];
|
|
25
|
-
}
|
|
3
|
+
import { refEdges, wiringOf } from './evaluate';
|
|
26
4
|
/** Build "dst node -> dst port -> has at least one incoming edge". */
|
|
27
5
|
function connectedPorts(edges) {
|
|
28
6
|
const map = new Map();
|
|
@@ -37,8 +15,8 @@ function connectedPorts(edges) {
|
|
|
37
15
|
return map;
|
|
38
16
|
}
|
|
39
17
|
function nodeHasProp(node, propName) {
|
|
40
|
-
const
|
|
41
|
-
return
|
|
18
|
+
const value = plainValue(node.props?.find(pr => pr.name === propName)?.value);
|
|
19
|
+
return value !== undefined && value !== null;
|
|
42
20
|
}
|
|
43
21
|
/**
|
|
44
22
|
* Statically validate a flow graph before publish/execution.
|
|
@@ -46,12 +24,17 @@ function nodeHasProp(node, propName) {
|
|
|
46
24
|
* Heuristic (intentionally not a proof — see the caveats below) that mirrors the
|
|
47
25
|
* SQL `validate_function_graph` definition-aware pass:
|
|
48
26
|
*
|
|
49
|
-
* -
|
|
27
|
+
* - Every node type must be `category:name` (`INVALID_TYPE`) and name a
|
|
28
|
+
* structural type or a definition known in the node's effective context
|
|
29
|
+
* (`UNKNOWN_TYPE`); a definition identity may be used once
|
|
30
|
+
* (`DUPLICATE_DEFINITION`, `INVALID_DEFINITION`). The evaluator refuses to
|
|
31
|
+
* run a graph that fails these.
|
|
32
|
+
* - For every non-boundary node, each **declared**
|
|
50
33
|
* input port that is required (`optional !== true`) and non-`multi` must have
|
|
51
34
|
* at least one incoming edge, else `MISSING_REQUIRED_INPUT`.
|
|
52
35
|
* - Each definition prop that is `required` and has no `default` must be
|
|
53
36
|
* supplied by a node instance prop value, else `MISSING_REQUIRED_PROP`.
|
|
54
|
-
* - Inline `subnet` nodes are checked against their declared interface inputs
|
|
37
|
+
* - Inline `core:subnet` nodes are checked against their declared interface inputs
|
|
55
38
|
* and recursed into.
|
|
56
39
|
* - Digital-asset definitions (definitions carrying an internal `graph`) are
|
|
57
40
|
* recursed into.
|
|
@@ -61,38 +44,49 @@ function nodeHasProp(node, propName) {
|
|
|
61
44
|
* is also a `CYCLE_DETECTED`.
|
|
62
45
|
*
|
|
63
46
|
* Deliberately NOT flagged (kept consistent with the evaluator + SQL):
|
|
64
|
-
* - `graphInput` / `graphProp` boundaries — supplied at runtime.
|
|
47
|
+
* - `core:graphInput` / `core:graphProp` boundaries — supplied at runtime.
|
|
65
48
|
* - `multi` inputs — zero-or-more, never required.
|
|
66
49
|
* - Dynamic/undeclared input ports — the evaluator accepts them, so requiredness
|
|
67
50
|
* is only enforced for declared ports.
|
|
68
|
-
* - Nodes with no resolvable definition — treated as opaque (may be a runtime
|
|
69
|
-
* handler), exactly as the SQL validator and evaluator do.
|
|
70
51
|
*
|
|
71
52
|
* @returns the list of validation errors; an empty array means the graph is
|
|
72
53
|
* statically valid. Never throws.
|
|
73
54
|
*/
|
|
74
55
|
export function validateGraph(graph, options = {}) {
|
|
75
56
|
const errors = [];
|
|
57
|
+
// Types first: every node type is `category:name` and names a known definition, with one definition per identity.
|
|
58
|
+
for (const issue of checkTypes(graph, { definitions: options.definitions })) {
|
|
59
|
+
errors.push({ ...issue, node: issue.node?.replace(/^\//, '') });
|
|
60
|
+
}
|
|
76
61
|
const defMap = new Map();
|
|
77
62
|
const registerDefs = (defs) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
defMap.set(defKey(def.context, category, def.name), def);
|
|
63
|
+
for (const def of defs ?? []) {
|
|
64
|
+
// A stored definition with no graph only describes a node; one that can run keeps its place.
|
|
65
|
+
if (def.graph || !defMap.has(definitionKey(def)))
|
|
66
|
+
defMap.set(definitionKey(def), def);
|
|
83
67
|
}
|
|
84
68
|
};
|
|
85
|
-
|
|
69
|
+
const tableOf = (definitions) => {
|
|
70
|
+
try {
|
|
71
|
+
return hoistDefinitions({ nodes: [], edges: [], definitions: definitions ?? [] }).definitions ?? [];
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return definitions ?? [];
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
registerDefs(tableOf(options.definitions));
|
|
78
|
+
registerDefs(tableOf(graph.definitions));
|
|
86
79
|
const qualify = (pathPrefix, name) => (pathPrefix ? `${pathPrefix}/${name}` : name);
|
|
87
80
|
// Digital assets currently being visited, outermost first, so an asset that
|
|
88
81
|
// contains itself is reported instead of recursed into forever.
|
|
89
82
|
const assetStack = [];
|
|
90
83
|
const visit = (g, pathPrefix, scope = []) => {
|
|
91
|
-
// Definitions can be embedded per-graph (digital assets carry their own).
|
|
92
|
-
registerDefs(g.definitions);
|
|
93
84
|
const graphContext = g.context || '';
|
|
94
85
|
const connected = connectedPorts(g.edges ?? []);
|
|
95
|
-
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
|
+
]);
|
|
96
90
|
if (cycle) {
|
|
97
91
|
const path = cycle.map(name => qualify(pathPrefix, name));
|
|
98
92
|
errors.push({
|
|
@@ -103,24 +97,25 @@ export function validateGraph(graph, options = {}) {
|
|
|
103
97
|
});
|
|
104
98
|
}
|
|
105
99
|
for (const node of g.nodes ?? []) {
|
|
106
|
-
if (
|
|
100
|
+
if (isBoundaryType(node.type))
|
|
107
101
|
continue;
|
|
108
102
|
const qualifiedName = qualify(pathPrefix, node.name);
|
|
109
103
|
const nodeConnected = connected.get(node.name) ?? new Set();
|
|
110
104
|
// Inline subnet: validate against its declared interface, then recurse.
|
|
111
|
-
if (node.type ===
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
.
|
|
115
|
-
|
|
105
|
+
if (node.type === SUBNET) {
|
|
106
|
+
const children = node.nodes ?? [];
|
|
107
|
+
const defaulted = new Set(children
|
|
108
|
+
.filter(n => n.type === GRAPH_INPUT && nodeHasProp(n, 'default'))
|
|
109
|
+
.map(boundaryName));
|
|
110
|
+
for (const port of derivePorts(node).inputs) {
|
|
116
111
|
if (port.optional === true || port.multi === true || defaulted.has(port.name))
|
|
117
112
|
continue;
|
|
118
|
-
if (!nodeConnected.has(port.name)) {
|
|
113
|
+
if (!nodeConnected.has(port.name) && plainValue(unwiredInput(node, port.name)) == null) {
|
|
119
114
|
errors.push({
|
|
120
115
|
code: 'MISSING_REQUIRED_INPUT',
|
|
121
116
|
node: qualifiedName,
|
|
122
117
|
port: port.name,
|
|
123
|
-
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`,
|
|
124
119
|
});
|
|
125
120
|
}
|
|
126
121
|
}
|
|
@@ -136,26 +131,26 @@ export function validateGraph(graph, options = {}) {
|
|
|
136
131
|
});
|
|
137
132
|
}
|
|
138
133
|
}
|
|
139
|
-
visit({ name: `${node.name}_subnet`, context: node.context || graphContext || undefined, nodes:
|
|
134
|
+
visit({ name: `${node.name}_subnet`, context: node.context || graphContext || undefined, nodes: children, edges: node.edges ?? [] }, qualifiedName, [...scope, node.name]);
|
|
140
135
|
continue;
|
|
141
136
|
}
|
|
142
137
|
const nodeContext = node.context || graphContext;
|
|
143
|
-
|
|
144
|
-
|
|
138
|
+
if (!isNodeType(node.type))
|
|
139
|
+
continue;
|
|
140
|
+
const key = typeKey(nodeContext, node.type);
|
|
145
141
|
const definition = defMap.get(key);
|
|
146
|
-
//
|
|
147
|
-
// matches the SQL validator + evaluator, which do not require a definition.
|
|
142
|
+
// Already reported as UNKNOWN_TYPE.
|
|
148
143
|
if (!definition)
|
|
149
144
|
continue;
|
|
150
145
|
for (const port of definition.inputs ?? []) {
|
|
151
146
|
if (port.optional === true || port.multi === true)
|
|
152
147
|
continue;
|
|
153
|
-
if (!nodeConnected.has(port.name)) {
|
|
148
|
+
if (!nodeConnected.has(port.name) && plainValue(unwiredInput(node, port.name, definition.props)) == null) {
|
|
154
149
|
errors.push({
|
|
155
150
|
code: 'MISSING_REQUIRED_INPUT',
|
|
156
151
|
node: qualifiedName,
|
|
157
152
|
port: port.name,
|
|
158
|
-
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`,
|
|
159
154
|
});
|
|
160
155
|
}
|
|
161
156
|
}
|
|
@@ -187,7 +182,7 @@ export function validateGraph(graph, options = {}) {
|
|
|
187
182
|
continue;
|
|
188
183
|
}
|
|
189
184
|
assetStack.push({ key, node: qualifiedName });
|
|
190
|
-
visit(definition.graph, qualifiedName);
|
|
185
|
+
visit({ ...definition.graph, context: definition.graph.context || definition.context }, qualifiedName);
|
|
191
186
|
assetStack.pop();
|
|
192
187
|
}
|
|
193
188
|
}
|
package/evaluate.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Edge, Graph, Node } from '@fbp/
|
|
1
|
+
import type { Edge, Graph, Node } from '@fbp/spec';
|
|
2
2
|
import type { EvaluateOptions } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* Evaluate a graph starting from the specified output node/port.
|
|
@@ -8,10 +8,30 @@ import type { EvaluateOptions } from './types';
|
|
|
8
8
|
* @param graph - The graph to evaluate
|
|
9
9
|
* @param options - Evaluation options including definitions, output node/port, and external inputs
|
|
10
10
|
* @returns Promise resolving to the value at the specified output port
|
|
11
|
+
* @throws before anything runs if a node type is not `category:name`, names no
|
|
12
|
+
* known definition, or two definitions share an identity
|
|
11
13
|
*/
|
|
12
14
|
export declare function evaluate(graph: Graph, options: EvaluateOptions): Promise<any>;
|
|
13
15
|
/**
|
|
14
16
|
* A `{ ref }` to a sibling node's value is a dependency, like an edge.
|
|
15
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.
|
|
16
22
|
*/
|
|
17
|
-
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,28 +2,9 @@
|
|
|
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
|
-
* Build a definition map key from context, category, and name.
|
|
9
|
-
*/
|
|
10
|
-
function defKey(context, category, name) {
|
|
11
|
-
return `${context}:${category}:${name}`;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Parse a node type (e.g., 'math:add' or 'const/string') into [category, name].
|
|
15
|
-
* Colon is the canonical separator (category:name); slash is checked as fallback
|
|
16
|
-
* for legacy graph files. Boundary nodes (no separator) default to ['core', type].
|
|
17
|
-
*/
|
|
18
|
-
function parseTaskIdentifier(type) {
|
|
19
|
-
const colonIdx = type.indexOf(':');
|
|
20
|
-
if (colonIdx !== -1)
|
|
21
|
-
return [type.slice(0, colonIdx), type.slice(colonIdx + 1)];
|
|
22
|
-
const slashIdx = type.indexOf('/');
|
|
23
|
-
if (slashIdx !== -1)
|
|
24
|
-
return [type.slice(0, slashIdx), type.slice(slashIdx + 1)];
|
|
25
|
-
return ['core', type];
|
|
26
|
-
}
|
|
27
8
|
/**
|
|
28
9
|
* Evaluate a graph starting from the specified output node/port.
|
|
29
10
|
* Uses lazy evaluation - only evaluates nodes that are needed for the output.
|
|
@@ -32,40 +13,102 @@ function parseTaskIdentifier(type) {
|
|
|
32
13
|
* @param graph - The graph to evaluate
|
|
33
14
|
* @param options - Evaluation options including definitions, output node/port, and external inputs
|
|
34
15
|
* @returns Promise resolving to the value at the specified output port
|
|
16
|
+
* @throws before anything runs if a node type is not `category:name`, names no
|
|
17
|
+
* known definition, or two definitions share an identity
|
|
35
18
|
*/
|
|
36
19
|
async function evaluate(graph, options) {
|
|
37
|
-
|
|
20
|
+
// One definition table: the graph's own (nested ones hoisted into it) beside the host's.
|
|
21
|
+
const root = (0, spec_1.hoistDefinitions)(graph);
|
|
22
|
+
const host = (0, spec_1.hoistDefinitions)({ nodes: [], edges: [], definitions: options.definitions }).definitions;
|
|
23
|
+
(0, spec_1.assertTypes)(root, { definitions: host });
|
|
24
|
+
// A stored definition with no graph only describes a node, so it never replaces one that can run.
|
|
25
|
+
const supplied = new Set(host.map(spec_1.definitionKey));
|
|
26
|
+
const stored = (root.definitions ?? []).filter(d => !supplied.has((0, spec_1.definitionKey)(d)));
|
|
27
|
+
const definitions = stored.length ? [...host, ...stored] : host;
|
|
28
|
+
const outputs = await openGraph(root, { ...options, definitions }, [])(options.outputNode);
|
|
38
29
|
return outputs[options.outputPort];
|
|
39
30
|
}
|
|
40
31
|
/**
|
|
41
32
|
* A `{ ref }` to a sibling node's value is a dependency, like an edge.
|
|
42
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.
|
|
43
38
|
*/
|
|
44
|
-
function refEdges(nodes, scopePath = []) {
|
|
39
|
+
function refEdges(nodes, scopePath = [], wiring) {
|
|
45
40
|
const names = new Set(nodes.map(n => n.name));
|
|
46
41
|
const scope = (0, spec_1.joinPath)(scopePath);
|
|
47
42
|
const edges = [];
|
|
48
|
-
|
|
49
|
-
|
|
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 ?? []) {
|
|
50
80
|
if (!(0, spec_1.isRef)(prop.value))
|
|
51
81
|
continue;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
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))
|
|
57
86
|
continue;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const sibling = segments[segments.length - 1];
|
|
61
|
-
if (target.prop === undefined && sibling !== undefined && names.has(sibling) &&
|
|
62
|
-
(0, spec_1.joinPath)(segments.slice(0, -1)) === scope) {
|
|
63
|
-
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 } });
|
|
64
89
|
}
|
|
65
90
|
}
|
|
66
|
-
|
|
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 ?? '');
|
|
67
99
|
return edges;
|
|
68
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
|
+
}
|
|
69
112
|
/**
|
|
70
113
|
* @param assetPath - definition keys of the digital assets being evaluated,
|
|
71
114
|
* outermost first, so an asset that contains itself throws instead of
|
|
@@ -73,14 +116,14 @@ function refEdges(nodes, scopePath = []) {
|
|
|
73
116
|
*/
|
|
74
117
|
function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
75
118
|
const { inputs = {}, props = {}, absent } = options;
|
|
76
|
-
|
|
77
|
-
// A stored definition with no graph only describes a node, so it never replaces one that can run.
|
|
78
|
-
const supplied = new Set(options.definitions.map(d => defKey(d.context, d.category ?? 'core', d.name)));
|
|
79
|
-
const stored = (graph.definitions ?? []).filter(d => d.graph || !supplied.has(defKey(d.context, d.category ?? 'core', d.name)));
|
|
80
|
-
const definitions = stored.length ? [...options.definitions, ...stored] : options.definitions;
|
|
119
|
+
const { definitions } = options;
|
|
81
120
|
// Reject cycles up front, as the validator and the SQL gate do: pulling
|
|
82
121
|
// through a loop would otherwise recurse until the stack overflows.
|
|
83
|
-
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))]);
|
|
84
127
|
if (cycle) {
|
|
85
128
|
throw new Error(`Graph "${graph.name}" contains a cycle: ${(0, cycles_1.formatCycle)(cycle)}`);
|
|
86
129
|
}
|
|
@@ -89,10 +132,6 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
89
132
|
for (const node of graph.nodes) {
|
|
90
133
|
nodeMap.set(node.name, node);
|
|
91
134
|
}
|
|
92
|
-
const defMap = new Map();
|
|
93
|
-
for (const def of definitions) {
|
|
94
|
-
defMap.set(defKey(def.context, def.category ?? 'core', def.name), def);
|
|
95
|
-
}
|
|
96
135
|
// Graph-level context used as default for node lookups
|
|
97
136
|
const graphContext = graph.context || '';
|
|
98
137
|
// Build edge lookup: destination node -> destination port -> edges (in array order)
|
|
@@ -114,9 +153,12 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
114
153
|
/**
|
|
115
154
|
* Resolve a prop value: a `{ ref }` takes the value of the node it points at
|
|
116
155
|
* (its `value` output, or the named prop for a '.prop' reference). The target
|
|
117
|
-
* 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.
|
|
118
158
|
*/
|
|
119
159
|
async function resolve(value, holder, chain = []) {
|
|
160
|
+
if ((0, spec_1.isLiteral)(value))
|
|
161
|
+
return value.literal;
|
|
120
162
|
if (!(0, spec_1.isRef)(value))
|
|
121
163
|
return value;
|
|
122
164
|
const target = (0, spec_1.resolveRef)((0, spec_1.joinPath)(scopePath), value.ref);
|
|
@@ -162,10 +204,8 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
162
204
|
}
|
|
163
205
|
// Handle system nodes (graphInput, graphOutput, graphProp)
|
|
164
206
|
// System nodes are identified by their type name — reserved, no definition lookup needed
|
|
165
|
-
if (node.type ===
|
|
166
|
-
|
|
167
|
-
const portNameProp = node.props?.find(p => p.name === 'portName');
|
|
168
|
-
const inputName = portNameProp?.value || nodeName;
|
|
207
|
+
if (node.type === spec_1.GRAPH_INPUT) {
|
|
208
|
+
const inputName = (0, spec_1.boundaryName)(node);
|
|
169
209
|
// A wired input whose arm was not taken stays dark, default or not, as it would in the flat graph.
|
|
170
210
|
if (absent?.has(inputName)) {
|
|
171
211
|
cache.set(nodeName, {});
|
|
@@ -175,25 +215,23 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
175
215
|
let value = inputs[inputName];
|
|
176
216
|
if (value === undefined) {
|
|
177
217
|
const defaultProp = node.props?.find(p => p.name === 'default');
|
|
178
|
-
value = defaultProp?.value;
|
|
218
|
+
value = (0, spec_1.plainValue)(defaultProp?.value);
|
|
179
219
|
}
|
|
180
220
|
const result = { value };
|
|
181
221
|
cache.set(nodeName, result);
|
|
182
222
|
return result;
|
|
183
223
|
}
|
|
184
|
-
if (node.type ===
|
|
185
|
-
|
|
186
|
-
const propNameProp = node.props?.find(p => p.name === 'propName');
|
|
187
|
-
const propName = propNameProp?.value || nodeName;
|
|
224
|
+
if (node.type === spec_1.GRAPH_PROP) {
|
|
225
|
+
const propName = (0, spec_1.boundaryName)(node);
|
|
188
226
|
let value = props[propName];
|
|
189
227
|
if (value === undefined) {
|
|
190
|
-
value = node.props?.find(p => p.name === 'default')?.value;
|
|
228
|
+
value = (0, spec_1.plainValue)(node.props?.find(p => p.name === 'default')?.value);
|
|
191
229
|
}
|
|
192
230
|
const result = { value };
|
|
193
231
|
cache.set(nodeName, result);
|
|
194
232
|
return result;
|
|
195
233
|
}
|
|
196
|
-
if (node.type ===
|
|
234
|
+
if (node.type === spec_1.GRAPH_OUTPUT) {
|
|
197
235
|
// graphOutput is a pass-through: evaluate its upstream 'value' input
|
|
198
236
|
const portEdges = edgesByDst.get(nodeName);
|
|
199
237
|
const edges = portEdges?.get('value') ?? [];
|
|
@@ -211,12 +249,11 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
211
249
|
cache.set(nodeName, result);
|
|
212
250
|
return result;
|
|
213
251
|
}
|
|
214
|
-
// Handle subnet nodes (
|
|
215
|
-
if (node.type ===
|
|
252
|
+
// Handle subnet nodes (inline nodes/edges)
|
|
253
|
+
if (node.type === spec_1.SUBNET) {
|
|
216
254
|
// The interface is derived from the boundary nodes inside the subnet.
|
|
217
|
-
const
|
|
218
|
-
const inputPorts =
|
|
219
|
-
const outputPorts = node.outputs ?? derived.outputs;
|
|
255
|
+
const children = node.nodes ?? [];
|
|
256
|
+
const { inputs: inputPorts, outputs: outputPorts } = (0, spec_1.derivePorts)(node);
|
|
220
257
|
// Collect inputs for the subnet by evaluating upstream nodes
|
|
221
258
|
const subnetInputs = {};
|
|
222
259
|
const absentInputs = new Set();
|
|
@@ -236,6 +273,14 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
236
273
|
}
|
|
237
274
|
}
|
|
238
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
|
+
}
|
|
239
284
|
// Promoted parameters: the value set on the subnet node, else the graphProp's default.
|
|
240
285
|
const subnetProps = {};
|
|
241
286
|
for (const propDef of (0, spec_1.derivePropDefinitions)(node)) {
|
|
@@ -245,10 +290,10 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
245
290
|
}
|
|
246
291
|
}
|
|
247
292
|
// One evaluation of the inner graph serves every output port.
|
|
248
|
-
const pull = openGraph({ name: `${nodeName}_subnet`, context: node.context || graphContext || undefined, nodes:
|
|
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);
|
|
249
294
|
const subnetOutputs = {};
|
|
250
295
|
for (const outputPort of outputPorts) {
|
|
251
|
-
const outputBoundaryNode =
|
|
296
|
+
const outputBoundaryNode = children.find(n => n.type === spec_1.GRAPH_OUTPUT && (0, spec_1.boundaryName)(n) === outputPort.name);
|
|
252
297
|
if (!outputBoundaryNode) {
|
|
253
298
|
throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
|
|
254
299
|
}
|
|
@@ -261,8 +306,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
261
306
|
}
|
|
262
307
|
// Get the definition for this node — resolve context from node override or graph default
|
|
263
308
|
const nodeContext = node.context || graphContext;
|
|
264
|
-
const
|
|
265
|
-
const key = defKey(nodeContext, nodeCategory, nodeDefName);
|
|
309
|
+
const key = (0, spec_1.typeKey)(nodeContext, node.type);
|
|
266
310
|
const definition = defMap.get(key);
|
|
267
311
|
if (!definition) {
|
|
268
312
|
throw new Error(`No definition found for node: ${nodeContext}:${node.type}`);
|
|
@@ -345,6 +389,19 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
345
389
|
}
|
|
346
390
|
}
|
|
347
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
|
+
}
|
|
348
405
|
// Get props from node instance (override definition defaults)
|
|
349
406
|
const nodeProps = {};
|
|
350
407
|
if (definition.props) {
|
|
@@ -365,13 +422,12 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
|
|
|
365
422
|
}
|
|
366
423
|
const assetGraph = definition.graph;
|
|
367
424
|
// One evaluation of the asset's graph serves every output port.
|
|
368
|
-
const pull = openGraph({ name: `${nodeName}_asset`, context: assetGraph.context, nodes: assetGraph.nodes, edges: assetGraph.edges
|
|
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]);
|
|
369
426
|
const assetOutputs = {};
|
|
370
|
-
for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type ===
|
|
371
|
-
const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
|
|
427
|
+
for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type === spec_1.GRAPH_OUTPUT)) {
|
|
372
428
|
const result = await pull(outputBoundaryNode.name);
|
|
373
429
|
if ('value' in result)
|
|
374
|
-
assetOutputs[
|
|
430
|
+
assetOutputs[(0, spec_1.boundaryName)(outputBoundaryNode)] = result.value;
|
|
375
431
|
}
|
|
376
432
|
cache.set(nodeName, assetOutputs);
|
|
377
433
|
return assetOutputs;
|
package/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { findCycle, formatCycle } from './cycles';
|
|
2
2
|
export { evaluate } from './evaluate';
|
|
3
|
-
export type { EvaluateOptions, NodeDefinitionWithImpl, NodeImplFn
|
|
3
|
+
export type { EvaluateOptions, NodeDefinitionWithImpl, NodeImplFn } from './types';
|
|
4
4
|
export type { GraphValidationError, GraphValidationErrorCode, ValidateGraphOptions } from './validate';
|
|
5
5
|
export { validateGraph } from './validate';
|
|
6
6
|
export { constBooleanDef, constStringDef, coreDefinitions, flowBranchDef, flowCoerceDef, flowFilterDef, flowForEachDef, flowGuardDef, flowMergeDef, flowSwitchDef, jsonMergeDef, jsonObjectDef, jsonSelectDef, jsonSplitDef, stringConcatDef, stringTemplateDef } from './definitions/core';
|
|
7
|
-
export { addDef, constNumberDef, mathDefinitions, multiplyDef, subtractDef } from './definitions/math';
|
|
7
|
+
export { addDef, constNumberDef, divideDef, mathDefinitions, multiplyDef, subtractDef } from './definitions/math';
|
|
8
8
|
export { graphqlRequestDef, netDefinitions } from './definitions/net';
|
|
9
9
|
export { buttonDef, formDef, graphInputDef, graphOutputDef, graphPropDef, inputDef, pageDef, textDef, uiDefinitions } from './definitions/ui';
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.uiDefinitions = exports.textDef = exports.pageDef = exports.inputDef = exports.graphPropDef = exports.graphOutputDef = exports.graphInputDef = exports.formDef = exports.buttonDef = exports.netDefinitions = exports.graphqlRequestDef = exports.subtractDef = exports.multiplyDef = exports.mathDefinitions = exports.constNumberDef = exports.addDef = exports.stringTemplateDef = exports.stringConcatDef = exports.jsonSplitDef = exports.jsonSelectDef = exports.jsonObjectDef = exports.jsonMergeDef = exports.flowSwitchDef = exports.flowMergeDef = exports.flowGuardDef = exports.flowForEachDef = exports.flowFilterDef = exports.flowCoerceDef = exports.flowBranchDef = exports.coreDefinitions = exports.constStringDef = exports.constBooleanDef = exports.validateGraph = exports.evaluate = exports.formatCycle = exports.findCycle = void 0;
|
|
3
|
+
exports.uiDefinitions = exports.textDef = exports.pageDef = exports.inputDef = exports.graphPropDef = exports.graphOutputDef = exports.graphInputDef = exports.formDef = exports.buttonDef = exports.netDefinitions = exports.graphqlRequestDef = exports.subtractDef = exports.multiplyDef = exports.mathDefinitions = exports.divideDef = exports.constNumberDef = exports.addDef = exports.stringTemplateDef = exports.stringConcatDef = exports.jsonSplitDef = exports.jsonSelectDef = exports.jsonObjectDef = exports.jsonMergeDef = exports.flowSwitchDef = exports.flowMergeDef = exports.flowGuardDef = exports.flowForEachDef = exports.flowFilterDef = exports.flowCoerceDef = exports.flowBranchDef = exports.coreDefinitions = exports.constStringDef = exports.constBooleanDef = exports.validateGraph = exports.evaluate = exports.formatCycle = exports.findCycle = void 0;
|
|
4
4
|
var cycles_1 = require("./cycles");
|
|
5
5
|
Object.defineProperty(exports, "findCycle", { enumerable: true, get: function () { return cycles_1.findCycle; } });
|
|
6
6
|
Object.defineProperty(exports, "formatCycle", { enumerable: true, get: function () { return cycles_1.formatCycle; } });
|
|
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "stringTemplateDef", { enumerable: true, get: fun
|
|
|
29
29
|
var math_1 = require("./definitions/math");
|
|
30
30
|
Object.defineProperty(exports, "addDef", { enumerable: true, get: function () { return math_1.addDef; } });
|
|
31
31
|
Object.defineProperty(exports, "constNumberDef", { enumerable: true, get: function () { return math_1.constNumberDef; } });
|
|
32
|
+
Object.defineProperty(exports, "divideDef", { enumerable: true, get: function () { return math_1.divideDef; } });
|
|
32
33
|
Object.defineProperty(exports, "mathDefinitions", { enumerable: true, get: function () { return math_1.mathDefinitions; } });
|
|
33
34
|
Object.defineProperty(exports, "multiplyDef", { enumerable: true, get: function () { return math_1.multiplyDef; } });
|
|
34
35
|
Object.defineProperty(exports, "subtractDef", { enumerable: true, get: function () { return math_1.subtractDef; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fbp/evaluator",
|
|
3
|
-
"version": "1.
|
|
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",
|
|
@@ -11,16 +11,8 @@
|
|
|
11
11
|
"access": "public",
|
|
12
12
|
"directory": "dist"
|
|
13
13
|
},
|
|
14
|
-
"scripts": {
|
|
15
|
-
"prepack": "npm run build",
|
|
16
|
-
"build": "makage build",
|
|
17
|
-
"build:dev": "makage build --dev",
|
|
18
|
-
"clean": "makage clean",
|
|
19
|
-
"test": "jest --config jest.config.js"
|
|
20
|
-
},
|
|
21
14
|
"dependencies": {
|
|
22
|
-
"@fbp/spec": "1.
|
|
23
|
-
"@fbp/types": "1.6.0"
|
|
15
|
+
"@fbp/spec": "2.1.0"
|
|
24
16
|
},
|
|
25
17
|
"devDependencies": {
|
|
26
18
|
"@types/jest": "^30.0.0",
|
|
@@ -37,5 +29,10 @@
|
|
|
37
29
|
"bugs": {
|
|
38
30
|
"url": "https://github.com/constructive-io/fbp/issues"
|
|
39
31
|
},
|
|
40
|
-
"
|
|
41
|
-
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "makage build",
|
|
34
|
+
"build:dev": "makage build --dev",
|
|
35
|
+
"clean": "makage clean",
|
|
36
|
+
"test": "jest --config jest.config.js"
|
|
37
|
+
}
|
|
38
|
+
}
|