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, 03:37 UTC
| 1 | import type { |
| 2 | ProviderConsumeResetCreditInput, |
| 3 | ServerProviderResetCredits, |
| 4 | ServerProviderUsageLimits, |
| 5 | ServerProviderUsageWindow, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { mFallback, msg } from "gt-i18n"; |
| 8 | import type { MFunctionType } from "gt-i18n/types"; |
| 9 | |
| 10 | import { |
| 11 | collectExternalUsageLinks as collectExternalUsageLinksWith, |
| 12 | cursorUsageWindowDetails as cursorUsageWindowDetailsBase, |
| 13 | type LimitPool, |
| 14 | type LimitPresentations, |
| 15 | providersWithLimits, |
| 16 | type ResetCreditChoice, |
| 17 | resetCountdown, |
| 18 | } from "./usageLimits.ts"; |
| 19 | |
| 20 | const MINUTE = 60_000; |
| 21 | const HOUR = 60 * MINUTE; |
| 22 | const DAY = 24 * HOUR; |
| 23 | |
| 24 | const CURSOR_USAGE_WINDOW_COPY: Readonly< |
| 25 | Record<string, { readonly label: string; readonly description: string }> |
| 26 | > = { |
| 27 | totalPercentUsed: { |
| 28 | label: msg("Overall", { $context: "usage limit covering the whole plan" }), |
| 29 | description: msg("All usage on your plan this billing cycle."), |
| 30 | }, |
| 31 | autoPercentUsed: { |
| 32 | label: msg("Cursor Models"), |
| 33 | description: msg("Grok, Composer, and Auto use this allowance."), |
| 34 | }, |
| 35 | apiPercentUsed: { |
| 36 | label: msg("Other Models"), |
| 37 | description: msg("Usage allowance for third-party models such as Claude, GPT, and Gemini."), |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | /** @public */ |
| 42 | export function cursorUsageWindowDetails( |
| 43 | id: string, |
| 44 | pool: LimitPool, |
| 45 | resolve: MFunctionType = mFallback, |
| 46 | ) { |
| 47 | const details = cursorUsageWindowDetailsBase(id, pool); |
| 48 | const copy = CURSOR_USAGE_WINDOW_COPY[id]; |
| 49 | if (!details || !copy) return details; |
| 50 | return { |
| 51 | ...details, |
| 52 | label: details.label === null ? null : resolve(copy.label), |
| 53 | description: details.description === undefined ? undefined : resolve(copy.description), |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | /** @public */ |
| 58 | export function collectExternalUsageLinks( |
| 59 | presentations: LimitPresentations, |
| 60 | resolve: MFunctionType = mFallback, |
| 61 | ) { |
| 62 | return collectExternalUsageLinksWith(presentations, (provider, environment) => |
| 63 | resolve(msg("{provider} on {environment}", { provider, environment })), |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** @public */ |
| 68 | export function limitsNotice( |
| 69 | limits: ServerProviderUsageLimits, |
| 70 | resolve: MFunctionType = mFallback, |
| 71 | ): string | null { |
| 72 | if (limits.unavailable?.reason === "unsupported") { |
| 73 | return limits.unavailable.message ?? resolve(msg("This account has no subscription limits.")); |
| 74 | } |
| 75 | if (limits.unavailable?.reason === "probeFailed") { |
| 76 | return limits.unavailable.message ?? resolve(msg("Could not read limits.")); |
| 77 | } |
| 78 | return limits.windows.length === 0 ? resolve(msg("No limits reported.")) : null; |
| 79 | } |
| 80 | |
| 81 | /** @public */ |
| 82 | export function collectLimitNotices( |
| 83 | presentations: LimitPresentations, |
| 84 | resolve: MFunctionType = mFallback, |
| 85 | ): readonly string[] { |
| 86 | const label = (environmentLabel: string, subject: string) => |
| 87 | presentations.size > 1 ? `${environmentLabel} · ${subject}` : subject; |
| 88 | const notices = new Set<string>(); |
| 89 | for (const presentation of presentations.values()) { |
| 90 | const environmentLabel = presentation.entry.target.label; |
| 91 | for (const provider of providersWithLimits(presentation.serverConfig?.providers ?? [])) { |
| 92 | if (provider.usageLimits?.unavailable?.reason === "unsupported") continue; |
| 93 | const notice = provider.usageLimits ? limitsNotice(provider.usageLimits, resolve) : null; |
| 94 | const name = provider.displayName?.trim() || String(provider.driver); |
| 95 | if (notice) notices.add(`${label(environmentLabel, name)}: ${notice}`); |
| 96 | } |
| 97 | for (const source of presentation.serverConfig?.usageLimitSources ?? []) { |
| 98 | if (source.error) { |
| 99 | notices.add(`${label(environmentLabel, source.label)}: ${source.error}`); |
| 100 | } else if (source.accounts.length === 0) { |
| 101 | notices.add( |
| 102 | resolve( |
| 103 | msg("{source}: No accounts reported.", { |
| 104 | source: label(environmentLabel, source.label), |
| 105 | }), |
| 106 | ), |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return [...notices]; |
| 112 | } |
| 113 | |
| 114 | /** @public */ |
| 115 | export function formatDuration(ms: number, resolve: MFunctionType = mFallback): string { |
| 116 | const remaining = Math.max(0, ms); |
| 117 | const days = Math.floor(remaining / DAY); |
| 118 | const hours = Math.floor((remaining % DAY) / HOUR); |
| 119 | const minutes = Math.floor((remaining % HOUR) / MINUTE); |
| 120 | if (days > 0) return resolve(msg("{days}d {hours}h", { days, hours })); |
| 121 | if (hours > 0) return resolve(msg("{hours}h {minutes}m", { hours, minutes })); |
| 122 | return resolve(msg("{minutes}m", { minutes })); |
| 123 | } |
| 124 | |
| 125 | /** @public */ |
| 126 | export function resetCreditChoices( |
| 127 | credits: ServerProviderResetCredits, |
| 128 | input: ProviderConsumeResetCreditInput, |
| 129 | now: number, |
| 130 | compact = false, |
| 131 | resolve: MFunctionType = mFallback, |
| 132 | ): ReadonlyArray<ResetCreditChoice> { |
| 133 | const expiresIn = (at: string | undefined) => |
| 134 | at ? formatDuration(Date.parse(at) - now, resolve) : null; |
| 135 | if (credits.options && "instanceId" in input) { |
| 136 | return credits.options.map((option) => { |
| 137 | const expires = expiresIn(option.expiresAt); |
| 138 | const values = { label: option.label, count: option.availableCount }; |
| 139 | return { |
| 140 | key: option.creditId, |
| 141 | summary: expires |
| 142 | ? resolve(msg("{label} · {count} left · expires in {expires}", { ...values, expires })) |
| 143 | : resolve(msg("{label} · {count} left", values)), |
| 144 | actionLabel: resolve(msg("Use", { $context: "button that redeems a reset credit" })), |
| 145 | confirmDescription: resolve( |
| 146 | msg("{label}: {description} This cannot be undone.", { |
| 147 | label: option.label, |
| 148 | description: option.description, |
| 149 | }), |
| 150 | ), |
| 151 | availableCount: option.availableCount, |
| 152 | input: { ...input, creditId: option.creditId }, |
| 153 | }; |
| 154 | }); |
| 155 | } |
| 156 | const count = credits.availableCount; |
| 157 | const expires = expiresIn(credits.nextExpiresAt); |
| 158 | return [ |
| 159 | { |
| 160 | key: "next", |
| 161 | summary: |
| 162 | count === 0 |
| 163 | ? resolve(msg("No reset credits banked")) |
| 164 | : compact |
| 165 | ? expires |
| 166 | ? resolve(msg("{count} banked · expires in {expires}", { count, expires })) |
| 167 | : resolve(msg("{count} banked", { count })) |
| 168 | : expires |
| 169 | ? resolve( |
| 170 | msg( |
| 171 | "{count, plural, one {{count} reset credit banked · next expires in {expires}} other {{count} reset credits banked · next expires in {expires}}}", |
| 172 | { count, expires }, |
| 173 | ), |
| 174 | ) |
| 175 | : resolve( |
| 176 | msg( |
| 177 | "{count, plural, one {{count} reset credit banked} other {{count} reset credits banked}}", |
| 178 | { count }, |
| 179 | ), |
| 180 | ), |
| 181 | actionLabel: resolve(msg("Use reset")), |
| 182 | confirmDescription: resolve( |
| 183 | msg( |
| 184 | "This redeems one credit on your account and clears the current rate-limit windows. It cannot be undone.", |
| 185 | ), |
| 186 | ), |
| 187 | availableCount: count, |
| 188 | input, |
| 189 | }, |
| 190 | ]; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * `full` reads "resets in 2h 5m", `in` reads "in 2h 5m", `refill` reads "↻ 2h 5m". |
| 195 | * @public |
| 196 | */ |
| 197 | export function formatResetsIn( |
| 198 | window: ServerProviderUsageWindow, |
| 199 | now: number, |
| 200 | resolve: MFunctionType = mFallback, |
| 201 | style: "full" | "in" | "refill" = "full", |
| 202 | ): string | null { |
| 203 | const countdown = resetCountdown(window, now); |
| 204 | if (countdown === null) return null; |
| 205 | if (countdown.kind === "now") return resolve(msg("resets now")); |
| 206 | if (countdown.kind === "momentarily") return resolve(msg("Resets momentarily")); |
| 207 | const duration = formatDuration(countdown.ms, resolve); |
| 208 | if (style === "in") { |
| 209 | return resolve( |
| 210 | msg("in {duration}", { duration, $context: "time until a usage window resets" }), |
| 211 | ); |
| 212 | } |
| 213 | if (style === "refill") return resolve(msg("↻ {duration}", { duration })); |
| 214 | return resolve(msg("resets in {duration}", { duration })); |
| 215 | } |