update:保存智能体配置时,删除对应的历史记录

This commit is contained in:
hrz
2025-05-12 16:31:06 +08:00
parent 4a0bdbf779
commit ac7b02d28a
7 changed files with 48 additions and 10 deletions
@@ -111,6 +111,11 @@ public interface Constant {
*/ */
String FILE_EXTENSION_SEG = "."; String FILE_EXTENSION_SEG = ".";
/**
* 无记忆
*/
String MEMORY_NO_MEM = "Memory_nomem";
enum SysBaseParam { enum SysBaseParam {
/** /**
* 系统全称 * 系统全称
@@ -187,6 +187,15 @@ public class AgentController {
agentService.updateById(existingEntity); agentService.updateById(existingEntity);
// 更新记忆策略
if (existingEntity.getMemModelId() == null || existingEntity.getMemModelId().equals(Constant.MEMORY_NO_MEM)) {
// 删除所有记录
agentChatHistoryService.deleteByAgentId(existingEntity.getId(), true, true);
} else if (existingEntity.getChatHistoryConf() != null && existingEntity.getChatHistoryConf() == 1) {
// 删除音频数据
agentChatHistoryService.deleteByAgentId(existingEntity.getId(), true, false);
}
return new Result<>(); return new Result<>();
} }
@@ -197,7 +206,7 @@ public class AgentController {
// 先删除关联的设备 // 先删除关联的设备
deviceService.deleteByAgentId(id); deviceService.deleteByAgentId(id);
// 删除关联的聊天记录 // 删除关联的聊天记录
agentChatHistoryService.deleteByAgentId(id); agentChatHistoryService.deleteByAgentId(id, true, true);
// 再删除智能体 // 再删除智能体
agentService.deleteById(id); agentService.deleteById(id);
return new Result<>(); return new Result<>();
@@ -28,4 +28,11 @@ public interface AiAgentChatHistoryDao extends BaseMapper<AgentChatHistoryEntity
* @param agentId 智能体ID * @param agentId 智能体ID
*/ */
void deleteHistoryByAgentId(String agentId); void deleteHistoryByAgentId(String agentId);
/**
* 根据智能体ID删除音频ID
*
* @param agentId 智能体ID
*/
void deleteAudioIdByAgentId(String agentId);
} }
@@ -39,7 +39,9 @@ public interface AgentChatHistoryService extends IService<AgentChatHistoryEntity
/** /**
* 根据智能体ID删除聊天记录 * 根据智能体ID删除聊天记录
* *
* @param agentId 智能体ID * @param agentId 智能体ID
* @param deleteAudio 是否删除音频
* @param deleteText 是否删除文本
*/ */
void deleteByAgentId(String agentId); void deleteByAgentId(String agentId, Boolean deleteAudio, Boolean deleteText);
} }
@@ -78,8 +78,16 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl<AiAgentChatHistoryD
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void deleteByAgentId(String agentId) { public void deleteByAgentId(String agentId, Boolean deleteAudio, Boolean deleteText) {
baseMapper.deleteAudioByAgentId(agentId); if (deleteAudio) {
baseMapper.deleteHistoryByAgentId(agentId); baseMapper.deleteAudioByAgentId(agentId);
}
if (deleteAudio && !deleteText) {
baseMapper.deleteAudioIdByAgentId(agentId);
}
if (deleteText) {
baseMapper.deleteHistoryByAgentId(agentId);
}
} }
} }
@@ -48,9 +48,10 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
@Override @Override
public AgentEntity getAgentById(String id) { public AgentEntity getAgentById(String id) {
AgentEntity agent = agentDao.selectById(id); AgentEntity agent = agentDao.selectById(id);
if (agent != null && agent.getMemModelId() != null && agent.getMemModelId().equals("Memory_nomem")) { if (agent != null && agent.getMemModelId() != null && agent.getMemModelId().equals(Constant.MEMORY_NO_MEM)) {
agent.setChatHistoryConf(Constant.ChatHistoryConfEnum.IGNORE.getCode()); agent.setChatHistoryConf(Constant.ChatHistoryConfEnum.IGNORE.getCode());
} else if (agent != null && agent.getMemModelId() != null && !agent.getMemModelId().equals("Memory_nomem") } else if (agent != null && agent.getMemModelId() != null
&& !agent.getMemModelId().equals(Constant.MEMORY_NO_MEM)
&& agent.getChatHistoryConf() == null) { && agent.getChatHistoryConf() == null) {
agent.setChatHistoryConf(Constant.ChatHistoryConfEnum.RECORD_TEXT_AUDIO.getCode()); agent.setChatHistoryConf(Constant.ChatHistoryConfEnum.RECORD_TEXT_AUDIO.getCode());
} }
@@ -28,11 +28,17 @@
SELECT audio_id SELECT audio_id
FROM ai_agent_chat_history FROM ai_agent_chat_history
WHERE agent_id = #{agentId} WHERE agent_id = #{agentId}
); )
</delete> </delete>
<update id="deleteAudioIdByAgentId">
UPDATE ai_agent_chat_history
SET audio_id = NULL
WHERE agent_id = #{agentId}
</update>
<delete id="deleteHistoryByAgentId"> <delete id="deleteHistoryByAgentId">
DELETE FROM ai_agent_chat_history DELETE FROM ai_agent_chat_history
WHERE agent_id = #{agentId}; WHERE agent_id = #{agentId}
</delete> </delete>
</mapper> </mapper>