| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | import { useCallback } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { useStore } from '../store' | 
					
						
							|  |  |  | import { getVarType, toNodeAvailableVars } from '@/app/components/workflow/nodes/_base/components/variable/utils' | 
					
						
							|  |  |  | import type { | 
					
						
							|  |  |  |   Node, | 
					
						
							|  |  |  |   NodeOutPutVar, | 
					
						
							|  |  |  |   ValueSelector, | 
					
						
							|  |  |  |   Var, | 
					
						
							|  |  |  | } from '@/app/components/workflow/types' | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  | import { useIsChatMode } from './use-workflow' | 
					
						
							|  |  |  | import { useStoreApi } from 'reactflow' | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const useWorkflowVariables = () => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const environmentVariables = useStore(s => s.environmentVariables) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |   const conversationVariables = useStore(s => s.conversationVariables) | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const getNodeAvailableVars = useCallback(({ | 
					
						
							|  |  |  |     parentNode, | 
					
						
							|  |  |  |     beforeNodes, | 
					
						
							|  |  |  |     isChatMode, | 
					
						
							|  |  |  |     filterVar, | 
					
						
							|  |  |  |     hideEnv, | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     hideChatVar, | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |   }: { | 
					
						
							|  |  |  |     parentNode?: Node | null | 
					
						
							|  |  |  |     beforeNodes: Node[] | 
					
						
							|  |  |  |     isChatMode: boolean | 
					
						
							|  |  |  |     filterVar: (payload: Var, selector: ValueSelector) => boolean | 
					
						
							|  |  |  |     hideEnv?: boolean | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     hideChatVar?: boolean | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |   }): NodeOutPutVar[] => { | 
					
						
							|  |  |  |     return toNodeAvailableVars({ | 
					
						
							|  |  |  |       parentNode, | 
					
						
							|  |  |  |       t, | 
					
						
							|  |  |  |       beforeNodes, | 
					
						
							|  |  |  |       isChatMode, | 
					
						
							|  |  |  |       environmentVariables: hideEnv ? [] : environmentVariables, | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |       conversationVariables: (isChatMode && !hideChatVar) ? conversationVariables : [], | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |       filterVar, | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |   }, [conversationVariables, environmentVariables, t]) | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const getCurrentVariableType = useCallback(({ | 
					
						
							|  |  |  |     parentNode, | 
					
						
							|  |  |  |     valueSelector, | 
					
						
							|  |  |  |     isIterationItem, | 
					
						
							| 
									
										
										
										
											2025-03-05 17:41:15 +08:00
										 |  |  |     isLoopItem, | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     availableNodes, | 
					
						
							|  |  |  |     isChatMode, | 
					
						
							|  |  |  |     isConstant, | 
					
						
							|  |  |  |   }: { | 
					
						
							|  |  |  |     valueSelector: ValueSelector | 
					
						
							|  |  |  |     parentNode?: Node | null | 
					
						
							|  |  |  |     isIterationItem?: boolean | 
					
						
							| 
									
										
										
										
											2025-03-05 17:41:15 +08:00
										 |  |  |     isLoopItem?: boolean | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     availableNodes: any[] | 
					
						
							|  |  |  |     isChatMode: boolean | 
					
						
							|  |  |  |     isConstant?: boolean | 
					
						
							|  |  |  |   }) => { | 
					
						
							|  |  |  |     return getVarType({ | 
					
						
							|  |  |  |       parentNode, | 
					
						
							|  |  |  |       valueSelector, | 
					
						
							|  |  |  |       isIterationItem, | 
					
						
							| 
									
										
										
										
											2025-03-05 17:41:15 +08:00
										 |  |  |       isLoopItem, | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |       availableNodes, | 
					
						
							|  |  |  |       isChatMode, | 
					
						
							|  |  |  |       isConstant, | 
					
						
							|  |  |  |       environmentVariables, | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |       conversationVariables, | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |   }, [conversationVariables, environmentVariables]) | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     getNodeAvailableVars, | 
					
						
							|  |  |  |     getCurrentVariableType, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const useWorkflowVariableType = () => { | 
					
						
							|  |  |  |   const store = useStoreApi() | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     getNodes, | 
					
						
							|  |  |  |   } = store.getState() | 
					
						
							|  |  |  |   const { getCurrentVariableType } = useWorkflowVariables() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const isChatMode = useIsChatMode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getVarType = ({ | 
					
						
							|  |  |  |     nodeId, | 
					
						
							|  |  |  |     valueSelector, | 
					
						
							|  |  |  |   }: { | 
					
						
							|  |  |  |     nodeId: string, | 
					
						
							|  |  |  |     valueSelector: ValueSelector, | 
					
						
							|  |  |  |   }) => { | 
					
						
							|  |  |  |     const node = getNodes().find(n => n.id === nodeId) | 
					
						
							|  |  |  |     const isInIteration = !!node?.data.isInIteration | 
					
						
							|  |  |  |     const iterationNode = isInIteration ? getNodes().find(n => n.id === node.parentId) : null | 
					
						
							|  |  |  |     const availableNodes = [node] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const type = getCurrentVariableType({ | 
					
						
							|  |  |  |       parentNode: iterationNode, | 
					
						
							|  |  |  |       valueSelector, | 
					
						
							|  |  |  |       availableNodes, | 
					
						
							|  |  |  |       isChatMode, | 
					
						
							|  |  |  |       isConstant: false, | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     return type | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return getVarType | 
					
						
							|  |  |  | } |