import 'dart:async'; import 'dart:collection'; import 'dart:convert'; import 'dart:typed_data'; import '../../../core/api_gateway.dart'; import '../../../core/transport/connection_state.dart'; import '../domain/chat_attachment_image.dart'; /// Why a transcript image cannot be drawn, in words the placeholder shows. class ChatAttachmentImageException implements Exception { const ChatAttachmentImageException(this.message); final String message; @override String toString() => message; } /// The placeholder's wording when the server no longer has the file. const String chatImageGoneMessage = 'Image no longer available'; /// Loads and caches the bytes of images attached to sent chat messages, keyed /// by the part's URI. A transcript row asks lazily when it builds; scrolling /// back reuses the bytes instead of fetching again. Decoded bytes are bounded /// by [maxCachedBytes], least recently used first out. class ChatAttachmentImages { ChatAttachmentImages(this.gateway, {this.maxCachedBytes = 48 << 20}); /// Null for a host with no server (only `data:` images draw). final ApiGateway? gateway; final int maxCachedBytes; final LinkedHashMap _ready = LinkedHashMap(); final Map> _pending = {}; final Map _failed = {}; final Map _uploads = {}; int _cachedBytes = 0; /// Bytes already loaded for [uri], so a rebuilt row draws without a frame /// of loading state. Uint8List? ready(String uri) { final bytes = _ready.remove(uri); if (bytes != null) _ready[uri] = bytes; return bytes; } /// The settled reason [uri] cannot be drawn, if a previous load failed. String? failure(String uri) => _failed[uri]; /// Records the composer's own bytes for a file it uploaded ahead of a /// prompt, so the sent turn draws them before and after the server echo. void rememberUpload(String storedName, Uint8List bytes) { if (storedName.isEmpty || bytes.isEmpty) return; _uploads[storedName] = bytes; while (_uploads.length > 16) { _uploads.remove(_uploads.keys.first); } } /// The image bytes behind [uri]. A server refusal is remembered; a transport /// failure is not, so a later rebuild after reconnecting tries again. Future load(String uri) { final bytes = ready(uri); if (bytes != null) return Future.value(bytes); final failed = _failed[uri]; if (failed != null) { return Future.error(ChatAttachmentImageException(failed)); } return _pending[uri] ??= _fetch(uri).then( (bytes) { _pending.remove(uri); _store(uri, bytes); return bytes; }, onError: (Object error) { _pending.remove(uri); if (error is ChatAttachmentImageException) { _failed[uri] = error.message; throw error; } // Not connected, timed out: worth another try on a later build. throw const ChatAttachmentImageException('Image unavailable'); }, ); } Future _fetch(String uri) async { final local = _uploads[chatStoredFileName(uri)]; switch (chatImageSourceOf(uri)) { case ChatImageSource.inline: final bytes = chatDataUriBytes(uri); if (bytes == null || bytes.isEmpty) { throw const ChatAttachmentImageException('Image could not be read'); } return bytes; case ChatImageSource.upload: if (local == null) { throw const ChatAttachmentImageException(chatImageGoneMessage); } return local; case ChatImageSource.stored: if (local != null) return local; return _request(uri); case null: throw const ChatAttachmentImageException('Image unavailable'); } } Future _request(String uri) async { final gateway = this.gateway; if (gateway == null) { throw const ChatAttachmentImageException('Image unavailable'); } final JsonMap reply; try { reply = await gateway.call('chat_attachment_image', { 'chat_attachment_image_request': {'uri': uri}, }); } on ProtocolError catch (error) { // The server's answer is settled; remember it rather than re-ask. throw ChatAttachmentImageException(_wording(error.message)); } final content = reply['chat_attachment_image']; final data = content is Map ? content['data'] : null; if (data is! String || data.isEmpty) { throw const ChatAttachmentImageException('Image could not be read'); } try { return base64Decode(data); } on FormatException { throw const ChatAttachmentImageException('Image could not be read'); } } void _store(String uri, Uint8List bytes) { _ready[uri] = bytes; _cachedBytes += bytes.length; while (_cachedBytes > maxCachedBytes && _ready.length > 1) { final oldest = _ready.keys.first; _cachedBytes -= _ready.remove(oldest)!.length; } } static String _wording(String message) { if (message.contains('no longer available')) return chatImageGoneMessage; if (message.contains('too large')) return 'Image too large to preview'; return 'Image unavailable'; } }