personal-bot 0.3.0 → 0.3.2

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 (256) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +1 -1
  4. package/README.zh-CN.md +1 -1
  5. package/bin/pbot-pi-acp.mjs +27 -11
  6. package/bin/pbot.mjs +1 -1
  7. package/docs/README.md +1 -1
  8. package/docs/architecture/design.md +23 -10
  9. package/docs/architecture/extensibility.md +20 -18
  10. package/docs/architecture/model-context.md +2 -2
  11. package/docs/architecture/principles.md +4 -3
  12. package/docs/guides/capabilities.md +3 -3
  13. package/docs/guides/history.md +50 -16
  14. package/docs/guides/workspace-configuration.md +31 -6
  15. package/docs/hosts/antigravity.md +2 -0
  16. package/docs/hosts/claude-code.md +1 -1
  17. package/docs/hosts/dsh.md +1 -1
  18. package/docs/hosts/pi.md +1 -1
  19. package/hooks/hooks.json +3 -0
  20. package/lib/antigravity-plugin/hooks.json +7 -0
  21. package/lib/antigravity-plugin/lib/claude.js +2 -2
  22. package/lib/antigravity-plugin/lib/extension/acp-worker.js +156 -11
  23. package/lib/antigravity-plugin/lib/extension/{plugin-runtime.d.ts → hosts/plugin-runtime.d.ts} +2 -2
  24. package/lib/antigravity-plugin/lib/extension/{plugin-runtime.js → hosts/plugin-runtime.js} +34 -8
  25. package/lib/antigravity-plugin/lib/extension/mcp.js +1113 -78
  26. package/lib/antigravity-plugin/lib/integrations/agent-resources.d.ts +1 -0
  27. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +5 -1
  28. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity/adapter.d.ts +6 -0
  29. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity/adapter.js +171 -0
  30. package/lib/antigravity-plugin/lib/integrations/hosts/{claude.d.ts → claude/adapter.d.ts} +1 -1
  31. package/lib/antigravity-plugin/lib/integrations/hosts/{claude.js → claude/adapter.js} +32 -4
  32. package/lib/antigravity-plugin/lib/integrations/hosts/{registry.js → plugin-registry.js} +2 -2
  33. package/lib/antigravity-plugin/lib/model/history.d.ts +169 -0
  34. package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +26 -0
  35. package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +25 -0
  36. package/lib/antigravity-plugin/lib/plugin.js +2 -2
  37. package/lib/antigravity-plugin/lib/services/history/history-capture.d.ts +4 -0
  38. package/lib/antigravity-plugin/lib/services/history/history-capture.js +86 -0
  39. package/lib/antigravity-plugin/lib/services/history/history-index.d.ts +17 -0
  40. package/lib/antigravity-plugin/lib/services/history/history-index.js +112 -0
  41. package/lib/antigravity-plugin/lib/services/history/history-migrate.d.ts +12 -0
  42. package/lib/antigravity-plugin/lib/services/history/history-migrate.js +88 -0
  43. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/monthly.d.ts +7 -0
  44. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/monthly.js +121 -0
  45. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/session.d.ts +7 -0
  46. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/session.js +124 -0
  47. package/lib/antigravity-plugin/lib/services/history/summary-registry.d.ts +7 -0
  48. package/lib/antigravity-plugin/lib/services/history/summary-registry.js +36 -0
  49. package/lib/antigravity-plugin/lib/services/history/summary-runner.d.ts +37 -0
  50. package/lib/antigravity-plugin/lib/services/history/summary-runner.js +301 -0
  51. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.d.ts +5 -1
  52. package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +75 -4
  53. package/lib/antigravity-plugin/lib/storage/file-access.d.ts +2 -0
  54. package/lib/antigravity-plugin/lib/storage/file-access.js +26 -0
  55. package/lib/antigravity-plugin/lib/storage/history-files.d.ts +54 -0
  56. package/lib/antigravity-plugin/lib/storage/history-files.js +467 -0
  57. package/lib/antigravity-plugin/lib/storage/history-format.d.ts +6 -0
  58. package/lib/antigravity-plugin/lib/storage/history-format.js +62 -0
  59. package/lib/antigravity-plugin/package.json +1 -1
  60. package/lib/antigravity-plugin/plugin.json +1 -1
  61. package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +22 -12
  62. package/lib/claude.js +2 -2
  63. package/lib/client.js +21 -11
  64. package/lib/dsh.js +4 -4
  65. package/lib/extension/{acp-member.d.ts → acp/member.d.ts} +5 -5
  66. package/lib/extension/{acp-member.js → acp/member.js} +6 -6
  67. package/lib/extension/acp/worker.d.ts +1 -0
  68. package/lib/extension/acp/worker.js +384 -0
  69. package/lib/extension/acp-worker.js +156 -11
  70. package/lib/extension/{dsh → hosts/dsh}/acp-member.js +8 -8
  71. package/lib/extension/hosts/dsh/command.js +92 -0
  72. package/lib/extension/{dsh → hosts/dsh}/files.js +3 -3
  73. package/lib/extension/hosts/dsh/history.d.ts +4 -0
  74. package/lib/extension/hosts/dsh/history.js +62 -0
  75. package/lib/extension/{dsh → hosts/dsh}/management.d.ts +1 -1
  76. package/lib/extension/{dsh → hosts/dsh}/management.js +12 -10
  77. package/lib/extension/{dsh → hosts/dsh}/workspace.d.ts +3 -9
  78. package/lib/extension/{dsh → hosts/dsh}/workspace.js +32 -70
  79. package/lib/extension/{plugin-runtime.d.ts → hosts/plugin-runtime.d.ts} +2 -2
  80. package/lib/extension/{plugin-runtime.js → hosts/plugin-runtime.js} +34 -8
  81. package/lib/extension/{mcp-bridge.d.ts → mcp/bridge.d.ts} +1 -1
  82. package/lib/extension/{mcp-bridge.js → mcp/bridge.js} +4 -4
  83. package/lib/extension/{mcp-workers.d.ts → mcp/workers.d.ts} +222 -2
  84. package/lib/extension/{mcp-workers.js → mcp/workers.js} +229 -63
  85. package/lib/extension/mcp.js +1113 -78
  86. package/lib/extension/{setup-cli.js → setup/cli.js} +9 -9
  87. package/lib/integrations/acp/connection.d.ts +11 -10
  88. package/lib/integrations/acp/connection.js +6 -0
  89. package/lib/integrations/acp/harnesses/claude.js +1 -1
  90. package/lib/integrations/acp/harnesses/dsh.js +1 -1
  91. package/lib/integrations/acp/history-summary.d.ts +17 -0
  92. package/lib/integrations/acp/history-summary.js +100 -0
  93. package/lib/integrations/agent-resources.d.ts +1 -0
  94. package/lib/integrations/agent-resources.js +5 -1
  95. package/lib/integrations/hosts/antigravity/adapter.d.ts +6 -0
  96. package/lib/integrations/hosts/antigravity/adapter.js +171 -0
  97. package/lib/integrations/hosts/{claude.d.ts → claude/adapter.d.ts} +1 -1
  98. package/lib/integrations/hosts/{claude.js → claude/adapter.js} +32 -4
  99. package/lib/integrations/{dsh → hosts/dsh}/mcp.d.ts +1 -1
  100. package/lib/integrations/{dsh → hosts/dsh}/resources.d.ts +1 -1
  101. package/lib/integrations/{dsh → hosts/dsh}/resources.js +4 -4
  102. package/lib/integrations/{dsh → hosts/dsh}/schema.js +1 -1
  103. package/lib/integrations/{dsh → hosts/dsh}/skills.js +1 -1
  104. package/lib/integrations/hosts/{registry.js → plugin-registry.js} +2 -2
  105. package/lib/integrations/installation/pi-setup.js +1 -1
  106. package/lib/model/history.d.ts +133 -34
  107. package/lib/model/plugin-host.d.ts +26 -0
  108. package/lib/model/workspace-config.d.ts +25 -0
  109. package/lib/plugin.js +2 -2
  110. package/lib/services/agents/agent-run.d.ts +1 -1
  111. package/lib/services/history/history-capture.d.ts +4 -0
  112. package/lib/services/history/history-capture.js +86 -0
  113. package/lib/services/history/history-index.d.ts +16 -7
  114. package/lib/services/history/history-index.js +48 -70
  115. package/lib/services/history/history-migrate.d.ts +12 -0
  116. package/lib/services/history/history-migrate.js +88 -0
  117. package/lib/services/history/history-query.d.ts +51 -0
  118. package/lib/services/history/history-query.js +371 -0
  119. package/lib/services/history/summary-command.d.ts +20 -0
  120. package/lib/services/history/summary-command.js +68 -0
  121. package/lib/services/history/summary-dimensions/monthly.d.ts +7 -0
  122. package/lib/services/history/summary-dimensions/monthly.js +121 -0
  123. package/lib/services/history/summary-dimensions/session.d.ts +7 -0
  124. package/lib/services/history/summary-dimensions/session.js +124 -0
  125. package/lib/services/history/summary-registry.d.ts +7 -0
  126. package/lib/services/history/summary-registry.js +36 -0
  127. package/lib/services/history/summary-runner.d.ts +37 -0
  128. package/lib/services/history/summary-runner.js +301 -0
  129. package/lib/services/workspace/workspace-config.d.ts +5 -1
  130. package/lib/services/workspace/workspace-config.js +75 -4
  131. package/lib/storage/history-files.d.ts +45 -9
  132. package/lib/storage/history-files.js +373 -247
  133. package/lib/storage/history-format.d.ts +3 -12
  134. package/lib/storage/history-format.js +26 -50
  135. package/lib/tools/history/actions.d.ts +3 -68
  136. package/lib/tools/history/actions.js +7 -5
  137. package/lib/tools/history/contract.d.ts +14 -7
  138. package/lib/tools/history/contract.js +58 -18
  139. package/lib/tools/history/dsh-tool.d.ts +1 -2
  140. package/lib/tools/history/dsh-tool.js +11 -5
  141. package/lib/ui/dsh/management.js +18 -5
  142. package/package.json +1 -1
  143. package/resources/prompts/history-coordinator.md +1 -1
  144. package/resources/prompts/history-monthly-summary.md +13 -0
  145. package/resources/prompts/history-summary.md +10 -4
  146. package/resources/skills/provider-connections/SKILL.md +22 -12
  147. package/src/claude.ts +2 -2
  148. package/src/dsh.ts +4 -4
  149. package/src/extension/{acp-member.ts → acp/member.ts} +7 -7
  150. package/src/extension/{acp-worker.ts → acp/worker.ts} +131 -17
  151. package/src/extension/{dsh → hosts/dsh}/acp-member.ts +9 -9
  152. package/src/extension/hosts/dsh/command.ts +92 -0
  153. package/src/extension/{dsh → hosts/dsh}/files.ts +3 -3
  154. package/src/extension/hosts/dsh/history.ts +67 -0
  155. package/src/extension/{dsh → hosts/dsh}/management.ts +11 -9
  156. package/src/extension/{dsh → hosts/dsh}/workspace.ts +31 -50
  157. package/src/extension/{pi-acp-member.ts → hosts/pi/acp-member.ts} +9 -9
  158. package/src/extension/hosts/pi/command.ts +158 -0
  159. package/src/extension/{events.ts → hosts/pi/events.ts} +14 -4
  160. package/src/extension/hosts/pi/history.ts +45 -0
  161. package/src/extension/{lifecycle.ts → hosts/pi/lifecycle.ts} +7 -11
  162. package/src/extension/{plugin-runtime.ts → hosts/plugin-runtime.ts} +29 -9
  163. package/src/extension/{mcp-bridge.ts → mcp/bridge.ts} +5 -5
  164. package/src/extension/{mcp-workers.ts → mcp/workers.ts} +237 -64
  165. package/src/extension/{setup-cli.ts → setup/cli.ts} +11 -11
  166. package/src/integrations/acp/connection.ts +6 -0
  167. package/src/integrations/acp/harnesses/claude.ts +1 -1
  168. package/src/integrations/acp/harnesses/dsh.ts +1 -1
  169. package/src/integrations/acp/history-summary.ts +108 -0
  170. package/src/integrations/agent-resources.ts +6 -0
  171. package/src/integrations/hosts/antigravity/adapter.ts +161 -0
  172. package/src/integrations/hosts/{claude.ts → claude/adapter.ts} +28 -5
  173. package/src/integrations/{dsh → hosts/dsh}/mcp.ts +1 -1
  174. package/src/integrations/{dsh → hosts/dsh}/resources.ts +5 -5
  175. package/src/integrations/{dsh → hosts/dsh}/schema.ts +1 -1
  176. package/src/integrations/{dsh → hosts/dsh}/session.ts +2 -2
  177. package/src/integrations/{dsh → hosts/dsh}/skills.ts +1 -1
  178. package/src/integrations/{pi → hosts/pi}/mcp.ts +1 -1
  179. package/src/integrations/{pi → hosts/pi}/skills.ts +1 -1
  180. package/src/integrations/hosts/{registry.ts → plugin-registry.ts} +2 -2
  181. package/src/integrations/installation/pi-setup.ts +1 -1
  182. package/src/mcp.ts +1 -1
  183. package/src/model/history.ts +151 -26
  184. package/src/model/plugin-host.ts +26 -2
  185. package/src/model/workspace-config.ts +30 -0
  186. package/src/pbot.ts +13 -22
  187. package/src/plugin.ts +2 -2
  188. package/src/services/history/history-capture.ts +83 -0
  189. package/src/services/history/history-index.ts +48 -62
  190. package/src/services/history/history-migrate.ts +89 -0
  191. package/src/services/history/history-query.ts +415 -0
  192. package/src/services/history/summary-command.ts +69 -0
  193. package/src/services/history/summary-dimensions/monthly.ts +139 -0
  194. package/src/services/history/summary-dimensions/session.ts +149 -0
  195. package/src/services/history/summary-registry.ts +44 -0
  196. package/src/services/history/summary-runner.ts +352 -0
  197. package/src/services/workspace/workspace-config.ts +93 -5
  198. package/src/storage/history-files.ts +408 -188
  199. package/src/storage/history-format.ts +46 -60
  200. package/src/tools/history/actions.ts +8 -6
  201. package/src/tools/history/contract.ts +65 -20
  202. package/src/tools/history/dsh-tool.ts +17 -8
  203. package/src/tools/history/pi-tool.ts +21 -0
  204. package/src/tools/register.ts +3 -4
  205. package/src/ui/dsh/management.ts +27 -11
  206. package/src/ui/{confirmation.ts → pi/confirmation.ts} +1 -1
  207. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.d.ts +0 -4
  208. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.js +0 -79
  209. package/lib/extension/dsh/command.js +0 -50
  210. package/lib/extension/dsh/history.d.ts +0 -15
  211. package/lib/extension/dsh/history.js +0 -87
  212. package/lib/integrations/dsh/history-summary.d.ts +0 -11
  213. package/lib/integrations/dsh/history-summary.js +0 -36
  214. package/lib/integrations/dsh/history.d.ts +0 -16
  215. package/lib/integrations/dsh/history.js +0 -167
  216. package/lib/integrations/history-summary.d.ts +0 -4
  217. package/lib/integrations/history-summary.js +0 -32
  218. package/lib/integrations/hosts/antigravity.d.ts +0 -4
  219. package/lib/integrations/hosts/antigravity.js +0 -79
  220. package/lib/services/history/history-service.d.ts +0 -106
  221. package/lib/services/history/history-service.js +0 -242
  222. package/lib/storage/acp-history.d.ts +0 -10
  223. package/lib/storage/acp-history.js +0 -57
  224. package/src/extension/command.ts +0 -76
  225. package/src/extension/dsh/command.ts +0 -54
  226. package/src/extension/dsh/history.ts +0 -74
  227. package/src/extension/history.ts +0 -26
  228. package/src/integrations/dsh/history-summary.ts +0 -39
  229. package/src/integrations/dsh/history.ts +0 -84
  230. package/src/integrations/history-summary.ts +0 -30
  231. package/src/integrations/hosts/antigravity.ts +0 -83
  232. package/src/integrations/pi/history.ts +0 -99
  233. package/src/services/history/history-service.ts +0 -195
  234. package/src/storage/acp-history.ts +0 -49
  235. package/src/tools/history/tool.ts +0 -19
  236. /package/lib/antigravity-plugin/lib/integrations/hosts/{registry.d.ts → plugin-registry.d.ts} +0 -0
  237. /package/lib/{extension/acp-worker.d.ts → antigravity-plugin/lib/model/history.js} +0 -0
  238. /package/lib/extension/{dsh → hosts/dsh}/acp-member.d.ts +0 -0
  239. /package/lib/extension/{dsh → hosts/dsh}/command.d.ts +0 -0
  240. /package/lib/extension/{dsh → hosts/dsh}/files.d.ts +0 -0
  241. /package/lib/extension/{setup-guard.js → setup/guard.js} +0 -0
  242. /package/lib/integrations/acp/{claude.d.ts → claude-settings.d.ts} +0 -0
  243. /package/lib/integrations/acp/{claude.js → claude-settings.js} +0 -0
  244. /package/lib/integrations/{dsh → hosts/dsh}/mcp.js +0 -0
  245. /package/lib/integrations/{dsh → hosts/dsh}/messages.d.ts +0 -0
  246. /package/lib/integrations/{dsh → hosts/dsh}/messages.js +0 -0
  247. /package/lib/integrations/{dsh → hosts/dsh}/root-agents.d.ts +0 -0
  248. /package/lib/integrations/{dsh → hosts/dsh}/root-agents.js +0 -0
  249. /package/lib/integrations/{dsh → hosts/dsh}/schema.d.ts +0 -0
  250. /package/lib/integrations/{dsh → hosts/dsh}/skills.d.ts +0 -0
  251. /package/lib/integrations/hosts/{registry.d.ts → plugin-registry.d.ts} +0 -0
  252. /package/src/extension/{setup-guard.ts → setup/guard.ts} +0 -0
  253. /package/src/integrations/acp/{claude.ts → claude-settings.ts} +0 -0
  254. /package/src/integrations/{dsh → hosts/dsh}/messages.ts +0 -0
  255. /package/src/integrations/{dsh → hosts/dsh}/root-agents.ts +0 -0
  256. /package/src/integrations/{pi → hosts/pi}/agent-tools.ts +0 -0
