Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: delete not found message should report error (#20678)

Wang Qi753c4cd75174
diff --git a/internal/dao/conversation_history.go b/internal/dao/conversation_history.go
index 287f995447..dbdb8c1d75 100644
--- a/internal/dao/conversation_history.go
+++ b/internal/dao/conversation_history.go
@@ -88,9 +88,6 @@ func updateConversationHistory(ctx context.Context, db *gorm.DB, messageTable, r
var question entity.ConversationMessage
query := db.WithContext(ctx).Table(messageTable).Where("conversation_id = ? AND message_id = ?", conversationID, update.DeleteMessageID)
if err := query.Session(&gorm.Session{}).Select("position").Where("role = ?", "user").Order("position").Take(&question).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil
- }
return err
}
positions := []int{question.Position}
diff --git a/internal/service/chat_session.go b/internal/service/chat_session.go
index df79f80c95..9089a81ff0 100644
--- a/internal/service/chat_session.go
+++ b/internal/service/chat_session.go
@@ -556,6 +556,7 @@ func (s *ChatSessionService) DeleteSessionMessage(ctx context.Context, userID, c
if len(session.Reference) > 0 && references == nil {
return nil, common.CodeDataError, errors.New("invalid session reference")
}
+ found := false
for i, msg := range messages {
if msgID != stringValue(msg["id"]) {
continue
@@ -572,8 +573,12 @@ func (s *ChatSessionService) DeleteSessionMessage(ctx context.Context, userID, c
end++
}
messages = append(messages[:i], messages[end:]...)
+ found = true
break
}
+ if !found {
+ return nil, common.CodeDataError, errors.New("message not found")
+ }
messageRaw, err := json.Marshal(messages)
if err != nil {
@@ -586,6 +591,9 @@ func (s *ChatSessionService) DeleteSessionMessage(ctx context.Context, userID, c
if err = s.chatSessionDAO.UpdateByID(ctx, dao.DB, session.ID, map[string]interface{}{
"history_update": dao.ConversationHistoryUpdate{DeleteMessageID: msgID},
}); err != nil {
+ if isChatSessionNotFound(err) {
+ return nil, common.CodeDataError, errors.New("message not found")
+ }
return nil, common.CodeServerError, err
}
session.Message = messageRaw