@@ -14,6 +14,7 @@ import { and, desc, eq, inArray, sql } from 'drizzle-orm'
1414import type { Edge } from 'reactflow'
1515import { v4 as uuidv4 } from 'uuid'
1616import type { DbOrTx } from '@/lib/db/types'
17+ import { remapConditionBlockIds , remapConditionEdgeHandle } from '@/lib/workflows/condition-ids'
1718import {
1819 backfillCanonicalModes ,
1920 migrateSubblockIds ,
@@ -833,7 +834,12 @@ export function regenerateWorkflowStateIds(state: RegenerateStateInput): Regener
833834 Object . entries ( state . blocks || { } ) . forEach ( ( [ oldId , block ] ) => {
834835 const newId = blockIdMapping . get ( oldId ) !
835836 // Duplicated blocks are always unlocked so users can edit them
836- const newBlock : BlockState = { ...block , id : newId , locked : false }
837+ const newBlock : BlockState = {
838+ ...block ,
839+ id : newId ,
840+ subBlocks : JSON . parse ( JSON . stringify ( block . subBlocks ) ) ,
841+ locked : false ,
842+ }
837843
838844 // Update parentId reference if it exists
839845 if ( newBlock . data ?. parentId ) {
@@ -857,6 +863,21 @@ export function regenerateWorkflowStateIds(state: RegenerateStateInput): Regener
857863 updatedSubBlock . value = blockIdMapping . get ( updatedSubBlock . value ) ?? updatedSubBlock . value
858864 }
859865
866+ // Remap condition/router IDs embedded in condition-input/router-input subBlocks
867+ if (
868+ ( updatedSubBlock . type === 'condition-input' || updatedSubBlock . type === 'router-input' ) &&
869+ typeof updatedSubBlock . value === 'string'
870+ ) {
871+ try {
872+ const parsed = JSON . parse ( updatedSubBlock . value )
873+ if ( Array . isArray ( parsed ) && remapConditionBlockIds ( parsed , oldId , newId ) ) {
874+ updatedSubBlock . value = JSON . stringify ( parsed )
875+ }
876+ } catch {
877+ // Not valid JSON, skip
878+ }
879+ }
880+
860881 updatedSubBlocks [ subId ] = updatedSubBlock
861882 } )
862883 newBlock . subBlocks = updatedSubBlocks
@@ -871,12 +892,17 @@ export function regenerateWorkflowStateIds(state: RegenerateStateInput): Regener
871892 const newId = edgeIdMapping . get ( edge . id ) !
872893 const newSource = blockIdMapping . get ( edge . source ) || edge . source
873894 const newTarget = blockIdMapping . get ( edge . target ) || edge . target
895+ const newSourceHandle =
896+ edge . sourceHandle && blockIdMapping . has ( edge . source )
897+ ? remapConditionEdgeHandle ( edge . sourceHandle , edge . source , newSource )
898+ : edge . sourceHandle
874899
875900 newEdges . push ( {
876901 ...edge ,
877902 id : newId ,
878903 source : newSource ,
879904 target : newTarget ,
905+ sourceHandle : newSourceHandle ,
880906 } )
881907 } )
882908
0 commit comments