@@ -0,0 +1,112 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { requireThat } from "../../common/validation.js";
3
+ /** Each native entry has one predecessor. A shortened observation can extend a known path, not erase its prefix. */
4
+ function mergeBranches(previous, observed, removed) {
5
+ const parents = new Map();
6
+ for (const branch of [...previous, ...observed]) {
7
+ let parent;
8
+ for (const id of branch) {
9
+ if (removed.has(id)) {
10
+ parent = undefined;
11
+ continue;
12
+ }
13
+ if (parent !== undefined || !parents.has(id))
14
+ parents.set(id, parent);
15
+ parent = id;
16
+ }
17
+ }
18
+ const internal = new Set(parents.values()), seen = new Set(), branches = [];
19
+ for (const tip of parents.keys()) {
20
+ if (internal.has(tip))
21
+ continue;
22
+ const path = [];
23
+ for (let id = tip; id !== undefined; id = parents.get(id)) {
24
+ requireThat(path.length < parents.size, "历史分支包含循环引用。");
25
+ path.push(id);
26
+ seen.add(id);
27
+ }
28
+ branches.push(path.reverse());
29
+ }
30
+ requireThat(seen.size === parents.size, "历史分支包含循环引用。");
31
+ return branches;
32
+ }
33
+ /** Merge a native session observation into its raw archive without modifying persisted order invariants. */
34
+ export function indexHistory(session, previous, host = previous?.host ?? "pi") {
35
+ requireThat(!previous || (previous.host === host && previous.sessionId === session.sessionId), "历史会话与归档来源不匹配。");
36
+ const id = previous?.id ?? (host === "pi" ? "" : host + "-") + randomUUID();
37
+ const exchanges = new Map(previous?.exchanges.map(item => [item.id, item]));
38
+ const byEnd = new Map(previous?.exchanges.map(item => [item.entryIds.at(-1), item.id]));
39
+ const observed = new Map();
40
+ const branches = session.branches.map(branch => branch.map(value => {
41
+ const end = value.entryIds.at(-1);
42
+ const key = byEnd.get(end) ?? `${id}:0:${end}`;
43
+ if (!observed.has(end)) {
44
+ observed.set(end, value);
45
+ const before = exchanges.get(key);
46
+ // Compaction can leave only the last inputs and answer of an already archived exchange.
47
+ if (!(before && value.entryIds.length < before.entryIds.length &&
48
+ value.entryIds.every((entry, i) => entry === before.entryIds[before.entryIds.length - value.entryIds.length + i]))) {
49
+ exchanges.set(key, { ...value, entryIds: [...value.entryIds], id: key });
50
+ }
51
+ }
52
+ return key;
53
+ }));
54
+ const removed = new Set();
55
+ for (const end of session.supersededEntryIds ?? []) {
56
+ const key = byEnd.get(end), item = key === undefined ? undefined : exchanges.get(key);
57
+ if (item && !item.completed && !observed.has(end) && [...observed.values()].some(value => item.entryIds.slice(0, -1).every(entry => value.entryIds.includes(entry)))) {
58
+ removed.add(item.id);
59
+ exchanges.delete(item.id);
60
+ }
61
+ }
62
+ return {
63
+ host,
64
+ id,
65
+ sessionId: session.sessionId,
66
+ name: session.name,
67
+ exchanges: [...exchanges.values()],
68
+ branches: mergeBranches(previous?.branches ?? [], branches, removed),
69
+ };
70
+ }
71
+ /** Return deduplicated exchanges in conversation order (branch order first, then any unbranched entries by timestamp). */
72
+ export function orderedRawExchanges(raw) {
73
+ const lookup = new Map(raw.exchanges.map(item => [item.id, item]));
74
+ const seen = new Set();
75
+ const ordered = [];
76
+ for (const branch of raw.branches) {
77
+ for (const id of branch) {
78
+ if (seen.has(id))
79
+ continue;
80
+ const item = lookup.get(id);
81
+ if (!item)
82
+ continue;
83
+ seen.add(id);
84
+ ordered.push(item);
85
+ }
86
+ }
87
+ const rest = raw.exchanges
88
+ .filter(item => !seen.has(item.id))
89
+ .sort((a, b) => a.from.localeCompare(b.from) || a.id.localeCompare(b.id));
90
+ ordered.push(...rest);
91
+ return ordered;
92
+ }
93
+ export function rawSessionTitle(raw) {
94
+ return raw.name.trim();
95
+ }
96
+ export function rawSessionSpan(exchanges) {
97
+ const dates = exchanges.flatMap(item => [item.from, item.to]).filter(Boolean).sort();
98
+ return {
99
+ from: dates[0] ?? "",
100
+ to: dates.at(-1) ?? "",
101
+ completed: exchanges.every(item => item.completed),
102
+ };
103
+ }
104
+ /** Stable SHA-256 fingerprint over ordered exchange contents for exact deduplication and prefix matching. */
105
+ export function fingerprintExchanges(exchanges) {
106
+ const hash = createHash("sha256");
107
+ for (const item of exchanges) {
108
+ hash.update(JSON.stringify([item.id, item.entryIds, item.text, item.from, item.to, item.completed]));
109
+ hash.update("\n");
110
+ }
111
+ return hash.digest("hex");
112
+ }
@@ -0,0 +1,12 @@
1
+ import type { HistoryMigrationReport } from "../../model/history.js";
2
+ export interface MigrateHistoryOptions {
3
+ dryRun?: boolean;
4
+ }
5
+ /** Read-only check for unmigrated flat history files when running under monthly layout. */
6
+ export declare function checkHistoryMigrationNotice(root: string): string | undefined;
7
+ /**
8
+ * Explicitly migrate flat .pbot/history/raw/*.json and .pbot/history/summaries/<dim>/*.json
9
+ * into UTC YYYY/MM/ (or unknown/) buckets. Moves files directly without keeping duplicates
10
+ * and never modifies .pbot/config.json or overwrites existing destinations.
11
+ */
12
+ export declare function migrateWorkspaceHistory(root: string, options?: MigrateHistoryOptions): HistoryMigrationReport;
@@ -0,0 +1,88 @@
1
+ import { lstatSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { requireThat } from "../../common/validation.js";
4
+ import { HistoryFiles, rawHistoryBucket, summaryArtifactBucket } from "../../storage/history-files.js";
5
+ import { readWorkspaceHistoryLayoutSync } from "../workspace/workspace-config.js";
6
+ /** Read-only check for unmigrated flat history files when running under monthly layout. */
7
+ export function checkHistoryMigrationNotice(root) {
8
+ const layout = readWorkspaceHistoryLayoutSync(root);
9
+ if (layout !== "monthly")
10
+ return undefined;
11
+ const files = new HistoryFiles(join(root, ".pbot/history"), "pi", layout);
12
+ return files.detectFlatFiles().message;
13
+ }
14
+ /**
15
+ * Explicitly migrate flat .pbot/history/raw/*.json and .pbot/history/summaries/<dim>/*.json
16
+ * into UTC YYYY/MM/ (or unknown/) buckets. Moves files directly without keeping duplicates
17
+ * and never modifies .pbot/config.json or overwrites existing destinations.
18
+ */
19
+ export function migrateWorkspaceHistory(root, options = {}) {
20
+ const layout = readWorkspaceHistoryLayoutSync(root);
21
+ requireThat(layout === "monthly", '当前工作区配置为平铺布局(history.layout: "flat"),无需迁移到年月目录;如需迁移,请先在 .pbot/config.json 中将 history.layout 设为 "monthly" 或移除该配置项。');
22
+ const dryRun = Boolean(options.dryRun);
23
+ const files = new HistoryFiles(join(root, ".pbot/history"), "pi", "monthly");
24
+ const moved = [];
25
+ const skipped = [];
26
+ const errors = [];
27
+ const run = () => {
28
+ for (const name of files.listFlatRawFiles()) {
29
+ const from = `raw/${name}`;
30
+ try {
31
+ const raw = files.readFlatRawFile(name);
32
+ const bucket = rawHistoryBucket(raw);
33
+ const to = `raw/${bucket}/${name}`;
34
+ if (dryRun) {
35
+ if (lstatSync(join(files.directory, to), { throwIfNoEntry: false })) {
36
+ skipped.push({ kind: "raw", from, to, reason: "目标路径已存在同名文件。" });
37
+ }
38
+ else {
39
+ moved.push({ kind: "raw", id: raw.id, from, to });
40
+ }
41
+ }
42
+ else {
43
+ const outcome = files.moveHistoryFile(from, to);
44
+ if (outcome === "moved")
45
+ moved.push({ kind: "raw", id: raw.id, from, to });
46
+ else
47
+ skipped.push({ kind: "raw", from, to, reason: "目标路径已存在同名文件。" });
48
+ }
49
+ }
50
+ catch (error) {
51
+ errors.push({ path: join(files.directory, from), error: error instanceof Error ? error.message : String(error) });
52
+ }
53
+ }
54
+ for (const dimension of files.listDimensions()) {
55
+ for (const name of files.listFlatSummaryFiles(dimension)) {
56
+ const from = `summaries/${dimension}/${name}`;
57
+ try {
58
+ const artifact = files.readFlatSummaryFile(dimension, name);
59
+ const bucket = summaryArtifactBucket(artifact);
60
+ const to = `summaries/${dimension}/${bucket}/${name}`;
61
+ if (dryRun) {
62
+ if (lstatSync(join(files.directory, to), { throwIfNoEntry: false })) {
63
+ skipped.push({ kind: "summary", dimension, from, to, reason: "目标路径已存在同名文件。" });
64
+ }
65
+ else {
66
+ moved.push({ kind: "summary", dimension, id: artifact.id, from, to });
67
+ }
68
+ }
69
+ else {
70
+ const outcome = files.moveHistoryFile(from, to);
71
+ if (outcome === "moved")
72
+ moved.push({ kind: "summary", dimension, id: artifact.id, from, to });
73
+ else
74
+ skipped.push({ kind: "summary", dimension, from, to, reason: "目标路径已存在同名文件。" });
75
+ }
76
+ }
77
+ catch (error) {
78
+ errors.push({ path: join(files.directory, from), error: error instanceof Error ? error.message : String(error) });
79
+ }
80
+ }
81
+ }
82
+ };
83
+ if (dryRun)
84
+ run();
85
+ else
86
+ files.controlMigration(run);
87
+ return { layout, dryRun, moved, skipped, errors };
88
+ }
@@ -0,0 +1,7 @@
1
+ import type { SummaryDimension } from "../../../model/history.js";
2
+ /**
3
+ * Monthly cross-session summary dimension (enabled only under history.layout: "monthly").
4
+ * Aggregates per-session summaries grouped by UTC YYYY/MM into one monthly artifact per month,
5
+ * preserving source.rawIds for pbot_history drill-down to session summaries and raw exchanges.
6
+ */
7
+ export declare const monthlySummaryDimension: SummaryDimension;
@@ -0,0 +1,121 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { resourceRoot } from "../../../common/paths.js";
5
+ import { requireThat } from "../../../common/validation.js";
6
+ import { historyBucketFromTimestamp, unknownHistoryBucket } from "../../../storage/history-files.js";
7
+ let cachedPrompt;
8
+ function systemPrompt() {
9
+ cachedPrompt ??= readFileSync(join(resourceRoot, "prompts/history-monthly-summary.md"), "utf8");
10
+ return cachedPrompt;
11
+ }
12
+ function fingerprintSessionSummaries(summaries) {
13
+ const hash = createHash("sha256");
14
+ for (const item of summaries) {
15
+ hash.update(JSON.stringify([item.id, item.title, item.from, item.to, item.source.fingerprint, item.text]));
16
+ hash.update("\n");
17
+ }
18
+ return hash.digest("hex");
19
+ }
20
+ function monthSpan(summaries) {
21
+ const dates = summaries.flatMap(item => [item.from, item.to]).filter(Boolean).sort();
22
+ return {
23
+ from: dates[0] ?? "",
24
+ to: dates.at(-1) ?? "",
25
+ };
26
+ }
27
+ function formatSessionSummaries(summaries) {
28
+ return summaries
29
+ .map(item => {
30
+ const header = [`会话 ID:${item.id}`];
31
+ if (item.title)
32
+ header.push(`标题:${item.title}`);
33
+ if (item.host)
34
+ header.push(`来源:${item.host}`);
35
+ header.push(`时间:${item.from} — ${item.to}`);
36
+ return `[${header.join(" | ")}]\n${item.text}`;
37
+ })
38
+ .join("\n\n");
39
+ }
40
+ /**
41
+ * Monthly cross-session summary dimension (enabled only under history.layout: "monthly").
42
+ * Aggregates per-session summaries grouped by UTC YYYY/MM into one monthly artifact per month,
43
+ * preserving source.rawIds for pbot_history drill-down to session summaries and raw exchanges.
44
+ */
45
+ export const monthlySummaryDimension = {
46
+ id: "monthly",
47
+ title: "月度历史汇总",
48
+ description: "按自然月(UTC YYYY/MM)归纳当月单会话摘要,提炼月度主题、关键决定与产出,支持逐层下钻。",
49
+ requiresMonthlyLayout: true,
50
+ plan(_raws, existing, options = {}) {
51
+ const force = Boolean(options.force);
52
+ const sessionMap = options.summariesByDimension?.get("session");
53
+ if (!sessionMap || sessionMap.size === 0)
54
+ return { tasks: [], skipped: 0 };
55
+ const byMonth = new Map();
56
+ for (const artifact of sessionMap.values()) {
57
+ const bucket = historyBucketFromTimestamp(artifact.from || artifact.to);
58
+ if (bucket === unknownHistoryBucket)
59
+ continue;
60
+ const monthId = bucket.replace("/", "-");
61
+ const list = byMonth.get(monthId);
62
+ if (list)
63
+ list.push(artifact);
64
+ else
65
+ byMonth.set(monthId, [artifact]);
66
+ }
67
+ const tasks = [];
68
+ let skipped = 0;
69
+ for (const [monthId, summaries] of byMonth) {
70
+ const ordered = [...summaries].sort((a, b) => a.from.localeCompare(b.from) || a.id.localeCompare(b.id));
71
+ const fingerprint = fingerprintSessionSummaries(ordered);
72
+ const prev = existing.get(monthId);
73
+ if (!force && prev && prev.source.fingerprint === fingerprint) {
74
+ skipped++;
75
+ continue;
76
+ }
77
+ const rawIds = [...new Set(ordered.flatMap(item => item.source.rawIds))];
78
+ if (!rawIds.length)
79
+ continue;
80
+ const span = monthSpan(ordered);
81
+ const [year, month] = monthId.split("-");
82
+ tasks.push({
83
+ id: monthId,
84
+ dimension: this.id,
85
+ mode: prev ? "rebuild" : "create",
86
+ title: `${year}年${month}月历史汇总`,
87
+ from: span.from,
88
+ to: span.to,
89
+ source: { rawIds, exchangeIds: [], fingerprint },
90
+ exchanges: [],
91
+ sourceSummaries: ordered,
92
+ ...(prev ? { existing: prev } : {}),
93
+ });
94
+ }
95
+ tasks.sort((a, b) => b.id.localeCompare(a.id));
96
+ return { tasks, skipped };
97
+ },
98
+ buildRequest(task) {
99
+ const system = systemPrompt();
100
+ const summaries = task.sourceSummaries ?? [];
101
+ const prompt = [
102
+ `月份:${task.id}(共 ${summaries.length} 个会话摘要)`,
103
+ formatSessionSummaries(summaries),
104
+ ].join("\n\n");
105
+ return { system, prompt, maxTokens: 3072 };
106
+ },
107
+ finalize(task, output, now) {
108
+ const text = output.trim();
109
+ requireThat(text.length > 0, "月度历史汇总为空。");
110
+ return {
111
+ id: task.id,
112
+ dimension: this.id,
113
+ title: task.title,
114
+ text,
115
+ from: task.from,
116
+ to: task.to,
117
+ updatedAt: now,
118
+ source: task.source,
119
+ };
120
+ },
121
+ };
@@ -0,0 +1,7 @@
1
+ import type { SummaryDimension } from "../../../model/history.js";
2
+ /**
3
+ * Default per-session task summary dimension.
4
+ * Produces one concise summary per raw session archive, supporting exact deduplication
5
+ * (0 tokens on unchanged sessions) and rolling incremental updates when new turns are appended.
6
+ */
7
+ export declare const sessionSummaryDimension: SummaryDimension;
@@ -0,0 +1,124 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { resourceRoot } from "../../../common/paths.js";
4
+ import { requireThat } from "../../../common/validation.js";
5
+ import { fingerprintExchanges, orderedRawExchanges, rawSessionSpan, rawSessionTitle } from "../history-index.js";
6
+ let cachedPrompt;
7
+ function systemPrompt() {
8
+ cachedPrompt ??= readFileSync(join(resourceRoot, "prompts/history-summary.md"), "utf8");
9
+ return cachedPrompt;
10
+ }
11
+ function formatExchanges(exchanges) {
12
+ return exchanges
13
+ .map(item => `[${item.from} — ${item.to}${item.completed ? "" : ";未完成"}]\n${item.text}`)
14
+ .join("\n\n");
15
+ }
16
+ function isStrictUnchangedPrefix(existing, ordered) {
17
+ const prevIds = existing.source.exchangeIds;
18
+ if (prevIds.length === 0 || prevIds.length >= ordered.length)
19
+ return false;
20
+ for (let i = 0; i < prevIds.length; i++) {
21
+ if (ordered[i].id !== prevIds[i])
22
+ return false;
23
+ }
24
+ const prefixFingerprint = fingerprintExchanges(ordered.slice(0, prevIds.length));
25
+ return prefixFingerprint === existing.source.fingerprint;
26
+ }
27
+ /**
28
+ * Default per-session task summary dimension.
29
+ * Produces one concise summary per raw session archive, supporting exact deduplication
30
+ * (0 tokens on unchanged sessions) and rolling incremental updates when new turns are appended.
31
+ */
32
+ export const sessionSummaryDimension = {
33
+ id: "session",
34
+ title: "会话任务摘要",
35
+ description: "按单个会话提炼任务目标、关键决定、结果与未决事项,支持去重跳过与增量滚动更新。",
36
+ plan(raws, existing, options = {}) {
37
+ const force = Boolean(options.force);
38
+ const tasks = [];
39
+ let skipped = 0;
40
+ for (const raw of raws) {
41
+ const ordered = orderedRawExchanges(raw);
42
+ if (!ordered.length)
43
+ continue;
44
+ const exchangeIds = ordered.map(item => item.id);
45
+ const fingerprint = fingerprintExchanges(ordered);
46
+ const prev = existing.get(raw.id);
47
+ if (!force && prev && prev.source.fingerprint === fingerprint) {
48
+ skipped++;
49
+ continue;
50
+ }
51
+ const span = rawSessionSpan(ordered);
52
+ const title = rawSessionTitle(raw);
53
+ const source = { rawIds: [raw.id], exchangeIds, fingerprint };
54
+ if (!force && prev && isStrictUnchangedPrefix(prev, ordered)) {
55
+ tasks.push({
56
+ id: raw.id,
57
+ dimension: this.id,
58
+ mode: "incremental",
59
+ title,
60
+ host: raw.host,
61
+ sessionId: raw.sessionId,
62
+ from: span.from,
63
+ to: span.to,
64
+ source,
65
+ exchanges: ordered,
66
+ existing: prev,
67
+ addedExchanges: ordered.slice(prev.source.exchangeIds.length),
68
+ });
69
+ }
70
+ else {
71
+ tasks.push({
72
+ id: raw.id,
73
+ dimension: this.id,
74
+ mode: prev ? "rebuild" : "create",
75
+ title,
76
+ host: raw.host,
77
+ sessionId: raw.sessionId,
78
+ from: span.from,
79
+ to: span.to,
80
+ source,
81
+ exchanges: ordered,
82
+ ...(prev ? { existing: prev } : {}),
83
+ });
84
+ }
85
+ }
86
+ // Process most recently active sessions first.
87
+ tasks.sort((a, b) => b.to.localeCompare(a.to) || a.id.localeCompare(b.id));
88
+ return { tasks, skipped };
89
+ },
90
+ buildRequest(task) {
91
+ const system = systemPrompt();
92
+ const heading = task.title ? [`会话标题:${task.title}`] : [];
93
+ if (task.mode === "incremental" && task.existing && task.addedExchanges?.length) {
94
+ const prompt = [
95
+ ...heading,
96
+ `【已有摘要(${task.existing.from} — ${task.existing.to})】\n${task.existing.text}`,
97
+ `【新增对话】\n${formatExchanges(task.addedExchanges)}`,
98
+ "在已有摘要基础上合并新增对话,输出更新后的完整摘要:保留仍有效的事实,用新决定覆盖旧决定,去重并保持简洁。",
99
+ ].join("\n\n");
100
+ return { system, prompt, maxTokens: 2048 };
101
+ }
102
+ const prompt = [
103
+ ...heading,
104
+ formatExchanges(task.exchanges),
105
+ ].join("\n\n");
106
+ return { system, prompt, maxTokens: 2048 };
107
+ },
108
+ finalize(task, output, now) {
109
+ const text = output.trim();
110
+ requireThat(text.length > 0, "历史摘要为空。");
111
+ return {
112
+ id: task.id,
113
+ dimension: this.id,
114
+ ...(task.host ? { host: task.host } : {}),
115
+ ...(task.sessionId ? { sessionId: task.sessionId } : {}),
116
+ title: task.title,
117
+ text,
118
+ from: task.from,
119
+ to: task.to,
120
+ updatedAt: now,
121
+ source: task.source,
122
+ };
123
+ },
124
+ };
@@ -0,0 +1,7 @@
1
+ import type { HistoryLayout, SummaryDimension } from "../../model/history.js";
2
+ /** Register a custom summary dimension (e.g. daily, decisions, topics). */
3
+ export declare function registerSummaryDimension(dimension: SummaryDimension): void;
4
+ export declare function unregisterSummaryDimension(id: string): boolean;
5
+ export declare function getSummaryDimension(id: string): SummaryDimension | undefined;
6
+ export declare function listSummaryDimensions(layout?: HistoryLayout): SummaryDimension[];
7
+ export declare function resolveSummaryDimensions(ids?: readonly string[], layout?: HistoryLayout): SummaryDimension[];
@@ -0,0 +1,36 @@
1
+ import { requireThat } from "../../common/validation.js";
2
+ import { isSummaryDimension } from "../../storage/history-format.js";
3
+ import { monthlySummaryDimension } from "./summary-dimensions/monthly.js";
4
+ import { sessionSummaryDimension } from "./summary-dimensions/session.js";
5
+ const registry = new Map([
6
+ [sessionSummaryDimension.id, sessionSummaryDimension],
7
+ [monthlySummaryDimension.id, monthlySummaryDimension],
8
+ ]);
9
+ /** Register a custom summary dimension (e.g. daily, decisions, topics). */
10
+ export function registerSummaryDimension(dimension) {
11
+ requireThat(isSummaryDimension(dimension.id), "摘要维度 ID 无效:" + dimension.id);
12
+ registry.set(dimension.id, dimension);
13
+ }
14
+ export function unregisterSummaryDimension(id) {
15
+ return registry.delete(id);
16
+ }
17
+ export function getSummaryDimension(id) {
18
+ return registry.get(id);
19
+ }
20
+ export function listSummaryDimensions(layout) {
21
+ const all = [...registry.values()];
22
+ if (layout === "flat")
23
+ return all.filter(dim => !dim.requiresMonthlyLayout);
24
+ return all;
25
+ }
26
+ export function resolveSummaryDimensions(ids, layout = "monthly") {
27
+ if (!ids || ids.length === 0)
28
+ return listSummaryDimensions(layout);
29
+ const unique = [...new Set(ids)];
30
+ return unique.map(id => {
31
+ const found = registry.get(id);
32
+ requireThat(found, `未知的摘要维度:${id}(可用:${[...registry.keys()].join(", ")})。`);
33
+ requireThat(!found.requiresMonthlyLayout || layout === "monthly", `摘要维度 ${id} 仅在年月目录布局(history.layout: "monthly")下启用。`);
34
+ return found;
35
+ });
36
+ }
@@ -0,0 +1,37 @@
1
+ import type { HistoryLayout, HistorySummarizer, HistorySummaryReminder, SummaryDimensionStatus, SummaryRunProgress, SummaryRunReport } from "../../model/history.js";
2
+ export declare const defaultSummaryReminderDays = 7;
3
+ export interface InspectSummaryOptions {
4
+ dimensions?: readonly string[];
5
+ thresholdDays?: number;
6
+ now?: Date;
7
+ }
8
+ export interface CheckSummaryReminderOptions {
9
+ dimensions?: readonly string[];
10
+ thresholdDays?: number;
11
+ now?: Date;
12
+ }
13
+ /** Read-only startup probe: reminds when raw history has existed >= thresholdDays and no summary ran within thresholdDays. */
14
+ export declare function checkHistorySummaryReminder(root: string, options?: CheckSummaryReminderOptions): HistorySummaryReminder | undefined;
15
+ /** Read-only inspection of raw archives vs derived summaries across requested dimensions. */
16
+ export declare function inspectWorkspaceSummaries(root: string, options?: InspectSummaryOptions): {
17
+ layout: HistoryLayout;
18
+ rawCount: number;
19
+ dimensions: SummaryDimensionStatus[];
20
+ warnings: string[];
21
+ reminder?: HistorySummaryReminder;
22
+ };
23
+ export interface RunSummaryOptions {
24
+ root: string;
25
+ summarizer: HistorySummarizer;
26
+ dimensions?: readonly string[];
27
+ force?: boolean;
28
+ concurrency?: number;
29
+ signal?: AbortSignal;
30
+ onProgress?: (progress: SummaryRunProgress) => void;
31
+ }
32
+ /**
33
+ * Trigger-agnostic summary execution engine.
34
+ * Reads raw archives once, plans deduplicated incremental/full tasks per dimension,
35
+ * executes LLM calls with bounded concurrency, and persists each artifact atomically.
36
+ */
37
+ export declare function runWorkspaceSummaries(options: RunSummaryOptions): Promise<SummaryRunReport>;