|
|
|
@@ -3,13 +3,22 @@ package com.xueyi.nlt.netty.server.handler; |
|
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
|
|
|
import com.xueyi.common.core.utils.core.ObjectUtil; |
|
|
|
import com.xueyi.common.core.utils.core.SpringUtils; |
|
|
|
import com.xueyi.common.core.web.result.AjaxResult; |
|
|
|
import com.xueyi.common.core.web.result.R; |
|
|
|
import com.xueyi.nlt.api.nlt.domain.vo.DmIntentVo; |
|
|
|
import com.xueyi.nlt.api.nlt.domain.vo.response.DmIntentResponse; |
|
|
|
import com.xueyi.nlt.netty.server.config.ServerConfig; |
|
|
|
import com.xueyi.nlt.nlt.service.IDmIntentService; |
|
|
|
import com.xueyi.nlt.nlt.service.IDmRegularService; |
|
|
|
import com.xueyi.nlt.nlt.service.impl.LogServiceImpl; |
|
|
|
import com.xueyi.nlt.nlt.template.FreeChatTemplate; |
|
|
|
import com.xueyi.nlt.nlt.template.MovieChatTemplate; |
|
|
|
import com.xueyi.system.api.digitalmans.domain.dto.DmManDeviceDto; |
|
|
|
import com.xueyi.system.api.digitalmans.feign.RemoteManDeviceService; |
|
|
|
import com.xueyi.system.api.organize.domain.dto.SysEnterpriseDto; |
|
|
|
import com.xueyi.system.api.organize.feign.RemoteEnterpriseService; |
|
|
|
import com.yomahub.tlog.context.TLogContext; |
|
|
|
import com.yomahub.tlog.core.annotation.TLogAspect; |
|
|
|
import com.yomahub.tlog.core.rpc.TLogLabelBean; |
|
|
|
import com.yomahub.tlog.core.rpc.TLogRPCHandler; |
|
|
|
@@ -25,18 +34,25 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.net.InetSocketAddress; |
|
|
|
|
|
|
|
@Component |
|
|
|
public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { |
|
|
|
// 添加log |
|
|
|
private static final Logger log = LoggerFactory.getLogger(ChatServerHandler.class); |
|
|
|
private static final Logger intentLog = LoggerFactory.getLogger("intentLog"); |
|
|
|
|
|
|
|
public static ChatServerHandler INSTANCE; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RemoteManDeviceService manDeviceService; |
|
|
|
@Autowired |
|
|
|
private FreeChatTemplate freeChatTemplate; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RemoteEnterpriseService remoteEnterpriseService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IDmIntentService intentService; |
|
|
|
|
|
|
|
@@ -44,6 +60,9 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket |
|
|
|
@Autowired |
|
|
|
private MovieChatTemplate movieChatTemplate; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private LogServiceImpl logService; |
|
|
|
|
|
|
|
private final TLogRPCHandler tLogRPCHandler = new TLogRPCHandler(); |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
@@ -52,6 +71,9 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket |
|
|
|
INSTANCE.freeChatTemplate = this.freeChatTemplate; |
|
|
|
INSTANCE.movieChatTemplate = this.movieChatTemplate; |
|
|
|
INSTANCE.intentService = this.intentService; |
|
|
|
INSTANCE.manDeviceService = this.manDeviceService; |
|
|
|
INSTANCE.remoteEnterpriseService = this.remoteEnterpriseService; |
|
|
|
INSTANCE.logService = this.logService; |
|
|
|
} |
|
|
|
@Override |
|
|
|
protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception { |
|
|
|
@@ -78,6 +100,24 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket |
|
|
|
Long operatorId = jsonObject.getLong("operatorId"); |
|
|
|
// 获取到发送人的用户id |
|
|
|
String msg = jsonObject.getString("msg"); |
|
|
|
// 判断当前数字人所属租户 |
|
|
|
R<DmManDeviceDto> manDeviceDtoR = INSTANCE.manDeviceService.manDeviceInfoInner(devId); |
|
|
|
if (!manDeviceDtoR.isOk() || manDeviceDtoR.getData() == null) { |
|
|
|
JSONObject jo = new JSONObject(); |
|
|
|
jo.put("action",""); |
|
|
|
jo.put("motion","idle"); |
|
|
|
jo.put("traceId",""); |
|
|
|
jo.put("status",2); |
|
|
|
jo.put("code",1); |
|
|
|
jo.put("tts","设备号未激活或已过期。"); |
|
|
|
channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); |
|
|
|
} |
|
|
|
String enterpriseName = ""; |
|
|
|
// 获取当前数字人租户信息 |
|
|
|
R<SysEnterpriseDto> enterpriseDtoR = INSTANCE.remoteEnterpriseService.getInfo(manDeviceDtoR.getData().getTId()); |
|
|
|
if (enterpriseDtoR.isOk()) { |
|
|
|
enterpriseName = enterpriseDtoR.getData().getName(); |
|
|
|
} |
|
|
|
synchronized (ServerConfig.class) { |
|
|
|
|
|
|
|
|
|
|
|
@@ -97,14 +137,15 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket |
|
|
|
|
|
|
|
if (ObjectUtil.isNotNull(dmIntentResponse) && StringUtils.isNotEmpty(dmIntentResponse.getSkillCode())) { |
|
|
|
JSONObject jo = formatResult(dmIntentResponse,2); |
|
|
|
channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); |
|
|
|
INSTANCE.reply(channel,msg, jo,enterpriseName,"正则"); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
// 调用知识库,如果知识库匹配到了,则返回知识库匹配到的结果 |
|
|
|
dmIntentResponse = INSTANCE.intentService.knowledgeAnswer(dmIntentVo); |
|
|
|
if (ObjectUtil.isNotNull(dmIntentResponse) && StringUtils.isNotEmpty(dmIntentResponse.getSkillCode()) && dmIntentResponse.getH5().getInteger("accurate") == 1) { |
|
|
|
if (ObjectUtil.isNotNull(dmIntentResponse) && StringUtils.isNotEmpty(dmIntentResponse.getSkillCode()) && ObjectUtil.isNotNull(dmIntentResponse.getH5()) && dmIntentResponse.getH5().getInteger("accurate") == 1) { |
|
|
|
JSONObject jo = formatResult(dmIntentResponse,2); |
|
|
|
channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); |
|
|
|
INSTANCE.reply(channel,msg, jo,enterpriseName,"知识库"); |
|
|
|
return; |
|
|
|
} |
|
|
|
else { |
|
|
|
@@ -112,9 +153,10 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket |
|
|
|
jo.put("action","chat"); |
|
|
|
jo.put("motion","idle"); |
|
|
|
jo.put("traceId",""); |
|
|
|
jo.put("status",1); |
|
|
|
jo.put("status",0); |
|
|
|
jo.put("tts","请稍等一下,我要查询一下功能。"); |
|
|
|
channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); |
|
|
|
INSTANCE.logService.record(jo,msg,enterpriseName,"大模型"); |
|
|
|
sendMsg(devId, msg); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
@@ -186,6 +228,22 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 向客户端发送消息,同时记录日志 |
|
|
|
* |
|
|
|
* @param jo |
|
|
|
* @param enterpriseName |
|
|
|
* @param type |
|
|
|
* |
|
|
|
*/ |
|
|
|
@TLogAspect(str = "testtest") |
|
|
|
// @TLogAspect({"enterpriseName","type"}) |
|
|
|
private void reply(Channel channel, String msg, JSONObject jo,String enterpriseName,String type ) { |
|
|
|
|
|
|
|
INSTANCE.logService.record(jo,msg,enterpriseName,type); |
|
|
|
channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 一旦客户端连接上来,该方法被执行 |
|
|
|
* |
|
|
|
|