concatenate method

ChatMessage concatenate(
  1. ChatMessage other
)

Concatenates this message with another message.

Throws ArgumentError if:

  • Roles are different.
  • Finish statuses are both not null and different.
  • Metadata sets are different.

Implementation

ChatMessage concatenate(ChatMessage other) {
  if (role != other.role) {
    throw ArgumentError('Roles must match for concatenation');
  }

  if (finishStatus != null &&
      other.finishStatus != null &&
      finishStatus != other.finishStatus) {
    throw ArgumentError('Finish statuses must match for concatenation');
  }

  if (!const DeepCollectionEquality().equals(metadata, other.metadata)) {
    throw ArgumentError(
      'Metadata sets should be equal, '
      'but found $metadata and ${other.metadata}',
    );
  }

  return copyWith(
    parts: [...parts, ...other.parts],
    finishStatus: finishStatus ?? other.finishStatus,
  );
}