opencode-websearch 0.2.3 → 0.3.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/LICENSE.md +21 -0
- package/README.md +42 -20
- package/dist/config.d.ts +13 -6
- package/dist/config.d.ts.map +1 -1
- package/dist/constants.d.ts +5 -4
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +10 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +158 -93
- package/dist/providers/anthropic.d.ts +2 -3
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/types.d.ts +33 -10
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -4
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.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# opencode-websearch
|
|
2
2
|
|
|
3
|
-
Web search plugin for [OpenCode](https://opencode.ai), powered by Anthropic's server-side `web_search`
|
|
3
|
+
Web search plugin for [OpenCode](https://opencode.ai), powered by Anthropic's server-side [`web_search` tool](https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool). Gives any OpenCode model access to real-time web results with source citations -- the same web search capability available in Claude Code, brought to OpenCode.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -16,9 +16,24 @@ OpenCode will install it automatically at startup.
|
|
|
16
16
|
|
|
17
17
|
## Configuration
|
|
18
18
|
|
|
19
|
-
The plugin
|
|
19
|
+
The plugin scans your OpenCode providers for any that use `@ai-sdk/anthropic` and picks up credentials however you've configured them -- via `/connect`, environment variables, or `options.apiKey` in your config.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Tag a model with `"websearch": "auto"` or `"websearch": "always"` to control how web search selects its model.
|
|
22
|
+
|
|
23
|
+
### Model selection
|
|
24
|
+
|
|
25
|
+
The plugin dynamically chooses which Anthropic model to use for each search, following this priority chain:
|
|
26
|
+
|
|
27
|
+
| Priority | Condition | Behavior |
|
|
28
|
+
| -------- | ----------------------------------- | --------------------------------------------------------------------------- |
|
|
29
|
+
| 1 | A model is tagged `"always"` | That model is **always** used, regardless of what you're chatting with |
|
|
30
|
+
| 2 | Your active chat model is Anthropic | The active model is used directly -- no extra configuration needed |
|
|
31
|
+
| 3 | A model is tagged `"auto"` | That model is used as a **fallback** when the active model is non-Anthropic |
|
|
32
|
+
| 4 | None of the above | An error is returned |
|
|
33
|
+
|
|
34
|
+
### `"auto"` mode (recommended)
|
|
35
|
+
|
|
36
|
+
Use `"auto"` when you want web search to work seamlessly whether you're chatting with an Anthropic model or not. When your active model is Anthropic, it's used directly; otherwise the tagged model kicks in as a fallback.
|
|
22
37
|
|
|
23
38
|
```json
|
|
24
39
|
{
|
|
@@ -27,7 +42,7 @@ Add `"websearch": true` to the model you want the plugin to use for searches:
|
|
|
27
42
|
"models": {
|
|
28
43
|
"claude-sonnet-4-5": {
|
|
29
44
|
"options": {
|
|
30
|
-
"websearch":
|
|
45
|
+
"websearch": "auto"
|
|
31
46
|
}
|
|
32
47
|
}
|
|
33
48
|
}
|
|
@@ -36,6 +51,28 @@ Add `"websearch": true` to the model you want the plugin to use for searches:
|
|
|
36
51
|
}
|
|
37
52
|
```
|
|
38
53
|
|
|
54
|
+
### `"always"` mode
|
|
55
|
+
|
|
56
|
+
Use `"always"` to hard-lock web search to a specific model. This is useful when you want a cheaper or faster model to always handle searches, no matter what you're chatting with.
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"provider": {
|
|
61
|
+
"anthropic": {
|
|
62
|
+
"models": {
|
|
63
|
+
"claude-haiku-3-5": {
|
|
64
|
+
"options": {
|
|
65
|
+
"websearch": "always"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Custom providers
|
|
75
|
+
|
|
39
76
|
This also works with custom providers that use `@ai-sdk/anthropic`, such as a LiteLLM proxy:
|
|
40
77
|
|
|
41
78
|
```json
|
|
@@ -50,7 +87,7 @@ This also works with custom providers that use `@ai-sdk/anthropic`, such as a Li
|
|
|
50
87
|
"models": {
|
|
51
88
|
"claude-sonnet-4-5": {
|
|
52
89
|
"options": {
|
|
53
|
-
"websearch":
|
|
90
|
+
"websearch": "auto"
|
|
54
91
|
}
|
|
55
92
|
}
|
|
56
93
|
}
|
|
@@ -59,21 +96,6 @@ This also works with custom providers that use `@ai-sdk/anthropic`, such as a Li
|
|
|
59
96
|
}
|
|
60
97
|
```
|
|
61
98
|
|
|
62
|
-
## Usage
|
|
63
|
-
|
|
64
|
-
Once configured, the `web-search` tool is available to the AI agent. It accepts:
|
|
65
|
-
|
|
66
|
-
| Argument | Type | Required | Description |
|
|
67
|
-
|---|---|---|---|
|
|
68
|
-
| `query` | `string` | Yes | The search query |
|
|
69
|
-
| `allowed_domains` | `string[]` | No | Restrict results to these domains |
|
|
70
|
-
| `blocked_domains` | `string[]` | No | Exclude results from these domains |
|
|
71
|
-
| `max_uses` | `number` (1-10) | No | Max searches per invocation (default: 5) |
|
|
72
|
-
|
|
73
|
-
You cannot specify both `allowed_domains` and `blocked_domains` at the same time.
|
|
74
|
-
|
|
75
|
-
Results are returned as formatted markdown with source links.
|
|
76
|
-
|
|
77
99
|
## Development
|
|
78
100
|
|
|
79
101
|
### Local Development
|
package/dist/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProviderResolution } from "./types.js";
|
|
2
2
|
interface ProviderData {
|
|
3
3
|
id: string;
|
|
4
4
|
key?: string;
|
|
@@ -12,10 +12,17 @@ interface ProviderData {
|
|
|
12
12
|
options: Record<string, unknown>;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* Scan providers
|
|
16
|
-
*
|
|
15
|
+
* Scan providers for Anthropic credentials and any websearch-tagged models.
|
|
16
|
+
*
|
|
17
|
+
* Resolution priority:
|
|
18
|
+
* - `lockedModel`: first model with `"websearch": "always"` (hard lock)
|
|
19
|
+
* - `fallbackModel`: first model with `"websearch": "auto"` (soft fallback)
|
|
20
|
+
* - `credentials`: API key + optional baseURL from the first Anthropic provider
|
|
21
|
+
*
|
|
22
|
+
* Returns `null` if no Anthropic provider with a valid API key is found.
|
|
17
23
|
*/
|
|
18
|
-
declare const resolveFromProviders: (providers: ProviderData[]) =>
|
|
19
|
-
declare const
|
|
20
|
-
|
|
24
|
+
declare const resolveFromProviders: (providers: ProviderData[]) => ProviderResolution | null;
|
|
25
|
+
declare const formatNoProviderError: () => string;
|
|
26
|
+
declare const formatNonAnthropicError: (activeModelID: string) => string;
|
|
27
|
+
export { formatNoProviderError, formatNonAnthropicError, ProviderData, resolveFromProviders };
|
|
21
28
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAItE,UAAU,YAAY;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,GAAG,EAAE;YAAE,GAAG,EAAE,MAAM,CAAA;SAAE,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC,CAAC;IAC/F,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AA0ED;;;;;;;;;GASG;AACH,QAAA,MAAM,oBAAoB,GAAI,WAAW,YAAY,EAAE,KAAG,kBAAkB,GAAG,IAgB9E,CAAC;AAIF,QAAA,MAAM,qBAAqB,QAAO,MAoBsB,CAAC;AAEzD,QAAA,MAAM,uBAAuB,GAAI,eAAe,MAAM,KAAG,MAuBiD,CAAC;AAE3G,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,YAAY,EAAE,oBAAoB,EAAE,CAAC"}
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
declare const ANTHROPIC_NPM_PACKAGE = "@ai-sdk/anthropic";
|
|
2
|
-
declare const DEFAULT_SEARCH_USES =
|
|
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
|
-
|
|
8
|
+
declare const SEARCH_SYSTEM_PROMPT = "You are an assistant for performing a web search tool use";
|
|
9
|
+
declare const WEBSEARCH_ALWAYS = "always";
|
|
10
|
+
declare const WEBSEARCH_AUTO = "auto";
|
|
11
|
+
export { ANTHROPIC_NPM_PACKAGE, DEFAULT_SEARCH_USES, EMPTY_LENGTH, MAX_RESPONSE_TOKENS, MIN_QUERY_LENGTH, MONTH_OFFSET, PAD_LENGTH, SEARCH_SYSTEM_PROMPT, WEBSEARCH_ALWAYS, WEBSEARCH_AUTO, };
|
|
11
12
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -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,
|
|
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;AACzF,QAAA,MAAM,gBAAgB,WAAW,CAAC;AAClC,QAAA,MAAM,cAAc,SAAS,CAAC;AAE9B,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,YAAY,EACZ,mBAAmB,EACnB,gBAAgB,EAChB,YAAY,EACZ,UAAU,EACV,oBAAoB,EACpB,gBAAgB,EAChB,cAAc,GACf,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
declare const _default: (input: import("@opencode-ai/plugin").PluginInput) => Promise<{
|
|
2
|
+
"chat.message": (hookInput: {
|
|
3
|
+
sessionID: string;
|
|
4
|
+
agent?: string;
|
|
5
|
+
model?: {
|
|
6
|
+
providerID: string;
|
|
7
|
+
modelID: string;
|
|
8
|
+
};
|
|
9
|
+
messageID?: string;
|
|
10
|
+
variant?: string;
|
|
11
|
+
}) => Promise<void>;
|
|
2
12
|
tool: {
|
|
3
13
|
"web-search": {
|
|
4
14
|
description: string;
|
|
5
15
|
args: {
|
|
6
16
|
allowed_domains: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
|
|
7
17
|
blocked_domains: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
|
|
8
|
-
max_uses: import("zod").ZodOptional<import("zod").ZodNumber>;
|
|
9
18
|
query: import("zod").ZodString;
|
|
10
19
|
};
|
|
11
20
|
execute(args: {
|
|
12
21
|
query: string;
|
|
13
22
|
allowed_domains?: string[] | undefined;
|
|
14
23
|
blocked_domains?: string[] | undefined;
|
|
15
|
-
max_uses?: number | undefined;
|
|
16
24
|
}, context: import("@opencode-ai/plugin").ToolContext): Promise<string>;
|
|
17
25
|
};
|
|
18
26
|
};
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAwFA,wBAoFoB"}
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
// src/constants.ts
|
|
2
2
|
var ANTHROPIC_NPM_PACKAGE = "@ai-sdk/anthropic";
|
|
3
|
-
var DEFAULT_SEARCH_USES =
|
|
3
|
+
var DEFAULT_SEARCH_USES = 8;
|
|
4
4
|
var EMPTY_LENGTH = 0;
|
|
5
5
|
var MAX_RESPONSE_TOKENS = 16000;
|
|
6
|
-
var MAX_SEARCH_USES = 10;
|
|
7
6
|
var MIN_QUERY_LENGTH = 2;
|
|
8
|
-
var
|
|
7
|
+
var SEARCH_SYSTEM_PROMPT = "You are an assistant for performing a web search tool use";
|
|
8
|
+
var WEBSEARCH_ALWAYS = "always";
|
|
9
|
+
var WEBSEARCH_AUTO = "auto";
|
|
9
10
|
|
|
10
11
|
// src/index.ts
|
|
11
12
|
import { tool } from "@opencode-ai/plugin";
|
|
12
13
|
|
|
13
14
|
// src/config.ts
|
|
14
|
-
var
|
|
15
|
-
var
|
|
15
|
+
var isAnthropicProvider = (model) => model.api.npm === ANTHROPIC_NPM_PACKAGE;
|
|
16
|
+
var getWebsearchOption = (model) => {
|
|
17
|
+
const value = model.options.websearch;
|
|
18
|
+
if (value === WEBSEARCH_ALWAYS || value === WEBSEARCH_AUTO) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
16
23
|
var normalizeBaseURL = (url) => url.replace(/\/v1\/?$/, "");
|
|
17
24
|
var extractApiKey = (options) => {
|
|
18
25
|
if (typeof options.apiKey !== "string") {
|
|
@@ -26,41 +33,82 @@ var extractBaseURL = (options) => {
|
|
|
26
33
|
}
|
|
27
34
|
return normalizeBaseURL(options.baseURL);
|
|
28
35
|
};
|
|
36
|
+
var extractCredentials = (provider) => {
|
|
37
|
+
const apiKey = provider.key ?? extractApiKey(provider.options);
|
|
38
|
+
if (!apiKey) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return { apiKey, baseURL: extractBaseURL(provider.options) };
|
|
42
|
+
};
|
|
43
|
+
var processProviderModel = (provider, model, accumulated) => {
|
|
44
|
+
if (!isAnthropicProvider(model)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!accumulated.credentials) {
|
|
48
|
+
accumulated.credentials = extractCredentials(provider);
|
|
49
|
+
}
|
|
50
|
+
const option = getWebsearchOption(model);
|
|
51
|
+
if (option === WEBSEARCH_ALWAYS && !accumulated.lockedModel) {
|
|
52
|
+
accumulated.lockedModel = model.id;
|
|
53
|
+
}
|
|
54
|
+
if (option === WEBSEARCH_AUTO && !accumulated.fallbackModel) {
|
|
55
|
+
accumulated.fallbackModel = model.id;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var scanProviderModels = (provider, accumulated) => {
|
|
59
|
+
for (const model of Object.values(provider.models)) {
|
|
60
|
+
processProviderModel(provider, model, accumulated);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
29
63
|
var resolveFromProviders = (providers) => {
|
|
64
|
+
const result = { credentials: null };
|
|
30
65
|
for (const provider of providers) {
|
|
31
|
-
|
|
32
|
-
if (isAnthropicModel(model) && hasWebSearch(model)) {
|
|
33
|
-
const apiKey = provider.key ?? extractApiKey(provider.options);
|
|
34
|
-
if (!apiKey) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
apiKey,
|
|
39
|
-
baseURL: extractBaseURL(provider.options),
|
|
40
|
-
model: model.id
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
}
|
|
66
|
+
scanProviderModels(provider, result);
|
|
44
67
|
}
|
|
45
|
-
|
|
68
|
+
if (!result.credentials) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
credentials: result.credentials,
|
|
73
|
+
fallbackModel: result.fallbackModel,
|
|
74
|
+
lockedModel: result.lockedModel
|
|
75
|
+
};
|
|
46
76
|
};
|
|
47
|
-
var
|
|
77
|
+
var formatNoProviderError = () => `Error: web-search requires an Anthropic provider.
|
|
48
78
|
|
|
49
|
-
No
|
|
79
|
+
No Anthropic provider with a valid API key was found in your opencode.json configuration.
|
|
50
80
|
|
|
51
|
-
To fix this, add an Anthropic provider to your opencode.json
|
|
81
|
+
To fix this, add an Anthropic provider to your opencode.json:
|
|
52
82
|
|
|
53
83
|
{
|
|
54
84
|
"provider": {
|
|
55
85
|
"anthropic": {
|
|
56
|
-
"npm": "@ai-sdk/anthropic",
|
|
57
86
|
"options": {
|
|
58
87
|
"apiKey": "{env:ANTHROPIC_API_KEY}"
|
|
59
|
-
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
Steps:
|
|
94
|
+
1. Open your opencode.json (project root, .opencode/, or ~/.config/opencode/)
|
|
95
|
+
2. Ensure you have an Anthropic provider configured with a valid API key
|
|
96
|
+
3. Restart OpenCode to pick up the configuration change`;
|
|
97
|
+
var formatNonAnthropicError = (activeModelID) => `Error: your current model (${activeModelID}) does not support web search.
|
|
98
|
+
|
|
99
|
+
Web search uses Anthropic's server-side web_search tool, which only works with Anthropic models.
|
|
100
|
+
|
|
101
|
+
You can either:
|
|
102
|
+
1. Switch to an Anthropic model (e.g. claude-sonnet-4-5)
|
|
103
|
+
2. Set \`"websearch": "auto"\` on an Anthropic model to use it as a fallback:
|
|
104
|
+
|
|
105
|
+
{
|
|
106
|
+
"provider": {
|
|
107
|
+
"anthropic": {
|
|
60
108
|
"models": {
|
|
61
109
|
"claude-sonnet-4-5": {
|
|
62
110
|
"options": {
|
|
63
|
-
"websearch":
|
|
111
|
+
"websearch": "auto"
|
|
64
112
|
}
|
|
65
113
|
}
|
|
66
114
|
}
|
|
@@ -68,54 +116,38 @@ To fix this, add an Anthropic provider to your opencode.json and set \`"websearc
|
|
|
68
116
|
}
|
|
69
117
|
}
|
|
70
118
|
|
|
71
|
-
|
|
72
|
-
1. Open your opencode.json (project root, .opencode/opencode.json, or ~/.config/opencode/opencode.json)
|
|
73
|
-
2. Ensure you have an Anthropic provider configured with a valid API key
|
|
74
|
-
3. Add \`"websearch": true\` to the \`options\` of the Claude model you want to use for web search
|
|
75
|
-
4. Restart OpenCode to pick up the configuration change`;
|
|
119
|
+
Or set \`"websearch": "always"\` to always use that model for web search regardless of your active model.`;
|
|
76
120
|
|
|
77
121
|
// src/providers/anthropic.ts
|
|
78
122
|
import Anthropic, { APIError } from "@anthropic-ai/sdk";
|
|
79
|
-
var
|
|
80
|
-
if (
|
|
81
|
-
return
|
|
82
|
-
}
|
|
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
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} else if (block.content?.type === "web_search_tool_result_error") {
|
|
99
|
-
results.push(`Search error: ${block.content.error_code}`);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
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;
|
|
123
|
+
var processBlock = (block) => {
|
|
124
|
+
if (block.type === "text" && block.text.trim().length > EMPTY_LENGTH) {
|
|
125
|
+
return block.text.trim();
|
|
106
126
|
}
|
|
107
127
|
if (block.type === "web_search_tool_result") {
|
|
108
|
-
|
|
109
|
-
|
|
128
|
+
if (!Array.isArray(block.content)) {
|
|
129
|
+
return `Web search error: ${block.content.error_code}`;
|
|
130
|
+
}
|
|
131
|
+
return block.content.map((sr) => ({
|
|
132
|
+
title: sr.title,
|
|
133
|
+
url: sr.url
|
|
134
|
+
}));
|
|
110
135
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
136
|
+
return null;
|
|
137
|
+
};
|
|
138
|
+
var processResponseBlocks = (query, content) => {
|
|
139
|
+
const results = [];
|
|
140
|
+
for (const block of content) {
|
|
141
|
+
const result = processBlock(block);
|
|
142
|
+
if (result !== null) {
|
|
143
|
+
results.push(result);
|
|
144
|
+
}
|
|
114
145
|
}
|
|
146
|
+
return { query, results };
|
|
115
147
|
};
|
|
116
148
|
var buildWebSearchTool = (args) => {
|
|
117
149
|
const searchTool = {
|
|
118
|
-
max_uses:
|
|
150
|
+
max_uses: DEFAULT_SEARCH_USES,
|
|
119
151
|
name: "web_search",
|
|
120
152
|
type: "web_search_20250305"
|
|
121
153
|
};
|
|
@@ -145,13 +177,6 @@ var createAnthropicClient = (config) => {
|
|
|
145
177
|
}
|
|
146
178
|
return new Anthropic(options);
|
|
147
179
|
};
|
|
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
180
|
var executeSearch = async (config, args) => {
|
|
156
181
|
const client = createAnthropicClient(config);
|
|
157
182
|
const webSearchTool = buildWebSearchTool(args);
|
|
@@ -159,44 +184,79 @@ var executeSearch = async (config, args) => {
|
|
|
159
184
|
max_tokens: MAX_RESPONSE_TOKENS,
|
|
160
185
|
messages: [
|
|
161
186
|
{
|
|
162
|
-
content: `Perform a web search for: ${args.query}`,
|
|
187
|
+
content: `Perform a web search for the query: ${args.query}`,
|
|
163
188
|
role: "user"
|
|
164
189
|
}
|
|
165
190
|
],
|
|
166
191
|
model: config.model,
|
|
192
|
+
system: SEARCH_SYSTEM_PROMPT,
|
|
167
193
|
tools: [webSearchTool]
|
|
168
194
|
});
|
|
169
|
-
const results = [];
|
|
170
195
|
const content = response.content;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
appendUsageInfo(response.usage, results);
|
|
175
|
-
return results.join(`
|
|
176
|
-
`) || "No results returned from web search.";
|
|
196
|
+
const structured = processResponseBlocks(args.query, content);
|
|
197
|
+
return JSON.stringify(structured);
|
|
177
198
|
};
|
|
178
199
|
|
|
179
200
|
// src/helpers.ts
|
|
180
201
|
var getCurrentMonthYear = () => new Date().toLocaleDateString("en-US", { month: "long", year: "numeric" });
|
|
181
202
|
|
|
182
203
|
// src/index.ts
|
|
183
|
-
var
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
204
|
+
var resolveSearchModel = (resolution, active) => {
|
|
205
|
+
if (resolution.lockedModel) {
|
|
206
|
+
return resolution.lockedModel;
|
|
207
|
+
}
|
|
208
|
+
if (active) {
|
|
209
|
+
return active.modelID;
|
|
210
|
+
}
|
|
211
|
+
if (resolution.fallbackModel) {
|
|
212
|
+
return resolution.fallbackModel;
|
|
187
213
|
}
|
|
188
214
|
return null;
|
|
189
215
|
};
|
|
216
|
+
var isAnthropicActive = (active, providers) => {
|
|
217
|
+
if (!active) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
for (const provider of providers) {
|
|
221
|
+
for (const model of Object.values(provider.models)) {
|
|
222
|
+
if (model.id === active.modelID && model.api.npm === ANTHROPIC_NPM_PACKAGE) {
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
};
|
|
229
|
+
var resolveProviderState = async (client) => {
|
|
230
|
+
const { data } = await client.config.providers();
|
|
231
|
+
if (!data) {
|
|
232
|
+
return { list: [], resolution: null };
|
|
233
|
+
}
|
|
234
|
+
const list = data.providers;
|
|
235
|
+
return { list, resolution: resolveFromProviders(list) };
|
|
236
|
+
};
|
|
237
|
+
var buildSearchConfig = (resolution, modelID) => ({
|
|
238
|
+
apiKey: resolution.credentials.apiKey,
|
|
239
|
+
baseURL: resolution.credentials.baseURL,
|
|
240
|
+
model: modelID
|
|
241
|
+
});
|
|
190
242
|
var src_default = async (input) => {
|
|
191
|
-
let
|
|
243
|
+
let state = null;
|
|
244
|
+
const activeModels = new Map;
|
|
192
245
|
return {
|
|
246
|
+
"chat.message": async (hookInput) => {
|
|
247
|
+
if (hookInput.model) {
|
|
248
|
+
activeModels.set(hookInput.sessionID, {
|
|
249
|
+
modelID: hookInput.model.modelID,
|
|
250
|
+
providerID: hookInput.model.providerID
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
},
|
|
193
254
|
tool: {
|
|
194
255
|
"web-search": tool({
|
|
195
256
|
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("
|
|
198
|
-
|
|
199
|
-
query: tool.schema.string().min(MIN_QUERY_LENGTH).describe("The search query to execute")
|
|
257
|
+
allowed_domains: tool.schema.array(tool.schema.string()).optional().describe("Only include search results from these domains"),
|
|
258
|
+
blocked_domains: tool.schema.array(tool.schema.string()).optional().describe("Never include search results from these domains"),
|
|
259
|
+
query: tool.schema.string().min(MIN_QUERY_LENGTH).describe("The search query to use")
|
|
200
260
|
},
|
|
201
261
|
description: `- Allows OpenCode to search the web and use the results to inform responses
|
|
202
262
|
- Provides up-to-date information for current events and recent data
|
|
@@ -222,18 +282,23 @@ Usage notes:
|
|
|
222
282
|
IMPORTANT - Use the correct year in search queries:
|
|
223
283
|
- It is currently ${getCurrentMonthYear()}. You MUST use this when searching for recent information, documentation, or current events.
|
|
224
284
|
- Example: If the user asks for "latest React docs", search for "React documentation" with the current year, NOT last year`,
|
|
225
|
-
async execute(args) {
|
|
226
|
-
if (!
|
|
227
|
-
|
|
285
|
+
async execute(args, context) {
|
|
286
|
+
if (!state) {
|
|
287
|
+
state = await resolveProviderState(input.client);
|
|
228
288
|
}
|
|
229
|
-
if (!
|
|
230
|
-
return
|
|
289
|
+
if (!state.resolution) {
|
|
290
|
+
return formatNoProviderError();
|
|
231
291
|
}
|
|
232
292
|
if (args.allowed_domains && args.blocked_domains) {
|
|
233
293
|
return "Error: Cannot specify both allowed_domains and blocked_domains.";
|
|
234
294
|
}
|
|
295
|
+
const active = activeModels.get(context.sessionID);
|
|
296
|
+
const modelID = resolveSearchModel(state.resolution, isAnthropicActive(active, state.list) ? active : undefined);
|
|
297
|
+
if (!modelID) {
|
|
298
|
+
return formatNonAnthropicError(active?.modelID ?? "unknown");
|
|
299
|
+
}
|
|
235
300
|
try {
|
|
236
|
-
return await executeSearch(
|
|
301
|
+
return await executeSearch(buildSearchConfig(state.resolution, modelID), args);
|
|
237
302
|
} catch (error) {
|
|
238
303
|
return formatErrorMessage(error);
|
|
239
304
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SearchConfig } from "../types.js";
|
|
2
2
|
declare const formatErrorMessage: (error: unknown) => string;
|
|
3
|
-
declare const executeSearch: (config:
|
|
3
|
+
declare const executeSearch: (config: SearchConfig, 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,
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,YAAY,EAAmB,MAAM,aAAa,CAAC;AAgF1E,QAAA,MAAM,kBAAkB,GAAI,OAAO,OAAO,KAAG,MAQ5C,CAAC;AAcF,QAAA,MAAM,aAAa,GACjB,QAAQ,YAAY,EACpB,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
|
@@ -1,10 +1,40 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Credentials needed to call the Anthropic API.
|
|
3
|
+
* Resolved from any Anthropic provider in the OpenCode config.
|
|
4
|
+
*/
|
|
5
|
+
interface AnthropicCredentials {
|
|
6
|
+
apiKey: string;
|
|
7
|
+
baseURL?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Fully resolved config for a single web search call:
|
|
11
|
+
* credentials + the specific model to use.
|
|
12
|
+
*/
|
|
13
|
+
interface SearchConfig {
|
|
2
14
|
apiKey: string;
|
|
3
15
|
baseURL?: string;
|
|
4
16
|
model: string;
|
|
5
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The result of scanning all providers at startup:
|
|
20
|
+
* - `credentials`: API key + optional base URL from the first Anthropic provider
|
|
21
|
+
* - `lockedModel`: model ID if a model has `websearch: "always"` (hard lock)
|
|
22
|
+
* - `fallbackModel`: model ID if a model has `"websearch": "auto"` (soft fallback)
|
|
23
|
+
*/
|
|
24
|
+
interface ProviderResolution {
|
|
25
|
+
credentials: AnthropicCredentials;
|
|
26
|
+
fallbackModel?: string;
|
|
27
|
+
lockedModel?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Tracks the model the user is currently chatting with,
|
|
31
|
+
* as reported by the `chat.message` hook.
|
|
32
|
+
*/
|
|
33
|
+
interface ActiveModel {
|
|
34
|
+
modelID: string;
|
|
35
|
+
providerID: string;
|
|
36
|
+
}
|
|
6
37
|
interface WebSearchResult {
|
|
7
|
-
page_age?: string;
|
|
8
38
|
title: string;
|
|
9
39
|
type: "web_search_result";
|
|
10
40
|
url: string;
|
|
@@ -25,16 +55,9 @@ interface ServerToolUse {
|
|
|
25
55
|
name: string;
|
|
26
56
|
type: "server_tool_use";
|
|
27
57
|
}
|
|
28
|
-
interface SearchUsage {
|
|
29
|
-
input_tokens: number;
|
|
30
|
-
output_tokens: number;
|
|
31
|
-
server_tool_use?: {
|
|
32
|
-
web_search_requests?: number;
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
58
|
type ContentBlock = {
|
|
36
59
|
text: string;
|
|
37
60
|
type: "text";
|
|
38
61
|
} | ServerToolUse | WebSearchToolResult;
|
|
39
|
-
export {
|
|
62
|
+
export { ActiveModel, AnthropicCredentials, ContentBlock, ProviderResolution, SearchConfig, ServerToolUse, WebSearchResult, WebSearchToolResult, };
|
|
40
63
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,UAAU,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,UAAU,oBAAoB;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,UAAU,YAAY;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;GAKG;AACH,UAAU,kBAAkB;IAC1B,WAAW,EAAE,oBAAoB,CAAC;IAClC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAID;;;GAGG;AACH,UAAU,WAAW;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAID,UAAU,eAAe;IACvB,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,wBAAwB,CAAC;CAChC;AAED,UAAU,aAAa;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,iBAAiB,CAAC;CACzB;AAED,KAAK,YAAY,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,GAAG,mBAAmB,CAAC;AAEzF,OAAO,EACL,WAAW,EACX,oBAAoB,EACpB,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,mBAAmB,GACpB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-websearch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
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"
|