From 82aca36fc1d8931260ffdba4f796d487327d5fd9 Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 28 Apr 2025 11:40:04 +0800 Subject: [PATCH] chore: use query to manage last run to decrease the code complex --- .../_base/components/workflow-panel/index.tsx | 3 +- .../workflow-panel/last-run/index.tsx | 27 ++------ .../workflow-panel/last-run/use-last-run.ts | 4 -- .../nodes/_base/hooks/use-one-step-run.ts | 8 +-- .../store/workflow/debug/last-run-slice.ts | 67 ------------------- .../workflow/store/workflow/index.ts | 4 -- web/service/use-workflow.ts | 13 +++- 7 files changed, 21 insertions(+), 105 deletions(-) delete mode 100644 web/app/components/workflow/store/workflow/debug/last-run-slice.ts 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 a378d51108..b6083ae68c 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 @@ -154,7 +154,6 @@ const BasePanel: FC = ({ setSingleRunParams, setRunInputData, hasLastRunData, - isDataFromHistory, handleRun, getExistVarValuesInForms, getFilteredExistVarForms, @@ -304,7 +303,7 @@ const BasePanel: FC = ({ )} {tabType === TabType.lastRun && ( - + )} { 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 cca5836926..5093ac69ee 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 @@ -1,56 +1,37 @@ 'use client' import ResultPanel from '@/app/components/workflow/run/result-panel' -import { useWorkflowStore } from '@/app/components/workflow/store' import { NodeRunningStatus } from '@/app/components/workflow/types' import type { FC } from 'react' -import React, { useEffect, useState } from 'react' +import React from 'react' import NoData from './no-data' import { useLastRun } from '@/service/use-workflow' import Loading from '@/app/components/base/loading' type Props = { - isDataFromHistory: boolean appId: string nodeId: string runningStatus?: NodeRunningStatus } const LastRun: FC = ({ - isDataFromHistory, appId, nodeId, runningStatus, }) => { - const workflowStore = useWorkflowStore() - - const { - getLastRunNodeInfo, - } = workflowStore.getState() - const { data: runResultFromHistory, isFetching } = useLastRun(appId, nodeId, isDataFromHistory) - const [runResultFromSingleRun, setRunResult] = useState(isDataFromHistory ? getLastRunNodeInfo(nodeId) : null) - const runResult = isDataFromHistory ? runResultFromHistory : runResultFromSingleRun const isRunning = runningStatus === NodeRunningStatus.Running - - // get data from current running result - useEffect(() => { - if (isDataFromHistory) - return - - setRunResult(getLastRunNodeInfo(nodeId)) - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [runningStatus, isDataFromHistory]) + const { data: runResult, isFetching } = useLastRun(appId, nodeId, !isRunning) const handleSingleRun = () => { console.log('run') } - if (isDataFromHistory && isFetching) + if (isFetching) return if (isRunning) return - if (!runResultFromSingleRun) { + if (!runResult) { return ( ) 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 8a6443ea50..cb69155a9e 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 @@ -29,10 +29,8 @@ const useLastRun = ({ setSingleRunParams(childPanelRef.current?.singleRunParams) }, [doSetRunInputData]) - const [isDataFromHistory, setIsDataFromHistory] = useState(true) const [tabType, setTabType] = useState(TabType.settings) const handleRun = async (data: Record) => { - setIsDataFromHistory(false) setTabType(TabType.lastRun) callRunApi(data) hideSingleRun() @@ -40,7 +38,6 @@ const useLastRun = ({ const handleTabClicked = useCallback((type: TabType) => { setTabType(type) - setIsDataFromHistory(true) }, []) const hasLastRunData = true // TODO: add disabled logic @@ -109,7 +106,6 @@ const useLastRun = ({ setSingleRunParams, setRunInputData, hasLastRunData, - isDataFromHistory, handleRun, getExistVarValuesInForms, getFilteredExistVarForms, 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 2cc4c2e260..8579a5f034 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 @@ -50,6 +50,7 @@ const { checkValid: checkLoopValid } = LoopDefault import { useStoreApi, } from 'reactflow' +import { useInvalidLastRun } from '@/service/use-workflow' // eslint-disable-next-line ts/no-unsafe-function-type const checkValidFns: Record = { [BlockEnum.LLM]: checkLLMValid, @@ -158,20 +159,19 @@ const useOneStepRun = ({ const store = useStoreApi() const workflowStore = useWorkflowStore() const { - - setLastRunNodeInfo, appendNodeInspectVars, setShowSingleRunPanel, } = workflowStore.getState() + const invalidLastRun = useInvalidLastRun(appId!, id) const [runResult, doSetRunResult] = useState(null) const setRunResult = useCallback(async (data: NodeRunResult | null) => { doSetRunResult(data) - setLastRunNodeInfo(id, data!) + invalidLastRun() const vars = await fetchNodeInspectVars(appId!, data!.id) const { getNodes } = store.getState() const nodes = getNodes() appendNodeInspectVars(id, vars, nodes) - }, [setLastRunNodeInfo, id, appId, store, appendNodeInspectVars]) + }, [invalidLastRun, appId, store, appendNodeInspectVars, id]) const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate() const [canShowSingleRun, setCanShowSingleRun] = useState(false) diff --git a/web/app/components/workflow/store/workflow/debug/last-run-slice.ts b/web/app/components/workflow/store/workflow/debug/last-run-slice.ts deleted file mode 100644 index b72d181c82..0000000000 --- a/web/app/components/workflow/store/workflow/debug/last-run-slice.ts +++ /dev/null @@ -1,67 +0,0 @@ -import produce from 'immer' -import type { StateCreator } from 'zustand' -import type { NodeRunResult } from '@/types/workflow' - -type LastRunState = { - nodes: NodeRunResult[] -} - -type LastRunActions = { - setLastRunInfos: (vars: NodeRunResult[]) => void - getLastRunInfos: () => NodeRunResult[] - setLastRunNodeInfo: (nodeId: string, payload: NodeRunResult) => void - getLastRunNodeInfo: (nodeId: string) => NodeRunResult | undefined - getLastRunVar: (nodeId: string, key: string) => any -} - -export type LastRunSliceShape = LastRunState & LastRunActions - -export const createLastRunSlice: StateCreator = (set, get) => { - return ({ - nodes: [], - setLastRunInfos: (vars) => { - set(() => ({ - nodes: vars, - })) - }, - getLastRunInfos: () => { - return get().nodes - }, - clearVars: () => { - set(() => ({ - nodes: [], - })) - }, - setLastRunNodeInfo: (nodeId, payload) => { - set((state) => { - const prevNodes = state.nodes - const nodes = produce(prevNodes, (draft) => { - const index = prevNodes.findIndex(node => node.id === nodeId) - if (index === -1) - draft.push(payload) - else - draft[index] = payload - }) - - return { - nodes, - } - }) - }, - getLastRunNodeInfo: (nodeId) => { - const nodes = get().nodes - return nodes.find(node => node.node_id === nodeId) - }, - getLastRunVar: (nodeId, key) => { - const node = get().getLastRunNodeInfo(nodeId) - if (!node) - return undefined - - const varItem = node - if (!varItem) - return undefined - - return varItem.outputs?.[key] - }, - }) -} diff --git a/web/app/components/workflow/store/workflow/index.ts b/web/app/components/workflow/store/workflow/index.ts index 2c1d1a4c6c..fe17cc21e0 100644 --- a/web/app/components/workflow/store/workflow/index.ts +++ b/web/app/components/workflow/store/workflow/index.ts @@ -28,8 +28,6 @@ import type { WorkflowDraftSliceShape } from './workflow-draft-slice' import { createWorkflowDraftSlice } from './workflow-draft-slice' import type { WorkflowSliceShape } from './workflow-slice' import { createWorkflowSlice } from './workflow-slice' -import type { LastRunSliceShape } from './debug/last-run-slice' -import { createLastRunSlice } from './debug/last-run-slice' import type { InspectVarsSliceShape } from './debug/inspect-vars-slice' import { createInspectVarsSlice } from './debug/inspect-vars-slice' @@ -50,7 +48,6 @@ export type Shape = VersionSliceShape & WorkflowDraftSliceShape & WorkflowSliceShape & - LastRunSliceShape & InspectVarsSliceShape & LayoutSliceShape & WorkflowAppSliceShape @@ -74,7 +71,6 @@ export const createWorkflowStore = (params: CreateWorkflowStoreParams) => { ...createVersionSlice(...args), ...createWorkflowDraftSlice(...args), ...createWorkflowSlice(...args), - ...createLastRunSlice(...args), ...createInspectVarsSlice(...args), ...createLayoutSlice(...args), ...(injectWorkflowStoreSliceFn?.(...args) || {} as WorkflowAppSliceShape), diff --git a/web/service/use-workflow.ts b/web/service/use-workflow.ts index 38187e6087..e1ec169e7f 100644 --- a/web/service/use-workflow.ts +++ b/web/service/use-workflow.ts @@ -89,11 +89,13 @@ export const usePublishWorkflow = (appId: string) => { }) } +const useLastRunKey = [NAME_SPACE, 'last-run'] export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => { return useQuery({ enabled, - queryKey: [NAME_SPACE, 'last-run', appID, nodeId], + queryKey: [...useLastRunKey, appID, nodeId], queryFn: async () => { + console.log(`fetch last run : ${nodeId}`) // TODO: mock data await sleep(1000) return Promise.resolve({ @@ -125,6 +127,15 @@ export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => { }) } +export const useInvalidLastRun = (appId: string, nodeId: string) => { + return useInvalid([NAME_SPACE, 'last-run', appId, nodeId]) +} + +// Rerun workflow or change the version of workflow +export const useInvalidAllLastRun = (appId: string) => { + return useInvalid([NAME_SPACE, 'last-run', appId]) +} + const useConversationVarValuesKey = [NAME_SPACE, 'conversation-variable'] export const useConversationVarValues = (appId: string) => {