The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { SDKControlGetUsageResponse, SDKRateLimitInfo } from "@anthropic-ai/claude-agent-sdk"; |
| 2 | import type { |
| 3 | ProviderUsageLimitsUpdate, |
| 4 | ServerProviderUsageLimits, |
| 5 | ServerProviderUsageWindow, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import * as DateTime from "effect/DateTime"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import * as Ref from "effect/Ref"; |
| 11 | import * as Schema from "effect/Schema"; |
| 12 | |
| 13 | import { |
| 14 | clampPercent, |
| 15 | makeUnavailableUsageLimits, |
| 16 | makeUsageLimits, |
| 17 | } from "@t3tools/provider-core/server/usageLimits"; |
| 18 | |
| 19 | const SESSION_MINS = 5 * 60; |
| 20 | const WEEK_MINS = 7 * 24 * 60; |
| 21 | |
| 22 | const WINDOWS: Readonly< |
| 23 | Record<string, Pick<ServerProviderUsageWindow, "kind" | "label" | "windowDurationMins">> |
| 24 | > = { |
| 25 | five_hour: { kind: "session", label: "Session", windowDurationMins: SESSION_MINS }, |
| 26 | seven_day: { kind: "weekly", label: "Weekly", windowDurationMins: WEEK_MINS }, |
| 27 | }; |
| 28 | |
| 29 | /** Reuse probed display names for streamed bucket types so mid-turn updates target the same row. */ |
| 30 | const OVERAGE_INCLUDED_EVENT_TYPE = "seven_day_overage_included"; |
| 31 | |
| 32 | // The SDK does not yet type the spend budget returned by `get_usage`. |
| 33 | const SpendMoney = Schema.Struct({ |
| 34 | amount_minor: Schema.Natural, |
| 35 | currency: Schema.NonEmptyString, |
| 36 | exponent: Schema.Natural, |
| 37 | }); |
| 38 | const decodeSpendBudget = Schema.decodeUnknownOption( |
| 39 | Schema.Struct({ enabled: Schema.Boolean, used: SpendMoney, limit: SpendMoney }), |
| 40 | ); |
| 41 | |
| 42 | export interface ClaudeScopedLimitNames { |
| 43 | readonly overageIncluded: string | undefined; |
| 44 | } |
| 45 | |
| 46 | export const makeClaudeScopedLimitNames = Ref.make<ClaudeScopedLimitNames>({ |
| 47 | overageIncluded: undefined, |
| 48 | }); |
| 49 | |
| 50 | const SCOPED_WINDOW_PREFIX = "seven_day_"; |
| 51 | const scopeToken = (name: string) => name.toLowerCase().replace(/[^a-z0-9]+/g, "_"); |
| 52 | |
| 53 | function scopedWindowId(displayName: string): string { |
| 54 | return `${SCOPED_WINDOW_PREFIX}${scopeToken(displayName)}`; |
| 55 | } |
| 56 | |
| 57 | /** A model-scoped weekly allowance blocks only its own model; an unknown model is not blocked. */ |
| 58 | export function windowLimitsModel( |
| 59 | window: Pick<ServerProviderUsageWindow, "id">, |
| 60 | model: string | null, |
| 61 | ): boolean { |
| 62 | if (!window.id.startsWith(SCOPED_WINDOW_PREFIX)) return true; |
| 63 | return model !== null && scopeToken(model).includes(window.id.slice(SCOPED_WINDOW_PREFIX.length)); |
| 64 | } |
| 65 | |
| 66 | function scopedWindow( |
| 67 | displayName: string, |
| 68 | usedPercent: number, |
| 69 | resetsAt: string | undefined, |
| 70 | ): ServerProviderUsageWindow { |
| 71 | return { |
| 72 | id: scopedWindowId(displayName), |
| 73 | kind: "weekly", |
| 74 | label: `Weekly · ${displayName}`, |
| 75 | windowDurationMins: WEEK_MINS, |
| 76 | usedPercent: clampPercent(usedPercent), |
| 77 | ...(resetsAt ? { resetsAt } : {}), |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | function percentFromAmounts(used: number | null | undefined, limit: number | null | undefined) { |
| 82 | if ( |
| 83 | typeof used !== "number" || |
| 84 | typeof limit !== "number" || |
| 85 | !Number.isFinite(used) || |
| 86 | !Number.isFinite(limit) || |
| 87 | used < 0 || |
| 88 | limit <= 0 |
| 89 | ) { |
| 90 | return undefined; |
| 91 | } |
| 92 | const percent = (used / limit) * 100; |
| 93 | return Number.isFinite(percent) ? clampPercent(percent) : undefined; |
| 94 | } |
| 95 | |
| 96 | function readSpendPercent(rateLimits: NonNullable<SDKControlGetUsageResponse["rate_limits"]>) { |
| 97 | const spend = Option.getOrUndefined( |
| 98 | decodeSpendBudget((rateLimits as { readonly spend?: unknown }).spend), |
| 99 | ); |
| 100 | // Minor-unit ratios require matching currency and scale; spend.percent is rounded. |
| 101 | if ( |
| 102 | spend?.enabled && |
| 103 | spend.used.currency.trim().length > 0 && |
| 104 | spend.used.currency.trim() === spend.limit.currency.trim() && |
| 105 | spend.used.exponent === spend.limit.exponent |
| 106 | ) { |
| 107 | const percent = percentFromAmounts(spend.used.amount_minor, spend.limit.amount_minor); |
| 108 | if (percent !== undefined) return percent; |
| 109 | } |
| 110 | |
| 111 | const extra = rateLimits.extra_usage; |
| 112 | if (extra?.is_enabled !== true) return undefined; |
| 113 | const percent = percentFromAmounts(extra.used_credits, extra.monthly_limit) ?? extra.utilization; |
| 114 | return typeof percent === "number" && Number.isFinite(percent) && percent >= 0 |
| 115 | ? clampPercent(percent) |
| 116 | : undefined; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * `model_scoped` shipped in the CLI after the SDK typings we pin, so it is |
| 121 | * read structurally until the `.d.ts` catches up. |
| 122 | */ |
| 123 | interface ModelScopedWindow { |
| 124 | readonly display_name: string; |
| 125 | readonly utilization: number | null; |
| 126 | readonly resets_at: string | null; |
| 127 | } |
| 128 | |
| 129 | function readModelScoped(rateLimits: object): ReadonlyArray<ModelScopedWindow> { |
| 130 | const raw = (rateLimits as { readonly model_scoped?: unknown }).model_scoped; |
| 131 | if (!Array.isArray(raw)) return []; |
| 132 | return raw.filter( |
| 133 | (entry): entry is ModelScopedWindow => |
| 134 | typeof entry === "object" && |
| 135 | entry !== null && |
| 136 | typeof (entry as ModelScopedWindow).display_name === "string", |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | function isoFromEpochSeconds(value: number | undefined): string | undefined { |
| 141 | if (value === undefined || !Number.isFinite(value) || value <= 0) return undefined; |
| 142 | const dt = DateTime.make(value * 1000); |
| 143 | return Option.isSome(dt) ? DateTime.formatIso(dt.value) : undefined; |
| 144 | } |
| 145 | |
| 146 | function isoFromString(value: string | null | undefined): string | undefined { |
| 147 | if (!value) return undefined; |
| 148 | const dt = DateTime.make(value); |
| 149 | return Option.isSome(dt) ? DateTime.formatIso(dt.value) : undefined; |
| 150 | } |
| 151 | |
| 152 | function makeWindow( |
| 153 | id: keyof typeof WINDOWS & string, |
| 154 | usedPercent: number, |
| 155 | resetsAt: string | undefined, |
| 156 | ): ServerProviderUsageWindow { |
| 157 | const window = WINDOWS[id]!; |
| 158 | return { |
| 159 | id, |
| 160 | ...window, |
| 161 | usedPercent: clampPercent(usedPercent), |
| 162 | ...(resetsAt ? { resetsAt } : {}), |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | /** Stream utilization is 0-1. Drop unnamed overage buckets until probed to avoid unreconcilable rows. */ |
| 167 | export function claudeRateLimitEventToUpdate( |
| 168 | info: SDKRateLimitInfo, |
| 169 | names: ClaudeScopedLimitNames, |
| 170 | ): ProviderUsageLimitsUpdate | undefined { |
| 171 | const type: string | undefined = info.rateLimitType; |
| 172 | if (!type || typeof info.utilization !== "number") { |
| 173 | return undefined; |
| 174 | } |
| 175 | const usedPercent = info.utilization * 100; |
| 176 | const resetsAt = isoFromEpochSeconds(info.resetsAt); |
| 177 | if (type in WINDOWS) { |
| 178 | return { windows: [makeWindow(type, usedPercent, resetsAt)] }; |
| 179 | } |
| 180 | if (type === OVERAGE_INCLUDED_EVENT_TYPE && names.overageIncluded) { |
| 181 | return { windows: [scopedWindow(names.overageIncluded, usedPercent, resetsAt)] }; |
| 182 | } |
| 183 | return undefined; |
| 184 | } |
| 185 | |
| 186 | /** get_usage percentages are already 0-100. */ |
| 187 | export function claudeUsageResponseToLimits(input: { |
| 188 | readonly response: Pick<SDKControlGetUsageResponse, "rate_limits_available" | "rate_limits">; |
| 189 | readonly checkedAt: string; |
| 190 | }): { readonly limits: ServerProviderUsageLimits; readonly names: ClaudeScopedLimitNames } { |
| 191 | const { response, checkedAt } = input; |
| 192 | if (!response.rate_limits_available || !response.rate_limits) { |
| 193 | return { |
| 194 | limits: makeUnavailableUsageLimits({ |
| 195 | checkedAt, |
| 196 | reason: response.rate_limits_available ? "probeFailed" : "unsupported", |
| 197 | }), |
| 198 | names: { overageIncluded: undefined }, |
| 199 | }; |
| 200 | } |
| 201 | const windows: ServerProviderUsageWindow[] = []; |
| 202 | for (const id of Object.keys(WINDOWS)) { |
| 203 | const window = response.rate_limits[id as "five_hour" | "seven_day"]; |
| 204 | if (!window || typeof window.utilization !== "number") continue; |
| 205 | windows.push(makeWindow(id, window.utilization, isoFromString(window.resets_at))); |
| 206 | } |
| 207 | // The CLI's overage allowlist currently has one model; events refer to its first entry. |
| 208 | let overageIncluded: string | undefined; |
| 209 | for (const entry of readModelScoped(response.rate_limits)) { |
| 210 | if (typeof entry.utilization !== "number") continue; |
| 211 | windows.push( |
| 212 | scopedWindow(entry.display_name, entry.utilization, isoFromString(entry.resets_at)), |
| 213 | ); |
| 214 | overageIncluded ??= entry.display_name; |
| 215 | } |
| 216 | const spendPercent = readSpendPercent(response.rate_limits); |
| 217 | const extra = response.rate_limits.extra_usage; |
| 218 | if (spendPercent !== undefined) { |
| 219 | windows.push({ |
| 220 | id: "monthly_spend", |
| 221 | kind: "monthly", |
| 222 | label: "Monthly spend", |
| 223 | usedPercent: spendPercent, |
| 224 | }); |
| 225 | } |
| 226 | return { |
| 227 | limits: { |
| 228 | ...makeUsageLimits({ checkedAt, windows }), |
| 229 | ...(extra?.is_enabled && extra.monthly_limit === 0 ? { zeroSpendCap: true } : {}), |
| 230 | }, |
| 231 | names: { overageIncluded }, |
| 232 | }; |
| 233 | } |
| 234 | |
| 235 | export const recordClaudeUsageResponse = ( |
| 236 | namesRef: Ref.Ref<ClaudeScopedLimitNames>, |
| 237 | input: Parameters<typeof claudeUsageResponseToLimits>[0], |
| 238 | ): Effect.Effect<ServerProviderUsageLimits> => { |
| 239 | const { limits, names } = claudeUsageResponseToLimits(input); |
| 240 | return Ref.set(namesRef, names).pipe(Effect.as(limits)); |
| 241 | }; |