execute<T> method

Future<T> execute<T>(
  1. Future<T> action()
)

Executes action with automatic retries on retryable errors.

Returns the result of action on success. Throws the last error if all retries are exhausted.

Implementation

Future<T> execute<T>(Future<T> Function() action) async {
  int attempt = 0;
  while (true) {
    try {
      return await action();
    } on AIError catch (e) {
      attempt++;
      if (!e.isRetryable || attempt >= maxRetries) {
        rethrow;
      }

      final delay = _calculateDelay(attempt, e);
      await Future.delayed(delay);
    }
  }
}