// Tolerate models that wrap JSON in prose or code fences. export function extractJson(text: string): string { const fenced = text.match(/```(?:json)?\s*([\s\S]*?)```/); if (fenced) return fenced[1]; const start = text.indexOf('{'); const end = text.lastIndexOf('}'); if (start !== -1 && end !== -1) return text.slice(start, end + 1); return text; }