opencode-search 1.0.0-next.3 → 1.0.0-next.7

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 (44) hide show
  1. package/README.md +7 -7
  2. package/REMOVED.md +3 -3
  3. package/dist/package.json +4 -2
  4. package/dist/src/providers/gnews-search.d.ts +1 -1
  5. package/dist/src/providers/gnews-search.d.ts.map +1 -1
  6. package/dist/src/providers/gnews-search.js +5 -3
  7. package/dist/src/providers/gnews-search.js.map +1 -1
  8. package/dist/src/search.d.ts.map +1 -1
  9. package/dist/src/search.js +35 -25
  10. package/dist/src/search.js.map +1 -1
  11. package/package.json +4 -2
  12. package/search.ts +22 -0
  13. package/src/cache.test.ts +14 -0
  14. package/src/cache.ts +61 -0
  15. package/src/format.test.ts +175 -0
  16. package/src/format.ts +72 -0
  17. package/src/html.ts +39 -0
  18. package/src/normalize-blurb.test.ts +67 -0
  19. package/src/normalize-blurb.ts +22 -0
  20. package/src/opencode/notify.ts +16 -0
  21. package/src/parse-results.test.ts +75 -0
  22. package/src/parse-results.ts +22 -0
  23. package/src/providers/brave-search.ts +39 -0
  24. package/src/providers/bsky-search.test.ts +16 -0
  25. package/src/providers/bsky-search.ts +88 -0
  26. package/src/providers/ddg-search.test.ts +15 -0
  27. package/src/providers/ddg-search.ts +117 -0
  28. package/src/providers/gnews-search.ts +72 -0
  29. package/src/providers/mdn-search.test.ts +17 -0
  30. package/src/providers/mdn-search.ts +83 -0
  31. package/src/providers/standard-search.test.ts +29 -0
  32. package/src/providers/standard-search.ts +94 -0
  33. package/src/providers/wiki-search.test.ts +16 -0
  34. package/src/providers/wiki-search.ts +72 -0
  35. package/src/renderers.test.ts +327 -0
  36. package/src/renderers.ts +51 -0
  37. package/src/safe.test.ts +87 -0
  38. package/src/safe.ts +27 -0
  39. package/src/search.ts +310 -0
  40. package/src/strategies.test.ts +92 -0
  41. package/src/strategies.ts +62 -0
  42. package/src/user-agent.ts +3 -0
  43. package/src/v1-message.test.ts +17 -0
  44. package/src/v1-message.ts +10 -0
