copy method

List<ChatMessage> copy()

Creates a copy of the list of ChatMessage objects.

This method iterates over the current list of ChatMessage instances, creates a new ChatMessage for each one with the same role and content, and returns a new list containing these copied messages.

Returns: A new list of ChatMessage objects with the same role and content as the original list.

Implementation

List<ChatMessage> copy() {
  final List<ChatMessage> messages = [];

  for (var message in this) {
    messages.add(
      ChatMessage.withRole(role: message.role, content: message.content),
    );
  }

  return messages;
}