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.
Files changed (54) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +2 -2
  4. package/README.zh-CN.md +2 -2
  5. package/bin/pbot.mjs +1 -1
  6. package/docs/architecture/extensibility.md +21 -0
  7. package/docs/guides/workspace-configuration.md +13 -0
  8. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +9 -2
  9. package/lib/antigravity-plugin/lib/extension/workspace-cli.js +49 -5
  10. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +12 -0
  11. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +52 -4
  12. package/lib/antigravity-plugin/package.json +1 -1
  13. package/lib/antigravity-plugin/plugin.json +1 -1
  14. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
  15. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
  16. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
  17. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +1 -1
  18. package/lib/extension/hosts/plugin-runtime.js +9 -2
  19. package/lib/extension/setup/cli.js +12 -0
  20. package/lib/extension/setup/upgrade.js +153 -0
  21. package/lib/extension/workspace-cli.js +49 -5
  22. package/lib/integrations/installation/cli-update.js +146 -0
  23. package/lib/integrations/installation/upgraders/antigravity.js +67 -0
  24. package/lib/integrations/installation/upgraders/claude.js +123 -0
  25. package/lib/integrations/installation/upgraders/codex.js +73 -0
  26. package/lib/integrations/installation/upgraders/dsh.js +62 -0
  27. package/lib/integrations/installation/upgraders/pi.js +90 -0
  28. package/lib/integrations/installation/upgraders/qoder.js +67 -0
  29. package/lib/integrations/installation/upgraders/registry.js +40 -0
  30. package/lib/qoder-plugin/.mcp.json +2 -2
  31. package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
  32. package/lib/qoder-plugin/hooks/hooks.json +3 -3
  33. package/lib/qoder-plugin/skills/pbot/SKILL.md +1 -1
  34. package/lib/services/installation/setup-service.js +4 -1
  35. package/lib/services/installation/upgrade-service.js +95 -0
  36. package/lib/services/workspace/plugin-releases.d.ts +12 -0
  37. package/lib/services/workspace/plugin-releases.js +52 -4
  38. package/package.json +1 -1
  39. package/src/extension/hosts/plugin-runtime.ts +9 -2
  40. package/src/extension/setup/cli.ts +7 -0
  41. package/src/extension/setup/upgrade.ts +158 -0
  42. package/src/extension/setup/workspace-cli.ts +10 -2
  43. package/src/integrations/installation/cli-update.ts +28 -0
  44. package/src/integrations/installation/upgraders/antigravity.ts +68 -0
  45. package/src/integrations/installation/upgraders/claude.ts +123 -0
  46. package/src/integrations/installation/upgraders/codex.ts +73 -0
  47. package/src/integrations/installation/upgraders/dsh.ts +63 -0
  48. package/src/integrations/installation/upgraders/pi.ts +87 -0
  49. package/src/integrations/installation/upgraders/qoder.ts +68 -0
  50. package/src/integrations/installation/upgraders/registry.ts +48 -0
  51. package/src/model/installation.ts +45 -0
  52. package/src/services/installation/setup-service.ts +7 -2
  53. package/src/services/installation/upgrade-service.ts +127 -0
  54. package/src/services/workspace/plugin-releases.ts +57 -4
