From 730e7cb7fe83f7c5c8aa554d8a9efb004185dc34 Mon Sep 17 00:00:00 2001 From: yorn <1057707203@qq.com> Date: Thu, 6 Nov 2025 18:10:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=AF=8D=E6=A8=A1=E6=9D=BF=E8=AF=86=E5=88=AB=E2=80=99{{}}?= =?UTF-8?q?=E2=80=98=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/prompts/prompts.ts | 2 +- app/routes/prompts.new.tsx | 41 +++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/api/prompts/prompts.ts b/app/api/prompts/prompts.ts index b721a0a..67ec53a 100644 --- a/app/api/prompts/prompts.ts +++ b/app/api/prompts/prompts.ts @@ -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, diff --git a/app/routes/prompts.new.tsx b/app/routes/prompts.new.tsx index 8efa694..3b1464b 100644 --- a/app/routes/prompts.new.tsx +++ b/app/routes/prompts.new.tsx @@ -212,14 +212,24 @@ const extractVariables = (content: string) => { const regex = /{([^{}]+)}/g; const variables: Record = {}; 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) {