@ -184,7 +184,8 @@ async function fetchReplicateModels(
apiKey : string ,
apiKey : string ,
searchQuery? : string
searchQuery? : string
) : Promise < ProviderModel [ ] > {
) : Promise < ProviderModel [ ] > {
let url : string ;
const allModels : ProviderModel [ ] = [ ] ;
let url : string | null ;
let isSearchRequest = false ;
let isSearchRequest = false ;
if ( searchQuery ) {
if ( searchQuery ) {
@ -194,6 +195,11 @@ async function fetchReplicateModels(
url = ` ${ REPLICATE_API_BASE } /models ` ;
url = ` ${ REPLICATE_API_BASE } /models ` ;
}
}
// Paginate through all results (limit to 5 pages = ~125 models to avoid timeout)
let pageCount = 0 ;
const maxPages = 5 ;
while ( url && pageCount < maxPages ) {
const response = await fetch ( url , {
const response = await fetch ( url , {
headers : {
headers : {
Authorization : ` Bearer ${ apiKey } ` ,
Authorization : ` Bearer ${ apiKey } ` ,
@ -206,13 +212,21 @@ async function fetchReplicateModels(
if ( isSearchRequest ) {
if ( isSearchRequest ) {
const data : ReplicateSearchResponse = await response . json ( ) ;
const data : ReplicateSearchResponse = await response . json ( ) ;
if ( ! data . results ) return [ ] ;
if ( data . results ) {
return data . results . map ( ( result ) = > mapReplicateModel ( result . model ) ) ;
allModels . push ( . . . data . results . map ( ( result ) = > mapReplicateModel ( result . model ) ) ) ;
}
url = data . next ;
} else {
} else {
const data : ReplicateModelsResponse = await response . json ( ) ;
const data : ReplicateModelsResponse = await response . json ( ) ;
if ( ! data . results ) return [ ] ;
if ( data . results ) {
return data . results . map ( mapReplicateModel ) ;
allModels . push ( . . . data . results . map ( mapReplicateModel ) ) ;
}
}
url = data . next ;
}
pageCount ++ ;
}
return allModels ;
}
}
// ============ Fal.ai Helpers ============
// ============ Fal.ai Helpers ============
@ -245,17 +259,28 @@ async function fetchFalModels(
apiKey : string | null ,
apiKey : string | null ,
searchQuery? : string
searchQuery? : string
) : Promise < ProviderModel [ ] > {
) : Promise < ProviderModel [ ] > {
let url = ` ${ FAL_API_BASE } /models?status=active ` ;
const allModels : ProviderModel [ ] = [ ] ;
let cursor : string | null = null ;
if ( searchQuery ) {
let hasMore = true ;
url += ` &q= ${ encodeURIComponent ( searchQuery ) } ` ;
}
const headers : HeadersInit = { } ;
const headers : HeadersInit = { } ;
if ( apiKey ) {
if ( apiKey ) {
headers [ "Authorization" ] = ` Key ${ apiKey } ` ;
headers [ "Authorization" ] = ` Key ${ apiKey } ` ;
}
}
// Paginate through all results (limit to 10 pages to avoid timeout)
let pageCount = 0 ;
const maxPages = 10 ;
while ( hasMore && pageCount < maxPages ) {
let url = ` ${ FAL_API_BASE } /models?status=active ` ;
if ( searchQuery ) {
url += ` &q= ${ encodeURIComponent ( searchQuery ) } ` ;
}
if ( cursor ) {
url += ` &cursor= ${ encodeURIComponent ( cursor ) } ` ;
}
const response = await fetch ( url , { headers } ) ;
const response = await fetch ( url , { headers } ) ;
if ( ! response . ok ) {
if ( ! response . ok ) {
@ -263,8 +288,14 @@ async function fetchFalModels(
}
}
const data : FalModelsResponse = await response . json ( ) ;
const data : FalModelsResponse = await response . json ( ) ;
allModels . push ( . . . data . models . filter ( isRelevantFalModel ) . map ( mapFalModel ) ) ;
cursor = data . next_cursor ;
hasMore = data . has_more ;
pageCount ++ ;
}
return data . models . filter ( isRelevantFalModel ) . map ( mapFalModel ) ;
return al lModels ;
}
}
// ============ Main Handler ============
// ============ Main Handler ============