(state: WorkflowState)
| 511 | * @returns A normalized workflow state suitable for comparison or hashing |
| 512 | */ |
| 513 | export function normalizeWorkflowState(state: WorkflowState): NormalizedWorkflowState { |
| 514 | // 1. Normalize and sort edges (connection-relevant fields only) |
| 515 | const normalizedEdges = sortEdges((state.edges || []).map(normalizeEdge)) |
| 516 | |
| 517 | // 2. Normalize blocks |
| 518 | const normalizedBlocks: Record<string, NormalizedBlock> = {} |
| 519 | |
| 520 | for (const [blockId, block] of Object.entries(state.blocks || {})) { |
| 521 | const { |
| 522 | blockRest, |
| 523 | normalizedData, |
| 524 | subBlocks: blockSubBlocks, |
| 525 | } = extractBlockFieldsForComparison(block) |
| 526 | |
| 527 | // Filter and normalize subBlocks (exclude system/trigger runtime subBlocks) |
| 528 | const normalizedSubBlocks: Record<string, NormalizedSubBlock> = {} |
| 529 | const subBlockIds = filterSubBlockIds(Object.keys(blockSubBlocks)) |
| 530 | |
| 531 | for (const subBlockId of subBlockIds) { |
| 532 | const subBlock = blockSubBlocks[subBlockId] as SubBlockWithDiffMarker |
| 533 | const value = normalizeSubBlockValue(subBlockId, subBlock.value) |
| 534 | const subBlockRest = extractSubBlockRest(subBlock as Record<string, unknown>) |
| 535 | |
| 536 | normalizedSubBlocks[subBlockId] = { |
| 537 | ...subBlockRest, |
| 538 | value: normalizeValue(value), |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | normalizedBlocks[blockId] = { |
| 543 | ...blockRest, |
| 544 | data: normalizedData, |
| 545 | subBlocks: normalizedSubBlocks, |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // 3. Normalize loops using specialized normalizeLoop (extracts only type-relevant fields) |
| 550 | const normalizedLoops: Record<string, unknown> = {} |
| 551 | for (const [loopId, loop] of Object.entries(state.loops || {})) { |
| 552 | normalizedLoops[loopId] = normalizeValue(normalizeLoop(loop)) |
| 553 | } |
| 554 | |
| 555 | // 4. Normalize parallels using specialized normalizeParallel |
| 556 | const normalizedParallels: Record<string, unknown> = {} |
| 557 | for (const [parallelId, parallel] of Object.entries(state.parallels || {})) { |
| 558 | normalizedParallels[parallelId] = normalizeValue(normalizeParallel(parallel)) |
| 559 | } |
| 560 | |
| 561 | // 5. Normalize variables (remove UI-only validationError field) |
| 562 | const variables = normalizeVariables(state.variables) |
| 563 | const normalizedVariablesObj = normalizeValue( |
| 564 | Object.fromEntries(Object.entries(variables).map(([id, v]) => [id, sanitizeVariable(v)])) |
| 565 | ) |
| 566 | |
| 567 | return { |
| 568 | blocks: normalizedBlocks, |
| 569 | edges: normalizedEdges, |
| 570 | loops: normalizedLoops, |
no test coverage detected