70cdf985c5
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.
56 lines
2.1 KiB
Dart
56 lines
2.1 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
|
|
/// The prompts the server holds for one session until its harness finishes
|
|
/// the turn in flight, in delivery order.
|
|
///
|
|
/// The server owns them: they survive this device reloading, closing or
|
|
/// losing its connection, so a reloaded thread reads them from the
|
|
/// `chat_subscribe` reply and every change from `chat_queue` frames instead of
|
|
/// from device storage.
|
|
class ChatServerQueue {
|
|
List<JsonMap> _prompts = const [];
|
|
|
|
/// The queued prompts, each `{operation_id, message_id, native_message_id,
|
|
/// text, client, attachments}` as the server sent it.
|
|
List<JsonMap> get prompts => _prompts;
|
|
|
|
bool get isEmpty => _prompts.isEmpty;
|
|
|
|
/// Whether [operationId] is still waiting on the server.
|
|
bool holds(String operationId) =>
|
|
_prompts.any((prompt) => prompt['operation_id'] == operationId);
|
|
|
|
/// Adopts one queue body (a reply's or a frame's `chat_queue`) for
|
|
/// [sessionId]. Returns false for another session's or an unusable body.
|
|
bool apply(String sessionId, Object? body) {
|
|
if (body is! Map || '${body['session_id'] ?? ''}' != sessionId) {
|
|
return false;
|
|
}
|
|
_prompts = [
|
|
for (final prompt in body['prompts'] as List? ?? const [])
|
|
if (prompt is Map && '${prompt['operation_id'] ?? ''}'.isNotEmpty)
|
|
Map<String, dynamic>.from(prompt),
|
|
];
|
|
return true;
|
|
}
|
|
|
|
/// The user message a queued [prompt] draws as, shaped like a transcript
|
|
/// message so the same bubble renders it.
|
|
static JsonMap message(JsonMap prompt) {
|
|
final operation = '${prompt['operation_id'] ?? ''}';
|
|
final id = '${prompt['message_id'] ?? ''}';
|
|
return {
|
|
'id': id.isEmpty ? 'queued-$operation' : id,
|
|
'role': 'user',
|
|
'operation_id': operation,
|
|
'native_message_id': prompt['native_message_id'] ?? '',
|
|
'client': prompt['client'],
|
|
'parts': [
|
|
{'type': 'text', 'text': '${prompt['text'] ?? ''}'},
|
|
for (final attachment in prompt['attachments'] as List? ?? const [])
|
|
if (attachment is Map) Map<String, dynamic>.from(attachment),
|
|
],
|
|
};
|
|
}
|
|
}
|