personal-bot 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (298) 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 +8 -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 +7 -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 +31 -23
  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 +20127 -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 +3 -0
  88. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +78 -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 +31 -23
  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 +3 -0
  151. package/lib/extension/workspace-cli.js +20127 -0
  152. package/lib/integrations/acp/agents.d.ts +2 -3
  153. package/lib/integrations/acp/harnesses/dsh.js +1 -1
  154. package/lib/integrations/acp/harnesses/presets.js +8 -0
  155. package/lib/integrations/acp/history-summary.js +2 -1
  156. package/lib/integrations/acp/launch.d.ts +2 -2
  157. package/lib/integrations/agent-resources.js +1 -1
  158. package/lib/integrations/hosts/claude/adapter.js +7 -59
  159. package/lib/integrations/hosts/codex/adapter.d.ts +2 -0
  160. package/lib/integrations/hosts/codex/adapter.js +14 -0
  161. package/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
  162. package/lib/integrations/hosts/pi/agent-tools.js +32 -0
  163. package/lib/integrations/hosts/pi/mcp.d.ts +4 -0
  164. package/lib/integrations/hosts/pi/mcp.js +23 -0
  165. package/lib/integrations/hosts/plugin-registry.d.ts +1 -0
  166. package/lib/integrations/hosts/plugin-registry.js +8 -1
  167. package/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
  168. package/lib/integrations/hosts/prompt-hooks.js +76 -0
  169. package/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
  170. package/lib/integrations/hosts/qoder/adapter.js +16 -0
  171. package/lib/model/acp.d.ts +7 -3
  172. package/lib/model/acp.js +8 -0
  173. package/lib/model/agent.d.ts +18 -4
  174. package/lib/model/agent.js +4 -1
  175. package/lib/model/history.d.ts +10 -0
  176. package/lib/model/history.js +4 -1
  177. package/lib/model/installation.js +1 -0
  178. package/lib/model/plugin-host.d.ts +5 -0
  179. package/lib/model/workspace-config.d.ts +8 -12
  180. package/lib/model/workspace-config.js +2 -1
  181. package/lib/plugin.d.ts +2 -1
  182. package/lib/plugin.js +2 -2
  183. package/lib/qoder-plugin/.mcp.json +17 -0
  184. package/lib/qoder-plugin/.qoder-plugin/plugin.json +7 -0
  185. package/lib/qoder-plugin/DISCLAIMER.md +17 -0
  186. package/lib/qoder-plugin/LICENSE +21 -0
  187. package/lib/qoder-plugin/hooks/hooks.json +37 -0
  188. package/lib/qoder-plugin/skills/pbot/SKILL.md +9 -0
  189. package/lib/services/agents/agent-service.d.ts +15 -45
  190. package/lib/services/agents/agent-service.js +22 -3
  191. package/lib/services/history/history-capture.js +34 -22
  192. package/lib/services/history/history-migrate.js +26 -60
  193. package/lib/services/history/history-query.js +38 -57
  194. package/lib/services/history/summary-command.d.ts +1 -1
  195. package/lib/services/history/summary-command.js +8 -7
  196. package/lib/services/history/summary-registry.js +2 -3
  197. package/lib/services/history/summary-runner.d.ts +2 -5
  198. package/lib/services/history/summary-runner.js +72 -74
  199. package/lib/services/history/workspace-history.d.ts +3 -0
  200. package/lib/services/history/workspace-history.js +6 -0
  201. package/lib/services/workspace/plugin-releases.d.ts +3 -0
  202. package/lib/services/workspace/plugin-releases.js +78 -0
  203. package/lib/services/workspace/workspace-config.js +9 -8
  204. package/lib/services/workspace/workspace-mcp.js +22 -29
  205. package/lib/services/workspace/workspace-skills.d.ts +1 -0
  206. package/lib/services/workspace/workspace-skills.js +4 -2
  207. package/lib/storage/acp-files.js +7 -12
  208. package/lib/storage/agent-files.js +6 -5
  209. package/lib/storage/atomic-json.d.ts +3 -1
  210. package/lib/storage/atomic-json.js +43 -3
  211. package/lib/storage/history-files.d.ts +2 -1
  212. package/lib/storage/history-files.js +21 -15
  213. package/lib/storage/history-format.d.ts +2 -3
  214. package/lib/storage/history-format.js +5 -7
  215. package/lib/tools/history/contract.d.ts +3 -11
  216. package/lib/tools/history/contract.js +8 -7
  217. package/lib/ui/dsh/elements.d.ts +2 -5
  218. package/lib/ui/dsh/elements.js +3 -16
  219. package/lib/ui/dsh/management.js +9 -9
  220. package/lib/ui/dsh/styles.d.ts +1 -1
  221. package/lib/ui/dsh/styles.js +5 -33
  222. package/package.json +4 -1
  223. package/resources/antigravity/skills/pbot/SKILL.md +11 -1
  224. package/resources/claude/skills/pbot/SKILL.md +11 -1
  225. package/resources/plugin/skills/pbot/SKILL.md +9 -0
  226. package/resources/prompts/history-coordinator.md +1 -0
  227. package/resources/prompts/plugin-coordinator.md +1 -0
  228. package/scripts/README.md +3 -2
  229. package/src/common/validation.ts +10 -0
  230. package/src/dsh.ts +2 -2
  231. package/src/extension/history/command.ts +68 -0
  232. package/src/extension/hosts/dsh/acp-entry.ts +21 -0
  233. package/src/extension/hosts/dsh/acp-member.ts +4 -15
  234. package/src/extension/hosts/dsh/command.ts +6 -46
  235. package/src/extension/hosts/dsh/workspace.ts +12 -38
  236. package/src/extension/hosts/pi/acp-member.ts +4 -10
  237. package/src/extension/hosts/pi/command.ts +14 -63
  238. package/src/extension/hosts/pi/events.ts +2 -8
  239. package/src/extension/hosts/pi/lifecycle.ts +3 -2
  240. package/src/extension/hosts/plugin-runtime.ts +32 -19
  241. package/src/extension/hosts/workspace-mcp.ts +25 -0
  242. package/src/extension/mcp/bridge.ts +80 -80
  243. package/src/extension/mcp/tool-contract.ts +462 -0
  244. package/src/extension/mcp/workers.ts +81 -551
  245. package/src/extension/setup/cli.ts +4 -1
  246. package/src/extension/setup/workspace-cli.ts +159 -0
  247. package/src/integrations/acp/agents.ts +2 -3
  248. package/src/integrations/acp/harnesses/dsh.ts +1 -1
  249. package/src/integrations/acp/harnesses/presets.ts +8 -0
  250. package/src/integrations/acp/history-summary.ts +2 -1
  251. package/src/integrations/agent-resources.ts +1 -1
  252. package/src/integrations/hosts/claude/adapter.ts +7 -53
  253. package/src/integrations/hosts/codex/adapter.ts +16 -0
  254. package/src/integrations/hosts/plugin-registry.ts +9 -1
  255. package/src/integrations/hosts/prompt-hooks.ts +72 -0
  256. package/src/integrations/hosts/qoder/adapter.ts +19 -0
  257. package/src/integrations/installation/cli-update.ts +116 -0
  258. package/src/integrations/installation/pi-bridge.ts +1 -1
  259. package/src/integrations/installation/pi-setup.ts +1 -1
  260. package/src/integrations/installation/processes.ts +1 -1
  261. package/src/model/acp.ts +11 -3
  262. package/src/model/agent.ts +20 -4
  263. package/src/model/history.ts +5 -0
  264. package/src/model/installation.ts +41 -0
  265. package/src/model/plugin-host.ts +5 -1
  266. package/src/model/workspace-config.ts +6 -12
  267. package/src/pbot.ts +13 -32
  268. package/src/plugin.ts +3 -3
  269. package/src/services/agents/agent-service.ts +29 -17
  270. package/src/services/history/history-capture.ts +30 -22
  271. package/src/services/history/history-migrate.ts +23 -50
  272. package/src/services/history/history-query.ts +34 -48
  273. package/src/services/history/summary-command.ts +9 -8
  274. package/src/services/history/summary-registry.ts +2 -3
  275. package/src/services/history/summary-runner.ts +72 -81
  276. package/src/services/history/workspace-history.ts +8 -0
  277. package/src/services/installation/setup-service.ts +2 -41
  278. package/src/services/workspace/plugin-releases.ts +68 -0
  279. package/src/services/workspace/workspace-config.ts +9 -8
  280. package/src/services/workspace/workspace-mcp.ts +23 -28
  281. package/src/services/workspace/workspace-skills.ts +5 -2
  282. package/src/storage/acp-files.ts +7 -12
  283. package/src/storage/agent-files.ts +6 -5
  284. package/src/storage/atomic-json.ts +31 -4
  285. package/src/storage/history-files.ts +22 -15
  286. package/src/storage/history-format.ts +6 -8
  287. package/src/tools/history/contract.ts +9 -9
  288. package/src/tools/history/pi-tool.ts +6 -4
  289. package/src/ui/dsh/elements.ts +5 -12
  290. package/src/ui/dsh/management.ts +10 -10
  291. package/src/ui/dsh/styles.ts +5 -33
  292. package/lib/antigravity-plugin/lib/services/history/history-migrate.d.ts +0 -12
  293. package/lib/antigravity-plugin/lib/services/history/history-migrate.js +0 -88
  294. package/lib/ui/presentation.d.ts +0 -3
  295. package/lib/ui/presentation.js +0 -12
  296. package/src/tools/access.ts +0 -7
  297. package/src/tools/register.ts +0 -21
  298. package/src/ui/presentation.ts +0 -12
@@ -2,7 +2,7 @@ import { mkdir, readFile } from "node:fs/promises";
2
2
  import { dirname, join, resolve } from "node:path";
3
3
  import { object } from "../../common/json.js";
4
4
  import { requireThat } from "../../common/validation.js";
5
- import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
5
+ import { updateJsonFile } from "../../storage/atomic-json.js";
6
6
  const serverNamePattern = /^[A-Za-z0-9][A-Za-z0-9_-]{0,31}$/;
7
7
  const reservedServerNames = new Set(["pbot"]);
8
8
  const stdioKeys = new Set(["command", "args", "env", "cwd", "description", "timeout", "enabled"]);
@@ -17,12 +17,10 @@ export function isWorkspaceMcpHttpServer(server) {
17
17
  }
18
18
  function validateStringRecord(value, label) {
19
19
  requireThat(object(value), `${label} 必须是字符串映射。`);
20
- const result = {};
21
- for (const [key, entry] of Object.entries(value)) {
20
+ return Object.fromEntries(Object.entries(value).map(([key, entry]) => {
22
21
  requireThat(key.length > 0 && !key.includes("\0") && typeof entry === "string" && !entry.includes("\0"), `${label}.${key} 必须是非空键与无空字节字符串值。`);
23
- result[key] = entry;
24
- }
25
- return result;
22
+ return [key, entry];
23
+ }));
26
24
  }
27
25
  export function validateWorkspaceMcpServer(name, value) {
28
26
  requireThat(serverNamePattern.test(name), `MCP 服务名无效:${name}(需匹配 [A-Za-z0-9][A-Za-z0-9_-]{0,31})。`);
@@ -90,7 +88,7 @@ function validateMcpFile(value) {
90
88
  for (const [name, entry] of Object.entries(rawServers)) {
91
89
  servers[name] = validateWorkspaceMcpServer(name, entry);
92
90
  }
93
- return { mcpServers: servers };
91
+ return { ...value, mcpServers: servers };
94
92
  }
95
93
  export function workspaceMcpPath(root) {
96
94
  return join(root, ".pbot", "mcp.json");
@@ -121,37 +119,32 @@ export function expandEnvTemplate(value, env, context) {
121
119
  return resolved;
122
120
  });
123
121
  }
