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.
Files changed (129) hide show
  1. package/README.md +27 -27
  2. package/dist/{search.d.ts → search.d.mts} +1 -2
  3. package/dist/search.mjs +947 -0
  4. package/package.json +22 -16
  5. package/dist/package.json +0 -68
  6. package/dist/search.d.ts.map +0 -1
  7. package/dist/search.js +0 -20
  8. package/dist/search.js.map +0 -1
  9. package/dist/src/cache.d.ts +0 -3
  10. package/dist/src/cache.d.ts.map +0 -1
  11. package/dist/src/cache.js +0 -43
  12. package/dist/src/cache.js.map +0 -1
  13. package/dist/src/format.d.ts +0 -20
  14. package/dist/src/format.d.ts.map +0 -1
  15. package/dist/src/format.js +0 -46
  16. package/dist/src/format.js.map +0 -1
  17. package/dist/src/html.d.ts +0 -8
  18. package/dist/src/html.d.ts.map +0 -1
  19. package/dist/src/html.js +0 -28
  20. package/dist/src/html.js.map +0 -1
  21. package/dist/src/normalize-blurb.d.ts +0 -2
  22. package/dist/src/normalize-blurb.d.ts.map +0 -1
  23. package/dist/src/normalize-blurb.js +0 -21
  24. package/dist/src/normalize-blurb.js.map +0 -1
  25. package/dist/src/opencode/notify.d.ts +0 -10
  26. package/dist/src/opencode/notify.d.ts.map +0 -1
  27. package/dist/src/opencode/notify.js +0 -8
  28. package/dist/src/opencode/notify.js.map +0 -1
  29. package/dist/src/parse-results.d.ts +0 -2
  30. package/dist/src/parse-results.d.ts.map +0 -1
  31. package/dist/src/parse-results.js +0 -20
  32. package/dist/src/parse-results.js.map +0 -1
  33. package/dist/src/providers/brave-search.d.ts +0 -4
  34. package/dist/src/providers/brave-search.d.ts.map +0 -1
  35. package/dist/src/providers/brave-search.js +0 -29
  36. package/dist/src/providers/brave-search.js.map +0 -1
  37. package/dist/src/providers/bsky-search.d.ts +0 -31
  38. package/dist/src/providers/bsky-search.d.ts.map +0 -1
  39. package/dist/src/providers/bsky-search.js +0 -61
  40. package/dist/src/providers/bsky-search.js.map +0 -1
  41. package/dist/src/providers/ddg-search.d.ts +0 -7
  42. package/dist/src/providers/ddg-search.d.ts.map +0 -1
  43. package/dist/src/providers/ddg-search.js +0 -88
  44. package/dist/src/providers/ddg-search.js.map +0 -1
  45. package/dist/src/providers/gnews-search.d.ts +0 -9
  46. package/dist/src/providers/gnews-search.d.ts.map +0 -1
  47. package/dist/src/providers/gnews-search.js +0 -50
  48. package/dist/src/providers/gnews-search.js.map +0 -1
  49. package/dist/src/providers/mdn-search.d.ts +0 -22
  50. package/dist/src/providers/mdn-search.d.ts.map +0 -1
  51. package/dist/src/providers/mdn-search.js +0 -59
  52. package/dist/src/providers/mdn-search.js.map +0 -1
  53. package/dist/src/providers/rfc-search.d.ts +0 -9
  54. package/dist/src/providers/rfc-search.d.ts.map +0 -1
  55. package/dist/src/providers/rfc-search.js +0 -38
  56. package/dist/src/providers/rfc-search.js.map +0 -1
  57. package/dist/src/providers/w3c-search.d.ts +0 -6
  58. package/dist/src/providers/w3c-search.d.ts.map +0 -1
  59. package/dist/src/providers/w3c-search.js +0 -42
  60. package/dist/src/providers/w3c-search.js.map +0 -1
  61. package/dist/src/providers/whatwg-search.d.ts +0 -8
  62. package/dist/src/providers/whatwg-search.d.ts.map +0 -1
  63. package/dist/src/providers/whatwg-search.js +0 -31
  64. package/dist/src/providers/whatwg-search.js.map +0 -1
  65. package/dist/src/providers/wiki-search.d.ts +0 -25
  66. package/dist/src/providers/wiki-search.d.ts.map +0 -1
  67. package/dist/src/providers/wiki-search.js +0 -50
  68. package/dist/src/providers/wiki-search.js.map +0 -1
  69. package/dist/src/renderers.d.ts +0 -18
  70. package/dist/src/renderers.d.ts.map +0 -1
  71. package/dist/src/renderers.js +0 -43
  72. package/dist/src/renderers.js.map +0 -1
  73. package/dist/src/safe.d.ts +0 -13
  74. package/dist/src/safe.d.ts.map +0 -1
  75. package/dist/src/safe.js +0 -17
  76. package/dist/src/safe.js.map +0 -1
  77. package/dist/src/search.d.ts +0 -13
  78. package/dist/src/search.d.ts.map +0 -1
  79. package/dist/src/search.js +0 -336
  80. package/dist/src/search.js.map +0 -1
  81. package/dist/src/strategies.d.ts +0 -13
  82. package/dist/src/strategies.d.ts.map +0 -1
  83. package/dist/src/strategies.js +0 -48
  84. package/dist/src/strategies.js.map +0 -1
  85. package/dist/src/user-agent.d.ts +0 -2
  86. package/dist/src/user-agent.d.ts.map +0 -1
  87. package/dist/src/user-agent.js +0 -3
  88. package/dist/src/user-agent.js.map +0 -1
  89. package/dist/src/v1-message.d.ts +0 -2
  90. package/dist/src/v1-message.d.ts.map +0 -1
  91. package/dist/src/v1-message.js +0 -11
  92. package/dist/src/v1-message.js.map +0 -1
  93. package/search.ts +0 -22
  94. package/src/cache.test.ts +0 -14
  95. package/src/cache.ts +0 -61
  96. package/src/format.test.ts +0 -175
  97. package/src/format.ts +0 -72
  98. package/src/html.ts +0 -39
  99. package/src/normalize-blurb.test.ts +0 -67
  100. package/src/normalize-blurb.ts +0 -22
  101. package/src/opencode/notify.ts +0 -16
  102. package/src/parse-results.test.ts +0 -75
  103. package/src/parse-results.ts +0 -22
  104. package/src/providers/brave-search.ts +0 -39
  105. package/src/providers/bsky-search.test.ts +0 -16
  106. package/src/providers/bsky-search.ts +0 -88
  107. package/src/providers/ddg-search.test.ts +0 -15
  108. package/src/providers/ddg-search.ts +0 -117
  109. package/src/providers/gnews-search.ts +0 -72
  110. package/src/providers/mdn-search.test.ts +0 -17
  111. package/src/providers/mdn-search.ts +0 -83
  112. package/src/providers/rfc-search.test.ts +0 -31
  113. package/src/providers/rfc-search.ts +0 -61
  114. package/src/providers/w3c-search.test.ts +0 -22
  115. package/src/providers/w3c-search.ts +0 -67
  116. package/src/providers/whatwg-search.test.ts +0 -21
  117. package/src/providers/whatwg-search.ts +0 -55
  118. package/src/providers/wiki-search.test.ts +0 -16
  119. package/src/providers/wiki-search.ts +0 -72
  120. package/src/renderers.test.ts +0 -263
  121. package/src/renderers.ts +0 -62
  122. package/src/safe.test.ts +0 -87
  123. package/src/safe.ts +0 -27
  124. package/src/search.ts +0 -401
  125. package/src/strategies.test.ts +0 -92
  126. package/src/strategies.ts +0 -62
  127. package/src/user-agent.ts +0 -3
  128. package/src/v1-message.test.ts +0 -17
  129. package/src/v1-message.ts +0 -10
