Browse Source

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.
Daniel 7 months ago
parent
commit
0504199ce4

+ 0 - 61
src/api/huggingface-models.ts

@@ -1,61 +0,0 @@
-import {
-	getHuggingFaceModels as fetchModels,
-	getCachedRawHuggingFaceModels,
-	type HuggingFaceModel,
-} from "./providers/fetchers/huggingface"
-import axios from "axios"
-import { HUGGINGFACE_API_URL } from "@roo-code/types"
-
-export interface HuggingFaceModelsResponse {
-	models: HuggingFaceModel[]
-	cached: boolean
-	timestamp: number
-}
-
-export async function getHuggingFaceModels(): Promise<HuggingFaceModelsResponse> {
-	try {
-		// First, trigger the fetch to populate cache
-		await fetchModels()
-
-		// Get the raw models from cache
-		const cachedRawModels = getCachedRawHuggingFaceModels()
-
-		if (cachedRawModels) {
-			return {
-				models: cachedRawModels,
-				cached: true,
-				timestamp: Date.now(),
-			}
-		}
-
-		// If no cached raw models, fetch directly from API
-		const response = await axios.get(HUGGINGFACE_API_URL, {
-			headers: {
-				"Upgrade-Insecure-Requests": "1",
-				"Sec-Fetch-Dest": "document",
-				"Sec-Fetch-Mode": "navigate",
-				"Sec-Fetch-Site": "none",
-				"Sec-Fetch-User": "?1",
-				Priority: "u=0, i",
-				Pragma: "no-cache",
-				"Cache-Control": "no-cache",
-			},
-			timeout: 10000,
-		})
-
-		const models = response.data?.data || []
-
-		return {
-			models,
-			cached: false,
-			timestamp: Date.now(),
-		}
-	} catch (error) {
-		console.error("Failed to get HuggingFace models:", error)
-		return {
-			models: [],
-			cached: false,
-			timestamp: Date.now(),
-		}
-	}
-}

+ 61 - 0
src/api/providers/fetchers/huggingface.ts

@@ -227,3 +227,64 @@ export function getCachedRawHuggingFaceModels(): HuggingFaceModel[] | null {
 export function clearHuggingFaceCache(): void {
 	cache = null
 }
+
+/**
+ * HuggingFace Models Response Interface
+ */
+export interface HuggingFaceModelsResponse {
+	models: HuggingFaceModel[]
+	cached: boolean
+	timestamp: number
+}
+
+/**
+ * Get HuggingFace models with response metadata
+ * This function provides a higher-level API that includes cache status and timestamp
+ */
+export async function getHuggingFaceModelsWithMetadata(): Promise<HuggingFaceModelsResponse> {
+	try {
+		// First, trigger the fetch to populate cache
+		await getHuggingFaceModels()
+
+		// Get the raw models from cache
+		const cachedRawModels = getCachedRawHuggingFaceModels()
+
+		if (cachedRawModels) {
+			return {
+				models: cachedRawModels,
+				cached: true,
+				timestamp: Date.now(),
+			}
+		}
+
+		// If no cached raw models, fetch directly from API
+		const response = await axios.get(HUGGINGFACE_API_URL, {
+			headers: {
+				"Upgrade-Insecure-Requests": "1",
+				"Sec-Fetch-Dest": "document",
+				"Sec-Fetch-Mode": "navigate",
+				"Sec-Fetch-Site": "none",
+				"Sec-Fetch-User": "?1",
+				Priority: "u=0, i",
+				Pragma: "no-cache",
+				"Cache-Control": "no-cache",
+			},
+			timeout: 10000,
+		})
+
+		const models = response.data?.data || []
+
+		return {
+			models,
+			cached: false,
+			timestamp: Date.now(),
+		}
+	} catch (error) {
+		console.error("Failed to get HuggingFace models:", error)
+		return {
+			models: [],
+			cached: false,
+			timestamp: Date.now(),
+		}
+	}
+}

+ 2 - 2
src/core/webview/webviewMessageHandler.ts

@@ -676,8 +676,8 @@ export const webviewMessageHandler = async (
 			break
 		case "requestHuggingFaceModels":
 			try {
-				const { getHuggingFaceModels } = await import("../../api/huggingface-models")
-				const huggingFaceModelsResponse = await getHuggingFaceModels()
+				const { getHuggingFaceModelsWithMetadata } = await import("../../api/providers/fetchers/huggingface")
+				const huggingFaceModelsResponse = await getHuggingFaceModelsWithMetadata()
 				provider.postMessageToWebview({
 					type: "huggingFaceModels",
 					huggingFaceModels: huggingFaceModelsResponse.models,