Strip markdown code block wrappers from a JSON string. Claude sometimes wraps JSON in ```json ... ``` blocks despite being asked not to. This strips those wrappers.
(s: &str)
| 316 | /// Claude sometimes wraps JSON in ```json ... ``` blocks despite being |
| 317 | /// asked not to. This strips those wrappers. |
| 318 | pub(crate) fn strip_markdown_code_blocks(s: &str) -> String { |
| 319 | let trimmed = s.trim(); |
| 320 | |
| 321 | // Check for ```json ... ``` blocks |
| 322 | if let Some(rest) = trimmed.strip_prefix("```json") { |
| 323 | if let Some(idx) = rest.rfind("```") { |
| 324 | return rest[..idx].trim().to_string(); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // Check for ``` ... ``` blocks |
| 329 | if let Some(rest) = trimmed.strip_prefix("```") { |
| 330 | if let Some(idx) = rest.rfind("```") { |
| 331 | return rest[..idx].trim().to_string(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | trimmed.to_string() |
| 336 | } |
| 337 | |
| 338 | /// Truncate a string for display in log messages and UI. |
| 339 | /// |
no outgoing calls