@@ -4,6 +4,7 @@ import { devtools } from 'zustand/middleware'
44import { createLogger } from '@/lib/logs/console/logger'
55import { getBlockOutputs } from '@/lib/workflows/block-outputs'
66import { getBlock } from '@/blocks'
7+ import type { SubBlockConfig } from '@/blocks/types'
78import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
89import { useSubBlockStore } from '@/stores/workflows/subblock/store'
910import {
@@ -21,6 +22,73 @@ import { generateLoopBlocks, generateParallelBlocks } from '@/stores/workflows/w
2122
2223const logger = createLogger ( 'WorkflowStore' )
2324
25+ /**
26+ * Creates a deep clone of an initial sub-block value to avoid shared references.
27+ *
28+ * @param value - The value to clone.
29+ * @returns A cloned value suitable for initializing sub-block state.
30+ */
31+ function cloneInitialSubblockValue ( value : unknown ) : unknown {
32+ if ( Array . isArray ( value ) ) {
33+ return value . map ( ( item ) => cloneInitialSubblockValue ( item ) )
34+ }
35+
36+ if ( value && typeof value === 'object' ) {
37+ return Object . entries ( value as Record < string , unknown > ) . reduce < Record < string , unknown > > (
38+ ( acc , [ key , entry ] ) => {
39+ acc [ key ] = cloneInitialSubblockValue ( entry )
40+ return acc
41+ } ,
42+ { }
43+ )
44+ }
45+
46+ return value ?? null
47+ }
48+
49+ /**
50+ * Resolves the initial value for a sub-block based on its configuration.
51+ *
52+ * @param config - The sub-block configuration.
53+ * @returns The resolved initial value or null when no defaults are defined.
54+ */
55+ function resolveInitialSubblockValue ( config : SubBlockConfig ) : unknown {
56+ if ( typeof config . value === 'function' ) {
57+ try {
58+ const resolved = config . value ( { } )
59+ return cloneInitialSubblockValue ( resolved )
60+ } catch ( error ) {
61+ logger . warn ( 'Failed to resolve dynamic sub-block default value' , {
62+ subBlockId : config . id ,
63+ error : error instanceof Error ? error . message : String ( error ) ,
64+ } )
65+ }
66+ }
67+
68+ if ( config . defaultValue !== undefined ) {
69+ return cloneInitialSubblockValue ( config . defaultValue )
70+ }
71+
72+ if ( config . type === 'input-format' ) {
73+ // Initialize with a default field structure instead of empty array
74+ return [
75+ {
76+ id : crypto . randomUUID ( ) ,
77+ name : '' ,
78+ type : 'string' ,
79+ value : '' ,
80+ collapsed : false ,
81+ } ,
82+ ]
83+ }
84+
85+ if ( config . type === 'table' ) {
86+ return [ ]
87+ }
88+
89+ return null
90+ }
91+
2492const initialState = {
2593 blocks : { } ,
2694 edges : [ ] ,
@@ -106,12 +174,40 @@ export const useWorkflowStore = create<WorkflowStore>()(
106174 }
107175
108176 const subBlocks : Record < string , SubBlockState > = { }
177+ const subBlockStore = useSubBlockStore . getState ( )
178+ const activeWorkflowId = useWorkflowRegistry . getState ( ) . activeWorkflowId
179+
109180 blockConfig . subBlocks . forEach ( ( subBlock ) => {
110181 const subBlockId = subBlock . id
182+ const initialValue = resolveInitialSubblockValue ( subBlock )
183+ const normalizedValue =
184+ initialValue !== undefined && initialValue !== null ? initialValue : null
185+
111186 subBlocks [ subBlockId ] = {
112187 id : subBlockId ,
113188 type : subBlock . type ,
114- value : null ,
189+ value : normalizedValue as SubBlockState [ 'value' ] ,
190+ }
191+
192+ // Always initialize the subblock store value, even if it's null or empty array
193+ // This ensures the editor panel can read the value correctly
194+ if ( activeWorkflowId ) {
195+ try {
196+ // Use the initial value if available, otherwise use null
197+ const valueToStore = initialValue !== undefined ? cloneInitialSubblockValue ( initialValue ) : null
198+ subBlockStore . setValue ( id , subBlockId , valueToStore )
199+ } catch ( error ) {
200+ logger . warn ( 'Failed to seed sub-block store value during block creation' , {
201+ blockId : id ,
202+ subBlockId,
203+ error : error instanceof Error ? error . message : String ( error ) ,
204+ } )
205+ }
206+ } else {
207+ logger . warn ( 'Cannot seed sub-block store value: activeWorkflowId not available' , {
208+ blockId : id ,
209+ subBlockId,
210+ } )
115211 }
116212 } )
117213
0 commit comments