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 [: ]`. /// 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.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 run, JsonMap group})> _groups = Expando( 'chat task notice group', ); bool _identicalRows(List a, List 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 groupTaskNotices(List rows) { final result = []; final run = []; 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 = { '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.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 chatTaskNoticesOf(JsonMap row) => [ for (final message in (row['messages'] as List? ?? const [])) if (message is Map) ?chatTaskNotice(Map.from(message)), ]; /// `1 background task` / `N background tasks`. String chatTaskNoticesLabel(int count) => count == 1 ? '1 background task' : '$count background tasks';