feat: tool valid

This commit is contained in:
Joel 2024-03-19 16:14:15 +08:00
parent dbaf54c93d
commit 8d3158a6d5
4 changed files with 47 additions and 26 deletions

View File

@ -49,7 +49,7 @@ type Params<T> = {
id: string
data: CommonNodeType<T>
defaultRunInputData: Record<string, any>
// beforeRunCheckValid?: () => CheckValidRes // has checked before run button clicked
moreDataForCheckValid?: any
}
const varTypeToInputVarType = (type: VarType, {
@ -77,7 +77,7 @@ const useOneStepRun = <T>({
id,
data,
defaultRunInputData,
// beforeRunCheckValid = () => ({ isValid: true }),
moreDataForCheckValid,
}: Params<T>) => {
const { t } = useTranslation()
const { getBeforeNodesInSameBranch } = useWorkflow()
@ -124,7 +124,7 @@ const useOneStepRun = <T>({
}
if (data._isSingleRun) {
const { isValid, errorMessage } = checkValid(data, t)
const { isValid, errorMessage } = checkValid(data, t, moreDataForCheckValid)
setCanShowSingleRun(isValid)
if (!isValid) {
handleNodeDataUpdate({

View File

@ -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<ToolNodeType> = {
defaultValue: {
tool_parameters: [],
@ -18,15 +20,30 @@ const nodeDefault: NodeDefault<ToolNodeType> = {
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,
}
},

View File

@ -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<ToolNodeType>({
id,
data: inputs,
defaultRunInputData: {},
})
const [inputVarValues, doSetInputVarValues] = useState<Record<string, any>>({})
const setInputVarValues = (value: Record<string, any>) => {
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<ToolNodeType>({
id,
data: inputs,
defaultRunInputData: {},
moreDataForCheckValid: {
toolInputsSchema: singleRunForms[0].inputs,
toolSettingSchema,
language,
},
})
return {
inputs,

View File

@ -172,7 +172,7 @@ export type NodeDefault<T> = {
defaultValue: Partial<T>
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