diff --git a/app/PARITY.md b/app/PARITY.md index 6f3fccf4..d1f4667f 100644 --- a/app/PARITY.md +++ b/app/PARITY.md @@ -44,8 +44,8 @@ or scratch servers only, never real operator sessions/runs/workflows. | Chat history/live transport | Preview: `/chat` list and `/chat/:id` history, paging, live failure notice; composer integrated with the thread | run-detail-style reconnect verification in the real app | | Chat message surfaces | Preview: formatted assistant markdown (safe links/headings/lists/tables/fences), user text exact, disclosures plain | exact attachments, unknown-part rendering beyond retained placeholders | | Chat activity/reasoning/tools | Preview: one activity line, expanded native overlay with lifecycle/input/output/error/duration/edit diff | compaction wording variants | -| Chat composer | Preview: field, Send/Stop, persisted draft, queued/refusal lines; attachments sent with the prompt (87bb518c); slash palette with no-send routing (63babc59); one verified send in the real app flow | paste; slash command actions | -| Chat attachments | Preview: host picker, bounded 1 MiB draft, composer chips, base64 wire payload, sent with the prompt (87bb518c) | paste, history reconciliation | +| Chat composer | Preview: field, Send/Stop, persisted draft, queued/refusal lines; attachments sent with the prompt (87bb518c); slash palette with no-send routing (63babc59); file and text paste (copied text keeps the field's caret and draft); one verified send in the real app flow | slash command actions | +| Chat attachments | Preview: host picker, pasted files through the same bounded 1 MiB draft, composer chips, base64 wire payload, sent with the prompt (87bb518c) | history reconciliation | | Chat permissions/questions | Preview: native approval/risk and question cards in the conversation view, Chrome fixture captures under the C4 probe (`notes/C4-approvals.md`) | authenticated smoke with a real pending approval/question; live multi-client resolution; no production decision sent | | Chat model/Info/background tasks | Preview: numeric Info sheet (unknown != 0); header model control and chooser (`notes/chat-model-picker.md`); `Background tasks` bar with `N running`, capped scrolling rows, selected-task detail and one bounded `chat_shell_output` page (cursor 0, 64 KiB) that reports empty/unavailable/refused states, re-read on reconnect; the bar keeps half the viewport and the output pane twelve scaled lines (run 139 + manager fix; widget tests, published, no rendered capture yet) | rendered browser captures of the model chooser and the task bar; authenticated smoke with a real running shell | | Chat commands/notices/scroll | Preview: dismissible local notices with reconnect clearing; slash palette with the unknown-command report and `Send as message` / `Cancel` (63babc59, `notes/C9-commands.md`); streamed follow and restored read position (e4c196d7) | command actions `/new`, `/close`, `/rename`, `/stop`, `/help` | diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 3b2a2089..343aa3b0 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -8,6 +8,7 @@ import '../../../core/api_gateway.dart'; import '../domain/chat_attachment_draft.dart'; import '../domain/chat_attachment_picker.dart'; import '../domain/chat_attachment_wire.dart'; +import '../domain/chat_clipboard_paste.dart'; import '../domain/chat_commands.dart'; import '../ui/sessions/chat_adaptive.dart'; import 'chat_command_palette.dart'; @@ -47,13 +48,16 @@ const String chatQueuedNotice = /// their names are shown with the host's own remove action and the bounded /// draft owns every 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. +/// refusal. Pasting files takes the same draft path as the picker; pasting text +/// stays with the host field, so its caret, selection and draft behave exactly +/// as typing does. class ChatComposerBar extends StatefulWidget { const ChatComposerBar({ super.key, required this.composer, required this.running, this.picker, + this.paste, }); final ChatComposerController composer; @@ -64,6 +68,9 @@ class ChatComposerBar extends StatefulWidget { /// The host picker seam; production uses [hostChatAttachmentPicker]. final ChatAttachmentPicker? picker; + /// The host clipboard-file seam; production uses [hostChatClipboardPaste]. + final ChatClipboardPaste? paste; + @override State createState() => _ChatComposerBarState(); } @@ -95,11 +102,15 @@ class _ChatComposerBarState extends State { String? _commandOffer; ChatAttachmentPicker get _picker => widget.picker ?? hostChatAttachmentPicker; + ChatClipboardPaste get _paste => widget.paste ?? hostChatClipboardPaste; + + ChatPasteUnsubscribe? _stopPaste; @override void initState() { super.initState(); widget.composer.addListener(_syncDraft); + _listenForPaste(); unawaited(widget.composer.loadDraft()); } @@ -111,6 +122,42 @@ class _ChatComposerBarState extends State { widget.composer.addListener(_syncDraft); _syncDraft(); } + if (oldWidget.paste != widget.paste) { + _stopListeningForPaste(); + _listenForPaste(); + } + } + + void _listenForPaste() { + if (!_paste.isSupported) return; + _stopPaste = _paste.listen( + onFiles: _onPastedFiles, + canAccept: _canAcceptPaste, + onError: _onPasteError, + ); + } + + void _stopListeningForPaste() { + final stop = _stopPaste; + _stopPaste = null; + stop?.call(); + } + + /// A pasted file is taken only while this composer owns the field and no + /// prompt is in flight, which is the same window as the attach control. The + /// host checks this before it suppresses the paste's default behaviour, so a + /// paste elsewhere is never swallowed. + bool _canAcceptPaste() => + mounted && _focus.hasFocus && !widget.composer.sending; + + void _onPastedFiles(List files) { + if (!mounted) return; + _addFiles(files); + } + + void _onPasteError(Object error) { + if (!mounted) return; + setState(() => _attachmentError = '$error'); } /// External draft changes (saved-draft load, send clear, refusal restore) @@ -322,12 +369,19 @@ class _ChatComposerBarState extends State { return; } if (!mounted || files == null) return; - final picked = files; + _addFiles(files); + } + + /// Adds each file through the landed attachment draft. A refusal keeps every + /// attachment already in the draft and shows the draft's own words; the + /// refused file's bytes are never retained. Picking and pasting share this + /// path so both apply the same name and 1 MiB decisions. + void _addFiles(List files) { + if (!mounted) return; setState(() { _attachmentError = null; - for (final file in picked) { - final attachment = file.attachment; - final refusal = _attachments.add(attachment); + for (final file in files) { + final refusal = _attachments.add(file.attachment); if (refusal != null) { _attachmentError = refusal.message; } else { @@ -377,6 +431,7 @@ class _ChatComposerBarState extends State { @override void dispose() { + _stopListeningForPaste(); widget.composer.removeListener(_syncDraft); _text.dispose(); _focus.dispose(); diff --git a/app/lib/features/chat/domain/chat_clipboard_paste.dart b/app/lib/features/chat/domain/chat_clipboard_paste.dart new file mode 100644 index 00000000..3aa60707 --- /dev/null +++ b/app/lib/features/chat/domain/chat_clipboard_paste.dart @@ -0,0 +1,82 @@ +import 'chat_attachment_picker.dart'; +import 'chat_clipboard_paste_stub.dart' + if (dart.library.js_interop) 'chat_clipboard_paste_web.dart' + as impl; + +/// Receives the files one paste carried, in clipboard order. +typedef ChatPasteFiles = void Function(List files); + +/// Whether the composer would take a pasted file right now. The host asks this +/// before it suppresses the paste's default behaviour. +typedef ChatPasteCanAccept = bool Function(); + +/// Reports a clipboard file whose bytes could not be read. +typedef ChatPasteFailed = void Function(Object error); + +/// Stops observing paste events for one composer. +typedef ChatPasteUnsubscribe = void Function(); + +/// Registers [onFiles] for file pastes the composer may take and returns the +/// cancellation for that registration. +/// +/// A text-only paste is never reported: it stays with the host field, so caret, +/// selection and the draft update exactly as typing does. +typedef ChatPasteSubscribe = + ChatPasteUnsubscribe Function( + ChatPasteFiles onFiles, + ChatPasteCanAccept canAccept, + ChatPasteFailed onError, + ); + +/// The clipboard seam for one host. +/// +/// The web host observes the browser's own `paste` event. A paste carrying at +/// least one file is consumed ([ChatPasteCanAccept] permitting) and handed to +/// the composer as [ChatPickedFile] rows, so the landed attachment draft owns +/// every size decision and refusal. A clipboard file the browser recorded +/// without a name takes the web client's pasted name. A host without +/// clipboard-file paste constructs [ChatClipboardPaste.unsupported] and the +/// composer simply keeps its native text behaviour. Tests inject a fake +/// [subscribe]. +class ChatClipboardPaste { + const ChatClipboardPaste({required ChatPasteSubscribe this.subscribe}) + : unsupportedMessage = null; + + const ChatClipboardPaste.unsupported(String message) + : subscribe = null, + unsupportedMessage = message; + + /// The host's paste listener, or null when this host cannot observe files. + final ChatPasteSubscribe? subscribe; + + /// Why this host cannot observe pasted files, when [subscribe] is null. + final String? unsupportedMessage; + + bool get isSupported => subscribe != null; + + /// Starts observing the host's paste event. A host without the capability + /// throws [UnsupportedError] with [unsupportedMessage]. + ChatPasteUnsubscribe listen({ + required ChatPasteFiles onFiles, + required ChatPasteCanAccept canAccept, + required ChatPasteFailed onError, + }) { + final subscribe = this.subscribe; + if (subscribe == null) throw UnsupportedError(unsupportedMessage!); + return subscribe(onFiles, canAccept, onError); + } +} + +/// The web composer's recorded name for a clipboard file that arrived without +/// one: the MIME subtype becomes the extension, with the web client's `jpeg` +/// to `jpg` mapping and `bin` fallback. +String chatPastedAttachmentName(String mime, int sequence) { + final subtype = mime.split('/').last.toLowerCase(); + final extension = subtype == 'jpeg' + ? 'jpg' + : subtype.replaceAll(RegExp('[^a-z0-9]'), ''); + return 'pasted-$sequence.${extension.isEmpty ? 'bin' : extension}'; +} + +/// The clipboard seam for the running host. +ChatClipboardPaste get hostChatClipboardPaste => impl.hostChatClipboardPaste; diff --git a/app/lib/features/chat/domain/chat_clipboard_paste_stub.dart b/app/lib/features/chat/domain/chat_clipboard_paste_stub.dart new file mode 100644 index 00000000..3b390d00 --- /dev/null +++ b/app/lib/features/chat/domain/chat_clipboard_paste_stub.dart @@ -0,0 +1,12 @@ +import 'chat_clipboard_paste.dart'; + +/// Native fallback for clipboard-file paste. +/// +/// The composer keeps the host field's own text paste on every platform; this +/// slice observes clipboard files on web only. A non-web host reports that +/// missing capability rather than pretending no paste happened. +const String chatClipboardPasteUnsupportedMessage = + 'Pasting files is not supported on this platform yet.'; + +ChatClipboardPaste get hostChatClipboardPaste => + const ChatClipboardPaste.unsupported(chatClipboardPasteUnsupportedMessage); diff --git a/app/lib/features/chat/domain/chat_clipboard_paste_web.dart b/app/lib/features/chat/domain/chat_clipboard_paste_web.dart new file mode 100644 index 00000000..599c638a --- /dev/null +++ b/app/lib/features/chat/domain/chat_clipboard_paste_web.dart @@ -0,0 +1,126 @@ +import 'dart:async'; +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; + +import 'chat_attachment_picker.dart'; +import 'chat_clipboard_paste.dart'; + +/// The browser's clipboard-file paste, observed at the document once per +/// composer. +/// +/// Declared with `dart:js_interop` rather than `package:web`, so the same source +/// compiles for the JavaScript and WebAssembly web targets. A paste carrying no +/// file is left completely alone: the field's own text paste keeps its caret, +/// selection and draft behaviour. Only a paste with files the composer will +/// accept is consumed, and its bytes are read before the files join the +/// composer's attachment draft. +@JS('document') +external _Document get _document; + +extension type _Document._(JSObject _) implements JSObject { + external void addEventListener(JSString type, JSFunction listener); + + external void removeEventListener(JSString type, JSFunction listener); +} + +extension type _ClipboardEvent._(JSObject _) implements JSObject { + external bool get defaultPrevented; + + external _ClipboardData? get clipboardData; + + external void preventDefault(); +} + +extension type _ClipboardData._(JSObject _) implements JSObject { + external _DataTransferItemList get items; +} + +extension type _DataTransferItemList._(JSObject _) implements JSObject { + external int get length; +} + +extension type _DataTransferItem._(JSObject _) implements JSObject { + external JSString get kind; + + external _ClipboardFile? getAsFile(); +} + +extension type _ClipboardFile._(JSObject _) implements JSObject { + external JSString get name; + + external JSString get type; + + external JSPromise arrayBuffer(); +} + +ChatClipboardPaste get hostChatClipboardPaste => + const ChatClipboardPaste(subscribe: _subscribeToPaste); + +ChatPasteUnsubscribe _subscribeToPaste( + ChatPasteFiles onFiles, + ChatPasteCanAccept canAccept, + ChatPasteFailed onError, +) { + var sequence = 0; + + void handlePaste(JSObject raw) { + final event = _ClipboardEvent._(raw); + if (event.defaultPrevented) return; + final data = event.clipboardData; + if (data == null) return; + final items = data.items; + final files = <_ClipboardFile>[]; + for (var index = 0; index < items.length; index += 1) { + // DataTransferItemList is indexed, not a method list like FileList. + final raw = items.getProperty('$index'.toJS); + if (raw == null) continue; + final item = _DataTransferItem._(raw); + if (item.kind.toDart != 'file') continue; + final file = item.getAsFile(); + if (file != null) files.add(file); + } + // A text-only clipboard keeps the field's native paste untouched. + if (files.isEmpty || !canAccept()) return; + event.preventDefault(); + unawaited(_readFiles(files, onFiles, onError, () => ++sequence)); + } + + final listener = handlePaste.toJS; + _document.addEventListener('paste'.toJS, listener); + return () => _document.removeEventListener('paste'.toJS, listener); +} + +Future _readFiles( + List<_ClipboardFile> files, + ChatPasteFiles onFiles, + ChatPasteFailed onError, + int Function() nextSequence, +) async { + final pasted = []; + final failures = []; + for (final file in files) { + try { + final buffer = await file.arrayBuffer().toDart; + final mime = file.type.toDart; + final recorded = file.name.toDart.trim(); + pasted.add( + ChatPickedFile( + name: recorded.isEmpty + ? chatPastedAttachmentName(mime, nextSequence()) + : recorded, + mime: mime, + bytes: buffer.toDart.asUint8List(), + ), + ); + } catch (error) { + // One unreadable clipboard file must not hide the others. + failures.add(error); + } + } + // Deliver the readable files first: the composer's add clears an older + // error, so a partial failure is reported after its kept files. + if (pasted.isNotEmpty) onFiles(pasted); + for (final failure in failures) { + onError(failure); + } +} diff --git a/app/notes/composer-paste.md b/app/notes/composer-paste.md new file mode 100644 index 00000000..a38d55b6 --- /dev/null +++ b/app/notes/composer-paste.md @@ -0,0 +1,71 @@ +# Composer paste + +Files and text can be pasted into the chat composer. Pasted files take the same +landed attachment draft path as the picker; text paste stays with the host field +and behaves exactly like typing. + +## Files + +- `lib/features/chat/domain/chat_clipboard_paste.dart` — the host seam + (`ChatClipboardPaste.listen(onFiles, canAccept, onError)`), the web client's + nameless-file name (`pasted-.`, `jpeg` → `jpg`, `bin` + fallback), and the running host's source. +- `lib/features/chat/domain/chat_clipboard_paste_web.dart` — a document-level + `paste` listener using `dart:js_interop` (JS and Wasm targets). It collects the + clipboard's file items synchronously, asks the composer whether it can take + them, consumes only that file paste (`preventDefault`), then reads the bytes + and hands back `ChatPickedFile`s. A text-only paste is never consumed. + Readable files are delivered before a partial read's failure, so the kept + files cannot clear the error line. + `DataTransferItemList` is indexed (`items[i]`), not a method list like + `FileList`; the first browser run caught the wrong `item()` call. +- `lib/features/chat/domain/chat_clipboard_paste_stub.dart` — non-web host: text + paste stays native, and the file-paste capability is reported as unsupported + instead of pretending it worked. +- `lib/features/chat/conversation/chat_composer_bar.dart` — subscribes once per + composer, unsubscribes on source replacement and dispose, and routes both + picker and paste files through `_addFiles`, so the landed + `ChatAttachmentDraft.add` owns the blank-name and 1 MiB decisions. Paste is + accepted only while this composer's field has focus and no prompt is in + flight, the same window as the attach control. No layout was changed. + +## Checks run + +- `flutter test test/features/chat` — 367 passed. +- `flutter test` — 1591 passed. +- `flutter test test/features/chat/conversation/chat_composer_paste_test.dart + test/features/chat/domain/chat_clipboard_paste_test.dart` — 14 passed. +- `flutter analyze` — no issues. +- `flutter build web` — built (the web seam compiles for the JS target; the + build also reports a successful Wasm dry run). +- `dart format` on touched Dart files — clean. + +## Browser evidence + +`tool/chat_paste_probe.mjs` builds the existing `tool/chat_composer_probe_entry.dart` +fixture, serves it on an OS-assigned loopback port, and drives real Chrome over +CDP. It dispatches native `ClipboardEvent`s carrying a `DataTransfer` at the +focused editing element. Result: + +- a text-only paste is **not** intercepted (`defaultPrevented: false`); +- a file paste is consumed (`defaultPrevented: true`), shows a `shot.png` chip, + and leaves the typed draft `keep me` untouched; +- a 1 MiB + 1 byte file shows the exact draft refusal and adds no `huge.bin` + chip while the existing chip stays; +- a nameless `image/png` clipboard file shows as `pasted-1.png`. + +Captures (in the run's shared artifacts, `public/paste-probe/`): +`paste-file-chip.png`, `paste-over-limit.png`, `paste-nameless-file.png`. + +The synthetic browser paste cannot exercise Chrome's default text insertion, so +the text caret/selection/draft assertion is a widget test that sends the +platform's own editing update to the field. The browser check proves the listener +does not consume text-only pastes. + +## Limits + +- Web only, as required. Android/iOS/desktop were not built or tested; on those + hosts text paste stays native and pasted files remain unsupported until a + native clipboard-file seam exists. +- Attachments stay memory-only and are not restored with a draft, matching the + landed draft store and the web client. diff --git a/app/test/features/chat/conversation/chat_composer_paste_test.dart b/app/test/features/chat/conversation/chat_composer_paste_test.dart new file mode 100644 index 00000000..e100c40e --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_paste_test.dart @@ -0,0 +1,340 @@ +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_draft.dart'; +import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart'; +import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart'; + +import 'chat_composer_bar_fixtures.dart'; + +/// A host paste source driven by the test. [paste] mirrors the web listener: +/// it asks [canAccept] before it hands files to the composer. +class _FakePaste { + ChatPasteFiles? onFiles; + ChatPasteCanAccept? canAccept; + ChatPasteFailed? onError; + int listens = 0; + int stops = 0; + + ChatClipboardPaste get source => ChatClipboardPaste( + subscribe: (onFiles, canAccept, onError) { + listens += 1; + this.onFiles = onFiles; + this.canAccept = canAccept; + this.onError = onError; + return () { + stops += 1; + this.onFiles = null; + this.canAccept = null; + this.onError = null; + }; + }, + ); + + void paste(List files) { + final handler = onFiles; + if (handler == null) return; + if (!(canAccept?.call() ?? false)) return; + handler(files); + } +} + +ChatPickedFile _file( + String name, + List bytes, { + String mime = 'text/plain', +}) => ChatPickedFile(name: name, mime: mime, bytes: Uint8List.fromList(bytes)); + +Finder get _field => find.byKey(const Key('chat-composer-field')); + +Finder get _chips => find.byKey(const Key('chat-composer-attachments')); + +void main() { + late ComposerBarGateway gateway; + late ChatComposerController composer; + late List controllers; + + setUp(() { + gateway = ComposerBarGateway(); + controllers = []; + }); + + tearDown(() { + for (final controller in controllers) { + controller.dispose(); + } + gateway.close(); + }); + + /// Rebuilds the same composer with a different paste source, so a test can + /// tell a replaced listener from a disposed one. + Future pumpWith( + WidgetTester tester, { + ChatClipboardPaste? paste, + }) async { + await tester.pumpWidget( + materialHost( + ChatComposerBar(composer: composer, running: false, paste: paste), + ), + ); + await tester.pump(); + } + + Future pumpBar( + WidgetTester tester, { + ChatClipboardPaste? paste, + ChatDraftStore? drafts, + }) async { + composer = ChatComposerController( + gateway, + 's', + drafts: drafts ?? MemoryChatDraftStore(), + newOperationId: () => 'op/1', + ); + controllers.add(composer); + await pumpWith(tester, paste: paste); + } + + Future focusField(WidgetTester tester) async { + await tester.showKeyboard(_field); + await tester.pump(); + } + + testWidgets('a pasted file makes the same chip as a picked file', ( + tester, + ) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + + fake.paste([ + _file('shot.png', [1, 2, 3], mime: 'image/png'), + ]); + await tester.pump(); + + expect(_chips, findsOneWidget); + expect(find.text('shot.png'), findsOneWidget); + expect(fake.listens, 1); + expect(fake.stops, 0); + }); + + testWidgets('pasting several files keeps clipboard order', (tester) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + + fake.paste([ + _file('first.png', [1], mime: 'image/png'), + _file('second.txt', [2]), + ]); + await tester.pump(); + + expect(find.text('first.png'), findsOneWidget); + expect(find.text('second.txt'), findsOneWidget); + expect( + tester.getTopLeft(find.text('first.png')).dx, + lessThan(tester.getTopLeft(find.text('second.txt')).dx), + ); + }); + + testWidgets('an over-limit paste shows the draft refusal and adds nothing', ( + tester, + ) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + + fake.paste([ + _file( + 'huge.bin', + List.filled(ChatAttachmentDraft.maxTotalBytes + 1, 0), + mime: 'application/octet-stream', + ), + ]); + await tester.pump(); + + expect( + find.text(ChatAttachmentRefusal.totalTooLarge.message), + findsOneWidget, + ); + expect(_chips, findsNothing); + expect(find.text('huge.bin'), findsNothing); + }); + + testWidgets('a refused paste keeps the files already in the draft', ( + tester, + ) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + + fake.paste([ + _file('kept.txt', [1]), + _file( + 'huge.bin', + List.filled(ChatAttachmentDraft.maxTotalBytes, 0), + mime: 'application/octet-stream', + ), + ]); + await tester.pump(); + + expect(find.text('kept.txt'), findsOneWidget); + expect(find.text('huge.bin'), findsNothing); + expect( + find.text(ChatAttachmentRefusal.totalTooLarge.message), + findsOneWidget, + ); + }); + + testWidgets('a pasted attachment sends its exact name, type and bytes', ( + tester, + ) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + + fake.paste([ + _file('shot.png', [9, 8, 7], mime: 'image/png'), + ]); + await tester.pump(); + await tester.enterText(_field, 'look'); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + + expect(gateway.promptBodies.single['attachments'], [ + { + 'name': 'shot.png', + 'mime': 'image/png', + 'data': base64Encode([9, 8, 7]), + }, + ]); + expect(_chips, findsNothing); + }); + + testWidgets('pasted text keeps its caret, selection and draft', ( + tester, + ) async { + final drafts = MemoryChatDraftStore(); + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source, drafts: drafts); + await focusField(tester); + + await tester.enterText(_field, 'hello world'); + await tester.pumpAndSettle(); + + final editable = tester.widget( + find.descendant(of: _field, matching: find.byType(EditableText)), + ); + editable.controller.selection = const TextSelection.collapsed(offset: 5); + // The platform's text update for a paste between "hello" and " world". + tester.testTextInput.updateEditingValue( + const TextEditingValue( + text: 'hello there world', + selection: TextSelection.collapsed(offset: 11), + ), + ); + await tester.pumpAndSettle(); + + expect(editable.controller.text, 'hello there world'); + expect( + editable.controller.selection, + const TextSelection.collapsed(offset: 11), + ); + expect(composer.draft, 'hello there world'); + expect(drafts.entries, {'s': 'hello there world'}); + }); + + testWidgets('paste is only taken while this composer owns the field', ( + tester, + ) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + + expect(fake.canAccept!(), isFalse); + + await focusField(tester); + expect(fake.canAccept!(), isTrue); + + tester.binding.focusManager.primaryFocus?.unfocus(); + await tester.pump(); + expect(fake.canAccept!(), isFalse); + }); + + testWidgets('a paste during an in-flight prompt adds nothing', ( + tester, + ) async { + final reply = Completer(); + gateway.prompt = (_, _) => reply.future; + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + await tester.enterText(_field, 'hello'); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pump(); + + expect(fake.canAccept!(), isFalse); + fake.paste([ + _file('late.png', [1], mime: 'image/png'), + ]); + await tester.pump(); + expect(_chips, findsNothing); + + reply.complete(const {}); + await tester.pumpAndSettle(); + }); + + testWidgets('an unreadable clipboard file is reported', (tester) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + + fake.onError!(StateError('clipboard read failed')); + await tester.pump(); + + expect(find.textContaining('clipboard read failed'), findsOneWidget); + }); + + testWidgets('a partial clipboard read shows its files and the failure', ( + tester, + ) async { + final fake = _FakePaste(); + await pumpBar(tester, paste: fake.source); + await focusField(tester); + + // The web source delivers readable files before a partial read's failure. + fake.paste([ + _file('readable.png', [1], mime: 'image/png'), + ]); + fake.onError!(StateError('second file failed')); + await tester.pump(); + + expect(find.text('readable.png'), findsOneWidget); + expect(find.textContaining('second file failed'), findsOneWidget); + }); + + testWidgets( + 'a replaced source stops listening and dispose stops the new one', + (tester) async { + final first = _FakePaste(); + await pumpBar(tester, paste: first.source); + expect(first.listens, 1); + + final second = _FakePaste(); + await pumpWith(tester, paste: second.source); + expect(first.stops, 1); + expect(second.listens, 1); + + await tester.pumpWidget(const SizedBox()); + expect(second.stops, 1); + }, + ); +} diff --git a/app/test/features/chat/domain/chat_clipboard_paste_test.dart b/app/test/features/chat/domain/chat_clipboard_paste_test.dart new file mode 100644 index 00000000..958dc0fa --- /dev/null +++ b/app/test/features/chat/domain/chat_clipboard_paste_test.dart @@ -0,0 +1,47 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart'; + +void main() { + test('a nameless clipboard file takes the web client name for its type', () { + expect(chatPastedAttachmentName('image/png', 1), 'pasted-1.png'); + expect(chatPastedAttachmentName('image/jpeg', 2), 'pasted-2.jpg'); + expect( + chatPastedAttachmentName('application/octet-stream', 3), + 'pasted-3.octetstream', + ); + expect(chatPastedAttachmentName('', 4), 'pasted-4.bin'); + }); + + test('the unsupported host keeps text paste and reports the gap', () { + const paste = ChatClipboardPaste.unsupported('no files here'); + + expect(paste.isSupported, isFalse); + expect( + () => + paste.listen(onFiles: (_) {}, canAccept: () => true, onError: (_) {}), + throwsUnsupportedError, + ); + }); + + test('a supported host subscribes once and returns its unsubscribe', () { + var listens = 0; + var stops = 0; + final paste = ChatClipboardPaste( + subscribe: (onFiles, canAccept, onError) { + listens += 1; + return () => stops += 1; + }, + ); + + final stop = paste.listen( + onFiles: (_) {}, + canAccept: () => true, + onError: (_) {}, + ); + + expect(paste.isSupported, isTrue); + expect(listens, 1); + stop(); + expect(stops, 1); + }); +} diff --git a/app/tool/chat_paste_probe.mjs b/app/tool/chat_paste_probe.mjs new file mode 100644 index 00000000..2c07409b --- /dev/null +++ b/app/tool/chat_paste_probe.mjs @@ -0,0 +1,213 @@ +import { createServer } from 'node:http'; +import { readFile, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; +import { spawn } from 'node:child_process'; +import { once } from 'node:events'; +import { setTimeout as delay } from 'node:timers/promises'; +import { join, extname, resolve, sep } from 'node:path'; +import { tmpdir } from 'node:os'; + +// Real Chrome acceptance for composer paste. +// +// Usage (from app/): +// flutter build web -t tool/chat_composer_probe_entry.dart -o build/composer_probe_paste +// OPENCODE_ARTIFACT_DIR= node tool/chat_paste_probe.mjs build/composer_probe_paste +// +// The fixture is served over loopback only. Flutter paints text on a canvas, so +// assertions read the semantics tree; the browser also reports whether the +// document paste listener called preventDefault. Captures are PNGs. + +const artifactDir = process.env.OPENCODE_ARTIFACT_DIR; +if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the captures'); +await mkdir(artifactDir, { recursive: true }); +const root = resolve(process.argv[2] ?? join(process.cwd(), 'build/composer_probe_paste')); + +const mime = { + '.html': 'text/html', + '.js': 'text/javascript', + '.json': 'application/json', + '.wasm': 'application/wasm', + '.png': 'image/png', + '.ico': 'image/x-icon', + '.ttf': 'font/ttf', + '.otf': 'font/otf', + '.woff': 'font/woff', + '.woff2': 'font/woff2', +}; +const server = createServer(async (req, res) => { + try { + const path = resolve(root, '.' + decodeURIComponent(new URL(req.url, 'http://localhost').pathname)); + if (path !== root && !path.startsWith(root + sep)) return res.writeHead(403).end(); + const filePath = path === root || path.endsWith('/') ? join(path, 'index.html') : path; + const file = await readFile(filePath); + res.writeHead(200, { 'content-type': mime[extname(filePath)] ?? 'application/octet-stream' }).end(file); + } catch { + res.writeHead(404).end('not found'); + } +}); +server.listen(0, '127.0.0.1'); +await once(server, 'listening'); +const port = server.address().port; +const profile = await mkdtemp(join(tmpdir(), 'chat-paste-chrome-')); +const chrome = spawn('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', [ + '--headless=new', '--no-first-run', '--no-default-browser-check', '--disable-gpu', + '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank', +], { stdio: ['ignore', 'ignore', 'pipe'] }); +let stderr = ''; +chrome.stderr.on('data', data => { stderr += data; }); + +let ws; +try { + const waitFor = async (fn, what) => { + for (let i = 0; i < 120; i++) { const value = await fn(); if (value) return value; await delay(100); } + throw new Error(`Timed out waiting for ${what}`); + }; + const endpoint = await waitFor( + () => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1], + 'Chrome DevTools', + ); + ws = new WebSocket(endpoint); + await once(ws, 'open'); + let id = 0; + const pending = new Map(); + ws.addEventListener('message', event => { + const msg = JSON.parse(event.data); + if (msg.method === 'Runtime.exceptionThrown') { + console.error('PAGE EXCEPTION', JSON.stringify(msg.params?.exceptionDetails?.exception?.description ?? msg.params?.exceptionDetails?.text)); + } + if (msg.id && pending.has(msg.id)) { pending.get(msg.id)(msg); pending.delete(msg.id); } + }); + const send = (method, params = {}, sessionId) => new Promise(resolve => { + const callId = ++id; + pending.set(callId, resolve); + ws.send(JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}) })); + }); + const created = await send('Target.createTarget', { url: 'about:blank' }); + if (!created.result) throw new Error(`Target creation failed: ${JSON.stringify(created)}`); + const session = (await send('Target.attachToTarget', { targetId: created.result.targetId, flatten: true })).result.sessionId; + await send('Page.enable', {}, session); + await send('Runtime.enable', {}, session); + const navigation = await send('Page.navigate', { url: `http://127.0.0.1:${port}/` }, session); + if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation)); + const evaluate = async expression => { + const response = await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }, session); + if (response.error) throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response)}`); + return response.result?.result?.value; + }; + for (let i = 0; i < 120; i++) { + if (await evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))")) break; + await delay(100); + } + await evaluate("document.querySelector('flt-semantics-placeholder')?.click()"); + await delay(1800); + + const labels = async () => + evaluate("[...document.querySelectorAll('flt-semantics')].map(x => x.getAttribute('aria-label') || x.innerText || '').filter(Boolean)"); + const semanticText = async () => (await labels()).join('\n'); + const assertSemantics = async (text, what) => { + const read = await semanticText(); + if (!read.toLowerCase().replace(/\s+/g, ' ').includes(text.toLowerCase())) { + throw new Error(`${what}: semantics lacks ${JSON.stringify(text)}\n${read.slice(0, 2000)}`); + } + }; + const assertMissing = async (text, what) => { + const read = await semanticText(); + if (read.toLowerCase().replace(/\s+/g, ' ').includes(text.toLowerCase())) { + throw new Error(`${what}: semantics still has ${JSON.stringify(text)}\n${read.slice(0, 2000)}`); + } + }; + const editingValue = async () => evaluate(`(() => { + const match = n => (n.getAttribute('aria-label') || n.getAttribute('placeholder') || '').includes('Message the agent'); + const node = document.querySelector('input.flt-text-editing, textarea.flt-text-editing') + || [...document.querySelectorAll('textarea, input')].find(match) + || null; + return node ? { tag: node.tagName, value: node.value } : null; + })()`); + const focusField = async () => { + const focused = await evaluate(`(() => { + const needle = 'Message the agent'; + const node = [...document.querySelectorAll('textarea, input')] + .find(n => (n.getAttribute('aria-label') || n.placeholder || '').includes(needle)); + if (!node) return null; + node.focus(); + return { tag: node.tagName }; + })()`); + if (!focused) throw new Error('Composer field was not exposed to semantics'); + await delay(500); + return focused; + }; + const dispatchPaste = async ({ text = null, files = [] }) => evaluate(`(() => { + const data = new DataTransfer(); + if (${JSON.stringify(text)} != null) data.setData('text/plain', ${JSON.stringify(text)}); + const specs = ${JSON.stringify(files)}; + for (const spec of specs) { + const bytes = new Uint8Array(spec.size).fill(7); + data.items.add(new File([bytes], spec.name, { type: spec.mime })); + } + const target = document.activeElement || document.body; + const event = new ClipboardEvent('paste', { + clipboardData: data, bubbles: true, cancelable: true, + }); + target.dispatchEvent(event); + return event.defaultPrevented; + })()`); + const type = async text => { await send('Input.insertText', { text }, session); await delay(250); }; + const capture = async name => { + const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).result.data; + const file = `paste-${name}.png`; + await writeFile(`${artifactDir}/${file}`, Buffer.from(data, 'base64')); + return file; + }; + const setSize = async (width, height, scale = 1) => { + await send('Emulation.setDeviceMetricsOverride', { + width, height, deviceScaleFactor: scale, mobile: width < 600, + }, session); + await delay(400); + }; + + const checks = []; + await setSize(390, 844); + await assertSemantics('Composer fixture', 'The fixture thread'); + await focusField(); + await type('keep me'); + + const textPrevented = await dispatchPaste({ text: ' clipboard words' }); + if (textPrevented) throw new Error('A text-only paste was prevented'); + checks.push({ check: 'a text-only paste is not intercepted', prevented: textPrevented }); + + const filePrevented = await dispatchPaste({ + files: [{ name: 'shot.png', mime: 'image/png', size: 3 }], + }); + if (!filePrevented) throw new Error('A file paste was not consumed'); + await waitFor(async () => (await semanticText()).includes('shot.png'), 'the pasted chip'); + const draftAfterFile = await editingValue(); + if (draftAfterFile?.value !== 'keep me') { + throw new Error(`The paste changed the draft: ${JSON.stringify(draftAfterFile)}`); + } + await capture('file-chip'); + checks.push({ check: 'a pasted file becomes a chip and leaves text alone', prevented: filePrevented, value: draftAfterFile.value }); + + const hugePrevented = await dispatchPaste({ + files: [{ name: 'huge.bin', mime: 'application/octet-stream', size: 1048577 }], + }); + await waitFor( + async () => (await semanticText()).includes('1 MiB total'), + 'the over-limit refusal', + ); + await assertSemantics('Attachments are too large for web chat (1 MiB total).', 'the over-limit refusal'); + await assertMissing('huge.bin', 'the refused file'); + await capture('over-limit'); + checks.push({ check: 'an over-limit paste shows the refusal and adds no chip', prevented: hugePrevented }); + + await dispatchPaste({ files: [{ name: '', mime: 'image/png', size: 4 }] }); + await waitFor(async () => (await semanticText()).includes('pasted-1.png'), 'the generated name'); + await capture('nameless-file'); + checks.push({ check: 'a nameless clipboard image takes the web pasted name' }); + + console.log(JSON.stringify({ checks }, null, 2)); +} finally { + ws?.close(); + chrome.kill('SIGTERM'); + await Promise.race([once(chrome, 'exit'), delay(3000)]); + server.close(); + await rm(profile, { recursive: true, force: true }); +}