personal-bot 0.3.5 → 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/docs/README.md +1 -1
- package/docs/architecture/design.md +3 -4
- 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/extension/cli.js +1392 -1493
- 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/extension/cli.js +1392 -1493
- 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/src/extension/cli/commands/install.ts +77 -0
- package/src/extension/cli/index.ts +3 -4
- 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/guard.js +0 -11
- package/lib/integrations/installation/pi-bridge.js +0 -131
- package/lib/model/installation.js +0 -1
- package/resources/installer/plugins.json +0 -16
- package/src/extension/cli/commands/setup.ts +0 -159
- package/src/extension/setup/guard.ts +0 -12
- package/src/integrations/installation/pi-bridge.ts +0 -109
- package/src/integrations/installation/pi-setup.ts +0 -165
- package/src/services/installation/setup-service.ts +0 -91
- package/src/ui/setup.ts +0 -40
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { requireThat } from "../../common/validation.js";
|
|
2
|
+
import type {
|
|
3
|
+
HostInstallContext,
|
|
4
|
+
HostInstaller,
|
|
5
|
+
HostInstallResult,
|
|
6
|
+
WorkspaceInstallSummary,
|
|
7
|
+
} from "../../model/installation.js";
|
|
8
|
+
|
|
9
|
+
export interface InstallArgs {
|
|
10
|
+
help: boolean;
|
|
11
|
+
target?: string;
|
|
12
|
+
workspace?: string;
|
|
13
|
+
dshProfile?: string;
|
|
14
|
+
json: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function formatInstallUsage(hosts: readonly string[]): string {
|
|
18
|
+
return (
|
|
19
|
+
`用法:pbot install <${hosts.join("|")}|all> [--workspace <目录>] [--dsh-profile <名称>] [--json]\n` +
|
|
20
|
+
"为指定宿主安装 Personal Bot 插件;传入 all 时为本机已安装的各宿主统一安装。"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function parseInstallArgs(args: readonly string[], hosts: readonly string[]): InstallArgs {
|
|
25
|
+
const usage = formatInstallUsage(hosts);
|
|
26
|
+
let target: string | undefined;
|
|
27
|
+
let workspace: string | undefined;
|
|
28
|
+
let dshProfile: string | undefined;
|
|
29
|
+
let json = false;
|
|
30
|
+
for (let index = 0; index < args.length; index++) {
|
|
31
|
+
const token = args[index]!;
|
|
32
|
+
if (token === "--help" || token === "-h") return { help: true, json: false };
|
|
33
|
+
if (token === "--json") { json = true; continue; }
|
|
34
|
+
if (token === "--workspace" || token.startsWith("--workspace=")) {
|
|
35
|
+
requireThat(workspace === undefined, "--workspace 只能指定一次。");
|
|
36
|
+
workspace = token === "--workspace" ? args[++index] : token.slice("--workspace=".length);
|
|
37
|
+
requireThat(workspace && !workspace.startsWith("-"), "--workspace 需要目录。");
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (token === "--dsh-profile" || token.startsWith("--dsh-profile=")) {
|
|
41
|
+
requireThat(dshProfile === undefined, "--dsh-profile 只能指定一次。");
|
|
42
|
+
dshProfile = token === "--dsh-profile" ? args[++index] : token.slice("--dsh-profile=".length);
|
|
43
|
+
requireThat(dshProfile && !dshProfile.startsWith("-"), "--dsh-profile 需要 profile 名称。");
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (token.startsWith("-")) {
|
|
47
|
+
throw new Error(`不支持的参数:${token}\n${usage}`);
|
|
48
|
+
}
|
|
49
|
+
requireThat(target === undefined, `只能指定一个安装目标,收到多余参数:${token}\n${usage}`);
|
|
50
|
+
target = token.trim().toLowerCase();
|
|
51
|
+
}
|
|
52
|
+
requireThat(target && target.length > 0, `缺少安装目标。\n${usage}`);
|
|
53
|
+
requireThat(target === "all" || hosts.includes(target), `未知宿主:${target}。\n${usage}`);
|
|
54
|
+
return {
|
|
55
|
+
help: false,
|
|
56
|
+
target,
|
|
57
|
+
...(workspace !== undefined ? { workspace } : {}),
|
|
58
|
+
...(dshProfile !== undefined ? { dshProfile } : {}),
|
|
59
|
+
json,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function executeHostInstall(input: {
|
|
64
|
+
workspaceRoot: string;
|
|
65
|
+
target: string;
|
|
66
|
+
hosts: readonly string[];
|
|
67
|
+
resolveInstaller: (host: string) => HostInstaller | undefined;
|
|
68
|
+
createContext: (host: string, explicit: boolean) => HostInstallContext;
|
|
69
|
+
onResult?: (result: HostInstallResult) => void;
|
|
70
|
+
signal: AbortSignal;
|
|
71
|
+
}): Promise<WorkspaceInstallSummary> {
|
|
72
|
+
const explicit = input.target !== "all";
|
|
73
|
+
const targets = explicit ? [input.target] : [...input.hosts];
|
|
74
|
+
const results: HostInstallResult[] = [];
|
|
75
|
+
for (const host of targets) {
|
|
76
|
+
input.signal.throwIfAborted();
|
|
77
|
+
const installer = input.resolveInstaller(host);
|
|
78
|
+
let result: HostInstallResult;
|
|
79
|
+
if (!installer) {
|
|
80
|
+
result = {
|
|
81
|
+
host,
|
|
82
|
+
state: explicit ? "failed" : "skipped",
|
|
83
|
+
detail: "未注册该宿主的安装适配器。",
|
|
84
|
+
};
|
|
85
|
+
} else {
|
|
86
|
+
try {
|
|
87
|
+
result = await installer.install(input.createContext(host, explicit));
|
|
88
|
+
} catch (error) {
|
|
89
|
+
input.signal.throwIfAborted();
|
|
90
|
+
result = {
|
|
91
|
+
host,
|
|
92
|
+
state: "failed",
|
|
93
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
results.push(result);
|
|
98
|
+
input.onResult?.(result);
|
|
99
|
+
}
|
|
100
|
+
const ok = !results.some(item => item.state === "failed");
|
|
101
|
+
return {
|
|
102
|
+
workspaceRoot: input.workspaceRoot,
|
|
103
|
+
target: input.target,
|
|
104
|
+
results,
|
|
105
|
+
ok,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { prerelease, satisfies, valid } from "semver";
|
|
2
|
-
export function npmName(source) {
|
|
3
|
-
return /^npm:((?:@[a-z0-9._-]+\/)?[a-z0-9._-]+)(?:@[^\s]+)?$/.exec(source)?.[1];
|
|
4
|
-
}
|
|
5
|
-
export function ordinaryNpm(source) {
|
|
6
|
-
const name = npmName(source);
|
|
7
|
-
return name !== undefined && (source === `npm:${name}` || source === `npm:${name}@latest`);
|
|
8
|
-
}
|
|
9
|
-
export function checkStableVersion(version, nodeRange) {
|
|
10
|
-
if (!valid(version) || prerelease(version))
|
|
11
|
-
throw new Error(`latest 不是稳定版本:${version}`);
|
|
12
|
-
if (nodeRange && !satisfies(process.versions.node, nodeRange)) {
|
|
13
|
-
throw new Error(`目标版本要求 Node.js ${nodeRange},当前为 ${process.versions.node}。请先升级 Node.js。`);
|
|
14
|
-
}
|
|
15
|
-
return version;
|
|
16
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// Native extension commands run before input handlers. Reaching this handler for
|
|
2
|
-
// /pbot init means it was not dispatched; never let the setup input reach a model.
|
|
3
|
-
export default function setupGuard(pi) {
|
|
4
|
-
pi.on("input", (event, ctx) => {
|
|
5
|
-
if (event.text.trim() !== "/pbot init")
|
|
6
|
-
return { action: "continue" };
|
|
7
|
-
ctx.ui.notify("pbot 未加载或命令存在冲突。请检查项目信任和 pi config --local 后重新运行 setup。", "error");
|
|
8
|
-
ctx.shutdown();
|
|
9
|
-
return { action: "handled" };
|
|
10
|
-
});
|
|
11
|
-
}
|
|
@@ -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 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"id": "task-conductor",
|
|
4
|
-
"label": "Task Conductor",
|
|
5
|
-
"description": "周期任务、流程和 DAG 的接入与使用引导;服务另行准备",
|
|
6
|
-
"source": "npm:task-conductor-plugin",
|
|
7
|
-
"defaultSelected": true
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
"id": "web-access",
|
|
11
|
-
"label": "Pi Web Access",
|
|
12
|
-
"description": "网页搜索与内容获取",
|
|
13
|
-
"source": "npm:pi-web-access",
|
|
14
|
-
"defaultSelected": true
|
|
15
|
-
}
|
|
16
|
-
]
|
|
@@ -1,159 +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 { join } from "node:path";
|
|
5
|
-
import { npmName, ordinaryNpm } from "../../../common/package-source.js";
|
|
6
|
-
import { packageRoot } from "../../../common/paths.js";
|
|
7
|
-
import { packageVersion } from "../../../common/version.js";
|
|
8
|
-
import { PiBootstrapRequired, PiSetup, piPackageName } from "../../../integrations/installation/pi-setup.js";
|
|
9
|
-
import { findExecutable, inspectSystem, SetupProcesses } from "../../../integrations/installation/processes.js";
|
|
10
|
-
import type { InstalledPackage, SetupPlan } from "../../../model/installation.js";
|
|
11
|
-
import { executeSetup, packageChoices, parseCatalog } from "../../../services/installation/setup-service.js";
|
|
12
|
-
import { SetupUI } from "../../../ui/setup.js";
|
|
13
|
-
import { CliError, requireNoExtraArgs, resolveCliWorkspace, withSignals } from "../args.js";
|
|
14
|
-
import type { CliCommand } from "../types.js";
|
|
15
|
-
|
|
16
|
-
const quote = (value: string) => "'" + value.replaceAll("'", "'\\''") + "'";
|
|
17
|
-
|
|
18
|
-
const setupUsage =
|
|
19
|
-
"用法:pbot setup [--workspace <目录>]\n" +
|
|
20
|
-
"安装或升级 Pi、Personal Bot 和选中的配套插件,然后启动 Pi 执行 /pbot init。";
|
|
21
|
-
|
|
22
|
-
const installAntigravityUsage =
|
|
23
|
-
"用法:pbot install-antigravity\n" +
|
|
24
|
-
"通过原生 agy plugin install 安装或更新 Antigravity 插件。";
|
|
25
|
-
|
|
26
|
-
export const setupCommand: CliCommand = {
|
|
27
|
-
name: "setup",
|
|
28
|
-
summary: "交互式安装或升级 Pi 与工作区插件",
|
|
29
|
-
usage: setupUsage,
|
|
30
|
-
updatePolicy: "none",
|
|
31
|
-
supportsWorkspaceFlag: true,
|
|
32
|
-
async run(ctx) {
|
|
33
|
-
requireNoExtraArgs(ctx.args, setupUsage);
|
|
34
|
-
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
35
|
-
throw new CliError("setup 需要交互终端。请在终端运行 npx personal-bot@latest setup。", 2);
|
|
36
|
-
}
|
|
37
|
-
const outcome = await withSignals(
|
|
38
|
-
async controller => {
|
|
39
|
-
const ui = new SetupUI(controller);
|
|
40
|
-
const processes = new SetupProcesses(controller.signal);
|
|
41
|
-
const cwd = ctx.cwd;
|
|
42
|
-
ui.text("Personal Bot " + packageVersion + " — 安装与升级\n正在检查 Node/npm 和 tmux…");
|
|
43
|
-
const system = await inspectSystem(processes, cwd);
|
|
44
|
-
const supplied = ctx.workspace ?? await ui.ask("工作区目录(当前、已有或新建目录)", cwd);
|
|
45
|
-
const workspace = await resolveCliWorkspace(supplied, cwd);
|
|
46
|
-
const host = new PiSetup(processes, system.npm, cwd);
|
|
47
|
-
let pi = await host.inspectPi();
|
|
48
|
-
let installed: InstalledPackage[];
|
|
49
|
-
try {
|
|
50
|
-
installed = await host.inspectPackages(pi, workspace);
|
|
51
|
-
} catch (error) {
|
|
52
|
-
if (!(error instanceof PiBootstrapRequired)) throw error;
|
|
53
|
-
const version = await host.latest(piPackageName);
|
|
54
|
-
ui.text(error.message + "\n需先安装 Pi 才能使用其原生接口核对这些来源。目标版本:" + version);
|
|
55
|
-
if (!await ui.confirm("先通过 npm 安装 Pi,再继续选择工作区插件")) return 130;
|
|
56
|
-
pi = await host.ensurePi(undefined, version);
|
|
57
|
-
installed = await host.inspectPackages(pi, workspace);
|
|
58
|
-
}
|
|
59
|
-
const catalog = parseCatalog(JSON.parse(await readFile(join(packageRoot, "resources/installer/plugins.json"), "utf8")));
|
|
60
|
-
const choices = packageChoices(catalog, installed);
|
|
61
|
-
const optional = choices.filter(choice => !choice.required);
|
|
62
|
-
ui.text("\nPersonal Bot 必装。选择配套插件(取消勾选不会卸载已有插件):");
|
|
63
|
-
const selected = await ui.selectMany(
|
|
64
|
-
optional.map(choice => choice.plugin.label + " — " + choice.plugin.description),
|
|
65
|
-
optional.flatMap((choice, index) => choice.selected ? [index] : []),
|
|
66
|
-
);
|
|
67
|
-
optional.forEach((choice, index) => { choice.selected = selected.includes(index); });
|
|
68
|
-
for (const choice of choices.filter(item => item.selected)) {
|
|
69
|
-
if (choice.existing.some(pkg => !ordinaryNpm(pkg.source))) {
|
|
70
|
-
ui.text("\n" + choice.plugin.label + " 的已有来源:\n" + choice.existing.map(pkg =>
|
|
71
|
-
" " + pkg.scope + ": " + pkg.source + " (" + (pkg.version ?? "版本未知") + ")").join("\n"));
|
|
72
|
-
choice.keep = !await ui.confirm("切换到 npm 最新稳定版", false);
|
|
73
|
-
}
|
|
74
|
-
if (choice.keep) {
|
|
75
|
-
if (choice.existing.length !== 1 || !choice.existing[0]?.installedPath) {
|
|
76
|
-
choice.error = "保留来源未安装或存在多份来源,请先通过 Pi 原生配置处理。";
|
|
77
|
-
}
|
|
78
|
-
} else {
|
|
79
|
-
const others = choice.existing.filter(pkg => !npmName(pkg.source));
|
|
80
|
-
if (others.length > 1 || others.some(pkg => pkg.scope === "user")) {
|
|
81
|
-
choice.error = "请先用 pi config 调整全局或重复来源的加载范围,再切换到 npm。";
|
|
82
|
-
} else {
|
|
83
|
-
try {
|
|
84
|
-
choice.target = await host.latest(npmName(choice.plugin.source)!);
|
|
85
|
-
} catch (error) {
|
|
86
|
-
controller.signal.throwIfAborted();
|
|
87
|
-
choice.error = String(error);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
const plan: SetupPlan = {
|
|
93
|
-
workspace,
|
|
94
|
-
piVersion: await host.latest(piPackageName),
|
|
95
|
-
packages: choices,
|
|
96
|
-
...(pi ? { pi } : {}),
|
|
97
|
-
...(system.tmux ? { tmux: system.tmux } : {}),
|
|
98
|
-
};
|
|
99
|
-
ui.text("\n安装计划\n工作区:" + workspace + "\nPi:" + (pi ? pi.command + " (" + pi.version + ")" : "通过 npm 全局安装") + " → " + plan.piVersion);
|
|
100
|
-
if (system.tmux) ui.text("tmux:" + [system.tmux.command, ...system.tmux.args].map(quote).join(" "));
|
|
101
|
-
for (const choice of choices) {
|
|
102
|
-
ui.text(" " + choice.plugin.label + ": " + (!choice.selected ? "本次跳过" : choice.error ??
|
|
103
|
-
(choice.keep ? "保留 " + choice.existing[0]?.source : (choice.existing.map(pkg => pkg.version ?? "未知").join(", ") || "未安装") + " → " + choice.target)));
|
|
104
|
-
if (choice.selected && choice.existing.some(pkg => pkg.filtered)) {
|
|
105
|
-
ui.text(" 保留已有资源过滤;如 pbot 被禁用,请通过 pi config --local 启用。");
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
const blocking = choices.find(choice => choice.required && choice.error);
|
|
109
|
-
if (blocking) throw new Error(blocking.error);
|
|
110
|
-
ui.text("升级前请停止活动 pbot 子会话。Pi 为当前用户共用,插件只处理此工作区。");
|
|
111
|
-
if (!await ui.confirm("执行以上计划")) return 130;
|
|
112
|
-
await mkdir(workspace, { recursive: true });
|
|
113
|
-
if (await realpath(workspace) !== workspace) throw new Error("工作区路径已发生变化,请重新运行 setup。");
|
|
114
|
-
const result = await executeSetup(plan, host, item => ui.text("[" + item.state + "] " + item.name + ": " + item.detail), controller.signal);
|
|
115
|
-
const failed = result.results.some(item => item.state === "failed");
|
|
116
|
-
if (!result.pi) return 1;
|
|
117
|
-
ui.text("\n软件安装阶段结束。工作区初始化和外部服务的状态由 Pi/pbot/插件分别报告。");
|
|
118
|
-
if (await ui.confirm(failed ? "部分插件失败,是否仍启动 Pi 并初始化启用工作区" : "启动 Pi 并初始化启用工作区", !failed)) {
|
|
119
|
-
process.stdin.pause();
|
|
120
|
-
await host.start(result.pi, workspace);
|
|
121
|
-
} else {
|
|
122
|
-
ui.text("稍后执行:\ncd " + quote(workspace) + "\n" + quote(result.pi.command) + " --extension " +
|
|
123
|
-
quote(join(packageRoot, "lib/extension/setup/guard.js")) + " '/pbot init'");
|
|
124
|
-
}
|
|
125
|
-
return failed ? 1 : 0;
|
|
126
|
-
},
|
|
127
|
-
() => console.error("安装已取消;已完成的步骤保留。"),
|
|
128
|
-
);
|
|
129
|
-
return outcome.ok ? outcome.value : outcome.exitCode;
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
export const installAntigravityCommand: CliCommand = {
|
|
134
|
-
name: "install-antigravity",
|
|
135
|
-
summary: "安装或更新 Antigravity 插件",
|
|
136
|
-
usage: installAntigravityUsage,
|
|
137
|
-
updatePolicy: "none",
|
|
138
|
-
async run(ctx) {
|
|
139
|
-
requireNoExtraArgs(ctx.args, installAntigravityUsage);
|
|
140
|
-
const pluginDir = join(packageRoot, "lib/antigravity-plugin");
|
|
141
|
-
if (!existsSync(join(pluginDir, "plugin.json"))) {
|
|
142
|
-
console.error("缺少 Antigravity 插件构建产物:" + pluginDir + "。请先运行 npm run build。");
|
|
143
|
-
return 1;
|
|
144
|
-
}
|
|
145
|
-
const agy = process.env.PBOT_AGY_COMMAND || await findExecutable("agy", process.env);
|
|
146
|
-
if (!agy) {
|
|
147
|
-
console.error("未找到 Antigravity CLI(agy)。请先安装 agy,然后运行:\n agy plugin install " + quote(pluginDir));
|
|
148
|
-
return 1;
|
|
149
|
-
}
|
|
150
|
-
const installed = spawnSync(agy, ["plugin", "install", pluginDir], { stdio: "inherit" });
|
|
151
|
-
if (installed.error) {
|
|
152
|
-
console.error(String(installed.error));
|
|
153
|
-
return 1;
|
|
154
|
-
}
|
|
155
|
-
if (installed.status !== 0) return installed.status ?? 1;
|
|
156
|
-
console.log("Antigravity 插件已安装。在目标工作区运行 /pbot init 启用 Personal Bot。");
|
|
157
|
-
return 0;
|
|
158
|
-
},
|
|
159
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
|
|
3
|
-
// Native extension commands run before input handlers. Reaching this handler for
|
|
4
|
-
// /pbot init means it was not dispatched; never let the setup input reach a model.
|
|
5
|
-
export default function setupGuard(pi: ExtensionAPI) {
|
|
6
|
-
pi.on("input", (event, ctx) => {
|
|
7
|
-
if (event.text.trim() !== "/pbot init") return { action: "continue" };
|
|
8
|
-
ctx.ui.notify("pbot 未加载或命令存在冲突。请检查项目信任和 pi config --local 后重新运行 setup。", "error");
|
|
9
|
-
ctx.shutdown();
|
|
10
|
-
return { action: "handled" };
|
|
11
|
-
});
|
|
12
|
-
}
|
|
@@ -1,109 +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 type { InstalledPackage } from "../../model/installation.js";
|
|
6
|
-
import { npmName } from "../../common/package-source.js";
|
|
7
|
-
|
|
8
|
-
type PiSdk = typeof import("@earendil-works/pi-coding-agent");
|
|
9
|
-
async function sdkAt(root: string): Promise<PiSdk> {
|
|
10
|
-
const manifest = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
|
|
11
|
-
if (manifest.name !== "@earendil-works/pi-coding-agent") throw new Error("不是支持的 Pi 安装。");
|
|
12
|
-
return import(pathToFileURL(join(root, "dist/index.js")).href) as Promise<PiSdk>;
|
|
13
|
-
}
|
|
14
|
-
const sourceOf = (entry: unknown): string => {
|
|
15
|
-
if (typeof entry === "string") return entry;
|
|
16
|
-
if (entry && typeof entry === "object" && "source" in entry && typeof entry.source === "string") return entry.source;
|
|
17
|
-
throw new Error("无效的 Pi packages 声明。");
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export async function inspectPackages(root: string, workspace: string): Promise<InstalledPackage[]> {
|
|
21
|
-
const sdk = await sdkAt(root), agentDir = sdk.getAgentDir();
|
|
22
|
-
// Inspect project declarations as data only. This does not load extensions, execute
|
|
23
|
-
// project npmCommand, grant project trust, or persist migrated settings.
|
|
24
|
-
const settings = sdk.SettingsManager.fromStorage({ withLock(scope, read) {
|
|
25
|
-
const file = scope === "global" ? join(agentDir, "settings.json") : join(workspace, sdk.CONFIG_DIR_NAME, "settings.json");
|
|
26
|
-
let content: string | undefined;
|
|
27
|
-
try { content = readFileSync(file, "utf8"); }
|
|
28
|
-
catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
|
|
29
|
-
if (content !== undefined) {
|
|
30
|
-
const value: unknown = JSON.parse(content);
|
|
31
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error(`无效的 Pi 配置:${file}`);
|
|
32
|
-
const packages = (value as { packages?: unknown }).packages;
|
|
33
|
-
if (packages !== undefined) {
|
|
34
|
-
if (!Array.isArray(packages)) throw new Error(`packages 必须是数组:${file}`);
|
|
35
|
-
packages.forEach(sourceOf);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (read(content) !== undefined) throw new Error("安装检查不得修改 Pi 配置。");
|
|
39
|
-
} }, { projectTrusted: true });
|
|
40
|
-
const errors = settings.drainErrors();
|
|
41
|
-
if (errors.length) throw new Error(errors.map(item => `${item.scope}: ${item.error.message}`).join("\n"));
|
|
42
|
-
const manager = new sdk.DefaultPackageManager({ cwd: workspace, agentDir, settingsManager: settings });
|
|
43
|
-
const result: InstalledPackage[] = [];
|
|
44
|
-
for (const pkg of manager.listConfiguredPackages()) {
|
|
45
|
-
const entries = (pkg.scope === "project" ? settings.getProjectSettings() : settings.getGlobalSettings()).packages ?? [];
|
|
46
|
-
const entry = entries.find(item => sourceOf(item) === pkg.source);
|
|
47
|
-
let name = npmName(pkg.source), version: string | undefined, singleFile = false;
|
|
48
|
-
if (pkg.installedPath) {
|
|
49
|
-
const info = await stat(pkg.installedPath);
|
|
50
|
-
singleFile = info.isFile(); // An existing standalone extension has no npm identity.
|
|
51
|
-
if (info.isDirectory()) {
|
|
52
|
-
try {
|
|
53
|
-
const manifest = JSON.parse(await readFile(join(pkg.installedPath, "package.json"), "utf8")) as { name?: string; version?: string };
|
|
54
|
-
if (name && manifest.name !== name) throw new Error(`安装包身份不匹配:${pkg.source}`);
|
|
55
|
-
name = typeof manifest.name === "string" && manifest.name.trim() ? manifest.name : name;
|
|
56
|
-
version = typeof manifest.version === "string" ? manifest.version : undefined;
|
|
57
|
-
} catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (!name && !singleFile) {
|
|
61
|
-
throw new Error(`无法辨认 ${pkg.scope} 来源的包身份:${pkg.source}。请先通过 Pi 原生配置恢复或修正该来源,再重新运行 setup。`);
|
|
62
|
-
}
|
|
63
|
-
result.push({ source: pkg.source, scope: pkg.scope, filtered: pkg.filtered, entry,
|
|
64
|
-
...(pkg.installedPath ? { installedPath: pkg.installedPath } : {}), ...(name ? { name } : {}), ...(version ? { version } : {}) });
|
|
65
|
-
}
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export async function replaceProjectSource(root: string, workspace: string, old: InstalledPackage, target: string): Promise<void> {
|
|
70
|
-
const fromProject = old.scope === "project" && !npmName(old.source);
|
|
71
|
-
const fromUser = old.scope === "user" && !!npmName(old.source);
|
|
72
|
-
if (!fromProject && !fromUser) throw new Error("此操作只替换工作区自定义来源,或继承全局 npm 资源过滤。");
|
|
73
|
-
const sdk = await sdkAt(root);
|
|
74
|
-
const settings = sdk.SettingsManager.create(workspace, sdk.getAgentDir(), { projectTrusted: true });
|
|
75
|
-
const errors = settings.drainErrors();
|
|
76
|
-
if (errors.length) throw new Error(errors.map(item => item.error.message).join("\n"));
|
|
77
|
-
const packages = settings.getProjectSettings().packages ?? [];
|
|
78
|
-
const originals = fromProject ? packages : settings.getGlobalSettings().packages ?? [];
|
|
79
|
-
const previous = originals.find(entry => sourceOf(entry) === old.source);
|
|
80
|
-
if (previous === undefined || JSON.stringify(previous) !== JSON.stringify(old.entry)) throw new Error("插件配置已发生变化,请重新运行 setup。");
|
|
81
|
-
if (!packages.some(entry => sourceOf(entry) === target)) throw new Error("目标包尚未安装,保留原声明。");
|
|
82
|
-
const manager = new sdk.DefaultPackageManager({ cwd: workspace, agentDir: sdk.getAgentDir(), settingsManager: settings });
|
|
83
|
-
const installed = manager.getInstalledPath(target, "project");
|
|
84
|
-
if (!installed || !npmName(target)) throw new Error("目标 npm 包尚未安装,保留原声明。");
|
|
85
|
-
const manifest = JSON.parse(await readFile(join(installed, "package.json"), "utf8"));
|
|
86
|
-
if (manifest.name !== npmName(target)) throw new Error("目标安装包身份不符,保留原声明。");
|
|
87
|
-
// Change only declarations; do not call remove(), which deletes managed git caches.
|
|
88
|
-
const replacement = typeof previous === "string" ? target : { ...previous, source: target };
|
|
89
|
-
const next = fromProject
|
|
90
|
-
? packages.filter(entry => sourceOf(entry) !== target).map(entry => sourceOf(entry) === old.source ? replacement : entry)
|
|
91
|
-
: packages.map(entry => sourceOf(entry) === target ? replacement : entry);
|
|
92
|
-
settings.setProjectPackages(next);
|
|
93
|
-
await settings.flush();
|
|
94
|
-
const failures = settings.drainErrors();
|
|
95
|
-
if (failures.length) throw new Error(failures.map(item => item.error.message).join("\n"));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function main() {
|
|
99
|
-
const [action, root, workspace, input] = process.argv.slice(2);
|
|
100
|
-
if (!root || !workspace) throw new Error("缺少 Pi 路径或工作区。");
|
|
101
|
-
if (action === "inspect") console.log(JSON.stringify(await inspectPackages(root, workspace)));
|
|
102
|
-
else if (action === "replace" && input) {
|
|
103
|
-
const { old, target } = JSON.parse(input) as { old: InstalledPackage; target: string };
|
|
104
|
-
await replaceProjectSource(root, workspace, old, target);
|
|
105
|
-
} else throw new Error("无效的安装检查操作。");
|
|
106
|
-
}
|
|
107
|
-
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
108
|
-
await main().catch(error => { console.error(error instanceof Error ? error.message : String(error)); process.exitCode = 1; });
|
|
109
|
-
}
|