修复同步更新agent configs问题
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.labelsys.backend.service;
|
package com.labelsys.backend.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
@@ -311,17 +312,8 @@ public class SysConfigService {
|
|||||||
private void syncUpdateAgentConfigs(Long companyId, Long configId, SysConfig config) {
|
private void syncUpdateAgentConfigs(Long companyId, Long configId, SysConfig config) {
|
||||||
try {
|
try {
|
||||||
if (ConfigType.MODEL.name().equalsIgnoreCase(config.getConfigType())) {
|
if (ConfigType.MODEL.name().equalsIgnoreCase(config.getConfigType())) {
|
||||||
// 更新所有使用该模型配置的agent配置
|
// 从配置值中提取模型URL、加密的API密钥和LLM类型
|
||||||
LambdaQueryWrapper<AnnotationAgentConfig> modelWrapper =
|
String encryptedApiKey = null;
|
||||||
new LambdaQueryWrapper<>();
|
|
||||||
modelWrapper.eq(AnnotationAgentConfig::getCompanyId, companyId)
|
|
||||||
.eq(AnnotationAgentConfig::getModelConfigId, configId);
|
|
||||||
|
|
||||||
List<AnnotationAgentConfig> modelAgentConfigs =
|
|
||||||
agentConfigMapper.selectList(modelWrapper);
|
|
||||||
|
|
||||||
// 先解密获取原始API密钥,再通过setModelApiKey加密存储
|
|
||||||
String decryptedApiKey = null;
|
|
||||||
String modelUrl = null;
|
String modelUrl = null;
|
||||||
String llmType = null;
|
String llmType = null;
|
||||||
try {
|
try {
|
||||||
@@ -330,42 +322,37 @@ public class SysConfigService {
|
|||||||
if (llmConfig != null) {
|
if (llmConfig != null) {
|
||||||
modelUrl = llmConfig.getModelUrl();
|
modelUrl = llmConfig.getModelUrl();
|
||||||
llmType = llmConfig.getLlmType();
|
llmType = llmConfig.getLlmType();
|
||||||
// 解密API密钥,setModelApiKey会再次加密
|
// 直接获取加密后的API密钥,不进行解密
|
||||||
if (llmConfig.getApiKey() != null) {
|
if (llmConfig.getApiKey() != null) {
|
||||||
decryptedApiKey = SM4Util.decryptSafe(llmConfig.getApiKey());
|
encryptedApiKey = llmConfig.getApiKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("解析模型配置值失败,configId={}, error={}", configId, e.getMessage());
|
log.warn("解析模型配置值失败,configId={}, error={}", configId, e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (AnnotationAgentConfig agentConfig : modelAgentConfigs) {
|
// 使用UpdateWrapper批量更新,直接存储加密后的API密钥
|
||||||
agentConfig.setModelName(config.getConfigName());
|
UpdateWrapper<AnnotationAgentConfig> updateWrapper = new UpdateWrapper<>();
|
||||||
agentConfig.setModelUrl(modelUrl);
|
updateWrapper.eq("company_id", companyId)
|
||||||
agentConfig.setModelApiKey(decryptedApiKey); // setModelApiKey会加密存储
|
.eq("model_config_id", configId)
|
||||||
agentConfig.setLlmType(llmType);
|
.set("model_name", config.getConfigName())
|
||||||
agentConfig.setCreatedAt(LocalDateTime.now());
|
.set("model_url", modelUrl)
|
||||||
agentConfigMapper.updateById(agentConfig);
|
.set("model_api_key", encryptedApiKey) // 直接设置加密后的API密钥
|
||||||
}
|
.set("llm_type", llmType)
|
||||||
|
.set("created_at", LocalDateTime.now());
|
||||||
|
|
||||||
log.info("sync updated {} agent configs with modelConfigId={}", modelAgentConfigs.size(), configId);
|
int updatedCount = agentConfigMapper.update(null, updateWrapper);
|
||||||
|
log.info("sync updated {} agent configs with modelConfigId={}", updatedCount, configId);
|
||||||
} else if (ConfigType.PROMPT.name().equalsIgnoreCase(config.getConfigType())) {
|
} else if (ConfigType.PROMPT.name().equalsIgnoreCase(config.getConfigType())) {
|
||||||
// 更新所有使用该提示词配置的agent配置
|
// 更新所有使用该提示词配置的agent配置
|
||||||
LambdaQueryWrapper<AnnotationAgentConfig> promptWrapper =
|
UpdateWrapper<AnnotationAgentConfig> updateWrapper = new UpdateWrapper<>();
|
||||||
new LambdaQueryWrapper<>();
|
updateWrapper.eq("company_id", companyId)
|
||||||
promptWrapper.eq(AnnotationAgentConfig::getCompanyId, companyId)
|
.eq("prompt_config_id", configId)
|
||||||
.eq(AnnotationAgentConfig::getPromptConfigId, configId);
|
.set("prompt_text", config.getConfigValue())
|
||||||
|
.set("created_at", LocalDateTime.now());
|
||||||
|
|
||||||
List<AnnotationAgentConfig> promptAgentConfigs =
|
int updatedCount = agentConfigMapper.update(null, updateWrapper);
|
||||||
agentConfigMapper.selectList(promptWrapper);
|
log.info("sync updated {} agent configs with promptConfigId={}", updatedCount, configId);
|
||||||
|
|
||||||
for (AnnotationAgentConfig agentConfig : promptAgentConfigs) {
|
|
||||||
agentConfig.setPromptText(config.getConfigValue());
|
|
||||||
agentConfig.setCreatedAt(LocalDateTime.now());
|
|
||||||
agentConfigMapper.updateById(agentConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("sync updated {} agent configs with promptConfigId={}", promptAgentConfigs.size(), configId);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("syncUpdateAgentConfigs failed, configId={}, error={}", configId, e.getMessage(), e);
|
log.error("syncUpdateAgentConfigs failed, configId={}, error={}", configId, e.getMessage(), e);
|
||||||
|
|||||||
Reference in New Issue
Block a user