2025-07-02 17:48:07 +08:00
|
|
|
import { useCallback } from 'react'
|
2025-06-24 09:10:30 +08:00
|
|
|
import type { NodeWithVar, VarInInspect } from '@/types/workflow'
|
2025-07-02 17:48:07 +08:00
|
|
|
import { useWorkflowStore } from '@/app/components/workflow/store'
|
2025-06-24 09:10:30 +08:00
|
|
|
import { useStoreApi } from 'reactflow'
|
|
|
|
import type { Node } from '@/app/components/workflow/types'
|
|
|
|
import { fetchAllInspectVars } from '@/service/workflow'
|
|
|
|
import { useInvalidateConversationVarValues, useInvalidateSysVarValues } from '@/service/use-workflow'
|
2025-07-02 17:48:07 +08:00
|
|
|
import { useNodesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-nodes-interactions-without-sync'
|
|
|
|
import { useConfigsMap } from './use-configs-map'
|
|
|
|
|
|
|
|
export const useSetWorkflowVarsWithValue = () => {
|
2025-06-24 09:10:30 +08:00
|
|
|
const workflowStore = useWorkflowStore()
|
|
|
|
const store = useStoreApi()
|
2025-07-02 17:48:07 +08:00
|
|
|
const { conversationVarsUrl, systemVarsUrl } = useConfigsMap()
|
|
|
|
const invalidateConversationVarValues = useInvalidateConversationVarValues(conversationVarsUrl)
|
|
|
|
const invalidateSysVarValues = useInvalidateSysVarValues(systemVarsUrl)
|
2025-06-24 09:10:30 +08:00
|
|
|
const { handleCancelAllNodeSuccessStatus } = useNodesInteractionsWithoutSync()
|
|
|
|
|
2025-07-02 17:48:07 +08:00
|
|
|
const setInspectVarsToStore = useCallback((inspectVars: VarInInspect[]) => {
|
|
|
|
const { setNodesWithInspectVars } = workflowStore.getState()
|
2025-06-24 09:10:30 +08:00
|
|
|
const { getNodes } = store.getState()
|
|
|
|
const nodeArr = getNodes()
|
|
|
|
const nodesKeyValue: Record<string, Node> = {}
|
|
|
|
nodeArr.forEach((node) => {
|
|
|
|
nodesKeyValue[node.id] = node
|
|
|
|
})
|
|
|
|
|
|
|
|
const withValueNodeIds: Record<string, boolean> = {}
|
|
|
|
inspectVars.forEach((varItem) => {
|
|
|
|
const nodeId = varItem.selector[0]
|
|
|
|
|
|
|
|
const node = nodesKeyValue[nodeId]
|
|
|
|
if (!node)
|
|
|
|
return
|
|
|
|
withValueNodeIds[nodeId] = true
|
|
|
|
})
|
|
|
|
const withValueNodes = Object.keys(withValueNodeIds).map((nodeId) => {
|
|
|
|
return nodesKeyValue[nodeId]
|
|
|
|
})
|
|
|
|
|
|
|
|
const res: NodeWithVar[] = withValueNodes.map((node) => {
|
|
|
|
const nodeId = node.id
|
|
|
|
const varsUnderTheNode = inspectVars.filter((varItem) => {
|
|
|
|
return varItem.selector[0] === nodeId
|
|
|
|
})
|
|
|
|
const nodeWithVar = {
|
|
|
|
nodeId,
|
|
|
|
nodePayload: node.data,
|
|
|
|
nodeType: node.data.type,
|
|
|
|
title: node.data.title,
|
|
|
|
vars: varsUnderTheNode,
|
|
|
|
isSingRunRunning: false,
|
|
|
|
isValueFetched: false,
|
|
|
|
}
|
|
|
|
return nodeWithVar
|
|
|
|
})
|
|
|
|
setNodesWithInspectVars(res)
|
2025-07-02 17:48:07 +08:00
|
|
|
}, [workflowStore, store])
|
2025-06-24 09:10:30 +08:00
|
|
|
|
2025-07-02 17:48:07 +08:00
|
|
|
const fetchInspectVars = useCallback(async () => {
|
|
|
|
const { appId } = workflowStore.getState()
|
2025-06-24 09:10:30 +08:00
|
|
|
invalidateConversationVarValues()
|
|
|
|
invalidateSysVarValues()
|
|
|
|
const data = await fetchAllInspectVars(appId)
|
|
|
|
setInspectVarsToStore(data)
|
|
|
|
handleCancelAllNodeSuccessStatus() // to make sure clear node output show the unset status
|
2025-07-02 17:48:07 +08:00
|
|
|
}, [workflowStore, invalidateConversationVarValues, invalidateSysVarValues, setInspectVarsToStore, handleCancelAllNodeSuccessStatus])
|
2025-06-24 09:10:30 +08:00
|
|
|
return {
|
|
|
|
fetchInspectVars,
|
|
|
|
}
|
|
|
|
}
|