Skip to content

Commit 9af9431

Browse files
refactor: extract the pagination logic (#242)
1 parent 74c6d6d commit 9af9431

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/McpResponse.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,30 +210,12 @@ Call ${handleDialog.name} to handle it before continuing.`);
210210

211211
response.push('## Network requests');
212212
if (requests.length) {
213-
const paginationResult = paginate(
213+
const data = this.#dataWithPagination(
214214
requests,
215215
this.#networkRequestsOptions.pagination,
216216
);
217-
if (paginationResult.invalidPage) {
218-
response.push('Invalid page number provided. Showing first page.');
219-
}
220-
221-
const {startIndex, endIndex, currentPage, totalPages} =
222-
paginationResult;
223-
response.push(
224-
`Showing ${startIndex + 1}-${endIndex} of ${requests.length} (Page ${currentPage + 1} of ${totalPages}).`,
225-
);
226-
227-
if (this.#networkRequestsOptions.pagination) {
228-
if (paginationResult.hasNextPage) {
229-
response.push(`Next page: ${currentPage + 1}`);
230-
}
231-
if (paginationResult.hasPreviousPage) {
232-
response.push(`Previous page: ${currentPage - 1}`);
233-
}
234-
}
235-
236-
for (const request of paginationResult.items) {
217+
response.push(...data.info);
218+
for (const request of data.items) {
237219
response.push(getShortDescriptionForRequest(request));
238220
}
239221
} else {
@@ -264,6 +246,32 @@ Call ${handleDialog.name} to handle it before continuing.`);
264246
return [text, ...images];
265247
}
266248

249+
#dataWithPagination<T>(data: T[], pagination?: PaginationOptions) {
250+
const response = [];
251+
const paginationResult = paginate<T>(data, pagination);
252+
if (paginationResult.invalidPage) {
253+
response.push('Invalid page number provided. Showing first page.');
254+
}
255+
256+
const {startIndex, endIndex, currentPage, totalPages} = paginationResult;
257+
response.push(
258+
`Showing ${startIndex + 1}-${endIndex} of ${data.length} (Page ${currentPage + 1} of ${totalPages}).`,
259+
);
260+
if (pagination) {
261+
if (paginationResult.hasNextPage) {
262+
response.push(`Next page: ${currentPage + 1}`);
263+
}
264+
if (paginationResult.hasPreviousPage) {
265+
response.push(`Previous page: ${currentPage - 1}`);
266+
}
267+
}
268+
269+
return {
270+
info: response,
271+
items: paginationResult.items,
272+
};
273+
}
274+
267275
#getIncludeNetworkRequestsData(context: McpContext): string[] {
268276
const response: string[] = [];
269277
const url = this.#attachedNetworkRequestUrl;

0 commit comments

Comments
 (0)