Commit fdfce929 authored by 何金镒's avatar 何金镒

优化对话返回

parent a2402a1b
...@@ -9,12 +9,14 @@ import cn.hutool.json.JSONUtil; ...@@ -9,12 +9,14 @@ import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ikonke.konkeaialibabamcp.constant.RedisKeys; import com.ikonke.konkeaialibabamcp.constant.RedisKeys;
import com.ikonke.konkeaialibabamcp.controller.param.DifyStreamResponse; import com.ikonke.konkeaialibabamcp.controller.param.DifyStreamResponse;
import com.ikonke.konkeaialibabamcp.controller.param.WebSocketParam;
import com.ikonke.konkeaialibabamcp.entity.cdc.UserEntity; import com.ikonke.konkeaialibabamcp.entity.cdc.UserEntity;
import com.ikonke.konkeaialibabamcp.entity.mysql.CDCToken; import com.ikonke.konkeaialibabamcp.entity.mysql.CDCToken;
import com.ikonke.konkeaialibabamcp.event.mode.DifyChatSteamEvent; import com.ikonke.konkeaialibabamcp.event.mode.DifyChatSteamEvent;
import com.ikonke.konkeaialibabamcp.service.cdc.CdcHttpUtils; import com.ikonke.konkeaialibabamcp.service.cdc.CdcHttpUtils;
import com.ikonke.konkeaialibabamcp.service.mysqlservice.ICDCTokenService; import com.ikonke.konkeaialibabamcp.service.mysqlservice.ICDCTokenService;
import com.ikonke.konkeaialibabamcp.utils.CcuUtils; import com.ikonke.konkeaialibabamcp.utils.CcuUtils;
import com.ikonke.konkeaialibabamcp.utils.WebSocketUtil;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -22,6 +24,7 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -22,6 +24,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
...@@ -41,6 +44,8 @@ public class DifyControllerV2 { ...@@ -41,6 +44,8 @@ public class DifyControllerV2 {
private WebClient webClient; private WebClient webClient;
@Autowired @Autowired
private CdcHttpUtils cdcHttpUtils; private CdcHttpUtils cdcHttpUtils;
@Autowired
private WebSocketUtil webSocketUtil;
private final ApplicationEventPublisher eventPublisher; private final ApplicationEventPublisher eventPublisher;
...@@ -73,6 +78,27 @@ public class DifyControllerV2 { ...@@ -73,6 +78,27 @@ public class DifyControllerV2 {
// } // }
// } // }
@PostMapping(value = "/sendToWebSocket")
public void sendToWebSocket(@RequestBody WebSocketParam param) {
String sn = param.getSn();
String msg = param.getMsg();
String type = param.getType();
String sceneid = param.getSceneid();
if(StrUtil.isNotBlank(sn) && StrUtil.isNotBlank(msg)){
log.info("【{}】:sendToWebSocket..请求参数:{},type:{}",sn,msg, type);
if(StrUtil.isBlank(type)){
type = "other";
}
JSONObject json = new JSONObject();
json.set("msg", msg);
json.set("type", type);
if(StrUtil.isNotBlank(sceneid)){
json.set("sceneid", sceneid);
}
webSocketUtil.sendToSingle(sn, json.toString());
}
}
/** /**
......
package com.ikonke.konkeaialibabamcp.controller.param;
import lombok.Data;
@Data
public class WebSocketParam {
private String sn;
private String type;
private String sceneid;
private String msg;
}
...@@ -57,24 +57,24 @@ public class DifyChatSteamEventListener { ...@@ -57,24 +57,24 @@ public class DifyChatSteamEventListener {
.execute().body(); .execute().body();
log.info("dify请求返回11--->:{}",ai_result); log.info("dify请求返回11--->:{}",ai_result);
ai_result = ai_result.replaceAll("\n", "##").trim(); // ai_result = ai_result.replaceAll("\n", "##").trim();
log.info("dify请求返回22--->:{}",ai_result); // log.info("dify请求返回22--->:{}",ai_result);
//
JSONObject json = new JSONObject(); // JSONObject json = new JSONObject();
if(!JSONUtil.isTypeJSON(ai_result)){ // if(!JSONUtil.isTypeJSON(ai_result)){
json.put("msg", ai_result); // json.put("msg", ai_result);
json.put("type", "other"); // json.put("type", "other");
}else{ // }else{
json = JSONUtil.parseObj(ai_result); // json = JSONUtil.parseObj(ai_result);
if(!json.containsKey("type")){ // if(!json.containsKey("type")){
json.put("type", "other"); // json.put("type", "other");
} // }
} // }
//
log.info("【{}】:dify请求返回:{}",event.getSn(),json); // log.info("【{}】:dify请求返回:{}",event.getSn(),json);
// 通过WebSocket发送消息给屏端 // 通过WebSocket发送消息给屏端
webSocketUtil.sendToSingle(event.getSn(), json.toString()); // webSocketUtil.sendToSingle(event.getSn(), json.toString());
log.info("【{}】:dify异步请求耗时--->:{}",event.getSn(),System.currentTimeMillis() - startTime); log.info("【{}】:dify异步请求耗时--->:{}",event.getSn(),System.currentTimeMillis() - startTime);
} }
......
...@@ -51,6 +51,34 @@ class KonkeAiAlibabaMcpApplicationTests { ...@@ -51,6 +51,34 @@ class KonkeAiAlibabaMcpApplicationTests {
// @Test
// void test() {
// String ai_result = "{\"msg\":\"{\"msg\":\"{\"msg\":\"中行游泳馆830目前无法获取详细信息,可能已下架或不存在。您可以尝试其他可预约的共享空间,如健身房或茶室。如需进一步帮助,请告知具体需求。\",\"type\":\"cdc\"}\",\"type\":\"cdc\"}\",\"type\":\"cdc\"}";
//
// ai_result = ai_result.replaceAll("\n", "##").trim();
//
// JSONObject json = new JSONObject();
// if(!JSONUtil.isTypeJSON(ai_result)){
// json.put("msg", ai_result);
// json.put("type", "other");
// }else{
// json = JSONUtil.parseObj(ai_result);
// if(json.containsKey("msg")){
// String msg = json.getStr("msg");
// if(JSONUtil.isTypeJSON(msg)){
// JSONObject msgJson = JSONUtil.parseObj(msg);
// if(msgJson.containsKey("type") && msgJson.containsKey("msg")){
// ai_result = msg;
// }
// }
// }
// if(!json.containsKey("type")){
// json.put("type", "other");
// }
// }
//
// System.out.println("----->"+ ai_result);
// }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment