@@ -409,12 +409,14 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise<Resp
409409 }
410410
411411 if ( url . pathname === "/api/provider-presets/discover" && req . method === "POST" ) {
412- let body : { presetId ?: unknown ; provider ?: unknown } ;
412+ let body : { presetId ?: unknown ; provider ?: unknown ; freeOnly ?: unknown } ;
413413 try { body = await req . json ( ) ; } catch { return jsonResponse ( { error : "invalid JSON body" } , 400 ) ; }
414414 const presetId = typeof body . presetId === "string" ? body . presetId . trim ( ) : "" ;
415415 if ( ! presetId || ! isValidProviderName ( presetId ) ) return jsonResponse ( { error : "invalid presetId" } , 400 ) ;
416416 const entry = getProviderRegistryEntry ( presetId ) ;
417417 if ( ! entry ) return jsonResponse ( { error : "unknown provider preset" } , 404 ) ;
418+ if ( body . freeOnly !== undefined && typeof body . freeOnly !== "boolean" ) return jsonResponse ( { error : "freeOnly must be a boolean" } , 400 ) ;
419+ if ( body . freeOnly === true && presetId !== "openrouter" ) return jsonResponse ( { error : "freeOnly is only supported for OpenRouter" } , 400 ) ;
418420 if ( entry . supportLevel === "reference" ) {
419421 return jsonResponse ( { error : "reference-only provider presets cannot be connected automatically" } , 400 ) ;
420422 }
@@ -459,11 +461,14 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise<Resp
459461 id,
460462 ...( entry . modelContextWindows ?. [ id ] ? { contextWindow : entry . modelContextWindows [ id ] } : { } ) ,
461463 } ) ) ;
464+ const fallbackModels = body . freeOnly === true
465+ ? staticModels . filter ( model => model . id . endsWith ( ":free" ) )
466+ : staticModels ;
462467 const destinationError = await providerDestinationResolvedError ( presetId , provider ) ;
463468 if ( destinationError ) return jsonResponse ( { error : destinationError } , 400 ) ;
464469 if ( entry . discovery === "static" || entry . discovery === "unsupported" || entry . liveModels === false ) {
465- return jsonResponse ( staticModels . length
466- ? { ok : true , models : staticModels , source : "static" , latencyMs : 0 }
470+ return jsonResponse ( fallbackModels . length
471+ ? { ok : true , models : fallbackModels , source : "static" , latencyMs : 0 }
467472 : { ok : false , models : [ ] , source : "static" , latencyMs : 0 , error : "No static model catalog is available for this preset" } ) ;
468473 }
469474 const { resolveModelsAuthToken, buildModelsRequest } = await import ( "../../oauth" ) ;
@@ -477,17 +482,17 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise<Resp
477482 const latencyMs = Date . now ( ) - started ;
478483 if ( ! response . ok ) {
479484 const error = `upstream model discovery returned ${ response . status } ` ;
480- return jsonResponse ( staticModels . length
481- ? { ok : true , models : staticModels , source : "static" , latencyMs, error }
485+ return jsonResponse ( fallbackModels . length
486+ ? { ok : true , models : fallbackModels , source : "static" , latencyMs, error }
482487 : { ok : false , models : [ ] , source : "live" , latencyMs, error } ) ;
483488 }
484489 const allowsModelsEnvelope = ( entry . adapter === "google" && ( entry . googleMode ?? "ai-studio" ) === "ai-studio" )
485490 || new URL ( request . url ) . pathname . replace ( / \/ + $ / , "" ) . endsWith ( "/api/tags" ) ;
486491 const rows = parseProviderModelsApiItems ( await response . json ( ) . catch ( ( ) => null ) , allowsModelsEnvelope ) ;
487492 if ( ! rows ) {
488493 const error = "upstream model discovery returned an unexpected shape" ;
489- return jsonResponse ( staticModels . length
490- ? { ok : true , models : staticModels , source : "static" , latencyMs, error }
494+ return jsonResponse ( fallbackModels . length
495+ ? { ok : true , models : fallbackModels , source : "static" , latencyMs, error }
491496 : { ok : false , models : [ ] , source : "live" , latencyMs, error } ) ;
492497 }
493498 const uniqueRows = new Map < string , ( typeof rows ) [ number ] > ( ) ;
@@ -496,22 +501,34 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise<Resp
496501 if ( ! current || ( ! current . context_length && row . context_length ) ) uniqueRows . set ( row . id , row ) ;
497502 if ( uniqueRows . size >= 2000 ) break ;
498503 }
499- const models = [ ...uniqueRows . values ( ) ] . map ( row => ( {
504+ const filteredRows = body . freeOnly === true
505+ ? [ ...uniqueRows . values ( ) ] . filter ( row => {
506+ const prompt = Number ( row . pricing ?. prompt ) ;
507+ const completion = Number ( row . pricing ?. completion ) ;
508+ return row . pricing !== undefined
509+ && Number . isFinite ( prompt ) && prompt >= 0
510+ && Number . isFinite ( completion ) && completion >= 0
511+ && prompt === 0 && completion === 0 ;
512+ } )
513+ : [ ...uniqueRows . values ( ) ] ;
514+ const models = filteredRows . map ( row => ( {
500515 id : row . id ,
501516 ...( row . context_length ? { contextWindow : row . context_length } : { } ) ,
502517 } ) ) ;
503518 if ( models . length === 0 ) {
504- const error = "upstream model discovery returned an empty catalog" ;
505- return jsonResponse ( staticModels . length
506- ? { ok : true , models : staticModels , source : "static" , latencyMs, error }
519+ const error = body . freeOnly === true
520+ ? "OpenRouter currently reports no free models"
521+ : "upstream model discovery returned an empty catalog" ;
522+ return jsonResponse ( fallbackModels . length
523+ ? { ok : true , models : fallbackModels , source : "static" , latencyMs, error }
507524 : { ok : false , models : [ ] , source : "live" , latencyMs, error } ) ;
508525 }
509526 return jsonResponse ( { ok : true , models, source : "live" , latencyMs } ) ;
510527 } catch {
511528 const latencyMs = Date . now ( ) - started ;
512529 const error = "model discovery request failed" ;
513- return jsonResponse ( staticModels . length
514- ? { ok : true , models : staticModels , source : "static" , latencyMs, error }
530+ return jsonResponse ( fallbackModels . length
531+ ? { ok : true , models : fallbackModels , source : "static" , latencyMs, error }
515532 : { ok : false , models : [ ] , source : "live" , latencyMs, error } ) ;
516533 }
517534 }
0 commit comments