feat(AgentConfigSidebar): 添加配置项验证和过滤功能

在保存配置前验证并过滤无效的工具ID和选项值,确保配置有效性
This commit is contained in:
Wenjie Zhang 2025-09-01 22:28:07 +08:00
parent a1080a4641
commit f16dc7ae49

View File

@ -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) {