diff --git a/CHANGELOG.md b/CHANGELOG.md index c095a17d62..93384f8bfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/open-sse/config/providers/index.ts b/open-sse/config/providers/index.ts index 17eb23d21b..25ec4911c1 100644 --- a/open-sse/config/providers/index.ts +++ b/open-sse/config/providers/index.ts @@ -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 = { deepseek: deepseekProvider, "deepseek-web": deepseek_webProvider, dgrid: dgridProvider, + bai: baiProvider, qiniu: qiniuProvider, "kimi-coding-apikey": kimi_coding_apikeyProvider, "kimi-coding": kimi_codingProvider, diff --git a/open-sse/config/providers/registry/bai/index.ts b/open-sse/config/providers/registry/bai/index.ts new file mode 100644 index 0000000000..574362693c --- /dev/null +++ b/open-sse/config/providers/registry/bai/index.ts @@ -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, +}; diff --git a/src/app/api/providers/[id]/models/discovery/providerSets.ts b/src/app/api/providers/[id]/models/discovery/providerSets.ts index 338a036386..4bf76ecb19 100644 --- a/src/app/api/providers/[id]/models/discovery/providerSets.ts +++ b/src/app/api/providers/[id]/models/discovery/providerSets.ts @@ -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. diff --git a/src/shared/constants/config.ts b/src/shared/constants/config.ts index 96d13b9482..6271c744bc 100644 --- a/src/shared/constants/config.ts +++ b/src/shared/constants/config.ts @@ -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", diff --git a/src/shared/constants/providers/apikey/gateways.ts b/src/shared/constants/providers/apikey/gateways.ts index 27e1d38fb7..27ef167735 100644 --- a/src/shared/constants/providers/apikey/gateways.ts +++ b/src/shared/constants/providers/apikey/gateways.ts @@ -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", diff --git a/tests/snapshots/provider/translate-path.json b/tests/snapshots/provider/translate-path.json index f22206dc0c..2ddaed3633 100644 --- a/tests/snapshots/provider/translate-path.json +++ b/tests/snapshots/provider/translate-path.json @@ -312,6 +312,29 @@ "stream": "auggie://cli/stdio" } }, + "bai": { + "format": "openai", + "headers": { + "apiKey": { + "Accept": "text/event-stream", + "Authorization": "Bearer ", + "Content-Type": "application/json" + }, + "nonStream": { + "Authorization": "Bearer ", + "Content-Type": "application/json" + }, + "oauth": { + "Accept": "text/event-stream", + "Authorization": "Bearer ", + "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": { diff --git a/tests/unit/bai-provider.test.ts b/tests/unit/bai-provider.test.ts new file mode 100644 index 0000000000..3669d53998 --- /dev/null +++ b/tests/unit/bai-provider.test.ts @@ -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; + } +}); diff --git a/tests/unit/providers-constants-split.test.ts b/tests/unit/providers-constants-split.test.ts index 355d323879..4a17769f26 100644 --- a/tests/unit/providers-constants-split.test.ts +++ b/tests/unit/providers-constants-split.test.ts @@ -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/.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).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", () => {