Files
pipelines/app/lib/features/chat/ui/transcript/chat_injected_rows.dart
T

256 lines
8.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../sessions/chat_adaptive.dart';
import 'chat_message_bubble.dart';
import 'chat_message_view.dart';
/// Content the harness delivered into the session, drawn on the agent's side:
/// an `agent_report` row (a delegated agent's report) and a `system_notice`
/// row (a one-line harness notice such as a background task finishing). Neither
/// is the operator's turn, so neither uses the operator's bubble.
/// The text and sender a row's message recorded.
({String text, String sender}) _content(JsonMap row) {
final message = row['message'];
final parts = message is Map ? message['parts'] : null;
final buffer = StringBuffer();
var sender = '';
if (parts is List) {
for (final part in parts) {
if (part is! Map || part['type'] != 'text') continue;
final text = '${part['text'] ?? ''}';
if (text.trim().isEmpty) continue;
if (buffer.isNotEmpty) buffer.write('\n\n');
buffer.write(text);
final name = '${part['name'] ?? ''}'.trim();
if (sender.isEmpty && name.isNotEmpty) sender = name;
}
}
return (text: buffer.toString().trim(), sender: sender);
}
/// The report's first line, with markdown markers removed, as its summary.
String chatAgentReportSummary(String text) {
for (final raw in text.split('\n')) {
final line = raw
.trim()
.replaceFirst(RegExp(r'^(#{1,6}\s+|[-*+]\s+|>\s*)'), '')
.replaceAll('**', '')
.trim();
if (line.isNotEmpty) return line;
}
return '';
}
/// A delegated agent's report: collapsed to its first line, expanded to the
/// whole report rendered as markdown.
class ChatAgentReportCard extends StatefulWidget {
const ChatAgentReportCard({super.key, required this.row, this.textBuilder});
final JsonMap row;
final ChatMessageTextBuilder? textBuilder;
@override
State<ChatAgentReportCard> createState() => _ChatAgentReportCardState();
}
class _ChatAgentReportCardState extends State<ChatAgentReportCard> {
bool _expanded = false;
void _toggle() => setState(() => _expanded = !_expanded);
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final content = _content(widget.row);
final title = content.sender.isEmpty
? 'Agent report'
: 'Agent report · ${content.sender}';
final summary = chatAgentReportSummary(content.text);
final secondary = cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
final header = Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 12, top: 2),
child: Icon(
cupertino ? CupertinoIcons.person_crop_rectangle : Icons.assignment,
color: secondary,
),
),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: cupertino
? CupertinoTheme.of(context).textTheme.textStyle.copyWith(
fontWeight: FontWeight.w600,
)
: Theme.of(context).textTheme.titleSmall,
),
if (summary.isNotEmpty)
Text(
summary,
maxLines: _expanded ? null : 1,
overflow: _expanded ? null : TextOverflow.ellipsis,
style:
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodyMedium)
?.copyWith(
// This summary is readable content, not disabled
// chrome; translucent secondaryLabel falls below
// normal-text contrast on a light card.
color: cupertino
? CupertinoColors.label.resolveFrom(context)
: secondary,
),
),
],
),
),
Icon(
cupertino
? (_expanded
? CupertinoIcons.chevron_up
: CupertinoIcons.chevron_down)
: (_expanded ? Icons.expand_less : Icons.expand_more),
color: secondary,
),
],
);
final toggle = Semantics(
container: true,
button: true,
expanded: _expanded,
label: title,
value: summary,
onTap: _toggle,
child: ExcludeSemantics(
child: cupertino
? CupertinoButton(
padding: const EdgeInsets.all(12),
onPressed: _toggle,
child: DefaultTextStyle.merge(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: header,
),
)
: InkWell(
onTap: _toggle,
child: Padding(
padding: const EdgeInsets.all(12),
child: header,
),
),
),
);
final body = _expanded
? Padding(
padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 12),
child: DefaultTextStyle.merge(
style: chatMessageTextStyle(context, cupertino),
child:
widget.textBuilder?.call(context, content.text) ??
SelectableText(content.text),
),
)
: null;
final column = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [toggle, ?body],
);
return Semantics(
container: true,
explicitChildNodes: true,
label: 'Agent report',
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Align(
alignment: AlignmentDirectional.centerStart,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 720),
child: cupertino
? DecoratedBox(
decoration: BoxDecoration(
color: CupertinoColors.secondarySystemGroupedBackground
.resolveFrom(context),
border: Border.all(
color: CupertinoColors.separator.resolveFrom(context),
),
borderRadius: const BorderRadius.all(Radius.circular(12)),
),
child: column,
)
: Card.outlined(
margin: EdgeInsets.zero,
clipBehavior: Clip.antiAlias,
child: column,
),
),
),
),
);
}
}
/// A one-line harness notice in the agent's column, in the secondary style.
class ChatSystemNoticeLine extends StatelessWidget {
const ChatSystemNoticeLine({super.key, required this.row});
final JsonMap row;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final text = _content(row).text;
if (text.isEmpty) return const SizedBox.shrink();
final color = cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
final style =
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodySmall)
?.copyWith(
color: cupertino
? CupertinoColors.label.resolveFrom(context)
: color,
);
return Semantics(
container: true,
label: 'System notice',
value: text,
child: ExcludeSemantics(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 8),
child: Icon(
cupertino ? CupertinoIcons.info_circle : Icons.info_outline,
size: style?.fontSize == null ? 16 : style!.fontSize! * 1.2,
color: color,
),
),
Expanded(child: Text(text, style: style)),
],
),
),
),
);
}
}