From 7a9faf909ea666a45bd5dc92f4b9c16c3487808a Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 14 Jul 2025 15:10:21 +0800 Subject: [PATCH] feat: workflow use common last run --- .../workflow-app/hooks/use-configs-map.ts | 3 ++ .../hooks/use-inspect-vars-crud.ts | 3 -- .../workflow-app/hooks/use-workflow-run.ts | 6 +-- .../workflow/header/header-in-restoring.tsx | 7 ++- .../components/workflow/hooks-store/store.ts | 3 ++ .../hooks/use-fetch-workflow-inspect-vars.ts | 7 ++- .../hooks/use-inspect-vars-crud-common.ts | 23 ++++---- .../_base/components/workflow-panel/index.tsx | 5 ++ .../workflow-panel/last-run/index.tsx | 5 +- .../workflow-panel/last-run/use-last-run.ts | 9 +++- .../nodes/_base/hooks/use-one-step-run.ts | 20 +++---- .../workflow/panel/debug-and-preview/hooks.ts | 6 +-- .../panel/version-history-panel/index.tsx | 6 +-- web/service/use-flow.ts | 29 +++++++++++ web/service/use-workflow.ts | 52 ++++++++++--------- web/service/utils.ts | 10 ++++ web/service/workflow.ts | 16 +++--- web/types/common.ts | 4 ++ 18 files changed, 143 insertions(+), 71 deletions(-) create mode 100644 web/service/use-flow.ts create mode 100644 web/service/utils.ts create mode 100644 web/types/common.ts diff --git a/web/app/components/workflow-app/hooks/use-configs-map.ts b/web/app/components/workflow-app/hooks/use-configs-map.ts index 0db4f77856..d2de9b8f43 100644 --- a/web/app/components/workflow-app/hooks/use-configs-map.ts +++ b/web/app/components/workflow-app/hooks/use-configs-map.ts @@ -1,10 +1,13 @@ import { useMemo } from 'react' import { useStore } from '@/app/components/workflow/store' +import { FlowType } from '@/types/common' export const useConfigsMap = () => { const appId = useStore(s => s.appId) return useMemo(() => { return { + flowId: appId!, + flowType: FlowType.appFlow, conversationVarsUrl: `apps/${appId}/workflows/draft/conversation-variables`, systemVarsUrl: `apps/${appId}/workflows/draft/system-variables`, } diff --git a/web/app/components/workflow-app/hooks/use-inspect-vars-crud.ts b/web/app/components/workflow-app/hooks/use-inspect-vars-crud.ts index 109ee85f96..48c65e6b30 100644 --- a/web/app/components/workflow-app/hooks/use-inspect-vars-crud.ts +++ b/web/app/components/workflow-app/hooks/use-inspect-vars-crud.ts @@ -1,12 +1,9 @@ -import { useStore } from '@/app/components/workflow/store' import { useInspectVarsCrudCommon } from '../../workflow/hooks/use-inspect-vars-crud-common' import { useConfigsMap } from './use-configs-map' export const useInspectVarsCrud = () => { - const appId = useStore(s => s.appId) const configsMap = useConfigsMap() const apis = useInspectVarsCrudCommon({ - flowId: appId, ...configsMap, }) diff --git a/web/app/components/workflow-app/hooks/use-workflow-run.ts b/web/app/components/workflow-app/hooks/use-workflow-run.ts index c303211715..15a4857ee6 100644 --- a/web/app/components/workflow-app/hooks/use-workflow-run.ts +++ b/web/app/components/workflow-app/hooks/use-workflow-run.ts @@ -31,11 +31,11 @@ export const useWorkflowRun = () => { const { doSyncWorkflowDraft } = useNodesSyncDraft() const { handleUpdateWorkflowCanvas } = useWorkflowUpdate() const pathname = usePathname() - const appId = useAppStore.getState().appDetail?.id - const invalidAllLastRun = useInvalidAllLastRun(appId as string) const configsMap = useConfigsMap() + const { flowId, flowType } = configsMap + const invalidAllLastRun = useInvalidAllLastRun(flowType, flowId) + const { fetchInspectVars } = useSetWorkflowVarsWithValue({ - flowId: appId as string, ...configsMap, }) diff --git a/web/app/components/workflow/header/header-in-restoring.tsx b/web/app/components/workflow/header/header-in-restoring.tsx index afa4e62099..edf9c603ce 100644 --- a/web/app/components/workflow/header/header-in-restoring.tsx +++ b/web/app/components/workflow/header/header-in-restoring.tsx @@ -17,8 +17,8 @@ import { import Toast from '../../base/toast' import RestoringTitle from './restoring-title' import Button from '@/app/components/base/button' -import { useStore as useAppStore } from '@/app/components/app/store' import { useInvalidAllLastRun } from '@/service/use-workflow' +import { useHooksStore } from '../hooks-store' export type HeaderInRestoringProps = { onRestoreSettled?: () => void @@ -28,9 +28,8 @@ const HeaderInRestoring = ({ }: HeaderInRestoringProps) => { const { t } = useTranslation() const workflowStore = useWorkflowStore() - const appDetail = useAppStore.getState().appDetail - - const invalidAllLastRun = useInvalidAllLastRun(appDetail!.id) + const configsMap = useHooksStore(s => s.configsMap) + const invalidAllLastRun = useInvalidAllLastRun(configsMap?.flowType, configsMap?.flowId) const { deleteAllInspectVars, } = workflowStore.getState() diff --git a/web/app/components/workflow/hooks-store/store.ts b/web/app/components/workflow/hooks-store/store.ts index d9a35cd36d..45892714e6 100644 --- a/web/app/components/workflow/hooks-store/store.ts +++ b/web/app/components/workflow/hooks-store/store.ts @@ -17,6 +17,7 @@ import type { Node, ValueSelector, } from '@/app/components/workflow/types' +import type { FlowType } from '@/types/common' export type AvailableNodesMetaData = { nodes: NodeDefault[] @@ -61,6 +62,8 @@ export type CommonHooksFnMap = { resetConversationVar: (varId: string) => Promise invalidateConversationVarValues: () => void configsMap?: { + flowId: string + flowType: FlowType conversationVarsUrl: string systemVarsUrl: string } diff --git a/web/app/components/workflow/hooks/use-fetch-workflow-inspect-vars.ts b/web/app/components/workflow/hooks/use-fetch-workflow-inspect-vars.ts index 27a9ea9d2d..09a106affe 100644 --- a/web/app/components/workflow/hooks/use-fetch-workflow-inspect-vars.ts +++ b/web/app/components/workflow/hooks/use-fetch-workflow-inspect-vars.ts @@ -6,14 +6,17 @@ import type { Node } from '@/app/components/workflow/types' import { fetchAllInspectVars } from '@/service/workflow' import { useInvalidateConversationVarValues, useInvalidateSysVarValues } from '@/service/use-workflow' import { useNodesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-nodes-interactions-without-sync' +import type { FlowType } from '@/types/common' type Params = { + flowType: FlowType flowId: string conversationVarsUrl: string systemVarsUrl: string } export const useSetWorkflowVarsWithValue = ({ + flowType, flowId, conversationVarsUrl, systemVarsUrl, @@ -68,10 +71,10 @@ export const useSetWorkflowVarsWithValue = ({ const fetchInspectVars = useCallback(async () => { invalidateConversationVarValues() invalidateSysVarValues() - const data = await fetchAllInspectVars(flowId) + const data = await fetchAllInspectVars(flowType, flowId) setInspectVarsToStore(data) handleCancelAllNodeSuccessStatus() // to make sure clear node output show the unset status - }, [invalidateConversationVarValues, invalidateSysVarValues, flowId, setInspectVarsToStore, handleCancelAllNodeSuccessStatus]) + }, [invalidateConversationVarValues, invalidateSysVarValues, flowType, flowId, setInspectVarsToStore, handleCancelAllNodeSuccessStatus]) return { fetchInspectVars, } diff --git a/web/app/components/workflow/hooks/use-inspect-vars-crud-common.ts b/web/app/components/workflow/hooks/use-inspect-vars-crud-common.ts index ffcfd81666..5c9851829d 100644 --- a/web/app/components/workflow/hooks/use-inspect-vars-crud-common.ts +++ b/web/app/components/workflow/hooks/use-inspect-vars-crud-common.ts @@ -4,14 +4,8 @@ import type { ValueSelector } from '@/app/components/workflow/types' import type { VarInInspect } from '@/types/workflow' import { VarInInspectType } from '@/types/workflow' import { - useDeleteAllInspectorVars, - useDeleteInspectVar, - useDeleteNodeInspectorVars, - useEditInspectorVar, useInvalidateConversationVarValues, useInvalidateSysVarValues, - useResetConversationVar, - useResetToLastRunValue, } from '@/service/use-workflow' import { useCallback } from 'react' import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils' @@ -19,18 +13,30 @@ 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' type Params = { flowId: string + flowType: FlowType conversationVarsUrl: string systemVarsUrl: string } export const useInspectVarsCrudCommon = ({ flowId, + flowType, conversationVarsUrl, systemVarsUrl, }: Params) => { const workflowStore = useWorkflowStore() + const { + useResetConversationVar, + useResetToLastRunValue, + useDeleteAllInspectorVars, + useDeleteNodeInspectorVars, + useDeleteInspectVar, + useEditInspectorVar, + } = useFLow({ flowType }) const invalidateConversationVarValues = useInvalidateConversationVarValues(conversationVarsUrl!) const { mutateAsync: doResetConversationVar } = useResetConversationVar(flowId) const { mutateAsync: doResetToLastRunValue } = useResetToLastRunValue(flowId) @@ -89,7 +95,6 @@ export const useInspectVarsCrudCommon = ({ const fetchInspectVarValue = useCallback(async (selector: ValueSelector) => { const { - appId, setNodeInspectVars, } = workflowStore.getState() const nodeId = selector[0] @@ -103,9 +108,9 @@ export const useInspectVarsCrudCommon = ({ invalidateConversationVarValues() return } - const vars = await fetchNodeInspectVars(appId, nodeId) + const vars = await fetchNodeInspectVars(flowType, flowId, nodeId) setNodeInspectVars(nodeId, vars) - }, [workflowStore, invalidateSysVarValues, invalidateConversationVarValues]) + }, [workflowStore, flowType, flowId, invalidateSysVarValues, invalidateConversationVarValues]) // after last run would call this const appendNodeInspectVars = useCallback((nodeId: string, payload: VarInInspect[], allNodes: Node[]) => { diff --git a/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx b/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx index 51badaee0f..3f035dea7d 100644 --- a/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx +++ b/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx @@ -59,6 +59,8 @@ import { useLogs } from '@/app/components/workflow/run/hooks' import PanelWrap from '../before-run-form/panel-wrap' import SpecialResultPanel from '@/app/components/workflow/run/special-result-panel' import { Stop } from '@/app/components/base/icons/src/vender/line/mediaAndDevices' +import { useHooksStore } from '@/app/components/workflow/hooks-store' +import { FlowType } from '@/types/common' type BasePanelProps = { children: ReactNode @@ -184,6 +186,7 @@ const BasePanel: FC = ({ nodesMap, } = useNodesMetaData() + const configsMap = useHooksStore(s => s.configsMap) const { isShowSingleRun, hideSingleRun, @@ -205,6 +208,8 @@ const BasePanel: FC = ({ getFilteredExistVarForms, } = useLastRun({ id, + flowId: configsMap?.flowId || '', + flowType: configsMap?.flowType || FlowType.appFlow, data, defaultRunInputData: nodesMap?.[data.type]?.defaultRunInputData || {}, isPaused, diff --git a/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/index.tsx b/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/index.tsx index a029987818..07ea5d354f 100644 --- a/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/index.tsx +++ b/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/index.tsx @@ -8,6 +8,8 @@ import NoData from './no-data' import { useLastRun } from '@/service/use-workflow' import { RiLoader2Line } from '@remixicon/react' import type { NodeTracing } from '@/types/workflow' +import { useHooksStore } from '@/app/components/workflow/hooks-store' +import { FlowType } from '@/types/common' type Props = { appId: string @@ -35,6 +37,7 @@ const LastRun: FC = ({ isPaused, ...otherResultPanelProps }) => { + const configsMap = useHooksStore(s => s.configsMap) const isOneStepRunSucceed = oneStepRunRunningStatus === NodeRunningStatus.Succeeded const isOneStepRunFailed = oneStepRunRunningStatus === NodeRunningStatus.Failed // hide page and return to page would lost the oneStepRunRunningStatus @@ -44,7 +47,7 @@ const LastRun: FC = ({ const hidePageOneStepRunFinished = [NodeRunningStatus.Succeeded, NodeRunningStatus.Failed].includes(hidePageOneStepFinishedStatus!) const canRunLastRun = !isRunAfterSingleRun || isOneStepRunSucceed || isOneStepRunFailed || (pageHasHide && hidePageOneStepRunFinished) - const { data: lastRunResult, isFetching, error } = useLastRun(appId, nodeId, canRunLastRun) + const { data: lastRunResult, isFetching, error } = useLastRun(configsMap?.flowType || FlowType.appFlow, configsMap?.flowId || '', nodeId, canRunLastRun) const isRunning = useMemo(() => { if(isPaused) return false diff --git a/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts b/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts index 014707cdfb..9854f99dcd 100644 --- a/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts +++ b/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts @@ -57,6 +57,8 @@ const singleRunFormParamsHooks: Record = { [BlockEnum.IterationStart]: undefined, [BlockEnum.LoopStart]: undefined, [BlockEnum.LoopEnd]: undefined, + [BlockEnum.DataSource]: undefined, + [BlockEnum.KnowledgeBase]: undefined, } const useSingleRunFormParamsHooks = (nodeType: BlockEnum) => { @@ -89,6 +91,8 @@ const getDataForCheckMoreHooks: Record = { [BlockEnum.Assigner]: undefined, [BlockEnum.LoopStart]: undefined, [BlockEnum.LoopEnd]: undefined, + [BlockEnum.DataSource]: undefined, + [BlockEnum.KnowledgeBase]: undefined, } const useGetDataForCheckMoreHooks = (nodeType: BlockEnum) => { @@ -119,6 +123,8 @@ const useLastRun = ({ const { id, + flowId, + flowType, data, } = oneStepRunParams const oneStepRunRes = useOneStepRun({ @@ -129,7 +135,6 @@ const useLastRun = ({ }) const { - appId, hideSingleRun, handleRun: doCallRunApi, getInputVars, @@ -199,7 +204,7 @@ const useLastRun = ({ setInitShowLastRunTab(false) // eslint-disable-next-line react-hooks/exhaustive-deps }, [initShowLastRunTab]) - const invalidLastRun = useInvalidLastRun(appId!, id) + const invalidLastRun = useInvalidLastRun(flowType, flowId, id) const handleRunWithParams = async (data: Record) => { const { isValid } = checkValid() diff --git a/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts b/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts index 769804a20b..10e269084f 100644 --- a/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts +++ b/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts @@ -11,7 +11,6 @@ import { getNodeInfoById, isConversationVar, isENV, isSystemVar, toNodeOutputVar import type { CommonNodeType, InputVar, ValueSelector, Var, Variable } from '@/app/components/workflow/types' import { BlockEnum, InputVarType, NodeRunningStatus, VarType } from '@/app/components/workflow/types' -import { useStore as useAppStore } from '@/app/components/app/store' import { useStore, useWorkflowStore } from '@/app/components/workflow/store' import { fetchNodeInspectVars, getIterationSingleNodeRunUrl, getLoopSingleNodeRunUrl, singleNodeRun } from '@/service/workflow' import Toast from '@/app/components/base/toast' @@ -52,6 +51,7 @@ import { } from 'reactflow' import { useInvalidLastRun } from '@/service/use-workflow' import useInspectVarsCrud from '../../../hooks/use-inspect-vars-crud' +import type { FlowType } from '@/types/common' // eslint-disable-next-line ts/no-unsafe-function-type const checkValidFns: Record = { [BlockEnum.LLM]: checkLLMValid, @@ -72,6 +72,8 @@ const checkValidFns: Record = { export type Params = { id: string + flowId: string + flowType: FlowType data: CommonNodeType defaultRunInputData: Record moreDataForCheckValid?: any @@ -106,6 +108,8 @@ const varTypeToInputVarType = (type: VarType, { const useOneStepRun = ({ id, + flowId, + flowType, data, defaultRunInputData, moreDataForCheckValid, @@ -153,7 +157,6 @@ const useOneStepRun = ({ const checkValid = checkValidFns[data.type] - const appId = useAppStore.getState().appDetail?.id const [runInputData, setRunInputData] = useState>(defaultRunInputData || {}) const runInputDataRef = useRef(runInputData) const handleSetRunInputData = useCallback((data: Record) => { @@ -168,7 +171,7 @@ const useOneStepRun = ({ const { setShowSingleRunPanel, } = workflowStore.getState() - const invalidLastRun = useInvalidLastRun(appId!, id) + const invalidLastRun = useInvalidLastRun(flowType, flowId!, id) const [runResult, doSetRunResult] = useState(null) const { appendNodeInspectVars, @@ -195,7 +198,7 @@ const useOneStepRun = ({ } // run fail may also update the inspect vars when the node set the error default output. - const vars = await fetchNodeInspectVars(appId!, id) + const vars = await fetchNodeInspectVars(flowType, flowId!, id) const { getNodes } = store.getState() const nodes = getNodes() appendNodeInspectVars(id, vars, nodes) @@ -205,7 +208,7 @@ const useOneStepRun = ({ invalidateSysVarValues() invalidateConversationVarValues() // loop, iteration, variable assigner node can update the conversation variables, but to simple the logic(some nodes may also can update in the future), all nodes refresh. } - }, [isRunAfterSingleRun, runningStatus, appId, id, store, appendNodeInspectVars, invalidLastRun, isStartNode, invalidateSysVarValues, invalidateConversationVarValues]) + }, [isRunAfterSingleRun, runningStatus, flowId, id, store, appendNodeInspectVars, invalidLastRun, isStartNode, invalidateSysVarValues, invalidateConversationVarValues]) const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate() const setNodeRunning = () => { @@ -305,14 +308,14 @@ const useOneStepRun = ({ else { postData.inputs = submitData } - res = await singleNodeRun(appId!, id, postData) as any + res = await singleNodeRun(flowId!, id, postData) as any } else if (isIteration) { setIterationRunResult([]) let _iterationResult: NodeTracing[] = [] let _runResult: any = null ssePost( - getIterationSingleNodeRunUrl(isChatMode, appId!, id), + getIterationSingleNodeRunUrl(isChatMode, flowId!, id), { body: { inputs: submitData } }, { onWorkflowStarted: noop, @@ -415,7 +418,7 @@ const useOneStepRun = ({ let _loopResult: NodeTracing[] = [] let _runResult: any = null ssePost( - getLoopSingleNodeRunUrl(isChatMode, appId!, id), + getLoopSingleNodeRunUrl(isChatMode, flowId!, id), { body: { inputs: submitData } }, { onWorkflowStarted: noop, @@ -633,7 +636,6 @@ const useOneStepRun = ({ } return { - appId, isShowSingleRun, hideSingleRun, showSingleRun, diff --git a/web/app/components/workflow/panel/debug-and-preview/hooks.ts b/web/app/components/workflow/panel/debug-and-preview/hooks.ts index d98cf49812..c82a5598f0 100644 --- a/web/app/components/workflow/panel/debug-and-preview/hooks.ts +++ b/web/app/components/workflow/panel/debug-and-preview/hooks.ts @@ -34,7 +34,7 @@ import { import type { FileEntity } from '@/app/components/base/file-uploader/types' import { getThreadMessages } from '@/app/components/base/chat/utils' import { useInvalidAllLastRun } from '@/service/use-workflow' -import { useParams } from 'next/navigation' +import { useHooksStore } from '../../hooks-store' type GetAbortController = (abortController: AbortController) => void type SendCallback = { @@ -58,8 +58,8 @@ export const useChat = ( const taskIdRef = useRef('') const [isResponding, setIsResponding] = useState(false) const isRespondingRef = useRef(false) - const { appId } = useParams() - const invalidAllLastRun = useInvalidAllLastRun(appId as string) + const configsMap = useHooksStore(s => s.configsMap) + const invalidAllLastRun = useInvalidAllLastRun(configsMap?.flowType, configsMap?.flowId) const { fetchInspectVars } = useSetWorkflowVarsWithValue() const [suggestedQuestions, setSuggestQuestions] = useState([]) const suggestedQuestionsAbortControllerRef = useRef(null) diff --git a/web/app/components/workflow/panel/version-history-panel/index.tsx b/web/app/components/workflow/panel/version-history-panel/index.tsx index 7e070a56ea..662b7bb1ff 100644 --- a/web/app/components/workflow/panel/version-history-panel/index.tsx +++ b/web/app/components/workflow/panel/version-history-panel/index.tsx @@ -17,6 +17,7 @@ import RestoreConfirmModal from './restore-confirm-modal' import DeleteConfirmModal from './delete-confirm-modal' import VersionInfoModal from '@/app/components/app/app-publisher/version-info-modal' import Toast from '@/app/components/base/toast' +import { useHooksStore } from '../../hooks-store' const HISTORY_PER_PAGE = 10 const INITIAL_PAGE = 1 @@ -46,9 +47,8 @@ export const VersionHistoryPanel = ({ const currentVersion = useStore(s => s.currentVersion) const setCurrentVersion = useStore(s => s.setCurrentVersion) const userProfile = useAppContextSelector(s => s.userProfile) - const appId = useStore(s => s.appId) - const pipelineId = useStore(s => s.pipelineId) - const invalidAllLastRun = useInvalidAllLastRun(appId || pipelineId || '') + const configsMap = useHooksStore(s => s.configsMap) + const invalidAllLastRun = useInvalidAllLastRun(configsMap?.flowType, configsMap?.flowId) const { deleteAllInspectVars, } = workflowStore.getState() diff --git a/web/service/use-flow.ts b/web/service/use-flow.ts new file mode 100644 index 0000000000..380ad5ba6d --- /dev/null +++ b/web/service/use-flow.ts @@ -0,0 +1,29 @@ +import type { FlowType } from '@/types/common' +import { + useDeleteAllInspectorVars as useDeleteAllInspectorVarsInner, + useDeleteInspectVar as useDeleteInspectVarInner, + useDeleteNodeInspectorVars as useDeleteNodeInspectorVarsInner, + useEditInspectorVar as useEditInspectorVarInner, + useResetConversationVar as useResetConversationVarInner, + useResetToLastRunValue as useResetToLastRunValueInner, +} from './use-workflow' +import { curry } from 'lodash-es' + +type Params = { + flowType: FlowType +} + +const useFLow = ({ + flowType, +}: Params) => { + return { + useResetConversationVar: curry(useResetConversationVarInner)(flowType), + useResetToLastRunValue: curry(useResetToLastRunValueInner)(flowType), + useDeleteAllInspectorVars: curry(useDeleteAllInspectorVarsInner)(flowType), + useDeleteNodeInspectorVars: curry(useDeleteNodeInspectorVarsInner)(flowType), + useDeleteInspectVar: curry(useDeleteInspectVarInner)(flowType), + useEditInspectorVar: curry(useEditInspectorVarInner)(flowType), + } +} + +export default useFLow diff --git a/web/service/use-workflow.ts b/web/service/use-workflow.ts index 09761b07f5..15cc2014d5 100644 --- a/web/service/use-workflow.ts +++ b/web/service/use-workflow.ts @@ -12,6 +12,8 @@ import type { } from '@/types/workflow' import type { CommonResponse } from '@/models/common' import { useInvalid, useReset } from './use-base' +import type { FlowType } from '@/types/common' +import { getFlowPrefix } from './utils' const NAME_SPACE = 'workflow' @@ -102,12 +104,12 @@ export const usePublishWorkflow = () => { } const useLastRunKey = [NAME_SPACE, 'last-run'] -export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => { +export const useLastRun = (flowType: FlowType, flowId: string, nodeId: string, enabled: boolean) => { return useQuery({ enabled, - queryKey: [...useLastRunKey, appID, nodeId], + queryKey: [...useLastRunKey, flowType, flowId, nodeId], queryFn: async () => { - return get(`apps/${appID}/workflows/draft/nodes/${nodeId}/last-run`, {}, { + return get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/nodes/${nodeId}/last-run`, {}, { silent: true, }) }, @@ -115,13 +117,13 @@ export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => { }) } -export const useInvalidLastRun = (appId: string, nodeId: string) => { - return useInvalid([NAME_SPACE, 'last-run', appId, nodeId]) +export const useInvalidLastRun = (flowType: FlowType, flowId: string, nodeId: string) => { + return useInvalid([NAME_SPACE, flowType, 'last-run', flowId, nodeId]) } // Rerun workflow or change the version of workflow -export const useInvalidAllLastRun = (appId: string) => { - return useInvalid([NAME_SPACE, 'last-run', appId]) +export const useInvalidAllLastRun = (flowType?: FlowType, flowId?: string) => { + return useInvalid([NAME_SPACE, flowType, 'last-run', flowId]) } const useConversationVarValuesKey = [NAME_SPACE, 'conversation-variable'] @@ -141,20 +143,20 @@ export const useInvalidateConversationVarValues = (url: string) => { return useInvalid([...useConversationVarValuesKey, url]) } -export const useResetConversationVar = (appId: string) => { +export const useResetConversationVar = (flowType: FlowType, flowId: string) => { return useMutation({ - mutationKey: [NAME_SPACE, 'reset conversation var', appId], + mutationKey: [NAME_SPACE, flowType, 'reset conversation var', flowId], mutationFn: async (varId: string) => { - return put(`apps/${appId}/workflows/draft/variables/${varId}/reset`) + return put(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}/reset`) }, }) } -export const useResetToLastRunValue = (appId: string) => { +export const useResetToLastRunValue = (flowType: FlowType, flowId: string) => { return useMutation({ - mutationKey: [NAME_SPACE, 'reset to last run value', appId], + mutationKey: [NAME_SPACE, flowType, 'reset to last run value', flowId], mutationFn: async (varId: string): Promise<{ value: any }> => { - return put(`apps/${appId}/workflows/draft/variables/${varId}/reset`) + return put(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}/reset`) }, }) } @@ -175,43 +177,43 @@ export const useInvalidateSysVarValues = (url: string) => { return useInvalid([...useSysVarValuesKey, url]) } -export const useDeleteAllInspectorVars = (appId: string) => { +export const useDeleteAllInspectorVars = (flowType: FlowType, flowId: string) => { return useMutation({ - mutationKey: [NAME_SPACE, 'delete all inspector vars', appId], + mutationKey: [NAME_SPACE, flowType, 'delete all inspector vars', flowId], mutationFn: async () => { - return del(`apps/${appId}/workflows/draft/variables`) + return del(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables`) }, }) } -export const useDeleteNodeInspectorVars = (appId: string) => { +export const useDeleteNodeInspectorVars = (flowType: FlowType, flowId: string) => { return useMutation({ - mutationKey: [NAME_SPACE, 'delete node inspector vars', appId], + mutationKey: [NAME_SPACE, flowType, 'delete node inspector vars', flowId], mutationFn: async (nodeId: string) => { - return del(`apps/${appId}/workflows/draft/nodes/${nodeId}/variables`) + return del(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/nodes/${nodeId}/variables`) }, }) } -export const useDeleteInspectVar = (appId: string) => { +export const useDeleteInspectVar = (flowType: FlowType, flowId: string) => { return useMutation({ - mutationKey: [NAME_SPACE, 'delete inspector var', appId], + mutationKey: [NAME_SPACE, flowType, 'delete inspector var', flowId], mutationFn: async (varId: string) => { - return del(`apps/${appId}/workflows/draft/variables/${varId}`) + return del(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}`) }, }) } // edit the name or value of the inspector var -export const useEditInspectorVar = (appId: string) => { +export const useEditInspectorVar = (flowType: FlowType, flowId: string) => { return useMutation({ - mutationKey: [NAME_SPACE, 'edit inspector var', appId], + mutationKey: [NAME_SPACE, flowType, 'edit inspector var', flowId], mutationFn: async ({ varId, ...rest }: { varId: string name?: string value?: any }) => { - return patch(`apps/${appId}/workflows/draft/variables/${varId}`, { + return patch(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}`, { body: rest, }) }, diff --git a/web/service/utils.ts b/web/service/utils.ts new file mode 100644 index 0000000000..fe26257dc6 --- /dev/null +++ b/web/service/utils.ts @@ -0,0 +1,10 @@ +import { FlowType } from '@/types/common' + +export const flowPrefixMap = { + [FlowType.appFlow]: 'apps', + [FlowType.ragFlow]: 'rag/pipelines', +} + +export const getFlowPrefix = (type: FlowType) => { + return flowPrefixMap[type] || flowPrefixMap[FlowType.appFlow] +} diff --git a/web/service/workflow.ts b/web/service/workflow.ts index edb04f292f..3311d7a205 100644 --- a/web/service/workflow.ts +++ b/web/service/workflow.ts @@ -10,6 +10,8 @@ import type { } from '@/types/workflow' import type { BlockEnum } from '@/app/components/workflow/types' import type { VarInInspect } from '@/types/workflow' +import type { FlowType } from '@/types/common' +import { getFlowPrefix } from './utils' export const fetchWorkflowDraft = (url: string) => { return get(url, {}, { silent: true }) as Promise @@ -78,13 +80,13 @@ export const fetchCurrentValueOfConversationVariable: Fetcher(url, { params }) } -const fetchAllInspectVarsOnePage = async (appId: string, page: number): Promise<{ total: number, items: VarInInspect[] }> => { - return get(`apps/${appId}/workflows/draft/variables`, { +const fetchAllInspectVarsOnePage = async (flowType: FlowType, flowId: string, page: number): Promise<{ total: number, items: VarInInspect[] }> => { + return get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables`, { params: { page, limit: 100 }, }) } -export const fetchAllInspectVars = async (appId: string): Promise => { - const res = await fetchAllInspectVarsOnePage(appId, 1) +export const fetchAllInspectVars = async (flowType: FlowType, flowId: string): Promise => { + const res = await fetchAllInspectVarsOnePage(flowType, flowId, 1) const { items, total } = res if (total <= 100) return items @@ -92,7 +94,7 @@ export const fetchAllInspectVars = async (appId: string): Promise { @@ -101,7 +103,7 @@ export const fetchAllInspectVars = async (appId: string): Promise => { - const { items } = (await get(`apps/${appId}/workflows/draft/nodes/${nodeId}/variables`)) as { items: VarInInspect[] } +export const fetchNodeInspectVars = async (flowType: FlowType, flowId: string, nodeId: string): Promise => { + const { items } = (await get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/nodes/${nodeId}/variables`)) as { items: VarInInspect[] } return items } diff --git a/web/types/common.ts b/web/types/common.ts new file mode 100644 index 0000000000..9f55b740a8 --- /dev/null +++ b/web/types/common.ts @@ -0,0 +1,4 @@ +export enum FlowType { + appFlow = 'appFlow', + ragFlow = 'ragFlow', +}