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, }); 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 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; /// Whether this exact intent can be sent again. bool get canRetry => !attachmentsOmitted; /// Mirrors the web's `nativeChatMessageId(operationId) || local-`. 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, '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.from(attachment), ], if (omit && attachments.isNotEmpty) 'attachments_omitted': true, }; } /// 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.from(attachment), ], attachmentsOmitted: value['attachments_omitted'] == true, ); } /// 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; }