Skip to content

Commit b4d14d7

Browse files
authored
refactor(config): move model normalization to input owners (#115965)
* refactor(config): move model normalization to input owners * fix(config): merge canonical doctor model rows * fix(config): align touched model map identities * ci(env): lower production variable budget * chore: leave changelog to release automation
1 parent c6dfee1 commit b4d14d7

10 files changed

Lines changed: 1097 additions & 458 deletions
Lines changed: 22 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,34 @@
1+
import { collectManifestModelIdNormalizationPolicies } from "@openclaw/model-catalog-core/provider-model-id-normalization";
12
import { expectDefined } from "@openclaw/normalization-core";
2-
import { isRecord as isPlainRecord } from "@openclaw/normalization-core/record-coerce";
3-
import {
4-
listAgentEntries,
5-
readAgentRosterProperty,
6-
toAgentEntriesRecord,
7-
} from "../agents/agent-scope-config.js";
8-
import { normalizeConfiguredProviderCatalogModelId } from "../agents/model-ref-shared.js";
9-
import {
10-
normalizeAgentModelMapForConfig,
11-
normalizeAgentModelRefForConfig,
12-
} from "../config/model-input.js";
3+
import { normalizeSubmittedConfigModelRefs } from "../config/model-input-normalization.js";
4+
import { normalizeAgentModelRefForConfig } from "../config/model-input.js";
135
import type { OpenClawConfig } from "../config/types.openclaw.js";
6+
import { loadPluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
147
import type { PathSegment } from "./config-cli-path.js";
158

16-
function normalizeAgentDefaultModelValue(value: unknown): unknown {
17-
if (typeof value === "string") {
18-
return normalizeAgentModelRefForConfig(value);
19-
}
20-
if (!isPlainRecord(value)) {
21-
return value;
22-
}
23-
24-
const next: Record<string, unknown> = { ...value };
25-
if (typeof next.primary === "string") {
26-
next.primary = normalizeAgentModelRefForConfig(next.primary);
27-
}
28-
if (Array.isArray(next.fallbacks)) {
29-
next.fallbacks = next.fallbacks.map((fallback) =>
30-
typeof fallback === "string" ? normalizeAgentModelRefForConfig(fallback) : fallback,
31-
);
32-
}
33-
return next;
34-
}
35-
36-
function normalizeAgentListModelRefs(value: unknown): unknown {
37-
if (!Array.isArray(value)) {
38-
return value;
39-
}
40-
41-
let mutated = false;
42-
const next = value.map((agent) => {
43-
if (!isPlainRecord(agent)) {
44-
return agent;
45-
}
46-
47-
let nextAgent = agent;
48-
if (Object.hasOwn(agent, "model")) {
49-
const model = normalizeAgentDefaultModelValue(agent.model);
50-
if (model !== agent.model) {
51-
nextAgent = { ...nextAgent, model };
52-
mutated = true;
53-
}
54-
}
55-
if (isPlainRecord(agent.models)) {
56-
const models = normalizeAgentModelMapForConfig(agent.models);
57-
if (models !== agent.models) {
58-
nextAgent = { ...nextAgent, models };
59-
mutated = true;
60-
}
61-
}
62-
return nextAgent;
63-
});
64-
65-
return mutated ? next : value;
66-
}
67-
68-
function normalizeProviderCatalogModels(provider: string, models: unknown): unknown {
69-
if (!Array.isArray(models)) {
70-
return models;
71-
}
72-
73-
let mutated = false;
74-
const next = models.map((model) => {
75-
if (!isPlainRecord(model) || typeof model.id !== "string") {
76-
return model;
77-
}
78-
const trimmed = model.id.trim();
79-
if (!trimmed) {
80-
return model;
81-
}
82-
const id = normalizeConfiguredProviderCatalogModelId(provider, trimmed);
83-
if (id === model.id) {
84-
return model;
85-
}
86-
mutated = true;
87-
return { ...model, id };
88-
});
89-
90-
return mutated ? next : models;
91-
}
92-
93-
function normalizeModelProviderRefs(
94-
providers: NonNullable<OpenClawConfig["models"]>["providers"] | undefined,
95-
): unknown {
96-
if (!isPlainRecord(providers)) {
97-
return providers;
98-
}
99-
100-
let mutated = false;
101-
const nextProviders: Record<string, unknown> = { ...providers };
102-
for (const [provider, providerConfig] of Object.entries(providers)) {
103-
if (!isPlainRecord(providerConfig)) {
104-
continue;
105-
}
106-
const models = normalizeProviderCatalogModels(provider, providerConfig.models);
107-
if (models === providerConfig.models) {
108-
continue;
109-
}
110-
nextProviders[provider] = { ...providerConfig, models };
111-
mutated = true;
112-
}
113-
114-
return mutated ? nextProviders : providers;
115-
}
116-
1179
export function normalizeConfigMutationModelRefs(cfg: OpenClawConfig): OpenClawConfig {
118-
const defaults = cfg.agents?.defaults;
119-
const agentList = listAgentEntries(cfg);
120-
const roster = readAgentRosterProperty(cfg);
121-
const providers = cfg.models?.providers;
122-
const normalizedAgentList = normalizeAgentListModelRefs(agentList);
123-
const normalizedProviders = normalizeModelProviderRefs(providers) as typeof providers | undefined;
124-
125-
return {
126-
...cfg,
127-
...(defaults || normalizedAgentList !== agentList
128-
? {
129-
agents: {
130-
...cfg.agents,
131-
...(defaults
132-
? {
133-
defaults: {
134-
...defaults,
135-
...(defaults.model !== undefined
136-
? {
137-
model: normalizeAgentDefaultModelValue(
138-
defaults.model,
139-
) as typeof defaults.model,
140-
}
141-
: undefined),
142-
...(defaults.models !== undefined
143-
? { models: normalizeAgentModelMapForConfig(defaults.models) }
144-
: undefined),
145-
},
146-
}
147-
: undefined),
148-
...(normalizedAgentList !== agentList && roster?.kind === "entries"
149-
? { entries: toAgentEntriesRecord(normalizedAgentList as typeof agentList) }
150-
: normalizedAgentList !== agentList && roster?.kind === "list"
151-
? { list: normalizedAgentList as typeof agentList }
152-
: undefined),
153-
},
154-
}
155-
: undefined),
156-
...(normalizedProviders !== providers
157-
? { models: { ...cfg.models, providers: normalizedProviders } }
158-
: undefined),
159-
};
10+
const pluginMetadata = loadPluginMetadataSnapshot({ config: cfg, env: process.env });
11+
return normalizeSubmittedConfigModelRefs(
12+
cfg,
13+
collectManifestModelIdNormalizationPolicies(pluginMetadata.plugins),
14+
);
16015
}
16116

16217
export function normalizeConfigMutationExplicitSetPath(path: PathSegment[]): PathSegment[] {
163-
if (path.length >= 4 && path[0] === "agents" && path[1] === "defaults" && path[2] === "models") {
164-
const normalizedModelId = normalizeAgentModelRefForConfig(
165-
expectDefined(path[3], "path entry at 3"),
166-
);
167-
return normalizedModelId === path[3]
18+
const modelKeyIndex =
19+
path[0] === "agents" && path[1] === "defaults" && path[2] === "models"
20+
? 3
21+
: path[0] === "agents" &&
22+
(path[1] === "entries" || path[1] === "list") &&
23+
path[3] === "models"
24+
? 4
25+
: undefined;
26+
if (modelKeyIndex !== undefined && path.length > modelKeyIndex) {
27+
const modelId = expectDefined(path[modelKeyIndex], `path entry at ${modelKeyIndex}`);
28+
const normalizedModelId = normalizeAgentModelRefForConfig(modelId);
29+
return normalizedModelId === modelId
16830
? path
169-
: [...path.slice(0, 3), normalizedModelId, ...path.slice(4)];
31+
: [...path.slice(0, modelKeyIndex), normalizedModelId, ...path.slice(modelKeyIndex + 1)];
17032
}
17133
return path;
17234
}

‎src/cli/config-cli.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,31 @@ describe("config cli", () => {
855855
]);
856856
});
857857

858+
it("normalizes explicit per-agent model-map paths before writing config mutations", async () => {
859+
const resolved: OpenClawConfig = {
860+
agents: {
861+
entries: {
862+
ops: { models: { "google/gemini-3-pro-preview": {} } },
863+
},
864+
},
865+
};
866+
setSnapshot(resolved, resolved);
867+
868+
await runConfigCommand([
869+
"config",
870+
"set",
871+
"agents.entries.ops.models.google/gemini-3-pro-preview.alias",
872+
"gemini",
873+
]);
874+
875+
expect(firstWrittenConfig().agents?.entries?.ops?.models).toEqual({
876+
"google/gemini-3.1-pro-preview": { alias: "gemini" },
877+
});
878+
expect(requireWriteOptions().explicitSetPaths).toEqual([
879+
["agents", "entries", "ops", "models", "google/gemini-3.1-pro-preview", "alias"],
880+
]);
881+
});
882+
858883
it("normalizes agent-list model refs before writing config mutations", async () => {
859884
const resolved: OpenClawConfig = {
860885
agents: {
@@ -913,6 +938,52 @@ describe("config cli", () => {
913938
);
914939
});
915940

941+
it("normalizes manifest-backed provider catalog refs before writing config mutations", async () => {
942+
mockLoadPluginMetadataSnapshot.mockReturnValue(
943+
createPluginMetadataSnapshot({
944+
diagnostics: [],
945+
plugins: [
946+
createPluginManifestRecord({
947+
id: "myproxy-plugin",
948+
providers: ["myproxy"],
949+
modelIdNormalization: {
950+
providers: {
951+
myproxy: { aliases: { latest: "modern-model" }, prefixWhenBare: "vendor" },
952+
},
953+
},
954+
}),
955+
],
956+
}),
957+
);
958+
const resolved: OpenClawConfig = {
959+
models: {
960+
providers: {
961+
myproxy: {
962+
baseUrl: "https://proxy.example/v1",
963+
models: [
964+
{
965+
id: "latest",
966+
name: "Custom latest",
967+
reasoning: false,
968+
input: ["text"],
969+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
970+
contextWindow: 200_000,
971+
maxTokens: 8192,
972+
},
973+
],
974+
},
975+
},
976+
},
977+
};
978+
setSnapshot(resolved, resolved);
979+
980+
await runConfigCommand(["config", "set", "gateway.port", "18790"]);
981+
982+
expect(firstWrittenConfig().models?.providers?.myproxy?.models?.[0]?.id).toBe(
983+
"vendor/modern-model",
984+
);
985+
});
986+
916987
it("rejects plugin install record config updates", async () => {
917988
await expect(
918989
runConfigCommand([

0 commit comments

Comments
 (0)