dsh-plugin-tool-management 0.21.6 → 0.21.8
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 +23 -0
- package/docs/UPDATE.md +2 -0
- package/lib/client.js +233 -50
- package/lib/hub-lock.js +121 -0
- package/lib/index.js +93 -8
- package/lib/mcp/state-section.js +4 -1
- package/lib/op-registry.js +2 -0
- package/lib/ops/candidates.js +5 -4
- package/lib/ops/compat.js +2 -2
- package/lib/ops/scope.js +29 -0
- package/lib/scenes/candidates.js +4 -2
- package/lib/scope/intent-store.js +30 -1
- package/lib/scope/intent.js +18 -0
- package/lib/scope/store.js +29 -1
- package/lib/update-check.js +199 -20
- package/package.json +12 -12
package/lib/update-check.js
CHANGED
|
@@ -7,7 +7,12 @@
|
|
|
7
7
|
// · 挑不出来就说 `no-compatible-version`,不给"最新版"这种没有意义的答案;
|
|
8
8
|
// · 命令固定 `dsh plugin --profile <p> add <pkg>@<version>`,**绝不出现 `@latest`**。
|
|
9
9
|
//
|
|
10
|
-
//
|
|
10
|
+
// 下载源跟宿主走,不自己再定一套:`pluginManager.registries()` 给出配置的第一源与 fallback,
|
|
11
|
+
// `pluginRegistryProbe.fastest()` 是宿主自己的双源测速;两个都取不到时退回 npm 官方单源。
|
|
12
|
+
// 这里只把宿主的顺序落成请求顺序 —— 私有源优先是用户明确配置的意图,测速结果不许把它挤后。
|
|
13
|
+
//
|
|
14
|
+
// 网络策略:只请求 npm registry(不穿透代理);首源 3 秒、回退源 4 秒——镜像的元数据可能比
|
|
15
|
+
// `/ping` 慢得多,3 秒会把能通的路判死;检测到代理环境变量直接返回 `proxy-unsupported`,
|
|
11
16
|
// 不尝试穿透(穿透失败会挂住启动流程,而这件事本来就不该阻塞任何东西)。
|
|
12
17
|
import { mkdir, readFile } from 'node:fs/promises';
|
|
13
18
|
import { join } from 'node:path';
|
|
@@ -17,17 +22,161 @@ import { writeFileAtomically } from './memories/index-io.js';
|
|
|
17
22
|
export const UPDATE_CHECK_FILE = 'update-check.json';
|
|
18
23
|
/** 本插件在 npm 上的包名 —— 查更新与拼安装命令都用它。 */
|
|
19
24
|
export const PACKAGE_NAME = 'dsh-plugin-tool-management';
|
|
20
|
-
/** npm registry
|
|
21
|
-
|
|
25
|
+
/** npm 官方 registry:没有宿主登记时的唯一来源,也是单源回退的默认值。 */
|
|
26
|
+
const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';
|
|
27
|
+
/** npm registry 的元数据地址(单源回退时使用)。 */
|
|
28
|
+
export const REGISTRY_URL = DEFAULT_REGISTRY + PACKAGE_NAME;
|
|
29
|
+
/** 公网源集合:宿主测速只覆盖这两个,只有它们能被测速结果提到最前。 */
|
|
30
|
+
const PUBLIC_REGISTRIES = new Set([DEFAULT_REGISTRY, 'https://registry.npmmirror.com/']);
|
|
31
|
+
/**
|
|
32
|
+
* registry 元数据的响应体上限(8 MiB)。
|
|
33
|
+
*
|
|
34
|
+
* 本包的 install-v1 元数据只有几十 KiB;上限只用来挡住恶意或误配的源在超时前推来
|
|
35
|
+
* 数百 MB —— `res.json()` 本身不设限,宿主与插件同进程,内存峰值会直接拖垮宿主。
|
|
36
|
+
*/
|
|
37
|
+
const MAX_REGISTRY_BYTES = 8 * 1024 * 1024;
|
|
22
38
|
/** 代理环境变量:命中任意一个就当作"网络路径不确定",不尝试请求。 */
|
|
23
39
|
export function proxyDetected(env) {
|
|
24
40
|
return ['HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy']
|
|
25
41
|
.some((key) => String(env[key] || '').trim() !== '');
|
|
26
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* 规范成与 pnpm 比较时同样的形状:http(s)、主机小写、路径以 `/` 结尾。
|
|
45
|
+
*
|
|
46
|
+
* 保留路径与端口 —— 私有源常挂在反向代理的子路径下(`/artifactory/api/npm/`),
|
|
47
|
+
* 砍掉就指到别处去了。
|
|
48
|
+
*
|
|
49
|
+
* @param raw 配置里的原始地址
|
|
50
|
+
* @returns 规范化后的地址;不是合法 http(s) URL 时 null
|
|
51
|
+
*/
|
|
52
|
+
function normalizeRegistry(raw) {
|
|
53
|
+
const text = String(raw ?? '').trim();
|
|
54
|
+
if (text === '')
|
|
55
|
+
return null;
|
|
56
|
+
try {
|
|
57
|
+
const url = new URL(text);
|
|
58
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:')
|
|
59
|
+
return null;
|
|
60
|
+
if (!url.pathname.endsWith('/'))
|
|
61
|
+
url.pathname += '/';
|
|
62
|
+
url.search = '';
|
|
63
|
+
url.hash = '';
|
|
64
|
+
// 凭据走 ~/.npmrc(宿主安装器的口径),不跟着地址进缓存与界面。
|
|
65
|
+
url.username = '';
|
|
66
|
+
url.password = '';
|
|
67
|
+
return url.toString();
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 把宿主的下载源登记整理成按序尝试的列表。
|
|
75
|
+
*
|
|
76
|
+
* 顺序就是宿主安装器自己的顺序:配置的第一源 → fallback 列表 →(配置为空时)pnpm 解析到的源。
|
|
77
|
+
* 测速结果只在**首项本来就是公网源**时提前:首项是私有源时保持用户配置的顺序,
|
|
78
|
+
* 一次公网测速不许把私有源挤到后面。
|
|
79
|
+
*
|
|
80
|
+
* @param configured 宿主登记;null(取不到)→ 返回空数组,由调用方走 npm 官方单源
|
|
81
|
+
* @param fastest 测速命中的源;null = 没测或都不可达
|
|
82
|
+
* @returns 规范化、去重后的源列表;可能为空
|
|
83
|
+
*/
|
|
84
|
+
export function sourcePlan(configured, fastest) {
|
|
85
|
+
if (!configured)
|
|
86
|
+
return [];
|
|
87
|
+
const first = normalizeRegistry(configured.registry) ?? normalizeRegistry(configured.resolved);
|
|
88
|
+
const plan = [];
|
|
89
|
+
for (const raw of [first, ...configured.fallbackRegistries.map(normalizeRegistry)]) {
|
|
90
|
+
if (raw !== null && !plan.includes(raw))
|
|
91
|
+
plan.push(raw);
|
|
92
|
+
}
|
|
93
|
+
const probed = normalizeRegistry(fastest);
|
|
94
|
+
const head = plan[0];
|
|
95
|
+
if (probed !== null && plan.includes(probed) && (head === undefined || PUBLIC_REGISTRIES.has(head))) {
|
|
96
|
+
return [probed, ...plan.filter((registry) => registry !== probed)];
|
|
97
|
+
}
|
|
98
|
+
return plan;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 限时等待外部答案;超时或抛错都得到 null。
|
|
102
|
+
*
|
|
103
|
+
* 超时的那次调用没法取消(宿主的 pnpm 子进程照跑),但这里不等它 ——
|
|
104
|
+
* 更新检查不该被一次 pnpm 慢启动拖住。
|
|
105
|
+
*/
|
|
106
|
+
async function settleWithin(run, ms) {
|
|
107
|
+
if (!run)
|
|
108
|
+
return null;
|
|
109
|
+
let timer;
|
|
110
|
+
try {
|
|
111
|
+
return await Promise.race([
|
|
112
|
+
run().catch(() => null),
|
|
113
|
+
new Promise((resolvePromise) => { timer = setTimeout(() => resolvePromise(null), ms); }),
|
|
114
|
+
]);
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
if (timer !== undefined)
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 读一份 registry 元数据,流式累计并限制响应体大小。
|
|
123
|
+
*
|
|
124
|
+
* @param fetchImpl 注入的 fetch(便于测试)
|
|
125
|
+
* @param url 完整元数据地址
|
|
126
|
+
* @param timeoutMs 本次请求的超时(与调用方的首源 / 回退源预算一致)
|
|
127
|
+
* @returns 解析后的元数据
|
|
128
|
+
* @throws 非 2xx、响应体超过 `MAX_REGISTRY_BYTES`、超时或 JSON 不合法
|
|
129
|
+
*/
|
|
130
|
+
async function readRegistryMeta(fetchImpl, url, timeoutMs) {
|
|
131
|
+
const res = await fetchImpl(url, {
|
|
132
|
+
headers: { accept: 'application/vnd.npm.install-v1+json' },
|
|
133
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
134
|
+
});
|
|
135
|
+
if (!res.ok)
|
|
136
|
+
throw new Error('registry HTTP ' + res.status);
|
|
137
|
+
const declared = Number(res.headers.get('content-length'));
|
|
138
|
+
if (Number.isFinite(declared) && declared > MAX_REGISTRY_BYTES)
|
|
139
|
+
throw new Error('registry response too large');
|
|
140
|
+
const body = res.body;
|
|
141
|
+
let text;
|
|
142
|
+
if (body && typeof body.getReader === 'function') {
|
|
143
|
+
const reader = body.getReader();
|
|
144
|
+
const chunks = [];
|
|
145
|
+
let total = 0;
|
|
146
|
+
try {
|
|
147
|
+
for (;;) {
|
|
148
|
+
const { done, value } = await reader.read();
|
|
149
|
+
if (done)
|
|
150
|
+
break;
|
|
151
|
+
if (!value)
|
|
152
|
+
continue;
|
|
153
|
+
total += value.byteLength;
|
|
154
|
+
if (total > MAX_REGISTRY_BYTES)
|
|
155
|
+
throw new Error('registry response too large');
|
|
156
|
+
chunks.push(value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
try {
|
|
161
|
+
await reader.cancel?.();
|
|
162
|
+
}
|
|
163
|
+
catch { /* 已经读完或已中止 */ }
|
|
164
|
+
}
|
|
165
|
+
text = Buffer.concat(chunks.map((chunk) => Buffer.from(chunk)), total).toString('utf8');
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
// 没有流实现的 fetch(测试替身 / 老运行时):只能读完再校验,至少不把超大文本交给 JSON.parse。
|
|
169
|
+
text = await res.text();
|
|
170
|
+
if (Buffer.byteLength(text, 'utf8') > MAX_REGISTRY_BYTES)
|
|
171
|
+
throw new Error('registry response too large');
|
|
172
|
+
}
|
|
173
|
+
return JSON.parse(text);
|
|
174
|
+
}
|
|
27
175
|
/**
|
|
28
176
|
* 从 registry 的 `versions` 里挑与 `host` 兼容的最高版本。
|
|
29
177
|
*
|
|
30
178
|
* 兼容判据是**每个版本自己的** `engines.dsh`:没有该字段、不是合法 range、或不满足 → 跳过。
|
|
179
|
+
* 被维护者标记 `deprecated` 的版本也跳过 —— 它可能仍满足 range,但正是维护者明确不建议装的。
|
|
31
180
|
* `host` 为空 / 非法 → `null`(调用方报 `host-unknown`)。
|
|
32
181
|
*/
|
|
33
182
|
export function pickBestCompatible(versions, host) {
|
|
@@ -40,6 +189,9 @@ export function pickBestCompatible(versions, host) {
|
|
|
40
189
|
for (const { version, meta } of list) {
|
|
41
190
|
if (!semver.valid(version))
|
|
42
191
|
continue;
|
|
192
|
+
const deprecated = meta && meta.deprecated;
|
|
193
|
+
if (deprecated !== undefined && deprecated !== null && deprecated !== false && deprecated !== '')
|
|
194
|
+
continue;
|
|
43
195
|
const range = meta && meta.engines && typeof meta.engines.dsh === 'string' ? meta.engines.dsh.trim() : '';
|
|
44
196
|
if (range === '')
|
|
45
197
|
continue;
|
|
@@ -86,11 +238,14 @@ export function installCommand(profile, version) {
|
|
|
86
238
|
return 'dsh plugin --profile ' + (profile && profile.trim() !== '' ? profile.trim() : '<profile>') +
|
|
87
239
|
' add ' + PACKAGE_NAME + '@' + version;
|
|
88
240
|
}
|
|
89
|
-
/** 更新检查器:按 TTL
|
|
241
|
+
/** 更新检查器:按 TTL 缓存结果,源跟随宿主登记,检测到代理直接跳过,绝不自动更新。 */
|
|
90
242
|
export function createUpdateChecker(options) {
|
|
91
243
|
const ttlMs = options.ttlMs ?? 12 * 60 * 60 * 1000;
|
|
92
244
|
const failureTtlMs = options.failureTtlMs ?? 60 * 60 * 1000;
|
|
93
245
|
const timeoutMs = options.timeoutMs ?? 3000;
|
|
246
|
+
const fallbackTimeoutMs = options.fallbackTimeoutMs ?? 4000;
|
|
247
|
+
const planTimeoutMs = options.planTimeoutMs ?? 2500;
|
|
248
|
+
const probeTimeoutMs = options.probeTimeoutMs ?? 2000;
|
|
94
249
|
const env = options.env ?? process.env;
|
|
95
250
|
const now = options.now ?? Date.now;
|
|
96
251
|
const cacheFile = join(options.stateDir, UPDATE_CHECK_FILE);
|
|
@@ -110,12 +265,14 @@ export function createUpdateChecker(options) {
|
|
|
110
265
|
host,
|
|
111
266
|
newest: null,
|
|
112
267
|
bestCompatible: null,
|
|
268
|
+
source: null,
|
|
113
269
|
command: null,
|
|
114
270
|
};
|
|
115
271
|
};
|
|
116
272
|
const finish = (status, extra = {}) => {
|
|
117
273
|
const result = {
|
|
118
|
-
|
|
274
|
+
// `host-unknown` 是"这一拍读不到宿主版本",不是结论:按失败 TTL 缓存,等宿主就绪后重试。
|
|
275
|
+
ok: status !== 'unknown' && status !== 'host-unknown',
|
|
119
276
|
...base(),
|
|
120
277
|
status,
|
|
121
278
|
checkedAt: new Date(now()).toISOString(),
|
|
@@ -145,7 +302,13 @@ export function createUpdateChecker(options) {
|
|
|
145
302
|
const ttl = raw.ok === false ? failureTtlMs : ttlMs;
|
|
146
303
|
if (age > ttl)
|
|
147
304
|
return null;
|
|
148
|
-
|
|
305
|
+
// 旧缓存没有 `source`:按 null 处理,装的时候不指定源即可,不算无效缓存。
|
|
306
|
+
const result = { ...raw, fromCache: true, source: typeof raw.source === 'string' ? raw.source : null };
|
|
307
|
+
// 命令按当前 profile 重建:启动早期的检查可能还没有 profile 名,缓存里的 `<profile>` 不能一直留着。
|
|
308
|
+
if (result.bestCompatible) {
|
|
309
|
+
const profile = typeof options.profile === 'function' ? options.profile() : options.profile;
|
|
310
|
+
result.command = installCommand(profile, result.bestCompatible);
|
|
311
|
+
}
|
|
149
312
|
last = result;
|
|
150
313
|
return result;
|
|
151
314
|
}
|
|
@@ -162,6 +325,18 @@ export function createUpdateChecker(options) {
|
|
|
162
325
|
// 缓存写不进去不影响结论:下一次重新请求即可。
|
|
163
326
|
}
|
|
164
327
|
};
|
|
328
|
+
/** 宿主登记 + 测速 → 本次按序尝试的源;登记取不到时返回空数组,调用方走默认单源。 */
|
|
329
|
+
const resolveSources = async () => {
|
|
330
|
+
const configured = await settleWithin(options.registries, planTimeoutMs);
|
|
331
|
+
if (!configured)
|
|
332
|
+
return [];
|
|
333
|
+
const head = sourcePlan(configured, null)[0];
|
|
334
|
+
// 首项已是私有源时不测速:测速只覆盖两个公网源,把结果提到私有源前面是错的。
|
|
335
|
+
const probed = head === undefined || PUBLIC_REGISTRIES.has(head)
|
|
336
|
+
? await settleWithin(options.fastest, probeTimeoutMs)
|
|
337
|
+
: null;
|
|
338
|
+
return sourcePlan(configured, probed);
|
|
339
|
+
};
|
|
165
340
|
const run = async () => {
|
|
166
341
|
const b = base();
|
|
167
342
|
if (proxyDetected(env))
|
|
@@ -171,20 +346,24 @@ export function createUpdateChecker(options) {
|
|
|
171
346
|
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
172
347
|
if (typeof fetchImpl !== 'function')
|
|
173
348
|
return finish('unknown', { ok: false, error: 'fetch unavailable' });
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
349
|
+
const plan = await resolveSources();
|
|
350
|
+
// 最多试两个:首项(通常已经过测速)失败时用回退源兜一次,再多只是把等待拖长。
|
|
351
|
+
const sources = plan.length ? plan.slice(0, 2) : [DEFAULT_REGISTRY];
|
|
352
|
+
let meta = null;
|
|
353
|
+
let source = null;
|
|
354
|
+
let lastError = '';
|
|
355
|
+
for (const [index, registry] of sources.entries()) {
|
|
356
|
+
try {
|
|
357
|
+
meta = await readRegistryMeta(fetchImpl, registry + PACKAGE_NAME, index === 0 ? timeoutMs : fallbackTimeoutMs);
|
|
358
|
+
source = registry;
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
catch (e) {
|
|
362
|
+
lastError = e instanceof Error ? e.message : String(e);
|
|
363
|
+
}
|
|
187
364
|
}
|
|
365
|
+
if (!meta)
|
|
366
|
+
return finish('unknown', { ok: false, error: lastError || 'registry unreachable' });
|
|
188
367
|
const versions = meta && meta.versions && typeof meta.versions === 'object' ? meta.versions : {};
|
|
189
368
|
const all = Object.keys(versions).filter((v) => semver.valid(v));
|
|
190
369
|
const newest = all.length ? semver.rsort(all)[0] : (meta && meta['dist-tags'] && meta['dist-tags'].latest) || null;
|
|
@@ -195,7 +374,7 @@ export function createUpdateChecker(options) {
|
|
|
195
374
|
bestCompatible,
|
|
196
375
|
currentEngines: options.currentEngines ?? null,
|
|
197
376
|
});
|
|
198
|
-
return finish(status, { newest, bestCompatible });
|
|
377
|
+
return finish(status, { newest, bestCompatible, source });
|
|
199
378
|
};
|
|
200
379
|
const check = async (force) => {
|
|
201
380
|
if (!force) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-tool-management",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.8",
|
|
4
4
|
"description": "一个设置面板,统一管理 MCP、技能、记忆、子智能体、提示词和归档会话;可自定义搭配场景,一个场景即可切换整个环境;Agent也能帮你操作这个插件。",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -96,17 +96,17 @@
|
|
|
96
96
|
},
|
|
97
97
|
"devDependencies": {
|
|
98
98
|
"@deepseek-ai/cordis": "^4.0.5",
|
|
99
|
-
"@deepseek-ai/dsh-agent-preset-registry": "^0.2.1-rc.
|
|
100
|
-
"@deepseek-ai/dsh-llm": "^0.2.1-rc.
|
|
101
|
-
"@deepseek-ai/dsh-session": "^0.2.1-rc.
|
|
102
|
-
"@deepseek-ai/dsh-session-projection-cache": "^0.2.1-rc.
|
|
103
|
-
"@deepseek-ai/dsh-settings": "^0.2.1-rc.
|
|
104
|
-
"@deepseek-ai/dsh-spill-local": "^0.2.1-rc.
|
|
105
|
-
"@deepseek-ai/dsh-storage-domain": "^0.2.1-rc.
|
|
106
|
-
"@deepseek-ai/dsh-subagent-fork-in-process": "^0.2.1-rc.
|
|
107
|
-
"@deepseek-ai/dsh-subagent-spawn-in-process": "^0.2.1-rc.
|
|
108
|
-
"@deepseek-ai/dsh-tools": "^0.2.1-rc.
|
|
109
|
-
"@deepseek-ai/dsh-workspace": "^0.2.1-rc.
|
|
99
|
+
"@deepseek-ai/dsh-agent-preset-registry": "^0.2.1-rc.3",
|
|
100
|
+
"@deepseek-ai/dsh-llm": "^0.2.1-rc.3",
|
|
101
|
+
"@deepseek-ai/dsh-session": "^0.2.1-rc.3",
|
|
102
|
+
"@deepseek-ai/dsh-session-projection-cache": "^0.2.1-rc.3",
|
|
103
|
+
"@deepseek-ai/dsh-settings": "^0.2.1-rc.3",
|
|
104
|
+
"@deepseek-ai/dsh-spill-local": "^0.2.1-rc.3",
|
|
105
|
+
"@deepseek-ai/dsh-storage-domain": "^0.2.1-rc.3",
|
|
106
|
+
"@deepseek-ai/dsh-subagent-fork-in-process": "^0.2.1-rc.3",
|
|
107
|
+
"@deepseek-ai/dsh-subagent-spawn-in-process": "^0.2.1-rc.3",
|
|
108
|
+
"@deepseek-ai/dsh-tools": "^0.2.1-rc.3",
|
|
109
|
+
"@deepseek-ai/dsh-workspace": "^0.2.1-rc.3",
|
|
110
110
|
"@types/js-yaml": "^4.0.9",
|
|
111
111
|
"@types/node": "^24.0.0",
|
|
112
112
|
"@types/semver": "^7.7.0",
|