mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
update:优化声纹识别有结果时内容的展示
This commit is contained in:
+45
-9
@@ -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<AiAgentChatHistoryD
|
||||
// 构建查询条件(不添加按照创建时间排序,数据本来就是主键越大创建时间越大
|
||||
// 不添加这样可以减少排序全部数据在分页的全盘扫描消耗)
|
||||
LambdaQueryWrapper<AgentChatHistoryEntity> 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<AiAgentChatHistoryD
|
||||
// 构建分页查询,查询前50页数据
|
||||
Page<AgentChatHistoryEntity> pageParam = new Page<>(0, 50);
|
||||
IPage<AgentChatHistoryEntity> 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<String, Object> 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<AgentChatHistoryEntity>()
|
||||
.select(AgentChatHistoryEntity::getContent)
|
||||
.eq(AgentChatHistoryEntity::getAudioId, audioId));
|
||||
AgentChatHistoryEntity agentChatHistoryEntity = baseMapper
|
||||
.selectOne(new LambdaQueryWrapper<AgentChatHistoryEntity>()
|
||||
.select(AgentChatHistoryEntity::getContent)
|
||||
.eq(AgentChatHistoryEntity::getAudioId, audioId));
|
||||
return agentChatHistoryEntity == null ? null : agentChatHistoryEntity.getContent();
|
||||
}
|
||||
|
||||
@@ -126,6 +162,6 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl<AiAgentChatHistoryD
|
||||
Long row = baseMapper.selectCount(new LambdaQueryWrapper<AgentChatHistoryEntity>()
|
||||
.eq(AgentChatHistoryEntity::getAudioId, audioId)
|
||||
.eq(AgentChatHistoryEntity::getAgentId, agentId));
|
||||
return row == 1 ;
|
||||
return row == 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
<img :src="message.chatType === 1 ? getUserAvatar(currentSessionId) : require('@/assets/xiaozhi-logo.png')"
|
||||
class="avatar" />
|
||||
<div class="message-content">
|
||||
{{ message.content }}
|
||||
{{ extractContentFromString(message.content) }}
|
||||
<i v-if="message.audioId" :class="getAudioIconClass(message)"
|
||||
@click="playAudio(message)" class="audio-icon"></i>
|
||||
</div>
|
||||
@@ -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 = [];
|
||||
|
||||
Reference in New Issue
Block a user