@agirails/sdk 2.5.3 → 2.5.5

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.
Files changed (169) hide show
  1. package/dist/ACTPClient.d.ts +18 -0
  2. package/dist/ACTPClient.d.ts.map +1 -1
  3. package/dist/ACTPClient.js +72 -23
  4. package/dist/ACTPClient.js.map +1 -1
  5. package/dist/adapters/BasicAdapter.d.ts +15 -0
  6. package/dist/adapters/BasicAdapter.d.ts.map +1 -1
  7. package/dist/adapters/BasicAdapter.js +33 -4
  8. package/dist/adapters/BasicAdapter.js.map +1 -1
  9. package/dist/adapters/StandardAdapter.d.ts +20 -3
  10. package/dist/adapters/StandardAdapter.d.ts.map +1 -1
  11. package/dist/adapters/StandardAdapter.js +90 -12
  12. package/dist/adapters/StandardAdapter.js.map +1 -1
  13. package/dist/cli/commands/publish.js +16 -4
  14. package/dist/cli/commands/publish.js.map +1 -1
  15. package/dist/cli/commands/register.js +16 -4
  16. package/dist/cli/commands/register.js.map +1 -1
  17. package/dist/cli/commands/tx.js +31 -3
  18. package/dist/cli/commands/tx.js.map +1 -1
  19. package/dist/config/networks.d.ts +10 -2
  20. package/dist/config/networks.d.ts.map +1 -1
  21. package/dist/config/networks.js +31 -22
  22. package/dist/config/networks.js.map +1 -1
  23. package/dist/level0/request.d.ts.map +1 -1
  24. package/dist/level0/request.js +2 -1
  25. package/dist/level0/request.js.map +1 -1
  26. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
  27. package/dist/runtime/BlockchainRuntime.js +11 -5
  28. package/dist/runtime/BlockchainRuntime.js.map +1 -1
  29. package/dist/utils/IPFSClient.d.ts +3 -1
  30. package/dist/utils/IPFSClient.d.ts.map +1 -1
  31. package/dist/utils/IPFSClient.js +27 -7
  32. package/dist/utils/IPFSClient.js.map +1 -1
  33. package/dist/wallet/AutoWalletProvider.d.ts +11 -1
  34. package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
  35. package/dist/wallet/AutoWalletProvider.js +84 -19
  36. package/dist/wallet/AutoWalletProvider.js.map +1 -1
  37. package/dist/wallet/IWalletProvider.d.ts +34 -0
  38. package/dist/wallet/IWalletProvider.d.ts.map +1 -1
  39. package/dist/wallet/SmartWalletRouter.d.ts +128 -0
  40. package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
  41. package/dist/wallet/SmartWalletRouter.js +248 -0
  42. package/dist/wallet/SmartWalletRouter.js.map +1 -0
  43. package/dist/wallet/aa/DualNonceManager.d.ts +26 -1
  44. package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
  45. package/dist/wallet/aa/DualNonceManager.js +140 -6
  46. package/dist/wallet/aa/DualNonceManager.js.map +1 -1
  47. package/package.json +3 -6
  48. package/src/ACTPClient.ts +0 -1579
  49. package/src/abi/ACTPKernel.json +0 -1356
  50. package/src/abi/AgentRegistry.json +0 -915
  51. package/src/abi/ERC20.json +0 -40
  52. package/src/abi/EscrowVault.json +0 -134
  53. package/src/abi/IdentityRegistry.json +0 -316
  54. package/src/adapters/AdapterRegistry.ts +0 -173
  55. package/src/adapters/AdapterRouter.ts +0 -416
  56. package/src/adapters/BaseAdapter.ts +0 -498
  57. package/src/adapters/BasicAdapter.ts +0 -514
  58. package/src/adapters/IAdapter.ts +0 -292
  59. package/src/adapters/StandardAdapter.ts +0 -555
  60. package/src/adapters/X402Adapter.ts +0 -731
  61. package/src/adapters/index.ts +0 -60
  62. package/src/builders/DeliveryProofBuilder.ts +0 -327
  63. package/src/builders/QuoteBuilder.ts +0 -483
  64. package/src/builders/index.ts +0 -17
  65. package/src/cli/commands/balance.ts +0 -110
  66. package/src/cli/commands/batch.ts +0 -487
  67. package/src/cli/commands/config.ts +0 -231
  68. package/src/cli/commands/deploy-check.ts +0 -364
  69. package/src/cli/commands/deploy-env.ts +0 -120
  70. package/src/cli/commands/diff.ts +0 -141
  71. package/src/cli/commands/init.ts +0 -469
  72. package/src/cli/commands/mint.ts +0 -116
  73. package/src/cli/commands/pay.ts +0 -113
  74. package/src/cli/commands/publish.ts +0 -475
  75. package/src/cli/commands/pull.ts +0 -124
  76. package/src/cli/commands/register.ts +0 -247
  77. package/src/cli/commands/simulate.ts +0 -345
  78. package/src/cli/commands/time.ts +0 -302
  79. package/src/cli/commands/tx.ts +0 -448
  80. package/src/cli/commands/watch.ts +0 -211
  81. package/src/cli/index.ts +0 -134
  82. package/src/cli/utils/client.ts +0 -252
  83. package/src/cli/utils/config.ts +0 -389
  84. package/src/cli/utils/output.ts +0 -465
  85. package/src/cli/utils/wallet.ts +0 -109
  86. package/src/config/agirailsmd.ts +0 -262
  87. package/src/config/networks.ts +0 -275
  88. package/src/config/pendingPublish.ts +0 -237
  89. package/src/config/publishPipeline.ts +0 -359
  90. package/src/config/syncOperations.ts +0 -279
  91. package/src/erc8004/ERC8004Bridge.ts +0 -462
  92. package/src/erc8004/ReputationReporter.ts +0 -468
  93. package/src/erc8004/index.ts +0 -61
  94. package/src/errors/index.ts +0 -427
  95. package/src/index.ts +0 -364
  96. package/src/level0/Provider.ts +0 -117
  97. package/src/level0/ServiceDirectory.ts +0 -131
  98. package/src/level0/index.ts +0 -10
  99. package/src/level0/provide.ts +0 -132
  100. package/src/level0/request.ts +0 -432
  101. package/src/level1/Agent.ts +0 -1426
  102. package/src/level1/index.ts +0 -10
  103. package/src/level1/pricing/PriceCalculator.ts +0 -255
  104. package/src/level1/pricing/PricingStrategy.ts +0 -198
  105. package/src/level1/types/Job.ts +0 -179
  106. package/src/level1/types/Options.ts +0 -291
  107. package/src/level1/types/index.ts +0 -8
  108. package/src/protocol/ACTPKernel.ts +0 -808
  109. package/src/protocol/AgentRegistry.ts +0 -559
  110. package/src/protocol/DIDManager.ts +0 -629
  111. package/src/protocol/DIDResolver.ts +0 -554
  112. package/src/protocol/EASHelper.ts +0 -378
  113. package/src/protocol/EscrowVault.ts +0 -255
  114. package/src/protocol/EventMonitor.ts +0 -204
  115. package/src/protocol/MessageSigner.ts +0 -510
  116. package/src/protocol/ProofGenerator.ts +0 -339
  117. package/src/protocol/QuoteBuilder.ts +0 -15
  118. package/src/registry/AgentRegistryClient.ts +0 -202
  119. package/src/runtime/BlockchainRuntime.ts +0 -1015
  120. package/src/runtime/IACTPRuntime.ts +0 -306
  121. package/src/runtime/MockRuntime.ts +0 -1298
  122. package/src/runtime/MockStateManager.ts +0 -577
  123. package/src/runtime/index.ts +0 -25
  124. package/src/runtime/types/MockState.ts +0 -237
  125. package/src/storage/ArchiveBundleBuilder.ts +0 -561
  126. package/src/storage/ArweaveClient.ts +0 -946
  127. package/src/storage/FilebaseClient.ts +0 -790
  128. package/src/storage/index.ts +0 -96
  129. package/src/storage/types.ts +0 -348
  130. package/src/types/adapter.ts +0 -310
  131. package/src/types/agent.ts +0 -79
  132. package/src/types/did.ts +0 -223
  133. package/src/types/eip712.ts +0 -175
  134. package/src/types/erc8004.ts +0 -293
  135. package/src/types/escrow.ts +0 -27
  136. package/src/types/index.ts +0 -17
  137. package/src/types/message.ts +0 -145
  138. package/src/types/state.ts +0 -87
  139. package/src/types/transaction.ts +0 -69
  140. package/src/types/x402.ts +0 -251
  141. package/src/utils/ErrorRecoveryGuide.ts +0 -676
  142. package/src/utils/Helpers.ts +0 -688
  143. package/src/utils/IPFSClient.ts +0 -368
  144. package/src/utils/Logger.ts +0 -484
  145. package/src/utils/NonceManager.ts +0 -591
  146. package/src/utils/RateLimiter.ts +0 -534
  147. package/src/utils/ReceivedNonceTracker.ts +0 -567
  148. package/src/utils/SDKLifecycle.ts +0 -416
  149. package/src/utils/SecureNonce.ts +0 -78
  150. package/src/utils/Semaphore.ts +0 -276
  151. package/src/utils/UsedAttestationTracker.ts +0 -385
  152. package/src/utils/canonicalJson.ts +0 -38
  153. package/src/utils/circuitBreaker.ts +0 -324
  154. package/src/utils/computeTypeHash.ts +0 -48
  155. package/src/utils/fsSafe.ts +0 -80
  156. package/src/utils/index.ts +0 -80
  157. package/src/utils/retry.ts +0 -364
  158. package/src/utils/security.ts +0 -418
  159. package/src/utils/validation.ts +0 -540
  160. package/src/wallet/AutoWalletProvider.ts +0 -299
  161. package/src/wallet/EOAWalletProvider.ts +0 -69
  162. package/src/wallet/IWalletProvider.ts +0 -135
  163. package/src/wallet/aa/BundlerClient.ts +0 -274
  164. package/src/wallet/aa/DualNonceManager.ts +0 -173
  165. package/src/wallet/aa/PaymasterClient.ts +0 -174
  166. package/src/wallet/aa/TransactionBatcher.ts +0 -353
  167. package/src/wallet/aa/UserOpBuilder.ts +0 -246
  168. package/src/wallet/aa/constants.ts +0 -60
  169. package/src/wallet/keystore.ts +0 -240
