| 11 | * `deepSeekModels` map for known models. Unknown models get sensible defaults. |
| 12 | */ |
| 13 | export async function getDeepSeekModels(baseUrl?: string, apiKey?: string): Promise<ModelRecord> { |
| 14 | const normalizedBase = (baseUrl || "https://api.deepseek.com").replace(/\/?v1\/?$/, "") |
| 15 | const url = `${normalizedBase}/models` |
| 16 | const allowModelListFallback = process.env.E2E_MOCK_MODEL_LIST_FALLBACK === "true" |
| 17 | |
| 18 | const headers: Record<string, string> = { |
| 19 | "Content-Type": "application/json", |
| 20 | ...DEFAULT_HEADERS, |
| 21 | } |
| 22 | |
| 23 | if (apiKey) { |
| 24 | headers["Authorization"] = `Bearer ${apiKey}` |
| 25 | } |
| 26 | |
| 27 | const controller = new AbortController() |
| 28 | const timeoutId = setTimeout(() => controller.abort(), 10000) |
| 29 | |
| 30 | try { |
| 31 | const response = await fetch(url, { |
| 32 | headers, |
| 33 | signal: controller.signal, |
| 34 | }) |
| 35 | |
| 36 | if (!response.ok) { |
| 37 | let errorBody = "" |
| 38 | try { |
| 39 | errorBody = await response.text() |
| 40 | } catch { |
| 41 | errorBody = "(unable to read response body)" |
| 42 | } |
| 43 | |
| 44 | console.error(`[getDeepSeekModels] HTTP error:`, { |
| 45 | status: response.status, |
| 46 | statusText: response.statusText, |
| 47 | url, |
| 48 | body: errorBody, |
| 49 | }) |
| 50 | |
| 51 | // In mocked e2e environments, /models may be intentionally unimplemented. |
| 52 | // Allow an explicit test-only fallback to static DeepSeek model metadata. |
| 53 | if (allowModelListFallback && response.status === 404) { |
| 54 | const models: ModelRecord = Object.create(null) |
| 55 | for (const [modelId, modelInfo] of Object.entries(deepSeekModels)) { |
| 56 | models[modelId] = { ...modelInfo } |
| 57 | } |
| 58 | return models |
| 59 | } |
| 60 | |
| 61 | throw new Error(`HTTP ${response.status}: ${response.statusText}`) |
| 62 | } |
| 63 | |
| 64 | const data = await response.json() |
| 65 | |
| 66 | if (!data?.data || !Array.isArray(data.data)) { |
| 67 | console.error("[getDeepSeekModels] Unexpected response format:", data) |
| 68 | throw new Error("Failed to fetch DeepSeek models: Unexpected response format.") |
| 69 | } |
| 70 | |