opencode-websearch-deepseek 0.2.0 → 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/README.md CHANGED
@@ -77,6 +77,34 @@ in:
77
77
  > so no environment variable is required when that provider is already
78
78
  > authenticated.
79
79
 
80
+ ### Plugin options
81
+
82
+ As an alternative to environment variables, configure the plugin with the
83
+ `plugins` object form (read from `ctx.options`):
84
+
85
+ ```jsonc
86
+ {
87
+ "$schema": "https://opencode.ai/config.json",
88
+ "plugins": [
89
+ {
90
+ "package": "opencode-websearch-deepseek",
91
+ "options": {
92
+ "apiKey": "{file:~/.config/opencode/deepseek.key}",
93
+ "model": "deepseek-v4-flash",
94
+ "maxUses": 5,
95
+ "thinking": "enabled"
96
+ }
97
+ }
98
+ ]
99
+ }
100
+ ```
101
+
102
+ Precedence for the API key: `options.apiKey` → `DEEPSEEK_API_KEY` /
103
+ `WEBSEARCH_API_KEY` → stored `deepseek` provider credential. `model`, `maxUses`,
104
+ and `thinking` fall back to their environment variables and defaults.
105
+ OpenCode interpolates `{file:...}` (and `{env:...}`) in config values, so the
106
+ key can live in a file instead of the OS environment.
107
+
80
108
  > The plugin always selects the DeepSeek provider. Without an API key, a
81
109
  > `websearch` call fails with a clear error rather than silently falling back
82
110
  > to a different provider.
package/dist/index.d.ts CHANGED
@@ -95,25 +95,48 @@ export interface IntegrationContext {
95
95
  resolve(connection: unknown): Promise<StoredCredential | undefined>;
96
96
  };
97
97
  }
98
+ /**
99
+ * Options accepted through the `plugins` object form in `opencode.json(c)` and
100
+ * forwarded to the plugin via `ctx.options`:
101
+ *
102
+ * ```jsonc
103
+ * { "plugins": [{ "package": "opencode-websearch-deepseek", "options": {
104
+ * "apiKey": "...", "model": "deepseek-v4-flash", "maxUses": 5, "thinking": "enabled"
105
+ * } }] }
106
+ * ```
107
+ */
108
+ export interface WebsearchOptions {
109
+ /** DeepSeek API key; takes precedence over env and the stored credential. */
110
+ apiKey?: string;
111
+ /** Model used for search and synthesis. */
112
+ model?: string;
113
+ /** Max server-side searches per query. */
114
+ maxUses?: number | string;
115
+ /** `enabled` or `disabled`. */
116
+ thinking?: string;
117
+ }
98
118
  /** Context passed to the plugin's `setup`. */
99
119
  export interface WebsearchContext {
100
120
  websearch: {
101
121
  transform(callback: (editor: WebsearchEditor) => void): Promise<unknown> | unknown;
102
122
  };
123
+ /** Plugin options from the `plugins` object form. */
124
+ options?: Record<string, unknown>;
103
125
  /** Present in OpenCode 2.x; used to reuse the DeepSeek provider credential. */
104
126
  integration?: IntegrationContext;
105
127
  }
106
128
  /**
107
- * Resolve the DeepSeek API key. An explicit environment variable takes
108
- * precedence; otherwise the key OpenCode stores for the `deepseek` provider
109
- * (configured via `opencode auth login`) is used.
129
+ * Resolve the DeepSeek API key, in order of precedence: the `apiKey` plugin
130
+ * option, then the `DEEPSEEK_API_KEY`/`WEBSEARCH_API_KEY` environment
131
+ * variables, then the credential OpenCode stores for the `deepseek` provider
132
+ * (configured via `opencode auth login`).
110
133
  */
111
- export declare function resolveApiKey(ctx: WebsearchContext): Promise<string | undefined>;
134
+ export declare function resolveApiKey(ctx: WebsearchContext, options?: WebsearchOptions): Promise<string | undefined>;
112
135
  /**
113
136
  * Resolve the `max_uses` value for the search tool declaration.
114
137
  * Falls back to {@link DEFAULT_MAX_USES} for missing or invalid input.
115
138
  */
