Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 5fee4a2

Browse files
feat: add pagination for thread list api (#1000)
1 parent fcbf842 commit 5fee4a2

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

cortex-js/src/infrastructure/controllers/threads.controller.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ export class ThreadsController {
4444
'Lists all the available threads along with its configurations.',
4545
})
4646
@Get()
47-
findAll() {
48-
return this.threadsUsecases.findAll();
47+
findAll(
48+
@Query('limit', new DefaultValuePipe(20)) limit: number,
49+
@Query('order', new DefaultValuePipe('desc')) order: 'asc' | 'desc',
50+
@Query('after') after?: string,
51+
@Query('before') before?: string,
52+
) {
53+
return this.threadsUsecases.findAll(limit, order, after, before);
4954
}
5055

5156
@ApiOperation({

cortex-js/src/usecases/threads/threads.usecases.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,69 @@ export class ThreadsUsecases {
5151
return this.threadRepository.create(thread);
5252
}
5353

54-
async findAll(): Promise<Thread[]> {
55-
return this.threadRepository.findAll({
56-
include: [{ all: true }],
57-
order: [['created_at', 'DESC']],
54+
async findAll(
55+
limit: number,
56+
order: 'asc' | 'desc',
57+
after?: string,
58+
before?: string,
59+
): Promise<PageDto<Thread>> {
60+
const normalizedOrder = order === 'asc' ? 'ASC' : 'DESC';
61+
let afterQuery = {};
62+
let beforeQuery = {};
63+
if (after) {
64+
const [afterDate, afterId] = after.split('_');
65+
const operator = order === 'asc' ? Op.gt : Op.lt;
66+
afterQuery = {
67+
[Op.or]: [
68+
{
69+
created_at: { [operator]: Number(afterDate) },
70+
},
71+
{
72+
created_at: Number(afterDate),
73+
id: { [operator]: afterId },
74+
},
75+
],
76+
};
77+
}
78+
if (before) {
79+
const [beforeDate, beforeId] = before.split('_');
80+
const operator = order === 'asc' ? Op.lt : Op.gt;
81+
beforeQuery = {
82+
[Op.or]: [
83+
{
84+
created_at: { [operator]: Number(beforeDate) },
85+
},
86+
{
87+
created_at: Number(beforeDate),
88+
id: { [operator]: beforeId },
89+
},
90+
],
91+
};
92+
}
93+
const threads = await this.threadRepository.findAll({
94+
order: [
95+
['created_at', normalizedOrder],
96+
['id', normalizedOrder],
97+
],
98+
limit: limit + 1,
99+
where: {
100+
[Op.and]: [afterQuery, beforeQuery],
101+
},
58102
});
103+
let hasMore = false;
104+
if (threads.length > limit) {
105+
hasMore = true;
106+
threads.pop();
107+
}
108+
const firstItem = threads[0];
109+
const lastItem = threads[threads.length - 1];
110+
const firstId = firstItem
111+
? `${firstItem.created_at}_${firstItem.id}`
112+
: undefined;
113+
const lastId = lastItem
114+
? `${lastItem?.created_at}_${lastItem?.id}`
115+
: undefined;
116+
return new PageDto(threads, hasMore, firstId, lastId);
59117
}
60118

61119
async getMessagesOfThread(

0 commit comments

Comments
 (0)