From a5a2e9182c0a2822c55cddab727ce0f6b25370d1 Mon Sep 17 00:00:00 2001 From: Victor Noguera Date: Tue, 7 Apr 2026 23:50:23 -0400 Subject: [PATCH] feat: enhance cleanLlmJson function to improve JSON extraction and formatting --- src/llm.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/llm.ts b/src/llm.ts index 66d0ca0..9c67720 100644 --- a/src/llm.ts +++ b/src/llm.ts @@ -177,9 +177,24 @@ function cleanLlmJson(text: string): string { const fenceMatch = text.match(/```(?:json)?\s*\n?([\s\S]*?)```/); let cleaned = fenceMatch ? fenceMatch[1]!.trim() : text.trim(); + // Strip any non-JSON wrapper text by taking the largest JSON-looking segment + const firstArray = cleaned.indexOf("["); + const firstObject = cleaned.indexOf("{"); + const startCandidates = [firstArray, firstObject].filter((i) => i >= 0); + const start = startCandidates.length > 0 ? Math.min(...startCandidates) : -1; + const endArray = cleaned.lastIndexOf("]"); + const endObject = cleaned.lastIndexOf("}"); + const end = Math.max(endArray, endObject); + if (start >= 0 && end > start) { + cleaned = cleaned.slice(start, end + 1); + } + // Fix trailing comma-quote before closing brace: ,"} → "} cleaned = cleaned.replace(/,"\s*}/g, '"}'); + // Fix malformed comma-quote before a closing bracket/brace: ,"} or ,"] + cleaned = cleaned.replace(/,\s*"\s*([}\]])/g, "$1"); + // Fix trailing commas before ] or } cleaned = cleaned.replace(/,\s*([}\]])/g, "$1");