| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import { useCallback, useEffect, useRef, useState } from 'react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import produce from 'immer' | 
					
						
							|  |  |  | import { BlockEnum, VarType } from '../../types' | 
					
						
							|  |  |  | import type { Memory, ValueSelector, Var } from '../../types' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   useIsChatMode, useNodesReadOnly, | 
					
						
							|  |  |  |   useWorkflow, | 
					
						
							|  |  |  | } from '../../hooks' | 
					
						
							|  |  |  | import { useStore } from '../../store' | 
					
						
							| 
									
										
										
										
											2024-06-04 14:01:40 +08:00
										 |  |  | import useAvailableVarList from '../_base/hooks/use-available-var-list' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import useConfigVision from '../../hooks/use-config-vision' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { QuestionClassifierNodeType } from './types' | 
					
						
							|  |  |  | import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' | 
					
						
							|  |  |  | import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks' | 
					
						
							|  |  |  | import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' | 
					
						
							| 
									
										
										
										
											2024-06-04 14:01:40 +08:00
										 |  |  | import { checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const useConfig = (id: string, payload: QuestionClassifierNodeType) => { | 
					
						
							|  |  |  |   const { nodesReadOnly: readOnly } = useNodesReadOnly() | 
					
						
							|  |  |  |   const isChatMode = useIsChatMode() | 
					
						
							|  |  |  |   const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type] | 
					
						
							|  |  |  |   const { getBeforeNodesInSameBranch } = useWorkflow() | 
					
						
							|  |  |  |   const startNode = getBeforeNodesInSameBranch(id).find(node => node.data.type === BlockEnum.Start) | 
					
						
							|  |  |  |   const startNodeId = startNode?.id | 
					
						
							|  |  |  |   const { inputs, setInputs } = useNodeCrud<QuestionClassifierNodeType>(id, payload) | 
					
						
							|  |  |  |   const inputRef = useRef(inputs) | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     inputRef.current = inputs | 
					
						
							|  |  |  |   }, [inputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   const [modelChanged, setModelChanged] = useState(false) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     currentProvider, | 
					
						
							|  |  |  |     currentModel, | 
					
						
							|  |  |  |   } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const model = inputs.model | 
					
						
							|  |  |  |   const modelMode = inputs.model?.mode | 
					
						
							|  |  |  |   const isChatModel = modelMode === 'chat' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     isVisionModel, | 
					
						
							|  |  |  |     handleVisionResolutionEnabledChange, | 
					
						
							|  |  |  |     handleVisionResolutionChange, | 
					
						
							|  |  |  |     handleModelChanged: handleVisionConfigAfterModelChanged, | 
					
						
							|  |  |  |   } = useConfigVision(model, { | 
					
						
							|  |  |  |     payload: inputs.vision, | 
					
						
							|  |  |  |     onChange: (newPayload) => { | 
					
						
							|  |  |  |       const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |         draft.vision = newPayload | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       setInputs(newInputs) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       draft.model.provider = model.provider | 
					
						
							|  |  |  |       draft.model.name = model.modelId | 
					
						
							|  |  |  |       draft.model.mode = model.mode! | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     setModelChanged(true) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (currentProvider?.provider && currentModel?.model && !model.provider) { | 
					
						
							|  |  |  |       handleModelChanged({ | 
					
						
							|  |  |  |         provider: currentProvider?.provider, | 
					
						
							|  |  |  |         modelId: currentModel?.model, | 
					
						
							|  |  |  |         mode: currentModel?.model_properties?.mode as string, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [model.provider, currentProvider, currentModel, handleModelChanged]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleCompletionParamsChange = useCallback((newParams: Record<string, any>) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.model.completion_params = newParams | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [inputs, setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   // change to vision model to set vision enabled, else disabled
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (!modelChanged) | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     setModelChanged(false) | 
					
						
							|  |  |  |     handleVisionConfigAfterModelChanged() | 
					
						
							| 
									
										
										
										
											2025-06-24 09:10:30 +08:00
										 |  |  |     // eslint-disable-next-line react-hooks/exhaustive-deps
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   }, [isVisionModel, modelChanged]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const handleQueryVarChange = useCallback((newVar: ValueSelector | string) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.query_variable_selector = newVar as ValueSelector | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [inputs, setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     const isReady = defaultConfig && Object.keys(defaultConfig).length > 0 | 
					
						
							|  |  |  |     if (isReady) { | 
					
						
							|  |  |  |       let query_variable_selector: ValueSelector = [] | 
					
						
							|  |  |  |       if (isChatMode && inputs.query_variable_selector.length === 0 && startNodeId) | 
					
						
							|  |  |  |         query_variable_selector = [startNodeId, 'sys.query'] | 
					
						
							|  |  |  |       setInputs({ | 
					
						
							|  |  |  |         ...inputs, | 
					
						
							|  |  |  |         ...defaultConfig, | 
					
						
							|  |  |  |         query_variable_selector: inputs.query_variable_selector.length > 0 ? inputs.query_variable_selector : query_variable_selector, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-06-24 09:10:30 +08:00
										 |  |  |     // eslint-disable-next-line react-hooks/exhaustive-deps
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   }, [defaultConfig]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleClassesChange = useCallback((newClasses: any) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.classes = newClasses | 
					
						
							|  |  |  |       draft._targetBranches = newClasses | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [inputs, setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-04 14:01:40 +08:00
										 |  |  |   const filterInputVar = useCallback((varPayload: Var) => { | 
					
						
							|  |  |  |     return [VarType.number, VarType.string].includes(varPayload.type) | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 13:29:59 +08:00
										 |  |  |   const filterVisionInputVar = useCallback((varPayload: Var) => { | 
					
						
							|  |  |  |     return [VarType.file, VarType.arrayFile].includes(varPayload.type) | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-04 14:01:40 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     availableVars, | 
					
						
							|  |  |  |     availableNodesWithParent, | 
					
						
							|  |  |  |   } = useAvailableVarList(id, { | 
					
						
							|  |  |  |     onlyLeafNodeVar: false, | 
					
						
							|  |  |  |     filterVar: filterInputVar, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 13:29:59 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     availableVars: availableVisionVars, | 
					
						
							|  |  |  |   } = useAvailableVarList(id, { | 
					
						
							|  |  |  |     onlyLeafNodeVar: false, | 
					
						
							|  |  |  |     filterVar: filterVisionInputVar, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-04 14:01:40 +08:00
										 |  |  |   const hasSetBlockStatus = { | 
					
						
							|  |  |  |     history: false, | 
					
						
							|  |  |  |     query: isChatMode ? checkHasQueryBlock(inputs.instruction) : false, | 
					
						
							|  |  |  |     context: false, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const handleInstructionChange = useCallback((instruction: string) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.instruction = instruction | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [inputs, setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleMemoryChange = useCallback((memory?: Memory) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.memory = memory | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [inputs, setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const filterVar = useCallback((varPayload: Var) => { | 
					
						
							|  |  |  |     return varPayload.type === VarType.string | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     readOnly, | 
					
						
							|  |  |  |     inputs, | 
					
						
							|  |  |  |     handleModelChanged, | 
					
						
							|  |  |  |     isChatMode, | 
					
						
							|  |  |  |     isChatModel, | 
					
						
							|  |  |  |     handleCompletionParamsChange, | 
					
						
							|  |  |  |     handleQueryVarChange, | 
					
						
							|  |  |  |     filterVar, | 
					
						
							|  |  |  |     handleTopicsChange: handleClassesChange, | 
					
						
							| 
									
										
										
										
											2024-06-04 14:01:40 +08:00
										 |  |  |     hasSetBlockStatus, | 
					
						
							|  |  |  |     availableVars, | 
					
						
							|  |  |  |     availableNodesWithParent, | 
					
						
							| 
									
										
										
										
											2025-03-03 13:29:59 +08:00
										 |  |  |     availableVisionVars, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     handleInstructionChange, | 
					
						
							|  |  |  |     handleMemoryChange, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     isVisionModel, | 
					
						
							|  |  |  |     handleVisionResolutionEnabledChange, | 
					
						
							|  |  |  |     handleVisionResolutionChange, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default useConfig |