MCPcopy
hub / github.com/codeaashu/claude-code / validateModel

Function validateModel

src/utils/model/validateModel.ts:20–82  ·  view source on GitHub ↗
(
  model: string,
)

Source from the content-addressed store, hash-verified

18 * Validates a model by attempting an actual API call.
19 */
20export 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
55 // Try to make an actual API call with minimal parameters
56 try {
57 await sideQuery({
58 model: normalizedModel,
59 max_tokens: 1,
60 maxRetries: 0,
61 querySource: 'model_validation',
62 messages: [
63 {
64 role: 'user',
65 content: [
66 {
67 type: 'text',
68 text: 'Hi',
69 cache_control: { type: 'ephemeral' },
70 },
71 ],
72 },
73 ],
74 })
75
76 // If we got here, the model is valid
77 validModelCache.set(normalizedModel, true)

Callers 3

callFunction · 0.85
handleModelChangeFunction · 0.85

Calls 5

isModelAllowedFunction · 0.85
sideQueryFunction · 0.85
handleValidationErrorFunction · 0.85
hasMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected