@fsai-flow/workflow 0.0.2 → 0.0.3
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/dist/README.md +31 -0
- package/dist/package.json +42 -0
- package/dist/src/index.d.ts +21 -0
- package/dist/src/index.js +33 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib/Constants.d.ts +68 -0
- package/dist/src/lib/Constants.js +106 -0
- package/dist/src/lib/Constants.js.map +1 -0
- package/dist/src/lib/DeferredPromise.d.ts +6 -0
- package/dist/src/lib/DeferredPromise.js +11 -0
- package/dist/src/lib/DeferredPromise.js.map +1 -0
- package/dist/src/lib/Expression.d.ts +65 -0
- package/dist/src/lib/Expression.js +215 -0
- package/dist/src/lib/Expression.js.map +1 -0
- package/dist/src/lib/Interfaces.d.ts +1579 -0
- package/dist/src/lib/Interfaces.js +44 -0
- package/dist/src/lib/Interfaces.js.map +1 -0
- package/dist/src/lib/LoggerProxy.d.ts +9 -0
- package/dist/src/lib/LoggerProxy.js +40 -0
- package/dist/src/lib/LoggerProxy.js.map +1 -0
- package/dist/src/lib/MetadataUtils.d.ts +4 -0
- package/dist/src/lib/MetadataUtils.js +27 -0
- package/dist/src/lib/MetadataUtils.js.map +1 -0
- package/dist/src/lib/NodeErrors.d.ts +82 -0
- package/dist/src/lib/NodeErrors.js +289 -0
- package/dist/src/lib/NodeErrors.js.map +1 -0
- package/dist/src/lib/NodeHelpers.d.ts +198 -0
- package/dist/src/lib/NodeHelpers.js +1348 -0
- package/dist/src/lib/NodeHelpers.js.map +1 -0
- package/dist/src/lib/ObservableObject.d.ts +5 -0
- package/dist/src/lib/ObservableObject.js +61 -0
- package/dist/src/lib/ObservableObject.js.map +1 -0
- package/dist/src/lib/RoutingNode.d.ts +18 -0
- package/dist/src/lib/RoutingNode.js +508 -0
- package/dist/src/lib/RoutingNode.js.map +1 -0
- package/dist/src/lib/TelemetryHelpers.d.ts +3 -0
- package/dist/src/lib/TelemetryHelpers.js +69 -0
- package/dist/src/lib/TelemetryHelpers.js.map +1 -0
- package/dist/src/lib/TypeValidation.d.ts +21 -0
- package/dist/src/lib/TypeValidation.js +385 -0
- package/dist/src/lib/TypeValidation.js.map +1 -0
- package/dist/src/lib/VersionedNodeType.d.ts +9 -0
- package/dist/src/lib/VersionedNodeType.js +26 -0
- package/dist/src/lib/VersionedNodeType.js.map +1 -0
- package/dist/src/lib/Workflow.d.ts +248 -0
- package/dist/src/lib/Workflow.js +904 -0
- package/dist/src/lib/Workflow.js.map +1 -0
- package/dist/src/lib/WorkflowDataProxy.d.ts +87 -0
- package/dist/src/lib/WorkflowDataProxy.js +556 -0
- package/dist/src/lib/WorkflowDataProxy.js.map +1 -0
- package/dist/src/lib/WorkflowErrors.d.ts +9 -0
- package/dist/src/lib/WorkflowErrors.js +18 -0
- package/dist/src/lib/WorkflowErrors.js.map +1 -0
- package/dist/src/lib/WorkflowHooks.d.ts +11 -0
- package/dist/src/lib/WorkflowHooks.js +34 -0
- package/dist/src/lib/WorkflowHooks.js.map +1 -0
- package/dist/src/lib/errors/base/base.error.d.ts +30 -0
- package/dist/src/lib/errors/base/base.error.js +45 -0
- package/dist/src/lib/errors/base/base.error.js.map +1 -0
- package/dist/src/lib/errors/base/operational.error.d.ts +15 -0
- package/dist/src/lib/errors/base/operational.error.js +19 -0
- package/dist/src/lib/errors/base/operational.error.js.map +1 -0
- package/dist/src/lib/errors/error.types.d.ts +11 -0
- package/dist/src/lib/errors/error.types.js +3 -0
- package/dist/src/lib/errors/error.types.js.map +1 -0
- package/dist/src/lib/errors/index.d.ts +1 -0
- package/dist/src/lib/errors/index.js +6 -0
- package/dist/src/lib/errors/index.js.map +1 -0
- package/dist/src/lib/result.d.ts +19 -0
- package/dist/src/lib/result.js +36 -0
- package/dist/src/lib/result.js.map +1 -0
- package/dist/src/lib/utils.d.ts +50 -0
- package/dist/src/lib/utils.js +119 -0
- package/dist/src/lib/utils.js.map +1 -0
- package/package.json +5 -1
- package/src/lib/Interfaces.ts +28 -33
- package/src/lib/Workflow.ts +1 -1
- package/src/lib/WorkflowDataProxy.ts +1 -1
- package/src/lib/utils.ts +99 -120
package/src/lib/utils.ts
CHANGED
|
@@ -1,141 +1,125 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from
|
|
7
|
-
import type { IDisplayOptions, INodeProperties } from
|
|
8
|
-
import { merge } from
|
|
2
|
+
parse as esprimaParse,
|
|
3
|
+
Syntax,
|
|
4
|
+
type Node as SyntaxNode,
|
|
5
|
+
type ExpressionStatement,
|
|
6
|
+
} from 'esprima-next';
|
|
7
|
+
import type { IDisplayOptions, INodeProperties } from './Interfaces';
|
|
8
|
+
import { merge } from 'lodash';
|
|
9
9
|
|
|
10
10
|
export function updateDisplayOptions(
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
displayOptions: IDisplayOptions,
|
|
12
|
+
properties: INodeProperties[],
|
|
13
13
|
) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
return properties.map((nodeProperty) => {
|
|
15
|
+
return {
|
|
16
|
+
...nodeProperty,
|
|
17
|
+
displayOptions: merge({}, nodeProperty.displayOptions, displayOptions),
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
20
|
}
|
|
21
21
|
export function randomInt(max: number): number;
|
|
22
22
|
export function randomInt(min: number, max: number): number;
|
|
23
23
|
/**
|
|
24
|
-
* Generates a
|
|
24
|
+
* Generates a random integer within a specified range.
|
|
25
25
|
*
|
|
26
|
-
* @param {number} min - The
|
|
27
|
-
* @param {number} [max] - The
|
|
28
|
-
* @returns {number} A random integer within
|
|
26
|
+
* @param {number} min - The lower bound of the range. If `max` is not provided, this value is used as the upper bound and the lower bound is set to 0.
|
|
27
|
+
* @param {number} [max] - The upper bound of the range, not inclusive.
|
|
28
|
+
* @returns {number} A random integer within the specified range.
|
|
29
29
|
*/
|
|
30
30
|
export function randomInt(min: number, max?: number): number {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
if (max === undefined) {
|
|
32
|
+
max = min;
|
|
33
|
+
min = 0;
|
|
34
|
+
}
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
if (max <= min) {
|
|
37
|
+
throw new Error("max must be greater than min");
|
|
38
|
+
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const range = max - min;
|
|
41
|
+
const maxUnbiased = Math.floor(0xffffffff / range) * range;
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
let rand: number;
|
|
44
|
+
do {
|
|
45
|
+
rand = crypto.getRandomValues(new Uint32Array(1))[0];
|
|
46
|
+
} while (rand >= maxUnbiased);
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
return min + (rand % range);
|
|
49
49
|
}
|
|
50
50
|
type MutuallyExclusive<T, U> =
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
| (T & { [k in Exclude<keyof U, keyof T>]?: never })
|
|
52
|
+
| (U & { [k in Exclude<keyof T, keyof U>]?: never });
|
|
53
53
|
|
|
54
54
|
type JSONParseOptions<T> = { acceptJSObject?: boolean } & MutuallyExclusive<
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
{ errorMessage?: string },
|
|
56
|
+
{ fallbackValue?: T }
|
|
57
57
|
>;
|
|
58
58
|
|
|
59
|
-
export const jsonParse = <T>(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
59
|
+
export const jsonParse = <T>(jsonString: string, options?: JSONParseOptions<T>): T => {
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(jsonString) as T;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (options?.acceptJSObject) {
|
|
64
|
+
try {
|
|
65
|
+
const jsonStringCleaned = parseJSObject(jsonString);
|
|
66
|
+
return jsonStringCleaned as T;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
// Ignore this error and return the original error or the fallback value
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (options?.fallbackValue !== undefined) {
|
|
72
|
+
if (options.fallbackValue instanceof Function) {
|
|
73
|
+
return options.fallbackValue();
|
|
74
|
+
}
|
|
75
|
+
return options.fallbackValue;
|
|
76
|
+
} else if (options?.errorMessage) {
|
|
77
|
+
// TODO REFACTOR: Use ApplicationError from offical @fsai-flow/workflow repo
|
|
78
|
+
throw new Error(options.errorMessage);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
86
83
|
};
|
|
87
84
|
|
|
88
85
|
type JSONStringifyOptions = {
|
|
89
|
-
|
|
86
|
+
replaceCircularRefs?: boolean;
|
|
90
87
|
};
|
|
91
88
|
|
|
92
|
-
export const replaceCircularReferences = <T>(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
for (const key in value) {
|
|
104
|
-
copy[key] = replaceCircularReferences(value[key], knownObjects);
|
|
105
|
-
}
|
|
106
|
-
knownObjects.delete(value);
|
|
107
|
-
return copy;
|
|
89
|
+
export const replaceCircularReferences = <T>(value: T, knownObjects = new WeakSet()): T => {
|
|
90
|
+
if (typeof value !== 'object' || value === null || value instanceof RegExp) return value;
|
|
91
|
+
if ('toJSON' in value && typeof value.toJSON === 'function') return value.toJSON() as T;
|
|
92
|
+
if (knownObjects.has(value)) return '[Circular Reference]' as T;
|
|
93
|
+
knownObjects.add(value);
|
|
94
|
+
const copy = (Array.isArray(value) ? [] : {}) as T;
|
|
95
|
+
for (const key in value) {
|
|
96
|
+
copy[key] = replaceCircularReferences(value[key], knownObjects);
|
|
97
|
+
}
|
|
98
|
+
knownObjects.delete(value);
|
|
99
|
+
return copy;
|
|
108
100
|
};
|
|
109
101
|
|
|
110
|
-
export const jsonStringify = (
|
|
111
|
-
|
|
112
|
-
options: JSONStringifyOptions = {}
|
|
113
|
-
): string => {
|
|
114
|
-
return JSON.stringify(
|
|
115
|
-
options?.replaceCircularRefs ? replaceCircularReferences(obj) : obj
|
|
116
|
-
);
|
|
102
|
+
export const jsonStringify = (obj: unknown, options: JSONStringifyOptions = {}): string => {
|
|
103
|
+
return JSON.stringify(options?.replaceCircularRefs ? replaceCircularReferences(obj) : obj);
|
|
117
104
|
};
|
|
118
105
|
|
|
119
106
|
function syntaxNodeToValue(expression?: SyntaxNode | null): unknown {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
default:
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
107
|
+
switch (expression?.type) {
|
|
108
|
+
case Syntax.ObjectExpression:
|
|
109
|
+
return Object.fromEntries(
|
|
110
|
+
expression.properties
|
|
111
|
+
.filter((prop) => prop.type === Syntax.Property)
|
|
112
|
+
.map(({ key, value }) => [syntaxNodeToValue(key), syntaxNodeToValue(value)]),
|
|
113
|
+
);
|
|
114
|
+
case Syntax.Identifier:
|
|
115
|
+
return expression.name;
|
|
116
|
+
case Syntax.Literal:
|
|
117
|
+
return expression.value;
|
|
118
|
+
case Syntax.ArrayExpression:
|
|
119
|
+
return expression.elements.map((exp) => syntaxNodeToValue(exp));
|
|
120
|
+
default:
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
139
123
|
}
|
|
140
124
|
|
|
141
125
|
/**
|
|
@@ -144,23 +128,18 @@ function syntaxNodeToValue(expression?: SyntaxNode | null): unknown {
|
|
|
144
128
|
* - unquoted keys
|
|
145
129
|
*/
|
|
146
130
|
function parseJSObject(objectAsString: string): object {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
);
|
|
131
|
+
const jsExpression = esprimaParse(`(${objectAsString})`).body.find(
|
|
132
|
+
(node): node is ExpressionStatement =>
|
|
133
|
+
node.type === Syntax.ExpressionStatement && node.expression.type === Syntax.ObjectExpression,
|
|
134
|
+
);
|
|
152
135
|
|
|
153
|
-
|
|
136
|
+
return syntaxNodeToValue(jsExpression?.expression) as object;
|
|
154
137
|
}
|
|
155
138
|
|
|
156
139
|
/**
|
|
157
140
|
* Checks if a value is an object with a specific key and provides a type guard for the key.
|
|
158
141
|
*/
|
|
159
|
-
export function hasKey<T extends PropertyKey>(
|
|
160
|
-
|
|
161
|
-
key: T
|
|
162
|
-
): value is Record<T, unknown> {
|
|
163
|
-
return (
|
|
164
|
-
value !== null && typeof value === "object" && value.hasOwnProperty(key)
|
|
165
|
-
);
|
|
142
|
+
export function hasKey<T extends PropertyKey>(value: unknown, key: T): value is Record<T, unknown> {
|
|
143
|
+
return value !== null && typeof value === 'object' && value.hasOwnProperty(key);
|
|
166
144
|
}
|
|
145
|
+
|