Skip to content

Commit 5592ad7

Browse files
committed
Address CodeRabbit review: use URL.hostname, bracket notation, remove changeset
1 parent 768e0f6 commit 5592ad7

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

‎.changeset/fix-grok-xai-false-positive.md‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,29 +1060,54 @@ describe("OpenAiHandler", () => {
10601060
openAiModelId: "gpt-4o",
10611061
}
10621062
const handler = new OpenAiHandler(nonGrokOptions)
1063-
// @ts-expect-error - accessing private method for testing
1064-
expect(handler._isGrokXAI(nonGrokOptions.openAiBaseUrl)).toBe(false)
1063+
expect(handler["_isGrokXAI"](nonGrokOptions.openAiBaseUrl)).toBe(false)
10651064
})
10661065

10671066
it("should NOT detect as Grok xAI for other domains containing 'x.ai' substring (e.g. fox.ai, max.ai)", () => {
10681067
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)
1068+
expect(handler["_isGrokXAI"]("https://fox.ai/v1")).toBe(false)
1069+
expect(handler["_isGrokXAI"]("https://max.ai/v1")).toBe(false)
10741070
})
10751071

10761072
it("should detect as Grok xAI for api.x.ai", () => {
10771073
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)
1074+
expect(handler["_isGrokXAI"]("https://api.x.ai/v1")).toBe(true)
10801075
})
10811076

10821077
it("should detect as Grok xAI for subdomains of x.ai (e.g. custom.x.ai)", () => {
10831078
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)
1079+
expect(handler["_isGrokXAI"]("https://custom.x.ai/v1")).toBe(true)
1080+
})
1081+
1082+
it("should detect as Grok xAI when api.x.ai uses a non-default port", () => {
1083+
const handler = new OpenAiHandler({ ...mockOptions, openAiBaseUrl: "https://api.x.ai:8443/v1" })
1084+
expect(handler["_isGrokXAI"]("https://api.x.ai:8443/v1")).toBe(true)
1085+
})
1086+
1087+
it("should exclude stream_options when streaming with api.x.ai on a non-default port", async () => {
1088+
const portOptions = {
1089+
...mockOptions,
1090+
openAiBaseUrl: "https://api.x.ai:8443/v1",
1091+
openAiModelId: "grok-1",
1092+
}
1093+
const handler = new OpenAiHandler(portOptions)
1094+
const systemPrompt = "You are a helpful assistant."
1095+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello!" }]
1096+
1097+
const stream = handler.createMessage(systemPrompt, messages)
1098+
await stream.next()
1099+
1100+
expect(mockCreate).toHaveBeenCalledWith(
1101+
expect.objectContaining({
1102+
model: portOptions.openAiModelId,
1103+
stream: true,
1104+
}),
1105+
{},
1106+
)
1107+
1108+
const mockCalls = mockCreate.mock.calls
1109+
const lastCall = mockCalls[mockCalls.length - 1]
1110+
expect(lastCall[0]).not.toHaveProperty("stream_options")
10861111
})
10871112

10881113
it("should include stream_options when using a non-Grok provider whose URL contains 'x.ai' substring", async () => {

‎src/api/providers/openai.ts‎

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

511511
protected _getUrlHost(baseUrl?: string): string {
512512
try {
513-
return new URL(baseUrl ?? "").host
513+
return new URL(baseUrl ?? "").hostname
514514
} catch (error) {
515515
return ""
516516
}

0 commit comments

Comments
 (0)