pi-web-ui 0.119.0 → 0.120.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 +79 -0
- package/bin/hub-upgrade.mjs +33 -15
- package/bin/lib/atomic-file.mjs +90 -0
- package/dist/server/agent-service-daemon.js +44 -10
- package/dist/server/agent-service.js +803 -106
- package/dist/server/atomic-file.js +130 -5
- package/dist/server/bash-timeout.js +15 -0
- package/dist/server/bash-tool.js +68 -4
- package/dist/server/bg-servers.js +19 -13
- package/dist/server/canvas/canvas-dispatch.js +12 -1
- package/dist/server/client-state.js +56 -14
- package/dist/server/conversation-tool.js +3 -2
- package/dist/server/delegate-task.js +8 -8
- package/dist/server/device-center/group-router-runtime.js +2 -0
- package/dist/server/device-center/hub-upgrade.js +5 -10
- package/dist/server/dispatch-domain-handlers.js +96 -22
- package/dist/server/dispatch-plugin-handlers.js +10 -0
- package/dist/server/dsh/dsh-agent-service.js +9 -9
- package/dist/server/durable/durable-agent-service.js +1 -1
- package/dist/server/files-service.js +41 -19
- package/dist/server/fs-copy.js +93 -0
- package/dist/server/goal-review-gate.js +2 -2
- package/dist/server/index.js +15 -0
- package/dist/server/job-runner.js +345 -0
- package/dist/server/marker-service.js +42 -26
- package/dist/server/model-session.js +16 -0
- package/dist/server/phone-tool.js +167 -0
- package/dist/server/plan-mode.js +2 -2
- package/dist/server/plugin-api-catalog.js +1 -1
- package/dist/server/plugin-bundled.js +35 -28
- package/dist/server/plugin-installer.js +15 -0
- package/dist/server/plugin-manifest-validate.js +10 -0
- package/dist/server/plugin-materialize.js +161 -0
- package/dist/server/plugin-paths.js +60 -0
- package/dist/server/plugin-removal.js +16 -3
- package/dist/server/plugin-schedule.js +8 -7
- package/dist/server/plugin-secrets.js +7 -3
- package/dist/server/plugin-updater.js +2 -4
- package/dist/server/plugins.js +74 -38
- package/dist/server/preset-fields.js +9 -9
- package/dist/server/process-utils.js +11 -0
- package/dist/server/project-scripts.js +38 -0
- package/dist/server/prompt-composer.js +40 -21
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/quick-ask.js +3 -0
- package/dist/server/remote-ssh-service.js +39 -0
- package/dist/server/scm-commitmsg.js +2 -1
- package/dist/server/serialize.js +8 -0
- package/dist/server/session-list-index.js +246 -0
- package/dist/server/session-list-scan.js +5 -0
- package/dist/server/session-parser.js +16 -0
- package/dist/server/settings-service.js +39 -6
- package/dist/server/subagent-budget.js +68 -0
- package/dist/server/subagent-evidence.js +297 -0
- package/dist/server/subagent-helpers.js +12 -0
- package/dist/server/subagent-ledger.js +401 -0
- package/dist/server/subagent-notify.js +6 -2
- package/dist/server/subagent-security-gate.js +2 -2
- package/dist/server/subagent-settle.js +71 -0
- package/dist/server/subagents.js +497 -79
- package/dist/server/tabs.js +8 -1
- package/dist/server/terminals.js +0 -1
- package/dist/server/tool-manager.js +90 -16
- package/dist/server/tool-prompts.js +5 -1
- package/dist/server/universal/universal-service.js +1 -1
- package/dist/server/universal/universal-session.js +45 -7
- package/dist/server/vision-bridge.js +2 -0
- package/dist/server/workspace-handle.js +119 -17
- package/dist/server/workspace-queue.js +37 -0
- package/docs/README.md +25 -23
- package/docs/architecture-plugins.md +23 -3
- package/docs/device-center.md +1 -1
- package/package.json +15 -3
- package/plugins/catalog.json +13 -2
- package/themes/bases/flow-base.css +7 -16
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/BgTasksModal-BdOJ19Hj.js +1 -0
- package/web/dist/assets/BgTasksModal-SAH2XCkY.js +1 -0
- package/web/dist/assets/{CanvasStudio-2ydYbnpW.js → CanvasStudio-BB_Iq7pl.js} +3 -3
- package/web/dist/assets/{CanvasStudio-DizWvRjn.js → CanvasStudio-DU6O3N4u.js} +3 -3
- package/web/dist/assets/DeviceCenter-By1zVTSY.js +3 -0
- package/web/dist/assets/DeviceCenter-Cnm0Tjrx.js +3 -0
- package/web/dist/assets/{DeviceExecutionSettings-B7R9ONaq.js → DeviceExecutionSettings-qJzbI0Cf.js} +1 -1
- package/web/dist/assets/{DeviceGroups-DFU4cv5b.js → DeviceGroups-DPrQxW6B.js} +1 -1
- package/web/dist/assets/{DeviceGroups-DoaQcxKj.js → DeviceGroups-DoucyUjI.js} +1 -1
- package/web/dist/assets/{Dropdown-Btjt6Dzh.js → Dropdown-kj_NIj6e.js} +1 -1
- package/web/dist/assets/FilePreview-Bub8Y9uq.js +2 -0
- package/web/dist/assets/FilePreview-CC76Z6an.js +2 -0
- package/web/dist/assets/GlobalSearchModal-01nkgw4X.js +1 -0
- package/web/dist/assets/GlobalSearchModal-CiT6wBmR.js +1 -0
- package/web/dist/assets/{GroupConversation-DwZZrMuE.js → GroupConversation-1-lsPO1f.js} +3 -3
- package/web/dist/assets/{GroupConversation-KOi8sa5b.js → GroupConversation-HeiyW_O_.js} +3 -3
- package/web/dist/assets/{GroupWorkItems-FqAL74bC.js → GroupWorkItems-BYJv2XFj.js} +1 -1
- package/web/dist/assets/{HintTip-BOwPzBGR.js → HintTip-DFhoclDw.js} +1 -1
- package/web/dist/assets/{ModelConfigModal-DLIfWKdB.js → ModelConfigModal-D2Ro-jUZ.js} +2 -2
- package/web/dist/assets/{ModelConfigModal-B140kWrs.js → ModelConfigModal-DPc9R1pq.js} +2 -2
- package/web/dist/assets/{NativeCliPanel-BwiLazSq.js → NativeCliPanel-B108d697.js} +3 -3
- package/web/dist/assets/{NativeCliPanel-U0F_EfAU.js → NativeCliPanel-DupvGwQM.js} +3 -3
- package/web/dist/assets/{PiSetupModal-Dc7EQ4L3.js → PiSetupModal-D0HitBfd.js} +1 -1
- package/web/dist/assets/{ProviderOAuthControls-C3opmPhu.js → ProviderOAuthControls-BLKUEDde.js} +1 -1
- package/web/dist/assets/{SCMPanel-BBxQfmq9.js → SCMPanel-DDbWrUHR.js} +1 -1
- package/web/dist/assets/{SCMPanel-D2t97Vvr.js → SCMPanel-DluNcd2-.js} +1 -1
- package/web/dist/assets/SettingsModal-Cy5lRC97.css +1 -0
- package/web/dist/assets/SettingsModal-DQu18366.js +33 -0
- package/web/dist/assets/SettingsModal-Dm_haPme.js +33 -0
- package/web/dist/assets/{TermXterm-B6nCtO2b.js → TermXterm-SYc-MUk2.js} +1 -1
- package/web/dist/assets/{TerminalPanel-BWB3iSeN.js → TerminalPanel-CgA_lhVT.js} +1 -1
- package/web/dist/assets/{TerminalPanel-CAV2SosY.js → TerminalPanel-vdSIOPjD.js} +1 -1
- package/web/dist/assets/{TuiOverlayModal-B0jBPOMc.js → TuiOverlayModal-Cy1jYYIw.js} +1 -1
- package/web/dist/assets/{TuiOverlayModal-DGd6fzU6.js → TuiOverlayModal-kXY_4hV8.js} +1 -1
- package/web/dist/assets/{app-globals-CR8Ls9nE.js → app-globals-CBEscsYP.js} +9 -9
- package/web/dist/assets/{canvas-client-B0lkqTtF.js → canvas-client-Clsypfs6.js} +1 -1
- package/web/dist/assets/{desktop-BME3DkyE.js → desktop-C6D9a8HZ.js} +1 -1
- package/web/dist/assets/index-BD29OL9L.js +404 -0
- package/web/dist/assets/index-BlCxbqqr.js +404 -0
- package/web/dist/assets/index-DB55rbKC.css +1 -0
- package/web/dist/assets/{plugin-host-CVgcdBOk.js → plugin-host-VWBcq-LQ.js} +1 -1
- package/web/dist/assets/remote-ssh-client-C2eab5Kw.js +1 -0
- package/web/dist/assets/theme-CpQk_Agp.js +1 -0
- package/web/dist/index.html +10 -10
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/BgTasksModal-CVHW1lRA.js +0 -1
- package/web/dist/assets/BgTasksModal-t5hxW1H6.js +0 -1
- package/web/dist/assets/DeviceCenter-CPaZNBJE.js +0 -3
- package/web/dist/assets/DeviceCenter-DsVakAYR.js +0 -3
- package/web/dist/assets/FilePreview-QGQfD2lV.js +0 -2
- package/web/dist/assets/FilePreview-qez9walH.js +0 -2
- package/web/dist/assets/GlobalSearchModal-B-cnTDJ5.js +0 -1
- package/web/dist/assets/GlobalSearchModal-BvbafjdU.js +0 -1
- package/web/dist/assets/SettingsModal-BqPQNUwF.js +0 -34
- package/web/dist/assets/SettingsModal-l7eeV-Rq.js +0 -34
- package/web/dist/assets/index-BBAQNi6v.js +0 -404
- package/web/dist/assets/index-BYW9d61A.js +0 -404
- package/web/dist/assets/index-BYWs88H2.css +0 -1
- package/web/dist/assets/remote-ssh-client-DEpvwgqG.js +0 -1
- package/web/dist/assets/theme-DVa7Z5Zu.js +0 -1
|
@@ -1,48 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
2
|
import { dirname, join } from "node:path";
|
|
3
3
|
import { writeJsonAtomicSync } from "./atomic-file.js";
|
|
4
4
|
import { BUILTIN_MODULE_IDS, isBuiltinModule } from "./builtin-modules.js";
|
|
5
|
+
import { isMaterialized, materializePlugin } from "./plugin-materialize.js";
|
|
5
6
|
const ID_RE = /^[A-Za-z0-9_-]+$/;
|
|
6
7
|
function preparePayload(dataDir, catalogPath, id) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return false;
|
|
13
|
-
const manifest = JSON.parse(readFileSync(join(source, "manifest.json"), "utf8"));
|
|
14
|
-
if (manifest.id !== id)
|
|
15
|
-
return false;
|
|
16
|
-
mkdirSync(dirname(target), { recursive: true });
|
|
17
|
-
const staging = mkdtempSync(join(dirname(target), ".bundled-"));
|
|
18
|
-
try {
|
|
19
|
-
cpSync(source, staging, {
|
|
20
|
-
recursive: true,
|
|
21
|
-
filter: (path) => !/(^|[\\/])(\.git|node_modules)([\\/]|$)/.test(path),
|
|
22
|
-
});
|
|
23
|
-
renameSync(staging, target);
|
|
24
|
-
}
|
|
25
|
-
finally {
|
|
26
|
-
rmSync(staging, { recursive: true, force: true });
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
8
|
+
return materializePlugin({
|
|
9
|
+
id,
|
|
10
|
+
sourceDir: join(dirname(catalogPath), id),
|
|
11
|
+
targetDir: join(dataDir, "plugins", id),
|
|
12
|
+
});
|
|
29
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* 把随包分发的插件(内置模块 + `preinstall` 候选)物化到数据目录。
|
|
16
|
+
*
|
|
17
|
+
* 「目录存在」不再等于「已准备」:完整性由物化回执裁定,半残目录会被就地修复,
|
|
18
|
+
* 而不是留下一个永远报 `manifest.json 缺失或解析失败` 的空壳(issue #856)。
|
|
19
|
+
*/
|
|
30
20
|
export function prepareBundledPlugins(dataDir, catalogPath, overriddenIds = new Set()) {
|
|
21
|
+
const outcomes = [];
|
|
31
22
|
for (const id of BUILTIN_MODULE_IDS)
|
|
32
|
-
preparePayload(dataDir, catalogPath, id);
|
|
23
|
+
outcomes.push(preparePayload(dataDir, catalogPath, id));
|
|
33
24
|
const catalog = JSON.parse(readFileSync(catalogPath, "utf8"));
|
|
34
25
|
if (!Array.isArray(catalog))
|
|
35
|
-
return;
|
|
26
|
+
return outcomes;
|
|
36
27
|
const optional = catalog.filter((entry) => entry &&
|
|
37
28
|
entry.preinstall === true &&
|
|
38
29
|
typeof entry.id === "string" &&
|
|
39
30
|
ID_RE.test(entry.id) &&
|
|
40
31
|
!isBuiltinModule(entry.id));
|
|
41
32
|
if (!optional.length)
|
|
42
|
-
return;
|
|
33
|
+
return outcomes;
|
|
43
34
|
const receiptPath = join(dataDir, "bundled-plugin-installs.json");
|
|
44
35
|
let initialized = new Set();
|
|
45
36
|
if (existsSync(receiptPath)) {
|
|
37
|
+
// 回执损坏时不猜:直接报错,绝不能当成「没装过」把用户卸掉的预装插件重新装回来
|
|
46
38
|
const receipt = JSON.parse(readFileSync(receiptPath, "utf8"));
|
|
47
39
|
if (!receipt ||
|
|
48
40
|
typeof receipt !== "object" ||
|
|
@@ -56,13 +48,28 @@ export function prepareBundledPlugins(dataDir, catalogPath, overriddenIds = new
|
|
|
56
48
|
let changed = false;
|
|
57
49
|
for (const entry of optional) {
|
|
58
50
|
const id = entry.id;
|
|
59
|
-
if (
|
|
51
|
+
if (overriddenIds.has(id))
|
|
60
52
|
continue;
|
|
61
|
-
|
|
53
|
+
const targetDir = join(dataDir, "plugins", id);
|
|
54
|
+
if (initialized.has(id)) {
|
|
55
|
+
// 预装过一次就不再复活:用户主动删掉目录就当卸载,但半残目录必须修
|
|
56
|
+
if (!existsSync(targetDir) || isMaterialized(targetDir, id))
|
|
57
|
+
continue;
|
|
58
|
+
const outcome = preparePayload(dataDir, catalogPath, id);
|
|
59
|
+
outcomes.push(outcome);
|
|
60
|
+
if (outcome.status === "failed" || outcome.status === "source-invalid") {
|
|
61
|
+
console.warn(`[plugins] 修复预装插件 ${id} 失败:${outcome.error ?? outcome.status}`);
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const outcome = preparePayload(dataDir, catalogPath, id);
|
|
66
|
+
outcomes.push(outcome);
|
|
67
|
+
if (outcome.status === "source-invalid" || outcome.status === "failed")
|
|
62
68
|
continue;
|
|
63
69
|
initialized.add(id);
|
|
64
70
|
changed = true;
|
|
65
71
|
}
|
|
66
72
|
if (changed)
|
|
67
73
|
writeJsonAtomicSync(receiptPath, { version: 1, ids: [...initialized].sort() });
|
|
74
|
+
return outcomes;
|
|
68
75
|
}
|
|
@@ -52,6 +52,21 @@ export function buildPluginJobArgs(spec, dataDir, lang) {
|
|
|
52
52
|
args.push("--no-build");
|
|
53
53
|
return { args };
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* 安装成功后的启用运算:把插件 id 从 `disabledPlugins` 里摘掉,返回新的禁用列表。
|
|
57
|
+
* 出厂默认设置把一批可选插件写进了禁用清单(`DEFAULT_CLIENT_SETTINGS.disabledPlugins`),
|
|
58
|
+
* 安装若不同步启用,用户拿到的是「装好了但界面上完全找不到」——「安装即启用」由这里兜住。
|
|
59
|
+
* 无需改动(未禁用 / 空 id)时返回 null,调用方据此跳过写盘。
|
|
60
|
+
*/
|
|
61
|
+
export function enablePluginAfterInstall(disabledPlugins, pluginId) {
|
|
62
|
+
const id = String(pluginId ?? "").trim();
|
|
63
|
+
if (!id)
|
|
64
|
+
return null;
|
|
65
|
+
const list = disabledPlugins ?? [];
|
|
66
|
+
if (!list.includes(id))
|
|
67
|
+
return null;
|
|
68
|
+
return list.filter((x) => x !== id);
|
|
69
|
+
}
|
|
55
70
|
export async function inspectInstallSpec(rawSpec, deps) {
|
|
56
71
|
const local = inspectLocalInstallSpec(rawSpec, deps);
|
|
57
72
|
const rejectBuiltin = () => ({ ...local, problem: "invalid-spec", detail: builtinModuleError("en") });
|
|
@@ -82,6 +82,16 @@ export function validatePluginManifest(raw, dirName) {
|
|
|
82
82
|
messageEn: '"alwaysOn" must be a boolean; treated as false',
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
|
+
// agentToolsDefaultOn: 插件 AI 工具是否默认进入对话上下文。
|
|
86
|
+
// 缺省 false(默认关)—— 未声明的插件工具默认不进对话,需用户在设置里手动打开;
|
|
87
|
+
// 设为 true 即 opt-in(默认开),适用于希望开箱即用的插件。
|
|
88
|
+
if (o.agentToolsDefaultOn !== undefined && typeof o.agentToolsDefaultOn !== "boolean") {
|
|
89
|
+
warnings.push({
|
|
90
|
+
path: "agentToolsDefaultOn",
|
|
91
|
+
message: "agentToolsDefaultOn 必须是布尔值,已按 false(默认关)处理",
|
|
92
|
+
messageEn: '"agentToolsDefaultOn" must be a boolean; treated as false (default off)',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
85
95
|
let permissions = [];
|
|
86
96
|
const perms = o.permissions;
|
|
87
97
|
if (perms === undefined) {
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { writeJsonAtomicSync } from "./atomic-file.js";
|
|
4
|
+
import { copyTree, listTreeFiles, pruneTreeFiles, SKIP_VCS_AND_DEPS } from "./fs-copy.js";
|
|
5
|
+
/**
|
|
6
|
+
* 把「一份插件代码目录」从包内(或任何来源)物化到数据目录,并留下一份可校验的回执。
|
|
7
|
+
*
|
|
8
|
+
* 三条不变量(issue #856 的根因全在这三条上):
|
|
9
|
+
* 1. **验收后才算成功**:副本里必须有 manifest.json 且文件数 > 0,否则返回失败,不落盘、不写回执。
|
|
10
|
+
* 2. **存在 ≠ 已准备**:完整性由回执(`.pi-materialized.json`)+「来源文件一个不缺」裁定。
|
|
11
|
+
* 目录在但没回执 / 缺文件 = 半残,下次调用就地补齐,而不是永久报 manifest 缺失。
|
|
12
|
+
* 3. **新装原子**:走 staging + rename,失败不会留下被当成「已装好」的空目录。
|
|
13
|
+
*
|
|
14
|
+
* 代码目录是纯缓存:用户状态(设置/机密/定时任务)住在 `plugin-data/<id>/`(见 `plugin-paths.ts`),
|
|
15
|
+
* 所以「覆盖」只可能覆盖代码,绝不会碰到用户数据 —— 这是敢自动修复的前提。
|
|
16
|
+
*/
|
|
17
|
+
export const MATERIALIZE_RECEIPT = ".pi-materialized.json";
|
|
18
|
+
function readSourceVersion(sourceDir, id) {
|
|
19
|
+
try {
|
|
20
|
+
const raw = JSON.parse(readFileSync(join(sourceDir, "manifest.json"), "utf8"));
|
|
21
|
+
if (raw?.id !== undefined && raw.id !== id)
|
|
22
|
+
return null;
|
|
23
|
+
return typeof raw.version === "string" ? raw.version.trim() : "";
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export function readMaterializeReceipt(targetDir) {
|
|
30
|
+
try {
|
|
31
|
+
const raw = JSON.parse(readFileSync(join(targetDir, MATERIALIZE_RECEIPT), "utf8"));
|
|
32
|
+
if (raw?.version !== 1 || typeof raw.id !== "string" || !Array.isArray(raw.files))
|
|
33
|
+
return null;
|
|
34
|
+
return {
|
|
35
|
+
version: 1,
|
|
36
|
+
id: raw.id,
|
|
37
|
+
installedVersion: typeof raw.installedVersion === "string" ? raw.installedVersion : "",
|
|
38
|
+
files: raw.files.filter((f) => typeof f === "string"),
|
|
39
|
+
at: typeof raw.at === "string" ? raw.at : "",
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function readInstalledVersion(targetDir, fallback) {
|
|
47
|
+
try {
|
|
48
|
+
const raw = JSON.parse(readFileSync(join(targetDir, "manifest.json"), "utf8"));
|
|
49
|
+
return typeof raw.version === "string" ? raw.version.trim() : fallback;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return fallback;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function writeReceipt(targetDir, receipt) {
|
|
56
|
+
writeJsonAtomicSync(join(targetDir, MATERIALIZE_RECEIPT), receipt, 0);
|
|
57
|
+
}
|
|
58
|
+
/** 回执在、id 对得上、manifest 在、登记的文件一个不少 → 这份物化结果完整 */
|
|
59
|
+
export function isMaterialized(targetDir, id) {
|
|
60
|
+
const receipt = readMaterializeReceipt(targetDir);
|
|
61
|
+
if (!receipt || receipt.id !== id || receipt.files.length === 0)
|
|
62
|
+
return false;
|
|
63
|
+
if (!existsSync(join(targetDir, "manifest.json")))
|
|
64
|
+
return false;
|
|
65
|
+
return receipt.files.every((rel) => existsSync(join(targetDir, rel)));
|
|
66
|
+
}
|
|
67
|
+
/** 来源有、目标缺的文件(用来识别「没回执的老副本」是不是半残) */
|
|
68
|
+
function missingFromTarget(sourceDir, targetDir, skip) {
|
|
69
|
+
const have = new Set(listTreeFiles(targetDir, { skip }));
|
|
70
|
+
return listTreeFiles(sourceDir, { skip }).filter((rel) => !have.has(rel));
|
|
71
|
+
}
|
|
72
|
+
function replaceDir(staging, targetDir) {
|
|
73
|
+
try {
|
|
74
|
+
renameSync(staging, targetDir);
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
// Windows 上 rename 覆盖已存在目录会 EPERM/ENOTEMPTY:先删再换
|
|
78
|
+
if (!existsSync(targetDir))
|
|
79
|
+
throw err;
|
|
80
|
+
rmSync(targetDir, { recursive: true, force: true });
|
|
81
|
+
renameSync(staging, targetDir);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 物化一份插件代码目录。
|
|
86
|
+
*
|
|
87
|
+
* - `created`:目标不存在 → staging 复制 + 验收 + rename 原子落盘
|
|
88
|
+
* - `present`:完整(回执全在,或来源文件一个不缺)且不要求升级 → 什么都不做
|
|
89
|
+
* - `upgraded`:`upgrade` 且目标已有 manifest → 先 `beforeOverwrite()`,再整份覆盖 + 清理旧登记文件
|
|
90
|
+
* - `repaired`:半残(无回执 / 缺文件 / 没有 manifest)→ **只补缺**,绝不覆盖已有文件
|
|
91
|
+
*/
|
|
92
|
+
export function materializePlugin(opts) {
|
|
93
|
+
const { id, sourceDir, targetDir } = opts;
|
|
94
|
+
const skip = opts.skip ?? SKIP_VCS_AND_DEPS;
|
|
95
|
+
const sourceVersion = readSourceVersion(sourceDir, id);
|
|
96
|
+
if (sourceVersion === null)
|
|
97
|
+
return { status: "source-invalid", id, files: 0 };
|
|
98
|
+
const exists = existsSync(targetDir);
|
|
99
|
+
const hasTargetManifest = exists && existsSync(join(targetDir, "manifest.json"));
|
|
100
|
+
const previous = readMaterializeReceipt(targetDir);
|
|
101
|
+
const complete = hasTargetManifest && (isMaterialized(targetDir, id) || missingFromTarget(sourceDir, targetDir, skip).length === 0);
|
|
102
|
+
const stamp = new Date().toISOString();
|
|
103
|
+
try {
|
|
104
|
+
if (!exists) {
|
|
105
|
+
mkdirSync(dirname(targetDir), { recursive: true });
|
|
106
|
+
const staging = mkdtempSync(join(dirname(targetDir), ".materialize-"));
|
|
107
|
+
let count = 0;
|
|
108
|
+
try {
|
|
109
|
+
copyTree(sourceDir, staging, { skip });
|
|
110
|
+
const files = listTreeFiles(staging, { skip });
|
|
111
|
+
count = files.length;
|
|
112
|
+
if (!existsSync(join(staging, "manifest.json")) || files.length === 0) {
|
|
113
|
+
throw new Error(`copy produced no payload (${files.length} files) — source ${sourceDir}`);
|
|
114
|
+
}
|
|
115
|
+
writeReceipt(staging, { version: 1, id, installedVersion: sourceVersion, files, at: stamp });
|
|
116
|
+
replaceDir(staging, targetDir);
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
rmSync(staging, { recursive: true, force: true });
|
|
120
|
+
}
|
|
121
|
+
return { status: "created", id, files: count };
|
|
122
|
+
}
|
|
123
|
+
if (opts.upgrade && hasTargetManifest) {
|
|
124
|
+
opts.beforeOverwrite?.();
|
|
125
|
+
copyTree(sourceDir, targetDir, { skip });
|
|
126
|
+
if (!existsSync(join(targetDir, "manifest.json"))) {
|
|
127
|
+
throw new Error(`copy left no manifest.json in ${targetDir}`);
|
|
128
|
+
}
|
|
129
|
+
const files = listTreeFiles(targetDir, { skip });
|
|
130
|
+
writeReceipt(targetDir, { version: 1, id, installedVersion: sourceVersion, files, at: stamp });
|
|
131
|
+
pruneStale(targetDir, previous, files, id);
|
|
132
|
+
return { status: "upgraded", id, files: files.length };
|
|
133
|
+
}
|
|
134
|
+
if (complete)
|
|
135
|
+
return { status: "present", id, files: previous?.files.length ?? 0 };
|
|
136
|
+
copyTree(sourceDir, targetDir, { skip, missingOnly: true });
|
|
137
|
+
if (!existsSync(join(targetDir, "manifest.json"))) {
|
|
138
|
+
throw new Error(`copy left no manifest.json in ${targetDir}`);
|
|
139
|
+
}
|
|
140
|
+
const files = listTreeFiles(targetDir, { skip });
|
|
141
|
+
writeReceipt(targetDir, {
|
|
142
|
+
version: 1,
|
|
143
|
+
id,
|
|
144
|
+
installedVersion: readInstalledVersion(targetDir, sourceVersion),
|
|
145
|
+
files,
|
|
146
|
+
at: stamp,
|
|
147
|
+
});
|
|
148
|
+
pruneStale(targetDir, previous, files, id);
|
|
149
|
+
return { status: "repaired", id, files: files.length };
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
return { status: "failed", id, files: 0, error: err instanceof Error ? err.message : String(err) };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** 只清理「上一份回执登记过、这一份没有」的文件,绝不碰 node_modules / 插件自建的缓存 */
|
|
156
|
+
function pruneStale(targetDir, previous, files, id) {
|
|
157
|
+
if (!previous || previous.id !== id)
|
|
158
|
+
return;
|
|
159
|
+
const current = new Set(files);
|
|
160
|
+
pruneTreeFiles(targetDir, previous.files.filter((rel) => !current.has(rel)));
|
|
161
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* 插件「代码」与「状态」的路径单源。
|
|
5
|
+
*
|
|
6
|
+
* 代码目录(`<dataDir>/plugins/<id>/`)= **纯缓存**:随包插件会被物化/升级覆盖,
|
|
7
|
+
* 依赖可重装,下载物可重下。半残了可以放心修、放心重放。
|
|
8
|
+
*
|
|
9
|
+
* 状态目录(`<dataDir>/plugin-data/<id>/`)= **不可再生**:用户设置、加密机密、
|
|
10
|
+
* 持久定时任务、能力确认标记。代码怎么重建都不该碰到它 —— 这才是「自愈」敢动手的前提。
|
|
11
|
+
*
|
|
12
|
+
* 历史坑:状态曾住在代码目录里(`plugins/<id>/storage.json` + `secrets.bin`),
|
|
13
|
+
* 于是任何「删了重放」都会顺手清掉用户的配置与渠道授权,修复逻辑只能靠手写
|
|
14
|
+
* keepDirs 名单硬撑(见 plugin-updater 的回滚)。issue #856 的空目录永久化
|
|
15
|
+
* 正是这套混住模型的下游症状。
|
|
16
|
+
*/
|
|
17
|
+
/** 曾住在代码目录、现在归状态目录的文件;迁移时逐个搬过去 */
|
|
18
|
+
export const PLUGIN_STATE_FILES = ["storage.json", "secrets.bin", "schedules.json", ".pi-approved"];
|
|
19
|
+
export function pluginCodeDir(dataDir, id) {
|
|
20
|
+
return join(dataDir, "plugins", id);
|
|
21
|
+
}
|
|
22
|
+
export function pluginStateDir(dataDir, id) {
|
|
23
|
+
return join(dataDir, "plugin-data", id);
|
|
24
|
+
}
|
|
25
|
+
export function pluginStateFile(dataDir, id, name) {
|
|
26
|
+
return join(pluginStateDir(dataDir, id), name);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 老布局 → 新布局的一次性搬家(幂等)。
|
|
30
|
+
*
|
|
31
|
+
* 只搬「代码目录里有、状态目录里没有」的文件:两边都有时以状态目录为准,
|
|
32
|
+
* 绝不拿旧副本覆盖新状态。返回实际搬走的文件名,调用方可据此打日志。
|
|
33
|
+
*/
|
|
34
|
+
export function migratePluginState(dataDir, id) {
|
|
35
|
+
const codeDir = pluginCodeDir(dataDir, id);
|
|
36
|
+
const stateDir = pluginStateDir(dataDir, id);
|
|
37
|
+
const moved = [];
|
|
38
|
+
for (const name of PLUGIN_STATE_FILES) {
|
|
39
|
+
const from = join(codeDir, name);
|
|
40
|
+
const to = join(stateDir, name);
|
|
41
|
+
if (!existsSync(from) || existsSync(to))
|
|
42
|
+
continue;
|
|
43
|
+
try {
|
|
44
|
+
mkdirSync(stateDir, { recursive: true });
|
|
45
|
+
renameSync(from, to);
|
|
46
|
+
moved.push(name);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
try {
|
|
50
|
+
copyFileSync(from, to);
|
|
51
|
+
rmSync(from, { force: true });
|
|
52
|
+
moved.push(name);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
console.warn(`[plugins] 迁移插件状态失败 ${from} → ${to}:`, err instanceof Error ? err.message : err);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return moved;
|
|
60
|
+
}
|
|
@@ -1,24 +1,37 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { lstatSync, mkdirSync, readdirSync, renameSync, rmSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
+
import { pluginCodeDir, pluginStateDir } from "./plugin-paths.js";
|
|
4
5
|
const ID_RE = /^[a-z0-9][a-z0-9_-]*$/;
|
|
5
6
|
const REMOVAL_RE = /^removed-[0-9a-f-]{36}$/;
|
|
6
7
|
export function removePlugin(dataDir, id) {
|
|
7
8
|
if (!ID_RE.test(id))
|
|
8
9
|
throw new Error(`Invalid plugin id: ${id}`);
|
|
9
|
-
const target =
|
|
10
|
+
const target = pluginCodeDir(dataDir, id);
|
|
10
11
|
lstatSync(target);
|
|
11
12
|
const trash = join(dataDir, "plugin-trash");
|
|
12
13
|
mkdirSync(trash, { recursive: true });
|
|
13
14
|
const removed = join(trash, `removed-${randomUUID()}`);
|
|
14
15
|
renameSync(target, removed);
|
|
16
|
+
let deferred = false;
|
|
15
17
|
try {
|
|
16
18
|
rmSync(removed, { recursive: true, force: true });
|
|
17
|
-
return { deferred: false };
|
|
18
19
|
}
|
|
19
20
|
catch {
|
|
20
|
-
|
|
21
|
+
deferred = true;
|
|
21
22
|
}
|
|
23
|
+
// 状态独立存放后卸载要连它一起删,否则重装会闦回旧的设置与机密
|
|
24
|
+
const state = pluginStateDir(dataDir, id);
|
|
25
|
+
try {
|
|
26
|
+
if (lstatSync(state).isDirectory())
|
|
27
|
+
rmSync(state, { recursive: true, force: true });
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
if (err.code !== "ENOENT") {
|
|
31
|
+
console.warn(`[plugins] 卸载时清理插件状态失败 (${state}):`, err instanceof Error ? err.message : err);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { deferred };
|
|
22
35
|
}
|
|
23
36
|
export function cleanupRemovedPlugins(dataDir) {
|
|
24
37
|
const trash = join(dataDir, "plugin-trash");
|
|
@@ -156,12 +156,13 @@ export function armDelay(nextAtMs, nowMs, maxChunkMs = 6 * 60 * 60 * 1000) {
|
|
|
156
156
|
return 0;
|
|
157
157
|
return Math.min(raw, Math.max(1, Math.min(maxChunkMs, MAX_TIMEOUT_MS)));
|
|
158
158
|
}
|
|
159
|
-
|
|
160
|
-
|
|
159
|
+
/** 状态目录里的持久任务表(代码目录可重建,定时任务的 lastRun 不能丢) */
|
|
160
|
+
export function scheduleFile(stateDir) {
|
|
161
|
+
return join(stateDir, "schedules.json");
|
|
161
162
|
}
|
|
162
|
-
export function loadScheduleRecords(
|
|
163
|
+
export function loadScheduleRecords(stateDir) {
|
|
163
164
|
try {
|
|
164
|
-
const parsed = JSON.parse(readFileSync(scheduleFile(
|
|
165
|
+
const parsed = JSON.parse(readFileSync(scheduleFile(stateDir), "utf8"));
|
|
165
166
|
if (!parsed || typeof parsed !== "object" || parsed.schedules === null || typeof parsed.schedules !== "object") {
|
|
166
167
|
return {};
|
|
167
168
|
}
|
|
@@ -186,11 +187,11 @@ export function loadScheduleRecords(pluginDir) {
|
|
|
186
187
|
return {};
|
|
187
188
|
}
|
|
188
189
|
}
|
|
189
|
-
export function saveScheduleRecords(
|
|
190
|
+
export function saveScheduleRecords(stateDir, records) {
|
|
190
191
|
try {
|
|
191
|
-
writeJsonAtomicSync(scheduleFile(
|
|
192
|
+
writeJsonAtomicSync(scheduleFile(stateDir), { v: 1, schedules: records });
|
|
192
193
|
}
|
|
193
194
|
catch (err) {
|
|
194
|
-
console.error(`[plugin-schedule] persist failed (${
|
|
195
|
+
console.error(`[plugin-schedule] persist failed (${stateDir}):`, err);
|
|
195
196
|
}
|
|
196
197
|
}
|
|
@@ -37,9 +37,13 @@ export class PluginSecrets {
|
|
|
37
37
|
file;
|
|
38
38
|
settingsFile;
|
|
39
39
|
key;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
/**
|
|
41
|
+
* @param stateDir 插件**状态**目录(`pluginStateDir(dataDir, id)`)。机密与设置都属于状态,
|
|
42
|
+
* 不放在代码目录里 —— 代码目录是纯缓存,随时可能被重新物化。
|
|
43
|
+
*/
|
|
44
|
+
constructor(dataDir, stateDir) {
|
|
45
|
+
this.file = join(stateDir, "secrets.bin");
|
|
46
|
+
this.settingsFile = join(stateDir, "storage.json");
|
|
43
47
|
this.key = PluginSecrets.keyFor(dataDir);
|
|
44
48
|
}
|
|
45
49
|
static keys = new Map();
|
|
@@ -4,6 +4,7 @@ import { fileURLToPath } from "node:url";
|
|
|
4
4
|
import { execFile } from "node:child_process";
|
|
5
5
|
import { pick } from "./i18n.js";
|
|
6
6
|
import { parseInstallSpec, manifestCandidateUrls } from "./plugin-install-spec.js";
|
|
7
|
+
import { copyTree } from "./fs-copy.js";
|
|
7
8
|
const PLUGIN_ID_RE = /^[A-Za-z0-9_-]+$/;
|
|
8
9
|
export const BACKUP_KEEP = 3;
|
|
9
10
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
@@ -36,10 +37,7 @@ export function ensureBackup(dataDir, id, opts) {
|
|
|
36
37
|
const dest = join(dataDir, "plugin-backups", `${id}-${ts}`);
|
|
37
38
|
try {
|
|
38
39
|
mkdirSync(dirnameOf(dest), { recursive: true });
|
|
39
|
-
|
|
40
|
-
recursive: true,
|
|
41
|
-
filter: (s) => !/(^|[\\/])(\.git|node_modules|whisper-cache)([\\/]|$)/.test(s),
|
|
42
|
-
});
|
|
40
|
+
copyTree(target, dest, { skip: [".git", "node_modules", "whisper-cache"] });
|
|
43
41
|
writeFileSync(join(dest, ".pi-backup.json"), JSON.stringify({ id, ts, source: opts?.source }, null, 2) + "\n");
|
|
44
42
|
pruneBackups(dataDir, id);
|
|
45
43
|
return ts;
|