feat(ts-sdk): add xAI (Grok) LLM provider to OSS SDK (#6115)

This commit is contained in:
Kartik
2026-07-07 11:29:56 +05:30
committed by GitHub
parent 9d36b2c94d
commit 002fe46ab3
7 changed files with 262 additions and 18 deletions
+50
View File
@@ -0,0 +1,50 @@
import { OpenAILLM } from "./openai";
import { LLMConfig, Message } from "../types";
import { LLMResponse } from "./base";
/**
* xAI (Grok) LLM provider.
*
* xAI's Grok API is OpenAI-compatible, so this simply reuses {@link OpenAILLM}
* and overrides the connection defaults — mirroring `mem0/llms/xai.py` in the
* Python SDK. The API key resolves from `config.apiKey` or the `XAI_API_KEY`
* env var, and the base URL from `config.baseURL`, `XAI_API_BASE`, else
* `https://api.x.ai/v1`.
*/
export class XAILLM extends OpenAILLM {
constructor(config: LLMConfig) {
const apiKey = config.apiKey || process.env.XAI_API_KEY;
if (!apiKey) {
throw new Error("xAI API key is required");
}
super({
...config,
apiKey,
baseURL:
config.baseURL || process.env.XAI_API_BASE || "https://api.x.ai/v1",
model: config.model || "grok-4.3",
});
}
async generateResponse(
messages: Message[],
responseFormat?: { type: string },
tools?: any[],
): Promise<string | LLMResponse> {
try {
return await super.generateResponse(messages, responseFormat, tools);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(`xAI LLM failed: ${message}`);
}
}
async generateChat(messages: Message[]): Promise<LLMResponse> {
try {
return await super.generateChat(messages);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(`xAI LLM failed: ${message}`);
}
}
}
+3
View File
@@ -24,6 +24,7 @@ import { ValkeyDB } from "../vector_stores/valkey";
import { OllamaLLM } from "../llms/ollama";
import { LMStudioLLM } from "../llms/lmstudio";
import { DeepSeekLLM } from "../llms/deepseek";
import { XAILLM } from "../llms/xai";
import { LiteLLM } from "../llms/litellm";
import { MiniMaxLLM } from "../llms/minimax";
import { VllmLLM } from "../llms/vllm";
@@ -102,6 +103,8 @@ export class LLMFactory {
return new LangchainLLM(config);
case "deepseek":
return new DeepSeekLLM(config);
case "xai":
return new XAILLM(config);
case "litellm":
return new LiteLLM(config);
case "minimax":
@@ -102,6 +102,11 @@ jest.mock("../src/llms/deepseek", () => ({
.fn()
.mockImplementation((config) => ({ type: "deepseek-llm", config })),
}));
jest.mock("../src/llms/xai", () => ({
XAILLM: jest
.fn()
.mockImplementation((config) => ({ type: "xai-llm", config })),
}));
jest.mock("../src/llms/litellm", () => ({
LiteLLM: jest
.fn()
@@ -258,6 +263,7 @@ describe("LLMFactory", () => {
["langchain"],
["lmstudio"],
["deepseek"],
["xai"],
["litellm"],
["minimax"],
["vllm"],
+153
View File
@@ -0,0 +1,153 @@
/// <reference types="jest" />
/**
* xAI (Grok) LLM — unit tests (mocked OpenAI).
*/
import { XAILLM } from "../src/llms/xai";
const mockCreate = jest.fn();
const mockOpenAICtor = jest.fn();
jest.mock("openai", () => {
return jest.fn().mockImplementation((config) => {
mockOpenAICtor(config);
return { chat: { completions: { create: mockCreate } } };
});
});
describe("XAILLM (unit)", () => {
const ORIGINAL_ENV = process.env;
beforeEach(() => {
jest.clearAllMocks();
process.env = { ...ORIGINAL_ENV };
delete process.env.XAI_API_KEY;
delete process.env.XAI_API_BASE;
mockCreate.mockResolvedValue({
choices: [{ message: { content: "hi", role: "assistant" } }],
});
});
afterAll(() => {
process.env = ORIGINAL_ENV;
});
it("defaults to grok-4.3 and the xAI base URL (matching the Python provider)", async () => {
const llm = new XAILLM({ apiKey: "test-key" });
const result = await llm.generateResponse([
{ role: "user", content: "hello" },
]);
expect(mockOpenAICtor).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: "test-key",
baseURL: "https://api.x.ai/v1",
}),
);
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({ model: "grok-4.3" }),
);
expect(result).toBe("hi");
});
it("resolves XAI_API_KEY / XAI_API_BASE from the environment", () => {
process.env.XAI_API_KEY = "env-key";
process.env.XAI_API_BASE = "https://custom.x.ai/v1";
new XAILLM({});
expect(mockOpenAICtor).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: "env-key",
baseURL: "https://custom.x.ai/v1",
}),
);
});
it("prefers explicit config over defaults and the environment", async () => {
process.env.XAI_API_KEY = "env-key";
const llm = new XAILLM({
apiKey: "explicit-key",
baseURL: "https://proxy.example.com/v1",
model: "grok-420-reasoning",
});
await llm.generateResponse([{ role: "user", content: "hello" }]);
expect(mockOpenAICtor).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: "explicit-key",
baseURL: "https://proxy.example.com/v1",
}),
);
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({ model: "grok-420-reasoning" }),
);
});
it("throws when no API key is provided", () => {
expect(() => new XAILLM({})).toThrow("xAI API key is required");
});
it("generateResponse() handles tool calls", async () => {
mockCreate.mockResolvedValueOnce({
choices: [
{
message: {
content: "",
role: "assistant",
tool_calls: [
{
function: { name: "get_weather", arguments: '{"city": "SF"}' },
},
],
},
},
],
});
const llm = new XAILLM({ apiKey: "test-key" });
const result = await llm.generateResponse(
[{ role: "user", content: "weather?" }],
undefined,
[{ type: "function", function: { name: "get_weather" } }],
);
expect(result).toEqual({
content: "",
role: "assistant",
toolCalls: [{ name: "get_weather", arguments: '{"city": "SF"}' }],
});
});
it("generateResponse() wraps downstream errors with an xAI-specific message", async () => {
mockCreate.mockRejectedValueOnce(new Error("Connection refused"));
const llm = new XAILLM({ apiKey: "test-key" });
await expect(
llm.generateResponse([{ role: "user", content: "hi" }]),
).rejects.toThrow("xAI LLM failed: Connection refused");
});
it("generateChat() returns the LLMResponse shape", async () => {
mockCreate.mockResolvedValueOnce({
choices: [{ message: { content: "I can help.", role: "assistant" } }],
});
const llm = new XAILLM({ apiKey: "test-key" });
const result = await llm.generateChat([
{ role: "user", content: "help me" },
]);
expect(result).toEqual({ content: "I can help.", role: "assistant" });
});
it("generateChat() wraps downstream errors with an xAI-specific message", async () => {
mockCreate.mockRejectedValueOnce(new Error("Timeout"));
const llm = new XAILLM({ apiKey: "test-key" });
await expect(
llm.generateChat([{ role: "user", content: "hi" }]),
).rejects.toThrow("xAI LLM failed: Timeout");
});
});