/** * Continuous OAuth usage fetching from Anthropic's private OAuth endpoint. * * Anthropic exposes `GET https://api.anthropic.com/api/oauth/usage` for OAuth * (Claude Max) subscribers. Unlike the SDK's `rate_limit_event` (which only * populates `utilization` near `allowed_warning` / `rejected`), this endpoint * always returns continuous percentage values for every active rate-limit * window — exactly what claude.ai's own UI uses. * * Headers required: * Authorization: Bearer * anthropic-beta: oauth-2025-04-20 * * We reuse `tokenRefresh.ts`'s cross-platform credential store (macOS Keychain * or `~/.claude/.credentials.json`) to read the access token, and trigger a * background refresh on 401. * * Per-profile caching: each profile has its own 30s TTL cache so multi-account * setups can be queried independently without cross-contamination. Concurrent * callers for the same profile share a single in-flight request. */ import { type CredentialStore } from "./tokenRefresh"; export interface OAuthUsageWindow { type: string; utilization: number | null; resetsAt: number | null; } export interface OAuthExtraUsageInfo { isEnabled: boolean; monthlyLimit: number; usedCredits: number; utilization: number | null; currency: string; } export interface OAuthUsageSnapshot { windows: OAuthUsageWindow[]; extraUsage: OAuthExtraUsageInfo | null; fetchedAt: number; } /** Minimal fetch shape used by callAnthropic. Avoids `typeof fetch`'s * `preconnect` property, which makes test casts unwieldy. */ type FetchLike = (input: string, init?: RequestInit) => Promise; export interface FetchOAuthUsageOpts { ttlMs?: number; force?: boolean; store?: CredentialStore; profileId?: string | null; claudeConfigDir?: string; fetchImpl?: FetchLike; } /** Test-only setter. Pass `null` to clear. */ export declare function __setFetchOAuthUsageOverride(fn: ((opts?: FetchOAuthUsageOpts) => Promise) | null): void; /** * Fetch latest OAuth usage for a specific profile (or the default OAuth * account if none specified). Returns null if no OAuth token is available * or the upstream call fails (after one refresh attempt). * * Per-profile in-process cache (30s TTL by default) prevents hammering * Anthropic's endpoint when many clients poll concurrently. Concurrent * callers for the same profile share a single in-flight request. * * @param ttlMs Override the cache TTL (default 30s). * @param force Bypass the cache and fetch fresh. * @param store Override the credential store (for testing). * @param profileId Logical profile identifier used as the cache key. * Pass null/undefined for the default OAuth account. * @param claudeConfigDir When provided, reads credentials from this dir's * keychain entry (macOS) or `.credentials.json` * (Linux) instead of the platform default. * @param fetchImpl Override the fetch implementation (for testing). * Defaults to globalThis.fetch. */ export declare function fetchOAuthUsage(opts?: FetchOAuthUsageOpts): Promise; /** Test-only / shutdown helper — clears all cached snapshots and pending fetches. */ export declare function resetOAuthUsageCache(): void; export {}; //# sourceMappingURL=oauthUsage.d.ts.map