MCPcopy Create free account
hub / github.com/Noumena-Network/code / retryWithBackoff

Function retryWithBackoff

src/services/api/filesApi.ts:98–124  ·  view source on GitHub ↗

* Executes an operation with exponential backoff retry logic * * @param operation - Operation name for logging * @param attemptFn - Function to execute on each attempt, returns RetryResult * @returns The successful result value * @throws Error if all retries exhausted

(
  operation: string,
  attemptFn: (attempt: number) => Promise<RetryResult<T>>,
)

Source from the content-addressed store, hash-verified

96 * @throws Error if all retries exhausted
97 */
98async function retryWithBackoff<T>(
99 operation: string,
100 attemptFn: (attempt: number) => Promise<RetryResult<T>>,
101): Promise<T> {
102 let lastError = ''
103
104 for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
105 const result = await attemptFn(attempt)
106
107 if (result.done) {
108 return result.value
109 }
110
111 lastError = result.error || `${operation} failed`
112 logDebug(
113 `${operation} attempt ${attempt}/${MAX_RETRIES} failed: ${lastError}`,
114 )
115
116 if (attempt < MAX_RETRIES) {
117 const delayMs = BASE_DELAY_MS * Math.pow(2, attempt - 1)
118 logDebug(`Retrying ${operation} in ${delayMs}ms...`)
119 await sleep(delayMs)
120 }
121 }
122
123 throw new Error(`${lastError} after ${MAX_RETRIES} attempts`)
124}
125
126/**
127 * Downloads a single file from the Anthropic Public Files API

Callers 3

downloadFileFunction · 0.85
uploadFileFunction · 0.85
listFilesCreatedAfterFunction · 0.85

Calls 2

logDebugFunction · 0.70
sleepFunction · 0.50

Tested by

no test coverage detected