Improves model list pagination with safety measures

Adds robust pagination handling for Anthropic model retrieval:
- Implements duplicate model filtering
- Adds iteration limit to prevent infinite loops
- Introduces cursor advancement validation
- Fixes potential API pagination edge cases

Ensures more reliable and predictable model list retrieval
This commit is contained in:
AlexKucera committed 2025-10-24 16:56:38 +02:00
1 parent b57fe8cd89
commit 3f53bb5a30
1 file changed
+28 -4
+28 -4
View File
@@ -130,19 +130,30 @@ export class AnthropicClient {
try { try {
// Accumulate all models across pages // Accumulate all models across pages
const allModels: ModelInfo[] = []; const allModels: ModelInfo[] = [];
// Track seen model IDs to filter duplicates
const seenIds = new Set<string>();
let hasMore = true; let hasMore = true;
let afterId: string | undefined = undefined; let afterId: string | undefined = undefined;
let firstId = ''; let firstId = '';
let lastId = ''; let lastId = '';
// Loop safety: prevent runaway pagination
let iterations = 0;
const maxIterations = 1000;
// Fetch all pages // Fetch all pages with safety guards
while (hasMore) { while (hasMore) {
// Guard against infinite loops
iterations++;
if (iterations > maxIterations) {
throw new Error(`Pagination exceeded maximum iterations (${maxIterations}). Possible API issue.`);
}
// Build URL with pagination parameters // Build URL with pagination parameters
// Use limit=1000 (Anthropic's max) to minimize round trips // Use limit=1000 (Anthropic's max) to minimize round trips
const params = new URLSearchParams(); const params = new URLSearchParams();
params.set('limit', '1000'); params.set('limit', '1000');
if (afterId) { if (afterId) {
params.set('after', afterId); params.set('after_id', afterId);
} }
const url = `${this.modelsUrl}?${params.toString()}`; const url = `${this.modelsUrl}?${params.toString()}`;
@@ -175,8 +186,13 @@ export class AnthropicClient {
const pageResponse = data as ModelsListResponse; const pageResponse = data as ModelsListResponse;
// Accumulate models from this page // Accumulate models from this page, filtering out duplicates
allModels.push(...pageResponse.data); for (const model of pageResponse.data) {
if (!seenIds.has(model.id)) {
seenIds.add(model.id);
allModels.push(model);
}
}
// Store first_id from the first page // Store first_id from the first page
if (!firstId) { if (!firstId) {
@@ -184,9 +200,17 @@ export class AnthropicClient {
} }
// Update pagination state // Update pagination state
const previousAfterId: string | undefined = afterId;
lastId = pageResponse.last_id; lastId = pageResponse.last_id;
hasMore = pageResponse.has_more; hasMore = pageResponse.has_more;
// Guard against cursor not advancing (would cause infinite loop)
if (!lastId || lastId === previousAfterId) {
// Cursor didn't advance - break to prevent infinite loop
console.warn('Pagination cursor did not advance. Stopping pagination.');
break;
}
// Set up for next iteration // Set up for next iteration
if (hasMore) { if (hasMore) {
afterId = lastId; afterId = lastId;