WIP: message delivery status and refresh safety; fix reconcile listener recursion
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.
This commit is contained in:
@@ -9,7 +9,6 @@ import '../../../shell/shell_bar.dart';
|
||||
import '../data/chat_attachment_images.dart';
|
||||
import '../data/chat_repository.dart';
|
||||
import '../domain/chat_activity_detail.dart';
|
||||
import '../domain/chat_attachment_image.dart';
|
||||
import '../domain/chat_notice.dart';
|
||||
import '../domain/chat_attachment_picker.dart';
|
||||
import '../domain/chat_transcript.dart';
|
||||
@@ -21,6 +20,7 @@ import 'chat_background_tasks.dart';
|
||||
import 'chat_command_actions.dart';
|
||||
import 'chat_composer_bar.dart';
|
||||
import 'chat_composer_controller.dart';
|
||||
import 'chat_delivery_indicator.dart';
|
||||
import 'chat_draft_store.dart';
|
||||
import 'chat_mode_control.dart';
|
||||
import 'chat_model_control.dart';
|
||||
@@ -203,11 +203,33 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
/// counted against it for the jump control's arrival count.
|
||||
String? _newestAtRelease;
|
||||
|
||||
ChatThreadController _attach() => ChatThreadController(
|
||||
widget.gateway,
|
||||
widget.sessionId,
|
||||
cached: widget.transcripts?.read(widget.sessionId),
|
||||
)..load();
|
||||
ChatThreadController _attach() {
|
||||
final controller = ChatThreadController(
|
||||
widget.gateway,
|
||||
widget.sessionId,
|
||||
cached: widget.transcripts?.read(widget.sessionId),
|
||||
);
|
||||
// The listener names this controller: load() can notify before _attach
|
||||
// returns, while reading _controller would build another one, and so on.
|
||||
controller.transcriptChanges.addListener(
|
||||
() => _reconcileUnsent(controller),
|
||||
);
|
||||
unawaited(controller.load());
|
||||
return controller;
|
||||
}
|
||||
|
||||
/// Settles stored unsent messages once the transcript is an authoritative
|
||||
/// read: a message the server recorded was sent after all.
|
||||
void _reconcileUnsent(ChatThreadController controller) {
|
||||
if (!mounted ||
|
||||
!controller.loaded ||
|
||||
controller.loading ||
|
||||
controller.error != null ||
|
||||
controller.showingCached) {
|
||||
return;
|
||||
}
|
||||
_composer.reconcile(controller.messages);
|
||||
}
|
||||
|
||||
/// `/close` finished this conversation; its transcript is not kept.
|
||||
bool _closed = false;
|
||||
@@ -238,38 +260,39 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
widget.onSessionClosed?.call();
|
||||
}
|
||||
|
||||
ChatComposerController _attachComposer() => ChatComposerController(
|
||||
widget.gateway,
|
||||
widget.sessionId,
|
||||
drafts: widget.drafts,
|
||||
newOperationId: widget.newOperationId,
|
||||
onOptimistic: _onOptimistic,
|
||||
onRejected: _onRejected,
|
||||
onAccepted: (send, queued) =>
|
||||
_controller.promptAccepted(send.operationId, queued: queued),
|
||||
);
|
||||
|
||||
void _onOptimistic(ChatComposerSend send) {
|
||||
if (!mounted) return;
|
||||
_controller.insertLocal({
|
||||
'id': send.optimisticId,
|
||||
'session_id': send.sessionId,
|
||||
'role': 'user',
|
||||
'operation_id': send.operationId,
|
||||
'native_message_id': send.nativeMessageId,
|
||||
'parts': [
|
||||
{'type': 'text', 'text': send.text},
|
||||
for (final attachment in send.attachments)
|
||||
{
|
||||
'type': 'file',
|
||||
'name': attachment['name'],
|
||||
'uri': chatOptimisticAttachmentUri(attachment),
|
||||
'mime': attachment['mime'],
|
||||
},
|
||||
],
|
||||
});
|
||||
ChatComposerController _attachComposer() {
|
||||
final composer = ChatComposerController(
|
||||
widget.gateway,
|
||||
widget.sessionId,
|
||||
drafts: widget.drafts,
|
||||
newOperationId: widget.newOperationId,
|
||||
onOptimistic: _onOptimistic,
|
||||
onRejected: _onRejected,
|
||||
onAccepted: (send, queued) =>
|
||||
_controller.promptAccepted(send.operationId, queued: queued),
|
||||
);
|
||||
// A message a previous visit sent but never saw accepted comes back as
|
||||
// "Not sent" unless the history read shows the server has it.
|
||||
unawaited(
|
||||
composer.loadUnsent().then((_) {
|
||||
if (mounted && identical(composer, _composer)) {
|
||||
_reconcileUnsent(_controller);
|
||||
}
|
||||
}),
|
||||
);
|
||||
return composer;
|
||||
}
|
||||
|
||||
/// Draws a sent (or retried) message where it was sent: a retry moves the
|
||||
/// message to the end, since that is when it is sent.
|
||||
void _onOptimistic(ChatComposerSend send) {
|
||||
if (!mounted) return;
|
||||
_controller.removeLocal(send.operationId);
|
||||
_controller.insertLocal(send.toMessage());
|
||||
}
|
||||
|
||||
/// A refused message leaves the transcript; it is drawn after it as "Not
|
||||
/// sent" from the composer's deliveries instead.
|
||||
void _onRejected(ChatComposerSend send, String error) {
|
||||
if (!mounted) return;
|
||||
_controller.removeLocal(send.operationId);
|
||||
@@ -643,7 +666,10 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
animation: _controller.transcriptChanges,
|
||||
animation: Listenable.merge([
|
||||
_controller.transcriptChanges,
|
||||
_composer.deliveries,
|
||||
]),
|
||||
builder: (context, _) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
@@ -845,6 +871,18 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
: null,
|
||||
),
|
||||
),
|
||||
// Messages the server has not accepted follow the
|
||||
// transcript until they are retried or edited.
|
||||
for (final send in _composer.deliveries.unsent)
|
||||
Padding(
|
||||
key: ValueKey<String>(
|
||||
'chat-unsent-${send.operationId}',
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
),
|
||||
child: _unsentRow(send, cupertino),
|
||||
),
|
||||
// The conversation's own command lines sit after the
|
||||
// turns, as on the web; they are never bubbles.
|
||||
for (final notice in _commandNotices)
|
||||
@@ -1024,13 +1062,56 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
child: chips.firstOrNull ?? const SizedBox.shrink(),
|
||||
);
|
||||
}
|
||||
final delivery = kind == 'user' && row['message'] is Map
|
||||
? _composer.deliveries.stateFor(
|
||||
Map<String, dynamic>.from(row['message'] as Map),
|
||||
)
|
||||
: null;
|
||||
return ChatMessageView(
|
||||
row: row,
|
||||
textBuilder: chatMarkdownText,
|
||||
footer: chips,
|
||||
footer: [
|
||||
...chips,
|
||||
if (delivery != null)
|
||||
ChatDeliveryIndicator(
|
||||
state: delivery,
|
||||
cupertino:
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino,
|
||||
),
|
||||
],
|
||||
images: _images,
|
||||
);
|
||||
}
|
||||
|
||||
/// One message the server has not accepted, as the operator's bubble with
|
||||
/// "Not sent" and its Retry and Edit.
|
||||
Widget _unsentRow(ChatComposerSend send, bool cupertino) {
|
||||
final composer = _composer;
|
||||
return ChatMessageView(
|
||||
row: {
|
||||
'kind': 'user',
|
||||
'key': send.optimisticId,
|
||||
'message': send.toMessage(),
|
||||
},
|
||||
textBuilder: chatMarkdownText,
|
||||
images: _images,
|
||||
footer: [
|
||||
ChatDeliveryIndicator(
|
||||
state: ChatDelivery.failed,
|
||||
cupertino: cupertino,
|
||||
onRetry: send.canRetry && !composer.sending
|
||||
? () => unawaited(composer.retry(send.operationId))
|
||||
: null,
|
||||
// Attachment bytes cannot go back into the composer's chips, so an
|
||||
// intent with files is discarded rather than silently trimmed.
|
||||
editLabel: send.attachments.isEmpty ? 'Edit' : 'Discard',
|
||||
onEdit: send.attachments.isEmpty
|
||||
? () => unawaited(composer.edit(send.operationId))
|
||||
: () => composer.discard(send.operationId),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The native dismiss control shared by a local notice and a running
|
||||
|
||||
Reference in New Issue
Block a user