Skip to content

Commit 78f818f

Browse files
committed
remove duplicate code
1 parent cd1c531 commit 78f818f

File tree

1 file changed

+6
-85
lines changed

1 file changed

+6
-85
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-block-output-fields.ts

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import { useMemo } from 'react'
44
import { extractFieldsFromSchema } from '@/lib/core/utils/response-format'
5-
import { getBlockOutputPaths, getBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
5+
import {
6+
getBlockOutputPaths,
7+
getBlockOutputs,
8+
getToolOutputs,
9+
} from '@/lib/workflows/blocks/block-outputs'
610
import { TRIGGER_TYPES } from '@/lib/workflows/triggers/triggers'
711
import type { SchemaField } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/connection-blocks/components/field-item/field-item'
812
import { getBlock } from '@/blocks'
9-
import type { BlockConfig } from '@/blocks/types'
1013
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
11-
import { getTool } from '@/tools/utils'
1214

1315
const RESERVED_KEYS = new Set(['type', 'description'])
1416

@@ -24,64 +26,6 @@ const getSubBlockValue = (blockId: string, property: string): any => {
2426
return useSubBlockStore.getState().getValue(blockId, property)
2527
}
2628

27-
/**
28-
* Generates output paths for a tool-based block
29-
*/
30-
const generateToolOutputPaths = (blockConfig: BlockConfig, operation: string): string[] => {
31-
if (!blockConfig?.tools?.config?.tool) return []
32-
33-
try {
34-
const toolId = blockConfig.tools.config.tool({ operation })
35-
if (!toolId) return []
36-
37-
const toolConfig = getTool(toolId)
38-
if (!toolConfig?.outputs) return []
39-
40-
return generateOutputPaths(toolConfig.outputs)
41-
} catch {
42-
return []
43-
}
44-
}
45-
46-
/**
47-
* Recursively generates all output paths from an outputs schema
48-
*/
49-
const generateOutputPaths = (outputs: Record<string, any>, prefix = ''): string[] => {
50-
const paths: string[] = []
51-
52-
for (const [key, value] of Object.entries(outputs)) {
53-
const currentPath = prefix ? `${prefix}.${key}` : key
54-
55-
if (typeof value === 'string') {
56-
paths.push(currentPath)
57-
} else if (typeof value === 'object' && value !== null) {
58-
if ('type' in value && typeof value.type === 'string') {
59-
paths.push(currentPath)
60-
// Handle nested objects and arrays
61-
if (value.type === 'object' && value.properties) {
62-
paths.push(...generateOutputPaths(value.properties, currentPath))
63-
} else if (value.type === 'array' && value.items?.properties) {
64-
paths.push(...generateOutputPaths(value.items.properties, currentPath))
65-
} else if (
66-
value.type === 'array' &&
67-
value.items &&
68-
typeof value.items === 'object' &&
69-
!('type' in value.items)
70-
) {
71-
paths.push(...generateOutputPaths(value.items, currentPath))
72-
}
73-
} else {
74-
const subPaths = generateOutputPaths(value, currentPath)
75-
paths.push(...subPaths)
76-
}
77-
} else {
78-
paths.push(currentPath)
79-
}
80-
}
81-
82-
return paths
83-
}
84-
8529
/**
8630
* Extracts nested fields from array or object properties
8731
*/
@@ -155,26 +99,6 @@ const createFieldFromOutput = (
15599
return field
156100
}
157101

158-
/**
159-
* Gets tool outputs for a block's operation
160-
*/
161-
const getToolOutputs = (
162-
blockConfig: BlockConfig | null,
163-
operation?: string
164-
): Record<string, any> => {
165-
if (!blockConfig?.tools?.config?.tool || !operation) return {}
166-
167-
try {
168-
const toolId = blockConfig.tools.config.tool({ operation })
169-
if (!toolId) return {}
170-
171-
const toolConfig = getTool(toolId)
172-
return toolConfig?.outputs || {}
173-
} catch {
174-
return {}
175-
}
176-
}
177-
178102
interface UseBlockOutputFieldsParams {
179103
blockId: string
180104
blockType: string
@@ -299,14 +223,11 @@ export function useBlockOutputFields({
299223
baseOutputs = getBlockOutputs(blockType, mergedSubBlocks)
300224
} else {
301225
// For tool-based blocks, try to get tool outputs first
302-
const operationValue =
303-
operation ?? mergedSubBlocks?.operation?.value ?? getSubBlockValue(blockId, 'operation')
304-
const toolOutputs = operationValue ? getToolOutputs(blockConfig, operationValue) : {}
226+
const toolOutputs = blockConfig ? getToolOutputs(blockConfig, mergedSubBlocks) : {}
305227

306228
if (Object.keys(toolOutputs).length > 0) {
307229
baseOutputs = toolOutputs
308230
} else {
309-
// Use getBlockOutputs which handles inputFormat merging
310231
baseOutputs = getBlockOutputs(blockType, mergedSubBlocks, triggerMode)
311232
}
312233
}

0 commit comments

Comments
 (0)