| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667 |
- import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import type { AnthropicMessagesCompat, Api, Context, Model, OpenAICompletionsCompat } from "@earendil-works/pi-ai";
- import { getApiProvider } from "@earendil-works/pi-ai";
- import { getOAuthProvider } from "@earendil-works/pi-ai/oauth";
- import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
- import { AuthStorage } from "../src/core/auth-storage.ts";
- import { clearApiKeyCache, ModelRegistry, type ProviderConfigInput } from "../src/core/model-registry.ts";
- import { clearDeprecationWarningsForTests } from "../src/utils/deprecation.ts";
- describe("ModelRegistry", () => {
- let tempDir: string;
- let modelsJsonPath: string;
- let authStorage: AuthStorage;
- beforeEach(() => {
- tempDir = join(tmpdir(), `pi-test-model-registry-${Date.now()}-${Math.random().toString(36).slice(2)}`);
- mkdirSync(tempDir, { recursive: true });
- modelsJsonPath = join(tempDir, "models.json");
- authStorage = AuthStorage.create(join(tempDir, "auth.json"));
- clearDeprecationWarningsForTests();
- });
- afterEach(() => {
- if (tempDir && existsSync(tempDir)) {
- rmSync(tempDir, { recursive: true });
- }
- clearApiKeyCache();
- clearDeprecationWarningsForTests();
- vi.restoreAllMocks();
- });
- /** Create minimal provider config */
- function providerConfig(
- baseUrl: string,
- models: Array<{ id: string; name?: string }>,
- api: string = "anthropic-messages",
- ): ProviderConfigInput {
- return {
- baseUrl,
- apiKey: "test-key",
- api: api as Api,
- models: models.map((m) => ({
- id: m.id,
- name: m.name ?? m.id,
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 100000,
- maxTokens: 8000,
- })),
- };
- }
- function writeModelsJson(providers: Record<string, ReturnType<typeof providerConfig>>) {
- writeFileSync(modelsJsonPath, JSON.stringify({ providers }));
- }
- function getModelsForProvider(registry: ModelRegistry, provider: string) {
- return registry.getAll().filter((m) => m.provider === provider);
- }
- function toShPath(value: string): string {
- return value.replace(/\\/g, "/").replace(/"/g, '\\"');
- }
- /** Create a baseUrl-only override (no custom models) */
- function overrideConfig(baseUrl: string, headers?: Record<string, string>) {
- return { baseUrl, ...(headers && { headers }) };
- }
- /** Write raw providers config (for mixed override/replacement scenarios) */
- function writeRawModelsJson(providers: Record<string, unknown>) {
- writeFileSync(modelsJsonPath, JSON.stringify({ providers }));
- }
- const openAiModel: Model<Api> = {
- id: "test-openai-model",
- name: "Test OpenAI Model",
- api: "openai-completions",
- provider: "openai",
- baseUrl: "https://api.openai.com/v1",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 128000,
- maxTokens: 4096,
- };
- const emptyContext: Context = {
- messages: [],
- };
- describe("baseUrl override (no custom models)", () => {
- test("overriding baseUrl keeps all built-in models", () => {
- writeRawModelsJson({
- anthropic: overrideConfig("https://my-proxy.example.com/v1"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- // Should have multiple built-in models, not just one
- expect(anthropicModels.length).toBeGreaterThan(1);
- expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true);
- });
- test("overriding baseUrl changes URL on all built-in models", () => {
- writeRawModelsJson({
- anthropic: overrideConfig("https://my-proxy.example.com/v1"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- // All models should have the new baseUrl
- for (const model of anthropicModels) {
- expect(model.baseUrl).toBe("https://my-proxy.example.com/v1");
- }
- });
- test("overriding headers resolves at request time", async () => {
- writeRawModelsJson({
- anthropic: overrideConfig("https://my-proxy.example.com/v1", {
- "X-Custom-Header": "custom-value",
- }),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- for (const model of anthropicModels) {
- const auth = await registry.getApiKeyAndHeaders(model);
- expect(auth.ok).toBe(true);
- if (auth.ok) {
- expect(auth.headers?.["X-Custom-Header"]).toBe("custom-value");
- }
- }
- });
- test("headers-only override resolves at request time", async () => {
- writeRawModelsJson({
- anthropic: {
- headers: {
- "X-Custom-Header": "custom-value",
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getError()).toBeUndefined();
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- for (const model of anthropicModels) {
- const auth = await registry.getApiKeyAndHeaders(model);
- expect(auth.ok).toBe(true);
- if (auth.ok) {
- expect(auth.headers?.["X-Custom-Header"]).toBe("custom-value");
- }
- }
- });
- test("baseUrl-only override does not affect other providers", () => {
- writeRawModelsJson({
- anthropic: overrideConfig("https://my-proxy.example.com/v1"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const googleModels = getModelsForProvider(registry, "google");
- // Google models should still have their original baseUrl
- expect(googleModels.length).toBeGreaterThan(0);
- expect(googleModels[0].baseUrl).not.toBe("https://my-proxy.example.com/v1");
- });
- test("can mix baseUrl override and models merge", () => {
- writeRawModelsJson({
- // baseUrl-only for anthropic
- anthropic: overrideConfig("https://anthropic-proxy.example.com/v1"),
- // Add custom model for google (merged with built-ins)
- google: providerConfig(
- "https://google-proxy.example.com/v1",
- [{ id: "gemini-custom" }],
- "google-generative-ai",
- ),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- // Anthropic: multiple built-in models with new baseUrl
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- expect(anthropicModels.length).toBeGreaterThan(1);
- expect(anthropicModels[0].baseUrl).toBe("https://anthropic-proxy.example.com/v1");
- // Google: built-ins plus custom model
- const googleModels = getModelsForProvider(registry, "google");
- expect(googleModels.length).toBeGreaterThan(1);
- expect(googleModels.some((m) => m.id === "gemini-custom")).toBe(true);
- });
- test("refresh() picks up baseUrl override changes", () => {
- writeRawModelsJson({
- anthropic: overrideConfig("https://first-proxy.example.com/v1"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(getModelsForProvider(registry, "anthropic")[0].baseUrl).toBe("https://first-proxy.example.com/v1");
- // Update and refresh
- writeRawModelsJson({
- anthropic: overrideConfig("https://second-proxy.example.com/v1"),
- });
- registry.refresh();
- expect(getModelsForProvider(registry, "anthropic")[0].baseUrl).toBe("https://second-proxy.example.com/v1");
- });
- });
- describe("custom models merge behavior", () => {
- test("built-in provider custom models inherit api and baseUrl without explicit fields", () => {
- // Built-in providers already have api/baseUrl on every model, and auth
- // comes from env vars / auth storage. No need to specify them.
- writeRawModelsJson({
- openrouter: {
- models: [
- {
- id: "fake-provider/fake-model",
- name: "Fake model",
- reasoning: true,
- input: ["text"],
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getError()).toBeUndefined();
- const model = registry.find("openrouter", "fake-provider/fake-model");
- expect(model).toBeDefined();
- expect(model?.api).toBe("openai-completions");
- expect(model?.baseUrl).toBe("https://openrouter.ai/api/v1");
- });
- test("non-built-in provider custom models still require baseUrl and apiKey", () => {
- writeRawModelsJson({
- "my-custom-provider": {
- models: [
- {
- id: "my-model",
- api: "openai-completions",
- reasoning: false,
- input: ["text"],
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getError()).toContain("baseUrl");
- });
- test("custom provider with same name as built-in merges with built-in models", () => {
- writeModelsJson({
- anthropic: providerConfig("https://my-proxy.example.com/v1", [{ id: "claude-custom" }]),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- expect(anthropicModels.length).toBeGreaterThan(1);
- expect(anthropicModels.some((m) => m.id === "claude-custom")).toBe(true);
- expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true);
- });
- test("custom model with same id replaces built-in model by id", () => {
- writeModelsJson({
- openrouter: providerConfig(
- "https://my-proxy.example.com/v1",
- [{ id: "anthropic/claude-sonnet-4" }],
- "openai-completions",
- ),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnetModels = models.filter((m) => m.id === "anthropic/claude-sonnet-4");
- expect(sonnetModels).toHaveLength(1);
- expect(sonnetModels[0].baseUrl).toBe("https://my-proxy.example.com/v1");
- });
- test("custom provider with same name as built-in does not affect other built-in providers", () => {
- writeModelsJson({
- anthropic: providerConfig("https://my-proxy.example.com/v1", [{ id: "claude-custom" }]),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(getModelsForProvider(registry, "google").length).toBeGreaterThan(0);
- expect(getModelsForProvider(registry, "openai").length).toBeGreaterThan(0);
- });
- test("provider-level baseUrl applies to both built-in and custom models", () => {
- writeModelsJson({
- anthropic: providerConfig("https://merged-proxy.example.com/v1", [{ id: "claude-custom" }]),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- for (const model of anthropicModels) {
- expect(model.baseUrl).toBe("https://merged-proxy.example.com/v1");
- }
- });
- test("provider-level compat applies to custom models", () => {
- writeRawModelsJson({
- demo: {
- baseUrl: "https://example.com/v1",
- apiKey: "DEMO_KEY",
- api: "openai-completions",
- compat: {
- supportsUsageInStreaming: false,
- maxTokensField: "max_tokens",
- },
- models: [
- {
- id: "demo-model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 1000,
- maxTokens: 100,
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const compat = registry.find("demo", "demo-model")?.compat as OpenAICompletionsCompat | undefined;
- expect(compat?.supportsUsageInStreaming).toBe(false);
- expect(compat?.maxTokensField).toBe("max_tokens");
- });
- test("model-level compat overrides provider-level compat for custom models", () => {
- writeRawModelsJson({
- demo: {
- baseUrl: "https://example.com/v1",
- apiKey: "DEMO_KEY",
- api: "openai-completions",
- compat: {
- supportsUsageInStreaming: false,
- maxTokensField: "max_tokens",
- },
- models: [
- {
- id: "demo-model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 1000,
- maxTokens: 100,
- compat: {
- supportsUsageInStreaming: true,
- maxTokensField: "max_completion_tokens",
- },
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const compat = registry.find("demo", "demo-model")?.compat as OpenAICompletionsCompat | undefined;
- expect(compat?.supportsUsageInStreaming).toBe(true);
- expect(compat?.maxTokensField).toBe("max_completion_tokens");
- });
- test("provider-level compat applies to built-in models", () => {
- writeRawModelsJson({
- openrouter: {
- compat: {
- supportsUsageInStreaming: false,
- supportsStrictMode: false,
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- expect(models.length).toBeGreaterThan(0);
- for (const model of models) {
- const compat = model.compat as OpenAICompletionsCompat | undefined;
- expect(compat?.supportsUsageInStreaming).toBe(false);
- expect(compat?.supportsStrictMode).toBe(false);
- }
- });
- test("model schema accepts thinkingLevelMap and compat schema accepts supportsStrictMode and cacheControlFormat", () => {
- writeRawModelsJson({
- demo: {
- baseUrl: "https://example.com/v1",
- apiKey: "DEMO_KEY",
- api: "openai-completions",
- models: [
- {
- id: "demo-model",
- reasoning: true,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 1000,
- maxTokens: 100,
- thinkingLevelMap: {
- minimal: null,
- high: "max",
- },
- compat: {
- supportsStrictMode: false,
- cacheControlFormat: "anthropic",
- },
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const model = registry.find("demo", "demo-model");
- const compat = model?.compat as OpenAICompletionsCompat | undefined;
- expect(registry.getError()).toBeUndefined();
- expect(model?.thinkingLevelMap).toEqual({ minimal: null, high: "max" });
- expect(compat?.supportsStrictMode).toBe(false);
- expect(compat?.cacheControlFormat).toBe("anthropic");
- });
- test("compat schema accepts Anthropic eager tool input streaming flag", () => {
- writeRawModelsJson({
- demo: {
- baseUrl: "https://example.com",
- apiKey: "DEMO_KEY",
- api: "anthropic-messages",
- compat: {
- supportsEagerToolInputStreaming: false,
- },
- models: [
- {
- id: "demo-model",
- reasoning: true,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 1000,
- maxTokens: 100,
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const compat = registry.find("demo", "demo-model")?.compat as AnthropicMessagesCompat | undefined;
- expect(registry.getError()).toBeUndefined();
- expect(compat?.supportsEagerToolInputStreaming).toBe(false);
- });
- test("compat schema accepts long cache retention flag", () => {
- writeRawModelsJson({
- demo: {
- baseUrl: "https://example.com",
- apiKey: "DEMO_KEY",
- api: "anthropic-messages",
- compat: {
- supportsLongCacheRetention: false,
- },
- models: [
- {
- id: "demo-model",
- reasoning: true,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 1000,
- maxTokens: 100,
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const compat = registry.find("demo", "demo-model")?.compat as AnthropicMessagesCompat | undefined;
- expect(registry.getError()).toBeUndefined();
- expect(compat?.supportsLongCacheRetention).toBe(false);
- });
- test("model-level baseUrl overrides provider-level baseUrl for custom models", () => {
- writeRawModelsJson({
- "opencode-go": {
- baseUrl: "https://opencode.ai/zen/go/v1",
- apiKey: "TEST_KEY",
- models: [
- {
- id: "minimax-m2.5",
- api: "anthropic-messages",
- baseUrl: "https://opencode.ai/zen/go",
- reasoning: true,
- input: ["text"],
- cost: { input: 0.3, output: 1.2, cacheRead: 0.03, cacheWrite: 0 },
- contextWindow: 204800,
- maxTokens: 131072,
- },
- {
- id: "glm-5",
- api: "openai-completions",
- reasoning: true,
- input: ["text"],
- cost: { input: 1, output: 3.2, cacheRead: 0.2, cacheWrite: 0 },
- contextWindow: 204800,
- maxTokens: 131072,
- },
- ],
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const m25 = registry.find("opencode-go", "minimax-m2.5");
- const glm5 = registry.find("opencode-go", "glm-5");
- expect(m25?.baseUrl).toBe("https://opencode.ai/zen/go");
- expect(glm5?.baseUrl).toBe("https://opencode.ai/zen/go/v1");
- });
- test("modelOverrides still apply when provider also defines models", () => {
- writeRawModelsJson({
- openrouter: {
- baseUrl: "https://my-proxy.example.com/v1",
- apiKey: "OPENROUTER_API_KEY",
- api: "openai-completions",
- models: [
- {
- id: "custom/openrouter-model",
- name: "Custom OpenRouter Model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 128000,
- maxTokens: 16384,
- },
- ],
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- name: "Overridden Built-in Sonnet",
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- expect(models.some((m) => m.id === "custom/openrouter-model")).toBe(true);
- expect(
- models.some((m) => m.id === "anthropic/claude-sonnet-4" && m.name === "Overridden Built-in Sonnet"),
- ).toBe(true);
- });
- test("refresh() reloads merged custom models from disk", () => {
- writeModelsJson({
- anthropic: providerConfig("https://first-proxy.example.com/v1", [{ id: "claude-custom" }]),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(getModelsForProvider(registry, "anthropic").some((m) => m.id === "claude-custom")).toBe(true);
- // Update and refresh
- writeModelsJson({
- anthropic: providerConfig("https://second-proxy.example.com/v1", [{ id: "claude-custom-2" }]),
- });
- registry.refresh();
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- expect(anthropicModels.some((m) => m.id === "claude-custom")).toBe(false);
- expect(anthropicModels.some((m) => m.id === "claude-custom-2")).toBe(true);
- expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true);
- });
- test("removing custom models from models.json keeps built-in provider models", () => {
- writeModelsJson({
- anthropic: providerConfig("https://proxy.example.com/v1", [{ id: "claude-custom" }]),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(getModelsForProvider(registry, "anthropic").some((m) => m.id === "claude-custom")).toBe(true);
- // Remove custom models and refresh
- writeModelsJson({});
- registry.refresh();
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- expect(anthropicModels.some((m) => m.id === "claude-custom")).toBe(false);
- expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true);
- });
- });
- describe("modelOverrides (per-model customization)", () => {
- test("model override applies to a single built-in model", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- name: "Custom Sonnet Name",
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
- expect(sonnet?.name).toBe("Custom Sonnet Name");
- // Other models should be unchanged
- const opus = models.find((m) => m.id === "anthropic/claude-opus-4");
- expect(opus?.name).not.toBe("Custom Sonnet Name");
- });
- test("model override with compat.openRouterRouting", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- compat: {
- openRouterRouting: { only: ["amazon-bedrock"] },
- },
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
- const compat = sonnet?.compat as OpenAICompletionsCompat | undefined;
- expect(compat?.openRouterRouting).toEqual({ only: ["amazon-bedrock"] });
- });
- test("model override deep merges compat settings", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- compat: {
- openRouterRouting: { order: ["anthropic", "together"] },
- },
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
- // Should have both the new routing AND preserve other compat settings
- const compat = sonnet?.compat as OpenAICompletionsCompat | undefined;
- expect(compat?.openRouterRouting).toEqual({ order: ["anthropic", "together"] });
- });
- test("multiple model overrides on same provider", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- compat: { openRouterRouting: { only: ["amazon-bedrock"] } },
- },
- "anthropic/claude-opus-4": {
- compat: { openRouterRouting: { only: ["anthropic"] } },
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
- const opus = models.find((m) => m.id === "anthropic/claude-opus-4");
- const sonnetCompat = sonnet?.compat as OpenAICompletionsCompat | undefined;
- const opusCompat = opus?.compat as OpenAICompletionsCompat | undefined;
- expect(sonnetCompat?.openRouterRouting).toEqual({ only: ["amazon-bedrock"] });
- expect(opusCompat?.openRouterRouting).toEqual({ only: ["anthropic"] });
- });
- test("model override combined with baseUrl override", () => {
- writeRawModelsJson({
- openrouter: {
- baseUrl: "https://my-proxy.example.com/v1",
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- name: "Proxied Sonnet",
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
- // Both overrides should apply
- expect(sonnet?.baseUrl).toBe("https://my-proxy.example.com/v1");
- expect(sonnet?.name).toBe("Proxied Sonnet");
- // Other models should have the baseUrl but not the name override
- const opus = models.find((m) => m.id === "anthropic/claude-opus-4");
- expect(opus?.baseUrl).toBe("https://my-proxy.example.com/v1");
- expect(opus?.name).not.toBe("Proxied Sonnet");
- });
- test("model override for non-existent model ID is ignored", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "nonexistent/model-id": {
- name: "This should not appear",
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- // Should not create a new model
- expect(models.find((m) => m.id === "nonexistent/model-id")).toBeUndefined();
- // Should not crash or show error
- expect(registry.getError()).toBeUndefined();
- });
- test("model override can change cost fields partially", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- cost: { input: 99 },
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
- // Input cost should be overridden
- expect(sonnet?.cost.input).toBe(99);
- // Other cost fields should be preserved from built-in
- expect(sonnet?.cost.output).toBeGreaterThan(0);
- });
- test("model override can add headers at request time", async () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- headers: { "X-Custom-Model-Header": "value" },
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const models = getModelsForProvider(registry, "openrouter");
- const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4");
- expect(sonnet).toBeDefined();
- const auth = await registry.getApiKeyAndHeaders(sonnet!);
- expect(auth.ok).toBe(true);
- if (auth.ok) {
- expect(auth.headers?.["X-Custom-Model-Header"]).toBe("value");
- }
- });
- test("refresh() picks up model override changes", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- name: "First Name",
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(
- getModelsForProvider(registry, "openrouter").find((m) => m.id === "anthropic/claude-sonnet-4")?.name,
- ).toBe("First Name");
- // Update and refresh
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- name: "Second Name",
- },
- },
- },
- });
- registry.refresh();
- expect(
- getModelsForProvider(registry, "openrouter").find((m) => m.id === "anthropic/claude-sonnet-4")?.name,
- ).toBe("Second Name");
- });
- test("removing model override restores built-in values", () => {
- writeRawModelsJson({
- openrouter: {
- modelOverrides: {
- "anthropic/claude-sonnet-4": {
- name: "Custom Name",
- },
- },
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const customName = getModelsForProvider(registry, "openrouter").find(
- (m) => m.id === "anthropic/claude-sonnet-4",
- )?.name;
- expect(customName).toBe("Custom Name");
- // Remove override and refresh
- writeRawModelsJson({});
- registry.refresh();
- const restoredName = getModelsForProvider(registry, "openrouter").find(
- (m) => m.id === "anthropic/claude-sonnet-4",
- )?.name;
- expect(restoredName).not.toBe("Custom Name");
- });
- });
- describe("dynamic provider lifecycle", () => {
- test("getProviderDisplayName resolves registered, OAuth, built-in, and fallback names", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getProviderDisplayName("openai")).toBe("OpenAI");
- expect(registry.getProviderDisplayName("github-copilot")).toBe("GitHub Copilot");
- expect(registry.getProviderDisplayName("unknown-provider")).toBe("unknown-provider");
- registry.registerProvider("named-provider", {
- name: "Named Provider",
- baseUrl: "https://provider.test/v1",
- apiKey: "test-key",
- api: "openai-completions",
- models: [
- {
- id: "demo-model",
- name: "Demo Model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 128000,
- maxTokens: 4096,
- },
- ],
- });
- expect(registry.getProviderDisplayName("named-provider")).toBe("Named Provider");
- registry.registerProvider("oauth-provider", {
- baseUrl: "https://provider.test/v1",
- api: "openai-completions",
- oauth: {
- name: "OAuth Provider",
- login: async () => ({ access: "access", refresh: "refresh", expires: Date.now() + 60_000 }),
- refreshToken: async (credentials) => credentials,
- getApiKey: (credentials) => credentials.access,
- },
- models: [
- {
- id: "demo-model",
- name: "Demo Model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 128000,
- maxTokens: 4096,
- },
- ],
- });
- expect(registry.getProviderDisplayName("oauth-provider")).toBe("OAuth Provider");
- });
- test("registerProvider warns and temporarily treats uppercase apiKey as an env reference", async () => {
- const originalEnv = process.env.CUSTOM_NAME;
- process.env.CUSTOM_NAME = "legacy-env-key";
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
- try {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider("legacy-provider", {
- ...providerConfig("https://provider.test/v1", [{ id: "demo-model" }], "openai-completions"),
- apiKey: "CUSTOM_NAME",
- });
- expect(await registry.getApiKeyForProvider("legacy-provider")).toBe("legacy-env-key");
- expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Pass "$CUSTOM_NAME" instead'));
- } finally {
- if (originalEnv === undefined) {
- delete process.env.CUSTOM_NAME;
- } else {
- process.env.CUSTOM_NAME = originalEnv;
- }
- }
- });
- test("failed registerProvider does not persist invalid streamSimple config", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(() =>
- registry.registerProvider("broken-provider", {
- streamSimple: (() => {
- throw new Error("should not run");
- }) as any,
- }),
- ).toThrow('Provider broken-provider: "api" is required when registering streamSimple.');
- expect(() => registry.refresh()).not.toThrow();
- });
- test("failed registerProvider does not remove existing provider models", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider("demo-provider", {
- baseUrl: "https://provider.test/v1",
- apiKey: "test-key",
- api: "openai-completions",
- models: [
- {
- id: "demo-model",
- name: "Demo Model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 128000,
- maxTokens: 4096,
- },
- ],
- });
- expect(registry.find("demo-provider", "demo-model")).toBeDefined();
- expect(() =>
- registry.registerProvider("demo-provider", {
- baseUrl: "https://provider.test/v2",
- apiKey: "test-key",
- models: [
- {
- id: "broken-model",
- name: "Broken Model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 128000,
- maxTokens: 4096,
- },
- ],
- }),
- ).toThrow('Provider demo-provider, model broken-model: no "api" specified.');
- expect(registry.find("demo-provider", "demo-model")).toBeDefined();
- expect(() => registry.refresh()).not.toThrow();
- expect(registry.find("demo-provider", "demo-model")).toBeDefined();
- });
- test("unregisterProvider removes custom OAuth provider and restores built-in OAuth provider", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider("anthropic", {
- oauth: {
- name: "Custom Anthropic OAuth",
- login: async () => ({
- access: "custom-access-token",
- refresh: "custom-refresh-token",
- expires: Date.now() + 60_000,
- }),
- refreshToken: async (credentials) => credentials,
- getApiKey: (credentials) => credentials.access,
- },
- });
- expect(getOAuthProvider("anthropic")?.name).toBe("Custom Anthropic OAuth");
- registry.unregisterProvider("anthropic");
- expect(getOAuthProvider("anthropic")?.name).not.toBe("Custom Anthropic OAuth");
- });
- test("unregisterProvider removes custom streamSimple override and restores built-in API stream handler", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider("stream-override-provider", {
- api: "openai-completions",
- streamSimple: () => {
- throw new Error("custom streamSimple override");
- },
- });
- let threwCustomOverride = false;
- try {
- getApiProvider("openai-completions")?.streamSimple(openAiModel, emptyContext);
- } catch (error) {
- threwCustomOverride = error instanceof Error && error.message === "custom streamSimple override";
- }
- expect(threwCustomOverride).toBe(true);
- registry.unregisterProvider("stream-override-provider");
- let threwCustomOverrideAfterUnregister = false;
- try {
- getApiProvider("openai-completions")?.streamSimple(openAiModel, emptyContext);
- } catch (error) {
- threwCustomOverrideAfterUnregister =
- error instanceof Error && error.message === "custom streamSimple override";
- }
- expect(threwCustomOverrideAfterUnregister).toBe(false);
- });
- describe("dynamic provider override persistence", () => {
- test("baseUrl-only override keeps built-in provider models after refresh", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider("anthropic", { baseUrl: "https://proxy.test/anthropic" });
- registry.refresh();
- const anthropicModels = getModelsForProvider(registry, "anthropic");
- expect(anthropicModels.length).toBeGreaterThan(1);
- expect(anthropicModels.every((m) => m.baseUrl === "https://proxy.test/anthropic")).toBe(true);
- });
- test("models-only override replaces built-in provider models after refresh", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider("anthropic", {
- ...providerConfig("https://custom.test/anthropic", [{ id: "custom-claude" }], "anthropic-messages"),
- baseUrl: "https://custom.test/anthropic",
- });
- registry.refresh();
- expect(getModelsForProvider(registry, "anthropic").map((m) => m.id)).toEqual(["custom-claude"]);
- expect(registry.find("anthropic", "custom-claude")?.baseUrl).toBe("https://custom.test/anthropic");
- });
- test("models plus baseUrl override replaces built-in provider models after refresh", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider("anthropic", {
- ...providerConfig("https://custom.test/anthropic", [{ id: "custom-claude" }], "anthropic-messages"),
- baseUrl: "https://custom.test/anthropic",
- });
- registry.registerProvider("anthropic", { baseUrl: "https://proxy.test/anthropic" });
- registry.refresh();
- expect(getModelsForProvider(registry, "anthropic").map((m) => m.id)).toEqual(["custom-claude"]);
- expect(registry.find("anthropic", "custom-claude")?.baseUrl).toBe("https://proxy.test/anthropic");
- });
- test("models-only custom provider registration survives refresh", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider(
- "custom-provider",
- providerConfig("https://custom.test/v1", [{ id: "custom-a" }, { id: "custom-b" }], "openai-completions"),
- );
- registry.refresh();
- expect(getModelsForProvider(registry, "custom-provider").map((m) => m.id)).toEqual([
- "custom-a",
- "custom-b",
- ]);
- });
- test("baseUrl-only override keeps custom provider models after refresh", () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider(
- "custom-provider",
- providerConfig("https://custom.test/v1", [{ id: "custom-a" }, { id: "custom-b" }], "openai-completions"),
- );
- registry.registerProvider("custom-provider", { baseUrl: "https://proxy.test/custom" });
- registry.refresh();
- expect(getModelsForProvider(registry, "custom-provider").map((m) => m.id)).toEqual([
- "custom-a",
- "custom-b",
- ]);
- expect(
- getModelsForProvider(registry, "custom-provider").every(
- (m) => m.baseUrl === "https://proxy.test/custom",
- ),
- ).toBe(true);
- });
- test("headers-only override keeps custom provider models after refresh", async () => {
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- registry.registerProvider(
- "custom-provider",
- providerConfig("https://custom.test/v1", [{ id: "custom-a" }, { id: "custom-b" }], "openai-completions"),
- );
- registry.registerProvider("custom-provider", { headers: { "x-proxy": "enabled" } });
- registry.refresh();
- const models = getModelsForProvider(registry, "custom-provider");
- expect(models.map((m) => m.id)).toEqual(["custom-a", "custom-b"]);
- expect(models.every((m) => m.baseUrl === "https://custom.test/v1")).toBe(true);
- expect(await registry.getApiKeyAndHeaders(models[0])).toMatchObject({
- ok: true,
- headers: { "x-proxy": "enabled" },
- });
- });
- });
- });
- describe("API key resolution", () => {
- /** Create provider config with custom apiKey */
- function providerWithApiKey(apiKey: string) {
- return {
- baseUrl: "https://example.com/v1",
- apiKey,
- api: "anthropic-messages",
- models: [
- {
- id: "test-model",
- name: "Test Model",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 100000,
- maxTokens: 8000,
- },
- ],
- };
- }
- test("apiKey with ! prefix executes command and uses stdout", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("!echo test-api-key-from-command"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("test-api-key-from-command");
- });
- test("apiKey with ! prefix trims whitespace from command output", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("!echo ' spaced-key '"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("spaced-key");
- });
- test("apiKey with ! prefix handles multiline output (uses trimmed result)", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("!printf 'line1\\nline2'"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("line1\nline2");
- });
- test("apiKey with ! prefix returns undefined on command failure", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("!exit 1"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBeUndefined();
- });
- test("apiKey with ! prefix returns undefined on nonexistent command", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("!nonexistent-command-12345"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBeUndefined();
- });
- test("apiKey with ! prefix returns undefined on empty output", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("!printf ''"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBeUndefined();
- });
- test("apiKey with $ prefix resolves to env value", async () => {
- const originalEnv = process.env.TEST_API_KEY_12345;
- process.env.TEST_API_KEY_12345 = "env-api-key-value";
- try {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("$TEST_API_KEY_12345"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("env-api-key-value");
- } finally {
- if (originalEnv === undefined) {
- delete process.env.TEST_API_KEY_12345;
- } else {
- process.env.TEST_API_KEY_12345 = originalEnv;
- }
- }
- });
- test("apiKey with braced env syntax resolves to env value", async () => {
- const originalEnv = process.env.TEST_BRACED_API_KEY_12345;
- process.env.TEST_BRACED_API_KEY_12345 = "braced-env-api-key-value";
- const bracedKey = "$" + "{TEST_BRACED_API_KEY_12345}";
- try {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(bracedKey),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("braced-env-api-key-value");
- } finally {
- if (originalEnv === undefined) {
- delete process.env.TEST_BRACED_API_KEY_12345;
- } else {
- process.env.TEST_BRACED_API_KEY_12345 = originalEnv;
- }
- }
- });
- test("apiKey interpolates braced env references inside literals", async () => {
- const originalPartA = process.env.TEST_INTERPOLATED_PART_A_12345;
- const originalPartB = process.env.TEST_INTERPOLATED_PART_B_12345;
- process.env.TEST_INTERPOLATED_PART_A_12345 = "left";
- process.env.TEST_INTERPOLATED_PART_B_12345 = "right";
- const interpolatedKey = ["$", "{TEST_INTERPOLATED_PART_A_12345}_$", "{TEST_INTERPOLATED_PART_B_12345}"].join(
- "",
- );
- try {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(interpolatedKey),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("left_right");
- } finally {
- if (originalPartA === undefined) {
- delete process.env.TEST_INTERPOLATED_PART_A_12345;
- } else {
- process.env.TEST_INTERPOLATED_PART_A_12345 = originalPartA;
- }
- if (originalPartB === undefined) {
- delete process.env.TEST_INTERPOLATED_PART_B_12345;
- } else {
- process.env.TEST_INTERPOLATED_PART_B_12345 = originalPartB;
- }
- }
- });
- test("apiKey with $$ prefix escapes a leading dollar", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("$$TEST_API_KEY_12345"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("$TEST_API_KEY_12345");
- });
- test("apiKey with $! escapes a literal bang and still interpolates later env refs", async () => {
- const originalEnv = process.env.TEST_API_KEY_12345;
- process.env.TEST_API_KEY_12345 = "env-api-key-value";
- try {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("$!literal-$TEST_API_KEY_12345"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("!literal-env-api-key-value");
- } finally {
- if (originalEnv === undefined) {
- delete process.env.TEST_API_KEY_12345;
- } else {
- process.env.TEST_API_KEY_12345 = originalEnv;
- }
- }
- });
- test("plain apiKey is used directly even when it matches an env var", async () => {
- const originalEnv = process.env.TEST_API_KEY_12345;
- process.env.TEST_API_KEY_12345 = "env-api-key-value";
- try {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("TEST_API_KEY_12345"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("TEST_API_KEY_12345");
- } finally {
- if (originalEnv === undefined) {
- delete process.env.TEST_API_KEY_12345;
- } else {
- process.env.TEST_API_KEY_12345 = originalEnv;
- }
- }
- });
- test("apiKey as literal value is used directly when not an env var", async () => {
- // Make sure this isn't an env var
- delete process.env.literal_api_key_value;
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("literal_api_key_value"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("literal_api_key_value");
- });
- test("apiKey command can use shell features like pipes", async () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("!echo 'hello world' | tr ' ' '-'"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const apiKey = await registry.getApiKeyForProvider("custom-provider");
- expect(apiKey).toBe("hello-world");
- });
- describe("request-time resolution", () => {
- test("command is executed on every provider lookup", async () => {
- const counterFile = join(tempDir, "counter");
- writeFileSync(counterFile, "0");
- const counterPath = toShPath(counterFile);
- const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; echo "key-value"'`;
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(command),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- await registry.getApiKeyForProvider("custom-provider");
- await registry.getApiKeyForProvider("custom-provider");
- await registry.getApiKeyForProvider("custom-provider");
- const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10);
- expect(count).toBe(3);
- });
- test("commands are re-executed across registry instances", async () => {
- const counterFile = join(tempDir, "counter");
- writeFileSync(counterFile, "0");
- const counterPath = toShPath(counterFile);
- const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; echo "key-value"'`;
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(command),
- });
- const registry1 = ModelRegistry.create(authStorage, modelsJsonPath);
- await registry1.getApiKeyForProvider("custom-provider");
- const registry2 = ModelRegistry.create(authStorage, modelsJsonPath);
- await registry2.getApiKeyForProvider("custom-provider");
- const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10);
- expect(count).toBe(2);
- });
- test("different commands resolve independently", async () => {
- writeRawModelsJson({
- "provider-a": providerWithApiKey("!echo key-a"),
- "provider-b": providerWithApiKey("!echo key-b"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const keyA = await registry.getApiKeyForProvider("provider-a");
- const keyB = await registry.getApiKeyForProvider("provider-b");
- expect(keyA).toBe("key-a");
- expect(keyB).toBe("key-b");
- });
- test("failed commands are retried", async () => {
- const counterFile = join(tempDir, "counter");
- writeFileSync(counterFile, "0");
- const counterPath = toShPath(counterFile);
- const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; exit 1'`;
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(command),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const key1 = await registry.getApiKeyForProvider("custom-provider");
- const key2 = await registry.getApiKeyForProvider("custom-provider");
- expect(key1).toBeUndefined();
- expect(key2).toBeUndefined();
- const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10);
- expect(count).toBe(2);
- });
- test("provider auth status reports apiKey environment variables from models.json", () => {
- const envVarName = "TEST_API_KEY_STATUS_TEST_98765";
- const originalEnv = process.env[envVarName];
- try {
- process.env[envVarName] = "status-test-key";
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(`$${envVarName}`),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getProviderAuthStatus("custom-provider")).toEqual({
- configured: true,
- source: "environment",
- label: envVarName,
- });
- } finally {
- if (originalEnv === undefined) {
- delete process.env[envVarName];
- } else {
- process.env[envVarName] = originalEnv;
- }
- }
- });
- test("provider auth status reports interpolated apiKey environment variables", () => {
- const envVarNameA = "TEST_API_KEY_STATUS_PART_A_98765";
- const envVarNameB = "TEST_API_KEY_STATUS_PART_B_98765";
- const originalEnvA = process.env[envVarNameA];
- const originalEnvB = process.env[envVarNameB];
- process.env[envVarNameA] = "left";
- process.env[envVarNameB] = "right";
- const interpolatedKey = ["$", "{", envVarNameA, "}_$", "{", envVarNameB, "}"].join("");
- try {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(interpolatedKey),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getProviderAuthStatus("custom-provider")).toEqual({
- configured: true,
- source: "environment",
- label: `${envVarNameA}, ${envVarNameB}`,
- });
- } finally {
- if (originalEnvA === undefined) {
- delete process.env[envVarNameA];
- } else {
- process.env[envVarNameA] = originalEnvA;
- }
- if (originalEnvB === undefined) {
- delete process.env[envVarNameB];
- } else {
- process.env[envVarNameB] = originalEnvB;
- }
- }
- });
- test("provider auth status reports non-env apiKey values from models.json as a config key", () => {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey("literal_api_key_value"),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getProviderAuthStatus("custom-provider")).toEqual({
- configured: true,
- source: "models_json_key",
- });
- });
- test("missing explicit env apiKey keeps provider unavailable", () => {
- const envVarName = "TEST_API_KEY_MISSING_TEST_98765";
- const originalEnv = process.env[envVarName];
- delete process.env[envVarName];
- try {
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(`$${envVarName}`),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getProviderAuthStatus("custom-provider")).toEqual({ configured: false });
- expect(registry.getAvailable().some((model) => model.provider === "custom-provider")).toBe(false);
- } finally {
- if (originalEnv === undefined) {
- delete process.env[envVarName];
- } else {
- process.env[envVarName] = originalEnv;
- }
- }
- });
- test("provider auth status reports command apiKey values from models.json without executing them", () => {
- const counterFile = join(tempDir, "status-counter");
- writeFileSync(counterFile, "0");
- const counterPath = toShPath(counterFile);
- const command = `!sh -c 'echo 1 > "${counterPath}"; echo key-value'`;
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(command),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- expect(registry.getProviderAuthStatus("custom-provider")).toEqual({
- configured: true,
- source: "models_json_command",
- });
- expect(readFileSync(counterFile, "utf-8")).toBe("0");
- });
- test("environment variables are not cached (changes are picked up)", async () => {
- const envVarName = "TEST_API_KEY_CACHE_TEST_98765";
- const originalEnv = process.env[envVarName];
- try {
- process.env[envVarName] = "first-value";
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(`$${envVarName}`),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const key1 = await registry.getApiKeyForProvider("custom-provider");
- expect(key1).toBe("first-value");
- process.env[envVarName] = "second-value";
- const key2 = await registry.getApiKeyForProvider("custom-provider");
- expect(key2).toBe("second-value");
- } finally {
- if (originalEnv === undefined) {
- delete process.env[envVarName];
- } else {
- process.env[envVarName] = originalEnv;
- }
- }
- });
- test("getAvailable does not execute command-backed apiKey resolution", async () => {
- const counterFile = join(tempDir, "counter");
- writeFileSync(counterFile, "0");
- const counterPath = toShPath(counterFile);
- const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; echo "key-value"'`;
- writeRawModelsJson({
- "custom-provider": providerWithApiKey(command),
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const available = registry.getAvailable();
- expect(available.some((m) => m.provider === "custom-provider")).toBe(true);
- const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10);
- expect(count).toBe(0);
- });
- test("getApiKeyAndHeaders resolves authHeader on every request", async () => {
- const tokenFile = join(tempDir, "token");
- writeFileSync(tokenFile, "token-1");
- const tokenPath = toShPath(tokenFile);
- writeRawModelsJson({
- "custom-provider": {
- ...providerWithApiKey(`!sh -c 'cat "${tokenPath}"'`),
- authHeader: true,
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const model = registry.find("custom-provider", "test-model");
- expect(model).toBeDefined();
- const auth1 = await registry.getApiKeyAndHeaders(model!);
- expect(auth1).toEqual({
- ok: true,
- apiKey: "token-1",
- headers: { Authorization: "Bearer token-1" },
- });
- writeFileSync(tokenFile, "token-2");
- const auth2 = await registry.getApiKeyAndHeaders(model!);
- expect(auth2).toEqual({
- ok: true,
- apiKey: "token-2",
- headers: { Authorization: "Bearer token-2" },
- });
- });
- test("getApiKeyAndHeaders returns an error for failed authHeader resolution", async () => {
- writeRawModelsJson({
- "custom-provider": {
- ...providerWithApiKey("!exit 1"),
- authHeader: true,
- },
- });
- const registry = ModelRegistry.create(authStorage, modelsJsonPath);
- const model = registry.find("custom-provider", "test-model");
- expect(model).toBeDefined();
- const auth = await registry.getApiKeyAndHeaders(model!);
- expect(auth.ok).toBe(false);
- if (!auth.ok) {
- expect(auth.error).toContain('Failed to resolve API key for provider "custom-provider"');
- }
- });
- });
- });
- });
|