package/src/search.ts ADDED
@@ -0,0 +1,310 @@
1
+ import type { Plugin } from '@opencode/plugin'
2
+ import type * as Tool from '@opencode/plugin/promise/tool'
3
+ import { formatResults } from './format.ts'
4
+ import { sendResult } from './opencode/notify.ts'
5
+ import { braveSearch } from './providers/brave-search.ts'
6
+ import { bskySearch } from './providers/bsky-search.ts'
7
+ import { ddgSearch } from './providers/ddg-search.ts'
8
+ import { gnewsSearch } from './providers/gnews-search.ts'
9
+ import { mdnSearch } from './providers/mdn-search.ts'
10
+ import { standardSearch } from './providers/standard-search.ts'
11
+ import { wikiSearch } from './providers/wiki-search.ts'
12
+ import {
13
+ renderBskyPost,
14
+ renderDdgResult,
15
+ renderGnewsItem,
16
+ renderMdnDoc,
17
+ renderStandardDoc,
18
+ renderWebResult,
19
+ renderWikiPage,
20
+ } from './renderers.ts'
21
+ import { safeAsync } from './safe.ts'
22
+
23
+ const postResult = async (context: Plugin.Context, ctx: Tool.ToolContext, text: string) => {
24
+ await safeAsync(() => sendResult({ context, sessionID: ctx.sessionID, text }))
25
+ }
26
+
27
+ // No `output` field: declaring one without a matching output schema in the tool definition makes
28
+ // OpenCode reject the result.
29
+ const textOutput = (text: string): Tool.Result => ({
30
+ content: [{ type: 'text', text }],
31
+ })
32
+
33
+ const isRecord = (value: unknown): value is Record<string, unknown> => (
34
+ typeof value === 'object' && value !== null && !Array.isArray(value)
35
+ )
36
+
37
+ const stringField = (input: unknown, key: string): string => {
38
+ if (!isRecord(input)) return ''
39
+
40
+ const value = input[key]
41
+ return typeof value === 'string' ? value : ''
42
+ }
43
+
44
+ const numberField = (input: unknown, key: string): number | undefined => {
45
+ if (!isRecord(input)) return undefined
46
+
47
+ const value = input[key]
48
+ return typeof value === 'number' ? value : undefined
49
+ }
50
+
51
+ const queryArg = {
52
+ type: 'object',
53
+ properties: {
54
+ query: { type: 'string', description: 'The search query' },
55
+ },
56
+ required: ['query'],
57
+ additionalProperties: false,
58
+ } as const
59
+
60
+ export const createDdgSearchTool = (context: Plugin.Context): Tool.Info => ({
61
+ name: 'ddg_search',
62
+ description: 'Search DuckDuckGo and return results as extracted text content',
63
+ input: queryArg,
64
+ async execute(input, ctx) {
65
+ const query = stringField(input, 'query')
66
+
67
+ const { data, error } = await safeAsync(() => ddgSearch(query))
68
+ if (error) return textOutput('DuckDuckGo search failed: ' + error.message)
69
+
70
+ const formatted = formatResults({
71
+ label: 'DuckDuckGo results',
72
+ items: data,
73
+ total: data.length,
74
+ renderItem: renderDdgResult,
75
+ })
76
+
77
+ await postResult(context, ctx, formatted)
78
+
79
+ return textOutput(formatted)
80
+ },
81
+ })
82
+
83
+ export const createBraveSearchTool = (context: Plugin.Context): Tool.Info => ({
84
+ name: 'brave_search',
85
+ description: 'Search the web via Brave Search, returning titles, URLs, and snippets.',
86
+ input: queryArg,
87
+ async execute(input, ctx) {
88
+ const query = stringField(input, 'query')
89
+
90
+ const { data, error } = await safeAsync(() => braveSearch(query))
91
+ if (error) return textOutput('Brave search failed: ' + error.message)
92
+
93
+ const formatted = formatResults({
94
+ label: 'Brave results',
95
+ items: data,
96
+ total: data.length,
97
+ renderItem: renderWebResult,
98
+ })
99
+
100
+ await postResult(context, ctx, formatted)
101
+
102
+ return textOutput(formatted)
103
+ },
104
+ })
105
+
106
+ export const createBskySearchTool = (context: Plugin.Context): Tool.Info => ({
107
+ name: 'bsky_search',
108
+ description: [
109
+ 'Search Bluesky posts via the AT Protocol.',
110
+ 'Returns posts with author, text, and engagement counts.',
111
+ ].join(' '),
112
+ input: {
113
+ type: 'object',
114
+ properties: {
115
+ query: { type: 'string', description: 'The search query' },
116
+ limit: { type: 'number', description: 'Maximum number of results to return' },
117
+ sort: { type: 'string', description: 'Sort order: top or latest' },
118
+ },
119
+ required: ['query'],
120
+ additionalProperties: false,
121
+ },
122
+ async execute(input, ctx) {
123
+ const query = stringField(input, 'query')
124
+ const limit = numberField(input, 'limit')
125
+ const sort = stringField(input, 'sort') || undefined
126
+
127
+ const { data, error } = await safeAsync(() => (
128
+ bskySearch({ query, limit, sort })
129
+ ))
130
+
131
+ if (error) return textOutput('Bluesky search failed: ' + error.message)
132
+
133
+ const formatted = formatResults({
134
+ label: 'Bluesky results',
135
+ items: data.posts,
136
+ total: data.hitsTotal,
137
+ limit,
138
+ offset: 0,
139
+ renderItem: renderBskyPost,
140
+ })
141
+
142
+ await postResult(context, ctx, formatted)
143
+
144
+ return textOutput(formatted)
145
+ },
146
+ })
147
+
148
+ export const createStandardSearchTool = (context: Plugin.Context): Tool.Info => ({
149
+ name: 'standard_search',
150
+ description: [
151
+ 'Search site.standard.document records on the AT Protocol.',
152
+ 'Returns blog posts and articles from the ATmosphere.',
153
+ ].join(' '),
154
+ input: {
155
+ type: 'object',
156
+ properties: {
157
+ query: { type: 'string', description: 'The search query' },
158
+ limit: { type: 'number', description: 'Maximum number of results to return' },
159
+ offset: { type: 'number', description: 'Number of results to skip' },
160
+ },
161
+ required: ['query'],
162
+ additionalProperties: false,
163
+ },
164
+ async execute(input, ctx) {
165
+ const query = stringField(input, 'query')
166
+ const limit = numberField(input, 'limit')
167
+ const offset = numberField(input, 'offset')
168
+
169
+ const { data, error } = await safeAsync(() => (
170
+ standardSearch({ query, limit, offset })
171
+ ))
172
+
173
+ if (error) return textOutput('Standard.site search failed: ' + error.message)
174
+
175
+ const total = Number(data.totalResults) || data.documents.length
176
+ const formatted = formatResults({
177
+ label: 'Standard.site results',
178
+ items: data.documents,
179
+ total,
180
+ limit,
181
+ offset,
182
+ renderItem: renderStandardDoc,
183
+ })
184
+
185
+ await postResult(context, ctx, formatted)
186
+
187
+ return textOutput(formatted)
188
+ },
189
+ })
190
+
191
+ export const createWikiSearchTool = (context: Plugin.Context): Tool.Info => ({
192
+ name: 'wiki_search',
193
+ description: 'Search Wikipedia articles. Returns page titles, descriptions, and excerpts.',
194
+ input: {
195
+ type: 'object',
196
+ properties: {
197
+ query: { type: 'string', description: 'The search query' },
198
+ limit: { type: 'number', description: 'Maximum number of results to return (1-100)' },
199
+ },
200
+ required: ['query'],
201
+ additionalProperties: false,
202
+ },
203
+ async execute(input, ctx) {
204
+ const query = stringField(input, 'query')
205
+ const limit = numberField(input, 'limit')
206
+
207
+ const { data, error } = await safeAsync(() => (
208
+ wikiSearch({ query, limit })
209
+ ))
210
+
211
+ if (error) return textOutput('Wikipedia search failed: ' + error.message)
212
+
213
+ const formatted = formatResults({
214
+ label: 'Wikipedia results',
215
+ items: data.pages,
216
+ total: data.pages.length,
217
+ limit,
218
+ offset: 0,
219
+ renderItem: renderWikiPage,
220
+ })
221
+
222
+ await postResult(context, ctx, formatted)
223
+
224
+ return textOutput(formatted)
225
+ },
226
+ })
227
+
228
+ export const createGnewsSearchTool = (context: Plugin.Context): Tool.Info => ({
229
+ name: 'gnews_search',
230
+ description: [
231
+ 'Search Google News for recent articles, returning headlines, publishers, and dates.',
232
+ 'News and tech media only — not general web search, and not documentation.',
233
+ 'Links are news.google.com redirect URLs, not publisher URLs.',
234
+ ].join(' '),
235
+ input: {
236
+ type: 'object',
237
+ properties: {
238
+ query: { type: 'string', description: 'The search query' },
239
+ limit: { type: 'number', description: 'Maximum number of results to return (default 20)' },
240
+ },
241
+ required: ['query'],
242
+ additionalProperties: false,
243
+ },
244
+ async execute(input, ctx) {
245
+ const query = stringField(input, 'query')
246
+ const limit = numberField(input, 'limit')
247
+
248
+ const { data, error } = await safeAsync(() => gnewsSearch(query, limit))
249
+ if (error) return textOutput('Google News search failed: ' + error.message)
250
+
251
+ const formatted = formatResults({
252
+ label: 'Google News results',
253
+ items: data,
254
+ total: data.length,
255
+ renderItem: renderGnewsItem,
256
+ })
257
+
258
+ await postResult(context, ctx, formatted)
259
+
260
+ return textOutput(formatted)
261
+ },
262
+ })
263
+
264
+ export const createMdnSearchTool = (context: Plugin.Context): Tool.Info => ({
265
+ name: 'mdn_search',
266
+ description: 'Search MDN Web Docs. Returns documentation pages for web technologies.',
267
+ input: {
268
+ type: 'object',
269
+ properties: {
270
+ query: { type: 'string', description: 'The search query' },
271
+ limit: { type: 'number', description: 'Maximum number of results to return' },
272
+ page: { type: 'number', description: 'Page number for pagination' },
273
+ },
274
+ required: ['query'],
275
+ additionalProperties: false,
276
+ },
277
+ async execute(input, ctx) {
278
+ const query = stringField(input, 'query')
279
+ const limit = numberField(input, 'limit')
280
+ const page = numberField(input, 'page')
281
+
282
+ const { data, error } = await safeAsync(() => (
283
+ mdnSearch({ query, limit, page })
284
+ ))
285
+
286
+ if (error) return textOutput('MDN search failed: ' + error.message)
287
+
288
+ const formatted = formatResults({
289
+ label: 'MDN Web Docs results',
290
+ items: data.documents,
291
+ total: data.total,
292
+ limit,
293
+ renderItem: renderMdnDoc,
294
+ })
295
+
296
+ await postResult(context, ctx, formatted)
297
+
298
+ return textOutput(formatted)
299
+ },
300
+ })
301
+
302
+ export const createSearchTools = (context: Plugin.Context): Tool.Info[] => [
303
+ createBraveSearchTool(context),
304
+ createBskySearchTool(context),
305
+ createDdgSearchTool(context),
306
+ createGnewsSearchTool(context),
307
+ createMdnSearchTool(context),
308
+ createStandardSearchTool(context),
309
+ createWikiSearchTool(context),
310
+ ]
@@ -0,0 +1,92 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { BlockedError, baseHeaders, detectBlock, nextUserAgent, runStrategies, USER_AGENTS } from './strategies.ts'
3
+
4
+ describe('nextUserAgent', () => {
5
+ it('rotates through the pool', () => {
6
+ const first = nextUserAgent()
7
+ const second = nextUserAgent()
8
+
9
+ expect(USER_AGENTS.includes(first)).toEqual(true)
10
+ expect(USER_AGENTS.includes(second)).toEqual(true)
11
+ expect(first === second).toEqual(false)
12
+ })
13
+ })
14
+
15
+ describe('baseHeaders', () => {
16
+ it('includes the user agent', () => {
17
+ const headers = baseHeaders('test-agent')
18
+
19
+ expect(headers['User-Agent']).toEqual('test-agent')
20
+ expect(typeof headers['Accept']).toEqual('string')
21
+ })
22
+ })
23
+
24
+ describe('detectBlock', () => {
25
+ it('throws BlockedError when a marker is present', () => {
26
+ expect(() => detectBlock('Test', '<div class="anomaly-modal">', ['anomaly-modal'])).toThrow(BlockedError)
27
+ })
28
+
29
+ it('does nothing when no marker is present', () => {
30
+ expect(() => detectBlock('Test', '<div>results</div>', ['anomaly-modal'])).not.toThrow()
31
+ })
32
+ })
33
+
34
+ describe('runStrategies', () => {
35
+ it('returns the first non-empty result', async () => {
36
+ const results = await runStrategies([
37
+ { name: 'a', run: async () => [] },
38
+ { name: 'b', run: async () => ['result'] },
39
+ ])
40
+
41
+ expect(results).toEqual(['result'])
42
+ })
43
+
44
+ it('falls through when a strategy throws', async () => {
45
+ const results = await runStrategies<string>([{
46
+ name: 'a',
47
+ run: async () => {
48
+ throw new Error('boom')
49
+ },
50
+ }, {
51
+ name: 'b',
52
+ run: async () => ['result'],
53
+ }])
54
+
55
+ expect(results).toEqual(['result'])
56
+ })
57
+
58
+ it('throws the last error when everything fails', async () => {
59
+ const attempt = runStrategies([
60
+ { name: 'a', run: async () => [] },
61
+ {
62
+ name: 'b',
63
+ run: async () => {
64
+ throw new Error('final failure')
65
+ },
66
+ },
67
+ ])
68
+
69
+ await expect(attempt).rejects.toThrow('final failure')
70
+ })
71
+
72
+ it('hands each strategy a different user agent', async () => {
73
+ const seen: string[] = []
74
+
75
+ await runStrategies([{
76
+ name: 'a',
77
+ run: async (userAgent) => {
78
+ seen.push(userAgent)
79
+ return []
80
+ },
81
+ }, {
82
+ name: 'b',
83
+ run: async (userAgent) => {
84
+ seen.push(userAgent)
85
+ return ['done']
86
+ },
87
+ }])
88
+
89
+ expect(seen.length).toEqual(2)
90
+ expect(seen[0] === seen[1]).toEqual(false)
91
+ })
92
+ })
@@ -0,0 +1,62 @@
1
+ import { safeAsync } from './safe.ts'
2
+
3
+ export const USER_AGENTS = [
4
+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
5
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
6
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15',
7
+ 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0',
8
+ ]
9
+
10
+ let uaIndex = 0
11
+
12
+ // Rotation gives each fallback attempt a fresh identity after a block
13
+ export const nextUserAgent = (): string => {
14
+ const agent = USER_AGENTS[uaIndex % USER_AGENTS.length]
15
+ uaIndex += 1
16
+
17
+ return agent ?? ''
18
+ }
19
+
20
+ export const baseHeaders = (userAgent: string): Record<string, string> => ({
21
+ 'User-Agent': userAgent,
22
+ 'Accept': 'text/html',
23
+ 'Accept-Encoding': 'gzip',
24
+ 'DNT': '1',
25
+ })
26
+
27
+ export class BlockedError extends Error {
28
+ constructor(source: string) {
29
+ super(source + ' blocked the request (CAPTCHA or rate limit)')
30
+ this.name = 'BlockedError'
31
+ }
32
+ }
33
+
34
+ export const detectBlock = (source: string, html: string, markers: string[]): void => {
35
+ for (const marker of markers) {
36
+ if (html.includes(marker)) throw new BlockedError(source)
37
+ }
38
+ }
39
+
40
+ export type Strategy<T> = {
41
+ name: string
42
+ run: (userAgent: string) => Promise<T[]>
43
+ }
44
+
45
+ // A strategy fails by throwing or by returning zero results; the next one gets a chance
46
+ export const runStrategies = async <T>(strategies: Strategy<T>[]): Promise<T[]> => {
47
+ let lastError: Error = new Error('no strategies provided')
48
+
49
+ for (const strategy of strategies) {
50
+ const { data, error } = await safeAsync(() => strategy.run(nextUserAgent()))
51
+ if (error) {
52
+ lastError = error
53
+ continue
54
+ }
55
+
56
+ if (data.length > 0) return data
57
+
58
+ lastError = new Error(strategy.name + ' returned no results')
59
+ }
60
+
61
+ throw lastError
62
+ }
@@ -0,0 +1,3 @@
1
+ import pkg from '../package.json'
2
+
3
+ export const USER_AGENT = pkg.name + '/' + pkg.version
@@ -0,0 +1,17 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { V1_MESSAGE } from './v1-message.ts'
3
+
4
+ describe('V1_MESSAGE', () => {
5
+ it('names the version to pin', () => {
6
+ expect(V1_MESSAGE).toContain('opencode-search@0.0.9')
7
+ })
8
+
9
+ it('offers the upgrade path as well as the pin', () => {
10
+ expect(V1_MESSAGE).toContain('OpenCode 2')
11
+ expect(V1_MESSAGE).toContain('https://opencode.ai/v2/docs')
12
+ })
13
+
14
+ it('says the v1 release is final, so pinning is not a temporary workaround', () => {
15
+ expect(V1_MESSAGE).toContain('will not be updated')
16
+ })
17
+ })
@@ -0,0 +1,10 @@
1
+ // Shown when OpenCode 1 loads this build, where the alternative is a module resolution error.
2
+ export const V1_MESSAGE = [
3
+ 'opencode-search 1.x requires OpenCode 2 and cannot load under OpenCode 1.',
4
+ '',
5
+ 'Either pin the last OpenCode 1 release, which will not be updated:',
6
+ '',
7
+ ' { "plugin": ["opencode-search@0.0.9"] }',
8
+ '',
9
+ 'or upgrade to OpenCode 2 (https://opencode.ai/v2/docs) and use the "plugins" key.',
10
+ ].join('\n')