Skip to content

feat: add automatic account switching when usage threshold reached#42

Open
122yjs wants to merge 1 commit intoLampese:mainfrom
122yjs:feature/auto-switch
Open

feat: add automatic account switching when usage threshold reached#42
122yjs wants to merge 1 commit intoLampese:mainfrom
122yjs:feature/auto-switch

Conversation

@122yjs
Copy link
Copy Markdown

@122yjs 122yjs commented Apr 11, 2026

Summary

This PR adds an automatic account switching feature that automatically rotates to a different account when the current account's usage reaches a configurable threshold.

Features

Auto-Switch Monitor

  • Background monitor that periodically checks account usage
  • Automatically switches to the next available account when threshold is exceeded (default 95%)
  • Skips switching while Codex is running to avoid interrupting active sessions
  • Respects weekly (secondary) limits in addition to 5-hour (primary) limits

Configuration

  • Threshold percentage (default 95%)
  • Check interval in seconds (default 60s)
  • Option to respect weekly limits
  • Priority account ordering
  • Account exclusion list

UI

  • New "Auto-Switch" button in header
  • Settings modal for configuration
  • Status indicator (running/stopped)
  • Switch history display

Implementation

Backend (Rust)

  • src-tauri/src/auto_switch.rs - Background monitoring logic
  • src-tauri/src/commands/auto_switch.rs - Tauri commands
  • AutoSwitchConfig and AutoSwitchEvent types in types.rs
  • Config stored in ~/.codex-switcher/auto_switch_config.json

Frontend (TypeScript/React)

  • src/hooks/useAutoSwitch.ts - React hook for state management
  • src/components/AutoSwitchSettings.tsx - Settings modal component

How It Works

┌─────────────────────────────────────┐
│     Auto-Switch Monitor            │
│  (checks every N seconds)          │
└──────────────┬──────────────────────┘                 ▼
┌─────────────────────────────────────┐
│ 1. Check if enabled & Codex not running  │
│ 2. Get current account usage             │
│ 3. If usage >= threshold:                │
│    - Find best available account          │
│    - Switch via ~/.codex/auth.json        │
│    - Log switch event                     │
└─────────────────────────────────────┘

Testing

Tested locally with multiple accounts. The monitor correctly:

  • Detects when Codex is running and skips checks
  • Switches accounts when threshold is reached
  • Respects priority order and exclusions
  • Handles API errors gracefully

- Add background monitor that periodically checks account usage
- Auto-switch to next available account when threshold (default 95%) exceeded
- Skip switching while Codex is running to avoid interrupting active sessions
- Add UI settings modal for configuration (threshold, interval, weekly limit)
- Store auto-switch config in ~/.codex-switcher/auto_switch_config.json
- Record switch events for history display

Backend changes:
- New auto_switch.rs module for background monitoring
- New Tauri/Web commands: get/set auto_switch_config, start/stop monitor
- AutoSwitchConfig and AutoSwitchEvent types in types.rs
- Storage functions for config persistence

Frontend changes:
- useAutoSwitch.ts hook for state management
- AutoSwitchSettings.tsx modal component
- Integration in App.tsx header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant