Skip to content

Commit 35fb3f7

Browse files
fix: preserve models.json baseUrls on regen (#67893) (thanks @lawrence3699)
* models-config: preserve existing models.json baseUrls * test: distill models-config baseUrl regression test * fix: preserve models.json baseUrls on regen (#67893) (thanks @lawrence3699) --------- Co-authored-by: lawrence3699 <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent a189394 commit 35fb3f7

6 files changed

Lines changed: 76 additions & 46 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Docs: https://docs.openclaw.ai
99
- Onboarding/non-interactive: preserve existing gateway auth tokens during re-onboard so active local gateway clients are not disconnected by an implicit token rotation. (#67821) Thanks @BKF-Gitty.
1010
- OpenAI Codex/Responses: unify native Responses API capability detection so Codex OAuth requests emit the required `store: false` field on the native Responses path. (#67918) Thanks @obviyus.
1111
- WhatsApp/setup: guard personal-phone and allowlist prompt values so setup fails with clear validation errors instead of crashing on undefined prompt text. (#67895) Thanks @lawrence3699.
12+
- Models/config: preserve an existing `models.json` provider `baseUrl` during merge-mode regeneration so custom endpoints do not get reset on restart. (#67893) Thanks @lawrence3699.
1213

1314
## 2026.4.15
1415

‎src/agents/models-config.merge.test.ts‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,13 @@ describe("models-config merge helpers", () => {
134134
} as ExistingProviderConfig,
135135
},
136136
secretRefManagedProviders: new Set<string>(),
137-
explicitBaseUrlProviders: new Set<string>(["custom-proxy"]),
138137
});
139138

140139
expect(merged.existing?.baseUrl).toBe("http://localhost:1234/v1");
141140
expect(merged["custom-proxy"]?.baseUrl).toBe("http://localhost:4000/v1");
142141
});
143142

144-
it("preserves non-empty existing apiKey while explicit baseUrl wins", async () => {
143+
it("preserves non-empty existing apiKey and baseUrl from models.json", async () => {
145144
const merged = mergeWithExistingProviderSecrets({
146145
nextProviders: {
147146
custom: createConfigProvider(),
@@ -150,14 +149,13 @@ describe("models-config merge helpers", () => {
150149
custom: createExistingProvider(),
151150
},
152151
secretRefManagedProviders: new Set<string>(),
153-
explicitBaseUrlProviders: new Set<string>(["custom"]),
154152
});
155153

156154
expect(merged.custom?.apiKey).toBe(preservedApiKey);
157-
expect(merged.custom?.baseUrl).toBe("https://config.example/v1");
155+
expect(merged.custom?.baseUrl).toBe("https://agent.example/v1");
158156
});
159157

160-
it("preserves existing apiKey after explicit provider key normalization", async () => {
158+
it("preserves existing baseUrl after explicit provider key normalization", async () => {
161159
const normalized = mergeProviders({
162160
explicit: {
163161
" custom ": createConfigProvider(),
@@ -169,11 +167,10 @@ describe("models-config merge helpers", () => {
169167
custom: createExistingProvider(),
170168
},
171169
secretRefManagedProviders: new Set<string>(),
172-
explicitBaseUrlProviders: new Set<string>(["custom"]),
173170
});
174171

175172
expect(merged.custom?.apiKey).toBe(preservedApiKey);
176-
expect(merged.custom?.baseUrl).toBe("https://config.example/v1");
173+
expect(merged.custom?.baseUrl).toBe("https://agent.example/v1");
177174
});
178175

179176
it("preserves implicit provider headers when explicit config adds extra headers", async () => {
@@ -228,7 +225,6 @@ describe("models-config merge helpers", () => {
228225
} as ExistingProviderConfig,
229226
},
230227
secretRefManagedProviders: new Set<string>(),
231-
explicitBaseUrlProviders: new Set<string>(),
232228
});
233229

234230
expect(merged.custom).toEqual(
@@ -255,7 +251,6 @@ describe("models-config merge helpers", () => {
255251
custom: existingProvider,
256252
},
257253
secretRefManagedProviders: new Set<string>(),
258-
explicitBaseUrlProviders: new Set<string>(["custom"]),
259254
});
260255

261256
expect(merged.custom?.apiKey).toBe(preservedApiKey);
@@ -277,7 +272,6 @@ describe("models-config merge helpers", () => {
277272
} as ExistingProviderConfig,
278273
},
279274
secretRefManagedProviders: new Set<string>(),
280-
explicitBaseUrlProviders: new Set<string>(),
281275
});
282276

283277
expect(merged.custom?.apiKey).toBe("GOOGLE_API_KEY"); // pragma: allowlist secret
@@ -294,11 +288,10 @@ describe("models-config merge helpers", () => {
294288
}),
295289
},
296290
secretRefManagedProviders: new Set<string>(),
297-
explicitBaseUrlProviders: new Set<string>(["custom"]),
298291
});
299292

300293
expect(merged.custom?.apiKey).toBe("ALLCAPS_SAMPLE"); // pragma: allowlist secret
301-
expect(merged.custom?.baseUrl).toBe("https://config.example/v1");
294+
expect(merged.custom?.baseUrl).toBe("https://agent.example/v1");
302295
});
303296

304297
it("uses config apiKey/baseUrl when existing values are empty", async () => {
@@ -313,7 +306,6 @@ describe("models-config merge helpers", () => {
313306
}),
314307
},
315308
secretRefManagedProviders: new Set<string>(),
316-
explicitBaseUrlProviders: new Set<string>(["custom"]),
317309
});
318310

319311
expect(merged.custom?.apiKey).toBe(configApiKey);

‎src/agents/models-config.merge.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,11 @@ function shouldPreserveExistingApiKey(params: {
196196
}
197197

198198
function shouldPreserveExistingBaseUrl(params: {
199-
providerKey: string;
200199
existing: ExistingProviderConfig;
201200
nextEntry: ProviderConfig;
202-
explicitBaseUrlProviders: ReadonlySet<string>;
203201
}): boolean {
204-
const { providerKey, existing, nextEntry, explicitBaseUrlProviders } = params;
205-
if (
206-
explicitBaseUrlProviders.has(providerKey) ||
207-
typeof existing.baseUrl !== "string" ||
208-
existing.baseUrl.length === 0
209-
) {
202+
const { existing, nextEntry } = params;
203+
if (typeof existing.baseUrl !== "string" || existing.baseUrl.length === 0) {
210204
return false;
211205
}
212206

@@ -219,10 +213,8 @@ export function mergeWithExistingProviderSecrets(params: {
219213
nextProviders: Record<string, ProviderConfig>;
220214
existingProviders: Record<string, ExistingProviderConfig>;
221215
secretRefManagedProviders: ReadonlySet<string>;
222-
explicitBaseUrlProviders: ReadonlySet<string>;
223216
}): Record<string, ProviderConfig> {
224-
const { nextProviders, existingProviders, secretRefManagedProviders, explicitBaseUrlProviders } =
225-
params;
217+
const { nextProviders, existingProviders, secretRefManagedProviders } = params;
226218
const mergedProviders: Record<string, ProviderConfig> = {};
227219
for (const [key, entry] of Object.entries(existingProviders)) {
228220
mergedProviders[key] = entry;
@@ -246,10 +238,8 @@ export function mergeWithExistingProviderSecrets(params: {
246238
}
247239
if (
248240
shouldPreserveExistingBaseUrl({
249-
providerKey: key,
250241
existing,
251242
nextEntry: newEntry,
252-
explicitBaseUrlProviders,
253243
})
254244
) {
255245
preserved.baseUrl = existing.baseUrl;

‎src/agents/models-config.plan.ts‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,11 @@ export async function resolveProvidersForModelsJsonWithDeps(
5858
});
5959
}
6060

61-
function resolveExplicitBaseUrlProviders(
62-
providers: OpenClawConfig["models"] | undefined,
63-
): ReadonlySet<string> {
64-
return new Set(
65-
Object.entries(providers?.providers ?? {})
66-
.map(([key, provider]) => [key.trim(), provider] as const)
67-
.filter(
68-
([key, provider]) =>
69-
Boolean(key) && typeof provider?.baseUrl === "string" && provider.baseUrl.trim(),
70-
)
71-
.map(([key]) => key),
72-
);
73-
}
74-
7561
function resolveProvidersForMode(params: {
7662
mode: NonNullable<ModelsConfig["mode"]>;
7763
existingParsed: unknown;
7864
providers: Record<string, ProviderConfig>;
7965
secretRefManagedProviders: ReadonlySet<string>;
80-
explicitBaseUrlProviders: ReadonlySet<string>;
8166
}): Record<string, ProviderConfig> {
8267
if (params.mode !== "merge") {
8368
return params.providers;
@@ -94,7 +79,6 @@ function resolveProvidersForMode(params: {
9479
nextProviders: params.providers,
9580
existingProviders: existingProviders as Record<string, ExistingProviderConfig>,
9681
secretRefManagedProviders: params.secretRefManagedProviders,
97-
explicitBaseUrlProviders: params.explicitBaseUrlProviders,
9882
});
9983
}
10084

@@ -135,7 +119,6 @@ export async function planOpenClawModelsJsonWithDeps(
135119
existingParsed: params.existingParsed,
136120
providers: normalizedProviders,
137121
secretRefManagedProviders,
138-
explicitBaseUrlProviders: resolveExplicitBaseUrlProviders(cfg.models),
139122
});
140123
const secretEnforcedProviders =
141124
enforceSourceManagedProviderSecrets({

‎src/agents/models-config.runtime-source-snapshot.test.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ describe("models-config runtime source snapshot", () => {
279279
openai: {
280280
...runtimeConfig.models!.providers!.openai,
281281
baseUrl: "https://api.openai.com/v1",
282+
headers: {
283+
"X-OpenClaw-Test": "one",
284+
},
282285
},
283286
},
284287
},
@@ -290,6 +293,9 @@ describe("models-config runtime source snapshot", () => {
290293
openai: {
291294
...runtimeConfig.models!.providers!.openai,
292295
baseUrl: "https://mirror.example/v1",
296+
headers: {
297+
"X-OpenClaw-Test": "two",
298+
},
293299
},
294300
},
295301
},
@@ -299,17 +305,26 @@ describe("models-config runtime source snapshot", () => {
299305
setRuntimeConfigSnapshot(runtimeConfig, sourceConfig);
300306
await ensureOpenClawModelsJson(firstCandidate);
301307
let parsed = await readGeneratedModelsJson<{
302-
providers: Record<string, { baseUrl?: string; apiKey?: string }>;
308+
providers: Record<
309+
string,
310+
{ baseUrl?: string; apiKey?: string; headers?: Record<string, string> }
311+
>;
303312
}>();
304313
expect(parsed.providers.openai?.baseUrl).toBe("https://api.openai.com/v1");
305314
expect(parsed.providers.openai?.apiKey).toBe("OPENAI_API_KEY"); // pragma: allowlist secret
315+
expect(parsed.providers.openai?.headers?.["X-OpenClaw-Test"]).toBe("one");
306316

317+
// Header changes still rewrite models.json, but merge mode preserves the existing baseUrl.
307318
await ensureOpenClawModelsJson(secondCandidate);
308319
parsed = await readGeneratedModelsJson<{
309-
providers: Record<string, { baseUrl?: string; apiKey?: string }>;
320+
providers: Record<
321+
string,
322+
{ baseUrl?: string; apiKey?: string; headers?: Record<string, string> }
323+
>;
310324
}>();
311-
expect(parsed.providers.openai?.baseUrl).toBe("https://mirror.example/v1");
325+
expect(parsed.providers.openai?.baseUrl).toBe("https://api.openai.com/v1");
312326
expect(parsed.providers.openai?.apiKey).toBe("OPENAI_API_KEY"); // pragma: allowlist secret
327+
expect(parsed.providers.openai?.headers?.["X-OpenClaw-Test"]).toBe("two");
313328
} finally {
314329
clearRuntimeConfigSnapshot();
315330
clearConfigCache();

‎src/agents/models-config.uses-first-github-copilot-profile-env-tokens.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,55 @@ describe("models-config", () => {
124124
});
125125
});
126126

127+
it("keeps a non-empty existing models.json baseUrl when merge mode regenerates the provider", async () => {
128+
const kilocodeProvider = {
129+
baseUrl: "https://api.kilo.ai/api/gateway/v1",
130+
api: "openai-completions" as const,
131+
models: [],
132+
};
133+
const existingContents = `${JSON.stringify(
134+
{
135+
providers: {
136+
kilocode: {
137+
baseUrl: "https://api.kilo.ai/api/gateway",
138+
api: "openai-completions",
139+
models: [],
140+
},
141+
},
142+
},
143+
null,
144+
2,
145+
)}\n`;
146+
147+
const plan = await planOpenClawModelsJsonWithDeps(
148+
{
149+
cfg: {
150+
models: {
151+
providers: {
152+
kilocode: kilocodeProvider,
153+
},
154+
},
155+
},
156+
sourceConfigForSecrets: {
157+
models: {
158+
providers: {
159+
kilocode: kilocodeProvider,
160+
},
161+
},
162+
},
163+
agentDir: "/tmp/openclaw-agent",
164+
env: {} as NodeJS.ProcessEnv,
165+
existingRaw: existingContents,
166+
existingParsed: JSON.parse(existingContents),
167+
},
168+
{
169+
resolveImplicitProviders: async () => ({}),
170+
},
171+
);
172+
173+
expect(plan).toEqual({ action: "noop" });
174+
});
175+
127176
it("uses tokenRef env var when github-copilot profile omits plaintext token", () => {
128177
const auth = createProviderAuthResolver(
129178
{

0 commit comments

Comments
 (0)