Show a chat turn's changed files as a chip that opens its diff

The answer footer shows '3 files changed +9 -2'; tapping opens the turn's
files in the sheet (wide) or a full-screen page (narrow), and a file opens
the run Changes diff viewer. Patches load through the chat's own gateway.
This commit is contained in:
2026-11-09 16:08:35 +00:00
parent 0696ab849a
commit 3374bea2ef
15 changed files with 1155 additions and 36 deletions
@@ -0,0 +1,126 @@
import '../../../core/api_gateway.dart';
import '../../changes/domain/unified_patch.dart';
/// The server's per-turn workspace change summaries for one chat: what the
/// transcript's "N files changed" chip shows. The server sends them with the
/// history (`chat_turn_diffs` on a `chat_messages` reply) and pushes one per
/// finished turn that changed files; the full patches are fetched on demand.
class ChatTurnDiffs {
final Map<String, JsonMap> _byTurn = {};
/// Summaries in the server's order (oldest first).
List<JsonMap> get summaries => List.unmodifiable(_byTurn.values);
bool get isEmpty => _byTurn.isEmpty;
/// Folds a `chat_turn_diffs` body for [sessionId]; true when it changed
/// anything. A body for another session, or without turns, is ignored.
bool apply(Object? body, String sessionId) {
if (body is! Map || '${body['session_id'] ?? ''}' != sessionId) {
return false;
}
final turns = body['turn_diffs'];
if (turns is! List) return false;
var changed = false;
for (final value in turns) {
if (value is! Map) continue;
final summary = Map<String, dynamic>.from(value);
final id = '${summary['turn_id'] ?? ''}'.trim();
if (id.isEmpty || chatTurnDiffFiles(summary) <= 0) continue;
_byTurn[id] = summary;
changed = true;
}
return changed;
}
}
/// Attaches each summary to the assistant row it belongs to as `turnDiff`.
/// A row holding one of the turn's streamed message ids wins; otherwise the
/// first assistant row created inside the turn's recorded time span. A
/// summary that matches no held row draws nothing (it may belong to a page
/// not loaded yet), and each summary attaches to at most one row.
List<JsonMap> attachChatTurnDiffs(List<JsonMap> rows, ChatTurnDiffs diffs) {
if (diffs.isEmpty) return rows;
final assigned = <int, JsonMap>{};
for (final summary in diffs.summaries) {
final index = _rowFor(rows, summary, assigned);
if (index != null) assigned[index] = summary;
}
if (assigned.isEmpty) return rows;
return [
for (var i = 0; i < rows.length; i++)
if (assigned[i] case final summary?)
{...rows[i], 'turnDiff': summary}
else
rows[i],
];
}
int? _rowFor(List<JsonMap> rows, JsonMap summary, Map<int, JsonMap> taken) {
final ids = {
for (final id in summary['message_ids'] as List? ?? const []) '$id',
};
if (ids.isNotEmpty) {
for (var i = 0; i < rows.length; i++) {
if (rows[i]['kind'] != 'assistant' || taken.containsKey(i)) continue;
final held = rows[i]['messageIds'] as List? ?? const [];
if (held.any((id) => ids.contains('$id'))) return i;
}
}
final started = summary['started_at'], ended = summary['ended_at'];
if (started is! num || ended is! num || started <= 0) return null;
for (var i = 0; i < rows.length; i++) {
if (rows[i]['kind'] != 'assistant' || taken.containsKey(i)) continue;
final created = rows[i]['created_at'];
if (created is num && created >= started && created <= ended) return i;
}
return null;
}
int chatTurnDiffFiles(JsonMap summary) => _count(summary['files']);
int chatTurnDiffAdditions(JsonMap summary) => _count(summary['additions']);
int chatTurnDiffDeletions(JsonMap summary) => _count(summary['deletions']);
bool chatTurnDiffTruncated(JsonMap summary) => summary['truncated'] == true;
/// The chip's label, e.g. `3 files changed +42 −7`.
String chatTurnDiffLabel(JsonMap summary) {
final files = chatTurnDiffFiles(summary);
return '${files == 1 ? '1 file' : '$files files'} changed '
'+${chatTurnDiffAdditions(summary)} '
'−${chatTurnDiffDeletions(summary)}';
}
/// The viewer's heading in the run Changes stat form, e.g.
/// `3 files changed +42 -7`; a zero count is left out, as there.
String chatTurnDiffHeading(JsonMap summary) {
final files = chatTurnDiffFiles(summary);
final stat = changeStat(
chatTurnDiffAdditions(summary),
chatTurnDiffDeletions(summary),
);
return '${files == 1 ? '1 file' : '$files files'} changed'
'${stat.isEmpty ? '' : ' $stat'}';
}
/// The chip's accessible name: the counts in words, and that it opens.
String chatTurnDiffSemantics(JsonMap summary) {
final files = chatTurnDiffFiles(summary);
final added = chatTurnDiffAdditions(summary);
final removed = chatTurnDiffDeletions(summary);
return '${files == 1 ? '1 file' : '$files files'} changed, '
'$added ${added == 1 ? 'line' : 'lines'} added, '
'$removed ${removed == 1 ? 'line' : 'lines'} removed'
'${chatTurnDiffTruncated(summary) ? ', partly shown' : ''}'
', open changes';
}
/// The note shown above a truncated turn's files.
String chatTurnDiffTruncationNote(String diagnostic) {
final detail = diagnostic.trim();
return detail.isEmpty
? 'Some changes exceeded the capture limits, so this is not the '
'complete diff.'
: detail;
}
int _count(Object? value) => value is num ? value.toInt() : 0;