Plugin Architecture
Codex Multi-Auth is built as a Codex CLI plugin using the@codex-ai/plugin framework. This architecture enables seamless integration with the Codex runtime while providing multi-account OAuth management and request routing.
Plugin Structure
The plugin exports a standard Plugin interface from@codex-ai/plugin with three main components:
Core Components
Authentication Provider
The plugin registers as an OAuth provider with the Codex runtime:Custom Fetch Pipeline
The plugin intercepts all OpenAI SDK calls with a 7-step request pipeline:- URL Rewriting:
/v1/chat/completions→https://chatgpt.com/backend-api/conversation - Token Refresh: Check expiry, refresh if needed (with skew window)
- Request Transform: Inject model-specific Codex instructions, apply fast-session defaults
- Account Selection: Choose account based on health, rate limits, session affinity
- Header Injection: Add OAuth bearer token, Codex-specific headers
- Stream Handling: Convert SSE to JSON, handle failover on stream errors
- Error Recovery: Retry with backoff, rotate accounts on rate limits
Tool Registration
The plugin registers tools using the@codex-ai/plugin/tool API:
Tool Context
Tools receive aToolContext with session metadata:
Hashline Tools
The plugin provides two advanced file editing tools:hashline_read
Reads files with SHA-1 hash verification for each line:hashline_edit
Performs hash-verified line edits with deterministic operation modes: Replace Operation:Event Handler
The plugin listens for runtime events to synchronize account selection:account.select- User switches account via TUI hotkey (1-9)openai.account.select- Provider-specific account selection
Plugin Lifecycle
Initialization (loader)
- Load plugin configuration from environment/Codex.json
- Apply UI theme and runtime settings
- Set storage scope (per-project vs global)
- Enable session affinity, refresh guardian, preemptive quota
- Load account pool with Codex CLI sync reconciliation
- Start live account sync watcher (if enabled)
- Prewarm Codex instructions and host prompts
Request Handling (fetch)
- Sync cached account manager from disk if stale
- Extract and rewrite request URL
- Parse request body, capture original stream flag
- Transform request with Codex instructions
- Resolve session affinity key from
CODEX_THREAD_IDorprompt_cache_key - Select account (prefer session-pinned, fallback to health-scored rotation)
- Execute request with retry/failover logic
- Update account health, rate limits, quota metadata
- Save updated storage on rotation or token refresh
Shutdown
Cleanup handlers registered viaregisterCleanup():
- Stop live account sync watcher
- Stop refresh guardian interval
- Flush pending storage writes
Advanced Tool Development
When creating custom tools for the plugin:Permission Requests
Always request permissions before file access:Path Resolution
Resolve paths relative to session directory:Display Path Formatting
Show relative paths when possible:Error Handling
Return user-friendly error messages:Plugin Configuration
Configure the plugin in your Codex.json:Next Steps
- Codex CLI Integration - Sync with @openai/codex CLI
- Automation - Use —json flags for CI/CD
- Configuration Reference - Full settings documentation