From 18d43cadc3effb2e8d7f62f961a15c18936a9b94 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 25 Oct 2026 17:38:56 +0000 Subject: [PATCH] Show an animated assistant bubble before response tokens --- .../chat_composer_controller.dart | 6 +- .../conversation/chat_thread_controller.dart | 23 ++ .../chat/conversation/chat_thread_view.dart | 13 +- .../features/chat/domain/chat_transcript.dart | 14 ++ .../chat/ui/transcript/chat_message_view.dart | 4 + .../ui/transcript/chat_response_pending.dart | 66 ++++++ app/notes/chat-response-pending.md | 46 ++++ .../chat_composer_attachment_send_test.dart | 7 +- .../chat_response_pending_state_test.dart | 109 ++++++++++ .../chat_response_pending_test.dart | 196 ++++++++++++++++++ app/tool/chat_response_pending_fixture.dart | 109 ++++++++++ app/tool/chat_response_pending_probe.mjs | 86 ++++++++ 12 files changed, 676 insertions(+), 3 deletions(-) create mode 100644 app/lib/features/chat/ui/transcript/chat_response_pending.dart create mode 100644 app/notes/chat-response-pending.md create mode 100644 app/test/features/chat/conversation/chat_response_pending_state_test.dart create mode 100644 app/test/features/chat/conversation/chat_response_pending_test.dart create mode 100644 app/tool/chat_response_pending_fixture.dart create mode 100644 app/tool/chat_response_pending_probe.mjs diff --git a/app/lib/features/chat/conversation/chat_composer_controller.dart b/app/lib/features/chat/conversation/chat_composer_controller.dart index 307eaf8f..33c639b9 100644 --- a/app/lib/features/chat/conversation/chat_composer_controller.dart +++ b/app/lib/features/chat/conversation/chat_composer_controller.dart @@ -36,6 +36,7 @@ class ChatComposerSend { typedef ChatSendCallback = void Function(ChatComposerSend send); typedef ChatSendRejected = void Function(ChatComposerSend send, String error); +typedef ChatSendAccepted = void Function(ChatComposerSend send, bool queued); /// The composer state for one conversation: exact draft, the one plain prompt /// in flight, the reply's queued flag and Stop. It is not a transcript: @@ -47,6 +48,7 @@ class ChatComposerController extends ChangeNotifier { ChatDraftStore? drafts, this.onOptimistic, this.onRejected, + this.onAccepted, this.newOperationId, }) : _drafts = drafts ?? @@ -60,6 +62,7 @@ class ChatComposerController extends ChangeNotifier { final String sessionId; final ChatSendCallback? onOptimistic; final ChatSendRejected? onRejected; + final ChatSendAccepted? onAccepted; final ChatDraftStore _drafts; /// Test/fixture seam; production mints a unique id per new intent. @@ -163,9 +166,9 @@ class ChatComposerController extends ChangeNotifier { _draft = ''; _edited = true; _notify(); + onOptimistic?.call(intent); await _persistDraft(''); if (_disposed) return false; - onOptimistic?.call(intent); try { final reply = await _repository.prompt( sessionId, @@ -175,6 +178,7 @@ class ChatComposerController extends ChangeNotifier { ); if (_disposed) return false; _queued = reply['chat_queued'] == true; + onAccepted?.call(intent, _queued); _pending = null; _sending = false; _error = null; diff --git a/app/lib/features/chat/conversation/chat_thread_controller.dart b/app/lib/features/chat/conversation/chat_thread_controller.dart index b5f7bf5c..8b7c4486 100644 --- a/app/lib/features/chat/conversation/chat_thread_controller.dart +++ b/app/lib/features/chat/conversation/chat_thread_controller.dart @@ -65,6 +65,7 @@ class ChatThreadController extends ChangeNotifier { String _cursor = ''; String _nativeTurnId = ''; String? _liveAssistantMessageId; + String? _awaitingResponseOperation; JsonMap? _usage; String? _error; @@ -102,6 +103,7 @@ class ChatThreadController extends ChangeNotifier { /// Usage-only snapshots refresh Info, not the expensive transcript tree. Listenable get transcriptChanges => _transcriptChanges; bool get running => _running; + bool get awaitingResponse => _awaitingResponseOperation != null; String? get liveAssistantMessageId => _liveAssistantMessageId; bool get loaded => _loaded; bool get loading => _loading; @@ -209,6 +211,7 @@ class ChatThreadController extends ChangeNotifier { _running = false; _nativeTurnId = ''; _liveAssistantMessageId = null; + _awaitingResponseOperation = null; _authoritative = false; _notify(); await _attach(force: true); @@ -317,6 +320,11 @@ class ChatThreadController extends ChangeNotifier { } else if (const ['text', 'reasoning', 'tool'].contains(body['kind'])) { _liveAssistantMessageId = body['message_id'] as String?; } + if (body['kind'] == 'step' && + (body['step'] as Map?)?['status'] == 'ended' && + (body['step'] as Map?)?['intermediate'] != true) { + _awaitingResponseOperation = null; + } _nativeTurnId = '${next['nativeTurnId'] ?? ''}'; _notify(); } @@ -374,6 +382,9 @@ class ChatThreadController extends ChangeNotifier { /// the history copy replaces it in place instead of moving it. void insertLocal(JsonMap message) { if (_disposed) return; + if (!_running) { + _awaitingResponseOperation ??= message['operation_id'] as String?; + } _messages = [..._messages, message]; _notify(); } @@ -381,6 +392,9 @@ class ChatThreadController extends ChangeNotifier { /// Removes a sent turn the server refused. void removeLocal(String operationId) { if (_disposed) return; + if (_awaitingResponseOperation == operationId) { + _awaitingResponseOperation = null; + } final next = [ for (final message in _messages) if (message['operation_id'] != operationId) message, @@ -390,6 +404,15 @@ class ChatThreadController extends ChangeNotifier { _notify(); } + /// An accepted queued prompt waits behind another turn, not its own reply. + void promptAccepted(String operationId, {required bool queued}) { + if (_disposed || !queued || _awaitingResponseOperation != operationId) { + return; + } + _awaitingResponseOperation = null; + _notify(); + } + void _notify({bool transcript = true}) { if (_disposed) return; notifyListeners(); diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index d1ddd02e..a76cc5d2 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -235,6 +235,8 @@ class _ChatThreadViewState extends State { newOperationId: widget.newOperationId, onOptimistic: _onOptimistic, onRejected: _onRejected, + onAccepted: (send, queued) => + _controller.promptAccepted(send.operationId, queued: queued), ); void _onOptimistic(ChatComposerSend send) { @@ -646,7 +648,16 @@ class _ChatThreadViewState extends State { transcriptRows( messages, trailing: cards, - running: _controller.running && !_reconnecting && !_staleTranscript, + running: + _controller.running && + !_reconnecting && + !_staleTranscript && + !cards.any(chatCardAwaitingOperator), + awaitingResponse: + _controller.awaitingResponse && + !_reconnecting && + !_staleTranscript && + !cards.any(chatCardAwaitingOperator), liveMessageId: _controller.liveAssistantMessageId, ), ); diff --git a/app/lib/features/chat/domain/chat_transcript.dart b/app/lib/features/chat/domain/chat_transcript.dart index 206d081d..88284b6e 100644 --- a/app/lib/features/chat/domain/chat_transcript.dart +++ b/app/lib/features/chat/domain/chat_transcript.dart @@ -77,6 +77,7 @@ List transcriptRows( List messages, { List trailing = const [], bool running = false, + bool awaitingResponse = false, String? liveMessageId, }) { final rows = []; @@ -146,15 +147,28 @@ List transcriptRows( (messages.isNotEmpty && messages.last['role'] == 'assistant' ? '${messages.last['id'] ?? ''}' : null); + var hasStreamingRow = false; if (running && streamingId != null) { for (final row in rows.reversed) { if (row['kind'] == 'assistant' && (row['messageIds'] as List).contains(streamingId)) { row['streaming'] = true; + hasStreamingRow = true; break; } } } + // Display-only response shell: never inserted into stored message history. + // A queued follow-up does not create a second shell beside the active reply. + if (!hasStreamingRow && (running || awaitingResponse)) { + rows.add({ + 'kind': 'assistant', + 'key': 'chat-pending-response', + 'messageIds': [], + 'parts': [], + 'streaming': true, + }); + } rows.addAll(trailing); return rows; } diff --git a/app/lib/features/chat/ui/transcript/chat_message_view.dart b/app/lib/features/chat/ui/transcript/chat_message_view.dart index 4254cf55..3ce562f8 100644 --- a/app/lib/features/chat/ui/transcript/chat_message_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_message_view.dart @@ -9,6 +9,7 @@ import '../../domain/chat_tool_calls.dart'; import 'chat_tool_calls_view.dart'; import 'chat_reasoning_chip.dart'; import 'native_disclosure.dart'; +import 'chat_response_pending.dart'; /// Builds an assistant text part; the default keeps the server's text exactly. typedef ChatMessageTextBuilder = Widget Function(BuildContext, String); @@ -265,6 +266,9 @@ class ChatMessageView extends StatelessWidget { KeyedSubtree(key: _partKey(part, index, seen), child: widget), ); } + if (widgets.isEmpty && row['streaming'] == true) { + widgets.add(const ChatResponsePending()); + } return widgets; } diff --git a/app/lib/features/chat/ui/transcript/chat_response_pending.dart b/app/lib/features/chat/ui/transcript/chat_response_pending.dart new file mode 100644 index 00000000..621a077d --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_response_pending.dart @@ -0,0 +1,66 @@ +import 'dart:math' as math; + +import 'package:flutter/widgets.dart'; + +/// A local animation inside the normal assistant bubble, before answer text. +/// It never rebuilds the transcript or changes its scroll position on a tick. +class ChatResponsePending extends StatefulWidget { + const ChatResponsePending({super.key}); + + @override + State createState() => _ChatResponsePendingState(); +} + +class _ChatResponsePendingState extends State + with SingleTickerProviderStateMixin { + late final AnimationController _animation = AnimationController( + vsync: this, + duration: const Duration(milliseconds: 1200), + ); + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + if (MediaQuery.disableAnimationsOf(context)) { + _animation.stop(); + } else { + _animation.repeat(); + } + } + + @override + void dispose() { + _animation.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) => Semantics( + label: 'Assistant is responding', + liveRegion: true, + excludeSemantics: true, + child: AnimatedBuilder( + animation: _animation, + builder: (context, _) => Row( + mainAxisSize: MainAxisSize.min, + children: [ + for (var index = 0; index < 3; index++) + Opacity( + opacity: MediaQuery.disableAnimationsOf(context) + ? 1 + : 0.35 + + 0.65 * + (0.5 + + 0.5 * + math.sin( + 2 * + math.pi * + (_animation.value - index / 3), + )), + child: const Text('.'), + ), + ], + ), + ), + ); +} diff --git a/app/notes/chat-response-pending.md b/app/notes/chat-response-pending.md new file mode 100644 index 00000000..e76c06d6 --- /dev/null +++ b/app/notes/chat-response-pending.md @@ -0,0 +1,46 @@ +# Assistant bubble before response tokens + +## Behavior + +- Send inserts the optimistic user turn and starts a display-only assistant + response shell immediately, before draft persistence or the server reply. +- The normal assistant bubble contains three animated dots while no answer body + exists, including reasoning-only and tool-only responses. Tool/reasoning chips + stay below the bubble. First answer content replaces the dots. +- No fabricated assistant message enters stored history or the transcript cache. + Rejection, a terminal step, or reconnect clears local waiting state. A queued + follow-up does not add a second response shell. Pending operator decisions + suppress the generating indicator. +- Animation rebuilds only the dots, not the transcript. Reduced-motion users + receive a static ellipsis; accessibility announces “Assistant is responding”. + +## Verification + +- Full `flutter test`: **1,900 passed**. Ten targeted new tests also passed on + the final checkout; `flutter analyze` is clean. +- Tests cover immediate appearance despite a blocked draft write, acceptance and + user echoes, reasoning/tools before text, animation changes, first text, + rejection, queued prompts, terminal steps, and reduced motion. +- Isolated release web build: `build/chat-response-pending-web`. + Real Material and Cupertino browser probes sent only fixture prompts and + checked the pre-frame bubble, changing dot pixels, tool-only state, and text + replacement. Inspected captures: + `build/chat-response-pending-{material,cupertino}/`. +- No pre-change screenshot was captured. The source diff shows the prior missing + assistant row before frames and the omission of the bubble for tool-only rows. + +## Pixel 9 Pro XL + +- Exercised the actual thread on `47241FDAS00DLK` using the isolated native entry + `tool/chat_response_pending_fixture.dart`, which has no server connection and + an in-memory draft store. No test prompts were sent to real operator chats. +- Native captures verify the bubble before any response frame, above a tool-only + footer, and then replaced by answer text. A four-second recording sampled the + dots' exact semantics bounds: **19 sampled frames, 19 distinct pixel hashes**. + Evidence: `build/chat-response-pending-pixel9/`. +- Restored the normal app release **0.1.29882493** after the fixture test with + `adb install -r`; normal Pipelines UI and installed version verified. App data + and sign-in preserved. Original display/density/power/timeout settings verified + restored; device capture files removed; all recorders completed. +- Normal APK: `build/chat-response-pending-apk/pipelines.apk`. No website or + public update-feed publication, shared-service restart, or Fold use. diff --git a/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart b/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart index 956c4654..85f86e01 100644 --- a/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart +++ b/app/test/features/chat/conversation/chat_composer_attachment_send_test.dart @@ -253,7 +253,12 @@ void main() { await typeAndSend(tester, 'plain text'); expect(find.text('plain text'), findsOneWidget); - expect(find.byType(ChatMessageView), findsOneWidget); + expect( + find.byWidgetPredicate( + (widget) => widget is ChatMessageView && widget.row['kind'] == 'user', + ), + findsOneWidget, + ); expect(find.text('screen.png'), findsNothing); expect(find.text('notes.txt'), findsNothing); }); diff --git a/app/test/features/chat/conversation/chat_response_pending_state_test.dart b/app/test/features/chat/conversation/chat_response_pending_state_test.dart new file mode 100644 index 00000000..9afb5b1c --- /dev/null +++ b/app/test/features/chat/conversation/chat_response_pending_state_test.dart @@ -0,0 +1,109 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.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/conversation/chat_thread_controller.dart'; +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; + +import 'chat_composer_fixtures.dart'; +import 'chat_thread_fixtures.dart' as thread; + +class _SlowDraftStore extends MemoryChatDraftStore { + final clear = Completer(); + + @override + Future write(String sessionId, String text) => + text.isEmpty ? clear.future : super.write(sessionId, text); +} + +void main() { + test( + 'optimistic response starts before disk persistence or server reply', + () async { + final drafts = _SlowDraftStore(); + final gateway = FakeComposerGateway(); + var optimistic = false; + final composer = ChatComposerController( + gateway, + 's', + drafts: drafts, + onOptimistic: (_) => optimistic = true, + ); + addTearDown(composer.dispose); + addTearDown(gateway.close); + await composer.setDraft('hello'); + final sending = composer.send(); + expect(optimistic, isTrue); + expect(gateway.calls, isEmpty); + drafts.clear.complete(true); + expect(await sending, isTrue); + }, + ); + + test( + 'local waiting survives a user echo and acceptance, then ends', + () async { + final gateway = thread.FakeChatGateway(); + final controller = ChatThreadController(gateway, 's'); + addTearDown(controller.dispose); + addTearDown(gateway.close); + controller.insertLocal({ + 'id': 'local', + 'role': 'user', + 'operation_id': 'op', + 'parts': [], + }); + expect(controller.awaitingResponse, isTrue); + controller.promptAccepted('op', queued: false); + gateway.emitEvent({ + 'session_id': 's', + 'kind': 'user', + 'message_id': 'echo', + 'part': {'text': 'hello'}, + }); + await pumpEventQueue(); + expect(controller.awaitingResponse, isTrue); + gateway.emitEvent(thread.chatStepFrame('ended')); + await pumpEventQueue(); + expect(controller.awaitingResponse, isFalse); + controller.promptAccepted('op', queued: false); + expect( + controller.awaitingResponse, + isFalse, + reason: 'a late ack cannot restart it', + ); + }, + ); + + test('a queued follow-up does not create a second assistant shell', () { + final rows = transcriptRows( + [ + thread.chatMessage('u', 'user'), + thread.chatMessage( + 'a', + 'assistant', + parts: [ + {'id': 't', 'type': 'tool', 'name': 'read'}, + ], + ), + thread.chatMessage('queued', 'user'), + ], + running: true, + liveMessageId: 'a', + awaitingResponse: true, + ); + expect(rows.where((row) => row['kind'] == 'assistant'), hasLength(1)); + expect(rows[1]['streaming'], isTrue); + expect(rows.last['kind'], 'user'); + }); + + test('a response shell is display-only and absent from settled history', () { + final messages = [thread.chatMessage('u', 'user')]; + final pending = transcriptRows(messages, awaitingResponse: true); + expect(pending.last['kind'], 'assistant'); + expect(pending.last['streaming'], isTrue); + expect(messages, hasLength(1)); + expect(transcriptRows(messages), hasLength(1)); + }); +} diff --git a/app/test/features/chat/conversation/chat_response_pending_test.dart b/app/test/features/chat/conversation/chat_response_pending_test.dart new file mode 100644 index 00000000..2d3c384b --- /dev/null +++ b/app/test/features/chat/conversation/chat_response_pending_test.dart @@ -0,0 +1,196 @@ +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 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_response_pending.dart'; + +import 'chat_thread_fixtures.dart'; + +class _Gateway extends FakeChatGateway { + Completer prompt = Completer(); + + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type == 'chat_prompt') return prompt.future; + return super.call(type, fields); + } +} + +Future _pump(WidgetTester tester) async { + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + await tester.pump(); +} + +Future _host( + WidgetTester tester, + _Gateway gateway, { + bool cupertino = false, +}) async { + gateway.history = (_) async => chatPage([]); + final thread = ChatThreadView( + gateway: gateway, + sessionId: 's', + onBack: () {}, + drafts: MemoryChatDraftStore(), + newOperationId: () => 'test-pending', + ); + Widget animations(BuildContext context, Widget? child) => MediaQuery( + data: MediaQuery.of(context).copyWith(disableAnimations: false), + child: child!, + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp( + builder: animations, + home: CupertinoPageScaffold(child: thread), + ) + : MaterialApp( + builder: animations, + home: Scaffold(body: thread), + ), + ); + await tester.pumpAndSettle(); +} + +Future _send(WidgetTester tester) async { + 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 _pump(tester); +} + +void main() { + for (final cupertino in [false, true]) { + testWidgets( + 'response bubble exists before frames, through tools, then text ' + '($cupertino)', + (tester) async { + final gateway = _Gateway(); + addTearDown(gateway.close); + await _host(tester, gateway, cupertino: cupertino); + expect(find.byType(ChatResponsePending), findsNothing); + await _send(tester); + expect(find.byType(ChatResponsePending), findsOneWidget); + expect( + find.bySemanticsLabel('Assistant is responding'), + findsOneWidget, + ); + expect(find.byType(ChatMessageView), findsNWidgets(2)); + final opacities = tester + .widgetList( + find.descendant( + of: find.byType(ChatResponsePending), + matching: find.byType(Opacity), + ), + ) + .map((widget) => widget.opacity) + .toList(); + await tester.pump(const Duration(milliseconds: 200)); + expect( + tester + .widgetList( + find.descendant( + of: find.byType(ChatResponsePending), + matching: find.byType(Opacity), + ), + ) + .map((widget) => widget.opacity) + .toList(), + isNot(opacities), + ); + gateway.prompt.complete({}); + await _pump(tester); + expect(find.byType(ChatResponsePending), findsOneWidget); + + gateway.emitEvent(chatStepFrame('running')); + gateway.emitEvent({ + 'session_id': 's', + 'kind': 'reasoning', + 'message_id': 'a', + 'part': {'id': 'reasoning', 'type': 'reasoning'}, + 'delta': 'Thinking through this.', + }); + await _pump(tester); + expect(find.byType(ChatResponsePending), findsOneWidget); + gateway.emitEvent({ + 'session_id': 's', + 'kind': 'tool', + 'message_id': 'a', + 'part': { + 'id': 'tool-1', + 'type': 'tool', + 'name': 'read', + 'state': {'status': 'completed'}, + }, + }); + await _pump(tester); + expect(find.byType(ChatResponsePending), findsOneWidget); + expect(find.text('1 tool'), findsOneWidget); + expect(find.byType(ChatMessageView), findsNWidgets(2)); + expect( + tester.getTopLeft(find.byType(ChatResponsePending)).dy, + lessThan(tester.getTopLeft(find.text('1 tool')).dy), + ); + + gateway.emitEvent(chatTextFrame('Here is the answer.')); + await _pump(tester); + expect(find.byType(ChatResponsePending), findsNothing); + expect(find.text('Here is the answer.'), findsOneWidget); + expect(find.text('1 tool'), findsOneWidget); + gateway.emitEvent(chatStepFrame('ended')); + await tester.pumpAndSettle(); + await tester.pumpWidget(const SizedBox.shrink()); + }, + ); + } + + for (final result in ['refused', 'queued', 'ended']) { + testWidgets( + '$result clears a response shell without leaving fake history', + (tester) async { + final gateway = _Gateway(); + addTearDown(gateway.close); + await _host(tester, gateway); + await _send(tester); + expect(find.byType(ChatResponsePending), findsOneWidget); + if (result == 'refused') { + gateway.prompt.completeError(StateError('Send refused')); + } else { + gateway.prompt.complete({'chat_queued': result == 'queued'}); + if (result == 'ended') gateway.emitEvent(chatStepFrame('ended')); + } + await tester.pumpAndSettle(); + expect(find.byType(ChatResponsePending), findsNothing); + expect( + tester + .widgetList(find.byType(ChatMessageView)) + .where((view) => view.row['kind'] == 'assistant'), + isEmpty, + ); + }, + ); + } + + testWidgets('reduced motion keeps a static accessible ellipsis', ( + tester, + ) async { + await tester.pumpWidget( + const MaterialApp( + home: MediaQuery( + data: MediaQueryData(disableAnimations: true), + child: ChatResponsePending(), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.text('.'), findsNWidgets(3)); + expect(find.bySemanticsLabel('Assistant is responding'), findsOneWidget); + expect(tester.binding.hasScheduledFrame, isFalse); + }); +} diff --git a/app/tool/chat_response_pending_fixture.dart b/app/tool/chat_response_pending_fixture.dart new file mode 100644 index 00000000..244b57b0 --- /dev/null +++ b/app/tool/chat_response_pending_fixture.dart @@ -0,0 +1,109 @@ +// Device-only fixture: exercises the real thread without a server or real chat. +import 'dart:async'; + +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'; +import 'package:pipelines_app/ui/theme/app_theme.dart'; + +void main() => runApp(const _FixtureApp()); + +class _FixtureGateway implements ApiGateway { + final events = StreamController.broadcast(); + + @override + Stream get frames => events.stream; + + @override + Future call(String type, [JsonMap fields = const {}]) async { + if (type == 'chat_messages') { + return { + 'chat_history': {'messages': [], 'next_cursor': '', 'has_more': false}, + }; + } + if (type == 'chat_prompt') return {'chat_queued': false}; + return {}; + } + + void emit(JsonMap body) => events.add({ + 'type': 'chat_event', + 'chat_event': {'session_id': 'fixture', ...body}, + }); +} + +class _FixtureApp extends StatefulWidget { + const _FixtureApp(); + + @override + State<_FixtureApp> createState() => _FixtureAppState(); +} + +class _FixtureAppState extends State<_FixtureApp> { + final gateway = _FixtureGateway(); + final drafts = MemoryChatDraftStore(); + + @override + void dispose() { + unawaited(gateway.events.close()); + super.dispose(); + } + + @override + Widget build(BuildContext context) => MaterialApp( + theme: appLightTheme(), + darkTheme: appDarkTheme(), + home: Scaffold( + appBar: AppBar( + title: const Text('Response fixture — no server'), + actions: [ + IconButton( + tooltip: 'Fixture tool only', + icon: const Icon(Icons.build), + onPressed: () { + gateway.emit({ + 'kind': 'step', + 'step': {'status': 'running', 'turn_id': 'fixture-turn'}, + }); + gateway.emit({ + 'kind': 'tool', + 'message_id': 'fixture-answer', + 'part': { + 'type': 'tool', + 'id': 'tool', + 'name': 'read', + 'state': {'status': 'completed'}, + }, + }); + }, + ), + IconButton( + tooltip: 'Fixture answer text', + icon: const Icon(Icons.text_fields), + onPressed: () => gateway.emit({ + 'kind': 'text', + 'message_id': 'fixture-answer', + 'part': {'type': 'text', 'id': 'text'}, + 'delta': 'The answer replaces the animated dots.', + }), + ), + IconButton( + tooltip: 'Fixture finish', + icon: const Icon(Icons.done), + onPressed: () => gateway.emit({ + 'kind': 'step', + 'step': {'status': 'ended', 'turn_id': 'fixture-turn'}, + }), + ), + ], + ), + body: ChatThreadView( + gateway: gateway, + sessionId: 'fixture', + title: 'Fixture chat', + drafts: drafts, + onBack: () {}, + ), + ), + ); +} diff --git a/app/tool/chat_response_pending_probe.mjs b/app/tool/chat_response_pending_probe.mjs new file mode 100644 index 00000000..810a9078 --- /dev/null +++ b/app/tool/chat_response_pending_probe.mjs @@ -0,0 +1,86 @@ +// Real-browser Send -> pending bubble -> tools -> answer, against loopback data. +import assert from 'node:assert/strict'; +import { mkdir, mkdtemp, writeFile } from 'node:fs/promises'; +import { once } from 'node:events'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { startFixture } from './runs_probe_server.mjs'; +import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs'; +import { axNodes, enableSemantics, waitFor, capture, click, clickAt } from './pipelines_probe_cdp_input.mjs'; + +const platform = process.env.PROBE_PLATFORM ?? 'material'; +const output = resolve(process.argv[3] ?? `build/chat-response-pending-${platform}`); +await mkdir(output, { recursive: true }); +const fixture = await startFixture({ root: resolve(process.argv[2]), chatMessages: [] }); +const profile = await mkdtemp(join(tmpdir(), 'chat-response-pending-')); +const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', profile, 'about:blank'); +const log = { platform, checks: [], captures: [] }; +let c; +const names = async () => (await axNodes(c)).map(n => n.name?.value ?? ''); +const pending = async () => (await axNodes(c)).find(n => n.name?.value === 'Assistant is responding' && n.backendDOMNodeId); +const emit = async body => { + const response = await fetch(fixture.url + '/fixture/chat-event', { method: 'POST', body: JSON.stringify(body) }); + assert.ok(response.ok); +}; +try { + c = await connect((await waitForPage(await waitForPort(join(profile, 'DevToolsActivePort')))).webSocketDebuggerUrl); + await c.send('Page.enable'); + if (platform === 'material') await c.send('Emulation.setUserAgentOverride', { + userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36', platform: 'Win32', + }); + await c.send('Emulation.setDeviceMetricsOverride', { width: 1280, height: 800, deviceScaleFactor: 1, mobile: false }); + await c.send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-reduced-motion', value: 'no-preference' }] }); + await c.send('Page.navigate', { url: fixture.url + '/chat/ses_probechat' }); + assert.ok(await waitFor(() => c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 30000)); + await enableSemantics(c); + const field = await waitFor(async () => (await axNodes(c)).find(n => n.role?.value === 'textbox' && n.backendDOMNodeId), 30000); + assert.ok(field, 'composer field'); + const { model } = await c.send('DOM.getBoxModel', { backendNodeId: field.backendDOMNodeId }); + const [x1, y1, , , x3, y3] = model.content; + await clickAt(c, (x1 + x3) / 2, (y1 + y3) / 2); + await c.send('Input.insertText', { text: 'Fixture prompt before any response tokens.' }); + await sleep(150); + await click(c, 'Send'); + assert.ok(await waitFor(pending, 10000), 'assistant bubble before a server response frame'); + await sleep(500); // The accepted prompt still has no assistant frames. + await capture(c, output, log, '01-before-first-frame'); + log.checks.push('normal assistant response bubble before any frames or tokens'); + + const node = await pending(); + const { model: dots } = await c.send('DOM.getBoxModel', { backendNodeId: node.backendDOMNodeId }); + const [dx1, dy1, , , dx3, dy3] = dots.content; + const clip = { x: dx1, y: dy1, width: dx3 - dx1, height: dy3 - dy1, scale: 1 }; + const first = await c.send('Page.captureScreenshot', { format: 'png', clip }); + await sleep(250); + const second = await c.send('Page.captureScreenshot', { format: 'png', clip }); + assert.notEqual(first.data, second.data, 'ellipsis pixels animate without response events'); + log.checks.push('ellipsis visibly animates independently of the transcript'); + + await emit({ kind: 'step', step: { status: 'running', turn_id: 'fixture-turn' } }); + await emit({ kind: 'tool', message_id: 'answer', part: { + type: 'tool', id: 'tool', name: 'read', state: { status: 'completed' }, + } }); + assert.ok(await waitFor(async () => (await names()).some(name => name.startsWith('1 tool call')), 10000)); + assert.ok(await pending(), 'response bubble remains above tool-only activity'); + await capture(c, output, log, '02-tool-only-response'); + log.checks.push('tool-only response retains its pending answer bubble'); + + await emit({ kind: 'text', message_id: 'answer', part: { type: 'text', id: 'text' }, delta: 'The first answer tokens replace the dots.' }); + await emit({ kind: 'step', step: { status: 'ended', turn_id: 'fixture-turn' } }); + assert.ok(await waitFor(async () => (await names()).includes('The first answer tokens replace the dots.'), 10000)); + assert.equal(await pending(), undefined); + await capture(c, output, log, '03-answer-text'); + log.checks.push('answer text replaces the dots; finished turn has no pending animation'); + console.log(JSON.stringify(log, null, 2)); +} catch (error) { + if (c) { + await capture(c, output, log, 'failure'); + await writeFile(join(output, 'failure-ax.json'), JSON.stringify(await axNodes(c), null, 2)); + } + throw error; +} finally { + await writeFile(join(output, 'result.json'), JSON.stringify(log, null, 2)); + c?.close(); + const exited = once(chrome, 'exit'); chrome.kill(); await exited; + await fixture.close(); +}