@fffangchenghao/dsh-web-search 0.19.3 → 0.20.1
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/CHANGELOG.md +173 -1
- package/README.md +214 -34
- package/README.zh-CN.md +187 -35
- package/SECURITY.md +42 -7
- package/client.js +248 -10
- package/docs/store-evidence.md +10 -5
- package/lib/advanced-search.d.ts +107 -0
- package/lib/advanced-search.js +255 -0
- package/lib/bridge.d.ts +34 -0
- package/lib/bridge.js +305 -0
- package/lib/cache.d.ts +11 -4
- package/lib/cache.js +9 -5
- package/lib/config.d.ts +19 -3
- package/lib/config.js +24 -0
- package/lib/domain-tool.d.ts +5 -0
- package/lib/domain-tool.js +45 -6
- package/lib/engine-test.d.ts +116 -0
- package/lib/engine-test.js +256 -0
- package/lib/engines.d.ts +11 -1
- package/lib/engines.js +118 -19
- package/lib/errors.d.ts +16 -2
- package/lib/errors.js +40 -1
- package/lib/index.d.ts +24 -2
- package/lib/index.js +309 -21
- package/lib/inject.d.ts +68 -0
- package/lib/inject.js +1 -0
- package/lib/markdown.js +18 -1
- package/lib/mirrors.d.ts +23 -0
- package/lib/mirrors.js +69 -0
- package/lib/official-feed.d.ts +114 -0
- package/lib/official-feed.js +288 -0
- package/lib/official-support.d.ts +74 -0
- package/lib/official-support.js +134 -0
- package/lib/platform-tool.d.ts +77 -0
- package/lib/platform-tool.js +173 -0
- package/lib/platforms.d.ts +178 -0
- package/lib/platforms.js +1035 -0
- package/lib/query-router.d.ts +29 -0
- package/lib/query-router.js +58 -0
- package/lib/read-page.js +2 -1
- package/lib/research-cache.d.ts +115 -0
- package/lib/research-cache.js +275 -0
- package/lib/research.d.ts +45 -4
- package/lib/research.js +505 -22
- package/lib/search-many.d.ts +7 -1
- package/lib/search-many.js +7 -2
- package/lib/system-prompt.d.ts +27 -0
- package/lib/system-prompt.js +50 -0
- package/lib/tool-suite.d.ts +97 -0
- package/lib/tool-suite.js +224 -0
- package/lib/transport.d.ts +33 -2
- package/lib/transport.js +42 -9
- package/lib/types.d.ts +20 -0
- package/lib/types.js +9 -1
- package/lib/untrusted.d.ts +33 -0
- package/lib/untrusted.js +42 -0
- package/lib/verticals.d.ts +3 -1
- package/lib/verticals.js +26 -7
- package/package.json +5 -3
- package/skills/dsh-web-search/SKILL.md +21 -6
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model-facing `advanced_search` tool: one search with a per-call date
|
|
3
|
+
* window.
|
|
4
|
+
*
|
|
5
|
+
* `web_search` takes only a query and a result count, and the Settings
|
|
6
|
+
* `timeRange` applies to every ordinary search. This tool is the narrow escape
|
|
7
|
+
* hatch for the common case where only one question needs a window ("what
|
|
8
|
+
* changed this week?"), without asking the user to change a global setting.
|
|
9
|
+
*
|
|
10
|
+
* It shares the engine, cache and cooldown path with `web_search`; the only
|
|
11
|
+
* difference is the `timeRange` override, so an `off` call can reuse an ordinary
|
|
12
|
+
* cached search and a `week` call gets its own cache key.
|
|
13
|
+
*
|
|
14
|
+
* @module @fangchenghao/dsh-web-search/advanced-search
|
|
15
|
+
*/
|
|
16
|
+
import { TIME_RANGES } from "./freshness.js";
|
|
17
|
+
import { wrapUntrustedContent } from "./untrusted.js";
|
|
18
|
+
/** The name this tool prefers. */
|
|
19
|
+
export const ADVANCED_SEARCH_TOOL_NAME = 'advanced_search';
|
|
20
|
+
/** The name used when another plugin already registered `advanced_search`. */
|
|
21
|
+
export const ADVANCED_SEARCH_FALLBACK_NAME = 'web_search_advanced';
|
|
22
|
+
/** Default result budget when the caller names none. */
|
|
23
|
+
export const ADVANCED_SEARCH_DEFAULT_LIMIT = 5;
|
|
24
|
+
/** Largest result budget the tool accepts; the deployment limit still caps it. */
|
|
25
|
+
export const ADVANCED_SEARCH_MAX_LIMIT = 20;
|
|
26
|
+
/**
|
|
27
|
+
* Explain an undated row when a window is active.
|
|
28
|
+
*
|
|
29
|
+
* The engine path prefers dated results inside the window, but falls back to
|
|
30
|
+
* undated rows when no dated result lands inside it. Saying so keeps the model
|
|
31
|
+
* from presenting an undated row as a confirmed "this week" item.
|
|
32
|
+
* @param sources - the rows returned by the search.
|
|
33
|
+
* @param timeRange - the effective window.
|
|
34
|
+
* @returns one sentence, or `undefined` when every row is dated or the window is off.
|
|
35
|
+
*/
|
|
36
|
+
export function undatedWindowNote(sources, timeRange) {
|
|
37
|
+
if (timeRange === 'off')
|
|
38
|
+
return undefined;
|
|
39
|
+
const undated = sources.filter((source) => source.publishedAt === undefined).length;
|
|
40
|
+
if (undated === 0)
|
|
41
|
+
return undefined;
|
|
42
|
+
return `The ${timeRange} publication window is on, but ${undated} of ${sources.length} result(s) have no publication date, so they are not proven to fall inside it.`;
|
|
43
|
+
}
|
|
44
|
+
/** True for a value usable as a record. */
|
|
45
|
+
function isRecord(value) {
|
|
46
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
47
|
+
}
|
|
48
|
+
/** The parameter schema, in the registry's raw JSON-Schema dialect. */
|
|
49
|
+
function parameterSchema() {
|
|
50
|
+
return {
|
|
51
|
+
type: 'object',
|
|
52
|
+
additionalProperties: false,
|
|
53
|
+
properties: {
|
|
54
|
+
query: {
|
|
55
|
+
type: 'string',
|
|
56
|
+
description: 'The search query. Put the distinctive words in it; do not paste a whole document.',
|
|
57
|
+
},
|
|
58
|
+
maxResults: {
|
|
59
|
+
type: 'integer',
|
|
60
|
+
description: `Results wanted, 1–${ADVANCED_SEARCH_MAX_LIMIT}; defaults to ${ADVANCED_SEARCH_DEFAULT_LIMIT}. The deployment limit still caps it.`,
|
|
61
|
+
},
|
|
62
|
+
timeRange: {
|
|
63
|
+
type: 'string',
|
|
64
|
+
enum: [...TIME_RANGES],
|
|
65
|
+
description: 'Publication window for this call: off, day, week, month or year. Omit to use the deployment setting; off disables the window for this query only.',
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
required: ['query'],
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** The output schema, in the same dialect. */
|
|
72
|
+
function outputSchema() {
|
|
73
|
+
return {
|
|
74
|
+
type: 'object',
|
|
75
|
+
additionalProperties: false,
|
|
76
|
+
properties: {
|
|
77
|
+
query: { type: 'string' },
|
|
78
|
+
timeRange: { type: 'string', enum: [...TIME_RANGES] },
|
|
79
|
+
sources: {
|
|
80
|
+
type: 'array',
|
|
81
|
+
items: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
additionalProperties: false,
|
|
84
|
+
properties: {
|
|
85
|
+
url: { type: 'string' },
|
|
86
|
+
title: { type: 'string' },
|
|
87
|
+
snippet: { type: 'string' },
|
|
88
|
+
publishedAt: { type: 'string' },
|
|
89
|
+
},
|
|
90
|
+
required: ['url'],
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
failures: { type: 'array', items: { type: 'string' } },
|
|
94
|
+
note: { type: 'string' },
|
|
95
|
+
truncated: { type: 'boolean' },
|
|
96
|
+
},
|
|
97
|
+
required: ['query', 'timeRange', 'sources', 'failures', 'truncated'],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** Parse model arguments into a validated shape, throwing on nonsense. */
|
|
101
|
+
function parseArgs(args) {
|
|
102
|
+
if (!isRecord(args))
|
|
103
|
+
throw new Error('advanced_search requires arguments');
|
|
104
|
+
const rawQuery = args['query'];
|
|
105
|
+
if (typeof rawQuery !== 'string' || rawQuery.trim().length === 0) {
|
|
106
|
+
throw new Error('advanced_search requires a non-empty query');
|
|
107
|
+
}
|
|
108
|
+
const query = rawQuery.trim();
|
|
109
|
+
const rawLimit = args['maxResults'];
|
|
110
|
+
let maxResults = ADVANCED_SEARCH_DEFAULT_LIMIT;
|
|
111
|
+
if (rawLimit !== undefined) {
|
|
112
|
+
if (typeof rawLimit !== 'number' || !Number.isInteger(rawLimit)
|
|
113
|
+
|| rawLimit < 1 || rawLimit > ADVANCED_SEARCH_MAX_LIMIT) {
|
|
114
|
+
throw new Error(`advanced_search maxResults must be an integer between 1 and ${ADVANCED_SEARCH_MAX_LIMIT}`);
|
|
115
|
+
}
|
|
116
|
+
maxResults = rawLimit;
|
|
117
|
+
}
|
|
118
|
+
const rawRange = args['timeRange'];
|
|
119
|
+
if (rawRange === undefined)
|
|
120
|
+
return { query, maxResults };
|
|
121
|
+
if (typeof rawRange !== 'string' || !TIME_RANGES.includes(rawRange)) {
|
|
122
|
+
throw new Error(`advanced_search timeRange must be one of ${TIME_RANGES.join(', ')}`);
|
|
123
|
+
}
|
|
124
|
+
return { query, maxResults, timeRange: rawRange };
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Remove a relative window phrase that the structured window already carries.
|
|
128
|
+
* "过去 7 天内 Chrome…" searches for `7` and `内`, not for Chrome; with an
|
|
129
|
+
* explicit `timeRange` the phrase is redundant and only adds noise.
|
|
130
|
+
* @param query - the caller's query.
|
|
131
|
+
* @returns the query without a relative-time phrase, or the original when the
|
|
132
|
+
* removal would leave nothing.
|
|
133
|
+
*/
|
|
134
|
+
export function stripRelativeWindow(query) {
|
|
135
|
+
const cleaned = query
|
|
136
|
+
.replace(/(?:过去|最近)\s*(?:\d+|一|几|半)\s*(?:天|日|周|星期|个月|月|年)(?:内|里|以内)?/gu, ' ')
|
|
137
|
+
.replace(/\b(?:in\s+the\s+)?(?:past|last)\s+\d+\s+(?:days?|weeks?|months?|years?)\b/giu, ' ')
|
|
138
|
+
.replace(/\s+/gu, ' ')
|
|
139
|
+
.replace(/^[\s,,。.;;::]+|[\s,,。.;;::]+$/gu, '')
|
|
140
|
+
.trim();
|
|
141
|
+
return cleaned.length > 0 ? cleaned : query;
|
|
142
|
+
}
|
|
143
|
+
/** Format the model-facing result text. */
|
|
144
|
+
export function formatAdvancedSearchValue(value) {
|
|
145
|
+
const head = `Search: ${JSON.stringify(value.query)} (timeRange=${value.timeRange}) — `
|
|
146
|
+
+ `${value.sources.length} result(s)${value.truncated ? ' (truncated)' : ''}.`;
|
|
147
|
+
const lines = value.sources.map((source, index) => {
|
|
148
|
+
const title = source.title !== undefined && source.title.length > 0 ? source.title : source.url;
|
|
149
|
+
const parts = [`${index + 1}. ${title}`, ` ${source.url}`];
|
|
150
|
+
if (source.snippet !== undefined && source.snippet.length > 0)
|
|
151
|
+
parts.push(` ${source.snippet}`);
|
|
152
|
+
if (source.publishedAt !== undefined && source.publishedAt.length > 0)
|
|
153
|
+
parts.push(` ${source.publishedAt}`);
|
|
154
|
+
return parts.join('\n');
|
|
155
|
+
});
|
|
156
|
+
if (value.note !== undefined && value.note.length > 0)
|
|
157
|
+
lines.push(`Note: ${value.note}`);
|
|
158
|
+
if (value.failures.length > 0)
|
|
159
|
+
lines.push(`failures: ${value.failures.join('; ')}`);
|
|
160
|
+
return [head, '', ...lines].join('\n').trimEnd();
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Build the tool definition the host registry accepts.
|
|
164
|
+
* @param name - `advanced_search`, or the fallback when that name is taken.
|
|
165
|
+
* @param runSearch - executes one search through the plugin's normal path.
|
|
166
|
+
* @returns a registry-ready definition.
|
|
167
|
+
*/
|
|
168
|
+
export function createAdvancedSearchDefinition(name, runSearch) {
|
|
169
|
+
return {
|
|
170
|
+
name,
|
|
171
|
+
description: 'Search the web with an optional publication-time window for this call. Use it instead of web_search when one query needs results from the last day, week, month or year. Returns ranked candidates (URL, title, snippet, date), not page bodies.',
|
|
172
|
+
parameters: parameterSchema(),
|
|
173
|
+
output: {
|
|
174
|
+
schema: outputSchema(),
|
|
175
|
+
render: (_args, value) => [{ type: 'text', text: wrapUntrustedContent(formatAdvancedSearchValue(value)) }],
|
|
176
|
+
},
|
|
177
|
+
timeoutMs: 30_000,
|
|
178
|
+
isConcurrencySafe: () => true,
|
|
179
|
+
async execute(args, exec) {
|
|
180
|
+
const parsed = parseArgs(args);
|
|
181
|
+
const searchQuery = parsed.timeRange === undefined || parsed.timeRange === 'off'
|
|
182
|
+
? parsed.query
|
|
183
|
+
: stripRelativeWindow(parsed.query);
|
|
184
|
+
const outcome = await runSearch(searchQuery, parsed.maxResults, parsed.timeRange, exec.signal);
|
|
185
|
+
return {
|
|
186
|
+
query: parsed.query,
|
|
187
|
+
timeRange: outcome.timeRange,
|
|
188
|
+
sources: outcome.sources.slice(0, parsed.maxResults).map((source) => ({ ...source })),
|
|
189
|
+
failures: [...outcome.failures],
|
|
190
|
+
...(outcome.note === undefined ? {} : { note: outcome.note }),
|
|
191
|
+
truncated: outcome.truncated || outcome.sources.length > parsed.maxResults,
|
|
192
|
+
};
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Pick the tool name this deployment can accept.
|
|
198
|
+
* @param tools - the live registry.
|
|
199
|
+
* @returns the preferred or fallback name.
|
|
200
|
+
*/
|
|
201
|
+
export function pickAdvancedSearchToolName(tools) {
|
|
202
|
+
if (typeof tools.get !== 'function')
|
|
203
|
+
return ADVANCED_SEARCH_TOOL_NAME;
|
|
204
|
+
try {
|
|
205
|
+
return tools.get(ADVANCED_SEARCH_TOOL_NAME) === undefined
|
|
206
|
+
? ADVANCED_SEARCH_TOOL_NAME
|
|
207
|
+
: ADVANCED_SEARCH_FALLBACK_NAME;
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
return ADVANCED_SEARCH_TOOL_NAME;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** Report a registration failure without throwing out of the plugin. */
|
|
214
|
+
function report(options, error) {
|
|
215
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
216
|
+
try {
|
|
217
|
+
options.warn?.(`advanced_search registration skipped: ${message}`);
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
// A diagnostic callback must never break plugin loading.
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Register the tool when a registry is available.
|
|
225
|
+
* @param tools - the live registry.
|
|
226
|
+
* @param runSearch - executes one search through the plugin's normal path.
|
|
227
|
+
* @param options - diagnostics.
|
|
228
|
+
* @returns the registered name and its disposer, or `undefined` when skipped.
|
|
229
|
+
*/
|
|
230
|
+
export function registerAdvancedSearchTool(tools, runSearch, options = {}) {
|
|
231
|
+
const name = pickAdvancedSearchToolName(tools);
|
|
232
|
+
try {
|
|
233
|
+
const registered = tools.register(createAdvancedSearchDefinition(name, runSearch));
|
|
234
|
+
const disposeHost = typeof registered === 'function' ? registered : undefined;
|
|
235
|
+
let disposed = false;
|
|
236
|
+
return {
|
|
237
|
+
name,
|
|
238
|
+
dispose: () => {
|
|
239
|
+
if (disposed)
|
|
240
|
+
return;
|
|
241
|
+
disposed = true;
|
|
242
|
+
try {
|
|
243
|
+
disposeHost?.();
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
// Disposal must stay contained; the plugin is already going away.
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
report(options, error);
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
}
|
package/lib/bridge.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { EngineTestRunner } from './engine-test.ts';
|
|
2
|
+
import type { InjectableHost, ServiceContext } from './inject.ts';
|
|
3
|
+
/** Route prefix the client fetches; kept stable for the shipped settings page. */
|
|
4
|
+
export declare const ENGINE_TEST_BRIDGE_PATH = "/api/dsh-web-search/engine-test";
|
|
5
|
+
/** Settings-page update button; the only package it may install is this one. */
|
|
6
|
+
export declare const UPDATE_BRIDGE_PATH = "/api/dsh-web-search/update";
|
|
7
|
+
/** Exact package this bridge is allowed to install. */
|
|
8
|
+
export declare const OWN_PACKAGE_NAME = "@fffangchenghao/dsh-web-search";
|
|
9
|
+
/** The injected services this bridge needs; `webServer` may be absent. */
|
|
10
|
+
export type BridgeContext = ServiceContext;
|
|
11
|
+
/** A context that can inject the web server and may look up the plugin manager. */
|
|
12
|
+
export type UpdateContext = InjectableHost & Pick<InjectableHost, 'get'>;
|
|
13
|
+
/** A context that may support `inject`; test hosts often do not. */
|
|
14
|
+
export type InjectableContext = InjectableHost;
|
|
15
|
+
export type { WebServerLike } from './inject.ts';
|
|
16
|
+
/**
|
|
17
|
+
* The Settings page's update button.
|
|
18
|
+
*
|
|
19
|
+
* It deliberately does not run a package manager itself: the host's own
|
|
20
|
+
* `pluginManager` owns the profile lock and the pnpm invocation. The bridge
|
|
21
|
+
* only accepts this package, refuses a local link/file install, and reports
|
|
22
|
+
* that a restart is required. The model tool path is untouched.
|
|
23
|
+
*
|
|
24
|
+
* @param ctx - injectable plugin context.
|
|
25
|
+
* @returns a disposer, or `undefined` when the host has no injectable server.
|
|
26
|
+
*/
|
|
27
|
+
export declare function registerUpdateBridge(ctx: UpdateContext): (() => void) | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Register the engine-test endpoint.
|
|
30
|
+
* @param ctx - injectable plugin context.
|
|
31
|
+
* @param runTest - engine test runner shared with the model tool.
|
|
32
|
+
* @returns a disposer, or `undefined` when the host has no injectable server.
|
|
33
|
+
*/
|
|
34
|
+
export declare function registerEngineTestBridge(ctx: InjectableContext, runTest: EngineTestRunner): (() => void) | undefined;
|
package/lib/bridge.js
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { runEngineTestValue } from "./engine-test.js";
|
|
2
|
+
import { HOSTS } from "./engines.js";
|
|
3
|
+
/** Route prefix the client fetches; kept stable for the shipped settings page. */
|
|
4
|
+
export const ENGINE_TEST_BRIDGE_PATH = '/api/dsh-web-search/engine-test';
|
|
5
|
+
/** Settings-page update button; the only package it may install is this one. */
|
|
6
|
+
export const UPDATE_BRIDGE_PATH = '/api/dsh-web-search/update';
|
|
7
|
+
/** Exact package this bridge is allowed to install. */
|
|
8
|
+
export const OWN_PACKAGE_NAME = '@fffangchenghao/dsh-web-search';
|
|
9
|
+
/** Largest request body the bridge will read. */
|
|
10
|
+
const MAX_BODY_BYTES = 16 * 1024;
|
|
11
|
+
/** Whether an address is loopback. */
|
|
12
|
+
function isLoopback(address) {
|
|
13
|
+
return address === '127.0.0.1' || address === '::1' || address === '::ffff:127.0.0.1';
|
|
14
|
+
}
|
|
15
|
+
/** Parse a Host header without throwing. */
|
|
16
|
+
function hostOf(value) {
|
|
17
|
+
if (typeof value !== 'string' || value.length === 0)
|
|
18
|
+
return undefined;
|
|
19
|
+
try {
|
|
20
|
+
return new URL(`http://${value}`).host;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Refuse cross-site and non-loopback callers.
|
|
28
|
+
*
|
|
29
|
+
* The Host already authenticates its own `/api` surface; this guard keeps the
|
|
30
|
+
* plugin from widening it. A remote settings page therefore cannot use the
|
|
31
|
+
* button, but it also cannot reach a search runner with no authentication.
|
|
32
|
+
*/
|
|
33
|
+
function isAllowed(request) {
|
|
34
|
+
if (!isLoopback(request.socket.remoteAddress))
|
|
35
|
+
return false;
|
|
36
|
+
if (request.headers['sec-fetch-site'] === 'cross-site')
|
|
37
|
+
return false;
|
|
38
|
+
const origin = request.headers.origin;
|
|
39
|
+
if (origin !== undefined) {
|
|
40
|
+
try {
|
|
41
|
+
if (new URL(origin).host !== hostOf(request.headers.host))
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
/** Send one JSON response. */
|
|
51
|
+
function writeJson(response, status, body) {
|
|
52
|
+
const payload = JSON.stringify(body);
|
|
53
|
+
response.writeHead(status, {
|
|
54
|
+
'content-type': 'application/json; charset=utf-8',
|
|
55
|
+
'cache-control': 'no-store',
|
|
56
|
+
'referrer-policy': 'no-referrer',
|
|
57
|
+
});
|
|
58
|
+
response.end(payload);
|
|
59
|
+
}
|
|
60
|
+
/** Read a bounded JSON body. */
|
|
61
|
+
async function readJson(request) {
|
|
62
|
+
const chunks = [];
|
|
63
|
+
let size = 0;
|
|
64
|
+
for await (const chunk of request) {
|
|
65
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
66
|
+
size += buffer.length;
|
|
67
|
+
if (size > MAX_BODY_BYTES)
|
|
68
|
+
return undefined;
|
|
69
|
+
chunks.push(buffer);
|
|
70
|
+
}
|
|
71
|
+
if (size === 0)
|
|
72
|
+
return {};
|
|
73
|
+
try {
|
|
74
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Parse and validate the request body. */
|
|
81
|
+
function parseBody(body) {
|
|
82
|
+
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
|
83
|
+
throw new Error('body must be a JSON object');
|
|
84
|
+
}
|
|
85
|
+
const record = body;
|
|
86
|
+
const engine = record['engine'];
|
|
87
|
+
const query = record['query'];
|
|
88
|
+
if (engine !== undefined && (typeof engine !== 'string' || engine.trim().length === 0)) {
|
|
89
|
+
throw new Error('engine must be a non-empty string');
|
|
90
|
+
}
|
|
91
|
+
if (query !== undefined && (typeof query !== 'string' || query.trim().length === 0)) {
|
|
92
|
+
throw new Error('query must be a non-empty string');
|
|
93
|
+
}
|
|
94
|
+
if (engine !== undefined && !HOSTS.some((host) => host.id === engine.trim())) {
|
|
95
|
+
throw new Error(`unknown engine ${JSON.stringify(engine)}`);
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
...(engine === undefined ? {} : { engine: engine.trim() }),
|
|
99
|
+
...(query === undefined ? {} : { query: query.trim() }),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** A local checkout must never be replaced by the registry package. */
|
|
103
|
+
function isLocalSource(source) {
|
|
104
|
+
return typeof source === 'string' && /(?:^|@)(?:link|file|workspace):/iu.test(source);
|
|
105
|
+
}
|
|
106
|
+
/** Read the plugin manager from the injected context or the service registry. */
|
|
107
|
+
function managerOf(ctx, services) {
|
|
108
|
+
const injected = services.pluginManager;
|
|
109
|
+
if (injected !== undefined)
|
|
110
|
+
return injected;
|
|
111
|
+
if (typeof ctx.get !== 'function')
|
|
112
|
+
return undefined;
|
|
113
|
+
try {
|
|
114
|
+
const candidate = ctx.get('pluginManager');
|
|
115
|
+
if (typeof candidate !== 'object' || candidate === null)
|
|
116
|
+
return undefined;
|
|
117
|
+
const manager = candidate;
|
|
118
|
+
return typeof manager.listBundles === 'function' && typeof manager.installBundle === 'function'
|
|
119
|
+
? manager
|
|
120
|
+
: undefined;
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** Human-readable failure from a manager result. */
|
|
127
|
+
function failureText(error) {
|
|
128
|
+
if (error === undefined)
|
|
129
|
+
return 'the plugin manager rejected the update';
|
|
130
|
+
return error.diagnostic ?? error.code ?? 'the plugin manager rejected the update';
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The Settings page's update button.
|
|
134
|
+
*
|
|
135
|
+
* It deliberately does not run a package manager itself: the host's own
|
|
136
|
+
* `pluginManager` owns the profile lock and the pnpm invocation. The bridge
|
|
137
|
+
* only accepts this package, refuses a local link/file install, and reports
|
|
138
|
+
* that a restart is required. The model tool path is untouched.
|
|
139
|
+
*
|
|
140
|
+
* @param ctx - injectable plugin context.
|
|
141
|
+
* @returns a disposer, or `undefined` when the host has no injectable server.
|
|
142
|
+
*/
|
|
143
|
+
export function registerUpdateBridge(ctx) {
|
|
144
|
+
if (typeof ctx.inject !== 'function')
|
|
145
|
+
return undefined;
|
|
146
|
+
try {
|
|
147
|
+
ctx.inject(['webServer'], (services) => {
|
|
148
|
+
const webServer = services?.webServer;
|
|
149
|
+
if (webServer === undefined || typeof webServer.register !== 'function')
|
|
150
|
+
return;
|
|
151
|
+
const mount = () => {
|
|
152
|
+
const off = webServer.register({
|
|
153
|
+
kind: 'exact',
|
|
154
|
+
path: UPDATE_BRIDGE_PATH,
|
|
155
|
+
async handler(request, response) {
|
|
156
|
+
if (request.method !== 'POST') {
|
|
157
|
+
writeJson(response, 405, { ok: false, code: 'method-not-allowed', message: 'POST only' });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (!isAllowed(request)) {
|
|
161
|
+
writeJson(response, 403, { ok: false, code: 'forbidden', message: 'loopback settings only' });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const body = await readJson(request);
|
|
165
|
+
if (body === undefined || typeof body !== 'object' || body === null || Array.isArray(body)) {
|
|
166
|
+
writeJson(response, 400, { ok: false, code: 'invalid-json', message: 'body must be a JSON object' });
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const manager = managerOf(ctx, services);
|
|
170
|
+
if (manager === undefined) {
|
|
171
|
+
writeJson(response, 503, {
|
|
172
|
+
ok: false,
|
|
173
|
+
code: 'manager-unavailable',
|
|
174
|
+
message: 'this deployment has no plugin manager; use the profile package manager instead',
|
|
175
|
+
});
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
const bundles = await manager.listBundles();
|
|
180
|
+
const own = bundles.find((bundle) => bundle.name === OWN_PACKAGE_NAME && bundle.installed !== false);
|
|
181
|
+
if (own === undefined) {
|
|
182
|
+
writeJson(response, 409, {
|
|
183
|
+
ok: false,
|
|
184
|
+
code: 'not-installed',
|
|
185
|
+
message: 'this package is not an installed profile bundle',
|
|
186
|
+
});
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (isLocalSource(own.source)) {
|
|
190
|
+
writeJson(response, 409, {
|
|
191
|
+
ok: false,
|
|
192
|
+
code: 'local-install',
|
|
193
|
+
message: 'link:/file: installs are not replaced by the button; install from npm first',
|
|
194
|
+
});
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const result = await manager.installBundle(`${OWN_PACKAGE_NAME}@latest`, { saveExact: true });
|
|
198
|
+
if (result.application === 'failed' || result.application === 'cancelled') {
|
|
199
|
+
writeJson(response, 502, {
|
|
200
|
+
ok: false,
|
|
201
|
+
code: result.application,
|
|
202
|
+
message: failureText(result.error),
|
|
203
|
+
});
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
writeJson(response, 200, {
|
|
207
|
+
ok: true,
|
|
208
|
+
changed: result.changed,
|
|
209
|
+
application: result.application,
|
|
210
|
+
version: result.version ?? null,
|
|
211
|
+
restartRequired: result.application === 'restart-required',
|
|
212
|
+
message: result.changed
|
|
213
|
+
? result.application === 'restart-required'
|
|
214
|
+
? 'update installed; restart DeepSeek Harness to load it'
|
|
215
|
+
: 'update installed'
|
|
216
|
+
: 'already at the latest version',
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
writeJson(response, 502, {
|
|
221
|
+
ok: false,
|
|
222
|
+
code: 'install-failed',
|
|
223
|
+
message: error instanceof Error ? error.message : String(error),
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
return typeof off === 'function' ? off : undefined;
|
|
229
|
+
};
|
|
230
|
+
if (typeof services.effect === 'function')
|
|
231
|
+
services.effect(mount);
|
|
232
|
+
else
|
|
233
|
+
mount();
|
|
234
|
+
});
|
|
235
|
+
return () => {
|
|
236
|
+
// The injected disposer owns the route; nothing else to release here.
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Register the engine-test endpoint.
|
|
245
|
+
* @param ctx - injectable plugin context.
|
|
246
|
+
* @param runTest - engine test runner shared with the model tool.
|
|
247
|
+
* @returns a disposer, or `undefined` when the host has no injectable server.
|
|
248
|
+
*/
|
|
249
|
+
export function registerEngineTestBridge(ctx, runTest) {
|
|
250
|
+
if (typeof ctx.inject !== 'function')
|
|
251
|
+
return undefined;
|
|
252
|
+
try {
|
|
253
|
+
ctx.inject(['webServer'], (services) => {
|
|
254
|
+
const webServer = services?.webServer;
|
|
255
|
+
if (webServer === undefined || typeof webServer.register !== 'function')
|
|
256
|
+
return;
|
|
257
|
+
const mount = () => {
|
|
258
|
+
const off = webServer.register({
|
|
259
|
+
kind: 'exact',
|
|
260
|
+
path: ENGINE_TEST_BRIDGE_PATH,
|
|
261
|
+
async handler(request, response) {
|
|
262
|
+
if (request.method !== 'POST') {
|
|
263
|
+
writeJson(response, 405, { ok: false, code: 'method-not-allowed', message: 'POST only' });
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (!isAllowed(request)) {
|
|
267
|
+
writeJson(response, 403, { ok: false, code: 'forbidden', message: 'loopback settings only' });
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const body = await readJson(request);
|
|
271
|
+
if (body === undefined) {
|
|
272
|
+
writeJson(response, 400, { ok: false, code: 'invalid-json', message: 'malformed or oversized JSON body' });
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
const parsed = parseBody(body);
|
|
277
|
+
const query = parsed.query ?? 'DeepSeek Harness';
|
|
278
|
+
const engines = parsed.engine === undefined ? HOSTS.map((host) => host.id) : [parsed.engine];
|
|
279
|
+
const result = await runEngineTestValue(runTest, engines, query);
|
|
280
|
+
writeJson(response, 200, { ok: true, value: result });
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
writeJson(response, 400, {
|
|
284
|
+
ok: false,
|
|
285
|
+
code: 'test-failed',
|
|
286
|
+
message: error instanceof Error ? error.message : String(error),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
return typeof off === 'function' ? off : undefined;
|
|
292
|
+
};
|
|
293
|
+
if (typeof services.effect === 'function')
|
|
294
|
+
services.effect(mount);
|
|
295
|
+
else
|
|
296
|
+
mount();
|
|
297
|
+
});
|
|
298
|
+
return () => {
|
|
299
|
+
// The injected disposer owns the route; nothing else to release here.
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return undefined;
|
|
304
|
+
}
|
|
305
|
+
}
|
package/lib/cache.d.ts
CHANGED
|
@@ -44,6 +44,12 @@ export interface CacheKeyParts {
|
|
|
44
44
|
engines: string[];
|
|
45
45
|
/** Active publication window; omitted and `off` share a key. */
|
|
46
46
|
timeRange?: TimeRange;
|
|
47
|
+
/** Content-filter level; omitted and `off` share a key. */
|
|
48
|
+
safeSearch?: string;
|
|
49
|
+
/** Fallback policy; omitted and the full policy share a key. */
|
|
50
|
+
fallbackOn?: readonly string[];
|
|
51
|
+
/** Domain blocklist; omitted and an empty list share a key. */
|
|
52
|
+
blockedDomains?: readonly string[];
|
|
47
53
|
}
|
|
48
54
|
/**
|
|
49
55
|
* Build the cache key for one search request.
|
|
@@ -52,10 +58,11 @@ export interface CacheKeyParts {
|
|
|
52
58
|
* `' PostgreSQL Replication '` and `'postgresql replication'` share an
|
|
53
59
|
* entry. Engine order never matters because the ids are sorted; every other
|
|
54
60
|
* field is positional, so differing `limit`, `wait`, `lead`, `timeRange` or
|
|
55
|
-
* engine set yields a different key. `timeRange`
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* different request's
|
|
61
|
+
* engine set yields a different key. `timeRange`, `safeSearch`, `fallbackOn`
|
|
62
|
+
* and `blockedDomains` are appended only when they are non-default, which keeps
|
|
63
|
+
* the key for an ordinary search unchanged. The query is last so a query
|
|
64
|
+
* containing the field separator cannot collide with a different request's
|
|
65
|
+
* prefix.
|
|
59
66
|
*
|
|
60
67
|
* @param parts - the logical request.
|
|
61
68
|
* @returns a deterministic, comparison-safe cache key.
|
package/lib/cache.js
CHANGED
|
@@ -12,10 +12,11 @@ const DEFAULT_PERSIST_THROTTLE_MS = 2_000;
|
|
|
12
12
|
* `' PostgreSQL Replication '` and `'postgresql replication'` share an
|
|
13
13
|
* entry. Engine order never matters because the ids are sorted; every other
|
|
14
14
|
* field is positional, so differing `limit`, `wait`, `lead`, `timeRange` or
|
|
15
|
-
* engine set yields a different key. `timeRange`
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* different request's
|
|
15
|
+
* engine set yields a different key. `timeRange`, `safeSearch`, `fallbackOn`
|
|
16
|
+
* and `blockedDomains` are appended only when they are non-default, which keeps
|
|
17
|
+
* the key for an ordinary search unchanged. The query is last so a query
|
|
18
|
+
* containing the field separator cannot collide with a different request's
|
|
19
|
+
* prefix.
|
|
19
20
|
*
|
|
20
21
|
* @param parts - the logical request.
|
|
21
22
|
* @returns a deterministic, comparison-safe cache key.
|
|
@@ -24,7 +25,10 @@ export function cacheKey(parts) {
|
|
|
24
25
|
const query = parts.query.trim().toLowerCase().replace(/\s+/gu, ' ');
|
|
25
26
|
const engines = [...parts.engines].sort().join('+');
|
|
26
27
|
const window = parts.timeRange === undefined || parts.timeRange === 'off' ? '' : `${parts.timeRange}|`;
|
|
27
|
-
|
|
28
|
+
const safe = parts.safeSearch === undefined || parts.safeSearch === 'off' ? '' : `safe=${parts.safeSearch}|`;
|
|
29
|
+
const fallback = parts.fallbackOn === undefined ? '' : `fallback=${[...parts.fallbackOn].sort().join(',')}|`;
|
|
30
|
+
const blocked = parts.blockedDomains === undefined ? '' : `blocked=${[...parts.blockedDomains].sort().join(',')}|`;
|
|
31
|
+
return `${parts.lead}|${parts.wait}|${parts.limit}|${engines}|${window}${safe}${fallback}${blocked}${query}`;
|
|
28
32
|
}
|
|
29
33
|
/**
|
|
30
34
|
* Copy the array and each source.
|