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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user