| 21 | const CACHE_TTL_MS = 5 * 60 * 1000 // 5 minutes |
| 22 | |
| 23 | async function fetchModelCapabilities(): Promise<Map<string, ModelCapabilities>> { |
| 24 | try { |
| 25 | const response = await fetch('https://openrouter.ai/api/v1/models', { |
| 26 | headers: { 'Content-Type': 'application/json' }, |
| 27 | }) |
| 28 | |
| 29 | if (!response.ok) { |
| 30 | await response.text().catch(() => {}) |
| 31 | logger.warn('Failed to fetch OpenRouter model capabilities', { |
| 32 | status: response.status, |
| 33 | }) |
| 34 | return new Map() |
| 35 | } |
| 36 | |
| 37 | const data = await response.json() |
| 38 | const capabilities = new Map<string, ModelCapabilities>() |
| 39 | |
| 40 | for (const model of (data.data ?? []) as OpenRouterModelData[]) { |
| 41 | const supportedParams = model.supported_parameters ?? [] |
| 42 | capabilities.set(model.id, { |
| 43 | supportsStructuredOutputs: supportedParams.includes('structured_outputs'), |
| 44 | supportsTools: supportedParams.includes('tools'), |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | logger.info('Cached OpenRouter model capabilities', { |
| 49 | modelCount: capabilities.size, |
| 50 | withStructuredOutputs: Array.from(capabilities.values()).filter( |
| 51 | (c) => c.supportsStructuredOutputs |
| 52 | ).length, |
| 53 | }) |
| 54 | |
| 55 | return capabilities |
| 56 | } catch (error) { |
| 57 | logger.error('Error fetching OpenRouter model capabilities', { |
| 58 | error: toError(error).message, |
| 59 | }) |
| 60 | return new Map() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets capabilities for a specific OpenRouter model. |