From e0d7facdddad64befe5c9b58127d97def073e774 Mon Sep 17 00:00:00 2001 From: JzoNg Date: Wed, 11 Jun 2025 15:54:01 +0800 Subject: [PATCH] data formatting of tool data --- .../components/tools/utils/to-form-schema.ts | 8 ++- .../_base/components/form-input-item.tsx | 14 ++++- .../components/workflow/nodes/tool/default.ts | 1 + .../components/workflow/nodes/tool/types.ts | 1 + .../workflow/nodes/tool/use-config.ts | 54 ++++++------------- .../workflow/utils/workflow-init.ts | 4 +- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/web/app/components/tools/utils/to-form-schema.ts b/web/app/components/tools/utils/to-form-schema.ts index 0ea451759d..d476e687c1 100644 --- a/web/app/components/tools/utils/to-form-schema.ts +++ b/web/app/components/tools/utils/to-form-schema.ts @@ -110,16 +110,22 @@ export const getConfiguredValue = (value: Record, formSchemas: { va if (formSchema.type === 'boolean') { if (typeof value === 'string') - newValues[formSchema.variable].value = value === 'true' + newValues[formSchema.variable].value = value === 'true' || value === '1' if (typeof value === 'boolean') newValues[formSchema.variable].value = value + + if (typeof value === 'number') + newValues[formSchema.variable].value = value === 1 } if (formSchema.type === 'number-input') { if (typeof value === 'string' && value !== '') newValues[formSchema.variable].value = Number.parseFloat(value) } + + if (formSchema.type === 'app-selector' || formSchema.type === 'model-selector') + newValues[formSchema.variable] = value } }) return newValues diff --git a/web/app/components/workflow/nodes/_base/components/form-input-item.tsx b/web/app/components/workflow/nodes/_base/components/form-input-item.tsx index 986093c19c..d5444872af 100644 --- a/web/app/components/workflow/nodes/_base/components/form-input-item.tsx +++ b/web/app/components/workflow/nodes/_base/components/form-input-item.tsx @@ -157,6 +157,16 @@ const FormInputItem: FC = ({ }) } + const handleAppOrModelSelect = (newValue: any) => { + onChange({ + ...value, + [variable]: { + ...varInput, + ...newValue, + }, + }) + } + const handleVariableSelectorChange = (newValue: ValueSelector | string, variable: string) => { onChange({ ...value, @@ -248,7 +258,7 @@ const FormInputItem: FC = ({ disabled={readOnly} scope={scope || 'all'} value={varInput?.value as any} - onSelect={handleValueChange} + onSelect={handleAppOrModelSelect} /> )} {isModelSelector && isConstant && ( @@ -257,7 +267,7 @@ const FormInputItem: FC = ({ isAdvancedMode isInWorkflow value={varInput?.value as any} - setModel={handleValueChange} + setModel={handleAppOrModelSelect} readonly={readOnly} scope={scope} /> diff --git a/web/app/components/workflow/nodes/tool/default.ts b/web/app/components/workflow/nodes/tool/default.ts index f245929684..a73274b713 100644 --- a/web/app/components/workflow/nodes/tool/default.ts +++ b/web/app/components/workflow/nodes/tool/default.ts @@ -10,6 +10,7 @@ const nodeDefault: NodeDefault = { defaultValue: { tool_parameters: {}, tool_configurations: {}, + version: '2', }, getAvailablePrevNodes(isChatMode: boolean) { const nodes = isChatMode diff --git a/web/app/components/workflow/nodes/tool/types.ts b/web/app/components/workflow/nodes/tool/types.ts index 4b78c53ab2..4584645a1e 100644 --- a/web/app/components/workflow/nodes/tool/types.ts +++ b/web/app/components/workflow/nodes/tool/types.ts @@ -22,4 +22,5 @@ export type ToolNodeType = CommonNodeType & { tool_configurations: Record output_schema: Record paramSchemas?: Record[] + version?: string } diff --git a/web/app/components/workflow/nodes/tool/use-config.ts b/web/app/components/workflow/nodes/tool/use-config.ts index 8b95237f64..b04193fe10 100644 --- a/web/app/components/workflow/nodes/tool/use-config.ts +++ b/web/app/components/workflow/nodes/tool/use-config.ts @@ -14,8 +14,7 @@ import { } from '@/app/components/tools/utils/to-form-schema' import Toast from '@/app/components/base/toast' import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form' -import { VarType as VarVarType } from '@/app/components/workflow/types' -import type { InputVar, ValueSelector, Var } from '@/app/components/workflow/types' +import type { InputVar, ValueSelector } from '@/app/components/workflow/types' import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run' import { useFetchToolsData, @@ -42,7 +41,7 @@ const useConfig = (id: string, payload: ToolNodeType) => { const workflowTools = useStore(s => s.workflowTools) const mcpTools = useStore(s => s.mcpTools) - const currentTools = (() => { + const currentTools = useMemo(() => { switch (provider_type) { case CollectionType.builtIn: return buildInTools @@ -55,7 +54,7 @@ const useConfig = (id: string, payload: ToolNodeType) => { default: return [] } - })() + }, [buildInTools, customTools, mcpTools, provider_type, workflowTools]) const currCollection = currentTools.find(item => canFindTool(item.id, provider_id)) // Auth @@ -99,10 +98,10 @@ const useConfig = (id: string, payload: ToolNodeType) => { const value = newConfig[key] if (schema?.type === 'boolean') { if (typeof value === 'string') - newConfig[key] = Number.parseInt(value, 10) + newConfig[key] = value === 'true' || value === '1' - if (typeof value === 'boolean') - newConfig[key] = value ? 1 : 0 + if (typeof value === 'number') + newConfig[key] = value === 1 } if (schema?.type === 'number-input') { @@ -128,24 +127,20 @@ const useConfig = (id: string, payload: ToolNodeType) => { }) }, [inputs, setInputs]) + const formattingParameters = () => { + const inputsWithDefaultValue = produce(inputs, (draft) => { + if (!draft.tool_configurations || Object.keys(draft.tool_configurations).length === 0) + draft.tool_configurations = getConfiguredValue(tool_configurations, toolSettingSchema) + if (!draft.tool_parameters || Object.keys(draft.tool_parameters).length === 0) + draft.tool_parameters = getConfiguredValue(tool_parameters, toolInputVarSchema) + }) + return inputsWithDefaultValue + } + useEffect(() => { if (!currTool) return - const inputsWithDefaultValue = produce(inputs, (draft) => { - if (!draft.tool_configurations || Object.keys(draft.tool_configurations).length === 0) { - draft.tool_configurations = getConfiguredValue(tool_configurations, toolSettingSchema) - } - else { - // TODO - } - - if (!draft.tool_parameters || Object.keys(draft.tool_configurations).length === 0) { - draft.tool_parameters = getConfiguredValue(tool_parameters, toolInputVarSchema) - } - else { - // TODO: boolean & model & app formatting BOTH configuration & parameters - } - }) + const inputsWithDefaultValue = formattingParameters() setInputs(inputsWithDefaultValue) // eslint-disable-next-line react-hooks/exhaustive-deps }, [currTool]) @@ -158,19 +153,6 @@ const useConfig = (id: string, payload: ToolNodeType) => { }) }, [inputs, setInputs]) - const [currVarIndex, setCurrVarIndex] = useState(-1) - const currVarType = toolInputVarSchema[currVarIndex]?._type - const handleOnVarOpen = useCallback((index: number) => { - setCurrVarIndex(index) - }, []) - - const filterVar = useCallback((varPayload: Var) => { - if (currVarType) - return varPayload.type === currVarType - - return varPayload.type !== VarVarType.arrayFile - }, [currVarType]) - const isLoading = currTool && (isBuiltIn ? !currCollection : false) // single run @@ -314,8 +296,6 @@ const useConfig = (id: string, payload: ToolNodeType) => { setToolSettingValue, toolInputVarSchema, setInputVar, - handleOnVarOpen, - filterVar, currCollection, isShowAuthBtn, showSetAuth, diff --git a/web/app/components/workflow/utils/workflow-init.ts b/web/app/components/workflow/utils/workflow-init.ts index dd952ad98f..8610bbe9d1 100644 --- a/web/app/components/workflow/utils/workflow-init.ts +++ b/web/app/components/workflow/utils/workflow-init.ts @@ -286,7 +286,9 @@ export const initialNodes = (originNodes: Node[], originEdges: Edge[]) => { } } - if (node.data.type === BlockEnum.Tool) { + if (node.data.type === BlockEnum.Tool && !(node as Node).data.version) { + (node as Node).data.version = '2' + const toolConfigurations = (node as Node).data.tool_configurations if (toolConfigurations && Object.keys(toolConfigurations).length > 0) { const newValues = { ...toolConfigurations }