/** * Real-API provider test matrix. Runs each capability against every provider with the * owner keys from .env and writes docs/provider-test-matrix.md. Each cell is ✅ only when * the real request succeeded. Cheap prompts, small max tokens. * * pnpm providers:matrix → all providers * pnpm providers:matrix anthropic → one provider */ import "./load-env"; import fs from "node:fs"; import zlib from "node:zlib"; import { getAdapter } from "@/lib/ai/providers"; import { ownerKey } from "@/lib/ai/providers/env-keys"; import { PROVIDER_IDS, type PolyModel, type ProviderId, type UnifiedChatRequest, type UnifiedStreamEvent } from "@/lib/ai/core/types"; const KEYS = Object.fromEntries(PROVIDER_IDS.map((p) => [p, ownerKey(p)])) as Record; /** Cheap, current models per provider used for the matrix. */ const TEST_MODELS: Record = { openai: { text: "gpt-5.4-mini", reasoning: "gpt-5.4-mini", vision: "gpt-5.4-mini" }, anthropic: { text: "claude-haiku-4-5", reasoning: "claude-sonnet-5", vision: "claude-haiku-4-5" }, gemini: { text: "gemini-3.5-flash-lite", reasoning: "gemini-3.5-flash-lite", vision: "gemini-3.5-flash-lite" }, xai: { text: "grok-4.20-0309-non-reasoning", reasoning: "grok-4.3", vision: "grok-4.20-0309-non-reasoning" }, mistral: { text: "mistral-small-latest", reasoning: "magistral-medium-latest", vision: "mistral-small-latest" }, deepseek: { text: "deepseek-v4-flash", reasoning: "deepseek-v4-flash", vision: "deepseek-v4-flash-vision-exp" }, kimi: { text: "kimi-k2.6", reasoning: "kimi-k3", vision: "kimi-k2.6" }, openrouter: { text: "openai/gpt-5.4-nano", reasoning: "openai/gpt-5.4-nano", vision: "openai/gpt-5.4-nano" }, cerebras: { text: "gemma-4-31b", reasoning: "gpt-oss-120b", vision: "gemma-4-31b" }, custom: { text: "", reasoning: "", vision: "" }, // per-user endpoints — never has an owner key, always skipped }; // 2x2 PNG (red, green / blue, white) — 32x32 needed for xAI minimums, so we scale via a bigger canvas. function makePng(size: number): string { // Simple uncompressed PNG writer (RGB) for a solid red square with a white diagonal. const crcTable: number[] = []; for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; crcTable[n] = c >>> 0; } const crc = (buf: Buffer) => { let c = 0xffffffff; for (const b of buf) c = crcTable[(c ^ b) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; }; const chunk = (type: string, data: Buffer) => { const len = Buffer.alloc(4); len.writeUInt32BE(data.length); const td = Buffer.concat([Buffer.from(type), data]); const c = Buffer.alloc(4); c.writeUInt32BE(crc(td)); return Buffer.concat([len, td, c]); }; const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(size, 0); ihdr.writeUInt32BE(size, 4); ihdr[8] = 8; ihdr[9] = 2; const raw = Buffer.alloc((size * 3 + 1) * size); for (let y = 0; y < size; y++) { raw[y * (size * 3 + 1)] = 0; for (let x = 0; x < size; x++) { const o = y * (size * 3 + 1) + 1 + x * 3; const diag = Math.abs(x - y) < 2; raw[o] = diag ? 255 : 220; raw[o + 1] = diag ? 255 : 30; raw[o + 2] = diag ? 255 : 30; } } return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", ihdr), chunk("IDAT", zlib.deflateSync(raw)), chunk("IEND", Buffer.alloc(0))]).toString("base64"); } type Cell = "✅" | "❌" | "—"; const ROWS = ["Auth", "Model list", "Text", "Streaming", "System", "Vision", "Tools", "Structured output", "Reasoning", "Token usage", "Invalid key", "Error normalization", "Long response"] as const; type Row = (typeof ROWS)[number]; async function collect(req: UnifiedChatRequest, attempt = 1): Promise>> { try { return await collectOnce(req); } catch (e) { const poly = (e as { poly?: { code: string; retryAfterMs?: number } }).poly; if (poly && (poly.code === "RATE_LIMITED" || poly.code === "PROVIDER_UNAVAILABLE") && attempt <= 3) { await new Promise((r) => setTimeout(r, Math.max(3000, poly.retryAfterMs ?? 0) * attempt)); return collect(req, attempt + 1); } throw e; } } async function collectOnce(req: UnifiedChatRequest) { const adapter = getAdapter(req.provider); const events: UnifiedStreamEvent[] = []; for await (const ev of adapter.streamChat(req)) events.push(ev); const err = events.find((e) => e.type === "error"); if (err && err.type === "error") throw Object.assign(new Error(err.error.message), { poly: err.error }); const text = events.filter((e) => e.type === "text-delta").map((e) => (e as { text: string }).text).join(""); const reasoning = events.filter((e) => e.type === "reasoning-delta").map((e) => (e as { text: string }).text).join(""); const usage = events.find((e) => e.type === "usage") as { usage: { inputTokens: number; outputTokens: number; reasoningTokens?: number } } | undefined; const tools = events.filter((e) => e.type === "tool-end") as { name: string; arguments: Record }[]; const deltas = events.filter((e) => e.type === "text-delta" || e.type === "reasoning-delta").length; return { text, reasoning, usage: usage?.usage, tools, deltas, finish: (events.find((e) => e.type === "finish") as { reason?: string } | undefined)?.reason }; } async function runProvider(p: ProviderId, results: Record>, notes: string[]) { const key = KEYS[p]; const adapter = getAdapter(p); const set = (r: Row, v: Cell) => (results[r][p] = v); if (!key) { for (const r of ROWS) set(r, "—"); notes.push(`${p}: no key in env`); return; } const note = (s: string) => notes.push(`${p}: ${s}`); const m = TEST_MODELS[p]; let models: PolyModel[] = []; try { const v = await adapter.validateApiKey(key); set("Auth", v.ok ? "✅" : "❌"); if (!v.ok) note(`auth: ${v.error?.code} ${v.error?.message}`); } catch (e) { set("Auth", "❌"); note(`auth threw: ${(e as Error).message}`); } try { models = await adapter.listModels(key); set("Model list", models.length ? "✅" : "❌"); note(`models: ${models.length} (${models.slice(0, 5).map((x) => x.id).join(", ")}…)`); } catch (e) { set("Model list", "❌"); note(`list: ${(e as Error).message}`); } // Aliases such as `claude-haiku-4-5` resolve to the dated snapshot listed by the provider. const info = (id: string) => models.find((x) => x.id === id) ?? models.find((x) => x.id.startsWith(`${id}-`)); const base = (model: string): UnifiedChatRequest => ({ provider: p, model, apiKey: key, messages: [{ role: "user", content: [{ type: "text", text: "Reply with exactly: OK" }] }], settings: { maxTokens: 2000 }, modelInfo: info(model) }); try { const r = await collect(base(m.text)); set("Text", r.text.trim().length ? "✅" : "❌"); set("Streaming", r.deltas >= 1 ? "✅" : "❌"); set("Token usage", r.usage && r.usage.inputTokens > 0 && r.usage.outputTokens > 0 ? "✅" : "❌"); note(`text(${m.text}): "${r.text.trim().slice(0, 40)}" deltas=${r.deltas} usage=${JSON.stringify(r.usage)} finish=${r.finish}`); } catch (e) { set("Text", "❌"); set("Streaming", "❌"); set("Token usage", "❌"); note(`text: ${(e as Error).message}`); } try { const r = await collect({ ...base(m.text), system: "You are a pirate. Always start your reply with 'Arr'.", messages: [{ role: "user", content: [{ type: "text", text: "Say hello in five words." }] }] }); set("System", /arr/i.test(r.text) ? "✅" : "❌"); note(`system: "${r.text.trim().slice(0, 60)}"`); } catch (e) { set("System", "❌"); note(`system: ${(e as Error).message}`); } try { const r = await collect({ ...base(m.vision), messages: [{ role: "user", content: [{ type: "image", mimeType: "image/png", data: makePng(64) }, { type: "text", text: "What is the dominant color of this image? One word." }] }] }); set("Vision", /red/i.test(r.text) ? "✅" : "❌"); note(`vision: "${r.text.trim().slice(0, 40)}"`); } catch (e) { set("Vision", "❌"); note(`vision: ${(e as Error).message}`); } try { const r = await collect({ ...base(m.text), messages: [{ role: "user", content: [{ type: "text", text: "What is 1234 * 5678? Use the calculator tool." }] }], tools: [{ name: "calculator", description: "Evaluate an arithmetic expression", parameters: { type: "object", properties: { expression: { type: "string" } }, required: ["expression"], additionalProperties: false }, strict: true }] }); set("Tools", r.tools.length && r.tools[0].name === "calculator" ? "✅" : "❌"); note(`tools: ${JSON.stringify(r.tools.map((t) => ({ n: t.name, a: t.arguments })))} finish=${r.finish}`); } catch (e) { set("Tools", "❌"); note(`tools: ${(e as Error).message}`); } try { const r = await collect({ ...base(m.text), settings: { maxTokens: 2000, responseFormat: { type: "json_schema", schema: { type: "object", properties: { city: { type: "string" }, country: { type: "string" } }, required: ["city", "country"], additionalProperties: false }, schemaName: "city" } }, messages: [{ role: "user", content: [{ type: "text", text: "Give the capital of Canada as JSON." }] }] }); let ok = false; try { const j = JSON.parse(r.text.trim().replace(/^```(?:json)?\s*|\s*```$/g, "")); ok = typeof j.city === "string" && typeof j.country === "string"; } catch { ok = false; } set("Structured output", ok ? "✅" : "❌"); note(`structured: ${r.text.trim().slice(0, 80)}`); } catch (e) { set("Structured output", "❌"); note(`structured: ${(e as Error).message}`); } try { const r = await collect({ ...base(m.reasoning), settings: { maxTokens: 6000, reasoningEffort: p === "anthropic" ? "high" : "low", includeReasoning: true }, messages: [{ role: "user", content: [{ type: "text", text: "How many prime numbers are there strictly between 1000 and 1100? Think it through carefully, then answer with just the number." }] }] }); const hasReasoning = r.reasoning.length > 0 || (r.usage?.reasoningTokens ?? 0) > 0; set("Reasoning", hasReasoning ? "✅" : "❌"); note(`reasoning(${m.reasoning}): text="${r.text.trim().slice(0, 30)}" reasoningChars=${r.reasoning.length} reasoningTokens=${r.usage?.reasoningTokens}`); } catch (e) { set("Reasoning", "❌"); note(`reasoning: ${(e as Error).message}`); } try { const badKeys: Record = { gemini: "AIzaSyInvalidKey0000000000000000000000", anthropic: "sk-ant-api03-invalid", xai: "xai-invalid", mistral: "invalidinvalidinvalidinvalid0000", openrouter: "sk-or-v1-invalid0000000000000000", cerebras: "csk-invalid00000000000000000000" }; const bad = await adapter.validateApiKey(badKeys[p] ?? "sk-invalid-0000000000000000"); set("Invalid key", !bad.ok && bad.error?.code === "INVALID_API_KEY" ? "✅" : "❌"); note(`invalid key → ${bad.error?.code} (${bad.error?.status})`); } catch (e) { set("Invalid key", "❌"); note(`invalid key threw: ${(e as Error).message}`); } try { await collect(base("definitely-not-a-model-xyz")); set("Error normalization", "❌"); } catch (e) { const poly = (e as { poly?: { code: string } }).poly; set("Error normalization", poly?.code === "MODEL_NOT_FOUND" || poly?.code === "INVALID_PARAMETER" ? "✅" : "❌"); note(`unknown model → ${poly?.code}: ${(e as Error).message.slice(0, 80)}`); } try { const r = await collect({ ...base(m.text), settings: { maxTokens: 3000 }, messages: [{ role: "user", content: [{ type: "text", text: "Write a 400-word story about a lighthouse keeper. Plain prose." }] }] }); set("Long response", r.text.split(/\s+/).length > 250 ? "✅" : "❌"); note(`long: ${r.text.split(/\s+/).length} words, ${r.deltas} deltas`); } catch (e) { set("Long response", "❌"); note(`long: ${(e as Error).message}`); } } async function main() { const only = process.argv[2] as ProviderId | undefined; const providers = only ? [only] : [...PROVIDER_IDS]; const results = Object.fromEntries(ROWS.map((r) => [r, Object.fromEntries(PROVIDER_IDS.map((p) => [p, "—" as Cell]))])) as Record>; const notes: string[] = []; await Promise.all(providers.map((p) => runProvider(p, results, notes))); // Merge with a previous full matrix so single-provider runs do not blank the others. if (only && fs.existsSync("docs/provider-test-matrix.md")) { const prev = fs.readFileSync("docs/provider-test-matrix.md", "utf8"); for (const r of ROWS) { const m = prev.match(new RegExp(`^\\| ${r.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")} \\| (.+) \\|$`, "m")); if (!m) continue; const cells = m[1].split(" | ") as Cell[]; PROVIDER_IDS.forEach((pp, i) => { if (pp !== only && cells[i]) results[r][pp] = cells[i]; }); } } const date = new Date().toISOString().slice(0, 10); const header = `| Capability | ${PROVIDER_IDS.map((p) => p).join(" | ")} |\n| --- | ${PROVIDER_IDS.map(() => ":-:").join(" | ")} |`; const lines = ROWS.map((r) => `| ${r} | ${PROVIDER_IDS.map((p) => results[r][p]).join(" | ")} |`); const md = `# Provider test matrix\n\nGenerated by \`pnpm providers:matrix\` against the REAL provider APIs on ${date}. A cell is ✅ only when the live request succeeded and the assertion passed. “—” = not run (no key).\n\nTest models: ${PROVIDER_IDS.map((p) => `${p}: ${TEST_MODELS[p].text} / reasoning ${TEST_MODELS[p].reasoning}`).join("; ")}.\n\n${header}\n${lines.join("\n")}\n\n## Notes\n\n${notes.map((n) => `- ${n.replace(/\n/g, " ")}`).join("\n")}\n`; fs.mkdirSync("docs", { recursive: true }); fs.writeFileSync("docs/provider-test-matrix.md", md); console.log(md); } main().catch((e) => { console.error(e); process.exit(1); });