ExtensionProvider@piex-dev/xai-oauth

xai-oauth — Subscription Login for Grok, Not API-Key-Locked

TL;DR

If you already pay for a Grok subscription, @piex-dev/xai-oauth lets Pi use that quota; the API Key path stays for automation and metered use.

Overview

Pi's built-in xai uses an API Key (metered, fixed model set, good for CI). But many users hold a SuperGrok subscription or X Premium+ quota — already paid, yet forced to open another API Key on a separate ledger. @piex-dev/xai-oauth adds xAI Grok to /login, authorizes via standard device-code OAuth, discovers models in the background after login (subscription vs public API routed separately), and coexists with the built-in xai.

How it works

Device Authorization Grant (RFC 8628): the extension acts as a public client (no secret), requests a device_code, the user authenticates on the official page, and the extension only gets a token; refresh_token renews before access_token expiry. After login it pulls /v1/models from both endpoints (public API api.x.ai + subscription proxy cli-chat-proxy.grok.com) and merges into the provider table — new models appear after /reload, no npm upgrade needed.

built-in xaixai-oauth
API Key authOAuth subscription
metered billingsubscription quota
fixed public setfallback + live discovery
built-inextension

Usage

Install

bash
pi install npm:@piex-dev/xai-oauth

Source: extensions/xai-oauth

Usage

flow
/login → pick xAI Grok (SuperGrok / X Premium+)
→ browser opens the verification page
→ /model grok-4.5 (or whatever id appears)

Configuration

Model whitelist/order: PI_XAI_OAUTH_MODELS=grok-build,grok-4.5. Subscription token stored in Pi's standard storage, never on disk in the extension. /reload picks up new models.

Verify

bash
bun test extensions/xai-oauth/test/xai-oauth.test.ts extensions/xai-oauth/test/models.test.ts

Implementation

xai-oauth.ts OAuth flow + registerProvider; models.ts catalog/discovery/merge/routing. One of the few piex packages with unit tests. Security posture: error truncation (extract only error/error_description, length-capped, no full response in logs); endpoint validation (*.x.ai only); token skew (expire minutes early + minimum TTL floor); slow_down (increase polling interval). Model layer: fallback catalog (offline-usable) + triggerDiscovery (background fetch after login) + rebuildModelsForOAuth (merge, mark routing) + env var filter.

Design notes

ProjectMechanismpiex choice
oh-my-pi xai-oauthfull RFC 8628 flow; dual-endpoint discovery; standard OAuth provider registrationAdopted: flow chain, client_id (same source), endpoint validation, error truncation/skew. Not adopted: Bun runtime
stnly/pi-grokpull /v1/models and merge into providerBorrowed: live-discovery idea, implementation rewritten independently
pi built-in xaiAPI Key + fixed model setCoexist: Key/CI + subscription/interactive, no conflict

Core trade-off: subscription token in Pi's standard storage (extension never persists), best-effort model discovery (fall back to fallback on failure), security posture "under-report rather than mis-report".

Changelog

Roadmap

DirectionPlan
Strong xAI-side dependencythin wrapper + fast follow; OIDC/device-code contract tests (snapshot mock) against regressions
Best-effort discovery/models or status marks source (discovered/fallback) and routing; PI_XAI_OAUTH_MODELS as first-class config
Raw error/quota semanticsper-error-class next steps (re-/login, switch model, check subscription)
Confusable with built-in xaiUI/status clarifies current provider and billing path
Enterprise uncoveredstabilize personal subscription path; evaluate enterprise separately

Version history

VersionDateChanges
0.1.12026-07-19Initial release: RFC 8628 OAuth flow; dual-endpoint model discovery (public API + subscription proxy); fallback catalog + triggerDiscovery + rebuildModelsForOAuth routing merge; PI_XAI_OAUTH_MODELS whitelist; security posture (error truncation, endpoint validation *.x.ai, token skew, slow_down); subscription token via Pi standard storage, no disk; unit-test coverage