11export interface HuggingFaceModel {
2- _id : string
32 id : string
4- inferenceProviderMapping : InferenceProviderMapping [ ]
5- trendingScore : number
6- config : ModelConfig
7- tags : string [ ]
8- pipeline_tag : "text-generation" | "image-text-to-text"
9- library_name ?: string
3+ object : "model"
4+ created : number
5+ owned_by : string
6+ providers : Provider [ ]
107}
118
12- export interface InferenceProviderMapping {
9+ export interface Provider {
1310 provider : string
14- providerId : string
1511 status : "live" | "staging" | "error"
16- task : "conversational"
17- }
18-
19- export interface ModelConfig {
20- architectures : string [ ]
21- model_type : string
22- tokenizer_config ?: {
23- chat_template ?: string | Array < { name : string ; template : string } >
24- model_max_length ?: number
12+ supports_tools ?: boolean
13+ supports_structured_output ?: boolean
14+ context_length ?: number
15+ pricing ?: {
16+ input : number
17+ output : number
2518 }
2619}
2720
28- interface HuggingFaceApiParams {
29- pipeline_tag ?: "text-generation" | "image-text-to-text"
30- filter : string
31- inference_provider : string
32- limit : number
33- expand : string [ ]
34- }
35-
36- const DEFAULT_PARAMS : HuggingFaceApiParams = {
37- filter : "conversational" ,
38- inference_provider : "all" ,
39- limit : 100 ,
40- expand : [
41- "inferenceProviderMapping" ,
42- "config" ,
43- "library_name" ,
44- "pipeline_tag" ,
45- "tags" ,
46- "mask_token" ,
47- "trendingScore" ,
48- ] ,
21+ interface HuggingFaceApiResponse {
22+ object : string
23+ data : HuggingFaceModel [ ]
4924}
5025
51- const BASE_URL = "https://huggingface.co/api /models"
26+ const BASE_URL = "https://router. huggingface.co/v1 /models?collection=roocode "
5227const CACHE_DURATION = 1000 * 60 * 60 // 1 hour
5328
5429interface CacheEntry {
@@ -59,24 +34,6 @@ interface CacheEntry {
5934
6035let cache : CacheEntry | null = null
6136
62- function buildApiUrl ( params : HuggingFaceApiParams ) : string {
63- const url = new URL ( BASE_URL )
64-
65- // Add simple params
66- Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
67- if ( ! Array . isArray ( value ) ) {
68- url . searchParams . append ( key , String ( value ) )
69- }
70- } )
71-
72- // Handle array params specially
73- params . expand . forEach ( ( item ) => {
74- url . searchParams . append ( "expand[]" , item )
75- } )
76-
77- return url . toString ( )
78- }
79-
8037const headers : HeadersInit = {
8138 "Upgrade-Insecure-Requests" : "1" ,
8239 "Sec-Fetch-Dest" : "document" ,
@@ -107,45 +64,25 @@ export async function fetchHuggingFaceModels(): Promise<HuggingFaceModel[]> {
10764 try {
10865 console . log ( "Fetching Hugging Face models from API..." )
10966
110- // Fetch both text-generation and image-text-to-text models in parallel
111- const [ textGenResponse , imgTextResponse ] = await Promise . allSettled ( [
112- fetch ( buildApiUrl ( { ...DEFAULT_PARAMS , pipeline_tag : "text-generation" } ) , requestInit ) ,
113- fetch ( buildApiUrl ( { ...DEFAULT_PARAMS , pipeline_tag : "image-text-to-text" } ) , requestInit ) ,
114- ] )
115-
116- let textGenModels : HuggingFaceModel [ ] = [ ]
117- let imgTextModels : HuggingFaceModel [ ] = [ ]
118- let hasErrors = false
119-
120- // Process text-generation models
121- if ( textGenResponse . status === "fulfilled" && textGenResponse . value . ok ) {
122- textGenModels = await textGenResponse . value . json ( )
123- } else {
124- console . error ( "Failed to fetch text-generation models:" , textGenResponse )
125- hasErrors = true
126- }
67+ const response = await fetch ( BASE_URL , requestInit )
12768
128- // Process image-text-to-text models
129- if ( imgTextResponse . status === "fulfilled" && imgTextResponse . value . ok ) {
130- imgTextModels = await imgTextResponse . value . json ( )
131- } else {
132- console . error ( "Failed to fetch image-text-to-text models:" , imgTextResponse )
133- hasErrors = true
69+ if ( ! response . ok ) {
70+ throw new Error ( `HTTP error! status: ${ response . status } ` )
13471 }
13572
136- // Combine and filter models
137- const allModels = [ ... textGenModels , ... imgTextModels ]
138- . filter ( ( model ) => model . inferenceProviderMapping . length > 0 )
73+ const apiResponse : HuggingFaceApiResponse = await response . json ( )
74+ const allModels = apiResponse . data
75+ . filter ( ( model ) => model . providers . length > 0 )
13976 . sort ( ( a , b ) => a . id . toLowerCase ( ) . localeCompare ( b . id . toLowerCase ( ) ) )
14077
14178 // Update cache
14279 cache = {
14380 data : allModels ,
14481 timestamp : now ,
145- status : hasErrors ? "partial" : "success" ,
82+ status : "success" ,
14683 }
14784
148- console . log ( `Fetched ${ allModels . length } Hugging Face models (status: ${ cache . status } ) ` )
85+ console . log ( `Fetched ${ allModels . length } Hugging Face models` )
14986 return allModels
15087 } catch ( error ) {
15188 console . error ( "Error fetching Hugging Face models:" , error )
0 commit comments