feat: to new api

This commit is contained in:
Joel
2023-05-09 14:56:37 +08:00
parent d728cbb267
commit 06f502a524
5 changed files with 59 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import { PromptVariable } from '@/types/app'
import { PromptVariable, UserInputFormItem } from '@/types/app'
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
@ -9,4 +9,32 @@ export function replaceVarWithValues(str: string, promptVariables: PromptVariabl
const valueObj: PromptVariable | undefined = promptVariables.find(v => v.key === key)
return valueObj ? `{{${valueObj.key}}}` : match
})
}
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
if (!useInputs) return []
const promptVariables: PromptVariable[] = []
useInputs.forEach((item: any) => {
const type = item['text-input'] ? 'string' : 'select'
const content = type === 'string' ? item['text-input'] : item['select']
if (type === 'string') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: 'string',
max_length: content.max_length,
options: [],
})
} else {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: 'select',
options: content.options,
})
}
})
return promptVariables
}