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; } /// 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) { 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 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';