Show message delivery state and keep unsent messages across reloads
A sent message is stored on the device until the server accepts it, so a reload mid-request no longer loses it: on reopening, a stored intent the history records is cleared as sent, otherwise it shows as Not sent with Retry (same operation id) and Edit. The operator's bubble shows Sending, Sent (newest only), Queued or Not sent. A live refusal keeps the old contract (exact draft and chips back in the composer, unchanged re-send reuses the id) and marks the bubble Not sent with Retry. On web, leaving or reloading the page while a prompt is in flight asks first.
This commit is contained in:
@@ -170,6 +170,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
|
||||
ChatPasteUnsubscribe? _stopPaste;
|
||||
|
||||
/// The composer's accepted-send count the chips were last cleared for.
|
||||
late int _seenAccepted = widget.composer.composerSendsAccepted;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -184,6 +187,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
if (oldWidget.composer != widget.composer) {
|
||||
oldWidget.composer.removeListener(_syncDraft);
|
||||
widget.composer.addListener(_syncDraft);
|
||||
_seenAccepted = widget.composer.composerSendsAccepted;
|
||||
_syncDraft();
|
||||
}
|
||||
if (oldWidget.paste != widget.paste) {
|
||||
@@ -229,6 +233,14 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
/// live transcript rebuilds.
|
||||
void _syncDraft() {
|
||||
if (!mounted) return;
|
||||
final accepted = widget.composer.composerSendsAccepted;
|
||||
if (accepted != _seenAccepted) {
|
||||
_seenAccepted = accepted;
|
||||
setState(() {
|
||||
_replaceAttachments(const [], const []);
|
||||
_attachmentError = null;
|
||||
});
|
||||
}
|
||||
final draft = widget.composer.draft;
|
||||
if (_text.text == draft) return;
|
||||
_text.value = TextEditingValue(
|
||||
@@ -458,8 +470,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
}
|
||||
|
||||
/// Builds the chips' exact wire parts and hands them to the composer with the
|
||||
/// draft's text and operation id. The chips leave the composer with the
|
||||
/// message; a refusal keeps them on its "Not sent" bubble for Retry.
|
||||
/// draft's text and operation id. The chips stay while the prompt is in
|
||||
/// flight so a refusal leaves the draft and its files together; only an
|
||||
/// accepted prompt clears them.
|
||||
Future<void> _send() async {
|
||||
final composer = widget.composer;
|
||||
if (_uploading || composer.sending || composer.draft.trim().isEmpty) {
|
||||
@@ -507,16 +520,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
if (_uploading) setState(() => _uploading = false);
|
||||
// Keep the caret in the field while the prompt is in flight, as before.
|
||||
_focus.requestFocus();
|
||||
final sending = composer.send(attachments: parts);
|
||||
// The sent message now owns its files: a refusal keeps them with its
|
||||
// "Not sent" bubble for Retry, so the composer is free for the next one.
|
||||
if (composer.sending) {
|
||||
setState(() {
|
||||
_replaceAttachments(const [], const []);
|
||||
_attachmentError = null;
|
||||
});
|
||||
}
|
||||
await sending;
|
||||
// Acceptance (here or through the bubble's Retry) clears the chips via
|
||||
// the composer's accepted-send count.
|
||||
await composer.send(attachments: parts);
|
||||
}
|
||||
|
||||
/// Adds the operator's explicit selection through the host seam. A dismissed
|
||||
|
||||
@@ -75,10 +75,27 @@ class ChatComposerController extends ChangeNotifier {
|
||||
/// The delivery state of this conversation's own messages.
|
||||
final ChatDeliveries deliveries = ChatDeliveries();
|
||||
|
||||
/// The exact text in the composer. A refused message is not put back here;
|
||||
/// it stays "Not sent" until the operator retries or edits it.
|
||||
/// The last intent sent from the composer while its text and chips may
|
||||
/// still be the composer's: a refusal puts its exact draft back, and an
|
||||
/// unchanged re-send (or its bubble's Retry) reuses its operation id.
|
||||
ChatComposerSend? _pending;
|
||||
String _pendingDraft = '';
|
||||
int _composerSendsAccepted = 0;
|
||||
|
||||
/// The exact text in the composer. A refusal restores it only when the
|
||||
/// operator has not typed anything newer.
|
||||
String get draft => _draft;
|
||||
|
||||
/// Advances whenever the server accepts the message the composer's chips
|
||||
/// belong to, including through its bubble's Retry, so the bar can clear
|
||||
/// them.
|
||||
int get composerSendsAccepted => _composerSendsAccepted;
|
||||
|
||||
/// Whether this unsent intent's text is back in the composer, so its bubble
|
||||
/// offers Retry but not Edit.
|
||||
bool isInComposer(String operationId) =>
|
||||
_pending?.operationId == operationId && _draft.trim() == _pending!.text;
|
||||
|
||||
/// One prompt request is awaiting its reply; a second send is refused.
|
||||
bool get sending => _sending;
|
||||
|
||||
@@ -123,33 +140,55 @@ class ChatComposerController extends ChangeNotifier {
|
||||
}
|
||||
|
||||
/// Sends the non-blank draft as one plain message, carrying [attachments] in
|
||||
/// draft order. Only the wire text is trimmed. The draft is cleared at once
|
||||
/// and the intent is stored until the server accepts it; a refusal or lost
|
||||
/// request leaves it in [deliveries] as "Not sent" for [retry] or [edit].
|
||||
/// Returns whether the server accepted this prompt.
|
||||
/// draft order. Only the wire text is trimmed. The intent is stored on the
|
||||
/// device until the server accepts it. A refusal restores the exact draft
|
||||
/// (unless newer text is present) and shows the message as "Not sent"; an
|
||||
/// unchanged re-send (same text and parts) reuses its operation id, and a
|
||||
/// changed one replaces it. Returns whether the server accepted this prompt.
|
||||
Future<bool> send({List<JsonMap> attachments = const []}) async {
|
||||
if (_disposed || _sending) return false;
|
||||
final text = _draft.trim();
|
||||
final exact = _draft;
|
||||
final text = exact.trim();
|
||||
if (text.isEmpty) return false;
|
||||
final operationId = _nextOperationId();
|
||||
final intent = ChatComposerSend(
|
||||
sessionId: sessionId,
|
||||
text: text,
|
||||
operationId: operationId,
|
||||
nativeMessageId: nativeChatMessageId(operationId),
|
||||
attachments: attachments,
|
||||
);
|
||||
final previous = _pending;
|
||||
late final ChatComposerSend intent;
|
||||
if (previous != null &&
|
||||
previous.text == text &&
|
||||
_sameAttachments(previous.attachments, attachments)) {
|
||||
intent = previous;
|
||||
} else {
|
||||
// The operator changed the refused message: the old one is replaced.
|
||||
if (previous != null) discard(previous.operationId);
|
||||
final operationId = _nextOperationId();
|
||||
intent = ChatComposerSend(
|
||||
sessionId: sessionId,
|
||||
text: text,
|
||||
operationId: operationId,
|
||||
nativeMessageId: nativeChatMessageId(operationId),
|
||||
attachments: attachments,
|
||||
);
|
||||
}
|
||||
_pending = intent;
|
||||
_pendingDraft = exact;
|
||||
_draft = '';
|
||||
_edited = true;
|
||||
return _deliver(intent, clearDraft: true);
|
||||
}
|
||||
|
||||
/// Sends an unsent intent again with its own operation id, so the server and
|
||||
/// the transcript see the same message rather than a second one.
|
||||
/// the transcript see the same message rather than a second one. When its
|
||||
/// text is back in the composer, the composer is cleared as for a send.
|
||||
Future<bool> retry(String operationId) async {
|
||||
if (_disposed || _sending) return false;
|
||||
final intent = _unsentIntent(operationId);
|
||||
if (intent == null || !intent.canRetry) return false;
|
||||
if (isInComposer(operationId)) {
|
||||
_pendingDraft = _draft;
|
||||
_draft = '';
|
||||
_edited = true;
|
||||
return _deliver(intent, clearDraft: true);
|
||||
}
|
||||
if (_pending?.operationId == operationId) _pending = null;
|
||||
return _deliver(intent, clearDraft: false);
|
||||
}
|
||||
|
||||
@@ -168,6 +207,7 @@ class ChatComposerController extends ChangeNotifier {
|
||||
/// Drops an unsent intent without sending it or touching the draft.
|
||||
void discard(String operationId) {
|
||||
if (_disposed) return;
|
||||
if (_pending?.operationId == operationId) _pending = null;
|
||||
deliveries.forget(operationId);
|
||||
_forgetStored(operationId);
|
||||
}
|
||||
@@ -232,14 +272,34 @@ class ChatComposerController extends ChangeNotifier {
|
||||
if (_disposed) return false;
|
||||
_queued = reply['chat_queued'] == true;
|
||||
_forgetStored(intent.operationId);
|
||||
if (_pending?.operationId == intent.operationId) {
|
||||
_pending = null;
|
||||
_composerSendsAccepted++;
|
||||
}
|
||||
deliveries.accepted(intent, queued: _queued);
|
||||
onAccepted?.call(intent, _queued);
|
||||
_settle(null);
|
||||
return true;
|
||||
// The accepted intent's stored copy is gone before the send completes.
|
||||
await _writes;
|
||||
return !_disposed;
|
||||
} catch (error) {
|
||||
if (_disposed) return false;
|
||||
final message = '$error';
|
||||
deliveries.failed(intent);
|
||||
if (_pending?.operationId == intent.operationId && _draft.isEmpty) {
|
||||
// The composer holds the message again (text and chips), so the
|
||||
// draft store keeps it across a reload instead of a second copy.
|
||||
_draft = _pendingDraft;
|
||||
_edited = true;
|
||||
_forgetStored(intent.operationId);
|
||||
_notify();
|
||||
await _persistDraft(_pendingDraft);
|
||||
if (_disposed) return false;
|
||||
} else if (_pending?.operationId == intent.operationId) {
|
||||
// Newer typing owns the composer; the refused message stays its own
|
||||
// "Not sent" bubble (and stored) for Retry or Edit.
|
||||
_pending = null;
|
||||
}
|
||||
onRejected?.call(intent, message);
|
||||
_settle(message);
|
||||
return false;
|
||||
@@ -260,6 +320,23 @@ class ChatComposerController extends ChangeNotifier {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Whether two prompts carry the same attachment parts, so a re-send after
|
||||
/// a refusal can keep the original operation id. Parts are the exact wire
|
||||
/// maps this composer built, so a shallow key-by-key comparison is enough.
|
||||
bool _sameAttachments(List<JsonMap> a, List<JsonMap> b) {
|
||||
if (identical(a, b)) return true;
|
||||
if (a.length != b.length) return false;
|
||||
for (var index = 0; index < a.length; index += 1) {
|
||||
final first = a[index];
|
||||
final second = b[index];
|
||||
if (first.length != second.length) return false;
|
||||
for (final key in first.keys) {
|
||||
if (!second.containsKey(key) || first[key] != second[key]) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void _forgetStored(String operationId) {
|
||||
final before = _stored.length;
|
||||
_stored.removeWhere((item) => item.operationId == operationId);
|
||||
|
||||
@@ -1104,8 +1104,11 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
: null,
|
||||
// Attachment bytes cannot go back into the composer's chips, so an
|
||||
// intent with files is discarded rather than silently trimmed.
|
||||
// Its text already back in the composer needs no Edit.
|
||||
editLabel: send.attachments.isEmpty ? 'Edit' : 'Discard',
|
||||
onEdit: send.attachments.isEmpty
|
||||
onEdit: composer.isInComposer(send.operationId)
|
||||
? null
|
||||
: send.attachments.isEmpty
|
||||
? () => unawaited(composer.edit(send.operationId))
|
||||
: () => composer.discard(send.operationId),
|
||||
),
|
||||
|
||||
@@ -206,8 +206,11 @@ void main() {
|
||||
expect(find.byKey(const Key('chat-composer-error')), findsOneWidget);
|
||||
expect(find.textContaining('response lost'), findsOneWidget);
|
||||
expect(gateway.promptBodies.single['operation_id'], 'op/1');
|
||||
// The refused optimistic turn is withdrawn, not left as a fake message.
|
||||
expect(find.text('keep me'), findsNothing);
|
||||
// The refused turn says it was not sent; its text is back in the
|
||||
// composer, so it offers Retry but not Edit.
|
||||
expect(find.byKey(const Key('chat-delivery-failed')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-delivery-retry')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-delivery-edit')), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pumpAndSettle();
|
||||
@@ -218,6 +221,7 @@ void main() {
|
||||
]);
|
||||
expect(fieldText(tester), '');
|
||||
expect(find.byKey(const Key('chat-composer-error')), findsNothing);
|
||||
expect(find.byKey(const Key('chat-delivery-failed')), findsNothing);
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-composer-field')),
|
||||
|
||||
@@ -102,11 +102,14 @@ void main() {
|
||||
|
||||
await controller.send();
|
||||
expect(controller.error, contains('response lost'));
|
||||
// The composer is free for the next message; the refused one is kept.
|
||||
expect(controller.draft, '');
|
||||
// The exact draft is back in the composer, and the message is Not sent.
|
||||
expect(controller.draft, ' keep me ');
|
||||
expect(await drafts.read('s'), ' keep me ');
|
||||
expect(rejected.single, contains('response lost'));
|
||||
expect(controller.deliveries.unsent.single.operationId, 'op/1');
|
||||
expect(drafts.unsentEntries['s'], hasLength(1));
|
||||
expect(controller.isInComposer('op/1'), isTrue);
|
||||
// The draft store keeps it across a reload, so no second stored copy.
|
||||
expect(drafts.unsentEntries, isEmpty);
|
||||
|
||||
fail = false;
|
||||
expect(await controller.retry('op/1'), isTrue);
|
||||
@@ -118,9 +121,11 @@ void main() {
|
||||
'msg_op1',
|
||||
'msg_op1',
|
||||
]);
|
||||
expect(controller.draft, '');
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.deliveries.unsent, isEmpty);
|
||||
expect(drafts.unsentEntries, isEmpty);
|
||||
expect(controller.composerSendsAccepted, 1);
|
||||
|
||||
fail = true;
|
||||
await controller.setDraft('other');
|
||||
|
||||
@@ -135,8 +135,9 @@ void main() {
|
||||
await _pump(tester);
|
||||
expect(_state('failed'), findsOneWidget);
|
||||
expect(find.text('Not sent'), findsOneWidget);
|
||||
expect(find.text('Please refuse'), findsOneWidget);
|
||||
expect(drafts.unsentEntries['s'], hasLength(1));
|
||||
// The text is back in the composer, so the bubble offers Retry only.
|
||||
expect(find.byKey(const Key('chat-delivery-edit')), findsNothing);
|
||||
expect(drafts.entries['s'], 'Please refuse');
|
||||
|
||||
await tester.tap(find.byKey(const Key('chat-delivery-retry')));
|
||||
await _pump(tester);
|
||||
@@ -150,7 +151,7 @@ void main() {
|
||||
gateway.prompts.last.complete({});
|
||||
await _pump(tester);
|
||||
expect(_state('sent'), findsOneWidget);
|
||||
expect(find.text('Please refuse'), findsOneWidget);
|
||||
expect(drafts.entries, isEmpty);
|
||||
expect(drafts.unsentEntries, isEmpty);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user