1- import { getBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
21import { normalizeName } from '@/executor/constants'
32import type { ExecutionContext } from '@/executor/types'
43import type { OutputSchema } from '@/executor/utils/block-reference'
4+ import type { SerializedBlock } from '@/serializer/types'
5+ import type { ToolConfig } from '@/tools/types'
6+ import { getTool } from '@/tools/utils'
57
68export interface BlockDataCollection {
79 blockData : Record < string , unknown >
810 blockNameMapping : Record < string , string >
911 blockOutputSchemas : Record < string , OutputSchema >
1012}
1113
14+ export function getBlockSchema (
15+ block : SerializedBlock ,
16+ toolConfig ?: ToolConfig
17+ ) : OutputSchema | undefined {
18+ const isTrigger =
19+ block . metadata ?. category === 'triggers' ||
20+ ( block . config ?. params as Record < string , unknown > | undefined ) ?. triggerMode === true
21+
22+ // Triggers use saved outputs (defines the trigger payload schema)
23+ if ( isTrigger && block . outputs && Object . keys ( block . outputs ) . length > 0 ) {
24+ return block . outputs as OutputSchema
25+ }
26+
27+ // When a tool is selected, tool outputs are the source of truth
28+ if ( toolConfig ?. outputs && Object . keys ( toolConfig . outputs ) . length > 0 ) {
29+ return toolConfig . outputs as OutputSchema
30+ }
31+
32+ // Fallback to saved outputs for blocks without tools
33+ if ( block . outputs && Object . keys ( block . outputs ) . length > 0 ) {
34+ return block . outputs as OutputSchema
35+ }
36+
37+ return undefined
38+ }
39+
1240export function collectBlockData ( ctx : ExecutionContext ) : BlockDataCollection {
1341 const blockData : Record < string , unknown > = { }
1442 const blockNameMapping : Record < string , string > = { }
@@ -18,24 +46,21 @@ export function collectBlockData(ctx: ExecutionContext): BlockDataCollection {
1846 if ( state . output !== undefined ) {
1947 blockData [ id ] = state . output
2048 }
49+ }
2150
22- const workflowBlock = ctx . workflow ?. blocks ?. find ( ( b ) => b . id === id )
23- if ( ! workflowBlock ) continue
51+ const workflowBlocks = ctx . workflow ?. blocks ?? [ ]
52+ for ( const block of workflowBlocks ) {
53+ const id = block . id
2454
25- if ( workflowBlock . metadata ?. name ) {
26- blockNameMapping [ normalizeName ( workflowBlock . metadata . name ) ] = id
55+ if ( block . metadata ?. name ) {
56+ blockNameMapping [ normalizeName ( block . metadata . name ) ] = id
2757 }
2858
29- const blockType = workflowBlock . metadata ?. id
30- if ( blockType ) {
31- const params = workflowBlock . config ?. params as Record < string , unknown > | undefined
32- const subBlocks = params
33- ? Object . fromEntries ( Object . entries ( params ) . map ( ( [ k , v ] ) => [ k , { value : v } ] ) )
34- : undefined
35- const schema = getBlockOutputs ( blockType , subBlocks )
36- if ( schema && Object . keys ( schema ) . length > 0 ) {
37- blockOutputSchemas [ id ] = schema
38- }
59+ const toolId = block . config ?. tool
60+ const toolConfig = toolId ? getTool ( toolId ) : undefined
61+ const schema = getBlockSchema ( block , toolConfig )
62+ if ( schema && Object . keys ( schema ) . length > 0 ) {
63+ blockOutputSchemas [ id ] = schema
3964 }
4065 }
4166
0 commit comments