personal-bot 0.3.4 → 0.3.6
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/README.md +191 -65
- package/README.zh-CN.md +189 -63
- package/bin/pbot.mjs +3 -17
- package/docs/README.md +1 -1
- package/docs/architecture/design.md +10 -9
- package/docs/architecture/extensibility.md +1 -1
- package/docs/architecture/principles.md +1 -1
- package/docs/hosts/antigravity.md +4 -2
- package/docs/hosts/pi.md +4 -15
- package/docs/maintainers/release.md +2 -2
- 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 +22495 -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 +22495 -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/install.ts +77 -0
- package/src/extension/cli/commands/mcp.ts +165 -0
- package/src/extension/cli/commands/plugin.ts +33 -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 +192 -0
- package/src/extension/cli/types.ts +30 -0
- package/src/integrations/installation/cli-update.ts +3 -3
- package/src/integrations/installation/installers/antigravity.ts +49 -0
- package/src/integrations/installation/installers/claude.ts +30 -0
- package/src/integrations/installation/installers/codex.ts +56 -0
- package/src/integrations/installation/installers/dsh.ts +25 -0
- package/src/integrations/installation/installers/pi.ts +26 -0
- package/src/integrations/installation/installers/qoder.ts +49 -0
- package/src/integrations/installation/installers/registry.ts +48 -0
- package/src/integrations/installation/processes.ts +2 -41
- package/src/model/installation.ts +28 -36
- package/src/services/installation/install-service.ts +107 -0
- package/lib/common/package-source.js +0 -16
- package/lib/extension/setup/cli.js +0 -364
- package/lib/extension/setup/guard.js +0 -11
- package/lib/extension/setup/upgrade.js +0 -153
- package/lib/integrations/installation/cli-update.js +0 -146
- package/lib/integrations/installation/pi-bridge.js +0 -131
- 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/model/installation.js +0 -1
- 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/resources/installer/plugins.json +0 -16
- package/src/extension/setup/cli.ts +0 -303
- package/src/extension/setup/guard.ts +0 -12
- package/src/extension/setup/upgrade.ts +0 -158
- package/src/extension/setup/workspace-cli.ts +0 -167
- package/src/integrations/installation/pi-bridge.ts +0 -109
- package/src/integrations/installation/pi-setup.ts +0 -162
- package/src/services/installation/setup-service.ts +0 -126
- package/src/ui/setup.ts +0 -40
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import { readFile, stat } from "node:fs/promises";
|
|
3
|
-
import { join, resolve } from "node:path";
|
|
4
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
|
-
import { npmName } from "../../common/package-source.js";
|
|
6
|
-
async function sdkAt(root) {
|
|
7
|
-
const manifest = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
|
|
8
|
-
if (manifest.name !== "@earendil-works/pi-coding-agent")
|
|
9
|
-
throw new Error("不是支持的 Pi 安装。");
|
|
10
|
-
return import(pathToFileURL(join(root, "dist/index.js")).href);
|
|
11
|
-
}
|
|
12
|
-
const sourceOf = (entry) => {
|
|
13
|
-
if (typeof entry === "string")
|
|
14
|
-
return entry;
|
|
15
|
-
if (entry && typeof entry === "object" && "source" in entry && typeof entry.source === "string")
|
|
16
|
-
return entry.source;
|
|
17
|
-
throw new Error("无效的 Pi packages 声明。");
|
|
18
|
-
};
|
|
19
|
-
export async function inspectPackages(root, workspace) {
|
|
20
|
-
const sdk = await sdkAt(root), agentDir = sdk.getAgentDir();
|
|
21
|
-
// Inspect project declarations as data only. This does not load extensions, execute
|
|
22
|
-
// project npmCommand, grant project trust, or persist migrated settings.
|
|
23
|
-
const settings = sdk.SettingsManager.fromStorage({ withLock(scope, read) {
|
|
24
|
-
const file = scope === "global" ? join(agentDir, "settings.json") : join(workspace, sdk.CONFIG_DIR_NAME, "settings.json");
|
|
25
|
-
let content;
|
|
26
|
-
try {
|
|
27
|
-
content = readFileSync(file, "utf8");
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
if (error.code !== "ENOENT")
|
|
31
|
-
throw error;
|
|
32
|
-
}
|
|
33
|
-
if (content !== undefined) {
|
|
34
|
-
const value = JSON.parse(content);
|
|
35
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
36
|
-
throw new Error(`无效的 Pi 配置:${file}`);
|
|
37
|
-
const packages = value.packages;
|
|
38
|
-
if (packages !== undefined) {
|
|
39
|
-
if (!Array.isArray(packages))
|
|
40
|
-
throw new Error(`packages 必须是数组:${file}`);
|
|
41
|
-
packages.forEach(sourceOf);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (read(content) !== undefined)
|
|
45
|
-
throw new Error("安装检查不得修改 Pi 配置。");
|
|
46
|
-
} }, { projectTrusted: true });
|
|
47
|
-
const errors = settings.drainErrors();
|
|
48
|
-
if (errors.length)
|
|
49
|
-
throw new Error(errors.map(item => `${item.scope}: ${item.error.message}`).join("\n"));
|
|
50
|
-
const manager = new sdk.DefaultPackageManager({ cwd: workspace, agentDir, settingsManager: settings });
|
|
51
|
-
const result = [];
|
|
52
|
-
for (const pkg of manager.listConfiguredPackages()) {
|
|
53
|
-
const entries = (pkg.scope === "project" ? settings.getProjectSettings() : settings.getGlobalSettings()).packages ?? [];
|
|
54
|
-
const entry = entries.find(item => sourceOf(item) === pkg.source);
|
|
55
|
-
let name = npmName(pkg.source), version, singleFile = false;
|
|
56
|
-
if (pkg.installedPath) {
|
|
57
|
-
const info = await stat(pkg.installedPath);
|
|
58
|
-
singleFile = info.isFile(); // An existing standalone extension has no npm identity.
|
|
59
|
-
if (info.isDirectory()) {
|
|
60
|
-
try {
|
|
61
|
-
const manifest = JSON.parse(await readFile(join(pkg.installedPath, "package.json"), "utf8"));
|
|
62
|
-
if (name && manifest.name !== name)
|
|
63
|
-
throw new Error(`安装包身份不匹配:${pkg.source}`);
|
|
64
|
-
name = typeof manifest.name === "string" && manifest.name.trim() ? manifest.name : name;
|
|
65
|
-
version = typeof manifest.version === "string" ? manifest.version : undefined;
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
if (error.code !== "ENOENT")
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (!name && !singleFile) {
|
|
74
|
-
throw new Error(`无法辨认 ${pkg.scope} 来源的包身份:${pkg.source}。请先通过 Pi 原生配置恢复或修正该来源,再重新运行 setup。`);
|
|
75
|
-
}
|
|
76
|
-
result.push({ source: pkg.source, scope: pkg.scope, filtered: pkg.filtered, entry,
|
|
77
|
-
...(pkg.installedPath ? { installedPath: pkg.installedPath } : {}), ...(name ? { name } : {}), ...(version ? { version } : {}) });
|
|
78
|
-
}
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
-
export async function replaceProjectSource(root, workspace, old, target) {
|
|
82
|
-
const fromProject = old.scope === "project" && !npmName(old.source);
|
|
83
|
-
const fromUser = old.scope === "user" && !!npmName(old.source);
|
|
84
|
-
if (!fromProject && !fromUser)
|
|
85
|
-
throw new Error("此操作只替换工作区自定义来源,或继承全局 npm 资源过滤。");
|
|
86
|
-
const sdk = await sdkAt(root);
|
|
87
|
-
const settings = sdk.SettingsManager.create(workspace, sdk.getAgentDir(), { projectTrusted: true });
|
|
88
|
-
const errors = settings.drainErrors();
|
|
89
|
-
if (errors.length)
|
|
90
|
-
throw new Error(errors.map(item => item.error.message).join("\n"));
|
|
91
|
-
const packages = settings.getProjectSettings().packages ?? [];
|
|
92
|
-
const originals = fromProject ? packages : settings.getGlobalSettings().packages ?? [];
|
|
93
|
-
const previous = originals.find(entry => sourceOf(entry) === old.source);
|
|
94
|
-
if (previous === undefined || JSON.stringify(previous) !== JSON.stringify(old.entry))
|
|
95
|
-
throw new Error("插件配置已发生变化,请重新运行 setup。");
|
|
96
|
-
if (!packages.some(entry => sourceOf(entry) === target))
|
|
97
|
-
throw new Error("目标包尚未安装,保留原声明。");
|
|
98
|
-
const manager = new sdk.DefaultPackageManager({ cwd: workspace, agentDir: sdk.getAgentDir(), settingsManager: settings });
|
|
99
|
-
const installed = manager.getInstalledPath(target, "project");
|
|
100
|
-
if (!installed || !npmName(target))
|
|
101
|
-
throw new Error("目标 npm 包尚未安装,保留原声明。");
|
|
102
|
-
const manifest = JSON.parse(await readFile(join(installed, "package.json"), "utf8"));
|
|
103
|
-
if (manifest.name !== npmName(target))
|
|
104
|
-
throw new Error("目标安装包身份不符,保留原声明。");
|
|
105
|
-
// Change only declarations; do not call remove(), which deletes managed git caches.
|
|
106
|
-
const replacement = typeof previous === "string" ? target : { ...previous, source: target };
|
|
107
|
-
const next = fromProject
|
|
108
|
-
? packages.filter(entry => sourceOf(entry) !== target).map(entry => sourceOf(entry) === old.source ? replacement : entry)
|
|
109
|
-
: packages.map(entry => sourceOf(entry) === target ? replacement : entry);
|
|
110
|
-
settings.setProjectPackages(next);
|
|
111
|
-
await settings.flush();
|
|
112
|
-
const failures = settings.drainErrors();
|
|
113
|
-
if (failures.length)
|
|
114
|
-
throw new Error(failures.map(item => item.error.message).join("\n"));
|
|
115
|
-
}
|
|
116
|
-
async function main() {
|
|
117
|
-
const [action, root, workspace, input] = process.argv.slice(2);
|
|
118
|
-
if (!root || !workspace)
|
|
119
|
-
throw new Error("缺少 Pi 路径或工作区。");
|
|
120
|
-
if (action === "inspect")
|
|
121
|
-
console.log(JSON.stringify(await inspectPackages(root, workspace)));
|
|
122
|
-
else if (action === "replace" && input) {
|
|
123
|
-
const { old, target } = JSON.parse(input);
|
|
124
|
-
await replaceProjectSource(root, workspace, old, target);
|
|
125
|
-
}
|
|
126
|
-
else
|
|
127
|
-
throw new Error("无效的安装检查操作。");
|
|
128
|
-
}
|
|
129
|
-
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
130
|
-
await main().catch(error => { console.error(error instanceof Error ? error.message : String(error)); process.exitCode = 1; });
|
|
131
|
-
}
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { access, readFile, realpath } from "node:fs/promises";
|
|
2
|
-
import { constants } from "node:fs";
|
|
3
|
-
import { dirname, join, resolve } from "node:path";
|
|
4
|
-
import { homedir } from "node:os";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { checkStableVersion, npmName } from "../../common/package-source.js";
|
|
7
|
-
import { findExecutable, npmGlobalBinary, SetupProcesses } from "./processes.js";
|
|
8
|
-
export const piPackageName = "@earendil-works/pi-coding-agent";
|
|
9
|
-
export class PiBootstrapRequired extends Error {
|
|
10
|
-
}
|
|
11
|
-
const bridge = fileURLToPath(new URL("./pi-bridge.js", import.meta.url));
|
|
12
|
-
const guard = fileURLToPath(new URL("../../extension/setup/guard.js", import.meta.url));
|
|
13
|
-
async function packageRoot(command, version, env) {
|
|
14
|
-
const resolved = await realpath(command);
|
|
15
|
-
let directory = dirname(resolved);
|
|
16
|
-
while (true) {
|
|
17
|
-
try {
|
|
18
|
-
const pkg = JSON.parse(await readFile(join(directory, "package.json"), "utf8"));
|
|
19
|
-
if (pkg.name === piPackageName && pkg.version === version)
|
|
20
|
-
return directory;
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
if (error.code !== "ENOENT")
|
|
24
|
-
throw error;
|
|
25
|
-
}
|
|
26
|
-
const parent = dirname(directory);
|
|
27
|
-
if (parent === directory)
|
|
28
|
-
break;
|
|
29
|
-
directory = parent;
|
|
30
|
-
}
|
|
31
|
-
const roots = [join(dirname(dirname(resolved)), "install"), env.PI_MANAGED_INSTALL_ROOT].filter((root) => !!root);
|
|
32
|
-
for (const root of roots) {
|
|
33
|
-
try {
|
|
34
|
-
// The native managed launcher resides beside install/. Only accept a matching
|
|
35
|
-
// launcher, not an unrelated PI_MANAGED_INSTALL_ROOT inherited from another Pi.
|
|
36
|
-
const launcher = await realpath(join(dirname(root), "bin/pi"));
|
|
37
|
-
if (launcher !== resolved)
|
|
38
|
-
continue;
|
|
39
|
-
const current = (await readFile(join(root, "current-version"), "utf8")).trim();
|
|
40
|
-
if (current !== version || !/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(current))
|
|
41
|
-
continue;
|
|
42
|
-
const candidate = join(root, "releases", current, "node_modules", piPackageName);
|
|
43
|
-
const pkg = JSON.parse(await readFile(join(candidate, "package.json"), "utf8"));
|
|
44
|
-
if (pkg.name === piPackageName && pkg.version === version)
|
|
45
|
-
return candidate;
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
if (error.code !== "ENOENT")
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
throw new Error(`无法定位 ${command} 对应的 Pi 包。请先按此 Pi 的原生安装方式修复,不会覆盖该命令。`);
|
|
53
|
-
}
|
|
54
|
-
export class PiSetup {
|
|
55
|
-
processes;
|
|
56
|
-
npm;
|
|
57
|
-
cwd;
|
|
58
|
-
constructor(processes, npm, cwd) {
|
|
59
|
-
this.processes = processes;
|
|
60
|
-
this.npm = npm;
|
|
61
|
-
this.cwd = cwd;
|
|
62
|
-
}
|
|
63
|
-
async inspectPi(command) {
|
|
64
|
-
const executable = command ?? await findExecutable("pi", this.processes.env);
|
|
65
|
-
if (!executable)
|
|
66
|
-
return undefined;
|
|
67
|
-
const version = await this.processes.run(executable, ["--version"], this.cwd);
|
|
68
|
-
if (!/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(version))
|
|
69
|
-
throw new Error(`PATH 中的 ${executable} 不是可识别的 Pi。`);
|
|
70
|
-
return { command: executable, version, root: await packageRoot(executable, version, this.processes.env) };
|
|
71
|
-
}
|
|
72
|
-
async latest(name) {
|
|
73
|
-
const text = await this.processes.run(this.npm, ["view", `${name}@latest`, "version", "engines", "--json",
|
|
74
|
-
"--fetch-retries=0", "--fetch-timeout=15000"], this.cwd);
|
|
75
|
-
const value = JSON.parse(text);
|
|
76
|
-
if (typeof value.version !== "string")
|
|
77
|
-
throw new Error(`无法取得 ${name} 的最新版本。`);
|
|
78
|
-
return checkStableVersion(value.version, value.engines?.node);
|
|
79
|
-
}
|
|
80
|
-
async inspectPackages(pi, workspace) {
|
|
81
|
-
if (!pi) {
|
|
82
|
-
// A fresh machine can be planned without loading a Pi SDK. Existing declarations
|
|
83
|
-
// require Pi's own resolver; don't guess git cache paths or package identities.
|
|
84
|
-
const agentDir = this.processes.env.PI_CODING_AGENT_DIR ?? join(this.processes.env.HOME ?? homedir(), ".pi/agent");
|
|
85
|
-
for (const file of [join(agentDir, "settings.json"), join(workspace, ".pi/settings.json")]) {
|
|
86
|
-
try {
|
|
87
|
-
const settings = JSON.parse(await readFile(file, "utf8"));
|
|
88
|
-
if (!settings || typeof settings !== "object" || Array.isArray(settings))
|
|
89
|
-
throw new Error(`无效的 Pi 配置:${file}`);
|
|
90
|
-
const packages = settings.packages;
|
|
91
|
-
if (packages !== undefined && !Array.isArray(packages))
|
|
92
|
-
throw new Error(`packages 必须是数组:${file}`);
|
|
93
|
-
if (Array.isArray(packages) && packages.length > 0) {
|
|
94
|
-
throw new PiBootstrapRequired(`已有插件配置但未找到 Pi:${file}`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
if (error.code !== "ENOENT")
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return [];
|
|
103
|
-
}
|
|
104
|
-
const data = await this.processes.run(process.execPath, [bridge, "inspect", pi.root, workspace], this.cwd);
|
|
105
|
-
return JSON.parse(data);
|
|
106
|
-
}
|
|
107
|
-
async installTmux(command) {
|
|
108
|
-
await this.processes.run(command.command, command.args, this.cwd, { interactive: true });
|
|
109
|
-
const tmux = await findExecutable("tmux", this.processes.env);
|
|
110
|
-
if (!tmux)
|
|
111
|
-
throw new Error("tmux 安装后仍不在 PATH 中,请修复 PATH 后重试。");
|
|
112
|
-
await this.processes.run(tmux, ["-V"], this.cwd);
|
|
113
|
-
}
|
|
114
|
-
async ensurePi(existing, version) {
|
|
115
|
-
if (existing) {
|
|
116
|
-
if (existing.version !== version)
|
|
117
|
-
await this.processes.run(existing.command, ["update"], this.cwd, { interactive: true });
|
|
118
|
-
const installed = await this.inspectPi(existing.command);
|
|
119
|
-
if (!installed || installed.version !== version)
|
|
120
|
-
throw new Error(`Pi 实际版本 ${installed?.version ?? "未知"} 与目标 ${version} 不同。请用原安装方式更新后重试。`);
|
|
121
|
-
return installed;
|
|
122
|
-
}
|
|
123
|
-
const prefix = await this.processes.run(this.npm, ["prefix", "--global"], this.cwd);
|
|
124
|
-
if (!prefix || !prefix.startsWith("/"))
|
|
125
|
-
throw new Error("无法确定 npm 全局安装目录。");
|
|
126
|
-
try {
|
|
127
|
-
await access(prefix, constants.W_OK);
|
|
128
|
-
}
|
|
129
|
-
catch {
|
|
130
|
-
throw new Error(`npm 全局目录不可写:${prefix}。请先配置当前用户的 npm prefix 或 Node.js 版本管理器。`);
|
|
131
|
-
}
|
|
132
|
-
await this.processes.run(this.npm, ["install", "--global", "--ignore-scripts", "--engine-strict",
|
|
133
|
-
`${piPackageName}@${version}`], this.cwd, { interactive: true });
|
|
134
|
-
const installed = await this.inspectPi(npmGlobalBinary(prefix));
|
|
135
|
-
if (!installed || installed.version !== version)
|
|
136
|
-
throw new Error("Pi 安装版本验证失败。");
|
|
137
|
-
const onPath = await findExecutable("pi", this.processes.env);
|
|
138
|
-
if (!onPath || await realpath(onPath) !== await realpath(installed.command)) {
|
|
139
|
-
throw new Error(`Pi 已安装到 ${installed.command},请把 ${join(prefix, "bin")} 加入 PATH 后重新运行 setup。`);
|
|
140
|
-
}
|
|
141
|
-
return installed;
|
|
142
|
-
}
|
|
143
|
-
async verifyKeptPackage(pi, workspace, choice) {
|
|
144
|
-
const old = choice.existing[0];
|
|
145
|
-
if (choice.existing.length !== 1 || !old)
|
|
146
|
-
throw new Error("保留来源不可用或存在多份来源,请先通过 pi config 处理。");
|
|
147
|
-
const current = (await this.inspectPackages(pi, workspace)).find(pkg => pkg.source === old.source && pkg.scope === old.scope);
|
|
148
|
-
if (!current?.installedPath || current.name !== npmName(choice.plugin.source) || JSON.stringify(current.entry) !== JSON.stringify(old.entry)) {
|
|
149
|
-
throw new Error("保留来源已变化或不可读取,请重新运行 setup。");
|
|
150
|
-
}
|
|
151
|
-
return current.source;
|
|
152
|
-
}
|
|
153
|
-
async installPackage(pi, workspace, choice) {
|
|
154
|
-
const name = npmName(choice.plugin.source);
|
|
155
|
-
if (!name || !choice.target)
|
|
156
|
-
throw new Error("缺少目标包或目标版本。");
|
|
157
|
-
// Re-read just before mutation so a stale plan cannot replace a changed source.
|
|
158
|
-
const current = (await this.inspectPackages(pi, workspace)).filter(pkg => pkg.name === name);
|
|
159
|
-
for (const old of choice.existing) {
|
|
160
|
-
const actual = current.find(pkg => pkg.scope === old.scope && pkg.source === old.source);
|
|
161
|
-
if (!actual || JSON.stringify(actual.entry) !== JSON.stringify(old.entry))
|
|
162
|
-
throw new Error(`${name} 配置已改变,请重新运行 setup。`);
|
|
163
|
-
}
|
|
164
|
-
if (current.some(pkg => !choice.existing.some(old => old.scope === pkg.scope && old.source === pkg.source) &&
|
|
165
|
-
!(pkg.scope === "user" && npmName(pkg.source) && choice.existing.some(old => old.scope === "project" && npmName(old.source))))) {
|
|
166
|
-
throw new Error(`${name} 出现新的来源,请重新运行 setup。`);
|
|
167
|
-
}
|
|
168
|
-
const otherSources = choice.existing.filter(pkg => !npmName(pkg.source));
|
|
169
|
-
if (otherSources.length > 1 || otherSources.some(pkg => pkg.scope === "user")) {
|
|
170
|
-
throw new Error("存在不同身份的全局来源或多份自定义来源。请先用 pi config 调整加载范围后重试。");
|
|
171
|
-
}
|
|
172
|
-
const globals = choice.existing.filter(pkg => pkg.scope === "user" && npmName(pkg.source));
|
|
173
|
-
if (globals.length > 1 && globals.some(pkg => pkg.filtered))
|
|
174
|
-
throw new Error("存在多份全局 npm 资源过滤,请先用 pi config 调整后重试。");
|
|
175
|
-
const template = otherSources[0] ?? globals.find(pkg => pkg.filtered);
|
|
176
|
-
const target = `npm:${name}@latest`;
|
|
177
|
-
await this.processes.run(pi.command, ["install", "--local", target], workspace, { interactive: true });
|
|
178
|
-
const records = await this.inspectPackages(pi, workspace);
|
|
179
|
-
const installed = records.find(pkg => pkg.scope === "project" && pkg.source === target);
|
|
180
|
-
if (!installed?.installedPath || installed.version !== choice.target) {
|
|
181
|
-
throw new Error(`${name} 实际版本 ${installed?.version ?? "未知"} 与目标 ${choice.target} 不同,请检查 registry 后重试。`);
|
|
182
|
-
}
|
|
183
|
-
if (template) {
|
|
184
|
-
await this.processes.run(process.execPath, [bridge, "replace", pi.root, workspace,
|
|
185
|
-
JSON.stringify({ old: template, target })], this.cwd);
|
|
186
|
-
}
|
|
187
|
-
return installed.version;
|
|
188
|
-
}
|
|
189
|
-
async start(pi, workspace) {
|
|
190
|
-
await access(guard, constants.R_OK);
|
|
191
|
-
await this.processes.run(pi.command, ["--extension", guard, "/pbot init"], resolve(workspace), { interactive: true, timeoutMs: 0 });
|
|
192
|
-
}
|
|
193
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { access, realpath, stat } from "node:fs/promises";
|
|
3
|
-
import { constants } from "node:fs";
|
|
4
|
-
import { delimiter, isAbsolute, join, resolve } from "node:path";
|
|
5
|
-
export async function findExecutable(name, env = process.env) {
|
|
6
|
-
const directories = (env.PATH ?? "").split(delimiter).filter(Boolean);
|
|
7
|
-
for (const directory of directories) {
|
|
8
|
-
const path = resolve(directory, name);
|
|
9
|
-
// npm exec prepends transient bins. Never use a cached peer as the user's Pi.
|
|
10
|
-
if (name === "pi" && path.split(/[\\/]/).includes("_npx"))
|
|
11
|
-
continue;
|
|
12
|
-
try {
|
|
13
|
-
await access(path, constants.X_OK);
|
|
14
|
-
if (!(await stat(path)).isFile())
|
|
15
|
-
continue;
|
|
16
|
-
const target = await realpath(path);
|
|
17
|
-
if (name === "pi" && target.split(/[\\/]/).includes("_npx"))
|
|
18
|
-
continue;
|
|
19
|
-
return path;
|
|
20
|
-
}
|
|
21
|
-
catch (error) {
|
|
22
|
-
if (!["ENOENT", "ENOTDIR", "EACCES"].includes(error.code ?? ""))
|
|
23
|
-
throw error;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
export class SetupProcesses {
|
|
29
|
-
signal;
|
|
30
|
-
env;
|
|
31
|
-
constructor(signal, env = process.env) {
|
|
32
|
-
this.signal = signal;
|
|
33
|
-
this.env = env;
|
|
34
|
-
}
|
|
35
|
-
async run(command, args, cwd, options = {}) {
|
|
36
|
-
this.signal.throwIfAborted();
|
|
37
|
-
if (!isAbsolute(cwd))
|
|
38
|
-
throw new Error("进程工作目录必须是绝对路径。");
|
|
39
|
-
const interactive = options.interactive ?? false;
|
|
40
|
-
const timeoutMs = options.timeoutMs ?? (interactive ? 15 * 60_000 : 60_000);
|
|
41
|
-
return new Promise((resolveResult, reject) => {
|
|
42
|
-
const child = spawn(command, args, { cwd, env: this.env, shell: false, detached: !interactive,
|
|
43
|
-
stdio: interactive ? "inherit" : ["pipe", "pipe", "pipe"] });
|
|
44
|
-
const stdout = [], stderr = [];
|
|
45
|
-
let bytes = 0, failure, escalation;
|
|
46
|
-
const kill = (signal) => {
|
|
47
|
-
if (!child.pid)
|
|
48
|
-
return;
|
|
49
|
-
try {
|
|
50
|
-
if (interactive)
|
|
51
|
-
child.kill(signal);
|
|
52
|
-
else
|
|
53
|
-
process.kill(-child.pid, signal);
|
|
54
|
-
}
|
|
55
|
-
catch { /* Process already exited. */ }
|
|
56
|
-
};
|
|
57
|
-
const stop = (reason) => {
|
|
58
|
-
if (failure)
|
|
59
|
-
return;
|
|
60
|
-
failure = reason;
|
|
61
|
-
kill("SIGTERM");
|
|
62
|
-
escalation = setTimeout(() => kill("SIGKILL"), 2000);
|
|
63
|
-
};
|
|
64
|
-
const cancel = () => stop(new Error("安装已取消。"));
|
|
65
|
-
const timer = timeoutMs > 0 ? setTimeout(() => stop(new Error(`命令超时:${command}`)), timeoutMs) : undefined;
|
|
66
|
-
this.signal.addEventListener("abort", cancel, { once: true });
|
|
67
|
-
const collect = (chunks) => (chunk) => {
|
|
68
|
-
bytes += chunk.length;
|
|
69
|
-
if (bytes > 2 * 1024 * 1024)
|
|
70
|
-
stop(new Error(`命令输出超过限制:${command}`));
|
|
71
|
-
else
|
|
72
|
-
chunks.push(chunk);
|
|
73
|
-
};
|
|
74
|
-
child.stdout?.on("data", collect(stdout));
|
|
75
|
-
child.stderr?.on("data", collect(stderr));
|
|
76
|
-
child.stdin?.on("error", () => { });
|
|
77
|
-
child.on("error", error => { failure = error; });
|
|
78
|
-
child.on("close", (code, exitSignal) => {
|
|
79
|
-
clearTimeout(timer);
|
|
80
|
-
clearTimeout(escalation);
|
|
81
|
-
this.signal.removeEventListener("abort", cancel);
|
|
82
|
-
if (this.signal.aborted)
|
|
83
|
-
return reject(this.signal.reason);
|
|
84
|
-
if (failure)
|
|
85
|
-
return reject(failure);
|
|
86
|
-
if (code !== 0)
|
|
87
|
-
return reject(new Error(`${command} 失败 (${exitSignal ?? code})\n${Buffer.concat(stderr).toString("utf8").slice(-8000)}`));
|
|
88
|
-
resolveResult(Buffer.concat(stdout).toString("utf8").trim());
|
|
89
|
-
});
|
|
90
|
-
child.stdin?.end(options.input);
|
|
91
|
-
if (this.signal.aborted)
|
|
92
|
-
cancel();
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
export function tmuxInstallCommand(platform, commands, root) {
|
|
97
|
-
let command, args;
|
|
98
|
-
if (platform === "darwin") {
|
|
99
|
-
command = commands.get("brew");
|
|
100
|
-
args = ["install", "tmux"];
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
const manager = ["apt-get", "dnf", "pacman", "zypper", "apk"].find(name => commands.has(name));
|
|
104
|
-
command = manager ? commands.get(manager) : undefined;
|
|
105
|
-
args = manager === "apt-get" ? ["install", "-y", "tmux"] : manager === "dnf" ? ["install", "-y", "tmux"]
|
|
106
|
-
: manager === "pacman" ? ["-S", "--needed", "--noconfirm", "tmux"]
|
|
107
|
-
: manager === "zypper" ? ["--non-interactive", "install", "tmux"] : ["add", "tmux"];
|
|
108
|
-
if (command && !root) {
|
|
109
|
-
const sudo = commands.get("sudo");
|
|
110
|
-
if (!sudo)
|
|
111
|
-
throw new Error("安装 tmux 需要管理员权限;请手动安装 tmux 后重新运行 setup。");
|
|
112
|
-
return { command: sudo, args: [command, ...args] };
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (!command)
|
|
116
|
-
throw new Error(platform === "darwin"
|
|
117
|
-
? "未找到 Homebrew。请先通过现有环境安装 tmux,再运行 setup。"
|
|
118
|
-
: "未找到支持的系统包管理器。请手动安装 tmux,再运行 setup。");
|
|
119
|
-
return { command, args };
|
|
120
|
-
}
|
|
121
|
-
export async function inspectSystem(processes, cwd) {
|
|
122
|
-
if (!["linux", "darwin"].includes(process.platform))
|
|
123
|
-
throw new Error("setup 支持 Linux 和 macOS;Windows 请使用 WSL。");
|
|
124
|
-
const [major = 0, minor = 0] = process.versions.node.split(".").map(Number);
|
|
125
|
-
if (major < 22 || major === 22 && minor < 19)
|
|
126
|
-
throw new Error("需要 Node.js 22.19 或更高版本。");
|
|
127
|
-
const npm = await findExecutable("npm", processes.env);
|
|
128
|
-
if (!npm)
|
|
129
|
-
throw new Error("未找到 npm。请先安装 Node.js/npm。");
|
|
130
|
-
const tmux = await findExecutable("tmux", processes.env);
|
|
131
|
-
if (tmux) {
|
|
132
|
-
await processes.run(tmux, ["-V"], cwd);
|
|
133
|
-
return { npm };
|
|
134
|
-
}
|
|
135
|
-
const names = process.platform === "darwin" ? ["brew"] : ["apt-get", "dnf", "pacman", "zypper", "apk", "sudo"];
|
|
136
|
-
const entries = await Promise.all(names.map(async (name) => [name, await findExecutable(name, processes.env)]));
|
|
137
|
-
const commands = new Map(entries.filter((entry) => entry[1] !== undefined));
|
|
138
|
-
return { npm, tmux: tmuxInstallCommand(process.platform, commands, process.getuid?.() === 0) };
|
|
139
|
-
}
|
|
140
|
-
export function npmGlobalBinary(prefix) { return join(prefix, "bin", "pi"); }
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { gte, lt, valid } from "semver";
|
|
4
|
-
import { object } from "../../../common/json.js";
|
|
5
|
-
async function readManifestVersion(path) {
|
|
6
|
-
try {
|
|
7
|
-
const data = JSON.parse(await readFile(path, "utf8"));
|
|
8
|
-
return object(data) && typeof data.version === "string" && valid(data.version) ? data.version : undefined;
|
|
9
|
-
}
|
|
10
|
-
catch (error) {
|
|
11
|
-
if (error.code === "ENOENT")
|
|
12
|
-
return undefined;
|
|
13
|
-
throw error;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export const antigravityUpgrader = {
|
|
17
|
-
host: "antigravity",
|
|
18
|
-
async upgrade(context) {
|
|
19
|
-
if (context.currentVersion && valid(context.currentVersion) && gte(context.currentVersion, context.targetVersion)) {
|
|
20
|
-
return {
|
|
21
|
-
host: this.host,
|
|
22
|
-
state: "up-to-date",
|
|
23
|
-
previousVersion: context.currentVersion,
|
|
24
|
-
version: context.currentVersion,
|
|
25
|
-
scope: "user",
|
|
26
|
-
detail: "已是最新版本。",
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
const pluginDir = join(context.packageRoot, "lib", "antigravity-plugin");
|
|
30
|
-
const bundledVersion = await readManifestVersion(join(pluginDir, "plugin.json"));
|
|
31
|
-
if (!bundledVersion) {
|
|
32
|
-
return {
|
|
33
|
-
host: this.host,
|
|
34
|
-
state: "skipped",
|
|
35
|
-
...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
|
|
36
|
-
detail: `缺少 Antigravity 插件构建产物:${pluginDir}`,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
if (lt(bundledVersion, context.targetVersion)) {
|
|
40
|
-
return {
|
|
41
|
-
host: this.host,
|
|
42
|
-
state: "skipped",
|
|
43
|
-
...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
|
|
44
|
-
detail: `当前本地包产物版本为 ${bundledVersion},低于目标版本 ${context.targetVersion};请先更新全局 personal-bot 包。`,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
const agy = context.env.PBOT_AGY_COMMAND || await context.findExecutable("agy");
|
|
48
|
-
if (!agy) {
|
|
49
|
-
return {
|
|
50
|
-
host: this.host,
|
|
51
|
-
state: "skipped",
|
|
52
|
-
...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
|
|
53
|
-
detail: "未找到 Antigravity CLI(agy),已跳过。",
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
await context.run(agy, ["plugin", "install", pluginDir], context.workspace);
|
|
57
|
-
return {
|
|
58
|
-
host: this.host,
|
|
59
|
-
state: "updated",
|
|
60
|
-
...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
|
|
61
|
-
version: bundledVersion,
|
|
62
|
-
scope: "user",
|
|
63
|
-
detail: `已通过 agy plugin install 更新到 ${bundledVersion}。`,
|
|
64
|
-
nextStep: "重启 Antigravity 会话使新版本生效。",
|
|
65
|
-
};
|
|
66
|
-
},
|
|
67
|
-
};
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { realpath } from "node:fs/promises";
|
|
2
|
-
import { gte, valid } from "semver";
|
|
3
|
-
import { object } from "../../../common/json.js";
|
|
4
|
-
function parsePluginList(raw) {
|
|
5
|
-
const parsed = JSON.parse(raw);
|
|
6
|
-
const items = Array.isArray(parsed)
|
|
7
|
-
? parsed
|
|
8
|
-
: object(parsed) && Array.isArray(parsed.plugins)
|
|
9
|
-
? parsed.plugins
|
|
10
|
-
: [];
|
|
11
|
-
const entries = [];
|
|
12
|
-
for (const item of items) {
|
|
13
|
-
if (!object(item))
|
|
14
|
-
continue;
|
|
15
|
-
const id = typeof item.id === "string" ? item.id : typeof item.name === "string" ? item.name : undefined;
|
|
16
|
-
if (!id)
|
|
17
|
-
continue;
|
|
18
|
-
entries.push({
|
|
19
|
-
id,
|
|
20
|
-
...(typeof item.version === "string" ? { version: item.version } : {}),
|
|
21
|
-
...(typeof item.scope === "string" ? { scope: item.scope } : {}),
|
|
22
|
-
...(typeof item.projectPath === "string" ? { projectPath: item.projectPath } : {}),
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
return entries;
|
|
26
|
-
}
|
|
27
|
-
async function resolvePathSafe(value) {
|
|
28
|
-
try {
|
|
29
|
-
return await realpath(value);
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
return value;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
async function selectMatchingEntry(entries, workspace) {
|
|
36
|
-
const canonicalWorkspace = await resolvePathSafe(workspace);
|
|
37
|
-
const candidates = entries.filter(entry => entry.id === "personal-bot" || entry.id.startsWith("personal-bot@"));
|
|
38
|
-
if (!candidates.length)
|
|
39
|
-
return undefined;
|
|
40
|
-
const scopedMatches = [];
|
|
41
|
-
for (const entry of candidates) {
|
|
42
|
-
if (!entry.projectPath) {
|
|
43
|
-
scopedMatches.push(entry);
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
if (await resolvePathSafe(entry.projectPath) === canonicalWorkspace)
|
|
47
|
-
scopedMatches.push(entry);
|
|
48
|
-
}
|
|
49
|
-
const priority = new Map([["local", 0], ["project", 1], ["user", 2]]);
|
|
50
|
-
scopedMatches.sort((a, b) => (priority.get(a.scope ?? "") ?? 9) - (priority.get(b.scope ?? "") ?? 9));
|
|
51
|
-
return scopedMatches[0];
|
|
52
|
-
}
|
|
53
|
-
export const claudeUpgrader = {
|
|
54
|
-
host: "claude",
|
|
55
|
-
async upgrade(context) {
|
|
56
|
-
const claude = context.env.PBOT_CLAUDE_COMMAND || await context.findExecutable("claude");
|
|
57
|
-
if (!claude) {
|
|
58
|
-
return {
|
|
59
|
-
host: this.host,
|
|
60
|
-
state: "skipped",
|
|
61
|
-
...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
|
|
62
|
-
detail: "未找到 Claude Code CLI(claude),已跳过。",
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
let entries;
|
|
66
|
-
try {
|
|
67
|
-
const raw = await context.run(claude, ["plugin", "list", "--json"], context.workspace);
|
|
68
|
-
entries = parsePluginList(raw);
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
return {
|
|
72
|
-
host: this.host,
|
|
73
|
-
state: "failed",
|
|
74
|
-
...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
|
|
75
|
-
detail: `查询 Claude 插件列表失败:${error instanceof Error ? error.message : String(error)}`,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
const entry = await selectMatchingEntry(entries, context.workspace);
|
|
79
|
-
if (!entry) {
|
|
80
|
-
return {
|
|
81
|
-
host: this.host,
|
|
82
|
-
state: "skipped",
|
|
83
|
-
...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
|
|
84
|
-
detail: "未在 Claude 插件列表中找到 personal-bot(可能通过 --plugin-dir 本地加载),已跳过。",
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
const installedVersion = entry.version ?? context.currentVersion;
|
|
88
|
-
const scope = entry.scope ?? "user";
|
|
89
|
-
if (installedVersion && valid(installedVersion) && gte(installedVersion, context.targetVersion)) {
|
|
90
|
-
return {
|
|
91
|
-
host: this.host,
|
|
92
|
-
state: "up-to-date",
|
|
93
|
-
previousVersion: installedVersion,
|
|
94
|
-
version: installedVersion,
|
|
95
|
-
scope,
|
|
96
|
-
detail: "已是最新版本。",
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
const atIndex = entry.id.indexOf("@");
|
|
100
|
-
const marketplace = atIndex > 0 ? entry.id.slice(atIndex + 1) : undefined;
|
|
101
|
-
if (marketplace) {
|
|
102
|
-
try {
|
|
103
|
-
await context.run(claude, ["plugin", "marketplace", "update", marketplace], context.workspace);
|
|
104
|
-
}
|
|
105
|
-
catch {
|
|
106
|
-
// Continue to plugin update even if marketplace refresh is unsupported or cached.
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const updateArgs = ["plugin", "update", entry.id, "--yes"];
|
|
110
|
-
if (entry.scope)
|
|
111
|
-
updateArgs.push("--scope", entry.scope);
|
|
112
|
-
await context.run(claude, updateArgs, context.workspace);
|
|
113
|
-
return {
|
|
114
|
-
host: this.host,
|
|
115
|
-
state: "updated",
|
|
116
|
-
...(installedVersion ? { previousVersion: installedVersion } : {}),
|
|
117
|
-
version: context.targetVersion,
|
|
118
|
-
scope,
|
|
119
|
-
detail: `已按原有作用域(${scope})更新 ${entry.id}。`,
|
|
120
|
-
nextStep: "重启 Claude Code 会话使新版本生效。",
|
|
121
|
-
};
|
|
122
|
-
},
|
|
123
|
-
};
|