|
| 1 | +import { Position, type Node } from '@vue-flow/core' |
| 2 | +import { |
| 3 | + BridgeType, |
| 4 | + FilterFormData, |
| 5 | + FilterFormType, |
| 6 | + FlowNodeType, |
| 7 | + FrontendSinkType, |
| 8 | + FrontendSourceType, |
| 9 | + NodeItem, |
| 10 | + NodeType, |
| 11 | + PositionData, |
| 12 | + ProcessingType, |
| 13 | +} from '../types' |
| 14 | +import { useFlowLocale } from './useFlowLocale' |
| 15 | + |
| 16 | +export const isNotBridgeSourceTypes = [FrontendSourceType.Event, FrontendSourceType.Message] |
| 17 | + |
| 18 | +/** |
| 19 | + * Cannot be added, only for show webhook |
| 20 | + */ |
| 21 | +export const SourceTypeAllMsgsAndEvents = 'all-msgs-and-events' |
| 22 | + |
| 23 | +/** |
| 24 | + * Because the exact type of the ai node needs to be known after the details are fetched, |
| 25 | + * in order to treat the data as an ai node when processing it, assign a placeholder to it first. |
| 26 | + */ |
| 27 | +export const AI_PLACEHOLDER_TYPE = 'ai-placeholder' |
| 28 | + |
| 29 | +export default (): { |
| 30 | + nodeWidth: number |
| 31 | + processingNodeList: Array<NodeItem> |
| 32 | + getNodeClass: (type: NodeType) => string |
| 33 | + getFlowNodeHookPosition: (nodeType: FlowNodeType) => PositionData |
| 34 | + getTypeCommonData: (type: NodeType) => { type: FlowNodeType; class: string } & PositionData |
| 35 | + isBridgerNode: (node: Partial<Node>) => boolean |
| 36 | + isActionBridgeNode: (node: Partial<Node>) => boolean |
| 37 | + isWithFallbackNodes: (node: Node) => boolean |
| 38 | + isBridgeType: (type: string) => boolean |
| 39 | + isAIType: (type: string) => boolean |
| 40 | + isLikeFunctionType: (type: string) => boolean |
| 41 | + getCommonTypeLabel: (specificType: string) => string |
| 42 | + getNodeInfoFunc: (node: Node, getEventLabel: (event: string) => string) => string |
| 43 | +} => { |
| 44 | + const { t } = useFlowLocale() |
| 45 | + |
| 46 | + /** |
| 47 | + * just record, not for setting |
| 48 | + */ |
| 49 | + const nodeWidth = 200 |
| 50 | + |
| 51 | + const nodeClassMap: Record<NodeType, string> = { |
| 52 | + [NodeType.Source]: 'node-source', |
| 53 | + [NodeType.Processing]: 'node-processing', |
| 54 | + [NodeType.Sink]: 'node-sink', |
| 55 | + [NodeType.Fallback]: 'node-sink', |
| 56 | + } |
| 57 | + const getNodeClass = (type: NodeType) => nodeClassMap[type] |
| 58 | + |
| 59 | + const getFlowNodeHookPosition = (nodeType: FlowNodeType) => { |
| 60 | + if (nodeType === FlowNodeType.Input) { |
| 61 | + return { sourcePosition: Position.Right } |
| 62 | + } |
| 63 | + if (nodeType === FlowNodeType.Output) { |
| 64 | + return { targetPosition: Position.Left } |
| 65 | + } |
| 66 | + return { sourcePosition: Position.Right, targetPosition: Position.Left } |
| 67 | + } |
| 68 | + |
| 69 | + const typeMap = { |
| 70 | + [NodeType.Source]: FlowNodeType.Input, |
| 71 | + [NodeType.Processing]: FlowNodeType.Default, |
| 72 | + [NodeType.Sink]: FlowNodeType.Output, |
| 73 | + [NodeType.Fallback]: FlowNodeType.Output, |
| 74 | + } |
| 75 | + const getTypeCommonData = (type: NodeType) => { |
| 76 | + const flowNodeType = typeMap[type] |
| 77 | + return { |
| 78 | + class: `node-item ${getNodeClass(type)}`, |
| 79 | + type: flowNodeType, |
| 80 | + ...getFlowNodeHookPosition(flowNodeType), |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const commonTypeLabelMap: Record<string, string> = { |
| 85 | + [FrontendSourceType.Message]: t('flow.message'), |
| 86 | + [FrontendSourceType.Event]: t('flow.event'), |
| 87 | + [ProcessingType.Function]: t('flow.dataProcessing'), |
| 88 | + [ProcessingType.Filter]: t('flow.filter'), |
| 89 | + [ProcessingType.AIOpenAI]: 'OpenAI', |
| 90 | + [ProcessingType.AIAnthropic]: 'Anthropic', |
| 91 | + [ProcessingType.AIGemini]: 'Gemini', |
| 92 | + [FrontendSinkType.Console]: t('flow.consoleOutput'), |
| 93 | + [FrontendSinkType.Republish]: t('flow.republish'), |
| 94 | + } |
| 95 | + const getCommonTypeLabel = (specificType: string): string => { |
| 96 | + return commonTypeLabelMap[specificType] || '' |
| 97 | + } |
| 98 | + |
| 99 | + const countFiltersNum = (filter: FilterFormData) => { |
| 100 | + return filter.items.reduce((count, item) => { |
| 101 | + if ('items' in item) { |
| 102 | + count += countFiltersNum(item) |
| 103 | + } else { |
| 104 | + count += 1 |
| 105 | + } |
| 106 | + return count |
| 107 | + }, 0) |
| 108 | + } |
| 109 | + |
| 110 | + const isNotBridgeSourceNodeTypes = [ |
| 111 | + FrontendSourceType.Message, |
| 112 | + FrontendSourceType.Event, |
| 113 | + SourceTypeAllMsgsAndEvents, |
| 114 | + ] |
| 115 | + const isNotBridgeSinkNodeTypes = [FrontendSinkType.Console, FrontendSinkType.Republish] |
| 116 | + /** |
| 117 | + * ‼️‼️‼️ bridge node contains source and action node |
| 118 | + */ |
| 119 | + const isBridgerNode = ({ type, data }: Partial<Node>): boolean => { |
| 120 | + const { specificType } = data || {} |
| 121 | + return ( |
| 122 | + (type === FlowNodeType.Input && |
| 123 | + !isNotBridgeSourceNodeTypes.includes(specificType as string)) || |
| 124 | + (type === FlowNodeType.Output && !isNotBridgeSinkNodeTypes.includes(specificType)) |
| 125 | + ) |
| 126 | + } |
| 127 | + const isActionBridgeNode = (node: Partial<Node>): boolean => |
| 128 | + node.type === FlowNodeType.Output && isBridgerNode(node) |
| 129 | + |
| 130 | + const isWithFallbackNodes = (node?: Node) => { |
| 131 | + if (!node || node?.type !== FlowNodeType.Output || !isBridgerNode(node)) { |
| 132 | + return false |
| 133 | + } |
| 134 | + const fallbackActions = node.data.formData?.fallback_actions ?? [] |
| 135 | + return fallbackActions.length > 0 |
| 136 | + } |
| 137 | + |
| 138 | + const isNotBridgeTypes: Array<string> = [ |
| 139 | + ...isNotBridgeSourceTypes, |
| 140 | + ...Object.values(ProcessingType), |
| 141 | + FrontendSinkType.Republish, |
| 142 | + FrontendSinkType.Console, |
| 143 | + ] |
| 144 | + const isBridgeType = (type: string) => { |
| 145 | + const isBridge = Object.entries(BridgeType).some(([, value]) => value === type) |
| 146 | + return !isNotBridgeTypes.includes(type) && isBridge |
| 147 | + } |
| 148 | + |
| 149 | + const defaultTypesNotAI = [ProcessingType.Filter, ProcessingType.Function] |
| 150 | + const isAIType = (type: string) => { |
| 151 | + return ( |
| 152 | + type === AI_PLACEHOLDER_TYPE || |
| 153 | + (Object.values(ProcessingType).includes(type as ProcessingType) && |
| 154 | + !defaultTypesNotAI.includes(type as ProcessingType)) |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + const isLikeFunctionType = (type: string) => { |
| 159 | + return isAIType(type) || type === ProcessingType.Function |
| 160 | + } |
| 161 | + |
| 162 | + const getFilterInfo = (filter: FilterFormType) => { |
| 163 | + const num = countFiltersNum(filter.form) |
| 164 | + return `${num} ${t('flow.condition', num)}` |
| 165 | + } |
| 166 | + |
| 167 | + const getNodeInfoFunc = (node: Node, getEventLabel: (event: string) => string): string => { |
| 168 | + const { specificType, formData } = node.data |
| 169 | + if (!specificType || !formData) { |
| 170 | + return '' |
| 171 | + } |
| 172 | + if (isAIType(specificType)) { |
| 173 | + return `${t('flow.systemPrompt')}${t('common.colon')}${formData.system_prompt}` |
| 174 | + } |
| 175 | + switch (specificType) { |
| 176 | + case FrontendSourceType.Message: |
| 177 | + return `${t('common.topic')}${t('common.colon')}${formData.topic}` |
| 178 | + case FrontendSourceType.Event: |
| 179 | + return `${t('flow.event')}${t('common.colon')}${getEventLabel(formData.event)}` |
| 180 | + case ProcessingType.Function: |
| 181 | + return '' |
| 182 | + case ProcessingType.Filter: |
| 183 | + return getFilterInfo(formData) |
| 184 | + case FrontendSinkType.Console: |
| 185 | + return '' |
| 186 | + case FrontendSinkType.Republish: |
| 187 | + return `${t('common.topic')}${t('common.colon')}${formData.args?.topic}` |
| 188 | + default: |
| 189 | + return `${t('common.name')}${t('common.colon')}${formData.name}` |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + const generateProcessingNodeByType = (type: string): NodeItem => ({ |
| 194 | + name: getCommonTypeLabel(type), |
| 195 | + specificType: type, |
| 196 | + }) |
| 197 | + const processingNodeList: Array<NodeItem> = Object.values(ProcessingType).map( |
| 198 | + generateProcessingNodeByType, |
| 199 | + ) |
| 200 | + |
| 201 | + return { |
| 202 | + nodeWidth, |
| 203 | + processingNodeList, |
| 204 | + getNodeClass, |
| 205 | + getFlowNodeHookPosition, |
| 206 | + getTypeCommonData, |
| 207 | + isBridgerNode, |
| 208 | + isActionBridgeNode, |
| 209 | + isWithFallbackNodes, |
| 210 | + isBridgeType, |
| 211 | + isAIType, |
| 212 | + isLikeFunctionType, |
| 213 | + getCommonTypeLabel, |
| 214 | + getNodeInfoFunc, |
| 215 | + } |
| 216 | +} |
0 commit comments