| useWorkflowRun, | useWorkflowRun, | ||||
| useWorkflowStartRun, | useWorkflowStartRun, | ||||
| } from '../hooks' | } from '../hooks' | ||||
| import { useStore, useWorkflowStore } from '@/app/components/workflow/store' | |||||
| import { useWorkflowStore } from '@/app/components/workflow/store' | |||||
| type WorkflowMainProps = Pick<WorkflowProps, 'nodes' | 'edges' | 'viewport'> | type WorkflowMainProps = Pick<WorkflowProps, 'nodes' | 'edges' | 'viewport'> | ||||
| const WorkflowMain = ({ | const WorkflowMain = ({ | ||||
| handleWorkflowStartRunInChatflow, | handleWorkflowStartRunInChatflow, | ||||
| handleWorkflowStartRunInWorkflow, | handleWorkflowStartRunInWorkflow, | ||||
| } = useWorkflowStartRun() | } = useWorkflowStartRun() | ||||
| const appId = useStore(s => s.appId) | |||||
| const availableNodesMetaData = useAvailableNodesMetaData() | const availableNodesMetaData = useAvailableNodesMetaData() | ||||
| const { getWorkflowRunAndTraceUrl } = useGetRunAndTraceUrl() | const { getWorkflowRunAndTraceUrl } = useGetRunAndTraceUrl() | ||||
| const { | const { | ||||
| handleExportDSL, | handleExportDSL, | ||||
| } = useDSL() | } = useDSL() | ||||
| const { fetchInspectVars } = useSetWorkflowVarsWithValue({ | const { fetchInspectVars } = useSetWorkflowVarsWithValue({ | ||||
| flowId: appId, | |||||
| ...useConfigsMap(), | ...useConfigsMap(), | ||||
| }) | }) | ||||
| const { | const { |
| return { | return { | ||||
| flowId: appId!, | flowId: appId!, | ||||
| flowType: FlowType.appFlow, | flowType: FlowType.appFlow, | ||||
| conversationVarsUrl: `apps/${appId}/workflows/draft/conversation-variables`, | |||||
| systemVarsUrl: `apps/${appId}/workflows/draft/system-variables`, | |||||
| } | } | ||||
| }, [appId]) | }, [appId]) | ||||
| } | } |
| configsMap?: { | configsMap?: { | ||||
| flowId: string | flowId: string | ||||
| flowType: FlowType | flowType: FlowType | ||||
| conversationVarsUrl: string | |||||
| systemVarsUrl: string | |||||
| } | } | ||||
| } | } | ||||
| type Params = { | type Params = { | ||||
| flowType: FlowType | flowType: FlowType | ||||
| flowId: string | flowId: string | ||||
| conversationVarsUrl: string | |||||
| systemVarsUrl: string | |||||
| } | } | ||||
| export const useSetWorkflowVarsWithValue = ({ | export const useSetWorkflowVarsWithValue = ({ | ||||
| flowType, | flowType, | ||||
| flowId, | flowId, | ||||
| conversationVarsUrl, | |||||
| systemVarsUrl, | |||||
| }: Params) => { | }: Params) => { | ||||
| const workflowStore = useWorkflowStore() | const workflowStore = useWorkflowStore() | ||||
| const store = useStoreApi() | const store = useStoreApi() | ||||
| const invalidateConversationVarValues = useInvalidateConversationVarValues(conversationVarsUrl) | |||||
| const invalidateSysVarValues = useInvalidateSysVarValues(systemVarsUrl) | |||||
| const invalidateConversationVarValues = useInvalidateConversationVarValues(flowType, flowId) | |||||
| const invalidateSysVarValues = useInvalidateSysVarValues(flowType, flowId) | |||||
| const { handleCancelAllNodeSuccessStatus } = useNodesInteractionsWithoutSync() | const { handleCancelAllNodeSuccessStatus } = useNodesInteractionsWithoutSync() | ||||
| const setInspectVarsToStore = useCallback((inspectVars: VarInInspect[]) => { | const setInspectVarsToStore = useCallback((inspectVars: VarInInspect[]) => { |
| import type { ValueSelector } from '@/app/components/workflow/types' | import type { ValueSelector } from '@/app/components/workflow/types' | ||||
| import type { VarInInspect } from '@/types/workflow' | import type { VarInInspect } from '@/types/workflow' | ||||
| import { VarInInspectType } from '@/types/workflow' | import { VarInInspectType } from '@/types/workflow' | ||||
| import { | |||||
| useInvalidateConversationVarValues, | |||||
| useInvalidateSysVarValues, | |||||
| } from '@/service/use-workflow' | |||||
| import { useCallback } from 'react' | import { useCallback } from 'react' | ||||
| import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils' | import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils' | ||||
| import produce from 'immer' | import produce from 'immer' | ||||
| type Params = { | type Params = { | ||||
| flowId: string | flowId: string | ||||
| flowType: FlowType | flowType: FlowType | ||||
| conversationVarsUrl: string | |||||
| systemVarsUrl: string | |||||
| } | } | ||||
| export const useInspectVarsCrudCommon = ({ | export const useInspectVarsCrudCommon = ({ | ||||
| flowId, | flowId, | ||||
| flowType, | flowType, | ||||
| conversationVarsUrl, | |||||
| systemVarsUrl, | |||||
| }: Params) => { | }: Params) => { | ||||
| const workflowStore = useWorkflowStore() | const workflowStore = useWorkflowStore() | ||||
| const { | const { | ||||
| useInvalidateConversationVarValues, | |||||
| useInvalidateSysVarValues, | |||||
| useResetConversationVar, | useResetConversationVar, | ||||
| useResetToLastRunValue, | useResetToLastRunValue, | ||||
| useDeleteAllInspectorVars, | useDeleteAllInspectorVars, | ||||
| useDeleteInspectVar, | useDeleteInspectVar, | ||||
| useEditInspectorVar, | useEditInspectorVar, | ||||
| } = useFLow({ flowType }) | } = useFLow({ flowType }) | ||||
| const invalidateConversationVarValues = useInvalidateConversationVarValues(conversationVarsUrl!) | |||||
| const invalidateConversationVarValues = useInvalidateConversationVarValues(flowId) | |||||
| const { mutateAsync: doResetConversationVar } = useResetConversationVar(flowId) | const { mutateAsync: doResetConversationVar } = useResetConversationVar(flowId) | ||||
| const { mutateAsync: doResetToLastRunValue } = useResetToLastRunValue(flowId) | const { mutateAsync: doResetToLastRunValue } = useResetToLastRunValue(flowId) | ||||
| const invalidateSysVarValues = useInvalidateSysVarValues(systemVarsUrl!) | |||||
| const invalidateSysVarValues = useInvalidateSysVarValues(flowId) | |||||
| const { mutateAsync: doDeleteAllInspectorVars } = useDeleteAllInspectorVars(flowId) | const { mutateAsync: doDeleteAllInspectorVars } = useDeleteAllInspectorVars(flowId) | ||||
| const { mutate: doDeleteNodeInspectorVars } = useDeleteNodeInspectorVars(flowId) | const { mutate: doDeleteNodeInspectorVars } = useDeleteNodeInspectorVars(flowId) |
| const useInspectVarsCrud = () => { | const useInspectVarsCrud = () => { | ||||
| const nodesWithInspectVars = useStore(s => s.nodesWithInspectVars) | const nodesWithInspectVars = useStore(s => s.nodesWithInspectVars) | ||||
| const configsMap = useHooksStore(s => s.configsMap) | const configsMap = useHooksStore(s => s.configsMap) | ||||
| const { data: conversationVars } = useConversationVarValues(configsMap?.conversationVarsUrl) | |||||
| const { data: systemVars } = useSysVarValues(configsMap?.systemVarsUrl) | |||||
| const { data: conversationVars } = useConversationVarValues(configsMap?.flowType, configsMap?.flowId) | |||||
| const { data: systemVars } = useSysVarValues(configsMap?.flowType, configsMap?.flowId) | |||||
| const hasNodeInspectVars = useHooksStore(s => s.hasNodeInspectVars) | const hasNodeInspectVars = useHooksStore(s => s.hasNodeInspectVars) | ||||
| const hasSetInspectVar = useHooksStore(s => s.hasSetInspectVar) | const hasSetInspectVar = useHooksStore(s => s.hasSetInspectVar) | ||||
| const fetchInspectVarValue = useHooksStore(s => s.fetchInspectVarValue) | const fetchInspectVarValue = useHooksStore(s => s.fetchInspectVarValue) |
| useDeleteInspectVar as useDeleteInspectVarInner, | useDeleteInspectVar as useDeleteInspectVarInner, | ||||
| useDeleteNodeInspectorVars as useDeleteNodeInspectorVarsInner, | useDeleteNodeInspectorVars as useDeleteNodeInspectorVarsInner, | ||||
| useEditInspectorVar as useEditInspectorVarInner, | useEditInspectorVar as useEditInspectorVarInner, | ||||
| useInvalidateConversationVarValues as useInvalidateConversationVarValuesInner, | |||||
| useInvalidateSysVarValues as useInvalidateSysVarValuesInner, | |||||
| useResetConversationVar as useResetConversationVarInner, | useResetConversationVar as useResetConversationVarInner, | ||||
| useResetToLastRunValue as useResetToLastRunValueInner, | useResetToLastRunValue as useResetToLastRunValueInner, | ||||
| } from './use-workflow' | } from './use-workflow' | ||||
| flowType, | flowType, | ||||
| }: Params) => { | }: Params) => { | ||||
| return { | return { | ||||
| useInvalidateConversationVarValues: curry(useInvalidateConversationVarValuesInner)(flowType), | |||||
| useInvalidateSysVarValues: curry(useInvalidateSysVarValuesInner)(flowType), | |||||
| useResetConversationVar: curry(useResetConversationVarInner)(flowType), | useResetConversationVar: curry(useResetConversationVarInner)(flowType), | ||||
| useResetToLastRunValue: curry(useResetToLastRunValueInner)(flowType), | useResetToLastRunValue: curry(useResetToLastRunValueInner)(flowType), | ||||
| useDeleteAllInspectorVars: curry(useDeleteAllInspectorVarsInner)(flowType), | useDeleteAllInspectorVars: curry(useDeleteAllInspectorVarsInner)(flowType), |
| return useInvalid([NAME_SPACE, flowType, 'last-run', flowId]) | return useInvalid([NAME_SPACE, flowType, 'last-run', flowId]) | ||||
| } | } | ||||
| const useConversationVarValuesKey = [NAME_SPACE, 'conversation-variable'] | |||||
| export const useConversationVarValues = (url?: string) => { | |||||
| export const useConversationVarValues = (flowType?: FlowType, flowId?: string) => { | |||||
| return useQuery({ | return useQuery({ | ||||
| enabled: !!url, | |||||
| queryKey: [...useConversationVarValuesKey, url], | |||||
| enabled: !!flowId, | |||||
| queryKey: [NAME_SPACE, flowType, 'conversation var values', flowId], | |||||
| queryFn: async () => { | queryFn: async () => { | ||||
| const { items } = (await get(url || '')) as { items: VarInInspect[] } | |||||
| const { items } = (await get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/conversation-variables`)) as { items: VarInInspect[] } | |||||
| return items | return items | ||||
| }, | }, | ||||
| }) | }) | ||||
| } | } | ||||
| export const useInvalidateConversationVarValues = (url: string) => { | |||||
| return useInvalid([...useConversationVarValuesKey, url]) | |||||
| export const useInvalidateConversationVarValues = (flowType: FlowType, flowId: string) => { | |||||
| return useInvalid([NAME_SPACE, flowType, 'conversation var values', flowId]) | |||||
| } | } | ||||
| export const useResetConversationVar = (flowType: FlowType, flowId: string) => { | export const useResetConversationVar = (flowType: FlowType, flowId: string) => { | ||||
| } | } | ||||
| export const useSysVarValuesKey = [NAME_SPACE, 'sys-variable'] | export const useSysVarValuesKey = [NAME_SPACE, 'sys-variable'] | ||||
| export const useSysVarValues = (url?: string) => { | |||||
| export const useSysVarValues = (flowType?: FlowType, flowId?: string) => { | |||||
| return useQuery({ | return useQuery({ | ||||
| enabled: !!url, | |||||
| queryKey: [...useSysVarValuesKey, url], | |||||
| enabled: !!flowId, | |||||
| queryKey: [NAME_SPACE, flowType, 'sys var values', flowId], | |||||
| queryFn: async () => { | queryFn: async () => { | ||||
| const { items } = (await get(url || '')) as { items: VarInInspect[] } | |||||
| const { items } = (await get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/system-variables`)) as { items: VarInInspect[] } | |||||
| return items | return items | ||||
| }, | }, | ||||
| }) | }) | ||||
| } | } | ||||
| export const useInvalidateSysVarValues = (url: string) => { | |||||
| return useInvalid([...useSysVarValuesKey, url]) | |||||
| export const useInvalidateSysVarValues = (flowType: FlowType, flowId: string) => { | |||||
| return useInvalid([NAME_SPACE, flowType, 'sys var values', flowId]) | |||||
| } | } | ||||
| export const useDeleteAllInspectorVars = (flowType: FlowType, flowId: string) => { | export const useDeleteAllInspectorVars = (flowType: FlowType, flowId: string) => { |
| [FlowType.ragFlow]: 'rag/pipelines', | [FlowType.ragFlow]: 'rag/pipelines', | ||||
| } | } | ||||
| export const getFlowPrefix = (type: FlowType) => { | |||||
| return flowPrefixMap[type] || flowPrefixMap[FlowType.appFlow] | |||||
| export const getFlowPrefix = (type?: FlowType) => { | |||||
| return flowPrefixMap[type!] || flowPrefixMap[FlowType.appFlow] | |||||
| } | } |