feat: support detect env and sys var

This commit is contained in:
Joel 2025-05-07 16:24:14 +08:00
parent 0cd6e55bde
commit d0dd99bd46
2 changed files with 26 additions and 27 deletions

View File

@ -28,6 +28,7 @@ import { BlockEnum } from '@/app/components/workflow/types'
import {
useNodesSyncDraft,
} from '@/app/components/workflow/hooks'
import useInspectVarsCrud from '@/app/components/workflow/hooks/use-inspect-vars-crud'
const singleRunFormParamsHooks: Record<BlockEnum, any> = {
[BlockEnum.LLM]: useLLMSingleRunFormParams,
@ -101,6 +102,7 @@ type Params<T> = OneStepRunParams<T>
const useLastRun = <T>({
...oneStepRunParams
}: Params<T>) => {
const { conversationVars } = useInspectVarsCrud()
const blockType = oneStepRunParams.data.type
const { handleSyncWorkflowDraft } = useNodesSyncDraft()
const {
@ -167,41 +169,20 @@ const useLastRun = <T>({
const workflowStore = useWorkflowStore()
const {
getInspectVar,
hasSetInspectVar,
} = workflowStore.getState()
const getExistVarValuesInForms = (forms: FormProps[]) => {
if (!forms || forms.length === 0)
return []
const valuesArr = forms.map((form) => {
const values: Record<string, any> = {}
const values: Record<string, boolean> = {}
form.inputs.forEach(({ variable }) => {
// #nodeId.path1?.path2?...# => [nodeId, path1]
// TODO: conversation vars and envs
const selector = variable.slice(1, -1).split('.')
const [nodeId, varName] = selector.slice(0, 2)
const inspectVarValue = getInspectVar(nodeId, varName)
if (inspectVarValue !== undefined) {
const subPathArr = selector.slice(2)
if (subPathArr.length > 0) {
let current = inspectVarValue.value
let invalid = false
subPathArr.forEach((subPath) => {
if (invalid)
return
if (current && typeof current === 'object' && subPath in current) {
current = current[subPath]
return
}
invalid = true
})
values[variable] = current
}
else {
values[variable] = inspectVarValue
}
}
const inspectVarValue = hasSetInspectVar(nodeId, varName, conversationVars) // also detect system var , env and conversation var
if (inspectVarValue)
values[variable] = true
})
return values
})

View File

@ -1,8 +1,9 @@
import type { StateCreator } from 'zustand'
import produce from 'immer'
import type { NodeWithVar, VarInInspect } from '@/types/workflow'
import type { ValueSelector } from '../../../types'
import { BlockEnum, type ValueSelector } from '../../../types'
import type { Node } from '@/app/components/workflow/types'
import { isConversationVar, isENV, isSystemVar } from '../../../nodes/_base/components/variable/utils'
type InspectVarsState = {
currentFocusNodeId: string | null
@ -25,6 +26,7 @@ type InspectVarsActions = {
renameInspectVarName: (nodeId: string, varId: string, selector: ValueSelector) => void
deleteInspectVar: (nodeId: string, varId: string) => void
getInspectVar: (nodeId: string, name: string) => any
hasSetInspectVar: (nodeId: string, name: string, conversationVars: VarInInspect[]) => boolean
isInspectVarEdited: (nodeId: string, name: string) => boolean
}
@ -172,6 +174,22 @@ export const createInspectVarsSlice: StateCreator<InspectVarsSliceShape> = (set,
})?.value
return variable
},
hasSetInspectVar: (nodeId, name, conversationVars: VarInInspect[]) => {
const isEnv = isENV([nodeId])
if (isEnv) // always have value
return true
const isSys = isSystemVar([nodeId])
if (isSys) {
const isStartNodeRun = get().nodesWithInspectVars.some((node) => {
return node.nodeType === BlockEnum.Start
})
return isStartNodeRun
}
const isChatVar = isConversationVar([nodeId])
if (isChatVar)
return conversationVars.some(varItem => varItem.selector?.[1] === name)
return get().getInspectVar(nodeId, name) !== undefined
},
isInspectVarEdited: (nodeId, name) => {
const inspectVar = get().getInspectVar(nodeId, name)
if (!inspectVar)