buildWriteDiff returns a unified-diff string describing what a write_file tool call would change on disk, suitable for display in the approval modal. Returns "" when the input is malformed (caller falls back to the plain "approve?" prompt). When the target file doesn't exist yet, the result still u
(rawInput string)
| 18 | // in chroma colors the body green, matching the natural "this is brand |
| 19 | // new" semantic. |
| 20 | func buildWriteDiff(rawInput string) string { |
| 21 | var in struct { |
| 22 | Path string `json:"path"` |
| 23 | Content string `json:"content"` |
| 24 | } |
| 25 | if err := json.Unmarshal([]byte(rawInput), &in); err != nil { |
| 26 | return "" |
| 27 | } |
| 28 | |
| 29 | existing, err := os.ReadFile(in.Path) |
| 30 | if err != nil { |
| 31 | // New file. Format as a unified diff against /dev/null so the |
| 32 | // "+" lines drive consistent highlighting downstream. |
| 33 | return synthesizeNewFileDiff(in.Path, in.Content) |
| 34 | } |
| 35 | |
| 36 | diff := difflib.UnifiedDiff{ |
| 37 | A: difflib.SplitLines(string(existing)), |
| 38 | B: difflib.SplitLines(in.Content), |
| 39 | FromFile: in.Path + " (current)", |
| 40 | ToFile: in.Path + " (proposed)", |
| 41 | Context: 3, |
| 42 | } |
| 43 | text, err := difflib.GetUnifiedDiffString(diff) |
| 44 | if err != nil { |
| 45 | return "" |
| 46 | } |
| 47 | if text == "" { |
| 48 | // File exists with identical content — nothing to approve. Still |
| 49 | // return a non-empty marker so the modal opens with a clear |
| 50 | // message instead of falling back to the plain prompt. |
| 51 | return "(no changes: proposed content is identical to current file)\n" |
| 52 | } |
| 53 | return text |
| 54 | } |
| 55 | |
| 56 | func synthesizeNewFileDiff(path, content string) string { |
| 57 | lines := difflib.SplitLines(content) |