| @@ -3,15 +3,21 @@ import { useWorkflowStore } from '@/app/components/workflow/store' | |||
| import type { ValueSelector } from '@/app/components/workflow/types' | |||
| import type { VarInInspect } from '@/types/workflow' | |||
| import { VarInInspectType } from '@/types/workflow' | |||
| import { useCallback } from 'react' | |||
| import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils' | |||
| import { | |||
| isConversationVar, | |||
| isENV, | |||
| isSystemVar, | |||
| toNodeOutputVars, | |||
| } from '@/app/components/workflow/nodes/_base/components/variable/utils' | |||
| import produce from 'immer' | |||
| import type { Node } from '@/app/components/workflow/types' | |||
| import { useNodesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-nodes-interactions-without-sync' | |||
| import { useEdgesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-edges-interactions-without-sync' | |||
| import type { FlowType } from '@/types/common' | |||
| import useFLow from '@/service/use-flow' | |||
| import useMatchSchemaType from '../nodes/_base/components/variable/use-match-schema-type' | |||
| import { useStoreApi } from 'reactflow' | |||
| type Params = { | |||
| flowId: string | |||
| @@ -22,6 +28,8 @@ export const useInspectVarsCrudCommon = ({ | |||
| flowType, | |||
| }: Params) => { | |||
| const workflowStore = useWorkflowStore() | |||
| const store = useStoreApi() | |||
| const { schemaTypeDefinitions } = useMatchSchemaType() | |||
| const { | |||
| useInvalidateConversationVarValues, | |||
| useInvalidateSysVarValues, | |||
| @@ -55,9 +63,9 @@ export const useInspectVarsCrudCommon = ({ | |||
| if (!node) | |||
| return undefined | |||
| const varId = node.vars.find((varItem) => { | |||
| return varItem.selector[1] === varName | |||
| })?.id | |||
| return varId | |||
| return varItem.selector[1] === varName | |||
| })?.id | |||
| return varId | |||
| }, [getNodeInspectVars]) | |||
| const getInspectVar = useCallback((nodeId: string, name: string): VarInInspect | undefined => { | |||
| @@ -72,16 +80,16 @@ export const useInspectVarsCrudCommon = ({ | |||
| }, [getNodeInspectVars]) | |||
| const hasSetInspectVar = useCallback((nodeId: string, name: string, sysVars: VarInInspect[], conversationVars: VarInInspect[]) => { | |||
| const isEnv = isENV([nodeId]) | |||
| if (isEnv) // always have value | |||
| return true | |||
| const isSys = isSystemVar([nodeId]) | |||
| if (isSys) | |||
| return sysVars.some(varItem => varItem.selector?.[1]?.replace('sys.', '') === name) | |||
| const isChatVar = isConversationVar([nodeId]) | |||
| if (isChatVar) | |||
| return conversationVars.some(varItem => varItem.selector?.[1] === name) | |||
| return getInspectVar(nodeId, name) !== undefined | |||
| const isEnv = isENV([nodeId]) | |||
| if (isEnv) // always have value | |||
| return true | |||
| const isSys = isSystemVar([nodeId]) | |||
| if (isSys) | |||
| return sysVars.some(varItem => varItem.selector?.[1]?.replace('sys.', '') === name) | |||
| const isChatVar = isConversationVar([nodeId]) | |||
| if (isChatVar) | |||
| return conversationVars.some(varItem => varItem.selector?.[1] === name) | |||
| return getInspectVar(nodeId, name) !== undefined | |||
| }, [getInspectVar]) | |||
| const hasNodeInspectVars = useCallback((nodeId: string) => { | |||
| @@ -91,6 +99,11 @@ export const useInspectVarsCrudCommon = ({ | |||
| const fetchInspectVarValue = useCallback(async (selector: ValueSelector) => { | |||
| const { | |||
| setNodeInspectVars, | |||
| buildInTools, | |||
| customTools, | |||
| workflowTools, | |||
| mcpTools, | |||
| dataSourceList, | |||
| } = workflowStore.getState() | |||
| const nodeId = selector[0] | |||
| const isSystemVar = nodeId === 'sys' | |||
| @@ -103,8 +116,26 @@ export const useInspectVarsCrudCommon = ({ | |||
| invalidateConversationVarValues() | |||
| return | |||
| } | |||
| const { getNodes } = store.getState() | |||
| const nodeArr = getNodes() | |||
| const currentNode = nodeArr.find(node => node.id === nodeId) | |||
| const allPluginInfoList = { | |||
| buildInTools, | |||
| customTools, | |||
| workflowTools, | |||
| mcpTools, | |||
| dataSourceList: dataSourceList ?? [], | |||
| } | |||
| const currentNodeOutputVars = toNodeOutputVars([currentNode], false, () => true, [], [], [], allPluginInfoList, schemaTypeDefinitions) | |||
| const vars = await fetchNodeInspectVars(flowType, flowId, nodeId) | |||
| setNodeInspectVars(nodeId, vars) | |||
| const varsWithSchemaType = vars.map((varItem) => { | |||
| const schemaType = currentNodeOutputVars[0].vars.find(v => v.variable === varItem.name)?.schemaType || '' | |||
| return { | |||
| ...varItem, | |||
| schemaType, | |||
| } | |||
| }) | |||
| setNodeInspectVars(nodeId, varsWithSchemaType) | |||
| }, [workflowStore, flowType, flowId, invalidateSysVarValues, invalidateConversationVarValues]) | |||
| // after last run would call this | |||
| @@ -115,23 +146,23 @@ export const useInspectVarsCrudCommon = ({ | |||
| } = workflowStore.getState() | |||
| const nodes = produce(nodesWithInspectVars, (draft) => { | |||
| const nodeInfo = allNodes.find(node => node.id === nodeId) | |||
| if (nodeInfo) { | |||
| const index = draft.findIndex(node => node.nodeId === nodeId) | |||
| if (index === -1) { | |||
| draft.unshift({ | |||
| nodeId, | |||
| nodeType: nodeInfo.data.type, | |||
| title: nodeInfo.data.title, | |||
| vars: payload, | |||
| nodePayload: nodeInfo.data, | |||
| }) | |||
| } | |||
| else { | |||
| draft[index].vars = payload | |||
| // put the node to the topAdd commentMore actions | |||
| draft.unshift(draft.splice(index, 1)[0]) | |||
| } | |||
| if (nodeInfo) { | |||
| const index = draft.findIndex(node => node.nodeId === nodeId) | |||
| if (index === -1) { | |||
| draft.unshift({ | |||
| nodeId, | |||
| nodeType: nodeInfo.data.type, | |||
| title: nodeInfo.data.title, | |||
| vars: payload, | |||
| nodePayload: nodeInfo.data, | |||
| }) | |||
| } | |||
| else { | |||
| draft[index].vars = payload | |||
| // put the node to the topAdd commentMore actions | |||
| draft.unshift(draft.splice(index, 1)[0]) | |||
| } | |||
| } | |||
| }) | |||
| setNodesWithInspectVars(nodes) | |||
| handleCancelNodeSuccessStatus(nodeId) | |||
| @@ -140,14 +171,14 @@ export const useInspectVarsCrudCommon = ({ | |||
| const hasNodeInspectVar = useCallback((nodeId: string, varId: string) => { | |||
| const { nodesWithInspectVars } = workflowStore.getState() | |||
| const targetNode = nodesWithInspectVars.find(item => item.nodeId === nodeId) | |||
| if(!targetNode || !targetNode.vars) | |||
| if (!targetNode || !targetNode.vars) | |||
| return false | |||
| return targetNode.vars.some(item => item.id === varId) | |||
| }, [workflowStore]) | |||
| const deleteInspectVar = useCallback(async (nodeId: string, varId: string) => { | |||
| const { deleteInspectVar } = workflowStore.getState() | |||
| if(hasNodeInspectVar(nodeId, varId)) { | |||
| if (hasNodeInspectVar(nodeId, varId)) { | |||
| await doDeleteInspectVar(varId) | |||
| deleteInspectVar(nodeId, varId) | |||
| } | |||
| @@ -215,7 +246,7 @@ export const useInspectVarsCrudCommon = ({ | |||
| const isSysVar = nodeId === 'sys' | |||
| const data = await doResetToLastRunValue(varId) | |||
| if(isSysVar) | |||
| if (isSysVar) | |||
| invalidateSysVarValues() | |||
| else | |||
| resetToLastRunVar(nodeId, varId, data.value) | |||