From 40c5396a80463f4ab528a3ee38c8bd47129d4952 Mon Sep 17 00:00:00 2001 From: sp mac bookpro 2605 Date: Wed, 3 Jun 2026 01:54:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A8=A1=E5=9E=8B=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=A0=BC=E5=BC=8F=E6=94=B9=E4=B8=BAJSON?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - handleSave中将model字段转换为JSON字符串格式:[{id, name}] - 更新parseModelSelections支持解析JSON格式和旧逗号分隔格式 - 移除normalize函数,由handleSave统一处理格式转换 --- .../components/CapabilitySettings.tsx | 3 -- src/pages/AgentEditor/constants.ts | 30 ++++++++++++++----- src/pages/AgentEditor/hooks/useAgentEditor.ts | 8 +++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/pages/AgentEditor/components/CapabilitySettings.tsx b/src/pages/AgentEditor/components/CapabilitySettings.tsx index cc24fc4..e6f9fe6 100644 --- a/src/pages/AgentEditor/components/CapabilitySettings.tsx +++ b/src/pages/AgentEditor/components/CapabilitySettings.tsx @@ -152,9 +152,6 @@ export default function CapabilitySettings({ label="模型" className="mb-0" getValueProps={(value) => ({ value: parseModelSelections(value) })} - normalize={(value) => - Array.isArray(value) ? value.map((item) => String(item).trim()).filter(Boolean).join(',') : '' - } > diff --git a/src/pages/AgentEditor/constants.ts b/src/pages/AgentEditor/constants.ts index d749c87..81a57d7 100644 --- a/src/pages/AgentEditor/constants.ts +++ b/src/pages/AgentEditor/constants.ts @@ -32,10 +32,26 @@ export const TYPE_TAG: Record url?.startsWith('http') || url?.startsWith('/'); -export const parseModelSelections = (value?: string | string[]) => - Array.isArray(value) - ? value - : String(value || '') - .split(',') - .map((item) => item.trim()) - .filter(Boolean); +export const parseModelSelections = (value?: string | string[]) => { + if (Array.isArray(value)) { + return value; + } + // 尝试解析 JSON 格式 + try { + const parsed = JSON.parse(String(value || '[]')); + if (Array.isArray(parsed)) { + return parsed.map((item: any) => { + if (typeof item === 'object' && item.id) { + return item.id; + } + return String(item); + }).filter(Boolean); + } + } catch { + // 兼容旧格式:逗号分隔的字符串 + } + return String(value || '') + .split(',') + .map((item) => item.trim()) + .filter(Boolean); +}; diff --git a/src/pages/AgentEditor/hooks/useAgentEditor.ts b/src/pages/AgentEditor/hooks/useAgentEditor.ts index e1e7fab..e913c9c 100644 --- a/src/pages/AgentEditor/hooks/useAgentEditor.ts +++ b/src/pages/AgentEditor/hooks/useAgentEditor.ts @@ -163,9 +163,13 @@ export function useAgentEditor({ id, isNew, form, message, navigate }: UseAgentE Object.keys(values).forEach((key) => { const formValue = (values as any)[key]; const originalValue = (agent as any)?.[key]; - // 特殊处理 model 字段:如果是数组,用逗号拼接成字符串 + // 特殊处理 model 字段:将 id 和 name 组合成 JSON 数组字符串 if (key === 'model' && Array.isArray(formValue)) { - changedFields[key] = formValue.join(','); + const modelObjects = formValue.map((modelId: string) => { + const model = models.find((m) => m.id === modelId); + return { id: modelId, name: model?.model_name || '' }; + }); + changedFields[key] = JSON.stringify(modelObjects); } else if (formValue !== originalValue) { changedFields[key] = formValue; }