Skip to content

Commit 89c004f

Browse files
authored
fix: expose agent model settings in the web form (#132353)
* fix: expose agent model settings in the web form * refactor: consolidate model key rejection handling * test: await durable draft retirement before reload
1 parent 027df17 commit 89c004f

10 files changed

Lines changed: 399 additions & 28 deletions

‎docs/tools/code-mode.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ Activation resolves from the first explicit setting in this order:
207207
In the Control UI, open **Settings → Agents → Agent defaults**, show **Advanced**
208208
settings, and find **Models** under **Agent Defaults**. Each model has a
209209
**Code Mode** selector beside its runtime: **Default** removes the override,
210-
**On** saves `true`, and **Off** saves `false`. Use **Raw** config or the CLI
211-
for agent-specific model overrides; the **Agent List** form does not yet
212-
support its constrained map schema.
210+
**On** saves `true`, and **Off** saves `false`. For agent-specific overrides,
211+
expand **Agent List**, then the agent's **Agent Model Overrides**. Unsupported fields remain
212+
marked for **Raw** editing without hiding the supported settings beside them.
213213

214214
Overrides affect the selected model on future runs, including fallback models;
215215
they do not enable tools on a tool-free run or change runtime selection. The

‎ui/src/components/config-form-collection-draft.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type ConfigFormCollectionDraftProps = {
1313
identity: string;
1414
sourceIdentity: unknown;
1515
existingKeys?: readonly string[];
16+
validateKey?: (key: string) => boolean;
1617
existingValues?: readonly unknown[];
1718
validateValue?: (value: unknown) => boolean;
1819
};
@@ -152,7 +153,10 @@ export class ConfigFormCollectionDraft extends OpenClawLightDomElement {
152153
return;
153154
}
154155
const key = this.draftKey.trim();
155-
if (props.existingKeys && (!key || props.existingKeys.includes(key))) {
156+
if (
157+
props.existingKeys &&
158+
(!key || props.existingKeys.includes(key) || props.validateKey?.(key) === false)
159+
) {
156160
this.fail("key", t("configForm.invalidString"));
157161
return;
158162
}

‎ui/src/components/config-form-map-integrity.browser.test.ts‎

Lines changed: 194 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "../lib/config/config-draft-model.ts";
88
import { createInitialConfigState } from "../lib/config/config-state-model.ts";
99
import { ConfigFormCollectionDraft } from "./config-form-collection-draft.ts";
10+
import type { JsonSchema } from "./config-form.shared.ts";
1011
import { analyzeConfigSchema, renderConfigForm } from "./config-form.ts";
1112

1213
function expectElement<T extends Element>(element: T | null | undefined, label: string): T {
@@ -32,13 +33,35 @@ describe("config form map integrity", () => {
3233
});
3334
const config = (codeMode?: boolean) => ({
3435
agents: wrapScope({
35-
models: { "example/model": modelSettings(codeMode), "example/other": { alias: "other" } },
36+
models: {
37+
"example/model.v1": modelSettings(codeMode),
38+
"example/other": { alias: "other" },
39+
},
40+
...(scope === "agent"
41+
? { tools: { codeMode: { enabled: "auto", maxOutputBytes: 4096 } } }
42+
: {}),
3643
}),
3744
tools: { codeMode: { enabled: "auto", maxOutputBytes: 4096 } },
3845
});
3946
const modelSchema = {
4047
type: "object",
4148
properties: {
49+
...(scope === "agent"
50+
? {
51+
tools: {
52+
type: "object",
53+
properties: {
54+
codeMode: {
55+
anyOf: [
56+
{ type: "boolean" },
57+
{ const: "auto" },
58+
{ type: "object", additionalProperties: true },
59+
],
60+
},
61+
},
62+
},
63+
}
64+
: {}),
4265
models: {
4366
type: "object",
4467
additionalProperties: {
@@ -62,7 +85,13 @@ describe("config form map integrity", () => {
6285
properties:
6386
scope === "defaults"
6487
? { defaults: modelSchema }
65-
: { entries: { type: "object", additionalProperties: modelSchema } },
88+
: {
89+
entries: {
90+
type: "object",
91+
propertyNames: { type: "string", pattern: "^[a-z0-9_][a-z0-9_-]{0,63}$" },
92+
additionalProperties: modelSchema,
93+
},
94+
},
6695
},
6796
},
6897
});
@@ -94,6 +123,9 @@ describe("config form map integrity", () => {
94123
);
95124

96125
renderValue();
126+
if (scope === "agent") {
127+
expect(container.textContent).toContain("Unsupported schema node. Use Raw mode.");
128+
}
97129
expect(Array.from(getSelect().options, (option) => option.textContent?.trim())).toEqual([
98130
"Default",
99131
"On",
@@ -170,13 +202,11 @@ describe("config form map integrity", () => {
170202
});
171203

172204
it.each([
173-
["pattern", { type: "string", pattern: "^[a-z]+$" }],
174-
["minimum length", { type: "string", minLength: 1 }],
175-
["enumeration", { enum: ["primary"] }],
176205
["boolean", true],
177206
["null", null],
178207
["array", [{ type: "string" }]],
179208
["wrong type", { type: "number" }],
209+
["unknown constraint", { type: "string", not: { const: "blocked" } }],
180210
["inherited type", Object.assign(Object.create({ type: "string" }), { unknown: true })],
181211
])("keeps %s property-name constraints fail-closed", (_label, propertyNames) => {
182212
const analysis = analyzeConfigSchema({
@@ -193,6 +223,165 @@ describe("config form map integrity", () => {
193223
expect(analysis.unsupportedPaths).toEqual(["values"]);
194224
});
195225

226+
it.each([
227+
{ label: "pattern", names: { type: "string", pattern: "^[a-z]+$" }, invalid: "bad/key" },
228+
{ label: "minimum length", names: { type: "string", minLength: 3 }, invalid: "x" },
229+
{ label: "enumeration", names: { enum: ["primary", "backup"] }, invalid: "other" },
230+
{
231+
label: "intersection",
232+
names: { allOf: [{ minLength: 3 }, { pattern: "^[a-z]+$" }] },
233+
invalid: "bad/key",
234+
},
235+
])(
236+
"rejects $label key edits even when an existing value is invalid",
237+
async ({ names, invalid }) => {
238+
const analysis = analyzeConfigSchema({
239+
type: "object",
240+
properties: {
241+
values: {
242+
type: "object",
243+
propertyNames: names,
244+
additionalProperties: { type: "string", pattern: "^[0-9]+$" },
245+
},
246+
},
247+
});
248+
expect(analysis.unsupportedPaths).toEqual([]);
249+
const state = createInitialConfigState();
250+
state.configSchema = analysis.schema;
251+
state.configForm = { values: { primary: "invalid existing value" } };
252+
const onPatch = vi.fn((path: Array<string | number>, value: unknown) =>
253+
updateConfigFormValue(state, path, value),
254+
);
255+
const container = document.createElement("div");
256+
document.body.append(container);
257+
try {
258+
render(
259+
renderConfigForm({
260+
schema: analysis.schema,
261+
uiHints: {},
262+
unsupportedPaths: analysis.unsupportedPaths,
263+
value: state.configForm,
264+
showAdvanced: true,
265+
onShowAdvanced: () => {},
266+
onPatch,
267+
}),
268+
container,
269+
);
270+
const keyInput = expectElement(
271+
container.querySelector<HTMLInputElement>('[aria-label="Key: primary"]'),
272+
"existing key",
273+
);
274+
keyInput.value = invalid;
275+
keyInput.dispatchEvent(new Event("change", { bubbles: true }));
276+
expect(keyInput.value).toBe("primary");
277+
expect(onPatch).not.toHaveBeenCalled();
278+
279+
const draft = expectElement(
280+
container.querySelector<ConfigFormCollectionDraft>(
281+
"openclaw-config-form-collection-draft",
282+
),
283+
"map draft",
284+
);
285+
expectElement(
286+
Array.from(container.querySelectorAll("button")).find(
287+
(button) => button.textContent?.trim() === "Add Entry",
288+
),
289+
"add entry",
290+
).click();
291+
await draft.updateComplete;
292+
const key = expectElement(
293+
draft.querySelector<HTMLInputElement>("[data-collection-draft-key]"),
294+
"draft key",
295+
);
296+
const value = expectElement(
297+
draft.querySelector<HTMLInputElement>("[data-collection-draft-value]"),
298+
"draft value",
299+
);
300+
key.value = invalid;
301+
key.dispatchEvent(new Event("input", { bubbles: true }));
302+
value.value = "123";
303+
value.dispatchEvent(new Event("input", { bubbles: true }));
304+
await draft.updateComplete;
305+
expectElement(
306+
Array.from(draft.querySelectorAll("button")).find(
307+
(button) => button.textContent?.trim() === "Add Entry",
308+
),
309+
"commit entry",
310+
).click();
311+
await draft.updateComplete;
312+
expect(key.getAttribute("aria-invalid")).toBe("true");
313+
expect(key.value).toBe(invalid);
314+
expect(onPatch).not.toHaveBeenCalled();
315+
key.value = "backup";
316+
key.dispatchEvent(new Event("input", { bubbles: true }));
317+
await draft.updateComplete;
318+
expectElement(
319+
Array.from(draft.querySelectorAll("button")).find(
320+
(button) => button.textContent?.trim() === "Add Entry",
321+
),
322+
"commit valid entry",
323+
).click();
324+
expect(state.configForm).toEqual({
325+
values: { primary: "invalid existing value", backup: "123" },
326+
});
327+
} finally {
328+
container.remove();
329+
}
330+
},
331+
);
332+
333+
it.each(["map", "array"])(
334+
"keeps supported %s children editable beside Raw-only fields",
335+
(kind) => {
336+
const rowSchema = {
337+
type: "object",
338+
properties: {
339+
name: { type: "string" },
340+
retained: { anyOf: [{ type: "string" }, { const: false }] },
341+
},
342+
} satisfies JsonSchema;
343+
const collection = (name: string) =>
344+
kind === "map"
345+
? { "example/model.v1": { name, retained: false } }
346+
: [{ name, retained: false }];
347+
const analysis = analyzeConfigSchema({
348+
type: "object",
349+
properties: {
350+
values:
351+
kind === "map"
352+
? { type: "object", additionalProperties: rowSchema }
353+
: { type: "array", items: rowSchema },
354+
},
355+
});
356+
expect(analysis.unsupportedPaths).toEqual(["values.*.retained"]);
357+
const state = createInitialConfigState();
358+
state.configSchema = analysis.schema;
359+
state.configForm = { values: collection("before") };
360+
const container = document.createElement("div");
361+
render(
362+
renderConfigForm({
363+
schema: analysis.schema,
364+
uiHints: {},
365+
unsupportedPaths: analysis.unsupportedPaths,
366+
value: state.configForm,
367+
showAdvanced: true,
368+
onShowAdvanced: () => {},
369+
onPatch: (path, value) => updateConfigFormValue(state, path, value),
370+
}),
371+
container,
372+
);
373+
expect(container.textContent).toContain("Unsupported schema node. Use Raw mode.");
374+
expect(container.querySelector('[aria-label="Retained"]')).toBeNull();
375+
const name = expectElement(
376+
container.querySelector<HTMLInputElement>('[aria-label="Name"]'),
377+
"supported name field",
378+
);
379+
name.value = "after";
380+
name.dispatchEvent(new Event("input", { bubbles: true }));
381+
expect(JSON.parse(serializeFormForSubmit(state))).toEqual({ values: collection("after") });
382+
},
383+
);
384+
196385
it("retains unset map drafts until the collection source changes", async () => {
197386
const container = document.createElement("div");
198387
document.body.append(container);

‎ui/src/components/config-form.analyze.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import {
66
objectPropertySchema,
77
requiredPropertyKeys,
88
} from "./config-form.constraints.ts";
9-
import { pathKey, schemaType, type JsonSchema } from "./config-form.shared.ts";
9+
import {
10+
pathKey,
11+
schemaMayAcceptString,
12+
schemaType,
13+
type JsonSchema,
14+
} from "./config-form.shared.ts";
1015

1116
export type ConfigSchemaAnalysis = {
1217
schema: JsonSchema | null;
@@ -142,21 +147,26 @@ function shouldNormalizeAllOfBranch(schema: JsonSchema): boolean {
142147
}
143148

144149
function hasOnlySupportedConstraintKeywords(schema: JsonSchema): boolean {
145-
return Object.keys(schema).every((key) => SUPPORTED_CONSTRAINT_ONLY_KEYS.has(key));
150+
return hasOnlySupportedKeywords(schema, SUPPORTED_CONSTRAINT_ONLY_KEYS);
146151
}
147152

148153
function hasOnlySupportedFormKeywords(schema: JsonSchema): boolean {
154+
return hasOnlySupportedKeywords(schema, SUPPORTED_FORM_SCHEMA_KEYS);
155+
}
156+
157+
function hasOnlySupportedKeywords(schema: JsonSchema, supported: ReadonlySet<string>): boolean {
149158
return Object.keys(schema).every(
150159
(key) =>
151-
SUPPORTED_FORM_SCHEMA_KEYS.has(key) ||
152-
// JSON object keys are already strings; constrained names must remain fail-closed.
160+
supported.has(key) ||
161+
// Key edits use the same value validator as fields. Admit its supported
162+
// string constraints without hiding the whole map behind Raw mode.
153163
(key === "propertyNames" &&
154164
typeof schema.propertyNames === "object" &&
155165
schema.propertyNames !== null &&
156166
!Array.isArray(schema.propertyNames) &&
157-
Object.keys(schema.propertyNames).length === 1 &&
158-
Object.hasOwn(schema.propertyNames, "type") &&
159-
schema.propertyNames.type === "string"),
167+
schemaMayAcceptString(schema.propertyNames) &&
168+
normalizeSchemaNode({ type: "string", ...schema.propertyNames }, []).unsupportedPaths
169+
.length === 0),
160170
);
161171
}
162172

@@ -376,8 +386,8 @@ function normalizeSchemaNode(
376386
compositionBranch,
377387
);
378388
normalized.additionalProperties = res.schema ?? schema.additionalProperties;
379-
if (res.unsupportedPaths.length > 0) {
380-
unsupported.add(pathLabel);
389+
for (const unsupportedPath of res.unsupportedPaths) {
390+
unsupported.add(unsupportedPath);
381391
}
382392
}
383393
}
@@ -435,8 +445,8 @@ function normalizeSchemaNode(
435445
} else {
436446
const res = normalizeSchemaNode(schema.items, [...path, "*"], compositionBranch);
437447
normalized.items = res.schema ?? schema.items;
438-
if (res.unsupportedPaths.length > 0) {
439-
unsupported.add(pathLabel);
448+
for (const unsupportedPath of res.unsupportedPaths) {
449+
unsupported.add(unsupportedPath);
440450
}
441451
}
442452
}

‎ui/src/components/config-form.constraints.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
33
import {
44
arrayInputConstraints,
55
canApplyArrayCandidate,
6+
canApplyObjectCandidate,
67
configValuesEqual,
78
defaultValue,
89
isSupportedConfigValueValid,
@@ -17,6 +18,20 @@ import { coerceConfigFormNumberString, formatConfigFormNumber } from "./config-f
1718
import type { JsonSchema } from "./config-form.shared.ts";
1819

1920
describe("config form schema constraints", () => {
21+
it("rejects newly invalid keys without blocking repairs to existing invalid entries", () => {
22+
const schema = {
23+
type: "object",
24+
propertyNames: { type: "string", pattern: "^[a-z]+$" },
25+
additionalProperties: { type: "integer", minimum: 0 },
26+
};
27+
const current = { primary: -1 };
28+
expect(canApplyObjectCandidate(schema, current, { "bad/key": -1 })).toBe(false);
29+
expect(canApplyObjectCandidate(schema, current, { ...current, "bad/key": 1 })).toBe(false);
30+
expect(canApplyObjectCandidate(schema, current, { ...current, backup: 1 })).toBe(true);
31+
expect(canApplyObjectCandidate(schema, current, { primary: 1 })).toBe(true);
32+
expect(canApplyObjectCandidate(schema, { "old/key": -1 }, { "old/key": 1 })).toBe(true);
33+
});
34+
2035
it("coerces only decimal and scientific config number spellings", () => {
2136
expect(coerceConfigFormNumberString("42.5", false)).toBe(42.5);
2237
expect(coerceConfigFormNumberString(".5e2", false)).toBe(50);

0 commit comments

Comments
 (0)