116
- export declare function resolveMaxUses(raw?: string | undefined): number;
139
+ export declare function resolveMaxUses(raw?: string | number | undefined): number;
117
140
  /**
118
141
  * Resolve the thinking mode. Only `disabled` disables extended thinking;
119
142
  * anything else (including unset) keeps the historical `enabled` behavior.
package/dist/index.js CHANGED
@@ -46,12 +46,20 @@ export const SYSTEM_PROMPT = [
46
46
  "",
47
47
  "Your response must be the final answer, not another search request.",
48
48
  ].join("\n");
49
+ /** Return a trimmed non-empty string, or undefined for any other value. */
50
+ function readString(value) {
51
+ return typeof value === "string" && value.trim() ? value.trim() : undefined;
52
+ }
49
53
  /**
50
- * Resolve the DeepSeek API key. An explicit environment variable takes
51
- * precedence; otherwise the key OpenCode stores for the `deepseek` provider
52
- * (configured via `opencode auth login`) is used.
54
+ * Resolve the DeepSeek API key, in order of precedence: the `apiKey` plugin
55
+ * option, then the `DEEPSEEK_API_KEY`/`WEBSEARCH_API_KEY` environment
56
+ * variables, then the credential OpenCode stores for the `deepseek` provider
57
+ * (configured via `opencode auth login`).
53
58
  */
54
- export async function resolveApiKey(ctx) {
59
+ export async function resolveApiKey(ctx, options) {
60
+ const fromOptions = readString(options?.apiKey);
61
+ if (fromOptions)
62
+ return fromOptions;
55
63
  const fromEnv = readEnv("DEEPSEEK_API_KEY") ?? readEnv("WEBSEARCH_API_KEY");
56
64
  if (fromEnv)
57
65
  return fromEnv;
@@ -97,6 +105,9 @@ function throwCancellation(signal, fallback) {
97
105
  * Falls back to {@link DEFAULT_MAX_USES} for missing or invalid input.
98
106
  */
99
107
  export function resolveMaxUses(raw = process.env.WEBSEARCH_MAX_USES) {
108
+ if (typeof raw === "number") {
109
+ return Number.isSafeInteger(raw) && raw > 0 ? raw : DEFAULT_MAX_USES;
110
+ }
100
111
  const value = (typeof raw === "string" ? raw : "").trim();
101
112
  // Strict: only a plain positive integer. Rejects "1e3", "5.5", "5abc" and
102
113
  // values outside the safe-integer range (e.g. a 24-digit number).
@@ -230,22 +241,23 @@ export function toResults(answer, sources = []) {
230
241
  export const plugin = {
231
242
  id: "websearch.deepseek",
232
243
  async setup(ctx) {
244
+ const options = (ctx.options ?? {});
233
245
  await ctx.websearch.transform((editor) => {
234
246
  editor.add({
235
247
  id: "deepseek",
236
248
  name: "DeepSeek Web Search",
237
249
  execute: async ({ query }, { signal } = {}) => {
238
- const apiKey = await resolveApiKey(ctx);
250
+ const apiKey = await resolveApiKey(ctx, options);
239
251
  if (!apiKey) {
240
- throw new Error("DEEPSEEK_API_KEY is not set and no stored DeepSeek credential was found; set the env var or sign in to the deepseek provider in OpenCode");
252
+ throw new Error("No DeepSeek API key: set the DEEPSEEK_API_KEY env var, pass the apiKey plugin option, or sign in to the deepseek provider in OpenCode");
241
253
  }
242
254
  if (!/^[\x21-\x7e]+$/.test(apiKey)) {
243
255
  throw new Error("The API key contains invalid characters");
244
256
  }
245
257
  const body = buildRequestBody(query, {
246
- model: readEnv("WEBSEARCH_MODEL") ?? DEFAULT_MODEL,
247
- maxUses: resolveMaxUses(),
248
- thinking: resolveThinking(),
258
+ model: readString(options.model) ?? readEnv("WEBSEARCH_MODEL") ?? DEFAULT_MODEL,
259
+ maxUses: resolveMaxUses(options.maxUses ?? readEnv("WEBSEARCH_MAX_USES")),
260
+ thinking: resolveThinking(readString(options.thinking) ?? readEnv("WEBSEARCH_THINKING")),
249
261
  });
250
262
  const response = await fetch(API_URL, {
251
263
  method: "POST",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "opencode-websearch-deepseek",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "description": "DeepSeek-powered web search provider for OpenCode's built-in websearch tool.",
6
6
  "type": "module",
7
7
  "license": "MIT",