import 'package:flutter/material.dart'; import '../../../../core/api_gateway.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 createState() => _ChatAgentReportCardState(); } class _ChatAgentReportCardState extends State { bool _expanded = false; void _toggle() => setState(() => _expanded = !_expanded); @override Widget build(BuildContext context) { final content = _content(widget.row); final title = content.sender.isEmpty ? 'Agent report' : 'Agent report ยท ${content.sender}'; final summary = chatAgentReportSummary(content.text); final secondary = Theme.of(context).colorScheme.onSurfaceVariant; final header = Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsetsDirectional.only(end: 12, top: 2), child: Icon(Icons.assignment, color: secondary), ), Expanded( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title, style: Theme.of(context).textTheme.titleSmall), if (summary.isNotEmpty) Text( summary, maxLines: _expanded ? null : 1, overflow: _expanded ? null : TextOverflow.ellipsis, style: Theme.of( context, ).textTheme.bodyMedium?.copyWith(color: secondary), ), ], ), ), Icon( _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: 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), 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: 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 text = _content(row).text; if (text.isEmpty) return const SizedBox.shrink(); final color = Theme.of(context).colorScheme.onSurfaceVariant; final style = Theme.of(context).textTheme.bodySmall?.copyWith(color: 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( Icons.info_outline, size: style?.fontSize == null ? 16 : style!.fontSize! * 1.2, color: color, ), ), Expanded(child: Text(text, style: style)), ], ), ), ), ); } }