122
+ function expandEnvRecord(values, env, context) {
123
+ return Object.fromEntries(Object.entries(values ?? {}).map(([key, value]) => [key, expandEnvTemplate(value, env, `${context}.${key}`)]));
124
+ }
124
125
  export function resolveWorkspaceMcpServer(root, name, server, env = process.env) {
125
126
  const timeout = server.timeout ?? defaultTimeoutSeconds;
126
127
  if (isWorkspaceMcpHttpServer(server)) {
127
128
  const url = expandEnvTemplate(server.url, env, `MCP 服务 "${name}" 的 url`);
128
129
  requireThat(/^https?:\/\/.+/.test(url), `MCP 服务 "${name}" 展开后的 url 必须以 http:// 或 https:// 开头。`);
129
- const headers = {};
130
- for (const [key, value] of Object.entries(server.headers ?? {})) {
131
- headers[key] = expandEnvTemplate(value, env, `MCP 服务 "${name}" 的 headers.${key}`);
132
- }
133
130
  return {
134
131
  transport: "http",
135
132
  name,
136
133
  url,
137
- headers,
134
+ headers: expandEnvRecord(server.headers, env, `MCP 服务 "${name}" 的 headers`),
138
135
  ...(server.description ? { description: server.description } : {}),
139
136
  timeout,
140
137
  };
141
138
  }
142
139
  const command = expandEnvTemplate(server.command, env, `MCP 服务 "${name}" 的 command`);
143
140
  const args = (server.args ?? []).map((arg, index) => expandEnvTemplate(arg, env, `MCP 服务 "${name}" 的 args[${index}]`));
144
- const resolvedEnv = {};
145
- for (const [key, value] of Object.entries(server.env ?? {})) {
146
- resolvedEnv[key] = expandEnvTemplate(value, env, `MCP 服务 "${name}" 的 env.${key}`);
147
- }
148
141
  const cwd = server.cwd ? resolve(root, expandEnvTemplate(server.cwd, env, `MCP 服务 "${name}" 的 cwd`)) : root;
149
142
  return {
150
143
  transport: "stdio",
151
144
  name,
152
145
  command,
153
146
  args,
154
- env: resolvedEnv,
147
+ env: expandEnvRecord(server.env, env, `MCP 服务 "${name}" 的 env`),
155
148
  cwd,
156
149
  ...(server.description ? { description: server.description } : {}),
157
150
  timeout,
@@ -175,11 +168,10 @@ export async function upsertWorkspaceMcpServer(root, name, server) {
175
168
  throw new Error(current.error);
176
169
  const directory = dirname(current.path);
177
170
  await mkdir(directory, { recursive: true });
178
- const existing = current.present ? readJsonFile(directory, "mcp.json", validateMcpFile) : { mcpServers: {} };
179
- const next = { mcpServers: { ...existing.mcpServers, [name]: validated } };
180
- const written = writeJsonFile(directory, "mcp.json", next, validateMcpFile, !current.present);
181
- if (!written)
182
- return upsertWorkspaceMcpServer(root, name, server);
171
+ await updateJsonFile(directory, "mcp.json", validateMcpFile, existing => ({
172
+ ...existing,
173
+ mcpServers: { ...existing?.mcpServers, [name]: validated },
174
+ }));
183
175
  return readWorkspaceMcpConfig(root);
184
176
  }
185
177
  export async function removeWorkspaceMcpServer(root, name) {
@@ -190,11 +182,12 @@ export async function removeWorkspaceMcpServer(root, name) {
190
182
  return { removed: false, configuration: current };
191
183
  }
192
184
  const directory = dirname(current.path);
193
- const existing = readJsonFile(directory, "mcp.json", validateMcpFile);
194
- if (!Object.hasOwn(existing.mcpServers, name)) {
195
- return { removed: false, configuration: await readWorkspaceMcpConfig(root) };
196
- }
197
- const remaining = Object.fromEntries(Object.entries(existing.mcpServers).filter(([key]) => key !== name));
198
- writeJsonFile(directory, "mcp.json", { mcpServers: remaining }, validateMcpFile);
199
- return { removed: true, configuration: await readWorkspaceMcpConfig(root) };
185
+ const removed = await updateJsonFile(directory, "mcp.json", validateMcpFile, existing => {
186
+ if (!existing || !Object.hasOwn(existing.mcpServers, name))
187
+ return undefined;
188
+ const remaining = { ...existing.mcpServers };
189
+ delete remaining[name];
190
+ return { ...existing, mcpServers: remaining };
191
+ });
192
+ return { removed, configuration: await readWorkspaceMcpConfig(root) };
200
193
  }
@@ -6,3 +6,4 @@ export declare function parseSkillFrontmatter(content: string): {
6
6
  } | undefined;
7
7
  /** Scan `<workspace>/.agents/skills/<dir>/SKILL.md` for workspace-authored skills. */
8
8
  export declare function discoverWorkspaceSkills(root: string): Promise<WorkspaceSkillMeta[]>;
9
+ export declare function discoverSkills(skillsDir: string, indexPath: string): Promise<WorkspaceSkillMeta[]>;
@@ -55,7 +55,9 @@ export function parseSkillFrontmatter(content) {
55
55
  }
56
56
  /** Scan `<workspace>/.agents/skills/<dir>/SKILL.md` for workspace-authored skills. */
57
57
  export async function discoverWorkspaceSkills(root) {
58
- const skillsDir = join(root, ".agents", "skills");
58
+ return discoverSkills(join(root, ".agents", "skills"), ".agents/skills");
59
+ }
60
+ export async function discoverSkills(skillsDir, indexPath) {
59
61
  let entries;
60
62
  try {
61
63
  entries = await readdir(skillsDir, { withFileTypes: true });
@@ -83,7 +85,7 @@ export async function discoverWorkspaceSkills(root) {
83
85
  results.push({
84
86
  name: meta.name,
85
87
  description: meta.description,
86
- path: `.agents/skills/${entry.name}/SKILL.md`,
88
+ path: join(indexPath, entry.name, "SKILL.md"),
87
89
  });
88
90
  }
89
91
  results.sort((a, b) => a.name.localeCompare(b.name));
@@ -1,7 +1,8 @@
1
1
  import { isAbsolute } from "node:path";
2
2
  import { object } from "../common/json.js";
3
- import { isAcpBackend, requireThat } from "../common/validation.js";
3
+ import { isAcpBackend, isOneOf, requireThat } from "../common/validation.js";
4
4
  import { fileId } from "./file-access.js";
5
+ import { acpAnswerStates, acpElicitationActions, acpPermissionPolicies, isAcpElicitationValue } from "../model/acp.js";
5
6
  const text = (value) => typeof value === "string" && value.length > 0 && !value.includes("\0");
6
7
  export function acpReference(value) {
7
8
  requireThat(object(value) && value.host === "acp" && isAcpBackend(value.backend) &&
@@ -10,7 +11,7 @@ export function acpReference(value) {
10
11
  if (value.env !== undefined) {
11
12
  requireThat(object(value.env) && Object.entries(value.env).every(([k, v]) => text(k) && typeof v === "string" && !v.includes("\0")), "ACP 环境变量配置无效。");
12
13
  }
13
- requireThat(value.permission === undefined || ["ask", "allow", "deny"].includes(String(value.permission)), "ACP 权限策略无效。");
14
+ requireThat(value.permission === undefined || isOneOf(value.permission, acpPermissionPolicies), "ACP 权限策略无效。");
14
15
  const capabilities = value.capabilities;
15
16
  if (capabilities !== undefined)
16
17
  requireThat(object(capabilities) &&
@@ -33,7 +34,7 @@ export function acpInput(value) {
33
34
  }
34
35
  export function acpAnswer(value) {
35
36
  acpInput(value);
36
- requireThat(object(value) && typeof value.answer === "string" && ["completed", "failed", "cancelled"].includes(String(value.state)) &&
37
+ requireThat(object(value) && typeof value.answer === "string" && isOneOf(value.state, acpAnswerStates) &&
37
38
  (value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
38
39
  return value;
39
40
  }
@@ -44,7 +45,7 @@ export function acpReport(value) {
44
45
  }
45
46
  export function acpPermission(value) {
46
47
  acpInput(value);
47
- requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && ["allow_once", "allow_always", "reject_once", "reject_always"].includes(String(option.kind))), "ACP 确认请求无效。");
48
+ requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
48
49
  return value;
49
50
  }
50
51
  export function acpPermissionReply(value) {
@@ -52,12 +53,6 @@ export function acpPermissionReply(value) {
52
53
  requireThat(object(value) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
53
54
  return value;
54
55
  }
55
- function elicitationValue(value) {
56
- return typeof value === "string"
57
- || (typeof value === "number" && Number.isFinite(value))
58
- || typeof value === "boolean"
59
- || (Array.isArray(value) && value.every(item => typeof item === "string"));
60
- }
61
56
  export function acpElicitation(value) {
62
57
  acpInput(value);
63
58
  requireThat(object(value) && (value.mode === "form" || value.mode === "url") && typeof value.message === "string", "ACP 交互请求无效。");
@@ -71,10 +66,10 @@ export function acpElicitation(value) {
71
66
  }
72
67
  export function acpElicitationReply(value) {
73
68
  acpInput(value);
74
- requireThat(object(value) && ["accept", "decline", "cancel"].includes(String(value.action)), "ACP 交互答复无效。");
69
+ requireThat(object(value) && isOneOf(value.action, acpElicitationActions), "ACP 交互答复无效。");
75
70
  if (value.content !== undefined) {
76
71
  requireThat(value.action === "accept" && object(value.content) &&
77
- Object.values(value.content).every(elicitationValue), "ACP 表单答复 content 无效。");
72
+ Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
78
73
  }
79
74
  return value;
80
75
  }
@@ -2,9 +2,10 @@ import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { canonical, object } from "../common/json.js";
5
- import { requireThat } from "../common/validation.js";
5
+ import { isOneOf, requireThat } from "../common/validation.js";
6
6
  import { readJsonFile, writeJsonFile } from "./atomic-json.js";
7
7
  import { fileId, privateDirectory } from "./file-access.js";
8
+ import { agentStates, cycleSources, cycleStates, messageDeliveries } from "../model/agent.js";
8
9
  import { acpElicitation, acpPermission, acpReference } from "./acp-files.js";
9
10
  const text = (v) => typeof v === "string" && v.length > 0;
10
11
  const number = (v) => typeof v === "number" && Number.isFinite(v);
@@ -20,18 +21,18 @@ export function launch(v) {
20
21
  }
21
22
  export function message(v) {
22
23
  requireThat(object(v) && text(v.requestId) && text(v.generation) && Number.isSafeInteger(v.sequence) && typeof v.message === "string" &&
23
- ["steer", "followUp"].includes(String(v.delivery)) && Array.isArray(v.files) && v.files.every(text), "子会话消息无效。");
24
+ isOneOf(v.delivery, messageDeliveries) && Array.isArray(v.files) && v.files.every(text), "子会话消息无效。");
24
25
  return v;
25
26
  }
26
27
  export function cycle(v) {
27
28
  requireThat(object(v) && text(v.cycleId) && text(v.generation) && number(v.startedAt) &&
28
- ["running", "completed", "failed", "cancelled", "interrupted"].includes(String(v.state)) &&
29
+ isOneOf(v.state, cycleStates) &&
29
30
  (v.leaf === null || text(v.leaf)) && Array.isArray(v.requestIds) && v.requestIds.every(text) &&
30
- ["parent", "interactive", "mixed"].includes(String(v.source)) && (v.finishedAt === undefined || number(v.finishedAt)), "子会话周期无效。");
31
+ isOneOf(v.source, cycleSources) && (v.finishedAt === undefined || number(v.finishedAt)), "子会话周期无效。");
31
32
  return v;
32
33
  }
33
34
  export function status(v) {
34
- requireThat(object(v) && text(v.generation) && ["starting", "busy", "idle", "stopping", "stopped", "error"].includes(String(v.state)) &&
35
+ requireThat(object(v) && text(v.generation) && isOneOf(v.state, agentStates) &&
35
36
  typeof v.activity === "string" && number(v.updatedAt), "子会话状态无效。");
36
37
  if (v.cycle)
37
38
  cycle(v.cycle);
@@ -1,5 +1,5 @@
1
1
  /** The root must already exist. Never follow aliases inside internal storage. */
2
- export declare function internalPath(directory: string, relative: string): string;
2
+ export declare function internalPath(directory: string, relative: string, allowMissingParents?: boolean): string;
3
3
  export declare function syncDirectory(directory: string): void;
4
4
  /** Validators return plain data and must reject incomplete contracts. */
5
5
  export declare function readJsonFile<T>(directory: string, relative: string, validate: (value: unknown) => T): T;
@@ -9,3 +9,5 @@ export declare function readJsonFile<T>(directory: string, relative: string, val
9
9
  * createOnly publishes by link, so an existing destination is never overwritten.
10
10
  */
11
11
  export declare function writeJsonFile<T>(directory: string, relative: string, value: unknown, validate: (value: unknown) => T, createOnly?: boolean): boolean;
12
+ /** Serialize a read-modify-write across processes. An undefined replacement means no change. */
13
+ export declare function updateJsonFile<T>(directory: string, relative: string, validate: (value: unknown) => T, update: (current: T | undefined) => T | undefined): Promise<boolean>;
@@ -1,10 +1,11 @@
1
- import { constants, closeSync, fstatSync, fsyncSync, linkSync, lstatSync, openSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
1
+ import { constants, closeSync, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
+ import { setTimeout as delay } from "node:timers/promises";
3
4
  import { randomUUID } from "node:crypto";
4
5
  import { canonical } from "../common/json.js";
5
6
  import { requireThat } from "../common/validation.js";
6
7
  /** The root must already exist. Never follow aliases inside internal storage. */
7
- export function internalPath(directory, relative) {
8
+ export function internalPath(directory, relative, allowMissingParents = false) {
8
9
  const parts = relative.split("/");
9
10
  requireThat(parts.every(part => part && part !== "." && part !== ".." && !/[\\\x00-\x1f]/.test(part)), "内部状态路径无效。");
10
11
  const info = lstatSync(directory);
@@ -17,7 +18,7 @@ export function internalPath(directory, relative) {
17
18
  requireThat(!info.isSymbolicLink() && (index === parts.length - 1 ? info.isFile() : info.isDirectory()), "内部状态不能使用符号链接或特殊文件。");
18
19
  }
19
20
  catch (error) {
20
- if (error.code !== "ENOENT" || index !== parts.length - 1)
21
+ if (error.code !== "ENOENT" || (!allowMissingParents && index !== parts.length - 1))
21
22
  throw error;
22
23
  }
23
24
  }
@@ -99,3 +100,42 @@ export function writeJsonFile(directory, relative, value, validate, createOnly =
99
100
  }
100
101
  }
101
102
  }
103
+ /** Serialize a read-modify-write across processes. An undefined replacement means no change. */
104
+ export async function updateJsonFile(directory, relative, validate, update) {
105
+ const lock = internalPath(directory, relative) + ".lock";
106
+ const deadline = Date.now() + 3000;
107
+ for (;;) {
108
+ try {
109
+ mkdirSync(lock, { mode: 0o700 });
110
+ break;
111
+ }
112
+ catch (error) {
113
+ if (error.code !== "EEXIST")
114
+ throw error;
115
+ if (Date.now() >= deadline)
116
+ throw new Error(`写锁被占用:${lock}。若写入进程已退出,可手动移除遗留锁目录。`);
117
+ await delay(25);
118
+ }
119
+ }
120
+ try {
121
+ for (;;) {
122
+ let current;
123
+ try {
124
+ current = readJsonFile(directory, relative, validate);
125
+ }
126
+ catch (error) {
127
+ if (error.code !== "ENOENT")
128
+ throw error;
129
+ }
130
+ const next = update(current);
131
+ if (next === undefined)
132
+ return false;
133
+ if (writeJsonFile(directory, relative, next, validate, current === undefined))
134
+ return true;
135
+ // An external writer created the file first. Apply the edit to its value.
136
+ }
137
+ }
138
+ finally {
139
+ rmdirSync(lock);
140
+ }
141
+ }
@@ -31,6 +31,7 @@ export declare class HistoryFiles {
31
31
  controlSummary<T>(dimension: string, operation: () => T): T;
32
32
  /** Synchronous migration lock for explicit layout migration. */
33
33
  controlMigration<T>(operation: () => T): T;
34
+ migrationNotice(): string | undefined;
34
35
  /** Detect unmigrated top-level flat JSON files when running under monthly layout. */
35
36
  detectFlatFiles(): FlatHistoryInventory;
36
37
  listFlatRawFiles(): string[];
@@ -38,7 +39,7 @@ export declare class HistoryFiles {
38
39
  readFlatRawFile(name: string): RawHistory;
39
40
  readFlatSummaryFile(dimension: string, name: string): SummaryArtifact;
40
41
  /** Atomically move a file within .pbot/history/ without ever overwriting an existing destination. */
41
- moveHistoryFile(fromRelative: string, toRelative: string): "moved" | "conflict";
42
+ moveHistoryFile(fromRelative: string, toRelative: string, dryRun?: boolean): "moved" | "conflict";
42
43
  listRaw(filterHost?: HistoryHost, onlyFiles?: readonly string[]): RawHistory[];
43
44
  readRaw(id: string): RawHistory | undefined;
44
45
  saveRaw(raw: RawHistory): void;
@@ -2,10 +2,10 @@ import { spawnSync } from "node:child_process";
2
2
  import { linkSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmdirSync, unlinkSync } from "node:fs";
3
3
  import { basename, dirname, join, relative, resolve } from "node:path";
4
4
  import { isDeepStrictEqual } from "node:util";
5
- import { requireThat } from "../common/validation.js";
5
+ import { isHistoryIdentifier, requireThat } from "../common/validation.js";
6
6
  import { internalPath, readJsonFile, syncDirectory, writeJsonFile } from "./atomic-json.js";
7
7
  import { fileId, privateDirectory } from "./file-access.js";
8
- import { isHistoryHost, isSummaryDimension, matchIdentity, rawHistory, summaryArtifact } from "./history-format.js";
8
+ import { matchIdentity, rawHistory, summaryArtifact } from "./history-format.js";
9
9
  const yearPattern = /^\d{4}$/;
10
10
  const monthPattern = /^(?:0[1-9]|1[0-2])$/;
11
11
  const monthlyIdPattern = /^(\d{4})-(0[1-9]|1[0-2])$/;
@@ -233,14 +233,14 @@ export class HistoryFiles {
233
233
  this.directory = directory;
234
234
  this.host = host;
235
235
  this.layout = layout;
236
- requireThat(isHistoryHost(host), "历史来源无效。");
236
+ requireThat(isHistoryIdentifier(host), "历史来源无效。");
237
237
  }
238
238
  get warnings() { return this.#warnings; }
239
239
  get rawWarnings() { return this.#rawWarnings; }
240
240
  get rawDirectory() { return join(this.directory, "raw"); }
241
241
  get summariesDirectory() { return join(this.directory, "summaries"); }
242
242
  summaryDirectory(dimension) {
243
- requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
243
+ requireThat(isHistoryIdentifier(dimension), "历史摘要维度无效:" + dimension);
244
244
  return join(this.summariesDirectory, dimension);
245
245
  }
246
246
  withLock(lockName, label, operation) {
@@ -268,13 +268,16 @@ export class HistoryFiles {
268
268
  }
269
269
  /** Synchronous dimension write lock for derived summary files. */
270
270
  controlSummary(dimension, operation) {
271
- requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
271
+ requireThat(isHistoryIdentifier(dimension), "历史摘要维度无效:" + dimension);
272
272
  return this.withLock(".write-summary-" + dimension, "summary:" + dimension, operation);
273
273
  }
274
274
  /** Synchronous migration lock for explicit layout migration. */
275
275
  controlMigration(operation) {
276
276
  return this.withLock(".write-migrate", "migrate", operation);
277
277
  }
278
+ migrationNotice() {
279
+ return this.layout === "monthly" ? this.detectFlatFiles().message : undefined;
280
+ }
278
281
  /** Detect unmigrated top-level flat JSON files when running under monthly layout. */
279
282
  detectFlatFiles() {
280
283
  if (!sharedDirectory(this.directory))
@@ -294,7 +297,7 @@ export class HistoryFiles {
294
297
  summaryCount,
295
298
  total,
296
299
  message: `检测到 ${total} 个未迁移的平铺历史文件(raw ${rawCount},summaries ${summaryCount});当前为年月目录布局(history.layout: monthly),未迁移文件不会被读取。` +
297
- `请运行 /pbot history migrate 整理到 YYYY/MM/ 目录,或在 .pbot/config.json 中设置 "history": { "layout": "flat" } 切回平铺布局。`,
300
+ `请在终端运行 pbot history migrate 整理到 YYYY/MM/ 目录(Pi/dsh 可用 /pbot history migrate),或在 .pbot/config.json 中设置 "history": { "layout": "flat" } 切回平铺布局。`,
298
301
  };
299
302
  }
300
303
  listFlatRawFiles() {
@@ -303,7 +306,7 @@ export class HistoryFiles {
303
306
  return topLevelJsonNames(this.rawDirectory);
304
307
  }
305
308
  listFlatSummaryFiles(dimension) {
306
- requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
309
+ requireThat(isHistoryIdentifier(dimension), "历史摘要维度无效:" + dimension);
307
310
  const dir = this.summaryDirectory(dimension);
308
311
  if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.summariesDirectory) || !ordinaryDirectory(dir))
309
312
  return [];
@@ -313,13 +316,16 @@ export class HistoryFiles {
313
316
  return readJsonFile(this.directory, "raw/" + name, value => namedRaw(value, name));
314
317
  }
315
318
  readFlatSummaryFile(dimension, name) {
316
- requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
319
+ requireThat(isHistoryIdentifier(dimension), "历史摘要维度无效:" + dimension);
317
320
  return readJsonFile(this.directory, `summaries/${dimension}/${name}`, value => namedSummary(value, name, dimension));
318
321
  }
319
322
  /** Atomically move a file within .pbot/history/ without ever overwriting an existing destination. */
320
- moveHistoryFile(fromRelative, toRelative) {
321
- ensureDirectory(this.directory);
323
+ moveHistoryFile(fromRelative, toRelative, dryRun = false) {
322
324
  const src = internalPath(this.directory, fromRelative);
325
+ if (dryRun) {
326
+ const target = internalPath(this.directory, toRelative, true);
327
+ return ordinaryFile(target) ? "conflict" : "moved";
328
+ }
323
329
  const dstParentRel = dirname(toRelative);
324
330
  if (dstParentRel && dstParentRel !== ".") {
325
331
  privateDirectory(this.directory, dstParentRel);
@@ -333,8 +339,8 @@ export class HistoryFiles {
333
339
  return "conflict";
334
340
  throw error;
335
341
  }
336
- unlinkSync(src);
337
342
  syncDirectory(dirname(dst));
343
+ unlinkSync(src);
338
344
  syncDirectory(dirname(src));
339
345
  return "moved";
340
346
  }
@@ -373,7 +379,7 @@ export class HistoryFiles {
373
379
  return optional(() => readJsonFile(this.directory, "raw/" + rel, value => namedRaw(value, rel)));
374
380
  }
375
381
  saveRaw(raw) {
376
- requireThat(isHistoryHost(raw.host) && raw.host === this.host, "历史归档只允许所属来源写入。");
382
+ requireThat(isHistoryIdentifier(raw.host) && raw.host === this.host, "历史归档只允许所属来源写入。");
377
383
  rawHistory(raw);
378
384
  ensureDirectory(this.directory);
379
385
  const name = filename(raw.id);
@@ -398,7 +404,7 @@ export class HistoryFiles {
398
404
  return [];
399
405
  const entries = readdirSync(this.summariesDirectory, { withFileTypes: true });
400
406
  return entries
401
- .filter(entry => entry.isDirectory() && !entry.isSymbolicLink() && isSummaryDimension(entry.name))
407
+ .filter(entry => entry.isDirectory() && !entry.isSymbolicLink() && isHistoryIdentifier(entry.name))
402
408
  .map(entry => entry.name)
403
409
  .sort();
404
410
  }
@@ -406,7 +412,7 @@ export class HistoryFiles {
406
412
  return dimension === "monthly" ? "year" : "month";
407
413
  }
408
414
  listSummaries(dimension, onlyFiles) {
409
- requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
415
+ requireThat(isHistoryIdentifier(dimension), "历史摘要维度无效:" + dimension);
410
416
  const dir = this.summaryDirectory(dimension);
411
417
  if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.summariesDirectory) || !ordinaryDirectory(dir))
412
418
  return [];
@@ -426,7 +432,7 @@ export class HistoryFiles {
426
432
  return results;
427
433
  }
428
434
  readSummary(dimension, id) {
429
- requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
435
+ requireThat(isHistoryIdentifier(dimension), "历史摘要维度无效:" + dimension);
430
436
  const dir = this.summaryDirectory(dimension);
431
437
  if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.summariesDirectory) || !ordinaryDirectory(dir))
432
438
  return undefined;
@@ -1,6 +1,5 @@
1
- import type { HistoryHost, HistoryIdentity, RawHistory, SummaryArtifact } from "../model/history.js";
2
- export declare const isHistoryHost: (value: unknown) => value is HistoryHost;
3
- export declare const isSummaryDimension: (value: unknown) => value is string;
1
+ import type { HistoryIdentity, RawHistory, SummaryArtifact } from "../model/history.js";
2
+ export { isHistoryIdentifier as isHistoryHost, isHistoryIdentifier as isSummaryDimension } from "../common/validation.js";
4
3
  export declare function matchIdentity(value: HistoryIdentity, expected: HistoryIdentity): void;
5
4
  export declare function rawHistory(value: unknown): RawHistory;
6
5
  export declare function summaryArtifact(value: unknown, expectedDimension?: string): SummaryArtifact;
@@ -1,5 +1,5 @@
1
1
  import { object } from "../common/json.js";
2
- import { requireThat } from "../common/validation.js";
2
+ import { isHistoryIdentifier, requireThat } from "../common/validation.js";
3
3
  import { fileId } from "./file-access.js";
4
4
  const strings = (value) => Array.isArray(value) && value.every(item => typeof item === "string");
5
5
  const fields = (value, names) => Object.keys(value).every(key => names.includes(key));
@@ -7,11 +7,9 @@ const rawFields = ["host", "id", "sessionId", "name", "exchanges", "branches"];
7
7
  const exchangeFields = ["id", "entryIds", "text", "from", "to", "completed"];
8
8
  const summaryArtifactFields = ["id", "dimension", "host", "sessionId", "title", "text", "from", "to", "updatedAt", "source"];
9
9
  const summarySourceFields = ["rawIds", "exchangeIds", "fingerprint"];
10
- const identifierPattern = /^[a-z0-9][a-z0-9-]{0,63}$/;
11
- export const isHistoryHost = (value) => typeof value === "string" && identifierPattern.test(value);
12
- export const isSummaryDimension = (value) => typeof value === "string" && identifierPattern.test(value);
10
+ export { isHistoryIdentifier as isHistoryHost, isHistoryIdentifier as isSummaryDimension } from "../common/validation.js";
13
11
  function identity(value) {
14
- requireThat(object(value) && isHistoryHost(value.host) && typeof value.id === "string" &&
12
+ requireThat(object(value) && isHistoryIdentifier(value.host) && typeof value.id === "string" &&
15
13
  typeof value.sessionId === "string" && value.sessionId.length > 0, "历史归档身份无效。");
16
14
  fileId(value.id);
17
15
  }
@@ -43,12 +41,12 @@ export function summaryArtifact(value, expectedDimension) {
43
41
  requireThat(object(value) && fields(value, summaryArtifactFields), "历史摘要产物格式无效。");
44
42
  requireThat(typeof value.id === "string" && value.id.length > 0, "历史摘要 ID 无效。");
45
43
  fileId(value.id);
46
- requireThat(isSummaryDimension(value.dimension), "历史摘要维度无效。");
44
+ requireThat(isHistoryIdentifier(value.dimension), "历史摘要维度无效。");
47
45
  if (expectedDimension !== undefined) {
48
46
  requireThat(value.dimension === expectedDimension, `历史摘要维度不匹配,预期 ${expectedDimension},实际 ${value.dimension}。`);
49
47
  }
50
48
  if (value.host !== undefined)
51
- requireThat(isHistoryHost(value.host), "历史摘要来源 host 无效。");
49
+ requireThat(isHistoryIdentifier(value.host), "历史摘要来源 host 无效。");
52
50
  if (value.sessionId !== undefined)
53
51
  requireThat(typeof value.sessionId === "string" && value.sessionId.length > 0, "历史摘要 sessionId 无效。");
54
52
  requireThat(typeof value.title === "string" && typeof value.text === "string" && value.text.trim().length > 0 &&
@@ -1,5 +1,5 @@
1
1
  import { Type } from "typebox";
2
- import type { HistoryHost } from "../../model/history.js";
2
+ import { type HistoryHost, type HistoryQuery } from "../../model/history.js";
3
3
  export declare const historyResultSchema: Type.TObject<{
4
4
  matches: Type.TOptional<Type.TArray<Type.TObject<{
5
5
  snippet: Type.TString;
@@ -84,17 +84,9 @@ export declare const historyParameters: Type.TObject<{
84
84
  limit: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TNull]>>;
85
85
  childrenOffset: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TNull]>>;
86
86
  }>;
87
- export type HistoryInput = {
87
+ export type HistoryInput = ({
88
88
  action: "search";
89
- host?: HistoryHost;
90
- dimension?: string;
91
- query?: string;
92
- sessionId?: string;
93
- from?: string;
94
- to?: string;
95
- offset?: number;
96
- limit?: number;
97
- } | {
89
+ } & HistoryQuery) | {
98
90
  action: "read";
99
91
  id: string;
100
92
  host?: HistoryHost;
@@ -1,8 +1,9 @@
1
1
  import { Type } from "../../common/typebox.js";
2
- import { requireThat } from "../../common/validation.js";
2
+ import { historyPageLimits } from "../../model/history.js";
3
+ import { historyIdentifierPattern, requireThat } from "../../common/validation.js";
3
4
  import { schema } from "../../common/schema.js";
4
5
  import { inputObject, need, nullable, pick } from "../input.js";
5
- const identifier = Type.String({ pattern: "^[a-z0-9][a-z0-9-]{0,63}$" });
6
+ const identifier = Type.String({ pattern: historyIdentifierPattern.source });
6
7
  const nullableOffset = Type.Union([Type.Integer(), Type.Null()]);
7
8
  const node = Type.Object({
8
9
  host: identifier,
@@ -52,23 +53,23 @@ export const historyParameters = Type.Object({
52
53
  action: Type.Union([Type.Literal("search"), Type.Literal("read")], {
53
54
  description: "search 搜索或列会话;read 读取指定节点正文与上下级。各动作仅读取自身字段,其余字段忽略。",
54
55
  }),
55
- host: nullable(Type.String({ pattern: "^[a-z0-9][a-z0-9-]{0,63}$", description: "可选来源筛选(如 pi、dsh、claude、antigravity)。" })),
56
- dimension: nullable(Type.String({ pattern: "^[a-z0-9][a-z0-9-]{0,63}$", description: "仅 search:可选范围筛选(raw 表示仅原文,或指定摘要维度如 monthly、session;省略查全部)。" })),
56
+ host: nullable(Type.String({ pattern: historyIdentifierPattern.source, description: "可选来源筛选(如 pi、dsh、claude、antigravity)。" })),
57
+ dimension: nullable(Type.String({ pattern: historyIdentifierPattern.source, description: "仅 search:可选范围筛选(raw 表示仅原文,或指定摘要维度如 monthly、session;省略查全部)。" })),
57
58
  query: nullable(Type.String({ description: "仅 search:不区分大小写的连续关键词;省略时列出各会话(或指定维度摘要)。" })),
58
59
  sessionId: nullable(Type.String({ description: "仅 search:限定在指定会话 ID 内搜索。" })),
59
60
  from: nullable(Type.String({ description: "仅 search:ISO 日期或时间下界。" })),
60
61
  to: nullable(Type.String({ description: "仅 search:ISO 日期或时间上界。" })),
61
62
  id: nullable(Type.String({ description: "仅 read,必填:search 或上下级返回的节点 id。" })),
62
63
  offset: nullable(Type.Integer({ minimum: 0, description: "search:匹配结果偏移;read:正文字符偏移。默认 0。" })),
63
- limit: nullable(Type.Integer({ minimum: 1, maximum: 24000, description: "search:最多返回条数(默认 20,上限 50);read:正文字符数(默认 12000)。" })),
64
+ limit: nullable(Type.Integer({ minimum: 1, maximum: historyPageLimits.read.max, description: `search:最多返回条数(默认 ${historyPageLimits.search.default},上限 ${historyPageLimits.search.max});read:正文字符数(默认 ${historyPageLimits.read.default})。` })),
64
65
  childrenOffset: nullable(Type.Integer({ minimum: 0, description: "仅 read:子节点列表偏移,默认 0;续读传上页 nextChildrenOffset。" })),
65
66
  }, { additionalProperties: false });
66
67
  const valid = schema(historyParameters);
67
68
  export function historyInput(value) {
68
69
  const args = inputObject(value, valid, "历史参数格式无效");
69
70
  if (args.action === "search") {
70
- const limit = args.limit === undefined ? 20 : args.limit;
71
- requireThat(limit <= 50, "search 的 limit 最多 50。");
71
+ const limit = args.limit === undefined ? historyPageLimits.search.default : args.limit;
72
+ requireThat(limit <= historyPageLimits.search.max, `search 的 limit 最多 ${historyPageLimits.search.max}。`);
72
73
  return {
73
74
  action: "search",
74
75
  ...pick(args, ["host", "dimension", "query", "sessionId", "from", "to", "offset", "limit"]),
@@ -1,17 +1,14 @@
1
1
  export declare const record: (value: unknown) => Record<string, unknown>;
2
2
  export declare const rows: (value: unknown) => Record<string, unknown>[];
3
3
  export declare const text: (value: unknown) => string;
4
- export declare const objectInput: (value: string) => Record<string, unknown>;
5
- export declare const lines: (value: string) => string[];
6
4
  export declare function element<K extends keyof HTMLElementTagNameMap>(tag: K, content?: string, className?: string): HTMLElementTagNameMap[K];
7
5
  export declare function block(value: unknown): HTMLPreElement;
8
6
  export declare function note(value: string): HTMLParagraphElement;
9
7
  export declare function group(parent: HTMLElement, className?: string): HTMLDivElement;
10
8
  export declare function details(parent: HTMLElement, title: string): HTMLDetailsElement;
11
- export declare function field(parent: HTMLElement, label: string, multiline?: boolean, value?: string): HTMLInputElement | HTMLTextAreaElement;
9
+ export declare function field(parent: HTMLElement, label: string): HTMLInputElement;
12
10
  export declare function select(parent: HTMLElement, label: string, values: [string, string][]): HTMLSelectElement;
13
- export declare function badge(state: unknown): HTMLSpanElement;
14
11
  export declare function facts(values: [string, unknown][]): HTMLDListElement;
15
12
  export declare function empty(title: string, description: string): HTMLDivElement;
16
- export declare function pageTitle(parent: HTMLElement, title: string, description: string): HTMLElement;
13
+ export declare function pageTitle(parent: HTMLElement, title: string): HTMLElement;
17
14
  export declare function time(value: unknown): string;