(response: string)
| 18 | * // Output: "{\"key\": \"value\"}" |
| 19 | */ |
| 20 | export function extractJSON(response: string): string { |
| 21 | // Try to match ```json ... ``` format first |
| 22 | const jsonBlockMatch = response.match(/```json\s*\n([\s\S]*?)\n```/); |
| 23 | if (jsonBlockMatch && jsonBlockMatch[1]) { |
| 24 | return jsonBlockMatch[1].trim(); |
| 25 | } |
| 26 | |
| 27 | // Try to match generic ``` ... ``` format (with newlines around content) |
| 28 | const codeBlockMatch = response.match(/```\s*\n([\s\S]*?)\n```/); |
| 29 | if (codeBlockMatch && codeBlockMatch[1]) { |
| 30 | return codeBlockMatch[1].trim(); |
| 31 | } |
| 32 | |
| 33 | // Fallback: no proper code block; use full content but strip loose markdown fences. |
| 34 | // Some models return raw JSON with trailing "```" (e.g. ...]```) or leading ```\n. |
| 35 | let cleaned = response.trim(); |
| 36 | cleaned = cleaned.replace(/^\s*```(?:json)?\s*\n?/g, '').replace(/\s*```+\s*$/g, ''); |
| 37 | return cleaned; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Extract code content from markdown code blocks |
no outgoing calls
no test coverage detected