fix: 修复提示词模板识别’{{}}‘的问题
This commit is contained in:
@@ -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",
|
template_type: template.template_type as "LLM_Extraction" | "VLM_Extraction" | "Evaluation" | "Summary" | "Common",
|
||||||
description: template.description || '',
|
description: template.description || '',
|
||||||
template_content: template.template_content,
|
template_content: template.template_content,
|
||||||
variables: template.variables,
|
variables: template.variables || {}, // 如果variables为null,则使用空对象
|
||||||
status: mapStatusToUI(template.status),
|
status: mapStatusToUI(template.status),
|
||||||
version: template.version,
|
version: template.version,
|
||||||
created_by: template.created_by,
|
created_by: template.created_by,
|
||||||
|
|||||||
@@ -215,7 +215,17 @@ const extractVariables = (content: string) => {
|
|||||||
|
|
||||||
while ((match = regex.exec(content)) !== null) {
|
while ((match = regex.exec(content)) !== null) {
|
||||||
const varName = match[1].trim();
|
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;
|
variables[varName] = varName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -269,9 +279,11 @@ export default function PromptsNew() {
|
|||||||
}));
|
}));
|
||||||
} else if (template) {
|
} else if (template) {
|
||||||
// 否则使用模板数据
|
// 否则使用模板数据
|
||||||
const variablesJson = typeof template.variables === 'string'
|
// 处理变量数据,确保null和undefined转换为空对象
|
||||||
? template.variables
|
const variablesObj = template.variables || {};
|
||||||
: JSON.stringify(template.variables);
|
const variablesJson = typeof variablesObj === 'string'
|
||||||
|
? variablesObj
|
||||||
|
: JSON.stringify(variablesObj);
|
||||||
|
|
||||||
const newFormData = {
|
const newFormData = {
|
||||||
...template,
|
...template,
|
||||||
@@ -284,13 +296,14 @@ export default function PromptsNew() {
|
|||||||
setFormData(newFormData);
|
setFormData(newFormData);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const vars = typeof template.variables === 'string'
|
const vars = typeof variablesObj === 'string'
|
||||||
? JSON.parse(template.variables)
|
? JSON.parse(variablesObj)
|
||||||
: template.variables;
|
: variablesObj;
|
||||||
|
|
||||||
setExampleValues(vars);
|
setExampleValues(vars || {});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("解析变量失败:", e);
|
console.error("解析变量失败:", e);
|
||||||
|
setExampleValues({});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (template.template_content) {
|
if (template.template_content) {
|
||||||
|
|||||||
Reference in New Issue
Block a user