dsh-web-search-workbuddy 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.
Files changed (3) hide show
  1. package/README.md +17 -5
  2. package/lib/index.js +163 -42
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -63,11 +63,8 @@ installed this way; `@deepseek-ai/schemastery` ships as a direct dependency.
63
63
  - insert:
64
64
  - id: web-search-workbuddy
65
65
  name: dsh-web-search-workbuddy
66
- config:
67
- baseURL: http://127.0.0.1:7864 # workbuddy-manager address
68
- apiKeyEnv: WORKBUDDY_MANNAGER_CN_API_KEY # same wbk_ key as the chat route
69
- timeoutMs: 30000
70
- maxResults: 5
66
+ # No baseURL / apiKeyEnv needed: both derive from the conversation's own
67
+ # model-provider route (its apiKeyEnv credential + baseURL with /v1/ stripped).
71
68
  - id: web
72
69
  name: '@deepseek-ai/dsh-web'
73
70
  config:
@@ -82,6 +79,21 @@ installed this way; `@deepseek-ai/schemastery` ships as a direct dependency.
82
79
  Fully restart `dsh web` afterwards (patch changes do not HMR). The model's tool
83
80
  catalog gains `web_search` in new sessions.
84
81
 
82
+ ### Zero-config credential derivation
83
+
84
+ With no explicit config, the plugin reads the **model provider route the
85
+ conversation is already using** (e.g. `workbuddy-mannager-cn` registered under
86
+ `llm-pi-ai`): its `apiKeyEnv` credential reference is resolved for the search
87
+ request, and its `baseURL` minus the `/v1/` suffix becomes the search origin —
88
+ the search endpoint lives on the same manager host (`/v1/tools/search` beside
89
+ `/v1/chat/completions`). Same mechanism as the official
90
+ `@deepseek-ai/dsh-web-deepseek-api` (`listConfigurableProviders` → loader
91
+ entries → effective config).
92
+
93
+ Explicit `baseURL` / `apiKeyEnv` still win when set — for deployments whose
94
+ search endpoint differs from the chat route, or whose model key cannot reach
95
+ the search route.
96
+
85
97
  ## Protocol
86
98
 
