Skip to content

Add Grok API and OAuth support - #413

Open
RasulOs wants to merge 1 commit into
mainfrom
feature/add-grok
Open

Add Grok API and OAuth support#413
RasulOs wants to merge 1 commit into
mainfrom
feature/add-grok

Conversation

@RasulOs

@RasulOs RasulOs commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add a first-class grok provider family with public xAI API-key support (XAI) and MobileRun-owned subscription OAuth (grok_oauth)
  • use grok-4.5 through the Responses API with a pinned 500,000-token context window, function calling, store=false, streamed usage extraction, and Grok-compatible sampling/structured-output handling
  • add apiKeys.xai / XAI_API_KEY, provider aliases, configure-wizard flows, CLI help, documentation, and a skipped-by-default Android 16 Portal E2E gate
  • centralize shared auth-profile updates behind cross-process locking, strict JSON validation, atomic/fsynced 0600 writes, and sibling-slot preservation

API-key behavior

XAI sends only to the pinned public endpoint https://api.x.ai/v1; generic endpoint overrides are discarded so the bearer key cannot be redirected. The adapter retains validated temperature and top_p, strips unsupported controls, pins the model and context metadata, supports text/image/tool/structured/streaming Responses operations, and records normal and completed-stream usage.

OAuth behavior and credential isolation

MobileRun performs its own xAI browser PKCE or device-code authorization and stores only its own grokOauth slot in the shared platform auth-profiles.json. It does not read, import, execute, or modify the Grok CLI or its credential file.

OAuth inference is pinned to https://cli-chat-proxy.grok.com/v1 with the observed compatibility headers. ID tokens are verified for signature, issuer, audience, expiry, and nonce. Refresh is coordinated across threads/processes, retries only replay-safe transient operations, preserves rotated refresh tokens, and retries one rejected inference exactly once before exposing stream events. There is no OAuth-to-API-key fallback.

The live gates verified that ~/.grok/auth.json retained the same existence, SHA-256, and mtime before and after all provider and Android tests. Saved logs, screenshots, UI trees, results, and trajectories passed recursive secret scanning.

Compatibility risk

The public API-key path uses xAI's documented API. The OAuth path interoperates with the observed public Grok Build OAuth client and subscription proxy contract, which xAI does not document as a stable third-party integration. It may require re-login or a compatibility update if xAI changes or revokes that contract; the implementation pins the observed contract and fails closed.

Validation

Automated checks:

  • 569 passed, 4 skipped, 3 subtests passed
  • scoped Ruff clean
  • git diff --check clean
  • lock resolves 196 packages
  • all 196 installed packages are compatible
  • staged diff passed a live-credential secret scan

Live public xAI API gate:

  • text response
  • forced tool call
  • image input
  • structured output
  • streaming and usage
  • temperature/top-p sampling

Live Grok OAuth gates:

  • browser PKCE login into MobileRun's grokOauth
  • isolated device-code login with the complete scope set
  • text/tool/image/structured/stream calls
  • forced expiry and refresh-token rotation
  • process restart using MobileRun-owned credentials
  • one real 401 refresh/replay recovery

Android 16 Portal E2E

All scenarios used only emulator-5558, Android 16 / API 36, the required AVD/snapshot, Portal-required TCP control, public mobilerun_core.Mobilerun harness actions/evidence, and independent ADB diagnostics/assertions. Each restored the clean snapshot first. The host emulator returned an all-black restored framebuffer, so the gate recorded that readiness failure, used the specified same-AVD no-wipe cold-boot fallback, and reran setup, both pings, doctor, Portal/accessibility checks, and evidence collection.

Authentication Mode / observation Result
XAI API key Direct / UI tree Reported Screen timeout as Not set, successful real tool calls, 33,170 Grok tokens, final Home
XAI API key Reasoning / vision-only Reported Android 16, successful real tool calls, 39,011 Grok tokens, final Home
grok_oauth Direct / UI tree Reported Screen timeout as Not set, successful real tool calls, 15,475 Grok tokens, final Home
grok_oauth Reasoning / vision-only Reported Android 16, successful real tool calls, 42,232 Grok tokens, final Home

@RasulOs
RasulOs marked this pull request as ready for review August 12, 2026 16:25
@RasulOs

RasulOs commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃挕 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b0c4b7cc7e

鈩癸笍 About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 馃憤.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

suffix=".tmp",
)
tmp_path = Path(raw_tmp_path)
os.fchmod(fd, 0o600)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use a Windows-compatible permission operation

On Windows, os.fchmod is unavailable, so every call that persists an API key or OAuth credential raises AttributeError before writing the temporary file. This affects the shared store now used by Grok, OpenAI, Anthropic, and Gemini authentication; use a portable permission operation or guard the Unix-only call as the later chmod calls are guarded.

Useful? React with 馃憤聽/ 馃憥.

Comment on lines +698 to 699
if provider_is_fixed:
provider_configured = _configure_provider_model(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Let Back escape provider-only configuration

When the command supplies only --provider for a family with one auth mode, such as ollama, minimax, or zai, this newly enters _configure_provider_model directly with provider_is_fixed=True and model_is_fixed=False. Selecting Back at the model prompt resets only the model, while the fixed family remains selected, so the inner loop immediately displays the same model prompt again and the user cannot return to the top-level menu without aborting the command.

Useful? React with 馃憤聽/ 馃憥.

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