From d8bc0fe1259cd0f7b5c04059710495f5942a8c6b Mon Sep 17 00:00:00 2001 From: RockYang Date: Fri, 21 Apr 2023 11:21:41 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=8F=91=E8=B5=B7=20socket=20=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=97=B6=E5=80=99=E4=BC=A0=E5=85=A5=20chatId=20?= =?UTF-8?q?=E6=9D=A5=E5=8C=BA=E5=88=86=E4=BC=9A=E8=AF=9D=E3=80=82=202.=20C?= =?UTF-8?q?hat-Plus=20=E7=95=8C=E9=9D=A2=E6=96=B0=E5=A2=9E=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E5=9B=9E=E5=A4=8D=E5=86=85=E5=AE=B9=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E3=80=82=203.=20=E4=BC=98=E5=8C=96=20ChatFree=20=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E4=BC=9A=E8=AF=9D=E5=88=87=E6=8D=A2=E5=92=8C=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/chat_handler.go | 53 ++++++++++------ server/server.go | 1 + types/chat.go | 1 + web/src/components/plus/ChatReply.vue | 77 ++++++++++++++++------- web/src/main.js | 1 - web/src/utils/libs.js | 14 +++++ web/src/utils/prototype.js | 8 --- web/src/views/ChatFree.vue | 91 ++++++++++++++++++--------- web/src/views/ChatPlus.vue | 2 +- 9 files changed, 168 insertions(+), 80 deletions(-) delete mode 100644 web/src/utils/prototype.js diff --git a/server/chat_handler.go b/server/chat_handler.go index bb540596..1ebb7bed 100644 --- a/server/chat_handler.go +++ b/server/chat_handler.go @@ -29,7 +29,9 @@ func (s *Server) ChatHandle(c *gin.Context) { } sessionId := c.Query("sessionId") roleKey := c.Query("role") + chatId := c.Query("chatId") session, ok := s.ChatSession[sessionId] + session.ChatId = chatId if !ok { // 用户未登录 c.Abort() return @@ -61,18 +63,18 @@ func (s *Server) ChatHandle(c *gin.Context) { return } logger.Info("Receive a message: ", string(message)) - //replyMessage(client, "当前 TOKEN 无效,请使用合法的 TOKEN 登录!", false) - //replyMessage(client, "![](images/wx.png)", true) - ctx, cancel := context.WithCancel(context.Background()) - s.ReqCancelFunc[sessionId] = cancel - // 回复消息 - err = s.sendMessage(ctx, session, chatRole, string(message), client, false) - if err != nil { - logger.Error(err) - } else { - replyChunkMessage(client, types.WsMessage{Type: types.WsEnd, IsHelloMsg: false}) - logger.Info("回答完毕: " + string(message)) - } + replyMessage(client, "当前 TOKEN 无效,请使用合法的 TOKEN 登录!", false) + replyMessage(client, "![](images/wx.png)", false) + //ctx, cancel := context.WithCancel(context.Background()) + //s.ReqCancelFunc[sessionId] = cancel + //// 回复消息 + //err = s.sendMessage(ctx, session, chatRole, string(message), client, false) + //if err != nil { + // logger.Error(err) + //} else { + // replyChunkMessage(client, types.WsMessage{Type: types.WsEnd, IsHelloMsg: false}) + // logger.Info("回答完毕: " + string(message)) + //} } }() @@ -94,20 +96,20 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol if user.Status == false { replyMessage(ws, "当前 TOKEN 已经被禁用,如果疑问,请联系管理员!", false) - replyMessage(ws, "![](images/wx.png)", true) + replyMessage(ws, "![](images/wx.png)", false) return errors.New("当前 TOKEN " + user.Name + "已经被禁用") } if time.Now().Unix() > user.ExpiredTime { exTime := time.Unix(user.ExpiredTime, 0).Format("2006-01-02 15:04:05") replyMessage(ws, "当前 TOKEN 已过期,过期时间为:"+exTime+",如果疑问,请联系管理员!", false) - replyMessage(ws, "![](images/wx.png)", true) + replyMessage(ws, "![](images/wx.png)", false) return errors.New("当前 TOKEN " + user.Name + "已过期") } if user.MaxCalls > 0 && user.RemainingCalls <= 0 { replyMessage(ws, "当前 TOKEN 点数已经用尽,加入我们的知识星球可以免费领取点卡!", false) - replyMessage(ws, "![](images/start.png)", true) + replyMessage(ws, "![](images/start.png)", false) return nil } var req = types.ApiRequest{ @@ -117,7 +119,7 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol Stream: true, } var chatCtx []types.Message - var ctxKey = fmt.Sprintf("%s-%s", session.SessionId, role.Key) + var ctxKey = fmt.Sprintf("%s-%s-%s", session.SessionId, role.Key, session.ChatId) if v, ok := s.ChatContexts[ctxKey]; ok && s.Config.Chat.EnableContext { chatCtx = v.Messages } else { @@ -190,7 +192,7 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol // 如果三次请求都失败的话,则返回对应的错误信息 if err != nil { replyMessage(ws, ErrorMsg, false) - replyMessage(ws, "![](images/wx.png)", true) + replyMessage(ws, "![](images/wx.png)", false) return err } @@ -237,7 +239,7 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol if err != nil { // 数据解析出错 logger.Error(err, line) replyMessage(ws, ErrorMsg, false) - replyMessage(ws, "![](images/wx.png)", true) + replyMessage(ws, "![](images/wx.png)", false) break } @@ -473,3 +475,18 @@ func (s *Server) StopGenerateHandle(c *gin.Context) { delete(s.ReqCancelFunc, sessionId) c.JSON(http.StatusOK, types.BizVo{Code: types.Success}) } + +// GetHelloMsgHandle 获取角色的打招呼信息 +func (s *Server) GetHelloMsgHandle(c *gin.Context) { + role := strings.TrimSpace(c.Query("role")) + if role == "" { + c.JSON(http.StatusOK, types.BizVo{Code: types.InvalidParams, Message: "Invalid args"}) + return + } + chatRole, err := GetChatRole(role) + if err != nil { + c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Role not found"}) + return + } + c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: chatRole.HelloMsg}) +} diff --git a/server/server.go b/server/server.go index 600d9a5b..84bdc947 100644 --- a/server/server.go +++ b/server/server.go @@ -94,6 +94,7 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) { engine.POST("api/chat/stop", s.StopGenerateHandle) engine.POST("api/chat/history", s.GetChatHistoryHandle) engine.POST("api/chat/history/clear", s.ClearHistoryHandle) + engine.GET("api/role/hello", s.GetHelloMsgHandle) engine.POST("api/config/set", s.ConfigSetHandle) engine.GET("api/config/chat-roles/get", s.GetChatRoleListHandle) diff --git a/types/chat.go b/types/chat.go index 8e4bf7c4..95abee61 100644 --- a/types/chat.go +++ b/types/chat.go @@ -46,6 +46,7 @@ type ChatSession struct { SessionId string `json:"session_id"` ClientIP string `json:"client_ip"` // 客户端 IP Username string `json:"user"` // 当前登录的 user + ChatId string `json:"chat_id"` // 客户端聊天会话 ID } // ChatContext 聊天上下文 diff --git a/web/src/components/plus/ChatReply.vue b/web/src/components/plus/ChatReply.vue index 66c5c157..27773b25 100644 --- a/web/src/components/plus/ChatReply.vue +++ b/web/src/components/plus/ChatReply.vue @@ -6,16 +6,34 @@
-
+
+
+
+ + + + + + + +
+