opencode-search 1.0.0 → 1.1.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -27
- package/dist/{search.d.ts → search.d.mts} +1 -2
- package/dist/search.mjs +947 -0
- package/package.json +22 -16
- package/dist/package.json +0 -68
- package/dist/search.d.ts.map +0 -1
- package/dist/search.js +0 -20
- package/dist/search.js.map +0 -1
- package/dist/src/cache.d.ts +0 -3
- package/dist/src/cache.d.ts.map +0 -1
- package/dist/src/cache.js +0 -43
- package/dist/src/cache.js.map +0 -1
- package/dist/src/format.d.ts +0 -20
- package/dist/src/format.d.ts.map +0 -1
- package/dist/src/format.js +0 -46
- package/dist/src/format.js.map +0 -1
- package/dist/src/html.d.ts +0 -8
- package/dist/src/html.d.ts.map +0 -1
- package/dist/src/html.js +0 -28
- package/dist/src/html.js.map +0 -1
- package/dist/src/normalize-blurb.d.ts +0 -2
- package/dist/src/normalize-blurb.d.ts.map +0 -1
- package/dist/src/normalize-blurb.js +0 -21
- package/dist/src/normalize-blurb.js.map +0 -1
- package/dist/src/opencode/notify.d.ts +0 -10
- package/dist/src/opencode/notify.d.ts.map +0 -1
- package/dist/src/opencode/notify.js +0 -8
- package/dist/src/opencode/notify.js.map +0 -1
- package/dist/src/parse-results.d.ts +0 -2
- package/dist/src/parse-results.d.ts.map +0 -1
- package/dist/src/parse-results.js +0 -20
- package/dist/src/parse-results.js.map +0 -1
- package/dist/src/providers/brave-search.d.ts +0 -4
- package/dist/src/providers/brave-search.d.ts.map +0 -1
- package/dist/src/providers/brave-search.js +0 -29
- package/dist/src/providers/brave-search.js.map +0 -1
- package/dist/src/providers/bsky-search.d.ts +0 -31
- package/dist/src/providers/bsky-search.d.ts.map +0 -1
- package/dist/src/providers/bsky-search.js +0 -61
- package/dist/src/providers/bsky-search.js.map +0 -1
- package/dist/src/providers/ddg-search.d.ts +0 -7
- package/dist/src/providers/ddg-search.d.ts.map +0 -1
- package/dist/src/providers/ddg-search.js +0 -88
- package/dist/src/providers/ddg-search.js.map +0 -1
- package/dist/src/providers/gnews-search.d.ts +0 -9
- package/dist/src/providers/gnews-search.d.ts.map +0 -1
- package/dist/src/providers/gnews-search.js +0 -50
- package/dist/src/providers/gnews-search.js.map +0 -1
- package/dist/src/providers/mdn-search.d.ts +0 -22
- package/dist/src/providers/mdn-search.d.ts.map +0 -1
- package/dist/src/providers/mdn-search.js +0 -59
- package/dist/src/providers/mdn-search.js.map +0 -1
- package/dist/src/providers/rfc-search.d.ts +0 -9
- package/dist/src/providers/rfc-search.d.ts.map +0 -1
- package/dist/src/providers/rfc-search.js +0 -38
- package/dist/src/providers/rfc-search.js.map +0 -1
- package/dist/src/providers/w3c-search.d.ts +0 -6
- package/dist/src/providers/w3c-search.d.ts.map +0 -1
- package/dist/src/providers/w3c-search.js +0 -42
- package/dist/src/providers/w3c-search.js.map +0 -1
- package/dist/src/providers/whatwg-search.d.ts +0 -8
- package/dist/src/providers/whatwg-search.d.ts.map +0 -1
- package/dist/src/providers/whatwg-search.js +0 -31
- package/dist/src/providers/whatwg-search.js.map +0 -1
- package/dist/src/providers/wiki-search.d.ts +0 -25
- package/dist/src/providers/wiki-search.d.ts.map +0 -1
- package/dist/src/providers/wiki-search.js +0 -50
- package/dist/src/providers/wiki-search.js.map +0 -1
- package/dist/src/renderers.d.ts +0 -18
- package/dist/src/renderers.d.ts.map +0 -1
- package/dist/src/renderers.js +0 -43
- package/dist/src/renderers.js.map +0 -1
- package/dist/src/safe.d.ts +0 -13
- package/dist/src/safe.d.ts.map +0 -1
- package/dist/src/safe.js +0 -17
- package/dist/src/safe.js.map +0 -1
- package/dist/src/search.d.ts +0 -13
- package/dist/src/search.d.ts.map +0 -1
- package/dist/src/search.js +0 -336
- package/dist/src/search.js.map +0 -1
- package/dist/src/strategies.d.ts +0 -13
- package/dist/src/strategies.d.ts.map +0 -1
- package/dist/src/strategies.js +0 -48
- package/dist/src/strategies.js.map +0 -1
- package/dist/src/user-agent.d.ts +0 -2
- package/dist/src/user-agent.d.ts.map +0 -1
- package/dist/src/user-agent.js +0 -3
- package/dist/src/user-agent.js.map +0 -1
- package/dist/src/v1-message.d.ts +0 -2
- package/dist/src/v1-message.d.ts.map +0 -1
- package/dist/src/v1-message.js +0 -11
- package/dist/src/v1-message.js.map +0 -1
- package/search.ts +0 -22
- package/src/cache.test.ts +0 -14
- package/src/cache.ts +0 -61
- package/src/format.test.ts +0 -175
- package/src/format.ts +0 -72
- package/src/html.ts +0 -39
- package/src/normalize-blurb.test.ts +0 -67
- package/src/normalize-blurb.ts +0 -22
- package/src/opencode/notify.ts +0 -16
- package/src/parse-results.test.ts +0 -75
- package/src/parse-results.ts +0 -22
- package/src/providers/brave-search.ts +0 -39
- package/src/providers/bsky-search.test.ts +0 -16
- package/src/providers/bsky-search.ts +0 -88
- package/src/providers/ddg-search.test.ts +0 -15
- package/src/providers/ddg-search.ts +0 -117
- package/src/providers/gnews-search.ts +0 -72
- package/src/providers/mdn-search.test.ts +0 -17
- package/src/providers/mdn-search.ts +0 -83
- package/src/providers/rfc-search.test.ts +0 -31
- package/src/providers/rfc-search.ts +0 -61
- package/src/providers/w3c-search.test.ts +0 -22
- package/src/providers/w3c-search.ts +0 -67
- package/src/providers/whatwg-search.test.ts +0 -21
- package/src/providers/whatwg-search.ts +0 -55
- package/src/providers/wiki-search.test.ts +0 -16
- package/src/providers/wiki-search.ts +0 -72
- package/src/renderers.test.ts +0 -263
- package/src/renderers.ts +0 -62
- package/src/safe.test.ts +0 -87
- package/src/safe.ts +0 -27
- package/src/search.ts +0 -401
- package/src/strategies.test.ts +0 -92
- package/src/strategies.ts +0 -62
- package/src/user-agent.ts +0 -3
- package/src/v1-message.test.ts +0 -17
- package/src/v1-message.ts +0 -10
package/dist/src/safe.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export const safe = (fn) => {
|
|
2
|
-
try {
|
|
3
|
-
return { data: fn(), error: null };
|
|
4
|
-
}
|
|
5
|
-
catch (error) {
|
|
6
|
-
return { data: null, error: error instanceof Error ? error : new Error(String(error)) };
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
export const safeAsync = async (fn) => {
|
|
10
|
-
try {
|
|
11
|
-
return { data: await fn(), error: null };
|
|
12
|
-
}
|
|
13
|
-
catch (error) {
|
|
14
|
-
return { data: null, error: error instanceof Error ? error : new Error(String(error)) };
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=safe.js.map
|
package/dist/src/safe.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"safe.js","sourceRoot":"","sources":["../../src/safe.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,IAAI,GAAG,CAAI,EAAW,EAAiB,EAAE;IACpD,IAAI,CAAC;QACH,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;IACpC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAA;IACzF,CAAC;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAAK,EAAoB,EAA0B,EAAE;IACjF,IAAI,CAAC;QACH,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;IAC1C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAA;IACzF,CAAC;AACH,CAAC,CAAA"}
|
package/dist/src/search.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from '@opencode/plugin';
|
|
2
|
-
import type * as Tool from '@opencode/plugin/promise/tool';
|
|
3
|
-
export declare const createDdgSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
4
|
-
export declare const createBraveSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
5
|
-
export declare const createBskySearchTool: (context: Plugin.Context) => Tool.Info;
|
|
6
|
-
export declare const createWikiSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
7
|
-
export declare const createGnewsSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
8
|
-
export declare const createMdnSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
9
|
-
export declare const createRfcSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
10
|
-
export declare const createW3cSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
11
|
-
export declare const createWhatwgSearchTool: (context: Plugin.Context) => Tool.Info;
|
|
12
|
-
export declare const createSearchTools: (context: Plugin.Context) => Tool.Info[];
|
|
13
|
-
//# sourceMappingURL=search.d.ts.map
|
package/dist/src/search.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/search.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,KAAK,KAAK,IAAI,MAAM,+BAA+B,CAAA;AA8D1D,eAAO,MAAM,mBAAmB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAyBjE,CAAA;AAEF,eAAO,MAAM,qBAAqB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAyBnE,CAAA;AAEF,eAAO,MAAM,oBAAoB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAyClE,CAAA;AAEF,eAAO,MAAM,oBAAoB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAuClE,CAAA;AAEF,eAAO,MAAM,qBAAqB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAkCnE,CAAA;AAEF,eAAO,MAAM,mBAAmB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAwCjE,CAAA;AAEF,eAAO,MAAM,mBAAmB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAmCjE,CAAA;AAEF,eAAO,MAAM,mBAAmB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAmCjE,CAAA;AAEF,eAAO,MAAM,sBAAsB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAmCpE,CAAA;AAEF,eAAO,MAAM,iBAAiB,GAAI,SAAS,MAAM,CAAC,OAAO,KAAG,IAAI,CAAC,IAAI,EAUpE,CAAA"}
|
package/dist/src/search.js
DELETED
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import { formatResults } from "./format.js";
|
|
2
|
-
import { sendResult } from "./opencode/notify.js";
|
|
3
|
-
import { braveSearch } from "./providers/brave-search.js";
|
|
4
|
-
import { bskySearch } from "./providers/bsky-search.js";
|
|
5
|
-
import { ddgSearch } from "./providers/ddg-search.js";
|
|
6
|
-
import { gnewsSearch } from "./providers/gnews-search.js";
|
|
7
|
-
import { mdnSearch } from "./providers/mdn-search.js";
|
|
8
|
-
import { rfcSearch } from "./providers/rfc-search.js";
|
|
9
|
-
import { w3cSearch } from "./providers/w3c-search.js";
|
|
10
|
-
import { whatwgSearch } from "./providers/whatwg-search.js";
|
|
11
|
-
import { wikiSearch } from "./providers/wiki-search.js";
|
|
12
|
-
import { renderBskyPost, renderDdgResult, renderGnewsItem, renderMdnDoc, renderRfc, renderW3c, renderWhatwg, renderWebResult, renderWikiPage, } from "./renderers.js";
|
|
13
|
-
import { safeAsync } from "./safe.js";
|
|
14
|
-
const postResult = async (context, ctx, text) => {
|
|
15
|
-
await safeAsync(() => sendResult({ context, sessionID: ctx.sessionID, text }));
|
|
16
|
-
};
|
|
17
|
-
// No `output` field: declaring one without a matching output schema in the tool definition makes
|
|
18
|
-
// OpenCode reject the result.
|
|
19
|
-
const textOutput = (text) => ({
|
|
20
|
-
content: [{ type: 'text', text }],
|
|
21
|
-
});
|
|
22
|
-
const isRecord = (value) => (typeof value === 'object' && value !== null && !Array.isArray(value));
|
|
23
|
-
const stringField = (input, key) => {
|
|
24
|
-
if (!isRecord(input))
|
|
25
|
-
return '';
|
|
26
|
-
const value = input[key];
|
|
27
|
-
return typeof value === 'string' ? value : '';
|
|
28
|
-
};
|
|
29
|
-
const numberField = (input, key) => {
|
|
30
|
-
if (!isRecord(input))
|
|
31
|
-
return undefined;
|
|
32
|
-
const value = input[key];
|
|
33
|
-
return typeof value === 'number' ? value : undefined;
|
|
34
|
-
};
|
|
35
|
-
const queryArg = {
|
|
36
|
-
type: 'object',
|
|
37
|
-
properties: {
|
|
38
|
-
query: { type: 'string', description: 'The search query' },
|
|
39
|
-
},
|
|
40
|
-
required: ['query'],
|
|
41
|
-
additionalProperties: false,
|
|
42
|
-
};
|
|
43
|
-
export const createDdgSearchTool = (context) => ({
|
|
44
|
-
name: 'ddg_search',
|
|
45
|
-
description: [
|
|
46
|
-
'Search the web with DuckDuckGo when you need current information from the internet.',
|
|
47
|
-
'Use for general web queries: products, people, events, how-tos, anything not covered by a more specific tool.',
|
|
48
|
-
'Returns titles, URLs and snippets.',
|
|
49
|
-
].join(' '),
|
|
50
|
-
input: queryArg,
|
|
51
|
-
async execute(input, ctx) {
|
|
52
|
-
const query = stringField(input, 'query');
|
|
53
|
-
const { data, error } = await safeAsync(() => ddgSearch(query));
|
|
54
|
-
if (error)
|
|
55
|
-
return textOutput('DuckDuckGo search failed: ' + error.message);
|
|
56
|
-
const formatted = formatResults({
|
|
57
|
-
label: 'DuckDuckGo results',
|
|
58
|
-
items: data,
|
|
59
|
-
total: data.length,
|
|
60
|
-
renderItem: renderDdgResult,
|
|
61
|
-
});
|
|
62
|
-
await postResult(context, ctx, formatted);
|
|
63
|
-
return textOutput(formatted);
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
export const createBraveSearchTool = (context) => ({
|
|
67
|
-
name: 'brave_search',
|
|
68
|
-
description: [
|
|
69
|
-
'Search the web with Brave when you need current information from the internet.',
|
|
70
|
-
'The most reliable general web search here; prefer it over ddg_search when either would do.',
|
|
71
|
-
'Returns titles, URLs and snippets.',
|
|
72
|
-
].join(' '),
|
|
73
|
-
input: queryArg,
|
|
74
|
-
async execute(input, ctx) {
|
|
75
|
-
const query = stringField(input, 'query');
|
|
76
|
-
const { data, error } = await safeAsync(() => braveSearch(query));
|
|
77
|
-
if (error)
|
|
78
|
-
return textOutput('Brave search failed: ' + error.message);
|
|
79
|
-
const formatted = formatResults({
|
|
80
|
-
label: 'Brave results',
|
|
81
|
-
items: data,
|
|
82
|
-
total: data.length,
|
|
83
|
-
renderItem: renderWebResult,
|
|
84
|
-
});
|
|
85
|
-
await postResult(context, ctx, formatted);
|
|
86
|
-
return textOutput(formatted);
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
export const createBskySearchTool = (context) => ({
|
|
90
|
-
name: 'bsky_search',
|
|
91
|
-
description: [
|
|
92
|
-
'Search Bluesky posts for what people are saying about a topic right now.',
|
|
93
|
-
'Use for public reaction, discussion and opinion, not for documentation or reference material.',
|
|
94
|
-
'Returns author handle, post text, and like, repost and reply counts.',
|
|
95
|
-
].join(' '),
|
|
96
|
-
input: {
|
|
97
|
-
type: 'object',
|
|
98
|
-
properties: {
|
|
99
|
-
query: { type: 'string', description: 'The search query' },
|
|
100
|
-
limit: { type: 'number', description: 'Maximum number of results to return' },
|
|
101
|
-
sort: { type: 'string', description: 'Sort order: top or latest' },
|
|
102
|
-
},
|
|
103
|
-
required: ['query'],
|
|
104
|
-
additionalProperties: false,
|
|
105
|
-
},
|
|
106
|
-
async execute(input, ctx) {
|
|
107
|
-
const query = stringField(input, 'query');
|
|
108
|
-
const limit = numberField(input, 'limit');
|
|
109
|
-
const sort = stringField(input, 'sort') || undefined;
|
|
110
|
-
const { data, error } = await safeAsync(() => (bskySearch({ query, limit, sort })));
|
|
111
|
-
if (error)
|
|
112
|
-
return textOutput('Bluesky search failed: ' + error.message);
|
|
113
|
-
const formatted = formatResults({
|
|
114
|
-
label: 'Bluesky results',
|
|
115
|
-
items: data.posts,
|
|
116
|
-
total: data.hitsTotal,
|
|
117
|
-
limit,
|
|
118
|
-
offset: 0,
|
|
119
|
-
renderItem: renderBskyPost,
|
|
120
|
-
});
|
|
121
|
-
await postResult(context, ctx, formatted);
|
|
122
|
-
return textOutput(formatted);
|
|
123
|
-
},
|
|
124
|
-
});
|
|
125
|
-
export const createWikiSearchTool = (context) => ({
|
|
126
|
-
name: 'wiki_search',
|
|
127
|
-
description: [
|
|
128
|
-
'Look up a subject on Wikipedia for background, definitions and general reference.',
|
|
129
|
-
'Use when the question is about what something IS rather than what is happening with it.',
|
|
130
|
-
'Returns page titles, descriptions and excerpts.',
|
|
131
|
-
].join(' '),
|
|
132
|
-
input: {
|
|
133
|
-
type: 'object',
|
|
134
|
-
properties: {
|
|
135
|
-
query: { type: 'string', description: 'The search query' },
|
|
136
|
-
limit: { type: 'number', description: 'Maximum number of results to return (1-100)' },
|
|
137
|
-
},
|
|
138
|
-
required: ['query'],
|
|
139
|
-
additionalProperties: false,
|
|
140
|
-
},
|
|
141
|
-
async execute(input, ctx) {
|
|
142
|
-
const query = stringField(input, 'query');
|
|
143
|
-
const limit = numberField(input, 'limit');
|
|
144
|
-
const { data, error } = await safeAsync(() => (wikiSearch({ query, limit })));
|
|
145
|
-
if (error)
|
|
146
|
-
return textOutput('Wikipedia search failed: ' + error.message);
|
|
147
|
-
const formatted = formatResults({
|
|
148
|
-
label: 'Wikipedia results',
|
|
149
|
-
items: data.pages,
|
|
150
|
-
total: data.pages.length,
|
|
151
|
-
limit,
|
|
152
|
-
offset: 0,
|
|
153
|
-
renderItem: renderWikiPage,
|
|
154
|
-
});
|
|
155
|
-
await postResult(context, ctx, formatted);
|
|
156
|
-
return textOutput(formatted);
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
export const createGnewsSearchTool = (context) => ({
|
|
160
|
-
name: 'gnews_search',
|
|
161
|
-
description: [
|
|
162
|
-
'Search Google News for recent news coverage of a topic.',
|
|
163
|
-
'News and tech media only — not general web search, and not documentation.',
|
|
164
|
-
'Links are news.google.com redirect URLs that only open in a browser, not publisher URLs.',
|
|
165
|
-
].join(' '),
|
|
166
|
-
input: {
|
|
167
|
-
type: 'object',
|
|
168
|
-
properties: {
|
|
169
|
-
query: { type: 'string', description: 'The search query' },
|
|
170
|
-
limit: { type: 'number', description: 'Maximum number of results to return (default 20)' },
|
|
171
|
-
},
|
|
172
|
-
required: ['query'],
|
|
173
|
-
additionalProperties: false,
|
|
174
|
-
},
|
|
175
|
-
async execute(input, ctx) {
|
|
176
|
-
const query = stringField(input, 'query');
|
|
177
|
-
const limit = numberField(input, 'limit');
|
|
178
|
-
const { data, error } = await safeAsync(() => gnewsSearch(query, limit));
|
|
179
|
-
if (error)
|
|
180
|
-
return textOutput('Google News search failed: ' + error.message);
|
|
181
|
-
const formatted = formatResults({
|
|
182
|
-
label: 'Google News results',
|
|
183
|
-
items: data,
|
|
184
|
-
total: data.length,
|
|
185
|
-
renderItem: renderGnewsItem,
|
|
186
|
-
});
|
|
187
|
-
await postResult(context, ctx, formatted);
|
|
188
|
-
return textOutput(formatted);
|
|
189
|
-
},
|
|
190
|
-
});
|
|
191
|
-
export const createMdnSearchTool = (context) => ({
|
|
192
|
-
name: 'mdn_search',
|
|
193
|
-
description: [
|
|
194
|
-
'Look up web platform documentation on MDN: JavaScript, CSS, HTML, and browser APIs.',
|
|
195
|
-
'Use for how a web API behaves, its syntax, parameters or browser support.',
|
|
196
|
-
'Returns documentation pages with titles, URLs and summaries.',
|
|
197
|
-
].join(' '),
|
|
198
|
-
input: {
|
|
199
|
-
type: 'object',
|
|
200
|
-
properties: {
|
|
201
|
-
query: { type: 'string', description: 'The search query' },
|
|
202
|
-
limit: { type: 'number', description: 'Maximum number of results to return' },
|
|
203
|
-
page: { type: 'number', description: 'Page number for pagination' },
|
|
204
|
-
},
|
|
205
|
-
required: ['query'],
|
|
206
|
-
additionalProperties: false,
|
|
207
|
-
},
|
|
208
|
-
async execute(input, ctx) {
|
|
209
|
-
const query = stringField(input, 'query');
|
|
210
|
-
const limit = numberField(input, 'limit');
|
|
211
|
-
const page = numberField(input, 'page');
|
|
212
|
-
const { data, error } = await safeAsync(() => (mdnSearch({ query, limit, page })));
|
|
213
|
-
if (error)
|
|
214
|
-
return textOutput('MDN search failed: ' + error.message);
|
|
215
|
-
const formatted = formatResults({
|
|
216
|
-
label: 'MDN Web Docs results',
|
|
217
|
-
items: data.documents,
|
|
218
|
-
total: data.total,
|
|
219
|
-
limit,
|
|
220
|
-
renderItem: renderMdnDoc,
|
|
221
|
-
});
|
|
222
|
-
await postResult(context, ctx, formatted);
|
|
223
|
-
return textOutput(formatted);
|
|
224
|
-
},
|
|
225
|
-
});
|
|
226
|
-
export const createRfcSearchTool = (context) => ({
|
|
227
|
-
name: 'rfc_search',
|
|
228
|
-
description: [
|
|
229
|
-
'Look up an IETF RFC by number or title.',
|
|
230
|
-
'Accepts a number like 9421, RFC9421 or \'rfc 9421\' for a direct lookup, or words from the title to search.',
|
|
231
|
-
'Use for internet protocol specifications: HTTP, TCP, TLS, DNS, email, and so on.',
|
|
232
|
-
'Returns the RFC number, title, date, abstract, and a link to the full text.',
|
|
233
|
-
].join(' '),
|
|
234
|
-
input: {
|
|
235
|
-
type: 'object',
|
|
236
|
-
properties: {
|
|
237
|
-
query: { type: 'string', description: "An RFC number like 9421 or 'RFC9421', or words from the title" },
|
|
238
|
-
limit: { type: 'number', description: 'Maximum number of results to return (default 20)' },
|
|
239
|
-
},
|
|
240
|
-
required: ['query'],
|
|
241
|
-
additionalProperties: false,
|
|
242
|
-
},
|
|
243
|
-
async execute(input, ctx) {
|
|
244
|
-
const query = stringField(input, 'query');
|
|
245
|
-
const limit = numberField(input, 'limit');
|
|
246
|
-
const { data, error } = await safeAsync(() => rfcSearch(query, limit));
|
|
247
|
-
if (error)
|
|
248
|
-
return textOutput('RFC search failed: ' + error.message);
|
|
249
|
-
const formatted = formatResults({
|
|
250
|
-
label: 'RFC results',
|
|
251
|
-
items: data,
|
|
252
|
-
total: data.length,
|
|
253
|
-
renderItem: renderRfc,
|
|
254
|
-
});
|
|
255
|
-
await postResult(context, ctx, formatted);
|
|
256
|
-
return textOutput(formatted);
|
|
257
|
-
},
|
|
258
|
-
});
|
|
259
|
-
export const createW3cSearchTool = (context) => ({
|
|
260
|
-
name: 'w3c_search',
|
|
261
|
-
description: [
|
|
262
|
-
'Look up a W3C specification by words in its title.',
|
|
263
|
-
'Use for web standards W3C publishes: CSS, WCAG accessibility, SVG, WebRTC, and similar.',
|
|
264
|
-
'Matches titles only, so query the spec name rather than a concept inside it.',
|
|
265
|
-
'Returns the specification title and URL.',
|
|
266
|
-
].join(' '),
|
|
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 (default 20)' },
|
|
272
|
-
},
|
|
273
|
-
required: ['query'],
|
|
274
|
-
additionalProperties: false,
|
|
275
|
-
},
|
|
276
|
-
async execute(input, ctx) {
|
|
277
|
-
const query = stringField(input, 'query');
|
|
278
|
-
const limit = numberField(input, 'limit');
|
|
279
|
-
const { data, error } = await safeAsync(() => w3cSearch(query, limit));
|
|
280
|
-
if (error)
|
|
281
|
-
return textOutput('W3C search failed: ' + error.message);
|
|
282
|
-
const formatted = formatResults({
|
|
283
|
-
label: 'W3C results',
|
|
284
|
-
items: data,
|
|
285
|
-
total: data.length,
|
|
286
|
-
renderItem: renderW3c,
|
|
287
|
-
});
|
|
288
|
-
await postResult(context, ctx, formatted);
|
|
289
|
-
return textOutput(formatted);
|
|
290
|
-
},
|
|
291
|
-
});
|
|
292
|
-
export const createWhatwgSearchTool = (context) => ({
|
|
293
|
-
name: 'whatwg_search',
|
|
294
|
-
description: [
|
|
295
|
-
'Look up a WHATWG living standard such as HTML, DOM, Fetch, URL or Streams.',
|
|
296
|
-
'Use for the current web platform specs WHATWG maintains, which supersede older W3C versions.',
|
|
297
|
-
'There are only 27 standards, so expect few results.',
|
|
298
|
-
'Returns the standard name, URL and description.',
|
|
299
|
-
].join(' '),
|
|
300
|
-
input: {
|
|
301
|
-
type: 'object',
|
|
302
|
-
properties: {
|
|
303
|
-
query: { type: 'string', description: 'The search query' },
|
|
304
|
-
limit: { type: 'number', description: 'Maximum number of results to return (default 20)' },
|
|
305
|
-
},
|
|
306
|
-
required: ['query'],
|
|
307
|
-
additionalProperties: false,
|
|
308
|
-
},
|
|
309
|
-
async execute(input, ctx) {
|
|
310
|
-
const query = stringField(input, 'query');
|
|
311
|
-
const limit = numberField(input, 'limit');
|
|
312
|
-
const { data, error } = await safeAsync(() => whatwgSearch(query, limit));
|
|
313
|
-
if (error)
|
|
314
|
-
return textOutput('WHATWG search failed: ' + error.message);
|
|
315
|
-
const formatted = formatResults({
|
|
316
|
-
label: 'WHATWG results',
|
|
317
|
-
items: data,
|
|
318
|
-
total: data.length,
|
|
319
|
-
renderItem: renderWhatwg,
|
|
320
|
-
});
|
|
321
|
-
await postResult(context, ctx, formatted);
|
|
322
|
-
return textOutput(formatted);
|
|
323
|
-
},
|
|
324
|
-
});
|
|
325
|
-
export const createSearchTools = (context) => [
|
|
326
|
-
createBraveSearchTool(context),
|
|
327
|
-
createBskySearchTool(context),
|
|
328
|
-
createDdgSearchTool(context),
|
|
329
|
-
createGnewsSearchTool(context),
|
|
330
|
-
createMdnSearchTool(context),
|
|
331
|
-
createRfcSearchTool(context),
|
|
332
|
-
createW3cSearchTool(context),
|
|
333
|
-
createWhatwgSearchTool(context),
|
|
334
|
-
createWikiSearchTool(context),
|
|
335
|
-
];
|
|
336
|
-
//# sourceMappingURL=search.js.map
|
package/dist/src/search.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../src/search.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AACvD,OAAO,EACL,cAAc,EACd,eAAe,EACf,eAAe,EACf,YAAY,EACZ,SAAS,EACT,SAAS,EACT,YAAY,EACZ,eAAe,EACf,cAAc,GACf,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,MAAM,UAAU,GAAG,KAAK,EAAE,OAAuB,EAAE,GAAqB,EAAE,IAAY,EAAE,EAAE;IACxF,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;AAChF,CAAC,CAAA;AAED,iGAAiG;AACjG,8BAA8B;AAC9B,MAAM,UAAU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W,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QACzC,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAEzC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;QACzE,IAAI,KAAK;YAAE,OAAO,UAAU,CAAC,wBAAwB,GAAG,KAAK,CAAC,OAAO,CAAC,CAAA;QAEtE,MAAM,SAAS,GAAG,aAAa,CAAC;YAC9B,KAAK,EAAE,gBAAgB;YACvB,KAAK,EAAE,IAAI;YACX,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,UAAU,EAAE,YAAY;SACzB,CAAC,CAAA;QAEF,MAAM,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,SAAS,CAAC,CAAA;QAEzC,OAAO,UAAU,CAAC,SAAS,CAAC,CAAA;IAC9B,CAAC;CACF,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,OAAuB,EAAe,EAAE,CAAC;IACzE,qBAAqB,CAAC,OAAO,CAAC;IAC9B,oBAAoB,CAAC,OAAO,CAAC;IAC7B,mBAAmB,CAAC,OAAO,CAAC;IAC5B,qBAAqB,CAAC,OAAO,CAAC;IAC9B,mBAAmB,CAAC,OAAO,CAAC;IAC5B,mBAAmB,CAAC,OAAO,CAAC;IAC5B,mBAAmB,CAAC,OAAO,CAAC;IAC5B,sBAAsB,CAAC,OAAO,CAAC;IAC/B,oBAAoB,CAAC,OAAO,CAAC;CAC9B,CAAA"}
|
package/dist/src/strategies.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare const USER_AGENTS: string[];
|
|
2
|
-
export declare const nextUserAgent: () => string;
|
|
3
|
-
export declare const baseHeaders: (userAgent: string) => Record<string, string>;
|
|
4
|
-
export declare class BlockedError extends Error {
|
|
5
|
-
constructor(source: string);
|
|
6
|
-
}
|
|
7
|
-
export declare const detectBlock: (source: string, html: string, markers: string[]) => void;
|
|
8
|
-
export type Strategy<T> = {
|
|
9
|
-
name: string;
|
|
10
|
-
run: (userAgent: string) => Promise<T[]>;
|
|
11
|
-
};
|
|
12
|
-
export declare const runStrategies: <T>(strategies: Strategy<T>[]) => Promise<T[]>;
|
|
13
|
-
//# sourceMappingURL=strategies.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"strategies.d.ts","sourceRoot":"","sources":["../../src/strategies.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW,UAKvB,CAAA;AAKD,eAAO,MAAM,aAAa,QAAO,MAKhC,CAAA;AAED,eAAO,MAAM,WAAW,GAAI,WAAW,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAKnE,CAAA;AAEF,qBAAa,YAAa,SAAQ,KAAK;gBACzB,MAAM,EAAE,MAAM;CAI3B;AAED,eAAO,MAAM,WAAW,GAAI,QAAQ,MAAM,EAAE,MAAM,MAAM,EAAE,SAAS,MAAM,EAAE,KAAG,IAI7E,CAAA;AAED,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC,CAAA;CACzC,CAAA;AAGD,eAAO,MAAM,aAAa,GAAU,CAAC,EAAE,YAAY,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAG,OAAO,CAAC,CAAC,EAAE,CAgB7E,CAAA"}
|
package/dist/src/strategies.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { safeAsync } from "./safe.js";
|
|
2
|
-
export const USER_AGENTS = [
|
|
3
|
-
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
4
|
-
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
|
|
5
|
-
'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',
|
|
6
|
-
'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0',
|
|
7
|
-
];
|
|
8
|
-
let uaIndex = 0;
|
|
9
|
-
// Rotation gives each fallback attempt a fresh identity after a block
|
|
10
|
-
export const nextUserAgent = () => {
|
|
11
|
-
const agent = USER_AGENTS[uaIndex % USER_AGENTS.length];
|
|
12
|
-
uaIndex += 1;
|
|
13
|
-
return agent ?? '';
|
|
14
|
-
};
|
|
15
|
-
export const baseHeaders = (userAgent) => ({
|
|
16
|
-
'User-Agent': userAgent,
|
|
17
|
-
'Accept': 'text/html',
|
|
18
|
-
'Accept-Encoding': 'gzip',
|
|
19
|
-
'DNT': '1',
|
|
20
|
-
});
|
|
21
|
-
export class BlockedError extends Error {
|
|
22
|
-
constructor(source) {
|
|
23
|
-
super(source + ' blocked the request (CAPTCHA or rate limit)');
|
|
24
|
-
this.name = 'BlockedError';
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
export const detectBlock = (source, html, markers) => {
|
|
28
|
-
for (const marker of markers) {
|
|
29
|
-
if (html.includes(marker))
|
|
30
|
-
throw new BlockedError(source);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
// A strategy fails by throwing or by returning zero results; the next one gets a chance
|
|
34
|
-
export const runStrategies = async (strategies) => {
|
|
35
|
-
let lastError = new Error('no strategies provided');
|
|
36
|
-
for (const strategy of strategies) {
|
|
37
|
-
const { data, error } = await safeAsync(() => strategy.run(nextUserAgent()));
|
|
38
|
-
if (error) {
|
|
39
|
-
lastError = error;
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
if (data.length > 0)
|
|
43
|
-
return data;
|
|
44
|
-
lastError = new Error(strategy.name + ' returned no results');
|
|
45
|
-
}
|
|
46
|
-
throw lastError;
|
|
47
|
-
};
|
|
48
|
-
//# sourceMappingURL=strategies.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"strategies.js","sourceRoot":"","sources":["../../src/strategies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,uGAAuG;IACvG,iHAAiH;IACjH,uHAAuH;IACvH,wEAAwE;CACzE,CAAA;AAED,IAAI,OAAO,GAAG,CAAC,CAAA;AAEf,sEAAsE;AACtE,MAAM,CAAC,MAAM,aAAa,GAAG,GAAW,EAAE;IACxC,MAAM,KAAK,GAAG,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,MAAM,CAAC,CAAA;IACvD,OAAO,IAAI,CAAC,CAAA;IAEZ,OAAO,KAAK,IAAI,EAAE,CAAA;AACpB,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,SAAiB,EAA0B,EAAE,CAAC,CAAC;IACzE,YAAY,EAAE,SAAS;IACvB,QAAQ,EAAE,WAAW;IACrB,iBAAiB,EAAE,MAAM;IACzB,KAAK,EAAE,GAAG;CACX,CAAC,CAAA;AAEF,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,YAAY,MAAc;QACxB,KAAK,CAAC,MAAM,GAAG,8CAA8C,CAAC,CAAA;QAC9D,IAAI,CAAC,IAAI,GAAG,cAAc,CAAA;IAC5B,CAAC;CACF;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAc,EAAE,IAAY,EAAE,OAAiB,EAAQ,EAAE;IACnF,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;YAAE,MAAM,IAAI,YAAY,CAAC,MAAM,CAAC,CAAA;IAC3D,CAAC;AACH,CAAC,CAAA;AAOD,wFAAwF;AACxF,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAAK,UAAyB,EAAgB,EAAE;IAChF,IAAI,SAAS,GAAU,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAA;IAE1D,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;QAClC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,aAAa,EAAE,CAAC,CAAC,CAAA;QAC5E,IAAI,KAAK,EAAE,CAAC;YACV,SAAS,GAAG,KAAK,CAAA;YACjB,SAAQ;QACV,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;YAAE,OAAO,IAAI,CAAA;QAEhC,SAAS,GAAG,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,GAAG,sBAAsB,CAAC,CAAA;IAC/D,CAAC;IAED,MAAM,SAAS,CAAA;AACjB,CAAC,CAAA"}
|
package/dist/src/user-agent.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"user-agent.d.ts","sourceRoot":"","sources":["../../src/user-agent.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU,QAA+B,CAAA"}
|
package/dist/src/user-agent.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"user-agent.js","sourceRoot":"","sources":["../../src/user-agent.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,iBAAiB,CAAA;AAEjC,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,CAAC,IAAI,GAAG,GAAG,GAAG,GAAG,CAAC,OAAO,CAAA"}
|
package/dist/src/v1-message.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"v1-message.d.ts","sourceRoot":"","sources":["../../src/v1-message.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,UAAU,QAQX,CAAA"}
|
package/dist/src/v1-message.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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');
|
|
11
|
-
//# sourceMappingURL=v1-message.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"v1-message.js","sourceRoot":"","sources":["../../src/v1-message.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,2EAA2E;IAC3E,EAAE;IACF,oEAAoE;IACpE,EAAE;IACF,2CAA2C;IAC3C,EAAE;IACF,mFAAmF;CACpF,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA"}
|
package/search.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { createSearchTools } from './src/search.ts'
|
|
2
|
-
import { safeAsync } from './src/safe.ts'
|
|
3
|
-
import { V1_MESSAGE } from './src/v1-message.ts'
|
|
4
|
-
|
|
5
|
-
// Under OpenCode 1 this package is absent or exports no Plugin.define.
|
|
6
|
-
// A dynamic import makes that catchable, so the failure can be a message instead of a stack trace.
|
|
7
|
-
const { data: plugin, error } = await safeAsync(() => import('@opencode/plugin'))
|
|
8
|
-
|
|
9
|
-
if (error || typeof plugin.Plugin?.define !== 'function') {
|
|
10
|
-
throw new Error(V1_MESSAGE, { cause: error ?? new Error('Plugin.define is not exported') })
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default plugin.Plugin.define({
|
|
14
|
-
id: 'opencode-search',
|
|
15
|
-
setup: async (ctx) => {
|
|
16
|
-
await ctx.tool.transform((tools) => {
|
|
17
|
-
for (const definition of createSearchTools(ctx)) {
|
|
18
|
-
tools.add(definition)
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
},
|
|
22
|
-
})
|
package/src/cache.test.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test'
|
|
2
|
-
import { get, set } from './cache.ts'
|
|
3
|
-
|
|
4
|
-
describe('cache', () => {
|
|
5
|
-
it('stores and retrieves data', async () => {
|
|
6
|
-
const testKey = 'test:cache-key'
|
|
7
|
-
const testData = { foo: 'bar', nested: { value: 123 } }
|
|
8
|
-
|
|
9
|
-
await set(testKey, testData)
|
|
10
|
-
const result = await get(testKey)
|
|
11
|
-
|
|
12
|
-
expect(result).toEqual(testData)
|
|
13
|
-
})
|
|
14
|
-
})
|
package/src/cache.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { readFile, writeFile } from 'node:fs/promises'
|
|
2
|
-
import { dirname, join } from 'node:path'
|
|
3
|
-
import { fileURLToPath } from 'node:url'
|
|
4
|
-
import { safe, safeAsync } from './safe.ts'
|
|
5
|
-
|
|
6
|
-
const CACHE_FILE = join(dirname(fileURLToPath(import.meta.url)), '..', '..', '.cache')
|
|
7
|
-
const CACHE_DURATION = 7 * 24 * 60 * 60 * 1000 // 7 days
|
|
8
|
-
|
|
9
|
-
type CacheEntry = {
|
|
10
|
-
data: unknown
|
|
11
|
-
timestamp: number
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type CacheStore = Record<string, CacheEntry>
|
|
15
|
-
|
|
16
|
-
let store: CacheStore = {}
|
|
17
|
-
let loaded = false
|
|
18
|
-
|
|
19
|
-
const loadCache = async () => {
|
|
20
|
-
if (loaded) return
|
|
21
|
-
|
|
22
|
-
const { data } = await safeAsync(() => readFile(CACHE_FILE, 'utf-8'))
|
|
23
|
-
|
|
24
|
-
if (data) {
|
|
25
|
-
const { data: parsed } = safe(() => JSON.parse(data) as CacheStore)
|
|
26
|
-
store = parsed ?? {}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
loaded = true
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const saveCache = async () => {
|
|
33
|
-
await writeFile(CACHE_FILE, JSON.stringify(store, null, 2))
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const get = async <T = unknown>(key: string): Promise<T | null> => {
|
|
37
|
-
await loadCache()
|
|
38
|
-
|
|
39
|
-
const entry = store[key]
|
|
40
|
-
if (!entry) return null
|
|
41
|
-
|
|
42
|
-
const age = Date.now() - entry.timestamp
|
|
43
|
-
if (age > CACHE_DURATION) {
|
|
44
|
-
delete store[key]
|
|
45
|
-
await saveCache()
|
|
46
|
-
return null
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return entry.data as T
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export const set = async (key: string, data: unknown): Promise<void> => {
|
|
53
|
-
await loadCache()
|
|
54
|
-
|
|
55
|
-
store[key] = {
|
|
56
|
-
data,
|
|
57
|
-
timestamp: Date.now(),
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
await saveCache()
|
|
61
|
-
}
|