|
| 1 | +import axios from "axios" |
| 2 | +import { z } from "zod" |
| 3 | +import type { ModelInfo } from "@roo-code/types" |
| 4 | +import { |
| 5 | + HUGGINGFACE_API_URL, |
| 6 | + HUGGINGFACE_CACHE_DURATION, |
| 7 | + HUGGINGFACE_DEFAULT_MAX_TOKENS, |
| 8 | + HUGGINGFACE_DEFAULT_CONTEXT_WINDOW, |
| 9 | +} from "@roo-code/types" |
| 10 | +import type { ModelRecord } from "../../../shared/api" |
| 11 | + |
| 12 | +/** |
| 13 | + * HuggingFace Provider Schema |
| 14 | + */ |
| 15 | +const huggingFaceProviderSchema = z.object({ |
| 16 | + provider: z.string(), |
| 17 | + status: z.enum(["live", "staging", "error"]), |
| 18 | + supports_tools: z.boolean().optional(), |
| 19 | + supports_structured_output: z.boolean().optional(), |
| 20 | + context_length: z.number().optional(), |
| 21 | + pricing: z |
| 22 | + .object({ |
| 23 | + input: z.number(), |
| 24 | + output: z.number(), |
| 25 | + }) |
| 26 | + .optional(), |
| 27 | +}) |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents a provider that can serve a HuggingFace model |
| 31 | + * @property provider - The provider identifier (e.g., "sambanova", "together") |
| 32 | + * @property status - The current status of the provider |
| 33 | + * @property supports_tools - Whether the provider supports tool/function calling |
| 34 | + * @property supports_structured_output - Whether the provider supports structured output |
| 35 | + * @property context_length - The maximum context length supported by this provider |
| 36 | + * @property pricing - The pricing information for input/output tokens |
| 37 | + */ |
| 38 | +export type HuggingFaceProvider = z.infer<typeof huggingFaceProviderSchema> |
| 39 | + |
| 40 | +/** |
| 41 | + * HuggingFace Model Schema |
| 42 | + */ |
| 43 | +const huggingFaceModelSchema = z.object({ |
| 44 | + id: z.string(), |
| 45 | + object: z.literal("model"), |
| 46 | + created: z.number(), |
| 47 | + owned_by: z.string(), |
| 48 | + providers: z.array(huggingFaceProviderSchema), |
| 49 | +}) |
| 50 | + |
| 51 | +/** |
| 52 | + * Represents a HuggingFace model available through the router API |
| 53 | + * @property id - The unique identifier of the model |
| 54 | + * @property object - The object type (always "model") |
| 55 | + * @property created - Unix timestamp of when the model was created |
| 56 | + * @property owned_by - The organization that owns the model |
| 57 | + * @property providers - List of providers that can serve this model |
| 58 | + */ |
| 59 | +export type HuggingFaceModel = z.infer<typeof huggingFaceModelSchema> |
| 60 | + |
| 61 | +/** |
| 62 | + * HuggingFace API Response Schema |
| 63 | + */ |
| 64 | +const huggingFaceApiResponseSchema = z.object({ |
| 65 | + object: z.string(), |
| 66 | + data: z.array(huggingFaceModelSchema), |
| 67 | +}) |
| 68 | + |
| 69 | +/** |
| 70 | + * Represents the response from the HuggingFace router API |
| 71 | + * @property object - The response object type |
| 72 | + * @property data - Array of available models |
| 73 | + */ |
| 74 | +type HuggingFaceApiResponse = z.infer<typeof huggingFaceApiResponseSchema> |
| 75 | + |
| 76 | +/** |
| 77 | + * Cache entry for storing fetched models |
| 78 | + * @property data - The cached model records |
| 79 | + * @property timestamp - Unix timestamp of when the cache was last updated |
| 80 | + */ |
| 81 | +interface CacheEntry { |
| 82 | + data: ModelRecord |
| 83 | + timestamp: number |
| 84 | +} |
| 85 | + |
| 86 | +let cache: CacheEntry | null = null |
| 87 | + |
| 88 | +/** |
| 89 | + * Parse a HuggingFace model into ModelInfo format |
| 90 | + * @param model - The HuggingFace model to parse |
| 91 | + * @param provider - Optional specific provider to use for capabilities |
| 92 | + * @returns ModelInfo object compatible with the application's model system |
| 93 | + */ |
| 94 | +function parseHuggingFaceModel(model: HuggingFaceModel, provider?: HuggingFaceProvider): ModelInfo { |
| 95 | + // Use provider-specific values if available, otherwise find first provider with values |
| 96 | + const contextLength = |
| 97 | + provider?.context_length || |
| 98 | + model.providers.find((p) => p.context_length)?.context_length || |
| 99 | + HUGGINGFACE_DEFAULT_CONTEXT_WINDOW |
| 100 | + |
| 101 | + const pricing = provider?.pricing || model.providers.find((p) => p.pricing)?.pricing |
| 102 | + |
| 103 | + return { |
| 104 | + maxTokens: Math.min(contextLength, HUGGINGFACE_DEFAULT_MAX_TOKENS), |
| 105 | + contextWindow: contextLength, |
| 106 | + supportsImages: false, // HuggingFace API doesn't provide this info yet |
| 107 | + supportsPromptCache: false, |
| 108 | + supportsComputerUse: false, |
| 109 | + inputPrice: pricing?.input, |
| 110 | + outputPrice: pricing?.output, |
| 111 | + description: `${model.id} via HuggingFace`, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Fetches available models from HuggingFace |
| 117 | + * |
| 118 | + * @returns A promise that resolves to a record of model IDs to model info |
| 119 | + * @throws Will throw an error if the request fails |
| 120 | + */ |
| 121 | +export async function getHuggingFaceModels(): Promise<ModelRecord> { |
| 122 | + const now = Date.now() |
| 123 | + |
| 124 | + // Check cache |
| 125 | + if (cache && now - cache.timestamp < HUGGINGFACE_CACHE_DURATION) { |
| 126 | + console.log("Using cached HuggingFace models") |
| 127 | + return cache.data |
| 128 | + } |
| 129 | + |
| 130 | + const models: ModelRecord = {} |
| 131 | + |
| 132 | + try { |
| 133 | + console.log("Fetching HuggingFace models from API...") |
| 134 | + |
| 135 | + const response = await axios.get<HuggingFaceApiResponse>(HUGGINGFACE_API_URL, { |
| 136 | + headers: { |
| 137 | + "Upgrade-Insecure-Requests": "1", |
| 138 | + "Sec-Fetch-Dest": "document", |
| 139 | + "Sec-Fetch-Mode": "navigate", |
| 140 | + "Sec-Fetch-Site": "none", |
| 141 | + "Sec-Fetch-User": "?1", |
| 142 | + Priority: "u=0, i", |
| 143 | + Pragma: "no-cache", |
| 144 | + "Cache-Control": "no-cache", |
| 145 | + }, |
| 146 | + timeout: 10000, // 10 second timeout |
| 147 | + }) |
| 148 | + |
| 149 | + const result = huggingFaceApiResponseSchema.safeParse(response.data) |
| 150 | + |
| 151 | + if (!result.success) { |
| 152 | + console.error("HuggingFace models response validation failed:", result.error.format()) |
| 153 | + throw new Error("Invalid response format from HuggingFace API") |
| 154 | + } |
| 155 | + |
| 156 | + const validModels = result.data.data.filter((model) => model.providers.length > 0) |
| 157 | + |
| 158 | + for (const model of validModels) { |
| 159 | + // Add the base model |
| 160 | + models[model.id] = parseHuggingFaceModel(model) |
| 161 | + |
| 162 | + // Add provider-specific variants if they have different capabilities |
| 163 | + for (const provider of model.providers) { |
| 164 | + if (provider.status === "live") { |
| 165 | + const providerKey = `${model.id}:${provider.provider}` |
| 166 | + const providerModel = parseHuggingFaceModel(model, provider) |
| 167 | + |
| 168 | + // Only add provider variant if it differs from base model |
| 169 | + if (JSON.stringify(models[model.id]) !== JSON.stringify(providerModel)) { |
| 170 | + models[providerKey] = providerModel |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Update cache |
| 177 | + cache = { |
| 178 | + data: models, |
| 179 | + timestamp: now, |
| 180 | + } |
| 181 | + |
| 182 | + console.log(`Fetched ${Object.keys(models).length} HuggingFace models`) |
| 183 | + return models |
| 184 | + } catch (error) { |
| 185 | + console.error("Error fetching HuggingFace models:", error) |
| 186 | + |
| 187 | + // Return cached data if available |
| 188 | + if (cache) { |
| 189 | + console.log("Using stale cached data due to fetch error") |
| 190 | + return cache.data |
| 191 | + } |
| 192 | + |
| 193 | + // Re-throw with more context |
| 194 | + if (axios.isAxiosError(error)) { |
| 195 | + if (error.response) { |
| 196 | + throw new Error( |
| 197 | + `Failed to fetch HuggingFace models: ${error.response.status} ${error.response.statusText}`, |
| 198 | + ) |
| 199 | + } else if (error.request) { |
| 200 | + throw new Error( |
| 201 | + "Failed to fetch HuggingFace models: No response from server. Check your internet connection.", |
| 202 | + ) |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + throw new Error( |
| 207 | + `Failed to fetch HuggingFace models: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 208 | + ) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +/** |
| 213 | + * Get cached models without making an API request |
| 214 | + */ |
| 215 | +export function getCachedHuggingFaceModels(): ModelRecord | null { |
| 216 | + return cache?.data || null |
| 217 | +} |
| 218 | + |
| 219 | +/** |
| 220 | + * Clear the cache |
| 221 | + */ |
| 222 | +export function clearHuggingFaceCache(): void { |
| 223 | + cache = null |
| 224 | +} |
0 commit comments