|
|
|
@ -1,15 +1,16 @@
|
|
|
|
import type { StateCreator } from 'zustand'
|
|
|
|
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'
|
|
|
|
|
|
|
|
|
|
|
|
type InspectVarsState = {
|
|
|
|
type InspectVarsState = {
|
|
|
|
currentFocusNodeId: string | null
|
|
|
|
currentFocusNodeId: string | null
|
|
|
|
nodes: NodeWithVar[] // the nodes have data
|
|
|
|
nodesWithInspectVars: NodeWithVar[] // the nodes have data
|
|
|
|
conversationVars: VarInInspect[]
|
|
|
|
conversationVars: VarInInspect[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type InspectVarsActions = {
|
|
|
|
type InspectVarsActions = {
|
|
|
|
|
|
|
|
setCurrentFocusNodeId: (nodeId: string | null) => void
|
|
|
|
getAllInspectVars: () => NodeWithVar[]
|
|
|
|
getAllInspectVars: () => NodeWithVar[]
|
|
|
|
clearInspectVars: () => void
|
|
|
|
clearInspectVars: () => void
|
|
|
|
setNodeInspectVars: (nodeId: string, payload: NodeWithVar) => void
|
|
|
|
setNodeInspectVars: (nodeId: string, payload: NodeWithVar) => void
|
|
|
|
@ -20,24 +21,29 @@ type InspectVarsActions = {
|
|
|
|
getInspectVar: (nodeId: string, selector: ValueSelector) => any
|
|
|
|
getInspectVar: (nodeId: string, selector: ValueSelector) => any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type CurrentVarsSliceShape = InspectVarsState & InspectVarsActions
|
|
|
|
export type InspectVarsSliceShape = InspectVarsState & InspectVarsActions
|
|
|
|
|
|
|
|
|
|
|
|
export const createInspectVarsSlice: StateCreator<CurrentVarsSliceShape> = (set, get) => {
|
|
|
|
export const createInspectVarsSlice: StateCreator<InspectVarsSliceShape> = (set, get) => {
|
|
|
|
return ({
|
|
|
|
return ({
|
|
|
|
currentFocusNodeId: null,
|
|
|
|
currentFocusNodeId: null,
|
|
|
|
nodes: [],
|
|
|
|
nodesWithInspectVars: [],
|
|
|
|
conversationVars: [],
|
|
|
|
conversationVars: [],
|
|
|
|
|
|
|
|
setCurrentFocusNodeId: (nodeId) => {
|
|
|
|
|
|
|
|
set(() => ({
|
|
|
|
|
|
|
|
currentFocusNodeId: nodeId,
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
},
|
|
|
|
getAllInspectVars: () => {
|
|
|
|
getAllInspectVars: () => {
|
|
|
|
return get().nodes
|
|
|
|
return get().nodesWithInspectVars
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clearInspectVars: () => {
|
|
|
|
clearInspectVars: () => {
|
|
|
|
set(() => ({
|
|
|
|
set(() => ({
|
|
|
|
nodes: [],
|
|
|
|
nodesWithInspectVars: [],
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setNodeInspectVars: (nodeId, payload) => {
|
|
|
|
setNodeInspectVars: (nodeId, payload) => {
|
|
|
|
set((state) => {
|
|
|
|
set((state) => {
|
|
|
|
const prevNodes = state.nodes
|
|
|
|
const prevNodes = state.nodesWithInspectVars
|
|
|
|
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)
|
|
|
|
@ -47,27 +53,27 @@ export const createInspectVarsSlice: StateCreator<CurrentVarsSliceShape> = (set,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
nodes,
|
|
|
|
nodesWithInspectVars: nodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clearNodeInspectVars: (nodeId) => {
|
|
|
|
clearNodeInspectVars: (nodeId) => {
|
|
|
|
set(produce((state: CurrentVarsSliceShape) => {
|
|
|
|
set(produce((state: InspectVarsSliceShape) => {
|
|
|
|
const nodes = state.nodes.filter(node => node.nodeId !== nodeId)
|
|
|
|
const nodes = state.nodesWithInspectVars.filter(node => node.nodeId !== nodeId)
|
|
|
|
state.nodes = nodes
|
|
|
|
state.nodesWithInspectVars = nodes
|
|
|
|
},
|
|
|
|
},
|
|
|
|
))
|
|
|
|
))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getNodeInspectVars: (nodeId) => {
|
|
|
|
getNodeInspectVars: (nodeId) => {
|
|
|
|
const nodes = get().nodes
|
|
|
|
const nodes = get().nodesWithInspectVars
|
|
|
|
return nodes.find(node => node.nodeId === nodeId)
|
|
|
|
return nodes.find(node => node.nodeId === nodeId)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hasNodeInspectVars: (nodeId) => {
|
|
|
|
hasNodeInspectVars: (nodeId) => {
|
|
|
|
return !!get().getNodeInspectVars(nodeId)
|
|
|
|
return !!get().getNodeInspectVars(nodeId)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setInspectVar: (nodeId, selector, value) => {
|
|
|
|
setInspectVar: (nodeId, selector, value) => {
|
|
|
|
set(produce((state: CurrentVarsSliceShape) => {
|
|
|
|
set(produce((state: InspectVarsSliceShape) => {
|
|
|
|
const nodes = state.nodes.map((node) => {
|
|
|
|
const nodes = state.nodesWithInspectVars.map((node) => {
|
|
|
|
if (node.nodeId === nodeId) {
|
|
|
|
if (node.nodeId === nodeId) {
|
|
|
|
return produce(node, (draft) => {
|
|
|
|
return produce(node, (draft) => {
|
|
|
|
const needChangeVarIndex = draft.vars.findIndex((varItem) => {
|
|
|
|
const needChangeVarIndex = draft.vars.findIndex((varItem) => {
|
|
|
|
@ -79,7 +85,7 @@ export const createInspectVarsSlice: StateCreator<CurrentVarsSliceShape> = (set,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
return node
|
|
|
|
})
|
|
|
|
})
|
|
|
|
state.nodes = nodes
|
|
|
|
state.nodesWithInspectVars = nodes
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getInspectVar(nodeId, key) {
|
|
|
|
getInspectVar(nodeId, key) {
|