feat: show edit and write tools as line diffs

Expanding an edit, write or patch call now renders the change as tinted
diff lines instead of an old/new text blob: added lines green, removed
lines red, @@ hunk headers marked, and file header noise hidden. The
patch comes from the server's metadata.files[].patch; older servers fall
back to a line diff of the edit's old/new strings, and a write with only
content shows as added lines.

The tool row also shows +N −M for these calls, as dsh-Go does, and a
phone-sized preview stops at 600 lines.

0.4.12 (412).
This commit is contained in:
blumia committed 2026-11-23 22:36:05 +08:00
1 parent 0f850b6191
commit d533bc95ef
7 files changed
+541 -6

No files matched your search

+51 -2
View File
@@ -12,6 +12,7 @@ import '../state.dart';
import '../widgets/composer_attachment.dart';
import '../widgets/markdown_view.dart';
import '../widgets/composer_box.dart';
import '../widgets/diff_view.dart';
import '../widgets/pet_sprite.dart';
import '../widgets/model_controls.dart';
import '../widgets/slash_palette.dart';
@@ -854,6 +855,13 @@ class _ChatScreenState extends State<ChatScreen> {
? const <_ImageRef>[]
: toolImages(p, chat.dir);
final subagentId = _subagentSessionId(p);
final diffFiles = reasoning ? const <DiffFile>[] : toolDiffFiles(p);
var diffAdded = 0;
var diffRemoved = 0;
for (final file in diffFiles) {
diffAdded += file.added;
diffRemoved += file.removed;
}
return Container(
margin: const EdgeInsets.only(left: 22),
decoration: BoxDecoration(
@@ -921,6 +929,14 @@ class _ChatScreenState extends State<ChatScreen> {
: theme.hintColor)),
),
],
if (diffFiles.isNotEmpty &&
!busy &&
status != 'error' &&
diffAdded + diffRemoved > 0)
Padding(
padding: const EdgeInsets.only(left: 6, right: 2),
child: DiffStats(added: diffAdded, removed: diffRemoved),
),
if (subagentId != null)
Icon(Icons.chevron_right,
size: 18, color: theme.hintColor)
@@ -958,7 +974,7 @@ class _ChatScreenState extends State<ChatScreen> {
_Reveal(
open: open,
onGrow: _grow,
child: _stepDetails(p, reasoning),
child: _stepDetails(p, reasoning, diffFiles),
),
],
),
@@ -1057,8 +1073,10 @@ class _ChatScreenState extends State<ChatScreen> {
/// What the step actually did: the command or path, the rest of the input,
/// and the result — never the request JSON as a blob.
Widget _stepDetails(ContentPart p, bool reasoning) {
Widget _stepDetails(
ContentPart p, bool reasoning, List<DiffFile> diffFiles) {
final theme = Theme.of(context);
if (diffFiles.isNotEmpty) return _diffDetails(p, diffFiles);
if (reasoning) {
if (p.text.isEmpty) return const SizedBox.shrink();
// The model writes Markdown here too; render it as such, a touch
@@ -1125,6 +1143,37 @@ class _ChatScreenState extends State<ChatScreen> {
);
}
/// An edit/write/patch opened: the before/after as tinted diff lines, with
/// the tool's own result in one quiet line under it.
Widget _diffDetails(ContentPart p, List<DiffFile> diffFiles) {
final theme = Theme.of(context);
final output = toolOutput(p).trim();
return Padding(
padding: const EdgeInsets.fromLTRB(10, 2, 10, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
DiffView(files: diffFiles),
if (output.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(
output,
maxLines: 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 11.5,
color: p.state?.error != null
? theme.colorScheme.error
: theme.hintColor,
),
),
),
],
),
);
}
static IconData _stepIcon(ContentPart p) {
if (p.type == 'reasoning') return Icons.psychology_outlined;
switch (p.name ?? '') {