From 421831b5851d3f3aae22e3473d2cdeba4a2410f1 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 21 Oct 2026 23:49:04 +0100 Subject: [PATCH] feat(app): add native chat composer with persisted drafts and stop --- .../chat/conversation/chat_composer_bar.dart | 267 ++++++++++++++++ .../chat/conversation/chat_thread_view.dart | 255 ++++++++++----- app/notes/C3-bar.md | 13 + .../chat_composer_bar_fixtures.dart | 120 +++++++ .../chat_composer_bar_state_test.dart | 148 +++++++++ .../conversation/chat_composer_bar_test.dart | 234 ++++++++++++++ app/tool/chat_composer_probe.mjs | 300 ++++++++++++++++++ app/tool/chat_composer_probe_entry.dart | 133 ++++++++ 8 files changed, 1386 insertions(+), 84 deletions(-) create mode 100644 app/lib/features/chat/conversation/chat_composer_bar.dart create mode 100644 app/notes/C3-bar.md create mode 100644 app/test/features/chat/conversation/chat_composer_bar_fixtures.dart create mode 100644 app/test/features/chat/conversation/chat_composer_bar_state_test.dart create mode 100644 app/test/features/chat/conversation/chat_composer_bar_test.dart create mode 100644 app/tool/chat_composer_probe.mjs create mode 100644 app/tool/chat_composer_probe_entry.dart diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart new file mode 100644 index 00000000..11fc5d7a --- /dev/null +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -0,0 +1,267 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +import '../ui/sessions/chat_adaptive.dart'; +import 'chat_composer_controller.dart'; + +/// The composer's multiline placeholder, verbatim from the web client. +const String chatComposerPlaceholder = 'Message the agent… or / for commands'; + +/// The single primary control's words while a turn is and is not running. +const String chatSendLabel = 'Send'; +const String chatStopLabel = 'Stop'; + +/// Shown while the reply says this prompt waits behind the running turn. +const String chatQueuedNotice = + 'Queued — it will send when the current turn finishes.'; + +/// The foot of one conversation: a native multiline field and one primary +/// Send/Stop control, driven by the landed [ChatComposerController]. +/// +/// 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. +class ChatComposerBar extends StatefulWidget { + const ChatComposerBar({ + super.key, + required this.composer, + required this.running, + }); + + final ChatComposerController composer; + + /// The thread controller's running bit; it alone decides Send versus Stop. + final bool running; + + @override + State createState() => _ChatComposerBarState(); +} + +class _ChatComposerBarState extends State { + late final TextEditingController _text = TextEditingController( + text: widget.composer.draft, + ); + final FocusNode _focus = FocusNode(debugLabel: 'chat-composer'); + + @override + void initState() { + super.initState(); + widget.composer.addListener(_syncDraft); + unawaited(widget.composer.loadDraft()); + } + + @override + void didUpdateWidget(covariant ChatComposerBar oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.composer != widget.composer) { + oldWidget.composer.removeListener(_syncDraft); + widget.composer.addListener(_syncDraft); + _syncDraft(); + } + } + + /// External draft changes (saved-draft load, send clear, refusal restore) + /// replace the field's text but never the focus node, so the caret survives + /// live transcript rebuilds. + void _syncDraft() { + if (!mounted) return; + final draft = widget.composer.draft; + if (_text.text == draft) return; + _text.value = TextEditingValue( + text: draft, + selection: TextSelection.collapsed(offset: draft.length), + ); + } + + KeyEventResult _onKey(FocusNode node, KeyEvent event) { + if (event.logicalKey != LogicalKeyboardKey.enter && + event.logicalKey != LogicalKeyboardKey.numpadEnter) { + return KeyEventResult.ignored; + } + final keyboard = HardwareKeyboard.instance; + if (!keyboard.isControlPressed && !keyboard.isMetaPressed) { + return KeyEventResult.ignored; + } + // A held key must not post a second prompt. + if (event is! KeyDownEvent) return KeyEventResult.handled; + _send(); + return KeyEventResult.handled; + } + + void _send() { + final composer = widget.composer; + if (composer.sending || composer.draft.trim().isEmpty) return; + unawaited(composer.send()); + _focus.requestFocus(); + } + + @override + void dispose() { + widget.composer.removeListener(_syncDraft); + _text.dispose(); + _focus.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + return ListenableBuilder( + listenable: widget.composer, + builder: (context, _) { + final composer = widget.composer; + final error = composer.error ?? composer.draftError; + return SafeArea( + top: false, + child: Padding( + key: const Key('chat-composer'), + padding: const EdgeInsetsDirectional.fromSTEB(16, 8, 16, 8), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + if (composer.queued) + _notice( + context, + cupertino, + key: const Key('chat-composer-queued'), + text: chatQueuedNotice, + ), + if (error != null) + _notice( + context, + cupertino, + key: const Key('chat-composer-error'), + text: error, + failed: true, + ), + Row( + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Expanded( + child: Focus( + canRequestFocus: false, + includeSemantics: false, + onKeyEvent: _onKey, + child: _field(cupertino), + ), + ), + const SizedBox(width: 8), + _primary(context, cupertino), + ], + ), + ], + ), + ), + ); + }, + ); + } + + Widget _field(bool cupertino) { + final composer = widget.composer; + void changed(String value) => unawaited(composer.setDraft(value)); + if (cupertino) { + return CupertinoTextField( + key: const Key('chat-composer-field'), + controller: _text, + focusNode: _focus, + placeholder: chatComposerPlaceholder, + minLines: 1, + maxLines: 6, + keyboardType: TextInputType.multiline, + textInputAction: TextInputAction.newline, + onChanged: changed, + ); + } + return TextField( + key: const Key('chat-composer-field'), + controller: _text, + focusNode: _focus, + minLines: 1, + maxLines: 6, + keyboardType: TextInputType.multiline, + textInputAction: TextInputAction.newline, + onChanged: changed, + decoration: const InputDecoration( + hintText: chatComposerPlaceholder, + border: OutlineInputBorder(), + ), + ); + } + + Widget _primary(BuildContext context, bool cupertino) { + final composer = widget.composer; + final stop = widget.running; + final enabled = stop + ? !composer.stopping + : !composer.sending && composer.draft.trim().isNotEmpty; + void press() { + if (stop) { + unawaited(composer.stop()); + } else { + _send(); + } + } + + final label = Text(stop ? chatStopLabel : chatSendLabel); + if (cupertino) { + return CupertinoButton( + key: const Key('chat-composer-primary'), + color: stop ? CupertinoColors.systemRed.resolveFrom(context) : null, + onPressed: enabled ? press : null, + child: label, + ); + } + final scheme = Theme.of(context).colorScheme; + return FilledButton( + key: const Key('chat-composer-primary'), + style: stop + ? FilledButton.styleFrom( + backgroundColor: scheme.error, + foregroundColor: scheme.onError, + ) + : null, + onPressed: enabled ? press : null, + child: label, + ); + } + + /// A polite, secondary line for queued/refused state. The error uses the + /// host's own semantic error tint, never a bespoke palette. + Widget _notice( + BuildContext context, + bool cupertino, { + required Key key, + required String text, + bool failed = false, + }) { + final theme = Theme.of(context); + final color = failed + ? (cupertino + ? CupertinoColors.systemRed.resolveFrom(context) + : theme.colorScheme.error) + : (cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : theme.colorScheme.onSurfaceVariant); + final style = + (cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : theme.textTheme.bodyMedium) + ?.copyWith(color: color); + return Semantics( + liveRegion: true, + child: Padding( + key: key, + padding: const EdgeInsets.only(bottom: 8), + child: Text(text, style: style), + ), + ); + } +} diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index b5257ab9..50a6659d 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -7,6 +7,9 @@ import '../../../core/api_gateway.dart'; import '../domain/chat_transcript.dart'; import '../ui/sessions/chat_adaptive.dart'; import '../ui/transcript/chat_message_view.dart'; +import 'chat_composer_bar.dart'; +import 'chat_composer_controller.dart'; +import 'chat_draft_store.dart'; import 'chat_row_keys.dart'; import 'chat_thread_controller.dart'; @@ -24,6 +27,8 @@ class ChatThreadView extends StatefulWidget { required this.onBack, this.title = '', this.syncToken = 0, + this.drafts, + this.newOperationId, }); final ApiGateway gateway; @@ -36,25 +41,91 @@ class ChatThreadView extends StatefulWidget { /// Advances when the shared socket is replaced, keeping this selection. final int syncToken; + /// Draft persistence for tests and previews; production uses the app store. + final ChatDraftStore? drafts; + + /// Test/fixture seam for deterministic operation ids. + final String Function()? newOperationId; + @override State createState() => _ChatThreadViewState(); } class _ChatThreadViewState extends State { late ChatThreadController _controller = _attach(); + late ChatComposerController _composer = _attachComposer(); late ChatRowKeys _rowKeys = ChatRowKeys(); + /// A sent turn shown before history or a live frame echoes it. + final List _optimistic = []; + ChatThreadController _attach() => ChatThreadController(widget.gateway, widget.sessionId)..load(); + ChatComposerController _attachComposer() => ChatComposerController( + widget.gateway, + widget.sessionId, + drafts: widget.drafts, + newOperationId: widget.newOperationId, + onOptimistic: _onOptimistic, + onRejected: _onRejected, + ); + + void _onOptimistic(ChatComposerSend send) { + if (!mounted) return; + setState(() { + _optimistic.add({ + 'id': send.optimisticId, + 'session_id': send.sessionId, + 'role': 'user', + 'operation_id': send.operationId, + 'native_message_id': send.nativeMessageId, + 'parts': [ + {'type': 'text', 'text': send.text}, + ], + }); + }); + } + + void _onRejected(ChatComposerSend send, String error) { + if (!mounted) return; + setState(() { + _optimistic.removeWhere((row) => row['operation_id'] == send.operationId); + }); + } + + /// Local turns whose authoritative copy is not in the held transcript yet. + /// A live user frame or a history page that carries the same operation, + /// native id or message id hides the local copy instead of drawing it twice. + List _visibleMessages() { + final messages = _controller.messages; + return [ + ...messages, + for (final pending in _optimistic) + if (!messages.any((message) => _isEcho(pending, message))) pending, + ]; + } + + bool _isEcho(JsonMap pending, JsonMap message) { + if (message['role'] != 'user') return false; + if (pending['id'] == message['id']) return true; + final operation = pending['operation_id']; + if (operation != null && operation == message['operation_id']) return true; + final native = '${pending['native_message_id'] ?? ''}'; + return native.isNotEmpty && native == message['native_message_id']; + } + @override void didUpdateWidget(covariant ChatThreadView oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.gateway != widget.gateway || oldWidget.sessionId != widget.sessionId) { _controller.dispose(); + _composer.dispose(); _controller = _attach(); + _composer = _attachComposer(); _rowKeys = ChatRowKeys(); + _optimistic.clear(); } else if (oldWidget.syncToken != widget.syncToken) { unawaited(_controller.resync()); } @@ -62,6 +133,7 @@ class _ChatThreadViewState extends State { @override void dispose() { + _composer.dispose(); _controller.dispose(); super.dispose(); } @@ -72,7 +144,7 @@ class _ChatThreadViewState extends State { builder: (context, _) { final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; - final rows = transcriptRows(_controller.messages); + final rows = transcriptRows(_visibleMessages()); // Older pages prepend messages and a live frame can append one, so a // folded assistant turn's first message id is not a stable identity. // ChatRowKeys carries a turn's key forward while it still holds any of @@ -80,95 +152,110 @@ class _ChatThreadViewState extends State { // across paging and streaming. final keys = _rowKeys.forRows(rows); final initialLoad = !_controller.loaded && _controller.loading; - return ListView( - key: ValueKey('chat-thread-${widget.sessionId}'), - padding: const EdgeInsets.symmetric(vertical: 8), + return Column( children: [ - Padding( - key: const Key('chat-thread-header'), - padding: const EdgeInsetsDirectional.fromSTEB(8, 0, 16, 4), - child: Wrap( - spacing: 8, - runSpacing: 4, - crossAxisAlignment: WrapCrossAlignment.center, + Expanded( + child: ListView( + key: ValueKey('chat-thread-${widget.sessionId}'), + padding: const EdgeInsets.symmetric(vertical: 8), children: [ - _backButton(cupertino), - if (widget.title.trim().isNotEmpty) - Text( - widget.title.trim(), - style: cupertino - ? CupertinoTheme.of(context).textTheme.navTitleTextStyle - : Theme.of(context).textTheme.titleLarge, + Padding( + key: const Key('chat-thread-header'), + padding: const EdgeInsetsDirectional.fromSTEB(8, 0, 16, 4), + child: Wrap( + spacing: 8, + runSpacing: 4, + crossAxisAlignment: WrapCrossAlignment.center, + children: [ + _backButton(cupertino), + if (widget.title.trim().isNotEmpty) + Text( + widget.title.trim(), + style: cupertino + ? CupertinoTheme.of( + context, + ).textTheme.navTitleTextStyle + : Theme.of(context).textTheme.titleLarge, + ), + ], + ), + ), + if (initialLoad) + Padding( + key: const Key('chat-thread-loading'), + padding: const EdgeInsets.all(16), + child: Center( + child: cupertino + ? const CupertinoActivityIndicator() + : const CircularProgressIndicator(), + ), + ), + if (_controller.hasMore) + Padding( + key: const Key('chat-load-older'), + padding: const EdgeInsets.symmetric(horizontal: 16), + child: Align( + alignment: AlignmentDirectional.centerStart, + child: cupertino + ? CupertinoButton( + padding: EdgeInsets.zero, + onPressed: _controller.loading + ? null + : _controller.loadOlder, + child: const Text('Load older'), + ) + : TextButton( + onPressed: _controller.loading + ? null + : _controller.loadOlder, + child: const Text('Load older'), + ), + ), + ), + if (_controller.error != null) + ChatErrorNotice( + key: const Key('chat-history-error'), + message: 'Chat history unavailable\n${_controller.error}', + onRetry: _controller.load, + ), + if (_controller.subscriptionError != null) + ChatErrorNotice( + key: const Key('chat-live-error'), + message: + 'Live updates unavailable\n${_controller.subscriptionError}', + onRetry: _controller.resync, + ), + if (_controller.loaded && + rows.isEmpty && + _controller.error == null) + const Padding( + key: Key('chat-thread-empty'), + padding: EdgeInsets.all(16), + child: Text('No messages in this chat yet.'), + ), + for (var index = 0; index < rows.length; index++) + Padding( + key: ValueKey(keys[index]), + padding: const EdgeInsets.symmetric(horizontal: 16), + child: ChatMessageView(row: rows[index]), + ), + if (_controller.running) + Padding( + key: const Key('chat-running'), + padding: const EdgeInsets.all(16), + child: Semantics( + liveRegion: true, + child: const Text('Assistant is responding…'), + ), ), ], ), ), - if (initialLoad) - Padding( - key: const Key('chat-thread-loading'), - padding: const EdgeInsets.all(16), - child: Center( - child: cupertino - ? const CupertinoActivityIndicator() - : const CircularProgressIndicator(), - ), - ), - if (_controller.hasMore) - Padding( - key: const Key('chat-load-older'), - padding: const EdgeInsets.symmetric(horizontal: 16), - child: Align( - alignment: AlignmentDirectional.centerStart, - child: cupertino - ? CupertinoButton( - padding: EdgeInsets.zero, - onPressed: _controller.loading - ? null - : _controller.loadOlder, - child: const Text('Load older'), - ) - : TextButton( - onPressed: _controller.loading - ? null - : _controller.loadOlder, - child: const Text('Load older'), - ), - ), - ), - if (_controller.error != null) - ChatErrorNotice( - key: const Key('chat-history-error'), - message: 'Chat history unavailable\n${_controller.error}', - onRetry: _controller.load, - ), - if (_controller.subscriptionError != null) - ChatErrorNotice( - key: const Key('chat-live-error'), - message: - 'Live updates unavailable\n${_controller.subscriptionError}', - onRetry: _controller.resync, - ), - if (_controller.loaded && rows.isEmpty && _controller.error == null) - const Padding( - key: Key('chat-thread-empty'), - padding: EdgeInsets.all(16), - child: Text('No messages in this chat yet.'), - ), - for (var index = 0; index < rows.length; index++) - Padding( - key: ValueKey(keys[index]), - padding: const EdgeInsets.symmetric(horizontal: 16), - child: ChatMessageView(row: rows[index]), - ), - if (_controller.running) - Padding( - key: const Key('chat-running'), - padding: const EdgeInsets.all(16), - child: Semantics( - liveRegion: true, - child: const Text('Assistant is responding…'), - ), - ), + ChatComposerBar( + key: ValueKey('chat-composer-${widget.sessionId}'), + composer: _composer, + running: _controller.running, + ), ], ); }, diff --git a/app/notes/C3-bar.md b/app/notes/C3-bar.md new file mode 100644 index 00000000..50da4df0 --- /dev/null +++ b/app/notes/C3-bar.md @@ -0,0 +1,13 @@ + +## Manager completion after the server persistence failure + +The run ended with the server's duplicate `step_file_changes` insert bug, not an +app fault; the worktree held a complete slice. The manager ran the chat suite (98 +tests pass) and inspected the composer: host-native multiline field, draft owned by +the controller so a live rebuild keeps the caret, Enter newline with Ctrl/Cmd+Enter +send, one Send/Stop control using the host's semantic error tint, queued and +refusal lines as polite live regions. Attachments and the slash palette are +deliberately absent and recorded as open work. + +The server defect is fixed with a regression and deployed separately; this app +slice needed no retry. diff --git a/app/test/features/chat/conversation/chat_composer_bar_fixtures.dart b/app/test/features/chat/conversation/chat_composer_bar_fixtures.dart new file mode 100644 index 00000000..2f816a11 --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_bar_fixtures.dart @@ -0,0 +1,120 @@ +import 'dart:async'; + +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_draft_store.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; + +import 'chat_thread_fixtures.dart'; + +/// A gateway for one open fixture conversation with a composer. History pages +/// are tests' own; prompt/interrupt replies are controlled per test. +class ComposerBarGateway implements ApiGateway { + final calls = <({String type, JsonMap fields})>[]; + final _frames = StreamController.broadcast(); + JsonMap page = chatPage(const []); + Future Function(String text, String operationId)? prompt; + Future Function()? interrupt; + + @override + Future call(String type, [JsonMap fields = const {}]) async { + calls.add((type: type, fields: fields)); + switch (type) { + case 'chat_subscribe': + return {}; + case 'chat_messages': + return page; + case 'chat_prompt': + final body = fields['chat_prompt'] as JsonMap? ?? const {}; + return prompt == null + ? {} + : prompt!('${body['text'] ?? ''}', '${body['operation_id'] ?? ''}'); + case 'chat_interrupt': + return interrupt == null ? {} : interrupt!(); + } + return {}; + } + + @override + Stream get frames => _frames.stream; + + List get promptBodies => calls + .where((call) => call.type == 'chat_prompt') + .map((call) => call.fields['chat_prompt'] as JsonMap) + .toList(); + + List get interruptBodies => calls + .where((call) => call.type == 'chat_interrupt') + .map((call) => call.fields['chat_query'] as JsonMap) + .toList(); + + void emitEvent(JsonMap body) => + _frames.add({'type': 'chat_event', 'chat_event': body}); + + void close() => _frames.close(); +} + +Widget materialHost(Widget child) => MaterialApp( + theme: ThemeData( + platform: TargetPlatform.android, + brightness: Brightness.light, + ), + home: Scaffold(body: child), +); + +Widget cupertinoHost(Widget child) => CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: CupertinoPageScaffold(child: child), +); + +Widget composerThread( + ComposerBarGateway gateway, { + bool cupertino = false, + int syncToken = 0, + ChatDraftStore? drafts, + String Function()? newOperationId, + VoidCallback? onBack, +}) { + final thread = ChatThreadView( + gateway: gateway, + sessionId: 's', + syncToken: syncToken, + onBack: onBack ?? () {}, + drafts: drafts ?? MemoryChatDraftStore(), + newOperationId: newOperationId, + ); + return cupertino ? cupertinoHost(thread) : materialHost(thread); +} + +/// The common fixture turn, so a rebuild has real transcript content to move. +JsonMap fixtureTurn({ + String user = 'Stored question', + String answer = 'Stored answer', +}) => chatPage([ + chatMessage( + 'u1', + 'user', + parts: [ + {'type': 'text', 'text': user}, + ], + ), + chatMessage( + 'a1', + 'assistant', + parts: [ + {'type': 'text', 'text': answer}, + ], + ), +]); + +String fieldText(WidgetTester tester) => tester + .widget( + find.descendant( + of: find.byKey(const Key('chat-composer-field')), + matching: find.byType(EditableText), + ), + ) + .controller + .text; diff --git a/app/test/features/chat/conversation/chat_composer_bar_state_test.dart b/app/test/features/chat/conversation/chat_composer_bar_state_test.dart new file mode 100644 index 00000000..336bd90c --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_bar_state_test.dart @@ -0,0 +1,148 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart'; + +import 'chat_composer_bar_fixtures.dart'; +import 'chat_thread_fixtures.dart'; + +void main() { + late ComposerBarGateway gateway; + late int sequence; + + String nextOperation() => 'op/${++sequence}'; + + setUp(() { + gateway = ComposerBarGateway()..page = fixtureTurn(); + sequence = 0; + }); + + tearDown(() => gateway.close()); + + testWidgets('Stop replaces Send while running and interrupts this session', ( + tester, + ) async { + await tester.pumpWidget( + composerThread(gateway, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + expect(find.text(chatSendLabel), findsOneWidget); + + gateway.emitEvent(chatStepFrame('running')); + await tester.pump(); + await tester.pump(); + + expect(find.text(chatSendLabel), findsNothing); + expect(find.text(chatStopLabel), findsOneWidget); + final button = tester.widget( + find.byKey(const Key('chat-composer-primary')), + ); + final scheme = Theme.of( + tester.element(find.byKey(const Key('chat-composer-primary'))), + ).colorScheme; + expect(button.style?.backgroundColor?.resolve({}), scheme.error); + + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + expect(gateway.interruptBodies.single, {'session_id': 's'}); + expect(gateway.promptBodies, isEmpty); + }); + + testWidgets('the queued notice is a polite line and clears on a frame', ( + tester, + ) async { + gateway.prompt = (_, _) async => {'chat_queued': true}; + final semantics = tester.ensureSemantics(); + await tester.pumpWidget( + composerThread(gateway, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'wait behind', + ); + await tester.pump(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + + expect(find.text(chatQueuedNotice), findsOneWidget); + expect( + tester.getSemantics(find.byKey(const Key('chat-composer-queued'))), + isSemantics(isLiveRegion: true), + ); + + gateway.emitEvent(chatStepFrame('running')); + await tester.pump(); + await tester.pump(); + expect(find.byKey(const Key('chat-composer-queued')), findsNothing); + semantics.dispose(); + }); + + testWidgets('a live rebuild keeps the field focus, text and caret', ( + tester, + ) async { + await tester.pumpWidget( + composerThread(gateway, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + + final field = find.byKey(const Key('chat-composer-field')); + await tester.enterText(field, 'caret draft'); + await tester.pump(); + final before = tester.widget( + find.descendant(of: field, matching: find.byType(EditableText)), + ); + before.controller.selection = const TextSelection.collapsed(offset: 5); + await tester.pump(); + + gateway.emitEvent(chatStepFrame('running')); + await tester.pump(); + + final after = tester.widget( + find.descendant(of: field, matching: find.byType(EditableText)), + ); + expect(after.focusNode, same(before.focusNode)); + expect(after.focusNode.hasFocus, isTrue); + expect(after.controller.text, 'caret draft'); + expect(after.controller.selection.baseOffset, 5); + }); + + for (final cupertino in [false, true]) { + testWidgets('the composer wraps at 320 logical px with 2x text ' + '(${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); + gateway.page = fixtureTurn( + user: + 'A stored question long enough to wrap onto several lines at a ' + 'narrow phone width without clipping any of its words.', + answer: + 'A stored answer that also wraps so the transcript has real ' + 'content above the composer.', + ); + + await tester.pumpWidget( + composerThread( + gateway, + cupertino: cupertino, + newOperationId: nextOperation, + ), + ); + await tester.pumpAndSettle(); + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'A draft with enough words to wrap inside the composer field on a ' + 'narrow screen while the host text is scaled up.', + ); + await tester.pump(); + + expect(tester.takeException(), isNull); + expect(find.byKey(const Key('chat-composer')), findsOneWidget); + expect(find.byKey(const Key('chat-composer-primary')), findsOneWidget); + }); + } +} diff --git a/app/test/features/chat/conversation/chat_composer_bar_test.dart b/app/test/features/chat/conversation/chat_composer_bar_test.dart new file mode 100644 index 00000000..78eac749 --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_bar_test.dart @@ -0,0 +1,234 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; + +import 'chat_composer_bar_fixtures.dart'; +import 'chat_thread_fixtures.dart'; + +void main() { + late ComposerBarGateway gateway; + late int sequence; + + String nextOperation() => 'op/${++sequence}'; + + setUp(() { + gateway = ComposerBarGateway()..page = fixtureTurn(); + sequence = 0; + }); + + tearDown(() => gateway.close()); + + testWidgets( + 'Send posts the exact trimmed prompt identity and shows it once', + (tester) async { + await tester.pumpWidget( + composerThread(gateway, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + ' hello ', + ); + await tester.pump(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + + expect(gateway.promptBodies.single, { + 'session_id': 's', + 'text': 'hello', + 'operation_id': 'op/1', + 'native_message_id': 'msg_op1', + }); + expect(fieldText(tester), ''); + expect(find.text('hello'), findsOneWidget); + + // The live user frame carries the same intent; the local row disappears + // instead of drawing the operator's turn twice. + gateway.emitEvent({ + 'session_id': 's', + 'message_id': 'msg_op1', + 'kind': 'user', + 'operation_id': 'op/1', + 'part': {'type': 'text', 'text': 'hello'}, + }); + await tester.pump(); + expect(find.text('hello'), findsOneWidget); + + // A history resync that includes the stored prompt keeps exactly one row. + gateway.page = chatPage([ + { + 'id': 'u1', + 'role': 'user', + 'parts': [ + {'type': 'text', 'text': 'Stored question'}, + ], + }, + { + 'id': 'a1', + 'role': 'assistant', + 'parts': [ + {'type': 'text', 'text': 'Stored answer'}, + ], + }, + { + 'id': 'msg_op1', + 'role': 'user', + 'operation_id': 'op/1', + 'parts': [ + {'type': 'text', 'text': 'hello'}, + ], + }, + ]); + await tester.pumpWidget( + composerThread(gateway, syncToken: 1, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + expect(find.text('hello'), findsOneWidget); + }, + ); + + testWidgets('Enter inserts a newline; Ctrl+Enter sends', (tester) async { + await tester.pumpWidget( + composerThread(gateway, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + + final field = find.byKey(const Key('chat-composer-field')); + await tester.enterText(field, 'line one'); + await tester.pump(); + + // A hardware Enter is left to the IME and never submits; the engine then + // inserts the newline as an editing value. + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.pump(); + expect(gateway.promptBodies, isEmpty); + tester.testTextInput.updateEditingValue( + const TextEditingValue( + text: 'line one\n', + selection: TextSelection.collapsed(offset: 9), + ), + ); + await tester.pump(); + expect(fieldText(tester), 'line one\n'); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pumpAndSettle(); + + expect(gateway.promptBodies.single['text'], 'line one'); + }); + + testWidgets('Cmd+Enter sends on the Cupertino host', (tester) async { + await tester.pumpWidget( + composerThread(gateway, cupertino: true, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + + final field = find.byKey(const Key('chat-composer-field')); + await tester.enterText(field, 'mac line'); + await tester.pump(); + await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft); + await tester.pumpAndSettle(); + + expect(gateway.promptBodies.single['text'], 'mac line'); + }); + + testWidgets('Send is disabled for a blank draft and while sending', ( + tester, + ) async { + final reply = Completer(); + gateway.prompt = (_, _) => reply.future; + await tester.pumpWidget( + composerThread(gateway, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + + FilledButton primary() => tester.widget( + find.byKey(const Key('chat-composer-primary')), + ); + + expect(primary().onPressed, isNull); + await tester.enterText(find.byKey(const Key('chat-composer-field')), ' '); + await tester.pump(); + expect(primary().onPressed, isNull); + + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'ready', + ); + await tester.pump(); + expect(primary().onPressed, isNotNull); + + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pump(); + expect(gateway.promptBodies, hasLength(1)); + + // Newer text cannot start a second prompt while the first awaits its reply. + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'second', + ); + await tester.pump(); + expect(primary().onPressed, isNull); + + reply.complete(const {}); + await tester.pumpAndSettle(); + expect(primary().onPressed, isNotNull); + expect(gateway.promptBodies, hasLength(1)); + }); + + testWidgets('a refusal restores the draft visibly and reuses its intent', ( + tester, + ) async { + var attempts = 0; + gateway.prompt = (_, _) async { + if (++attempts == 1) throw StateError('response lost'); + return const {}; + }; + await tester.pumpWidget( + composerThread(gateway, newOperationId: nextOperation), + ); + await tester.pumpAndSettle(); + + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + ' keep me ', + ); + await tester.pump(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + + expect(fieldText(tester), ' keep me '); + expect(find.byKey(const Key('chat-composer-error')), findsOneWidget); + expect(find.textContaining('response lost'), findsOneWidget); + expect(gateway.promptBodies.single['operation_id'], 'op/1'); + // The refused optimistic turn is withdrawn, not left as a fake message. + expect(find.text('keep me'), findsNothing); + + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + + expect(gateway.promptBodies.map((body) => body['operation_id']), [ + 'op/1', + 'op/1', + ]); + expect(fieldText(tester), ''); + expect(find.byKey(const Key('chat-composer-error')), findsNothing); + + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'different', + ); + await tester.pump(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + expect(gateway.promptBodies.last['operation_id'], 'op/2'); + }); +} diff --git a/app/tool/chat_composer_probe.mjs b/app/tool/chat_composer_probe.mjs new file mode 100644 index 00000000..95932f4e --- /dev/null +++ b/app/tool/chat_composer_probe.mjs @@ -0,0 +1,300 @@ +import { createServer } from 'node:http'; +import { readFile, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; +import { spawn, execFile } from 'node:child_process'; +import { promisify } from 'node:util'; +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 the chat composer strip. +// +// Usage (from app/): +// flutter build web -t tool/chat_composer_probe_entry.dart \ +// --dart-define=CHAT_COMPOSER_PROBE_STYLE=material -o build/composer_probe_material +// flutter build web -t tool/chat_composer_probe_entry.dart \ +// --dart-define=CHAT_COMPOSER_PROBE_STYLE=cupertino -o build/composer_probe_cupertino +// OPENCODE_ARTIFACT_DIR= PROBE_VARIANT=material \ +// node tool/chat_composer_probe.mjs build/composer_probe_material +// OPENCODE_ARTIFACT_DIR= PROBE_VARIANT=cupertino \ +// node tool/chat_composer_probe.mjs build/composer_probe_cupertino +// +// The fixture is served over loopback only. Flutter paints text on a canvas, so +// assertions read the semantics tree (aria-label/innerText) and the focused +// editing element; captures are PNGs for visual inspection. + +const variant = process.env.PROBE_VARIANT ?? 'material'; +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_material')); + +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-composer-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?.includes('detached') || msg.method?.includes('crashed')) { + console.error('CDP event', JSON.stringify(msg)); + } + 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; + }; + await delay(3500); + 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, 3000)}`); + } + }; + const clickLabel = async (label, { contains = false } = {}) => { + const point = await evaluate(`(() => { + const needle = ${JSON.stringify(label)}; + const nodes = [...document.querySelectorAll('flt-semantics')]; + const e = nodes.find(x => { + const text = x.getAttribute('aria-label') || x.innerText || ''; + return ${contains ? 'text.includes(needle)' : 'text === needle'}; + }); + if (!e) return null; + const r = e.getBoundingClientRect(); + return { x: r.x + r.width / 2, y: r.y + r.height / 2 }; + })()`); + if (!point) return false; + await send('Input.dispatchMouseEvent', { type: 'mousePressed', x: point.x, y: point.y, button: 'left', clickCount: 1 }, session); + await send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: point.x, y: point.y, button: 'left', clickCount: 1 }, session); + return true; + }; + const focusField = async () => { + const focused = await evaluate(`(() => { + const needle = 'Message the agent'; + const nodes = [...document.querySelectorAll('textarea, input')]; + const node = nodes.find(n => (n.getAttribute('aria-label') || n.placeholder || '').includes(needle)); + if (!node) return null; + node.focus(); + const r = node.getBoundingClientRect(); + return { tag: node.tagName, label: node.getAttribute('aria-label'), rect: { x: r.x, y: r.y, w: r.width, h: r.height } }; + })()`); + if (!focused) { + const read = await evaluate("[...document.querySelectorAll('textarea, input')].map(x => ({tag:x.tagName,label:x.getAttribute('aria-label'),placeholder:x.placeholder,value:x.value}))"); + throw new Error(`Composer field was not exposed to semantics: ${JSON.stringify(read)}`); + } + await delay(500); + const editing = await editingValue(); + if (!editing) throw new Error('The composer did not expose an editing element'); + return { ...focused, ...editing }; + }; + 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 type = async text => { await send('Input.insertText', { text }, session); await delay(250); }; + const key = async (type, params) => send('Input.dispatchKeyEvent', { type, ...params }, session); + const pressEnter = async modifiers => { + if (modifiers === 0) { + await key('keyDown', { + key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13, + text: '\r', unmodifiedText: '\r', + }); + } else { + const modifier = modifiers === 4 + ? { key: 'Meta', code: 'MetaLeft', windowsVirtualKeyCode: 91, nativeVirtualKeyCode: 55 } + : { key: 'Control', code: 'ControlLeft', windowsVirtualKeyCode: 17, nativeVirtualKeyCode: 59 }; + await key('rawKeyDown', { ...modifier, modifiers }); + await key('rawKeyDown', { + modifiers, key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13, + }); + await key('keyUp', { + modifiers, key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13, + }); + await key('keyUp', { ...modifier, modifiers: 0 }); + await delay(500); + return; + } + await key('keyUp', { + modifiers, key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13, + }); + await delay(500); + }; + const setSize = async (width, height, scale = 1) => { + await send('Emulation.setDeviceMetricsOverride', { + width, height, deviceScaleFactor: scale, mobile: width < 600, + }, session); + await delay(400); + }; + const captures = []; + const capture = async name => { + const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).result.data; + const file = `${variant}-${name}.png`; + await writeFile(`${artifactDir}/${file}`, Buffer.from(data, 'base64')); + captures.push(file); + return file; + }; + const ocr = async file => + (await promisify(execFile)('tesseract', [`${artifactDir}/${file}`, 'stdout'])).stdout; + const assertOcr = async (file, text) => { + const read = await ocr(file); + if (!read.toLowerCase().replace(/\s+/g, ' ').includes(text.toLowerCase())) { + throw new Error(`OCR of ${file} lacks ${JSON.stringify(text)}:\n${read.slice(0, 2000)}`); + } + }; + + const checks = []; + await setSize(390, 844); + await assertSemantics('Composer fixture', 'The fixture thread'); + const initial = await editingValue(); + if (initial?.value !== '') throw new Error(`The composer started with ${JSON.stringify(initial)}`); + await capture('composer-initial'); + const focused = await focusField(); + checks.push({ check: 'field exposed as an editing element', focused }); + + await type('First line'); + await pressEnter(0); + const afterEnter = await editingValue(); + if (!afterEnter?.value?.includes('\n')) { + throw new Error(`Enter did not insert a newline: ${JSON.stringify(afterEnter)}`); + } + await type('Second line'); + const multiline = await editingValue(); + if (multiline?.value !== 'First line\nSecond line') { + throw new Error(`Multiline draft is ${JSON.stringify(multiline)}`); + } + const multilineCapture = await capture('composer-multiline'); + await assertOcr(multilineCapture, 'Second'); + checks.push({ check: 'Enter inserted a newline without sending', value: multiline.value }); + + const usersBefore = (await labels()).filter(label => label === 'User message').length; + await pressEnter(variant === 'cupertino' ? 4 : 2); + await waitFor(async () => (await semanticText()).includes('Stop'), 'Stop after send'); + const usersAfter = (await labels()).filter(label => label === 'User message').length; + if (usersAfter <= usersBefore) { + throw new Error(`No optimistic user row appeared (${usersBefore} -> ${usersAfter})`); + } + const cleared = await editingValue(); + if (cleared?.value !== '') throw new Error(`Send left the field as ${JSON.stringify(cleared)}`); + const runningCapture = await capture('composer-running'); + await assertOcr(runningCapture, 'First'); + checks.push({ check: 'Ctrl/Cmd+Enter sent and the optimistic turn is visible', cleared }); + + // The reply that carries `chat_queued` lands after the running frame. + await delay(700); + await focusField(); + await type('Queued follow-up'); + const queuedDraft = await editingValue(); + if (process.env.PROBE_DEBUG) console.log('queued draft', JSON.stringify(queuedDraft)); + await pressEnter(variant === 'cupertino' ? 4 : 2); + await delay(900); + const queuedCapture = await capture('composer-queued'); + const queuedRead = await ocr(queuedCapture); + if (process.env.PROBE_DEBUG) console.log('queued OCR', JSON.stringify(queuedRead.slice(0, 500))); + if (!queuedRead.toLowerCase().replace(/\s+/g, ' ').includes('current turn finishes')) { + throw new Error(`The queued notice did not render:\n${queuedRead.slice(0, 2000)}`); + } + checks.push({ check: 'the queued reply shows its polite notice while Stop stays' }); + + if (!await clickLabel('Stop')) throw new Error('Could not press Stop'); + await waitFor(async () => !(await semanticText()).includes('Stop'), 'the stopped turn'); + await capture('composer-stopped'); + checks.push({ check: 'Stop interrupted the turn and Send returned' }); + + await focusField(); + await type('refuse me'); + await pressEnter(variant === 'cupertino' ? 4 : 2); + await waitFor( + async () => (await semanticText()).includes('The fixture refused this prompt.'), + 'the refusal line', + ); + const restored = await editingValue(); + if (restored?.value !== 'refuse me') { + throw new Error(`Refused draft was not restored: ${JSON.stringify(restored)}`); + } + await capture('composer-refused'); + await assertOcr(`${variant}-composer-refused.png`, 'refuse me'); + checks.push({ check: 'the refusal restored the exact draft and reported it', restored }); + + await setSize(320, 640, 2); + await delay(500); + const wrapped = await semanticText(); + if (!wrapped.includes('Composer fixture') || !wrapped.includes('Send')) { + throw new Error(`The 320px/2x layout lost its content:\n${wrapped.slice(0, 2000)}`); + } + await capture('composer-320-2x'); + checks.push({ check: 'the composer still renders at 320 logical px with 2x pixels' }); + + console.log(JSON.stringify({ variant, checks, captures }, 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 }); +} diff --git a/app/tool/chat_composer_probe_entry.dart b/app/tool/chat_composer_probe_entry.dart new file mode 100644 index 00000000..d774848b --- /dev/null +++ b/app/tool/chat_composer_probe_entry.dart @@ -0,0 +1,133 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; + +/// A live-looking fixture conversation with a deterministic composer. +/// +/// A successful prompt starts a running turn (so the primary control becomes +/// Stop); text containing `queued` additionally returns the queued flag. Text +/// containing `refuse` is rejected so the browser can see the restored draft +/// and the polite error line. Stop ends the turn through a live step frame. +class _ProbeGateway implements ApiGateway { + final _frames = StreamController.broadcast(); + var _sequence = 0; + + static const _session = 'probe-composer'; + + @override + Stream get frames => _frames.stream; + + @override + Future call(String type, [JsonMap fields = const {}]) async { + switch (type) { + case 'chat_subscribe': + return {}; + case 'chat_messages': + return { + 'chat_history': { + 'messages': [ + { + 'id': 'probe-user', + 'role': 'user', + 'parts': [ + { + 'type': 'text', + 'text': 'Show me the composer as an operator would use it.', + }, + ], + }, + { + 'id': 'probe-assistant', + 'role': 'assistant', + 'parts': [ + { + 'type': 'text', + 'text': + 'The composer sits below the conversation with a ' + 'multiline field and one primary control.', + }, + ], + }, + ], + 'next_cursor': '', + 'has_more': false, + }, + }; + case 'chat_prompt': + final body = fields['chat_prompt'] as JsonMap? ?? const {}; + final text = '${body['text'] ?? ''}'; + if (text.toLowerCase().contains('refuse')) { + throw StateError('The fixture refused this prompt.'); + } + final queued = text.toLowerCase().contains('queued'); + Timer(const Duration(milliseconds: 150), _running); + await Future.delayed(const Duration(milliseconds: 450)); + return queued ? {'chat_queued': true} : {}; + case 'chat_interrupt': + Timer(const Duration(milliseconds: 200), _ended); + return {}; + } + return {}; + } + + void _running() => _emit({ + 'kind': 'step', + 'step': { + 'status': 'running', + 'turn_id': 'probe-turn', + 'intermediate': false, + }, + }); + + void _ended() => _emit({ + 'kind': 'step', + 'step': {'status': 'ended', 'turn_id': 'probe-turn', 'intermediate': false}, + }); + + void _emit(JsonMap body) => _frames.add({ + 'type': 'chat_event', + 'chat_event': {'session_id': _session, ...body}, + }); + + String nextOperation() => 'probe-op-${++_sequence}'; +} + +const _probeStyle = String.fromEnvironment( + 'CHAT_COMPOSER_PROBE_STYLE', + defaultValue: 'material', +); + +void main() { + final gateway = _ProbeGateway(); + final thread = ChatThreadView( + gateway: gateway, + sessionId: 'probe-composer', + title: 'Composer fixture', + onBack: () {}, + drafts: MemoryChatDraftStore(), + newOperationId: gateway.nextOperation, + ); + if (_probeStyle == 'cupertino') { + runApp( + CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: CupertinoPageScaffold(child: SafeArea(child: thread)), + ), + ); + return; + } + runApp( + MaterialApp( + theme: ThemeData( + platform: TargetPlatform.android, + brightness: Brightness.light, + ), + home: Scaffold(body: SafeArea(child: thread)), + ), + ); +}