mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
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:
@@ -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 = `[
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user