feat: vars in node updated call api

pull/21369/head
Joel 1 year ago
parent 3ab5af39a0
commit 4c6c50f7d0

@ -1,3 +1,4 @@
import { fetchNodeInspectVars } from '@/service/workflow'
import { useWorkflowStore } from '../store' import { useWorkflowStore } from '../store'
import type { ValueSelector, VarType } from '../types' import type { ValueSelector, VarType } from '../types'
import { import {
@ -16,6 +17,7 @@ const useInspectVarsCrud = () => {
const { const {
appId, appId,
nodesWithInspectVars, nodesWithInspectVars,
setNodeInspectVars,
getInspectVar, getInspectVar,
setInspectVarValue, setInspectVarValue,
renameInspectVarName: renameInspectVarNameInStore, renameInspectVarName: renameInspectVarNameInStore,
@ -36,12 +38,21 @@ const useInspectVarsCrud = () => {
const { mutate: doEditInspectorVar } = useEditInspectorVar(appId) const { mutate: doEditInspectorVar } = useEditInspectorVar(appId)
const fetchInspectVarValue = (selector: ValueSelector) => { const fetchInspectVarValue = async (selector: ValueSelector) => {
const nodeId = selector[0] const nodeId = selector[0]
const isSystemVar = selector[1] === 'sys' const isSystemVar = selector[1] === 'sys'
const isConversationVar = selector[1] === 'conversation' const isConversationVar = selector[1] === 'conversation'
console.log(nodeId, isSystemVar, isConversationVar) console.log(nodeId, isSystemVar, isConversationVar)
// fetch values under nodeId. system var and conversation var has different fetch method if (isSystemVar) {
invalidateSysVarValues()
return
}
if (isConversationVar) {
invalidateConversationVarValues()
return
}
const vars = await fetchNodeInspectVars(appId, nodeId)
setNodeInspectVars(nodeId, vars)
} }
const deleteInspectVar = async (nodeId: string, varId: string) => { const deleteInspectVar = async (nodeId: string, varId: string) => {

@ -13,7 +13,7 @@ import type { CommonNodeType, InputVar, ValueSelector, Var, Variable } from '@/a
import { BlockEnum, InputVarType, NodeRunningStatus, VarType } from '@/app/components/workflow/types' import { BlockEnum, InputVarType, NodeRunningStatus, VarType } from '@/app/components/workflow/types'
import { useStore as useAppStore } from '@/app/components/app/store' import { useStore as useAppStore } from '@/app/components/app/store'
import { useStore, useWorkflowStore } from '@/app/components/workflow/store' import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
import { getIterationSingleNodeRunUrl, getLoopSingleNodeRunUrl, singleNodeRun } from '@/service/workflow' import { fetchNodeInspectVars, getIterationSingleNodeRunUrl, getLoopSingleNodeRunUrl, singleNodeRun } from '@/service/workflow'
import Toast from '@/app/components/base/toast' import Toast from '@/app/components/base/toast'
import LLMDefault from '@/app/components/workflow/nodes/llm/default' import LLMDefault from '@/app/components/workflow/nodes/llm/default'
import KnowledgeRetrievalDefault from '@/app/components/workflow/nodes/knowledge-retrieval/default' import KnowledgeRetrievalDefault from '@/app/components/workflow/nodes/knowledge-retrieval/default'
@ -33,7 +33,6 @@ 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
@ -48,7 +47,9 @@ const { checkValid: checkParameterExtractorValid } = ParameterExtractorDefault
const { checkValid: checkIterationValid } = IterationDefault const { checkValid: checkIterationValid } = IterationDefault
const { checkValid: checkDocumentExtractorValid } = DocumentExtractorDefault const { checkValid: checkDocumentExtractorValid } = DocumentExtractorDefault
const { checkValid: checkLoopValid } = LoopDefault const { checkValid: checkLoopValid } = LoopDefault
import {
useStoreApi,
} from 'reactflow'
// eslint-disable-next-line ts/no-unsafe-function-type // eslint-disable-next-line ts/no-unsafe-function-type
const checkValidFns: Record<BlockEnum, Function> = { const checkValidFns: Record<BlockEnum, Function> = {
[BlockEnum.LLM]: checkLLMValid, [BlockEnum.LLM]: checkLLMValid,
@ -154,24 +155,23 @@ const useOneStepRun = <T>({
const iterationTimes = iteratorInputKey ? runInputData[iteratorInputKey].length : 0 const iterationTimes = iteratorInputKey ? runInputData[iteratorInputKey].length : 0
const loopTimes = loopInputKey ? runInputData[loopInputKey].length : 0 const loopTimes = loopInputKey ? runInputData[loopInputKey].length : 0
const store = useStoreApi()
const workflowStore = useWorkflowStore() const workflowStore = useWorkflowStore()
const { const {
setLastRunNodeInfo, setLastRunNodeInfo,
setNodeInspectVars, appendNodeInspectVars,
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(async (data: NodeRunResult | null) => {
const setRunResult = useCallback((data: NodeRunResult | null) => {
doSetRunResult(data) doSetRunResult(data)
setLastRunNodeInfo(id, data!) setLastRunNodeInfo(id, data!)
setNodeInspectVars(id, getNodeWithVar({ const vars = await fetchNodeInspectVars(appId!, data!.id)
nodeId: id, const { getNodes } = store.getState()
nodeType: nodeData.type, const nodes = getNodes()
title: nodeData.title, appendNodeInspectVars(id, vars, nodes)
values: data?.outputs || {}, }, [setLastRunNodeInfo, id, appId, store, appendNodeInspectVars])
}))
}, [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)

@ -2,6 +2,7 @@ import type { StateCreator } from 'zustand'
import produce from 'immer' import produce from 'immer'
import type { NodeWithVar, VarInInspect } from '@/types/workflow' import type { NodeWithVar, VarInInspect } from '@/types/workflow'
import type { ValueSelector } from '../../../types' import type { ValueSelector } from '../../../types'
import type { Node } from '@/app/components/workflow/types'
type InspectVarsState = { type InspectVarsState = {
currentFocusNodeId: string | null currentFocusNodeId: string | null
@ -14,7 +15,8 @@ type InspectVarsActions = {
setNodesWithInspectVars: (payload: NodeWithVar[]) => void setNodesWithInspectVars: (payload: NodeWithVar[]) => void
deleteAllInspectVars: () => void deleteAllInspectVars: () => void
getAllInspectVars: () => NodeWithVar[] getAllInspectVars: () => NodeWithVar[]
setNodeInspectVars: (nodeId: string, payload: NodeWithVar) => void setNodeInspectVars: (nodeId: string, payload: VarInInspect[]) => void
appendNodeInspectVars: (nodeId: string, payload: VarInInspect[], allNodes: Node[]) => void
deleteNodeInspectVars: (nodeId: string) => void deleteNodeInspectVars: (nodeId: string) => void
getNodeInspectVars: (nodeId: string) => NodeWithVar | undefined getNodeInspectVars: (nodeId: string) => NodeWithVar | undefined
hasNodeInspectVars: (nodeId: string) => boolean hasNodeInspectVars: (nodeId: string) => boolean
@ -55,9 +57,7 @@ export const createInspectVarsSlice: StateCreator<InspectVarsSliceShape> = (set,
const nodes = produce(prevNodes, (draft) => { const nodes = produce(prevNodes, (draft) => {
const index = prevNodes.findIndex(node => node.nodeId === nodeId) const index = prevNodes.findIndex(node => node.nodeId === nodeId)
if (index === -1) if (index === -1)
draft.push(payload) draft[index].vars = payload
else
draft[index] = payload
}) })
return { return {
@ -65,6 +65,23 @@ export const createInspectVarsSlice: StateCreator<InspectVarsSliceShape> = (set,
} }
}) })
}, },
appendNodeInspectVars: (nodeId, payload, allNodes) => {
set((state) => {
const nodes = state.nodesWithInspectVars
const nodeInfo = allNodes.find(node => node.id === nodeId)
if (nodeInfo) {
nodes.push({
nodeId,
nodeType: nodeInfo.data.type,
title: nodeInfo.data.title,
vars: payload,
})
}
return {
nodesWithInspectVars: nodes,
}
})
},
deleteNodeInspectVars: (nodeId) => { deleteNodeInspectVars: (nodeId) => {
set(produce((state: InspectVarsSliceShape) => { set(produce((state: InspectVarsSliceShape) => {
const nodes = state.nodesWithInspectVars.filter(node => node.nodeId !== nodeId) const nodes = state.nodesWithInspectVars.filter(node => node.nodeId !== nodeId)

@ -1,6 +1,5 @@
import type { NodeWithVar, VarInInspect } from '@/types/workflow' import type { VarInInspect } from '@/types/workflow'
import { VarInInspectType } from '@/types/workflow' import { VarInInspectType } from '@/types/workflow'
import type { BlockEnum } from '../types'
import { VarType } from '../types' import { VarType } from '../types'
type OutputToVarInInspectParams = { type OutputToVarInInspectParams = {
@ -24,33 +23,3 @@ export const outputToVarInInspect = ({
edited: false, 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
}

@ -85,3 +85,9 @@ export const fetchAllInspectVars = async (appId: string): Promise<VarInInspect[]
return res return res
} }
export const fetchNodeInspectVars = async (appId: string, nodeId: string): Promise<VarInInspect[]> => {
// TODO
console.log('fetchNodeInspectVars', appId, nodeId)
return []
}

Loading…
Cancel
Save