dify/web/utils/model-config.ts

75 lines
2.0 KiB
TypeScript
Raw Normal View History

import type { UserInputFormItem } from '@/types/app'
import type { PromptVariable } from '@/models/debug'
2023-05-15 08:51:32 +08:00
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
if (!useInputs)
return []
2023-05-15 08:51:32 +08:00
const promptVariables: PromptVariable[] = []
useInputs.forEach((item: any) => {
const isParagraph = !!item.paragraph
const [type, content] = (() => {
if (isParagraph)
return ['paragraph', item.paragraph]
if (item['text-input'])
return ['string', item['text-input']]
return ['select', item.select]
})()
if (type === 'string' || type === 'paragraph') {
2023-05-15 08:51:32 +08:00
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
2023-05-15 08:51:32 +08:00
max_length: content.max_length,
options: [],
})
}
else {
2023-05-15 08:51:32 +08:00
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: 'select',
options: content.options,
})
}
})
return promptVariables
}
export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
const userInputs: UserInputFormItem[] = []
promptVariables.filter(({ key, name }) => {
if (key && key.trim() && name && name.trim())
2023-05-15 08:51:32 +08:00
return true
2023-05-15 08:51:32 +08:00
return false
}).forEach((item: any) => {
if (item.type === 'string' || item.type === 'paragraph') {
2023-05-15 08:51:32 +08:00
userInputs.push({
[item.type === 'string' ? 'text-input' : 'paragraph']: {
2023-05-15 08:51:32 +08:00
label: item.name,
variable: item.key,
required: item.required !== false, // default true
2023-05-15 08:51:32 +08:00
max_length: item.max_length,
default: '',
2023-05-15 08:51:32 +08:00
},
} as any)
}
else {
2023-05-15 08:51:32 +08:00
userInputs.push({
select: {
2023-05-15 08:51:32 +08:00
label: item.name,
variable: item.key,
required: item.required !== false, // default true
2023-05-15 08:51:32 +08:00
options: item.options,
default: '',
2023-05-15 08:51:32 +08:00
},
} as any)
}
})
return userInputs
}