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 <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-04-10 14:33:32 +08:00
committed by GitHub
parent 18cafff790
commit 3d59448b0d
2 changed files with 41 additions and 2 deletions

View File

@@ -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,

View File

@@ -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 {