personal-bot 0.3.1 → 0.3.3

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 (436) 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 +27 -11
  8. package/bin/pbot.mjs +22 -2
  9. package/docs/README.md +3 -1
  10. package/docs/architecture/design.md +37 -24
  11. package/docs/architecture/extensibility.md +27 -19
  12. package/docs/architecture/model-context.md +4 -3
  13. package/docs/architecture/principles.md +5 -4
  14. package/docs/guides/capabilities.md +4 -4
  15. package/docs/guides/cross-harness-workers.md +3 -1
  16. package/docs/guides/history.md +73 -16
  17. package/docs/guides/workspace-configuration.md +35 -6
  18. package/docs/hosts/antigravity.md +2 -0
  19. package/docs/hosts/claude-code.md +1 -1
  20. package/docs/hosts/codex-qoder.md +63 -0
  21. package/docs/hosts/dsh.md +1 -1
  22. package/docs/hosts/pi.md +1 -1
  23. package/docs/maintainers/architecture-governance.md +78 -0
  24. package/hooks/hooks.json +3 -0
  25. package/lib/antigravity-plugin/DISCLAIMER.md +17 -0
  26. package/lib/antigravity-plugin/LICENSE +21 -0
  27. package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +36 -0
  28. package/lib/antigravity-plugin/hooks.json +7 -0
  29. package/lib/antigravity-plugin/lib/claude.js +2 -2
  30. package/lib/antigravity-plugin/lib/common/files.d.ts +2 -0
  31. package/lib/antigravity-plugin/lib/common/files.js +44 -0
  32. package/lib/antigravity-plugin/lib/common/validation.d.ts +4 -0
  33. package/lib/antigravity-plugin/lib/common/validation.js +8 -0
  34. package/lib/{extension/acp-member.d.ts → antigravity-plugin/lib/extension/acp/member.d.ts} +5 -5
  35. package/lib/{extension/acp-member.js → antigravity-plugin/lib/extension/acp/member.js} +6 -6
  36. package/lib/antigravity-plugin/lib/extension/acp-worker.js +222 -51
  37. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
  38. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.js +22 -0
  39. package/lib/{extension → antigravity-plugin/lib/extension/hosts}/dsh/acp-member.js +11 -25
  40. package/lib/{extension → antigravity-plugin/lib/extension/hosts}/dsh/files.js +3 -3
  41. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.d.ts +3 -0
  42. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +100 -0
  43. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.d.ts +12 -0
  44. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +101 -0
  45. package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.d.ts +6 -0
  46. package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.js +21 -0
  47. package/lib/antigravity-plugin/lib/extension/mcp.js +2705 -1348
  48. package/lib/antigravity-plugin/lib/extension/workspace-cli.js +20127 -0
  49. package/lib/antigravity-plugin/lib/integrations/agent-resources.d.ts +1 -0
  50. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +6 -2
  51. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity/adapter.d.ts +6 -0
  52. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity/adapter.js +171 -0
  53. package/lib/antigravity-plugin/lib/integrations/hosts/{claude.d.ts → claude/adapter.d.ts} +1 -1
  54. package/lib/antigravity-plugin/lib/integrations/hosts/claude/adapter.js +36 -0
  55. package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.d.ts +2 -0
  56. package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.js +14 -0
  57. package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/mcp.d.ts +1 -1
  58. package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/resources.d.ts +1 -1
  59. package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/resources.js +4 -4
  60. package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/skills.js +1 -1
  61. package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
  62. package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.js +32 -0
  63. package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.d.ts +4 -0
  64. package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.js +23 -0
  65. package/lib/antigravity-plugin/lib/integrations/hosts/{registry.d.ts → plugin-registry.d.ts} +1 -0
  66. package/lib/{integrations/hosts/registry.js → antigravity-plugin/lib/integrations/hosts/plugin-registry.js} +10 -3
  67. package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
  68. package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.js +76 -0
  69. package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
  70. package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.js +16 -0
  71. package/lib/antigravity-plugin/lib/model/acp.d.ts +7 -3
  72. package/lib/antigravity-plugin/lib/model/acp.js +8 -0
  73. package/lib/antigravity-plugin/lib/model/agent.d.ts +121 -0
  74. package/lib/antigravity-plugin/lib/model/agent.js +4 -0
  75. package/lib/antigravity-plugin/lib/model/history.d.ts +179 -0
  76. package/lib/antigravity-plugin/lib/model/history.js +4 -0
  77. package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +31 -0
  78. package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +28 -7
  79. package/lib/antigravity-plugin/lib/model/workspace-config.js +2 -1
  80. package/lib/antigravity-plugin/lib/plugin.d.ts +2 -1
  81. package/lib/antigravity-plugin/lib/plugin.js +4 -4
  82. package/lib/antigravity-plugin/lib/services/history/history-capture.d.ts +4 -0
  83. package/lib/antigravity-plugin/lib/services/history/history-capture.js +98 -0
  84. package/lib/antigravity-plugin/lib/services/history/history-index.d.ts +17 -0
  85. package/lib/antigravity-plugin/lib/services/history/history-index.js +112 -0
  86. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/monthly.d.ts +7 -0
  87. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/monthly.js +121 -0
  88. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/session.d.ts +7 -0
  89. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/session.js +124 -0
  90. package/lib/antigravity-plugin/lib/services/history/summary-registry.d.ts +7 -0
  91. package/lib/antigravity-plugin/lib/services/history/summary-registry.js +35 -0
  92. package/lib/antigravity-plugin/lib/services/history/summary-runner.d.ts +34 -0
  93. package/lib/antigravity-plugin/lib/services/history/summary-runner.js +299 -0
  94. package/lib/antigravity-plugin/lib/services/history/workspace-history.d.ts +3 -0
  95. package/lib/antigravity-plugin/lib/services/history/workspace-history.js +6 -0
  96. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +3 -0
  97. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +78 -0
  98. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.d.ts +5 -1
  99. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +77 -7
  100. package/lib/antigravity-plugin/lib/services/workspace/workspace-files.d.ts +2 -0
  101. package/lib/antigravity-plugin/lib/services/workspace/workspace-files.js +17 -0
  102. package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.d.ts +14 -0
  103. package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.js +193 -0
  104. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.d.ts +1 -0
  105. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.js +4 -2
  106. package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +11 -0
  107. package/lib/antigravity-plugin/lib/storage/acp-files.js +75 -0
  108. package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +31 -0
  109. package/lib/antigravity-plugin/lib/storage/agent-files.js +159 -0
  110. package/lib/antigravity-plugin/lib/storage/atomic-json.d.ts +3 -1
  111. package/lib/antigravity-plugin/lib/storage/atomic-json.js +43 -3
  112. package/lib/antigravity-plugin/lib/storage/file-access.d.ts +2 -0
  113. package/lib/antigravity-plugin/lib/storage/file-access.js +26 -0
  114. package/lib/antigravity-plugin/lib/storage/history-files.d.ts +55 -0
  115. package/lib/antigravity-plugin/lib/storage/history-files.js +473 -0
  116. package/lib/antigravity-plugin/lib/storage/history-format.d.ts +5 -0
  117. package/lib/antigravity-plugin/lib/storage/history-format.js +60 -0
  118. package/lib/antigravity-plugin/package.json +1 -1
  119. package/lib/antigravity-plugin/plugin.json +1 -1
  120. package/lib/antigravity-plugin/resources/prompts/history-monthly-summary.md +13 -0
  121. package/lib/antigravity-plugin/resources/prompts/history-summary.md +13 -0
  122. package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -0
  123. package/lib/antigravity-plugin/resources/skills/.claude-plugin/plugin.json +4 -0
  124. package/lib/antigravity-plugin/resources/skills/project-maintenance/SKILL.md +35 -0
  125. package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +52 -0
  126. package/lib/antigravity-plugin/skills/pbot/SKILL.md +11 -1
  127. package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +22 -12
  128. package/lib/claude.js +2 -2
  129. package/lib/client.js +42 -60
  130. package/lib/codex-marketplace/.agents/plugins/marketplace.json +17 -0
  131. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +7 -0
  132. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +17 -0
  133. package/lib/codex-marketplace/plugins/personal-bot/DISCLAIMER.md +17 -0
  134. package/lib/codex-marketplace/plugins/personal-bot/LICENSE +21 -0
  135. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +37 -0
  136. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +9 -0
  137. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/agents/openai.yaml +2 -0
  138. package/lib/common/validation.d.ts +4 -0
  139. package/lib/common/validation.js +8 -0
  140. package/lib/dsh.js +6 -6
  141. package/lib/extension/acp/member.d.ts +18 -0
  142. package/lib/extension/acp/member.js +44 -0
  143. package/lib/extension/acp/worker.js +384 -0
  144. package/lib/extension/acp-worker.js +222 -51
  145. package/lib/extension/history/command.d.ts +37 -0
  146. package/lib/extension/history/command.js +57 -0
  147. package/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
  148. package/lib/extension/hosts/dsh/acp-entry.js +22 -0
  149. package/lib/extension/hosts/dsh/acp-member.d.ts +4 -0
  150. package/lib/extension/hosts/dsh/acp-member.js +58 -0
  151. package/lib/extension/{dsh → hosts/dsh}/command.d.ts +1 -2
  152. package/lib/extension/{dsh → hosts/dsh}/command.js +9 -11
  153. package/lib/extension/hosts/dsh/files.d.ts +5 -0
  154. package/lib/extension/hosts/dsh/files.js +67 -0
  155. package/lib/extension/hosts/dsh/history.d.ts +4 -0
  156. package/lib/extension/hosts/dsh/history.js +62 -0
  157. package/lib/extension/{dsh → hosts/dsh}/management.d.ts +1 -1
  158. package/lib/extension/{dsh → hosts/dsh}/management.js +12 -10
  159. package/lib/extension/{dsh → hosts/dsh}/workspace.d.ts +3 -9
  160. package/lib/extension/{dsh → hosts/dsh}/workspace.js +30 -99
  161. package/lib/extension/hosts/pi/acp-member.d.ts +3 -0
  162. package/lib/extension/hosts/pi/acp-member.js +100 -0
  163. package/lib/extension/hosts/plugin-runtime.d.ts +12 -0
  164. package/lib/extension/hosts/plugin-runtime.js +101 -0
  165. package/lib/extension/hosts/workspace-mcp.d.ts +6 -0
  166. package/lib/extension/hosts/workspace-mcp.js +21 -0
  167. package/lib/extension/{mcp-bridge.d.ts → mcp/bridge.d.ts} +7 -9
  168. package/lib/extension/{mcp-bridge.js → mcp/bridge.js} +70 -88
  169. package/lib/extension/{mcp-workers.d.ts → mcp/tool-contract.d.ts} +129 -374
  170. package/lib/extension/mcp/tool-contract.js +441 -0
  171. package/lib/extension/mcp/workers.d.ts +487 -0
  172. package/lib/extension/mcp/workers.js +598 -0
  173. package/lib/extension/mcp.js +2705 -1348
  174. package/lib/extension/{setup-cli.js → setup/cli.js} +12 -9
  175. package/lib/extension/workspace-cli.js +20127 -0
  176. package/lib/integrations/acp/agents.d.ts +2 -3
  177. package/lib/integrations/acp/connection.d.ts +11 -10
  178. package/lib/integrations/acp/connection.js +6 -0
  179. package/lib/integrations/acp/harnesses/claude.js +1 -1
  180. package/lib/integrations/acp/harnesses/dsh.js +1 -1
  181. package/lib/integrations/acp/harnesses/presets.js +8 -0
  182. package/lib/integrations/acp/history-summary.d.ts +17 -0
  183. package/lib/integrations/acp/history-summary.js +101 -0
  184. package/lib/integrations/acp/launch.d.ts +2 -2
  185. package/lib/integrations/agent-resources.d.ts +1 -0
  186. package/lib/integrations/agent-resources.js +6 -2
  187. package/lib/integrations/hosts/antigravity/adapter.d.ts +6 -0
  188. package/lib/integrations/hosts/antigravity/adapter.js +171 -0
  189. package/lib/integrations/hosts/{claude.d.ts → claude/adapter.d.ts} +1 -1
  190. package/lib/integrations/hosts/claude/adapter.js +36 -0
  191. package/lib/integrations/hosts/codex/adapter.d.ts +2 -0
  192. package/lib/integrations/hosts/codex/adapter.js +14 -0
  193. package/lib/integrations/hosts/dsh/mcp.d.ts +4 -0
  194. package/lib/integrations/hosts/dsh/mcp.js +28 -0
  195. package/lib/integrations/hosts/dsh/messages.d.ts +36 -0
  196. package/lib/integrations/hosts/dsh/messages.js +11 -0
  197. package/lib/integrations/hosts/dsh/resources.d.ts +6 -0
  198. package/lib/integrations/hosts/dsh/resources.js +47 -0
  199. package/lib/integrations/hosts/dsh/root-agents.d.ts +4 -0
  200. package/lib/integrations/hosts/dsh/root-agents.js +3 -0
  201. package/lib/integrations/{dsh → hosts/dsh}/schema.js +1 -1
  202. package/lib/integrations/hosts/dsh/skills.d.ts +8 -0
  203. package/lib/integrations/hosts/dsh/skills.js +31 -0
  204. package/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
  205. package/lib/integrations/hosts/pi/agent-tools.js +32 -0
  206. package/lib/integrations/hosts/pi/mcp.d.ts +4 -0
  207. package/lib/integrations/hosts/pi/mcp.js +23 -0
  208. package/lib/integrations/hosts/{registry.d.ts → plugin-registry.d.ts} +1 -0
  209. package/lib/{antigravity-plugin/lib/integrations/hosts/registry.js → integrations/hosts/plugin-registry.js} +10 -3
  210. package/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
  211. package/lib/integrations/hosts/prompt-hooks.js +76 -0
  212. package/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
  213. package/lib/integrations/hosts/qoder/adapter.js +16 -0
  214. package/lib/integrations/installation/pi-setup.js +1 -1
  215. package/lib/model/acp.d.ts +7 -3
  216. package/lib/model/acp.js +8 -0
  217. package/lib/model/agent.d.ts +18 -4
  218. package/lib/model/agent.js +4 -1
  219. package/lib/model/history.d.ts +143 -34
  220. package/lib/model/history.js +4 -1
  221. package/lib/model/installation.js +1 -0
  222. package/lib/model/plugin-host.d.ts +31 -0
  223. package/lib/model/workspace-config.d.ts +28 -7
  224. package/lib/model/workspace-config.js +2 -1
  225. package/lib/plugin.d.ts +2 -1
  226. package/lib/plugin.js +4 -4
  227. package/lib/qoder-plugin/.mcp.json +17 -0
  228. package/lib/qoder-plugin/.qoder-plugin/plugin.json +7 -0
  229. package/lib/qoder-plugin/DISCLAIMER.md +17 -0
  230. package/lib/qoder-plugin/LICENSE +21 -0
  231. package/lib/qoder-plugin/hooks/hooks.json +37 -0
  232. package/lib/qoder-plugin/skills/pbot/SKILL.md +9 -0
  233. package/lib/services/agents/agent-run.d.ts +1 -1
  234. package/lib/services/agents/agent-service.d.ts +15 -45
  235. package/lib/services/agents/agent-service.js +22 -3
  236. package/lib/services/history/history-capture.d.ts +4 -0
  237. package/lib/services/history/history-capture.js +98 -0
  238. package/lib/services/history/history-index.d.ts +16 -7
  239. package/lib/services/history/history-index.js +48 -70
  240. package/lib/services/history/history-migrate.d.ts +12 -0
  241. package/lib/services/history/history-migrate.js +54 -0
  242. package/lib/services/history/history-query.d.ts +51 -0
  243. package/lib/services/history/history-query.js +352 -0
  244. package/lib/services/history/summary-command.d.ts +20 -0
  245. package/lib/services/history/summary-command.js +69 -0
  246. package/lib/services/history/summary-dimensions/monthly.d.ts +7 -0
  247. package/lib/services/history/summary-dimensions/monthly.js +121 -0
  248. package/lib/services/history/summary-dimensions/session.d.ts +7 -0
  249. package/lib/services/history/summary-dimensions/session.js +124 -0
  250. package/lib/services/history/summary-registry.d.ts +7 -0
  251. package/lib/services/history/summary-registry.js +35 -0
  252. package/lib/services/history/summary-runner.d.ts +34 -0
  253. package/lib/services/history/summary-runner.js +299 -0
  254. package/lib/services/history/workspace-history.d.ts +3 -0
  255. package/lib/services/history/workspace-history.js +6 -0
  256. package/lib/services/workspace/plugin-releases.d.ts +3 -0
  257. package/lib/services/workspace/plugin-releases.js +78 -0
  258. package/lib/services/workspace/workspace-config.d.ts +5 -1
  259. package/lib/services/workspace/workspace-config.js +77 -7
  260. package/lib/services/workspace/workspace-mcp.js +22 -29
  261. package/lib/services/workspace/workspace-skills.d.ts +1 -0
  262. package/lib/services/workspace/workspace-skills.js +4 -2
  263. package/lib/storage/acp-files.js +7 -12
  264. package/lib/storage/agent-files.js +6 -5
  265. package/lib/storage/atomic-json.d.ts +3 -1
  266. package/lib/storage/atomic-json.js +43 -3
  267. package/lib/storage/history-files.d.ts +46 -9
  268. package/lib/storage/history-files.js +382 -250
  269. package/lib/storage/history-format.d.ts +3 -13
  270. package/lib/storage/history-format.js +26 -52
  271. package/lib/tools/history/actions.d.ts +3 -68
  272. package/lib/tools/history/actions.js +7 -5
  273. package/lib/tools/history/contract.d.ts +16 -17
  274. package/lib/tools/history/contract.js +63 -22
  275. package/lib/tools/history/dsh-tool.d.ts +1 -2
  276. package/lib/tools/history/dsh-tool.js +11 -5
  277. package/lib/ui/dsh/elements.d.ts +2 -5
  278. package/lib/ui/dsh/elements.js +3 -16
  279. package/lib/ui/dsh/management.js +26 -13
  280. package/lib/ui/dsh/styles.d.ts +1 -1
  281. package/lib/ui/dsh/styles.js +5 -33
  282. package/package.json +4 -1
  283. package/resources/antigravity/skills/pbot/SKILL.md +11 -1
  284. package/resources/claude/skills/pbot/SKILL.md +11 -1
  285. package/resources/plugin/skills/pbot/SKILL.md +9 -0
  286. package/resources/prompts/history-coordinator.md +2 -1
  287. package/resources/prompts/history-monthly-summary.md +13 -0
  288. package/resources/prompts/history-summary.md +10 -4
  289. package/resources/prompts/plugin-coordinator.md +1 -0
  290. package/resources/skills/provider-connections/SKILL.md +22 -12
  291. package/scripts/README.md +3 -2
  292. package/src/claude.ts +2 -2
  293. package/src/common/validation.ts +10 -0
  294. package/src/dsh.ts +6 -6
  295. package/src/extension/{acp-member.ts → acp/member.ts} +7 -7
  296. package/src/extension/{acp-worker.ts → acp/worker.ts} +131 -17
  297. package/src/extension/history/command.ts +68 -0
  298. package/src/extension/hosts/dsh/acp-entry.ts +21 -0
  299. package/src/extension/{dsh → hosts/dsh}/acp-member.ts +12 -23
  300. package/src/extension/{dsh → hosts/dsh}/command.ts +10 -12
  301. package/src/extension/{dsh → hosts/dsh}/files.ts +3 -3
  302. package/src/extension/hosts/dsh/history.ts +67 -0
  303. package/src/extension/{dsh → hosts/dsh}/management.ts +11 -9
  304. package/src/extension/{dsh → hosts/dsh}/workspace.ts +31 -76
  305. package/src/extension/{pi-acp-member.ts → hosts/pi/acp-member.ts} +12 -18
  306. package/src/extension/{command.ts → hosts/pi/command.ts} +44 -11
  307. package/src/extension/{events.ts → hosts/pi/events.ts} +8 -4
  308. package/src/extension/hosts/pi/history.ts +45 -0
  309. package/src/extension/{lifecycle.ts → hosts/pi/lifecycle.ts} +9 -12
  310. package/src/extension/hosts/plugin-runtime.ts +96 -0
  311. package/src/extension/hosts/workspace-mcp.ts +25 -0
  312. package/src/extension/{mcp-bridge.ts → mcp/bridge.ts} +85 -85
  313. package/src/extension/mcp/tool-contract.ts +462 -0
  314. package/src/extension/{mcp-workers.ts → mcp/workers.ts} +151 -448
  315. package/src/extension/{setup-cli.ts → setup/cli.ts} +14 -11
  316. package/src/extension/setup/workspace-cli.ts +159 -0
  317. package/src/integrations/acp/agents.ts +2 -3
  318. package/src/integrations/acp/connection.ts +6 -0
  319. package/src/integrations/acp/harnesses/claude.ts +1 -1
  320. package/src/integrations/acp/harnesses/dsh.ts +1 -1
  321. package/src/integrations/acp/harnesses/presets.ts +8 -0
  322. package/src/integrations/acp/history-summary.ts +109 -0
  323. package/src/integrations/agent-resources.ts +7 -1
  324. package/src/integrations/hosts/antigravity/adapter.ts +161 -0
  325. package/src/integrations/hosts/claude/adapter.ts +39 -0
  326. package/src/integrations/hosts/codex/adapter.ts +16 -0
  327. package/src/integrations/{dsh → hosts/dsh}/mcp.ts +1 -1
  328. package/src/integrations/{dsh → hosts/dsh}/resources.ts +5 -5
  329. package/src/integrations/{dsh → hosts/dsh}/schema.ts +1 -1
  330. package/src/integrations/{dsh → hosts/dsh}/session.ts +2 -2
  331. package/src/integrations/{dsh → hosts/dsh}/skills.ts +1 -1
  332. package/src/integrations/{pi → hosts/pi}/mcp.ts +1 -1
  333. package/src/integrations/{pi → hosts/pi}/skills.ts +1 -1
  334. package/src/integrations/hosts/{registry.ts → plugin-registry.ts} +11 -3
  335. package/src/integrations/hosts/prompt-hooks.ts +72 -0
  336. package/src/integrations/hosts/qoder/adapter.ts +19 -0
  337. package/src/integrations/installation/cli-update.ts +116 -0
  338. package/src/integrations/installation/pi-bridge.ts +1 -1
  339. package/src/integrations/installation/pi-setup.ts +2 -2
  340. package/src/integrations/installation/processes.ts +1 -1
  341. package/src/mcp.ts +1 -1
  342. package/src/model/acp.ts +11 -3
  343. package/src/model/agent.ts +20 -4
  344. package/src/model/history.ts +156 -26
  345. package/src/model/installation.ts +41 -0
  346. package/src/model/plugin-host.ts +30 -2
  347. package/src/model/workspace-config.ts +31 -7
  348. package/src/pbot.ts +22 -50
  349. package/src/plugin.ts +4 -4
  350. package/src/services/agents/agent-service.ts +29 -17
  351. package/src/services/history/history-capture.ts +91 -0
  352. package/src/services/history/history-index.ts +48 -62
  353. package/src/services/history/history-migrate.ts +62 -0
  354. package/src/services/history/history-query.ts +401 -0
  355. package/src/services/history/summary-command.ts +70 -0
  356. package/src/services/history/summary-dimensions/monthly.ts +139 -0
  357. package/src/services/history/summary-dimensions/session.ts +149 -0
  358. package/src/services/history/summary-registry.ts +43 -0
  359. package/src/services/history/summary-runner.ts +343 -0
  360. package/src/services/history/workspace-history.ts +8 -0
  361. package/src/services/installation/setup-service.ts +2 -41
  362. package/src/services/workspace/plugin-releases.ts +68 -0
  363. package/src/services/workspace/workspace-config.ts +95 -8
  364. package/src/services/workspace/workspace-mcp.ts +23 -28
  365. package/src/services/workspace/workspace-skills.ts +5 -2
  366. package/src/storage/acp-files.ts +7 -12
  367. package/src/storage/agent-files.ts +6 -5
  368. package/src/storage/atomic-json.ts +31 -4
  369. package/src/storage/history-files.ts +416 -189
  370. package/src/storage/history-format.ts +45 -61
  371. package/src/tools/history/actions.ts +8 -6
  372. package/src/tools/history/contract.ts +70 -25
  373. package/src/tools/history/dsh-tool.ts +17 -8
  374. package/src/tools/history/pi-tool.ts +23 -0
  375. package/src/ui/dsh/elements.ts +5 -12
  376. package/src/ui/dsh/management.ts +36 -20
  377. package/src/ui/dsh/styles.ts +5 -33
  378. package/src/ui/{confirmation.ts → pi/confirmation.ts} +1 -1
  379. package/lib/antigravity-plugin/lib/extension/plugin-runtime.d.ts +0 -10
  380. package/lib/antigravity-plugin/lib/extension/plugin-runtime.js +0 -67
  381. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.d.ts +0 -4
  382. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.js +0 -79
  383. package/lib/antigravity-plugin/lib/integrations/hosts/claude.js +0 -60
  384. package/lib/extension/dsh/history.d.ts +0 -15
  385. package/lib/extension/dsh/history.js +0 -87
  386. package/lib/extension/mcp-workers.js +0 -882
  387. package/lib/extension/plugin-runtime.d.ts +0 -10
  388. package/lib/extension/plugin-runtime.js +0 -67
  389. package/lib/integrations/dsh/history-summary.d.ts +0 -11
  390. package/lib/integrations/dsh/history-summary.js +0 -36
  391. package/lib/integrations/dsh/history.d.ts +0 -16
  392. package/lib/integrations/dsh/history.js +0 -167
  393. package/lib/integrations/history-summary.d.ts +0 -4
  394. package/lib/integrations/history-summary.js +0 -32
  395. package/lib/integrations/hosts/antigravity.d.ts +0 -4
  396. package/lib/integrations/hosts/antigravity.js +0 -79
  397. package/lib/integrations/hosts/claude.js +0 -60
  398. package/lib/services/history/history-service.d.ts +0 -106
  399. package/lib/services/history/history-service.js +0 -242
  400. package/lib/storage/acp-history.d.ts +0 -10
  401. package/lib/storage/acp-history.js +0 -57
  402. package/lib/ui/presentation.d.ts +0 -3
  403. package/lib/ui/presentation.js +0 -12
  404. package/src/extension/dsh/history.ts +0 -74
  405. package/src/extension/history.ts +0 -26
  406. package/src/extension/plugin-runtime.ts +0 -63
  407. package/src/integrations/dsh/history-summary.ts +0 -39
  408. package/src/integrations/dsh/history.ts +0 -84
  409. package/src/integrations/history-summary.ts +0 -30
  410. package/src/integrations/hosts/antigravity.ts +0 -83
  411. package/src/integrations/hosts/claude.ts +0 -62
  412. package/src/integrations/pi/history.ts +0 -99
  413. package/src/services/history/history-service.ts +0 -195
  414. package/src/storage/acp-history.ts +0 -49
  415. package/src/tools/access.ts +0 -7
  416. package/src/tools/history/tool.ts +0 -19
  417. package/src/tools/register.ts +0 -22
  418. package/src/ui/presentation.ts +0 -12
  419. /package/lib/{extension → antigravity-plugin/lib/extension/hosts}/dsh/acp-member.d.ts +0 -0
  420. /package/lib/{extension → antigravity-plugin/lib/extension/hosts}/dsh/files.d.ts +0 -0
  421. /package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/mcp.js +0 -0
  422. /package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/messages.d.ts +0 -0
  423. /package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/messages.js +0 -0
  424. /package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/root-agents.d.ts +0 -0
  425. /package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/root-agents.js +0 -0
  426. /package/lib/{integrations → antigravity-plugin/lib/integrations/hosts}/dsh/skills.d.ts +0 -0
  427. /package/lib/extension/{acp-worker.d.ts → acp/worker.d.ts} +0 -0
  428. /package/lib/extension/{setup-guard.js → setup/guard.js} +0 -0
  429. /package/lib/integrations/acp/{claude.d.ts → claude-settings.d.ts} +0 -0
  430. /package/lib/integrations/acp/{claude.js → claude-settings.js} +0 -0
  431. /package/lib/integrations/{dsh → hosts/dsh}/schema.d.ts +0 -0
  432. /package/src/extension/{setup-guard.ts → setup/guard.ts} +0 -0
  433. /package/src/integrations/acp/{claude.ts → claude-settings.ts} +0 -0
  434. /package/src/integrations/{dsh → hosts/dsh}/messages.ts +0 -0
  435. /package/src/integrations/{dsh → hosts/dsh}/root-agents.ts +0 -0
  436. /package/src/integrations/{pi → hosts/pi}/agent-tools.ts +0 -0
