|
| 1 | +import { ModelRecord } from "../../../shared/api" |
| 2 | +import { COMETAPI_MODELS } from "@roo-code/types" |
| 3 | + |
| 4 | +/** |
| 5 | + * Fetch CometAPI models from the API |
| 6 | + * @param apiKey - The API key for CometAPI |
| 7 | + * @param baseUrl - The base URL for CometAPI (optional) |
| 8 | + * @returns The models from CometAPI |
| 9 | + */ |
| 10 | +export async function getCometAPIModels(apiKey?: string, baseUrl?: string): Promise<ModelRecord> { |
| 11 | + const url = `${baseUrl || "https://api.cometapi.com/v1"}/models` |
| 12 | + |
| 13 | + try { |
| 14 | + if (!apiKey) { |
| 15 | + // Return fallback models if no API key is provided |
| 16 | + return COMETAPI_MODELS |
| 17 | + } |
| 18 | + |
| 19 | + const response = await fetch(url, { |
| 20 | + headers: { |
| 21 | + Authorization: `Bearer ${apiKey}`, |
| 22 | + Accept: "application/json", |
| 23 | + }, |
| 24 | + }) |
| 25 | + |
| 26 | + if (!response.ok) { |
| 27 | + console.warn(`Failed to fetch CometAPI models: ${response.status} ${response.statusText}`) |
| 28 | + // Return fallback models on error |
| 29 | + return COMETAPI_MODELS |
| 30 | + } |
| 31 | + |
| 32 | + const data = await response.json() |
| 33 | + |
| 34 | + // Transform the API response to match our ModelRecord format |
| 35 | + const models: ModelRecord = {} |
| 36 | + |
| 37 | + if (data.data && Array.isArray(data.data)) { |
| 38 | + for (const model of data.data) { |
| 39 | + // Use fallback model info if available, otherwise create basic info |
| 40 | + const fallbackInfo = COMETAPI_MODELS[model.id] |
| 41 | + models[model.id] = fallbackInfo || { |
| 42 | + maxTokens: model.max_tokens || 8192, |
| 43 | + contextWindow: model.context_length || 128000, |
| 44 | + supportsImages: model.supports_images || false, |
| 45 | + supportsPromptCache: false, |
| 46 | + inputPrice: model.pricing?.prompt || 0, |
| 47 | + outputPrice: model.pricing?.completion || 0, |
| 48 | + description: model.description || model.id, |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // If no models were returned, use fallback models |
| 54 | + return Object.keys(models).length > 0 ? models : COMETAPI_MODELS |
| 55 | + } catch (error) { |
| 56 | + console.error("Error fetching CometAPI models:", error) |
| 57 | + // Return fallback models on error |
| 58 | + return COMETAPI_MODELS |
| 59 | + } |
| 60 | +} |
0 commit comments