personal-bot 0.3.3 → 0.3.4
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 +2 -2
- package/README.zh-CN.md +2 -2
- package/bin/pbot.mjs +1 -1
- package/docs/architecture/extensibility.md +21 -0
- package/docs/guides/workspace-configuration.md +13 -0
- package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +9 -2
- package/lib/antigravity-plugin/lib/extension/workspace-cli.js +49 -5
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +12 -0
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +52 -4
- 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/hosts/plugin-runtime.js +9 -2
- package/lib/extension/setup/cli.js +12 -0
- package/lib/extension/setup/upgrade.js +153 -0
- package/lib/extension/workspace-cli.js +49 -5
- package/lib/integrations/installation/cli-update.js +146 -0
- package/lib/integrations/installation/upgraders/antigravity.js +67 -0
- package/lib/integrations/installation/upgraders/claude.js +123 -0
- package/lib/integrations/installation/upgraders/codex.js +73 -0
- package/lib/integrations/installation/upgraders/dsh.js +62 -0
- package/lib/integrations/installation/upgraders/pi.js +90 -0
- package/lib/integrations/installation/upgraders/qoder.js +67 -0
- package/lib/integrations/installation/upgraders/registry.js +40 -0
- 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/lib/services/installation/setup-service.js +4 -1
- package/lib/services/installation/upgrade-service.js +95 -0
- package/lib/services/workspace/plugin-releases.d.ts +12 -0
- package/lib/services/workspace/plugin-releases.js +52 -4
- package/package.json +1 -1
- package/src/extension/hosts/plugin-runtime.ts +9 -2
- package/src/extension/setup/cli.ts +7 -0
- package/src/extension/setup/upgrade.ts +158 -0
- package/src/extension/setup/workspace-cli.ts +10 -2
- package/src/integrations/installation/cli-update.ts +28 -0
- package/src/integrations/installation/upgraders/antigravity.ts +68 -0
- package/src/integrations/installation/upgraders/claude.ts +123 -0
- package/src/integrations/installation/upgraders/codex.ts +73 -0
- package/src/integrations/installation/upgraders/dsh.ts +63 -0
- package/src/integrations/installation/upgraders/pi.ts +87 -0
- package/src/integrations/installation/upgraders/qoder.ts +68 -0
- package/src/integrations/installation/upgraders/registry.ts +48 -0
- package/src/model/installation.ts +45 -0
- package/src/services/installation/setup-service.ts +7 -2
- package/src/services/installation/upgrade-service.ts +127 -0
- package/src/services/workspace/plugin-releases.ts +57 -4
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { valid } from "semver";
|
|
6
|
+
import { object } from "../../common/json.js";
|
|
7
|
+
import { checkStableVersion } from "../../common/package-source.js";
|
|
8
|
+
import { packageVersion } from "../../common/version.js";
|
|
9
|
+
import { execCliEntry, upgradeManagedGlobalCli } from "../../integrations/installation/cli-update.js";
|
|
10
|
+
import { findExecutable, SetupProcesses } from "../../integrations/installation/processes.js";
|
|
11
|
+
import { getHostUpgrader } from "../../integrations/installation/upgraders/registry.js";
|
|
12
|
+
import { resolveWorkspace } from "../../integrations/installation/workspace.js";
|
|
13
|
+
import { executeWorkspaceUpgrade, parseUpgradeArgs, upgradeUsage, } from "../../services/installation/upgrade-service.js";
|
|
14
|
+
import { workspacePluginBindings } from "../../services/workspace/plugin-releases.js";
|
|
15
|
+
const packageRoot = fileURLToPath(new URL("../../../", import.meta.url));
|
|
16
|
+
async function latestStableVersion(processes, cwd) {
|
|
17
|
+
const npm = await findExecutable("npm", processes.env);
|
|
18
|
+
if (!npm)
|
|
19
|
+
throw new Error("未找到 npm,无法查询 personal-bot 最新稳定版。");
|
|
20
|
+
const text = await processes.run(npm, ["view", "personal-bot@latest", "version", "engines", "--json",
|
|
21
|
+
"--fetch-retries=0", "--fetch-timeout=15000"], cwd);
|
|
22
|
+
const value = JSON.parse(text);
|
|
23
|
+
if (typeof value.version !== "string")
|
|
24
|
+
throw new Error("无法取得 personal-bot 的最新版本。");
|
|
25
|
+
return checkStableVersion(value.version, value.engines?.node);
|
|
26
|
+
}
|
|
27
|
+
async function discoverWorkspaceTargets(workspace, requestedHosts) {
|
|
28
|
+
const bindings = workspacePluginBindings(workspace);
|
|
29
|
+
const byHost = new Map();
|
|
30
|
+
for (const [host, entry] of Object.entries(bindings)) {
|
|
31
|
+
byHost.set(host, {
|
|
32
|
+
host,
|
|
33
|
+
currentVersion: entry.version,
|
|
34
|
+
...(entry.bindingPath ? { bindingPath: entry.bindingPath } : {}),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (!byHost.has("pi")) {
|
|
38
|
+
const localPiManifest = join(workspace, ".pi", "npm", "node_modules", "personal-bot", "package.json");
|
|
39
|
+
try {
|
|
40
|
+
const manifest = JSON.parse(await readFile(localPiManifest, "utf8"));
|
|
41
|
+
if (object(manifest) && manifest.name === "personal-bot" && typeof manifest.version === "string" && valid(manifest.version)) {
|
|
42
|
+
byHost.set("pi", { host: "pi", currentVersion: manifest.version, bindingPath: localPiManifest });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error.code !== "ENOENT")
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (requestedHosts) {
|
|
51
|
+
return requestedHosts.map(host => byHost.get(host) ?? { host });
|
|
52
|
+
}
|
|
53
|
+
return [...byHost.values()].sort((a, b) => a.host.localeCompare(b.host));
|
|
54
|
+
}
|
|
55
|
+
function formatLine(result) {
|
|
56
|
+
const transition = result.previousVersion && result.version && result.previousVersion !== result.version
|
|
57
|
+
? `${result.previousVersion} → ${result.version}`
|
|
58
|
+
: result.version ?? result.previousVersion ?? "";
|
|
59
|
+
const scope = result.scope ? ` (${result.scope})` : "";
|
|
60
|
+
const head = `[${result.state}] ${result.host}${scope}${transition ? " " + transition : ""}: ${result.detail}`;
|
|
61
|
+
return result.nextStep ? `${head}\n 下一步:${result.nextStep}` : head;
|
|
62
|
+
}
|
|
63
|
+
function printHumanSummary(summary) {
|
|
64
|
+
console.log(`工作区:${summary.workspaceRoot}`);
|
|
65
|
+
console.log(`目标版本:${summary.targetVersion}`);
|
|
66
|
+
console.log(`[${summary.cli.state}] cli ${summary.cli.previousVersion} → ${summary.cli.version}: ${summary.cli.detail}`);
|
|
67
|
+
if (!summary.results.length) {
|
|
68
|
+
console.log("当前工作区尚未绑定任何宿主插件。如需指定宿主,可传入 --hosts <宿主列表>。");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for (const result of summary.results)
|
|
72
|
+
console.log(formatLine(result));
|
|
73
|
+
}
|
|
74
|
+
export async function runUpgradeCommand(rawArgs, rawVerb = "upgrade") {
|
|
75
|
+
const options = parseUpgradeArgs(rawArgs);
|
|
76
|
+
if (options.help) {
|
|
77
|
+
console.log(upgradeUsage);
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
const cancel = () => controller.abort(new Error("升级已取消。"));
|
|
82
|
+
process.on("SIGINT", cancel);
|
|
83
|
+
process.on("SIGTERM", cancel);
|
|
84
|
+
try {
|
|
85
|
+
const cwd = process.cwd();
|
|
86
|
+
const workspace = await resolveWorkspace(options.workspace ?? cwd, cwd, homedir());
|
|
87
|
+
const processes = new SetupProcesses(controller.signal, process.env);
|
|
88
|
+
const targetVersion = options.version ?? await latestStableVersion(processes, cwd);
|
|
89
|
+
let cli;
|
|
90
|
+
if (process.env.PBOT_UPGRADING_CLI === targetVersion) {
|
|
91
|
+
cli = {
|
|
92
|
+
state: "updated",
|
|
93
|
+
previousVersion: process.env.PBOT_UPGRADING_PREVIOUS_VERSION || packageVersion,
|
|
94
|
+
version: packageVersion,
|
|
95
|
+
detail: `已将全局 CLI 更新到 ${packageVersion}。`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const cliResult = await upgradeManagedGlobalCli(targetVersion, process.env);
|
|
100
|
+
if (cliResult.state === "updated" && cliResult.globalDirectory) {
|
|
101
|
+
const nextEnv = {
|
|
102
|
+
...process.env,
|
|
103
|
+
PBOT_UPGRADING_CLI: targetVersion,
|
|
104
|
+
PBOT_UPGRADING_PREVIOUS_VERSION: packageVersion,
|
|
105
|
+
};
|
|
106
|
+
return await execCliEntry(join(cliResult.globalDirectory, "bin", "pbot.mjs"), [rawVerb, ...rawArgs], nextEnv);
|
|
107
|
+
}
|
|
108
|
+
cli = {
|
|
109
|
+
state: cliResult.state,
|
|
110
|
+
previousVersion: packageVersion,
|
|
111
|
+
version: packageVersion,
|
|
112
|
+
detail: cliResult.detail,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const targets = await discoverWorkspaceTargets(workspace, options.hosts);
|
|
116
|
+
const summary = await executeWorkspaceUpgrade({
|
|
117
|
+
workspaceRoot: workspace,
|
|
118
|
+
targetVersion,
|
|
119
|
+
cli,
|
|
120
|
+
targets,
|
|
121
|
+
resolveUpgrader: getHostUpgrader,
|
|
122
|
+
createContext: target => ({
|
|
123
|
+
workspace,
|
|
124
|
+
targetVersion,
|
|
125
|
+
...(target.currentVersion ? { currentVersion: target.currentVersion } : {}),
|
|
126
|
+
packageRoot,
|
|
127
|
+
...(target.bindingPath ? { bindingPath: target.bindingPath } : {}),
|
|
128
|
+
env: process.env,
|
|
129
|
+
signal: controller.signal,
|
|
130
|
+
options: { dshProfile: options.dshProfile },
|
|
131
|
+
run: (command, args, runCwd, runOptions) => processes.run(command, args, runCwd, runOptions),
|
|
132
|
+
findExecutable: name => findExecutable(name, process.env),
|
|
133
|
+
}),
|
|
134
|
+
signal: controller.signal,
|
|
135
|
+
});
|
|
136
|
+
if (options.json)
|
|
137
|
+
console.log(JSON.stringify(summary, null, 2));
|
|
138
|
+
else
|
|
139
|
+
printHumanSummary(summary);
|
|
140
|
+
return summary.ok ? 0 : 1;
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (controller.signal.aborted) {
|
|
144
|
+
console.error("升级已取消。");
|
|
145
|
+
return 130;
|
|
146
|
+
}
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
process.off("SIGINT", cancel);
|
|
151
|
+
process.off("SIGTERM", cancel);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -19894,6 +19894,22 @@ import { mkdirSync as mkdirSync2, readdirSync, readFileSync as readFileSync3 } f
|
|
|
19894
19894
|
import { homedir as homedir2 } from "node:os";
|
|
19895
19895
|
import { isAbsolute as isAbsolute3, join as join5, resolve as resolve4 } from "node:path";
|
|
19896
19896
|
init_atomic_json();
|
|
19897
|
+
function compareSemver(a, b) {
|
|
19898
|
+
const parse = (value) => {
|
|
19899
|
+
const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?/.exec(value);
|
|
19900
|
+
return match ? { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]), pre: match[4] ?? "" } : void 0;
|
|
19901
|
+
};
|
|
19902
|
+
const left = parse(a);
|
|
19903
|
+
const right = parse(b);
|
|
19904
|
+
if (!left || !right) return a.localeCompare(b);
|
|
19905
|
+
if (left.major !== right.major) return left.major - right.major;
|
|
19906
|
+
if (left.minor !== right.minor) return left.minor - right.minor;
|
|
19907
|
+
if (left.patch !== right.patch) return left.patch - right.patch;
|
|
19908
|
+
if (left.pre === right.pre) return 0;
|
|
19909
|
+
if (!left.pre) return 1;
|
|
19910
|
+
if (!right.pre) return -1;
|
|
19911
|
+
return left.pre.localeCompare(right.pre);
|
|
19912
|
+
}
|
|
19897
19913
|
function bindingDirectory(root) {
|
|
19898
19914
|
const key = createHash("sha256").update(root).digest("hex");
|
|
19899
19915
|
return join5(homedir2(), ".pbot", "workspaces", key);
|
|
@@ -19906,7 +19922,7 @@ function pluginBinding(value) {
|
|
|
19906
19922
|
function permissionDenied(error) {
|
|
19907
19923
|
return ["EACCES", "EPERM", "EROFS"].includes(error.code ?? "");
|
|
19908
19924
|
}
|
|
19909
|
-
function
|
|
19925
|
+
function workspacePluginBindings(root) {
|
|
19910
19926
|
const directory = bindingDirectory(root);
|
|
19911
19927
|
let files;
|
|
19912
19928
|
try {
|
|
@@ -19915,25 +19931,45 @@ function workspacePluginReleases(root) {
|
|
|
19915
19931
|
if (error.code === "ENOENT" || permissionDenied(error)) return {};
|
|
19916
19932
|
throw error;
|
|
19917
19933
|
}
|
|
19918
|
-
const
|
|
19934
|
+
const bindings = /* @__PURE__ */ Object.create(null);
|
|
19919
19935
|
for (const file of files.filter((name) => name.endsWith(".json"))) {
|
|
19920
19936
|
const host = decodeURIComponent(file.slice(0, -5));
|
|
19921
19937
|
try {
|
|
19922
19938
|
const binding = readJsonFile(directory, file, pluginBinding);
|
|
19923
19939
|
if (!isAbsolute3(binding)) {
|
|
19924
|
-
|
|
19940
|
+
bindings[host] = { version: binding };
|
|
19925
19941
|
continue;
|
|
19926
19942
|
}
|
|
19927
19943
|
const manifest2 = JSON.parse(readFileSync3(binding, "utf8"));
|
|
19928
19944
|
requireThat(object(manifest2) && typeof manifest2.version === "string", "\u65E0\u6548\u7684\u63D2\u4EF6\u5B89\u88C5\u6E05\u5355\uFF1A" + binding);
|
|
19929
19945
|
releaseLine(manifest2.version);
|
|
19930
|
-
|
|
19946
|
+
bindings[host] = { version: manifest2.version, bindingPath: binding };
|
|
19931
19947
|
} catch (error) {
|
|
19932
19948
|
if (error.code !== "ENOENT" && !permissionDenied(error)) throw error;
|
|
19933
19949
|
}
|
|
19934
19950
|
}
|
|
19951
|
+
return bindings;
|
|
19952
|
+
}
|
|
19953
|
+
function workspacePluginReleases(root) {
|
|
19954
|
+
const bindings = workspacePluginBindings(root);
|
|
19955
|
+
const releases = /* @__PURE__ */ Object.create(null);
|
|
19956
|
+
for (const [host, entry] of Object.entries(bindings)) releases[host] = entry.version;
|
|
19935
19957
|
return releases;
|
|
19936
19958
|
}
|
|
19959
|
+
function workspacePluginDrift(releases) {
|
|
19960
|
+
let highest;
|
|
19961
|
+
let lowest;
|
|
19962
|
+
for (const version of Object.values(releases)) {
|
|
19963
|
+
if (!highest || compareSemver(version, highest) > 0) highest = version;
|
|
19964
|
+
if (!lowest || compareSemver(version, lowest) < 0) lowest = version;
|
|
19965
|
+
}
|
|
19966
|
+
return {
|
|
19967
|
+
outOfSync: Boolean(highest && lowest && highest !== lowest),
|
|
19968
|
+
...highest ? { highest } : {},
|
|
19969
|
+
...lowest ? { lowest } : {},
|
|
19970
|
+
versions: releases
|
|
19971
|
+
};
|
|
19972
|
+
}
|
|
19937
19973
|
|
|
19938
19974
|
// src/extension/setup/workspace-cli.ts
|
|
19939
19975
|
init_workspace_config2();
|
|
@@ -20077,7 +20113,15 @@ async function main(args) {
|
|
|
20077
20113
|
}
|
|
20078
20114
|
requireThat((await stat4(root)).isDirectory(), "\u5DE5\u4F5C\u533A\u5FC5\u987B\u662F\u76EE\u5F55\u3002");
|
|
20079
20115
|
if (verb !== "history") {
|
|
20080
|
-
|
|
20116
|
+
const drift = workspacePluginDrift(plugins);
|
|
20117
|
+
print({
|
|
20118
|
+
workspaceRoot: root,
|
|
20119
|
+
version: packageVersion,
|
|
20120
|
+
plugins,
|
|
20121
|
+
outOfSync: drift.outOfSync,
|
|
20122
|
+
minimumCliVersion: required ?? null,
|
|
20123
|
+
configuration
|
|
20124
|
+
});
|
|
20081
20125
|
return configuration.error ? 1 : 0;
|
|
20082
20126
|
}
|
|
20083
20127
|
const command = historyAction(tail);
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { lstat, mkdir, readFile, realpath, rmdir } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { gt, lt, valid } from "semver";
|
|
7
|
+
import { object } from "../../common/json.js";
|
|
8
|
+
import { resourceRoot } from "../../common/paths.js";
|
|
9
|
+
import { requireThat } from "../../common/validation.js";
|
|
10
|
+
import { packageVersion } from "../../common/version.js";
|
|
11
|
+
/** Check exact installed manifests, never resolve dependencies in an ancestor workspace. */
|
|
12
|
+
export async function minimumCliRelease(root, plugins) {
|
|
13
|
+
const versions = Object.values(plugins);
|
|
14
|
+
if (process.env.PBOT_PLUGIN_VERSION)
|
|
15
|
+
versions.push(process.env.PBOT_PLUGIN_VERSION);
|
|
16
|
+
for (const prefix of ["", ".pi/npm"]) {
|
|
17
|
+
const path = join(root, prefix, "node_modules/personal-bot/package.json");
|
|
18
|
+
try {
|
|
19
|
+
const manifest = JSON.parse(await readFile(path, "utf8"));
|
|
20
|
+
requireThat(object(manifest) && manifest.name === "personal-bot" && typeof manifest.version === "string", "无效的 personal-bot 安装清单:" + path);
|
|
21
|
+
versions.push(manifest.version);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (error.code !== "ENOENT")
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let required;
|
|
29
|
+
for (const version of versions) {
|
|
30
|
+
requireThat(valid(version) && /^[0-9]/.test(version) && version === version.trim(), "无效的插件发布版本:" + version);
|
|
31
|
+
if (!required || gt(version, required))
|
|
32
|
+
required = version;
|
|
33
|
+
}
|
|
34
|
+
return required;
|
|
35
|
+
}
|
|
36
|
+
async function runProgram(command, args, env, install = false, input) {
|
|
37
|
+
const child = spawn(command, args, install
|
|
38
|
+
? { cwd: homedir(), stdio: ["ignore", 2, 2], env }
|
|
39
|
+
: { stdio: input === undefined ? "inherit" : ["pipe", "inherit", "inherit"], env });
|
|
40
|
+
const interrupt = () => child.kill("SIGINT");
|
|
41
|
+
const terminate = () => child.kill("SIGTERM");
|
|
42
|
+
process.on("SIGINT", interrupt);
|
|
43
|
+
process.on("SIGTERM", terminate);
|
|
44
|
+
try {
|
|
45
|
+
return await new Promise((resolveCode, reject) => {
|
|
46
|
+
child.once("error", reject);
|
|
47
|
+
child.once("close", (code, signal) => resolveCode(code ?? (signal === "SIGINT" ? 130 : 143)));
|
|
48
|
+
if (input !== undefined && child.stdin) {
|
|
49
|
+
child.stdin.on("error", (error) => { if (error.code !== "EPIPE")
|
|
50
|
+
reject(error); });
|
|
51
|
+
child.stdin.end(input);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
process.off("SIGINT", interrupt);
|
|
57
|
+
process.off("SIGTERM", terminate);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function globalPackage() {
|
|
61
|
+
const { stdout } = await promisify(execFile)("npm", ["root", "--global"], { cwd: homedir() });
|
|
62
|
+
const directory = join(stdout.trim(), "personal-bot");
|
|
63
|
+
try {
|
|
64
|
+
// An npm link points at a development checkout; keep updates out of that checkout.
|
|
65
|
+
if ((await lstat(directory)).isDirectory() && await realpath(directory) === await realpath(resolve(resourceRoot, "..")))
|
|
66
|
+
return directory;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error.code !== "ENOENT")
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
async function upgradeGlobal(directory, required, env) {
|
|
75
|
+
const lib = dirname(dirname(directory));
|
|
76
|
+
const lock = join(lib, ".pbot-cli-update.lock");
|
|
77
|
+
try {
|
|
78
|
+
await mkdir(lock);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
// A read-only prefix or an active updater can still use npm's per-user versioned cache.
|
|
82
|
+
if (["EEXIST", "EACCES", "EPERM", "EROFS"].includes(error.code ?? ""))
|
|
83
|
+
return undefined;
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
const installedVersion = async () => {
|
|
87
|
+
const installed = JSON.parse(await readFile(join(directory, "package.json"), "utf8"));
|
|
88
|
+
requireThat(object(installed) && installed.name === "personal-bot" && typeof installed.version === "string" && valid(installed.version), "全局 CLI 安装清单无效:" + directory);
|
|
89
|
+
return installed.version;
|
|
90
|
+
};
|
|
91
|
+
try {
|
|
92
|
+
if (lt(await installedVersion(), required)) {
|
|
93
|
+
const code = await runProgram("npm", ["install", "--global", "--prefix", dirname(lib), "--ignore-scripts",
|
|
94
|
+
"--no-audit", "--no-fund", "--prefer-offline", "personal-bot@" + required], env, true);
|
|
95
|
+
if (code)
|
|
96
|
+
return code;
|
|
97
|
+
requireThat(!lt(await installedVersion(), required), "npm 更新后的 CLI 低于插件所需版本:" + required);
|
|
98
|
+
}
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
await rmdir(lock);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export async function upgradeManagedGlobalCli(required, env = process.env) {
|
|
106
|
+
requireThat(valid(required) && /^[0-9]/.test(required) && required === required.trim(), "无效的插件发布版本:" + required);
|
|
107
|
+
if (!lt(packageVersion, required)) {
|
|
108
|
+
return { state: "up-to-date", detail: `CLI 已是 ${packageVersion}。` };
|
|
109
|
+
}
|
|
110
|
+
const global = await globalPackage();
|
|
111
|
+
if (!global) {
|
|
112
|
+
return { state: "skipped", detail: `当前运行的是本地或非全局安装(${packageVersion}),跳过全局 CLI 覆盖。` };
|
|
113
|
+
}
|
|
114
|
+
const status = await upgradeGlobal(global, required, env);
|
|
115
|
+
if (status === undefined) {
|
|
116
|
+
return { state: "skipped", globalDirectory: global, detail: "全局 npm 目录不可写或正被其他进程更新,已跳过全局 CLI 覆盖。" };
|
|
117
|
+
}
|
|
118
|
+
if (status !== 0) {
|
|
119
|
+
return { state: "failed", globalDirectory: global, exitCode: status, detail: `npm 更新全局 personal-bot@${required} 失败(退出码 ${status})。` };
|
|
120
|
+
}
|
|
121
|
+
return { state: "updated", globalDirectory: global, detail: `已将全局 CLI 从 ${packageVersion} 更新到 ${required}。` };
|
|
122
|
+
}
|
|
123
|
+
export function execCliEntry(entry, args, env = process.env) {
|
|
124
|
+
return runProgram(process.execPath, [entry, ...args], env, false);
|
|
125
|
+
}
|
|
126
|
+
/** Upgrade managed global installs in place. Project, npx and linked installs use npm's shared version cache. */
|
|
127
|
+
export async function runUpdatedCli(required, args, input) {
|
|
128
|
+
if (!required)
|
|
129
|
+
return undefined;
|
|
130
|
+
requireThat(valid(required) && /^[0-9]/.test(required) && required === required.trim(), "无效的插件发布版本:" + required);
|
|
131
|
+
if (!lt(packageVersion, required))
|
|
132
|
+
return undefined;
|
|
133
|
+
requireThat(process.env.PBOT_CLI_UPDATING !== required, `npm 启动了 CLI ${packageVersion},工作区插件需要 ${required}。`);
|
|
134
|
+
console.error(`工作区需要 Personal Bot CLI ${required},正在更新 ${packageVersion}。`);
|
|
135
|
+
const env = { ...process.env, PBOT_CLI_UPDATING: required };
|
|
136
|
+
const global = await globalPackage();
|
|
137
|
+
if (global) {
|
|
138
|
+
const status = await upgradeGlobal(global, required, env);
|
|
139
|
+
if (status !== undefined) {
|
|
140
|
+
if (status !== 0)
|
|
141
|
+
return status;
|
|
142
|
+
return runProgram(process.execPath, [join(global, "bin/pbot.mjs"), ...args], env, false, input);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return runProgram("npx", ["--yes", "--ignore-scripts", "--prefer-offline", "personal-bot@" + required, ...args], env, false, input);
|
|
146
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
};
|