|
@@ -17,12 +17,16 @@ import org.dromara.talk.mapper.WhatsAppHistoryMapper;
|
|
|
import org.dromara.talk.service.IWhatsAppHistoryService;
|
|
import org.dromara.talk.service.IWhatsAppHistoryService;
|
|
|
import org.dromara.talk.service.IWhatsAppUserService;
|
|
import org.dromara.talk.service.IWhatsAppUserService;
|
|
|
import org.dromara.talk.domain.bo.WhatsAppUserBo;
|
|
import org.dromara.talk.domain.bo.WhatsAppUserBo;
|
|
|
-import org.dromara.talk.domain.dto.WhatsAppMessageRequest;
|
|
|
|
|
|
|
+import org.dromara.talk.domain.dto.ConversationMessageRequest;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import com.alibaba.fastjson2.TypeReference;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* WhatsApp历史记录Service业务层处理
|
|
* WhatsApp历史记录Service业务层处理
|
|
@@ -81,7 +85,6 @@ public class WhatsAppHistoryServiceImpl implements IWhatsAppHistoryService {
|
|
|
lqw.eq(StringUtils.isNotBlank(bo.getSessionId()), WhatsAppHistory::getSessionId, bo.getSessionId());
|
|
lqw.eq(StringUtils.isNotBlank(bo.getSessionId()), WhatsAppHistory::getSessionId, bo.getSessionId());
|
|
|
lqw.eq(StringUtils.isNotBlank(bo.getUserAccount()), WhatsAppHistory::getUserAccount, bo.getUserAccount());
|
|
lqw.eq(StringUtils.isNotBlank(bo.getUserAccount()), WhatsAppHistory::getUserAccount, bo.getUserAccount());
|
|
|
lqw.eq(bo.getStartTime() != null, WhatsAppHistory::getStartTime, bo.getStartTime());
|
|
lqw.eq(bo.getStartTime() != null, WhatsAppHistory::getStartTime, bo.getStartTime());
|
|
|
- lqw.eq(bo.getEndTime() != null, WhatsAppHistory::getEndTime, bo.getEndTime());
|
|
|
|
|
return lqw;
|
|
return lqw;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -133,28 +136,86 @@ public class WhatsAppHistoryServiceImpl implements IWhatsAppHistoryService {
|
|
|
* @return 是否保存成功
|
|
* @return 是否保存成功
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- public Boolean receiveMessage(WhatsAppMessageRequest request) {
|
|
|
|
|
- log.info("接收到WhatsApp消息 - sessionId: {}, userAccount: {}",
|
|
|
|
|
- request.getSessionId(), request.getUserAccount());
|
|
|
|
|
|
|
+ public Boolean receiveConversationMessage(ConversationMessageRequest request) {
|
|
|
|
|
+ log.info("接收到对话消息 - conversationId: {}, userId: {}, round: {}",
|
|
|
|
|
+ request.getConversationId(), request.getUserId(), request.getRound());
|
|
|
|
|
|
|
|
// 1. 检查并保存用户
|
|
// 1. 检查并保存用户
|
|
|
WhatsAppUserBo userBo = new WhatsAppUserBo();
|
|
WhatsAppUserBo userBo = new WhatsAppUserBo();
|
|
|
- userBo.setUserAccount(request.getUserAccount());
|
|
|
|
|
|
|
+ userBo.setUserAccount(request.getUserId());
|
|
|
try {
|
|
try {
|
|
|
whatsAppUserService.insertByBo(userBo);
|
|
whatsAppUserService.insertByBo(userBo);
|
|
|
- log.info("新用户已保存: {}", request.getUserAccount());
|
|
|
|
|
|
|
+ log.info("新用户已保存: {}", request.getUserId());
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- // 用户可能已存在,忽略错误
|
|
|
|
|
- log.debug("用户可能已存在: {}", request.getUserAccount());
|
|
|
|
|
|
|
+ log.debug("用户可能已存在: {}", request.getUserId());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 2. 保存对话历史
|
|
|
|
|
- WhatsAppHistoryBo historyBo = new WhatsAppHistoryBo();
|
|
|
|
|
- historyBo.setSessionId(request.getSessionId());
|
|
|
|
|
- historyBo.setUserAccount(request.getUserAccount());
|
|
|
|
|
- historyBo.setConversationJson(JSON.toJSONString(request.getConversationHistory()));
|
|
|
|
|
- historyBo.setStartTime(new Date());
|
|
|
|
|
|
|
+ // 2. 查询是否已存在该会话的记录
|
|
|
|
|
+ WhatsAppHistoryBo queryBo = new WhatsAppHistoryBo();
|
|
|
|
|
+ queryBo.setSessionId(request.getConversationId());
|
|
|
|
|
+ List<WhatsAppHistoryVo> existingRecords = queryList(queryBo);
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, String>> conversationHistory;
|
|
|
|
|
+ WhatsAppHistoryBo historyBo;
|
|
|
|
|
+
|
|
|
|
|
+ if (!existingRecords.isEmpty()) {
|
|
|
|
|
+ // 已存在记录,读取并追加
|
|
|
|
|
+ WhatsAppHistoryVo existing = existingRecords.get(0);
|
|
|
|
|
+ String existingJson = existing.getConversationJson();
|
|
|
|
|
+ conversationHistory = JSON.parseObject(existingJson, new TypeReference<List<Map<String, String>>>() {});
|
|
|
|
|
+
|
|
|
|
|
+ historyBo = new WhatsAppHistoryBo();
|
|
|
|
|
+ historyBo.setId(existing.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 新会话,创建新记录
|
|
|
|
|
+ conversationHistory = new ArrayList<>();
|
|
|
|
|
+ historyBo = new WhatsAppHistoryBo();
|
|
|
|
|
+ historyBo.setSessionId(request.getConversationId());
|
|
|
|
|
+ historyBo.setUserAccount(request.getUserId());
|
|
|
|
|
+ historyBo.setStartTime(new Date());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return insertByBo(historyBo);
|
|
|
|
|
|
|
+ // 3. 追加新的对话轮次(user消息 + assistant消息)
|
|
|
|
|
+ Map<String, String> userMessage = new HashMap<>();
|
|
|
|
|
+ userMessage.put("role", "user");
|
|
|
|
|
+ userMessage.put("content", request.getQuery());
|
|
|
|
|
+ userMessage.put("timestamp", request.getTimestamp());
|
|
|
|
|
+ userMessage.put("round", String.valueOf(request.getRound()));
|
|
|
|
|
+ conversationHistory.add(userMessage);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> assistantMessage = new HashMap<>();
|
|
|
|
|
+ assistantMessage.put("role", "assistant");
|
|
|
|
|
+ assistantMessage.put("content", request.getAgent());
|
|
|
|
|
+ assistantMessage.put("timestamp", request.getTimestamp());
|
|
|
|
|
+ assistantMessage.put("round", String.valueOf(request.getRound()));
|
|
|
|
|
+ conversationHistory.add(assistantMessage);
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 按round排序,同一轮内user在assistant之前
|
|
|
|
|
+ conversationHistory.sort((m1, m2) -> {
|
|
|
|
|
+ int round1 = Integer.parseInt(m1.getOrDefault("round", "0"));
|
|
|
|
|
+ int round2 = Integer.parseInt(m2.getOrDefault("round", "0"));
|
|
|
|
|
+ int roundCompare = Integer.compare(round1, round2);
|
|
|
|
|
+ if (roundCompare != 0) {
|
|
|
|
|
+ return roundCompare;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 同一轮内,user 在 assistant 之前
|
|
|
|
|
+ String role1 = m1.getOrDefault("role", "");
|
|
|
|
|
+ String role2 = m2.getOrDefault("role", "");
|
|
|
|
|
+ if ("user".equals(role1) && "assistant".equals(role2)) {
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ } else if ("assistant".equals(role1) && "user".equals(role2)) {
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 保存或更新记录
|
|
|
|
|
+ historyBo.setConversationJson(JSON.toJSONString(conversationHistory));
|
|
|
|
|
+
|
|
|
|
|
+ if (historyBo.getId() != null) {
|
|
|
|
|
+ return updateByBo(historyBo);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return insertByBo(historyBo);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|