Files
webapp-conversation/utils/prompt.ts
lyzno1 05dcfcf0ca feat: migrate ESLint to v9 flat config
- Replace .eslintrc.json with eslint.config.mjs
- Simplify configuration using @antfu/eslint-config
- Add necessary ESLint plugin dependencies
- Disable overly strict style rules
- Set package.json type to module for ESM support
- Fix ESLint disable comment format

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-15 10:55:10 +08:00

64 lines
1.8 KiB
TypeScript

import type { PromptVariable, UserInputFormItem } 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
})
}
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
if (!useInputs) { return [] }
const promptVariables: PromptVariable[] = []
useInputs.forEach((item: any) => {
const [type, content] = (() => {
const type = Object.keys(item)[0]
return [type === 'text-input' ? 'string' : type, item[type]]
})()
if (type === 'string' || type === 'paragraph') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
max_length: content.max_length,
options: [],
})
}
else if (type === 'number') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
options: [],
})
}
else if (type === 'file' || type === 'file-list') {
promptVariables.push({
...content,
key: content.variable,
name: content.label,
required: content.required,
type,
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
}