░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░                                                                                   ░░
░░   ██╗     ███╗   ███╗ █████╗ ██████╗ ███████╗███╗   ██╗ █████╗                    ░░
░░   ██║     ████╗ ████║██╔══██╗██╔══██╗██╔════╝████╗  ██║██╔══██╗                   ░░
░░   ██║     ██╔████╔██║███████║██████╔╝█████╗  ██╔██╗ ██║███████║                   ░░
░░   ██║     ██║╚██╔╝██║██╔══██║██╔══██╗██╔══╝  ██║╚██╗██║██╔══██║                   ░░
░░   ███████╗██║ ╚═╝ ██║██║  ██║██║  ██║███████╗██║ ╚████║██║  ██║                   ░░
░░   ╚══════╝╚═╝     ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚═╝  ╚═══╝╚═╝  ╚═╝                   ░░
░░                                                                                   ░░
░░    █████╗ ██████╗ ██╗    ██████╗ ██████╗ ██╗██████╗  ██████╗ ███████╗             ░░
░░   ██╔══██╗██╔══██╗██║    ██╔══██╗██╔══██╗██║██╔══██╗██╔════╝ ██╔════╝             ░░
░░   ███████║██████╔╝██║    ██████╔╝██████╔╝██║██║  ██║██║  ███╗█████╗               ░░
░░   ██╔══██║██╔═══╝ ██║    ██╔══██╗██╔══██╗██║██║  ██║██║   ██║██╔══╝               ░░
░░   ██║  ██║██║     ██║    ██████╔╝██║  ██║██║██████╔╝╚██████╔╝███████╗             ░░
░░   ╚═╝  ╚═╝╚═╝     ╚═╝    ╚═════╝ ╚═╝  ╚═╝╚═╝╚═════╝  ╚═════╝ ╚══════╝             ░░
░░                                                                                   ░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

                              ⚡ STANDARD EDITION v3.2.6 ⚡
                       Advanced Automation & Rate Limit Protection


    ┌──────────────────────────────────────────────────────────────────────────────┐
    │                                                                              │
    │   This Tampermonkey script acts as a robust bridge between LMArena.ai and    │
    │   your local backend server.                                                 │
    │                                                                              │
    │   KEY FEATURES:                                                              │
    │     ✦ Automated reCAPTCHA token generation                                   │
    │     ✦ Intelligent Rate Limiting (Window + Cooldown)                          │
    │     ✦ Request Queue System (Optional, for slow models)                       │
    │     ✦ Session ID & Model List Capture                                        │
    │     ✦ 429 Lockdown Mode                                                      │
    │     ✦ Smart Error Handling (Rollbacks limits on generic errors)              │
    │                                                                              │
    └──────────────────────────────────────────────────────────────────────────────┘


═══════════════════════════════════════════════════════════════════════════════════
█▀▀ █▀▀ ▀█▀ █░█ █▀█   █▀▀ █░█ █ █▀▄ █▀▀
▄▄█ ██▄ ░█░ █▄█ █▀▀   █▄█ █▄█ █ █▄▀ ██▄
═══════════════════════════════════════════════════════════════════════════════════

    ╔═══════════════════════════════════════════════════════════════════════════╗
    ║                     🚀 SETUP INSTRUCTIONS                                 ║
    ╠═══════════════════════════════════════════════════════════════════════════╣
    ║                                                                           ║
    ║  STEP 1: INSTALLATION                                                     ║
    ║  ─────────────────────                                                    ║
    ║  1. Install the Tampermonkey extension for your browser.                  ║
    ║  2. Create a New Script.                                                  ║
    ║  3. Paste the contents of the `v3.2.6` script.                            ║
    ║  4. Save and Enable.                                                      ║
    ║                                                                           ║
    ║  STEP 2: CONNECTION                                                       ║
    ║  ──────────────────                                                       ║
    ║  1. Ensure your backend server is running on localhost:5102               ║
    ║  2. Open https://lmarena.ai                                               ║
    ║  3. Open Browser Console (F12). Look for:                                 ║
    ║     "[API Bridge] ✅ WebSocket connection established."                   ║
    ║                                                                           ║
    ║  STEP 3: GET MODEL LIST                                                   ║
    ║  ──────────────────────                                                   ║
    ║  1. Send "send_page_source" command from your backend                     ║
    ║  1a. Depending on your script, the command is usually                     ║
    ║  1b. 'python model_updater.py'                                            ║
    ║  Note: Step 3 is optional if you already have retrieved your models.      ║
    ║                                                                           ║
    ║  STEP 4: CONFIGURATION                                                    ║
    ║  ─────────────────────                                                    ║
    ║  1. Send `send_page_source` from backend to update model list.            ║
    ║  2. Send `activate_id_capture` from backend.                              ║
    ║  3. Click "Retry" on any message in LMArena to capture Session IDs.       ║
    ║  Depending on your script, the command is usually 'python id_updater.py'  ║
    ║                                                                           ║
    ║                                                                           ║
    ║  ╔═══════════════════════════════════════════════════════════════════╗    ║
    ║  ║  ⚠️  CRITICAL STEP 5: CAPTURE RECAPTCHA PARAMS                 ⚠️ ║    ║
    ║  ╠═══════════════════════════════════════════════════════════════════╣    ║
    ║  ║                                                                   ║    ║
    ║  ║  After capturing IDs, you MUST send ONE manual text message       ║    ║
    ║  ║  in the browser window.                                           ║    ║
    ║  ║                                                                   ║    ║
    ║  ║  Check the console for:                                           ║    ║
    ║  ║  "[API Bridge] ✅ Captured reCAPTCHA params"                      ║    ║
    ║  ║                                                                   ║    ║
    ║  ║  If you skip this, all requests will fail with 403 errors.        ║    ║
    ║  ║                                                                   ║    ║
    ║  ╚═══════════════════════════════════════════════════════════════════╝    ║
    ║                                                                           ║
    ║  STEP 6: START PROXYING! 🎉                                               ║
    ║  ──────────────────────────                                               ║
    ║  Your backend can now send requests through the bridge                    ║
    ║  Type help() in console for all commands                                  ║
    ║                                                                           ║
    ╚═══════════════════════════════════════════════════════════════════════════╝


