personal-bot 0.3.2 → 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 (318) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +2 -1
  3. package/DISCLAIMER.md +17 -0
  4. package/LICENSE +21 -0
  5. package/README.md +15 -5
  6. package/README.zh-CN.md +15 -5
  7. package/bin/pbot-pi-acp.mjs +1 -1
  8. package/bin/pbot.mjs +22 -2
  9. package/docs/README.md +2 -0
  10. package/docs/architecture/design.md +16 -16
  11. package/docs/architecture/extensibility.md +29 -2
  12. package/docs/architecture/model-context.md +3 -2
  13. package/docs/architecture/principles.md +2 -2
  14. package/docs/guides/capabilities.md +1 -1
  15. package/docs/guides/cross-harness-workers.md +3 -1
  16. package/docs/guides/history.md +30 -7
  17. package/docs/guides/workspace-configuration.md +20 -3
  18. package/docs/hosts/antigravity.md +1 -1
  19. package/docs/hosts/claude-code.md +1 -1
  20. package/docs/hosts/codex-qoder.md +63 -0
  21. package/docs/maintainers/architecture-governance.md +78 -0
  22. package/lib/antigravity-plugin/DISCLAIMER.md +17 -0
  23. package/lib/antigravity-plugin/LICENSE +21 -0
  24. package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +36 -0
  25. package/lib/antigravity-plugin/lib/common/files.d.ts +2 -0
  26. package/lib/antigravity-plugin/lib/common/files.js +44 -0
  27. package/lib/antigravity-plugin/lib/common/validation.d.ts +4 -0
  28. package/lib/antigravity-plugin/lib/common/validation.js +8 -0
  29. package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +18 -0
  30. package/lib/antigravity-plugin/lib/extension/acp/member.js +44 -0
  31. package/lib/antigravity-plugin/lib/extension/acp-worker.js +71 -45
  32. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
  33. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.js +22 -0
  34. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.d.ts +4 -0
  35. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +58 -0
  36. package/lib/antigravity-plugin/lib/extension/hosts/dsh/files.d.ts +5 -0
  37. package/lib/antigravity-plugin/lib/extension/hosts/dsh/files.js +67 -0
  38. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.d.ts +3 -0
  39. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +100 -0
  40. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.d.ts +3 -1
  41. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +37 -22
  42. package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.d.ts +6 -0
  43. package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.js +21 -0
  44. package/lib/antigravity-plugin/lib/extension/mcp.js +2577 -2255
  45. package/lib/antigravity-plugin/lib/extension/workspace-cli.js +20171 -0
  46. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +1 -1
  47. package/lib/antigravity-plugin/lib/integrations/hosts/claude/adapter.js +7 -59
  48. package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.d.ts +2 -0
  49. package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.js +14 -0
  50. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/mcp.d.ts +4 -0
  51. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/mcp.js +28 -0
  52. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/messages.d.ts +36 -0
  53. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/messages.js +11 -0
  54. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/resources.d.ts +6 -0
  55. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/resources.js +47 -0
  56. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/root-agents.d.ts +4 -0
  57. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/root-agents.js +3 -0
  58. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/skills.d.ts +8 -0
  59. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/skills.js +31 -0
  60. package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
  61. package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.js +32 -0
  62. package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.d.ts +4 -0
  63. package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.js +23 -0
  64. package/lib/antigravity-plugin/lib/integrations/hosts/plugin-registry.d.ts +1 -0
  65. package/lib/antigravity-plugin/lib/integrations/hosts/plugin-registry.js +8 -1
  66. package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
  67. package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.js +76 -0
  68. package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
  69. package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.js +16 -0
  70. package/lib/antigravity-plugin/lib/model/acp.d.ts +7 -3
  71. package/lib/antigravity-plugin/lib/model/acp.js +8 -0
  72. package/lib/antigravity-plugin/lib/model/agent.d.ts +121 -0
  73. package/lib/antigravity-plugin/lib/model/agent.js +4 -0
  74. package/lib/antigravity-plugin/lib/model/history.d.ts +10 -0
  75. package/lib/antigravity-plugin/lib/model/history.js +4 -1
  76. package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +5 -0
  77. package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +8 -12
  78. package/lib/antigravity-plugin/lib/model/workspace-config.js +2 -1
  79. package/lib/antigravity-plugin/lib/plugin.d.ts +2 -1
  80. package/lib/antigravity-plugin/lib/plugin.js +2 -2
  81. package/lib/antigravity-plugin/lib/services/history/history-capture.js +34 -22
  82. package/lib/antigravity-plugin/lib/services/history/summary-registry.js +2 -3
  83. package/lib/antigravity-plugin/lib/services/history/summary-runner.d.ts +2 -5
  84. package/lib/antigravity-plugin/lib/services/history/summary-runner.js +72 -74
  85. package/lib/antigravity-plugin/lib/services/history/workspace-history.d.ts +3 -0
  86. package/lib/antigravity-plugin/lib/services/history/workspace-history.js +6 -0
  87. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +15 -0
  88. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +126 -0
  89. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +9 -8
  90. package/lib/antigravity-plugin/lib/services/workspace/workspace-files.d.ts +2 -0
  91. package/lib/antigravity-plugin/lib/services/workspace/workspace-files.js +17 -0
  92. package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.d.ts +14 -0
  93. package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.js +193 -0
  94. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.d.ts +1 -0
  95. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.js +4 -2
  96. package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +11 -0
  97. package/lib/antigravity-plugin/lib/storage/acp-files.js +75 -0
  98. package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +31 -0
  99. package/lib/antigravity-plugin/lib/storage/agent-files.js +159 -0
  100. package/lib/antigravity-plugin/lib/storage/atomic-json.d.ts +3 -1
  101. package/lib/antigravity-plugin/lib/storage/atomic-json.js +43 -3
  102. package/lib/antigravity-plugin/lib/storage/history-files.d.ts +2 -1
  103. package/lib/antigravity-plugin/lib/storage/history-files.js +21 -15
  104. package/lib/antigravity-plugin/lib/storage/history-format.d.ts +2 -3
  105. package/lib/antigravity-plugin/lib/storage/history-format.js +5 -7
  106. package/lib/antigravity-plugin/package.json +1 -1
  107. package/lib/antigravity-plugin/plugin.json +1 -1
  108. package/lib/antigravity-plugin/resources/prompts/history-monthly-summary.md +13 -0
  109. package/lib/antigravity-plugin/resources/prompts/history-summary.md +13 -0
  110. package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -0
  111. package/lib/antigravity-plugin/resources/skills/.claude-plugin/plugin.json +4 -0
  112. package/lib/antigravity-plugin/resources/skills/project-maintenance/SKILL.md +35 -0
  113. package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +52 -0
  114. package/lib/antigravity-plugin/skills/pbot/SKILL.md +11 -1
  115. package/lib/client.js +22 -50
  116. package/lib/codex-marketplace/.agents/plugins/marketplace.json +17 -0
  117. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +7 -0
  118. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +17 -0
  119. package/lib/codex-marketplace/plugins/personal-bot/DISCLAIMER.md +17 -0
  120. package/lib/codex-marketplace/plugins/personal-bot/LICENSE +21 -0
  121. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +37 -0
  122. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +9 -0
  123. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/agents/openai.yaml +2 -0
  124. package/lib/common/validation.d.ts +4 -0
  125. package/lib/common/validation.js +8 -0
  126. package/lib/dsh.js +2 -2
  127. package/lib/extension/acp/member.d.ts +1 -1
  128. package/lib/extension/acp-worker.js +71 -45
  129. package/lib/extension/history/command.d.ts +37 -0
  130. package/lib/extension/history/command.js +57 -0
  131. package/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
  132. package/lib/extension/hosts/dsh/acp-entry.js +22 -0
  133. package/lib/extension/hosts/dsh/acp-member.js +4 -18
  134. package/lib/extension/hosts/dsh/command.d.ts +1 -2
  135. package/lib/extension/hosts/dsh/command.js +5 -49
  136. package/lib/extension/hosts/dsh/workspace.js +13 -44
  137. package/lib/extension/hosts/pi/acp-member.d.ts +3 -0
  138. package/lib/extension/hosts/pi/acp-member.js +100 -0
  139. package/lib/extension/hosts/plugin-runtime.d.ts +3 -1
  140. package/lib/extension/hosts/plugin-runtime.js +37 -22
  141. package/lib/extension/hosts/workspace-mcp.d.ts +6 -0
  142. package/lib/extension/hosts/workspace-mcp.js +21 -0
  143. package/lib/extension/mcp/bridge.d.ts +6 -8
  144. package/lib/extension/mcp/bridge.js +66 -84
  145. package/lib/extension/mcp/tool-contract.d.ts +735 -0
  146. package/lib/extension/mcp/tool-contract.js +441 -0
  147. package/lib/extension/mcp/workers.d.ts +29 -742
  148. package/lib/extension/mcp/workers.js +75 -525
  149. package/lib/extension/mcp.js +2577 -2255
  150. package/lib/extension/setup/cli.js +15 -0
  151. package/lib/extension/setup/upgrade.js +153 -0
  152. package/lib/extension/workspace-cli.js +20171 -0
  153. package/lib/integrations/acp/agents.d.ts +2 -3
  154. package/lib/integrations/acp/harnesses/dsh.js +1 -1
  155. package/lib/integrations/acp/harnesses/presets.js +8 -0
  156. package/lib/integrations/acp/history-summary.js +2 -1
  157. package/lib/integrations/acp/launch.d.ts +2 -2
  158. package/lib/integrations/agent-resources.js +1 -1
  159. package/lib/integrations/hosts/claude/adapter.js +7 -59
  160. package/lib/integrations/hosts/codex/adapter.d.ts +2 -0
  161. package/lib/integrations/hosts/codex/adapter.js +14 -0
  162. package/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
  163. package/lib/integrations/hosts/pi/agent-tools.js +32 -0
  164. package/lib/integrations/hosts/pi/mcp.d.ts +4 -0
  165. package/lib/integrations/hosts/pi/mcp.js +23 -0
  166. package/lib/integrations/hosts/plugin-registry.d.ts +1 -0
  167. package/lib/integrations/hosts/plugin-registry.js +8 -1
  168. package/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
  169. package/lib/integrations/hosts/prompt-hooks.js +76 -0
  170. package/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
  171. package/lib/integrations/hosts/qoder/adapter.js +16 -0
  172. package/lib/integrations/installation/cli-update.js +146 -0
  173. package/lib/integrations/installation/upgraders/antigravity.js +67 -0
  174. package/lib/integrations/installation/upgraders/claude.js +123 -0
  175. package/lib/integrations/installation/upgraders/codex.js +73 -0
  176. package/lib/integrations/installation/upgraders/dsh.js +62 -0
  177. package/lib/integrations/installation/upgraders/pi.js +90 -0
  178. package/lib/integrations/installation/upgraders/qoder.js +67 -0
  179. package/lib/integrations/installation/upgraders/registry.js +40 -0
  180. package/lib/model/acp.d.ts +7 -3
  181. package/lib/model/acp.js +8 -0
  182. package/lib/model/agent.d.ts +18 -4
  183. package/lib/model/agent.js +4 -1
  184. package/lib/model/history.d.ts +10 -0
  185. package/lib/model/history.js +4 -1
  186. package/lib/model/installation.js +1 -0
  187. package/lib/model/plugin-host.d.ts +5 -0
  188. package/lib/model/workspace-config.d.ts +8 -12
  189. package/lib/model/workspace-config.js +2 -1
  190. package/lib/plugin.d.ts +2 -1
  191. package/lib/plugin.js +2 -2
  192. package/lib/qoder-plugin/.mcp.json +17 -0
  193. package/lib/qoder-plugin/.qoder-plugin/plugin.json +7 -0
  194. package/lib/qoder-plugin/DISCLAIMER.md +17 -0
  195. package/lib/qoder-plugin/LICENSE +21 -0
  196. package/lib/qoder-plugin/hooks/hooks.json +37 -0
  197. package/lib/qoder-plugin/skills/pbot/SKILL.md +9 -0
  198. package/lib/services/agents/agent-service.d.ts +15 -45
  199. package/lib/services/agents/agent-service.js +22 -3
  200. package/lib/services/history/history-capture.js +34 -22
  201. package/lib/services/history/history-migrate.js +26 -60
  202. package/lib/services/history/history-query.js +38 -57
  203. package/lib/services/history/summary-command.d.ts +1 -1
  204. package/lib/services/history/summary-command.js +8 -7
  205. package/lib/services/history/summary-registry.js +2 -3
  206. package/lib/services/history/summary-runner.d.ts +2 -5
  207. package/lib/services/history/summary-runner.js +72 -74
  208. package/lib/services/history/workspace-history.d.ts +3 -0
  209. package/lib/services/history/workspace-history.js +6 -0
  210. package/lib/services/installation/setup-service.js +4 -1
  211. package/lib/services/installation/upgrade-service.js +95 -0
  212. package/lib/services/workspace/plugin-releases.d.ts +15 -0
  213. package/lib/services/workspace/plugin-releases.js +126 -0
  214. package/lib/services/workspace/workspace-config.js +9 -8
  215. package/lib/services/workspace/workspace-mcp.js +22 -29
  216. package/lib/services/workspace/workspace-skills.d.ts +1 -0
  217. package/lib/services/workspace/workspace-skills.js +4 -2
  218. package/lib/storage/acp-files.js +7 -12
  219. package/lib/storage/agent-files.js +6 -5
  220. package/lib/storage/atomic-json.d.ts +3 -1
  221. package/lib/storage/atomic-json.js +43 -3
  222. package/lib/storage/history-files.d.ts +2 -1
  223. package/lib/storage/history-files.js +21 -15
  224. package/lib/storage/history-format.d.ts +2 -3
  225. package/lib/storage/history-format.js +5 -7
  226. package/lib/tools/history/contract.d.ts +3 -11
  227. package/lib/tools/history/contract.js +8 -7
  228. package/lib/ui/dsh/elements.d.ts +2 -5
  229. package/lib/ui/dsh/elements.js +3 -16
  230. package/lib/ui/dsh/management.js +9 -9
  231. package/lib/ui/dsh/styles.d.ts +1 -1
  232. package/lib/ui/dsh/styles.js +5 -33
  233. package/package.json +4 -1
  234. package/resources/antigravity/skills/pbot/SKILL.md +11 -1
  235. package/resources/claude/skills/pbot/SKILL.md +11 -1
  236. package/resources/plugin/skills/pbot/SKILL.md +9 -0
  237. package/resources/prompts/history-coordinator.md +1 -0
  238. package/resources/prompts/plugin-coordinator.md +1 -0
  239. package/scripts/README.md +3 -2
  240. package/src/common/validation.ts +10 -0
  241. package/src/dsh.ts +2 -2
  242. package/src/extension/history/command.ts +68 -0
  243. package/src/extension/hosts/dsh/acp-entry.ts +21 -0
  244. package/src/extension/hosts/dsh/acp-member.ts +4 -15
  245. package/src/extension/hosts/dsh/command.ts +6 -46
  246. package/src/extension/hosts/dsh/workspace.ts +12 -38
  247. package/src/extension/hosts/pi/acp-member.ts +4 -10
  248. package/src/extension/hosts/pi/command.ts +14 -63
  249. package/src/extension/hosts/pi/events.ts +2 -8
  250. package/src/extension/hosts/pi/lifecycle.ts +3 -2
  251. package/src/extension/hosts/plugin-runtime.ts +39 -19
  252. package/src/extension/hosts/workspace-mcp.ts +25 -0
  253. package/src/extension/mcp/bridge.ts +80 -80
  254. package/src/extension/mcp/tool-contract.ts +462 -0
  255. package/src/extension/mcp/workers.ts +81 -551
  256. package/src/extension/setup/cli.ts +11 -1
  257. package/src/extension/setup/upgrade.ts +158 -0
  258. package/src/extension/setup/workspace-cli.ts +167 -0
  259. package/src/integrations/acp/agents.ts +2 -3
  260. package/src/integrations/acp/harnesses/dsh.ts +1 -1
  261. package/src/integrations/acp/harnesses/presets.ts +8 -0
  262. package/src/integrations/acp/history-summary.ts +2 -1
  263. package/src/integrations/agent-resources.ts +1 -1
  264. package/src/integrations/hosts/claude/adapter.ts +7 -53
  265. package/src/integrations/hosts/codex/adapter.ts +16 -0
  266. package/src/integrations/hosts/plugin-registry.ts +9 -1
  267. package/src/integrations/hosts/prompt-hooks.ts +72 -0
  268. package/src/integrations/hosts/qoder/adapter.ts +19 -0
  269. package/src/integrations/installation/cli-update.ts +144 -0
  270. package/src/integrations/installation/pi-bridge.ts +1 -1
  271. package/src/integrations/installation/pi-setup.ts +1 -1
  272. package/src/integrations/installation/processes.ts +1 -1
  273. package/src/integrations/installation/upgraders/antigravity.ts +68 -0
  274. package/src/integrations/installation/upgraders/claude.ts +123 -0
  275. package/src/integrations/installation/upgraders/codex.ts +73 -0
  276. package/src/integrations/installation/upgraders/dsh.ts +63 -0
  277. package/src/integrations/installation/upgraders/pi.ts +87 -0
  278. package/src/integrations/installation/upgraders/qoder.ts +68 -0
  279. package/src/integrations/installation/upgraders/registry.ts +48 -0
  280. package/src/model/acp.ts +11 -3
  281. package/src/model/agent.ts +20 -4
  282. package/src/model/history.ts +5 -0
  283. package/src/model/installation.ts +86 -0
  284. package/src/model/plugin-host.ts +5 -1
  285. package/src/model/workspace-config.ts +6 -12
  286. package/src/pbot.ts +13 -32
  287. package/src/plugin.ts +3 -3
  288. package/src/services/agents/agent-service.ts +29 -17
  289. package/src/services/history/history-capture.ts +30 -22
  290. package/src/services/history/history-migrate.ts +23 -50
  291. package/src/services/history/history-query.ts +34 -48
  292. package/src/services/history/summary-command.ts +9 -8
  293. package/src/services/history/summary-registry.ts +2 -3
  294. package/src/services/history/summary-runner.ts +72 -81
  295. package/src/services/history/workspace-history.ts +8 -0
  296. package/src/services/installation/setup-service.ts +9 -43
  297. package/src/services/installation/upgrade-service.ts +127 -0
  298. package/src/services/workspace/plugin-releases.ts +121 -0
  299. package/src/services/workspace/workspace-config.ts +9 -8
  300. package/src/services/workspace/workspace-mcp.ts +23 -28
  301. package/src/services/workspace/workspace-skills.ts +5 -2
  302. package/src/storage/acp-files.ts +7 -12
  303. package/src/storage/agent-files.ts +6 -5
  304. package/src/storage/atomic-json.ts +31 -4
  305. package/src/storage/history-files.ts +22 -15
  306. package/src/storage/history-format.ts +6 -8
  307. package/src/tools/history/contract.ts +9 -9
  308. package/src/tools/history/pi-tool.ts +6 -4
  309. package/src/ui/dsh/elements.ts +5 -12
  310. package/src/ui/dsh/management.ts +10 -10
  311. package/src/ui/dsh/styles.ts +5 -33
  312. package/lib/antigravity-plugin/lib/services/history/history-migrate.d.ts +0 -12
  313. package/lib/antigravity-plugin/lib/services/history/history-migrate.js +0 -88
  314. package/lib/ui/presentation.d.ts +0 -3
  315. package/lib/ui/presentation.js +0 -12
  316. package/src/tools/access.ts +0 -7
  317. package/src/tools/register.ts +0 -21
  318. package/src/ui/presentation.ts +0 -12
