flowcraft 2.1.1 → 2.3.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/README.md +41 -35
- package/dist/analysis.d.ts +5 -2
- package/dist/analysis.js +1 -1
- package/dist/{chunk-HN72TZY5.js → chunk-233SESC2.js} +34 -23
- package/dist/chunk-233SESC2.js.map +1 -0
- package/dist/chunk-33NO4PUJ.js +74 -0
- package/dist/chunk-33NO4PUJ.js.map +1 -0
- package/dist/chunk-5KKSQWSC.js +90 -0
- package/dist/chunk-5KKSQWSC.js.map +1 -0
- package/dist/{chunk-O3XD45IL.js → chunk-6INWPSZT.js} +44 -17
- package/dist/chunk-6INWPSZT.js.map +1 -0
- package/dist/chunk-BC4G7OM6.js +42 -0
- package/dist/chunk-BC4G7OM6.js.map +1 -0
- package/dist/chunk-BCRWXTWX.js +21 -0
- package/dist/chunk-BCRWXTWX.js.map +1 -0
- package/dist/chunk-C4HYIJI3.js +279 -0
- package/dist/chunk-C4HYIJI3.js.map +1 -0
- package/dist/chunk-CD3Q4N6V.js +13 -0
- package/dist/chunk-CD3Q4N6V.js.map +1 -0
- package/dist/chunk-CD4FUZOJ.js +114 -0
- package/dist/chunk-CD4FUZOJ.js.map +1 -0
- package/dist/chunk-CY755I7I.js +25 -0
- package/dist/chunk-CY755I7I.js.map +1 -0
- package/dist/chunk-DL7KVYZF.js +39 -0
- package/dist/chunk-DL7KVYZF.js.map +1 -0
- package/dist/{chunk-UETC63DP.js → chunk-EUJWJWFA.js} +24 -4
- package/dist/chunk-EUJWJWFA.js.map +1 -0
- package/dist/chunk-FRKO3WX4.js +32 -0
- package/dist/chunk-FRKO3WX4.js.map +1 -0
- package/dist/chunk-G53CSLBF.js +54 -0
- package/dist/chunk-G53CSLBF.js.map +1 -0
- package/dist/chunk-G5BGBPFP.js +172 -0
- package/dist/chunk-G5BGBPFP.js.map +1 -0
- package/dist/chunk-HAZ26F3P.js +98 -0
- package/dist/chunk-HAZ26F3P.js.map +1 -0
- package/dist/chunk-IB2BISIC.js +446 -0
- package/dist/chunk-IB2BISIC.js.map +1 -0
- package/dist/{chunk-U5V5O5MN.js → chunk-LNK7LZER.js} +5 -3
- package/dist/chunk-LNK7LZER.js.map +1 -0
- package/dist/chunk-MCGK3FXQ.js +143 -0
- package/dist/chunk-MCGK3FXQ.js.map +1 -0
- package/dist/chunk-MUYLRTSR.js +82 -0
- package/dist/chunk-MUYLRTSR.js.map +1 -0
- package/dist/chunk-NVJ3ZO3P.js +3 -0
- package/dist/{chunk-HMR2GEGE.js.map → chunk-NVJ3ZO3P.js.map} +1 -1
- package/dist/chunk-NVLZFLYM.js +3 -0
- package/dist/chunk-NVLZFLYM.js.map +1 -0
- package/dist/chunk-ONH7PIJZ.js +300 -0
- package/dist/chunk-ONH7PIJZ.js.map +1 -0
- package/dist/chunk-QNYXQKFW.js +25 -0
- package/dist/chunk-QNYXQKFW.js.map +1 -0
- package/dist/{chunk-KWQHFT7E.js → chunk-R3HQXIEL.js} +6 -6
- package/dist/chunk-R3HQXIEL.js.map +1 -0
- package/dist/chunk-RM677CNU.js +52 -0
- package/dist/chunk-RM677CNU.js.map +1 -0
- package/dist/chunk-WWGFIYKW.js +47 -0
- package/dist/chunk-WWGFIYKW.js.map +1 -0
- package/dist/chunk-XNRIM27H.js +76 -0
- package/dist/chunk-XNRIM27H.js.map +1 -0
- package/dist/container-factory.d.ts +17 -0
- package/dist/container-factory.js +13 -0
- package/dist/container-factory.js.map +1 -0
- package/dist/container.d.ts +23 -0
- package/dist/container.js +3 -0
- package/dist/container.js.map +1 -0
- package/dist/context.d.ts +3 -1
- package/dist/context.js +1 -1
- package/dist/errors.d.ts +18 -17
- package/dist/errors.js +1 -1
- package/dist/evaluator.d.ts +3 -1
- package/dist/flow.d.ts +29 -8
- package/dist/flow.js +2 -2
- package/dist/index.d.ts +7 -7
- package/dist/index.js +27 -15
- package/dist/linter.d.ts +5 -3
- package/dist/linter.js +2 -2
- package/dist/logger.d.ts +3 -1
- package/dist/node.d.ts +3 -1
- package/dist/node.js +1 -1
- package/dist/nodes/batch-gather.d.ts +9 -0
- package/dist/nodes/batch-gather.js +4 -0
- package/dist/nodes/batch-gather.js.map +1 -0
- package/dist/nodes/batch-scatter.d.ts +9 -0
- package/dist/nodes/batch-scatter.js +4 -0
- package/dist/nodes/batch-scatter.js.map +1 -0
- package/dist/nodes/subflow.d.ts +9 -0
- package/dist/nodes/subflow.js +10 -0
- package/dist/nodes/subflow.js.map +1 -0
- package/dist/nodes/wait.d.ts +9 -0
- package/dist/nodes/wait.js +4 -0
- package/dist/nodes/wait.js.map +1 -0
- package/dist/runtime/adapter.d.ts +6 -5
- package/dist/runtime/adapter.js +20 -10
- package/dist/runtime/builtin-keys.d.ts +38 -0
- package/dist/runtime/builtin-keys.js +10 -0
- package/dist/runtime/builtin-keys.js.map +1 -0
- package/dist/runtime/execution-context.d.ts +3 -0
- package/dist/runtime/execution-context.js +6 -0
- package/dist/runtime/execution-context.js.map +1 -0
- package/dist/runtime/executors.d.ts +3 -26
- package/dist/runtime/executors.js +2 -2
- package/dist/runtime/index.d.ts +5 -6
- package/dist/runtime/index.js +22 -11
- package/dist/runtime/node-executor-factory.d.ts +12 -0
- package/dist/runtime/node-executor-factory.js +6 -0
- package/dist/runtime/node-executor-factory.js.map +1 -0
- package/dist/runtime/orchestrator.d.ts +9 -0
- package/dist/runtime/orchestrator.js +8 -0
- package/dist/runtime/orchestrator.js.map +1 -0
- package/dist/runtime/orchestrators/step-by-step.d.ts +16 -0
- package/dist/runtime/orchestrators/step-by-step.js +5 -0
- package/dist/runtime/orchestrators/step-by-step.js.map +1 -0
- package/dist/runtime/orchestrators/utils.d.ts +35 -0
- package/dist/runtime/orchestrators/utils.js +4 -0
- package/dist/runtime/orchestrators/utils.js.map +1 -0
- package/dist/runtime/runtime.d.ts +3 -35
- package/dist/runtime/runtime.js +19 -9
- package/dist/runtime/state.d.ts +3 -21
- package/dist/runtime/state.js +3 -2
- package/dist/runtime/traverser.d.ts +3 -26
- package/dist/runtime/traverser.js +2 -3
- package/dist/runtime/types.d.ts +3 -16
- package/dist/runtime/types.js +1 -1
- package/dist/runtime/workflow-logic-handler.d.ts +17 -0
- package/dist/runtime/workflow-logic-handler.js +5 -0
- package/dist/runtime/workflow-logic-handler.js.map +1 -0
- package/dist/sanitizer.d.ts +3 -1
- package/dist/serializer.d.ts +3 -1
- package/dist/testing/event-logger.d.ts +63 -0
- package/dist/testing/event-logger.js +3 -0
- package/dist/testing/event-logger.js.map +1 -0
- package/dist/testing/index.d.ts +6 -0
- package/dist/testing/index.js +31 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/run-with-trace.d.ts +38 -0
- package/dist/testing/run-with-trace.js +29 -0
- package/dist/testing/run-with-trace.js.map +1 -0
- package/dist/testing/stepper.d.ts +79 -0
- package/dist/testing/stepper.js +11 -0
- package/dist/testing/stepper.js.map +1 -0
- package/dist/types-ezHUBdpL.d.ts +564 -0
- package/dist/types.d.ts +3 -1
- package/package.json +6 -2
- package/dist/chunk-3XVVR2SR.js +0 -417
- package/dist/chunk-3XVVR2SR.js.map +0 -1
- package/dist/chunk-4A627Q6L.js +0 -147
- package/dist/chunk-4A627Q6L.js.map +0 -1
- package/dist/chunk-5ZXV3R5D.js +0 -28
- package/dist/chunk-5ZXV3R5D.js.map +0 -1
- package/dist/chunk-CSZ6EOWG.js +0 -61
- package/dist/chunk-CSZ6EOWG.js.map +0 -1
- package/dist/chunk-HMR2GEGE.js +0 -3
- package/dist/chunk-HN72TZY5.js.map +0 -1
- package/dist/chunk-KWQHFT7E.js.map +0 -1
- package/dist/chunk-M2FRTT2K.js +0 -144
- package/dist/chunk-M2FRTT2K.js.map +0 -1
- package/dist/chunk-NBIRTKZ7.js +0 -192
- package/dist/chunk-NBIRTKZ7.js.map +0 -1
- package/dist/chunk-O3XD45IL.js.map +0 -1
- package/dist/chunk-U5V5O5MN.js.map +0 -1
- package/dist/chunk-UETC63DP.js.map +0 -1
- package/dist/types-CQCe_nBM.d.ts +0 -214
package/dist/chunk-3XVVR2SR.js
DELETED
|
@@ -1,417 +0,0 @@
|
|
|
1
|
-
import { WorkflowState } from './chunk-CSZ6EOWG.js';
|
|
2
|
-
import { GraphTraverser } from './chunk-NBIRTKZ7.js';
|
|
3
|
-
import { sanitizeBlueprint } from './chunk-DSYAC4WB.js';
|
|
4
|
-
import { JsonSerializer } from './chunk-CYHZ2YVH.js';
|
|
5
|
-
import { BuiltInNodeExecutor, ClassNodeExecutor, FunctionNodeExecutor } from './chunk-M2FRTT2K.js';
|
|
6
|
-
import { AsyncContextView } from './chunk-KWQHFT7E.js';
|
|
7
|
-
import { CancelledWorkflowError, NodeExecutionError, FatalNodeExecutionError } from './chunk-5ZXV3R5D.js';
|
|
8
|
-
import { PropertyEvaluator } from './chunk-PH2IYZHV.js';
|
|
9
|
-
import { isNodeClass } from './chunk-U5V5O5MN.js';
|
|
10
|
-
import { analyzeBlueprint } from './chunk-HN72TZY5.js';
|
|
11
|
-
import { NullLogger } from './chunk-4PELJWF7.js';
|
|
12
|
-
|
|
13
|
-
// src/runtime/runtime.ts
|
|
14
|
-
var FlowRuntime = class {
|
|
15
|
-
registry;
|
|
16
|
-
blueprints;
|
|
17
|
-
dependencies;
|
|
18
|
-
logger;
|
|
19
|
-
eventBus;
|
|
20
|
-
serializer;
|
|
21
|
-
middleware;
|
|
22
|
-
evaluator;
|
|
23
|
-
analysisCache;
|
|
24
|
-
options;
|
|
25
|
-
constructor(options) {
|
|
26
|
-
this.registry = options.registry || {};
|
|
27
|
-
this.blueprints = options.blueprints || {};
|
|
28
|
-
this.dependencies = options.dependencies || {};
|
|
29
|
-
this.logger = options.logger || new NullLogger();
|
|
30
|
-
this.eventBus = options.eventBus || { emit: () => {
|
|
31
|
-
} };
|
|
32
|
-
this.serializer = options.serializer || new JsonSerializer();
|
|
33
|
-
this.middleware = options.middleware || [];
|
|
34
|
-
this.evaluator = options.evaluator || new PropertyEvaluator();
|
|
35
|
-
this.analysisCache = /* @__PURE__ */ new WeakMap();
|
|
36
|
-
this.options = options;
|
|
37
|
-
}
|
|
38
|
-
async run(blueprint, initialState = {}, options) {
|
|
39
|
-
const executionId = globalThis.crypto?.randomUUID();
|
|
40
|
-
const startTime = Date.now();
|
|
41
|
-
const contextData = typeof initialState === "string" ? this.serializer.deserialize(initialState) : initialState;
|
|
42
|
-
blueprint = sanitizeBlueprint(blueprint);
|
|
43
|
-
const state = new WorkflowState(contextData);
|
|
44
|
-
this.logger.info(`Starting workflow execution`, {
|
|
45
|
-
blueprintId: blueprint.id,
|
|
46
|
-
executionId
|
|
47
|
-
});
|
|
48
|
-
try {
|
|
49
|
-
await this.eventBus.emit("workflow:start", {
|
|
50
|
-
blueprintId: blueprint.id,
|
|
51
|
-
executionId
|
|
52
|
-
});
|
|
53
|
-
const analysis = this.analysisCache.get(blueprint) ?? (() => {
|
|
54
|
-
const computed = analyzeBlueprint(blueprint);
|
|
55
|
-
this.analysisCache.set(blueprint, computed);
|
|
56
|
-
return computed;
|
|
57
|
-
})();
|
|
58
|
-
if (options?.strict && !analysis.isDag) {
|
|
59
|
-
throw new Error(`Workflow '${blueprint.id}' failed strictness check: Cycles are not allowed.`);
|
|
60
|
-
}
|
|
61
|
-
if (!analysis.isDag) {
|
|
62
|
-
this.logger.warn(`Workflow contains cycles`, {
|
|
63
|
-
blueprintId: blueprint.id
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
const traverser = new GraphTraverser(
|
|
67
|
-
blueprint,
|
|
68
|
-
this,
|
|
69
|
-
state,
|
|
70
|
-
options?.functionRegistry,
|
|
71
|
-
executionId,
|
|
72
|
-
options?.signal,
|
|
73
|
-
options?.concurrency
|
|
74
|
-
);
|
|
75
|
-
await traverser.traverse();
|
|
76
|
-
const status = state.getStatus(traverser.getAllNodeIds(), traverser.getFallbackNodeIds());
|
|
77
|
-
const result = state.toResult(this.serializer);
|
|
78
|
-
result.status = status;
|
|
79
|
-
const duration = Date.now() - startTime;
|
|
80
|
-
if (status === "stalled") {
|
|
81
|
-
await this.eventBus.emit("workflow:stall", {
|
|
82
|
-
blueprintId: blueprint.id,
|
|
83
|
-
executionId,
|
|
84
|
-
remainingNodes: traverser.getAllNodeIds().size - state.getCompletedNodes().size
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
this.logger.info(`Workflow execution completed`, {
|
|
88
|
-
blueprintId: blueprint.id,
|
|
89
|
-
executionId,
|
|
90
|
-
status,
|
|
91
|
-
duration,
|
|
92
|
-
errors: result.errors?.length || 0
|
|
93
|
-
});
|
|
94
|
-
await this.eventBus.emit("workflow:finish", {
|
|
95
|
-
blueprintId: blueprint.id,
|
|
96
|
-
executionId,
|
|
97
|
-
status,
|
|
98
|
-
errors: result.errors
|
|
99
|
-
});
|
|
100
|
-
return result;
|
|
101
|
-
} catch (error) {
|
|
102
|
-
const duration = Date.now() - startTime;
|
|
103
|
-
if (error instanceof DOMException ? error.name === "AbortError" : error instanceof CancelledWorkflowError) {
|
|
104
|
-
this.logger.info(`Workflow execution cancelled`, {
|
|
105
|
-
blueprintId: blueprint.id,
|
|
106
|
-
executionId,
|
|
107
|
-
duration
|
|
108
|
-
});
|
|
109
|
-
await this.eventBus.emit("workflow:finish", {
|
|
110
|
-
blueprintId: blueprint.id,
|
|
111
|
-
executionId,
|
|
112
|
-
status: "cancelled",
|
|
113
|
-
error
|
|
114
|
-
});
|
|
115
|
-
return {
|
|
116
|
-
context: {},
|
|
117
|
-
serializedContext: "{}",
|
|
118
|
-
status: "cancelled"
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
this.logger.error(`Workflow execution failed`, {
|
|
122
|
-
blueprintId: blueprint.id,
|
|
123
|
-
executionId,
|
|
124
|
-
duration,
|
|
125
|
-
error: error instanceof Error ? error.message : String(error)
|
|
126
|
-
});
|
|
127
|
-
throw error;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
async executeNode(blueprint, nodeId, state, allPredecessors, functionRegistry, executionId, signal) {
|
|
131
|
-
const nodeDef = blueprint.nodes.find((n) => n.id === nodeId);
|
|
132
|
-
if (!nodeDef) {
|
|
133
|
-
throw new NodeExecutionError(
|
|
134
|
-
`Node '${nodeId}' not found in blueprint.`,
|
|
135
|
-
nodeId,
|
|
136
|
-
blueprint.id,
|
|
137
|
-
void 0,
|
|
138
|
-
executionId
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
const contextImpl = state.getContext();
|
|
142
|
-
const asyncContext = contextImpl.type === "sync" ? new AsyncContextView(contextImpl) : contextImpl;
|
|
143
|
-
const nodeContext = {
|
|
144
|
-
context: asyncContext,
|
|
145
|
-
input: await this._resolveNodeInput(nodeDef, asyncContext, allPredecessors),
|
|
146
|
-
params: nodeDef.params || {},
|
|
147
|
-
dependencies: { ...this.dependencies, logger: this.logger },
|
|
148
|
-
signal
|
|
149
|
-
};
|
|
150
|
-
const beforeHooks = this.middleware.map((m) => m.beforeNode).filter((hook) => !!hook);
|
|
151
|
-
const afterHooks = this.middleware.map((m) => m.afterNode).filter((hook) => !!hook);
|
|
152
|
-
const aroundHooks = this.middleware.map((m) => m.aroundNode).filter((hook) => !!hook);
|
|
153
|
-
const executor = this.getExecutor(nodeDef, functionRegistry);
|
|
154
|
-
const coreExecution = async () => {
|
|
155
|
-
let result;
|
|
156
|
-
let error;
|
|
157
|
-
try {
|
|
158
|
-
for (const hook of beforeHooks) await hook(nodeContext.context, nodeId);
|
|
159
|
-
result = await this.executeWithFallback(
|
|
160
|
-
blueprint,
|
|
161
|
-
nodeDef,
|
|
162
|
-
nodeContext,
|
|
163
|
-
executor,
|
|
164
|
-
executionId,
|
|
165
|
-
signal,
|
|
166
|
-
state,
|
|
167
|
-
functionRegistry
|
|
168
|
-
);
|
|
169
|
-
return result;
|
|
170
|
-
} catch (e) {
|
|
171
|
-
error = e;
|
|
172
|
-
throw e;
|
|
173
|
-
} finally {
|
|
174
|
-
for (const hook of afterHooks) await hook(nodeContext.context, nodeId, result, error);
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
let executionChain = coreExecution;
|
|
178
|
-
for (let i = aroundHooks.length - 1; i >= 0; i--) {
|
|
179
|
-
const hook = aroundHooks[i];
|
|
180
|
-
const next = executionChain;
|
|
181
|
-
executionChain = () => hook(nodeContext.context, nodeId, next);
|
|
182
|
-
}
|
|
183
|
-
try {
|
|
184
|
-
await this.eventBus.emit("node:start", {
|
|
185
|
-
blueprintId: blueprint.id,
|
|
186
|
-
nodeId,
|
|
187
|
-
executionId
|
|
188
|
-
});
|
|
189
|
-
const result = await executionChain();
|
|
190
|
-
await this.eventBus.emit("node:finish", {
|
|
191
|
-
blueprintId: blueprint.id,
|
|
192
|
-
nodeId,
|
|
193
|
-
result,
|
|
194
|
-
executionId
|
|
195
|
-
});
|
|
196
|
-
return result;
|
|
197
|
-
} catch (error) {
|
|
198
|
-
await this.eventBus.emit("node:error", {
|
|
199
|
-
blueprintId: blueprint.id,
|
|
200
|
-
nodeId,
|
|
201
|
-
error,
|
|
202
|
-
executionId
|
|
203
|
-
});
|
|
204
|
-
if (error instanceof DOMException && error.name === "AbortError") {
|
|
205
|
-
throw new CancelledWorkflowError("Workflow cancelled");
|
|
206
|
-
}
|
|
207
|
-
throw error instanceof NodeExecutionError ? error : new NodeExecutionError(`Node '${nodeId}' failed execution.`, nodeId, blueprint.id, error, executionId);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
getExecutor(nodeDef, functionRegistry) {
|
|
211
|
-
if (nodeDef.uses.startsWith("batch-") || nodeDef.uses.startsWith("loop-") || nodeDef.uses === "subflow") {
|
|
212
|
-
return new BuiltInNodeExecutor((nodeDef2, context) => this._executeBuiltInNode(nodeDef2, context));
|
|
213
|
-
}
|
|
214
|
-
const implementation = functionRegistry?.get(nodeDef.uses) || this.registry[nodeDef.uses];
|
|
215
|
-
if (!implementation) {
|
|
216
|
-
throw new FatalNodeExecutionError(
|
|
217
|
-
`Implementation for '${nodeDef.uses}' not found for node '${nodeDef.id}'.`,
|
|
218
|
-
nodeDef.id,
|
|
219
|
-
""
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
const maxRetries = nodeDef.config?.maxRetries ?? 1;
|
|
223
|
-
return isNodeClass(implementation) ? new ClassNodeExecutor(implementation, maxRetries, this.eventBus) : new FunctionNodeExecutor(implementation, maxRetries, this.eventBus);
|
|
224
|
-
}
|
|
225
|
-
async executeWithFallback(blueprint, nodeDef, context, executor, executionId, signal, state, functionRegistry) {
|
|
226
|
-
try {
|
|
227
|
-
return await executor.execute(nodeDef, context, executionId, signal);
|
|
228
|
-
} catch (error) {
|
|
229
|
-
const isFatal = error instanceof FatalNodeExecutionError || error instanceof NodeExecutionError && error.originalError instanceof FatalNodeExecutionError;
|
|
230
|
-
if (isFatal) throw error;
|
|
231
|
-
const fallbackNodeId = nodeDef.config?.fallback;
|
|
232
|
-
if (fallbackNodeId && state) {
|
|
233
|
-
context.dependencies.logger.warn(`Executing fallback for node`, {
|
|
234
|
-
nodeId: nodeDef.id,
|
|
235
|
-
fallbackNodeId,
|
|
236
|
-
error: error instanceof Error ? error.message : String(error),
|
|
237
|
-
executionId
|
|
238
|
-
});
|
|
239
|
-
await this.eventBus.emit("node:fallback", {
|
|
240
|
-
blueprintId: blueprint.id,
|
|
241
|
-
nodeId: nodeDef.id,
|
|
242
|
-
executionId,
|
|
243
|
-
fallback: fallbackNodeId
|
|
244
|
-
});
|
|
245
|
-
const fallbackNode = blueprint.nodes.find((n) => n.id === fallbackNodeId);
|
|
246
|
-
if (!fallbackNode) {
|
|
247
|
-
throw new NodeExecutionError(
|
|
248
|
-
`Fallback node '${fallbackNodeId}' not found in blueprint.`,
|
|
249
|
-
nodeDef.id,
|
|
250
|
-
blueprint.id,
|
|
251
|
-
void 0,
|
|
252
|
-
executionId
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
const fallbackExecutor = this.getExecutor(fallbackNode, functionRegistry);
|
|
256
|
-
const fallbackResult = await fallbackExecutor.execute(fallbackNode, context, executionId, signal);
|
|
257
|
-
state.markFallbackExecuted();
|
|
258
|
-
state.addCompletedNode(fallbackNodeId, fallbackResult.output);
|
|
259
|
-
context.dependencies.logger.info(`Fallback execution completed`, {
|
|
260
|
-
nodeId: nodeDef.id,
|
|
261
|
-
fallbackNodeId,
|
|
262
|
-
executionId
|
|
263
|
-
});
|
|
264
|
-
return { ...fallbackResult, _fallbackExecuted: true };
|
|
265
|
-
}
|
|
266
|
-
throw error;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
async determineNextNodes(blueprint, nodeId, result, context) {
|
|
270
|
-
const outgoingEdges = blueprint.edges.filter((edge) => edge.source === nodeId);
|
|
271
|
-
const matched = [];
|
|
272
|
-
const evaluateEdge = async (edge) => {
|
|
273
|
-
if (!edge.condition) return true;
|
|
274
|
-
const contextData = context.type === "sync" ? context.toJSON() : await context.toJSON();
|
|
275
|
-
return !!this.evaluator.evaluate(edge.condition, {
|
|
276
|
-
...contextData,
|
|
277
|
-
result
|
|
278
|
-
});
|
|
279
|
-
};
|
|
280
|
-
if (result.action) {
|
|
281
|
-
const actionEdges = outgoingEdges.filter((edge) => edge.action === result.action);
|
|
282
|
-
for (const edge of actionEdges) {
|
|
283
|
-
if (await evaluateEdge(edge)) {
|
|
284
|
-
const targetNode = blueprint.nodes.find((n) => n.id === edge.target);
|
|
285
|
-
if (targetNode) matched.push({ node: targetNode, edge });
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
if (matched.length === 0) {
|
|
290
|
-
const defaultEdges = outgoingEdges.filter((edge) => !edge.action);
|
|
291
|
-
for (const edge of defaultEdges) {
|
|
292
|
-
if (await evaluateEdge(edge)) {
|
|
293
|
-
const targetNode = blueprint.nodes.find((n) => n.id === edge.target);
|
|
294
|
-
if (targetNode) matched.push({ node: targetNode, edge });
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
this.logger.debug(`Determined next nodes for ${nodeId}`, {
|
|
299
|
-
matchedNodes: matched.map((m) => m.node.id),
|
|
300
|
-
action: result.action
|
|
301
|
-
});
|
|
302
|
-
return matched;
|
|
303
|
-
}
|
|
304
|
-
async applyEdgeTransform(edge, sourceResult, targetNode, context, allPredecessors) {
|
|
305
|
-
const asyncContext = context.type === "sync" ? new AsyncContextView(context) : context;
|
|
306
|
-
const finalInput = edge.transform ? this.evaluator.evaluate(edge.transform, {
|
|
307
|
-
input: sourceResult.output,
|
|
308
|
-
context: await asyncContext.toJSON()
|
|
309
|
-
}) : sourceResult.output;
|
|
310
|
-
const inputKey = `${targetNode.id}_input`;
|
|
311
|
-
await asyncContext.set(inputKey, finalInput);
|
|
312
|
-
if (targetNode.config?.joinStrategy === "any") {
|
|
313
|
-
targetNode.inputs = inputKey;
|
|
314
|
-
} else if (!targetNode.inputs) {
|
|
315
|
-
const predecessors = allPredecessors?.get(targetNode.id);
|
|
316
|
-
if (!predecessors || predecessors.size === 1) {
|
|
317
|
-
targetNode.inputs = inputKey;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
async _resolveNodeInput(nodeDef, context, allPredecessors) {
|
|
322
|
-
if (nodeDef.inputs) {
|
|
323
|
-
if (typeof nodeDef.inputs === "string") return await context.get(nodeDef.inputs);
|
|
324
|
-
if (typeof nodeDef.inputs === "object") {
|
|
325
|
-
const input = {};
|
|
326
|
-
for (const key in nodeDef.inputs) {
|
|
327
|
-
const contextKey = nodeDef.inputs[key];
|
|
328
|
-
input[key] = await context.get(contextKey);
|
|
329
|
-
}
|
|
330
|
-
return input;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
if (allPredecessors) {
|
|
334
|
-
const predecessors = allPredecessors.get(nodeDef.id);
|
|
335
|
-
if (predecessors && predecessors.size === 1) {
|
|
336
|
-
const singlePredecessorId = predecessors.values().next().value;
|
|
337
|
-
return await context.get(singlePredecessorId);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
return void 0;
|
|
341
|
-
}
|
|
342
|
-
async _executeBuiltInNode(nodeDef, contextImpl) {
|
|
343
|
-
const context = contextImpl.type === "sync" ? new AsyncContextView(contextImpl) : contextImpl;
|
|
344
|
-
const { params = {}, id, inputs } = nodeDef;
|
|
345
|
-
switch (nodeDef.uses) {
|
|
346
|
-
case "batch-scatter": {
|
|
347
|
-
const inputArray = await context.get(inputs) || [];
|
|
348
|
-
if (!Array.isArray(inputArray))
|
|
349
|
-
throw new FatalNodeExecutionError(`Input for batch-scatter node '${id}' must be an array.`, id, "");
|
|
350
|
-
const batchId = globalThis.crypto.randomUUID();
|
|
351
|
-
const dynamicNodes = [];
|
|
352
|
-
for (let i = 0; i < inputArray.length; i++) {
|
|
353
|
-
const item = inputArray[i];
|
|
354
|
-
const itemInputKey = `${id}_${batchId}_item_${i}`;
|
|
355
|
-
await context.set(itemInputKey, item);
|
|
356
|
-
dynamicNodes.push({
|
|
357
|
-
id: `${params.workerUsesKey}_${batchId}_${i}`,
|
|
358
|
-
uses: params.workerUsesKey,
|
|
359
|
-
inputs: itemInputKey
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
const gatherNodeId = params.gatherNodeId;
|
|
363
|
-
return { dynamicNodes, output: { gatherNodeId } };
|
|
364
|
-
}
|
|
365
|
-
case "batch-gather": {
|
|
366
|
-
return { output: {} };
|
|
367
|
-
}
|
|
368
|
-
case "loop-controller": {
|
|
369
|
-
const contextData = await context.toJSON();
|
|
370
|
-
const shouldContinue = !!this.evaluator.evaluate(params.condition, contextData);
|
|
371
|
-
return { action: shouldContinue ? "continue" : "break" };
|
|
372
|
-
}
|
|
373
|
-
case "subflow": {
|
|
374
|
-
const { blueprintId, inputs: inputMapping, outputs: outputMapping } = params;
|
|
375
|
-
if (!blueprintId)
|
|
376
|
-
throw new FatalNodeExecutionError(`Subflow node '${id}' is missing the 'blueprintId' parameter.`, id, "");
|
|
377
|
-
const subBlueprint = this.blueprints[blueprintId];
|
|
378
|
-
if (!subBlueprint)
|
|
379
|
-
throw new FatalNodeExecutionError(
|
|
380
|
-
`Sub-blueprint with ID '${blueprintId}' not found in runtime registry.`,
|
|
381
|
-
id,
|
|
382
|
-
""
|
|
383
|
-
);
|
|
384
|
-
const subflowInitialContext = {};
|
|
385
|
-
if (inputMapping) {
|
|
386
|
-
for (const [targetKey, sourceKey] of Object.entries(inputMapping)) {
|
|
387
|
-
if (await context.has(sourceKey)) {
|
|
388
|
-
subflowInitialContext[targetKey] = await context.get(sourceKey);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
const subflowResult = await this.run(subBlueprint, subflowInitialContext);
|
|
393
|
-
if (subflowResult.status !== "completed")
|
|
394
|
-
throw new NodeExecutionError(
|
|
395
|
-
`Sub-workflow '${blueprintId}' did not complete successfully. Status: ${subflowResult.status}`,
|
|
396
|
-
id,
|
|
397
|
-
subBlueprint.id
|
|
398
|
-
);
|
|
399
|
-
if (outputMapping) {
|
|
400
|
-
for (const [parentKey, subKey] of Object.entries(outputMapping)) {
|
|
401
|
-
const subflowFinalContext = subflowResult.context;
|
|
402
|
-
if (Object.hasOwn(subflowFinalContext, subKey)) {
|
|
403
|
-
await context.set(parentKey, subflowFinalContext[subKey]);
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
return { output: subflowResult.context };
|
|
408
|
-
}
|
|
409
|
-
default:
|
|
410
|
-
throw new FatalNodeExecutionError(`Unknown built-in node type: '${nodeDef.uses}'`, id, "");
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
export { FlowRuntime };
|
|
416
|
-
//# sourceMappingURL=chunk-3XVVR2SR.js.map
|
|
417
|
-
//# sourceMappingURL=chunk-3XVVR2SR.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/runtime/runtime.ts"],"names":["nodeDef"],"mappings":";;;;;;;;;;;;;AAmCO,IAAM,cAAN,MAEP;AAAA,EACQ,Q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AA;AAC1C,YAAA,IAAI,MAAA,CAAO,MAAA,CAAO,mBAAA,EAAqB,MAAgB,CAAA,EAAG;AACzD,cAAA,MAAM,OAAA,CAAQ,GAAA,CAAI,SAAA,EAAkB,mBAAA,CAAoB,MAAgB,CAAC,CAAA;AAAA,YAC1E;AAAA,UACD;AAAA,QACD;AAEA,QAAA,OAAO,EAAE,MAAA,EAAQ,aAAA,CAAc,OAAA,EAAQ;AAAA,MACxC;AAAA,MACA;AACC,QAAA,MAAM,IAAI,uBAAA,CAAwB,CAAA,6BAAA,EAAgC,QAAQ,IAAI,CAAA,CAAA,CAAA,EAAK,IAAI,EAAE,CAAA;AAAA;AAC3F,EACD;AACD","file":"chunk-3XVVR2SR.js","sourcesContent":["import type { BlueprintAnalysis } from '../analysis'\nimport { analyzeBlueprint } from '../analysis'\nimport { AsyncContextView } from '../context'\nimport { CancelledWorkflowError, FatalNodeExecutionError, NodeExecutionError } from '../errors'\nimport { PropertyEvaluator } from '../evaluator'\nimport { NullLogger } from '../logger'\nimport type { BaseNode } from '../node'\nimport { isNodeClass } from '../node'\nimport { sanitizeBlueprint } from '../sanitizer'\nimport { JsonSerializer } from '../serializer'\nimport type {\n\tContextImplementation,\n\tEdgeDefinition,\n\tIAsyncContext,\n\tIEvaluator,\n\tIEventBus,\n\tILogger,\n\tISerializer,\n\tISyncContext,\n\tMiddleware,\n\tNodeClass,\n\tNodeContext,\n\tNodeDefinition,\n\tNodeFunction,\n\tNodeResult,\n\tRuntimeOptions,\n\tWorkflowBlueprint,\n\tWorkflowResult,\n} from '../types'\nimport type { ExecutionStrategy } from './executors'\nimport { BuiltInNodeExecutor, ClassNodeExecutor, FunctionNodeExecutor } from './executors'\nimport { WorkflowState } from './state'\nimport { GraphTraverser } from './traverser'\nimport type { IRuntime } from './types'\n\nexport class FlowRuntime<TContext extends Record<string, any>, TDependencies extends Record<string, any>>\n\timplements IRuntime<TContext, TDependencies>\n{\n\tpublic registry: Record<string, NodeFunction | NodeClass | typeof BaseNode>\n\tprivate blueprints: Record<string, WorkflowBlueprint>\n\tprivate dependencies: TDependencies\n\tprivate logger: ILogger\n\tprivate eventBus: IEventBus\n\tprivate serializer: ISerializer\n\tprivate middleware: Middleware[]\n\tprivate evaluator: IEvaluator\n\tprivate analysisCache: WeakMap<WorkflowBlueprint, BlueprintAnalysis>\n\tpublic options: RuntimeOptions<TDependencies>\n\n\tconstructor(options: RuntimeOptions<TDependencies>) {\n\t\tthis.registry = options.registry || {}\n\t\tthis.blueprints = options.blueprints || {}\n\t\tthis.dependencies = options.dependencies || ({} as TDependencies)\n\t\tthis.logger = options.logger || new NullLogger()\n\t\tthis.eventBus = options.eventBus || { emit: () => {} }\n\t\tthis.serializer = options.serializer || new JsonSerializer()\n\t\tthis.middleware = options.middleware || []\n\t\tthis.evaluator = options.evaluator || new PropertyEvaluator()\n\t\tthis.analysisCache = new WeakMap()\n\t\tthis.options = options\n\t}\n\n\tasync run(\n\t\tblueprint: WorkflowBlueprint,\n\t\tinitialState: Partial<TContext> | string = {},\n\t\toptions?: {\n\t\t\tfunctionRegistry?: Map<string, any>\n\t\t\tstrict?: boolean\n\t\t\tsignal?: AbortSignal\n\t\t\tconcurrency?: number\n\t\t},\n\t): Promise<WorkflowResult<TContext>> {\n\t\tconst executionId = globalThis.crypto?.randomUUID()\n\t\tconst startTime = Date.now()\n\t\tconst contextData =\n\t\t\ttypeof initialState === 'string' ? (this.serializer.deserialize(initialState) as Partial<TContext>) : initialState\n\t\tblueprint = sanitizeBlueprint(blueprint)\n\t\tconst state = new WorkflowState<TContext>(contextData)\n\n\t\tthis.logger.info(`Starting workflow execution`, {\n\t\t\tblueprintId: blueprint.id,\n\t\t\texecutionId,\n\t\t})\n\n\t\ttry {\n\t\t\tawait this.eventBus.emit('workflow:start', {\n\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\texecutionId,\n\t\t\t})\n\t\t\tconst analysis =\n\t\t\t\tthis.analysisCache.get(blueprint) ??\n\t\t\t\t(() => {\n\t\t\t\t\tconst computed = analyzeBlueprint(blueprint)\n\t\t\t\t\tthis.analysisCache.set(blueprint, computed)\n\t\t\t\t\treturn computed\n\t\t\t\t})()\n\t\t\tif (options?.strict && !analysis.isDag) {\n\t\t\t\tthrow new Error(`Workflow '${blueprint.id}' failed strictness check: Cycles are not allowed.`)\n\t\t\t}\n\t\t\tif (!analysis.isDag) {\n\t\t\t\tthis.logger.warn(`Workflow contains cycles`, {\n\t\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\t})\n\t\t\t}\n\t\t\tconst traverser = new GraphTraverser<TContext, TDependencies>(\n\t\t\t\tblueprint,\n\t\t\t\tthis,\n\t\t\t\tstate,\n\t\t\t\toptions?.functionRegistry,\n\t\t\t\texecutionId,\n\t\t\t\toptions?.signal,\n\t\t\t\toptions?.concurrency,\n\t\t\t)\n\t\t\tawait traverser.traverse()\n\t\t\tconst status = state.getStatus(traverser.getAllNodeIds(), traverser.getFallbackNodeIds())\n\t\t\tconst result = state.toResult(this.serializer)\n\t\t\tresult.status = status\n\t\t\tconst duration = Date.now() - startTime\n\t\t\tif (status === 'stalled') {\n\t\t\t\tawait this.eventBus.emit('workflow:stall', {\n\t\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\t\texecutionId,\n\t\t\t\t\tremainingNodes: traverser.getAllNodeIds().size - state.getCompletedNodes().size,\n\t\t\t\t})\n\t\t\t}\n\t\t\tthis.logger.info(`Workflow execution completed`, {\n\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\texecutionId,\n\t\t\t\tstatus,\n\t\t\t\tduration,\n\t\t\t\terrors: result.errors?.length || 0,\n\t\t\t})\n\t\t\tawait this.eventBus.emit('workflow:finish', {\n\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\texecutionId,\n\t\t\t\tstatus,\n\t\t\t\terrors: result.errors,\n\t\t\t})\n\t\t\treturn result\n\t\t} catch (error) {\n\t\t\tconst duration = Date.now() - startTime\n\t\t\tif (error instanceof DOMException ? error.name === 'AbortError' : error instanceof CancelledWorkflowError) {\n\t\t\t\tthis.logger.info(`Workflow execution cancelled`, {\n\t\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\t\texecutionId,\n\t\t\t\t\tduration,\n\t\t\t\t})\n\t\t\t\tawait this.eventBus.emit('workflow:finish', {\n\t\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\t\texecutionId,\n\t\t\t\t\tstatus: 'cancelled',\n\t\t\t\t\terror,\n\t\t\t\t})\n\t\t\t\treturn {\n\t\t\t\t\tcontext: {} as TContext,\n\t\t\t\t\tserializedContext: '{}',\n\t\t\t\t\tstatus: 'cancelled',\n\t\t\t\t}\n\t\t\t}\n\t\t\tthis.logger.error(`Workflow execution failed`, {\n\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\texecutionId,\n\t\t\t\tduration,\n\t\t\t\terror: error instanceof Error ? error.message : String(error),\n\t\t\t})\n\t\t\tthrow error\n\t\t}\n\t}\n\n\tasync executeNode(\n\t\tblueprint: WorkflowBlueprint,\n\t\tnodeId: string,\n\t\tstate: WorkflowState<TContext>,\n\t\tallPredecessors?: Map<string, Set<string>>,\n\t\tfunctionRegistry?: Map<string, any>,\n\t\texecutionId?: string,\n\t\tsignal?: AbortSignal,\n\t): Promise<NodeResult<any, any>> {\n\t\tconst nodeDef = blueprint.nodes.find((n) => n.id === nodeId)\n\t\tif (!nodeDef) {\n\t\t\tthrow new NodeExecutionError(\n\t\t\t\t`Node '${nodeId}' not found in blueprint.`,\n\t\t\t\tnodeId,\n\t\t\t\tblueprint.id,\n\t\t\t\tundefined,\n\t\t\t\texecutionId,\n\t\t\t)\n\t\t}\n\n\t\tconst contextImpl = state.getContext()\n\t\tconst asyncContext: IAsyncContext<TContext> =\n\t\t\tcontextImpl.type === 'sync'\n\t\t\t\t? new AsyncContextView(contextImpl as ISyncContext<TContext>)\n\t\t\t\t: (contextImpl as IAsyncContext<TContext>)\n\t\tconst nodeContext: NodeContext<TContext, TDependencies, any> = {\n\t\t\tcontext: asyncContext,\n\t\t\tinput: await this._resolveNodeInput(nodeDef, asyncContext, allPredecessors),\n\t\t\tparams: nodeDef.params || {},\n\t\t\tdependencies: { ...this.dependencies, logger: this.logger },\n\t\t\tsignal,\n\t\t}\n\n\t\tconst beforeHooks = this.middleware\n\t\t\t.map((m) => m.beforeNode)\n\t\t\t.filter((hook): hook is NonNullable<Middleware['beforeNode']> => !!hook)\n\t\tconst afterHooks = this.middleware\n\t\t\t.map((m) => m.afterNode)\n\t\t\t.filter((hook): hook is NonNullable<Middleware['afterNode']> => !!hook)\n\t\tconst aroundHooks = this.middleware\n\t\t\t.map((m) => m.aroundNode)\n\t\t\t.filter((hook): hook is NonNullable<Middleware['aroundNode']> => !!hook)\n\n\t\tconst executor = this.getExecutor(nodeDef, functionRegistry)\n\t\tconst coreExecution = async (): Promise<NodeResult> => {\n\t\t\tlet result: NodeResult | undefined\n\t\t\tlet error: Error | undefined\n\t\t\ttry {\n\t\t\t\tfor (const hook of beforeHooks) await hook(nodeContext.context, nodeId)\n\t\t\t\tresult = await this.executeWithFallback(\n\t\t\t\t\tblueprint,\n\t\t\t\t\tnodeDef,\n\t\t\t\t\tnodeContext,\n\t\t\t\t\texecutor,\n\t\t\t\t\texecutionId,\n\t\t\t\t\tsignal,\n\t\t\t\t\tstate,\n\t\t\t\t\tfunctionRegistry,\n\t\t\t\t)\n\t\t\t\treturn result\n\t\t\t} catch (e: any) {\n\t\t\t\terror = e\n\t\t\t\tthrow e\n\t\t\t} finally {\n\t\t\t\tfor (const hook of afterHooks) await hook(nodeContext.context, nodeId, result, error)\n\t\t\t}\n\t\t}\n\n\t\tlet executionChain: () => Promise<NodeResult> = coreExecution\n\t\tfor (let i = aroundHooks.length - 1; i >= 0; i--) {\n\t\t\tconst hook = aroundHooks[i]\n\t\t\tconst next = executionChain\n\t\t\texecutionChain = () => hook(nodeContext.context, nodeId, next)\n\t\t}\n\n\t\ttry {\n\t\t\tawait this.eventBus.emit('node:start', {\n\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\tnodeId,\n\t\t\t\texecutionId,\n\t\t\t})\n\t\t\tconst result = await executionChain()\n\t\t\tawait this.eventBus.emit('node:finish', {\n\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\tnodeId,\n\t\t\t\tresult,\n\t\t\t\texecutionId,\n\t\t\t})\n\t\t\treturn result\n\t\t} catch (error: any) {\n\t\t\tawait this.eventBus.emit('node:error', {\n\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\tnodeId,\n\t\t\t\terror,\n\t\t\t\texecutionId,\n\t\t\t})\n\t\t\tif (error instanceof DOMException && error.name === 'AbortError') {\n\t\t\t\tthrow new CancelledWorkflowError('Workflow cancelled')\n\t\t\t}\n\t\t\tthrow error instanceof NodeExecutionError\n\t\t\t\t? error\n\t\t\t\t: new NodeExecutionError(`Node '${nodeId}' failed execution.`, nodeId, blueprint.id, error, executionId)\n\t\t}\n\t}\n\n\tprivate getExecutor(nodeDef: NodeDefinition, functionRegistry?: Map<string, any>): ExecutionStrategy {\n\t\tif (nodeDef.uses.startsWith('batch-') || nodeDef.uses.startsWith('loop-') || nodeDef.uses === 'subflow') {\n\t\t\treturn new BuiltInNodeExecutor((nodeDef, context) => this._executeBuiltInNode(nodeDef, context))\n\t\t}\n\t\tconst implementation = functionRegistry?.get(nodeDef.uses) || this.registry[nodeDef.uses]\n\t\tif (!implementation) {\n\t\t\tthrow new FatalNodeExecutionError(\n\t\t\t\t`Implementation for '${nodeDef.uses}' not found for node '${nodeDef.id}'.`,\n\t\t\t\tnodeDef.id,\n\t\t\t\t'',\n\t\t\t)\n\t\t}\n\t\tconst maxRetries = nodeDef.config?.maxRetries ?? 1\n\t\treturn isNodeClass(implementation)\n\t\t\t? new ClassNodeExecutor(implementation, maxRetries, this.eventBus)\n\t\t\t: new FunctionNodeExecutor(implementation, maxRetries, this.eventBus)\n\t}\n\n\tprivate async executeWithFallback(\n\t\tblueprint: WorkflowBlueprint,\n\t\tnodeDef: NodeDefinition,\n\t\tcontext: NodeContext<TContext, TDependencies, any>,\n\t\texecutor: ExecutionStrategy,\n\t\texecutionId?: string,\n\t\tsignal?: AbortSignal,\n\t\tstate?: WorkflowState<TContext>,\n\t\tfunctionRegistry?: Map<string, any>,\n\t): Promise<NodeResult<any, any>> {\n\t\ttry {\n\t\t\treturn await executor.execute(nodeDef, context, executionId, signal)\n\t\t} catch (error) {\n\t\t\tconst isFatal =\n\t\t\t\terror instanceof FatalNodeExecutionError ||\n\t\t\t\t(error instanceof NodeExecutionError && error.originalError instanceof FatalNodeExecutionError)\n\t\t\tif (isFatal) throw error\n\t\t\tconst fallbackNodeId = nodeDef.config?.fallback\n\t\t\tif (fallbackNodeId && state) {\n\t\t\t\tcontext.dependencies.logger.warn(`Executing fallback for node`, {\n\t\t\t\t\tnodeId: nodeDef.id,\n\t\t\t\t\tfallbackNodeId,\n\t\t\t\t\terror: error instanceof Error ? error.message : String(error),\n\t\t\t\t\texecutionId,\n\t\t\t\t})\n\t\t\t\tawait this.eventBus.emit('node:fallback', {\n\t\t\t\t\tblueprintId: blueprint.id,\n\t\t\t\t\tnodeId: nodeDef.id,\n\t\t\t\t\texecutionId,\n\t\t\t\t\tfallback: fallbackNodeId,\n\t\t\t\t})\n\t\t\t\tconst fallbackNode = blueprint.nodes.find((n: NodeDefinition) => n.id === fallbackNodeId)\n\t\t\t\tif (!fallbackNode) {\n\t\t\t\t\tthrow new NodeExecutionError(\n\t\t\t\t\t\t`Fallback node '${fallbackNodeId}' not found in blueprint.`,\n\t\t\t\t\t\tnodeDef.id,\n\t\t\t\t\t\tblueprint.id,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\texecutionId,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst fallbackExecutor = this.getExecutor(fallbackNode, functionRegistry)\n\t\t\t\tconst fallbackResult = await fallbackExecutor.execute(fallbackNode, context, executionId, signal)\n\t\t\t\tstate.markFallbackExecuted()\n\t\t\t\tstate.addCompletedNode(fallbackNodeId, fallbackResult.output)\n\t\t\t\tcontext.dependencies.logger.info(`Fallback execution completed`, {\n\t\t\t\t\tnodeId: nodeDef.id,\n\t\t\t\t\tfallbackNodeId,\n\t\t\t\t\texecutionId,\n\t\t\t\t})\n\t\t\t\treturn { ...fallbackResult, _fallbackExecuted: true }\n\t\t\t}\n\t\t\tthrow error\n\t\t}\n\t}\n\n\tasync determineNextNodes(\n\t\tblueprint: WorkflowBlueprint,\n\t\tnodeId: string,\n\t\tresult: NodeResult<any, any>,\n\t\tcontext: ContextImplementation<TContext>,\n\t): Promise<{ node: NodeDefinition; edge: EdgeDefinition }[]> {\n\t\tconst outgoingEdges = blueprint.edges.filter((edge) => edge.source === nodeId)\n\t\tconst matched: { node: NodeDefinition; edge: EdgeDefinition }[] = []\n\t\tconst evaluateEdge = async (edge: EdgeDefinition): Promise<boolean> => {\n\t\t\tif (!edge.condition) return true\n\t\t\tconst contextData = context.type === 'sync' ? context.toJSON() : await context.toJSON()\n\t\t\treturn !!this.evaluator.evaluate(edge.condition, {\n\t\t\t\t...contextData,\n\t\t\t\tresult,\n\t\t\t})\n\t\t}\n\t\tif (result.action) {\n\t\t\tconst actionEdges = outgoingEdges.filter((edge) => edge.action === result.action)\n\t\t\tfor (const edge of actionEdges) {\n\t\t\t\tif (await evaluateEdge(edge)) {\n\t\t\t\t\tconst targetNode = blueprint.nodes.find((n) => n.id === edge.target)\n\t\t\t\t\tif (targetNode) matched.push({ node: targetNode, edge })\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (matched.length === 0) {\n\t\t\tconst defaultEdges = outgoingEdges.filter((edge) => !edge.action)\n\t\t\tfor (const edge of defaultEdges) {\n\t\t\t\tif (await evaluateEdge(edge)) {\n\t\t\t\t\tconst targetNode = blueprint.nodes.find((n) => n.id === edge.target)\n\t\t\t\t\tif (targetNode) matched.push({ node: targetNode, edge })\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.logger.debug(`Determined next nodes for ${nodeId}`, {\n\t\t\tmatchedNodes: matched.map((m) => m.node.id),\n\t\t\taction: result.action,\n\t\t})\n\t\treturn matched\n\t}\n\n\tpublic async applyEdgeTransform(\n\t\tedge: EdgeDefinition,\n\t\tsourceResult: NodeResult<any, any>,\n\t\ttargetNode: NodeDefinition,\n\t\tcontext: ContextImplementation<TContext>,\n\t\tallPredecessors?: Map<string, Set<string>>,\n\t): Promise<void> {\n\t\tconst asyncContext = context.type === 'sync' ? new AsyncContextView(context) : context\n\t\tconst finalInput = edge.transform\n\t\t\t? this.evaluator.evaluate(edge.transform, {\n\t\t\t\t\tinput: sourceResult.output,\n\t\t\t\t\tcontext: await asyncContext.toJSON(),\n\t\t\t\t})\n\t\t\t: sourceResult.output\n\t\tconst inputKey = `${targetNode.id}_input`\n\t\tawait asyncContext.set(inputKey as any, finalInput)\n\t\tif (targetNode.config?.joinStrategy === 'any') {\n\t\t\ttargetNode.inputs = inputKey\n\t\t} else if (!targetNode.inputs) {\n\t\t\tconst predecessors = allPredecessors?.get(targetNode.id)\n\t\t\tif (!predecessors || predecessors.size === 1) {\n\t\t\t\ttargetNode.inputs = inputKey\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async _resolveNodeInput(\n\t\tnodeDef: NodeDefinition,\n\t\tcontext: IAsyncContext<TContext>,\n\t\tallPredecessors?: Map<string, Set<string>>,\n\t): Promise<any> {\n\t\tif (nodeDef.inputs) {\n\t\t\tif (typeof nodeDef.inputs === 'string') return await context.get(nodeDef.inputs as any)\n\t\t\tif (typeof nodeDef.inputs === 'object') {\n\t\t\t\tconst input: Record<string, any> = {}\n\t\t\t\tfor (const key in nodeDef.inputs) {\n\t\t\t\t\tconst contextKey = nodeDef.inputs[key]\n\t\t\t\t\tinput[key] = await context.get(contextKey as any)\n\t\t\t\t}\n\t\t\t\treturn input\n\t\t\t}\n\t\t}\n\t\tif (allPredecessors) {\n\t\t\tconst predecessors = allPredecessors.get(nodeDef.id)\n\t\t\tif (predecessors && predecessors.size === 1) {\n\t\t\t\tconst singlePredecessorId = predecessors.values().next().value\n\t\t\t\treturn await context.get(singlePredecessorId as any)\n\t\t\t}\n\t\t}\n\t\treturn undefined\n\t}\n\n\tprotected async _executeBuiltInNode(\n\t\tnodeDef: NodeDefinition,\n\t\tcontextImpl: ContextImplementation<TContext>,\n\t): Promise<NodeResult<any, any>> {\n\t\tconst context = contextImpl.type === 'sync' ? new AsyncContextView(contextImpl) : contextImpl\n\t\tconst { params = {}, id, inputs } = nodeDef\n\t\tswitch (nodeDef.uses) {\n\t\t\tcase 'batch-scatter': {\n\t\t\t\tconst inputArray = (await context.get(inputs as any)) || []\n\t\t\t\tif (!Array.isArray(inputArray))\n\t\t\t\t\tthrow new FatalNodeExecutionError(`Input for batch-scatter node '${id}' must be an array.`, id, '')\n\t\t\t\tconst batchId = globalThis.crypto.randomUUID()\n\t\t\t\tconst dynamicNodes: NodeDefinition[] = []\n\t\t\t\tfor (let i = 0; i < inputArray.length; i++) {\n\t\t\t\t\tconst item = inputArray[i]\n\t\t\t\t\tconst itemInputKey = `${id}_${batchId}_item_${i}`\n\t\t\t\t\tawait context.set(itemInputKey as any, item)\n\t\t\t\t\tdynamicNodes.push({\n\t\t\t\t\t\tid: `${params.workerUsesKey}_${batchId}_${i}`,\n\t\t\t\t\t\tuses: params.workerUsesKey,\n\t\t\t\t\t\tinputs: itemInputKey,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tconst gatherNodeId = params.gatherNodeId\n\t\t\t\treturn { dynamicNodes, output: { gatherNodeId } }\n\t\t\t}\n\t\t\tcase 'batch-gather': {\n\t\t\t\treturn { output: {} }\n\t\t\t}\n\t\t\tcase 'loop-controller': {\n\t\t\t\tconst contextData = await context.toJSON()\n\t\t\t\tconst shouldContinue = !!this.evaluator.evaluate(params.condition, contextData)\n\t\t\t\treturn { action: shouldContinue ? 'continue' : 'break' }\n\t\t\t}\n\t\t\tcase 'subflow': {\n\t\t\t\tconst { blueprintId, inputs: inputMapping, outputs: outputMapping } = params\n\t\t\t\tif (!blueprintId)\n\t\t\t\t\tthrow new FatalNodeExecutionError(`Subflow node '${id}' is missing the 'blueprintId' parameter.`, id, '')\n\n\t\t\t\tconst subBlueprint = this.blueprints[blueprintId]\n\t\t\t\tif (!subBlueprint)\n\t\t\t\t\tthrow new FatalNodeExecutionError(\n\t\t\t\t\t\t`Sub-blueprint with ID '${blueprintId}' not found in runtime registry.`,\n\t\t\t\t\t\tid,\n\t\t\t\t\t\t'',\n\t\t\t\t\t)\n\n\t\t\t\tconst subflowInitialContext: Record<string, any> = {}\n\n\t\t\t\tif (inputMapping) {\n\t\t\t\t\tfor (const [targetKey, sourceKey] of Object.entries(inputMapping)) {\n\t\t\t\t\t\tif (await context.has(sourceKey as any)) {\n\t\t\t\t\t\t\tsubflowInitialContext[targetKey] = await context.get(sourceKey as any)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst subflowResult = await this.run(subBlueprint, subflowInitialContext as Partial<TContext>)\n\n\t\t\t\tif (subflowResult.status !== 'completed')\n\t\t\t\t\tthrow new NodeExecutionError(\n\t\t\t\t\t\t`Sub-workflow '${blueprintId}' did not complete successfully. Status: ${subflowResult.status}`,\n\t\t\t\t\t\tid,\n\t\t\t\t\t\tsubBlueprint.id,\n\t\t\t\t\t)\n\n\t\t\t\tif (outputMapping) {\n\t\t\t\t\tfor (const [parentKey, subKey] of Object.entries(outputMapping)) {\n\t\t\t\t\t\tconst subflowFinalContext = subflowResult.context as Record<string, any>\n\t\t\t\t\t\tif (Object.hasOwn(subflowFinalContext, subKey as string)) {\n\t\t\t\t\t\t\tawait context.set(parentKey as any, subflowFinalContext[subKey as string])\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn { output: subflowResult.context }\n\t\t\t}\n\t\t\tdefault:\n\t\t\t\tthrow new FatalNodeExecutionError(`Unknown built-in node type: '${nodeDef.uses}'`, id, '')\n\t\t}\n\t}\n}\n"]}
|
package/dist/chunk-4A627Q6L.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { isNodeClass } from './chunk-U5V5O5MN.js';
|
|
2
|
-
|
|
3
|
-
// src/flow.ts
|
|
4
|
-
function _hashFunction(fn) {
|
|
5
|
-
const source = fn.toString();
|
|
6
|
-
let hash = 0;
|
|
7
|
-
for (let i = 0; i < source.length; i++) {
|
|
8
|
-
const char = source.charCodeAt(i);
|
|
9
|
-
hash = (hash << 5) - hash + char;
|
|
10
|
-
hash = hash & hash;
|
|
11
|
-
}
|
|
12
|
-
return Math.abs(hash).toString(16);
|
|
13
|
-
}
|
|
14
|
-
var Flow = class {
|
|
15
|
-
blueprint;
|
|
16
|
-
functionRegistry;
|
|
17
|
-
loopControllerIds;
|
|
18
|
-
loopDefinitions;
|
|
19
|
-
constructor(id) {
|
|
20
|
-
this.blueprint = { id, nodes: [], edges: [] };
|
|
21
|
-
this.functionRegistry = /* @__PURE__ */ new Map();
|
|
22
|
-
this.loopControllerIds = /* @__PURE__ */ new Map();
|
|
23
|
-
this.loopDefinitions = [];
|
|
24
|
-
}
|
|
25
|
-
node(id, implementation, options) {
|
|
26
|
-
let usesKey;
|
|
27
|
-
if (isNodeClass(implementation)) {
|
|
28
|
-
usesKey = implementation.name && implementation.name !== "BaseNode" ? implementation.name : `class_${_hashFunction(implementation)}`;
|
|
29
|
-
this.functionRegistry.set(usesKey, implementation);
|
|
30
|
-
} else {
|
|
31
|
-
usesKey = `fn_${_hashFunction(implementation)}`;
|
|
32
|
-
this.functionRegistry.set(usesKey, implementation);
|
|
33
|
-
}
|
|
34
|
-
const nodeDef = { id, uses: usesKey, ...options };
|
|
35
|
-
this.blueprint.nodes?.push(nodeDef);
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
edge(source, target, options) {
|
|
39
|
-
const edgeDef = { source, target, ...options };
|
|
40
|
-
this.blueprint.edges?.push(edgeDef);
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Creates a batch processing pattern.
|
|
45
|
-
* It takes an input array, runs a worker node on each item in parallel, and gathers the results.
|
|
46
|
-
* @param id The base ID for this batch operation.
|
|
47
|
-
* @param worker The node implementation to run on each item.
|
|
48
|
-
* @param options Configuration for the batch operation.
|
|
49
|
-
* @param options.inputKey The key in the context that holds the input array for the batch.
|
|
50
|
-
* @param options.outputKey The key in the context where the array of results will be stored.
|
|
51
|
-
* @returns The Flow instance for chaining.
|
|
52
|
-
*/
|
|
53
|
-
batch(id, worker, options) {
|
|
54
|
-
const { inputKey, outputKey } = options;
|
|
55
|
-
const scatterId = `${id}_scatter`;
|
|
56
|
-
const gatherId = `${id}_gather`;
|
|
57
|
-
let workerUsesKey;
|
|
58
|
-
if (isNodeClass(worker)) {
|
|
59
|
-
workerUsesKey = worker.name && worker.name !== "BaseNode" ? worker.name : `class_batch_worker_${_hashFunction(worker)}`;
|
|
60
|
-
this.functionRegistry.set(workerUsesKey, worker);
|
|
61
|
-
} else {
|
|
62
|
-
workerUsesKey = `fn_batch_worker_${_hashFunction(worker)}`;
|
|
63
|
-
this.functionRegistry.set(workerUsesKey, worker);
|
|
64
|
-
}
|
|
65
|
-
this.blueprint.nodes?.push({
|
|
66
|
-
id: scatterId,
|
|
67
|
-
uses: "batch-scatter",
|
|
68
|
-
// built-in
|
|
69
|
-
inputs: inputKey,
|
|
70
|
-
params: { workerUsesKey, outputKey, gatherNodeId: gatherId }
|
|
71
|
-
});
|
|
72
|
-
this.blueprint.nodes?.push({
|
|
73
|
-
id: gatherId,
|
|
74
|
-
uses: "batch-gather",
|
|
75
|
-
// built-in
|
|
76
|
-
params: { outputKey },
|
|
77
|
-
config: { joinStrategy: "all" }
|
|
78
|
-
// important: must wait for all scattered jobs
|
|
79
|
-
});
|
|
80
|
-
this.edge(scatterId, gatherId);
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Creates a loop pattern in the workflow graph.
|
|
85
|
-
* @param id A unique identifier for the loop construct.
|
|
86
|
-
* @param options Defines the start, end, and continuation condition of the loop.
|
|
87
|
-
* @param options.startNodeId The ID of the first node inside the loop body.
|
|
88
|
-
* @param options.endNodeId The ID of the last node inside the loop body.
|
|
89
|
-
* @param options.condition An expression that, if true, causes the loop to run again.
|
|
90
|
-
*/
|
|
91
|
-
loop(id, options) {
|
|
92
|
-
const { startNodeId, endNodeId, condition } = options;
|
|
93
|
-
const controllerId = `${id}-loop`;
|
|
94
|
-
this.loopControllerIds.set(id, controllerId);
|
|
95
|
-
this.loopDefinitions.push({ id, startNodeId, endNodeId });
|
|
96
|
-
this.blueprint.nodes?.push({
|
|
97
|
-
id: controllerId,
|
|
98
|
-
uses: "loop-controller",
|
|
99
|
-
// built-in
|
|
100
|
-
params: { condition },
|
|
101
|
-
config: { joinStrategy: "any" }
|
|
102
|
-
// to allow re-execution on each loop iteration
|
|
103
|
-
});
|
|
104
|
-
this.edge(endNodeId, controllerId);
|
|
105
|
-
this.edge(controllerId, startNodeId, {
|
|
106
|
-
action: "continue",
|
|
107
|
-
transform: `context.${endNodeId}`
|
|
108
|
-
// pass the end node's value to the start node
|
|
109
|
-
});
|
|
110
|
-
return this;
|
|
111
|
-
}
|
|
112
|
-
getLoopControllerId(id) {
|
|
113
|
-
const controllerId = this.loopControllerIds.get(id);
|
|
114
|
-
if (!controllerId) {
|
|
115
|
-
throw new Error(`Loop with id '${id}' not found. Ensure you have defined it using the .loop() method.`);
|
|
116
|
-
}
|
|
117
|
-
return controllerId;
|
|
118
|
-
}
|
|
119
|
-
toBlueprint() {
|
|
120
|
-
if (!this.blueprint.nodes || this.blueprint.nodes.length === 0) {
|
|
121
|
-
throw new Error("Cannot build a blueprint with no nodes.");
|
|
122
|
-
}
|
|
123
|
-
for (const loopDef of this.loopDefinitions) {
|
|
124
|
-
const startNode = this.blueprint.nodes?.find((n) => n.id === loopDef.startNodeId);
|
|
125
|
-
const endNode = this.blueprint.nodes?.find((n) => n.id === loopDef.endNodeId);
|
|
126
|
-
if (!startNode) {
|
|
127
|
-
throw new Error(`Loop '${loopDef.id}' references non-existent start node '${loopDef.startNodeId}'.`);
|
|
128
|
-
}
|
|
129
|
-
if (!endNode) {
|
|
130
|
-
throw new Error(`Loop '${loopDef.id}' references non-existent end node '${loopDef.endNodeId}'.`);
|
|
131
|
-
}
|
|
132
|
-
startNode.config = { ...startNode.config, joinStrategy: "any" };
|
|
133
|
-
endNode.config = { ...endNode.config, joinStrategy: "any" };
|
|
134
|
-
}
|
|
135
|
-
return this.blueprint;
|
|
136
|
-
}
|
|
137
|
-
getFunctionRegistry() {
|
|
138
|
-
return this.functionRegistry;
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
function createFlow(id) {
|
|
142
|
-
return new Flow(id);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export { Flow, createFlow };
|
|
146
|
-
//# sourceMappingURL=chunk-4A627Q6L.js.map
|
|
147
|
-
//# sourceMappingURL=chunk-4A627Q6L.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/flow.ts"],"names":[],"mappings":";;;AAMA,SAAS,cAAc,EAAA,EAAwF;AAC9G,EAAA,MAAM,MAAA,GAAS,GAAG,QAAA,EAAS;AAC3B,EAAA,IAAI,IAAA,GAAO,CAAA;AACX,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,MAAA,CAAO,QAAQ,CAAA,EAAA,EAAK;AACvC,IAAA,MAAM,IAAA,GAAO,MAAA,CAAO,UAAA,CAAW,CAAC,CAAA;AAChC,IAAA,IAAA,GAAA,CAAQ,IAAA,IAAQ,KAAK,IAAA,GAAO,IAAA;AAC5B,IAAA,IAAA,GAAO,IAAA,GAAO,IAAA;AAAA,EACf;AACA,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,IAAI,CAAA,CAAE,SAAS,EAAE,CAAA;AAClC;AAKO,IAAM,OAAN,MAGL;AAAA,EACO,SAAA;AAAA,EACA,gBAAA;AAAA,EACA,iBAAA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SAAA;AAAA,EACb;AAAA,EAEA,mBAAA,GAAsB;AACrB,IAAA,OAAO,IAAA,CAAK,gBAAA;AAAA,EACb;AACD;AAKO,SAAS,WAGd,EAAA,EAA2C;AAC5C,EAAA,OAAO,IAAI,KAAK,EAAE,CAAA;AACnB","file":"chunk-4A627Q6L.js","sourcesContent":["import { isNodeClass } from './node'\nimport type { EdgeDefinition, NodeClass, NodeDefinition, NodeFunction, WorkflowBlueprint } from './types'\n\n/**\n * Generates a deterministic hash for a function based on its source code.\n */\nfunction _hashFunction(fn: NodeFunction<any, any, any, any, any> | NodeClass<any, any, any, any, any>): string {\n\tconst source = fn.toString()\n\tlet hash = 0\n\tfor (let i = 0; i < source.length; i++) {\n\t\tconst char = source.charCodeAt(i)\n\t\thash = (hash << 5) - hash + char\n\t\thash = hash & hash // Convert to 32-bit integer\n\t}\n\treturn Math.abs(hash).toString(16)\n}\n\n/**\n * A fluent API for programmatically constructing a WorkflowBlueprint.\n */\nexport class Flow<\n\tTContext extends Record<string, any> = Record<string, any>,\n\tTDependencies extends Record<string, any> = Record<string, any>,\n> {\n\tprivate blueprint: Partial<WorkflowBlueprint>\n\tprivate functionRegistry: Map<string, NodeFunction | NodeClass>\n\tprivate loopControllerIds: Map<string, string>\n\tprivate loopDefinitions: Array<{\n\t\tid: string\n\t\tstartNodeId: string\n\t\tendNodeId: string\n\t}>\n\n\tconstructor(id: string) {\n\t\tthis.blueprint = { id, nodes: [], edges: [] }\n\t\tthis.functionRegistry = new Map()\n\t\tthis.loopControllerIds = new Map()\n\t\tthis.loopDefinitions = []\n\t}\n\n\tnode<TInput = any, TOutput = any, TAction extends string = string>(\n\t\tid: string,\n\t\timplementation:\n\t\t\t| NodeFunction<TContext, TDependencies, TInput, TOutput, TAction>\n\t\t\t| NodeClass<TContext, TDependencies, TInput, TOutput, TAction>,\n\t\toptions?: Omit<NodeDefinition, 'id' | 'uses'>,\n\t): this {\n\t\tlet usesKey: string\n\n\t\tif (isNodeClass(implementation)) {\n\t\t\tusesKey =\n\t\t\t\timplementation.name && implementation.name !== 'BaseNode'\n\t\t\t\t\t? implementation.name\n\t\t\t\t\t: `class_${_hashFunction(implementation)}`\n\t\t\tthis.functionRegistry.set(usesKey, implementation)\n\t\t} else {\n\t\t\tusesKey = `fn_${_hashFunction(implementation)}`\n\t\t\tthis.functionRegistry.set(usesKey, implementation as unknown as NodeFunction)\n\t\t}\n\n\t\tconst nodeDef: NodeDefinition = { id, uses: usesKey, ...options }\n\t\tthis.blueprint.nodes?.push(nodeDef)\n\t\treturn this\n\t}\n\n\tedge(source: string, target: string, options?: Omit<EdgeDefinition, 'source' | 'target'>): this {\n\t\tconst edgeDef: EdgeDefinition = { source, target, ...options }\n\t\tthis.blueprint.edges?.push(edgeDef)\n\t\treturn this\n\t}\n\n\t/**\n\t * Creates a batch processing pattern.\n\t * It takes an input array, runs a worker node on each item in parallel, and gathers the results.\n\t * @param id The base ID for this batch operation.\n\t * @param worker The node implementation to run on each item.\n\t * @param options Configuration for the batch operation.\n\t * @param options.inputKey The key in the context that holds the input array for the batch.\n\t * @param options.outputKey The key in the context where the array of results will be stored.\n\t * @returns The Flow instance for chaining.\n\t */\n\tbatch<TInput = any, TOutput = any, TAction extends string = string>(\n\t\tid: string,\n\t\tworker:\n\t\t\t| NodeFunction<TContext, TDependencies, TInput, TOutput, TAction>\n\t\t\t| NodeClass<TContext, TDependencies, TInput, TOutput, TAction>,\n\t\toptions: {\n\t\t\t/** The key in the context that holds the input array for the batch. */\n\t\t\tinputKey: keyof TContext\n\t\t\t/** The key in the context where the array of results will be stored. */\n\t\t\toutputKey: keyof TContext\n\t\t},\n\t): this {\n\t\tconst { inputKey, outputKey } = options\n\t\tconst scatterId = `${id}_scatter`\n\t\tconst gatherId = `${id}_gather`\n\n\t\t// register worker implementation under a unique key.\n\t\tlet workerUsesKey: string\n\t\tif (isNodeClass(worker)) {\n\t\t\tworkerUsesKey =\n\t\t\t\tworker.name && worker.name !== 'BaseNode' ? worker.name : `class_batch_worker_${_hashFunction(worker)}`\n\t\t\tthis.functionRegistry.set(workerUsesKey, worker)\n\t\t} else {\n\t\t\tworkerUsesKey = `fn_batch_worker_${_hashFunction(worker)}`\n\t\t\tthis.functionRegistry.set(workerUsesKey, worker as unknown as NodeFunction)\n\t\t}\n\n\t\t// scatter node: takes an array and dynamically schedules worker nodes\n\t\tthis.blueprint.nodes?.push({\n\t\t\tid: scatterId,\n\t\t\tuses: 'batch-scatter', // built-in\n\t\t\tinputs: inputKey as string,\n\t\t\tparams: { workerUsesKey, outputKey: outputKey as string, gatherNodeId: gatherId },\n\t\t})\n\n\t\t// gather node: waits for all workers to finish and collects the results\n\t\tthis.blueprint.nodes?.push({\n\t\t\tid: gatherId,\n\t\t\tuses: 'batch-gather', // built-in\n\t\t\tparams: { outputKey },\n\t\t\tconfig: { joinStrategy: 'all' }, // important: must wait for all scattered jobs\n\t\t})\n\n\t\t// edge to connect scatter and gather nodes. orchestrator will manage dynamic workers\n\t\tthis.edge(scatterId, gatherId)\n\n\t\treturn this\n\t}\n\n\t/**\n\t * Creates a loop pattern in the workflow graph.\n\t * @param id A unique identifier for the loop construct.\n\t * @param options Defines the start, end, and continuation condition of the loop.\n\t * @param options.startNodeId The ID of the first node inside the loop body.\n\t * @param options.endNodeId The ID of the last node inside the loop body.\n\t * @param options.condition An expression that, if true, causes the loop to run again.\n\t */\n\tloop(\n\t\tid: string,\n\t\toptions: {\n\t\t\t/** The ID of the first node inside the loop body. */\n\t\t\tstartNodeId: string\n\t\t\t/** The ID of the last node inside the loop body. */\n\t\t\tendNodeId: string\n\t\t\t/** An expression that, if true, causes the loop to run again. */\n\t\t\tcondition: string\n\t\t},\n\t): this {\n\t\tconst { startNodeId, endNodeId, condition } = options\n\t\tconst controllerId = `${id}-loop`\n\n\t\tthis.loopControllerIds.set(id, controllerId)\n\n\t\tthis.loopDefinitions.push({ id, startNodeId, endNodeId })\n\n\t\t// controller node: evaluates the loop condition\n\t\tthis.blueprint.nodes?.push({\n\t\t\tid: controllerId,\n\t\t\tuses: 'loop-controller', // built-in\n\t\t\tparams: { condition },\n\t\t\tconfig: { joinStrategy: 'any' }, // to allow re-execution on each loop iteration\n\t\t})\n\n\t\tthis.edge(endNodeId, controllerId)\n\n\t\tthis.edge(controllerId, startNodeId, {\n\t\t\taction: 'continue',\n\t\t\ttransform: `context.${endNodeId}`, // pass the end node's value to the start node\n\t\t})\n\n\t\treturn this\n\t}\n\n\tgetLoopControllerId(id: string): string {\n\t\tconst controllerId = this.loopControllerIds.get(id)\n\t\tif (!controllerId) {\n\t\t\tthrow new Error(`Loop with id '${id}' not found. Ensure you have defined it using the .loop() method.`)\n\t\t}\n\t\treturn controllerId\n\t}\n\n\ttoBlueprint(): WorkflowBlueprint {\n\t\tif (!this.blueprint.nodes || this.blueprint.nodes.length === 0) {\n\t\t\tthrow new Error('Cannot build a blueprint with no nodes.')\n\t\t}\n\n\t\tfor (const loopDef of this.loopDefinitions) {\n\t\t\tconst startNode = this.blueprint.nodes?.find((n) => n.id === loopDef.startNodeId)\n\t\t\tconst endNode = this.blueprint.nodes?.find((n) => n.id === loopDef.endNodeId)\n\n\t\t\tif (!startNode) {\n\t\t\t\tthrow new Error(`Loop '${loopDef.id}' references non-existent start node '${loopDef.startNodeId}'.`)\n\t\t\t}\n\t\t\tif (!endNode) {\n\t\t\t\tthrow new Error(`Loop '${loopDef.id}' references non-existent end node '${loopDef.endNodeId}'.`)\n\t\t\t}\n\n\t\t\tstartNode.config = { ...startNode.config, joinStrategy: 'any' }\n\t\t\tendNode.config = { ...endNode.config, joinStrategy: 'any' }\n\t\t}\n\n\t\treturn this.blueprint as WorkflowBlueprint\n\t}\n\n\tgetFunctionRegistry() {\n\t\treturn this.functionRegistry\n\t}\n}\n\n/**\n * Helper function to create a new Flow builder instance.\n */\nexport function createFlow<\n\tTContext extends Record<string, any> = Record<string, any>,\n\tTDependencies extends Record<string, any> = Record<string, any>,\n>(id: string): Flow<TContext, TDependencies> {\n\treturn new Flow(id)\n}\n"]}
|