part of 'chat_shell_runs_controller.dart'; /// Dismissing finished command cards for every client of the chat, the /// dismissed runs the server keeps (the transcript's command history), and /// Restore, which puts a card back. extension ChatShellRunsDismissal on ChatShellRunsController { /// The dismissed runs this copy holds, oldest first. List get dismissedRuns => [..._dismissed.values] ..sort((a, b) => a.startedAt.compareTo(b.startedAt)); /// One dismissed run, or null once it was restored (or aged out). ChatShellRun? dismissedRun(String id) => _dismissed[id]; /// Dismisses finished runs [ids] for every client of the chat: their cards /// go at once into the command history and come back (with the refusal) if /// the server refuses. Returns the refusal, or null. A running command is /// never dismissed. Future dismiss(List ids) async { final removed = [ for (final id in ids) if (_runs[id] case final run? when !run.running) run, ]; if (removed.isEmpty) return null; for (final run in removed) { _remove(run.id); } _changed(membership: true); try { await _repository.dismiss(sessionId, [for (final r in removed) r.id]); return null; } catch (error) { if (_disposed) return null; final reason = _reason(error); for (final run in removed) { _gone.remove(run.id); _dismissed.remove(run.id); _runs[run.id] = run; _errors[run.id] = reason; } _changed(membership: true); return reason; } } /// Puts dismissed runs [ids] back in the transcript as cards, for every /// client. Returns the refusal, or null. Future restore(List ids) async { try { final restored = await _repository.restore(sessionId, ids); if (_disposed) return null; for (final run in restored) { _gone.remove(run.id); _dismissed.remove(run.id); final held = _runs[run.id]; final keep = held != null && held.outputEnd >= run.outputEnd; _runs[run.id] = keep ? held.withLifecycle(run) : run; if (!keep) terminals.replaced(run); } _changed(membership: true); return null; } catch (error) { return _reason(error); } } /// The dismissed run [id] with everything the server kept of its output /// and its plain-text rendering, for the history's read-only output view. Future fullOutput(String id) => _repository.output(sessionId, id); /// Folds a readback's dismissed runs into the history. Runs this copy /// dismissed but the readback does not list yet stay held. void _holdDismissed(List runs) { for (final run in runs) { _gone.add(run.id); _dismissed[run.id] = run; } } /// Takes [id]'s card out of the transcript; a held copy moves into the /// command history. void _remove(String id) { if (_runs[id] case final run?) _dismissed[id] = run; _gone.add(id); _runs.remove(id); _errors.remove(id); _shares.remove(id); _stopping.remove(id); terminals.removed(id); } }