* Processes and validates JSON output for headless mode * @param response - The raw response from the LLM * @returns Valid JSON string
(response: string)
| 42 | * @returns Valid JSON string |
| 43 | */ |
| 44 | function processJsonOutput(response: string): string { |
| 45 | const trimmedResponse = response.trim(); |
| 46 | |
| 47 | try { |
| 48 | // Try to parse the response as JSON to validate it |
| 49 | JSON.parse(trimmedResponse); |
| 50 | // If it parses successfully, return as-is |
| 51 | return trimmedResponse; |
| 52 | } catch { |
| 53 | // If it's not valid JSON, wrap it in a JSON object |
| 54 | return JSON.stringify({ |
| 55 | response: trimmedResponse, |
| 56 | status: "success", |
| 57 | note: "Response was not valid JSON, so it was wrapped in a JSON object", |
| 58 | }); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Strips <think></think> tags and excess whitespace from response |
no outgoing calls
no test coverage detected