update:优化配置读取方式

This commit is contained in:
hrz 2025-04-11 13:19:17 +08:00
parent 323fcaf120
commit f26cf43f67
9 changed files with 239 additions and 135 deletions

View file

@ -62,4 +62,10 @@ public class RedisKeys {
return "agent:device:count:" + id;
}
/**
* 获取系统配置缓存key
*/
public static String getServerConfigKey() {
return "server:config";
}
}

View file

@ -42,12 +42,4 @@ public interface AgentService extends BaseService<AgentEntity> {
* @return 设备数量
*/
Integer getDeviceCountByAgentId(String agentId);
/**
* 根据设备MAC地址获取智能体模型配置
*
* @param macAddress 设备MAC地址
* @return 模型配置信息
*/
Map<String, Object> getAgentModelsByMac(String macAddress);
}

View file

@ -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<AgentDao, AgentEntity> 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<AgentDao, AgentEntity> imp
return deviceCount != null ? deviceCount : 0;
}
@Override
public Map<String, Object> 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<String, String> selectedModule = new HashMap<>();
// 构建返回数据
Map<String, Object> 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<String, Object> 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;
}
}

View file

@ -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<Object>().ok(config);
}
@ -49,7 +47,7 @@ public class ConfigController {
// 效验数据
ValidatorUtils.validateEntity(dto);
checkSecret(dto.getSecret());
Map<String, Object> models = agentService.getAgentModelsByMac(dto.getMacAddress());
Object models = configService.getAgentModels(dto.getMacAddress());
return new Result<Object>().ok(models);
}

View file

@ -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);
}
}

View file

@ -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<String, Object> getAgentModels(String macAddress);
}

View file

@ -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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> config) {
// 查询所有系统参数
List<SysParamsDTO> paramsList = sysParamsService.list(new HashMap<>());
for (SysParamsDTO param : paramsList) {
String[] keys = param.getParamCode().split("\\.");
Map<String, Object> 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<String, Object>());
}
current = (Map<String, Object>) 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<String> 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<String, Object> result) {
Map<String, String> 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<String, Object> 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);
}
}

View file

@ -53,11 +53,4 @@ public interface SysParamsService extends BaseService<SysParamsEntity> {
* 初始化服务器密钥
*/
void initServerSecret();
/**
* 获取配置
*
* @return 配置信息
*/
Object getConfig();
}

View file

@ -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<SysParamsDao, SysParam
updateValueByCode(Constant.SERVER_SECRET, newSecret);
}
}
@Override
@SuppressWarnings("unchecked")
public Object getConfig() {
// 查询所有系统参数
QueryWrapper<SysParamsEntity> wrapper = new QueryWrapper<>();
wrapper.eq("param_type", 1);
List<SysParamsEntity> paramsList = baseDao.selectList(wrapper);
// 构建配置信息
Map<String, Object> config = new HashMap<>();
for (SysParamsEntity param : paramsList) {
String[] keys = param.getParamCode().split("\\.");
Map<String, Object> 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<String, Object>());
}
current = (Map<String, Object>) 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<String> 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;
}
}