@@ -18,7 +18,6 @@ import {
1818 isHaikuModel ,
1919} from '@/lib/providers/anthropic' ;
2020import { getBYOKforOrganization , getBYOKforUser , getModelUserByokProviders } from '@/lib/byok' ;
21- import type { CustomLlm } from '@kilocode/db/schema' ;
2221import { custom_llm , type User } from '@kilocode/db/schema' ;
2322import { OpenRouterInferenceProviderIdSchema } from '@/lib/providers/openrouter/inference-provider-id' ;
2423import { hasAttemptCompletionTool } from '@/lib/tool-calling' ;
@@ -35,6 +34,15 @@ import { isXiaomiModel } from '@/lib/providers/xiaomi';
3534import type { BYOKResult , Provider } from '@/lib/providers/types' ;
3635import PROVIDERS from '@/lib/providers/provider-definitions' ;
3736import { getCodingPlanModel } from '@/lib/providers/coding-plans' ;
37+ import type { CustomLlmProvider } from '@kilocode/db' ;
38+
39+ function inferSupportedChatApis ( aiSdkProvider : CustomLlmProvider ) {
40+ return aiSdkProvider === 'anthropic'
41+ ? ( [ 'messages' ] as const )
42+ : aiSdkProvider === 'openai'
43+ ? ( [ 'responses' ] as const )
44+ : ( [ 'chat_completions' ] as const ) ;
45+ }
3846
3947async function checkCodingPlanBYOK (
4048 user : User | AnonymousUserContext ,
@@ -56,13 +64,14 @@ async function checkCodingPlanBYOK(
5664 id : 'coding-plan' ,
5765 apiUrl : codingPlan . base_url ,
5866 apiKey : userByok [ 0 ] . decryptedAPIKey ,
67+ supportedChatApis : inferSupportedChatApis ( codingPlan . ai_sdk_provider ) ,
5968 transformRequest ( context ) {
6069 context . request . body . model = codingPlanModel . id ;
6170 codingPlan . transformRequest ( context ) ;
6271 } ,
6372 } satisfies Provider ,
6473 userByok,
65- customLlm : null ,
74+ bypassAccessCheck : false ,
6675 } ;
6776}
6877
@@ -85,7 +94,7 @@ export async function getProvider(
8594 user : User | AnonymousUserContext ,
8695 organizationId : string | undefined ,
8796 taskId : string | undefined
88- ) : Promise < { provider : Provider ; userByok : BYOKResult [ ] | null ; customLlm : CustomLlm | null } > {
97+ ) : Promise < { provider : Provider ; userByok : BYOKResult [ ] | null ; bypassAccessCheck : boolean } > {
8998 const codingPlanByok = await checkCodingPlanBYOK ( user , requestedModel , organizationId ) ;
9099 if ( codingPlanByok ) {
91100 return codingPlanByok ;
@@ -96,7 +105,7 @@ export async function getProvider(
96105 return {
97106 provider : PROVIDERS . VERCEL_AI_GATEWAY ,
98107 userByok : vercelByok ,
99- customLlm : null ,
108+ bypassAccessCheck : false ,
100109 } ;
101110 }
102111
@@ -111,6 +120,7 @@ export async function getProvider(
111120 id : 'custom' ,
112121 apiUrl : customLlm . base_url ,
113122 apiKey : customLlm . api_key ,
123+ supportedChatApis : inferSupportedChatApis ( customLlm . provider ) ,
114124 transformRequest ( context ) {
115125 Object . assign ( context . request . body , customLlm ?. extra_body ?? { } ) ;
116126 for ( const [ key , value ] of Object . entries ( customLlm . extra_headers ?? { } ) ) {
@@ -120,13 +130,13 @@ export async function getProvider(
120130 } ,
121131 } ,
122132 userByok : null ,
123- customLlm ,
133+ bypassAccessCheck : true ,
124134 } ;
125135 }
126136 }
127137
128138 if ( await shouldRouteToVercel ( requestedModel , request , taskId || user . id ) ) {
129- return { provider : PROVIDERS . VERCEL_AI_GATEWAY , userByok : null , customLlm : null } ;
139+ return { provider : PROVIDERS . VERCEL_AI_GATEWAY , userByok : null , bypassAccessCheck : false } ;
130140 }
131141
132142 const kiloFreeModel = kiloFreeModels . find ( m => m . public_id === requestedModel ) ;
@@ -135,7 +145,7 @@ export async function getProvider(
135145 return {
136146 provider : freeModelProvider ?? PROVIDERS . OPENROUTER ,
137147 userByok : null ,
138- customLlm : null ,
148+ bypassAccessCheck : false ,
139149 } ;
140150}
141151
0 commit comments