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