tryCompactJSON attempts to compact a multiline string that contains valid JSON. This handles the common case where AI models send pretty-printed JSON args. Returns the compacted single-line JSON string, or empty string if not valid JSON.
(input string)
| 78 | // This handles the common case where AI models send pretty-printed JSON args. |
| 79 | // Returns the compacted single-line JSON string, or empty string if not valid JSON. |
| 80 | func tryCompactJSON(input string) string { |
| 81 | trimmed := strings.TrimSpace(input) |
| 82 | if len(trimmed) == 0 { |
| 83 | return "" |
| 84 | } |
| 85 | |
| 86 | // If it starts with { or [ it might be JSON - try to compact it |
| 87 | if trimmed[0] == '{' || trimmed[0] == '[' { |
| 88 | var buf bytes.Buffer |
| 89 | if err := json.Compact(&buf, []byte(trimmed)); err == nil { |
| 90 | return buf.String() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Not valid JSON - try collapsing newlines to spaces for CLI-style args |
| 95 | collapsed := strings.Join(strings.Fields(trimmed), " ") |
| 96 | if !hasAnyNewline(collapsed) { |
| 97 | return collapsed |
| 98 | } |
| 99 | |
| 100 | return "" |
| 101 | } |
| 102 | |
| 103 | // processLineContinuations processa "\" + whitespace + newline de forma robusta |
| 104 | // Respeita aspas e mantém o conteúdo que vem depois do newline |
no test coverage detected