Skip to content

Commit 1ab7b75

Browse files
fix(config): accept declared model compatibility settings (#118674)
Align the strict root model compatibility schema with the declared config type so routing, streaming, session, and cache settings validate and reach provider request builders. Add owner-boundary coverage for value preservation and nested Control UI schema unions. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent e7f6aaa commit 1ab7b75

3 files changed

Lines changed: 134 additions & 15 deletions

File tree

‎src/config/schema-control-ui-contract.test.ts‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,43 @@ function collectMixedUnions(
6868
return result;
6969
}
7070

71+
function schemaChildAtSegment(schema: JsonSchema, segment: string): JsonSchema | undefined {
72+
let direct: JsonSchema | undefined;
73+
if (segment === "*") {
74+
direct = Array.isArray(schema.items)
75+
? undefined
76+
: schema.items ||
77+
(schema.additionalProperties && typeof schema.additionalProperties === "object"
78+
? schema.additionalProperties
79+
: undefined);
80+
} else if (/^\d+$/u.test(segment) && Array.isArray(schema.items)) {
81+
direct = schema.items[Number(segment)];
82+
} else {
83+
direct = schema.properties?.[segment];
84+
}
85+
if (direct) {
86+
return direct;
87+
}
88+
for (const branch of [
89+
...(schema.allOf ?? []),
90+
...(schema.anyOf ?? []),
91+
...(schema.oneOf ?? []),
92+
]) {
93+
const nested = schemaChildAtSegment(branch, segment);
94+
if (nested) {
95+
return nested;
96+
}
97+
}
98+
return undefined;
99+
}
100+
71101
function schemaAtPath(schema: JsonSchema, path: string): JsonSchema | undefined {
72102
let current: JsonSchema | undefined = schema;
73103
for (const segment of path === "<root>" ? [] : path.split(".")) {
74104
if (!current) {
75105
return undefined;
76106
}
77-
if (segment === "*") {
78-
current = Array.isArray(current.items)
79-
? undefined
80-
: current.items ||
81-
(current.additionalProperties && typeof current.additionalProperties === "object"
82-
? current.additionalProperties
83-
: undefined);
84-
continue;
85-
}
86-
if (/^\d+$/u.test(segment) && Array.isArray(current.items)) {
87-
current = current.items[Number(segment)];
88-
continue;
89-
}
90-
current = current.properties?.[segment];
107+
current = schemaChildAtSegment(current, segment);
91108
}
92109
return current;
93110
}

‎src/config/zod-schema.core.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33
import { normalizeStringEntries } from "@openclaw/normalization-core/string-normalization";
44
import { z } from "zod";
55
import { isSafeExecutableValue } from "../infra/exec-safety.js";
6+
import type { OpenRouterRouting, VercelGatewayRouting } from "../llm/types.js";
67
import { normalizeExactAllowedHost } from "../secrets/exact-hostname.js";
78
import {
89
formatExecSecretRefIdValidationMessage,
@@ -263,6 +264,59 @@ const ModelApiSchema = z.enum(MODEL_APIS, {
263264
: undefined,
264265
});
265266

267+
const RoutingPercentileCutoffsSchema = z
268+
.object({
269+
p50: z.number().optional(),
270+
p75: z.number().optional(),
271+
p90: z.number().optional(),
272+
p99: z.number().optional(),
273+
})
274+
.strict();
275+
276+
const OpenRouterRoutingSchema = z
277+
.object({
278+
allow_fallbacks: z.boolean().optional(),
279+
require_parameters: z.boolean().optional(),
280+
data_collection: z.enum(["deny", "allow"]).optional(),
281+
zdr: z.boolean().optional(),
282+
enforce_distillable_text: z.boolean().optional(),
283+
order: z.array(z.string()).optional(),
284+
only: z.array(z.string()).optional(),
285+
ignore: z.array(z.string()).optional(),
286+
quantizations: z.array(z.string()).optional(),
287+
sort: z
288+
.union([
289+
z.string(),
290+
z
291+
.object({
292+
by: z.string().optional(),
293+
partition: z.string().nullable().optional(),
294+
})
295+
.strict(),
296+
])
297+
.optional(),
298+
max_price: z
299+
.object({
300+
prompt: z.union([z.number(), z.string()]).optional(),
301+
completion: z.union([z.number(), z.string()]).optional(),
302+
image: z.union([z.number(), z.string()]).optional(),
303+
audio: z.union([z.number(), z.string()]).optional(),
304+
request: z.union([z.number(), z.string()]).optional(),
305+
})
306+
.strict()
307+
.optional(),
308+
preferred_min_throughput: z.union([z.number(), RoutingPercentileCutoffsSchema]).optional(),
309+
preferred_max_latency: z.union([z.number(), RoutingPercentileCutoffsSchema]).optional(),
310+
} satisfies Record<keyof OpenRouterRouting, z.ZodType>)
311+
.strict();
312+
313+
const VercelGatewayRoutingSchema = z
314+
.object({
315+
only: z.array(z.string()).optional(),
316+
order: z.array(z.string()).optional(),
317+
} satisfies Record<keyof VercelGatewayRouting, z.ZodType>)
318+
.strict();
319+
266320
const ModelCompatSchema = z
267321
.object({
268322
supportsStore: z.boolean().optional(),
@@ -293,7 +347,15 @@ const ModelCompatSchema = z
293347
unsupportedToolSchemaKeywords: z.array(z.string().min(1)).optional(),
294348
toolCallArgumentsEncoding: z.string().optional(),
295349
requiresOpenAiAnthropicToolPayload: z.boolean().optional(),
296-
})
350+
openRouterRouting: OpenRouterRoutingSchema.optional(),
351+
vercelGatewayRouting: VercelGatewayRoutingSchema.optional(),
352+
zaiToolStream: z.boolean().optional(),
353+
cacheControlFormat: z.literal("anthropic").optional(),
354+
sendSessionAffinityHeaders: z.boolean().optional(),
355+
sendSessionIdHeader: z.boolean().optional(),
356+
supportsEagerToolInputStreaming: z.boolean().optional(),
357+
supportsLongCacheRetention: z.boolean().optional(),
358+
} satisfies Record<keyof ModelCompatConfig, z.ZodType>)
297359
.strict()
298360
.optional();
299361
type AssertAssignable<_Left extends _Right, _Right> = true;

