Add show_diff chat tool and diff card
The Pipelines MCP server gains show_diff: it runs Git in a repository inside the operator's home (default: the chat's directory) and records the per-file patches. Without base it shows uncommitted work against HEAD; with base it shows everything since the merge base (a pull request preview), optionally committed changes only. Bounded at 300 files, 256 KiB per patch and 1 MiB total, with partial diffs labelled; diff bodies share the per-chat retention budget with HTML views. The chat draws the item as a card listing each changed file with its stat; a row opens the existing file diff viewer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../changes/domain/change_capture.dart';
|
||||
import '../../changes/domain/unified_patch.dart';
|
||||
|
||||
/// A `show_diff` item's comparison, as the server records it
|
||||
/// (`internal/protocol/chat_shown.go` ChatShownDiffBody): a repository's work
|
||||
/// tree, or HEAD when committed only, against HEAD or the merge base with a
|
||||
/// base branch, with the same per-file changes a run records.
|
||||
class ChatShownDiff {
|
||||
const ChatShownDiff({
|
||||
required this.repo,
|
||||
required this.base,
|
||||
required this.baseCommit,
|
||||
required this.head,
|
||||
required this.committedOnly,
|
||||
required this.files,
|
||||
required this.additions,
|
||||
required this.deletions,
|
||||
required this.capture,
|
||||
});
|
||||
|
||||
/// The diff of a `diff` item, or null for any other item or a malformed one.
|
||||
static ChatShownDiff? of(JsonMap item) {
|
||||
final body = item['diff'];
|
||||
if (item['kind'] != 'diff' || body is! Map) return null;
|
||||
final changes = body['changes'];
|
||||
return ChatShownDiff(
|
||||
repo: _text(body['repo']),
|
||||
base: _text(body['base']),
|
||||
baseCommit: _text(body['base_commit']),
|
||||
head: _text(body['head']),
|
||||
committedOnly: body['committed_only'] == true,
|
||||
files: _count(body['files']),
|
||||
additions: _count(body['additions']),
|
||||
deletions: _count(body['deletions']),
|
||||
capture: item['expired'] == true || changes is! Map
|
||||
? null
|
||||
: ChangeCapture.fromJson(Map<String, dynamic>.from(changes)),
|
||||
);
|
||||
}
|
||||
|
||||
final String repo;
|
||||
final String base;
|
||||
final String baseCommit;
|
||||
final String head;
|
||||
final bool committedOnly;
|
||||
final int files;
|
||||
final int additions;
|
||||
final int deletions;
|
||||
|
||||
/// The per-file changes, or null once the server no longer retains them.
|
||||
final ChangeCapture? capture;
|
||||
|
||||
/// The repository's folder name.
|
||||
String get repoName {
|
||||
final parts = repo.split('/').where((part) => part.isNotEmpty);
|
||||
return parts.isEmpty ? repo : parts.last;
|
||||
}
|
||||
|
||||
/// What was compared, in words: the branch against its base and whether
|
||||
/// uncommitted work is included.
|
||||
String get comparison {
|
||||
final head = this.head.isEmpty ? 'HEAD' : this.head;
|
||||
final against = base.isEmpty
|
||||
? (baseCommit.isEmpty ? 'no commits yet' : 'uncommitted changes')
|
||||
: '$head vs $base${committedOnly ? ' · committed only' : ' · with uncommitted work'}';
|
||||
final where = repoName.isEmpty ? '' : '$repoName · ';
|
||||
return base.isEmpty ? '$where$head · $against' : '$where$against';
|
||||
}
|
||||
|
||||
/// "3 files changed +12 -4", or "No changes".
|
||||
String get heading {
|
||||
if (files == 0) return 'No changes';
|
||||
final stat = changeStat(additions, deletions);
|
||||
return '${files == 1 ? '1 file' : '$files files'} changed'
|
||||
'${stat.isEmpty ? '' : ' $stat'}';
|
||||
}
|
||||
|
||||
/// True when files or patches were left out by the size limits.
|
||||
bool get partial {
|
||||
final capture = this.capture;
|
||||
return capture != null && (!capture.complete || capture.omitted > 0);
|
||||
}
|
||||
}
|
||||
|
||||
String _text(Object? value) => '${value ?? ''}'.trim();
|
||||
|
||||
int _count(Object? value) => value is num && value > 0 ? value.toInt() : 0;
|
||||
@@ -2,15 +2,15 @@ import '../../../core/api_gateway.dart';
|
||||
import 'chat_transcript.dart';
|
||||
|
||||
/// Entries a chat's model showed the operator through the Pipelines tools
|
||||
/// (`show_file`, `show_image`, `show_html`, `notify`).
|
||||
/// (`show_file`, `show_image`, `show_html`, `show_diff`, `notify`).
|
||||
///
|
||||
/// The server keeps them per chat, outside the harness transcript: the
|
||||
/// `chat_subscribe` reply carries the retained list (oldest first) and a
|
||||
/// pushed `chat_shown` frame carries each new or updated item. Items are keyed
|
||||
/// by `id`; a later copy replaces the held one in place.
|
||||
|
||||
/// The four recorded kinds.
|
||||
const chatShownKinds = {'file', 'image', 'html', 'notify'};
|
||||
/// The recorded kinds.
|
||||
const chatShownKinds = {'file', 'image', 'html', 'diff', 'notify'};
|
||||
|
||||
/// The transcript row kind an item is drawn as.
|
||||
const String chatShownRowKind = 'shown';
|
||||
@@ -52,7 +52,7 @@ List<JsonMap> mergeChatShownItems(
|
||||
/// `pipelines/show_file`.
|
||||
final RegExp _pipelinesTool = RegExp(
|
||||
r'^(?:mcp__pipelines__|pipelines_|pipelines/)'
|
||||
r'(?:show_file|show_image|show_html|notify)$',
|
||||
r'(?:show_file|show_image|show_html|show_diff|notify)$',
|
||||
);
|
||||
|
||||
/// Whether [part] is a call of a Pipelines tool whose result the transcript
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter/widgets.dart';
|
||||
import '../../../../../core/api_gateway.dart';
|
||||
import '../../../../artifacts/ui/artifact_html_view.dart';
|
||||
import '../../../files/files_viewer.dart';
|
||||
import 'chat_shown_diff_card.dart';
|
||||
import 'chat_shown_file_card.dart';
|
||||
import 'chat_shown_html_card.dart';
|
||||
import 'chat_shown_link_image.dart';
|
||||
@@ -35,6 +36,7 @@ class ChatShownCard extends StatelessWidget {
|
||||
return switch ('${item['kind']}') {
|
||||
'notify' => ChatShownNotifyRow(item: item),
|
||||
'html' => ChatShownHtmlCard(item: item, htmlBuilder: htmlBuilder),
|
||||
'diff' => ChatShownDiffCard(item: item),
|
||||
'image' when path.isEmpty => ChatShownLinkImage(
|
||||
item: item,
|
||||
openExternal: viewer.openExternal,
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../../core/api_gateway.dart';
|
||||
import '../../../../changes/changes.dart';
|
||||
import '../../../domain/chat_shown_diff.dart';
|
||||
import 'chat_shown_surface.dart';
|
||||
|
||||
/// How many changed files the card lists before "Show all".
|
||||
const chatShownDiffPreviewFiles = 8;
|
||||
|
||||
/// A Git comparison the model showed through `show_diff`: what was compared,
|
||||
/// the totals, an honest note when the size limits left something out, and
|
||||
/// one row per changed file that opens the app's file diff viewer, stepping
|
||||
/// through every file of the diff. A diff the server no longer retains says so.
|
||||
class ChatShownDiffCard extends StatefulWidget {
|
||||
const ChatShownDiffCard({super.key, required this.item});
|
||||
|
||||
final JsonMap item;
|
||||
|
||||
@override
|
||||
State<ChatShownDiffCard> createState() => _ChatShownDiffCardState();
|
||||
}
|
||||
|
||||
class _ChatShownDiffCardState extends State<ChatShownDiffCard> {
|
||||
bool _all = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final diff = ChatShownDiff.of(widget.item);
|
||||
if (diff == null) return const SizedBox.shrink();
|
||||
final title = chatShownTitle(widget.item, 'Diff');
|
||||
final secondary = chatShownSecondary(context);
|
||||
final noteStyle = Theme.of(
|
||||
context,
|
||||
).textTheme.bodySmall?.copyWith(color: secondary);
|
||||
final capture = diff.capture;
|
||||
final files = capture?.files ?? const <FileChange>[];
|
||||
final shown = _all || files.length <= chatShownDiffPreviewFiles
|
||||
? files
|
||||
: files.take(chatShownDiffPreviewFiles).toList();
|
||||
return ChatShownSurface(
|
||||
label: 'Shown diff $title',
|
||||
maxWidth: 720,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(12, 8, 12, 4),
|
||||
child: ChatShownHeading(
|
||||
title: title,
|
||||
subtitle: diff.comparison,
|
||||
leading: Icon(Icons.difference_outlined, color: secondary),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 8),
|
||||
child: ChangeStatText(
|
||||
diff.heading,
|
||||
key: const Key('chat-shown-diff-heading'),
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
if (capture == null)
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 12),
|
||||
child: Text('This diff is no longer retained', style: noteStyle),
|
||||
)
|
||||
else ...[
|
||||
if (diff.partial)
|
||||
Padding(
|
||||
key: const Key('chat-shown-diff-partial'),
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 8),
|
||||
child: Text(
|
||||
capture.diagnostic.isEmpty
|
||||
? 'Part of this diff was left out by the size limit.'
|
||||
: 'Partial diff: ${capture.diagnostic}',
|
||||
style: noteStyle,
|
||||
),
|
||||
),
|
||||
for (final file in shown)
|
||||
ChangeFileRow(
|
||||
key: ValueKey<String>('chat-shown-diff-file-${file.path}'),
|
||||
file: file,
|
||||
onOpen: (file) =>
|
||||
showFileDiffViewer(context, file, files: files),
|
||||
),
|
||||
if (shown.length < files.length)
|
||||
Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(4, 0, 4, 4),
|
||||
child: TextButton(
|
||||
key: const Key('chat-shown-diff-all'),
|
||||
onPressed: () => setState(() => _all = true),
|
||||
child: Text('Show all ${files.length} files'),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (capture.omitted > 0)
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 12),
|
||||
child: Text(
|
||||
'${capture.omitted} more '
|
||||
'${capture.omitted == 1 ? 'file is' : 'files are'} '
|
||||
'not listed.',
|
||||
style: noteStyle,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user