Files
pipelines/app/lib/features/chat/domain/chat_task_notices.dart
T
alan 9be31b8ba0 Re-derive only the live turn's transcript rows per streamed delta
ChatTranscriptRows keeps each message run's rows while the run holds the
identical message objects; applyChatEvent and the controller keep untouched
messages' identity. Shown-item filtering, task-notice groups and row-key ids
are held per row object, and the view reads message ids once per list.
Tests check the incremental path equals a full derivation across streaming,
appends, shown items, cards, turn diffs and prepended pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 18:24:02 +00:00

130 lines
4.3 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;
}
// The group row last built for a run, by the run's first row. Rows are values
// (see `transcriptRows`), so the same run yields the same group object.
final Expando<({List<JsonMap> run, JsonMap group})> _groups = Expando(
'chat task notice group',
);
bool _identicalRows(List<JsonMap> a, List<JsonMap> b) {
if (a.length != b.length) return false;
for (var index = 0; index < a.length; index++) {
if (!identical(a[index], b[index])) return false;
}
return true;
}
/// 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) {
final held = _groups[run.first];
if (held != null && _identicalRows(held.run, run)) {
result.add(held.group);
} else {
final group = <String, dynamic>{
'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']],
};
_groups[run.first] = (run: List<JsonMap>.of(run), group: group);
result.add(group);
}
}
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';