Skip to content

Commit 3201a43

Browse files
committed
fix: preserve HuggingFace provider details in model response
- Store raw HuggingFace models in cache to preserve provider information - Export getCachedRawHuggingFaceModels to retrieve full model data - Update huggingface-models.ts to return cached raw models when available - Include provider name in model descriptions - Always add provider-specific variants to show all available providers - Remove console.log statements from fetcher
1 parent 08807bb commit 3201a43

File tree

2 files changed

+64
-40
lines changed

2 files changed

+64
-40
lines changed

src/api/huggingface-models.ts

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { getHuggingFaceModels as fetchModels, type HuggingFaceModel } from "./providers/fetchers/huggingface"
2-
import type { ModelRecord } from "../shared/api"
1+
import {
2+
getHuggingFaceModels as fetchModels,
3+
getCachedRawHuggingFaceModels,
4+
type HuggingFaceModel,
5+
} from "./providers/fetchers/huggingface"
6+
import axios from "axios"
7+
import { HUGGINGFACE_API_URL } from "@roo-code/types"
38

49
export interface HuggingFaceModelsResponse {
510
models: HuggingFaceModel[]
@@ -8,35 +13,49 @@ export interface HuggingFaceModelsResponse {
813
}
914

1015
export async function getHuggingFaceModels(): Promise<HuggingFaceModelsResponse> {
11-
// Fetch models as ModelRecord
12-
const modelRecord = await fetchModels()
16+
try {
17+
// First, trigger the fetch to populate cache
18+
await fetchModels()
1319

14-
// Convert ModelRecord to array of HuggingFaceModel for backward compatibility
15-
// Note: This is a temporary solution to maintain API compatibility
16-
const models: HuggingFaceModel[] = Object.entries(modelRecord).map(([id, info]) => ({
17-
id,
18-
object: "model" as const,
19-
created: Date.now(),
20-
owned_by: "huggingface",
21-
providers: [
22-
{
23-
provider: "auto",
24-
status: "live" as const,
25-
context_length: info.contextWindow,
26-
pricing:
27-
info.inputPrice && info.outputPrice
28-
? {
29-
input: info.inputPrice,
30-
output: info.outputPrice,
31-
}
32-
: undefined,
20+
// Get the raw models from cache
21+
const cachedRawModels = getCachedRawHuggingFaceModels()
22+
23+
if (cachedRawModels) {
24+
return {
25+
models: cachedRawModels,
26+
cached: true,
27+
timestamp: Date.now(),
28+
}
29+
}
30+
31+
// If no cached raw models, fetch directly from API
32+
const response = await axios.get(HUGGINGFACE_API_URL, {
33+
headers: {
34+
"Upgrade-Insecure-Requests": "1",
35+
"Sec-Fetch-Dest": "document",
36+
"Sec-Fetch-Mode": "navigate",
37+
"Sec-Fetch-Site": "none",
38+
"Sec-Fetch-User": "?1",
39+
Priority: "u=0, i",
40+
Pragma: "no-cache",
41+
"Cache-Control": "no-cache",
3342
},
34-
],
35-
}))
43+
timeout: 10000,
44+
})
45+
46+
const models = response.data?.data || []
3647

37-
return {
38-
models,
39-
cached: false,
40-
timestamp: Date.now(),
48+
return {
49+
models,
50+
cached: false,
51+
timestamp: Date.now(),
52+
}
53+
} catch (error) {
54+
console.error("Failed to get HuggingFace models:", error)
55+
return {
56+
models: [],
57+
cached: false,
58+
timestamp: Date.now(),
59+
}
4160
}
4261
}

src/api/providers/fetchers/huggingface.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type HuggingFaceApiResponse = z.infer<typeof huggingFaceApiResponseSchema>
8080
*/
8181
interface CacheEntry {
8282
data: ModelRecord
83+
rawModels?: HuggingFaceModel[]
8384
timestamp: number
8485
}
8586

@@ -100,6 +101,9 @@ function parseHuggingFaceModel(model: HuggingFaceModel, provider?: HuggingFacePr
100101

101102
const pricing = provider?.pricing || model.providers.find((p) => p.pricing)?.pricing
102103

104+
// Include provider name in description if specific provider is given
105+
const description = provider ? `${model.id} via ${provider.provider}` : `${model.id} via HuggingFace`
106+
103107
return {
104108
maxTokens: Math.min(contextLength, HUGGINGFACE_DEFAULT_MAX_TOKENS),
105109
contextWindow: contextLength,
@@ -108,7 +112,7 @@ function parseHuggingFaceModel(model: HuggingFaceModel, provider?: HuggingFacePr
108112
supportsComputerUse: false,
109113
inputPrice: pricing?.input,
110114
outputPrice: pricing?.output,
111-
description: `${model.id} via HuggingFace`,
115+
description,
112116
}
113117
}
114118

@@ -123,15 +127,12 @@ export async function getHuggingFaceModels(): Promise<ModelRecord> {
123127

124128
// Check cache
125129
if (cache && now - cache.timestamp < HUGGINGFACE_CACHE_DURATION) {
126-
console.log("Using cached HuggingFace models")
127130
return cache.data
128131
}
129132

130133
const models: ModelRecord = {}
131134

132135
try {
133-
console.log("Fetching HuggingFace models from API...")
134-
135136
const response = await axios.get<HuggingFaceApiResponse>(HUGGINGFACE_API_URL, {
136137
headers: {
137138
"Upgrade-Insecure-Requests": "1",
@@ -159,34 +160,31 @@ export async function getHuggingFaceModels(): Promise<ModelRecord> {
159160
// Add the base model
160161
models[model.id] = parseHuggingFaceModel(model)
161162

162-
// Add provider-specific variants if they have different capabilities
163+
// Add provider-specific variants for all live providers
163164
for (const provider of model.providers) {
164165
if (provider.status === "live") {
165166
const providerKey = `${model.id}:${provider.provider}`
166167
const providerModel = parseHuggingFaceModel(model, provider)
167168

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-
}
169+
// Always add provider variants to show all available providers
170+
models[providerKey] = providerModel
172171
}
173172
}
174173
}
175174

176175
// Update cache
177176
cache = {
178177
data: models,
178+
rawModels: validModels,
179179
timestamp: now,
180180
}
181181

182-
console.log(`Fetched ${Object.keys(models).length} HuggingFace models`)
183182
return models
184183
} catch (error) {
185184
console.error("Error fetching HuggingFace models:", error)
186185

187186
// Return cached data if available
188187
if (cache) {
189-
console.log("Using stale cached data due to fetch error")
190188
return cache.data
191189
}
192190

@@ -216,6 +214,13 @@ export function getCachedHuggingFaceModels(): ModelRecord | null {
216214
return cache?.data || null
217215
}
218216

217+
/**
218+
* Get cached raw models for UI display
219+
*/
220+
export function getCachedRawHuggingFaceModels(): HuggingFaceModel[] | null {
221+
return cache?.rawModels || null
222+
}
223+
219224
/**
220225
* Clear the cache
221226
*/

0 commit comments

Comments
 (0)