mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-13 04:33:42 +08:00
feat: ddg插件支持单独配置http代理
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { decode } from "html-entities";
|
||||
import { convert as htmlToText } from "html-to-text";
|
||||
import { Tool } from "langchain/tools";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
const DDG_PROXY_URL = process.env.DDG_PROXY_URL;
|
||||
|
||||
const SEARCH_REGEX =
|
||||
/DDG\.pageLayout\.load\('d',(\[.+\])\);DDG\.duckbar\.load\('images'/;
|
||||
@@ -323,9 +326,15 @@ async function search(
|
||||
// cdrexp: 'b'
|
||||
}),
|
||||
};
|
||||
let proxyAgent;
|
||||
if (DDG_PROXY_URL) {
|
||||
const { HttpsProxyAgent } = require("https-proxy-agent");
|
||||
proxyAgent = new HttpsProxyAgent(DDG_PROXY_URL);
|
||||
}
|
||||
|
||||
const response = await fetch(
|
||||
`https://links.duckduckgo.com/d.js?${queryString(queryObject)}`,
|
||||
{ agent: proxyAgent },
|
||||
);
|
||||
const data = await response.text();
|
||||
|
||||
@@ -455,8 +464,14 @@ function queryString(query: Record<string, string>) {
|
||||
|
||||
async function getVQD(query: string, ia = "web") {
|
||||
try {
|
||||
let proxyAgent;
|
||||
if (DDG_PROXY_URL) {
|
||||
const { HttpsProxyAgent } = require("https-proxy-agent");
|
||||
proxyAgent = new HttpsProxyAgent(DDG_PROXY_URL);
|
||||
}
|
||||
const response = await fetch(
|
||||
`https://duckduckgo.com/?${queryString({ q: query, ia })}`,
|
||||
{ agent: proxyAgent },
|
||||
);
|
||||
const data = await response.text();
|
||||
return VQD_REGEX.exec(data)![1];
|
||||
|
||||
@@ -181,7 +181,8 @@ async function handle(req: NextRequest) {
|
||||
description: bingSearchTool.description,
|
||||
func: async (input: string) => bingSearchTool.call(input),
|
||||
});
|
||||
} else if (process.env.SERPAPI_API_KEY) {
|
||||
}
|
||||
if (process.env.SERPAPI_API_KEY) {
|
||||
let serpAPITool = new langchainTools["SerpAPI"](
|
||||
process.env.SERPAPI_API_KEY,
|
||||
);
|
||||
@@ -290,4 +291,4 @@ async function handle(req: NextRequest) {
|
||||
export const GET = handle;
|
||||
export const POST = handle;
|
||||
|
||||
export const runtime = "edge";
|
||||
export const runtime = process.env.DDG_PROXY_URL ? "nodejs" : "edge";
|
||||
|
||||
Reference in New Issue
Block a user