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.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/bin/pbot.mjs +3 -17
- package/docs/architecture/design.md +8 -6
- package/docs/architecture/extensibility.md +1 -1
- package/docs/architecture/principles.md +1 -1
- package/lib/antigravity-plugin/lib/common/paths.d.ts +1 -0
- package/lib/antigravity-plugin/lib/common/paths.js +2 -0
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +26 -24
- package/lib/antigravity-plugin/lib/extension/cli.js +22596 -0
- package/lib/antigravity-plugin/lib/extension/mcp.js +22 -20
- package/lib/antigravity-plugin/lib/extension/workspace-cli.js +5 -20165
- package/lib/antigravity-plugin/package.json +1 -1
- package/lib/antigravity-plugin/plugin.json +1 -1
- package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
- package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
- package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
- package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +1 -1
- package/lib/common/paths.d.ts +1 -0
- package/lib/common/paths.js +2 -0
- package/lib/extension/acp-worker.js +26 -24
- package/lib/extension/cli.js +22596 -0
- package/lib/extension/mcp.js +22 -20
- package/lib/extension/workspace-cli.js +5 -20165
- package/lib/qoder-plugin/.mcp.json +2 -2
- package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
- package/lib/qoder-plugin/hooks/hooks.json +3 -3
- package/lib/qoder-plugin/skills/pbot/SKILL.md +1 -1
- package/package.json +1 -1
- package/scripts/README.md +1 -1
- package/src/common/paths.ts +2 -0
- package/src/extension/cli/args.ts +104 -0
- package/src/extension/cli/commands/history.ts +81 -0
- package/src/extension/cli/commands/mcp.ts +165 -0
- package/src/extension/cli/commands/plugin.ts +33 -0
- package/src/extension/cli/commands/setup.ts +159 -0
- package/src/extension/cli/commands/upgrade.ts +158 -0
- package/src/extension/cli/commands/workspace.ts +71 -0
- package/src/extension/cli/index.ts +193 -0
- package/src/extension/cli/types.ts +30 -0
- package/src/integrations/installation/cli-update.ts +3 -3
- package/src/integrations/installation/pi-setup.ts +6 -3
- package/src/services/installation/setup-service.ts +0 -35
- package/lib/extension/setup/cli.js +0 -364
- package/lib/extension/setup/upgrade.js +0 -153
- package/lib/integrations/installation/cli-update.js +0 -146
- package/lib/integrations/installation/pi-setup.js +0 -193
- package/lib/integrations/installation/processes.js +0 -140
- package/lib/integrations/installation/upgraders/antigravity.js +0 -67
- package/lib/integrations/installation/upgraders/claude.js +0 -123
- package/lib/integrations/installation/upgraders/codex.js +0 -73
- package/lib/integrations/installation/upgraders/dsh.js +0 -62
- package/lib/integrations/installation/upgraders/pi.js +0 -90
- package/lib/integrations/installation/upgraders/qoder.js +0 -67
- package/lib/integrations/installation/upgraders/registry.js +0 -40
- package/lib/integrations/installation/workspace.js +0 -21
- package/lib/services/installation/setup-service.js +0 -140
- package/lib/services/installation/upgrade-service.js +0 -95
- package/lib/ui/setup.js +0 -52
- package/src/extension/setup/cli.ts +0 -303
- package/src/extension/setup/upgrade.ts +0 -158
- package/src/extension/setup/workspace-cli.ts +0 -167
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { valid } from "semver";
|
|
4
|
+
import { object } from "../../../common/json.js";
|
|
5
|
+
import { checkStableVersion } from "../../../common/package-source.js";
|
|
6
|
+
import { packageRoot } from "../../../common/paths.js";
|
|
7
|
+
import { packageVersion } from "../../../common/version.js";
|
|
8
|
+
import { execCliEntry, upgradeManagedGlobalCli } from "../../../integrations/installation/cli-update.js";
|
|
9
|
+
import { findExecutable, SetupProcesses } from "../../../integrations/installation/processes.js";
|
|
10
|
+
import { getHostUpgrader } from "../../../integrations/installation/upgraders/registry.js";
|
|
11
|
+
import type { CliUpgradeSummary, HostUpgradeResult, WorkspaceUpgradeSummary } from "../../../model/installation.js";
|
|
12
|
+
import {
|
|
13
|
+
executeWorkspaceUpgrade,
|
|
14
|
+
parseUpgradeArgs,
|
|
15
|
+
upgradeUsage,
|
|
16
|
+
type WorkspaceUpgradeTarget,
|
|
17
|
+
} from "../../../services/installation/upgrade-service.js";
|
|
18
|
+
import { workspacePluginBindings } from "../../../services/workspace/plugin-releases.js";
|
|
19
|
+
import { CliError, resolveCliWorkspace, withSignals } from "../args.js";
|
|
20
|
+
import type { CliCommand } from "../types.js";
|
|
21
|
+
|
|
22
|
+
async function latestStableVersion(processes: SetupProcesses, cwd: string): Promise<string> {
|
|
23
|
+
const npm = await findExecutable("npm", processes.env);
|
|
24
|
+
if (!npm) throw new Error("未找到 npm,无法查询 personal-bot 最新稳定版。");
|
|
25
|
+
const text = await processes.run(npm, ["view", "personal-bot@latest", "version", "engines", "--json",
|
|
26
|
+
"--fetch-retries=0", "--fetch-timeout=15000"], cwd);
|
|
27
|
+
const value = JSON.parse(text) as { version?: string; engines?: { node?: string } };
|
|
28
|
+
if (typeof value.version !== "string") throw new Error("无法取得 personal-bot 的最新版本。");
|
|
29
|
+
return checkStableVersion(value.version, value.engines?.node);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function discoverWorkspaceTargets(workspace: string, requestedHosts?: readonly string[]): Promise<WorkspaceUpgradeTarget[]> {
|
|
33
|
+
const bindings = workspacePluginBindings(workspace);
|
|
34
|
+
const byHost = new Map<string, WorkspaceUpgradeTarget>();
|
|
35
|
+
for (const [host, entry] of Object.entries(bindings)) {
|
|
36
|
+
byHost.set(host, {
|
|
37
|
+
host,
|
|
38
|
+
currentVersion: entry.version,
|
|
39
|
+
...(entry.bindingPath ? { bindingPath: entry.bindingPath } : {}),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (!byHost.has("pi")) {
|
|
43
|
+
const localPiManifest = join(workspace, ".pi", "npm", "node_modules", "personal-bot", "package.json");
|
|
44
|
+
try {
|
|
45
|
+
const manifest: unknown = JSON.parse(await readFile(localPiManifest, "utf8"));
|
|
46
|
+
if (object(manifest) && manifest.name === "personal-bot" && typeof manifest.version === "string" && valid(manifest.version)) {
|
|
47
|
+
byHost.set("pi", { host: "pi", currentVersion: manifest.version, bindingPath: localPiManifest });
|
|
48
|
+
}
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (requestedHosts) {
|
|
54
|
+
return requestedHosts.map(host => byHost.get(host) ?? { host });
|
|
55
|
+
}
|
|
56
|
+
return [...byHost.values()].sort((a, b) => a.host.localeCompare(b.host));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function formatLine(result: HostUpgradeResult): string {
|
|
60
|
+
const transition = result.previousVersion && result.version && result.previousVersion !== result.version
|
|
61
|
+
? `${result.previousVersion} → ${result.version}`
|
|
62
|
+
: result.version ?? result.previousVersion ?? "";
|
|
63
|
+
const scope = result.scope ? ` (${result.scope})` : "";
|
|
64
|
+
const head = `[${result.state}] ${result.host}${scope}${transition ? " " + transition : ""}: ${result.detail}`;
|
|
65
|
+
return result.nextStep ? `${head}\n 下一步:${result.nextStep}` : head;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function printHumanSummary(summary: WorkspaceUpgradeSummary): void {
|
|
69
|
+
console.log(`工作区:${summary.workspaceRoot}`);
|
|
70
|
+
console.log(`目标版本:${summary.targetVersion}`);
|
|
71
|
+
console.log(`[${summary.cli.state}] cli ${summary.cli.previousVersion} → ${summary.cli.version}: ${summary.cli.detail}`);
|
|
72
|
+
if (!summary.results.length) {
|
|
73
|
+
console.log("当前工作区尚未绑定任何宿主插件。如需指定宿主,可传入 --hosts <宿主列表>。");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
for (const result of summary.results) console.log(formatLine(result));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const upgradeCommand: CliCommand = {
|
|
80
|
+
name: "upgrade",
|
|
81
|
+
aliases: ["update"],
|
|
82
|
+
summary: "统一升级全局 CLI 与当前工作区已绑定的各宿主插件",
|
|
83
|
+
usage: upgradeUsage,
|
|
84
|
+
updatePolicy: "none",
|
|
85
|
+
supportsWorkspaceFlag: true,
|
|
86
|
+
supportsJsonFlag: true,
|
|
87
|
+
async run(ctx) {
|
|
88
|
+
let parsed: ReturnType<typeof parseUpgradeArgs>;
|
|
89
|
+
try {
|
|
90
|
+
parsed = parseUpgradeArgs(ctx.args);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw new CliError(error instanceof Error ? error.message : String(error), 2);
|
|
93
|
+
}
|
|
94
|
+
const workspaceArg = ctx.workspace ?? parsed.workspace;
|
|
95
|
+
const json = ctx.json || parsed.json;
|
|
96
|
+
const outcome = await withSignals(
|
|
97
|
+
async controller => {
|
|
98
|
+
const workspace = await resolveCliWorkspace(workspaceArg, ctx.cwd);
|
|
99
|
+
const processes = new SetupProcesses(controller.signal, process.env);
|
|
100
|
+
const targetVersion = parsed.version ?? await latestStableVersion(processes, ctx.cwd);
|
|
101
|
+
|
|
102
|
+
let cli: CliUpgradeSummary;
|
|
103
|
+
if (process.env.PBOT_UPGRADING_CLI === targetVersion) {
|
|
104
|
+
cli = {
|
|
105
|
+
state: "updated",
|
|
106
|
+
previousVersion: process.env.PBOT_UPGRADING_PREVIOUS_VERSION || packageVersion,
|
|
107
|
+
version: packageVersion,
|
|
108
|
+
detail: `已将全局 CLI 更新到 ${packageVersion}。`,
|
|
109
|
+
};
|
|
110
|
+
} else {
|
|
111
|
+
const cliResult = await upgradeManagedGlobalCli(targetVersion, process.env);
|
|
112
|
+
if (cliResult.state === "updated" && cliResult.globalDirectory) {
|
|
113
|
+
const nextEnv = {
|
|
114
|
+
...process.env,
|
|
115
|
+
PBOT_UPGRADING_CLI: targetVersion,
|
|
116
|
+
PBOT_UPGRADING_PREVIOUS_VERSION: packageVersion,
|
|
117
|
+
};
|
|
118
|
+
return execCliEntry(join(cliResult.globalDirectory, "bin", "pbot.mjs"), ctx.rawArgs, nextEnv);
|
|
119
|
+
}
|
|
120
|
+
cli = {
|
|
121
|
+
state: cliResult.state,
|
|
122
|
+
previousVersion: packageVersion,
|
|
123
|
+
version: packageVersion,
|
|
124
|
+
detail: cliResult.detail,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const targets = await discoverWorkspaceTargets(workspace, parsed.hosts);
|
|
129
|
+
const summary = await executeWorkspaceUpgrade({
|
|
130
|
+
workspaceRoot: workspace,
|
|
131
|
+
targetVersion,
|
|
132
|
+
cli,
|
|
133
|
+
targets,
|
|
134
|
+
resolveUpgrader: getHostUpgrader,
|
|
135
|
+
createContext: target => ({
|
|
136
|
+
workspace,
|
|
137
|
+
targetVersion,
|
|
138
|
+
...(target.currentVersion ? { currentVersion: target.currentVersion } : {}),
|
|
139
|
+
packageRoot,
|
|
140
|
+
...(target.bindingPath ? { bindingPath: target.bindingPath } : {}),
|
|
141
|
+
env: process.env,
|
|
142
|
+
signal: controller.signal,
|
|
143
|
+
options: { dshProfile: parsed.dshProfile },
|
|
144
|
+
run: (command, args, runCwd, runOptions) => processes.run(command, args, runCwd, runOptions),
|
|
145
|
+
findExecutable: name => findExecutable(name, process.env),
|
|
146
|
+
}),
|
|
147
|
+
signal: controller.signal,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
if (json) console.log(JSON.stringify(summary, null, 2));
|
|
151
|
+
else printHumanSummary(summary);
|
|
152
|
+
return summary.ok ? 0 : 1;
|
|
153
|
+
},
|
|
154
|
+
() => console.error("升级已取消。"),
|
|
155
|
+
);
|
|
156
|
+
return outcome.ok ? outcome.value : outcome.exitCode;
|
|
157
|
+
},
|
|
158
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { mkdir, stat } from "node:fs/promises";
|
|
2
|
+
import { requireThat } from "../../../common/validation.js";
|
|
3
|
+
import { packageVersion } from "../../../common/version.js";
|
|
4
|
+
import { minimumCliRelease } from "../../../integrations/installation/cli-update.js";
|
|
5
|
+
import { workspacePluginDrift, workspacePluginReleases } from "../../../services/workspace/plugin-releases.js";
|
|
6
|
+
import { readWorkspaceConfig } from "../../../services/workspace/workspace-config.js";
|
|
7
|
+
import { initializeWorkspace } from "../../../services/workspace/workspace-preparation.js";
|
|
8
|
+
import { printJson, requireNoExtraArgs, resolveCliWorkspace } from "../args.js";
|
|
9
|
+
import type { CliCommand } from "../types.js";
|
|
10
|
+
|
|
11
|
+
const initUsage = "用法:pbot init [--workspace <目录>] [--json]\n初始化共享工作区资料并启用 Personal Bot。";
|
|
12
|
+
const statusUsage = "用法:pbot status [--workspace <目录>] [--json]\n查看工作区根目录、CLI 版本、各宿主插件版本、同步状态与配置。";
|
|
13
|
+
|
|
14
|
+
async function reportWorkspaceStatus(root: string, compact: boolean, checkRelease: boolean): Promise<number> {
|
|
15
|
+
const configuration = await readWorkspaceConfig(root);
|
|
16
|
+
const active = checkRelease || configuration.enabled;
|
|
17
|
+
const plugins = active ? workspacePluginReleases(root) : {};
|
|
18
|
+
const required = active ? await minimumCliRelease(root, plugins) : undefined;
|
|
19
|
+
const drift = workspacePluginDrift(plugins);
|
|
20
|
+
printJson({
|
|
21
|
+
workspaceRoot: root,
|
|
22
|
+
version: packageVersion,
|
|
23
|
+
plugins,
|
|
24
|
+
outOfSync: drift.outOfSync,
|
|
25
|
+
minimumCliVersion: required ?? null,
|
|
26
|
+
configuration,
|
|
27
|
+
}, compact);
|
|
28
|
+
return configuration.error ? 1 : 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const initCommand: CliCommand = {
|
|
32
|
+
name: "init",
|
|
33
|
+
summary: "初始化共享资料并启用工作区",
|
|
34
|
+
usage: initUsage,
|
|
35
|
+
updatePolicy: "workspace",
|
|
36
|
+
supportsWorkspaceFlag: true,
|
|
37
|
+
supportsJsonFlag: true,
|
|
38
|
+
allowUninitializedUpdate: () => true,
|
|
39
|
+
beforeUpdate(ctx, _root, configuration) {
|
|
40
|
+
requireNoExtraArgs(ctx.args, initUsage);
|
|
41
|
+
if (configuration.error) throw new Error(configuration.error);
|
|
42
|
+
},
|
|
43
|
+
async run(ctx) {
|
|
44
|
+
requireNoExtraArgs(ctx.args, initUsage);
|
|
45
|
+
const root = await resolveCliWorkspace(ctx.workspace, ctx.cwd);
|
|
46
|
+
const existing = await readWorkspaceConfig(root);
|
|
47
|
+
if (existing.error) throw new Error(existing.error);
|
|
48
|
+
await mkdir(root, { recursive: true });
|
|
49
|
+
await initializeWorkspace(root);
|
|
50
|
+
requireThat((await stat(root)).isDirectory(), "工作区必须是目录。");
|
|
51
|
+
return reportWorkspaceStatus(root, ctx.json, true);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const statusCommand: CliCommand = {
|
|
56
|
+
name: "status",
|
|
57
|
+
summary: "查看工作区状态、插件版本与配置",
|
|
58
|
+
usage: statusUsage,
|
|
59
|
+
updatePolicy: "workspace",
|
|
60
|
+
supportsWorkspaceFlag: true,
|
|
61
|
+
supportsJsonFlag: true,
|
|
62
|
+
beforeUpdate(ctx) {
|
|
63
|
+
requireNoExtraArgs(ctx.args, statusUsage);
|
|
64
|
+
},
|
|
65
|
+
async run(ctx) {
|
|
66
|
+
requireNoExtraArgs(ctx.args, statusUsage);
|
|
67
|
+
const root = await resolveCliWorkspace(ctx.workspace, ctx.cwd);
|
|
68
|
+
requireThat((await stat(root)).isDirectory(), "工作区必须是目录。");
|
|
69
|
+
return reportWorkspaceStatus(root, ctx.json, false);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import { gte, valid } from "semver";
|
|
4
|
+
import { packageVersion } from "../../common/version.js";
|
|
5
|
+
import { minimumCliRelease, runUpdatedCli } from "../../integrations/installation/cli-update.js";
|
|
6
|
+
import { workspacePluginReleases } from "../../services/workspace/plugin-releases.js";
|
|
7
|
+
import { readWorkspaceConfig } from "../../services/workspace/workspace-config.js";
|
|
8
|
+
import { CliError, extractCommonFlags, resolveCliWorkspace } from "./args.js";
|
|
9
|
+
import { historyCommand } from "./commands/history.js";
|
|
10
|
+
import { mcpCommand } from "./commands/mcp.js";
|
|
11
|
+
import { pluginCommand } from "./commands/plugin.js";
|
|
12
|
+
import { installAntigravityCommand, setupCommand } from "./commands/setup.js";
|
|
13
|
+
import { upgradeCommand } from "./commands/upgrade.js";
|
|
14
|
+
import { initCommand, statusCommand } from "./commands/workspace.js";
|
|
15
|
+
import type { CliCommand, CliContext } from "./types.js";
|
|
16
|
+
|
|
17
|
+
const commands: readonly CliCommand[] = [
|
|
18
|
+
initCommand,
|
|
19
|
+
statusCommand,
|
|
20
|
+
historyCommand,
|
|
21
|
+
upgradeCommand,
|
|
22
|
+
mcpCommand,
|
|
23
|
+
pluginCommand,
|
|
24
|
+
setupCommand,
|
|
25
|
+
installAntigravityCommand,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const commandIndex = new Map<string, CliCommand>();
|
|
29
|
+
for (const command of commands) {
|
|
30
|
+
commandIndex.set(command.name, command);
|
|
31
|
+
for (const alias of command.aliases ?? []) commandIndex.set(alias, command);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function listCommands(): readonly CliCommand[] {
|
|
35
|
+
return commands;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function resolveCommand(name: string | undefined): CliCommand | undefined {
|
|
39
|
+
return name ? commandIndex.get(name) : undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function formatRootHelp(): string {
|
|
43
|
+
const lines = [
|
|
44
|
+
`Personal Bot ${packageVersion}`,
|
|
45
|
+
"",
|
|
46
|
+
"用法:pbot <命令> [选项] [参数]",
|
|
47
|
+
"",
|
|
48
|
+
"命令:",
|
|
49
|
+
...commands.map(cmd => {
|
|
50
|
+
const label = cmd.aliases?.length ? `${cmd.name}|${cmd.aliases.join("|")}` : cmd.name;
|
|
51
|
+
return ` ${label.padEnd(22)} ${cmd.summary}`;
|
|
52
|
+
}),
|
|
53
|
+
"",
|
|
54
|
+
"通用选项:",
|
|
55
|
+
" --workspace <目录> 指定工作区根目录(默认当前目录,适用于工作区命令)",
|
|
56
|
+
" --json 输出 JSON(init/status/history/mcp 输出单行 JSON)",
|
|
57
|
+
" -h, --help 显示帮助(可配合具体命令使用:pbot <命令> --help)",
|
|
58
|
+
" -v, --version 显示当前 CLI 版本",
|
|
59
|
+
" --at-least <版本> 检查当前 CLI 是否满足最低版本要求",
|
|
60
|
+
"",
|
|
61
|
+
"支持 Linux/macOS,需要 Node.js 22.19+ 和 npm。",
|
|
62
|
+
];
|
|
63
|
+
return lines.join("\n");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function updateForWorkspace(root: string, args: readonly string[], input?: string): Promise<number | undefined> {
|
|
67
|
+
return runUpdatedCli(await minimumCliRelease(root, workspacePluginReleases(root)), args, input);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function updateForCommand(args: readonly string[]): Promise<number | undefined> {
|
|
71
|
+
if (!args.length) return undefined;
|
|
72
|
+
if (["--version", "-v", "--at-least", "--help", "-h", "help"].includes(args[0]!)) return undefined;
|
|
73
|
+
const parsed = extractCommonFlags(args);
|
|
74
|
+
if (parsed.help || !parsed.rest.length) return undefined;
|
|
75
|
+
const [verb, ...rest] = parsed.rest;
|
|
76
|
+
const command = resolveCommand(verb);
|
|
77
|
+
if (!command) return undefined;
|
|
78
|
+
const ctx: CliContext = {
|
|
79
|
+
verb: verb!,
|
|
80
|
+
rawArgs: args,
|
|
81
|
+
args: rest,
|
|
82
|
+
...(parsed.workspace !== undefined ? { workspace: parsed.workspace } : {}),
|
|
83
|
+
json: parsed.json,
|
|
84
|
+
cwd: process.cwd(),
|
|
85
|
+
};
|
|
86
|
+
const policy = typeof command.updatePolicy === "function" ? command.updatePolicy(ctx) : command.updatePolicy;
|
|
87
|
+
if (policy !== "workspace") return undefined;
|
|
88
|
+
const root = command.resolveWorkspaceRoot
|
|
89
|
+
? await command.resolveWorkspaceRoot(ctx)
|
|
90
|
+
: await resolveCliWorkspace(ctx.workspace, ctx.cwd);
|
|
91
|
+
const configuration = await readWorkspaceConfig(root);
|
|
92
|
+
command.beforeUpdate?.(ctx, root, configuration);
|
|
93
|
+
const initializing = command.allowUninitializedUpdate?.(ctx) ?? false;
|
|
94
|
+
if (!configuration.enabled && !(initializing && !configuration.error)) return undefined;
|
|
95
|
+
return updateForWorkspace(root, args);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function main(args: readonly string[]): Promise<number> {
|
|
99
|
+
try {
|
|
100
|
+
// Fast local probes: never touch workspace config or npm.
|
|
101
|
+
if (args[0] === "--at-least") {
|
|
102
|
+
const version = args[1];
|
|
103
|
+
if (args.length !== 2 || !version || !valid(version)) {
|
|
104
|
+
throw new CliError("用法:pbot --at-least <MAJOR.MINOR.PATCH>", 1);
|
|
105
|
+
}
|
|
106
|
+
return gte(packageVersion, version) ? 0 : 1;
|
|
107
|
+
}
|
|
108
|
+
if (args.length === 1 && (args[0] === "--version" || args[0] === "-v")) {
|
|
109
|
+
console.log(packageVersion);
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const parsed = extractCommonFlags(args);
|
|
114
|
+
const [verb, ...rest] = parsed.rest;
|
|
115
|
+
|
|
116
|
+
// Top-level help or `pbot help [command]`
|
|
117
|
+
if (!verb) {
|
|
118
|
+
if (parsed.help || args.length === 0) {
|
|
119
|
+
console.log(formatRootHelp());
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
throw new CliError(`缺少命令。\n\n${formatRootHelp()}`, 2);
|
|
123
|
+
}
|
|
124
|
+
if (verb === "help") {
|
|
125
|
+
const target = resolveCommand(rest[0]);
|
|
126
|
+
if (rest.length > 1 || (rest[0] && !target)) {
|
|
127
|
+
throw new CliError(`未知命令:${rest.join(" ")}\n\n${formatRootHelp()}`, 2);
|
|
128
|
+
}
|
|
129
|
+
console.log(target ? target.usage : formatRootHelp());
|
|
130
|
+
return 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const command = resolveCommand(verb);
|
|
134
|
+
if (!command) {
|
|
135
|
+
if (parsed.help) {
|
|
136
|
+
console.log(formatRootHelp());
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
throw new CliError(`未知命令:${verb}\n\n${formatRootHelp()}`, 2);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (parsed.help) {
|
|
143
|
+
console.log(command.usage);
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (parsed.workspace !== undefined && !command.supportsWorkspaceFlag) {
|
|
148
|
+
throw new CliError(`命令 ${command.name} 不支持 --workspace 参数。\n${command.usage}`, 2);
|
|
149
|
+
}
|
|
150
|
+
if (parsed.json && !command.supportsJsonFlag) {
|
|
151
|
+
throw new CliError(`命令 ${command.name} 不支持 --json 参数。\n${command.usage}`, 2);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const ctx: CliContext = {
|
|
155
|
+
verb,
|
|
156
|
+
rawArgs: args,
|
|
157
|
+
args: rest,
|
|
158
|
+
...(parsed.workspace !== undefined ? { workspace: parsed.workspace } : {}),
|
|
159
|
+
json: parsed.json,
|
|
160
|
+
cwd: process.cwd(),
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const policy = typeof command.updatePolicy === "function" ? command.updatePolicy(ctx) : command.updatePolicy;
|
|
164
|
+
if (policy === "workspace") {
|
|
165
|
+
const root = command.resolveWorkspaceRoot
|
|
166
|
+
? await command.resolveWorkspaceRoot(ctx)
|
|
167
|
+
: await resolveCliWorkspace(ctx.workspace, ctx.cwd);
|
|
168
|
+
const configuration = await readWorkspaceConfig(root);
|
|
169
|
+
command.beforeUpdate?.(ctx, root, configuration);
|
|
170
|
+
const initializing = command.allowUninitializedUpdate?.(ctx) ?? false;
|
|
171
|
+
if (configuration.enabled || (initializing && !configuration.error)) {
|
|
172
|
+
const updated = await updateForWorkspace(root, args);
|
|
173
|
+
if (updated !== undefined) return updated;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return await command.run(ctx, { updateForWorkspace });
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error instanceof CliError) {
|
|
180
|
+
console.error(error.message);
|
|
181
|
+
return error.exitCode;
|
|
182
|
+
}
|
|
183
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
184
|
+
return 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
|
|
189
|
+
main(process.argv.slice(2)).then(code => { process.exitCode = code; }).catch(error => {
|
|
190
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
191
|
+
process.exitCode = 1;
|
|
192
|
+
});
|
|
193
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { WorkspaceConfiguration } from "../../model/workspace-config.js";
|
|
2
|
+
|
|
3
|
+
export type CliUpdatePolicy = "none" | "workspace" | "hook";
|
|
4
|
+
|
|
5
|
+
export interface CliContext {
|
|
6
|
+
readonly verb: string;
|
|
7
|
+
readonly rawArgs: readonly string[];
|
|
8
|
+
readonly args: string[];
|
|
9
|
+
readonly workspace?: string;
|
|
10
|
+
readonly json: boolean;
|
|
11
|
+
readonly cwd: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface CliCommand {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly aliases?: readonly string[];
|
|
17
|
+
readonly summary: string;
|
|
18
|
+
readonly usage: string;
|
|
19
|
+
readonly updatePolicy: CliUpdatePolicy | ((ctx: CliContext) => CliUpdatePolicy);
|
|
20
|
+
readonly supportsWorkspaceFlag?: boolean;
|
|
21
|
+
readonly supportsJsonFlag?: boolean;
|
|
22
|
+
readonly resolveWorkspaceRoot?: (ctx: CliContext) => Promise<string>;
|
|
23
|
+
readonly allowUninitializedUpdate?: (ctx: CliContext) => boolean;
|
|
24
|
+
readonly beforeUpdate?: (ctx: CliContext, root: string, configuration: WorkspaceConfiguration) => void;
|
|
25
|
+
readonly run: (ctx: CliContext, helpers: CliRuntimeHelpers) => Promise<number>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface CliRuntimeHelpers {
|
|
29
|
+
readonly updateForWorkspace: (root: string, args: readonly string[], input?: string) => Promise<number | undefined>;
|
|
30
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { execFile, spawn } from "node:child_process";
|
|
2
2
|
import { lstat, mkdir, readFile, realpath, rmdir } from "node:fs/promises";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
|
-
import { dirname, join
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
5
|
import { promisify } from "node:util";
|
|
6
6
|
import { gt, lt, valid } from "semver";
|
|
7
7
|
import { object } from "../../common/json.js";
|
|
8
|
-
import {
|
|
8
|
+
import { packageRoot } from "../../common/paths.js";
|
|
9
9
|
import { requireThat } from "../../common/validation.js";
|
|
10
10
|
import { packageVersion } from "../../common/version.js";
|
|
11
11
|
|
|
@@ -60,7 +60,7 @@ async function globalPackage(): Promise<string | undefined> {
|
|
|
60
60
|
const directory = join(stdout.trim(), "personal-bot");
|
|
61
61
|
try {
|
|
62
62
|
// An npm link points at a development checkout; keep updates out of that checkout.
|
|
63
|
-
if ((await lstat(directory)).isDirectory() && await realpath(directory) === await realpath(
|
|
63
|
+
if ((await lstat(directory)).isDirectory() && await realpath(directory) === await realpath(packageRoot)) return directory;
|
|
64
64
|
} catch (error) {
|
|
65
65
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
66
66
|
}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { access, readFile, realpath } from "node:fs/promises";
|
|
2
|
-
import { constants } from "node:fs";
|
|
2
|
+
import { constants, existsSync } from "node:fs";
|
|
3
3
|
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { checkStableVersion, npmName } from "../../common/package-source.js";
|
|
7
|
+
import { packageRoot as pbotPackageRoot } from "../../common/paths.js";
|
|
7
8
|
import type { InstalledPackage, PackageChoice, PiInstallation, SetupOperations, SystemCommand } from "../../model/installation.js";
|
|
8
9
|
import { findExecutable, npmGlobalBinary, SetupProcesses } from "./processes.js";
|
|
9
10
|
|
|
10
11
|
export const piPackageName = "@earendil-works/pi-coding-agent";
|
|
11
12
|
export class PiBootstrapRequired extends Error {}
|
|
12
|
-
const
|
|
13
|
-
const
|
|
13
|
+
const localBridge = fileURLToPath(new URL("./pi-bridge.js", import.meta.url));
|
|
14
|
+
const bridge = existsSync(localBridge) ? localBridge : join(pbotPackageRoot, "lib/integrations/installation/pi-bridge.js");
|
|
15
|
+
const localGuard = fileURLToPath(new URL("../../extension/setup/guard.js", import.meta.url));
|
|
16
|
+
const guard = existsSync(localGuard) ? localGuard : join(pbotPackageRoot, "lib/extension/setup/guard.js");
|
|
14
17
|
|
|
15
18
|
async function packageRoot(command: string, version: string, env: NodeJS.ProcessEnv): Promise<string> {
|
|
16
19
|
const resolved = await realpath(command);
|
|
@@ -41,41 +41,6 @@ export function packageChoices(catalog: PluginSpec[], installed: InstalledPackag
|
|
|
41
41
|
selected: plugin.id === "pbot" || (first ? plugin.defaultSelected : existing.length > 0), keep: custom };
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
|
-
export function parseSetupArgs(args: string[]): {
|
|
45
|
-
action: "help" | "version" | "setup" | "upgrade" | "install-antigravity" | "mcp" | "mcp-manage";
|
|
46
|
-
workspace?: string;
|
|
47
|
-
mcpArgs?: string[];
|
|
48
|
-
upgradeArgs?: string[];
|
|
49
|
-
upgradeVerb?: "upgrade" | "update";
|
|
50
|
-
} {
|
|
51
|
-
if (!args.length || args.length === 1 && ["--help", "-h"].includes(args[0]!)) return { action: "help" };
|
|
52
|
-
if (args.length === 1 && ["--version", "-v"].includes(args[0]!)) return { action: "version" };
|
|
53
|
-
if (args[0] === "upgrade" || args[0] === "update") {
|
|
54
|
-
return { action: "upgrade", upgradeArgs: args.slice(1), upgradeVerb: args[0] };
|
|
55
|
-
}
|
|
56
|
-
if (args[0] === "install-antigravity") {
|
|
57
|
-
if (args.length === 1) return { action: "install-antigravity" };
|
|
58
|
-
if (args.length === 2 && ["--help", "-h"].includes(args[1]!)) return { action: "help" };
|
|
59
|
-
throw new Error("用法:pbot install-antigravity");
|
|
60
|
-
}
|
|
61
|
-
if (args[0] === "mcp") {
|
|
62
|
-
if (args.length === 1) return { action: "mcp" };
|
|
63
|
-
if (args.length === 2 && ["--help", "-h"].includes(args[1]!)) return { action: "help" };
|
|
64
|
-
if (["list", "add", "remove"].includes(args[1]!)) return { action: "mcp-manage", mcpArgs: args.slice(1) };
|
|
65
|
-
throw new Error("用法:pbot mcp [list|add|remove ...]");
|
|
66
|
-
}
|
|
67
|
-
if (args[0] !== "setup") throw new Error("用法:pbot setup [--workspace <目录>] | pbot upgrade|update [--workspace <目录>] | pbot install-antigravity | pbot mcp [list|add|remove ...]");
|
|
68
|
-
let workspace: string | undefined;
|
|
69
|
-
for (let index = 1; index < args.length; index++) {
|
|
70
|
-
const arg = args[index]!;
|
|
71
|
-
if ((arg === "--help" || arg === "-h") && args.length === 2) return { action: "help" };
|
|
72
|
-
if (arg !== "--workspace" || workspace !== undefined) throw new Error(`不支持的参数:${arg}`);
|
|
73
|
-
const value = args[++index];
|
|
74
|
-
if (!value || value.startsWith("--") || /[\x00-\x1f\x7f]/.test(value)) throw new Error("--workspace 需要目录。");
|
|
75
|
-
workspace = value;
|
|
76
|
-
}
|
|
77
|
-
return workspace === undefined ? { action: "setup" } : { action: "setup", workspace };
|
|
78
|
-
}
|
|
79
44
|
|
|
80
45
|
export async function executeSetup(plan: SetupPlan, operations: SetupOperations,
|
|
81
46
|
report: (result: SetupResult) => void, signal: AbortSignal): Promise<{ pi?: PiInstallation; results: SetupResult[] }> {
|