Files
pipelines/app/lib/features/chat/ui/transcript/chat_message_view.dart
T
alan 9e4a925587 feat(app): add native chat transcript message row
Render one transcriptRows row with native controls: role-labelled user and
agent messages, verbatim user text, client and attachment names (own name,
URI basename, else file), ordered agent parts, and collapsed reasoning, tool
and unknown-part bodies using ExpansionTile or a Cupertino disclosure.

Blank known parts draw nothing so they no longer fall through to the
unknown-part disclosure; closed bodies build lazily. Adds focused widget
tests, the fixture probe pair and the C2 evidence note.
2026-10-21 20:44:00 +01:00

196 lines
6.5 KiB
Dart

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../sessions/chat_adaptive.dart';
import 'native_disclosure.dart';
/// Builds an assistant text part; the default keeps the server's text exactly.
typedef ChatMessageTextBuilder = Widget Function(BuildContext, String);
/// One `transcriptRows` row: a user message or one folded agent turn. Parts keep
/// order; reasoning/tool bodies are native disclosures and unknown parts keep raw JSON.
class ChatMessageView extends StatelessWidget {
const ChatMessageView({super.key, required this.row, this.textBuilder});
final JsonMap row;
final ChatMessageTextBuilder? textBuilder;
@override
Widget build(BuildContext context) {
final user = row['kind'] == 'user';
final role = user ? 'User' : 'Assistant';
final cupertino = _isCupertino(context);
final content = user ? _userParts(context) : _agentParts(context);
final body = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// The row's semantics label already says the role; announcing the
// visible label again would read it twice.
ExcludeSemantics(
child: Text(role, style: _roleStyle(context, cupertino)),
),
...content,
],
);
return Semantics(
container: true,
explicitChildNodes: true,
label: '$role message',
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: cupertino
? DefaultTextStyle.merge(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: body,
)
: body,
),
);
}
TextStyle? _roleStyle(BuildContext context, bool cupertino) =>
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.labelLarge)
?.copyWith(
fontWeight: FontWeight.w600,
color: _secondaryColor(context, cupertino),
);
Color _secondaryColor(BuildContext context, bool cupertino) => cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
bool _isCupertino(BuildContext context) =>
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
List<Widget> _userParts(BuildContext context) {
final message = row['message'] is Map
? Map<String, dynamic>.from(row['message'] as Map)
: row;
final parts = _partsOf(message);
final text = parts
.where((part) => part['type'] == null || part['type'] == 'text')
.map((part) => '${part['text'] ?? ''}')
.join('\n');
final widgets = <Widget>[
if (text.trim().isNotEmpty) SelectableText(text),
for (final part in parts.where(_isFile)) _attachment(context, part),
];
final client = '${message['client'] ?? ''}'.trim();
if (client.isNotEmpty) {
widgets.insert(0, Text(client, style: _clientStyle(context)));
}
return widgets;
}
List<Widget> _agentParts(BuildContext context) {
final widgets = <Widget>[];
for (final part in _partsOf(row)) {
final widget = _agentPart(context, part);
if (widget != null) widgets.add(widget);
}
return widgets;
}
/// One agent part, or null when a known part has nothing to draw. The web's
/// `partRendering` hides blank text/reasoning too, so a blank known part must
/// not fall through to the unknown-part disclosure.
Widget? _agentPart(BuildContext context, JsonMap part) {
final type = '${part['type'] ?? ''}';
final text = part['text'] is String ? part['text'] as String : '';
final hasText = text.trim().isNotEmpty;
if (type.isEmpty || type == 'text') {
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),
);
}
if (type == 'file') {
if (_isFile(part)) return _attachment(context, part);
return hasText ? SelectableText(text) : null;
}
if (hasText) return SelectableText(text);
return _unrecognised(part);
}
Widget _unrecognised(JsonMap part) {
final type = '${part['type'] ?? ''}';
return NativeDisclosure(
label: 'Unrecognised part${type.isEmpty ? '' : ': $type'}',
childBuilder: (context) => _raw(part),
);
}
TextStyle? _clientStyle(BuildContext context) {
final cupertino = _isCupertino(context);
return (cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodySmall)
?.copyWith(color: _secondaryColor(context, cupertino));
}
Widget _attachment(BuildContext context, JsonMap part) {
final cupertino = _isCupertino(context);
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(right: 8),
child: Icon(cupertino ? CupertinoIcons.paperclip : Icons.attach_file),
),
Expanded(
child: SelectableText(
_attachmentDisplayName(part['name'], part['uri']),
),
),
],
);
}
/// Pretty-printed on demand: a closed disclosure never pays for it.
Widget _raw(JsonMap part) =>
SelectableText(const JsonEncoder.withIndent(' ').convert(part));
}
List<JsonMap> _partsOf(Map<dynamic, dynamic> source) {
final parts = source['parts'];
if (parts is! List) return const [];
return parts
.whereType<Map>()
.map((part) => Map<String, dynamic>.from(part))
.toList();
}
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) {
final shown = '${name ?? ''}'.trim();
final basename = '${uri ?? ''}'.split('/').last.trim();
return shown.isNotEmpty ? shown : (basename.isEmpty ? 'file' : basename);
}