(state: WorkflowState)
| 469 | * Creates nested structure for loops/parallels with their child blocks inside |
| 470 | */ |
| 471 | export function sanitizeForCopilot(state: WorkflowState): CopilotWorkflowState { |
| 472 | const sanitizedBlocks: Record<string, CopilotBlockState> = {} |
| 473 | const processedBlocks = new Set<string>() |
| 474 | |
| 475 | // Helper to find child blocks of a parent (loop/parallel container) |
| 476 | const findChildBlocks = (parentId: string): string[] => { |
| 477 | return Object.keys(state.blocks).filter( |
| 478 | (blockId) => state.blocks[blockId].data?.parentId === parentId |
| 479 | ) |
| 480 | } |
| 481 | |
| 482 | // Helper to recursively sanitize a block and its children |
| 483 | const sanitizeBlock = (blockId: string, block: BlockState): CopilotBlockState => { |
| 484 | const connections = extractConnectionsForBlock(blockId, state.edges, block) |
| 485 | |
| 486 | // For loop/parallel blocks, extract config from block.data instead of subBlocks |
| 487 | let inputs: Record<string, string | number | string[][] | object> |
| 488 | |
| 489 | if (block.type === 'loop' || block.type === 'parallel') { |
| 490 | // Extract configuration from block.data (only include type-appropriate fields) |
| 491 | const loopInputs: Record<string, string | number | string[][] | object> = {} |
| 492 | |
| 493 | if (block.type === 'loop') { |
| 494 | const loopType = block.data?.loopType || 'for' |
| 495 | loopInputs.loopType = loopType |
| 496 | // Only export fields relevant to the current loopType |
| 497 | if (loopType === 'for' && block.data?.count !== undefined) { |
| 498 | loopInputs.iterations = block.data.count |
| 499 | } |
| 500 | if (loopType === 'forEach' && block.data?.collection !== undefined) { |
| 501 | loopInputs.collection = block.data.collection |
| 502 | } |
| 503 | if (loopType === 'while' && block.data?.whileCondition !== undefined) { |
| 504 | loopInputs.condition = block.data.whileCondition |
| 505 | } |
| 506 | if (loopType === 'doWhile' && block.data?.doWhileCondition !== undefined) { |
| 507 | loopInputs.condition = block.data.doWhileCondition |
| 508 | } |
| 509 | } else if (block.type === 'parallel') { |
| 510 | const parallelType = block.data?.parallelType || 'count' |
| 511 | loopInputs.parallelType = parallelType |
| 512 | // Only export fields relevant to the current parallelType |
| 513 | if (parallelType === 'count' && block.data?.count !== undefined) { |
| 514 | loopInputs.iterations = block.data.count |
| 515 | } |
| 516 | if (parallelType === 'collection' && block.data?.collection !== undefined) { |
| 517 | loopInputs.collection = block.data.collection |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | inputs = loopInputs |
| 522 | } else { |
| 523 | // For regular blocks, sanitize subBlocks |
| 524 | inputs = sanitizeSubBlocks(block.subBlocks) |
| 525 | } |
| 526 | |
| 527 | // Check if this is a loop or parallel (has children) |
| 528 | const childBlockIds = findChildBlocks(blockId) |
no test coverage detected