|
| 1 | +import { collectManifestModelIdNormalizationPolicies } from "@openclaw/model-catalog-core/provider-model-id-normalization"; |
1 | 2 | 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"; |
13 | 5 | import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 6 | +import { loadPluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js"; |
14 | 7 | import type { PathSegment } from "./config-cli-path.js"; |
15 | 8 |
|
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 | | - |
117 | 9 | 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 | + ); |
160 | 15 | } |
161 | 16 |
|
162 | 17 | 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 |
168 | 30 | ? path |
169 | | - : [...path.slice(0, 3), normalizedModelId, ...path.slice(4)]; |
| 31 | + : [...path.slice(0, modelKeyIndex), normalizedModelId, ...path.slice(modelKeyIndex + 1)]; |
170 | 32 | } |
171 | 33 | return path; |
172 | 34 | } |
0 commit comments