fix(grok-cli): align Grok Build with current subscription protocol (#2590)

This commit is contained in:
ryanngit
2026-07-16 15:33:19 +07:00
committed by decolua
parent d6761c6fb0
commit 59b7828237
13 changed files with 839 additions and 84 deletions
+216 -9
View File
@@ -4,11 +4,14 @@ import {
countGrokCliUserTurns,
resolveGrokCliTurnIdx,
_resetGrokCliTurnStore,
_getGrokCliTurnStoreSize,
normalizeGrokCliEffort,
supportsGrokCliReasoningEffort,
} from "../../open-sse/executors/grok-cli.js";
import { getExecutor, hasSpecializedExecutor } from "../../open-sse/executors/index.js";
import { PROVIDERS, PROVIDER_OAUTH, PROVIDER_MODELS } from "../../open-sse/providers/index.js";
import { getModelUpstreamId } from "../../open-sse/config/providerModels.js";
import { resolveProviderAlias } from "../../open-sse/services/model.js";
import { getModelInfoCore, resolveProviderAlias } from "../../open-sse/services/model.js";
import { OAUTH_PROVIDERS } from "../../src/shared/constants/providers.js";
describe("grok-cli registry", () => {
@@ -27,7 +30,7 @@ describe("grok-cli registry", () => {
expect(oauth.scope).toContain("conversations:write");
expect(oauth.referrer).toBe("grok-build");
expect(PROVIDER_MODELS.gcli?.some((m) => m.id === "grok-4.5")).toBe(true);
expect(PROVIDER_MODELS.gcli?.some((m) => m.id === "grok-build")).toBe(true);
});
it("is listed as oauth provider for dashboard", () => {
@@ -42,6 +45,13 @@ describe("grok-cli registry", () => {
expect(resolveProviderAlias("grok-cli")).toBe("grok-cli");
});
it("routes bare grok-build to the subscription provider", async () => {
await expect(getModelInfoCore("grok-build", {})).resolves.toEqual({
provider: "grok-cli",
model: "grok-build",
});
});
it("maps effort virtual models to upstream grok-4.5", () => {
expect(getModelUpstreamId("gcli", "grok-4.5-high")).toBe("grok-4.5");
expect(getModelUpstreamId("gcli", "grok-4.5-medium")).toBe("grok-4.5");
@@ -86,19 +96,19 @@ describe("GrokCliExecutor", () => {
expect(headers.Authorization).toBe("Bearer tok_test");
expect(headers.Accept).toBe("text/event-stream");
expect(headers["x-xai-token-auth"]).toBe("xai-grok-cli");
expect(headers["x-grok-client-identifier"]).toBe("grok-pager");
expect(headers["x-grok-client-version"]).toBe("0.2.93");
expect(headers["x-xai-token-auth"]).toBeUndefined();
expect(headers["x-grok-client-identifier"]).toBe("grok-shell");
expect(headers["x-grok-client-version"]).toBe("0.2.99");
expect(headers["x-grok-session-id"]).toBe("sess-abc");
expect(headers["x-grok-conv-id"]).toBe("sess-abc");
expect(headers["x-grok-req-id"]).toBe("req-xyz");
expect(headers["x-grok-turn-idx"]).toBe("3");
expect(headers["x-grok-agent-id"]).toBe("agent-1");
expect(headers["x-grok-model-override"]).toBe("grok-4.5");
expect(headers["x-compaction-at"]).toBe("400000");
expect(headers["x-compaction-at"]).toBeUndefined();
expect(headers["x-email"]).toBe("u@example.com");
expect(headers["x-userid"]).toBe("uid-1");
expect(headers["x-authenticateresponse"]).toBe("authenticate-response");
expect(headers["x-authenticateresponse"]).toBeUndefined();
});
it("buildHeaders falls back to top-level email/userId (OAuth mapTokens shape)", () => {
@@ -190,6 +200,164 @@ describe("GrokCliExecutor", () => {
expect(out.reasoning.effort).toBe("medium");
});
it("normalizes Codex cross-provider tool and reasoning history", () => {
const out = executor.transformRequest("grok-4.5", {
model: "grok-4.5",
input: [
{ type: "message", role: "user", content: "continue" },
{
type: "reasoning",
id: "rs_07fe505b3114f180016a5698411c448191bdcdcba678464461",
encrypted_content: "openai-ciphertext",
summary: [],
internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },
},
{
type: "custom_tool_call",
id: "ctc_openai",
call_id: "call-custom",
name: "exec",
input: "run this",
internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },
},
{
type: "custom_tool_call_output",
call_id: "call-custom",
output: [{ type: "input_text", text: "first" }, { type: "input_text", text: "second" }],
},
{
type: "function_call_output",
call_id: "call-function",
output: [{ type: "input_text", text: "function result" }],
},
],
tools: [{ type: "custom", name: "exec", description: "Run command" }],
}, true, { connectionId: "cross-provider" });
expect(out.input.some((item) => item.type === "reasoning")).toBe(false);
expect(out.input[1]).toEqual({
type: "function_call",
call_id: "call-custom",
name: "exec",
arguments: JSON.stringify({ input: "run this" }),
});
expect(out.input[2]).toEqual({
type: "function_call_output",
call_id: "call-custom",
output: JSON.stringify([{ type: "input_text", text: "first" }, { type: "input_text", text: "second" }]),
});
expect(out.input.some((item) => item.call_id === "call-function")).toBe(false);
expect(out.tools[0].parameters).toEqual({
type: "object",
properties: { input: { type: "string" } },
required: ["input"],
});
});
it("stringifies structured outputs and removes orphaned output items", () => {
const out = executor.transformRequest("grok-4.5", {
model: "grok-4.5",
input: [
{ type: "function_call", call_id: "call-array", name: "array_tool", arguments: "{}" },
{ type: "function_call_output", call_id: "call-array", output: [1, 2] },
{ type: "function_call", call_id: "call-null", name: "null_tool", arguments: "{}" },
{ type: "function_call_output", call_id: "call-null", output: null },
{ type: "custom_tool_call", call_id: "call-invalid", input: "missing name" },
{ type: "custom_tool_call_output", call_id: "call-invalid", output: "orphan" },
],
}, true, { connectionId: "structured-output" });
const outputs = out.input.filter((item) => item.type === "function_call_output");
expect(outputs).toEqual([
{ type: "function_call_output", call_id: "call-array", output: "[1,2]" },
{ type: "function_call_output", call_id: "call-null", output: "null" },
]);
expect(out.input.some((item) => item.call_id === "call-invalid")).toBe(false);
});
it("preserves native Grok encrypted reasoning and item ids", () => {
const reasoningId = "rs_3e3f6187-892a-96db-893b-904eff019e19";
const messageId = "msg_3e3f6187-892a-96db-893b-904eff019e19";
const functionId = "fc_3e3f6187-892a-96db-893b-904eff019e19";
const out = executor.transformRequest("grok-4.5", {
model: "grok-4.5",
input: [
{
type: "reasoning",
id: reasoningId,
status: "completed",
encrypted_content: "grok-ciphertext",
summary: [],
internal_chat_message_metadata_passthrough: { turn_id: "turn-2" },
},
{ type: "message", id: messageId, role: "assistant", content: "done" },
{ type: "function_call", id: functionId, call_id: "native-call", name: "wait", arguments: "{}" },
{ type: "function_call_output", call_id: "native-call", output: "done" },
{ type: "message", role: "user", content: "next" },
],
}, true, { connectionId: "native-grok" });
expect(out.input[0]).toMatchObject({
type: "reasoning",
id: reasoningId,
encrypted_content: "grok-ciphertext",
});
expect(out.input[0].internal_chat_message_metadata_passthrough).toBeUndefined();
expect(out.input[1].id).toBe(messageId);
expect(out.input[2].id).toBe(functionId);
});
it("normalizes official effort aliases", () => {
expect(normalizeGrokCliEffort("none")).toBe("high");
expect(normalizeGrokCliEffort("minimal")).toBe("high");
expect(normalizeGrokCliEffort("max")).toBe("xhigh");
expect(normalizeGrokCliEffort("xhigh")).toBe("xhigh");
expect(normalizeGrokCliEffort("ultra")).toBe("high");
const out = executor.transformRequest("grok-4.5", {
model: "grok-4.5",
input: "hi",
reasoning: { effort: "max", summary: "detailed" },
}, true, { connectionId: "effort-conn" });
expect(out.reasoning).toEqual({ effort: "xhigh", summary: "detailed" });
});
it("omits reasoning effort for models that reject it", () => {
expect(supportsGrokCliReasoningEffort("grok-4.5")).toBe(true);
expect(supportsGrokCliReasoningEffort("grok-build")).toBe(false);
expect(supportsGrokCliReasoningEffort("grok-composer-2.5-fast")).toBe(false);
for (const model of ["grok-build", "grok-composer-2.5-fast"]) {
const out = executor.transformRequest(model, {
model,
input: "hi",
reasoning: { effort: "max" },
}, true, { connectionId: `effort-${model}` });
expect(out.reasoning).toEqual({ summary: "concise" });
expect(out.include).toContain("reasoning.encrypted_content");
}
});
it("drops stale tool_choice and normalizes converted custom choices", () => {
const noTools = executor.transformRequest("grok-build", {
model: "grok-build",
input: "hi",
tool_choice: "auto",
}, true, { connectionId: "tools-none" });
expect(noTools.tool_choice).toBeUndefined();
const custom = executor.transformRequest("grok-build", {
model: "grok-build",
input: "hi",
tools: [{ type: "custom", name: "apply_patch", description: "Patch files" }],
tool_choice: { type: "custom", name: "apply_patch" },
}, true, { connectionId: "tools-custom" });
expect(custom.tools).toEqual([
expect.objectContaining({ type: "function", name: "apply_patch" }),
]);
expect(custom.tool_choice).toEqual({ type: "function", name: "apply_patch" });
});
it("increments x-grok-turn-idx from user-message count and stays monotonic", () => {
const creds = {
connectionId: "turn-conn",
@@ -238,7 +406,7 @@ describe("GrokCliExecutor", () => {
headers = executor.buildHeaders({ accessToken: "t" }, true);
expect(headers["x-grok-turn-idx"]).toBe("2");
// Same session, payload that only has 1 user msg (delta-style client) must not go backwards
// Same session, a new delta-style request advances without relying on full history.
executor.transformRequest(
"grok-4.5",
{
@@ -248,7 +416,7 @@ describe("GrokCliExecutor", () => {
true,
creds
);
expect(executor._currentTurnIdx).toBe(2);
expect(executor._currentTurnIdx).toBe(3);
});
it("countGrokCliUserTurns / resolveGrokCliTurnIdx helpers", () => {
@@ -273,6 +441,45 @@ describe("GrokCliExecutor", () => {
expect(resolveGrokCliTurnIdx("s1", [{ role: "user", type: "message", content: "a" }])).toBe(2);
});
it("keeps fallback session stable when assistant history appears", () => {
const creds = { connectionId: "fallback-conn", rawHeaders: {} };
executor.transformRequest("grok-build", {
model: "grok-build",
input: [{ type: "message", role: "user", content: "first" }],
}, true, creds);
const firstSession = executor._currentSessionId;
executor.transformRequest("grok-build", {
model: "grok-build",
input: [
{ type: "message", role: "user", content: "first" },
{ type: "message", role: "assistant", content: "x".repeat(100) },
{ type: "message", role: "user", content: "second" },
],
}, true, creds);
expect(executor._currentSessionId).toBe(firstSession);
expect(executor._currentTurnIdx).toBe(2);
});
it("does not advance turn index when retrying the same request body", () => {
const body = {
model: "grok-build",
input: [{ type: "message", role: "user", content: "retry me" }],
};
const creds = { connectionId: "retry-conn" };
executor.transformRequest("grok-build", body, true, creds);
const firstTurn = executor._currentTurnIdx;
executor.transformRequest("grok-build", body, true, creds);
expect(executor._currentTurnIdx).toBe(firstTurn);
});
it("bounds per-session turn state", () => {
for (let i = 0; i < 5100; i += 1) {
resolveGrokCliTurnIdx(`session-${i}`, [{ role: "user", content: "hi" }]);
}
expect(_getGrokCliTurnStoreSize()).toBe(5000);
});
it("parseError surfaces 402 spending-limit", () => {
const err = executor.parseError(
{ status: 402 },
+80
View File
@@ -0,0 +1,80 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../../open-sse/services/oauthCredentialManager.js", () => ({
refreshProviderCredentials: vi.fn(),
}));
import { refreshProviderCredentials } from "../../open-sse/services/oauthCredentialManager.js";
import {
parseGrokCliModels,
resolveGrokCliModels,
} from "../../open-sse/services/grokCliModels.js";
function jsonResponse(body, status = 200) {
return new Response(JSON.stringify(body), {
status,
headers: { "Content-Type": "application/json" },
});
}
describe("Grok CLI live models", () => {
beforeEach(() => vi.clearAllMocks());
it("normalizes official model metadata", () => {
expect(parseGrokCliModels({
models: [{
model_id: "grok-build",
display_name: "Grok Build",
context_window: 500000,
max_output_tokens: 64000,
supported_in_api: false,
}],
})).toEqual([
expect.objectContaining({
id: "grok-build",
name: "Grok Build",
contextLength: 500000,
maxOutputTokens: 64000,
supported_in_api: false,
}),
]);
});
it("refreshes and retries through selected proxy", async () => {
const fetchFn = vi.fn()
.mockResolvedValueOnce(jsonResponse({ error: "expired" }, 401))
.mockResolvedValueOnce(jsonResponse({ data: [{ id: "grok-build" }] }));
const onCredentialsRefreshed = vi.fn();
const proxyOptions = {
connectionProxyEnabled: true,
connectionProxyUrl: "http://proxy.test:8080",
strictProxy: true,
};
refreshProviderCredentials.mockResolvedValue({ accessToken: "new-token" });
const result = await resolveGrokCliModels({
accessToken: "old-token",
refreshToken: "refresh-token",
providerSpecificData: { email: "user@example.com" },
}, { fetchFn, proxyOptions, onCredentialsRefreshed });
expect(result.models).toEqual([
expect.objectContaining({
id: "grok-build",
contextLength: 500000,
maxOutputTokens: 64000,
}),
]);
expect(refreshProviderCredentials).toHaveBeenCalledWith(
"grok-cli",
expect.any(Object),
expect.anything(),
proxyOptions,
);
expect(onCredentialsRefreshed).toHaveBeenCalledWith({ accessToken: "new-token" });
expect(fetchFn).toHaveBeenCalledTimes(2);
expect(fetchFn.mock.calls[0][2]).toBe(proxyOptions);
expect(fetchFn.mock.calls[1][1].headers.Authorization).toBe("Bearer new-token");
expect(fetchFn.mock.calls[1][1].headers["x-grok-client-version"]).toBe("0.2.99");
});
});
+49
View File
@@ -101,6 +101,35 @@ describe("parseGrokCliBilling", () => {
});
expect(parsed.plan).toBe("Super Grok");
});
it("does not report paid subscription access as depleted on-demand credit", () => {
const parsed = parseGrokCliBilling(EXHAUSTED_BILLING, {
...USER_PROFILE,
subscriptionTier: "XPremiumPlus",
});
expect(parsed.plan).toBe("XPremiumPlus");
expect(parsed.subscriptionAccess).toBe(true);
expect(parsed.quotas).toEqual({});
expect(parsed.exhausted).toBe(false);
});
it("maps current monthly fields and snake-case subscription tier", () => {
const parsed = parseGrokCliBilling({
monthlyLimit: { val: 1000 },
includedUsed: { val: 275 },
totalUsed: { val: 300 },
resetAt: "2026-08-01T00:00:00Z",
}, {
subscription_tier: "premium_plus",
});
expect(parsed.plan).toBe("Premium Plus");
expect(parsed.quotas["Monthly included"]).toMatchObject({
used: 275,
total: 1000,
remainingPercentage: 72.5,
resetAt: "2026-08-01T00:00:00.000Z",
});
});
});
describe("getUsageForProvider(grok-cli)", () => {
@@ -140,6 +169,8 @@ describe("getUsageForProvider(grok-cli)", () => {
expect(billingCall[0]).toContain("/v1/billing");
expect(billingCall[1].headers.Authorization).toBe("Bearer test-token");
expect(billingCall[1].headers["x-xai-token-auth"]).toBe("xai-grok-cli");
expect(billingCall[1].headers["x-grok-client-version"]).toBe("0.2.99");
expect(billingCall[1].headers["x-grok-client-identifier"]).toBe("grok-shell");
expect(billingCall[1].headers["x-userid"]).toBe(
"d84768dd-224d-4052-ba49-0d336fa9160c",
);
@@ -174,6 +205,24 @@ describe("getUsageForProvider(grok-cli)", () => {
expect(usage.quotas["On-demand"].remainingPercentage).toBe(0);
expect(usage.quotas["On-demand"].total).toBe(1);
});
it("reports active paid access when provider exposes no numeric quota", async () => {
proxyAwareFetch
.mockResolvedValueOnce(jsonResponse(EXHAUSTED_BILLING))
.mockResolvedValueOnce(jsonResponse({
...USER_PROFILE,
subscriptionTier: "XPremiumPlus",
}));
const usage = await getUsageForProvider({
provider: "grok-cli",
accessToken: "test-token",
});
expect(usage.plan).toBe("XPremiumPlus");
expect(usage.message).toMatch(/active.*numeric included quota/i);
expect(usage.quotas).toEqual({});
});
});
describe("parseQuotaData(grok-cli)", () => {
@@ -138,21 +138,21 @@ describe("openai ↔ responses multi-turn reasoning", () => {
});
describe("GrokCliExecutor multi-turn input", () => {
it("keeps reasoning items (incl. encrypted_content) and strips only server message ids", () => {
it("keeps native Grok reasoning and item ids", () => {
_resetGrokCliTurnStore();
const executor = new GrokCliExecutor();
const body = {
model: "grok-4.5",
input: [
{ type: "message", role: "system", content: "You are Grok" },
{ type: "message", role: "user", content: "hi", id: "msg_server_prev" },
{ type: "message", role: "user", content: "hi", id: "msg_3e3f6187-892a-96db-893b-904eff019e19" },
{
type: "reasoning",
id: "rs_server_prev",
id: "rs_3e3f6187-892a-96db-893b-904eff019e19",
summary: [{ type: "summary_text", text: "prior plan" }],
encrypted_content: "enc_from_cli",
},
{ type: "message", role: "assistant", content: "hello", id: "msg_server_asst" },
{ type: "message", role: "assistant", content: "hello", id: "msg_4e3f6187-892a-96db-893b-904eff019e19" },
{ type: "message", role: "user", content: "again" },
],
include: ["reasoning.encrypted_content"],
@@ -166,14 +166,13 @@ describe("GrokCliExecutor multi-turn input", () => {
expect(reasoning).toHaveLength(1);
expect(reasoning[0].encrypted_content).toBe("enc_from_cli");
expect(reasoning[0].summary?.[0]?.text).toBe("prior plan");
// server id stripped from reasoning item, content kept
expect(reasoning[0].id).toBeUndefined();
expect(reasoning[0].id).toBe("rs_3e3f6187-892a-96db-893b-904eff019e19");
// system preserved (not developer)
expect(out.input[0].role).toBe("system");
// message server ids stripped
// Native Grok IDs are required for encrypted continuity.
for (const item of out.input) {
if (item.type === "message") expect(item.id).toBeUndefined();
if (item.type === "message" && item.id) expect(item.id).toMatch(/^msg_[0-9a-f-]{36}$/);
}
expect(out.include).toContain("reasoning.encrypted_content");
expect(out.store).toBe(false);