collapseBackgroundBashNotifications static method

List<RenderableMessage> collapseBackgroundBashNotifications(
  1. List<RenderableMessage> messages, {
  2. required bool verbose,
  3. required bool fullscreenEnabled,
})

Collapses consecutive completed-background-bash task-notifications into a single synthetic "N background commands completed" notification.

Implementation

static List<RenderableMessage> collapseBackgroundBashNotifications(
  List<RenderableMessage> messages, {
  required bool verbose,
  required bool fullscreenEnabled,
}) {
  if (!fullscreenEnabled) return messages;
  if (verbose) return messages;

  final result = <RenderableMessage>[];
  var i = 0;

  while (i < messages.length) {
    final msg = messages[i];
    if (_isCompletedBackgroundBash(msg)) {
      var count = 0;
      while (i < messages.length && _isCompletedBackgroundBash(messages[i])) {
        count++;
        i++;
      }
      if (count == 1) {
        result.add(msg);
      } else {
        result.add(
          RenderableMessage(
            type: msg.type,
            uuid: msg.uuid,
            timestamp: msg.timestamp,
            message: MessageData(
              role: 'user',
              content: [
                {
                  'type': 'text',
                  'text':
                      '<$taskNotificationTag><$statusTag>completed</$statusTag><$summaryTag>$count background commands completed</$summaryTag></$taskNotificationTag>',
                },
              ],
            ),
          ),
        );
      }
    } else {
      result.add(msg);
      i++;
    }
  }

  return result;
}