feat(app): dismiss recorded local chat notices in the thread
Show dismissible notices for refused history reads, refused live watches and a running compaction. A dismissal lasts only while the recorded condition is unchanged; a syncToken gap hides pre-gap notices until fresh state arrives.
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/chat_activity_detail.dart';
|
||||
import '../domain/chat_notice.dart';
|
||||
import '../domain/chat_transcript.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import '../ui/transcript/chat_markdown_view.dart';
|
||||
@@ -63,6 +64,25 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
late ChatComposerController _composer = _attachComposer();
|
||||
late ChatRowKeys _rowKeys = ChatRowKeys();
|
||||
|
||||
/// Local notices the operator hid; cleared when the conversation or socket
|
||||
/// changes so a reconnect never keeps a stale dismissal.
|
||||
ChatNoticeDismissals _dismissals = ChatNoticeDismissals();
|
||||
|
||||
/// True while a reconnect repair is in flight: notices recorded before the
|
||||
/// gap are stale, so none is drawn until the repair reports fresh state.
|
||||
bool _reconnecting = false;
|
||||
int _syncGeneration = 0;
|
||||
|
||||
/// The transcript held when the socket was replaced. A running state recorded
|
||||
/// before the gap cannot be trusted again until a fresh read or frame
|
||||
/// replaces that transcript.
|
||||
List<JsonMap>? _preGapMessages;
|
||||
|
||||
/// The chain that repairs the newest socket gap. A second gap waits for an
|
||||
/// in-flight repair instead of treating the controller's immediate return as
|
||||
/// completion, which would expose stale notices mid-repair.
|
||||
Future<void>? _repair;
|
||||
|
||||
/// A sent turn shown before history or a live frame echoes it.
|
||||
final List<JsonMap> _optimistic = [];
|
||||
|
||||
@@ -132,9 +152,38 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_controller = _attach();
|
||||
_composer = _attachComposer();
|
||||
_rowKeys = ChatRowKeys();
|
||||
_dismissals = ChatNoticeDismissals();
|
||||
_reconnecting = false;
|
||||
_preGapMessages = null;
|
||||
_syncGeneration++;
|
||||
_optimistic.clear();
|
||||
} else if (oldWidget.syncToken != widget.syncToken) {
|
||||
unawaited(_controller.resync());
|
||||
// The socket was replaced: conditions recorded before the gap are stale,
|
||||
// so no earlier dismissal survives and nothing recorded before the gap is
|
||||
// painted while the repair runs.
|
||||
_dismissals.clear();
|
||||
_preGapMessages = _controller.messages;
|
||||
_reconnecting = true;
|
||||
final generation = ++_syncGeneration;
|
||||
final previous = _repair;
|
||||
final repair = (previous ?? Future<void>.value())
|
||||
.then((_) async {
|
||||
// A newer gap owns the repair; its own step in this chain runs next.
|
||||
if (!mounted || generation != _syncGeneration) return;
|
||||
await _controller.resync();
|
||||
})
|
||||
.whenComplete(() {
|
||||
if (!mounted || generation != _syncGeneration) return;
|
||||
setState(() {
|
||||
_reconnecting = false;
|
||||
// A successful repair replaced the transcript with fresh state.
|
||||
if (!identical(_controller.messages, _preGapMessages)) {
|
||||
_preGapMessages = null;
|
||||
}
|
||||
});
|
||||
});
|
||||
_repair = repair;
|
||||
unawaited(repair);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,6 +200,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
builder: (context, _) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final messages = _visibleMessages();
|
||||
final cards = chatCardRows(
|
||||
permissions: _controller.permissionFrames,
|
||||
forms: _controller.questionForms,
|
||||
@@ -162,7 +212,29 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
// not generating, so the transcript indicator and its live region stand
|
||||
// down; the composer's Stop control still follows `running`.
|
||||
final awaiting = cards.any(chatCardAwaitingOperator);
|
||||
final rows = transcriptRows(_visibleMessages(), trailing: cards);
|
||||
final allRows = transcriptRows(messages, trailing: cards);
|
||||
// A transcript held from before a socket gap may still say a compaction is
|
||||
// running even when the repair's read is refused. That running state is
|
||||
// stale, so no compaction notice is derived from it until a fresh read or
|
||||
// frame replaces the transcript. Fresh failure notices still report.
|
||||
//
|
||||
// Notices report recorded conditions only: a refused read/watch and a
|
||||
// running compaction. A dismissal keeps one hidden until its condition
|
||||
// changes. A running compaction's own activity line is that notice, so it
|
||||
// is not drawn twice; hiding it when dismissed is the whole dismissal.
|
||||
final allNotices = _reconnecting
|
||||
? const <ChatNotice>[]
|
||||
: chatNotices(
|
||||
historyError: _controller.error,
|
||||
liveError: _controller.subscriptionError,
|
||||
messages: _staleTranscript ? const [] : _controller.messages,
|
||||
);
|
||||
final notices = _dismissals.visible(allNotices);
|
||||
final compactionNotice = _noticeOf(notices, ChatNoticeKind.compaction);
|
||||
final rows = [
|
||||
for (final row in allRows)
|
||||
if (!_hiddenRunningCompaction(row)) row,
|
||||
];
|
||||
// Older pages prepend messages and a live frame can append one, so a
|
||||
// folded assistant turn's first message id is not a stable identity.
|
||||
// ChatRowKeys carries a turn's key forward while it still holds any of
|
||||
@@ -170,6 +242,11 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
// across paging and streaming.
|
||||
final keys = _rowKeys.forRows(rows);
|
||||
final initialLoad = !_controller.loaded && _controller.loading;
|
||||
final historyNotice = _noticeOf(
|
||||
notices,
|
||||
ChatNoticeKind.connectionRefused,
|
||||
);
|
||||
final liveNotice = _noticeOf(notices, ChatNoticeKind.liveFailure);
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
@@ -230,21 +307,23 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_controller.error != null)
|
||||
ChatErrorNotice(
|
||||
if (historyNotice != null)
|
||||
_ChatNoticeLine(
|
||||
key: const Key('chat-history-error'),
|
||||
message: 'Chat history unavailable\n${_controller.error}',
|
||||
notice: historyNotice,
|
||||
onDismiss: _dismiss,
|
||||
onRetry: _controller.load,
|
||||
),
|
||||
if (_controller.subscriptionError != null)
|
||||
ChatErrorNotice(
|
||||
if (liveNotice != null)
|
||||
_ChatNoticeLine(
|
||||
key: const Key('chat-live-error'),
|
||||
message:
|
||||
'Live updates unavailable\n${_controller.subscriptionError}',
|
||||
notice: liveNotice,
|
||||
onDismiss: _dismiss,
|
||||
onRetry: _controller.resync,
|
||||
),
|
||||
if (_controller.loaded &&
|
||||
rows.isEmpty &&
|
||||
allRows.isEmpty &&
|
||||
allNotices.isEmpty &&
|
||||
_controller.error == null)
|
||||
const Padding(
|
||||
key: Key('chat-thread-empty'),
|
||||
@@ -255,7 +334,15 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
Padding(
|
||||
key: ValueKey<String>(keys[index]),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: _transcriptRow(rows[index]),
|
||||
child: _transcriptRow(
|
||||
rows[index],
|
||||
onDismissCompaction:
|
||||
compactionNotice != null &&
|
||||
chatNoticeActivityCondition(rows[index]) ==
|
||||
compactionNotice.condition
|
||||
? () => _dismiss(compactionNotice)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
if (_controller.running && !awaiting)
|
||||
Padding(
|
||||
@@ -279,9 +366,41 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
},
|
||||
);
|
||||
|
||||
Widget _transcriptRow(JsonMap row) {
|
||||
ChatNotice? _noticeOf(List<ChatNotice> notices, ChatNoticeKind kind) {
|
||||
for (final notice in notices) {
|
||||
if (notice.kind == kind) return notice;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Whether a running compaction's activity line must stay off screen.
|
||||
///
|
||||
/// That line is the compaction notice, so it follows the same conditions: it
|
||||
/// is hidden while the repair runs, while the held transcript is still the
|
||||
/// pre-gap one, and when the operator dismissed this exact compaction. A
|
||||
/// different running compaction in the transcript keeps its own line.
|
||||
bool _hiddenRunningCompaction(JsonMap row) {
|
||||
final condition = chatNoticeActivityCondition(row);
|
||||
if (condition == null) return false;
|
||||
return _reconnecting ||
|
||||
_staleTranscript ||
|
||||
_dismissals.isDismissed(condition);
|
||||
}
|
||||
|
||||
/// Whether the held transcript is still the one recorded before the gap.
|
||||
bool get _staleTranscript =>
|
||||
_preGapMessages != null &&
|
||||
identical(_controller.messages, _preGapMessages);
|
||||
|
||||
void _dismiss(ChatNotice notice) {
|
||||
setState(() => _dismissals.dismiss(notice));
|
||||
}
|
||||
|
||||
Widget _transcriptRow(JsonMap row, {VoidCallback? onDismissCompaction}) {
|
||||
final kind = '${row['kind'] ?? ''}';
|
||||
if (kind == 'activity') return _ChatActivityLine(row: row);
|
||||
if (kind == 'activity') {
|
||||
return _ChatActivityLine(row: row, onDismiss: onDismissCompaction);
|
||||
}
|
||||
if (kind == 'permission' || kind == 'question') {
|
||||
return ChatTranscriptCard(
|
||||
row: row,
|
||||
@@ -312,6 +431,126 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
);
|
||||
}
|
||||
|
||||
/// The native dismiss control shared by a local notice and a running
|
||||
/// compaction's activity line. Material uses a tooltip; Cupertino carries the
|
||||
/// same accessible name on the wrapping node, since its button has none.
|
||||
Widget _noticeDismissControl({
|
||||
required bool cupertino,
|
||||
required String label,
|
||||
required Key key,
|
||||
required VoidCallback onPressed,
|
||||
}) {
|
||||
final icon = Icon(cupertino ? CupertinoIcons.xmark_circle_fill : Icons.close);
|
||||
if (!cupertino) {
|
||||
return IconButton(
|
||||
key: key,
|
||||
tooltip: label,
|
||||
onPressed: onPressed,
|
||||
icon: icon,
|
||||
);
|
||||
}
|
||||
return Semantics(
|
||||
container: true,
|
||||
button: true,
|
||||
label: label,
|
||||
onTap: onPressed,
|
||||
child: ExcludeSemantics(
|
||||
child: CupertinoButton(
|
||||
key: key,
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: onPressed,
|
||||
child: icon,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// One local failure notice beside the conversation.
|
||||
///
|
||||
/// It is never a message bubble and never sent to the agent. The line carries
|
||||
/// the kind's own words plus the recorded reason, keeps its native Retry and
|
||||
/// adds a native dismiss control. Colour only repeats the failure state, so the
|
||||
/// words remain readable without it. A running compaction is a recorded activity
|
||||
/// line and is dismissed there instead.
|
||||
class _ChatNoticeLine extends StatelessWidget {
|
||||
const _ChatNoticeLine({
|
||||
super.key,
|
||||
required this.notice,
|
||||
required this.onDismiss,
|
||||
this.onRetry,
|
||||
});
|
||||
|
||||
final ChatNotice notice;
|
||||
final ValueChanged<ChatNotice> onDismiss;
|
||||
final VoidCallback? onRetry;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final theme = Theme.of(context);
|
||||
final color = cupertino
|
||||
? CupertinoColors.systemRed.resolveFrom(context)
|
||||
: theme.colorScheme.error;
|
||||
final style =
|
||||
(cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: theme.textTheme.bodyMedium)
|
||||
?.copyWith(color: color);
|
||||
final icon = cupertino
|
||||
? CupertinoIcons.exclamationmark_circle
|
||||
: Icons.error_outline;
|
||||
return Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 8, 4),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.only(top: 8),
|
||||
child: Icon(icon, color: color),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: Semantics(
|
||||
liveRegion: true,
|
||||
child: Text(notice.text, style: style),
|
||||
),
|
||||
),
|
||||
),
|
||||
_dismissControl(cupertino),
|
||||
],
|
||||
),
|
||||
if (onRetry != null)
|
||||
Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: cupertino
|
||||
? CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: onRetry,
|
||||
child: const Text('Retry'),
|
||||
)
|
||||
: TextButton(onPressed: onRetry, child: const Text('Retry')),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _dismissControl(bool cupertino) {
|
||||
return _noticeDismissControl(
|
||||
cupertino: cupertino,
|
||||
label: chatNoticeDismissLabel(notice.kind),
|
||||
key: ValueKey<String>('chat-notice-dismiss-${notice.kind.name}'),
|
||||
onPressed: () => onDismiss(notice),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// One recorded activity line for a turn.
|
||||
///
|
||||
/// The line is a summary of the recorded sequence. Its wording and state come
|
||||
@@ -321,10 +560,14 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
/// input, output, error, duration and edit diff; collapsing restores the single
|
||||
/// line without touching the turn.
|
||||
class _ChatActivityLine extends StatefulWidget {
|
||||
const _ChatActivityLine({required this.row});
|
||||
const _ChatActivityLine({required this.row, this.onDismiss});
|
||||
|
||||
final JsonMap row;
|
||||
|
||||
/// Set only for a running compaction, whose line is the local notice the
|
||||
/// operator can hide until that compaction's condition changes.
|
||||
final VoidCallback? onDismiss;
|
||||
|
||||
@override
|
||||
State<_ChatActivityLine> createState() => _ChatActivityLineState();
|
||||
}
|
||||
@@ -405,6 +648,13 @@ class _ChatActivityLineState extends State<_ChatActivityLine> {
|
||||
),
|
||||
),
|
||||
_toggleControl(cupertino),
|
||||
if (widget.onDismiss != null)
|
||||
_noticeDismissControl(
|
||||
cupertino: cupertino,
|
||||
label: chatNoticeDismissLabel(ChatNoticeKind.compaction),
|
||||
key: const ValueKey<String>('chat-notice-dismiss-compaction'),
|
||||
onPressed: widget.onDismiss!,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (_expanded) _detail(context, cupertino, secondary),
|
||||
|
||||
Reference in New Issue
Block a user