| 
									
										
										
										
											2024-05-10 18:14:05 +08:00
										 |  |  | import { BlockEnum, EditionType } from '../../types' | 
					
						
							|  |  |  | import { type NodeDefault, type PromptItem, PromptRole } from '../../types' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { LLMNodeType } 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-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const i18nPrefix = 'workflow.errorMsg' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const nodeDefault: NodeDefault<LLMNodeType> = { | 
					
						
							|  |  |  |   defaultValue: { | 
					
						
							|  |  |  |     model: { | 
					
						
							|  |  |  |       provider: '', | 
					
						
							|  |  |  |       name: '', | 
					
						
							|  |  |  |       mode: 'chat', | 
					
						
							|  |  |  |       completion_params: { | 
					
						
							|  |  |  |         temperature: 0.7, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     prompt_template: [{ | 
					
						
							|  |  |  |       role: PromptRole.system, | 
					
						
							|  |  |  |       text: '', | 
					
						
							|  |  |  |     }], | 
					
						
							|  |  |  |     context: { | 
					
						
							|  |  |  |       enabled: false, | 
					
						
							|  |  |  |       variable_selector: [], | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     vision: { | 
					
						
							|  |  |  |       enabled: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   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: LLMNodeType, t: any) { | 
					
						
							|  |  |  |     let errorMessages = '' | 
					
						
							|  |  |  |     if (!errorMessages && !payload.model.provider) | 
					
						
							|  |  |  |       errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.model`) }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!errorMessages && !payload.memory) { | 
					
						
							|  |  |  |       const isChatModel = payload.model.mode === 'chat' | 
					
						
							| 
									
										
										
										
											2024-09-08 12:14:11 +07:00
										 |  |  |       const isPromptEmpty = isChatModel | 
					
						
							| 
									
										
										
										
											2024-05-11 13:40:27 +08:00
										 |  |  |         ? !(payload.prompt_template as PromptItem[]).some((t) => { | 
					
						
							|  |  |  |           if (t.edition_type === EditionType.jinja2) | 
					
						
							|  |  |  |             return t.jinja2_text !== '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return t.text !== '' | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         : ((payload.prompt_template as PromptItem).edition_type === EditionType.jinja2 ? (payload.prompt_template as PromptItem).jinja2_text === '' : (payload.prompt_template as PromptItem).text === '') | 
					
						
							| 
									
										
										
										
											2024-09-08 12:14:11 +07:00
										 |  |  |       if (isPromptEmpty) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.llm.prompt') }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-25 18:01:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!errorMessages && !!payload.memory) { | 
					
						
							|  |  |  |       const isChatModel = payload.model.mode === 'chat' | 
					
						
							|  |  |  |       // payload.memory.query_prompt_template not pass is default: {{#sys.query#}}
 | 
					
						
							|  |  |  |       if (isChatModel && !!payload.memory.query_prompt_template && !payload.memory.query_prompt_template.includes('{{#sys.query#}}')) | 
					
						
							|  |  |  |         errorMessages = t('workflow.nodes.llm.sysQueryInUser') | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-05-10 18:14:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!errorMessages) { | 
					
						
							|  |  |  |       const isChatModel = payload.model.mode === 'chat' | 
					
						
							|  |  |  |       const isShowVars = (() => { | 
					
						
							|  |  |  |         if (isChatModel) | 
					
						
							|  |  |  |           return (payload.prompt_template as PromptItem[]).some(item => item.edition_type === EditionType.jinja2) | 
					
						
							|  |  |  |         return (payload.prompt_template as PromptItem).edition_type === EditionType.jinja2 | 
					
						
							|  |  |  |       })() | 
					
						
							|  |  |  |       if (isShowVars && payload.prompt_config?.jinja2_variables) { | 
					
						
							|  |  |  |         payload.prompt_config?.jinja2_variables.forEach((i) => { | 
					
						
							|  |  |  |           if (!errorMessages && !i.variable) | 
					
						
							|  |  |  |             errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) }) | 
					
						
							|  |  |  |           if (!errorMessages && !i.value_selector.length) | 
					
						
							|  |  |  |             errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) }) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     if (!errorMessages && payload.vision?.enabled && !payload.vision.configs?.variable_selector?.length) | 
					
						
							|  |  |  |       errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.visionVariable`) }) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     return { | 
					
						
							|  |  |  |       isValid: !errorMessages, | 
					
						
							|  |  |  |       errorMessage: errorMessages, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default nodeDefault |