From c0ada940bdb4d633f29030a7b955c4fb784206d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=9E=E6=B3=95=E6=93=8D=E4=BD=9C?= Date: Tue, 23 Jul 2024 17:00:39 +0800 Subject: [PATCH] fix: tool params not work as expected when develop a tool (#6550) --- .../tools/utils/tool_parameter_converter.py | 2 +- .../components/variable/constant-field.tsx | 62 +++++++++++++++++++ .../variable/var-reference-picker.tsx | 21 +++---- .../nodes/tool/components/input-var-list.tsx | 26 +++++--- 4 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 web/app/components/workflow/nodes/_base/components/variable/constant-field.tsx diff --git a/api/core/tools/utils/tool_parameter_converter.py b/api/core/tools/utils/tool_parameter_converter.py index 0c4ec00ec6..6f88eeaa0a 100644 --- a/api/core/tools/utils/tool_parameter_converter.py +++ b/api/core/tools/utils/tool_parameter_converter.py @@ -53,7 +53,7 @@ class ToolParameterConverter: case ToolParameter.ToolParameterType.NUMBER: if isinstance(value, int) | isinstance(value, float): return value - elif isinstance(value, str): + elif isinstance(value, str) and value != '': if '.' in value: return float(value) else: diff --git a/web/app/components/workflow/nodes/_base/components/variable/constant-field.tsx b/web/app/components/workflow/nodes/_base/components/variable/constant-field.tsx new file mode 100644 index 0000000000..bd7d159906 --- /dev/null +++ b/web/app/components/workflow/nodes/_base/components/variable/constant-field.tsx @@ -0,0 +1,62 @@ +'use client' +import type { FC } from 'react' +import React, { useCallback } from 'react' +import type { CredentialFormSchema, CredentialFormSchemaNumberInput, CredentialFormSchemaSelect } from '@/app/components/header/account-setting/model-provider-page/declarations' +import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' +import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks' +import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types' +import type { Var } from '@/app/components/workflow/types' +import { SimpleSelect } from '@/app/components/base/select' + +type Props = { + schema: CredentialFormSchema + readonly: boolean + value: string + onChange: (value: string | number, varKindType: VarKindType, varInfo?: Var) => void +} + +const ConstantField: FC = ({ + schema, + readonly, + value, + onChange, +}) => { + const language = useLanguage() + const placeholder = (schema as CredentialFormSchemaSelect).placeholder + const handleStaticChange = useCallback((e: React.ChangeEvent) => { + const value = e.target.value === '' ? '' : parseFloat(e.target.value) + onChange(value, VarKindType.constant) + }, [onChange]) + const handleSelectChange = useCallback((value: string | number) => { + value = value === null ? '' : value + onChange(value as string, VarKindType.constant) + }, [onChange]) + + return ( + <> + {schema.type === FormTypeEnum.select && ( + ({ value: option.value, name: option.label[language] || option.label.en_US }))} + onSelect={item => handleSelectChange(item.value)} + placeholder={placeholder?.[language] || placeholder?.en_US} + /> + )} + {schema.type === FormTypeEnum.textNumber && ( + + )} + + ) +} +export default React.memo(ConstantField) diff --git a/web/app/components/workflow/nodes/_base/components/variable/var-reference-picker.tsx b/web/app/components/workflow/nodes/_base/components/variable/var-reference-picker.tsx index 1e041ba5b1..bd8d5db88f 100644 --- a/web/app/components/workflow/nodes/_base/components/variable/var-reference-picker.tsx +++ b/web/app/components/workflow/nodes/_base/components/variable/var-reference-picker.tsx @@ -10,8 +10,10 @@ import produce from 'immer' import { useStoreApi } from 'reactflow' import VarReferencePopup from './var-reference-popup' import { getNodeInfoById, isENV, isSystemVar } from './utils' +import ConstantField from './constant-field' import cn from '@/utils/classnames' import type { Node, NodeOutPutVar, ValueSelector, Var } from '@/app/components/workflow/types' +import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations' import { BlockEnum } from '@/app/components/workflow/types' import { VarBlockIcon } from '@/app/components/workflow/block-icon' import { Line3 } from '@/app/components/base/icons/src/public/common' @@ -47,6 +49,7 @@ type Props = { availableNodes?: Node[] availableVars?: NodeOutPutVar[] isAddBtnTrigger?: boolean + schema?: CredentialFormSchema } const VarReferencePicker: FC = ({ @@ -64,6 +67,7 @@ const VarReferencePicker: FC = ({ availableNodes: passedInAvailableNodes, availableVars, isAddBtnTrigger, + schema, }) => { const { t } = useTranslation() const store = useStoreApi() @@ -192,10 +196,6 @@ const VarReferencePicker: FC = ({ setOpen(false) }, [onChange, varKindType]) - const handleStaticChange = useCallback((e: React.ChangeEvent) => { - onChange(e.target.value as string, varKindType) - }, [onChange, varKindType]) - const handleClearVar = useCallback(() => { if (varKindType === VarKindType.constant) onChange('', varKindType) @@ -265,14 +265,11 @@ const VarReferencePicker: FC = ({ )} {isConstant ? ( - setIsFocus(true)} - onBlur={() => setIsFocus(false)} - readOnly={readonly} + void)} + schema={schema as CredentialFormSchema} + readonly={readonly} /> ) : ( diff --git a/web/app/components/workflow/nodes/tool/components/input-var-list.tsx b/web/app/components/workflow/nodes/tool/components/input-var-list.tsx index 3f8447a557..bfa4a542bd 100644 --- a/web/app/components/workflow/nodes/tool/components/input-var-list.tsx +++ b/web/app/components/workflow/nodes/tool/components/input-var-list.tsx @@ -48,6 +48,8 @@ const InputVarList: FC = ({ return 'Number' else if (type === FormTypeEnum.files) return 'Files' + else if (type === FormTypeEnum.select) + return 'Options' else return 'String' } @@ -114,17 +116,19 @@ const InputVarList: FC = ({ return (
{ - schema.map(({ - variable, - label, - type, - required, - tooltip, - }, index) => { + schema.map((schema, index) => { + const { + variable, + label, + type, + required, + tooltip, + } = schema const varInput = value[variable] const isNumber = type === FormTypeEnum.textNumber + const isSelect = type === FormTypeEnum.select const isFile = type === FormTypeEnum.files - const isString = type !== FormTypeEnum.textNumber && type !== FormTypeEnum.files + const isString = type !== FormTypeEnum.textNumber && type !== FormTypeEnum.files && type !== FormTypeEnum.select return (
@@ -145,7 +149,7 @@ const InputVarList: FC = ({ placeholderClassName='!leading-[21px]' /> )} - {isNumber && ( + {(isNumber || isSelect) && ( = ({ onOpen={handleOpen(index)} isSupportConstantValue={isSupportConstantValue} defaultVarKindType={varInput?.type} - filterVar={filterVar} + filterVar={isNumber ? filterVar : undefined} + availableVars={isSelect ? availableVars : undefined} + schema={schema} /> )} {isFile && (