feat(providers): add b.ai OpenAI-compatible provider (#5969)

* feat(providers): add b.ai OpenAI-compatible provider

Adds bai as a new OpenAI-compatible BYOK provider, distinct from the
existing thebai/theb.ai provider, using passthrough model discovery
(no hardcoded model list, live catalog served from api.b.ai/v1/models).

Co-authored-by: Delynn Assistant <zhen@dkzhen.org>
Inspired-by: https://github.com/decolua/9router/pull/963

* test(golden): regenerate translate-path for b.ai provider

* test(providers): bump APIKEY count 161→162 for b.ai

---------

Co-authored-by: Delynn Assistant <zhen@dkzhen.org>
This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-07-03 00:42:04 -03:00
committed by GitHub
parent a518ce8f8a
commit 70dd5df445
9 changed files with 223 additions and 6 deletions

View File

@@ -22,6 +22,7 @@
- **feat(providers):** add Augment (Auggie CLI) as a local no-auth provider. (thanks @chamdanilukman)
- **feat(providers):** add ModelScope as an OpenAI-compatible (API-key) provider. (thanks @tn5052)
- **feat(providers):** add Qiniu as an OpenAI-compatible (API-key) provider. (thanks @JackChiang233)
- **feat(providers):** add b.ai as an OpenAI-compatible (API-key) provider. (thanks @DEYLNN)
### 🔧 Bug Fixes

View File

@@ -41,6 +41,7 @@ import { yiProvider } from "./registry/yi/index.ts";
import { deepseekProvider } from "./registry/deepseek/index.ts";
import { deepseek_webProvider } from "./registry/deepseek/web/index.ts";
import { dgridProvider } from "./registry/dgrid/index.ts";
import { baiProvider } from "./registry/bai/index.ts";
import { qiniuProvider } from "./registry/qiniu/index.ts";
import { kimi_coding_apikeyProvider } from "./registry/kimi/coding-apikey/index.ts";
import { kimi_codingProvider } from "./registry/kimi/coding/index.ts";
@@ -216,6 +217,7 @@ export const REGISTRY: Record<string, RegistryEntry> = {
deepseek: deepseekProvider,
"deepseek-web": deepseek_webProvider,
dgrid: dgridProvider,
bai: baiProvider,
qiniu: qiniuProvider,
"kimi-coding-apikey": kimi_coding_apikeyProvider,
"kimi-coding": kimi_codingProvider,

View File

@@ -0,0 +1,14 @@
import type { RegistryEntry } from "../../shared.ts";
export const baiProvider: RegistryEntry = {
id: "bai",
alias: "bai",
format: "openai",
executor: "default",
baseUrl: "https://api.b.ai/v1/chat/completions",
authType: "apikey",
authHeader: "bearer",
modelsUrl: "https://api.b.ai/v1/models",
models: [],
passthroughModels: true,
};

View File

@@ -56,6 +56,10 @@ export const NAMED_OPENAI_STYLE_PROVIDERS = new Set([
// DGrid is an OpenAI-compatible gateway whose default seed is the free auto-router;
// the full model catalog is discovered live from https://api.dgrid.ai/v1/models.
"dgrid",
// b.ai is an OpenAI-compatible LLM gateway with no static seed — it proxies many
// upstream models (GPT, Claude, Gemini, MiniMax, Kimi, GLM...) behind one key, so the
// full catalog is discovered live from https://api.b.ai/v1/models.
"bai",
// Qiniu (七牛云 AI inference) is an OpenAI-compatible gateway with no static seed —
// it proxies many upstream models (DeepSeek, Claude, Kimi...) behind one key, so the
// full catalog is discovered live from https://api.qnaigc.com/v1/models.

View File

@@ -5,6 +5,7 @@ export const PROVIDER_ENDPOINTS = {
agentrouter: "https://agentrouter.org/v1/chat/completions",
openrouter: "https://openrouter.ai/api/v1/chat/completions",
dgrid: "https://api.dgrid.ai/v1/chat/completions",
bai: "https://api.b.ai/v1/chat/completions",
qiniu: "https://api.qnaigc.com/v1/chat/completions",
glm: "https://api.z.ai/api/anthropic/v1/messages",
glmt: "https://api.z.ai/api/anthropic/v1/messages",

View File

@@ -407,6 +407,19 @@ export const APIKEY_PROVIDERS_GATEWAYS = {
authHint: "Bearer API key for the TheB.AI OpenAI-compatible gateway.",
passthroughModels: true,
},
bai: {
id: "bai",
alias: "bai",
name: "b.ai",
icon: "hub",
color: "#6366F1",
textIcon: "BA",
website: "https://b.ai",
authHint:
"Bearer API key for the b.ai OpenAI-compatible LLM gateway (distinct from TheB.AI). " +
"Create a key at https://docs.b.ai, then use https://api.b.ai/v1 as the OpenAI-compatible base URL.",
passthroughModels: true,
},
fenayai: {
id: "fenayai",
alias: "fenayai",

View File

@@ -312,6 +312,29 @@
"stream": "auggie://cli/stdio"
}
},
"bai": {
"format": "openai",
"headers": {
"apiKey": {
"Accept": "text/event-stream",
"Authorization": "Bearer <TOK>",
"Content-Type": "application/json"
},
"nonStream": {
"Authorization": "Bearer <TOK>",
"Content-Type": "application/json"
},
"oauth": {
"Accept": "text/event-stream",
"Authorization": "Bearer <TOK>",
"Content-Type": "application/json"
}
},
"url": {
"nonStream": "https://api.b.ai/v1/chat/completions",
"stream": "https://api.b.ai/v1/chat/completions"
}
},
"baichuan": {
"format": "openai",
"headers": {

View File

@@ -0,0 +1,159 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const { APIKEY_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
const { PROVIDER_ENDPOINTS } = await import("../../src/shared/constants/config.ts");
const { REGISTRY: providerRegistry } = await import("../../open-sse/config/providerRegistry.ts");
const { isValidModel } = await import("../../src/shared/constants/models.ts");
const BAI_CHAT_URL = "https://api.b.ai/v1/chat/completions";
const BAI_MODELS_URL = "https://api.b.ai/v1/models";
test("b.ai is registered as an API-key provider", () => {
const entry = APIKEY_PROVIDERS.bai;
assert.ok(entry, "APIKEY_PROVIDERS.bai must be defined");
assert.equal(entry.id, "bai");
assert.equal(entry.alias, "bai");
assert.equal(entry.name, "b.ai");
assert.equal(entry.website, "https://b.ai");
assert.equal(entry.passthroughModels, true);
});
test("b.ai is distinct from the existing thebai (TheB.AI) provider", () => {
const bai = APIKEY_PROVIDERS.bai;
const thebai = APIKEY_PROVIDERS.thebai;
assert.ok(bai, "APIKEY_PROVIDERS.bai must be defined");
assert.ok(thebai, "APIKEY_PROVIDERS.thebai must be defined");
assert.notEqual(bai.id, thebai.id);
assert.notEqual(bai.website, thebai.website);
assert.equal(bai.website, "https://b.ai");
assert.equal(thebai.website, "https://theb.ai");
});
test("b.ai exposes the OpenAI-compatible chat completions endpoint", () => {
assert.equal(PROVIDER_ENDPOINTS.bai, BAI_CHAT_URL);
});
test("b.ai registry entry uses OpenAI format with bearer API-key auth", () => {
const entry = providerRegistry.bai;
assert.ok(entry, "providerRegistry.bai must be defined");
assert.equal(entry.id, "bai");
assert.equal(entry.alias, "bai");
assert.equal(entry.format, "openai");
assert.equal(entry.executor, "default");
assert.equal(entry.authType, "apikey");
assert.equal(entry.authHeader, "bearer");
assert.equal(entry.baseUrl, BAI_CHAT_URL);
assert.equal(entry.modelsUrl, BAI_MODELS_URL);
assert.equal(entry.passthroughModels, true);
});
test("b.ai ships no static model seed — relies fully on passthrough + live catalog", () => {
const models = providerRegistry.bai.models;
assert.deepEqual(models, []);
});
test("b.ai accepts any model id via passthrough models (GPT/Claude/Gemini/Kimi/GLM behind one key)", () => {
assert.equal(isValidModel("bai", "gpt-5.2"), true);
assert.equal(isValidModel("bai", "claude-opus-4-5"), true);
assert.equal(isValidModel("bai", "gemini-3-pro"), true);
assert.equal(isValidModel("bai", "kimi-k2.5"), true);
});
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-bai-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const modelsRoute = await import("../../src/app/api/providers/[id]/models/route.ts");
async function resetStorage() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
interface ModelsBody {
provider: string;
connectionId: string;
models: Array<{ id: string }>;
source?: string;
}
test("b.ai import fetches the live /v1/models catalog", async () => {
await resetStorage();
const connection = await providersDb.createProviderConnection({
provider: "bai",
authType: "apikey",
name: "bai-live",
apiKey: "bai-key",
});
let fetched = false;
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url) => {
if (String(url) === BAI_MODELS_URL) {
fetched = true;
return Response.json({
object: "list",
data: [{ id: "gpt-5.2" }, { id: "claude-opus-4-5" }, { id: "kimi-k2.5" }],
});
}
return new Response("not found", { status: 404 });
};
try {
const response = await modelsRoute.GET(
new Request(`http://localhost/api/providers/${connection.id}/models?refresh=true`),
{ params: { id: connection.id } }
);
assert.equal(response.status, 200);
const body = (await response.json()) as ModelsBody;
assert.equal(body.provider, "bai");
assert.equal(body.source, "api", "should serve the live upstream catalog");
assert.ok(fetched, `should have probed ${BAI_MODELS_URL}`);
const ids = body.models.map((model) => model.id);
assert.ok(ids.includes("gpt-5.2"), `live catalog model missing: ${ids.join(",")}`);
assert.ok(ids.includes("kimi-k2.5"), `live catalog model missing: ${ids.join(",")}`);
} finally {
globalThis.fetch = originalFetch;
}
});
test("b.ai import falls back to an empty local catalog when live fetch fails", async () => {
await resetStorage();
const connection = await providersDb.createProviderConnection({
provider: "bai",
authType: "apikey",
name: "bai-fallback",
apiKey: "bai-key-2",
});
const originalFetch = globalThis.fetch;
globalThis.fetch = async () => new Response("bad gateway", { status: 502 });
try {
const response = await modelsRoute.GET(
new Request(`http://localhost/api/providers/${connection.id}/models?refresh=true`),
{ params: { id: connection.id } }
);
assert.equal(response.status, 200);
const body = (await response.json()) as ModelsBody;
assert.equal(body.provider, "bai");
assert.equal(body.source, "local_catalog", "import must not break when upstream is down");
assert.deepEqual(
body.models.map((model) => model.id),
[]
);
} finally {
globalThis.fetch = originalFetch;
}
});

View File

@@ -1,7 +1,7 @@
// Characterization of the providers.ts catalog split (god-file decomposition): the host became a
// barrel that re-exports 10 data catalogs now living under constants/providers/*, and APIKEY is
// merged from 6 semantic family files (apikey/<family>.ts). Locks: the public surface (every catalog
// + helpers still exported), the spread-merge integrity (161 APIKEY entries, no loss/dup), and that
// + helpers still exported), the spread-merge integrity (162 APIKEY entries, no loss/dup), and that
// load-time Zod validation still runs. Pure-data move → behavior must be identical.
import { test } from "node:test";
import assert from "node:assert/strict";
@@ -31,12 +31,12 @@ test("barrel still exports every catalog + key helpers", () => {
}
});
test("APIKEY_PROVIDERS merges the 6 family files into 161 entries (no loss / no dup)", async () => {
test("APIKEY_PROVIDERS merges the 6 family files into 162 entries (no loss / no dup)", async () => {
const keys = Object.keys((P as Record<string, object>).APIKEY_PROVIDERS);
assert.equal(keys.length, 161);
assert.equal(new Set(keys).size, 161, "duplicate keys after spread-merge");
assert.equal(keys.length, 162);
assert.equal(new Set(keys).size, 162, "duplicate keys after spread-merge");
// the merged object's entry-count equals the sum of the 6 semantic family files; families are a
// strict partition (every provider in exactly one), so the sum must be exactly 161.
// strict partition (every provider in exactly one), so the sum must be exactly 162.
const families: [string, string][] = [
["gateways", "APIKEY_PROVIDERS_GATEWAYS"],
["frontier-labs", "APIKEY_PROVIDERS_FRONTIER"],
@@ -56,7 +56,7 @@ test("APIKEY_PROVIDERS merges the 6 family files into 161 entries (no loss / no
seen.add(k);
}
}
assert.equal(famTotal, 161, "families must partition all 161 providers");
assert.equal(famTotal, 162, "families must partition all 162 providers");
});
test("AI_PROVIDERS Proxy aggregates all sections; lookups resolve", () => {