feat(usage): unified usage tracking with Copilot/Claude auth refresh #9545
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
Add
/usageas the single source of truth for provider usage data (Codex, Copilot, Claude) with TUI integration and automatic token refresh. Users can now view rate limits via/usagecommand or sidebar panel, with provider-specific auth flows handling token expiry transparently.Closes #9281, closes #728
Supersedes #6905, #7837
Alternate implementation to #9301
Changes
Core
packages/opencode/src/usage/index.ts(~850 lines): usage fetchers for Codex (/backend-api/wham/usage), Copilot (/copilot_internal/user), Claude (/api/oauth/usage) with schema validation and cachingpackages/opencode/src/server/usage.ts:GET /usageendpoint with?provider=filter and?refresh=optionconsole.anthropic.com/v1/oauth/tokenusage.updatedbus event for sidebar syncAuth
packages/opencode/src/cli/cmd/auth.ts: second device-flow prompt during Copilot login using VS Code client ID (Iv1.b507a08c87ecfe98) to captureusagetoken forcopilot_internal/userpackages/opencode/src/auth/index.ts: optionalusagefield in OAuth schemapackages/opencode/src/plugin/copilot.ts: attempt service token exchange viacopilot_internal/v2/token; fallback to device tokenpackages/opencode/src/provider/auth.ts: persistusagetokenTUI
dialog-usage.tsx: renders usage windows with progress bars, reset times, credits/quotaprompt/index.tsx:/usagecommand with--current,--allflags; always fetches fresh datasidebar.tsx: collapsible Usage panel; refreshes onusage.updatedevents and assistant turn completionpackages/opencode/src/config/config.ts:tui.show_usage_scopesetting ("current"|"all")SDK/OpenAPI
packages/sdk/openapi.jsonandpackages/sdk/js/src/v2/gen/types.gen.tsTests
packages/opencode/test/server/usage.test.ts: mock auth scenarios, provider filtering, error handlingAI Assistance
bun turbo typecheck&&bun run --cwd packages/opencode test test/server/usage.test.ts&&bun run --cwd packages/opencode build)Notes
bun turbo testnot run this sessiontui.show_usage_scopeconfig