Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

2.7 KB
Raw Blame
import { RETRY_CONFIG } from "../config/constants.ts";
import { LLM_REQUEST_TIMEOUT_CODE } from "../helpers/llmRequestTimeout.js";
import { PROVIDER_ERROR_CODES } from "../helpers/providerHttpErrors.js";

export interface RetryOptions {
  maxRetries?: number;
  initialDelay?: number;
  maxDelay?: number;
  backoffMultiplier?: number;
  shouldRetry?: (error: any) => boolean;
}

export async function withRetry<T>(fn: () => Promise<T>, options: RetryOptions = {}): Promise<T> {
  const {
    maxRetries = RETRY_CONFIG.MAX_RETRIES,
    initialDelay = RETRY_CONFIG.INITIAL_DELAY,
    maxDelay = RETRY_CONFIG.MAX_DELAY,
    backoffMultiplier = RETRY_CONFIG.BACKOFF_MULTIPLIER,
    shouldRetry = () => true,
  } = options;

  let lastError: any;
  let delay = initialDelay;

  for (let attempt = 0; attempt <= maxRetries; attempt++) {
    try {
      return await fn();
    } catch (error) {
      lastError = error;

      if (attempt === maxRetries || !shouldRetry(error)) {
        throw error;
      }

      // Wait before retrying with exponential backoff
      await new Promise((resolve) => setTimeout(resolve, delay));
      delay = Math.min(delay * backoffMultiplier, maxDelay);
    }
  }

  throw lastError;
}

// Status lets createApiRetryStrategy tell an HTTP rejection from a network fault.
export function httpError(message: string, status: number): Error & { status: number } {
  return Object.assign(new Error(message), { status });
}

// Specific retry strategy for API calls
export function createApiRetryStrategy() {
  return {
    shouldRetry: (error: any) => {
      // A client-side deadline is not a transient fault: the same request under
      // the same deadline expires again, and the provider bills every attempt.
      if (error?.code === LLM_REQUEST_TIMEOUT_CODE) return false;

      // An empty account stays empty: retrying only delays the message.
      if (error?.code === PROVIDER_ERROR_CODES.QUOTA_EXHAUSTED) return false;

      // No HTTP status means the request never got an answer (network drop).
      const status = error?.status ?? error?.response?.status;
      if (typeof status !== "number") return true;

      // Most 4xx are deterministic rejections. 408 is a request timeout and 429 is
      // a rate limit; both can clear on retry, as can 5xx server faults.
      return status === 408 || status === 429 || (status >= 500 && status < 600);
    },
  };
}

// Specific retry strategy for file operations
export function createFileRetryStrategy() {
  return {
    shouldRetry: (error: any) => {
      // Retry on temporary file system errors
      const retriableErrors = ["EBUSY", "ENOENT", "EPERM", "EAGAIN"];
      return retriableErrors.includes(error.code);
    },
    maxRetries: 2,
    initialDelay: 500,
  };
}