Files
webapp-conversation/utils/prompt.ts
2023-04-14 20:35:08 +08:00

12 lines
431 B
TypeScript

import { PromptVariable } from '@/types/app'
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
const name = inputs[key]
if (name)
return name
const valueObj: PromptVariable | undefined = promptVariables.find(v => v.key === key)
return valueObj ? `{{${valueObj.key}}}` : match
})
}