The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL, |
| 3 | UsageLimitSourceError, |
| 4 | type UsageLimitSourceConsumeResetCreditInput, |
| 5 | type ProviderConsumeResetCreditResult, |
| 6 | type ProviderDriverKind, |
| 7 | type ServerSettings, |
| 8 | UsageLimitSourceConfig, |
| 9 | settingsSecretLiteral, |
| 10 | type UsageLimitSourceId, |
| 11 | type UsageLimitSourceSnapshot, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; |
| 14 | import { antigravityWindowLimitsModel, hubRoutes } from "@t3tools/shared/usageLimits"; |
| 15 | import * as Context from "effect/Context"; |
| 16 | import * as DateTime from "effect/DateTime"; |
| 17 | import * as Duration from "effect/Duration"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import * as Equal from "effect/Equal"; |
| 20 | import * as Layer from "effect/Layer"; |
| 21 | import * as PubSub from "effect/PubSub"; |
| 22 | import * as Ref from "effect/Ref"; |
| 23 | import * as Semaphore from "effect/Semaphore"; |
| 24 | import * as Stream from "effect/Stream"; |
| 25 | |
| 26 | import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; |
| 27 | import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; |
| 28 | import * as SecretResolverLayer from "../secrets/SecretResolver.ts"; |
| 29 | import { resolveSettingsSecrets } from "../secrets/settingsSecretValue.ts"; |
| 30 | import * as Settings from "../serverSettings.ts"; |
| 31 | import { windowLimitsModel } from "../provider/claudeUsageLimits.ts"; |
| 32 | import { makeCliproxyApi } from "./cliproxyApi.ts"; |
| 33 | |
| 34 | export class UsageLimitSources extends Context.Service< |
| 35 | UsageLimitSources, |
| 36 | { |
| 37 | readonly current: Effect.Effect<ReadonlyArray<UsageLimitSourceSnapshot>>; |
| 38 | readonly streamChanges: Stream.Stream<ReadonlyArray<UsageLimitSourceSnapshot>>; |
| 39 | readonly refresh: Effect.Effect<void>; |
| 40 | readonly consumeResetCredit: ( |
| 41 | input: UsageLimitSourceConsumeResetCreditInput, |
| 42 | ) => Effect.Effect<ProviderConsumeResetCreditResult, UsageLimitSourceError>; |
| 43 | } |
| 44 | >()("t3/usage/UsageLimitSources") {} |
| 45 | |
| 46 | function sourceLabel(id: string, config: UsageLimitSourceConfig): string { |
| 47 | if (config.label) return config.label; |
| 48 | const fallback = id.trim() || "Usage limit source"; |
| 49 | try { |
| 50 | return new URL(config.url).host || fallback; |
| 51 | } catch { |
| 52 | return fallback; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * A refused read of one account keeps its last good reading, as provider probes keep their bars |
| 58 | * (see ServerProviderUsageLimits); a reading with none to keep stays listed as unreadable. |
| 59 | */ |
| 60 | function keepLastGoodReadings( |
| 61 | previous: ReadonlyArray<UsageLimitSourceSnapshot>, |
| 62 | next: ReadonlyArray<UsageLimitSourceSnapshot>, |
| 63 | ): ReadonlyArray<UsageLimitSourceSnapshot> { |
| 64 | return next.map((source) => { |
| 65 | // Auth file names can be reused, so only the same hub and login may lend a reading. |
| 66 | const before = previous.find( |
| 67 | (candidate) => candidate.id === source.id && candidate.endpoint === source.endpoint, |
| 68 | ); |
| 69 | if (!before) return source; |
| 70 | return { |
| 71 | ...source, |
| 72 | accounts: source.accounts.map((account) => { |
| 73 | if (account.usageLimits.unavailable?.reason !== "probeFailed") return account; |
| 74 | // Without a listed login, a reused auth file name cannot be told apart from the old one. |
| 75 | if (account.email === undefined && account.workspaceId === undefined) return account; |
| 76 | const last = before.accounts.find( |
| 77 | (candidate) => |
| 78 | candidate.id === account.id && |
| 79 | candidate.driver === account.driver && |
| 80 | // Fingerprinted Antigravity readings omit the listed identity, so they are never lent. |
| 81 | candidate.usageLimits.credentialFingerprint === undefined && |
| 82 | candidate.email === account.email && |
| 83 | candidate.workspaceId === account.workspaceId, |
| 84 | ); |
| 85 | if (!last || last.usageLimits.unavailable) return account; |
| 86 | // Only the reading is lent; an unread model list stays unknown. |
| 87 | return { ...last, models: account.models }; |
| 88 | }), |
| 89 | }; |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | function missingManagementKey(config: UsageLimitSourceConfig): string { |
| 94 | return typeof config.managementKey === "string" |
| 95 | ? "No management key configured." |
| 96 | : "Could not read the management key from its secret provider."; |
| 97 | } |
| 98 | |
| 99 | /** @public */ |
| 100 | export const make = Effect.gen(function* () { |
| 101 | const api = yield* makeCliproxyApi; |
| 102 | const settingsService = yield* Settings.ServerSettingsService; |
| 103 | const secrets = yield* SecretResolver.SecretResolver; |
| 104 | const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy; |
| 105 | const readManagementKey = Effect.fn("UsageLimitSources.readManagementKey")(function* ( |
| 106 | config: UsageLimitSourceConfig, |
| 107 | ) { |
| 108 | const resolved = yield* resolveSettingsSecrets(secrets, UsageLimitSourceConfig, config); |
| 109 | return settingsSecretLiteral(resolved.managementKey); |
| 110 | }); |
| 111 | const stateRef = yield* Ref.make<ReadonlyArray<UsageLimitSourceSnapshot>>([]); |
| 112 | const changes = yield* Effect.acquireRelease( |
| 113 | PubSub.unbounded<ReadonlyArray<UsageLimitSourceSnapshot>>(), |
| 114 | PubSub.shutdown, |
| 115 | ); |
| 116 | |
| 117 | const readSource = Effect.fn("UsageLimitSources.readSource")(function* ( |
| 118 | id: UsageLimitSourceId, |
| 119 | config: UsageLimitSourceConfig, |
| 120 | ) { |
| 121 | const checkedAt = DateTime.formatIso(yield* DateTime.now); |
| 122 | const url = URL.parse(config.url); |
| 123 | const base = { |
| 124 | id, |
| 125 | kind: config.kind, |
| 126 | label: sourceLabel(id, config), |
| 127 | ...(url && /^https?:$/.test(url.protocol) |
| 128 | ? { |
| 129 | origin: url.origin, |
| 130 | endpoint: `${url.origin}${url.pathname.replace(/(\/management\.html)?\/*$/i, "")}`, |
| 131 | } |
| 132 | : {}), |
| 133 | checkedAt, |
| 134 | } as const; |
| 135 | const managementKey = yield* readManagementKey(config); |
| 136 | if (managementKey.length === 0) { |
| 137 | return { ...base, accounts: [], error: missingManagementKey(config) }; |
| 138 | } |
| 139 | const accounts = yield* api |
| 140 | .readAccounts({ url: config.url, managementKey }) |
| 141 | .pipe(Effect.result); |
| 142 | if (accounts._tag === "Failure") { |
| 143 | yield* Effect.logDebug("usage limit source read failed", { id, cause: accounts.failure }); |
| 144 | return { ...base, accounts: [], error: accounts.failure.detail }; |
| 145 | } |
| 146 | return { ...base, accounts: accounts.success }; |
| 147 | }); |
| 148 | |
| 149 | const publish = Effect.fn("UsageLimitSources.publish")(function* ( |
| 150 | next: ReadonlyArray<UsageLimitSourceSnapshot>, |
| 151 | ) { |
| 152 | const changed = yield* Ref.modify(stateRef, (previous) => |
| 153 | Equal.equals(previous, next) ? [false, previous] : [true, next], |
| 154 | ); |
| 155 | if (changed) yield* PubSub.publish(changes, next); |
| 156 | }); |
| 157 | |
| 158 | // Serialize refreshes so slow old-settings reads cannot resurrect removed sources after newer refreshes. |
| 159 | const refreshLock = yield* Semaphore.make(1); |
| 160 | const refresh = Effect.gen(function* () { |
| 161 | const settings = yield* settingsService.getSettings.pipe( |
| 162 | Effect.orElseSucceed((): ServerSettings | null => null), |
| 163 | ); |
| 164 | const entries = Object.entries(settings?.usageLimitSources ?? {}).filter( |
| 165 | ([, config]) => config.enabled, |
| 166 | ); |
| 167 | const snapshots = yield* Effect.forEach( |
| 168 | entries, |
| 169 | ([id, config]) => readSource(id as UsageLimitSourceId, config), |
| 170 | { concurrency: 4 }, |
| 171 | ); |
| 172 | yield* publish(keepLastGoodReadings(yield* Ref.get(stateRef), snapshots)); |
| 173 | }).pipe(refreshLock.withPermits(1), Effect.ignoreCause({ log: true })); |
| 174 | |
| 175 | // Shares the refresh lock so a stale in-flight read cannot overwrite a redemption. |
| 176 | const consumeResetCredit = Effect.fn("UsageLimitSources.consumeResetCredit")(function* ( |
| 177 | input: UsageLimitSourceConsumeResetCreditInput, |
| 178 | ) { |
| 179 | const settings = yield* settingsService.getSettings.pipe( |
| 180 | Effect.mapError(() => new UsageLimitSourceError({ detail: "Could not read hub settings." })), |
| 181 | ); |
| 182 | const config = settings.usageLimitSources[input.sourceId]; |
| 183 | if (!config?.enabled) { |
| 184 | return yield* new UsageLimitSourceError({ |
| 185 | detail: "The usage limit source is missing or disabled.", |
| 186 | }); |
| 187 | } |
| 188 | const managementKey = yield* readManagementKey(config); |
| 189 | if (managementKey.length === 0) { |
| 190 | return yield* new UsageLimitSourceError({ detail: missingManagementKey(config) }); |
| 191 | } |
| 192 | const result = yield* api.consume( |
| 193 | { url: config.url, managementKey }, |
| 194 | input.accountId, |
| 195 | input.creditId, |
| 196 | ); |
| 197 | // A redemption changes the reading, so a refused re-read must not bring back the one before it. |
| 198 | const snapshot = yield* readSource(input.sourceId, config); |
| 199 | const previous = yield* Ref.get(stateRef); |
| 200 | yield* publish(previous.map((source) => (source.id === input.sourceId ? snapshot : source))); |
| 201 | return result; |
| 202 | }, refreshLock.withPermits(1)); |
| 203 | |
| 204 | yield* settingsService.streamChanges.pipe( |
| 205 | Stream.map((settings) => settings.usageLimitSources), |
| 206 | Stream.changes, |
| 207 | Stream.runForEach(() => refresh), |
| 208 | Effect.forkScoped, |
| 209 | ); |
| 210 | |
| 211 | const interval = settingsService.getSettings.pipe( |
| 212 | Effect.map( |
| 213 | (settings) => resolveServerBackgroundActivitySettings(settings).providerHealthRefreshInterval, |
| 214 | ), |
| 215 | Effect.orElseSucceed(() => DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL), |
| 216 | ); |
| 217 | yield* Effect.forever( |
| 218 | interval.pipe( |
| 219 | Effect.flatMap((wait) => Effect.sleep(Duration.isPositive(wait) ? wait : "60 seconds")), |
| 220 | Effect.andThen(interval), |
| 221 | Effect.flatMap((current) => |
| 222 | Duration.isPositive(current) |
| 223 | ? backgroundPolicy.shouldRunScopeWork({ type: "provider-status" }) |
| 224 | : Effect.succeed(false), |
| 225 | ), |
| 226 | Effect.flatMap((shouldRun) => (shouldRun ? refresh : Effect.void)), |
| 227 | Effect.ignoreCause({ log: true }), |
| 228 | ), |
| 229 | ).pipe(Effect.forkScoped); |
| 230 | |
| 231 | yield* refresh.pipe(Effect.forkScoped); |
| 232 | |
| 233 | return { |
| 234 | current: Ref.get(stateRef), |
| 235 | consumeResetCredit, |
| 236 | refresh, |
| 237 | get streamChanges() { |
| 238 | return Stream.unwrap( |
| 239 | Effect.gen(function* () { |
| 240 | const subscription = yield* PubSub.subscribe(changes); |
| 241 | const snapshot = yield* Ref.get(stateRef); |
| 242 | return Stream.concat(Stream.make(snapshot), Stream.fromSubscription(subscription)).pipe( |
| 243 | Stream.changes, |
| 244 | ); |
| 245 | }), |
| 246 | ); |
| 247 | }, |
| 248 | } satisfies UsageLimitSources["Service"]; |
| 249 | }); |
| 250 | |
| 251 | export const layer = Layer.effect(UsageLimitSources, make).pipe( |
| 252 | Layer.provide(SecretResolverLayer.layer), |
| 253 | ); |
| 254 | |
| 255 | /** A hub returns a usage limit only when every account it routes the model to is spent. */ |
| 256 | export function hubPoolResetAt( |
| 257 | sources: ReadonlyArray<UsageLimitSourceSnapshot>, |
| 258 | driver: ProviderDriverKind, |
| 259 | model: string | null, |
| 260 | now: number, |
| 261 | ): string | null { |
| 262 | let earliest: number | null = null; |
| 263 | const routes = hubRoutes(sources, driver, model); |
| 264 | for (const account of sources.flatMap((source) => source.accounts).filter(routes)) { |
| 265 | // An unread account may be the one still blocked, so its refill is unknown. |
| 266 | if (account.usageLimits.unavailable) return null; |
| 267 | const spent = account.usageLimits.windows.filter( |
| 268 | (window) => |
| 269 | window.usedPercent >= 100 && |
| 270 | windowLimitsModel(window, model) && |
| 271 | (account.driver !== "antigravity" || antigravityWindowLimitsModel(window, model)), |
| 272 | ); |
| 273 | if (spent.length === 0) return null; |
| 274 | const resets = spent.map((window) => Date.parse(window.resetsAt ?? "")); |
| 275 | if (resets.some((reset) => !Number.isFinite(reset))) return null; |
| 276 | const recoversAt = Math.max(...resets); |
| 277 | if (recoversAt <= now) return null; |
| 278 | earliest = earliest === null ? recoversAt : Math.min(earliest, recoversAt); |
| 279 | } |
| 280 | return earliest === null ? null : DateTime.formatIso(DateTime.makeUnsafe(earliest)); |
| 281 | } |
| 282 | |
| 283 | export class UsageLimitResetFallback extends Context.Reference<{ |
| 284 | readonly resetAt: ( |
| 285 | driver: ProviderDriverKind, |
| 286 | endpoint: string, |
| 287 | model: string | null, |
| 288 | ) => Effect.Effect<string | null>; |
| 289 | }>("t3/usage/UsageLimitResetFallback", { |
| 290 | defaultValue: () => ({ resetAt: () => Effect.succeed(null) }), |
| 291 | }) {} |
| 292 | |
| 293 | export const layerResetFallback = Layer.effect( |
| 294 | UsageLimitResetFallback, |
| 295 | Effect.gen(function* () { |
| 296 | const sources = yield* UsageLimitSources; |
| 297 | return { |
| 298 | resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, endpoint, model) { |
| 299 | // Only the hub at the instance's endpoint serves its turns. |
| 300 | const atEndpoint = Effect.map(sources.current, (current) => |
| 301 | current.filter((source) => source.origin === endpoint), |
| 302 | ); |
| 303 | if ((yield* atEndpoint).length === 0) return null; |
| 304 | // The pool usually ran out after the last poll, so the cached accounts still show room. |
| 305 | yield* sources.refresh.pipe(Effect.timeout("5 seconds"), Effect.ignore); |
| 306 | return hubPoolResetAt( |
| 307 | yield* atEndpoint, |
| 308 | driver, |
| 309 | model, |
| 310 | DateTime.toEpochMillis(yield* DateTime.now), |
| 311 | ); |
| 312 | }), |
| 313 | }; |
| 314 | }), |
| 315 | ); |