| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | import { BlockEnum } from '../../types' | 
					
						
							|  |  |  | import type { NodeDefault } from '../../types' | 
					
						
							|  |  |  | import { type AssignerNodeType, WriteMode } from './types' | 
					
						
							| 
									
										
										
										
											2025-02-04 03:26:01 +02:00
										 |  |  | import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/blocks' | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | const i18nPrefix = 'workflow.errorMsg' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const nodeDefault: NodeDefault<AssignerNodeType> = { | 
					
						
							|  |  |  |   defaultValue: { | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |     version: '2', | 
					
						
							|  |  |  |     items: [], | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   getAvailablePrevNodes(isChatMode: boolean) { | 
					
						
							|  |  |  |     const nodes = isChatMode | 
					
						
							|  |  |  |       ? ALL_CHAT_AVAILABLE_BLOCKS | 
					
						
							|  |  |  |       : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End) | 
					
						
							|  |  |  |     return nodes | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   getAvailableNextNodes(isChatMode: boolean) { | 
					
						
							|  |  |  |     const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS | 
					
						
							|  |  |  |     return nodes | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   checkValid(payload: AssignerNodeType, t: any) { | 
					
						
							|  |  |  |     let errorMessages = '' | 
					
						
							|  |  |  |     const { | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |       items: operationItems, | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     } = payload | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |     operationItems?.forEach((value) => { | 
					
						
							|  |  |  |       if (!errorMessages && !value.variable_selector?.length) | 
					
						
							|  |  |  |         errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.assignedVariable') }) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |       if (!errorMessages && value.operation !== WriteMode.clear) { | 
					
						
							| 
									
										
										
										
											2024-12-04 15:46:54 +08:00
										 |  |  |         if (value.operation === WriteMode.set || value.operation === WriteMode.increment | 
					
						
							|  |  |  |           || value.operation === WriteMode.decrement || value.operation === WriteMode.multiply | 
					
						
							|  |  |  |           || value.operation === WriteMode.divide) { | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |           if (!value.value && typeof value.value !== 'number') | 
					
						
							|  |  |  |             errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (!value.value?.length) { | 
					
						
							|  |  |  |           errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       isValid: !errorMessages, | 
					
						
							|  |  |  |       errorMessage: errorMessages, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default nodeDefault |