opencode-websearch 0.2.3 → 0.2.4

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nils Emil Svensson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,11 +1,10 @@
1
1
  declare const ANTHROPIC_NPM_PACKAGE = "@ai-sdk/anthropic";
2
- declare const DEFAULT_SEARCH_USES = 5;
2
+ declare const DEFAULT_SEARCH_USES = 8;
3
3
  declare const EMPTY_LENGTH = 0;
4
4
  declare const MAX_RESPONSE_TOKENS = 16000;
5
- declare const MAX_SEARCH_USES = 10;
6
5
  declare const MIN_QUERY_LENGTH = 2;
7
- declare const MIN_SEARCH_USES = 1;
8
6
  declare const MONTH_OFFSET = 1;
9
7
  declare const PAD_LENGTH = 2;
10
- export { ANTHROPIC_NPM_PACKAGE, DEFAULT_SEARCH_USES, EMPTY_LENGTH, MAX_RESPONSE_TOKENS, MAX_SEARCH_USES, MIN_QUERY_LENGTH, MIN_SEARCH_USES, MONTH_OFFSET, PAD_LENGTH, };
8
+ declare const SEARCH_SYSTEM_PROMPT = "You are an assistant for performing a web search tool use";
9
+ export { ANTHROPIC_NPM_PACKAGE, DEFAULT_SEARCH_USES, EMPTY_LENGTH, MAX_RESPONSE_TOKENS, MIN_QUERY_LENGTH, MONTH_OFFSET, PAD_LENGTH, SEARCH_SYSTEM_PROMPT, };
11
10
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,qBAAqB,sBAAsB,CAAC;AAClD,QAAA,MAAM,mBAAmB,IAAI,CAAC;AAC9B,QAAA,MAAM,YAAY,IAAI,CAAC;AACvB,QAAA,MAAM,mBAAmB,QAAS,CAAC;AACnC,QAAA,MAAM,eAAe,KAAK,CAAC;AAC3B,QAAA,MAAM,gBAAgB,IAAI,CAAC;AAC3B,QAAA,MAAM,eAAe,IAAI,CAAC;AAC1B,QAAA,MAAM,YAAY,IAAI,CAAC;AACvB,QAAA,MAAM,UAAU,IAAI,CAAC;AAErB,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,YAAY,EACZ,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,UAAU,GACX,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,qBAAqB,sBAAsB,CAAC;AAClD,QAAA,MAAM,mBAAmB,IAAI,CAAC;AAC9B,QAAA,MAAM,YAAY,IAAI,CAAC;AACvB,QAAA,MAAM,mBAAmB,QAAS,CAAC;AACnC,QAAA,MAAM,gBAAgB,IAAI,CAAC;AAC3B,QAAA,MAAM,YAAY,IAAI,CAAC;AACvB,QAAA,MAAM,UAAU,IAAI,CAAC;AACrB,QAAA,MAAM,oBAAoB,8DAA8D,CAAC;AAEzF,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,YAAY,EACZ,mBAAmB,EACnB,gBAAgB,EAChB,YAAY,EACZ,UAAU,EACV,oBAAoB,GACrB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -5,14 +5,12 @@ declare const _default: (input: import("@opencode-ai/plugin").PluginInput) => Pr
5
5
  args: {
6
6
  allowed_domains: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
7
7
  blocked_domains: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
8
- max_uses: import("zod").ZodOptional<import("zod").ZodNumber>;
9
8
  query: import("zod").ZodString;
10
9
  };
11
10
  execute(args: {
12
11
  query: string;
13
12
  allowed_domains?: string[] | undefined;
14
13
  blocked_domains?: string[] | undefined;
15
- max_uses?: number | undefined;
16
14
  }, context: import("@opencode-ai/plugin").ToolContext): Promise<string>;
17
15
  };
18
16
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AA2BA,wBAsEoB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AA2BA,wBAgEoB"}
package/dist/index.js CHANGED
@@ -1,14 +1,13 @@
1
+ // src/index.ts
2
+ import { tool } from "@opencode-ai/plugin";
3
+
1
4
  // src/constants.ts
2
5
  var ANTHROPIC_NPM_PACKAGE = "@ai-sdk/anthropic";
3
- var DEFAULT_SEARCH_USES = 5;
6
+ var DEFAULT_SEARCH_USES = 8;
4
7
  var EMPTY_LENGTH = 0;
5
8
  var MAX_RESPONSE_TOKENS = 16000;
6
- var MAX_SEARCH_USES = 10;
7
9
  var MIN_QUERY_LENGTH = 2;
