Files
t3code/packages/client-runtime/src/state/serverConfigProjection.ts
T
19d8ab2ae9 feat(usage): show Codex and Claude subscription limits on a Limits tab (#9507)
Users on Codex or Claude Code subscriptions could not see how much quota was left or when it resets without leaving T3 Code. A user whose CLIs route through a CLIProxyAPI hub could not see it at all.

Each driver now returns `usageLimits` on its own snapshot (Codex from `account/rateLimits/read`, Claude from the SDK's `get_usage`), adapters normalise turn-driven rate-limit events at the boundary, and a driver-blind ingestion layer folds them onto the owning instance. The Usage page gains a Limits tab (mobile a card) with a bar per window, elapsed marker, pace, and reset countdown. CLIProxyAPI hubs can be added as read-only usage-limit sources; their accounts show badged "via CLIProxyAPI" with emails blurred.

Distilled from #1732 (server model, provider rows) and #9421 (Limits tab, window bars, pace maths). Closes #228.

Co-authored-by: Aditya Mer <[email protected]>
Co-authored-by: Wout Stiens <[email protected]>
Co-authored-by: Claude Code <[email protected]>
2026-09-03 17:47:54 -07:00

100 lines
3.4 KiB
TypeScript

import type { ServerConfig, ServerConfigStreamEvent } from "@t3tools/contracts";
import * as Option from "effect/Option";
export interface ServerConfigProjection {
readonly config: ServerConfig;
readonly latestEvent: ServerConfigStreamEvent;
readonly source: "cache" | "live";
}
/**
* Cached config keeps the provider and model catalog available across reconnects.
* Published themes and usage-limit sources are current machine state, so a
* cache could restore a set the machine no longer reports. Replay sends both
* as separate events.
*/
export function withoutEnvironmentThemes(config: ServerConfig): ServerConfig {
if (config.environmentThemes === undefined && config.usageLimitSources === undefined) {
return config;
}
const { environmentThemes: _themes, usageLimitSources: _sources, ...rest } = config;
return rest;
}
export function applyServerConfigProjection(
current: Option.Option<ServerConfigProjection>,
event: ServerConfigStreamEvent,
): Option.Option<ServerConfigProjection> {
switch (event.type) {
case "snapshot": {
// Wire snapshots never contain published themes. Keep the previous set
// until a capable server sends its authoritative theme event. A legacy
// server cannot send a later removal, so a downgrade must clear the set.
const capabilities = event.config.environment.capabilities;
const carriedThemes =
capabilities.environmentThemes === true && Option.isSome(current)
? current.value.config.environmentThemes
: undefined;
const carriedSources =
capabilities.usageLimitSources === true && Option.isSome(current)
? current.value.config.usageLimitSources
: undefined;
return Option.some({
config: {
...event.config,
...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),
...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),
},
latestEvent: event,
source: "live" as const,
});
}
case "keybindingsUpdated":
return Option.map(current, (projection) => ({
config: {
...projection.config,
keybindings: event.payload.keybindings,
issues: event.payload.issues,
},
latestEvent: event,
source: "live",
}));
case "providerStatuses":
return Option.map(current, (projection) => ({
config: {
...projection.config,
providers: event.payload.providers,
},
latestEvent: event,
source: "live",
}));
case "settingsUpdated":
return Option.map(current, (projection) => ({
config: {
...projection.config,
settings: event.payload.settings,
},
latestEvent: event,
source: "live",
}));
case "environmentThemesUpdated":
return Option.map(current, (projection) => ({
config: {
...projection.config,
environmentThemes: event.payload.themes.length > 0 ? event.payload.themes : undefined,
},
latestEvent: event,
source: "live",
}));
case "usageLimitSourcesUpdated":
return Option.map(current, (projection) => ({
config: {
...projection.config,
usageLimitSources: event.payload.sources.length > 0 ? event.payload.sources : undefined,
},
latestEvent: event,
source: "live",
}));
}
}