opencode-search 1.0.0 → 1.1.0-next.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/README.md +27 -27
- package/dist/{search.d.ts → search.d.mts} +1 -2
- package/dist/search.mjs +947 -0
- package/package.json +22 -16
- package/dist/package.json +0 -68
- package/dist/search.d.ts.map +0 -1
- package/dist/search.js +0 -20
- package/dist/search.js.map +0 -1
- package/dist/src/cache.d.ts +0 -3
- package/dist/src/cache.d.ts.map +0 -1
- package/dist/src/cache.js +0 -43
- package/dist/src/cache.js.map +0 -1
- package/dist/src/format.d.ts +0 -20
- package/dist/src/format.d.ts.map +0 -1
- package/dist/src/format.js +0 -46
- package/dist/src/format.js.map +0 -1
- package/dist/src/html.d.ts +0 -8
- package/dist/src/html.d.ts.map +0 -1
- package/dist/src/html.js +0 -28
- package/dist/src/html.js.map +0 -1
- package/dist/src/normalize-blurb.d.ts +0 -2
- package/dist/src/normalize-blurb.d.ts.map +0 -1
- package/dist/src/normalize-blurb.js +0 -21
- package/dist/src/normalize-blurb.js.map +0 -1
- package/dist/src/opencode/notify.d.ts +0 -10
- package/dist/src/opencode/notify.d.ts.map +0 -1
- package/dist/src/opencode/notify.js +0 -8
- package/dist/src/opencode/notify.js.map +0 -1
- package/dist/src/parse-results.d.ts +0 -2
- package/dist/src/parse-results.d.ts.map +0 -1
- package/dist/src/parse-results.js +0 -20
- package/dist/src/parse-results.js.map +0 -1
- package/dist/src/providers/brave-search.d.ts +0 -4
- package/dist/src/providers/brave-search.d.ts.map +0 -1
- package/dist/src/providers/brave-search.js +0 -29
- package/dist/src/providers/brave-search.js.map +0 -1
- package/dist/src/providers/bsky-search.d.ts +0 -31
- package/dist/src/providers/bsky-search.d.ts.map +0 -1
- package/dist/src/providers/bsky-search.js +0 -61
- package/dist/src/providers/bsky-search.js.map +0 -1
- package/dist/src/providers/ddg-search.d.ts +0 -7
- package/dist/src/providers/ddg-search.d.ts.map +0 -1
- package/dist/src/providers/ddg-search.js +0 -88
- package/dist/src/providers/ddg-search.js.map +0 -1
- package/dist/src/providers/gnews-search.d.ts +0 -9
- package/dist/src/providers/gnews-search.d.ts.map +0 -1
- package/dist/src/providers/gnews-search.js +0 -50
- package/dist/src/providers/gnews-search.js.map +0 -1
- package/dist/src/providers/mdn-search.d.ts +0 -22
- package/dist/src/providers/mdn-search.d.ts.map +0 -1
- package/dist/src/providers/mdn-search.js +0 -59
- package/dist/src/providers/mdn-search.js.map +0 -1
- package/dist/src/providers/rfc-search.d.ts +0 -9
- package/dist/src/providers/rfc-search.d.ts.map +0 -1
- package/dist/src/providers/rfc-search.js +0 -38
- package/dist/src/providers/rfc-search.js.map +0 -1
- package/dist/src/providers/w3c-search.d.ts +0 -6
- package/dist/src/providers/w3c-search.d.ts.map +0 -1
- package/dist/src/providers/w3c-search.js +0 -42
- package/dist/src/providers/w3c-search.js.map +0 -1
- package/dist/src/providers/whatwg-search.d.ts +0 -8
- package/dist/src/providers/whatwg-search.d.ts.map +0 -1
- package/dist/src/providers/whatwg-search.js +0 -31
- package/dist/src/providers/whatwg-search.js.map +0 -1
- package/dist/src/providers/wiki-search.d.ts +0 -25
- package/dist/src/providers/wiki-search.d.ts.map +0 -1
- package/dist/src/providers/wiki-search.js +0 -50
- package/dist/src/providers/wiki-search.js.map +0 -1
- package/dist/src/renderers.d.ts +0 -18
- package/dist/src/renderers.d.ts.map +0 -1
- package/dist/src/renderers.js +0 -43
- package/dist/src/renderers.js.map +0 -1
- package/dist/src/safe.d.ts +0 -13
- package/dist/src/safe.d.ts.map +0 -1
- package/dist/src/safe.js +0 -17
- package/dist/src/safe.js.map +0 -1
- package/dist/src/search.d.ts +0 -13
- package/dist/src/search.d.ts.map +0 -1
- package/dist/src/search.js +0 -336
- package/dist/src/search.js.map +0 -1
- package/dist/src/strategies.d.ts +0 -13
- package/dist/src/strategies.d.ts.map +0 -1
- package/dist/src/strategies.js +0 -48
- package/dist/src/strategies.js.map +0 -1
- package/dist/src/user-agent.d.ts +0 -2
- package/dist/src/user-agent.d.ts.map +0 -1
- package/dist/src/user-agent.js +0 -3
- package/dist/src/user-agent.js.map +0 -1
- package/dist/src/v1-message.d.ts +0 -2
- package/dist/src/v1-message.d.ts.map +0 -1
- package/dist/src/v1-message.js +0 -11
- package/dist/src/v1-message.js.map +0 -1
- package/search.ts +0 -22
- package/src/cache.test.ts +0 -14
- package/src/cache.ts +0 -61
- package/src/format.test.ts +0 -175
- package/src/format.ts +0 -72
- package/src/html.ts +0 -39
- package/src/normalize-blurb.test.ts +0 -67
- package/src/normalize-blurb.ts +0 -22
- package/src/opencode/notify.ts +0 -16
- package/src/parse-results.test.ts +0 -75
- package/src/parse-results.ts +0 -22
- package/src/providers/brave-search.ts +0 -39
- package/src/providers/bsky-search.test.ts +0 -16
- package/src/providers/bsky-search.ts +0 -88
- package/src/providers/ddg-search.test.ts +0 -15
- package/src/providers/ddg-search.ts +0 -117
- package/src/providers/gnews-search.ts +0 -72
- package/src/providers/mdn-search.test.ts +0 -17
- package/src/providers/mdn-search.ts +0 -83
- package/src/providers/rfc-search.test.ts +0 -31
- package/src/providers/rfc-search.ts +0 -61
- package/src/providers/w3c-search.test.ts +0 -22
- package/src/providers/w3c-search.ts +0 -67
- package/src/providers/whatwg-search.test.ts +0 -21
- package/src/providers/whatwg-search.ts +0 -55
- package/src/providers/wiki-search.test.ts +0 -16
- package/src/providers/wiki-search.ts +0 -72
- package/src/renderers.test.ts +0 -263
- package/src/renderers.ts +0 -62
- package/src/safe.test.ts +0 -87
- package/src/safe.ts +0 -27
- package/src/search.ts +0 -401
- package/src/strategies.test.ts +0 -92
- package/src/strategies.ts +0 -62
- package/src/user-agent.ts +0 -3
- package/src/v1-message.test.ts +0 -17
- package/src/v1-message.ts +0 -10
package/dist/search.mjs
ADDED
|
@@ -0,0 +1,947 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { JSDOM, VirtualConsole } from "jsdom";
|
|
6
|
+
// src/safe.ts
|
|
7
|
+
const safe = (fn) => {
|
|
8
|
+
try {
|
|
9
|
+
return {
|
|
10
|
+
data: fn(),
|
|
11
|
+
error: null
|
|
12
|
+
};
|
|
13
|
+
} catch (error) {
|
|
14
|
+
return {
|
|
15
|
+
data: null,
|
|
16
|
+
error: error instanceof Error ? error : new Error(String(error))
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const safeAsync = async (fn) => {
|
|
21
|
+
try {
|
|
22
|
+
return {
|
|
23
|
+
data: await fn(),
|
|
24
|
+
error: null
|
|
25
|
+
};
|
|
26
|
+
} catch (error) {
|
|
27
|
+
return {
|
|
28
|
+
data: null,
|
|
29
|
+
error: error instanceof Error ? error : new Error(String(error))
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const fileAt = (path) => ({
|
|
34
|
+
read: () => readFile(path, "utf-8"),
|
|
35
|
+
write: (text) => writeFile(path, text)
|
|
36
|
+
});
|
|
37
|
+
const loadStore = async (file) => {
|
|
38
|
+
const { data: text, error: readError } = await safeAsync(file.read);
|
|
39
|
+
if (readError) return {};
|
|
40
|
+
const { data: parsed, error: parseError } = safe(() => JSON.parse(text));
|
|
41
|
+
if (parseError) return {};
|
|
42
|
+
return parsed;
|
|
43
|
+
};
|
|
44
|
+
const createCache = ({ file, now }) => {
|
|
45
|
+
let loading = null;
|
|
46
|
+
const storeOf = () => {
|
|
47
|
+
loading ??= loadStore(file);
|
|
48
|
+
return loading;
|
|
49
|
+
};
|
|
50
|
+
const save = (store) => {
|
|
51
|
+
file.write(JSON.stringify(store, null, 2)).catch(() => {});
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
get: async (key) => {
|
|
55
|
+
const store = await storeOf();
|
|
56
|
+
const entry = store[key];
|
|
57
|
+
if (!entry) return null;
|
|
58
|
+
if (now() - entry.timestamp > 6048e5) {
|
|
59
|
+
delete store[key];
|
|
60
|
+
save(store);
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return entry.data;
|
|
64
|
+
},
|
|
65
|
+
set: async (key, data) => {
|
|
66
|
+
const store = await storeOf();
|
|
67
|
+
store[key] = {
|
|
68
|
+
data,
|
|
69
|
+
timestamp: now()
|
|
70
|
+
};
|
|
71
|
+
save(store);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
// src/format.ts
|
|
76
|
+
const formatHeader = (label, options) => {
|
|
77
|
+
const { total, count, limit } = options;
|
|
78
|
+
const offset = options.offset ?? 0;
|
|
79
|
+
if (count === 0) return label + " (0 results)";
|
|
80
|
+
const start = offset + 1;
|
|
81
|
+
const end = offset + count;
|
|
82
|
+
const pageSize = limit ?? count;
|
|
83
|
+
const page = pageSize > 0 ? Math.floor(offset / pageSize) + 1 : 1;
|
|
84
|
+
return label + " (showing " + start + "-" + end + " of " + total + ", page " + page + ")";
|
|
85
|
+
};
|
|
86
|
+
const DATE_FMT = {
|
|
87
|
+
month: "short",
|
|
88
|
+
day: "numeric",
|
|
89
|
+
year: "numeric"
|
|
90
|
+
};
|
|
91
|
+
const DATETIME_FMT = {
|
|
92
|
+
month: "short",
|
|
93
|
+
day: "numeric",
|
|
94
|
+
year: "numeric",
|
|
95
|
+
hour: "numeric",
|
|
96
|
+
minute: "2-digit"
|
|
97
|
+
};
|
|
98
|
+
const parseDate = (raw) => {
|
|
99
|
+
const d = new Date(raw);
|
|
100
|
+
return isNaN(d.getTime()) ? null : d;
|
|
101
|
+
};
|
|
102
|
+
const formatDate = (raw) => {
|
|
103
|
+
const d = parseDate(raw);
|
|
104
|
+
return d ? d.toLocaleString("en-US", DATE_FMT) : raw;
|
|
105
|
+
};
|
|
106
|
+
const formatDateTime = (raw) => {
|
|
107
|
+
const d = parseDate(raw);
|
|
108
|
+
return d ? d.toLocaleString("en-US", DATETIME_FMT) : raw;
|
|
109
|
+
};
|
|
110
|
+
const formatResults = (options) => {
|
|
111
|
+
const { label, items, total, limit, offset, renderItem } = options;
|
|
112
|
+
const header = formatHeader(label, {
|
|
113
|
+
total,
|
|
114
|
+
count: items.length,
|
|
115
|
+
limit,
|
|
116
|
+
offset
|
|
117
|
+
});
|
|
118
|
+
if (items.length === 0) return header;
|
|
119
|
+
const body = items.map((item, i) => renderItem(item, i)).join("\n\n");
|
|
120
|
+
return header + "\n\n" + body;
|
|
121
|
+
};
|
|
122
|
+
// src/opencode/notify.ts
|
|
123
|
+
const sendResult = async (options) => {
|
|
124
|
+
await options.context.session.synthetic({
|
|
125
|
+
sessionID: options.sessionID,
|
|
126
|
+
text: options.text,
|
|
127
|
+
description: "Search results"
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
// src/strategies.ts
|
|
131
|
+
const USER_AGENTS = [
|
|
132
|
+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
133
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
|
|
134
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15",
|
|
135
|
+
"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0"
|
|
136
|
+
];
|
|
137
|
+
let uaIndex = 0;
|
|
138
|
+
const nextUserAgent = () => {
|
|
139
|
+
uaIndex = (uaIndex + 1) % USER_AGENTS.length;
|
|
140
|
+
return USER_AGENTS[uaIndex] ?? "";
|
|
141
|
+
};
|
|
142
|
+
const baseHeaders = (userAgent) => ({
|
|
143
|
+
"User-Agent": userAgent,
|
|
144
|
+
Accept: "text/html",
|
|
145
|
+
"Accept-Encoding": "gzip",
|
|
146
|
+
DNT: "1"
|
|
147
|
+
});
|
|
148
|
+
var BlockedError = class extends Error {
|
|
149
|
+
constructor(source) {
|
|
150
|
+
super(source + " blocked the request (CAPTCHA or rate limit)");
|
|
151
|
+
this.name = "BlockedError";
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const detectBlock = (source, html, markers) => {
|
|
155
|
+
for (const marker of markers) if (html.includes(marker)) throw new BlockedError(source);
|
|
156
|
+
};
|
|
157
|
+
const runStrategies = async (strategies) => {
|
|
158
|
+
const failures = [];
|
|
159
|
+
for (const strategy of strategies) {
|
|
160
|
+
const { data, error } = await safeAsync(() => strategy.run(nextUserAgent()));
|
|
161
|
+
if (error) {
|
|
162
|
+
failures.push(strategy.name + ": " + error.message);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (data.length > 0) return data;
|
|
166
|
+
failures.push(strategy.name + ": no results");
|
|
167
|
+
}
|
|
168
|
+
throw new Error(failures.join("; "));
|
|
169
|
+
};
|
|
170
|
+
// src/html.ts
|
|
171
|
+
const makeDom = (html, url) => {
|
|
172
|
+
return new JSDOM(html, {
|
|
173
|
+
url,
|
|
174
|
+
virtualConsole: new VirtualConsole()
|
|
175
|
+
}).window.document;
|
|
176
|
+
};
|
|
177
|
+
const statusHeadingPattern = /\b\d{3}\s*-?\s*(Forbidden|Too Many Requests)\b/gi;
|
|
178
|
+
const blockedStatuses = /* @__PURE__ */ new Set([403, 429]);
|
|
179
|
+
const fetchHtml = async ({ fetch, url, userAgent }) => {
|
|
180
|
+
const res = await fetch(url, { headers: baseHeaders(userAgent) });
|
|
181
|
+
if (blockedStatuses.has(res.status)) {
|
|
182
|
+
const reason = (await res.text().catch(() => "")).replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").replace(statusHeadingPattern, "").trim().slice(0, 140);
|
|
183
|
+
throw new BlockedError(new URL(url).hostname + (reason ? " — " + reason : ""));
|
|
184
|
+
}
|
|
185
|
+
if (!res.ok) throw new Error(res.status + " " + res.statusText);
|
|
186
|
+
return res.text();
|
|
187
|
+
};
|
|
188
|
+
// src/providers/brave-search.ts
|
|
189
|
+
const BRAVE_URL = "https://search.brave.com/search";
|
|
190
|
+
const BLOCK_MARKERS$1 = ["Verifying you are human", "challenge-error"];
|
|
191
|
+
const textOf = (element) => element?.textContent.trim() ?? "";
|
|
192
|
+
const braveSearch = async ({ fetch, cache }, query) => {
|
|
193
|
+
const cacheKey = "brave:" + query;
|
|
194
|
+
const cached = await cache.get(cacheKey);
|
|
195
|
+
if (cached) return cached;
|
|
196
|
+
const html = await fetchHtml({
|
|
197
|
+
fetch,
|
|
198
|
+
url: BRAVE_URL + "?q=" + encodeURIComponent(query),
|
|
199
|
+
userAgent: nextUserAgent()
|
|
200
|
+
});
|
|
201
|
+
detectBlock("Brave", html, BLOCK_MARKERS$1);
|
|
202
|
+
const doc = makeDom(html, BRAVE_URL);
|
|
203
|
+
const results = [];
|
|
204
|
+
for (const container of doc.querySelectorAll("div.snippet[data-type=\"web\"]")) {
|
|
205
|
+
const title = textOf(container.querySelector(".title"));
|
|
206
|
+
const url = container.querySelector("a")?.getAttribute("href");
|
|
207
|
+
const abstract = textOf(container.querySelector(".generic-snippet .content"));
|
|
208
|
+
if (title && url?.startsWith("http")) results.push({
|
|
209
|
+
title,
|
|
210
|
+
url,
|
|
211
|
+
abstract
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
cache.set(cacheKey, results);
|
|
215
|
+
return results;
|
|
216
|
+
};
|
|
217
|
+
// src/providers/bsky-search.ts
|
|
218
|
+
const authorSchema = z.object({
|
|
219
|
+
did: z.string().default(""),
|
|
220
|
+
handle: z.string().min(1),
|
|
221
|
+
displayName: z.string().default("")
|
|
222
|
+
});
|
|
223
|
+
const recordSchema = z.object({
|
|
224
|
+
text: z.string().min(1),
|
|
225
|
+
createdAt: z.string().default("")
|
|
226
|
+
});
|
|
227
|
+
const postSchema = z.object({
|
|
228
|
+
uri: z.string().default(""),
|
|
229
|
+
cid: z.string().default(""),
|
|
230
|
+
author: authorSchema,
|
|
231
|
+
record: recordSchema,
|
|
232
|
+
likeCount: z.number().default(0),
|
|
233
|
+
repostCount: z.number().default(0),
|
|
234
|
+
replyCount: z.number().default(0)
|
|
235
|
+
});
|
|
236
|
+
const responseSchema$3 = z.object({
|
|
237
|
+
posts: z.array(z.unknown()),
|
|
238
|
+
hitsTotal: z.number().optional(),
|
|
239
|
+
cursor: z.string().default("")
|
|
240
|
+
});
|
|
241
|
+
const bskySearch = async ({ fetch, cache }, options) => {
|
|
242
|
+
const cacheKey = [
|
|
243
|
+
"bsky",
|
|
244
|
+
options.query,
|
|
245
|
+
options.sort ?? "latest",
|
|
246
|
+
options.limit ?? 25
|
|
247
|
+
].join(":");
|
|
248
|
+
const cached = await cache.get(cacheKey);
|
|
249
|
+
if (cached) return cached;
|
|
250
|
+
const params = new URLSearchParams({ q: options.query });
|
|
251
|
+
if (options.sort) params.set("sort", options.sort);
|
|
252
|
+
if (options.limit) params.set("limit", String(options.limit));
|
|
253
|
+
const res = await fetch("https://api.bsky.app/xrpc/app.bsky.feed.searchPosts?" + params.toString());
|
|
254
|
+
if (!res.ok) throw new Error("Bluesky search error (" + res.status + "): " + await res.text());
|
|
255
|
+
const response = responseSchema$3.parse(await res.json());
|
|
256
|
+
const posts = response.posts.flatMap((raw) => {
|
|
257
|
+
const parsed = postSchema.safeParse(raw);
|
|
258
|
+
return parsed.success ? [parsed.data] : [];
|
|
259
|
+
});
|
|
260
|
+
const result = {
|
|
261
|
+
posts,
|
|
262
|
+
hitsTotal: response.hitsTotal ?? posts.length,
|
|
263
|
+
cursor: response.cursor
|
|
264
|
+
};
|
|
265
|
+
if (posts.length > 0) cache.set(cacheKey, result);
|
|
266
|
+
return result;
|
|
267
|
+
};
|
|
268
|
+
// src/providers/ddg-search.ts
|
|
269
|
+
const DDG_HTML_URL = "https://html.duckduckgo.com/html/";
|
|
270
|
+
const DDG_LITE_URL = "https://lite.duckduckgo.com/lite/";
|
|
271
|
+
const BLOCK_MARKERS = ["anomaly-modal", "Please try again"];
|
|
272
|
+
const HTML_FRONTEND = {
|
|
273
|
+
url: DDG_HTML_URL,
|
|
274
|
+
link: ".result__a",
|
|
275
|
+
snippet: ".result__snippet"
|
|
276
|
+
};
|
|
277
|
+
const LITE_FRONTEND = {
|
|
278
|
+
url: DDG_LITE_URL,
|
|
279
|
+
link: "a.result-link",
|
|
280
|
+
snippet: "td.result-snippet"
|
|
281
|
+
};
|
|
282
|
+
const fetchDdg = async ({ fetch, url, query, userAgent }) => {
|
|
283
|
+
const body = new URLSearchParams({
|
|
284
|
+
q: query,
|
|
285
|
+
b: "",
|
|
286
|
+
kf: "-1",
|
|
287
|
+
kh: "1",
|
|
288
|
+
kl: "us-en",
|
|
289
|
+
kp: "1",
|
|
290
|
+
k1: "-1"
|
|
291
|
+
});
|
|
292
|
+
const response = await fetch(url, {
|
|
293
|
+
method: "POST",
|
|
294
|
+
headers: {
|
|
295
|
+
...baseHeaders(userAgent),
|
|
296
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
297
|
+
},
|
|
298
|
+
body
|
|
299
|
+
});
|
|
300
|
+
if (!response.ok) throw new Error(response.status + " " + response.statusText);
|
|
301
|
+
const html = await response.text();
|
|
302
|
+
detectBlock("DuckDuckGo", html, BLOCK_MARKERS);
|
|
303
|
+
return html;
|
|
304
|
+
};
|
|
305
|
+
const parseResults = (html, frontend) => {
|
|
306
|
+
const doc = makeDom(html, frontend.url);
|
|
307
|
+
const snippets = Array.from(doc.querySelectorAll(frontend.snippet));
|
|
308
|
+
return Array.from(doc.querySelectorAll(frontend.link)).flatMap((link, i) => {
|
|
309
|
+
const title = link.textContent.trim();
|
|
310
|
+
const url = link.getAttribute("href");
|
|
311
|
+
const abstract = snippets[i]?.textContent.trim() ?? "";
|
|
312
|
+
return title && url ? [{
|
|
313
|
+
title,
|
|
314
|
+
url,
|
|
315
|
+
abstract
|
|
316
|
+
}] : [];
|
|
317
|
+
});
|
|
318
|
+
};
|
|
319
|
+
const ddgSearch = async ({ fetch, cache }, query) => {
|
|
320
|
+
const cacheKey = "ddg:" + query;
|
|
321
|
+
const cached = await cache.get(cacheKey);
|
|
322
|
+
if (cached) return cached;
|
|
323
|
+
const strategyOf = (name, frontend) => ({
|
|
324
|
+
name,
|
|
325
|
+
run: async (userAgent) => parseResults(await fetchDdg({
|
|
326
|
+
fetch,
|
|
327
|
+
url: frontend.url,
|
|
328
|
+
query,
|
|
329
|
+
userAgent
|
|
330
|
+
}), frontend)
|
|
331
|
+
});
|
|
332
|
+
const results = await runStrategies([strategyOf("ddg-html", HTML_FRONTEND), strategyOf("ddg-lite", LITE_FRONTEND)]);
|
|
333
|
+
cache.set(cacheKey, results);
|
|
334
|
+
return results;
|
|
335
|
+
};
|
|
336
|
+
// src/providers/gnews-search.ts
|
|
337
|
+
const decodeEntities$1 = (value) => value.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"").replace(/'/g, "'").replace(/'/g, "'").replace(/&/g, "&");
|
|
338
|
+
const stripSource = (title, source) => {
|
|
339
|
+
const suffix = " - " + source;
|
|
340
|
+
return title.endsWith(suffix) ? title.slice(0, -suffix.length) : title;
|
|
341
|
+
};
|
|
342
|
+
const itemPattern = /<item>.*?<\/item>/gs;
|
|
343
|
+
const cdataPattern = /<!\[CDATA\[(.*?)\]\]>/gs;
|
|
344
|
+
const tagOf = (item, name) => {
|
|
345
|
+
const tagPattern = new RegExp("(?<=<" + name + "[^>]*>).*?(?=</" + name + ">)", "s");
|
|
346
|
+
const value = item.match(tagPattern)?.[0] ?? "";
|
|
347
|
+
return decodeEntities$1(value.replace(cdataPattern, "$1").trim());
|
|
348
|
+
};
|
|
349
|
+
const DEFAULT_LIMIT$3 = 20;
|
|
350
|
+
const gnewsSearch = async ({ fetch, cache }, query, limit = DEFAULT_LIMIT$3) => {
|
|
351
|
+
const cacheKey = "gnews:" + query;
|
|
352
|
+
const cached = await cache.get(cacheKey);
|
|
353
|
+
if (cached) return cached.slice(0, limit);
|
|
354
|
+
const res = await fetch("https://news.google.com/rss/search?" + new URLSearchParams({
|
|
355
|
+
q: query,
|
|
356
|
+
hl: "en-US",
|
|
357
|
+
gl: "US",
|
|
358
|
+
ceid: "US:en"
|
|
359
|
+
}), { headers: baseHeaders("") });
|
|
360
|
+
if (!res.ok) throw new Error("Google News returned " + res.status + " " + res.statusText);
|
|
361
|
+
const results = [];
|
|
362
|
+
for (const [item] of (await res.text()).matchAll(itemPattern)) {
|
|
363
|
+
const source = tagOf(item, "source");
|
|
364
|
+
const title = stripSource(tagOf(item, "title"), source);
|
|
365
|
+
const url = tagOf(item, "link");
|
|
366
|
+
if (title && url) results.push({
|
|
367
|
+
title,
|
|
368
|
+
url,
|
|
369
|
+
abstract: "",
|
|
370
|
+
source,
|
|
371
|
+
date: tagOf(item, "pubDate")
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
cache.set(cacheKey, results);
|
|
375
|
+
return results.slice(0, limit);
|
|
376
|
+
};
|
|
377
|
+
// src/user-agent.ts
|
|
378
|
+
const USER_AGENT = "opencode-search/1.1.0-next.0";
|
|
379
|
+
// src/providers/mdn-search.ts
|
|
380
|
+
const documentSchema$1 = z.object({
|
|
381
|
+
mdn_url: z.string().min(1),
|
|
382
|
+
title: z.string().min(1),
|
|
383
|
+
summary: z.string().default(""),
|
|
384
|
+
slug: z.string().default(""),
|
|
385
|
+
locale: z.string().default("en-US"),
|
|
386
|
+
popularity: z.number().default(0)
|
|
387
|
+
});
|
|
388
|
+
const totalSchema = z.object({ value: z.number() });
|
|
389
|
+
const metadataSchema = z.object({ total: totalSchema });
|
|
390
|
+
const responseSchema$2 = z.object({
|
|
391
|
+
documents: z.array(z.unknown()),
|
|
392
|
+
metadata: metadataSchema.optional()
|
|
393
|
+
});
|
|
394
|
+
const mdnSearch = async ({ fetch, cache }, options) => {
|
|
395
|
+
const cacheKey = [
|
|
396
|
+
"mdn",
|
|
397
|
+
options.query,
|
|
398
|
+
options.limit ?? 10,
|
|
399
|
+
options.page ?? 1
|
|
400
|
+
].join(":");
|
|
401
|
+
const cached = await cache.get(cacheKey);
|
|
402
|
+
if (cached) return cached;
|
|
403
|
+
const params = new URLSearchParams({
|
|
404
|
+
q: options.query,
|
|
405
|
+
locale: "en-US"
|
|
406
|
+
});
|
|
407
|
+
if (options.limit) params.set("size", String(options.limit));
|
|
408
|
+
if (options.page) params.set("page", String(options.page));
|
|
409
|
+
const res = await fetch("https://developer.mozilla.org/api/v1/search?" + params.toString(), { headers: { "User-Agent": USER_AGENT } });
|
|
410
|
+
if (!res.ok) throw new Error("MDN search error (" + res.status + "): " + await res.text());
|
|
411
|
+
const response = responseSchema$2.parse(await res.json());
|
|
412
|
+
const documents = response.documents.flatMap((raw) => {
|
|
413
|
+
const parsed = documentSchema$1.safeParse(raw);
|
|
414
|
+
return parsed.success ? [parsed.data] : [];
|
|
415
|
+
});
|
|
416
|
+
const result = {
|
|
417
|
+
documents,
|
|
418
|
+
total: response.metadata?.total.value ?? documents.length
|
|
419
|
+
};
|
|
420
|
+
if (documents.length > 0) cache.set(cacheKey, result);
|
|
421
|
+
return result;
|
|
422
|
+
};
|
|
423
|
+
// src/providers/rfc-search.ts
|
|
424
|
+
const DEFAULT_LIMIT$2 = 20;
|
|
425
|
+
const documentSchema = z.object({
|
|
426
|
+
name: z.string().default(""),
|
|
427
|
+
title: z.string().default(""),
|
|
428
|
+
time: z.string().default(""),
|
|
429
|
+
abstract: z.string().default("")
|
|
430
|
+
});
|
|
431
|
+
const responseSchema$1 = z.object({ objects: z.array(documentSchema).default([]) });
|
|
432
|
+
const rfcNumberPattern = /^(?:rfc[\s-]?)?(\d{1,5})$/i;
|
|
433
|
+
const rfcSearch = async ({ fetch, cache }, query, limit = DEFAULT_LIMIT$2) => {
|
|
434
|
+
const cacheKey = "rfc:" + query;
|
|
435
|
+
const cached = await cache.get(cacheKey);
|
|
436
|
+
if (cached) return cached.slice(0, limit);
|
|
437
|
+
const number = rfcNumberPattern.exec(query.trim())?.[1];
|
|
438
|
+
const res = await fetch("https://datatracker.ietf.org/api/v1/doc/document/?" + new URLSearchParams({
|
|
439
|
+
...number ? { name: "rfc" + number } : { title__icontains: query },
|
|
440
|
+
type: "rfc",
|
|
441
|
+
limit: String(Math.min(limit, 100)),
|
|
442
|
+
format: "json"
|
|
443
|
+
}));
|
|
444
|
+
if (!res.ok) throw new Error("IETF Datatracker returned " + res.status + " " + res.statusText);
|
|
445
|
+
const { objects } = responseSchema$1.parse(await res.json());
|
|
446
|
+
const results = objects.filter((doc) => doc.name).map((doc) => ({
|
|
447
|
+
id: doc.name.toUpperCase(),
|
|
448
|
+
title: doc.title,
|
|
449
|
+
url: "https://www.rfc-editor.org/rfc/" + doc.name + ".html",
|
|
450
|
+
date: doc.time,
|
|
451
|
+
abstract: doc.abstract
|
|
452
|
+
}));
|
|
453
|
+
cache.set(cacheKey, results);
|
|
454
|
+
return results.slice(0, limit);
|
|
455
|
+
};
|
|
456
|
+
// src/providers/w3c-search.ts
|
|
457
|
+
const PAGE_SIZE = 1e3;
|
|
458
|
+
const DEFAULT_LIMIT$1 = 20;
|
|
459
|
+
const MAX_PAGES = 5;
|
|
460
|
+
const specSchema = z.object({
|
|
461
|
+
title: z.string().default(""),
|
|
462
|
+
href: z.string().default(""),
|
|
463
|
+
shortlink: z.string().optional()
|
|
464
|
+
});
|
|
465
|
+
const pageSchema$1 = z.object({
|
|
466
|
+
pages: z.number().int().positive().default(1),
|
|
467
|
+
_links: z.object({ specifications: z.array(specSchema).default([]) }).default({ specifications: [] })
|
|
468
|
+
});
|
|
469
|
+
const fetchPage = async (fetch, page) => {
|
|
470
|
+
const res = await fetch("https://api.w3.org/specifications?" + new URLSearchParams({
|
|
471
|
+
items: String(PAGE_SIZE),
|
|
472
|
+
page: String(page)
|
|
473
|
+
}));
|
|
474
|
+
if (!res.ok) throw new Error("W3C API returned " + res.status + " " + res.statusText);
|
|
475
|
+
return pageSchema$1.parse(await res.json());
|
|
476
|
+
};
|
|
477
|
+
const w3cSearch = async ({ fetch, cache }, query, limit = DEFAULT_LIMIT$1) => {
|
|
478
|
+
const cacheKey = "w3c:" + query;
|
|
479
|
+
const cached = await cache.get(cacheKey);
|
|
480
|
+
if (cached) return cached.slice(0, limit);
|
|
481
|
+
const first = await fetchPage(fetch, 1);
|
|
482
|
+
const specs = [first, ...await Promise.all(Array.from({ length: Math.min(first.pages, MAX_PAGES) - 1 }, (_, i) => fetchPage(fetch, i + 2)))].flatMap((page) => page._links.specifications);
|
|
483
|
+
const needle = query.toLowerCase();
|
|
484
|
+
const results = [];
|
|
485
|
+
for (const spec of specs) {
|
|
486
|
+
const url = spec.shortlink ?? spec.href;
|
|
487
|
+
if (spec.title && url && spec.title.toLowerCase().includes(needle)) results.push({
|
|
488
|
+
title: spec.title,
|
|
489
|
+
url
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
cache.set(cacheKey, results);
|
|
493
|
+
return results.slice(0, limit);
|
|
494
|
+
};
|
|
495
|
+
// src/providers/whatwg-search.ts
|
|
496
|
+
const DB_URL = "https://raw.githubusercontent.com/whatwg/sg/main/db.json";
|
|
497
|
+
const DEFAULT_LIMIT = 20;
|
|
498
|
+
const standardSchema = z.object({
|
|
499
|
+
name: z.string().default(""),
|
|
500
|
+
href: z.string().default(""),
|
|
501
|
+
description: z.string().default("")
|
|
502
|
+
});
|
|
503
|
+
const workstreamSchema = z.object({
|
|
504
|
+
name: z.string().default(""),
|
|
505
|
+
standards: z.array(standardSchema).default([])
|
|
506
|
+
});
|
|
507
|
+
const indexSchema = z.object({ workstreams: z.array(workstreamSchema).default([]) });
|
|
508
|
+
const whatwgSearch = async ({ fetch, cache }, query, limit = DEFAULT_LIMIT) => {
|
|
509
|
+
const cacheKey = "whatwg:" + query;
|
|
510
|
+
const cached = await cache.get(cacheKey);
|
|
511
|
+
if (cached) return cached.slice(0, limit);
|
|
512
|
+
const res = await fetch(DB_URL);
|
|
513
|
+
if (!res.ok) throw new Error("WHATWG index returned " + res.status + " " + res.statusText);
|
|
514
|
+
const { workstreams } = indexSchema.parse(await res.json());
|
|
515
|
+
const needle = query.toLowerCase();
|
|
516
|
+
const results = workstreams.flatMap((workstream) => workstream.standards.flatMap((standard) => {
|
|
517
|
+
const matches = (standard.name + " " + standard.description).toLowerCase().includes(needle);
|
|
518
|
+
if (!standard.name || !standard.href || !matches) return [];
|
|
519
|
+
return [{
|
|
520
|
+
title: standard.name,
|
|
521
|
+
url: standard.href,
|
|
522
|
+
abstract: standard.description,
|
|
523
|
+
workstream: workstream.name
|
|
524
|
+
}];
|
|
525
|
+
}));
|
|
526
|
+
cache.set(cacheKey, results);
|
|
527
|
+
return results.slice(0, limit);
|
|
528
|
+
};
|
|
529
|
+
// src/normalize-blurb.ts
|
|
530
|
+
const decodeEntities = (text) => {
|
|
531
|
+
return text.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"").replace(/'/g, "'").replace(/ /g, " ").replace(/&/g, "&").replace(/&#(\d+);/g, (_, n) => String.fromCharCode(n)).replace(/&#x([0-9a-fA-F]+);/g, (_, h) => String.fromCharCode(parseInt(h, 16)));
|
|
532
|
+
};
|
|
533
|
+
const normalizeBlurb = (text) => {
|
|
534
|
+
const stripped = text.replace(/<[^>]*>/g, " ");
|
|
535
|
+
return decodeEntities(stripped).replace(/[\s\n\r\t]+/g, " ").trim();
|
|
536
|
+
};
|
|
537
|
+
// src/providers/wiki-search.ts
|
|
538
|
+
const thumbnailSchema = z.object({
|
|
539
|
+
url: z.string().min(1),
|
|
540
|
+
width: z.number(),
|
|
541
|
+
height: z.number()
|
|
542
|
+
});
|
|
543
|
+
const pageSchema = z.object({
|
|
544
|
+
id: z.number().default(0),
|
|
545
|
+
key: z.string().min(1),
|
|
546
|
+
title: z.string().min(1),
|
|
547
|
+
excerpt: z.string().default("").transform(normalizeBlurb),
|
|
548
|
+
description: z.string().nullable().default(null),
|
|
549
|
+
matched_title: z.string().nullable().default(null),
|
|
550
|
+
thumbnail: thumbnailSchema.nullable().default(null)
|
|
551
|
+
});
|
|
552
|
+
const responseSchema = z.object({ pages: z.array(z.unknown()) });
|
|
553
|
+
const wikiSearch = async ({ fetch, cache }, options) => {
|
|
554
|
+
const cacheKey = "wiki:" + options.query + ":" + (options.limit ?? 10);
|
|
555
|
+
const cached = await cache.get(cacheKey);
|
|
556
|
+
if (cached) return cached;
|
|
557
|
+
const params = new URLSearchParams({ q: options.query });
|
|
558
|
+
if (options.limit) params.set("limit", String(options.limit));
|
|
559
|
+
const res = await fetch("https://en.wikipedia.org/w/rest.php/v1/search/page?" + params.toString(), { headers: { "User-Agent": USER_AGENT } });
|
|
560
|
+
if (!res.ok) throw new Error("Wikipedia search error (" + res.status + "): " + await res.text());
|
|
561
|
+
const pages = responseSchema.parse(await res.json()).pages.flatMap((raw) => {
|
|
562
|
+
const parsed = pageSchema.safeParse(raw);
|
|
563
|
+
return parsed.success ? [parsed.data] : [];
|
|
564
|
+
});
|
|
565
|
+
if (pages.length > 0) cache.set(cacheKey, { pages });
|
|
566
|
+
return { pages };
|
|
567
|
+
};
|
|
568
|
+
// src/renderers.ts
|
|
569
|
+
const renderDdgResult = (result, index) => {
|
|
570
|
+
return `${index + 1}. ${result.title}
|
|
571
|
+
${result.url}
|
|
572
|
+
${normalizeBlurb(result.abstract)}`;
|
|
573
|
+
};
|
|
574
|
+
const renderWebResult = renderDdgResult;
|
|
575
|
+
const renderGnewsItem = (result, index) => {
|
|
576
|
+
return `${index + 1}. ${result.title}
|
|
577
|
+
${result.source}${result.date ? " — " + formatDate(result.date) : ""}
|
|
578
|
+
${result.url}`;
|
|
579
|
+
};
|
|
580
|
+
const renderBskyPost = (post, index) => `${index + 1}. @${post.author.handle} (${formatDateTime(post.record.createdAt)})
|
|
581
|
+
${normalizeBlurb(post.record.text)}
|
|
582
|
+
Likes: ${post.likeCount} Reposts: ${post.repostCount} Replies: ${post.replyCount}`;
|
|
583
|
+
const renderWikiPage = (page, index) => {
|
|
584
|
+
const desc = page.description ? `\n ${normalizeBlurb(page.description)}` : "";
|
|
585
|
+
return `${index + 1}. ${page.title}
|
|
586
|
+
https://en.wikipedia.org/wiki/${page.key}${desc}
|
|
587
|
+
${normalizeBlurb(page.excerpt)}`;
|
|
588
|
+
};
|
|
589
|
+
const renderMdnDoc = (doc, index) => {
|
|
590
|
+
return `${index + 1}. ${doc.title}
|
|
591
|
+
https://developer.mozilla.org${doc.mdn_url}
|
|
592
|
+
${normalizeBlurb(doc.summary)}`;
|
|
593
|
+
};
|
|
594
|
+
const renderRfc = (result, index) => {
|
|
595
|
+
return `${index + 1}. ${result.id} — ${result.title}
|
|
596
|
+
${result.url}
|
|
597
|
+
${normalizeBlurb(result.abstract)}`;
|
|
598
|
+
};
|
|
599
|
+
const renderW3c = (result, index) => {
|
|
600
|
+
return `${index + 1}. ${result.title}
|
|
601
|
+
${result.url}`;
|
|
602
|
+
};
|
|
603
|
+
const renderWhatwg = (result, index) => {
|
|
604
|
+
return `${index + 1}. ${result.title}
|
|
605
|
+
${result.url}
|
|
606
|
+
${normalizeBlurb(result.abstract)}`;
|
|
607
|
+
};
|
|
608
|
+
// src/search.ts
|
|
609
|
+
const textOutput = (text) => ({ content: [{
|
|
610
|
+
type: "text",
|
|
611
|
+
text
|
|
612
|
+
}] });
|
|
613
|
+
const count = z.number().int().positive();
|
|
614
|
+
const queryInput = z.object({ query: z.string() });
|
|
615
|
+
const limitInput = z.object({
|
|
616
|
+
query: z.string(),
|
|
617
|
+
limit: count.optional()
|
|
618
|
+
});
|
|
619
|
+
const bskyInput = z.object({
|
|
620
|
+
query: z.string(),
|
|
621
|
+
limit: count.optional(),
|
|
622
|
+
sort: z.string().optional()
|
|
623
|
+
});
|
|
624
|
+
const mdnInput = z.object({
|
|
625
|
+
query: z.string(),
|
|
626
|
+
limit: count.optional(),
|
|
627
|
+
page: count.optional()
|
|
628
|
+
});
|
|
629
|
+
const queryArg = {
|
|
630
|
+
type: "object",
|
|
631
|
+
properties: { query: {
|
|
632
|
+
type: "string",
|
|
633
|
+
description: "The search query"
|
|
634
|
+
} },
|
|
635
|
+
required: ["query"],
|
|
636
|
+
additionalProperties: false
|
|
637
|
+
};
|
|
638
|
+
const limitArg = (description) => ({
|
|
639
|
+
type: "object",
|
|
640
|
+
properties: {
|
|
641
|
+
query: {
|
|
642
|
+
type: "string",
|
|
643
|
+
description: "The search query"
|
|
644
|
+
},
|
|
645
|
+
limit: {
|
|
646
|
+
type: "number",
|
|
647
|
+
description
|
|
648
|
+
}
|
|
649
|
+
},
|
|
650
|
+
required: ["query"],
|
|
651
|
+
additionalProperties: false
|
|
652
|
+
});
|
|
653
|
+
const searchTool = ({ context, name, description, input, failure, search }) => ({
|
|
654
|
+
name,
|
|
655
|
+
description: description.join(" "),
|
|
656
|
+
input,
|
|
657
|
+
async execute(raw, ctx) {
|
|
658
|
+
const { data: formatted, error } = await safeAsync(() => search(raw));
|
|
659
|
+
if (error) return textOutput(failure + " failed: " + error.message);
|
|
660
|
+
await safeAsync(() => sendResult({
|
|
661
|
+
context,
|
|
662
|
+
sessionID: ctx.sessionID,
|
|
663
|
+
text: formatted
|
|
664
|
+
}));
|
|
665
|
+
return textOutput(formatted);
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
const createSearchTools = (context, deps) => [
|
|
669
|
+
searchTool({
|
|
670
|
+
context,
|
|
671
|
+
name: "brave_search",
|
|
672
|
+
description: [
|
|
673
|
+
"Search the web with Brave when you need current information from the internet.",
|
|
674
|
+
"The most reliable general web search here; prefer it over ddg_search when either would do.",
|
|
675
|
+
"Returns titles, URLs and snippets."
|
|
676
|
+
],
|
|
677
|
+
input: queryArg,
|
|
678
|
+
failure: "Brave search",
|
|
679
|
+
search: async (raw) => {
|
|
680
|
+
const items = await braveSearch(deps, queryInput.parse(raw).query);
|
|
681
|
+
return formatResults({
|
|
682
|
+
label: "Brave results",
|
|
683
|
+
items,
|
|
684
|
+
total: items.length,
|
|
685
|
+
renderItem: renderWebResult
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
}),
|
|
689
|
+
searchTool({
|
|
690
|
+
context,
|
|
691
|
+
name: "bsky_search",
|
|
692
|
+
description: [
|
|
693
|
+
"Search Bluesky posts for what people are saying about a topic right now.",
|
|
694
|
+
"Use for public reaction, discussion and opinion, not for documentation or reference material.",
|
|
695
|
+
"Returns author handle, post text, and like, repost and reply counts."
|
|
696
|
+
],
|
|
697
|
+
input: {
|
|
698
|
+
type: "object",
|
|
699
|
+
properties: {
|
|
700
|
+
query: {
|
|
701
|
+
type: "string",
|
|
702
|
+
description: "The search query"
|
|
703
|
+
},
|
|
704
|
+
limit: {
|
|
705
|
+
type: "number",
|
|
706
|
+
description: "Maximum number of results to return"
|
|
707
|
+
},
|
|
708
|
+
sort: {
|
|
709
|
+
type: "string",
|
|
710
|
+
description: "Sort order: top or latest"
|
|
711
|
+
}
|
|
712
|
+
},
|
|
713
|
+
required: ["query"],
|
|
714
|
+
additionalProperties: false
|
|
715
|
+
},
|
|
716
|
+
failure: "Bluesky search",
|
|
717
|
+
search: async (raw) => {
|
|
718
|
+
const options = bskyInput.parse(raw);
|
|
719
|
+
const { posts, hitsTotal } = await bskySearch(deps, options);
|
|
720
|
+
return formatResults({
|
|
721
|
+
label: "Bluesky results",
|
|
722
|
+
items: posts,
|
|
723
|
+
total: hitsTotal,
|
|
724
|
+
limit: options.limit,
|
|
725
|
+
offset: 0,
|
|
726
|
+
renderItem: renderBskyPost
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
}),
|
|
730
|
+
searchTool({
|
|
731
|
+
context,
|
|
732
|
+
name: "ddg_search",
|
|
733
|
+
description: [
|
|
734
|
+
"Search the web with DuckDuckGo when you need current information from the internet.",
|
|
735
|
+
"Use for general web queries: products, people, events, how-tos, anything not covered by a more specific tool.",
|
|
736
|
+
"Returns titles, URLs and snippets."
|
|
737
|
+
],
|
|
738
|
+
input: queryArg,
|
|
739
|
+
failure: "DuckDuckGo search",
|
|
740
|
+
search: async (raw) => {
|
|
741
|
+
const items = await ddgSearch(deps, queryInput.parse(raw).query);
|
|
742
|
+
return formatResults({
|
|
743
|
+
label: "DuckDuckGo results",
|
|
744
|
+
items,
|
|
745
|
+
total: items.length,
|
|
746
|
+
renderItem: renderDdgResult
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
}),
|
|
750
|
+
searchTool({
|
|
751
|
+
context,
|
|
752
|
+
name: "gnews_search",
|
|
753
|
+
description: [
|
|
754
|
+
"Search Google News for recent news coverage of a topic.",
|
|
755
|
+
"News and tech media only — not general web search, and not documentation.",
|
|
756
|
+
"Links are news.google.com redirect URLs that only open in a browser, not publisher URLs."
|
|
757
|
+
],
|
|
758
|
+
input: limitArg("Maximum number of results to return (default 20)"),
|
|
759
|
+
failure: "Google News search",
|
|
760
|
+
search: async (raw) => {
|
|
761
|
+
const { query, limit } = limitInput.parse(raw);
|
|
762
|
+
const items = await gnewsSearch(deps, query, limit);
|
|
763
|
+
return formatResults({
|
|
764
|
+
label: "Google News results",
|
|
765
|
+
items,
|
|
766
|
+
total: items.length,
|
|
767
|
+
renderItem: renderGnewsItem
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
}),
|
|
771
|
+
searchTool({
|
|
772
|
+
context,
|
|
773
|
+
name: "mdn_search",
|
|
774
|
+
description: [
|
|
775
|
+
"Look up web platform documentation on MDN: JavaScript, CSS, HTML, and browser APIs.",
|
|
776
|
+
"Use for how a web API behaves, its syntax, parameters or browser support.",
|
|
777
|
+
"Returns documentation pages with titles, URLs and summaries."
|
|
778
|
+
],
|
|
779
|
+
input: {
|
|
780
|
+
type: "object",
|
|
781
|
+
properties: {
|
|
782
|
+
query: {
|
|
783
|
+
type: "string",
|
|
784
|
+
description: "The search query"
|
|
785
|
+
},
|
|
786
|
+
limit: {
|
|
787
|
+
type: "number",
|
|
788
|
+
description: "Maximum number of results to return"
|
|
789
|
+
},
|
|
790
|
+
page: {
|
|
791
|
+
type: "number",
|
|
792
|
+
description: "Page number for pagination"
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
required: ["query"],
|
|
796
|
+
additionalProperties: false
|
|
797
|
+
},
|
|
798
|
+
failure: "MDN search",
|
|
799
|
+
search: async (raw) => {
|
|
800
|
+
const options = mdnInput.parse(raw);
|
|
801
|
+
const { documents, total } = await mdnSearch(deps, options);
|
|
802
|
+
return formatResults({
|
|
803
|
+
label: "MDN Web Docs results",
|
|
804
|
+
items: documents,
|
|
805
|
+
total,
|
|
806
|
+
limit: options.limit,
|
|
807
|
+
renderItem: renderMdnDoc
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
}),
|
|
811
|
+
searchTool({
|
|
812
|
+
context,
|
|
813
|
+
name: "rfc_search",
|
|
814
|
+
description: [
|
|
815
|
+
"Look up an IETF RFC by number or title.",
|
|
816
|
+
"Accepts a number like 9421, RFC9421 or 'rfc 9421' for a direct lookup, or words from the title to search.",
|
|
817
|
+
"Use for internet protocol specifications: HTTP, TCP, TLS, DNS, email, and so on.",
|
|
818
|
+
"Returns the RFC number, title, date, abstract, and a link to the full text."
|
|
819
|
+
],
|
|
820
|
+
input: {
|
|
821
|
+
type: "object",
|
|
822
|
+
properties: {
|
|
823
|
+
query: {
|
|
824
|
+
type: "string",
|
|
825
|
+
description: "An RFC number like 9421 or 'RFC9421', or words from the title"
|
|
826
|
+
},
|
|
827
|
+
limit: {
|
|
828
|
+
type: "number",
|
|
829
|
+
description: "Maximum number of results to return (default 20)"
|
|
830
|
+
}
|
|
831
|
+
},
|
|
832
|
+
required: ["query"],
|
|
833
|
+
additionalProperties: false
|
|
834
|
+
},
|
|
835
|
+
failure: "RFC search",
|
|
836
|
+
search: async (raw) => {
|
|
837
|
+
const { query, limit } = limitInput.parse(raw);
|
|
838
|
+
const items = await rfcSearch(deps, query, limit);
|
|
839
|
+
return formatResults({
|
|
840
|
+
label: "RFC results",
|
|
841
|
+
items,
|
|
842
|
+
total: items.length,
|
|
843
|
+
renderItem: renderRfc
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
}),
|
|
847
|
+
searchTool({
|
|
848
|
+
context,
|
|
849
|
+
name: "w3c_search",
|
|
850
|
+
description: [
|
|
851
|
+
"Look up a W3C specification by words in its title.",
|
|
852
|
+
"Use for web standards W3C publishes: CSS, WCAG accessibility, SVG, WebRTC, and similar.",
|
|
853
|
+
"Matches titles only, so query the spec name rather than a concept inside it.",
|
|
854
|
+
"Returns the specification title and URL."
|
|
855
|
+
],
|
|
856
|
+
input: limitArg("Maximum number of results to return (default 20)"),
|
|
857
|
+
failure: "W3C search",
|
|
858
|
+
search: async (raw) => {
|
|
859
|
+
const { query, limit } = limitInput.parse(raw);
|
|
860
|
+
const items = await w3cSearch(deps, query, limit);
|
|
861
|
+
return formatResults({
|
|
862
|
+
label: "W3C results",
|
|
863
|
+
items,
|
|
864
|
+
total: items.length,
|
|
865
|
+
renderItem: renderW3c
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
}),
|
|
869
|
+
searchTool({
|
|
870
|
+
context,
|
|
871
|
+
name: "whatwg_search",
|
|
872
|
+
description: [
|
|
873
|
+
"Look up a WHATWG living standard such as HTML, DOM, Fetch, URL or Streams.",
|
|
874
|
+
"Use for the current web platform specs WHATWG maintains, which supersede older W3C versions.",
|
|
875
|
+
"There are only 27 standards, so expect few results.",
|
|
876
|
+
"Returns the standard name, URL and description."
|
|
877
|
+
],
|
|
878
|
+
input: limitArg("Maximum number of results to return (default 20)"),
|
|
879
|
+
failure: "WHATWG search",
|
|
880
|
+
search: async (raw) => {
|
|
881
|
+
const { query, limit } = limitInput.parse(raw);
|
|
882
|
+
const items = await whatwgSearch(deps, query, limit);
|
|
883
|
+
return formatResults({
|
|
884
|
+
label: "WHATWG results",
|
|
885
|
+
items,
|
|
886
|
+
total: items.length,
|
|
887
|
+
renderItem: renderWhatwg
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
}),
|
|
891
|
+
searchTool({
|
|
892
|
+
context,
|
|
893
|
+
name: "wiki_search",
|
|
894
|
+
description: [
|
|
895
|
+
"Look up a subject on Wikipedia for background, definitions and general reference.",
|
|
896
|
+
"Use when the question is about what something IS rather than what is happening with it.",
|
|
897
|
+
"Returns page titles, descriptions and excerpts."
|
|
898
|
+
],
|
|
899
|
+
input: limitArg("Maximum number of results to return (1-100)"),
|
|
900
|
+
failure: "Wikipedia search",
|
|
901
|
+
search: async (raw) => {
|
|
902
|
+
const options = limitInput.parse(raw);
|
|
903
|
+
const { pages } = await wikiSearch(deps, options);
|
|
904
|
+
return formatResults({
|
|
905
|
+
label: "Wikipedia results",
|
|
906
|
+
items: pages,
|
|
907
|
+
total: pages.length,
|
|
908
|
+
limit: options.limit,
|
|
909
|
+
offset: 0,
|
|
910
|
+
renderItem: renderWikiPage
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
})
|
|
914
|
+
];
|
|
915
|
+
// src/plugin.ts
|
|
916
|
+
const createSetup = (deps) => {
|
|
917
|
+
return async (context) => {
|
|
918
|
+
await context.tool.transform((tools) => {
|
|
919
|
+
for (const definition of createSearchTools(context, deps)) tools.add(definition);
|
|
920
|
+
});
|
|
921
|
+
};
|
|
922
|
+
};
|
|
923
|
+
// src/v1-message.ts
|
|
924
|
+
const V1_MESSAGE = [
|
|
925
|
+
"opencode-search 1.x requires OpenCode 2 and cannot load under OpenCode 1.",
|
|
926
|
+
"",
|
|
927
|
+
"Either pin the last OpenCode 1 release, which will not be updated:",
|
|
928
|
+
"",
|
|
929
|
+
" { \"plugin\": [\"opencode-search@0.0.9\"] }",
|
|
930
|
+
"",
|
|
931
|
+
"or upgrade to OpenCode 2 (https://opencode.ai/v2/docs) and use the \"plugins\" key."
|
|
932
|
+
].join("\n");
|
|
933
|
+
// search.ts
|
|
934
|
+
const { data: plugin, error } = await safeAsync(() => import("@opencode/plugin"));
|
|
935
|
+
if (error || typeof plugin.Plugin?.define !== "function") throw new Error(V1_MESSAGE, { cause: error ?? /* @__PURE__ */ new Error("Plugin.define is not exported") });
|
|
936
|
+
const cache = createCache({
|
|
937
|
+
file: fileAt(join(dirname(fileURLToPath(import.meta.url)), ".cache")),
|
|
938
|
+
now: Date.now
|
|
939
|
+
});
|
|
940
|
+
var search_default = plugin.Plugin.define({
|
|
941
|
+
id: "opencode-search",
|
|
942
|
+
setup: createSetup({
|
|
943
|
+
fetch,
|
|
944
|
+
cache
|
|
945
|
+
})
|
|
946
|
+
});
|
|
947
|
+
export { search_default as default };
|