feat(usage): unified usage tracking with auth refresh (#9281)#9545
feat(usage): unified usage tracking with auth refresh (#9281)#9545CasualDeveloper wants to merge 1 commit intoanomalyco:devfrom
Conversation
|
The following comment was made by an LLM, it may be inaccurate: Potential Duplicates Found:1. PR #9301: feat(tui): add /usage command and sidebar usage section
2. PR #6905: feat: display Anthropic and OpenAI OAuth usage in status dialog and sidebar
3. PR #9069: feat: Multi-Account OAuth Rotation with Settings UI and CLI Enhancements
|
ba7d89c to
9ce0a41
Compare
06731ef to
a396eca
Compare
f0e277d to
60f388a
Compare
bd214cc to
a7d1cc1
Compare
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
4089f14 to
824997e
Compare
824997e to
7cdb698
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a unified /usage API as the single source of truth for usage/rate-limit snapshots across OpenAI ChatGPT (Codex), GitHub Copilot, and Anthropic Claude, and wires it into the TUI (dialog + sidebar + toast warnings) with provider-specific auth handling (including optional Copilot “usage token” capture and Claude refresh-on-401).
Changes:
- Add core usage module + provider fetchers with caching, schema validation, and
usage.updatedbus events. - Add
GET /usageserver route (provider filter + refresh + slash-command parsing) and regenerate SDK/OpenAPI types/clients. - Add TUI integrations:
/usagecommand + dialog, sidebar usage panel, and automatic refresh/toast warnings after assistant turns; extend OAuth schema with optionalusagetoken.
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/sdk/openapi.json | Adds /usage endpoint + usage.updated event + OAuth usage field to OpenAPI schema. |
| packages/sdk/js/src/v2/gen/types.gen.ts | Regenerates SDK types for /usage, event, and OAuth usage?: string. |
| packages/sdk/js/src/v2/gen/sdk.gen.ts | Regenerates JS SDK client with client.usage.get(...). |
| packages/plugin/src/index.ts | Extends plugin-facing OAuth result typing with optional usage token. |
| packages/opencode/src/auth/index.ts | Extends persisted OAuth auth schema with optional usage token. |
| packages/opencode/src/usage/types.ts | Defines zod schemas + TS types for usage snapshots. |
| packages/opencode/src/usage/registry.ts | Defines supported usage providers and their auth requirements. |
| packages/opencode/src/usage/store.ts | Adds usage cache storage, usage.updated bus event, and auth/provider helpers. |
| packages/opencode/src/usage/providers/openai.ts | Implements ChatGPT usage fetcher (/backend-api/wham/usage) + normalization. |
| packages/opencode/src/usage/providers/github-copilot.ts | Implements Copilot usage fetcher (copilot_internal/user) + token fallback parsing. |
| packages/opencode/src/usage/providers/anthropic.ts | Implements Claude OAuth usage fetcher + refresh-on-expiry/401 flow. |
| packages/opencode/src/usage/index.ts | Exposes usage module API (Usage.*) and provider fetchers. |
| packages/opencode/src/usage/command.ts | Adds /usage command parsing and “current vs all” provider resolution logic. |
| packages/opencode/src/server/usage.ts | Adds GET /usage route: filtering, refresh behavior, concurrency dedupe, cached fallback errors. |
| packages/opencode/src/server/instance.ts | Registers /usage routes in server instance router. |
| packages/opencode/src/plugin/github-copilot/copilot.ts | Updates Copilot auth flow (optional second device flow for usage token) + service token exchange + header fixes. |
| packages/opencode/src/config/tui-schema.ts | Adds TUI config options for usage scope + used/remaining display mode. |
| packages/opencode/src/cli/cmd/tui/plugin/internal.ts | Registers new sidebar usage internal plugin. |
| packages/opencode/src/cli/cmd/tui/feature-plugins/sidebar/usage.tsx | Adds sidebar Usage panel rendering + refetch on events/turn completion. |
| packages/opencode/src/cli/cmd/tui/component/usage-data.ts | Adds TUI-side usage data shapes. |
| packages/opencode/src/cli/cmd/tui/component/usage-client.ts | Adds /usage client/resource + event-driven refetch logic. |
| packages/opencode/src/cli/cmd/tui/component/usage-format.ts | Adds formatting helpers (labels, bars, resets, credits). |
| packages/opencode/src/cli/cmd/tui/component/usage-toast.ts | Adds threshold-crossing detection + refresh/backoff helpers for toast warnings. |
| packages/opencode/src/cli/cmd/tui/component/dialog-usage.tsx | Adds /usage dialog UI with used/remaining toggle. |
| packages/opencode/src/cli/cmd/tui/component/dialog-command.tsx | Adds slashDescription field used for slash-command UI descriptions. |
| packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx | Implements built-in /usage command handling + post-turn refresh/toast logic + submission refactor. |
| packages/opencode/test/server/usage.test.ts | Adds server+fetcher tests for cache behavior, parsing, provider filtering, and error fallback. |
| packages/opencode/test/server/usage-copilot.test.ts | Adds Copilot fallback behavior test when usage endpoint fails. |
| packages/opencode/test/plugin/copilot-auth.test.ts | Adds tests for Copilot dual device-flow + optional usage polling + service token behavior. |
| packages/opencode/test/cli/tui/usage-toast.test.ts | Adds tests for warning thresholds, refresh cooldown/backoff, and dedupe/eviction. |
| packages/opencode/test/cli/tui/usage-format.test.ts | Adds tests for formatting/labeling and used vs remaining display conversion. |
| packages/opencode/test/cli/tui/usage-command.test.ts | Adds tests for /usage command parsing and conflict validation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8309992 to
1b0d195
Compare
|
Fork updated to sync with dev (as of cb1a500). |
9e63bb7 to
552fe6c
Compare
ca4c730 to
aa38024
Compare
3db7c0e to
2e5be67
Compare
6c9a249 to
b3616d6
Compare
d7e9c27 to
b3a7965
Compare
Adds Usage.Service as the shared usage tracking layer for OAuth-authenticated providers, including cached snapshots, in-flight fetch dedupe, token-scoped cache entries, usage.updated events, and OAuth refresh persistence with actionable Claude reauth errors. Exposes /usage through both the default Hono route and Effect HttpApi adapter, with OpenAPI/SDK support for explicit provider, refresh, and showUsageValueMode query parameters. Adds provider OAuth follow-up authorize/callback support for optional Copilot usage tracking, plus TUI /usage command parsing, usage dialog/sidebar display, idle refreshes, threshold toasts, and provider-specific usage formatting. Covers usage fetching, Copilot/OpenAI/Claude auth behavior, HttpApi parity, and TUI usage command/format/toast behavior.
b3a7965 to
c59711c
Compare
Summary
Adds built-in usage tracking for OAuth-authenticated providers: Anthropic Claude, GitHub Copilot, and OpenAI ChatGPT.
This PR implements the shared
GET /usageendpoint, generated SDK support, and terminal TUI surfaces. In the terminal TUI, users can view usage through a/usagedetail dialog and a sidebar Usage section./usageis the detailed all-provider view, while the sidebar remains configurable as a glance view.Web/app UI is intentionally left for a follow-up;
client.usage.get(...)is available for that future integration.Closes #9281
Related: #768
Supersedes #6905, #7837
Alternate implementation to #9301
Behavior
/usagefetches all authenticated usage providers and opens the detail dialog./usage --used//usage --remainingselect the display mode for that dialog invocation./usage --refreshrefreshes all provider caches and updates the sidebar without opening the dialog.tui.show_usage_provider_scopeapplies to the sidebar only (current/all).tui.show_usage_value_modecontrols the initial used/remaining display mode./usagefetches by explicitly refetching the shared usage resource.Core
GET /usageunderpackages/opencode/src/server/routes/instance/usage.ts.packages/opencode/src/usage/providers/for Anthropic Claude, GitHub Copilot, and OpenAI ChatGPT.usage.updatedwhen stored snapshots change.Auth
AuthOAuthFollowup).provider.oauth.followup.authorize/provider.oauth.followup.callbackserver endpoints.copilot_internal/userusage data.TUI
/usagedialog with provider-specific windows, progress bars, reset labels, and credits/quota details.Follow-up
GET /usageendpoint, likely as a Usage tab in the existing top-right status popover rather than a custom side panel.Verification
./script/generate.tsbun turbo typecheckbun --cwd packages/opencode test --timeout 600000 test/plugin/copilot-auth.test.ts test/server/usage.test.ts test/server/usage-copilot.test.ts test/cli/tui/usage-command.test.ts test/cli/tui/usage-format.test.ts test/cli/tui/usage-toast.test.ts48 tests pass in the targeted usage suite.
AI Assistance