Spaces:
Paused
Paused
Update src/index.ts
Browse files- src/index.ts +49 -30
src/index.ts
CHANGED
|
@@ -1,21 +1,11 @@
|
|
| 1 |
import { Hono } from 'hono';
|
| 2 |
import { serve } from '@hono/node-server';
|
| 3 |
import { cors } from 'hono/cors';
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
//
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
// Initialize browser
|
| 10 |
-
async function initBrowser() {
|
| 11 |
-
if (!browser) {
|
| 12 |
-
browser = await chromium.launch({
|
| 13 |
-
headless: true,
|
| 14 |
-
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
|
| 15 |
-
});
|
| 16 |
-
}
|
| 17 |
-
return browser;
|
| 18 |
-
}
|
| 19 |
|
| 20 |
// Fake headers
|
| 21 |
const FAKE_HEADERS = {
|
|
@@ -27,9 +17,37 @@ const FAKE_HEADERS = {
|
|
| 27 |
'x-vqd-accept': '1',
|
| 28 |
};
|
| 29 |
|
| 30 |
-
//
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
// Clean headers
|
| 35 |
function cleanHeaders(headers: Record<string, string>) {
|
|
@@ -55,13 +73,14 @@ function cleanHeaders(headers: Record<string, string>) {
|
|
| 55 |
// Get token
|
| 56 |
async function playwrightRequestToken(clientIp?: string) {
|
| 57 |
const browser = await initBrowser();
|
| 58 |
-
const
|
|
|
|
| 59 |
|
| 60 |
try {
|
| 61 |
const url = 'https://duckduckgo.com/duckchat/v1/status';
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
await page.route('**/*', async (route
|
| 65 |
if (route.request().url() === url) {
|
| 66 |
const headers = cleanHeaders({
|
| 67 |
...FAKE_HEADERS,
|
|
@@ -69,7 +88,6 @@ async function playwrightRequestToken(clientIp?: string) {
|
|
| 69 |
});
|
| 70 |
|
| 71 |
await route.continue({
|
| 72 |
-
method: 'GET',
|
| 73 |
headers,
|
| 74 |
});
|
| 75 |
} else {
|
|
@@ -86,11 +104,11 @@ async function playwrightRequestToken(clientIp?: string) {
|
|
| 86 |
throw new Error('Request Token failed!');
|
| 87 |
}
|
| 88 |
|
| 89 |
-
responseBody = await response.json();
|
| 90 |
-
await
|
| 91 |
return responseBody?.vqd;
|
| 92 |
} catch (error) {
|
| 93 |
-
await
|
| 94 |
console.error('Request Token Error:', error);
|
| 95 |
throw error;
|
| 96 |
}
|
|
@@ -112,7 +130,8 @@ async function playwrightCreateCompletion(
|
|
| 112 |
retryCount = 0
|
| 113 |
): Promise<ApiResponse> {
|
| 114 |
const browser = await initBrowser();
|
| 115 |
-
const
|
|
|
|
| 116 |
|
| 117 |
try {
|
| 118 |
const token = await playwrightRequestToken(clientIp);
|
|
@@ -125,7 +144,7 @@ async function playwrightCreateCompletion(
|
|
| 125 |
let responseBody: any;
|
| 126 |
let status: number;
|
| 127 |
|
| 128 |
-
await page.route('**/*', async (route
|
| 129 |
if (route.request().url() === url) {
|
| 130 |
const headers = cleanHeaders({
|
| 131 |
...FAKE_HEADERS,
|
|
@@ -156,7 +175,7 @@ async function playwrightCreateCompletion(
|
|
| 156 |
if (!response.ok()) {
|
| 157 |
if (status === 418 && retryCount < MAX_RETRY_COUNT) {
|
| 158 |
console.warn('Rate limit hit (418), retrying...');
|
| 159 |
-
await
|
| 160 |
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
|
| 161 |
return playwrightCreateCompletion(model, content, returnStream, clientIp, retryCount + 1);
|
| 162 |
}
|
|
@@ -164,7 +183,7 @@ async function playwrightCreateCompletion(
|
|
| 164 |
}
|
| 165 |
|
| 166 |
responseBody = await response.body();
|
| 167 |
-
await
|
| 168 |
|
| 169 |
if (!returnStream) {
|
| 170 |
const text = responseBody.toString();
|
|
@@ -197,7 +216,7 @@ async function playwrightCreateCompletion(
|
|
| 197 |
body: responseBody,
|
| 198 |
};
|
| 199 |
} catch (error: any) {
|
| 200 |
-
await
|
| 201 |
console.error('Create Completion Error:', error.message);
|
| 202 |
throw error;
|
| 203 |
}
|
|
|
|
| 1 |
import { Hono } from 'hono';
|
| 2 |
import { serve } from '@hono/node-server';
|
| 3 |
import { cors } from 'hono/cors';
|
| 4 |
+
import * as playwright from 'playwright';
|
| 5 |
|
| 6 |
+
// Config
|
| 7 |
+
const MAX_RETRY_COUNT = 3;
|
| 8 |
+
const RETRY_DELAY = 5000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
// Fake headers
|
| 11 |
const FAKE_HEADERS = {
|
|
|
|
| 17 |
'x-vqd-accept': '1',
|
| 18 |
};
|
| 19 |
|
| 20 |
+
// Browser instance
|
| 21 |
+
let browser: playwright.Browser | null = null;
|
| 22 |
+
|
| 23 |
+
// Ensure browser is installed and launched
|
| 24 |
+
async function initBrowser() {
|
| 25 |
+
if (!browser) {
|
| 26 |
+
try {
|
| 27 |
+
// First try to launch
|
| 28 |
+
browser = await playwright.chromium.launch({
|
| 29 |
+
headless: true,
|
| 30 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
|
| 31 |
+
});
|
| 32 |
+
} catch (error) {
|
| 33 |
+
console.log('Installing browsers first...');
|
| 34 |
+
// If that fails, install the browsers
|
| 35 |
+
const { execSync } = require('child_process');
|
| 36 |
+
try {
|
| 37 |
+
execSync('npx playwright install chromium', { stdio: 'inherit' });
|
| 38 |
+
// Try launching again after installation
|
| 39 |
+
browser = await playwright.chromium.launch({
|
| 40 |
+
headless: true,
|
| 41 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
|
| 42 |
+
});
|
| 43 |
+
} catch (installError) {
|
| 44 |
+
console.error('Failed to install or launch browser:', installError);
|
| 45 |
+
throw installError;
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
return browser;
|
| 50 |
+
}
|
| 51 |
|
| 52 |
// Clean headers
|
| 53 |
function cleanHeaders(headers: Record<string, string>) {
|
|
|
|
| 73 |
// Get token
|
| 74 |
async function playwrightRequestToken(clientIp?: string) {
|
| 75 |
const browser = await initBrowser();
|
| 76 |
+
const context = await browser.newContext();
|
| 77 |
+
const page = await context.newPage();
|
| 78 |
|
| 79 |
try {
|
| 80 |
const url = 'https://duckduckgo.com/duckchat/v1/status';
|
| 81 |
+
|
| 82 |
+
// Set up route handler
|
| 83 |
+
await page.route('**/*', async (route) => {
|
| 84 |
if (route.request().url() === url) {
|
| 85 |
const headers = cleanHeaders({
|
| 86 |
...FAKE_HEADERS,
|
|
|
|
| 88 |
});
|
| 89 |
|
| 90 |
await route.continue({
|
|
|
|
| 91 |
headers,
|
| 92 |
});
|
| 93 |
} else {
|
|
|
|
| 104 |
throw new Error('Request Token failed!');
|
| 105 |
}
|
| 106 |
|
| 107 |
+
const responseBody = await response.json();
|
| 108 |
+
await context.close();
|
| 109 |
return responseBody?.vqd;
|
| 110 |
} catch (error) {
|
| 111 |
+
await context.close();
|
| 112 |
console.error('Request Token Error:', error);
|
| 113 |
throw error;
|
| 114 |
}
|
|
|
|
| 130 |
retryCount = 0
|
| 131 |
): Promise<ApiResponse> {
|
| 132 |
const browser = await initBrowser();
|
| 133 |
+
const context = await browser.newContext();
|
| 134 |
+
const page = await context.newPage();
|
| 135 |
|
| 136 |
try {
|
| 137 |
const token = await playwrightRequestToken(clientIp);
|
|
|
|
| 144 |
let responseBody: any;
|
| 145 |
let status: number;
|
| 146 |
|
| 147 |
+
await page.route('**/*', async (route) => {
|
| 148 |
if (route.request().url() === url) {
|
| 149 |
const headers = cleanHeaders({
|
| 150 |
...FAKE_HEADERS,
|
|
|
|
| 175 |
if (!response.ok()) {
|
| 176 |
if (status === 418 && retryCount < MAX_RETRY_COUNT) {
|
| 177 |
console.warn('Rate limit hit (418), retrying...');
|
| 178 |
+
await context.close();
|
| 179 |
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
|
| 180 |
return playwrightCreateCompletion(model, content, returnStream, clientIp, retryCount + 1);
|
| 181 |
}
|
|
|
|
| 183 |
}
|
| 184 |
|
| 185 |
responseBody = await response.body();
|
| 186 |
+
await context.close();
|
| 187 |
|
| 188 |
if (!returnStream) {
|
| 189 |
const text = responseBody.toString();
|
|
|
|
| 216 |
body: responseBody,
|
| 217 |
};
|
| 218 |
} catch (error: any) {
|
| 219 |
+
await context.close();
|
| 220 |
console.error('Create Completion Error:', error.message);
|
| 221 |
throw error;
|
| 222 |
}
|