mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-10 05:01:28 -10:00
write_file/edit_file only reported "Created X (8462 bytes)" / "edited X", so the user never saw WHAT changed — no code preview, no red/green. The TUI already renders a colored diff card whenever a tool's output looks like a unified diff (diffFirstToolBodyRenderer + looksLikeDiff), so the fix is purely tool-side: emit a diff. - edit_file appends a unified diff (old vs new) after its summary line → red/green change preview. - write_file appends a unified diff of prior vs new content: a fresh create previews as all-green additions, an overwrite as red/green. It reads the prior bytes before writing only when the file existed. - boundedUnifiedDiff (via the already-vendored go-udiff, now a direct require) caps the emitted diff at 48KB so a large generated file can't flood the transcript or balloon persisted session events; past the cap only the summary shows. The summary stays the first output line so any consumer that reads it (swarm/Task result bubbling) is unaffected. Tests: edit emits @@/-/+ ; new-file emits additions-only; overwrite emits red/green. Full tools/tui/agent/swarm suites green.
21 lines
874 B
Go
21 lines
874 B
Go
package tools
|
|
|
|
import udiff "github.com/aymanbagabas/go-udiff"
|
|
|
|
// maxToolPreviewBytes caps the inline diff a write tool appends to its result, so
|
|
// a large generated file can't flood the transcript or balloon the persisted
|
|
// session events. Past this the tool falls back to its summary line alone.
|
|
const maxToolPreviewBytes = 48 * 1024
|
|
|
|
// boundedUnifiedDiff returns a unified diff of oldContent -> newContent labelled
|
|
// with path, suitable for the TUI's diff card renderer. A create (oldContent "")
|
|
// yields an all-additions (green) preview; an overwrite/edit yields red/green.
|
|
// Returns "" when there is no change or the diff exceeds maxToolPreviewBytes.
|
|
func boundedUnifiedDiff(path, oldContent, newContent string) string {
|
|
diff := udiff.Unified(path, path, oldContent, newContent)
|
|
if diff == "" || len(diff) > maxToolPreviewBytes {
|
|
return ""
|
|
}
|
|
return diff
|
|
}
|