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 <Agents+cyolo@huggingface.co>

* 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 <Agents+cyolo@huggingface.co>

---------

Co-authored-by: julien-agent <Agents+cyolo@huggingface.co>
This commit is contained in:
Julien Chaumond
2026-05-04 22:44:35 +02:00
committed by GitHub
parent bac2df36b9
commit bb25a3944c

View File

@@ -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 =