personal-bot 0.2.20 → 0.3.1

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 (343) hide show
  1. package/.claude-plugin/marketplace.json +3 -3
  2. package/.claude-plugin/plugin.json +2 -5
  3. package/.mcp.json +11 -0
  4. package/CONTEXT.md +17 -73
  5. package/README.md +41 -120
  6. package/README.zh-CN.md +41 -120
  7. package/docs/README.md +31 -23
  8. package/docs/architecture/design.md +74 -0
  9. package/docs/architecture/extensibility.md +78 -0
  10. package/docs/architecture/model-context.md +31 -0
  11. package/docs/architecture/model-instruction-guidelines.md +34 -0
  12. package/docs/architecture/principles.md +33 -0
  13. package/docs/guides/capabilities.md +36 -0
  14. package/docs/guides/cross-harness-workers.md +83 -0
  15. package/docs/guides/history.md +25 -0
  16. package/docs/guides/workspace-configuration.md +102 -0
  17. package/docs/hosts/antigravity.md +52 -0
  18. package/docs/hosts/claude-code.md +47 -0
  19. package/docs/hosts/dsh.md +67 -0
  20. package/docs/hosts/pi.md +49 -0
  21. package/docs/maintainers/release.md +59 -0
  22. package/hooks/hooks.json +0 -3
  23. package/lib/antigravity-plugin/hooks.json +23 -0
  24. package/lib/antigravity-plugin/lib/claude.d.ts +1 -0
  25. package/lib/antigravity-plugin/lib/claude.js +13 -0
  26. package/lib/antigravity-plugin/lib/common/json.d.ts +6 -0
  27. package/lib/antigravity-plugin/lib/common/json.js +16 -0
  28. package/lib/antigravity-plugin/lib/common/paths.d.ts +1 -0
  29. package/lib/antigravity-plugin/lib/common/paths.js +2 -0
  30. package/lib/antigravity-plugin/lib/common/validation.d.ts +3 -0
  31. package/lib/antigravity-plugin/lib/common/validation.js +8 -0
  32. package/lib/antigravity-plugin/lib/common/version.d.ts +3 -0
  33. package/lib/antigravity-plugin/lib/common/version.js +13 -0
  34. package/lib/antigravity-plugin/lib/extension/acp-worker.js +11597 -0
  35. package/lib/antigravity-plugin/lib/extension/mcp.js +2715 -0
  36. package/lib/antigravity-plugin/lib/extension/plugin-runtime.d.ts +10 -0
  37. package/lib/antigravity-plugin/lib/extension/plugin-runtime.js +67 -0
  38. package/lib/antigravity-plugin/lib/integrations/agent-resources.d.ts +16 -0
  39. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +56 -0
  40. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.d.ts +4 -0
  41. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.js +79 -0
  42. package/lib/antigravity-plugin/lib/integrations/hosts/claude.d.ts +3 -0
  43. package/lib/antigravity-plugin/lib/integrations/hosts/claude.js +60 -0
  44. package/lib/antigravity-plugin/lib/integrations/hosts/registry.d.ts +15 -0
  45. package/lib/antigravity-plugin/lib/integrations/hosts/registry.js +34 -0
  46. package/lib/antigravity-plugin/lib/model/acp.d.ts +164 -0
  47. package/lib/antigravity-plugin/lib/model/acp.js +12 -0
  48. package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +48 -0
  49. package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +24 -0
  50. package/lib/antigravity-plugin/lib/model/workspace-config.js +1 -0
  51. package/lib/antigravity-plugin/lib/model/workspace-mcp.d.ts +51 -0
  52. package/lib/antigravity-plugin/lib/model/workspace-mcp.js +4 -0
  53. package/lib/antigravity-plugin/lib/plugin.d.ts +1 -0
  54. package/lib/antigravity-plugin/lib/plugin.js +17 -0
  55. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.d.ts +8 -0
  56. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +95 -0
  57. package/lib/antigravity-plugin/lib/services/workspace/workspace-preparation.d.ts +3 -0
  58. package/lib/antigravity-plugin/lib/services/workspace/workspace-preparation.js +26 -0
  59. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.d.ts +8 -0
  60. package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.js +91 -0
  61. package/lib/antigravity-plugin/lib/storage/atomic-json.d.ts +11 -0
  62. package/lib/antigravity-plugin/lib/storage/atomic-json.js +101 -0
  63. package/lib/antigravity-plugin/mcp_config.json +13 -0
  64. package/lib/antigravity-plugin/package.json +6 -0
  65. package/lib/antigravity-plugin/plugin.json +7 -0
  66. package/lib/antigravity-plugin/resources/agents/coordinator.md +11 -0
  67. package/{resources/claude → lib/antigravity-plugin/resources}/agents/general-purpose.md +0 -5
  68. package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -0
  69. package/lib/antigravity-plugin/resources/prompts/workspace.md +31 -0
  70. package/lib/antigravity-plugin/resources/references/compute-finance.md +22 -0
  71. package/lib/antigravity-plugin/resources/references/compute.md +16 -0
  72. package/lib/antigravity-plugin/resources/references/task-conductor.md +25 -0
  73. package/lib/antigravity-plugin/resources/templates/workspace-agents.md +21 -0
  74. package/lib/antigravity-plugin/skills/pbot/SKILL.md +16 -0
  75. package/lib/antigravity-plugin/skills/project-maintenance/SKILL.md +35 -0
  76. package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +42 -0
  77. package/lib/claude.js +4 -11
  78. package/lib/client.js +14 -531
  79. package/lib/common/markdown.js +2 -3
  80. package/lib/common/validation.d.ts +2 -0
  81. package/lib/common/validation.js +4 -0
  82. package/lib/extension/acp-member.d.ts +2 -0
  83. package/lib/extension/acp-member.js +9 -8
  84. package/lib/extension/acp-worker.js +11578 -121
  85. package/lib/extension/dsh/acp-member.js +26 -2
  86. package/lib/extension/dsh/command.js +19 -57
  87. package/lib/extension/dsh/history.js +2 -4
  88. package/lib/extension/dsh/management.d.ts +0 -2
  89. package/lib/extension/dsh/management.js +27 -126
  90. package/lib/extension/dsh/workspace.d.ts +5 -47
  91. package/lib/extension/dsh/workspace.js +76 -108
  92. package/lib/extension/mcp-bridge.d.ts +41 -0
  93. package/lib/extension/mcp-bridge.js +412 -0
  94. package/lib/extension/mcp-workers.d.ts +980 -0
  95. package/lib/extension/mcp-workers.js +882 -0
  96. package/lib/extension/mcp.js +2715 -0
  97. package/lib/extension/plugin-runtime.d.ts +10 -0
  98. package/lib/extension/plugin-runtime.js +67 -0
  99. package/lib/extension/setup-cli.js +215 -2
  100. package/lib/integrations/acp/agents.d.ts +10 -11
  101. package/lib/integrations/acp/agents.js +65 -33
  102. package/lib/integrations/acp/claude.d.ts +2 -2
  103. package/lib/integrations/acp/claude.js +5 -3
  104. package/lib/integrations/acp/connection.d.ts +17 -4
  105. package/lib/integrations/acp/connection.js +53 -27
  106. package/lib/integrations/acp/executable.d.ts +3 -0
  107. package/lib/integrations/acp/executable.js +42 -0
  108. package/lib/integrations/acp/harnesses/claude.d.ts +2 -0
  109. package/lib/integrations/acp/harnesses/claude.js +49 -0
  110. package/lib/integrations/acp/harnesses/dsh.d.ts +2 -0
  111. package/lib/integrations/acp/harnesses/dsh.js +35 -0
  112. package/lib/integrations/acp/harnesses/generic.d.ts +13 -0
  113. package/lib/integrations/acp/harnesses/generic.js +37 -0
  114. package/lib/integrations/acp/harnesses/pi.d.ts +2 -0
  115. package/lib/integrations/acp/harnesses/pi.js +34 -0
  116. package/lib/integrations/acp/harnesses/presets.d.ts +3 -0
  117. package/lib/integrations/acp/harnesses/presets.js +20 -0
  118. package/lib/integrations/acp/launch.d.ts +33 -4
  119. package/lib/integrations/acp/launch.js +60 -48
  120. package/lib/integrations/acp/mcp.d.ts +6 -0
  121. package/lib/integrations/acp/mcp.js +40 -0
  122. package/lib/integrations/acp/model-selection.d.ts +9 -2
  123. package/lib/integrations/acp/model-selection.js +73 -27
  124. package/lib/integrations/acp/registry.d.ts +18 -0
  125. package/lib/integrations/acp/registry.js +50 -0
  126. package/lib/integrations/agent-resources.d.ts +16 -1
  127. package/lib/integrations/agent-resources.js +45 -9
  128. package/lib/integrations/dsh/mcp.d.ts +4 -0
  129. package/lib/integrations/dsh/mcp.js +28 -0
  130. package/lib/integrations/dsh/resources.d.ts +3 -3
  131. package/lib/integrations/dsh/resources.js +21 -12
  132. package/lib/integrations/dsh/skills.d.ts +5 -1
  133. package/lib/integrations/dsh/skills.js +10 -6
  134. package/lib/integrations/hosts/antigravity.d.ts +4 -0
  135. package/lib/integrations/hosts/antigravity.js +79 -0
  136. package/lib/integrations/hosts/claude.d.ts +3 -0
  137. package/lib/integrations/hosts/claude.js +60 -0
  138. package/lib/integrations/hosts/registry.d.ts +15 -0
  139. package/lib/integrations/hosts/registry.js +34 -0
  140. package/lib/model/acp.d.ts +107 -2
  141. package/lib/model/acp.js +12 -1
  142. package/lib/model/agent.d.ts +35 -33
  143. package/lib/model/plugin-host.d.ts +48 -0
  144. package/lib/model/plugin-host.js +1 -0
  145. package/lib/model/workspace-config.d.ts +12 -0
  146. package/lib/model/workspace-mcp.d.ts +51 -0
  147. package/lib/model/workspace-mcp.js +4 -0
  148. package/lib/plugin.d.ts +1 -0
  149. package/lib/plugin.js +17 -0
  150. package/lib/services/agents/acp-run.d.ts +13 -1
  151. package/lib/services/agents/acp-run.js +30 -2
  152. package/lib/services/agents/agent-service.d.ts +35 -120
  153. package/lib/services/agents/agent-service.js +156 -160
  154. package/lib/services/installation/setup-service.js +17 -1
  155. package/lib/services/workspace/workspace-config.d.ts +2 -1
  156. package/lib/services/workspace/workspace-config.js +43 -9
  157. package/lib/services/workspace/workspace-mcp.d.ts +14 -0
  158. package/lib/services/workspace/workspace-mcp.js +200 -0
  159. package/lib/services/workspace/workspace-skills.d.ts +8 -0
  160. package/lib/services/workspace/workspace-skills.js +91 -0
  161. package/lib/storage/acp-files.d.ts +3 -1
  162. package/lib/storage/acp-files.js +32 -2
  163. package/lib/storage/acp-history.d.ts +3 -2
  164. package/lib/storage/acp-history.js +19 -7
  165. package/lib/storage/agent-files.d.ts +8 -8
  166. package/lib/storage/agent-files.js +27 -47
  167. package/lib/tools/history/contract.d.ts +1 -1
  168. package/lib/tools/history/contract.js +17 -11
  169. package/lib/ui/dsh/index.js +8 -3
  170. package/lib/ui/dsh/management.d.ts +1 -1
  171. package/lib/ui/dsh/management.js +24 -456
  172. package/lib/ui/dsh/remote.d.ts +0 -2
  173. package/lib/ui/dsh/remote.js +1 -1
  174. package/package.json +12 -7
  175. package/resources/agents/coordinator.md +2 -2
  176. package/resources/antigravity/skills/pbot/SKILL.md +16 -0
  177. package/resources/claude/skills/pbot/SKILL.md +1 -1
  178. package/resources/prompts/history-coordinator.md +1 -0
  179. package/resources/prompts/plugin-coordinator.md +1 -0
  180. package/resources/prompts/workspace.md +9 -8
  181. package/resources/references/compute.md +9 -11
  182. package/resources/references/task-conductor.md +12 -26
  183. package/resources/skills/project-maintenance/SKILL.md +16 -21
  184. package/resources/skills/provider-connections/SKILL.md +24 -40
  185. package/resources/templates/workspace-agents.md +4 -0
  186. package/scripts/README.md +17 -34
  187. package/src/claude.ts +4 -8
  188. package/src/common/markdown.ts +2 -3
  189. package/src/common/validation.ts +6 -0
  190. package/src/extension/acp-member.ts +9 -8
  191. package/src/extension/acp-worker.ts +239 -97
  192. package/src/extension/command.ts +38 -36
  193. package/src/extension/dsh/acp-member.ts +21 -2
  194. package/src/extension/dsh/command.ts +21 -51
  195. package/src/extension/dsh/files.ts +1 -0
  196. package/src/extension/dsh/history.ts +9 -6
  197. package/src/extension/dsh/management.ts +27 -58
  198. package/src/extension/dsh/workspace.ts +88 -99
  199. package/src/extension/events.ts +13 -10
  200. package/src/extension/history.ts +2 -50
  201. package/src/extension/lifecycle.ts +37 -22
  202. package/src/extension/mcp-bridge.ts +412 -0
  203. package/src/extension/mcp-workers.ts +941 -0
  204. package/src/extension/pi-acp-member.ts +29 -5
  205. package/src/extension/plugin-runtime.ts +63 -0
  206. package/src/extension/setup-cli.ts +186 -2
  207. package/src/integrations/acp/agents.ts +70 -24
  208. package/src/integrations/acp/claude.ts +11 -4
  209. package/src/integrations/acp/connection.ts +63 -25
  210. package/src/integrations/acp/executable.ts +41 -0
  211. package/src/integrations/acp/harnesses/claude.ts +50 -0
  212. package/src/integrations/acp/harnesses/dsh.ts +40 -0
  213. package/src/integrations/acp/harnesses/generic.ts +52 -0
  214. package/src/integrations/acp/harnesses/pi.ts +35 -0
  215. package/src/integrations/acp/harnesses/presets.ts +22 -0
  216. package/src/integrations/acp/launch.ts +69 -47
  217. package/src/integrations/acp/mcp.ts +43 -0
  218. package/src/integrations/acp/model-selection.ts +68 -25
  219. package/src/integrations/acp/registry.ts +60 -0
  220. package/src/integrations/agent-resources.ts +70 -6
  221. package/src/integrations/dsh/mcp.ts +30 -0
  222. package/src/integrations/dsh/resources.ts +28 -13
  223. package/src/integrations/dsh/skills.ts +16 -6
  224. package/src/integrations/hosts/antigravity.ts +83 -0
  225. package/src/integrations/hosts/claude.ts +62 -0
  226. package/src/integrations/hosts/registry.ts +42 -0
  227. package/src/integrations/pi/mcp.ts +26 -0
  228. package/src/mcp.ts +14 -0
  229. package/src/model/acp.ts +113 -3
  230. package/src/model/agent.ts +18 -14
  231. package/src/model/plugin-host.ts +39 -0
  232. package/src/model/workspace-config.ts +14 -0
  233. package/src/model/workspace-mcp.ts +64 -0
  234. package/src/pbot.ts +69 -20
  235. package/src/plugin.ts +19 -0
  236. package/src/services/agents/acp-run.ts +35 -3
  237. package/src/services/agents/agent-service.ts +180 -160
  238. package/src/services/installation/setup-service.ts +17 -2
  239. package/src/services/workspace/workspace-config.ts +46 -8
  240. package/src/services/workspace/workspace-mcp.ts +242 -0
  241. package/src/services/workspace/workspace-skills.ts +78 -0
  242. package/src/storage/acp-files.ts +32 -3
  243. package/src/storage/acp-history.ts +18 -7
  244. package/src/storage/agent-files.ts +27 -41
  245. package/src/tools/history/contract.ts +17 -11
  246. package/src/tools/register.ts +4 -8
  247. package/src/ui/dsh/index.ts +8 -3
  248. package/src/ui/dsh/management.ts +40 -282
  249. package/src/ui/dsh/remote.ts +2 -2
  250. package/docs/adr/0001-claude-skill-discovery.md +0 -3
  251. package/docs/adr/0002-claude-native-collaboration.md +0 -3
  252. package/docs/agent-collaboration.md +0 -190
  253. package/docs/architecture-guidelines.md +0 -37
  254. package/docs/capability-design.md +0 -55
  255. package/docs/claude-code-design.md +0 -44
  256. package/docs/claude-code.md +0 -57
  257. package/docs/dsh.md +0 -337
  258. package/docs/file-storage.md +0 -97
  259. package/docs/history.md +0 -54
  260. package/docs/installation-design.md +0 -86
  261. package/docs/interaction-design.md +0 -21
  262. package/docs/model-context.md +0 -76
  263. package/docs/model-instruction-guidelines.md +0 -51
  264. package/docs/release.md +0 -108
  265. package/docs/technical-design.md +0 -163
  266. package/docs/workspace-configuration.md +0 -79
  267. package/lib/extension/claude-hooks.d.ts +0 -9
  268. package/lib/extension/claude-hooks.js +0 -78
  269. package/lib/extension/dsh/agent-events.d.ts +0 -24
  270. package/lib/extension/dsh/agent-events.js +0 -242
  271. package/lib/extension/dsh/agent-runtime.d.ts +0 -49
  272. package/lib/extension/dsh/agent-runtime.js +0 -351
  273. package/lib/integrations/dsh/dsh-agents.d.ts +0 -30
  274. package/lib/integrations/dsh/dsh-agents.js +0 -104
  275. package/lib/integrations/dsh/input.d.ts +0 -21
  276. package/lib/integrations/dsh/input.js +0 -62
  277. package/lib/integrations/dsh/notifications.d.ts +0 -8
  278. package/lib/integrations/dsh/notifications.js +0 -55
  279. package/lib/integrations/dsh/session.d.ts +0 -14
  280. package/lib/integrations/dsh/session.js +0 -60
  281. package/lib/integrations/team-context.d.ts +0 -18
  282. package/lib/integrations/team-context.js +0 -61
  283. package/lib/model/team.d.ts +0 -85
  284. package/lib/services/agents/agent-notices.d.ts +0 -13
  285. package/lib/services/agents/agent-notices.js +0 -25
  286. package/lib/services/agents/start-options.d.ts +0 -4
  287. package/lib/services/agents/start-options.js +0 -20
  288. package/lib/services/agents/team-service.d.ts +0 -168
  289. package/lib/services/agents/team-service.js +0 -345
  290. package/lib/storage/team-files.d.ts +0 -16
  291. package/lib/storage/team-files.js +0 -111
  292. package/lib/tools/agents/actions.d.ts +0 -62
  293. package/lib/tools/agents/actions.js +0 -62
  294. package/lib/tools/agents/contract.d.ts +0 -181
  295. package/lib/tools/agents/contract.js +0 -84
  296. package/lib/tools/agents/dsh-tool.d.ts +0 -5
  297. package/lib/tools/agents/dsh-tool.js +0 -34
  298. package/lib/tools/result.d.ts +0 -18
  299. package/lib/tools/result.js +0 -10
  300. package/lib/tools/teams/actions.d.ts +0 -23
  301. package/lib/tools/teams/actions.js +0 -40
  302. package/lib/tools/teams/contract.d.ts +0 -201
  303. package/lib/tools/teams/contract.js +0 -91
  304. package/lib/tools/teams/dsh-tool.d.ts +0 -8
  305. package/lib/tools/teams/dsh-tool.js +0 -39
  306. package/lib/ui/dsh/attachments.d.ts +0 -5
  307. package/lib/ui/dsh/attachments.js +0 -39
  308. package/resources/prompts/claude-coordinator.md +0 -1
  309. package/resources/prompts/native-coordinator.md +0 -1
  310. package/resources/prompts/team-member.md +0 -7
  311. package/src/extension/agent-events.ts +0 -210
  312. package/src/extension/agents-command.ts +0 -89
  313. package/src/extension/claude-hooks.ts +0 -74
  314. package/src/extension/dsh/agent-events.ts +0 -148
  315. package/src/extension/dsh/agent-runtime.ts +0 -252
  316. package/src/extension/subagent-entry.ts +0 -270
  317. package/src/extension/teams-command.ts +0 -67
  318. package/src/integrations/dsh/dsh-agents.ts +0 -97
  319. package/src/integrations/dsh/input.ts +0 -66
  320. package/src/integrations/dsh/notifications.ts +0 -46
  321. package/src/integrations/pi/agent-context.ts +0 -18
  322. package/src/integrations/pi/agent-input.ts +0 -19
  323. package/src/integrations/pi/pi-agents.ts +0 -84
  324. package/src/integrations/pi/team-context.ts +0 -26
  325. package/src/integrations/team-context.ts +0 -64
  326. package/src/model/team.ts +0 -29
  327. package/src/services/agents/agent-notices.ts +0 -28
  328. package/src/services/agents/start-options.ts +0 -19
  329. package/src/services/agents/team-service.ts +0 -275
  330. package/src/storage/team-files.ts +0 -93
  331. package/src/tools/agents/actions.ts +0 -57
  332. package/src/tools/agents/contract.ts +0 -101
  333. package/src/tools/agents/dsh-tool.ts +0 -37
  334. package/src/tools/agents/tool.ts +0 -20
  335. package/src/tools/teams/actions.ts +0 -34
  336. package/src/tools/teams/contract.ts +0 -109
  337. package/src/tools/teams/dsh-tool.ts +0 -45
  338. package/src/tools/teams/tool.ts +0 -29
  339. package/src/ui/agent-display.ts +0 -14
  340. package/src/ui/dsh/attachments.ts +0 -42
  341. package/src/ui/pi/menu.ts +0 -37
  342. package/src/ui/pi/reader.ts +0 -46
  343. /package/lib/{model/team.js → antigravity-plugin/lib/model/plugin-host.js} +0 -0
