From bb25a3944c0a8747c99d4360d71eb659554d65a1 Mon Sep 17 00:00:00 2001 From: Julien Chaumond Date: Mon, 4 May 2026 22:44:35 +0200 Subject: [PATCH] feat(coding-agent): allow comments and trailing commas in models.json (#4162) * feat(coding-agent): allow comments and trailing commas in models.json Run user-supplied models.json through a small `stripJsonComments` helper before JSON.parse so users can annotate their config and leave trailing commas without breaking the loader. Co-Authored-By: julien-agent * fix(coding-agent): strip comments before trailing commas in models.json The single-pass regex couldn't see a trailing comma when a `//` comment sat between the comma and its closer. Split into two passes: strip comments first, then strip trailing commas on the cleaned input. Co-Authored-By: julien-agent --------- Co-authored-by: julien-agent --- packages/coding-agent/src/core/model-registry.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/coding-agent/src/core/model-registry.ts b/packages/coding-agent/src/core/model-registry.ts index 798b8f8c..f2584aa9 100644 --- a/packages/coding-agent/src/core/model-registry.ts +++ b/packages/coding-agent/src/core/model-registry.ts @@ -213,6 +213,13 @@ function formatValidationPath(error: TLocalizedValidationError): string { return path || "root"; } +/** Strip `//` line comments and trailing commas from JSON, leaving string literals untouched. */ +function stripJsonComments(input: string): string { + return input + .replace(/"(?:\\.|[^"\\])*"|\/\/[^\n]*/g, (m) => (m[0] === '"' ? m : "")) + .replace(/"(?:\\.|[^"\\])*"|,(\s*[}\]])/g, (m, tail) => tail ?? (m[0] === '"' ? m : "")); +} + /** Provider override config (baseUrl, compat) without request auth/headers */ interface ProviderOverride { baseUrl?: string; @@ -450,7 +457,7 @@ export class ModelRegistry { try { const content = readFileSync(modelsJsonPath, "utf-8"); - const parsed = JSON.parse(content) as unknown; + const parsed = JSON.parse(stripJsonComments(content)) as unknown; if (!validateModelsConfig.Check(parsed)) { const errors =