|
| 1 | +import axios from "axios" |
| 2 | +import type { ModelRecord } from "../../../shared/api" |
| 3 | + |
| 4 | +const MAKEHUB_BASE_URL = "https://api.makehub.ai/v1" |
| 5 | + |
| 6 | +interface MakehubModelResponse { |
| 7 | + data: Array<{ |
| 8 | + context: number |
| 9 | + model_id: string |
| 10 | + model_name: string |
| 11 | + display_name?: string |
| 12 | + organisation: string |
| 13 | + price_per_input_token: number |
| 14 | + price_per_output_token: number |
| 15 | + provider_name: string |
| 16 | + quantisation: string | null |
| 17 | + max_tokens?: number |
| 18 | + supports_images?: boolean |
| 19 | + supports_prompt_cache?: boolean |
| 20 | + cache_writes_price?: number |
| 21 | + cache_reads_price?: number |
| 22 | + assistant_ready: boolean |
| 23 | + providers_available?: string[] |
| 24 | + thinking_config?: { |
| 25 | + max_budget?: number |
| 26 | + output_price?: number |
| 27 | + } |
| 28 | + tiers?: Array<{ |
| 29 | + context_window: number |
| 30 | + input_price?: number |
| 31 | + output_price?: number |
| 32 | + cache_writes_price?: number |
| 33 | + cache_reads_price?: number |
| 34 | + }> |
| 35 | + capabilities?: { |
| 36 | + image_input?: boolean |
| 37 | + tool_calling?: boolean |
| 38 | + json_mode?: boolean |
| 39 | + } |
| 40 | + }> |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Fetches available models from the MakeHub API |
| 45 | + * |
| 46 | + * @param apiKey - The API key for authentication |
| 47 | + * @returns A promise that resolves to a record of model IDs to model info |
| 48 | + */ |
| 49 | +export const getMakehubModels = async (apiKey?: string): Promise<ModelRecord> => { |
| 50 | + try { |
| 51 | + // Configure headers based on whether API key is provided |
| 52 | + const headers: Record<string, string> = { |
| 53 | + Accept: "application/json", |
| 54 | + "Content-Type": "application/json", |
| 55 | + "HTTP-Referer": "vscode.dev", |
| 56 | + "X-Title": "RooCode", |
| 57 | + } |
| 58 | + |
| 59 | + // Add Authorization header if API key is provided |
| 60 | + if (apiKey) { |
| 61 | + headers.Authorization = `Bearer ${apiKey}` |
| 62 | + } |
| 63 | + |
| 64 | + const response = await axios.get<MakehubModelResponse>(`${MAKEHUB_BASE_URL}/models`, { |
| 65 | + headers, |
| 66 | + timeout: 10000, |
| 67 | + }) |
| 68 | + |
| 69 | + if (!response.data?.data) { |
| 70 | + console.error("Invalid MakeHub API response format:", response.data) |
| 71 | + return {} |
| 72 | + } |
| 73 | + |
| 74 | + const modelRecord: ModelRecord = {} |
| 75 | + |
| 76 | + for (const model of response.data.data) { |
| 77 | + if (!model.model_id || !model.assistant_ready) continue |
| 78 | + |
| 79 | + // Create a model ID that includes provider information |
| 80 | + // Use just the model_id as provided by the API, since it already has the proper format |
| 81 | + const fullModelId = model.model_id.includes("/") |
| 82 | + ? model.model_id // Already has organization format |
| 83 | + : `${model.organisation}/${model.model_id}` // Add organization prefix |
| 84 | + |
| 85 | + // Log the raw price values from the API for debugging |
| 86 | + console.log(`Model ${fullModelId} raw prices:`, { |
| 87 | + input: model.price_per_input_token, |
| 88 | + output: model.price_per_output_token, |
| 89 | + }) |
| 90 | + |
| 91 | + // MakeHub API returns prices already in cost per million tokens, |
| 92 | + // so we can use them directly without further conversion |
| 93 | + const inputPrice = model.price_per_input_token |
| 94 | + const outputPrice = model.price_per_output_token |
| 95 | + |
| 96 | + console.log(`Model ${fullModelId} stored prices:`, { |
| 97 | + input: inputPrice, |
| 98 | + output: outputPrice, |
| 99 | + }) |
| 100 | + |
| 101 | + modelRecord[fullModelId] = { |
| 102 | + maxTokens: model.max_tokens ?? undefined, |
| 103 | + contextWindow: model.context, |
| 104 | + supportsImages: model.capabilities?.image_input ?? false, |
| 105 | + supportsComputerUse: model.capabilities?.tool_calling ?? false, |
| 106 | + supportsPromptCache: model.supports_prompt_cache ?? false, |
| 107 | + inputPrice: inputPrice, |
| 108 | + outputPrice: outputPrice, |
| 109 | + cacheWritesPrice: model.cache_writes_price, |
| 110 | + cacheReadsPrice: model.cache_reads_price, |
| 111 | + description: model.display_name, |
| 112 | + tiers: model.tiers?.map((tier) => ({ |
| 113 | + contextWindow: tier.context_window, |
| 114 | + inputPrice: tier.input_price, |
| 115 | + outputPrice: tier.output_price, |
| 116 | + cacheWritesPrice: tier.cache_writes_price, |
| 117 | + cacheReadsPrice: tier.cache_reads_price, |
| 118 | + })), |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return modelRecord |
| 123 | + } catch (error) { |
| 124 | + console.error("Error fetching MakeHub models:", error) |
| 125 | + if (axios.isAxiosError(error)) { |
| 126 | + console.error("Response:", error.response?.data) |
| 127 | + console.error("Status:", error.response?.status) |
| 128 | + } |
| 129 | + return {} |
| 130 | + } |
| 131 | +} |
0 commit comments