data formatting

pull/22091/head
JzoNg 12 months ago
parent ed2d971cf9
commit 57e1b896cd

@ -54,7 +54,7 @@ export const toolCredentialToFormSchemas = (parameters: ToolCredential[]) => {
return formSchemas return formSchemas
} }
export const addDefaultValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any }[]) => { export const addDefaultValue = (value: Record<string, any>, formSchemas: { variable: string; type: string; default?: any }[]) => {
const newValues = { ...value } const newValues = { ...value }
formSchemas.forEach((formSchema) => { formSchemas.forEach((formSchema) => {
const itemValue = value[formSchema.variable] const itemValue = value[formSchema.variable]
@ -94,3 +94,30 @@ export const getStructureValue = (value: Record<string, any>) => {
}) })
return newValue return newValue
} }
export const getConfiguredValue = (value: Record<string, any>, formSchemas: { variable: string; type: string; default?: any }[]) => {
const newValues = { ...value }
formSchemas.forEach((formSchema) => {
const itemValue = value[formSchema.variable]
if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined)) {
const value = formSchema.default
newValues[formSchema.variable] = {
type: 'constant',
value: formSchema.default,
}
if (formSchema.type === 'boolean') {
if (typeof value === 'string')
newValues[formSchema.variable].value = value === 'true' ? 1 : 0
if (typeof value === 'boolean')
newValues[formSchema.variable].value = value ? 1 : 0
}
if (formSchema.type === 'number-input') {
if (typeof value === 'string' && value !== '')
newValues[formSchema.variable].value = Number.parseFloat(value)
}
}
})
return newValues
}

@ -21,14 +21,14 @@ const Node: FC<NodeProps<ToolNodeType>> = ({
<div title={key} className='max-w-[100px] shrink-0 truncate text-xs font-medium uppercase text-text-tertiary'> <div title={key} className='max-w-[100px] shrink-0 truncate text-xs font-medium uppercase text-text-tertiary'>
{key} {key}
</div> </div>
{typeof tool_configurations[key] === 'string' && ( {typeof tool_configurations[key].value === 'string' && (
<div title={tool_configurations[key]} className='w-0 shrink-0 grow truncate text-right text-xs font-normal text-text-secondary'> <div title={tool_configurations[key]} className='w-0 shrink-0 grow truncate text-right text-xs font-normal text-text-secondary'>
{paramSchemas?.find(i => i.name === key)?.type === FormTypeEnum.secretInput ? '********' : tool_configurations[key]} {paramSchemas?.find(i => i.name === key)?.type === FormTypeEnum.secretInput ? '********' : tool_configurations[key].value}
</div> </div>
)} )}
{typeof tool_configurations[key] === 'number' && ( {typeof tool_configurations[key].value === 'number' && (
<div title={tool_configurations[key].toString()} className='w-0 shrink-0 grow truncate text-right text-xs font-normal text-text-secondary'> <div title={tool_configurations[key].toString()} className='w-0 shrink-0 grow truncate text-right text-xs font-normal text-text-secondary'>
{tool_configurations[key]} {tool_configurations[key].value}
</div> </div>
)} )}
{typeof tool_configurations[key] !== 'string' && tool_configurations[key]?.type === FormTypeEnum.modelSelector && ( {typeof tool_configurations[key] !== 'string' && tool_configurations[key]?.type === FormTypeEnum.modelSelector && (
@ -36,11 +36,6 @@ const Node: FC<NodeProps<ToolNodeType>> = ({
{tool_configurations[key].model} {tool_configurations[key].model}
</div> </div>
)} )}
{/* {typeof tool_configurations[key] !== 'string' && tool_configurations[key]?.type === FormTypeEnum.appSelector && (
<div title={tool_configurations[key].app_id} className='grow w-0 shrink-0 truncate text-right text-xs font-normal text-gray-700'>
{tool_configurations[key].app_id}
</div>
)} */}
</div> </div>
))} ))}

