11import { shallow } from 'zustand/shallow'
22import { BlockPathCalculator } from '@/lib/block-path-calculator'
33import { createLogger } from '@/lib/logs/console/logger'
4+ import { getBlockOutputs } from '@/lib/workflows/block-outputs'
5+ import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
46import { useSubBlockStore } from '@/stores/workflows/subblock/store'
57import { useWorkflowStore } from '@/stores/workflows/workflow/store'
68
@@ -92,6 +94,22 @@ export function useBlockConnections(blockId: string) {
9294 shallow
9395 )
9496
97+ const workflowId = useWorkflowRegistry ( ( state ) => state . activeWorkflowId )
98+ const workflowSubBlockValues = useSubBlockStore ( ( state ) =>
99+ workflowId ? ( state . workflowValues [ workflowId ] ?? { } ) : { }
100+ )
101+
102+ // Helper function to merge block subBlocks with live values from subblock store
103+ const getMergedSubBlocks = ( sourceBlockId : string ) : Record < string , any > => {
104+ const base = blocks [ sourceBlockId ] ?. subBlocks || { }
105+ const live = workflowSubBlockValues ?. [ sourceBlockId ] || { }
106+ const merged : Record < string , any > = { ...base }
107+ for ( const [ subId , liveVal ] of Object . entries ( live ) ) {
108+ merged [ subId ] = { ...( base [ subId ] || { } ) , value : liveVal }
109+ }
110+ return merged
111+ }
112+
95113 // Find all blocks along paths leading to this block
96114 const allPathNodeIds = BlockPathCalculator . findAllPathNodes ( edges , blockId )
97115
@@ -101,20 +119,37 @@ export function useBlockConnections(blockId: string) {
101119 const sourceBlock = blocks [ sourceId ]
102120 if ( ! sourceBlock ) return null
103121
122+ // Get merged subblocks for this source block
123+ const mergedSubBlocks = getMergedSubBlocks ( sourceId )
124+
104125 // Get the response format from the subblock store
105126 const responseFormatValue = useSubBlockStore . getState ( ) . getValue ( sourceId , 'responseFormat' )
106127
107128 // Safely parse response format with proper error handling
108129 const responseFormat = parseResponseFormatSafely ( responseFormatValue , sourceId )
109130
110- // Get the default output type from the block's outputs
111- const defaultOutputs : Field [ ] = Object . entries ( sourceBlock . outputs || { } ) . map ( ( [ key ] ) => ( {
112- name : key ,
113- type : 'string' ,
114- } ) )
115-
116- // Extract fields from the response format using our helper function
117- const outputFields = responseFormat ? extractFieldsFromSchema ( responseFormat ) : defaultOutputs
131+ // Use getBlockOutputs to properly handle dynamic outputs from inputFormat
132+ const blockOutputs = getBlockOutputs (
133+ sourceBlock . type ,
134+ mergedSubBlocks ,
135+ sourceBlock . triggerMode
136+ )
137+
138+ // Extract fields from the response format if available, otherwise use block outputs
139+ let outputFields : Field [ ]
140+ if ( responseFormat ) {
141+ outputFields = extractFieldsFromSchema ( responseFormat )
142+ } else {
143+ // Convert block outputs to field format
144+ outputFields = Object . entries ( blockOutputs ) . map ( ( [ key , value ] : [ string , any ] ) => ( {
145+ name : key ,
146+ type : value && typeof value === 'object' && 'type' in value ? value . type : 'string' ,
147+ description :
148+ value && typeof value === 'object' && 'description' in value
149+ ? value . description
150+ : undefined ,
151+ } ) )
152+ }
118153
119154 return {
120155 id : sourceBlock . id ,
@@ -133,6 +168,9 @@ export function useBlockConnections(blockId: string) {
133168 const sourceBlock = blocks [ edge . source ]
134169 if ( ! sourceBlock ) return null
135170
171+ // Get merged subblocks for this source block
172+ const mergedSubBlocks = getMergedSubBlocks ( edge . source )
173+
136174 // Get the response format from the subblock store instead
137175 const responseFormatValue = useSubBlockStore
138176 . getState ( )
@@ -141,14 +179,28 @@ export function useBlockConnections(blockId: string) {
141179 // Safely parse response format with proper error handling
142180 const responseFormat = parseResponseFormatSafely ( responseFormatValue , edge . source )
143181
144- // Get the default output type from the block's outputs
145- const defaultOutputs : Field [ ] = Object . entries ( sourceBlock . outputs || { } ) . map ( ( [ key ] ) => ( {
146- name : key ,
147- type : 'string' ,
148- } ) )
149-
150- // Extract fields from the response format using our helper function
151- const outputFields = responseFormat ? extractFieldsFromSchema ( responseFormat ) : defaultOutputs
182+ // Use getBlockOutputs to properly handle dynamic outputs from inputFormat
183+ const blockOutputs = getBlockOutputs (
184+ sourceBlock . type ,
185+ mergedSubBlocks ,
186+ sourceBlock . triggerMode
187+ )
188+
189+ // Extract fields from the response format if available, otherwise use block outputs
190+ let outputFields : Field [ ]
191+ if ( responseFormat ) {
192+ outputFields = extractFieldsFromSchema ( responseFormat )
193+ } else {
194+ // Convert block outputs to field format
195+ outputFields = Object . entries ( blockOutputs ) . map ( ( [ key , value ] : [ string , any ] ) => ( {
196+ name : key ,
197+ type : value && typeof value === 'object' && 'type' in value ? value . type : 'string' ,
198+ description :
199+ value && typeof value === 'object' && 'description' in value
200+ ? value . description
201+ : undefined ,
202+ } ) )
203+ }
152204
153205 return {
154206 id : sourceBlock . id ,
0 commit comments