data formatting of tool data

pull/22091/head
JzoNg 12 months ago
parent 45941778c9
commit e0d7facddd

@ -110,16 +110,22 @@ export const getConfiguredValue = (value: Record<string, any>, formSchemas: { va
if (formSchema.type === 'boolean') { if (formSchema.type === 'boolean') {
if (typeof value === 'string') if (typeof value === 'string')
newValues[formSchema.variable].value = value === 'true' newValues[formSchema.variable].value = value === 'true' || value === '1'
if (typeof value === 'boolean') if (typeof value === 'boolean')
newValues[formSchema.variable].value = value newValues[formSchema.variable].value = value
if (typeof value === 'number')
newValues[formSchema.variable].value = value === 1
} }
if (formSchema.type === 'number-input') { if (formSchema.type === 'number-input') {
if (typeof value === 'string' && value !== '') if (typeof value === 'string' && value !== '')
newValues[formSchema.variable].value = Number.parseFloat(value) newValues[formSchema.variable].value = Number.parseFloat(value)
} }
if (formSchema.type === 'app-selector' || formSchema.type === 'model-selector')
newValues[formSchema.variable] = value
} }
}) })
return newValues return newValues

@ -157,6 +157,16 @@ const FormInputItem: FC<Props> = ({
}) })
} }
const handleAppOrModelSelect = (newValue: any) => {
onChange({
...value,
[variable]: {
...varInput,
...newValue,
},
})
}
const handleVariableSelectorChange = (newValue: ValueSelector | string, variable: string) => { const handleVariableSelectorChange = (newValue: ValueSelector | string, variable: string) => {
onChange({ onChange({
...value, ...value,
@ -248,7 +258,7 @@ const FormInputItem: FC<Props> = ({
disabled={readOnly} disabled={readOnly}
scope={scope || 'all'} scope={scope || 'all'}
value={varInput?.value as any} value={varInput?.value as any}
onSelect={handleValueChange} onSelect={handleAppOrModelSelect}
/> />
)} )}
{isModelSelector && isConstant && ( {isModelSelector && isConstant && (
@ -257,7 +267,7 @@ const FormInputItem: FC<Props> = ({
isAdvancedMode isAdvancedMode
isInWorkflow isInWorkflow
value={varInput?.value as any} value={varInput?.value as any}
setModel={handleValueChange} setModel={handleAppOrModelSelect}
readonly={readOnly} readonly={readOnly}
scope={scope} scope={scope}
/> />

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

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

@ -14,8 +14,7 @@ import {
} from '@/app/components/tools/utils/to-form-schema' } from '@/app/components/tools/utils/to-form-schema'
import Toast from '@/app/components/base/toast' import Toast from '@/app/components/base/toast'
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form' 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 } from '@/app/components/workflow/types'
import type { InputVar, ValueSelector, Var } from '@/app/components/workflow/types'
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run' import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
import { import {
useFetchToolsData, useFetchToolsData,
@ -42,7 +41,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
const workflowTools = useStore(s => s.workflowTools) const workflowTools = useStore(s => s.workflowTools)
const mcpTools = useStore(s => s.mcpTools) const mcpTools = useStore(s => s.mcpTools)
const currentTools = (() => { const currentTools = useMemo(() => {
switch (provider_type) { switch (provider_type) {
case CollectionType.builtIn: case CollectionType.builtIn:
return buildInTools return buildInTools
@ -55,7 +54,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
default: default:
return [] return []
} }
})() }, [buildInTools, customTools, mcpTools, provider_type, workflowTools])
const currCollection = currentTools.find(item => canFindTool(item.id, provider_id)) const currCollection = currentTools.find(item => canFindTool(item.id, provider_id))
// Auth // Auth
@ -99,10 +98,10 @@ const useConfig = (id: string, payload: ToolNodeType) => {
const value = newConfig[key] const value = newConfig[key]
if (schema?.type === 'boolean') { if (schema?.type === 'boolean') {
if (typeof value === 'string') if (typeof value === 'string')
newConfig[key] = Number.parseInt(value, 10) newConfig[key] = value === 'true' || value === '1'
if (typeof value === 'boolean') if (typeof value === 'number')
newConfig[key] = value ? 1 : 0 newConfig[key] = value === 1
} }
if (schema?.type === 'number-input') { if (schema?.type === 'number-input') {
@ -128,24 +127,20 @@ const useConfig = (id: string, payload: ToolNodeType) => {
}) })
}, [inputs, setInputs]) }, [inputs, setInputs])
useEffect(() => { const formattingParameters = () => {
if (!currTool)
return
const inputsWithDefaultValue = produce(inputs, (draft) => { const inputsWithDefaultValue = produce(inputs, (draft) => {
if (!draft.tool_configurations || Object.keys(draft.tool_configurations).length === 0) { if (!draft.tool_configurations || Object.keys(draft.tool_configurations).length === 0)
draft.tool_configurations = getConfiguredValue(tool_configurations, toolSettingSchema) draft.tool_configurations = getConfiguredValue(tool_configurations, toolSettingSchema)
} if (!draft.tool_parameters || Object.keys(draft.tool_parameters).length === 0)
else {
// TODO
}
if (!draft.tool_parameters || Object.keys(draft.tool_configurations).length === 0) {
draft.tool_parameters = getConfiguredValue(tool_parameters, toolInputVarSchema) draft.tool_parameters = getConfiguredValue(tool_parameters, toolInputVarSchema)
}
else {
// TODO: boolean & model & app formatting BOTH configuration & parameters
}
}) })
return inputsWithDefaultValue
}
useEffect(() => {
if (!currTool)
return
const inputsWithDefaultValue = formattingParameters()
setInputs(inputsWithDefaultValue) setInputs(inputsWithDefaultValue)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [currTool]) }, [currTool])
@ -158,19 +153,6 @@ const useConfig = (id: string, payload: ToolNodeType) => {
}) })
}, [inputs, setInputs]) }, [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) const isLoading = currTool && (isBuiltIn ? !currCollection : false)
// single run // single run
@ -314,8 +296,6 @@ const useConfig = (id: string, payload: ToolNodeType) => {
setToolSettingValue, setToolSettingValue,
toolInputVarSchema, toolInputVarSchema,
setInputVar, setInputVar,
handleOnVarOpen,
filterVar,
currCollection, currCollection,
isShowAuthBtn, isShowAuthBtn,
showSetAuth, showSetAuth,

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

Loading…
Cancel
Save