@@ -39,6 +39,8 @@ export const QUOTA_RESPONSE_MAX_BYTES = 512 * 1024;
3939const KIMI_CODE_BASE_URL = "https://api.kimi.com/coding/v1" ;
4040const KIMI_CODE_USAGE_URL = `${ KIMI_CODE_BASE_URL } /usages` ;
4141const A6API_BASE_URL = "https://api.a6api.com" ;
42+ const OPENCODE_GO_BASE_URL = "https://opencode.ai/zen/go/v1" ;
43+ const OPENCODE_GO_USAGE_URL = `${ OPENCODE_GO_BASE_URL } /usage` ;
4244const OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1" ;
4345const DEEPSEEK_BASE_URL = "https://api.deepseek.com" ;
4446const CLINE_BASE_URL = "https://api.cline.bot" ;
@@ -309,6 +311,10 @@ function isCanonicalA6apiBaseUrl(baseUrl: string): boolean {
309311 return normalized === A6API_BASE_URL || normalized === `${ A6API_BASE_URL } /v1` ;
310312}
311313
314+ function isCanonicalOpenCodeGoBaseUrl ( baseUrl : string ) : boolean {
315+ return normalizedBaseUrl ( baseUrl ) === OPENCODE_GO_BASE_URL ;
316+ }
317+
312318function isCanonicalOpenRouterBaseUrl ( baseUrl : string ) : boolean {
313319 const normalized = normalizedBaseUrl ( baseUrl ) ;
314320 return normalized === OPENROUTER_BASE_URL ;
@@ -456,6 +462,54 @@ async function fetchA6apiQuota(provider: string, config: OcxProviderConfig): Pro
456462 } ) ;
457463}
458464
465+ function parseOpenCodeGoUsageWindow ( value : unknown ) : { percent : number ; resetAt ?: number } | null {
466+ const row = asRecord ( value ) ;
467+ if ( ! row ) return null ;
468+ const percent = normalizePercent ( row . percent ) ;
469+ if ( percent === undefined ) return null ;
470+ const resetAt = normalizeResetAt ( row . resetsAt ) ;
471+ return { percent, ...( resetAt !== undefined ? { resetAt } : { } ) } ;
472+ }
473+
474+ async function fetchOpenCodeGoQuota ( provider : string , config : OcxProviderConfig ) : Promise < ProviderQuotaProbeResult > {
475+ // Never send a configured API key when the provider destination is not the built-in Go endpoint.
476+ if ( ! isCanonicalOpenCodeGoBaseUrl ( config . baseUrl ) ) return null ;
477+ const apiKey = resolveEnvValue ( config . apiKey ) ?. trim ( ) ;
478+ if ( ! apiKey ) return null ;
479+ const response = await fetch ( OPENCODE_GO_USAGE_URL , {
480+ headers : { Accept : "application/json" , Authorization : `Bearer ${ apiKey } ` } ,
481+ redirect : "error" ,
482+ signal : AbortSignal . timeout ( REQUEST_TIMEOUT_MS ) ,
483+ } ) ;
484+ if ( ! response . ok ) {
485+ return response . status >= 400 && response . status < 500 && response . status !== 408 && response . status !== 429
486+ ? TERMINAL_QUOTA_FAILURE
487+ : null ;
488+ }
489+ const body = asRecord ( await readQuotaJson ( response ) ) ;
490+ const usage = asRecord ( body ?. usage ) ;
491+ if ( ! usage ) return null ;
492+ const rolling = parseOpenCodeGoUsageWindow ( usage . rolling ) ;
493+ const weekly = parseOpenCodeGoUsageWindow ( usage . weekly ) ;
494+ const monthly = parseOpenCodeGoUsageWindow ( usage . monthly ) ;
495+ const quota : ProviderQuota = {
496+ ...( rolling ? {
497+ fiveHourPercent : rolling . percent ,
498+ ...( rolling . resetAt !== undefined ? { fiveHourResetAt : rolling . resetAt } : { } ) ,
499+ } : { } ) ,
500+ ...( weekly ? {
501+ weeklyPercent : weekly . percent ,
502+ ...( weekly . resetAt !== undefined ? { weeklyResetAt : weekly . resetAt } : { } ) ,
503+ } : { } ) ,
504+ ...( monthly ? {
505+ monthlyPercent : monthly . percent ,
506+ ...( monthly . resetAt !== undefined ? { monthlyResetAt : monthly . resetAt } : { } ) ,
507+ } : { } ) ,
508+ updatedAt : Date . now ( ) ,
509+ } ;
510+ return report ( provider , "opencode-go:usage" , quota ) ;
511+ }
512+
459513/**
460514 * OpenRouter `GET /api/v1/key` — the key's own credit balance and optional
461515 * per-key spending cap. `limit` is the configured cap (absent = uncapped);
@@ -1858,6 +1912,9 @@ async function maybeFetchProviderQuota(
18581912 if ( provider . authMode === "key" && isCanonicalKimiCodeBaseUrl ( provider . baseUrl ) ) {
18591913 return fetchKimiQuota ( name , provider ) ;
18601914 }
1915+ if ( ( provider . authMode ?? "key" ) === "key" && name === "opencode-go" ) {
1916+ return fetchOpenCodeGoQuota ( name , provider ) ;
1917+ }
18611918 if ( ( provider . authMode ?? "key" ) === "key" && isCanonicalA6apiBaseUrl ( provider . baseUrl ) ) {
18621919 return fetchA6apiQuota ( name , provider ) ;
18631920 }
0 commit comments