copyWith method

ChatMessage copyWith({
  1. String? message,
  2. bool? isUser,
  3. bool? isWaiting,
  4. DateTime? timestamp,
  5. String? type,
  6. Object? threadId = const Object(),
  7. Object? citations = const Object(),
  8. bool? clearCitations,
})

Create a copy of this message with updated fields

Implementation

ChatMessage copyWith({
  String? message,
  bool? isUser,
  bool? isWaiting,
  DateTime? timestamp,
  String? type,
  // Use Object() as a sentinel for nulling the value if needed, otherwise standard null means no change
  Object? threadId = const Object(),
  Object? citations = const Object(), // Use Object() as sentinel for nulling
  bool? clearCitations, // Explicit flag to clear citations
}) {
  // Determine final citations based on flags and input
  List<Map<String, dynamic>>? finalCitations;
  if (clearCitations == true) {
    finalCitations = null;
  } else if (citations is List<Map<String, dynamic>>?) {
    // Explicitly passed citations (could be null)
    finalCitations = citations;
  } else {
    // Sentinel means no change from original
    finalCitations = this.citations;
  }

  return ChatMessage(
    message: message ?? this.message,
    isUser: isUser ?? this.isUser,
    isWaiting: isWaiting ?? this.isWaiting,
    timestamp: timestamp ?? this.timestamp,
    type: type ?? this.type,
    // Handle threadId update or removal
    threadId: threadId is String?
        ? threadId
        : (threadId == const Object() ? this.threadId : null),
    citations: finalCitations,
  );
}