11import type { Edge } from 'reactflow'
22import { v4 as uuidv4 } from 'uuid'
33import { getBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
4- import { mergeSubBlockValues } from '@/lib/workflows/subblocks'
4+ import { mergeSubBlockValues , mergeSubblockStateWithValues } from '@/lib/workflows/subblocks'
55import { getBlock } from '@/blocks'
66import { normalizeName } from '@/executor/constants'
77import { useSubBlockStore } from '@/stores/workflows/subblock/store'
@@ -249,11 +249,16 @@ export function mergeSubblockState(
249249 workflowId ?: string ,
250250 blockId ?: string
251251) : Record < string , BlockState > {
252- const blocksToProcess = blockId ? { [ blockId ] : blocks [ blockId ] } : blocks
253252 const subBlockStore = useSubBlockStore . getState ( )
254253
255254 const workflowSubblockValues = workflowId ? subBlockStore . workflowValues [ workflowId ] || { } : { }
256255
256+ if ( workflowId ) {
257+ return mergeSubblockStateWithValues ( blocks , workflowSubblockValues , blockId )
258+ }
259+
260+ const blocksToProcess = blockId ? { [ blockId ] : blocks [ blockId ] } : blocks
261+
257262 return Object . entries ( blocksToProcess ) . reduce (
258263 ( acc , [ id , block ] ) => {
259264 if ( ! block ) {
@@ -332,9 +337,15 @@ export async function mergeSubblockStateAsync(
332337 workflowId ?: string ,
333338 blockId ?: string
334339) : Promise < Record < string , BlockState > > {
335- const blocksToProcess = blockId ? { [ blockId ] : blocks [ blockId ] } : blocks
336340 const subBlockStore = useSubBlockStore . getState ( )
337341
342+ if ( workflowId ) {
343+ const workflowValues = subBlockStore . workflowValues [ workflowId ] || { }
344+ return mergeSubblockStateWithValues ( blocks , workflowValues , blockId )
345+ }
346+
347+ const blocksToProcess = blockId ? { [ blockId ] : blocks [ blockId ] } : blocks
348+
338349 // Process blocks in parallel for better performance
339350 const processedBlockEntries = await Promise . all (
340351 Object . entries ( blocksToProcess ) . map ( async ( [ id , block ] ) => {
@@ -351,16 +362,7 @@ export async function mergeSubblockStateAsync(
351362 return null
352363 }
353364
354- let storedValue = null
355-
356- if ( workflowId ) {
357- const workflowValues = subBlockStore . workflowValues [ workflowId ]
358- if ( workflowValues ?. [ id ] ) {
359- storedValue = workflowValues [ id ] [ subBlockId ]
360- }
361- } else {
362- storedValue = subBlockStore . getValue ( id , subBlockId )
363- }
365+ const storedValue = subBlockStore . getValue ( id , subBlockId )
364366
365367 return [
366368 subBlockId ,
@@ -379,23 +381,6 @@ export async function mergeSubblockStateAsync(
379381 subBlockEntries . filter ( ( entry ) : entry is readonly [ string , SubBlockState ] => entry !== null )
380382 ) as Record < string , SubBlockState >
381383
382- // Add any values that exist in the store but aren't in the block structure
383- // This handles cases where block config has been updated but values still exist
384- // IMPORTANT: This includes runtime subblock IDs like webhookId, triggerPath, etc.
385- if ( workflowId ) {
386- const workflowValues = subBlockStore . workflowValues [ workflowId ]
387- const blockValues = workflowValues ?. [ id ] || { }
388- Object . entries ( blockValues ) . forEach ( ( [ subBlockId , value ] ) => {
389- if ( ! mergedSubBlocks [ subBlockId ] && value !== null && value !== undefined ) {
390- mergedSubBlocks [ subBlockId ] = {
391- id : subBlockId ,
392- type : 'short-input' ,
393- value : value as SubBlockState [ 'value' ] ,
394- }
395- }
396- } )
397- }
398-
399384 // Return the full block state with updated subBlocks (including orphaned values)
400385 return [
401386 id ,
0 commit comments