@fbp/evaluator 0.2.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/LICENSE +21 -0
- package/README.md +85 -0
- package/definitions/core.d.ts +72 -0
- package/definitions/core.js +218 -0
- package/definitions/math.d.ts +8 -0
- package/definitions/math.js +53 -0
- package/definitions/net.d.ts +25 -0
- package/definitions/net.js +132 -0
- package/definitions/ui.d.ts +10 -0
- package/definitions/ui.js +194 -0
- package/esm/definitions/core.d.ts +72 -0
- package/esm/definitions/core.js +215 -0
- package/esm/definitions/math.d.ts +8 -0
- package/esm/definitions/math.js +50 -0
- package/esm/definitions/net.d.ts +25 -0
- package/esm/definitions/net.js +129 -0
- package/esm/definitions/ui.d.ts +10 -0
- package/esm/definitions/ui.js +191 -0
- package/esm/evaluate.d.ts +12 -0
- package/esm/evaluate.js +177 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.js +6 -0
- package/esm/types.d.ts +38 -0
- package/esm/types.js +1 -0
- package/evaluate.d.ts +12 -0
- package/evaluate.js +180 -0
- package/index.d.ts +6 -0
- package/index.js +31 -0
- package/package.json +44 -0
- package/types.d.ts +38 -0
- package/types.js +2 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NodeDefinitionWithImpl } from '../types';
|
|
2
|
+
export declare const graphInputDef: NodeDefinitionWithImpl;
|
|
3
|
+
export declare const graphOutputDef: NodeDefinitionWithImpl;
|
|
4
|
+
export declare const graphPropDef: NodeDefinitionWithImpl;
|
|
5
|
+
export declare const pageDef: NodeDefinitionWithImpl;
|
|
6
|
+
export declare const formDef: NodeDefinitionWithImpl;
|
|
7
|
+
export declare const inputDef: NodeDefinitionWithImpl;
|
|
8
|
+
export declare const buttonDef: NodeDefinitionWithImpl;
|
|
9
|
+
export declare const textDef: NodeDefinitionWithImpl;
|
|
10
|
+
export declare const uiDefinitions: NodeDefinitionWithImpl[];
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UI component node definitions with implementations for testing.
|
|
3
|
+
* These produce vdom JSON structures.
|
|
4
|
+
*/
|
|
5
|
+
// Type coercion helper for boundary nodes
|
|
6
|
+
function coerceValue(value, valueType) {
|
|
7
|
+
if (value === undefined || value === null)
|
|
8
|
+
return value;
|
|
9
|
+
switch (valueType) {
|
|
10
|
+
case 'number':
|
|
11
|
+
return typeof value === 'number' ? value : parseFloat(String(value)) || 0;
|
|
12
|
+
case 'string':
|
|
13
|
+
return String(value);
|
|
14
|
+
case 'boolean':
|
|
15
|
+
return value === true || value === 'true' || value === 1;
|
|
16
|
+
case 'Element':
|
|
17
|
+
case 'any':
|
|
18
|
+
default:
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// Boundary node definitions for graph inputs/outputs
|
|
23
|
+
export const graphInputDef = {
|
|
24
|
+
context: 'core',
|
|
25
|
+
category: 'graph',
|
|
26
|
+
type: 'core/graph/input',
|
|
27
|
+
icon: 'arrow-right',
|
|
28
|
+
inputs: [],
|
|
29
|
+
outputs: [{ name: 'value', type: 'any' }],
|
|
30
|
+
props: [
|
|
31
|
+
{ name: 'valueType', type: 'enum', default: 'any', options: ['any', 'number', 'string', 'boolean', 'Element'] },
|
|
32
|
+
{ name: 'default', type: 'any' }
|
|
33
|
+
],
|
|
34
|
+
description: 'Graph input boundary node',
|
|
35
|
+
impl: (_inputs, props) => ({ value: coerceValue(props?.value ?? props?.default, props?.valueType ?? 'any') })
|
|
36
|
+
};
|
|
37
|
+
export const graphOutputDef = {
|
|
38
|
+
context: 'core',
|
|
39
|
+
category: 'graph',
|
|
40
|
+
type: 'core/graph/output',
|
|
41
|
+
icon: 'arrow-left',
|
|
42
|
+
inputs: [{ name: 'value', type: 'any' }],
|
|
43
|
+
outputs: [],
|
|
44
|
+
props: [
|
|
45
|
+
{ name: 'valueType', type: 'enum', default: 'any', options: ['any', 'number', 'string', 'boolean', 'Element'] }
|
|
46
|
+
],
|
|
47
|
+
description: 'Graph output boundary node',
|
|
48
|
+
impl: (inputs, props) => ({ value: coerceValue(inputs.value, props?.valueType ?? 'any') })
|
|
49
|
+
};
|
|
50
|
+
export const graphPropDef = {
|
|
51
|
+
context: 'core',
|
|
52
|
+
category: 'graph',
|
|
53
|
+
type: 'core/graph/prop',
|
|
54
|
+
icon: 'settings',
|
|
55
|
+
inputs: [],
|
|
56
|
+
outputs: [{ name: 'value', type: 'any' }],
|
|
57
|
+
props: [
|
|
58
|
+
{ name: 'valueType', type: 'enum', default: 'any', options: ['any', 'number', 'string', 'boolean', 'Element'] },
|
|
59
|
+
{ name: 'default', type: 'any' }
|
|
60
|
+
],
|
|
61
|
+
description: 'Graph property boundary node',
|
|
62
|
+
impl: (_inputs, props) => ({ value: coerceValue(props?.value ?? props?.default, props?.valueType ?? 'any') })
|
|
63
|
+
};
|
|
64
|
+
export const pageDef = {
|
|
65
|
+
context: 'ui',
|
|
66
|
+
category: 'layout',
|
|
67
|
+
type: 'ui/layout/Page',
|
|
68
|
+
icon: 'file',
|
|
69
|
+
inputs: [
|
|
70
|
+
{ name: 'children', type: 'Element[]', multi: true }
|
|
71
|
+
],
|
|
72
|
+
outputs: [{ name: 'element', type: 'Element' }],
|
|
73
|
+
props: [
|
|
74
|
+
{ name: 'className', type: 'string', default: '' },
|
|
75
|
+
{ name: 'key', type: 'string', required: true }
|
|
76
|
+
],
|
|
77
|
+
description: 'A page container component',
|
|
78
|
+
impl: (inputs, props) => ({
|
|
79
|
+
element: {
|
|
80
|
+
type: 'Page',
|
|
81
|
+
key: props.key,
|
|
82
|
+
props: { className: props.className },
|
|
83
|
+
children: inputs.children ?? []
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
};
|
|
87
|
+
export const formDef = {
|
|
88
|
+
context: 'ui',
|
|
89
|
+
category: 'form',
|
|
90
|
+
type: 'ui/form/Form',
|
|
91
|
+
icon: 'file-text',
|
|
92
|
+
inputs: [
|
|
93
|
+
{ name: 'children', type: 'Element[]', multi: true }
|
|
94
|
+
],
|
|
95
|
+
outputs: [{ name: 'element', type: 'Element' }],
|
|
96
|
+
props: [
|
|
97
|
+
{ name: 'className', type: 'string', default: '' },
|
|
98
|
+
{ name: 'key', type: 'string', required: true }
|
|
99
|
+
],
|
|
100
|
+
description: 'A form container component',
|
|
101
|
+
impl: (inputs, props) => ({
|
|
102
|
+
element: {
|
|
103
|
+
type: 'Form',
|
|
104
|
+
key: props.key,
|
|
105
|
+
props: { className: props.className },
|
|
106
|
+
children: inputs.children ?? []
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
};
|
|
110
|
+
export const inputDef = {
|
|
111
|
+
context: 'ui',
|
|
112
|
+
category: 'form',
|
|
113
|
+
type: 'ui/form/Input',
|
|
114
|
+
icon: 'text-cursor',
|
|
115
|
+
inputs: [],
|
|
116
|
+
outputs: [{ name: 'element', type: 'Element' }],
|
|
117
|
+
props: [
|
|
118
|
+
{ name: 'key', type: 'string', required: true },
|
|
119
|
+
{ name: 'name', type: 'string', required: true },
|
|
120
|
+
{ name: 'type', type: 'string', default: 'text' },
|
|
121
|
+
{ name: 'placeholder', type: 'string', default: '' }
|
|
122
|
+
],
|
|
123
|
+
description: 'An input field component',
|
|
124
|
+
impl: (_inputs, props) => ({
|
|
125
|
+
element: {
|
|
126
|
+
type: 'Input',
|
|
127
|
+
key: props.key,
|
|
128
|
+
props: {
|
|
129
|
+
name: props.name,
|
|
130
|
+
type: props.type ?? 'text',
|
|
131
|
+
placeholder: props.placeholder ?? ''
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
export const buttonDef = {
|
|
137
|
+
context: 'ui',
|
|
138
|
+
category: 'form',
|
|
139
|
+
type: 'ui/form/Button',
|
|
140
|
+
icon: 'square',
|
|
141
|
+
inputs: [],
|
|
142
|
+
outputs: [{ name: 'element', type: 'Element' }],
|
|
143
|
+
props: [
|
|
144
|
+
{ name: 'key', type: 'string', required: true },
|
|
145
|
+
{ name: 'type', type: 'string', default: 'button' },
|
|
146
|
+
{ name: 'text', type: 'string', required: true }
|
|
147
|
+
],
|
|
148
|
+
description: 'A button component',
|
|
149
|
+
impl: (_inputs, props) => ({
|
|
150
|
+
element: {
|
|
151
|
+
type: 'Button',
|
|
152
|
+
key: props.key,
|
|
153
|
+
props: {
|
|
154
|
+
type: props.type ?? 'button',
|
|
155
|
+
text: props.text
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
};
|
|
160
|
+
export const textDef = {
|
|
161
|
+
context: 'ui',
|
|
162
|
+
category: 'content',
|
|
163
|
+
type: 'ui/content/Text',
|
|
164
|
+
icon: 'type',
|
|
165
|
+
inputs: [],
|
|
166
|
+
outputs: [{ name: 'element', type: 'Element' }],
|
|
167
|
+
props: [
|
|
168
|
+
{ name: 'key', type: 'string', required: true },
|
|
169
|
+
{ name: 'content', type: 'string', required: true }
|
|
170
|
+
],
|
|
171
|
+
description: 'A text content component',
|
|
172
|
+
impl: (_inputs, props) => ({
|
|
173
|
+
element: {
|
|
174
|
+
type: 'Text',
|
|
175
|
+
key: props.key,
|
|
176
|
+
props: {
|
|
177
|
+
content: props.content
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
})
|
|
181
|
+
};
|
|
182
|
+
export const uiDefinitions = [
|
|
183
|
+
pageDef,
|
|
184
|
+
formDef,
|
|
185
|
+
inputDef,
|
|
186
|
+
buttonDef,
|
|
187
|
+
textDef,
|
|
188
|
+
graphInputDef,
|
|
189
|
+
graphOutputDef,
|
|
190
|
+
graphPropDef
|
|
191
|
+
];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Graph } from '@fbp/types';
|
|
2
|
+
import type { EvaluateOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Evaluate a graph starting from the specified output node/port.
|
|
5
|
+
* Uses lazy evaluation - only evaluates nodes that are needed for the output.
|
|
6
|
+
* Fully async to support async node implementations.
|
|
7
|
+
*
|
|
8
|
+
* @param graph - The graph to evaluate
|
|
9
|
+
* @param options - Evaluation options including definitions, output node/port, and external inputs
|
|
10
|
+
* @returns Promise resolving to the value at the specified output port
|
|
11
|
+
*/
|
|
12
|
+
export declare function evaluate(graph: Graph, options: EvaluateOptions): Promise<any>;
|
package/esm/evaluate.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evaluate a graph starting from the specified output node/port.
|
|
3
|
+
* Uses lazy evaluation - only evaluates nodes that are needed for the output.
|
|
4
|
+
* Fully async to support async node implementations.
|
|
5
|
+
*
|
|
6
|
+
* @param graph - The graph to evaluate
|
|
7
|
+
* @param options - Evaluation options including definitions, output node/port, and external inputs
|
|
8
|
+
* @returns Promise resolving to the value at the specified output port
|
|
9
|
+
*/
|
|
10
|
+
export async function evaluate(graph, options) {
|
|
11
|
+
const { definitions, outputNode, outputPort, inputs = {}, props = {} } = options;
|
|
12
|
+
// Build lookup maps
|
|
13
|
+
const nodeMap = new Map();
|
|
14
|
+
for (const node of graph.nodes) {
|
|
15
|
+
nodeMap.set(node.name, node);
|
|
16
|
+
}
|
|
17
|
+
const defMap = new Map();
|
|
18
|
+
for (const def of definitions) {
|
|
19
|
+
defMap.set(def.type, def);
|
|
20
|
+
}
|
|
21
|
+
// Build edge lookup: destination node -> destination port -> edges (in array order)
|
|
22
|
+
const edgesByDst = new Map();
|
|
23
|
+
for (const edge of graph.edges) {
|
|
24
|
+
const dstNode = edge.dst.node;
|
|
25
|
+
const dstPort = edge.dst.port;
|
|
26
|
+
if (!edgesByDst.has(dstNode)) {
|
|
27
|
+
edgesByDst.set(dstNode, new Map());
|
|
28
|
+
}
|
|
29
|
+
const portMap = edgesByDst.get(dstNode);
|
|
30
|
+
if (!portMap.has(dstPort)) {
|
|
31
|
+
portMap.set(dstPort, []);
|
|
32
|
+
}
|
|
33
|
+
portMap.get(dstPort).push(edge);
|
|
34
|
+
}
|
|
35
|
+
// Cache for evaluated node outputs to avoid re-evaluation
|
|
36
|
+
const cache = new Map();
|
|
37
|
+
/**
|
|
38
|
+
* Recursively evaluate a node and return all its outputs.
|
|
39
|
+
* Fully async to support async node implementations.
|
|
40
|
+
*/
|
|
41
|
+
async function evaluateNode(nodeName) {
|
|
42
|
+
// Check cache first
|
|
43
|
+
if (cache.has(nodeName)) {
|
|
44
|
+
return cache.get(nodeName);
|
|
45
|
+
}
|
|
46
|
+
const node = nodeMap.get(nodeName);
|
|
47
|
+
if (!node) {
|
|
48
|
+
throw new Error(`Node not found: ${nodeName}`);
|
|
49
|
+
}
|
|
50
|
+
// Handle special boundary nodes (graphInput, graphOutput, graphProp)
|
|
51
|
+
// Boundary nodes are identified by their type property, not by name prefix
|
|
52
|
+
if (node.type === 'graphInput') {
|
|
53
|
+
// Get the port name from the portName property
|
|
54
|
+
const portNameProp = node.props?.find(p => p.name === 'portName');
|
|
55
|
+
const inputName = portNameProp?.value || nodeName;
|
|
56
|
+
// Check external inputs first, then fall back to node's default prop
|
|
57
|
+
let value = inputs[inputName];
|
|
58
|
+
if (value === undefined) {
|
|
59
|
+
const defaultProp = node.props?.find(p => p.name === 'default');
|
|
60
|
+
value = defaultProp?.value;
|
|
61
|
+
}
|
|
62
|
+
const result = { value };
|
|
63
|
+
cache.set(nodeName, result);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
if (node.type === 'graphProp') {
|
|
67
|
+
// Get the prop name from the propName property
|
|
68
|
+
const propNameProp = node.props?.find(p => p.name === 'propName');
|
|
69
|
+
const propName = propNameProp?.value || nodeName;
|
|
70
|
+
const value = props[propName];
|
|
71
|
+
const result = { value };
|
|
72
|
+
cache.set(nodeName, result);
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
// Handle subnet nodes (kind: 'subnet')
|
|
76
|
+
if (node.kind === 'subnet' && node.nodes && node.edges) {
|
|
77
|
+
// Collect inputs for the subnet by evaluating upstream nodes
|
|
78
|
+
const subnetInputs = {};
|
|
79
|
+
const subnetPortEdges = edgesByDst.get(nodeName);
|
|
80
|
+
if (node.inputs && subnetPortEdges) {
|
|
81
|
+
for (const inputPort of node.inputs) {
|
|
82
|
+
const edges = subnetPortEdges.get(inputPort.name) ?? [];
|
|
83
|
+
if (edges.length > 0) {
|
|
84
|
+
const edge = edges[0];
|
|
85
|
+
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
86
|
+
subnetInputs[inputPort.name] = upstreamOutputs[edge.src.port];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Find the output boundary nodes in the subnet
|
|
91
|
+
const subnetOutputs = {};
|
|
92
|
+
if (node.outputs) {
|
|
93
|
+
for (const outputPort of node.outputs) {
|
|
94
|
+
// Find the graphOutput boundary node by matching portName property
|
|
95
|
+
const outputBoundaryNode = node.nodes.find(n => {
|
|
96
|
+
if (n.type !== 'graphOutput')
|
|
97
|
+
return false;
|
|
98
|
+
const portNameProp = n.props?.find(p => p.name === 'portName');
|
|
99
|
+
return portNameProp?.value === outputPort.name;
|
|
100
|
+
});
|
|
101
|
+
if (!outputBoundaryNode) {
|
|
102
|
+
throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
|
|
103
|
+
}
|
|
104
|
+
// Recursively evaluate the subnet's internal graph
|
|
105
|
+
const subnetResult = await evaluate({
|
|
106
|
+
name: `${nodeName}_subnet`,
|
|
107
|
+
nodes: node.nodes,
|
|
108
|
+
edges: node.edges
|
|
109
|
+
}, {
|
|
110
|
+
definitions,
|
|
111
|
+
outputNode: outputBoundaryNode.name,
|
|
112
|
+
outputPort: 'value',
|
|
113
|
+
inputs: subnetInputs
|
|
114
|
+
});
|
|
115
|
+
subnetOutputs[outputPort.name] = subnetResult;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
cache.set(nodeName, subnetOutputs);
|
|
119
|
+
return subnetOutputs;
|
|
120
|
+
}
|
|
121
|
+
// Get the definition for this node type
|
|
122
|
+
const definition = defMap.get(node.type);
|
|
123
|
+
if (!definition) {
|
|
124
|
+
throw new Error(`No definition found for node type: ${node.type}`);
|
|
125
|
+
}
|
|
126
|
+
if (!definition.impl) {
|
|
127
|
+
throw new Error(`No implementation found for node type: ${node.type}`);
|
|
128
|
+
}
|
|
129
|
+
// Collect inputs by evaluating upstream nodes
|
|
130
|
+
const nodeInputs = {};
|
|
131
|
+
const portEdges = edgesByDst.get(nodeName);
|
|
132
|
+
if (definition.inputs) {
|
|
133
|
+
for (const inputPort of definition.inputs) {
|
|
134
|
+
const edges = portEdges?.get(inputPort.name) ?? [];
|
|
135
|
+
if (inputPort.multi) {
|
|
136
|
+
// Multi-input port: collect all values in edge array order
|
|
137
|
+
const values = await Promise.all(edges.map(async (edge) => {
|
|
138
|
+
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
139
|
+
return upstreamOutputs[edge.src.port];
|
|
140
|
+
}));
|
|
141
|
+
nodeInputs[inputPort.name] = values;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
// Single input: take first edge if exists
|
|
145
|
+
if (edges.length > 0) {
|
|
146
|
+
const edge = edges[0];
|
|
147
|
+
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
148
|
+
nodeInputs[inputPort.name] = upstreamOutputs[edge.src.port];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Get props from node instance
|
|
154
|
+
const nodeProps = {};
|
|
155
|
+
if (definition.props) {
|
|
156
|
+
for (const propDef of definition.props) {
|
|
157
|
+
// First check node instance props
|
|
158
|
+
const instanceProp = node.props?.find(p => p.name === propDef.name);
|
|
159
|
+
if (instanceProp !== undefined && instanceProp.value !== undefined) {
|
|
160
|
+
nodeProps[propDef.name] = instanceProp.value;
|
|
161
|
+
}
|
|
162
|
+
else if (propDef.default !== undefined) {
|
|
163
|
+
// Fall back to definition default
|
|
164
|
+
nodeProps[propDef.name] = propDef.default;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Call the implementation (await in case it's async)
|
|
169
|
+
const outputs = await definition.impl(nodeInputs, nodeProps);
|
|
170
|
+
// Cache and return
|
|
171
|
+
cache.set(nodeName, outputs);
|
|
172
|
+
return outputs;
|
|
173
|
+
}
|
|
174
|
+
// Evaluate starting from the output node
|
|
175
|
+
const outputs = await evaluateNode(outputNode);
|
|
176
|
+
return outputs[outputPort];
|
|
177
|
+
}
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { evaluate } from './evaluate';
|
|
2
|
+
export type { NodeImplFn, NodeDefinitionWithImpl, EvaluateOptions, PropDefinitionWithOptions } from './types';
|
|
3
|
+
export { mathDefinitions, constNumberDef, addDef, multiplyDef } from './definitions/math';
|
|
4
|
+
export { uiDefinitions, pageDef, formDef, inputDef, buttonDef, textDef, graphInputDef, graphOutputDef, graphPropDef } from './definitions/ui';
|
|
5
|
+
export { coreDefinitions, jsonSelectDef, jsonObjectDef, flowGuardDef, stringTemplateDef, stringConcatDef } from './definitions/core';
|
|
6
|
+
export { netDefinitions, graphqlRequestDef } from './definitions/net';
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { evaluate } from './evaluate';
|
|
2
|
+
// Re-export node definitions for convenience
|
|
3
|
+
export { mathDefinitions, constNumberDef, addDef, multiplyDef } from './definitions/math';
|
|
4
|
+
export { uiDefinitions, pageDef, formDef, inputDef, buttonDef, textDef, graphInputDef, graphOutputDef, graphPropDef } from './definitions/ui';
|
|
5
|
+
export { coreDefinitions, jsonSelectDef, jsonObjectDef, flowGuardDef, stringTemplateDef, stringConcatDef } from './definitions/core';
|
|
6
|
+
export { netDefinitions, graphqlRequestDef } from './definitions/net';
|
package/esm/types.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { NodeDefinition, PropDefinition } from '@fbp/types';
|
|
2
|
+
/**
|
|
3
|
+
* A function that implements a node's computation.
|
|
4
|
+
* Takes inputs (keyed by port name) and props, returns outputs (keyed by port name).
|
|
5
|
+
* Can be sync or async - the evaluator will await the result.
|
|
6
|
+
*/
|
|
7
|
+
export type NodeImplFn = (inputs: Record<string, any>, props: Record<string, any>) => Record<string, any> | Promise<Record<string, any>>;
|
|
8
|
+
/**
|
|
9
|
+
* Extended PropDefinition with runtime-only options for enum/select types.
|
|
10
|
+
* The options field is not part of the JSON schema but is used by the UI.
|
|
11
|
+
*/
|
|
12
|
+
export interface PropDefinitionWithOptions extends PropDefinition {
|
|
13
|
+
options?: string[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A NodeDefinition extended with an optional implementation function.
|
|
17
|
+
* The impl function is not part of the JSON schema (not serializable),
|
|
18
|
+
* but is used at runtime for evaluation.
|
|
19
|
+
*/
|
|
20
|
+
export interface NodeDefinitionWithImpl extends Omit<NodeDefinition, 'props'> {
|
|
21
|
+
props?: PropDefinitionWithOptions[];
|
|
22
|
+
impl?: NodeImplFn;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for the evaluate function.
|
|
26
|
+
*/
|
|
27
|
+
export interface EvaluateOptions {
|
|
28
|
+
/** Node definitions with implementations */
|
|
29
|
+
definitions: NodeDefinitionWithImpl[];
|
|
30
|
+
/** The node to get output from */
|
|
31
|
+
outputNode: string;
|
|
32
|
+
/** The port to get output from */
|
|
33
|
+
outputPort: string;
|
|
34
|
+
/** External inputs to provide to graphInput nodes (keyed by portName property value) */
|
|
35
|
+
inputs?: Record<string, any>;
|
|
36
|
+
/** Props to provide to graphProp nodes (keyed by propName property value) */
|
|
37
|
+
props?: Record<string, any>;
|
|
38
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/evaluate.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Graph } from '@fbp/types';
|
|
2
|
+
import type { EvaluateOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Evaluate a graph starting from the specified output node/port.
|
|
5
|
+
* Uses lazy evaluation - only evaluates nodes that are needed for the output.
|
|
6
|
+
* Fully async to support async node implementations.
|
|
7
|
+
*
|
|
8
|
+
* @param graph - The graph to evaluate
|
|
9
|
+
* @param options - Evaluation options including definitions, output node/port, and external inputs
|
|
10
|
+
* @returns Promise resolving to the value at the specified output port
|
|
11
|
+
*/
|
|
12
|
+
export declare function evaluate(graph: Graph, options: EvaluateOptions): Promise<any>;
|
package/evaluate.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.evaluate = evaluate;
|
|
4
|
+
/**
|
|
5
|
+
* Evaluate a graph starting from the specified output node/port.
|
|
6
|
+
* Uses lazy evaluation - only evaluates nodes that are needed for the output.
|
|
7
|
+
* Fully async to support async node implementations.
|
|
8
|
+
*
|
|
9
|
+
* @param graph - The graph to evaluate
|
|
10
|
+
* @param options - Evaluation options including definitions, output node/port, and external inputs
|
|
11
|
+
* @returns Promise resolving to the value at the specified output port
|
|
12
|
+
*/
|
|
13
|
+
async function evaluate(graph, options) {
|
|
14
|
+
const { definitions, outputNode, outputPort, inputs = {}, props = {} } = options;
|
|
15
|
+
// Build lookup maps
|
|
16
|
+
const nodeMap = new Map();
|
|
17
|
+
for (const node of graph.nodes) {
|
|
18
|
+
nodeMap.set(node.name, node);
|
|
19
|
+
}
|
|
20
|
+
const defMap = new Map();
|
|
21
|
+
for (const def of definitions) {
|
|
22
|
+
defMap.set(def.type, def);
|
|
23
|
+
}
|
|
24
|
+
// Build edge lookup: destination node -> destination port -> edges (in array order)
|
|
25
|
+
const edgesByDst = new Map();
|
|
26
|
+
for (const edge of graph.edges) {
|
|
27
|
+
const dstNode = edge.dst.node;
|
|
28
|
+
const dstPort = edge.dst.port;
|
|
29
|
+
if (!edgesByDst.has(dstNode)) {
|
|
30
|
+
edgesByDst.set(dstNode, new Map());
|
|
31
|
+
}
|
|
32
|
+
const portMap = edgesByDst.get(dstNode);
|
|
33
|
+
if (!portMap.has(dstPort)) {
|
|
34
|
+
portMap.set(dstPort, []);
|
|
35
|
+
}
|
|
36
|
+
portMap.get(dstPort).push(edge);
|
|
37
|
+
}
|
|
38
|
+
// Cache for evaluated node outputs to avoid re-evaluation
|
|
39
|
+
const cache = new Map();
|
|
40
|
+
/**
|
|
41
|
+
* Recursively evaluate a node and return all its outputs.
|
|
42
|
+
* Fully async to support async node implementations.
|
|
43
|
+
*/
|
|
44
|
+
async function evaluateNode(nodeName) {
|
|
45
|
+
// Check cache first
|
|
46
|
+
if (cache.has(nodeName)) {
|
|
47
|
+
return cache.get(nodeName);
|
|
48
|
+
}
|
|
49
|
+
const node = nodeMap.get(nodeName);
|
|
50
|
+
if (!node) {
|
|
51
|
+
throw new Error(`Node not found: ${nodeName}`);
|
|
52
|
+
}
|
|
53
|
+
// Handle special boundary nodes (graphInput, graphOutput, graphProp)
|
|
54
|
+
// Boundary nodes are identified by their type property, not by name prefix
|
|
55
|
+
if (node.type === 'graphInput') {
|
|
56
|
+
// Get the port name from the portName property
|
|
57
|
+
const portNameProp = node.props?.find(p => p.name === 'portName');
|
|
58
|
+
const inputName = portNameProp?.value || nodeName;
|
|
59
|
+
// Check external inputs first, then fall back to node's default prop
|
|
60
|
+
let value = inputs[inputName];
|
|
61
|
+
if (value === undefined) {
|
|
62
|
+
const defaultProp = node.props?.find(p => p.name === 'default');
|
|
63
|
+
value = defaultProp?.value;
|
|
64
|
+
}
|
|
65
|
+
const result = { value };
|
|
66
|
+
cache.set(nodeName, result);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
if (node.type === 'graphProp') {
|
|
70
|
+
// Get the prop name from the propName property
|
|
71
|
+
const propNameProp = node.props?.find(p => p.name === 'propName');
|
|
72
|
+
const propName = propNameProp?.value || nodeName;
|
|
73
|
+
const value = props[propName];
|
|
74
|
+
const result = { value };
|
|
75
|
+
cache.set(nodeName, result);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
// Handle subnet nodes (kind: 'subnet')
|
|
79
|
+
if (node.kind === 'subnet' && node.nodes && node.edges) {
|
|
80
|
+
// Collect inputs for the subnet by evaluating upstream nodes
|
|
81
|
+
const subnetInputs = {};
|
|
82
|
+
const subnetPortEdges = edgesByDst.get(nodeName);
|
|
83
|
+
if (node.inputs && subnetPortEdges) {
|
|
84
|
+
for (const inputPort of node.inputs) {
|
|
85
|
+
const edges = subnetPortEdges.get(inputPort.name) ?? [];
|
|
86
|
+
if (edges.length > 0) {
|
|
87
|
+
const edge = edges[0];
|
|
88
|
+
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
89
|
+
subnetInputs[inputPort.name] = upstreamOutputs[edge.src.port];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Find the output boundary nodes in the subnet
|
|
94
|
+
const subnetOutputs = {};
|
|
95
|
+
if (node.outputs) {
|
|
96
|
+
for (const outputPort of node.outputs) {
|
|
97
|
+
// Find the graphOutput boundary node by matching portName property
|
|
98
|
+
const outputBoundaryNode = node.nodes.find(n => {
|
|
99
|
+
if (n.type !== 'graphOutput')
|
|
100
|
+
return false;
|
|
101
|
+
const portNameProp = n.props?.find(p => p.name === 'portName');
|
|
102
|
+
return portNameProp?.value === outputPort.name;
|
|
103
|
+
});
|
|
104
|
+
if (!outputBoundaryNode) {
|
|
105
|
+
throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
|
|
106
|
+
}
|
|
107
|
+
// Recursively evaluate the subnet's internal graph
|
|
108
|
+
const subnetResult = await evaluate({
|
|
109
|
+
name: `${nodeName}_subnet`,
|
|
110
|
+
nodes: node.nodes,
|
|
111
|
+
edges: node.edges
|
|
112
|
+
}, {
|
|
113
|
+
definitions,
|
|
114
|
+
outputNode: outputBoundaryNode.name,
|
|
115
|
+
outputPort: 'value',
|
|
116
|
+
inputs: subnetInputs
|
|
117
|
+
});
|
|
118
|
+
subnetOutputs[outputPort.name] = subnetResult;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
cache.set(nodeName, subnetOutputs);
|
|
122
|
+
return subnetOutputs;
|
|
123
|
+
}
|
|
124
|
+
// Get the definition for this node type
|
|
125
|
+
const definition = defMap.get(node.type);
|
|
126
|
+
if (!definition) {
|
|
127
|
+
throw new Error(`No definition found for node type: ${node.type}`);
|
|
128
|
+
}
|
|
129
|
+
if (!definition.impl) {
|
|
130
|
+
throw new Error(`No implementation found for node type: ${node.type}`);
|
|
131
|
+
}
|
|
132
|
+
// Collect inputs by evaluating upstream nodes
|
|
133
|
+
const nodeInputs = {};
|
|
134
|
+
const portEdges = edgesByDst.get(nodeName);
|
|
135
|
+
if (definition.inputs) {
|
|
136
|
+
for (const inputPort of definition.inputs) {
|
|
137
|
+
const edges = portEdges?.get(inputPort.name) ?? [];
|
|
138
|
+
if (inputPort.multi) {
|
|
139
|
+
// Multi-input port: collect all values in edge array order
|
|
140
|
+
const values = await Promise.all(edges.map(async (edge) => {
|
|
141
|
+
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
142
|
+
return upstreamOutputs[edge.src.port];
|
|
143
|
+
}));
|
|
144
|
+
nodeInputs[inputPort.name] = values;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
// Single input: take first edge if exists
|
|
148
|
+
if (edges.length > 0) {
|
|
149
|
+
const edge = edges[0];
|
|
150
|
+
const upstreamOutputs = await evaluateNode(edge.src.node);
|
|
151
|
+
nodeInputs[inputPort.name] = upstreamOutputs[edge.src.port];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// Get props from node instance
|
|
157
|
+
const nodeProps = {};
|
|
158
|
+
if (definition.props) {
|
|
159
|
+
for (const propDef of definition.props) {
|
|
160
|
+
// First check node instance props
|
|
161
|
+
const instanceProp = node.props?.find(p => p.name === propDef.name);
|
|
162
|
+
if (instanceProp !== undefined && instanceProp.value !== undefined) {
|
|
163
|
+
nodeProps[propDef.name] = instanceProp.value;
|
|
164
|
+
}
|
|
165
|
+
else if (propDef.default !== undefined) {
|
|
166
|
+
// Fall back to definition default
|
|
167
|
+
nodeProps[propDef.name] = propDef.default;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Call the implementation (await in case it's async)
|
|
172
|
+
const outputs = await definition.impl(nodeInputs, nodeProps);
|
|
173
|
+
// Cache and return
|
|
174
|
+
cache.set(nodeName, outputs);
|
|
175
|
+
return outputs;
|
|
176
|
+
}
|
|
177
|
+
// Evaluate starting from the output node
|
|
178
|
+
const outputs = await evaluateNode(outputNode);
|
|
179
|
+
return outputs[outputPort];
|
|
180
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { evaluate } from './evaluate';
|
|
2
|
+
export type { NodeImplFn, NodeDefinitionWithImpl, EvaluateOptions, PropDefinitionWithOptions } from './types';
|
|
3
|
+
export { mathDefinitions, constNumberDef, addDef, multiplyDef } from './definitions/math';
|
|
4
|
+
export { uiDefinitions, pageDef, formDef, inputDef, buttonDef, textDef, graphInputDef, graphOutputDef, graphPropDef } from './definitions/ui';
|
|
5
|
+
export { coreDefinitions, jsonSelectDef, jsonObjectDef, flowGuardDef, stringTemplateDef, stringConcatDef } from './definitions/core';
|
|
6
|
+
export { netDefinitions, graphqlRequestDef } from './definitions/net';
|