@kagan-sh/opensearch 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/sources/web.js +61 -29
- package/package.json +1 -1
package/dist/sources/web.js
CHANGED
|
@@ -1,42 +1,74 @@
|
|
|
1
1
|
import { failure, messageFromError } from "./shared";
|
|
2
|
+
function buildQueries(query) {
|
|
3
|
+
const cleaned = query.trim();
|
|
4
|
+
if (!cleaned)
|
|
5
|
+
return [];
|
|
6
|
+
if (cleaned.includes("site:"))
|
|
7
|
+
return [cleaned];
|
|
8
|
+
const repoLike = cleaned.match(/\b([a-z0-9_.-]+\/[a-z0-9_.-]+)\b/i)?.[1];
|
|
9
|
+
return [
|
|
10
|
+
cleaned,
|
|
11
|
+
...(repoLike ? [`site:github.com ${repoLike}`] : []),
|
|
12
|
+
...(repoLike ? [] : [`site:github.com ${cleaned}`]),
|
|
13
|
+
`site:stackoverflow.com ${cleaned}`,
|
|
14
|
+
`${cleaned} official docs`,
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
function normalizeResults(list, depth) {
|
|
18
|
+
const limit = depth === "quick" ? 5 : 10;
|
|
19
|
+
const deduped = new Map();
|
|
20
|
+
for (const item of list) {
|
|
21
|
+
const key = item.url ?? `${item.title ?? "untitled"}:${item.content ?? ""}`;
|
|
22
|
+
if (!deduped.has(key))
|
|
23
|
+
deduped.set(key, item);
|
|
24
|
+
}
|
|
25
|
+
return Array.from(deduped.values())
|
|
26
|
+
.slice(0, limit)
|
|
27
|
+
.map((item, i, arr) => ({
|
|
28
|
+
id: `web-${i}`,
|
|
29
|
+
type: "web",
|
|
30
|
+
title: item.title ?? item.url ?? "Untitled",
|
|
31
|
+
snippet: (item.content ?? item.url ?? "").slice(0, 700),
|
|
32
|
+
url: item.url,
|
|
33
|
+
relevance: Math.max(0.1, 1 - i / Math.max(1, arr.length)),
|
|
34
|
+
timestamp: item.publishedDate
|
|
35
|
+
? Date.parse(item.publishedDate) || undefined
|
|
36
|
+
: undefined,
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
2
39
|
export async function searchWeb(query, baseUrl, depth) {
|
|
3
40
|
const source = "web";
|
|
4
41
|
if (!baseUrl) {
|
|
5
42
|
return failure(source, "unavailable", "Web source requires OPENSEARCH_WEB_URL.");
|
|
6
43
|
}
|
|
7
44
|
try {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
45
|
+
const collected = [];
|
|
46
|
+
for (const candidate of buildQueries(query)) {
|
|
47
|
+
const url = new URL("search", baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`);
|
|
48
|
+
url.searchParams.set("q", candidate);
|
|
49
|
+
url.searchParams.set("format", "json");
|
|
50
|
+
const res = await fetch(url, {
|
|
51
|
+
headers: {
|
|
52
|
+
Accept: "application/json",
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
if (!res.ok) {
|
|
56
|
+
return failure(source, "request_failed", res.status === 403
|
|
57
|
+
? "SearXNG search failed with status 403. Enable JSON output on the instance."
|
|
58
|
+
: `SearXNG search failed with status ${res.status}.`);
|
|
59
|
+
}
|
|
60
|
+
const body = (await res.json());
|
|
61
|
+
if (!Array.isArray(body.results)) {
|
|
62
|
+
return failure(source, "invalid_response", "SearXNG search returned an invalid payload.");
|
|
63
|
+
}
|
|
64
|
+
collected.push(...body.results);
|
|
65
|
+
if (collected.length > 0)
|
|
66
|
+
break;
|
|
24
67
|
}
|
|
25
|
-
const
|
|
26
|
-
const list = body.results.slice(0, limit);
|
|
68
|
+
const results = normalizeResults(collected, depth);
|
|
27
69
|
return {
|
|
28
70
|
source,
|
|
29
|
-
results
|
|
30
|
-
id: `web-${i}`,
|
|
31
|
-
type: source,
|
|
32
|
-
title: item.title ?? item.url ?? "Untitled",
|
|
33
|
-
snippet: (item.content ?? item.url ?? "").slice(0, 700),
|
|
34
|
-
url: item.url,
|
|
35
|
-
relevance: Math.max(0.1, 1 - i / Math.max(1, list.length)),
|
|
36
|
-
timestamp: item.publishedDate
|
|
37
|
-
? Date.parse(item.publishedDate) || undefined
|
|
38
|
-
: undefined,
|
|
39
|
-
})),
|
|
71
|
+
results,
|
|
40
72
|
};
|
|
41
73
|
}
|
|
42
74
|
catch (error) {
|