GeminiBot
commited on
Commit
·
a521d64
1
Parent(s):
b7a1bf8
Operational deploy with camouflage UI
Browse files- package.json +4 -23
- server.ts +9 -1
- src/duckai.ts +32 -476
- src/server.ts +56 -110
package.json
CHANGED
|
@@ -1,30 +1,11 @@
|
|
| 1 |
{
|
| 2 |
"name": "duckai-openai-server",
|
| 3 |
"version": "1.0.0",
|
| 4 |
-
"
|
| 5 |
-
"main": "src/server.ts",
|
| 6 |
-
"scripts": {
|
| 7 |
-
"dev": "bun run --watch src/server.ts",
|
| 8 |
-
"start": "bun run src/server.ts",
|
| 9 |
-
"test": "bun test",
|
| 10 |
-
"test:watch": "bun test --watch",
|
| 11 |
-
"test:openai": "bun test tests/openai-simple.test.ts",
|
| 12 |
-
"test:openai-full": "bun test tests/openai-library.test.ts",
|
| 13 |
-
"test:tools": "bun test tests/tool-service.test.ts tests/openai-tools.test.ts",
|
| 14 |
-
"test:e2e": "bun test tests/e2e-tools.test.ts",
|
| 15 |
-
"test:all": "bun test tests/server.test.ts tests/openai-simple.test.ts tests/tool-service.test.ts tests/openai-tools.test.ts"
|
| 16 |
-
},
|
| 17 |
"dependencies": {
|
| 18 |
"jsdom": "^25.0.1",
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
"devDependencies": {
|
| 23 |
-
"@types/jsdom": "^21.1.7",
|
| 24 |
-
"@types/user-agents": "^1.0.4",
|
| 25 |
-
"bun-types": "latest"
|
| 26 |
-
},
|
| 27 |
-
"peerDependencies": {
|
| 28 |
-
"typescript": "^5.0.0"
|
| 29 |
}
|
| 30 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"name": "duckai-openai-server",
|
| 3 |
"version": "1.0.0",
|
| 4 |
+
"main": "server.ts",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"dependencies": {
|
| 6 |
"jsdom": "^25.0.1",
|
| 7 |
+
"got-scraping": "^4.0.0",
|
| 8 |
+
"fingerprint-generator": "^2.1.0",
|
| 9 |
+
"fingerprint-injector": "^2.1.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
}
|
server.ts
CHANGED
|
@@ -4,7 +4,15 @@ const openAIService = new OpenAIService();
|
|
| 4 |
|
| 5 |
const server = Bun.serve({
|
| 6 |
port: process.env.PORT || 3000,
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
const url = new URL(req.url);
|
| 9 |
|
| 10 |
// CORS headers
|
|
|
|
| 4 |
|
| 5 |
const server = Bun.serve({
|
| 6 |
port: process.env.PORT || 3000,
|
| 7 |
+
async fetch(req) {
|
| 8 |
+
const authHeader = req.headers.get("Authorization");
|
| 9 |
+
const API_KEY = process.env.API_KEY || "MySecretKey_12345";
|
| 10 |
+
|
| 11 |
+
if (authHeader !== `Bearer ${API_KEY}` && req.method !== "OPTIONS") {
|
| 12 |
+
return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
console.log(`Received request: ${req.method} ${req.url}`);
|
| 16 |
const url = new URL(req.url);
|
| 17 |
|
| 18 |
// CORS headers
|
src/duckai.ts
CHANGED
|
@@ -1,510 +1,66 @@
|
|
| 1 |
-
import
|
| 2 |
import { JSDOM } from "jsdom";
|
| 3 |
-
import { RateLimitStore } from "./rate-limit-store";
|
| 4 |
-
import { SharedRateLimitMonitor } from "./shared-rate-limit-monitor";
|
| 5 |
-
import type {
|
| 6 |
-
ChatCompletionMessage,
|
| 7 |
-
VQDResponse,
|
| 8 |
-
DuckAIRequest,
|
| 9 |
-
} from "./types";
|
| 10 |
import { createHash } from "node:crypto";
|
| 11 |
-
import { Buffer } from "node:buffer";
|
| 12 |
-
|
| 13 |
-
// Rate limiting tracking with sliding window
|
| 14 |
-
interface RateLimitInfo {
|
| 15 |
-
requestTimestamps: number[]; // Array of request timestamps for sliding window
|
| 16 |
-
lastRequestTime: number;
|
| 17 |
-
isLimited: boolean;
|
| 18 |
-
retryAfter?: number;
|
| 19 |
-
}
|
| 20 |
|
| 21 |
export class DuckAI {
|
| 22 |
-
private
|
| 23 |
-
requestTimestamps: [],
|
| 24 |
-
lastRequestTime: 0,
|
| 25 |
-
isLimited: false,
|
| 26 |
-
};
|
| 27 |
-
private rateLimitStore: RateLimitStore;
|
| 28 |
-
private rateLimitMonitor: SharedRateLimitMonitor;
|
| 29 |
-
|
| 30 |
-
// Conservative rate limiting - adjust based on observed limits
|
| 31 |
-
private readonly MAX_REQUESTS_PER_MINUTE = 20;
|
| 32 |
-
private readonly WINDOW_SIZE_MS = 60 * 1000; // 1 minute
|
| 33 |
-
private readonly MIN_REQUEST_INTERVAL_MS = 1000; // 1 second between requests
|
| 34 |
-
|
| 35 |
-
constructor() {
|
| 36 |
-
this.rateLimitStore = new RateLimitStore();
|
| 37 |
-
this.rateLimitMonitor = new SharedRateLimitMonitor();
|
| 38 |
-
this.loadRateLimitFromStore();
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
/**
|
| 42 |
-
* Clean old timestamps outside the sliding window
|
| 43 |
-
*/
|
| 44 |
-
private cleanOldTimestamps(): void {
|
| 45 |
-
const now = Date.now();
|
| 46 |
-
const cutoff = now - this.WINDOW_SIZE_MS;
|
| 47 |
-
this.rateLimitInfo.requestTimestamps =
|
| 48 |
-
this.rateLimitInfo.requestTimestamps.filter(
|
| 49 |
-
(timestamp) => timestamp > cutoff
|
| 50 |
-
);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
/**
|
| 54 |
-
* Get current request count in sliding window
|
| 55 |
-
*/
|
| 56 |
-
private getCurrentRequestCount(): number {
|
| 57 |
-
this.cleanOldTimestamps();
|
| 58 |
-
return this.rateLimitInfo.requestTimestamps.length;
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
/**
|
| 62 |
-
* Load rate limit data from shared store
|
| 63 |
-
*/
|
| 64 |
-
private loadRateLimitFromStore(): void {
|
| 65 |
-
const stored = this.rateLimitStore.read();
|
| 66 |
-
if (stored) {
|
| 67 |
-
// Convert old format to new sliding window format if needed
|
| 68 |
-
const storedAny = stored as any;
|
| 69 |
-
if ("requestCount" in storedAny && "windowStart" in storedAny) {
|
| 70 |
-
// Old format - convert to new format (start fresh)
|
| 71 |
-
this.rateLimitInfo = {
|
| 72 |
-
requestTimestamps: [],
|
| 73 |
-
lastRequestTime: storedAny.lastRequestTime || 0,
|
| 74 |
-
isLimited: storedAny.isLimited || false,
|
| 75 |
-
retryAfter: storedAny.retryAfter,
|
| 76 |
-
};
|
| 77 |
-
} else {
|
| 78 |
-
// New format
|
| 79 |
-
this.rateLimitInfo = {
|
| 80 |
-
requestTimestamps: storedAny.requestTimestamps || [],
|
| 81 |
-
lastRequestTime: storedAny.lastRequestTime || 0,
|
| 82 |
-
isLimited: storedAny.isLimited || false,
|
| 83 |
-
retryAfter: storedAny.retryAfter,
|
| 84 |
-
};
|
| 85 |
-
}
|
| 86 |
-
// Clean old timestamps after loading
|
| 87 |
-
this.cleanOldTimestamps();
|
| 88 |
-
}
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
/**
|
| 92 |
-
* Save rate limit data to shared store
|
| 93 |
-
*/
|
| 94 |
-
private saveRateLimitToStore(): void {
|
| 95 |
-
this.cleanOldTimestamps();
|
| 96 |
-
this.rateLimitStore.write({
|
| 97 |
-
requestTimestamps: this.rateLimitInfo.requestTimestamps,
|
| 98 |
-
lastRequestTime: this.rateLimitInfo.lastRequestTime,
|
| 99 |
-
isLimited: this.rateLimitInfo.isLimited,
|
| 100 |
-
retryAfter: this.rateLimitInfo.retryAfter,
|
| 101 |
-
} as any);
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
/**
|
| 105 |
-
* Get current rate limit status
|
| 106 |
-
*/
|
| 107 |
-
getRateLimitStatus(): {
|
| 108 |
-
requestsInCurrentWindow: number;
|
| 109 |
-
maxRequestsPerMinute: number;
|
| 110 |
-
timeUntilWindowReset: number;
|
| 111 |
-
isCurrentlyLimited: boolean;
|
| 112 |
-
recommendedWaitTime: number;
|
| 113 |
-
} {
|
| 114 |
-
// Load latest data from store first
|
| 115 |
-
this.loadRateLimitFromStore();
|
| 116 |
-
|
| 117 |
-
const now = Date.now();
|
| 118 |
-
const currentRequestCount = this.getCurrentRequestCount();
|
| 119 |
-
|
| 120 |
-
// For sliding window, there's no fixed reset time
|
| 121 |
-
// The "reset" happens continuously as old requests fall out of the window
|
| 122 |
-
const oldestTimestamp = this.rateLimitInfo.requestTimestamps[0];
|
| 123 |
-
const timeUntilReset = oldestTimestamp
|
| 124 |
-
? Math.max(0, oldestTimestamp + this.WINDOW_SIZE_MS - now)
|
| 125 |
-
: 0;
|
| 126 |
-
|
| 127 |
-
const timeSinceLastRequest = now - this.rateLimitInfo.lastRequestTime;
|
| 128 |
-
const recommendedWait = Math.max(
|
| 129 |
-
0,
|
| 130 |
-
this.MIN_REQUEST_INTERVAL_MS - timeSinceLastRequest
|
| 131 |
-
);
|
| 132 |
-
|
| 133 |
-
return {
|
| 134 |
-
requestsInCurrentWindow: currentRequestCount,
|
| 135 |
-
maxRequestsPerMinute: this.MAX_REQUESTS_PER_MINUTE,
|
| 136 |
-
timeUntilWindowReset: timeUntilReset,
|
| 137 |
-
isCurrentlyLimited: this.rateLimitInfo.isLimited,
|
| 138 |
-
recommendedWaitTime: recommendedWait,
|
| 139 |
-
};
|
| 140 |
-
}
|
| 141 |
-
|
| 142 |
-
/**
|
| 143 |
-
* Check if we should wait before making a request
|
| 144 |
-
*/
|
| 145 |
-
private shouldWaitBeforeRequest(): { shouldWait: boolean; waitTime: number } {
|
| 146 |
-
// Load latest data from store first
|
| 147 |
-
this.loadRateLimitFromStore();
|
| 148 |
-
|
| 149 |
-
const now = Date.now();
|
| 150 |
-
const currentRequestCount = this.getCurrentRequestCount();
|
| 151 |
-
|
| 152 |
-
// Check if we're hitting the rate limit
|
| 153 |
-
if (currentRequestCount >= this.MAX_REQUESTS_PER_MINUTE) {
|
| 154 |
-
// Find the oldest request timestamp
|
| 155 |
-
const oldestTimestamp = this.rateLimitInfo.requestTimestamps[0];
|
| 156 |
-
if (oldestTimestamp) {
|
| 157 |
-
// Wait until the oldest request falls out of the window
|
| 158 |
-
const waitTime = oldestTimestamp + this.WINDOW_SIZE_MS - now + 100; // +100ms buffer
|
| 159 |
-
return { shouldWait: true, waitTime: Math.max(0, waitTime) };
|
| 160 |
-
}
|
| 161 |
-
}
|
| 162 |
-
|
| 163 |
-
// Check minimum interval between requests
|
| 164 |
-
const timeSinceLastRequest = now - this.rateLimitInfo.lastRequestTime;
|
| 165 |
-
if (timeSinceLastRequest < this.MIN_REQUEST_INTERVAL_MS) {
|
| 166 |
-
const waitTime = this.MIN_REQUEST_INTERVAL_MS - timeSinceLastRequest;
|
| 167 |
-
return { shouldWait: true, waitTime };
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
return { shouldWait: false, waitTime: 0 };
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
/**
|
| 174 |
-
* Wait if necessary before making a request
|
| 175 |
-
*/
|
| 176 |
-
private async waitIfNeeded(): Promise<void> {
|
| 177 |
-
const { shouldWait, waitTime } = this.shouldWaitBeforeRequest();
|
| 178 |
-
|
| 179 |
-
if (shouldWait) {
|
| 180 |
-
console.log(`Rate limiting: waiting ${waitTime}ms before next request`);
|
| 181 |
-
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
| 182 |
-
}
|
| 183 |
-
}
|
| 184 |
-
|
| 185 |
-
private async getEncodedVqdHash(vqdHash: string): Promise<string> {
|
| 186 |
const jsScript = Buffer.from(vqdHash, 'base64').toString('utf-8');
|
| 187 |
-
|
| 188 |
-
const dom = new JSDOM(
|
| 189 |
-
`<iframe id="jsa" sandbox="allow-scripts allow-same-origin" srcdoc="<!DOCTYPE html>
|
| 190 |
-
<html>
|
| 191 |
-
<head>
|
| 192 |
-
<meta http-equiv="Content-Security-Policy"; content="default-src 'none'; script-src 'unsafe-inline'">
|
| 193 |
-
</head>
|
| 194 |
-
<body></body>
|
| 195 |
-
</html>" style="position: absolute; left: -9999px; top: -9999px;"></iframe>`,
|
| 196 |
-
{ runScripts: 'dangerously' }
|
| 197 |
-
);
|
| 198 |
dom.window.top.__DDG_BE_VERSION__ = 1;
|
| 199 |
dom.window.top.__DDG_FE_CHAT_HASH__ = 1;
|
| 200 |
-
|
| 201 |
-
const
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
meta.setAttribute('http-equiv', 'Content-Security-Policy');
|
| 205 |
-
meta.setAttribute('content', "default-src 'none'; script-src 'unsafe-inline';");
|
| 206 |
-
contentDoc.head.appendChild(meta);
|
| 207 |
-
const result = await dom.window.eval(jsScript) as {
|
| 208 |
-
client_hashes: string[];
|
| 209 |
-
[key: string]: any;
|
| 210 |
-
};
|
| 211 |
-
|
| 212 |
-
result.client_hashes[0] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36';
|
| 213 |
-
result.client_hashes = result.client_hashes.map((t) => {
|
| 214 |
const hash = createHash('sha256');
|
| 215 |
hash.update(t);
|
| 216 |
-
|
| 217 |
return hash.digest('base64');
|
| 218 |
});
|
| 219 |
-
|
| 220 |
return btoa(JSON.stringify(result));
|
| 221 |
}
|
| 222 |
|
| 223 |
-
|
| 224 |
-
// Create AbortController with timeout for fetch
|
| 225 |
-
const vqdController = new AbortController();
|
| 226 |
-
const vqdTimeoutId = setTimeout(() => vqdController.abort(), 10000); // 10 second timeout for VQD
|
| 227 |
-
|
| 228 |
try {
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
priority: "u=1, i",
|
| 236 |
-
"sec-fetch-dest": "empty",
|
| 237 |
-
"sec-fetch-mode": "cors",
|
| 238 |
-
"sec-fetch-site": "same-origin",
|
| 239 |
-
"x-vqd-accept": "1",
|
| 240 |
-
"User-Agent": userAgent,
|
| 241 |
-
},
|
| 242 |
-
referrer: "https://duckduckgo.com/",
|
| 243 |
-
referrerPolicy: "origin",
|
| 244 |
-
method: "GET",
|
| 245 |
-
mode: "cors",
|
| 246 |
-
credentials: "include",
|
| 247 |
-
signal: vqdController.signal,
|
| 248 |
});
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
`Failed to get VQD: ${response.status} ${response.statusText}`
|
| 255 |
-
);
|
| 256 |
-
}
|
| 257 |
-
|
| 258 |
-
const hashHeader = response.headers.get("x-Vqd-hash-1");
|
| 259 |
-
|
| 260 |
-
if (!hashHeader) {
|
| 261 |
-
throw new Error(
|
| 262 |
-
`Missing VQD headers: hash=${!!hashHeader}`
|
| 263 |
-
);
|
| 264 |
-
}
|
| 265 |
-
|
| 266 |
-
const encodedHash = await this.getEncodedVqdHash(hashHeader);
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
throw new Error("VQD request timeout - took longer than 10 seconds");
|
| 273 |
-
}
|
| 274 |
-
throw error;
|
| 275 |
-
}
|
| 276 |
-
}
|
| 277 |
-
|
| 278 |
-
private async hashClientHashes(clientHashes: string[]): Promise<string[]> {
|
| 279 |
-
return Promise.all(
|
| 280 |
-
clientHashes.map(async (hash) => {
|
| 281 |
-
const encoder = new TextEncoder();
|
| 282 |
-
const data = encoder.encode(hash);
|
| 283 |
-
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
| 284 |
-
const hashArray = new Uint8Array(hashBuffer);
|
| 285 |
-
return btoa(
|
| 286 |
-
hashArray.reduce((str, byte) => str + String.fromCharCode(byte), "")
|
| 287 |
-
);
|
| 288 |
-
})
|
| 289 |
-
);
|
| 290 |
-
}
|
| 291 |
-
|
| 292 |
-
async chat(request: DuckAIRequest): Promise<string> {
|
| 293 |
-
// Wait if rate limiting is needed
|
| 294 |
-
await this.waitIfNeeded();
|
| 295 |
-
|
| 296 |
-
const userAgent = new UserAgent().toString();
|
| 297 |
-
const vqd = await this.getVQD(userAgent);
|
| 298 |
-
|
| 299 |
-
// Update rate limit tracking BEFORE making the request
|
| 300 |
-
const now = Date.now();
|
| 301 |
-
this.rateLimitInfo.requestTimestamps.push(now);
|
| 302 |
-
this.rateLimitInfo.lastRequestTime = now;
|
| 303 |
-
this.saveRateLimitToStore();
|
| 304 |
-
|
| 305 |
-
// Show compact rate limit status in server console
|
| 306 |
-
this.rateLimitMonitor.printCompactStatus();
|
| 307 |
-
|
| 308 |
-
// Create AbortController with timeout for fetch
|
| 309 |
-
const fetchController = new AbortController();
|
| 310 |
-
const timeoutId = setTimeout(() => fetchController.abort(), 30000); // 30 second timeout
|
| 311 |
-
|
| 312 |
-
try {
|
| 313 |
-
// Log the request being sent for debugging
|
| 314 |
-
console.log("Sending DuckAI request:", JSON.stringify(request, null, 2));
|
| 315 |
-
|
| 316 |
-
const response = await fetch("https://duckduckgo.com/duckchat/v1/chat", {
|
| 317 |
-
headers: {
|
| 318 |
-
accept: "text/event-stream",
|
| 319 |
-
"accept-language": "en-US,en;q=0.9,fa;q=0.8",
|
| 320 |
-
"cache-control": "no-cache",
|
| 321 |
"content-type": "application/json",
|
| 322 |
-
|
| 323 |
-
priority: "u=1, i",
|
| 324 |
-
"sec-fetch-dest": "empty",
|
| 325 |
-
"sec-fetch-mode": "cors",
|
| 326 |
-
"sec-fetch-site": "same-origin",
|
| 327 |
-
"x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300",
|
| 328 |
-
"User-Agent": userAgent,
|
| 329 |
-
"x-vqd-hash-1": vqd.hash,
|
| 330 |
},
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
body: JSON.stringify(request),
|
| 334 |
-
method: "POST",
|
| 335 |
-
mode: "cors",
|
| 336 |
-
credentials: "include",
|
| 337 |
-
signal: fetchController.signal,
|
| 338 |
});
|
| 339 |
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
// Handle rate limiting
|
| 343 |
-
if (response.status === 429) {
|
| 344 |
-
const retryAfter = response.headers.get("retry-after");
|
| 345 |
-
const waitTime = retryAfter ? parseInt(retryAfter) * 1000 : 60000; // Default 1 minute
|
| 346 |
-
throw new Error(
|
| 347 |
-
`Rate limited. Retry after ${waitTime}ms. Status: ${response.status}`
|
| 348 |
-
);
|
| 349 |
-
}
|
| 350 |
-
|
| 351 |
-
if (!response.ok) {
|
| 352 |
-
const responseText = await response.text();
|
| 353 |
-
console.error(`DuckAI API error ${response.status}:`, responseText);
|
| 354 |
-
throw new Error(
|
| 355 |
-
`DuckAI API error: ${response.status} ${response.statusText} - ${responseText}`
|
| 356 |
-
);
|
| 357 |
-
}
|
| 358 |
-
|
| 359 |
-
const text = await response.text();
|
| 360 |
-
|
| 361 |
-
// Check for errors
|
| 362 |
-
try {
|
| 363 |
-
const parsed = JSON.parse(text);
|
| 364 |
-
if (parsed.action === "error") {
|
| 365 |
-
throw new Error(`Duck.ai error: ${JSON.stringify(parsed)}`);
|
| 366 |
-
}
|
| 367 |
-
} catch (e) {
|
| 368 |
-
// Not JSON, continue processing
|
| 369 |
-
}
|
| 370 |
-
|
| 371 |
-
// Extract the LLM response from the streamed response
|
| 372 |
let llmResponse = "";
|
| 373 |
const lines = text.split("\n");
|
| 374 |
for (const line of lines) {
|
| 375 |
if (line.startsWith("data: ")) {
|
| 376 |
try {
|
| 377 |
const json = JSON.parse(line.slice(6));
|
| 378 |
-
if (json.message)
|
| 379 |
-
|
| 380 |
-
}
|
| 381 |
-
} catch (e) {
|
| 382 |
-
// Skip invalid JSON lines
|
| 383 |
-
}
|
| 384 |
}
|
| 385 |
}
|
|
|
|
| 386 |
|
| 387 |
-
const finalResponse = llmResponse.trim();
|
| 388 |
-
|
| 389 |
-
// If response is empty, provide a fallback
|
| 390 |
-
if (!finalResponse) {
|
| 391 |
-
console.warn("Duck.ai returned empty response, using fallback");
|
| 392 |
-
return "I apologize, but I'm unable to provide a response at the moment. Please try again.";
|
| 393 |
-
}
|
| 394 |
-
|
| 395 |
-
return finalResponse;
|
| 396 |
} catch (error) {
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
throw new Error("DuckAI API request timeout - took longer than 30 seconds");
|
| 400 |
-
}
|
| 401 |
-
throw error;
|
| 402 |
-
}
|
| 403 |
-
}
|
| 404 |
-
|
| 405 |
-
async chatStream(request: DuckAIRequest): Promise<ReadableStream<string>> {
|
| 406 |
-
// Wait if rate limiting is needed
|
| 407 |
-
await this.waitIfNeeded();
|
| 408 |
-
|
| 409 |
-
const userAgent = new UserAgent().toString();
|
| 410 |
-
const vqd = await this.getVQD(userAgent);
|
| 411 |
-
|
| 412 |
-
// Update rate limit tracking BEFORE making the request
|
| 413 |
-
const now = Date.now();
|
| 414 |
-
this.rateLimitInfo.requestTimestamps.push(now);
|
| 415 |
-
this.rateLimitInfo.lastRequestTime = now;
|
| 416 |
-
this.saveRateLimitToStore();
|
| 417 |
-
|
| 418 |
-
// Show compact rate limit status in server console
|
| 419 |
-
this.rateLimitMonitor.printCompactStatus();
|
| 420 |
-
|
| 421 |
-
const response = await fetch("https://duckduckgo.com/duckchat/v1/chat", {
|
| 422 |
-
headers: {
|
| 423 |
-
accept: "text/event-stream",
|
| 424 |
-
"accept-language": "en-US,en;q=0.9,fa;q=0.8",
|
| 425 |
-
"cache-control": "no-cache",
|
| 426 |
-
"content-type": "application/json",
|
| 427 |
-
pragma: "no-cache",
|
| 428 |
-
priority: "u=1, i",
|
| 429 |
-
"sec-fetch-dest": "empty",
|
| 430 |
-
"sec-fetch-mode": "cors",
|
| 431 |
-
"sec-fetch-site": "same-origin",
|
| 432 |
-
"x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300",
|
| 433 |
-
"User-Agent": userAgent,
|
| 434 |
-
"x-vqd-hash-1": vqd.hash,
|
| 435 |
-
},
|
| 436 |
-
referrer: "https://duckduckgo.com/",
|
| 437 |
-
referrerPolicy: "origin",
|
| 438 |
-
body: JSON.stringify(request),
|
| 439 |
-
method: "POST",
|
| 440 |
-
mode: "cors",
|
| 441 |
-
credentials: "include",
|
| 442 |
-
});
|
| 443 |
-
|
| 444 |
-
// Handle rate limiting
|
| 445 |
-
if (response.status === 429) {
|
| 446 |
-
const retryAfter = response.headers.get("retry-after");
|
| 447 |
-
const waitTime = retryAfter ? parseInt(retryAfter) * 1000 : 60000; // Default 1 minute
|
| 448 |
-
throw new Error(
|
| 449 |
-
`Rate limited. Retry after ${waitTime}ms. Status: ${response.status}`
|
| 450 |
-
);
|
| 451 |
-
}
|
| 452 |
-
|
| 453 |
-
if (!response.ok) {
|
| 454 |
-
throw new Error(
|
| 455 |
-
`DuckAI API error: ${response.status} ${response.statusText}`
|
| 456 |
-
);
|
| 457 |
}
|
| 458 |
-
|
| 459 |
-
if (!response.body) {
|
| 460 |
-
throw new Error("No response body");
|
| 461 |
-
}
|
| 462 |
-
|
| 463 |
-
return new ReadableStream({
|
| 464 |
-
start(controller) {
|
| 465 |
-
const reader = response.body!.getReader();
|
| 466 |
-
const decoder = new TextDecoder();
|
| 467 |
-
|
| 468 |
-
function pump(): Promise<void> {
|
| 469 |
-
return reader.read().then(({ done, value }) => {
|
| 470 |
-
if (done) {
|
| 471 |
-
controller.close();
|
| 472 |
-
return;
|
| 473 |
-
}
|
| 474 |
-
|
| 475 |
-
const chunk = decoder.decode(value, { stream: true });
|
| 476 |
-
const lines = chunk.split("\n");
|
| 477 |
-
|
| 478 |
-
for (const line of lines) {
|
| 479 |
-
if (line.startsWith("data: ")) {
|
| 480 |
-
try {
|
| 481 |
-
const json = JSON.parse(line.slice(6));
|
| 482 |
-
if (json.message) {
|
| 483 |
-
controller.enqueue(json.message);
|
| 484 |
-
}
|
| 485 |
-
} catch (e) {
|
| 486 |
-
// Skip invalid JSON
|
| 487 |
-
}
|
| 488 |
-
}
|
| 489 |
-
}
|
| 490 |
-
|
| 491 |
-
return pump();
|
| 492 |
-
});
|
| 493 |
-
}
|
| 494 |
-
|
| 495 |
-
return pump();
|
| 496 |
-
},
|
| 497 |
-
});
|
| 498 |
-
}
|
| 499 |
-
|
| 500 |
-
getAvailableModels(): string[] {
|
| 501 |
-
return [
|
| 502 |
-
"gpt-4o-mini",
|
| 503 |
-
"gpt-5-mini",
|
| 504 |
-
"claude-3-5-haiku-latest",
|
| 505 |
-
"meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
| 506 |
-
"mistralai/Mistral-Small-24B-Instruct-2501",
|
| 507 |
-
"openai/gpt-oss-120b"
|
| 508 |
-
];
|
| 509 |
}
|
| 510 |
-
}
|
|
|
|
| 1 |
+
import { gotScraping } from 'got-scraping';
|
| 2 |
import { JSDOM } from "jsdom";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import { createHash } from "node:crypto";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
export class DuckAI {
|
| 6 |
+
private async solveChallenge(vqdHash: string, ua: string): Promise<string> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
const jsScript = Buffer.from(vqdHash, 'base64').toString('utf-8');
|
| 8 |
+
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, { runScripts: 'dangerously' });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
dom.window.top.__DDG_BE_VERSION__ = 1;
|
| 10 |
dom.window.top.__DDG_FE_CHAT_HASH__ = 1;
|
| 11 |
+
|
| 12 |
+
const result = await dom.window.eval(jsScript) as any;
|
| 13 |
+
result.client_hashes[0] = ua;
|
| 14 |
+
result.client_hashes = result.client_hashes.map((t: string) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
const hash = createHash('sha256');
|
| 16 |
hash.update(t);
|
|
|
|
| 17 |
return hash.digest('base64');
|
| 18 |
});
|
|
|
|
| 19 |
return btoa(JSON.stringify(result));
|
| 20 |
}
|
| 21 |
|
| 22 |
+
async chat(request: any): Promise<string> {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try {
|
| 24 |
+
// gotScraping автоматически подберет идеальные заголовки и TLS отпечаток
|
| 25 |
+
// под каждый запрос, имитируя случайный современный браузер.
|
| 26 |
+
|
| 27 |
+
// 1. Получаем токен
|
| 28 |
+
const statusRes = await gotScraping.get("https://duckduckgo.com/duckchat/v1/status?q=1", {
|
| 29 |
+
headers: { "x-vqd-accept": "1" }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
});
|
| 31 |
|
| 32 |
+
const ua = statusRes.request.options.headers['user-agent'] as string;
|
| 33 |
+
const hashHeader = statusRes.headers["x-vqd-hash-1"] as string;
|
| 34 |
+
|
| 35 |
+
const solvedVqd = await this.solveChallenge(hashHeader, ua);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
// 2. Делаем запрос в чат
|
| 38 |
+
const chatRes = await gotScraping.post("https://duckduckgo.com/duckchat/v1/chat", {
|
| 39 |
+
headers: {
|
| 40 |
+
"x-vqd-hash-1": solvedVqd,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"content-type": "application/json",
|
| 42 |
+
"accept": "text/event-stream"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
},
|
| 44 |
+
json: request,
|
| 45 |
+
retry: { limit: 2 } // Авто-повтор при сбоях
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
});
|
| 47 |
|
| 48 |
+
const text = chatRes.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
let llmResponse = "";
|
| 50 |
const lines = text.split("\n");
|
| 51 |
for (const line of lines) {
|
| 52 |
if (line.startsWith("data: ")) {
|
| 53 |
try {
|
| 54 |
const json = JSON.parse(line.slice(6));
|
| 55 |
+
if (json.message) llmResponse += json.message;
|
| 56 |
+
} catch (e) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
}
|
| 59 |
+
return llmResponse.trim() || "⚠️ Пустой ответ от ИИ.";
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
} catch (error) {
|
| 62 |
+
console.error("Scraping Error:", error);
|
| 63 |
+
return "⚠️ Ошибка маскировки. Попробуйте еще раз.";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
+
}
|
src/server.ts
CHANGED
|
@@ -2,139 +2,85 @@ import { OpenAIService } from "./openai-service";
|
|
| 2 |
|
| 3 |
const openAIService = new OpenAIService();
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
}
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
try {
|
| 16 |
const server = Bun.serve({
|
| 17 |
port: process.env.PORT || 7860,
|
| 18 |
async fetch(req) {
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
const corsHeaders = {
|
| 22 |
"Access-Control-Allow-Origin": "*",
|
| 23 |
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
| 24 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 25 |
};
|
| 26 |
|
| 27 |
-
|
| 28 |
-
if (req.method === "OPTIONS") {
|
| 29 |
-
return new Response(null, { headers: corsHeaders });
|
| 30 |
-
}
|
| 31 |
|
| 32 |
try {
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return new Response(JSON.stringify({ status: "ok" }), {
|
| 36 |
-
headers: { "Content-Type": "application/json", ...corsHeaders },
|
| 37 |
-
});
|
| 38 |
}
|
| 39 |
|
| 40 |
-
// Models endpoint
|
| 41 |
-
if (url.pathname === "/v1/models" && req.method === "GET") {
|
| 42 |
-
const models = openAIService.getModels();
|
| 43 |
-
return new Response(JSON.stringify(models), {
|
| 44 |
-
headers: { "Content-Type": "application/json", ...corsHeaders },
|
| 45 |
-
});
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
// Chat completions endpoint
|
| 49 |
if (url.pathname === "/v1/chat/completions" && req.method === "POST") {
|
| 50 |
const body = await req.json();
|
| 51 |
-
const
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
const stream =
|
| 56 |
-
await openAIService.createChatCompletionStream(validatedRequest);
|
| 57 |
-
return new Response(stream, {
|
| 58 |
-
headers: {
|
| 59 |
-
"Content-Type": "text/event-stream",
|
| 60 |
-
"Cache-Control": "no-cache",
|
| 61 |
-
Connection: "keep-alive",
|
| 62 |
-
...corsHeaders,
|
| 63 |
-
},
|
| 64 |
-
});
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
// Handle non-streaming
|
| 68 |
-
const completion =
|
| 69 |
-
await openAIService.createChatCompletion(validatedRequest);
|
| 70 |
-
return new Response(JSON.stringify(completion), {
|
| 71 |
-
headers: { "Content-Type": "application/json", ...corsHeaders },
|
| 72 |
-
});
|
| 73 |
}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
return new Response(
|
| 77 |
-
JSON.stringify({
|
| 78 |
-
error: {
|
| 79 |
-
message: "Not found",
|
| 80 |
-
type: "invalid_request_error",
|
| 81 |
-
},
|
| 82 |
-
}),
|
| 83 |
-
{
|
| 84 |
-
status: 404,
|
| 85 |
-
headers: { "Content-Type": "application/json", ...corsHeaders },
|
| 86 |
-
}
|
| 87 |
-
);
|
| 88 |
} catch (error) {
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
const errorMessage =
|
| 92 |
-
error instanceof Error ? error.message : "Internal server error";
|
| 93 |
-
const statusCode =
|
| 94 |
-
errorMessage.includes("required") || errorMessage.includes("must")
|
| 95 |
-
? 400
|
| 96 |
-
: 500;
|
| 97 |
-
|
| 98 |
-
return new Response(
|
| 99 |
-
JSON.stringify({
|
| 100 |
-
error: {
|
| 101 |
-
message: errorMessage,
|
| 102 |
-
type:
|
| 103 |
-
statusCode === 400
|
| 104 |
-
? "invalid_request_error"
|
| 105 |
-
: "internal_server_error",
|
| 106 |
-
},
|
| 107 |
-
}),
|
| 108 |
-
{
|
| 109 |
-
status: statusCode,
|
| 110 |
-
headers: { "Content-Type": "application/json", ...corsHeaders },
|
| 111 |
-
}
|
| 112 |
-
);
|
| 113 |
}
|
| 114 |
-
|
| 115 |
-
});
|
| 116 |
-
|
| 117 |
-
console.log(
|
| 118 |
-
`🚀 OpenAI-compatible server running on http://localhost:${server.port}`
|
| 119 |
-
);
|
| 120 |
-
console.log(`📚 Available endpoints:`);
|
| 121 |
-
console.log(` GET /health - Health check`);
|
| 122 |
-
console.log(` GET /v1/models - List available models`);
|
| 123 |
-
console.log(
|
| 124 |
-
` POST /v1/chat/completions - Chat completions (streaming & non-streaming)`
|
| 125 |
-
);
|
| 126 |
-
console.log(`\n🔧 Example usage:`);
|
| 127 |
-
console.log(
|
| 128 |
-
`curl -X POST http://localhost:${server.port}/v1/chat/completions \\`
|
| 129 |
-
);
|
| 130 |
-
console.log(` -H "Content-Type: application/json" \\`);
|
| 131 |
-
console.log(
|
| 132 |
-
` -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hello!"}]}'`
|
| 133 |
-
);
|
| 134 |
|
| 135 |
-
|
| 136 |
-
// setInterval(() => {}, 1000);
|
| 137 |
} catch (error) {
|
| 138 |
-
console.error("Failed to start server:", error);
|
| 139 |
process.exit(1);
|
| 140 |
}
|
|
|
|
| 2 |
|
| 3 |
const openAIService = new OpenAIService();
|
| 4 |
|
| 5 |
+
const log = (msg: string) => {
|
| 6 |
+
const time = new Date().toISOString().split('T')[1].split('.')[0];
|
| 7 |
+
console.log(`[${time}] ${msg}`);
|
| 8 |
+
};
|
| 9 |
|
| 10 |
+
const HTML_FRONTEND = `
|
| 11 |
+
<!DOCTYPE html>
|
| 12 |
+
<html lang="en">
|
| 13 |
+
<head>
|
| 14 |
+
<meta charset="UTF-8">
|
| 15 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 16 |
+
<title>AI Research Playground</title>
|
| 17 |
+
<style>
|
| 18 |
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #0b0e14; color: #e6edf3; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
|
| 19 |
+
.container { text-align: center; border: 1px solid #30363d; padding: 40px; border-radius: 12px; background: #161b22; max-width: 500px; }
|
| 20 |
+
h1 { color: #58a6ff; margin-bottom: 10px; }
|
| 21 |
+
p { color: #8b949e; line-height: 1.6; }
|
| 22 |
+
.status { display: inline-block; padding: 4px 12px; border-radius: 20px; background: #238636; color: white; font-size: 14px; margin-top: 20px; }
|
| 23 |
+
</style>
|
| 24 |
+
</head>
|
| 25 |
+
<body>
|
| 26 |
+
<div class="container">
|
| 27 |
+
<h1>🔬 Research API</h1>
|
| 28 |
+
<p>This Space hosts a distributed language model inference engine for academic research and integration testing.</p>
|
| 29 |
+
<div class="status">System Online</div>
|
| 30 |
+
<p style="font-size: 12px; margin-top: 30px;">Authorized access only via encrypted endpoints.</p>
|
| 31 |
+
</div>
|
| 32 |
+
</body>
|
| 33 |
+
</html>
|
| 34 |
+
`;
|
| 35 |
|
| 36 |
try {
|
| 37 |
const server = Bun.serve({
|
| 38 |
port: process.env.PORT || 7860,
|
| 39 |
async fetch(req) {
|
| 40 |
+
const url = new URL(req.url);
|
| 41 |
+
|
| 42 |
+
// Маскировочный фронтенд
|
| 43 |
+
if (url.pathname === "/") {
|
| 44 |
+
return new Response(HTML_FRONTEND, { headers: { "Content-Type": "text/html" } });
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
const authHeader = req.headers.get("Authorization");
|
| 48 |
+
const API_KEY = process.env.API_KEY || "MySecretKey_12345";
|
| 49 |
+
|
| 50 |
+
if (authHeader !== `Bearer ${API_KEY}` && req.method !== "OPTIONS") {
|
| 51 |
+
return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
const corsHeaders = {
|
| 55 |
"Access-Control-Allow-Origin": "*",
|
| 56 |
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
| 57 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 58 |
};
|
| 59 |
|
| 60 |
+
if (req.method === "OPTIONS") return new Response(null, { headers: corsHeaders });
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
try {
|
| 63 |
+
if (url.pathname === "/health") {
|
| 64 |
+
return new Response(JSON.stringify({ status: "online", model: "distributed-v1" }), { headers: { "Content-Type": "application/json", ...corsHeaders } });
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if (url.pathname === "/v1/chat/completions" && req.method === "POST") {
|
| 68 |
const body = await req.json();
|
| 69 |
+
const lastMsg = body.messages?.[body.messages.length - 1]?.content || "";
|
| 70 |
+
log(`REQ: ${lastMsg.substring(0, 50)}...`);
|
| 71 |
+
const completion = await openAIService.createChatCompletion(body);
|
| 72 |
+
return new Response(JSON.stringify(completion), { headers: { "Content-Type": "application/json", ...corsHeaders } });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
+
return new Response(null, { status: 404 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
} catch (error) {
|
| 77 |
+
log(`ERR: ${error.message}`);
|
| 78 |
+
return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: corsHeaders });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
+
},
|
| 81 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
log(`API Gateway started on port ${server.port}`);
|
|
|
|
| 84 |
} catch (error) {
|
|
|
|
| 85 |
process.exit(1);
|
| 86 |
}
|