@fffangchenghao/dsh-web-search 0.19.2 → 0.20.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.
Files changed (60) hide show
  1. package/CHANGELOG.md +160 -0
  2. package/README.md +195 -34
  3. package/README.zh-CN.md +171 -34
  4. package/SECURITY.md +42 -7
  5. package/client.js +131 -9
  6. package/docs/store-evidence.md +10 -5
  7. package/lib/advanced-search.d.ts +107 -0
  8. package/lib/advanced-search.js +255 -0
  9. package/lib/bridge.d.ts +16 -0
  10. package/lib/bridge.js +160 -0
  11. package/lib/cache.d.ts +11 -4
  12. package/lib/cache.js +9 -5
  13. package/lib/config.d.ts +19 -3
  14. package/lib/config.js +24 -0
  15. package/lib/domain-tool.d.ts +5 -0
  16. package/lib/domain-tool.js +45 -6
  17. package/lib/engine-test.d.ts +116 -0
  18. package/lib/engine-test.js +256 -0
  19. package/lib/engines.d.ts +11 -1
  20. package/lib/engines.js +122 -20
  21. package/lib/errors.d.ts +16 -2
  22. package/lib/errors.js +40 -1
  23. package/lib/index.d.ts +24 -2
  24. package/lib/index.js +304 -20
  25. package/lib/inject.d.ts +43 -0
  26. package/lib/inject.js +1 -0
  27. package/lib/markdown.js +18 -1
  28. package/lib/mirrors.d.ts +23 -0
  29. package/lib/mirrors.js +69 -0
  30. package/lib/official-feed.d.ts +114 -0
  31. package/lib/official-feed.js +288 -0
  32. package/lib/official-support.d.ts +74 -0
  33. package/lib/official-support.js +134 -0
  34. package/lib/platform-tool.d.ts +77 -0
  35. package/lib/platform-tool.js +173 -0
  36. package/lib/platforms.d.ts +178 -0
  37. package/lib/platforms.js +1035 -0
  38. package/lib/query-router.d.ts +29 -0
  39. package/lib/query-router.js +58 -0
  40. package/lib/read-page.js +2 -1
  41. package/lib/research-cache.d.ts +115 -0
  42. package/lib/research-cache.js +275 -0
  43. package/lib/research.d.ts +45 -4
  44. package/lib/research.js +505 -22
  45. package/lib/search-many.d.ts +7 -1
  46. package/lib/search-many.js +7 -2
  47. package/lib/system-prompt.d.ts +27 -0
  48. package/lib/system-prompt.js +50 -0
  49. package/lib/tool-suite.d.ts +97 -0
  50. package/lib/tool-suite.js +224 -0
  51. package/lib/transport.d.ts +33 -2
  52. package/lib/transport.js +42 -9
  53. package/lib/types.d.ts +20 -0
  54. package/lib/types.js +9 -1
  55. package/lib/untrusted.d.ts +33 -0
  56. package/lib/untrusted.js +42 -0
  57. package/lib/verticals.d.ts +3 -1
  58. package/lib/verticals.js +26 -7
  59. package/package.json +5 -3
  60. package/skills/dsh-web-search/SKILL.md +21 -6
