Add xAI's Grok as a new web_search provider alongside Brave and Perplexity.
Uses the xAI /v1/responses API with tools: [{type: "web_search"}].
Configuration:
- tools.web.search.provider: "grok"
- tools.web.search.grok.apiKey or XAI_API_KEY env var
- tools.web.search.grok.model (default: grok-4-1-fast)
- tools.web.search.grok.inlineCitations (optional, embeds markdown links)
Returns AI-synthesized answers with citations similar to Perplexity.
108 lines
3.5 KiB
TypeScript
108 lines
3.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { __testing } from "./web-search.js";
|
|
|
|
const {
|
|
inferPerplexityBaseUrlFromApiKey,
|
|
resolvePerplexityBaseUrl,
|
|
normalizeFreshness,
|
|
resolveGrokApiKey,
|
|
resolveGrokModel,
|
|
resolveGrokInlineCitations,
|
|
} = __testing;
|
|
|
|
describe("web_search perplexity baseUrl defaults", () => {
|
|
it("detects a Perplexity key prefix", () => {
|
|
expect(inferPerplexityBaseUrlFromApiKey("pplx-123")).toBe("direct");
|
|
});
|
|
|
|
it("detects an OpenRouter key prefix", () => {
|
|
expect(inferPerplexityBaseUrlFromApiKey("sk-or-v1-123")).toBe("openrouter");
|
|
});
|
|
|
|
it("returns undefined for unknown key formats", () => {
|
|
expect(inferPerplexityBaseUrlFromApiKey("unknown-key")).toBeUndefined();
|
|
});
|
|
|
|
it("prefers explicit baseUrl over key-based defaults", () => {
|
|
expect(resolvePerplexityBaseUrl({ baseUrl: "https://example.com" }, "config", "pplx-123")).toBe(
|
|
"https://example.com",
|
|
);
|
|
});
|
|
|
|
it("defaults to direct when using PERPLEXITY_API_KEY", () => {
|
|
expect(resolvePerplexityBaseUrl(undefined, "perplexity_env")).toBe("https://api.perplexity.ai");
|
|
});
|
|
|
|
it("defaults to OpenRouter when using OPENROUTER_API_KEY", () => {
|
|
expect(resolvePerplexityBaseUrl(undefined, "openrouter_env")).toBe(
|
|
"https://openrouter.ai/api/v1",
|
|
);
|
|
});
|
|
|
|
it("defaults to direct when config key looks like Perplexity", () => {
|
|
expect(resolvePerplexityBaseUrl(undefined, "config", "pplx-123")).toBe(
|
|
"https://api.perplexity.ai",
|
|
);
|
|
});
|
|
|
|
it("defaults to OpenRouter when config key looks like OpenRouter", () => {
|
|
expect(resolvePerplexityBaseUrl(undefined, "config", "sk-or-v1-123")).toBe(
|
|
"https://openrouter.ai/api/v1",
|
|
);
|
|
});
|
|
|
|
it("defaults to OpenRouter for unknown config key formats", () => {
|
|
expect(resolvePerplexityBaseUrl(undefined, "config", "weird-key")).toBe(
|
|
"https://openrouter.ai/api/v1",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("web_search freshness normalization", () => {
|
|
it("accepts Brave shortcut values", () => {
|
|
expect(normalizeFreshness("pd")).toBe("pd");
|
|
expect(normalizeFreshness("PW")).toBe("pw");
|
|
});
|
|
|
|
it("accepts valid date ranges", () => {
|
|
expect(normalizeFreshness("2024-01-01to2024-01-31")).toBe("2024-01-01to2024-01-31");
|
|
});
|
|
|
|
it("rejects invalid date ranges", () => {
|
|
expect(normalizeFreshness("2024-13-01to2024-01-31")).toBeUndefined();
|
|
expect(normalizeFreshness("2024-02-30to2024-03-01")).toBeUndefined();
|
|
expect(normalizeFreshness("2024-03-10to2024-03-01")).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("web_search grok config resolution", () => {
|
|
it("uses config apiKey when provided", () => {
|
|
expect(resolveGrokApiKey({ apiKey: "xai-test-key" })).toBe("xai-test-key");
|
|
});
|
|
|
|
it("returns undefined when no apiKey is available", () => {
|
|
expect(resolveGrokApiKey({})).toBeUndefined();
|
|
expect(resolveGrokApiKey(undefined)).toBeUndefined();
|
|
});
|
|
|
|
it("uses default model when not specified", () => {
|
|
expect(resolveGrokModel({})).toBe("grok-4-1-fast");
|
|
expect(resolveGrokModel(undefined)).toBe("grok-4-1-fast");
|
|
});
|
|
|
|
it("uses config model when provided", () => {
|
|
expect(resolveGrokModel({ model: "grok-3" })).toBe("grok-3");
|
|
});
|
|
|
|
it("defaults inlineCitations to false", () => {
|
|
expect(resolveGrokInlineCitations({})).toBe(false);
|
|
expect(resolveGrokInlineCitations(undefined)).toBe(false);
|
|
});
|
|
|
|
it("respects inlineCitations config", () => {
|
|
expect(resolveGrokInlineCitations({ inlineCitations: true })).toBe(true);
|
|
expect(resolveGrokInlineCitations({ inlineCitations: false })).toBe(false);
|
|
});
|
|
});
|