‎src/config/zod-schema.models.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,46 @@ describe("ModelsConfigSchema", () => {
114114
expect(result.success).toBe(true);
115115
});
116116

117+
it("accepts and preserves declared model compatibility settings", () => {
118+
const compat = {
119+
openRouterRouting: {
120+
allow_fallbacks: false,
121+
require_parameters: true,
122+
data_collection: "deny",
123+
zdr: true,
124+
enforce_distillable_text: true,
125+
order: ["anthropic", "openai"],
126+
only: ["anthropic"],
127+
ignore: ["openai"],
128+
quantizations: ["fp16"],
129+
sort: { by: "latency", partition: null },
130+
max_price: { prompt: "0.5", completion: 1, image: 2, audio: 3, request: 4 },
131+
preferred_min_throughput: { p50: 10, p75: 20, p90: 30, p99: 40 },
132+
preferred_max_latency: 5,
133+
},
134+
vercelGatewayRouting: {
135+
only: ["anthropic"],
136+
order: ["anthropic", "openai"],
137+
},
138+
zaiToolStream: true,
139+
cacheControlFormat: "anthropic",
140+
sendSessionAffinityHeaders: true,
141+
sendSessionIdHeader: true,
142+
supportsEagerToolInputStreaming: true,
143+
supportsLongCacheRetention: true,
144+
};
145+
const parsed = ModelsConfigSchema.parse({
146+
providers: {
147+
"my-proxy": {
148+
baseUrl: "https://my-proxy.example.com/v1",
149+
models: [{ id: "custom-model", name: "Custom Model", compat }],
150+
},
151+
},
152+
});
153+
154+
expect(parsed?.providers?.["my-proxy"]?.models?.[0]?.compat).toEqual(compat);
155+
});
156+
117157
it("accepts catalog-declared temperature compatibility", () => {
118158
const result = ModelsConfigSchema.safeParse({
119159
providers: {

0 commit comments

Comments
 (0)