MCPcopy Create free account
hub / github.com/diillson/chatcli / tryCompactJSON

Function tryCompactJSON

cli/agent_tool_sanitizer.go:80–101  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
80func 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

Callers 1

Calls 3

hasAnyNewlineFunction · 0.85
CompactMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected