-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(web): show remaining usage limits in the sidebar footer #10707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lnieuwenhuis
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
lnieuwenhuis:feat/sidebar-usage-limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
219 changes: 219 additions & 0 deletions
219
apps/web/src/components/sidebar/SidebarUsageLimits.logic.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| import { | ||
| EnvironmentId, | ||
| ProviderDriverKind, | ||
| ProviderInstanceId, | ||
| type ServerProvider, | ||
| } from "@t3tools/contracts"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { collectSidebarLimits, limitTone } from "./SidebarUsageLimits.logic"; | ||
|
|
||
| const now = Date.parse("2026-09-03T12:00:00.000Z"); | ||
|
|
||
| const session = { | ||
| id: "five_hour", | ||
| kind: "session", | ||
| label: "Session", | ||
| usedPercent: 40, | ||
| windowDurationMins: 300, | ||
| resetsAt: "2026-09-03T14:00:00.000Z", | ||
| } as const; | ||
|
|
||
| const weekly = { | ||
| id: "seven_day", | ||
| kind: "weekly", | ||
| label: "Weekly", | ||
| usedPercent: 85, | ||
| windowDurationMins: 10_080, | ||
| resetsAt: "2026-09-06T15:30:00.000Z", | ||
| } as const; | ||
|
|
||
| function provider(overrides: Partial<ServerProvider>): ServerProvider { | ||
| return { | ||
| instanceId: ProviderInstanceId.make("codex"), | ||
| driver: ProviderDriverKind.make("codex"), | ||
| enabled: true, | ||
| installed: true, | ||
| version: null, | ||
| status: "ready", | ||
| auth: { status: "authenticated" }, | ||
| checkedAt: "2026-09-03T11:00:00.000Z", | ||
| models: [], | ||
| slashCommands: [], | ||
| skills: [], | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function presentations( | ||
| ...environments: ReadonlyArray<readonly [label: string, providers: readonly ServerProvider[]]> | ||
| ) { | ||
| return new Map( | ||
| environments.map(([label, providers], index) => [ | ||
| EnvironmentId.make(`env-${index + 1}`), | ||
| { entry: { target: { label } }, serverConfig: { providers } }, | ||
| ]), | ||
| ); | ||
| } | ||
|
|
||
| describe("limitTone", () => { | ||
| it("steps from neutral to low to critical as the quota drains", () => { | ||
| expect(limitTone(100)).toBe("ok"); | ||
| expect(limitTone(26)).toBe("ok"); | ||
| expect(limitTone(25)).toBe("low"); | ||
| expect(limitTone(11)).toBe("low"); | ||
| expect(limitTone(10)).toBe("critical"); | ||
| expect(limitTone(0)).toBe("critical"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("collectSidebarLimits", () => { | ||
| it("shows the tightest window per provider and keeps every window for the details", () => { | ||
| const [codex] = collectSidebarLimits( | ||
| presentations([ | ||
| "Laptop", | ||
| [ | ||
| provider({ | ||
| usageLimits: { checkedAt: "2026-09-03T11:00:00.000Z", windows: [session, weekly] }, | ||
| }), | ||
| ], | ||
| ]), | ||
| now, | ||
| ); | ||
| expect(codex).toMatchObject({ | ||
| driver: "codex", | ||
| accountCount: 1, | ||
| remainingPercent: 15, | ||
| tone: "low", | ||
| }); | ||
| expect(codex?.windows).toEqual([ | ||
| { | ||
| id: "session:five_hour", | ||
| label: "Session", | ||
| remainingPercent: 60, | ||
| resetsAt: Date.parse(session.resetsAt), | ||
| }, | ||
| { | ||
| id: "weekly:seven_day", | ||
| label: "Weekly", | ||
| remainingPercent: 15, | ||
| resetsAt: Date.parse(weekly.resetsAt), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("pools accounts across environments and reports the soonest reset", () => { | ||
| const views = collectSidebarLimits( | ||
| presentations( | ||
| [ | ||
| "Laptop", | ||
| [ | ||
| provider({ | ||
| auth: { status: "authenticated", email: "a@example.com" }, | ||
| usageLimits: { | ||
| checkedAt: "2026-09-03T11:00:00.000Z", | ||
| windows: [{ ...session, usedPercent: 90, resetsAt: "2026-09-03T13:00:00.000Z" }], | ||
| }, | ||
| }), | ||
| ], | ||
| ], | ||
| [ | ||
| "Desktop", | ||
| [ | ||
| provider({ | ||
| auth: { status: "authenticated", email: "b@example.com" }, | ||
| usageLimits: { | ||
| checkedAt: "2026-09-03T11:00:00.000Z", | ||
| windows: [{ ...session, usedPercent: 10 }], | ||
| }, | ||
| }), | ||
| ], | ||
| ], | ||
| ), | ||
| now, | ||
| ); | ||
| expect(views).toHaveLength(1); | ||
| expect(views[0]).toMatchObject({ accountCount: 2, remainingPercent: 50, tone: "ok" }); | ||
| expect(views[0]?.windows[0]?.resetsAt).toBe(Date.parse("2026-09-03T13:00:00.000Z")); | ||
| }); | ||
|
|
||
| it("orders providers by driver and leaves out ones with nothing usable", () => { | ||
| const views = collectSidebarLimits( | ||
| presentations([ | ||
| "Laptop", | ||
| [ | ||
| provider({ | ||
| usageLimits: { checkedAt: "2026-09-03T11:00:00.000Z", windows: [session] }, | ||
| }), | ||
| provider({ | ||
| instanceId: ProviderInstanceId.make("claude"), | ||
| driver: ProviderDriverKind.make("claudeAgent"), | ||
| usageLimits: { | ||
| checkedAt: "2026-09-03T11:00:00.000Z", | ||
| windows: [{ ...weekly, usedPercent: 95 }], | ||
| }, | ||
| }), | ||
| provider({ | ||
| instanceId: ProviderInstanceId.make("claude-api"), | ||
| driver: ProviderDriverKind.make("claudeAgent"), | ||
| usageLimits: { | ||
| checkedAt: "2026-09-03T11:00:00.000Z", | ||
| windows: [], | ||
| unavailable: { reason: "unsupported" }, | ||
| }, | ||
| }), | ||
| provider({ | ||
| instanceId: ProviderInstanceId.make("cursor"), | ||
| driver: ProviderDriverKind.make("cursor"), | ||
| }), | ||
| ], | ||
| ]), | ||
| now, | ||
| ); | ||
| expect(views.map((view) => [view.driver, view.remainingPercent, view.tone])).toEqual([ | ||
| ["claudeAgent", 5, "critical"], | ||
| ["codex", 60, "ok"], | ||
| ]); | ||
| }); | ||
|
|
||
| it("keeps a session and a monthly window apart when the provider reuses their id", () => { | ||
| const [codex] = collectSidebarLimits( | ||
| presentations( | ||
| [ | ||
| "Laptop", | ||
| [ | ||
| provider({ | ||
| auth: { status: "authenticated", email: "paid@example.com" }, | ||
| usageLimits: { | ||
| checkedAt: "2026-09-03T11:00:00.000Z", | ||
| windows: [{ ...session, id: "primary", usedPercent: 30 }], | ||
| }, | ||
| }), | ||
| ], | ||
| ], | ||
| [ | ||
| "Desktop", | ||
| [ | ||
| provider({ | ||
| auth: { status: "authenticated", email: "free@example.com" }, | ||
| usageLimits: { | ||
| checkedAt: "2026-09-03T11:00:00.000Z", | ||
| windows: [{ id: "primary", kind: "monthly", label: "Monthly", usedPercent: 70 }], | ||
| }, | ||
| }), | ||
| ], | ||
| ], | ||
| ), | ||
| now, | ||
| ); | ||
| expect(codex?.windows.map((window) => [window.id, window.remainingPercent])).toEqual([ | ||
| ["session:primary", 70], | ||
| ["monthly:primary", 30], | ||
| ]); | ||
| }); | ||
|
|
||
| it("is empty when no provider reports limits", () => { | ||
| expect(collectSidebarLimits(presentations(["Laptop", [provider({})]]), now)).toEqual([]); | ||
| expect(collectSidebarLimits(new Map(), now)).toEqual([]); | ||
| }); | ||
| }); | ||
67 changes: 67 additions & 0 deletions
67
apps/web/src/components/sidebar/SidebarUsageLimits.logic.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import type { ServerProvider } from "@t3tools/contracts"; | ||
| import { collectLimitAccounts, collectLimitPools } from "@t3tools/shared/usageLimits"; | ||
|
|
||
| /** Neutral until a quarter is left, then warning, then destructive at a tenth. */ | ||
| export type SidebarLimitTone = "ok" | "low" | "critical"; | ||
|
|
||
| const LOW_REMAINING_PERCENT = 25; | ||
| const CRITICAL_REMAINING_PERCENT = 10; | ||
|
|
||
| export interface SidebarLimitWindow { | ||
| /** `kind:id`, since Codex reuses `primary` for a session window on one plan and a monthly one on another. */ | ||
| readonly id: string; | ||
| readonly label: string; | ||
| readonly remainingPercent: number; | ||
| /** Soonest reset across the pooled accounts, epoch millis, or null when none reports one. */ | ||
| readonly resetsAt: number | null; | ||
| } | ||
|
|
||
| export interface SidebarLimitView { | ||
| readonly driver: ServerProvider["driver"]; | ||
| /** Distinct accounts pooled into these numbers. */ | ||
| readonly accountCount: number; | ||
| /** Quota left in the tightest window, which is what decides whether the next turn runs. */ | ||
| readonly remainingPercent: number; | ||
| readonly tone: SidebarLimitTone; | ||
| /** Every pooled window, ordered as Usage → Limits orders them. */ | ||
| readonly windows: readonly SidebarLimitWindow[]; | ||
| } | ||
|
|
||
| export function limitTone(remainingPercent: number): SidebarLimitTone { | ||
| if (remainingPercent <= CRITICAL_REMAINING_PERCENT) return "critical"; | ||
| if (remainingPercent <= LOW_REMAINING_PERCENT) return "low"; | ||
| return "ok"; | ||
| } | ||
|
|
||
| /** | ||
| * One entry per provider with usable limits, pooled across accounts and | ||
| * environments exactly as Usage → Limits pools them, reduced to the number | ||
| * the sidebar has room for: the share left in the most constrained window. | ||
| * Providers sort by driver so the pill keeps a stable reading order across | ||
| * sessions. `now` only feeds the pace maths the pill does not show. | ||
| */ | ||
| export function collectSidebarLimits( | ||
| presentations: Parameters<typeof collectLimitAccounts>[0], | ||
| now: number, | ||
| ): readonly SidebarLimitView[] { | ||
| const pools = collectLimitPools(collectLimitAccounts(presentations), now); | ||
| return pools | ||
| .filter((pool) => pool.windows.length > 0) | ||
| .map((pool) => { | ||
| const windows = pool.windows.map((window) => ({ | ||
| id: `${window.kind}:${window.id}`, | ||
| label: window.label, | ||
| remainingPercent: window.remainingPercent, | ||
| resetsAt: window.resets[0]?.at ?? null, | ||
| })); | ||
| const remainingPercent = Math.min(...windows.map((window) => window.remainingPercent)); | ||
| return { | ||
| driver: pool.driver, | ||
| accountCount: pool.accounts.length, | ||
| remainingPercent, | ||
| tone: limitTone(remainingPercent), | ||
| windows, | ||
| }; | ||
| }) | ||
| .sort((left, right) => left.driver.localeCompare(right.driver)); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.