|
@@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.infra.api.ai.AiApi;
|
|
|
import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
|
|
|
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
|
|
|
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
|
|
|
+import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationRespVO;
|
|
|
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageListReqVO;
|
|
|
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageRespVO;
|
|
|
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO;
|
|
@@ -113,11 +114,7 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
|
|
|
public Long sendMessage(AppKeFuMessageSendReqVO sendReqVO) {
|
|
|
// 1.1 设置会话编号
|
|
|
KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class);
|
|
|
- if (ObjUtil.isNull(kefuMessage.getConversationId())) {
|
|
|
- KeFuConversationDO conversation = conversationService.getOrCreateConversation(sendReqVO.getSenderId(),
|
|
|
- sendReqVO.getRelUserId());
|
|
|
- kefuMessage.setConversationId(conversation.getId());
|
|
|
- }
|
|
|
+
|
|
|
kefuMessage.setReceiverId(sendReqVO.getRelUserId());
|
|
|
// 1.2 保存消息
|
|
|
keFuMessageMapper.insert(kefuMessage);
|
|
@@ -140,18 +137,38 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
|
|
|
// 1.1 设置会话编号
|
|
|
String apiKey = DictFrameworkUtils.parseDictDataValue("ai_key", "多轮对话");
|
|
|
Map<String, Object> inputs = new HashMap<>();
|
|
|
- inputs.put("type", "活林木");
|
|
|
+ if (sendReqVO.getRelUserId() == 1L) {
|
|
|
+ inputs.put("type", "系统客服");
|
|
|
+ } else if (sendReqVO.getRelUserId() == 2L) {
|
|
|
+ inputs.put("type", "贷款专家");
|
|
|
+ } else if (sendReqVO.getRelUserId() == 3L) {
|
|
|
+ inputs.put("type", "律师专家");
|
|
|
+ } else {
|
|
|
+ inputs.put("type", "其他");
|
|
|
+ }
|
|
|
+ inputs.put("token", sendReqVO.getToken());
|
|
|
+ String conversationId;
|
|
|
+ KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class);
|
|
|
+ if (ObjUtil.isNull(kefuMessage.getConversationId())) {
|
|
|
+ KeFuConversationDO conversation = conversationService.getOrCreateConversation(sendReqVO.getSenderId(),
|
|
|
+ sendReqVO.getRelUserId());
|
|
|
+ kefuMessage.setConversationId(conversation.getId());
|
|
|
+ sendReqVO.setConversationId(conversation.getId());
|
|
|
+ conversationId = conversation.getDifyConversationId();
|
|
|
+ } else {conversationId = "";}
|
|
|
sendMessage(sendReqVO);
|
|
|
|
|
|
+
|
|
|
AppKeFuMessageSendReqVO aiSendReqVO = sendReqVO;
|
|
|
aiSendReqVO.setRelUserId(SecurityFrameworkUtils.getLoginUserId());
|
|
|
aiSendReqVO.setSenderId(aiSendReqVO.getRelUserId());
|
|
|
aiSendReqVO.setContentType(11);
|
|
|
Long aiId = sendMessage(aiSendReqVO);
|
|
|
StringBuffer contentBuffer = new StringBuffer();
|
|
|
+ StringBuffer contentBufferCon = new StringBuffer();
|
|
|
return aiApi.getDifyMessageStreaming(inputs,
|
|
|
SecurityFrameworkUtils.getLoginUserId().toString(),
|
|
|
- apiKey, sendReqVO.getContent(), "132")
|
|
|
+ apiKey, sendReqVO.getContent(), conversationId)
|
|
|
.flatMap(response -> {
|
|
|
log.info("流式结果:" + response.toString());
|
|
|
if (response.getEvent().equals("message")) {
|
|
@@ -164,6 +181,7 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
|
|
|
}
|
|
|
if (response.getEvent().equals("message_end")) {
|
|
|
log.info("进入message_end");
|
|
|
+ contentBufferCon.append(response.getConversation_id());
|
|
|
}
|
|
|
return Flux.empty(); // 如果不是 workflow_finished 或 message_end,返回空 Flux
|
|
|
}).doOnComplete(() -> {
|
|
@@ -173,7 +191,14 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
|
|
|
// 1.2 保存消息
|
|
|
TenantUtils.executeIgnore(() ->
|
|
|
keFuMessageMapper.updateById(aiMessage));
|
|
|
-
|
|
|
+ if (conversationId == null) {
|
|
|
+ KeFuConversationRespVO updatePinnedReqVO = new KeFuConversationRespVO();
|
|
|
+ updatePinnedReqVO.setId(sendReqVO.getConversationId());
|
|
|
+ updatePinnedReqVO.setDifyConversationId(contentBufferCon.toString());
|
|
|
+ TenantUtils.executeIgnore(() ->
|
|
|
+ conversationService.updateConversation(updatePinnedReqVO));
|
|
|
+
|
|
|
+ }
|
|
|
})
|
|
|
.onErrorResume(e -> {
|
|
|
log.error("流式处理出错:", e);
|