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

View File

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

View File

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