Skip to content

Commit 66c7d72

Browse files
authored
fix(models): seed configured provider model entries from catalog metadata (#126068)
* fix(config): seed configured provider model entries from catalog metadata applyModelDefaults replaced omitted input/reasoning/cost/maxTokens on models.providers entries with generic defaults at config load, erasing the fact that the operator never authored them. An override entry that pins only sizing fields materialized as a text-only, non-reasoning, zero-cost model, which silently dropped vision-gated tools (most visibly the computer tool) for that model downstream. Seed omitted fields from the owning plugin manifest catalog row (same provider+model id, using the existing id-normalization policies) before falling back to generic defaults. Authored fields always win; entries with no catalog row behave exactly as before. * fix(config): preserve tiered pricing when seeding model cost defaults resolveModelCost keeps only flat per-token fields; carry an authored or catalog tieredPricing table through explicitly so cost defaulting does not silently discard it.
1 parent 54ebb30 commit 66c7d72

2 files changed

Lines changed: 319 additions & 17 deletions

File tree

‎src/config/defaults.test.ts‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,201 @@ describe("config defaults", () => {
138138
expect(next.agents?.defaults?.subagents?.maxConcurrent).toBe(DEFAULT_SUBAGENT_MAX_CONCURRENT);
139139
});
140140
});
141+
142+
describe("applyModelDefaults catalog seeding", () => {
143+
const catalogRegistry = {
144+
plugins: [
145+
{
146+
id: "openai",
147+
modelCatalog: {
148+
providers: {
149+
openai: {
150+
models: [
151+
{
152+
id: "gpt-5.6-sol",
153+
name: "GPT-5.6 Sol",
154+
reasoning: true,
155+
input: ["text", "image"],
156+
contextWindow: 400_000,
157+
contextTokens: 272_000,
158+
maxTokens: 128_000,
159+
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
160+
thinkingLevelMap: { off: "none" },
161+
},
162+
],
163+
},
164+
},
165+
},
166+
},
167+
],
168+
// SAFETY: minimal manifest record carrying only the fields applyModelDefaults reads.
169+
} as never;
170+
171+
// Regression: an override entry pinning only sizing fields materialized as a
172+
// text-only, non-reasoning, zero-cost model, silently dropping vision-gated
173+
// tools (like `computer`) for that model downstream.
174+
it("fills omitted fields from the owning catalog row before generic defaults", async () => {
175+
const { applyModelDefaults } = await import("./defaults.js");
176+
const cfg = applyModelDefaults(
177+
{
178+
models: {
179+
providers: {
180+
openai: {
181+
baseUrl: "https://api.openai.com/v1",
182+
models: [
183+
// SAFETY: mirrors a real operator config entry that omits input/reasoning/cost.
184+
{
185+
id: "gpt-5.6-sol",
186+
name: "GPT-5.6",
187+
contextWindow: 1_050_000,
188+
contextTokens: 922_000,
189+
} as never,
190+
],
191+
},
192+
},
193+
},
194+
},
195+
{ manifestRegistry: catalogRegistry },
196+
);
197+
const model = expectDefined(
198+
cfg.models?.providers?.openai?.models?.[0],
199+
"materialized model entry",
200+
);
201+
expect(model.input).toEqual(["text", "image"]);
202+
expect(model.reasoning).toBe(true);
203+
expect(model.cost).toEqual({ input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 });
204+
expect(model.maxTokens).toBe(128_000);
205+
expect(model.thinkingLevelMap).toEqual({ off: "none" });
206+
// Authored fields stay authoritative.
207+
expect(model.contextWindow).toBe(1_050_000);
208+
expect(model.contextTokens).toBe(922_000);
209+
expect(model.name).toBe("GPT-5.6");
210+
});
211+
212+
it("keeps authored metadata authoritative over the catalog row", async () => {
213+
const { applyModelDefaults } = await import("./defaults.js");
214+
const cfg = applyModelDefaults(
215+
{
216+
models: {
217+
providers: {
218+
openai: {
219+
baseUrl: "https://api.openai.com/v1",
220+
models: [
221+
{
222+
id: "gpt-5.6-sol",
223+
name: "text-only override",
224+
reasoning: false,
225+
input: ["text"],
226+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
227+
contextWindow: 8_192,
228+
maxTokens: 4_096,
229+
},
230+
],
231+
},
232+
},
233+
},
234+
},
235+
{ manifestRegistry: catalogRegistry },
236+
);
237+
const model = expectDefined(
238+
cfg.models?.providers?.openai?.models?.[0],
239+
"materialized model entry",
240+
);
241+
expect(model.input).toEqual(["text"]);
242+
expect(model.reasoning).toBe(false);
243+
expect(model.maxTokens).toBe(4_096);
244+
});
245+
246+
it("preserves catalog tiered pricing when flat cost fields are authored", async () => {
247+
const { applyModelDefaults } = await import("./defaults.js");
248+
const tieredRegistry = {
249+
plugins: [
250+
{
251+
id: "openai",
252+
modelCatalog: {
253+
providers: {
254+
openai: {
255+
models: [
256+
{
257+
id: "gpt-5.6-sol",
258+
name: "GPT-5.6 Sol",
259+
input: ["text", "image"],
260+
cost: {
261+
input: 5,
262+
output: 30,
263+
cacheRead: 0.5,
264+
cacheWrite: 6.25,
265+
tieredPricing: [
266+
{
267+
input: 2.5,
268+
output: 15,
269+
cacheRead: 0.25,
270+
cacheWrite: 3,
271+
maxInputTokens: 200_000,
272+
},
273+
],
274+
},
275+
},
276+
],
277+
},
278+
},
279+
},
280+
},
281+
],
282+
// SAFETY: minimal manifest record carrying only the fields applyModelDefaults reads.
283+
} as never;
284+
const cfg = applyModelDefaults(
285+
{
286+
models: {
287+
providers: {
288+
openai: {
289+
baseUrl: "https://api.openai.com/v1",
290+
models: [
291+
// SAFETY: mirrors an operator override with explicit flat cost only.
292+
{
293+
id: "gpt-5.6-sol",
294+
name: "GPT-5.6",
295+
cost: { input: 4, output: 24, cacheRead: 0.4, cacheWrite: 5 },
296+
} as never,
297+
],
298+
},
299+
},
300+
},
301+
},
302+
{ manifestRegistry: tieredRegistry },
303+
);
304+
const model = expectDefined(
305+
cfg.models?.providers?.openai?.models?.[0],
306+
"materialized model entry",
307+
);
308+
expect(model.cost?.input).toBe(4);
309+
expect(model.cost?.tieredPricing).toHaveLength(1);
310+
expect(model.input).toEqual(["text", "image"]);
311+
});
312+
313+
it("falls back to generic defaults when no catalog row matches", async () => {
314+
const { applyModelDefaults } = await import("./defaults.js");
315+
const cfg = applyModelDefaults(
316+
{
317+
models: {
318+
providers: {
319+
custom: {
320+
baseUrl: "https://custom.example.com/v1",
321+
models: [
322+
// SAFETY: mirrors a real operator config entry that omits input/reasoning/cost.
323+
{ id: "house-model", name: "House Model" } as never,
324+
],
325+
},
326+
},
327+
},
328+
},
329+
{ manifestRegistry: catalogRegistry },
330+
);
331+
const model = expectDefined(
332+
cfg.models?.providers?.custom?.models?.[0],
333+
"materialized model entry",
334+
);
335+
expect(model.input).toEqual(["text"]);
336+
expect(model.reasoning).toBe(false);
337+
});
338+
});

