feat: support filter obj select type

This commit is contained in:
Joel 2024-03-15 14:01:15 +08:00
parent 5fbf8ee6c6
commit 5adf94bd7d
7 changed files with 60 additions and 21 deletions

View File

@ -1,7 +1,7 @@
import type { CodeNodeType } from '../../../code/types' import type { CodeNodeType } from '../../../code/types'
import { BlockEnum, InputVarType, VarType } from '@/app/components/workflow/types' import { BlockEnum, InputVarType, VarType } from '@/app/components/workflow/types'
import type { StartNodeType } from '@/app/components/workflow/nodes/start/types' import type { StartNodeType } from '@/app/components/workflow/nodes/start/types'
import type { NodeOutPutVar } from '@/app/components/workflow/types' import type { NodeOutPutVar, Var } from '@/app/components/workflow/types'
import type { VariableAssignerNodeType } from '@/app/components/workflow/nodes/variable-assigner/types' import type { VariableAssignerNodeType } from '@/app/components/workflow/nodes/variable-assigner/types'
import { import {
CHAT_QUESTION_CLASSIFIER_OUTPUT_STRUCT, CHAT_QUESTION_CLASSIFIER_OUTPUT_STRUCT,
@ -21,7 +21,24 @@ const inputVarTypeToVarType = (type: InputVarType): VarType => {
return VarType.string return VarType.string
} }
const formatItem = (item: any, isChatMode: boolean, varType?: VarType): NodeOutPutVar => { const findExceptVarInObject = (obj: any, filterVar: (payload: Var) => boolean): Var => {
const { children } = obj
const res: Var = {
variable: obj.variable,
type: VarType.object,
children: children.filter((item: Var) => {
const { children } = item
if (!children)
return filterVar(item)
const obj = findExceptVarInObject(item, filterVar)
return obj.children && obj.children?.length > 0
}),
}
return res
}
const formatItem = (item: any, isChatMode: boolean, filterVar: (payload: any) => boolean): NodeOutPutVar => {
const { id, data } = item const { id, data } = item
const res: NodeOutPutVar = { const res: NodeOutPutVar = {
nodeId: id, nodeId: id,
@ -109,15 +126,28 @@ const formatItem = (item: any, isChatMode: boolean, varType?: VarType): NodeOutP
break break
} }
} }
if (varType)
res.vars = res.vars.filter(v => v.type === varType) res.vars = res.vars.filter((v) => {
const { children } = v
if (!children)
return filterVar(v)
const obj = findExceptVarInObject(v, filterVar)
return obj?.children && obj?.children.length > 0
}).map((v) => {
const { children } = v
if (!children)
return v
return findExceptVarInObject(v, filterVar)
})
return res return res
} }
export const toNodeOutputVars = (nodes: any[], isChatMode: boolean, varType?: VarType): NodeOutPutVar[] => { export const toNodeOutputVars = (nodes: any[], isChatMode: boolean, filterVar = (_payload: Var) => true): NodeOutPutVar[] => {
const res = nodes const res = nodes
.filter(node => SUPPORT_OUTPUT_VARS_NODE.includes(node.data.type)) .filter(node => SUPPORT_OUTPUT_VARS_NODE.includes(node.data.type))
.map(node => formatItem(node, isChatMode, varType)) .map(node => formatItem(node, isChatMode, filterVar))
.filter(item => item.vars.length > 0) .filter(item => item.vars.length > 0)
return res return res
} }

View File

