dsh-vscode-mode 0.1.15 → 0.1.16
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 +13 -3
- package/lib/client.js +140 -33
- package/lib/client.js.map +1 -1
- package/lib/index.js +386 -198
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client/compat.ts +91 -0
- package/src/client/fileOpeners.ts +2 -1
- package/src/client/index.ts +18 -10
- package/src/client/openPathRouter.ts +5 -5
- package/src/client/settingsContext.ts +7 -17
- package/src/client/styles/mcp.css +8 -0
- package/src/client/ui/McpSettings.ts +36 -1
- package/src/compat.ts +146 -0
- package/src/fileOpenSettings.ts +58 -6
- package/src/index.ts +33 -5
- package/src/mcp.ts +8 -9
- package/src/mcpIsolation.ts +6 -12
- package/src/routes.ts +26 -7
- package/src/rpc.ts +2 -0
- package/src/shared/compat.ts +20 -0
- package/src/shared/rpc.ts +3 -0
package/lib/index.js
CHANGED
|
@@ -2,10 +2,335 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import { readFile } from "node:fs/promises";
|
|
3
3
|
import { dirname, extname, join, normalize, resolve, sep } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { existsSync } from "node:fs";
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
6
|
import { createHash } from "node:crypto";
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
//#region src/mcp.ts
|
|
8
|
+
/**
|
|
9
|
+
* dsh-vscode-mode host — MCP 运行时管理适配层。
|
|
10
|
+
* 只操作已装配的 @deepseek-ai/dsh-mcp-client loader entry,不在浏览器侧连接 MCP。
|
|
11
|
+
* 身份常量(包名/条目前缀/新旧格式判定)统一由 compat 层持有,本模块再导出保持既有 import 面。
|
|
12
|
+
* 作者 ddj 2026年08月22号
|
|
13
|
+
*/
|
|
14
|
+
/** 列出当前 loader 中的 MCP entry。 */
|
|
15
|
+
function entriesOf(ctx) {
|
|
16
|
+
const loader = ctx.get("loader");
|
|
17
|
+
if (!loader || typeof loader.entries !== "function") return [];
|
|
18
|
+
return [...loader.entries()].filter((entry) => entry?.options?.name === MCP_PACKAGE);
|
|
19
|
+
}
|
|
20
|
+
/** 脱敏配置并保留可编辑字段的存在性。 */
|
|
21
|
+
function publicConfig(config) {
|
|
22
|
+
const out = { ...config };
|
|
23
|
+
if (out.env && typeof out.env === "object") out.env = Object.fromEntries(Object.keys(out.env).map((key) => [key, "••••••"]));
|
|
24
|
+
if (out.headers && typeof out.headers === "object") out.headers = Object.fromEntries(Object.keys(out.headers).map((key) => [key, "••••••"]));
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
/** 从工具注册表读取服务器命名空间下的工具摘要。 */
|
|
28
|
+
function toolsOf(ctx, serverName) {
|
|
29
|
+
const visible = (ctx.get("tools")?.view?.(void 0))?.visible;
|
|
30
|
+
if (!(visible instanceof Map)) return [];
|
|
31
|
+
const prefix = `mcp__${serverName}__`;
|
|
32
|
+
return [...visible.entries()].filter(([name]) => String(name).startsWith(prefix)).map(([name, definition]) => ({
|
|
33
|
+
name: String(name).slice(prefix.length),
|
|
34
|
+
description: typeof definition?.description === "string" ? definition.description : void 0
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
/** 将 loader entry 映射为设置页摘要。 */
|
|
38
|
+
function serverOf(ctx, entry) {
|
|
39
|
+
const config = entry.options?.config ?? {};
|
|
40
|
+
const serverName = typeof config.serverName === "string" ? config.serverName : entry.options?.id ?? "unknown";
|
|
41
|
+
const tools = toolsOf(ctx, serverName);
|
|
42
|
+
const state = entry.fiber?.state;
|
|
43
|
+
return {
|
|
44
|
+
id: String(entry.id ?? entry.options?.id ?? serverName),
|
|
45
|
+
serverName,
|
|
46
|
+
enabled: entry.disabled !== true,
|
|
47
|
+
transport: config.transport === "streamable-http" ? "streamable-http" : "stdio",
|
|
48
|
+
config: publicConfig(config),
|
|
49
|
+
status: state === 2 ? "connected" : state === 1 ? "connecting" : "error",
|
|
50
|
+
toolCount: tools.length,
|
|
51
|
+
tools,
|
|
52
|
+
error: state === 3 ? "MCP 插件未正常运行" : void 0
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** 校验 MCP 配置。 */
|
|
56
|
+
function validateConfig(config) {
|
|
57
|
+
if (!/^[A-Za-z0-9_-]{1,32}$/.test(config.serverName)) throw new Error("serverName 只能包含字母、数字、下划线和连字符(最多 32 位)");
|
|
58
|
+
if (config.transport === "stdio" && !config.command?.trim()) throw new Error("stdio MCP 必须填写 command");
|
|
59
|
+
if (config.transport === "streamable-http" && !/^https?:\/\//.test(config.url ?? "")) throw new Error("HTTP MCP 必须填写 http(s) URL");
|
|
60
|
+
}
|
|
61
|
+
/** 列出全局(profile)MCP 服务,过滤掉新旧格式的项目级条目。 */
|
|
62
|
+
function listMcp(ctx) {
|
|
63
|
+
return { servers: entriesOf(ctx).filter((entry) => !isProjectEntryId(String(entry.id ?? entry.options?.id ?? ""))).map((entry) => serverOf(ctx, entry)) };
|
|
64
|
+
}
|
|
65
|
+
/** 保存 MCP 服务配置并触发 loader 热更新。 */
|
|
66
|
+
async function saveMcp(ctx, config) {
|
|
67
|
+
validateConfig(config);
|
|
68
|
+
const loader = ctx.get("loader");
|
|
69
|
+
if (!loader) throw new Error("缺少 loader 服务");
|
|
70
|
+
const existing = entriesOf(ctx).find((entry) => !isProjectEntryId(String(entry.id ?? entry.options?.id ?? "")) && entry.options?.config?.serverName === config.serverName);
|
|
71
|
+
if (existing) {
|
|
72
|
+
await loader.update(existing.id, { config: { ...config } });
|
|
73
|
+
return serverOf(ctx, loader.resolve(existing.id));
|
|
74
|
+
}
|
|
75
|
+
const id = await loader.create({
|
|
76
|
+
name: MCP_PACKAGE,
|
|
77
|
+
config: { ...config }
|
|
78
|
+
});
|
|
79
|
+
return serverOf(ctx, loader.resolve(id));
|
|
80
|
+
}
|
|
81
|
+
/** 删除 MCP 服务。 */
|
|
82
|
+
async function removeMcp(ctx, id) {
|
|
83
|
+
const loader = ctx.get("loader");
|
|
84
|
+
if (!loader) throw new Error("缺少 loader 服务");
|
|
85
|
+
const entry = entriesOf(ctx).find((candidate) => String(candidate.id) === id);
|
|
86
|
+
if (!entry) throw new Error("MCP 服务不存在");
|
|
87
|
+
await loader.remove(entry.id);
|
|
88
|
+
}
|
|
89
|
+
/** 切换 MCP 服务启用状态。 */
|
|
90
|
+
async function toggleMcp(ctx, id, enabled) {
|
|
91
|
+
const loader = ctx.get("loader");
|
|
92
|
+
if (!loader) throw new Error("缺少 loader 服务");
|
|
93
|
+
const entry = entriesOf(ctx).find((candidate) => String(candidate.id) === id);
|
|
94
|
+
if (!entry) throw new Error("MCP 服务不存在");
|
|
95
|
+
await loader.update(entry.id, { disabled: !enabled });
|
|
96
|
+
return serverOf(ctx, loader.resolve(entry.id));
|
|
97
|
+
}
|
|
98
|
+
/** 重新加载 MCP 服务。 */
|
|
99
|
+
async function refreshMcp(ctx, id) {
|
|
100
|
+
const loader = ctx.get("loader");
|
|
101
|
+
if (!loader) throw new Error("缺少 loader 服务");
|
|
102
|
+
const entry = entriesOf(ctx).find((candidate) => String(candidate.id) === id);
|
|
103
|
+
if (!entry) throw new Error("MCP 服务不存在");
|
|
104
|
+
await loader.update(entry.id, { config: { ...entry.options?.config ?? {} } });
|
|
105
|
+
return serverOf(ctx, loader.resolve(entry.id));
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/fileOpenSettings.ts
|
|
109
|
+
const FILE_OPEN_SETTINGS_NS = "dsh-vscode-mode";
|
|
110
|
+
const FILE_OPEN_DEFAULT = "auto";
|
|
111
|
+
let depsPromise;
|
|
112
|
+
/**
|
|
113
|
+
* 动态加载设置依赖(模块级缓存;任一缺失返回 null 而非抛错)。
|
|
114
|
+
* @author ddj 2026年08月24号
|
|
115
|
+
* @returns 设置依赖或 null
|
|
116
|
+
*/
|
|
117
|
+
function loadSettingsDeps() {
|
|
118
|
+
if (!depsPromise) depsPromise = Promise.all([import("@deepseek-ai/dsh-settings"), import("schemastery")]).then(([dshSettings, schemastery]) => ({
|
|
119
|
+
installSettingsSection: dshSettings.installSettingsSection,
|
|
120
|
+
z: schemastery.default
|
|
121
|
+
})).catch(() => null);
|
|
122
|
+
return depsPromise;
|
|
123
|
+
}
|
|
124
|
+
function normalizeValue(value) {
|
|
125
|
+
if (typeof value !== "string" || value.trim() === "") return FILE_OPEN_DEFAULT;
|
|
126
|
+
return value.trim();
|
|
127
|
+
}
|
|
128
|
+
function configValue(config) {
|
|
129
|
+
return normalizeValue(config?.fileOpenTool);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 安装设置 section(动态依赖;缺失返回 false 不抛错)。
|
|
133
|
+
* @author ddj 2026年08月24号
|
|
134
|
+
* @param ctx DSH host 上下文
|
|
135
|
+
* @param ns 设置命名空间
|
|
136
|
+
* @param entry 初始设置值
|
|
137
|
+
* @param hooks 设置源绑定与变更回调
|
|
138
|
+
* @param loader 依赖装载器(测试注入)
|
|
139
|
+
* @returns 是否成功安装
|
|
140
|
+
*/
|
|
141
|
+
async function installOpenSettingsSection(ctx, ns, entry, hooks, loader = loadSettingsDeps) {
|
|
142
|
+
const deps = await loader();
|
|
143
|
+
if (!deps) return false;
|
|
144
|
+
const schema = deps.z.object({ fileOpenTool: deps.z.string().default(FILE_OPEN_DEFAULT) });
|
|
145
|
+
deps.installSettingsSection(ctx, ns, schema, entry, hooks);
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 注册设置命名空间,并提供 host 侧的读取与更新状态。
|
|
150
|
+
* @author ddj 2026年08月24号
|
|
151
|
+
* @param ctx DSH host 上下文
|
|
152
|
+
* @param config 插件组合配置
|
|
153
|
+
* @param onChange 设置变化回调
|
|
154
|
+
* @returns 设置状态
|
|
155
|
+
*/
|
|
156
|
+
function setupOpenSettings(ctx, config, onChange) {
|
|
157
|
+
let current = configValue(config);
|
|
158
|
+
let revision;
|
|
159
|
+
let provider;
|
|
160
|
+
const notify = (value) => {
|
|
161
|
+
current = normalizeValue(value);
|
|
162
|
+
onChange(current);
|
|
163
|
+
};
|
|
164
|
+
const syncRevision = () => {
|
|
165
|
+
revision = (provider?.describe?.({ redactSecrets: true })?.find((item) => item.ns === FILE_OPEN_SETTINGS_NS))?.revision;
|
|
166
|
+
};
|
|
167
|
+
const setSource = (source) => notify(source().fileOpenTool);
|
|
168
|
+
const settingsChange = () => syncRevision();
|
|
169
|
+
installOpenSettingsSection(ctx, FILE_OPEN_SETTINGS_NS, { fileOpenTool: current }, {
|
|
170
|
+
setSource,
|
|
171
|
+
onChange: settingsChange
|
|
172
|
+
});
|
|
173
|
+
ctx.inject?.(["settings"], (settingsCtx) => {
|
|
174
|
+
provider = settingsCtx.get("settings");
|
|
175
|
+
syncRevision();
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
get value() {
|
|
179
|
+
return current;
|
|
180
|
+
},
|
|
181
|
+
get revision() {
|
|
182
|
+
return revision;
|
|
183
|
+
},
|
|
184
|
+
update: async (value, expectedRevision) => {
|
|
185
|
+
const next = normalizeValue(value);
|
|
186
|
+
if (!provider?.update) {
|
|
187
|
+
notify(next);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
await provider.update(FILE_OPEN_SETTINGS_NS, { fileOpenTool: next }, expectedRevision);
|
|
191
|
+
const stored = (provider.describe?.({ redactSecrets: true })?.find((item) => item.ns === FILE_OPEN_SETTINGS_NS))?.value;
|
|
192
|
+
notify(stored?.fileOpenTool ?? next);
|
|
193
|
+
syncRevision();
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function normalizeFileOpenTool(value) {
|
|
198
|
+
return normalizeValue(value);
|
|
199
|
+
}
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region src/compat.ts
|
|
202
|
+
/**
|
|
203
|
+
* dsh-vscode-mode host — 兼容层:与其他插件 / DSH 版本的统一探测、适配、护栏与自诊断。
|
|
204
|
+
* 职责:身份常量(包名 / MCP 条目前缀 / 路由前缀)、外部插件探测、路由冲突与重复装配检测、
|
|
205
|
+
* 兼容性报告构建(RPC edrv.compat 与启动日志共用)。
|
|
206
|
+
* 注意:与 mcp.ts 存在模块环(compat 提供身份常量、mcp 提供 entriesOf),
|
|
207
|
+
* 双方仅在函数体内交叉使用导出,模块求值期无依赖,ESM 安全。
|
|
208
|
+
* 作者 ddj 2026-08-24
|
|
209
|
+
*/
|
|
210
|
+
const PLUGIN_NAME = "dsh-vscode-mode";
|
|
211
|
+
const MCP_PACKAGE = "@deepseek-ai/dsh-mcp-client";
|
|
212
|
+
/** 项目级 MCP 的 loader entry id 固定前缀(区分 profile 全局 MCP;loader id 不能用 ':',故用 '.')。 */
|
|
213
|
+
const PROJECT_ENTRY_PREFIX = "vsm-mcp.";
|
|
214
|
+
/** webServer 路由前缀(RPC 精确路由 + 静态资源前缀共用)。 */
|
|
215
|
+
const ROUTE_PREFIX = "/edrv";
|
|
216
|
+
/** 判断新旧格式项目 MCP 条目 id(旧残留不被视为全局 MCP)。 */
|
|
217
|
+
function isProjectEntryId(id) {
|
|
218
|
+
return id.startsWith("vsm-mcp.") || id.startsWith("vsm-mcp:");
|
|
219
|
+
}
|
|
220
|
+
/** 从项目条目 id 解析 workspace hash(新旧格式通用)。 */
|
|
221
|
+
function entryHash(id) {
|
|
222
|
+
if (id.startsWith("vsm-mcp.")) return id.slice(8).split(".")[0];
|
|
223
|
+
if (id.startsWith("vsm-mcp:")) return id.slice(8).split(":")[0];
|
|
224
|
+
}
|
|
225
|
+
/** 读取随包 package.json 版本号(缺失/解析失败降级为空串)。 */
|
|
226
|
+
function pluginVersionOf() {
|
|
227
|
+
try {
|
|
228
|
+
const file = join(dirname(fileURLToPath(import.meta.url)), "..", "package.json");
|
|
229
|
+
const pkg = JSON.parse(readFileSync(file, "utf8"));
|
|
230
|
+
return typeof pkg.version === "string" ? pkg.version : "";
|
|
231
|
+
} catch {
|
|
232
|
+
return "";
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/** 探测外部插件/服务适配状态(depsAvailable 由调用方注入,便于测试)。 */
|
|
236
|
+
function detectExternal(ctx, depsAvailable) {
|
|
237
|
+
const mcpCount = entriesOf(ctx).length;
|
|
238
|
+
const settings = ctx.get("settings");
|
|
239
|
+
const hasSettings = Boolean(settings?.describe || settings?.update);
|
|
240
|
+
return [
|
|
241
|
+
{
|
|
242
|
+
name: MCP_PACKAGE,
|
|
243
|
+
active: mcpCount > 0,
|
|
244
|
+
note: mcpCount > 0 ? mcpCount + " 个 MCP 服务条目" : "未检测到 MCP 条目(MCP 管理页显示为空)"
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
name: "设置持久化(@deepseek-ai/dsh-settings)",
|
|
248
|
+
active: depsAvailable,
|
|
249
|
+
note: depsAvailable ? "设置 section 已安装" : "未安装:fileOpenTool 持久化降级为配置值"
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "settings 服务",
|
|
253
|
+
active: hasSettings,
|
|
254
|
+
note: hasSettings ? "可读写设置" : "不可用(设置读写走配置回退)"
|
|
255
|
+
}
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
/** 本插件已注册的路由(kind → path 集合),冲突检测时排除自身,避免自误报。 */
|
|
259
|
+
const ownRoutes = {
|
|
260
|
+
exact: /* @__PURE__ */ new Set(),
|
|
261
|
+
prefixes: /* @__PURE__ */ new Set()
|
|
262
|
+
};
|
|
263
|
+
/** 清空本插件路由记录(registerRoutes 装配开始时调用,热重载不残留)。 */
|
|
264
|
+
function resetOwnRoutes() {
|
|
265
|
+
ownRoutes.exact.clear();
|
|
266
|
+
ownRoutes.prefixes.clear();
|
|
267
|
+
}
|
|
268
|
+
/** 记录本插件路由(routes.ts 注册成功时调用)。 */
|
|
269
|
+
function noteOwnRoute(kind, path) {
|
|
270
|
+
(kind === "exact" ? ownRoutes.exact : ownRoutes.prefixes).add(path);
|
|
271
|
+
}
|
|
272
|
+
/** 检测路由前缀占用与重叠(排除本插件自有路由;内部表不可读视为无冲突)。 */
|
|
273
|
+
function routeConflict(web, prefix) {
|
|
274
|
+
try {
|
|
275
|
+
const w = web;
|
|
276
|
+
const exact = w?.exact;
|
|
277
|
+
const prefixes = w?.prefixes;
|
|
278
|
+
if (exact instanceof Map) for (const path of exact.keys()) {
|
|
279
|
+
const other = String(path);
|
|
280
|
+
if (other.startsWith(prefix + "/") && !ownRoutes.exact.has(other)) return "精确路由 " + other + " 与 " + prefix + " 前缀重叠(可能来自热重载残留或其他插件)";
|
|
281
|
+
}
|
|
282
|
+
if (prefixes instanceof Map) for (const path of prefixes.keys()) {
|
|
283
|
+
const other = String(path);
|
|
284
|
+
if (other !== prefix && (other.startsWith(prefix + "/") || prefix.startsWith(other + "/")) && !ownRoutes.prefixes.has(other)) return "路由前缀 " + prefix + " 与 " + other + " 重叠";
|
|
285
|
+
}
|
|
286
|
+
} catch {}
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
/** 检测本插件是否被重复装配(bundle 层 + 用户层重复 insert 的自诊断)。 */
|
|
290
|
+
function duplicateEntries(ctx) {
|
|
291
|
+
const loader = ctx.get("loader");
|
|
292
|
+
if (!loader || typeof loader.entries !== "function") return [];
|
|
293
|
+
const own = [...loader.entries()].filter((entry) => String(entry.options?.name ?? entry.id ?? "") === PLUGIN_NAME);
|
|
294
|
+
if (own.length > 1) return ["检测到 " + own.length + " 个 dsh-vscode-mode loader 条目(重复装配)。请删除 profile 用户层 cordis.patch.yml 的重复 insert(或运行 dev_fix_patch 清理)"];
|
|
295
|
+
return [];
|
|
296
|
+
}
|
|
297
|
+
/** 护栏适配状态:路由前缀唯一性 + 插件装配唯一性。 */
|
|
298
|
+
function detectGuards(ctx) {
|
|
299
|
+
const web = ctx.get("webServer");
|
|
300
|
+
const conflict = web ? routeConflict(web, ROUTE_PREFIX) : null;
|
|
301
|
+
const duplicates = duplicateEntries(ctx);
|
|
302
|
+
return [{
|
|
303
|
+
name: "路由前缀 /edrv",
|
|
304
|
+
active: !conflict,
|
|
305
|
+
note: conflict ?? "无冲突"
|
|
306
|
+
}, {
|
|
307
|
+
name: "插件装配唯一性",
|
|
308
|
+
active: duplicates.length === 0,
|
|
309
|
+
note: duplicates[0] ?? "唯一装配"
|
|
310
|
+
}];
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* 构建完整兼容性报告(RPC 与启动日志共用)。
|
|
314
|
+
* @author ddj 2026年08月24号
|
|
315
|
+
* @param ctx DSH host 上下文
|
|
316
|
+
* @param options 测试注入:depsAvailable 跳过动态导入、version 固定版本号
|
|
317
|
+
* @returns 兼容性报告
|
|
318
|
+
*/
|
|
319
|
+
async function buildReport(ctx, options) {
|
|
320
|
+
const deps = options?.depsAvailable ?? await loadSettingsDeps() !== null;
|
|
321
|
+
const external = detectExternal(ctx, deps);
|
|
322
|
+
const guards = detectGuards(ctx);
|
|
323
|
+
const warnings = [];
|
|
324
|
+
if (!deps) warnings.push("未安装 @deepseek-ai/dsh-settings:文件打开工具设置持久化不可用(降级为配置值)");
|
|
325
|
+
for (const guard of guards) if (!guard.active && guard.note) warnings.push(guard.note);
|
|
326
|
+
return {
|
|
327
|
+
pluginVersion: options?.version ?? pluginVersionOf(),
|
|
328
|
+
external,
|
|
329
|
+
guards,
|
|
330
|
+
warnings
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
//#endregion
|
|
9
334
|
//#region src/shared/rpc.ts
|
|
10
335
|
/** webServer 精确路由。 */
|
|
11
336
|
const RPC_PATH = "/edrv/rpc";
|
|
@@ -50,17 +375,32 @@ function imageDirOf(config) {
|
|
|
50
375
|
/** Monaco AMD 静态目录(随包发布)。 */
|
|
51
376
|
const vendorDir = join(dirname(fileURLToPath(import.meta.url)), "..", "assets", "vendor");
|
|
52
377
|
/**
|
|
53
|
-
* 注册全部 webServer
|
|
378
|
+
* 注册全部 webServer 路由(带兼容护栏:前缀冲突预警 + 注册失败降级跳过)。
|
|
54
379
|
* @author ddj 2026年08月20号
|
|
55
380
|
* @param ctx DSH 上下文
|
|
56
381
|
* @param config 插件配置(可选 imageDir 等)
|
|
57
382
|
* @param handleRpc RPC 分发入口(method,args → RpcResult)
|
|
383
|
+
* @param onWarning 兼容性警告收集(可选)
|
|
58
384
|
*/
|
|
59
|
-
function registerRoutes(ctx, config, handleRpc) {
|
|
385
|
+
function registerRoutes(ctx, config, handleRpc, onWarning) {
|
|
60
386
|
const imgDir = imageDirOf(config);
|
|
61
387
|
const web = ctx.get("webServer");
|
|
62
388
|
if (!web) return;
|
|
63
|
-
|
|
389
|
+
resetOwnRoutes();
|
|
390
|
+
const conflict = routeConflict(web, ROUTE_PREFIX);
|
|
391
|
+
if (conflict) onWarning?.("兼容性:" + conflict);
|
|
392
|
+
/** 护栏注册:重复路由抛错时降级为警告并跳过,不让单条路由拖垮装配。 */
|
|
393
|
+
const guarded = (route, label) => {
|
|
394
|
+
try {
|
|
395
|
+
const dispose = web.register(route);
|
|
396
|
+
noteOwnRoute(route.kind, route.path);
|
|
397
|
+
return dispose;
|
|
398
|
+
} catch (error) {
|
|
399
|
+
onWarning?.("兼容性:" + label + " 注册失败(" + String(error) + ")");
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
ctx.effect(() => guarded({
|
|
64
404
|
kind: "exact",
|
|
65
405
|
path: RPC_PATH,
|
|
66
406
|
handler: async (req, res) => {
|
|
@@ -85,8 +425,8 @@ function registerRoutes(ctx, config, handleRpc) {
|
|
|
85
425
|
}));
|
|
86
426
|
}
|
|
87
427
|
}
|
|
88
|
-
}
|
|
89
|
-
for (const r of IMG_ROUTES) ctx.effect(() =>
|
|
428
|
+
}, "/edrv/rpc 精确路由"), "edrv: routes");
|
|
429
|
+
for (const r of IMG_ROUTES) ctx.effect(() => guarded({
|
|
90
430
|
kind: "exact",
|
|
91
431
|
path: r.url,
|
|
92
432
|
handler: async (req, res) => {
|
|
@@ -101,8 +441,8 @@ function registerRoutes(ctx, config, handleRpc) {
|
|
|
101
441
|
res.end();
|
|
102
442
|
}
|
|
103
443
|
}
|
|
104
|
-
}
|
|
105
|
-
ctx.effect(() =>
|
|
444
|
+
}, "静态资源 " + r.file), "edrv: routes");
|
|
445
|
+
ctx.effect(() => guarded({
|
|
106
446
|
kind: "prefix",
|
|
107
447
|
path: VENDOR_PREFIX,
|
|
108
448
|
handler: async (req, res) => {
|
|
@@ -135,7 +475,7 @@ function registerRoutes(ctx, config, handleRpc) {
|
|
|
135
475
|
res.end();
|
|
136
476
|
}
|
|
137
477
|
}
|
|
138
|
-
}
|
|
478
|
+
}, "/edrv/vendor 前缀路由"), "edrv: routes");
|
|
139
479
|
}
|
|
140
480
|
//#endregion
|
|
141
481
|
//#region src/shared/diff.ts
|
|
@@ -1646,109 +1986,6 @@ async function restoreFile(ctx, session, rec) {
|
|
|
1646
1986
|
}
|
|
1647
1987
|
}
|
|
1648
1988
|
//#endregion
|
|
1649
|
-
//#region src/mcp.ts
|
|
1650
|
-
const MCP_PACKAGE = "@deepseek-ai/dsh-mcp-client";
|
|
1651
|
-
/** 项目级 MCP 的 loader entry id 固定前缀(区分 profile 全局 MCP;注意 loader id 不能用 ':' 分隔,故用 '.')。 */
|
|
1652
|
-
const PROJECT_ENTRY_PREFIX = "vsm-mcp.";
|
|
1653
|
-
const LEGACY_PROJECT_PREFIX = "vsm-mcp:";
|
|
1654
|
-
/** 判断新旧格式项目 entry id,避免旧残留被显示为全局 MCP。 */
|
|
1655
|
-
function isProjectEntryId(id) {
|
|
1656
|
-
return id.startsWith("vsm-mcp.") || id.startsWith(LEGACY_PROJECT_PREFIX);
|
|
1657
|
-
}
|
|
1658
|
-
/** 列出当前 loader 中的 MCP entry。 */
|
|
1659
|
-
function entriesOf(ctx) {
|
|
1660
|
-
const loader = ctx.get("loader");
|
|
1661
|
-
if (!loader || typeof loader.entries !== "function") return [];
|
|
1662
|
-
return [...loader.entries()].filter((entry) => entry?.options?.name === MCP_PACKAGE);
|
|
1663
|
-
}
|
|
1664
|
-
/** 脱敏配置并保留可编辑字段的存在性。 */
|
|
1665
|
-
function publicConfig(config) {
|
|
1666
|
-
const out = { ...config };
|
|
1667
|
-
if (out.env && typeof out.env === "object") out.env = Object.fromEntries(Object.keys(out.env).map((key) => [key, "••••••"]));
|
|
1668
|
-
if (out.headers && typeof out.headers === "object") out.headers = Object.fromEntries(Object.keys(out.headers).map((key) => [key, "••••••"]));
|
|
1669
|
-
return out;
|
|
1670
|
-
}
|
|
1671
|
-
/** 从工具注册表读取服务器命名空间下的工具摘要。 */
|
|
1672
|
-
function toolsOf(ctx, serverName) {
|
|
1673
|
-
const visible = (ctx.get("tools")?.view?.(void 0))?.visible;
|
|
1674
|
-
if (!(visible instanceof Map)) return [];
|
|
1675
|
-
const prefix = `mcp__${serverName}__`;
|
|
1676
|
-
return [...visible.entries()].filter(([name]) => String(name).startsWith(prefix)).map(([name, definition]) => ({
|
|
1677
|
-
name: String(name).slice(prefix.length),
|
|
1678
|
-
description: typeof definition?.description === "string" ? definition.description : void 0
|
|
1679
|
-
}));
|
|
1680
|
-
}
|
|
1681
|
-
/** 将 loader entry 映射为设置页摘要。 */
|
|
1682
|
-
function serverOf(ctx, entry) {
|
|
1683
|
-
const config = entry.options?.config ?? {};
|
|
1684
|
-
const serverName = typeof config.serverName === "string" ? config.serverName : entry.options?.id ?? "unknown";
|
|
1685
|
-
const tools = toolsOf(ctx, serverName);
|
|
1686
|
-
const state = entry.fiber?.state;
|
|
1687
|
-
return {
|
|
1688
|
-
id: String(entry.id ?? entry.options?.id ?? serverName),
|
|
1689
|
-
serverName,
|
|
1690
|
-
enabled: entry.disabled !== true,
|
|
1691
|
-
transport: config.transport === "streamable-http" ? "streamable-http" : "stdio",
|
|
1692
|
-
config: publicConfig(config),
|
|
1693
|
-
status: state === 2 ? "connected" : state === 1 ? "connecting" : "error",
|
|
1694
|
-
toolCount: tools.length,
|
|
1695
|
-
tools,
|
|
1696
|
-
error: state === 3 ? "MCP 插件未正常运行" : void 0
|
|
1697
|
-
};
|
|
1698
|
-
}
|
|
1699
|
-
/** 校验 MCP 配置。 */
|
|
1700
|
-
function validateConfig(config) {
|
|
1701
|
-
if (!/^[A-Za-z0-9_-]{1,32}$/.test(config.serverName)) throw new Error("serverName 只能包含字母、数字、下划线和连字符(最多 32 位)");
|
|
1702
|
-
if (config.transport === "stdio" && !config.command?.trim()) throw new Error("stdio MCP 必须填写 command");
|
|
1703
|
-
if (config.transport === "streamable-http" && !/^https?:\/\//.test(config.url ?? "")) throw new Error("HTTP MCP 必须填写 http(s) URL");
|
|
1704
|
-
}
|
|
1705
|
-
/** 列出全局(profile)MCP 服务,过滤掉新旧格式的项目级条目。 */
|
|
1706
|
-
function listMcp(ctx) {
|
|
1707
|
-
return { servers: entriesOf(ctx).filter((entry) => !isProjectEntryId(String(entry.id ?? entry.options?.id ?? ""))).map((entry) => serverOf(ctx, entry)) };
|
|
1708
|
-
}
|
|
1709
|
-
/** 保存 MCP 服务配置并触发 loader 热更新。 */
|
|
1710
|
-
async function saveMcp(ctx, config) {
|
|
1711
|
-
validateConfig(config);
|
|
1712
|
-
const loader = ctx.get("loader");
|
|
1713
|
-
if (!loader) throw new Error("缺少 loader 服务");
|
|
1714
|
-
const existing = entriesOf(ctx).find((entry) => !isProjectEntryId(String(entry.id ?? entry.options?.id ?? "")) && entry.options?.config?.serverName === config.serverName);
|
|
1715
|
-
if (existing) {
|
|
1716
|
-
await loader.update(existing.id, { config: { ...config } });
|
|
1717
|
-
return serverOf(ctx, loader.resolve(existing.id));
|
|
1718
|
-
}
|
|
1719
|
-
const id = await loader.create({
|
|
1720
|
-
name: MCP_PACKAGE,
|
|
1721
|
-
config: { ...config }
|
|
1722
|
-
});
|
|
1723
|
-
return serverOf(ctx, loader.resolve(id));
|
|
1724
|
-
}
|
|
1725
|
-
/** 删除 MCP 服务。 */
|
|
1726
|
-
async function removeMcp(ctx, id) {
|
|
1727
|
-
const loader = ctx.get("loader");
|
|
1728
|
-
if (!loader) throw new Error("缺少 loader 服务");
|
|
1729
|
-
const entry = entriesOf(ctx).find((candidate) => String(candidate.id) === id);
|
|
1730
|
-
if (!entry) throw new Error("MCP 服务不存在");
|
|
1731
|
-
await loader.remove(entry.id);
|
|
1732
|
-
}
|
|
1733
|
-
/** 切换 MCP 服务启用状态。 */
|
|
1734
|
-
async function toggleMcp(ctx, id, enabled) {
|
|
1735
|
-
const loader = ctx.get("loader");
|
|
1736
|
-
if (!loader) throw new Error("缺少 loader 服务");
|
|
1737
|
-
const entry = entriesOf(ctx).find((candidate) => String(candidate.id) === id);
|
|
1738
|
-
if (!entry) throw new Error("MCP 服务不存在");
|
|
1739
|
-
await loader.update(entry.id, { disabled: !enabled });
|
|
1740
|
-
return serverOf(ctx, loader.resolve(entry.id));
|
|
1741
|
-
}
|
|
1742
|
-
/** 重新加载 MCP 服务。 */
|
|
1743
|
-
async function refreshMcp(ctx, id) {
|
|
1744
|
-
const loader = ctx.get("loader");
|
|
1745
|
-
if (!loader) throw new Error("缺少 loader 服务");
|
|
1746
|
-
const entry = entriesOf(ctx).find((candidate) => String(candidate.id) === id);
|
|
1747
|
-
if (!entry) throw new Error("MCP 服务不存在");
|
|
1748
|
-
await loader.update(entry.id, { config: { ...entry.options?.config ?? {} } });
|
|
1749
|
-
return serverOf(ctx, loader.resolve(entry.id));
|
|
1750
|
-
}
|
|
1751
|
-
//#endregion
|
|
1752
1989
|
//#region src/mcpProject.ts
|
|
1753
1990
|
/**
|
|
1754
1991
|
* dsh-vscode-mode host — 项目级 MCP 管理。
|
|
@@ -2023,74 +2260,6 @@ async function projectRefresh(ctx, workspacePath, serverName) {
|
|
|
2023
2260
|
return projectOf(ctx, workspacePath, workspace.title ?? "");
|
|
2024
2261
|
}
|
|
2025
2262
|
//#endregion
|
|
2026
|
-
//#region src/fileOpenSettings.ts
|
|
2027
|
-
/**
|
|
2028
|
-
* dsh-vscode-mode host — 文件链接打开工具的持久化设置。
|
|
2029
|
-
* @author ddj 2026年08月24号
|
|
2030
|
-
*/
|
|
2031
|
-
const FILE_OPEN_SETTINGS_NS = settingsNamespace("dsh-vscode-mode");
|
|
2032
|
-
const FILE_OPEN_DEFAULT = "auto";
|
|
2033
|
-
const FILE_OPEN_SCHEMA = z.object({ fileOpenTool: z.string().default(FILE_OPEN_DEFAULT) });
|
|
2034
|
-
function normalizeValue(value) {
|
|
2035
|
-
if (typeof value !== "string" || value.trim() === "") return FILE_OPEN_DEFAULT;
|
|
2036
|
-
return value.trim();
|
|
2037
|
-
}
|
|
2038
|
-
function configValue(config) {
|
|
2039
|
-
return normalizeValue(config?.fileOpenTool);
|
|
2040
|
-
}
|
|
2041
|
-
/**
|
|
2042
|
-
* 注册设置命名空间,并提供 host 侧的读取与更新状态。
|
|
2043
|
-
* @author ddj 2026年08月24号
|
|
2044
|
-
* @param ctx DSH host 上下文
|
|
2045
|
-
* @param config 插件组合配置
|
|
2046
|
-
* @param onChange 设置变化回调
|
|
2047
|
-
* @returns 设置状态
|
|
2048
|
-
*/
|
|
2049
|
-
function setupOpenSettings(ctx, config, onChange) {
|
|
2050
|
-
let current = configValue(config);
|
|
2051
|
-
let revision;
|
|
2052
|
-
let provider;
|
|
2053
|
-
const notify = (value) => {
|
|
2054
|
-
current = normalizeValue(value);
|
|
2055
|
-
onChange(current);
|
|
2056
|
-
};
|
|
2057
|
-
const syncRevision = () => {
|
|
2058
|
-
revision = (provider?.describe?.({ redactSecrets: true })?.find((item) => item.ns === FILE_OPEN_SETTINGS_NS))?.revision;
|
|
2059
|
-
};
|
|
2060
|
-
const setSource = (source) => notify(source().fileOpenTool);
|
|
2061
|
-
const settingsChange = () => syncRevision();
|
|
2062
|
-
installSettingsSection(ctx, FILE_OPEN_SETTINGS_NS, FILE_OPEN_SCHEMA, { fileOpenTool: current }, {
|
|
2063
|
-
setSource,
|
|
2064
|
-
onChange: settingsChange
|
|
2065
|
-
});
|
|
2066
|
-
ctx.inject?.(["settings"], (settingsCtx) => {
|
|
2067
|
-
provider = settingsCtx.get("settings");
|
|
2068
|
-
syncRevision();
|
|
2069
|
-
});
|
|
2070
|
-
return {
|
|
2071
|
-
get value() {
|
|
2072
|
-
return current;
|
|
2073
|
-
},
|
|
2074
|
-
get revision() {
|
|
2075
|
-
return revision;
|
|
2076
|
-
},
|
|
2077
|
-
update: async (value, expectedRevision) => {
|
|
2078
|
-
const next = normalizeValue(value);
|
|
2079
|
-
if (!provider?.update) {
|
|
2080
|
-
notify(next);
|
|
2081
|
-
return;
|
|
2082
|
-
}
|
|
2083
|
-
await provider.update(FILE_OPEN_SETTINGS_NS, { fileOpenTool: next }, expectedRevision);
|
|
2084
|
-
const stored = (provider.describe?.({ redactSecrets: true })?.find((item) => item.ns === FILE_OPEN_SETTINGS_NS))?.value;
|
|
2085
|
-
notify(stored?.fileOpenTool ?? next);
|
|
2086
|
-
syncRevision();
|
|
2087
|
-
}
|
|
2088
|
-
};
|
|
2089
|
-
}
|
|
2090
|
-
function normalizeFileOpenTool(value) {
|
|
2091
|
-
return normalizeValue(value);
|
|
2092
|
-
}
|
|
2093
|
-
//#endregion
|
|
2094
2263
|
//#region src/rpc.ts
|
|
2095
2264
|
/** cwd → Promise 链:串行化 debug 日志追加(fs read+write 非原子,避免并发丢行)。 */
|
|
2096
2265
|
const debugWriteQueues = /* @__PURE__ */ new Map();
|
|
@@ -2617,7 +2786,11 @@ function buildHandlers(ctx, registry, searcher = newSearcher(ctx)) {
|
|
|
2617
2786
|
error: String(error)
|
|
2618
2787
|
};
|
|
2619
2788
|
}
|
|
2620
|
-
}
|
|
2789
|
+
},
|
|
2790
|
+
"compat": async () => ({
|
|
2791
|
+
ok: true,
|
|
2792
|
+
report: await buildReport(ctx)
|
|
2793
|
+
})
|
|
2621
2794
|
};
|
|
2622
2795
|
}
|
|
2623
2796
|
/**
|
|
@@ -2640,7 +2813,6 @@ async function handleRpc(ctx, registry, method, args, searcher = newSearcher(ctx
|
|
|
2640
2813
|
* tools.restrict() 负责模型可见性,tools.guard() 负责执行层兜底拒绝。
|
|
2641
2814
|
* 作者 ddj 2026年08月22号
|
|
2642
2815
|
*/
|
|
2643
|
-
const LEGACY_PREFIX = "vsm-mcp:";
|
|
2644
2816
|
const UNKNOWN_PROJECT = "__unknown_project__";
|
|
2645
2817
|
/** 统一 workspace 路径,供 cwd 父路径匹配。 */
|
|
2646
2818
|
function normalizePath(path) {
|
|
@@ -2657,15 +2829,8 @@ function matchWorkspace(cwd, paths) {
|
|
|
2657
2829
|
matches.sort((a, b) => normalizePath(b).length - normalizePath(a).length);
|
|
2658
2830
|
return matches[0];
|
|
2659
2831
|
}
|
|
2660
|
-
/** 根据 entry id 判断是否为新旧格式项目 MCP
|
|
2661
|
-
|
|
2662
|
-
return id.startsWith("vsm-mcp.") || id.startsWith(LEGACY_PREFIX);
|
|
2663
|
-
}
|
|
2664
|
-
/** 从项目 entry id 解析 workspace hash。 */
|
|
2665
|
-
function entryHash(id) {
|
|
2666
|
-
if (id.startsWith("vsm-mcp.")) return id.slice(8).split(".")[0];
|
|
2667
|
-
if (id.startsWith(LEGACY_PREFIX)) return id.slice(8).split(":")[0];
|
|
2668
|
-
}
|
|
2832
|
+
/** 根据 entry id 判断是否为新旧格式项目 MCP(兼容层统一判定)。 */
|
|
2833
|
+
const isProjectEntry = isProjectEntryId;
|
|
2669
2834
|
/** 返回一个 agent 不应继承的项目工具名。 */
|
|
2670
2835
|
function denyTools(toolProjects, currentPath) {
|
|
2671
2836
|
return [...toolProjects.entries()].filter(([, owner]) => owner !== currentPath).map(([name]) => name).sort();
|
|
@@ -2779,9 +2944,10 @@ function dropFileIndex(cwd) {
|
|
|
2779
2944
|
//#region src/index.ts
|
|
2780
2945
|
/**
|
|
2781
2946
|
* @dsh-external 生态 → dsh-vscode-mode:DSH 上的类 VSCode 编码体验(Host 半入口)。
|
|
2782
|
-
* 职责:装配 capture(tools/result 捕获 edit/write 差异)、RPC 分发(
|
|
2783
|
-
* webServer 路由(/edrv/rpc、/edrv/assets/*、/edrv/vendor
|
|
2784
|
-
*
|
|
2947
|
+
* 职责:装配 capture(tools/result 捕获 edit/write 差异)、RPC 分发(edrv.* / mcp.* / vscode.* / compat)、
|
|
2948
|
+
* webServer 路由(/edrv/rpc、/edrv/assets/*、/edrv/vendor/*)、工作区旁车持久化、
|
|
2949
|
+
* 兼容层装配(依赖守卫 + 重复装配自诊断 + 启动日志兼容性报告)。
|
|
2950
|
+
* 结构:shared/(双面契约) + model/store/capture/workspace/revert/registry/rpc/routes/compat(host 模块)。
|
|
2785
2951
|
* 作者 ddj 2026-08-20
|
|
2786
2952
|
*/
|
|
2787
2953
|
const name = "dsh-vscode-mode";
|
|
@@ -2795,7 +2961,7 @@ const inject = [
|
|
|
2795
2961
|
"agents"
|
|
2796
2962
|
];
|
|
2797
2963
|
/**
|
|
2798
|
-
*
|
|
2964
|
+
* 装配插件:挂事件监听、注册路由、安装兼容层。
|
|
2799
2965
|
* @author ddj 2026年08月20号
|
|
2800
2966
|
* @param ctx DSH 上下文(sessions/fs/webServer 由 inject 提供;sandboxPolicy/subprocess 惰性获取)
|
|
2801
2967
|
* @param config 插件配置(可选 imageDir 覆盖图标目录)
|
|
@@ -2803,6 +2969,8 @@ const inject = [
|
|
|
2803
2969
|
function apply(ctx, config) {
|
|
2804
2970
|
const registry = /* @__PURE__ */ new Map();
|
|
2805
2971
|
const searcher = newSearcher(ctx);
|
|
2972
|
+
/** 兼容性警告收集(route 护栏等写入,启动日志一并输出)。 */
|
|
2973
|
+
const warnings = [];
|
|
2806
2974
|
setupOpenSettings(ctx, config, () => {});
|
|
2807
2975
|
ctx.on("tools/result", (exec, result) => {
|
|
2808
2976
|
captureToolResult(ctx, registry, exec, result);
|
|
@@ -2815,10 +2983,30 @@ function apply(ctx, config) {
|
|
|
2815
2983
|
searcher.dispose(cwd);
|
|
2816
2984
|
}
|
|
2817
2985
|
});
|
|
2818
|
-
registerRoutes(ctx, config, (method, args) => handleRpc(ctx, registry, method, args, searcher));
|
|
2986
|
+
registerRoutes(ctx, config, (method, args) => handleRpc(ctx, registry, method, args, searcher), (warning) => warnings.push(warning));
|
|
2819
2987
|
installIsolation(ctx);
|
|
2988
|
+
logCompatSummary(ctx, warnings);
|
|
2820
2989
|
ctx.logger?.info?.("[dsh-vscode-mode] 编辑差异审查已装配(/edrv/rpc 路由就绪,项目 MCP 隔离已启用)");
|
|
2821
2990
|
}
|
|
2991
|
+
/**
|
|
2992
|
+
* 异步输出兼容性报告摘要(含重复装配/路由冲突自诊断)。
|
|
2993
|
+
* @author ddj 2026年08月24号
|
|
2994
|
+
* @param ctx DSH host 上下文
|
|
2995
|
+
* @param warnings 装配期收集的兼容性警告
|
|
2996
|
+
*/
|
|
2997
|
+
async function logCompatSummary(ctx, warnings) {
|
|
2998
|
+
try {
|
|
2999
|
+
const report = await buildReport(ctx);
|
|
3000
|
+
for (const warning of warnings) report.warnings.push(warning);
|
|
3001
|
+
const logger = ctx.logger;
|
|
3002
|
+
if (!logger?.info) return;
|
|
3003
|
+
const head = "[dsh-vscode-mode] 兼容性:" + report.external.length + " 项外部适配 / " + report.guards.length + " 项护栏";
|
|
3004
|
+
if (report.warnings.length) logger.warn?.(head + ",警告 " + report.warnings.length + " 条:" + report.warnings.join(";"));
|
|
3005
|
+
else logger.info?.(head + ",无警告");
|
|
3006
|
+
} catch (error) {
|
|
3007
|
+
ctx.logger?.warn?.("[dsh-vscode-mode] 兼容性报告生成失败:" + String(error));
|
|
3008
|
+
}
|
|
3009
|
+
}
|
|
2822
3010
|
//#endregion
|
|
2823
3011
|
export { apply, inject, name };
|
|
2824
3012
|
|