personal-bot 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (318) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +2 -1
  3. package/DISCLAIMER.md +17 -0
  4. package/LICENSE +21 -0
  5. package/README.md +15 -5
  6. package/README.zh-CN.md +15 -5
  7. package/bin/pbot-pi-acp.mjs +1 -1
  8. package/bin/pbot.mjs +22 -2
  9. package/docs/README.md +2 -0
  10. package/docs/architecture/design.md +16 -16
  11. package/docs/architecture/extensibility.md +29 -2
  12. package/docs/architecture/model-context.md +3 -2
  13. package/docs/architecture/principles.md +2 -2
  14. package/docs/guides/capabilities.md +1 -1
  15. package/docs/guides/cross-harness-workers.md +3 -1
  16. package/docs/guides/history.md +30 -7
  17. package/docs/guides/workspace-configuration.md +20 -3
  18. package/docs/hosts/antigravity.md +1 -1
  19. package/docs/hosts/claude-code.md +1 -1
  20. package/docs/hosts/codex-qoder.md +63 -0
  21. package/docs/maintainers/architecture-governance.md +78 -0
  22. package/lib/antigravity-plugin/DISCLAIMER.md +17 -0
  23. package/lib/antigravity-plugin/LICENSE +21 -0
  24. package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +36 -0
  25. package/lib/antigravity-plugin/lib/common/files.d.ts +2 -0
  26. package/lib/antigravity-plugin/lib/common/files.js +44 -0
  27. package/lib/antigravity-plugin/lib/common/validation.d.ts +4 -0
  28. package/lib/antigravity-plugin/lib/common/validation.js +8 -0
  29. package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +18 -0
  30. package/lib/antigravity-plugin/lib/extension/acp/member.js +44 -0
  31. package/lib/antigravity-plugin/lib/extension/acp-worker.js +71 -45
  32. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
  33. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.js +22 -0
  34. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.d.ts +4 -0
  35. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +58 -0
  36. package/lib/antigravity-plugin/lib/extension/hosts/dsh/files.d.ts +5 -0
  37. package/lib/antigravity-plugin/lib/extension/hosts/dsh/files.js +67 -0
  38. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.d.ts +3 -0
  39. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +100 -0
  40. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.d.ts +3 -1
  41. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +37 -22
  42. package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.d.ts +6 -0
  43. package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.js +21 -0
  44. package/lib/antigravity-plugin/lib/extension/mcp.js +2577 -2255
  45. package/lib/antigravity-plugin/lib/extension/workspace-cli.js +20171 -0
  46. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +1 -1
  47. package/lib/antigravity-plugin/lib/integrations/hosts/claude/adapter.js +7 -59
  48. package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.d.ts +2 -0
  49. package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.js +14 -0
  50. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/mcp.d.ts +4 -0
  51. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/mcp.js +28 -0
  52. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/messages.d.ts +36 -0
  53. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/messages.js +11 -0
  54. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/resources.d.ts +6 -0
  55. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/resources.js +47 -0
  56. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/root-agents.d.ts +4 -0
  57. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/root-agents.js +3 -0
  58. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/skills.d.ts +8 -0
  59. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/skills.js +31 -0
  60. package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
  61. package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.js +32 -0
  62. package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.d.ts +4 -0
  63. package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.js +23 -0
  64. package/lib/antigravity-plugin/lib/integrations/hosts/plugin-registry.d.ts +1 -0
  65. package/lib/antigravity-plugin/lib/integrations/hosts/plugin-registry.js +8 -1
  66. package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
  67. package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.js +76 -0
  68. package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
  69. package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.js +16 -0
  70. package/lib/antigravity-plugin/lib/model/acp.d.ts +7 -3
  71. package/lib/antigravity-plugin/lib/model/acp.js +8 -0
  72. package/lib/antigravity-plugin/lib/model/agent.d.ts +121 -0
  73. package/lib/antigravity-plugin/lib/model/agent.js +4 -0
  74. package/lib/antigravity-plugin/lib/model/history.d.ts +10 -0
  75. package/lib/antigravity-plugin/lib/model/history.js +4 -1
  76. package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +5 -0
  77. package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +8 -12
  78. package/lib/antigravity-plugin/lib/model/workspace-config.js +2 -1
  79. package/lib/antigravity-plugin/lib/plugin.d.ts +2 -1
  80. package/lib/antigravity-plugin/lib/plugin.js +2 -2
  81. package/lib/antigravity-plugin/lib/services/history/history-capture.js +34 -22
  82. package/lib/antigravity-plugin/lib/services/history/summary-registry.js +2 -3
  83. package/lib/antigravity-plugin/lib/services/history/summary-runner.d.ts +2 -5
  84. package/lib/antigravity-plugin/lib/services/history/summary-runner.js +72 -74
  85. package/lib/antigravity-plugin/lib/services/history/workspace-history.d.ts +3 -0
  86. package/lib/antigravity-plugin/lib/services/history/workspace-history.js +6 -0
  87. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +15 -0
  88. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +126 -0
  89. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +9 -8
  90. package/lib/antigravity-plugin/lib/services/workspace/workspace-files.d.ts +2 -0
  91. package/lib/antigravity-plugin/lib/services/workspace/workspace-files.js +17 -0
  92. package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.d.ts +14 -0
  93. package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.js +193 -0
  94. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.d.ts +1 -0
  95. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.js +4 -2
  96. package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +11 -0
  97. package/lib/antigravity-plugin/lib/storage/acp-files.js +75 -0
  98. package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +31 -0
  99. package/lib/antigravity-plugin/lib/storage/agent-files.js +159 -0
  100. package/lib/antigravity-plugin/lib/storage/atomic-json.d.ts +3 -1
  101. package/lib/antigravity-plugin/lib/storage/atomic-json.js +43 -3
  102. package/lib/antigravity-plugin/lib/storage/history-files.d.ts +2 -1
  103. package/lib/antigravity-plugin/lib/storage/history-files.js +21 -15
  104. package/lib/antigravity-plugin/lib/storage/history-format.d.ts +2 -3
  105. package/lib/antigravity-plugin/lib/storage/history-format.js +5 -7
  106. package/lib/antigravity-plugin/package.json +1 -1
  107. package/lib/antigravity-plugin/plugin.json +1 -1
  108. package/lib/antigravity-plugin/resources/prompts/history-monthly-summary.md +13 -0
  109. package/lib/antigravity-plugin/resources/prompts/history-summary.md +13 -0
  110. package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -0
  111. package/lib/antigravity-plugin/resources/skills/.claude-plugin/plugin.json +4 -0
  112. package/lib/antigravity-plugin/resources/skills/project-maintenance/SKILL.md +35 -0
  113. package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +52 -0
  114. package/lib/antigravity-plugin/skills/pbot/SKILL.md +11 -1
  115. package/lib/client.js +22 -50
  116. package/lib/codex-marketplace/.agents/plugins/marketplace.json +17 -0
  117. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +7 -0
  118. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +17 -0
  119. package/lib/codex-marketplace/plugins/personal-bot/DISCLAIMER.md +17 -0
  120. package/lib/codex-marketplace/plugins/personal-bot/LICENSE +21 -0
  121. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +37 -0
  122. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +9 -0
  123. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/agents/openai.yaml +2 -0
  124. package/lib/common/validation.d.ts +4 -0
  125. package/lib/common/validation.js +8 -0
  126. package/lib/dsh.js +2 -2
  127. package/lib/extension/acp/member.d.ts +1 -1
  128. package/lib/extension/acp-worker.js +71 -45
  129. package/lib/extension/history/command.d.ts +37 -0
  130. package/lib/extension/history/command.js +57 -0
  131. package/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
  132. package/lib/extension/hosts/dsh/acp-entry.js +22 -0
  133. package/lib/extension/hosts/dsh/acp-member.js +4 -18
  134. package/lib/extension/hosts/dsh/command.d.ts +1 -2
  135. package/lib/extension/hosts/dsh/command.js +5 -49
  136. package/lib/extension/hosts/dsh/workspace.js +13 -44
  137. package/lib/extension/hosts/pi/acp-member.d.ts +3 -0
  138. package/lib/extension/hosts/pi/acp-member.js +100 -0
  139. package/lib/extension/hosts/plugin-runtime.d.ts +3 -1
  140. package/lib/extension/hosts/plugin-runtime.js +37 -22
  141. package/lib/extension/hosts/workspace-mcp.d.ts +6 -0
  142. package/lib/extension/hosts/workspace-mcp.js +21 -0
  143. package/lib/extension/mcp/bridge.d.ts +6 -8
  144. package/lib/extension/mcp/bridge.js +66 -84
  145. package/lib/extension/mcp/tool-contract.d.ts +735 -0
  146. package/lib/extension/mcp/tool-contract.js +441 -0
  147. package/lib/extension/mcp/workers.d.ts +29 -742
  148. package/lib/extension/mcp/workers.js +75 -525
  149. package/lib/extension/mcp.js +2577 -2255
  150. package/lib/extension/setup/cli.js +15 -0
  151. package/lib/extension/setup/upgrade.js +153 -0
  152. package/lib/extension/workspace-cli.js +20171 -0
  153. package/lib/integrations/acp/agents.d.ts +2 -3
  154. package/lib/integrations/acp/harnesses/dsh.js +1 -1
  155. package/lib/integrations/acp/harnesses/presets.js +8 -0
  156. package/lib/integrations/acp/history-summary.js +2 -1
  157. package/lib/integrations/acp/launch.d.ts +2 -2
  158. package/lib/integrations/agent-resources.js +1 -1
  159. package/lib/integrations/hosts/claude/adapter.js +7 -59
  160. package/lib/integrations/hosts/codex/adapter.d.ts +2 -0
  161. package/lib/integrations/hosts/codex/adapter.js +14 -0
  162. package/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
  163. package/lib/integrations/hosts/pi/agent-tools.js +32 -0
  164. package/lib/integrations/hosts/pi/mcp.d.ts +4 -0
  165. package/lib/integrations/hosts/pi/mcp.js +23 -0
  166. package/lib/integrations/hosts/plugin-registry.d.ts +1 -0
  167. package/lib/integrations/hosts/plugin-registry.js +8 -1
  168. package/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
  169. package/lib/integrations/hosts/prompt-hooks.js +76 -0
  170. package/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
  171. package/lib/integrations/hosts/qoder/adapter.js +16 -0
  172. package/lib/integrations/installation/cli-update.js +146 -0
  173. package/lib/integrations/installation/upgraders/antigravity.js +67 -0
  174. package/lib/integrations/installation/upgraders/claude.js +123 -0
  175. package/lib/integrations/installation/upgraders/codex.js +73 -0
  176. package/lib/integrations/installation/upgraders/dsh.js +62 -0
  177. package/lib/integrations/installation/upgraders/pi.js +90 -0
  178. package/lib/integrations/installation/upgraders/qoder.js +67 -0
  179. package/lib/integrations/installation/upgraders/registry.js +40 -0
  180. package/lib/model/acp.d.ts +7 -3
  181. package/lib/model/acp.js +8 -0
  182. package/lib/model/agent.d.ts +18 -4
  183. package/lib/model/agent.js +4 -1
  184. package/lib/model/history.d.ts +10 -0
  185. package/lib/model/history.js +4 -1
  186. package/lib/model/installation.js +1 -0
  187. package/lib/model/plugin-host.d.ts +5 -0
  188. package/lib/model/workspace-config.d.ts +8 -12
  189. package/lib/model/workspace-config.js +2 -1
  190. package/lib/plugin.d.ts +2 -1
  191. package/lib/plugin.js +2 -2
  192. package/lib/qoder-plugin/.mcp.json +17 -0
  193. package/lib/qoder-plugin/.qoder-plugin/plugin.json +7 -0
  194. package/lib/qoder-plugin/DISCLAIMER.md +17 -0
  195. package/lib/qoder-plugin/LICENSE +21 -0
  196. package/lib/qoder-plugin/hooks/hooks.json +37 -0
  197. package/lib/qoder-plugin/skills/pbot/SKILL.md +9 -0
  198. package/lib/services/agents/agent-service.d.ts +15 -45
  199. package/lib/services/agents/agent-service.js +22 -3
  200. package/lib/services/history/history-capture.js +34 -22
  201. package/lib/services/history/history-migrate.js +26 -60
  202. package/lib/services/history/history-query.js +38 -57
  203. package/lib/services/history/summary-command.d.ts +1 -1
  204. package/lib/services/history/summary-command.js +8 -7
  205. package/lib/services/history/summary-registry.js +2 -3
  206. package/lib/services/history/summary-runner.d.ts +2 -5
  207. package/lib/services/history/summary-runner.js +72 -74
  208. package/lib/services/history/workspace-history.d.ts +3 -0
  209. package/lib/services/history/workspace-history.js +6 -0
  210. package/lib/services/installation/setup-service.js +4 -1
  211. package/lib/services/installation/upgrade-service.js +95 -0
  212. package/lib/services/workspace/plugin-releases.d.ts +15 -0
  213. package/lib/services/workspace/plugin-releases.js +126 -0
  214. package/lib/services/workspace/workspace-config.js +9 -8
  215. package/lib/services/workspace/workspace-mcp.js +22 -29
  216. package/lib/services/workspace/workspace-skills.d.ts +1 -0
  217. package/lib/services/workspace/workspace-skills.js +4 -2
  218. package/lib/storage/acp-files.js +7 -12
  219. package/lib/storage/agent-files.js +6 -5
  220. package/lib/storage/atomic-json.d.ts +3 -1
  221. package/lib/storage/atomic-json.js +43 -3
  222. package/lib/storage/history-files.d.ts +2 -1
  223. package/lib/storage/history-files.js +21 -15
  224. package/lib/storage/history-format.d.ts +2 -3
  225. package/lib/storage/history-format.js +5 -7
  226. package/lib/tools/history/contract.d.ts +3 -11
  227. package/lib/tools/history/contract.js +8 -7
  228. package/lib/ui/dsh/elements.d.ts +2 -5
  229. package/lib/ui/dsh/elements.js +3 -16
  230. package/lib/ui/dsh/management.js +9 -9
  231. package/lib/ui/dsh/styles.d.ts +1 -1
  232. package/lib/ui/dsh/styles.js +5 -33
  233. package/package.json +4 -1
  234. package/resources/antigravity/skills/pbot/SKILL.md +11 -1
  235. package/resources/claude/skills/pbot/SKILL.md +11 -1
  236. package/resources/plugin/skills/pbot/SKILL.md +9 -0
  237. package/resources/prompts/history-coordinator.md +1 -0
  238. package/resources/prompts/plugin-coordinator.md +1 -0
  239. package/scripts/README.md +3 -2
  240. package/src/common/validation.ts +10 -0
  241. package/src/dsh.ts +2 -2
  242. package/src/extension/history/command.ts +68 -0
  243. package/src/extension/hosts/dsh/acp-entry.ts +21 -0
  244. package/src/extension/hosts/dsh/acp-member.ts +4 -15
  245. package/src/extension/hosts/dsh/command.ts +6 -46
  246. package/src/extension/hosts/dsh/workspace.ts +12 -38
  247. package/src/extension/hosts/pi/acp-member.ts +4 -10
  248. package/src/extension/hosts/pi/command.ts +14 -63
  249. package/src/extension/hosts/pi/events.ts +2 -8
  250. package/src/extension/hosts/pi/lifecycle.ts +3 -2
  251. package/src/extension/hosts/plugin-runtime.ts +39 -19
  252. package/src/extension/hosts/workspace-mcp.ts +25 -0
  253. package/src/extension/mcp/bridge.ts +80 -80
  254. package/src/extension/mcp/tool-contract.ts +462 -0
  255. package/src/extension/mcp/workers.ts +81 -551
  256. package/src/extension/setup/cli.ts +11 -1
  257. package/src/extension/setup/upgrade.ts +158 -0
  258. package/src/extension/setup/workspace-cli.ts +167 -0
  259. package/src/integrations/acp/agents.ts +2 -3
  260. package/src/integrations/acp/harnesses/dsh.ts +1 -1
  261. package/src/integrations/acp/harnesses/presets.ts +8 -0
  262. package/src/integrations/acp/history-summary.ts +2 -1
  263. package/src/integrations/agent-resources.ts +1 -1
  264. package/src/integrations/hosts/claude/adapter.ts +7 -53
  265. package/src/integrations/hosts/codex/adapter.ts +16 -0
  266. package/src/integrations/hosts/plugin-registry.ts +9 -1
  267. package/src/integrations/hosts/prompt-hooks.ts +72 -0
  268. package/src/integrations/hosts/qoder/adapter.ts +19 -0
  269. package/src/integrations/installation/cli-update.ts +144 -0
  270. package/src/integrations/installation/pi-bridge.ts +1 -1
  271. package/src/integrations/installation/pi-setup.ts +1 -1
  272. package/src/integrations/installation/processes.ts +1 -1
  273. package/src/integrations/installation/upgraders/antigravity.ts +68 -0
  274. package/src/integrations/installation/upgraders/claude.ts +123 -0
  275. package/src/integrations/installation/upgraders/codex.ts +73 -0
  276. package/src/integrations/installation/upgraders/dsh.ts +63 -0
  277. package/src/integrations/installation/upgraders/pi.ts +87 -0
  278. package/src/integrations/installation/upgraders/qoder.ts +68 -0
  279. package/src/integrations/installation/upgraders/registry.ts +48 -0
  280. package/src/model/acp.ts +11 -3
  281. package/src/model/agent.ts +20 -4
  282. package/src/model/history.ts +5 -0
  283. package/src/model/installation.ts +86 -0
  284. package/src/model/plugin-host.ts +5 -1
  285. package/src/model/workspace-config.ts +6 -12
  286. package/src/pbot.ts +13 -32
  287. package/src/plugin.ts +3 -3
  288. package/src/services/agents/agent-service.ts +29 -17
  289. package/src/services/history/history-capture.ts +30 -22
  290. package/src/services/history/history-migrate.ts +23 -50
  291. package/src/services/history/history-query.ts +34 -48
  292. package/src/services/history/summary-command.ts +9 -8
  293. package/src/services/history/summary-registry.ts +2 -3
  294. package/src/services/history/summary-runner.ts +72 -81
  295. package/src/services/history/workspace-history.ts +8 -0
  296. package/src/services/installation/setup-service.ts +9 -43
  297. package/src/services/installation/upgrade-service.ts +127 -0
  298. package/src/services/workspace/plugin-releases.ts +121 -0
  299. package/src/services/workspace/workspace-config.ts +9 -8
  300. package/src/services/workspace/workspace-mcp.ts +23 -28
  301. package/src/services/workspace/workspace-skills.ts +5 -2
  302. package/src/storage/acp-files.ts +7 -12
  303. package/src/storage/agent-files.ts +6 -5
  304. package/src/storage/atomic-json.ts +31 -4
  305. package/src/storage/history-files.ts +22 -15
  306. package/src/storage/history-format.ts +6 -8
  307. package/src/tools/history/contract.ts +9 -9
  308. package/src/tools/history/pi-tool.ts +6 -4
  309. package/src/ui/dsh/elements.ts +5 -12
  310. package/src/ui/dsh/management.ts +10 -10
  311. package/src/ui/dsh/styles.ts +5 -33
  312. package/lib/antigravity-plugin/lib/services/history/history-migrate.d.ts +0 -12
  313. package/lib/antigravity-plugin/lib/services/history/history-migrate.js +0 -88
  314. package/lib/ui/presentation.d.ts +0 -3
  315. package/lib/ui/presentation.js +0 -12
  316. package/src/tools/access.ts +0 -7
  317. package/src/tools/register.ts +0 -21
  318. package/src/ui/presentation.ts +0 -12
