feat(app): chat turns show reasoning above the answer and tool calls as one chip with a formatted call sheet, as the web

This commit is contained in:
2026-10-23 15:52:51 +01:00
parent c2f1a4cb94
commit 522729ad57
7 changed files with 516 additions and 48 deletions
@@ -5,6 +5,8 @@ import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../sessions/chat_adaptive.dart';
import '../../domain/chat_tool_calls.dart';
import 'chat_tool_calls_view.dart';
import 'native_disclosure.dart';
/// Builds an assistant text part; the default keeps the server's text exactly.
@@ -77,7 +79,7 @@ class ChatMessageView extends StatelessWidget {
? (constraints.maxWidth * 0.85).clamp(0, 560).toDouble()
: 560,
),
child: bubble,
child: user ? bubble : _agentTurn(context, content, bubble),
),
),
),
@@ -186,6 +188,38 @@ 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.
Widget _agentTurn(BuildContext context, List<Widget> content, Widget bubble) {
final parts = _partsOf(row);
final reasoning = [
for (final part in parts)
if (part['type'] == 'reasoning' &&
'${part['text'] ?? ''}'.trim().isNotEmpty)
'${part['text']}',
];
final tools = [
for (final part in parts)
if (isChatToolPart(part)) part,
];
return Column(
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),
],
);
}
List<Widget> _agentParts(BuildContext context) {
final widgets = <Widget>[];
final parts = _partsOf(row);
@@ -227,19 +261,8 @@ class ChatMessageView extends StatelessWidget {
if (!hasText) return type.isEmpty ? _unrecognised(part) : null;
return textBuilder?.call(context, text) ?? SelectableText(text);
}
if (type == 'reasoning') {
if (!hasText) return null;
return NativeDisclosure(
label: 'Reasoning',
childBuilder: (context) => SelectableText(text),
);
}
if (type == 'tool') {
return NativeDisclosure(
label: _toolLabel(part),
childBuilder: (context) => _raw(part),
);
}
// Reasoning and tool calls are drawn outside the bubble (_agentTurn).
if (type == 'reasoning' || type == 'tool') return null;
if (type == 'file') {
if (_isFile(part)) return _attachment(context, part);
return hasText ? SelectableText(text) : null;
@@ -300,11 +323,6 @@ bool _isFile(JsonMap part) =>
part['type'] == 'file' &&
'${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty;
String _toolLabel(JsonMap part) {
final name = '${part['name'] ?? ''}'.trim();
return name.isEmpty ? 'tool' : name;
}
/// The web's attachment name rule: its own name, else the URI basename, else
/// `file`, so an attachment never shows an empty label.
String _attachmentDisplayName(Object? name, Object? uri) {