pi-web-ui 0.106.0 → 0.108.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -11
- package/README.zh-TW.md +2 -2
- package/dist/server/agent-formatters.js +19 -4
- package/dist/server/agent-service.js +33 -16
- package/dist/server/bg-servers.js +205 -53
- package/dist/server/client-state.js +6 -2
- package/dist/server/dispatch-domain-handlers.js +2 -0
- package/dist/server/dsh/dsh-agent-service.js +24 -10
- package/dist/server/durable/durable-agent-service.js +153 -0
- package/dist/server/durable/durable-capabilities.js +11 -0
- package/dist/server/durable/durable-driver.js +481 -0
- package/dist/server/durable/durable-loader.js +38 -0
- package/dist/server/durable/durable-models.js +283 -0
- package/dist/server/durable/durable-storage.js +42 -0
- package/dist/server/durable/durable-tools.js +45 -0
- package/dist/server/durable/index.js +8 -0
- package/dist/server/engine-capabilities.js +20 -1
- package/dist/server/engine-probe.js +156 -8
- package/dist/server/files-service.js +15 -1
- package/dist/server/global-loader.js +335 -0
- package/dist/server/index.js +127 -17
- package/dist/server/managed.js +2 -0
- package/dist/server/marker-service.js +5 -19
- package/dist/server/markers/index.js +1 -3
- package/dist/server/model-admin.js +61 -10
- package/dist/server/model-resolver.js +91 -3
- package/dist/server/plugins.js +18 -1
- package/dist/server/preset-fields.js +14 -0
- package/dist/server/preset-share.js +2 -0
- package/dist/server/process-utils.js +114 -12
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/sdk-origin.js +2 -2
- package/dist/server/serialize.js +1 -1
- package/dist/server/settings-service.js +4 -0
- package/dist/server/text-sniff.js +2 -0
- package/dist/server/tool-manager.js +3 -19
- package/dist/server/universal/driver-base.js +4 -0
- package/dist/server/universal/drivers/claude-code.js +3 -2
- package/dist/server/universal/universal-service.js +19 -2
- package/dist/server/universal/universal-session.js +63 -7
- package/dist/server/update-check.js +20 -8
- package/package.json +2 -1
- package/plugins/catalog.json +13 -2
- package/themes/dark-teal.css +0 -1
- package/themes/flow-light.css +1085 -0
- package/themes/flow.css +1094 -0
- package/themes/wallpaper-flow.css +1158 -0
- package/themes/wallpaper-obsidian.css +50 -0
- package/themes/zhujian-dark.css +0 -7
- package/themes/zhujian.css +0 -7
- package/themes/zhupi-dark.css +0 -7
- package/themes/zhupi.css +0 -7
- package/web/dist/assets/{TerminalPanel-CfxKLDW9.js → TerminalPanel-CslRhcS7.js} +2 -2
- package/web/dist/assets/index-C_2Fl6f6.js +690 -0
- package/web/dist/assets/index-D0BtO5dn.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/index-BtcP6BDk.css +0 -1
- package/web/dist/assets/index-XVtCzcHR.js +0 -690
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* global-loader.ts — 通用全局环境动态解析与穿透加载器(Universal Global Package Loader)。
|
|
3
|
+
*
|
|
4
|
+
* 背景与架构意义:
|
|
5
|
+
* 在 Node.js ESM 规范中,裸模块说明符(如 import("foo"))仅支持沿当前项目的祖先链
|
|
6
|
+
* 回溯 node_modules,无法跨盘符或跨目录直接发现用户通过全局安装(npm i -g、pnpm add -g、
|
|
7
|
+
* fnm、nvm、volta、bun 等)的包。
|
|
8
|
+
*
|
|
9
|
+
* 本模块作为宿主级基础服务,提供一站式的:
|
|
10
|
+
* 1. 全版本管理器与包管理器全局目录自动嗅探(fnm / nvm / pnpm / volta / bun / npm);
|
|
11
|
+
* 2. 物理定位任意包根路径与包版本元数据(findPackageRoot / probeGlobalPackage);
|
|
12
|
+
* 3. 严格遵循 package.json 的 exports 条件导出规范解析入口文件;
|
|
13
|
+
* 4. 基于 file:// URL 协议跨环境穿透动态导入 ESM 模块(dynamicImportGlobalPackage)。
|
|
14
|
+
*/
|
|
15
|
+
import { execSync } from "node:child_process";
|
|
16
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
17
|
+
import { homedir } from "node:os";
|
|
18
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "node:path";
|
|
19
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
20
|
+
let cachedNpmRootG = null;
|
|
21
|
+
let cachedCandidateRoots = null;
|
|
22
|
+
const packageRootCache = new Map();
|
|
23
|
+
/**
|
|
24
|
+
* 安全且带缓存地获取系统 npm 全局 root 目录。
|
|
25
|
+
*/
|
|
26
|
+
export function getSystemNpmRootG() {
|
|
27
|
+
if (cachedNpmRootG !== null)
|
|
28
|
+
return cachedNpmRootG;
|
|
29
|
+
try {
|
|
30
|
+
const out = execSync("npm root -g", {
|
|
31
|
+
timeout: 2500,
|
|
32
|
+
encoding: "utf8",
|
|
33
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
34
|
+
shell: process.platform === "win32" ? "cmd.exe" : undefined,
|
|
35
|
+
windowsHide: true,
|
|
36
|
+
}).trim();
|
|
37
|
+
if (out && existsSync(out)) {
|
|
38
|
+
cachedNpmRootG = resolve(out);
|
|
39
|
+
return cachedNpmRootG;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
/* ignore */
|
|
44
|
+
}
|
|
45
|
+
cachedNpmRootG = null;
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 全方位收集系统所有可能存放全局或本地包的 node_modules 候选根目录。
|
|
50
|
+
* 覆盖:本地项目链、Node 邻近目录、fnm、nvm、pnpm、volta、yarn、bun、npm 全局 prefix 等。
|
|
51
|
+
*/
|
|
52
|
+
export function getCandidateNodeModulesRoots(options) {
|
|
53
|
+
if (!options?.refresh && cachedCandidateRoots !== null) {
|
|
54
|
+
return cachedCandidateRoots;
|
|
55
|
+
}
|
|
56
|
+
const candidates = new Set();
|
|
57
|
+
// 1. 本包内部与向上的祖先链路 node_modules
|
|
58
|
+
let cur = dirname(fileURLToPath(import.meta.url));
|
|
59
|
+
for (let i = 0; i < 6; i++) {
|
|
60
|
+
const nm = join(cur, "node_modules");
|
|
61
|
+
if (existsSync(nm))
|
|
62
|
+
candidates.add(resolve(nm));
|
|
63
|
+
const parent = dirname(cur);
|
|
64
|
+
if (parent === cur)
|
|
65
|
+
break;
|
|
66
|
+
cur = parent;
|
|
67
|
+
}
|
|
68
|
+
// 2. 当前工作区 process.cwd() 及其父目录链
|
|
69
|
+
let cwdCur = process.cwd();
|
|
70
|
+
for (let i = 0; i < 5; i++) {
|
|
71
|
+
const nm = join(cwdCur, "node_modules");
|
|
72
|
+
if (existsSync(nm))
|
|
73
|
+
candidates.add(resolve(nm));
|
|
74
|
+
const parent = dirname(cwdCur);
|
|
75
|
+
if (parent === cwdCur)
|
|
76
|
+
break;
|
|
77
|
+
cwdCur = parent;
|
|
78
|
+
}
|
|
79
|
+
// 3. 运行中 Node 解释器自身的邻近目录(process.execPath)
|
|
80
|
+
try {
|
|
81
|
+
const nodeDir = dirname(process.execPath);
|
|
82
|
+
candidates.add(resolve(join(nodeDir, "node_modules")));
|
|
83
|
+
candidates.add(resolve(join(nodeDir, "..", "node_modules")));
|
|
84
|
+
candidates.add(resolve(join(nodeDir, "..", "lib", "node_modules")));
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
/* ignore */
|
|
88
|
+
}
|
|
89
|
+
// 4. 环境变量 NODE_PATH 显式配置的目录
|
|
90
|
+
if (process.env.NODE_PATH) {
|
|
91
|
+
const parts = process.env.NODE_PATH.split(delimiter).filter(Boolean);
|
|
92
|
+
for (const p of parts) {
|
|
93
|
+
if (existsSync(p))
|
|
94
|
+
candidates.add(resolve(p));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// 5. 显式探测系统级 npm root -g
|
|
98
|
+
const npmGlobal = getSystemNpmRootG();
|
|
99
|
+
if (npmGlobal)
|
|
100
|
+
candidates.add(npmGlobal);
|
|
101
|
+
const env = process.env;
|
|
102
|
+
const home = env.HOME || env.USERPROFILE || homedir();
|
|
103
|
+
// 6. 版本管理器:fnm 自动嗅探
|
|
104
|
+
if (process.platform === "win32") {
|
|
105
|
+
const localAppData = env.LOCALAPPDATA;
|
|
106
|
+
if (localAppData) {
|
|
107
|
+
const fnmMultishells = join(localAppData, "fnm_multishells");
|
|
108
|
+
if (existsSync(fnmMultishells)) {
|
|
109
|
+
try {
|
|
110
|
+
const entries = readdirSync(fnmMultishells);
|
|
111
|
+
for (const entry of entries) {
|
|
112
|
+
const subNm = join(fnmMultishells, entry, "node_modules");
|
|
113
|
+
if (existsSync(subNm))
|
|
114
|
+
candidates.add(resolve(subNm));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
/* ignore */
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (env.APPDATA) {
|
|
123
|
+
const fnmCurrent = join(env.APPDATA, "fnm", "current", "node_modules");
|
|
124
|
+
if (existsSync(fnmCurrent))
|
|
125
|
+
candidates.add(resolve(fnmCurrent));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
const xdgData = env.XDG_DATA_HOME || join(home, ".local", "share");
|
|
130
|
+
const fnmCurrent = join(xdgData, "fnm", "current", "lib", "node_modules");
|
|
131
|
+
if (existsSync(fnmCurrent))
|
|
132
|
+
candidates.add(resolve(fnmCurrent));
|
|
133
|
+
}
|
|
134
|
+
// 7. 版本管理器:nvm 自动嗅探
|
|
135
|
+
if (process.platform === "win32") {
|
|
136
|
+
if (env.NVM_SYMLINK) {
|
|
137
|
+
candidates.add(resolve(join(env.NVM_SYMLINK, "node_modules")));
|
|
138
|
+
}
|
|
139
|
+
if (env.NVM_HOME && existsSync(env.NVM_HOME)) {
|
|
140
|
+
try {
|
|
141
|
+
const nvmVersions = readdirSync(env.NVM_HOME);
|
|
142
|
+
for (const v of nvmVersions) {
|
|
143
|
+
const subNm = join(env.NVM_HOME, v, "node_modules");
|
|
144
|
+
if (existsSync(subNm))
|
|
145
|
+
candidates.add(resolve(subNm));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
/* ignore */
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
const nvmDir = env.NVM_DIR || join(home, ".nvm");
|
|
155
|
+
const nvmVersionsDir = join(nvmDir, "versions", "node");
|
|
156
|
+
if (existsSync(nvmVersionsDir)) {
|
|
157
|
+
try {
|
|
158
|
+
const versions = readdirSync(nvmVersionsDir);
|
|
159
|
+
for (const v of versions) {
|
|
160
|
+
const subNm = join(nvmVersionsDir, v, "lib", "node_modules");
|
|
161
|
+
if (existsSync(subNm))
|
|
162
|
+
candidates.add(resolve(subNm));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
/* ignore */
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// 8. 包管理器:pnpm 全局目录嗅探
|
|
171
|
+
if (env.PNPM_HOME) {
|
|
172
|
+
candidates.add(resolve(join(env.PNPM_HOME, "node_modules")));
|
|
173
|
+
candidates.add(resolve(join(env.PNPM_HOME, "global", "5", "node_modules")));
|
|
174
|
+
}
|
|
175
|
+
if (home) {
|
|
176
|
+
const pnpmPosix = join(home, ".local", "share", "pnpm", "global", "5", "node_modules");
|
|
177
|
+
if (existsSync(pnpmPosix))
|
|
178
|
+
candidates.add(resolve(pnpmPosix));
|
|
179
|
+
}
|
|
180
|
+
if (env.LOCALAPPDATA) {
|
|
181
|
+
const pnpmWin = join(env.LOCALAPPDATA, "pnpm", "global", "5", "node_modules");
|
|
182
|
+
if (existsSync(pnpmWin))
|
|
183
|
+
candidates.add(resolve(pnpmWin));
|
|
184
|
+
}
|
|
185
|
+
// 9. 包管理器:npm / yarn / volta / bun 标准系统全局目录
|
|
186
|
+
const prefix = env.NPM_CONFIG_PREFIX || env.npm_config_prefix || env.PREFIX;
|
|
187
|
+
if (prefix) {
|
|
188
|
+
candidates.add(resolve(join(prefix, "lib", "node_modules")));
|
|
189
|
+
candidates.add(resolve(join(prefix, "node_modules")));
|
|
190
|
+
}
|
|
191
|
+
if (env.APPDATA) {
|
|
192
|
+
candidates.add(resolve(join(env.APPDATA, "npm", "node_modules")));
|
|
193
|
+
}
|
|
194
|
+
if (home) {
|
|
195
|
+
candidates.add(resolve(join(home, ".npm-global", "lib", "node_modules")));
|
|
196
|
+
candidates.add(resolve(join(home, ".npm-global", "node_modules")));
|
|
197
|
+
candidates.add(resolve(join(home, ".volta", "tools", "image", "packages")));
|
|
198
|
+
candidates.add(resolve(join(home, ".bun", "install", "global", "node_modules")));
|
|
199
|
+
}
|
|
200
|
+
if (process.platform !== "win32") {
|
|
201
|
+
candidates.add(resolve("/usr/local/lib/node_modules"));
|
|
202
|
+
candidates.add(resolve("/usr/lib/node_modules"));
|
|
203
|
+
}
|
|
204
|
+
cachedCandidateRoots = Array.from(candidates);
|
|
205
|
+
return cachedCandidateRoots;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* 在所有候选路径中精准物理定位某个 npm 包的真实安装根目录。
|
|
209
|
+
* 验证规则:目录存在且内部包含匹配的 package.json 文件。
|
|
210
|
+
*/
|
|
211
|
+
export function resolvePackageRoot(packageName, options) {
|
|
212
|
+
if (!options?.refresh && packageRootCache.has(packageName)) {
|
|
213
|
+
const cached = packageRootCache.get(packageName);
|
|
214
|
+
if (cached && existsSync(cached))
|
|
215
|
+
return cached;
|
|
216
|
+
}
|
|
217
|
+
const roots = getCandidateNodeModulesRoots(options);
|
|
218
|
+
const subpathParts = packageName.split("/");
|
|
219
|
+
for (const root of roots) {
|
|
220
|
+
const candidateDir = join(root, ...subpathParts);
|
|
221
|
+
const candidatePkgJson = join(candidateDir, "package.json");
|
|
222
|
+
if (existsSync(candidatePkgJson)) {
|
|
223
|
+
try {
|
|
224
|
+
const info = JSON.parse(readFileSync(candidatePkgJson, "utf8"));
|
|
225
|
+
if (info.name === packageName) {
|
|
226
|
+
const finalRoot = resolve(candidateDir);
|
|
227
|
+
packageRootCache.set(packageName, finalRoot);
|
|
228
|
+
return finalRoot;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
/* ignore corrupt json */
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
packageRootCache.set(packageName, null);
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* 探测某个全局或本地包的安装就绪状态与版本信息。
|
|
241
|
+
*/
|
|
242
|
+
export function probeGlobalPackage(packageName, options) {
|
|
243
|
+
const root = resolvePackageRoot(packageName, options);
|
|
244
|
+
if (!root) {
|
|
245
|
+
return { installed: false };
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
249
|
+
return {
|
|
250
|
+
installed: true,
|
|
251
|
+
version: pkg.version,
|
|
252
|
+
packagePath: root,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return { installed: true, packagePath: root };
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* 依据目标包的 package.json 中的 exports/main/module 规范,
|
|
261
|
+
* 将逻辑子路径解析为物理入口文件的绝对路径。
|
|
262
|
+
*/
|
|
263
|
+
export function resolvePackageEntryFile(packageName, subpath = "") {
|
|
264
|
+
const root = resolvePackageRoot(packageName);
|
|
265
|
+
if (!root)
|
|
266
|
+
return null;
|
|
267
|
+
try {
|
|
268
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
269
|
+
const exportKey = subpath ? `./${subpath}` : ".";
|
|
270
|
+
let targetRel;
|
|
271
|
+
if (pkg.exports && pkg.exports[exportKey]) {
|
|
272
|
+
const exp = pkg.exports[exportKey];
|
|
273
|
+
if (typeof exp === "string") {
|
|
274
|
+
targetRel = exp;
|
|
275
|
+
}
|
|
276
|
+
else if (exp && typeof exp === "object") {
|
|
277
|
+
targetRel = exp.import || exp.default || exp.node || exp.require;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
else if (!subpath) {
|
|
281
|
+
targetRel = pkg.main || pkg.module;
|
|
282
|
+
}
|
|
283
|
+
// 默认回落入口推断
|
|
284
|
+
if (!targetRel) {
|
|
285
|
+
const fallbacks = [
|
|
286
|
+
subpath ? `dist/${subpath}.js` : "dist/index.js",
|
|
287
|
+
subpath ? `dist/${subpath}/index.js` : "index.js",
|
|
288
|
+
subpath ? `${subpath}.js` : "index.js",
|
|
289
|
+
];
|
|
290
|
+
for (const fb of fallbacks) {
|
|
291
|
+
const cand = join(root, fb);
|
|
292
|
+
if (existsSync(cand)) {
|
|
293
|
+
targetRel = fb;
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (targetRel) {
|
|
299
|
+
const full = isAbsolute(targetRel) ? targetRel : resolve(root, targetRel);
|
|
300
|
+
if (existsSync(full))
|
|
301
|
+
return full;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
/* ignore */
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* 终极通用动态穿透加载器:
|
|
311
|
+
* 1. 优先尝试本地裸模块名说明符原生 import;
|
|
312
|
+
* 2. 失败后穿透使用全局候选路径定位物理入口,并通过 file:// 协议动态导入。
|
|
313
|
+
*
|
|
314
|
+
* @param packageName 目标包名(如 "@earendil-works/pi-durable" 或 "@earendil-works/chord")
|
|
315
|
+
* @param subpath 子导出路径(如 "tools" 或 "storage/sqlite/node" 或 "context")
|
|
316
|
+
*/
|
|
317
|
+
export async function dynamicImportGlobalPackage(packageName, subpath = "") {
|
|
318
|
+
const specifier = subpath ? `${packageName}/${subpath}` : packageName;
|
|
319
|
+
// 1. 优先尝试本地 Node.js ESM 规范解析
|
|
320
|
+
try {
|
|
321
|
+
const mod = await import(/* webpackIgnore: true */ specifier);
|
|
322
|
+
if (mod)
|
|
323
|
+
return mod;
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
/* fall through to global physical loader */
|
|
327
|
+
}
|
|
328
|
+
// 2. 自动穿透全局候选目录定位入口
|
|
329
|
+
const entryFile = resolvePackageEntryFile(packageName, subpath);
|
|
330
|
+
if (!entryFile) {
|
|
331
|
+
throw new Error(`无法加载模块 "${specifier}":未在本地或全局环境找到匹配的包安装或有效入口文件。`);
|
|
332
|
+
}
|
|
333
|
+
const fileUrl = pathToFileURL(entryFile).href;
|
|
334
|
+
return import(/* webpackIgnore: true */ fileUrl);
|
|
335
|
+
}
|
package/dist/server/index.js
CHANGED
|
@@ -23,9 +23,8 @@ import { existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
|
23
23
|
import { stat } from "node:fs/promises";
|
|
24
24
|
import { createServer, request as proxyRequest } from "node:http";
|
|
25
25
|
import { createConnection } from "node:net";
|
|
26
|
-
import { basename, delimiter,
|
|
26
|
+
import { basename, delimiter, join, resolve, sep } from "node:path";
|
|
27
27
|
import { homedir } from "node:os";
|
|
28
|
-
import { fileURLToPath } from "node:url";
|
|
29
28
|
import { createHash, randomUUID, timingSafeEqual } from "node:crypto";
|
|
30
29
|
import express from "express";
|
|
31
30
|
import compression from "compression";
|
|
@@ -57,7 +56,7 @@ import { syncPluginCatalog } from "./plugin-catalog-sync.js";
|
|
|
57
56
|
import { McpBridge } from "./mcp-bridge.js";
|
|
58
57
|
import { createMcpHotReload } from "./mcp-hot-reload.js";
|
|
59
58
|
import { createHostMetricsSampler } from "./host-metrics.js";
|
|
60
|
-
import { SchedulerStore
|
|
59
|
+
import { SchedulerStore } from "./scheduler-tasks.js";
|
|
61
60
|
import { initHttpProxy } from "./http-proxy.js";
|
|
62
61
|
import { globalLspPool } from "./lsp-tool.js";
|
|
63
62
|
import { buildPiWebTokenCookie, decodeCookieToken, isTlsRequest } from "./auth-cookie.js";
|
|
@@ -341,17 +340,18 @@ if (AUTH_TOKEN) {
|
|
|
341
340
|
}
|
|
342
341
|
/** 共享客户端持久化存储:双引擎(pi + dsh)共用同一份内存缓存与落盘通道,避免互相覆盖。 */
|
|
343
342
|
const sharedStateStore = new ClientStateStore(join(DATA_DIR, "client-state.json"));
|
|
344
|
-
/** 启动参数显式指定的引擎(--engine pi|dsh|claude 或 PI_WEB_ENGINE)。未显式传则优先读持久化默认引擎。 */
|
|
343
|
+
/** 启动参数显式指定的引擎(--engine pi|dsh|claude|durable 或 PI_WEB_ENGINE)。未显式传则优先读持久化默认引擎。 */
|
|
345
344
|
const EXPLICIT_ENGINE_ARG = cliFlag("--engine") ?? process.env.PI_WEB_ENGINE;
|
|
346
|
-
let currentDefaultEngine = EXPLICIT_ENGINE_ARG === "
|
|
347
|
-
? "
|
|
348
|
-
: EXPLICIT_ENGINE_ARG === "
|
|
349
|
-
? "
|
|
350
|
-
: EXPLICIT_ENGINE_ARG === "
|
|
351
|
-
? "
|
|
352
|
-
:
|
|
345
|
+
let currentDefaultEngine = EXPLICIT_ENGINE_ARG === "durable"
|
|
346
|
+
? "durable"
|
|
347
|
+
: EXPLICIT_ENGINE_ARG === "claude"
|
|
348
|
+
? "claude"
|
|
349
|
+
: EXPLICIT_ENGINE_ARG === "dsh"
|
|
350
|
+
? "dsh"
|
|
351
|
+
: EXPLICIT_ENGINE_ARG === "pi"
|
|
352
|
+
? "pi"
|
|
353
|
+
: (sharedStateStore.getDefaultEngine() ?? "pi");
|
|
353
354
|
/** 默认引擎标识(兼容 /api/health 等读取)。 */
|
|
354
|
-
const ENGINE = currentDefaultEngine;
|
|
355
355
|
/** 每个客户端(浏览器标签页 clientId)当前激活的引擎。支持多标签页同时跑不同引擎 + 零重启热切。 */
|
|
356
356
|
const clientEngines = new Map();
|
|
357
357
|
function resolveClientEngine(clientId) {
|
|
@@ -607,7 +607,6 @@ app.get("/api/preview/*splat", async (req, res) => {
|
|
|
607
607
|
// Production: serve the built frontend from web/dist. Resolve relative to this
|
|
608
608
|
// module so it works when installed as a package (global/npx/Docker), not just
|
|
609
609
|
// from the repo root. In dev, Vite serves the UI on :5173 and proxies /ws.
|
|
610
|
-
const here = dirname(fileURLToPath(import.meta.url)); // <pkg>/dist/server or <pkg>/server
|
|
611
610
|
// Resolve the package root robustly: dev runs from <repo>/server (tsx), prod
|
|
612
611
|
// from <pkg>/dist/server — the ancestor that actually has package.json wins.
|
|
613
612
|
function resolvePkgRoot() {
|
|
@@ -1089,13 +1088,16 @@ const heartbeatTimer = setInterval(() => {
|
|
|
1089
1088
|
heartbeatTimer.unref?.();
|
|
1090
1089
|
// 引擎分发:PI_WEB_ENGINE=dsh 时使用 DeepSeek Harness 引擎(server/dsh/),
|
|
1091
1090
|
// PI_WEB_ENGINE=claude 时使用通用底座下的 Claude Code 驱动(server/universal/),
|
|
1091
|
+
// PI_WEB_ENGINE=durable 时使用 Pi Durable 引擎(server/durable/),
|
|
1092
1092
|
// 默认 pi 引擎。同一 wire 协议,前端无感知(ready/health 携带 engine 字段)。
|
|
1093
1093
|
import { DshAgentService } from "./dsh/dsh-agent-service.js";
|
|
1094
1094
|
import { UniversalAgentService, ClaudeCodeDriver, probeClaudeCode } from "./universal/index.js";
|
|
1095
|
-
import {
|
|
1095
|
+
import { DurableAgentService } from "./durable/index.js";
|
|
1096
|
+
import { probeAllEngines, probeEngine, getCachedEngineStatuses, updateEngineBackground, isEngineUpdating, ENGINE_PACKAGES, } from "./engine-probe.js";
|
|
1096
1097
|
let piServiceInstance = null;
|
|
1097
1098
|
let dshServiceInstance = null;
|
|
1098
1099
|
let claudeServiceInstance = null;
|
|
1100
|
+
let durableServiceInstance = null;
|
|
1099
1101
|
let wireEngineHooks = null;
|
|
1100
1102
|
let sharedSocketCount = 0;
|
|
1101
1103
|
let sharedQuiesced = false;
|
|
@@ -1107,9 +1109,27 @@ function activeEngines() {
|
|
|
1107
1109
|
list.push(dshServiceInstance);
|
|
1108
1110
|
if (claudeServiceInstance)
|
|
1109
1111
|
list.push(claudeServiceInstance);
|
|
1112
|
+
if (durableServiceInstance)
|
|
1113
|
+
list.push(durableServiceInstance);
|
|
1110
1114
|
return list;
|
|
1111
1115
|
}
|
|
1112
1116
|
function ensureEngine(engine) {
|
|
1117
|
+
if (engine === "durable") {
|
|
1118
|
+
if (!durableServiceInstance) {
|
|
1119
|
+
durableServiceInstance = new DurableAgentService({
|
|
1120
|
+
cwd: CWD,
|
|
1121
|
+
dataDir: DATA_DIR,
|
|
1122
|
+
agentDir: getAgentDir(),
|
|
1123
|
+
stateStore: sharedStateStore,
|
|
1124
|
+
});
|
|
1125
|
+
for (let i = 0; i < sharedSocketCount; i++)
|
|
1126
|
+
durableServiceInstance.noteSocketOpen();
|
|
1127
|
+
if (sharedQuiesced)
|
|
1128
|
+
durableServiceInstance.quiesce();
|
|
1129
|
+
wireEngineHooks?.(durableServiceInstance);
|
|
1130
|
+
}
|
|
1131
|
+
return durableServiceInstance;
|
|
1132
|
+
}
|
|
1113
1133
|
if (engine === "claude") {
|
|
1114
1134
|
if (!claudeServiceInstance) {
|
|
1115
1135
|
claudeServiceInstance = new UniversalAgentService({
|
|
@@ -1932,13 +1952,21 @@ wss.on("connection", (ws) => {
|
|
|
1932
1952
|
.catch(() => { });
|
|
1933
1953
|
};
|
|
1934
1954
|
const handleSwitchEngine = async (msg) => {
|
|
1935
|
-
const targetEngine = msg.engine === "
|
|
1955
|
+
const targetEngine = msg.engine === "durable"
|
|
1956
|
+
? "durable"
|
|
1957
|
+
: msg.engine === "claude"
|
|
1958
|
+
? "claude"
|
|
1959
|
+
: msg.engine === "dsh"
|
|
1960
|
+
? "dsh"
|
|
1961
|
+
: msg.engine === "pi"
|
|
1962
|
+
? "pi"
|
|
1963
|
+
: null;
|
|
1936
1964
|
if (!targetEngine) {
|
|
1937
1965
|
send({
|
|
1938
1966
|
type: "notice",
|
|
1939
1967
|
level: "error",
|
|
1940
|
-
text: `无效的引擎类型:${String(msg.engine)}(仅支持 pi / dsh / claude)`,
|
|
1941
|
-
textEn: `Invalid engine "${String(msg.engine)}" (supported: pi, dsh, claude)`,
|
|
1968
|
+
text: `无效的引擎类型:${String(msg.engine)}(仅支持 pi / dsh / claude / durable)`,
|
|
1969
|
+
textEn: `Invalid engine "${String(msg.engine)}" (supported: pi, dsh, claude, durable)`,
|
|
1942
1970
|
});
|
|
1943
1971
|
return;
|
|
1944
1972
|
}
|
|
@@ -2041,6 +2069,84 @@ wss.on("connection", (ws) => {
|
|
|
2041
2069
|
});
|
|
2042
2070
|
});
|
|
2043
2071
|
};
|
|
2072
|
+
const handleUpdateEngine = async (msg) => {
|
|
2073
|
+
const targetEngine = String(msg.engineId || "").trim();
|
|
2074
|
+
const meta = ENGINE_PACKAGES[targetEngine];
|
|
2075
|
+
if (!meta || !meta.packageName) {
|
|
2076
|
+
send({
|
|
2077
|
+
type: "notice",
|
|
2078
|
+
level: "error",
|
|
2079
|
+
text: `不支持或未知的引擎「${targetEngine}」`,
|
|
2080
|
+
textEn: `Unsupported or unknown engine "${targetEngine}"`,
|
|
2081
|
+
});
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
const refusal = managedRefusal("update_engine", MANAGED);
|
|
2085
|
+
if (refusal) {
|
|
2086
|
+
send({ type: "notice", level: "error", text: refusal });
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
if (isEngineUpdating(targetEngine)) {
|
|
2090
|
+
send({
|
|
2091
|
+
type: "notice",
|
|
2092
|
+
level: "warning",
|
|
2093
|
+
text: `引擎「${targetEngine.toUpperCase()}」正在后台更新中,请稍候…`,
|
|
2094
|
+
textEn: `Engine "${targetEngine.toUpperCase()}" is already updating, please wait…`,
|
|
2095
|
+
});
|
|
2096
|
+
return;
|
|
2097
|
+
}
|
|
2098
|
+
const engineDisplayNames = {
|
|
2099
|
+
pi: { zh: "Pi", en: "Pi" },
|
|
2100
|
+
dsh: { zh: "DSH", en: "DSH" },
|
|
2101
|
+
claude: { zh: "Claude Code", en: "Claude Code" },
|
|
2102
|
+
durable: { zh: "Pi Durable", en: "Pi Durable" },
|
|
2103
|
+
};
|
|
2104
|
+
const displayName = engineDisplayNames[targetEngine] ?? {
|
|
2105
|
+
zh: targetEngine.toUpperCase(),
|
|
2106
|
+
en: targetEngine.toUpperCase(),
|
|
2107
|
+
};
|
|
2108
|
+
const broadcastStatuses = (list) => {
|
|
2109
|
+
const payload = JSON.stringify({ type: "engines_status", engines: list });
|
|
2110
|
+
for (const c of wss.clients) {
|
|
2111
|
+
if (c.readyState === WebSocket.OPEN) {
|
|
2112
|
+
try {
|
|
2113
|
+
c.send(payload);
|
|
2114
|
+
}
|
|
2115
|
+
catch {
|
|
2116
|
+
/* ignore */
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
};
|
|
2121
|
+
send({
|
|
2122
|
+
type: "notice",
|
|
2123
|
+
level: "info",
|
|
2124
|
+
text: `正在后台更新 ${displayName.zh} 引擎(npm i -g ${meta.packageName}@latest)…`,
|
|
2125
|
+
textEn: `Updating ${displayName.en} engine in background (npm i -g ${meta.packageName}@latest)…`,
|
|
2126
|
+
});
|
|
2127
|
+
const updatePromise = updateEngineBackground(targetEngine);
|
|
2128
|
+
broadcastStatuses(getCachedEngineStatuses());
|
|
2129
|
+
const res = await updatePromise;
|
|
2130
|
+
const freshEngines = await probeAllEngines(true);
|
|
2131
|
+
broadcastStatuses(freshEngines);
|
|
2132
|
+
if (res.ok) {
|
|
2133
|
+
send({
|
|
2134
|
+
type: "notice",
|
|
2135
|
+
level: "info",
|
|
2136
|
+
text: `✅ ${displayName.zh} 引擎更新完成`,
|
|
2137
|
+
textEn: `✅ ${displayName.en} engine updated successfully`,
|
|
2138
|
+
});
|
|
2139
|
+
}
|
|
2140
|
+
else {
|
|
2141
|
+
const detail = res.error || (res.output ? res.output.slice(-200) : "");
|
|
2142
|
+
send({
|
|
2143
|
+
type: "notice",
|
|
2144
|
+
level: "error",
|
|
2145
|
+
text: `❌ ${displayName.zh} 引擎更新失败${detail ? `:${detail}` : ""}`,
|
|
2146
|
+
textEn: `❌ Failed to update ${displayName.en} engine${detail ? `: ${detail}` : ""}`,
|
|
2147
|
+
});
|
|
2148
|
+
}
|
|
2149
|
+
};
|
|
2044
2150
|
const dispatch = (msg) => {
|
|
2045
2151
|
if (!clientId) {
|
|
2046
2152
|
pending.push(msg);
|
|
@@ -2051,6 +2157,10 @@ wss.on("connection", (ws) => {
|
|
|
2051
2157
|
void handleSwitchEngine(msg);
|
|
2052
2158
|
return;
|
|
2053
2159
|
}
|
|
2160
|
+
if (msg.type === "update_engine") {
|
|
2161
|
+
void handleUpdateEngine(msg);
|
|
2162
|
+
return;
|
|
2163
|
+
}
|
|
2054
2164
|
if (msg.type === "check_engines") {
|
|
2055
2165
|
void probeAllEngines(msg.force).then((engines) => {
|
|
2056
2166
|
if (!closed)
|
package/dist/server/managed.js
CHANGED
|
@@ -31,6 +31,8 @@ export const MANAGED_MESSAGES = [
|
|
|
31
31
|
"check_updates_all",
|
|
32
32
|
/** Installs the pi agent itself. */
|
|
33
33
|
"install_pi_agent",
|
|
34
|
+
/** Updates an agent engine package via background process. */
|
|
35
|
+
"update_engine",
|
|
34
36
|
/** Plugin market: fetches and installs a plugin from the network. */
|
|
35
37
|
"plugin_catalog_add",
|
|
36
38
|
/** Plugin install/update/uninstall run as a background job (issue #152). */
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
import { pick } from "./i18n.js";
|
|
5
5
|
import { ensureMarkersRegistered, parseMarkers, getMarker, allMarkers, collectGuidance, listMarkerNames, } from "./markers/index.js";
|
|
6
6
|
import { loadStateFromBranch, appendSnapshot } from "./markers/store.js";
|
|
7
|
-
import { TODO_NAMESPACE, initTodoState } from "./markers/builtins/todo.js";
|
|
8
7
|
import { ACTION_NAMESPACE, initActionState } from "./markers/builtins/action.js";
|
|
9
8
|
ensureMarkersRegistered();
|
|
10
9
|
const _DEFAULT_MARKERS = {
|
|
@@ -51,11 +50,11 @@ export class MarkerService {
|
|
|
51
50
|
// place them (a static table value could never inline them).
|
|
52
51
|
const linesText = lines.join("\n");
|
|
53
52
|
return pick(this.lang(), "\n\n────────── 内联标记工具(重要)──────────\n" +
|
|
54
|
-
"
|
|
53
|
+
"状态类操作(操作建议、对话重命名、通知)请直接写在回答正文里,采用内联标记语法,而不要调用对应工具。\n" +
|
|
55
54
|
"插件会替你执行并把标记从展示文本中移除,不会中断你的回答。\n\n" +
|
|
56
55
|
linesText +
|
|
57
56
|
"\n──────────\n", "\n\n────────── Inline marker tools (important) ──────────\n" +
|
|
58
|
-
"Write status operations (
|
|
57
|
+
"Write status operations (action suggestions, conversation renaming, notifications) directly in your reply text using inline marker syntax — do not call a tool for them.\n" +
|
|
59
58
|
"The plugin executes them and strips the markers from the displayed text without interrupting your reply.\n\n" +
|
|
60
59
|
linesText +
|
|
61
60
|
"\n──────────\n", "markers.service.guidance.frame", { lines: linesText });
|
|
@@ -160,9 +159,7 @@ export class MarkerService {
|
|
|
160
159
|
let st = states.get(canonicalNs);
|
|
161
160
|
if (st !== undefined)
|
|
162
161
|
return st;
|
|
163
|
-
if (canonicalNs ===
|
|
164
|
-
st = this.getState(conversationId, canonicalNs, initTodoState);
|
|
165
|
-
else if (canonicalNs === ACTION_NAMESPACE)
|
|
162
|
+
if (canonicalNs === ACTION_NAMESPACE)
|
|
166
163
|
st = this.getState(conversationId, canonicalNs, initActionState);
|
|
167
164
|
else {
|
|
168
165
|
const marker = getMarker(canonicalNs);
|
|
@@ -200,7 +197,7 @@ export class MarkerService {
|
|
|
200
197
|
};
|
|
201
198
|
}
|
|
202
199
|
if (result.applied) {
|
|
203
|
-
//
|
|
200
|
+
// action 落库;notify/conv 即时生效(通知已发 / 对话已重命名),无需快照。
|
|
204
201
|
const canonicalTool = token.tool === "suggest" ? ACTION_NAMESPACE : token.tool;
|
|
205
202
|
if (canonicalTool !== "notify" && canonicalTool !== "conv") {
|
|
206
203
|
dirty.add(canonicalTool);
|
|
@@ -247,9 +244,7 @@ export class MarkerService {
|
|
|
247
244
|
if (!m.overlay)
|
|
248
245
|
continue;
|
|
249
246
|
let state;
|
|
250
|
-
if (m.name ===
|
|
251
|
-
state = this.getState(conversationId, m.name, initTodoState);
|
|
252
|
-
else if (m.name === ACTION_NAMESPACE)
|
|
247
|
+
if (m.name === ACTION_NAMESPACE)
|
|
253
248
|
state = this.getState(conversationId, m.name, initActionState);
|
|
254
249
|
else {
|
|
255
250
|
state = this.getState(conversationId, m.name, () => m.init?.() ?? {});
|
|
@@ -269,16 +264,7 @@ export class MarkerService {
|
|
|
269
264
|
}
|
|
270
265
|
return lines;
|
|
271
266
|
}
|
|
272
|
-
describe(conversationId, tool, includeDeleted = false) {
|
|
273
|
-
const st = this.getState(conversationId, TODO_NAMESPACE, initTodoState);
|
|
274
|
-
const visible = st.tasks.filter((t) => includeDeleted || t.status !== "deleted");
|
|
275
|
-
if (visible.length === 0)
|
|
276
|
-
return pick(this.lang(), "[todo] (空)", "[todo] (empty)", "markers.service.describe.empty");
|
|
277
|
-
return visible.map((t) => `[${t.status}] #${t.id}: ${t.subject}`).join("\n");
|
|
278
|
-
}
|
|
279
267
|
getRawState(conversationId, namespace) {
|
|
280
|
-
if (namespace === TODO_NAMESPACE)
|
|
281
|
-
return this.getState(conversationId, namespace, initTodoState);
|
|
282
268
|
if (namespace === ACTION_NAMESPACE || namespace === "suggest")
|
|
283
269
|
return this.getState(conversationId, ACTION_NAMESPACE, initActionState);
|
|
284
270
|
const m = getMarker(namespace);
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* markers/index.ts — 聚合内置标记并提供便捷初始化。
|
|
3
3
|
*/
|
|
4
4
|
import { registerMarker } from "./registry.js";
|
|
5
|
-
import { todoMarker } from "./builtins/todo.js";
|
|
6
5
|
import { notifyMarker } from "./builtins/notify.js";
|
|
7
6
|
import { renameMarker } from "./builtins/rename.js";
|
|
8
7
|
import { actionMarker, suggestMarker } from "./builtins/action.js";
|
|
@@ -10,14 +9,13 @@ let initialized = false;
|
|
|
10
9
|
export function ensureMarkersRegistered() {
|
|
11
10
|
if (initialized)
|
|
12
11
|
return;
|
|
13
|
-
registerMarker(todoMarker);
|
|
14
12
|
registerMarker(notifyMarker);
|
|
15
13
|
registerMarker(renameMarker);
|
|
16
14
|
registerMarker(actionMarker);
|
|
17
15
|
registerMarker(suggestMarker);
|
|
18
16
|
initialized = true;
|
|
19
17
|
}
|
|
20
|
-
export {
|
|
18
|
+
export { notifyMarker, renameMarker, actionMarker, suggestMarker };
|
|
21
19
|
export * from "./marker.js";
|
|
22
20
|
export * from "./registry.js";
|
|
23
21
|
export * from "./store.js";
|