The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | ProviderDriverKind, |
| 3 | UsageLimitSourceError, |
| 4 | type ProviderConsumeResetCreditResult, |
| 5 | type UsageLimitSourceAccount, |
| 6 | type UsageLimitSourceConfig, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 9 | import * as Cause from "effect/Cause"; |
| 10 | import * as Digest from "effect/crypto/Digest"; |
| 11 | import * as DateTime from "effect/DateTime"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Hex from "effect/encoding/Hex"; |
| 14 | import * as Option from "effect/Option"; |
| 15 | import * as Schema from "effect/Schema"; |
| 16 | import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 17 | |
| 18 | import { |
| 19 | GROK_USAGE_URL, |
| 20 | GrokUsageResponse, |
| 21 | grokUsageResponseToLimits, |
| 22 | } from "@t3tools/provider-grok/server"; |
| 23 | import { codexPlanLabel } from "../provider/CodexProvider.ts"; |
| 24 | import { codexRateLimitsToLimits } from "../provider/codexUsageLimits.ts"; |
| 25 | import { claudeUsageResponseToLimits } from "../provider/claudeUsageLimits.ts"; |
| 26 | import { |
| 27 | antigravityAccountFingerprint, |
| 28 | antigravitySummaryWindows, |
| 29 | antigravityUserAgent, |
| 30 | } from "../provider/antigravityUsageLimits.ts"; |
| 31 | import { |
| 32 | makeUnavailableUsageLimits, |
| 33 | makeUsageLimits, |
| 34 | } from "@t3tools/provider-core/server/usageLimits"; |
| 35 | |
| 36 | /** A hub with its management key already read from wherever the setting keeps it. */ |
| 37 | export interface CliproxyHub { |
| 38 | readonly url: UsageLimitSourceConfig["url"]; |
| 39 | readonly managementKey: string; |
| 40 | } |
| 41 | |
| 42 | const AuthFile = Schema.Struct({ |
| 43 | id: Schema.String, |
| 44 | auth_index: Schema.String, |
| 45 | provider: Schema.String, |
| 46 | email: Schema.optional(Schema.String), |
| 47 | disabled: Schema.optional(Schema.Boolean), |
| 48 | project_id: Schema.optional(Schema.String), |
| 49 | id_token: Schema.optional( |
| 50 | Schema.Struct({ |
| 51 | chatgpt_account_id: Schema.optional(Schema.String), |
| 52 | chatgpt_plan_type: Schema.optional(Schema.String), |
| 53 | plan_type: Schema.optional(Schema.String), |
| 54 | }), |
| 55 | ), |
| 56 | }); |
| 57 | const AuthFiles = Schema.Struct({ files: Schema.Array(AuthFile) }); |
| 58 | const AuthFileModels = Schema.Struct({ |
| 59 | models: Schema.Array(Schema.Struct({ id: Schema.String })), |
| 60 | }); |
| 61 | const ApiResponse = Schema.Struct({ status_code: Schema.Number, body: Schema.String }); |
| 62 | const CodexWindow = Schema.Struct({ |
| 63 | used_percent: Schema.Number, |
| 64 | reset_at: Schema.optional(Schema.NullOr(Schema.Number)), |
| 65 | limit_window_seconds: Schema.optional(Schema.Number), |
| 66 | }); |
| 67 | const CodexUsage = Schema.Struct({ |
| 68 | plan_type: Schema.optional(Schema.String), |
| 69 | rate_limit: Schema.NullOr( |
| 70 | Schema.Struct({ |
| 71 | primary_window: Schema.optional(Schema.NullOr(CodexWindow)), |
| 72 | secondary_window: Schema.optional(Schema.NullOr(CodexWindow)), |
| 73 | }), |
| 74 | ), |
| 75 | }); |
| 76 | // The usage endpoint can report a window without a utilization, as the CLI's own schema allows. |
| 77 | const ClaudeWindow = Schema.Struct({ |
| 78 | utilization: Schema.NullOr(Schema.Number), |
| 79 | resets_at: Schema.NullOr(Schema.String), |
| 80 | }); |
| 81 | const ClaudeUsage = Schema.Struct({ |
| 82 | five_hour: Schema.optional(Schema.NullOr(ClaudeWindow)), |
| 83 | seven_day: Schema.optional(Schema.NullOr(ClaudeWindow)), |
| 84 | spend: Schema.optional(Schema.Unknown), |
| 85 | extra_usage: Schema.optional( |
| 86 | Schema.NullOr( |
| 87 | Schema.Struct({ |
| 88 | is_enabled: Schema.Boolean, |
| 89 | monthly_limit: Schema.NullOr(Schema.Number), |
| 90 | used_credits: Schema.NullOr(Schema.Number), |
| 91 | utilization: Schema.NullOr(Schema.Number), |
| 92 | }), |
| 93 | ), |
| 94 | ), |
| 95 | limits: Schema.optional( |
| 96 | Schema.Array( |
| 97 | Schema.Struct({ |
| 98 | kind: Schema.String, |
| 99 | percent: Schema.optional(Schema.NullOr(Schema.Number)), |
| 100 | resets_at: Schema.optional(Schema.NullOr(Schema.String)), |
| 101 | scope: Schema.optional( |
| 102 | Schema.NullOr( |
| 103 | Schema.Struct({ |
| 104 | model: Schema.optional(Schema.NullOr(Schema.Struct({ display_name: Schema.String }))), |
| 105 | }), |
| 106 | ), |
| 107 | ), |
| 108 | }), |
| 109 | ), |
| 110 | ), |
| 111 | }); |
| 112 | const CreditList = Schema.Struct({ |
| 113 | credits: Schema.Array( |
| 114 | Schema.Struct({ |
| 115 | id: Schema.String, |
| 116 | status: Schema.String, |
| 117 | reset_type: Schema.String, |
| 118 | expires_at: Schema.String, |
| 119 | }), |
| 120 | ), |
| 121 | }); |
| 122 | |
| 123 | const decodeAuthFiles = Schema.decodeUnknownEffect(AuthFiles); |
| 124 | const decodeAuthFileModels = Schema.decodeUnknownEffect(AuthFileModels); |
| 125 | const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); |
| 126 | const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); |
| 127 | const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList)); |
| 128 | const decodeClaudeUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeUsage)); |
| 129 | const decodeCodexUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(CodexUsage)); |
| 130 | const decodeGrokUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(GrokUsageResponse)); |
| 131 | const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); |
| 132 | const isUsageLimitSourceError = Schema.is(UsageLimitSourceError); |
| 133 | |
| 134 | // Extra CAs can fix untrusted chains; expiry/hostname failures report codes only. |
| 135 | const UNTRUSTED_CERTIFICATE_CODES = new Set([ |
| 136 | "DEPTH_ZERO_SELF_SIGNED_CERT", |
| 137 | "SELF_SIGNED_CERT_IN_CHAIN", |
| 138 | "UNABLE_TO_GET_ISSUER_CERT_LOCALLY", |
| 139 | "UNABLE_TO_VERIFY_LEAF_SIGNATURE", |
| 140 | ]); |
| 141 | |
| 142 | // fetch wraps Node error codes in generic TypeErrors. |
| 143 | const nodeErrorCode = (cause: unknown) => { |
| 144 | for (let error = cause, depth = 0; error instanceof Error && depth < 3; depth += 1) { |
| 145 | const code = (error as { readonly code?: unknown }).code; |
| 146 | if (typeof code === "string" && /^[A-Z0-9_]{1,64}$/.test(code)) return code; |
| 147 | error = error.cause; |
| 148 | } |
| 149 | return undefined; |
| 150 | }; |
| 151 | |
| 152 | // Hub responses can contain credentials; expose only failure kind, HTTP status or Node code. |
| 153 | const describeManagementFailure = (error: unknown) => { |
| 154 | if (Cause.isTimeoutError(error)) return "The hub did not answer within 15 seconds."; |
| 155 | if (HttpClientError.isHttpClientError(error)) { |
| 156 | const reason = error.reason; |
| 157 | if (reason._tag === "StatusCodeError") { |
| 158 | return `The hub answered with HTTP ${reason.response.status}.`; |
| 159 | } |
| 160 | const code = reason._tag === "TransportError" ? nodeErrorCode(reason.cause) : undefined; |
| 161 | if (code !== undefined && UNTRUSTED_CERTIFICATE_CODES.has(code)) { |
| 162 | return `The hub's TLS certificate is not trusted (${code}). If it comes from a private CA, add that CA to NODE_EXTRA_CA_CERTS.`; |
| 163 | } |
| 164 | if (code !== undefined) return `The hub request failed (${code}).`; |
| 165 | } |
| 166 | return "The hub management request failed."; |
| 167 | }; |
| 168 | const decodeConsumeResponse = Schema.decodeUnknownEffect( |
| 169 | Schema.fromJsonString( |
| 170 | Schema.Struct({ |
| 171 | code: Schema.Literals(["reset", "nothing_to_reset", "no_credit", "already_redeemed"]), |
| 172 | }), |
| 173 | ), |
| 174 | ); |
| 175 | |
| 176 | const CODEX_BASE = "https://chatgpt.com/backend-api/wham"; |
| 177 | const CREDIT_URL = `${CODEX_BASE}/rate-limit-reset-credits`; |
| 178 | |
| 179 | const CREDIT_REDEEM_NAMESPACE = new Uint8Array([ |
| 180 | 0x6f, 0x1c, 0x2a, 0x9e, 0x2d, 0x4b, 0x4c, 0x1e, 0x9a, 0x7f, 0x3b, 0x8d, 0x5e, 0x0c, 0x1a, 0x42, |
| 181 | ]); |
| 182 | |
| 183 | // UUIDv5 per account and credit also deduplicates retries across T3 environments. |
| 184 | const creditRedeemRequestId = Effect.fn("CliproxyApi.creditRedeemRequestId")(function* ( |
| 185 | accountId: string, |
| 186 | creditId: string, |
| 187 | ) { |
| 188 | const digest = yield* Digest.Digest; |
| 189 | const name = new TextEncoder().encode(`${accountId}:${creditId}`); |
| 190 | const input = new Uint8Array(CREDIT_REDEEM_NAMESPACE.length + name.length); |
| 191 | input.set(CREDIT_REDEEM_NAMESPACE); |
| 192 | input.set(name, CREDIT_REDEEM_NAMESPACE.length); |
| 193 | const bytes = (yield* digest.digest(Digest.sha1, input).pipe(Effect.orDie)).slice(0, 16); |
| 194 | bytes[6] = (bytes[6]! & 0x0f) | 0x50; |
| 195 | bytes[8] = (bytes[8]! & 0x3f) | 0x80; |
| 196 | const hex = Hex.encode(bytes); |
| 197 | return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; |
| 198 | }); |
| 199 | |
| 200 | type AuthFile = typeof AuthFile.Type; |
| 201 | /** Sends one upstream request with the account's token, as GET or as a JSON POST of `data`. */ |
| 202 | type HubCall = ( |
| 203 | url: string, |
| 204 | data?: unknown, |
| 205 | ) => Effect.Effect<string, UsageLimitSourceError | Schema.SchemaError>; |
| 206 | type HubReading = Omit<UsageLimitSourceAccount, "id" | "driver">; |
| 207 | interface HubProvider { |
| 208 | readonly driver: ProviderDriverKind; |
| 209 | readonly header: ( |
| 210 | account: AuthFile, |
| 211 | platform: NodeJS.Platform, |
| 212 | architecture: NodeJS.Architecture, |
| 213 | ) => Record<string, string>; |
| 214 | readonly read: (input: { |
| 215 | readonly account: AuthFile; |
| 216 | readonly call: HubCall; |
| 217 | readonly checkedAt: string; |
| 218 | }) => Effect.Effect<HubReading, UsageLimitSourceError | Schema.SchemaError, Digest.Digest>; |
| 219 | } |
| 220 | |
| 221 | const listedIdentity = (account: AuthFile) => ({ |
| 222 | ...(account.email ? { email: account.email } : {}), |
| 223 | ...(account.id_token?.chatgpt_account_id?.trim() |
| 224 | ? { workspaceId: account.id_token.chatgpt_account_id.trim() } |
| 225 | : {}), |
| 226 | }); |
| 227 | |
| 228 | const credits = Effect.fn("CliproxyApi.credits")(function* (call: HubCall) { |
| 229 | const response = yield* decodeCreditList(yield* call(CREDIT_URL)); |
| 230 | const now = DateTime.toEpochMillis(yield* DateTime.now); |
| 231 | return response.credits |
| 232 | .filter( |
| 233 | (credit) => |
| 234 | credit.reset_type === "codex_rate_limits" && |
| 235 | credit.status === "available" && |
| 236 | Date.parse(credit.expires_at) > now, |
| 237 | ) |
| 238 | .toSorted((a, b) => Date.parse(a.expires_at) - Date.parse(b.expires_at)); |
| 239 | }); |
| 240 | |
| 241 | const CODEX_HUB: HubProvider = { |
| 242 | driver: ProviderDriverKind.make("codex"), |
| 243 | header: (account) => ({ |
| 244 | Authorization: "Bearer $TOKEN$", |
| 245 | "Content-Type": "application/json", |
| 246 | "OpenAI-Beta": "codex-1", |
| 247 | Originator: "Codex Desktop", |
| 248 | ...(account.id_token?.chatgpt_account_id |
| 249 | ? { "Chatgpt-Account-Id": account.id_token.chatgpt_account_id } |
| 250 | : {}), |
| 251 | }), |
| 252 | read: Effect.fnUntraced(function* ({ account, call, checkedAt }) { |
| 253 | const usage = yield* decodeCodexUsage(yield* call(`${CODEX_BASE}/usage`)); |
| 254 | const toWindow = (window: typeof CodexWindow.Type | null | undefined) => |
| 255 | window |
| 256 | ? { |
| 257 | usedPercent: window.used_percent, |
| 258 | resetsAt: window.reset_at ?? null, |
| 259 | ...(window.limit_window_seconds === undefined |
| 260 | ? {} |
| 261 | : { windowDurationMins: window.limit_window_seconds / 60 }), |
| 262 | } |
| 263 | : null; |
| 264 | const available = yield* credits(call).pipe(Effect.orElseSucceed(() => undefined)); |
| 265 | const next = available?.[0]; |
| 266 | const planType = |
| 267 | usage.plan_type ?? account.id_token?.plan_type ?? account.id_token?.chatgpt_plan_type; |
| 268 | return { |
| 269 | ...listedIdentity(account), |
| 270 | plan: codexPlanLabel(planType), |
| 271 | usageLimits: { |
| 272 | ...codexRateLimitsToLimits({ |
| 273 | checkedAt, |
| 274 | snapshot: { |
| 275 | planType: planType ?? null, |
| 276 | primary: toWindow(usage.rate_limit?.primary_window), |
| 277 | secondary: toWindow(usage.rate_limit?.secondary_window), |
| 278 | }, |
| 279 | }), |
| 280 | ...(available |
| 281 | ? { |
| 282 | resetCredits: { |
| 283 | availableCount: available.length, |
| 284 | ...(next |
| 285 | ? { |
| 286 | nextCreditId: next.id, |
| 287 | nextExpiresAt: DateTime.formatIso(DateTime.makeUnsafe(next.expires_at)), |
| 288 | } |
| 289 | : {}), |
| 290 | }, |
| 291 | } |
| 292 | : {}), |
| 293 | }, |
| 294 | }; |
| 295 | }), |
| 296 | }; |
| 297 | |
| 298 | const GROK_CLI_VERSION = "1.0.77"; |
| 299 | const ANTIGRAVITY_SUMMARY_URL = |
| 300 | "https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary"; |
| 301 | |
| 302 | // Keyed by the auth-files `provider`; accounts of any other provider are skipped. |
| 303 | const HUB_PROVIDERS = new Map<string, HubProvider>([ |
| 304 | ["codex", CODEX_HUB], |
| 305 | [ |
| 306 | "claude", |
| 307 | { |
| 308 | driver: ProviderDriverKind.make("claudeAgent"), |
| 309 | header: () => ({ |
| 310 | Authorization: "Bearer $TOKEN$", |
| 311 | "anthropic-beta": "oauth-2025-04-20", |
| 312 | // The OAuth usage endpoint can return 429 without a Claude Code user agent. |
| 313 | "User-Agent": "claude-code/2.1.273", |
| 314 | }), |
| 315 | read: Effect.fnUntraced(function* ({ account, call, checkedAt }) { |
| 316 | const usage = yield* decodeClaudeUsage( |
| 317 | yield* call("https://api.anthropic.com/api/oauth/usage"), |
| 318 | ); |
| 319 | const model_scoped = (usage.limits ?? []).flatMap((limit) => |
| 320 | limit.kind === "weekly_scoped" && limit.scope?.model && typeof limit.percent === "number" |
| 321 | ? [ |
| 322 | { |
| 323 | display_name: limit.scope.model.display_name, |
| 324 | utilization: limit.percent, |
| 325 | resets_at: limit.resets_at ?? null, |
| 326 | }, |
| 327 | ] |
| 328 | : [], |
| 329 | ); |
| 330 | const rateLimits = { |
| 331 | five_hour: usage.five_hour ?? null, |
| 332 | seven_day: usage.seven_day ?? null, |
| 333 | model_scoped, |
| 334 | spend: usage.spend, |
| 335 | extra_usage: usage.extra_usage ?? null, |
| 336 | }; |
| 337 | return { |
| 338 | ...listedIdentity(account), |
| 339 | plan: "Claude Subscription", |
| 340 | usageLimits: claudeUsageResponseToLimits({ |
| 341 | checkedAt, |
| 342 | response: { rate_limits_available: true, rate_limits: rateLimits }, |
| 343 | }).limits, |
| 344 | }; |
| 345 | }), |
| 346 | }, |
| 347 | ], |
| 348 | [ |
| 349 | "xai", |
| 350 | { |
| 351 | driver: ProviderDriverKind.make("grok"), |
| 352 | header: () => ({ |
| 353 | Authorization: "Bearer $TOKEN$", |
| 354 | "x-xai-token-auth": "xai-grok-cli", |
| 355 | "x-grok-client-version": GROK_CLI_VERSION, |
| 356 | "User-Agent": `grok-cli/${GROK_CLI_VERSION}`, |
| 357 | }), |
| 358 | read: Effect.fnUntraced(function* ({ account, call, checkedAt }) { |
| 359 | const usage = yield* decodeGrokUsage(yield* call(GROK_USAGE_URL)); |
| 360 | return { |
| 361 | ...listedIdentity(account), |
| 362 | usageLimits: grokUsageResponseToLimits(usage, checkedAt), |
| 363 | }; |
| 364 | }), |
| 365 | }, |
| 366 | ], |
| 367 | [ |
| 368 | "antigravity", |
| 369 | { |
| 370 | driver: ProviderDriverKind.make("antigravity"), |
| 371 | header: (_account, platform, architecture) => ({ |
| 372 | Authorization: "Bearer $TOKEN$", |
| 373 | "Content-Type": "application/json", |
| 374 | "User-Agent": antigravityUserAgent(platform, architecture), |
| 375 | }), |
| 376 | read: Effect.fnUntraced(function* ({ account, call, checkedAt }) { |
| 377 | const project = account.project_id?.trim(); |
| 378 | if (!project) { |
| 379 | return yield* new UsageLimitSourceError({ detail: "No Antigravity project reported." }); |
| 380 | } |
| 381 | const requestedAt = DateTime.toEpochMillis(yield* DateTime.now); |
| 382 | const summary = yield* call(ANTIGRAVITY_SUMMARY_URL, { project }); |
| 383 | const windows = yield* antigravitySummaryWindows(yield* decodeJson(summary), requestedAt); |
| 384 | if (windows.length === 0) { |
| 385 | return yield* new UsageLimitSourceError({ detail: "No Antigravity quota reported." }); |
| 386 | } |
| 387 | // Local Antigravity reports no e-mail, so the Google account fingerprint is what pools the two. |
| 388 | const fingerprint = yield* call("https://www.googleapis.com/oauth2/v2/userinfo").pipe( |
| 389 | Effect.flatMap(decodeJson), |
| 390 | Effect.flatMap(antigravityAccountFingerprint), |
| 391 | Effect.option, |
| 392 | ); |
| 393 | const usageLimits = makeUsageLimits({ checkedAt, windows }); |
| 394 | return Option.match(fingerprint, { |
| 395 | onNone: () => ({ ...listedIdentity(account), usageLimits }), |
| 396 | onSome: (credentialFingerprint) => ({ |
| 397 | usageLimits: { ...usageLimits, credentialFingerprint }, |
| 398 | }), |
| 399 | }); |
| 400 | }), |
| 401 | }, |
| 402 | ], |
| 403 | ]); |
| 404 | |
| 405 | export const makeCliproxyApi = Effect.gen(function* () { |
| 406 | const client = yield* HttpClient.HttpClient; |
| 407 | const digest = yield* Digest.Digest; |
| 408 | const platform = yield* HostProcess.Platform; |
| 409 | const architecture = yield* HostProcess.Architecture; |
| 410 | |
| 411 | const management = Effect.fn("CliproxyApi.management")(function* ( |
| 412 | hub: CliproxyHub, |
| 413 | path: string, |
| 414 | body?: unknown, |
| 415 | ) { |
| 416 | const url = yield* Effect.try({ |
| 417 | try: () => { |
| 418 | const base = new URL(hub.url); |
| 419 | // Users often paste the hub's web panel URL, which ends in management.html. |
| 420 | base.pathname = base.pathname.replace(/(\/management\.html)?\/?$/i, "/"); |
| 421 | return new URL(`v0/management/${path}`, base).toString(); |
| 422 | }, |
| 423 | catch: () => new UsageLimitSourceError({ detail: "The hub URL is not valid." }), |
| 424 | }); |
| 425 | const request = ( |
| 426 | body === undefined ? HttpClientRequest.get(url) : HttpClientRequest.post(url) |
| 427 | ).pipe(HttpClientRequest.setHeader("Authorization", `Bearer ${hub.managementKey}`)); |
| 428 | const response = yield* client |
| 429 | .execute(body === undefined ? request : request.pipe(HttpClientRequest.bodyJsonUnsafe(body))) |
| 430 | .pipe( |
| 431 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 432 | Effect.flatMap((response) => response.json), |
| 433 | Effect.timeout("15 seconds"), |
| 434 | Effect.mapError( |
| 435 | (error) => new UsageLimitSourceError({ detail: describeManagementFailure(error) }), |
| 436 | ), |
| 437 | ); |
| 438 | return response; |
| 439 | }); |
| 440 | |
| 441 | const authFiles = Effect.fn("CliproxyApi.authFiles")(function* (hub: CliproxyHub) { |
| 442 | const response = yield* management(hub, "auth-files"); |
| 443 | return (yield* decodeAuthFiles(response)).files; |
| 444 | }); |
| 445 | |
| 446 | // Older hubs lack this endpoint; the account then counts for every model of its driver. |
| 447 | const routedModels = Effect.fn("CliproxyApi.routedModels")(function* ( |
| 448 | hub: CliproxyHub, |
| 449 | account: AuthFile, |
| 450 | ) { |
| 451 | const response = yield* management( |
| 452 | hub, |
| 453 | `auth-files/models?name=${encodeURIComponent(account.id)}`, |
| 454 | ); |
| 455 | return (yield* decodeAuthFileModels(response)).models.flatMap(({ id }) => |
| 456 | id.trim() ? [id.trim()] : [], |
| 457 | ); |
| 458 | }); |
| 459 | |
| 460 | const apiCall = Effect.fn("CliproxyApi.apiCall")(function* ( |
| 461 | hub: CliproxyHub, |
| 462 | account: AuthFile, |
| 463 | header: Record<string, string>, |
| 464 | url: string, |
| 465 | data?: unknown, |
| 466 | ) { |
| 467 | const raw = yield* management(hub, "api-call", { |
| 468 | auth_index: account.auth_index, |
| 469 | method: data === undefined ? "GET" : "POST", |
| 470 | url, |
| 471 | header, |
| 472 | ...(data === undefined ? {} : { data: yield* encodeJson(data) }), |
| 473 | }); |
| 474 | const response = yield* decodeApiResponse(raw); |
| 475 | if (response.status_code < 200 || response.status_code >= 300) { |
| 476 | return yield* new UsageLimitSourceError({ |
| 477 | detail: `The provider refused the hub request (HTTP ${response.status_code}).`, |
| 478 | }); |
| 479 | } |
| 480 | return response.body; |
| 481 | }); |
| 482 | |
| 483 | const readAccount = Effect.fn("CliproxyApi.readAccount")(function* ( |
| 484 | hub: CliproxyHub, |
| 485 | account: AuthFile, |
| 486 | provider: HubProvider, |
| 487 | ) { |
| 488 | const checkedAt = DateTime.formatIso(yield* DateTime.now); |
| 489 | const call: HubCall = (url, data) => |
| 490 | apiCall(hub, account, provider.header(account, platform, architecture), url, data); |
| 491 | const reading = yield* provider.read({ account, call, checkedAt }).pipe( |
| 492 | Effect.provideService(Digest.Digest, digest), |
| 493 | Effect.orElseSucceed(() => ({ |
| 494 | ...listedIdentity(account), |
| 495 | usageLimits: makeUnavailableUsageLimits({ |
| 496 | checkedAt, |
| 497 | reason: "probeFailed", |
| 498 | message: "The hub could not read this account's usage.", |
| 499 | }), |
| 500 | })), |
| 501 | ); |
| 502 | return { id: account.id, driver: provider.driver, ...reading }; |
| 503 | }); |
| 504 | |
| 505 | const readAccounts = Effect.fn("CliproxyApi.readAccounts")(function* ( |
| 506 | hub: CliproxyHub, |
| 507 | ): Effect.fn.Return<ReadonlyArray<UsageLimitSourceAccount>, UsageLimitSourceError> { |
| 508 | const accounts = yield* authFiles(hub).pipe( |
| 509 | Effect.mapError((error) => |
| 510 | isUsageLimitSourceError(error) |
| 511 | ? error |
| 512 | : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), |
| 513 | ), |
| 514 | ); |
| 515 | const readable = accounts.flatMap((account) => { |
| 516 | const provider = HUB_PROVIDERS.get(account.provider); |
| 517 | return provider && !account.disabled ? [{ account, provider }] : []; |
| 518 | }); |
| 519 | // Model lists are optional, so their lookups run apart from the quota reads and stop at one |
| 520 | // short deadline; a slow lookup must not push a refresh past its budget. |
| 521 | const [readings, models] = yield* Effect.all( |
| 522 | [ |
| 523 | Effect.forEach(readable, ({ account, provider }) => readAccount(hub, account, provider), { |
| 524 | concurrency: 4, |
| 525 | }), |
| 526 | Effect.forEach( |
| 527 | readable, |
| 528 | ({ account }) => |
| 529 | routedModels(hub, account).pipe( |
| 530 | Effect.timeoutOption("2 seconds"), |
| 531 | Effect.orElseSucceed(() => Option.none()), |
| 532 | ), |
| 533 | { concurrency: "unbounded" }, |
| 534 | ), |
| 535 | ], |
| 536 | { concurrency: 2 }, |
| 537 | ); |
| 538 | return readings.map((reading, index) => { |
| 539 | const listed = models[index]; |
| 540 | return listed && Option.isSome(listed) ? { ...reading, models: listed.value } : reading; |
| 541 | }); |
| 542 | }); |
| 543 | |
| 544 | const consume = Effect.fn("CliproxyApi.consume")(function* ( |
| 545 | hub: CliproxyHub, |
| 546 | accountId: string, |
| 547 | creditId: string, |
| 548 | ): Effect.fn.Return<ProviderConsumeResetCreditResult, UsageLimitSourceError> { |
| 549 | const operation = Effect.gen(function* () { |
| 550 | const account = (yield* authFiles(hub)).find((account) => account.id === accountId); |
| 551 | if (!account || account.disabled || account.provider !== "codex") { |
| 552 | return yield* new UsageLimitSourceError({ |
| 553 | detail: "The Codex hub account is missing or disabled.", |
| 554 | }); |
| 555 | } |
| 556 | const body = yield* apiCall( |
| 557 | hub, |
| 558 | account, |
| 559 | CODEX_HUB.header(account, platform, architecture), |
| 560 | `${CREDIT_URL}/consume`, |
| 561 | { |
| 562 | redeem_request_id: yield* creditRedeemRequestId( |
| 563 | account.id_token?.chatgpt_account_id ?? account.id, |
| 564 | creditId, |
| 565 | ).pipe(Effect.provideService(Digest.Digest, digest)), |
| 566 | credit_id: creditId, |
| 567 | }, |
| 568 | ); |
| 569 | const response = yield* decodeConsumeResponse(body); |
| 570 | const outcome = ( |
| 571 | { |
| 572 | reset: "reset", |
| 573 | nothing_to_reset: "nothingToReset", |
| 574 | no_credit: "noCredit", |
| 575 | already_redeemed: "alreadyRedeemed", |
| 576 | } as const |
| 577 | )[response.code]; |
| 578 | if (outcome !== "reset" && outcome !== "alreadyRedeemed") return { outcome }; |
| 579 | const cleared = yield* management(hub, "reset-quota", { |
| 580 | auth_index: account.auth_index, |
| 581 | }).pipe(Effect.result); |
| 582 | return { |
| 583 | outcome, |
| 584 | ...(cleared._tag === "Failure" |
| 585 | ? { |
| 586 | warning: |
| 587 | "Credit redeemed, but the hub cooldown could not be cleared. Routing may resume after its cooldown expires.", |
| 588 | } |
| 589 | : {}), |
| 590 | } as const; |
| 591 | }); |
| 592 | return yield* operation.pipe( |
| 593 | Effect.mapError((error) => |
| 594 | isUsageLimitSourceError(error) |
| 595 | ? error |
| 596 | : new UsageLimitSourceError({ |
| 597 | detail: "The hub returned an unexpected reset-credit response.", |
| 598 | }), |
| 599 | ), |
| 600 | ); |
| 601 | }); |
| 602 | return { readAccounts, consume }; |
| 603 | }); |