@@ -0,0 +1,144 @@
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
+
12
+ /** Check exact installed manifests, never resolve dependencies in an ancestor workspace. */
13
+ export async function minimumCliRelease(root: string, plugins: Record<string, string>): Promise<string | undefined> {
14
+ const versions = Object.values(plugins);
15
+ if (process.env.PBOT_PLUGIN_VERSION) 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: unknown = JSON.parse(await readFile(path, "utf8"));
20
+ requireThat(object(manifest) && manifest.name === "personal-bot" && typeof manifest.version === "string",
21
+ "无效的 personal-bot 安装清单:" + path);
22
+ versions.push(manifest.version);
23
+ } catch (error) {
24
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
25
+ }
26
+ }
27
+ let required: string | undefined;
28
+ for (const version of versions) {
29
+ requireThat(valid(version) && /^[0-9]/.test(version) && version === version.trim(), "无效的插件发布版本:" + version);
30
+ if (!required || gt(version, required)) required = version;
31
+ }
32
+ return required;
33
+ }
34
+
35
+ async function runProgram(command: string, args: string[], env: NodeJS.ProcessEnv, install = false, input?: string): Promise<number> {
36
+ const child = spawn(command, args, install
37
+ ? { cwd: homedir(), stdio: ["ignore", 2, 2], env }
38
+ : { stdio: input === undefined ? "inherit" : ["pipe", "inherit", "inherit"], env });
39
+ const interrupt = () => child.kill("SIGINT");
40
+ const terminate = () => child.kill("SIGTERM");
41
+ process.on("SIGINT", interrupt);
42
+ process.on("SIGTERM", terminate);
43
+ try {
44
+ return await new Promise<number>((resolveCode, reject) => {
45
+ child.once("error", reject);
46
+ child.once("close", (code, signal) => resolveCode(code ?? (signal === "SIGINT" ? 130 : 143)));
47
+ if (input !== undefined && child.stdin) {
48
+ child.stdin.on("error", (error: NodeJS.ErrnoException) => { if (error.code !== "EPIPE") reject(error); });
49
+ child.stdin.end(input);
50
+ }
51
+ });
52
+ } finally {
53
+ process.off("SIGINT", interrupt);
54
+ process.off("SIGTERM", terminate);
55
+ }
56
+ }
57
+
58
+ async function globalPackage(): Promise<string | undefined> {
59
+ const { stdout } = await promisify(execFile)("npm", ["root", "--global"], { cwd: homedir() });
60
+ const directory = join(stdout.trim(), "personal-bot");
61
+ try {
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(resolve(resourceRoot, ".."))) return directory;
64
+ } catch (error) {
65
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
66
+ }
67
+ return undefined;
68
+ }
69
+
70
+ async function upgradeGlobal(directory: string, required: string, env: NodeJS.ProcessEnv): Promise<number | undefined> {
71
+ const lib = dirname(dirname(directory));
72
+ const lock = join(lib, ".pbot-cli-update.lock");
73
+ try { await mkdir(lock); }
74
+ catch (error) {
75
+ // A read-only prefix or an active updater can still use npm's per-user versioned cache.
76
+ if (["EEXIST", "EACCES", "EPERM", "EROFS"].includes((error as NodeJS.ErrnoException).code ?? "")) return undefined;
77
+ throw error;
78
+ }
79
+ const installedVersion = async () => {
80
+ const installed: unknown = JSON.parse(await readFile(join(directory, "package.json"), "utf8"));
81
+ requireThat(object(installed) && installed.name === "personal-bot" && typeof installed.version === "string" && valid(installed.version),
82
+ "全局 CLI 安装清单无效:" + directory);
83
+ return installed.version;
84
+ };
85
+ try {
86
+ if (lt(await installedVersion(), required)) {
87
+ const code = await runProgram("npm", ["install", "--global", "--prefix", dirname(lib), "--ignore-scripts",
88
+ "--no-audit", "--no-fund", "--prefer-offline", "personal-bot@" + required], env, true);
89
+ if (code) return code;
90
+ requireThat(!lt(await installedVersion(), required), "npm 更新后的 CLI 低于插件所需版本:" + required);
91
+ }
92
+ return 0;
93
+ } finally {
94
+ await rmdir(lock);
95
+ }
96
+ }
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
+
126
+ /** Upgrade managed global installs in place. Project, npx and linked installs use npm's shared version cache. */
127
+ export async function runUpdatedCli(required: string | undefined, args: readonly string[], input?: string): Promise<number | undefined> {
128
+ if (!required) return undefined;
129
+ requireThat(valid(required) && /^[0-9]/.test(required) && required === required.trim(), "无效的插件发布版本:" + required);
130
+ if (!lt(packageVersion, required)) return undefined;
131
+ requireThat(process.env.PBOT_CLI_UPDATING !== required,
132
+ `npm 启动了 CLI ${packageVersion},工作区插件需要 ${required}。`);
133
+ console.error(`工作区需要 Personal Bot CLI ${required},正在更新 ${packageVersion}。`);
134
+ const env = { ...process.env, PBOT_CLI_UPDATING: required };
135
+ const global = await globalPackage();
136
+ if (global) {
137
+ const status = await upgradeGlobal(global, required, env);
138
+ if (status !== undefined) {
139
+ if (status !== 0) return status;
140
+ return runProgram(process.execPath, [join(global, "bin/pbot.mjs"), ...args], env, false, input);
141
+ }
142
+ }
143
+ return runProgram("npx", ["--yes", "--ignore-scripts", "--prefer-offline", "personal-bot@" + required, ...args], env, false, input);
144
+ }
@@ -2,7 +2,7 @@ import { readFileSync } from "node:fs";
2
2
  import { readFile, stat } from "node:fs/promises";
