/** * Claude usage on the shared subscription core (design 5.3, Claude track * C2a): the adapter's usage members (`fetchUsage`, `decodeQuota`) and the * projection of one core connection's quota row onto the legacy * `ClaudeSubscriptionUsage` response. * * - `fetchSubscriptionCoreClaudeUsage` is the legacy `/api/oauth/usage` read * (`requestClaudeUsage`) through the core's request custody: a ten-second * deadline, a bounded body and `redirect: "error"`. A credential without * the `user:profile` OAuth scope (every setup token) is never sent: it is * `scope_required` with no network call. A 401 is `reconnect`, the 403 * naming the `user:profile` scope requirement is `scope_required`, and any * other failure is `unavailable` (never an implied zero quota). * - `decodeSubscriptionCoreClaudeUsage` decodes an `available` reading * through `decodeSubscriptionCoreClaudeQuota`, with every Claude model the * caller's catalog lists (`modelIds`), so a model-family window becomes a * cooldown of each listed model of the family. * - `projectSubscriptionCoreClaudeUsage` maps a stored core quota back to the * legacy window shape, with the connection's row `version` as the * credential version (the account listing's `version`). `scope_required` * and `reconnect` are response values derived from the connection, never * stored: `reconnect` while the connection needs a new sign-in, * `scope_required` when the adapter-owned `provider_state.oauthScopes` * (written by the connect writers; `[]` for a setup token, absent when * unknown) lacks `user:profile`. * * Usage reads present Claude Code's CLI surface (its account-API user * agent): Claude reports limit reset grants only to that surface. The * read asks for the reset status (`cedar_ember=1`); while that status says * the account is at a limit, a second read asks for the at-the-limit * program (`at_wall=1`), as Claude Code does. Both skip spend reporting. */ import { type ClaudeUsageObservation } from "@opengeni/config"; import { type ClaudeSubscriptionUsage } from "@opengeni/contracts"; import { type SubscriptionCoreConnectionRead, type SubscriptionQuota, type SubscriptionQuotaBlock } from "@opengeni/subscriptions"; import { type SubscriptionCoreClaudeCredential } from "./subscription-core-claude-adapter.js"; /** The Claude usage endpoint (the legacy `requestClaudeUsage` URL). */ export declare const SUBSCRIPTION_CORE_CLAUDE_USAGE_URL = "https://api.anthropic.com/api/oauth/usage"; /** The usage read with the limit reset status (Claude Code's ordinary reset check). */ export declare const SUBSCRIPTION_CORE_CLAUDE_RESET_STATUS_URL = "https://api.anthropic.com/api/oauth/usage?cedar_ember=1&skip_spend=1"; /** The usage read at a limit, which also reports the at-the-limit program. */ export declare const SUBSCRIPTION_CORE_CLAUDE_AT_LIMIT_URL = "https://api.anthropic.com/api/oauth/usage?at_wall=1&skip_spend=1"; /** The OAuth scope the usage endpoint requires. */ export declare const SUBSCRIPTION_CORE_CLAUDE_USAGE_SCOPE = "user:profile"; /** The headers of an account-API read: the bearer and Claude Code's CLI surface. */ export declare function subscriptionCoreClaudeAccountHeaders(token: string): Record; /** One usage read: the legacy refresh outcome and, when available, its observation. */ export type SubscriptionCoreClaudeUsageReading = { status: "available" | "unavailable" | "scope_required" | "reconnect"; observation: ClaudeUsageObservation | null; /** When the read was requested (a refusal stored after it stands). */ checkedAt: string; /** * Why an `unavailable` read failed, for the operator log: Claude's HTTP * status, the kind of transport error (never its message), or a response * that carried no reading. */ failure?: SubscriptionCoreClaudeUsageFailure; }; /** Why one usage read failed. Holds no token, header or response text. */ export type SubscriptionCoreClaudeUsageFailure = { kind: "http_status"; status: number; } | { kind: "fetch_error"; error: string; } | { kind: "no_reading"; }; /** The kind of a thrown transport error: its name and system code, never its message. */ export declare function subscriptionCoreClaudeUsageErrorKind(error: unknown): string; /** The adapter's `fetchUsage`: one usage read with the connection's current bearer. */ export declare function fetchSubscriptionCoreClaudeUsage(read: SubscriptionCoreConnectionRead): Promise; /** The upstream Claude model ids among product model ids (other providers' ids are dropped). */ export declare function subscriptionCoreClaudeUpstreamModelIds(productModelIds: readonly string[]): string[]; /** * The adapter's `decodeQuota`: an available reading in the shared quota * model, observed when the core received it (`input.observedAt`), else null. */ export declare function decodeSubscriptionCoreClaudeUsage(input: { response: unknown; observedAt: number; refreshGeneration: number; modelIds?: readonly string[]; }): SubscriptionQuota | null; /** * The adapter's `usageReadConfirmsAvailability`: an available reading in * which each account window (five hours, seven days) is either reported with * a percentage below the limit or answered with an explicit `null` (the plan * has no such limit now: a verified Ultra plan has no five-hour window), and * at least one is reported below the limit. A window that is omitted, or * reported without a percentage, says nothing about the limit that refused * the connection, so the reading ends nothing. An explicit `null` only lets * the other window's reading count: it never ends a block that claims the * null window (`subscriptionCoreClaudeUsageEndsClaimedBlock`). */ export declare function subscriptionCoreClaudeUsageConfirmsAvailability(response: unknown): boolean; /** * The adapter's `usageReadEndsClaimedBlock`: with a reading that confirms * availability, a stored block (a model cooldown, or the quota exhaustion * deadline) has ended when its claim names only plan windows this build * recognizes, was observed before the reading was requested (a refusal * that arrived while it was in flight stands), and the reading reports * every claimed window with a percentage below the limit. Everything else * ends nothing (fail closed): a block without a claim (no or an * unrecognized representative claim, or a refusal without usage headers), * a claimed window the reading omits or answers as `null` (no such limit * now never clears a limit that refused), and a reading that does not * confirm availability. A plan without a family limit still recovers from * an account-window claim: only the claimed windows are required. */ export declare function subscriptionCoreClaudeUsageEndsClaimedBlock(response: unknown): (block: SubscriptionQuotaBlock) => boolean; /** The fields of a core connection row the usage projection reads. */ export type SubscriptionCoreClaudeUsageRow = { status: string; provider_state: Record | null; quota: unknown; quota_revision: number | string | null; quota_observed_refresh_generation: number | string | null; quota_updated_at: Date | string | null; /** * The connection's row version (the account listing's `version`): the * shared connection reads select it; a personal row gets it from the * owner-only version reader. Required: the projection throws without it. */ version?: number | string; }; /** A live refresh outcome to report instead of the one derived from the row. */ export type SubscriptionCoreClaudeUsageRefresh = { status: ClaudeSubscriptionUsage["refreshStatus"]; checkedAt: string | null; }; /** * Whether the connection's credential lacks the usage scope, from the * adapter-owned `provider_state.oauthScopes` (absent or malformed: unknown, * so not reported). */ export declare function subscriptionCoreClaudeUsageScopeMissing(providerState: Record | null): boolean; /** A connection row without a usable row version (never reported as 0 or null). */ export declare class SubscriptionCoreClaudeUsageVersionMissing extends Error { constructor(); } /** The row version a usage response reports; throws when the row has none. */ export declare function subscriptionCoreClaudeUsageVersion(row: { version?: number | string; }): number; /** * The stored provider details as usage response fields. Each part is * checked against its contract and dropped when it does not match (a * newer build may have stored a shape this one does not know). */ export declare function subscriptionCoreClaudeUsageDetails(quota: SubscriptionQuota | null): Pick; /** * One core connection's stored quota as the legacy usage response. * * - `credentialVersion` is the connection's row version (the account * listing's `version`; it moves on reconnect and rename, never on a token * refresh). A row without one throws: a connected account never reports 0 * or null. * - Windows keep the legacy ids, percentages and ISO resets; every stored * window stays visible whatever credential generation observed it (as * legacy kept them across token refreshes). Statuses: see `windowStatus`. * The shared quota keeps no per-window time or source, so each window * carries the newest observation's. * - The refresh status is `reconnect` while the connection needs a new * sign-in; else `refresh` when given (a live read decides by the * credential it decoded); else `scope_required` when * `provider_state.oauthScopes` lacks the usage scope, `available` when the * newest stored observation is a usage-endpoint reading and `not_checked` * otherwise. * - `options.now` (epoch milliseconds, default `Date.now()`) is the instant * window statuses are judged at; a refresh passes its own clock. */ export declare function projectSubscriptionCoreClaudeUsage(row: SubscriptionCoreClaudeUsageRow, refresh?: SubscriptionCoreClaudeUsageRefresh, options?: { now?: number; }): ClaudeSubscriptionUsage;