Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion bin/provider-catalog-sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,45 @@ export function stripJsonc(text) {
out.push(c);
i += 1;
}
return out.join('').replace(/,(\s*[}\]])/g, '$1');
const stripped = out.join('');
// String-aware trailing-comma removal: a ',' followed by only whitespace
// then '}' or ']' is structural, but the same sequence inside a string
// literal is content and must survive verbatim (#3430).
const res = [];
let j = 0;
let inStr = false;
let e = false;
while (j < stripped.length) {
const ch = stripped[j];
if (inStr) {
res.push(ch);
if (e) e = false;
else if (ch === '\\') e = true;
else if (ch === '"') inStr = false;
j += 1;
continue;
}
if (ch === '"') {
inStr = true;
res.push(ch);
j += 1;
continue;
}
if (ch === ',') {
let k = j + 1;
while (k < stripped.length && /\s/.test(stripped[k])) k += 1;
if (stripped[k] === '}' || stripped[k] === ']') {
j += 1;
continue;
}
res.push(ch);
j += 1;
continue;
}
res.push(ch);
j += 1;
}
return res.join('');
}

export function parseJsonc(text) {
Expand Down
20 changes: 20 additions & 0 deletions bin/provider-catalog-sync.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ test('parseJsonc strips comments and trailing commas', () => {
assert.equal(cfg.provider['proxy-a'].models['gpt-1-sol'].limit.context, 1000);
});

test('parseJsonc preserves commas before brackets inside strings (#3430)', () => {
assert.equal(parseJsonc('{ "note": "x,}", "n": 1 }').note, 'x,}');
assert.equal(parseJsonc('{ "note": "a,]", "n": 1 }').note, 'a,]');
assert.equal(parseJsonc('{ "note": "a, }", "n": 1 }').note, 'a, }');
assert.equal(parseJsonc('{ "list": ["x, ]", "y"] }').list[0], 'x, ]');
const nested = parseJsonc(
'{ "provider": { "p": { "models": { "m": { "variants": { "v": { "headers": "a,}" } } } } } } }',
);
assert.equal(nested.provider.p.models.m.variants.v.headers, 'a,}');
});

test('parseJsonc still removes real trailing commas and strips comments (#3430)', () => {
assert.deepEqual(parseJsonc('{ "a": 1, }'), { a: 1 });
assert.deepEqual(parseJsonc('// hi\n{ "a": [1, 2,], "b": { "c": 3, } }'), {
a: [1, 2],
b: { c: 3 },
});
assert.equal(parseJsonc('{ "url": "https://example.com//x" }').url, 'https://example.com//x');
});

test('findModelsSpan locates the models object per provider', () => {
const span = findModelsSpan(FIXTURE, 'name-only-b');
assert.ok(span);
Expand Down