From 08d3cb191240af3c8fd3939aa88ce69017069f23 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 30 Aug 2024 17:25:32 +0800 Subject: [PATCH] fix: filter file and file sub variable --- .../nodes/_base/components/variable/utils.ts | 59 ++++++++++++++----- .../variable/var-reference-vars.tsx | 2 +- .../workflow/nodes/if-else/default.ts | 1 + 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/web/app/components/workflow/nodes/_base/components/variable/utils.ts b/web/app/components/workflow/nodes/_base/components/variable/utils.ts index f86d2d529b..eaa35d9951 100644 --- a/web/app/components/workflow/nodes/_base/components/variable/utils.ts +++ b/web/app/components/workflow/nodes/_base/components/variable/utils.ts @@ -14,6 +14,7 @@ import type { ToolNodeType } from '../../../tool/types' import type { ParameterExtractorNodeType } from '../../../parameter-extractor/types' import type { IterationNodeType } from '../../../iteration/types' import type { ListFilterNodeType } from '../../../list-filter/types' +import { OUTPUT_FILE_SUB_VARIABLES } from '../../../if-else/default' import { BlockEnum, InputVarType, VarType } from '@/app/components/workflow/types' import type { StartNodeType } from '@/app/components/workflow/nodes/start/types' import type { ConversationVariable, EnvironmentVariable, Node, NodeOutPutVar, ValueSelector, Var } from '@/app/components/workflow/types' @@ -76,8 +77,6 @@ const formatItem = ( ): NodeOutPutVar => { const { id, data } = item - console.log(data.type) - const res: NodeOutPutVar = { nodeId: id, title: data.title, @@ -303,27 +302,57 @@ const formatItem = ( } const selector = [id] + res.vars = res.vars.filter((v) => { - const { children } = v - if (!children) { - return filterVar(v, (() => { - const variableArr = v.variable.split('.') - const [first, ..._other] = variableArr - if (first === 'sys' || first === 'env' || first === 'conversation') - return variableArr + const isCurrentMatched = filterVar(v, (() => { + const variableArr = v.variable.split('.') + const [first, ..._other] = variableArr + if (first === 'sys' || first === 'env' || first === 'conversation') + return variableArr - return [...selector, ...variableArr] - })()) - } + return [...selector, ...variableArr] + })()) + if (isCurrentMatched) + return true - const obj = findExceptVarInObject(v, filterVar, selector) + const isFile = v.type === VarType.file + const children = (() => { + if (isFile) { + return OUTPUT_FILE_SUB_VARIABLES.map((key) => { + return { + variable: key, + type: key === 'size' ? VarType.number : VarType.string, + } + }) + } + return v.children + })() + if (!children) + return false + + const obj = findExceptVarInObject(isFile ? { ...v, children } : v, filterVar, selector) return obj?.children && obj?.children.length > 0 }).map((v) => { - const { children } = v + const isFile = v.type === VarType.file + + const { children } = (() => { + if (isFile) { + return { + children: OUTPUT_FILE_SUB_VARIABLES.map((key) => { + return { + variable: key, + type: key === 'size' ? VarType.number : VarType.string, + } + }), + } + } + return v + })() + if (!children) return v - return findExceptVarInObject(v, filterVar, selector) + return findExceptVarInObject(isFile ? { ...v, children } : v, filterVar, selector) }) return res diff --git a/web/app/components/workflow/nodes/_base/components/variable/var-reference-vars.tsx b/web/app/components/workflow/nodes/_base/components/variable/var-reference-vars.tsx index a105579947..fdf1037c6d 100644 --- a/web/app/components/workflow/nodes/_base/components/variable/var-reference-vars.tsx +++ b/web/app/components/workflow/nodes/_base/components/variable/var-reference-vars.tsx @@ -46,8 +46,8 @@ const Item: FC = ({ onHovering, itemWidth, }) => { - const isObj = (itemData.type === VarType.object && itemData.children && itemData.children.length > 0) || itemData.type === VarType.file const isFile = itemData.type === VarType.file + const isObj = ([VarType.object, VarType.file].includes(itemData.type) && itemData.children && itemData.children.length > 0) const isSys = itemData.variable.startsWith('sys.') const isEnv = itemData.variable.startsWith('env.') const isChatVar = itemData.variable.startsWith('conversation.') diff --git a/web/app/components/workflow/nodes/if-else/default.ts b/web/app/components/workflow/nodes/if-else/default.ts index ec3ff83075..add35f272f 100644 --- a/web/app/components/workflow/nodes/if-else/default.ts +++ b/web/app/components/workflow/nodes/if-else/default.ts @@ -76,3 +76,4 @@ export const TRANSFER_METHOD = [ ] export const SUB_VARIABLES = ['type', 'size', 'name', 'url', 'extension', 'mime_type', 'transfer_method', 'url'] +export const OUTPUT_FILE_SUB_VARIABLES = SUB_VARIABLES.filter(key => key !== 'transfer_method')