@@ -1,17 +1,16 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import { realpathSync, readdirSync } from "node:fs";
2
+ import { realpathSync } from "node:fs";
3
3
  import { resolve } from "node:path";
4
+ import { object } from "../../common/json.js";
4
5
  import { packageVersion, releaseLine } from "../../common/version.js";
5
6
  import { requireThat } from "../../common/validation.js";
6
7
  import { AgentFiles, descriptor, launch, status, cycle } from "../../storage/agent-files.js";
7
- import { TeamFiles } from "../../storage/team-files.js";
8
- import { acpModelState, acpPermission, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
9
- import { applicableStartOptions } from "./start-options.js";
8
+ import { acpElicitationReply, acpModelState, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
9
+ /** Host-neutral lifecycle manager for cross-harness ACP workers. */
10
10
  export class AgentService {
11
11
  root;
12
12
  host;
13
13
  files;
14
- teamFiles;
15
14
  #closed = false;
16
15
  #preserve = false;
17
16
  #starting = new Map();
@@ -21,19 +20,18 @@ export class AgentService {
21
20
  constructor(root, directory, host) {
22
21
  this.root = root;
23
22
  this.host = host;
24
- this.files = new AgentFiles(directory, host.kind);
25
- this.teamFiles = new TeamFiles(this.files);
23
+ this.files = new AgentFiles(directory);
26
24
  }
27
25
  alive(id) { return this.#starting.has(id) || this.#releasing.has(id) || this.host.alive(id); }
28
- check() { requireThat(!this.#closed, "子会话服务已关闭。"); }
26
+ check() { requireThat(!this.#closed, "worker 服务已关闭。"); }
29
27
  owned(d, owner) {
30
28
  return d.workspace === this.root && d.parentSessionId === owner.sessionId && (d.parentLeaf === null || owner.branch.includes(d.parentLeaf));
31
29
  }
32
30
  get(id, owner) {
33
31
  this.check();
34
32
  const d = this.files.read(id, "descriptor.json", descriptor);
35
- requireThat(this.owned(d, owner), "子会话不属于当前会话分支。");
36
- requireThat(releaseLine(d.release) === releaseLine(packageVersion), "子会话版本不兼容;请停止后使用匹配版本。");
33
+ requireThat(this.owned(d, owner), "worker 不属于当前会话。");
34
+ requireThat(releaseLine(d.release) === releaseLine(packageVersion), "worker 版本不兼容;请停止后使用匹配版本。");
37
35
  return d;
38
36
  }
39
37
  records(owner) {
@@ -48,35 +46,132 @@ export class AgentService {
48
46
  const current = saved?.generation === run.generation ? saved : undefined;
49
47
  const alive = this.alive(d.agentId), pending = this.#starting.get(d.agentId);
50
48
  const starting = pending && !pending.controller.signal.aborted;
51
- const reference = this.files.optional(d.agentId, "acp-session.json", acpReference);
52
- const model = reference ? this.files.optional(d.agentId, "acp-model.json", acpModelState) : undefined;
53
- const selection = model?.generation === run.generation ? { ...(model.provider ? { provider: model.provider } : {}),
54
- ...(model.model ? { model: model.model } : {}), ...(model.resolvedModel ? { resolvedModel: model.resolvedModel } : {}),
55
- ...(model.thinking ? { thinking: model.thinking } : {}) } : undefined;
56
- return { ...d, name: run.name, generation: run.generation, owned: this.owned(d, owner), alive,
57
- ...(reference?.host === "acp" ? { backend: reference.backend, transport: "acp",
58
- capabilities: { steer: false, resume: Boolean(reference.capabilities?.resume || reference.capabilities?.load), team: false, transcript: false },
59
- ...(selection ? { selection } : {}) } : {}),
49
+ const reference = this.files.read(d.agentId, "acp-session.json", acpReference);
50
+ const model = this.files.optional(d.agentId, "acp-model.json", acpModelState);
51
+ const selection = model?.generation === run.generation ? {
52
+ ...(model.provider ? { provider: model.provider } : {}),
53
+ ...(model.model ? { model: model.model } : {}),
54
+ ...(model.resolvedModel ? { resolvedModel: model.resolvedModel } : {}),
55
+ ...(model.thinking ? { thinking: model.thinking } : {}),
56
+ } : undefined;
57
+ const pendingRequests = alive ? [
58
+ ...this.files.pendingPermissions(d.agentId, run.generation)
59
+ .map(item => ({ requestId: item.requestId, kind: "permission", description: item.description, options: item.options })),
60
+ ...this.files.pendingElicitations(d.agentId, run.generation)
61
+ .map(item => (item.mode === "form"
62
+ ? { requestId: item.requestId, kind: "form", message: item.message, schema: item.schema ?? { type: "object", properties: {} } }
63
+ : { requestId: item.requestId, kind: "url", message: item.message, url: item.url ?? "" })),
64
+ ] : [];
65
+ return {
66
+ ...d,
67
+ name: run.name,
68
+ generation: run.generation,
69
+ owned: this.owned(d, owner),
70
+ alive,
71
+ backend: reference.backend,
72
+ transport: "acp",
73
+ capabilities: {
74
+ steer: false,
75
+ resume: Boolean(reference.capabilities?.resume || reference.capabilities?.load),
76
+ transcript: false,
77
+ },
78
+ ...(selection ? { selection } : {}),
60
79
  state: alive ? starting ? "starting" : run.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
61
80
  activity: starting ? "启动" : alive ? current?.activity ?? "启动" : current?.error ?? "已停止",
62
- ...(current?.cycle ? { cycle: current.cycle } : {}), ...(current?.error ? { error: current.error } : {}) };
81
+ ...(current?.cycle ? { cycle: current.cycle } : {}),
82
+ ...(current?.error ? { error: current.error } : {}),
83
+ ...(pendingRequests.length ? { pendingRequests } : {}),
84
+ };
85
+ }
86
+ /** Answer a pending permission or elicitation request forwarded by the worker. */
87
+ respond(id, input, owner) {
88
+ const d = this.get(id, owner), run = this.files.read(id, "launch.json", launch);
89
+ const pending = this.view(d, owner).pendingRequests ?? [];
90
+ if (!input.requestId && pending.length > 1) {
91
+ throw new Error("存在多条待回复请求,请指定 requestId:" + pending.map(item => item.requestId).join(", "));
92
+ }
93
+ const target = input.requestId ? pending.find(item => item.requestId === input.requestId) : pending[0];
94
+ requireThat(target, input.requestId
95
+ ? "没有待回复的请求:" + input.requestId + ";用 worker_discover 查看 pendingRequests。"
96
+ : "当前没有待回复的请求。");
97
+ if (target.kind === "permission") {
98
+ requireThat(input.content === undefined, "权限请求不使用 content;请用 action(accept / decline / cancel)或指定 optionId。");
99
+ let resolved = null;
100
+ if (input.action === "cancel") {
101
+ requireThat(input.optionId === undefined, "action 为 cancel 时不能同时指定 optionId。");
102
+ }
103
+ else if (input.optionId !== undefined) {
104
+ const exact = target.options.find(option => option.optionId === input.optionId);
105
+ const byKind = target.options.filter(option => option.kind === input.optionId);
106
+ const matched = exact ?? (byKind.length === 1 ? byKind[0] : undefined);
107
+ requireThat(matched, "权限 optionId 无效:" + input.optionId + ";可选:" +
108
+ target.options.map(option => `${option.optionId}(${option.name}/${option.kind})`).join(", "));
109
+ if (input.action === "accept") {
110
+ requireThat(!matched.kind.startsWith("reject_"), `optionId ${matched.optionId} 是拒绝选项,与 action: "accept" 冲突。`);
111
+ }
112
+ else {
113
+ requireThat(!matched.kind.startsWith("allow_"), `optionId ${matched.optionId} 是允许选项,与 action: "decline" 冲突。`);
114
+ }
115
+ resolved = matched.optionId;
116
+ }
117
+ else {
118
+ const pick = (...kinds) => kinds.map(kind => target.options.find(option => option.kind === kind)).find(Boolean)?.optionId;
119
+ if (input.action === "accept") {
120
+ const chosen = pick("allow_once", "allow_always");
121
+ requireThat(chosen, "该权限请求没有可允许的选项;可选:" + target.options.map(option => option.optionId).join(", "));
122
+ resolved = chosen;
123
+ }
124
+ else {
125
+ resolved = pick("reject_once", "reject_always") ?? null;
126
+ }
127
+ }
128
+ this.files.ensureSubdirectory(id, "permissions");
129
+ this.files.write(id, "permissions/" + target.requestId + "-reply.json", { generation: run.generation, requestId: target.requestId, optionId: resolved }, acpPermissionReply, true);
130
+ return this.view(d, owner);
131
+ }
132
+ if (target.kind === "form") {
133
+ requireThat(input.optionId === undefined, "问答/表单请求不使用 optionId;请将答案填入 content。");
134
+ if (input.action !== "accept") {
135
+ requireThat(input.content === undefined, `action 为 ${input.action} 时不能传入 content。`);
136
+ }
137
+ else {
138
+ requireThat(input.content === undefined || object(input.content), "问答/表单答复的 content 必须是对象。");
139
+ }
140
+ this.files.ensureSubdirectory(id, "elicitations");
141
+ this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
142
+ generation: run.generation,
143
+ requestId: target.requestId,
144
+ action: input.action,
145
+ ...(input.action === "accept" ? { content: input.content ?? {} } : {}),
146
+ }, acpElicitationReply, true);
147
+ return this.view(d, owner);
148
+ }
149
+ requireThat(input.optionId === undefined, "URL 确认请求不使用 optionId。");
150
+ requireThat(input.content === undefined, "URL 确认请求不使用 content。");
151
+ this.files.ensureSubdirectory(id, "elicitations");
152
+ this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
153
+ generation: run.generation,
154
+ requestId: target.requestId,
155
+ action: input.action,
156
+ }, acpElicitationReply, true);
157
+ return this.view(d, owner);
63
158
  }
64
159
  async start(input, owner) {
65
160
  this.check();
66
161
  const { backend, ...supplied } = input;
67
162
  this.host.available(backend);
68
- const options = applicableStartOptions(this.host.kind, backend, Object.fromEntries(Object.entries(supplied).filter(([, v]) => v !== undefined)));
69
- this.host.validateOptions?.(options, backend);
70
- requireThat(backend || options.provider && options.model, "请选择可在专家会话使用的模型。");
71
- const reference = backend ? this.host.prepare?.(backend, options) : undefined;
72
- requireThat(!backend || reference, "当前执行接入不支持 ACP 后端。");
163
+ const clean = Object.fromEntries(Object.entries(supplied).filter(([, v]) => v !== undefined));
164
+ const options = this.host.filterOptions ? this.host.filterOptions(backend, clean) : clean;
165
+ const reference = this.host.prepare(backend, options);
73
166
  const prepared = this.files.control(() => {
74
- requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中子会话;请先停止一个空闲会话。");
167
+ requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker;请先停止一个空闲会话。");
75
168
  const agentId = randomUUID();
76
169
  const cwd = realpathSync(resolve(this.root, options.cwd ?? "."));
77
170
  const files = (options.files ?? []).map(p => realpathSync(resolve(this.root, p)));
78
- const d = { agentId, release: packageVersion, workspace: this.root, cwd,
79
- parentSessionId: owner.sessionId, parentLeaf: owner.leaf, createdAt: Date.now(), options: { ...options, files, cwd } };
171
+ const d = {
172
+ agentId, release: packageVersion, workspace: this.root, cwd,
173
+ parentSessionId: owner.sessionId, parentLeaf: owner.leaf, createdAt: Date.now(), options: { ...options, files, cwd },
174
+ };
80
175
  const run = { generation: randomUUID(), name: options.name ?? agentId.slice(0, 8), sequence: 0, stop: false, startedAt: Date.now() };
81
176
  this.files.create(d, run, reference);
82
177
  const initial = options.message ?? "";
@@ -93,18 +188,20 @@ export class AgentService {
93
188
  const operation = (async () => {
94
189
  try {
95
190
  await this.host.launch(d, this.files.directory(d.agentId), generation, this.files.reference(d.agentId), resume, pending.controller.signal);
96
- requireThat(!this.#closed || this.#preserve, "子会话服务已关闭。");
191
+ requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
97
192
  pending.controller.signal.throwIfAborted();
98
193
  const run = this.files.read(d.agentId, "launch.json", launch);
99
- requireThat(run.generation === generation && !run.stop, "子会话启动已取消或代次变化。");
194
+ requireThat(run.generation === generation && !run.stop, "worker 启动已取消或代次变化。");
100
195
  }
101
196
  catch (error) {
102
197
  const run = this.files.read(d.agentId, "launch.json", launch);
103
198
  if (run.generation === generation) {
104
199
  if (this.host.alive(d.agentId))
105
200
  await this.release(d.agentId);
106
- this.files.write(d.agentId, "status.json", { generation, state: run.stop ? "stopped" : "error",
107
- activity: run.stop ? "已停止" : "启动失败", updatedAt: Date.now(), ...(run.stop ? {} : { error: String(error) }) }, status);
201
+ this.files.write(d.agentId, "status.json", {
202
+ generation, state: run.stop ? "stopped" : "error",
203
+ activity: run.stop ? "已停止" : "启动失败", updatedAt: Date.now(), ...(run.stop ? {} : { error: String(error) }),
204
+ }, status);
108
205
  }
109
206
  throw error;
110
207
  }
@@ -124,43 +221,14 @@ export class AgentService {
124
221
  send(id, text, delivery, owner, attachments = []) {
125
222
  const d = this.get(id, owner);
126
223
  return this.files.control(() => {
127
- requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "子会话已停止或仍在恢复校验,请等待显式恢复完成。");
128
- this.assertPrivate(id);
224
+ requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "worker 已停止或仍在恢复校验,请等待显式恢复完成。");
129
225
  const view = this.view(d, owner);
130
- requireThat(!(view.transport === "acp" && delivery === "steer" && view.state !== "idle"), "该子代理运行中不支持 steer;请用 delivery: followUp 排队。");
226
+ requireThat(!(delivery === "steer" && view.state !== "idle"), "该 ACP worker 运行中不支持 steer;请用 delivery: followUp 排队。");
131
227
  const files = attachments.map(path => realpathSync(resolve(this.root, path)));
132
228
  const request = this.files.enqueue(id, text, delivery, files);
133
229
  return { ...this.view(d, owner), requestId: request.requestId, messageStatus: "queued" };
134
230
  });
135
231
  }
136
- assertPrivate(id) {
137
- this.teamFiles.check();
138
- const run = this.files.read(id, "launch.json", launch), saved = this.files.optional(id, "status.json", status);
139
- const complete = this.files.completions(id), alive = this.host.alive(id);
140
- const occupied = this.teamFiles.list().some(t => t.members.some(m => m.agentId === id) && t.runs.some(r => r.state === "running" || r.issued.some(q => q.agentId === id && q.generation === run.generation && alive &&
141
- saved?.generation === run.generation && saved.accepted?.includes(q.requestId) &&
142
- !complete.some(c => c.generation === q.generation && c.requestIds.includes(q.requestId)))));
143
- if (occupied)
144
- throw Object.assign(new Error("TEAM_BUSY:请先暂停团队并等待在途工作结束。"), { code: "TEAM_BUSY" });
145
- }
146
- enqueueTeam(input, owner) {
147
- const d = this.get(input.agentId, owner);
148
- return this.files.control(() => {
149
- this.teamFiles.check();
150
- const team = this.teamFiles.read(input.teamId), run = team.runs.find(r => r.runId === input.runId);
151
- const q = run?.issued.find(q => q.requestId === input.requestId), ready = this.teamFiles.ready(input.agentId);
152
- const process = this.files.read(input.agentId, "launch.json", launch);
153
- const valid = team.workspace === this.root && team.parentSessionId === owner.sessionId &&
154
- (team.parentLeaf === null || owner.branch.includes(team.parentLeaf)) && !team.closed && run?.state === "running" &&
155
- run.deadlineAt > Date.now() && q?.agentId === input.agentId && q.generation === input.generation &&
156
- process.generation === input.generation && !process.stop && this.host.alive(input.agentId) &&
157
- ready?.ready && ready.generation === input.generation;
158
- if (!valid)
159
- throw Object.assign(new Error("团队安排已失效;未重新执行。"), { code: "TEAM_INPUT_STALE" });
160
- const request = this.files.enqueue(input.agentId, input.message, "followUp", [], input.requestId);
161
- return { ...this.view(d, owner), requestId: request.requestId, messageStatus: "queued" };
162
- });
163
- }
164
232
  rename(id, name, owner) {
165
233
  requireThat(name.trim().length > 0 && name.length <= 64 && !/[\x00-\x1f]/.test(name), "名称必须是 1 到 64 字符的单行文本。");
166
234
  const d = this.get(id, owner);
@@ -179,7 +247,7 @@ export class AgentService {
179
247
  async enter(id, owner) {
180
248
  const d = this.get(id, owner);
181
249
  if (this.alive(id)) {
182
- requireThat(!this.files.read(id, "launch.json", launch).stop, "子会话正在停止,请稍后进入。");
250
+ requireThat(!this.files.read(id, "launch.json", launch).stop, "worker 正在停止,请稍后操作。");
183
251
  return this.view(d, owner);
184
252
  }
185
253
  return this.restart(id, owner);
@@ -189,11 +257,10 @@ export class AgentService {
189
257
  }
190
258
  async restart(id, owner, text) {
191
259
  const d = this.get(id, owner), reference = this.files.reference(id);
192
- this.host.available(reference.host === "acp" ? reference.backend : undefined);
260
+ this.host.available(reference.backend);
193
261
  const reserved = this.files.control(() => {
194
- requireThat(!this.alive(id), "子会话仍在运行。");
195
- this.assertPrivate(id);
196
- requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中子会话。");
262
+ requireThat(!this.alive(id), "worker 仍在运行。");
263
+ requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker。");
197
264
  const previous = this.files.read(id, "launch.json", launch), interrupted = this.cycles(id, owner);
198
265
  const pending = { generation: previous.generation, controller: new AbortController(), stage: "validate" };
199
266
  this.#starting.set(id, pending);
@@ -201,18 +268,15 @@ export class AgentService {
201
268
  });
202
269
  const operation = (async () => {
203
270
  await this.host.validateSession(d, this.files.reference(id));
204
- requireThat(!this.#closed || this.#preserve, "子会话服务已关闭。");
271
+ requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
205
272
  reserved.pending.controller.signal.throwIfAborted();
206
273
  const prepared = this.files.control(() => {
207
- requireThat(!this.host.alive(id) && this.files.read(id, "launch.json", launch).generation === reserved.previous.generation, "子会话已由其他操作恢复。");
208
- this.assertPrivate(id);
209
- // Only the generation known to be stopped before validation can be sealed.
274
+ requireThat(!this.host.alive(id) && this.files.read(id, "launch.json", launch).generation === reserved.previous.generation, "worker 已由其他操作恢复。");
210
275
  for (const ended of reserved.interrupted) {
211
276
  if (ended.generation === reserved.previous.generation && !this.files.completions(id).some(c => c.cycleId === ended.cycleId)) {
212
277
  this.files.write(id, "completions/" + ended.cycleId + ".json", ended, cycle, true);
213
278
  }
214
279
  }
215
- this.#stopping.delete(id);
216
280
  const run = { ...reserved.previous, generation: randomUUID(), sequence: 0, stop: false, startedAt: Date.now() };
217
281
  this.files.write(id, "launch.json", run, launch);
218
282
  const request = text === undefined ? undefined : this.files.enqueue(id, text, "steer");
@@ -240,16 +304,20 @@ export class AgentService {
240
304
  }
241
305
  if (["stopped", "error"].includes(view.state)) {
242
306
  const run = this.files.read(id, "launch.json", launch), saved = this.files.optional(id, "status.json", status);
243
- // Preserve receipt-only crash windows before resume replaces the generation and status.
244
307
  for (const requestId of saved?.generation === run.generation ? saved.accepted ?? [] : []) {
245
308
  if (values.some(c => c.generation === run.generation && c.requestIds.includes(requestId)))
246
309
  continue;
247
- values.push({ cycleId: requestId, generation: run.generation, startedAt: run.startedAt, finishedAt: Date.now(),
248
- state: "interrupted", leaf: null, requestIds: [requestId], source: "parent", error: "领取后执行结果不明;未重跑。" });
310
+ values.push({
311
+ cycleId: requestId, generation: run.generation, startedAt: run.startedAt, finishedAt: Date.now(),
312
+ state: "interrupted", leaf: null, requestIds: [requestId], source: "parent", error: "领取后执行结果不明;未重跑。",
313
+ });
314
+ }
315
+ if (run.sequence > 0 && !values.some(c => c.generation === view.generation)) {
316
+ values.push({
317
+ cycleId: run.generation, generation: run.generation, startedAt: run.startedAt, finishedAt: run.startedAt,
318
+ state: "interrupted", leaf: null, requestIds: [], source: "parent", error: view.error ?? "子进程在交付前退出。",
319
+ });
249
320
  }
250
- if (run.sequence > 0 && !values.some(c => c.generation === view.generation))
251
- values.push({ cycleId: run.generation, generation: run.generation, startedAt: run.startedAt, finishedAt: run.startedAt,
252
- state: "interrupted", leaf: null, requestIds: [], source: "parent", error: view.error ?? "子进程在交付前退出。" });
253
321
  }
254
322
  return values;
255
323
  }
@@ -264,74 +332,20 @@ export class AgentService {
264
332
  const report = c ? await this.report(d, c) : "";
265
333
  this.check();
266
334
  requireThat(offset <= report.length, "分页位置超出报告。");
267
- return { ...this.view(d, owner), ...(c ? { cycle: c } : {}), cycles: cycles.map(v => ({ cycleId: v.cycleId, state: v.state })),
268
- report: report.slice(offset, offset + limit), totalLength: report.length, nextOffset: offset + limit < report.length ? offset + limit : null };
269
- }
270
- async completions(owner) {
271
- const values = await Promise.all(this.list(owner).filter(v => v.owned).flatMap(v => this.cycles(v.agentId, owner).filter(c => c.state !== "running").map(async (c) => ({ agentId: v.agentId, name: v.name, cycle: c, report: await this.report(v, c) }))));
272
- this.check();
273
- return values;
335
+ return {
336
+ ...this.view(d, owner),
337
+ ...(c ? { cycle: c } : {}),
338
+ cycles: cycles.map(v => ({ cycleId: v.cycleId, state: v.state })),
339
+ report: report.slice(offset, offset + limit),
340
+ totalLength: report.length,
341
+ nextOffset: offset + limit < report.length ? offset + limit : null,
342
+ };
274
343
  }
275
344
  report(d, c) {
276
345
  if (c.leaf === null)
277
346
  return "";
278
347
  return this.host.report(d, this.files.reference(d.agentId), c.leaf);
279
348
  }
280
- async recordPage(id, owner, options = {}) {
281
- const d = this.get(id, owner), view = this.view(d, owner);
282
- requireThat(view.transport === "acp", "该记录入口用于 ACP 成员。");
283
- const messages = this.files.messages(id), cycles = this.cycles(id, owner).sort((a, b) => a.startedAt - b.startedAt);
284
- const records = [];
285
- const included = new Set();
286
- for (const c of cycles) {
287
- for (const requestId of c.requestIds) {
288
- const request = messages.find(message => message.requestId === requestId);
289
- if (!request || included.has(requestId))
290
- continue;
291
- included.add(requestId);
292
- records.push({ seq: records.length, type: "acp/input", time: c.startedAt, requestId, state: c.state,
293
- text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
294
- }
295
- const report = await this.report(d, c);
296
- this.check();
297
- records.push({ seq: records.length, type: "acp/report", time: c.finishedAt ?? c.startedAt, cycleId: c.cycleId, state: c.state,
298
- text: [c.error, report].filter(Boolean).join("\n\n") });
299
- }
300
- const run = this.files.read(id, "launch.json", launch);
301
- for (const request of messages.filter(message => message.generation === run.generation && !included.has(message.requestId))) {
302
- records.push({ seq: records.length, type: "acp/input", time: run.startedAt, requestId: request.requestId, state: "queued",
303
- text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
304
- }
305
- const cursor = run.generation + ":" + records.length + ":" + cycles.at(-1)?.state;
306
- if (options.seq !== undefined) {
307
- const record = records[options.seq];
308
- requireThat(record, "ACP 记录位置不存在。");
309
- const text = options.format === "json" ? JSON.stringify(record, null, 2) : record.text;
310
- const offset = options.textOffset ?? 0, end = offset + (options.textLimit ?? 12000);
311
- requireThat(offset <= text.length, "ACP 记录分页位置超出范围。");
312
- return { kind: "acp", agent: view, cursor, seq: record.seq, offset, text: text.slice(offset, end), totalLength: text.length, nextOffset: end < text.length ? end : null };
313
- }
314
- const limit = options.limit ?? 30, offset = options.offset ?? Math.max(0, records.length - limit);
315
- requireThat(offset <= records.length, "ACP 记录分页位置超出范围。");
316
- return { kind: "acp", agent: view, cursor, total: records.length, offset, nextOffset: offset + limit < records.length ? offset + limit : null,
317
- records: records.slice(offset, offset + limit).map(record => { const { text, ...fields } = record; return { ...fields, preview: text.slice(0, 800) }; }) };
318
- }
319
- pendingPermissions(owner) {
320
- return this.list(owner).filter(view => view.owned && view.alive && view.transport === "acp").flatMap(view => readdirSync(resolve(this.files.directory(view.agentId), "permissions")).filter(name => name.endsWith(".json") && !name.endsWith("-reply.json")).flatMap(name => {
321
- const request = this.files.read(view.agentId, "permissions/" + name, acpPermission);
322
- if (request.generation !== view.generation || this.files.optional(view.agentId, "permissions/" + request.requestId + "-reply.json", acpPermissionReply))
323
- return [];
324
- return [{ ...request, agentId: view.agentId, name: view.name }];
325
- })).slice(0, 32);
326
- }
327
- answerPermission(id, requestId, optionId, owner) {
328
- this.get(id, owner);
329
- const request = this.files.read(id, "permissions/" + requestId + ".json", acpPermission);
330
- const run = this.files.read(id, "launch.json", launch);
331
- requireThat(run.generation === request.generation && !run.stop && this.alive(id), "ACP 确认请求已失效。");
332
- requireThat(optionId === null || request.options.some(option => option.optionId === optionId), "ACP 确认选项无效。");
333
- this.files.write(id, "permissions/" + requestId + "-reply.json", { generation: run.generation, requestId, optionId }, acpPermissionReply, true);
334
- }
335
349
  release(id) {
336
350
  let pending = this.#releasing.get(id);
337
351
  if (!pending) {
@@ -344,24 +358,6 @@ export class AgentService {
344
358
  }
345
359
  return pending;
346
360
  }
347
- /** Called by the parent scanner; stop remains nonblocking to the model. */
348
- async refresh(owner) {
349
- const releases = [];
350
- for (const d of this.records(owner)) {
351
- const run = this.files.read(d.agentId, "launch.json", launch);
352
- if (!run.stop)
353
- this.#stopping.delete(d.agentId);
354
- if (run.stop && this.alive(d.agentId)) {
355
- this.#starting.get(d.agentId)?.controller.abort();
356
- const since = this.#stopping.get(d.agentId) ?? Date.now();
357
- this.#stopping.set(d.agentId, since);
358
- if (Date.now() - since > 5000)
359
- releases.push(this.release(d.agentId));
360
- }
361
- }
362
- await Promise.all(releases);
363
- }
364
- #stopping = new Map();
365
361
  close(owner, preserve = false) {
366
362
  return this.#closing ??= this.dispose(owner, preserve);
367
363
  }
@@ -392,6 +388,6 @@ export class AgentService {
392
388
  errors.push(value.reason);
393
389
  await Promise.allSettled([...this.#launches]);
394
390
  if (errors.length)
395
- throw new AggregateError(errors, "停止或释放专家失败;请核对仍存活的执行资源。");
391
+ throw new AggregateError(errors, "停止或释放 worker 失败;请核对仍存活的执行资源。");
396
392
  }
397
393
  }
@@ -45,8 +45,24 @@ export function parseSetupArgs(args) {
45
45
  return { action: "help" };
46
46
  if (args.length === 1 && ["--version", "-v"].includes(args[0]))
47
47
  return { action: "version" };
48
+ if (args[0] === "install-antigravity") {
49
+ if (args.length === 1)
50
+ return { action: "install-antigravity" };
51
+ if (args.length === 2 && ["--help", "-h"].includes(args[1]))
52
+ return { action: "help" };
53
+ throw new Error("用法:pbot install-antigravity");
54
+ }
55
+ if (args[0] === "mcp") {
56
+ if (args.length === 1)
57
+ return { action: "mcp" };
58
+ if (args.length === 2 && ["--help", "-h"].includes(args[1]))
59
+ return { action: "help" };
60
+ if (["list", "add", "remove"].includes(args[1]))
61
+ return { action: "mcp-manage", mcpArgs: args.slice(1) };
62
+ throw new Error("用法:pbot mcp [list|add|remove ...]");
63
+ }
48
64
  if (args[0] !== "setup")
49
- throw new Error("用法:pbot setup [--workspace <目录>]");
65
+ throw new Error("用法:pbot setup [--workspace <目录>] | pbot install-antigravity | pbot mcp [list|add|remove ...]");
50
66
  let workspace;
51
67
  for (let index = 1; index < args.length; index++) {
52
68
  const arg = args[index];
@@ -1,4 +1,5 @@
1
- import type { WorkspaceConfiguration } from "../../model/workspace-config.js";
1
+ import type { ResolvedWorkspaceFeatures, WorkspaceConfiguration, WorkspaceFeatures } from "../../model/workspace-config.js";
2
+ export declare function workspaceFeatures(configuration?: Pick<WorkspaceConfiguration, "features"> | WorkspaceFeatures): ResolvedWorkspaceFeatures;
2
3
  export declare function workspaceConfigPath(root: string): string;
3
4
  /** Read only this workspace's configuration; parent directories never grant activation. */
4
5
  export declare function readWorkspaceConfig(root: string): Promise<WorkspaceConfiguration>;
@@ -1,23 +1,49 @@
1
1
  import { mkdir, readFile } from "node:fs/promises";
2
2
  import { dirname, join } from "node:path";
3
3
  import { object } from "../../common/json.js";
4
+ import { isAcpBackend } from "../../common/validation.js";
4
5
  import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
6
+ const featureKeys = ["prompts", "skills", "mcp"];
5
7
  function config(value) {
6
8
  if (!object(value) || value.enabled !== undefined && typeof value.enabled !== "boolean") {
7
9
  throw new Error("配置必须是 JSON 对象,enabled 必须是布尔值。");
8
10
  }
11
+ if (value.features !== undefined) {
12
+ if (!object(value.features) ||
13
+ Object.entries(value.features).some(([key, entry]) => !featureKeys.includes(key) || (entry !== undefined && typeof entry !== "boolean"))) {
14
+ throw new Error("features 必须是 JSON 对象,仅支持 prompts、skills、mcp 布尔开关。");
15
+ }
16
+ }
9
17
  if (value.acp !== undefined) {
10
- if (!object(value.acp) || Object.keys(value.acp).some(key => !["pi", "dsh", "claude"].includes(key)))
11
- throw new Error("acp 仅接受 pi、dsh 和 claude 启动配置。");
18
+ if (!object(value.acp) || Object.keys(value.acp).some(key => !isAcpBackend(key))) {
19
+ throw new Error("acp 键名必须为小写字母、数字和连字符(1–64 字符)。");
20
+ }
12
21
  for (const command of Object.values(value.acp)) {
13
- if (!object(command) || typeof command.command !== "string" || !command.command.trim() || command.command.includes("\0") ||
14
- !Array.isArray(command.args) || command.args.some(arg => typeof arg !== "string" || arg.includes("\0")) ||
15
- Object.keys(command).some(key => !["command", "args"].includes(key)))
16
- throw new Error("ACP 启动配置需要 command 和 args 字符串数组。");
22
+ // Without command, an entry keeps the registered default command and only sets env or permission.
23
+ const launch = !object(command) ? false : command.command === undefined
24
+ ? command.args === undefined
25
+ : typeof command.command === "string" && command.command.trim() && !command.command.includes("\0") &&
26
+ (command.args === undefined || (Array.isArray(command.args) && command.args.every(arg => typeof arg === "string" && !arg.includes("\0"))));
27
+ if (!object(command) || !launch ||
28
+ (command.env !== undefined && (!object(command.env) || Object.entries(command.env).some(([k, v]) => !k || k.includes("\0") || typeof v !== "string" || v.includes("\0")))) ||
29
+ (command.permission !== undefined && !["ask", "allow", "deny"].includes(String(command.permission))) ||
30
+ Object.keys(command).some(key => !["command", "args", "env", "permission"].includes(key))) {
31
+ throw new Error("ACP 配置需要 command(内置目标可省略)及可选 args 字符串数组、env 字符串映射、permission(ask、allow 或 deny)。");
32
+ }
17
33
  }
18
34
  }
19
35
  return value;
20
36
  }
37
+ export function workspaceFeatures(configuration) {
38
+ const raw = configuration && "features" in configuration
39
+ ? configuration.features
40
+ : configuration;
41
+ return {
42
+ prompts: raw?.prompts !== false,
43
+ skills: raw?.skills !== false,
44
+ mcp: raw?.mcp !== false,
45
+ };
46
+ }
21
47
  export function workspaceConfigPath(root) {
22
48
  return join(root, ".pbot", "config.json");
23
49
  }
@@ -36,7 +62,13 @@ export async function readWorkspaceConfig(root) {
36
62
  try {
37
63
  const value = config(JSON.parse(content));
38
64
  const settings = value;
39
- return { path, present: true, enabled: settings.enabled === true, ...(settings.acp ? { acp: settings.acp } : {}) };
65
+ return {
66
+ path,
67
+ present: true,
68
+ enabled: settings.enabled === true,
69
+ ...(settings.features ? { features: settings.features } : {}),
70
+ ...(settings.acp ? { acp: settings.acp } : {}),
71
+ };
40
72
  }
41
73
  catch (error) {
42
74
  return { path, present: true, enabled: false, error: `工作区配置无效 ${path}: ${String(error)}` };
@@ -47,12 +79,14 @@ export async function initializeWorkspaceConfig(root) {
47
79
  const current = await readWorkspaceConfig(root);
48
80
  if (current.error)
49
81
  throw new Error(current.error);
50
- if (current.enabled)
82
+ const hasExplicitFeatures = featureKeys.every(key => typeof current.features?.[key] === "boolean");
83
+ if (current.enabled && hasExplicitFeatures)
51
84
  return;
52
85
  const directory = dirname(current.path);
53
86
  await mkdir(directory, { recursive: true });
54
87
  const value = current.present ? readJsonFile(directory, "config.json", config) : {};
55
- const written = writeJsonFile(directory, "config.json", { ...value, enabled: true }, config, !current.present);
88
+ const features = workspaceFeatures(value.features);
89
+ const written = writeJsonFile(directory, "config.json", { ...value, enabled: true, features }, config, !current.present);
56
90
  if (!written)
57
91
  await initializeWorkspaceConfig(root);
58
92
  }
@@ -0,0 +1,14 @@
1
+ import type { ResolvedWorkspaceMcpServer, WorkspaceMcpConfiguration, WorkspaceMcpHttpServer, WorkspaceMcpServer } from "../../model/workspace-mcp.js";
2
+ export declare function isWorkspaceMcpServerName(name: string): boolean;
3
+ export declare function isWorkspaceMcpHttpServer(server: WorkspaceMcpServer): server is WorkspaceMcpHttpServer;
4
+ export declare function validateWorkspaceMcpServer(name: string, value: unknown): WorkspaceMcpServer;
5
+ export declare function workspaceMcpPath(root: string): string;
6
+ export declare function readWorkspaceMcpConfig(root: string): Promise<WorkspaceMcpConfiguration>;
7
+ export declare function expandEnvTemplate(value: string, env: NodeJS.ProcessEnv, context: string): string;
8
+ export declare function resolveWorkspaceMcpServer(root: string, name: string, server: WorkspaceMcpServer, env?: NodeJS.ProcessEnv): ResolvedWorkspaceMcpServer;
9
+ export declare function resolveWorkspaceMcpServers(root: string, configuration: WorkspaceMcpConfiguration, env?: NodeJS.ProcessEnv): ResolvedWorkspaceMcpServer[];
10
+ export declare function upsertWorkspaceMcpServer(root: string, name: string, server: WorkspaceMcpServer): Promise<WorkspaceMcpConfiguration>;
11
+ export declare function removeWorkspaceMcpServer(root: string, name: string): Promise<{
12
+ removed: boolean;
13
+ configuration: WorkspaceMcpConfiguration;
14
+ }>;