@ -8,7 +8,10 @@ import { useLanguage } from '@/app/components/header/account-setting/model-provi
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import { CollectionType } from '@/app/components/tools/types' import { CollectionType } from '@/app/components/tools/types'
import { updateBuiltInToolCredential } from '@/service/tools' import { updateBuiltInToolCredential } from '@/service/tools'
import { addDefaultValue, toolParametersToFormSchemas } from '@/app/components/tools/utils/to-form-schema' import {
getConfiguredValue,
toolParametersToFormSchemas,
} 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 { VarType as VarVarType } from '@/app/components/workflow/types'
@ -28,8 +31,8 @@ const useConfig = (id: string, payload: ToolNodeType) => {
const language = useLanguage() const language = useLanguage()
const { inputs, setInputs: doSetInputs } = useNodeCrud<ToolNodeType>(id, payload) const { inputs, setInputs: doSetInputs } = useNodeCrud<ToolNodeType>(id, payload)
/* /*
* tool_configurations: tool setting, not dynamic setting * tool_configurations: tool setting, not dynamic setting (form type = form)
* tool_parameters: tool dynamic setting(by user) * tool_parameters: tool dynamic setting(form type = llm)
* output_schema: tool dynamic output * output_schema: tool dynamic output
*/ */
const { provider_id, provider_type, tool_name, tool_configurations, output_schema } = inputs const { provider_id, provider_type, tool_name, tool_configurations, output_schema } = inputs
@ -112,12 +115,11 @@ const useConfig = (id: string, payload: ToolNodeType) => {
doSetInputs(newInputs) doSetInputs(newInputs)
}, [doSetInputs, formSchemas, hasShouldTransferTypeSettingInput]) }, [doSetInputs, formSchemas, hasShouldTransferTypeSettingInput])
const [notSetDefaultValue, setNotSetDefaultValue] = useState(false) const [notSetDefaultValue, setNotSetDefaultValue] = useState(false)
const toolSettingValue = (() => { const toolSettingValue = useMemo(() => {
if (notSetDefaultValue) if (notSetDefaultValue)
return tool_configurations return tool_configurations
return getConfiguredValue(tool_configurations, toolSettingSchema)
return addDefaultValue(tool_configurations, toolSettingSchema) }, [notSetDefaultValue, toolSettingSchema, tool_configurations])
})()
const setToolSettingValue = useCallback((value: Record<string, any>) => { const setToolSettingValue = useCallback((value: Record<string, any>) => {
setNotSetDefaultValue(true) setNotSetDefaultValue(true)
setInputs({ setInputs({
@ -131,7 +133,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
return 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 = addDefaultValue(tool_configurations, toolSettingSchema) draft.tool_configurations = getConfiguredValue(tool_configurations, toolSettingSchema)
if (!draft.tool_parameters) if (!draft.tool_parameters)
draft.tool_parameters = {} draft.tool_parameters = {}

@ -28,6 +28,7 @@ import type { IfElseNodeType } from '../nodes/if-else/types'
import { branchNameCorrect } from '../nodes/if-else/utils' import { branchNameCorrect } from '../nodes/if-else/utils'
import type { IterationNodeType } from '../nodes/iteration/types' import type { IterationNodeType } from '../nodes/iteration/types'
import type { LoopNodeType } from '../nodes/loop/types' import type { LoopNodeType } from '../nodes/loop/types'
import type { ToolNodeType } from '../nodes/tool/types'
import { import {
getIterationStartNode, getIterationStartNode,
getLoopStartNode, getLoopStartNode,
@ -276,6 +277,7 @@ export const initialNodes = (originNodes: Node[], originEdges: Edge[]) => {
if (node.data.type === BlockEnum.ParameterExtractor) if (node.data.type === BlockEnum.ParameterExtractor)
(node as any).data.model.provider = correctModelProvider((node as any).data.model.provider) (node as any).data.model.provider = correctModelProvider((node as any).data.model.provider)
if (node.data.type === BlockEnum.HttpRequest && !node.data.retry_config) { if (node.data.type === BlockEnum.HttpRequest && !node.data.retry_config) {
node.data.retry_config = { node.data.retry_config = {
retry_enabled: true, retry_enabled: true,
@ -284,6 +286,22 @@ export const initialNodes = (originNodes: Node[], originEdges: Edge[]) => {
} }
} }
if (node.data.type === BlockEnum.Tool) {
const toolConfigurations = (node as Node<ToolNodeType>).data.tool_configurations
if (toolConfigurations && Object.keys(toolConfigurations).length > 0) {
const newValues = { ...toolConfigurations }
Object.keys(toolConfigurations).forEach((key) => {
if (typeof toolConfigurations[key] !== 'object') {
newValues[key] = {
type: 'constant',
value: toolConfigurations[key],
}
}
});
(node as Node<ToolNodeType>).data.tool_configurations = newValues
}
}
return node return node
}) })
} }

Loading…
Cancel
Save