Dismiss finished shell command cards with Undo

Finished command cards get a Dismiss button and the chat's More menu a
Dismiss all finished commands entry. Dismissal applies to every client of
the chat; a SnackBar (Material) or transient notice (Cupertino) offers Undo
for 5 s. Running commands cannot be dismissed until they end.
This commit is contained in:
2026-11-09 21:40:20 +00:00
parent 03123c23fd
commit bac21efc5b
11 changed files with 639 additions and 9 deletions
@@ -8,6 +8,8 @@ import 'chat_shell_run.dart';
import 'chat_shell_runs_repository.dart';
import 'chat_shell_terminals.dart';
part 'chat_shell_runs_dismissal.dart';
/// Where one run's "Send to agent" stands.
enum ChatShellShareState { idle, sending, sent }
@@ -45,6 +47,10 @@ class ChatShellRunsController extends ChangeNotifier {
final Set<String> _stopping = {};
final Map<String, ChatShellShareState> _shares = {};
final Map<String, String> _errors = {};
/// Runs dismissed here or by another client: a readback racing the
/// dismissal does not bring their cards back.
final Set<String> _gone = {};
bool _disposed = false;
int _generation = 0;
@@ -55,6 +61,12 @@ class ChatShellRunsController extends ChangeNotifier {
ChatShellShareState shareState(String id) =>
_shares[id] ?? ChatShellShareState.idle;
/// The finished runs, oldest first: what Dismiss all finished removes.
List<String> get finishedIds => [
for (final run in _runs.values)
if (!run.running) run.id,
];
/// The last refused card action of [id], in the server's words.
String? errorFor(String id) => _errors[id];
@@ -63,9 +75,11 @@ class ChatShellRunsController extends ChangeNotifier {
Future<void> refresh() async {
final token = ++_generation;
try {
final listed = await _repository.runs(sessionId);
final (:runs, :dismissed) = await _repository.runs(sessionId);
if (_disposed || token != _generation) return;
for (final run in listed) {
dismissed.forEach(_remove);
for (final run in runs) {
if (_gone.contains(run.id)) continue;
final held = _runs[run.id];
if (held != null &&
held.running &&
@@ -189,8 +203,15 @@ class ChatShellRunsController extends ChangeNotifier {
final frame = envelope['chat_shell_run_event'];
if (frame is! Map || frame['session_id'] != sessionId) return;
final id = '${frame['run_id'] ?? ''}';
if (frame['dismissed'] == true) {
_remove(id);
_changed(membership: true);
return;
}
final record = ChatShellRun.fromJson(frame['run']);
if (record != null) {
// A restored run (another client's Undo) comes back without output.
if (_gone.remove(record.id) && !record.running) unawaited(refresh());
final held = _runs[record.id];
final next = held == null ? record : held.withLifecycle(record);
_runs[record.id] = next;
@@ -198,9 +219,11 @@ class ChatShellRunsController extends ChangeNotifier {
_stopping.remove(record.id);
if (held != null && held.running) terminals.finished(next);
}
_changed(membership: held == null);
// Membership also tracks finishing: Dismiss all finished depends on it.
_changed(membership: held == null || held.running != next.running);
return;
}
if (_gone.contains(id)) return;
final held = _runs[id];
if (held == null) {
// Output for a run this copy has not seen yet: read the record.