@@ -0,0 +1,193 @@
1
+ import { mkdir, readFile } from "node:fs/promises";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { object } from "../../common/json.js";
4
+ import { requireThat } from "../../common/validation.js";
5
+ import { updateJsonFile } from "../../storage/atomic-json.js";
6
+ const serverNamePattern = /^[A-Za-z0-9][A-Za-z0-9_-]{0,31}$/;
7
+ const reservedServerNames = new Set(["pbot"]);
8
+ const stdioKeys = new Set(["command", "args", "env", "cwd", "description", "timeout", "enabled"]);
9
+ const httpKeys = new Set(["url", "headers", "description", "timeout", "enabled"]);
10
+ const defaultTimeoutSeconds = 60;
11
+ const envPattern = /\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
12
+ export function isWorkspaceMcpServerName(name) {
13
+ return serverNamePattern.test(name) && !reservedServerNames.has(name.toLowerCase());
14
+ }
15
+ export function isWorkspaceMcpHttpServer(server) {
16
+ return "url" in server && typeof server.url === "string";
17
+ }
18
+ function validateStringRecord(value, label) {
19
+ requireThat(object(value), `${label} 必须是字符串映射。`);
20
+ return Object.fromEntries(Object.entries(value).map(([key, entry]) => {
21
+ requireThat(key.length > 0 && !key.includes("\0") && typeof entry === "string" && !entry.includes("\0"), `${label}.${key} 必须是非空键与无空字节字符串值。`);
22
+ return [key, entry];
23
+ }));
24
+ }
25
+ export function validateWorkspaceMcpServer(name, value) {
26
+ requireThat(serverNamePattern.test(name), `MCP 服务名无效:${name}(需匹配 [A-Za-z0-9][A-Za-z0-9_-]{0,31})。`);
27
+ requireThat(!reservedServerNames.has(name.toLowerCase()), `MCP 服务名 "${name}" 为 Personal Bot 保留名称。`);
28
+ requireThat(object(value), `MCP 服务 "${name}" 配置必须是 JSON 对象。`);
29
+ const hasCommand = value.command !== undefined;
30
+ const hasUrl = value.url !== undefined;
31
+ requireThat(hasCommand !== hasUrl, `MCP 服务 "${name}" 必须且只能声明 command(stdio)或 url(Streamable HTTP)之一。`);
32
+ const allowed = hasCommand ? stdioKeys : httpKeys;
33
+ const unknown = Object.keys(value).filter(key => !allowed.has(key));
34
+ requireThat(unknown.length === 0, `MCP 服务 "${name}" 包含未知字段:${unknown.join(", ")}。`);
35
+ if (value.description !== undefined) {
36
+ requireThat(typeof value.description === "string" && !value.description.includes("\0"), `MCP 服务 "${name}" 的 description 必须是字符串。`);
37
+ }
38
+ if (value.timeout !== undefined) {
39
+ requireThat(typeof value.timeout === "number" && Number.isInteger(value.timeout) && value.timeout >= 1 && value.timeout <= 3600, `MCP 服务 "${name}" 的 timeout 必须是 1–3600 的整数秒。`);
40
+ }
41
+ if (value.enabled !== undefined) {
42
+ requireThat(typeof value.enabled === "boolean", `MCP 服务 "${name}" 的 enabled 必须是布尔值。`);
43
+ }
44
+ const base = {
45
+ ...(value.description !== undefined ? { description: value.description } : {}),
46
+ ...(value.timeout !== undefined ? { timeout: value.timeout } : {}),
47
+ ...(value.enabled !== undefined ? { enabled: value.enabled } : {}),
48
+ };
49
+ if (hasCommand) {
50
+ requireThat(typeof value.command === "string" && value.command.trim().length > 0 && !value.command.includes("\0"), `MCP 服务 "${name}" 的 command 必须是非空字符串。`);
51
+ let args;
52
+ if (value.args !== undefined) {
53
+ requireThat(Array.isArray(value.args) && value.args.every(item => typeof item === "string" && !item.includes("\0")), `MCP 服务 "${name}" 的 args 必须是字符串数组。`);
54
+ args = [...value.args];
55
+ }
56
+ const env = value.env !== undefined ? validateStringRecord(value.env, `MCP 服务 "${name}" 的 env`) : undefined;
57
+ let cwd;
58
+ if (value.cwd !== undefined) {
59
+ requireThat(typeof value.cwd === "string" && value.cwd.trim().length > 0 && !value.cwd.includes("\0"), `MCP 服务 "${name}" 的 cwd 必须是非空字符串。`);
60
+ cwd = value.cwd;
61
+ }
62
+ const entry = {
63
+ command: value.command,
64
+ ...(args ? { args } : {}),
65
+ ...(env ? { env } : {}),
66
+ ...(cwd ? { cwd } : {}),
67
+ ...base,
68
+ };
69
+ return entry;
70
+ }
71
+ requireThat(typeof value.url === "string" && value.url.trim().length > 0 && !value.url.includes("\0"), `MCP 服务 "${name}" 的 url 必须是非空字符串。`);
72
+ const rawUrl = value.url.trim();
73
+ requireThat(/^https?:\/\/.+/.test(rawUrl) || envPattern.test(rawUrl), `MCP 服务 "${name}" 的 url 必须以 http:// 或 https:// 开头。`);
74
+ envPattern.lastIndex = 0;
75
+ const headers = value.headers !== undefined ? validateStringRecord(value.headers, `MCP 服务 "${name}" 的 headers`) : undefined;
76
+ const entry = {
77
+ url: rawUrl,
78
+ ...(headers ? { headers } : {}),
79
+ ...base,
80
+ };
81
+ return entry;
82
+ }
83
+ function validateMcpFile(value) {
84
+ requireThat(object(value), "工作区 MCP 配置必须是 JSON 对象。");
85
+ const rawServers = value.mcpServers;
86
+ requireThat(rawServers !== undefined && object(rawServers), "工作区 MCP 配置需要 mcpServers 对象。");
87
+ const servers = {};
88
+ for (const [name, entry] of Object.entries(rawServers)) {
89
+ servers[name] = validateWorkspaceMcpServer(name, entry);
90
+ }
91
+ return { ...value, mcpServers: servers };
92
+ }
93
+ export function workspaceMcpPath(root) {
94
+ return join(root, ".pbot", "mcp.json");
95
+ }
96
+ export async function readWorkspaceMcpConfig(root) {
97
+ const path = workspaceMcpPath(root);
98
+ let content;
99
+ try {
100
+ content = await readFile(path, "utf8");
101
+ }
102
+ catch (error) {
103
+ if (error.code === "ENOENT")
104
+ return { path, present: false, servers: {} };
105
+ return { path, present: true, servers: {}, error: `无法读取工作区 MCP 配置 ${path}: ${String(error)}` };
106
+ }
107
+ try {
108
+ const parsed = validateMcpFile(JSON.parse(content));
109
+ return { path, present: true, servers: parsed.mcpServers };
110
+ }
111
+ catch (error) {
112
+ return { path, present: true, servers: {}, error: `工作区 MCP 配置无效 ${path}: ${error instanceof Error ? error.message : String(error)}` };
113
+ }
114
+ }
115
+ export function expandEnvTemplate(value, env, context) {
116
+ return value.replace(envPattern, (_match, variable) => {
117
+ const resolved = env[variable];
118
+ requireThat(resolved !== undefined, `${context} 引用的环境变量 ${variable} 未设置。`);
119
+ return resolved;
120
+ });
121
+ }
122
+ function expandEnvRecord(values, env, context) {
123
+ return Object.fromEntries(Object.entries(values ?? {}).map(([key, value]) => [key, expandEnvTemplate(value, env, `${context}.${key}`)]));
124
+ }
125
+ export function resolveWorkspaceMcpServer(root, name, server, env = process.env) {
126
+ const timeout = server.timeout ?? defaultTimeoutSeconds;
127
+ if (isWorkspaceMcpHttpServer(server)) {
128
+ const url = expandEnvTemplate(server.url, env, `MCP 服务 "${name}" 的 url`);
129
+ requireThat(/^https?:\/\/.+/.test(url), `MCP 服务 "${name}" 展开后的 url 必须以 http:// 或 https:// 开头。`);
130
+ return {
131
+ transport: "http",
132
+ name,
133
+ url,
134
+ headers: expandEnvRecord(server.headers, env, `MCP 服务 "${name}" 的 headers`),
135
+ ...(server.description ? { description: server.description } : {}),
136
+ timeout,
137
+ };
138
+ }
139
+ const command = expandEnvTemplate(server.command, env, `MCP 服务 "${name}" 的 command`);
140
+ const args = (server.args ?? []).map((arg, index) => expandEnvTemplate(arg, env, `MCP 服务 "${name}" 的 args[${index}]`));
141
+ const cwd = server.cwd ? resolve(root, expandEnvTemplate(server.cwd, env, `MCP 服务 "${name}" 的 cwd`)) : root;
142
+ return {
143
+ transport: "stdio",
144
+ name,
145
+ command,
146
+ args,
147
+ env: expandEnvRecord(server.env, env, `MCP 服务 "${name}" 的 env`),
148
+ cwd,
149
+ ...(server.description ? { description: server.description } : {}),
150
+ timeout,
151
+ };
152
+ }
153
+ export function resolveWorkspaceMcpServers(root, configuration, env = process.env) {
154
+ if (configuration.error)
155
+ throw new Error(configuration.error);
156
+ const resolved = [];
157
+ for (const [name, server] of Object.entries(configuration.servers)) {
158
+ if (server.enabled === false)
159
+ continue;
160
+ resolved.push(resolveWorkspaceMcpServer(root, name, server, env));
161
+ }
162
+ return resolved;
163
+ }
164
+ export async function upsertWorkspaceMcpServer(root, name, server) {
165
+ const validated = validateWorkspaceMcpServer(name, server);
166
+ const current = await readWorkspaceMcpConfig(root);
167
+ if (current.error)
168
+ throw new Error(current.error);
169
+ const directory = dirname(current.path);
170
+ await mkdir(directory, { recursive: true });
171
+ await updateJsonFile(directory, "mcp.json", validateMcpFile, existing => ({
172
+ ...existing,
173
+ mcpServers: { ...existing?.mcpServers, [name]: validated },
174
+ }));
175
+ return readWorkspaceMcpConfig(root);
176
+ }
177
+ export async function removeWorkspaceMcpServer(root, name) {
178
+ const current = await readWorkspaceMcpConfig(root);
179
+ if (current.error)
180
+ throw new Error(current.error);
181
+ if (!current.present || !Object.hasOwn(current.servers, name)) {
182
+ return { removed: false, configuration: current };
183
+ }
184
+ const directory = dirname(current.path);
185
+ const removed = await updateJsonFile(directory, "mcp.json", validateMcpFile, existing => {
186
+ if (!existing || !Object.hasOwn(existing.mcpServers, name))
187
+ return undefined;
188
+ const remaining = { ...existing.mcpServers };
189
+ delete remaining[name];
190
+ return { ...existing, mcpServers: remaining };
191
+ });
192
+ return { removed, configuration: await readWorkspaceMcpConfig(root) };
193
+ }
@@ -6,3 +6,4 @@ export declare function parseSkillFrontmatter(content: string): {
6
6
  } | undefined;