3
3
  import { join, resolve } from "node:path";
4
4
  import { fileURLToPath, pathToFileURL } from "node:url";
5
- import type { InstalledPackage } from "../../services/installation/setup-service.js";
5
+ import type { InstalledPackage } from "../../model/installation.js";
6
6
  import { npmName } from "../../common/package-source.js";
7
7
 
8
8
  type PiSdk = typeof import("@earendil-works/pi-coding-agent");
@@ -4,7 +4,7 @@ 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 type { InstalledPackage, PackageChoice, PiInstallation, SetupOperations, SystemCommand } from "../../services/installation/setup-service.js";
7
+ import type { InstalledPackage, PackageChoice, PiInstallation, SetupOperations, SystemCommand } from "../../model/installation.js";
8
8
  import { findExecutable, npmGlobalBinary, SetupProcesses } from "./processes.js";
9
9
 
10
10
  export const piPackageName = "@earendil-works/pi-coding-agent";
@@ -2,7 +2,7 @@ import { spawn } from "node:child_process";
2
2
  import { access, realpath, stat } from "node:fs/promises";
3
3
  import { constants } from "node:fs";
4
4
  import { delimiter, isAbsolute, join, resolve } from "node:path";
5
- import type { SystemCommand } from "../../services/installation/setup-service.js";
5
+ import type { SystemCommand } from "../../model/installation.js";
6
6
 
