11import { createLogger } from '@sim/logger'
2+ import { DEFAULT_SUBBLOCK_TYPE } from '@sim/workflow-persistence/subblocks'
3+ import { isPlainRecord } from '@/lib/core/utils/records'
4+ import { sanitizeMalformedSubBlocks } from '@/lib/workflows/sanitization/subblocks'
25import {
36 buildCanonicalIndex ,
47 buildSubBlockValues ,
@@ -68,6 +71,7 @@ export const SUBBLOCK_ID_MIGRATIONS: Record<string, Record<string, string>> = {
6871 * Returns a new subBlocks record if anything changed, or the original if not.
6972 */
7073function migrateBlockSubblockIds (
74+ blockType : string ,
7175 subBlocks : Record < string , BlockState [ 'subBlocks' ] [ string ] > ,
7276 renames : Record < string , string >
7377) : { subBlocks : Record < string , BlockState [ 'subBlocks' ] [ string ] > ; migrated : boolean } {
@@ -83,6 +87,7 @@ function migrateBlockSubblockIds(
8387 if ( ! migrated ) return { subBlocks, migrated : false }
8488
8589 const result = { ...subBlocks }
90+ const blockConfig = getBlock ( blockType )
8691
8792 for ( const [ oldId , newId ] of Object . entries ( renames ) ) {
8893 if ( ! ( oldId in result ) ) continue
@@ -93,7 +98,24 @@ function migrateBlockSubblockIds(
9398 }
9499
95100 const oldEntry = result [ oldId ]
96- 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 ] )
97119 delete result [ oldId ]
98120 }
99121
@@ -112,20 +134,28 @@ export function migrateSubblockIds(blocks: Record<string, BlockState>): {
112134 const result : Record < string , BlockState > = { }
113135
114136 for ( const [ blockId , block ] of Object . entries ( blocks ) ) {
115- const renames = SUBBLOCK_ID_MIGRATIONS [ block . type ]
116- if ( ! renames || ! block . subBlocks ) {
137+ if ( ! block . subBlocks ) {
117138 result [ blockId ] = block
118139 continue
119140 }
120141
121- const { subBlocks, migrated } = migrateBlockSubblockIds ( block . subBlocks , renames )
122- if ( migrated ) {
123- logger . info ( 'Migrated legacy subblock IDs' , {
124- blockId : block . id ,
125- blockType : block . type ,
126- } )
142+ const renames = SUBBLOCK_ID_MIGRATIONS [ block . type ]
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
149+
150+ if ( blockMigrated ) {
151+ if ( renamed . migrated ) {
152+ logger . info ( 'Migrated legacy subblock IDs' , {
153+ blockId : block . id ,
154+ blockType : block . type ,
155+ } )
156+ }
127157 anyMigrated = true
128- result [ blockId ] = { ...block , subBlocks }
158+ result [ blockId ] = { ...renamedBlock , subBlocks : sanitized . subBlocks }
129159 } else {
130160 result [ blockId ] = block
131161 }
0 commit comments