| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   useCallback, | 
					
						
							|  |  |  |   useEffect, | 
					
						
							|  |  |  |   useRef, | 
					
						
							|  |  |  |   useState, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import produce from 'immer' | 
					
						
							|  |  |  | import { isEqual } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  | import { v4 as uuid4 } from 'uuid' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { ValueSelector, Var } from '../../types' | 
					
						
							|  |  |  | import { BlockEnum, VarType } from '../../types' | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  |   useIsChatMode, | 
					
						
							|  |  |  |   useNodesReadOnly, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   useWorkflow, | 
					
						
							|  |  |  | } from '../../hooks' | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  | import type { | 
					
						
							|  |  |  |   HandleAddCondition, | 
					
						
							|  |  |  |   HandleRemoveCondition, | 
					
						
							|  |  |  |   HandleToggleConditionLogicalOperator, | 
					
						
							|  |  |  |   HandleUpdateCondition, | 
					
						
							|  |  |  |   KnowledgeRetrievalNodeType, | 
					
						
							|  |  |  |   MetadataFilteringModeEnum, | 
					
						
							|  |  |  |   MultipleRetrievalConfig, | 
					
						
							|  |  |  | } from './types' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   ComparisonOperator, | 
					
						
							|  |  |  |   LogicalOperator, | 
					
						
							|  |  |  |   MetadataFilteringVariableType, | 
					
						
							|  |  |  | } from './types' | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   getMultipleRetrievalConfig, | 
					
						
							|  |  |  |   getSelectedDatasetsMode, | 
					
						
							|  |  |  | } from './utils' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { RETRIEVE_TYPE } from '@/types/app' | 
					
						
							|  |  |  | import { DATASET_DEFAULT } from '@/config' | 
					
						
							|  |  |  | import type { DataSet } from '@/models/datasets' | 
					
						
							|  |  |  | import { fetchDatasets } from '@/service/datasets' | 
					
						
							|  |  |  | import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  | import { useCurrentProviderAndModel, useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  | import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list' | 
					
						
							| 
									
										
										
										
											2025-03-24 14:30:26 +08:00
										 |  |  | import { useDatasetsDetailStore } from '../../datasets-detail-store/store' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const useConfig = (id: string, payload: KnowledgeRetrievalNodeType) => { | 
					
						
							|  |  |  |   const { nodesReadOnly: readOnly } = useNodesReadOnly() | 
					
						
							|  |  |  |   const isChatMode = useIsChatMode() | 
					
						
							|  |  |  |   const { getBeforeNodesInSameBranch } = useWorkflow() | 
					
						
							|  |  |  |   const startNode = getBeforeNodesInSameBranch(id).find(node => node.data.type === BlockEnum.Start) | 
					
						
							|  |  |  |   const startNodeId = startNode?.id | 
					
						
							|  |  |  |   const { inputs, setInputs: doSetInputs } = useNodeCrud<KnowledgeRetrievalNodeType>(id, payload) | 
					
						
							| 
									
										
										
										
											2025-03-24 14:30:26 +08:00
										 |  |  |   const updateDatasetsDetail = useDatasetsDetailStore(s => s.updateDatasetsDetail) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |   const inputRef = useRef(inputs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const setInputs = useCallback((s: KnowledgeRetrievalNodeType) => { | 
					
						
							|  |  |  |     const newInputs = produce(s, (draft) => { | 
					
						
							|  |  |  |       if (s.retrieval_mode === RETRIEVE_TYPE.multiWay) | 
					
						
							|  |  |  |         delete draft.single_retrieval_config | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         delete draft.multiple_retrieval_config | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     // not work in pass to draft...
 | 
					
						
							|  |  |  |     doSetInputs(newInputs) | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |     inputRef.current = newInputs | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   }, [doSetInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleQueryVarChange = useCallback((newVar: ValueSelector | string) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.query_variable_selector = newVar as ValueSelector | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [inputs, setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     currentProvider, | 
					
						
							|  |  |  |     currentModel, | 
					
						
							|  |  |  |   } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |     modelList: rerankModelList, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     defaultModel: rerankDefaultModel, | 
					
						
							|  |  |  |   } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.rerank) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     currentModel: currentRerankModel, | 
					
						
							| 
									
										
										
										
											2024-11-12 14:38:24 +08:00
										 |  |  |     currentProvider: currentRerankProvider, | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |   } = useCurrentProviderAndModel( | 
					
						
							|  |  |  |     rerankModelList, | 
					
						
							|  |  |  |     rerankDefaultModel | 
					
						
							|  |  |  |       ? { | 
					
						
							|  |  |  |         ...rerankDefaultModel, | 
					
						
							|  |  |  |         provider: rerankDefaultModel.provider.provider, | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       : undefined, | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       if (!draft.single_retrieval_config) { | 
					
						
							|  |  |  |         draft.single_retrieval_config = { | 
					
						
							|  |  |  |           model: { | 
					
						
							|  |  |  |             provider: '', | 
					
						
							|  |  |  |             name: '', | 
					
						
							|  |  |  |             mode: '', | 
					
						
							|  |  |  |             completion_params: {}, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const draftModel = draft.single_retrieval_config?.model | 
					
						
							|  |  |  |       draftModel.provider = model.provider | 
					
						
							|  |  |  |       draftModel.name = model.modelId | 
					
						
							|  |  |  |       draftModel.mode = model.mode! | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleCompletionParamsChange = useCallback((newParams: Record<string, any>) => { | 
					
						
							|  |  |  |     // inputRef.current.single_retrieval_config?.model is old  when change the provider...
 | 
					
						
							|  |  |  |     if (isEqual(newParams, inputRef.current.single_retrieval_config?.model.completion_params)) | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       if (!draft.single_retrieval_config) { | 
					
						
							|  |  |  |         draft.single_retrieval_config = { | 
					
						
							|  |  |  |           model: { | 
					
						
							|  |  |  |             provider: '', | 
					
						
							|  |  |  |             name: '', | 
					
						
							|  |  |  |             mode: '', | 
					
						
							|  |  |  |             completion_params: {}, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       draft.single_retrieval_config.model.completion_params = newParams | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // set defaults models
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     const inputs = inputRef.current | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |     if (inputs.retrieval_mode === RETRIEVE_TYPE.multiWay && inputs.multiple_retrieval_config?.reranking_model?.provider && currentRerankModel && rerankDefaultModel) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (inputs.retrieval_mode === RETRIEVE_TYPE.oneWay && inputs.single_retrieval_config?.model?.provider) | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newInput = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       if (currentProvider?.provider && currentModel?.model) { | 
					
						
							|  |  |  |         const hasSetModel = draft.single_retrieval_config?.model?.provider | 
					
						
							|  |  |  |         if (!hasSetModel) { | 
					
						
							|  |  |  |           draft.single_retrieval_config = { | 
					
						
							|  |  |  |             model: { | 
					
						
							|  |  |  |               provider: currentProvider?.provider, | 
					
						
							|  |  |  |               name: currentModel?.model, | 
					
						
							|  |  |  |               mode: currentModel?.model_properties?.mode as string, | 
					
						
							|  |  |  |               completion_params: {}, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const multipleRetrievalConfig = draft.multiple_retrieval_config | 
					
						
							|  |  |  |       draft.multiple_retrieval_config = { | 
					
						
							|  |  |  |         top_k: multipleRetrievalConfig?.top_k || DATASET_DEFAULT.top_k, | 
					
						
							|  |  |  |         score_threshold: multipleRetrievalConfig?.score_threshold, | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |         reranking_model: multipleRetrievalConfig?.reranking_model, | 
					
						
							| 
									
										
										
										
											2024-09-28 10:54:50 +08:00
										 |  |  |         reranking_mode: multipleRetrievalConfig?.reranking_mode, | 
					
						
							|  |  |  |         weights: multipleRetrievalConfig?.weights, | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |         reranking_enable: multipleRetrievalConfig?.reranking_enable !== undefined | 
					
						
							|  |  |  |           ? multipleRetrievalConfig.reranking_enable | 
					
						
							|  |  |  |           : Boolean(currentRerankModel && rerankDefaultModel), | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInput) | 
					
						
							| 
									
										
										
										
											2025-06-24 09:10:30 +08:00
										 |  |  |     // eslint-disable-next-line react-hooks/exhaustive-deps
 | 
					
						
							| 
									
										
										
										
											2025-01-02 16:07:21 +08:00
										 |  |  |   }, [currentProvider?.provider, currentModel, currentRerankModel, rerankDefaultModel]) | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |   const [selectedDatasets, setSelectedDatasets] = useState<DataSet[]>([]) | 
					
						
							|  |  |  |   const [rerankModelOpen, setRerankModelOpen] = useState(false) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const handleRetrievalModeChange = useCallback((newMode: RETRIEVE_TYPE) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.retrieval_mode = newMode | 
					
						
							|  |  |  |       if (newMode === RETRIEVE_TYPE.multiWay) { | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |         const multipleRetrievalConfig = draft.multiple_retrieval_config | 
					
						
							| 
									
										
										
										
											2024-11-12 14:38:24 +08:00
										 |  |  |         draft.multiple_retrieval_config = getMultipleRetrievalConfig(multipleRetrievalConfig!, selectedDatasets, selectedDatasets, { | 
					
						
							|  |  |  |           provider: currentRerankProvider?.provider, | 
					
						
							|  |  |  |           model: currentRerankModel?.model, | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         const hasSetModel = draft.single_retrieval_config?.model?.provider | 
					
						
							|  |  |  |         if (!hasSetModel) { | 
					
						
							|  |  |  |           draft.single_retrieval_config = { | 
					
						
							|  |  |  |             model: { | 
					
						
							|  |  |  |               provider: currentProvider?.provider || '', | 
					
						
							|  |  |  |               name: currentModel?.model || '', | 
					
						
							|  |  |  |               mode: currentModel?.model_properties?.mode as string, | 
					
						
							|  |  |  |               completion_params: {}, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							| 
									
										
										
										
											2024-11-12 14:38:24 +08:00
										 |  |  |   }, [currentModel?.model, currentModel?.model_properties?.mode, currentProvider?.provider, inputs, setInputs, selectedDatasets, currentRerankModel, currentRerankProvider]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const handleMultipleRetrievalConfigChange = useCallback((newConfig: MultipleRetrievalConfig) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							| 
									
										
										
										
											2024-11-12 14:38:24 +08:00
										 |  |  |       draft.multiple_retrieval_config = getMultipleRetrievalConfig(newConfig!, selectedDatasets, selectedDatasets, { | 
					
						
							|  |  |  |         provider: currentRerankProvider?.provider, | 
					
						
							|  |  |  |         model: currentRerankModel?.model, | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							| 
									
										
										
										
											2024-11-12 14:38:24 +08:00
										 |  |  |   }, [inputs, setInputs, selectedDatasets, currentRerankModel, currentRerankProvider]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  |   const [selectedDatasetsLoaded, setSelectedDatasetsLoaded] = useState(false) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   // datasets
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     (async () => { | 
					
						
							|  |  |  |       const inputs = inputRef.current | 
					
						
							|  |  |  |       const datasetIds = inputs.dataset_ids | 
					
						
							|  |  |  |       if (datasetIds?.length > 0) { | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  |         const { data: dataSetsWithDetail } = await fetchDatasets({ url: '/datasets', params: { page: 1, ids: datasetIds } as any }) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         setSelectedDatasets(dataSetsWithDetail) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |         draft.dataset_ids = datasetIds | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       setInputs(newInputs) | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  |       setSelectedDatasetsLoaded(true) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     })() | 
					
						
							| 
									
										
										
										
											2025-06-24 09:10:30 +08:00
										 |  |  |     // eslint-disable-next-line react-hooks/exhaustive-deps
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |     const inputs = inputRef.current | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     let query_variable_selector: ValueSelector = inputs.query_variable_selector | 
					
						
							|  |  |  |     if (isChatMode && inputs.query_variable_selector.length === 0 && startNodeId) | 
					
						
							|  |  |  |       query_variable_selector = [startNodeId, 'sys.query'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 14:26:38 +08:00
										 |  |  |     setInputs(produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.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
										 |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleOnDatasetsChange = useCallback((newDatasets: DataSet[]) => { | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |     const { | 
					
						
							|  |  |  |       mixtureHighQualityAndEconomic, | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |       mixtureInternalAndExternal, | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |       inconsistentEmbeddingModel, | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |       allInternal, | 
					
						
							|  |  |  |       allExternal, | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |     } = getSelectedDatasetsMode(newDatasets) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     const newInputs = produce(inputs, (draft) => { | 
					
						
							|  |  |  |       draft.dataset_ids = newDatasets.map(d => d.id) | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (payload.retrieval_mode === RETRIEVE_TYPE.multiWay && newDatasets.length > 0) { | 
					
						
							|  |  |  |         const multipleRetrievalConfig = draft.multiple_retrieval_config | 
					
						
							| 
									
										
										
										
											2024-11-12 14:38:24 +08:00
										 |  |  |         draft.multiple_retrieval_config = getMultipleRetrievalConfig(multipleRetrievalConfig!, newDatasets, selectedDatasets, { | 
					
						
							|  |  |  |           provider: currentRerankProvider?.provider, | 
					
						
							|  |  |  |           model: currentRerankModel?.model, | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2025-03-24 14:30:26 +08:00
										 |  |  |     updateDatasetsDetail(newDatasets) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |     setSelectedDatasets(newDatasets) | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |     if ( | 
					
						
							|  |  |  |       (allInternal && (mixtureHighQualityAndEconomic || inconsistentEmbeddingModel)) | 
					
						
							|  |  |  |       || mixtureInternalAndExternal | 
					
						
							| 
									
										
										
										
											2024-10-31 20:20:46 +08:00
										 |  |  |       || allExternal | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |       setRerankModelOpen(true) | 
					
						
							| 
									
										
										
										
											2025-03-24 14:30:26 +08:00
										 |  |  |   }, [inputs, setInputs, payload.retrieval_mode, selectedDatasets, currentRerankModel, currentRerankProvider, updateDatasetsDetail]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const filterVar = useCallback((varPayload: Var) => { | 
					
						
							|  |  |  |     return varPayload.type === VarType.string | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  |   const handleMetadataFilterModeChange = useCallback((newMode: MetadataFilteringModeEnum) => { | 
					
						
							|  |  |  |     setInputs(produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       draft.metadata_filtering_mode = newMode | 
					
						
							|  |  |  |     })) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleAddCondition = useCallback<HandleAddCondition>(({ name, type }) => { | 
					
						
							|  |  |  |     let operator: ComparisonOperator = ComparisonOperator.is | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (type === MetadataFilteringVariableType.number) | 
					
						
							|  |  |  |       operator = ComparisonOperator.equal | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newCondition = { | 
					
						
							|  |  |  |       id: uuid4(), | 
					
						
							|  |  |  |       name, | 
					
						
							|  |  |  |       comparison_operator: operator, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       if (draft.metadata_filtering_conditions) { | 
					
						
							|  |  |  |         draft.metadata_filtering_conditions.conditions.push(newCondition) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         draft.metadata_filtering_conditions = { | 
					
						
							|  |  |  |           logical_operator: LogicalOperator.and, | 
					
						
							|  |  |  |           conditions: [newCondition], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleRemoveCondition = useCallback<HandleRemoveCondition>((id) => { | 
					
						
							|  |  |  |     const conditions = inputRef.current.metadata_filtering_conditions?.conditions || [] | 
					
						
							|  |  |  |     const index = conditions.findIndex(c => c.id === id) | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       if (index > -1) | 
					
						
							|  |  |  |         draft.metadata_filtering_conditions?.conditions.splice(index, 1) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleUpdateCondition = useCallback<HandleUpdateCondition>((id, newCondition) => { | 
					
						
							|  |  |  |     const conditions = inputRef.current.metadata_filtering_conditions?.conditions || [] | 
					
						
							|  |  |  |     const index = conditions.findIndex(c => c.id === id) | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       if (index > -1) | 
					
						
							|  |  |  |         draft.metadata_filtering_conditions!.conditions[index] = newCondition | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleToggleConditionLogicalOperator = useCallback<HandleToggleConditionLogicalOperator>(() => { | 
					
						
							|  |  |  |     const oldLogicalOperator = inputRef.current.metadata_filtering_conditions?.logical_operator | 
					
						
							|  |  |  |     const newLogicalOperator = oldLogicalOperator === LogicalOperator.and ? LogicalOperator.or : LogicalOperator.and | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       draft.metadata_filtering_conditions!.logical_operator = newLogicalOperator | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleMetadataModelChange = useCallback((model: { provider: string; modelId: string; mode?: string }) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       draft.metadata_model_config = { | 
					
						
							|  |  |  |         provider: model.provider, | 
					
						
							|  |  |  |         name: model.modelId, | 
					
						
							|  |  |  |         mode: model.mode || 'chat', | 
					
						
							|  |  |  |         completion_params: draft.metadata_model_config?.completion_params || { temperature: 0.7 }, | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleMetadataCompletionParamsChange = useCallback((newParams: Record<string, any>) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputRef.current, (draft) => { | 
					
						
							|  |  |  |       draft.metadata_model_config = { | 
					
						
							|  |  |  |         ...draft.metadata_model_config!, | 
					
						
							|  |  |  |         completion_params: newParams, | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const filterStringVar = useCallback((varPayload: Var) => { | 
					
						
							|  |  |  |     return [VarType.string].includes(varPayload.type) | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     availableVars: availableStringVars, | 
					
						
							|  |  |  |     availableNodesWithParent: availableStringNodesWithParent, | 
					
						
							|  |  |  |   } = useAvailableVarList(id, { | 
					
						
							|  |  |  |     onlyLeafNodeVar: false, | 
					
						
							|  |  |  |     filterVar: filterStringVar, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const filterNumberVar = useCallback((varPayload: Var) => { | 
					
						
							|  |  |  |     return [VarType.number].includes(varPayload.type) | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     availableVars: availableNumberVars, | 
					
						
							|  |  |  |     availableNodesWithParent: availableNumberNodesWithParent, | 
					
						
							|  |  |  |   } = useAvailableVarList(id, { | 
					
						
							|  |  |  |     onlyLeafNodeVar: false, | 
					
						
							|  |  |  |     filterVar: filterNumberVar, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   return { | 
					
						
							|  |  |  |     readOnly, | 
					
						
							|  |  |  |     inputs, | 
					
						
							|  |  |  |     handleQueryVarChange, | 
					
						
							|  |  |  |     filterVar, | 
					
						
							|  |  |  |     handleRetrievalModeChange, | 
					
						
							|  |  |  |     handleMultipleRetrievalConfigChange, | 
					
						
							|  |  |  |     handleModelChanged, | 
					
						
							|  |  |  |     handleCompletionParamsChange, | 
					
						
							| 
									
										
										
										
											2024-08-01 13:45:18 +08:00
										 |  |  |     selectedDatasets: selectedDatasets.filter(d => d.name), | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  |     selectedDatasetsLoaded, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     handleOnDatasetsChange, | 
					
						
							| 
									
										
										
										
											2024-07-24 12:50:48 +08:00
										 |  |  |     rerankModelOpen, | 
					
						
							|  |  |  |     setRerankModelOpen, | 
					
						
							| 
									
										
										
										
											2025-03-18 11:01:06 +08:00
										 |  |  |     handleMetadataFilterModeChange, | 
					
						
							|  |  |  |     handleUpdateCondition, | 
					
						
							|  |  |  |     handleAddCondition, | 
					
						
							|  |  |  |     handleRemoveCondition, | 
					
						
							|  |  |  |     handleToggleConditionLogicalOperator, | 
					
						
							|  |  |  |     handleMetadataModelChange, | 
					
						
							|  |  |  |     handleMetadataCompletionParamsChange, | 
					
						
							|  |  |  |     availableStringVars, | 
					
						
							|  |  |  |     availableStringNodesWithParent, | 
					
						
							|  |  |  |     availableNumberVars, | 
					
						
							|  |  |  |     availableNumberNodesWithParent, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default useConfig |