feat: output to vars

This commit is contained in:
Joel 2025-04-24 14:24:15 +08:00
parent bec7812e69
commit b0cc339c06
4 changed files with 74 additions and 13 deletions

View File

@ -33,6 +33,7 @@ import { ssePost } from '@/service/base'
import { noop } from 'lodash-es' import { noop } from 'lodash-es'
import { getInputVars as doGetInputVars } from '@/app/components/base/prompt-editor/constants' import { getInputVars as doGetInputVars } from '@/app/components/base/prompt-editor/constants'
import type { NodeRunResult, NodeTracing } from '@/types/workflow' import type { NodeRunResult, NodeTracing } from '@/types/workflow'
import { getNodeWithVar } from '../../../utils/debug'
const { checkValid: checkLLMValid } = LLMDefault const { checkValid: checkLLMValid } = LLMDefault
const { checkValid: checkKnowledgeRetrievalValid } = KnowledgeRetrievalDefault const { checkValid: checkKnowledgeRetrievalValid } = KnowledgeRetrievalDefault
const { checkValid: checkIfElseValid } = IfElseDefault const { checkValid: checkIfElseValid } = IfElseDefault
@ -156,15 +157,21 @@ const useOneStepRun = <T>({
const workflowStore = useWorkflowStore() const workflowStore = useWorkflowStore()
const { const {
setLastRunNodeInfo, setLastRunNodeInfo,
setNodeInspectVars: setCurrentNodeVars, setNodeInspectVars,
setShowSingleRunPanel, setShowSingleRunPanel,
} = workflowStore.getState() } = workflowStore.getState()
const [runResult, doSetRunResult] = useState<NodeRunResult | null>(null) const [runResult, doSetRunResult] = useState<NodeRunResult | null>(null)
const nodeData = data
const setRunResult = useCallback((data: NodeRunResult | null) => { const setRunResult = useCallback((data: NodeRunResult | null) => {
doSetRunResult(data) doSetRunResult(data)
setLastRunNodeInfo(id, data as any) setLastRunNodeInfo(id, data!)
setCurrentNodeVars(id, data as any) setNodeInspectVars(id, getNodeWithVar({
}, [id, setLastRunNodeInfo, setCurrentNodeVars]) nodeId: id,
nodeType: nodeData.type,
title: nodeData.title,
values: data?.outputs || {},
}))
}, [nodeData, id, setLastRunNodeInfo, setNodeInspectVars])
const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate() const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate()
const [canShowSingleRun, setCanShowSingleRun] = useState(false) const [canShowSingleRun, setCanShowSingleRun] = useState(false)

View File

@ -1,18 +1,16 @@
import type { NodeTracing } from '@/types/workflow'
import produce from 'immer' import produce from 'immer'
import type { StateCreator } from 'zustand' import type { StateCreator } from 'zustand'
import type { NodeRunResult } from '@/types/workflow'
type NodeInfo = NodeTracing
type LastRunState = { type LastRunState = {
nodes: NodeInfo[] nodes: NodeRunResult[]
} }
type LastRunActions = { type LastRunActions = {
setLastRunInfos: (vars: NodeInfo[]) => void setLastRunInfos: (vars: NodeRunResult[]) => void
getLastRunInfos: () => NodeInfo[] getLastRunInfos: () => NodeRunResult[]
setLastRunNodeInfo: (nodeId: string, payload: NodeInfo) => void setLastRunNodeInfo: (nodeId: string, payload: NodeRunResult) => void
getLastRunNodeInfo: (nodeId: string) => NodeInfo | undefined getLastRunNodeInfo: (nodeId: string) => NodeRunResult | undefined
getLastRunVar: (nodeId: string, key: string) => any getLastRunVar: (nodeId: string, key: string) => any
} }

View File

@ -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<string, any>
}
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
}

View File

@ -379,7 +379,7 @@ export enum VarInInspectType {
} }
export type VarInInspect = { export type VarInInspect = {
id?: string // value parse from output not has id id: string
type: VarInInspectType type: VarInInspectType
name: string name: string
description: string description: string