GeminiBot commited on
Commit 路
d819405
1
Parent(s): c98a3e7
Fix syntax error in duckai.ts and consolidate all patches
Browse files13 unmodified lines141516171819202122232425262728293031323334353637383940414210 unmodified lines5354555657585 unmodified lines646566676869707172737475767778798081828384858687888990919293949596978 unmodified lines10610710810911011111211311411511611711811912012120 unmodified lines14214314414514614714810 unmodified lines15916016116216316416515 unmodified lines18118218318413 unmodified linesresources: "usable"});const window = dom.window as any;// 袚袥校袘袨袣袗携 协袦校袥携笑袠携Object.defineProperties(window.navigator, {userAgent: { value: ua },platform: { value: 'Win32' },webdriver: { value: false },languages: { value: ['en-US', 'en'] }});window.screen = { width: 1920, height: 1080, availWidth: 1920, availHeight: 1080 };window.chrome = { runtime: {} };// Fix for 'Cannot read properties of null (reading 'contentDocument')'// Immortal DOM + Environment Mocksconst mockWindow = dom.window as any;// Self-reference fixesmockWindow.self = mockWindow;mockWindow.parent = mockWindow;mockWindow.top = mockWindow;// Immortal DOM Strategyconst originalGetElementById = mockWindow.document.getElementById;mockWindow.document.getElementById = function(id: string) {const el = originalGetElementById.call(this, id);10 unmodified linesreturn mockWindow.document.createElement('div');};// Aggressive Patch for iframe contentDocumentObject.defineProperty(mockWindow.HTMLIFrameElement.prototype, 'contentDocument', {get: function() { return mockWindow.document; },5 unmodified linesconfigurable: true});dom.window.top.__DDG_BE_VERSION__ = 1;dom.window.top.__DDG_FE_CHAT_HASH__ = 1;const result = await dom.window.eval(jsScript) as any;if (!result || !result.client_hashes) {throw new Error("Invalid Challenge Response");}result.client_hashes[0] = ua;result.client_hashes = result.client_hashes.map((t: string) => {const hash = createHash('sha256');hash.update(t);return hash.digest('base64');});return btoa(JSON.stringify(result));} catch (e: any) {console.error(`[DuckAI] Challenge Solver Failed: ${e.message}`);// Fallback: Return the original hash (sometimes works if challenge is optional or server is lenient)// or return a dummy base64 json.console.log("[DuckAI] Attempting fallback with original VQD...");return vqdHash; // Try returning the VQD itself as a fallback}}dom.window.top.__DDG_FE_CHAT_HASH__ = 1;const result = await dom.window.eval(jsScript) as any;if (!result || !result.client_hashes) {throw new Error("Invalid Challenge Response");8 unmodified linesreturn btoa(JSON.stringify(result));} catch (e: any) {throw new Error(`Challenge Solver Error: ${e.message}`);}}getAvailableModels(): string[] {return ["gpt-4o-mini","claude-3-haiku-20240307","meta-llama/Llama-3.3-70B-Instruct-Turbo","mistralai/Mistral-Small-24B-Instruct-2501"];}20 unmodified linesconsole.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",10 unmodified lines}const text = await response.text();// console.log(`[DuckAI] Raw response: ${text.substring(0, 200)}...`); // Uncomment for ultra verboselet llmResponse = "";const lines = text.split("\n");15 unmodified linesreturn llmResponse.trim();}}13 unmodified lines141516171819202122232425262710 unmodified lines38394041424344454647484950515253545 unmodified lines606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581598 unmodified lines16816917017117217317417517617717817918018118218318420 unmodified lines20520620720820921021110 unmodified lines22222322422522622715 unmodified lines24324424524613 unmodified linesresources: "usable"});const mockWindow = dom.window as any;// Self-reference fixesmockWindow.self = mockWindow;mockWindow.parent = mockWindow;mockWindow.top = mockWindow;// IMMORTAL DOM Strategyconst originalGetElementById = mockWindow.document.getElementById;mockWindow.document.getElementById = function(id: string) {const el = originalGetElementById.call(this, id);10 unmodified linesreturn mockWindow.document.createElement('div');};// 袚袥校袘袨袣袗携 协袦校袥携笑袠携Object.defineProperties(mockWindow.navigator, {userAgent: { value: ua },platform: { value: 'Win32' },webdriver: { value: false },languages: { value: ['en-US', 'en'] }});mockWindow.screen = { width: 1920, height: 1080, availWidth: 1920, availHeight: 1080 };mockWindow.chrome = { runtime: {} };// Aggressive Patch for iframe contentDocumentObject.defineProperty(mockWindow.HTMLIFrameElement.prototype, 'contentDocument', {get: function() { return mockWindow.document; },5 unmodified linesconfigurable: true});// Also keep the createElement hook just in caseconst originalCreateElement = mockWindow.document.createElement;mockWindow.document.createElement = function(tagName: string) {const element = originalCreateElement.call(this, tagName);if (tagName.toLowerCase() === 'iframe') {try {Object.defineProperty(element, 'contentDocument', {get: () => mockWindow.document,configurable: true});Object.defineProperty(element, 'contentWindow', {get: () => mockWindow,configurable: true});} catch (e) {}}return element;};// Robust Canvas Mockconst originalGetContext = mockWindow.HTMLCanvasElement.prototype.getContext;mockWindow.HTMLCanvasElement.prototype.getContext = function (type: string, options?: any) {try {const ctx = originalGetContext.call(this, type, options);if (ctx) return ctx;} catch (e) {}return {canvas: this,fillRect: () => {},clearRect: () => {},getImageData: (x: number, y: number, w: number, h: number) => ({data: new Uint8ClampedArray(w * h * 4),width: w,height: h}),putImageData: () => {},createImageData: () => ({ data: new Uint8ClampedArray(4) }),setTransform: () => {},drawImage: () => {},save: () => {},restore: () => {},beginPath: () => {},moveTo: () => {},lineTo: () => {},closePath: () => {},stroke: () => {},translate: () => {},scale: () => {},rotate: () => {},arc: () => {},fill: () => {},measureText: () => ({ width: 0, actualBoundingBoxAscent: 0, actualBoundingBoxDescent: 0 }),transform: () => {},rect: () => {},clip: () => {},createLinearGradient: () => ({ addColorStop: () => {} }),createRadialGradient: () => ({ addColorStop: () => {} }),createPattern: () => ({}),bezierCurveTo: () => {},quadraticCurveTo: () => {},fillText: () => {},strokeText: () => {},globalAlpha: 1,globalCompositeOperation: 'source-over',fillStyle: '#000000',strokeStyle: '#000000',lineWidth: 1,lineCap: 'butt',lineJoin: 'miter',miterLimit: 10,shadowOffsetX: 0,shadowOffsetY: 0,shadowBlur: 0,shadowColor: 'rgba(0, 0, 0, 0)',font: '10px sans-serif',textAlign: 'start',textBaseline: 'alphabetic'};} as any;const originalToDataURL = mockWindow.HTMLCanvasElement.prototype.toDataURL;mockWindow.HTMLCanvasElement.prototype.toDataURL = function(type?: string, quality?: any) {try {const result = originalToDataURL.call(this, type, quality);if (result && result !== "data:,") return result;} catch(e) {}return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";};mockWindow.top.__DDG_BE_VERSION__ = 1;mockWindow.top.__DDG_FE_CHAT_HASH__ = 1;const result = await mockWindow.eval(jsScript) as any;if (!result || !result.client_hashes) {throw new Error("Invalid Challenge Response");8 unmodified linesreturn btoa(JSON.stringify(result));} catch (e: any) {console.error(`[DuckAI] Challenge Solver Failed: ${e.message}`);console.log("[DuckAI] Attempting fallback with original VQD...");return vqdHash;}}getAvailableModels(): string[] {return ["gpt-4o-mini","gpt-5-mini","openai/gpt-oss-120b"];}20 unmodified linesconsole.log("[DuckAI] Solving challenge...");const solvedVqd = await this.solveChallenge(hashHeader, ua);console.log("[DuckAI] Challenge solved (or fallback used).");const response = await fetch("https://duckduckgo.com/duckchat/v1/chat", {method: "POST",10 unmodified lines}const text = await response.text();let llmResponse = "";const lines = text.split("\n");15 unmodified linesreturn llmResponse.trim();}}
src/duckai.ts+112-50