Spaces:
Sleeping
Sleeping
File size: 5,126 Bytes
c16e487 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | import { useSettingStore } from "@/store/setting";
import {
createSearchProvider,
type SearchProviderOptions,
} from "@/utils/deep-research/search";
import { multiApiKeyPolling } from "@/utils/model";
import { generateSignature } from "@/utils/signature";
function normalizeDomain(input: string) {
return input
.trim()
.toLowerCase()
.replace(/^https?:\/\//, "")
.replace(/^www\./, "")
.replace(/^\*\./, "")
.replace(/\/.*$/, "")
.replace(/:\d+$/, "");
}
function parseDomainList(value: string) {
return value
.split(/[\s,\n]+/g)
.map((item) => normalizeDomain(item))
.filter((item) => item.length > 0);
}
function matchDomain(hostname: string, domain: string) {
return hostname === domain || hostname.endsWith(`.${domain}`);
}
function isUrlAllowed(
url: string,
includeDomains: string[],
excludeDomains: string[]
) {
try {
const hostname = normalizeDomain(new URL(url).hostname);
if (excludeDomains.some((domain) => matchDomain(hostname, domain))) {
return false;
}
if (includeDomains.length === 0) {
return true;
}
return includeDomains.some((domain) => matchDomain(hostname, domain));
} catch {
return includeDomains.length === 0;
}
}
function applyDomainFilters(
result: { sources: Source[]; images: ImageSource[] },
includeDomains: string[],
excludeDomains: string[]
) {
if (includeDomains.length === 0 && excludeDomains.length === 0) {
return result;
}
return {
sources: result.sources.filter((source) =>
isUrlAllowed(source.url, includeDomains, excludeDomains)
),
images: result.images.filter((image) =>
isUrlAllowed(image.url, includeDomains, excludeDomains)
),
};
}
function useWebSearch() {
async function search(query: string) {
const {
mode,
searchProvider,
searchMaxResult,
accessPassword,
searchIncludeDomains,
searchExcludeDomains,
} = useSettingStore.getState();
const options: SearchProviderOptions = {
provider: searchProvider,
maxResult: searchMaxResult,
query,
};
const includeDomains = parseDomainList(searchIncludeDomains);
const excludeDomains = parseDomainList(searchExcludeDomains);
switch (searchProvider) {
case "tavily":
const { tavilyApiKey, tavilyApiProxy, tavilyScope } =
useSettingStore.getState();
if (mode === "local") {
options.baseURL = tavilyApiProxy;
options.apiKey = multiApiKeyPolling(tavilyApiKey);
} else {
options.baseURL = typeof window !== "undefined" ? `${window.location.origin}/api/search/tavily` : "/api/search/tavily";
}
options.scope = tavilyScope;
break;
case "firecrawl":
const { firecrawlApiKey, firecrawlApiProxy } =
useSettingStore.getState();
if (mode === "local") {
options.baseURL = firecrawlApiProxy;
options.apiKey = multiApiKeyPolling(firecrawlApiKey);
} else {
options.baseURL = typeof window !== "undefined" ? `${window.location.origin}/api/search/firecrawl` : "/api/search/firecrawl";
}
break;
case "exa":
const { exaApiKey, exaApiProxy, exaScope } = useSettingStore.getState();
if (mode === "local") {
options.baseURL = exaApiProxy;
options.apiKey = multiApiKeyPolling(exaApiKey);
} else {
options.baseURL = typeof window !== "undefined" ? `${window.location.origin}/api/search/exa` : "/api/search/exa";
}
options.scope = exaScope;
break;
case "bocha":
const { bochaApiKey, bochaApiProxy } = useSettingStore.getState();
if (mode === "local") {
options.baseURL = bochaApiProxy;
options.apiKey = multiApiKeyPolling(bochaApiKey);
} else {
options.baseURL = typeof window !== "undefined" ? `${window.location.origin}/api/search/bocha` : "/api/search/bocha";
}
break;
case "brave":
const { braveApiKey, braveApiProxy } = useSettingStore.getState();
if (mode === "local") {
options.baseURL = braveApiProxy;
options.apiKey = multiApiKeyPolling(braveApiKey);
} else {
options.baseURL = typeof window !== "undefined" ? `${window.location.origin}/api/search/brave` : "/api/search/brave";
}
break;
case "searxng":
const { searxngApiProxy, searxngScope } = useSettingStore.getState();
if (mode === "local") {
options.baseURL = searxngApiProxy;
} else {
options.baseURL = typeof window !== "undefined" ? `${window.location.origin}/api/search/searxng` : "/api/search/searxng";
}
options.scope = searxngScope;
break;
default:
break;
}
if (mode === "proxy") {
options.apiKey = generateSignature(accessPassword, Date.now());
}
const result = await createSearchProvider(options);
return applyDomainFilters(result, includeDomains, excludeDomains);
}
return { search };
}
export default useWebSearch;
|