loadConversation method

Future<void> loadConversation(
  1. String conversationId, {
  2. bool isManualLoad = false,
})

Implementation

Future<void> loadConversation(
  String conversationId, {
  bool isManualLoad = false,
}) async {
  try {
    if (isManualLoad) {
      PupauEventService.instance.emitPupauEvent(
        PupauEvent(
          type: UpdateConversationType.conversationChanged,
          payload: {"conversationId": conversationId},
        ),
      );
    }
    resetConversation();
    isLoadingConversation.value = true;
    loadingMessage.value = LoadingMessage(
      message: "",
      loadingType: LoadingType.dots,
    );
    update();
    conversation.value = await ConversationService.getConversation(
      assistantId,
      conversationId,
      isMarketplace,
    );
    if (conversation.value == null) {
      isLoadingConversation.value = false;
      isConversationHistoryLoaded = false;
      update();
      return;
    }
    Get.find<AttachmentsController>().loadAttachments();
    await loadConversationMessages(reset: true);
    if (conversation.value != null && messages.length > 1) {
      _isFirstMessage = false;
    }
    isLoadingConversation.value = false;
    isConversationHistoryLoaded = true;
    update();
    scrollToBottomChat();
    PupauEventService.instance.emitPupauEvent(
      PupauEvent(
        type: UpdateConversationType.conversationChanged,
        payload: {
          "assistantId": assistantId,
          "assistantType": assistant.value?.type ?? AssistantType.assistant,
          "conversationId": conversation.value!.id,
        },
      ),
    );
  } catch (e) {
    String errorMessage = 'Failed to load conversation: ${e.toString()}';
    PupauEventService.instance.emitPupauEvent(
      PupauEvent(
        type: UpdateConversationType.error,
        payload: {
          "error": errorMessage,
          "assistantId": assistantId,
          "assistantType": assistant.value?.type ?? AssistantType.assistant,
          "conversationId": conversationId,
        },
      ),
    );
    isLoadingConversation.value = false;
    update();
  }
}