@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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Dan Lynch <pyramation@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # @fbp/evaluator
2
+
3
+ Lazy graph evaluator for Flow-Based Programming.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @fbp/evaluator
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { evaluate } from '@fbp/evaluator';
15
+ import type { Graph } from '@fbp/types';
16
+ import type { NodeDefinitionWithImpl } from '@fbp/evaluator';
17
+
18
+ // Define node implementations
19
+ const addDef: NodeDefinitionWithImpl = {
20
+ context: 'js',
21
+ category: 'math',
22
+ type: 'js/math/add',
23
+ inputs: [
24
+ { name: 'a', type: 'number' },
25
+ { name: 'b', type: 'number' }
26
+ ],
27
+ outputs: [{ name: 'sum', type: 'number' }],
28
+ impl: (inputs) => ({
29
+ sum: (inputs.a ?? 0) + (inputs.b ?? 0)
30
+ })
31
+ };
32
+
33
+ // Create a graph
34
+ const graph: Graph = {
35
+ name: 'simple-add',
36
+ nodes: [
37
+ { name: 'num1', type: 'js/const/number', props: [{ name: 'value', type: 'number', value: 5 }] },
38
+ { name: 'num2', type: 'js/const/number', props: [{ name: 'value', type: 'number', value: 3 }] },
39
+ { name: 'add', type: 'js/math/add' }
40
+ ],
41
+ edges: [
42
+ { src: { node: 'num1', port: 'value' }, dst: { node: 'add', port: 'a' } },
43
+ { src: { node: 'num2', port: 'value' }, dst: { node: 'add', port: 'b' } }
44
+ ]
45
+ };
46
+
47
+ // Evaluate the graph
48
+ const result = evaluate(graph, {
49
+ definitions: [constNumberDef, addDef],
50
+ outputNode: 'add',
51
+ outputPort: 'sum'
52
+ });
53
+
54
+ console.log(result); // 8
55
+ ```
56
+
57
+ ## Features
58
+
59
+ - **Lazy evaluation**: Only evaluates nodes that are needed for the output
60
+ - **Multi-input ports**: Supports ports that accept multiple incoming edges (values collected in edge array order)
61
+ - **Boundary nodes**: Supports `graphInput`, `graphOutput`, and `graphProp` boundary nodes for graph inputs/outputs/props
62
+
63
+ ## Boundary Node Design
64
+
65
+ Boundary nodes use a **property-based approach**:
66
+ - Node keys are normal identifiers (e.g., `input_a`, `output_result`, `prop_scale`)
67
+ - The node's `type` property identifies it as a boundary node: `graphInput`, `graphOutput`, `graphProp`
68
+ - The port/prop name is stored as a property: `{ name: 'portName', value: 'a' }` or `{ name: 'propName', value: 'scale' }`
69
+
70
+ ## API
71
+
72
+ ### `evaluate(graph, options)`
73
+
74
+ Evaluates a graph starting from the specified output node/port.
75
+
76
+ **Parameters:**
77
+ - `graph: Graph` - The graph to evaluate
78
+ - `options: EvaluateOptions` - Evaluation options
79
+ - `definitions: NodeDefinitionWithImpl[]` - Node definitions with implementations
80
+ - `outputNode: string` - The node to get output from
81
+ - `outputPort: string` - The port to get output from
82
+ - `inputs?: Record<string, any>` - External inputs for `graphInput` nodes (keyed by portName)
83
+ - `props?: Record<string, any>` - Props for `graphProp` nodes (keyed by propName)
84
+
85
+ **Returns:** The value at the specified output port
@@ -0,0 +1,72 @@
1
+ import type { NodeDefinitionWithImpl } from '../types';
2
+ /**
3
+ * Core node definitions with implementations.
4
+ * These nodes handle JSON manipulation, flow control, and string operations.
5
+ */
6
+ /**
7
+ * core/json/select - Extract a value from JSON by path
8
+ *
9
+ * Props:
10
+ * - path: string (dot-path like "a.b.c" or "data.user.email")
11
+ *
12
+ * Inputs:
13
+ * - obj: json (the object to extract from)
14
+ *
15
+ * Outputs:
16
+ * - value: any (the extracted value)
17
+ */
18
+ export declare const jsonSelectDef: NodeDefinitionWithImpl;
19
+ /**
20
+ * core/json/object - Build a JSON object from named inputs
21
+ *
22
+ * This node has dynamic inputs - any input wired to it becomes a key in the output object.
23
+ * The implementation receives all inputs as a Record<string, any>.
24
+ *
25
+ * Inputs:
26
+ * - (dynamic) arbitrary named inputs
27
+ *
28
+ * Outputs:
29
+ * - value: json (the constructed object)
30
+ */
31
+ export declare const jsonObjectDef: NodeDefinitionWithImpl;
32
+ /**
33
+ * core/flow/guard - Stop the flow if a condition fails
34
+ *
35
+ * Inputs:
36
+ * - ok: boolean (the condition to check)
37
+ * - error?: json (optional error info)
38
+ *
39
+ * Outputs:
40
+ * - pass: signal (emitted if ok is true)
41
+ * - fail: signal (emitted if ok is false)
42
+ * - error: json (the error if failed)
43
+ */
44
+ export declare const flowGuardDef: NodeDefinitionWithImpl;
45
+ /**
46
+ * core/string/template - Build a string from a template with placeholders
47
+ *
48
+ * Props:
49
+ * - template: string (template with {{placeholder}} syntax)
50
+ *
51
+ * Inputs:
52
+ * - (dynamic) values to substitute into the template
53
+ *
54
+ * Outputs:
55
+ * - value: string (the resulting string)
56
+ */
57
+ export declare const stringTemplateDef: NodeDefinitionWithImpl;
58
+ /**
59
+ * core/string/concat - Concatenate strings with an optional separator
60
+ *
61
+ * Props:
62
+ * - prefix: string (optional prefix)
63
+ * - suffix: string (optional suffix)
64
+ *
65
+ * Inputs:
66
+ * - value: string (the main value)
67
+ *
68
+ * Outputs:
69
+ * - value: string (the resulting string)
70
+ */
71
+ export declare const stringConcatDef: NodeDefinitionWithImpl;
72
+ export declare const coreDefinitions: NodeDefinitionWithImpl[];
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.coreDefinitions = exports.stringConcatDef = exports.stringTemplateDef = exports.flowGuardDef = exports.jsonObjectDef = exports.jsonSelectDef = void 0;
4
+ /**
5
+ * Core node definitions with implementations.
6
+ * These nodes handle JSON manipulation, flow control, and string operations.
7
+ */
8
+ /**
9
+ * core/json/select - Extract a value from JSON by path
10
+ *
11
+ * Props:
12
+ * - path: string (dot-path like "a.b.c" or "data.user.email")
13
+ *
14
+ * Inputs:
15
+ * - obj: json (the object to extract from)
16
+ *
17
+ * Outputs:
18
+ * - value: any (the extracted value)
19
+ */
20
+ exports.jsonSelectDef = {
21
+ context: 'core',
22
+ category: 'json',
23
+ type: 'core/json/select',
24
+ icon: 'circle',
25
+ inputs: [
26
+ { name: 'obj', type: 'json' }
27
+ ],
28
+ outputs: [
29
+ { name: 'value', type: 'any' }
30
+ ],
31
+ props: [
32
+ { name: 'path', type: 'string', default: '' }
33
+ ],
34
+ description: 'Extract a value from JSON by dot-path',
35
+ impl: (inputs, props) => {
36
+ const { obj } = inputs;
37
+ const { path } = props;
38
+ if (!path || obj === undefined || obj === null) {
39
+ return { value: undefined };
40
+ }
41
+ const parts = path.split('.');
42
+ let current = obj;
43
+ for (const part of parts) {
44
+ if (current === undefined || current === null) {
45
+ return { value: undefined };
46
+ }
47
+ // Handle array index access like "items.0.name"
48
+ const index = parseInt(part, 10);
49
+ if (!isNaN(index) && Array.isArray(current)) {
50
+ current = current[index];
51
+ }
52
+ else if (typeof current === 'object') {
53
+ current = current[part];
54
+ }
55
+ else {
56
+ return { value: undefined };
57
+ }
58
+ }
59
+ return { value: current };
60
+ }
61
+ };
62
+ /**
63
+ * core/json/object - Build a JSON object from named inputs
64
+ *
65
+ * This node has dynamic inputs - any input wired to it becomes a key in the output object.
66
+ * The implementation receives all inputs as a Record<string, any>.
67
+ *
68
+ * Inputs:
69
+ * - (dynamic) arbitrary named inputs
70
+ *
71
+ * Outputs:
72
+ * - value: json (the constructed object)
73
+ */
74
+ exports.jsonObjectDef = {
75
+ context: 'core',
76
+ category: 'json',
77
+ type: 'core/json/object',
78
+ icon: 'braces',
79
+ inputs: [], // Dynamic inputs - any input name is valid
80
+ outputs: [
81
+ { name: 'value', type: 'json' }
82
+ ],
83
+ props: [],
84
+ description: 'Build a JSON object from named inputs',
85
+ impl: (inputs) => {
86
+ // All inputs become keys in the output object
87
+ return { value: { ...inputs } };
88
+ }
89
+ };
90
+ /**
91
+ * core/flow/guard - Stop the flow if a condition fails
92
+ *
93
+ * Inputs:
94
+ * - ok: boolean (the condition to check)
95
+ * - error?: json (optional error info)
96
+ *
97
+ * Outputs:
98
+ * - pass: signal (emitted if ok is true)
99
+ * - fail: signal (emitted if ok is false)
100
+ * - error: json (the error if failed)
101
+ */
102
+ exports.flowGuardDef = {
103
+ context: 'core',
104
+ category: 'flow',
105
+ type: 'core/flow/guard',
106
+ icon: 'zap',
107
+ inputs: [
108
+ { name: 'ok', type: 'boolean' },
109
+ { name: 'error', type: 'json' }
110
+ ],
111
+ outputs: [
112
+ { name: 'pass', type: 'signal' },
113
+ { name: 'fail', type: 'signal' },
114
+ { name: 'error', type: 'json' }
115
+ ],
116
+ props: [],
117
+ description: 'Stop the flow if a condition fails',
118
+ impl: (inputs) => {
119
+ const { ok, error } = inputs;
120
+ if (ok) {
121
+ return {
122
+ pass: true,
123
+ fail: false,
124
+ error: null
125
+ };
126
+ }
127
+ else {
128
+ return {
129
+ pass: false,
130
+ fail: true,
131
+ error: error || { message: 'Guard condition failed' }
132
+ };
133
+ }
134
+ }
135
+ };
136
+ /**
137
+ * core/string/template - Build a string from a template with placeholders
138
+ *
139
+ * Props:
140
+ * - template: string (template with {{placeholder}} syntax)
141
+ *
142
+ * Inputs:
143
+ * - (dynamic) values to substitute into the template
144
+ *
145
+ * Outputs:
146
+ * - value: string (the resulting string)
147
+ */
148
+ exports.stringTemplateDef = {
149
+ context: 'core',
150
+ category: 'string',
151
+ type: 'core/string/template',
152
+ icon: 'quote',
153
+ inputs: [], // Dynamic inputs based on template placeholders
154
+ outputs: [
155
+ { name: 'value', type: 'string' }
156
+ ],
157
+ props: [
158
+ { name: 'template', type: 'string', default: '' }
159
+ ],
160
+ description: 'Build a string from a template with {{placeholder}} syntax',
161
+ impl: (inputs, props) => {
162
+ const { template } = props;
163
+ if (!template) {
164
+ return { value: '' };
165
+ }
166
+ // Replace {{placeholder}} with input values
167
+ const result = template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
168
+ const value = inputs[key];
169
+ if (value === undefined || value === null) {
170
+ return match; // Keep placeholder if no value
171
+ }
172
+ return String(value);
173
+ });
174
+ return { value: result };
175
+ }
176
+ };
177
+ /**
178
+ * core/string/concat - Concatenate strings with an optional separator
179
+ *
180
+ * Props:
181
+ * - prefix: string (optional prefix)
182
+ * - suffix: string (optional suffix)
183
+ *
184
+ * Inputs:
185
+ * - value: string (the main value)
186
+ *
187
+ * Outputs:
188
+ * - value: string (the resulting string)
189
+ */
190
+ exports.stringConcatDef = {
191
+ context: 'core',
192
+ category: 'string',
193
+ type: 'core/string/concat',
194
+ icon: 'link',
195
+ inputs: [
196
+ { name: 'value', type: 'string' }
197
+ ],
198
+ outputs: [
199
+ { name: 'value', type: 'string' }
200
+ ],
201
+ props: [
202
+ { name: 'prefix', type: 'string', default: '' },
203
+ { name: 'suffix', type: 'string', default: '' }
204
+ ],
205
+ description: 'Concatenate strings with optional prefix/suffix',
206
+ impl: (inputs, props) => {
207
+ const { value = '' } = inputs;
208
+ const { prefix = '', suffix = '' } = props;
209
+ return { value: `${prefix}${value}${suffix}` };
210
+ }
211
+ };
212
+ exports.coreDefinitions = [
213
+ exports.jsonSelectDef,
214
+ exports.jsonObjectDef,
215
+ exports.flowGuardDef,
216
+ exports.stringTemplateDef,
217
+ exports.stringConcatDef
218
+ ];
@@ -0,0 +1,8 @@
1
+ import type { NodeDefinitionWithImpl } from '../types';
2
+ /**
3
+ * Math node definitions with implementations for testing.
4
+ */
5
+ export declare const constNumberDef: NodeDefinitionWithImpl;
6
+ export declare const addDef: NodeDefinitionWithImpl;
7
+ export declare const multiplyDef: NodeDefinitionWithImpl;
8
+ export declare const mathDefinitions: NodeDefinitionWithImpl[];
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mathDefinitions = exports.multiplyDef = exports.addDef = exports.constNumberDef = void 0;
4
+ /**
5
+ * Math node definitions with implementations for testing.
6
+ */
7
+ exports.constNumberDef = {
8
+ context: 'js',
9
+ category: 'const',
10
+ type: 'js/const/number',
11
+ icon: 'hash',
12
+ outputs: [{ name: 'value', type: 'number' }],
13
+ props: [{ name: 'value', type: 'number', default: 0 }],
14
+ description: 'Outputs a constant number value',
15
+ impl: (_inputs, props) => ({
16
+ value: props.value ?? 0
17
+ })
18
+ };
19
+ exports.addDef = {
20
+ context: 'js',
21
+ category: 'math',
22
+ type: 'js/math/add',
23
+ icon: 'plus',
24
+ inputs: [
25
+ { name: 'a', type: 'number' },
26
+ { name: 'b', type: 'number' }
27
+ ],
28
+ outputs: [{ name: 'sum', type: 'number' }],
29
+ description: 'Adds two numbers',
30
+ impl: (inputs) => ({
31
+ sum: (inputs.a ?? 0) + (inputs.b ?? 0)
32
+ })
33
+ };
34
+ exports.multiplyDef = {
35
+ context: 'js',
36
+ category: 'math',
37
+ type: 'js/math/multiply',
38
+ icon: 'x',
39
+ inputs: [
40
+ { name: 'a', type: 'number' },
41
+ { name: 'b', type: 'number' }
42
+ ],
43
+ outputs: [{ name: 'product', type: 'number' }],
44
+ description: 'Multiplies two numbers',
45
+ impl: (inputs) => ({
46
+ product: (inputs.a ?? 0) * (inputs.b ?? 0)
47
+ })
48
+ };
49
+ exports.mathDefinitions = [
50
+ exports.constNumberDef,
51
+ exports.addDef,
52
+ exports.multiplyDef
53
+ ];
@@ -0,0 +1,25 @@
1
+ import type { NodeDefinitionWithImpl } from '../types';
2
+ /**
3
+ * Network node definitions with implementations.
4
+ * These nodes handle GraphQL requests and related operations.
5
+ */
6
+ /**
7
+ * net/graphql/request - Execute GraphQL query or mutation
8
+ *
9
+ * Props:
10
+ * - document: string (GraphQL query/mutation document)
11
+ * - operationName?: string (optional operation name)
12
+ * - endpoint: string (GraphQL endpoint URL)
13
+ * - timeout?: number (request timeout in ms, default 30000)
14
+ *
15
+ * Inputs:
16
+ * - variables: json (GraphQL variables)
17
+ * - headers?: json (additional headers)
18
+ *
19
+ * Outputs:
20
+ * - data: json (response data)
21
+ * - error?: json (error if any)
22
+ * - ok: boolean (true if request succeeded without errors)
23
+ */
24
+ export declare const graphqlRequestDef: NodeDefinitionWithImpl;
25
+ export declare const netDefinitions: NodeDefinitionWithImpl[];
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.netDefinitions = exports.graphqlRequestDef = void 0;
4
+ /**
5
+ * Network node definitions with implementations.
6
+ * These nodes handle GraphQL requests and related operations.
7
+ */
8
+ /**
9
+ * net/graphql/request - Execute GraphQL query or mutation
10
+ *
11
+ * Props:
12
+ * - document: string (GraphQL query/mutation document)
13
+ * - operationName?: string (optional operation name)
14
+ * - endpoint: string (GraphQL endpoint URL)
15
+ * - timeout?: number (request timeout in ms, default 30000)
16
+ *
17
+ * Inputs:
18
+ * - variables: json (GraphQL variables)
19
+ * - headers?: json (additional headers)
20
+ *
21
+ * Outputs:
22
+ * - data: json (response data)
23
+ * - error?: json (error if any)
24
+ * - ok: boolean (true if request succeeded without errors)
25
+ */
26
+ exports.graphqlRequestDef = {
27
+ context: 'net',
28
+ category: 'graphql',
29
+ type: 'net/graphql/request',
30
+ icon: 'graphql',
31
+ inputs: [
32
+ { name: 'variables', type: 'json' },
33
+ { name: 'headers', type: 'json' }
34
+ ],
35
+ outputs: [
36
+ { name: 'data', type: 'json' },
37
+ { name: 'error', type: 'json' },
38
+ { name: 'ok', type: 'boolean' }
39
+ ],
40
+ props: [
41
+ { name: 'document', type: 'string', default: '' },
42
+ { name: 'operationName', type: 'string' },
43
+ { name: 'endpoint', type: 'string', default: '' },
44
+ { name: 'timeout', type: 'number', default: 30000 }
45
+ ],
46
+ description: 'Execute a GraphQL query or mutation',
47
+ impl: async (inputs, props) => {
48
+ const { variables = {}, headers = {} } = inputs;
49
+ const { document, operationName, endpoint, timeout = 30000 } = props;
50
+ if (!endpoint) {
51
+ return {
52
+ data: null,
53
+ error: { message: 'No endpoint specified' },
54
+ ok: false
55
+ };
56
+ }
57
+ if (!document) {
58
+ return {
59
+ data: null,
60
+ error: { message: 'No document specified' },
61
+ ok: false
62
+ };
63
+ }
64
+ const controller = new AbortController();
65
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
66
+ try {
67
+ const response = await fetch(endpoint, {
68
+ method: 'POST',
69
+ headers: {
70
+ 'Content-Type': 'application/json',
71
+ Accept: 'application/json',
72
+ ...headers
73
+ },
74
+ body: JSON.stringify({
75
+ query: document,
76
+ variables,
77
+ operationName: operationName || undefined
78
+ }),
79
+ signal: controller.signal
80
+ });
81
+ clearTimeout(timeoutId);
82
+ if (!response.ok) {
83
+ return {
84
+ data: null,
85
+ error: {
86
+ message: `HTTP error: ${response.status} ${response.statusText}`,
87
+ statusCode: response.status
88
+ },
89
+ ok: false
90
+ };
91
+ }
92
+ const json = await response.json();
93
+ if (json.errors && json.errors.length > 0) {
94
+ return {
95
+ data: json.data || null,
96
+ error: json.errors,
97
+ ok: false
98
+ };
99
+ }
100
+ return {
101
+ data: json.data || null,
102
+ error: null,
103
+ ok: true
104
+ };
105
+ }
106
+ catch (error) {
107
+ clearTimeout(timeoutId);
108
+ if (error instanceof Error) {
109
+ if (error.name === 'AbortError') {
110
+ return {
111
+ data: null,
112
+ error: { message: `Request timed out after ${timeout}ms` },
113
+ ok: false
114
+ };
115
+ }
116
+ return {
117
+ data: null,
118
+ error: { message: error.message },
119
+ ok: false
120
+ };
121
+ }
122
+ return {
123
+ data: null,
124
+ error: { message: String(error) },
125
+ ok: false
126
+ };
127
+ }
128
+ }
129
+ };
130
+ exports.netDefinitions = [
131
+ exports.graphqlRequestDef
132
+ ];
@@ -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[];