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
@@ -0,0 +1,48 @@
1
+ import { requireThat } from "../../../common/validation.js";
2
+ import type { HostUpgrader } from "../../../model/installation.js";
3
+ import { antigravityUpgrader } from "./antigravity.js";
4
+ import { claudeUpgrader } from "./claude.js";
5
+ import { codexUpgrader } from "./codex.js";
6
+ import { dshUpgrader } from "./dsh.js";
7
+ import { piUpgrader } from "./pi.js";
8
+ import { qoderUpgrader } from "./qoder.js";
9
+
10
+ /** Extensible registry of per-host plugin upgraders; core upgrade orchestration never branches on host IDs. */
11
+ export class HostUpgraderRegistry {
12
+ readonly #upgraders = new Map<string, HostUpgrader>();
13
+
14
+ constructor(initial: readonly HostUpgrader[] = []) {
15
+ for (const upgrader of initial) this.register(upgrader);
16
+ }
17
+
18
+ register(upgrader: HostUpgrader): this {
19
+ requireThat(typeof upgrader.host === "string" && upgrader.host.length > 0, "宿主升级适配器标识无效。");
20
+ this.#upgraders.set(upgrader.host, upgrader);
21
+ return this;
22
+ }
23
+
24
+ unregister(host: string): boolean {
25
+ return this.#upgraders.delete(host);
26
+ }
27
+
28
+ get(host: string): HostUpgrader | undefined {
29
+ return this.#upgraders.get(host);
30
+ }
31
+
32
+ list(): string[] {
33
+ return [...this.#upgraders.keys()];
34
+ }
35
+ }
36
+
37
+ export const hostUpgraders = new HostUpgraderRegistry([
38
+ piUpgrader,
39
+ claudeUpgrader,
40
+ antigravityUpgrader,
41
+ codexUpgrader,
42
+ qoderUpgrader,
43
+ dshUpgrader,
44
+ ]);
45
+
46
+ export const registerHostUpgrader = (upgrader: HostUpgrader) => hostUpgraders.register(upgrader);
47
+ export const getHostUpgrader = (host: string) => hostUpgraders.get(host);
48
+ export const listHostUpgraders = () => hostUpgraders.list();
package/src/model/acp.ts CHANGED
@@ -19,7 +19,8 @@ export interface AcpCommand {
19
19
  env?: Record<string, string>;
20
20
  }
21
21
  /** ask: forward target permission requests to the parent session; allow/deny: answer them automatically. */
22
- export type AcpPermissionPolicy = "ask" | "allow" | "deny";
22
+ export const acpPermissionPolicies = ["ask", "allow", "deny"] as const;
23
+ export type AcpPermissionPolicy = typeof acpPermissionPolicies[number];
23
24
  /** Workspace `acp.<name>` entry; omitting `command` keeps a registered harness's default command. */
24
25
  export interface AcpHarnessConfig {
25
26
  command?: string;
@@ -48,9 +49,10 @@ export interface AcpModelsResult {
48
49
  export interface AcpModelState extends AcpModelSelection { generation: string }
49
50
  export interface AcpProcess { generation: string; pid: number; startedAt: number }
50
51
  export interface AcpInput { generation: string; requestId: string }
52
+ export const acpAnswerStates = ["completed", "failed", "cancelled"] as const;
51
53
  export interface AcpAnswer extends AcpInput {
52
54
  answer: string;
53
- state: "completed" | "failed" | "cancelled";
55
+ state: typeof acpAnswerStates[number];
54
56
  error?: string;
55
57
  }
56
58
  export interface AcpReport { generation: string; cycleId: string; text: string }
@@ -61,8 +63,14 @@ export interface AcpPermission {
61
63
  options: { optionId: string; name: string; kind: string }[];
62
64
  }
63
65
  export interface AcpPermissionReply { generation: string; requestId: string; optionId: string | null }
64
- export type AcpElicitationAction = "accept" | "decline" | "cancel";
66
+ export const acpElicitationActions = ["accept", "decline", "cancel"] as const;
67
+ export type AcpElicitationAction = typeof acpElicitationActions[number];
65
68
  export type AcpElicitationValue = string | number | boolean | string[];
69
+ export function isAcpElicitationValue(value: unknown): value is AcpElicitationValue {
70
+ return typeof value === "string" || typeof value === "boolean" || (typeof value === "number" && Number.isFinite(value))
71
+ || (Array.isArray(value) && value.every(item => typeof item === "string"));
72
+ }
73
+
66
74
  export type AcpElicitationContent = Record<string, AcpElicitationValue>;
67
75
  export interface AcpElicitation {
68
76
  generation: string;
@@ -9,15 +9,20 @@ export interface AgentDescriptor {
9
9
  }
10
10
  export type AgentSessionReference = AcpSessionReference;
11
11
  export interface AgentLaunch { generation: string; name: string; sequence: number; stop: boolean; startedAt: number }
12
- export interface AgentMessage { requestId: string; generation: string; sequence: number; message: string; delivery: "steer" | "followUp"; files: string[] }
12
+ export const messageDeliveries = ["steer", "followUp"] as const;
13
+ export const cycleStates = ["running", "completed", "failed", "cancelled", "interrupted"] as const;
14
+ export const cycleSources = ["parent", "interactive", "mixed"] as const;
15
+ export const agentStates = ["starting", "busy", "idle", "stopping", "stopped", "error"] as const;
16
+
17
+ export interface AgentMessage { requestId: string; generation: string; sequence: number; message: string; delivery: typeof messageDeliveries[number]; files: string[] }
13
18
  export interface AgentCycle {
14
19
  cycleId: string; generation: string; startedAt: number; finishedAt?: number;
15
- state: "running" | "completed" | "failed" | "cancelled" | "interrupted";
16
- leaf: string | null; requestIds: string[]; source: "parent" | "interactive" | "mixed";
20
+ state: typeof cycleStates[number];
21
+ leaf: string | null; requestIds: string[]; source: typeof cycleSources[number];
17
22
  error?: string;
18
23
  }
19
24
  export interface AgentStatus {
20
- generation: string; state: "starting" | "busy" | "idle" | "stopping" | "stopped" | "error";
25
+ generation: string; state: typeof agentStates[number];
21
26
  activity: string; updatedAt: number; accepted?: string[]; cycle?: AgentCycle; error?: string;
22
27
  }
23
28
  export interface AgentView extends AgentDescriptor {
@@ -39,3 +44,14 @@ export interface AgentResponseInput {
39
44
  content?: AcpElicitationContent;
40
45
  }
41
46
  export interface AgentCompletion { agentId: string; name: string; cycle: AgentCycle; report: string }
47
+
48
+ export interface AgentHost {
49
+ available(backend: AcpBackend): void;
50
+ filterOptions?(backend: AcpBackend, options: AgentOptions): AgentOptions;
51
+ prepare(backend: AcpBackend, options: AgentOptions): AcpSessionReference;
52
+ alive(id: string): boolean;
53
+ validateSession(value: AgentDescriptor, reference: AgentSessionReference): void | Promise<void>;
54
+ launch(value: AgentDescriptor, directory: string, generation: string, reference: AgentSessionReference, resume: boolean, signal: AbortSignal): void | Promise<void>;
55
+ release(id: string, generation: string): void | Promise<void>;
56
+ report(value: AgentDescriptor, reference: AgentSessionReference, leaf: string): string | Promise<string>;
57
+ }
@@ -172,6 +172,11 @@ export interface SummaryRunReport {
172
172
  warnings: string[];
173
173
  }
174
174
 
175
+ export const historyPageLimits = {
176
+ search: { default: 20, max: 50 },
177
+ read: { default: 12_000, max: 24_000 },
178
+ } as const;
179
+
175
180
  export interface HistoryQuery {
176
181
  host?: HistoryHost;
177
182
  query?: string;
@@ -0,0 +1,86 @@
1
+ export interface PluginSpec {
2
+ id: string;
3
+ label: string;
4
+ description: string;
5
+ source: string;
6
+ defaultSelected: boolean;
7
+ }
8
+ export interface InstalledPackage {
9
+ source: string;
10
+ scope: "user" | "project";
11
+ name?: string;
12
+ version?: string;
13
+ installedPath?: string;
14
+ filtered: boolean;
15
+ entry: unknown;
16
+ }
17
+ export interface PackageChoice {
18
+ plugin: PluginSpec;
19
+ required: boolean;
20
+ existing: InstalledPackage[];
21
+ selected: boolean;
22
+ keep: boolean;
23
+ target?: string;
24
+ error?: string;
25
+ }
26
+ export interface PiInstallation { command: string; root: string; version: string }
27
+ export interface SystemCommand { command: string; args: string[] }
28
+ export interface SetupPlan {
29
+ workspace: string;
30
+ pi?: PiInstallation;
31
+ piVersion: string;
32
+ tmux?: SystemCommand;
33
+ packages: PackageChoice[];
34
+ }
35
+ export interface SetupResult { name: string; state: "done" | "kept" | "skipped" | "failed"; detail: string }
36
+ export interface SetupOperations {
37
+ installTmux(command: SystemCommand): Promise<void>;
38
+ ensurePi(existing: PiInstallation | undefined, version: string): Promise<PiInstallation>;
39
+ installPackage(pi: PiInstallation, workspace: string, choice: PackageChoice): Promise<string>;
40
+ verifyKeptPackage(pi: PiInstallation, workspace: string, choice: PackageChoice): Promise<string>;
41
+ }
42
+
43
+ export type HostUpgradeState = "updated" | "up-to-date" | "skipped" | "failed";
44
+
45
+ export interface HostUpgradeContext {
46
+ workspace: string;
47
+ targetVersion: string;
48
+ currentVersion?: string;
49
+ packageRoot: string;
50
+ bindingPath?: string;
51
+ env: NodeJS.ProcessEnv;
52
+ signal: AbortSignal;
53
+ options: Readonly<Record<string, string | undefined>>;
54
+ run(command: string, args: string[], cwd: string, options?: { interactive?: boolean; timeoutMs?: number }): Promise<string>;
55
+ findExecutable(name: string): Promise<string | undefined>;
56
+ }
57
+
58
+ export interface HostUpgradeResult {
59
+ host: string;
60
+ state: HostUpgradeState;
61
+ previousVersion?: string;
62
+ version?: string;
63
+ scope?: string;
64
+ detail: string;
65
+ nextStep?: string;
66
+ }
67
+
68
+ export interface HostUpgrader {
69
+ readonly host: string;
70
+ upgrade(context: HostUpgradeContext): Promise<HostUpgradeResult>;
71
+ }
72
+
73
+ export interface CliUpgradeSummary {
74
+ state: HostUpgradeState;
75
+ previousVersion: string;
76
+ version: string;
77
+ detail: string;
78
+ }
79
+
80
+ export interface WorkspaceUpgradeSummary {
81
+ workspaceRoot: string;
82
+ targetVersion: string;
83
+ cli: CliUpgradeSummary;
84
+ results: HostUpgradeResult[];
85
+ ok: boolean;
86
+ }
@@ -24,12 +24,14 @@ export type HistoryCapture =
24
24
  | {
25
25
  kind: "input";
26
26
  sessionId: string;
27
+ turnId?: string;
27
28
  text: string;
28
29
  timestamp?: string;
29
30
  }
30
31
  | {
31
32
  kind: "answer";
32
33
  sessionId: string;
34
+ turnId?: string;
33
35
  userText?: string;
34
36
  assistantText: string;
35
37
  completed: boolean;
@@ -43,17 +45,19 @@ export type HistoryCapture =
43
45
 
44
46
  export type HostEvent =
45
47
  | { kind: "ignore" }
46
- | { kind: "inject-context"; role: WorkspaceRole; localRoleOnly?: boolean; isSessionStart?: boolean; cwd: string; raw: unknown }
48
+ | { kind: "inject-context"; role: WorkspaceRole; localRoleOnly?: boolean; isSessionStart?: boolean; noticesOnly?: boolean; cwd: string; raw: unknown }
47
49
  | { kind: "check-skill"; skill: string; cwd: string; raw: unknown }
48
50
  | { kind: "record-history"; cwd: string; raw: unknown };
49
51
 
50
52
  export interface PluginHostAdapter {
51
53
  readonly host: PluginHostId;
52
54
  readonly nativeRootAgents: boolean;
55
+ readonly indexBundledSkills?: boolean;
53
56
  readonly nativeWorkspaceSkills?: boolean;
54
57
  readonly coordinatorPrompt?: string;
55
58
  readonly initCommandHint: string;
56
59
  readonly usage: string;
60
+ boundWorkspaceRoot?(): string | undefined;
57
61
  resolveRoot(cwd: string, rawPayload?: unknown): Promise<string>;
58
62
  parseEvent(payload: unknown): HostEvent;
59
63
  extractHistory?(event: Extract<HostEvent, { kind: "inject-context" | "record-history" }>): Promise<HistoryCapture | undefined>;
@@ -7,12 +7,9 @@ export interface WorkspaceFeatures {
7
7
  history?: boolean;
8
8
  }
9
9
 
10
- export interface ResolvedWorkspaceFeatures {
11
- readonly prompts: boolean;
12
- readonly skills: boolean;
13
- readonly mcp: boolean;
14
- readonly history: boolean;
15
- }
10
+ export type ResolvedWorkspaceFeatures = Readonly<Required<WorkspaceFeatures>>;
11
+
12
+ export const summaryConcurrency = { default: 3, max: 8 } as const;
16
13
 
17
14
  export interface WorkspaceSummaryConfig {
18
15
  backend?: string;
@@ -28,7 +25,8 @@ export interface ResolvedWorkspaceSummaryConfig {
28
25
  readonly concurrency: number;
29
26
  }
30
27
 
31
- export type HistoryLayout = "monthly" | "flat";
28
+ export const historyLayouts = ["monthly", "flat"] as const;
29
+ export type HistoryLayout = typeof historyLayouts[number];
32
30
 
33
31
  export interface WorkspaceHistoryConfig {
34
32
  layout?: HistoryLayout;
@@ -46,13 +44,9 @@ export interface WorkspaceConfig {
46
44
  history?: WorkspaceHistoryConfig;
47
45
  }
48
46
 
49
- export interface WorkspaceConfiguration {
47
+ export interface WorkspaceConfiguration extends WorkspaceConfig {
50
48
  path: string;
51
49
  present: boolean;
52
50
  enabled: boolean;
53
51
  error?: string;
54
- features?: WorkspaceFeatures;
55
- acp?: AcpCommands;
56
- summary?: WorkspaceSummaryConfig;
57
- history?: WorkspaceHistoryConfig;
58
52
  }
package/src/pbot.ts CHANGED
@@ -1,16 +1,13 @@
1
- import { existsSync } from "node:fs";
2
- import { resolve } from "node:path";
3
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
4
- import { resourceRoot } from "./common/paths.js";
5
2
  import { Confirmations } from "./ui/pi/confirmation.js";
6
- import { ToolRegistry, mainTools } from "./tools/register.js";
3
+ import { registerPiHistoryTool } from "./tools/history/pi-tool.js";
7
4
  import { Lifecycle, WorkspaceInstance } from "./extension/hosts/pi/lifecycle.js";
8
5
  import { registerCommand } from "./extension/hosts/pi/command.js";
9
6
  import { registerEvents } from "./extension/hosts/pi/events.js";
10
7
  import { registerHistoryEvents } from "./extension/hosts/pi/history.js";
11
8
  import { McpWorkerRuntime, defaultWorkerDirectory } from "./extension/mcp/workers.js";
12
9
  import { workspaceFeatures } from "./services/workspace/workspace-config.js";
13
- import { readWorkspaceMcpConfig, resolveWorkspaceMcpServer } from "./services/workspace/workspace-mcp.js";
10
+ import { loadNativeWorkspaceServers } from "./extension/hosts/workspace-mcp.js";
14
11
  import { toPiMcpConfig } from "./integrations/hosts/pi/mcp.js";
15
12
 
16
13
  const mcpServerName = "pbot";
@@ -19,8 +16,7 @@ export default function pbot(pi: ExtensionAPI) {
19
16
  // The explicitly loaded child entry owns the child; normal discovery never grants main privileges.
20
17
  if (process.env.PBOT_SUBAGENT_RUN_ID) return;
21
18
  const confirmations = new Confirmations();
22
- const registry = new ToolRegistry(pi);
23
- const mcpEntry = resolve(resourceRoot, "../lib/extension/mcp.js");
19
+ let hideHistory: (() => void) | undefined;
24
20
  const registeredMcpServers: string[] = [];
25
21
 
26
22
  const lifecycle = new Lifecycle(
@@ -30,20 +26,12 @@ export default function pbot(pi: ExtensionAPI) {
30
26
  return new WorkspaceInstance(root, sessionId, workers, workspaceFeatures(configuration));
31
27
  },
32
28
  async instance => {
33
- if (instance.features.history) registry.register(mainTools(instance.root, instance));
34
- if (existsSync(mcpEntry)) {
29
+ if (instance.features.history) hideHistory = registerPiHistoryTool(pi, instance.root, instance);
30
+ const server = instance.workers.nativeServer(instance.root);
31
+ if (server) {
35
32
  pi.registerMcpServer(mcpServerName, {
36
33
  type: "stdio",
37
- command: process.execPath,
38
- args: [mcpEntry],
39
- env: {
40
- PBOT_HOST: "pi",
41
- PBOT_WORKSPACE_ROOT: instance.root,
42
- PBOT_MCP_AGENT_DIR: defaultWorkerDirectory(),
43
- PBOT_MCP_SESSION_ID: instance.sessionId,
44
- PBOT_NATIVE_WORKSPACE_MCP: "1",
45
- PBOT_NATIVE_HISTORY: "1",
46
- },
34
+ ...server,
47
35
  exposure: "direct",
48
36
  timeout: 600,
49
37
  description: "跨代理程序委派(worker_*)",
@@ -51,22 +39,15 @@ export default function pbot(pi: ExtensionAPI) {
51
39
  registeredMcpServers.push(mcpServerName);
52
40
  }
53
41
  if (instance.features.mcp) {
54
- const mcpConfig = await readWorkspaceMcpConfig(instance.root);
55
- instance.check();
56
- if (!mcpConfig.error) {
57
- for (const [name, server] of Object.entries(mcpConfig.servers)) {
58
- if (server.enabled === false) continue;
59
- try {
60
- const resolved = resolveWorkspaceMcpServer(instance.root, name, server);
61
- pi.registerMcpServer(resolved.name, toPiMcpConfig(resolved));
62
- registeredMcpServers.push(resolved.name);
63
- } catch { /* Keep the workspace active even if one external MCP env var is unset. */ }
64
- }
65
- }
42
+ await loadNativeWorkspaceServers(instance.root, server => {
43
+ pi.registerMcpServer(server.name, toPiMcpConfig(server));
44
+ registeredMcpServers.push(server.name);
45
+ }, { check: () => instance.check() });
66
46
  }
67
47
  },
68
48
  () => {
69
- registry.hide();
49
+ hideHistory?.();
50
+ hideHistory = undefined;
70
51
  for (const name of registeredMcpServers.splice(0)) {
71
52
  pi.unregisterMcpServer(name);
72
53
  }
package/src/plugin.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { realpathSync } from "node:fs";
2
2
  import { pathToFileURL } from "node:url";
3
3
  import { requireThat } from "./common/validation.js";
4
- import { runPluginHost } from "./extension/hosts/plugin-runtime.js";
4
+ import { runPluginHost, type ReplayPluginHook } from "./extension/hosts/plugin-runtime.js";
5
5
  import { listPluginHosts, pluginHosts } from "./integrations/hosts/plugin-registry.js";
6
6
 
7
- export async function main(args: readonly string[]): Promise<void> {
7
+ export async function main(args: readonly string[], replayHook?: ReplayPluginHook): Promise<void> {
8
8
  const [host, ...rest] = args;
9
9
  const adapter = host ? pluginHosts.get(host) : undefined;
10
10
  requireThat(adapter, `用法:node <插件目录>/lib/plugin.js <${listPluginHosts().join("|")}> hook|init|status`);
11
- await runPluginHost(adapter, rest);
11
+ await runPluginHost(adapter, rest, replayHook);
12
12
  }
13
13
 
14
14
  if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
@@ -5,24 +5,14 @@ import { object } from "../../common/json.js";
5
5
  import { packageVersion, releaseLine } from "../../common/version.js";
6
6
  import { requireThat } from "../../common/validation.js";
7
7
  import { AgentFiles, descriptor, launch, status, cycle } from "../../storage/agent-files.js";
8
- import type { AgentOwner, AgentOptions, AgentPendingRequest, AgentResponseInput, AgentStartOptions, AgentDescriptor, AgentCycle, AgentSessionReference, AgentView } from "../../model/agent.js";
9
- import type { AcpBackend, AcpSessionReference } from "../../model/acp.js";
8
+ import type { AgentOwner, AgentOptions, AgentMessage, AgentPendingRequest, AgentResponseInput, AgentStartOptions, AgentDescriptor, AgentCycle, AgentHost, AgentView } from "../../model/agent.js";
10
9
  import { acpElicitationReply, acpModelState, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
11
10
 
12
- export interface AgentHost {
13
- available(backend: AcpBackend): void;
14
- filterOptions?(backend: AcpBackend, options: AgentOptions): AgentOptions;
15
- prepare(backend: AcpBackend, options: AgentOptions): AcpSessionReference;
16
- alive(id: string): boolean;
17
- validateSession(value: AgentDescriptor, reference: AgentSessionReference): void | Promise<void>;
18
- launch(value: AgentDescriptor, directory: string, generation: string, reference: AgentSessionReference, resume: boolean, signal: AbortSignal): void | Promise<void>;
19
- release(id: string, generation: string): void | Promise<void>;
20
- report(value: AgentDescriptor, reference: AgentSessionReference, leaf: string): string | Promise<string>;
21
- }
11
+ export type { AgentHost } from "../../model/agent.js";
22
12
 
23
13
  /** Host-neutral lifecycle manager for cross-harness ACP workers. */
24
14
  export class AgentService {
25
- readonly files: AgentFiles;
15
+ private readonly files: AgentFiles;
26
16
  #closed = false;
27
17
  #preserve = false;
28
18
  readonly #starting = new Map<string, { generation: string; controller: AbortController; stage: "validate" | "launch" }>();
@@ -218,7 +208,7 @@ export class AgentService {
218
208
  try { await operation; } finally { this.#launches.delete(operation); }
219
209
  }
220
210
 
221
- send(id: string, text: string, delivery: "steer" | "followUp", owner: AgentOwner, attachments: readonly string[] = []) {
211
+ send(id: string, text: string, delivery: AgentMessage["delivery"], owner: AgentOwner, attachments: readonly string[] = []) {
222
212
  const d = this.get(id, owner);
223
213
  return this.files.control(() => {
224
214
  requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "worker 已停止或仍在恢复校验,请等待显式恢复完成。");
@@ -298,7 +288,12 @@ export class AgentService {
298
288
  }
299
289
 
300
290
  cycles(id: string, owner: AgentOwner): AgentCycle[] {
301
- const d = this.get(id, owner), view = this.view(d, owner), values = this.files.completions(id);
291
+ return this.cyclesFor(this.view(this.get(id, owner), owner));
292
+ }
293
+
294
+ private cyclesFor(view: AgentView): AgentCycle[] {
295
+ const id = view.agentId;
296
+ const values = this.files.completions(id);
302
297
  if (view.cycle && !values.some(c => c.cycleId === view.cycle!.cycleId)) {
303
298
  values.push(view.state === "stopped" || view.state === "error" ? { ...view.cycle, state: "interrupted", finishedAt: view.cycle.finishedAt ?? Date.now() } : view.cycle);
304
299
  }
@@ -321,9 +316,26 @@ export class AgentService {
321
316
  return values;
322
317
  }
323
318
 
319
+ private snapshot(id: string, owner: AgentOwner) {
320
+ const view = this.view(this.get(id, owner), owner);
321
+ const cycles = this.cyclesFor(view);
322
+ const latest = cycles.at(-1);
323
+ return { current: latest ? { ...view, cycle: latest } : view, cycles };
324
+ }
325
+
324
326
  status(id: string, owner: AgentOwner) {
325
- const d = this.get(id, owner), latest = this.cycles(id, owner).at(-1);
326
- return { ...this.view(d, owner), ...(latest ? { cycle: latest } : {}) };
327
+ return this.snapshot(id, owner).current;
328
+ }
329
+
330
+ outputStatus(id: string, owner: AgentOwner, requestId?: string) {
331
+ const { current, cycles } = this.snapshot(id, owner);
332
+ const request = requestId ?? this.files.messages(id).findLast(item => item.generation === current.generation)?.requestId;
333
+ const cycle = request
334
+ ? cycles.find(item => item.generation === current.generation && item.requestIds.includes(request))
335
+ : current.cycle;
336
+ const stopped = current.state === "stopped" || current.state === "error";
337
+ const terminal = request ? stopped && !cycle : stopped || (current.state === "idle" && !cycle);
338
+ return { current, cycle, terminal };
327
339
  }
328
340
 
329
341
  async read(id: string, owner: AgentOwner, cycleId?: string, offset = 0, limit = 16000) {
@@ -1,10 +1,8 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import { join } from "node:path";
3
2
  import { isDeepStrictEqual } from "node:util";
4
3
  import type { HistoryExchange, HistoryHost, HistorySession, RawHistory } from "../../model/history.js";
5
4
  import type { HistoryCapture } from "../../model/plugin-host.js";
6
- import { HistoryFiles } from "../../storage/history-files.js";
7
- import { readWorkspaceHistoryLayoutSync } from "../workspace/workspace-config.js";
5
+ import { workspaceHistoryFiles } from "./workspace-history.js";
8
6
  import { indexHistory, orderedRawExchanges } from "./history-index.js";
9
7
 
10
8
  function orderedExchanges(raw?: RawHistory): HistoryExchange[] {
@@ -20,9 +18,12 @@ function orderedExchanges(raw?: RawHistory): HistoryExchange[] {
20
18
 
21
19
  function replaceAssistantSuffix(text: string, assistantText: string): string {
22
20
  const marker = "\n\n助手:\n";
21
+ const section = `助手:\n${assistantText}`;
22
+ if (text === section || text.endsWith("\n\n" + section)) return text;
23
+ if (!text || text.startsWith("助手:\n")) return section;
23
24
  const index = text.lastIndexOf(marker);
24
25
  if (index >= 0) return text.slice(0, index) + marker + assistantText;
25
- return text ? `${text}${marker}${assistantText}` : `助手:\n${assistantText}`;
26
+ return text + marker + assistantText;
26
27
  }
27
28
 
28
29
  function buildSession(host: HistoryHost, capture: HistoryCapture, previous?: RawHistory): HistorySession | undefined {
@@ -31,31 +32,39 @@ function buildSession(host: HistoryHost, capture: HistoryCapture, previous?: Raw
31
32
  }
32
33
  const timestamp = capture.timestamp ?? new Date().toISOString();
33
34
  const branch = orderedExchanges(previous);
35
+ // entryIds already identify native observations on disk. A turn keeps its ID
36
+ // across steering, attachment-only inputs and Stop hooks arriving out of order.
37
+ const turnEntry = capture.turnId ? `${host}:turn:${capture.turnId}` : undefined;
38
+ const turn = turnEntry ? branch.find(item => item.entryIds.includes(turnEntry)) : undefined;
39
+ const entryId = () => turnEntry ?? `${host}:${Date.now()}:${randomUUID().slice(0, 8)}`;
34
40
  if (capture.kind === "input") {
35
41
  const text = capture.text.trim();
36
- if (!text) return undefined;
37
- const entryId = `${host}:${Date.now()}:${randomUUID().slice(0, 8)}`;
38
- branch.push({
39
- entryIds: [entryId],
40
- text: "用户:\n" + text,
41
- from: timestamp,
42
- to: timestamp,
43
- completed: false,
44
- });
42
+ if (!text && !turnEntry) return undefined;
43
+ const input = "用户:\n" + (text || "(无文本输入)");
44
+ if (turn) {
45
+ turn.text += "\n\n" + input;
46
+ turn.to = timestamp;
47
+ turn.completed = false;
48
+ } else {
49
+ branch.push({ entryIds: [entryId()], text: input, from: timestamp, to: timestamp, completed: false });
50
+ }
45
51
  return { sessionId: capture.sessionId, name: previous?.name ?? "", branches: [branch] };
46
52
  }
47
53
  const assistantText = capture.assistantText.trim();
48
54
  if (!assistantText) return undefined;
49
- const last = branch.at(-1);
50
- if (last && (!last.completed || capture.isContinuation)) {
51
- last.text = replaceAssistantSuffix(last.text, assistantText);
52
- last.to = timestamp;
53
- last.completed = capture.completed;
55
+ const target = turnEntry ? turn : branch.at(-1);
56
+ if (target && (turnEntry || !target.completed || capture.isContinuation)) {
57
+ const updated = turnEntry && !target.completed
58
+ ? target.text + "\n\n助手:\n" + assistantText
59
+ : replaceAssistantSuffix(target.text, assistantText);
60
+ if (target.text === updated && target.completed === capture.completed) return undefined;
61
+ target.text = updated;
62
+ target.to = timestamp;
63
+ target.completed = capture.completed;
54
64
  } else {
55
- const entryId = `${host}:${Date.now()}:${randomUUID().slice(0, 8)}`;
56
65
  const userText = capture.userText?.trim();
57
66
  branch.push({
58
- entryIds: [entryId],
67
+ entryIds: [entryId()],
59
68
  text: userText ? `用户:\n${userText}\n\n助手:\n${assistantText}` : `助手:\n${assistantText}`,
60
69
  from: timestamp,
61
70
  to: timestamp,
@@ -69,8 +78,7 @@ function buildSession(host: HistoryHost, capture: HistoryCapture, previous?: Raw
69
78
  export function recordHistoryCapture(root: string, host: HistoryHost, capture: HistoryCapture): void {
70
79
  const sessionId = capture.kind === "session" ? capture.session.sessionId : capture.sessionId;
71
80
  if (!sessionId) return;
72
- const layout = readWorkspaceHistoryLayoutSync(root);
73
- const files = new HistoryFiles(join(root, ".pbot/history"), host, layout);
81
+ const files = workspaceHistoryFiles(root, host);
74
82
  files.control(() => {
75
83
  const previous = files.listRaw(host).find(raw => raw.sessionId === sessionId);
76
84
  if (!previous && files.rawWarnings.length) return;