* grok 0.1.13: import a CLIProxyAPI (CPA) xAI account from its file
CPA (CLIProxyAPI) keeps each of its accounts in ~/.cli-proxy-api as a flat
JSON file: {type: "xai", auth_kind: "oauth", access_token, refresh_token,
expired, last_refresh, sub, email, disabled, …}. Its xAI accounts sign in
with the same OAuth client Grok Build does, so such an account is a sign-in
Grok Build can hold — but the plugin could only take the CLI's own sign-in
or make a new one, so an account that lives in CPA could not be used here.
The third sign-in method takes the path to one of those files, reads it and
writes Grok Build's own shape (key, auth_mode: "oidc", create_time,
user_id, email, refresh_token, expires_at, oidc_issuer, oidc_client_id)
into a home of the plugin's own, as a device-code sign-in does. The
imported file is only read, and only the fields above are carried over:
base_url is not honored and expires_in is not turned into a lifetime
measured from the import.
A file is refused, before any home is made, when it is not an enabled
(type xai, auth_kind oauth, not disabled) xAI account with tokens, identity
and timestamps, or when the token's own iss/aud are not the Grok Build
xAI ones. The refresh token rotates and both clients would spend it, so the
prompt and the README say to stop using the account in CPA first. Nothing
here refreshes a live token or touches CPA.
bun test: 39 pass on the grok package (4 new in import.test.mjs: the
translated home, the refusals, no Grok Build, and a home that can't be
made). bun scripts/check.mjs grok and node --check pass.
* grok: string timestamps, and import.test.mjs off POSIX
`Date.parse` coerces, so a `last_refresh` or `expired` that was a list or an
object read as a date: the file must say a string before it is parsed.
The test's fake CLI is named for the platform (`grok.exe` on Windows), PATH
joins with the platform delimiter, and the 0600 file is only checked where
POSIX gives it. The mkdir that fails is what the last test covers, not the
cleanup after a write that fails, and it now says so.
* grok: the import's placeholder names the file CPA writes, xai-<email>.json (review of #92)
CLIProxyAPI's xAI sign-in saves each account as xai-<email>.json
(internal/auth/xai/token.go CredentialFileName), not cpa_*.json.
Text only; import.test.mjs still 5 pass.
---------
Co-authored-by: yetone <yetoneful@gmail.com>
5.1 KiB
@magpie-community/opencode-grok-auth
Grok with a SuperGrok or X Premium+ subscription, through the sign-in of
xAI's Grok Build CLI. Provider id: grok.
It needs Grok Build installed:
curl -fsSL https://x.ai/cli/install.sh | bash # macOS, Linux
irm https://x.ai/cli/install.ps1 | iex # Windows
Sign in
- Sign in with Grok Build (device code) runs
grok login --device-authand opens the page it prints. Approve the sign-in there, and the CLI finishes it.- If the CLI isn't signed in yet, the sign-in goes to the CLI's own home
(
GROK_HOME, else~/.grok), as if you had rungrok login. - If it is signed in, the sign-in goes to a home of this plugin's,
$XDG_CONFIG_HOME/opencode-grok-auth/<random>(else under~/.config). Grok Build's own sign-in stays as it is.
- If the CLI isn't signed in yet, the sign-in goes to the CLI's own home
(
- Use Grok Build's current sign-in takes the account the CLI is signed in to, with nothing to approve.
- Import CPA Grok account JSON copies one enabled xAI OAuth account from
a CLIProxyAPI (CPA) account JSON file (CPA keeps it as
~/.cli-proxy-api/xai-<email>.json) into a private home for this plugin. CPA and Grok Build rotate the same refresh token, so stop using that account in CPA before importing it. The file is read, never rewritten.
Where the sign-in is kept
The CLI keeps the sign-in in its home, as auth.json. OpenCode (or
magpie) keeps where that home is, the token and its expiry. Importing a CPA
account writes only its translated xAI OAuth fields to a private home owned
by this plugin; the selected CPA file is left unchanged.
The plugin reads auth.json; Grok Build refreshes the token, which changes
each time. The plugin runs grok models in that home, so the CLI renews
it, then reads it again:
- magpie renews the token 5 minutes before it ends, through the plugin's
auth.refresh, once for the account and before its requests, models and usage need it. magpie saves what the CLI gave. - OpenCode doesn't call that hook: there the token is renewed when it is 5 minutes from expiring, before a request.
- The CLI runs once for a home at a time: a request or renewal that comes while it runs waits for it and reads what it gave. A token the CLI already renewed is taken as it is.
- When the CLI no longer holds a sign-in in that home (
grok logout), the account has to be signed in again. When it couldn't renew a token that has ended, magpie tries again later.
Requests
Requests go to https://cli-chat-proxy.grok.com/v1, OpenAI's Responses
API (@ai-sdk/openai), signed as the CLI signs them:
Authorization: Bearer <token>x-xai-token-auth: xai-grok-clix-grok-client-identifier: grok-shellx-grok-client-mode: headlessx-grok-client-versionandUser-Agent: grok-shell/<version> (<os>; <arch>). The version is 1.0.41, or the installed CLI's if it is newer.x-grok-model-override: the request's model. A window suffix (grok-4.7@500k) is the plain model (grok-4.7) here and in the body.x-grok-context-window: the token count a suffixed model names (500000for@500k). The plain id sends no such header and stays on the default window.x-grok-conv-id: the request'sprompt_cache_key.
The backend turns away some of what a request may carry, so the plugin leaves these out:
- tools of types it doesn't take (a freeform
apply_patch, namespaces) external_web_accesson web search- a
tool_choicenaming a dropped type - a reasoning item's
"content": null
Plain Codex collaboration items (agent_message) reach Grok as user
messages, with their sender, recipient and content preserved. Sealed or
unrecognized content stays unchanged; the plugin does not decrypt tasks.
Models
Grok 4.7 until signed in. Signed in, the models are the ones the account
lists at /v1/models (Responses models only), each with its reasoning
efforts as variants.
context_window is the default window and keeps the plain id, so a saved
grok-4.7 stays a 256k model. Each other size in context_windows is
offered beside it, named Grok 4.7 @ 500k with id grok-4.7@500k and
that many tokens as its window. A request for it sends
x-grok-context-window: 500000. The proxy serves up to its largest
window either way and refuses past it; the header names the window the
caller asked to compact against. Pick grok-4.7@500k where a client
compacts at the advertised window, such as Claude Code.
Usage
magpie's card shows the credits of the current period
(/v1/billing?format=credits): weekly for SuperGrok, with on-demand
spending beside it when it has a cap.
Grok's billing says nothing of a plan's rate limit, so a free account can
read 0% used while its requests come back 429. After a 429 the card shows
a spent "Rate limit" window until the reset it named (Retry-After, or a
*ratelimit*reset* header). When it named none, the window stays until a
request goes through, or for 5 minutes.
Grok Bot's separate allowance is shown by the Cursor plugin. Sign in there with the Cursor account linked in Grok Bot, including for access through SuperGrok.