diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 11fc5d7a..6f646a7d 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -4,6 +4,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import '../domain/chat_attachment_draft.dart'; +import '../domain/chat_attachment_picker.dart'; import '../ui/sessions/chat_adaptive.dart'; import 'chat_composer_controller.dart'; @@ -14,6 +16,9 @@ const String chatComposerPlaceholder = 'Message the agent… or / for commands'; const String chatSendLabel = 'Send'; const String chatStopLabel = 'Stop'; +/// The attach control's accessible name, verbatim from the web composer. +const String chatAttachLabel = 'Attach file'; + /// Shown while the reply says this prompt waits behind the running turn. const String chatQueuedNotice = 'Queued — it will send when the current turn finishes.'; @@ -24,13 +29,15 @@ const String chatQueuedNotice = /// The field owns only its [TextEditingController] and focus; the durable draft /// text lives in the composer controller, so a live transcript rebuild never /// replaces the operator's caret. Enter inserts a newline and Ctrl/Cmd+Enter -/// sends the plain message. Attachments and the slash palette are not part of -/// this slice. +/// 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. class ChatComposerBar extends StatefulWidget { const ChatComposerBar({ super.key, required this.composer, required this.running, + this.picker, }); final ChatComposerController composer; @@ -38,6 +45,9 @@ class ChatComposerBar extends StatefulWidget { /// The thread controller's running bit; it alone decides Send versus Stop. final bool running; + /// The host picker seam; production uses [hostChatAttachmentPicker]. + final ChatAttachmentPicker? picker; + @override State createState() => _ChatComposerBarState(); } @@ -47,6 +57,10 @@ class _ChatComposerBarState extends State { text: widget.composer.draft, ); final FocusNode _focus = FocusNode(debugLabel: 'chat-composer'); + final ChatAttachmentDraft _attachments = ChatAttachmentDraft(); + String? _attachmentError; + + ChatAttachmentPicker get _picker => widget.picker ?? hostChatAttachmentPicker; @override void initState() { @@ -100,6 +114,37 @@ class _ChatComposerBarState extends State { _focus.requestFocus(); } + /// Adds the operator's explicit selection through the host seam. A dismissed + /// picker changes nothing; a bounded draft refusal is shown in its own words + /// while every attachment already in the draft stays. + Future _pickAttachments() async { + if (widget.composer.sending || !_picker.isSupported) return; + List? files; + try { + files = await _picker.pick(); + } catch (error) { + if (!mounted) return; + setState(() => _attachmentError = '$error'); + return; + } + if (!mounted || files == null) return; + final picked = files; + setState(() { + _attachmentError = null; + for (final file in picked) { + final refusal = _attachments.add(file.attachment); + if (refusal != null) _attachmentError = refusal.message; + } + }); + } + + void _removeAttachment(ChatAttachment attachment) { + setState(() { + _attachments.remove(attachment); + _attachmentError = null; + }); + } + @override void dispose() { widget.composer.removeListener(_syncDraft); @@ -126,6 +171,15 @@ class _ChatComposerBarState extends State { mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ + if (!_attachments.isEmpty) _attachmentStrip(context, cupertino), + if (_attachmentError != null) + _notice( + context, + cupertino, + key: const Key('chat-composer-attachment-error'), + text: _attachmentError!, + failed: true, + ), if (composer.queued) _notice( context, @@ -144,6 +198,8 @@ class _ChatComposerBarState extends State { Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ + _attach(context, cupertino), + const SizedBox(width: 8), Expanded( child: Focus( canRequestFocus: false, @@ -164,6 +220,118 @@ class _ChatComposerBarState extends State { ); } + /// The attach control: a host button that opens the picker, disabled when the + /// host has no picker or one prompt is already awaiting its reply. + Widget _attach(BuildContext context, bool cupertino) { + final enabled = _picker.isSupported && !widget.composer.sending; + final onPressed = enabled ? () => unawaited(_pickAttachments()) : null; + if (cupertino) { + return Semantics( + label: chatAttachLabel, + button: true, + enabled: enabled, + child: ExcludeSemantics( + child: CupertinoButton( + key: const Key('chat-composer-attach'), + padding: EdgeInsets.zero, + onPressed: onPressed, + child: const Icon(CupertinoIcons.paperclip), + ), + ), + ); + } + return IconButton( + key: const Key('chat-composer-attach'), + tooltip: chatAttachLabel, + onPressed: onPressed, + icon: const Icon(Icons.attach_file), + ); + } + + /// The files attached to the next message. Each chip carries only the + /// operator's recorded name and a native remove action. + Widget _attachmentStrip(BuildContext context, bool cupertino) { + final attachments = _attachments.attachments; + return Padding( + key: const Key('chat-composer-attachments'), + padding: const EdgeInsetsDirectional.only(bottom: 8), + child: Wrap( + spacing: 8, + runSpacing: 4, + children: [ + for (var index = 0; index < attachments.length; index += 1) + if (cupertino) + _cupertinoChip(context, attachments[index], index) + else + _materialChip(attachments[index], index), + ], + ), + ); + } + + Widget _materialChip(ChatAttachment attachment, int index) => Chip( + key: ValueKey('chat-composer-attachment-$index'), + label: Text(attachment.name), + onDeleted: () => _removeAttachment(attachment), + deleteButtonTooltipMessage: 'Remove ${attachment.name}', + ); + + /// Cupertino has no chip control, so the host's capsule fill and its own + /// button carry the same name and remove action. + Widget _cupertinoChip( + BuildContext context, + ChatAttachment attachment, + int index, + ) { + return Semantics( + container: true, + explicitChildNodes: true, + child: DefaultTextStyle.merge( + style: CupertinoTheme.of(context).textTheme.textStyle, + child: DecoratedBox( + key: ValueKey('chat-composer-attachment-$index'), + decoration: ShapeDecoration( + shape: const StadiumBorder(), + color: CupertinoColors.secondarySystemFill.resolveFrom(context), + ), + child: Padding( + padding: const EdgeInsetsDirectional.fromSTEB(12, 4, 4, 4), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Flexible( + child: Text(attachment.name, overflow: TextOverflow.ellipsis), + ), + const SizedBox(width: 4), + Semantics( + container: true, + button: true, + label: 'Remove ${attachment.name}', + onTap: () => _removeAttachment(attachment), + child: ExcludeSemantics( + child: CupertinoButton( + key: ValueKey( + 'chat-composer-attachment-remove-$index', + ), + padding: EdgeInsets.zero, + onPressed: () => _removeAttachment(attachment), + child: Icon( + CupertinoIcons.xmark_circle_fill, + color: CupertinoColors.secondaryLabel.resolveFrom( + context, + ), + ), + ), + ), + ), + ], + ), + ), + ), + ), + ); + } + Widget _field(bool cupertino) { final composer = widget.composer; void changed(String value) => unawaited(composer.setDraft(value)); @@ -191,6 +359,7 @@ class _ChatComposerBarState extends State { onChanged: changed, decoration: const InputDecoration( hintText: chatComposerPlaceholder, + hintMaxLines: 1, border: OutlineInputBorder(), ), ); diff --git a/app/test/features/chat/conversation/chat_composer_attachment_test.dart b/app/test/features/chat/conversation/chat_composer_attachment_test.dart new file mode 100644 index 00000000..1ceef5fc --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_attachment_test.dart @@ -0,0 +1,246 @@ +import 'dart:async'; +import 'dart:typed_data'; + +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/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_draft.dart'; +import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart'; + +import 'chat_composer_bar_fixtures.dart'; + +/// A picker whose selections are supplied by the test, one call at a time. +class _FakePicker { + _FakePicker(this.results); + + final List?> results; + int calls = 0; + + ChatAttachmentPicker get picker => ChatAttachmentPicker( + select: () async { + calls += 1; + return results.isEmpty ? null : results.removeAt(0); + }, + ); +} + +ChatPickedFile _file(String name, {int size = 3, String mime = 'text/plain'}) => + ChatPickedFile(name: name, mime: mime, bytes: Uint8List(size)); + +Widget _host(Widget bar, {required bool cupertino}) => cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: bar)) + : materialHost(bar); + +void main() { + late ComposerBarGateway gateway; + late ChatComposerController composer; + late List controllers; + + setUp(() { + gateway = ComposerBarGateway(); + controllers = []; + }); + + /// Created inside the test body so the controller's futures belong to the + /// widget tester's clock rather than to `setUp`'s. + ChatComposerController newComposer() { + final controller = ChatComposerController( + gateway, + 's', + drafts: MemoryChatDraftStore(), + ); + controllers.add(controller); + composer = controller; + return controller; + } + + tearDown(() { + for (final controller in controllers) { + controller.dispose(); + } + gateway.close(); + }); + + Future pumpBar( + WidgetTester tester, { + ChatAttachmentPicker? picker, + bool cupertino = false, + }) async { + newComposer(); + await tester.pumpWidget( + _host( + ChatComposerBar(composer: composer, running: false, picker: picker), + cupertino: cupertino, + ), + ); + await tester.pump(); + } + + for (final cupertino in [false, true]) { + testWidgets('picking a file shows its recorded name and removes it ' + '(${cupertino ? 'cupertino' : 'material'})', (tester) async { + final fake = _FakePicker([ + [_file('notes.txt')], + ]); + await pumpBar(tester, picker: fake.picker, cupertino: cupertino); + + await tester.tap(find.byKey(const Key('chat-composer-attach'))); + await tester.pumpAndSettle(); + + expect(fake.calls, 1); + expect( + find.byKey(const Key('chat-composer-attachments')), + findsOneWidget, + ); + expect(find.text('notes.txt'), findsOneWidget); + + if (cupertino) { + await tester.tap( + find.byKey(const Key('chat-composer-attachment-remove-0')), + ); + } else { + await tester.tap(find.byTooltip('Remove notes.txt')); + } + await tester.pump(); + + expect(find.text('notes.txt'), findsNothing); + expect(find.byKey(const Key('chat-composer-attachments')), findsNothing); + }); + } + + testWidgets('a file over the draft budget shows the draft refusal line', ( + tester, + ) async { + final fake = _FakePicker([ + [ + _file( + 'huge.bin', + size: ChatAttachmentDraft.maxTotalBytes + 1, + mime: 'application/octet-stream', + ), + ], + ]); + await pumpBar(tester, picker: fake.picker); + + await tester.tap(find.byKey(const Key('chat-composer-attach'))); + await tester.pumpAndSettle(); + + expect( + find.text(ChatAttachmentRefusal.totalTooLarge.message), + findsOneWidget, + ); + expect(find.byKey(const Key('chat-composer-attachments')), findsNothing); + expect(find.text('huge.bin'), findsNothing); + }); + + testWidgets('attachments survive a normal parent rebuild', (tester) async { + newComposer(); + final fake = _FakePicker([ + [_file('keep me.pdf', mime: 'application/pdf')], + ]); + var rebuilt = false; + late StateSetter rebuild; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: StatefulBuilder( + builder: (context, setState) { + rebuild = setState; + return ChatComposerBar( + composer: composer, + running: rebuilt, + picker: fake.picker, + ); + }, + ), + ), + ), + ); + await tester.pump(); + + await tester.tap(find.byKey(const Key('chat-composer-attach'))); + await tester.pumpAndSettle(); + expect(find.text('keep me.pdf'), findsOneWidget); + + rebuild(() => rebuilt = true); + await tester.pump(); + + expect(find.text('keep me.pdf'), findsOneWidget); + expect(find.byKey(const Key('chat-composer-attachments')), findsOneWidget); + }); + + testWidgets('the attach control is disabled while a prompt is in flight', ( + tester, + ) async { + final reply = Completer(); + gateway.prompt = (_, _) => reply.future; + final fake = _FakePicker([ + [_file('notes.txt')], + ]); + await pumpBar(tester, picker: fake.picker); + + IconButton attach() => tester.widget( + find.byKey(const Key('chat-composer-attach')), + ); + expect(attach().onPressed, isNotNull); + + 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.pumpAndSettle(); + + expect(attach().onPressed, isNull); + expect(gateway.promptBodies, hasLength(1)); + + reply.complete(const {}); + await tester.pumpAndSettle(); + expect(attach().onPressed, isNotNull); + }); + + testWidgets('the attach control is disabled without a host picker', ( + tester, + ) async { + await pumpBar(tester); + + final attach = tester.widget( + find.byKey(const Key('chat-composer-attach')), + ); + expect(attach.onPressed, isNull); + }); + + for (final cupertino in [false, true]) { + testWidgets('a long attachment name fits a narrow 2x composer ' + '(${cupertino ? 'cupertino' : 'material'})', (tester) async { + tester.view.physicalSize = const Size(640, 1200); + tester.view.devicePixelRatio = 2; + tester.platformDispatcher.textScaleFactorTestValue = 2; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); + + final name = + 'a-very-long-recorded-attachment-name-that-must-not-overflow.pdf'; + final fake = _FakePicker([ + [_file(name)], + ]); + await pumpBar(tester, picker: fake.picker, cupertino: cupertino); + + await tester.tap(find.byKey(const Key('chat-composer-attach'))); + await tester.pumpAndSettle(); + + expect(tester.takeException(), isNull); + expect(find.text(name), findsOneWidget); + expect( + find.byKey(const Key('chat-composer-attachments')), + findsOneWidget, + ); + }); + } +}