GeminiBot
commited on
Commit
·
5ceef9b
1
Parent(s):
aa5670d
Enhance resilience: add jitter and status fetch retries to handle bursts
Browse files- src/duckai.ts +23 -4
src/duckai.ts
CHANGED
|
@@ -84,6 +84,9 @@ export class DuckAI {
|
|
| 84 |
|
| 85 |
console.log(`[${reqId}] [Chat] NEW REQUEST. Parallel active: ${activeRequests}`);
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36';
|
| 88 |
const headers = {
|
| 89 |
"User-Agent": userAgent,
|
|
@@ -93,11 +96,27 @@ export class DuckAI {
|
|
| 93 |
};
|
| 94 |
|
| 95 |
try {
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
if (!hashHeader) throw new Error(
|
| 101 |
|
| 102 |
const solvedVqd = await this.solveChallenge(hashHeader, reqId);
|
| 103 |
|
|
|
|
| 84 |
|
| 85 |
console.log(`[${reqId}] [Chat] NEW REQUEST. Parallel active: ${activeRequests}`);
|
| 86 |
|
| 87 |
+
// Добавляем Jitter (случайную задержку 0-500мс), чтобы не долбить DDG пачкой
|
| 88 |
+
await new Promise(r => setTimeout(resolve => r(resolve), Math.random() * 500));
|
| 89 |
+
|
| 90 |
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36';
|
| 91 |
const headers = {
|
| 92 |
"User-Agent": userAgent,
|
|
|
|
| 96 |
};
|
| 97 |
|
| 98 |
try {
|
| 99 |
+
let hashHeader = null;
|
| 100 |
+
let statusAttempt = 0;
|
| 101 |
+
|
| 102 |
+
// Ретрай для получения статуса (VQD)
|
| 103 |
+
while (!hashHeader && statusAttempt < 3) {
|
| 104 |
+
console.log(`[${reqId}] [Chat] Fetching status (Attempt ${statusAttempt + 1})...`);
|
| 105 |
+
const statusRes = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", { headers });
|
| 106 |
+
hashHeader = statusRes.headers.get("x-vqd-hash-1");
|
| 107 |
+
|
| 108 |
+
if (!hashHeader) {
|
| 109 |
+
if (statusRes.status === 429) {
|
| 110 |
+
console.log(`[${reqId}] [Chat] Status 429 detected. Waiting...`);
|
| 111 |
+
await new Promise(r => setTimeout(resolve => r(resolve), 1000 * (statusAttempt + 1)));
|
| 112 |
+
} else {
|
| 113 |
+
throw new Error(`Status ${statusRes.status}: Missing VQD hash`);
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
statusAttempt++;
|
| 117 |
+
}
|
| 118 |
|
| 119 |
+
if (!hashHeader) throw new Error("Failed to obtain VQD hash after retries");
|
| 120 |
|
| 121 |
const solvedVqd = await this.solveChallenge(hashHeader, reqId);
|
| 122 |
|