From f16dc7ae493f817c87b74ef8a486e22ebee8bcd1 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Mon, 1 Sep 2025 22:28:07 +0800 Subject: [PATCH] =?UTF-8?q?feat(AgentConfigSidebar):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9=E9=AA=8C=E8=AF=81=E5=92=8C=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在保存配置前验证并过滤无效的工具ID和选项值,确保配置有效性 --- web/src/components/AgentConfigSidebar.vue | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/web/src/components/AgentConfigSidebar.vue b/web/src/components/AgentConfigSidebar.vue index 875980cb..a27d826b 100644 --- a/web/src/components/AgentConfigSidebar.vue +++ b/web/src/components/AgentConfigSidebar.vue @@ -466,6 +466,49 @@ const cancelToolsSelection = () => { selectedTools.value = []; }; +// 验证和过滤配置项 +const validateAndFilterConfig = () => { + const validatedConfig = { ...agentConfig.value }; + const configItems = configurableItems.value; + + // 遍历所有配置项 + Object.keys(configItems).forEach(key => { + const configItem = configItems[key]; + const currentValue = validatedConfig[key]; + + // 检查工具配置 + if (configItem.template_metadata?.kind === 'tools' && Array.isArray(currentValue)) { + const availableToolIds = availableTools.value ? Object.values(availableTools.value).map(tool => tool.id) : []; + validatedConfig[key] = currentValue.filter(toolId => availableToolIds.includes(toolId)); + + if (validatedConfig[key].length !== currentValue.length) { + console.warn(`工具配置 ${key} 中包含无效的工具ID,已自动过滤`); + } + } + + // 检查多选配置项 (type === 'list' 且有 options) + else if (configItem.type === 'list' && configItem.options && Array.isArray(currentValue)) { + const validOptions = configItem.options; + validatedConfig[key] = currentValue.filter(value => validOptions.includes(value)); + + if (validatedConfig[key].length !== currentValue.length) { + console.warn(`配置项 ${key} 中包含无效的选项,已自动过滤`); + } + } + + // 检查单选配置项 (有 options 且不是 list 类型) + else if (configItem.options && configItem.type !== 'list' && currentValue !== undefined) { + const validOptions = configItem.options; + if (!validOptions.includes(currentValue)) { + console.warn(`配置项 ${key} 的值 "${currentValue}" 不在有效选项中,将重置为默认值`); + validatedConfig[key] = configItem.default || validOptions[0] || null; + } + } + }); + + return validatedConfig; +}; + // 配置保存和重置 const saveConfig = async () => { if (!selectedAgentId.value) { @@ -474,6 +517,15 @@ const saveConfig = async () => { } try { + // 验证和过滤配置 + const validatedConfig = validateAndFilterConfig(); + + // 如果配置有变化,先更新到store + if (JSON.stringify(validatedConfig) !== JSON.stringify(agentConfig.value)) { + agentStore.updateAgentConfig(validatedConfig); + message.info('检测到无效配置项,已自动过滤'); + } + await agentStore.saveAgentConfig(); message.success('配置已保存到服务器'); } catch (error) {