[PR #9545] feat(usage): unified usage tracking with auth refresh (#9281) #13144

Open
opened 2026-02-16 18:18:01 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/9545

State: open
Merged: No


Summary

Add /usage as 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 /usage command 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

  • New 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 caching
  • New packages/opencode/src/server/usage.ts: GET /usage endpoint with ?provider= filter and ?refresh= option
  • Claude token auto-refresh on 401 via console.anthropic.com/v1/oauth/token
  • usage.updated bus event for sidebar sync

Auth

  • packages/opencode/src/cli/cmd/auth.ts: second device-flow prompt during Copilot login using VS Code client ID (Iv1.b507a08c87ecfe98) to capture usage token for copilot_internal/user
  • packages/opencode/src/auth/index.ts: optional usage field in OAuth schema
  • packages/opencode/src/plugin/copilot.ts: attempt service token exchange via copilot_internal/v2/token; fallback to device token
  • packages/opencode/src/provider/auth.ts: persist usage token

TUI

  • New dialog-usage.tsx: renders usage windows with progress bars, reset times, credits/quota
  • prompt/index.tsx: /usage command with --current, --all flags; always fetches fresh data
  • sidebar.tsx: collapsible Usage panel; refreshes on usage.updated events and assistant turn completion
  • packages/opencode/src/config/config.ts: tui.show_usage_provider_scope setting ("current" | "all")

SDK/OpenAPI

  • Regenerated packages/sdk/openapi.json and packages/sdk/js/src/v2/gen/types.gen.ts

Tests

  • New packages/opencode/test/server/usage.test.ts: mock auth scenarios, provider filtering, error handling

AI Assistance

  • Tools: OpenCode + openai/gpt-5.2-codex
  • Testing: Tested on test build (bun turbo typecheck && bun run --cwd packages/opencode test test/server/usage.test.ts && bun run --cwd packages/opencode build)
  • Review: Human operator personally guided and reviewed changes

Notes

  • Baseline test failures (snapshot/skill tests) are unrelated
  • Full bun turbo test not run this session
  • E2E tests for device-flow not added (manual verification)
  • Follow-up: docs for tui.show_usage_provider_scope config
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9545 **State:** open **Merged:** No --- ## Summary Add `/usage` as 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 `/usage` command 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 - New `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 caching - New `packages/opencode/src/server/usage.ts`: `GET /usage` endpoint with `?provider=` filter and `?refresh=` option - Claude token auto-refresh on 401 via `console.anthropic.com/v1/oauth/token` - `usage.updated` bus event for sidebar sync ### Auth - `packages/opencode/src/cli/cmd/auth.ts`: second device-flow prompt during Copilot login using VS Code client ID (`Iv1.b507a08c87ecfe98`) to capture `usage` token for `copilot_internal/user` - `packages/opencode/src/auth/index.ts`: optional `usage` field in OAuth schema - `packages/opencode/src/plugin/copilot.ts`: attempt service token exchange via `copilot_internal/v2/token`; fallback to device token - `packages/opencode/src/provider/auth.ts`: persist `usage` token ### TUI - New `dialog-usage.tsx`: renders usage windows with progress bars, reset times, credits/quota - `prompt/index.tsx`: `/usage` command with `--current`, `--all` flags; always fetches fresh data - `sidebar.tsx`: collapsible Usage panel; refreshes on `usage.updated` events and assistant turn completion - `packages/opencode/src/config/config.ts`: `tui.show_usage_provider_scope` setting (`"current"` | `"all"`) ### SDK/OpenAPI - Regenerated `packages/sdk/openapi.json` and `packages/sdk/js/src/v2/gen/types.gen.ts` ### Tests - New `packages/opencode/test/server/usage.test.ts`: mock auth scenarios, provider filtering, error handling ## AI Assistance - **Tools**: OpenCode + openai/gpt-5.2-codex - **Testing**: Tested on test build (`bun turbo typecheck` && `bun run --cwd packages/opencode test test/server/usage.test.ts` && `bun run --cwd packages/opencode build`) - **Review**: Human operator personally guided and reviewed changes ## Notes - Baseline test failures (snapshot/skill tests) are unrelated - Full `bun turbo test` not run this session - E2E tests for device-flow not added (manual verification) - Follow-up: docs for `tui.show_usage_provider_scope` config
yindo added the pull-request label 2026-02-16 18:18:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13144