@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.
Files changed (79) hide show
  1. package/dist/README.md +31 -0
  2. package/dist/package.json +42 -0
  3. package/dist/src/index.d.ts +21 -0
  4. package/dist/src/index.js +33 -0
  5. package/dist/src/index.js.map +1 -0
  6. package/dist/src/lib/Constants.d.ts +68 -0
  7. package/dist/src/lib/Constants.js +106 -0
  8. package/dist/src/lib/Constants.js.map +1 -0
  9. package/dist/src/lib/DeferredPromise.d.ts +6 -0
  10. package/dist/src/lib/DeferredPromise.js +11 -0
  11. package/dist/src/lib/DeferredPromise.js.map +1 -0
  12. package/dist/src/lib/Expression.d.ts +65 -0
  13. package/dist/src/lib/Expression.js +215 -0
  14. package/dist/src/lib/Expression.js.map +1 -0
  15. package/dist/src/lib/Interfaces.d.ts +1579 -0
  16. package/dist/src/lib/Interfaces.js +44 -0
  17. package/dist/src/lib/Interfaces.js.map +1 -0
  18. package/dist/src/lib/LoggerProxy.d.ts +9 -0
  19. package/dist/src/lib/LoggerProxy.js +40 -0
  20. package/dist/src/lib/LoggerProxy.js.map +1 -0
  21. package/dist/src/lib/MetadataUtils.d.ts +4 -0
  22. package/dist/src/lib/MetadataUtils.js +27 -0
  23. package/dist/src/lib/MetadataUtils.js.map +1 -0
  24. package/dist/src/lib/NodeErrors.d.ts +82 -0
  25. package/dist/src/lib/NodeErrors.js +289 -0
  26. package/dist/src/lib/NodeErrors.js.map +1 -0
  27. package/dist/src/lib/NodeHelpers.d.ts +198 -0
  28. package/dist/src/lib/NodeHelpers.js +1348 -0
  29. package/dist/src/lib/NodeHelpers.js.map +1 -0
  30. package/dist/src/lib/ObservableObject.d.ts +5 -0
  31. package/dist/src/lib/ObservableObject.js +61 -0
  32. package/dist/src/lib/ObservableObject.js.map +1 -0
  33. package/dist/src/lib/RoutingNode.d.ts +18 -0
  34. package/dist/src/lib/RoutingNode.js +508 -0
  35. package/dist/src/lib/RoutingNode.js.map +1 -0
  36. package/dist/src/lib/TelemetryHelpers.d.ts +3 -0
  37. package/dist/src/lib/TelemetryHelpers.js +69 -0
  38. package/dist/src/lib/TelemetryHelpers.js.map +1 -0
  39. package/dist/src/lib/TypeValidation.d.ts +21 -0
  40. package/dist/src/lib/TypeValidation.js +385 -0
  41. package/dist/src/lib/TypeValidation.js.map +1 -0
  42. package/dist/src/lib/VersionedNodeType.d.ts +9 -0
  43. package/dist/src/lib/VersionedNodeType.js +26 -0
  44. package/dist/src/lib/VersionedNodeType.js.map +1 -0
  45. package/dist/src/lib/Workflow.d.ts +248 -0
  46. package/dist/src/lib/Workflow.js +904 -0
  47. package/dist/src/lib/Workflow.js.map +1 -0
  48. package/dist/src/lib/WorkflowDataProxy.d.ts +87 -0
  49. package/dist/src/lib/WorkflowDataProxy.js +556 -0
  50. package/dist/src/lib/WorkflowDataProxy.js.map +1 -0
  51. package/dist/src/lib/WorkflowErrors.d.ts +9 -0
  52. package/dist/src/lib/WorkflowErrors.js +18 -0
  53. package/dist/src/lib/WorkflowErrors.js.map +1 -0
  54. package/dist/src/lib/WorkflowHooks.d.ts +11 -0
  55. package/dist/src/lib/WorkflowHooks.js +34 -0
  56. package/dist/src/lib/WorkflowHooks.js.map +1 -0
  57. package/dist/src/lib/errors/base/base.error.d.ts +30 -0
  58. package/dist/src/lib/errors/base/base.error.js +45 -0
  59. package/dist/src/lib/errors/base/base.error.js.map +1 -0
  60. package/dist/src/lib/errors/base/operational.error.d.ts +15 -0
  61. package/dist/src/lib/errors/base/operational.error.js +19 -0
  62. package/dist/src/lib/errors/base/operational.error.js.map +1 -0
  63. package/dist/src/lib/errors/error.types.d.ts +11 -0
  64. package/dist/src/lib/errors/error.types.js +3 -0
  65. package/dist/src/lib/errors/error.types.js.map +1 -0
  66. package/dist/src/lib/errors/index.d.ts +1 -0
  67. package/dist/src/lib/errors/index.js +6 -0
  68. package/dist/src/lib/errors/index.js.map +1 -0
  69. package/dist/src/lib/result.d.ts +19 -0
  70. package/dist/src/lib/result.js +36 -0
  71. package/dist/src/lib/result.js.map +1 -0
  72. package/dist/src/lib/utils.d.ts +50 -0
  73. package/dist/src/lib/utils.js +119 -0
  74. package/dist/src/lib/utils.js.map +1 -0
  75. package/package.json +5 -1
  76. package/src/lib/Interfaces.ts +28 -33
  77. package/src/lib/Workflow.ts +1 -1
  78. package/src/lib/WorkflowDataProxy.ts +1 -1
  79. package/src/lib/utils.ts +99 -120
