From 09be3a4de3c96cdf9752e1640ba38e51107284c5 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Fri, 11 Jul 2025 12:14:25 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E5=A3=B0=E7=BA=B9?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E6=9C=89=E7=BB=93=E6=9E=9C=E6=97=B6=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E7=9A=84=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/AgentChatHistoryServiceImpl.java | 54 +++++++++++++++---- .../src/components/ChatHistoryDialog.vue | 28 +++++++++- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatHistoryServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatHistoryServiceImpl.java index efcd5404..4486dc9b 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatHistoryServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatHistoryServiceImpl.java @@ -5,10 +5,10 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -17,6 +17,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import xiaozhi.common.constant.Constant; import xiaozhi.common.page.PageData; import xiaozhi.common.utils.ConvertUtils; +import xiaozhi.common.utils.JsonUtils; import xiaozhi.modules.agent.Enums.AgentChatHistoryType; import xiaozhi.modules.agent.dao.AiAgentChatHistoryDao; import xiaozhi.modules.agent.dto.AgentChatHistoryDTO; @@ -99,7 +100,7 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl wrapper = new LambdaQueryWrapper<>(); - wrapper.select(AgentChatHistoryEntity::getContent,AgentChatHistoryEntity::getAudioId) + wrapper.select(AgentChatHistoryEntity::getContent, AgentChatHistoryEntity::getAudioId) .eq(AgentChatHistoryEntity::getAgentId, agentId) .eq(AgentChatHistoryEntity::getChatType, AgentChatHistoryType.USER.getValue()) .isNotNull(AgentChatHistoryEntity::getAudioId); @@ -107,16 +108,51 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl pageParam = new Page<>(0, 50); IPage result = this.baseMapper.selectPage(pageParam, wrapper); - return result.getRecords().stream().map( item -> - ConvertUtils.sourceToTarget(item,AgentChatHistoryUserVO.class) - ).toList(); + return result.getRecords().stream().map(item -> { + AgentChatHistoryUserVO vo = ConvertUtils.sourceToTarget(item, AgentChatHistoryUserVO.class); + // 处理 content 字段,确保只返回聊天内容 + if (vo != null && vo.getContent() != null) { + vo.setContent(extractContentFromString(vo.getContent())); + } + return vo; + }).toList(); + } + + /** + * 从 content 字段中提取聊天内容 + * 如果 content 是 JSON 格式(如 {"speaker": "未知说话人", "content": "现在几点了。"}),则提取 content + * 字段 + * 如果 content 是普通字符串,则直接返回 + * + * @param content 原始内容 + * @return 提取的聊天内容 + */ + private String extractContentFromString(String content) { + if (content == null || content.trim().isEmpty()) { + return content; + } + + // 尝试解析为 JSON + try { + Map jsonMap = JsonUtils.parseObject(content, Map.class); + if (jsonMap != null && jsonMap.containsKey("content")) { + Object contentObj = jsonMap.get("content"); + return contentObj != null ? contentObj.toString() : content; + } + } catch (Exception e) { + // 如果不是有效的 JSON,直接返回原内容 + } + + // 如果不是 JSON 格式或没有 content 字段,直接返回原内容 + return content; } @Override public String getContentByAudioId(String audioId) { - AgentChatHistoryEntity agentChatHistoryEntity = baseMapper.selectOne(new LambdaQueryWrapper() - .select(AgentChatHistoryEntity::getContent) - .eq(AgentChatHistoryEntity::getAudioId, audioId)); + AgentChatHistoryEntity agentChatHistoryEntity = baseMapper + .selectOne(new LambdaQueryWrapper() + .select(AgentChatHistoryEntity::getContent) + .eq(AgentChatHistoryEntity::getAudioId, audioId)); return agentChatHistoryEntity == null ? null : agentChatHistoryEntity.getContent(); } @@ -126,6 +162,6 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl() .eq(AgentChatHistoryEntity::getAudioId, audioId) .eq(AgentChatHistoryEntity::getAgentId, agentId)); - return row == 1 ; + return row == 1; } } diff --git a/main/manager-web/src/components/ChatHistoryDialog.vue b/main/manager-web/src/components/ChatHistoryDialog.vue index 33350208..a784d0b8 100644 --- a/main/manager-web/src/components/ChatHistoryDialog.vue +++ b/main/manager-web/src/components/ChatHistoryDialog.vue @@ -24,7 +24,7 @@
- {{ message.content }} + {{ extractContentFromString(message.content) }}
@@ -129,6 +129,32 @@ export default { } }, methods: { + /** + * 从 content 字段中提取聊天内容 + * 如果 content 是 JSON 格式(如 {"speaker": "未知说话人", "content": "现在几点了。"}),则提取 content 字段 + * 如果 content 是普通字符串,则直接返回 + * + * @param {string} content 原始内容 + * @returns {string} 提取的聊天内容 + */ + extractContentFromString(content) { + if (!content || content.trim() === '') { + return content; + } + + // 尝试解析为 JSON + try { + const jsonObj = JSON.parse(content); + if (jsonObj && typeof jsonObj === 'object' && jsonObj.content) { + return jsonObj.content; + } + } catch (e) { + // 如果不是有效的 JSON,直接返回原内容 + } + + // 如果不是 JSON 格式或没有 content 字段,直接返回原内容 + return content; + }, resetData() { this.sessions = []; this.messages = [];