|
|
@@ -1,9 +1,11 @@
|
|
|
package org.dromara.talk.service.impl;
|
|
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.dromara.talk.domain.vo.TalkAgentVo;
|
|
|
import org.dromara.talk.service.IChatService;
|
|
|
+import org.dromara.talk.service.IDifyService;
|
|
|
import org.dromara.talk.service.ITalkAgentService;
|
|
|
import org.dromara.talk.service.ITtsService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -21,10 +23,11 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
|
private final ITtsService ttsService;
|
|
|
private final ITalkAgentService talkAgentService;
|
|
|
+ private final IDifyService difyService;
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> processMessage(String userMessage, Long agentId, String agentGender, List<Map<String, String>> ttsVcnList) {
|
|
|
- log.info("处理用户消息: {}, 客服ID: {}, 客服性别: {}", userMessage, agentId, agentGender);
|
|
|
+ public Map<String, Object> processMessage(String userMessage, Long agentId, String agentGender, List<Map<String, String>> ttsVcnList, String conversationId, Boolean isGreeting) {
|
|
|
+ log.info("处理用户消息: {}, 客服ID: {}, 客服性别: {}, 对话ID: {}, 是否欢迎语: {}", userMessage, agentId, agentGender, conversationId, isGreeting);
|
|
|
|
|
|
// 获取客服配置
|
|
|
TalkAgentVo agentConfig = null;
|
|
|
@@ -32,14 +35,33 @@ public class ChatServiceImpl implements IChatService {
|
|
|
agentConfig = talkAgentService.queryById(agentId);
|
|
|
}
|
|
|
|
|
|
- // 生成回复(预留AI接口,返回包含发言人和文本的JSON)
|
|
|
- Map<String, String> aiResult = generateReply(userMessage, agentGender, ttsVcnList, agentConfig);
|
|
|
- String reply = aiResult.get("replyText");
|
|
|
- String selectedVcn = aiResult.get("ttsVcn");
|
|
|
+ String reply;
|
|
|
+ String newConversationId = conversationId;
|
|
|
+
|
|
|
+ // 如果是欢迎语,直接使用消息文本,不调用 Dify 工作流
|
|
|
+ if (Boolean.TRUE.equals(isGreeting)) {
|
|
|
+ log.info("处理欢迎语,跳过 Dify 工作流");
|
|
|
+ reply = userMessage;
|
|
|
+ } else {
|
|
|
+ // 获取当前登录用户ID
|
|
|
+ Long userId = null;
|
|
|
+ try {
|
|
|
+ userId = StpUtil.getLoginIdAsLong();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("获取登录用户ID失败,使用默认值", e);
|
|
|
+ userId = 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 调用 Dify 生成回复
|
|
|
+ Map<String, String> aiResult = generateReply(userMessage, agentGender, ttsVcnList, agentConfig, userId, conversationId);
|
|
|
+ reply = aiResult.get("replyText");
|
|
|
+ String selectedVcn = aiResult.get("ttsVcn");
|
|
|
+ newConversationId = aiResult.get("conversationId");
|
|
|
|
|
|
- // 如果AI选择了发言人,更新客服配置
|
|
|
- if (selectedVcn != null && agentConfig != null) {
|
|
|
- agentConfig.setTtsVcn(selectedVcn);
|
|
|
+ // 如果AI选择了发言人,更新客服配置
|
|
|
+ if (selectedVcn != null && agentConfig != null) {
|
|
|
+ agentConfig.setTtsVcn(selectedVcn);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 合成语音(传递客服配置)
|
|
|
@@ -50,6 +72,9 @@ public class ChatServiceImpl implements IChatService {
|
|
|
response.put("reply", reply);
|
|
|
response.put("audio", audioBase64);
|
|
|
response.put("timestamp", System.currentTimeMillis());
|
|
|
+ if (newConversationId != null) {
|
|
|
+ response.put("conversationId", newConversationId);
|
|
|
+ }
|
|
|
|
|
|
log.info("消息处理完成: reply长度={}, audio长度={}",
|
|
|
reply != null ? reply.length() : 0,
|
|
|
@@ -58,25 +83,23 @@ public class ChatServiceImpl implements IChatService {
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
- private Map<String, String> generateReply(String userMessage, String agentGender, List<Map<String, String>> ttsVcnList, TalkAgentVo agentConfig) {
|
|
|
- // 组装发送给AI的JSON数据
|
|
|
- Map<String, Object> aiRequest = new HashMap<>();
|
|
|
- aiRequest.put("userMessage", userMessage);
|
|
|
- aiRequest.put("agentGender", agentGender);
|
|
|
- aiRequest.put("ttsVcnList", ttsVcnList);
|
|
|
- aiRequest.put("currentVcn", agentConfig != null ? agentConfig.getTtsVcn() : null);
|
|
|
-
|
|
|
- log.info("AI接口预留 - 请求数据: {}", aiRequest);
|
|
|
+ private Map<String, String> generateReply(String userMessage, String agentGender,
|
|
|
+ List<Map<String, String>> ttsVcnList,
|
|
|
+ TalkAgentVo agentConfig,
|
|
|
+ Long userId,
|
|
|
+ String conversationId) {
|
|
|
+ // 组装发送给 Dify 的数据
|
|
|
+ Map<String, Object> inputs = new HashMap<>();
|
|
|
+ inputs.put("agentGender", agentGender);
|
|
|
+ inputs.put("ttsVcnList", ttsVcnList);
|
|
|
+ inputs.put("currentVcn", agentConfig != null ? agentConfig.getTtsVcn() : null);
|
|
|
|
|
|
- // TODO: 后续接入AI,发送 aiRequest 给AI服务
|
|
|
- // AI应返回格式: {"ttsVcn": "选择的发言人", "replyText": "回复文本"}
|
|
|
+ log.info("调用 Dify 工作流 - 用户ID: {}, 对话ID: {}", userId, conversationId);
|
|
|
|
|
|
- // 暂时返回默认值
|
|
|
- Map<String, String> aiResponse = new HashMap<>();
|
|
|
- aiResponse.put("ttsVcn", agentConfig != null ? agentConfig.getTtsVcn() : "x4_yezi");
|
|
|
- aiResponse.put("replyText", userMessage);
|
|
|
+ // 调用 Dify 工作流
|
|
|
+ Map<String, String> aiResponse = difyService.callWorkflow(userMessage, inputs, userId, conversationId);
|
|
|
|
|
|
- log.info("AI接口预留 - 响应数据: {}", aiResponse);
|
|
|
+ log.info("Dify 工作流响应: {}", aiResponse);
|
|
|
|
|
|
return aiResponse;
|
|
|
}
|