-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathnode.ts
More file actions
377 lines (336 loc) · 11.7 KB
/
Copy pathnode.ts
File metadata and controls
377 lines (336 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { createLogger } from '@sim/logger'
import { isLargeValueRef } from '@/lib/execution/payloads/large-value-ref'
import { EDGE } from '@/executor/constants'
import type { DAG, DAGNode } from '@/executor/dag/builder'
import type { BlockExecutor } from '@/executor/execution/block-executor'
import type { BlockStateController } from '@/executor/execution/types'
import type { LoopOrchestrator } from '@/executor/orchestrators/loop'
import type { ParallelOrchestrator } from '@/executor/orchestrators/parallel'
import type { ExecutionContext, NormalizedBlockOutput } from '@/executor/types'
import {
buildOuterBranchScopedId,
extractBaseBlockId,
extractOuterBranchIndex,
} from '@/executor/utils/subflow-utils'
const logger = createLogger('NodeExecutionOrchestrator')
function getResultCount(value: unknown): number {
if (isLargeValueRef(value)) {
const preview = value.preview
if (
preview &&
typeof preview === 'object' &&
typeof (preview as Record<string, unknown>).length === 'number'
) {
return (preview as { length: number }).length
}
}
return Array.isArray(value) ? value.length : 0
}
function getSubflowResultOutput(output: NormalizedBlockOutput): NormalizedBlockOutput {
return { results: output.results ?? [] }
}
export interface NodeExecutionResult {
nodeId: string
output: NormalizedBlockOutput
isFinalOutput: boolean
}
export class NodeExecutionOrchestrator {
constructor(
private dag: DAG,
private state: BlockStateController,
private blockExecutor: BlockExecutor,
private loopOrchestrator: LoopOrchestrator,
private parallelOrchestrator: ParallelOrchestrator
) {}
async executeNode(ctx: ExecutionContext, nodeId: string): Promise<NodeExecutionResult> {
const node = this.dag.nodes.get(nodeId)
if (!node) {
throw new Error(`Node not found in DAG: ${nodeId}`)
}
if (ctx.runFromBlockContext && !ctx.runFromBlockContext.dirtySet.has(nodeId)) {
const cachedOutput = this.state.getBlockOutput(nodeId) || {}
logger.debug('Skipping non-dirty block in run-from-block mode', { nodeId })
return {
nodeId,
output: cachedOutput,
isFinalOutput: false,
}
}
const isDirtyBlock = ctx.runFromBlockContext?.dirtySet.has(nodeId) ?? false
if (!isDirtyBlock && this.state.hasExecuted(nodeId)) {
const output = this.state.getBlockOutput(nodeId) || {}
return {
nodeId,
output,
isFinalOutput: false,
}
}
const loopId = node.metadata.subflowType === 'loop' ? node.metadata.subflowId : undefined
if (loopId && !this.loopOrchestrator.getLoopScope(ctx, loopId)) {
await this.loopOrchestrator.initializeLoopScope(ctx, loopId)
}
const parallelId =
node.metadata.subflowType === 'parallel' ? node.metadata.subflowId : undefined
if (parallelId && !this.parallelOrchestrator.getParallelScope(ctx, parallelId)) {
await this.parallelOrchestrator.initializeParallelScope(ctx, parallelId)
}
if (node.metadata.isSentinel) {
const output = await this.handleSentinel(ctx, node)
const isFinalOutput = this.isFinalSentinelOutput(node, output)
return {
nodeId,
output,
isFinalOutput,
}
}
const output = await this.blockExecutor.execute(ctx, node, node.block)
const isFinalOutput = node.outgoingEdges.size === 0
return {
nodeId,
output,
isFinalOutput,
}
}
private isFinalSentinelOutput(node: DAGNode, output: NormalizedBlockOutput): boolean {
const selectedRoute = output.selectedRoute
if (selectedRoute === EDGE.LOOP_CONTINUE || selectedRoute === EDGE.PARALLEL_CONTINUE) {
return false
}
if (selectedRoute === EDGE.LOOP_EXIT || selectedRoute === EDGE.PARALLEL_EXIT) {
return !Array.from(node.outgoingEdges.values()).some(
(edge) => edge.sourceHandle === selectedRoute
)
}
return node.outgoingEdges.size === 0
}
private async handleSentinel(
ctx: ExecutionContext,
node: DAGNode
): Promise<NormalizedBlockOutput> {
const sentinelType = node.metadata.sentinelType
const subflowType = node.metadata.subflowType
const subflowId = node.metadata.subflowId
if (!subflowType || !subflowId) {
logger.warn('Sentinel missing subflow metadata', { nodeId: node.id, sentinelType })
return {}
}
if (subflowType === 'parallel') {
return await this.handleParallelSentinel(ctx, node, sentinelType, subflowId)
}
switch (sentinelType) {
case 'start': {
const shouldExecute = await this.loopOrchestrator.evaluateInitialCondition(ctx, subflowId)
if (!shouldExecute) {
logger.info('Loop initial condition false, skipping loop body', { loopId: subflowId })
return {
sentinelStart: true,
shouldExit: true,
selectedRoute: EDGE.LOOP_EXIT,
}
}
return { sentinelStart: true }
}
case 'end': {
const continuationResult = await this.loopOrchestrator.evaluateLoopContinuation(
ctx,
subflowId
)
if (continuationResult.shouldContinue) {
return {
shouldContinue: true,
shouldExit: false,
selectedRoute: continuationResult.selectedRoute,
}
}
return {
results: continuationResult.aggregatedResults || [],
shouldContinue: false,
shouldExit: true,
selectedRoute: continuationResult.selectedRoute,
totalIterations:
continuationResult.totalIterations ??
getResultCount(continuationResult.aggregatedResults),
}
}
default:
logger.warn('Unknown sentinel type', { sentinelType })
return {}
}
}
private async handleParallelSentinel(
ctx: ExecutionContext,
node: DAGNode,
sentinelType: string | undefined,
parallelId: string
): Promise<NormalizedBlockOutput> {
if (sentinelType === 'start') {
if (!this.parallelOrchestrator.getParallelScope(ctx, parallelId)) {
const parallelConfig = this.dag.parallelConfigs.get(parallelId)
if (parallelConfig) {
await this.parallelOrchestrator.initializeParallelScope(ctx, parallelId)
}
}
const scope = this.parallelOrchestrator.getParallelScope(ctx, parallelId)
if (scope?.isEmpty) {
logger.info('Parallel has empty distribution, skipping parallel body', { parallelId })
return {
sentinelStart: true,
shouldExit: true,
selectedRoute: EDGE.PARALLEL_EXIT,
}
}
this.parallelOrchestrator.prepareCurrentBatch(ctx, parallelId)
return { sentinelStart: true }
}
if (sentinelType === 'end') {
const result = await this.parallelOrchestrator.aggregateParallelResults(ctx, parallelId)
if (!result.allBranchesComplete) {
return {
results: [],
sentinelEnd: true,
selectedRoute: EDGE.PARALLEL_CONTINUE,
totalBranches: result.totalBranches,
}
}
return {
results: result.results || [],
sentinelEnd: true,
selectedRoute: EDGE.PARALLEL_EXIT,
totalBranches: result.totalBranches,
}
}
logger.warn('Unknown parallel sentinel type', { sentinelType })
return {}
}
async handleNodeCompletion(
ctx: ExecutionContext,
nodeId: string,
output: NormalizedBlockOutput
): Promise<void> {
const node = this.dag.nodes.get(nodeId)
if (!node) {
logger.error('Node not found during completion handling', { nodeId })
return
}
const loopId = node.metadata.subflowType === 'loop' ? node.metadata.subflowId : undefined
const isParallelBranch = node.metadata.isParallelBranch
const isSentinel = node.metadata.isSentinel
if (isSentinel) {
this.handleRegularNodeCompletion(ctx, node, output)
this.handleParentSubflowCompletion(ctx, node, output)
} else if (loopId) {
this.handleLoopNodeCompletion(ctx, node, output, loopId)
} else if (isParallelBranch) {
const parallelId =
node.metadata.subflowType === 'parallel' ? node.metadata.subflowId : undefined
if (parallelId) {
await this.handleParallelNodeCompletion(ctx, node, output, parallelId)
} else {
logger.warn('Parallel branch missing subflow metadata', { nodeId: node.id })
this.handleRegularNodeCompletion(ctx, node, output)
}
} else {
this.handleRegularNodeCompletion(ctx, node, output)
}
}
private handleLoopNodeCompletion(
ctx: ExecutionContext,
node: DAGNode,
output: NormalizedBlockOutput,
loopId: string
): void {
this.loopOrchestrator.storeLoopNodeOutput(ctx, loopId, node.id, output)
this.state.setBlockOutput(node.id, output)
}
private async handleParallelNodeCompletion(
ctx: ExecutionContext,
node: DAGNode,
output: NormalizedBlockOutput,
parallelId: string
): Promise<void> {
const scope = this.parallelOrchestrator.getParallelScope(ctx, parallelId)
if (!scope) {
await this.parallelOrchestrator.initializeParallelScope(ctx, parallelId)
}
this.parallelOrchestrator.handleParallelBranchCompletion(ctx, parallelId, node.id, output)
const branchIndex = node.metadata.branchIndex
if (branchIndex !== undefined && extractOuterBranchIndex(node.id) === undefined) {
const originalBlockId = node.metadata.originalBlockId ?? extractBaseBlockId(node.id)
this.state.setBlockOutput(buildOuterBranchScopedId(originalBlockId, branchIndex), output)
}
this.state.setBlockOutput(node.id, output)
}
private handleParentSubflowCompletion(
ctx: ExecutionContext,
node: DAGNode,
output: NormalizedBlockOutput
): void {
if (node.metadata.sentinelType !== 'end' || !node.metadata.subflowId) {
return
}
if (
output.selectedRoute === EDGE.LOOP_CONTINUE ||
output.selectedRoute === EDGE.LOOP_CONTINUE_ALT ||
output.selectedRoute === EDGE.PARALLEL_CONTINUE
) {
return
}
const subflowId = node.metadata.subflowId
const parentEntry = ctx.subflowParentMap?.get(subflowId)
if (!parentEntry) {
return
}
if (parentEntry.parentType === 'parallel') {
if (parentEntry.branchIndex === undefined) {
return
}
this.parallelOrchestrator.handleParallelBranchCompletion(
ctx,
parentEntry.parentId,
node.id,
getSubflowResultOutput(output),
parentEntry.branchIndex
)
return
}
this.loopOrchestrator.storeLoopNodeOutput(
ctx,
parentEntry.parentId,
subflowId,
getSubflowResultOutput(output)
)
}
private handleRegularNodeCompletion(
ctx: ExecutionContext,
node: DAGNode,
output: NormalizedBlockOutput
): void {
this.state.setBlockOutput(node.id, output)
if (
node.metadata.isSentinel &&
node.metadata.subflowType === 'loop' &&
node.metadata.sentinelType === 'end' &&
output.selectedRoute === 'loop_continue'
) {
const loopId = node.metadata.subflowId
if (!loopId) {
logger.warn('Loop sentinel missing subflow metadata', { nodeId: node.id })
return
}
this.loopOrchestrator.clearLoopExecutionState(loopId, ctx)
this.loopOrchestrator.restoreLoopEdges(loopId)
}
if (
node.metadata.subflowType === 'parallel' &&
node.metadata.sentinelType === 'end' &&
output.selectedRoute === EDGE.PARALLEL_CONTINUE
) {
const parallelId = node.metadata.subflowId
if (!parallelId) {
logger.warn('Parallel sentinel missing subflow metadata', { nodeId: node.id })
return
}
this.parallelOrchestrator.prepareForBatchContinuation(parallelId)
}
}
}