@agirails/sdk 2.5.2 → 2.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ACTPClient.d.ts +18 -0
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +67 -22
- package/dist/ACTPClient.js.map +1 -1
- package/dist/adapters/BasicAdapter.d.ts +12 -0
- package/dist/adapters/BasicAdapter.d.ts.map +1 -1
- package/dist/adapters/BasicAdapter.js +30 -4
- package/dist/adapters/BasicAdapter.js.map +1 -1
- package/dist/adapters/StandardAdapter.d.ts +20 -3
- package/dist/adapters/StandardAdapter.d.ts.map +1 -1
- package/dist/adapters/StandardAdapter.js +45 -11
- package/dist/adapters/StandardAdapter.js.map +1 -1
- package/dist/cli/commands/publish.js +16 -4
- package/dist/cli/commands/publish.js.map +1 -1
- package/dist/cli/commands/register.js +16 -4
- package/dist/cli/commands/register.js.map +1 -1
- package/dist/cli/commands/tx.js +31 -3
- package/dist/cli/commands/tx.js.map +1 -1
- package/dist/cli/utils/client.d.ts.map +1 -1
- package/dist/cli/utils/client.js +1 -0
- package/dist/cli/utils/client.js.map +1 -1
- package/dist/config/networks.d.ts +2 -2
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +27 -22
- package/dist/config/networks.js.map +1 -1
- package/dist/level0/request.d.ts.map +1 -1
- package/dist/level0/request.js +2 -1
- package/dist/level0/request.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
- package/dist/runtime/BlockchainRuntime.js +11 -5
- package/dist/runtime/BlockchainRuntime.js.map +1 -1
- package/dist/runtime/MockStateManager.d.ts.map +1 -1
- package/dist/runtime/MockStateManager.js +2 -1
- package/dist/runtime/MockStateManager.js.map +1 -1
- package/dist/utils/IPFSClient.d.ts +3 -1
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +27 -7
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
- package/dist/wallet/AutoWalletProvider.js +52 -18
- package/dist/wallet/AutoWalletProvider.js.map +1 -1
- package/dist/wallet/SmartWalletRouter.d.ts +116 -0
- package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
- package/dist/wallet/SmartWalletRouter.js +212 -0
- package/dist/wallet/SmartWalletRouter.js.map +1 -0
- package/dist/wallet/aa/DualNonceManager.d.ts +19 -0
- package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
- package/dist/wallet/aa/DualNonceManager.js +100 -5
- package/dist/wallet/aa/DualNonceManager.js.map +1 -1
- package/package.json +3 -6
- package/src/ACTPClient.ts +0 -1579
- package/src/abi/ACTPKernel.json +0 -1356
- package/src/abi/AgentRegistry.json +0 -915
- package/src/abi/ERC20.json +0 -40
- package/src/abi/EscrowVault.json +0 -134
- package/src/abi/IdentityRegistry.json +0 -316
- package/src/adapters/AdapterRegistry.ts +0 -173
- package/src/adapters/AdapterRouter.ts +0 -416
- package/src/adapters/BaseAdapter.ts +0 -498
- package/src/adapters/BasicAdapter.ts +0 -514
- package/src/adapters/IAdapter.ts +0 -292
- package/src/adapters/StandardAdapter.ts +0 -555
- package/src/adapters/X402Adapter.ts +0 -731
- package/src/adapters/index.ts +0 -60
- package/src/builders/DeliveryProofBuilder.ts +0 -327
- package/src/builders/QuoteBuilder.ts +0 -483
- package/src/builders/index.ts +0 -17
- package/src/cli/commands/balance.ts +0 -110
- package/src/cli/commands/batch.ts +0 -487
- package/src/cli/commands/config.ts +0 -231
- package/src/cli/commands/deploy-check.ts +0 -364
- package/src/cli/commands/deploy-env.ts +0 -120
- package/src/cli/commands/diff.ts +0 -141
- package/src/cli/commands/init.ts +0 -469
- package/src/cli/commands/mint.ts +0 -116
- package/src/cli/commands/pay.ts +0 -113
- package/src/cli/commands/publish.ts +0 -475
- package/src/cli/commands/pull.ts +0 -124
- package/src/cli/commands/register.ts +0 -247
- package/src/cli/commands/simulate.ts +0 -345
- package/src/cli/commands/time.ts +0 -302
- package/src/cli/commands/tx.ts +0 -448
- package/src/cli/commands/watch.ts +0 -211
- package/src/cli/index.ts +0 -134
- package/src/cli/utils/client.ts +0 -251
- package/src/cli/utils/config.ts +0 -389
- package/src/cli/utils/output.ts +0 -465
- package/src/cli/utils/wallet.ts +0 -109
- package/src/config/agirailsmd.ts +0 -262
- package/src/config/networks.ts +0 -275
- package/src/config/pendingPublish.ts +0 -237
- package/src/config/publishPipeline.ts +0 -359
- package/src/config/syncOperations.ts +0 -279
- package/src/erc8004/ERC8004Bridge.ts +0 -462
- package/src/erc8004/ReputationReporter.ts +0 -468
- package/src/erc8004/index.ts +0 -61
- package/src/errors/index.ts +0 -427
- package/src/index.ts +0 -364
- package/src/level0/Provider.ts +0 -117
- package/src/level0/ServiceDirectory.ts +0 -131
- package/src/level0/index.ts +0 -10
- package/src/level0/provide.ts +0 -132
- package/src/level0/request.ts +0 -432
- package/src/level1/Agent.ts +0 -1426
- package/src/level1/index.ts +0 -10
- package/src/level1/pricing/PriceCalculator.ts +0 -255
- package/src/level1/pricing/PricingStrategy.ts +0 -198
- package/src/level1/types/Job.ts +0 -179
- package/src/level1/types/Options.ts +0 -291
- package/src/level1/types/index.ts +0 -8
- package/src/protocol/ACTPKernel.ts +0 -808
- package/src/protocol/AgentRegistry.ts +0 -559
- package/src/protocol/DIDManager.ts +0 -629
- package/src/protocol/DIDResolver.ts +0 -554
- package/src/protocol/EASHelper.ts +0 -378
- package/src/protocol/EscrowVault.ts +0 -255
- package/src/protocol/EventMonitor.ts +0 -204
- package/src/protocol/MessageSigner.ts +0 -510
- package/src/protocol/ProofGenerator.ts +0 -339
- package/src/protocol/QuoteBuilder.ts +0 -15
- package/src/registry/AgentRegistryClient.ts +0 -202
- package/src/runtime/BlockchainRuntime.ts +0 -1015
- package/src/runtime/IACTPRuntime.ts +0 -306
- package/src/runtime/MockRuntime.ts +0 -1298
- package/src/runtime/MockStateManager.ts +0 -576
- package/src/runtime/index.ts +0 -25
- package/src/runtime/types/MockState.ts +0 -237
- package/src/storage/ArchiveBundleBuilder.ts +0 -561
- package/src/storage/ArweaveClient.ts +0 -946
- package/src/storage/FilebaseClient.ts +0 -790
- package/src/storage/index.ts +0 -96
- package/src/storage/types.ts +0 -348
- package/src/types/adapter.ts +0 -310
- package/src/types/agent.ts +0 -79
- package/src/types/did.ts +0 -223
- package/src/types/eip712.ts +0 -175
- package/src/types/erc8004.ts +0 -293
- package/src/types/escrow.ts +0 -27
- package/src/types/index.ts +0 -17
- package/src/types/message.ts +0 -145
- package/src/types/state.ts +0 -87
- package/src/types/transaction.ts +0 -69
- package/src/types/x402.ts +0 -251
- package/src/utils/ErrorRecoveryGuide.ts +0 -676
- package/src/utils/Helpers.ts +0 -688
- package/src/utils/IPFSClient.ts +0 -368
- package/src/utils/Logger.ts +0 -484
- package/src/utils/NonceManager.ts +0 -591
- package/src/utils/RateLimiter.ts +0 -534
- package/src/utils/ReceivedNonceTracker.ts +0 -567
- package/src/utils/SDKLifecycle.ts +0 -416
- package/src/utils/SecureNonce.ts +0 -78
- package/src/utils/Semaphore.ts +0 -276
- package/src/utils/UsedAttestationTracker.ts +0 -385
- package/src/utils/canonicalJson.ts +0 -38
- package/src/utils/circuitBreaker.ts +0 -324
- package/src/utils/computeTypeHash.ts +0 -48
- package/src/utils/fsSafe.ts +0 -80
- package/src/utils/index.ts +0 -80
- package/src/utils/retry.ts +0 -364
- package/src/utils/security.ts +0 -418
- package/src/utils/validation.ts +0 -540
- package/src/wallet/AutoWalletProvider.ts +0 -299
- package/src/wallet/EOAWalletProvider.ts +0 -69
- package/src/wallet/IWalletProvider.ts +0 -135
- package/src/wallet/aa/BundlerClient.ts +0 -274
- package/src/wallet/aa/DualNonceManager.ts +0 -173
- package/src/wallet/aa/PaymasterClient.ts +0 -174
- package/src/wallet/aa/TransactionBatcher.ts +0 -353
- package/src/wallet/aa/UserOpBuilder.ts +0 -246
- package/src/wallet/aa/constants.ts +0 -60
- package/src/wallet/keystore.ts +0 -240
package/src/utils/retry.ts
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retry Utility - Exponential Backoff with Jitter (P1-2)
|
|
3
|
-
*
|
|
4
|
-
* Implements retry logic for storage operations with:
|
|
5
|
-
* - Exponential backoff (doubles delay each attempt)
|
|
6
|
-
* - Random jitter (prevents thundering herd)
|
|
7
|
-
* - Maximum retry limit
|
|
8
|
-
* - Configurable retry conditions
|
|
9
|
-
*
|
|
10
|
-
* @module utils/retry
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { StorageRateLimitError } from '../errors';
|
|
14
|
-
|
|
15
|
-
// ============================================================================
|
|
16
|
-
// Types
|
|
17
|
-
// ============================================================================
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Retry configuration options
|
|
21
|
-
*/
|
|
22
|
-
export interface RetryOptions {
|
|
23
|
-
/** Maximum number of retry attempts (default: 3) */
|
|
24
|
-
maxAttempts?: number;
|
|
25
|
-
|
|
26
|
-
/** Initial delay in milliseconds (default: 1000) */
|
|
27
|
-
initialDelayMs?: number;
|
|
28
|
-
|
|
29
|
-
/** Maximum delay in milliseconds (default: 30000) */
|
|
30
|
-
maxDelayMs?: number;
|
|
31
|
-
|
|
32
|
-
/** Multiplier for exponential backoff (default: 2) */
|
|
33
|
-
backoffMultiplier?: number;
|
|
34
|
-
|
|
35
|
-
/** Jitter factor 0-1 (default: 0.1 = ±10%) */
|
|
36
|
-
jitterFactor?: number;
|
|
37
|
-
|
|
38
|
-
/** Function to determine if error is retryable (default: built-in check) */
|
|
39
|
-
isRetryable?: (error: unknown) => boolean;
|
|
40
|
-
|
|
41
|
-
/** Callback for each retry attempt (for logging) */
|
|
42
|
-
onRetry?: (attempt: number, error: unknown, delayMs: number) => void;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Result of retry operation
|
|
47
|
-
*/
|
|
48
|
-
export interface RetryResult<T> {
|
|
49
|
-
/** Operation result (if successful) */
|
|
50
|
-
result?: T;
|
|
51
|
-
|
|
52
|
-
/** Final error (if all retries failed) */
|
|
53
|
-
error?: unknown;
|
|
54
|
-
|
|
55
|
-
/** Number of attempts made */
|
|
56
|
-
attempts: number;
|
|
57
|
-
|
|
58
|
-
/** Total time spent (including delays) */
|
|
59
|
-
totalTimeMs: number;
|
|
60
|
-
|
|
61
|
-
/** Whether operation succeeded */
|
|
62
|
-
success: boolean;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// ============================================================================
|
|
66
|
-
// Constants
|
|
67
|
-
// ============================================================================
|
|
68
|
-
|
|
69
|
-
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
70
|
-
const DEFAULT_INITIAL_DELAY_MS = 1000;
|
|
71
|
-
const DEFAULT_MAX_DELAY_MS = 10000; // Reduced from 30s to 10s (NEW-4: prevent long stalls)
|
|
72
|
-
const DEFAULT_BACKOFF_MULTIPLIER = 2;
|
|
73
|
-
const DEFAULT_JITTER_FACTOR = 0.1;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Default list of retryable HTTP status codes
|
|
77
|
-
*/
|
|
78
|
-
const RETRYABLE_STATUS_CODES = new Set([
|
|
79
|
-
408, // Request Timeout
|
|
80
|
-
429, // Too Many Requests
|
|
81
|
-
500, // Internal Server Error
|
|
82
|
-
502, // Bad Gateway
|
|
83
|
-
503, // Service Unavailable
|
|
84
|
-
504 // Gateway Timeout
|
|
85
|
-
]);
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Default list of retryable error codes
|
|
89
|
-
*/
|
|
90
|
-
const RETRYABLE_ERROR_CODES = new Set([
|
|
91
|
-
'ECONNRESET',
|
|
92
|
-
'ECONNREFUSED',
|
|
93
|
-
'ETIMEDOUT',
|
|
94
|
-
'ENOTFOUND',
|
|
95
|
-
'ENETUNREACH',
|
|
96
|
-
'EAI_AGAIN',
|
|
97
|
-
'EPIPE',
|
|
98
|
-
'EHOSTUNREACH'
|
|
99
|
-
]);
|
|
100
|
-
|
|
101
|
-
// ============================================================================
|
|
102
|
-
// Functions
|
|
103
|
-
// ============================================================================
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Default function to determine if error is retryable
|
|
107
|
-
*
|
|
108
|
-
* @param error - Error to check
|
|
109
|
-
* @returns True if error should trigger retry
|
|
110
|
-
*/
|
|
111
|
-
export function isRetryableError(error: unknown): boolean {
|
|
112
|
-
if (!error) return false;
|
|
113
|
-
|
|
114
|
-
// Rate limit errors are always retryable
|
|
115
|
-
if (error instanceof StorageRateLimitError) {
|
|
116
|
-
return true;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const e = error as any;
|
|
120
|
-
|
|
121
|
-
// Check HTTP status codes
|
|
122
|
-
if (e.statusCode && RETRYABLE_STATUS_CODES.has(e.statusCode)) {
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (e.status && RETRYABLE_STATUS_CODES.has(e.status)) {
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Check error codes (network errors)
|
|
131
|
-
if (e.code && RETRYABLE_ERROR_CODES.has(e.code)) {
|
|
132
|
-
return true;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Check for timeout errors
|
|
136
|
-
if (e.name === 'AbortError' || e.name === 'TimeoutError') {
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Check message for common retryable patterns
|
|
141
|
-
const message = String(e.message || e).toLowerCase();
|
|
142
|
-
if (
|
|
143
|
-
message.includes('timeout') ||
|
|
144
|
-
message.includes('rate limit') ||
|
|
145
|
-
message.includes('too many requests') ||
|
|
146
|
-
message.includes('service unavailable') ||
|
|
147
|
-
message.includes('temporarily unavailable')
|
|
148
|
-
) {
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Calculate delay with exponential backoff and jitter
|
|
157
|
-
*
|
|
158
|
-
* @param attempt - Current attempt number (1-based)
|
|
159
|
-
* @param options - Retry options
|
|
160
|
-
* @returns Delay in milliseconds
|
|
161
|
-
*/
|
|
162
|
-
export function calculateBackoffDelay(
|
|
163
|
-
attempt: number,
|
|
164
|
-
options: RetryOptions = {}
|
|
165
|
-
): number {
|
|
166
|
-
const {
|
|
167
|
-
initialDelayMs = DEFAULT_INITIAL_DELAY_MS,
|
|
168
|
-
maxDelayMs = DEFAULT_MAX_DELAY_MS,
|
|
169
|
-
backoffMultiplier = DEFAULT_BACKOFF_MULTIPLIER,
|
|
170
|
-
jitterFactor = DEFAULT_JITTER_FACTOR
|
|
171
|
-
} = options;
|
|
172
|
-
|
|
173
|
-
// Exponential backoff: initialDelay * (multiplier ^ (attempt - 1))
|
|
174
|
-
const exponentialDelay = initialDelayMs * Math.pow(backoffMultiplier, attempt - 1);
|
|
175
|
-
|
|
176
|
-
// Cap at max delay
|
|
177
|
-
const cappedDelay = Math.min(exponentialDelay, maxDelayMs);
|
|
178
|
-
|
|
179
|
-
// Add jitter: ±jitterFactor
|
|
180
|
-
const jitter = cappedDelay * jitterFactor * (Math.random() * 2 - 1);
|
|
181
|
-
const finalDelay = Math.max(0, cappedDelay + jitter);
|
|
182
|
-
|
|
183
|
-
return Math.round(finalDelay);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Sleep for specified duration
|
|
188
|
-
*
|
|
189
|
-
* @param ms - Milliseconds to sleep
|
|
190
|
-
*/
|
|
191
|
-
function sleep(ms: number): Promise<void> {
|
|
192
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Execute async operation with retry logic
|
|
197
|
-
*
|
|
198
|
-
* Implements exponential backoff with jitter for handling transient failures.
|
|
199
|
-
* Particularly useful for storage operations that may fail due to:
|
|
200
|
-
* - Rate limiting
|
|
201
|
-
* - Network timeouts
|
|
202
|
-
* - Temporary service unavailability
|
|
203
|
-
*
|
|
204
|
-
* @param operation - Async function to execute
|
|
205
|
-
* @param options - Retry configuration
|
|
206
|
-
* @returns Promise resolving to operation result or throwing final error
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```typescript
|
|
210
|
-
* // Basic usage
|
|
211
|
-
* const result = await withRetry(
|
|
212
|
-
* () => client.uploadJSON(data),
|
|
213
|
-
* { maxAttempts: 3 }
|
|
214
|
-
* );
|
|
215
|
-
*
|
|
216
|
-
* // With logging
|
|
217
|
-
* const result = await withRetry(
|
|
218
|
-
* () => client.downloadJSON(cid),
|
|
219
|
-
* {
|
|
220
|
-
* maxAttempts: 5,
|
|
221
|
-
* onRetry: (attempt, error, delay) => {
|
|
222
|
-
* console.log(`Retry ${attempt}, waiting ${delay}ms:`, error);
|
|
223
|
-
* }
|
|
224
|
-
* }
|
|
225
|
-
* );
|
|
226
|
-
* ```
|
|
227
|
-
*/
|
|
228
|
-
export async function withRetry<T>(
|
|
229
|
-
operation: () => Promise<T>,
|
|
230
|
-
options: RetryOptions = {}
|
|
231
|
-
): Promise<T> {
|
|
232
|
-
const {
|
|
233
|
-
maxAttempts = DEFAULT_MAX_ATTEMPTS,
|
|
234
|
-
isRetryable = isRetryableError,
|
|
235
|
-
onRetry
|
|
236
|
-
} = options;
|
|
237
|
-
|
|
238
|
-
let lastError: unknown;
|
|
239
|
-
|
|
240
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
241
|
-
try {
|
|
242
|
-
return await operation();
|
|
243
|
-
} catch (error) {
|
|
244
|
-
lastError = error;
|
|
245
|
-
|
|
246
|
-
// Check if this is the last attempt
|
|
247
|
-
if (attempt >= maxAttempts) {
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Check if error is retryable
|
|
252
|
-
if (!isRetryable(error)) {
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Calculate delay
|
|
257
|
-
let delayMs = calculateBackoffDelay(attempt, options);
|
|
258
|
-
|
|
259
|
-
// If rate limit error with retry-after header, use that instead
|
|
260
|
-
if (error instanceof StorageRateLimitError && error.details?.retryAfter) {
|
|
261
|
-
delayMs = Math.max(delayMs, error.details.retryAfter * 1000);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Notify callback
|
|
265
|
-
if (onRetry) {
|
|
266
|
-
onRetry(attempt, error, delayMs);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// Wait before retry
|
|
270
|
-
await sleep(delayMs);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// All retries failed
|
|
275
|
-
throw lastError;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Execute async operation with retry logic, returning detailed result
|
|
280
|
-
*
|
|
281
|
-
* Unlike `withRetry`, this function never throws - it returns a result object
|
|
282
|
-
* with success/failure status and metadata.
|
|
283
|
-
*
|
|
284
|
-
* @param operation - Async function to execute
|
|
285
|
-
* @param options - Retry configuration
|
|
286
|
-
* @returns Promise resolving to detailed result object
|
|
287
|
-
*
|
|
288
|
-
* @example
|
|
289
|
-
* ```typescript
|
|
290
|
-
* const result = await withRetryResult(
|
|
291
|
-
* () => client.uploadJSON(data),
|
|
292
|
-
* { maxAttempts: 3 }
|
|
293
|
-
* );
|
|
294
|
-
*
|
|
295
|
-
* if (result.success) {
|
|
296
|
-
* console.log('Uploaded:', result.result);
|
|
297
|
-
* } else {
|
|
298
|
-
* console.error(`Failed after ${result.attempts} attempts:`, result.error);
|
|
299
|
-
* }
|
|
300
|
-
* ```
|
|
301
|
-
*/
|
|
302
|
-
export async function withRetryResult<T>(
|
|
303
|
-
operation: () => Promise<T>,
|
|
304
|
-
options: RetryOptions = {}
|
|
305
|
-
): Promise<RetryResult<T>> {
|
|
306
|
-
const {
|
|
307
|
-
maxAttempts = DEFAULT_MAX_ATTEMPTS,
|
|
308
|
-
isRetryable = isRetryableError,
|
|
309
|
-
onRetry
|
|
310
|
-
} = options;
|
|
311
|
-
|
|
312
|
-
let lastError: unknown;
|
|
313
|
-
const startTime = Date.now();
|
|
314
|
-
let attempts = 0;
|
|
315
|
-
|
|
316
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
317
|
-
attempts = attempt;
|
|
318
|
-
|
|
319
|
-
try {
|
|
320
|
-
const result = await operation();
|
|
321
|
-
return {
|
|
322
|
-
result,
|
|
323
|
-
attempts,
|
|
324
|
-
totalTimeMs: Date.now() - startTime,
|
|
325
|
-
success: true
|
|
326
|
-
};
|
|
327
|
-
} catch (error) {
|
|
328
|
-
lastError = error;
|
|
329
|
-
|
|
330
|
-
// Check if this is the last attempt
|
|
331
|
-
if (attempt >= maxAttempts) {
|
|
332
|
-
break;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// Check if error is retryable
|
|
336
|
-
if (!isRetryable(error)) {
|
|
337
|
-
break;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// Calculate delay
|
|
341
|
-
let delayMs = calculateBackoffDelay(attempt, options);
|
|
342
|
-
|
|
343
|
-
// If rate limit error with retry-after header, use that instead
|
|
344
|
-
if (error instanceof StorageRateLimitError && error.details?.retryAfter) {
|
|
345
|
-
delayMs = Math.max(delayMs, error.details.retryAfter * 1000);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// Notify callback
|
|
349
|
-
if (onRetry) {
|
|
350
|
-
onRetry(attempt, error, delayMs);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Wait before retry
|
|
354
|
-
await sleep(delayMs);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
return {
|
|
359
|
-
error: lastError,
|
|
360
|
-
attempts,
|
|
361
|
-
totalTimeMs: Date.now() - startTime,
|
|
362
|
-
success: false
|
|
363
|
-
};
|
|
364
|
-
}
|