diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 2491196e..02096aea 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -9,6 +9,7 @@ import 'package:flutter/services.dart'; import '../../../core/api_gateway.dart'; import '../../../core/transport/pipelines_client.dart'; +import '../data/chat_attachment_images.dart'; import '../data/upload/chat_upload.dart'; import '../domain/chat_attachment_draft.dart'; import '../domain/chat_attachment_picker.dart'; @@ -94,8 +95,13 @@ class ChatComposerBar extends StatefulWidget { this.backgroundTasks, this.placeholder = chatComposerPlaceholder, this.enterSends, + this.images, }); + /// The transcript's image cache, told about uploaded files so the sent turn + /// shows them without a round trip. + final ChatAttachmentImages? images; + /// Overrides [chatEnterSends] for this composer; null follows the host. final bool? enterSends; @@ -472,8 +478,18 @@ class _ChatComposerBarState extends State { parts = await chatAttachmentUploadParts( _attachments, read: _readAttachment, - upload: (attachment, bytes) async => _uploaded[attachment] ??= - await uploadChatAttachment(endpoint, attachment, bytes), + upload: (attachment, bytes) async { + final stored = _uploaded[attachment] ??= await uploadChatAttachment( + endpoint, + attachment, + bytes, + ); + // The sent turn draws these bytes instead of fetching them back. + if (attachment.mime.toLowerCase().startsWith('image/')) { + widget.images?.rememberUpload(stored, bytes); + } + return stored; + }, ); } } on ChatAttachmentWireException catch (error) { diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 5ffdac45..72cc12df 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -6,8 +6,10 @@ import 'package:flutter/rendering.dart'; import '../../../core/api_gateway.dart'; import '../../../shell/shell_bar.dart'; +import '../data/chat_attachment_images.dart'; import '../data/chat_repository.dart'; import '../domain/chat_activity_detail.dart'; +import '../domain/chat_attachment_image.dart'; import '../domain/chat_notice.dart'; import '../domain/chat_attachment_picker.dart'; import '../domain/chat_transcript.dart'; @@ -129,6 +131,9 @@ class _ChatThreadViewState extends State { late ChatComposerController _composer = _attachComposer(); late ChatRowKeys _rowKeys = ChatRowKeys(); + /// Sent images' bytes for this server, cached across rebuilds and scrolling. + late ChatAttachmentImages _images = ChatAttachmentImages(widget.gateway); + /// Local notices the operator hid; cleared when the conversation or socket /// changes so a reconnect never keeps a stale dismissal. ChatNoticeDismissals _dismissals = ChatNoticeDismissals(); @@ -253,7 +258,7 @@ class _ChatThreadViewState extends State { { 'type': 'file', 'name': attachment['name'], - 'uri': attachment['name'], + 'uri': chatOptimisticAttachmentUri(attachment), 'mime': attachment['mime'], }, ], @@ -352,6 +357,8 @@ class _ChatThreadViewState extends State { // A transcript from another server is not this one's to keep. if (oldWidget.gateway == widget.gateway) { _saveTranscript(oldWidget.transcripts, oldWidget.sessionId); + } else { + _images = ChatAttachmentImages(widget.gateway); } _controller.dispose(); _composer.dispose(); @@ -901,6 +908,7 @@ class _ChatThreadViewState extends State { running: _controller.running, commands: _commandActions(), picker: widget.attachmentPicker, + images: _images, placeholder: chatComposerPlaceholderFor( '${widget.session?['directory'] ?? ''}', ), @@ -1026,6 +1034,7 @@ class _ChatThreadViewState extends State { row: row, textBuilder: chatMarkdownText, footer: chips, + images: _images, ); } diff --git a/app/lib/features/chat/data/chat_attachment_images.dart b/app/lib/features/chat/data/chat_attachment_images.dart new file mode 100644 index 00000000..1b52eab5 --- /dev/null +++ b/app/lib/features/chat/data/chat_attachment_images.dart @@ -0,0 +1,150 @@ +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'; + } +} diff --git a/app/lib/features/chat/domain/chat_attachment_image.dart b/app/lib/features/chat/domain/chat_attachment_image.dart new file mode 100644 index 00000000..98116929 --- /dev/null +++ b/app/lib/features/chat/domain/chat_attachment_image.dart @@ -0,0 +1,107 @@ +import 'dart:typed_data'; + +import '../../../core/api_gateway.dart'; + +/// Raster types the transcript can draw. SVG and unknown types stay file rows. +const Set _imageMimes = { + 'image/png', + 'image/jpeg', + 'image/jpg', + 'image/gif', + 'image/webp', + 'image/bmp', +}; + +const Map _imageExtensions = { + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', + 'webp': 'image/webp', + 'bmp': 'image/bmp', +}; + +/// Where a transcript image's bytes come from. +enum ChatImageSource { + /// A `data:` URI the history already carries (Claude Code transcripts). + inline, + + /// A server-local `file://` path read through `chat_attachment_image`. + stored, + + /// A just-sent prompt's upload, known only by its stored name until the + /// server echoes the recorded turn. + upload, +} + +/// The image type a file part names: its own MIME, else the type of a `data:` +/// URI, else its name or URI extension. Null when it is not a drawable image. +String? chatPartImageMime(JsonMap part) { + if (part['type'] != 'file') return null; + final mime = '${part['mime'] ?? ''}'.trim().toLowerCase(); + if (mime.isNotEmpty && mime != 'application/octet-stream') { + return _imageMimes.contains(mime) ? mime : null; + } + final uri = '${part['uri'] ?? ''}'.trim(); + if (uri.startsWith('data:')) { + final type = uri.substring(5).split(RegExp('[;,]')).first.toLowerCase(); + return _imageMimes.contains(type) ? type : null; + } + for (final candidate in ['${part['name'] ?? ''}', uri]) { + final dot = candidate.lastIndexOf('.'); + if (dot < 0) continue; + final type = _imageExtensions[candidate.substring(dot + 1).toLowerCase()]; + if (type != null) return type; + } + return null; +} + +/// How a drawable image part is loaded, or null when its URI is one the client +/// does not fetch (remote http(s) URLs stay plain file rows). +ChatImageSource? chatImageSourceOf(String uri) { + if (uri.startsWith('data:')) return ChatImageSource.inline; + if (uri.startsWith('file:///')) return ChatImageSource.stored; + if (uri.startsWith(chatUploadUriPrefix)) return ChatImageSource.upload; + return null; +} + +/// The optimistic row's URI for a file the composer uploaded ahead of the +/// prompt; the loader resolves it from the composer's own bytes. +const String chatUploadUriPrefix = 'upload:'; + +/// The URI a just-sent turn's file part carries before the server records it: +/// an inline file's own `data:` URI, an uploaded file's `upload:` name, else +/// its name (drawn as a plain file row). +String chatOptimisticAttachmentUri(JsonMap wirePart) { + final name = '${wirePart['name'] ?? ''}'; + final data = '${wirePart['data'] ?? ''}'; + final upload = '${wirePart['upload'] ?? ''}'; + if (upload.isNotEmpty) return '$chatUploadUriPrefix$upload'; + if (data.isNotEmpty) { + final mime = '${wirePart['mime'] ?? ''}'.trim(); + return 'data:${mime.isEmpty ? 'application/octet-stream' : mime};base64,$data'; + } + return name; +} + +/// The stored file name a `file://` or `upload:` URI ends in; the upload store +/// names files `up--`, so the same bytes match before and after the +/// server records the turn. +String chatStoredFileName(String uri) { + if (uri.startsWith(chatUploadUriPrefix)) { + return uri.substring(chatUploadUriPrefix.length); + } + final path = Uri.tryParse(uri)?.pathSegments ?? const []; + return path.isEmpty ? '' : path.last; +} + +/// The bytes of a base64 `data:` image URI, or null when it is malformed. +Uint8List? chatDataUriBytes(String uri) { + try { + final data = UriData.parse(uri); + if (!data.isBase64) return null; + return data.contentAsBytes(); + } on FormatException { + return null; + } +} diff --git a/app/lib/features/chat/ui/transcript/chat_attachment_gallery.dart b/app/lib/features/chat/ui/transcript/chat_attachment_gallery.dart new file mode 100644 index 00000000..d13729f1 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_attachment_gallery.dart @@ -0,0 +1,57 @@ +import 'package:flutter/widgets.dart'; + +import '../../../../core/api_gateway.dart'; +import '../../data/chat_attachment_images.dart'; +import '../../domain/chat_attachment_image.dart'; +import 'chat_attachment_thumbnail.dart'; + +/// Whether a file part draws as a thumbnail: a raster image whose bytes the +/// client can reach (inline, the server's attachment store or a fresh upload). +bool chatPartIsThumbnail(JsonMap part) => + chatPartImageMime(part) != null && + chatImageSourceOf('${part['uri'] ?? ''}'.trim()) != null; + +/// The operator's attached images above their message, end-aligned like the +/// turn itself, wrapping onto further lines on a narrow pane. +class ChatAttachmentGallery extends StatelessWidget { + const ChatAttachmentGallery({ + super.key, + required this.parts, + required this.images, + required this.cupertino, + }); + + /// File parts for which [chatPartIsThumbnail] holds. + final List parts; + final ChatAttachmentImages images; + final bool cupertino; + + @override + Widget build(BuildContext context) => Wrap( + key: const Key('chat-attachment-gallery'), + alignment: WrapAlignment.end, + spacing: 8, + runSpacing: 8, + children: [ + for (final (index, part) in parts.indexed) + ChatAttachmentThumbnail( + key: ValueKey('chat-attachment-image-$index'), + name: chatAttachmentPartName(part), + uri: '${part['uri'] ?? ''}'.trim(), + images: images, + cupertino: cupertino, + ), + ], + ); +} + +/// The web's attachment name rule: its own name, else the URI basename, else +/// `file`. A `data:` URI has no useful basename, so it reads `image`. +String chatAttachmentPartName(JsonMap part) { + final name = '${part['name'] ?? ''}'.trim(); + if (name.isNotEmpty) return name; + final uri = '${part['uri'] ?? ''}'.trim(); + if (uri.startsWith('data:')) return 'image'; + final basename = uri.split('/').last.trim(); + return basename.isEmpty ? 'file' : basename; +} diff --git a/app/lib/features/chat/ui/transcript/chat_attachment_thumbnail.dart b/app/lib/features/chat/ui/transcript/chat_attachment_thumbnail.dart new file mode 100644 index 00000000..02296999 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_attachment_thumbnail.dart @@ -0,0 +1,202 @@ +import 'dart:typed_data'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../data/chat_attachment_images.dart'; +import 'chat_image_viewer.dart'; + +/// The largest edge a sent image's thumbnail takes in the transcript. +const double chatThumbnailMaxWidth = 200; +const double chatThumbnailMaxHeight = 240; +const BorderRadius _radius = BorderRadius.all(Radius.circular(12)); + +/// One image the operator attached, drawn as a rounded thumbnail that opens +/// [ChatImageViewer]. Bytes load lazily through [images] and stay cached there; +/// a file the server no longer has becomes a small labelled placeholder. +class ChatAttachmentThumbnail extends StatefulWidget { + const ChatAttachmentThumbnail({ + super.key, + required this.name, + required this.uri, + required this.images, + required this.cupertino, + }); + + final String name; + final String uri; + final ChatAttachmentImages images; + final bool cupertino; + + @override + State createState() => + _ChatAttachmentThumbnailState(); +} + +class _ChatAttachmentThumbnailState extends State { + Uint8List? _bytes; + String? _failure; + + @override + void initState() { + super.initState(); + _resolve(); + } + + @override + void didUpdateWidget(covariant ChatAttachmentThumbnail oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.uri != widget.uri || oldWidget.images != widget.images) { + _resolve(); + } + } + + void _resolve() { + final uri = widget.uri; + _bytes = widget.images.ready(uri); + _failure = widget.images.failure(uri); + if (_bytes != null || _failure != null) return; + widget.images + .load(uri) + .then( + (bytes) { + if (mounted && widget.uri == uri) setState(() => _bytes = bytes); + }, + onError: (Object error) { + if (mounted && widget.uri == uri) { + setState(() => _failure = '$error'); + } + }, + ); + } + + @override + Widget build(BuildContext context) { + final bytes = _bytes; + if (bytes != null) return _thumbnail(context, bytes); + final failure = _failure; + if (failure != null) return _placeholder(context, failure); + return Semantics( + label: '${widget.name}, loading', + child: _pending(context), + ); + } + + /// A quiet surface square while loaded bytes decode, which is brief. + Widget _decoding(BuildContext context) => SizedBox.square( + dimension: 96, + child: ColoredBox( + color: widget.cupertino + ? CupertinoColors.systemGrey5.resolveFrom(context) + : Theme.of(context).colorScheme.surfaceContainerHighest, + ), + ); + + /// A square the thumbnail occupies while its bytes load from the server. + Widget _pending(BuildContext context) => SizedBox.square( + key: const Key('chat-attachment-thumbnail-loading'), + dimension: 96, + child: Center( + child: widget.cupertino + ? const CupertinoActivityIndicator() + : const CircularProgressIndicator.adaptive(), + ), + ); + + Widget _thumbnail(BuildContext context, Uint8List bytes) { + final ratio = MediaQuery.devicePixelRatioOf(context); + final image = ClipRRect( + borderRadius: _radius, + child: Image( + key: const Key('chat-attachment-thumbnail-image'), + // Decode at the thumbnail's size, not the photo's, while scrolling. + image: ResizeImage( + MemoryImage(bytes), + width: (chatThumbnailMaxWidth * ratio).round(), + height: (chatThumbnailMaxHeight * ratio).round(), + policy: ResizeImagePolicy.fit, + allowUpscaling: false, + ), + fit: BoxFit.contain, + gaplessPlayback: true, + // Until the first frame decodes, hold the loading square's place so + // the row does not collapse and the control can still be tapped. + frameBuilder: (context, child, frame, synchronous) => + frame == null && !synchronous ? _decoding(context) : child, + errorBuilder: (context, _, _) => + _placeholder(context, 'Image could not be read'), + ), + ); + void open() => openChatImageViewer( + context, + name: widget.name, + bytes: bytes, + cupertino: widget.cupertino, + ); + final constrained = ConstrainedBox( + constraints: const BoxConstraints( + maxWidth: chatThumbnailMaxWidth, + maxHeight: chatThumbnailMaxHeight, + ), + child: image, + ); + return Semantics( + button: true, + image: true, + label: widget.name, + excludeSemantics: true, + onTap: open, + child: widget.cupertino + ? CupertinoButton( + key: const Key('chat-attachment-thumbnail'), + padding: EdgeInsets.zero, + minimumSize: Size.zero, + onPressed: open, + child: constrained, + ) + : Material( + type: MaterialType.transparency, + child: InkWell( + key: const Key('chat-attachment-thumbnail'), + borderRadius: _radius, + onTap: open, + child: constrained, + ), + ), + ); + } + + Widget _placeholder(BuildContext context, String reason) { + final secondary = widget.cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant; + return Semantics( + key: const Key('chat-attachment-thumbnail-unavailable'), + label: '${widget.name}, $reason', + excludeSemantics: true, + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: chatThumbnailMaxWidth), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + widget.cupertino + ? CupertinoIcons.photo + : Icons.broken_image_outlined, + color: secondary, + ), + const SizedBox(width: 8), + Flexible( + child: Text( + reason, + style: DefaultTextStyle.of( + context, + ).style.copyWith(color: secondary), + ), + ), + ], + ), + ), + ); + } +} diff --git a/app/lib/features/chat/ui/transcript/chat_image_viewer.dart b/app/lib/features/chat/ui/transcript/chat_image_viewer.dart new file mode 100644 index 00000000..afe0ce3e --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_image_viewer.dart @@ -0,0 +1,90 @@ +import 'dart:typed_data'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +/// The accessible name and visible label of the viewer's close control. +const String chatImageViewerClose = 'Close'; + +/// One attached image full screen: pinch or scroll to zoom, drag to pan once +/// zoomed, and the host's own close control in the navigation bar. +class ChatImageViewer extends StatelessWidget { + const ChatImageViewer({ + super.key, + required this.name, + required this.bytes, + required this.cupertino, + }); + + final String name; + final Uint8List bytes; + final bool cupertino; + + @override + Widget build(BuildContext context) { + final body = SafeArea( + child: InteractiveViewer( + key: const Key('chat-image-viewer-zoom'), + maxScale: 8, + child: Center( + child: Semantics( + image: true, + label: name, + child: Image.memory( + bytes, + key: const Key('chat-image-viewer-image'), + fit: BoxFit.contain, + gaplessPlayback: true, + errorBuilder: (context, _, _) => Icon( + cupertino ? CupertinoIcons.photo : Icons.broken_image_outlined, + ), + ), + ), + ), + ), + ); + void close() => Navigator.of(context).maybePop(); + if (cupertino) { + return CupertinoPageScaffold( + navigationBar: CupertinoNavigationBar( + automaticallyImplyLeading: false, + middle: Text(name, overflow: TextOverflow.ellipsis), + leading: CupertinoButton( + key: const Key('chat-image-viewer-close'), + padding: EdgeInsets.zero, + onPressed: close, + child: const Text(chatImageViewerClose), + ), + ), + child: body, + ); + } + return Scaffold( + appBar: AppBar( + automaticallyImplyLeading: false, + leading: IconButton( + key: const Key('chat-image-viewer-close'), + tooltip: chatImageViewerClose, + onPressed: close, + icon: const Icon(Icons.close), + ), + title: Text(name, overflow: TextOverflow.ellipsis), + ), + body: body, + ); + } +} + +/// Pushes [ChatImageViewer] with the host's full-screen dialog transition. +Future openChatImageViewer( + BuildContext context, { + required String name, + required Uint8List bytes, + required bool cupertino, +}) { + final page = ChatImageViewer(name: name, bytes: bytes, cupertino: cupertino); + final route = cupertino + ? CupertinoPageRoute(fullscreenDialog: true, builder: (_) => page) + : MaterialPageRoute(fullscreenDialog: true, builder: (_) => page); + return Navigator.of(context).push(route); +} diff --git a/app/lib/features/chat/ui/transcript/chat_message_view.dart b/app/lib/features/chat/ui/transcript/chat_message_view.dart index ba0e1edf..e6b38790 100644 --- a/app/lib/features/chat/ui/transcript/chat_message_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_message_view.dart @@ -4,7 +4,9 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../../core/api_gateway.dart'; +import '../../data/chat_attachment_images.dart'; import '../sessions/chat_adaptive.dart'; +import 'chat_attachment_gallery.dart'; import '../../domain/chat_tool_calls.dart'; import 'chat_copy_button.dart'; import 'chat_message_bubble.dart'; @@ -22,11 +24,18 @@ class ChatMessageView extends StatelessWidget { required this.row, this.textBuilder, this.footer = const [], + this.images, }); final JsonMap row; final ChatMessageTextBuilder? textBuilder; + /// Loads attached images for the operator's turn; without one, only images + /// the history carries inline can draw. + final ChatAttachmentImages? images; + + static final ChatAttachmentImages _inlineImages = ChatAttachmentImages(null); + /// The turn's settled permissions, independent of passive activity details. final List footer; @@ -69,7 +78,7 @@ class ChatMessageView extends StatelessWidget { : 720, ), child: user - ? ChatUserBubble(cupertino: cupertino, child: body) + ? _userTurn(cupertino, content, body) : _agentTurn( context, content, @@ -89,10 +98,38 @@ class ChatMessageView extends StatelessWidget { bool _isCupertino(BuildContext context) => chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + JsonMap get _userMessage => row['message'] is Map + ? Map.from(row['message'] as Map) + : row; + + /// Attached images sit above the operator's bubble, as their own thumbnails; + /// an images-only turn draws no empty bubble. + Widget _userTurn(bool cupertino, List content, Widget body) { + final thumbnails = [ + for (final part in _partsOf(_userMessage)) + if (_isFile(part) && chatPartIsThumbnail(part)) part, + ]; + if (thumbnails.isEmpty) { + return ChatUserBubble(cupertino: cupertino, child: body); + } + return Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + ChatAttachmentGallery( + parts: thumbnails, + images: images ?? _inlineImages, + cupertino: cupertino, + ), + if (content.isNotEmpty) const SizedBox(height: 6), + if (content.isNotEmpty) + ChatUserBubble(cupertino: cupertino, child: body), + ], + ); + } + List _userParts(BuildContext context) { - final message = row['message'] is Map - ? Map.from(row['message'] as Map) - : row; + final message = _userMessage; final parts = _partsOf(message); final text = parts .where((part) => part['type'] == null || part['type'] == 'text') @@ -100,7 +137,8 @@ class ChatMessageView extends StatelessWidget { .join('\n'); final widgets = [ if (text.trim().isNotEmpty) SelectableText(text), - for (final part in parts.where(_isFile)) _attachment(context, part), + for (final part in parts.where(_isFile)) + if (!chatPartIsThumbnail(part)) _attachment(context, part), for (final part in parts) if (part['type'] != 'text' && !_isFile(part) && @@ -262,11 +300,7 @@ class ChatMessageView extends StatelessWidget { padding: const EdgeInsets.only(right: 8), child: Icon(cupertino ? CupertinoIcons.paperclip : Icons.attach_file), ), - Expanded( - child: SelectableText( - _attachmentDisplayName(part['name'], part['uri']), - ), - ), + Expanded(child: SelectableText(chatAttachmentPartName(part))), ], ); } @@ -288,11 +322,3 @@ List _partsOf(Map source) { bool _isFile(JsonMap part) => part['type'] == 'file' && '${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty; - -/// The web's attachment name rule: its own name, else the URI basename, else -/// `file`, so an attachment never shows an empty label. -String _attachmentDisplayName(Object? name, Object? uri) { - final shown = '${name ?? ''}'.trim(); - final basename = '${uri ?? ''}'.split('/').last.trim(); - return shown.isNotEmpty ? shown : (basename.isEmpty ? 'file' : basename); -} diff --git a/app/notes/chat-attachment-thumbnails.md b/app/notes/chat-attachment-thumbnails.md new file mode 100644 index 00000000..8eca53b6 --- /dev/null +++ b/app/notes/chat-attachment-thumbnails.md @@ -0,0 +1,60 @@ +# Chat attachment thumbnails + +Images the operator sends now show as rounded thumbnails above the user bubble +(up to 200x240 logical px). Tapping one opens a full-screen viewer that you can +zoom with pinch or scroll. The viewer uses the platform's own close control: +an IconButton in the AppBar on Material, and a CupertinoButton in the +CupertinoNavigationBar on Cupertino. Other files keep the paperclip row. + +## How the image bytes are loaded + +- **`data:` URI.** Claude Code history stores image blocks inline, so the app + decodes these directly. +- **`file://` URI.** OpenCode and Codex history point at the server's + attachment store. The app fetches these with the new websocket request + `chat_attachment_image`: + - Request: `{chat_attachment_image_request: {uri}}`. + - Reply: `{chat_attachment_image: {uri, mime, size, data(base64)}}`. + - The server opens the store with `os.Root`. It serves only regular files + that sit directly in that directory and are 4 MiB or smaller. The type is + sniffed from the bytes and must be png, jpeg, gif, webp or bmp. + - It refuses traversal, symlinks, nested paths, other hosts and other + schemes. + - A missing file gets the error "image is no longer available". The app + then shows a placeholder that reads "Image no longer available". +- **`upload:`.** This is the URI on a just-sent (optimistic) row. + The composer gives its own bytes to the cache, so the image shows right + away. The same bytes also serve the recorded `file:///…/up-…` URI after the + server records the turn, because the stored file name is the same. Files + sent inline use their own `data:` URI. +- **http(s) URLs.** These are never loaded. They stay as file rows. + +`ChatAttachmentImages` is created once per thread view and per gateway. It +holds the decoded bytes in memory, keyed by URI, up to 48 MiB, and drops the +least recently used first. When the server refuses an image, that answer is +kept and the image is not requested again. Transport errors are not kept, so a +later build tries again. + +## Evidence + +- `go test ./internal/server/ -run ChatAttachment` passes. This covers a + stored image, the outside, traversal, encoded, nested, symlink, directory, + host, scheme, query, fragment and non-image cases, and the missing, + oversize and removed-store cases. +- `flutter test` (full suite) passes, including + `test/features/chat/ui/transcript/chat_attachment_thumbnail_test.dart` and + `test/features/chat/domain/chat_attachment_image_test.dart`. +- `flutter analyze` reports only the existing info in + `artifact_viewer_page_test.dart`. +- `flutter build web` succeeds. +- No browser or device capture was taken. The rendered look has not been + checked outside widget tests. + +## Known gaps + +- Claude Code history drops an image block whose base64 data is over 512 KiB + (`internal/claude/transcript.go`). After a reload, such an image is missing + from the turn entirely, so there is nothing to draw. +- Images over 4 MiB show "Image too large to preview". The limit exists + because the reply is a single websocket frame that the server must write + within its 5 s deadline, and the client sends requests one at a time. diff --git a/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart b/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart index 85f86e01..627c3d31 100644 --- a/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart +++ b/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart @@ -215,7 +215,13 @@ void main() { await tester.tap(find.byKey(const Key('chat-composer-attach'))); await tester.pumpAndSettle(); await typeAndSend(tester, 'hello'); - expect(find.text('screen.png'), findsOneWidget); + // The sent image draws from its own inline bytes as a thumbnail named + // for the file; the text file stays a named row. + expect( + find.byKey(const Key('chat-attachment-thumbnail')), + findsOneWidget, + ); + expect(find.bySemanticsLabel('screen.png'), findsOneWidget); expect(find.text('notes.txt'), findsOneWidget); expect(find.textContaining('data'), findsNothing); diff --git a/app/test/features/chat/domain/chat_attachment_image_test.dart b/app/test/features/chat/domain/chat_attachment_image_test.dart new file mode 100644 index 00000000..0bd61f66 --- /dev/null +++ b/app/test/features/chat/domain/chat_attachment_image_test.dart @@ -0,0 +1,70 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/domain/chat_attachment_image.dart'; + +void main() { + test('image type comes from MIME, data: URI or extension', () { + expect( + chatPartImageMime({'type': 'file', 'mime': 'image/PNG', 'uri': 'x'}), + 'image/png', + ); + expect( + chatPartImageMime({'type': 'file', 'uri': 'data:image/jpeg;base64,AA'}), + 'image/jpeg', + ); + expect( + chatPartImageMime({'type': 'file', 'name': 'a.WEBP', 'uri': 'file:///a'}), + 'image/webp', + ); + expect( + chatPartImageMime({'type': 'file', 'mime': 'image/svg+xml', 'uri': 'a'}), + isNull, + ); + expect( + chatPartImageMime({ + 'type': 'file', + 'mime': 'text/plain', + 'name': 'a.png', + }), + isNull, + ); + expect(chatPartImageMime({'type': 'text', 'mime': 'image/png'}), isNull); + }); + + test('only inline, stored and uploaded URIs are loaded', () { + expect( + chatImageSourceOf('data:image/png;base64,AA'), + ChatImageSource.inline, + ); + expect(chatImageSourceOf('file:///s/up-1-a.png'), ChatImageSource.stored); + expect(chatImageSourceOf('upload:up-1-a.png'), ChatImageSource.upload); + expect(chatImageSourceOf('https://x.test/a.png'), isNull); + expect(chatImageSourceOf('a.png'), isNull); + }); + + test('optimistic URIs and stored names line up with the recorded turn', () { + expect( + chatOptimisticAttachmentUri({ + 'name': 'a b.png', + 'upload': 'up-1-a b.png', + }), + 'upload:up-1-a b.png', + ); + expect( + chatOptimisticAttachmentUri({ + 'name': 'a.png', + 'mime': 'image/png', + 'data': 'AA', + }), + 'data:image/png;base64,AA', + ); + expect(chatOptimisticAttachmentUri({'name': 'a.png'}), 'a.png'); + expect(chatStoredFileName('upload:up-1-a b.png'), 'up-1-a b.png'); + expect(chatStoredFileName('file:///s/up-1-a%20b.png'), 'up-1-a b.png'); + }); + + test('data: URIs decode, malformed ones do not', () { + expect(chatDataUriBytes('data:image/png;base64,AAEC'), [0, 1, 2]); + expect(chatDataUriBytes('data:image/png,plain'), isNull); + expect(chatDataUriBytes('data:image/png;base64,%%%'), isNull); + }); +} diff --git a/app/test/features/chat/ui/transcript/chat_attachment_thumbnail_test.dart b/app/test/features/chat/ui/transcript/chat_attachment_thumbnail_test.dart new file mode 100644 index 00000000..0458f4a2 --- /dev/null +++ b/app/test/features/chat/ui/transcript/chat_attachment_thumbnail_test.dart @@ -0,0 +1,253 @@ +import 'dart:convert'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/core/transport/connection_state.dart'; +import 'package:pipelines_app/features/chat/data/chat_attachment_images.dart'; +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart'; + +/// A 1x1 PNG. +const String _png = + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='; +const String _stored = + 'file:///home/op/.pipelines-chat-attachments/up-1-shot.png'; + +class _ImageGateway implements ApiGateway { + final calls = []; + Object? failure; + + @override + Stream get frames => const Stream.empty(); + + @override + Future call(String type, [JsonMap fields = const {}]) async { + expect(type, 'chat_attachment_image'); + calls.add(fields); + final failure = this.failure; + if (failure != null) throw failure; + final uri = (fields['chat_attachment_image_request'] as Map)['uri']; + return { + 'type': 'chat_attachment_image', + 'chat_attachment_image': {'uri': uri, 'mime': 'image/png', 'data': _png}, + }; + } +} + +JsonMap _user(List parts) => transcriptRows([ + {'id': 'u1', 'role': 'user', 'parts': parts}, +]).single; + +Widget _host( + JsonMap row, + ChatAttachmentImages? images, { + bool cupertino = false, +}) { + final body = ListView( + children: [ChatMessageView(row: row, images: images)], + ); + if (cupertino) { + return CupertinoApp(home: CupertinoPageScaffold(child: body)); + } + return MaterialApp( + theme: ThemeData(platform: TargetPlatform.android), + home: Scaffold(body: body), + ); +} + +final _thumbnail = find.byKey(const Key('chat-attachment-thumbnail')); +final _viewerImage = find.byKey(const Key('chat-image-viewer-image')); + +void main() { + for (final cupertino in [false, true]) { + final style = cupertino ? 'Cupertino' : 'Material'; + + testWidgets( + '$style: an inline data: image draws a thumbnail above the text ' + 'and opens the full-screen viewer', + (tester) async { + final handle = tester.ensureSemantics(); + await tester.pumpWidget( + _host( + _user([ + {'type': 'text', 'text': 'See this'}, + { + 'type': 'file', + 'name': 'image', + 'mime': 'image/png', + 'uri': 'data:image/png;base64,$_png', + }, + ]), + null, + cupertino: cupertino, + ), + ); + await tester.pump(); + expect(_thumbnail, findsOneWidget); + expect( + find.byKey(const Key('chat-attachment-thumbnail-image')), + findsOneWidget, + ); + // No paperclip row and no base64 shown as a file name. + expect(find.byIcon(Icons.attach_file), findsNothing); + expect(find.byIcon(CupertinoIcons.paperclip), findsNothing); + expect(find.textContaining('base64'), findsNothing); + expect( + tester.getTopLeft(_thumbnail).dy, + lessThan(tester.getTopLeft(find.text('See this')).dy), + ); + expect( + tester.getSemantics(_thumbnail.first), + matchesSemantics( + label: 'image', + isButton: true, + isImage: true, + hasTapAction: true, + ), + ); + handle.dispose(); + + await tester.tap(_thumbnail); + await tester.pumpAndSettle(); + expect(_viewerImage, findsOneWidget); + expect(find.byKey(const Key('chat-image-viewer-zoom')), findsOneWidget); + await tester.tap(find.byKey(const Key('chat-image-viewer-close'))); + await tester.pumpAndSettle(); + expect(_viewerImage, findsNothing); + expect(_thumbnail, findsOneWidget); + }, + ); + } + + testWidgets('a stored file:// image is fetched once through the gateway and ' + 'reused when the row is rebuilt', (tester) async { + final gateway = _ImageGateway(); + final images = ChatAttachmentImages(gateway); + // Codex records no MIME: the name's extension makes it an image. + final row = _user([ + {'type': 'file', 'name': 'shot.png', 'uri': _stored}, + ]); + await tester.pumpWidget(_host(row, images)); + expect( + find.byKey(const Key('chat-attachment-thumbnail-loading')), + findsOneWidget, + ); + await tester.pump(); + await tester.pump(); + expect(_thumbnail, findsOneWidget); + expect(gateway.calls, [ + { + 'chat_attachment_image_request': {'uri': _stored}, + }, + ]); + + await tester.pumpWidget(const SizedBox()); + await tester.pumpWidget(_host(row, images)); + expect(_thumbnail, findsOneWidget); + expect(gateway.calls, hasLength(1)); + + await tester.tap(_thumbnail); + await tester.pumpAndSettle(); + expect(_viewerImage, findsOneWidget); + expect(find.text('shot.png'), findsOneWidget); + }); + + testWidgets('an expired stored image becomes a labelled placeholder', ( + tester, + ) async { + final gateway = _ImageGateway() + ..failure = const ProtocolError('image is no longer available'); + final images = ChatAttachmentImages(gateway); + final row = _user([ + {'type': 'text', 'text': 'Old screenshot'}, + {'type': 'file', 'name': 'shot.png', 'mime': 'image/png', 'uri': _stored}, + ]); + await tester.pumpWidget(_host(row, images)); + await tester.pump(); + await tester.pump(); + expect(find.text(chatImageGoneMessage), findsOneWidget); + expect(_thumbnail, findsNothing); + expect(find.text('Old screenshot'), findsOneWidget); + + // The refusal is settled: rebuilding does not ask again. + await tester.pumpWidget(const SizedBox()); + await tester.pumpWidget(_host(row, images)); + expect(find.text(chatImageGoneMessage), findsOneWidget); + expect(gateway.calls, hasLength(1)); + }); + + testWidgets('non-image files and remote image URLs keep the file row', ( + tester, + ) async { + final gateway = _ImageGateway(); + await tester.pumpWidget( + _host( + _user([ + {'type': 'text', 'text': 'Files'}, + { + 'type': 'file', + 'name': 'report.pdf', + 'mime': 'application/pdf', + 'uri': + 'file:///home/op/.pipelines-chat-attachments/up-2-report.pdf', + }, + {'type': 'file', 'name': 'remote.png', 'uri': 'https://x.test/a.png'}, + { + 'type': 'file', + 'name': 'logo.svg', + 'mime': 'image/svg+xml', + 'uri': 'file:///home/op/.pipelines-chat-attachments/up-3-logo.svg', + }, + ]), + ChatAttachmentImages(gateway), + ), + ); + await tester.pump(); + expect(_thumbnail, findsNothing); + expect(find.text('report.pdf'), findsOneWidget); + expect(find.text('remote.png'), findsOneWidget); + expect(find.text('logo.svg'), findsOneWidget); + expect(find.byIcon(Icons.attach_file), findsNWidgets(3)); + expect(gateway.calls, isEmpty); + }); + + testWidgets('a just-uploaded image draws from the composer bytes before and ' + 'after the server records the turn', (tester) async { + final gateway = _ImageGateway(); + final images = ChatAttachmentImages(gateway) + ..rememberUpload('up-1-shot.png', base64Decode(_png)); + await tester.pumpWidget( + _host( + _user([ + { + 'type': 'file', + 'name': 'shot.png', + 'mime': 'image/png', + 'uri': 'upload:up-1-shot.png', + }, + ]), + images, + ), + ); + await tester.pump(); + expect(_thumbnail, findsOneWidget); + await tester.pumpWidget( + _host( + _user([ + { + 'type': 'file', + 'name': 'shot.png', + 'mime': 'image/png', + 'uri': _stored, + }, + ]), + images, + ), + ); + await tester.pump(); + expect(_thumbnail, findsOneWidget); + expect(gateway.calls, isEmpty); + }); +} diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index 35bae301..737afef5 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -80,6 +80,12 @@ const ( // session. ChatShellOutput reads a bounded page from one such shell. ChatShells = "chat_shells" ChatShellOutput = "chat_shell_output" + // ChatAttachmentImage reads back one image the operator attached earlier, + // so a transcript can draw it after a reload. The request body is + // ChatAttachmentImageRequest (the history part's file:// URI) and the reply + // carries ChatAttachmentImageContent. Only regular image files inside the + // server's private chat attachment store are served. + ChatAttachmentImage = "chat_attachment_image" // ChatEvent and ChatPermission are the server-pushed frames; they are the // only types the server sends without a matching request. ChatEvent = "chat_event" @@ -361,6 +367,21 @@ type ChatAttachment struct { Upload string `json:"upload,omitempty"` } +// ChatAttachmentImageRequest names one stored attachment by the file:// URI a +// history file part carries. +type ChatAttachmentImageRequest struct { + URI string `json:"uri"` +} + +// ChatAttachmentImageContent is the chat_attachment_image reply: the file's +// bytes as standard base64 and the image MIME type sniffed from those bytes. +type ChatAttachmentImageContent struct { + URI string `json:"uri"` + Mime string `json:"mime"` + Size int64 `json:"size"` + Data string `json:"data"` +} + // ChatMessage is one stored turn returned by chat_messages. A user turn is a // single text part; an assistant turn holds the text, reasoning and tool parts // the phone renders. Parts corresponds to the OpenCode content array. diff --git a/internal/protocol/message.go b/internal/protocol/message.go index ae9ac743..732e0385 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -229,6 +229,10 @@ type Message struct { ChatShellOutputQuery *ChatShellOutputQuery `json:"chat_shell_output_query,omitempty"` ChatShells []ChatShell `json:"chat_shells,omitempty"` ChatShellOutput *ChatShellOutputPage `json:"chat_shell_output,omitempty"` + // ChatAttachmentImageRequest is the chat_attachment_image body and + // ChatAttachmentImage its reply. + ChatAttachmentImageRequest *ChatAttachmentImageRequest `json:"chat_attachment_image_request,omitempty"` + ChatAttachmentImage *ChatAttachmentImageContent `json:"chat_attachment_image,omitempty"` // RunEvent is the server-pushed run state delta (see runevents.go) and // RunIDs the optional subscribe scope, so a client asks for the runs it // displays. Both are optional so older peers ignore them. diff --git a/internal/server/chat.go b/internal/server/chat.go index c6d6a0e3..3d3c07a7 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -215,6 +215,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker case protocol.ChatShellOutput: handleChatShellOutput(ctx, request, broker, registry, write) return true + case protocol.ChatAttachmentImage: + handleChatAttachmentImage(request, broker, write) + return true } return false } diff --git a/internal/server/chat_attachment_image.go b/internal/server/chat_attachment_image.go new file mode 100644 index 00000000..765242e3 --- /dev/null +++ b/internal/server/chat_attachment_image.go @@ -0,0 +1,136 @@ +package server + +import ( + "encoding/base64" + "errors" + "fmt" + "io" + "io/fs" + "net/http" + "net/url" + "os" + "path/filepath" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// maxChatAttachmentImageBytes bounds one chat_attachment_image reply. The +// bytes travel base64-encoded in a single websocket frame that the server must +// write within its per-frame deadline, and clients serialize requests, so a +// preview stays well below the 100 MiB upload limit. +const maxChatAttachmentImageBytes = 4 << 20 + +// errChatAttachmentGone is the reply for a stored file that was collected by +// retention or never existed; clients show it as "no longer available". +var errChatAttachmentGone = errors.New("image is no longer available") + +// chatAttachmentImageTypes are the sniffed types a client can draw. SVG and +// anything unrecognised stay a plain file row. +var chatAttachmentImageTypes = map[string]bool{ + "image/png": true, + "image/jpeg": true, + "image/gif": true, + "image/webp": true, + "image/bmp": true, +} + +// handleChatAttachmentImage returns one image file the operator attached, so a +// transcript can draw it after a reload. Only regular files directly inside the +// private attachment store are readable; anything else is refused before it is +// opened. +func handleChatAttachmentImage(request protocol.Message, broker chatBroker, write func(protocol.Message) error) { + fail := func(message string) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: message}) + } + if broker == nil { + fail("chat is not configured on this server") + return + } + query := request.ChatAttachmentImageRequest + if query == nil || query.URI == "" { + fail("a chat_attachment_image_request with a uri is required") + return + } + data, mime, err := readChatAttachmentImage(query.URI) + if err != nil { + fail(err.Error()) + return + } + _ = write(protocol.Message{Type: protocol.ChatAttachmentImage, ID: request.ID, ChatAttachmentImage: &protocol.ChatAttachmentImageContent{ + URI: query.URI, + Mime: mime, + Size: int64(len(data)), + Data: base64.StdEncoding.EncodeToString(data), + }}) +} + +// readChatAttachmentImage resolves a file:// URI to one regular file in the +// attachment store and returns its bytes and sniffed image type. +func readChatAttachmentImage(uri string) ([]byte, string, error) { + notStored := errors.New("not a stored chat attachment") + u, err := url.Parse(uri) + if err != nil || u.Scheme != "file" || u.Host != "" || u.User != nil || u.Opaque != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" { + return nil, "", notStored + } + path := u.Path + if !filepath.IsAbs(path) || filepath.Clean(path) != path { + return nil, "", notStored + } + dir, err := chatAttachmentDir() + if err != nil { + return nil, "", err + } + name := filepath.Base(path) + if filepath.Dir(path) != dir || name == "." || name == ".." || name == string(filepath.Separator) { + return nil, "", notStored + } + dirInfo, err := os.Lstat(dir) + if errors.Is(err, fs.ErrNotExist) { + return nil, "", errChatAttachmentGone + } + if err != nil || !dirInfo.IsDir() { + return nil, "", errors.New("attachment storage must be a directory, not a symlink") + } + root, err := os.OpenRoot(dir) + if err != nil { + return nil, "", err + } + defer root.Close() + if actual, err := root.Stat("."); err != nil || !os.SameFile(dirInfo, actual) { + return nil, "", errors.New("chat attachment storage changed while opening") + } + info, err := root.Lstat(name) + if errors.Is(err, fs.ErrNotExist) { + return nil, "", errChatAttachmentGone + } + if err != nil || !info.Mode().IsRegular() { + return nil, "", notStored + } + if info.Size() > maxChatAttachmentImageBytes { + return nil, "", fmt.Errorf("image is too large to preview (over %d MiB)", maxChatAttachmentImageBytes>>20) + } + file, err := root.Open(name) + if errors.Is(err, fs.ErrNotExist) { + return nil, "", errChatAttachmentGone + } + if err != nil { + return nil, "", notStored + } + defer file.Close() + opened, err := file.Stat() + if err != nil || !os.SameFile(info, opened) || !opened.Mode().IsRegular() { + return nil, "", errors.New("attachment changed while opening") + } + data, err := io.ReadAll(io.LimitReader(file, maxChatAttachmentImageBytes+1)) + if err != nil { + return nil, "", fmt.Errorf("read attachment: %w", err) + } + if len(data) > maxChatAttachmentImageBytes { + return nil, "", fmt.Errorf("image is too large to preview (over %d MiB)", maxChatAttachmentImageBytes>>20) + } + mime := http.DetectContentType(data) + if !chatAttachmentImageTypes[mime] { + return nil, "", errors.New("attachment is not a previewable image") + } + return data, mime, nil +} diff --git a/internal/server/chat_attachment_image_test.go b/internal/server/chat_attachment_image_test.go new file mode 100644 index 00000000..271eeb11 --- /dev/null +++ b/internal/server/chat_attachment_image_test.go @@ -0,0 +1,149 @@ +package server + +import ( + "context" + "encoding/base64" + "net/url" + "os" + "path/filepath" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// pngHeader is enough for content sniffing to report image/png. +var pngHeader = []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00") + +func attachmentImageStore(t *testing.T) string { + t.Helper() + dir := filepath.Join(t.TempDir(), "uploads") + if err := os.Mkdir(dir, 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir) + return dir +} + +func fileURI(path string) string { return (&url.URL{Scheme: "file", Path: path}).String() } + +func requestAttachmentImage(t *testing.T, uri string) protocol.Message { + t.Helper() + var reply protocol.Message + request := protocol.Message{Type: protocol.ChatAttachmentImage, ID: "img-1", ChatAttachmentImageRequest: &protocol.ChatAttachmentImageRequest{URI: uri}} + handled := handleChat(context.Background(), request, &fakeChat{}, nil, nil, "conn-test", func(message protocol.Message) error { + reply = message + return nil + }) + if !handled { + t.Fatal("chat_attachment_image was not dispatched") + } + if reply.ID != "img-1" { + t.Fatalf("reply id = %q", reply.ID) + } + return reply +} + +func TestChatAttachmentImageServesStoredImage(t *testing.T) { + dir := attachmentImageStore(t) + path := filepath.Join(dir, "up-0123456789abcdef0123456789abcdef-shot one.png") + if err := os.WriteFile(path, pngHeader, 0o600); err != nil { + t.Fatal(err) + } + reply := requestAttachmentImage(t, fileURI(path)) + if reply.Type != protocol.ChatAttachmentImage || reply.ChatAttachmentImage == nil { + t.Fatalf("reply = %+v", reply) + } + got := reply.ChatAttachmentImage + data, err := base64.StdEncoding.DecodeString(got.Data) + if err != nil || string(data) != string(pngHeader) { + t.Fatalf("data = %q err=%v", data, err) + } + if got.Mime != "image/png" || got.Size != int64(len(pngHeader)) || got.URI != fileURI(path) { + t.Fatalf("content = %+v", got) + } +} + +func TestChatAttachmentImageRefusesOutsideAndUnsafeReferences(t *testing.T) { + dir := attachmentImageStore(t) + outside := filepath.Join(filepath.Dir(dir), "secret.png") + if err := os.WriteFile(outside, pngHeader, 0o600); err != nil { + t.Fatal(err) + } + nested := filepath.Join(dir, "nested") + if err := os.Mkdir(nested, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(nested, "inner.png"), pngHeader, 0o600); err != nil { + t.Fatal(err) + } + link := filepath.Join(dir, "link.png") + if err := os.Symlink(outside, link); err != nil { + t.Fatal(err) + } + text := filepath.Join(dir, "notes.txt") + if err := os.WriteFile(text, []byte("plain text, not an image"), 0o600); err != nil { + t.Fatal(err) + } + cases := map[string]string{ + "outside": fileURI(outside), + "traversal": "file://" + dir + "/../secret.png", + "encoded": "file://" + dir + "/%2e%2e/secret.png", + "nested": fileURI(filepath.Join(nested, "inner.png")), + "symlink": fileURI(link), + "directory": fileURI(nested), + "store itself": fileURI(dir), + "relative": "file:secret.png", + "remote host": "file://example.com" + outside, + "http": "https://example.com/a.png", + "data": "data:image/png;base64,AAAA", + "query": fileURI(outside) + "?x=1", + "not an image": fileURI(text), + "plain path": outside, + "fragment": fileURI(outside) + "#top", + } + for name, uri := range cases { + t.Run(name, func(t *testing.T) { + reply := requestAttachmentImage(t, uri) + if reply.Type != protocol.Error || reply.ChatAttachmentImage != nil { + t.Fatalf("%s was served: %+v", uri, reply) + } + }) + } +} + +func TestChatAttachmentImageMissingAndOversize(t *testing.T) { + dir := attachmentImageStore(t) + missing := requestAttachmentImage(t, fileURI(filepath.Join(dir, "up-gone.png"))) + if missing.Type != protocol.Error || !strings.Contains(missing.Error, "no longer available") { + t.Fatalf("missing = %+v", missing) + } + large := filepath.Join(dir, "large.png") + body := append(append([]byte{}, pngHeader...), make([]byte, maxChatAttachmentImageBytes)...) + if err := os.WriteFile(large, body, 0o600); err != nil { + t.Fatal(err) + } + oversize := requestAttachmentImage(t, fileURI(large)) + if oversize.Type != protocol.Error || !strings.Contains(oversize.Error, "too large") { + t.Fatalf("oversize = %+v", oversize) + } + if err := os.RemoveAll(dir); err != nil { + t.Fatal(err) + } + gone := requestAttachmentImage(t, fileURI(filepath.Join(dir, "up-any.png"))) + if gone.Type != protocol.Error || !strings.Contains(gone.Error, "no longer available") { + t.Fatalf("store gone = %+v", gone) + } +} + +func TestChatAttachmentImageRequiresRequestBody(t *testing.T) { + attachmentImageStore(t) + var reply protocol.Message + handleChat(context.Background(), protocol.Message{Type: protocol.ChatAttachmentImage, ID: "x"}, &fakeChat{}, nil, nil, "c", func(m protocol.Message) error { + reply = m + return nil + }) + if reply.Type != protocol.Error { + t.Fatalf("reply = %+v", reply) + } +} diff --git a/internal/server/chat_attachments.go b/internal/server/chat_attachments.go index 0e851582..2dbeccca 100644 --- a/internal/server/chat_attachments.go +++ b/internal/server/chat_attachments.go @@ -190,22 +190,31 @@ func safeChatAttachmentName(value string) string { return name } -// openChatAttachmentStore opens the private attachment directory, collects -// unleased files past retention and reports what the rest occupy. The caller -// holds chatAttachmentStore and closes the root. -func openChatAttachmentStore() (*os.Root, string, int64, int, error) { +// chatAttachmentDir is the configured attachment store directory: an absolute, +// cleaned path that may not exist yet. +func chatAttachmentDir() (string, error) { dir := os.Getenv("PIPELINES_CHAT_ATTACHMENT_DIR") if dir == "" { home, err := os.UserHomeDir() if err != nil { - return nil, "", 0, 0, err + return "", err } dir = filepath.Join(home, ".pipelines-chat-attachments") } if !filepath.IsAbs(dir) { - return nil, "", 0, 0, fmt.Errorf("chat attachment storage must be an absolute path") + return "", fmt.Errorf("chat attachment storage must be an absolute path") + } + return filepath.Clean(dir), nil +} + +// openChatAttachmentStore opens the private attachment directory, collects +// unleased files past retention and reports what the rest occupy. The caller +// holds chatAttachmentStore and closes the root. +func openChatAttachmentStore() (*os.Root, string, int64, int, error) { + dir, err := chatAttachmentDir() + if err != nil { + return nil, "", 0, 0, err } - dir = filepath.Clean(dir) if err := os.Mkdir(dir, 0o700); err != nil && !os.IsExist(err) { return nil, "", 0, 0, fmt.Errorf("prepare attachment directory: %w", err) }