From 97e489901a627f28affc575e217b7b71a00fb61e Mon Sep 17 00:00:00 2001 From: RockYang Date: Wed, 28 May 2025 22:55:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AE=A1=E7=90=86=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E5=AF=B9=E8=AF=9D=E6=98=BE=E7=A4=BA=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/handler/admin/chat_handler.go | 14 +- api/handler/chat_handler.go | 47 ++-- api/handler/chat_item_handler.go | 6 - web/src/views/ChatPlus.vue | 6 +- web/src/views/admin/records/ImageList.vue | 273 ++++++++++++++-------- 5 files changed, 210 insertions(+), 136 deletions(-) diff --git a/api/handler/admin/chat_handler.go b/api/handler/admin/chat_handler.go index d3670eeb..f99defa6 100644 --- a/api/handler/admin/chat_handler.go +++ b/api/handler/admin/chat_handler.go @@ -218,11 +218,19 @@ func (h *ChatHandler) History(c *gin.Context) { for _, item := range items { var v vo.ChatMessage err := utils.CopyObject(item, &v) + if err != nil { + continue + } + // 解析内容 + var content vo.MsgContent + err = utils.JsonDecode(item.Content, &content) + if err != nil { + content.Text = item.Content + } + v.Content = content v.CreatedAt = item.CreatedAt.Unix() v.UpdatedAt = item.UpdatedAt.Unix() - if err == nil { - messages = append(messages, v) - } + messages = append(messages, v) } } diff --git a/api/handler/chat_handler.go b/api/handler/chat_handler.go index 70569cff..caa6bd33 100644 --- a/api/handler/chat_handler.go +++ b/api/handler/chat_handler.go @@ -64,7 +64,6 @@ type ChatHandler struct { uploadManager *oss.UploaderManager licenseService *service.LicenseService ReqCancelFunc *types.LMap[string, context.CancelFunc] // HttpClient 请求取消 handle function - ChatContexts *types.LMap[string, []any] // 聊天上下文 Map [chatId] => []Message userService *service.UserService } @@ -75,7 +74,6 @@ func NewChatHandler(app *core.AppServer, db *gorm.DB, redis *redis.Client, manag uploadManager: manager, licenseService: licenseService, ReqCancelFunc: types.NewLMap[string, context.CancelFunc](), - ChatContexts: types.NewLMap[string, []any](), userService: userService, } } @@ -223,28 +221,24 @@ func (h *ChatHandler) sendMessage(ctx context.Context, input ChatInput, c *gin.C chatCtx := make([]any, 0) messages := make([]any, 0) if h.App.SysConfig.EnableContext { - if h.ChatContexts.Has(input.ChatId) { - messages = h.ChatContexts.Get(input.ChatId) - } else { - _ = utils.JsonDecode(input.ChatRole.Context, &messages) - if h.App.SysConfig.ContextDeep > 0 { - var historyMessages []model.ChatMessage - dbSession := h.DB.Session(&gorm.Session{}).Where("chat_id", input.ChatId) - if input.LastMsgId > 0 { // 重新生成逻辑 - dbSession = dbSession.Where("id < ?", input.LastMsgId) - // 删除对应的聊天记录 - h.DB.Where("chat_id", input.ChatId).Where("id >= ?", input.LastMsgId).Delete(&model.ChatMessage{}) - } - err = dbSession.Limit(h.App.SysConfig.ContextDeep).Order("id DESC").Find(&historyMessages).Error - if err == nil { - for i := len(historyMessages) - 1; i >= 0; i-- { - msg := historyMessages[i] - ms := types.Message{Role: "user", Content: msg.Content} - if msg.Type == types.ReplyMsg { - ms.Role = "assistant" - } - chatCtx = append(chatCtx, ms) + _ = utils.JsonDecode(input.ChatRole.Context, &messages) + if h.App.SysConfig.ContextDeep > 0 { + var historyMessages []model.ChatMessage + dbSession := h.DB.Session(&gorm.Session{}).Where("chat_id", input.ChatId) + if input.LastMsgId > 0 { // 重新生成逻辑 + dbSession = dbSession.Where("id < ?", input.LastMsgId) + // 删除对应的聊天记录 + h.DB.Debug().Where("chat_id", input.ChatId).Where("id >= ?", input.LastMsgId).Delete(&model.ChatMessage{}) + } + err = dbSession.Limit(h.App.SysConfig.ContextDeep).Order("id DESC").Find(&historyMessages).Error + if err == nil { + for i := len(historyMessages) - 1; i >= 0; i-- { + msg := historyMessages[i] + ms := types.Message{Role: "user", Content: msg.Content} + if msg.Type == types.ReplyMsg { + ms.Role = "assistant" } + chatCtx = append(chatCtx, ms) } } } @@ -521,13 +515,6 @@ func (h *ChatHandler) saveChatHistory( promptCreatedAt time.Time, replyCreatedAt time.Time) { - // 更新上下文消息 - if h.App.SysConfig.EnableContext { - chatCtx := req.Messages // 提问消息 - chatCtx = append(chatCtx, message) // 回复消息 - h.ChatContexts.Put(input.ChatId, chatCtx) - } - // 追加聊天记录 // for prompt var promptTokens, replyTokens, totalTokens int diff --git a/api/handler/chat_item_handler.go b/api/handler/chat_item_handler.go index 24c4e973..53d95b64 100644 --- a/api/handler/chat_item_handler.go +++ b/api/handler/chat_item_handler.go @@ -104,8 +104,6 @@ func (h *ChatHandler) Clear(c *gin.Context) { var chatIds = make([]string, 0) for _, chat := range chats { chatIds = append(chatIds, chat.ChatId) - // 清空会话上下文 - h.ChatContexts.Delete(chat.ChatId) } err = h.DB.Transaction(func(tx *gorm.DB) error { res := h.DB.Where("user_id =?", user.Id).Delete(&model.ChatItem{}) @@ -187,10 +185,6 @@ func (h *ChatHandler) Remove(c *gin.Context) { return } - // TODO: 是否要删除 MidJourney 绘画记录和图片文件? - - // 清空会话上下文 - h.ChatContexts.Delete(chatId) resp.SUCCESS(c, types.OkMsg) } diff --git a/web/src/views/ChatPlus.vue b/web/src/views/ChatPlus.vue index 56ec3ecf..53e07b90 100644 --- a/web/src/views/ChatPlus.vue +++ b/web/src/views/ChatPlus.vue @@ -873,7 +873,6 @@ const sendMessage = (messageId) => { }, model: getModelValue(modelID.value), created_at: new Date().getTime() / 1000, - message_id: messageId, }) // 添加空回复消息 @@ -909,6 +908,7 @@ const sendMessage = (messageId) => { tools: toolSelected.value, stream: stream.value, files: files.value, + last_msg_id: messageId, }) prompt.value = '' @@ -1196,10 +1196,10 @@ const reGenerate = function (messageId) { // 保存用户消息内容,填入输入框 const userPrompt = chatData.value[chatData.value.length - 1].content.text // 删除用户消息 - chatData.value.pop() + const lastMessage = chatData.value.pop() // 填入输入框 prompt.value = userPrompt - sendMessage(messageId) + sendMessage(lastMessage.id) // 将光标定位到输入框并聚焦 nextTick(() => { if (inputRef.value) { diff --git a/web/src/views/admin/records/ImageList.vue b/web/src/views/admin/records/ImageList.vue index 47c66b33..92c8323b 100644 --- a/web/src/views/admin/records/ImageList.vue +++ b/web/src/views/admin/records/ImageList.vue @@ -3,8 +3,20 @@
- - + + @@ -35,12 +49,25 @@