From 0600f10e128c850e51043c430302c6cc82e0dd69 Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 28 Apr 2025 17:18:38 +0800 Subject: [PATCH] feat: reset to last run var --- .../workflow/hooks/use-inspect-vars-crud.ts | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) 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 adfd9f1068..f2bd945c70 100644 --- a/web/app/components/workflow/hooks/use-inspect-vars-crud.ts +++ b/web/app/components/workflow/hooks/use-inspect-vars-crud.ts @@ -9,14 +9,17 @@ import { useEditInspectorVar, useInvalidateConversationVarValues, useInvalidateSysVarValues, + useLastRun, useSysVarValues, } from '@/service/use-workflow' +import { useCallback, useEffect, useState } from 'react' const useInspectVarsCrud = () => { const workflowStore = useWorkflowStore() const { appId, nodesWithInspectVars, + getNodeInspectVars, setNodeInspectVars, setInspectVarValue, renameInspectVarName: renameInspectVarNameInStore, @@ -74,14 +77,27 @@ const useInspectVarsCrud = () => { deleteAllInspectVarsInStore() } - const editInspectVarValue = async (nodeId: string, varId: string, value: any) => { + const editInspectVarValue = useCallback(async (nodeId: string, varId: string, value: any) => { await doEditInspectorVar({ nodeId, varId, value, }) setInspectVarValue(nodeId, varId, value) - } + }, [doEditInspectorVar, setInspectVarValue]) + + const [currNodeId, setCurrNodeId] = useState(null) + const [currEditVarId, setCurrEditVarId] = useState(null) + 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) + })() + } + }, [data, currNodeId, currEditVarId, getNodeInspectVars, editInspectVarValue]) const renameInspectVarName = async (nodeId: string, varId: string, selector: ValueSelector) => { await doEditInspectorVar({ @@ -96,11 +112,9 @@ const useInspectVarsCrud = () => { deleteNodeInspectorVars(nodeId) } - const resetToLastRunVar = (nodeId: string, key: string) => { - // const lastRunVar = getLastRunVar(nodeId, key) - // if (lastRunVar) - // editInspectVarValue(nodeId, key, lastRunVar) - // TODO + const resetToLastRunVar = (nodeId: string, varId: string) => { + setCurrNodeId(nodeId) + setCurrEditVarId(varId) } // console.log(conversationVars, systemVars)