87
99
  ```json
package/lib/index.js CHANGED
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * WorkBuddy gateway-backed search provider for the DSH web capability seam
5
5
  * (ctx.web). Bridges `ctx.web.search()` to the workbuddy2api-go gateway's
6
- * agenttool web-search passthrough endpoint (0057 patch):
6
+ * agenttool web-search passthrough endpoint (0057 patch) **through
7
+ * workbuddy-manager**:
7
8
  *
8
9
  * POST {baseURL}/v1/tools/search
9
10
  * body: {"query": "...", "max_results": 5, "blocked_domains": [...], "freshness": "d1|m1|y1"}
@@ -12,6 +13,26 @@
12
13
  *
13
14
  * Provider id registers as "workbuddy-gateway"; pin it with
14
15
  * `web.searchProvider: workbuddy-gateway` in the profile patch.
16
+ *
17
+ * ## Credential & endpoint resolution (zero-config by default)
18
+ *
19
+ * With no `baseURL`/`apiKeyEnv` in this plugin's config, both are **derived
20
+ * from the model provider route the conversation is already using**: the
21
+ * initiating session's `requestContext().provider` names a route (e.g.
22
+ * `workbuddy-mannager-cn`), this plugin reads that route's profile from the
23
+ * `llm-pi-ai` settings section (`llm.listConfigurableProviders()` →
24
+ * `loader.entries()` → effective config, same mechanism as the official
25
+ * `dsh-web-deepseek-api`), and takes its `apiKeyEnv` + `baseURL`.
26
+ *
27
+ * - `apiKeyEnv` resolution order: the route profile's `apiKeyEnv` credential
28
+ * reference (resolved via `ctx.credentials`, then the launch environment).
29
+ * - `baseURL`: the route profile's `baseURL` with any `/v1/` suffix stripped
30
+ * (the chat route posts to `/v1/chat/completions`; search posts to
31
+ * `/v1/tools/search` on the same origin). A `credentialUrl`-shaped baseURL
32
+ * is rejected — this is not a per-model endpoint.
33
+ *
34
+ * Explicit config always wins (for deployments whose search endpoint differs
35
+ * from the chat route); omit both fields and the assembly is three lines.
15
36
  */
16
37
  import z from "@deepseek-ai/schemastery";
17
38
  import { credentialRef } from "@deepseek-ai/dsh-credentials";
@@ -26,11 +47,20 @@ const name = "web-search-workbuddy";
26
47
  /** The web seam this provider registers into. */
27
48
  const inject = ["web"];
28
49
 
50
+ /** Loader package that owns the pi-ai provider profiles. */
51
+ const PI_AI_PACKAGE = "@deepseek-ai/dsh-llm-pi-ai";
52
+
29
53
  const Config = z.object({
30
- /** Gateway base (manager reverse-proxy or the wb2api-go port directly). */
31
- baseURL: z.string().default("http://127.0.0.1:7864").volatile(),
32
- /** Credential reference holding the gateway API key. */
33
- apiKeyEnv: z.string().role("credential-ref").default("WORKBUDDY_MANNAGER_CN_API_KEY").volatile(),
54
+ /**
55
+ * Gateway base. Omit to derive it from the conversation's model-provider
56
+ * route profile (its `baseURL` with any `/v1/` suffix stripped).
57
+ */
58
+ baseURL: z.string().volatile(),
59
+ /**
60
+ * Credential reference holding the gateway API key. Omit to derive it from
61
+ * the conversation's model-provider route profile (`apiKeyEnv`).
62
+ */
63
+ apiKeyEnv: z.string().role("credential-ref").volatile(),
34
64
  /** Single-request timeout; the gateway itself bounds upstream at 20s/account. */
35
65
  timeoutMs: z.number().step(1).min(1000).default(30000).volatile(),
36
66
  /** Default max_results when the request omits maxResults. */
@@ -38,35 +68,132 @@ const Config = z.object({
38
68
  });
39
69
 
40
70
  /**
41
- * Resolve the gateway API key for one search: credentials service first,
42
- * launch environment fallback. Undefined when neither holds a value.
71
+ * The model provider route of the conversation this search serves.
72
+ * Falls back to the sole configured route when there is no initiating
73
+ * session (headless / direct calls) and exactly one exists.
43
74
  */
44
- async function resolveApiKey(ctx, options) {
45
- const credentials = ctx.get("credentials");
46
- if (credentials !== undefined) {
47
- const value = (await credentials.resolve(credentialRef(options.apiKeyEnv)))?.value;
48
- if (value !== undefined && value.length > 0) return value;
49
- }
50
- const ambient = process.env[options.apiKeyEnv];
51
- return ambient !== undefined && ambient.length > 0 ? ambient : undefined;
75
+ function conversationRoute(ctx) {
76
+ const llm = ctx.get("llm");
77
+ if (llm === undefined) return undefined;
78
+ const fromSession = ctx.get("agents")?.currentInitiator()?.session.requestContext()?.provider;
79
+ if (fromSession !== undefined) return fromSession;
80
+ const configured = llm.listConfigurableProviders();
81
+ return configured.length === 1 ? configured[0].provider : undefined;
52
82
  }
53
83
 
54
84
  /**
55
- * Whether a search for a model request has a gateway key to send. Presence
56
- * only: no network calls, no secret value read beyond existence.
85
+ * Read the pi-ai provider profile for one route: the official
86
+ * `dsh-web-deepseek-api` mechanism (listConfigurableProviders → loader
87
+ * entries → effective config), generalized to any `llm-pi-ai` route by
88
+ * walking the directory entry's `settingsPath`.
89
+ * @returns the profile object, or undefined when the route is not a
90
+ * pi-ai route or its settings section is not mounted.
57
91
  */
58
- async function searchConfigured(ctx, options) {
92
+ function routeProfile(ctx, provider) {
93
+ const llm = ctx.get("llm");
94
+ const loader = ctx.get("loader");
95
+ if (llm === undefined || loader === undefined) return undefined;
96
+ if (!llm.listProviders().some((row) => row.id === provider)) return undefined;
97
+ const directory = llm.listConfigurableProviders().find((row) => row.provider === provider);
98
+ if (directory === undefined || directory.settingsNs !== "llm-pi-ai") return undefined;
99
+ const entry = [...loader.entries()].find(
100
+ (row) => row.options.id === directory.settingsNs && row.options.name === PI_AI_PACKAGE,
101
+ );
102
+ if (entry?.fiber?.state !== 2) return undefined;
103
+ let node = entry.fiber.effectiveConfig;
104
+ for (const key of directory.settingsPath) node = node?.[key];
105
+ return node;
106
+ }
107
+
108
+ /** Strip a chat-route `/v1/` suffix so search posts to the same origin. */
109
+ function originOf(baseURL) {
110
+ return baseURL.replace(/\/v1\/?$/, "").replace(/\/+$/, "");
111
+ }
112
+
113
+ /**
114
+ * Resolve one operation's endpoint + credential. Explicit config wins;
115
+ * otherwise both derive from the conversation's model-provider route profile.
116
+ * @returns {Promise<{baseURL: string, apiKey: string}>}
117
+ * @throws {WebError} WEB_PROVIDER_CREDENTIAL_MISSING / WEB_PROVIDER_CONFIGURED_MISSING
118
+ */
119
+ async function resolveTarget(ctx, options, signal) {
120
+ let { baseURL, apiKeyEnv } = options;
121
+
122
+ if (baseURL === undefined || apiKeyEnv === undefined) {
123
+ const route = conversationRoute(ctx);
124
+ const profile = route !== undefined ? routeProfile(ctx, route) : undefined;
125
+ if (profile === undefined) {
126
+ throw new WebError(
127
+ `web-search-workbuddy has no baseURL/apiKeyEnv configured and cannot derive them: ` +
128
+ `no pi-ai model-provider route is resolvable for this conversation ` +
129
+ `(route: ${String(route)}). Set web-search-workbuddy.baseURL and .apiKeyEnv explicitly.`,
130
+ "WEB_PROVIDER_CONFIGURED_MISSING",
131
+ );
132
+ }
133
+ if (apiKeyEnv === undefined) apiKeyEnv = profile.apiKeyEnv;
134
+ if (baseURL === undefined && typeof profile.baseURL === "string" && profile.baseURL.length > 0) {
135
+ baseURL = originOf(profile.baseURL);
136
+ }
137
+ }
138
+
139
+ if (baseURL === undefined || !URL.canParse(baseURL)) {
140
+ throw new WebError(
141
+ `web-search-workbuddy has no usable baseURL (configured or derived: ${String(baseURL)}). ` +
142
+ `Set web-search-workbuddy.baseURL to the workbuddy-manager origin.`,
143
+ "WEB_PROVIDER_CONFIGURED_MISSING",
144
+ );
145
+ }
146
+ baseURL = baseURL.replace(/\/+$/, "");
147
+
148
+ if (apiKeyEnv === undefined) {
149
+ throw new WebError(
150
+ `web-search-workbuddy could not derive an apiKeyEnv: the conversation's model route ` +
151
+ `profile names no credential reference. Set web-search-workbuddy.apiKeyEnv explicitly.`,
152
+ "WEB_PROVIDER_CONFIGURED_MISSING",
153
+ );
154
+ }
155
+
59
156
  const credentials = ctx.get("credentials");
