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:
@@ -0,0 +1,166 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../ui/adaptive/segmented_bar_overflow.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import 'chat_shell_runs_controller.dart';
|
||||
|
||||
const String chatShellDismissLabel = 'Dismiss';
|
||||
const String chatShellDismissAllLabel = 'Dismiss all finished commands';
|
||||
const String chatShellUndoLabel = 'Undo';
|
||||
|
||||
/// How long the Undo notice stays. The server keeps a dismissed run
|
||||
/// restorable for longer (protocol ChatShellRunUndoWindow, 30 s).
|
||||
const Duration chatShellUndoDuration = Duration(seconds: 5);
|
||||
|
||||
/// The card's Dismiss button, offered once the command has finished.
|
||||
class ChatShellDismissButton extends StatelessWidget {
|
||||
const ChatShellDismissButton({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.onPressed,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final VoidCallback onPressed;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!cupertino) {
|
||||
return IconButton(
|
||||
key: const Key('chat-shell-run-dismiss'),
|
||||
tooltip: chatShellDismissLabel,
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: onPressed,
|
||||
);
|
||||
}
|
||||
return Semantics(
|
||||
label: chatShellDismissLabel,
|
||||
button: true,
|
||||
excludeSemantics: true,
|
||||
child: CupertinoButton(
|
||||
key: const Key('chat-shell-run-dismiss'),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
onPressed: onPressed,
|
||||
child: const Icon(CupertinoIcons.xmark),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The conversation's "Dismiss all finished commands" More menu entry.
|
||||
Widget chatShellDismissAllMenuAction({required VoidCallback onPressed}) =>
|
||||
SegmentedOverflowAction(
|
||||
key: const ValueKey<String>('chat-shell-dismiss-all-action'),
|
||||
overflowEntry: SegmentedOverflowEntry(
|
||||
label: chatShellDismissAllLabel,
|
||||
icon: Icons.clear_all,
|
||||
onPressed: onPressed,
|
||||
),
|
||||
child: const SizedBox.shrink(),
|
||||
);
|
||||
|
||||
/// Dismisses [ids] for every client of the chat, then offers Undo for
|
||||
/// [chatShellUndoDuration]: a Material SnackBar, or a transient Cupertino
|
||||
/// notice floating over the conversation. A refusal goes to [onRefused].
|
||||
Future<void> dismissChatShellRuns(
|
||||
BuildContext context,
|
||||
ChatShellRunsController controller,
|
||||
List<String> ids, {
|
||||
required bool cupertino,
|
||||
void Function(String refusal)? onRefused,
|
||||
}) async {
|
||||
if (ids.isEmpty) return;
|
||||
final messenger = cupertino ? null : ScaffoldMessenger.maybeOf(context);
|
||||
final overlay = Overlay.maybeOf(context, rootOverlay: true);
|
||||
final refusal = await controller.dismiss(ids);
|
||||
if (refusal != null) {
|
||||
onRefused?.call(refusal);
|
||||
return;
|
||||
}
|
||||
void show(String message, {VoidCallback? onUndo}) {
|
||||
if (messenger != null) {
|
||||
messenger.hideCurrentSnackBar();
|
||||
messenger.showSnackBar(
|
||||
SnackBar(
|
||||
key: const Key('chat-shell-undo-notice'),
|
||||
content: Text(message),
|
||||
duration: chatShellUndoDuration,
|
||||
// An action would otherwise keep it until dismissed by hand.
|
||||
persist: false,
|
||||
action: onUndo == null
|
||||
? null
|
||||
: SnackBarAction(label: chatShellUndoLabel, onPressed: onUndo),
|
||||
),
|
||||
);
|
||||
} else if (overlay != null && overlay.mounted) {
|
||||
_showOverlayNotice(overlay, message, onUndo);
|
||||
}
|
||||
}
|
||||
|
||||
show(
|
||||
ids.length == 1 ? 'Command dismissed' : '${ids.length} commands dismissed',
|
||||
onUndo: () => unawaited(() async {
|
||||
final failed = await controller.restore(ids);
|
||||
if (failed != null) show("Couldn't restore: $failed");
|
||||
}()),
|
||||
);
|
||||
}
|
||||
|
||||
OverlayEntry? _overlayNotice;
|
||||
Timer? _overlayTimer;
|
||||
|
||||
void _hideOverlayNotice() {
|
||||
_overlayTimer?.cancel();
|
||||
_overlayTimer = null;
|
||||
final entry = _overlayNotice;
|
||||
_overlayNotice = null;
|
||||
if (entry != null && entry.mounted) entry.remove();
|
||||
}
|
||||
|
||||
void _showOverlayNotice(
|
||||
OverlayState overlay,
|
||||
String message,
|
||||
VoidCallback? onUndo,
|
||||
) {
|
||||
_hideOverlayNotice();
|
||||
final entry = OverlayEntry(
|
||||
builder: (context) => Positioned(
|
||||
left: 16,
|
||||
right: 16,
|
||||
bottom: 16,
|
||||
child: SafeArea(
|
||||
child: Semantics(
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
liveRegion: true,
|
||||
child: KeyedSubtree(
|
||||
key: const Key('chat-shell-undo-notice'),
|
||||
child: onUndo == null
|
||||
? CupertinoPopupSurface(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: DefaultTextStyle.merge(
|
||||
style: CupertinoTheme.of(context).textTheme.textStyle,
|
||||
child: Text(message),
|
||||
),
|
||||
),
|
||||
)
|
||||
: ChatUndoBar(
|
||||
message: message,
|
||||
onUndo: () {
|
||||
_hideOverlayNotice();
|
||||
onUndo();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
_overlayNotice = entry;
|
||||
overlay.insert(entry);
|
||||
_overlayTimer = Timer(chatShellUndoDuration, _hideOverlayNotice);
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -6,6 +8,7 @@ import '../../../ui/theme/app_theme.dart';
|
||||
import '../conversation/chat_card_styles.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import 'chat_shell_card_actions.dart';
|
||||
import 'chat_shell_dismiss.dart';
|
||||
import 'chat_shell_output_view.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
import 'chat_shell_runs_controller.dart';
|
||||
@@ -15,7 +18,9 @@ import 'chat_shell_terminal_page.dart';
|
||||
/// One operator command in the transcript, as a terminal card in the reading
|
||||
/// column: the command and folder, its terminal (typed into while it runs),
|
||||
/// a spinner and Stop while it runs, then its exit status, Copy output and
|
||||
/// Send to agent. Full screen opens the same terminal over the whole window.
|
||||
/// Send to agent, and Dismiss (for every client of the chat, with Undo). A
|
||||
/// running command has no Dismiss: Stop it first. Full screen opens the same
|
||||
/// terminal over the whole window.
|
||||
///
|
||||
/// It listens to [controller] itself, so streamed output rebuilds only this
|
||||
/// card, never the transcript.
|
||||
@@ -126,6 +131,16 @@ class _ChatShellRunCardState extends State<ChatShellRunCard> {
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ChatShellStatus(run: run, cupertino: cupertino),
|
||||
if (!run.running)
|
||||
ChatShellDismissButton(
|
||||
cupertino: cupertino,
|
||||
onPressed: () => unawaited(
|
||||
// A refusal brings the card back with the server's reason.
|
||||
dismissChatShellRuns(context, controller, [
|
||||
run.id,
|
||||
], cupertino: cupertino),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
part of 'chat_shell_runs_controller.dart';
|
||||
|
||||
/// Dismissing finished command cards for every client of the chat, and the
|
||||
/// Undo that restores them while the server still keeps them.
|
||||
extension ChatShellRunsDismissal on ChatShellRunsController {
|
||||
/// Dismisses finished runs [ids] for every client of the chat: their cards
|
||||
/// go at once and come back (with the refusal) if the server refuses.
|
||||
/// Returns the refusal, or null. A running command is never dismissed.
|
||||
Future<String?> dismiss(List<String> 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);
|
||||
_runs[run.id] = run;
|
||||
_errors[run.id] = reason;
|
||||
}
|
||||
_changed(membership: true);
|
||||
return reason;
|
||||
}
|
||||
}
|
||||
|
||||
/// Undoes the dismissal of [ids] while the server still keeps them.
|
||||
/// Returns the refusal, or null.
|
||||
Future<String?> restore(List<String> ids) async {
|
||||
try {
|
||||
final restored = await _repository.restore(sessionId, ids);
|
||||
if (_disposed) return null;
|
||||
for (final run in restored) {
|
||||
_gone.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);
|
||||
}
|
||||
}
|
||||
|
||||
void _remove(String id) {
|
||||
_gone.add(id);
|
||||
_runs.remove(id);
|
||||
_errors.remove(id);
|
||||
_shares.remove(id);
|
||||
_stopping.remove(id);
|
||||
terminals.removed(id);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ class ChatShellRunsRepository {
|
||||
int? rows,
|
||||
String? data,
|
||||
int? after,
|
||||
List<String>? runIds,
|
||||
}) => {
|
||||
'chat_shell_run_request': {
|
||||
'session_id': sessionId,
|
||||
@@ -23,6 +24,7 @@ class ChatShellRunsRepository {
|
||||
'rows': ?rows,
|
||||
'data': ?data,
|
||||
'after': ?after,
|
||||
'run_ids': ?runIds,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -55,11 +57,37 @@ class ChatShellRunsRepository {
|
||||
Future<void> stop(String sessionId, String runId) =>
|
||||
gateway.call('chat_shell_stop', _body(sessionId, runId: runId));
|
||||
|
||||
/// The session's recorded runs, oldest first, each with its output tail.
|
||||
Future<List<ChatShellRun>> runs(String sessionId) async {
|
||||
/// The session's recorded runs, oldest first, each with its output tail,
|
||||
/// and the ids of its dismissed runs (whose cards go away).
|
||||
Future<({List<ChatShellRun> runs, Set<String> dismissed})> runs(
|
||||
String sessionId,
|
||||
) async {
|
||||
final reply = await gateway.call('chat_shell_runs', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
});
|
||||
return (runs: _records(reply), dismissed: _ids(reply));
|
||||
}
|
||||
|
||||
/// Dismisses finished runs for every client of the chat; the reply names
|
||||
/// the runs the server dismissed.
|
||||
Future<Set<String>> dismiss(String sessionId, List<String> runIds) async =>
|
||||
_ids(
|
||||
await gateway.call(
|
||||
'chat_shell_dismiss',
|
||||
_body(sessionId, runIds: runIds),
|
||||
),
|
||||
);
|
||||
|
||||
/// Undoes a dismissal still inside the server's undo window; the reply is
|
||||
/// the restored runs with their output tail.
|
||||
Future<List<ChatShellRun>> restore(
|
||||
String sessionId,
|
||||
List<String> runIds,
|
||||
) async => _records(
|
||||
await gateway.call('chat_shell_restore', _body(sessionId, runIds: runIds)),
|
||||
);
|
||||
|
||||
List<ChatShellRun> _records(JsonMap reply) {
|
||||
final value = reply['chat_shell_runs'];
|
||||
return [
|
||||
if (value is List)
|
||||
@@ -67,6 +95,14 @@ class ChatShellRunsRepository {
|
||||
];
|
||||
}
|
||||
|
||||
Set<String> _ids(JsonMap reply) {
|
||||
final value = reply['chat_shell_dismissed'];
|
||||
return {
|
||||
if (value is List)
|
||||
for (final id in value) '$id',
|
||||
};
|
||||
}
|
||||
|
||||
/// One run with the whole output the server retained and its plain-text
|
||||
/// rendering, or only the output from absolute byte [after] on.
|
||||
Future<ChatShellRun> output(
|
||||
|
||||
@@ -54,6 +54,14 @@ class ChatShellTerminals {
|
||||
void appended(ChatShellRun run, String text, int offset, int end) =>
|
||||
_sessions[run.id]?.append(run, text, offset, end);
|
||||
|
||||
/// [runId]'s card was dismissed: drop its emulator.
|
||||
void removed(String runId) {
|
||||
_resizes.remove(runId)?.cancel();
|
||||
_inputs.remove(runId);
|
||||
_sentSizes.remove(runId);
|
||||
_sessions.remove(runId);
|
||||
}
|
||||
|
||||
/// [run] ended: draw its final output read-only.
|
||||
void finished(ChatShellRun run) {
|
||||
_resizes.remove(run.id)?.cancel();
|
||||
|
||||
Reference in New Issue
Block a user