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