diff --git a/app/w/components/workflow-block/components/connection-block/connection-block.tsx b/app/w/components/workflow-block/components/connection-block/connection-block.tsx index 85b517d365..228961f0c4 100644 --- a/app/w/components/workflow-block/components/connection-block/connection-block.tsx +++ b/app/w/components/workflow-block/components/connection-block/connection-block.tsx @@ -23,7 +23,7 @@ export function ConnectionBlock({ blockId }: ConnectionBlockProps) { {connection.name.replace(' ', '').toLowerCase()} - .{connection.outputType} + .{connection.outputType === 'any' ? 'res' : connection.outputType} diff --git a/blocks/utils.ts b/blocks/utils.ts new file mode 100644 index 0000000000..389303ea22 --- /dev/null +++ b/blocks/utils.ts @@ -0,0 +1,44 @@ +import { BlockState, SubBlockState } from '@/stores/workflow/types' +import { OutputTypeConfig, OutputType } from '@/blocks/types' + +interface CodeLine { + id: string + content: string +} + +function isEmptyValue(value: SubBlockState['value']): boolean { + if (value === null || value === undefined) return true + if (typeof value === 'string') return value.trim() === '' + if (typeof value === 'number') return false + if (Array.isArray(value)) { + // Handle code editor's array of lines format + if (value.length === 0) return true + if (isCodeEditorValue(value)) { + return value.every((line: any) => !line.content.trim()) + } + return value.length === 0 + } + return false +} + +function isCodeEditorValue(value: any[]): value is CodeLine[] { + return value.length > 0 && 'id' in value[0] && 'content' in value[0] +} + +export function resolveOutputType( + outputTypeConfig: OutputTypeConfig, + subBlocks: Record +): OutputType { + // If outputType is a string, return it directly + if (typeof outputTypeConfig === 'string') { + return outputTypeConfig + } + + // Handle dependent output types + const { dependsOn } = outputTypeConfig + const subBlock = subBlocks[dependsOn.subBlockId] + + return isEmptyValue(subBlock?.value) + ? dependsOn.condition.whenEmpty + : dependsOn.condition.whenFilled +} \ No newline at end of file diff --git a/stores/workflow/workflow-store.ts b/stores/workflow/workflow-store.ts index e0c91d872b..23f59d17e7 100644 --- a/stores/workflow/workflow-store.ts +++ b/stores/workflow/workflow-store.ts @@ -4,6 +4,7 @@ import { Edge } from 'reactflow' import { Position, SubBlockState, WorkflowStore } from './types' import { getBlock } from '@/blocks' import { withHistory, WorkflowStoreWithHistory, pushHistory } from './history-middleware' +import { resolveOutputType } from '@/blocks/utils' const initialState = { blocks: {}, @@ -29,21 +30,38 @@ export const useWorkflowStore = create()( canRedo: () => false, updateSubBlock: (blockId: string, subBlockId: string, value: any) => { - set((state) => ({ - blocks: { - ...state.blocks, - [blockId]: { - ...state.blocks[blockId], - subBlocks: { - ...state.blocks[blockId].subBlocks, - [subBlockId]: { - ...state.blocks[blockId].subBlocks[subBlockId], - value, - }, + set((state) => { + const block = state.blocks[blockId] + const blockConfig = getBlock(block.type) + + if (!blockConfig) return state + + // Create new subBlocks state + const newSubBlocks = { + ...block.subBlocks, + [subBlockId]: { + ...block.subBlocks[subBlockId], + value, + }, + } + + // Resolve new output type + const newOutputType = resolveOutputType( + blockConfig.workflow.outputType, + newSubBlocks + ) + + return { + blocks: { + ...state.blocks, + [blockId]: { + ...block, + subBlocks: newSubBlocks, + outputType: newOutputType, }, }, - }, - })) + } + }) }, addBlock: (id: string, type: string, name: string, position: Position) => {