getMessageIdFromLastStep method

Future<String?> getMessageIdFromLastStep({
  1. required String runId,
  2. required String threadId,
})

Implementation

Future<String?> getMessageIdFromLastStep({
  required String runId,
  required String threadId,
}) async {
  final result = await _repository.getListRunSteps(runId, threadId);

  return await result.fold(
    (l) => null,
    (r) async {
      if (r.isEmpty) {
        await Future.delayed(const Duration(seconds: 1));
        return getMessageIdFromLastStep(runId: runId, threadId: threadId);
      }

      final lastStep = r.first;

      if (lastStep.status != 'completed') {
        await Future.delayed(const Duration(seconds: 1));
        return getMessageIdFromLastStep(runId: runId, threadId: threadId);
      }

      return lastStep.stepDetails?.messageCreation?.messageId == null
          ? null
          : lastStep.stepDetails!.messageCreation!.messageId!;
    },
  );
}