( model: string, )
| 20 | * Validates a model by attempting an actual API call. |
| 21 | */ |
| 22 | export async function validateModel( |
| 23 | model: string, |
| 24 | ): Promise<{ valid: boolean; error?: string }> { |
| 25 | const normalizedModel = model.trim() |
| 26 | const customBaseURL = readCurrentCustomApiProvider()?.baseURL |
| 27 | |
| 28 | // Empty model is invalid |
| 29 | if (!normalizedModel) { |
| 30 | return { valid: false, error: 'Model name cannot be empty' } |
| 31 | } |
| 32 | |
| 33 | // Check against availableModels allowlist before any API call |
| 34 | if (!isModelAllowed(normalizedModel)) { |
| 35 | return { |
| 36 | valid: false, |
| 37 | error: `Model '${normalizedModel}' is not in the list of available models`, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Check if it's a known alias (these are always valid) |
| 42 | const lowerModel = stripModelContextSuffix(normalizedModel).toLowerCase() |
| 43 | if ((MODEL_ALIASES as readonly string[]).includes(lowerModel)) { |
| 44 | return { valid: true } |
| 45 | } |
| 46 | |
| 47 | // Check if it matches ANTHROPIC_CUSTOM_MODEL_OPTION (pre-validated by the user) |
| 48 | if (normalizedModel === process.env.ANTHROPIC_CUSTOM_MODEL_OPTION) { |
| 49 | return { valid: true } |
| 50 | } |
| 51 | |
| 52 | // For custom Anthropic-compatible gateways, allow arbitrary model strings. |
| 53 | // Many OpenAI-compatible/relay providers expose non-Claude identifiers such as |
| 54 | // gpt-5.4 while still speaking the Anthropic wire format. |
| 55 | if (customBaseURL && customBaseURL.trim() !== '') { |
| 56 | validModelCache.set(normalizedModel, true) |
| 57 | return { valid: true } |
| 58 | } |
| 59 | |
| 60 | // Check cache first |
| 61 | if (validModelCache.has(normalizedModel)) { |
| 62 | return { valid: true } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // Try to make an actual API call with minimal parameters |
| 67 | try { |
| 68 | await sideQuery({ |
| 69 | model: normalizedModel, |
| 70 | max_tokens: 1, |
| 71 | maxRetries: 0, |
| 72 | querySource: 'model_validation', |
| 73 | messages: [ |
| 74 | { |
| 75 | role: 'user', |
| 76 | content: [ |
| 77 | { |
| 78 | type: 'text', |
| 79 | text: 'Hi', |
no test coverage detected