fix: 修复提示词模板识别’{{}}‘的问题

This commit is contained in:
2025-11-06 18:10:55 +08:00
parent 1f9c8293a8
commit 730e7cb7fe
2 changed files with 28 additions and 15 deletions
+1 -1
View File
@@ -102,7 +102,7 @@ export function convertToUITemplate(template: PromptTemplate & { sso_users?: { u
template_type: template.template_type as "LLM_Extraction" | "VLM_Extraction" | "Evaluation" | "Summary" | "Common",
description: template.description || '',
template_content: template.template_content,
variables: template.variables,
variables: template.variables || {}, // 如果variables为null,则使用空对象
status: mapStatusToUI(template.status),
version: template.version,
created_by: template.created_by,
+27 -14
View File
@@ -212,14 +212,24 @@ const extractVariables = (content: string) => {
const regex = /{([^{}]+)}/g;
const variables: Record<string, string> = {};
let match;
while ((match = regex.exec(content)) !== null) {
const varName = match[1].trim();
if (varName && !variables[varName]) {
// 过滤掉无效的变量名:
// 1. 不能为空
// 2. 不能包含冒号(避免匹配 JSON 对象)
// 3. 不能包含引号(避免匹配 JSON 字符串)
// 4. 只允许字母、数字、下划线、中文
if (varName &&
!variables[varName] &&
!varName.includes(':') &&
!varName.includes('"') &&
!varName.includes("'") &&
/^[\w\u4e00-\u9fa5]+$/.test(varName)) {
variables[varName] = varName;
}
}
return variables;
};
@@ -269,10 +279,12 @@ export default function PromptsNew() {
}));
} else if (template) {
// 否则使用模板数据
const variablesJson = typeof template.variables === 'string'
? template.variables
: JSON.stringify(template.variables);
// 处理变量数据,确保null和undefined转换为空对象
const variablesObj = template.variables || {};
const variablesJson = typeof variablesObj === 'string'
? variablesObj
: JSON.stringify(variablesObj);
const newFormData = {
...template,
id: mode === "clone" ? "" : template.id,
@@ -280,17 +292,18 @@ export default function PromptsNew() {
version: mode === "clone" ? "v1.0" : template.version,
variables: variablesJson
};
setFormData(newFormData);
try {
const vars = typeof template.variables === 'string'
? JSON.parse(template.variables)
: template.variables;
setExampleValues(vars);
const vars = typeof variablesObj === 'string'
? JSON.parse(variablesObj)
: variablesObj;
setExampleValues(vars || {});
} catch (e) {
console.error("解析变量失败:", e);
setExampleValues({});
}
if (template.template_content) {