GeminiBot commited on
Commit ·
a118953
1
Parent(s): 4bca23b
Fix iframe crash in JSDOM and add verbose logging
Browse filesFiles changed (2)
- src/duckai.ts+33
- src/server.ts+9-5
26 unmodified lines272829303132115 unmodified lines1481491501511521531541551561571581 unmodified line1601611621631641651661671681691701718 unmodified lines18018118218318418526 unmodified lineswindow.screen = { width: 1920, height: 1080, availWidth: 1920, availHeight: 1080 };window.chrome = { runtime: {} };// Robust Canvas Mockconst originalGetContext = window.HTMLCanvasElement.prototype.getContext;window.HTMLCanvasElement.prototype.getContext = function (type: string, options?: any) {115 unmodified lines"x-vqd-accept": "1"};const statusRes = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", { headers });const hashHeader = statusRes.headers.get("x-vqd-hash-1");if (!hashHeader) throw new Error("Missing x-vqd-hash-1 - DuckDuckGo might be blocking this IP or Challenge failed.");const solvedVqd = await this.solveChallenge(hashHeader, ua);const response = await fetch("https://duckduckgo.com/duckchat/v1/chat", {method: "POST",1 unmodified linebody: JSON.stringify(request)});if (!response.ok) {const errorText = await response.text();throw new Error(`DuckDuckGo API Error (${response.status}): ${errorText.substring(0, 100)}`);}const text = await response.text();let llmResponse = "";const lines = text.split("\n");for (const line of lines) {8 unmodified lines}if (!llmResponse) {throw new Error("Empty response from DuckDuckGo");}26 unmodified lines2728293031323334353637383940414243444546474849505152115 unmodified lines1681691701711721731741751761771781791801811821831841851 unmodified line1871881891901911921931941951961971981992002012022038 unmodified lines21221321421521621721826 unmodified lineswindow.screen = { width: 1920, height: 1080, availWidth: 1920, availHeight: 1080 };window.chrome = { runtime: {} };// Fix for 'Cannot read properties of null (reading 'contentDocument')'// Some challenge scripts create an iframe and immediately access contentDocumentconst originalCreateElement = window.document.createElement;window.document.createElement = function(tagName: string) {const element = originalCreateElement.call(this, tagName);if (tagName.toLowerCase() === 'iframe') {try {Object.defineProperty(element, 'contentDocument', {get: () => window.document, // Return main document as fallback or a dummyconfigurable: true});Object.defineProperty(element, 'contentWindow', {get: () => window,configurable: true});} catch (e) {}}return element;};// Robust Canvas Mockconst originalGetContext = window.HTMLCanvasElement.prototype.getContext;window.HTMLCanvasElement.prototype.getContext = function (type: string, options?: any) {115 unmodified lines"x-vqd-accept": "1"};console.log("[DuckAI] Fetching status...");const statusRes = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", { headers });console.log(`[DuckAI] Status response: ${statusRes.status}`);const hashHeader = statusRes.headers.get("x-vqd-hash-1");console.log(`[DuckAI] x-vqd-hash-1: ${hashHeader ? "FOUND" : "MISSING"}`);if (!hashHeader) throw new Error("Missing x-vqd-hash-1 - DuckDuckGo might be blocking this IP or Challenge failed.");console.log("[DuckAI] Solving challenge...");const solvedVqd = await this.solveChallenge(hashHeader, ua);console.log("[DuckAI] Challenge solved.");const response = await fetch("https://duckduckgo.com/duckchat/v1/chat", {method: "POST",1 unmodified linebody: JSON.stringify(request)});console.log(`[DuckAI] Chat response status: ${response.status}`);if (!response.ok) {const errorText = await response.text();console.log(`[DuckAI] Error body: ${errorText}`);throw new Error(`DuckDuckGo API Error (${response.status}): ${errorText.substring(0, 100)}`);}const text = await response.text();// console.log(`[DuckAI] Raw response: ${text.substring(0, 200)}...`); // Uncomment for ultra verboselet llmResponse = "";const lines = text.split("\n");for (const line of lines) {8 unmodified lines}if (!llmResponse) {console.log("[DuckAI] Warning: Empty LLM response extracted.");throw new Error("Empty response from DuckDuckGo");}
89 unmodified lines9091929394959697989910010110210310489 unmodified linesreq.on("data", (chunk) => { body += chunk; });req.on("end", async () => {try {const jsonBody = JSON.parse(body);const lastMsg = jsonBody.messages?.[jsonBody.messages.length - 1]?.content || "";log(`REQ: ${lastMsg.substring(0, 50)}...`);const completion = await openAIService.createChatCompletion(jsonBody);sendJSON(200, completion);} catch (error: any) {log(`ERR: ${error.message}`);sendJSON(500, { error: error.message });}});return;89 unmodified lines9091929394959697989910010110210310410510610710889 unmodified linesreq.on("data", (chunk) => { body += chunk; });req.on("end", async () => {try {// Log full request for debugginglog(`INCOMING REQUEST:\n${body}`);const jsonBody = JSON.parse(body);const completion = await openAIService.createChatCompletion(jsonBody);// Log success summarylog(`SUCCESS: Sent response for ${jsonBody.model}`);sendJSON(200, completion);} catch (error: any) {log(`ERROR: ${error.message}\nSTACK: ${error.stack}`);sendJSON(500, { error: error.message, details: error.stack });}});return;
src/duckai.ts+33
src/server.ts+9-5