‎src/config/defaults.ts‎

Lines changed: 121 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,59 @@ export function applyTalkConfigNormalization(config: OpenClawConfig): OpenClawCo
157157
return normalizeTalkConfig(config);
158158
}
159159

160+
/** Catalog metadata eligible to fill fields the operator did not author. */
161+
type CatalogSeedModel = Pick<
162+
ModelDefinitionConfig,
163+
| "input"
164+
| "reasoning"
165+
| "cost"
166+
| "contextWindow"
167+
| "contextTokens"
168+
| "maxTokens"
169+
| "thinkingLevelMap"
170+
| "compat"
171+
>;
172+
173+
/**
174+
* Indexes plugin manifest catalog rows so configured model entries can inherit
175+
* metadata the operator omitted. Without this, materialization would turn an
176+
* override entry that pins only sizing fields into a text-only, non-reasoning,
177+
* zero-cost model — silently dropping vision-gated tools downstream.
178+
*/
179+
function buildManifestCatalogModelLookup(
180+
manifestRegistry: Pick<PluginManifestRegistry, "plugins"> | undefined,
181+
policies: ReturnType<typeof collectManifestModelIdNormalizationPolicies> | undefined,
182+
): (providerId: string, modelId: string) => Partial<CatalogSeedModel> | undefined {
183+
const plugins = manifestRegistry?.plugins;
184+
if (!plugins || plugins.length === 0) {
185+
return () => undefined;
186+
}
187+
let index: Map<string, Partial<CatalogSeedModel>> | undefined;
188+
const keyFor = (providerId: string, modelId: string) =>
189+
normalizeProviderId(providerId) +
190+
" " +
191+
normalizeConfiguredProviderCatalogModelId(providerId, modelId, policies).toLowerCase();
192+
return (providerId, modelId) => {
193+
if (!index) {
194+
index = new Map();
195+
for (const plugin of plugins) {
196+
for (const [catalogProviderId, provider] of Object.entries(
197+
plugin.modelCatalog?.providers ?? {},
198+
)) {
199+
for (const model of provider.models) {
200+
const key = keyFor(catalogProviderId, model.id);
201+
if (!index.has(key)) {
202+
// SAFETY: ModelCatalogModel's seed fields are a structural subset of ModelDefinitionConfig; only the picked metadata fields are read from this entry.
203+
index.set(key, model as Partial<CatalogSeedModel>);
204+
}
205+
}
206+
}
207+
}
208+
}
209+
return index.get(keyFor(providerId, modelId));
210+
};
211+
}
212+
160213
export function applyModelDefaults(
161214
cfg: OpenClawConfig,
162215
options: ProviderPolicyDefaultsOptions = {},
@@ -170,6 +223,10 @@ export function applyModelDefaults(
170223
const modelIdNormalizationPolicies = manifestRegistry
171224
? collectManifestModelIdNormalizationPolicies(manifestRegistry.plugins)
172225
: undefined;
226+
const resolveCatalogModel = buildManifestCatalogModelLookup(
227+
manifestRegistry,
228+
modelIdNormalizationPolicies,
229+
);
173230
const nextProviders = { ...providerConfig };
174231
for (const [providerId, provider] of Object.entries(providerConfig)) {
175232
const normalizedProvider = normalizeProviderConfigForConfigDefaults({
@@ -206,33 +263,58 @@ export function applyModelDefaults(
206263
modelMutated = true;
207264
}
208265

209-
const reasoning = typeof raw.reasoning === "boolean" ? raw.reasoning : false;
266+
// Config entries are overrides, not full definitions: authored fields
267+
// win, the owning catalog row fills omitted fields, and only then do
268+
// generic defaults apply. Defaulting straight past the catalog would
269+
// erase field absence (for example turning an entry that pins only
270+
// contextWindow into a text-only model, dropping vision-gated tools).
271+
const catalogModel = resolveCatalogModel(providerId, id);
272+
const reasoning =
273+
typeof raw.reasoning === "boolean" ? raw.reasoning : (catalogModel?.reasoning ?? false);
210274
if (raw.reasoning !== reasoning) {
211275
modelMutated = true;
212276
}
213277

214-
const input = raw.input ?? [...DEFAULT_MODEL_INPUT];
278+
const input = raw.input ?? catalogModel?.input ?? [...DEFAULT_MODEL_INPUT];
215279
if (raw.input === undefined) {
216280
modelMutated = true;
217281
}
218282

219-
const cost = resolveModelCost(raw.cost);
283+
const cost = resolveModelCost(
284+
raw.cost || catalogModel?.cost ? { ...catalogModel?.cost, ...raw.cost } : undefined,
285+
);
286+
// resolveModelCost keeps only the flat per-token fields; carry tiered
287+
// pricing through explicitly so an authored or catalog tier table is
288+
// not silently discarded when other cost fields are defaulted.
289+
const tieredPricing = raw.cost?.tieredPricing ?? catalogModel?.cost?.tieredPricing;
290+
if (tieredPricing) {
291+
cost.tieredPricing = tieredPricing;
292+
}
220293
const costMutated =
221294
!raw.cost ||
222295
raw.cost.input !== cost.input ||
223296
raw.cost.output !== cost.output ||
224297
raw.cost.cacheRead !== cost.cacheRead ||
225-
raw.cost.cacheWrite !== cost.cacheWrite;
298+
raw.cost.cacheWrite !== cost.cacheWrite ||
299+
raw.cost.tieredPricing !== cost.tieredPricing;
226300
if (costMutated) {
227301
modelMutated = true;
228302
}
229303

230-
const contextWindow = isPositiveNumber(raw.contextWindow) ? raw.contextWindow : undefined;
304+
const contextWindow = isPositiveNumber(raw.contextWindow)
305+
? raw.contextWindow
306+
: isPositiveNumber(catalogModel?.contextWindow)
307+
? catalogModel.contextWindow
308+
: undefined;
231309
if (raw.contextWindow !== contextWindow) {
232310
modelMutated = true;
233311
}
234312

235-
const contextTokens = isPositiveNumber(raw.contextTokens) ? raw.contextTokens : undefined;
313+
const contextTokens = isPositiveNumber(raw.contextTokens)
314+
? raw.contextTokens
315+
: isPositiveNumber(catalogModel?.contextTokens)
316+
? catalogModel.contextTokens
317+
: undefined;
236318
if (raw.contextTokens !== contextTokens) {
237319
modelMutated = true;
238320
}
@@ -242,7 +324,11 @@ export function applyModelDefaults(
242324
providerMaxTokens ?? DEFAULT_MODEL_MAX_TOKENS,
243325
maxTokenContextWindow,
244326
);
245-
const rawMaxTokens = isPositiveNumber(raw.maxTokens) ? raw.maxTokens : defaultMaxTokens;
327+
const rawMaxTokens = isPositiveNumber(raw.maxTokens)
328+
? raw.maxTokens
329+
: isPositiveNumber(catalogModel?.maxTokens)
330+
? catalogModel.maxTokens
331+
: defaultMaxTokens;
246332
const maxTokens = resolveNormalizedProviderModelMaxTokens({
247333
providerId,
248334
modelId: id,
@@ -257,20 +343,38 @@ export function applyModelDefaults(
257343
modelMutated = true;
258344
}
259345

346+
const thinkingLevelMap =
347+
raw.thinkingLevelMap === undefined && catalogModel?.thinkingLevelMap !== undefined
348+
? catalogModel.thinkingLevelMap
349+
: undefined;
350+
const compat =
351+
raw.compat === undefined && catalogModel?.compat !== undefined
352+
? catalogModel.compat
353+
: undefined;
354+
if (thinkingLevelMap !== undefined || compat !== undefined) {
355+
modelMutated = true;
356+
}
357+
260358
if (!modelMutated) {
261359
return model;
262360
}
263361
providerMutated = true;
264-
return Object.assign({}, raw, {
265-
id,
266-
reasoning,
267-
input,
268-
cost,
269-
contextWindow,
270-
contextTokens,
271-
maxTokens,
272-
api,
273-
}) as ModelDefinitionConfig;
362+
return Object.assign(
363+
{},
364+
raw,
365+
{
366+
id,
367+
reasoning,
368+
input,
369+
cost,
370+
contextWindow,
371+
contextTokens,
372+
maxTokens,
373+
api,
374+
},
375+
thinkingLevelMap !== undefined ? { thinkingLevelMap } : {},
376+
compat !== undefined ? { compat } : {},
377+
) as ModelDefinitionConfig;
274378
});
275379

276380
if (!providerMutated) {

0 commit comments

Comments
 (0)