═══════════════════════════════════════════════════════════════════════════════════
█▀▀ █▀█ █▄░█ █▀ █▀█ █░░ █▀▀   █▀▀ █▀█ █▀▄▀█ █▀▄▀█ █▀█ █▄░█ █▀▄ █▀
█▄▄ █▄█ █░▀█ ▄█ █▄█ █▄▄ ██▄   █▄▄ █▄█ █░▀░█ █░▀░█ █▀█ █░▀█ █▄▀ ▄█
═══════════════════════════════════════════════════════════════════════════════════

    Type these commands in the Browser Console (F12) to control the bridge.

    ┌──────────────────────────────────────────────────────────────────────────┐
    │                         🛠️  SYSTEM COMMANDS                              │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                                                                          │
    │  help()                    │ Show detailed command list & status         │
    │                                                                          │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                           QUEUE CONTROLS                                 │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                                                                          │
    │  enableQueue()             │ Enable request queuing (Default: OFF)       │
    │  disableQueue()            │ Disable queue (Send immediately)            │
    │  toggleQueue()             │ Toggle ON/OFF                               │
    │  queueStatus()             │ View current queue depth & timers           │
    │  clearQueue()              │ Cancel all pending requests                 │
    │  setQueueDelay(sec)        │ Set delay between items (Default: 120s)     │
    │                                                                          │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                        RATE LIMIT CONTROLS                               │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                                                                          │
    │  enableRateLimit()         │ Enable Window Limit (Default: ON)           │
    │  disableRateLimit()        │ Disable Window Limit (Risk of 429)          │
    │  enableCooldown()          │ Enable Cooldown (Default: ON)               │
    │  disableCooldown()         │ Disable Cooldown (Risk of 429)              │
    │  disableAllLimits()        │ ⚠️ Disable ALL limits (DANGER)              │
    │  enableAllLimits()         │ ✅ Restore ALL protections                  │
    │  setCooldown(sec)          │ Set wait time between requests              │
    │  setRateLimit(req, sec)    │ Set max requests per window                 │
    │                                                                          │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                          STATUS & DEBUG                                  │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                                                                          │
    │  rateLimitStatus()         │ Show live countdowns for limits             │
    │  debugRecaptcha()          │ Verify reCAPTCHA token generation           │
    │                                                                          │
    └──────────────────────────────────────────────────────────────────────────┘


