@@ -7,10 +7,18 @@ import {
77 getApiKeyFunProtocolUrls ,
88 resolveApiKeyFunBaseUrl ,
99} from './sponsor' ;
10+ import { CLAUDE_API_DISPLAY_NAME } from './claudeApi' ;
11+ import {
12+ CODE0_DISPLAY_NAME ,
13+ CODE0_PROTOCOL_LABELS ,
14+ getCode0ProtocolUrls ,
15+ resolveCode0BaseUrl ,
16+ } from './code0' ;
1017import type {
1118 ProviderBrand ,
1219 ProviderResource ,
1320 ProviderResourceSelector ,
21+ SponsorProviderBrand ,
1422 SponsorProviderRaw ,
1523} from './types' ;
1624
@@ -40,7 +48,7 @@ const truncateForId = (value: string | undefined | null): string => {
4048} ;
4149
4250function providerKeyToResource (
43- brand : 'gemini' | 'codex' | 'claude' | 'vertex' ,
51+ brand : 'gemini' | 'codex' | 'claude' | 'claudeApi' | ' vertex',
4452 config : GeminiKeyConfig | ProviderKeyConfig ,
4553 index : number
4654) : ProviderResource {
@@ -50,7 +58,7 @@ function providerKeyToResource(
5058 if ( brand === 'codex' ) {
5159 flags . websockets = ( config as ProviderKeyConfig ) . websockets === true ;
5260 }
53- if ( brand === 'claude' ) {
61+ if ( brand === 'claude' || brand === 'claudeApi' ) {
5462 const cloak = ( config as ProviderKeyConfig ) . cloak ;
5563 flags . cloakEnabled = Boolean ( cloak ?. mode ?. trim ( ) ) ;
5664 }
@@ -99,6 +107,14 @@ export function claudeToResource(config: ProviderKeyConfig, index: number): Prov
99107 return providerKeyToResource ( 'claude' , config , index ) ;
100108}
101109
110+ export function claudeApiToResource ( config : ProviderKeyConfig , index : number ) : ProviderResource {
111+ const resource = providerKeyToResource ( 'claudeApi' , config , index ) ;
112+ return {
113+ ...resource ,
114+ name : CLAUDE_API_DISPLAY_NAME ,
115+ } ;
116+ }
117+
102118export function vertexToResource ( config : ProviderKeyConfig , index : number ) : ProviderResource {
103119 return providerKeyToResource ( 'vertex' , config , index ) ;
104120}
@@ -132,22 +148,49 @@ export function openaiToResource(config: OpenAIProviderConfig, index: number): P
132148 } ;
133149}
134150
135- export function apiKeyFunToResource ( raw : SponsorProviderRaw ) : ProviderResource | null {
136- if ( raw . openai . length === 0 && raw . claude . length === 0 && raw . codex . length === 0 ) {
151+ interface SponsorResourceOptions {
152+ displayName : string ;
153+ protocolLabels : readonly string [ ] ;
154+ resolveBaseUrl : ( value : string | undefined | null ) => string ;
155+ getProtocolUrls : ( value : string | undefined | null ) => {
156+ anthropic : string ;
157+ openai : string ;
158+ codex : string ;
159+ gemini : string ;
160+ } ;
161+ }
162+
163+ function sponsorRawToResource (
164+ brand : SponsorProviderBrand ,
165+ raw : SponsorProviderRaw ,
166+ options : SponsorResourceOptions
167+ ) : ProviderResource | null {
168+ if (
169+ raw . openai . length === 0 &&
170+ raw . claude . length === 0 &&
171+ raw . codex . length === 0 &&
172+ raw . gemini . length === 0
173+ ) {
137174 return null ;
138175 }
139176 const openaiKeyCount = raw . openai . reduce (
140177 ( count , item ) => count + ( item . config . apiKeyEntries ?. length ?? 0 ) ,
141178 0
142179 ) ;
143180 const codexKeyCount = raw . codex . length ;
181+ const geminiKeyCount = raw . gemini . length ;
144182 const firstOpenAIEntry = raw . openai
145183 . flatMap ( ( item ) => item . config . apiKeyEntries ?? [ ] )
146184 . find ( ( entry ) => entry . apiKey ?. trim ( ) ) ;
147185 const firstCodex = raw . codex . find ( ( item ) => item . config . apiKey ?. trim ( ) ) ;
148186 const firstClaude = raw . claude . find ( ( item ) => item . config . apiKey ?. trim ( ) ) ;
187+ const firstGemini = raw . gemini . find ( ( item ) => item . config . apiKey ?. trim ( ) ) ;
149188 const apiKey =
150- firstOpenAIEntry ?. apiKey ?? firstCodex ?. config . apiKey ?? firstClaude ?. config . apiKey ?? '' ;
189+ firstOpenAIEntry ?. apiKey ??
190+ firstCodex ?. config . apiKey ??
191+ firstClaude ?. config . apiKey ??
192+ firstGemini ?. config . apiKey ??
193+ '' ;
151194 const openaiDisabled =
152195 raw . openai . length > 0 && raw . openai . every ( ( item ) => item . config . disabled === true ) ;
153196 const codexDisabled =
@@ -156,53 +199,70 @@ export function apiKeyFunToResource(raw: SponsorProviderRaw): ProviderResource |
156199 const claudeDisabled =
157200 raw . claude . length > 0 &&
158201 raw . claude . every ( ( item ) => hasDisableAllModelsRule ( item . config . excludedModels ) ) ;
202+ const geminiDisabled =
203+ raw . gemini . length > 0 &&
204+ raw . gemini . every ( ( item ) => hasDisableAllModelsRule ( item . config . excludedModels ) ) ;
159205 const enabledCount =
160206 ( raw . openai . length > 0 && ! openaiDisabled ? 1 : 0 ) +
161207 ( raw . codex . length > 0 && ! codexDisabled ? 1 : 0 ) +
162- ( raw . claude . length > 0 && ! claudeDisabled ? 1 : 0 ) ;
208+ ( raw . claude . length > 0 && ! claudeDisabled ? 1 : 0 ) +
209+ ( raw . gemini . length > 0 && ! geminiDisabled ? 1 : 0 ) ;
163210 const allResourcesConfigured =
164- raw . openai . length > 0 || raw . codex . length > 0 || raw . claude . length > 0 ;
211+ raw . openai . length > 0 ||
212+ raw . codex . length > 0 ||
213+ raw . claude . length > 0 ||
214+ raw . gemini . length > 0 ;
165215 const disabled = allResourcesConfigured && enabledCount === 0 ;
166216 const models = [
167217 ...raw . openai . flatMap ( ( item ) => collectModelNames ( item . config . models ) ) ,
168218 ...raw . codex . flatMap ( ( item ) => collectModelNames ( item . config . models ) ) ,
169219 ...raw . claude . flatMap ( ( item ) => collectModelNames ( item . config . models ) ) ,
220+ ...raw . gemini . flatMap ( ( item ) => collectModelNames ( item . config . models ) ) ,
170221 ] ;
171222 const uniqueModels = Array . from ( new Set ( models ) ) ;
172223 const headerCount =
173224 raw . openai . reduce ( ( count , item ) => count + countHeaders ( item . config . headers ) , 0 ) +
174225 raw . codex . reduce ( ( count , item ) => count + countHeaders ( item . config . headers ) , 0 ) +
175- raw . claude . reduce ( ( count , item ) => count + countHeaders ( item . config . headers ) , 0 ) ;
226+ raw . claude . reduce ( ( count , item ) => count + countHeaders ( item . config . headers ) , 0 ) +
227+ raw . gemini . reduce ( ( count , item ) => count + countHeaders ( item . config . headers ) , 0 ) ;
176228 const priority = Math . max (
177229 0 ,
178230 ...raw . openai . map ( ( item ) => normalizePriority ( item . config . priority ) ) ,
179231 ...raw . codex . map ( ( item ) => normalizePriority ( item . config . priority ) ) ,
180- ...raw . claude . map ( ( item ) => normalizePriority ( item . config . priority ) )
232+ ...raw . claude . map ( ( item ) => normalizePriority ( item . config . priority ) ) ,
233+ ...raw . gemini . map ( ( item ) => normalizePriority ( item . config . priority ) )
181234 ) ;
182- const baseUrl = resolveApiKeyFunBaseUrl (
183- raw . openai [ 0 ] ?. config . baseUrl ?? raw . codex [ 0 ] ?. config . baseUrl ?? raw . claude [ 0 ] ?. config . baseUrl
235+ const baseUrl = options . resolveBaseUrl (
236+ raw . openai [ 0 ] ?. config . baseUrl ??
237+ raw . codex [ 0 ] ?. config . baseUrl ??
238+ raw . claude [ 0 ] ?. config . baseUrl ??
239+ raw . gemini [ 0 ] ?. config . baseUrl
184240 ) ;
185- const protocolUrls = getApiKeyFunProtocolUrls ( baseUrl ) ;
241+ const protocolUrls = options . getProtocolUrls ( baseUrl ) ;
186242
187243 return {
188- id : buildId ( 'apikeyFun' , 0 , 'sponsor' ) ,
189- brand : 'apikeyFun' ,
244+ id : buildId ( brand , 0 , 'sponsor' ) ,
245+ brand,
190246 originalIndex : 0 ,
191- name : APIKEY_FUN_DISPLAY_NAME ,
192- identifier : APIKEY_FUN_DISPLAY_NAME ,
247+ name : options . displayName ,
248+ identifier : options . displayName ,
193249 apiKeyPreview : apiKey ? maskApiKey ( apiKey ) : null ,
194250 apiKey : apiKey || null ,
195251 authIndex : null ,
196- baseUrl : `${ protocolUrls . openai } / ${ protocolUrls . anthropic } ` ,
252+ baseUrl : [ protocolUrls . openai , protocolUrls . anthropic , protocolUrls . gemini ]
253+ . filter ( Boolean )
254+ . join ( ' / ' ) ,
197255 proxyUrl :
198256 firstOpenAIEntry ?. proxyUrl ??
199257 raw . codex . find ( ( item ) => item . config . proxyUrl ) ?. config . proxyUrl ??
200258 raw . claude . find ( ( item ) => item . config . proxyUrl ) ?. config . proxyUrl ??
259+ raw . gemini . find ( ( item ) => item . config . proxyUrl ) ?. config . proxyUrl ??
201260 null ,
202261 prefix :
203262 raw . openai [ 0 ] ?. config . prefix ??
204263 raw . codex [ 0 ] ?. config . prefix ??
205264 raw . claude [ 0 ] ?. config . prefix ??
265+ raw . gemini [ 0 ] ?. config . prefix ??
206266 null ,
207267 modelCount : uniqueModels . length ,
208268 models : uniqueModels ,
@@ -216,18 +276,41 @@ export function apiKeyFunToResource(raw: SponsorProviderRaw): ProviderResource |
216276 raw . claude . reduce (
217277 ( count , item ) => count + stripDisableAllModelsRule ( item . config . excludedModels ) . length ,
218278 0
279+ ) +
280+ raw . gemini . reduce (
281+ ( count , item ) => count + stripDisableAllModelsRule ( item . config . excludedModels ) . length ,
282+ 0
219283 ) ,
220- apiKeyEntryCount : openaiKeyCount + codexKeyCount + raw . claude . length ,
284+ apiKeyEntryCount : openaiKeyCount + codexKeyCount + raw . claude . length + geminiKeyCount ,
221285 disabled,
222286 flags : {
223- protocols : [ ...APIKEY_FUN_PROTOCOLS ] ,
287+ protocols : [ ...options . protocolLabels ] ,
224288 } ,
225289 selector : {
226- brand : 'apikeyFun' ,
290+ brand,
227291 openaiIndices : raw . openai . map ( ( item ) => item . index ) ,
228292 claudeIndices : raw . claude . map ( ( item ) => item . index ) ,
229293 codexIndices : raw . codex . map ( ( item ) => item . index ) ,
294+ geminiIndices : raw . gemini . map ( ( item ) => item . index ) ,
230295 } as ProviderResourceSelector ,
231296 raw,
232297 } ;
233298}
299+
300+ export function apiKeyFunToResource ( raw : SponsorProviderRaw ) : ProviderResource | null {
301+ return sponsorRawToResource ( 'apikeyFun' , raw , {
302+ displayName : APIKEY_FUN_DISPLAY_NAME ,
303+ protocolLabels : APIKEY_FUN_PROTOCOLS ,
304+ resolveBaseUrl : resolveApiKeyFunBaseUrl ,
305+ getProtocolUrls : getApiKeyFunProtocolUrls ,
306+ } ) ;
307+ }
308+
309+ export function code0ToResource ( raw : SponsorProviderRaw ) : ProviderResource | null {
310+ return sponsorRawToResource ( 'code0' , raw , {
311+ displayName : CODE0_DISPLAY_NAME ,
312+ protocolLabels : CODE0_PROTOCOL_LABELS ,
313+ resolveBaseUrl : resolveCode0BaseUrl ,
314+ getProtocolUrls : getCode0ProtocolUrls ,
315+ } ) ;
316+ }
0 commit comments