opencode-search 1.0.0-next.4 → 1.0.0-next.8
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 +4 -18
- package/REMOVED.md +10 -0
- package/dist/package.json +6 -5
- package/dist/src/providers/gnews-search.d.ts +1 -1
- package/dist/src/providers/gnews-search.d.ts.map +1 -1
- package/dist/src/providers/gnews-search.js +5 -3
- package/dist/src/providers/gnews-search.js.map +1 -1
- package/dist/src/renderers.d.ts +0 -2
- package/dist/src/renderers.d.ts.map +1 -1
- package/dist/src/renderers.js +0 -8
- package/dist/src/renderers.js.map +1 -1
- package/dist/src/search.d.ts +0 -1
- package/dist/src/search.d.ts.map +1 -1
- package/dist/src/search.js +27 -55
- package/dist/src/search.js.map +1 -1
- package/package.json +6 -5
- package/search.ts +22 -0
- package/src/cache.test.ts +14 -0
- package/src/cache.ts +61 -0
- package/src/format.test.ts +175 -0
- package/src/format.ts +72 -0
- package/src/html.ts +39 -0
- package/src/normalize-blurb.test.ts +67 -0
- package/src/normalize-blurb.ts +22 -0
- package/src/opencode/notify.ts +16 -0
- package/src/parse-results.test.ts +75 -0
- package/src/parse-results.ts +22 -0
- package/src/providers/brave-search.ts +39 -0
- package/src/providers/bsky-search.test.ts +16 -0
- package/src/providers/bsky-search.ts +88 -0
- package/src/providers/ddg-search.test.ts +15 -0
- package/src/providers/ddg-search.ts +117 -0
- package/src/providers/gnews-search.ts +72 -0
- package/src/providers/mdn-search.test.ts +17 -0
- package/src/providers/mdn-search.ts +83 -0
- package/src/providers/wiki-search.test.ts +16 -0
- package/src/providers/wiki-search.ts +72 -0
- package/src/renderers.test.ts +263 -0
- package/src/renderers.ts +42 -0
- package/src/safe.test.ts +87 -0
- package/src/safe.ts +27 -0
- package/src/search.ts +264 -0
- package/src/strategies.test.ts +92 -0
- package/src/strategies.ts +62 -0
- package/src/user-agent.ts +3 -0
- package/src/v1-message.test.ts +17 -0
- package/src/v1-message.ts +10 -0
- package/dist/src/providers/standard-search.d.ts +0 -18
- package/dist/src/providers/standard-search.d.ts.map +0 -1
- package/dist/src/providers/standard-search.js +0 -61
- package/dist/src/providers/standard-search.js.map +0 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { JSDOM, VirtualConsole } from 'jsdom'
|
|
2
|
+
import * as cache from '../cache.ts'
|
|
3
|
+
import { baseHeaders, detectBlock, runStrategies } from '../strategies.ts'
|
|
4
|
+
|
|
5
|
+
// The lite frontend is the fallback when the html frontend is blocked or empty
|
|
6
|
+
const DDG_HTML_URL = 'https://html.duckduckgo.com/html/'
|
|
7
|
+
const DDG_LITE_URL = 'https://lite.duckduckgo.com/lite/'
|
|
8
|
+
|
|
9
|
+
const BLOCK_MARKERS = ['anomaly-modal', 'Please try again']
|
|
10
|
+
|
|
11
|
+
export type DdgResult = {
|
|
12
|
+
title: string
|
|
13
|
+
url: string
|
|
14
|
+
abstract: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const searchParams = (query: string): URLSearchParams => (
|
|
18
|
+
new URLSearchParams({
|
|
19
|
+
q: query,
|
|
20
|
+
b: '',
|
|
21
|
+
kf: '-1',
|
|
22
|
+
kh: '1',
|
|
23
|
+
kl: 'us-en',
|
|
24
|
+
kp: '1',
|
|
25
|
+
k1: '-1',
|
|
26
|
+
})
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
const parseDom = (html: string, url: string): Document => {
|
|
30
|
+
const virtualConsole = new VirtualConsole()
|
|
31
|
+
virtualConsole.on('jsdomError', () => {
|
|
32
|
+
// Silently ignore JSDOM errors
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
return new JSDOM(html, { url, virtualConsole }).window.document
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const fetchDdg = async (url: string, query: string, userAgent: string): Promise<string> => {
|
|
39
|
+
const response = await fetch(url, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: {
|
|
42
|
+
...baseHeaders(userAgent),
|
|
43
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
44
|
+
},
|
|
45
|
+
body: searchParams(query),
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
if (!response.ok) throw new Error(response.status + ' ' + response.statusText)
|
|
49
|
+
|
|
50
|
+
const html = await response.text()
|
|
51
|
+
detectBlock('DuckDuckGo', html, BLOCK_MARKERS)
|
|
52
|
+
|
|
53
|
+
return html
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const parseHtmlResults = (html: string): DdgResult[] => {
|
|
57
|
+
const doc = parseDom(html, DDG_HTML_URL)
|
|
58
|
+
|
|
59
|
+
const links = doc.querySelectorAll('.result__a')
|
|
60
|
+
const snippets = doc.querySelectorAll('.result__snippet')
|
|
61
|
+
|
|
62
|
+
const results: DdgResult[] = []
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < links.length; i++) {
|
|
65
|
+
const link = links[i]
|
|
66
|
+
if (!link) continue
|
|
67
|
+
|
|
68
|
+
const title = link.textContent?.trim() ?? ''
|
|
69
|
+
const href = link.getAttribute('href') ?? ''
|
|
70
|
+
const abstract = snippets[i]?.textContent?.trim() ?? ''
|
|
71
|
+
|
|
72
|
+
if (title && href) results.push({ title, url: href, abstract })
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return results
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const parseLiteResults = (html: string): DdgResult[] => {
|
|
79
|
+
const doc = parseDom(html, DDG_LITE_URL)
|
|
80
|
+
|
|
81
|
+
const links = doc.querySelectorAll('a.result-link')
|
|
82
|
+
const snippets = doc.querySelectorAll('td.result-snippet')
|
|
83
|
+
|
|
84
|
+
const results: DdgResult[] = []
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i < links.length; i++) {
|
|
87
|
+
const link = links[i]
|
|
88
|
+
if (!link) continue
|
|
89
|
+
|
|
90
|
+
const title = link.textContent?.trim() ?? ''
|
|
91
|
+
const href = link.getAttribute('href') ?? ''
|
|
92
|
+
const abstract = snippets[i]?.textContent?.trim() ?? ''
|
|
93
|
+
|
|
94
|
+
if (title && href) results.push({ title, url: href, abstract })
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return results
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const ddgSearch = async (query: string): Promise<DdgResult[]> => {
|
|
101
|
+
const cacheKey = 'ddg:' + query
|
|
102
|
+
|
|
103
|
+
const cached = await cache.get<DdgResult[]>(cacheKey)
|
|
104
|
+
if (cached) return cached
|
|
105
|
+
|
|
106
|
+
const results = await runStrategies([{
|
|
107
|
+
name: 'ddg-html',
|
|
108
|
+
run: async (userAgent) => parseHtmlResults(await fetchDdg(DDG_HTML_URL, query, userAgent)),
|
|
109
|
+
}, {
|
|
110
|
+
name: 'ddg-lite',
|
|
111
|
+
run: async (userAgent) => parseLiteResults(await fetchDdg(DDG_LITE_URL, query, userAgent)),
|
|
112
|
+
}])
|
|
113
|
+
|
|
114
|
+
await cache.set(cacheKey, results)
|
|
115
|
+
|
|
116
|
+
return results
|
|
117
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as cache from '../cache.ts'
|
|
2
|
+
import { baseHeaders } from '../strategies.ts'
|
|
3
|
+
|
|
4
|
+
// Not a general web search: the index is news-weighted and reference pages are largely absent.
|
|
5
|
+
const GNEWS_URL = 'https://news.google.com/rss/search'
|
|
6
|
+
|
|
7
|
+
export type GnewsResult = {
|
|
8
|
+
title: string
|
|
9
|
+
url: string
|
|
10
|
+
abstract: string
|
|
11
|
+
source: string
|
|
12
|
+
date: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const ENTITIES: Record<string, string> = {
|
|
16
|
+
'&': '&',
|
|
17
|
+
'<': '<',
|
|
18
|
+
'>': '>',
|
|
19
|
+
'"': '"',
|
|
20
|
+
''': "'",
|
|
21
|
+
''': "'",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const decodeEntities = (value: string): string => (
|
|
25
|
+
value.replace(/&(?:amp|lt|gt|quot|apos|#39);/g, (match) => ENTITIES[match] ?? match)
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
// Feed titles arrive as 'Headline goes here - Publisher', with the publisher repeated in <source>.
|
|
29
|
+
const stripSource = (title: string, source: string): string => {
|
|
30
|
+
const suffix = ' - ' + source
|
|
31
|
+
return title.endsWith(suffix) ? title.slice(0, -suffix.length) : title
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The feed always returns about 100 items, which is ~10k tokens of context for one call.
|
|
35
|
+
const DEFAULT_LIMIT = 20
|
|
36
|
+
|
|
37
|
+
export const gnewsSearch = async (query: string, limit = DEFAULT_LIMIT): Promise<GnewsResult[]> => {
|
|
38
|
+
const cacheKey = 'gnews:' + query
|
|
39
|
+
|
|
40
|
+
const cached = await cache.get<GnewsResult[]>(cacheKey)
|
|
41
|
+
if (cached) return cached.slice(0, limit)
|
|
42
|
+
|
|
43
|
+
const params = new URLSearchParams({ q: query, hl: 'en-US', gl: 'US', ceid: 'US:en' })
|
|
44
|
+
const res = await fetch(GNEWS_URL + '?' + params, { headers: baseHeaders('') })
|
|
45
|
+
|
|
46
|
+
if (!res.ok) throw new Error('Google News returned ' + res.status + ' ' + res.statusText)
|
|
47
|
+
|
|
48
|
+
const xml = await res.text()
|
|
49
|
+
const results: GnewsResult[] = []
|
|
50
|
+
|
|
51
|
+
// Matches one <item> element and captures its body.
|
|
52
|
+
// Not jsdom: it treats <link> as a void element, so every item parses as having no URL.
|
|
53
|
+
for (const match of xml.matchAll(/<item>(.*?)<\/item>/gs)) {
|
|
54
|
+
const item = match[1] ?? ''
|
|
55
|
+
|
|
56
|
+
const tag = (name: string): string => {
|
|
57
|
+
const [, value = ''] = item.match(new RegExp('<' + name + '[^>]*>(.*?)</' + name + '>', 's')) ?? []
|
|
58
|
+
return decodeEntities(value.replace(/<!\[CDATA\[(.*?)\]\]>/gs, '$1').trim())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const source = tag('source')
|
|
62
|
+
const title = stripSource(tag('title'), source)
|
|
63
|
+
// A news.google.com redirector that only resolves in a browser, reported as-is.
|
|
64
|
+
const url = tag('link')
|
|
65
|
+
|
|
66
|
+
if (title && url) results.push({ title, url, abstract: '', source, date: tag('pubDate') })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
await cache.set(cacheKey, results)
|
|
70
|
+
|
|
71
|
+
return results.slice(0, limit)
|
|
72
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { mdnSearch } from './mdn-search.ts'
|
|
3
|
+
|
|
4
|
+
describe('mdnSearch', () => {
|
|
5
|
+
it('should return MDN documents for a valid query', async () => {
|
|
6
|
+
const results = await mdnSearch({ query: 'fetch' })
|
|
7
|
+
|
|
8
|
+
expect(results.documents.length).toBeGreaterThan(0)
|
|
9
|
+
|
|
10
|
+
const first = results.documents[0]
|
|
11
|
+
if (!first) throw new Error('Expected at least one document')
|
|
12
|
+
|
|
13
|
+
expect(first.title).toBeDefined()
|
|
14
|
+
expect(first.mdn_url).toBeDefined()
|
|
15
|
+
expect(first.summary).toBeDefined()
|
|
16
|
+
})
|
|
17
|
+
})
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import * as cache from '../cache.ts'
|
|
3
|
+
import { USER_AGENT } from '../user-agent.ts'
|
|
4
|
+
|
|
5
|
+
const MDN_API = 'https://developer.mozilla.org/api/v1/search'
|
|
6
|
+
|
|
7
|
+
const documentSchema = z.object({
|
|
8
|
+
mdn_url: z.string().min(1),
|
|
9
|
+
title: z.string().min(1),
|
|
10
|
+
summary: z.string().default(''),
|
|
11
|
+
slug: z.string().default(''),
|
|
12
|
+
locale: z.string().default('en-US'),
|
|
13
|
+
popularity: z.number().default(0),
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
const responseSchema = z.object({
|
|
17
|
+
documents: z.array(z.unknown()),
|
|
18
|
+
metadata: z.object({
|
|
19
|
+
total: z.object({
|
|
20
|
+
value: z.number(),
|
|
21
|
+
}),
|
|
22
|
+
}).optional(),
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export type MdnDocument = z.infer<typeof documentSchema>
|
|
26
|
+
|
|
27
|
+
export type MdnSearchResult = {
|
|
28
|
+
documents: Array<MdnDocument>
|
|
29
|
+
total: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type MdnSearchOptions = {
|
|
33
|
+
query: string
|
|
34
|
+
limit?: number
|
|
35
|
+
page?: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const mdnSearch = async (options: MdnSearchOptions): Promise<MdnSearchResult> => {
|
|
39
|
+
const cacheKey = [
|
|
40
|
+
'mdn',
|
|
41
|
+
options.query,
|
|
42
|
+
options.limit ?? 10,
|
|
43
|
+
options.page ?? 1,
|
|
44
|
+
].join(':')
|
|
45
|
+
|
|
46
|
+
const cached = await cache.get<MdnSearchResult>(cacheKey)
|
|
47
|
+
if (cached && cached.documents && cached.documents.length > 0) return cached
|
|
48
|
+
|
|
49
|
+
const params = new URLSearchParams({
|
|
50
|
+
q: options.query,
|
|
51
|
+
locale: 'en-US',
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (options.limit) params.set('size', String(options.limit))
|
|
55
|
+
|
|
56
|
+
if (options.page) params.set('page', String(options.page))
|
|
57
|
+
|
|
58
|
+
const url = MDN_API + '?' + params.toString()
|
|
59
|
+
const res = await fetch(url, {
|
|
60
|
+
headers: { 'User-Agent': USER_AGENT },
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
const text = await res.text()
|
|
65
|
+
throw new Error('MDN search error (' + res.status + '): ' + text)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const json: unknown = await res.json()
|
|
69
|
+
const response = responseSchema.parse(json)
|
|
70
|
+
|
|
71
|
+
const documents = response.documents.flatMap((raw) => {
|
|
72
|
+
const parsed = documentSchema.safeParse(raw)
|
|
73
|
+
return parsed.success ? [parsed.data] : []
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const total = response.metadata?.total.value ?? documents.length
|
|
77
|
+
|
|
78
|
+
const result: MdnSearchResult = { documents, total }
|
|
79
|
+
|
|
80
|
+
if (documents.length > 0) await cache.set(cacheKey, result)
|
|
81
|
+
|
|
82
|
+
return result
|
|
83
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { wikiSearch } from './wiki-search.ts'
|
|
3
|
+
|
|
4
|
+
describe('wikiSearch', () => {
|
|
5
|
+
it('returns page results', async () => {
|
|
6
|
+
const results = await wikiSearch({ query: 'typescript programming' })
|
|
7
|
+
|
|
8
|
+
expect(results.pages.length > 0).toEqual(true)
|
|
9
|
+
|
|
10
|
+
const first = results.pages[0]
|
|
11
|
+
if (!first) throw new Error('expected at least one page')
|
|
12
|
+
expect(first.title).toBeDefined()
|
|
13
|
+
expect(first.key).toBeDefined()
|
|
14
|
+
expect(first.excerpt).toBeDefined()
|
|
15
|
+
})
|
|
16
|
+
})
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import * as cache from '../cache.ts'
|
|
3
|
+
import { normalizeBlurb } from '../normalize-blurb.ts'
|
|
4
|
+
import { USER_AGENT } from '../user-agent.ts'
|
|
5
|
+
|
|
6
|
+
const WIKI_API = 'https://en.wikipedia.org/w/rest.php/v1/search/page'
|
|
7
|
+
|
|
8
|
+
const thumbnailSchema = z.object({
|
|
9
|
+
url: z.string().min(1),
|
|
10
|
+
width: z.number(),
|
|
11
|
+
height: z.number(),
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const pageSchema = z.object({
|
|
15
|
+
id: z.number().default(0),
|
|
16
|
+
key: z.string().min(1),
|
|
17
|
+
title: z.string().min(1),
|
|
18
|
+
excerpt: z.string().default('').transform(normalizeBlurb),
|
|
19
|
+
description: z.string().nullable().default(null),
|
|
20
|
+
matched_title: z.string().nullable().default(null),
|
|
21
|
+
thumbnail: thumbnailSchema.nullable().default(null),
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const responseSchema = z.object({
|
|
25
|
+
pages: z.array(z.unknown()),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export type WikiPage = z.infer<typeof pageSchema>
|
|
29
|
+
|
|
30
|
+
export type WikiSearchResult = {
|
|
31
|
+
pages: Array<WikiPage>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type WikiSearchOptions = {
|
|
35
|
+
query: string
|
|
36
|
+
limit?: number
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const wikiSearch = async (options: WikiSearchOptions): Promise<WikiSearchResult> => {
|
|
40
|
+
const cacheKey = 'wiki:' + options.query + ':' + (options.limit ?? 10)
|
|
41
|
+
|
|
42
|
+
const cached = await cache.get<WikiSearchResult>(cacheKey)
|
|
43
|
+
if (cached && cached.pages && cached.pages.length > 0) return cached
|
|
44
|
+
|
|
45
|
+
const params = new URLSearchParams({ q: options.query })
|
|
46
|
+
|
|
47
|
+
if (options.limit) params.set('limit', String(options.limit))
|
|
48
|
+
|
|
49
|
+
const url = WIKI_API + '?' + params.toString()
|
|
50
|
+
const res = await fetch(url, {
|
|
51
|
+
headers: { 'User-Agent': USER_AGENT },
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (!res.ok) {
|
|
55
|
+
const text = await res.text()
|
|
56
|
+
throw new Error('Wikipedia search error (' + res.status + '): ' + text)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const json: unknown = await res.json()
|
|
60
|
+
const response = responseSchema.parse(json)
|
|
61
|
+
|
|
62
|
+
const pages = response.pages.flatMap((raw) => {
|
|
63
|
+
const parsed = pageSchema.safeParse(raw)
|
|
64
|
+
return parsed.success ? [parsed.data] : []
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
const result: WikiSearchResult = { pages }
|
|
68
|
+
|
|
69
|
+
if (pages.length > 0) await cache.set(cacheKey, result)
|
|
70
|
+
|
|
71
|
+
return result
|
|
72
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
renderBskyPost,
|
|
4
|
+
renderDdgResult,
|
|
5
|
+
renderGnewsItem,
|
|
6
|
+
renderMdnDoc,
|
|
7
|
+
renderWebResult,
|
|
8
|
+
renderWikiPage,
|
|
9
|
+
} from './renderers.ts'
|
|
10
|
+
|
|
11
|
+
describe('renderDdgResult', () => {
|
|
12
|
+
it('returns three lines: title, url, abstract', () => {
|
|
13
|
+
const result = { title: 'TypeScript', url: 'https://typescriptlang.org', abstract: 'A typed superset of JavaScript' }
|
|
14
|
+
const lines = renderDdgResult(result, 0).split('\n')
|
|
15
|
+
expect(lines).toHaveLength(3)
|
|
16
|
+
expect(lines[0]).toContain('1. TypeScript')
|
|
17
|
+
expect(lines[1]).toContain('https://typescriptlang.org')
|
|
18
|
+
expect(lines[2]).toContain('A typed superset of JavaScript')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('uses 1-based index', () => {
|
|
22
|
+
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
23
|
+
const lines = renderDdgResult(result, 4).split('\n')
|
|
24
|
+
expect(lines[0]).toStartWith('5. ')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('indents url and abstract lines', () => {
|
|
28
|
+
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
29
|
+
const lines = renderDdgResult(result, 0).split('\n')
|
|
30
|
+
expect(lines[1]).toStartWith(' ')
|
|
31
|
+
expect(lines[2]).toStartWith(' ')
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('renderWebResult', () => {
|
|
36
|
+
it('returns three lines: title, url, abstract', () => {
|
|
37
|
+
const result = { title: 'TypeScript', url: 'https://typescriptlang.org', abstract: 'A typed superset of JavaScript' }
|
|
38
|
+
const lines = renderWebResult(result, 0).split('\n')
|
|
39
|
+
expect(lines).toHaveLength(3)
|
|
40
|
+
expect(lines[0]).toContain('1. TypeScript')
|
|
41
|
+
expect(lines[1]).toContain('https://typescriptlang.org')
|
|
42
|
+
expect(lines[2]).toContain('A typed superset of JavaScript')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('uses 1-based index', () => {
|
|
46
|
+
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
47
|
+
const lines = renderWebResult(result, 4).split('\n')
|
|
48
|
+
expect(lines[0]).toStartWith('5. ')
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
describe('renderGnewsItem', () => {
|
|
53
|
+
const item = {
|
|
54
|
+
title: 'Rust 2.0 announced',
|
|
55
|
+
url: 'https://news.google.com/rss/articles/CBMiabc',
|
|
56
|
+
abstract: '',
|
|
57
|
+
source: 'The Register',
|
|
58
|
+
date: 'Fri, 18 Sep 2026 16:55:50 GMT',
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
it('returns three lines: title, source and date, url', () => {
|
|
62
|
+
const lines = renderGnewsItem(item, 0).split('\n')
|
|
63
|
+
|
|
64
|
+
expect(lines).toHaveLength(3)
|
|
65
|
+
expect(lines[0]).toContain('1. Rust 2.0 announced')
|
|
66
|
+
expect(lines[1]).toContain('The Register')
|
|
67
|
+
expect(lines[2]).toContain('news.google.com')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('omits the separator when there is no date', () => {
|
|
71
|
+
const lines = renderGnewsItem({ ...item, date: '' }, 0).split('\n')
|
|
72
|
+
|
|
73
|
+
expect(lines[1]).not.toContain('—')
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('renderBskyPost', () => {
|
|
78
|
+
const post = {
|
|
79
|
+
uri: 'at://did:plc:test/app.bsky.feed.post/abc',
|
|
80
|
+
cid: 'cid123',
|
|
81
|
+
author: {
|
|
82
|
+
did: 'did:plc:test',
|
|
83
|
+
handle: 'alice.bsky.social',
|
|
84
|
+
displayName: 'Alice',
|
|
85
|
+
},
|
|
86
|
+
record: {
|
|
87
|
+
text: 'Hello world from Bluesky!',
|
|
88
|
+
createdAt: '2025-06-15T14:30:00.000Z',
|
|
89
|
+
},
|
|
90
|
+
likeCount: 42,
|
|
91
|
+
repostCount: 7,
|
|
92
|
+
replyCount: 3,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
it('returns three lines', () => {
|
|
96
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
97
|
+
expect(lines).toHaveLength(3)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('includes index and author handle on first line', () => {
|
|
101
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
102
|
+
expect(lines[0]).toContain('1. @alice.bsky.social')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('uses 1-based index', () => {
|
|
106
|
+
const lines = renderBskyPost(post, 4).split('\n')
|
|
107
|
+
expect(lines[0]).toStartWith('5. @')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('includes formatted date with time on first line', () => {
|
|
111
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
112
|
+
expect(lines[0]).toContain('Jun')
|
|
113
|
+
expect(lines[0]).toContain('2025')
|
|
114
|
+
expect(lines[0]).toMatch(/AM|PM/)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('includes post text on second line', () => {
|
|
118
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
119
|
+
expect(lines[1]).toContain('Hello world from Bluesky!')
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('includes engagement counts on third line', () => {
|
|
123
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
124
|
+
expect(lines[2]).toContain('Likes: 42')
|
|
125
|
+
expect(lines[2]).toContain('Reposts: 7')
|
|
126
|
+
expect(lines[2]).toContain('Replies: 3')
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('indents second and third lines', () => {
|
|
130
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
131
|
+
expect(lines[1]).toStartWith(' ')
|
|
132
|
+
expect(lines[2]).toStartWith(' ')
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
describe('renderWikiPage', () => {
|
|
137
|
+
it('includes title and wikipedia url', () => {
|
|
138
|
+
const result = renderWikiPage({
|
|
139
|
+
id: 1,
|
|
140
|
+
key: 'TypeScript',
|
|
141
|
+
title: 'TypeScript',
|
|
142
|
+
excerpt: 'A programming language',
|
|
143
|
+
description: 'Open-source language',
|
|
144
|
+
matched_title: null,
|
|
145
|
+
thumbnail: null,
|
|
146
|
+
}, 0)
|
|
147
|
+
expect(result).toContain('1. TypeScript')
|
|
148
|
+
expect(result).toContain('https://en.wikipedia.org/wiki/TypeScript')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('includes description when present', () => {
|
|
152
|
+
const result = renderWikiPage({
|
|
153
|
+
id: 1,
|
|
154
|
+
key: 'TypeScript',
|
|
155
|
+
title: 'TypeScript',
|
|
156
|
+
excerpt: 'A programming language',
|
|
157
|
+
description: 'Open-source language',
|
|
158
|
+
matched_title: null,
|
|
159
|
+
thumbnail: null,
|
|
160
|
+
}, 0)
|
|
161
|
+
expect(result).toContain('Open-source language')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it('omits description when null', () => {
|
|
165
|
+
const result = renderWikiPage({
|
|
166
|
+
id: 2,
|
|
167
|
+
key: 'JavaScript',
|
|
168
|
+
title: 'JavaScript',
|
|
169
|
+
excerpt: 'A scripting language',
|
|
170
|
+
description: null,
|
|
171
|
+
matched_title: null,
|
|
172
|
+
thumbnail: null,
|
|
173
|
+
}, 0)
|
|
174
|
+
const lines = result.split('\n')
|
|
175
|
+
expect(lines).toHaveLength(3)
|
|
176
|
+
expect(lines[0]).toContain('1. JavaScript')
|
|
177
|
+
expect(lines[1]).toContain('https://en.wikipedia.org/wiki/JavaScript')
|
|
178
|
+
expect(lines[2]).toContain('A scripting language')
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('returns four lines when description is present', () => {
|
|
182
|
+
const lines = renderWikiPage({
|
|
183
|
+
id: 1,
|
|
184
|
+
key: 'TypeScript',
|
|
185
|
+
title: 'TypeScript',
|
|
186
|
+
excerpt: 'A programming language',
|
|
187
|
+
description: 'Open-source language',
|
|
188
|
+
matched_title: null,
|
|
189
|
+
thumbnail: null,
|
|
190
|
+
}, 0).split('\n')
|
|
191
|
+
expect(lines).toHaveLength(4)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('uses 1-based index', () => {
|
|
195
|
+
const lines = renderWikiPage({
|
|
196
|
+
id: 1,
|
|
197
|
+
key: 'Test',
|
|
198
|
+
title: 'Test',
|
|
199
|
+
excerpt: 'Excerpt',
|
|
200
|
+
description: null,
|
|
201
|
+
matched_title: null,
|
|
202
|
+
thumbnail: null,
|
|
203
|
+
}, 2).split('\n')
|
|
204
|
+
expect(lines[0]).toStartWith('3. ')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('indents all lines after the title', () => {
|
|
208
|
+
const lines = renderWikiPage({
|
|
209
|
+
id: 1,
|
|
210
|
+
key: 'Test',
|
|
211
|
+
title: 'Test',
|
|
212
|
+
excerpt: 'Excerpt',
|
|
213
|
+
description: 'Desc',
|
|
214
|
+
matched_title: null,
|
|
215
|
+
thumbnail: null,
|
|
216
|
+
}, 0).split('\n')
|
|
217
|
+
for (let i = 1; i < lines.length; i++) {
|
|
218
|
+
expect(lines[i]).toStartWith(' ')
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
describe('renderMdnDoc', () => {
|
|
224
|
+
const doc = {
|
|
225
|
+
mdn_url: '/en-US/docs/Web/API/Fetch_API',
|
|
226
|
+
title: 'Fetch API',
|
|
227
|
+
summary: 'The Fetch API provides an interface for fetching resources.',
|
|
228
|
+
slug: 'Web/API/Fetch_API',
|
|
229
|
+
locale: 'en-US',
|
|
230
|
+
popularity: 0.5,
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
it('returns three lines: title, url, summary', () => {
|
|
234
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
235
|
+
expect(lines).toHaveLength(3)
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
it('includes index and title on first line', () => {
|
|
239
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
240
|
+
expect(lines[0]).toBe('1. Fetch API')
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('uses 1-based index', () => {
|
|
244
|
+
const lines = renderMdnDoc(doc, 4).split('\n')
|
|
245
|
+
expect(lines[0]).toStartWith('5. ')
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('includes full MDN url on second line', () => {
|
|
249
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
250
|
+
expect(lines[1]).toContain('https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API')
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it('includes summary on third line', () => {
|
|
254
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
255
|
+
expect(lines[2]).toContain('The Fetch API provides an interface for fetching resources.')
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
it('indents url and summary lines', () => {
|
|
259
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
260
|
+
expect(lines[1]).toStartWith(' ')
|
|
261
|
+
expect(lines[2]).toStartWith(' ')
|
|
262
|
+
})
|
|
263
|
+
})
|
package/src/renderers.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { formatDate, formatDateTime } from './format.ts'
|
|
2
|
+
import { normalizeBlurb } from './normalize-blurb.ts'
|
|
3
|
+
import type { BskyPost } from './providers/bsky-search.ts'
|
|
4
|
+
import type { DdgResult } from './providers/ddg-search.ts'
|
|
5
|
+
import type { GnewsResult } from './providers/gnews-search.ts'
|
|
6
|
+
import type { MdnDocument } from './providers/mdn-search.ts'
|
|
7
|
+
import type { WikiPage } from './providers/wiki-search.ts'
|
|
8
|
+
|
|
9
|
+
export const renderDdgResult = (result: DdgResult, index: number): string => {
|
|
10
|
+
return `${index + 1}. ${result.title}
|
|
11
|
+
${result.url}
|
|
12
|
+
${normalizeBlurb(result.abstract)}`
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Brave and DuckDuckGo return the same title/url/abstract shape.
|
|
16
|
+
export const renderWebResult = renderDdgResult
|
|
17
|
+
|
|
18
|
+
export const renderGnewsItem = (result: GnewsResult, index: number): string => {
|
|
19
|
+
return `${index + 1}. ${result.title}
|
|
20
|
+
${result.source}${result.date ? ' — ' + formatDate(result.date) : ''}
|
|
21
|
+
${result.url}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const renderBskyPost = (post: BskyPost, index: number): string => (
|
|
25
|
+
`${index + 1}. @${post.author.handle} (${formatDateTime(post.record.createdAt)})
|
|
26
|
+
${normalizeBlurb(post.record.text)}
|
|
27
|
+
Likes: ${post.likeCount} Reposts: ${post.repostCount} Replies: ${post.replyCount}`
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
export const renderWikiPage = (page: WikiPage, index: number): string => {
|
|
32
|
+
const desc = page.description ? `\n ${normalizeBlurb(page.description)}` : ''
|
|
33
|
+
return `${index + 1}. ${page.title}
|
|
34
|
+
https://en.wikipedia.org/wiki/${page.key}${desc}
|
|
35
|
+
${normalizeBlurb(page.excerpt)}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const renderMdnDoc = (doc: MdnDocument, index: number): string => {
|
|
39
|
+
return `${index + 1}. ${doc.title}
|
|
40
|
+
https://developer.mozilla.org${doc.mdn_url}
|
|
41
|
+
${normalizeBlurb(doc.summary)}`
|
|
42
|
+
}
|