MCPcopy Index your code
hub / github.com/freecodexyz/free-code / validateModel

Function validateModel

src/utils/model/validateModel.ts:20–90  ·  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's a known Codex/OpenAI model (skip Anthropic API validation)
45 const { isCodexSubscriber } = await import('../auth.js')
46 const { isCodexModel } = await import('../../services/api/codex-fetch-adapter.js')
47 if (isCodexSubscriber() && isCodexModel(normalizedModel)) {
48 validModelCache.set(normalizedModel, true)
49 return { valid: true }
50 }
51
52 // Check if it matches ANTHROPIC_CUSTOM_MODEL_OPTION (pre-validated by the user)
53 if (normalizedModel === process.env.ANTHROPIC_CUSTOM_MODEL_OPTION) {
54 return { valid: true }
55 }
56
57 // Check cache first
58 if (validModelCache.has(normalizedModel)) {
59 return { valid: true }
60 }
61
62
63 // Try to make an actual API call with minimal parameters
64 try {
65 await sideQuery({
66 model: normalizedModel,
67 max_tokens: 1,
68 maxRetries: 0,
69 querySource: 'model_validation',
70 messages: [
71 {
72 role: 'user',
73 content: [
74 {
75 type: 'text',
76 text: 'Hi',
77 cache_control: { type: 'ephemeral' },

Callers 3

callFunction · 0.85
handleModelChangeFunction · 0.85

Calls 7

isModelAllowedFunction · 0.85
isCodexSubscriberFunction · 0.85
isCodexModelFunction · 0.85
sideQueryFunction · 0.85
handleValidationErrorFunction · 0.85
setMethod · 0.80
hasMethod · 0.45

Tested by

no test coverage detected