@@ -1,21 +1,18 @@
1
- import { existsSync } from "node:fs";
2
1
  import { realpath } from "node:fs/promises";
3
- import { resolve } from "node:path";
4
2
  import * as McpClient from "@deepseek-ai/dsh-mcp-client";
5
3
  import { rootAgents } from "../../../integrations/hosts/dsh/root-agents.js";
6
4
  import { dshResources } from "../../../integrations/hosts/dsh/resources.js";
7
5
  import { registerDshFileEvents } from "./files.js";
8
6
  import { registerDshHistoryEvents } from "./history.js";
9
7
  import { registerDshHistoryTool } from "../../../tools/history/dsh-tool.js";
10
- import { resourceRoot } from "../../../common/paths.js";
11
8
  import { requireThat } from "../../../common/validation.js";
12
9
  import { toDshMcpConfig } from "../../../integrations/hosts/dsh/mcp.js";
13
10
  import { registerDshSkills } from "../../../integrations/hosts/dsh/skills.js";
11
+ import { bindWorkspacePlugin } from "../../../services/workspace/plugin-releases.js";
14
12
  import { readWorkspaceConfig, workspaceDisabledMessage, workspaceFeatures } from "../../../services/workspace/workspace-config.js";
15
- import { readWorkspaceMcpConfig, resolveWorkspaceMcpServer } from "../../../services/workspace/workspace-mcp.js";
13
+ import { loadNativeWorkspaceServers } from "../workspace-mcp.js";
16
14
  import { initializeWorkspace } from "../../../services/workspace/workspace-preparation.js";
