-
Notifications
You must be signed in to change notification settings - Fork 7.9k
feat(auth): Auto-Relogin via Persistent Browser Sessions #9455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(auth): Auto-Relogin via Persistent Browser Sessions #9455
Conversation
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found |
Non-network errors seen in logs: - AI_APICallError (402 deactivated_workspace) - AI_APICallError (500 server_error) - AI_LoadAPIKeyError / OpenAI API key is missing - ProviderInitError - ConfigInvalidError - ProviderAuthOauthCallbackFailed - NotFoundError - EditBuffer is destroyed
- Add getUsage() to fetch OAuth account status and health - Add fetchAnthropicUsage() to fetch Claude Max rate limits from Anthropic API - Add GET /auth/usage endpoint to expose usage data
- Add DialogAuthUsage component with rate limit visualization - Display Anthropic 5-hour and 7-day limits with progress bars - Show OAuth account status, cooldown state, and request counts - Add button in sidebar to open the dialog
- Add AuthUsageCommand to display rate limit info in terminal - Show account status, cooldown state, and request counts - Display Anthropic rate limits when available
The AI SDK's convertToModelMessages() does not accept 'step-start' as a valid UIMessagePart type. This caused AI_InvalidPromptError during session compaction. - Remove step-start from being added to UIMessage parts - Simplify the filter since step-start is no longer included - Fixes compaction breaking sessions with context overflow
Implements comprehensive multi-account OAuth support with automatic rate limit rotation, manual account switching, and a new Settings menu for the desktop app. - Add `Auth.OAuthPool.setActive()` to manually switch active OAuth account - Add `Auth.OAuthPool.snapshot()` returns `activeID` for credential selection - Update `rotating-fetch.ts` to prefer `activeID` while keeping auto-rotation - Update `fetchAnthropicUsage()` to respect `provider.active[namespace]` - Update `getAccounts()` to correctly identify active account - Add `POST /auth/active` endpoint to switch active OAuth account - Returns updated `anthropicUsage` for immediate UI updates - New `DialogSettings` component with tabbed interface - **Providers Tab**: View connected providers, add new providers with search - **Provider Detail View**: Account list, usage stats, switch functionality - **About Tab**: GitHub, docs, Discord links, keyboard shortcuts - Inline provider search without leaving settings context - Add Anthropic Rate Limits section in session context panel - Shows 5-hour, weekly (all models), weekly (sonnet) usage bars - Account switch buttons when multiple accounts configured - Only visible when current session uses Anthropic provider - `opencode auth usage`: Shows individual usage per OAuth account - `opencode auth switch`: Interactive command to switch active account - `opencode auth list`: Shows account count per provider - All provider lists now sorted alphabetically - `packages/opencode/src/auth/index.ts`: Core OAuth pool functions - `packages/opencode/src/auth/rotating-fetch.ts`: Credential selection - `packages/opencode/src/server/server.ts`: API endpoint - `packages/opencode/src/cli/cmd/auth.ts`: CLI commands - `packages/app/src/components/dialog-settings.tsx`: New settings UI - `packages/app/src/components/session/session-context-tab.tsx`: Context panel - `packages/app/src/pages/layout.tsx`: Settings button integration 1. Request uses `activeID` (manually selected or first available) 2. On 429 rate limit → account gets cooldown, moved to back 3. Next request automatically uses next available account 4. Manual switch via UI/CLI updates `provider.active[namespace]` Currently only Anthropic provides OAuth usage statistics. Other providers show multi-account switching but no usage bars. Contributions welcome for additional provider support.
- Add Auth.OAuthPool.removeRecord() to remove individual OAuth accounts - Add DELETE /auth/account API endpoint for Desktop app - Update CLI 'opencode auth logout' to select specific accounts - Add delete button with confirmation in Desktop Provider settings
- Move auth endpoints to provider routes - Add delete account functionality to dialog-auth-usage - Fix duplicate imports in layout.tsx - Fix message-v2.ts convertToModelMessages call - Regenerate SDK types
a88f9a5 to
9f73bcd
Compare
|
Rebased onto latest dev (v1.1.30+) and restructured to build on top of my other PRs: Branch chain: Changes in this update:
Files added/modified:
Build and typecheck pass. Ready for review! |
- Add browser.ts for Puppeteer-based browser session management - Add rotating-fetch.ts integration with auto-relogin trigger on 401s - Add AuthBrowserCommand CLI for setup/status/refresh operations - Add /provider/auth/browser/* API endpoints for browser session control - Add OAuthPool.updateRecord() to persist refreshed tokens
9f73bcd to
4d09284
Compare
Update: Rebased onto latest dev (v1.1.30+)Branch has been rebased onto the updated #9069 (Multi-Account OAuth). Structure:Note: This PR is independent of #9073 (YOLO Mode) - they are sibling branches that can be merged separately. Features:
How it works:
Files:
Typecheck passes ✅ | Build passes ✅ Depends on #9069 being merged first. |
- Add browser session management UI - Setup/Test/Remove buttons per account - Show last refresh time - Automatic token refresh via headless browser - Integration with auth.usage command
|
Closing in favor of combined PR with all features (Multi-Account, YOLO Mode, Auto-Relogin) |
Summary
Automatically refresh expired Anthropic OAuth tokens using persistent browser sessions with Puppeteer + Stealth plugin. This eliminates the need for manual re-authentication when tokens expire overnight.
Closes #9360
Problem
When using Anthropic OAuth (Claude Max subscription), tokens expire after a few hours. The refresh token also expires, causing "Token refresh failed: 400" errors. Users had to manually run
opencode authagain each morning.Solution
This PR adds automatic re-login via headless browser sessions:
opencode auth browser setupwhich opens a browser windowFeatures
CLI Commands
opencode auth browser setup- Configure browser session for an accountopencode auth browser status- Show status of all browser sessionsopencode auth browser remove- Remove a browser sessionTechnical Implementation
puppeteer-extra-plugin-stealthto bypass Cloudflare bot detectionconsole.anthropic.comandplatform.claude.comOAuth callbacksError Handling
rotating-fetch.tsFiles Changed
auth/browser.tsauth/rotating-fetch.tsauth/index.tsupdateRecord()for token updatescli/cmd/auth.tsserver/routes/provider.tsdialog-settings.tsxpackage.jsonDependencies
Added as optional dependencies (only installed when needed):
puppeteer: ^24.9.0puppeteer-extra: ^3.3.6puppeteer-extra-plugin-stealth: ^2.11.2Testing
opencode auth browser setupand log inScreenshots
(Screenshots to be added by @mguttmann)