feat(coderd): add pagination to chat list endpoint (#22887)

Adds offset and cursor-based pagination to the `GET
/api/experimental/chats` endpoint, following the exact same patterns
used by `GetUsers` and `GetTemplateVersionsByTemplateID`.

## Changes

### Database
- Add `after_id`, `offset_opt`, `limit_opt` params to
`GetChatsByOwnerID` SQL query
- Use composite `(updated_at, id) DESC` cursor for stable, deterministic
pagination
- Add migration with composite index on `chats (owner_id, updated_at
DESC, id DESC)`

### Backend
- Use `ParsePagination()` in `listChats` handler (matches `users.go`
pattern)
- Add `Pagination` field to `ListChatsOptions` SDK struct

### Frontend
- Add `infiniteChats()` query factory using `useInfiniteQuery` with
offset-based page params (same pattern as `infiniteWorkspaceBuilds`)
- Update `AgentsPage` to use `useInfiniteQuery`
- Add "Show more" button at the bottom of the agents sidebar (matches
`HistorySidebar` pattern)
- Keep existing `chats()` query for non-paginated uses (e.g., parent
chat lookup in `AgentDetail`)

### Tests
- Add `TestListChats/Pagination` covering `limit`, `after_id` cursor,
`offset`, and no-limit behavior
This commit is contained in:
Kyle Carberry
2026-03-10 13:55:33 +00:00
committed by GitHub
parent beed379b1d
commit e18ce505ec
14 changed files with 244 additions and 19 deletions
+10
View File
@@ -179,8 +179,18 @@ func (api *API) listChats(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
apiKey := httpmw.APIKey(r)
paginationParams, ok := ParsePagination(rw, r)
if !ok {
return
}
params := database.GetChatsByOwnerIDParams{
OwnerID: apiKey.UserID,
AfterID: paginationParams.AfterID,
// #nosec G115 - Pagination offsets are small and fit in int32
OffsetOpt: int32(paginationParams.Offset),
// #nosec G115 - Pagination limits are small and fit in int32
LimitOpt: int32(paginationParams.Limit),
}
if v := r.URL.Query().Get("archived"); v != "" {
b, err := strconv.ParseBool(v)
+84
View File
@@ -387,6 +387,90 @@ func TestListChats(t *testing.T) {
_, err := unauthenticatedClient.ListChats(ctx, nil)
requireSDKError(t, err, http.StatusUnauthorized)
})
t.Run("Pagination", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
client, _ := newChatClientWithDatabase(t)
_ = coderdtest.CreateFirstUser(t, client)
_ = createChatModelConfig(t, client)
// Create 5 chats.
const totalChats = 5
createdChats := make([]codersdk.Chat, 0, totalChats)
for i := 0; i < totalChats; i++ {
chat, err := client.CreateChat(ctx, codersdk.CreateChatRequest{
Content: []codersdk.ChatInputPart{
{
Type: codersdk.ChatInputPartTypeText,
Text: fmt.Sprintf("chat-%d", i),
},
},
})
require.NoError(t, err)
createdChats = append(createdChats, chat)
}
// Fetch first page with limit=2.
page1, err := client.ListChats(ctx, &codersdk.ListChatsOptions{
Pagination: codersdk.Pagination{Limit: 2},
})
require.NoError(t, err)
require.Len(t, page1, 2)
// Fetch second page using after_id from last item of page 1.
page2, err := client.ListChats(ctx, &codersdk.ListChatsOptions{
Pagination: codersdk.Pagination{
AfterID: uuid.MustParse(page1[len(page1)-1].ID.String()),
Limit: 2,
},
})
require.NoError(t, err)
require.Len(t, page2, 2)
// Ensure page1 and page2 have no overlap.
page1IDs := make(map[uuid.UUID]struct{})
for _, c := range page1 {
page1IDs[c.ID] = struct{}{}
}
for _, c := range page2 {
_, overlap := page1IDs[c.ID]
require.False(t, overlap, "page2 should not contain items from page1")
}
// Fetch third page — should have 1 remaining chat.
page3, err := client.ListChats(ctx, &codersdk.ListChatsOptions{
Pagination: codersdk.Pagination{
AfterID: uuid.MustParse(page2[len(page2)-1].ID.String()),
Limit: 2,
},
})
require.NoError(t, err)
require.Len(t, page3, 1)
// All 5 chats should be accounted for.
allIDs := make(map[uuid.UUID]struct{})
for _, c := range append(append(page1, page2...), page3...) {
allIDs[c.ID] = struct{}{}
}
for _, c := range createdChats {
_, found := allIDs[c.ID]
require.True(t, found, "chat %s should appear in paginated results", c.ID)
}
// Fetch with offset=3, limit=2 — should return 2 chats.
offsetPage, err := client.ListChats(ctx, &codersdk.ListChatsOptions{
Pagination: codersdk.Pagination{Offset: 3, Limit: 2},
})
require.NoError(t, err)
require.Len(t, offsetPage, 2)
// No limit should return all chats.
allChats, err := client.ListChats(ctx, nil)
require.NoError(t, err)
require.Len(t, allChats, totalChats)
})
}
func TestListChatModels(t *testing.T) {
+2
View File
@@ -3534,6 +3534,8 @@ CREATE INDEX idx_chats_last_model_config_id ON chats USING btree (last_model_con
CREATE INDEX idx_chats_owner ON chats USING btree (owner_id);
CREATE INDEX idx_chats_owner_updated_id ON chats USING btree (owner_id, updated_at DESC, id DESC);
CREATE INDEX idx_chats_parent_chat_id ON chats USING btree (parent_chat_id);
CREATE INDEX idx_chats_pending ON chats USING btree (status) WHERE (status = 'pending'::chat_status);
@@ -0,0 +1 @@
DROP INDEX IF EXISTS idx_chats_owner_updated_id;
@@ -0,0 +1 @@
CREATE INDEX idx_chats_owner_updated_id ON chats (owner_id, updated_at DESC, id DESC);
+38 -4
View File
@@ -3454,17 +3454,51 @@ WHERE
WHEN $2 :: boolean IS NULL THEN true
ELSE chats.archived = $2 :: boolean
END
AND CASE
-- This allows using the last element on a page as effectively a cursor.
-- This is an important option for scripts that need to paginate without
-- duplicating or missing data.
WHEN $3 :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN (
-- The pagination cursor is the last ID of the previous page.
-- The query is ordered by the updated_at field, so select all
-- rows before the cursor.
(updated_at, id) < (
SELECT
updated_at, id
FROM
chats
WHERE
id = $3
)
)
ELSE true
END
ORDER BY
updated_at DESC
-- Deterministic and consistent ordering of all rows, even if they share
-- a timestamp. This is to ensure consistent pagination.
(updated_at, id) DESC OFFSET $4
LIMIT
-- The chat list is unbounded and expected to grow large.
-- Default to 50 to prevent accidental excessively large queries.
COALESCE(NULLIF($5 :: int, 0), 50)
`
type GetChatsByOwnerIDParams struct {
OwnerID uuid.UUID `db:"owner_id" json:"owner_id"`
Archived sql.NullBool `db:"archived" json:"archived"`
OwnerID uuid.UUID `db:"owner_id" json:"owner_id"`
Archived sql.NullBool `db:"archived" json:"archived"`
AfterID uuid.UUID `db:"after_id" json:"after_id"`
OffsetOpt int32 `db:"offset_opt" json:"offset_opt"`
LimitOpt int32 `db:"limit_opt" json:"limit_opt"`
}
func (q *sqlQuerier) GetChatsByOwnerID(ctx context.Context, arg GetChatsByOwnerIDParams) ([]Chat, error) {
rows, err := q.db.QueryContext(ctx, getChatsByOwnerID, arg.OwnerID, arg.Archived)
rows, err := q.db.QueryContext(ctx, getChatsByOwnerID,
arg.OwnerID,
arg.Archived,
arg.AfterID,
arg.OffsetOpt,
arg.LimitOpt,
)
if err != nil {
return nil, err
}
+26 -1
View File
@@ -113,8 +113,33 @@ WHERE
WHEN sqlc.narg('archived') :: boolean IS NULL THEN true
ELSE chats.archived = sqlc.narg('archived') :: boolean
END
AND CASE
-- This allows using the last element on a page as effectively a cursor.
-- This is an important option for scripts that need to paginate without
-- duplicating or missing data.
WHEN @after_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN (
-- The pagination cursor is the last ID of the previous page.
-- The query is ordered by the updated_at field, so select all
-- rows before the cursor.
(updated_at, id) < (
SELECT
updated_at, id
FROM
chats
WHERE
id = @after_id
)
)
ELSE true
END
ORDER BY
updated_at DESC;
-- Deterministic and consistent ordering of all rows, even if they share
-- a timestamp. This is to ensure consistent pagination.
(updated_at, id) DESC OFFSET @offset_opt
LIMIT
-- The chat list is unbounded and expected to grow large.
-- Default to 50 to prevent accidental excessively large queries.
COALESCE(NULLIF(@limit_opt :: int, 0), 50);
-- name: ListChildChatsByParentID :many
SELECT