From b0cc339c066879e56c1e73c973494d0541273e76 Mon Sep 17 00:00:00 2001 From: Joel Date: Thu, 24 Apr 2025 14:24:15 +0800 Subject: [PATCH] feat: output to vars --- .../nodes/_base/hooks/use-one-step-run.ts | 15 +++-- .../store/workflow/debug/last-run-slice.ts | 14 ++--- web/app/components/workflow/utils/debug.ts | 56 +++++++++++++++++++ web/types/workflow.ts | 2 +- 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 web/app/components/workflow/utils/debug.ts 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 74ea1df288..6fb89af658 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 @@ -33,6 +33,7 @@ import { ssePost } from '@/service/base' import { noop } from 'lodash-es' import { getInputVars as doGetInputVars } from '@/app/components/base/prompt-editor/constants' import type { NodeRunResult, NodeTracing } from '@/types/workflow' +import { getNodeWithVar } from '../../../utils/debug' const { checkValid: checkLLMValid } = LLMDefault const { checkValid: checkKnowledgeRetrievalValid } = KnowledgeRetrievalDefault const { checkValid: checkIfElseValid } = IfElseDefault @@ -156,15 +157,21 @@ const useOneStepRun = ({ const workflowStore = useWorkflowStore() const { setLastRunNodeInfo, - setNodeInspectVars: setCurrentNodeVars, + setNodeInspectVars, setShowSingleRunPanel, } = workflowStore.getState() const [runResult, doSetRunResult] = useState(null) + const nodeData = data const setRunResult = useCallback((data: NodeRunResult | null) => { doSetRunResult(data) - setLastRunNodeInfo(id, data as any) - setCurrentNodeVars(id, data as any) - }, [id, setLastRunNodeInfo, setCurrentNodeVars]) + setLastRunNodeInfo(id, data!) + setNodeInspectVars(id, getNodeWithVar({ + nodeId: id, + nodeType: nodeData.type, + title: nodeData.title, + values: data?.outputs || {}, + })) + }, [nodeData, id, setLastRunNodeInfo, setNodeInspectVars]) 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 index 6fc16ed3d5..b72d181c82 100644 --- a/web/app/components/workflow/store/workflow/debug/last-run-slice.ts +++ b/web/app/components/workflow/store/workflow/debug/last-run-slice.ts @@ -1,18 +1,16 @@ -import type { NodeTracing } from '@/types/workflow' import produce from 'immer' import type { StateCreator } from 'zustand' - -type NodeInfo = NodeTracing +import type { NodeRunResult } from '@/types/workflow' type LastRunState = { - nodes: NodeInfo[] + nodes: NodeRunResult[] } type LastRunActions = { - setLastRunInfos: (vars: NodeInfo[]) => void - getLastRunInfos: () => NodeInfo[] - setLastRunNodeInfo: (nodeId: string, payload: NodeInfo) => void - getLastRunNodeInfo: (nodeId: string) => NodeInfo | undefined + setLastRunInfos: (vars: NodeRunResult[]) => void + getLastRunInfos: () => NodeRunResult[] + setLastRunNodeInfo: (nodeId: string, payload: NodeRunResult) => void + getLastRunNodeInfo: (nodeId: string) => NodeRunResult | undefined getLastRunVar: (nodeId: string, key: string) => any } diff --git a/web/app/components/workflow/utils/debug.ts b/web/app/components/workflow/utils/debug.ts new file mode 100644 index 0000000000..17814141f6 --- /dev/null +++ b/web/app/components/workflow/utils/debug.ts @@ -0,0 +1,56 @@ +import type { NodeWithVar, VarInInspect } from '@/types/workflow' +import { VarInInspectType } from '@/types/workflow' +import type { BlockEnum } from '../types' +import { VarType } from '../types' + +type OutputToVarInInspectParams = { + nodeId: string + name: string + value: any +} +export const outputToVarInInspect = ({ + nodeId, + name, + value, +}: OutputToVarInInspectParams): VarInInspect => { + return { + id: `${Date.now()}`, // TODO: wait for api + type: VarInInspectType.node, + name, + description: '', + selector: [nodeId, name], + value_type: VarType.string, // TODO: wait for api or get from node + value, + edited: false, + } +} + +type NodeWithVarParams = { + nodeId: string + nodeType: BlockEnum + title: string + values: Record +} +export const getNodeWithVar = ({ + nodeId, + nodeType, + title, + values, +}: NodeWithVarParams): NodeWithVar => { + const res: NodeWithVar = { + nodeId, + nodeType, + title, + vars: [], + } + + res.vars = Object.entries(values).map(([key, value]) => { + return outputToVarInInspect({ + nodeId, + name: key, + value, + }) + }) + + return res +} diff --git a/web/types/workflow.ts b/web/types/workflow.ts index ece3b6a31c..5cb4044a9e 100644 --- a/web/types/workflow.ts +++ b/web/types/workflow.ts @@ -379,7 +379,7 @@ export enum VarInInspectType { } export type VarInInspect = { - id?: string // value parse from output not has id + id: string type: VarInInspectType name: string description: string