pi-web-ui 0.114.1 → 0.114.2

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 (117) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +187 -187
  3. package/README.zh-CN.md +227 -227
  4. package/README.zh-TW.md +227 -227
  5. package/bin/device-hub-cli.mjs +577 -577
  6. package/bin/frp-manager.mjs +1573 -1573
  7. package/bin/pi-device-hub.mjs +9 -9
  8. package/bin/pi-web-ui.mjs +4081 -4081
  9. package/deploy/nginx-subpath.conf +100 -100
  10. package/deploy/pi-web-ui.service +36 -36
  11. package/dist/server/agent-service.js +59 -10
  12. package/dist/server/attachments.js +6 -6
  13. package/dist/server/canvas/canvas-orchestrator.js +3 -3
  14. package/dist/server/communicate-tool.js +37 -11
  15. package/dist/server/compaction-instructions.js +8 -8
  16. package/dist/server/device-center/desktop-page.js +228 -228
  17. package/dist/server/device-center/hub-page.js +64 -64
  18. package/dist/server/device-center/native-desktop/macos.js +190 -190
  19. package/dist/server/device-center/native-desktop/windows.js +116 -116
  20. package/dist/server/dsh/runtime/cordis.yml +1 -1
  21. package/dist/server/dsh/runtime/custom-prompt.mjs +33 -33
  22. package/dist/server/dsh/runtime/goal-rpc.mjs +1309 -1309
  23. package/dist/server/dsh/runtime/launcher.mjs +235 -235
  24. package/dist/server/dsh/runtime/override.patch.yml +89 -89
  25. package/dist/server/dsh/runtime/runtime-root.mjs +405 -405
  26. package/dist/server/durable/durable-event-projector.js +19 -3
  27. package/dist/server/durable/durable-models.js +134 -65
  28. package/dist/server/eval-tool.js +135 -135
  29. package/dist/server/model-resolver.js +4 -1
  30. package/dist/server/patch-bash-process.js +89 -89
  31. package/dist/server/patch-tool.js +9 -9
  32. package/dist/server/patch-turn-end-boundary.js +3 -3
  33. package/dist/server/persona-chain.js +8 -8
  34. package/dist/server/remote-ssh-service.js +39 -39
  35. package/dist/server/schedule-agent-tool.js +11 -6
  36. package/dist/server/scheduler-tasks.js +84 -2
  37. package/dist/server/scm-commitmsg.js +10 -10
  38. package/dist/server/setup-page.js +620 -620
  39. package/dist/server/universal/universal-session.js +19 -18
  40. package/dist/server/vision-bridge.js +18 -18
  41. package/docs/device-center.md +148 -148
  42. package/docs/device-desktop.md +48 -48
  43. package/docs/dsh-engine.md +493 -493
  44. package/extensions/webui.ts +335 -335
  45. package/package.json +1 -1
  46. package/plugin-sdk/README.md +174 -174
  47. package/plugin-sdk/client-utils.mjs +279 -279
  48. package/plugin-sdk/import-check.mjs +210 -210
  49. package/plugin-sdk/index.d.ts +698 -698
  50. package/plugin-sdk/index.mjs +696 -696
  51. package/plugin-sdk/package.json +13 -13
  52. package/plugin-sdk/ssh-host-trust.mjs +225 -225
  53. package/themes/aetheris.css +501 -501
  54. package/themes/ayu-light.css +116 -116
  55. package/themes/bases/flow-base.css +1357 -1357
  56. package/themes/bases/obsidian-base.css +895 -895
  57. package/themes/bases/statusbar-flat-base.css +112 -112
  58. package/themes/bases/zhujian-base.css +688 -688
  59. package/themes/bases/zhupi-base.css +660 -660
  60. package/themes/catppuccin-latte.css +119 -119
  61. package/themes/catppuccin.css +116 -116
  62. package/themes/claude-code-dark.css +147 -147
  63. package/themes/codex.css +114 -114
  64. package/themes/cyberpunk.css +50 -50
  65. package/themes/dark-teal.css +264 -264
  66. package/themes/dazzle.css +38 -38
  67. package/themes/everforest-light.css +115 -115
  68. package/themes/flow-light.css +164 -164
  69. package/themes/flow.css +161 -161
  70. package/themes/geist.css +114 -114
  71. package/themes/gruvbox-light.css +115 -115
  72. package/themes/kanagawa-lotus.css +115 -115
  73. package/themes/md-preview.css +24 -24
  74. package/themes/mist.css +166 -166
  75. package/themes/nord.css +107 -107
  76. package/themes/one-dark.css +106 -106
  77. package/themes/paper.css +174 -174
  78. package/themes/rose-pine-dawn.css +114 -114
  79. package/themes/sakura.css +166 -166
  80. package/themes/solarized-light.css +114 -114
  81. package/themes/tokyo-night.css +115 -115
  82. package/themes/translucent.css +85 -85
  83. package/themes/transparent.css +116 -116
  84. package/themes/vs-code-dark.css +147 -147
  85. package/themes/wallpaper-flow.css +382 -382
  86. package/themes/wallpaper-obsidian.css +74 -74
  87. package/themes/white.css +164 -164
  88. package/themes/zhujian-dark.css +115 -115
  89. package/themes/zhujian.css +114 -114
  90. package/themes/zhupi-dark.css +115 -115
  91. package/themes/zhupi.css +114 -114
  92. package/web/dist/.build-assets.json +1 -1
  93. package/web/dist/assets/{BgTasksModal-BcXJ20RO.js → BgTasksModal-BIlQ89vr.js} +1 -1
  94. package/web/dist/assets/{BgTasksModal-DjQmiKnE.js → BgTasksModal-Ck2sjlPM.js} +1 -1
  95. package/web/dist/assets/{CanvasStudio-PI9pid0Y.js → CanvasStudio-B3W6b_rA.js} +1 -1
  96. package/web/dist/assets/{CanvasStudio-nf5-HxLM.js → CanvasStudio-Csv1Sj1-.js} +1 -1
  97. package/web/dist/assets/{DeviceCenter-CYttJw2a.js → DeviceCenter-BQdDcO8A.js} +1 -1
  98. package/web/dist/assets/{DeviceCenter-DPe4sz74.js → DeviceCenter-CHRKL0Vy.js} +1 -1
  99. package/web/dist/assets/{FilePreview-C9Ut4Lq6.js → FilePreview-B9xoU6ON.js} +1 -1
  100. package/web/dist/assets/{FilePreview-CvK3ZzX5.js → FilePreview-DUQpVZkH.js} +1 -1
  101. package/web/dist/assets/{GlobalSearchModal-C5e-OCw2.js → GlobalSearchModal-BXEGvUZq.js} +1 -1
  102. package/web/dist/assets/{GlobalSearchModal-H7pFK2UC.js → GlobalSearchModal-CnVPmfOI.js} +1 -1
  103. package/web/dist/assets/{GroupConversation-C2wNM0dQ.js → GroupConversation-CyIpQzDd.js} +1 -1
  104. package/web/dist/assets/{GroupConversation-ChN3TWQK.js → GroupConversation-DgYV3uYS.js} +1 -1
  105. package/web/dist/assets/{SCMPanel-buTGziDB.js → SCMPanel-imYI2Ecw.js} +1 -1
  106. package/web/dist/assets/{SCMPanel-kx-SCL2s.js → SCMPanel-sVZcV7dR.js} +1 -1
  107. package/web/dist/assets/{SettingsModal-BsUF8jqr.js → SettingsModal-Xw1Yoi3_.js} +1 -1
  108. package/web/dist/assets/{SettingsModal-D357Ky7P.js → SettingsModal-mb4e34-0.js} +1 -1
  109. package/web/dist/assets/{TerminalPanel-BBuM_Sez.js → TerminalPanel-CLkJJvxg.js} +1 -1
  110. package/web/dist/assets/{TerminalPanel-D6x6xz8Q.js → TerminalPanel-DpcIellm.js} +1 -1
  111. package/web/dist/assets/{TuiOverlayModal-CdbGV3Za.js → TuiOverlayModal-4-fFAZe7.js} +1 -1
  112. package/web/dist/assets/{TuiOverlayModal-d6x9u25n.js → TuiOverlayModal-DCu01lks.js} +1 -1
  113. package/web/dist/assets/{index-D6u8xH0o.js → index-Cdh3sibd.js} +4 -4
  114. package/web/dist/assets/{index-BPvpea8t.js → index-DWw_SxRq.js} +4 -4
  115. package/web/dist/index.html +29 -29
  116. package/web/dist/sw.js +179 -179
  117. package/web/public/sw.js +179 -179
