mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-19 01:36:38 +08:00
修复 ChatFree 重复刷消息的 Bug
This commit is contained in:
parent
e60decedd4
commit
21adfd441c
@ -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 {
|
||||||
|
@ -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')();
|
||||||
|
Loading…
Reference in New Issue
Block a user