From 8d3158a6d57e81db225e1d06b61d7a28ff99f5dc Mon Sep 17 00:00:00 2001 From: Joel Date: Tue, 19 Mar 2024 16:14:15 +0800 Subject: [PATCH] feat: tool valid --- .../nodes/_base/hooks/use-one-step-run.ts | 6 ++-- .../components/workflow/nodes/tool/default.ts | 29 +++++++++++---- .../workflow/nodes/tool/use-config.ts | 36 ++++++++++--------- web/app/components/workflow/types.ts | 2 +- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts b/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts index 5163df7e4d..2bcfc3eab9 100644 --- a/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts +++ b/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts @@ -49,7 +49,7 @@ type Params = { id: string data: CommonNodeType defaultRunInputData: Record - // beforeRunCheckValid?: () => CheckValidRes // has checked before run button clicked + moreDataForCheckValid?: any } const varTypeToInputVarType = (type: VarType, { @@ -77,7 +77,7 @@ const useOneStepRun = ({ id, data, defaultRunInputData, - // beforeRunCheckValid = () => ({ isValid: true }), + moreDataForCheckValid, }: Params) => { const { t } = useTranslation() const { getBeforeNodesInSameBranch } = useWorkflow() @@ -124,7 +124,7 @@ const useOneStepRun = ({ } if (data._isSingleRun) { - const { isValid, errorMessage } = checkValid(data, t) + const { isValid, errorMessage } = checkValid(data, t, moreDataForCheckValid) setCanShowSingleRun(isValid) if (!isValid) { handleNodeDataUpdate({ diff --git a/web/app/components/workflow/nodes/tool/default.ts b/web/app/components/workflow/nodes/tool/default.ts index 24d1dc29a7..7e1e369957 100644 --- a/web/app/components/workflow/nodes/tool/default.ts +++ b/web/app/components/workflow/nodes/tool/default.ts @@ -3,6 +3,8 @@ import type { NodeDefault } from '../../types' import type { ToolNodeType } from './types' import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants' +const i18nPrefix = 'workflow.errorMsg' + const nodeDefault: NodeDefault = { defaultValue: { tool_parameters: [], @@ -18,15 +20,30 @@ const nodeDefault: NodeDefault = { const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS return nodes }, - checkValid(payload: ToolNodeType) { - let isValid = true + checkValid(payload: ToolNodeType, t: any, moreDataForCheckValid: any) { + const { toolInputsSchema, toolSettingSchema, language } = moreDataForCheckValid let errorMessages = '' - if (payload.type) { - isValid = true - errorMessages = '' + + toolInputsSchema.filter((field: any) => { + return field.required + }).forEach((field: any) => { + const value = payload.tool_parameters.find((item: any) => item.variable === field.variable)?.value + if (!errorMessages && (value === undefined || value === null || value === '')) + errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label[language] }) + }) + + if (!errorMessages) { + toolSettingSchema.filter((field: any) => { + return field.required + }).forEach((field: any) => { + const value = payload.tool_configurations[field.variable] + if (!errorMessages && (value === undefined || value === null || value === '')) + errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label[language] }) + }) } + return { - isValid, + isValid: !errorMessages, errorMessage: errorMessages, } }, diff --git a/web/app/components/workflow/nodes/tool/use-config.ts b/web/app/components/workflow/nodes/tool/use-config.ts index 5355c8c7a3..db99bce409 100644 --- a/web/app/components/workflow/nodes/tool/use-config.ts +++ b/web/app/components/workflow/nodes/tool/use-config.ts @@ -17,6 +17,7 @@ import type { InputVar, Var } from '@/app/components/workflow/types' import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run' const useConfig = (id: string, payload: ToolNodeType) => { const { t } = useTranslation() + const language = useLanguage() const toolsMap = useStore(s => s.toolsMap) const setToolsMap = useStore(s => s.setToolsMap) @@ -137,26 +138,12 @@ const useConfig = (id: string, payload: ToolNodeType) => { }, [provider_name]) // single run - const { - isShowSingleRun, - hideSingleRun, - runningStatus, - setRunInputData, - handleRun, - handleStop, - runResult, - } = useOneStepRun({ - id, - data: inputs, - defaultRunInputData: {}, - }) - const [inputVarValues, doSetInputVarValues] = useState>({}) const setInputVarValues = (value: Record) => { doSetInputVarValues(value) + // eslint-disable-next-line @typescript-eslint/no-use-before-define setRunInputData(value) } - // fill single run form variable with constant value first time const inputVarValuesWithConstantValue = () => { const res = produce(inputVarValues, (draft) => { @@ -167,7 +154,6 @@ const useConfig = (id: string, payload: ToolNodeType) => { }) return res } - const singleRunForms = (() => { const formInputs: InputVar[] = [] toolInputVarSchema.forEach((item: any) => { @@ -185,6 +171,24 @@ const useConfig = (id: string, payload: ToolNodeType) => { }] return forms })() + const { + isShowSingleRun, + hideSingleRun, + runningStatus, + setRunInputData, + handleRun, + handleStop, + runResult, + } = useOneStepRun({ + id, + data: inputs, + defaultRunInputData: {}, + moreDataForCheckValid: { + toolInputsSchema: singleRunForms[0].inputs, + toolSettingSchema, + language, + }, + }) return { inputs, diff --git a/web/app/components/workflow/types.ts b/web/app/components/workflow/types.ts index eb48766993..29068cfd14 100644 --- a/web/app/components/workflow/types.ts +++ b/web/app/components/workflow/types.ts @@ -172,7 +172,7 @@ export type NodeDefault = { defaultValue: Partial getAvailablePrevNodes: (isChatMode: boolean) => BlockEnum[] getAvailableNextNodes: (isChatMode: boolean) => BlockEnum[] - checkValid: (payload: T, t: any) => { isValid: boolean; errorMessage?: string } + checkValid: (payload: T, t: any, moreDataForCheckValid?: any) => { isValid: boolean; errorMessage?: string } } export type OnSelectBlock = (type: BlockEnum, toolDefaultValue?: ToolDefaultValue) => void