pi-web-ui 0.118.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 +143 -1
- 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 +930 -166
- package/dist/server/approval-default-rules.js +4 -4
- package/dist/server/ask-user-question-tool.js +3 -40
- package/dist/server/atomic-file.js +130 -5
- package/dist/server/background-executors.js +235 -0
- package/dist/server/bash-timeout.js +47 -0
- package/dist/server/bash-tool.js +72 -5
- package/dist/server/bg-servers.js +19 -13
- package/dist/server/browser-page-tool.js +38 -29
- package/dist/server/browser-script-tool.js +119 -0
- package/dist/server/canvas/canvas-agent-tool.js +46 -35
- package/dist/server/canvas/canvas-dispatch.js +12 -1
- package/dist/server/claim-files-tool.js +6 -7
- package/dist/server/client-state.js +457 -41
- package/dist/server/communicate-tool.js +59 -77
- package/dist/server/compact-context-tool.js +4 -13
- package/dist/server/conversation-communication.js +23 -1
- package/dist/server/conversation-read-tool.js +14 -23
- package/dist/server/conversation-rows.js +17 -0
- package/dist/server/conversation-tool-names.js +2 -2
- package/dist/server/conversation-tool.js +23 -15
- package/dist/server/conversation-touches.js +1 -1
- package/dist/server/delegate-mode.js +0 -1
- package/dist/server/delegate-task.js +18 -39
- package/dist/server/device-center/approval-summary.js +0 -1
- package/dist/server/device-center/bot-execution.js +0 -1
- package/dist/server/device-center/group-router-runtime.js +2 -0
- package/dist/server/device-center/hub-upgrade.js +5 -10
- package/dist/server/device-center/task-progress.js +2 -2
- package/dist/server/dispatch-domain-handlers.js +97 -24
- package/dist/server/dispatch-plugin-handlers.js +10 -0
- package/dist/server/dsh/dsh-agent-service.js +21 -19
- package/dist/server/durable/durable-agent-service.js +1 -1
- package/dist/server/edit-tool.js +293 -0
- package/dist/server/engine-catalog.js +8 -2
- package/dist/server/engine-cli.js +29 -7
- package/dist/server/engine-probe.js +11 -6
- package/dist/server/eval-tool.js +5 -19
- package/dist/server/files-service.js +41 -19
- package/dist/server/fs-copy.js +93 -0
- package/dist/server/goal-review-gate.js +2 -3
- package/dist/server/index.js +92 -3
- package/dist/server/job-runner.js +345 -0
- package/dist/server/lsp-tool.js +10 -28
- package/dist/server/marker-service.js +42 -30
- package/dist/server/mcp-agent-tool.js +22 -47
- package/dist/server/model-session.js +16 -0
- package/dist/server/native-cli-session-hooks.js +3 -2
- package/dist/server/native-cli-session.js +27 -2
- package/dist/server/patch-tool.js +2 -8
- package/dist/server/phone-tool.js +167 -0
- package/dist/server/plan-mode.js +7 -4
- package/dist/server/plan-update-tool.js +5 -8
- package/dist/server/plugin-api-catalog.js +13 -1
- package/dist/server/plugin-bundled.js +35 -28
- package/dist/server/plugin-catalog.js +6 -0
- package/dist/server/plugin-delivery.js +358 -0
- package/dist/server/plugin-inbox.js +152 -0
- package/dist/server/plugin-installer.js +15 -0
- package/dist/server/plugin-ledger.js +183 -0
- package/dist/server/plugin-manifest-validate.js +19 -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 +205 -48
- package/dist/server/present-files-tool.js +7 -13
- package/dist/server/preset-fields.js +23 -12
- package/dist/server/preset-share.js +1 -1
- 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/question-params.js +98 -0
- package/dist/server/quick-ask-settings.js +0 -1
- package/dist/server/quick-ask.js +3 -0
- package/dist/server/read-tool.js +10 -17
- package/dist/server/remote-ssh-service.js +40 -1
- package/dist/server/sandbox-runtime-gate.js +1 -1
- package/dist/server/sandbox-service.js +0 -1
- package/dist/server/sandbox-tool.js +25 -34
- package/dist/server/schedule-agent-tool.js +9 -19
- 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 +49 -19
- package/dist/server/skill-tool.js +4 -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 -3
- package/dist/server/subagent-settle.js +71 -0
- package/dist/server/subagents.js +566 -211
- package/dist/server/tabs.js +8 -1
- package/dist/server/terminals.js +34 -28
- package/dist/server/tool-approval.js +15 -1
- package/dist/server/tool-guards.js +0 -91
- package/dist/server/tool-manager.js +127 -47
- package/dist/server/tool-overrides.js +8 -1
- package/dist/server/tool-prompt-overrides.js +3 -6
- package/dist/server/tool-prompts.js +60 -17
- package/dist/server/universal/driver-base.js +2 -0
- package/dist/server/universal/drivers/claude-code.js +210 -211
- package/dist/server/universal/drivers/codex.js +29 -24
- package/dist/server/universal/engine-mcp.js +107 -0
- package/dist/server/universal/universal-attachments.js +77 -1
- package/dist/server/universal/universal-service.js +1 -1
- package/dist/server/universal/universal-session.js +106 -69
- 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/dist/server/worktree-tool.js +13 -20
- package/docs/README.md +40 -36
- package/docs/architecture-plugins.md +64 -35
- package/docs/device-center.md +1 -1
- package/package.json +16 -4
- package/plugin-sdk/index.d.ts +102 -0
- package/plugins/catalog.json +23 -51
- package/themes/bases/flow-base.css +8 -17
- package/themes/dark-teal.css +0 -3
- 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-CAWcs9tm.js → CanvasStudio-BB_Iq7pl.js} +3 -3
- package/web/dist/assets/CanvasStudio-DU6O3N4u.js +1579 -0
- package/web/dist/assets/DeviceCenter-By1zVTSY.js +3 -0
- package/web/dist/assets/DeviceCenter-Cnm0Tjrx.js +3 -0
- package/web/dist/assets/DeviceExecutionSettings-qJzbI0Cf.js +1 -0
- package/web/dist/assets/{DeviceGroups-C_fZMNeK.js → DeviceGroups-DPrQxW6B.js} +1 -1
- package/web/dist/assets/DeviceGroups-DoucyUjI.js +1 -0
- package/web/dist/assets/{Dropdown-CTgVLUbp.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-1-lsPO1f.js +61 -0
- package/web/dist/assets/GroupConversation-HeiyW_O_.js +61 -0
- package/web/dist/assets/{GroupWorkItems-BIvZtr10.js → GroupWorkItems-BYJv2XFj.js} +1 -1
- package/web/dist/assets/{HintTip-Da6qc566.js → HintTip-DFhoclDw.js} +1 -1
- package/web/dist/assets/ModelConfigModal-D2Ro-jUZ.js +2 -0
- package/web/dist/assets/ModelConfigModal-DPc9R1pq.js +2 -0
- package/web/dist/assets/{NativeCliPanel-CU1rZmyu.js → NativeCliPanel-B108d697.js} +3 -3
- package/web/dist/assets/NativeCliPanel-DupvGwQM.js +5 -0
- package/web/dist/assets/{PiSetupModal-DZKY5Wiw.js → PiSetupModal-D0HitBfd.js} +1 -1
- package/web/dist/assets/ProviderOAuthControls-BLKUEDde.js +1 -0
- package/web/dist/assets/SCMPanel-DDbWrUHR.js +2 -0
- package/web/dist/assets/SCMPanel-DluNcd2-.js +2 -0
- 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-Bt5WKAxP.js → TermXterm-SYc-MUk2.js} +2 -2
- package/web/dist/assets/TerminalPanel-CgA_lhVT.js +1 -0
- package/web/dist/assets/TerminalPanel-vdSIOPjD.js +1 -0
- package/web/dist/assets/TuiOverlayModal-Cy1jYYIw.js +1 -0
- package/web/dist/assets/TuiOverlayModal-kXY_4hV8.js +1 -0
- package/web/dist/assets/app-globals-CBEscsYP.js +129 -0
- package/web/dist/assets/{canvas-client-WkhM55mR.js → canvas-client-Clsypfs6.js} +1 -1
- package/web/dist/assets/{desktop-B8NZ3KW-.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-VWBcq-LQ.js +1 -0
- 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/dist/server/edit-soft-tool.js +0 -297
- package/web/dist/assets/BgTasksModal-D-x7kA63.js +0 -1
- package/web/dist/assets/DeviceCenter-Dc0whRcu.js +0 -3
- package/web/dist/assets/DeviceExecutionSettings-BoX7-0h7.js +0 -1
- package/web/dist/assets/FilePreview-C6W0PvaK.js +0 -2
- package/web/dist/assets/GlobalSearchModal-HR9LSspC.js +0 -1
- package/web/dist/assets/GroupConversation-C81YjF2C.js +0 -61
- package/web/dist/assets/ModelConfigModal-DhSyuIuV.js +0 -2
- package/web/dist/assets/ProviderOAuthControls-CIL85nA7.js +0 -1
- package/web/dist/assets/SCMPanel-CuEgzAlO.js +0 -2
- package/web/dist/assets/SettingsModal-Cdu5h29p.js +0 -33
- package/web/dist/assets/TerminalPanel-BCIBV8se.js +0 -1
- package/web/dist/assets/TuiOverlayModal-3NDd9T97.js +0 -1
- package/web/dist/assets/app-globals-CZjFtI4F.js +0 -129
- package/web/dist/assets/index-CQc3VrWa.css +0 -1
- package/web/dist/assets/index-D_AGu4Zv.js +0 -404
- package/web/dist/assets/plugin-host-uqZGpvCs.js +0 -1
- package/web/dist/assets/remote-ssh-client-Cdf-cTUe.js +0 -1
- package/web/dist/assets/theme-DGL7hLUs.js +0 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件投递账本(Plugin Ledger)
|
|
3
|
+
*
|
|
4
|
+
* 渠道插件有两条「消息到底有没有送达」的路径,直到现在两边都没有权威状态:
|
|
5
|
+
*
|
|
6
|
+
* - **入站**:以前是各插件自己攒的内存 Set(QQ 500 条上限,重启即失)——崩溃/重启会重复处理,
|
|
7
|
+
* 出问题也查不到哪条卡在哪一步。
|
|
8
|
+
* - **出站**:发送失败直接 `throw`,大多数调用点 `.catch(() => {})` 吞掉 —— 回复静默消失,只剩一行 log。
|
|
9
|
+
*
|
|
10
|
+
* 本文件是**一个账本、两个方向**的存储层(`direction: "in" | "out"`),两个视图:
|
|
11
|
+
* - `PluginInbox`(入站,至少一次 + 幂等键):`server/plugin-inbox.ts`
|
|
12
|
+
* - `DeliveryQueue`(出站,排队 + 退避 + 死信):`server/plugin-delivery.ts`
|
|
13
|
+
*
|
|
14
|
+
* 边界:`<dataDir>/plugin-inbox/<pluginId>.json`,最多 `maxEntries` 条、终态保留 `keepMs`。
|
|
15
|
+
* 写入是 debounce 的原子写(默认 300ms)—— 崩溃最多丢最近几百毫秒的记录,代价是可能重复投递一次,
|
|
16
|
+
* 而「至少一次」本来就允许重复,所以这个取舍安全。
|
|
17
|
+
*/
|
|
18
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
import { writeJsonAtomicSync } from "./atomic-file.js";
|
|
21
|
+
const KEY_MAX = 200;
|
|
22
|
+
export const DEFAULT_MAX_ENTRIES = 2_000;
|
|
23
|
+
export const DEFAULT_KEEP_MS = 7 * 24 * 60 * 60 * 1000;
|
|
24
|
+
export const DEFAULT_MAX_ATTEMPTS = 3;
|
|
25
|
+
const DEFAULT_FLUSH_DELAY_MS = 300;
|
|
26
|
+
export function normalizeLedgerKey(key) {
|
|
27
|
+
const k = typeof key === "string" ? key.trim() : "";
|
|
28
|
+
if (!k)
|
|
29
|
+
return null;
|
|
30
|
+
return k.length > KEY_MAX ? k.slice(0, KEY_MAX) : k;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 账本存储:唯一负责「读一次、写一次」的地方。
|
|
34
|
+
* 入站/出站两个视图共享同一个实例,才能真正做到「一条消息只有一个状态」。
|
|
35
|
+
*/
|
|
36
|
+
export class PluginLedgerStore {
|
|
37
|
+
pluginId;
|
|
38
|
+
maxAttempts;
|
|
39
|
+
maxEntries;
|
|
40
|
+
keepMs;
|
|
41
|
+
now;
|
|
42
|
+
records = new Map();
|
|
43
|
+
file;
|
|
44
|
+
flushDelayMs;
|
|
45
|
+
flushTimer = null;
|
|
46
|
+
dirty = false;
|
|
47
|
+
closed = false;
|
|
48
|
+
constructor(opts) {
|
|
49
|
+
this.pluginId = opts.pluginId;
|
|
50
|
+
this.maxEntries = Math.max(16, opts.maxEntries ?? DEFAULT_MAX_ENTRIES);
|
|
51
|
+
this.keepMs = Math.max(0, opts.keepMs ?? DEFAULT_KEEP_MS);
|
|
52
|
+
this.maxAttempts = Math.max(1, opts.maxAttempts ?? DEFAULT_MAX_ATTEMPTS);
|
|
53
|
+
this.flushDelayMs = Math.max(0, opts.flushDelayMs ?? DEFAULT_FLUSH_DELAY_MS);
|
|
54
|
+
this.now = opts.now ?? Date.now;
|
|
55
|
+
this.file = join(opts.dataDir, "plugin-inbox", `${opts.pluginId}.json`);
|
|
56
|
+
this.load();
|
|
57
|
+
}
|
|
58
|
+
load() {
|
|
59
|
+
if (!existsSync(this.file))
|
|
60
|
+
return;
|
|
61
|
+
try {
|
|
62
|
+
const parsed = JSON.parse(readFileSync(this.file, "utf8"));
|
|
63
|
+
if (!parsed || parsed.v !== 1 || !Array.isArray(parsed.records))
|
|
64
|
+
return;
|
|
65
|
+
for (const r of parsed.records) {
|
|
66
|
+
if (!r || typeof r.key !== "string" || typeof r.state !== "string")
|
|
67
|
+
continue;
|
|
68
|
+
if (!["claimed", "done", "failed"].includes(r.state))
|
|
69
|
+
continue;
|
|
70
|
+
const direction = r.direction === "out" ? "out" : "in";
|
|
71
|
+
this.records.set(r.key, {
|
|
72
|
+
...r,
|
|
73
|
+
direction,
|
|
74
|
+
id: typeof r.id === "string" ? r.id : `${this.pluginId}:${r.key}`,
|
|
75
|
+
attempts: Number.isFinite(r.attempts) ? Number(r.attempts) : 1,
|
|
76
|
+
createdAt: Number.isFinite(r.createdAt) ? Number(r.createdAt) : this.now(),
|
|
77
|
+
updatedAt: Number.isFinite(r.updatedAt) ? Number(r.updatedAt) : this.now(),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// 坏文件不阻断插件启动:当作空账本,下次 flush 覆盖。
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
get(key) {
|
|
86
|
+
return this.records.get(key);
|
|
87
|
+
}
|
|
88
|
+
/** 写入/更新记录并安排落盘。 */
|
|
89
|
+
put(record) {
|
|
90
|
+
this.records.set(record.key, record);
|
|
91
|
+
this.touch();
|
|
92
|
+
}
|
|
93
|
+
delete(key) {
|
|
94
|
+
if (!this.records.delete(key))
|
|
95
|
+
return;
|
|
96
|
+
this.touch();
|
|
97
|
+
}
|
|
98
|
+
all() {
|
|
99
|
+
return [...this.records.values()];
|
|
100
|
+
}
|
|
101
|
+
byDirection(direction) {
|
|
102
|
+
const out = [];
|
|
103
|
+
for (const r of this.records.values())
|
|
104
|
+
if (r.direction === direction)
|
|
105
|
+
out.push(r);
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
newRecord(fields) {
|
|
109
|
+
const now = this.now();
|
|
110
|
+
return {
|
|
111
|
+
id: `${this.pluginId}:${fields.key}`,
|
|
112
|
+
key: fields.key,
|
|
113
|
+
direction: fields.direction,
|
|
114
|
+
state: fields.state ?? "claimed",
|
|
115
|
+
attempts: fields.attempts ?? 1,
|
|
116
|
+
createdAt: now,
|
|
117
|
+
updatedAt: now,
|
|
118
|
+
...fields.record,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
touch() {
|
|
122
|
+
this.dirty = true;
|
|
123
|
+
if (this.closed)
|
|
124
|
+
return;
|
|
125
|
+
if (this.flushDelayMs === 0) {
|
|
126
|
+
this.flush();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (this.flushTimer)
|
|
130
|
+
return;
|
|
131
|
+
this.flushTimer = setTimeout(() => {
|
|
132
|
+
this.flushTimer = null;
|
|
133
|
+
this.flush();
|
|
134
|
+
}, this.flushDelayMs);
|
|
135
|
+
this.flushTimer.unref?.();
|
|
136
|
+
}
|
|
137
|
+
/** 终态:done,或已达重试上限的 failed(死信)。 */
|
|
138
|
+
isTerminal(record) {
|
|
139
|
+
if (record.state === "done")
|
|
140
|
+
return true;
|
|
141
|
+
return record.state === "failed" && record.attempts >= this.maxAttempts;
|
|
142
|
+
}
|
|
143
|
+
/** 有界保留:先按 TTL 删终态,再按条数裁剪最旧的。 */
|
|
144
|
+
prune() {
|
|
145
|
+
const now = this.now();
|
|
146
|
+
if (this.keepMs > 0) {
|
|
147
|
+
for (const [key, r] of this.records) {
|
|
148
|
+
if (this.isTerminal(r) && now - r.updatedAt > this.keepMs)
|
|
149
|
+
this.records.delete(key);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (this.records.size <= this.maxEntries)
|
|
153
|
+
return;
|
|
154
|
+
const sorted = [...this.records.values()].sort((a, b) => a.updatedAt - b.updatedAt);
|
|
155
|
+
const drop = this.records.size - this.maxEntries;
|
|
156
|
+
for (let i = 0; i < drop; i++) {
|
|
157
|
+
const r = sorted[i];
|
|
158
|
+
if (r)
|
|
159
|
+
this.records.delete(r.key);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
flush() {
|
|
163
|
+
if (this.flushTimer) {
|
|
164
|
+
clearTimeout(this.flushTimer);
|
|
165
|
+
this.flushTimer = null;
|
|
166
|
+
}
|
|
167
|
+
if (!this.dirty)
|
|
168
|
+
return;
|
|
169
|
+
this.dirty = false;
|
|
170
|
+
this.prune();
|
|
171
|
+
try {
|
|
172
|
+
writeJsonAtomicSync(this.file, { v: 1, records: this.all() });
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
// 写盘失败:保持在内存里,下次变更再试;绝不因此丢消息。
|
|
176
|
+
this.dirty = true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
dispose() {
|
|
180
|
+
this.flush();
|
|
181
|
+
this.closed = true;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -73,6 +73,25 @@ export function validatePluginManifest(raw, dirName) {
|
|
|
73
73
|
else {
|
|
74
74
|
apiVersion = av;
|
|
75
75
|
}
|
|
76
|
+
// alwaysOn: 声明为「无客户端连接时也应在宿主启动时激活」的常驻插件。
|
|
77
|
+
// 缺省 false —— 未声明的插件继续走懒激活(首个 WS attach 时)。
|
|
78
|
+
if (o.alwaysOn !== undefined && typeof o.alwaysOn !== "boolean") {
|
|
79
|
+
warnings.push({
|
|
80
|
+
path: "alwaysOn",
|
|
81
|
+
message: "alwaysOn 必须是布尔值,已按 false 处理",
|
|
82
|
+
messageEn: '"alwaysOn" must be a boolean; treated as false',
|
|
83
|
+
});
|
|
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
|
+
}
|
|
76
95
|
let permissions = [];
|
|
77
96
|
const perms = o.permissions;
|
|
78
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;
|