Render all usage windows dynamically from the new limits[] API (per-model / Fable, spend)#7
Open
amalakhovsky wants to merge 2 commits into
Open
Conversation
The usage endpoint moved its per-window data out of the flat top-level keys (five_hour, seven_day, seven_day_<model>, which are now null) and into a self-describing `limits[]` array. Each entry carries its own kind/group/percent/severity/resets_at plus an optional scope naming a per-model window — e.g. a weekly Fable limit. The old code only knew the flat keys, so scoped-model windows (and any future window type) were silently dropped. - Add src/lib/usageModel.js: pure, unit-testable data-shaping (normalizeWindows / normalizeSpend) that prefers limits[] and falls back to the legacy keys. Covered by tools/test-usageModel.mjs. - Render one meter per reported window dynamically, reusing meters by a stable key and dropping windows the API stops reporting. - Floor each gauge's computed burn-consequence color at the API's own severity, so a window flagged warning/critical never reads calmer. - Use the structured `spend` object for the extra-usage line (with extra_usage as fallback), scaled by the API's decimal places, tinted by its severity. - Add a "Worst active limit" panel-window option so a maxed-out per-model window reaches the top-bar gauge. - Update poll.js to print the normalised windows + spend; refresh AGENTS.md data-source notes and the changelog.
These doc updates belong with the previous commit (they describe the new limits[]/spend shape and usageModel.js) but missed its staging.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Anthropic's usage endpoint (
GET /api/oauth/usage) has moved its per-window data out of the flat top-level keys (five_hour,seven_day,seven_day_<model>— now returned asnull) and into a self-describinglimits[]array. Each entry carries its ownkind(session/weekly_all/weekly_scoped),group,percent,severity(normal/warning/critical),resets_at,is_active, and an optionalscope.model.display_namenaming a per-model window (e.g. a weekly Fable limit). Money likewise moved to a structuredspendobject.The current code only knows the flat keys, so per-model windows — and any future window type — are silently dropped. In practice a maxed-out scoped limit (e.g. Fable at 100% /
critical) is completely invisible.This PR reads the new shape and renders every window the API reports, dynamically, while keeping the legacy keys as a fallback for older responses.
What changed
src/lib/usageModel.js— a pure, GI-free, unit-testable module that turns the payload into an ordered list of windows (normalizeWindows, preferringlimits[]and falling back to the legacy keys) and normalises the extra-usage money block (normalizeSpend).warning/criticalnever reads calmer than that.spend— the extra-usage line now usesspend(authoritative amount / limit / percent / severity), falling back toextra_usageand scaling by the API's owndecimal_placesinstead of assuming cents. It tints amber/red with the spend severity.tools/poll.jsnow prints the normalised windows + spend.Testing
node tools/test-usageModel.mjs— new pure-node unit tests (10, all passing) covering the newlimits[]shape, the legacy fallback, unknown/future scoped models, and the spend/extra_usage normalisation.normalizeWindows/normalizeSpendagainst a real live payload: renders5-hour 8%,7-day (all models) 64%,7-day Fable 100% (critical, active), andExtra usage: USD 416.54 / USD 500.00 (83%).glib-compile-schemasand./build.shboth succeed; the bundle includes the new module.Note: I couldn't get a live
gjs -m tools/poll.jsrun on my machine (libsoup throwsGio.TlsErrorreachingapi.anthropic.com, thoughcurlworks), so the model was validated against a captured real payload via node rather than end-to-end through the shell. A second pair of eyes on the live popup would be welcome.