@yaoyuanchao/dingtalk 1.4.7 → 1.4.8

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/api.ts +36 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaoyuanchao/dingtalk",
3
- "version": "1.4.7",
3
+ "version": "1.4.8",
4
4
  "type": "module",
5
5
  "description": "DingTalk channel plugin for Clawdbot with Stream Mode support",
6
6
  "license": "MIT",
package/src/api.ts CHANGED
@@ -70,6 +70,29 @@ function httpGetBuffer(url: string, headers?: Record<string, string>): Promise<B
70
70
  });
71
71
  }
72
72
 
73
+ /** Retry wrapper for async functions */
74
+ async function withRetry<T>(
75
+ fn: () => Promise<T>,
76
+ maxRetries: number = 3,
77
+ delayMs: number = 1000,
78
+ backoffMultiplier: number = 2,
79
+ ): Promise<T> {
80
+ let lastError: Error | undefined;
81
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
82
+ try {
83
+ return await fn();
84
+ } catch (err) {
85
+ lastError = err instanceof Error ? err : new Error(String(err));
86
+ if (attempt < maxRetries) {
87
+ const delay = delayMs * Math.pow(backoffMultiplier, attempt - 1);
88
+ console.log(`[dingtalk] Retry ${attempt}/${maxRetries} after ${delay}ms: ${lastError.message}`);
89
+ await new Promise(resolve => setTimeout(resolve, delay));
90
+ }
91
+ }
92
+ }
93
+ throw lastError;
94
+ }
95
+
73
96
  export async function getDingTalkAccessToken(clientId: string, clientSecret: string): Promise<string> {
74
97
  const cached = tokenCache.get(clientId);
75
98
  if (cached && cached.expiresAt > Date.now() + 60_000) {
@@ -337,9 +360,14 @@ export async function downloadPicture(
337
360
  return { error: response.errmsg || "Download failed" };
338
361
  }
339
362
 
340
- // If response has a file URL, download it
363
+ // If response has a file URL, download it with retry
341
364
  if (response.downloadUrl) {
342
- const imageBuffer = await httpGetBuffer(response.downloadUrl);
365
+ const imageBuffer = await withRetry(
366
+ () => httpGetBuffer(response.downloadUrl),
367
+ 3, // maxRetries
368
+ 1000, // initial delay 1s
369
+ 2, // backoff multiplier
370
+ );
343
371
 
344
372
  // Convert to base64
345
373
  const base64 = imageBuffer.toString('base64');
@@ -404,7 +432,12 @@ export async function downloadMediaFile(
404
432
  }
405
433
 
406
434
  if (response.downloadUrl) {
407
- const mediaBuffer = await httpGetBuffer(response.downloadUrl);
435
+ const mediaBuffer = await withRetry(
436
+ () => httpGetBuffer(response.downloadUrl),
437
+ 3, // maxRetries
438
+ 1000, // initial delay 1s
439
+ 2, // backoff multiplier
440
+ );
408
441
 
409
442
  if (!fs.existsSync(TEMP_DIR)) {
410
443
  fs.mkdirSync(TEMP_DIR, { recursive: true });