1a9db0206c
Two or more consecutive Claude Code task notices fold into one task_notices row: a chip 'N background tasks' (tinted when one failed) that opens a sheet with one compact row per task (outcome icon, summary, outcome word), like the tools and permissions chips. A lone notice stays one line. The server also treats Claude Code's [SYSTEM NOTIFICATION] banner as harness text, so a bannered notification without an origin is still a notice, never the operator's turn. The chat fixture gains ?tasks=N.
109 lines
3.5 KiB
Dart
109 lines
3.5 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
|
|
/// Background-task notices: the one-line `notice` messages the server records
|
|
/// when a harness reports that a background task ended (Claude Code's task
|
|
/// notifications). The server writes them as
|
|
/// `Background task <finished|failed|stopped>[: <summary>]`.
|
|
|
|
/// How a background task ended, as its notice reports it.
|
|
enum ChatTaskOutcome { finished, failed, stopped }
|
|
|
|
/// One background task's notice: its outcome and the harness's summary.
|
|
class ChatTaskNotice {
|
|
const ChatTaskNotice({
|
|
required this.id,
|
|
required this.outcome,
|
|
required this.summary,
|
|
});
|
|
|
|
final String id;
|
|
final ChatTaskOutcome outcome;
|
|
|
|
/// The harness's summary (may be blank when it recorded none).
|
|
final String summary;
|
|
}
|
|
|
|
final _taskNoticeLine = RegExp(
|
|
r'^Background task (finished|failed|stopped)(?::\s*([\s\S]*))?$',
|
|
);
|
|
|
|
/// The notice text of [message], its text parts joined.
|
|
String _noticeText(JsonMap message) {
|
|
final parts = message['parts'];
|
|
if (parts is! List) return '';
|
|
return [
|
|
for (final part in parts)
|
|
if (part is Map && part['type'] == 'text') '${part['text'] ?? ''}'.trim(),
|
|
].where((text) => text.isNotEmpty).join(' ');
|
|
}
|
|
|
|
/// The background task [message] reports, or null for any other message
|
|
/// (another notice, an ordinary turn, a report).
|
|
ChatTaskNotice? chatTaskNotice(JsonMap message) {
|
|
if (message['kind'] != 'notice') return null;
|
|
final match = _taskNoticeLine.firstMatch(_noticeText(message));
|
|
if (match == null) return null;
|
|
return ChatTaskNotice(
|
|
id: '${message['id'] ?? ''}',
|
|
outcome: ChatTaskOutcome.values.byName(match.group(1)!),
|
|
summary: (match.group(2) ?? '').trim(),
|
|
);
|
|
}
|
|
|
|
/// The task notice a `system_notice` row carries, or null.
|
|
ChatTaskNotice? _rowNotice(JsonMap row) {
|
|
if (row['kind'] != 'system_notice') return null;
|
|
final message = row['message'];
|
|
return message is Map
|
|
? chatTaskNotice(Map<String, dynamic>.from(message))
|
|
: null;
|
|
}
|
|
|
|
/// Folds each run of two or more consecutive background-task notice rows into
|
|
/// one `task_notices` row, the way a turn's tool calls fold into one chip.
|
|
///
|
|
/// The group keeps the first notice's key and lists every notice's id in
|
|
/// `messageIds`, so the row keeps its place when a later notice joins it, and
|
|
/// its `messages` in transcript order. A lone
|
|
/// notice, and any other notice, stays its own `system_notice` line.
|
|
List<JsonMap> groupTaskNotices(List<JsonMap> rows) {
|
|
final result = <JsonMap>[];
|
|
final run = <JsonMap>[];
|
|
void close() {
|
|
if (run.length == 1) {
|
|
result.add(run.single);
|
|
} else if (run.isNotEmpty) {
|
|
result.add({
|
|
'kind': 'task_notices',
|
|
'key': run.first['key'],
|
|
// Every grouped message's id, so the row keeps one widget identity as
|
|
// notices join it at either end (see ChatRowKeys).
|
|
'messageIds': [for (final row in run) '${row['key'] ?? ''}'],
|
|
'messages': [for (final row in run) row['message']],
|
|
});
|
|
}
|
|
run.clear();
|
|
}
|
|
|
|
for (final row in rows) {
|
|
if (_rowNotice(row) != null) {
|
|
run.add(row);
|
|
continue;
|
|
}
|
|
close();
|
|
result.add(row);
|
|
}
|
|
close();
|
|
return result;
|
|
}
|
|
|
|
/// The notices a `task_notices` row groups, in transcript order.
|
|
List<ChatTaskNotice> chatTaskNoticesOf(JsonMap row) => [
|
|
for (final message in (row['messages'] as List? ?? const []))
|
|
if (message is Map) ?chatTaskNotice(Map<String, dynamic>.from(message)),
|
|
];
|
|
|
|
/// `1 background task` / `N background tasks`.
|
|
String chatTaskNoticesLabel(int count) =>
|
|
count == 1 ? '1 background task' : '$count background tasks';
|