From 3d59448b0d364ce85fe5f9123dd19f972d3fe956 Mon Sep 17 00:00:00 2001 From: Jin Hai Date: Fri, 10 Apr 2026 14:33:32 +0800 Subject: [PATCH] Go: add parameter parsing of list chats (#14026) ### What problem does this PR solve? As title. ### Type of change - [x] New Feature (non-breaking change which adds functionality) Signed-off-by: Jin Hai --- internal/handler/chat.go | 26 +++++++++++++++++++++++++- internal/service/chat.go | 17 ++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/internal/handler/chat.go b/internal/handler/chat.go index 4fa7928126..186763cbcc 100644 --- a/internal/handler/chat.go +++ b/internal/handler/chat.go @@ -56,8 +56,32 @@ func (h *ChatHandler) ListChats(c *gin.Context) { } userID := user.ID + // Parse query parameters + keywords := c.Query("keywords") + + page := 0 + if pageStr := c.Query("page"); pageStr != "" { + if p, err := strconv.Atoi(pageStr); err == nil && p > 0 { + page = p + } + } + + pageSize := 0 + if pageSizeStr := c.Query("page_size"); pageSizeStr != "" { + if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 { + pageSize = ps + } + } + + orderby := c.DefaultQuery("orderby", "create_time") + + desc := true + if descStr := c.Query("desc"); descStr != "" { + desc = descStr != "false" + } + // List chats - default to valid status "1" (same as Python StatusEnum.VALID.value) - result, err := h.chatService.ListChats(userID, "1") + result, err := h.chatService.ListChats(userID, keywords, "1", page, pageSize, orderby, desc) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, diff --git a/internal/service/chat.go b/internal/service/chat.go index d02ffd200f..832154ffd7 100644 --- a/internal/service/chat.go +++ b/internal/service/chat.go @@ -59,7 +59,7 @@ type ListChatsResponse struct { } // ListChats list chats for a user -func (s *ChatService) ListChats(userID string, status string) (*ListChatsResponse, error) { +func (s *ChatService) ListChats(userID, status, keywords string, page, pageSize int, orderby string, desc bool) (*ListChatsResponse, error) { // Get tenant IDs by user ID tenantIDs, err := s.userTenantDAO.GetTenantIDsByUserID(userID) if err != nil { @@ -81,6 +81,21 @@ func (s *ChatService) ListChats(userID string, status string) (*ListChatsRespons return nil, err } + total := int64(len(chats)) + + 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.Chat{} + } + } + // Enrich with knowledge base names chatsWithKBNames := make([]*ChatWithKBNames, 0, len(chats)) for _, chat := range chats {