Files
pipelines/app/lib/features/chat/conversation/chat_composer_send.dart
T
alan 53d8b911a6 Schedule a message for when the usage limit resets
When the open chat's account has an exhausted quota window, a banner above
the composer reads "Usage limit reached · resets at HH:MM" and offers to
send the last message (or the current draft) a minute after the reset, via
chat_prompt send_at. Queued prompts with a future send_at read
"Scheduled · sends at HH:MM" and keep Remove.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 00:59:33 +00:00

124 lines
4.4 KiB
Dart

import '../../../core/api_gateway.dart';
import '../domain/chat_attachment_image.dart';
/// One plain-message send: the optimistic identity the thread shows and the
/// exact wire intent. A retry sends this same intent with these ids.
class ChatComposerSend {
const ChatComposerSend({
required this.sessionId,
required this.text,
required this.operationId,
required this.nativeMessageId,
this.attachments = const [],
this.attachmentsOmitted = false,
this.sendAt = 0,
});
final String sessionId;
/// The trimmed wire text.
final String text;
final String operationId;
final String nativeMessageId;
/// The `{name, mime, data}` parts already built from the composer's chips,
/// in draft order. Empty for a text-only prompt.
final List<JsonMap> attachments;
/// True for an intent read back from device storage whose attachment bytes
/// were too large to keep: [attachments] then carries names only, so the
/// intent cannot be re-sent as it was.
final bool attachmentsOmitted;
/// Epoch milliseconds the server holds this prompt until (`send_at`), or 0
/// for one sent as soon as the harness is free.
final int sendAt;
/// Whether this exact intent can be sent again.
bool get canRetry => !attachmentsOmitted;
/// Mirrors the web's `nativeChatMessageId(operationId) || local-<operationId>`.
String get optimisticId =>
nativeMessageId.isEmpty ? 'local-$operationId' : nativeMessageId;
/// The local user message the thread draws for this intent.
JsonMap toMessage() => {
'id': optimisticId,
'session_id': sessionId,
'role': 'user',
'operation_id': operationId,
'native_message_id': nativeMessageId,
if (sendAt > 0) 'send_at': sendAt,
'parts': [
{'type': 'text', 'text': text},
for (final attachment in attachments)
{
'type': 'file',
'name': attachment['name'],
'uri': chatOptimisticAttachmentUri(attachment),
'mime': attachment['mime'],
},
],
};
/// Whether [message] is the server's record of this intent: the same
/// operation id, or the native message id the prompt carried.
bool recordedBy(JsonMap message) {
if (message['role'] != 'user') return false;
if (message['operation_id'] == operationId) return true;
if (nativeMessageId.isEmpty) return false;
return message['id'] == nativeMessageId ||
message['native_message_id'] == nativeMessageId;
}
/// The device-storage form. Attachment bytes are kept only while their
/// total stays under [maxStoredAttachmentData]; beyond that only names and
/// types are kept and the read-back intent reports [attachmentsOmitted].
JsonMap toJson() {
var size = 0;
for (final attachment in attachments) {
size += '${attachment['data'] ?? ''}'.length;
}
final omit = attachmentsOmitted || size > maxStoredAttachmentData;
return {
'session_id': sessionId,
'text': text,
'operation_id': operationId,
'native_message_id': nativeMessageId,
'attachments': [
for (final attachment in attachments)
omit
? {'name': attachment['name'], 'mime': attachment['mime']}
: Map<String, dynamic>.from(attachment),
],
if (omit && attachments.isNotEmpty) 'attachments_omitted': true,
if (sendAt > 0) 'send_at': sendAt,
};
}
/// Reads a stored intent, or null when the entry is not a usable intent.
static ChatComposerSend? fromJson(Object? value) {
if (value is! Map) return null;
final text = '${value['text'] ?? ''}';
final operationId = '${value['operation_id'] ?? ''}';
if (text.trim().isEmpty || operationId.isEmpty) return null;
return ChatComposerSend(
sessionId: '${value['session_id'] ?? ''}',
text: text,
operationId: operationId,
nativeMessageId: '${value['native_message_id'] ?? ''}',
attachments: [
for (final attachment in value['attachments'] as List? ?? const [])
if (attachment is Map) Map<String, dynamic>.from(attachment),
],
attachmentsOmitted: value['attachments_omitted'] == true,
sendAt: value['send_at'] is num ? (value['send_at'] as num).toInt() : 0,
);
}
/// Base64 characters of attachment data kept on the device with an unsent
/// intent. Browser storage is small and shared, so larger files are named
/// but not kept.
static const int maxStoredAttachmentData = 512 * 1024;
}