mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-26 07:57:40 +00:00
refactor(providers): share paired api-key catalogs
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { buildSingleProviderApiKeyCatalog, findCatalogTemplate } from "./provider-catalog.js";
|
||||
import {
|
||||
buildPairedProviderApiKeyCatalog,
|
||||
buildSingleProviderApiKeyCatalog,
|
||||
findCatalogTemplate,
|
||||
} from "./provider-catalog.js";
|
||||
import type { ProviderCatalogContext } from "./types.js";
|
||||
|
||||
function createCatalogContext(params: {
|
||||
@@ -90,4 +94,32 @@ describe("buildSingleProviderApiKeyCatalog", () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("adds api key to each paired provider", async () => {
|
||||
const result = await buildPairedProviderApiKeyCatalog({
|
||||
ctx: createCatalogContext({
|
||||
apiKeys: { "test-provider": "secret-key" },
|
||||
}),
|
||||
providerId: "test-provider",
|
||||
buildProviders: async () => ({
|
||||
alpha: { api: "openai-completions", provider: "alpha" },
|
||||
beta: { api: "openai-completions", provider: "beta" },
|
||||
}),
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
providers: {
|
||||
alpha: {
|
||||
api: "openai-completions",
|
||||
provider: "alpha",
|
||||
apiKey: "secret-key",
|
||||
},
|
||||
beta: {
|
||||
api: "openai-completions",
|
||||
provider: "beta",
|
||||
apiKey: "secret-key",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -42,3 +42,23 @@ export async function buildSingleProviderApiKeyCatalog(params: {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function buildPairedProviderApiKeyCatalog(params: {
|
||||
ctx: ProviderCatalogContext;
|
||||
providerId: string;
|
||||
buildProviders: () =>
|
||||
| Record<string, ModelProviderConfig>
|
||||
| Promise<Record<string, ModelProviderConfig>>;
|
||||
}): Promise<ProviderCatalogResult> {
|
||||
const apiKey = params.ctx.resolveProviderApiKey(params.providerId).apiKey;
|
||||
if (!apiKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const providers = await params.buildProviders();
|
||||
return {
|
||||
providers: Object.fromEntries(
|
||||
Object.entries(providers).map(([id, provider]) => [id, { ...provider, apiKey }]),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user