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 dfbc1c16..dc049554 100644 --- a/app/lib/features/chat/ui/transcript/chat_message_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_message_view.dart @@ -203,6 +203,18 @@ class ChatMessageView extends StatelessWidget { if (isChatToolPart(part)) part, ]; final model = '${row['model'] ?? ''}'.trim(); + final footer = [ + if (tools.isNotEmpty) ChatToolCallsChip(calls: tools), + if (model.isNotEmpty) + Text( + model, + key: const Key('chat-reply-model'), + style: TextStyle( + fontSize: 12, + color: _secondaryColor(context, _isCupertino(context)), + ), + ), + ]; return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, @@ -213,24 +225,13 @@ class ChatMessageView extends StatelessWidget { key: const Key('chat-turn-reasoning'), steps: reasoning, streaming: row['streaming'] == true, + trailing: footer, ), - if (tools.isNotEmpty || model.isNotEmpty) + if (reasoning.isEmpty && footer.isNotEmpty) Wrap( spacing: 8, crossAxisAlignment: WrapCrossAlignment.center, - children: [ - if (tools.isNotEmpty) ChatToolCallsChip(calls: tools), - // Which model answered, quietly, after the chips. - if (model.isNotEmpty) - Text( - model, - key: const Key('chat-reply-model'), - style: TextStyle( - fontSize: 12, - color: _secondaryColor(context, _isCupertino(context)), - ), - ), - ], + children: footer, ), ], ); diff --git a/app/lib/features/chat/ui/transcript/chat_reasoning_disclosure.dart b/app/lib/features/chat/ui/transcript/chat_reasoning_disclosure.dart index 38048320..5af336f9 100644 --- a/app/lib/features/chat/ui/transcript/chat_reasoning_disclosure.dart +++ b/app/lib/features/chat/ui/transcript/chat_reasoning_disclosure.dart @@ -2,52 +2,100 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../sessions/chat_adaptive.dart'; -import 'native_disclosure.dart'; /// Only reasoning actually supplied by the backend. Keeping its body in the /// transcript (rather than a modal snapshot) lets an open disclosure rebuild /// with every delta, even while answer text is also arriving. -class ChatReasoningDisclosure extends StatelessWidget { +class ChatReasoningDisclosure extends StatefulWidget { const ChatReasoningDisclosure({ super.key, required this.steps, required this.streaming, + this.trailing = const [], }); final List steps; final bool streaming; + final List trailing; + + @override + State createState() => + _ChatReasoningDisclosureState(); +} + +class _ChatReasoningDisclosureState extends State { + late bool _expanded = widget.streaming; + + void _toggle() => setState(() => _expanded = !_expanded); @override Widget build(BuildContext context) { final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; - final label = steps.length == 1 + final label = widget.steps.length == 1 ? 'Reasoning' - : 'Reasoning · ${steps.length} steps'; - final disclosure = NativeDisclosure( - key: const Key('chat-reasoning-disclosure'), + : 'Reasoning · ${widget.steps.length} steps'; + final control = Semantics( + key: const Key('chat-reasoning-toggle'), + button: true, + expanded: _expanded, label: label, - initiallyExpanded: streaming, - childBuilder: (_) => Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - for (final (index, step) in steps.indexed) - Padding( - padding: EdgeInsets.only(top: index == 0 ? 0 : 12, bottom: 8), - child: SelectableText(step), + onTap: _toggle, + excludeSemantics: true, + child: cupertino + ? CupertinoButton( + padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4), + minimumSize: Size.zero, + onPressed: _toggle, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Flexible(child: Text(label)), + Icon( + _expanded + ? CupertinoIcons.chevron_up + : CupertinoIcons.chevron_down, + size: 14, + ), + ], + ), + ) + : TextButton( + onPressed: _toggle, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Flexible(child: Text(label)), + Icon( + _expanded ? Icons.expand_less : Icons.expand_more, + size: 14, + ), + ], + ), ), - ], - ), ); - // Material's native ExpansionTile needs a surface; Cupertino must not - // acquire a Material dependency just to show its own disclosure button. return DefaultTextStyle.merge( style: cupertino ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.bodyMedium, - child: cupertino - ? disclosure - : Material(color: Colors.transparent, child: disclosure), + child: Column( + key: const Key('chat-reasoning-disclosure'), + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Wrap( + key: const Key('chat-message-footer'), + spacing: 8, + crossAxisAlignment: WrapCrossAlignment.center, + children: [control, ...widget.trailing], + ), + if (_expanded) + for (final (index, step) in widget.steps.indexed) + Padding( + padding: EdgeInsets.only(top: index == 0 ? 0 : 12, bottom: 8), + child: SelectableText(step), + ), + ], + ), ); } } diff --git a/app/notes/per-message-reasoning-footer.md b/app/notes/per-message-reasoning-footer.md new file mode 100644 index 00000000..8bf4d1c1 --- /dev/null +++ b/app/notes/per-message-reasoning-footer.md @@ -0,0 +1,18 @@ +# Per-message reasoning footer + +User requested each assistant message's Reasoning control to the left of its +tools count. The compact native disclosure now shares the footer with tools and +model metadata; expanded text remains inline below and updates while streaming. +Each message retains independent expansion state, including through paging. +No reasoning is fabricated when the backend provides none. Narrow/scaled layouts +may wrap controls in their logical order instead of clipping them. + +- Both-host layout regressions failed before the change. +- New per-message tests cover left-to-right placement and independent expansion. +- Existing live reasoning, paging, collapse, resync and queued-send tests pass. +- Full gate: clean analysis and **1,850 tests pass**. +- Browser: 18 checks/10 captures per host in `build/reasoning-footer-material/` + and `build/reasoning-footer-cupertino/`, including actual footer geometry and + live reasoning updates. Both footer captures visually inspected. +- Web/APK publication pending. No backend changes, service restarts, production + chat/run mutations or device use. On-device behavior remains unverified. 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 17087ae5..97e3e890 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 @@ -161,7 +161,80 @@ void main() { }); for (final cupertino in [false, true]) { - testWidgets('reasoning disclosure and tool chip follow the answer ' + testWidgets( + 'each message keeps its reasoning beside its own tools ($cupertino)', + (tester) async { + _phone(tester); + final first = _assistant([ + {'type': 'text', 'text': 'First message'}, + {'type': 'reasoning', 'text': 'First reasoning'}, + { + 'type': 'tool', + 'name': 'read', + 'state': {'status': 'completed'}, + }, + ]); + final second = _assistant([ + {'type': 'text', 'text': 'Second message'}, + {'type': 'reasoning', 'text': 'Second reasoning'}, + { + 'type': 'tool', + 'name': 'read', + 'state': {'status': 'completed'}, + }, + { + 'type': 'tool', + 'name': 'write', + 'state': {'status': 'completed'}, + }, + ]); + final body = ListView( + children: [ + ChatMessageView(key: const Key('message-first'), row: first), + ChatMessageView(key: const Key('message-second'), row: second), + ], + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: body)) + : MaterialApp(home: Scaffold(body: body)), + ); + await tester.pumpAndSettle(); + for (final (key, count) in [ + ('message-first', '1 tool'), + ('message-second', '2 tools'), + ]) { + final message = find.byKey(Key(key)); + final reasoning = find.descendant( + of: message, + matching: find.text('Reasoning'), + ); + final tools = find.descendant( + of: message, + matching: find.text(count), + ); + expect( + tester.getRect(reasoning).right, + lessThan(tester.getRect(tools).left), + ); + expect( + tester.getRect(reasoning).center.dy, + closeTo(tester.getRect(tools).center.dy, 2), + ); + } + await tester.tap( + find.descendant( + of: find.byKey(const Key('message-first')), + matching: find.text('Reasoning'), + ), + ); + await tester.pumpAndSettle(); + expect(find.text('First reasoning'), findsOneWidget); + expect(find.text('Second reasoning'), findsNothing); + }, + ); + + testWidgets('reasoning sits immediately left of the tools count ' '(${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. @@ -191,7 +264,8 @@ void main() { reasoning.top, greaterThan(tester.getRect(find.text('The answer')).bottom), ); - expect(reasoning.bottom, lessThan(tools.top)); + expect(reasoning.center.dy, closeTo(tools.center.dy, 2)); + expect(reasoning.right, lessThan(tools.left)); // The inline disclosure opens with every recorded step. await tester.tap(find.text('Reasoning · 2 steps')); diff --git a/app/tool/chat_stage_debug_probe.mjs b/app/tool/chat_stage_debug_probe.mjs index 22e0f82e..a38550b1 100644 --- a/app/tool/chat_stage_debug_probe.mjs +++ b/app/tool/chat_stage_debug_probe.mjs @@ -96,6 +96,17 @@ try { part: { id: 'reasoning-0', type: 'reasoning' }, delta: 'Checking the recorded evidence.' }); await event({ kind: 'text', message_id: 'live-answer', part: { id: 'text-0', type: 'text' }, delta: 'The answer is arriving.' }); + await event({ kind: 'tool', message_id: 'live-answer', + part: { id: 'tool-0', type: 'tool', name: 'read', state: { status: 'completed' } } }); + const footerNodes = await axNodes(connection); + const reasoningControl = footerNodes.find(node => node.name?.value === 'Reasoning' && node.role?.value === 'button'); + const toolsControl = footerNodes.find(node => node.name?.value === '1 tool call, expand for details'); + assert.ok(reasoningControl?.backendDOMNodeId && toolsControl?.backendDOMNodeId, 'message footer controls'); + const reasoningBox = (await connection.send('DOM.getBoxModel', { backendNodeId: reasoningControl.backendDOMNodeId })).model.content; + const toolsBox = (await connection.send('DOM.getBoxModel', { backendNodeId: toolsControl.backendDOMNodeId })).model.content; + assert.ok(reasoningBox[2] < toolsBox[0], 'reasoning is left of tools'); + assert.ok(Math.abs((reasoningBox[1] + reasoningBox[5]) / 2 - (toolsBox[1] + toolsBox[5]) / 2) < 3, 'same footer row'); + log.checks.push('per-message reasoning is immediately left of tools count'); await assertPainted('Checking the recorded evidence.', 'reasoning visible while text streams', 'chat-live-reasoning'); await assertPainted('The answer is arriving.', 'streamed answer visible', 'chat-live-answer'); const field = (await axNodes(connection)).find(node => node.role?.value === 'textbox');