fix: treat model api drift as baseUrl refresh

This commit is contained in:
Peter Steinberger
2026-03-08 16:04:27 +00:00
parent fa00b1d0ca
commit 79c5c660bb
2 changed files with 52 additions and 3 deletions

View File

@@ -65,7 +65,7 @@ async function runCustomProviderMergeTest(params: {
baseUrl: string;
apiKey: string;
api: string;
models: Array<{ id: string; name: string; input: string[] }>;
models: Array<{ id: string; name: string; input: string[]; api?: string }>;
};
existingProviderKey?: string;
configProviderKey?: string;
@@ -261,6 +261,28 @@ describe("models-config", () => {
});
});
it("replaces stale merged baseUrl when only model-level apis change", async () => {
await withTempHome(async () => {
const parsed = await runCustomProviderMergeTest({
seedProvider: {
baseUrl: "https://agent.example/v1",
apiKey: "AGENT_KEY", // pragma: allowlist secret
api: "",
models: [
{
id: "agent-model",
name: "Agent model",
input: ["text"],
api: "openai-completions",
},
],
},
});
expect(parsed.providers.custom?.apiKey).toBe("AGENT_KEY");
expect(parsed.providers.custom?.baseUrl).toBe("https://config.example/v1");
});
});
it("replaces stale merged apiKey when provider is SecretRef-managed in current config", async () => {
await withTempHome(async () => {
await writeAgentModelsJson({

View File

@@ -178,6 +178,33 @@ function resolveProviderApi(entry: { api?: unknown } | undefined): string | unde
return api || undefined;
}
function resolveModelApiSurface(entry: { models?: unknown } | undefined): string | undefined {
if (!Array.isArray(entry?.models)) {
return undefined;
}
const apis = entry.models
.flatMap((model) => {
if (!model || typeof model !== "object") {
return [];
}
const api = (model as { api?: unknown }).api;
return typeof api === "string" && api.trim() ? [api.trim()] : [];
})
.toSorted();
if (apis.length === 0) {
return undefined;
}
return JSON.stringify(apis);
}
function resolveProviderApiSurface(
entry: ({ api?: unknown; models?: unknown } & Record<string, unknown>) | undefined,
): string | undefined {
return resolveProviderApi(entry) ?? resolveModelApiSurface(entry);
}
function shouldPreserveExistingApiKey(params: {
providerKey: string;
existing: ExistingProviderConfig;
@@ -207,8 +234,8 @@ function shouldPreserveExistingBaseUrl(params: {
return false;
}
const existingApi = resolveProviderApi(existing);
const nextApi = resolveProviderApi(nextEntry);
const existingApi = resolveProviderApiSurface(existing);
const nextApi = resolveProviderApiSurface(nextEntry);
return !existingApi || !nextApi || existingApi === nextApi;
}