Merge pull request #4 from Brioch/main
Browse filesfix(api): fix error: Missing VQD headers: vqd=false, hash=true
Files changed (2)
- README.md+5-4
- src/duckai.ts+50-54
71 unmodified lines7273747576777879808171 unmodified lines### Supported Models- `gpt-4o-mini` (Default)- `o3-mini`- `claude-3-haiku-20240307`- `meta-llama/Llama-3.3-70B-Instruct-Turbo`- `mistralai/Mixtral-8x7B-Instruct-v0.1`### Features71 unmodified lines727374757677787980818271 unmodified lines### Supported Models- `gpt-4o-mini` (Default)- `gpt-5-mini`- `claude-3-5-haiku-latest`- `meta-llama/Llama-4-Scout-17B-16E-Instruct`- `mistralai/Mistral-Small-24B-Instruct-2501`- `openai/gpt-oss-120b`### Features
6 unmodified lines789101112167 unmodified lines18018118218318418521 unmodified lines20720820921021121221321421521621721821922022122222322422522622722822917 unmodified lines24724824925025125225325425525625725825926026126226326415 unmodified lines28028128228328428528628728828929029129229329462 unmodified lines35735835936036136236336436536636736836937037137237337437537637715 unmodified lines39339439539639739839940040140240340440540640762 unmodified lines4704714724734744754764774784796 unmodified linesVQDResponse,DuckAIRequest,} from "./types";// Rate limiting tracking with sliding windowinterface RateLimitInfo {167 unmodified lines}}private async getVQD(userAgent: string): Promise<VQDResponse> {const response = await fetch("https://duckduckgo.com/duckchat/v1/status", {headers: {21 unmodified lines);}const vqd = response.headers.get("x-Vqd-4");const hashHeader = response.headers.get("x-Vqd-hash-1");if (!vqd || !hashHeader) {throw new Error(`Missing VQD headers: vqd=${!!vqd}, hash=${!!hashHeader}`);}let hash: string;try {hash = atob(hashHeader);} catch (e) {throw new Error(`Failed to decode VQD hash: ${e}`);}return { vqd, hash };}private async hashClientHashes(clientHashes: string[]): Promise<string[]> {17 unmodified linesconst userAgent = new UserAgent().toString();const vqd = await this.getVQD(userAgent);const { window } = new JSDOM(`<html><body><script>window.hash = ${vqd.hash}</script></body></html>`,{ runScripts: "dangerously" });const hash = (window as any).hash;if (!hash || !hash.client_hashes || !Array.isArray(hash.client_hashes)) {throw new Error(`Invalid hash structure: ${JSON.stringify(hash)}`);}const clientHashes = await this.hashClientHashes(hash.client_hashes);// Update rate limit tracking BEFORE making the requestconst now = Date.now();this.rateLimitInfo.requestTimestamps.push(now);15 unmodified lines"sec-fetch-mode": "cors","sec-fetch-site": "same-origin","x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300","x-vqd-4": vqd.vqd,"User-Agent": userAgent,"x-vqd-hash-1": btoa(JSON.stringify({server_hashes: hash.server_hashes,client_hashes: clientHashes,signals: hash.signal,})),},referrer: "https://duckduckgo.com/",referrerPolicy: "origin",62 unmodified linesawait this.waitIfNeeded();const userAgent = new UserAgent().toString();const vqd = await this.getVQD(userAgent);const { window } = new JSDOM(`<html><body><script>window.hash = ${vqd.hash}</script></body></html>`,{ runScripts: "dangerously" });const hash = (window as any).hash;if (!hash || !hash.client_hashes || !Array.isArray(hash.client_hashes)) {throw new Error(`Invalid hash structure: ${JSON.stringify(hash)}`);}const clientHashes = await this.hashClientHashes(hash.client_hashes);// Update rate limit tracking BEFORE making the requestconst now = Date.now();this.rateLimitInfo.requestTimestamps.push(now);15 unmodified lines"sec-fetch-mode": "cors","sec-fetch-site": "same-origin","x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300","x-vqd-4": vqd.vqd,"User-Agent": userAgent,"x-vqd-hash-1": btoa(JSON.stringify({server_hashes: hash.server_hashes,client_hashes: clientHashes,signals: hash.signal,})),},referrer: "https://duckduckgo.com/",referrerPolicy: "origin",62 unmodified linesgetAvailableModels(): string[] {return ["gpt-4o-mini","o3-mini","claude-3-haiku-20240307","meta-llama/Llama-3.3-70B-Instruct-Turbo","mistralai/Mistral-Small-24B-Instruct-2501",];}}6 unmodified lines7891011121314167 unmodified lines18218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422521 unmodified lines24724824925025125225325425525625725825926026126226317 unmodified lines28128228328428528615 unmodified lines30230330430530630730830962 unmodified lines37237337437537637737837915 unmodified lines39539639739839940040140262 unmodified lines4654664674684694704714724734744756 unmodified linesVQDResponse,DuckAIRequest,} from "./types";import { createHash } from "node:crypto";import { Buffer } from "node:buffer";// Rate limiting tracking with sliding windowinterface RateLimitInfo {167 unmodified lines}}private async getEncodedVqdHash(vqdHash: string): Promise<string> {const jsScript = Buffer.from(vqdHash, 'base64').toString('utf-8');const dom = new JSDOM(`<iframe id="jsa" sandbox="allow-scripts allow-same-origin" srcdoc="<!DOCTYPE html><html><head><meta http-equiv="Content-Security-Policy"; content="default-src 'none'; script-src 'unsafe-inline'"></head><body></body></html>" style="position: absolute; left: -9999px; top: -9999px;"></iframe>`,{ runScripts: 'dangerously' });dom.window.top.__DDG_BE_VERSION__ = 1;dom.window.top.__DDG_FE_CHAT_HASH__ = 1;const jsa = dom.window.top.document.querySelector('#jsa') as HTMLIFrameElement;const contentDoc = jsa.contentDocument || jsa.contentWindow!.document;const meta = contentDoc.createElement('meta');meta.setAttribute('http-equiv', 'Content-Security-Policy');meta.setAttribute('content', "default-src 'none'; script-src 'unsafe-inline';");contentDoc.head.appendChild(meta);const result = await dom.window.eval(jsScript) as {client_hashes: string[];[key: string]: any;};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';result.client_hashes = result.client_hashes.map((t) => {const hash = createHash('sha256');hash.update(t);return hash.digest('base64');});return btoa(JSON.stringify(result));}private async getVQD(userAgent: string): Promise<VQDResponse> {const response = await fetch("https://duckduckgo.com/duckchat/v1/status", {headers: {21 unmodified lines);}const hashHeader = response.headers.get("x-Vqd-hash-1");if (!hashHeader) {throw new Error(`Missing VQD headers: hash=${!!hashHeader}`);}const encodedHash = await this.getEncodedVqdHash(hashHeader);return { hash: encodedHash };}private async hashClientHashes(clientHashes: string[]): Promise<string[]> {17 unmodified linesconst userAgent = new UserAgent().toString();const vqd = await this.getVQD(userAgent);// Update rate limit tracking BEFORE making the requestconst now = Date.now();this.rateLimitInfo.requestTimestamps.push(now);15 unmodified lines"sec-fetch-mode": "cors","sec-fetch-site": "same-origin","x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300","User-Agent": userAgent,"x-vqd-hash-1": vqd.hash,},referrer: "https://duckduckgo.com/",referrerPolicy: "origin",62 unmodified linesawait this.waitIfNeeded();const userAgent = new UserAgent().toString();const vqd = await this.getVQD(userAgent);// Update rate limit tracking BEFORE making the requestconst now = Date.now();this.rateLimitInfo.requestTimestamps.push(now);15 unmodified lines"sec-fetch-mode": "cors","sec-fetch-site": "same-origin","x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300","User-Agent": userAgent,"x-vqd-hash-1": vqd.hash,},referrer: "https://duckduckgo.com/",referrerPolicy: "origin",62 unmodified linesgetAvailableModels(): string[] {return ["gpt-4o-mini","gpt-5-mini","claude-3-5-haiku-latest","meta-llama/Llama-4-Scout-17B-16E-Instruct","mistralai/Mistral-Small-24B-Instruct-2501","openai/gpt-oss-120b"];}}
README.md+5-4
src/duckai.ts+50-54