11import { createLogger } from '@sim/logger'
2+ import { DEFAULT_SUBBLOCK_TYPE } from '@sim/workflow-persistence/subblocks'
3+ import { isPlainRecord } from '@/lib/core/utils/records'
24import { sanitizeMalformedSubBlocks } from '@/lib/workflows/sanitization/subblocks'
35import {
46 buildCanonicalIndex ,
@@ -69,6 +71,7 @@ export const SUBBLOCK_ID_MIGRATIONS: Record<string, Record<string, string>> = {
6971 * Returns a new subBlocks record if anything changed, or the original if not.
7072 */
7173function migrateBlockSubblockIds (
74+ blockType : string ,
7275 subBlocks : Record < string , BlockState [ 'subBlocks' ] [ string ] > ,
7376 renames : Record < string , string >
7477) : { subBlocks : Record < string , BlockState [ 'subBlocks' ] [ string ] > ; migrated : boolean } {
@@ -84,6 +87,7 @@ function migrateBlockSubblockIds(
8487 if ( ! migrated ) return { subBlocks, migrated : false }
8588
8689 const result = { ...subBlocks }
90+ const blockConfig = getBlock ( blockType )
8791
8892 for ( const [ oldId , newId ] of Object . entries ( renames ) ) {
8993 if ( ! ( oldId in result ) ) continue
@@ -94,7 +98,24 @@ function migrateBlockSubblockIds(
9498 }
9599
96100 const oldEntry = result [ oldId ]
97- result [ newId ] = { ...oldEntry , id : newId }
101+ const configuredType = blockConfig ?. subBlocks ?. find ( ( config ) => config . id === newId ) ?. type
102+ result [ newId ] = isPlainRecord ( oldEntry )
103+ ? {
104+ ...oldEntry ,
105+ id : newId ,
106+ type :
107+ configuredType ||
108+ ( typeof oldEntry . type === 'string' && oldEntry . type . length > 0
109+ ? oldEntry . type === 'unknown'
110+ ? DEFAULT_SUBBLOCK_TYPE
111+ : oldEntry . type
112+ : DEFAULT_SUBBLOCK_TYPE ) ,
113+ }
114+ : ( {
115+ id : newId ,
116+ type : configuredType || DEFAULT_SUBBLOCK_TYPE ,
117+ value : oldEntry ,
118+ } as BlockState [ 'subBlocks' ] [ string ] )
98119 delete result [ oldId ]
99120 }
100121
@@ -118,25 +139,23 @@ export function migrateSubblockIds(blocks: Record<string, BlockState>): {
118139 continue
119140 }
120141
121- const sanitized = sanitizeMalformedSubBlocks ( block )
122142 const renames = SUBBLOCK_ID_MIGRATIONS [ block . type ]
123- if ( ! renames ) {
124- result [ blockId ] = sanitized . changed ? { ...block , subBlocks : sanitized . subBlocks } : block
125- anyMigrated = anyMigrated || sanitized . changed
126- continue
127- }
143+ const renamed = renames
144+ ? migrateBlockSubblockIds ( block . type , block . subBlocks , renames )
145+ : { subBlocks : block . subBlocks , migrated : false }
146+ const renamedBlock = renamed . migrated ? { ...block , subBlocks : renamed . subBlocks } : block
147+ const sanitized = sanitizeMalformedSubBlocks ( renamedBlock )
148+ const blockMigrated = renamed . migrated || sanitized . changed
128149
129- const { subBlocks, migrated } = migrateBlockSubblockIds ( sanitized . subBlocks , renames )
130- const blockMigrated = sanitized . changed || migrated
131150 if ( blockMigrated ) {
132- if ( migrated ) {
151+ if ( renamed . migrated ) {
133152 logger . info ( 'Migrated legacy subblock IDs' , {
134153 blockId : block . id ,
135154 blockType : block . type ,
136155 } )
137156 }
138157 anyMigrated = true
139- result [ blockId ] = { ...block , subBlocks }
158+ result [ blockId ] = { ...renamedBlock , subBlocks : sanitized . subBlocks }
140159 } else {
141160 result [ blockId ] = block
142161 }
0 commit comments