|
| 1 | +import { |
| 2 | + extractFieldsFromSchema, |
| 3 | + parseResponseFormatSafely, |
| 4 | +} from '@/lib/core/utils/response-format' |
| 5 | +import { getBlockOutputPaths } from '@/lib/workflows/blocks/block-outputs' |
| 6 | +import { getBlock } from '@/blocks' |
| 7 | +import { useVariablesStore } from '@/stores/panel/variables/store' |
| 8 | +import type { Variable } from '@/stores/panel/variables/types' |
| 9 | +import { useSubBlockStore } from '@/stores/workflows/subblock/store' |
| 10 | +import { normalizeName } from '@/stores/workflows/utils' |
| 11 | +import type { BlockState, Loop, Parallel } from '@/stores/workflows/workflow/types' |
| 12 | + |
| 13 | +export interface WorkflowContext { |
| 14 | + workflowId: string |
| 15 | + blocks: Record<string, BlockState> |
| 16 | + loops: Record<string, Loop> |
| 17 | + parallels: Record<string, Parallel> |
| 18 | + subBlockValues: Record<string, Record<string, any>> |
| 19 | +} |
| 20 | + |
| 21 | +export interface VariableOutput { |
| 22 | + id: string |
| 23 | + name: string |
| 24 | + type: string |
| 25 | + tag: string |
| 26 | +} |
| 27 | + |
| 28 | +export function getWorkflowSubBlockValues(workflowId: string): Record<string, Record<string, any>> { |
| 29 | + const subBlockStore = useSubBlockStore.getState() |
| 30 | + return subBlockStore.workflowValues[workflowId] ?? {} |
| 31 | +} |
| 32 | + |
| 33 | +export function getMergedSubBlocks( |
| 34 | + blocks: Record<string, BlockState>, |
| 35 | + subBlockValues: Record<string, Record<string, any>>, |
| 36 | + targetBlockId: string |
| 37 | +): Record<string, any> { |
| 38 | + const base = blocks[targetBlockId]?.subBlocks || {} |
| 39 | + const live = subBlockValues?.[targetBlockId] || {} |
| 40 | + const merged: Record<string, any> = { ...base } |
| 41 | + for (const [subId, liveVal] of Object.entries(live)) { |
| 42 | + merged[subId] = { ...(base[subId] || {}), value: liveVal } |
| 43 | + } |
| 44 | + return merged |
| 45 | +} |
| 46 | + |
| 47 | +export function getSubBlockValue( |
| 48 | + blocks: Record<string, BlockState>, |
| 49 | + subBlockValues: Record<string, Record<string, any>>, |
| 50 | + targetBlockId: string, |
| 51 | + subBlockId: string |
| 52 | +): any { |
| 53 | + const live = subBlockValues?.[targetBlockId]?.[subBlockId] |
| 54 | + if (live !== undefined) return live |
| 55 | + return blocks[targetBlockId]?.subBlocks?.[subBlockId]?.value |
| 56 | +} |
| 57 | + |
| 58 | +export function getWorkflowVariables(workflowId: string): VariableOutput[] { |
| 59 | + const getVariablesByWorkflowId = useVariablesStore.getState().getVariablesByWorkflowId |
| 60 | + const workflowVariables = getVariablesByWorkflowId(workflowId) |
| 61 | + const validVariables = workflowVariables.filter( |
| 62 | + (variable: Variable) => variable.name.trim() !== '' |
| 63 | + ) |
| 64 | + return validVariables.map((variable: Variable) => ({ |
| 65 | + id: variable.id, |
| 66 | + name: variable.name, |
| 67 | + type: variable.type, |
| 68 | + tag: `variable.${normalizeName(variable.name)}`, |
| 69 | + })) |
| 70 | +} |
| 71 | + |
| 72 | +export function getSubflowInsidePaths( |
| 73 | + blockType: 'loop' | 'parallel', |
| 74 | + blockId: string, |
| 75 | + loops: Record<string, Loop>, |
| 76 | + parallels: Record<string, Parallel> |
| 77 | +): string[] { |
| 78 | + const paths = ['index'] |
| 79 | + if (blockType === 'loop') { |
| 80 | + const loopType = loops[blockId]?.loopType || 'for' |
| 81 | + if (loopType === 'forEach') { |
| 82 | + paths.push('currentItem', 'items') |
| 83 | + } |
| 84 | + } else { |
| 85 | + const parallelType = parallels[blockId]?.parallelType || 'count' |
| 86 | + if (parallelType === 'collection') { |
| 87 | + paths.push('currentItem', 'items') |
| 88 | + } |
| 89 | + } |
| 90 | + return paths |
| 91 | +} |
| 92 | + |
| 93 | +export function computeBlockOutputPaths(block: BlockState, ctx: WorkflowContext): string[] { |
| 94 | + const { blocks, loops, parallels, subBlockValues } = ctx |
| 95 | + const blockConfig = getBlock(block.type) |
| 96 | + const mergedSubBlocks = getMergedSubBlocks(blocks, subBlockValues, block.id) |
| 97 | + |
| 98 | + if (block.type === 'loop' || block.type === 'parallel') { |
| 99 | + const insidePaths = getSubflowInsidePaths(block.type, block.id, loops, parallels) |
| 100 | + return ['results', ...insidePaths] |
| 101 | + } |
| 102 | + |
| 103 | + if (block.type === 'evaluator') { |
| 104 | + const metricsValue = getSubBlockValue(blocks, subBlockValues, block.id, 'metrics') |
| 105 | + if (metricsValue && Array.isArray(metricsValue) && metricsValue.length > 0) { |
| 106 | + const validMetrics = metricsValue.filter((metric: { name?: string }) => metric?.name) |
| 107 | + return validMetrics.map((metric: { name: string }) => metric.name.toLowerCase()) |
| 108 | + } |
| 109 | + return getBlockOutputPaths(block.type, mergedSubBlocks) |
| 110 | + } |
| 111 | + |
| 112 | + if (block.type === 'variables') { |
| 113 | + const variablesValue = getSubBlockValue(blocks, subBlockValues, block.id, 'variables') |
| 114 | + if (variablesValue && Array.isArray(variablesValue) && variablesValue.length > 0) { |
| 115 | + const validAssignments = variablesValue.filter((assignment: { variableName?: string }) => |
| 116 | + assignment?.variableName?.trim() |
| 117 | + ) |
| 118 | + return validAssignments.map((assignment: { variableName: string }) => |
| 119 | + assignment.variableName.trim() |
| 120 | + ) |
| 121 | + } |
| 122 | + return [] |
| 123 | + } |
| 124 | + |
| 125 | + if (blockConfig) { |
| 126 | + const responseFormatValue = mergedSubBlocks?.responseFormat?.value |
| 127 | + const responseFormat = parseResponseFormatSafely(responseFormatValue, block.id) |
| 128 | + if (responseFormat) { |
| 129 | + const schemaFields = extractFieldsFromSchema(responseFormat) |
| 130 | + if (schemaFields.length > 0) { |
| 131 | + return schemaFields.map((field) => field.name) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return getBlockOutputPaths(block.type, mergedSubBlocks, block.triggerMode) |
| 137 | +} |
| 138 | + |
| 139 | +export function formatOutputsWithPrefix(paths: string[], blockName: string): string[] { |
| 140 | + const normalizedName = normalizeName(blockName) |
| 141 | + return paths.map((path) => `${normalizedName}.${path}`) |
| 142 | +} |
0 commit comments