export function extractFirstJsonObject(text: string): string | null { const start = text.indexOf('{') if (start === -1) { return null } let depth = 0 let inString = false let escaped = false for (let index = start; index < text.length; index += 1) { const char = text[index] if (inString) { if (escaped) { escaped = false } else if (char === '\\') { escaped = true } else if (char === '"') { inString = false } continue } if (char === '"') { inString = true continue } if (char === '{') { depth += 1 } else if (char === '}') { depth -= 1 if (depth === 0) { return text.slice(start, index + 1) } } } return null }