AIToolArgumentMessage.fromMap constructor

AIToolArgumentMessage.fromMap(
  1. dynamic map, {
  2. AppEntity? receiver,
})

Creates an instance from a map.

Implementation

factory AIToolArgumentMessage.fromMap(dynamic map, {AppEntity? receiver}) {
  if (map == null) {
    throw ArgumentError('The AIToolArgumentMessage map is null');
  }

  final appEntity = (map['receiver'] == null)
      ? receiver
      : (map['receiverType'] == CometChatReceiverType.user)
      ? User.fromMap(map['receiver'])
      : Group.fromMap(map['receiver']);

  final conversationId = (map['conversationId'] == null ||
      (map['conversationId'] as String).isEmpty)
      ? map['receiverType'] == CometChatReceiverType.user
      ? '${map['sender']?['uid']}_user_${(appEntity as User).uid}'
      : 'group_${map['receiver']['guid']}'
      : map['conversationId'];

  // Parse toolCalls list from JSON string or List<dynamic>
  List<AIToolCall>? toolCallsList;
// toolCalls parsing
  if (map['toolCalls'] != null) {
    if (map['toolCalls'] is String) {
      final decoded = json.decode(map['toolCalls']) as List<dynamic>;
      toolCallsList = decoded
          .map((e) => AIToolCall.fromMap(Map<String, dynamic>.from(e)))
          .toList();
    } else if (map['toolCalls'] is List) {
      toolCallsList = (map['toolCalls'] as List)
          .map((e) => AIToolCall.fromMap(Map<String, dynamic>.from(e)))
          .toList();
    }
  }


  return AIToolArgumentMessage(
    runId: map['runId'] ?? map['runID'], // handle possible key variation
    threadId: map['threadId'],
    toolCalls: toolCallsList,
    tags: (map['tags'] != null)
        ? List<String>.from(map['tags'])
        : null,
    id: map['id'] ?? 0,
    muid: map['muid'] ?? '',
    sender: map['sender'] != null ? User.fromMap(map['sender']) : null,
    receiver: appEntity,
    receiverUid: map['receiverUid'] ?? '',
    type: map['type'] ?? CometChatMessageType.assistant,
    receiverType: map['receiverType'] ?? '',
    category: map['category'] ?? CometChatMessageCategory.categoryAgentic,
    sentAt: map['sentAt'] == null || map['sentAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['sentAt'] * 1000),
    deliveredAt: map['deliveredAt'] == null || map['deliveredAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deliveredAt'] * 1000),
    readAt: map['readAt'] == null || map['readAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['readAt'] * 1000),
    metadata: map['metadata'] == null
        ? {}
        : Map<String, dynamic>.from(
        map['metadata'] is String ? json.decode(map['metadata']) : map['metadata']),
    readByMeAt: map['readByMeAt'] == null || map['readByMeAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['readByMeAt'] * 1000),
    deliveredToMeAt: map['deliveredToMeAt'] == null || map['deliveredToMeAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deliveredToMeAt'] * 1000),
    deletedAt: map['deletedAt'] == null || map['deletedAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deletedAt'] * 1000),
    editedAt: map['editedAt'] == null || map['editedAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['editedAt'] * 1000),
    deletedBy: map['deletedBy'],
    editedBy: map['editedBy'],
    updatedAt: map['updatedAt'] == null || map['updatedAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['updatedAt'] * 1000),
    conversationId: conversationId,
    parentMessageId: map['parentMessageId'] ?? 0,
    replyCount: map['replyCount'] ?? 0,
    unreadRepliesCount: map['unreadRepliesCount'] ?? 0,
    mentionedUsers: map['mentionedUsers'] != null
        ? (map['mentionedUsers'] as List)
        .map((e) => User.fromMap(e))
        .toList()
        : [],
    hasMentionedMe: map['hasMentionedMe'] ?? false,
    reactions: map['reactions'] != null
        ? (map['reactions'] as List)
        .map((e) => ReactionCount.fromMap(e))
        .toList()
        : [],
  );
}