Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

add Grok subscription provider

Raine Virta69dfe2ef92a9
diff --git a/Cargo.toml b/Cargo.toml
index 07d58eb..5a4903c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -25,8 +25,7 @@ async-trait = "0.1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "json"] }
uuid = { version = "1", features = ["v4", "serde"] }
-time = { version = "0.3", features = ["formatting"] }
-jiff = { version = "0.2", default-features = false, features = ["std", "tz-system", "tzdb-zoneinfo"] }
+time = { version = "0.3", features = ["formatting", "parsing"] }
reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls", "blocking", "http2"] }
base64 = "0.22"
rand = "0.8"
@@ -40,6 +39,7 @@ prost = "0.13"
flate2 = "1.0"
ratatui = "0.29"
crossterm = "0.28"
+jiff = { version = "0.2", default-features = false, features = ["std", "tz-system", "tzdb-zoneinfo"] }
[dev-dependencies]
tempfile = "3.14"
diff --git a/README.md b/README.md
index e944eff..ffca6c1 100644
--- a/README.md
+++ b/README.md
@@ -2,8 +2,8 @@
`claude-code-proxy` lets you use
[Claude Code](https://www.anthropic.com/claude-code) with your **ChatGPT
-Plus/Pro** subscription, your **Kimi Code** (kimi.com) account, or **Cursor
-Agent**.
+Plus/Pro** subscription, your **Kimi Code** (kimi.com) account, your **Grok**
+subscription, or **Cursor Agent**.
<img src="meta/claude-code-screenshot.webp" alt="Claude Code running through claude-code-proxy" width="630" />
@@ -46,7 +46,7 @@ artifacts are published as `claude-code-proxy-windows-amd64.zip` and
### 2. Pick a provider and authenticate
-The proxy supports three upstream providers. Pick one and run its login flow; the
+The proxy supports four upstream providers. Pick one and run its login flow; the
proxy will refuse to start traffic until a token is stored.
**Codex (ChatGPT Plus/Pro):**
@@ -68,6 +68,15 @@ claude-code-proxy kimi auth login # device-code flow (prints URL + code)
Sign in with your **kimi.com account**. The verification URL is displayed; open
it in any browser, confirm the code, and the CLI polls until done.
+**Grok (grok.com):**
+
+```sh
+claude-code-proxy grok auth login # browser OAuth (PKCE)
+```
+
+Sign in with your **grok.com account**. The proxy stores and refreshes its own
+OAuth session and does not use the official Grok CLI credential file.
+
**Cursor Agent:**
```sh
@@ -90,6 +99,7 @@ Verify:
```sh
claude-code-proxy codex auth status
claude-code-proxy kimi auth status
+claude-code-proxy grok auth status
claude-code-proxy cursor auth status
```
@@ -112,6 +122,7 @@ requests, and error events. Use `--no-monitor` for plain terminal output.
- `gpt-5.6-sol`, `gpt-5.6-terra`, `gpt-5.6-luna`, `gpt-5.5`, `gpt-5.4`, `gpt-5.3-codex`, `gpt-5.3-codex-spark`, `gpt-5.4-mini`, `gpt-5.2` → **codex**
- `kimi-for-coding`, `kimi-k2.6`, `k2.6` → **kimi**
+- `grok-composer-2.5-fast`, `grok-4.5` → **grok**
- `cursor`, `cursor-plan`, `cursor-ask`, `composer-2.5`, `composer-2.5-fast`, `cursor:<model-id>`, `cursor-plan:<model-id>`, `cursor-ask:<model-id>` → **cursor**
An unknown model returns a 400 listing the supported ids. There is no
@@ -143,6 +154,15 @@ CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 \
CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK=1 \
claude
+# Grok
+ANTHROPIC_BASE_URL=http://localhost:18765 \
+ANTHROPIC_AUTH_TOKEN=unused \
+ANTHROPIC_MODEL=grok-composer-2.5-fast \
+ANTHROPIC_SMALL_FAST_MODEL=grok-composer-2.5-fast \
+CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 \
+CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK=1 \
+ claude --model grok-composer-2.5-fast
+
# Cursor Agent
ANTHROPIC_BASE_URL=http://localhost:18765 \
ANTHROPIC_AUTH_TOKEN=unused \
@@ -330,6 +350,25 @@ Auth:
| `kimi auth status` | Show user ID + token expiry |
| `kimi auth logout` | Delete stored credentials |
+### Grok
+
+Upstream: `https://cli-chat-proxy.grok.com/v1/responses` (Responses API).
+
+Supported model ids are `grok-composer-2.5-fast` and `grok-4.5`. Model access
+can vary by account and region. The proxy translates Claude Code messages,
+function tools, tool results, thinking, token counts, and streaming events.
+Grok reasoning text appears in Claude Code as Anthropic `thinking` blocks.
+
+Authentication uses browser OAuth with S256 PKCE through `auth.x.ai` and an
+ephemeral loopback callback. The proxy stores its own access and refresh tokens,
+refreshes them five minutes before expiry, and does not use `~/.grok/auth.json`.
+
+| Command | What it does |
+| ------------------ | ----------------------------------- |
+| `grok auth login` | Browser OAuth with a local callback |
+| `grok auth status` | Show token expiry and storage path |
+| `grok auth logout` | Delete proxy-owned credentials |
+
### Cursor Agent
Upstream: `https://api2.cursor.sh/agent.v1.AgentService/Run` (Cursor Agent's
@@ -629,6 +668,10 @@ Windows, and at
"oauthHost": "https://auth.kimi.com",
"baseUrl": "https://api.kimi.com/coding/v1"
},
+ "grok": {
+ "baseUrl": "https://cli-chat-proxy.grok.com/v1",
+ "clientVersion": "0.2.93"
+ },
"cursor": {
"baseUrl": "https://api2.cursor.sh",
"clientVersion": "cli-2026.06.04-5fd875e",
@@ -662,6 +705,8 @@ Windows, and at
| `CCP_CODEX_ORIGINATOR` | `codex.originator` | `claude-code-proxy` | Override the `originator` header sent to Codex |
| `CCP_CODEX_USER_AGENT` | `codex.userAgent` | `claude-code-proxy/<version>` | Override the `User-Agent` header sent to Codex |
| `CCP_KIMI_USER_AGENT` | `kimi.userAgent` | `KimiCLI/1.37.0` | Override the `User-Agent` header sent to Kimi |
+| `CCP_GROK_BASE_URL` | `grok.baseUrl` | `https://cli-chat-proxy.grok.com/v1` | Override the Grok Responses API base URL |
+| `CCP_GROK_CLIENT_VERSION` | `grok.clientVersion` | `0.2.93` | Override the Grok client version header |
| `CCP_CURSOR_BASE_URL` | `cursor.baseUrl` | `https://api2.cursor.sh` | Override Cursor's API base URL |
| `CCP_CURSOR_CLIENT_VERSION` | `cursor.clientVersion` | `cli-2026.06.04-5fd875e` | Override Cursor client version headers |
| `CCP_CURSOR_AGENT_BUNDLE` | `cursor.agentBundle` | auto-detected | Path to Cursor Agent's bundled `index.js` used only for protobuf schemas |
diff --git a/src/auth.rs b/src/auth.rs
index 3db4a2e..279c294 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -335,11 +335,27 @@ pub fn write_atomically<T: Serialize>(path: &str, value: &T) -> Result<()> {
fs::create_dir_all(dir)?;
set_mode(dir, 0o700);
- let tmp = format!("{path}.tmp-{}", std::process::id());
- let mut out = File::create(&tmp)?;
+ let tmp = format!("{path}.tmp-{}", uuid::Uuid::new_v4());
+ #[cfg(unix)]
+ let mut out = {
+ use std::os::unix::fs::OpenOptionsExt;
+ std::fs::OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .mode(0o600)
+ .open(&tmp)?
+ };
+ #[cfg(not(unix))]
+ let mut out = std::fs::OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .open(&tmp)?;
out.write_all(to_string_pretty(value)?.as_bytes())?;
out.sync_all()?;
- fs::rename(&tmp, path)?;
+ if let Err(err) = fs::rename(&tmp, path) {
+ let _ = fs::remove_file(&tmp);
+ return Err(err.into());
+ }
set_mode(std::path::Path::new(path), 0o600);
Ok(())
}
diff --git a/src/config.rs b/src/config.rs
index bd0c605..65023ab 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -38,6 +38,7 @@ struct FileConfig {
pub kimi: Option<KimiConfig>,
pub codex: Option<CodexConfig>,
pub cursor: Option<CursorConfig>,
+ pub grok: Option<GrokConfig>,
}
#[derive(Deserialize, Clone)]
@@ -81,6 +82,14 @@ struct KimiConfig {
pub base_url: Option<String>,
}
+#[derive(Deserialize, Clone)]
+struct GrokConfig {
+ #[serde(rename = "baseUrl")]
+ pub base_url: Option<String>,
+ #[serde(rename = "clientVersion")]
+ pub client_version: Option<String>,
+}
+
#[derive(Deserialize)]
struct FileLog {
pub verbose: Option<bool>,
@@ -206,6 +215,12 @@ pub fn config_override_summary_lines(cfg: &LoadedConfig) -> Vec<String> {
if env.contains_key("CCP_KIMI_USER_AGENT") {
out.push("kimi.userAgent (env)".to_string());
}
+ if env.contains_key("CCP_GROK_BASE_URL") {
+ out.push("grok.baseUrl (env)".to_string());
+ }
+ if env.contains_key("CCP_GROK_CLIENT_VERSION") {
+ out.push("grok.clientVersion (env)".to_string());
+ }
if env
.get("CCP_CODEX_REASONING_SUMMARY")
.is_some_and(|raw| !raw.is_empty())
@@ -237,6 +252,32 @@ pub fn config_override_summary_lines(cfg: &LoadedConfig) -> Vec<String> {
out
}
+pub fn grok_base_url() -> String {
+ let env: HashMap<_, _> = std::env::vars().collect();
+ if let Some(raw) = env.get("CCP_GROK_BASE_URL") {
+ return raw.clone();
+ }
+ if let Some(grok) = read_file_config(&paths::config_dir()).and_then(|f| f.grok)
+ && let Some(url) = grok.base_url
+ {
+ return url;
+ }
+ "https://cli-chat-proxy.grok.com/v1".to_string()
+}
+
+pub fn grok_client_version() -> String {
+ let env: HashMap<_, _> = std::env::vars().collect();
+ if let Some(raw) = env.get("CCP_GROK_CLIENT_VERSION") {
+ return raw.clone();
+ }
+ if let Some(grok) = read_file_config(&paths::config_dir()).and_then(|f| f.grok)
+ && let Some(version) = grok.client_version
+ {
+ return version;
+ }
+ "0.2.93".to_string()
+}
+
pub fn is_verbose() -> bool {
log_verbose()
}
diff --git a/src/main.rs b/src/main.rs
index dbf14cb..ef4b703 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -52,6 +52,10 @@ enum Commands {
#[command(subcommand)]
command: ProviderGroup,
},
+ Grok {
+ #[command(subcommand)]
+ command: ProviderGroup,
+ },
}
#[derive(Debug, Subcommand)]
@@ -130,6 +134,7 @@ fn main() -> Result<()> {
Commands::Codex { command } => run_provider_cli("codex", command),
Commands::Kimi { command } => run_provider_cli("kimi", command),
Commands::Cursor { command } => run_provider_cli("cursor", command),
+ Commands::Grok { command } => run_provider_cli("grok", command),
}
}
@@ -189,7 +194,7 @@ fn run_provider_cli(name: &str, command: ProviderGroup) -> Result<()> {
fn print_models(registry: &Registry, full: bool) {
let grouped = registry.grouped_models();
- for provider in ["codex", "kimi", "cursor"] {
+ for provider in ["codex", "kimi", "grok", "cursor"] {
let Some(models) = grouped.get(provider) else {
continue;
};
diff --git a/src/providers/grok/auth/login.rs b/src/providers/grok/auth/login.rs
new file mode 100644
index 0000000..3d641d0
--- /dev/null
+++ b/src/providers/grok/auth/login.rs
@@ -0,0 +1,353 @@
+use std::collections::HashMap;
+use std::io::{Read, Write};
+use std::net::{TcpListener, TcpStream};
+use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
+
+use serde::Deserialize;
+use url::Url;
+
+use super::pkce::{PkceCodes, generate_pkce, generate_state};
+use super::token_store::{GrokTokenStore, StoredAuth};
+use crate::auth::AuthStorage;
+
+pub const CANONICAL_ISSUER: &str = "https://auth.x.ai";
+pub const CLIENT_ID: &str = "b1a00492-073a-47ea-816f-4c329264a828";
+const SCOPES: &str = "openid profile email offline_access grok-cli:access api:access conversations:read conversations:write";
+const LOGIN_TIMEOUT: Duration = Duration::from_secs(300);
+const MAX_METADATA_BYTES: usize = 256 * 1024;
+
+#[derive(Debug, Deserialize)]
+struct Discovery {
+ issuer: String,
+ authorization_endpoint: String,
+ token_endpoint: String,
+ response_types_supported: Vec<String>,
+ grant_types_supported: Vec<String>,
+ code_challenge_methods_supported: Vec<String>,
+ token_endpoint_auth_methods_supported: Vec<String>,
+}
+
+#[derive(Deserialize)]
+struct TokenResponse {
+ access_token: String,
+ refresh_token: Option<String>,
+ expires_in: u64,
+ token_type: Option<String>,
+}
+
+pub fn login<S: AuthStorage<StoredAuth>>(store: &GrokTokenStore<S>) -> anyhow::Result<()> {
+ let client = client()?;
+ let discovery = discover(&client)?;
+ let listener = TcpListener::bind("127.0.0.1:0")?;
+ listener.set_nonblocking(true)?;
+ let port = listener.local_addr()?.port();
+ let redirect_uri = format!("http://127.0.0.1:{port}/callback");
+ let pkce = generate_pkce();
+ let state = generate_state();
+ let auth_url = authorize_url(&discovery, &redirect_uri, &pkce, &state)?;
+
+ println!("Open this URL in your browser to authorize:\n\n {auth_url}\n");
+ open_browser(&auth_url);
+ let code = wait_for_callback(&listener, &state, LOGIN_TIMEOUT)?;
+ let tokens = exchange_code(&client, &discovery, &code, &pkce, &redirect_uri)?;
+ validate_tokens(&tokens)?;
+ let refresh = tokens
+ .refresh_token
+ .as_ref()
+ .filter(|value| !value.is_empty())
+ .cloned()
+ .ok_or_else(|| anyhow::anyhow!("Grok login did not grant an offline session"))?;
+ store.save_auth(StoredAuth {
+ access: tokens.access_token,
+ refresh,
+ expires_at_ms: now_ms().saturating_add(tokens.expires_in.saturating_mul(1000)),
+ issuer: CANONICAL_ISSUER.into(),
+ client_id: CLIENT_ID.into(),
+ })?;
+ Ok(())
+}
+
+fn client() -> anyhow::Result<reqwest::blocking::Client> {
+ Ok(reqwest::blocking::Client::builder()
+ .redirect(reqwest::redirect::Policy::none())
+ .connect_timeout(Duration::from_secs(10))
+ .timeout(Duration::from_secs(20))
+ .build()?)
+}
+
+fn discover(client: &reqwest::blocking::Client) -> anyhow::Result<Discovery> {
+ let url = format!("{CANONICAL_ISSUER}/.well-known/openid-configuration");
+ let response = client.get(url).send()?.error_for_status()?;
+ if response
+ .content_length()
+ .is_some_and(|size| size as usize > MAX_METADATA_BYTES)
+ {
+ anyhow::bail!("Grok OIDC metadata exceeds the size limit");
+ }
+ let bytes = response.bytes()?;
+ if bytes.len() > MAX_METADATA_BYTES {
+ anyhow::bail!("Grok OIDC metadata exceeds the size limit");
+ }
+ let discovery: Discovery = serde_json::from_slice(&bytes)?;
+ validate_discovery(&discovery)?;
+ Ok(discovery)
+}
+
+fn validate_discovery(discovery: &Discovery) -> anyhow::Result<()> {
+ if discovery.issuer != CANONICAL_ISSUER {
+ anyhow::bail!("OIDC discovery issuer mismatch");
+ }
+ let issuer = Url::parse(CANONICAL_ISSUER)?;
+ for endpoint in [&discovery.authorization_endpoint, &discovery.token_endpoint] {
+ let endpoint = Url::parse(endpoint)?;
+ if endpoint.scheme() != "https" || endpoint.origin() != issuer.origin() {
+ anyhow::bail!("OIDC endpoint is outside the canonical issuer");
+ }
+ }
+ if !discovery
+ .response_types_supported
+ .iter()
+ .any(|v| v == "code")
+ || !discovery
+ .grant_types_supported
+ .iter()
+ .any(|v| v == "authorization_code")
+ || !discovery
+ .grant_types_supported
+ .iter()
+ .any(|v| v == "refresh_token")
+ || !discovery
+ .code_challenge_methods_supported
+ .iter()
+ .any(|v| v == "S256")
+ || !discovery
+ .token_endpoint_auth_methods_supported
+ .iter()
+ .any(|v| v == "none")
+ {
+ anyhow::bail!("Grok OIDC provider lacks required OAuth capabilities");
+ }
+ Ok(())
+}
+
+fn authorize_url(
+ discovery: &Discovery,
+ redirect_uri: &str,
+ pkce: &PkceCodes,
+ state: &str,
+) -> anyhow::Result<String> {
+ let mut url = Url::parse(&discovery.authorization_endpoint)?;
+ url.query_pairs_mut()
+ .append_pair("response_type", "code")
+ .append_pair("client_id", CLIENT_ID)
+ .append_pair("redirect_uri", redirect_uri)
+ .append_pair("scope", SCOPES)
+ .append_pair("code_challenge", &pkce.challenge)
+ .append_pair("code_challenge_method", "S256")
+ .append_pair("state", state);
+ Ok(url.into())
+}
+
+fn exchange_code(
+ client: &reqwest::blocking::Client,
+ discovery: &Discovery,
+ code: &str,
+ pkce: &PkceCodes,
+ redirect_uri: &str,
+) -> anyhow::Result<TokenResponse> {
+ let response = client
+ .post(&discovery.token_endpoint)
+ .form(&[
+ ("grant_type", "authorization_code"),
+ ("client_id", CLIENT_ID),
+ ("code", code),
+ ("code_verifier", pkce.verifier.as_str()),
+ ("redirect_uri", redirect_uri),
+ ])
+ .send()?;
+ if !response.status().is_success() {
+ anyhow::bail!(
+ "Grok token exchange failed with status {}",
+ response.status()
+ );
+ }
+ let tokens: TokenResponse = response.json()?;
+ validate_tokens(&tokens)?;
+ Ok(tokens)
+}
+
+fn validate_tokens(tokens: &TokenResponse) -> anyhow::Result<()> {
+ if tokens.access_token.is_empty()
+ || tokens.expires_in == 0
+ || tokens
+ .token_type
+ .as_deref()
+ .is_some_and(|value| !value.eq_ignore_ascii_case("bearer"))
+ {
+ anyhow::bail!("Grok token response is invalid");
+ }
+ Ok(())
+}
+
+fn wait_for_callback(
+ listener: &TcpListener,
+ state: &str,
+ timeout: Duration,
+) -> anyhow::Result<String> {
+ let deadline = Instant::now() + timeout;
+ loop {
+ if Instant::now() >= deadline {
+ anyhow::bail!("Grok OAuth login timed out");
+ }
+ match listener.accept() {
+ Ok((mut stream, _)) => match callback(&mut stream, state) {
+ Callback::Code(code) => return Ok(code),
+ Callback::Terminal(error) => anyhow::bail!(error),
+ Callback::Ignore => {}
+ },
+ Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
+ std::thread::sleep(Duration::from_millis(100));
+ }
+ Err(error) => return Err(error.into()),
+ }
+ }
+}
+
+enum Callback {
+ Code(String),
+ Terminal(String),
+ Ignore,
+}
+
+fn callback(stream: &mut TcpStream, expected_state: &str) -> Callback {
+ let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
+ let mut buffer = [0_u8; 8192];
+ let Ok(length) = stream.read(&mut buffer) else {
+ return Callback::Ignore;
+ };
+ let request = String::from_utf8_lossy(&buffer[..length]);
+ let Some(target) = request.lines().next().and_then(|line| {
+ let mut parts = line.split_whitespace();
+ (parts.next() == Some("GET"))
+ .then(|| parts.next())
+ .flatten()
+ }) else {
+ return Callback::Ignore;
+ };
+ let Ok(url) = Url::parse(&format!("http://127.0.0.1{target}")) else {
+ return Callback::Ignore;
+ };
+ if url.path() != "/callback" {
+ respond(stream, "404 Not Found", "Not found");
+ return Callback::Ignore;
+ }
+ let params: HashMap<_, _> = url.query_pairs().into_owned().collect();
+ if params.contains_key("error") {
+ respond(stream, "400 Bad Request", "Authorization failed");
+ return Callback::Terminal("Grok authorization was denied".into());
+ }
+ let valid_state = params
+ .get("state")
+ .is_some_and(|value| constant_time_eq(value, expected_state));
+ let code = params.get("code").filter(|value| !value.is_empty());
+ if !valid_state || code.is_none() {
+ respond(stream, "400 Bad Request", "Invalid authorization callback");
+ return Callback::Terminal("Grok authorization callback is invalid".into());
+ }
+ respond(
+ stream,
+ "200 OK",
+ "Authorization received. Return to the terminal.",
+ );
+ Callback::Code(code.unwrap().clone())
+}
+
+fn constant_time_eq(left: &str, right: &str) -> bool {
+ if left.len() != right.len() {
+ return false;
+ }
+ left.bytes()
+ .zip(right.bytes())
+ .fold(0_u8, |diff, (a, b)| diff | (a ^ b))
+ == 0
+}
+
+fn respond(stream: &mut TcpStream, status: &str, body: &str) {
+ let response = format!(
+ "HTTP/1.1 {status}\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
+ body.len()
+ );
+ let _ = stream.write_all(response.as_bytes());
+}
+
+fn open_browser(url: &str) {
+ #[cfg(target_os = "macos")]
+ let command = ("open", vec![url]);
+ #[cfg(target_os = "linux")]
+ let command = ("xdg-open", vec![url]);
+ #[cfg(target_os = "windows")]
+ let command = ("cmd", vec!["/C", "start", "", url]);
+ #[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
+ {
+ let _ = std::process::Command::new(command.0)
+ .args(command.1)
+ .spawn();
+ }
+}
+
+fn now_ms() -> u64 {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_millis() as u64
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn metadata() -> Discovery {
+ Discovery {
+ issuer: CANONICAL_ISSUER.into(),
+ authorization_endpoint: "https://auth.x.ai/oauth2/authorize".into(),
+ token_endpoint: "https://auth.x.ai/oauth2/token".into(),
+ response_types_supported: vec!["code".into()],
+ grant_types_supported: vec!["authorization_code".into(), "refresh_token".into()],
+ code_challenge_methods_supported: vec!["S256".into()],
+ token_endpoint_auth_methods_supported: vec!["none".into()],
+ }
+ }
+
+ #[test]
+ fn authorization_url_has_exact_public_client_contract() {
+ let url = authorize_url(
+ &metadata(),
+ "http://127.0.0.1:1234/callback",
+ &generate_pkce(),
+ "state",
+ )
+ .unwrap();
+ let url = Url::parse(&url).unwrap();
+ let params: HashMap<_, _> = url.query_pairs().into_owned().collect();
+ assert_eq!(params.get("client_id").unwrap(), CLIENT_ID);
+ assert_eq!(params.get("scope").unwrap(), SCOPES);
+ assert_eq!(
+ params.get("redirect_uri").unwrap(),
+ "http://127.0.0.1:1234/callback"
+ );
+ assert!(!params.contains_key("client_secret"));
+ }
+
+ #[test]
+ fn discovery_rejects_cross_origin_endpoint() {
+ let mut value = metadata();
+ value.token_endpoint = "https://example.com/token".into();
+ assert!(validate_discovery(&value).is_err());
+ }
+
+ #[test]
+ fn callback_validates_state_and_path() {
+ assert!(constant_time_eq("state", "state"));
+ assert!(!constant_time_eq("state", "other"));
+ }
+}
diff --git a/src/providers/grok/auth/manager.rs b/src/providers/grok/auth/manager.rs
new file mode 100644
index 0000000..7e9c847
--- /dev/null
+++ b/src/providers/grok/auth/manager.rs
@@ -0,0 +1,180 @@
+use std::sync::Arc;
+use std::time::{Duration, SystemTime, UNIX_EPOCH};
+
+use serde::Deserialize;
+use tokio::sync::Mutex;
+use url::Url;
+
+use super::login::{CANONICAL_ISSUER, CLIENT_ID};
+use super::token_store::{GrokTokenStore, StoredAuth};
+use crate::auth::AuthStorage;
+
+const REFRESH_SKEW_MS: u64 = 5 * 60 * 1000;
+
+#[derive(Deserialize)]
+struct Discovery {
+ issuer: String,
+ token_endpoint: String,
+}
+
+#[derive(Deserialize)]
+struct RefreshResponse {
+ access_token: String,
+ expires_in: u64,
+ #[serde(default)]
+ refresh_token: Option<String>,
+}
+
+pub struct GrokAuthManager<S: AuthStorage<StoredAuth>> {
+ store: GrokTokenStore<S>,
+ client: reqwest::Client,
+ refresh_lock: Arc<Mutex<()>>,
+}
+
+impl<S: AuthStorage<StoredAuth>> GrokAuthManager<S> {
+ pub fn new(store: GrokTokenStore<S>) -> anyhow::Result<Self> {
+ let client = reqwest::Client::builder()
+ .redirect(reqwest::redirect::Policy::none())
+ .connect_timeout(Duration::from_secs(10))
+ .timeout(Duration::from_secs(20))
+ .build()?;
+ Ok(Self {
+ store,
+ client,
+ refresh_lock: Arc::new(Mutex::new(())),
+ })
+ }
+
+ pub fn store(&self) -> &GrokTokenStore<S> {
+ &self.store
+ }
+
+ pub async fn get_auth(&self) -> anyhow::Result<StoredAuth> {
+ let auth = self
+ .store
+ .load_auth()?
+ .ok_or_else(|| anyhow::anyhow!("Not authenticated"))?;
+ if auth.expires_at_ms > now_ms().saturating_add(REFRESH_SKEW_MS) {
+ return Ok(auth);
+ }
+ self.refresh(false, None).await
+ }
+
+ pub async fn force_refresh(&self, rejected_access: &str) -> anyhow::Result<StoredAuth> {
+ self.refresh(true, Some(rejected_access)).await
+ }
+
+ async fn refresh(
+ &self,
+ force: bool,
+ rejected_access: Option<&str>,
+ ) -> anyhow::Result<StoredAuth> {
+ let _guard = self.refresh_lock.lock().await;
+ let auth = self
+ .store
+ .load_auth()?
+ .ok_or_else(|| anyhow::anyhow!("Not authenticated"))?;
+ if (!force && auth.expires_at_ms > now_ms().saturating_add(REFRESH_SKEW_MS))
+ || rejected_access.is_some_and(|access| auth.access != access)
+ {
+ return Ok(auth);
+ }
+ if auth.issuer != CANONICAL_ISSUER || auth.client_id != CLIENT_ID {
+ anyhow::bail!("Unsupported Grok OAuth session");
+ }
+ let issuer = Url::parse(CANONICAL_ISSUER)?;
+ let discovery_url = issuer.join("/.well-known/openid-configuration")?;
+ let discovery: Discovery = self
+ .client
+ .get(discovery_url)
+ .send()
+ .await?
+ .error_for_status()?
+ .json()
+ .await?;
+ if discovery.issuer != CANONICAL_ISSUER {
+ anyhow::bail!("OIDC discovery issuer mismatch");
+ }
+ let endpoint = Url::parse(&discovery.token_endpoint)?;
+ if endpoint.scheme() != "https" || endpoint.origin() != issuer.origin() {
+ anyhow::bail!("OIDC token endpoint is outside the canonical issuer");
+ }
+ let refreshed: RefreshResponse = self
+ .client
+ .post(endpoint)
+ .form(&[
+ ("grant_type", "refresh_token"),
+ ("refresh_token", auth.refresh.as_str()),
+ ("client_id", auth.client_id.as_str()),
+ ])
+ .send()
+ .await?
+ .error_for_status()?
+ .json()
+ .await?;
+ if refreshed.access_token.is_empty() || refreshed.expires_in == 0 {
+ anyhow::bail!("Invalid token refresh response");
+ }
+ let updated = StoredAuth {
+ access: refreshed.access_token,
+ refresh: refreshed
+ .refresh_token
+ .filter(|token| !token.is_empty())
+ .unwrap_or(auth.refresh),
+ expires_at_ms: now_ms().saturating_add(refreshed.expires_in.saturating_mul(1000)),
+ issuer: auth.issuer,
+ client_id: auth.client_id,
+ };
+ self.store.save_auth(updated.clone())?;
+ Ok(updated)
+ }
+}
+
+fn now_ms() -> u64 {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_millis() as u64
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::auth::InMemoryAuthStore;
+
+ fn auth(access: &str) -> StoredAuth {
+ StoredAuth {
+ access: access.into(),
+ refresh: "synthetic-refresh".into(),
+ expires_at_ms: now_ms().saturating_add(3_600_000),
+ issuer: CANONICAL_ISSUER.into(),
+ client_id: "synthetic-client".into(),
+ }
+ }
+
+ #[test]
+ fn discovery_accepts_standard_metadata_fields() {
+ let discovery: Discovery = serde_json::from_value(serde_json::json!({
+ "issuer": CANONICAL_ISSUER,
+ "token_endpoint": "https://auth.x.ai/oauth/token",
+ "authorization_endpoint": "https://auth.x.ai/oauth/authorize",
+ "jwks_uri": "https://auth.x.ai/.well-known/jwks.json"
+ }))
+ .unwrap();
+
+ assert_eq!(discovery.issuer, CANONICAL_ISSUER);
+ }
+
+ #[tokio::test]
+ async fn concurrent_stale_401_refreshes_reuse_the_rotated_access_token() {
+ let store = GrokTokenStore::new(InMemoryAuthStore::new());
+ store.save_auth(auth("rotated-access")).unwrap();
+ let manager = Arc::new(GrokAuthManager::new(store).unwrap());
+ let (first, second) = tokio::join!(
+ manager.force_refresh("rejected-access"),
+ manager.force_refresh("rejected-access"),
+ );
+ assert_eq!(first.unwrap().access, "rotated-access");
+ assert_eq!(second.unwrap().access, "rotated-access");
+ }
+}
diff --git a/src/providers/grok/auth/mod.rs b/src/providers/grok/auth/mod.rs
new file mode 100644
index 0000000..25ffb54
--- /dev/null
+++ b/src/providers/grok/auth/mod.rs
@@ -0,0 +1,4 @@
+pub mod login;
+pub mod manager;
+pub mod pkce;
+pub mod token_store;
diff --git a/src/providers/grok/auth/pkce.rs b/src/providers/grok/auth/pkce.rs
new file mode 100644
index 0000000..a41a677
--- /dev/null
+++ b/src/providers/grok/auth/pkce.rs
@@ -0,0 +1,52 @@
+use base64::Engine;
+use rand::RngCore;
+use sha2::{Digest, Sha256};
+
+#[derive(Debug, Clone)]
+pub struct PkceCodes {
+ pub verifier: String,
+ pub challenge: String,
+}
+
+fn encode(bytes: &[u8]) -> String {
+ base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
+}
+
+pub fn generate_pkce() -> PkceCodes {
+ let mut bytes = [0_u8; 32];
+ rand::thread_rng().fill_bytes(&mut bytes);
+ let verifier = encode(&bytes);
+ let challenge = encode(&Sha256::digest(verifier.as_bytes()));
+ PkceCodes {
+ verifier,
+ challenge,
+ }
+}
+
+pub fn generate_state() -> String {
+ let mut bytes = [0_u8; 32];
+ rand::thread_rng().fill_bytes(&mut bytes);
+ encode(&bytes)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn pkce_uses_s256() {
+ let pkce = generate_pkce();
+ assert_eq!(pkce.verifier.len(), 43);
+ assert_eq!(
+ pkce.challenge,
+ encode(&Sha256::digest(pkce.verifier.as_bytes()))
+ );
+ }
+
+ #[test]
+ fn state_is_url_safe() {
+ let state = generate_state();
+ assert_eq!(state.len(), 43);
+ assert!(!state.contains(['+', '/', '=']));
+ }
+}
diff --git a/src/providers/grok/auth/token_store.rs b/src/providers/grok/auth/token_store.rs
new file mode 100644
index 0000000..70fda05
--- /dev/null
+++ b/src/providers/grok/auth/token_store.rs
@@ -0,0 +1,45 @@
+use serde::{Deserialize, Serialize};
+
+use crate::auth::{AuthStorage, FileAuthStore};
+use crate::paths;
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+pub struct StoredAuth {
+ pub access: String,
+ pub refresh: String,
+ pub expires_at_ms: u64,
+ pub issuer: String,
+ pub client_id: String,
+}
+
+pub struct GrokTokenStore<S: AuthStorage<StoredAuth>> {
+ store: S,
+}
+
+impl<S: AuthStorage<StoredAuth>> GrokTokenStore<S> {
+ pub fn new(store: S) -> Self {
+ Self { store }
+ }
+ pub fn load_auth(&self) -> anyhow::Result<Option<StoredAuth>> {
+ self.store.load()
+ }
+ pub fn save_auth(&self, auth: StoredAuth) -> anyhow::Result<()> {
+ self.store.save(auth)
+ }
+ pub fn clear_auth(&self) -> anyhow::Result<()> {
+ self.store.clear()
+ }
+ pub fn auth_path(&self) -> String {
+ self.store.path()
+ }
+}
+
+pub fn file_store() -> GrokTokenStore<FileAuthStore<StoredAuth>> {
+ let primary = paths::provider_auth_file("grok");
+ let legacy = paths::provider_legacy_auth_file("grok");
+ GrokTokenStore::new(FileAuthStore::new(
+ primary.to_string_lossy().into_owned(),
+ legacy.to_string_lossy().into_owned(),
+ ))
+}
diff --git a/src/providers/grok/client.rs b/src/providers/grok/client.rs
new file mode 100644
index 0000000..e0d17f0
--- /dev/null
+++ b/src/providers/grok/client.rs
@@ -0,0 +1,199 @@
+use std::sync::Arc;
+use std::time::Duration;
+
+use futures_util::StreamExt;
+use http::StatusCode;
+
+use super::auth::manager::GrokAuthManager;
+use super::auth::token_store::{StoredAuth, file_store};
+use super::translate::request::GrokResponsesRequest;
+
+const DEFAULT_BASE_URL: &str = "https://cli-chat-proxy.grok.com/v1";
+const MAX_BUFFERED_RESPONSE_BYTES: usize = 8 * 1024 * 1024;
+
+pub struct GrokClient {
+ client: Arc<reqwest::Client>,
+ auth: Arc<GrokAuthManager<crate::auth::FileAuthStore<StoredAuth>>>,
+ url: String,
+ client_version: String,
+}
+
+pub struct GrokResponse {
+ response: reqwest::Response,
+}
+pub struct GrokError {
+ pub status: StatusCode,
+ pub retry_after: Option<String>,
+ pub message: String,
+}
+
+impl GrokResponse {
+ pub fn into_response(self) -> reqwest::Response {
+ self.response
+ }
+
+ pub fn into_stream(
+ self,
+ ) -> impl futures_util::Stream<Item = Result<bytes::Bytes, GrokError>> + Send {
+ self.response.bytes_stream().map(|chunk| {
+ chunk.map_err(|_| GrokError {
+ status: StatusCode::BAD_GATEWAY,
+ retry_after: None,
+ message: "Grok upstream stream failed".into(),
+ })
+ })
+ }
+
+ pub async fn into_bytes(self) -> Result<Vec<u8>, GrokError> {
+ let mut stream = self.into_stream();
+ let mut bytes = Vec::new();
+ while let Some(chunk) = stream.next().await {
+ let chunk = chunk?;
+ if bytes.len().saturating_add(chunk.len()) > MAX_BUFFERED_RESPONSE_BYTES {
+ return Err(GrokError {
+ status: StatusCode::BAD_GATEWAY,
+ retry_after: None,
+ message: "Grok upstream response exceeds the size limit".into(),
+ });
+ }
+ bytes.extend_from_slice(&chunk);
+ }
+ Ok(bytes)
+ }
+}
+
+impl GrokClient {
+ pub fn new(base_url: String, client_version: String) -> anyhow::Result<Self> {
+ let client = Arc::new(
+ reqwest::Client::builder()
+ .redirect(reqwest::redirect::Policy::none())
+ .connect_timeout(Duration::from_secs(10))
+ .timeout(Duration::from_secs(120))
+ .build()?,
+ );
+ let auth = Arc::new(GrokAuthManager::new(file_store())?);
+ Ok(Self::with_shared(
+ url_for(base_url)?,
+ client_version,
+ client,
+ auth,
+ ))
+ }
+
+ fn with_shared(
+ url: String,
+ client_version: String,
+ client: Arc<reqwest::Client>,
+ auth: Arc<GrokAuthManager<crate::auth::FileAuthStore<StoredAuth>>>,
+ ) -> Self {
+ Self {
+ client,
+ auth,
+ url,
+ client_version,
+ }
+ }
+
+ pub async fn post(&self, body: &GrokResponsesRequest) -> Result<GrokResponse, GrokError> {
+ let auth = self.auth.get_auth().await.map_err(auth_error)?;
+ let response = self.attempt(&auth.access, body).await?;
+ if response.status() == StatusCode::UNAUTHORIZED {
+ let refreshed = self
+ .auth
+ .force_refresh(&auth.access)
+ .await
+ .map_err(auth_error)?;
+ let replay = self.attempt(&refreshed.access, body).await?;
+ if replay.status() == StatusCode::UNAUTHORIZED {
+ return Err(auth_error(anyhow::anyhow!("unauthorized")));
+ }
+ return Ok(GrokResponse { response: replay });
+ }
+ Ok(GrokResponse { response })
+ }
+
+ async fn attempt(
+ &self,
+ access: &str,
+ body: &GrokResponsesRequest,
+ ) -> Result<reqwest::Response, GrokError> {
+ let response = self
+ .client
+ .post(&self.url)
+ .header("accept", "text/event-stream")
+ .header("content-type", "application/json")
+ .header("authorization", format!("Bearer {access}"))
+ .header("x-xai-token-auth", "xai-grok-cli")
+ .header("x-grok-client-identifier", "grok-shell")
+ .header("x-grok-client-version", &self.client_version)
+ .json(body)
+ .send()
+ .await
+ .map_err(|_| GrokError {
+ status: StatusCode::BAD_GATEWAY,
+ retry_after: None,
+ message: "Grok upstream request failed".into(),
+ })?;
+ let status = response.status();
+ if !status.is_success() && status != StatusCode::UNAUTHORIZED {
+ return Err(GrokError {
+ status,
+ retry_after: response
+ .headers()
+ .get("retry-after")
+ .and_then(|v| v.to_str().ok())
+ .map(str::to_string),
+ message: "Grok upstream rejected the request".into(),
+ });
+ }
+ Ok(response)
+ }
+}
+
+fn url_for(base_url: String) -> anyhow::Result<String> {
+ responses_url(&base_url)
+}
+fn responses_url(base_url: &str) -> anyhow::Result<String> {
+ let base_url = if base_url.trim().is_empty() {
+ DEFAULT_BASE_URL
+ } else {
+ base_url.trim()
+ };
+ let mut url = reqwest::Url::parse(base_url)?;
+ let path = url.path().trim_end_matches('/');
+ if !path.ends_with("/responses") {
+ url.set_path(&format!("{path}/responses"));
+ }
+ Ok(url.to_string().trim_end_matches('/').to_string())
+}
+
+fn auth_error(_: anyhow::Error) -> GrokError {
+ GrokError {
+ status: StatusCode::UNAUTHORIZED,
+ retry_after: None,
+ message: "Grok authentication requires official CLI login and proxy import".into(),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::responses_url;
+ #[test]
+ fn responses_url_appends_responses_to_base_path() {
+ assert_eq!(
+ responses_url("http://127.0.0.1:8080/v1").unwrap(),
+ "http://127.0.0.1:8080/v1/responses"
+ );
+ }
+ #[test]
+ fn responses_url_preserves_responses_endpoint() {
+ assert_eq!(
+ responses_url("https://example.com/custom/responses/").unwrap(),
+ "https://example.com/custom/responses"
+ );
+ }
+ #[test]
+ fn responses_url_rejects_invalid_url() {
+ assert!(responses_url(":invalid").is_err());
+ }
+}
diff --git a/src/providers/grok/count_tokens.rs b/src/providers/grok/count_tokens.rs
new file mode 100644
index 0000000..b456e6e
--- /dev/null
+++ b/src/providers/grok/count_tokens.rs
@@ -0,0 +1,124 @@
+use super::translate::request::{GrokContentPart, GrokInputItem, GrokResponsesRequest};
+
+const MESSAGE_OVERHEAD_TOKENS: u64 = 4;
+const TOOL_OVERHEAD_TOKENS: u64 = 4;
+
+pub fn count_tokens(request: &GrokResponsesRequest) -> u64 {
+ let instructions = request
+ .instructions
+ .as_deref()
+ .map(approx_token_count)
+ .unwrap_or(0);
+ let input: u64 = request.input.iter().map(count_input_item).sum();
+ let tools: u64 = request
+ .tools
+ .as_deref()
+ .unwrap_or_default()
+ .iter()
+ .map(|tool| {
+ tool.name.as_deref().map(approx_token_count).unwrap_or(0)
+ + tool
+ .description
+ .as_deref()
+ .map(approx_token_count)
+ .unwrap_or(0)
+ + approx_token_count(&serde_json::to_string(&tool.parameters).unwrap_or_default())
+ + TOOL_OVERHEAD_TOKENS
+ })
+ .sum();
+
+ (instructions
+ + input
+ + tools
+ + request.input.len() as u64 * MESSAGE_OVERHEAD_TOKENS
+ + approx_token_count(&request.model))
+ .max(1)
+}
+
+fn count_input_item(item: &GrokInputItem) -> u64 {
+ match item {
+ GrokInputItem::Message { content, .. } => content
+ .iter()
+ .map(|part| match part {
+ GrokContentPart::InputText { text } | GrokContentPart::OutputText { text } => {
+ approx_token_count(text)
+ }
+ })
+ .sum(),
+ GrokInputItem::FunctionCall {
+ name, arguments, ..
+ } => approx_token_count(name) + approx_token_count(arguments),
+ GrokInputItem::FunctionCallOutput { output, .. } => approx_token_count(output),
+ }
+}
+
+fn approx_token_count(text: &str) -> u64 {
+ if text.is_empty() {
+ return 0;
+ }
+ let mut count = 0;
+ let mut in_word = false;
+ for character in text.chars() {
+ if character.is_alphanumeric() || character == '-' || character == '_' {
+ if !in_word {
+ count += 1;
+ in_word = true;
+ }
+ } else {
+ in_word = false;
+ if !character.is_whitespace() {
+ count += 1;
+ }
+ }
+ }
+ count.max(1)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::anthropic::schema::MessagesRequest;
+ use crate::providers::grok::translate::request::translate_request;
+ use serde_json::json;
+
+ fn translated_request(value: serde_json::Value) -> GrokResponsesRequest {
+ let request: MessagesRequest = serde_json::from_value(value).unwrap();
+ translate_request(&request, "grok-4.5".into()).unwrap()
+ }
+
+ #[test]
+ fn count_tokens_returns_a_positive_count() {
+ let request = translated_request(json!({
+ "model": "grok-4.5",
+ "messages": [{"role": "user", "content": "hello"}]
+ }));
+
+ assert!(count_tokens(&request) > 0);
+ }
+
+ #[test]
+ fn count_tokens_increases_for_more_input() {
+ let short = translated_request(json!({
+ "model": "grok-4.5",
+ "messages": [{"role": "user", "content": "hello"}]
+ }));
+ let long = translated_request(json!({
+ "model": "grok-4.5",
+ "system": "Follow all instructions carefully.",
+ "messages": [{"role": "user", "content": "hello, please explain this request in detail"}],
+ "tools": [{"name": "lookup", "description": "Look up a record", "input_schema": {"type": "object"}}]
+ }));
+
+ assert!(count_tokens(&long) > count_tokens(&short));
+ }
+
+ #[test]
+ fn count_tokens_is_deterministic() {
+ let request = translated_request(json!({
+ "model": "grok-4.5",
+ "messages": [{"role": "user", "content": "repeatable input"}]
+ }));
+
+ assert_eq!(count_tokens(&request), count_tokens(&request));
+ }
+}
diff --git a/src/providers/grok/mod.rs b/src/providers/grok/mod.rs
new file mode 100644
index 0000000..38da460
--- /dev/null
+++ b/src/providers/grok/mod.rs
@@ -0,0 +1,503 @@
+pub mod auth;
+pub mod client;
+pub mod count_tokens;
+pub mod translate;
+
+use std::convert::Infallible;
+use std::sync::Arc;
+use std::time::{SystemTime, UNIX_EPOCH};
+
+use async_trait::async_trait;
+use axum::{
+ Json,
+ body::Body,
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+use bytes::Bytes;
+use futures_util::{Stream, StreamExt};
+
+use crate::anthropic::{
+ error::json_error,
+ schema::{CountTokensResponse, MessagesRequest},
+};
+use crate::monitor::MonitorHandle;
+use crate::provider::{CliHandlers, Provider, RequestContext};
+use crate::registry::GROK_MODELS;
+
+use self::auth::token_store::file_store;
+use self::translate::{
+ accumulate::accumulate_response,
+ model_allowlist::{assert_allowed_model, resolve_model},
+ request::translate_request,
+ stream::{SseDecoder, StreamTranslator, stream_error},
+};
+
+pub struct GrokProvider {
+ client: Arc<client::GrokClient>,
+}
+impl GrokProvider {
+ pub fn new() -> Self {
+ Self {
+ client: Arc::new(
+ client::GrokClient::new(
+ crate::config::grok_base_url(),
+ crate::config::grok_client_version(),
+ )
+ .expect("Grok transport is unavailable"),
+ ),
+ }
+ }
+
+ pub fn with_client(client: client::GrokClient) -> Self {
+ Self {
+ client: Arc::new(client),
+ }
+ }
+}
+impl Default for GrokProvider {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+#[async_trait]
+impl Provider for GrokProvider {
+ fn name(&self) -> &'static str {
+ "grok"
+ }
+ fn supported_models(&self) -> Vec<String> {
+ GROK_MODELS
+ .iter()
+ .map(|model| (*model).to_string())
+ .collect()
+ }
+ fn cli(&self) -> &'static dyn CliHandlers {
+ &GROK_CLI
+ }
+ async fn handle_messages(&self, body: MessagesRequest, ctx: RequestContext) -> Response {
+ let requested = body.model.clone().unwrap_or_else(|| "grok-4.5".into());
+ let resolved = resolve_model(&requested);
+ if let Err(error) = assert_allowed_model(&resolved) {
+ return json_error(
+ StatusCode::BAD_REQUEST,
+ "invalid_request_error",
+ error.to_string(),
+ );
+ }
+ let translated = match translate_request(&body, resolved.clone()) {
+ Ok(value) => value,
+ Err(error) => {
+ return json_error(
+ StatusCode::BAD_REQUEST,
+ "invalid_request_error",
+ error.to_string(),
+ );
+ }
+ };
+ if let Some(monitor) = &ctx.monitor {
+ monitor.model_resolved(&ctx.req_id, &resolved);
+ monitor.upstream_started(&ctx.req_id);
+ }
+ let upstream = match self.client.post(&translated).await {
+ Ok(response) => response,
+ Err(error) => return map_error(error),
+ };
+ if body.stream {
+ stream_response(
+ upstream,
+ format!("msg_{}", uuid::Uuid::new_v4().simple()),
+ requested,
+ ctx.monitor.clone(),
+ ctx.req_id.clone(),
+ )
+ } else {
+ let upstream_bytes = match upstream.into_bytes().await {
+ Ok(bytes) => bytes,
+ Err(error) => return map_error(error),
+ };
+ match accumulate_response(
+ &upstream_bytes,
+ &format!("msg_{}", uuid::Uuid::new_v4().simple()),
+ &requested,
+ ) {
+ Ok(value) => {
+ if let Some(monitor) = ctx.monitor.as_ref() {
+ monitor.usage_updated(
+ &ctx.req_id,
+ value
+ .pointer("/usage/input_tokens")
+ .and_then(|v| v.as_u64()),
+ value
+ .pointer("/usage/output_tokens")
+ .and_then(|v| v.as_u64()),
+ );
+ }
+ (StatusCode::OK, Json(value)).into_response()
+ }
+ Err(_) => json_error(
+ StatusCode::BAD_GATEWAY,
+ "api_error",
+ "Grok response is invalid",
+ ),
+ }
+ }
+ }
+ async fn handle_count_tokens(&self, body: MessagesRequest, ctx: RequestContext) -> Response {
+ let requested = body.model.clone().unwrap_or_else(|| "grok-4.5".into());
+ let resolved = resolve_model(&requested);
+ if let Err(error) = assert_allowed_model(&resolved) {
+ return json_error(
+ StatusCode::BAD_REQUEST,
+ "invalid_request_error",
+ error.to_string(),
+ );
+ }
+ let translated = match translate_request(&body, resolved) {
+ Ok(value) => value,
+ Err(error) => {
+ return json_error(
+ StatusCode::BAD_REQUEST,
+ "invalid_request_error",
+ error.to_string(),
+ );
+ }
+ };
+ let tokens = count_tokens::count_tokens(&translated);
+ if let Some(monitor) = ctx.monitor.as_ref() {
+ monitor.usage_updated(&ctx.req_id, Some(tokens), None);
+ }
+ (
+ StatusCode::OK,
+ Json(CountTokensResponse {
+ input_tokens: tokens,
+ }),
+ )
+ .into_response()
+ }
+}
+
+fn stream_response(
+ response: client::GrokResponse,
+ message_id: String,
+ model: String,
+ monitor: Option<MonitorHandle>,
+ req_id: String,
+) -> Response {
+ stream_body(response.into_stream(), message_id, model, monitor, req_id)
+}
+
+fn stream_body<S>(
+ upstream: S,
+ message_id: String,
+ model: String,
+ monitor: Option<MonitorHandle>,
+ req_id: String,
+) -> Response
+where
+ S: Stream<Item = Result<Bytes, client::GrokError>> + Unpin + Send + 'static,
+{
+ let state = GrokStreamState {
+ upstream,
+ decoder: SseDecoder::default(),
+ reducer: translate::reducer::Reducer::default(),
+ translator: StreamTranslator::new(message_id, model),
+ terminal: false,
+ error_sent: false,
+ monitor,
+ req_id,
+ bytes: 0,
+ chunks: 0,
+ };
+ let stream = futures_util::stream::unfold(state, |mut state| async move {
+ state
+ .next_output()
+ .await
+ .map(|bytes| (Ok::<Bytes, Infallible>(Bytes::from(bytes)), state))
+ });
+ (
+ [
+ (http::header::CONTENT_TYPE, "text/event-stream"),
+ (http::header::CACHE_CONTROL, "no-cache"),
+ ],
+ Body::from_stream(stream),
+ )
+ .into_response()
+}
+
+struct GrokStreamState<S> {
+ upstream: S,
+ decoder: SseDecoder,
+ reducer: translate::reducer::Reducer,
+ translator: StreamTranslator,
+ terminal: bool,
+ error_sent: bool,
+ monitor: Option<MonitorHandle>,
+ req_id: String,
+ bytes: u64,
+ chunks: u64,
+}
+
+impl<S> GrokStreamState<S>
+where
+ S: Stream<Item = Result<Bytes, client::GrokError>> + Unpin,
+{
+ async fn next_output(&mut self) -> Option<Vec<u8>> {
+ if self.terminal {
+ return None;
+ }
+ if self.error_sent {
+ self.terminal = true;
+ return None;
+ }
+ loop {
+ let chunk = match self.upstream.next().await {
+ Some(Ok(chunk)) => chunk,
+ Some(Err(_)) => return Some(self.fail()),
+ None => {
+ if self.decoder.finish().is_err() || !self.reducer.finished() {
+ return Some(self.fail());
+ }
+ self.terminal = true;
+ return None;
+ }
+ };
+ self.bytes = self.bytes.saturating_add(chunk.len() as u64);
+ self.chunks = self.chunks.saturating_add(1);
+ if let Some(monitor) = self.monitor.as_ref() {
+ monitor.stream_progress(&self.req_id, self.bytes, self.chunks, None, None);
+ }
+ let events = match self.decoder.push(&chunk) {
+ Ok(events) => events,
+ Err(_) => return Some(self.fail()),
+ };
+ let mut out = Vec::new();
+ for event in events {
+ let value: serde_json::Value = match serde_json::from_str(&event.data) {
+ Ok(value) => value,
+ Err(_) => return Some(self.fail()),
+ };
+ let reduced = match self.reducer.push(value) {
+ Ok(events) => events,
+ Err(_) => return Some(self.fail()),
+ };
+ let usage = reduced.iter().find_map(|event| match event {
+ translate::reducer::ReducerEvent::Finish {
+ input_tokens,
+ output_tokens,
+ ..
+ } => Some((*input_tokens, *output_tokens)),
+ _ => None,
+ });
+ match self.translator.render(reduced) {
+ Ok(bytes) => out.extend(bytes),
+ Err(_) => return Some(self.fail()),
+ }
+ if let Some((input_tokens, output_tokens)) = usage
+ && let Some(monitor) = self.monitor.as_ref()
+ {
+ monitor.usage_updated(&self.req_id, Some(input_tokens), Some(output_tokens));
+ }
+ if self.reducer.finished() {
+ self.terminal = true;
+ return if out.is_empty() { None } else { Some(out) };
+ }
+ }
+ if !out.is_empty() {
+ return Some(out);
+ }
+ }
+ }
+
+ fn fail(&mut self) -> Vec<u8> {
+ self.error_sent = true;
+ stream_error()
+ }
+}
+
+fn map_error(error: client::GrokError) -> Response {
+ match error.status {
+ StatusCode::UNAUTHORIZED => json_error(
+ StatusCode::UNAUTHORIZED,
+ "authentication_error",
+ error.message,
+ ),
+ StatusCode::TOO_MANY_REQUESTS => {
+ let response = json_error(
+ StatusCode::TOO_MANY_REQUESTS,
+ "rate_limit_error",
+ error.message,
+ );
+ if let Some(retry_after) = error.retry_after {
+ ([(http::header::RETRY_AFTER, retry_after)], response).into_response()
+ } else {
+ response
+ }
+ }
+ StatusCode::PAYMENT_REQUIRED | StatusCode::FORBIDDEN => {
+ json_error(error.status, "permission_error", error.message)
+ }
+ _ => json_error(StatusCode::BAD_GATEWAY, "api_error", error.message),
+ }
+}
+
+pub struct GrokCli;
+pub static GROK_CLI: GrokCli = GrokCli;
+impl CliHandlers for GrokCli {
+ fn login(&self) -> anyhow::Result<()> {
+ let store = file_store();
+ auth::login::login(&store)?;
+ println!("Grok authentication saved in {}", store.auth_path());
+ Ok(())
+ }
+ fn device(&self) -> anyhow::Result<()> {
+ anyhow::bail!("Grok device login is unavailable; use grok auth login")
+ }
+ fn status(&self) -> anyhow::Result<()> {
+ let store = file_store();
+ match store.load_auth()? {
+ Some(auth) => {
+ println!("Auth path: {}", store.auth_path());
+ println!("Authenticated: true");
+ println!(
+ "Expires in {}s",
+ auth.expires_at_ms.saturating_sub(now_ms()) / 1000
+ );
+ Ok(())
+ }
+ None => anyhow::bail!("Not authenticated"),
+ }
+ }
+ fn logout(&self) -> anyhow::Result<()> {
+ let store = file_store();
+ store.clear_auth()?;
+ println!("Grok proxy credentials removed");
+ Ok(())
+ }
+}
+fn now_ms() -> u64 {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_millis() as u64
+}
+
+#[cfg(test)]
+mod tests {
+ use std::pin::Pin;
+ use std::task::{Context, Poll};
+ use std::time::Duration;
+
+ use crate::monitor::{EndpointKind, MonitorHandle};
+ use http_body_util::BodyExt;
+ use tokio::sync::mpsc;
+
+ use super::*;
+
+ struct ChannelStream(mpsc::Receiver<Result<Bytes, client::GrokError>>);
+
+ impl Stream for ChannelStream {
+ type Item = Result<Bytes, client::GrokError>;
+
+ fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
+ self.0.poll_recv(cx)
+ }
+ }
+
+ #[tokio::test]
+ async fn streaming_usage_updates_completed_monitor_request() {
+ let monitor = MonitorHandle::new(10);
+ monitor.request_started(
+ "req_1",
+ Some("session_1".into()),
+ Some(1),
+ EndpointKind::Messages,
+ );
+ monitor.provider_selected("req_1", "grok", "grok-4.5", None);
+ monitor.request_completed("req_1", 200, None, None);
+
+ let upstream = futures_util::stream::iter(vec![Ok(Bytes::from_static(
+ b"data: {\"type\":\"response.output_text.delta\",\"delta\":\"ok\"}\n\ndata: {\"type\":\"response.output_text.done\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":12,\"output_tokens\":3}}}\n\n",
+ ))]);
+ let response = stream_body(
+ upstream,
+ "msg_1".into(),
+ "grok-4.5".into(),
+ Some(monitor.clone()),
+ "req_1".into(),
+ );
+ let _ = response.into_body().collect().await.unwrap();
+
+ let snapshot = monitor.snapshot();
+ let request = snapshot
+ .recent
+ .iter()
+ .find(|request| request.request_id == "req_1")
+ .unwrap();
+ assert_eq!(request.input_tokens, Some(12));
+ assert_eq!(request.output_tokens, Some(3));
+ assert!(request.streamed_bytes > 0);
+ assert!(request.stream_chunks > 0);
+ let session = snapshot
+ .sessions
+ .iter()
+ .find(|session| session.session_id.as_deref() == Some("session_1"))
+ .unwrap();
+ assert_eq!(session.input_tokens, 12);
+ assert_eq!(session.output_tokens, 3);
+ }
+
+ #[tokio::test]
+ async fn downstream_event_arrives_before_upstream_completion() {
+ let (tx, rx) = mpsc::channel(2);
+ let response = stream_body(
+ ChannelStream(rx),
+ "msg_1".into(),
+ "grok-4.5".into(),
+ None,
+ "req_1".into(),
+ );
+ let mut body = response.into_body();
+
+ tx.send(Ok(Bytes::from_static(
+ b"data: {\"type\":\"response.output_text.delta\",\"delta\":\"first\"}\n\n",
+ )))
+ .await
+ .unwrap();
+
+ let first = tokio::time::timeout(Duration::from_millis(250), body.frame())
+ .await
+ .expect("downstream body waited for upstream completion")
+ .expect("downstream body ended before its first event")
+ .expect("downstream body frame failed")
+ .into_data()
+ .expect("first downstream frame was not data");
+ let first = String::from_utf8(first.to_vec()).unwrap();
+ assert!(first.contains("event: message_start"));
+ assert!(first.contains("first"));
+
+ tx.send(Ok(Bytes::from_static(
+ b"data: {\"type\":\"response.completed\",\"response\":{\"usage\":{}}}\n\n",
+ )))
+ .await
+ .unwrap();
+ let terminal = tokio::time::timeout(Duration::from_millis(250), body.frame())
+ .await
+ .expect("downstream completion timed out")
+ .expect("downstream completion was missing")
+ .expect("downstream completion frame failed")
+ .into_data()
+ .expect("downstream completion frame was not data");
+ assert!(
+ String::from_utf8(terminal.to_vec())
+ .unwrap()
+ .contains("event: message_stop")
+ );
+ assert!(
+ tokio::time::timeout(Duration::from_millis(250), body.frame())
+ .await
+ .expect("downstream EOF waited for upstream EOF")
+ .is_none()
+ );
+ }
+}
diff --git a/src/providers/grok/translate/accumulate.rs b/src/providers/grok/translate/accumulate.rs
new file mode 100644
index 0000000..bb448a1
--- /dev/null
+++ b/src/providers/grok/translate/accumulate.rs
@@ -0,0 +1,103 @@
+use std::collections::HashMap;
+
+use super::reducer::{ReducerEvent, reduce_upstream_bytes};
+use serde_json::Value;
+
+pub fn accumulate_response(
+ upstream: &[u8],
+ message_id: &str,
+ model: &str,
+) -> anyhow::Result<Value> {
+ let mut blocks: Vec<Value> = Vec::new();
+ let mut block_positions = HashMap::new();
+ let mut stop = "end_turn".to_string();
+ let mut input = 0;
+ let mut output = 0;
+ for event in reduce_upstream_bytes(upstream)? {
+ match event {
+ ReducerEvent::ThinkingStart(index) => {
+ block_positions.insert(index, blocks.len());
+ blocks.push(serde_json::json!({"type":"thinking","thinking":"","signature":""}))
+ }
+ ReducerEvent::ThinkingDelta(index, text) => {
+ if let Some(block) = block_positions
+ .get(&index)
+ .and_then(|position| blocks.get_mut(*position))
+ {
+ block["thinking"] = Value::String(format!(
+ "{}{}",
+ block["thinking"].as_str().unwrap_or(""),
+ text
+ ));
+ }
+ }
+ ReducerEvent::TextStart(index) => {
+ block_positions.insert(index, blocks.len());
+ blocks.push(serde_json::json!({"type":"text","text":""}))
+ }
+ ReducerEvent::TextDelta(index, text) => {
+ if let Some(block) = block_positions
+ .get(&index)
+ .and_then(|position| blocks.get_mut(*position))
+ {
+ block["text"] =
+ Value::String(format!("{}{}", block["text"].as_str().unwrap_or(""), text));
+ }
+ }
+ ReducerEvent::ToolStart(index, id, name) => {
+ block_positions.insert(index, blocks.len());
+ blocks.push(serde_json::json!({"type":"tool_use","id":id,"name":name,"input":{}}))
+ }
+ ReducerEvent::ToolDelta(index, text) => {
+ if let Some(block) = block_positions
+ .get(&index)
+ .and_then(|position| blocks.get_mut(*position))
+ {
+ let raw = format!(
+ "{}{}",
+ block.get("_args").and_then(Value::as_str).unwrap_or(""),
+ text
+ );
+ block["_args"] = Value::String(raw);
+ }
+ }
+ ReducerEvent::ToolStop(index) => {
+ if let Some(block) = block_positions
+ .get(&index)
+ .and_then(|position| blocks.get_mut(*position))
+ {
+ let raw = block.get("_args").and_then(Value::as_str).unwrap_or("{}");
+ block["input"] = serde_json::from_str(raw)?;
+ block.as_object_mut().unwrap().remove("_args");
+ }
+ }
+ ReducerEvent::Finish {
+ stop_reason,
+ input_tokens,
+ output_tokens,
+ } => {
+ stop = stop_reason;
+ input = input_tokens;
+ output = output_tokens;
+ }
+ _ => {}
+ }
+ }
+ Ok(
+ serde_json::json!({"id":message_id,"type":"message","role":"assistant","model":model,"content":blocks,"stop_reason":stop,"stop_sequence":null,"usage":{"input_tokens":input,"output_tokens":output}}),
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn accumulate_response_tracks_two_interleaved_tool_calls() {
+ let input = b"data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_1\",\"name\":\"first\"}}\n\ndata: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_2\",\"name\":\"second\"}}\n\ndata: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"call_1\",\"delta\":\"{\\\"value\\\":1}\"}\n\ndata: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"call_2\",\"delta\":\"{\\\"value\\\":2}\"}\n\ndata: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_2\"}}\n\ndata: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_1\"}}\n\ndata: {\"type\":\"response.completed\",\"response\":{}}\n\n";
+ let response = accumulate_response(input, "message", "grok-4.5").unwrap();
+
+ assert_eq!(response["content"][0]["input"]["value"], 1);
+ assert_eq!(response["content"][1]["input"]["value"], 2);
+ }
+}
diff --git a/src/providers/grok/translate/mod.rs b/src/providers/grok/translate/mod.rs
new file mode 100644
index 0000000..3e9f944
--- /dev/null
+++ b/src/providers/grok/translate/mod.rs
@@ -0,0 +1,5 @@
+pub mod accumulate;
+pub mod model_allowlist;
+pub mod reducer;
+pub mod request;
+pub mod stream;
diff --git a/src/providers/grok/translate/model_allowlist.rs b/src/providers/grok/translate/model_allowlist.rs
new file mode 100644
index 0000000..8b035be
--- /dev/null
+++ b/src/providers/grok/translate/model_allowlist.rs
@@ -0,0 +1,11 @@
+pub fn resolve_model(model: &str) -> String {
+ model.to_string()
+}
+
+pub fn assert_allowed_model(model: &str) -> anyhow::Result<()> {
+ if matches!(model, "grok-composer-2.5-fast" | "grok-4.5") {
+ Ok(())
+ } else {
+ anyhow::bail!("unsupported Grok model")
+ }
+}
diff --git a/src/providers/grok/translate/reducer.rs b/src/providers/grok/translate/reducer.rs
new file mode 100644
index 0000000..72f1dc0
--- /dev/null
+++ b/src/providers/grok/translate/reducer.rs
@@ -0,0 +1,365 @@
+use std::collections::HashMap;
+
+use serde_json::Value;
+
+use super::stream::SseDecoder;
+
+const MAX_TOOL_ARGUMENT_BYTES: usize = 1024 * 1024;
+const MAX_INCOMPLETE_TOOL_CALLS: usize = 128;
+
+#[derive(Debug, Clone)]
+pub enum ReducerEvent {
+ ThinkingStart(usize),
+ ThinkingDelta(usize, String),
+ ThinkingStop(usize),
+ TextStart(usize),
+ TextDelta(usize, String),
+ TextStop(usize),
+ ToolStart(usize, String, String),
+ ToolDelta(usize, String),
+ ToolStop(usize),
+ Finish {
+ stop_reason: String,
+ input_tokens: u64,
+ output_tokens: u64,
+ },
+}
+
+#[derive(Default)]
+pub struct Reducer {
+ next_index: usize,
+ active: Option<(String, usize)>,
+ calls: HashMap<String, (usize, String)>,
+ item_calls: HashMap<String, String>,
+ tool_args: HashMap<String, String>,
+ completed_arguments: HashMap<String, bool>,
+ saw_tool: bool,
+ completed: bool,
+}
+
+impl Reducer {
+ pub fn push(&mut self, value: Value) -> anyhow::Result<Vec<ReducerEvent>> {
+ if self.completed {
+ anyhow::bail!("event after terminal completion");
+ }
+ let typ = value
+ .get("type")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("event lacks type"))?;
+ match typ {
+ "response.created" | "response.in_progress" => Ok(vec![]),
+ "response.reasoning_summary_part.added"
+ | "response.reasoning_summary_part.done"
+ | "response.content_part.added" => Ok(vec![]),
+ "response.reasoning_summary_text.delta" | "response.reasoning_text.delta" => self
+ .delta(
+ "thinking",
+ value
+ .get("delta")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("reasoning delta is invalid"))?,
+ ),
+ "response.custom_tool_call_input.delta"
+ | "response.custom_tool_call_input.done"
+ | "response.output_text.annotation.added" => Ok(vec![]),
+ "response.output_text.delta" => self.delta(
+ "text",
+ value
+ .get("delta")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("text delta is invalid"))?,
+ ),
+ "response.output_item.added" => {
+ let item = value
+ .get("item")
+ .and_then(Value::as_object)
+ .ok_or_else(|| anyhow::anyhow!("output item is invalid"))?;
+ if item.get("type").and_then(Value::as_str) == Some("function_call") {
+ let id = item
+ .get("call_id")
+ .and_then(Value::as_str)
+ .filter(|v| !v.is_empty())
+ .ok_or_else(|| anyhow::anyhow!("function call id is invalid"))?;
+ let name = item
+ .get("name")
+ .and_then(Value::as_str)
+ .filter(|v| !v.is_empty())
+ .ok_or_else(|| anyhow::anyhow!("function call name is invalid"))?;
+ if self.calls.contains_key(id) {
+ anyhow::bail!("duplicate function call id");
+ }
+ if self.calls.len() >= MAX_INCOMPLETE_TOOL_CALLS {
+ anyhow::bail!("too many incomplete function calls");
+ }
+ let mut out = self.close_active()?;
+ let index = self.next_index;
+ self.next_index += 1;
+ self.calls.insert(id.into(), (index, name.into()));
+ if let Some(item_id) = item.get("id").and_then(Value::as_str) {
+ self.item_calls.insert(item_id.into(), id.into());
+ }
+ self.tool_args.insert(id.into(), String::new());
+ self.completed_arguments.insert(id.into(), false);
+ self.saw_tool = true;
+ out.push(ReducerEvent::ToolStart(index, id.into(), name.into()));
+ Ok(out)
+ } else {
+ Ok(vec![])
+ }
+ }
+ "response.function_call_arguments.delta" => {
+ let id = value
+ .get("call_id")
+ .and_then(Value::as_str)
+ .or_else(|| {
+ value
+ .get("item_id")
+ .and_then(Value::as_str)
+ .and_then(|item_id| self.item_calls.get(item_id).map(String::as_str))
+ })
+ .ok_or_else(|| anyhow::anyhow!("function delta lacks call id"))?;
+ let delta = value
+ .get("delta")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("function delta is invalid"))?;
+ let (index, _) = self
+ .calls
+ .get(id)
+ .ok_or_else(|| anyhow::anyhow!("function delta is out of order"))?
+ .clone();
+ let args = self
+ .tool_args
+ .get_mut(id)
+ .ok_or_else(|| anyhow::anyhow!("function delta is out of order"))?;
+ if args.len().saturating_add(delta.len()) > MAX_TOOL_ARGUMENT_BYTES {
+ anyhow::bail!("function arguments exceed the size limit");
+ }
+ args.push_str(delta);
+ Ok(vec![ReducerEvent::ToolDelta(index, delta.into())])
+ }
+ "response.function_call_arguments.done" => {
+ let id = value
+ .get("call_id")
+ .and_then(Value::as_str)
+ .or_else(|| {
+ value
+ .get("item_id")
+ .and_then(Value::as_str)
+ .and_then(|item_id| self.item_calls.get(item_id).map(String::as_str))
+ })
+ .ok_or_else(|| anyhow::anyhow!("function completion lacks call id"))?;
+ let args = value.get("arguments").and_then(Value::as_str);
+ let accumulated = self
+ .tool_args
+ .get(id)
+ .ok_or_else(|| anyhow::anyhow!("function completion is out of order"))?;
+ let index = self
+ .calls
+ .get(id)
+ .map(|(index, _)| *index)
+ .ok_or_else(|| anyhow::anyhow!("function completion is out of order"))?;
+ let output = match args {
+ Some(args) if accumulated.is_empty() && !args.is_empty() => {
+ self.tool_args.get_mut(id).unwrap().push_str(args);
+ vec![ReducerEvent::ToolDelta(index, args.into())]
+ }
+ Some(args) if args != accumulated => {
+ anyhow::bail!("function completion arguments disagree with deltas")
+ }
+ _ => vec![],
+ };
+ self.completed_arguments.insert(id.into(), true);
+ Ok(output)
+ }
+ "response.output_text.done" => self.close_kind("text"),
+ "response.reasoning_summary_text.done" | "response.reasoning_text.done" => {
+ self.close_kind("thinking")
+ }
+ "response.content_part.done" => Ok(vec![]),
+ "response.output_item.done" => {
+ let item = value
+ .get("item")
+ .and_then(Value::as_object)
+ .ok_or_else(|| anyhow::anyhow!("completed output item is invalid"))?;
+ if item.get("type").and_then(Value::as_str) != Some("function_call") {
+ return Ok(vec![]);
+ }
+ let id = item
+ .get("call_id")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("completed function call lacks call id"))?;
+ let (index, _) = self
+ .calls
+ .remove(id)
+ .ok_or_else(|| anyhow::anyhow!("completed function call is unknown"))?;
+ let args = self.tool_args.remove(id).unwrap_or_default();
+ self.completed_arguments.remove(id);
+ serde_json::from_str::<Value>(&args)
+ .map_err(|_| anyhow::anyhow!("function arguments are incomplete"))?;
+ Ok(vec![ReducerEvent::ToolStop(index)])
+ }
+ "response.completed" => {
+ if !self.calls.is_empty() {
+ anyhow::bail!("function call is incomplete");
+ }
+ let mut out = self.close_active()?;
+ let response = value.get("response").unwrap_or(&value);
+ let usage = response.get("usage").unwrap_or(&Value::Null);
+ let input = usage
+ .get("input_tokens")
+ .and_then(Value::as_u64)
+ .unwrap_or(0);
+ let output = usage
+ .get("output_tokens")
+ .and_then(Value::as_u64)
+ .unwrap_or(0);
+ let stop = if self.saw_tool {
+ "tool_use"
+ } else {
+ "end_turn"
+ };
+ self.completed = true;
+ out.push(ReducerEvent::Finish {
+ stop_reason: stop.into(),
+ input_tokens: input,
+ output_tokens: output,
+ });
+ Ok(out)
+ }
+ "error" | "response.failed" => anyhow::bail!("upstream Grok stream failed"),
+ _ => anyhow::bail!("unsupported Grok stream event: {typ}"),
+ }
+ }
+ fn delta(&mut self, kind: &str, delta: &str) -> anyhow::Result<Vec<ReducerEvent>> {
+ let mut out = Vec::new();
+ if self
+ .active
+ .as_ref()
+ .is_none_or(|(active, _)| active != kind)
+ {
+ out.extend(self.close_active()?);
+ let index = self.next_index;
+ self.next_index += 1;
+ self.active = Some((kind.into(), index));
+ out.push(if kind == "thinking" {
+ ReducerEvent::ThinkingStart(index)
+ } else {
+ ReducerEvent::TextStart(index)
+ });
+ }
+ let index = self.active.as_ref().unwrap().1;
+ out.push(if kind == "thinking" {
+ ReducerEvent::ThinkingDelta(index, delta.into())
+ } else {
+ ReducerEvent::TextDelta(index, delta.into())
+ });
+ Ok(out)
+ }
+ fn close_active(&mut self) -> anyhow::Result<Vec<ReducerEvent>> {
+ Ok(match self.active.take() {
+ Some((kind, index)) if kind == "thinking" => vec![ReducerEvent::ThinkingStop(index)],
+ Some((_, index)) => vec![ReducerEvent::TextStop(index)],
+ None => vec![],
+ })
+ }
+ fn close_kind(&mut self, kind: &str) -> anyhow::Result<Vec<ReducerEvent>> {
+ if self
+ .active
+ .as_ref()
+ .is_some_and(|(active, _)| active == kind)
+ {
+ self.close_active()
+ } else {
+ Ok(vec![])
+ }
+ }
+ pub fn finished(&self) -> bool {
+ self.completed
+ }
+}
+
+pub fn reduce_upstream_bytes(bytes: &[u8]) -> anyhow::Result<Vec<ReducerEvent>> {
+ let mut reducer = Reducer::default();
+ let mut out = Vec::new();
+ let mut decoder = SseDecoder::default();
+ for event in decoder.push(bytes)? {
+ let value: Value = serde_json::from_str(&event.data)
+ .map_err(|_| anyhow::anyhow!("malformed Grok SSE event"))?;
+ out.extend(reducer.push(value)?);
+ }
+ decoder.finish()?;
+ if !reducer.finished() {
+ anyhow::bail!("Grok stream ended without completion");
+ }
+ Ok(out)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ #[test]
+ fn grok_reducer_handles_text_tool_and_completion() {
+ let input = b"data: {\"type\":\"response.output_text.delta\",\"delta\":\"hi\"}\n\ndata: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_1\",\"name\":\"lookup\"}}\n\ndata: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"call_1\",\"delta\":\"{}\"}\n\ndata: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_1\"}}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":3,\"output_tokens\":2}}}\n\n";
+ let events = reduce_upstream_bytes(input).unwrap();
+ assert!(
+ matches!(events.last(), Some(ReducerEvent::Finish { stop_reason, .. }) if stop_reason == "tool_use")
+ );
+ }
+
+ #[test]
+ fn grok_reducer_maps_item_id_argument_events() {
+ let input = b"data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"id\":\"item_1\",\"call_id\":\"call_1\",\"name\":\"Bash\"}}\n\ndata: {\"type\":\"response.function_call_arguments.delta\",\"item_id\":\"item_1\",\"delta\":\"{}\"}\n\ndata: {\"type\":\"response.function_call_arguments.done\",\"item_id\":\"item_1\",\"arguments\":\"{}\"}\n\ndata: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_1\"}}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{}}}\n\n";
+ let events = reduce_upstream_bytes(input).unwrap();
+ assert!(
+ events
+ .iter()
+ .any(|event| matches!(event, ReducerEvent::ToolDelta(_, delta) if delta == "{}"))
+ );
+ assert!(matches!(events.last(), Some(ReducerEvent::Finish { .. })));
+ }
+
+ #[test]
+ fn grok_reducer_accepts_live_reasoning_text_events() {
+ let input = b"data: {\"type\":\"response.reasoning_text.delta\",\"delta\":\"think\"}\n\ndata: {\"type\":\"response.reasoning_text.done\"}\n\ndata: {\"type\":\"response.output_text.delta\",\"delta\":\"answer\"}\n\ndata: {\"type\":\"response.output_text.done\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":4,\"output_tokens\":2}}}\n\n";
+ let events = reduce_upstream_bytes(input).unwrap();
+ assert!(matches!(events[0], ReducerEvent::ThinkingStart(0)));
+ assert!(matches!(
+ &events[1],
+ ReducerEvent::ThinkingDelta(0, delta) if delta == "think"
+ ));
+ assert!(matches!(events.last(), Some(ReducerEvent::Finish { .. })));
+ }
+
+ #[test]
+ fn grok_reducer_accepts_hosted_search_lifecycle() {
+ let input = b"data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"custom_tool_call\",\"id\":\"search_1\"}}\n\ndata: {\"type\":\"response.custom_tool_call_input.delta\",\"item_id\":\"search_1\",\"delta\":\"{\\\"query\\\":\\\"test\\\"}\"}\n\ndata: {\"type\":\"response.custom_tool_call_input.done\",\"item_id\":\"search_1\"}\n\ndata: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"custom_tool_call\",\"id\":\"search_1\"}}\n\ndata: {\"type\":\"response.output_text.annotation.added\",\"annotation\":{\"type\":\"url_citation\"}}\n\ndata: {\"type\":\"response.output_text.delta\",\"delta\":\"result\"}\n\ndata: {\"type\":\"response.output_text.done\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{}}}\n\n";
+ let events = reduce_upstream_bytes(input).unwrap();
+ assert!(
+ events
+ .iter()
+ .any(|event| matches!(event, ReducerEvent::TextDelta(_, text) if text == "result"))
+ );
+ assert!(matches!(events.last(), Some(ReducerEvent::Finish { .. })));
+ }
+
+ #[test]
+ fn grok_reducer_accepts_reasoning_summary_part_completion() {
+ let input = b"data: {\"type\":\"response.reasoning_summary_part.added\"}\n\ndata: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"think\"}\n\ndata: {\"type\":\"response.reasoning_summary_text.done\"}\n\ndata: {\"type\":\"response.reasoning_summary_part.done\",\"part\":{\"type\":\"summary_text\",\"text\":\"think\"}}\n\ndata: {\"type\":\"response.output_text.delta\",\"delta\":\"answer\"}\n\ndata: {\"type\":\"response.output_text.done\"}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{}}}\n\n";
+ let events = reduce_upstream_bytes(input).unwrap();
+ assert!(matches!(events.last(), Some(ReducerEvent::Finish { .. })));
+ }
+
+ #[test]
+ fn grok_reducer_accepts_complete_observed_lifecycle() {
+ let input = b"data: {\"type\":\"response.created\"}\n\ndata: {\"type\":\"response.in_progress\"}\n\ndata: {\"type\":\"response.reasoning_summary_part.added\"}\n\ndata: {\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"think\"}\n\ndata: {\"type\":\"response.reasoning_summary_text.done\"}\n\ndata: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"message\"}}\n\ndata: {\"type\":\"response.content_part.added\"}\n\ndata: {\"type\":\"response.output_text.delta\",\"delta\":\"answer\"}\n\ndata: {\"type\":\"response.output_text.done\"}\n\ndata: {\"type\":\"response.content_part.done\"}\n\ndata: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_1\",\"name\":\"lookup\"}}\n\ndata: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"call_1\",\"delta\":\"{}\"}\n\ndata: {\"type\":\"response.function_call_arguments.done\",\"call_id\":\"call_1\",\"arguments\":\"{}\"}\n\ndata: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"call_id\":\"call_1\"}}\n\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":4,\"output_tokens\":2}}}\n\n";
+ let events = reduce_upstream_bytes(input).unwrap();
+ assert!(matches!(
+ events.last(),
+ Some(ReducerEvent::Finish {
+ input_tokens: 4,
+ output_tokens: 2,
+ ..
+ })
+ ));
+ }
+}
diff --git a/src/providers/grok/translate/request.rs b/src/providers/grok/translate/request.rs
new file mode 100644
index 0000000..108dd26
--- /dev/null
+++ b/src/providers/grok/translate/request.rs
@@ -0,0 +1,592 @@
+use std::collections::HashSet;
+
+use serde::Serialize;
+use serde_json::Value;
+
+use crate::anthropic::schema::{Message, MessagesRequest};
+
+#[derive(Debug, Clone, Serialize)]
+pub struct GrokResponsesRequest {
+ pub model: String,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub instructions: Option<String>,
+ pub input: Vec<GrokInputItem>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub tools: Option<Vec<GrokTool>>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub tool_choice: Option<GrokToolChoice>,
+ pub store: bool,
+ pub stream: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub max_output_tokens: Option<u32>,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(tag = "type")]
+pub enum GrokInputItem {
+ #[serde(rename = "message")]
+ Message {
+ role: String,
+ content: Vec<GrokContentPart>,
+ },
+ #[serde(rename = "function_call")]
+ FunctionCall {
+ call_id: String,
+ name: String,
+ arguments: String,
+ },
+ #[serde(rename = "function_call_output")]
+ FunctionCallOutput { call_id: String, output: String },
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(tag = "type")]
+pub enum GrokContentPart {
+ #[serde(rename = "input_text")]
+ InputText { text: String },
+ #[serde(rename = "output_text")]
+ OutputText { text: String },
+}
+
+#[derive(Debug, Clone, Serialize)]
+pub struct GrokTool {
+ #[serde(rename = "type")]
+ pub kind: String,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub name: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub description: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub parameters: Option<Value>,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(untagged)]
+pub enum GrokToolChoice {
+ Auto(String),
+ Required(String),
+ None(String),
+ Function { r#type: String, name: String },
+}
+
+pub fn translate_request(
+ req: &MessagesRequest,
+ model: String,
+) -> anyhow::Result<GrokResponsesRequest> {
+ reject_unknown_top_level(req)?;
+ let mut instructions = parse_system(req.extra.get("system"))?;
+ let mut tools = parse_tools(req.extra.get("tools"))?;
+ let hosted_search = tools
+ .as_ref()
+ .is_some_and(|tools| tools.iter().any(|tool| tool.kind == "x_search"));
+ let force_hosted_search = hosted_search && requests_web_search(req);
+ if force_hosted_search {
+ tools = Some(vec![GrokTool {
+ kind: "x_search".into(),
+ name: None,
+ description: None,
+ parameters: None,
+ }]);
+ }
+ if hosted_search {
+ let guidance = "For web searches, use the hosted x_search tool. Do not use shell commands, HTTP clients, or local tools to search the web.";
+ instructions = Some(match instructions {
+ Some(existing) if !existing.is_empty() => format!("{existing}\n\n{guidance}"),
+ _ => guidance.into(),
+ });
+ }
+ let tool_choice = if force_hosted_search {
+ Some(GrokToolChoice::Required("required".into()))
+ } else {
+ parse_tool_choice(req.extra.get("tool_choice"), tools.as_ref())?
+ };
+ let mut call_ids = HashSet::new();
+ let mut input = Vec::new();
+ for message in &req.messages {
+ parse_message(message, &mut input, &mut call_ids)?;
+ }
+ Ok(GrokResponsesRequest {
+ model,
+ instructions,
+ input,
+ tools,
+ tool_choice,
+ store: false,
+ stream: true,
+ max_output_tokens: req.max_tokens,
+ })
+}
+
+fn requests_web_search(req: &MessagesRequest) -> bool {
+ let Some(message) = req
+ .messages
+ .iter()
+ .rev()
+ .find(|message| message.role == "user")
+ else {
+ return false;
+ };
+ let text = match &message.content {
+ Value::String(text) => text.clone(),
+ Value::Array(blocks) => blocks
+ .iter()
+ .filter_map(|block| block.get("text").and_then(Value::as_str))
+ .collect::<Vec<_>>()
+ .join(" "),
+ _ => return false,
+ };
+ let text = text.to_ascii_lowercase();
+ [
+ "search online",
+ "search the web",
+ "web search",
+ "look up online",
+ "look up on the web",
+ ]
+ .iter()
+ .any(|phrase| text.contains(phrase))
+}
+
+fn reject_unknown_top_level(req: &MessagesRequest) -> anyhow::Result<()> {
+ for key in req.extra.keys() {
+ if ![
+ "system",
+ "tools",
+ "tool_choice",
+ "context_management",
+ "metadata",
+ "output_config",
+ "thinking",
+ "temperature",
+ "top_p",
+ "top_k",
+ "stop_sequences",
+ "service_tier",
+ ]
+ .contains(&key.as_str())
+ {
+ anyhow::bail!("unsupported Grok request field: {key}");
+ }
+ }
+ Ok(())
+}
+
+fn parse_system(value: Option<&Value>) -> anyhow::Result<Option<String>> {
+ let Some(value) = value else { return Ok(None) };
+ match value {
+ Value::String(text) => Ok(Some(text.clone())),
+ Value::Array(blocks) => {
+ let mut text = String::new();
+ for block in blocks {
+ let object = block
+ .as_object()
+ .ok_or_else(|| anyhow::anyhow!("system content must contain text blocks"))?;
+ if object
+ .keys()
+ .any(|key| !["type", "text", "cache_control"].contains(&key.as_str()))
+ || object.get("type").and_then(Value::as_str) != Some("text")
+ || !valid_cache_control(object.get("cache_control"))
+ {
+ anyhow::bail!("unsupported system block");
+ }
+ let part = object
+ .get("text")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("system text is invalid"))?;
+ text.push_str(part);
+ }
+ Ok(Some(text))
+ }
+ _ => anyhow::bail!("system must be text"),
+ }
+}
+
+fn parse_tools(value: Option<&Value>) -> anyhow::Result<Option<Vec<GrokTool>>> {
+ let Some(value) = value else { return Ok(None) };
+ let tools = value
+ .as_array()
+ .ok_or_else(|| anyhow::anyhow!("tools must be an array"))?;
+ let mut names = HashSet::new();
+ let mut out = Vec::new();
+ for tool in tools {
+ let obj = tool
+ .as_object()
+ .ok_or_else(|| anyhow::anyhow!("tool must be an object"))?;
+ for key in obj.keys() {
+ if !["name", "description", "input_schema"].contains(&key.as_str()) {
+ anyhow::bail!("unsupported tool field: {key}");
+ }
+ }
+ let name = obj
+ .get("name")
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty())
+ .ok_or_else(|| anyhow::anyhow!("tool name is invalid"))?;
+ if !names.insert(name.to_string()) {
+ anyhow::bail!("duplicate tool name");
+ }
+ if name == "WebSearch" {
+ out.push(GrokTool {
+ kind: "x_search".into(),
+ name: None,
+ description: None,
+ parameters: None,
+ });
+ continue;
+ }
+ let parameters = obj
+ .get("input_schema")
+ .filter(|value| value.is_object())
+ .cloned()
+ .ok_or_else(|| anyhow::anyhow!("tool input_schema must be an object"))?;
+ out.push(GrokTool {
+ kind: "function".to_string(),
+ name: Some(name.to_string()),
+ description: obj
+ .get("description")
+ .and_then(Value::as_str)
+ .map(str::to_string),
+ parameters: Some(parameters),
+ });
+ }
+ Ok(Some(out))
+}
+
+fn parse_tool_choice(
+ value: Option<&Value>,
+ tools: Option<&Vec<GrokTool>>,
+) -> anyhow::Result<Option<GrokToolChoice>> {
+ let Some(value) = value else { return Ok(None) };
+ let obj = value
+ .as_object()
+ .ok_or_else(|| anyhow::anyhow!("tool_choice must be an object"))?;
+ let kind = obj
+ .get("type")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("tool_choice type is invalid"))?;
+ match kind {
+ "auto" if obj.len() == 1 => Ok(Some(GrokToolChoice::Auto("auto".into()))),
+ "any" if obj.len() == 1 => Ok(Some(GrokToolChoice::Required("required".into()))),
+ "none" if obj.len() == 1 => Ok(Some(GrokToolChoice::None("none".into()))),
+ "tool" if obj.len() == 2 => {
+ let name = obj
+ .get("name")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("tool_choice name is invalid"))?;
+ if !tools
+ .is_some_and(|items| items.iter().any(|tool| tool.name.as_deref() == Some(name)))
+ {
+ anyhow::bail!("tool_choice references an unknown tool");
+ }
+ Ok(Some(GrokToolChoice::Function {
+ r#type: "function".into(),
+ name: name.into(),
+ }))
+ }
+ _ => anyhow::bail!("unsupported tool_choice"),
+ }
+}
+
+fn parse_message(
+ message: &Message,
+ out: &mut Vec<GrokInputItem>,
+ calls: &mut HashSet<String>,
+) -> anyhow::Result<()> {
+ if !["system", "user", "assistant"].contains(&message.role.as_str()) {
+ anyhow::bail!("unsupported message role");
+ }
+ let blocks: Vec<Value> = match &message.content {
+ Value::String(text) => vec![serde_json::json!({"type":"text", "text":text})],
+ Value::Array(items) => items.clone(),
+ _ => anyhow::bail!("message content must be text or blocks"),
+ };
+ let mut content = Vec::new();
+ for block in blocks {
+ let object = block
+ .as_object()
+ .ok_or_else(|| anyhow::anyhow!("content block must be an object"))?;
+ let typ = object
+ .get("type")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("content block type is invalid"))?;
+ match (message.role.as_str(), typ) {
+ (_, "thinking") | (_, "redacted_thinking") => {}
+ (_, "text") => {
+ if object
+ .keys()
+ .any(|key| !["type", "text", "cache_control"].contains(&key.as_str()))
+ || !valid_cache_control(object.get("cache_control"))
+ {
+ anyhow::bail!("unsupported text block field");
+ }
+ let text = object
+ .get("text")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("text block is invalid"))?;
+ content.push(if message.role == "assistant" {
+ GrokContentPart::OutputText { text: text.into() }
+ } else {
+ GrokContentPart::InputText { text: text.into() }
+ });
+ }
+ ("assistant", "tool_use") => {
+ if object
+ .keys()
+ .any(|key| !["type", "id", "name", "input"].contains(&key.as_str()))
+ || object.len() != 4
+ {
+ anyhow::bail!("unsupported tool_use field");
+ }
+ flush_message(&message.role, &mut content, out);
+ let id = object
+ .get("id")
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty())
+ .ok_or_else(|| anyhow::anyhow!("tool call id is invalid"))?;
+ let name = object
+ .get("name")
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty())
+ .ok_or_else(|| anyhow::anyhow!("tool call name is invalid"))?;
+ let input = object
+ .get("input")
+ .filter(|value| value.is_object())
+ .ok_or_else(|| anyhow::anyhow!("tool call input must be an object"))?;
+ if !calls.insert(id.into()) {
+ anyhow::bail!("duplicate tool call id");
+ }
+ out.push(GrokInputItem::FunctionCall {
+ call_id: id.into(),
+ name: name.into(),
+ arguments: serde_json::to_string(input)?,
+ });
+ }
+ ("user", "tool_result") => {
+ if object.keys().any(|key| {
+ ![
+ "type",
+ "tool_use_id",
+ "content",
+ "is_error",
+ "cache_control",
+ ]
+ .contains(&key.as_str())
+ }) {
+ anyhow::bail!("unsupported tool_result field");
+ }
+ if let Some(is_error) = object.get("is_error")
+ && !is_error.is_boolean()
+ {
+ anyhow::bail!("tool result is_error must be boolean");
+ }
+ flush_message(&message.role, &mut content, out);
+ let id = object
+ .get("tool_use_id")
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty())
+ .ok_or_else(|| anyhow::anyhow!("tool result id is invalid"))?;
+ if !calls.remove(id) {
+ anyhow::bail!("tool result references an unknown or resolved tool call");
+ }
+ let value = object
+ .get("content")
+ .ok_or_else(|| anyhow::anyhow!("tool result content is required"))?;
+ let output = match value {
+ Value::String(text) => text.clone(),
+ Value::Array(parts) => parts
+ .iter()
+ .map(|part| {
+ let part = part.as_object().ok_or_else(|| {
+ anyhow::anyhow!("tool result child must be an object")
+ })?;
+ if part.len() != 2
+ || part.get("type").and_then(Value::as_str) != Some("text")
+ || part
+ .keys()
+ .any(|key| !["type", "text"].contains(&key.as_str()))
+ {
+ anyhow::bail!("tool result supports exact text children only");
+ }
+ part.get("text")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("tool result text is invalid"))
+ })
+ .collect::<anyhow::Result<Vec<_>>>()?
+ .join(""),
+ _ => anyhow::bail!("tool result supports text only"),
+ };
+ out.push(GrokInputItem::FunctionCallOutput {
+ call_id: id.into(),
+ output,
+ });
+ }
+ _ => anyhow::bail!("unsupported content block: {typ}"),
+ }
+ }
+ flush_message(&message.role, &mut content, out);
+ Ok(())
+}
+
+fn valid_cache_control(value: Option<&Value>) -> bool {
+ let Some(value) = value else { return true };
+ let Some(object) = value.as_object() else {
+ return false;
+ };
+ object.keys().all(|key| key == "type" || key == "ttl")
+ && object.get("type").and_then(Value::as_str) == Some("ephemeral")
+ && object
+ .get("ttl")
+ .is_none_or(|ttl| matches!(ttl.as_str(), Some("5m") | Some("1h")))
+}
+
+fn flush_message(role: &str, content: &mut Vec<GrokContentPart>, out: &mut Vec<GrokInputItem>) {
+ if !content.is_empty() {
+ out.push(GrokInputItem::Message {
+ role: role.into(),
+ content: std::mem::take(content),
+ });
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ #[test]
+ fn grok_translation_maps_text_and_function_round_trip() {
+ let request: MessagesRequest = serde_json::from_value(serde_json::json!({
+ "model":"grok-4.5", "max_tokens":12, "system":"rules",
+ "tools":[{"name":"lookup","input_schema":{"type":"object"}}],
+ "tool_choice":{"type":"tool","name":"lookup"},
+ "messages":[
+ {"role":"user","content":"hello"},
+ {"role":"assistant","content":[{"type":"tool_use","id":"call_1","name":"lookup","input":{"q":"a"}}]},
+ {"role":"user","content":[{"type":"tool_result","tool_use_id":"call_1","content":"result"}]}
+ ]
+ })).unwrap();
+ let value =
+ serde_json::to_value(translate_request(&request, "grok-4.5".into()).unwrap()).unwrap();
+ assert_eq!(value["instructions"], "rules");
+ assert_eq!(value["input"][1]["type"], "function_call");
+ assert_eq!(value["input"][2]["type"], "function_call_output");
+ assert_eq!(value["tool_choice"]["type"], "function");
+ }
+ #[test]
+ fn grok_translation_maps_claude_web_search_to_x_search() {
+ let request: MessagesRequest = serde_json::from_value(serde_json::json!({
+ "model":"grok-4.5",
+ "messages":[{"role":"user","content":"search online for the project"}],
+ "tools":[{
+ "name":"WebSearch",
+ "description":"Search the web",
+ "input_schema":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}
+ }]
+ }))
+ .unwrap();
+ let translated =
+ serde_json::to_value(translate_request(&request, "grok-4.5".into()).unwrap()).unwrap();
+ assert_eq!(
+ translated["tools"],
+ serde_json::json!([{"type":"x_search"}])
+ );
+ assert!(
+ translated["instructions"]
+ .as_str()
+ .unwrap()
+ .contains("use the hosted x_search tool")
+ );
+ assert_eq!(translated["tool_choice"], "required");
+ }
+
+ #[test]
+ fn grok_translation_accepts_claude_code_context_management() {
+ let request: MessagesRequest = serde_json::from_value(serde_json::json!({
+ "model":"grok-composer-2.5-fast",
+ "messages":[{"role":"user","content":"hello"}],
+ "context_management":{"edits":[{"type":"clear_tool_uses_20250919","trigger":{"type":"input_tokens","value":100000}}]}
+ }))
+ .unwrap();
+ let translated = translate_request(&request, "grok-composer-2.5-fast".into()).unwrap();
+ assert_eq!(translated.input.len(), 1);
+ }
+
+ #[test]
+ fn grok_translation_rejects_unknown_fields() {
+ let request: MessagesRequest = serde_json::from_value(
+ serde_json::json!({"model":"grok-4.5","messages":[],"unknown_field":true}),
+ )
+ .unwrap();
+ assert!(translate_request(&request, "grok-4.5".into()).is_err());
+ }
+
+ #[test]
+ fn grok_translation_accepts_verified_cache_control_without_forwarding_it() {
+ let request: MessagesRequest = serde_json::from_value(serde_json::json!({
+ "model":"grok-4.5",
+ "system":[{"type":"text","text":"rules","cache_control":{"type":"ephemeral"}}],
+ "messages":[{"role":"user","content":[{"type":"text","text":"hello","cache_control":{"type":"ephemeral","ttl":"5m"}}]}]
+ })).unwrap();
+ let translated =
+ serde_json::to_value(translate_request(&request, "grok-4.5".into()).unwrap()).unwrap();
+ assert_eq!(translated["instructions"], "rules");
+ assert_eq!(translated["input"][0]["content"][0]["text"], "hello");
+ assert!(!translated.to_string().contains("cache_control"));
+ }
+
+ #[test]
+ fn grok_translation_rejects_invalid_cache_control() {
+ let request: MessagesRequest = serde_json::from_value(serde_json::json!({
+ "model":"grok-4.5", "messages":[{"role":"user","content":[{"type":"text","text":"hello","cache_control":{"type":"persistent"}}]}]
+ })).unwrap();
+ assert!(translate_request(&request, "grok-4.5".into()).is_err());
+ }
+
+ fn request_with_blocks(blocks: Value) -> MessagesRequest {
+ serde_json::from_value(serde_json::json!({
+ "model":"grok-4.5",
+ "messages":[
+ {"role":"assistant","content":[{"type":"tool_use","id":"call_1","name":"lookup","input":{}}]},
+ {"role":"user","content":blocks}
+ ]
+ }))
+ .unwrap()
+ }
+
+ #[test]
+ fn grok_translation_rejects_unknown_tool_block_fields() {
+ let mut request = request_with_blocks(serde_json::json!([
+ {"type":"tool_result","tool_use_id":"call_1","content":"ok"}
+ ]));
+ request.messages[0].content[0]["unknown"] = Value::Bool(true);
+ assert!(translate_request(&request, "grok-4.5".into()).is_err());
+
+ let request = request_with_blocks(serde_json::json!([
+ {"type":"tool_result","tool_use_id":"call_1","content":"ok","unknown":true}
+ ]));
+ assert!(translate_request(&request, "grok-4.5".into()).is_err());
+ }
+
+ #[test]
+ fn grok_translation_rejects_malformed_tool_result_children() {
+ for child in [
+ serde_json::json!("text"),
+ serde_json::json!({"text":"ok"}),
+ serde_json::json!({"type":"image","text":"ok"}),
+ serde_json::json!({"type":"text","text":1}),
+ serde_json::json!({"type":"text","text":"ok","unknown":true}),
+ ] {
+ let request = request_with_blocks(serde_json::json!([
+ {"type":"tool_result","tool_use_id":"call_1","content":[child]}
+ ]));
+ assert!(translate_request(&request, "grok-4.5".into()).is_err());
+ }
+ }
+
+ #[test]
+ fn grok_translation_rejects_duplicate_tool_results() {
+ let request = request_with_blocks(serde_json::json!([
+ {"type":"tool_result","tool_use_id":"call_1","content":"first"},
+ {"type":"tool_result","tool_use_id":"call_1","content":"second"}
+ ]));
+ assert!(translate_request(&request, "grok-4.5".into()).is_err());
+ }
+}
diff --git a/src/providers/grok/translate/stream.rs b/src/providers/grok/translate/stream.rs
new file mode 100644
index 0000000..6ede3e6
--- /dev/null
+++ b/src/providers/grok/translate/stream.rs
@@ -0,0 +1,309 @@
+use super::reducer::{Reducer, ReducerEvent};
+use crate::anthropic::sse::{SseEvent, encode_sse_event};
+
+pub const MAX_SSE_FRAME_BYTES: usize = 1024 * 1024;
+
+#[derive(Default)]
+pub struct SseDecoder {
+ frame: Vec<u8>,
+ line_start: usize,
+ skip_lf: bool,
+}
+
+impl SseDecoder {
+ pub fn push(&mut self, input: &[u8]) -> anyhow::Result<Vec<SseEvent>> {
+ let mut events = Vec::new();
+ for &byte in input {
+ if self.skip_lf {
+ self.skip_lf = false;
+ if byte == b'\n' {
+ continue;
+ }
+ }
+ match byte {
+ b'\n' => self.end_line(&mut events)?,
+ b'\r' => {
+ self.end_line(&mut events)?;
+ self.skip_lf = true;
+ }
+ _ => self.push_byte(byte)?,
+ }
+ }
+ Ok(events)
+ }
+
+ pub fn finish(&mut self) -> anyhow::Result<()> {
+ if self.frame.is_empty() {
+ Ok(())
+ } else {
+ anyhow::bail!("Grok SSE stream ended with an incomplete frame")
+ }
+ }
+
+ fn push_byte(&mut self, byte: u8) -> anyhow::Result<()> {
+ if self.frame.len() >= MAX_SSE_FRAME_BYTES {
+ anyhow::bail!("Grok SSE frame exceeds the size limit");
+ }
+ self.frame.push(byte);
+ Ok(())
+ }
+
+ fn end_line(&mut self, events: &mut Vec<SseEvent>) -> anyhow::Result<()> {
+ if self.frame.len() == self.line_start {
+ if !self.frame.is_empty() {
+ events.push(parse_frame(&self.frame)?);
+ }
+ self.frame.clear();
+ self.line_start = 0;
+ return Ok(());
+ }
+ self.push_byte(b'\n')?;
+ self.line_start = self.frame.len();
+ Ok(())
+ }
+}
+
+fn parse_frame(frame: &[u8]) -> anyhow::Result<SseEvent> {
+ let frame = std::str::from_utf8(frame)
+ .map_err(|_| anyhow::anyhow!("Grok SSE frame contains invalid UTF-8"))?;
+ let mut event = None;
+ let mut data = Vec::new();
+ for line in frame.lines() {
+ if line.starts_with(':') {
+ continue;
+ }
+ let (field, value) = line.split_once(':').unwrap_or((line, ""));
+ let value = value.strip_prefix(' ').unwrap_or(value);
+ match field {
+ "event" => event = Some(value.to_owned()),
+ "data" => data.push(value),
+ _ => {}
+ }
+ }
+ if data.is_empty() {
+ anyhow::bail!("Grok SSE frame lacks data")
+ }
+ Ok(SseEvent {
+ event,
+ data: data.join("\n"),
+ })
+}
+
+pub struct StreamTranslator {
+ message_id: String,
+ model: String,
+ started: bool,
+ finished: bool,
+}
+
+pub struct LiveStreamTranslator {
+ decoder: SseDecoder,
+ reducer: Reducer,
+ renderer: StreamTranslator,
+}
+
+impl LiveStreamTranslator {
+ pub fn new(message_id: String, model: String) -> Self {
+ Self {
+ decoder: SseDecoder::default(),
+ reducer: Reducer::default(),
+ renderer: StreamTranslator::new(message_id, model),
+ }
+ }
+
+ pub fn push(&mut self, chunk: &[u8]) -> anyhow::Result<Vec<u8>> {
+ let mut out = Vec::new();
+ for event in self.decoder.push(chunk)? {
+ let value = serde_json::from_str(&event.data)
+ .map_err(|_| anyhow::anyhow!("malformed Grok SSE event"))?;
+ out.extend(self.renderer.render(self.reducer.push(value)?)?);
+ }
+ Ok(out)
+ }
+
+ pub fn finish(mut self) -> anyhow::Result<()> {
+ self.decoder.finish()?;
+ if !self.reducer.finished() {
+ anyhow::bail!("Grok stream ended without completion");
+ }
+ Ok(())
+ }
+}
+
+impl StreamTranslator {
+ pub fn new(message_id: String, model: String) -> Self {
+ Self {
+ message_id,
+ model,
+ started: false,
+ finished: false,
+ }
+ }
+
+ pub fn render(&mut self, events: Vec<ReducerEvent>) -> anyhow::Result<Vec<u8>> {
+ if self.finished && !events.is_empty() {
+ anyhow::bail!("event after terminal completion");
+ }
+ let mut out = Vec::new();
+ for event in events {
+ if !self.started
+ && matches!(
+ event,
+ ReducerEvent::ThinkingStart(_)
+ | ReducerEvent::TextStart(_)
+ | ReducerEvent::ToolStart(_, _, _)
+ | ReducerEvent::Finish { .. }
+ )
+ {
+ self.started = true;
+ emit(
+ &mut out,
+ "message_start",
+ serde_json::json!({"type":"message_start","message":{"id":self.message_id,"type":"message","role":"assistant","model":self.model,"content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}}),
+ );
+ }
+ if matches!(event, ReducerEvent::Finish { .. }) {
+ self.finished = true;
+ }
+ render(&mut out, event);
+ }
+ Ok(out)
+ }
+}
+
+pub fn translate_stream_bytes(
+ upstream: &[u8],
+ message_id: &str,
+ model: &str,
+) -> anyhow::Result<Vec<u8>> {
+ let mut decoder = SseDecoder::default();
+ let mut reducer = Reducer::default();
+ let mut translator = StreamTranslator::new(message_id.into(), model.into());
+ let mut out = Vec::new();
+ for event in decoder.push(upstream)? {
+ let value = serde_json::from_str(&event.data)
+ .map_err(|_| anyhow::anyhow!("malformed Grok SSE event"))?;
+ out.extend(translator.render(reducer.push(value)?)?);
+ }
+ decoder.finish()?;
+ if !reducer.finished() {
+ anyhow::bail!("Grok stream ended without completion");
+ }
+ Ok(out)
+}
+
+pub fn stream_error() -> Vec<u8> {
+ let data = serde_json::json!({"type":"error","error":{"type":"api_error","message":"Grok stream is invalid"}});
+ encode_sse_event(Some("error"), &data.to_string())
+}
+
+fn emit(out: &mut Vec<u8>, event: &str, data: serde_json::Value) {
+ out.extend(encode_sse_event(Some(event), &data.to_string()));
+}
+
+fn render(out: &mut Vec<u8>, event: ReducerEvent) {
+ match event {
+ ReducerEvent::ThinkingStart(i) => emit(
+ out,
+ "content_block_start",
+ serde_json::json!({"type":"content_block_start","index":i,"content_block":{"type":"thinking","thinking":"","signature":""}}),
+ ),
+ ReducerEvent::ThinkingDelta(i, t) => emit(
+ out,
+ "content_block_delta",
+ serde_json::json!({"type":"content_block_delta","index":i,"delta":{"type":"thinking_delta","thinking":t}}),
+ ),
+ ReducerEvent::ThinkingStop(i) | ReducerEvent::TextStop(i) | ReducerEvent::ToolStop(i) => {
+ emit(
+ out,
+ "content_block_stop",
+ serde_json::json!({"type":"content_block_stop","index":i}),
+ )
+ }
+ ReducerEvent::TextStart(i) => emit(
+ out,
+ "content_block_start",
+ serde_json::json!({"type":"content_block_start","index":i,"content_block":{"type":"text","text":""}}),
+ ),
+ ReducerEvent::TextDelta(i, t) => emit(
+ out,
+ "content_block_delta",
+ serde_json::json!({"type":"content_block_delta","index":i,"delta":{"type":"text_delta","text":t}}),
+ ),
+ ReducerEvent::ToolStart(i, id, name) => emit(
+ out,
+ "content_block_start",
+ serde_json::json!({"type":"content_block_start","index":i,"content_block":{"type":"tool_use","id":id,"name":name,"input":{}}}),
+ ),
+ ReducerEvent::ToolDelta(i, t) => emit(
+ out,
+ "content_block_delta",
+ serde_json::json!({"type":"content_block_delta","index":i,"delta":{"type":"input_json_delta","partial_json":t}}),
+ ),
+ ReducerEvent::Finish {
+ stop_reason,
+ output_tokens,
+ ..
+ } => {
+ emit(
+ out,
+ "message_delta",
+ serde_json::json!({"type":"message_delta","delta":{"stop_reason":stop_reason,"stop_sequence":null},"usage":{"output_tokens":output_tokens}}),
+ );
+ emit(
+ out,
+ "message_stop",
+ serde_json::json!({"type":"message_stop"}),
+ );
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn decoder_accepts_every_boundary_and_line_ending() {
+ let input = b": note\r\nevent: ignored\r\ndata: first\r\ndata: second\r\n\r\n";
+ let expected = vec![SseEvent {
+ event: Some("ignored".into()),
+ data: "first\nsecond".into(),
+ }];
+ for split in 0..=input.len() {
+ let mut decoder = SseDecoder::default();
+ let mut events = decoder.push(&input[..split]).unwrap();
+ events.extend(decoder.push(&input[split..]).unwrap());
+ decoder.finish().unwrap();
+ assert_eq!(events, expected);
+ }
+ }
+
+ #[test]
+ fn decoder_requires_terminated_valid_frames_and_bounds_them() {
+ assert!(SseDecoder::default().push(b"data: \xff\n\n").is_err());
+ let mut decoder = SseDecoder::default();
+ decoder.push(b"data: incomplete").unwrap();
+ assert!(decoder.finish().is_err());
+ let mut decoder = SseDecoder::default();
+ let exact = vec![b'x'; MAX_SSE_FRAME_BYTES - b"data: \n".len()];
+ assert!(decoder.push(b"data: ").is_ok());
+ assert!(decoder.push(&exact).is_ok());
+ let events = decoder.push(b"\n\n").unwrap();
+ assert_eq!(events[0].data.len(), exact.len());
+ decoder.finish().unwrap();
+ let mut decoder = SseDecoder::default();
+ assert!(decoder.push(b"data: ").is_ok());
+ assert!(decoder.push(&vec![b'x'; exact.len() + 1]).is_ok());
+ assert!(decoder.push(b"\n").is_err());
+ }
+
+ #[test]
+ fn live_translator_emits_first_event_before_upstream_completion() {
+ let mut translator = LiveStreamTranslator::new("msg_1".into(), "grok-4.5".into());
+ let output = translator
+ .push(b"data: {\"type\":\"response.output_text.delta\",\"delta\":\"first\"}\n\n")
+ .unwrap();
+ assert!(String::from_utf8(output).unwrap().contains("first"));
+ }
+}
diff --git a/src/providers/mod.rs b/src/providers/mod.rs
index d539db4..438af71 100644
--- a/src/providers/mod.rs
+++ b/src/providers/mod.rs
@@ -1,4 +1,5 @@
pub mod codex;
pub mod cursor;
+pub mod grok;
pub mod kimi;
pub mod translate_shared;
diff --git a/src/registry.rs b/src/registry.rs
index ddb606f..4b149a3 100644
--- a/src/registry.rs
+++ b/src/registry.rs
@@ -49,6 +49,7 @@ pub(crate) const CODEX_MODELS: &[&str] = &[
];
pub(crate) const KIMI_MODELS: &[&str] = &["kimi-for-coding", "kimi-k2.6", "k2.6"];
+pub(crate) const GROK_MODELS: &[&str] = &["grok-composer-2.5-fast", "grok-4.5"];
pub struct Registry {
alias_provider: AliasProvider,
@@ -65,6 +66,13 @@ impl Registry {
KIMI_MODELS.iter().map(|m| (*m).to_string()).collect(),
);
models.insert("cursor".into(), build_cursor_models());
+ models.insert(
+ "grok".into(),
+ GROK_MODELS
+ .iter()
+ .map(|model| (*model).to_string())
+ .collect(),
+ );
let mut handlers = BTreeMap::new();
for (name, entries) in &models {
@@ -72,6 +80,7 @@ impl Registry {
"codex" => Arc::new(crate::providers::codex::CodexProvider::new()),
"kimi" => Arc::new(crate::providers::kimi::KimiProvider::new()),
"cursor" => Arc::new(crate::providers::cursor::CursorProvider::new()),
+ "grok" => Arc::new(crate::providers::grok::GrokProvider::new()),
_ => Arc::new(PlaceholderProvider::new(name, entries.clone())),
};
handlers.insert(name.clone(), handler);
@@ -196,6 +205,7 @@ impl PlaceholderProvider {
"codex" => "codex",
"kimi" => "kimi",
"cursor" => "cursor",
+ "grok" => "grok",
_ => "codex",
};
Self { name, models }
@@ -217,6 +227,7 @@ impl Provider for PlaceholderProvider {
"codex" => &CODEX_CLI,
"kimi" => &KIMI_CLI,
"cursor" => &CURSOR_CLI,
+ "grok" => &GROK_CLI,
_ => &CODEX_CLI,
}
}
@@ -275,6 +286,7 @@ impl CliHandlers for PlaceholderCli {
const CODEX_CLI: PlaceholderCli = PlaceholderCli { provider: "codex" };
const KIMI_CLI: PlaceholderCli = PlaceholderCli { provider: "kimi" };
const CURSOR_CLI: PlaceholderCli = PlaceholderCli { provider: "cursor" };
+const GROK_CLI: PlaceholderCli = PlaceholderCli { provider: "grok" };
fn expand_codex_models() -> Vec<String> {
let mut set = HashSet::new();