@@ -0,0 +1,158 @@
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 type { CliUpgradeSummary, HostUpgradeResult, WorkspaceUpgradeSummary } from "../../model/installation.js";
14
+ import {
15
+ executeWorkspaceUpgrade,
16
+ parseUpgradeArgs,
17
+ upgradeUsage,
18
+ type WorkspaceUpgradeTarget,
19
+ } from "../../services/installation/upgrade-service.js";
20
+ import { workspacePluginBindings } from "../../services/workspace/plugin-releases.js";
21
+
22
+ const packageRoot = fileURLToPath(new URL("../../../", import.meta.url));
23
+
24
+ async function latestStableVersion(processes: SetupProcesses, cwd: string): Promise<string> {
25
+ const npm = await findExecutable("npm", processes.env);
26
+ if (!npm) throw new Error("未找到 npm,无法查询 personal-bot 最新稳定版。");
27
+ const text = await processes.run(npm, ["view", "personal-bot@latest", "version", "engines", "--json",
28
+ "--fetch-retries=0", "--fetch-timeout=15000"], cwd);
29
+ const value = JSON.parse(text) as { version?: string; engines?: { node?: string } };
30
+ if (typeof value.version !== "string") throw new Error("无法取得 personal-bot 的最新版本。");
31
+ return checkStableVersion(value.version, value.engines?.node);
32
+ }
33
+
34
+ async function discoverWorkspaceTargets(workspace: string, requestedHosts?: readonly string[]): Promise<WorkspaceUpgradeTarget[]> {
35
+ const bindings = workspacePluginBindings(workspace);
36
+ const byHost = new Map<string, WorkspaceUpgradeTarget>();
37
+ for (const [host, entry] of Object.entries(bindings)) {
38
+ byHost.set(host, {
39
+ host,
40
+ currentVersion: entry.version,
41
+ ...(entry.bindingPath ? { bindingPath: entry.bindingPath } : {}),
42
+ });
43
+ }
44
+ if (!byHost.has("pi")) {
45
+ const localPiManifest = join(workspace, ".pi", "npm", "node_modules", "personal-bot", "package.json");
46
+ try {
47
+ const manifest: unknown = JSON.parse(await readFile(localPiManifest, "utf8"));
48
+ if (object(manifest) && manifest.name === "personal-bot" && typeof manifest.version === "string" && valid(manifest.version)) {
49
+ byHost.set("pi", { host: "pi", currentVersion: manifest.version, bindingPath: localPiManifest });
50
+ }
51
+ } catch (error) {
52
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
53
+ }
54
+ }
55
+ if (requestedHosts) {
56
+ return requestedHosts.map(host => byHost.get(host) ?? { host });
57
+ }
58
+ return [...byHost.values()].sort((a, b) => a.host.localeCompare(b.host));
59
+ }
60
+
61
+ function formatLine(result: HostUpgradeResult): string {
62
+ const transition = result.previousVersion && result.version && result.previousVersion !== result.version
63
+ ? `${result.previousVersion} → ${result.version}`
64
+ : result.version ?? result.previousVersion ?? "";
65
+ const scope = result.scope ? ` (${result.scope})` : "";
66
+ const head = `[${result.state}] ${result.host}${scope}${transition ? " " + transition : ""}: ${result.detail}`;
67
+ return result.nextStep ? `${head}\n 下一步:${result.nextStep}` : head;
68
+ }
69
+
70
+ function printHumanSummary(summary: WorkspaceUpgradeSummary): void {
71
+ console.log(`工作区:${summary.workspaceRoot}`);
72
+ console.log(`目标版本:${summary.targetVersion}`);
73
+ console.log(`[${summary.cli.state}] cli ${summary.cli.previousVersion} → ${summary.cli.version}: ${summary.cli.detail}`);
74
+ if (!summary.results.length) {
75
+ console.log("当前工作区尚未绑定任何宿主插件。如需指定宿主,可传入 --hosts <宿主列表>。");
76
+ return;
77
+ }
78
+ for (const result of summary.results) console.log(formatLine(result));
79
+ }
80
+
81
+ export async function runUpgradeCommand(rawArgs: string[], rawVerb: "upgrade" | "update" = "upgrade"): Promise<number> {
82
+ const options = parseUpgradeArgs(rawArgs);
83
+ if (options.help) {
84
+ console.log(upgradeUsage);
85
+ return 0;
86
+ }
87
+ const controller = new AbortController();
88
+ const cancel = () => controller.abort(new Error("升级已取消。"));
89
+ process.on("SIGINT", cancel);
90
+ process.on("SIGTERM", cancel);
91
+ try {
92
+ const cwd = process.cwd();
93
+ const workspace = await resolveWorkspace(options.workspace ?? cwd, cwd, homedir());
94
+ const processes = new SetupProcesses(controller.signal, process.env);
95
+ const targetVersion = options.version ?? await latestStableVersion(processes, cwd);
96
+
97
+ let cli: CliUpgradeSummary;
98
+ if (process.env.PBOT_UPGRADING_CLI === targetVersion) {
99
+ cli = {
100
+ state: "updated",
101
+ previousVersion: process.env.PBOT_UPGRADING_PREVIOUS_VERSION || packageVersion,
102
+ version: packageVersion,
103
+ detail: `已将全局 CLI 更新到 ${packageVersion}。`,
104
+ };
105
+ } else {
106
+ const cliResult = await upgradeManagedGlobalCli(targetVersion, process.env);
107
+ if (cliResult.state === "updated" && cliResult.globalDirectory) {
108
+ const nextEnv = {
109
+ ...process.env,
110
+ PBOT_UPGRADING_CLI: targetVersion,
111
+ PBOT_UPGRADING_PREVIOUS_VERSION: packageVersion,
112
+ };
113
+ return await execCliEntry(join(cliResult.globalDirectory, "bin", "pbot.mjs"), [rawVerb, ...rawArgs], nextEnv);
114
+ }
115
+ cli = {
116
+ state: cliResult.state,
117
+ previousVersion: packageVersion,
118
+ version: packageVersion,
119
+ detail: cliResult.detail,
120
+ };
121
+ }
122
+
123
+ const targets = await discoverWorkspaceTargets(workspace, options.hosts);
124
+ const summary = await executeWorkspaceUpgrade({
125
+ workspaceRoot: workspace,
126
+ targetVersion,
127
+ cli,
128
+ targets,
129
+ resolveUpgrader: getHostUpgrader,
130
+ createContext: target => ({
131
+ workspace,
132
+ targetVersion,
133
+ ...(target.currentVersion ? { currentVersion: target.currentVersion } : {}),
134
+ packageRoot,
135
+ ...(target.bindingPath ? { bindingPath: target.bindingPath } : {}),
136
+ env: process.env,
137
+ signal: controller.signal,
138
+ options: { dshProfile: options.dshProfile },
139
+ run: (command, args, runCwd, runOptions) => processes.run(command, args, runCwd, runOptions),
140
+ findExecutable: name => findExecutable(name, process.env),
141
+ }),
142
+ signal: controller.signal,
143
+ });
144
+
145
+ if (options.json) console.log(JSON.stringify(summary, null, 2));
146
+ else printHumanSummary(summary);
147
+ return summary.ok ? 0 : 1;
148
+ } catch (error) {
149
+ if (controller.signal.aborted) {
150
+ console.error("升级已取消。");
151
+ return 130;
152
+ }
153
+ throw error;
154
+ } finally {
155
+ process.off("SIGINT", cancel);
156
+ process.off("SIGTERM", cancel);
157
+ }
158
+ }
@@ -10,7 +10,7 @@ import { getPluginHost, pluginMcpWorkspaceRoot } from "../../integrations/hosts/
10
10
  import { minimumCliRelease, runUpdatedCli } from "../../integrations/installation/cli-update.js";
