mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-23 04:09:49 +00:00
feat(grok-cli): add Grok CLI / Grok Build provider with OAuth device-code flow (#2502)
New OAuth provider routing through cli-chat-proxy.grok.com (OpenAI Responses
API), distinct from xai (api.x.ai) and grok-web (cookie SSO):
- Registry + GrokCliExecutor: Chat Completions -> Responses transform, CLI
fingerprint headers, virtual effort models grok-4.5-{low,medium,high}
- OAuth device-code flow (auth.x.ai) with no-PKCE, shared xAI token refresh
- store=false multi-turn continuity via reasoning encrypted_content
- Quota tracker: on-demand window + prepaid balance on dashboard
- Connection test: 402 spending-limit = soft success (auth OK, out of credits)
- Alias/oauth/provider baselines + unit tests
This commit is contained in:
committed by
decolua
parent
c73c419d09
commit
a11937cdd6
@@ -0,0 +1,202 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
vi.mock("../../open-sse/utils/proxyFetch.js", () => ({
|
||||
proxyAwareFetch: vi.fn(),
|
||||
}));
|
||||
|
||||
import { proxyAwareFetch } from "../../open-sse/utils/proxyFetch.js";
|
||||
import { getUsageForProvider } from "../../open-sse/services/usage.js";
|
||||
import { parseGrokCliBilling } from "../../open-sse/services/usage/grok-cli.js";
|
||||
import { USAGE_SUPPORTED_PROVIDERS } from "../../src/shared/constants/providers.js";
|
||||
import { PROVIDERS } from "../../open-sse/providers/index.js";
|
||||
import { parseQuotaData } from "../../src/app/(dashboard)/dashboard/usage/components/ProviderLimits/utils.js";
|
||||
|
||||
function jsonResponse(body, status = 200) {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const EXHAUSTED_BILLING = {
|
||||
config: {
|
||||
currentPeriod: {
|
||||
type: "USAGE_PERIOD_TYPE_WEEKLY",
|
||||
start: "2026-07-08T00:00:00+00:00",
|
||||
end: "2026-07-15T00:00:00+00:00",
|
||||
},
|
||||
onDemandCap: { val: 0 },
|
||||
onDemandUsed: { val: 0 },
|
||||
isUnifiedBillingUser: true,
|
||||
prepaidBalance: { val: 0 },
|
||||
topUpMethod: "TOP_UP_METHOD_SAVED_PAYMENT_METHOD",
|
||||
billingPeriodStart: "2026-07-08T00:00:00+00:00",
|
||||
billingPeriodEnd: "2026-07-15T00:00:00+00:00",
|
||||
},
|
||||
};
|
||||
|
||||
const ACTIVE_BILLING = {
|
||||
config: {
|
||||
currentPeriod: {
|
||||
type: "USAGE_PERIOD_TYPE_WEEKLY",
|
||||
start: "2026-07-08T00:00:00+00:00",
|
||||
end: "2026-07-15T00:00:00+00:00",
|
||||
},
|
||||
onDemandCap: { val: 100 },
|
||||
onDemandUsed: { val: 35 },
|
||||
isUnifiedBillingUser: true,
|
||||
prepaidBalance: { val: 12.5 },
|
||||
billingPeriodStart: "2026-07-08T00:00:00+00:00",
|
||||
billingPeriodEnd: "2026-07-15T00:00:00+00:00",
|
||||
},
|
||||
};
|
||||
|
||||
const USER_PROFILE = {
|
||||
userId: "d84768dd-224d-4052-ba49-0d336fa9160c",
|
||||
email: "user@example.com",
|
||||
hasGrokCodeAccess: true,
|
||||
subscriptionTier: null,
|
||||
};
|
||||
|
||||
describe("grok-cli registry usage flag", () => {
|
||||
it("exposes transport.usage urls", () => {
|
||||
const cfg = PROVIDERS["grok-cli"];
|
||||
expect(cfg.usage?.url).toContain("/v1/billing");
|
||||
expect(cfg.usage?.userUrl).toContain("/v1/user");
|
||||
});
|
||||
|
||||
it("is listed in USAGE_SUPPORTED_PROVIDERS", () => {
|
||||
expect(USAGE_SUPPORTED_PROVIDERS).toContain("grok-cli");
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseGrokCliBilling", () => {
|
||||
it("maps on-demand cap/used + prepaid balance", () => {
|
||||
const parsed = parseGrokCliBilling(ACTIVE_BILLING, USER_PROFILE);
|
||||
expect(parsed.plan).toBe("Grok Code");
|
||||
expect(parsed.quotas["On-demand"]).toMatchObject({
|
||||
used: 35,
|
||||
total: 100,
|
||||
remainingPercentage: 65,
|
||||
});
|
||||
// Prepaid is remaining-balance style: 0 used of current pot
|
||||
expect(parsed.quotas.Prepaid).toMatchObject({
|
||||
used: 0,
|
||||
total: 12.5,
|
||||
remainingPercentage: 100,
|
||||
});
|
||||
expect(parsed.exhausted).toBe(false);
|
||||
});
|
||||
|
||||
it("marks depleted free/promo account as exhausted", () => {
|
||||
const parsed = parseGrokCliBilling(EXHAUSTED_BILLING, USER_PROFILE);
|
||||
expect(parsed.quotas["On-demand"].remainingPercentage).toBe(0);
|
||||
expect(parsed.exhausted).toBe(true);
|
||||
});
|
||||
|
||||
it("uses subscriptionTier for plan when present", () => {
|
||||
const parsed = parseGrokCliBilling(ACTIVE_BILLING, {
|
||||
...USER_PROFILE,
|
||||
subscriptionTier: "super_grok",
|
||||
});
|
||||
expect(parsed.plan).toBe("Super Grok");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUsageForProvider(grok-cli)", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns normalized quotas from billing + user endpoints", async () => {
|
||||
proxyAwareFetch
|
||||
.mockResolvedValueOnce(jsonResponse(ACTIVE_BILLING))
|
||||
.mockResolvedValueOnce(jsonResponse(USER_PROFILE));
|
||||
|
||||
const usage = await getUsageForProvider({
|
||||
provider: "grok-cli",
|
||||
accessToken: "test-token",
|
||||
providerSpecificData: {
|
||||
email: "user@example.com",
|
||||
userId: "d84768dd-224d-4052-ba49-0d336fa9160c",
|
||||
},
|
||||
});
|
||||
|
||||
expect(usage.message).toBeUndefined();
|
||||
expect(usage.plan).toBe("Grok Code");
|
||||
expect(usage.quotas["On-demand"]).toMatchObject({
|
||||
used: 35,
|
||||
total: 100,
|
||||
remainingPercentage: 65,
|
||||
});
|
||||
expect(usage.quotas.Prepaid).toMatchObject({
|
||||
used: 0,
|
||||
total: 12.5,
|
||||
remainingPercentage: 100,
|
||||
});
|
||||
|
||||
// Official CLI fingerprint headers
|
||||
const billingCall = proxyAwareFetch.mock.calls[0];
|
||||
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-userid"]).toBe(
|
||||
"d84768dd-224d-4052-ba49-0d336fa9160c",
|
||||
);
|
||||
});
|
||||
|
||||
it("surfaces auth-expired message on 401", async () => {
|
||||
proxyAwareFetch
|
||||
.mockResolvedValueOnce(jsonResponse({ error: "unauthorized" }, 401))
|
||||
.mockResolvedValueOnce(jsonResponse(USER_PROFILE));
|
||||
|
||||
const usage = await getUsageForProvider({
|
||||
provider: "grok-cli",
|
||||
accessToken: "expired",
|
||||
});
|
||||
|
||||
expect(usage.message).toMatch(/expired|re-authorize/i);
|
||||
});
|
||||
|
||||
it("returns depleted on-demand bar without blocking message when cap is zero", async () => {
|
||||
proxyAwareFetch
|
||||
.mockResolvedValueOnce(jsonResponse(EXHAUSTED_BILLING))
|
||||
.mockResolvedValueOnce(jsonResponse(USER_PROFILE));
|
||||
|
||||
const usage = await getUsageForProvider({
|
||||
provider: "grok-cli",
|
||||
accessToken: "test-token",
|
||||
});
|
||||
|
||||
// Dashboard hides QuotaTable when `message` is set — keep message empty
|
||||
// so the 0% bar still renders for exhausted free/promo accounts.
|
||||
expect(usage.message).toBeUndefined();
|
||||
expect(usage.quotas["On-demand"].remainingPercentage).toBe(0);
|
||||
expect(usage.quotas["On-demand"].total).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseQuotaData(grok-cli)", () => {
|
||||
it("forwards remainingPercentage for dashboard bars", () => {
|
||||
const rows = parseQuotaData("grok-cli", {
|
||||
plan: "Grok Code",
|
||||
quotas: {
|
||||
"On-demand": {
|
||||
used: 35,
|
||||
total: 100,
|
||||
remaining: 65,
|
||||
remainingPercentage: 65,
|
||||
resetAt: "2026-07-15T00:00:00.000Z",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]).toMatchObject({
|
||||
name: "On-demand",
|
||||
used: 35,
|
||||
total: 100,
|
||||
remainingPercentage: 65,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user