opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat: Multi-Account OAuth Rotation with Settings UI and CLI Enhancements

Open mguttmann opened this issue 4 days ago • 2 comments

Summary

Implements comprehensive multi-account OAuth support with automatic rate limit rotation, manual account switching, and a new Settings menu for the desktop app.

Closes #9068

Changes

Backend - OAuth Pool Enhancements

  • Auth.OAuthPool.setActive() - Manually switch active OAuth account
  • Auth.OAuthPool.snapshot() - Returns activeID for credential selection
  • rotating-fetch.ts - Prefers activeID while preserving auto-rotation on 429
  • fetchAnthropicUsage() - Respects provider.active[namespace]
  • getAccounts() - Correctly identifies active account

API

  • POST /auth/active - Switch active account, returns updated usage data

Desktop App

New Settings Menu (DialogSettings):

  • Providers Tab: Connected providers, add new with search
  • Provider Detail: Account list, usage bars, switch functionality
  • About Tab: GitHub, docs, Discord, keyboard shortcuts

Context Panel:

  • Anthropic Rate Limits section (5h, 7d-all, 7d-sonnet bars)
  • Account switch buttons
  • Only visible for Anthropic sessions

CLI

  • opencode auth usage: Per-account usage with rate limits
  • opencode auth switch: Interactive account switching
  • opencode auth list: Shows account counts
  • All lists sorted alphabetically

Auto-Rotation Flow

1. Request with activeID (manually selected or first available)
2. On 429 rate limit:
   - Account gets cooldownUntil
   - Account moved to back of queue
   - Loop continues with next candidate
3. Next available account used automatically

Files Changed

Package File Description
opencode auth/index.ts Core OAuth pool functions
opencode auth/rotating-fetch.ts Credential selection logic
opencode server/server.ts /auth/active endpoint
opencode cli/cmd/auth.ts CLI commands
app dialog-settings.tsx New settings dialog
app session-context-tab.tsx Context panel usage section
app layout.tsx Settings button
sdk sdk.gen.ts, types.gen.ts Regenerated

Testing

CLI

opencode auth list      # Shows providers with account counts
opencode auth usage     # Shows per-account stats with rate limits
opencode auth switch    # Interactive account switching

Desktop

  1. Click ⚙️ Settings button → Providers tab
  2. Click connected provider → View accounts and usage
  3. Switch accounts → Verify bars update
  4. Open Anthropic session → Context panel shows rate limits

Limitations

  • Usage statistics: Anthropic only (OAuth API limitation)
  • Multi-account: Anthropic, OpenAI, GitHub Copilot
  • Other providers: Contributions welcome

Checklist

  • [x] Backend OAuth pool enhancements
  • [x] API endpoint for account switching
  • [x] Desktop Settings menu
  • [x] Context panel integration
  • [x] CLI commands
  • [x] Alphabetical sorting
  • [x] Auto-rotation preserved
  • [x] Local state updates (no full refresh on switch)

mguttmann avatar Jan 17 '26 10:01 mguttmann

The following comment was made by an LLM, it may be inaccurate:

Potential Related PRs Found

#8590 - feat(auth): OAuth Marathon - multi-account credential rotation

  • https://github.com/anomalyco/opencode/pull/8590
  • This PR directly addresses OAuth credential rotation across multiple accounts, which is the core feature in PR #9069. This may be a predecessor or overlapping work on the same feature.

#6905 - feat: display Anthropic and OpenAI OAuth usage in status dialog and sidebar

  • https://github.com/anomalyco/opencode/pull/6905
  • Related to OAuth usage display functionality, which overlaps with the usage bars and account statistics features in PR #9069.

#5158 - add ratelimiter to opencode server

  • https://github.com/anomalyco/opencode/pull/5158
  • Related to rate limiting infrastructure, which is relevant to the 429 rate limit rotation logic in PR #9069.

github-actions[bot] avatar Jan 17 '26 10:01 github-actions[bot]

Context on Related PRs

PR Status Relationship
#8590 Closed Earlier implementation attempt. This PR provides a complete, production-ready version.
#6905 Merged Basic OAuth usage display. This PR extends with multi-account support and Settings UI.
#5158 Merged Server rate limiter. This PR handles client-side rate limit rotation (429 responses).
#8912 Merged Usage dashboard foundation. This PR builds the full feature on top.

This PR represents the complete implementation of multi-account OAuth with:

  • Backend credential rotation ✅
  • API endpoints ✅
  • Desktop Settings UI ✅
  • Context panel integration ✅
  • CLI commands ✅

All functionality has been tested and works together cohesively.

mguttmann avatar Jan 17 '26 10:01 mguttmann