11
11
  import { resolveWorkspace } from "../../integrations/installation/workspace.js";
12
12
  import { parseHistoryCommand, type ParsedHistoryCommand } from "../../services/history/summary-command.js";
13
- import { workspacePluginReleases } from "../../services/workspace/plugin-releases.js";
13
+ import { workspacePluginDrift, workspacePluginReleases } from "../../services/workspace/plugin-releases.js";
14
14
  import { readWorkspaceConfig, workspaceDisabledMessage, workspaceFeatures } from "../../services/workspace/workspace-config.js";
15
15
  import { initializeWorkspace } from "../../services/workspace/workspace-preparation.js";
16
16
 
@@ -121,7 +121,15 @@ export async function main(args: string[]): Promise<number> {
121
121
  }
122
122
  requireThat((await stat(root)).isDirectory(), "工作区必须是目录。");
123
123
  if (verb !== "history") {
124
- print({ workspaceRoot: root, version: packageVersion, plugins, minimumCliVersion: required ?? null, configuration });
124
+ const drift = workspacePluginDrift(plugins);
125
+ print({
126
+ workspaceRoot: root,
127
+ version: packageVersion,
128
+ plugins,
129
+ outOfSync: drift.outOfSync,
130
+ minimumCliVersion: required ?? null,
131
+ configuration,
132
+ });
125
133
  return configuration.error ? 1 : 0;
126
134
  }
