Show server-queued chat prompts from the server, with Remove

The thread reads the session's queued prompts from the chat_subscribe reply
and chat_queue frames: a reload draws them as Queued after the transcript,
a delivered one enters the transcript as Sent, a refused one becomes Not
sent, and Remove sends chat_unqueue. A prompt the server reports queued is
dropped from the device unsent store; the any-frame queued heuristic is
used only with a server that does not report its queue.
This commit is contained in:
2026-11-01 18:42:10 +00:00
parent e42f9cc031
commit 70cdf985c5
9 changed files with 565 additions and 17 deletions
@@ -6,6 +6,7 @@ import '../../../core/api_gateway.dart';
import '../data/chat_repository.dart';
import '../domain/chat_sessions.dart';
import '../domain/chat_transcript.dart';
import 'chat_server_queue.dart';
import 'chat_thread_cards.dart';
import 'chat_transcript_cache.dart';
@@ -56,6 +57,11 @@ class ChatThreadController extends ChangeNotifier {
);
StreamSubscription<JsonMap>? _subscription;
final _transcriptChanges = ChangeNotifier();
final ChatServerQueue _queue = ChatServerQueue();
/// Told about every queue body the server sends (the subscribe reply's and
/// each `chat_queue` frame), so delivery states follow the server.
void Function(JsonMap queue)? onQueue;
List<JsonMap> _messages = const [];
bool _running = false;
@@ -100,6 +106,31 @@ class ChatThreadController extends ChangeNotifier {
List<JsonMap> get messages => _messages;
/// The prompts the server still holds for this session, in delivery order.
List<JsonMap> get queuedPrompts => _queue.prompts;
/// The queued prompts not already drawn in the transcript (this device's
/// own optimistic bubble and another device's echo are drawn in place).
/// They follow the transcript, since none has reached the harness yet.
List<JsonMap> get queuedOutsideTranscript => [
for (final prompt in _queue.prompts)
if (!_messages.any(
(message) => message['operation_id'] == prompt['operation_id'],
))
prompt,
];
/// Whether [operationId] is still waiting in the server's queue.
bool isQueued(String operationId) => _queue.holds(operationId);
/// Removes one queued prompt on the server. A refusal (it was already sent)
/// is thrown to the caller; the queue itself only changes from the reply.
Future<void> unqueue(String operationId) async {
final reply = await _repository.unqueue(sessionId, operationId);
if (_disposed) return;
_applyQueue(reply['chat_queue']);
}
/// Usage-only snapshots refresh Info, not the expensive transcript tree.
Listenable get transcriptChanges => _transcriptChanges;
bool get running => _running;
@@ -260,10 +291,11 @@ class ChatThreadController extends ChangeNotifier {
Future<void> _attach({bool force = false}) async {
if (_disposed || (_subscribed && !force)) return;
try {
await _repository.subscribe(sessionId);
final reply = await _repository.subscribe(sessionId);
if (_disposed) return;
_subscribed = true;
_subscriptionError = null;
_applyQueue(reply['chat_queue']);
} catch (error) {
// History stays usable, but the UI must report that live updates failed.
if (_disposed) return;
@@ -272,8 +304,32 @@ class ChatThreadController extends ChangeNotifier {
}
}
/// Adopts the server's queue. A prompt the server removed or could not
/// deliver leaves the transcript: it never reached the harness.
void _applyQueue(Object? body) {
if (!_queue.apply(sessionId, body)) return;
final frame = Map<String, dynamic>.from(body as Map);
for (final key in const ['removed', 'failed']) {
final operation = '${frame[key] ?? ''}';
if (operation.isEmpty) continue;
_messages = [
for (final message in _messages)
if (message['operation_id'] != operation) message,
];
if (_awaitingResponseOperation == operation) {
_awaitingResponseOperation = null;
}
}
onQueue?.call(frame);
_notify();
}
void _onFrame(JsonMap envelope) {
if (_disposed) return;
if (envelope['type'] == 'chat_queue') {
_applyQueue(envelope['chat_queue']);
return;
}
if (envelope['type'] == 'chat_permission') {
final permission = envelope['chat_permission'];
if (permission is! Map) return;