@fsai-flow/workflow 0.0.2 → 0.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.
Files changed (136) hide show
  1. package/dist/README.md +31 -0
  2. package/dist/package.json +42 -0
  3. package/dist/src/index.d.ts +22 -0
  4. package/dist/src/index.d.ts.map +1 -0
  5. package/dist/src/index.js +68 -0
  6. package/dist/src/index.js.map +1 -0
  7. package/dist/src/lib/Constants.d.ts +69 -0
  8. package/dist/src/lib/Constants.d.ts.map +1 -0
  9. package/dist/src/lib/Constants.js +106 -0
  10. package/dist/src/lib/Constants.js.map +1 -0
  11. package/dist/src/lib/DeferredPromise.d.ts +7 -0
  12. package/dist/src/lib/DeferredPromise.d.ts.map +1 -0
  13. package/dist/src/lib/DeferredPromise.js +11 -0
  14. package/dist/src/lib/DeferredPromise.js.map +1 -0
  15. package/dist/src/lib/Expression.d.ts +66 -0
  16. package/dist/src/lib/Expression.d.ts.map +1 -0
  17. package/dist/src/lib/Expression.js +247 -0
  18. package/dist/src/lib/Expression.js.map +1 -0
  19. package/dist/src/lib/Interfaces.d.ts +1623 -0
  20. package/dist/src/lib/Interfaces.d.ts.map +1 -0
  21. package/dist/src/lib/Interfaces.js +44 -0
  22. package/dist/src/lib/Interfaces.js.map +1 -0
  23. package/dist/src/lib/LoggerProxy.d.ts +10 -0
  24. package/dist/src/lib/LoggerProxy.d.ts.map +1 -0
  25. package/dist/src/lib/LoggerProxy.js +40 -0
  26. package/dist/src/lib/LoggerProxy.js.map +1 -0
  27. package/dist/src/lib/MetadataUtils.d.ts +5 -0
  28. package/dist/src/lib/MetadataUtils.d.ts.map +1 -0
  29. package/dist/src/lib/MetadataUtils.js +27 -0
  30. package/dist/src/lib/MetadataUtils.js.map +1 -0
  31. package/dist/src/lib/NodeErrors.d.ts +83 -0
  32. package/dist/src/lib/NodeErrors.d.ts.map +1 -0
  33. package/dist/src/lib/NodeErrors.js +284 -0
  34. package/dist/src/lib/NodeErrors.js.map +1 -0
  35. package/dist/src/lib/NodeHelpers.d.ts +199 -0
  36. package/dist/src/lib/NodeHelpers.d.ts.map +1 -0
  37. package/dist/src/lib/NodeHelpers.js +1335 -0
  38. package/dist/src/lib/NodeHelpers.js.map +1 -0
  39. package/dist/src/lib/ObservableObject.d.ts +6 -0
  40. package/dist/src/lib/ObservableObject.d.ts.map +1 -0
  41. package/dist/src/lib/ObservableObject.js +61 -0
  42. package/dist/src/lib/ObservableObject.js.map +1 -0
  43. package/dist/src/lib/RoutingNode.d.ts +24 -0
  44. package/dist/src/lib/RoutingNode.d.ts.map +1 -0
  45. package/dist/src/lib/RoutingNode.js +528 -0
  46. package/dist/src/lib/RoutingNode.js.map +1 -0
  47. package/dist/src/lib/TelemetryHelpers.d.ts +4 -0
  48. package/dist/src/lib/TelemetryHelpers.d.ts.map +1 -0
  49. package/dist/src/lib/TelemetryHelpers.js +67 -0
  50. package/dist/src/lib/TelemetryHelpers.js.map +1 -0
  51. package/dist/src/lib/TypeValidation.d.ts +22 -0
  52. package/dist/src/lib/TypeValidation.d.ts.map +1 -0
  53. package/dist/src/lib/TypeValidation.js +376 -0
  54. package/dist/src/lib/TypeValidation.js.map +1 -0
  55. package/dist/src/lib/VersionedNodeType.d.ts +10 -0
  56. package/dist/src/lib/VersionedNodeType.d.ts.map +1 -0
  57. package/dist/src/lib/VersionedNodeType.js +24 -0
  58. package/dist/src/lib/VersionedNodeType.js.map +1 -0
  59. package/dist/src/lib/Workflow.d.ts +249 -0
  60. package/dist/src/lib/Workflow.d.ts.map +1 -0
  61. package/dist/src/lib/Workflow.js +899 -0
  62. package/dist/src/lib/Workflow.js.map +1 -0
  63. package/dist/src/lib/WorkflowDataProxy.d.ts +88 -0
  64. package/dist/src/lib/WorkflowDataProxy.d.ts.map +1 -0
  65. package/dist/src/lib/WorkflowDataProxy.js +583 -0
  66. package/dist/src/lib/WorkflowDataProxy.js.map +1 -0
  67. package/dist/src/lib/WorkflowErrors.d.ts +10 -0
  68. package/dist/src/lib/WorkflowErrors.d.ts.map +1 -0
  69. package/dist/src/lib/WorkflowErrors.js +18 -0
  70. package/dist/src/lib/WorkflowErrors.js.map +1 -0
  71. package/dist/src/lib/WorkflowHooks.d.ts +12 -0
  72. package/dist/src/lib/WorkflowHooks.d.ts.map +1 -0
  73. package/dist/src/lib/WorkflowHooks.js +32 -0
  74. package/dist/src/lib/WorkflowHooks.js.map +1 -0
  75. package/dist/src/lib/errors/base/base.error.d.ts +30 -0
  76. package/dist/src/lib/errors/base/base.error.d.ts.map +1 -0
  77. package/dist/src/lib/errors/base/base.error.js +47 -0
  78. package/dist/src/lib/errors/base/base.error.js.map +1 -0
  79. package/dist/src/lib/errors/base/operational.error.d.ts +16 -0
  80. package/dist/src/lib/errors/base/operational.error.d.ts.map +1 -0
  81. package/dist/src/lib/errors/base/operational.error.js +19 -0
  82. package/dist/src/lib/errors/base/operational.error.js.map +1 -0
  83. package/dist/src/lib/errors/error.types.d.ts +14 -0
  84. package/dist/src/lib/errors/error.types.d.ts.map +1 -0
  85. package/dist/src/lib/errors/error.types.js +3 -0
  86. package/dist/src/lib/errors/error.types.js.map +1 -0
  87. package/dist/src/lib/errors/index.d.ts +2 -0
  88. package/dist/src/lib/errors/index.d.ts.map +1 -0
  89. package/dist/src/lib/errors/index.js +6 -0
  90. package/dist/src/lib/errors/index.js.map +1 -0
  91. package/dist/src/lib/result.d.ts +20 -0
  92. package/dist/src/lib/result.d.ts.map +1 -0
  93. package/dist/src/lib/result.js +36 -0
  94. package/dist/src/lib/result.js.map +1 -0
  95. package/dist/src/lib/utils.d.ts +51 -0
  96. package/dist/src/lib/utils.d.ts.map +1 -0
  97. package/dist/src/lib/utils.js +119 -0
  98. package/dist/src/lib/utils.js.map +1 -0
  99. package/package.json +49 -35
  100. package/.eslintrc.json +0 -33
  101. package/eslint.config.js +0 -19
  102. package/jest.config.ts +0 -10
  103. package/project.json +0 -19
  104. package/src/index.ts +0 -33
  105. package/src/lib/Constants.ts +0 -124
  106. package/src/lib/DeferredPromise.ts +0 -14
  107. package/src/lib/Expression.ts +0 -375
  108. package/src/lib/Interfaces.ts +0 -2262
  109. package/src/lib/LoggerProxy.ts +0 -43
  110. package/src/lib/MetadataUtils.ts +0 -34
  111. package/src/lib/NodeErrors.ts +0 -332
  112. package/src/lib/NodeHelpers.ts +0 -1666
  113. package/src/lib/ObservableObject.ts +0 -77
  114. package/src/lib/RoutingNode.ts +0 -862
  115. package/src/lib/TelemetryHelpers.ts +0 -86
  116. package/src/lib/TypeValidation.ts +0 -431
  117. package/src/lib/VersionedNodeType.ts +0 -30
  118. package/src/lib/Workflow.ts +0 -1270
  119. package/src/lib/WorkflowDataProxy.ts +0 -708
  120. package/src/lib/WorkflowErrors.ts +0 -18
  121. package/src/lib/WorkflowHooks.ts +0 -51
  122. package/src/lib/errors/base/base.error.ts +0 -68
  123. package/src/lib/errors/base/operational.error.ts +0 -21
  124. package/src/lib/errors/error.types.ts +0 -14
  125. package/src/lib/errors/index.ts +0 -1
  126. package/src/lib/result.ts +0 -34
  127. package/src/lib/utils.ts +0 -166
  128. package/tests/Helpers.ts +0 -667
  129. package/tests/NodeHelpers.test.ts +0 -3053
  130. package/tests/ObservableObject.test.ts +0 -171
  131. package/tests/RoutingNode.test.ts +0 -1680
  132. package/tests/Workflow.test.ts +0 -1284
  133. package/tests/WorkflowDataProxy.test.ts +0 -199
  134. package/tsconfig.json +0 -27
  135. package/tsconfig.lib.json +0 -11
  136. package/tsconfig.spec.json +0 -14
