data formatting of tool data

This commit is contained in:
JzoNg
2025-06-11 15:54:01 +08:00
parent 45941778c9
commit e0d7facddd
6 changed files with 41 additions and 41 deletions

View File

@ -110,16 +110,22 @@ export const getConfiguredValue = (value: Record<string, any>, 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

View File

@ -157,6 +157,16 @@ const FormInputItem: FC<Props> = ({
})
}
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<Props> = ({
disabled={readOnly}
scope={scope || 'all'}
value={varInput?.value as any}
onSelect={handleValueChange}
onSelect={handleAppOrModelSelect}
/>
)}
{isModelSelector && isConstant && (
@ -257,7 +267,7 @@ const FormInputItem: FC<Props> = ({
isAdvancedMode
isInWorkflow
value={varInput?.value as any}
setModel={handleValueChange}
setModel={handleAppOrModelSelect}
readonly={readOnly}
scope={scope}
/>

View File

@ -10,6 +10,7 @@ const nodeDefault: NodeDefault<ToolNodeType> = {
defaultValue: {
tool_parameters: {},
tool_configurations: {},
version: '2',
},
getAvailablePrevNodes(isChatMode: boolean) {
const nodes = isChatMode

View File

@ -22,4 +22,5 @@ export type ToolNodeType = CommonNodeType & {
tool_configurations: Record<string, any>
output_schema: Record<string, any>
paramSchemas?: Record<string, any>[]
version?: string
}

View File

@ -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,

View File

@ -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<ToolNodeType>).data.version) {
(node as Node<ToolNodeType>).data.version = '2'
const toolConfigurations = (node as Node<ToolNodeType>).data.tool_configurations
if (toolConfigurations && Object.keys(toolConfigurations).length > 0) {
const newValues = { ...toolConfigurations }