update:优化代码

This commit is contained in:
hrz
2025-11-08 16:00:37 +08:00
parent d09fa0c1f6
commit c02f2105d7
4 changed files with 141 additions and 105 deletions
@@ -1,5 +1,6 @@
package xiaozhi.modules.agent.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -7,11 +8,11 @@ import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import xiaozhi.common.utils.JsonUtils;
import xiaozhi.modules.agent.dao.AgentPluginMappingMapper;
import xiaozhi.modules.agent.entity.AgentPluginMapping;
import xiaozhi.modules.agent.service.AgentPluginMappingService;
@@ -35,7 +36,8 @@ public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappin
@Override
public List<AgentPluginMapping> agentPluginParamsByAgentId(String agentId) {
List<AgentPluginMapping> list = agentPluginMappingMapper.selectPluginsByAgentId(agentId);
int index = 0;
Map<String, List<KnowledgeBaseEntity>> knowledgeBaseMap = new HashMap<>();
Map<String, ModelConfigEntity> modelConfigMap = new HashMap<>();
for (int i = list.size() - 1; i >= 0; i--) {
AgentPluginMapping mapping = list.get(i);
if (StringUtils.isBlank(mapping.getProviderCode())) {
@@ -51,14 +53,39 @@ public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappin
list.remove(i);
continue;
}
Map<String, String> paramInfo = new HashMap<>(2);
paramInfo.put("name", knowledgeBaseEntity.getName());
paramInfo.put("description", knowledgeBaseEntity.getDescription());
mapping.setParamInfo(JSONUtils.toJSONString(paramInfo));
String providerCode = "xzKnowledgeBase_search_from_" + modelConfigEntity.getModelCode() + "_"
+ index;
index++;
mapping.setProviderCode(providerCode);
List<KnowledgeBaseEntity> knowledgeBaseList = knowledgeBaseMap.get(modelConfigEntity.getModelCode());
if (knowledgeBaseList == null) {
knowledgeBaseList = new ArrayList<>();
}
modelConfigMap.put(modelConfigEntity.getModelCode(), modelConfigEntity);
knowledgeBaseList.add(knowledgeBaseEntity);
knowledgeBaseMap.put(modelConfigEntity.getModelCode(), knowledgeBaseList);
list.remove(i);
}
}
if (knowledgeBaseMap.size() > 0) {
for (String pluginCode : knowledgeBaseMap.keySet()) {
List<KnowledgeBaseEntity> knowledgeBaseList = knowledgeBaseMap.get(pluginCode);
if (knowledgeBaseList == null || knowledgeBaseList.isEmpty()) {
continue;
}
AgentPluginMapping agentPluginMapping = new AgentPluginMapping();
agentPluginMapping.setAgentId(agentId);
agentPluginMapping.setPluginId(pluginCode);
agentPluginMapping.setProviderCode("search_from_" + pluginCode);
agentPluginMapping.setId(Long.valueOf(list.size() + 1));
Map<String, Object> paramInfo = new HashMap<>(4);
ModelConfigEntity modelConfigEntity = modelConfigMap.get(pluginCode);
paramInfo.put("base_url", modelConfigEntity.getConfigJson().getStr("base_url"));
paramInfo.put("api_key", modelConfigEntity.getConfigJson().getStr("api_key"));
paramInfo.put("dataset_ids",
knowledgeBaseList.stream().map(KnowledgeBaseEntity::getDatasetId).toList());
paramInfo.put("description",
String.join(",", knowledgeBaseList.stream().map(KnowledgeBaseEntity::getDescription).toList()));
agentPluginMapping.setParamInfo(JsonUtils.toJsonString(paramInfo));
list.add(agentPluginMapping);
}
}
return list;