7
7
  /** Scan `<workspace>/.agents/skills/<dir>/SKILL.md` for workspace-authored skills. */
8
8
  export declare function discoverWorkspaceSkills(root: string): Promise<WorkspaceSkillMeta[]>;
9
+ export declare function discoverSkills(skillsDir: string, indexPath: string): Promise<WorkspaceSkillMeta[]>;
@@ -55,7 +55,9 @@ export function parseSkillFrontmatter(content) {
55
55
  }
56
56
  /** Scan `<workspace>/.agents/skills/<dir>/SKILL.md` for workspace-authored skills. */
57
57
  export async function discoverWorkspaceSkills(root) {
58
- const skillsDir = join(root, ".agents", "skills");
58
+ return discoverSkills(join(root, ".agents", "skills"), ".agents/skills");
59
+ }
60
+ export async function discoverSkills(skillsDir, indexPath) {
59
61
  let entries;
60
62
  try {
61
63
  entries = await readdir(skillsDir, { withFileTypes: true });
@@ -83,7 +85,7 @@ export async function discoverWorkspaceSkills(root) {
83
85
  results.push({
84
86
  name: meta.name,
85
87
  description: meta.description,
86
- path: `.agents/skills/${entry.name}/SKILL.md`,
88
+ path: join(indexPath, entry.name, "SKILL.md"),
87
89
  });
88
90
  }
89
91
  results.sort((a, b) => a.name.localeCompare(b.name));
@@ -0,0 +1,11 @@
1
+ import type { AcpAnswer, AcpElicitation, AcpElicitationReply, AcpInput, AcpPermission, AcpPermissionReply, AcpModelState, AcpProcess, AcpReport, AcpSessionReference } from "../model/acp.js";
2
+ export declare function acpReference(value: unknown): AcpSessionReference;
3
+ export declare function acpModelState(value: unknown): AcpModelState;
4
+ export declare function acpProcess(value: unknown): AcpProcess;
5
+ export declare function acpInput(value: unknown): AcpInput;
6
+ export declare function acpAnswer(value: unknown): AcpAnswer;
7
+ export declare function acpReport(value: unknown): AcpReport;
8
+ export declare function acpPermission(value: unknown): AcpPermission;
9
+ export declare function acpPermissionReply(value: unknown): AcpPermissionReply;
10
+ export declare function acpElicitation(value: unknown): AcpElicitation;
11
+ export declare function acpElicitationReply(value: unknown): AcpElicitationReply;
@@ -0,0 +1,75 @@
1
+ import { isAbsolute } from "node:path";
2
+ import { object } from "../common/json.js";
3
+ import { isAcpBackend, isOneOf, requireThat } from "../common/validation.js";
4
+ import { fileId } from "./file-access.js";
5
+ import { acpAnswerStates, acpElicitationActions, acpPermissionPolicies, isAcpElicitationValue } from "../model/acp.js";
6
+ const text = (value) => typeof value === "string" && value.length > 0 && !value.includes("\0");
7
+ export function acpReference(value) {
8
+ requireThat(object(value) && value.host === "acp" && isAcpBackend(value.backend) &&
9
+ text(value.command) && isAbsolute(value.command) && Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")) &&
10
+ (value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
11
+ if (value.env !== undefined) {
12
+ requireThat(object(value.env) && Object.entries(value.env).every(([k, v]) => text(k) && typeof v === "string" && !v.includes("\0")), "ACP 环境变量配置无效。");
13
+ }
14
+ requireThat(value.permission === undefined || isOneOf(value.permission, acpPermissionPolicies), "ACP 权限策略无效。");
15
+ const capabilities = value.capabilities;
16
+ if (capabilities !== undefined)
17
+ requireThat(object(capabilities) &&
18
+ ["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
19
+ return value;
20
+ }
21
+ export function acpModelState(value) {
22
+ requireThat(object(value) && text(value.generation) && ["provider", "model", "resolvedModel", "thinking"].every(key => value[key] === undefined || text(value[key])), "ACP 模型选择无效。");
23
+ return value;
24
+ }
25
+ export function acpProcess(value) {
26
+ requireThat(object(value) && text(value.generation) && Number.isSafeInteger(value.pid) && Number(value.pid) > 1 &&
27
+ typeof value.startedAt === "number" && Number.isFinite(value.startedAt), "ACP 进程记录无效。");
28
+ return value;
29
+ }
30
+ export function acpInput(value) {
31
+ requireThat(object(value) && text(value.generation) && text(value.requestId), "ACP 输入关联无效。");
32
+ fileId(value.requestId);
33
+ return value;
34
+ }
35
+ export function acpAnswer(value) {
36
+ acpInput(value);
37
+ requireThat(object(value) && typeof value.answer === "string" && isOneOf(value.state, acpAnswerStates) &&
38
+ (value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
39
+ return value;
40
+ }
41
+ export function acpReport(value) {
42
+ requireThat(object(value) && text(value.generation) && text(value.cycleId) && typeof value.text === "string", "ACP 周期报告无效。");
43
+ fileId(value.cycleId);
44
+ return value;
45
+ }
46
+ export function acpPermission(value) {
47
+ acpInput(value);
48
+ requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
49
+ return value;
50
+ }
51
+ export function acpPermissionReply(value) {
52
+ acpInput(value);
53
+ requireThat(object(value) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
54
+ return value;
55
+ }
56
+ export function acpElicitation(value) {
57
+ acpInput(value);
58
+ requireThat(object(value) && (value.mode === "form" || value.mode === "url") && typeof value.message === "string", "ACP 交互请求无效。");
59
+ if (value.mode === "form") {
60
+ requireThat(object(value.schema) && value.url === undefined, "ACP 表单交互请求缺少有效 schema。");
61
+ }
62
+ else {
63
+ requireThat(text(value.url) && value.schema === undefined, "ACP URL 交互请求缺少有效 url。");
64
+ }
65
+ return value;
66
+ }
67
+ export function acpElicitationReply(value) {
68
+ acpInput(value);
69
+ requireThat(object(value) && isOneOf(value.action, acpElicitationActions), "ACP 交互答复无效。");
70
+ if (value.content !== undefined) {
71
+ requireThat(value.action === "accept" && object(value.content) &&
72
+ Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
73
+ }
74
+ return value;
75
+ }
@@ -0,0 +1,31 @@
1
+ import type { AgentDescriptor, AgentLaunch, AgentMessage, AgentStatus, AgentCycle, AgentSessionReference } from "../model/agent.js";
2
+ import type { AcpSessionReference } from "../model/acp.js";
3
+ export declare function descriptor(v: unknown): AgentDescriptor;
4
+ export declare function launch(v: unknown): AgentLaunch;
5
+ export declare function message(v: unknown): AgentMessage;
6
+ export declare function cycle(v: unknown): AgentCycle;
7
+ export declare function status(v: unknown): AgentStatus;
8
+ export declare class AgentFiles {
9
+ #private;
10
+ readonly root: string;
11
+ constructor(root: string);
12
+ check(): void;
13
+ fail(error: unknown): void;
14
+ directory(id: string): string;
15
+ read<T>(id: string, path: string, parse: (v: unknown) => T): T;
16
+ optional<T>(id: string, path: string, parse: (v: unknown) => T): T | undefined;
17
+ write<T>(id: string, path: string, value: T, parse: (v: unknown) => T, create?: boolean): void;
18
+ create(value: AgentDescriptor, run: AgentLaunch, acp: AcpSessionReference): void;
19
+ list(): AgentDescriptor[];
20
+ messages(id: string): AgentMessage[];
21
+ completions(id: string): AgentCycle[];
22
+ /** Permission requests of a generation that have no reply yet. */
23
+ pendingPermissions(id: string, generation: string): import("../model/acp.js").AcpPermission[];
24
+ /** Elicitation requests (forms, choices, text inputs, URL confirmations) of a generation that have no reply yet. */
25
+ pendingElicitations(id: string, generation: string): import("../model/acp.js").AcpElicitation[];
26
+ ensureSubdirectory(id: string, name: string): void;
27
+ reference(id: string): AgentSessionReference;
28
+ /** Short controller edits are serialized; an abandoned lock fails closed. */
29
+ control<T>(work: () => T): T;
30
+ enqueue(id: string, text: string, delivery: AgentMessage["delivery"], files?: string[], requestId?: string): AgentMessage;
31
+ }
@@ -0,0 +1,159 @@
1
+ import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { randomUUID } from "node:crypto";
4
+ import { canonical, object } from "../common/json.js";
5
+ import { isOneOf, requireThat } from "../common/validation.js";
6
+ import { readJsonFile, writeJsonFile } from "./atomic-json.js";
7
+ import { fileId, privateDirectory } from "./file-access.js";
8
+ import { agentStates, cycleSources, cycleStates, messageDeliveries } from "../model/agent.js";
9
+ import { acpElicitation, acpPermission, acpReference } from "./acp-files.js";
10
+ const text = (v) => typeof v === "string" && v.length > 0;
11
+ const number = (v) => typeof v === "number" && Number.isFinite(v);
12
+ export function descriptor(v) {
13
+ requireThat(object(v) && text(v.agentId) && text(v.release) && text(v.workspace) && text(v.cwd) && text(v.parentSessionId) &&
14
+ (v.parentLeaf === null || text(v.parentLeaf)) && number(v.createdAt) && object(v.options), "子会话身份无效。");
15
+ fileId(v.agentId);
16
+ return v;
17
+ }
18
+ export function launch(v) {
19
+ requireThat(object(v) && text(v.generation) && text(v.name) && Number.isSafeInteger(v.sequence) && typeof v.stop === "boolean" && number(v.startedAt), "子会话启动状态无效。");
20
+ return v;
21
+ }
22
+ export function message(v) {
23
+ requireThat(object(v) && text(v.requestId) && text(v.generation) && Number.isSafeInteger(v.sequence) && typeof v.message === "string" &&
24
+ isOneOf(v.delivery, messageDeliveries) && Array.isArray(v.files) && v.files.every(text), "子会话消息无效。");
25
+ return v;
26
+ }
27
+ export function cycle(v) {
28
+ requireThat(object(v) && text(v.cycleId) && text(v.generation) && number(v.startedAt) &&
29
+ isOneOf(v.state, cycleStates) &&
30
+ (v.leaf === null || text(v.leaf)) && Array.isArray(v.requestIds) && v.requestIds.every(text) &&
31
+ isOneOf(v.source, cycleSources) && (v.finishedAt === undefined || number(v.finishedAt)), "子会话周期无效。");
32
+ return v;
33
+ }
34
+ export function status(v) {
35
+ requireThat(object(v) && text(v.generation) && isOneOf(v.state, agentStates) &&
36
+ typeof v.activity === "string" && number(v.updatedAt), "子会话状态无效。");
37
+ if (v.cycle)
38
+ cycle(v.cycle);
39
+ return v;
40
+ }
41
+ export class AgentFiles {
42
+ root;
43
+ #failure;
44
+ constructor(root) {
45
+ this.root = root;
46
+ }
47
+ check() { if (this.#failure)
48
+ throw this.#failure; }
49
+ fail(error) { this.#failure = error; }
50
+ directory(id) { return join(this.root, fileId(id)); }
51
+ read(id, path, parse) { return readJsonFile(this.directory(id), path, parse); }
52
+ optional(id, path, parse) {
53
+ try {
54
+ return this.read(id, path, parse);
55
+ }
56
+ catch (error) {
57
+ if (error.code === "ENOENT")
58
+ return undefined;
59
+ throw error;
60
+ }
61
+ }
62
+ write(id, path, value, parse, create = false) {
63
+ this.check();
64
+ parse(value);
65
+ let published;
66
+ try {
67
+ published = writeJsonFile(this.directory(id), path, value, parse, create);
68
+ }
69
+ catch (error) {
70
+ this.fail(error);
71
+ throw error;
72
+ }
73
+ requireThat(published, "子会话文件已存在。");
74
+ }
75
+ create(value, run, acp) {
76
+ this.check();
77
+ acpReference(acp);
78
+ mkdirSync(this.root, { recursive: true, mode: 0o700 });
79
+ mkdirSync(this.directory(value.agentId), { mode: 0o700 });
80
+ for (const dir of ["inbox", "completions", "reports", "permissions", "elicitations"])
81
+ privateDirectory(this.directory(value.agentId), dir);
82
+ this.write(value.agentId, "descriptor.json", value, descriptor, true);
83
+ this.write(value.agentId, "launch.json", run, launch, true);
84
+ this.write(value.agentId, "acp-session.json", acp, acpReference, true);
85
+ }
86
+ list() {
87
+ if (!existsSync(this.root))
88
+ return [];
89
+ return readdirSync(this.root, { withFileTypes: true }).filter(e => e.isDirectory() && !e.name.startsWith(".")).flatMap(e => {
90
+ const d = this.optional(e.name, "descriptor.json", descriptor);
91
+ return d ? [d] : [];
92
+ });
93
+ }
94
+ messages(id) {
95
+ return readdirSync(join(this.directory(id), "inbox")).filter(n => n.endsWith(".json"))
96
+ .map(n => this.read(id, "inbox/" + n, message)).sort((a, b) => a.sequence - b.sequence);
97
+ }
98
+ completions(id) {
99
+ return readdirSync(join(this.directory(id), "completions")).filter(n => n.endsWith(".json"))
100
+ .map(n => this.read(id, "completions/" + n, cycle)).sort((a, b) => (a.finishedAt ?? 0) - (b.finishedAt ?? 0));
101
+ }
102
+ /** Permission requests of a generation that have no reply yet. */
103
+ pendingPermissions(id, generation) {
104
+ const dir = join(this.directory(id), "permissions");
105
+ if (!existsSync(dir))
106
+ return [];
107
+ const names = readdirSync(dir);
108
+ return names.filter(n => n.endsWith(".json") && !n.endsWith("-reply.json") && !names.includes(n.slice(0, -5) + "-reply.json"))
109
+ .map(n => this.read(id, "permissions/" + n, acpPermission)).filter(item => item.generation === generation);
110
+ }
111
+ /** Elicitation requests (forms, choices, text inputs, URL confirmations) of a generation that have no reply yet. */
112
+ pendingElicitations(id, generation) {
113
+ const dir = join(this.directory(id), "elicitations");
114
+ if (!existsSync(dir))
115
+ return [];
116
+ const names = readdirSync(dir);
117
+ return names.filter(n => n.endsWith(".json") && !n.endsWith("-reply.json") && !names.includes(n.slice(0, -5) + "-reply.json"))
118
+ .map(n => this.read(id, "elicitations/" + n, acpElicitation)).filter(item => item.generation === generation);
119
+ }
120
+ ensureSubdirectory(id, name) {
121
+ privateDirectory(this.directory(id), name);
122
+ }
123
+ reference(id) {
124
+ return this.read(id, "acp-session.json", acpReference);
125
+ }
126
+ /** Short controller edits are serialized; an abandoned lock fails closed. */
127
+ control(work) {
128
+ this.check();
129
+ mkdirSync(this.root, { recursive: true, mode: 0o700 });
130
+ const path = join(this.root, ".control");
131
+ try {
132
+ mkdirSync(path, { mode: 0o700 });
133
+ }
134
+ catch (error) {
135
+ if (error.code !== "EEXIST")
136
+ throw error;
137
+ throw Object.assign(new Error("子会话控制正在进行;稍后重试。崩溃遗留的 .control 需停止相关执行实例后手动移除。"), { code: "PBOT_CONTROL_BUSY" });
138
+ }
139
+ try {
140
+ return work();
141
+ }
142
+ finally {
143
+ rmSync(path, { recursive: true });
144
+ }
145
+ }
146
+ enqueue(id, text, delivery, files = [], requestId = randomUUID()) {
147
+ fileId(requestId);
148
+ const run = this.read(id, "launch.json", launch), previous = this.optional(id, "inbox/" + requestId + ".json", message);
149
+ if (previous) {
150
+ requireThat(previous.generation === run.generation && previous.message === text && previous.delivery === delivery && canonical(previous.files) === canonical(files), "请求 ID 已被不同输入使用。");
151
+ return previous;
152
+ }
153
+ run.sequence++;
154
+ this.write(id, "launch.json", run, launch);
155
+ const value = { requestId, generation: run.generation, sequence: run.sequence, message: text, delivery, files };
156
+ this.write(id, "inbox/" + value.requestId + ".json", value, message, true);
157
+ return value;
158
+ }
159
+ }
@@ -1,5 +1,5 @@
1
1
  /** The root must already exist. Never follow aliases inside internal storage. */
2
- export declare function internalPath(directory: string, relative: string): string;
2
+ export declare function internalPath(directory: string, relative: string, allowMissingParents?: boolean): string;
3
3
  export declare function syncDirectory(directory: string): void;
4
4
  /** Validators return plain data and must reject incomplete contracts. */
5
5
  export declare function readJsonFile<T>(directory: string, relative: string, validate: (value: unknown) => T): T;
@@ -9,3 +9,5 @@ export declare function readJsonFile<T>(directory: string, relative: string, val
9
9
  * createOnly publishes by link, so an existing destination is never overwritten.
10
10
  */
11
11
  export declare function writeJsonFile<T>(directory: string, relative: string, value: unknown, validate: (value: unknown) => T, createOnly?: boolean): boolean;
12
+ /** Serialize a read-modify-write across processes. An undefined replacement means no change. */
13
+ export declare function updateJsonFile<T>(directory: string, relative: string, validate: (value: unknown) => T, update: (current: T | undefined) => T | undefined): Promise<boolean>;
@@ -1,10 +1,11 @@
1
- import { constants, closeSync, fstatSync, fsyncSync, linkSync, lstatSync, openSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
1
+ import { constants, closeSync, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
+ import { setTimeout as delay } from "node:timers/promises";
3
4
  import { randomUUID } from "node:crypto";
4
5
  import { canonical } from "../common/json.js";
5
6
  import { requireThat } from "../common/validation.js";
6
7
  /** The root must already exist. Never follow aliases inside internal storage. */
7
- export function internalPath(directory, relative) {
8
+ export function internalPath(directory, relative, allowMissingParents = false) {
8
9
  const parts = relative.split("/");
9
10
  requireThat(parts.every(part => part && part !== "." && part !== ".." && !/[\\\x00-\x1f]/.test(part)), "内部状态路径无效。");
10
11
  const info = lstatSync(directory);
@@ -17,7 +18,7 @@ export function internalPath(directory, relative) {
17
18
  requireThat(!info.isSymbolicLink() && (index === parts.length - 1 ? info.isFile() : info.isDirectory()), "内部状态不能使用符号链接或特殊文件。");
18
19
  }
19
20
  catch (error) {
20
- if (error.code !== "ENOENT" || index !== parts.length - 1)
21
+ if (error.code !== "ENOENT" || (!allowMissingParents && index !== parts.length - 1))
21
22
  throw error;
22
23
  }
23
24
  }
@@ -99,3 +100,42 @@ export function writeJsonFile(directory, relative, value, validate, createOnly =
99
100
  }
100
101
  }
101
102
  }
103
+ /** Serialize a read-modify-write across processes. An undefined replacement means no change. */
104
+ export async function updateJsonFile(directory, relative, validate, update) {
105
+ const lock = internalPath(directory, relative) + ".lock";
106
+ const deadline = Date.now() + 3000;
107
+ for (;;) {
108
+ try {
109
+ mkdirSync(lock, { mode: 0o700 });
110
+ break;
111
+ }
112
+ catch (error) {
113
+ if (error.code !== "EEXIST")
114
+ throw error;
115
+ if (Date.now() >= deadline)
116
+ throw new Error(`写锁被占用:${lock}。若写入进程已退出,可手动移除遗留锁目录。`);
117
+ await delay(25);
118
+ }
119
+ }
120
+ try {
121
+ for (;;) {
122
+ let current;
123
+ try {
124
+ current = readJsonFile(directory, relative, validate);
125
+ }
126
+ catch (error) {
127
+ if (error.code !== "ENOENT")
128
+ throw error;
129
+ }
130
+ const next = update(current);
131
+ if (next === undefined)
132
+ return false;
133
+ if (writeJsonFile(directory, relative, next, validate, current === undefined))
134
+ return true;
135
+ // An external writer created the file first. Apply the edit to its value.
136
+ }
137
+ }
138
+ finally {
139
+ rmdirSync(lock);
140
+ }
141
+ }
@@ -0,0 +1,2 @@
1
+ export declare function fileId(id: string): string;
2
+ export declare function privateDirectory(root: string, relative: string): void;
@@ -0,0 +1,26 @@
1
+ import { lstatSync, mkdirSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { requireThat } from "../common/validation.js";
4
+ import { syncDirectory } from "./atomic-json.js";
5
+ export function fileId(id) {
6
+ requireThat(/^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/.test(id), "内部记录 ID 无效。");
7
+ return id;
8
+ }
9
+ export function privateDirectory(root, relative) {
10
+ const parts = relative.split("/");
11
+ let parent = root;
12
+ for (const part of parts) {
13
+ fileId(part);
14
+ const path = join(parent, part);
15
+ try {
16
+ mkdirSync(path, { mode: 0o700 });
17
+ syncDirectory(parent);
18
+ }
19
+ catch (error) {
20
+ if (error.code !== "EEXIST")
21
+ throw error;
22
+ }
23
+ requireThat(lstatSync(path).isDirectory() && !lstatSync(path).isSymbolicLink(), "内部状态目录无效。");
24
+ parent = path;
25
+ }
26
+ }