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,598 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { existsSync } from "node:fs";
3
+ import { realpath, stat } from "node:fs/promises";
4
+ import { homedir } from "node:os";
5
+ import { isAbsolute, join } from "node:path";
6
+ import { setTimeout as delay } from "node:timers/promises";
7
+ import { object } from "../../common/json.js";
8
+ import { isAcpBackend, isHistoryIdentifier, isOneOf, requireThat } from "../../common/validation.js";
9
+ import { resourceRoot } from "../../common/paths.js";
10
+ import { packageVersion } from "../../common/version.js";
11
+ import { AcpWorkerHost } from "../../integrations/acp/agents.js";
12
+ import { canonicalAcpBackend, inspectAcpBackends, knownAcpBackends } from "../../integrations/acp/launch.js";
13
+ import { pluginMcpWorkspaceRoot } from "../../integrations/hosts/plugin-registry.js";
14
+ import { mcpWorkerToolNames, acpElicitationActions, isAcpElicitationValue, } from "../../model/acp.js";
15
+ import { buildHistoryMcpTools, buildPbotWorkerTools, buildWorkspaceMcpTools, defaultWaitMs, discoverTargets, maxWaitMs, parseMcpToolCall, } from "./tool-contract.js";
16
+ import { historyPageLimits } from "../../model/history.js";
17
+ import { AgentService } from "../../services/agents/agent-service.js";
18
+ import { readWorkspaceHistory, searchWorkspaceHistory } from "../../services/history/history-query.js";
19
+ import { readWorkspaceConfig, workspaceDisabledMessage, workspaceFeatures } from "../../services/workspace/workspace-config.js";
20
+ import { WorkspaceMcpBridge } from "./bridge.js";
21
+ export { buildHistoryMcpTools, buildPbotWorkerTools, buildWorkspaceMcpTools, pbotWorkerTools } from "./tool-contract.js";
22
+ const protocolVersion = "2025-03-26";
23
+ const readyTimeoutMs = 45_000;
24
+ const reportPageSize = 16_000;
25
+ const progressIntervalMs = 5_000;
26
+ export const mcpWorkerOnlyInstructions = "提供跨助手协作能力(worker_*):可启动并调度其他外部 AI 助手在当前工作区中独立执行任务,适用于用户指定其他助手、需要特定模型或思考深度、获取独立审查/第二意见,或并行处理子任务。不需要借助其他助手时,优先自行完成或使用当前内置子代理。\n" +
27
+ "使用流程:用 worker_create 启动目标助手(或用 worker_discover 复用已有会话)→ 用 worker_request 派发任务或继续追问 → 任务完成后可用 worker_close 结束会话。后台等待、超时续等(timedOut)或长结果翻页(nextOffset)用 worker_output;助手请求授权或提问(needsResponse)时,先向用户确认,再用 worker_response 回复。";
28
+ export const mcpInstructions = "提供三类能力:\n" +
29
+ "1. 跨助手协作(worker_*):启动并调度其他外部 AI 助手在当前工作区中独立执行任务,适用于用户指定其他助手、需要特定模型或思考深度、获取独立审查/第二意见,或并行处理子任务。不需要借助其他助手时,优先自行完成或使用当前内置子代理。使用流程:worker_create 启动助手(或 worker_discover 复用已有会话)→ worker_request 派发任务或追问 → 完成后 worker_close 结束;后台等待、超时续等(timedOut)或长结果翻页(nextOffset)用 worker_output;返回 needsResponse 时先向用户确认,再用 worker_response 回复。\n" +
30
+ "2. 工作区共享外部工具(workspace_mcp_*):发现并调用当前工作区接入的外部工具服务与数据源(可与当前环境原生工具同时使用)。先用 workspace_mcp_discover 查看可用服务与工具说明,再用 workspace_mcp_call 调用。\n" +
31
+ "3. 跨宿主历史查询(history_search / history_read):只读检索当前工作区在 Pi、dsh、Claude Code、Antigravity 等宿主中记录的历史问答与摘要归档。";
32
+ export function defaultWorkerDirectory() {
33
+ if (process.env.PBOT_MCP_AGENT_DIR)
34
+ return process.env.PBOT_MCP_AGENT_DIR;
35
+ if (process.env.PLUGIN_DATA)
36
+ return join(process.env.PLUGIN_DATA, "subagents");
37
+ return join(homedir(), ".pbot/subagents");
38
+ }
39
+ function workerCommandTool(name) {
40
+ const full = name.startsWith("worker_") ? name : `worker_${name}`;
41
+ return isOneOf(full, mcpWorkerToolNames) ? full : undefined;
42
+ }
43
+ /** Parse Pi/dsh `/pbot worker ...` CLI arguments into a canonical MCP tool call. */
44
+ export function parseWorkerCommand(raw) {
45
+ const trimmed = raw.trim();
46
+ if (!trimmed || trimmed === "overview")
47
+ return { tool: "worker_discover", args: { target: "overview" } };
48
+ if (trimmed === "status" || trimmed === "list" || trimmed === "workers") {
49
+ return { tool: "worker_discover", args: { target: "workers" } };
50
+ }
51
+ if (trimmed.startsWith("status ") || trimmed.startsWith("workers ")) {
52
+ const workerId = trimmed.slice(trimmed.indexOf(" ") + 1).trim();
53
+ requireThat(workerId.length > 0, "请提供 workerId。");
54
+ return { tool: "worker_discover", args: { target: "workers", workerId } };
55
+ }
56
+ if (trimmed === "backends")
57
+ return { tool: "worker_discover", args: { target: "backends" } };
58
+ if (trimmed === "models")
59
+ return { tool: "worker_discover", args: { target: "models" } };
60
+ if (trimmed.startsWith("models ")) {
61
+ const backend = trimmed.slice(7).trim();
62
+ requireThat(backend.length > 0, "请提供 backend 名称。");
63
+ return { tool: "worker_discover", args: { target: "models", backend } };
64
+ }
65
+ if (trimmed.startsWith("close ")) {
66
+ const rest = trimmed.slice(6).trim();
67
+ if (!rest.startsWith("{")) {
68
+ requireThat(rest.length > 0, "请提供要关闭的 workerId。");
69
+ return { tool: "worker_close", args: { workerId: rest } };
70
+ }
71
+ }
72
+ if (trimmed.startsWith("{")) {
73
+ const parsed = JSON.parse(trimmed);
74
+ requireThat(object(parsed), 'worker 参数必须是 JSON 对象,例如:/pbot worker create {"backend":"codex"}');
75
+ const resolved = typeof parsed.tool === "string" ? workerCommandTool(parsed.tool) : undefined;
76
+ requireThat(resolved, `JSON 形式需包含有效 tool 字段(${mcpWorkerToolNames.join(", ")}),或使用 /pbot worker <工具名> <JSON>。`);
77
+ const rest = Object.fromEntries(Object.entries(parsed).filter(([key]) => key !== "tool"));
78
+ return { tool: resolved, args: rest };
79
+ }
80
+ const match = trimmed.match(/^(\S+)(?:\s+([\s\S]*))?$/);
81
+ const verb = match?.[1] ?? "";
82
+ const rest = (match?.[2] ?? "").trim();
83
+ const tool = workerCommandTool(verb);
84
+ requireThat(tool, `未知 worker 命令:${verb}(支持:overview | status | backends | models | ${mcpWorkerToolNames.map(name => name.slice("worker_".length)).join(" | ")})。`);
85
+ if (!rest)
86
+ return { tool, args: {} };
87
+ const parsed = JSON.parse(rest);
88
+ requireThat(object(parsed), `${tool} 的参数必须是 JSON 对象。`);
89
+ return { tool, args: parsed };
90
+ }
91
+ function asString(args, key, required = false) {
92
+ const value = args[key];
93
+ if (value === undefined || value === null) {
94
+ requireThat(!required, `缺少必填参数:${key}`);
95
+ return undefined;
96
+ }
97
+ requireThat(typeof value === "string" && value.length > 0, `参数 ${key} 必须是非空字符串。`);
98
+ return value;
99
+ }
100
+ function asInt(args, key, min, max) {
101
+ const value = args[key];
102
+ if (value === undefined || value === null)
103
+ return undefined;
104
+ requireThat(typeof value === "number" && Number.isInteger(value) && value >= min && value <= max, `参数 ${key} 必须是 ${min}–${max} 的整数。`);
105
+ return value;
106
+ }
107
+ function asContent(args, key) {
108
+ const value = args[key];
109
+ if (value === undefined || value === null)
110
+ return undefined;
111
+ requireThat(object(value), `参数 ${key} 必须是 JSON 对象。`);
112
+ for (const [field, entry] of Object.entries(value)) {
113
+ requireThat(isAcpElicitationValue(entry), `参数 ${key}.${field} 必须是字符串、数字、布尔值或字符串数组。`);
114
+ }
115
+ return value;
116
+ }
117
+ function presentCycle(cycle) {
118
+ return {
119
+ state: cycle.state,
120
+ ...(cycle.error ? { error: cycle.error } : {}),
121
+ };
122
+ }
123
+ function presentSelection(selection) {
124
+ if (!selection)
125
+ return undefined;
126
+ const entries = Object.entries(selection).filter(([, value]) => value !== undefined);
127
+ return entries.length ? Object.fromEntries(entries) : undefined;
128
+ }
129
+ function presentWorker(value) {
130
+ const selection = presentSelection(value.selection);
131
+ const truncated = value.nextOffset !== undefined && value.nextOffset !== null;
132
+ return {
133
+ workerId: value.agentId,
134
+ backend: value.backend,
135
+ state: value.state,
136
+ activity: value.activity,
137
+ alive: value.alive,
138
+ ...(selection ? { selection } : {}),
139
+ ...(value.pendingRequests?.length ? { pendingRequests: value.pendingRequests } : {}),
140
+ ...(value.cycle ? { cycle: presentCycle(value.cycle) } : {}),
141
+ ...(value.report !== undefined ? { report: value.report } : {}),
142
+ ...(truncated ? { totalLength: value.totalLength, nextOffset: value.nextOffset } : {}),
143
+ ...(value.error ? { error: value.error } : {}),
144
+ };
145
+ }
146
+ export class McpWorkerRuntime {
147
+ #services = new Map();
148
+ #bridge = new WorkspaceMcpBridge();
149
+ #sessionId;
150
+ #directory;
151
+ #defaultBackend;
152
+ #nativeWorkspaceMcp;
153
+ #nativeHistory;
154
+ #closing;
155
+ constructor(directory = defaultWorkerDirectory(), sessionId = process.env.PBOT_MCP_SESSION_ID || ("mcp-" + randomUUID()), defaultBackend = process.env.PBOT_HOST || undefined, nativeWorkspaceMcp = /^(1|true)$/i.test(process.env.PBOT_NATIVE_WORKSPACE_MCP ?? ""), nativeHistory = /^(1|true)$/i.test(process.env.PBOT_NATIVE_HISTORY ?? "")) {
156
+ this.#directory = directory;
157
+ this.#sessionId = sessionId;
158
+ this.#defaultBackend = defaultBackend?.trim() || undefined;
159
+ this.#nativeWorkspaceMcp = nativeWorkspaceMcp;
160
+ this.#nativeHistory = nativeHistory;
161
+ }
162
+ /** Mount the stdio sibling with the same worker ownership as the native host command. */
163
+ nativeServer(root) {
164
+ const entry = join(resourceRoot, "../lib/extension/mcp.js");
165
+ if (!existsSync(entry))
166
+ return undefined;
167
+ requireThat(this.#defaultBackend, "原生 MCP 挂载需要宿主名称。");
168
+ return {
169
+ command: process.execPath,
170
+ args: [entry],
171
+ env: {
172
+ PBOT_HOST: this.#defaultBackend,
173
+ PBOT_WORKSPACE_ROOT: root,
174
+ PBOT_MCP_AGENT_DIR: this.#directory,
175
+ PBOT_MCP_SESSION_ID: this.#sessionId,
176
+ PBOT_NATIVE_WORKSPACE_MCP: "1",
177
+ PBOT_NATIVE_HISTORY: "1",
178
+ },
179
+ };
180
+ }
181
+ get nativeWorkspaceMcp() {
182
+ return this.#nativeWorkspaceMcp;
183
+ }
184
+ get nativeHistory() {
185
+ return this.#nativeHistory;
186
+ }
187
+ resolveBackend(rawArgs, commands, allowDefault) {
188
+ const raw = asString(rawArgs, "backend") ?? (allowDefault ? this.#defaultBackend : undefined);
189
+ requireThat(raw, `缺少必填参数:backend(请指定目标程序:${knownAcpBackends(commands).join(", ")})。`);
190
+ const backend = canonicalAcpBackend(raw, commands);
191
+ requireThat(isAcpBackend(backend), "无效的 backend 名称:" + raw);
192
+ return backend;
193
+ }
194
+ async resolveWorkspace(supplied) {
195
+ const target = supplied || pluginMcpWorkspaceRoot() || process.cwd();
196
+ requireThat(isAbsolute(target), "工作区需要绝对路径。");
197
+ const root = await realpath(target);
198
+ requireThat((await stat(root)).isDirectory(), "工作区路径不是目录。");
199
+ return root;
200
+ }
201
+ workerService(root, commands = {}) {
202
+ let entry = this.#services.get(root);
203
+ if (!entry) {
204
+ const host = new AcpWorkerHost(this.#directory, commands);
205
+ entry = {
206
+ host,
207
+ service: new AgentService(root, this.#directory, host),
208
+ owner: { sessionId: this.#sessionId, leaf: null, branch: [] },
209
+ };
210
+ this.#services.set(root, entry);
211
+ }
212
+ else {
213
+ entry.host.commands = commands;
214
+ }
215
+ return entry;
216
+ }
217
+ async toolDefinitions() {
218
+ let commands;
219
+ let includeWorkspaceMcp = !this.#nativeWorkspaceMcp;
220
+ let includeHistory = !this.#nativeHistory;
221
+ try {
222
+ const root = await this.resolveWorkspace();
223
+ const configuration = await readWorkspaceConfig(root);
224
+ if (configuration.enabled) {
225
+ commands = configuration.acp;
226
+ const features = workspaceFeatures(configuration);
227
+ includeWorkspaceMcp = !this.#nativeWorkspaceMcp && features.mcp;
228
+ includeHistory = !this.#nativeHistory && features.history;
229
+ }
230
+ }
231
+ catch { /* Fall back to registered built-in harnesses when cwd is outside an enabled workspace. */ }
232
+ const defaultBackend = this.#defaultBackend ? canonicalAcpBackend(this.#defaultBackend, commands) : undefined;
233
+ const workspaceBound = Boolean(pluginMcpWorkspaceRoot());
234
+ return [
235
+ ...buildPbotWorkerTools(commands, defaultBackend, workspaceBound),
236
+ ...(includeWorkspaceMcp ? buildWorkspaceMcpTools(workspaceBound) : []),
237
+ ...(includeHistory ? buildHistoryMcpTools(workspaceBound) : []),
238
+ ];
239
+ }
240
+ /** Wait until ACP handshake and model/thinking configuration finish so invalid options fail synchronously on create/resume. */
241
+ async waitReady(service, owner, workerId, timeoutMs, progress) {
242
+ const deadline = Date.now() + timeoutMs;
243
+ let lastProgressAt = 0;
244
+ let lastMarker = "";
245
+ for (;;) {
246
+ requireThat(!this.#closing, "Personal Bot MCP 服务已关闭。");
247
+ const current = service.status(workerId, owner);
248
+ if (current.state === "error" || current.state === "stopped") {
249
+ throw new Error(current.error || "worker 启动失败。");
250
+ }
251
+ if (current.state === "idle" || current.state === "busy")
252
+ return current;
253
+ if (Date.now() >= deadline)
254
+ return current;
255
+ if (progress) {
256
+ const marker = `${current.state}:${current.activity}`;
257
+ const now = Date.now();
258
+ if (marker !== lastMarker || now - lastProgressAt >= progressIntervalMs) {
259
+ lastMarker = marker;
260
+ lastProgressAt = now;
261
+ progress(`${current.backend}: ${current.activity}`);
262
+ }
263
+ }
264
+ await delay(50);
265
+ }
266
+ }
267
+ async waitFor(service, owner, workerId, requestId, timeoutMs, offset = 0, progress) {
268
+ const deadline = Date.now() + timeoutMs;
269
+ let lastProgressAt = 0;
270
+ let lastMarker = "";
271
+ for (;;) {
272
+ requireThat(!this.#closing, "Personal Bot MCP 服务已关闭。");
273
+ const { current, cycle, terminal } = service.outputStatus(workerId, owner, requestId);
274
+ if (cycle && cycle.state !== "running") {
275
+ const page = await service.read(workerId, owner, cycle.cycleId, offset, reportPageSize);
276
+ return { settled: true, ...presentWorker(page) };
277
+ }
278
+ if (terminal) {
279
+ if (offset > 0)
280
+ await service.read(workerId, owner, undefined, offset, reportPageSize);
281
+ return { settled: true, ...presentWorker(current) };
282
+ }
283
+ if (current.pendingRequests?.length) {
284
+ return {
285
+ settled: false,
286
+ needsResponse: true,
287
+ ...presentWorker(current),
288
+ };
289
+ }
290
+ if (Date.now() >= deadline) {
291
+ return { settled: false, timedOut: true, ...presentWorker(current) };
292
+ }
293
+ if (progress) {
294
+ const marker = `${current.state}:${current.activity}`;
295
+ const now = Date.now();
296
+ if (marker !== lastMarker || now - lastProgressAt >= progressIntervalMs) {
297
+ lastMarker = marker;
298
+ lastProgressAt = now;
299
+ progress(`${current.backend}: ${current.activity}`);
300
+ }
301
+ }
302
+ await delay(100);
303
+ }
304
+ }
305
+ async callTool(toolName, input, progress) {
306
+ const { name, args: rawArgs } = parseMcpToolCall(toolName, input);
307
+ requireThat(!this.#closing, "Personal Bot MCP 服务已关闭。");
308
+ const root = await this.resolveWorkspace(asString(rawArgs, "workspace"));
309
+ const configuration = await readWorkspaceConfig(root);
310
+ requireThat(!this.#closing, "Personal Bot MCP 服务已关闭。");
311
+ requireThat(configuration.enabled, workspaceDisabledMessage(configuration));
312
+ switch (name) {
313
+ case "workspace_mcp_discover":
314
+ case "workspace_mcp_call": {
315
+ requireThat(!this.#nativeWorkspaceMcp, "当前宿主已原生挂载工作区 .pbot/mcp.json 服务,请直接调用对应原生 MCP 工具,勿重复经由 workspace_mcp_* 桥接。");
316
+ requireThat(workspaceFeatures(configuration).mcp, `工作区统一 MCP 功能已在 ${configuration.path} 中停用(features.mcp: false)。`);
317
+ if (name === "workspace_mcp_discover")
318
+ return this.#bridge.discover(root, asString(rawArgs, "server"));
319
+ const server = asString(rawArgs, "server", true);
320
+ const tool = asString(rawArgs, "tool", true);
321
+ const toolArgs = rawArgs.arguments === undefined ? {} : rawArgs.arguments;
322
+ requireThat(object(toolArgs), "参数 arguments 必须是 JSON 对象。");
323
+ return this.#bridge.call(root, server, tool, toolArgs);
324
+ }
325
+ case "history_search": {
326
+ requireThat(!this.#nativeHistory, "当前宿主已提供原生 pbot_history 工具,请直接调用 pbot_history。");
327
+ requireThat(workspaceFeatures(configuration).history, `工作区历史功能已在 ${configuration.path} 中停用(features.history: false)。`);
328
+ const hostFilter = asString(rawArgs, "host");
329
+ if (hostFilter !== undefined)
330
+ requireThat(isHistoryIdentifier(hostFilter), "无效的 host 来源:" + hostFilter);
331
+ const dimension = asString(rawArgs, "dimension");
332
+ if (dimension !== undefined)
333
+ requireThat(isHistoryIdentifier(dimension), "无效的 dimension:" + dimension);
334
+ const query = asString(rawArgs, "query");
335
+ const sessionId = asString(rawArgs, "sessionId");
336
+ const from = asString(rawArgs, "from");
337
+ const to = asString(rawArgs, "to");
338
+ const offset = asInt(rawArgs, "offset", 0, 10_000_000) ?? 0;
339
+ const limit = asInt(rawArgs, "limit", 1, historyPageLimits.search.max) ?? historyPageLimits.search.default;
340
+ return searchWorkspaceHistory(root, {
341
+ ...(hostFilter ? { host: hostFilter } : {}),
342
+ ...(dimension ? { dimension } : {}),
343
+ ...(query ? { query } : {}),
344
+ ...(sessionId ? { sessionId } : {}),
345
+ ...(from ? { from } : {}),
346
+ ...(to ? { to } : {}),
347
+ offset,
348
+ limit,
349
+ });
350
+ }
351
+ case "history_read": {
352
+ requireThat(!this.#nativeHistory, "当前宿主已提供原生 pbot_history 工具,请直接调用 pbot_history。");
353
+ requireThat(workspaceFeatures(configuration).history, `工作区历史功能已在 ${configuration.path} 中停用(features.history: false)。`);
354
+ const id = asString(rawArgs, "id", true);
355
+ const hostFilter = asString(rawArgs, "host");
356
+ if (hostFilter !== undefined)
357
+ requireThat(isHistoryIdentifier(hostFilter), "无效的 host 来源:" + hostFilter);
358
+ const offset = asInt(rawArgs, "offset", 0, 10_000_000) ?? 0;
359
+ const limit = asInt(rawArgs, "limit", 1, historyPageLimits.read.max) ?? historyPageLimits.read.default;
360
+ const childrenOffset = asInt(rawArgs, "childrenOffset", 0, 10_000_000) ?? 0;
361
+ return { node: readWorkspaceHistory(root, id, offset, limit, childrenOffset, hostFilter) };
362
+ }
363
+ }
364
+ const { host, service, owner } = this.workerService(root, configuration.acp);
365
+ switch (name) {
366
+ case "worker_create": {
367
+ const backend = this.resolveBackend(rawArgs, configuration.acp, false);
368
+ const model = asString(rawArgs, "model");
369
+ const thinking = asString(rawArgs, "thinking");
370
+ const cwd = asString(rawArgs, "cwd");
371
+ const created = await service.start({
372
+ backend,
373
+ ...(model ? { model } : {}),
374
+ ...(thinking ? { thinking } : {}),
375
+ ...(cwd ? { cwd } : {}),
376
+ }, owner);
377
+ const ready = await this.waitReady(service, owner, created.agentId, readyTimeoutMs, progress);
378
+ return {
379
+ ...presentWorker(ready),
380
+ capabilities: { resume: ready.capabilities.resume },
381
+ };
382
+ }
383
+ case "worker_close":
384
+ return presentWorker(service.stop(asString(rawArgs, "workerId", true), owner));
385
+ case "worker_request": {
386
+ const workerId = asString(rawArgs, "workerId", true);
387
+ const message = asString(rawArgs, "message", true);
388
+ const waitMs = asInt(rawArgs, "waitMs", 0, maxWaitMs) ?? defaultWaitMs;
389
+ const current = service.status(workerId, owner);
390
+ requireThat(current.state !== "stopping", "worker 正在停止,请稍后重试。");
391
+ if (current.alive) {
392
+ const sent = service.send(workerId, message, "followUp", owner);
393
+ if (waitMs > 0)
394
+ return this.waitFor(service, owner, workerId, sent.requestId, waitMs, 0, progress);
395
+ return presentWorker(sent);
396
+ }
397
+ const startedAt = Date.now();
398
+ const resumed = await service.resume(workerId, message, owner);
399
+ const ready = await this.waitReady(service, owner, workerId, waitMs > 0 ? Math.min(waitMs, readyTimeoutMs) : readyTimeoutMs, progress);
400
+ if (waitMs > 0 && resumed.requestId) {
401
+ const remaining = Math.max(0, waitMs - (Date.now() - startedAt));
402
+ return this.waitFor(service, owner, workerId, resumed.requestId, remaining, 0, progress);
403
+ }
404
+ return {
405
+ ...presentWorker(ready),
406
+ ...(ready.pendingRequests?.length ? { settled: false, needsResponse: true } : {}),
407
+ };
408
+ }
409
+ case "worker_response": {
410
+ const workerId = asString(rawArgs, "workerId", true);
411
+ const action = asString(rawArgs, "action", true);
412
+ requireThat(isOneOf(action, acpElicitationActions), `参数 action 无效:${action}(可用:${acpElicitationActions.join(", ")})。`);
413
+ const requestId = asString(rawArgs, "requestId");
414
+ const optionId = asString(rawArgs, "optionId");
415
+ const content = asContent(rawArgs, "content");
416
+ const waitMs = asInt(rawArgs, "waitMs", 0, maxWaitMs) ?? defaultWaitMs;
417
+ const updated = service.respond(workerId, {
418
+ action,
419
+ ...(requestId ? { requestId } : {}),
420
+ ...(optionId ? { optionId } : {}),
421
+ ...(content !== undefined ? { content } : {}),
422
+ }, owner);
423
+ if (waitMs > 0)
424
+ return this.waitFor(service, owner, workerId, undefined, waitMs, 0, progress);
425
+ return presentWorker(updated);
426
+ }
427
+ case "worker_output": {
428
+ const workerId = asString(rawArgs, "workerId", true);
429
+ const offset = asInt(rawArgs, "offset", 0, 10_000_000) ?? 0;
430
+ const waitMs = asInt(rawArgs, "waitMs", 0, maxWaitMs) ?? defaultWaitMs;
431
+ return this.waitFor(service, owner, workerId, undefined, waitMs, offset, progress);
432
+ }
433
+ case "worker_discover": {
434
+ const rawTarget = asString(rawArgs, "target") ?? "overview";
435
+ requireThat(isOneOf(rawTarget, discoverTargets), `参数 target 无效:${rawTarget}(可用:${discoverTargets.join(", ")})。`);
436
+ const target = rawTarget;
437
+ const workerId = asString(rawArgs, "workerId");
438
+ const backendRaw = asString(rawArgs, "backend");
439
+ const model = asString(rawArgs, "model");
440
+ if (target !== "workers") {
441
+ requireThat(workerId === undefined, '参数 workerId 仅在 target="workers" 时可用。');
442
+ }
443
+ if (target !== "models") {
444
+ requireThat(backendRaw === undefined && model === undefined, '参数 backend / model 仅在 target="models" 时可用。');
445
+ }
446
+ if (target === "overview") {
447
+ return {
448
+ target,
449
+ backends: inspectAcpBackends(configuration.acp),
450
+ workers: service.list(owner).map(presentWorker),
451
+ };
452
+ }
453
+ if (target === "backends") {
454
+ return { target, backends: inspectAcpBackends(configuration.acp) };
455
+ }
456
+ if (target === "workers") {
457
+ return {
458
+ target,
459
+ workers: workerId
460
+ ? [presentWorker(service.status(workerId, owner))]
461
+ : service.list(owner).map(presentWorker),
462
+ };
463
+ }
464
+ const backend = this.resolveBackend(rawArgs, configuration.acp, true);
465
+ return {
466
+ target,
467
+ ...(await host.inspectModels(backend, root, { ...(model ? { model } : {}) })),
468
+ };
469
+ }
470
+ }
471
+ throw new Error(`未知工具:${name}`);
472
+ }
473
+ close(preserve = false) {
474
+ if (this.#closing)
475
+ return this.#closing;
476
+ const entries = [...this.#services.values()];
477
+ this.#services.clear();
478
+ this.#closing = Promise.allSettled([
479
+ this.#bridge.close(),
480
+ ...entries.map(({ service, owner }) => service.close(owner, preserve)),
481
+ ]).then(() => { });
482
+ return this.#closing;
483
+ }
484
+ }
485
+ export async function handleMcpRequest(runtime, message, notify) {
486
+ if (!object(message) || message.jsonrpc !== "2.0" || typeof message.method !== "string") {
487
+ return { jsonrpc: "2.0", id: null, error: { code: -32600, message: "Invalid JSON-RPC request" } };
488
+ }
489
+ const hasId = Object.hasOwn(message, "id") && message.id !== undefined;
490
+ const id = hasId ? message.id : null;
491
+ if (!hasId)
492
+ return null; // Notifications expect no response.
493
+ try {
494
+ if (message.method === "initialize") {
495
+ const requested = object(message.params) && typeof message.params.protocolVersion === "string"
496
+ ? message.params.protocolVersion
497
+ : protocolVersion;
498
+ return {
499
+ jsonrpc: "2.0",
500
+ id,
501
+ result: {
502
+ protocolVersion: requested || protocolVersion,
503
+ capabilities: { tools: {} },
504
+ serverInfo: { name: "personal-bot", version: packageVersion },
505
+ instructions: runtime.nativeWorkspaceMcp ? mcpWorkerOnlyInstructions : mcpInstructions,
506
+ },
507
+ };
508
+ }
509
+ if (message.method === "ping")
510
+ return { jsonrpc: "2.0", id, result: {} };
511
+ if (message.method === "tools/list") {
512
+ return { jsonrpc: "2.0", id, result: { tools: await runtime.toolDefinitions() } };
513
+ }
514
+ if (message.method === "tools/call") {
515
+ requireThat(object(message.params) && typeof message.params.name === "string", "tools/call 缺少 name。");
516
+ const meta = object(message.params._meta) ? message.params._meta : undefined;
517
+ const progressToken = meta && (typeof meta.progressToken === "string" || typeof meta.progressToken === "number")
518
+ ? meta.progressToken
519
+ : undefined;
520
+ let progressStep = 0;
521
+ const progress = progressToken !== undefined && notify
522
+ ? (statusMessage) => {
523
+ progressStep++;
524
+ notify({
525
+ jsonrpc: "2.0",
526
+ method: "notifications/progress",
527
+ params: { progressToken, progress: progressStep, message: statusMessage },
528
+ });
529
+ }
530
+ : undefined;
531
+ try {
532
+ const result = await runtime.callTool(message.params.name, message.params.arguments ?? {}, progress);
533
+ return {
534
+ jsonrpc: "2.0",
535
+ id,
536
+ result: {
537
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
538
+ },
539
+ };
540
+ }
541
+ catch (error) {
542
+ return {
543
+ jsonrpc: "2.0",
544
+ id,
545
+ result: {
546
+ isError: true,
547
+ content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }],
548
+ },
549
+ };
550
+ }
551
+ }
552
+ return { jsonrpc: "2.0", id, error: { code: -32601, message: "Method not found: " + message.method } };
553
+ }
554
+ catch (error) {
555
+ return { jsonrpc: "2.0", id, error: { code: -32603, message: error instanceof Error ? error.message : String(error) } };
556
+ }
557
+ }
558
+ export async function serveMcpStdio(runtime = new McpWorkerRuntime()) {
559
+ const shutdown = () => { void runtime.close().finally(() => process.exit(0)); };
560
+ process.on("SIGTERM", shutdown);
561
+ process.on("SIGINT", shutdown);
562
+ let buffer = "";
563
+ const pending = new Set();
564
+ const write = (value) => { process.stdout.write(JSON.stringify(value) + "\n"); };
565
+ process.stdin.setEncoding("utf8");
566
+ for await (const chunk of process.stdin) {
567
+ buffer += String(chunk);
568
+ for (;;) {
569
+ const nl = buffer.indexOf("\n");
570
+ if (nl < 0)
571
+ break;
572
+ const line = buffer.slice(0, nl).trim();
573
+ buffer = buffer.slice(nl + 1);
574
+ if (!line)
575
+ continue;
576
+ let parsed;
577
+ try {
578
+ parsed = JSON.parse(line);
579
+ }
580
+ catch {
581
+ write({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
582
+ continue;
583
+ }
584
+ const task = handleMcpRequest(runtime, parsed, write)
585
+ .then(reply => { if (reply)
586
+ write(reply); })
587
+ .catch(error => {
588
+ const id = object(parsed) && Object.hasOwn(parsed, "id") && parsed.id !== undefined ? parsed.id : null;
589
+ if (id !== null)
590
+ write({ jsonrpc: "2.0", id, error: { code: -32603, message: error instanceof Error ? error.message : String(error) } });
591
+ })
592
+ .finally(() => { pending.delete(task); });
593
+ pending.add(task);
594
+ }
595
+ }
596
+ await Promise.allSettled(pending);
597
+ await runtime.close();
598
+ }