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.
Files changed (62) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/bin/pbot.mjs +3 -17
  4. package/docs/architecture/design.md +8 -6
  5. package/docs/architecture/extensibility.md +1 -1
  6. package/docs/architecture/principles.md +1 -1
  7. package/lib/antigravity-plugin/lib/common/paths.d.ts +1 -0
  8. package/lib/antigravity-plugin/lib/common/paths.js +2 -0
  9. package/lib/antigravity-plugin/lib/extension/acp-worker.js +26 -24
  10. package/lib/antigravity-plugin/lib/extension/cli.js +22596 -0
  11. package/lib/antigravity-plugin/lib/extension/mcp.js +22 -20
  12. package/lib/antigravity-plugin/lib/extension/workspace-cli.js +5 -20165
  13. package/lib/antigravity-plugin/package.json +1 -1
  14. package/lib/antigravity-plugin/plugin.json +1 -1
  15. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
  16. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
  17. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
  18. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +1 -1
  19. package/lib/common/paths.d.ts +1 -0
  20. package/lib/common/paths.js +2 -0
  21. package/lib/extension/acp-worker.js +26 -24
  22. package/lib/extension/cli.js +22596 -0
  23. package/lib/extension/mcp.js +22 -20
  24. package/lib/extension/workspace-cli.js +5 -20165
  25. package/lib/qoder-plugin/.mcp.json +2 -2
  26. package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
  27. package/lib/qoder-plugin/hooks/hooks.json +3 -3
  28. package/lib/qoder-plugin/skills/pbot/SKILL.md +1 -1
  29. package/package.json +1 -1
  30. package/scripts/README.md +1 -1
  31. package/src/common/paths.ts +2 -0
  32. package/src/extension/cli/args.ts +104 -0
  33. package/src/extension/cli/commands/history.ts +81 -0
  34. package/src/extension/cli/commands/mcp.ts +165 -0
  35. package/src/extension/cli/commands/plugin.ts +33 -0
  36. package/src/extension/cli/commands/setup.ts +159 -0
  37. package/src/extension/cli/commands/upgrade.ts +158 -0
  38. package/src/extension/cli/commands/workspace.ts +71 -0
  39. package/src/extension/cli/index.ts +193 -0
  40. package/src/extension/cli/types.ts +30 -0
  41. package/src/integrations/installation/cli-update.ts +3 -3
  42. package/src/integrations/installation/pi-setup.ts +6 -3
  43. package/src/services/installation/setup-service.ts +0 -35
  44. package/lib/extension/setup/cli.js +0 -364
  45. package/lib/extension/setup/upgrade.js +0 -153
  46. package/lib/integrations/installation/cli-update.js +0 -146
  47. package/lib/integrations/installation/pi-setup.js +0 -193
  48. package/lib/integrations/installation/processes.js +0 -140
  49. package/lib/integrations/installation/upgraders/antigravity.js +0 -67
  50. package/lib/integrations/installation/upgraders/claude.js +0 -123
  51. package/lib/integrations/installation/upgraders/codex.js +0 -73
  52. package/lib/integrations/installation/upgraders/dsh.js +0 -62
  53. package/lib/integrations/installation/upgraders/pi.js +0 -90
  54. package/lib/integrations/installation/upgraders/qoder.js +0 -67
  55. package/lib/integrations/installation/upgraders/registry.js +0 -40
  56. package/lib/integrations/installation/workspace.js +0 -21
  57. package/lib/services/installation/setup-service.js +0 -140
  58. package/lib/services/installation/upgrade-service.js +0 -95
  59. package/lib/ui/setup.js +0 -52
  60. package/src/extension/setup/cli.ts +0 -303
  61. package/src/extension/setup/upgrade.ts +0 -158
  62. package/src/extension/setup/workspace-cli.ts +0 -167
