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) {
@@ -0,0 +1,211 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../../../ui/text/monospace.dart';
import '../../domain/chat_tool_calls.dart';
import '../sessions/chat_adaptive.dart';
import 'native_disclosure.dart';
/// A turn's one tool summary below its answer, as the web's chip: `N tools`,
/// spinning while a call runs and tinted when one failed. It opens a sheet with
/// one row per call.
class ChatToolCallsChip extends StatelessWidget {
const ChatToolCallsChip({super.key, required this.calls});
final List<JsonMap> calls;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final running = calls.any(chatToolRunning);
final failed = calls.any(chatToolFailed);
final color = failed
? (cupertino
? CupertinoColors.systemRed.resolveFrom(context)
: Theme.of(context).colorScheme.error)
: (cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant);
final label = chatToolCallsLabel(calls.length);
final leading = running
? (cupertino
? const CupertinoActivityIndicator(radius: 7)
: const SizedBox.square(
dimension: 12,
child: CircularProgressIndicator(strokeWidth: 2),
))
: Icon(
cupertino ? CupertinoIcons.wrench : Icons.build_outlined,
size: 14,
color: color,
);
final content = Row(
mainAxisSize: MainAxisSize.min,
children: [
leading,
const SizedBox(width: 6),
Text(label, style: TextStyle(color: color, fontSize: 13)),
Icon(
cupertino ? CupertinoIcons.chevron_up : Icons.expand_less,
size: 14,
color: color,
),
],
);
return Semantics(
button: true,
// 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',
excludeSemantics: true,
child: Padding(
padding: const EdgeInsets.only(top: 2),
child: cupertino
? CupertinoButton(
key: const Key('chat-tool-chip'),
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
minimumSize: Size.zero,
onPressed: () => _open(context, cupertino),
child: content,
)
: InkWell(
key: const Key('chat-tool-chip'),
borderRadius: BorderRadius.circular(8),
onTap: () => _open(context, cupertino),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 6,
),
child: content,
),
),
),
);
}
Future<void> _open(BuildContext context, bool cupertino) {
final sheet = _ToolCallsSheet(calls: calls);
if (cupertino) {
return showCupertinoModalPopup<void>(
context: context,
builder: (_) => CupertinoPopupSurface(child: SafeArea(child: sheet)),
);
}
return showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
showDragHandle: true,
useSafeArea: true,
builder: (_) => sheet,
);
}
}
class _ToolCallsSheet extends StatelessWidget {
const _ToolCallsSheet({required this.calls});
final List<JsonMap> calls;
@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-tool-sheet'),
shrinkWrap: true,
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
chatToolCallsLabel(calls.length),
style: const TextStyle(fontWeight: FontWeight.w600),
),
),
for (final call in calls) _ToolCallRow(call: call),
],
),
),
);
}
}
/// One call: status glyph, name, its command/description and duration; it
/// opens to the formatted INPUT and OUTPUT (or ERROR).
class _ToolCallRow extends StatelessWidget {
const _ToolCallRow({required this.call});
final JsonMap call;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final detail = chatToolDetail(call);
final duration = chatToolDurationMillis(call);
final failed = chatToolFailed(call);
final running = chatToolRunning(call);
final title = [
chatToolLabel(call),
?detail,
if (duration != null) formatChatToolDuration(duration),
if (failed) 'failed',
if (running) 'running',
].join(' · ');
return NativeDisclosure(
label: title,
childBuilder: (context) {
final mono = const TextStyle(
fontFamily: appMonospaceFamily,
fontSize: 12,
);
final input = chatToolInputText(call);
final output = failed
? chatToolErrorText(call)
: chatToolOutputText(call);
Widget section(String heading, String body) => Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
heading,
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 2),
SelectableText(body, style: mono),
],
),
);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (input.isNotEmpty) section('INPUT', input),
if (output.isNotEmpty) section(failed ? 'ERROR' : 'OUTPUT', output),
],
);
},
);
}
}