Files
TREK/server/src/nest/llm-parse/lenient-json.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

87 lines
3.5 KiB
TypeScript

import JSON5 from 'json5';
/**
* Parse LLM output that is *meant* to be JSON but may not be strict JSON.
*
* Cloud providers reached through the OpenAI-compatible endpoint don't all honour
* `response_format` faithfully — Gemini in particular emits JavaScript-object-literal
* text: single-quoted strings, unquoted keys, and trailing commas (#1638), e.g.
*
* [ { '@type': 'LodgingReservation', checkinTime: '2026-08-28T00:00:00', price: 146.25, } ]
*
* Strict `JSON.parse` throws on all three, so the reservation list came back empty and
* the UI showed nothing. We try strict JSON first (the common, cheapest path) and fall
* back to JSON5, which accepts exactly that relaxed superset. Returns `null` on failure.
*
* The leading/trailing code-fence strip stays here because some models still wrap the
* payload in a ```json fence even when asked for raw JSON.
*/
export function parseLenientJson(content: string | undefined | null): unknown {
if (!content) return null;
const stripped = content
.trim()
.replace(/^```(?:json)?/i, '')
.replace(/```$/, '')
.trim();
try {
return JSON.parse(stripped);
} catch {
try {
return JSON5.parse(stripped);
} catch {
return null;
}
}
}
/**
* Whatever a provider handed back, as a list of reservation nodes.
*
* The extraction tool declares `reservations` as an array and the call forces
* that tool, so the answer should already be one. It is not always. Anthropic
* sometimes serialises its tool input as a JSON-encoded string instead (#1968),
* in either of two shapes seen in the wild:
*
* "[{\"@type\":\"LodgingReservation\", …}]" a stringified array
* "{\"reservations\":[{…}]}" stringified and re-wrapped
*
* Whether it happens is up to how the model serialises that particular call, so
* the same document imported fine one minute and came back empty the next —
* indistinguishable, to the person waiting, from a document with no booking in
* it. Nothing was logged, because nothing had failed: the value simply was not
* an array, and the check that only accepted arrays dropped a good extraction.
*
* One unwrap of a string, not a loop: a value that is still not a list after
* that is genuinely not one, and guessing further would start inventing
* bookings out of prose.
*
* A single reservation answered on its own, with no array and no wrapper around
* it, is the third shape seen in the wild:
*
* {"@type":"LodgingReservation","reservationNumber":"733", …}
*
* A string `@type` is the same gate `kiReservationSchema` applies, so a node that
* would have been accepted inside an array is accepted alone rather than thrown
* away without a word (#2375). It is still not guessing: an object without that
* key stays refused.
*
* `rootKey` names the wrapper for a caller that asks for another list than
* reservations (the receipt read asks for `receipts`).
*/
export function toReservationList(value: unknown, rootKey = 'reservations'): Record<string, unknown>[] {
const list = (v: unknown): Record<string, unknown>[] | null => {
if (Array.isArray(v)) return v as Record<string, unknown>[];
if (v && typeof v === 'object') {
const o = v as Record<string, unknown>;
if (Array.isArray(o[rootKey])) return o[rootKey] as Record<string, unknown>[];
if (typeof o['@type'] === 'string') return [v as Record<string, unknown>];
}
return null;
};
const direct = list(value);
if (direct) return direct;
if (typeof value === 'string') return list(parseLenientJson(value)) ?? [];
return [];
}