127
135
  const command = historyAction(tail);
@@ -95,6 +95,34 @@ async function upgradeGlobal(directory: string, required: string, env: NodeJS.Pr
95
95
  }
96
96
  }
97
97
 
98
+ export async function upgradeManagedGlobalCli(required: string, env: NodeJS.ProcessEnv = process.env): Promise<{
99
+ state: "updated" | "up-to-date" | "skipped" | "failed";
100
+ globalDirectory?: string;
101
+ exitCode?: number;
102
+ detail: string;
103
+ }> {
104
+ requireThat(valid(required) && /^[0-9]/.test(required) && required === required.trim(), "无效的插件发布版本:" + required);
105
+ if (!lt(packageVersion, required)) {
106
+ return { state: "up-to-date", detail: `CLI 已是 ${packageVersion}。` };
107
+ }
108
+ const global = await globalPackage();
109
+ if (!global) {
110
+ return { state: "skipped", detail: `当前运行的是本地或非全局安装(${packageVersion}),跳过全局 CLI 覆盖。` };
111
+ }
112
+ const status = await upgradeGlobal(global, required, env);
113
+ if (status === undefined) {
114
+ return { state: "skipped", globalDirectory: global, detail: "全局 npm 目录不可写或正被其他进程更新,已跳过全局 CLI 覆盖。" };
115
+ }
116
+ if (status !== 0) {
117
+ return { state: "failed", globalDirectory: global, exitCode: status, detail: `npm 更新全局 personal-bot@${required} 失败(退出码 ${status})。` };
118
+ }
119
+ return { state: "updated", globalDirectory: global, detail: `已将全局 CLI 从 ${packageVersion} 更新到 ${required}。` };
120
+ }
121
+
122
+ export function execCliEntry(entry: string, args: readonly string[], env: NodeJS.ProcessEnv = process.env): Promise<number> {
123
+ return runProgram(process.execPath, [entry, ...args], env, false);
124
+ }
125
+
98
126
  /** Upgrade managed global installs in place. Project, npx and linked installs use npm's shared version cache. */
