-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathpath.ts
More file actions
300 lines (255 loc) · 9.31 KB
/
path.ts
File metadata and controls
300 lines (255 loc) · 9.31 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
import { createLogger } from '@/lib/logs/console-logger'
import type { SerializedBlock, SerializedConnection, SerializedWorkflow } from '@/serializer/types'
import type { BlockState, ExecutionContext } from './types'
const logger = createLogger('PathTracker')
/**
* Manages the active execution paths in the workflow.
* Tracks which blocks should be executed based on routing decisions.
*/
export class PathTracker {
constructor(private workflow: SerializedWorkflow) {}
/**
* Checks if a block is in the active execution path.
* Considers router and condition block decisions.
*
* @param blockId - ID of the block to check
* @param context - Current execution context
* @returns Whether the block is in the active execution path
*/
isInActivePath(blockId: string, context: ExecutionContext): boolean {
// Early return if already in active path
if (context.activeExecutionPath.has(blockId)) {
return true
}
// Get all incoming connections to this block
const incomingConnections = this.getIncomingConnections(blockId)
// A block is in the active path if at least one of its incoming connections
// is from an active and executed block
return incomingConnections.some((conn) => this.isConnectionActive(conn, context))
}
/**
* Updates execution paths based on newly executed blocks.
* Handles router and condition block decisions to activate paths without deactivating others.
*
* @param executedBlockIds - IDs of blocks that were just executed
* @param context - Current execution context
*/
updateExecutionPaths(executedBlockIds: string[], context: ExecutionContext): void {
logger.info(`Updating paths for blocks: ${executedBlockIds.join(', ')}`)
for (const blockId of executedBlockIds) {
const block = this.getBlock(blockId)
if (!block) continue
this.updatePathForBlock(block, context)
}
}
/**
* Get all incoming connections to a block
*/
private getIncomingConnections(blockId: string): SerializedConnection[] {
return this.workflow.connections.filter((conn) => conn.target === blockId)
}
/**
* Get all outgoing connections from a block
*/
private getOutgoingConnections(blockId: string): SerializedConnection[] {
return this.workflow.connections.filter((conn) => conn.source === blockId)
}
/**
* Get a block by ID
*/
private getBlock(blockId: string): SerializedBlock | undefined {
return this.workflow.blocks.find((b) => b.id === blockId)
}
/**
* Check if a connection is active based on its source block type and state
*/
private isConnectionActive(connection: SerializedConnection, context: ExecutionContext): boolean {
const sourceBlock = this.getBlock(connection.source)
if (!sourceBlock) return false
const blockType = sourceBlock.metadata?.id
// Use strategy pattern for different block types
switch (blockType) {
case 'router':
return this.isRouterConnectionActive(connection, context)
case 'condition':
return this.isConditionConnectionActive(connection, context)
default:
return this.isRegularConnectionActive(connection, context)
}
}
/**
* Check if a router connection is active
*/
private isRouterConnectionActive(
connection: SerializedConnection,
context: ExecutionContext
): boolean {
const selectedTarget = context.decisions.router.get(connection.source)
return context.executedBlocks.has(connection.source) && selectedTarget === connection.target
}
/**
* Check if a condition connection is active
*/
private isConditionConnectionActive(
connection: SerializedConnection,
context: ExecutionContext
): boolean {
if (!connection.sourceHandle?.startsWith('condition-')) {
return false
}
const conditionId = connection.sourceHandle.replace('condition-', '')
const selectedCondition = context.decisions.condition.get(connection.source)
return context.executedBlocks.has(connection.source) && conditionId === selectedCondition
}
/**
* Check if a regular connection is active
*/
private isRegularConnectionActive(
connection: SerializedConnection,
context: ExecutionContext
): boolean {
return (
context.activeExecutionPath.has(connection.source) &&
context.executedBlocks.has(connection.source)
)
}
/**
* Update paths for a specific block based on its type
*/
private updatePathForBlock(block: SerializedBlock, context: ExecutionContext): void {
const blockType = block.metadata?.id
switch (blockType) {
case 'router':
this.updateRouterPaths(block, context)
break
case 'condition':
this.updateConditionPaths(block, context)
break
case 'loop':
this.updateLoopPaths(block, context)
break
default:
this.updateRegularBlockPaths(block, context)
break
}
}
/**
* Update paths for router blocks
*/
private updateRouterPaths(block: SerializedBlock, context: ExecutionContext): void {
const routerOutput = context.blockStates.get(block.id)?.output
const selectedPath = routerOutput?.selectedPath?.blockId
if (selectedPath) {
context.decisions.router.set(block.id, selectedPath)
context.activeExecutionPath.add(selectedPath)
this.activateDownstreamPaths(selectedPath, context)
logger.info(`Router ${block.id} selected path: ${selectedPath}`)
}
}
/**
* Recursively activate downstream paths from a block
*/
private activateDownstreamPaths(blockId: string, context: ExecutionContext): void {
const outgoingConnections = this.getOutgoingConnections(blockId)
for (const conn of outgoingConnections) {
if (!context.activeExecutionPath.has(conn.target)) {
context.activeExecutionPath.add(conn.target)
this.activateDownstreamPaths(conn.target, context)
}
}
}
/**
* Update paths for condition blocks
*/
private updateConditionPaths(block: SerializedBlock, context: ExecutionContext): void {
const conditionOutput = context.blockStates.get(block.id)?.output
const selectedConditionId = conditionOutput?.selectedConditionId
if (!selectedConditionId) return
context.decisions.condition.set(block.id, selectedConditionId)
const targetConnections = this.workflow.connections.filter(
(conn) => conn.source === block.id && conn.sourceHandle === `condition-${selectedConditionId}`
)
for (const conn of targetConnections) {
context.activeExecutionPath.add(conn.target)
logger.debug(`Condition ${block.id} activated path to: ${conn.target}`)
}
}
/**
* Update paths for loop blocks
*/
private updateLoopPaths(block: SerializedBlock, context: ExecutionContext): void {
const outgoingConnections = this.getOutgoingConnections(block.id)
for (const conn of outgoingConnections) {
// Only activate loop-start connections
if (conn.sourceHandle === 'loop-start-source') {
context.activeExecutionPath.add(conn.target)
logger.info(`Loop ${block.id} activated start path to: ${conn.target}`)
}
// loop-end-source connections will be activated by the loop manager
}
}
/**
* Update paths for regular blocks
*/
private updateRegularBlockPaths(block: SerializedBlock, context: ExecutionContext): void {
const blockState = context.blockStates.get(block.id)
const hasError = this.blockHasError(blockState)
const outgoingConnections = this.getOutgoingConnections(block.id)
// Check if block is part of loops
const blockLoops = this.getBlockLoops(block.id, context)
const isPartOfLoop = blockLoops.length > 0
for (const conn of outgoingConnections) {
if (this.shouldActivateConnection(conn, hasError, isPartOfLoop, blockLoops, context)) {
context.activeExecutionPath.add(conn.target)
}
}
}
/**
* Check if a block has an error
*/
private blockHasError(blockState: BlockState | undefined): boolean {
return blockState?.output?.error !== undefined
}
/**
* Get loops that contain a block
*/
private getBlockLoops(
blockId: string,
context: ExecutionContext
): Array<{ id: string; loop: any }> {
return Object.entries(context.workflow?.loops || {})
.filter(([_, loop]) => loop.nodes.includes(blockId))
.map(([id, loop]) => ({ id, loop }))
}
/**
* Determine if a connection should be activated
*/
private shouldActivateConnection(
conn: SerializedConnection,
hasError: boolean,
isPartOfLoop: boolean,
blockLoops: Array<{ id: string; loop: any }>,
context: ExecutionContext
): boolean {
// Check if this is an external loop connection
if (isPartOfLoop) {
const isInternalConnection = blockLoops.some(({ loop }) => loop.nodes.includes(conn.target))
const isExternalConnection = !isInternalConnection
const allLoopsCompleted = blockLoops.every(({ id }) => context.completedLoops?.has(id))
// Skip external connections unless all loops are completed
if (isExternalConnection && !allLoopsCompleted) {
return false
}
}
// Handle error connections
if (conn.sourceHandle === 'error') {
return hasError
}
// Handle regular connections
if (conn.sourceHandle === 'source' || !conn.sourceHandle) {
return !hasError
}
// All other connection types are activated
return true
}
}