═══════════════════════════════════════════════════════════════════════════════════
▀█▀ █▀█ █▀█ █░█ █▄▄ █░░ █▀▀ █▀ █░█ █▀█ █▀█ ▀█▀ █ █▄░█ █▀▀
░█░ █▀▄ █▄█ █▄█ █▄█ █▄▄ ██▄ ▄█ █▀█ █▄█ █▄█ ░█░ █ █░▀█ █▄█
═══════════════════════════════════════════════════════════════════════════════════


    ╔═══════════════════════════════════════════════════════════════════════════╗
    ║                          🚫 403 RECAPTCHA ERRORS                          ║
    ╠═══════════════════════════════════════════════════════════════════════════╣
    ║                                                                           ║
    ║  SYMPTOM: Proxy returns `{"error": "recaptcha validation failed"}`        ║
    ║                                                                           ║
    ║  SOLUTION:                                                                ║
    ║  1. Refresh the page.                                                     ║
    ║  2. Send a generic manual message (e.g., "hi") in the web interface.      ║
    ║  3. The script will log "✅ Captured reCAPTCHA params".                   ║
    ║  4. Retry your automated request.                                         ║
    ║                                                                           ║
    ╚═══════════════════════════════════════════════════════════════════════════╝


    ╔═══════════════════════════════════════════════════════════════════════════╗
    ║                          🔒 429 LOCKDOWN MODE                             ║
    ╠═══════════════════════════════════════════════════════════════════════════╣
    ║                                                                           ║
    ║  SYMPTOM: Proxy returns `RATE_LIMIT_EXCEEDED` and screen has red overlay. ║
    ║                                                                           ║
    ║  MECHANISM:                                                               ║
    ║  If the server returns HTTP 429 (Too Many Requests), the script enters    ║
    ║  Lockdown Mode automatically:                                             ║
    ║                                                                           ║
    ║  1. All Rate Limits & Queues are FORCED ON.                               ║
    ║  2. Settings CANNOT be disabled or adjusted.                              ║
    ║  3. Rate Limit Window increases by +2 minutes.                            ║
    ║  4. Anti-Refresh Shield is active (prevents accidental F5).               ║
    ║                                                                           ║
    ║  SOLUTION:                                                                ║
    ║  Wait for the timers to expire naturally. Refreshing the page resets      ║
    ║  the internal counters and may prolong your IP ban.                       ║
    ║                                                                           ║
    ╚═══════════════════════════════════════════════════════════════════════════╝


    ╔═══════════════════════════════════════════════════════════════════════════╗
    ║                          ⚠️ 422 FILTER ERRORS                             ║
    ╠═══════════════════════════════════════════════════════════════════════════╣
    ║                                                                           ║
    ║  SYMPTOM: Proxy returns generic error or 422.                             ║
    ║                                                                           ║
    ║  SOLUTION:                                                                ║
    ║  LMArena rejected your prompt. Modify your input content.                 ║
    ║  Note: 422 errors automatically rollback the rate limit counter,          ║
    ║  so you don't lose quota for a filter rejection.                          ║
    ║                                                                           ║
    ╚═══════════════════════════════════════════════════════════════════════════╝


═══════════════════════════════════════════════════════════════════════════════════
█▀▀ █▀█ █▄░█ █▄░█ █▀▀ █▀▀ ▀█▀ █ █▀█ █▄░█ █▀
█▄▄ █▄█ █░▀█ █░▀█ ██▄ █▄▄ ░█░ █ █▄█ █░▀█ ▄█
═══════════════════════════════════════════════════════════════════════════════════

    ┌──────────────────────────────────────────────────────────────────────────┐
    │                          🔌 ENDPOINTS                                    │
    ├──────────────────────────────────────────────────────────────────────────┤
    │                                                                          │
    │  WebSocket:      ws://localhost:5102/ws                                  │
    │  ID Capture:     http://localhost:5103/update                            │
    │  Models:         http://localhost:5102/internal/update_available_models  │
    │                  http://localhost:5102/internal/update_models            │
    │                  (Script attempts both automatically)                    │
    │                                                                          │
    └──────────────────────────────────────────────────────────────────────────┘

═══════════════════════════════════════════════════════════════════════════════════
█▄░█ █▀█ ▀█▀ █▀▀ █▀
█░▀█ █▄█ ░█░ ██▄ ▄█
═══════════════════════════════════════════════════════════════════════════════════

    ┌──────────────────────────────────────────────────────────────────────────┐
    │                                                                          │
    │  • Treat the script like it's LMArenaBridge's swansong!                  │
    │  • If something breaks, refresh the page and try again                   │
    │  • Queue is your friend for Opus/Gemini models                           │
    │  • If you get TypeStream errors, try not waiting too long to send        │
    │    messages and occassionally send a manual message.                     │
    │  • This script was tested with Liuanes's LMArenaBridge. It may or        │
    │    may not with similar proxy scripts.                                   │
    │                                                                          │
    └──────────────────────────────────────────────────────────────────────────┘



                                ~ END OF FILE ~