fix(llm-parse): read fenced booking JSON surrounded by prose

Some booking models return a complete JSON code block followed by an explanation. Read the fenced payload when parsing the full answer fails, and leave the existing JSON and JSON5 behavior intact.

Closes #3307
This commit is contained in:
Maurice
2026-12-03 11:00:59 +01:00
parent ca201ab4d0
commit 75020dd2df
2 changed files with 56 additions and 6 deletions
@@ -13,6 +13,25 @@ describe('parseLenientJson', () => {
]);
});
it('reads fenced booking JSON followed by explanatory text', () => {
const booking = {
bookings: [
{ kind: 'new_booking', reservations: [{ '@type': 'LodgingReservation', reservationNumber: 'TREK42' }] },
],
};
const fence = '```';
const response = [fence + 'json', JSON.stringify(booking), fence, 'I found one booking.'].join('\n');
expect(parseLenientJson(response)).toEqual(booking);
expect(parseLenientJson('Extracted from the email:\r\n' + response.replaceAll('\n', '\r\n'))).toEqual(booking);
expect(parseLenientJson(response.replace(`${fence}\nI found`, `${fence} I found`))).toEqual(booking);
expect(
parseLenientJson(response.replace(`${fence}\nI found one booking.`, `${fence} I found \`TREK42\`.`)),
).toEqual(booking);
const unrelatedCode = [fence + 'text', JSON.stringify({ example: true }), fence].join('\n');
expect(parseLenientJson(`${unrelatedCode}\n\n${response}`)).toEqual(booking);
expect(parseLenientJson(`${unrelatedCode}\n\nI found no bookings.`)).toBeNull();
});
it('parses single-quoted, unquoted-key, trailing-comma output (Gemini, #1638)', () => {
const gemini = `[
{