From f26cf43f67f59b0d9536dfb869adc26552fb17bf Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Fri, 11 Apr 2025 13:19:17 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xiaozhi/common/redis/RedisKeys.java | 6 + .../modules/agent/service/AgentService.java | 8 - .../agent/service/impl/AgentServiceImpl.java | 46 ---- .../config/controller/ConfigController.java | 10 +- .../init}/SystemInitConfig.java | 7 +- .../modules/config/service/ConfigService.java | 21 ++ .../service/impl/ConfigServiceImpl.java | 202 ++++++++++++++++++ .../modules/sys/service/SysParamsService.java | 7 - .../service/impl/SysParamsServiceImpl.java | 67 ------ 9 files changed, 239 insertions(+), 135 deletions(-) rename main/manager-api/src/main/java/xiaozhi/modules/{sys/config => config/init}/SystemInitConfig.java (73%) create mode 100644 main/manager-api/src/main/java/xiaozhi/modules/config/service/ConfigService.java create mode 100644 main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java diff --git a/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java b/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java index 9d855fc26..e4c2fb424 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java +++ b/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java @@ -62,4 +62,10 @@ public class RedisKeys { return "agent:device:count:" + id; } + /** + * 获取系统配置缓存key + */ + public static String getServerConfigKey() { + return "server:config"; + } } diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/AgentService.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/AgentService.java index cd50d4cf0..8d0b29593 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/AgentService.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/AgentService.java @@ -42,12 +42,4 @@ public interface AgentService extends BaseService { * @return 设备数量 */ Integer getDeviceCountByAgentId(String agentId); - - /** - * 根据设备MAC地址获取智能体模型配置 - * - * @param macAddress 设备MAC地址 - * @return 模型配置信息 - */ - Map getAgentModelsByMac(String macAddress); } \ No newline at end of file diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java index 6a87be551..bb2a3ed0f 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java @@ -1,6 +1,5 @@ package xiaozhi.modules.agent.service.impl; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -14,7 +13,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; -import xiaozhi.common.exception.RenException; import xiaozhi.common.page.PageData; import xiaozhi.common.redis.RedisKeys; import xiaozhi.common.redis.RedisUtils; @@ -23,9 +21,6 @@ import xiaozhi.modules.agent.dao.AgentDao; import xiaozhi.modules.agent.dto.AgentDTO; import xiaozhi.modules.agent.entity.AgentEntity; import xiaozhi.modules.agent.service.AgentService; -import xiaozhi.modules.device.entity.DeviceEntity; -import xiaozhi.modules.device.service.DeviceService; -import xiaozhi.modules.model.entity.ModelConfigEntity; import xiaozhi.modules.model.service.ModelConfigService; import xiaozhi.modules.timbre.service.TimbreService; @@ -42,9 +37,6 @@ public class AgentServiceImpl extends BaseServiceImpl imp @Autowired private RedisUtils redisUtils; - @Autowired - private DeviceService deviceService; - public AgentServiceImpl(AgentDao agentDao) { this.agentDao = agentDao; } @@ -138,42 +130,4 @@ public class AgentServiceImpl extends BaseServiceImpl imp return deviceCount != null ? deviceCount : 0; } - - @Override - public Map getAgentModelsByMac(String macAddress) { - - // 根据MAC地址查找设备 - DeviceEntity device = deviceService.getDeviceByMacAddress(macAddress); - if (device == null) { - throw new RenException("设备未找到"); - } - - // 获取智能体信息 - AgentEntity agent = getAgentById(device.getAgentId()); - if (agent == null) { - throw new RenException("智能体未找到"); - } - - // 构建模块选择 - Map selectedModule = new HashMap<>(); - - // 构建返回数据 - Map result = new HashMap<>(); - result.put("prompt", agent.getSystemPrompt()); - - String[] modelTypes = { "VAD", "Intent", "Memory", "ASR", "LLM", "TTS" }; - String[] modelIds = { agent.getVadModelId(), agent.getIntentModelId(), agent.getMemModelId(), - agent.getAsrModelId(), agent.getLlmModelId(), agent.getTtsModelId() }; - - for (int i = 0; i < modelIds.length; i++) { - ModelConfigEntity model = modelConfigService.getModelById(modelIds[i]); - Map typeConfig = new HashMap<>(); - typeConfig.put(model.getModelCode(), model.getConfigJson()); - result.put(modelTypes[i], typeConfig); - selectedModule.put(modelTypes[i], model.getModelCode()); - } - result.put("selected_module", selectedModule); - - return result; - } } \ No newline at end of file diff --git a/main/manager-api/src/main/java/xiaozhi/modules/config/controller/ConfigController.java b/main/manager-api/src/main/java/xiaozhi/modules/config/controller/ConfigController.java index a8433097c..cfe4a5e27 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/config/controller/ConfigController.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/config/controller/ConfigController.java @@ -1,7 +1,5 @@ package xiaozhi.modules.config.controller; -import java.util.Map; - import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -15,8 +13,8 @@ import xiaozhi.common.constant.Constant; import xiaozhi.common.exception.RenException; import xiaozhi.common.utils.Result; import xiaozhi.common.validator.ValidatorUtils; -import xiaozhi.modules.agent.service.AgentService; import xiaozhi.modules.config.dto.AgentModelsDTO; +import xiaozhi.modules.config.service.ConfigService; import xiaozhi.modules.sys.dto.ConfigSecretDTO; import xiaozhi.modules.sys.service.SysParamsService; @@ -30,8 +28,8 @@ import xiaozhi.modules.sys.service.SysParamsService; @Tag(name = "参数管理") @AllArgsConstructor public class ConfigController { + private final ConfigService configService; private final SysParamsService sysParamsService; - private final AgentService agentService; @PostMapping("server-base") @Operation(summary = "获取配置") @@ -39,7 +37,7 @@ public class ConfigController { // 效验数据 ValidatorUtils.validateEntity(dto); checkSecret(dto.getSecret()); - Object config = sysParamsService.getConfig(); + Object config = configService.getConfig(true); return new Result().ok(config); } @@ -49,7 +47,7 @@ public class ConfigController { // 效验数据 ValidatorUtils.validateEntity(dto); checkSecret(dto.getSecret()); - Map models = agentService.getAgentModelsByMac(dto.getMacAddress()); + Object models = configService.getAgentModels(dto.getMacAddress()); return new Result().ok(models); } diff --git a/main/manager-api/src/main/java/xiaozhi/modules/sys/config/SystemInitConfig.java b/main/manager-api/src/main/java/xiaozhi/modules/config/init/SystemInitConfig.java similarity index 73% rename from main/manager-api/src/main/java/xiaozhi/modules/sys/config/SystemInitConfig.java rename to main/manager-api/src/main/java/xiaozhi/modules/config/init/SystemInitConfig.java index 5122367d7..a8e3794e9 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/sys/config/SystemInitConfig.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/config/init/SystemInitConfig.java @@ -1,10 +1,11 @@ -package xiaozhi.modules.sys.config; +package xiaozhi.modules.config.init; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import jakarta.annotation.PostConstruct; +import xiaozhi.modules.config.service.ConfigService; import xiaozhi.modules.sys.service.SysParamsService; @Configuration @@ -14,8 +15,12 @@ public class SystemInitConfig { @Autowired private SysParamsService sysParamsService; + @Autowired + private ConfigService configService; + @PostConstruct public void init() { sysParamsService.initServerSecret(); + configService.getConfig(false); } } \ No newline at end of file diff --git a/main/manager-api/src/main/java/xiaozhi/modules/config/service/ConfigService.java b/main/manager-api/src/main/java/xiaozhi/modules/config/service/ConfigService.java new file mode 100644 index 000000000..220de77e6 --- /dev/null +++ b/main/manager-api/src/main/java/xiaozhi/modules/config/service/ConfigService.java @@ -0,0 +1,21 @@ +package xiaozhi.modules.config.service; + +import java.util.Map; + +public interface ConfigService { + /** + * 获取服务器配置 + * + * @param isCache 是否缓存 + * @return 配置信息 + */ + Object getConfig(Boolean isCache); + + /** + * 获取智能体模型配置 + * + * @param macAddress MAC地址 + * @return 模型配置信息 + */ + Map getAgentModels(String macAddress); +} \ No newline at end of file diff --git a/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java new file mode 100644 index 000000000..58f4b945b --- /dev/null +++ b/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java @@ -0,0 +1,202 @@ +package xiaozhi.modules.config.service.impl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import lombok.AllArgsConstructor; +import xiaozhi.common.exception.RenException; +import xiaozhi.common.redis.RedisKeys; +import xiaozhi.common.redis.RedisUtils; +import xiaozhi.common.utils.JsonUtils; +import xiaozhi.modules.agent.entity.AgentEntity; +import xiaozhi.modules.agent.entity.AgentTemplateEntity; +import xiaozhi.modules.agent.service.AgentService; +import xiaozhi.modules.agent.service.AgentTemplateService; +import xiaozhi.modules.config.service.ConfigService; +import xiaozhi.modules.device.entity.DeviceEntity; +import xiaozhi.modules.device.service.DeviceService; +import xiaozhi.modules.model.entity.ModelConfigEntity; +import xiaozhi.modules.model.service.ModelConfigService; +import xiaozhi.modules.sys.dto.SysParamsDTO; +import xiaozhi.modules.sys.service.SysParamsService; + +@Service +@AllArgsConstructor +public class ConfigServiceImpl implements ConfigService { + private final SysParamsService sysParamsService; + private final DeviceService deviceService; + private final ModelConfigService modelConfigService; + private final AgentService agentService; + private final AgentTemplateService agentTemplateService; + private final RedisUtils redisUtils; + + @Override + public Object getConfig(Boolean isCache) { + if (isCache) { + // 先从Redis获取配置 + Object cachedConfig = redisUtils.get(RedisKeys.getServerConfigKey()); + if (cachedConfig != null) { + return cachedConfig; + } + } + + // 构建配置信息 + Map result = new HashMap<>(); + buildConfig(result); + + // 查询默认智能体 + AgentTemplateEntity agent = agentTemplateService.getDefaultTemplate(); + if (agent == null) { + throw new RenException("默认智能体未找到"); + } + + // 构建模块配置 + buildModuleConfig( + agent.getVadModelId(), + agent.getAsrModelId(), + agent.getLlmModelId(), + agent.getTtsModelId(), + agent.getMemModelId(), + agent.getIntentModelId(), + result); + + // 将配置存入Redis + redisUtils.set(RedisKeys.getServerConfigKey(), result); + + return result; + } + + @Override + public Map getAgentModels(String macAddress) { + // 根据MAC地址查找设备 + DeviceEntity device = deviceService.getDeviceByMacAddress(macAddress); + if (device == null) { + throw new RenException("设备未找到"); + } + // 获取智能体信息 + AgentEntity agent = agentService.getAgentById(device.getAgentId()); + if (agent == null) { + throw new RenException("智能体未找到"); + } + // 构建返回数据 + Map result = new HashMap<>(); + result.put("prompt", agent.getSystemPrompt()); + + // 构建模块配置 + buildModuleConfig( + agent.getVadModelId(), + agent.getAsrModelId(), + agent.getLlmModelId(), + agent.getTtsModelId(), + agent.getMemModelId(), + agent.getIntentModelId(), + result); + + return result; + } + + /** + * 构建配置信息 + * + * @param paramsList 系统参数列表 + * @return 配置信息 + */ + @SuppressWarnings("unchecked") + private Object buildConfig(Map config) { + + // 查询所有系统参数 + List paramsList = sysParamsService.list(new HashMap<>()); + + for (SysParamsDTO param : paramsList) { + String[] keys = param.getParamCode().split("\\."); + Map current = config; + + // 遍历除最后一个key之外的所有key + for (int i = 0; i < keys.length - 1; i++) { + String key = keys[i]; + if (!current.containsKey(key)) { + current.put(key, new HashMap()); + } + current = (Map) current.get(key); + } + + // 处理最后一个key + String lastKey = keys[keys.length - 1]; + String value = param.getParamValue(); + + // 根据valueType转换值 + switch (param.getValueType().toLowerCase()) { + case "number": + try { + current.put(lastKey, Double.parseDouble(value)); + } catch (NumberFormatException e) { + current.put(lastKey, value); + } + break; + case "boolean": + current.put(lastKey, Boolean.parseBoolean(value)); + break; + case "array": + // 将分号分隔的字符串转换为数字数组 + List list = new ArrayList<>(); + for (String num : value.split(";")) { + if (StringUtils.isNotBlank(num)) { + list.add(num.trim()); + } + } + current.put(lastKey, list); + break; + case "json": + try { + current.put(lastKey, JsonUtils.parseObject(value, Object.class)); + } catch (Exception e) { + current.put(lastKey, value); + } + break; + default: + current.put(lastKey, value); + } + } + + return config; + } + + /** + * 构建模块配置 + * + * @param vadModelId VAD模型ID + * @param asrModelId ASR模型ID + * @param llmModelId LLM模型ID + * @param ttsModelId TTS模型ID + * @param memModelId 记忆模型ID + * @param intentModelId 意图模型ID + * @param result 结果Map + */ + private void buildModuleConfig( + String vadModelId, + String asrModelId, + String llmModelId, + String ttsModelId, + String memModelId, + String intentModelId, + Map result) { + Map selectedModule = new HashMap<>(); + + String[] modelTypes = { "VAD", "ASR", "LLM", "TTS", "Memory", "Intent" }; + String[] modelIds = { vadModelId, asrModelId, llmModelId, ttsModelId, memModelId, intentModelId }; + + for (int i = 0; i < modelIds.length; i++) { + ModelConfigEntity model = modelConfigService.getModelById(modelIds[i]); + Map typeConfig = new HashMap<>(); + typeConfig.put(model.getModelCode(), model.getConfigJson()); + result.put(modelTypes[i], typeConfig); + selectedModule.put(modelTypes[i], model.getModelCode()); + } + result.put("selected_module", selectedModule); + } +} diff --git a/main/manager-api/src/main/java/xiaozhi/modules/sys/service/SysParamsService.java b/main/manager-api/src/main/java/xiaozhi/modules/sys/service/SysParamsService.java index e673d744b..e7a33a33e 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/sys/service/SysParamsService.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/sys/service/SysParamsService.java @@ -53,11 +53,4 @@ public interface SysParamsService extends BaseService { * 初始化服务器密钥 */ void initServerSecret(); - - /** - * 获取配置 - * - * @return 配置信息 - */ - Object getConfig(); } diff --git a/main/manager-api/src/main/java/xiaozhi/modules/sys/service/impl/SysParamsServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/sys/service/impl/SysParamsServiceImpl.java index 71282456b..011261566 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/sys/service/impl/SysParamsServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/sys/service/impl/SysParamsServiceImpl.java @@ -1,8 +1,6 @@ package xiaozhi.modules.sys.service.impl; -import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -198,69 +196,4 @@ public class SysParamsServiceImpl extends BaseServiceImpl wrapper = new QueryWrapper<>(); - wrapper.eq("param_type", 1); - List paramsList = baseDao.selectList(wrapper); - - // 构建配置信息 - Map config = new HashMap<>(); - - for (SysParamsEntity param : paramsList) { - String[] keys = param.getParamCode().split("\\."); - Map current = config; - - // 遍历除最后一个key之外的所有key - for (int i = 0; i < keys.length - 1; i++) { - String key = keys[i]; - if (!current.containsKey(key)) { - current.put(key, new HashMap()); - } - current = (Map) current.get(key); - } - - // 处理最后一个key - String lastKey = keys[keys.length - 1]; - String value = param.getParamValue(); - - // 根据valueType转换值 - switch (param.getValueType().toLowerCase()) { - case "number": - try { - current.put(lastKey, Double.parseDouble(value)); - } catch (NumberFormatException e) { - current.put(lastKey, value); - } - break; - case "boolean": - current.put(lastKey, Boolean.parseBoolean(value)); - break; - case "array": - // 将分号分隔的字符串转换为数字数组 - List list = new ArrayList<>(); - for (String num : value.split(";")) { - if (StringUtils.isNotBlank(num)) { - list.add(num.trim()); - } - } - current.put(lastKey, list); - break; - case "json": - try { - current.put(lastKey, JsonUtils.parseObject(value, Object.class)); - } catch (Exception e) { - current.put(lastKey, value); - } - break; - default: - current.put(lastKey, value); - } - } - - return config; - } } \ No newline at end of file