personal-bot 0.3.4 → 0.3.5
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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/bin/pbot.mjs +3 -17
- package/docs/architecture/design.md +8 -6
- package/docs/architecture/extensibility.md +1 -1
- package/docs/architecture/principles.md +1 -1
- package/lib/antigravity-plugin/lib/common/paths.d.ts +1 -0
- package/lib/antigravity-plugin/lib/common/paths.js +2 -0
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +26 -24
- package/lib/antigravity-plugin/lib/extension/cli.js +22596 -0
- package/lib/antigravity-plugin/lib/extension/mcp.js +22 -20
- package/lib/antigravity-plugin/lib/extension/workspace-cli.js +5 -20165
- package/lib/antigravity-plugin/package.json +1 -1
- package/lib/antigravity-plugin/plugin.json +1 -1
- package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
- package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
- package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
- package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +1 -1
- package/lib/common/paths.d.ts +1 -0
- package/lib/common/paths.js +2 -0
- package/lib/extension/acp-worker.js +26 -24
- package/lib/extension/cli.js +22596 -0
- package/lib/extension/mcp.js +22 -20
- package/lib/extension/workspace-cli.js +5 -20165
- package/lib/qoder-plugin/.mcp.json +2 -2
- package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
- package/lib/qoder-plugin/hooks/hooks.json +3 -3
- package/lib/qoder-plugin/skills/pbot/SKILL.md +1 -1
- package/package.json +1 -1
- package/scripts/README.md +1 -1
- package/src/common/paths.ts +2 -0
- package/src/extension/cli/args.ts +104 -0
- package/src/extension/cli/commands/history.ts +81 -0
- package/src/extension/cli/commands/mcp.ts +165 -0
- package/src/extension/cli/commands/plugin.ts +33 -0
- package/src/extension/cli/commands/setup.ts +159 -0
- package/src/extension/cli/commands/upgrade.ts +158 -0
- package/src/extension/cli/commands/workspace.ts +71 -0
- package/src/extension/cli/index.ts +193 -0
- package/src/extension/cli/types.ts +30 -0
- package/src/integrations/installation/cli-update.ts +3 -3
- package/src/integrations/installation/pi-setup.ts +6 -3
- package/src/services/installation/setup-service.ts +0 -35
- package/lib/extension/setup/cli.js +0 -364
- package/lib/extension/setup/upgrade.js +0 -153
- package/lib/integrations/installation/cli-update.js +0 -146
- package/lib/integrations/installation/pi-setup.js +0 -193
- package/lib/integrations/installation/processes.js +0 -140
- package/lib/integrations/installation/upgraders/antigravity.js +0 -67
- package/lib/integrations/installation/upgraders/claude.js +0 -123
- package/lib/integrations/installation/upgraders/codex.js +0 -73
- package/lib/integrations/installation/upgraders/dsh.js +0 -62
- package/lib/integrations/installation/upgraders/pi.js +0 -90
- package/lib/integrations/installation/upgraders/qoder.js +0 -67
- package/lib/integrations/installation/upgraders/registry.js +0 -40
- package/lib/integrations/installation/workspace.js +0 -21
- package/lib/services/installation/setup-service.js +0 -140
- package/lib/services/installation/upgrade-service.js +0 -95
- package/lib/ui/setup.js +0 -52
- package/src/extension/setup/cli.ts +0 -303
- package/src/extension/setup/upgrade.ts +0 -158
- package/src/extension/setup/workspace-cli.ts +0 -167
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { mkdir, readFile, realpath } from "node:fs/promises";
|
|
4
|
-
import { homedir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { executeSetup, packageChoices, parseCatalog, parseSetupArgs } from "../../services/installation/setup-service.js";
|
|
8
|
-
import { npmName, ordinaryNpm } from "../../common/package-source.js";
|
|
9
|
-
import { PiBootstrapRequired, PiSetup, piPackageName } from "../../integrations/installation/pi-setup.js";
|
|
10
|
-
import { findExecutable, inspectSystem, SetupProcesses } from "../../integrations/installation/processes.js";
|
|
11
|
-
import { resolveWorkspace } from "../../integrations/installation/workspace.js";
|
|
12
|
-
import { readWorkspaceMcpConfig, removeWorkspaceMcpServer, upsertWorkspaceMcpServer, } from "../../services/workspace/workspace-mcp.js";
|
|
13
|
-
import { SetupUI } from "../../ui/setup.js";
|
|
14
|
-
import { runUpgradeCommand } from "./upgrade.js";
|
|
15
|
-
const root = new URL("../../../", import.meta.url);
|
|
16
|
-
const quote = (value) => "'" + value.replaceAll("'", "'\\''") + "'";
|
|
17
|
-
const mcpManageUsage = "用法:\n" +
|
|
18
|
-
" pbot mcp list [--workspace <目录>]\n" +
|
|
19
|
-
" pbot mcp add <name> --url <url> [--header K=V]... [--description <说明>] [--timeout <秒>] [--disabled] [--workspace <目录>]\n" +
|
|
20
|
-
" pbot mcp add <name> [--env K=V]... [--cwd <目录>] [--description <说明>] [--timeout <秒>] [--disabled] [--workspace <目录>] -- <command> [args...]\n" +
|
|
21
|
-
" pbot mcp remove <name> [--workspace <目录>]";
|
|
22
|
-
function parseKeyValue(raw, flag) {
|
|
23
|
-
const sep = raw.indexOf("=");
|
|
24
|
-
if (sep <= 0)
|
|
25
|
-
throw new Error(`${flag} 需要 KEY=VALUE 格式:${raw}`);
|
|
26
|
-
return [raw.slice(0, sep), raw.slice(sep + 1)];
|
|
27
|
-
}
|
|
28
|
-
async function runMcpManage(mcpArgs) {
|
|
29
|
-
const sub = mcpArgs[0];
|
|
30
|
-
if (!sub || ["--help", "-h"].includes(sub)) {
|
|
31
|
-
console.log(mcpManageUsage);
|
|
32
|
-
return 0;
|
|
33
|
-
}
|
|
34
|
-
const rest = mcpArgs.slice(1);
|
|
35
|
-
if (sub === "list") {
|
|
36
|
-
let workspace;
|
|
37
|
-
for (let i = 0; i < rest.length; i++) {
|
|
38
|
-
if (rest[i] === "--workspace") {
|
|
39
|
-
workspace = rest[++i];
|
|
40
|
-
if (!workspace)
|
|
41
|
-
throw new Error("--workspace 需要目录。");
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
throw new Error(`未知参数:${rest[i]}\n${mcpManageUsage}`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
const target = await resolveWorkspace(workspace ?? process.cwd(), process.cwd(), homedir());
|
|
48
|
-
const configuration = await readWorkspaceMcpConfig(target);
|
|
49
|
-
if (configuration.error)
|
|
50
|
-
throw new Error(configuration.error);
|
|
51
|
-
console.log(JSON.stringify({ workspaceRoot: target, path: configuration.path, mcpServers: configuration.servers }, null, 2));
|
|
52
|
-
return 0;
|
|
53
|
-
}
|
|
54
|
-
if (sub === "remove") {
|
|
55
|
-
const name = rest[0];
|
|
56
|
-
if (!name || name.startsWith("-"))
|
|
57
|
-
throw new Error(`remove 需要服务名。\n${mcpManageUsage}`);
|
|
58
|
-
let workspace;
|
|
59
|
-
for (let i = 1; i < rest.length; i++) {
|
|
60
|
-
if (rest[i] === "--workspace") {
|
|
61
|
-
workspace = rest[++i];
|
|
62
|
-
if (!workspace)
|
|
63
|
-
throw new Error("--workspace 需要目录。");
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
throw new Error(`未知参数:${rest[i]}\n${mcpManageUsage}`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const target = await resolveWorkspace(workspace ?? process.cwd(), process.cwd(), homedir());
|
|
70
|
-
const result = await removeWorkspaceMcpServer(target, name);
|
|
71
|
-
console.log(JSON.stringify({ workspaceRoot: target, path: result.configuration.path, removed: result.removed, name }, null, 2));
|
|
72
|
-
return result.removed ? 0 : 1;
|
|
73
|
-
}
|
|
74
|
-
if (sub === "add") {
|
|
75
|
-
const name = rest[0];
|
|
76
|
-
if (!name || name.startsWith("-"))
|
|
77
|
-
throw new Error(`add 需要服务名。\n${mcpManageUsage}`);
|
|
78
|
-
let workspace;
|
|
79
|
-
let url;
|
|
80
|
-
let cwd;
|
|
81
|
-
let description;
|
|
82
|
-
let timeout;
|
|
83
|
-
let enabled;
|
|
84
|
-
const env = {};
|
|
85
|
-
const headers = {};
|
|
86
|
-
let commandTokens = [];
|
|
87
|
-
for (let i = 1; i < rest.length; i++) {
|
|
88
|
-
const arg = rest[i];
|
|
89
|
-
if (arg === "--") {
|
|
90
|
-
commandTokens = rest.slice(i + 1);
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
if (arg === "--workspace") {
|
|
94
|
-
workspace = rest[++i];
|
|
95
|
-
if (!workspace)
|
|
96
|
-
throw new Error("--workspace 需要目录。");
|
|
97
|
-
}
|
|
98
|
-
else if (arg === "--url") {
|
|
99
|
-
url = rest[++i];
|
|
100
|
-
if (!url)
|
|
101
|
-
throw new Error("--url 需要地址。");
|
|
102
|
-
}
|
|
103
|
-
else if (arg === "--cwd") {
|
|
104
|
-
cwd = rest[++i];
|
|
105
|
-
if (!cwd)
|
|
106
|
-
throw new Error("--cwd 需要目录。");
|
|
107
|
-
}
|
|
108
|
-
else if (arg === "--description") {
|
|
109
|
-
description = rest[++i];
|
|
110
|
-
if (description === undefined)
|
|
111
|
-
throw new Error("--description 需要说明文本。");
|
|
112
|
-
}
|
|
113
|
-
else if (arg === "--timeout") {
|
|
114
|
-
const raw = rest[++i];
|
|
115
|
-
timeout = Number(raw);
|
|
116
|
-
if (!raw || !Number.isInteger(timeout))
|
|
117
|
-
throw new Error("--timeout 需要整数秒。");
|
|
118
|
-
}
|
|
119
|
-
else if (arg === "--disabled") {
|
|
120
|
-
enabled = false;
|
|
121
|
-
}
|
|
122
|
-
else if (arg === "--env") {
|
|
123
|
-
const raw = rest[++i];
|
|
124
|
-
if (!raw)
|
|
125
|
-
throw new Error("--env 需要 KEY=VALUE。");
|
|
126
|
-
const [k, v] = parseKeyValue(raw, "--env");
|
|
127
|
-
env[k] = v;
|
|
128
|
-
}
|
|
129
|
-
else if (arg === "--header") {
|
|
130
|
-
const raw = rest[++i];
|
|
131
|
-
if (!raw)
|
|
132
|
-
throw new Error("--header 需要 KEY=VALUE。");
|
|
133
|
-
const [k, v] = parseKeyValue(raw, "--header");
|
|
134
|
-
headers[k] = v;
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
throw new Error(`未知参数:${arg}\n${mcpManageUsage}`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
const base = {
|
|
141
|
-
...(description !== undefined ? { description } : {}),
|
|
142
|
-
...(timeout !== undefined ? { timeout } : {}),
|
|
143
|
-
...(enabled !== undefined ? { enabled } : {}),
|
|
144
|
-
};
|
|
145
|
-
let server;
|
|
146
|
-
if (url !== undefined) {
|
|
147
|
-
if (commandTokens.length || Object.keys(env).length || cwd !== undefined) {
|
|
148
|
-
throw new Error("--url(HTTP)不能与 stdio 命令/--env/--cwd 同时使用。");
|
|
149
|
-
}
|
|
150
|
-
server = {
|
|
151
|
-
url,
|
|
152
|
-
...(Object.keys(headers).length ? { headers } : {}),
|
|
153
|
-
...base,
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
const [command, ...cmdArgs] = commandTokens;
|
|
158
|
-
if (!command)
|
|
159
|
-
throw new Error(`stdio 服务需要通过 -- <command> [args...] 指定启动命令。\n${mcpManageUsage}`);
|
|
160
|
-
if (Object.keys(headers).length)
|
|
161
|
-
throw new Error("--header 仅适用于 --url(HTTP)服务。");
|
|
162
|
-
server = {
|
|
163
|
-
command,
|
|
164
|
-
...(cmdArgs.length ? { args: cmdArgs } : {}),
|
|
165
|
-
...(Object.keys(env).length ? { env } : {}),
|
|
166
|
-
...(cwd !== undefined ? { cwd } : {}),
|
|
167
|
-
...base,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
const target = await resolveWorkspace(workspace ?? process.cwd(), process.cwd(), homedir());
|
|
171
|
-
const updated = await upsertWorkspaceMcpServer(target, name, server);
|
|
172
|
-
console.log(JSON.stringify({ workspaceRoot: target, path: updated.path, name, server: updated.servers[name] }, null, 2));
|
|
173
|
-
return 0;
|
|
174
|
-
}
|
|
175
|
-
throw new Error(`未知 mcp 子命令:${sub}\n${mcpManageUsage}`);
|
|
176
|
-
}
|
|
177
|
-
export async function main(args) {
|
|
178
|
-
let options;
|
|
179
|
-
try {
|
|
180
|
-
options = parseSetupArgs(args);
|
|
181
|
-
}
|
|
182
|
-
catch (error) {
|
|
183
|
-
console.error(String(error));
|
|
184
|
-
return 2;
|
|
185
|
-
}
|
|
186
|
-
const manifest = JSON.parse(await readFile(new URL("package.json", root), "utf8"));
|
|
187
|
-
if (options.action === "version") {
|
|
188
|
-
console.log(manifest.version);
|
|
189
|
-
return 0;
|
|
190
|
-
}
|
|
191
|
-
if (options.action === "help") {
|
|
192
|
-
console.log("Personal Bot " + manifest.version + "\n\n用法:pbot setup [--workspace <目录>]\n" +
|
|
193
|
-
" npx personal-bot@latest setup\n" +
|
|
194
|
-
" pbot upgrade|update [--workspace <目录>] [--version <版本>] [--hosts <列表>] [--dsh-profile <名称>] [--json]\n" +
|
|
195
|
-
" pbot init | status [--workspace <目录>] [--json]\n" +
|
|
196
|
-
" pbot history status|list|summary|migrate|search|read [参数] [--workspace <目录>] [--json]\n" +
|
|
197
|
-
" pbot install-antigravity\n" +
|
|
198
|
-
" pbot plugin <host> hook|init|status\n" +
|
|
199
|
-
" pbot mcp\n" +
|
|
200
|
-
" pbot mcp list|add|remove ...\n\n" +
|
|
201
|
-
"setup 安装或升级 Pi、pbot 和选中的插件,然后启动 Pi 执行 /pbot init。\n" +
|
|
202
|
-
"upgrade|update 将全局 CLI 与当前工作区已绑定的各宿主插件统一升级到 npm 最新稳定版。\n" +
|
|
203
|
-
"install-antigravity 通过原生 agy plugin install 安装或更新 Antigravity 插件。\n" +
|
|
204
|
-
"mcp 无参数时启动统一 stdio MCP 服务(暴露 worker_* 与 workspace_mcp_*);带 list/add/remove 时管理工作区 .pbot/mcp.json。\n" +
|
|
205
|
-
"支持 Linux/macOS,需要 Node.js 22.19+ 和 npm。\n--help 显示帮助;--version 显示安装器版本。");
|
|
206
|
-
return 0;
|
|
207
|
-
}
|
|
208
|
-
if (options.action === "upgrade") {
|
|
209
|
-
try {
|
|
210
|
-
return await runUpgradeCommand(options.upgradeArgs ?? [], options.upgradeVerb ?? "upgrade");
|
|
211
|
-
}
|
|
212
|
-
catch (error) {
|
|
213
|
-
console.error(error instanceof Error ? error.message : String(error));
|
|
214
|
-
return 2;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
if (options.action === "mcp-manage") {
|
|
218
|
-
try {
|
|
219
|
-
return await runMcpManage(options.mcpArgs ?? []);
|
|
220
|
-
}
|
|
221
|
-
catch (error) {
|
|
222
|
-
console.error(error instanceof Error ? error.message : String(error));
|
|
223
|
-
return 2;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
if (options.action === "mcp") {
|
|
227
|
-
const entry = fileURLToPath(new URL("lib/extension/mcp.js", root));
|
|
228
|
-
if (!existsSync(entry)) {
|
|
229
|
-
console.error("缺少 MCP 构建产物:" + entry + "。请先运行 npm run build。");
|
|
230
|
-
return 1;
|
|
231
|
-
}
|
|
232
|
-
const child = spawnSync(process.execPath, [entry], { stdio: "inherit" });
|
|
233
|
-
if (child.error) {
|
|
234
|
-
console.error(String(child.error));
|
|
235
|
-
return 1;
|
|
236
|
-
}
|
|
237
|
-
return child.status ?? 0;
|
|
238
|
-
}
|
|
239
|
-
if (options.action === "install-antigravity") {
|
|
240
|
-
const pluginDir = fileURLToPath(new URL("lib/antigravity-plugin", root));
|
|
241
|
-
if (!existsSync(join(pluginDir, "plugin.json"))) {
|
|
242
|
-
console.error("缺少 Antigravity 插件构建产物:" + pluginDir + "。请先运行 npm run build。");
|
|
243
|
-
return 1;
|
|
244
|
-
}
|
|
245
|
-
const agy = process.env.PBOT_AGY_COMMAND || await findExecutable("agy", process.env);
|
|
246
|
-
if (!agy) {
|
|
247
|
-
console.error("未找到 Antigravity CLI(agy)。请先安装 agy,然后运行:\n agy plugin install " + quote(pluginDir));
|
|
248
|
-
return 1;
|
|
249
|
-
}
|
|
250
|
-
const installed = spawnSync(agy, ["plugin", "install", pluginDir], { stdio: "inherit" });
|
|
251
|
-
if (installed.error) {
|
|
252
|
-
console.error(String(installed.error));
|
|
253
|
-
return 1;
|
|
254
|
-
}
|
|
255
|
-
if (installed.status !== 0)
|
|
256
|
-
return installed.status ?? 1;
|
|
257
|
-
console.log("Antigravity 插件已安装。在目标工作区运行 /pbot init 启用 Personal Bot。");
|
|
258
|
-
return 0;
|
|
259
|
-
}
|
|
260
|
-
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
261
|
-
console.error("setup 需要交互终端。请在终端运行 npx personal-bot@latest setup。");
|
|
262
|
-
return 2;
|
|
263
|
-
}
|
|
264
|
-
const controller = new AbortController();
|
|
265
|
-
const cancel = () => controller.abort(new Error("安装已取消;已完成的步骤保留。"));
|
|
266
|
-
process.on("SIGINT", cancel);
|
|
267
|
-
process.on("SIGTERM", cancel);
|
|
268
|
-
const ui = new SetupUI(controller), processes = new SetupProcesses(controller.signal);
|
|
269
|
-
try {
|
|
270
|
-
const cwd = process.cwd();
|
|
271
|
-
ui.text("Personal Bot " + manifest.version + " — 安装与升级\n正在检查 Node/npm 和 tmux…");
|
|
272
|
-
const system = await inspectSystem(processes, cwd);
|
|
273
|
-
const supplied = options.workspace ?? await ui.ask("工作区目录(当前、已有或新建目录)", cwd);
|
|
274
|
-
const workspace = await resolveWorkspace(supplied, cwd, homedir());
|
|
275
|
-
const host = new PiSetup(processes, system.npm, cwd);
|
|
276
|
-
let pi = await host.inspectPi();
|
|
277
|
-
let installed;
|
|
278
|
-
try {
|
|
279
|
-
installed = await host.inspectPackages(pi, workspace);
|
|
280
|
-
}
|
|
281
|
-
catch (error) {
|
|
282
|
-
if (!(error instanceof PiBootstrapRequired))
|
|
283
|
-
throw error;
|
|
284
|
-
const version = await host.latest(piPackageName);
|
|
285
|
-
ui.text(error.message + "\n需先安装 Pi 才能使用其原生接口核对这些来源。目标版本:" + version);
|
|
286
|
-
if (!await ui.confirm("先通过 npm 安装 Pi,再继续选择工作区插件"))
|
|
287
|
-
return 130;
|
|
288
|
-
pi = await host.ensurePi(undefined, version);
|
|
289
|
-
installed = await host.inspectPackages(pi, workspace);
|
|
290
|
-
}
|
|
291
|
-
const catalog = parseCatalog(JSON.parse(await readFile(new URL("resources/installer/plugins.json", root), "utf8")));
|
|
292
|
-
const choices = packageChoices(catalog, installed), optional = choices.filter(choice => !choice.required);
|
|
293
|
-
ui.text("\nPersonal Bot 必装。选择配套插件(取消勾选不会卸载已有插件):");
|
|
294
|
-
const selected = await ui.selectMany(optional.map(choice => choice.plugin.label + " — " + choice.plugin.description), optional.flatMap((choice, index) => choice.selected ? [index] : []));
|
|
295
|
-
optional.forEach((choice, index) => { choice.selected = selected.includes(index); });
|
|
296
|
-
for (const choice of choices.filter(item => item.selected)) {
|
|
297
|
-
if (choice.existing.some(pkg => !ordinaryNpm(pkg.source))) {
|
|
298
|
-
ui.text("\n" + choice.plugin.label + " 的已有来源:\n" + choice.existing.map(pkg => " " + pkg.scope + ": " + pkg.source + " (" + (pkg.version ?? "版本未知") + ")").join("\n"));
|
|
299
|
-
choice.keep = !await ui.confirm("切换到 npm 最新稳定版", false);
|
|
300
|
-
}
|
|
301
|
-
if (choice.keep) {
|
|
302
|
-
if (choice.existing.length !== 1 || !choice.existing[0]?.installedPath)
|
|
303
|
-
choice.error = "保留来源未安装或存在多份来源,请先通过 Pi 原生配置处理。";
|
|
304
|
-
}
|
|
305
|
-
else {
|
|
306
|
-
const others = choice.existing.filter(pkg => !npmName(pkg.source));
|
|
307
|
-
if (others.length > 1 || others.some(pkg => pkg.scope === "user")) {
|
|
308
|
-
choice.error = "请先用 pi config 调整全局或重复来源的加载范围,再切换到 npm。";
|
|
309
|
-
}
|
|
310
|
-
else {
|
|
311
|
-
try {
|
|
312
|
-
choice.target = await host.latest(npmName(choice.plugin.source));
|
|
313
|
-
}
|
|
314
|
-
catch (error) {
|
|
315
|
-
controller.signal.throwIfAborted();
|
|
316
|
-
choice.error = String(error);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
const plan = { workspace, piVersion: await host.latest(piPackageName), packages: choices,
|
|
322
|
-
...(pi ? { pi } : {}), ...(system.tmux ? { tmux: system.tmux } : {}) };
|
|
323
|
-
ui.text("\n安装计划\n工作区:" + workspace + "\nPi:" + (pi ? pi.command + " (" + pi.version + ")" : "通过 npm 全局安装") + " → " + plan.piVersion);
|
|
324
|
-
if (system.tmux)
|
|
325
|
-
ui.text("tmux:" + [system.tmux.command, ...system.tmux.args].map(quote).join(" "));
|
|
326
|
-
for (const choice of choices) {
|
|
327
|
-
ui.text(" " + choice.plugin.label + ": " + (!choice.selected ? "本次跳过" : choice.error ??
|
|
328
|
-
(choice.keep ? "保留 " + choice.existing[0]?.source : (choice.existing.map(pkg => pkg.version ?? "未知").join(", ") || "未安装") + " → " + choice.target)));
|
|
329
|
-
if (choice.selected && choice.existing.some(pkg => pkg.filtered))
|
|
330
|
-
ui.text(" 保留已有资源过滤;如 pbot 被禁用,请通过 pi config --local 启用。");
|
|
331
|
-
}
|
|
332
|
-
const blocking = choices.find(choice => choice.required && choice.error);
|
|
333
|
-
if (blocking)
|
|
334
|
-
throw new Error(blocking.error);
|
|
335
|
-
ui.text("升级前请停止活动 pbot 子会话。Pi 为当前用户共用,插件只处理此工作区。");
|
|
336
|
-
if (!await ui.confirm("执行以上计划"))
|
|
337
|
-
return 130;
|
|
338
|
-
await mkdir(workspace, { recursive: true });
|
|
339
|
-
if (await realpath(workspace) !== workspace)
|
|
340
|
-
throw new Error("工作区路径已发生变化,请重新运行 setup。");
|
|
341
|
-
const result = await executeSetup(plan, host, item => ui.text("[" + item.state + "] " + item.name + ": " + item.detail), controller.signal);
|
|
342
|
-
const failed = result.results.some(item => item.state === "failed");
|
|
343
|
-
if (!result.pi)
|
|
344
|
-
return 1;
|
|
345
|
-
ui.text("\n软件安装阶段结束。工作区初始化和外部服务的状态由 Pi/pbot/插件分别报告。");
|
|
346
|
-
if (await ui.confirm(failed ? "部分插件失败,是否仍启动 Pi 并初始化启用工作区" : "启动 Pi 并初始化启用工作区", !failed)) {
|
|
347
|
-
process.stdin.pause();
|
|
348
|
-
await host.start(result.pi, workspace);
|
|
349
|
-
}
|
|
350
|
-
else {
|
|
351
|
-
ui.text("稍后执行:\ncd " + quote(workspace) + "\n" + quote(result.pi.command) + " --extension " +
|
|
352
|
-
quote(fileURLToPath(new URL("extension/setup/guard.js", new URL("../../", import.meta.url)))) + " '/pbot init'");
|
|
353
|
-
}
|
|
354
|
-
return failed ? 1 : 0;
|
|
355
|
-
}
|
|
356
|
-
catch (error) {
|
|
357
|
-
console.error(error instanceof Error ? error.message : String(error));
|
|
358
|
-
return controller.signal.aborted ? 130 : 1;
|
|
359
|
-
}
|
|
360
|
-
finally {
|
|
361
|
-
process.off("SIGINT", cancel);
|
|
362
|
-
process.off("SIGTERM", cancel);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { valid } from "semver";
|
|
6
|
-
import { object } from "../../common/json.js";
|
|
7
|
-
import { checkStableVersion } from "../../common/package-source.js";
|
|
8
|
-
import { packageVersion } from "../../common/version.js";
|
|
9
|
-
import { execCliEntry, upgradeManagedGlobalCli } from "../../integrations/installation/cli-update.js";
|
|
10
|
-
import { findExecutable, SetupProcesses } from "../../integrations/installation/processes.js";
|
|
11
|
-
import { getHostUpgrader } from "../../integrations/installation/upgraders/registry.js";
|
|
12
|
-
import { resolveWorkspace } from "../../integrations/installation/workspace.js";
|
|
13
|
-
import { executeWorkspaceUpgrade, parseUpgradeArgs, upgradeUsage, } from "../../services/installation/upgrade-service.js";
|
|
14
|
-
import { workspacePluginBindings } from "../../services/workspace/plugin-releases.js";
|
|
15
|
-
const packageRoot = fileURLToPath(new URL("../../../", import.meta.url));
|
|
16
|
-
async function latestStableVersion(processes, cwd) {
|
|
17
|
-
const npm = await findExecutable("npm", processes.env);
|
|
18
|
-
if (!npm)
|
|
19
|
-
throw new Error("未找到 npm,无法查询 personal-bot 最新稳定版。");
|
|
20
|
-
const text = await processes.run(npm, ["view", "personal-bot@latest", "version", "engines", "--json",
|
|
21
|
-
"--fetch-retries=0", "--fetch-timeout=15000"], cwd);
|
|
22
|
-
const value = JSON.parse(text);
|
|
23
|
-
if (typeof value.version !== "string")
|
|
24
|
-
throw new Error("无法取得 personal-bot 的最新版本。");
|
|
25
|
-
return checkStableVersion(value.version, value.engines?.node);
|
|
26
|
-
}
|
|
27
|
-
async function discoverWorkspaceTargets(workspace, requestedHosts) {
|
|
28
|
-
const bindings = workspacePluginBindings(workspace);
|
|
29
|
-
const byHost = new Map();
|
|
30
|
-
for (const [host, entry] of Object.entries(bindings)) {
|
|
31
|
-
byHost.set(host, {
|
|
32
|
-
host,
|
|
33
|
-
currentVersion: entry.version,
|
|
34
|
-
...(entry.bindingPath ? { bindingPath: entry.bindingPath } : {}),
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
if (!byHost.has("pi")) {
|
|
38
|
-
const localPiManifest = join(workspace, ".pi", "npm", "node_modules", "personal-bot", "package.json");
|
|
39
|
-
try {
|
|
40
|
-
const manifest = JSON.parse(await readFile(localPiManifest, "utf8"));
|
|
41
|
-
if (object(manifest) && manifest.name === "personal-bot" && typeof manifest.version === "string" && valid(manifest.version)) {
|
|
42
|
-
byHost.set("pi", { host: "pi", currentVersion: manifest.version, bindingPath: localPiManifest });
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
if (error.code !== "ENOENT")
|
|
47
|
-
throw error;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
if (requestedHosts) {
|
|
51
|
-
return requestedHosts.map(host => byHost.get(host) ?? { host });
|
|
52
|
-
}
|
|
53
|
-
return [...byHost.values()].sort((a, b) => a.host.localeCompare(b.host));
|
|
54
|
-
}
|
|
55
|
-
function formatLine(result) {
|
|
56
|
-
const transition = result.previousVersion && result.version && result.previousVersion !== result.version
|
|
57
|
-
? `${result.previousVersion} → ${result.version}`
|
|
58
|
-
: result.version ?? result.previousVersion ?? "";
|
|
59
|
-
const scope = result.scope ? ` (${result.scope})` : "";
|
|
60
|
-
const head = `[${result.state}] ${result.host}${scope}${transition ? " " + transition : ""}: ${result.detail}`;
|
|
61
|
-
return result.nextStep ? `${head}\n 下一步:${result.nextStep}` : head;
|
|
62
|
-
}
|
|
63
|
-
function printHumanSummary(summary) {
|
|
64
|
-
console.log(`工作区:${summary.workspaceRoot}`);
|
|
65
|
-
console.log(`目标版本:${summary.targetVersion}`);
|
|
66
|
-
console.log(`[${summary.cli.state}] cli ${summary.cli.previousVersion} → ${summary.cli.version}: ${summary.cli.detail}`);
|
|
67
|
-
if (!summary.results.length) {
|
|
68
|
-
console.log("当前工作区尚未绑定任何宿主插件。如需指定宿主,可传入 --hosts <宿主列表>。");
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
for (const result of summary.results)
|
|
72
|
-
console.log(formatLine(result));
|
|
73
|
-
}
|
|
74
|
-
export async function runUpgradeCommand(rawArgs, rawVerb = "upgrade") {
|
|
75
|
-
const options = parseUpgradeArgs(rawArgs);
|
|
76
|
-
if (options.help) {
|
|
77
|
-
console.log(upgradeUsage);
|
|
78
|
-
return 0;
|
|
79
|
-
}
|
|
80
|
-
const controller = new AbortController();
|
|
81
|
-
const cancel = () => controller.abort(new Error("升级已取消。"));
|
|
82
|
-
process.on("SIGINT", cancel);
|
|
83
|
-
process.on("SIGTERM", cancel);
|
|
84
|
-
try {
|
|
85
|
-
const cwd = process.cwd();
|
|
86
|
-
const workspace = await resolveWorkspace(options.workspace ?? cwd, cwd, homedir());
|
|
87
|
-
const processes = new SetupProcesses(controller.signal, process.env);
|
|
88
|
-
const targetVersion = options.version ?? await latestStableVersion(processes, cwd);
|
|
89
|
-
let cli;
|
|
90
|
-
if (process.env.PBOT_UPGRADING_CLI === targetVersion) {
|
|
91
|
-
cli = {
|
|
92
|
-
state: "updated",
|
|
93
|
-
previousVersion: process.env.PBOT_UPGRADING_PREVIOUS_VERSION || packageVersion,
|
|
94
|
-
version: packageVersion,
|
|
95
|
-
detail: `已将全局 CLI 更新到 ${packageVersion}。`,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
const cliResult = await upgradeManagedGlobalCli(targetVersion, process.env);
|
|
100
|
-
if (cliResult.state === "updated" && cliResult.globalDirectory) {
|
|
101
|
-
const nextEnv = {
|
|
102
|
-
...process.env,
|
|
103
|
-
PBOT_UPGRADING_CLI: targetVersion,
|
|
104
|
-
PBOT_UPGRADING_PREVIOUS_VERSION: packageVersion,
|
|
105
|
-
};
|
|
106
|
-
return await execCliEntry(join(cliResult.globalDirectory, "bin", "pbot.mjs"), [rawVerb, ...rawArgs], nextEnv);
|
|
107
|
-
}
|
|
108
|
-
cli = {
|
|
109
|
-
state: cliResult.state,
|
|
110
|
-
previousVersion: packageVersion,
|
|
111
|
-
version: packageVersion,
|
|
112
|
-
detail: cliResult.detail,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
const targets = await discoverWorkspaceTargets(workspace, options.hosts);
|
|
116
|
-
const summary = await executeWorkspaceUpgrade({
|
|
117
|
-
workspaceRoot: workspace,
|
|
118
|
-
targetVersion,
|
|
119
|
-
cli,
|
|
120
|
-
targets,
|
|
121
|
-
resolveUpgrader: getHostUpgrader,
|
|
122
|
-
createContext: target => ({
|
|
123
|
-
workspace,
|
|
124
|
-
targetVersion,
|
|
125
|
-
...(target.currentVersion ? { currentVersion: target.currentVersion } : {}),
|
|
126
|
-
packageRoot,
|
|
127
|
-
...(target.bindingPath ? { bindingPath: target.bindingPath } : {}),
|
|
128
|
-
env: process.env,
|
|
129
|
-
signal: controller.signal,
|
|
130
|
-
options: { dshProfile: options.dshProfile },
|
|
131
|
-
run: (command, args, runCwd, runOptions) => processes.run(command, args, runCwd, runOptions),
|
|
132
|
-
findExecutable: name => findExecutable(name, process.env),
|
|
133
|
-
}),
|
|
134
|
-
signal: controller.signal,
|
|
135
|
-
});
|
|
136
|
-
if (options.json)
|
|
137
|
-
console.log(JSON.stringify(summary, null, 2));
|
|
138
|
-
else
|
|
139
|
-
printHumanSummary(summary);
|
|
140
|
-
return summary.ok ? 0 : 1;
|
|
141
|
-
}
|
|
142
|
-
catch (error) {
|
|
143
|
-
if (controller.signal.aborted) {
|
|
144
|
-
console.error("升级已取消。");
|
|
145
|
-
return 130;
|
|
146
|
-
}
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
finally {
|
|
150
|
-
process.off("SIGINT", cancel);
|
|
151
|
-
process.off("SIGTERM", cancel);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { execFile, spawn } from "node:child_process";
|
|
2
|
-
import { lstat, mkdir, readFile, realpath, rmdir } from "node:fs/promises";
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
import { dirname, join, resolve } from "node:path";
|
|
5
|
-
import { promisify } from "node:util";
|
|
6
|
-
import { gt, lt, valid } from "semver";
|
|
7
|
-
import { object } from "../../common/json.js";
|
|
8
|
-
import { resourceRoot } from "../../common/paths.js";
|
|
9
|
-
import { requireThat } from "../../common/validation.js";
|
|
10
|
-
import { packageVersion } from "../../common/version.js";
|
|
11
|
-
/** Check exact installed manifests, never resolve dependencies in an ancestor workspace. */
|
|
12
|
-
export async function minimumCliRelease(root, plugins) {
|
|
13
|
-
const versions = Object.values(plugins);
|
|
14
|
-
if (process.env.PBOT_PLUGIN_VERSION)
|
|
15
|
-
versions.push(process.env.PBOT_PLUGIN_VERSION);
|
|
16
|
-
for (const prefix of ["", ".pi/npm"]) {
|
|
17
|
-
const path = join(root, prefix, "node_modules/personal-bot/package.json");
|
|
18
|
-
try {
|
|
19
|
-
const manifest = JSON.parse(await readFile(path, "utf8"));
|
|
20
|
-
requireThat(object(manifest) && manifest.name === "personal-bot" && typeof manifest.version === "string", "无效的 personal-bot 安装清单:" + path);
|
|
21
|
-
versions.push(manifest.version);
|
|
22
|
-
}
|
|
23
|
-
catch (error) {
|
|
24
|
-
if (error.code !== "ENOENT")
|
|
25
|
-
throw error;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
let required;
|
|
29
|
-
for (const version of versions) {
|
|
30
|
-
requireThat(valid(version) && /^[0-9]/.test(version) && version === version.trim(), "无效的插件发布版本:" + version);
|
|
31
|
-
if (!required || gt(version, required))
|
|
32
|
-
required = version;
|
|
33
|
-
}
|
|
34
|
-
return required;
|
|
35
|
-
}
|
|
36
|
-
async function runProgram(command, args, env, install = false, input) {
|
|
37
|
-
const child = spawn(command, args, install
|
|
38
|
-
? { cwd: homedir(), stdio: ["ignore", 2, 2], env }
|
|
39
|
-
: { stdio: input === undefined ? "inherit" : ["pipe", "inherit", "inherit"], env });
|
|
40
|
-
const interrupt = () => child.kill("SIGINT");
|
|
41
|
-
const terminate = () => child.kill("SIGTERM");
|
|
42
|
-
process.on("SIGINT", interrupt);
|
|
43
|
-
process.on("SIGTERM", terminate);
|
|
44
|
-
try {
|
|
45
|
-
return await new Promise((resolveCode, reject) => {
|
|
46
|
-
child.once("error", reject);
|
|
47
|
-
child.once("close", (code, signal) => resolveCode(code ?? (signal === "SIGINT" ? 130 : 143)));
|
|
48
|
-
if (input !== undefined && child.stdin) {
|
|
49
|
-
child.stdin.on("error", (error) => { if (error.code !== "EPIPE")
|
|
50
|
-
reject(error); });
|
|
51
|
-
child.stdin.end(input);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
finally {
|
|
56
|
-
process.off("SIGINT", interrupt);
|
|
57
|
-
process.off("SIGTERM", terminate);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
async function globalPackage() {
|
|
61
|
-
const { stdout } = await promisify(execFile)("npm", ["root", "--global"], { cwd: homedir() });
|
|
62
|
-
const directory = join(stdout.trim(), "personal-bot");
|
|
63
|
-
try {
|
|
64
|
-
// An npm link points at a development checkout; keep updates out of that checkout.
|
|
65
|
-
if ((await lstat(directory)).isDirectory() && await realpath(directory) === await realpath(resolve(resourceRoot, "..")))
|
|
66
|
-
return directory;
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
if (error.code !== "ENOENT")
|
|
70
|
-
throw error;
|
|
71
|
-
}
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
async function upgradeGlobal(directory, required, env) {
|
|
75
|
-
const lib = dirname(dirname(directory));
|
|
76
|
-
const lock = join(lib, ".pbot-cli-update.lock");
|
|
77
|
-
try {
|
|
78
|
-
await mkdir(lock);
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
// A read-only prefix or an active updater can still use npm's per-user versioned cache.
|
|
82
|
-
if (["EEXIST", "EACCES", "EPERM", "EROFS"].includes(error.code ?? ""))
|
|
83
|
-
return undefined;
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
86
|
-
const installedVersion = async () => {
|
|
87
|
-
const installed = JSON.parse(await readFile(join(directory, "package.json"), "utf8"));
|
|
88
|
-
requireThat(object(installed) && installed.name === "personal-bot" && typeof installed.version === "string" && valid(installed.version), "全局 CLI 安装清单无效:" + directory);
|
|
89
|
-
return installed.version;
|
|
90
|
-
};
|
|
91
|
-
try {
|
|
92
|
-
if (lt(await installedVersion(), required)) {
|
|
93
|
-
const code = await runProgram("npm", ["install", "--global", "--prefix", dirname(lib), "--ignore-scripts",
|
|
94
|
-
"--no-audit", "--no-fund", "--prefer-offline", "personal-bot@" + required], env, true);
|
|
95
|
-
if (code)
|
|
96
|
-
return code;
|
|
97
|
-
requireThat(!lt(await installedVersion(), required), "npm 更新后的 CLI 低于插件所需版本:" + required);
|
|
98
|
-
}
|
|
99
|
-
return 0;
|
|
100
|
-
}
|
|
101
|
-
finally {
|
|
102
|
-
await rmdir(lock);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
export async function upgradeManagedGlobalCli(required, env = process.env) {
|
|
106
|
-
requireThat(valid(required) && /^[0-9]/.test(required) && required === required.trim(), "无效的插件发布版本:" + required);
|
|
107
|
-
if (!lt(packageVersion, required)) {
|
|
108
|
-
return { state: "up-to-date", detail: `CLI 已是 ${packageVersion}。` };
|
|
109
|
-
}
|
|
110
|
-
const global = await globalPackage();
|
|
111
|
-
if (!global) {
|
|
112
|
-
return { state: "skipped", detail: `当前运行的是本地或非全局安装(${packageVersion}),跳过全局 CLI 覆盖。` };
|
|
113
|
-
}
|
|
114
|
-
const status = await upgradeGlobal(global, required, env);
|
|
115
|
-
if (status === undefined) {
|
|
116
|
-
return { state: "skipped", globalDirectory: global, detail: "全局 npm 目录不可写或正被其他进程更新,已跳过全局 CLI 覆盖。" };
|
|
117
|
-
}
|
|
118
|
-
if (status !== 0) {
|
|
119
|
-
return { state: "failed", globalDirectory: global, exitCode: status, detail: `npm 更新全局 personal-bot@${required} 失败(退出码 ${status})。` };
|
|
120
|
-
}
|
|
121
|
-
return { state: "updated", globalDirectory: global, detail: `已将全局 CLI 从 ${packageVersion} 更新到 ${required}。` };
|
|
122
|
-
}
|
|
123
|
-
export function execCliEntry(entry, args, env = process.env) {
|
|
124
|
-
return runProgram(process.execPath, [entry, ...args], env, false);
|
|
125
|
-
}
|
|
126
|
-
/** Upgrade managed global installs in place. Project, npx and linked installs use npm's shared version cache. */
|
|
127
|
-
export async function runUpdatedCli(required, args, input) {
|
|
128
|
-
if (!required)
|
|
129
|
-
return undefined;
|
|
130
|
-
requireThat(valid(required) && /^[0-9]/.test(required) && required === required.trim(), "无效的插件发布版本:" + required);
|
|
131
|
-
if (!lt(packageVersion, required))
|
|
132
|
-
return undefined;
|
|
133
|
-
requireThat(process.env.PBOT_CLI_UPDATING !== required, `npm 启动了 CLI ${packageVersion},工作区插件需要 ${required}。`);
|
|
134
|
-
console.error(`工作区需要 Personal Bot CLI ${required},正在更新 ${packageVersion}。`);
|
|
135
|
-
const env = { ...process.env, PBOT_CLI_UPDATING: required };
|
|
136
|
-
const global = await globalPackage();
|
|
137
|
-
if (global) {
|
|
138
|
-
const status = await upgradeGlobal(global, required, env);
|
|
139
|
-
if (status !== undefined) {
|
|
140
|
-
if (status !== 0)
|
|
141
|
-
return status;
|
|
142
|
-
return runProgram(process.execPath, [join(global, "bin/pbot.mjs"), ...args], env, false, input);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return runProgram("npx", ["--yes", "--ignore-scripts", "--prefer-offline", "personal-bot@" + required, ...args], env, false, input);
|
|
146
|
-
}
|