Skip to content

Commit 0504199

Browse files
authored
refactor: consolidate HuggingFace models API into providers/fetchers (#6228)
refactor: move HuggingFace models API to providers/fetchers - Moved getHuggingFaceModels functionality from src/api/huggingface-models.ts to src/api/providers/fetchers/huggingface.ts - Added getHuggingFaceModelsWithMetadata function to maintain the same API interface - Updated import in webviewMessageHandler.ts to use the new location - Deleted the now redundant src/api/huggingface-models.ts file This consolidates all HuggingFace-related API logic into a single location within the providers/fetchers directory structure.
1 parent 440ec30 commit 0504199

File tree

3 files changed

+63
-63
lines changed

3 files changed

+63
-63
lines changed

src/api/huggingface-models.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/api/providers/fetchers/huggingface.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,64 @@ export function getCachedRawHuggingFaceModels(): HuggingFaceModel[] | null {
227227
export function clearHuggingFaceCache(): void {
228228
cache = null
229229
}
230+
231+
/**
232+
* HuggingFace Models Response Interface
233+
*/
234+
export interface HuggingFaceModelsResponse {
235+
models: HuggingFaceModel[]
236+
cached: boolean
237+
timestamp: number
238+
}
239+
240+
/**
241+
* Get HuggingFace models with response metadata
242+
* This function provides a higher-level API that includes cache status and timestamp
243+
*/
244+
export async function getHuggingFaceModelsWithMetadata(): Promise<HuggingFaceModelsResponse> {
245+
try {
246+
// First, trigger the fetch to populate cache
247+
await getHuggingFaceModels()
248+
249+
// Get the raw models from cache
250+
const cachedRawModels = getCachedRawHuggingFaceModels()
251+
252+
if (cachedRawModels) {
253+
return {
254+
models: cachedRawModels,
255+
cached: true,
256+
timestamp: Date.now(),
257+
}
258+
}
259+
260+
// If no cached raw models, fetch directly from API
261+
const response = await axios.get(HUGGINGFACE_API_URL, {
262+
headers: {
263+
"Upgrade-Insecure-Requests": "1",
264+
"Sec-Fetch-Dest": "document",
265+
"Sec-Fetch-Mode": "navigate",
266+
"Sec-Fetch-Site": "none",
267+
"Sec-Fetch-User": "?1",
268+
Priority: "u=0, i",
269+
Pragma: "no-cache",
270+
"Cache-Control": "no-cache",
271+
},
272+
timeout: 10000,
273+
})
274+
275+
const models = response.data?.data || []
276+
277+
return {
278+
models,
279+
cached: false,
280+
timestamp: Date.now(),
281+
}
282+
} catch (error) {
283+
console.error("Failed to get HuggingFace models:", error)
284+
return {
285+
models: [],
286+
cached: false,
287+
timestamp: Date.now(),
288+
}
289+
}
290+
}

src/core/webview/webviewMessageHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ export const webviewMessageHandler = async (
676676
break
677677
case "requestHuggingFaceModels":
678678
try {
679-
const { getHuggingFaceModels } = await import("../../api/huggingface-models")
680-
const huggingFaceModelsResponse = await getHuggingFaceModels()
679+
const { getHuggingFaceModelsWithMetadata } = await import("../../api/providers/fetchers/huggingface")
680+
const huggingFaceModelsResponse = await getHuggingFaceModelsWithMetadata()
681681
provider.postMessageToWebview({
682682
type: "huggingFaceModels",
683683
huggingFaceModels: huggingFaceModelsResponse.models,

0 commit comments

Comments
 (0)