Files
pipelines/app/lib/features/changes/domain/file_change.dart
T
alan 61c7eee2b0 App: open a chat, the terminal or a diff in a new window
"Open in new window" sits beside Float: the chat pane and chat list row
menus, a floating chat window's title bar, the terminal options menu and
the diff bar, plus Alt+Shift+Y for the chat with the keyboard (listed in
the shortcut help). Once the window opens the item leaves the main window
as Float takes a pane out of the tiling.

The window is its own app instance at /window/chat/<id>,
/window/terminal?tab=<key> or /window/diff/<token>: only that item, named
after it, with "Bring back to main window". It has its own connection and
writes no shared layout (no open-chat workspace, window store or terminal
layout) and posts no notifications, so they stay the main window's.

Web opens a popup window (a blocked popup offers a tab); Linux and macOS
start a second process with --detached (Flutter's multi-window API is still
experimental); Android and iOS hide the action. The capture kit gains a
follow step for windows a page opens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:47:42 +00:00

153 lines
5.2 KiB
Dart

import '../../../core/api_gateway.dart';
import 'change_kind.dart';
import 'unified_patch.dart';
/// One recorded file change, as the server's `file_changes.files[]` entry
/// describes it. A null/blank [patch] is a recorded fact (binary, truncated or
/// no text diff), never an invented empty diff.
class FileChange {
FileChange({
required this.path,
required this.kind,
String? patch,
this.binary = false,
this.truncated = false,
String? noPatchReason,
String? beforeMode,
String? afterMode,
this.steps = const [],
ParsedPatch? parsedPatch,
}) : patch = _nonBlank(patch),
beforeMode = _nonBlank(beforeMode),
noPatchReason = _nonBlank(noPatchReason),
afterMode = _nonBlank(afterMode),
// Aggregate copies pass their already-parsed patch through instead of
// re-parsing the same recorded bytes for every stage that touched a path.
parsedPatch = parsedPatch ?? parseUnifiedPatch(patch);
final String path;
final ChangeKind kind;
/// The server's unified patch verbatim, or null when it recorded none. This
/// exact string is what Copy patch puts on the clipboard, including any
/// trailing newline or truncated final line.
final String? patch;
final bool binary;
final bool truncated;
/// Why the server recorded no (complete) patch: `binary`, `file_size`,
/// `budget`, `lines`, `evidence_size`, `git_status` or `baseline`. Null from
/// an older server or when a patch was recorded.
final String? noPatchReason;
final String? beforeMode;
final String? afterMode;
/// Stage names that recorded a change to this path, in run order and deduped.
/// Empty for a single recorded entry; filled by [runChanges] aggregation.
final List<String> steps;
final ParsedPatch parsedPatch;
int get added => parsedPatch.added;
int get removed => parsedPatch.removed;
/// True when a patch the operator can read is present.
bool get hasPatch => !binary && patch != null;
/// True when the recorded permissions differ.
bool get modeChanged =>
beforeMode != null && afterMode != null && beforeMode != afterMode;
/// True when the recorded permissions differ and no content change explains
/// it. The server records no text diff for a mode-only change.
bool get otherModeChange =>
modeChanged && !binary && added == 0 && removed == 0;
List<String> get tags => [
if (binary) 'binary',
if (truncated) 'truncated',
if (otherModeChange) 'mode change',
];
FileChange withSteps(List<String> value) => FileChange(
path: path,
kind: kind,
patch: patch,
binary: binary,
truncated: truncated,
noPatchReason: noPatchReason,
beforeMode: beforeMode,
afterMode: afterMode,
steps: List.unmodifiable(value),
parsedPatch: parsedPatch,
);
/// The entry as the server records it ([tryParse] reads it back), with
/// the stages that touched it, so a diff can be handed to another window.
Map<String, Object?> toJson() => {
'path': path,
'kind': kind.name,
'patch': ?patch,
if (binary) 'binary': true,
if (truncated) 'truncated': true,
'no_patch_reason': ?noPatchReason,
'before_mode': ?beforeMode,
'after_mode': ?afterMode,
if (steps.isNotEmpty) 'steps': steps,
};
/// Reads back an entry [toJson] wrote, stages included.
static FileChange? fromHandover(Object? value) {
final file = tryParse(value);
if (file == null || value is! Map) return file;
final steps = value['steps'];
return steps is List && steps.isNotEmpty
? file.withSteps([for (final step in steps) '$step'])
: file;
}
/// Parses one server file entry; null when it has no usable path. The
/// capture owns the decision to skip such an entry, so it is never rendered
/// as an anonymous row.
static FileChange? tryParse(dynamic value) {
if (value is! Map) return null;
final json = Map<String, dynamic>.from(value);
final path = _text(json['path']);
if (path.trim().isEmpty) return null;
return FileChange(
path: path,
kind: changeKind(json['kind']),
patch: json['patch'] is String ? json['patch'] as String : null,
binary: json['binary'] == true,
truncated: json['truncated'] == true,
noPatchReason: json['no_patch_reason'] is String
? json['no_patch_reason'] as String
: null,
beforeMode: _textOrNull(json['before_mode']),
afterMode: _textOrNull(json['after_mode']),
);
}
/// Parses a required file entry. Integration code normally receives files
/// from [ChangeCapture] or [runChanges]; this constructor is for a viewer
/// opened directly with one recorded entry.
factory FileChange.fromJson(JsonMap json) {
final file = tryParse(json);
if (file == null) {
throw const FormatException('A recorded file change needs a path.');
}
return file;
}
}
/// A value kept as itself when present, else null (`?:` over nullable fields).
String? _textOrNull(dynamic value) => value == null ? null : '$value';
String _text(dynamic value) => '${value ?? ''}';
/// A string kept only when it has non-whitespace content.
String? _nonBlank(String? value) {
if (value == null) return null;
return value.trim().isEmpty ? null : value;
}