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 1 day 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