perf(coding-agent): cache ModelsConfig validator (#1772)
This commit is contained in:
@@ -28,6 +28,7 @@ import type { AuthStorage } from "./auth-storage.js";
|
|||||||
import { clearConfigValueCache, resolveConfigValue, resolveHeaders } from "./resolve-config-value.js";
|
import { clearConfigValueCache, resolveConfigValue, resolveHeaders } from "./resolve-config-value.js";
|
||||||
|
|
||||||
const Ajv = (AjvModule as any).default || AjvModule;
|
const Ajv = (AjvModule as any).default || AjvModule;
|
||||||
|
const ajv = new Ajv();
|
||||||
|
|
||||||
// Schema for OpenRouter routing preferences
|
// Schema for OpenRouter routing preferences
|
||||||
const OpenRouterRoutingSchema = Type.Object({
|
const OpenRouterRoutingSchema = Type.Object({
|
||||||
@@ -120,6 +121,8 @@ const ModelsConfigSchema = Type.Object({
|
|||||||
providers: Type.Record(Type.String(), ProviderConfigSchema),
|
providers: Type.Record(Type.String(), ProviderConfigSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ajv.addSchema(ModelsConfigSchema, "ModelsConfig");
|
||||||
|
|
||||||
type ModelsConfig = Static<typeof ModelsConfigSchema>;
|
type ModelsConfig = Static<typeof ModelsConfigSchema>;
|
||||||
|
|
||||||
/** Provider override config (baseUrl, headers, apiKey) without custom models */
|
/** Provider override config (baseUrl, headers, apiKey) without custom models */
|
||||||
@@ -350,8 +353,7 @@ export class ModelRegistry {
|
|||||||
const config: ModelsConfig = JSON.parse(content);
|
const config: ModelsConfig = JSON.parse(content);
|
||||||
|
|
||||||
// Validate schema
|
// Validate schema
|
||||||
const ajv = new Ajv();
|
const validate = ajv.getSchema("ModelsConfig")!;
|
||||||
const validate = ajv.compile(ModelsConfigSchema);
|
|
||||||
if (!validate(config)) {
|
if (!validate(config)) {
|
||||||
const errors =
|
const errors =
|
||||||
validate.errors?.map((e: any) => ` - ${e.instancePath || "root"}: ${e.message}`).join("\n") ||
|
validate.errors?.map((e: any) => ` - ${e.instancePath || "root"}: ${e.message}`).join("\n") ||
|
||||||
|
|||||||
Reference in New Issue
Block a user