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,139 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { resourceRoot } from "../../../common/paths.js";
5
+ import { requireThat } from "../../../common/validation.js";
6
+ import type {
7
+ HistorySummaryRequest,
8
+ RawHistory,
9
+ SummaryArtifact,
10
+ SummaryDimension,
11
+ SummaryPlanOptions,
12
+ SummaryPlanResult,
13
+ SummaryTask,
14
+ } from "../../../model/history.js";
15
+ import { historyBucketFromTimestamp, unknownHistoryBucket } from "../../../storage/history-files.js";
16
+
17
+ let cachedPrompt: string | undefined;
18
+ function systemPrompt(): string {
19
+ cachedPrompt ??= readFileSync(join(resourceRoot, "prompts/history-monthly-summary.md"), "utf8");
20
+ return cachedPrompt;
21
+ }
22
+
23
+ function fingerprintSessionSummaries(summaries: readonly SummaryArtifact[]): string {
24
+ const hash = createHash("sha256");
25
+ for (const item of summaries) {
26
+ hash.update(JSON.stringify([item.id, item.title, item.from, item.to, item.source.fingerprint, item.text]));
27
+ hash.update("\n");
28
+ }
29
+ return hash.digest("hex");
30
+ }
31
+
32
+ function monthSpan(summaries: readonly SummaryArtifact[]): { from: string; to: string } {
33
+ const dates = summaries.flatMap(item => [item.from, item.to]).filter(Boolean).sort();
34
+ return {
35
+ from: dates[0] ?? "",
36
+ to: dates.at(-1) ?? "",
37
+ };
38
+ }
39
+
40
+ function formatSessionSummaries(summaries: readonly SummaryArtifact[]): string {
41
+ return summaries
42
+ .map(item => {
43
+ const header = [`会话 ID:${item.id}`];
44
+ if (item.title) header.push(`标题:${item.title}`);
45
+ if (item.host) header.push(`来源:${item.host}`);
46
+ header.push(`时间:${item.from} — ${item.to}`);
47
+ return `[${header.join(" | ")}]\n${item.text}`;
48
+ })
49
+ .join("\n\n");
50
+ }
51
+
52
+ /**
53
+ * Monthly cross-session summary dimension (enabled only under history.layout: "monthly").
54
+ * Aggregates per-session summaries grouped by UTC YYYY/MM into one monthly artifact per month,
55
+ * preserving source.rawIds for pbot_history drill-down to session summaries and raw exchanges.
56
+ */
57
+ export const monthlySummaryDimension: SummaryDimension = {
58
+ id: "monthly",
59
+ title: "月度历史汇总",
60
+ description: "按自然月(UTC YYYY/MM)归纳当月单会话摘要,提炼月度主题、关键决定与产出,支持逐层下钻。",
61
+ requiresMonthlyLayout: true,
62
+
63
+ plan(
64
+ _raws: readonly RawHistory[],
65
+ existing: ReadonlyMap<string, SummaryArtifact>,
66
+ options: SummaryPlanOptions = {},
67
+ ): SummaryPlanResult {
68
+ const force = Boolean(options.force);
69
+ const sessionMap = options.summariesByDimension?.get("session");
70
+ if (!sessionMap || sessionMap.size === 0) return { tasks: [], skipped: 0 };
71
+
72
+ const byMonth = new Map<string, SummaryArtifact[]>();
73
+ for (const artifact of sessionMap.values()) {
74
+ const bucket = historyBucketFromTimestamp(artifact.from || artifact.to);
75
+ if (bucket === unknownHistoryBucket) continue;
76
+ const monthId = bucket.replace("/", "-");
77
+ const list = byMonth.get(monthId);
78
+ if (list) list.push(artifact);
79
+ else byMonth.set(monthId, [artifact]);
80
+ }
81
+
82
+ const tasks: SummaryTask[] = [];
83
+ let skipped = 0;
84
+
85
+ for (const [monthId, summaries] of byMonth) {
86
+ const ordered = [...summaries].sort((a, b) => a.from.localeCompare(b.from) || a.id.localeCompare(b.id));
87
+ const fingerprint = fingerprintSessionSummaries(ordered);
88
+ const prev = existing.get(monthId);
89
+ if (!force && prev && prev.source.fingerprint === fingerprint) {
90
+ skipped++;
91
+ continue;
92
+ }
93
+ const rawIds = [...new Set(ordered.flatMap(item => item.source.rawIds))];
94
+ if (!rawIds.length) continue;
95
+ const span = monthSpan(ordered);
96
+ const [year, month] = monthId.split("-");
97
+ tasks.push({
98
+ id: monthId,
99
+ dimension: this.id,
100
+ mode: prev ? "rebuild" : "create",
101
+ title: `${year}年${month}月历史汇总`,
102
+ from: span.from,
103
+ to: span.to,
104
+ source: { rawIds, exchangeIds: [], fingerprint },
105
+ exchanges: [],
106
+ sourceSummaries: ordered,
107
+ ...(prev ? { existing: prev } : {}),
108
+ });
109
+ }
110
+
111
+ tasks.sort((a, b) => b.id.localeCompare(a.id));
112
+ return { tasks, skipped };
113
+ },
114
+
115
+ buildRequest(task: SummaryTask): HistorySummaryRequest {
116
+ const system = systemPrompt();
117
+ const summaries = task.sourceSummaries ?? [];
118
+ const prompt = [
119
+ `月份:${task.id}(共 ${summaries.length} 个会话摘要)`,
120
+ formatSessionSummaries(summaries),
121
+ ].join("\n\n");
122
+ return { system, prompt, maxTokens: 3072 };
123
+ },
124
+
125
+ finalize(task: SummaryTask, output: string, now: string): SummaryArtifact {
126
+ const text = output.trim();
127
+ requireThat(text.length > 0, "月度历史汇总为空。");
128
+ return {
129
+ id: task.id,
130
+ dimension: this.id,
131
+ title: task.title,
132
+ text,
133
+ from: task.from,
134
+ to: task.to,
135
+ updatedAt: now,
136
+ source: task.source,
137
+ };
138
+ },
139
+ };
@@ -0,0 +1,149 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { resourceRoot } from "../../../common/paths.js";
4
+ import { requireThat } from "../../../common/validation.js";
5
+ import type {
6
+ HistoryExchange,
7
+ HistorySummaryRequest,
8
+ RawHistory,
9
+ SummaryArtifact,
10
+ SummaryDimension,
11
+ SummaryPlanResult,
12
+ SummaryTask,
13
+ } from "../../../model/history.js";
14
+ import { fingerprintExchanges, orderedRawExchanges, rawSessionSpan, rawSessionTitle } from "../history-index.js";
15
+
16
+ let cachedPrompt: string | undefined;
17
+ function systemPrompt(): string {
18
+ cachedPrompt ??= readFileSync(join(resourceRoot, "prompts/history-summary.md"), "utf8");
19
+ return cachedPrompt;
20
+ }
21
+
22
+ function formatExchanges(exchanges: readonly (HistoryExchange & { id: string })[]): string {
23
+ return exchanges
24
+ .map(item => `[${item.from} — ${item.to}${item.completed ? "" : ";未完成"}]\n${item.text}`)
25
+ .join("\n\n");
26
+ }
27
+
28
+ function isStrictUnchangedPrefix(
29
+ existing: SummaryArtifact,
30
+ ordered: readonly (HistoryExchange & { id: string })[],
31
+ ): boolean {
32
+ const prevIds = existing.source.exchangeIds;
33
+ if (prevIds.length === 0 || prevIds.length >= ordered.length) return false;
34
+ for (let i = 0; i < prevIds.length; i++) {
35
+ if (ordered[i]!.id !== prevIds[i]) return false;
36
+ }
37
+ const prefixFingerprint = fingerprintExchanges(ordered.slice(0, prevIds.length));
38
+ return prefixFingerprint === existing.source.fingerprint;
39
+ }
40
+
41
+ /**
42
+ * Default per-session task summary dimension.
43
+ * Produces one concise summary per raw session archive, supporting exact deduplication
44
+ * (0 tokens on unchanged sessions) and rolling incremental updates when new turns are appended.
45
+ */
46
+ export const sessionSummaryDimension: SummaryDimension = {
47
+ id: "session",
48
+ title: "会话任务摘要",
49
+ description: "按单个会话提炼任务目标、关键决定、结果与未决事项,支持去重跳过与增量滚动更新。",
50
+
51
+ plan(
52
+ raws: readonly RawHistory[],
53
+ existing: ReadonlyMap<string, SummaryArtifact>,
54
+ options: { force?: boolean } = {},
55
+ ): SummaryPlanResult {
56
+ const force = Boolean(options.force);
57
+ const tasks: SummaryTask[] = [];
58
+ let skipped = 0;
59
+
60
+ for (const raw of raws) {
61
+ const ordered = orderedRawExchanges(raw);
62
+ if (!ordered.length) continue;
63
+
64
+ const exchangeIds = ordered.map(item => item.id);
65
+ const fingerprint = fingerprintExchanges(ordered);
66
+ const prev = existing.get(raw.id);
67
+
68
+ if (!force && prev && prev.source.fingerprint === fingerprint) {
69
+ skipped++;
70
+ continue;
71
+ }
72
+
73
+ const span = rawSessionSpan(ordered);
74
+ const title = rawSessionTitle(raw);
75
+ const source = { rawIds: [raw.id], exchangeIds, fingerprint };
76
+
77
+ if (!force && prev && isStrictUnchangedPrefix(prev, ordered)) {
78
+ tasks.push({
79
+ id: raw.id,
80
+ dimension: this.id,
81
+ mode: "incremental",
82
+ title,
83
+ host: raw.host,
84
+ sessionId: raw.sessionId,
85
+ from: span.from,
86
+ to: span.to,
87
+ source,
88
+ exchanges: ordered,
89
+ existing: prev,
90
+ addedExchanges: ordered.slice(prev.source.exchangeIds.length),
91
+ });
92
+ } else {
93
+ tasks.push({
94
+ id: raw.id,
95
+ dimension: this.id,
96
+ mode: prev ? "rebuild" : "create",
97
+ title,
98
+ host: raw.host,
99
+ sessionId: raw.sessionId,
100
+ from: span.from,
101
+ to: span.to,
102
+ source,
103
+ exchanges: ordered,
104
+ ...(prev ? { existing: prev } : {}),
105
+ });
106
+ }
107
+ }
108
+
109
+ // Process most recently active sessions first.
110
+ tasks.sort((a, b) => b.to.localeCompare(a.to) || a.id.localeCompare(b.id));
111
+ return { tasks, skipped };
112
+ },
113
+
114
+ buildRequest(task: SummaryTask): HistorySummaryRequest {
115
+ const system = systemPrompt();
116
+ const heading = task.title ? [`会话标题:${task.title}`] : [];
117
+ if (task.mode === "incremental" && task.existing && task.addedExchanges?.length) {
118
+ const prompt = [
119
+ ...heading,
120
+ `【已有摘要(${task.existing.from} — ${task.existing.to})】\n${task.existing.text}`,
121
+ `【新增对话】\n${formatExchanges(task.addedExchanges)}`,
122
+ "在已有摘要基础上合并新增对话,输出更新后的完整摘要:保留仍有效的事实,用新决定覆盖旧决定,去重并保持简洁。",
123
+ ].join("\n\n");
124
+ return { system, prompt, maxTokens: 2048 };
125
+ }
126
+ const prompt = [
127
+ ...heading,
128
+ formatExchanges(task.exchanges),
129
+ ].join("\n\n");
130
+ return { system, prompt, maxTokens: 2048 };
131
+ },
132
+
133
+ finalize(task: SummaryTask, output: string, now: string): SummaryArtifact {
134
+ const text = output.trim();
135
+ requireThat(text.length > 0, "历史摘要为空。");
136
+ return {
137
+ id: task.id,
138
+ dimension: this.id,
139
+ ...(task.host ? { host: task.host } : {}),
140
+ ...(task.sessionId ? { sessionId: task.sessionId } : {}),
141
+ title: task.title,
142
+ text,
143
+ from: task.from,
144
+ to: task.to,
145
+ updatedAt: now,
146
+ source: task.source,
147
+ };
148
+ },
149
+ };
@@ -0,0 +1,43 @@
1
+ import { isHistoryIdentifier, requireThat } from "../../common/validation.js";
2
+ import type { HistoryLayout, SummaryDimension } from "../../model/history.js";
3
+ import { monthlySummaryDimension } from "./summary-dimensions/monthly.js";
4
+ import { sessionSummaryDimension } from "./summary-dimensions/session.js";
5
+
6
+ const registry = new Map<string, SummaryDimension>([
7
+ [sessionSummaryDimension.id, sessionSummaryDimension],
8
+ [monthlySummaryDimension.id, monthlySummaryDimension],
9
+ ]);
10
+
11
+ /** Register a custom summary dimension (e.g. daily, decisions, topics). */
12
+ export function registerSummaryDimension(dimension: SummaryDimension): void {
13
+ requireThat(isHistoryIdentifier(dimension.id), "摘要维度 ID 无效:" + dimension.id);
14
+ registry.set(dimension.id, dimension);
15
+ }
16
+
17
+ export function unregisterSummaryDimension(id: string): boolean {
18
+ return registry.delete(id);
19
+ }
20
+
21
+ export function getSummaryDimension(id: string): SummaryDimension | undefined {
22
+ return registry.get(id);
23
+ }
24
+
25
+ export function listSummaryDimensions(layout?: HistoryLayout): SummaryDimension[] {
26
+ const all = [...registry.values()];
27
+ if (layout === "flat") return all.filter(dim => !dim.requiresMonthlyLayout);
28
+ return all;
29
+ }
30
+
31
+ export function resolveSummaryDimensions(ids?: readonly string[], layout: HistoryLayout = "monthly"): SummaryDimension[] {
32
+ if (!ids || ids.length === 0) return listSummaryDimensions(layout);
33
+ const unique = [...new Set(ids)];
34
+ return unique.map(id => {
35
+ const found = registry.get(id);
36
+ requireThat(found, `未知的摘要维度:${id}(可用:${[...registry.keys()].join(", ")})。`);
37
+ requireThat(
38
+ !found.requiresMonthlyLayout || layout === "monthly",
39
+ `摘要维度 ${id} 仅在年月目录布局(history.layout: "monthly")下启用。`,
40
+ );
41
+ return found;
42
+ });
43
+ }
@@ -0,0 +1,343 @@
1
+ import { isDeepStrictEqual } from "node:util";
2
+ import { setTimeout as delay } from "node:timers/promises";
3
+ import { requireThat } from "../../common/validation.js";
4
+ import type {
5
+ HistoryLayout,
6
+ HistorySummarizer,
7
+ HistorySummaryReminder,
8
+ RawHistory,
9
+ SummaryArtifact,
10
+ SummaryDimensionReport,
11
+ SummaryDimensionStatus,
12
+ SummaryRunProgress,
13
+ SummaryRunReport,
14
+ } from "../../model/history.js";
15
+ import { summaryConcurrency } from "../../model/workspace-config.js";
16
+ import type { HistoryFiles } from "../../storage/history-files.js";
17
+ import { workspaceHistoryFiles } from "./workspace-history.js";
18
+ import { rawSessionSpan } from "./history-index.js";
19
+ import { resolveSummaryDimensions } from "./summary-registry.js";
20
+
21
+ const lockRetryAttempts = 5;
22
+ const lockRetryDelayMs = 40;
23
+ export const defaultSummaryReminderDays = 7;
24
+ const dayMs = 24 * 60 * 60 * 1000;
25
+
26
+ async function withSummaryLockRetry<T>(
27
+ files: HistoryFiles,
28
+ dimension: string,
29
+ operation: () => T,
30
+ signal?: AbortSignal,
31
+ ): Promise<T> {
32
+ for (let attempt = 0; ; attempt++) {
33
+ signal?.throwIfAborted();
34
+ try {
35
+ return files.controlSummary(dimension, operation);
36
+ } catch (error) {
37
+ const code = (error as NodeJS.ErrnoException).code;
38
+ if (code === "PBOT_HISTORY_BUSY" && attempt < lockRetryAttempts - 1) {
39
+ await delay(lockRetryDelayMs * (attempt + 1), undefined, { signal });
40
+ continue;
41
+ }
42
+ throw error;
43
+ }
44
+ }
45
+ }
46
+
47
+ export interface InspectSummaryOptions {
48
+ dimensions?: readonly string[];
49
+ thresholdDays?: number;
50
+ now?: Date;
51
+ }
52
+
53
+ export type CheckSummaryReminderOptions = InspectSummaryOptions;
54
+
55
+ function oldestRawTimestamp(raws: readonly RawHistory[]): number | undefined {
56
+ let oldest: number | undefined;
57
+ for (const raw of raws) {
58
+ if (!raw.exchanges.length) continue;
59
+ const { from } = rawSessionSpan(raw.exchanges);
60
+ if (!from) continue;
61
+ const parsed = Date.parse(from);
62
+ if (Number.isNaN(parsed)) continue;
63
+ if (oldest === undefined || parsed < oldest) oldest = parsed;
64
+ }
65
+ return oldest;
66
+ }
67
+
68
+ function latestSummaryTimestamp(artifacts: Iterable<SummaryArtifact>): number | undefined {
69
+ let latest: number | undefined;
70
+ for (const artifact of artifacts) {
71
+ const parsed = Date.parse(artifact.updatedAt);
72
+ if (Number.isNaN(parsed)) continue;
73
+ if (latest === undefined || parsed > latest) latest = parsed;
74
+ }
75
+ return latest;
76
+ }
77
+
78
+ function evaluateSummaryReminder(
79
+ raws: readonly RawHistory[],
80
+ allSummaries: readonly SummaryArtifact[],
81
+ pendingCount: number,
82
+ options: { thresholdDays?: number; now?: Date } = {},
83
+ ): HistorySummaryReminder | undefined {
84
+ if (!raws.length || pendingCount <= 0) return undefined;
85
+ const thresholdDays = options.thresholdDays ?? defaultSummaryReminderDays;
86
+ requireThat(Number.isFinite(thresholdDays) && thresholdDays > 0, "历史摘要提醒天数必须大于 0。");
87
+ const thresholdMs = thresholdDays * dayMs;
88
+ const nowMs = (options.now ?? new Date()).getTime();
89
+ const oldestRawMs = oldestRawTimestamp(raws);
90
+ if (oldestRawMs === undefined || nowMs - oldestRawMs < thresholdMs) return undefined;
91
+
92
+ const latestSummaryMs = latestSummaryTimestamp(allSummaries);
93
+ if (latestSummaryMs !== undefined && nowMs - latestSummaryMs < thresholdMs) return undefined;
94
+
95
+ const rawAgeDays = Math.floor((nowMs - oldestRawMs) / dayMs);
96
+ const daysSinceLastSummary = latestSummaryMs === undefined ? undefined : Math.floor((nowMs - latestSummaryMs) / dayMs);
97
+ const message = daysSinceLastSummary === undefined
98
+ ? `历史原文已累计 ${rawAgeDays} 天(待摘要 ${pendingCount} 项),尚未触发过摘要整理;可在终端运行 pbot history summary 生成摘要(Pi/dsh 可用 /pbot history summary)。`
99
+ : `历史原文已累计 ${rawAgeDays} 天,且已 ${daysSinceLastSummary} 天未触发摘要整理(待摘要 ${pendingCount} 项);可在终端运行 pbot history summary 更新摘要(Pi/dsh 可用 /pbot history summary)。`;
100
+
101
+ return {
102
+ rawCount: raws.length,
103
+ pendingCount,
104
+ oldestRawAt: new Date(oldestRawMs).toISOString(),
105
+ ...(latestSummaryMs !== undefined ? { latestSummaryAt: new Date(latestSummaryMs).toISOString() } : {}),
106
+ rawAgeDays,
107
+ ...(daysSinceLastSummary !== undefined ? { daysSinceLastSummary } : {}),
108
+ thresholdDays,
109
+ message,
110
+ };
111
+ }
112
+
113
+ /** Read-only startup probe: reminds when raw history has existed >= thresholdDays and no summary ran within thresholdDays. */
114
+ export function checkHistorySummaryReminder(
115
+ root: string,
116
+ options: CheckSummaryReminderOptions = {},
117
+ ): HistorySummaryReminder | undefined {
118
+ return summaryReminder(workspaceHistoryFiles(root), options);
119
+ }
120
+
121
+ export function historyStartupNotices(root: string): string[] {
122
+ const notices: string[] = [];
123
+ try {
124
+ const files = workspaceHistoryFiles(root);
125
+ const migration = files.migrationNotice();
126
+ if (migration) notices.push(migration);
127
+ const reminder = summaryReminder(files, {});
128
+ if (reminder) notices.push(reminder.message);
129
+ } catch { /* History diagnostics are best effort during host startup. */ }
130
+ return notices;
131
+ }
132
+
133
+ function summaryReminder(files: HistoryFiles, options: CheckSummaryReminderOptions) {
134
+ const raws = files.listRaw();
135
+ if (!raws.length) return undefined;
136
+
137
+ const thresholdDays = options.thresholdDays ?? defaultSummaryReminderDays;
138
+ requireThat(Number.isFinite(thresholdDays) && thresholdDays > 0, "历史摘要提醒天数必须大于 0。");
139
+ const nowMs = (options.now ?? new Date()).getTime();
140
+ const oldestRawMs = oldestRawTimestamp(raws);
141
+ if (oldestRawMs === undefined || nowMs - oldestRawMs < thresholdDays * dayMs) return undefined;
142
+
143
+ const { totalPending, allSummaries } = inspectDimensions(files, raws, options, []);
144
+ return evaluateSummaryReminder(raws, allSummaries, totalPending, options);
145
+ }
146
+
147
+ function inspectDimensions(files: HistoryFiles, raws: RawHistory[], options: InspectSummaryOptions, warnings: string[]) {
148
+ const dimensions = resolveSummaryDimensions(options.dimensions, files.layout);
149
+
150
+ const statuses: SummaryDimensionStatus[] = [];
151
+ const seenDimensions = new Set<string>();
152
+ const summariesByDimension = new Map<string, Map<string, SummaryArtifact>>();
153
+ const allSummaries: SummaryArtifact[] = [];
154
+ let totalPending = 0;
155
+
156
+ const loadDimension = (dimId: string) => {
157
+ const cached = summariesByDimension.get(dimId);
158
+ if (cached) return cached;
159
+ const list = files.listSummaries(dimId);
160
+ warnings.push(...files.warnings);
161
+ const map = new Map(list.map(item => [item.id, item]));
162
+ summariesByDimension.set(dimId, map);
163
+ return map;
164
+ };
165
+
166
+ for (const dimension of dimensions) {
167
+ seenDimensions.add(dimension.id);
168
+ if (dimension.id !== "session") loadDimension("session");
169
+ const existing = loadDimension(dimension.id);
170
+ allSummaries.push(...existing.values());
171
+ const plan = dimension.plan(raws, existing, { force: false, summariesByDimension });
172
+ totalPending += plan.tasks.length;
173
+
174
+ let pendingCreate = 0;
175
+ let pendingIncremental = 0;
176
+ let pendingRebuild = 0;
177
+ for (const task of plan.tasks) {
178
+ if (task.mode === "create") pendingCreate++;
179
+ else if (task.mode === "incremental") pendingIncremental++;
180
+ else pendingRebuild++;
181
+ }
182
+ statuses.push({
183
+ dimension: dimension.id,
184
+ title: dimension.title,
185
+ description: dimension.description,
186
+ total: plan.skipped + plan.tasks.length,
187
+ ready: plan.skipped,
188
+ pendingCreate,
189
+ pendingIncremental,
190
+ pendingRebuild,
191
+ });
192
+ }
193
+
194
+ for (const dim of files.listDimensions()) {
195
+ if (seenDimensions.has(dim)) continue;
196
+ allSummaries.push(...loadDimension(dim).values());
197
+ }
198
+
199
+ return { statuses, totalPending, allSummaries };
200
+ }
201
+
202
+ /** Read-only inspection of raw archives vs derived summaries across requested dimensions. */
203
+ export function inspectWorkspaceSummaries(
204
+ root: string,
205
+ options: InspectSummaryOptions = {},
206
+ ): { layout: HistoryLayout; rawCount: number; dimensions: SummaryDimensionStatus[]; warnings: string[]; reminder?: HistorySummaryReminder } {
207
+ const files = workspaceHistoryFiles(root);
208
+ const raws = files.listRaw();
209
+ const warnings = [...files.warnings];
210
+ const flatNotice = files.migrationNotice();
211
+ if (flatNotice) warnings.push(flatNotice);
212
+ const { statuses, totalPending, allSummaries } = inspectDimensions(files, raws, options, warnings);
213
+ const reminder = evaluateSummaryReminder(raws, allSummaries, totalPending, options);
214
+ return {
215
+ layout: files.layout,
216
+ rawCount: raws.length,
217
+ dimensions: statuses,
218
+ warnings: [...new Set(warnings)],
219
+ ...(reminder ? { reminder } : {}),
220
+ };
221
+ }
222
+
223
+ export interface RunSummaryOptions {
224
+ root: string;
225
+ summarizer: HistorySummarizer;
226
+ dimensions?: readonly string[];
227
+ force?: boolean;
228
+ concurrency?: number;
229
+ signal?: AbortSignal;
230
+ onProgress?: (progress: SummaryRunProgress) => void;
231
+ }
232
+
233
+ /**
234
+ * Trigger-agnostic summary execution engine.
235
+ * Reads raw archives once, plans deduplicated incremental/full tasks per dimension,
236
+ * executes LLM calls with bounded concurrency, and persists each artifact atomically.
237
+ */
238
+ export async function runWorkspaceSummaries(options: RunSummaryOptions): Promise<SummaryRunReport> {
239
+ const startedAt = Date.now();
240
+ const signal = options.signal ?? new AbortController().signal;
241
+ signal.throwIfAborted();
242
+
243
+ const concurrency = Math.max(1, Math.min(summaryConcurrency.max, Math.floor(options.concurrency ?? summaryConcurrency.default)));
244
+ const files = workspaceHistoryFiles(options.root);
245
+ const dimensions = resolveSummaryDimensions(options.dimensions, files.layout);
246
+
247
+ const warnings: string[] = [];
248
+ const loadSummaries = (id: string) => {
249
+ const items = files.listSummaries(id);
250
+ warnings.push(...files.warnings);
251
+ return new Map(items.map(item => [item.id, item]));
252
+ };
253
+ // Capture each publication baseline before reading the input it will summarize.
254
+ const plannedDimensions = dimensions.map(dimension => ({ dimension, existing: loadSummaries(dimension.id) }));
255
+ const raws = files.listRaw();
256
+ warnings.push(...files.warnings);
257
+ const flatNotice = files.migrationNotice();
258
+ if (flatNotice) warnings.push(flatNotice);
259
+ const summariesByDimension = new Map<string, Map<string, SummaryArtifact>>();
260
+ const reports: SummaryDimensionReport[] = [];
261
+
262
+ for (const { dimension, existing } of plannedDimensions) {
263
+ signal.throwIfAborted();
264
+ if (dimension.id !== "session") summariesByDimension.set("session", loadSummaries("session"));
265
+ summariesByDimension.set(dimension.id, existing);
266
+ const plan = dimension.plan(raws, existing, { force: Boolean(options.force), summariesByDimension });
267
+
268
+ const report: SummaryDimensionReport = {
269
+ dimension: dimension.id,
270
+ scanned: dimension.id === "monthly" ? (summariesByDimension.get("session")?.size ?? 0) : raws.length,
271
+ skipped: plan.skipped,
272
+ created: 0,
273
+ incremental: 0,
274
+ rebuilt: 0,
275
+ failed: [],
276
+ };
277
+
278
+ if (plan.tasks.length > 0) {
279
+ let nextIndex = 0;
280
+ let completed = 0;
281
+ const total = plan.tasks.length;
282
+
283
+ const worker = async () => {
284
+ for (;;) {
285
+ signal.throwIfAborted();
286
+ const index = nextIndex++;
287
+ if (index >= total) return;
288
+ const task = plan.tasks[index]!;
289
+ try {
290
+ const request = dimension.buildRequest(task);
291
+ const output = await options.summarizer(request, signal);
292
+ signal.throwIfAborted();
293
+ const artifact = dimension.finalize(task, output, new Date().toISOString());
294
+ const published = await withSummaryLockRetry(files, dimension.id, () => {
295
+ signal.throwIfAborted();
296
+ const current = files.readSummary(dimension.id, artifact.id);
297
+ if (isDeepStrictEqual(current, existing.get(artifact.id))) {
298
+ files.saveSummary(artifact);
299
+ return artifact;
300
+ }
301
+ if (current) existing.set(artifact.id, current);
302
+ else existing.delete(artifact.id);
303
+ requireThat(current && isDeepStrictEqual(current.source, artifact.source),
304
+ "摘要在生成期间已变更,请重新运行摘要。");
305
+ return current;
306
+ }, signal);
307
+ existing.set(artifact.id, published);
308
+ if (published !== artifact) report.skipped++;
309
+ else if (task.mode === "create") report.created++;
310
+ else if (task.mode === "incremental") report.incremental++;
311
+ else report.rebuilt++;
312
+ } catch (error) {
313
+ if (signal.aborted) throw error;
314
+ report.failed.push({ id: task.id, error: error instanceof Error ? error.message : String(error) });
315
+ } finally {
316
+ completed++;
317
+ if (!signal.aborted) {
318
+ options.onProgress?.({
319
+ dimension: dimension.id,
320
+ completed,
321
+ total,
322
+ currentId: task.id,
323
+ mode: task.mode,
324
+ });
325
+ }
326
+ }
327
+ }
328
+ };
329
+
330
+ const workerCount = Math.min(concurrency, total);
331
+ await Promise.all(Array.from({ length: workerCount }, () => worker()));
332
+ }
333
+
334
+ reports.push(report);
335
+ }
336
+
337
+ requireThat(!signal.aborted, "历史摘要已取消。");
338
+ return {
339
+ dimensions: reports,
340
+ durationMs: Date.now() - startedAt,
341
+ warnings: [...new Set(warnings)],
342
+ };
343
+ }
@@ -0,0 +1,8 @@
1
+ import { join } from "node:path";
2
+ import type { HistoryHost } from "../../model/history.js";
3
+ import { HistoryFiles } from "../../storage/history-files.js";
4
+ import { readWorkspaceHistoryLayoutSync } from "../workspace/workspace-config.js";
5
+
6
+ export function workspaceHistoryFiles(root: string, host?: HistoryHost): HistoryFiles {
7
+ return new HistoryFiles(join(root, ".pbot/history"), host, readWorkspaceHistoryLayoutSync(root));
8
+ }