Files
pipelines/app/lib/features/chat/ui/transcript/chat_automated_prompt.dart
T
2026-12-03 20:01:13 +00:00

263 lines
8.6 KiB
Dart

import 'package:flutter/material.dart';
import '../../../../ui/theme/app_design_tokens.dart';
import '../../conversation/chat_schedule_time.dart';
import '../../domain/chat_prompt_origin.dart';
import 'chat_copy_button.dart';
import 'chat_hover_actions.dart';
import 'paper/paper_editorial_note.dart';
import 'paper/paper_quote.dart';
import 'terminal/terminal_glyphs.dart';
/// The header words of an automated prompt.
String chatAutomatedPromptLabel(ChatPromptOrigin origin) => switch (origin) {
ChatPromptOrigin.followup => 'Follow-up from agent',
ChatPromptOrigin.autoContinue => 'Auto-continue after limit reset',
ChatPromptOrigin.autoSwitch => 'Continued on another account',
_ => '',
};
IconData _icon(ChatPromptOrigin origin) => switch (origin) {
ChatPromptOrigin.followup => Icons.update,
ChatPromptOrigin.autoContinue => Icons.autorenew,
_ => Icons.swap_horiz,
};
/// A prompt the agent or the server sent on the operator's behalf. It is not
/// the operator's filled bubble: a quiet outlined card on the agent's side,
/// headed by what sent it (and, for a follow-up, when the agent scheduled it
/// or what run it waited for), with the prompt's [body] beneath. [footer]
/// (its delivery line) and the time sit under the card with its Copy.
class ChatAutomatedPromptCard extends StatelessWidget {
const ChatAutomatedPromptCard({
super.key,
required this.prompt,
required this.body,
this.footer = const [],
this.timestamp,
});
final ChatAutomatedPrompt prompt;
final Widget body;
final List<Widget> footer;
final Widget? timestamp;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
final label = chatAutomatedPromptLabel(prompt.origin);
final detail = switch (prompt) {
ChatAutomatedPrompt(:final condition?) => 'when $condition',
ChatAutomatedPrompt(:final scheduledAt?) =>
'scheduled ${chatScheduleTime(context, scheduledAt)}',
_ => null,
};
final muted = theme.textTheme.labelMedium?.copyWith(
color: scheme.onSurfaceVariant,
);
final header = SelectionContainer.disabled(
child: Wrap(
key: const Key('chat-automated-prompt-header'),
spacing: 6,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Icon(_icon(prompt.origin), size: 16, color: scheme.tertiary),
Text(label, style: muted?.copyWith(fontWeight: FontWeight.w600)),
if (detail != null) Text('· $detail', style: muted),
],
),
);
final tokens = AppDesignTokens.of(context);
final density = tokens.density;
final transcript = tokens.transcript;
final card = transcript == TranscriptStyle.manuscript
? PaperEditorialNote(
label: label,
detail: detail,
body: body,
hasBody: prompt.body.trim().isNotEmpty,
)
: transcript == TranscriptStyle.prompt
? _terminalLine(context, label, detail)
: Material(
key: const Key('chat-automated-prompt'),
color: scheme.surfaceContainerLow,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: tokens.corners(
const BorderRadius.all(Radius.circular(16)),
),
side: BorderSide(color: scheme.outlineVariant),
),
child: Padding(
padding: EdgeInsets.fromLTRB(
density.inset(14),
density.space(8),
density.inset(14),
density.space(10),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
header,
if (prompt.body.trim().isNotEmpty) ...[
const SizedBox(height: 4),
DefaultTextStyle.merge(
style: theme.textTheme.bodyMedium,
child: body,
),
],
],
),
),
);
final copy = prompt.body.trim().isEmpty
? null
: ChatHoverReveal(child: ChatCopyButton(text: prompt.body.trim()));
final footerInset = switch (transcript) {
TranscriptStyle.prompt => 8 + terminalGlyphColumn(context),
TranscriptStyle.manuscript => paperQuoteTextInset,
TranscriptStyle.bubbles => 14.0,
};
return ChatHoverRegion(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
card,
if (footer.isNotEmpty || timestamp != null || copy != null)
Padding(
padding: EdgeInsetsDirectional.only(start: footerInset, top: 2),
child: Wrap(
key: const Key('chat-message-footer'),
spacing: 8,
crossAxisAlignment: WrapCrossAlignment.center,
children: [...footer, ?timestamp, ?copy],
),
),
],
),
);
}
}
extension on ChatAutomatedPromptCard {
/// Under a terminal design the prompt is not a card but dim bracketed
/// lines, as a CLI prints a message it injected: `[Follow-up from agent ·
/// scheduled …]`, then the prompt behind a dim rule.
Widget _terminalLine(BuildContext context, String label, String? detail) {
final theme = Theme.of(context);
final dim = theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
);
final header = SelectionContainer.disabled(
child: Text.rich(
key: const Key('chat-automated-prompt-header'),
TextSpan(
style: dim,
children: [
const TextSpan(text: '['),
TextSpan(
text: label,
style: const TextStyle(fontWeight: FontWeight.w700),
),
if (detail != null) TextSpan(text: ' · $detail'),
const TextSpan(text: ']'),
],
),
),
);
// Inset like the answer's text, so the bracket starts in the same
// column as the agent's dots.
return Material(
key: const Key('chat-automated-prompt'),
type: MaterialType.transparency,
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
header,
if (prompt.body.trim().isNotEmpty)
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
width: terminalGlyphColumn(context),
child: CustomPaint(
painter: _RulePainter(theme.colorScheme.outline),
),
),
Expanded(
child: DefaultTextStyle.merge(style: dim, child: body),
),
],
),
),
],
),
),
);
}
}
/// The dim vertical rule beside an injected prompt's text.
class _RulePainter extends CustomPainter {
const _RulePainter(this.color);
final Color color;
@override
void paint(Canvas canvas, Size size) {
final x = size.width * 0.25;
canvas.drawLine(
Offset(x, 2),
Offset(x, size.height - 2),
Paint()
..color = color
..strokeWidth = 1,
);
}
@override
bool shouldRepaint(_RulePainter old) => old.color != color;
}
/// The small "Scheduled" mark under the operator's own message they had
/// scheduled to send later, once it is no longer waiting.
class ChatScheduledMark extends StatelessWidget {
const ChatScheduledMark({super.key});
@override
Widget build(BuildContext context) {
final style = Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
);
return SelectionContainer.disabled(
child: Semantics(
container: true,
label: 'Scheduled message',
excludeSemantics: true,
child: Row(
key: const Key('chat-scheduled-mark'),
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.schedule_send,
size: (style?.fontSize ?? 12) + 2,
color: style?.color,
),
const SizedBox(width: 4),
Text('Scheduled', style: style),
],
),
),
);
}
}