RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
refactor(chat): push pagination down to ListByOwnerIDs (#20450)
ciel5bb817189b79
diff --git a/internal/dao/chat.go b/internal/dao/chat.goindex 452281bde0..744dc4aaf2 100644--- a/internal/dao/chat.go+++ b/internal/dao/chat.go@@ -106,11 +106,12 @@ func (dao *ChatDAO) ListByTenantIDs(ctx context.Context, db *gorm.DB, tenantIDsreturn chats, total, nil}-// ListByOwnerIDs list chats by owner IDs with filtering (manual pagination)-func (dao *ChatDAO) ListByOwnerIDs(ctx context.Context, db *gorm.DB, ownerIDs []string, userID string, terms []OrderTerm, keywords string) ([]*entity.ChatListItem, int64, error) {+// ListByOwnerIDs list chats by owner IDs with pagination and filtering+func (dao *ChatDAO) ListByOwnerIDs(ctx context.Context, db *gorm.DB, ownerIDs []string, userID string, page, pageSize int, terms []OrderTerm, keywords string) ([]*entity.ChatListItem, int64, error) {var chats []*entity.ChatListItem+ var total int64- // Build query with join to user table+ // Build query with join to user table for nickname and avatarquery := db.WithContext(ctx).Model(&entity.Chat{}).Select(`dialog.*,@@ -135,12 +136,22 @@ func (dao *ChatDAO) ListByOwnerIDs(ctx context.Context, db *gorm.DB, ownerIDs []// codeql[go/sql-injection] False positive: chatOrderClausequery = query.Order(chatOrderClause(terms))- // Get all matching records- if err := query.Scan(&chats).Error; err != nil {+ // Count total+ if err := query.Count(&total).Error; err != nil {return nil, 0, err}- total := int64(len(chats))+ // Apply pagination+ if page > 0 && pageSize > 0 {+ offset := (page - 1) * pageSize+ if err := query.Offset(offset).Limit(pageSize).Scan(&chats).Error; err != nil {+ return nil, 0, err+ }+ } else {+ if err := query.Scan(&chats).Error; err != nil {+ return nil, 0, err+ }+ }return chats, total, nil}diff --git a/internal/dao/chat_test.go b/internal/dao/chat_test.goindex 93fdddc123..e95ee0a248 100644--- a/internal/dao/chat_test.go+++ b/internal/dao/chat_test.go@@ -17,6 +17,7 @@package daoimport (+ "fmt""testing""github.com/glebarez/sqlite"@@ -121,7 +122,7 @@ func TestChatDAOListByOwnerIDsOrderByExpressionFallsBack(t *testing.T) {for _, orderby := range chatOrderExpressions {t.Run(orderby, func(t *testing.T) {- rows, _, err := d.ListByOwnerIDs(ctx, db, []string{"t1"}, "t1", []OrderTerm{{Column: orderby}}, "")+ rows, _, err := d.ListByOwnerIDs(ctx, db, []string{"t1"}, "t1", 1, 10, []OrderTerm{{Column: orderby}}, "")if err != nil {t.Fatalf("ListByOwnerIDs with orderby %q: %v", orderby, err)}@@ -151,3 +152,120 @@ func TestChatDAOListOrderByAllowedColumn(t *testing.T) {}assertChatOrder(t, rows, []string{"c-1", "c-2", "c-3"}, "name")}++// assertChatIDs compares the returned row IDs against wantIDs, naming the call+// under test in the failure message. assertChatOrder is for the ORDER BY+// allowlist tests, whose failure message talks about expressions reaching the+// ORDER BY clause.+func assertChatIDs(t *testing.T, rows []*entity.ChatListItem, wantIDs []string, call string) {+ t.Helper()+ gotIDs := make([]string, 0, len(rows))+ for _, row := range rows {+ gotIDs = append(gotIDs, row.ID)+ }+ if len(gotIDs) != len(wantIDs) {+ t.Fatalf("%s returned %v, want %v", call, gotIDs, wantIDs)+ }+ for i := range gotIDs {+ if gotIDs[i] != wantIDs[i] {+ t.Fatalf("%s returned %v, want %v", call, gotIDs, wantIDs)+ }+ }+}++// seedChatRowsWithCreateTime creates count chats owned by tenantID whose+// create_time grows with their index, so page boundaries are deterministic.+func seedChatRowsWithCreateTime(t *testing.T, db *gorm.DB, tenantID string, count int) {+ t.Helper()+ for i := 1; i <= count; i++ {+ name := fmt.Sprintf("chat-%d", i)+ createTime := int64(i * 100)+ chat := entity.Chat{+ ID: fmt.Sprintf("c-%d", i),+ TenantID: tenantID,+ Name: &name,+ LLMID: "llm-1",+ LLMSetting: entity.JSONMap{},+ PromptType: "simple",+ PromptConfig: entity.JSONMap{},+ DoRefer: "1",+ KBIDs: entity.JSONSlice{},+ Status: stringPtr("1"),+ BaseModel: entity.BaseModel{CreateTime: &createTime},+ }+ if err := db.Create(&chat).Error; err != nil {+ t.Fatalf("failed to create chat %s: %v", chat.ID, err)+ }+ }+}++// TestChatDAOListByOwnerIDsPaginatesInSQL checks that page/pageSize reach the+// query as LIMIT/OFFSET while total keeps reporting the full COUNT(*).+// ChatService.ListChats returns that total as the response's total field and no+// longer slices the returned rows itself.+func TestChatDAOListByOwnerIDsPaginatesInSQL(t *testing.T) {+ db := setupChatTestDB(t)+ pushDB(t, db)+ seedChatRowsWithCreateTime(t, db, "t1", 5)+ ctx := t.Context()+ d := NewChatDAO()+ terms := []OrderTerm{{Column: "create_time", Desc: true}}++ tests := []struct {+ name string+ page int+ pageSize int+ wantIDs []string+ }{+ {name: "first page", page: 1, pageSize: 2, wantIDs: []string{"c-5", "c-4"}},+ {name: "middle page", page: 2, pageSize: 2, wantIDs: []string{"c-3", "c-2"}},+ {name: "partial last page", page: 3, pageSize: 2, wantIDs: []string{"c-1"}},+ {name: "page past the end", page: 4, pageSize: 2, wantIDs: []string{}},+ {name: "unpaginated returns every row", page: 0, pageSize: 0, wantIDs: []string{"c-5", "c-4", "c-3", "c-2", "c-1"}},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ rows, total, err := d.ListByOwnerIDs(ctx, db, []string{"t1"}, "t1", tt.page, tt.pageSize, terms, "")+ if err != nil {+ t.Fatalf("ListByOwnerIDs(page=%d, pageSize=%d): %v", tt.page, tt.pageSize, err)+ }+ if total != 5 {+ t.Fatalf("total = %d, want 5: total must count every matching row, not just the page", total)+ }+ assertChatIDs(t, rows, tt.wantIDs, fmt.Sprintf("page=%d pageSize=%d", tt.page, tt.pageSize))+ })+ }+}++// TestChatDAOListByOwnerIDsKeepsJoinedOwnerColumnsWhenPaginated guards the COUNT+// followed by the paged Scan: the second statement must still select the joined+// user columns instead of inheriting count(*) from the first one.+func TestChatDAOListByOwnerIDsKeepsJoinedOwnerColumnsWhenPaginated(t *testing.T) {+ db := setupChatTestDB(t)+ pushDB(t, db)+ seedChatRowsWithCreateTime(t, db, "t1", 3)+ ctx := t.Context()++ if err := db.Create(&entity.User{ID: "t1", Nickname: "owner of t1", Status: stringPtr("1")}).Error; err != nil {+ t.Fatalf("failed to create user: %v", err)+ }++ rows, total, err := NewChatDAO().ListByOwnerIDs(ctx, db, []string{"t1"}, "t1", 1, 2, []OrderTerm{{Column: "create_time", Desc: true}}, "")+ if err != nil {+ t.Fatalf("ListByOwnerIDs: %v", err)+ }+ if total != 3 {+ t.Fatalf("total = %d, want 3", total)+ }+ if len(rows) != 2 {+ t.Fatalf("got %d rows, want 2", len(rows))+ }+ for i, row := range rows {+ if row.Name == nil || *row.Name == "" || row.TenantID != "t1" {+ t.Fatalf("row %d = %+v, want the chat columns of tenant t1", i, row.Chat)+ }+ if row.Nickname == nil || *row.Nickname != "owner of t1" {+ t.Fatalf("row %d nickname = %v, want owner of t1: the paged scan dropped the joined owner column", i, row.Nickname)+ }+ }+}diff --git a/internal/service/chat.go b/internal/service/chat.goindex d0227b0624..1bdd0fe4b5 100644--- a/internal/service/chat.go+++ b/internal/service/chat.go@@ -129,23 +129,10 @@ func (s *ChatService) ListChats(ctx context.Context, userID, status, keywords st}, nil}- chats, total, err = s.chatDAO.ListByOwnerIDs(ctx, dao.DB, filterOwnerIDs, userID, terms, keywords)+ chats, total, err = s.chatDAO.ListByOwnerIDs(ctx, dao.DB, filterOwnerIDs, userID, page, pageSize, terms, keywords)if err != nil {return nil, err}-- if page > 0 && pageSize > 0 {- start := (page - 1) * pageSize- end := start + pageSize- if start < int(total) {- if end > int(total) {- end = int(total)- }- chats = chats[start:end]- } else {- chats = []*entity.ChatListItem{}- }- }}// Enrich with knowledge base namesdiff --git a/internal/service/chat_list_test.go b/internal/service/chat_list_test.goindex bffd266a0a..0e278a71c2 100644--- a/internal/service/chat_list_test.go+++ b/internal/service/chat_list_test.go@@ -1,6 +1,7 @@package serviceimport (+ "fmt""testing""github.com/glebarez/sqlite"@@ -227,3 +228,60 @@ func TestChatServiceListChatsExcludesDeletedChats(t *testing.T) {t.Fatalf("expected active_chat, got %+v", result.Chats[0].Name)}}++// TestChatServiceListChatsPaginatesOwnerBranch covers the owner_ids branch end+// to end: pagination happens in SQL now, so each page must carry only its own+// rows while total still reports every matching chat.+func TestChatServiceListChatsPaginatesOwnerBranch(t *testing.T) {+ db := setupChatListTestDB(t)++ for i := 1; i <= 5; i++ {+ name := fmt.Sprintf("owner_page_%d", i)+ createTime := int64(i * 100)+ status := string(entity.StatusValid)+ chat := &entity.Chat{+ ID: fmt.Sprintf("chat-%d", i),+ TenantID: "user-1",+ Name: &name,+ LLMID: "model-a",+ LLMSetting: entity.JSONMap{},+ PromptType: "simple",+ PromptConfig: entity.JSONMap{},+ KBIDs: entity.JSONSlice{},+ Status: &status,+ BaseModel: entity.BaseModel{CreateTime: &createTime},+ }+ if err := db.Create(chat).Error; err != nil {+ t.Fatalf("failed to create chat %d: %v", i, err)+ }+ }++ tests := []struct {+ name string+ page int+ pageSize int+ wantIDs []string+ }{+ {name: "middle page", page: 2, pageSize: 2, wantIDs: []string{"chat-3", "chat-2"}},+ {name: "unpaginated returns every row", page: 0, pageSize: 0, wantIDs: []string{"chat-5", "chat-4", "chat-3", "chat-2", "chat-1"}},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ result, err := NewChatService().ListChats(t.Context(), "user-1", "1", "", tt.page, tt.pageSize, []dao.OrderTerm{{Column: "create_time", Desc: true}}, []string{"user-1"})+ if err != nil {+ t.Fatalf("ListChats failed: %v", err)+ }+ if result.Total != 5 {+ t.Fatalf("total = %d, want 5", result.Total)+ }+ if len(result.Chats) != len(tt.wantIDs) {+ t.Fatalf("got %d chats, want %d", len(result.Chats), len(tt.wantIDs))+ }+ for i, chat := range result.Chats {+ if chat.ID != tt.wantIDs[i] {+ t.Fatalf("chat %d id = %v, want %s", i, chat.ID, tt.wantIDs[i])+ }+ }+ })+ }+}