diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 6f646a7d..00f41864 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -4,8 +4,10 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import '../../../core/api_gateway.dart'; import '../domain/chat_attachment_draft.dart'; import '../domain/chat_attachment_picker.dart'; +import '../domain/chat_attachment_wire.dart'; import '../ui/sessions/chat_adaptive.dart'; import 'chat_composer_controller.dart'; @@ -31,7 +33,9 @@ const String chatQueuedNotice = /// replaces the operator's caret. Enter inserts a newline and Ctrl/Cmd+Enter /// sends the plain message. Picked files are memory-only chips: their names are /// shown with the host's own remove action and the bounded draft owns every -/// refusal. Sending does not yet carry attachments, and paste is not wired. +/// refusal. Send carries the chips as the landed wire parts, keeps them until +/// the prompt is accepted and leaves them in place on a refusal; paste is not +/// wired. class ChatComposerBar extends StatefulWidget { const ChatComposerBar({ super.key, @@ -58,6 +62,10 @@ class _ChatComposerBarState extends State { ); final FocusNode _focus = FocusNode(debugLabel: 'chat-composer'); final ChatAttachmentDraft _attachments = ChatAttachmentDraft(); + + /// The picked bytes behind each chip, positionally aligned with the draft's + /// attachments so two picks of the same name stay distinct. + final List _bytes = []; String? _attachmentError; ChatAttachmentPicker get _picker => widget.picker ?? hostChatAttachmentPicker; @@ -103,15 +111,34 @@ class _ChatComposerBarState extends State { } // A held key must not post a second prompt. if (event is! KeyDownEvent) return KeyEventResult.handled; - _send(); + unawaited(_send()); return KeyEventResult.handled; } - void _send() { + /// Builds the chips' exact wire parts and hands them to the composer with the + /// draft's text and operation id. The chips stay while the prompt is in + /// flight so a refusal leaves the draft and its files together; only an + /// accepted prompt clears them. + Future _send() async { final composer = widget.composer; if (composer.sending || composer.draft.trim().isEmpty) return; - unawaited(composer.send()); + List parts; + try { + parts = chatAttachmentParts(_attachments, read: _readAttachment); + } on ChatAttachmentWireException catch (error) { + setState(() => _attachmentError = error.message); + return; + } + // Keep the caret in the field while the prompt is in flight, as before. _focus.requestFocus(); + final accepted = await composer.send(attachments: parts); + if (!mounted) return; + if (accepted) { + setState(() { + _replaceAttachments(const [], const []); + _attachmentError = null; + }); + } } /// Adds the operator's explicit selection through the host seam. A dismissed @@ -132,19 +159,55 @@ class _ChatComposerBarState extends State { setState(() { _attachmentError = null; for (final file in picked) { - final refusal = _attachments.add(file.attachment); - if (refusal != null) _attachmentError = refusal.message; + final attachment = file.attachment; + final refusal = _attachments.add(attachment); + if (refusal != null) { + _attachmentError = refusal.message; + } else { + _bytes.add(file.bytes); + } } }); } - void _removeAttachment(ChatAttachment attachment) { + /// The picked bytes for the draft's own attachment instance. Identity is the + /// only reliable match when two picks share a name, type and size. + Uint8List? _readAttachment(ChatAttachment attachment) { + final attachments = _attachments.attachments; + for (var index = 0; index < attachments.length; index += 1) { + if (identical(attachments[index], attachment)) return _bytes[index]; + } + return null; + } + + void _removeAttachmentAt(int index) { setState(() { - _attachments.remove(attachment); + final remaining = List.from(_attachments.attachments) + ..removeAt(index); + final remainingBytes = List.from(_bytes)..removeAt(index); + _replaceAttachments(remaining, remainingBytes); _attachmentError = null; }); } + /// Rebuilds the draft and its aligned bytes in [attachments] order. The draft + /// owns the count and size bounds, and every kept attachment already passed + /// them, so re-adding never refuses. + void _replaceAttachments( + List attachments, + List bytes, + ) { + for (final attachment in _attachments.attachments) { + _attachments.remove(attachment); + } + _bytes + ..clear() + ..addAll(bytes); + for (final attachment in attachments) { + _attachments.add(attachment); + } + } + @override void dispose() { widget.composer.removeListener(_syncDraft); @@ -272,7 +335,7 @@ class _ChatComposerBarState extends State { Widget _materialChip(ChatAttachment attachment, int index) => Chip( key: ValueKey('chat-composer-attachment-$index'), label: Text(attachment.name), - onDeleted: () => _removeAttachment(attachment), + onDeleted: () => _removeAttachmentAt(index), deleteButtonTooltipMessage: 'Remove ${attachment.name}', ); @@ -307,14 +370,14 @@ class _ChatComposerBarState extends State { container: true, button: true, label: 'Remove ${attachment.name}', - onTap: () => _removeAttachment(attachment), + onTap: () => _removeAttachmentAt(index), child: ExcludeSemantics( child: CupertinoButton( key: ValueKey( 'chat-composer-attachment-remove-$index', ), padding: EdgeInsets.zero, - onPressed: () => _removeAttachment(attachment), + onPressed: () => _removeAttachmentAt(index), child: Icon( CupertinoIcons.xmark_circle_fill, color: CupertinoColors.secondaryLabel.resolveFrom( @@ -375,7 +438,7 @@ class _ChatComposerBarState extends State { if (stop) { unawaited(composer.stop()); } else { - _send(); + unawaited(_send()); } } diff --git a/app/lib/features/chat/conversation/chat_composer_controller.dart b/app/lib/features/chat/conversation/chat_composer_controller.dart index 3f56cc9d..63e1caa6 100644 --- a/app/lib/features/chat/conversation/chat_composer_controller.dart +++ b/app/lib/features/chat/conversation/chat_composer_controller.dart @@ -14,6 +14,7 @@ class ChatComposerSend { required this.text, required this.operationId, required this.nativeMessageId, + this.attachments = const [], }); final String sessionId; @@ -23,6 +24,10 @@ class ChatComposerSend { 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; + /// Mirrors the web's `nativeChatMessageId(operationId) || local-`. String get optimisticId => nativeMessageId.isEmpty ? 'local-$operationId' : nativeMessageId; @@ -120,17 +125,22 @@ class ChatComposerController extends ChangeNotifier { return _persistDraft(text); } - /// Sends the non-blank draft as one plain message. Only the wire text is - /// trimmed. A refused unchanged intent reuses its operation id; a refusal - /// restores the exact draft unless newer text is already present. - Future send() async { - if (_disposed || _sending) return; + /// Sends the non-blank draft as one plain message, carrying [attachments] in + /// draft order. Only the wire text is trimmed. A refused unchanged intent + /// (same text and parts) reuses its operation id; a refusal restores the + /// exact draft unless newer text is already present. Returns whether the + /// server accepted this prompt, so the composer can clear its chips only + /// once the send is accepted. + Future send({List attachments = const []}) async { + if (_disposed || _sending) return false; final exact = _draft; final text = exact.trim(); - if (text.isEmpty) return; + if (text.isEmpty) return false; final previous = _pending; late final ChatComposerSend intent; - if (previous != null && previous.text == text) { + if (previous != null && + previous.text == text && + _sameAttachments(previous.attachments, attachments)) { intent = previous; } else { final operationId = _nextOperationId(); @@ -139,6 +149,7 @@ class ChatComposerController extends ChangeNotifier { text: text, operationId: operationId, nativeMessageId: nativeChatMessageId(operationId), + attachments: attachments, ); } _pending = intent; @@ -148,22 +159,24 @@ class ChatComposerController extends ChangeNotifier { _edited = true; _notify(); await _persistDraft(''); - if (_disposed) return; + if (_disposed) return false; onOptimistic?.call(intent); try { final reply = await _repository.prompt( sessionId, text, operationId: intent.operationId, + attachments: intent.attachments, ); - if (_disposed) return; + if (_disposed) return false; _queued = reply['chat_queued'] == true; _pending = null; _sending = false; _error = null; _notify(); + return true; } catch (error) { - if (_disposed) return; + if (_disposed) return false; final message = '$error'; _sending = false; _error = message; @@ -172,10 +185,11 @@ class ChatComposerController extends ChangeNotifier { _edited = true; _notify(); await _persistDraft(exact); - if (_disposed) return; + if (_disposed) return false; } onRejected?.call(intent, message); _notify(); + return false; } } @@ -215,6 +229,23 @@ class ChatComposerController extends ChangeNotifier { newOperationId?.call() ?? 'native-chat-${DateTime.now().microsecondsSinceEpoch}-${_operationSeq++}'; + /// Whether two prompts carry the same attachment parts, so a retry after a + /// refusal can keep the original operation id. Parts are the exact wire maps + /// this composer built, so a shallow key-by-key comparison is enough. + bool _sameAttachments(List a, List b) { + if (identical(a, b)) return true; + if (a.length != b.length) return false; + for (var index = 0; index < a.length; index += 1) { + final first = a[index]; + final second = b[index]; + if (first.length != second.length) return false; + for (final key in first.keys) { + if (!second.containsKey(key) || first[key] != second[key]) return false; + } + } + return true; + } + Future _persistDraft(String text) { final write = _writes.then((_) async { var stored = false; 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 new file mode 100644 index 00000000..3aaed834 --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart @@ -0,0 +1,299 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; +import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart'; + +import 'chat_composer_bar_fixtures.dart'; +import 'chat_composer_fixtures.dart'; + +/// A picker whose selections are supplied by the test, one call at a time. +class _FakePicker { + _FakePicker(this.results); + + final List?> results; + + ChatAttachmentPicker get picker => ChatAttachmentPicker( + select: () async => results.isEmpty ? null : results.removeAt(0), + ); +} + +ChatPickedFile _file( + String name, + List bytes, { + String mime = 'text/plain', +}) => ChatPickedFile(name: name, mime: mime, bytes: Uint8List.fromList(bytes)); + +List _wireParts(List files) => [ + for (final file in files) + {'name': file.name, 'mime': file.mime, 'data': base64Encode(file.bytes)}, +]; + +void main() { + group('ChatComposerController.send', () { + late FakeComposerGateway gateway; + late ChatComposerController controller; + var operationSeq = 0; + + setUp(() { + operationSeq = 0; + gateway = FakeComposerGateway(); + controller = ChatComposerController( + gateway, + 's', + drafts: MemoryChatDraftStore(), + newOperationId: () => 'op/${operationSeq += 1}', + ); + }); + + tearDown(() { + controller.dispose(); + gateway.close(); + }); + + test( + 'carries the exact parts in draft order with the text and id', + () async { + final parts = _wireParts([ + _file('screen.png', [0, 1, 2], mime: 'image/png'), + _file('notes.txt', utf8.encode('hello')), + ]); + await controller.setDraft(' hello '); + + final accepted = await controller.send(attachments: parts); + + expect(accepted, isTrue); + expect(gateway.calls.single.fields, { + 'chat_prompt': { + 'session_id': 's', + 'text': 'hello', + 'operation_id': 'op/1', + 'native_message_id': 'msg_op1', + 'attachments': parts, + }, + }); + expect(controller.draft, ''); + }, + ); + + test('a refused prompt reuses its id only for matching parts', () async { + gateway.prompt = (_, _) async => throw StateError('response lost'); + final first = _wireParts([ + _file('notes.txt', [1]), + ]); + final second = _wireParts([ + _file('notes.txt', [2]), + ]); + await controller.setDraft('hello'); + + await controller.send(attachments: first); + await controller.send(attachments: second); + await controller.send( + attachments: _wireParts([ + _file('notes.txt', [2]), + ]), + ); + + expect(gateway.promptBodies.map((body) => body['operation_id']), [ + 'op/1', + 'op/2', + 'op/2', + ]); + expect(controller.draft, 'hello'); + }); + }); + + group('ChatComposerBar send', () { + late ComposerBarGateway gateway; + late ChatComposerController composer; + late List controllers; + + setUp(() { + gateway = ComposerBarGateway(); + controllers = []; + }); + + tearDown(() { + for (final controller in controllers) { + controller.dispose(); + } + gateway.close(); + }); + + Future pumpBar( + WidgetTester tester, + ChatAttachmentPicker picker, + ) async { + composer = ChatComposerController( + gateway, + 's', + drafts: MemoryChatDraftStore(), + newOperationId: () => 'op/1', + ); + controllers.add(composer); + await tester.pumpWidget( + materialHost( + ChatComposerBar(composer: composer, running: false, picker: picker), + ), + ); + await tester.pump(); + } + + Future pickFiles(WidgetTester tester) async { + await tester.tap(find.byKey(const Key('chat-composer-attach'))); + await tester.pumpAndSettle(); + } + + Future typeAndSend(WidgetTester tester, String text) async { + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + text, + ); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + } + + testWidgets( + 'Send carries the chips in order and clears them on acceptance', + (tester) async { + final files = [ + _file('screen.png', [0, 1, 2], mime: 'image/png'), + _file('notes.txt', utf8.encode('hello')), + ]; + await pumpBar(tester, _FakePicker([files]).picker); + + await pickFiles(tester); + expect(find.text('screen.png'), findsOneWidget); + expect(find.text('notes.txt'), findsOneWidget); + + await typeAndSend(tester, ' hello '); + + final body = gateway.promptBodies.single; + expect(body['text'], 'hello'); + expect(body['operation_id'], 'op/1'); + expect(body['attachments'], _wireParts(files)); + expect( + find.byKey(const Key('chat-composer-attachments')), + findsNothing, + ); + expect(find.text('screen.png'), findsNothing); + expect(fieldText(tester), ''); + }, + ); + + testWidgets('removing one duplicate name sends the other file', ( + tester, + ) async { + // Same name, type and size, so only position tells the two picks apart. + final files = [ + _file('notes.txt', utf8.encode('aaaaa')), + _file('notes.txt', utf8.encode('bbbbb')), + ]; + await pumpBar(tester, _FakePicker([files]).picker); + + await pickFiles(tester); + expect(find.text('notes.txt'), findsNWidgets(2)); + + await tester.tap( + find.descendant( + of: find.byKey(const ValueKey('chat-composer-attachment-1')), + matching: find.byTooltip('Remove notes.txt'), + ), + ); + await tester.pump(); + expect(find.text('notes.txt'), findsOneWidget); + + await typeAndSend(tester, 'hello'); + + expect( + gateway.promptBodies.single['attachments'], + _wireParts([files[0]]), + ); + expect(find.byKey(const Key('chat-composer-attachments')), findsNothing); + }); + + testWidgets('a refusal restores the exact draft and keeps the chips', ( + tester, + ) async { + gateway.prompt = (_, _) async => throw StateError('refused'); + final file = _file('notes.txt', utf8.encode('keep')); + await pumpBar( + tester, + _FakePicker([ + [file], + ]).picker, + ); + + await pickFiles(tester); + await typeAndSend(tester, ' keep me '); + + expect(gateway.promptBodies.single['attachments'], _wireParts([file])); + expect(fieldText(tester), ' keep me '); + expect( + find.byKey(const Key('chat-composer-attachments')), + findsOneWidget, + ); + expect(find.text('notes.txt'), findsOneWidget); + }); + + testWidgets( + 'Send is disabled while a prompt with attachments is in flight', + (tester) async { + final reply = Completer(); + gateway.prompt = (_, _) => reply.future; + final file = _file('notes.txt', [1, 2, 3]); + await pumpBar( + tester, + _FakePicker([ + [file], + ]).picker, + ); + + await pickFiles(tester); + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'hello', + ); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pump(); + + FilledButton primary() => tester.widget( + find.byKey(const Key('chat-composer-primary')), + ); + IconButton attach() => tester.widget( + find.byKey(const Key('chat-composer-attach')), + ); + + expect(primary().onPressed, isNull); + expect(attach().onPressed, isNull); + expect(gateway.promptBodies.single['attachments'], _wireParts([file])); + // The chips stay until the prompt is accepted. + expect( + find.byKey(const Key('chat-composer-attachments')), + findsOneWidget, + ); + + reply.complete(const {}); + await tester.pumpAndSettle(); + + expect( + find.byKey(const Key('chat-composer-attachments')), + findsNothing, + ); + expect( + primary().onPressed, + isNull, + reason: 'the accepted draft is empty', + ); + }, + ); + }); +}