Show injected Claude input as agent reports and notices, not operator bubbles

Claude Code records subagent/peer reports, task notifications, reminders and
skill bodies as user-role entries. Classify them server-side (history and live
stream) by origin/isMeta/queued_command mode: reports become kind agent_report,
task notifications kind notice, harness meta is hidden, and the mid-turn
wrapper is unwrapped to the operator's text. The app draws a collapsed Agent
report card and a one-line system notice on the agent's side.
This commit is contained in:
2026-11-02 16:44:07 +00:00
parent d78cbe3f66
commit 07d4740d0c
15 changed files with 1208 additions and 16 deletions
@@ -373,7 +373,8 @@ class ChatThreadController extends ChangeNotifier {
_running = next['running'] == true;
if (!_running) {
_liveAssistantMessageId = null;
} else if (const ['text', 'reasoning', 'tool'].contains(body['kind'])) {
} else if (const ['text', 'reasoning', 'tool'].contains(body['kind']) &&
chatInjectedKind({'kind': body['message_kind']}) == null) {
_liveAssistantMessageId = body['message_id'] as String?;
}
if (body['kind'] == 'step' &&
@@ -16,6 +16,7 @@ import '../ui/sessions/chat_adaptive.dart';
import '../ui/sessions/session_actions.dart';
import '../ui/transcript/chat_markdown_view.dart';
import '../ui/transcript/chat_message_view.dart';
import '../ui/transcript/chat_injected_rows.dart';
import 'chat_background_tasks.dart';
import 'chat_command_actions.dart';
import 'chat_composer_bar.dart';
@@ -1160,6 +1161,10 @@ class _ChatThreadViewState extends State<ChatThreadView> {
if (kind == 'activity') {
return _ChatActivityLine(row: row, onDismiss: onDismissCompaction);
}
if (kind == 'agent_report') {
return ChatAgentReportCard(row: row, textBuilder: chatMarkdownText);
}
if (kind == 'system_notice') return ChatSystemNoticeLine(row: row);
if (kind == 'permission' || kind == 'question') {
return ChatTranscriptCard(
row: row,
@@ -70,6 +70,11 @@ JsonMap refreshCachedChatHistory(
/// the latest record and that record's lifecycle state. Several updates of one
/// compaction are therefore one line, never one line per update.
///
/// A message the server marks with a `kind` is content the harness delivered,
/// not a turn: an `agent_report` becomes its own `agent_report` row and a
/// `notice` its own `system_notice` row, on the agent's side, never folded into
/// an answer and never drawn as the operator's bubble (see [chatInjectedKind]).
///
/// [trailing] rows are conversation cards (permission requests and questions),
/// not messages. They keep the server's order after the transcript, the way the
/// web renders approvals and forms at the end of the conversation.
@@ -124,6 +129,18 @@ List<JsonMap> transcriptRows(
}
for (final message in messages) {
final injected = chatInjectedKind(message);
if (injected != null) {
flush();
rows.add({
'kind': injected == chatKindAgentReport
? 'agent_report'
: 'system_notice',
'key': message['id'],
'message': message,
});
continue;
}
if (message['role'] == 'assistant') {
final id = '${message['id'] ?? ''}'.trim();
key ??= id;
@@ -149,10 +166,14 @@ List<JsonMap> transcriptRows(
// Prefer the assistant identity that actually streamed: a queued follow-up
// can be later in the transcript while this same reply is still arriving.
// Before a first delta, do not reopen an older reply behind a new user turn.
final answers = [
for (final message in messages)
if (chatInjectedKind(message) == null) message,
];
final streamingId =
liveMessageId ??
(messages.isNotEmpty && messages.last['role'] == 'assistant'
? '${messages.last['id'] ?? ''}'
(answers.isNotEmpty && answers.last['role'] == 'assistant'
? '${answers.last['id'] ?? ''}'
: null);
var hasStreamingRow = false;
if (running && streamingId != null) {
@@ -180,6 +201,19 @@ List<JsonMap> transcriptRows(
return rows;
}
/// `ChatMessage.Kind` of a report another agent delivered into the session.
const String chatKindAgentReport = 'agent_report';
/// `ChatMessage.Kind` of a one-line harness notice (a background task ended).
const String chatKindNotice = 'notice';
/// The injected-content kind the server recorded on [message], or null for an
/// ordinary turn. An unknown future kind keeps the message's own role.
String? chatInjectedKind(JsonMap message) {
final kind = '${message['kind'] ?? ''}';
return kind == chatKindAgentReport || kind == chatKindNotice ? kind : null;
}
/// Whether [part] is a recorded activity record rather than answer content.
///
/// Both spellings occur: `compaction` is the native lifecycle record and
@@ -307,6 +341,9 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) {
final incoming = frame['part'];
if (incoming is! Map || incoming['id'] == null) return thread;
final messageId = frame['message_id'];
// An agent report or notice the harness delivered: drawn on its own, and
// never evidence that the agent's turn is running.
final injected = chatInjectedKind({'kind': frame['message_kind']});
var index = messages.indexWhere(
(m) => m['id'] == messageId && m['role'] == 'assistant',
);
@@ -315,6 +352,7 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) {
'id': messageId,
'session_id': frame['session_id'],
'role': 'assistant',
'kind': ?injected,
'parts': <JsonMap>[],
});
index = messages.length - 1;
@@ -331,7 +369,12 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) {
final delta = frame['delta'];
if (delta is! String || delta.isEmpty) return thread;
if (partIndex < 0) {
parts.add({'type': kind, 'id': incoming['id'], 'text': delta});
parts.add({
'type': kind,
'id': incoming['id'],
'text': delta,
if (incoming['name'] is String) 'name': incoming['name'],
});
} else {
parts[partIndex] = {
...parts[partIndex],
@@ -354,6 +397,9 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) {
parts[partIndex] = merged;
}
messages[index] = {...message, 'parts': parts};
if (chatInjectedKind(message) != null) {
return {...thread, 'messages': messages};
}
return {...thread, 'messages': messages, 'running': true};
}
if (kind == 'step' && frame['step'] is Map) {
@@ -0,0 +1,244 @@
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(color: 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: 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)),
],
),
),
),
);
}
}