@@ -15,6 +15,7 @@ const CACHE_TTL_MS = 5 * 60_000;
1515const REQUEST_TIMEOUT_MS = 8_000 ;
1616const KIMI_CODE_BASE_URL = "https://api.kimi.com/coding/v1" ;
1717const KIMI_CODE_USAGE_URL = `${ KIMI_CODE_BASE_URL } /usages` ;
18+ const A6API_BASE_URL = "https://api.a6api.com" ;
1819/** Keep a failed probe's previous row at most this long before dropping it. */
1920const LAST_GOOD_MAX_AGE_MS = 30 * 60_000 ;
2021
@@ -118,6 +119,61 @@ function isBuiltInChatGptForwardProvider(name: string, provider: OcxProviderConf
118119 return name === OPENAI_CODEX_PROVIDER_ID && isCanonicalOpenAiForwardProvider ( provider ) ;
119120}
120121
122+ function isCanonicalA6apiBaseUrl ( baseUrl : string ) : boolean {
123+ const normalized = baseUrl . trim ( ) . replace ( / \/ + $ / , "" ) ;
124+ return normalized === A6API_BASE_URL || normalized === `${ A6API_BASE_URL } /v1` ;
125+ }
126+
127+ function a6apiPayload ( value : unknown ) : Record < string , unknown > | null {
128+ const body = asRecord ( value ) ;
129+ return asRecord ( body ?. data ) ?? body ;
130+ }
131+
132+ function firstFinite ( record : Record < string , unknown > | null , names : string [ ] ) : number | undefined {
133+ if ( ! record ) return undefined ;
134+ for ( const name of names ) {
135+ const value = toFiniteNumber ( record [ name ] ) ;
136+ if ( value !== undefined ) return value ;
137+ }
138+ return undefined ;
139+ }
140+
141+ async function fetchA6apiQuota ( provider : string , config : OcxProviderConfig ) : Promise < ProviderQuotaReport | null > {
142+ // Never send a configured API key to a lookalike host or through a redirect.
143+ if ( ! isCanonicalA6apiBaseUrl ( config . baseUrl ) ) return null ;
144+ const apiKey = resolveEnvValue ( config . apiKey ) ?. trim ( ) ;
145+ if ( ! apiKey ) return null ;
146+ const headers = { Accept : "application/json" , Authorization : `Bearer ${ apiKey } ` } as const ;
147+ const [ subscriptionResponse , tokenResponse ] = await Promise . all ( [
148+ fetch ( `${ A6API_BASE_URL } /dashboard/billing/subscription` , {
149+ headers, redirect : "error" , signal : AbortSignal . timeout ( REQUEST_TIMEOUT_MS ) ,
150+ } ) ,
151+ fetch ( `${ A6API_BASE_URL } /api/usage/token/` , {
152+ headers, redirect : "error" , signal : AbortSignal . timeout ( REQUEST_TIMEOUT_MS ) ,
153+ } ) ,
154+ ] ) ;
155+ if ( ! subscriptionResponse . ok || ! tokenResponse . ok ) return null ;
156+ const subscription = a6apiPayload ( await subscriptionResponse . json ( ) . catch ( ( ) => null ) ) ;
157+ const token = a6apiPayload ( await tokenResponse . json ( ) . catch ( ( ) => null ) ) ;
158+ const limitUsd = firstFinite ( subscription , [ "hard_limit_usd" ] ) ;
159+ const grantedUnits = firstFinite ( token , [ "total_granted" ] ) ;
160+ const usedUnits = firstFinite ( token , [ "total_used" ] ) ;
161+ const availableUnits = firstFinite ( token , [ "total_available" ] ) ;
162+ if ( limitUsd === undefined || grantedUnits === undefined || usedUnits === undefined
163+ || availableUnits === undefined || limitUsd <= 0 || grantedUnits <= 0 ) return null ;
164+ const usdPerUnit = limitUsd / grantedUnits ;
165+ const usedUsd = usedUnits * usdPerUnit ;
166+ const remainingUsd = Math . max ( 0 , availableUnits * usdPerUnit ) ;
167+ const percent = normalizePercent ( ( usedUsd / limitUsd ) * 100 ) ;
168+ if ( percent === undefined ) return null ;
169+ const resetAt = normalizeResetAt ( token ?. expires_at ) ;
170+ const label = `API credits ($${ remainingUsd . toFixed ( 2 ) } of $${ limitUsd . toFixed ( 2 ) } remaining)` ;
171+ return report ( provider , "a6api:billing" , {
172+ customWindows : [ { label, percent, ...( resetAt !== undefined ? { resetAt } : { } ) } ] ,
173+ updatedAt : Date . now ( ) ,
174+ } ) ;
175+ }
176+
121177function report ( provider : string , source : string , quota : ProviderQuota ) : ProviderQuotaReport | null {
122178 if ( ! hasQuotaRows ( quota ) ) return null ;
123179 return {
@@ -853,6 +909,9 @@ async function maybeFetchProviderQuota(
853909 if ( provider . authMode === "key" && isCanonicalKimiCodeBaseUrl ( provider . baseUrl ) ) {
854910 return fetchKimiQuota ( name , provider ) ;
855911 }
912+ if ( ( provider . authMode ?? "key" ) === "key" && name === "a6api" ) {
913+ return fetchA6apiQuota ( name , provider ) ;
914+ }
856915 return null ;
857916 } catch {
858917 return null ;
0 commit comments