17
- import { checkHistoryMigrationNotice } from "../../../services/history/history-migrate.js";
18
- import { checkHistorySummaryReminder } from "../../../services/history/summary-runner.js";
15
+ import { historyStartupNotices } from "../../../services/history/summary-runner.js";
19
16
  import { McpWorkerRuntime, defaultWorkerDirectory } from "../../mcp/workers.js";
20
17
  export const dshDependencies = ["agents", "sessions", "sessionPersistence", "sessionQuery", "llm", "tools", "systemPrompt", "skills", "fs", "workingDirectory", "approval"];
21
18
  export class DshWorkspace {
@@ -39,8 +36,6 @@ export class DshWorkspace {
39
36
  check = () => { this.component.throwIfAborted(); this.lifetime.signal.throwIfAborted(); };
40
37
  async open() {
41
38
  this.check();
42
- const mcpEntry = resolve(resourceRoot, "../lib/extension/mcp.js");
43
- const workerDir = defaultWorkerDirectory();
44
39
  this.#scope = await this.agent.ctx.plugin({
45
40
  name: "pbot-coordinator",
46
41
  inject: dshDependencies,
@@ -53,32 +48,20 @@ export class DshWorkspace {
53
48
  if (this.features.history) {
54
49
  registerDshHistoryTool(ctx, this.agent, this.root, this.lifetime.signal, this.check);
55
50
  registerDshHistoryEvents(ctx, this.agent, this.root);
56
- try {
57
- const migrationNotice = checkHistoryMigrationNotice(this.root);
58
- if (migrationNotice)
59
- this.ctx.logger.info(migrationNotice);
60
- const reminder = checkHistorySummaryReminder(this.root);
61
- if (reminder)
62
- this.ctx.logger.info(reminder.message);
63
- }
64
- catch { /* Never block workspace startup if history reminder probe fails. */ }
51
+ for (const notice of historyStartupNotices(this.root))
52
+ this.ctx.logger.info(notice);
65
53
  }
66
- if (existsSync(mcpEntry)) {
54
+ const server = this.workers.nativeServer(this.root);
55
+ if (server) {
67
56
  await ctx.plugin(McpClient, McpClient.Config({
68
57
  transport: "stdio",
69
58
  serverName: "pbot",
70
- command: process.execPath,
71
- args: [mcpEntry],
59
+ ...server,
72
60
  env: {
73
61
  ...(process.env.PATH ? { PATH: process.env.PATH } : {}),
74
62
  ...(process.env.HOME ? { HOME: process.env.HOME } : {}),
75
63
  ...(process.env.DSH_HOME ? { DSH_HOME: process.env.DSH_HOME } : {}),
76
- PBOT_HOST: "dsh",
77
- PBOT_WORKSPACE_ROOT: this.root,
78
- PBOT_MCP_AGENT_DIR: workerDir,
79
- PBOT_MCP_SESSION_ID: this.agent.id,
80
- PBOT_NATIVE_WORKSPACE_MCP: "1",
81
- PBOT_NATIVE_HISTORY: "1",
64
+ ...server.env,
82
65
  },
83
66
  cwd: this.root,
84
67
  toolCallTimeoutMs: 600_000,
@@ -86,24 +69,9 @@ export class DshWorkspace {
86
69
  }));
87
70
  }
88
71
  if (this.features.mcp) {
89
- const mcpConfig = await readWorkspaceMcpConfig(this.root);
90
- this.check();
91
- if (mcpConfig.error) {
92
- this.ctx.logger.warn(mcpConfig.error);
93
- }
94
- else {
95
- for (const [serverName, server] of Object.entries(mcpConfig.servers)) {
96
- if (server.enabled === false)
97
- continue;
98
- try {
99
- const resolved = resolveWorkspaceMcpServer(this.root, serverName, server);
100
- await ctx.plugin(McpClient, toDshMcpConfig(resolved));
101
- }
102
- catch (error) {
103
- this.ctx.logger.warn(error instanceof Error ? error.message : String(error));
104
- }
105
- }
106
- }
72
+ await loadNativeWorkspaceServers(this.root, async (server) => {
73
+ await ctx.plugin(McpClient, toDshMcpConfig(server));
74
+ }, { check: this.check, warn: message => this.ctx.logger.warn(message) });
107
75
  }
108
76
  },
109
77
  });
@@ -169,6 +137,7 @@ export class DshWorkspaces {
169
137
  }
170
138
  let workspace;
171
139
  try {
140
+ bindWorkspacePlugin(root, "dsh");
172
141
  workspace = new DshWorkspace(this.ctx, root, agent, this.#lifetime.signal, workspaceFeatures(configuration));
173
142
  const owned = workspace;
174
143
  const dispose = agent.ctx.effect(() => async () => {
@@ -0,0 +1,3 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ /** Member resources and authoritative final answers; ACP owns input delivery. */
3
+ export default function piAcpMember(pi: ExtensionAPI): Promise<void>;
@@ -0,0 +1,100 @@
1
+ import { stat } from "node:fs/promises";
2
+ import { realpathSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { isMcpWorkerTool } from "../../../model/acp.js";
5
+ import { acpMember } from "../../acp/member.js";
6
+ import { agentFileTools } from "../../../integrations/hosts/pi/agent-tools.js";
7
+ import { toPiMcpConfig } from "../../../integrations/hosts/pi/mcp.js";
8
+ import { authorizeMutation } from "../../../services/workspace/workspace-files.js";
9
+ import { loadNativeWorkspaceServers } from "../workspace-mcp.js";
10
+ import { resourceRoot } from "../../../common/paths.js";
11
+ import { launch } from "../../../storage/agent-files.js";
12
+ import { requireThat } from "../../../common/validation.js";
13
+ const isBlockedTool = (name) => name === "pbot_history" || isMcpWorkerTool(name);
14
+ /** Member resources and authoritative final answers; ACP owns input delivery. */
15
+ export default async function piAcpMember(pi) {
16
+ const member = await acpMember(), lifetime = new AbortController();
17
+ const registeredMcp = [];
18
+ let input, startLeaf = null;
19
+ const checkSession = (ctx) => {
20
+ const reference = member.files.reference(member.id);
21
+ requireThat(reference.host === "acp" && reference.backend === "pi" && realpathSync(ctx.cwd) === member.d.cwd &&
22
+ (reference.sessionId === null || reference.sessionId === ctx.sessionManager.getSessionId()), "Pi ACP 会话身份或工作目录不匹配。");
23
+ };
24
+ const workspaceMcpServers = [];
25
+ if (member.features.mcp && process.env.PBOT_ACP_VALIDATE !== "1") {
26
+ await loadNativeWorkspaceServers(member.d.workspace, server => { workspaceMcpServers.push(server); });
27
+ }
28
+ pi.on("resources_discover", async () => {
29
+ if (!member.features.skills || member.d.options.noSkills)
30
+ return {};
31
+ const skillPaths = [join(resourceRoot, "skills")], workspaceSkills = join(member.d.workspace, ".agents/skills");
32
+ try {
33
+ if ((await stat(workspaceSkills)).isDirectory())
34
+ skillPaths.push(workspaceSkills);
35
+ }
36
+ catch (error) {
37
+ if (error.code !== "ENOENT")
38
+ throw error;
39
+ }
40
+ return { skillPaths };
41
+ });
42
+ pi.on("session_start", (_event, ctx) => {
43
+ checkSession(ctx);
44
+ const active = pi.getActiveTools();
45
+ for (const tool of agentFileTools(member.d.cwd, lifetime.signal, async (path, change, signal) => {
46
+ member.check();
47
+ await authorizeMutation(member.d.workspace, path, change, async () => false, signal);
48
+ member.check();
49
+ }))
50
+ pi.registerTool(tool);
51
+ for (const server of workspaceMcpServers) {
52
+ pi.registerMcpServer(server.name, toPiMcpConfig(server));
53
+ registeredMcp.push(server.name);
54
+ }
55
+ pi.setActiveTools(active.filter(name => !isBlockedTool(name)));
56
+ pi.setSessionName(member.files.read(member.id, "launch.json", launch).name);
57
+ startLeaf = ctx.sessionManager.getLeafId();
58
+ });
59
+ pi.on("before_agent_start", async (event, ctx) => {
60
+ try {
61
+ checkSession(ctx);
62
+ const current = member.input();
63
+ if (current.requestId !== input?.requestId) {
64
+ input = current;
65
+ startLeaf = ctx.sessionManager.getLeafId();
66
+ }
67
+ if (!member.features.prompts)
68
+ return;
69
+ const context = await member.context();
70
+ member.check();
71
+ Object.assign(event.systemPromptOptions.sections, { pbot_workspace: context.workspace, pbot_child: context.role, pbot_environment: context.environment });
72
+ }
73
+ catch (error) {
74
+ ctx.abort();
75
+ throw error;
76
+ }
77
+ });
78
+ pi.on("tool_call", event => {
79
+ member.check();
80
+ if (isBlockedTool(event.toolName))
81
+ return { block: true, reason: "专家不提供协调管理工具。" };
82
+ return;
83
+ });
84
+ pi.on("agent_settled", (event, ctx) => {
85
+ if (!input)
86
+ return;
87
+ const branch = ctx.sessionManager.getBranch(), at = startLeaf === null ? -1 : branch.findIndex(entry => entry.id === startLeaf);
88
+ const last = branch.slice(at + 1).findLast(entry => entry.type === "message" && entry.message.role === "assistant");
89
+ const assistant = last?.type === "message" && last.message.role === "assistant" ? last.message : undefined;
90
+ const answer = assistant?.content.filter(block => block.type === "text").map(block => block.text).join("\n") ?? "";
91
+ member.answer(input, event.aborted ? "cancelled" : assistant?.stopReason === "stop" ? "completed" : "failed", answer, assistant?.errorMessage);
92
+ });
93
+ pi.on("session_before_switch", () => ({ cancel: true }));
94
+ pi.on("session_before_fork", () => ({ cancel: true }));
95
+ pi.on("session_shutdown", () => {
96
+ for (const name of registeredMcp.splice(0))
97
+ pi.unregisterMcpServer(name);
98
+ lifetime.abort();
99
+ });
100
+ }
@@ -5,6 +5,8 @@ export declare function pluginCommand(adapter: PluginHostAdapter, args: readonly
5
5
  resourceRoot: string;
6
6
  configuration: import("../../model/workspace-config.js").WorkspaceConfiguration;
7
7
  }>;
8
- export declare function runPluginHost(adapter: PluginHostAdapter, args: readonly string[]): Promise<void>;
8
+ export type ReplayPluginHook = (root: string, input: string) => Promise<number | undefined>;
9
+ export declare function runPluginHost(adapter: PluginHostAdapter, args: readonly string[], replay?: ReplayPluginHook): Promise<void>;
9
10
  /** Shared hook execution engine; host-specific protocols stay in PluginHostAdapter implementations. */
10
11
  export declare function pluginHook(adapter: PluginHostAdapter, payload: unknown): Promise<Record<string, unknown>>;
12
+ export declare function pluginHook(adapter: PluginHostAdapter, payload: unknown, replay: ((root: string) => Promise<number | undefined>) | undefined): Promise<Record<string, unknown> | number>;
@@ -1,13 +1,14 @@
1
+ import { join } from "node:path";
1
2
  import { resourceRoot } from "../../common/paths.js";
2
3
  import { packageVersion } from "../../common/version.js";
3
4
  import { requireThat } from "../../common/validation.js";
4
5
  import { loadWorkspaceContext } from "../../integrations/agent-resources.js";
5
6
  import { recordHistoryCapture } from "../../services/history/history-capture.js";
6
- import { checkHistoryMigrationNotice } from "../../services/history/history-migrate.js";
7
- import { checkHistorySummaryReminder } from "../../services/history/summary-runner.js";
7
+ import { historyStartupNotices } from "../../services/history/summary-runner.js";
8
+ import { bindWorkspacePlugin, workspacePluginDrift, workspacePluginReleases } from "../../services/workspace/plugin-releases.js";
8
9
  import { readWorkspaceConfig, workspaceFeatures } from "../../services/workspace/workspace-config.js";
9
10
  import { initializeWorkspace } from "../../services/workspace/workspace-preparation.js";
10
- import { discoverWorkspaceSkills } from "../../services/workspace/workspace-skills.js";
11
+ import { discoverSkills, discoverWorkspaceSkills } from "../../services/workspace/workspace-skills.js";
11
12
  function disabledMessage(adapter, configuration) {
12
13
  return configuration.error ?? `Personal Bot 未在此工作区启用。运行 ${adapter.initCommandHint};配置文件:${configuration.path}`;
13
14
  }
@@ -15,25 +16,30 @@ export async function pluginCommand(adapter, args, cwd) {
15
16
  const root = await adapter.resolveRoot(cwd);
16
17
  const action = args.join(" ");
17
18
  requireThat(["init", "status"].includes(action), adapter.usage);
18
- if (action === "init")
19
+ if (action === "init") {
19
20
  await initializeWorkspace(root);
21
+ bindWorkspacePlugin(root, adapter.host, process.env.PBOT_PLUGIN_MANIFEST || process.env.PBOT_PLUGIN_VERSION);
22
+ }
20
23
  const configuration = await readWorkspaceConfig(root);
21
24
  return { version: packageVersion, workspaceRoot: root, resourceRoot, configuration };
22
25
  }
23
- export async function runPluginHost(adapter, args) {
26
+ export async function runPluginHost(adapter, args, replay) {
24
27
  if (args[0] === "hook") {
25
28
  let input = "";
26
29
  process.stdin.setEncoding("utf8");
27
30
  for await (const chunk of process.stdin)
28
31
  input += String(chunk);
29
- process.stdout.write(JSON.stringify(await pluginHook(adapter, JSON.parse(input))) + "\n");
32
+ const result = await pluginHook(adapter, JSON.parse(input), replay && (root => replay(root, input)));
33
+ if (typeof result === "number")
34
+ process.exitCode = result;
35
+ else
36
+ process.stdout.write(JSON.stringify(result) + "\n");
30
37
  }
31
38
  else {
32
39
  process.stdout.write(JSON.stringify(await pluginCommand(adapter, args, process.cwd()), null, 2) + "\n");
33
40
  }
34
41
  }
35
- /** Shared hook execution engine; host-specific protocols stay in PluginHostAdapter implementations. */
36
- export async function pluginHook(adapter, payload) {
42
+ export async function pluginHook(adapter, payload, replay) {
37
43
  if (process.env.PBOT_SUBAGENT_RUN_ID)
38
44
  return adapter.formatPass(payload);
39
45
  const event = adapter.parseEvent(payload);
@@ -41,6 +47,11 @@ export async function pluginHook(adapter, payload) {
41
47
  return adapter.formatPass(payload);
42
48
  const root = await adapter.resolveRoot(event.cwd, event.raw);
43
49
  const configuration = await readWorkspaceConfig(root);
50
+ if (configuration.enabled && replay) {
51
+ const code = await replay(root);
52
+ if (code !== undefined)
53
+ return code;
54
+ }
44
55
  const features = workspaceFeatures(configuration);
45
56
  if (event.kind === "check-skill") {
46
57
  if (!configuration.enabled)
@@ -51,6 +62,8 @@ export async function pluginHook(adapter, payload) {
51
62
  }
52
63
  if (!configuration.enabled)
53
64
  return adapter.formatPass(payload);
65
+ if (event.kind === "inject-context")
66
+ bindWorkspacePlugin(root, adapter.host, process.env.PBOT_PLUGIN_MANIFEST || process.env.PBOT_PLUGIN_VERSION);
54
67
  if (features.history && adapter.extractHistory) {
55
68
  try {
56
69
  const capture = await adapter.extractHistory(event);
@@ -61,30 +74,32 @@ export async function pluginHook(adapter, payload) {
61
74
  }
62
75
  if (event.kind === "record-history")
63
76
  return adapter.formatPass(payload);
64
- const workspaceSkills = features.skills && !adapter.nativeWorkspaceSkills
77
+ const includePrompts = features.prompts && !event.noticesOnly;
78
+ const includeSkills = features.skills && !event.noticesOnly;
79
+ const workspaceSkills = includeSkills && !adapter.nativeWorkspaceSkills
65
80
  ? await discoverWorkspaceSkills(root)
66
81
  : [];
67
- const notices = [];
68
- if (event.isSessionStart && features.history) {
69
- try {
70
- const migrationNotice = checkHistoryMigrationNotice(root);
71
- if (migrationNotice)
72
- notices.push(migrationNotice);
73
- const reminder = checkHistorySummaryReminder(root);
74
- if (reminder)
75
- notices.push(reminder.message);
82
+ if (includeSkills && adapter.indexBundledSkills) {
83
+ const directory = join(resourceRoot, "skills");
84
+ workspaceSkills.push(...await discoverSkills(directory, directory));
85
+ }
86
+ const notices = event.isSessionStart && features.history ? [...historyStartupNotices(root)] : [];
87
+ if (event.isSessionStart) {
88
+ const drift = workspacePluginDrift(workspacePluginReleases(root));
89
+ if (drift.outOfSync) {
90
+ const summary = Object.entries(drift.versions).map(([host, version]) => `${host}=${version}`).join(", ");
91
+ notices.push(`检测到当前工作区各宿主 Personal Bot 插件版本不一致(${summary}),可在终端运行 pbot upgrade 统一升级。`);
76
92
  }
77
- catch { /* Never block session start if history reminder probe fails. */ }
78
93
  }
79
- if (!features.prompts && !workspaceSkills.length && !notices.length)
94
+ if (!includePrompts && !workspaceSkills.length && !notices.length)
80
95
  return adapter.formatPass(payload);
81
96
  const context = await loadWorkspaceContext({
82
97
  host: adapter.host,
83
98
  root,
84
99
  role: event.role,
85
100
  ...(adapter.coordinatorPrompt ? { coordinatorPrompt: adapter.coordinatorPrompt } : {}),
86
- includePrompts: features.prompts,
87
- includeRootAgents: features.prompts && !adapter.nativeRootAgents,
101
+ includePrompts,
102
+ includeRootAgents: includePrompts && !adapter.nativeRootAgents,
88
103
  ...(event.localRoleOnly ? { localRoleOnly: true } : {}),
89
104
  workspaceSkills,
90
105
  notices,
@@ -0,0 +1,6 @@
1
+ import type { ResolvedWorkspaceMcpServer } from "../../model/workspace-mcp.js";
2
+ /** A native host can keep its other tools when one workspace server cannot be mounted. */
3
+ export declare function loadNativeWorkspaceServers(root: string, mount: (server: ResolvedWorkspaceMcpServer) => void | Promise<void>, options?: {
4
+ check?: () => void;
5
+ warn?: (message: string) => void;
6
+ }): Promise<void>;
@@ -0,0 +1,21 @@
1
+ import { readWorkspaceMcpConfig, resolveWorkspaceMcpServer } from "../../services/workspace/workspace-mcp.js";
2
+ /** A native host can keep its other tools when one workspace server cannot be mounted. */
3
+ export async function loadNativeWorkspaceServers(root, mount, options = {}) {
4
+ const config = await readWorkspaceMcpConfig(root);
5
+ options.check?.();
6
+ if (config.error) {
7
+ options.warn?.(config.error);
8
+ return;
9
+ }
10
+ for (const [name, server] of Object.entries(config.servers)) {
11
+ if (server.enabled === false)
12
+ continue;
13
+ options.check?.();
14
+ try {
15
+ await mount(resolveWorkspaceMcpServer(root, name, server));
16
+ }
17
+ catch (error) {
18
+ options.warn?.(error instanceof Error ? error.message : String(error));
19
+ }
20
+ }
21
+ }
@@ -2,6 +2,12 @@ import { resolveWorkspaceMcpServers } from "../../services/workspace/workspace-m
2
2
  export declare class WorkspaceMcpBridge {
3
3
  #private;
4
4
  discover(root: string, serverName?: string): Promise<{
5
+ tools: unknown[];
6
+ description?: string;
7
+ workspace: string;
8
+ server: string;
9
+ transport: "stdio" | "http";
10
+ } | {
5
11
  workspace: string;
6
12
  configPath: string;
7
13
  servers: ({
@@ -20,14 +26,6 @@ export declare class WorkspaceMcpBridge {
20
26
  transport: string;
21
27
  enabled: boolean;
22
28
  })[];
23
- } | {
24
- tools: unknown[];
25
- description?: string;
26
- workspace: string;
27
- server: string;
28
- transport: "stdio" | "http";
29
- configPath?: never;
30
- servers?: never;
31
29
  }>;
32
30
  call(root: string, serverName: string, toolName: string, args?: Record<string, unknown>): Promise<{
33
31
  structuredContent?: {} | null;
@@ -260,70 +260,66 @@ class HttpMcpClient {
260
260
  }
261
261
  }
262
262
  }
263
+ function configuredServer(root, configuration, serverName) {
264
+ const raw = Object.hasOwn(configuration.servers, serverName) ? configuration.servers[serverName] : undefined;
265
+ requireThat(raw, `工作区未注册名为 "${serverName}" 的 MCP 服务(已注册:${Object.keys(configuration.servers).join(", ") || "无"})。`);
266
+ requireThat(raw.enabled !== false, `工作区 MCP 服务 "${serverName}" 当前已停用(enabled: false)。`);
267
+ return resolveWorkspaceMcpServer(root, serverName, raw);
268
+ }
263
269
  export class WorkspaceMcpBridge {
264
- #clients = new Map();
265
- #connecting = new Map();
266
- #closed = false;
267
- #key(root, name) {
268
- return `${root}\0${name}`;
270
+ #connections = new Set();
271
+ #closing;
272
+ #close(connection) {
273
+ return connection.closing ??= connection.ready.catch(() => { }).then(() => connection.client.close())
274
+ .finally(() => this.#connections.delete(connection));
275
+ }
276
+ #retire(connection) {
277
+ connection.retired = true;
278
+ if (!connection.users)
279
+ return this.#close(connection);
269
280
  }
270
- async #connect(root, server) {
271
- requireThat(!this.#closed, "Workspace MCP 桥接已关闭。");
272
- const key = this.#key(root, server.name);
281
+ async #withClient(root, server, operation) {
282
+ requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
283
+ const key = `${root}\0${server.name}`;
273
284
  const fingerprint = JSON.stringify(server);
274
- const existing = this.#clients.get(key);
275
- if (existing && existing.alive && existing.fingerprint === fingerprint)
276
- return existing;
277
- if (existing) {
278
- this.#clients.delete(key);
279
- await existing.close().catch(() => { });
285
+ let connection = [...this.#connections].find(entry => entry.key === key && !entry.retired);
286
+ if (connection && (!connection.client.alive || connection.client.fingerprint !== fingerprint)) {
287
+ void this.#retire(connection)?.catch(() => { });
288
+ connection = undefined;
280
289
  }
281
- const inflight = this.#connecting.get(key);
282
- if (inflight)
283
- return inflight;
284
- const creating = (async () => {
290
+ if (!connection) {
285
291
  const client = server.transport === "http"
286
292
  ? new HttpMcpClient(server, fingerprint)
287
293
  : new StdioMcpClient(server, fingerprint);
294
+ connection = { key, client, ready: client.initialize(), users: 0, retired: false };
295
+ this.#connections.add(connection);
296
+ }
297
+ connection.users++;
298
+ try {
288
299
  try {
289
- await client.initialize();
290
- if (this.#closed) {
291
- await client.close();
292
- throw new Error("Workspace MCP 桥接已关闭。");
293
- }
294
- this.#clients.set(key, client);
295
- return client;
300
+ await connection.ready;
296
301
  }
297
302
  catch (error) {
298
- await client.close().catch(() => { });
303
+ connection.retired = true;
299
304
  throw error;
300
305
  }
301
- })();
302
- this.#connecting.set(key, creating);
303
- try {
304
- return await creating;
306
+ requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
307
+ return await operation(connection.client);
305
308
  }
306
309
  finally {
307
- if (this.#connecting.get(key) === creating)
308
- this.#connecting.delete(key);
310
+ connection.users--;
311
+ if (connection.retired && !connection.users)
312
+ await this.#close(connection).catch(() => { });
309
313
  }
310
314
  }
311
315
  async #pruneRemoved(root, activeNames) {
312
316
  const prefix = `${root}\0`;
313
- const stale = [];
314
- for (const [key, client] of this.#clients) {
315
- if (!key.startsWith(prefix))
316
- continue;
317
- const name = key.slice(prefix.length);
318
- if (!activeNames.has(name)) {
319
- this.#clients.delete(key);
320
- stale.push(client);
321
- }
322
- }
323
- await Promise.allSettled(stale.map(client => client.close()));
317
+ await Promise.allSettled([...this.#connections]
318
+ .filter(entry => entry.key.startsWith(prefix) && !activeNames.has(entry.key.slice(prefix.length)))
319
+ .map(entry => this.#retire(entry)));
324
320
  }
325
321
  async discover(root, serverName) {
326
- requireThat(!this.#closed, "Workspace MCP 桥接已关闭。");
322
+ requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
327
323
  const configuration = await readWorkspaceMcpConfig(root);
328
324
  if (configuration.error)
329
325
  throw new Error(configuration.error);
@@ -346,41 +342,35 @@ export class WorkspaceMcpBridge {
346
342
  servers,
347
343
  };
348
344
  }
349
- const raw = Object.hasOwn(configuration.servers, serverName) ? configuration.servers[serverName] : undefined;
350
- requireThat(raw, `工作区未注册名为 "${serverName}" 的 MCP 服务(已注册:${Object.keys(configuration.servers).join(", ") || "无"})。`);
351
- requireThat(raw.enabled !== false, `工作区 MCP 服务 "${serverName}" 当前已停用(enabled: false)。`);
352
- const resolved = resolveWorkspaceMcpServer(root, serverName, raw);
353
- const client = await this.#connect(root, resolved);
354
- const tools = [];
355
- let cursor;
356
- for (;;) {
357
- const page = await client.request("tools/list", cursor ? { cursor } : {});
358
- if (object(page) && Array.isArray(page.tools))
359
- tools.push(...page.tools);
360
- const next = object(page) && typeof page.nextCursor === "string" && page.nextCursor ? page.nextCursor : undefined;
361
- if (!next)
362
- break;
363
- cursor = next;
364
- }
365
- return {
366
- workspace: root,
367
- server: resolved.name,
368
- transport: resolved.transport,
369
- ...(resolved.description ? { description: resolved.description } : {}),
370
- tools,
371
- };
345
+ const resolved = configuredServer(root, configuration, serverName);
346
+ return this.#withClient(root, resolved, async (client) => {
347
+ const tools = [];
348
+ let cursor;
349
+ for (;;) {
350
+ const page = await client.request("tools/list", cursor ? { cursor } : {});
351
+ if (object(page) && Array.isArray(page.tools))
352
+ tools.push(...page.tools);
353
+ const next = object(page) && typeof page.nextCursor === "string" && page.nextCursor ? page.nextCursor : undefined;
354
+ if (!next)
355
+ break;
356
+ cursor = next;
357
+ }
358
+ return {
359
+ workspace: root,
360
+ server: resolved.name,
361
+ transport: resolved.transport,
362
+ ...(resolved.description ? { description: resolved.description } : {}),
363
+ tools,
364
+ };
365
+ });
372
366
  }
373
367
  async call(root, serverName, toolName, args = {}) {
374
- requireThat(!this.#closed, "Workspace MCP 桥接已关闭。");
368
+ requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
375
369
  const configuration = await readWorkspaceMcpConfig(root);
376
370
  if (configuration.error)
377
371
  throw new Error(configuration.error);
378
- const raw = Object.hasOwn(configuration.servers, serverName) ? configuration.servers[serverName] : undefined;
379
- requireThat(raw, `工作区未注册名为 "${serverName}" 的 MCP 服务(已注册:${Object.keys(configuration.servers).join(", ") || "无"})。`);
380
- requireThat(raw.enabled !== false, `工作区 MCP 服务 "${serverName}" 当前已停用(enabled: false)。`);
381
- const resolved = resolveWorkspaceMcpServer(root, serverName, raw);
382
- const client = await this.#connect(root, resolved);
383
- const result = await client.request("tools/call", { name: toolName, arguments: args });
372
+ const resolved = configuredServer(root, configuration, serverName);
373
+ const result = await this.#withClient(root, resolved, client => client.request("tools/call", { name: toolName, arguments: args }));
384
374
  if (object(result)) {
385
375
  return {
386
376
  server: resolved.name,
@@ -397,16 +387,8 @@ export class WorkspaceMcpBridge {
397
387
  content: result === undefined ? [] : [{ type: "text", text: JSON.stringify(result) }],
398
388
  };
399
389
  }
400
- async close() {
401
- if (this.#closed)
402
- return;
403
- this.#closed = true;
404
- const inflight = [...this.#connecting.values()];
405
- this.#connecting.clear();
406
- await Promise.allSettled(inflight);
407
- const clients = [...this.#clients.values()];
408
- this.#clients.clear();
409
- await Promise.allSettled(clients.map(client => client.close()));
390
+ close() {
391
+ return this.#closing ??= Promise.allSettled([...this.#connections].map(entry => this.#close(entry))).then(() => { });
410
392
  }
411
393
  }
412
394
  export { resolveWorkspaceMcpServers };