CleanOutput extracts the result from Cursor's json or stream-json output. For stream-json, finds the last type "result", subtype "success" and returns its result field. For single-line json, parses and returns result.
(output string)
| 70 | // For stream-json, finds the last type "result", subtype "success" and returns its result field. |
| 71 | // For single-line json, parses and returns result. |
| 72 | func (p *CursorProvider) CleanOutput(output string) string { |
| 73 | output = strings.TrimSpace(output) |
| 74 | if output == "" { |
| 75 | return output |
| 76 | } |
| 77 | // Try single JSON object (json output format) |
| 78 | var single cursorResultLine |
| 79 | if json.Unmarshal([]byte(output), &single) == nil && single.Type == "result" && single.Subtype == "success" && single.Result != "" { |
| 80 | return single.Result |
| 81 | } |
| 82 | // NDJSON: find last result/success line |
| 83 | lines := strings.Split(output, "\n") |
| 84 | for i := len(lines) - 1; i >= 0; i-- { |
| 85 | line := strings.TrimSpace(lines[i]) |
| 86 | if line == "" { |
| 87 | continue |
| 88 | } |
| 89 | var ev cursorResultLine |
| 90 | if json.Unmarshal([]byte(line), &ev) == nil && ev.Type == "result" && ev.Subtype == "success" && ev.Result != "" { |
| 91 | return ev.Result |
| 92 | } |
| 93 | } |
| 94 | return output |
| 95 | } |
no outgoing calls