@@ -1,51 +0,0 @@
1
- // eslint-disable-next-line import/no-cycle
2
- import {
3
- IWorkflowBase,
4
- IWorkflowExecuteHooks,
5
- IWorkflowHooksOptionalParameters,
6
- WorkflowExecuteMode,
7
- } from './Interfaces';
8
-
9
- export class WorkflowHooks {
10
- mode: WorkflowExecuteMode;
11
-
12
- workflowData: IWorkflowBase;
13
-
14
- executionId: string;
15
-
16
- sessionId?: string;
17
-
18
- retryOf?: string;
19
-
20
- hookFunctions: IWorkflowExecuteHooks;
21
-
22
- constructor(
23
- hookFunctions: IWorkflowExecuteHooks,
24
- mode: WorkflowExecuteMode,
25
- executionId: string,
26
- workflowData: IWorkflowBase,
27
- optionalParameters?: IWorkflowHooksOptionalParameters,
28
- ) {
29
- // eslint-disable-next-line no-param-reassign, @typescript-eslint/prefer-nullish-coalescing
30
- optionalParameters = optionalParameters || {};
31
-
32
- this.hookFunctions = hookFunctions;
33
- this.mode = mode;
34
- this.executionId = executionId;
35
- this.workflowData = workflowData;
36
- this.sessionId = optionalParameters.sessionId;
37
- this.retryOf = optionalParameters.retryOf;
38
- }
39
-
40
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
41
- async executeHookFunctions(hookName: string, parameters: any[]) {
42
- // tslint:disable-line:no-any
43
- if (this.hookFunctions[hookName] !== undefined && Array.isArray(this.hookFunctions[hookName])) {
44
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, no-restricted-syntax
45
- for (const hookFunction of this.hookFunctions[hookName]!) {
46
- // eslint-disable-next-line no-await-in-loop
47
- await hookFunction.apply(this, parameters);
48
- }
49
- }
50
- }
51
- }
@@ -1,68 +0,0 @@
1
- import type { Event } from '@sentry/node';
2
- import callsites from 'callsites';
3
-
4
- import type { ErrorTags, ErrorLevel, ReportingOptions } from '../error.types';
5
-
6
- export type BaseErrorOptions = { description?: undefined | null } & ErrorOptions & ReportingOptions;
7
-
8
- interface ErrorOptions {
9
- cause?: unknown;
10
- }
11
-
12
- /**
13
- * Base class for all errors
14
- */
15
- export abstract class BaseError extends Error {
16
- /**
17
- * Error level. Defines which level the error should be logged/reported
18
- * @default 'error'
19
- */
20
- level: ErrorLevel;
21
-
22
- /**
23
- * Whether the error should be reported to Sentry.
24
- * @default true
25
- */
26
- readonly shouldReport: boolean;
27
-
28
- readonly description: string | null | undefined;
29
-
30
- readonly tags: ErrorTags;
31
-
32
- readonly extra?: Event['extra'];
33
-
34
- readonly packageName?: string;
35
-
36
- readonly cause?: unknown;
37
-
38
- constructor(
39
- message: string,
40
- {
41
- level = 'error',
42
- description,
43
- shouldReport,
44
- tags = {},
45
- extra,
46
- ...rest
47
- }: BaseErrorOptions = {},
48
- ) {
49
- super(message);
50
-
51
- if (rest.cause !== undefined) {
52
- this.cause = rest.cause;
53
- }
54
-
55
- this.level = level;
56
- this.shouldReport = shouldReport ?? (level === 'error' || level === 'fatal');
57
- this.description = description;
58
- this.tags = tags;
59
- this.extra = extra;
60
-
61
- try {
62
- const filePath = callsites()[2].getFileName() ?? '';
63
- const match = /packages\/([^\/]+)\//.exec(filePath)?.[1];
64
-
65
- if (match) this.tags['packageName'] = match;
66
- } catch {}
67
- }
68
- }
@@ -1,21 +0,0 @@
1
- import type { BaseErrorOptions } from './base.error';
2
- import { BaseError } from './base.error';
3
-
4
- export type OperationalErrorOptions = Omit<BaseErrorOptions, 'level'> & {
5
- level?: 'info' | 'warning' | 'error';
6
- };
7
-
8
- /**
9
- * Error that indicates a transient issue, like a network request failing,
10
- * a database query timing out, etc. These are expected to happen, are
11
- * transient by nature and should be handled gracefully.
12
- *
13
- * Default level: warning
14
- */
15
- export class OperationalError extends BaseError {
16
- constructor(message: string, opts: OperationalErrorOptions = {}) {
17
- opts.level = opts.level ?? 'warning';
18
-
19
- super(message, opts);
20
- }
21
- }
@@ -1,14 +0,0 @@
1
- import type { Event } from '@sentry/node';
2
-
3
- export type ErrorLevel = 'fatal' | 'error' | 'warning' | 'info';
4
-
5
- export type ErrorTags = NonNullable<Event['tags']>;
6
-
7
- export type ReportingOptions = {
8
- /** Whether the error should be reported to Sentry */
9
- shouldReport?: boolean;
10
- level?: ErrorLevel;
11
- tags?: ErrorTags;
12
- extra?: Event['extra'];
13
- executionId?: string;
14
- };
@@ -1 +0,0 @@
1
- export { OperationalError, type OperationalErrorOptions } from './base/operational.error';
package/src/lib/result.ts DELETED
@@ -1,34 +0,0 @@
1
- export type ResultOk<T> = { ok: true; result: T };
2
- export type ResultError<E> = { ok: false; error: E };
3
- export type Result<T, E> = ResultOk<T> | ResultError<E>;
4
-
5
- export const createResultOk = <T>(data: T): ResultOk<T> => ({
6
- ok: true,
7
- result: data,
8
- });
9
-
10
- export const createResultError = <E = unknown>(error: E): ResultError<E> => ({
11
- ok: false,
12
- error,
13
- });
14
-
15
- export function ensureError(error: unknown): Error {
16
- return error instanceof Error
17
- ? error
18
- : new Error('Error that was not an instance of Error was thrown, cause: ' + error);
19
- }
20
-
21
- /**
22
- * Executes the given function and converts it to a Result object.
23
- *
24
- * @example
25
- * const result = toResult(() => fs.writeFileSync('file.txt', 'Hello, World!'));
26
- */
27
- export const toResult = <T, E extends Error = Error>(fn: () => T): Result<T, E> => {
28
- try {
29
- return createResultOk<T>(fn());
30
- } catch (e) {
31
- const error = ensureError(e);
32
- return createResultError<E>(error as E);
33
- }
34
- };
package/src/lib/utils.ts DELETED
@@ -1,166 +0,0 @@
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";
9
-
10
- export function updateDisplayOptions(
11
- displayOptions: IDisplayOptions,
12
- properties: INodeProperties[]
13
- ) {
14
- return properties.map((nodeProperty) => {
15
- return {
16
- ...nodeProperty,
17
- displayOptions: merge({}, nodeProperty.displayOptions, displayOptions),
18
- };
19
- });
20
- }
21
- export function randomInt(max: number): number;
22
- export function randomInt(min: number, max: number): number;
23
- /**
24
- * Generates a cryptographically secure random integer within a specified range.
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).
29
- */
30
- export function randomInt(min: number, max?: number): number {
31
- if (max === undefined) {
32
- max = min;
33
- min = 0;
34
- }
35
-
36
- if (max <= min) {
37
- throw new Error("max must be greater than min");
38
- }
39
-
40
- const range = max - min;
41
- const maxUnbiased = Math.floor(0xffffffff / range) * range;
42
-
43
- let rand: number;
44
- do {
45
- rand = crypto.getRandomValues(new Uint32Array(1))[0];
46
- } while (rand >= maxUnbiased);
47
-
48
- return min + (rand % range);
49
- }
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 });
53
-
54
- type JSONParseOptions<T> = { acceptJSObject?: boolean } & MutuallyExclusive<
55
- { errorMessage?: string },
56
- { fallbackValue?: T }
57
- >;
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
- }
86
- };
87
-
88
- type JSONStringifyOptions = {
89
- replaceCircularRefs?: boolean;
90
- };
91
-
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;
108
- };
109
-
110
- export const jsonStringify = (
111
- obj: unknown,
112
- options: JSONStringifyOptions = {}
113
- ): string => {
114
- return JSON.stringify(
115
- options?.replaceCircularRefs ? replaceCircularReferences(obj) : obj
116
- );
117
- };
118
-
119
- 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
- }
139
- }
140
-
141
- /**
142
- * Parse any JavaScript ObjectExpression, including:
143
- * - single quoted keys
144
- * - unquoted keys
145
- */
146
- 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
- );
152
-
153
- return syntaxNodeToValue(jsExpression?.expression) as object;
154
- }
155
-
156
- /**
157
- * Checks if a value is an object with a specific key and provides a type guard for the key.
158
- */
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
- );
166
- }