11import { createLogger } from '@sim/logger'
22import type { Edge } from 'reactflow'
33import { BlockPathCalculator } from '@/lib/workflows/blocks/block-path-calculator'
4+ import type { CanonicalModeOverrides } from '@/lib/workflows/subblocks/visibility'
45import {
56 buildCanonicalIndex ,
67 buildSubBlockValues ,
78 evaluateSubBlockCondition ,
89 getCanonicalValues ,
10+ isCanonicalPair ,
911 isNonEmptyValue ,
1012 isSubBlockFeatureEnabled ,
13+ resolveCanonicalMode ,
1114} from '@/lib/workflows/subblocks/visibility'
1215import { getBlock } from '@/blocks'
1316import type { SubBlockConfig } from '@/blocks/types'
@@ -42,7 +45,8 @@ function shouldSerializeSubBlock(
4245 displayAdvancedOptions : boolean ,
4346 isTriggerContext : boolean ,
4447 isTriggerCategory : boolean ,
45- canonicalIndex : ReturnType < typeof buildCanonicalIndex >
48+ canonicalIndex : ReturnType < typeof buildCanonicalIndex > ,
49+ canonicalModeOverrides ?: CanonicalModeOverrides
4650) : boolean {
4751 if ( ! isSubBlockFeatureEnabled ( subBlockConfig ) ) return false
4852
@@ -54,6 +58,18 @@ function shouldSerializeSubBlock(
5458
5559 const isCanonicalMember = Boolean ( canonicalIndex . canonicalIdBySubBlockId [ subBlockConfig . id ] )
5660 if ( isCanonicalMember ) {
61+ const canonicalId = canonicalIndex . canonicalIdBySubBlockId [ subBlockConfig . id ]
62+ const group = canonicalId ? canonicalIndex . groupsById [ canonicalId ] : undefined
63+ if ( group && isCanonicalPair ( group ) ) {
64+ const mode =
65+ canonicalModeOverrides ?. [ group . canonicalId ] ??
66+ ( displayAdvancedOptions ? 'advanced' : resolveCanonicalMode ( group , values ) )
67+ const matchesMode =
68+ mode === 'advanced'
69+ ? group . advancedIds . includes ( subBlockConfig . id )
70+ : group . basicId === subBlockConfig . id
71+ return matchesMode && evaluateSubBlockCondition ( subBlockConfig . condition , values )
72+ }
5773 return evaluateSubBlockCondition ( subBlockConfig . condition , values )
5874 }
5975
@@ -327,7 +343,8 @@ export class Serializer {
327343 legacyAdvancedMode ,
328344 isTriggerContext ,
329345 isTriggerCategory ,
330- canonicalIndex
346+ canonicalIndex ,
347+ canonicalModeOverrides
331348 )
332349 )
333350
@@ -351,7 +368,8 @@ export class Serializer {
351368 legacyAdvancedMode ,
352369 isTriggerContext ,
353370 isTriggerCategory ,
354- canonicalIndex
371+ canonicalIndex ,
372+ canonicalModeOverrides
355373 )
356374 ) {
357375 params [ id ] = subBlockConfig . value ( params )
@@ -365,9 +383,7 @@ export class Serializer {
365383 const chosen = pairMode === 'advanced' ? advancedValue : basicValue
366384
367385 const sourceIds = [ group . basicId , ...group . advancedIds ] . filter ( Boolean ) as string [ ]
368- sourceIds . forEach ( ( id ) => {
369- if ( id !== group . canonicalId ) delete params [ id ]
370- } )
386+ sourceIds . forEach ( ( id ) => delete params [ id ] )
371387
372388 if ( chosen !== undefined ) {
373389 params [ group . canonicalId ] = chosen
@@ -420,6 +436,8 @@ export class Serializer {
420436 const isTriggerContext = block . triggerMode ?? false
421437 const isTriggerCategory = blockConfig . category === 'triggers'
422438 const canonicalIndex = buildCanonicalIndex ( blockConfig . subBlocks || [ ] )
439+ const canonicalModeOverrides = block . data ?. canonicalModes
440+ const allValues = buildSubBlockValues ( block . subBlocks )
423441
424442 // Iterate through the tool's parameters, not the block's subBlocks
425443 Object . entries ( currentTool . params || { } ) . forEach ( ( [ paramId , paramConfig ] ) => {
@@ -432,11 +450,12 @@ export class Serializer {
432450 shouldValidateParam = matchingConfigs . some ( ( subBlockConfig : any ) => {
433451 const includedByMode = shouldSerializeSubBlock (
434452 subBlockConfig ,
435- params ,
453+ allValues ,
436454 displayAdvancedOptions ,
437455 isTriggerContext ,
438456 isTriggerCategory ,
439- canonicalIndex
457+ canonicalIndex ,
458+ canonicalModeOverrides
440459 )
441460
442461 const isRequired = ( ( ) => {
@@ -458,11 +477,12 @@ export class Serializer {
458477 const activeConfig = matchingConfigs . find ( ( config : any ) =>
459478 shouldSerializeSubBlock (
460479 config ,
461- params ,
480+ allValues ,
462481 displayAdvancedOptions ,
463482 isTriggerContext ,
464483 isTriggerCategory ,
465- canonicalIndex
484+ canonicalIndex ,
485+ canonicalModeOverrides
466486 )
467487 )
468488 const displayName = activeConfig ?. title || paramId
0 commit comments