|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { getErrorMessage } from '@sim/utils/errors' |
| 3 | +import { type NextRequest, NextResponse } from 'next/server' |
| 4 | +import { |
| 5 | + providerModelsResponseSchema, |
| 6 | + vllmUpstreamResponseSchema, |
| 7 | +} from '@/lib/api/contracts/providers' |
| 8 | +import { env } from '@/lib/core/config/env' |
| 9 | +import { withRouteHandler } from '@/lib/core/utils/with-route-handler' |
| 10 | +import { filterBlacklistedModels, isProviderBlacklisted } from '@/providers/utils' |
| 11 | + |
| 12 | +const logger = createLogger('LiteLLMModelsAPI') |
| 13 | + |
| 14 | +export const GET = withRouteHandler(async (_request: NextRequest) => { |
| 15 | + if (isProviderBlacklisted('litellm')) { |
| 16 | + logger.info('LiteLLM provider is blacklisted, returning empty models') |
| 17 | + return NextResponse.json({ models: [] }) |
| 18 | + } |
| 19 | + |
| 20 | + const baseUrl = (env.LITELLM_BASE_URL || '').replace(/\/$/, '') |
| 21 | + |
| 22 | + if (!baseUrl) { |
| 23 | + logger.info('LITELLM_BASE_URL not configured') |
| 24 | + return NextResponse.json({ models: [] }) |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + logger.info('Fetching LiteLLM models', { baseUrl }) |
| 29 | + |
| 30 | + const headers: Record<string, string> = { |
| 31 | + 'Content-Type': 'application/json', |
| 32 | + } |
| 33 | + |
| 34 | + if (env.LITELLM_API_KEY) { |
| 35 | + headers.Authorization = `Bearer ${env.LITELLM_API_KEY}` |
| 36 | + } |
| 37 | + |
| 38 | + const response = await fetch(`${baseUrl}/v1/models`, { |
| 39 | + headers, |
| 40 | + next: { revalidate: 60 }, |
| 41 | + }) |
| 42 | + |
| 43 | + if (!response.ok) { |
| 44 | + logger.warn('LiteLLM service is not available', { |
| 45 | + status: response.status, |
| 46 | + statusText: response.statusText, |
| 47 | + }) |
| 48 | + return NextResponse.json({ models: [] }) |
| 49 | + } |
| 50 | + |
| 51 | + const data = vllmUpstreamResponseSchema.parse(await response.json()) |
| 52 | + const allModels = data.data.map((model) => `litellm/${model.id}`) |
| 53 | + const models = filterBlacklistedModels(allModels) |
| 54 | + |
| 55 | + logger.info('Successfully fetched LiteLLM models', { |
| 56 | + count: models.length, |
| 57 | + filtered: allModels.length - models.length, |
| 58 | + models, |
| 59 | + }) |
| 60 | + |
| 61 | + return NextResponse.json(providerModelsResponseSchema.parse({ models })) |
| 62 | + } catch (error) { |
| 63 | + logger.error('Failed to fetch LiteLLM models', { |
| 64 | + error: getErrorMessage(error, 'Unknown error'), |
| 65 | + baseUrl, |
| 66 | + }) |
| 67 | + |
| 68 | + return NextResponse.json({ models: [] }) |
| 69 | + } |
| 70 | +}) |
0 commit comments