Chat: draw agent follow-ups and automatic continues apart from the operator
The server now names a user turn's origin (followup, auto_continue, auto_switch, scheduled) on chat messages, user frames and queued prompts, from the prompt's operation id while it holds the prompt and from its own text prefixes in history read later. Origin is a new field, so older clients keep treating these as ordinary user turns. The app draws an agent follow-up, auto-continue and account-switch prompt as a quiet outlined card on the agent's side with an icon, label and the schedule facts from the preamble; the operator's own scheduled message keeps its bubble with a small Scheduled mark. Find searches the drawn text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,7 @@ class ChatServerQueue {
|
||||
List<JsonMap> _prompts = const [];
|
||||
|
||||
/// The queued prompts, each `{operation_id, message_id, native_message_id,
|
||||
/// text, client, attachments}` as the server sent it.
|
||||
/// text, client, attachments, send_at, origin}` as the server sent it.
|
||||
List<JsonMap> get prompts => _prompts;
|
||||
|
||||
bool get isEmpty => _prompts.isEmpty;
|
||||
@@ -45,6 +45,7 @@ class ChatServerQueue {
|
||||
'operation_id': operation,
|
||||
'native_message_id': prompt['native_message_id'] ?? '',
|
||||
'client': prompt['client'],
|
||||
'origin': ?prompt['origin'],
|
||||
'parts': [
|
||||
{'type': 'text', 'text': '${prompt['text'] ?? ''}'},
|
||||
for (final attachment in prompt['attachments'] as List? ?? const [])
|
||||
|
||||
@@ -1919,6 +1919,7 @@ class _ChatThreadViewState extends State<ChatThreadView> with ChatFindHost {
|
||||
() => ChatMessageView(
|
||||
row: row,
|
||||
speechScope: widget.sessionId,
|
||||
scheduleShown: sendAt != null,
|
||||
// The same reader id as the reply's Read aloud button, so the spoken
|
||||
// word is highlighted in this reply's text.
|
||||
textBuilder: (context, text) => ChatMarkdownView(
|
||||
@@ -1983,6 +1984,7 @@ class _ChatThreadViewState extends State<ChatThreadView> with ChatFindHost {
|
||||
row: {'kind': 'user', 'key': message['id'], 'message': message},
|
||||
textBuilder: chatMarkdownText,
|
||||
images: _images,
|
||||
scheduleShown: true,
|
||||
footer: [
|
||||
ChatDeliveryIndicator(
|
||||
state: ChatDelivery.queued,
|
||||
@@ -2005,6 +2007,7 @@ class _ChatThreadViewState extends State<ChatThreadView> with ChatFindHost {
|
||||
},
|
||||
textBuilder: chatMarkdownText,
|
||||
images: _images,
|
||||
scheduleShown: true,
|
||||
footer: [
|
||||
ChatDeliveryIndicator(
|
||||
state: ChatDelivery.failed,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import 'chat_prompt_origin.dart';
|
||||
|
||||
/// One piece of transcript text find searches, as the transcript shows it:
|
||||
/// a user turn's text (its text parts joined by line breaks, as its bubble
|
||||
@@ -75,7 +76,10 @@ List<ChatFindUnit> chatFindUnits(List<JsonMap> rows, List<String> keys) {
|
||||
final kind = row['kind'];
|
||||
if (kind == 'user') {
|
||||
final message = row['message'] is Map ? row['message'] as Map : row;
|
||||
final text = _textParts(message).join('\n');
|
||||
final text = chatPromptDisplayText(
|
||||
message,
|
||||
_textParts(message).join('\n'),
|
||||
);
|
||||
if (text.trim().isEmpty) continue;
|
||||
final id = '${row['key'] ?? message['id'] ?? ''}';
|
||||
units.add(ChatFindUnit(index, keys[index], text, [id]));
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/// Who put a user turn into the chat when the operator did not type it just
|
||||
/// now. The server names it in `origin` (on a message, a user frame and a
|
||||
/// queued prompt); an older server's operation ids and the server's own text
|
||||
/// prefixes say the same.
|
||||
enum ChatPromptOrigin {
|
||||
/// The operator's own message, sent now.
|
||||
operator,
|
||||
|
||||
/// The operator's own message they scheduled to send later.
|
||||
scheduled,
|
||||
|
||||
/// A follow-up the agent scheduled for itself (schedule_followup).
|
||||
followup,
|
||||
|
||||
/// The server resending the chat after its usage limit reset.
|
||||
autoContinue,
|
||||
|
||||
/// The server continuing the chat on another Claude account.
|
||||
autoSwitch;
|
||||
|
||||
/// Whether the agent or the server, not the operator, sent it.
|
||||
bool get automated =>
|
||||
this == followup || this == autoContinue || this == autoSwitch;
|
||||
}
|
||||
|
||||
const _followupTextPrefix = '[Follow-up you scheduled at ';
|
||||
const _autoSwitchTextPrefix = '[Sent automatically by Pipelines]';
|
||||
|
||||
/// The origin of a user [message] (a transcript message or a queued prompt).
|
||||
ChatPromptOrigin chatPromptOrigin(Map<dynamic, dynamic> message) {
|
||||
switch ('${message['origin'] ?? ''}') {
|
||||
case 'followup':
|
||||
return ChatPromptOrigin.followup;
|
||||
case 'auto_continue':
|
||||
return ChatPromptOrigin.autoContinue;
|
||||
case 'auto_switch':
|
||||
return ChatPromptOrigin.autoSwitch;
|
||||
case 'scheduled':
|
||||
return ChatPromptOrigin.scheduled;
|
||||
}
|
||||
final operation = '${message['operation_id'] ?? ''}';
|
||||
if (operation.startsWith('followup-')) return ChatPromptOrigin.followup;
|
||||
if (operation.startsWith('auto-continue-')) {
|
||||
return ChatPromptOrigin.autoContinue;
|
||||
}
|
||||
if (operation.startsWith('auto-switch-')) return ChatPromptOrigin.autoSwitch;
|
||||
final text = _firstText(message).trimLeft();
|
||||
if (text.startsWith(_followupTextPrefix)) return ChatPromptOrigin.followup;
|
||||
if (text.startsWith(_autoSwitchTextPrefix)) {
|
||||
return ChatPromptOrigin.autoSwitch;
|
||||
}
|
||||
final sendAt = message['send_at'];
|
||||
if (sendAt is num && sendAt > 0) return ChatPromptOrigin.scheduled;
|
||||
return ChatPromptOrigin.operator;
|
||||
}
|
||||
|
||||
/// An automated prompt as the transcript shows it: the server's bracketed
|
||||
/// preamble becomes the header's facts and [body] is the message itself.
|
||||
class ChatAutomatedPrompt {
|
||||
const ChatAutomatedPrompt({
|
||||
required this.origin,
|
||||
required this.body,
|
||||
this.scheduledAt,
|
||||
this.condition,
|
||||
});
|
||||
|
||||
final ChatPromptOrigin origin;
|
||||
|
||||
/// The prompt's text less the server's preamble.
|
||||
final String body;
|
||||
|
||||
/// When the agent scheduled a follow-up, from its preamble.
|
||||
final DateTime? scheduledAt;
|
||||
|
||||
/// What a run-triggered follow-up waits for: "run 42 finishes".
|
||||
final String? condition;
|
||||
}
|
||||
|
||||
final RegExp _followupPreamble = RegExp(
|
||||
r'^\s*\[Follow-up you scheduled at (\S+) with schedule_followup'
|
||||
r'(?:, for when (run \d+ [^;\]]+?))?'
|
||||
r'(?:; delivered at \S+ at the latest)?\]\s*',
|
||||
);
|
||||
|
||||
/// [text] of a prompt from [origin], split into its preamble's facts and body.
|
||||
ChatAutomatedPrompt chatAutomatedPrompt(ChatPromptOrigin origin, String text) {
|
||||
if (origin == ChatPromptOrigin.followup) {
|
||||
final match = _followupPreamble.firstMatch(text);
|
||||
if (match != null) {
|
||||
return ChatAutomatedPrompt(
|
||||
origin: origin,
|
||||
body: text.substring(match.end),
|
||||
scheduledAt: DateTime.tryParse(match.group(1)!)?.toLocal(),
|
||||
condition: match.group(2),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (origin == ChatPromptOrigin.autoSwitch) {
|
||||
final trimmed = text.trimLeft();
|
||||
if (trimmed.startsWith(_autoSwitchTextPrefix)) {
|
||||
return ChatAutomatedPrompt(
|
||||
origin: origin,
|
||||
body: trimmed.substring(_autoSwitchTextPrefix.length).trimLeft(),
|
||||
);
|
||||
}
|
||||
}
|
||||
return ChatAutomatedPrompt(origin: origin, body: text);
|
||||
}
|
||||
|
||||
/// The text a user turn's row draws for its joined text parts [text]: an
|
||||
/// automated prompt's body without the server's preamble, else [text].
|
||||
/// Find searches this same string, so its matches line up with the drawing.
|
||||
String chatPromptDisplayText(Map<dynamic, dynamic> message, String text) {
|
||||
final origin = chatPromptOrigin(message);
|
||||
if (!origin.automated) return text;
|
||||
return chatAutomatedPrompt(origin, text).body;
|
||||
}
|
||||
|
||||
String _firstText(Map<dynamic, dynamic> message) {
|
||||
final parts = message['parts'];
|
||||
if (parts is! List) return '';
|
||||
for (final part in parts) {
|
||||
if (part is Map &&
|
||||
(part['type'] == null || part['type'] == 'text') &&
|
||||
part['text'] is String) {
|
||||
return part['text'] as String;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -212,6 +212,7 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame, {int Function()? now}) {
|
||||
'created_at': arrived(),
|
||||
'client': frame['client'],
|
||||
'operation_id': frame['operation_id'],
|
||||
'origin': ?frame['origin'],
|
||||
'parts': [
|
||||
{'type': 'text', 'text': text},
|
||||
...(frame['attachments'] is List
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../conversation/chat_schedule_time.dart';
|
||||
import '../../domain/chat_prompt_origin.dart';
|
||||
import 'chat_copy_button.dart';
|
||||
import 'chat_hover_actions.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 card = Material(
|
||||
key: const Key('chat-automated-prompt'),
|
||||
color: scheme.surfaceContainerLow,
|
||||
clipBehavior: Clip.antiAlias,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
side: BorderSide(color: scheme.outlineVariant),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(14, 8, 14, 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()));
|
||||
return ChatHoverRegion(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
card,
|
||||
if (footer.isNotEmpty || timestamp != null || copy != null)
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.only(start: 14, top: 2),
|
||||
child: Wrap(
|
||||
key: const Key('chat-message-footer'),
|
||||
spacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [...footer, ?timestamp, ?copy],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import 'package:flutter/material.dart';
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../data/chat_attachment_images.dart';
|
||||
import 'chat_attachment_gallery.dart';
|
||||
import 'chat_automated_prompt.dart';
|
||||
import '../../domain/chat_prompt_origin.dart';
|
||||
import '../../domain/chat_tool_calls.dart';
|
||||
import '../../domain/chat_transcript.dart';
|
||||
import 'chat_copy_button.dart';
|
||||
@@ -31,8 +33,14 @@ class ChatMessageView extends StatelessWidget {
|
||||
this.footer = const [],
|
||||
this.images,
|
||||
this.speechScope = 'chat',
|
||||
this.scheduleShown = false,
|
||||
});
|
||||
|
||||
/// Whether [footer] already says the message is scheduled (its delivery
|
||||
/// line reads "Scheduled · sends at …"), so the operator's own scheduled
|
||||
/// message needs no second mark.
|
||||
final bool scheduleShown;
|
||||
|
||||
final JsonMap row;
|
||||
|
||||
/// Names the conversation in the reader's message ids, so leaving it can
|
||||
@@ -53,7 +61,17 @@ class ChatMessageView extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final user = row['kind'] == 'user';
|
||||
final role = user ? 'User' : 'Assistant';
|
||||
final origin = user
|
||||
? chatPromptOrigin(_userMessage)
|
||||
: ChatPromptOrigin.operator;
|
||||
// A prompt the agent or the server sent reads as neither side's turn: a
|
||||
// quiet card on the agent's side, headed by what sent it.
|
||||
final automated = origin.automated;
|
||||
final role = automated
|
||||
? chatAutomatedPromptLabel(origin)
|
||||
: 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.
|
||||
@@ -78,7 +96,7 @@ class ChatMessageView extends StatelessWidget {
|
||||
padding: const EdgeInsets.symmetric(vertical: 6),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) => Align(
|
||||
alignment: user
|
||||
alignment: user && !automated
|
||||
? AlignmentDirectional.centerEnd
|
||||
: AlignmentDirectional.centerStart,
|
||||
child: ConstrainedBox(
|
||||
@@ -90,8 +108,10 @@ class ChatMessageView extends StatelessWidget {
|
||||
? (constraints.maxWidth * 0.85).clamp(0, 560).toDouble()
|
||||
: 720 + gutter,
|
||||
),
|
||||
child: user
|
||||
? _userTurn(content, body)
|
||||
child: automated
|
||||
? _automatedTurn(origin, content)
|
||||
: user
|
||||
? _userTurn(content, body, origin)
|
||||
: _agentRow(
|
||||
context,
|
||||
content,
|
||||
@@ -115,7 +135,7 @@ class ChatMessageView extends StatelessWidget {
|
||||
/// 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) {
|
||||
Widget _userTurn(List<Widget> content, Widget body, ChatPromptOrigin origin) {
|
||||
final thumbnails = [
|
||||
for (final part in _partsOf(_userMessage))
|
||||
if (_isFile(part) && chatPartIsThumbnail(part)) part,
|
||||
@@ -124,7 +144,8 @@ class ChatMessageView extends StatelessWidget {
|
||||
createdAt: chatMessageCreatedAt(_userMessage),
|
||||
);
|
||||
final hasTimestamp = chatMessageCreatedAt(_userMessage) != null;
|
||||
if (thumbnails.isEmpty && footer.isEmpty && !hasTimestamp) {
|
||||
final marked = origin == ChatPromptOrigin.scheduled && !scheduleShown;
|
||||
if (thumbnails.isEmpty && footer.isEmpty && !hasTimestamp && !marked) {
|
||||
return ChatUserBubble(child: body);
|
||||
}
|
||||
return Column(
|
||||
@@ -141,27 +162,53 @@ class ChatMessageView extends StatelessWidget {
|
||||
if (content.isNotEmpty || thumbnails.isEmpty)
|
||||
ChatUserBubble(child: body),
|
||||
// The delivery line and the time share one line under the bubble.
|
||||
if (footer.isNotEmpty || hasTimestamp) ...[
|
||||
if (footer.isNotEmpty || hasTimestamp || marked) ...[
|
||||
const SizedBox(height: 2),
|
||||
Wrap(
|
||||
key: const Key('chat-message-footer'),
|
||||
spacing: 8,
|
||||
alignment: WrapAlignment.end,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [...footer, if (hasTimestamp) timestamp],
|
||||
children: [
|
||||
...footer,
|
||||
if (marked) const ChatScheduledMark(),
|
||||
if (hasTimestamp) timestamp,
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// A prompt the agent or the server sent: its card, with the delivery line
|
||||
/// and time beneath.
|
||||
Widget _automatedTurn(ChatPromptOrigin origin, List<Widget> content) {
|
||||
final created = chatMessageCreatedAt(_userMessage);
|
||||
return ChatAutomatedPromptCard(
|
||||
prompt: chatAutomatedPrompt(origin, _userText(_userMessage)),
|
||||
body: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: content,
|
||||
),
|
||||
footer: footer,
|
||||
timestamp: created == null
|
||||
? null
|
||||
: ChatMessageTimestamp(createdAt: created),
|
||||
);
|
||||
}
|
||||
|
||||
/// The user turn's text parts joined as its row draws them.
|
||||
static String _userText(JsonMap message) => _partsOf(message)
|
||||
.where((part) => part['type'] == null || part['type'] == 'text')
|
||||
.map((part) => '${part['text'] ?? ''}')
|
||||
.join('\n');
|
||||
|
||||
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 automated = chatPromptOrigin(message).automated;
|
||||
final text = chatPromptDisplayText(message, _userText(message));
|
||||
final widgets = <Widget>[
|
||||
if (text.trim().isNotEmpty) ChatLinkText(text),
|
||||
for (final part in parts.where(_isFile))
|
||||
@@ -173,7 +220,7 @@ class ChatMessageView extends StatelessWidget {
|
||||
_unrecognised(part),
|
||||
];
|
||||
final client = '${message['client'] ?? ''}'.trim();
|
||||
if (client.isNotEmpty) {
|
||||
if (client.isNotEmpty && !automated) {
|
||||
widgets.insert(0, Text(client, style: _clientStyle(context)));
|
||||
}
|
||||
return widgets;
|
||||
|
||||
Reference in New Issue
Block a user