157
+ const ref = credentialRef(apiKeyEnv);
158
+ let apiKey;
60
159
  if (credentials !== undefined) {
61
- try {
62
- const described = await credentials.describe(credentialRef(options.apiKeyEnv));
63
- if (described?.configured === true) return true;
64
- } catch (_unreadableStore) {
65
- // fall through to the environment check
66
- }
160
+ const value = (await credentials.resolve(ref))?.value;
161
+ if (value !== undefined && value !== null && value.length > 0) apiKey = value;
162
+ }
163
+ if (apiKey === undefined || apiKey.length === 0) {
164
+ const ambient = process.env[apiKeyEnv];
165
+ if (ambient !== undefined && ambient.length > 0) apiKey = ambient;
166
+ }
167
+ if (apiKey === undefined || apiKey.length === 0) {
168
+ throw new WebError(
169
+ `WorkBuddy gateway search has no API key for "${apiKeyEnv}": the model route's credential ` +
170
+ `reference resolves nothing for search. Store it through the credentials service or export it ` +
171
+ `in the launching environment.`,
172
+ "WEB_PROVIDER_CREDENTIAL_MISSING",
173
+ );
174
+ }
175
+ if (signal?.aborted === true) {
176
+ throw new WebError("WorkBuddy gateway search aborted", "WEB_ABORTED", { cause: signal.reason });
67
177
  }
68
- const ambient = process.env[options.apiKeyEnv];
69
- return ambient !== undefined && ambient.length > 0;
178
+ return { baseURL, apiKey };
179
+ }
180
+
181
+ /**
182
+ * Whether a search for a model request has an endpoint + credential to send.
183
+ * Presence only: no network calls, no secret value read beyond existence.
184
+ */
185
+ async function searchConfigured(ctx, options, llmProvider) {
186
+ if (options.baseURL !== undefined && options.apiKeyEnv !== undefined) return true;
187
+ const route = llmProvider ?? conversationRoute(ctx);
188
+ const profile = route !== undefined ? routeProfile(ctx, route) : undefined;
189
+ if (profile === undefined) return false;
190
+ const hasKey =
191
+ (typeof profile.apiKeyEnv === "string" && profile.apiKeyEnv.length > 0) ||
192
+ process.env[profile.apiKeyEnv ?? ""] !== undefined;
193
+ const hasURL =
194
+ options.baseURL !== undefined ||
195
+ (typeof profile.baseURL === "string" && profile.baseURL.length > 0);
196
+ return hasKey && hasURL;
70
197
  }
