Merge pull request #1377 from trheyi/main

Refactor ListChats response handling to support grouping
This commit is contained in:
Max 2025-12-10 17:15:03 +08:00 committed by GitHub
commit bedd98eb52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 7 deletions

View file

@ -1673,6 +1673,11 @@ GET /v1/chat/sessions/chat_123/messages?limit=100&offset=0&role=assistant&type=t
| `type` | string | - | Filter by message type | | `type` | string | - | Filter by message type |
| `limit` | int | 100 | Max messages to return (max 1000) | | `limit` | int | 100 | Max messages to return (max 1000) |
| `offset` | int | 0 | Offset for pagination | | `offset` | int | 0 | Offset for pagination |
| `locale` | string | - | Locale for assistant info (e.g., `zh-cn`, `en-us`). Falls back to `Accept-Language` header |
**Locale Resolution Priority:**
1. Query parameter `locale`
2. HTTP header `Accept-Language`
**Response:** **Response:**
@ -1708,10 +1713,20 @@ GET /v1/chat/sessions/chat_123/messages?limit=100&offset=0&role=assistant&type=t
"created_at": "2024-01-15T10:00:05Z" "created_at": "2024-01-15T10:00:05Z"
} }
], ],
"count": 2 "count": 2,
"assistants": {
"weather_assistant": {
"assistant_id": "weather_assistant",
"name": "Weather Assistant",
"avatar": "https://example.com/weather-avatar.png",
"description": "Get weather information for any location"
}
}
} }
``` ```
**Note:** The `assistants` field contains localized assistant information (name, avatar, description) for all unique `assistant_id` values found in the messages. This allows the frontend to display assistant details without additional API calls. The locale is determined by the `locale` query parameter or `Accept-Language` header.
### Permission Filtering ### Permission Filtering
All endpoints respect Yao's permission system: All endpoints respect Yao's permission system:

View file

@ -49,15 +49,24 @@ func ListChats(c *gin.Context) {
return return
} }
// Return result // Build response based on grouping mode
response.RespondWithSuccess(c, response.StatusOK, gin.H{ // When group_by is set, data should be nil to avoid duplication
"data": result.Data, resp := gin.H{
"groups": result.Groups,
"page": result.Page, "page": result.Page,
"pagesize": result.PageSize, "pagesize": result.PageSize,
"pagecount": result.PageCount, "pagecount": result.PageCount,
"total": result.Total, "total": result.Total,
}) }
if len(result.Groups) > 0 {
// Grouped response: only include groups
resp["groups"] = result.Groups
} else {
// Flat response: only include data
resp["data"] = result.Data
}
response.RespondWithSuccess(c, response.StatusOK, resp)
} }
// GetChat retrieves a single chat session by ID // GetChat retrieves a single chat session by ID

View file

@ -300,9 +300,11 @@ func TestListChatSessions(t *testing.T) {
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&response)
assert.NoError(t, err) assert.NoError(t, err)
// Check for groups in response // Check for groups in response (when group_by=time, only groups is returned, not data)
_, hasGroups := response["groups"] _, hasGroups := response["groups"]
_, hasData := response["data"]
assert.True(t, hasGroups, "Response should contain groups when group_by=time") assert.True(t, hasGroups, "Response should contain groups when group_by=time")
assert.False(t, hasData, "Response should NOT contain data when group_by=time (to avoid duplication)")
t.Logf("Successfully retrieved chat sessions with time grouping") t.Logf("Successfully retrieved chat sessions with time grouping")
}) })