7
7
  export async function findExecutable(name: string, env: NodeJS.ProcessEnv = process.env): Promise<string | undefined> {
8
8
  const directories = (env.PATH ?? "").split(delimiter).filter(Boolean);
@@ -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
+ };
@@ -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 qoderUpgrader: HostUpgrader = {
18
+ host: "qoder",
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", "qoder-plugin");
31
+ const bundledVersion = await readManifestVersion(join(pluginDir, ".qoder-plugin", "plugin.json"));
32
+ if (!bundledVersion) {
33
+ return {
34
+ host: this.host,
35
+ state: "skipped",
36
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
37
+ detail: `缺少 Qoder CLI 插件构建产物:${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 qodercli = context.env.PBOT_QODER_COMMAND || await context.findExecutable("qodercli");
49
+ if (!qodercli) {
50
+ return {
51
+ host: this.host,
52
+ state: "skipped",
53
+ ...(context.currentVersion ? { previousVersion: context.currentVersion } : {}),
54
+ detail: "未找到 Qoder CLI(qodercli),已跳过。",
55
+ };
56
+ }
57
+ await context.run(qodercli, ["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: `已通过 qodercli plugin install 更新到 ${bundledVersion}。`,
65
+ nextStep: "重启 Qoder CLI 会话使新版本生效。",
66
+ };
67
+ },
68
+ };