@@ -1,175 +0,0 @@
1
- import { describe, expect, it } from 'bun:test'
2
- import { formatDate, formatDateTime, formatHeader, formatResults } from './format.ts'
3
-
4
- describe('formatHeader', () => {
5
- it('formats basic header with defaults', () => {
6
- const result = formatHeader('Test results', {
7
- total: 10,
8
- count: 5,
9
- })
10
- expect(result).toBe('Test results (showing 1-5 of 10, page 1)')
11
- })
12
-
13
- it('includes offset in range', () => {
14
- const result = formatHeader('Test results', {
15
- total: 50,
16
- count: 10,
17
- limit: 10,
18
- offset: 20,
19
- })
20
- expect(result).toBe('Test results (showing 21-30 of 50, page 3)')
21
- })
22
-
23
- it('calculates page 1 when limit matches count', () => {
24
- const result = formatHeader('Test results', {
25
- total: 25,
26
- count: 25,
27
- limit: 25,
28
- offset: 0,
29
- })
30
- expect(result).toBe('Test results (showing 1-25 of 25, page 1)')
31
- })
32
-
33
- it('handles zero count', () => {
34
- const result = formatHeader('Empty', {
35
- total: 0,
36
- count: 0,
37
- })
38
- expect(result).toBe('Empty (0 results)')
39
- })
40
- })
41
-
42
- describe('formatDate', () => {
43
- it('formats a valid ISO date string without time', () => {
44
- const result = formatDate('2025-06-15T14:30:00.000Z')
45
- expect(result).toContain('Jun')
46
- expect(result).toContain('2025')
47
- expect(result).not.toContain(':')
48
- })
49
-
50
- it('returns raw string for invalid date', () => {
51
- expect(formatDate('not-a-date')).toBe('not-a-date')
52
- })
53
- })
54
-
55
- describe('formatDateTime', () => {
56
- it('includes time', () => {
57
- const result = formatDateTime('2025-01-01T03:05:00.000Z')
58
- expect(result).toMatch(/AM|PM/)
59
- expect(result).toMatch(/:05/)
60
- })
61
-
62
- it('returns raw string for invalid date', () => {
63
- expect(formatDateTime('not-a-date')).toBe('not-a-date')
64
- })
65
- })
66
-
67
- describe('formatResults', () => {
68
- it('renders header and items using renderItem callback', () => {
69
- const result = formatResults({
70
- label: 'Test',
71
- items: ['a', 'b'],
72
- total: 2,
73
- renderItem: (item, i) => `${i + 1}. ${item}`,
74
- })
75
- expect(result).toBe([
76
- 'Test (showing 1-2 of 2, page 1)',
77
- '',
78
- '1. a',
79
- '',
80
- '2. b',
81
- ].join('\n'))
82
- })
83
-
84
- it('supports multi-line renderItem output', () => {
85
- const result = formatResults({
86
- label: 'Multi',
87
- items: [{ title: 'Foo', url: 'https://foo.com' }],
88
- total: 1,
89
- renderItem: (item) => `${item.title}\n ${item.url}`,
90
- })
91
- expect(result).toBe([
92
- 'Multi (showing 1-1 of 1, page 1)',
93
- '',
94
- 'Foo',
95
- ' https://foo.com',
96
- ].join('\n'))
97
- })
98
-
99
- it('passes limit and offset to header', () => {
100
- const result = formatResults({
101
- label: 'Paged',
102
- items: ['x'],
103
- total: 30,
104
- limit: 10,
105
- offset: 10,
106
- renderItem: (item) => item,
107
- })
108
- expect(result).toContain('showing 11-11 of 30, page 2')
109
- })
110
-
111
- it('handles empty items array', () => {
112
- const result = formatResults({
113
- label: 'Empty',
114
- items: [],
115
- total: 0,
116
- renderItem: () => 'should not appear',
117
- })
118
- expect(result).toBe('Empty (0 results)')
119
- })
120
- })
121
-
122
- describe('formatResults', () => {
123
- it('renders header and items using renderItem callback', () => {
124
- const result = formatResults({
125
- label: 'Test',
126
- items: ['a', 'b'],
127
- total: 2,
128
- renderItem: (item, i) => `${i + 1}. ${item}`,
129
- })
130
- expect(result).toBe([
131
- 'Test (showing 1-2 of 2, page 1)',
132
- '',
133
- '1. a',
134
- '',
135
- '2. b',
136
- ].join('\n'))
137
- })
138
-
139
- it('supports multi-line renderItem output', () => {
140
- const result = formatResults({
141
- label: 'Multi',
142
- items: [{ title: 'Foo', url: 'https://foo.com' }],
143
- total: 1,
144
- renderItem: (item) => `${item.title}\n ${item.url}`,
145
- })
146
- expect(result).toBe([
147
- 'Multi (showing 1-1 of 1, page 1)',
148
- '',
149
- 'Foo',
150
- ' https://foo.com',
151
- ].join('\n'))
152
- })
153
-
154
- it('passes limit and offset to header', () => {
155
- const result = formatResults({
156
- label: 'Paged',
157
- items: ['x'],
158
- total: 30,
159
- limit: 10,
160
- offset: 10,
161
- renderItem: (item) => item,
162
- })
163
- expect(result).toContain('showing 11-11 of 30, page 2')
164
- })
165
-
166
- it('handles empty items array', () => {
167
- const result = formatResults({
168
- label: 'Empty',
169
- items: [],
170
- total: 0,
171
- renderItem: () => 'should not appear',
172
- })
173
- expect(result).toBe('Empty (0 results)')
174
- })
175
- })
package/src/format.ts DELETED
@@ -1,72 +0,0 @@
1
- type PaginationOptions = {
2
- total: number
3
- count: number
4
- limit?: number
5
- offset?: number
6
- }
7
-
8
- export const formatHeader = (label: string, options: PaginationOptions): string => {
9
- const { total, count, limit } = options
10
- const offset = options.offset ?? 0
11
-
12
- if (count === 0) return label + ' (0 results)'
13
-
14
- const start = offset + 1
15
- const end = offset + count
16
- const pageSize = limit ?? count
17
- const page = pageSize > 0 ? Math.floor(offset / pageSize) + 1 : 1
18
-
19
- return label + ' (showing ' + start + '-' + end + ' of ' + total + ', page ' + page + ')'
20
- }
21
-
22
- const DATE_FMT: Intl.DateTimeFormatOptions = {
23
- month: 'short',
24
- day: 'numeric',
25
- year: 'numeric',
26
- }
27
-
28
- const DATETIME_FMT: Intl.DateTimeFormatOptions = {
29
- month: 'short',
30
- day: 'numeric',
31
- year: 'numeric',
32
- hour: 'numeric',
33
- minute: '2-digit',
34
- }
35
-
36
- const parseDate = (raw: string): Date | null => {
37
- const d = new Date(raw)
38
- return isNaN(d.getTime()) ? null : d
39
- }
40
-
41
- export const formatDate = (raw: string): string => {
42
- const d = parseDate(raw)
43
- return d ? d.toLocaleString('en-US', DATE_FMT) : raw
44
- }
45
-
46
- export const formatDateTime = (raw: string): string => {
47
- const d = parseDate(raw)
48
- return d ? d.toLocaleString('en-US', DATETIME_FMT) : raw
49
- }
50
-
51
- type FormatResultsOptions<T> = {
52
- label: string
53
- items: Array<T>
54
- total: number
55
- limit?: number
56
- offset?: number
57
- renderItem: (item: T, index: number) => string
58
- }
59
-
60
- export const formatResults = <T>(options: FormatResultsOptions<T>): string => {
61
- const { label, items, total, limit, offset, renderItem } = options
62
-
63
- const header = formatHeader(label, { total, count: items.length, limit, offset })
64
-
65
- if (items.length === 0) return header
66
-
67
- const body = items
68
- .map((item, i) => renderItem(item, i))
69
- .join('\n\n')
70
-
71
- return header + '\n\n' + body
72
- }
package/src/html.ts DELETED
@@ -1,39 +0,0 @@
1
- import { JSDOM, VirtualConsole } from 'jsdom'
2
- import { baseHeaders, BlockedError } from './strategies.ts'
3
-
4
- export type SearchResult = {
5
- title: string
6
- url: string
7
- abstract: string
8
- }
9
-
10
- export const makeDom = (html: string, url: string): Document => {
11
- const virtualConsole = new VirtualConsole()
12
- virtualConsole.on('jsdomError', () => {
13
- // Silently ignore JSDOM errors
14
- })
15
-
16
- return new JSDOM(html, { url, virtualConsole }).window.document
17
- }
18
-
19
- export const fetchHtml = async (url: string, userAgent: string): Promise<string> => {
20
- const res = await fetch(url, { headers: baseHeaders(userAgent) })
21
-
22
- // A bare status code reads as a transient fault and invites a retry.
23
- if (res.status === 403 || res.status === 429) {
24
- const body = await res.text().catch(() => '')
25
- // Matches the status heading these pages repeat before the useful sentence.
26
- const reason = body
27
- .replace(/<[^>]+>/g, ' ')
28
- .replace(/\s+/g, ' ')
29
- .replace(/\b\d{3}\s*-?\s*(Forbidden|Too Many Requests)\b/gi, '')
30
- .trim()
31
- .slice(0, 140)
32
-
33
- throw new BlockedError(new URL(url).hostname + (reason ? ' — ' + reason : ''))
34
- }
35
-
36
- if (!res.ok) throw new Error(res.status + ' ' + res.statusText)
37
-
38
- return res.text()
39
- }
@@ -1,67 +0,0 @@
1
- import { describe, expect, it } from 'bun:test'
2
- import { normalizeBlurb } from './normalize-blurb.ts'
3
-
4
- describe('normalizeBlurb', () => {
5
- it('strips HTML tags', () => {
6
- expect(normalizeBlurb('<b>bold</b> and <em>italic</em>')).toBe('bold and italic')
7
- })
8
-
9
- it('strips self-closing tags', () => {
10
- expect(normalizeBlurb('line one<br/>line two')).toBe('line one line two')
11
- })
12
-
13
- it('strips tags with attributes', () => {
14
- expect(normalizeBlurb('<a href="https://example.com">link</a>')).toBe('link')
15
- })
16
-
17
- it('decodes named HTML entities', () => {
18
- expect(normalizeBlurb('rock &amp; roll')).toBe('rock & roll')
19
- expect(normalizeBlurb('1 &lt; 2 &gt; 0')).toBe('1 < 2 > 0')
20
- expect(normalizeBlurb('&quot;hello&quot;')).toBe('"hello"')
21
- expect(normalizeBlurb('it&apos;s fine')).toBe("it's fine")
22
- expect(normalizeBlurb('non&nbsp;breaking')).toBe('non breaking')
23
- })
24
-
25
- it('decodes numeric character references', () => {
26
- expect(normalizeBlurb('&#39;quoted&#39;')).toBe("'quoted'")
27
- expect(normalizeBlurb('&#169; 2026')).toBe('© 2026')
28
- })
29
-
30
- it('decodes hex character references', () => {
31
- expect(normalizeBlurb('&#x27;hey&#x27;')).toBe("'hey'")
32
- expect(normalizeBlurb('&#xA9; 2026')).toBe('© 2026')
33
- })
34
-
35
- it('collapses multiple spaces', () => {
36
- expect(normalizeBlurb('too many spaces')).toBe('too many spaces')
37
- })
38
-
39
- it('collapses newlines and tabs', () => {
40
- expect(normalizeBlurb('line one\nline two\ttab')).toBe('line one line two tab')
41
- })
42
-
43
- it('collapses carriage returns', () => {
44
- expect(normalizeBlurb('hello\r\nworld')).toBe('hello world')
45
- })
46
-
47
- it('trims leading and trailing whitespace', () => {
48
- expect(normalizeBlurb(' padded ')).toBe('padded')
49
- })
50
-
51
- it('handles combined mess', () => {
52
- const input = ' <span class="highlight">Rock &amp; Roll</span>\n\n is &#x27;great&#x27; '
53
- expect(normalizeBlurb(input)).toBe("Rock & Roll is 'great'")
54
- })
55
-
56
- it('returns empty string for empty input', () => {
57
- expect(normalizeBlurb('')).toBe('')
58
- })
59
-
60
- it('returns empty string for whitespace-only input', () => {
61
- expect(normalizeBlurb(' \n\t ')).toBe('')
62
- })
63
-
64
- it('passes through clean text unchanged', () => {
65
- expect(normalizeBlurb('nothing to normalize')).toBe('nothing to normalize')
66
- })
67
- })
@@ -1,22 +0,0 @@
1
- const HTML_ENTITIES: Record<string, string> = {
2
- '&amp;': '&',
3
- '&lt;': '<',
4
- '&gt;': '>',
5
- '&quot;': '"',
6
- '&#39;': "'",
7
- '&apos;': "'",
8
- '&nbsp;': ' ',
9
- }
10
-
11
- const decodeEntities = (text: string): string => {
12
- return text
13
- .replace(/&(?:amp|lt|gt|quot|apos|nbsp|#39);/g, (m) => HTML_ENTITIES[m] ?? m)
14
- .replace(/&#(\d+);/g, (_, n) => String.fromCharCode(Number(n)))
15
- .replace(/&#x([0-9a-fA-F]+);/g, (_, h) => String.fromCharCode(parseInt(h, 16)))
16
- }
17
-
18
- export const normalizeBlurb = (text: string): string => {
19
- const stripped = text.replace(/<[^>]*>/g, ' ')
20
- const decoded = decodeEntities(stripped)
21
- return decoded.replace(/[\s\n\r\t]+/g, ' ').trim()
22
- }
@@ -1,16 +0,0 @@
1
- import type { Plugin } from '@opencode/plugin'
2
- import type * as Tool from '@opencode/plugin/promise/tool'
3
-
4
- type SendResultOptions = {
5
- context: Plugin.Context
6
- sessionID: Tool.ToolContext['sessionID']
7
- text: string
8
- }
9
-
10
- export const sendResult = async (options: SendResultOptions): Promise<void> => {
11
- await options.context.session.synthetic({
12
- sessionID: options.sessionID,
13
- text: options.text,
14
- description: 'Search results',
15
- })
16
- }
@@ -1,75 +0,0 @@
1
- import { describe, expect, it } from 'bun:test'
2
- import { JSDOM } from 'jsdom'
3
- import { parseResults } from './parse-results.ts'
4
-
5
- const makeDoc = (html: string): Document => {
6
- const dom = new JSDOM(html)
7
- return dom.window.document
8
- }
9
-
10
- describe('parseResults', () => {
11
- it('extracts titles, links, and snippets', () => {
12
- const html = [
13
- '<div class="result">',
14
- ' <a class="result__a" href="https://example.com">Example Title</a>',
15
- ' <span class="result__snippet">Example snippet text</span>',
16
- '</div>',
17
- '<div class="result">',
18
- ' <a class="result__a" href="https://other.com">Other Title</a>',
19
- ' <span class="result__snippet">Other snippet text</span>',
20
- '</div>',
21
- ].join('\n')
22
-
23
- const result = parseResults(makeDoc(html))
24
- const expected = [
25
- 'Example Title',
26
- 'https://example.com',
27
- 'Example snippet text',
28
- '',
29
- 'Other Title',
30
- 'https://other.com',
31
- 'Other snippet text',
32
- ].join('\n')
33
-
34
- expect(result).toEqual(expected)
35
- })
36
-
37
- it('skips results with no title', () => {
38
- const html = [
39
- '<div class="result">',
40
- ' <a class="result__a" href="https://empty.com"></a>',
41
- ' <span class="result__snippet">Should be skipped</span>',
42
- '</div>',
43
- '<div class="result">',
44
- ' <a class="result__a" href="https://valid.com">Valid</a>',
45
- ' <span class="result__snippet">Kept</span>',
46
- '</div>',
47
- ].join('\n')
48
-
49
- const result = parseResults(makeDoc(html))
50
- const expected = [
51
- 'Valid',
52
- 'https://valid.com',
53
- 'Kept',
54
- ].join('\n')
55
-
56
- expect(result).toEqual(expected)
57
- })
58
-
59
- it('handles missing snippets', () => {
60
- const html = '<a class="result__a" href="https://example.com">Title Only</a>'
61
-
62
- const result = parseResults(makeDoc(html))
63
- const expected = [
64
- 'Title Only',
65
- 'https://example.com',
66
- ].join('\n')
67
-
68
- expect(result).toEqual(expected)
69
- })
70
-
71
- it('returns empty string for no results', () => {
72
- const result = parseResults(makeDoc('<div>no results here</div>'))
73
- expect(result).toEqual('')
74
- })
75
- })
@@ -1,22 +0,0 @@
1
- export const parseResults = (doc: Document): string => {
2
- const links = Array.from(doc.querySelectorAll('.result__a'))
3
- const snippets = doc.querySelectorAll('.result__snippet')
4
-
5
- const lines = links.flatMap((link, i) => {
6
- const title = link.textContent
7
- if (!title) return []
8
-
9
- const href = link.getAttribute('href')
10
- const snippet = snippets[i]
11
- const text = snippet ? snippet.textContent : null
12
-
13
- return [
14
- title.trim(),
15
- ...(href ? [href.trim()] : []),
16
- ...(text ? [text.trim()] : []),
17
- '',
18
- ]
19
- })
20
-
21
- return lines.join('\n').trim()
22
- }
@@ -1,39 +0,0 @@
1
- import * as cache from '../cache.ts'
2
- import { fetchHtml, makeDom, type SearchResult } from '../html.ts'
3
- import { detectBlock, nextUserAgent } from '../strategies.ts'
4
-
5
- const BRAVE_URL = 'https://search.brave.com/search'
6
-
7
- // A challenge returns HTTP 200, so without this a block reads as an empty result set.
8
- // Only phrases unique to it: a bare 'captcha' also matches normal results pages.
9
- const BLOCK_MARKERS = ['Verifying you are human', 'challenge-error']
10
-
11
- export type BraveResult = SearchResult
12
-
13
- export const braveSearch = async (query: string): Promise<BraveResult[]> => {
14
- const cacheKey = 'brave:' + query
15
-
16
- const cached = await cache.get<BraveResult[]>(cacheKey)
17
- if (cached) return cached
18
-
19
- const html = await fetchHtml(BRAVE_URL + '?q=' + encodeURIComponent(query), nextUserAgent())
20
- detectBlock('Brave', html, BLOCK_MARKERS)
21
-
22
- const doc = makeDom(html, BRAVE_URL)
23
-
24
- const containers = doc.querySelectorAll('div.snippet[data-type="web"]')
25
- const results: BraveResult[] = []
26
-
27
- for (const container of containers) {
28
- const anchor = container.querySelector('a')
29
- const title = container.querySelector('.title')?.textContent?.trim() ?? ''
30
- const url = anchor?.getAttribute('href') ?? ''
31
- const abstract = container.querySelector('.generic-snippet .content')?.textContent?.trim() ?? ''
32
-
33
- if (title && url.startsWith('http')) results.push({ title, url, abstract })
34
- }
35
-
36
- await cache.set(cacheKey, results)
37
-
38
- return results
39
- }
@@ -1,16 +0,0 @@
1
- import { describe, expect, it } from 'bun:test'
2
- import { bskySearch } from './bsky-search.ts'
3
-
4
- describe('bskySearch', () => {
5
- it('returns post results', async () => {
6
- const results = await bskySearch({ query: 'atproto' })
7
-
8
- expect(results.posts.length > 0).toEqual(true)
9
-
10
- const first = results.posts[0]
11
- if (!first) throw new Error('expected at least one post')
12
- expect(first.uri).toBeDefined()
13
- expect(first.author).toBeDefined()
14
- expect(first.record).toBeDefined()
15
- })
16
- })
@@ -1,88 +0,0 @@
1
- import { z } from 'zod'
2
- import * as cache from '../cache.ts'
3
-
4
- const BSKY_API = 'https://api.bsky.app/xrpc/app.bsky.feed.searchPosts'
5
-
6
- const authorSchema = z.object({
7
- did: z.string().default(''),
8
- handle: z.string().min(1),
9
- displayName: z.string().default(''),
10
- })
11
-
12
- const recordSchema = z.object({
13
- text: z.string().min(1),
14
- createdAt: z.string().default(''),
15
- })
16
-
17
- const postSchema = z.object({
18
- uri: z.string().default(''),
19
- cid: z.string().default(''),
20
- author: authorSchema,
21
- record: recordSchema,
22
- likeCount: z.number().default(0),
23
- repostCount: z.number().default(0),
24
- replyCount: z.number().default(0),
25
- })
26
-
27
- const responseSchema = z.object({
28
- posts: z.array(z.unknown()),
29
- hitsTotal: z.number().optional(),
30
- cursor: z.string().optional(),
31
- })
32
-
33
- export type BskyPost = z.infer<typeof postSchema>
34
-
35
- export type BskySearchResult = {
36
- posts: Array<BskyPost>
37
- hitsTotal: number
38
- cursor: string
39
- }
40
-
41
- type BskySearchOptions = {
42
- query: string
43
- sort?: string
44
- limit?: number
45
- }
46
-
47
- export const bskySearch = async (options: BskySearchOptions): Promise<BskySearchResult> => {
48
- const cacheKey = [
49
- 'bsky',
50
- options.query,
51
- options.sort ?? 'latest',
52
- options.limit ?? 25,
53
- ].join(':')
54
-
55
- const cached = await cache.get<BskySearchResult>(cacheKey)
56
- if (cached && cached.posts && cached.posts.length > 0) return cached
57
-
58
- const params = new URLSearchParams({ q: options.query })
59
-
60
- if (options.sort) params.set('sort', options.sort)
61
-
62
- if (options.limit) params.set('limit', String(options.limit))
63
-
64
- const url = BSKY_API + '?' + params.toString()
65
- const res = await fetch(url)
66
-
67
- if (!res.ok) {
68
- const text = await res.text()
69
- throw new Error('Bluesky search error (' + res.status + '): ' + text)
70
- }
71
-
72
- const json: unknown = await res.json()
73
- const response = responseSchema.parse(json)
74
-
75
- const posts = response.posts.flatMap((raw) => {
76
- const parsed = postSchema.safeParse(raw)
77
- return parsed.success ? [parsed.data] : []
78
- })
79
-
80
- const hitsTotal = response.hitsTotal ?? posts.length
81
- const cursor = response.cursor ?? ''
82
-
83
- const result: BskySearchResult = { posts, hitsTotal, cursor }
84
-
85
- if (posts.length > 0) await cache.set(cacheKey, result)
86
-
87
- return result
88
- }
@@ -1,15 +0,0 @@
1
- import { describe, expect, it } from 'bun:test'
2
- import { ddgSearch } from './ddg-search.ts'
3
-
4
- describe('ddgSearch', () => {
5
- it('returns structured results', async () => {
6
- const results = await ddgSearch('typescript programming language')
7
-
8
- expect(Array.isArray(results)).toEqual(true)
9
- expect(results.length > 0).toEqual(true)
10
-
11
- expect(typeof results[0].title).toEqual('string')
12
- expect(typeof results[0].url).toEqual('string')
13
- expect(typeof results[0].abstract).toEqual('string')
14
- })
15
- })