Skip to content

Commit 768e0f6

Browse files
committed
fix: _isGrokXAI false-positive substring match breaks token usage for domains containing 'x.ai'
Fixes #1483 The _isGrokXAI() method used urlHost.includes('x.ai') which matches any domain containing 'x.ai' as a substring (e.g. box.ai, fox.ai, max.ai). This false-positive causes stream_options:{include_usage:true} to be omitted, so the API never returns usage data and the token bar shows 0. Fix: Use exact host match (api.x.ai) or subdomain match (*.x.ai) instead of substring includes. Added tests for false-positive scenarios and valid x.ai domain detection. AI-assisted: developed with Zoo Code/GLM-5.2, reviewed and verified by the contributor.
1 parent a5f4192 commit 768e0f6

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"zoo-code": patch
3+
---
4+
5+
Fix `_isGrokXAI()` false-positive substring match that broke token usage for OpenAI-compatible providers whose domain contains "x.ai" as a substring (e.g. box.ai, fox.ai, max.ai).
6+
7+
The `_isGrokXAI()` method in `src/api/providers/openai.ts` used `urlHost.includes("x.ai")` which is a substring match. Any domain containing "x.ai" anywhere in its host (e.g. `box.ai`, `fox.ai`, `max.ai`) was falsely identified as a Grok/xAI endpoint. This caused `stream_options: { include_usage: true }` to be omitted from API requests in both `createMessage()` and `handleO3FamilyMessage()`, so the API never returned usage data and the token bar showed 0 — a silent failure with no error message.
8+
9+
Fixed by using exact host match (`api.x.ai`) or subdomain suffix check (`.x.ai`) instead of substring `includes()`.

‎src/api/providers/__tests__/openai.spec.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,67 @@ describe("OpenAiHandler", () => {
10521052
})
10531053
})
10541054

1055+
describe("Grok xAI false-positive prevention", () => {
1056+
it("should NOT detect as Grok xAI when host contains 'x.ai' as a substring but is not x.ai (e.g. box.ai)", () => {
1057+
const nonGrokOptions = {
1058+
...mockOptions,
1059+
openAiBaseUrl: "https://box.ai/v1",
1060+
openAiModelId: "gpt-4o",
1061+
}
1062+
const handler = new OpenAiHandler(nonGrokOptions)
1063+
// @ts-expect-error - accessing private method for testing
1064+
expect(handler._isGrokXAI(nonGrokOptions.openAiBaseUrl)).toBe(false)
1065+
})
1066+
1067+
it("should NOT detect as Grok xAI for other domains containing 'x.ai' substring (e.g. fox.ai, max.ai)", () => {
1068+
const handler = new OpenAiHandler({ ...mockOptions, openAiBaseUrl: "https://fox.ai/v1" })
1069+
// @ts-expect-error - accessing private method for testing
1070+
expect(handler._isGrokXAI("https://fox.ai/v1")).toBe(false)
1071+
1072+
// @ts-expect-error - accessing private method for testing
1073+
expect(handler._isGrokXAI("https://max.ai/v1")).toBe(false)
1074+
})
1075+
1076+
it("should detect as Grok xAI for api.x.ai", () => {
1077+
const handler = new OpenAiHandler({ ...mockOptions, openAiBaseUrl: "https://api.x.ai/v1" })
1078+
// @ts-expect-error - accessing private method for testing
1079+
expect(handler._isGrokXAI("https://api.x.ai/v1")).toBe(true)
1080+
})
1081+
1082+
it("should detect as Grok xAI for subdomains of x.ai (e.g. custom.x.ai)", () => {
1083+
const handler = new OpenAiHandler({ ...mockOptions, openAiBaseUrl: "https://custom.x.ai/v1" })
1084+
// @ts-expect-error - accessing private method for testing
1085+
expect(handler._isGrokXAI("https://custom.x.ai/v1")).toBe(true)
1086+
})
1087+
1088+
it("should include stream_options when using a non-Grok provider whose URL contains 'x.ai' substring", async () => {
1089+
const nonGrokOptions = {
1090+
...mockOptions,
1091+
openAiBaseUrl: "https://box.ai/v1",
1092+
openAiModelId: "gpt-4o",
1093+
}
1094+
const handler = new OpenAiHandler(nonGrokOptions)
1095+
const systemPrompt = "You are a helpful assistant."
1096+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello!" }]
1097+
1098+
const stream = handler.createMessage(systemPrompt, messages)
1099+
await stream.next()
1100+
1101+
expect(mockCreate).toHaveBeenCalledWith(
1102+
expect.objectContaining({
1103+
model: nonGrokOptions.openAiModelId,
1104+
stream: true,
1105+
}),
1106+
{},
1107+
)
1108+
1109+
const mockCalls = mockCreate.mock.calls
1110+
const lastCall = mockCalls[mockCalls.length - 1]
1111+
expect(lastCall[0]).toHaveProperty("stream_options")
1112+
expect(lastCall[0].stream_options).toEqual({ include_usage: true })
1113+
})
1114+
})
1115+
10551116
describe("O3 Family Models", () => {
10561117
const o3Options = {
10571118
...mockOptions,

‎src/api/providers/openai.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
518518

519519
private _isGrokXAI(baseUrl?: string): boolean {
520520
const urlHost = this._getUrlHost(baseUrl)
521-
return urlHost.includes("x.ai")
521+
return urlHost === "api.x.ai" || urlHost.endsWith(".x.ai")
522522
}
523523

524524
protected _isAzureAiInference(baseUrl?: string): boolean {

0 commit comments

Comments
 (0)