@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next'
import produce from 'immer' import produce from 'immer'
import RemoveButton from '../remove-button' import RemoveButton from '../remove-button'
import VarReferencePicker from './var-reference-picker' import VarReferencePicker from './var-reference-picker'
import { type ValueSelector, type VarType, type Variable } from '@/app/components/workflow/types' import type { ValueSelector, Var, Variable } from '@/app/components/workflow/types'
import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types' import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
type Props = { type Props = {
@ -15,7 +15,7 @@ type Props = {
onChange: (list: Variable[]) => void onChange: (list: Variable[]) => void
isSupportConstantValue?: boolean isSupportConstantValue?: boolean
onlyLeafNodeVar?: boolean onlyLeafNodeVar?: boolean
onlyVarType?: VarType filterVar?: (payload: Var) => boolean
} }
const VarList: FC<Props> = ({ const VarList: FC<Props> = ({
@ -25,7 +25,7 @@ const VarList: FC<Props> = ({
onChange, onChange,
isSupportConstantValue, isSupportConstantValue,
onlyLeafNodeVar, onlyLeafNodeVar,
onlyVarType, filterVar,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
@ -90,7 +90,7 @@ const VarList: FC<Props> = ({
onChange={handleVarReferenceChange(index)} onChange={handleVarReferenceChange(index)}
defaultVarKindType={item.variable_type} defaultVarKindType={item.variable_type}
onlyLeafNodeVar={onlyLeafNodeVar} onlyLeafNodeVar={onlyLeafNodeVar}
onlyVarType={onlyVarType} filterVar={filterVar}
/> />
<RemoveButton <RemoveButton
className='!p-2 !bg-gray-100 hover:!bg-gray-200' className='!p-2 !bg-gray-100 hover:!bg-gray-200'

View File

@ -6,7 +6,7 @@ import cn from 'classnames'
import { isArray } from 'lodash-es' import { isArray } from 'lodash-es'
import VarReferencePopup from './var-reference-popup' import VarReferencePopup from './var-reference-popup'
import { toNodeOutputVars } from './utils' import { toNodeOutputVars } from './utils'
import type { ValueSelector } from '@/app/components/workflow/types' import type { ValueSelector, Var } from '@/app/components/workflow/types'
import { VarType } from '@/app/components/workflow/types' import { VarType } from '@/app/components/workflow/types'
import { VarBlockIcon } from '@/app/components/workflow/block-icon' import { VarBlockIcon } from '@/app/components/workflow/block-icon'
import { Line3 } from '@/app/components/base/icons/src/public/common' import { Line3 } from '@/app/components/base/icons/src/public/common'
@ -35,7 +35,7 @@ type Props = {
isSupportConstantValue?: boolean isSupportConstantValue?: boolean
defaultVarKindType?: VarKindType defaultVarKindType?: VarKindType
onlyLeafNodeVar?: boolean onlyLeafNodeVar?: boolean
onlyVarType?: VarType filterVar?: (payload: Var) => boolean
} }
export const getNodeInfoById = (nodes: any, id: string) => { export const getNodeInfoById = (nodes: any, id: string) => {
@ -56,7 +56,7 @@ const VarReferencePicker: FC<Props> = ({
isSupportConstantValue, isSupportConstantValue,
defaultVarKindType = VarKindType.static, defaultVarKindType = VarKindType.static,
onlyLeafNodeVar, onlyLeafNodeVar,
onlyVarType, filterVar = () => true,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
@ -65,7 +65,7 @@ const VarReferencePicker: FC<Props> = ({
const isConstant = isSupportConstantValue && varKindType === VarKindType.static const isConstant = isSupportConstantValue && varKindType === VarKindType.static
const { getTreeLeafNodes, getBeforeNodesInSameBranch } = useWorkflow() const { getTreeLeafNodes, getBeforeNodesInSameBranch } = useWorkflow()
const availableNodes = onlyLeafNodeVar ? getTreeLeafNodes(nodeId) : getBeforeNodesInSameBranch(nodeId) const availableNodes = onlyLeafNodeVar ? getTreeLeafNodes(nodeId) : getBeforeNodesInSameBranch(nodeId)
const outputVars = toNodeOutputVars(availableNodes, isChatMode, onlyVarType) const outputVars = toNodeOutputVars(availableNodes, isChatMode, filterVar)
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const hasValue = !isConstant && value.length > 0 const hasValue = !isConstant && value.length > 0
const outputVarNodeId = hasValue ? value[0] : '' const outputVarNodeId = hasValue ? value[0] : ''

View File

@ -86,7 +86,7 @@ const ObjectChildren: FC<ObjectChildrenProps> = ({
return ( return (
<div className='absolute top-[-2px] bg-white rounded-lg border border-gray-200 shadow-lg space-y-1' style={{ <div className='absolute top-[-2px] bg-white rounded-lg border border-gray-200 shadow-lg space-y-1' style={{
right: itemWidth ? itemWidth - 4 : 215, right: itemWidth ? itemWidth - 10 : 215,
minWidth: 252, minWidth: 252,
}}> }}>
<div className='flex items-center h-[22px] px-3 text-xs font-normal text-gray-700'><span className='text-gray-500'>{title}.</span>{currObjPath.join('.')}</div> <div className='flex items-center h-[22px] px-3 text-xs font-normal text-gray-700'><span className='text-gray-500'>{title}.</span>{currObjPath.join('.')}</div>

View File

@ -5,7 +5,7 @@ import React, { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import RemoveButton from '../../../_base/components/remove-button' import RemoveButton from '../../../_base/components/remove-button'
import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker' import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
import type { ValueSelector, VarType } from '@/app/components/workflow/types' import type { ValueSelector, Var } from '@/app/components/workflow/types'
type Props = { type Props = {
readonly: boolean readonly: boolean
@ -13,7 +13,7 @@ type Props = {
list: ValueSelector[] list: ValueSelector[]
onChange: (list: ValueSelector[]) => void onChange: (list: ValueSelector[]) => void
onlyLeafNodeVar?: boolean onlyLeafNodeVar?: boolean
onlyVarType?: VarType filterVar?: (payload: Var) => boolean
} }
const VarList: FC<Props> = ({ const VarList: FC<Props> = ({
@ -22,7 +22,7 @@ const VarList: FC<Props> = ({
list, list,
onChange, onChange,
onlyLeafNodeVar, onlyLeafNodeVar,
onlyVarType, filterVar,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const handleVarReferenceChange = useCallback((index: number) => { const handleVarReferenceChange = useCallback((index: number) => {
@ -63,7 +63,7 @@ const VarList: FC<Props> = ({
value={item} value={item}
onChange={handleVarReferenceChange(index)} onChange={handleVarReferenceChange(index)}
onlyLeafNodeVar={onlyLeafNodeVar} onlyLeafNodeVar={onlyLeafNodeVar}
onlyVarType={onlyVarType} filterVar={filterVar}
width={350} width={350}
/> />
<RemoveButton <RemoveButton

View File

@ -24,6 +24,7 @@ const Panel: FC<NodePanelProps<VariableAssignerNodeType>> = ({
handleOutputTypeChange, handleOutputTypeChange,
handleVarListChange, handleVarListChange,
handleAddVariable, handleAddVariable,
filterVar,
} = useConfig(id, data) } = useConfig(id, data)
const typeOptions = [ const typeOptions = [
@ -66,7 +67,7 @@ const Panel: FC<NodePanelProps<VariableAssignerNodeType>> = ({
list={inputs.variables} list={inputs.variables}
onChange={handleVarListChange} onChange={handleVarListChange}
onlyLeafNodeVar onlyLeafNodeVar
onlyVarType={inputs.output_type} filterVar={filterVar}
/> />
</Field> </Field>
</div> </div>

View File

@ -3,13 +3,14 @@ import produce from 'immer'
import useVarList from './components/var-list/use-var-list' import useVarList from './components/var-list/use-var-list'
import type { VariableAssignerNodeType } from './types' import type { VariableAssignerNodeType } from './types'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import type { Var, VarType } from '@/app/components/workflow/types'
const useConfig = (id: string, payload: VariableAssignerNodeType) => { const useConfig = (id: string, payload: VariableAssignerNodeType) => {
const { inputs, setInputs } = useNodeCrud<VariableAssignerNodeType>(id, payload) const { inputs, setInputs } = useNodeCrud<VariableAssignerNodeType>(id, payload)
const handleOutputTypeChange = useCallback((outputType: string) => { const handleOutputTypeChange = useCallback((outputType: string) => {
const newInputs = produce(inputs, (draft: VariableAssignerNodeType) => { const newInputs = produce(inputs, (draft: VariableAssignerNodeType) => {
draft.output_type = outputType draft.output_type = outputType as VarType
}) })
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs]) }, [inputs, setInputs])
@ -19,11 +20,18 @@ const useConfig = (id: string, payload: VariableAssignerNodeType) => {
inputs, inputs,
setInputs, setInputs,
}) })
const filterVar = useCallback((varPayload: Var) => {
if (varPayload.type !== inputs.output_type)
return false
return true
}, [inputs])
return { return {
inputs, inputs,
handleOutputTypeChange, handleOutputTypeChange,
handleVarListChange, handleVarListChange,
handleAddVariable, handleAddVariable,
filterVar,
} }
} }