11import type { BYOKResult } from '@/lib/providers/types' ;
22import { kiloFreeModels } from '@/lib/models' ;
3- import { isAnthropicModel } from '@/lib/providers/anthropic' ;
43import { getGatewayErrorRate } from '@/lib/providers/gateway-error-rate' ;
5- import { isGeminiModel } from '@/lib/providers/google' ;
6- import { isMinimaxModel } from '@/lib/providers/minimax' ;
7- import { isMoonshotModel } from '@/lib/providers/moonshotai' ;
8- import { isOpenAiModel , isOpenAiOssModel } from '@/lib/providers/openai' ;
94import type { VercelUserByokInferenceProviderId } from '@/lib/providers/openrouter/inference-provider-id' ;
105import {
116 DirectUserByokInferenceProviderIdSchema ,
@@ -20,9 +15,11 @@ import type {
2015 VercelProviderConfig ,
2116} from '@/lib/providers/openrouter/types' ;
2217import { mapModelIdToVercel } from '@/lib/providers/vercel/mapModelIdToVercel' ;
23- import { mimo_v2_pro_free_model } from '@/lib/providers/xiaomi' ;
24- import { isZaiModel } from '@/lib/providers/zai' ;
2518import * as crypto from 'crypto' ;
19+ import { unstable_cache } from 'next/cache' ;
20+ import { readDb } from '@/lib/drizzle' ;
21+ import { modelsByProvider } from '@kilocode/db/schema' ;
22+ import { desc } from 'drizzle-orm' ;
2623
2724// EMERGENCY SWITCH
2825// This routes all models that normally would be routed to OpenRouter to Vercel instead.
@@ -61,6 +58,19 @@ function isLikelyAvailableOnAllGateways(requestedModel: string) {
6158 ) ;
6259}
6360
61+ const getVercelModels_cached = unstable_cache (
62+ async ( ) => {
63+ const result = await readDb
64+ . select ( { vercel : modelsByProvider . vercel } )
65+ . from ( modelsByProvider )
66+ . orderBy ( desc ( modelsByProvider . id ) )
67+ . limit ( 1 ) ;
68+ return result . at ( 0 ) ?. vercel ?? null ;
69+ } ,
70+ undefined ,
71+ { revalidate : 3600 }
72+ ) ;
73+
6474export async function shouldRouteToVercel (
6575 requestedModel : string ,
6676 request : GatewayRequest ,
@@ -90,25 +100,23 @@ export async function shouldRouteToVercel(
90100 return true ;
91101 }
92102
93- if (
94- ! isAnthropicModel ( requestedModel ) &&
95- ! isGeminiModel ( requestedModel ) &&
96- ! isMinimaxModel ( requestedModel ) &&
97- ! isMoonshotModel ( requestedModel ) &&
98- ! isOpenAiModel ( requestedModel ) &&
99- ! isOpenAiOssModel ( requestedModel ) &&
100- requestedModel !== mimo_v2_pro_free_model . public_id &&
101- ! isZaiModel ( requestedModel )
102- ) {
103- console . debug ( `[shouldRouteToVercel] model family not allowed for randomized Vercel routing` ) ;
103+ console . debug ( '[shouldRouteToVercel] randomizing user to either OpenRouter or Vercel' ) ;
104+ const passedRandomization =
105+ getRandomNumberLessThan100 ( 'vercel_routing_' + randomSeed ) <
106+ ( await getVercelRoutingPercentage ( ) ) ;
107+
108+ if ( ! passedRandomization ) {
104109 return false ;
105110 }
106111
107- console . debug ( '[shouldRouteToVercel] randomizing user to either OpenRouter or Vercel' ) ;
108- return (
109- getRandomNumberLessThan100 ( 'vercel_routing_' + randomSeed ) <
110- ( await getVercelRoutingPercentage ( ) )
111- ) ;
112+ const vercelModels = await getVercelModels_cached ( ) ;
113+ const vercelModelId = mapModelIdToVercel ( requestedModel ) ;
114+ if ( ! vercelModels || ! ( vercelModelId in vercelModels ) ) {
115+ console . debug ( `[shouldRouteToVercel] model not found in models_by_provider.vercel` ) ;
116+ return false ;
117+ }
118+
119+ return true ;
112120}
113121
114122function convertProviderOptions (
0 commit comments