chore: change to backend var struct

This commit is contained in:
Joel 2025-04-22 14:27:27 +08:00
parent a8b6062f0e
commit 33258f1acf
3 changed files with 22 additions and 57 deletions

View File

@ -1,17 +1,9 @@
import type { StateCreator } from 'zustand' import type { StateCreator } from 'zustand'
import produce from 'immer' import produce from 'immer'
import type { NodeTracing } from '@/types/workflow'
type NodeVars = { // TODO: Missing var type
id: string type NodeVars = NodeTracing
name: string
type: string
vars: {
key: string
type: string
value: any
}[]
}
type CurrentVarsState = { type CurrentVarsState = {
currentNodes: NodeVars[] currentNodes: NodeVars[]
@ -33,12 +25,7 @@ export type CurrentVarsSliceShape = CurrentVarsState & CurrentVarsActions
export const createCurrentVarsSlice: StateCreator<CurrentVarsSliceShape> = (set, get) => { export const createCurrentVarsSlice: StateCreator<CurrentVarsSliceShape> = (set, get) => {
return ({ return ({
currentNodes: [{ currentNodes: [],
id: 'abc',
name: '',
type: '',
vars: [],
}],
setCurrentVars: (vars) => { setCurrentVars: (vars) => {
set(() => ({ set(() => ({
currentNodes: vars, currentNodes: vars,
@ -52,13 +39,12 @@ export const createCurrentVarsSlice: StateCreator<CurrentVarsSliceShape> = (set,
currentNodes: [], currentNodes: [],
})) }))
}, },
setCurrentNodeVars: (nodeId, vars) => { setCurrentNodeVars: (nodeId, payload) => {
set((state) => { set((state) => {
const nodes = state.currentNodes.map((node) => { const nodes = state.currentNodes.map((node) => {
if (node.id === nodeId) { // eslint-disable-next-line curly
return produce(node, (draft) => { if (node.node_id === nodeId) {
draft.vars = vars.vars return payload
})
} }
return node return node
@ -70,14 +56,14 @@ export const createCurrentVarsSlice: StateCreator<CurrentVarsSliceShape> = (set,
}, },
clearCurrentNodeVars: (nodeId) => { clearCurrentNodeVars: (nodeId) => {
set(produce((state: CurrentVarsSliceShape) => { set(produce((state: CurrentVarsSliceShape) => {
const nodes = state.currentNodes.filter(node => node.id !== nodeId) const nodes = state.currentNodes.filter(node => node.node_id !== nodeId)
state.currentNodes = nodes state.currentNodes = nodes
}, },
)) ))
}, },
getCurrentNodeVars: (nodeId) => { getCurrentNodeVars: (nodeId) => {
const nodes = get().currentNodes const nodes = get().currentNodes
return nodes.find(node => node.id === nodeId) return nodes.find(node => node.node_id === nodeId)
}, },
hasCurrentNodeVars: (nodeId) => { hasCurrentNodeVars: (nodeId) => {
return !!get().getCurrentNodeVars(nodeId) return !!get().getCurrentNodeVars(nodeId)
@ -87,9 +73,9 @@ export const createCurrentVarsSlice: StateCreator<CurrentVarsSliceShape> = (set,
const nodes = state.currentNodes.map((node) => { const nodes = state.currentNodes.map((node) => {
if (node.id === nodeId) { if (node.id === nodeId) {
return produce(node, (draft) => { return produce(node, (draft) => {
const index = draft.vars.findIndex(v => v.key === key) if (!draft.outputs)
if (index !== -1) draft.outputs = {}
draft.vars[index].value = value draft.outputs[key] = value
}) })
} }
return node return node
@ -102,11 +88,8 @@ export const createCurrentVarsSlice: StateCreator<CurrentVarsSliceShape> = (set,
if (!node) if (!node)
return undefined return undefined
const variable = node.vars.find(v => v.key === key) const variable = node.outputs?.[key]
if (!variable) return variable
return undefined
return variable.value
}, },
}) })
} }

View File

@ -1,18 +1,7 @@
import type { NodeTracing } from '@/types/workflow'
import type { StateCreator } from 'zustand' import type { StateCreator } from 'zustand'
type NodeInfo = { type NodeInfo = NodeTracing
id: string
name: string
type: string
vars: {
key: string
type: string
value: any
}[]
} & {
input: Record<string, any>
output: Record<string, any>
}
type LastRunState = { type LastRunState = {
nodes: NodeInfo[] nodes: NodeInfo[]
@ -29,14 +18,7 @@ export type LastRunSliceShape = LastRunState & LastRunActions
export const createLastRunSlice: StateCreator<LastRunSliceShape> = (set, get) => { export const createLastRunSlice: StateCreator<LastRunSliceShape> = (set, get) => {
return ({ return ({
nodes: [{ nodes: [],
id: 'test',
name: '',
type: '',
vars: [],
input: {},
output: {},
}],
setLastRunInfos: (vars) => { setLastRunInfos: (vars) => {
set(() => ({ set(() => ({
nodes: vars, nodes: vars,
@ -52,18 +34,18 @@ export const createLastRunSlice: StateCreator<LastRunSliceShape> = (set, get) =>
}, },
getLastRunNodeInfo: (nodeId) => { getLastRunNodeInfo: (nodeId) => {
const nodes = get().nodes const nodes = get().nodes
return nodes.find(node => node.id === nodeId) return nodes.find(node => node.node_id === nodeId)
}, },
getLastRunVar: (nodeId, key) => { getLastRunVar: (nodeId, key) => {
const node = get().getLastRunNodeInfo(nodeId) const node = get().getLastRunNodeInfo(nodeId)
if (!node) if (!node)
return undefined return undefined
const varItem = node.vars.find(v => v.key === key) const varItem = node
if (!varItem) if (!varItem)
return undefined return undefined
return varItem.value return varItem.outputs?.[key]
}, },
}) })
} }

View File

@ -38,7 +38,7 @@ export type NodeTracing = {
title: string title: string
inputs: any inputs: any
process_data: any process_data: any
outputs?: any outputs?: Record<string, any>
status: string status: string
parallel_run_id?: string parallel_run_id?: string
error?: string error?: string