Files
pipelines/app/lib/features/chat/ui/transcript/chat_message_view.dart
T
alan a3a59adb30 Quieter transcript metadata: day dividers, time-only rows, muted meta rows
- Date once per calendar day: a muted centred divider above each day's
  first timed row (chatDayDividers); rows show only their time ("10:16"),
  with the full date and time kept in the tooltip and semantics. The user's
  time shares the delivery line; a harness notice's time trails its line.
- Activity line and turn chips are secondary: bodyMedium, regular weight,
  onSurfaceVariant (error colour kept for failures), a small chevron_right
  that turns down while its details are open (replaces expand_less).
- Copy / Read aloud: 18 px muted icons; on pointer devices shown only while
  the reply is hovered or an action is focused (also without the gutter);
  touch keeps them visible with a full tap target. The time leads the
  footer so hidden actions leave no gap.
- Settled questions: the question on one line, the operator's answer in
  full on its own line beneath it.
- Meta rows (activity, chips, notices, compaction line) start at the reply
  text's leading edge; turn chips sit in their own column under the answer.
- Scroll fixture: ?timestamps=1 spreads messages over two days.

Row keys are unchanged; the divider joins a row without replacing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:10:51 +00:00

458 lines
16 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../data/chat_attachment_images.dart';
import 'chat_attachment_gallery.dart';
import '../../domain/chat_tool_calls.dart';
import '../../domain/chat_transcript.dart';
import 'chat_copy_button.dart';
import 'chat_copy_gutter.dart';
import 'chat_hover_actions.dart';
import 'chat_link_text.dart';
import 'chat_message_bubble.dart';
import 'chat_turn_activity_view.dart';
import 'native_disclosure.dart';
import 'chat_message_timestamp.dart';
import 'chat_read_aloud_button.dart';
import '../../speech/speech_controller.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 for agent turns; native disclosures retain unknown parts as raw JSON.
class ChatMessageView extends StatelessWidget {
const ChatMessageView({
super.key,
required this.row,
this.textBuilder,
this.footer = const [],
this.images,
this.speechScope = 'chat',
});
final JsonMap row;
/// Names the conversation in the reader's message ids, so leaving it can
/// stop only its own reply (see [speechMessageId]).
final String speechScope;
final ChatMessageTextBuilder? textBuilder;
/// Loads attached images for the operator's turn; without one, only images
/// the history carries inline can draw.
final ChatAttachmentImages? images;
static final ChatAttachmentImages _inlineImages = ChatAttachmentImages(null);
/// Beneath the turn: an agent turn's settled permissions, or the delivery
/// line under the operator's own message.
final List<Widget> footer;
@override
Widget build(BuildContext context) {
final user = row['kind'] == 'user';
final role = user ? 'User' : 'Assistant';
final content = user ? _userParts(context) : _agentParts(context);
// A wide window draws an answer's Copy in the gutter left of the reading
// column; without room it trails the time in the answer's footer.
final gutter = user ? 0.0 : ChatCopyGutter.of(context);
// No visible role label: side and surface say who spoke and the row's
// semantics label names the role. The operator's turn is a neutral bubble
// that shrinks to its content; the agent's answer sits on the page with no
// bubble, so long replies read as a document rather than a balloon.
final body = DefaultTextStyle.merge(
style: chatMessageTextStyle(context),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: content,
),
);
return Semantics(
container: true,
explicitChildNodes: true,
label: '$role message',
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: LayoutBuilder(
builder: (context, constraints) => Align(
alignment: user
? AlignmentDirectional.centerEnd
: AlignmentDirectional.centerStart,
child: ConstrainedBox(
// The operator's turn leaves a margin on a phone, so the two
// sides read as a conversation; the answer keeps a readable
// measure on a wide pane.
constraints: BoxConstraints(
maxWidth: user
? (constraints.maxWidth * 0.85).clamp(0, 560).toDouble()
: 720 + gutter,
),
child: user
? _userTurn(content, body)
: _agentRow(
context,
content,
ChatAgentSurface(child: body),
gutter,
),
),
),
),
),
);
}
Color _secondaryColor(BuildContext context) =>
Theme.of(context).colorScheme.onSurfaceVariant;
JsonMap get _userMessage => row['message'] is Map
? Map<String, dynamic>.from(row['message'] as Map)
: row;
/// Attached images sit above the operator's bubble, as their own thumbnails;
/// an images-only turn draws no empty bubble. The footer (the delivery line)
/// sits under the bubble's trailing corner.
Widget _userTurn(List<Widget> content, Widget body) {
final thumbnails = [
for (final part in _partsOf(_userMessage))
if (_isFile(part) && chatPartIsThumbnail(part)) part,
];
final timestamp = ChatMessageTimestamp(
createdAt: chatMessageCreatedAt(_userMessage),
);
final hasTimestamp = chatMessageCreatedAt(_userMessage) != null;
if (thumbnails.isEmpty && footer.isEmpty && !hasTimestamp) {
return ChatUserBubble(child: body);
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (thumbnails.isNotEmpty)
ChatAttachmentGallery(
parts: thumbnails,
images: images ?? _inlineImages,
),
if (thumbnails.isNotEmpty && content.isNotEmpty)
const SizedBox(height: 6),
if (content.isNotEmpty || thumbnails.isEmpty)
ChatUserBubble(child: body),
// The delivery line and the time share one line under the bubble.
if (footer.isNotEmpty || hasTimestamp) ...[
const SizedBox(height: 2),
Wrap(
key: const Key('chat-message-footer'),
spacing: 8,
alignment: WrapAlignment.end,
crossAxisAlignment: WrapCrossAlignment.center,
children: [...footer, if (hasTimestamp) timestamp],
),
],
],
);
}
List<Widget> _userParts(BuildContext context) {
final message = _userMessage;
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) ChatLinkText(text),
for (final part in parts.where(_isFile))
if (!chatPartIsThumbnail(part)) _attachment(context, part),
for (final part in parts)
if (part['type'] != 'text' &&
!_isFile(part) &&
!(part['type'] == null && part['text'] is String))
_unrecognised(part),
];
final client = '${message['client'] ?? ''}'.trim();
if (client.isNotEmpty) {
widgets.insert(0, Text(client, style: _clientStyle(context)));
}
return widgets;
}
/// The answer, with its Copy button in the [gutter] to its left when there
/// is one, or else after the time in its footer.
Widget _agentRow(
BuildContext context,
List<Widget> content,
Widget answer,
double gutter,
) {
final answerParts = row['streaming'] == true
? const <String>[]
: _answerParts(_partsOf(row));
final copyText = answerParts.join('\n\n');
final id = speechMessageId(speechScope, '${row['key'] ?? row['id'] ?? ''}');
Widget? actions(Axis direction) => copyText.isEmpty
? null
: Flex(
direction: direction,
mainAxisSize: MainAxisSize.min,
verticalDirection: VerticalDirection.up,
children: [
ChatCopyButton(text: copyText),
ChatReadAloudButton(
messageId: id,
parts: answerParts,
direction: direction,
),
],
);
if (gutter <= 0) {
// Without a gutter the actions trail the footer line; a pointer device
// shows them while the answer is hovered or one has focus.
final row = actions(Axis.horizontal);
final reveal = row == null
? null
: ListenableBuilder(
listenable: speechController,
child: row,
builder: (context, child) => ChatHoverReveal(
visibilityKey: const Key('chat-message-actions-visibility'),
pinned: speechController.messageId == id,
child: child!,
),
);
return ChatHoverRegion(
child: _agentTurn(context, content, answer, reveal),
);
}
// The gutter is one button wide: Read aloud stacks above Copy, and stays
// shown on a desktop while this reply reads.
final turn = _agentTurn(context, content, answer, null);
return ListenableBuilder(
listenable: speechController,
child: turn,
builder: (context, child) => ChatCopyGutterRow(
width: gutter,
copy: actions(Axis.vertical),
pinned: speechController.messageId == id,
child: child!,
),
);
}
/// Stable activity precedes the answer; permission summaries and recorded
/// model remain beneath it. Pending interaction cards are rendered separately.
Widget _agentTurn(
BuildContext context,
List<Widget> content,
Widget answer,
Widget? copy,
) {
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,
];
final model = '${row['model'] ?? ''}'.trim();
// The metadata line: the model that answered and the time, muted; the
// actions (when not in the gutter) trail it.
final meta = <Widget>[
if (model.isNotEmpty)
Text(
model,
key: const Key('chat-reply-model'),
style: chatMetaTextStyle(context),
),
if (chatMessageCreatedAt(row) case final time?)
ChatMessageTimestamp(createdAt: time),
];
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (row['streaming'] == true ||
reasoning.isNotEmpty ||
tools.isNotEmpty ||
(row['activityRecords'] as List? ?? const []).isNotEmpty)
ChatTurnActivityView(
key: const Key('chat-stable-turn-activity'),
parts: [
...parts,
...List<JsonMap>.from(
row['activityRecords'] as List? ?? const [],
),
],
streaming: row['streaming'] == true,
),
// A little air between the one-line turn status and the answer.
if (content.isNotEmpty) const SizedBox(height: 6),
if (content.isNotEmpty)
Padding(
padding: const EdgeInsets.symmetric(
horizontal: chatAgentContentInset,
),
child: answer,
),
// Settled permissions, questions and changes: meta rows that, like
// the activity line, sit at the row edge with their content inset to
// the answer's text.
if (footer.isNotEmpty)
Column(
key: const Key('chat-turn-chips'),
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: footer,
),
if (copy != null || meta.isNotEmpty)
Padding(
// The time starts at the answer's text edge and the actions
// trail it, so hidden actions leave no gap before it. Without
// metadata the buttons' own inset aligns their icons instead.
padding: EdgeInsetsDirectional.only(
start: meta.isEmpty ? 0 : chatAgentContentInset,
end: chatAgentContentInset,
),
child: Wrap(
key: const Key('chat-message-footer'),
spacing: 8,
crossAxisAlignment: WrapCrossAlignment.center,
children: [...meta, ?copy],
),
),
],
);
}
/// The answer as the operator reads it: the turn's text parts in order,
/// without reasoning, tools or attachments.
List<String> _answerParts(List<JsonMap> parts) => [
for (final part in parts)
if ((part['type'] == null || part['type'] == 'text') &&
part['text'] is String &&
(part['text'] as String).trim().isNotEmpty)
(part['text'] as String).trim(),
];
List<Widget> _agentParts(BuildContext context) {
final widgets = <Widget>[];
final parts = _partsOf(row);
final seen = <String, int>{};
// Long replies arrive as several text parts (and folded messages); an
// empty line between consecutive ones keeps them reading as paragraphs.
final gap = chatParagraphGap(
context,
chatMessageTextStyle(context)?.fontSize,
);
var previousText = false;
for (var index = 0; index < parts.length; index++) {
final part = parts[index];
var widget = _agentPart(context, part);
if (widget == null) continue;
final text = _isTextPart(part);
if (text && previousText) {
widget = Padding(
padding: EdgeInsets.only(top: gap),
child: widget,
);
}
previousText = text;
// Part identity, not position, so an older assistant message that folds
// into the same turn can prepend its parts without moving an open
// disclosure. The enclosing row's identity is carried by ChatRowKeys; a
// part with no server id still falls back to its position.
widgets.add(
KeyedSubtree(key: _partKey(part, index, seen), child: widget),
);
}
return widgets;
}
/// The server's part id when there is one, else this part's position. A
/// repeated identity gets an ordinal so two sibling keys never collide.
Key _partKey(JsonMap part, int index, Map<String, int> seen) {
final type = '${part['type'] ?? ''}';
final id = '${part['id'] ?? ''}'.trim();
final base = id.isEmpty ? 'index-$index' : '$type:$id';
final ordinal = (seen[base] ?? 0) + 1;
seen[base] = ordinal;
return ValueKey<String>('chat-part-$base-$ordinal');
}
/// 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);
}
// Reasoning and tools are retained in the turn's activity disclosure.
if (type == 'reasoning' || type == 'tool') return null;
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) {
return Theme.of(
context,
).textTheme.bodySmall?.copyWith(color: _secondaryColor(context));
}
Widget _attachment(BuildContext context, JsonMap part) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.only(right: 8),
child: Icon(Icons.attach_file),
),
Expanded(child: SelectableText(chatAttachmentPartName(part))),
],
);
}
/// 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 _isTextPart(JsonMap part) =>
(part['type'] == null || part['type'] == 'text') &&
part['text'] is String &&
(part['text'] as String).trim().isNotEmpty;
bool _isFile(JsonMap part) =>
part['type'] == 'file' &&
'${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty;