From f2aa559d0e518d46eb6ecdf00b5a202fb274f80e Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 16:22:31 +0100 Subject: [PATCH] feat(app): chat reasoning is a chip beside the tool chip, opening its own sheet --- .../chat/ui/transcript/chat_message_view.dart | 22 +-- .../ui/transcript/chat_tool_calls_view.dart | 119 ++++++++++++++- .../conversation/chat_thread_view_test.dart | 135 ++---------------- .../ui/transcript/chat_message_view_test.dart | 78 +++++++--- app/tool/chat_message_probe.mjs | 22 ++- 5 files changed, 202 insertions(+), 174 deletions(-) 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 e7f71a30..5cf8f4eb 100644 --- a/app/lib/features/chat/ui/transcript/chat_message_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_message_view.dart @@ -188,8 +188,8 @@ class ChatMessageView extends StatelessWidget { return widgets; } - /// The agent turn as the web lays it out: its reasoning as a quiet line - /// above the answer, the answer in the bubble, and one tool chip below. + /// The agent turn: the answer in the bubble, then one row with its reasoning + /// and tool-call chips, each opening its own sheet. Widget _agentTurn(BuildContext context, List content, Widget bubble) { final parts = _partsOf(row); final reasoning = [ @@ -206,16 +206,16 @@ class ChatMessageView extends StatelessWidget { mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ - if (reasoning.isNotEmpty) - NativeDisclosure( - key: const Key('chat-reasoning'), - label: reasoning.length == 1 - ? 'Reasoning' - : 'Reasoning · ${reasoning.length} steps', - childBuilder: (context) => SelectableText(reasoning.join('\n\n')), - ), if (content.isNotEmpty) bubble, - if (tools.isNotEmpty) ChatToolCallsChip(calls: tools), + // Reasoning and tool calls share one quiet row below the answer. + if (reasoning.isNotEmpty || tools.isNotEmpty) + Wrap( + spacing: 8, + children: [ + if (reasoning.isNotEmpty) ChatReasoningChip(steps: reasoning), + if (tools.isNotEmpty) ChatToolCallsChip(calls: tools), + ], + ), ], ); } diff --git a/app/lib/features/chat/ui/transcript/chat_tool_calls_view.dart b/app/lib/features/chat/ui/transcript/chat_tool_calls_view.dart index 378d9734..c2a9e31e 100644 --- a/app/lib/features/chat/ui/transcript/chat_tool_calls_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_tool_calls_view.dart @@ -41,6 +41,73 @@ class ChatToolCallsChip extends StatelessWidget { size: 14, color: color, ); + return _TurnChip( + chipKey: const Key('chat-tool-chip'), + leading: leading, + label: label, + color: color, + semanticsLabel: calls.length == 1 + ? '1 tool call, expand for details' + : '${calls.length} tool calls, expand for details', + sheet: _ToolCallsSheet(calls: calls), + ); + } +} + +/// A turn's reasoning as a chip beside its tool chip, the same way: `Reasoning` +/// or `Reasoning · N steps`, opening a sheet with the steps. +class ChatReasoningChip extends StatelessWidget { + const ChatReasoningChip({super.key, required this.steps}); + + final List steps; + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final color = cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant; + final label = steps.length == 1 + ? 'Reasoning' + : 'Reasoning · ${steps.length} steps'; + return _TurnChip( + chipKey: const Key('chat-reasoning-chip'), + leading: Icon( + cupertino ? CupertinoIcons.lightbulb : Icons.lightbulb_outline, + size: 14, + color: color, + ), + label: label, + color: color, + semanticsLabel: '$label, expand for details', + sheet: _ReasoningSheet(label: label, steps: steps), + ); + } +} + +/// The quiet chip shell both turn summaries share, and its native sheet. +class _TurnChip extends StatelessWidget { + const _TurnChip({ + required this.chipKey, + required this.leading, + required this.label, + required this.color, + required this.semanticsLabel, + required this.sheet, + }); + + final Key chipKey; + final Widget leading; + final String label; + final Color color; + final String semanticsLabel; + final Widget sheet; + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; final content = Row( mainAxisSize: MainAxisSize.min, children: [ @@ -59,22 +126,20 @@ class ChatToolCallsChip extends StatelessWidget { // The child's own action is excluded with its label, so the node // carries the activation for assistive technology. onTap: () => _open(context, cupertino), - label: calls.length == 1 - ? '1 tool call, expand for details' - : '${calls.length} tool calls, expand for details', + label: semanticsLabel, excludeSemantics: true, child: Padding( padding: const EdgeInsets.only(top: 2), child: cupertino ? CupertinoButton( - key: const Key('chat-tool-chip'), + key: chipKey, padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4), minimumSize: Size.zero, onPressed: () => _open(context, cupertino), child: content, ) : InkWell( - key: const Key('chat-tool-chip'), + key: chipKey, borderRadius: BorderRadius.circular(8), onTap: () => _open(context, cupertino), child: Padding( @@ -90,7 +155,6 @@ class ChatToolCallsChip extends StatelessWidget { } Future _open(BuildContext context, bool cupertino) { - final sheet = _ToolCallsSheet(calls: calls); if (cupertino) { return showCupertinoModalPopup( context: context, @@ -107,6 +171,49 @@ class ChatToolCallsChip extends StatelessWidget { } } +class _ReasoningSheet extends StatelessWidget { + const _ReasoningSheet({required this.label, required this.steps}); + + final String label; + final List steps; + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final style = cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : Theme.of(context).textTheme.bodyMedium; + return DefaultTextStyle.merge( + style: style, + child: ConstrainedBox( + constraints: BoxConstraints( + maxHeight: MediaQuery.sizeOf(context).height * 0.8, + ), + child: ListView( + key: const Key('chat-reasoning-sheet'), + shrinkWrap: true, + padding: const EdgeInsets.fromLTRB(16, 8, 16, 16), + children: [ + Padding( + padding: const EdgeInsets.only(bottom: 8), + child: Text( + label, + style: const TextStyle(fontWeight: FontWeight.w600), + ), + ), + for (final (index, step) in steps.indexed) + Padding( + padding: EdgeInsets.only(top: index == 0 ? 0 : 12), + child: SelectableText(step), + ), + ], + ), + ), + ); + } +} + class _ToolCallsSheet extends StatelessWidget { const _ToolCallsSheet({required this.calls}); diff --git a/app/test/features/chat/conversation/chat_thread_view_test.dart b/app/test/features/chat/conversation/chat_thread_view_test.dart index 255a9428..120a4c8b 100644 --- a/app/test/features/chat/conversation/chat_thread_view_test.dart +++ b/app/test/features/chat/conversation/chat_thread_view_test.dart @@ -132,125 +132,10 @@ void main() { expect(find.text('Load older'), findsNothing); }); - // Paging prepends messages, so an unkeyed transcript would reuse the element - // at a message's old index: the operator's open reasoning would move to the - // previous answer, or be dropped when `Load older` disappears. - for (final cupertino in [false, true]) { - testWidgets( - 'an open reasoning block stays on its answer while has_more remains ' - '($cupertino)', - (tester) async { - final gateway = FakeChatGateway(); - addTearDown(gateway.close); - var reads = 0; - gateway.history = (_) async => ++reads == 1 - ? pagedChatPage( - 'a2', - 'NEWEST reasoning body that belongs to the newest answer.', - cursor: 'opaque/older', - hasMore: true, - ) - : pagedChatPage( - 'a1', - 'OLDER reasoning body that must not steal the newest disclosure.', - cursor: 'opaque/older-2', - hasMore: true, - ); - - await tester.pumpWidget( - (cupertino ? cupertinoHost : host)( - ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}), - ), - ); - await tester.pumpAndSettle(); - - await tester.tap(find.text('Reasoning')); - await tester.pumpAndSettle(); - expect( - find.text('NEWEST reasoning body that belongs to the newest answer.'), - findsOneWidget, - ); - - await tester.tap(find.text('Load older')); - await tester.pumpAndSettle(); - - // Both answers are present; only the one the operator opened is open. - expect(find.text('Reasoning'), findsNWidgets(2)); - expect(find.text('Load older'), findsOneWidget); - expect( - find.text('NEWEST reasoning body that belongs to the newest answer.'), - findsOneWidget, - ); - expect( - find.text( - 'OLDER reasoning body that must not steal the newest disclosure.', - ), - findsNothing, - ); - }, - ); - - testWidgets( - 'an open reasoning block survives the removal of Load older ($cupertino)', - (tester) async { - final gateway = FakeChatGateway(); - addTearDown(gateway.close); - var reads = 0; - gateway.history = (_) async => ++reads == 1 - ? pagedChatPage( - 'a2', - 'NEWEST reasoning body that belongs to the newest answer.', - cursor: 'opaque/older', - hasMore: true, - ) - : pagedChatPage( - 'a1', - 'OLDER reasoning body that must not steal the newest disclosure.', - cursor: '', - hasMore: false, - ); - - await tester.pumpWidget( - (cupertino ? cupertinoHost : host)( - ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}), - ), - ); - await tester.pumpAndSettle(); - - await tester.tap(find.text('Reasoning')); - await tester.pumpAndSettle(); - expect( - find.text('NEWEST reasoning body that belongs to the newest answer.'), - findsOneWidget, - ); - - await tester.tap(find.text('Load older')); - await tester.pumpAndSettle(); - - expect(find.text('Reasoning'), findsNWidgets(2)); - expect(find.text('Load older'), findsNothing); - expect( - find.text('NEWEST reasoning body that belongs to the newest answer.'), - findsOneWidget, - ); - expect( - find.text( - 'OLDER reasoning body that must not steal the newest disclosure.', - ), - findsNothing, - ); - }, - ); - } - - // The repository's own paged-history fixture pages consecutive assistant - // messages, so an older page can prepend into the one folded turn. Its first - // message id changes; the row must still be the same element and keep the - // operator's open disclosure. for (final cupertino in [false, true]) { for (final keepsControl in [true, false]) { testWidgets( - 'an open reasoning block in a folded assistant turn survives paging ' + 'a folded assistant turn counts every reasoning step across paging ' '(keeps Load older: $keepsControl, $cupertino)', (tester) async { final gateway = FakeChatGateway(); @@ -283,28 +168,24 @@ void main() { ); await tester.pumpAndSettle(); - // The turn's reasoning is one quiet block above its answer, as on - // the web; it counts the steps it holds. + // The turn's reasoning is one chip below its answer; it counts the + // steps of every message folded into the turn. expect(find.text('Reasoning · 2 steps'), findsOneWidget); - await tester.tap(find.text('Reasoning · 2 steps')); - await tester.pumpAndSettle(); - expect( - find.textContaining('NEWEST folded reasoning body.'), - findsOneWidget, - ); await tester.tap(find.text('Load older')); await tester.pumpAndSettle(); - // All four assistant messages now fold into the same turn, and the - // open block stays open while it grows. expect(find.text('Reasoning · 4 steps'), findsOneWidget); + await tester.tap(find.text('Reasoning · 4 steps')); + await tester.pumpAndSettle(); for (final body in [ 'NEWEST folded reasoning body.', 'OLDEST folded reasoning body.', ]) { - expect(find.textContaining(body), findsOneWidget, reason: body); + expect(find.text(body), findsOneWidget, reason: body); } + Navigator.of(tester.element(find.text('NEWEST folded reasoning body.'))).pop(); + await tester.pumpAndSettle(); expect( find.text('Load older'), keepsControl ? findsOneWidget : findsNothing, diff --git a/app/test/features/chat/ui/transcript/chat_message_view_test.dart b/app/test/features/chat/ui/transcript/chat_message_view_test.dart index 6c41ec29..b47aec8d 100644 --- a/app/test/features/chat/ui/transcript/chat_message_view_test.dart +++ b/app/test/features/chat/ui/transcript/chat_message_view_test.dart @@ -124,22 +124,24 @@ void main() { expect(find.textContaining('"payload": 7'), findsNothing); expect(find.text('plain future text'), findsOneWidget); - // As the web: reasoning is a quiet line above the answer, the answer - // keeps its own order in the bubble, and the tool calls are one chip below. + // The answer keeps its own order in the bubble; reasoning and the tool + // calls are two chips in one row below it. double top(String text) => tester.getTopLeft(find.text(text)).dy; - expect(top('Reasoning'), lessThan(top('First answer'))); expect(top('First answer'), lessThan(top('Second answer'))); expect( top('Second answer'), lessThan(top('Unrecognised part: future_part')), ); expect(top('Unrecognised part: future_part'), lessThan(top('1 tool'))); + expect(top('Unrecognised part: future_part'), lessThan(top('Reasoning'))); await tester.ensureVisible(find.text('Reasoning')); await tester.pumpAndSettle(); await tester.tap(find.text('Reasoning')); await tester.pumpAndSettle(); expect(find.text('private reasoning'), findsOneWidget); + await tester.tapAt(const Offset(10, 10)); + await tester.pumpAndSettle(); // The chip opens the calls; a call opens to its formatted input. await tester.ensureVisible(find.text('1 tool')); @@ -159,6 +161,48 @@ void main() { expect(find.textContaining('"payload": 7'), findsOneWidget); }); + for (final cupertino in [false, true]) { + testWidgets('reasoning and tool chips share one row below the answer ' + '(${cupertino ? 'cupertino' : 'material'})', (tester) async { + // The test font draws every glyph as a full square, so the chips need + // more room than real text does on a phone. + tester.view.physicalSize = const Size(800, 844); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + _host( + _assistant([ + {'type': 'reasoning', 'text': 'first thought'}, + {'type': 'text', 'text': 'The answer'}, + {'type': 'reasoning', 'text': 'second thought'}, + { + 'type': 'tool', + 'name': 'shell', + 'state': {'status': 'completed'}, + }, + ]), + cupertino: cupertino, + ), + ); + await tester.pumpAndSettle(); + + final reasoning = tester.getRect(find.text('Reasoning · 2 steps')); + final tools = tester.getRect(find.text('1 tool')); + expect( + reasoning.top, + greaterThan(tester.getRect(find.text('The answer')).bottom), + ); + expect(reasoning.center.dy, moreOrLessEquals(tools.center.dy)); + expect(reasoning.right, lessThan(tools.left)); + + // Like the tool chip, it opens a sheet with every step. + await tester.tap(find.text('Reasoning · 2 steps')); + await tester.pumpAndSettle(); + expect(find.text('first thought'), findsOneWidget); + expect(find.text('second thought'), findsOneWidget); + }); + } + testWidgets( 'Material disclosure bodies start at the label edge at 1x and 2x', (tester) async { @@ -169,19 +213,20 @@ void main() { await tester.pumpWidget( _host( _assistant([ - {'type': 'reasoning', 'text': 'short thought'}, + {'type': 'future_part', 'payload': 'short thought'}, ]), scale: scale, ), ); await tester.pumpAndSettle(); - await tester.tap(find.text('Reasoning')); + await tester.tap(find.text('Unrecognised part: future_part')); await tester.pumpAndSettle(); - final label = tester.getTopLeft(find.text('Reasoning')); - final body = tester.getTopLeft(find.text('short thought')); - expect(body.dx, label.dx, reason: 'short reasoning body at ${scale}x'); - expect(find.text('short thought'), findsOneWidget); + final label = tester.getTopLeft( + find.text('Unrecognised part: future_part'), + ); + final body = tester.getTopLeft(find.textContaining('short thought')); + expect(body.dx, label.dx, reason: 'disclosure body at ${scale}x'); } }, ); @@ -253,6 +298,8 @@ void main() { await tester.tap(find.text('Reasoning')); await tester.pumpAndSettle(); expect(find.text('thought'), findsOneWidget); + await tester.tapAt(const Offset(10, 10)); + await tester.pumpAndSettle(); await tester.tap(find.text('1 tool')); await tester.pumpAndSettle(); @@ -269,7 +316,7 @@ void main() { await tester.pumpWidget( _host( _assistant([ - {'type': 'reasoning', 'text': 'thought'}, + {'type': 'future_part', 'payload': 'thought'}, ]), cupertino: true, ), @@ -277,13 +324,13 @@ void main() { await tester.pumpAndSettle(); Tristate state() => tester - .getSemantics(find.bySemanticsLabel('Reasoning')) + .getSemantics(find.bySemanticsLabel('Unrecognised part: future_part')) .getSemanticsData() .flagsCollection .isExpanded; expect(state(), Tristate.isFalse); - await tester.tap(find.text('Reasoning')); + await tester.tap(find.text('Unrecognised part: future_part')); await tester.pumpAndSettle(); expect(state(), Tristate.isTrue); handle.dispose(); @@ -335,10 +382,9 @@ void main() { await tester.pumpAndSettle(); final row = tester.getRect(find.byType(ChatMessageView)); final bubble = tester.getRect( - find.ancestor( - of: find.text('Ada phone'), - matching: find.byType(Material), - ).first, + find + .ancestor(of: find.text('Ada phone'), matching: find.byType(Material)) + .first, ); expect(bubble.width, lessThanOrEqualTo(row.width * 0.85 + 0.5)); expect(bubble.right, closeTo(row.right, 1)); diff --git a/app/tool/chat_message_probe.mjs b/app/tool/chat_message_probe.mjs index ad1eaec8..6189e6ca 100644 --- a/app/tool/chat_message_probe.mjs +++ b/app/tool/chat_message_probe.mjs @@ -312,7 +312,7 @@ try { for (const name of [ 'User message', 'Assistant message', - 'Reasoning', + 'Reasoning, expand for details', '1 tool call, expand for details', 'Unrecognised part: future_part', 'Ada phone', @@ -347,22 +347,16 @@ try { throw new Error(`Closed disclosure leaked ${JSON.stringify(hidden)}`); } } - const reasoning = await requireNode('Reasoning'); - evidence.checks.push({ - check: 'closed disclosures expose their collapsed state', - host: style, - ariaExpanded: reasoning.expanded, - }); - - await click('Reasoning'); + // Reasoning is a chip beside the tool chip; it opens a sheet. + await click('Reasoning, expand for details'); const openReasoning = await capture(`${style}-reasoning-expanded`); await assertOcr(openReasoning, 'The user asked for exact text'); - const openReasoningNode = await requireNode('Reasoning'); - if (style === 'cupertino' && openReasoningNode.expanded !== 'true') { - throw new Error( - `Cupertino reasoning did not announce aria-expanded=true: ${JSON.stringify(openReasoningNode)}`, - ); + // Dismiss the sheet through its barrier. + for (const type of ['mousePressed', 'mouseReleased']) { + await send('Input.dispatchMouseEvent', { type, x: 10, y: 10, button: 'left', clickCount: 1 }, session); } + await delay(800); + await click('Unrecognised part: future_part'); const openUnknown = await capture(`${style}-unknown-expanded`); for (const text of ['future_part', 'payload', 'kept', 'count']) {