99
127
  export async function runUpdatedCli(required: string | undefined, args: readonly string[], input?: string): Promise<number | undefined> {
100
128
  if (!required) return undefined;
@@ -0,0 +1,68 @@
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
+ import type { HostUpgradeContext, HostUpgradeResult, HostUpgrader } from "../../../model/installation.js";
6
+
7
+ async function readManifestVersion(path: string): Promise<string | undefined> {
8
+ try {
9
+ const data: unknown = JSON.parse(await readFile(path, "utf8"));
10
+ return object(data) && typeof data.version === "string" && valid(data.version) ? data.version : undefined;
11
+ } catch (error) {
12
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
13
+ throw error;
14
+ }
15
+ }
16
+
17
+ export const antigravityUpgrader: HostUpgrader = {
18
+ host: "antigravity",
19
+ async upgrade(context: HostUpgradeContext): Promise<HostUpgradeResult> {
20
+ if (context.currentVersion && valid(context.currentVersion) && gte(context.currentVersion, context.targetVersion)) {
21
+ return {
22
+ host: this.host,
23
+ state: "up-to-date",
24
+ previousVersion: context.currentVersion,
25
+ version: context.currentVersion,
26
+ scope: "user",
27
+ detail: "已是最新版本。",
28
+ };
29
+ }
30
+ const pluginDir = join(context.packageRoot, "lib", "antigravity-plugin");
31
+ const bundledVersion = await readManifestVersion(join(pluginDir, "plugin.json"));
32
+ if (!bundledVersion) {
33
+ return {
34
+ host: this.host,
35
+ state: "skipped",
36
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
37
+ detail: `缺少 Antigravity 插件构建产物:${pluginDir}`,
38
+ };
39
+ }
40
+ if (lt(bundledVersion, context.targetVersion)) {
41
+ return {
42
+ host: this.host,
43
+ state: "skipped",
44
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
45
+ detail: `当前本地包产物版本为 ${bundledVersion},低于目标版本 ${context.targetVersion};请先更新全局 personal-bot 包。`,
46
+ };
47
+ }
48
+ const agy = context.env.PBOT_AGY_COMMAND || await context.findExecutable("agy");
49
+ if (!agy) {
50
+ return {
51
+ host: this.host,
52
+ state: "skipped",
53
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
54
+ detail: "未找到 Antigravity CLI(agy),已跳过。",
55
+ };
56
+ }
57
+ await context.run(agy, ["plugin", "install", pluginDir], context.workspace);
58
+ return {
59
+ host: this.host,
60
+ state: "updated",
61
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
62
+ version: bundledVersion,
63
+ scope: "user",
64
+ detail: `已通过 agy plugin install 更新到 ${bundledVersion}。`,
65
+ nextStep: "重启 Antigravity 会话使新版本生效。",
66
+ };
67
+ },
68
+ };
@@ -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
+ import type { HostUpgradeContext, HostUpgradeResult, HostUpgrader } from "../../../model/installation.js";
5
+
6
+ interface ClaudePluginEntry {
7
+ id: string;
8
+ version?: string;
9
+ scope?: string;
10
+ projectPath?: string;
11
+ }
12
+
13
+ function parsePluginList(raw: string): ClaudePluginEntry[] {
14
+ const parsed: unknown = JSON.parse(raw);
15
+ const items = Array.isArray(parsed)
16
+ ? parsed
17
+ : object(parsed) && Array.isArray(parsed.plugins)
18
+ ? parsed.plugins
19
+ : [];
20
+ const entries: ClaudePluginEntry[] = [];
21
+ for (const item of items) {
22
+ if (!object(item)) continue;
23
+ const id = typeof item.id === "string" ? item.id : typeof item.name === "string" ? item.name : undefined;
24
+ if (!id) continue;
25
+ entries.push({
26
+ id,
27
+ ...(typeof item.version === "string" ? { version: item.version } : {}),
28
+ ...(typeof item.scope === "string" ? { scope: item.scope } : {}),
29
+ ...(typeof item.projectPath === "string" ? { projectPath: item.projectPath } : {}),
30
+ });
31
+ }
32
+ return entries;
33
+ }
34
+
35
+ async function resolvePathSafe(value: string): Promise<string> {
36
+ try { return await realpath(value); } catch { return value; }
37
+ }
38
+
39
+ async function selectMatchingEntry(entries: ClaudePluginEntry[], workspace: string): Promise<ClaudePluginEntry | undefined> {
40
+ const canonicalWorkspace = await resolvePathSafe(workspace);
41
+ const candidates = entries.filter(entry => entry.id === "personal-bot" || entry.id.startsWith("personal-bot@"));
42
+ if (!candidates.length) return undefined;
43
+ const scopedMatches: ClaudePluginEntry[] = [];
44
+ for (const entry of candidates) {
45
+ if (!entry.projectPath) {
46
+ scopedMatches.push(entry);
47
+ continue;
48
+ }
49
+ if (await resolvePathSafe(entry.projectPath) === canonicalWorkspace) scopedMatches.push(entry);
50
+ }
51
+ const priority = new Map<string, number>([["local", 0], ["project", 1], ["user", 2]]);
52
+ scopedMatches.sort((a, b) => (priority.get(a.scope ?? "") ?? 9) - (priority.get(b.scope ?? "") ?? 9));
53
+ return scopedMatches[0];
54
+ }
55
+
56
+ export const claudeUpgrader: HostUpgrader = {
57
+ host: "claude",
58
+ async upgrade(context: HostUpgradeContext): Promise<HostUpgradeResult> {
59
+ const claude = context.env.PBOT_CLAUDE_COMMAND || await context.findExecutable("claude");
60
+ if (!claude) {
61
+ return {
62
+ host: this.host,
63
+ state: "skipped",
64
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
65
+ detail: "未找到 Claude Code CLI(claude),已跳过。",
66
+ };
67
+ }
68
+ let entries: ClaudePluginEntry[];
69
+ try {
70
+ const raw = await context.run(claude, ["plugin", "list", "--json"], context.workspace);
71
+ entries = parsePluginList(raw);
72
+ } catch (error) {
73
+ return {
74
+ host: this.host,
75
+ state: "failed",
76
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
77
+ detail: `查询 Claude 插件列表失败:${error instanceof Error ? error.message : String(error)}`,
78
+ };
79
+ }
80
+ const entry = await selectMatchingEntry(entries, context.workspace);
81
+ if (!entry) {
82
+ return {
83
+ host: this.host,
84
+ state: "skipped",
85
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
86
+ detail: "未在 Claude 插件列表中找到 personal-bot(可能通过 --plugin-dir 本地加载),已跳过。",
87
+ };
88
+ }
89
+ const installedVersion = entry.version ?? context.currentVersion;
90
+ const scope = entry.scope ?? "user";
91
+ if (installedVersion && valid(installedVersion) && gte(installedVersion, context.targetVersion)) {
92
+ return {
93
+ host: this.host,
94
+ state: "up-to-date",
95
+ previousVersion: installedVersion,
96
+ version: installedVersion,
97
+ scope,
98
+ detail: "已是最新版本。",
99
+ };
100
+ }
101
+ const atIndex = entry.id.indexOf("@");
102
+ const marketplace = atIndex > 0 ? entry.id.slice(atIndex + 1) : undefined;
103
+ if (marketplace) {
104
+ try {
105
+ await context.run(claude, ["plugin", "marketplace", "update", marketplace], context.workspace);
106
+ } catch {
107
+ // Continue to plugin update even if marketplace refresh is unsupported or cached.
108
+ }
109
+ }
110
+ const updateArgs = ["plugin", "update", entry.id, "--yes"];
111
+ if (entry.scope) 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
+ import type { HostUpgradeContext, HostUpgradeResult, HostUpgrader } from "../../../model/installation.js";
6
+
7
+ async function readManifestVersion(path: string): Promise<string | undefined> {
8
+ try {
9
+ const data: unknown = JSON.parse(await readFile(path, "utf8"));
10
+ return object(data) && typeof data.version === "string" && valid(data.version) ? data.version : undefined;
11
+ } catch (error) {
12
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
13
+ throw error;
14
+ }
15
+ }
16
+
17
+ export const codexUpgrader: HostUpgrader = {
18
+ host: "codex",
19
+ async upgrade(context: HostUpgradeContext): Promise<HostUpgradeResult> {
20
+ if (context.currentVersion && valid(context.currentVersion) && gte(context.currentVersion, context.targetVersion)) {
21
+ return {
22
+ host: this.host,
23
+ state: "up-to-date",
24
+ previousVersion: context.currentVersion,
25
+ version: context.currentVersion,
26
+ scope: "user",
27
+ detail: "已是最新版本。",
28
+ };
29
+ }
30
+ const marketplaceDir = join(context.packageRoot, "lib", "codex-marketplace");
31
+ const bundledVersion = await readManifestVersion(join(marketplaceDir, "plugins", "personal-bot", ".codex-plugin", "plugin.json"));
32
+ if (!bundledVersion) {
33
+ return {
34
+ host: this.host,
35
+ state: "skipped",
36
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
37
+ detail: `缺少 Codex 插件构建产物:${marketplaceDir}`,
38
+ };
39
+ }
40
+ if (lt(bundledVersion, context.targetVersion)) {
41
+ return {
42
+ host: this.host,
43
+ state: "skipped",
44
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
45
+ detail: `当前本地包产物版本为 ${bundledVersion},低于目标版本 ${context.targetVersion};请先更新全局 personal-bot 包。`,
46
+ };
47
+ }
48
+ const codex = context.env.PBOT_CODEX_COMMAND || await context.findExecutable("codex");
49
+ if (!codex) {
50
+ return {
51
+ host: this.host,
52
+ state: "skipped",
53
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
54
+ detail: "未找到 Codex CLI(codex),已跳过。",
55
+ };
56
+ }
57
+ try {
58
+ await context.run(codex, ["plugin", "marketplace", "add", marketplaceDir], context.workspace);
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
+ };
@@ -0,0 +1,63 @@
1
+ import { gte, valid } from "semver";
2
+ import type { HostUpgradeContext, HostUpgradeResult, HostUpgrader } from "../../../model/installation.js";
3
+
4
+ export function inferDshProfile(bindingPath?: string): string | undefined {
5
+ if (!bindingPath) return undefined;
6
+ const normalized = bindingPath.replaceAll("\\", "/");
7
+ const match = /\/profiles\/([^/]+)\/node_modules\/personal-bot\/package\.json$/.exec(normalized);
8
+ return match?.[1];
9
+ }
10
+
11
+ export const dshUpgrader: HostUpgrader = {
12
+ host: "dsh",
13
+ async upgrade(context: HostUpgradeContext): Promise<HostUpgradeResult> {
14
+ if (context.currentVersion && valid(context.currentVersion) && gte(context.currentVersion, context.targetVersion)) {
15
+ return {
16
+ host: this.host,
17
+ state: "up-to-date",
18
+ previousVersion: context.currentVersion,
19
+ version: context.currentVersion,
20
+ detail: "已是最新版本。",
21
+ };
22
+ }
23
+ const profile = context.options.dshProfile || inferDshProfile(context.bindingPath);
24
+ if (!profile) {
25
+ return {
26
+ host: this.host,
27
+ state: "skipped",
28
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
29
+ detail: "无法自动识别 dsh profile;请传入 --dsh-profile <名称> 或手动执行:dsh plugin --profile <名称> update personal-bot --latest",
30
+ };
31
+ }
32
+ const dsh = context.env.PBOT_DSH_COMMAND || await context.findExecutable("dsh");
33
+ if (!dsh) {
34
+ return {
35
+ host: this.host,
36
+ state: "skipped",
37
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
38
+ scope: `profile:${profile}`,
39
+ detail: "未找到 dsh CLI(dsh),已跳过。",
40
+ };
41
+ }
42
+ try {
43
+ await context.run(dsh, ["plugin", "--profile", profile, "update", "personal-bot", "--latest"], context.workspace);
44
+ } catch (error) {
45
+ return {
46
+ host: this.host,
47
+ state: "failed",
48
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
49
+ scope: `profile:${profile}`,
50
+ detail: `${error instanceof Error ? error.message : String(error)}(提示:更新 dsh 插件前需先停止对应 profile 的 dsh 服务)`,
51
+ };
52
+ }
53
+ return {
54
+ host: this.host,
55
+ state: "updated",
56
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
57
+ version: context.targetVersion,
58
+ scope: `profile:${profile}`,
59
+ detail: `已更新 dsh profile "${profile}" 中的 personal-bot。`,
60
+ nextStep: `重新启动 dsh --profile ${profile} 使新版本生效。`,
61
+ };
62
+ },
63
+ };
@@ -0,0 +1,87 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import { gte, valid } from "semver";
4
+ import { object } from "../../../common/json.js";
5
+ import { npmName } from "../../../common/package-source.js";
6
+ import type { HostUpgradeContext, HostUpgradeResult, HostUpgrader } from "../../../model/installation.js";
7
+
8
+ function packageSource(entry: unknown): string | undefined {
9
+ if (typeof entry === "string") return entry;
10
+ if (object(entry) && typeof entry.source === "string") return entry.source;
11
+ return undefined;
12
+ }
13
+
14
+ async function hasCustomOnlySource(workspace: string): Promise<boolean> {
15
+ const settingsPath = join(workspace, ".pi", "settings.json");
16
+ try {
17
+ const data: unknown = JSON.parse(await readFile(settingsPath, "utf8"));
18
+ if (!object(data) || !Array.isArray(data.packages)) return false;
19
+ const sources = data.packages.map(packageSource).filter((value): value is string => value !== undefined);
20
+ const matches = sources.filter(source => source.includes("personal-bot"));
21
+ if (!matches.length) return false;
22
+ return matches.every(source => npmName(source) !== "personal-bot");
23
+ } catch (error) {
24
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
25
+ throw error;
26
+ }
27
+ }
28
+
29
+ async function readLocalPiVersion(workspace: string): Promise<string | undefined> {
30
+ const manifestPath = join(workspace, ".pi", "npm", "node_modules", "personal-bot", "package.json");
31
+ try {
32
+ const data: unknown = JSON.parse(await readFile(manifestPath, "utf8"));
33
+ return object(data) && data.name === "personal-bot" && typeof data.version === "string" && valid(data.version)
34
+ ? data.version
35
+ : undefined;
36
+ } catch (error) {
37
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
38
+ throw error;
39
+ }
40
+ }
41
+
42
+ export const piUpgrader: HostUpgrader = {
43
+ host: "pi",
44
+ async upgrade(context: HostUpgradeContext): Promise<HostUpgradeResult> {
45
+ const current = await readLocalPiVersion(context.workspace) ?? context.currentVersion;
46
+ if (current && valid(current) && gte(current, context.targetVersion)) {
47
+ return {
48
+ host: this.host,
49
+ state: "up-to-date",
50
+ previousVersion: current,
51
+ version: current,
52
+ scope: "local",
53
+ detail: "已是最新版本。",
54
+ };
55
+ }
56
+ if (await hasCustomOnlySource(context.workspace)) {
57
+ return {
58
+ host: this.host,
59
+ state: "skipped",
60
+ ...(current ? { previousVersion: current } : {}),
61
+ scope: "local",
62
+ detail: "工作区 Pi 配置中的 personal-bot 使用非 npm 自定义来源,已保留当前配置。",
63
+ };
64
+ }
65
+ const pi = context.env.PBOT_PI_COMMAND || await context.findExecutable("pi");
66
+ if (!pi) {
67
+ return {
68
+ host: this.host,
69
+ state: "skipped",
70
+ ...(current ? { previousVersion: current } : {}),
71
+ scope: "local",
72
+ detail: "未找到 Pi CLI(pi),已跳过。",
73
+ };
74
+ }
75
+ await context.run(pi, ["install", "--local", `npm:personal-bot@${context.targetVersion}`], context.workspace);
76
+ const installed = await readLocalPiVersion(context.workspace) ?? context.targetVersion;
77
+ return {
78
+ host: this.host,
79
+ state: "updated",
80
+ ...(current ? { previousVersion: current } : {}),
81
+ version: installed,
82
+ scope: "local",
83
+ detail: `已通过 pi install --local 更新工作区 personal-bot 到 ${installed}。`,
84
+ nextStep: "在 Pi 中执行 /reload 或重启 Pi 使新版本生效。",
85
+ };
86
+ },
87
+ };