Skip to content

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

Fix: chat channel name rule (#21294)

cielfe7af3544934
diff --git a/internal/channels/discord.go b/internal/channels/discord.go
index 3df964ccb3..944cd3f306 100644
--- a/internal/channels/discord.go
+++ b/internal/channels/discord.go
@@ -521,18 +521,12 @@ func (c *discordChannel) enqueueIncoming(ctx context.Context, incoming core.Inco
go c.runChatWorker(ctx, incoming.ChatID, worker)
}
return true
- case <-ctx.Done():
- c.mu.Unlock()
default:
queueFull = true
c.mu.Unlock()
}
- if startWorker {
- go c.runChatWorker(ctx, incoming.ChatID, worker)
- }
-
- if queueFull {
+ if queueFull && ctx.Err() == nil {
common.Warn("discord: dropping message, queue is full", zap.String("account_id", c.account.AccountID), zap.String("message_id", incoming.MessageID), zap.String("chat_id", incoming.ChatID))
}
diff --git a/internal/service/chat_channel.go b/internal/service/chat_channel.go
index a675c040a6..50ae66d64b 100644
--- a/internal/service/chat_channel.go
+++ b/internal/service/chat_channel.go
@@ -259,7 +259,7 @@ func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg Chat
return "", err
}
- session, err := s.chatSessionSvc.GetOrCreateForChannel(ctx, dialog.ID, channel.ID, msg.ChatID)
+ session, err := s.chatSessionSvc.GetOrCreateForChannel(ctx, dialog.ID, channel.ID, msg.ChatID, msg.Text, channel.Name)
if err != nil {
return "", err
}
diff --git a/internal/service/chat_channel_test.go b/internal/service/chat_channel_test.go
index ee82feed44..6a97f37267 100644
--- a/internal/service/chat_channel_test.go
+++ b/internal/service/chat_channel_test.go
@@ -2,7 +2,9 @@ package service
import (
"errors"
+ "strings"
"testing"
+ "unicode/utf8"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
@@ -286,3 +288,191 @@ func TestChatChannelServiceDeleteChatChannel(t *testing.T) {
t.Fatal("expected deleted record to be gone")
}
}
+
+func countChannelSessions(t *testing.T, db *gorm.DB) int64 {
+ t.Helper()
+
+ var count int64
+ if err := db.Model(&entity.ChatSession{}).Count(&count).Error; err != nil {
+ t.Fatalf("failed to count sessions: %v", err)
+ }
+ return count
+}
+
+func TestGetOrCreateForChannelNamesFromFirstQueryAndChannelName(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+
+ session, err := NewChatSessionService().GetOrCreateForChannel(
+ t.Context(), "dialog-1", "cc-1", "discord-channel-1", "你好,帮我查一下退款政策", "Discord 客服",
+ )
+ if err != nil {
+ t.Fatalf("GetOrCreateForChannel failed: %v", err)
+ }
+ if session.ID != channelSessionID("dialog-1", "cc-1", "discord-channel-1") {
+ t.Fatalf("unexpected session id: %s", session.ID)
+ }
+ if session.DialogID != "dialog-1" {
+ t.Fatalf("unexpected dialog id: %s", session.DialogID)
+ }
+ if session.Name == nil || *session.Name != "(channel: Discord 客服) 你好,帮我查一下退款政策" {
+ t.Fatalf("unexpected session name: %v", session.Name)
+ }
+}
+
+func TestGetOrCreateForChannelKeepsNameAfterFirstMessage(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ svc := NewChatSessionService()
+ ctx := t.Context()
+
+ first, err := svc.GetOrCreateForChannel(ctx, "dialog-1", "cc-1", "discord-channel-1", "第一句", "客服")
+ if err != nil {
+ t.Fatalf("first GetOrCreateForChannel failed: %v", err)
+ }
+
+ second, err := svc.GetOrCreateForChannel(ctx, "dialog-1", "cc-1", "discord-channel-1", "第二句", "改过的名字")
+ if err != nil {
+ t.Fatalf("second GetOrCreateForChannel failed: %v", err)
+ }
+ if second.ID != first.ID {
+ t.Fatalf("expected the same session, got %s and %s", first.ID, second.ID)
+ }
+ if second.Name == nil || *second.Name != "(channel: 客服) 第一句" {
+ t.Fatalf("expected the first name to stick, got %v", second.Name)
+ }
+ if count := countChannelSessions(t, db); count != 1 {
+ t.Fatalf("expected 1 session, got %d", count)
+ }
+}
+
+func TestGetOrCreateForChannelTruncatesLongFirstQuery(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+
+ query := strings.Repeat("长", 300)
+ session, err := NewChatSessionService().GetOrCreateForChannel(
+ t.Context(), "dialog-1", "cc-1", "discord-channel-1", query, "客服",
+ )
+ if err != nil {
+ t.Fatalf("GetOrCreateForChannel failed: %v", err)
+ }
+ if session.Name == nil {
+ t.Fatal("expected a session name")
+ }
+ name := *session.Name
+ if utf8.RuneCountInString(name) != channelSessionNameMaxRunes {
+ t.Fatalf("expected %d runes, got %d", channelSessionNameMaxRunes, utf8.RuneCountInString(name))
+ }
+ if !strings.HasPrefix(name, "(channel: 客服) ") {
+ t.Fatalf("expected the channel prefix to survive, got %q", name)
+ }
+ wantPrefix := "(channel: 客服) " + string([]rune(query)[:channelSessionNameMaxRunes-utf8.RuneCountInString("(channel: 客服) ")])
+ if name != wantPrefix {
+ t.Fatalf("unexpected truncated name: %q", name)
+ }
+}
+
+func TestGetOrCreateForChannelNormalizesWhitespace(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+
+ session, err := NewChatSessionService().GetOrCreateForChannel(
+ t.Context(), "dialog-1", "cc-1", "discord-channel-1", "你好\n\n帮我\t查一下", "客服",
+ )
+ if err != nil {
+ t.Fatalf("GetOrCreateForChannel failed: %v", err)
+ }
+ if session.Name == nil || *session.Name != "(channel: 客服) 你好 帮我 查一下" {
+ t.Fatalf("unexpected session name: %v", session.Name)
+ }
+}
+
+func TestGetOrCreateForChannelStripsMentionMarkupFromName(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+
+ session, err := NewChatSessionService().GetOrCreateForChannel(
+ t.Context(), "dialog-1", "cc-1", "discord-channel-1", "<@1562356664286445598> 林黛玉是谁", "cc",
+ )
+ if err != nil {
+ t.Fatalf("GetOrCreateForChannel failed: %v", err)
+ }
+ if session.Name == nil || *session.Name != "(channel: cc) 林黛玉是谁" {
+ t.Fatalf("unexpected session name: %v", session.Name)
+ }
+}
+
+func TestChannelSessionNameStripsMentionMarkup(t *testing.T) {
+ tests := []struct {
+ name string
+ firstQuery string
+ channelName string
+ want string
+ }{
+ {
+ name: "user mention prefix",
+ firstQuery: "<@1562356664286445598> 林黛玉是谁",
+ channelName: "cc",
+ want: "(channel: cc) 林黛玉是谁",
+ },
+ {
+ name: "nickname mention and role mention",
+ firstQuery: "<@!42> <@&7> 查一下退款政策",
+ channelName: "客服",
+ want: "(channel: 客服) 查一下退款政策",
+ },
+ {
+ name: "channel mention",
+ firstQuery: "<#99> 历史消息",
+ channelName: "客服",
+ want: "(channel: 客服) 历史消息",
+ },
+ {
+ name: "mention only falls back to the channel label",
+ firstQuery: "<@1562356664286445598>",
+ channelName: "cc",
+ want: "(channel: cc)",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := channelSessionName(tt.firstQuery, tt.channelName); got != tt.want {
+ t.Fatalf("channelSessionName(%q, %q) = %q, want %q", tt.firstQuery, tt.channelName, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestGetOrCreateForChannelFallbackNames(t *testing.T) {
+ t.Run("blank query uses the channel label", func(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+
+ session, err := NewChatSessionService().GetOrCreateForChannel(
+ t.Context(), "dialog-1", "cc-1", "discord-channel-1", " \n ", "客服",
+ )
+ if err != nil {
+ t.Fatalf("GetOrCreateForChannel failed: %v", err)
+ }
+ if session.Name == nil || *session.Name != "(channel: 客服)" {
+ t.Fatalf("unexpected session name: %v", session.Name)
+ }
+ })
+
+ t.Run("blank query and channel name use the legacy name", func(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+
+ session, err := NewChatSessionService().GetOrCreateForChannel(
+ t.Context(), "dialog-1", "cc-1", "discord-channel-1", "", "",
+ )
+ if err != nil {
+ t.Fatalf("GetOrCreateForChannel failed: %v", err)
+ }
+ if session.Name == nil || *session.Name != "channel:cc-1:discord-channel-1" {
+ t.Fatalf("unexpected session name: %v", session.Name)
+ }
+ })
+}
diff --git a/internal/service/chat_session.go b/internal/service/chat_session.go
index 95be479703..4a4077a0ee 100644
--- a/internal/service/chat_session.go
+++ b/internal/service/chat_session.go
@@ -26,6 +26,7 @@ import (
"ragflow/internal/common"
"ragflow/internal/engine"
"ragflow/internal/storage"
+ "regexp"
"strconv"
"strings"
"time"
@@ -37,6 +38,7 @@ import (
"ragflow/internal/entity"
modelModule "ragflow/internal/entity/models"
"ragflow/internal/permission"
+ "ragflow/internal/utility"
)
// Interfaces for testability — satisfied by the concrete DAO/pipeline types.
@@ -1610,7 +1612,9 @@ func (s *ChatSessionService) createSessionForCompletion(ctx context.Context, cha
}
// GetOrCreateForChannel finds or creates the deterministic conversation for one external chat.
-func (s *ChatSessionService) GetOrCreateForChannel(ctx context.Context, dialogID, channelID, chatID string) (*entity.ChatSession, error) {
+// firstQuery and channelName only affect the display name, and only when the
+// conversation is created here; an existing conversation keeps its name.
+func (s *ChatSessionService) GetOrCreateForChannel(ctx context.Context, dialogID, channelID, chatID, firstQuery, channelName string) (*entity.ChatSession, error) {
sessionID := channelSessionID(dialogID, channelID, chatID)
session, err := s.chatSessionDAO.GetByID(ctx, dao.DB, sessionID)
if err == nil {
@@ -1622,7 +1626,10 @@ func (s *ChatSessionService) GetOrCreateForChannel(ctx context.Context, dialogID
messagesJSON, _ := json.Marshal([]map[string]interface{}{})
referenceJSON, _ := json.Marshal([]interface{}{})
- name := fmt.Sprintf("channel:%s:%s", channelID, chatID)
+ name := channelSessionName(firstQuery, channelName)
+ if name == "" {
+ name = fmt.Sprintf("channel:%s:%s", channelID, chatID)
+ }
session = &entity.ChatSession{
ID: sessionID,
DialogID: dialogID,
@@ -1646,6 +1653,47 @@ func channelSessionID(dialogID, channelID, chatID string) string {
return fmt.Sprintf("%x", sum[:])[:32]
}
+// channelSessionNameMaxRunes matches the conversation.name column width.
+const channelSessionNameMaxRunes = 255
+
+// channelMentionMarkup matches platform mention markup that carries an opaque
+// ID instead of readable text, such as Discord's <@123>, <@!123>, <@&123>, and
+// <#123>.
+var channelMentionMarkup = regexp.MustCompile(`<(?:@[!&]?|#)\d+>`)
+
+// channelSessionName builds the display name for a newly created channel
+// session: the chat-channel name in parentheses, then the first user query,
+// as in "(channel: cc) who is Lin Daiyu". Mention markup is removed from the query,
+// so a bot mention never shows up in the name. It returns the empty string
+// when both inputs are blank, so the caller can fall back to the deterministic
+// ID-based name.
+func channelSessionName(firstQuery, channelName string) string {
+ query := normalizeChannelSessionNameText(channelMentionMarkup.ReplaceAllString(firstQuery, " "))
+ channel := normalizeChannelSessionNameText(channelName)
+ if channel == "" {
+ return utility.TruncateRunes(query, channelSessionNameMaxRunes)
+ }
+
+ prefix := "(channel: " + channel + ") "
+ if query == "" {
+ return utility.TruncateRunes(strings.TrimSpace(prefix), channelSessionNameMaxRunes)
+ }
+ prefixRunes := []rune(prefix)
+ if len(prefixRunes) >= channelSessionNameMaxRunes {
+ return string(prefixRunes[:channelSessionNameMaxRunes])
+ }
+ if room := channelSessionNameMaxRunes - len(prefixRunes); len([]rune(query)) > room {
+ query = utility.TruncateRunes(query, room)
+ }
+ return prefix + query
+}
+
+// normalizeChannelSessionNameText collapses every whitespace run, including
+// newlines, into one space and trims the ends.
+func normalizeChannelSessionNameText(s string) string {
+ return strings.Join(strings.Fields(s), " ")
+}
+
// appendSessionMessage appends the last user message to the session's message history.
func (s *ChatSessionService) appendSessionMessage(session *entity.ChatSession, requestMsg []map[string]interface{}, receivedAt float64) *entity.ChatSession {
msgs := parseMessages(session.Message)