4bc99f490b
The transcript listener read the lazily built _controller while _attach was still building it (load() notifies synchronously), so it built another controller, recursively, until the test process ran out of memory.
163 lines
5.4 KiB
Dart
163 lines
5.4 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'chat_composer_send.dart';
|
|
|
|
/// Where one of the operator's own messages is on its way to the server.
|
|
enum ChatDelivery {
|
|
/// The prompt request is awaiting the server's reply.
|
|
sending,
|
|
|
|
/// The server accepted the prompt.
|
|
sent,
|
|
|
|
/// The server accepted the prompt behind the turn in flight.
|
|
queued,
|
|
|
|
/// The server refused the prompt, or its request was lost.
|
|
failed,
|
|
}
|
|
|
|
/// The delivery state of this conversation's own sends, and the messages that
|
|
/// were not sent.
|
|
///
|
|
/// Messages read from history carry no state: they were sent. Only sends made
|
|
/// from this composer (and intents read back from device storage) are
|
|
/// tracked. An unsent intent is not a transcript message, so it is listed in
|
|
/// [unsent] and drawn after the transcript until it is re-sent, edited or
|
|
/// found in the server's history.
|
|
class ChatDeliveries extends ChangeNotifier {
|
|
final Map<String, ChatDelivery> _states = {};
|
|
final Map<String, String> _operationByNative = {};
|
|
final List<ChatComposerSend> _unsent = [];
|
|
|
|
/// Intents read back from device storage that no history read has checked.
|
|
final List<ChatComposerSend> _unverified = [];
|
|
String? _latestSent;
|
|
bool _disposed = false;
|
|
|
|
/// The intents the server has not accepted, in send order.
|
|
List<ChatComposerSend> get unsent => List.unmodifiable(_unsent);
|
|
|
|
/// Whether any intent still waits for a history read to settle it.
|
|
bool get hasUnverified => _unverified.isNotEmpty;
|
|
|
|
/// The state to draw under a transcript user [message], or null for a quiet
|
|
/// message. "Sent" is shown only on the newest accepted send.
|
|
ChatDelivery? stateFor(JsonMap message) {
|
|
final operation = _operationOf(message);
|
|
if (operation == null) return null;
|
|
final state = _states[operation];
|
|
if (state == ChatDelivery.sent && operation != _latestSent) return null;
|
|
return state;
|
|
}
|
|
|
|
String? _operationOf(JsonMap message) {
|
|
final operation = message['operation_id'];
|
|
if (operation is String && _states.containsKey(operation)) {
|
|
return operation;
|
|
}
|
|
for (final key in [message['native_message_id'], message['id']]) {
|
|
if (key is String && _operationByNative.containsKey(key)) {
|
|
return _operationByNative[key];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void sending(ChatComposerSend send) {
|
|
_unsent.removeWhere((item) => item.operationId == send.operationId);
|
|
_set(send, ChatDelivery.sending);
|
|
}
|
|
|
|
void accepted(ChatComposerSend send, {required bool queued}) {
|
|
if (!queued) _latestSent = send.operationId;
|
|
_set(send, queued ? ChatDelivery.queued : ChatDelivery.sent);
|
|
}
|
|
|
|
void failed(ChatComposerSend send) {
|
|
_unsent
|
|
..removeWhere((item) => item.operationId == send.operationId)
|
|
..add(send);
|
|
_set(send, ChatDelivery.failed);
|
|
}
|
|
|
|
/// A queued prompt was admitted: the turn it waited behind moved on.
|
|
void admitQueued() {
|
|
var changed = false;
|
|
for (final entry in _states.entries.toList()) {
|
|
if (entry.value != ChatDelivery.queued) continue;
|
|
_states[entry.key] = ChatDelivery.sent;
|
|
_latestSent = entry.key;
|
|
changed = true;
|
|
}
|
|
if (changed) _notify();
|
|
}
|
|
|
|
/// Drops an unsent intent the operator took back into the composer.
|
|
void forget(String operationId) {
|
|
final before = _unsent.length + _unverified.length;
|
|
_unsent.removeWhere((item) => item.operationId == operationId);
|
|
_unverified.removeWhere((item) => item.operationId == operationId);
|
|
_states.remove(operationId);
|
|
if (_unsent.length + _unverified.length != before) _notify();
|
|
}
|
|
|
|
/// Adopts intents read back from device storage. They stay hidden until
|
|
/// [reconcile] has checked them against an authoritative history read.
|
|
void restore(List<ChatComposerSend> intents) {
|
|
var changed = false;
|
|
for (final intent in intents) {
|
|
if (_states.containsKey(intent.operationId)) continue;
|
|
_remember(intent);
|
|
_states[intent.operationId] = ChatDelivery.failed;
|
|
_unverified.add(intent);
|
|
changed = true;
|
|
}
|
|
if (changed) _notify();
|
|
}
|
|
|
|
/// Settles stored and unsent intents against an authoritative transcript:
|
|
/// one the server recorded was sent and is returned so its stored copy can
|
|
/// be cleared; an unchecked one that is absent becomes "Not sent".
|
|
List<ChatComposerSend> reconcile(List<JsonMap> messages) {
|
|
bool recorded(ChatComposerSend intent) =>
|
|
messages.any((message) => intent.recordedBy(message));
|
|
final confirmed = <ChatComposerSend>[
|
|
for (final intent in [..._unverified, ..._unsent])
|
|
if (recorded(intent)) intent,
|
|
];
|
|
final changed = confirmed.isNotEmpty || _unverified.isNotEmpty;
|
|
for (final intent in confirmed) {
|
|
_states[intent.operationId] = ChatDelivery.sent;
|
|
}
|
|
_unsent.removeWhere(confirmed.contains);
|
|
_unverified.removeWhere(confirmed.contains);
|
|
_unsent.addAll(_unverified);
|
|
_unverified.clear();
|
|
if (changed) _notify();
|
|
return confirmed;
|
|
}
|
|
|
|
void _remember(ChatComposerSend send) {
|
|
if (send.nativeMessageId.isEmpty) return;
|
|
_operationByNative[send.nativeMessageId] = send.operationId;
|
|
}
|
|
|
|
void _set(ChatComposerSend send, ChatDelivery state) {
|
|
_remember(send);
|
|
_states[send.operationId] = state;
|
|
_notify();
|
|
}
|
|
|
|
void _notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
}
|