mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 19:59:50 +08:00
7bbe12b2bd
Co-authored-by: StyleZhang <jasonapring2015@outlook.com>
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import type { ToolCredential, ToolParameter } from '../types'
|
|
const toType = (type: string) => {
|
|
switch (type) {
|
|
case 'string':
|
|
return 'text-input'
|
|
case 'number':
|
|
return 'number-input'
|
|
default:
|
|
return type
|
|
}
|
|
}
|
|
export const toolParametersToFormSchemas = (parameters: ToolParameter[]) => {
|
|
if (!parameters)
|
|
return []
|
|
|
|
const formSchemas = parameters.map((parameter) => {
|
|
return {
|
|
...parameter,
|
|
variable: parameter.name,
|
|
type: toType(parameter.type),
|
|
show_on: [],
|
|
options: parameter.options?.map((option) => {
|
|
return {
|
|
...option,
|
|
show_on: [],
|
|
}
|
|
}),
|
|
tooltip: parameter.human_description,
|
|
}
|
|
})
|
|
return formSchemas
|
|
}
|
|
|
|
export const toolCredentialToFormSchemas = (parameters: ToolCredential[]) => {
|
|
if (!parameters)
|
|
return []
|
|
|
|
const formSchemas = parameters.map((parameter) => {
|
|
return {
|
|
...parameter,
|
|
variable: parameter.name,
|
|
label: parameter.label,
|
|
tooltip: parameter.help,
|
|
show_on: [],
|
|
options: parameter.options?.map((option) => {
|
|
return {
|
|
...option,
|
|
show_on: [],
|
|
}
|
|
}),
|
|
}
|
|
})
|
|
return formSchemas
|
|
}
|
|
|
|
export const addDefaultValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any }[]) => {
|
|
const newValues = { ...value }
|
|
formSchemas.forEach((formSchema) => {
|
|
const itemValue = value[formSchema.variable]
|
|
if (formSchema.default && (value === undefined || itemValue === null || itemValue === ''))
|
|
newValues[formSchema.variable] = formSchema.default
|
|
})
|
|
return newValues
|
|
}
|