修复 ChatFree 重复刷消息的 Bug

This commit is contained in:
RockYang 2023-05-05 17:45:31 +08:00
parent e60decedd4
commit 21adfd441c
2 changed files with 11 additions and 9 deletions

View File

@ -68,7 +68,7 @@ func (s *Server) ChatHandle(c *gin.Context) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
s.ReqCancelFunc[sessionId] = cancel s.ReqCancelFunc[sessionId] = cancel
// 回复消息 // 回复消息
err = s.sendMessage(ctx, session, chatRole, string(message), client, false) err = s.sendMessage(ctx, session, chatRole, string(message), client)
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
} else { } else {
@ -81,7 +81,7 @@ func (s *Server) ChatHandle(c *gin.Context) {
} }
// 将消息发送给 ChatGPT 并获取结果,通过 WebSocket 推送到客户端 // 将消息发送给 ChatGPT 并获取结果,通过 WebSocket 推送到客户端
func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, role types.ChatRole, prompt string, ws Client, resetContext bool) error { func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, role types.ChatRole, prompt string, ws Client) error {
cancel := s.ReqCancelFunc[session.SessionId] cancel := s.ReqCancelFunc[session.SessionId]
defer func() { defer func() {
cancel() cancel()
@ -189,6 +189,10 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol
retryCount-- retryCount--
} }
if response != nil {
defer response.Body.Close()
}
// 如果三次请求都失败的话,则返回对应的错误信息 // 如果三次请求都失败的话,则返回对应的错误信息
if err != nil { if err != nil {
replyMessage(ws, ErrorMsg, false) replyMessage(ws, ErrorMsg, false)
@ -223,14 +227,15 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol
_ = utils.SaveConfig(s.Config, s.ConfigPath) _ = utils.SaveConfig(s.Config, s.ConfigPath)
// 重发当前消息 // 重发当前消息
return s.sendMessage(ctx, session, role, prompt, ws, false) return s.sendMessage(ctx, session, role, prompt, ws)
// 上下文超出长度了 // 上下文超出长度了
} else if strings.Contains(line, "This model's maximum context length is 4097 tokens") { } else if strings.Contains(line, "This model's maximum context length is 4097 tokens") {
logger.Infof("会话上下文长度超出限制, Username: %s", user.Name) logger.Infof("会话上下文长度超出限制, Username: %s", user.Name)
// 重置上下文,重发当前消息 replyMessage(ws, "温馨提示:当前会话上下文长度超出限制,已为您重置会话上下文!", false)
// 重置上下文
delete(s.ChatContexts, ctxKey) delete(s.ChatContexts, ctxKey)
return s.sendMessage(ctx, session, role, prompt, ws, true) break
} else if !strings.Contains(line, "data:") { } else if !strings.Contains(line, "data:") {
continue continue
} }
@ -243,9 +248,6 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol
break break
} }
if resetContext {
replyMessage(ws, "温馨提示:当前会话上下文长度超出限制,已为您重置会话上下文!", false)
}
// 初始化 role // 初始化 role
if responseBody.Choices[0].Delta.Role != "" && message.Role == "" { if responseBody.Choices[0].Delta.Role != "" && message.Role == "" {
message.Role = responseBody.Choices[0].Delta.Role message.Role = responseBody.Choices[0].Delta.Role
@ -272,7 +274,6 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol
continue continue
} }
} // end for } // end for
_ = response.Body.Close() // 关闭资源
// 消息发送成功 // 消息发送成功
if len(contents) > 0 { if len(contents) > 0 {

View File

@ -430,6 +430,7 @@ export default defineComponent({
// //
fetchChatHistory: function (chatId) { fetchChatHistory: function (chatId) {
this.chatData = [];
const list = getChatHistory(chatId); const list = getChatHistory(chatId);
if (list) { if (list) {
const md = require('markdown-it')(); const md = require('markdown-it')();