8
- var MIN_SEARCH_USES = 1;
9
-
10
- // src/index.ts
11
- import { tool } from "@opencode-ai/plugin";
10
+ var SEARCH_SYSTEM_PROMPT = "You are an assistant for performing a web search tool use";
12
11
 
13
12
  // src/config.ts
14
13
  var isAnthropicModel = (model) => model.api.npm === ANTHROPIC_NPM_PACKAGE;
@@ -76,46 +75,34 @@ Steps:
76
75
 
77
76
  // src/providers/anthropic.ts
78
77
  import Anthropic, { APIError } from "@anthropic-ai/sdk";
79
- var formatSearchResult = (result) => {
80
- if (result.page_age) {
81
- return `- [${result.title}](${result.url}) (Updated: ${result.page_age})`;
78
+ var processBlock = (block) => {
79
+ if (block.type === "text" && block.text.trim().length > EMPTY_LENGTH) {
80
+ return block.text.trim();
82
81
  }
83
- return `- [${result.title}](${result.url})`;
84
- };
85
- var processSearchToolResult = (block, results) => {
86
- if (Array.isArray(block.content)) {
87
- const searchResults = block.content;
88
- if (searchResults.length === EMPTY_LENGTH) {
89
- results.push("No results found.");
90
- } else {
91
- results.push(`
92
- Found ${searchResults.length} results:
93
- `);
94
- for (const result of searchResults) {
95
- results.push(formatSearchResult(result));
96
- }
82
+ if (block.type === "web_search_tool_result") {
83
+ if (!Array.isArray(block.content)) {
84
+ return `Web search error: ${block.content.error_code}`;
97
85
  }
98
- } else if (block.content?.type === "web_search_tool_result_error") {
99
- results.push(`Search error: ${block.content.error_code}`);
86
+ return block.content.map((sr) => ({
87
+ title: sr.title,
88
+ url: sr.url
89
+ }));
100
90
  }
91
+ return null;
101
92
  };
102
- var processBlock = (block, results) => {
103
- if (block.type === "server_tool_use" && block.name === "web_search") {
104
- results.push(`Search query: "${block.input.query}"`);
105
- return;
106
- }
107
- if (block.type === "web_search_tool_result") {
108
- processSearchToolResult(block, results);
109
- return;
110
- }
111
- if (block.type === "text" && block.text) {
112
- results.push(`
113
- ${block.text}`);
93
+ var processResponseBlocks = (query, content) => {
94
+ const results = [];
95
+ for (const block of content) {
96
+ const result = processBlock(block);
97
+ if (result !== null) {
98
+ results.push(result);
99
+ }
114
100
  }
101
+ return { query, results };
115
102
  };
116
103
  var buildWebSearchTool = (args) => {
117
104
  const searchTool = {
118
- max_uses: args.max_uses ?? DEFAULT_SEARCH_USES,
105
+ max_uses: DEFAULT_SEARCH_USES,
119
106
  name: "web_search",
120
107
  type: "web_search_20250305"
121
108
  };
@@ -145,13 +132,6 @@ var createAnthropicClient = (config) => {
145
132
  }
146
133
  return new Anthropic(options);
147
134
  };
148
- var appendUsageInfo = (usage, results) => {
149
- if (usage.server_tool_use?.web_search_requests) {
150
- results.push(`
151
- ---
152
- Searches performed: ${usage.server_tool_use.web_search_requests}`);
153
- }
154
- };
155
135
  var executeSearch = async (config, args) => {
156
136
  const client = createAnthropicClient(config);
157
137
  const webSearchTool = buildWebSearchTool(args);
@@ -159,21 +139,17 @@ var executeSearch = async (config, args) => {
159
139
  max_tokens: MAX_RESPONSE_TOKENS,
160
140
  messages: [
161
141
  {
162
- content: `Perform a web search for: ${args.query}`,
142
+ content: `Perform a web search for the query: ${args.query}`,
163
143
  role: "user"
164
144
  }
165
145
  ],
166
146
  model: config.model,
147
+ system: SEARCH_SYSTEM_PROMPT,
167
148
  tools: [webSearchTool]
168
149
  });
169
- const results = [];
170
150
  const content = response.content;
171
- for (const block of content) {
172
- processBlock(block, results);
173
- }
174
- appendUsageInfo(response.usage, results);
175
- return results.join(`
176
- `) || "No results returned from web search.";
151
+ const structured = processResponseBlocks(args.query, content);
152
+ return JSON.stringify(structured);
177
153
  };
178
154
 
179
155
  // src/helpers.ts
@@ -193,10 +169,9 @@ var src_default = async (input) => {
193
169
  tool: {
194
170
  "web-search": tool({
195
171
  args: {
196
- allowed_domains: tool.schema.array(tool.schema.string()).optional().describe("Only include results from these domains"),
197
- blocked_domains: tool.schema.array(tool.schema.string()).optional().describe("Exclude results from these domains"),
198
- max_uses: tool.schema.number().min(MIN_SEARCH_USES).max(MAX_SEARCH_USES).optional().describe("Maximum number of searches to perform (default: 5)"),
199
- query: tool.schema.string().min(MIN_QUERY_LENGTH).describe("The search query to execute")
172
+ allowed_domains: tool.schema.array(tool.schema.string()).optional().describe("Only include search results from these domains"),
173
+ blocked_domains: tool.schema.array(tool.schema.string()).optional().describe("Never include search results from these domains"),
174
+ query: tool.schema.string().min(MIN_QUERY_LENGTH).describe("The search query to use")
200
175
  },
201
176
  description: `- Allows OpenCode to search the web and use the results to inform responses
202
177
  - Provides up-to-date information for current events and recent data
@@ -3,7 +3,6 @@ declare const formatErrorMessage: (error: unknown) => string;
3
3
  declare const executeSearch: (config: AnthropicConfig, args: {
4
4
  allowed_domains?: string[];
5
5
  blocked_domains?: string[];
6
- max_uses?: number;
7
6
  query: string;
8
7
  }) => Promise<string>;
9
8
  export { executeSearch, formatErrorMessage };
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EAKhB,MAAM,aAAa,CAAC;AAqErB,QAAA,MAAM,kBAAkB,GAAI,OAAO,OAAO,KAAG,MAQ5C,CAAC;AAoBF,QAAA,MAAM,aAAa,GACjB,QAAQ,eAAe,EACvB,MAAM;IACJ,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;CACf,KACA,OAAO,CAAC,MAAM,CA0BhB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,CAAC"}
1
+ {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAiC,MAAM,aAAa,CAAC;AAgF7E,QAAA,MAAM,kBAAkB,GAAI,OAAO,OAAO,KAAG,MAQ5C,CAAC;AAcF,QAAA,MAAM,aAAa,GACjB,QAAQ,eAAe,EACvB,MAAM;IACJ,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,KAAK,EAAE,MAAM,CAAC;CACf,KACA,OAAO,CAAC,MAAM,CAqBhB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,CAAC"}
package/dist/types.d.ts CHANGED
@@ -4,7 +4,6 @@ interface AnthropicConfig {
4
4
  model: string;
5
5
  }
6
6
  interface WebSearchResult {
7
- page_age?: string;
8
7
  title: string;
9
8
  type: "web_search_result";
10
9
  url: string;
@@ -25,16 +24,9 @@ interface ServerToolUse {
25
24
  name: string;
26
25
  type: "server_tool_use";
27
26
  }
28
- interface SearchUsage {
29
- input_tokens: number;
30
- output_tokens: number;
31
- server_tool_use?: {
32
- web_search_requests?: number;
33
- };
34
- }
35
27
  type ContentBlock = {
36
28
  text: string;
37
29
  type: "text";
38
30
  } | ServerToolUse | WebSearchToolResult;
39
- export { AnthropicConfig, ContentBlock, SearchUsage, ServerToolUse, WebSearchResult, WebSearchToolResult, };
31
+ export { AnthropicConfig, ContentBlock, ServerToolUse, WebSearchResult, WebSearchToolResult };
40
32
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,UAAU,eAAe;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf;AAID,UAAU,eAAe;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,mBAAmB,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;CACb;AAED,UAAU,mBAAmB;IAC3B,OAAO,EAAE,eAAe,EAAE,GAAG;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,8BAA8B,CAAA;KAAE,CAAC;IAC1F,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,wB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CACzB;AAED,KAAK,YAAY,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,GAAG,mBAAmB,CAAC;AAEzF,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,mBAAmB,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-websearch",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Web search plugin for OpenCode, inspired by Claude Code's WebSearch tool",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -37,9 +37,6 @@
37
37
  "type": "git",
38
38
  "url": "https://github.com/emilsvennesson/opencode-websearch.git"
39
39
  },
40
- "engines": {
41
- "node": ">=24"
42
- },
43
40
  "license": "MIT",
44
41
  "dependencies": {
45
42
  "@anthropic-ai/sdk": "^0.52.0"