package/src/lib/utils.ts CHANGED
@@ -1,141 +1,125 @@
1
1
  import {
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";
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
- displayOptions: IDisplayOptions,
12
- properties: INodeProperties[]
11
+ displayOptions: IDisplayOptions,
12
+ properties: INodeProperties[],
13
13
  ) {
14
- return properties.map((nodeProperty) => {
15
- return {
16
- ...nodeProperty,
17
- displayOptions: merge({}, nodeProperty.displayOptions, displayOptions),
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 cryptographically secure random integer within a specified range.
24
+ * Generates a random integer within a specified range.
25
25
  *
26
- * @param {number} min - The inclusive lower bound.
27
- * @param {number} [max] - The exclusive upper bound. If not provided, min is treated as max and lower bound is 0.
28
- * @returns {number} A random integer within [min, max).
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
- if (max === undefined) {
32
- max = min;
33
- min = 0;
34
- }
31
+ if (max === undefined) {
32
+ max = min;
33
+ min = 0;
34
+ }
35
35
 
36
- if (max <= min) {
37
- throw new Error("max must be greater than min");
38
- }
36
+ if (max <= min) {
37
+ throw new Error("max must be greater than min");
38
+ }
39
39
 
40
- const range = max - min;
41
- const maxUnbiased = Math.floor(0xffffffff / range) * range;
40
+ const range = max - min;
41
+ const maxUnbiased = Math.floor(0xffffffff / range) * range;
42
42
 
43
- let rand: number;
44
- do {
45
- rand = crypto.getRandomValues(new Uint32Array(1))[0];
46
- } while (rand >= maxUnbiased);
43
+ let rand: number;
44
+ do {
45
+ rand = crypto.getRandomValues(new Uint32Array(1))[0];
46
+ } while (rand >= maxUnbiased);
47
47
 
48
- return min + (rand % range);
48
+ return min + (rand % range);
49
49
  }
50
50
  type MutuallyExclusive<T, U> =
51
- | (T & { [k in Exclude<keyof U, keyof T>]?: never })
52
- | (U & { [k in Exclude<keyof T, keyof U>]?: never });
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
- { errorMessage?: string },
56
- { fallbackValue?: T }
55
+ { errorMessage?: string },
56
+ { fallbackValue?: T }
57
57
  >;
58
58
 
59
- export const jsonParse = <T>(
60
- jsonString: string,
61
- options?: JSONParseOptions<T>
62
- ): T => {
63
- try {
64
- return JSON.parse(jsonString) as T;
65
- } catch (error) {
66
- if (options?.acceptJSObject) {
67
- try {
68
- const jsonStringCleaned = parseJSObject(jsonString);
69
- return jsonStringCleaned as T;
70
- } catch (e) {
71
- // Ignore this error and return the original error or the fallback value
72
- }
73
- }
74
- if (options?.fallbackValue !== undefined) {
75
- if (options.fallbackValue instanceof Function) {
76
- return options.fallbackValue();
77
- }
78
- return options.fallbackValue;
79
- } else if (options?.errorMessage) {
80
- // TODO REFACTOR: Use ApplicationError from offical @fsai-flow/workflow repo
81
- throw new Error(options.errorMessage);
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
- replaceCircularRefs?: boolean;
86
+ replaceCircularRefs?: boolean;
90
87
  };
91
88
 
92
- export const replaceCircularReferences = <T>(
93
- value: T,
94
- knownObjects = new WeakSet()
95
- ): T => {
96
- if (typeof value !== "object" || value === null || value instanceof RegExp)
97
- return value;
98
- if ("toJSON" in value && typeof value.toJSON === "function")
99
- return value.toJSON() as T;
100
- if (knownObjects.has(value)) return "[Circular Reference]" as T;
101
- knownObjects.add(value);
102
- const copy = (Array.isArray(value) ? [] : {}) as T;
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
- obj: unknown,
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
- switch (expression?.type) {
121
- case Syntax.ObjectExpression:
122
- return Object.fromEntries(
123
- expression.properties
124
- .filter((prop) => prop.type === Syntax.Property)
125
- .map(({ key, value }) => [
126
- syntaxNodeToValue(key),
127
- syntaxNodeToValue(value),
128
- ])
129
- );
130
- case Syntax.Identifier:
131
- return expression.name;
132
- case Syntax.Literal:
133
- return expression.value;
134
- case Syntax.ArrayExpression:
135
- return expression.elements.map((exp) => syntaxNodeToValue(exp));
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
- const jsExpression = esprimaParse(`(${objectAsString})`).body.find(
148
- (node): node is ExpressionStatement =>
149
- node.type === Syntax.ExpressionStatement &&
150
- node.expression.type === Syntax.ObjectExpression
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
- return syntaxNodeToValue(jsExpression?.expression) as object;
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
- value: unknown,
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
+