|
|
@@ -7,6 +7,7 @@ import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
import org.dromara.talk.domain.bo.PhoneUserBo;
|
|
|
import org.dromara.talk.domain.bo.TalkAgentBo;
|
|
|
import org.dromara.talk.domain.bo.TalkSessionBo;
|
|
|
+import org.dromara.talk.domain.dto.MessageStreamRequest;
|
|
|
import org.dromara.talk.domain.vo.TalkAgentVo;
|
|
|
import org.dromara.talk.domain.vo.TalkSessionVo;
|
|
|
import org.dromara.talk.service.IChatService;
|
|
|
@@ -18,8 +19,6 @@ import org.dromara.talk.service.ITtsService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
-import java.util.Date;
|
|
|
-
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
@@ -85,9 +84,7 @@ public class ChatServiceImpl implements IChatService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void processMessageStream(String userMessage, Long agentId, String agentGender,
|
|
|
- List<Map<String, String>> ttsVcnList, String conversationId,
|
|
|
- Boolean isGreeting, Integer requestId, String customerPhone, Integer type, SseEmitter emitter) {
|
|
|
+ public void processMessageStream(MessageStreamRequest request, SseEmitter emitter) {
|
|
|
// 在主线程中获取用户ID,避免在异步线程中访问ThreadLocal
|
|
|
Long userId = LoginHelper.getUserId();
|
|
|
if (userId == null) {
|
|
|
@@ -96,9 +93,9 @@ public class ChatServiceImpl implements IChatService {
|
|
|
}
|
|
|
|
|
|
// 更新最新请求ID
|
|
|
- if (requestId != null) {
|
|
|
- latestRequestIdMap.put(userId, requestId);
|
|
|
- log.info("流式请求 - 更新用户 {} 的最新请求ID为: {}", userId, requestId);
|
|
|
+ if (request.getRequestId() != null) {
|
|
|
+ latestRequestIdMap.put(userId, request.getRequestId());
|
|
|
+ log.info("流式请求 - 更新用户 {} 的最新请求ID为: {}", userId, request.getRequestId());
|
|
|
}
|
|
|
|
|
|
Long finalUserId = userId;
|
|
|
@@ -107,14 +104,14 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
|
// 获取客服配置
|
|
|
TalkAgentVo agentConfig = null;
|
|
|
- if (agentId != null) {
|
|
|
- agentConfig = talkAgentService.queryById(agentId);
|
|
|
+ if (request.getAgentId() != null) {
|
|
|
+ agentConfig = talkAgentService.queryById(request.getAgentId());
|
|
|
}
|
|
|
|
|
|
// 如果是欢迎语,合成语音但不发送text事件(避免前端重复显示)
|
|
|
- if (Boolean.TRUE.equals(isGreeting)) {
|
|
|
+ if (Boolean.TRUE.equals(request.getIsGreeting())) {
|
|
|
// 合成欢迎语语音
|
|
|
- synthesizeAndSendAudio(userMessage, agentConfig, emitter);
|
|
|
+ synthesizeAndSendAudio(request.getMessage(), agentConfig, emitter);
|
|
|
|
|
|
// 发送完成信号
|
|
|
Map<String, String> doneEvent = new HashMap<>();
|
|
|
@@ -127,10 +124,10 @@ public class ChatServiceImpl implements IChatService {
|
|
|
}
|
|
|
|
|
|
TalkAgentVo finalAgentConfig = agentConfig;
|
|
|
- Integer finalRequestId = requestId;
|
|
|
+ Integer finalRequestId = request.getRequestId();
|
|
|
|
|
|
// 初始化对话记录列表
|
|
|
- String finalConversationId = conversationId;
|
|
|
+ String finalConversationId = request.getConversationId();
|
|
|
if (finalConversationId != null) {
|
|
|
// 使用 computeIfAbsent 避免竞态条件
|
|
|
conversationMap.computeIfAbsent(finalConversationId, k -> new ArrayList<>());
|
|
|
@@ -138,7 +135,7 @@ public class ChatServiceImpl implements IChatService {
|
|
|
// 添加用户消息(客户)
|
|
|
Map<String, String> userMsg = new HashMap<>();
|
|
|
userMsg.put("role", "user");
|
|
|
- userMsg.put("content", userMessage);
|
|
|
+ userMsg.put("content", request.getMessage());
|
|
|
userMsg.put("timestamp", String.valueOf(System.currentTimeMillis()));
|
|
|
conversationMap.get(finalConversationId).add(userMsg);
|
|
|
}
|
|
|
@@ -148,10 +145,10 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
|
// 检查 conversationId 是否是临时 sessionId
|
|
|
// 如果是临时 sessionId(数据库中还未被 Dify 更新),则传递 null 给 Dify
|
|
|
- String difyConversationId = conversationId;
|
|
|
- if (conversationId != null) {
|
|
|
- TalkSessionVo session = talkSessionService.queryBySessionId(conversationId);
|
|
|
- if (session != null && conversationId.equals(session.getSessionId())) {
|
|
|
+ String difyConversationId = request.getConversationId();
|
|
|
+ if (request.getConversationId() != null) {
|
|
|
+ TalkSessionVo session = talkSessionService.queryBySessionId(request.getConversationId());
|
|
|
+ if (session != null && request.getConversationId().equals(session.getSessionId())) {
|
|
|
// 检查这个 sessionId 是否是刚创建的(没有对话内容)
|
|
|
// 如果没有对话内容,说明这是第一次对话,不应该传递给 Dify
|
|
|
if (session.getConversationJson() == null || session.getConversationJson().isEmpty()) {
|
|
|
@@ -161,7 +158,7 @@ public class ChatServiceImpl implements IChatService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- difyService.callWorkflowStream(userMessage, agentGender, ttsVcnList, agentConfig, finalUserId, difyConversationId, customerPhone, type,
|
|
|
+ difyService.callWorkflowStream(request.getMessage(), request.getAgentGender(), request.getTtsVcnList(), agentConfig, finalUserId, difyConversationId, request.getCustomerPhone(), request.getType(),
|
|
|
(textChunk) -> {
|
|
|
try {
|
|
|
// 累积AI回复文本
|
|
|
@@ -199,7 +196,7 @@ public class ChatServiceImpl implements IChatService {
|
|
|
// 添加用户消息(客户)- 每次对话都要添加
|
|
|
Map<String, String> userMsg = new HashMap<>();
|
|
|
userMsg.put("role", "user");
|
|
|
- userMsg.put("content", userMessage);
|
|
|
+ userMsg.put("content", request.getMessage());
|
|
|
userMsg.put("timestamp", String.valueOf(System.currentTimeMillis()));
|
|
|
conversationMap.get(newConversationId).add(userMsg);
|
|
|
|