@@ -0,0 +1,1035 @@
1
+ import { SearchError } from "./errors.js";
2
+ import { parsePublishedAt, publishedTimestamp } from "./freshness.js";
3
+ import { decodeEntities } from "./markdown.js";
4
+ import { proxyGet } from "./proxy.js";
5
+ /** Every supported platform, in the order results are interleaved. */
6
+ export const PLATFORM_IDS = Object.freeze([
7
+ 'bilibili',
8
+ 'v2ex',
9
+ 'reddit',
10
+ 'youtube',
11
+ 'zhihu',
12
+ 'juejin',
13
+ ]);
14
+ /** Human labels, used in notes and in the model-facing tool description. */
15
+ export const PLATFORM_LABELS = Object.freeze({
16
+ bilibili: 'Bilibili',
17
+ v2ex: 'V2EX',
18
+ reddit: 'Reddit',
19
+ youtube: 'YouTube',
20
+ zhihu: '知乎',
21
+ juejin: '掘金',
22
+ });
23
+ /** Results wanted when the caller names no limit. */
24
+ export const DEFAULT_PLATFORM_LIMIT = 5;
25
+ /** Upper bound a single call may request. */
26
+ export const MAX_PLATFORM_LIMIT = 10;
27
+ /** Per-platform deadline when the caller names no timeout. */
28
+ const DEFAULT_TIMEOUT_MS = 8000;
29
+ /** Longest snippet kept per source. */
30
+ const SNIPPET_LIMIT = 300;
31
+ /** Browser user agent: Bilibili and YouTube reject the plugin's own. */
32
+ const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36';
33
+ const ACCEPT_LANGUAGE = 'zh-CN,zh;q=0.9,en;q=0.8';
34
+ /** True for a value usable as a platform id. */
35
+ export function isPlatformId(value) {
36
+ return typeof value === 'string' && PLATFORM_IDS.includes(value);
37
+ }
38
+ /** The direct route for every platform, keyed by id. */
39
+ const SPECS = {
40
+ bilibili: {
41
+ id: 'bilibili',
42
+ site: 'bilibili.com',
43
+ accept: 'application/json, text/plain, */*',
44
+ headers: { referer: 'https://www.bilibili.com/' },
45
+ build: (query) => `https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword=${encodeURIComponent(query)}&page=1`,
46
+ alternateBuild: (query) => `https://api.bilibili.com/x/web-interface/search/all/v2?keyword=${encodeURIComponent(query)}&page=1`,
47
+ parse: (payload, query, limit) => parseBilibili(payload, limit, query),
48
+ },
49
+ v2ex: {
50
+ id: 'v2ex',
51
+ site: 'v2ex.com',
52
+ accept: 'application/json',
53
+ build: () => 'https://www.v2ex.com/api/topics/hot.json',
54
+ parse: (payload, query, limit) => parseV2ex(payload, query, limit),
55
+ },
56
+ reddit: {
57
+ id: 'reddit',
58
+ site: 'reddit.com',
59
+ accept: 'application/json',
60
+ acceptLanguage: 'en-US,en;q=0.9',
61
+ build: (query, limit) => `https://www.reddit.com/search.json?q=${encodeURIComponent(query)}&limit=${limit}&sort=relevance&raw_json=1`,
62
+ parse: (payload, _query, limit) => parseReddit(payload, limit),
63
+ },
64
+ youtube: {
65
+ id: 'youtube',
66
+ site: 'youtube.com',
67
+ accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
68
+ acceptLanguage: 'en-US,en;q=0.9',
69
+ build: (query) => `https://www.youtube.com/results?search_query=${encodeURIComponent(query)}&hl=en`,
70
+ parse: (payload, _query, limit) => parseYoutube(payload, limit),
71
+ },
72
+ zhihu: {
73
+ id: 'zhihu',
74
+ site: 'zhihu.com',
75
+ accept: 'application/json',
76
+ fallbackOnly: true,
77
+ },
78
+ juejin: {
79
+ id: 'juejin',
80
+ site: 'juejin.cn',
81
+ accept: 'application/json',
82
+ headers: { referer: 'https://juejin.cn/' },
83
+ build: (query, limit) => `https://api.juejin.cn/search_api/v1/search?query=${encodeURIComponent(query)}&id_type=0&cursor=0&limit=${limit}`,
84
+ parse: (payload, _query, limit) => parseJuejin(payload, limit),
85
+ },
86
+ };
87
+ /** True for a value usable as a JSON record. */
88
+ function isRecord(value) {
89
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
90
+ }
91
+ /** Read one optional record. */
92
+ function asRecord(value) {
93
+ return isRecord(value) ? value : undefined;
94
+ }
95
+ /** Read a non-empty string. */
96
+ function stringValue(value) {
97
+ return typeof value === 'string' && value.trim().length > 0 ? value : undefined;
98
+ }
99
+ /** Read a finite number, accepting the numeric strings these APIs sometimes send. */
100
+ function numberValue(value) {
101
+ const parsed = typeof value === 'number' ? value : typeof value === 'string' && value.trim().length > 0 ? Number(value) : Number.NaN;
102
+ return Number.isFinite(parsed) ? parsed : undefined;
103
+ }
104
+ /** Read an id that may be a string or a number. */
105
+ function identifier(value) {
106
+ if (typeof value === 'string' && value.trim().length > 0)
107
+ return value.trim();
108
+ if (typeof value === 'number' && Number.isFinite(value))
109
+ return String(value);
110
+ return undefined;
111
+ }
112
+ /** True when a payload is markup rather than the JSON the endpoint promised. */
113
+ function looksLikeHtml(payload) {
114
+ return payload.trimStart().startsWith('<');
115
+ }
116
+ /**
117
+ * Strip markup and decode entities without breaking words that a highlight tag
118
+ * split apart: `<em>鸣潮</em>` is `鸣潮`, not ` 鸣潮 `. Block boundaries become
119
+ * a space so a description that used `<br>` keeps its words apart.
120
+ * @param value - raw field from the API.
121
+ * @returns plain single-line text.
122
+ */
123
+ function cleanMarkup(value) {
124
+ if (typeof value !== 'string')
125
+ return '';
126
+ return decodeEntities(value
127
+ .replace(/<br\s*\/?>|<\/(?:p|div|li|h[1-6]|tr|blockquote)>/gi, ' ')
128
+ .replace(/<[^>]*>/g, ''))
129
+ .replace(/\s+/g, ' ')
130
+ .trim();
131
+ }
132
+ /**
133
+ * Parse a JSON payload, turning an anti-bot page into an actionable reason.
134
+ * @param payload - response body.
135
+ * @returns the parsed JSON value.
136
+ * @throws `Error` when the body is HTML or is not valid JSON.
137
+ */
138
+ function readJson(payload) {
139
+ try {
140
+ return JSON.parse(payload);
141
+ }
142
+ catch {
143
+ throw new Error(looksLikeHtml(payload) ? 'returned HTML (anti-bot)' : 'invalid JSON');
144
+ }
145
+ }
146
+ /** Render a thrown value as one line. */
147
+ function messageOf(error) {
148
+ if (error instanceof Error) {
149
+ return error.message.length > 0 ? error.message : error.name.length > 0 ? error.name : 'request failed';
150
+ }
151
+ return String(error);
152
+ }
153
+ /** Prefix a reason with its platform id exactly once. */
154
+ function failureText(id, reason) {
155
+ return reason.startsWith(`${id}: `) ? reason : `${id}: ${reason}`;
156
+ }
157
+ /** Convert an epoch-seconds or epoch-milliseconds value to ISO 8601. */
158
+ function isoFromEpoch(value) {
159
+ const raw = numberValue(value);
160
+ if (raw === undefined || raw <= 0)
161
+ return undefined;
162
+ const milliseconds = raw > 1_000_000_000_000 ? raw : raw * 1000;
163
+ const date = new Date(milliseconds);
164
+ if (Number.isNaN(date.getTime()))
165
+ return undefined;
166
+ const year = date.getUTCFullYear();
167
+ // Outside this range the field is almost certainly not a timestamp.
168
+ return year >= 2000 && year <= 2100 ? date.toISOString() : undefined;
169
+ }
170
+ /** Normalise a date string to ISO 8601. */
171
+ function isoDate(value) {
172
+ const raw = stringValue(value);
173
+ if (raw === undefined)
174
+ return undefined;
175
+ const at = Date.parse(raw);
176
+ return Number.isNaN(at) ? undefined : new Date(at).toISOString();
177
+ }
178
+ /** Resolve a value to an absolute http(s) URL, dropping anything else. */
179
+ function absoluteHttpUrl(value, base) {
180
+ const raw = stringValue(value);
181
+ if (raw === undefined)
182
+ return undefined;
183
+ const candidate = raw.startsWith('//') ? `https:${raw}` : raw;
184
+ try {
185
+ const parsed = new URL(candidate, base);
186
+ return parsed.protocol === 'http:' || parsed.protocol === 'https:' ? parsed.toString() : undefined;
187
+ }
188
+ catch {
189
+ return undefined;
190
+ }
191
+ }
192
+ /** Bound a snippet to the documented length. */
193
+ function boundSnippet(value) {
194
+ return value.length > SNIPPET_LIMIT ? value.slice(0, SNIPPET_LIMIT).trimEnd() : value;
195
+ }
196
+ /** Validate, clean and bound one source; `undefined` when it is unusable. */
197
+ function cleanSource(raw, base) {
198
+ const url = absoluteHttpUrl(raw.url, base);
199
+ if (url === undefined)
200
+ return undefined;
201
+ const title = cleanMarkup(raw.title);
202
+ const snippet = boundSnippet(cleanMarkup(raw.snippet));
203
+ const publishedAt = isoDate(raw.publishedAt);
204
+ return {
205
+ url,
206
+ ...(title.length > 0 ? { title } : {}),
207
+ ...(snippet.length > 0 ? { snippet } : {}),
208
+ ...(publishedAt === undefined ? {} : { publishedAt }),
209
+ };
210
+ }
211
+ /** Hosts that legitimately redirect to a platform's canonical domain. */
212
+ const PLATFORM_ALIASES = {
213
+ 'bilibili.com': ['b23.tv'],
214
+ 'youtube.com': ['youtu.be'],
215
+ 'reddit.com': ['redd.it'],
216
+ };
217
+ /** True when a fallback row actually belongs to the platform's site. */
218
+ function isPlatformSource(url, site) {
219
+ try {
220
+ const host = new URL(url).hostname.toLowerCase();
221
+ const allowed = [site, ...(PLATFORM_ALIASES[site] ?? [])];
222
+ return allowed.some((domain) => host === domain || host.endsWith(`.${domain}`));
223
+ }
224
+ catch {
225
+ return false;
226
+ }
227
+ }
228
+ /** Canonical comparison key: fragments and a trailing slash do not make a new row. */
229
+ function urlKey(url) {
230
+ try {
231
+ const parsed = new URL(url);
232
+ parsed.hash = '';
233
+ return parsed.toString().replace(/\/$/, '');
234
+ }
235
+ catch {
236
+ return url;
237
+ }
238
+ }
239
+ /** Clean a list, drop duplicate URLs and cap it. */
240
+ function normaliseSources(raw, limit, base) {
241
+ const sources = [];
242
+ const seen = new Set();
243
+ for (const item of raw) {
244
+ if (typeof item !== 'object' || item === null)
245
+ continue;
246
+ const source = cleanSource(item, base);
247
+ if (source === undefined)
248
+ continue;
249
+ const key = urlKey(source.url);
250
+ if (seen.has(key))
251
+ continue;
252
+ seen.add(key);
253
+ sources.push(source);
254
+ if (sources.length >= limit)
255
+ break;
256
+ }
257
+ return sources;
258
+ }
259
+ /** Clamp a caller limit into the accepted range. */
260
+ function clampLimit(value) {
261
+ if (typeof value !== 'number' || !Number.isFinite(value))
262
+ return DEFAULT_PLATFORM_LIMIT;
263
+ return Math.max(1, Math.min(MAX_PLATFORM_LIMIT, Math.floor(value)));
264
+ }
265
+ /** Combine the caller signal with this request's own deadline. */
266
+ function deadline(timeoutMs, signal) {
267
+ const timer = AbortSignal.timeout(timeoutMs);
268
+ return signal === undefined ? timer : AbortSignal.any([signal, timer]);
269
+ }
270
+ /** Turn a non-2xx status into a reason. */
271
+ function statusError(status) {
272
+ return new Error(status === 0 ? 'no response' : `HTTP ${status}`);
273
+ }
274
+ /** Turn a thrown transport failure into a reason, keeping timeouts readable. */
275
+ function transportError(error, timeoutMs) {
276
+ const name = error instanceof Error ? error.name : '';
277
+ const message = messageOf(error);
278
+ if (name === 'TimeoutError' || name === 'AbortError' || /timed out|timeout/i.test(message)) {
279
+ return new Error(`timed out after ${timeoutMs}ms`);
280
+ }
281
+ return new Error(message);
282
+ }
283
+ /**
284
+ * Fetch one direct payload, directly or through the configured tunnel.
285
+ * @param url - absolute request URL.
286
+ * @param spec - route whose headers are used.
287
+ * @param options - proxy, fetch seam, cancellation.
288
+ * @param timeoutMs - per-platform deadline.
289
+ * @returns the response body.
290
+ * @throws `Error` on a transport failure or a non-2xx answer.
291
+ */
292
+ async function requestText(url, spec, options, timeoutMs) {
293
+ const headers = {
294
+ 'user-agent': USER_AGENT,
295
+ accept: spec.accept,
296
+ 'accept-language': spec.acceptLanguage ?? ACCEPT_LANGUAGE,
297
+ ...spec.headers,
298
+ };
299
+ if (options.proxy !== undefined && options.proxy.length > 0) {
300
+ const response = await proxyGet(url, {
301
+ proxy: options.proxy,
302
+ headers,
303
+ timeoutMs,
304
+ ...(options.signal === undefined ? {} : { signal: options.signal }),
305
+ });
306
+ if (response.status < 200 || response.status >= 300)
307
+ throw statusError(response.status);
308
+ return response.text;
309
+ }
310
+ const send = options.fetch ?? fetch;
311
+ let response;
312
+ try {
313
+ response = await send(url, {
314
+ headers,
315
+ redirect: 'follow',
316
+ signal: deadline(timeoutMs, options.signal),
317
+ });
318
+ }
319
+ catch (error) {
320
+ if (options.signal?.aborted === true)
321
+ throw error;
322
+ throw transportError(error, timeoutMs);
323
+ }
324
+ if (!response.ok)
325
+ throw statusError(response.status);
326
+ try {
327
+ return await response.text();
328
+ }
329
+ catch (error) {
330
+ if (options.signal?.aborted === true)
331
+ throw error;
332
+ throw transportError(error, timeoutMs);
333
+ }
334
+ }
335
+ /* -------------------------------------------------------------------------- */
336
+ /* Parsers */
337
+ /* -------------------------------------------------------------------------- */
338
+ /**
339
+ * Collect Bilibili video rows from either `search/type` (`data.result` is the
340
+ * row list) or `search/all/v2` (each section carries its own `data` list).
341
+ * @param value - candidate node.
342
+ * @param out - accumulator.
343
+ */
344
+ function collectBilibiliRows(value, out) {
345
+ if (Array.isArray(value)) {
346
+ for (const item of value)
347
+ collectBilibiliRows(item, out);
348
+ return;
349
+ }
350
+ if (!isRecord(value))
351
+ return;
352
+ if (typeof value['arcurl'] === 'string' || typeof value['bvid'] === 'string') {
353
+ out.push(value);
354
+ return;
355
+ }
356
+ if (Array.isArray(value['data']))
357
+ collectBilibiliRows(value['data'], out);
358
+ }
359
+ /**
360
+ * Parse Bilibili's video search envelope.
361
+ *
362
+ * Row-level drift yields fewer sources; an anti-bot envelope (HTML or a
363
+ * non-zero `code`) throws so the caller knows to fall back.
364
+ *
365
+ * @param payload - `api.bilibili.com/x/web-interface/search/type` JSON.
366
+ * @param limit - rows to keep.
367
+ * @returns video sources in API order.
368
+ * @throws `Error` on a wall or an unparsable envelope.
369
+ */
370
+ export function parseBilibili(payload, limit = DEFAULT_PLATFORM_LIMIT, query = '') {
371
+ const root = asRecord(readJson(payload));
372
+ if (root === undefined)
373
+ throw new Error('unexpected response shape');
374
+ const code = numberValue(root['code']) ?? 0;
375
+ if (code !== 0) {
376
+ const message = cleanMarkup(root['message'] ?? root['msg']);
377
+ throw new Error(`API code ${code}${message.length > 0 ? `: ${message}` : ''}`);
378
+ }
379
+ const rows = [];
380
+ collectBilibiliRows(asRecord(root['data'])?.['result'], rows);
381
+ const sources = [];
382
+ for (const row of rows) {
383
+ const bvid = stringValue(row['bvid']);
384
+ const url = absoluteHttpUrl(row['arcurl']) ??
385
+ (bvid === undefined ? undefined : `https://www.bilibili.com/video/${encodeURIComponent(bvid)}`);
386
+ if (url === undefined)
387
+ continue;
388
+ const author = cleanMarkup(row['author'] ?? row['up_name']);
389
+ const description = cleanMarkup(row['description'] ?? row['desc']);
390
+ const play = numberValue(row['play']);
391
+ const duration = cleanMarkup(row['duration']);
392
+ const facts = [
393
+ author.length > 0 ? `UP主 ${author}` : undefined,
394
+ play === undefined ? undefined : `播放 ${play}`,
395
+ duration.length > 0 ? `时长 ${duration}` : undefined,
396
+ ].filter((part) => part !== undefined);
397
+ const snippet = description.length > 0
398
+ ? (facts.length > 0 ? `${facts.join(' · ')} · ${description}` : description)
399
+ : facts.join(' · ');
400
+ const source = cleanSource({
401
+ url,
402
+ title: cleanMarkup(row['title']) || bvid,
403
+ snippet,
404
+ publishedAt: isoFromEpoch(row['pubdate']),
405
+ });
406
+ if (source !== undefined)
407
+ sources.push(source);
408
+ }
409
+ return normaliseBilibiliSources(normaliseSources(sources, clampLimit(limit * 3)), query).slice(0, clampLimit(limit));
410
+ }
411
+ /** Query tokens for a light relevance gate on Bilibili's broad search. */
412
+ function bilibiliQueryTokens(query) {
413
+ const text = query.toLowerCase();
414
+ const tokens = new Set();
415
+ for (const word of text.match(/[a-z][a-z0-9.+#-]{2,}/gu) ?? [])
416
+ tokens.add(word);
417
+ for (const run of text.match(/[\u4e00-\u9fff]{2,}/gu) ?? []) {
418
+ if (run.length === 2)
419
+ tokens.add(run);
420
+ else
421
+ for (let index = 0; index < run.length - 1; index += 1)
422
+ tokens.add(run.slice(index, index + 2));
423
+ }
424
+ return [...tokens];
425
+ }
426
+ /**
427
+ * Bilibili's relevance search sometimes returns an adjacent topic (a Python
428
+ * async video for a Rust async query, or a Spring `@Async` video). Keep rows
429
+ * that carry every Latin term and at least two distinct query tokens; when the
430
+ * gate would empty the list, return the original rows so the caller can still
431
+ * decide rather than seeing a fabricated empty answer.
432
+ */
433
+ function normaliseBilibiliSources(sources, query, now = Date.now()) {
434
+ const tokens = bilibiliQueryTokens(query);
435
+ if (tokens.length === 0)
436
+ return sources;
437
+ const latin = tokens.filter((token) => /^[a-z]/u.test(token));
438
+ // When the query names a narrow topic (`async`, `异步`), a row that merely
439
+ // says "programming" is not the same topic. This removes a Goose-language
440
+ // news video or an Axum backend course from a Rust-async query without
441
+ // requiring every related video to repeat the exact token.
442
+ const topics = tokens.filter((token) => /异步|async|并发|concurr|tokio/iu.test(token));
443
+ const filtered = sources.filter((source) => {
444
+ const haystack = `${source.title ?? ''} ${source.snippet ?? ''}`.toLowerCase();
445
+ if (!latin.every((token) => haystack.includes(token)))
446
+ return false;
447
+ if (topics.length > 0 && !topics.some((token) => haystack.includes(token)))
448
+ return false;
449
+ const hits = tokens.filter((token) => haystack.includes(token)).length;
450
+ return hits >= Math.min(2, tokens.length);
451
+ });
452
+ if (filtered.length === 0)
453
+ return sources;
454
+ // The API returns relevance order, not recency or popularity. A question
455
+ // that asks for "最近半年" or "播放量较高" should say so; only then do we
456
+ // apply a bounded filter/sort rather than silently reordering every query.
457
+ const wantsRecent = /最近半年|近半年|过去半年|last six months|recent|今年|2026/iu.test(query);
458
+ const wantsPopular = /播放量|高播放|最热|热门|popular|most viewed|views?/iu.test(query);
459
+ let rows = filtered;
460
+ if (wantsRecent) {
461
+ const cutoff = now - 183 * 24 * 60 * 60 * 1_000;
462
+ rows = rows.filter((source) => {
463
+ const at = publishedTimestamp(source.publishedAt);
464
+ return at === undefined || at >= cutoff;
465
+ });
466
+ if (rows.length === 0)
467
+ return [];
468
+ }
469
+ if (wantsPopular) {
470
+ const plays = (source) => {
471
+ const match = /播放\s+(\d+)/u.exec(source.snippet ?? '');
472
+ return match?.[1] === undefined ? -1 : Number.parseInt(match[1], 10);
473
+ };
474
+ rows = [...rows].sort((left, right) => plays(right) - plays(left));
475
+ }
476
+ return rows;
477
+ }
478
+ /** Recursively collect every `videoRenderer` object in YouTube's page data. */
479
+ function collectVideoRenderers(value, out) {
480
+ if (Array.isArray(value)) {
481
+ for (const item of value)
482
+ collectVideoRenderers(item, out);
483
+ return;
484
+ }
485
+ if (!isRecord(value))
486
+ return;
487
+ const renderer = value['videoRenderer'];
488
+ if (isRecord(renderer))
489
+ out.push(renderer);
490
+ for (const key of Object.keys(value)) {
491
+ if (key === 'videoRenderer')
492
+ continue;
493
+ const child = value[key];
494
+ if (typeof child === 'object' && child !== null)
495
+ collectVideoRenderers(child, out);
496
+ }
497
+ }
498
+ /** Read `{ simpleText }`/`{ runs: [{ text }] }` as one line. */
499
+ function inlineText(value) {
500
+ const record = asRecord(value);
501
+ if (record === undefined)
502
+ return '';
503
+ const simple = stringValue(record['simpleText']);
504
+ if (simple !== undefined)
505
+ return simple;
506
+ const runs = record['runs'];
507
+ if (!Array.isArray(runs))
508
+ return '';
509
+ return runs
510
+ .map((run) => cleanMarkup(asRecord(run)?.['text']))
511
+ .filter((part) => part.length > 0)
512
+ .join('');
513
+ }
514
+ /** Extract the balanced JSON object that follows `ytInitialData`. */
515
+ function youtubeInitialData(payload) {
516
+ const marker = /ytInitialData["'\]\s]*=\s*\{/.exec(payload);
517
+ if (marker === null)
518
+ return undefined;
519
+ const start = marker.index + marker[0].lastIndexOf('{');
520
+ return sliceBalancedObject(payload, start);
521
+ }
522
+ /** Slice one balanced `{...}` object, skipping braces inside strings. */
523
+ function sliceBalancedObject(textValue, start) {
524
+ let depth = 0;
525
+ let inString = false;
526
+ let escaped = false;
527
+ for (let index = start; index < textValue.length; index += 1) {
528
+ const character = textValue[index];
529
+ if (inString) {
530
+ if (escaped)
531
+ escaped = false;
532
+ else if (character === '\\')
533
+ escaped = true;
534
+ else if (character === '"')
535
+ inString = false;
536
+ continue;
537
+ }
538
+ if (character === '"')
539
+ inString = true;
540
+ else if (character === '{')
541
+ depth += 1;
542
+ else if (character === '}') {
543
+ depth -= 1;
544
+ if (depth === 0)
545
+ return textValue.slice(start, index + 1);
546
+ }
547
+ }
548
+ return undefined;
549
+ }
550
+ /** Expand YouTube's compact relative stamps (`1d ago`) for the shared parser. */
551
+ function youtubePublishedAt(value) {
552
+ const units = {
553
+ s: 'second',
554
+ m: 'minute',
555
+ h: 'hour',
556
+ d: 'day',
557
+ w: 'week',
558
+ mo: 'month',
559
+ y: 'year',
560
+ };
561
+ const expanded = value.replace(/\b(\d+)\s*(mo|[smhdwy])\s+ago\b/gi, (_whole, amount, unit) => `${amount} ${units[unit.toLowerCase()] ?? 'day'} ago`);
562
+ return parsePublishedAt(expanded);
563
+ }
564
+ /**
565
+ * Parse a YouTube results page through the embedded `ytInitialData` graph.
566
+ *
567
+ * `videoRenderer` is looked up at any depth because YouTube reshuffles the
568
+ * section wrappers between experiments; the marker and the JSON are the only
569
+ * two things the parser insists on.
570
+ *
571
+ * @param payload - `/results?search_query=` HTML.
572
+ * @param limit - rows to keep.
573
+ * @returns video sources in page order.
574
+ * @throws `Error` when the page embeds no parsable `ytInitialData`.
575
+ */
576
+ export function parseYoutube(payload, limit = DEFAULT_PLATFORM_LIMIT) {
577
+ const json = youtubeInitialData(payload);
578
+ if (json === undefined)
579
+ throw new Error('ytInitialData not found');
580
+ let root;
581
+ try {
582
+ root = JSON.parse(json);
583
+ }
584
+ catch {
585
+ throw new Error('invalid ytInitialData JSON');
586
+ }
587
+ const renderers = [];
588
+ collectVideoRenderers(root, renderers);
589
+ const sources = [];
590
+ for (const renderer of renderers) {
591
+ const videoId = stringValue(renderer['videoId']);
592
+ if (videoId === undefined)
593
+ continue;
594
+ const owner = inlineText(renderer['ownerText'] ?? renderer['longBylineText']);
595
+ const length = inlineText(renderer['lengthText']);
596
+ const views = inlineText(renderer['viewCountText']);
597
+ const published = inlineText(renderer['publishedTimeText']);
598
+ const snippet = [owner, length, views, published].filter((part) => part.length > 0).join(' · ');
599
+ const source = cleanSource({
600
+ url: `https://www.youtube.com/watch?v=${encodeURIComponent(videoId)}`,
601
+ title: inlineText(renderer['title']),
602
+ snippet,
603
+ publishedAt: youtubePublishedAt(published),
604
+ });
605
+ if (source !== undefined)
606
+ sources.push(source);
607
+ }
608
+ return normaliseSources(sources, clampLimit(limit));
609
+ }
610
+ /**
611
+ * Parse a Reddit search listing.
612
+ *
613
+ * The permalink is preferred over the external target so a post's citation
614
+ * points at the discussion; `selftext` becomes the snippet, with a
615
+ * subreddit/score line used when the post is link-only.
616
+ *
617
+ * @param payload - `reddit.com/search.json` JSON.
618
+ * @param limit - rows to keep.
619
+ * @returns post sources in listing order.
620
+ * @throws `Error` on a wall, invalid JSON or an unexpected envelope.
621
+ */
622
+ export function parseReddit(payload, limit = DEFAULT_PLATFORM_LIMIT) {
623
+ const root = asRecord(readJson(payload));
624
+ const children = asRecord(root?.['data'])?.['children'];
625
+ if (!Array.isArray(children))
626
+ throw new Error('unexpected response shape');
627
+ const sources = [];
628
+ for (const child of children) {
629
+ const post = asRecord(asRecord(child)?.['data']);
630
+ if (post === undefined)
631
+ continue;
632
+ const permalink = stringValue(post['permalink']);
633
+ const url = (permalink === undefined ? undefined : absoluteHttpUrl(permalink, 'https://www.reddit.com')) ??
634
+ absoluteHttpUrl(post['url']);
635
+ if (url === undefined)
636
+ continue;
637
+ const subreddit = cleanMarkup(post['subreddit_name_prefixed'] ?? post['subreddit']);
638
+ const score = numberValue(post['score']);
639
+ const comments = numberValue(post['num_comments']);
640
+ const meta = [
641
+ subreddit,
642
+ score === undefined ? '' : `${score} points`,
643
+ comments === undefined ? '' : `${comments} comments`,
644
+ ]
645
+ .filter((part) => part.length > 0)
646
+ .join(' · ');
647
+ const source = cleanSource({
648
+ url,
649
+ title: cleanMarkup(post['title']),
650
+ snippet: cleanMarkup(post['selftext']) || meta,
651
+ publishedAt: isoFromEpoch(post['created_utc']),
652
+ });
653
+ if (source !== undefined)
654
+ sources.push(source);
655
+ }
656
+ return normaliseSources(sources, clampLimit(limit));
657
+ }
658
+ /** Recursively collect Juejin article info objects, tolerating envelope drift. */
659
+ function collectJuejinArticles(value, out) {
660
+ if (Array.isArray(value)) {
661
+ for (const item of value)
662
+ collectJuejinArticles(item, out);
663
+ return;
664
+ }
665
+ if (!isRecord(value))
666
+ return;
667
+ const articleId = identifier(value['article_id']);
668
+ if (articleId !== undefined) {
669
+ out.push(value);
670
+ return;
671
+ }
672
+ const direct = asRecord(value['article_info']);
673
+ if (direct !== undefined && identifier(direct['article_id']) !== undefined) {
674
+ out.push(direct);
675
+ return;
676
+ }
677
+ const model = asRecord(value['result_model']);
678
+ if (model !== undefined) {
679
+ const info = asRecord(model['article_info']);
680
+ if (info !== undefined && identifier(info['article_id']) !== undefined) {
681
+ out.push(info);
682
+ return;
683
+ }
684
+ collectJuejinArticles(model, out);
685
+ return;
686
+ }
687
+ if (Array.isArray(value['data']))
688
+ collectJuejinArticles(value['data'], out);
689
+ }
690
+ /**
691
+ * Parse Juejin's article search envelope.
692
+ *
693
+ * The documented shape is `data[].result_model.article_info`; the collector
694
+ * also accepts a bare `article_info` or an unwrapped `data` array because the
695
+ * search API has changed wrappers before.
696
+ *
697
+ * @param payload - `api.juejin.cn/search_api/v1/search` JSON.
698
+ * @param limit - rows to keep.
699
+ * @returns article sources in API order.
700
+ * @throws `Error` on a wall, invalid JSON or an API error code.
701
+ */
702
+ export function parseJuejin(payload, limit = DEFAULT_PLATFORM_LIMIT) {
703
+ const root = asRecord(readJson(payload));
704
+ if (root === undefined)
705
+ throw new Error('unexpected response shape');
706
+ const errorNumber = numberValue(root['err_no']);
707
+ if (errorNumber !== undefined && errorNumber !== 0) {
708
+ const message = cleanMarkup(root['err_msg']);
709
+ throw new Error(`API err_no ${errorNumber}${message.length > 0 ? `: ${message}` : ''}`);
710
+ }
711
+ const articles = [];
712
+ collectJuejinArticles(root['data'], articles);
713
+ const sources = [];
714
+ for (const article of articles) {
715
+ const articleId = identifier(article['article_id']);
716
+ if (articleId === undefined)
717
+ continue;
718
+ const likes = numberValue(article['digg_count'] ?? article['like_count']);
719
+ const views = numberValue(article['view_count'] ?? article['read_count']);
720
+ const engagement = [
721
+ likes === undefined ? undefined : `点赞 ${likes}`,
722
+ views === undefined ? undefined : `阅读 ${views}`,
723
+ ].filter((part) => part !== undefined);
724
+ const body = cleanMarkup(article['brief_content']) || cleanMarkup(article['content']);
725
+ const source = cleanSource({
726
+ url: `https://juejin.cn/post/${encodeURIComponent(articleId)}`,
727
+ title: cleanMarkup(article['title']),
728
+ snippet: engagement.length > 0
729
+ ? (body.length > 0 ? `${engagement.join(' · ')} · ${body}` : engagement.join(' · '))
730
+ : body,
731
+ publishedAt: isoFromEpoch(article['ctime'] ?? article['publish_time']),
732
+ });
733
+ if (source !== undefined)
734
+ sources.push(source);
735
+ }
736
+ return normaliseSources(sources, clampLimit(limit));
737
+ }
738
+ /**
739
+ * Sort Juejin rows by likes only when the query asks for popular posts. The
740
+ * API returns a relevance order; an explicit "高赞/热门" request should not
741
+ * leave a 16-like post above a 217-like one. Rows without a like count keep
742
+ * their relative order after measured rows.
743
+ *
744
+ * @param sources - rows from {@link parseJuejin}, or a fallback.
745
+ * @param query - the caller's query.
746
+ * @returns the original order when no popularity intent is present.
747
+ */
748
+ export function normaliseJuejinSources(sources, query) {
749
+ if (!/高赞|点赞|热门|受欢迎|popular|most liked|top\b/iu.test(query))
750
+ return [...sources];
751
+ return [...sources]
752
+ .map((source, index) => {
753
+ const match = /点赞\s*(\d+)/u.exec(source.snippet ?? '');
754
+ return { source, index, likes: match === null ? -1 : Number(match[1]) };
755
+ })
756
+ .sort((left, right) => right.likes - left.likes || left.index - right.index)
757
+ .map((entry) => entry.source);
758
+ }
759
+ /**
760
+ * Filter V2EX's hot-topic snapshot by the query.
761
+ *
762
+ * The public API has no search endpoint, so the hot list is the only keyless
763
+ * source; a query that does not appear in any title or body filters everything
764
+ * out and the caller is expected to fall back to `site:v2ex.com`.
765
+ *
766
+ * @param payload - `v2ex.com/api/topics/hot.json` JSON.
767
+ * @param query - text to look for in a title or body, case-insensitively.
768
+ * @param limit - rows to keep.
769
+ * @returns matching topics in hot-list order.
770
+ * @throws `Error` on a Cloudflare page, invalid JSON or an unexpected envelope.
771
+ */
772
+ export function parseV2ex(payload, query, limit = DEFAULT_PLATFORM_LIMIT) {
773
+ const parsed = readJson(payload);
774
+ if (!Array.isArray(parsed))
775
+ throw new Error('unexpected response shape');
776
+ const needle = typeof query === 'string' ? query.trim().toLowerCase() : '';
777
+ const sources = [];
778
+ const seen = new Set();
779
+ for (const item of parsed) {
780
+ if (!isRecord(item))
781
+ continue;
782
+ const title = cleanMarkup(item['title']);
783
+ const content = cleanMarkup(item['content']);
784
+ if (needle.length > 0 && !`${title} ${content}`.toLowerCase().includes(needle))
785
+ continue;
786
+ const id = identifier(item['id']);
787
+ const url = absoluteHttpUrl(item['url']) ??
788
+ (id === undefined ? undefined : `https://www.v2ex.com/t/${encodeURIComponent(id)}`);
789
+ if (url === undefined)
790
+ continue;
791
+ const key = urlKey(url);
792
+ if (seen.has(key))
793
+ continue;
794
+ seen.add(key);
795
+ const source = cleanSource({
796
+ url,
797
+ title,
798
+ snippet: content,
799
+ publishedAt: isoFromEpoch(item['created']),
800
+ });
801
+ if (source !== undefined)
802
+ sources.push(source);
803
+ if (sources.length >= clampLimit(limit))
804
+ break;
805
+ }
806
+ return sources;
807
+ }
808
+ /* -------------------------------------------------------------------------- */
809
+ /* Orchestration */
810
+ /* -------------------------------------------------------------------------- */
811
+ /**
812
+ * Try one platform's fallback search, or report why it cannot run.
813
+ * @param spec - platform being served.
814
+ * @param query - original query, already trimmed.
815
+ * @param limit - per-platform budget.
816
+ * @param options - caller options.
817
+ * @param directReason - why the direct attempt produced nothing, if it failed.
818
+ * @returns the platform answer.
819
+ */
820
+ async function fallbackAnswer(spec, query, limit, options, directReason) {
821
+ if (options.fallbackSearch === undefined) {
822
+ return {
823
+ id: spec.id,
824
+ sources: [],
825
+ via: spec.fallbackOnly === true ? 'fallback' : 'api',
826
+ ...(directReason === undefined ? {} : { failure: failureText(spec.id, directReason) }),
827
+ };
828
+ }
829
+ const fallbackQuery = `site:${spec.site} ${query}`;
830
+ try {
831
+ const result = await options.fallbackSearch(fallbackQuery, limit, options.signal);
832
+ // A search engine may ignore the site: operator and return generic pages.
833
+ // A platform result must at least live on that platform's domain.
834
+ const allowed = normaliseSources(result.sources, limit * 2)
835
+ .filter((source) => isPlatformSource(source.url, spec.site));
836
+ const sourced = spec.id === 'bilibili'
837
+ ? normaliseBilibiliSources(allowed, query)
838
+ : spec.id === 'juejin'
839
+ ? normaliseJuejinSources(allowed, query)
840
+ : spec.id === 'zhihu'
841
+ ? normaliseZhihuFallback(allowed, query)
842
+ : allowed;
843
+ const sources = sourced.slice(0, limit);
844
+ if (sources.length > 0) {
845
+ return { id: spec.id, sources, via: 'fallback' };
846
+ }
847
+ const fallbackFailures = result.failures.filter((item) => typeof item === 'string' && item.trim().length > 0);
848
+ const noDomainRows = fallbackFailures.length === 0 && result.sources.length > 0;
849
+ const fragments = [
850
+ ...(directReason === undefined ? [] : [directReason]),
851
+ ...(fallbackFailures.length === 0 ? [] : [`fallback failed: ${fallbackFailures.join('; ')}`]),
852
+ ...(noDomainRows ? [`fallback returned no ${spec.site} result`] : []),
853
+ ];
854
+ return {
855
+ id: spec.id,
856
+ sources: [],
857
+ via: 'fallback',
858
+ ...(fragments.length === 0 ? {} : { failure: failureText(spec.id, fragments.join('; ')) }),
859
+ };
860
+ }
861
+ catch (error) {
862
+ if (options.signal?.aborted === true)
863
+ throw error;
864
+ const reason = messageOf(error);
865
+ const combined = directReason === undefined ? `fallback failed: ${reason}` : `${directReason}; fallback failed: ${reason}`;
866
+ return { id: spec.id, sources: [], via: 'fallback', failure: failureText(spec.id, combined) };
867
+ }
868
+ }
869
+ /**
870
+ * Search one platform: its direct endpoint first, then the fallback.
871
+ * @param spec - platform to query.
872
+ * @param query - original query, already trimmed.
873
+ * @param limit - per-platform budget.
874
+ * @param options - caller options.
875
+ * @param timeoutMs - per-platform deadline.
876
+ * @returns the platform answer.
877
+ */
878
+ async function searchPlatform(spec, query, limit, options, timeoutMs) {
879
+ if (spec.fallbackOnly === true || spec.build === undefined || spec.parse === undefined) {
880
+ if (options.fallbackSearch === undefined) {
881
+ return {
882
+ id: spec.id,
883
+ sources: [],
884
+ via: 'fallback',
885
+ failure: failureText(spec.id, 'no keyless direct API; fallback search not configured'),
886
+ };
887
+ }
888
+ return fallbackAnswer(spec, query, limit, options, undefined);
889
+ }
890
+ let directSources = [];
891
+ let directReason;
892
+ const builds = [spec.build, ...(spec.alternateBuild === undefined ? [] : [spec.alternateBuild])];
893
+ for (const build of builds) {
894
+ try {
895
+ const payload = await requestText(build(query, limit), spec, options, timeoutMs);
896
+ const parsed = normaliseSources(spec.parse(payload, query, limit), limit);
897
+ const found = spec.id === 'juejin' ? normaliseJuejinSources(parsed, query) : parsed;
898
+ // The primary endpoint can answer with one row while the alternate has
899
+ // the wider set (Bilibili does this for a long query). Merge rather than
900
+ // stop at the first non-empty answer; the URL de-duplication keeps the
901
+ // result stable when both endpoints overlap.
902
+ const seen = new Set(directSources.map((source) => source.url));
903
+ for (const source of found) {
904
+ if (!seen.has(source.url)) {
905
+ seen.add(source.url);
906
+ directSources.push(source);
907
+ }
908
+ }
909
+ }
910
+ catch (error) {
911
+ if (options.signal?.aborted === true)
912
+ throw error;
913
+ directReason = messageOf(error);
914
+ }
915
+ if (directSources.length >= limit)
916
+ break;
917
+ }
918
+ if (directSources.length > 0) {
919
+ // The primary and alternate endpoints may each return a popularity-sorted
920
+ // list; sort the merged rows again so their interleaving cannot undo it.
921
+ const ordered = spec.id === 'bilibili'
922
+ ? normaliseBilibiliSources(directSources, query)
923
+ : spec.id === 'juejin'
924
+ ? normaliseJuejinSources(directSources, query)
925
+ : directSources;
926
+ return { id: spec.id, sources: ordered, via: 'api' };
927
+ }
928
+ return fallbackAnswer(spec, query, limit, options, directReason);
929
+ }
930
+ /**
931
+ * Search the requested platforms and merge their answers.
932
+ *
933
+ * Every platform is asked concurrently. A direct endpoint that is blocked or
934
+ * unparsable is retried through `fallbackSearch`; a platform that still has
935
+ * nothing contributes a `failures` line instead of failing the call. Answers
936
+ * are interleaved by rank (one row from each platform, then the second row
937
+ * from each), duplicate URLs are dropped, and the merged list is capped at the
938
+ * caller's budget.
939
+ *
940
+ * @param query - what to search for; empty input yields an empty outcome.
941
+ * @param options - platform ids, budget, deadline, cancellation, tunnel, fetch
942
+ * seam and fallback search.
943
+ * @returns merged sources plus per-platform counts, channels and failures.
944
+ * @throws SearchError `SEARCH_ABORTED` when the caller cancelled the run.
945
+ */
946
+ export async function searchPlatforms(query, options = {}) {
947
+ const trimmed = typeof query === 'string' ? query.trim() : '';
948
+ if (trimmed.length === 0) {
949
+ return { sources: [], failures: [], counts: {}, truncated: false, via: {} };
950
+ }
951
+ const limit = clampLimit(options.limit);
952
+ const timeoutMs = typeof options.timeoutMs === 'number' && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0
953
+ ? options.timeoutMs
954
+ : DEFAULT_TIMEOUT_MS;
955
+ const requested = (options.ids ?? []).filter(isPlatformId);
956
+ const selected = requested.length > 0 ? PLATFORM_IDS.filter((id) => requested.includes(id)) : [...PLATFORM_IDS];
957
+ let answers;
958
+ try {
959
+ answers = await Promise.all(selected.map((id) => searchPlatform(SPECS[id], trimmed, limit, options, timeoutMs)));
960
+ }
961
+ catch (error) {
962
+ if (options.signal?.aborted === true) {
963
+ throw new SearchError('platform search was cancelled', 'SEARCH_ABORTED', false);
964
+ }
965
+ throw error;
966
+ }
967
+ const counts = {};
968
+ const via = {};
969
+ const failures = [];
970
+ let deepest = 0;
971
+ for (const answer of answers) {
972
+ counts[answer.id] = answer.sources.length;
973
+ via[answer.id] = answer.via;
974
+ deepest = Math.max(deepest, answer.sources.length);
975
+ if (answer.failure !== undefined)
976
+ failures.push(answer.failure);
977
+ }
978
+ const merged = [];
979
+ const seen = new Set();
980
+ for (let rank = 0; rank < deepest; rank += 1) {
981
+ for (const answer of answers) {
982
+ const source = answer.sources[rank];
983
+ if (source === undefined)
984
+ continue;
985
+ const key = urlKey(source.url);
986
+ if (seen.has(key))
987
+ continue;
988
+ seen.add(key);
989
+ merged.push(source);
990
+ }
991
+ }
992
+ return {
993
+ sources: merged.slice(0, limit),
994
+ failures,
995
+ counts,
996
+ truncated: merged.length > limit,
997
+ via,
998
+ };
999
+ }
1000
+ /**
1001
+ * Drop a generic fallback row that shares almost none of a specific query.
1002
+ *
1003
+ * Zhihu has no keyless direct endpoint. Its fallback sometimes puts a broad
1004
+ * question such as "目前有哪些主流的 AI?" above an answer that actually names
1005
+ * coding assistants. This is a light preference, not a hard filter: when fewer
1006
+ * than two rows survive, the original list is returned.
1007
+ *
1008
+ * @param sources - fallback rows already filtered to the Zhihu host.
1009
+ * @param query - the caller's query.
1010
+ * @returns rows with at least two query-term matches, or the original list.
1011
+ */
1012
+ export function normaliseZhihuFallback(sources, query) {
1013
+ const terms = new Set();
1014
+ const text = query.toLowerCase();
1015
+ for (const word of text.match(/[a-z][a-z0-9.+#-]{2,}/gu) ?? [])
1016
+ terms.add(word);
1017
+ for (const run of text.match(/[\u4e00-\u9fff]{2,}/gu) ?? []) {
1018
+ for (let index = 0; index < run.length - 1; index += 1)
1019
+ terms.add(run.slice(index, index + 2));
1020
+ }
1021
+ const stop = new Set(['哪些', '主流', '目前', '什么', '怎么', '哪个', '如何', '可以', '是否', '这个', '那个']);
1022
+ for (const word of stop)
1023
+ terms.delete(word);
1024
+ if (terms.size === 0)
1025
+ return [...sources];
1026
+ const kept = sources.filter((source) => {
1027
+ const haystack = `${source.title ?? ''} ${source.snippet ?? ''}`.toLowerCase();
1028
+ let matches = 0;
1029
+ for (const term of terms)
1030
+ if (haystack.includes(term))
1031
+ matches += 1;
1032
+ return matches >= 2;
1033
+ });
1034
+ return kept.length >= Math.min(2, sources.length) ? kept : [...sources];
1035
+ }