|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { getBYOKKey } from '@/lib/api-key/byok' |
| 4 | +import { getSession } from '@/lib/auth' |
| 5 | +import { env } from '@/lib/core/config/env' |
| 6 | +import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils' |
| 7 | +import { filterBlacklistedModels, isProviderBlacklisted } from '@/providers/utils' |
| 8 | + |
| 9 | +const logger = createLogger('FireworksModelsAPI') |
| 10 | + |
| 11 | +interface FireworksModel { |
| 12 | + id: string |
| 13 | + object?: string |
| 14 | + created?: number |
| 15 | + owned_by?: string |
| 16 | +} |
| 17 | + |
| 18 | +interface FireworksModelsResponse { |
| 19 | + data: FireworksModel[] |
| 20 | + object?: string |
| 21 | +} |
| 22 | + |
| 23 | +export async function GET(request: NextRequest) { |
| 24 | + if (isProviderBlacklisted('fireworks')) { |
| 25 | + logger.info('Fireworks provider is blacklisted, returning empty models') |
| 26 | + return NextResponse.json({ models: [] }) |
| 27 | + } |
| 28 | + |
| 29 | + let apiKey: string | undefined |
| 30 | + |
| 31 | + const workspaceId = request.nextUrl.searchParams.get('workspaceId') |
| 32 | + if (workspaceId) { |
| 33 | + const session = await getSession() |
| 34 | + if (session?.user?.id) { |
| 35 | + const permission = await getUserEntityPermissions(session.user.id, 'workspace', workspaceId) |
| 36 | + if (permission) { |
| 37 | + const byokResult = await getBYOKKey(workspaceId, 'fireworks') |
| 38 | + if (byokResult) { |
| 39 | + apiKey = byokResult.apiKey |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (!apiKey) { |
| 46 | + apiKey = env.FIREWORKS_API_KEY |
| 47 | + } |
| 48 | + |
| 49 | + if (!apiKey) { |
| 50 | + logger.info('No Fireworks API key available, returning empty models') |
| 51 | + return NextResponse.json({ models: [] }) |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + const response = await fetch('https://api.fireworks.ai/inference/v1/models', { |
| 56 | + headers: { |
| 57 | + Authorization: `Bearer ${apiKey}`, |
| 58 | + 'Content-Type': 'application/json', |
| 59 | + }, |
| 60 | + cache: 'no-store', |
| 61 | + }) |
| 62 | + |
| 63 | + if (!response.ok) { |
| 64 | + logger.warn('Failed to fetch Fireworks models', { |
| 65 | + status: response.status, |
| 66 | + statusText: response.statusText, |
| 67 | + }) |
| 68 | + return NextResponse.json({ models: [] }) |
| 69 | + } |
| 70 | + |
| 71 | + const data = (await response.json()) as FireworksModelsResponse |
| 72 | + |
| 73 | + const allModels: string[] = [] |
| 74 | + for (const model of data.data ?? []) { |
| 75 | + allModels.push(`fireworks/${model.id}`) |
| 76 | + } |
| 77 | + |
| 78 | + const uniqueModels = Array.from(new Set(allModels)) |
| 79 | + const models = filterBlacklistedModels(uniqueModels) |
| 80 | + |
| 81 | + logger.info('Successfully fetched Fireworks models', { |
| 82 | + count: models.length, |
| 83 | + filtered: uniqueModels.length - models.length, |
| 84 | + }) |
| 85 | + |
| 86 | + return NextResponse.json({ models }) |
| 87 | + } catch (error) { |
| 88 | + logger.error('Error fetching Fireworks models', { |
| 89 | + error: error instanceof Error ? error.message : 'Unknown error', |
| 90 | + }) |
| 91 | + return NextResponse.json({ models: [] }) |
| 92 | + } |
| 93 | +} |
0 commit comments