Adds dynamic Claude model fetching and management #1

Merged
AlexKucera merged 60 commits from dynamic-models into master 2025-10-31 13:42:04 +01:00
Showing only changes of commit 329c12ab4b - Show all commits
+11 -5
View File
@@ -17,10 +17,14 @@ import { AnthropicRequest, AnthropicResponse, ModelInfo, ModelsListResponse } fr
*/ */
export class AnthropicClient { export class AnthropicClient {
private apiKey: string; private apiKey: string;
/** Anthropic API base URL - centralized to avoid drift */
private readonly API_BASE = 'https://api.anthropic.com/v1';
/** Anthropic API version - centralized for consistency across all requests */
private readonly API_VERSION = '2023-06-01';
/** Base URL for the Anthropic Messages API endpoint */ /** Base URL for the Anthropic Messages API endpoint */
private messagesUrl = 'https://api.anthropic.com/v1/messages'; private messagesUrl = `${this.API_BASE}/messages`;
/** Base URL for the Anthropic Models API endpoint */ /** Base URL for the Anthropic Models API endpoint */
private modelsUrl = 'https://api.anthropic.com/v1/models'; private modelsUrl = `${this.API_BASE}/models`;
/** /**
* Suffix appended to all system prompts to enforce consistent output format. * Suffix appended to all system prompts to enforce consistent output format.
3
@@ -147,7 +151,7 @@ export class AnthropicClient {
method: 'GET', method: 'GET',
headers: { headers: {
'x-api-key': this.apiKey, 'x-api-key': this.apiKey,
'anthropic-version': '2023-06-01' 'anthropic-version': this.API_VERSION
} }
}); });
@@ -244,7 +248,7 @@ export class AnthropicClient {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'x-api-key': this.apiKey, 'x-api-key': this.apiKey,
'anthropic-version': '2023-06-01' 'anthropic-version': this.API_VERSION
}, },
coderabbitai[bot] commented 2025-10-24 13:55:13 +02:00 (Migrated from github.com)
Review

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Critical: Pagination uses after instead of after_id — can cause infinite loop/duplicate pages.

Anthropic’s models pagination uses before_id/after_id. Using after likely re-fetches the first page forever if has_more is true, looping and duplicating results.

Apply this diff:

-                if (afterId) {
-                    params.set('after', afterId);
-                }
+                if (afterId) {
+                    params.set('after_id', afterId);
+                }

Please confirm param naming against current docs:


🌐 Web query:

Does Anthropic's List Models endpoint use the query param name `after_id` (not `after`) for pagination?

💡 Result:

Yes — the List Models endpoint uses the query parameter after_id (not after) for pagination. [1][2]

Sources:

  • Anthropic API — List Models documentation. [1]
  • Claude (Anthropic) localized docs — List Models (shows after_id). [2]

🏁 Script executed:

#!/bin/bash
# Verify the pagination bug in api/anthropicClient.ts
fd api/anthropicClient.ts --exec cat {}

Length of output: 0


🏁 Script executed:

#!/bin/bash
# Search for anthropicClient file with broader pattern
fd -e ts anthropicClient

Length of output: 22


🏁 Script executed:

#!/bin/bash
# View the anthropicClient.ts file with line numbers, focusing on pagination section
cat -n api/anthropicClient.ts | sed -n '130,150p'

Length of output: 976


🏁 Script executed:

#!/bin/bash
# View complete pagination loop
cat -n api/anthropicClient.ts | sed -n '135,190p'

Length of output: 2700


Critical: Fix pagination parameter from after to after_id at line 141.

Anthropic's List Models endpoint uses the query parameter after_id (not after) for pagination. The current code at line 141 uses the incorrect parameter name, which will cause the API to ignore the pagination cursor and potentially loop indefinitely or return duplicate results.

                if (afterId) {
-                    params.set('after', afterId);
+                    params.set('after_id', afterId);
                }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

                // Build URL with pagination parameters
                // Use limit=1000 (Anthropic's max) to minimize round trips
                const params = new URLSearchParams();
                params.set('limit', '1000');
                if (afterId) {
                    params.set('after_id', afterId);
                }
                const url = `${this.modelsUrl}?${params.toString()}`;
🤖 Prompt for AI Agents
In api/anthropicClient.ts around lines 136 to 144, the code sets the pagination
query parameter as 'after' which is incorrect for Anthropic's List Models
endpoint; change the parameter key to 'after_id' when afterId is present (i.e.,
params.set('after_id', afterId)) so the pagination cursor is passed correctly,
leaving the rest of the URL construction and limit=1000 logic unchanged.

✅ Addressed in commits afa725d to 3f53bb5

_⚠️ Potential issue_ | _🔴 Critical_ <details> <summary>🧩 Analysis chain</summary> **Critical: Pagination uses `after` instead of `after_id` — can cause infinite loop/duplicate pages.** Anthropic’s models pagination uses `before_id`/`after_id`. Using `after` likely re-fetches the first page forever if `has_more` is true, looping and duplicating results. Apply this diff: ```diff - if (afterId) { - params.set('after', afterId); - } + if (afterId) { + params.set('after_id', afterId); + } ``` Please confirm param naming against current docs: --- 🌐 Web query: ```web Does Anthropic's List Models endpoint use the query param name `after_id` (not `after`) for pagination? ``` 💡 Result: Yes — the List Models endpoint uses the query parameter after_id (not after) for pagination. [1][2] Sources: - Anthropic API — List Models documentation. [1] - Claude (Anthropic) localized docs — List Models (shows after_id). [2] --- 🏁 Script executed: ```shell #!/bin/bash # Verify the pagination bug in api/anthropicClient.ts fd api/anthropicClient.ts --exec cat {} ``` Length of output: 0 --- 🏁 Script executed: ```shell #!/bin/bash # Search for anthropicClient file with broader pattern fd -e ts anthropicClient ``` Length of output: 22 --- 🏁 Script executed: ```shell #!/bin/bash # View the anthropicClient.ts file with line numbers, focusing on pagination section cat -n api/anthropicClient.ts | sed -n '130,150p' ``` Length of output: 976 --- 🏁 Script executed: ```shell #!/bin/bash # View complete pagination loop cat -n api/anthropicClient.ts | sed -n '135,190p' ``` Length of output: 2700 --- </details> **Critical: Fix pagination parameter from `after` to `after_id` at line 141.** Anthropic's List Models endpoint uses the query parameter `after_id` (not `after`) for pagination. The current code at line 141 uses the incorrect parameter name, which will cause the API to ignore the pagination cursor and potentially loop indefinitely or return duplicate results. ```diff if (afterId) { - params.set('after', afterId); + params.set('after_id', afterId); } ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion // Build URL with pagination parameters // Use limit=1000 (Anthropic's max) to minimize round trips const params = new URLSearchParams(); params.set('limit', '1000'); if (afterId) { params.set('after_id', afterId); } const url = `${this.modelsUrl}?${params.toString()}`; ``` </details> <!-- suggestion_end --> <details> <summary>🤖 Prompt for AI Agents</summary> ``` In api/anthropicClient.ts around lines 136 to 144, the code sets the pagination query parameter as 'after' which is incorrect for Anthropic's List Models endpoint; change the parameter key to 'after_id' when afterId is present (i.e., params.set('after_id', afterId)) so the pagination cursor is passed correctly, leaving the rest of the URL construction and limit=1000 logic unchanged. ``` </details> <!-- fingerprinting:phantom:medusa:chinchilla --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commits afa725d to 3f53bb5
body: JSON.stringify(request) body: JSON.stringify(request)
}); });
@@ -375,8 +379,10 @@ export class AnthropicClient {
// Validate that the assistant actually returned "ping" // Validate that the assistant actually returned "ping"
// This ensures the API is working correctly, not just returning a 2xx status // This ensures the API is working correctly, not just returning a 2xx status
// Normalize to tolerate punctuation and quotes (e.g., "ping.", ""ping"")
const returnedText = (response.content?.[0]?.text || '').trim().toLowerCase(); const returnedText = (response.content?.[0]?.text || '').trim().toLowerCase();
const isValid = returnedText === 'ping'; const normalized = returnedText.replace(/^[^a-z]+|[^a-z]+$/g, '').replace(/\s+/g, '');
const isValid = normalized === 'ping';
return { return {
success: isValid, success: isValid,