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 { |
| 2 | EnvironmentId, |
| 3 | type ProviderInstanceId, |
| 4 | type ServerProvider, |
| 5 | type ServerProviderUsageLimits, |
| 6 | type UsageLimitSourceSnapshots, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { |
| 9 | antigravityWindowLimitsModel, |
| 10 | collectLimitAccounts, |
| 11 | collectLimitPools, |
| 12 | collectProviderUsageLimits, |
| 13 | displayLimitWindows, |
| 14 | hubRoutes, |
| 15 | instanceHubs, |
| 16 | type LimitPoolWindow, |
| 17 | } from "@t3tools/shared/usageLimits"; |
| 18 | import { limitsNotice } from "@t3tools/shared/usageLimitsCopy"; |
| 19 | |
| 20 | export type ThreadDetailsLimits = |
| 21 | | { readonly kind: "instance"; readonly limits: ServerProviderUsageLimits } |
| 22 | | { readonly kind: "pool"; readonly windows: readonly LimitPoolWindow[] }; |
| 23 | |
| 24 | // collectLimitAccounts keys its input by environment; the card reads one, so any id serves. |
| 25 | const CARD_ENVIRONMENT = EnvironmentId.make("thread-details"); |
| 26 | |
| 27 | export function resolveThreadDetailsLimits( |
| 28 | providerInstanceId: ProviderInstanceId | null, |
| 29 | model: string | null, |
| 30 | providers: readonly ServerProvider[], |
| 31 | sources: UsageLimitSourceSnapshots, |
| 32 | now: number, |
| 33 | ): ThreadDetailsLimits | null { |
| 34 | const provider = providers.find((entry) => entry.instanceId === providerInstanceId); |
| 35 | if (!provider) return null; |
| 36 | const report = collectProviderUsageLimits(provider.instanceId, providers, sources, now, model); |
| 37 | // Only this instance's data: the report keeps one instance per login, and any other entry may be |
| 38 | // a different login or workspace, so fall back to the instance's own snapshot. |
| 39 | const own = |
| 40 | report?.accounts.find((entry) => entry.instanceId === provider.instanceId)?.limits ?? |
| 41 | provider.usageLimits; |
| 42 | if (!own) return null; |
| 43 | const hubs = instanceHubs(provider, sources); |
| 44 | if (limitsNotice(own) === null || hubs.length === 0) return { kind: "instance", limits: own }; |
| 45 | // An instance sent to a hub reads no limits of its own; its hub pools the accounts it spends. |
| 46 | return hubPool(provider.driver, model, providers, hubs, now) ?? { kind: "instance", limits: own }; |
| 47 | } |
| 48 | |
| 49 | export function nextPoolReset(window: LimitPoolWindow, now: number) { |
| 50 | const pooled = window.members.length > 1; |
| 51 | return ( |
| 52 | window.resets.find((reset) => reset.at > now && (!pooled || reset.restoresPercent > 0)) ?? null |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | function hubPool( |
| 57 | driver: ServerProvider["driver"], |
| 58 | model: string | null, |
| 59 | providers: readonly ServerProvider[], |
| 60 | sources: UsageLimitSourceSnapshots, |
| 61 | now: number, |
| 62 | ): ThreadDetailsLimits | null { |
| 63 | const routes = hubRoutes(sources, driver, model); |
| 64 | const routed = sources.map((source) => ({ |
| 65 | ...source, |
| 66 | accounts: source.accounts.filter(routes).map((account) => |
| 67 | account.driver === "antigravity" |
| 68 | ? { |
| 69 | ...account, |
| 70 | usageLimits: { |
| 71 | ...account.usageLimits, |
| 72 | windows: account.usageLimits.windows.filter((window) => |
| 73 | antigravityWindowLimitsModel(window, model), |
| 74 | ), |
| 75 | }, |
| 76 | } |
| 77 | : account, |
| 78 | ), |
| 79 | })); |
| 80 | const readable = routed |
| 81 | .flatMap((source) => source.accounts) |
| 82 | .filter((account) => limitsNotice(account.usageLimits) === null); |
| 83 | if (readable.length === 0) return null; |
| 84 | // A signed-in login joins the pool only as a fresher reading of an account the hub routes to. |
| 85 | const signedIn = providers.filter((provider) => |
| 86 | readable.some( |
| 87 | (account) => |
| 88 | account.driver === provider.driver && |
| 89 | account.workspaceId === provider.auth.workspaceId && |
| 90 | account.email !== undefined && |
| 91 | account.email.toLowerCase() === provider.auth.email?.toLowerCase(), |
| 92 | ), |
| 93 | ); |
| 94 | const presentations = new Map([ |
| 95 | [ |
| 96 | CARD_ENVIRONMENT, |
| 97 | { |
| 98 | entry: { target: { label: "" } }, |
| 99 | serverConfig: { providers: signedIn, usageLimitSources: routed }, |
| 100 | }, |
| 101 | ], |
| 102 | ]); |
| 103 | const pools = collectLimitPools(collectLimitAccounts(presentations), now); |
| 104 | return pools.length > 0 ? { kind: "pool", windows: pools.flatMap(displayLimitWindows) } : null; |
| 105 | } |
| 106 | |
| 107 | export function poolSources(window: LimitPoolWindow) { |
| 108 | const hubs = window.members.flatMap(({ account }) => |
| 109 | account.environments.length === 0 && account.sourceLabel ? [account.sourceLabel] : [], |
| 110 | ); |
| 111 | const signedIn = window.members.some(({ account }) => account.environments.length > 0); |
| 112 | return { hubs: [...new Set(hubs)], signedIn }; |
| 113 | } |