GeminiBot
commited on
Commit
·
1922b30
1
Parent(s):
ed3288f
Fix backend error handling and model list
Browse files- src/duckai.ts +44 -23
src/duckai.ts
CHANGED
|
@@ -127,6 +127,19 @@ export class DuckAI {
|
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
async chat(request: any): Promise<string> {
|
| 131 |
const ua = new UserAgent({ deviceCategory: 'desktop' }).toString();
|
| 132 |
const headers: any = {
|
|
@@ -135,33 +148,41 @@ export class DuckAI {
|
|
| 135 |
"x-vqd-accept": "1"
|
| 136 |
};
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
if (!hashHeader) throw new Error("Missing x-vqd-hash-1");
|
| 142 |
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
-
return llmResponse.trim() || "⚠️ Ошибка потока.";
|
| 163 |
-
} catch (error: any) {
|
| 164 |
-
return `⚠️ Ошибка бэкенда: ${error.message}`;
|
| 165 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
}
|
|
|
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
| 130 |
+
getAvailableModels(): string[] {
|
| 131 |
+
return [
|
| 132 |
+
"gpt-4o-mini",
|
| 133 |
+
"claude-3-haiku-20240307",
|
| 134 |
+
"meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
| 135 |
+
"mistralai/Mistral-Small-24B-Instruct-2501"
|
| 136 |
+
];
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
getRateLimitStatus() {
|
| 140 |
+
return { status: "unknown" };
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
async chat(request: any): Promise<string> {
|
| 144 |
const ua = new UserAgent({ deviceCategory: 'desktop' }).toString();
|
| 145 |
const headers: any = {
|
|
|
|
| 148 |
"x-vqd-accept": "1"
|
| 149 |
};
|
| 150 |
|
| 151 |
+
const statusRes = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", { headers });
|
| 152 |
+
const hashHeader = statusRes.headers.get("x-vqd-hash-1");
|
| 153 |
+
if (!hashHeader) throw new Error("Missing x-vqd-hash-1 - DuckDuckGo might be blocking this IP or Challenge failed.");
|
|
|
|
| 154 |
|
| 155 |
+
const solvedVqd = await this.solveChallenge(hashHeader, ua);
|
| 156 |
|
| 157 |
+
const response = await fetch("https://duckduckgo.com/duckchat/v1/chat", {
|
| 158 |
+
method: "POST",
|
| 159 |
+
headers: { ...headers, "x-vqd-hash-1": solvedVqd, "Content-Type": "application/json" },
|
| 160 |
+
body: JSON.stringify(request)
|
| 161 |
+
});
|
| 162 |
|
| 163 |
+
if (!response.ok) {
|
| 164 |
+
const errorText = await response.text();
|
| 165 |
+
throw new Error(`DuckDuckGo API Error (${response.status}): ${errorText.substring(0, 100)}`);
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
const text = await response.text();
|
| 169 |
+
let llmResponse = "";
|
| 170 |
+
const lines = text.split("\n");
|
| 171 |
+
for (const line of lines) {
|
| 172 |
+
if (line.startsWith("data: ")) {
|
| 173 |
+
try {
|
| 174 |
+
const chunk = line.slice(6);
|
| 175 |
+
if (chunk === "[DONE]") break;
|
| 176 |
+
const json = JSON.parse(chunk);
|
| 177 |
+
if (json.message) llmResponse += json.message;
|
| 178 |
+
} catch (e) {}
|
| 179 |
}
|
|
|
|
|
|
|
|
|
|
| 180 |
}
|
| 181 |
+
|
| 182 |
+
if (!llmResponse) {
|
| 183 |
+
throw new Error("Empty response from DuckDuckGo");
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
return llmResponse.trim();
|
| 187 |
}
|
| 188 |
}
|