-
Notifications
You must be signed in to change notification settings - Fork 5.3k
feat(web): show current-provider usage in the chat box #8445
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
Aditya190803
wants to merge
2
commits into
pingdotgg:main
Choose a base branch
from
Aditya190803:feat/usage-in-chat
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.
+521
−1
Open
Changes from all commits
Commits
Show all changes
2 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
134 changes: 134 additions & 0 deletions
134
apps/web/src/components/chat/ComposerUsageMeter.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,134 @@ | ||
| import { | ||
| ProviderDriverKind, | ||
| ProviderInstanceId, | ||
| type ServerProvider, | ||
| type ServerProviderUsageLimits, | ||
| } from "@t3tools/contracts"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { headlineUsageUsedPercent, resolveComposerUsageMeter } from "./ComposerUsageMeter.logic"; | ||
|
|
||
| function usageLimits(windows: ServerProviderUsageLimits["windows"]): ServerProviderUsageLimits { | ||
| return { | ||
| checkedAt: "2026-08-16T00:00:00.000Z", | ||
| windows, | ||
| }; | ||
| } | ||
|
|
||
| function provider( | ||
| overrides: Partial<ServerProvider> & Pick<ServerProvider, "driver">, | ||
| ): ServerProvider { | ||
| return { | ||
| instanceId: ProviderInstanceId.make("codex"), | ||
| enabled: true, | ||
| installed: true, | ||
| availability: "available", | ||
| auth: { status: "authenticated" }, | ||
| models: [], | ||
| ...overrides, | ||
| } as ServerProvider; | ||
| } | ||
|
|
||
| describe("headlineUsageUsedPercent", () => { | ||
| it("uses the most constrained window so the ring tracks the tighter limit", () => { | ||
| expect( | ||
| headlineUsageUsedPercent([ | ||
| { id: "primary", kind: "session", label: "Session", usedPercent: 35 }, | ||
| { id: "secondary", kind: "weekly", label: "Weekly", usedPercent: 82 }, | ||
| ]), | ||
| ).toBe(82); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveComposerUsageMeter", () => { | ||
| const codex = provider({ | ||
| driver: ProviderDriverKind.make("codex"), | ||
| displayName: "Codex", | ||
| usageLimits: usageLimits([ | ||
| { id: "primary", kind: "session", label: "Session", usedPercent: 20 }, | ||
| { id: "secondary", kind: "weekly", label: "Weekly", usedPercent: 55 }, | ||
| ]), | ||
| }); | ||
| const claude = provider({ | ||
| driver: ProviderDriverKind.make("claudeAgent"), | ||
| instanceId: ProviderInstanceId.make("claudeAgent"), | ||
| displayName: "Claude Code", | ||
| usageLimits: usageLimits([ | ||
| { id: "primary", kind: "session", label: "Session", usedPercent: 90 }, | ||
| ]), | ||
| }); | ||
|
|
||
| it("stays hidden until the setting is on", () => { | ||
| expect( | ||
| resolveComposerUsageMeter({ enabled: false, hasStartedTurn: true, provider: codex }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("stays hidden until the thread has started a turn", () => { | ||
| expect( | ||
| resolveComposerUsageMeter({ enabled: true, hasStartedTurn: false, provider: codex }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("shows only the current provider's windows", () => { | ||
| expect( | ||
| resolveComposerUsageMeter({ enabled: true, hasStartedTurn: true, provider: codex }), | ||
| ).toEqual({ | ||
| providerLabel: "Codex", | ||
| usageLimits: codex.usageLimits, | ||
| usedPercent: 55, | ||
| }); | ||
| expect( | ||
| resolveComposerUsageMeter({ enabled: true, hasStartedTurn: true, provider: claude }) | ||
| ?.providerLabel, | ||
| ).toBe("Claude Code"); | ||
| expect( | ||
| resolveComposerUsageMeter({ enabled: true, hasStartedTurn: true, provider: claude }) | ||
| ?.usedPercent, | ||
| ).toBe(90); | ||
| }); | ||
|
|
||
| it("hides when the current provider has no usable quota snapshot", () => { | ||
| expect( | ||
| resolveComposerUsageMeter({ enabled: true, hasStartedTurn: true, provider: undefined }), | ||
| ).toBeNull(); | ||
| expect( | ||
| resolveComposerUsageMeter({ | ||
| enabled: true, | ||
| hasStartedTurn: true, | ||
| provider: provider({ driver: ProviderDriverKind.make("codex"), usageLimits: undefined }), | ||
| }), | ||
| ).toBeNull(); | ||
| expect( | ||
| resolveComposerUsageMeter({ | ||
| enabled: true, | ||
| hasStartedTurn: true, | ||
| provider: provider({ | ||
| driver: ProviderDriverKind.make("codex"), | ||
| enabled: false, | ||
| usageLimits: usageLimits([ | ||
| { id: "primary", kind: "session", label: "Session", usedPercent: 10 }, | ||
| ]), | ||
| }), | ||
| }), | ||
| ).toBeNull(); | ||
| expect( | ||
| resolveComposerUsageMeter({ | ||
| enabled: true, | ||
| hasStartedTurn: true, | ||
| provider: provider({ | ||
| driver: ProviderDriverKind.make("grok"), | ||
| auth: { status: "authenticated", label: "Free", type: "Free" }, | ||
| usageLimits: { | ||
| checkedAt: "2026-08-16T00:00:00.000Z", | ||
| windows: [], | ||
| unavailable: { | ||
| reason: "unsupported", | ||
| message: "Usage is only shown for paid tiers", | ||
| }, | ||
| }, | ||
| }), | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
| }); |
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,57 @@ | ||
| import type { ServerProvider, ServerProviderUsageLimits } from "@t3tools/contracts"; | ||
|
|
||
| import { getDriverOption } from "../settings/providerDriverMeta"; | ||
|
|
||
| export type ComposerUsageMeterModel = { | ||
| readonly providerLabel: string; | ||
| readonly usageLimits: ServerProviderUsageLimits; | ||
| readonly usedPercent: number; | ||
| }; | ||
|
|
||
| export function headlineUsageUsedPercent(windows: ServerProviderUsageLimits["windows"]): number { | ||
| if (windows.length === 0) return 0; | ||
| return Math.max(...windows.map((window) => window.usedPercent)); | ||
| } | ||
|
|
||
| export function composerUsageProviderLabel(provider: ServerProvider): string { | ||
| return ( | ||
| provider.displayName?.trim() || | ||
| getDriverOption(provider.driver)?.label || | ||
| String(provider.driver) | ||
| ); | ||
| } | ||
|
|
||
| function isGrokFreeTier(provider: Pick<ServerProvider, "driver" | "auth">): boolean { | ||
| if (provider.driver !== "grok") return false; | ||
| const tier = (provider.auth.label ?? provider.auth.type ?? "").trim().toLowerCase(); | ||
| return tier === "free"; | ||
| } | ||
|
|
||
| /** | ||
| * Chat-box usage is opt-in, scoped to the thread's current provider, and | ||
| * hidden until the thread has started a turn. Missing, failed, or empty | ||
| * snapshots stay hidden so the chat box never renders an error state next | ||
| * to send. Any provider with usable windows shows. | ||
| */ | ||
| export function resolveComposerUsageMeter(input: { | ||
| readonly enabled: boolean; | ||
| readonly hasStartedTurn: boolean; | ||
| readonly provider: ServerProvider | null | undefined; | ||
| }): ComposerUsageMeterModel | null { | ||
| if (!input.enabled || !input.hasStartedTurn) return null; | ||
| const provider = input.provider; | ||
| if (!provider) return null; | ||
| if (!provider.enabled || !provider.installed || provider.availability === "unavailable") { | ||
| return null; | ||
| } | ||
| if (isGrokFreeTier(provider)) return null; | ||
|
|
||
| const usageLimits = provider.usageLimits; | ||
| if (!usageLimits || usageLimits.unavailable || usageLimits.windows.length === 0) return null; | ||
|
|
||
| return { | ||
| providerLabel: composerUsageProviderLabel(provider), | ||
| usageLimits, | ||
| usedPercent: headlineUsageUsedPercent(usageLimits.windows), | ||
| }; | ||
| } |
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,43 @@ | ||
| import type { ReactNode } from "react"; | ||
| import { renderToStaticMarkup } from "react-dom/server"; | ||
| import { describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| vi.mock("../ui/popover", () => ({ | ||
| Popover: ({ children }: { children: ReactNode }) => children, | ||
| PopoverPopup: ({ children }: { children: ReactNode }) => children, | ||
| PopoverTrigger: ({ render }: { render: ReactNode }) => <div>{render}</div>, | ||
| })); | ||
|
|
||
| vi.mock("~/hooks/useSettings", () => ({ | ||
| usePrimarySettings: (selector?: (settings: { timestampFormat: "locale" }) => unknown) => | ||
| selector ? selector({ timestampFormat: "locale" }) : { timestampFormat: "locale" }, | ||
| })); | ||
|
|
||
| import { ComposerUsageMeter } from "./ComposerUsageMeter"; | ||
|
|
||
| describe("ComposerUsageMeter", () => { | ||
| it("labels the control with the current provider and keeps usage out of the context meter", () => { | ||
| const markup = renderToStaticMarkup( | ||
| <ComposerUsageMeter | ||
| usage={{ | ||
| providerLabel: "Codex", | ||
| usedPercent: 55, | ||
| usageLimits: { | ||
| checkedAt: "2026-08-16T00:00:00.000Z", | ||
| windows: [ | ||
| { id: "primary", kind: "session", label: "Session", usedPercent: 20 }, | ||
| { id: "secondary", kind: "weekly", label: "Weekly", usedPercent: 55 }, | ||
| ], | ||
| }, | ||
| }} | ||
| />, | ||
| ); | ||
|
|
||
| expect(markup).toContain('aria-label="Codex usage 55% used"'); | ||
| expect(markup).toContain(">55%<"); | ||
| expect(markup).toContain("Usage"); | ||
| expect(markup).toContain("Session"); | ||
| expect(markup).toContain("Weekly"); | ||
| expect(markup).not.toContain("Context Window"); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium
chat/ChatComposer.tsx:5626When the desktop composer is resting without
activeContextWindow,composerUsageadds a percent button beside the send controls, but the prompt row still reserves onlypr-12(orpr-20with Attach), so the meter and send controls overlap the end of the one-line prompt for opted-in providers with quota data. Increase the resting prompt’s right padding to account for the usage meter, or keep the meter outside that absolutely positioned footer.🤖 Copy this AI Prompt to have your agent fix this: