diff --git a/web/app/components/workflow/hooks/use-inspect-vars-crud.ts b/web/app/components/workflow/hooks/use-inspect-vars-crud.ts index b29bd5ff85..9353234ad8 100644 --- a/web/app/components/workflow/hooks/use-inspect-vars-crud.ts +++ b/web/app/components/workflow/hooks/use-inspect-vars-crud.ts @@ -13,6 +13,7 @@ import { useInvalidateSysVarValues, useLastRun, useResetConversationVar, + useResetToLastRunValue, useSysVarValues, } from '@/service/use-workflow' import { useCallback, useEffect, useState } from 'react' @@ -34,11 +35,13 @@ const useInspectVarsCrud = () => { deleteNodeInspectVars: deleteNodeInspectVarsInStore, deleteInspectVar: deleteInspectVarInStore, setNodesWithInspectVars, + resetToLastRunVar: resetToLastRunVarInStore, } = workflowStore.getState() const { data: conversationVars } = useConversationVarValues(appId) const invalidateConversationVarValues = useInvalidateConversationVarValues(appId) const { mutateAsync: doResetConversationVar } = useResetConversationVar(appId) + const { mutateAsync: doResetToLastRunValue } = useResetToLastRunValue(appId) const { data: systemVars } = useSysVarValues(appId) const invalidateSysVarValues = useInvalidateSysVarValues(appId) @@ -186,13 +189,10 @@ const useInspectVarsCrud = () => { const { data } = useLastRun(appId, currNodeId || '', !!currNodeId) useEffect(() => { if (data && currNodeId && currEditVarId) { - const inspectVar = getNodeInspectVars(currNodeId)?.vars?.find(item => item.id === currEditVarId); - (async () => { - await editInspectVarValue(currNodeId, currEditVarId, data.outputs?.[inspectVar?.selector?.[1] || '']) - setCurrNodeId(null) - })() + const inspectVar = getNodeInspectVars(currNodeId)?.vars?.find(item => item.id === currEditVarId) + resetToLastRunVarInStore(currNodeId, currEditVarId, data.outputs?.[inspectVar?.selector?.[1] || '']) } - }, [data, currNodeId, currEditVarId, getNodeInspectVars, editInspectVarValue]) + }, [data, currNodeId, currEditVarId, getNodeInspectVars, editInspectVarValue, resetToLastRunVarInStore]) const renameInspectVarName = async (nodeId: string, oldName: string, newName: string) => { const varId = getVarId(nodeId, oldName) @@ -215,7 +215,8 @@ const useInspectVarsCrud = () => { return inspectVar.edited }, [getInspectVar]) - const resetToLastRunVar = (nodeId: string, varId: string) => { + const resetToLastRunVar = async (nodeId: string, varId: string) => { + await doResetToLastRunValue(varId) setCurrNodeId(nodeId) setCurrEditVarId(varId) } diff --git a/web/app/components/workflow/store/workflow/debug/inspect-vars-slice.ts b/web/app/components/workflow/store/workflow/debug/inspect-vars-slice.ts index 8c56ba9a3a..51f66bee13 100644 --- a/web/app/components/workflow/store/workflow/debug/inspect-vars-slice.ts +++ b/web/app/components/workflow/store/workflow/debug/inspect-vars-slice.ts @@ -16,6 +16,7 @@ type InspectVarsActions = { setNodeInspectVars: (nodeId: string, payload: VarInInspect[]) => void deleteNodeInspectVars: (nodeId: string) => void setInspectVarValue: (nodeId: string, name: string, value: any) => void + resetToLastRunVar: (nodeId: string, varId: string, value: any) => void renameInspectVarName: (nodeId: string, varId: string, selector: ValueSelector) => void deleteInspectVar: (nodeId: string, varId: string) => void } @@ -85,6 +86,24 @@ export const createInspectVarsSlice: StateCreator = (set, } }) }, + resetToLastRunVar: (nodeId, varId, value) => { + set((state: InspectVarsSliceShape) => { + const nodes = produce(state.nodesWithInspectVars, (draft) => { + const targetNode = draft.find(node => node.nodeId === nodeId) + if (!targetNode) + return + const targetVar = targetNode.vars.find(varItem => varItem.id === varId) + if(!targetVar) + return + targetVar.value = value + targetVar.edited = false + }, + ) + return { + nodesWithInspectVars: nodes, + } + }) + }, renameInspectVarName: (nodeId, varId, selector) => { set((state: InspectVarsSliceShape) => { const nodes = produce(state.nodesWithInspectVars, (draft) => { diff --git a/web/service/use-workflow.ts b/web/service/use-workflow.ts index b1bba9531f..e070b46e65 100644 --- a/web/service/use-workflow.ts +++ b/web/service/use-workflow.ts @@ -136,6 +136,15 @@ export const useResetConversationVar = (appId: string) => { }) } +export const useResetToLastRunValue = (appId: string) => { + return useMutation({ + mutationKey: [NAME_SPACE, 'reset to last run value', appId], + mutationFn: async (varId: string) => { + return put(`apps/${appId}/workflows/draft/variables/${varId}/reset`) + }, + }) +} + export const useSysVarValuesKey = [NAME_SPACE, 'sys-variable'] export const useSysVarValues = (appId: string) => { return useQuery({