@@ -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
- };
@@ -1,73 +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 codexUpgrader = {
17
- host: "codex",
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 marketplaceDir = join(context.packageRoot, "lib", "codex-marketplace");
30
- const bundledVersion = await readManifestVersion(join(marketplaceDir, "plugins", "personal-bot", ".codex-plugin", "plugin.json"));
31
- if (!bundledVersion) {
32
- return {
33
- host: this.host,
34
- state: "skipped",
35
- ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
36
- detail: `缺少 Codex 插件构建产物:${marketplaceDir}`,
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 codex = context.env.PBOT_CODEX_COMMAND || await context.findExecutable("codex");
48
- if (!codex) {
49
- return {
50
- host: this.host,
51
- state: "skipped",
52
- ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
53
- detail: "未找到 Codex CLI(codex),已跳过。",
54
- };
55
- }
56
- try {
57
- await context.run(codex, ["plugin", "marketplace", "add", marketplaceDir], context.workspace);
58
- }
59
- catch {
60
- // Marketplace may already be registered; proceed to re-install the plugin from it.
61
- }
62
- await context.run(codex, ["plugin", "add", "personal-bot@pbot-local"], context.workspace);
63
- return {
64
- host: this.host,
65
- state: "updated",
66
- ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
67
- version: bundledVersion,
68
- scope: "user",
69
- detail: `已通过 codex plugin add 更新到 ${bundledVersion}。`,
70
- nextStep: "重启 Codex 会话;若 hooks 有变化,请在 Codex /hooks 中重新核对信任。",
71
- };
72
- },
73
- };
@@ -1,62 +0,0 @@
1
- import { gte, valid } from "semver";
2
- export function inferDshProfile(bindingPath) {
3
- if (!bindingPath)
4
- return undefined;
5
- const normalized = bindingPath.replaceAll("\\", "/");
6
- const match = /\/profiles\/([^/]+)\/node_modules\/personal-bot\/package\.json$/.exec(normalized);
7
- return match?.[1];
8
- }
9
- export const dshUpgrader = {
10
- host: "dsh",
11
- async upgrade(context) {
12
- if (context.currentVersion && valid(context.currentVersion) && gte(context.currentVersion, context.targetVersion)) {
13
- return {
14
- host: this.host,
15
- state: "up-to-date",
16
- previousVersion: context.currentVersion,
17
- version: context.currentVersion,
18
- detail: "已是最新版本。",
19
- };
20
- }
21
- const profile = context.options.dshProfile || inferDshProfile(context.bindingPath);
22
- if (!profile) {
23
- return {
24
- host: this.host,
25
- state: "skipped",
26
- ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
27
- detail: "无法自动识别 dsh profile;请传入 --dsh-profile <名称> 或手动执行:dsh plugin --profile <名称> update personal-bot --latest",
28
- };
29
- }
30
- const dsh = context.env.PBOT_DSH_COMMAND || await context.findExecutable("dsh");
31
- if (!dsh) {
32
- return {
33
- host: this.host,
34
- state: "skipped",
35
- ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
36
- scope: `profile:${profile}`,
37
- detail: "未找到 dsh CLI(dsh),已跳过。",
38
- };
39
- }
40
- try {
41
- await context.run(dsh, ["plugin", "--profile", profile, "update", "personal-bot", "--latest"], context.workspace);
42
- }
43
- catch (error) {
44
- return {
45
- host: this.host,
46
- state: "failed",
47
- ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
48
- scope: `profile:${profile}`,
49
- detail: `${error instanceof Error ? error.message : String(error)}(提示:更新 dsh 插件前需先停止对应 profile 的 dsh 服务)`,
50
- };
51
- }
52
- return {
53
- host: this.host,
54
- state: "updated",
55
- ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
56
- version: context.targetVersion,
57
- scope: `profile:${profile}`,
58
- detail: `已更新 dsh profile "${profile}" 中的 personal-bot。`,
59
- nextStep: `重新启动 dsh --profile ${profile} 使新版本生效。`,
60
- };
61
- },
62
- };