@@ -1,405 +1,405 @@
1
- // Shared runtime-tree resolution for the pi-web-ui DSH runtime.
2
- // Used by launcher.mjs (boot-time) and dev/dsh-probes/probe-mixed.mjs (self-check).
3
- //
4
- // A "runtime tree" is a node_modules root that directly contains the dsh
5
- // runtime packages: `@deepseek-ai/dsh-base` (with its cordis.patch.yml
6
- // bundle) and `@deepseek-ai/dsh-app-boot`. Two layouts are accepted:
7
- //
8
- // flat: <root>/@deepseek-ai/dsh-base/cordis.patch.yml
9
- // (a node_modules root where the runtime packages live top-level)
10
- //
11
- // nested: <root>/@deepseek-ai/dsh/node_modules/@deepseek-ai/…
12
- // (`npm i -g @deepseek-ai/dsh` installs the CLI package with its
13
- // OWN nested runtime tree of ~196 packages; the global root itself
14
- // only holds the `dsh` package)
15
- //
16
- // The returned value is always the *bare-module base dir*: the node_modules
17
- // root that directly contains the runtime packages. That is what
18
- // boot(bareModuleBaseUrl) anchors bare package names (dsh-base rows) to.
19
- import { existsSync, readFileSync, realpathSync } from "node:fs";
20
- import { delimiter, dirname, join, resolve } from "node:path";
21
- import { homedir } from "node:os";
22
- import { fileURLToPath } from "node:url";
23
-
24
- const HERE = dirname(fileURLToPath(import.meta.url));
25
-
26
- /** 兜底固定的 DSH 版本族,与 package.json / lockfile 对齐 */
27
- export const FALLBACK_DSH_VERSION = "0.2.1-rc.2";
28
-
29
- /** Given a candidate node_modules root, return the bare-module base dir, or null. */
30
- export function runtimeBaseFor(root) {
31
- if (root == null) return null;
32
- const flatScope = join(root, "@deepseek-ai");
33
- if (
34
- existsSync(join(flatScope, "dsh-base", "cordis.patch.yml")) &&
35
- existsSync(join(flatScope, "dsh-app-boot", "lib", "index.js"))
36
- ) {
37
- return resolve(root);
38
- }
39
- const nestedScope = join(root, "@deepseek-ai", "dsh", "node_modules", "@deepseek-ai");
40
- if (
41
- existsSync(join(nestedScope, "dsh-base", "cordis.patch.yml")) &&
42
- existsSync(join(nestedScope, "dsh-app-boot", "lib", "index.js"))
43
- ) {
44
- return resolve(join(root, "@deepseek-ai", "dsh", "node_modules"));
45
- }
46
- return null;
47
- }
48
-
49
- /** 向上查找 pi-web-ui 包根目录(支持源码态 server/ 与构建态 dist/) */
50
- export function findLocalPackageRoot(startDir = HERE) {
51
- let cur = resolve(startDir);
52
- for (let i = 0; i < 6; i++) {
53
- const pkgPath = join(cur, "package.json");
54
- if (existsSync(pkgPath)) {
55
- try {
56
- const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
57
- if (pkg.name === "pi-web-ui") return cur;
58
- } catch {}
59
- }
60
- const parent = dirname(cur);
61
- if (parent === cur) break;
62
- cur = parent;
63
- }
64
- return null;
65
- }
66
-
67
- /**
68
- * 提取 DSH 版本族。
69
- * 在 pre-release(0.x)阶段,按 major.minor 分族(如 0.1.x 同族,0.2.x 同族),
70
- * 既隔离 0.1 与 0.2 底层重大协议断裂,又避免 patch/rc 微调时过度阻断;正式版(>=1.0)按 major 分族。
71
- */
72
- export function getDshFamily(version) {
73
- if (!version || typeof version !== "string") return "";
74
- const clean = version.replace(/^[v^~]/, "").trim();
75
- const m = clean.match(/^(\d+)\.(\d+)/);
76
- if (!m) return clean;
77
- const [, major, minor] = m;
78
- if (major === "0") {
79
- return `${major}.${minor}`;
80
- }
81
- return `${major}`;
82
- }
83
-
84
- /** 读取运行时树中已安装的 @deepseek-ai/dsh-base 版本 */
85
- export function getRuntimeDshVersion(runtimeBase) {
86
- if (!runtimeBase) return null;
87
- const pkgPath = join(runtimeBase, "@deepseek-ai", "dsh-base", "package.json");
88
- if (existsSync(pkgPath)) {
89
- try {
90
- return JSON.parse(readFileSync(pkgPath, "utf8")).version || null;
91
- } catch {
92
- return null;
93
- }
94
- }
95
- return null;
96
- }
97
-
98
- /** 读取 pi-web-ui 本地期望绑定的 DSH 版本 */
99
- export function getExpectedDshVersion(pkgRoot) {
100
- const root = pkgRoot || findLocalPackageRoot(HERE);
101
- if (root) {
102
- try {
103
- const p = join(root, "node_modules", "@deepseek-ai", "dsh-sdk-jsonrpc-server", "package.json");
104
- if (existsSync(p)) {
105
- const v = JSON.parse(readFileSync(p, "utf8")).version;
106
- if (v) return v;
107
- }
108
- } catch {}
109
- try {
110
- const p = join(root, "package.json");
111
- if (existsSync(p)) {
112
- const pkg = JSON.parse(readFileSync(p, "utf8"));
113
- const v = pkg.dependencies?.["@deepseek-ai/dsh-sdk-jsonrpc-server"];
114
- if (v) return v.replace(/^[v^~]/, "").trim();
115
- }
116
- } catch {}
117
- }
118
- return FALLBACK_DSH_VERSION;
119
- }
120
-
121
- /** 校验运行时树与 pi-web-ui 依赖的版本族兼容性 */
122
- export function checkDshRuntimeCompatibility(runtimeBase, expectedVersion) {
123
- const runtimeVersion = getRuntimeDshVersion(runtimeBase);
124
- const expected = expectedVersion || getExpectedDshVersion();
125
- const runtimeFamily = getDshFamily(runtimeVersion);
126
- const expectedFamily = getDshFamily(expected);
127
- const compatible = Boolean(runtimeFamily && expectedFamily && runtimeFamily === expectedFamily);
128
- return {
129
- compatible,
130
- runtimeVersion,
131
- expectedVersion: expected,
132
- runtimeFamily,
133
- expectedFamily,
134
- };
135
- }
136
-
137
- /** 解析 .npmrc 内容中的 prefix 配置值 */
138
- export function parseNpmrcPrefix(content) {
139
- if (!content || typeof content !== "string") return null;
140
- const lines = content.split(/\r?\n/);
141
- for (const line of lines) {
142
- const trimmed = line.trim();
143
- if (!trimmed || trimmed.startsWith("#") || trimmed.startsWith(";")) continue;
144
- const m = trimmed.match(/^prefix\s*=\s*(.+)$/);
145
- if (m) {
146
- let val = m[1].trim();
147
- if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
148
- val = val.slice(1, -1).trim();
149
- }
150
- if (val.startsWith("~")) {
151
- val = join(homedir(), val.slice(1));
152
- }
153
- return resolve(val);
154
- }
155
- }
156
- return null;
157
- }
158
-
159
- /** 从常见 npmrc 位置发现用户与系统配置的 prefix */
160
- export function findPrefixFromNpmrc() {
161
- const prefixes = [];
162
- const npmrcLocations = [
163
- join(homedir(), ".npmrc"),
164
- process.env.PREFIX ? join(process.env.PREFIX, "etc", "npmrc") : null,
165
- process.platform === "win32"
166
- ? process.env.APPDATA
167
- ? join(process.env.APPDATA, "npm", "etc", "npmrc")
168
- : null
169
- : "/usr/local/etc/npmrc",
170
- process.platform === "win32"
171
- ? process.env.ProgramFiles
172
- ? join(process.env.ProgramFiles, "nodejs", "etc", "npmrc")
173
- : null
174
- : "/etc/npmrc",
175
- process.platform !== "win32" ? "/usr/etc/npmrc" : null,
176
- ].filter(Boolean);
177
-
178
- for (const loc of npmrcLocations) {
179
- try {
180
- if (existsSync(loc)) {
181
- const content = readFileSync(loc, "utf8");
182
- const p = parseNpmrcPrefix(content);
183
- if (p && !prefixes.includes(p)) {
184
- prefixes.push(p);
185
- }
186
- }
187
- } catch {}
188
- }
189
- return prefixes;
190
- }
191
-
192
- /** 从 $PATH 中探测 dsh / pi-web-ui / npm 二进制并推导 node_modules 候选 */
193
- export function findCandidatesFromPath() {
194
- const candidates = [];
195
- const rawPath = process.env.PATH || process.env.Path || "";
196
- if (!rawPath) return candidates;
197
-
198
- const binNames =
199
- process.platform === "win32"
200
- ? [
201
- "dsh.cmd",
202
- "dsh.ps1",
203
- "dsh.exe",
204
- "dsh",
205
- "pi-web-ui.cmd",
206
- "pi-web-ui.ps1",
207
- "pi-web-ui.exe",
208
- "pi-web-ui",
209
- "npm.cmd",
210
- "npm.ps1",
211
- "npm.exe",
212
- "npm",
213
- ]
214
- : ["dsh", "pi-web-ui", "npm"];
215
-
216
- const dirs = rawPath.split(delimiter).filter(Boolean);
217
- for (const dir of dirs) {
218
- let dirExists = false;
219
- try {
220
- dirExists = existsSync(dir);
221
- } catch {}
222
- if (!dirExists) continue;
223
-
224
- for (const bin of binNames) {
225
- const binPath = join(dir, bin);
226
- let binExists = false;
227
- try {
228
- binExists = existsSync(binPath);
229
- } catch {}
230
- if (!binExists) continue;
231
-
232
- // a) 符号链接真实路径反查 node_modules 根
233
- try {
234
- const real = realpathSync(binPath);
235
- const nmIdx = real.lastIndexOf("node_modules");
236
- if (nmIdx !== -1) {
237
- const nmRoot = real.slice(0, nmIdx + "node_modules".length);
238
- candidates.push({ path: nmRoot, source: `path:${bin} (symlink realpath)` });
239
- }
240
- } catch {}
241
-
242
- // b) Windows npm 全局安装常见布局:<prefix>/dsh.cmd,对应 <prefix>/node_modules
243
- candidates.push({ path: join(dir, "node_modules"), source: `path:${bin} (adjacent)` });
244
- candidates.push({ path: join(dir, "lib", "node_modules"), source: `path:${bin} (lib)` });
245
-
246
- // c) POSIX npm 全局安装常见布局:<prefix>/bin/dsh,对应 <prefix>/lib/node_modules
247
- const parent = dirname(dir);
248
- if (parent && parent !== dir) {
249
- candidates.push({ path: join(parent, "lib", "node_modules"), source: `path:${bin} (parent/lib)` });
250
- candidates.push({ path: join(parent, "node_modules"), source: `path:${bin} (parent)` });
251
- }
252
- }
253
- }
254
- return candidates;
255
- }
256
-
257
- /**
258
- * 完整运行时树探测与详情收集(纯 fs 操作,无子进程 spawn,杜绝 Android/Termux 死锁)。
259
- * 顺序:
260
- * 1. $PI_WEB_DSH_RUNTIME — 显式指定最高优先级
261
- * 2. 本包(pi-web-ui)的 node_modules
262
- * 3. execPath 邻近 node_modules
263
- * 4. 从 $PATH 二进制推断的全局目录(支持符号链接 realpath 反查)
264
- * 5. 从 .npmrc prefix 配置派生的全局目录
265
- * 6. 环境变量 NPM_CONFIG_PREFIX / npm_config_prefix / PREFIX
266
- * 7. 系统常见全局 node_modules 目录
267
- */
268
- export async function resolveRuntimeBaseWithDetails() {
269
- const scannedPaths = [];
270
- const seenPaths = new Set();
271
- let firstFoundBase = null;
272
- let firstCompatibleBase = null;
273
-
274
- const expectedVersion = getExpectedDshVersion();
275
-
276
- const probe = (candidatePath, source) => {
277
- if (!candidatePath) return null;
278
- const norm = resolve(candidatePath);
279
- if (seenPaths.has(norm)) return null;
280
- seenPaths.add(norm);
281
-
282
- let exists = false;
283
- try {
284
- exists = existsSync(norm);
285
- } catch {}
286
-
287
- if (!exists) {
288
- scannedPaths.push({ path: norm, exists: false, validRuntime: false, source });
289
- return null;
290
- }
291
-
292
- const base = runtimeBaseFor(norm);
293
- if (!base) {
294
- scannedPaths.push({
295
- path: norm,
296
- exists: true,
297
- validRuntime: false,
298
- reason: "缺少 @deepseek-ai/dsh-base 或 dsh-app-boot",
299
- source,
300
- });
301
- return null;
302
- }
303
-
304
- const compat = checkDshRuntimeCompatibility(base, expectedVersion);
305
- scannedPaths.push({
306
- path: norm,
307
- exists: true,
308
- validRuntime: true,
309
- base,
310
- version: compat.runtimeVersion,
311
- family: compat.runtimeFamily,
312
- compatible: compat.compatible,
313
- source,
314
- });
315
-
316
- if (!firstFoundBase) {
317
- firstFoundBase = base;
318
- }
319
- if (compat.compatible && !firstCompatibleBase) {
320
- firstCompatibleBase = base;
321
- }
322
-
323
- return base;
324
- };
325
-
326
- // 1. $PI_WEB_DSH_RUNTIME — 显式指定
327
- const explicit = process.env.PI_WEB_DSH_RUNTIME;
328
- if (explicit) {
329
- const base = probe(explicit, "env:PI_WEB_DSH_RUNTIME");
330
- if (base) {
331
- return {
332
- base,
333
- scannedPaths,
334
- compatibility: checkDshRuntimeCompatibility(base, expectedVersion),
335
- };
336
- }
337
- }
338
-
339
- // 2. 本包(pi-web-ui)的 node_modules
340
- const pkgRoot = findLocalPackageRoot(HERE);
341
- if (pkgRoot) {
342
- probe(join(pkgRoot, "node_modules"), "local:packageRoot");
343
- }
344
- probe(join(resolve(HERE, "..", "..", ".."), "node_modules"), "local:relative-3");
345
- probe(join(resolve(HERE, "..", "..", "..", ".."), "node_modules"), "local:relative-4");
346
-
347
- // 3. execPath 邻近 node_modules
348
- probe(join(dirname(process.execPath), "node_modules"), "execPath:adjacent");
349
- probe(join(dirname(process.execPath), "..", "lib", "node_modules"), "execPath:parent-lib");
350
- probe(join(dirname(process.execPath), "..", "node_modules"), "execPath:parent");
351
-
352
- // 4. 从 $PATH 中探测到的候选
353
- for (const cand of findCandidatesFromPath()) {
354
- probe(cand.path, cand.source);
355
- }
356
-
357
- // 5. 从 npmrc 中探测到的 prefix
358
- for (const p of findPrefixFromNpmrc()) {
359
- probe(join(p, "lib", "node_modules"), "npmrc:prefix-lib");
360
- probe(join(p, "node_modules"), "npmrc:prefix");
361
- }
362
-
363
- // 6. 环境变量里的前缀
364
- const envPrefixes = [process.env.NPM_CONFIG_PREFIX, process.env.npm_config_prefix, process.env.PREFIX];
365
- for (const p of envPrefixes) {
366
- if (p) {
367
- probe(join(p, "lib", "node_modules"), "env:prefix-lib");
368
- probe(join(p, "node_modules"), "env:prefix");
369
- }
370
- }
371
-
372
- // 7. 常见系统默认路径
373
- if (process.platform === "win32") {
374
- if (process.env.APPDATA) probe(join(process.env.APPDATA, "npm", "node_modules"), "default:win-appdata");
375
- if (process.env.LOCALAPPDATA)
376
- probe(join(process.env.LOCALAPPDATA, "npm", "node_modules"), "default:win-localappdata");
377
- if (process.env.ProgramFiles)
378
- probe(join(process.env.ProgramFiles, "nodejs", "node_modules"), "default:win-programfiles");
379
- } else {
380
- probe("/usr/local/lib/node_modules", "default:posix-usr-local");
381
- probe("/usr/lib/node_modules", "default:posix-usr");
382
- const home = homedir();
383
- probe(join(home, ".local", "npm", "lib", "node_modules"), "default:posix-home-local-npm");
384
- probe(join(home, ".local", "npm", "node_modules"), "default:posix-home-local-npm-flat");
385
- probe(join(home, ".local", "lib", "node_modules"), "default:posix-home-local");
386
- probe(join(home, ".npm-global", "lib", "node_modules"), "default:posix-npm-global");
387
- probe(join(home, ".npm-global", "node_modules"), "default:posix-npm-global-flat");
388
- }
389
-
390
- // 若存在同版本族的有效树,优先选择;否则回退至首个可用树(由 launcher 报错指引版本)
391
- const chosenBase = firstCompatibleBase || firstFoundBase || null;
392
- const compatibility = chosenBase ? checkDshRuntimeCompatibility(chosenBase, expectedVersion) : null;
393
-
394
- return {
395
- base: chosenBase,
396
- scannedPaths,
397
- compatibility,
398
- };
399
- }
400
-
401
- /** 供已有代码直接获取有效 runtimeBase 路径 */
402
- export async function resolveRuntimeBase() {
403
- const { base } = await resolveRuntimeBaseWithDetails();
404
- return base;
405
- }
1
+ // Shared runtime-tree resolution for the pi-web-ui DSH runtime.
2
+ // Used by launcher.mjs (boot-time) and dev/dsh-probes/probe-mixed.mjs (self-check).
3
+ //
4
+ // A "runtime tree" is a node_modules root that directly contains the dsh
5
+ // runtime packages: `@deepseek-ai/dsh-base` (with its cordis.patch.yml
6
+ // bundle) and `@deepseek-ai/dsh-app-boot`. Two layouts are accepted:
7
+ //
8
+ // flat: <root>/@deepseek-ai/dsh-base/cordis.patch.yml
9
+ // (a node_modules root where the runtime packages live top-level)
10
+ //
11
+ // nested: <root>/@deepseek-ai/dsh/node_modules/@deepseek-ai/…
12
+ // (`npm i -g @deepseek-ai/dsh` installs the CLI package with its
13
+ // OWN nested runtime tree of ~196 packages; the global root itself
14
+ // only holds the `dsh` package)
15
+ //
16
+ // The returned value is always the *bare-module base dir*: the node_modules
17
+ // root that directly contains the runtime packages. That is what
18
+ // boot(bareModuleBaseUrl) anchors bare package names (dsh-base rows) to.
19
+ import { existsSync, readFileSync, realpathSync } from "node:fs";
20
+ import { delimiter, dirname, join, resolve } from "node:path";
21
+ import { homedir } from "node:os";
22
+ import { fileURLToPath } from "node:url";
23
+
24
+ const HERE = dirname(fileURLToPath(import.meta.url));
25
+
26
+ /** 兜底固定的 DSH 版本族,与 package.json / lockfile 对齐 */
27
+ export const FALLBACK_DSH_VERSION = "0.2.1-rc.2";
28
+
29
+ /** Given a candidate node_modules root, return the bare-module base dir, or null. */
30
+ export function runtimeBaseFor(root) {
31
+ if (root == null) return null;
32
+ const flatScope = join(root, "@deepseek-ai");
33
+ if (
34
+ existsSync(join(flatScope, "dsh-base", "cordis.patch.yml")) &&
35
+ existsSync(join(flatScope, "dsh-app-boot", "lib", "index.js"))
36
+ ) {
37
+ return resolve(root);
38
+ }
39
+ const nestedScope = join(root, "@deepseek-ai", "dsh", "node_modules", "@deepseek-ai");
40
+ if (
41
+ existsSync(join(nestedScope, "dsh-base", "cordis.patch.yml")) &&
42
+ existsSync(join(nestedScope, "dsh-app-boot", "lib", "index.js"))
43
+ ) {
44
+ return resolve(join(root, "@deepseek-ai", "dsh", "node_modules"));
45
+ }
46
+ return null;
47
+ }
48
+
49
+ /** 向上查找 pi-web-ui 包根目录(支持源码态 server/ 与构建态 dist/) */
50
+ export function findLocalPackageRoot(startDir = HERE) {
51
+ let cur = resolve(startDir);
52
+ for (let i = 0; i < 6; i++) {
53
+ const pkgPath = join(cur, "package.json");
54
+ if (existsSync(pkgPath)) {
55
+ try {
56
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
57
+ if (pkg.name === "pi-web-ui") return cur;
58
+ } catch {}
59
+ }
60
+ const parent = dirname(cur);
61
+ if (parent === cur) break;
62
+ cur = parent;
63
+ }
64
+ return null;
65
+ }
66
+
67
+ /**
68
+ * 提取 DSH 版本族。
69
+ * 在 pre-release(0.x)阶段,按 major.minor 分族(如 0.1.x 同族,0.2.x 同族),
70
+ * 既隔离 0.1 与 0.2 底层重大协议断裂,又避免 patch/rc 微调时过度阻断;正式版(>=1.0)按 major 分族。
71
+ */
72
+ export function getDshFamily(version) {
73
+ if (!version || typeof version !== "string") return "";
74
+ const clean = version.replace(/^[v^~]/, "").trim();
75
+ const m = clean.match(/^(\d+)\.(\d+)/);
76
+ if (!m) return clean;
77
+ const [, major, minor] = m;
78
+ if (major === "0") {
79
+ return `${major}.${minor}`;
80
+ }
81
+ return `${major}`;
82
+ }
83
+
84
+ /** 读取运行时树中已安装的 @deepseek-ai/dsh-base 版本 */
85
+ export function getRuntimeDshVersion(runtimeBase) {
86
+ if (!runtimeBase) return null;
87
+ const pkgPath = join(runtimeBase, "@deepseek-ai", "dsh-base", "package.json");
88
+ if (existsSync(pkgPath)) {
89
+ try {
90
+ return JSON.parse(readFileSync(pkgPath, "utf8")).version || null;
91
+ } catch {
92
+ return null;
93
+ }
94
+ }
95
+ return null;
96
+ }
97
+
98
+ /** 读取 pi-web-ui 本地期望绑定的 DSH 版本 */
99
+ export function getExpectedDshVersion(pkgRoot) {
100
+ const root = pkgRoot || findLocalPackageRoot(HERE);
101
+ if (root) {
102
+ try {
103
+ const p = join(root, "node_modules", "@deepseek-ai", "dsh-sdk-jsonrpc-server", "package.json");
104
+ if (existsSync(p)) {
105
+ const v = JSON.parse(readFileSync(p, "utf8")).version;
106
+ if (v) return v;
107
+ }
108
+ } catch {}
109
+ try {
110
+ const p = join(root, "package.json");
111
+ if (existsSync(p)) {
112
+ const pkg = JSON.parse(readFileSync(p, "utf8"));
113
+ const v = pkg.dependencies?.["@deepseek-ai/dsh-sdk-jsonrpc-server"];
114
+ if (v) return v.replace(/^[v^~]/, "").trim();
115
+ }
116
+ } catch {}
117
+ }
118
+ return FALLBACK_DSH_VERSION;
119
+ }
120
+
121
+ /** 校验运行时树与 pi-web-ui 依赖的版本族兼容性 */
122
+ export function checkDshRuntimeCompatibility(runtimeBase, expectedVersion) {
123
+ const runtimeVersion = getRuntimeDshVersion(runtimeBase);
124
+ const expected = expectedVersion || getExpectedDshVersion();
125
+ const runtimeFamily = getDshFamily(runtimeVersion);
126
+ const expectedFamily = getDshFamily(expected);
127
+ const compatible = Boolean(runtimeFamily && expectedFamily && runtimeFamily === expectedFamily);
128
+ return {
129
+ compatible,
130
+ runtimeVersion,
131
+ expectedVersion: expected,
132
+ runtimeFamily,
133
+ expectedFamily,
134
+ };
135
+ }
136
+
137
+ /** 解析 .npmrc 内容中的 prefix 配置值 */
138
+ export function parseNpmrcPrefix(content) {
139
+ if (!content || typeof content !== "string") return null;
140
+ const lines = content.split(/\r?\n/);
141
+ for (const line of lines) {
142
+ const trimmed = line.trim();
143
+ if (!trimmed || trimmed.startsWith("#") || trimmed.startsWith(";")) continue;
144
+ const m = trimmed.match(/^prefix\s*=\s*(.+)$/);
145
+ if (m) {
146
+ let val = m[1].trim();
147
+ if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
148
+ val = val.slice(1, -1).trim();
149
+ }
150
+ if (val.startsWith("~")) {
151
+ val = join(homedir(), val.slice(1));
152
+ }
153
+ return resolve(val);
154
+ }
155
+ }
156
+ return null;
157
+ }
158
+
159
+ /** 从常见 npmrc 位置发现用户与系统配置的 prefix */
160
+ export function findPrefixFromNpmrc() {
161
+ const prefixes = [];
162
+ const npmrcLocations = [
163
+ join(homedir(), ".npmrc"),
164
+ process.env.PREFIX ? join(process.env.PREFIX, "etc", "npmrc") : null,
165
+ process.platform === "win32"
166
+ ? process.env.APPDATA
167
+ ? join(process.env.APPDATA, "npm", "etc", "npmrc")
168
+ : null
169
+ : "/usr/local/etc/npmrc",
170
+ process.platform === "win32"
171
+ ? process.env.ProgramFiles
172
+ ? join(process.env.ProgramFiles, "nodejs", "etc", "npmrc")
173
+ : null
174
+ : "/etc/npmrc",
175
+ process.platform !== "win32" ? "/usr/etc/npmrc" : null,
176
+ ].filter(Boolean);
177
+
178
+ for (const loc of npmrcLocations) {
179
+ try {
180
+ if (existsSync(loc)) {
181
+ const content = readFileSync(loc, "utf8");
182
+ const p = parseNpmrcPrefix(content);
183
+ if (p && !prefixes.includes(p)) {
184
+ prefixes.push(p);
185
+ }
186
+ }
187
+ } catch {}
188
+ }
189
+ return prefixes;
190
+ }
191
+
192
+ /** 从 $PATH 中探测 dsh / pi-web-ui / npm 二进制并推导 node_modules 候选 */
193
+ export function findCandidatesFromPath() {
194
+ const candidates = [];
195
+ const rawPath = process.env.PATH || process.env.Path || "";
196
+ if (!rawPath) return candidates;
197
+
198
+ const binNames =
199
+ process.platform === "win32"
200
+ ? [
201
+ "dsh.cmd",
202
+ "dsh.ps1",
203
+ "dsh.exe",
204
+ "dsh",
205
+ "pi-web-ui.cmd",
206
+ "pi-web-ui.ps1",
207
+ "pi-web-ui.exe",
208
+ "pi-web-ui",
209
+ "npm.cmd",
210
+ "npm.ps1",
211
+ "npm.exe",
212
+ "npm",
213
+ ]
214
+ : ["dsh", "pi-web-ui", "npm"];
215
+
216
+ const dirs = rawPath.split(delimiter).filter(Boolean);
217
+ for (const dir of dirs) {
218
+ let dirExists = false;
219
+ try {
220
+ dirExists = existsSync(dir);
221
+ } catch {}
222
+ if (!dirExists) continue;
223
+
224
+ for (const bin of binNames) {
225
+ const binPath = join(dir, bin);
226
+ let binExists = false;
227
+ try {
228
+ binExists = existsSync(binPath);
229
+ } catch {}
230
+ if (!binExists) continue;
231
+
232
+ // a) 符号链接真实路径反查 node_modules 根
233
+ try {
234
+ const real = realpathSync(binPath);
235
+ const nmIdx = real.lastIndexOf("node_modules");
236
+ if (nmIdx !== -1) {
237
+ const nmRoot = real.slice(0, nmIdx + "node_modules".length);
238
+ candidates.push({ path: nmRoot, source: `path:${bin} (symlink realpath)` });
239
+ }
240
+ } catch {}
241
+
242
+ // b) Windows npm 全局安装常见布局:<prefix>/dsh.cmd,对应 <prefix>/node_modules
243
+ candidates.push({ path: join(dir, "node_modules"), source: `path:${bin} (adjacent)` });
244
+ candidates.push({ path: join(dir, "lib", "node_modules"), source: `path:${bin} (lib)` });
245
+
246
+ // c) POSIX npm 全局安装常见布局:<prefix>/bin/dsh,对应 <prefix>/lib/node_modules
247
+ const parent = dirname(dir);
248
+ if (parent && parent !== dir) {
249
+ candidates.push({ path: join(parent, "lib", "node_modules"), source: `path:${bin} (parent/lib)` });
250
+ candidates.push({ path: join(parent, "node_modules"), source: `path:${bin} (parent)` });
251
+ }
252
+ }
253
+ }
254
+ return candidates;
255
+ }
256
+
257
+ /**
258
+ * 完整运行时树探测与详情收集(纯 fs 操作,无子进程 spawn,杜绝 Android/Termux 死锁)。
259
+ * 顺序:
260
+ * 1. $PI_WEB_DSH_RUNTIME — 显式指定最高优先级
261
+ * 2. 本包(pi-web-ui)的 node_modules
262
+ * 3. execPath 邻近 node_modules
263
+ * 4. 从 $PATH 二进制推断的全局目录(支持符号链接 realpath 反查)
264
+ * 5. 从 .npmrc prefix 配置派生的全局目录
265
+ * 6. 环境变量 NPM_CONFIG_PREFIX / npm_config_prefix / PREFIX
266
+ * 7. 系统常见全局 node_modules 目录
267
+ */
268
+ export async function resolveRuntimeBaseWithDetails() {
269
+ const scannedPaths = [];
270
+ const seenPaths = new Set();
271
+ let firstFoundBase = null;
272
+ let firstCompatibleBase = null;
273
+
274
+ const expectedVersion = getExpectedDshVersion();
275
+
276
+ const probe = (candidatePath, source) => {
277
+ if (!candidatePath) return null;
278
+ const norm = resolve(candidatePath);
279
+ if (seenPaths.has(norm)) return null;
280
+ seenPaths.add(norm);
281
+
282
+ let exists = false;
283
+ try {
284
+ exists = existsSync(norm);
285
+ } catch {}
286
+
287
+ if (!exists) {
288
+ scannedPaths.push({ path: norm, exists: false, validRuntime: false, source });
289
+ return null;
290
+ }
291
+
292
+ const base = runtimeBaseFor(norm);
293
+ if (!base) {
294
+ scannedPaths.push({
295
+ path: norm,
296
+ exists: true,
297
+ validRuntime: false,
298
+ reason: "缺少 @deepseek-ai/dsh-base 或 dsh-app-boot",
299
+ source,
300
+ });
301
+ return null;
302
+ }
303
+
304
+ const compat = checkDshRuntimeCompatibility(base, expectedVersion);
305
+ scannedPaths.push({
306
+ path: norm,
307
+ exists: true,
308
+ validRuntime: true,
309
+ base,
310
+ version: compat.runtimeVersion,
311
+ family: compat.runtimeFamily,
312
+ compatible: compat.compatible,
313
+ source,
314
+ });
315
+
316
+ if (!firstFoundBase) {
317
+ firstFoundBase = base;
318
+ }
319
+ if (compat.compatible && !firstCompatibleBase) {
320
+ firstCompatibleBase = base;
321
+ }
322
+
323
+ return base;
324
+ };
325
+
326
+ // 1. $PI_WEB_DSH_RUNTIME — 显式指定
327
+ const explicit = process.env.PI_WEB_DSH_RUNTIME;
328
+ if (explicit) {
329
+ const base = probe(explicit, "env:PI_WEB_DSH_RUNTIME");
330
+ if (base) {
331
+ return {
332
+ base,
333
+ scannedPaths,
334
+ compatibility: checkDshRuntimeCompatibility(base, expectedVersion),
335
+ };
336
+ }
337
+ }
338
+
339
+ // 2. 本包(pi-web-ui)的 node_modules
340
+ const pkgRoot = findLocalPackageRoot(HERE);
341
+ if (pkgRoot) {
342
+ probe(join(pkgRoot, "node_modules"), "local:packageRoot");
343
+ }
344
+ probe(join(resolve(HERE, "..", "..", ".."), "node_modules"), "local:relative-3");
345
+ probe(join(resolve(HERE, "..", "..", "..", ".."), "node_modules"), "local:relative-4");
346
+
347
+ // 3. execPath 邻近 node_modules
348
+ probe(join(dirname(process.execPath), "node_modules"), "execPath:adjacent");
349
+ probe(join(dirname(process.execPath), "..", "lib", "node_modules"), "execPath:parent-lib");
350
+ probe(join(dirname(process.execPath), "..", "node_modules"), "execPath:parent");
351
+
352
+ // 4. 从 $PATH 中探测到的候选
353
+ for (const cand of findCandidatesFromPath()) {
354
+ probe(cand.path, cand.source);
355
+ }
356
+
357
+ // 5. 从 npmrc 中探测到的 prefix
358
+ for (const p of findPrefixFromNpmrc()) {
359
+ probe(join(p, "lib", "node_modules"), "npmrc:prefix-lib");
360
+ probe(join(p, "node_modules"), "npmrc:prefix");
361
+ }
362
+
363
+ // 6. 环境变量里的前缀
364
+ const envPrefixes = [process.env.NPM_CONFIG_PREFIX, process.env.npm_config_prefix, process.env.PREFIX];
365
+ for (const p of envPrefixes) {
366
+ if (p) {
367
+ probe(join(p, "lib", "node_modules"), "env:prefix-lib");
368
+ probe(join(p, "node_modules"), "env:prefix");
369
+ }
370
+ }
371
+
372
+ // 7. 常见系统默认路径
373
+ if (process.platform === "win32") {
374
+ if (process.env.APPDATA) probe(join(process.env.APPDATA, "npm", "node_modules"), "default:win-appdata");
375
+ if (process.env.LOCALAPPDATA)
376
+ probe(join(process.env.LOCALAPPDATA, "npm", "node_modules"), "default:win-localappdata");
377
+ if (process.env.ProgramFiles)
378
+ probe(join(process.env.ProgramFiles, "nodejs", "node_modules"), "default:win-programfiles");
379
+ } else {
380
+ probe("/usr/local/lib/node_modules", "default:posix-usr-local");
381
+ probe("/usr/lib/node_modules", "default:posix-usr");
382
+ const home = homedir();
383
+ probe(join(home, ".local", "npm", "lib", "node_modules"), "default:posix-home-local-npm");
384
+ probe(join(home, ".local", "npm", "node_modules"), "default:posix-home-local-npm-flat");
385
+ probe(join(home, ".local", "lib", "node_modules"), "default:posix-home-local");
386
+ probe(join(home, ".npm-global", "lib", "node_modules"), "default:posix-npm-global");
387
+ probe(join(home, ".npm-global", "node_modules"), "default:posix-npm-global-flat");
388
+ }
389
+
390
+ // 若存在同版本族的有效树,优先选择;否则回退至首个可用树(由 launcher 报错指引版本)
391
+ const chosenBase = firstCompatibleBase || firstFoundBase || null;
392
+ const compatibility = chosenBase ? checkDshRuntimeCompatibility(chosenBase, expectedVersion) : null;
393
+
394
+ return {
395
+ base: chosenBase,
396
+ scannedPaths,
397
+ compatibility,
398
+ };
399
+ }
400
+
401
+ /** 供已有代码直接获取有效 runtimeBase 路径 */
402
+ export async function resolveRuntimeBase() {
403
+ const { base } = await resolveRuntimeBaseWithDetails();
404
+ return base;
405
+ }