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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user