71
198
 
72
199
  /** Map the agenttool envelope to the seam's normalized result. */
@@ -92,17 +219,11 @@ function mapResponse(data) {
92
219
 
93
220
  /** One search against the gateway. Honors `signal` for cancellation. */
94
221
  async function search(ctx, options, request, signal) {
95
- const apiKey = await resolveApiKey(ctx, options);
96
- if (apiKey === undefined) {
97
- throw new WebError(
98
- `WorkBuddy gateway search has no API key for "${options.apiKeyEnv}"; store it through the credentials service or export it in the launching environment`,
99
- "WEB_PROVIDER_CREDENTIAL_MISSING",
100
- );
101
- }
222
+ const { baseURL, apiKey } = await resolveTarget(ctx, options, signal);
102
223
 
103
224
  let response;
104
225
  try {
105
- response = await fetch(`${options.baseURL}/v1/tools/search`, {
226
+ response = await fetch(`${baseURL}/v1/tools/search`, {
106
227
  method: "POST",
107
228
  redirect: "error",
108
229
  headers: {
@@ -120,7 +241,7 @@ async function search(ctx, options, request, signal) {
120
241
  throw new WebError("WorkBuddy gateway search aborted", "WEB_ABORTED", { cause: signal?.reason ?? error });
121
242
  }
122
243
  throw new WebError(
123
- `WorkBuddy gateway search request failed: ${String(error)}\n\nThe web request used endpoint ${JSON.stringify(`${options.baseURL}/v1/tools/search`)}. Check that the workbuddy2api gateway (0057 patch build) is running and that web-search-workbuddy.baseURL points at it.`,
244
+ `WorkBuddy gateway search request failed: ${String(error)}\n\nThe web request used endpoint ${JSON.stringify(`${baseURL}/v1/tools/search`)}. Check that workbuddy-manager (with the /v1/tools/search passthrough) is running and reachable.`,
124
245
  "WEB_PROVIDER_ERROR",
125
246
  { cause: error },
126
247
  );
@@ -143,7 +264,7 @@ async function search(ctx, options, request, signal) {
143
264
  );
144
265
  }
145
266
  throw new WebError(
146
- `WorkBuddy gateway search failed (HTTP ${response.status})${detail.length > 0 ? `: ${detail}` : ""}\n\nThe web request used endpoint ${JSON.stringify(`${options.baseURL}/v1/tools/search`)}.`,
267
+ `WorkBuddy gateway search failed (HTTP ${response.status})${detail.length > 0 ? `: ${detail}` : ""}\n\nThe web request used endpoint ${JSON.stringify(`${baseURL}/v1/tools/search`)}.`,
147
268
  "WEB_PROVIDER_ERROR",
148
269
  );
149
270
  }
@@ -169,13 +290,13 @@ async function search(ctx, options, request, signal) {
169
290
  }
170
291
 
171
292
  /**
172
- * Register the gateway-backed search provider with ctx.web. `ready()` answers
173
- * whether a search has a gateway key configured (tool-web uses this to decide
174
- * whether the model step admits `web_search`).
293
+ * Register the gateway-backed search provider with ctx.web. Endpoint and
294
+ * credential default to the conversation's own model-provider route; `ready()`
295
+ * answers presence (tool-web uses it to admit `web_search` into a request).
175
296
  */
176
297
  function apply(ctx, config) {
177
298
  const options = () => ({
178
- baseURL: config.baseURL.get().replace(/\/+$/, ""),
299
+ baseURL: config.baseURL.get(),
179
300
  apiKeyEnv: config.apiKeyEnv.get(),
180
301
  timeoutMs: config.timeoutMs.get(),
181
302
  maxResults: config.maxResults.get(),
@@ -183,8 +304,8 @@ function apply(ctx, config) {
183
304
 
184
305
  ctx.web.registerSearchProvider({
185
306
  id: PROVIDER_ID,
186
- available: () => URL.canParse(options().baseURL),
187
- ready: (async () => searchConfigured(ctx, options())),
307
+ available: () => true,
308
+ ready: (llmProvider) => searchConfigured(ctx, options(), llmProvider),
188
309
  search: (request, signal) => search(ctx, options(), request, signal),
189
310
  });
190
311
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-web-search-workbuddy",
3
3
  "description": "WorkBuddy gateway-backed search provider (agenttool /v1/tools/search passthrough via the account-pool gateway) for the DeepSeek Harness web capability seam (ctx.web)",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/types/index.d.ts",