@@ -1,676 +0,0 @@
1
- /**
2
- * ErrorRecoveryGuide - Structured Error Classification and Recovery Guidance
3
- *
4
- * SECURITY FIX (HIGH-6): Provides comprehensive error recovery documentation
5
- * to help developers handle errors appropriately and avoid security pitfalls.
6
- *
7
- * @module utils/ErrorRecoveryGuide
8
- */
9
-
10
- import { sdkLogger } from './Logger';
11
-
12
- /**
13
- * Error severity levels for prioritization
14
- */
15
- export type ErrorSeverity = 'critical' | 'high' | 'medium' | 'low';
16
-
17
- /**
18
- * Error categories for classification
19
- */
20
- export type ErrorCategory =
21
- | 'network' // Network/RPC issues
22
- | 'authentication' // Signer/key issues
23
- | 'validation' // Input validation failures
24
- | 'state' // Invalid state transitions
25
- | 'escrow' // Escrow/fund issues
26
- | 'attestation' // EAS/attestation issues
27
- | 'permission' // Access control issues
28
- | 'timeout' // Deadline/timeout issues
29
- | 'unknown'; // Unclassified errors
30
-
31
- /**
32
- * Structured error recovery information
33
- */
34
- export interface ErrorRecoveryInfo {
35
- /** Error category */
36
- category: ErrorCategory;
37
- /** Severity level */
38
- severity: ErrorSeverity;
39
- /** Human-readable description */
40
- description: string;
41
- /** Recovery steps to attempt */
42
- recoverySteps: string[];
43
- /** Whether the operation can be safely retried */
44
- retryable: boolean;
45
- /** Suggested retry delay in milliseconds (if retryable) */
46
- retryDelayMs?: number;
47
- /** Maximum retry attempts (if retryable) */
48
- maxRetries?: number;
49
- /** Whether user intervention is required */
50
- requiresUserAction: boolean;
51
- /** Security implications to be aware of */
52
- securityNotes?: string[];
53
- }
54
-
55
- /**
56
- * Error pattern for matching
57
- */
58
- interface ErrorPattern {
59
- /** Pattern to match (string contains or regex) */
60
- pattern: string | RegExp;
61
- /** Recovery information */
62
- recovery: ErrorRecoveryInfo;
63
- }
64
-
65
- /**
66
- * Predefined error patterns and their recovery guidance
67
- */
68
- const ERROR_PATTERNS: ErrorPattern[] = [
69
- // Network Errors
70
- {
71
- pattern: /provider connection lost/i,
72
- recovery: {
73
- category: 'network',
74
- severity: 'high',
75
- description: 'Lost connection to the blockchain RPC provider',
76
- recoverySteps: [
77
- '1. Check your internet connection',
78
- '2. Verify RPC endpoint is reachable',
79
- '3. SDK will automatically attempt reconnection with exponential backoff',
80
- '4. If persistent, try a different RPC provider',
81
- '5. Check RPC rate limits (may need paid tier)',
82
- ],
83
- retryable: true,
84
- retryDelayMs: 1000,
85
- maxRetries: 5,
86
- requiresUserAction: false,
87
- securityNotes: [
88
- 'Do not expose RPC URLs with API keys in client-side code',
89
- 'Consider using a fallback RPC provider',
90
- ],
91
- },
92
- },
93
- {
94
- pattern: /nonce too low/i,
95
- recovery: {
96
- category: 'network',
97
- severity: 'medium',
98
- description: 'Transaction nonce is out of sync with blockchain',
99
- recoverySteps: [
100
- '1. Wait for pending transactions to confirm',
101
- '2. Reset transaction nonce manually if needed',
102
- '3. Use getTransactionCount("pending") for accurate nonce',
103
- ],
104
- retryable: true,
105
- retryDelayMs: 5000,
106
- maxRetries: 3,
107
- requiresUserAction: false,
108
- },
109
- },
110
- {
111
- pattern: /replacement transaction underpriced/i,
112
- recovery: {
113
- category: 'network',
114
- severity: 'medium',
115
- description: 'Transaction fee too low for replacement',
116
- recoverySteps: [
117
- '1. Increase gas price by at least 10%',
118
- '2. Wait for original transaction to be mined',
119
- '3. Or wait for it to be dropped from mempool',
120
- ],
121
- retryable: true,
122
- retryDelayMs: 10000,
123
- maxRetries: 2,
124
- requiresUserAction: true,
125
- },
126
- },
127
-
128
- // Authentication Errors
129
- {
130
- pattern: /privateKey is required/i,
131
- recovery: {
132
- category: 'authentication',
133
- severity: 'critical',
134
- description: 'Missing private key for blockchain operations',
135
- recoverySteps: [
136
- '1. Provide privateKey in ACTPClient.create() config',
137
- '2. Use environment variables (process.env.PRIVATE_KEY)',
138
- '3. NEVER hardcode private keys in source code',
139
- ],
140
- retryable: false,
141
- requiresUserAction: true,
142
- securityNotes: [
143
- 'Never log or expose private keys',
144
- 'Use hardware wallets in production',
145
- 'Store keys in secure environment variables',
146
- ],
147
- },
148
- },
149
- {
150
- pattern: /not initialized.*call initialize/i,
151
- recovery: {
152
- category: 'authentication',
153
- severity: 'high',
154
- description: 'BlockchainRuntime not properly initialized',
155
- recoverySteps: [
156
- '1. Ensure await runtime.initialize() is called after construction',
157
- '2. Use ACTPClient.create() factory which handles initialization',
158
- '3. Check for initialization errors in try/catch',
159
- ],
160
- retryable: false,
161
- requiresUserAction: true,
162
- },
163
- },
164
-
165
- // Validation Errors
166
- {
167
- pattern: /invalid.*address/i,
168
- recovery: {
169
- category: 'validation',
170
- severity: 'medium',
171
- description: 'Invalid Ethereum address format',
172
- recoverySteps: [
173
- '1. Verify address is 0x-prefixed',
174
- '2. Verify address is 40 hex characters (42 total)',
175
- '3. Use ethers.isAddress() to validate before passing',
176
- ],
177
- retryable: false,
178
- requiresUserAction: true,
179
- },
180
- },
181
- {
182
- pattern: /amount must be positive/i,
183
- recovery: {
184
- category: 'validation',
185
- severity: 'medium',
186
- description: 'Transaction amount must be greater than zero',
187
- recoverySteps: [
188
- '1. Verify amount is a positive number',
189
- '2. Check for unit conversion issues (USDC has 6 decimals)',
190
- '3. Use parseUnits("100", 6) for 100 USDC',
191
- ],
192
- retryable: false,
193
- requiresUserAction: true,
194
- },
195
- },
196
- {
197
- pattern: /deadline must be in the future/i,
198
- recovery: {
199
- category: 'validation',
200
- severity: 'medium',
201
- description: 'Transaction deadline has already passed',
202
- recoverySteps: [
203
- '1. Set deadline to a future timestamp',
204
- '2. Use relative format like "+24h" or "+7d"',
205
- '3. Account for block time variations',
206
- ],
207
- retryable: false,
208
- requiresUserAction: true,
209
- },
210
- },
211
-
212
- // State Errors
213
- {
214
- pattern: /invalid state transition/i,
215
- recovery: {
216
- category: 'state',
217
- severity: 'high',
218
- description: 'Cannot transition from current state to requested state',
219
- recoverySteps: [
220
- '1. Check current transaction state with getTransaction()',
221
- '2. Verify state transition is allowed (see ACTP state machine)',
222
- '3. Valid transitions: INITIATED->COMMITTED->DELIVERED->SETTLED',
223
- ],
224
- retryable: false,
225
- requiresUserAction: true,
226
- },
227
- },
228
- {
229
- pattern: /cannot link escrow.*state/i,
230
- recovery: {
231
- category: 'state',
232
- severity: 'high',
233
- description: 'Escrow can only be linked in INITIATED or QUOTED state',
234
- recoverySteps: [
235
- '1. Check transaction state with getTransaction()',
236
- '2. linkEscrow() only works in INITIATED or QUOTED state',
237
- '3. Create a new transaction if needed',
238
- ],
239
- retryable: false,
240
- requiresUserAction: true,
241
- },
242
- },
243
- {
244
- pattern: /unknown transaction state/i,
245
- recovery: {
246
- category: 'state',
247
- severity: 'critical',
248
- description: 'Contract returned unexpected state value',
249
- recoverySteps: [
250
- '1. Check SDK version matches contract version',
251
- '2. Verify contract address is correct',
252
- '3. Contact support - may indicate contract upgrade needed',
253
- ],
254
- retryable: false,
255
- requiresUserAction: true,
256
- securityNotes: [
257
- 'Unknown states may indicate contract tampering',
258
- 'Verify contract address against official deployment',
259
- ],
260
- },
261
- },
262
-
263
- // Escrow Errors
264
- {
265
- pattern: /insufficient.*balance/i,
266
- recovery: {
267
- category: 'escrow',
268
- severity: 'high',
269
- description: 'Insufficient token balance for escrow deposit',
270
- recoverySteps: [
271
- '1. Check USDC balance with getBalance()',
272
- '2. Ensure sufficient USDC to cover amount + gas',
273
- '3. In mock mode: use mintTokens() to add funds',
274
- '4. In testnet: get USDC from faucet',
275
- ],
276
- retryable: false,
277
- requiresUserAction: true,
278
- },
279
- },
280
- {
281
- pattern: /escrow.*not found/i,
282
- recovery: {
283
- category: 'escrow',
284
- severity: 'high',
285
- description: 'Referenced escrow does not exist',
286
- recoverySteps: [
287
- '1. Verify escrowId is correct',
288
- '2. Check if escrow was already released',
289
- '3. Use getTransaction() to find linked escrowId',
290
- ],
291
- retryable: false,
292
- requiresUserAction: true,
293
- },
294
- },
295
-
296
- // Attestation Errors
297
- {
298
- pattern: /attestation.*required.*escrow release/i,
299
- recovery: {
300
- category: 'attestation',
301
- severity: 'critical',
302
- description: 'Attestation verification required before escrow release',
303
- recoverySteps: [
304
- '1. Provider must submit delivery attestation via EAS',
305
- '2. Pass attestationParams to releaseEscrow()',
306
- '3. attestationParams: { txId, attestationUID }',
307
- '4. Verify attestation is not revoked before using',
308
- ],
309
- retryable: false,
310
- requiresUserAction: true,
311
- securityNotes: [
312
- 'Never release escrow without valid attestation',
313
- 'Attestation prevents fund theft',
314
- ],
315
- },
316
- },
317
- {
318
- pattern: /attestation.*already used/i,
319
- recovery: {
320
- category: 'attestation',
321
- severity: 'critical',
322
- description: 'Attestation has been used for a different transaction',
323
- recoverySteps: [
324
- '1. This may indicate a replay attack attempt',
325
- '2. Verify the attestation belongs to this transaction',
326
- '3. Provider must create new attestation for this transaction',
327
- ],
328
- retryable: false,
329
- requiresUserAction: true,
330
- securityNotes: [
331
- 'Attestation replay is a security vulnerability',
332
- 'Each transaction needs unique attestation',
333
- ],
334
- },
335
- },
336
- {
337
- pattern: /nonce replay detected/i,
338
- recovery: {
339
- category: 'attestation',
340
- severity: 'critical',
341
- description: 'Message nonce has been reused (replay attack prevention)',
342
- recoverySteps: [
343
- '1. Use NonceManager to generate fresh nonces',
344
- '2. Nonces must be monotonically increasing per sender',
345
- '3. Do not reuse nonces from previous messages',
346
- ],
347
- retryable: false,
348
- requiresUserAction: true,
349
- securityNotes: [
350
- 'Nonce replay is a serious security vulnerability',
351
- 'Indicates potential message replay attack',
352
- ],
353
- },
354
- },
355
- {
356
- pattern: /global nonce tracker limit reached/i,
357
- recovery: {
358
- category: 'attestation',
359
- severity: 'high',
360
- description: 'Nonce tracker memory limit reached (DoS protection)',
361
- recoverySteps: [
362
- '1. This may indicate a DoS attack with many sender addresses',
363
- '2. Clean up old nonce entries if legitimate traffic',
364
- '3. Increase maxTotalEntries limit if needed',
365
- '4. Consider using persistent storage for large deployments',
366
- ],
367
- retryable: false,
368
- requiresUserAction: true,
369
- securityNotes: [
370
- 'Global limit prevents memory exhaustion attacks',
371
- 'Monitor for unusual sender patterns',
372
- ],
373
- },
374
- },
375
-
376
- // Permission Errors
377
- {
378
- pattern: /only available in mock mode/i,
379
- recovery: {
380
- category: 'permission',
381
- severity: 'low',
382
- description: 'Method only available in local development mode',
383
- recoverySteps: [
384
- '1. This method (reset/mintTokens) is for testing only',
385
- '2. In testnet/mainnet, use actual faucets for tokens',
386
- '3. Consider switching to mock mode for local testing',
387
- ],
388
- retryable: false,
389
- requiresUserAction: true,
390
- },
391
- },
392
- {
393
- pattern: /cannot pay yourself/i,
394
- recovery: {
395
- category: 'permission',
396
- severity: 'medium',
397
- description: 'Requester and provider cannot be the same address',
398
- recoverySteps: [
399
- '1. Use different addresses for requester and provider',
400
- '2. Verify provider address is correct',
401
- ],
402
- retryable: false,
403
- requiresUserAction: true,
404
- },
405
- },
406
-
407
- // Timeout Errors
408
- {
409
- pattern: /deadline.*passed/i,
410
- recovery: {
411
- category: 'timeout',
412
- severity: 'high',
413
- description: 'Transaction deadline has expired',
414
- recoverySteps: [
415
- '1. Transaction can no longer be accepted after deadline',
416
- '2. Create a new transaction with longer deadline',
417
- '3. Consider using cancel if still in INITIATED state',
418
- ],
419
- retryable: false,
420
- requiresUserAction: true,
421
- },
422
- },
423
- {
424
- pattern: /dispute window.*active/i,
425
- recovery: {
426
- category: 'timeout',
427
- severity: 'medium',
428
- description: 'Cannot finalize during active dispute window',
429
- recoverySteps: [
430
- '1. Wait for dispute window to expire',
431
- '2. Check transaction.completedAt + disputeWindow',
432
- '3. Default dispute window is 48 hours',
433
- ],
434
- retryable: true,
435
- retryDelayMs: 60000, // Check every minute
436
- maxRetries: 1000,
437
- requiresUserAction: false,
438
- },
439
- },
440
- ];
441
-
442
- /**
443
- * Default recovery info for unclassified errors
444
- */
445
- const DEFAULT_RECOVERY: ErrorRecoveryInfo = {
446
- category: 'unknown',
447
- severity: 'medium',
448
- description: 'An unexpected error occurred',
449
- recoverySteps: [
450
- '1. Check error message for specific details',
451
- '2. Verify all input parameters are correct',
452
- '3. Check network connectivity',
453
- '4. Review SDK documentation',
454
- '5. If persistent, report issue with full error details',
455
- ],
456
- retryable: false,
457
- requiresUserAction: true,
458
- securityNotes: [
459
- 'Do not include sensitive data in error reports',
460
- 'Never expose private keys or secrets',
461
- ],
462
- };
463
-
464
- /**
465
- * ErrorRecoveryGuide - Utility for error classification and recovery guidance
466
- *
467
- * @example
468
- * ```typescript
469
- * try {
470
- * await client.basic.pay({ to: provider, amount: '100' });
471
- * } catch (error) {
472
- * const recovery = ErrorRecoveryGuide.analyze(error);
473
- * console.log('Category:', recovery.category);
474
- * console.log('Steps:', recovery.recoverySteps.join('\n'));
475
- *
476
- * if (recovery.retryable) {
477
- * console.log(`Retrying in ${recovery.retryDelayMs}ms...`);
478
- * }
479
- * }
480
- * ```
481
- */
482
- export class ErrorRecoveryGuide {
483
- /**
484
- * Analyze an error and return recovery guidance
485
- *
486
- * @param error - The error to analyze
487
- * @returns Recovery information with steps and recommendations
488
- */
489
- static analyze(error: unknown): ErrorRecoveryInfo {
490
- const errorMessage = error instanceof Error ? error.message : String(error);
491
-
492
- // Find matching pattern
493
- for (const pattern of ERROR_PATTERNS) {
494
- const matches =
495
- typeof pattern.pattern === 'string'
496
- ? errorMessage.toLowerCase().includes(pattern.pattern.toLowerCase())
497
- : pattern.pattern.test(errorMessage);
498
-
499
- if (matches) {
500
- return {
501
- ...pattern.recovery,
502
- description: `${pattern.recovery.description}: ${errorMessage}`,
503
- };
504
- }
505
- }
506
-
507
- // Return default recovery if no pattern matched
508
- return {
509
- ...DEFAULT_RECOVERY,
510
- description: `${DEFAULT_RECOVERY.description}: ${errorMessage}`,
511
- };
512
- }
513
-
514
- /**
515
- * Get all known error patterns (for documentation/testing)
516
- */
517
- static getKnownPatterns(): string[] {
518
- return ERROR_PATTERNS.map((p) =>
519
- typeof p.pattern === 'string' ? p.pattern : p.pattern.source
520
- );
521
- }
522
-
523
- /**
524
- * Check if an error is retryable
525
- *
526
- * @param error - The error to check
527
- * @returns true if the error can be retried
528
- */
529
- static isRetryable(error: unknown): boolean {
530
- const recovery = this.analyze(error);
531
- return recovery.retryable;
532
- }
533
-
534
- /**
535
- * Get retry parameters for an error
536
- *
537
- * @param error - The error to check
538
- * @returns Retry parameters or null if not retryable
539
- */
540
- static getRetryParams(
541
- error: unknown
542
- ): { delayMs: number; maxRetries: number } | null {
543
- const recovery = this.analyze(error);
544
-
545
- if (!recovery.retryable) {
546
- return null;
547
- }
548
-
549
- return {
550
- delayMs: recovery.retryDelayMs || 1000,
551
- maxRetries: recovery.maxRetries || 3,
552
- };
553
- }
554
-
555
- /**
556
- * Check if an error requires user action
557
- *
558
- * @param error - The error to check
559
- * @returns true if user intervention is needed
560
- */
561
- static requiresUserAction(error: unknown): boolean {
562
- const recovery = this.analyze(error);
563
- return recovery.requiresUserAction;
564
- }
565
-
566
- /**
567
- * Get security notes for an error (if any)
568
- *
569
- * @param error - The error to check
570
- * @returns Security notes or undefined
571
- */
572
- static getSecurityNotes(error: unknown): string[] | undefined {
573
- const recovery = this.analyze(error);
574
- return recovery.securityNotes;
575
- }
576
-
577
- /**
578
- * Format recovery guidance as a string for logging
579
- *
580
- * @param error - The error to analyze
581
- * @returns Formatted recovery guidance string
582
- */
583
- static formatGuidance(error: unknown): string {
584
- const recovery = this.analyze(error);
585
-
586
- let output = `
587
- ================================================================================
588
- ERROR RECOVERY GUIDANCE
589
- ================================================================================
590
- Category: ${recovery.category.toUpperCase()}
591
- Severity: ${recovery.severity.toUpperCase()}
592
- Description: ${recovery.description}
593
-
594
- Recovery Steps:
595
- ${recovery.recoverySteps.join('\n')}
596
-
597
- Retryable: ${recovery.retryable ? 'Yes' : 'No'}`;
598
-
599
- if (recovery.retryable) {
600
- output += `
601
- Retry Delay: ${recovery.retryDelayMs || 1000}ms
602
- Max Retries: ${recovery.maxRetries || 3}`;
603
- }
604
-
605
- output += `
606
- User Action: ${recovery.requiresUserAction ? 'Required' : 'Not Required'}`;
607
-
608
- if (recovery.securityNotes && recovery.securityNotes.length > 0) {
609
- output += `
610
-
611
- Security Notes:
612
- ${recovery.securityNotes.map((n) => ` ! ${n}`).join('\n')}`;
613
- }
614
-
615
- output += '\n================================================================================';
616
-
617
- return output;
618
- }
619
- }
620
-
621
- /**
622
- * Helper function for try-catch with automatic recovery guidance
623
- *
624
- * @example
625
- * ```typescript
626
- * const result = await withRecoveryGuidance(
627
- * () => client.basic.pay({ to: provider, amount: '100' }),
628
- * { logGuidance: true, autoRetry: true }
629
- * );
630
- * ```
631
- */
632
- export async function withRecoveryGuidance<T>(
633
- operation: () => Promise<T>,
634
- options: {
635
- logGuidance?: boolean;
636
- autoRetry?: boolean;
637
- onError?: (error: unknown, recovery: ErrorRecoveryInfo) => void;
638
- } = {}
639
- ): Promise<T> {
640
- const { logGuidance = false, autoRetry = false, onError } = options;
641
-
642
- let attempts = 0;
643
-
644
- do {
645
- try {
646
- return await operation();
647
- } catch (error) {
648
- const recovery = ErrorRecoveryGuide.analyze(error);
649
-
650
- if (onError) {
651
- onError(error, recovery);
652
- }
653
-
654
- if (logGuidance) {
655
- sdkLogger.error(ErrorRecoveryGuide.formatGuidance(error));
656
- }
657
-
658
- if (autoRetry && recovery.retryable) {
659
- const retryParams = ErrorRecoveryGuide.getRetryParams(error);
660
- if (retryParams && attempts < retryParams.maxRetries) {
661
- attempts++;
662
- sdkLogger.info(
663
- `Retrying (${attempts}/${retryParams.maxRetries}) after ${retryParams.delayMs}ms...`
664
- );
665
- await new Promise((resolve) =>
666
- setTimeout(resolve, retryParams.delayMs)
667
- );
668
- continue;
669
- }
670
- }
671
-
672
- throw error;
673
- }
674
- // eslint-disable-next-line no-constant-condition
675
- } while (true);
676
- }