diff --git a/src/plugins/provider-catalog.test.ts b/src/plugins/provider-catalog.test.ts index 84e1c824456..34e4c7b1a98 100644 --- a/src/plugins/provider-catalog.test.ts +++ b/src/plugins/provider-catalog.test.ts @@ -121,6 +121,36 @@ describe("buildSingleProviderApiKeyCatalog", () => { }); }); + it("matches explicit base url config across canonical provider aliases", async () => { + const result = await buildSingleProviderApiKeyCatalog({ + ctx: createCatalogContext({ + apiKeys: { zai: "secret-key" }, + config: { + models: { + providers: { + "z.ai": { + baseUrl: " https://api.z.ai/custom ", + models: [], + }, + }, + }, + }, + }), + providerId: "z-ai", + buildProvider: () => createProviderConfig({ baseUrl: "https://default.example/zai" }), + allowExplicitBaseUrl: true, + }); + + expect(result).toEqual({ + provider: { + api: "openai-completions", + baseUrl: "https://api.z.ai/custom", + models: [], + apiKey: "secret-key", + }, + }); + }); + it("adds api key to each paired provider", async () => { const result = await buildPairedProviderApiKeyCatalog({ ctx: createCatalogContext({ diff --git a/src/plugins/provider-catalog.ts b/src/plugins/provider-catalog.ts index a8934fa6c0a..cf38fb12a92 100644 --- a/src/plugins/provider-catalog.ts +++ b/src/plugins/provider-catalog.ts @@ -24,14 +24,18 @@ export async function buildSingleProviderApiKeyCatalog(params: { buildProvider: () => ModelProviderConfig | Promise; allowExplicitBaseUrl?: boolean; }): Promise { - const apiKey = params.ctx.resolveProviderApiKey(params.providerId).apiKey; + const providerId = normalizeProviderId(params.providerId); + const apiKey = params.ctx.resolveProviderApiKey(providerId).apiKey; if (!apiKey) { return null; } - const explicitProvider = params.allowExplicitBaseUrl - ? params.ctx.config.models?.providers?.[params.providerId] - : undefined; + const explicitProvider = + params.allowExplicitBaseUrl && params.ctx.config.models?.providers + ? Object.entries(params.ctx.config.models.providers).find( + ([configuredProviderId]) => normalizeProviderId(configuredProviderId) === providerId, + )?.[1] + : undefined; const explicitBaseUrl = typeof explicitProvider?.baseUrl === "string" ? explicitProvider.baseUrl.trim() : ""; @@ -51,7 +55,7 @@ export async function buildPairedProviderApiKeyCatalog(params: { | Record | Promise>; }): Promise { - const apiKey = params.ctx.resolveProviderApiKey(params.providerId).apiKey; + const apiKey = params.ctx.resolveProviderApiKey(normalizeProviderId(params.providerId)).apiKey; if (!apiKey) { return null; }