@oh-my-pi/pi-coding-agent 0.1.0

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 (337) hide show
  1. package/CHANGELOG.md +1629 -0
  2. package/README.md +1041 -0
  3. package/docs/compaction.md +403 -0
  4. package/docs/config-usage.md +113 -0
  5. package/docs/custom-tools.md +541 -0
  6. package/docs/extension-loading.md +1004 -0
  7. package/docs/hooks.md +867 -0
  8. package/docs/rpc.md +1040 -0
  9. package/docs/sdk.md +994 -0
  10. package/docs/session-tree-plan.md +441 -0
  11. package/docs/session.md +240 -0
  12. package/docs/skills.md +290 -0
  13. package/docs/theme.md +670 -0
  14. package/docs/tree.md +197 -0
  15. package/docs/tui.md +341 -0
  16. package/examples/README.md +21 -0
  17. package/examples/custom-tools/README.md +124 -0
  18. package/examples/custom-tools/hello/index.ts +20 -0
  19. package/examples/custom-tools/question/index.ts +84 -0
  20. package/examples/custom-tools/subagent/README.md +172 -0
  21. package/examples/custom-tools/subagent/agents/planner.md +37 -0
  22. package/examples/custom-tools/subagent/agents/scout.md +50 -0
  23. package/examples/custom-tools/subagent/agents/worker.md +24 -0
  24. package/examples/custom-tools/subagent/agents.ts +156 -0
  25. package/examples/custom-tools/subagent/commands/implement-and-review.md +10 -0
  26. package/examples/custom-tools/subagent/commands/implement.md +10 -0
  27. package/examples/custom-tools/subagent/commands/scout-and-plan.md +9 -0
  28. package/examples/custom-tools/subagent/index.ts +1002 -0
  29. package/examples/custom-tools/todo/index.ts +212 -0
  30. package/examples/hooks/README.md +56 -0
  31. package/examples/hooks/auto-commit-on-exit.ts +49 -0
  32. package/examples/hooks/confirm-destructive.ts +59 -0
  33. package/examples/hooks/custom-compaction.ts +116 -0
  34. package/examples/hooks/dirty-repo-guard.ts +52 -0
  35. package/examples/hooks/file-trigger.ts +41 -0
  36. package/examples/hooks/git-checkpoint.ts +53 -0
  37. package/examples/hooks/handoff.ts +150 -0
  38. package/examples/hooks/permission-gate.ts +34 -0
  39. package/examples/hooks/protected-paths.ts +30 -0
  40. package/examples/hooks/qna.ts +119 -0
  41. package/examples/hooks/snake.ts +343 -0
  42. package/examples/hooks/status-line.ts +40 -0
  43. package/examples/sdk/01-minimal.ts +22 -0
  44. package/examples/sdk/02-custom-model.ts +49 -0
  45. package/examples/sdk/03-custom-prompt.ts +44 -0
  46. package/examples/sdk/04-skills.ts +44 -0
  47. package/examples/sdk/05-tools.ts +90 -0
  48. package/examples/sdk/06-hooks.ts +61 -0
  49. package/examples/sdk/07-context-files.ts +36 -0
  50. package/examples/sdk/08-slash-commands.ts +42 -0
  51. package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
  52. package/examples/sdk/10-settings.ts +38 -0
  53. package/examples/sdk/11-sessions.ts +48 -0
  54. package/examples/sdk/12-full-control.ts +95 -0
  55. package/examples/sdk/README.md +154 -0
  56. package/package.json +89 -0
  57. package/src/bun-imports.d.ts +16 -0
  58. package/src/capability/context-file.ts +40 -0
  59. package/src/capability/extension.ts +48 -0
  60. package/src/capability/hook.ts +40 -0
  61. package/src/capability/index.ts +616 -0
  62. package/src/capability/instruction.ts +37 -0
  63. package/src/capability/mcp.ts +52 -0
  64. package/src/capability/prompt.ts +35 -0
  65. package/src/capability/rule.ts +56 -0
  66. package/src/capability/settings.ts +35 -0
  67. package/src/capability/skill.ts +49 -0
  68. package/src/capability/slash-command.ts +40 -0
  69. package/src/capability/system-prompt.ts +35 -0
  70. package/src/capability/tool.ts +38 -0
  71. package/src/capability/types.ts +166 -0
  72. package/src/cli/args.ts +259 -0
  73. package/src/cli/file-processor.ts +121 -0
  74. package/src/cli/list-models.ts +104 -0
  75. package/src/cli/plugin-cli.ts +661 -0
  76. package/src/cli/session-picker.ts +41 -0
  77. package/src/cli/update-cli.ts +274 -0
  78. package/src/cli.ts +10 -0
  79. package/src/config.ts +391 -0
  80. package/src/core/agent-session.ts +2178 -0
  81. package/src/core/auth-storage.ts +258 -0
  82. package/src/core/bash-executor.ts +197 -0
  83. package/src/core/compaction/branch-summarization.ts +315 -0
  84. package/src/core/compaction/compaction.ts +664 -0
  85. package/src/core/compaction/index.ts +7 -0
  86. package/src/core/compaction/utils.ts +153 -0
  87. package/src/core/custom-commands/bundled/review/index.ts +156 -0
  88. package/src/core/custom-commands/index.ts +15 -0
  89. package/src/core/custom-commands/loader.ts +226 -0
  90. package/src/core/custom-commands/types.ts +112 -0
  91. package/src/core/custom-tools/index.ts +22 -0
  92. package/src/core/custom-tools/loader.ts +248 -0
  93. package/src/core/custom-tools/types.ts +185 -0
  94. package/src/core/custom-tools/wrapper.ts +29 -0
  95. package/src/core/exec.ts +139 -0
  96. package/src/core/export-html/index.ts +159 -0
  97. package/src/core/export-html/template.css +774 -0
  98. package/src/core/export-html/template.generated.ts +2 -0
  99. package/src/core/export-html/template.html +45 -0
  100. package/src/core/export-html/template.js +1185 -0
  101. package/src/core/export-html/template.macro.ts +24 -0
  102. package/src/core/file-mentions.ts +54 -0
  103. package/src/core/hooks/index.ts +16 -0
  104. package/src/core/hooks/loader.ts +288 -0
  105. package/src/core/hooks/runner.ts +434 -0
  106. package/src/core/hooks/tool-wrapper.ts +98 -0
  107. package/src/core/hooks/types.ts +770 -0
  108. package/src/core/index.ts +53 -0
  109. package/src/core/logger.ts +112 -0
  110. package/src/core/mcp/client.ts +185 -0
  111. package/src/core/mcp/config.ts +248 -0
  112. package/src/core/mcp/index.ts +45 -0
  113. package/src/core/mcp/loader.ts +99 -0
  114. package/src/core/mcp/manager.ts +235 -0
  115. package/src/core/mcp/tool-bridge.ts +156 -0
  116. package/src/core/mcp/transports/http.ts +316 -0
  117. package/src/core/mcp/transports/index.ts +6 -0
  118. package/src/core/mcp/transports/stdio.ts +252 -0
  119. package/src/core/mcp/types.ts +228 -0
  120. package/src/core/messages.ts +211 -0
  121. package/src/core/model-registry.ts +334 -0
  122. package/src/core/model-resolver.ts +494 -0
  123. package/src/core/plugins/doctor.ts +67 -0
  124. package/src/core/plugins/index.ts +38 -0
  125. package/src/core/plugins/installer.ts +189 -0
  126. package/src/core/plugins/loader.ts +339 -0
  127. package/src/core/plugins/manager.ts +672 -0
  128. package/src/core/plugins/parser.ts +105 -0
  129. package/src/core/plugins/paths.ts +37 -0
  130. package/src/core/plugins/types.ts +190 -0
  131. package/src/core/sdk.ts +900 -0
  132. package/src/core/session-manager.ts +1837 -0
  133. package/src/core/settings-manager.ts +860 -0
  134. package/src/core/skills.ts +352 -0
  135. package/src/core/slash-commands.ts +132 -0
  136. package/src/core/system-prompt.ts +442 -0
  137. package/src/core/timings.ts +25 -0
  138. package/src/core/title-generator.ts +110 -0
  139. package/src/core/tools/ask.ts +193 -0
  140. package/src/core/tools/bash-interceptor.ts +120 -0
  141. package/src/core/tools/bash.ts +91 -0
  142. package/src/core/tools/context.ts +32 -0
  143. package/src/core/tools/edit-diff.ts +487 -0
  144. package/src/core/tools/edit.ts +140 -0
  145. package/src/core/tools/exa/company.ts +59 -0
  146. package/src/core/tools/exa/index.ts +63 -0
  147. package/src/core/tools/exa/linkedin.ts +59 -0
  148. package/src/core/tools/exa/mcp-client.ts +368 -0
  149. package/src/core/tools/exa/render.ts +200 -0
  150. package/src/core/tools/exa/researcher.ts +90 -0
  151. package/src/core/tools/exa/search.ts +338 -0
  152. package/src/core/tools/exa/types.ts +167 -0
  153. package/src/core/tools/exa/websets.ts +248 -0
  154. package/src/core/tools/find.ts +244 -0
  155. package/src/core/tools/grep.ts +584 -0
  156. package/src/core/tools/index.ts +283 -0
  157. package/src/core/tools/ls.ts +142 -0
  158. package/src/core/tools/lsp/client.ts +767 -0
  159. package/src/core/tools/lsp/clients/biome-client.ts +207 -0
  160. package/src/core/tools/lsp/clients/index.ts +49 -0
  161. package/src/core/tools/lsp/clients/lsp-linter-client.ts +98 -0
  162. package/src/core/tools/lsp/config.ts +845 -0
  163. package/src/core/tools/lsp/edits.ts +110 -0
  164. package/src/core/tools/lsp/index.ts +1364 -0
  165. package/src/core/tools/lsp/render.ts +560 -0
  166. package/src/core/tools/lsp/rust-analyzer.ts +145 -0
  167. package/src/core/tools/lsp/types.ts +495 -0
  168. package/src/core/tools/lsp/utils.ts +526 -0
  169. package/src/core/tools/notebook.ts +182 -0
  170. package/src/core/tools/output.ts +198 -0
  171. package/src/core/tools/path-utils.ts +61 -0
  172. package/src/core/tools/read.ts +507 -0
  173. package/src/core/tools/renderers.ts +820 -0
  174. package/src/core/tools/review.ts +275 -0
  175. package/src/core/tools/rulebook.ts +124 -0
  176. package/src/core/tools/task/agents.ts +158 -0
  177. package/src/core/tools/task/artifacts.ts +114 -0
  178. package/src/core/tools/task/commands.ts +157 -0
  179. package/src/core/tools/task/discovery.ts +217 -0
  180. package/src/core/tools/task/executor.ts +531 -0
  181. package/src/core/tools/task/index.ts +548 -0
  182. package/src/core/tools/task/model-resolver.ts +176 -0
  183. package/src/core/tools/task/parallel.ts +38 -0
  184. package/src/core/tools/task/render.ts +502 -0
  185. package/src/core/tools/task/subprocess-tool-registry.ts +89 -0
  186. package/src/core/tools/task/types.ts +142 -0
  187. package/src/core/tools/truncate.ts +265 -0
  188. package/src/core/tools/web-fetch.ts +2511 -0
  189. package/src/core/tools/web-search/auth.ts +199 -0
  190. package/src/core/tools/web-search/index.ts +583 -0
  191. package/src/core/tools/web-search/providers/anthropic.ts +198 -0
  192. package/src/core/tools/web-search/providers/exa.ts +196 -0
  193. package/src/core/tools/web-search/providers/perplexity.ts +195 -0
  194. package/src/core/tools/web-search/render.ts +372 -0
  195. package/src/core/tools/web-search/types.ts +180 -0
  196. package/src/core/tools/write.ts +63 -0
  197. package/src/core/ttsr.ts +211 -0
  198. package/src/core/utils.ts +187 -0
  199. package/src/discovery/agents-md.ts +75 -0
  200. package/src/discovery/builtin.ts +647 -0
  201. package/src/discovery/claude.ts +623 -0
  202. package/src/discovery/cline.ts +104 -0
  203. package/src/discovery/codex.ts +571 -0
  204. package/src/discovery/cursor.ts +266 -0
  205. package/src/discovery/gemini.ts +368 -0
  206. package/src/discovery/github.ts +120 -0
  207. package/src/discovery/helpers.test.ts +127 -0
  208. package/src/discovery/helpers.ts +249 -0
  209. package/src/discovery/index.ts +84 -0
  210. package/src/discovery/mcp-json.ts +127 -0
  211. package/src/discovery/vscode.ts +99 -0
  212. package/src/discovery/windsurf.ts +219 -0
  213. package/src/index.ts +192 -0
  214. package/src/main.ts +507 -0
  215. package/src/migrations.ts +156 -0
  216. package/src/modes/cleanup.ts +23 -0
  217. package/src/modes/index.ts +48 -0
  218. package/src/modes/interactive/components/armin.ts +382 -0
  219. package/src/modes/interactive/components/assistant-message.ts +86 -0
  220. package/src/modes/interactive/components/bash-execution.ts +199 -0
  221. package/src/modes/interactive/components/bordered-loader.ts +41 -0
  222. package/src/modes/interactive/components/branch-summary-message.ts +42 -0
  223. package/src/modes/interactive/components/compaction-summary-message.ts +45 -0
  224. package/src/modes/interactive/components/custom-editor.ts +122 -0
  225. package/src/modes/interactive/components/diff.ts +147 -0
  226. package/src/modes/interactive/components/dynamic-border.ts +25 -0
  227. package/src/modes/interactive/components/extensions/extension-dashboard.ts +296 -0
  228. package/src/modes/interactive/components/extensions/extension-list.ts +479 -0
  229. package/src/modes/interactive/components/extensions/index.ts +9 -0
  230. package/src/modes/interactive/components/extensions/inspector-panel.ts +313 -0
  231. package/src/modes/interactive/components/extensions/state-manager.ts +558 -0
  232. package/src/modes/interactive/components/extensions/types.ts +191 -0
  233. package/src/modes/interactive/components/hook-editor.ts +117 -0
  234. package/src/modes/interactive/components/hook-input.ts +64 -0
  235. package/src/modes/interactive/components/hook-message.ts +96 -0
  236. package/src/modes/interactive/components/hook-selector.ts +91 -0
  237. package/src/modes/interactive/components/model-selector.ts +560 -0
  238. package/src/modes/interactive/components/oauth-selector.ts +136 -0
  239. package/src/modes/interactive/components/plugin-settings.ts +481 -0
  240. package/src/modes/interactive/components/queue-mode-selector.ts +56 -0
  241. package/src/modes/interactive/components/session-selector.ts +220 -0
  242. package/src/modes/interactive/components/settings-defs.ts +597 -0
  243. package/src/modes/interactive/components/settings-selector.ts +545 -0
  244. package/src/modes/interactive/components/show-images-selector.ts +45 -0
  245. package/src/modes/interactive/components/status-line/index.ts +4 -0
  246. package/src/modes/interactive/components/status-line/presets.ts +94 -0
  247. package/src/modes/interactive/components/status-line/segments.ts +350 -0
  248. package/src/modes/interactive/components/status-line/separators.ts +55 -0
  249. package/src/modes/interactive/components/status-line/types.ts +81 -0
  250. package/src/modes/interactive/components/status-line-segment-editor.ts +357 -0
  251. package/src/modes/interactive/components/status-line.ts +384 -0
  252. package/src/modes/interactive/components/theme-selector.ts +62 -0
  253. package/src/modes/interactive/components/thinking-selector.ts +64 -0
  254. package/src/modes/interactive/components/tool-execution.ts +946 -0
  255. package/src/modes/interactive/components/tree-selector.ts +877 -0
  256. package/src/modes/interactive/components/ttsr-notification.ts +82 -0
  257. package/src/modes/interactive/components/user-message-selector.ts +159 -0
  258. package/src/modes/interactive/components/user-message.ts +18 -0
  259. package/src/modes/interactive/components/visual-truncate.ts +50 -0
  260. package/src/modes/interactive/components/welcome.ts +228 -0
  261. package/src/modes/interactive/interactive-mode.ts +2669 -0
  262. package/src/modes/interactive/theme/dark.json +102 -0
  263. package/src/modes/interactive/theme/defaults/dark-arctic.json +111 -0
  264. package/src/modes/interactive/theme/defaults/dark-catppuccin.json +106 -0
  265. package/src/modes/interactive/theme/defaults/dark-cyberpunk.json +109 -0
  266. package/src/modes/interactive/theme/defaults/dark-dracula.json +105 -0
  267. package/src/modes/interactive/theme/defaults/dark-forest.json +103 -0
  268. package/src/modes/interactive/theme/defaults/dark-github.json +112 -0
  269. package/src/modes/interactive/theme/defaults/dark-gruvbox.json +119 -0
  270. package/src/modes/interactive/theme/defaults/dark-monochrome.json +101 -0
  271. package/src/modes/interactive/theme/defaults/dark-monokai.json +105 -0
  272. package/src/modes/interactive/theme/defaults/dark-nord.json +104 -0
  273. package/src/modes/interactive/theme/defaults/dark-ocean.json +108 -0
  274. package/src/modes/interactive/theme/defaults/dark-one.json +107 -0
  275. package/src/modes/interactive/theme/defaults/dark-retro.json +99 -0
  276. package/src/modes/interactive/theme/defaults/dark-rose-pine.json +95 -0
  277. package/src/modes/interactive/theme/defaults/dark-solarized.json +96 -0
  278. package/src/modes/interactive/theme/defaults/dark-sunset.json +106 -0
  279. package/src/modes/interactive/theme/defaults/dark-synthwave.json +102 -0
  280. package/src/modes/interactive/theme/defaults/dark-tokyo-night.json +108 -0
  281. package/src/modes/interactive/theme/defaults/index.ts +67 -0
  282. package/src/modes/interactive/theme/defaults/light-arctic.json +106 -0
  283. package/src/modes/interactive/theme/defaults/light-catppuccin.json +105 -0
  284. package/src/modes/interactive/theme/defaults/light-cyberpunk.json +103 -0
  285. package/src/modes/interactive/theme/defaults/light-forest.json +107 -0
  286. package/src/modes/interactive/theme/defaults/light-github.json +114 -0
  287. package/src/modes/interactive/theme/defaults/light-gruvbox.json +115 -0
  288. package/src/modes/interactive/theme/defaults/light-monochrome.json +100 -0
  289. package/src/modes/interactive/theme/defaults/light-ocean.json +106 -0
  290. package/src/modes/interactive/theme/defaults/light-one.json +105 -0
  291. package/src/modes/interactive/theme/defaults/light-retro.json +105 -0
  292. package/src/modes/interactive/theme/defaults/light-solarized.json +101 -0
  293. package/src/modes/interactive/theme/defaults/light-sunset.json +106 -0
  294. package/src/modes/interactive/theme/defaults/light-synthwave.json +105 -0
  295. package/src/modes/interactive/theme/defaults/light-tokyo-night.json +118 -0
  296. package/src/modes/interactive/theme/light.json +99 -0
  297. package/src/modes/interactive/theme/theme-schema.json +424 -0
  298. package/src/modes/interactive/theme/theme.ts +2211 -0
  299. package/src/modes/print-mode.ts +163 -0
  300. package/src/modes/rpc/rpc-client.ts +527 -0
  301. package/src/modes/rpc/rpc-mode.ts +494 -0
  302. package/src/modes/rpc/rpc-types.ts +203 -0
  303. package/src/prompts/architect-plan.md +10 -0
  304. package/src/prompts/branch-summary-preamble.md +3 -0
  305. package/src/prompts/branch-summary.md +28 -0
  306. package/src/prompts/browser.md +71 -0
  307. package/src/prompts/compaction-summary.md +34 -0
  308. package/src/prompts/compaction-turn-prefix.md +16 -0
  309. package/src/prompts/compaction-update-summary.md +41 -0
  310. package/src/prompts/explore.md +82 -0
  311. package/src/prompts/implement-with-critic.md +11 -0
  312. package/src/prompts/implement.md +11 -0
  313. package/src/prompts/init.md +30 -0
  314. package/src/prompts/plan.md +54 -0
  315. package/src/prompts/reviewer.md +81 -0
  316. package/src/prompts/summarization-system.md +3 -0
  317. package/src/prompts/system-prompt.md +27 -0
  318. package/src/prompts/task.md +56 -0
  319. package/src/prompts/title-system.md +8 -0
  320. package/src/prompts/tools/ask.md +24 -0
  321. package/src/prompts/tools/bash.md +23 -0
  322. package/src/prompts/tools/edit.md +9 -0
  323. package/src/prompts/tools/find.md +6 -0
  324. package/src/prompts/tools/grep.md +12 -0
  325. package/src/prompts/tools/lsp.md +14 -0
  326. package/src/prompts/tools/output.md +23 -0
  327. package/src/prompts/tools/read.md +25 -0
  328. package/src/prompts/tools/web-fetch.md +8 -0
  329. package/src/prompts/tools/web-search.md +10 -0
  330. package/src/prompts/tools/write.md +10 -0
  331. package/src/utils/changelog.ts +99 -0
  332. package/src/utils/clipboard.ts +265 -0
  333. package/src/utils/fuzzy.ts +108 -0
  334. package/src/utils/mime.ts +30 -0
  335. package/src/utils/shell-snapshot.ts +218 -0
  336. package/src/utils/shell.ts +364 -0
  337. package/src/utils/tools-manager.ts +265 -0
@@ -0,0 +1,664 @@
1
+ /**
2
+ * Context compaction for long sessions.
3
+ *
4
+ * Pure functions for compaction logic. The session manager handles I/O,
5
+ * and after compaction the session is reloaded.
6
+ */
7
+
8
+ import type { AgentMessage } from "@oh-my-pi/pi-agent-core";
9
+ import type { AssistantMessage, Model, Usage } from "@oh-my-pi/pi-ai";
10
+ import { complete, completeSimple } from "@oh-my-pi/pi-ai";
11
+ import compactionSummaryPrompt from "../../prompts/compaction-summary.md" with { type: "text" };
12
+ import compactionTurnPrefixPrompt from "../../prompts/compaction-turn-prefix.md" with { type: "text" };
13
+ import compactionUpdateSummaryPrompt from "../../prompts/compaction-update-summary.md" with { type: "text" };
14
+ import { convertToLlm, createBranchSummaryMessage, createHookMessage } from "../messages";
15
+ import type { CompactionEntry, SessionEntry } from "../session-manager";
16
+ import {
17
+ computeFileLists,
18
+ createFileOps,
19
+ extractFileOpsFromMessage,
20
+ type FileOperations,
21
+ formatFileOperations,
22
+ SUMMARIZATION_SYSTEM_PROMPT,
23
+ serializeConversation,
24
+ } from "./utils";
25
+
26
+ // ============================================================================
27
+ // File Operation Tracking
28
+ // ============================================================================
29
+
30
+ /** Details stored in CompactionEntry.details for file tracking */
31
+ export interface CompactionDetails {
32
+ readFiles: string[];
33
+ modifiedFiles: string[];
34
+ }
35
+
36
+ /**
37
+ * Extract file operations from messages and previous compaction entries.
38
+ */
39
+ function extractFileOperations(
40
+ messages: AgentMessage[],
41
+ entries: SessionEntry[],
42
+ prevCompactionIndex: number,
43
+ ): FileOperations {
44
+ const fileOps = createFileOps();
45
+
46
+ // Collect from previous compaction's details (if pi-generated)
47
+ if (prevCompactionIndex >= 0) {
48
+ const prevCompaction = entries[prevCompactionIndex] as CompactionEntry;
49
+ if (!prevCompaction.fromHook && prevCompaction.details) {
50
+ const details = prevCompaction.details as CompactionDetails;
51
+ if (Array.isArray(details.readFiles)) {
52
+ for (const f of details.readFiles) fileOps.read.add(f);
53
+ }
54
+ if (Array.isArray(details.modifiedFiles)) {
55
+ for (const f of details.modifiedFiles) fileOps.edited.add(f);
56
+ }
57
+ }
58
+ }
59
+
60
+ // Extract from tool calls in messages
61
+ for (const msg of messages) {
62
+ extractFileOpsFromMessage(msg, fileOps);
63
+ }
64
+
65
+ return fileOps;
66
+ }
67
+
68
+ // ============================================================================
69
+ // Message Extraction
70
+ // ============================================================================
71
+
72
+ /**
73
+ * Extract AgentMessage from an entry if it produces one.
74
+ * Returns undefined for entries that don't contribute to LLM context.
75
+ */
76
+ function getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {
77
+ if (entry.type === "message") {
78
+ return entry.message;
79
+ }
80
+ if (entry.type === "custom_message") {
81
+ return createHookMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp);
82
+ }
83
+ if (entry.type === "branch_summary") {
84
+ return createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);
85
+ }
86
+ return undefined;
87
+ }
88
+
89
+ /** Result from compact() - SessionManager adds uuid/parentUuid when saving */
90
+ export interface CompactionResult<T = unknown> {
91
+ summary: string;
92
+ firstKeptEntryId: string;
93
+ tokensBefore: number;
94
+ /** Hook-specific data (e.g., ArtifactIndex, version markers for structured compaction) */
95
+ details?: T;
96
+ }
97
+
98
+ // ============================================================================
99
+ // Types
100
+ // ============================================================================
101
+
102
+ export interface CompactionSettings {
103
+ enabled: boolean;
104
+ reserveTokens: number;
105
+ keepRecentTokens: number;
106
+ }
107
+
108
+ export const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = {
109
+ enabled: true,
110
+ reserveTokens: 16384,
111
+ keepRecentTokens: 20000,
112
+ };
113
+
114
+ // ============================================================================
115
+ // Token calculation
116
+ // ============================================================================
117
+
118
+ /**
119
+ * Calculate total context tokens from usage.
120
+ * Uses the native totalTokens field when available, falls back to computing from components.
121
+ */
122
+ export function calculateContextTokens(usage: Usage): number {
123
+ return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
124
+ }
125
+
126
+ /**
127
+ * Get usage from an assistant message if available.
128
+ * Skips aborted and error messages as they don't have valid usage data.
129
+ */
130
+ function getAssistantUsage(msg: AgentMessage): Usage | undefined {
131
+ if (msg.role === "assistant" && "usage" in msg) {
132
+ const assistantMsg = msg as AssistantMessage;
133
+ if (assistantMsg.stopReason !== "aborted" && assistantMsg.stopReason !== "error" && assistantMsg.usage) {
134
+ return assistantMsg.usage;
135
+ }
136
+ }
137
+ return undefined;
138
+ }
139
+
140
+ /**
141
+ * Find the last non-aborted assistant message usage from session entries.
142
+ */
143
+ export function getLastAssistantUsage(entries: SessionEntry[]): Usage | undefined {
144
+ for (let i = entries.length - 1; i >= 0; i--) {
145
+ const entry = entries[i];
146
+ if (entry.type === "message") {
147
+ const usage = getAssistantUsage(entry.message);
148
+ if (usage) return usage;
149
+ }
150
+ }
151
+ return undefined;
152
+ }
153
+
154
+ /**
155
+ * Check if compaction should trigger based on context usage.
156
+ */
157
+ export function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean {
158
+ if (!settings.enabled) return false;
159
+ return contextTokens > contextWindow - settings.reserveTokens;
160
+ }
161
+
162
+ // ============================================================================
163
+ // Cut point detection
164
+ // ============================================================================
165
+
166
+ /**
167
+ * Estimate token count for a message using chars/4 heuristic.
168
+ * This is conservative (overestimates tokens).
169
+ */
170
+ export function estimateTokens(message: AgentMessage): number {
171
+ let chars = 0;
172
+
173
+ switch (message.role) {
174
+ case "user": {
175
+ const content = (message as { content: string | Array<{ type: string; text?: string }> }).content;
176
+ if (typeof content === "string") {
177
+ chars = content.length;
178
+ } else if (Array.isArray(content)) {
179
+ for (const block of content) {
180
+ if (block.type === "text" && block.text) {
181
+ chars += block.text.length;
182
+ }
183
+ }
184
+ }
185
+ return Math.ceil(chars / 4);
186
+ }
187
+ case "assistant": {
188
+ const assistant = message as AssistantMessage;
189
+ for (const block of assistant.content) {
190
+ if (block.type === "text") {
191
+ chars += block.text.length;
192
+ } else if (block.type === "thinking") {
193
+ chars += block.thinking.length;
194
+ } else if (block.type === "toolCall") {
195
+ chars += block.name.length + JSON.stringify(block.arguments).length;
196
+ }
197
+ }
198
+ return Math.ceil(chars / 4);
199
+ }
200
+ case "hookMessage":
201
+ case "toolResult": {
202
+ if (typeof message.content === "string") {
203
+ chars = message.content.length;
204
+ } else {
205
+ for (const block of message.content) {
206
+ if (block.type === "text" && block.text) {
207
+ chars += block.text.length;
208
+ }
209
+ if (block.type === "image") {
210
+ chars += 4800; // Estimate images as 4000 chars, or 1200 tokens
211
+ }
212
+ }
213
+ }
214
+ return Math.ceil(chars / 4);
215
+ }
216
+ case "bashExecution": {
217
+ chars = message.command.length + message.output.length;
218
+ return Math.ceil(chars / 4);
219
+ }
220
+ case "branchSummary":
221
+ case "compactionSummary": {
222
+ chars = message.summary.length;
223
+ return Math.ceil(chars / 4);
224
+ }
225
+ }
226
+
227
+ return 0;
228
+ }
229
+
230
+ /**
231
+ * Find valid cut points: indices of user, assistant, custom, or bashExecution messages.
232
+ * Never cut at tool results (they must follow their tool call).
233
+ * When we cut at an assistant message with tool calls, its tool results follow it
234
+ * and will be kept.
235
+ * BashExecutionMessage is treated like a user message (user-initiated context).
236
+ */
237
+ function findValidCutPoints(entries: SessionEntry[], startIndex: number, endIndex: number): number[] {
238
+ const cutPoints: number[] = [];
239
+ for (let i = startIndex; i < endIndex; i++) {
240
+ const entry = entries[i];
241
+ switch (entry.type) {
242
+ case "message": {
243
+ const role = entry.message.role;
244
+ switch (role) {
245
+ case "bashExecution":
246
+ case "hookMessage":
247
+ case "branchSummary":
248
+ case "compactionSummary":
249
+ case "user":
250
+ case "assistant":
251
+ cutPoints.push(i);
252
+ break;
253
+ case "toolResult":
254
+ break;
255
+ }
256
+ break;
257
+ }
258
+ case "thinking_level_change":
259
+ case "model_change":
260
+ case "compaction":
261
+ case "branch_summary":
262
+ case "custom":
263
+ case "custom_message":
264
+ case "label":
265
+ }
266
+ // branch_summary and custom_message are user-role messages, valid cut points
267
+ if (entry.type === "branch_summary" || entry.type === "custom_message") {
268
+ cutPoints.push(i);
269
+ }
270
+ }
271
+ return cutPoints;
272
+ }
273
+
274
+ /**
275
+ * Find the user message (or bashExecution) that starts the turn containing the given entry index.
276
+ * Returns -1 if no turn start found before the index.
277
+ * BashExecutionMessage is treated like a user message for turn boundaries.
278
+ */
279
+ export function findTurnStartIndex(entries: SessionEntry[], entryIndex: number, startIndex: number): number {
280
+ for (let i = entryIndex; i >= startIndex; i--) {
281
+ const entry = entries[i];
282
+ // branch_summary and custom_message are user-role messages, can start a turn
283
+ if (entry.type === "branch_summary" || entry.type === "custom_message") {
284
+ return i;
285
+ }
286
+ if (entry.type === "message") {
287
+ const role = entry.message.role;
288
+ if (role === "user" || role === "bashExecution") {
289
+ return i;
290
+ }
291
+ }
292
+ }
293
+ return -1;
294
+ }
295
+
296
+ export interface CutPointResult {
297
+ /** Index of first entry to keep */
298
+ firstKeptEntryIndex: number;
299
+ /** Index of user message that starts the turn being split, or -1 if not splitting */
300
+ turnStartIndex: number;
301
+ /** Whether this cut splits a turn (cut point is not a user message) */
302
+ isSplitTurn: boolean;
303
+ }
304
+
305
+ /**
306
+ * Find the cut point in session entries that keeps approximately `keepRecentTokens`.
307
+ *
308
+ * Algorithm: Walk backwards from newest, accumulating estimated message sizes.
309
+ * Stop when we've accumulated >= keepRecentTokens. Cut at that point.
310
+ *
311
+ * Can cut at user OR assistant messages (never tool results). When cutting at an
312
+ * assistant message with tool calls, its tool results come after and will be kept.
313
+ *
314
+ * Returns CutPointResult with:
315
+ * - firstKeptEntryIndex: the entry index to start keeping from
316
+ * - turnStartIndex: if cutting mid-turn, the user message that started that turn
317
+ * - isSplitTurn: whether we're cutting in the middle of a turn
318
+ *
319
+ * Only considers entries between `startIndex` and `endIndex` (exclusive).
320
+ */
321
+ export function findCutPoint(
322
+ entries: SessionEntry[],
323
+ startIndex: number,
324
+ endIndex: number,
325
+ keepRecentTokens: number,
326
+ ): CutPointResult {
327
+ const cutPoints = findValidCutPoints(entries, startIndex, endIndex);
328
+
329
+ if (cutPoints.length === 0) {
330
+ return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };
331
+ }
332
+
333
+ // Walk backwards from newest, accumulating estimated message sizes
334
+ let accumulatedTokens = 0;
335
+ let cutIndex = cutPoints[0]; // Default: keep from first message (not header)
336
+
337
+ for (let i = endIndex - 1; i >= startIndex; i--) {
338
+ const entry = entries[i];
339
+ if (entry.type !== "message") continue;
340
+
341
+ // Estimate this message's size
342
+ const messageTokens = estimateTokens(entry.message);
343
+ accumulatedTokens += messageTokens;
344
+
345
+ // Check if we've exceeded the budget
346
+ if (accumulatedTokens >= keepRecentTokens) {
347
+ // Find the closest valid cut point at or after this entry
348
+ for (let c = 0; c < cutPoints.length; c++) {
349
+ if (cutPoints[c] >= i) {
350
+ cutIndex = cutPoints[c];
351
+ break;
352
+ }
353
+ }
354
+ break;
355
+ }
356
+ }
357
+
358
+ // Scan backwards from cutIndex to include any non-message entries (bash, settings, etc.)
359
+ while (cutIndex > startIndex) {
360
+ const prevEntry = entries[cutIndex - 1];
361
+ // Stop at session header or compaction boundaries
362
+ if (prevEntry.type === "compaction") {
363
+ break;
364
+ }
365
+ if (prevEntry.type === "message") {
366
+ // Stop if we hit any message
367
+ break;
368
+ }
369
+ // Include this non-message entry (bash, settings change, etc.)
370
+ cutIndex--;
371
+ }
372
+
373
+ // Determine if this is a split turn
374
+ const cutEntry = entries[cutIndex];
375
+ const isUserMessage = cutEntry.type === "message" && cutEntry.message.role === "user";
376
+ const turnStartIndex = isUserMessage ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);
377
+
378
+ return {
379
+ firstKeptEntryIndex: cutIndex,
380
+ turnStartIndex,
381
+ isSplitTurn: !isUserMessage && turnStartIndex !== -1,
382
+ };
383
+ }
384
+
385
+ // ============================================================================
386
+ // Summarization
387
+ // ============================================================================
388
+
389
+ const SUMMARIZATION_PROMPT = compactionSummaryPrompt;
390
+
391
+ const UPDATE_SUMMARIZATION_PROMPT = compactionUpdateSummaryPrompt;
392
+
393
+ /**
394
+ * Generate a summary of the conversation using the LLM.
395
+ * If previousSummary is provided, uses the update prompt to merge.
396
+ */
397
+ export async function generateSummary(
398
+ currentMessages: AgentMessage[],
399
+ model: Model<any>,
400
+ reserveTokens: number,
401
+ apiKey: string,
402
+ signal?: AbortSignal,
403
+ customInstructions?: string,
404
+ previousSummary?: string,
405
+ ): Promise<string> {
406
+ const maxTokens = Math.floor(0.8 * reserveTokens);
407
+
408
+ // Use update prompt if we have a previous summary, otherwise initial prompt
409
+ let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
410
+ if (customInstructions) {
411
+ basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`;
412
+ }
413
+
414
+ // Serialize conversation to text so model doesn't try to continue it
415
+ // Convert to LLM messages first (handles custom types like bashExecution, hookMessage, etc.)
416
+ const llmMessages = convertToLlm(currentMessages);
417
+ const conversationText = serializeConversation(llmMessages);
418
+
419
+ // Build the prompt with conversation wrapped in tags
420
+ let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
421
+ if (previousSummary) {
422
+ promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
423
+ }
424
+ promptText += basePrompt;
425
+
426
+ const summarizationMessages = [
427
+ {
428
+ role: "user" as const,
429
+ content: [{ type: "text" as const, text: promptText }],
430
+ timestamp: Date.now(),
431
+ },
432
+ ];
433
+
434
+ const response = await completeSimple(
435
+ model,
436
+ { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },
437
+ { maxTokens, signal, apiKey, reasoning: "high" },
438
+ );
439
+
440
+ if (response.stopReason === "error") {
441
+ throw new Error(`Summarization failed: ${response.errorMessage || "Unknown error"}`);
442
+ }
443
+
444
+ const textContent = response.content
445
+ .filter((c): c is { type: "text"; text: string } => c.type === "text")
446
+ .map((c) => c.text)
447
+ .join("\n");
448
+
449
+ return textContent;
450
+ }
451
+
452
+ // ============================================================================
453
+ // Compaction Preparation (for hooks)
454
+ // ============================================================================
455
+
456
+ export interface CompactionPreparation {
457
+ /** UUID of first entry to keep */
458
+ firstKeptEntryId: string;
459
+ /** Messages that will be summarized and discarded */
460
+ messagesToSummarize: AgentMessage[];
461
+ /** Messages that will be turned into turn prefix summary (if splitting) */
462
+ turnPrefixMessages: AgentMessage[];
463
+ /** Whether this is a split turn (cut point in middle of turn) */
464
+ isSplitTurn: boolean;
465
+ tokensBefore: number;
466
+ /** Summary from previous compaction, for iterative update */
467
+ previousSummary?: string;
468
+ /** File operations extracted from messagesToSummarize */
469
+ fileOps: FileOperations;
470
+ /** Compaction settions from settings.jsonl */
471
+ settings: CompactionSettings;
472
+ }
473
+
474
+ export function prepareCompaction(
475
+ pathEntries: SessionEntry[],
476
+ settings: CompactionSettings,
477
+ ): CompactionPreparation | undefined {
478
+ if (pathEntries.length > 0 && pathEntries[pathEntries.length - 1].type === "compaction") {
479
+ return undefined;
480
+ }
481
+
482
+ let prevCompactionIndex = -1;
483
+ for (let i = pathEntries.length - 1; i >= 0; i--) {
484
+ if (pathEntries[i].type === "compaction") {
485
+ prevCompactionIndex = i;
486
+ break;
487
+ }
488
+ }
489
+ const boundaryStart = prevCompactionIndex + 1;
490
+ const boundaryEnd = pathEntries.length;
491
+
492
+ const lastUsage = getLastAssistantUsage(pathEntries);
493
+ const tokensBefore = lastUsage ? calculateContextTokens(lastUsage) : 0;
494
+
495
+ const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, settings.keepRecentTokens);
496
+
497
+ // Get UUID of first kept entry
498
+ const firstKeptEntry = pathEntries[cutPoint.firstKeptEntryIndex];
499
+ if (!firstKeptEntry?.id) {
500
+ return undefined; // Session needs migration
501
+ }
502
+ const firstKeptEntryId = firstKeptEntry.id;
503
+
504
+ const historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;
505
+
506
+ // Messages to summarize (will be discarded after summary)
507
+ const messagesToSummarize: AgentMessage[] = [];
508
+ for (let i = boundaryStart; i < historyEnd; i++) {
509
+ const msg = getMessageFromEntry(pathEntries[i]);
510
+ if (msg) messagesToSummarize.push(msg);
511
+ }
512
+
513
+ // Messages for turn prefix summary (if splitting a turn)
514
+ const turnPrefixMessages: AgentMessage[] = [];
515
+ if (cutPoint.isSplitTurn) {
516
+ for (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) {
517
+ const msg = getMessageFromEntry(pathEntries[i]);
518
+ if (msg) turnPrefixMessages.push(msg);
519
+ }
520
+ }
521
+
522
+ // Get previous summary for iterative update
523
+ let previousSummary: string | undefined;
524
+ if (prevCompactionIndex >= 0) {
525
+ const prevCompaction = pathEntries[prevCompactionIndex] as CompactionEntry;
526
+ previousSummary = prevCompaction.summary;
527
+ }
528
+
529
+ // Extract file operations from messages and previous compaction
530
+ const fileOps = extractFileOperations(messagesToSummarize, pathEntries, prevCompactionIndex);
531
+
532
+ // Also extract file ops from turn prefix if splitting
533
+ if (cutPoint.isSplitTurn) {
534
+ for (const msg of turnPrefixMessages) {
535
+ extractFileOpsFromMessage(msg, fileOps);
536
+ }
537
+ }
538
+
539
+ return {
540
+ firstKeptEntryId,
541
+ messagesToSummarize,
542
+ turnPrefixMessages,
543
+ isSplitTurn: cutPoint.isSplitTurn,
544
+ tokensBefore,
545
+ previousSummary,
546
+ fileOps,
547
+ settings,
548
+ };
549
+ }
550
+
551
+ // ============================================================================
552
+ // Main compaction function
553
+ // ============================================================================
554
+
555
+ const TURN_PREFIX_SUMMARIZATION_PROMPT = compactionTurnPrefixPrompt;
556
+
557
+ /**
558
+ * Generate summaries for compaction using prepared data.
559
+ * Returns CompactionResult - SessionManager adds uuid/parentUuid when saving.
560
+ *
561
+ * @param preparation - Pre-calculated preparation from prepareCompaction()
562
+ * @param customInstructions - Optional custom focus for the summary
563
+ */
564
+ export async function compact(
565
+ preparation: CompactionPreparation,
566
+ model: Model<any>,
567
+ apiKey: string,
568
+ customInstructions?: string,
569
+ signal?: AbortSignal,
570
+ ): Promise<CompactionResult> {
571
+ const {
572
+ firstKeptEntryId,
573
+ messagesToSummarize,
574
+ turnPrefixMessages,
575
+ isSplitTurn,
576
+ tokensBefore,
577
+ previousSummary,
578
+ fileOps,
579
+ settings,
580
+ } = preparation;
581
+
582
+ // Generate summaries (can be parallel if both needed) and merge into one
583
+ let summary: string;
584
+
585
+ if (isSplitTurn && turnPrefixMessages.length > 0) {
586
+ // Generate both summaries in parallel
587
+ const [historyResult, turnPrefixResult] = await Promise.all([
588
+ messagesToSummarize.length > 0
589
+ ? generateSummary(
590
+ messagesToSummarize,
591
+ model,
592
+ settings.reserveTokens,
593
+ apiKey,
594
+ signal,
595
+ customInstructions,
596
+ previousSummary,
597
+ )
598
+ : Promise.resolve("No prior history."),
599
+ generateTurnPrefixSummary(turnPrefixMessages, model, settings.reserveTokens, apiKey, signal),
600
+ ]);
601
+ // Merge into single summary
602
+ summary = `${historyResult}\n\n---\n\n**Turn Context (split turn):**\n\n${turnPrefixResult}`;
603
+ } else {
604
+ // Just generate history summary
605
+ summary = await generateSummary(
606
+ messagesToSummarize,
607
+ model,
608
+ settings.reserveTokens,
609
+ apiKey,
610
+ signal,
611
+ customInstructions,
612
+ previousSummary,
613
+ );
614
+ }
615
+
616
+ // Compute file lists and append to summary
617
+ const { readFiles, modifiedFiles } = computeFileLists(fileOps);
618
+ summary += formatFileOperations(readFiles, modifiedFiles);
619
+
620
+ if (!firstKeptEntryId) {
621
+ throw new Error("First kept entry has no UUID - session may need migration");
622
+ }
623
+
624
+ return {
625
+ summary,
626
+ firstKeptEntryId,
627
+ tokensBefore,
628
+ details: { readFiles, modifiedFiles } as CompactionDetails,
629
+ };
630
+ }
631
+
632
+ /**
633
+ * Generate a summary for a turn prefix (when splitting a turn).
634
+ */
635
+ async function generateTurnPrefixSummary(
636
+ messages: AgentMessage[],
637
+ model: Model<any>,
638
+ reserveTokens: number,
639
+ apiKey: string,
640
+ signal?: AbortSignal,
641
+ ): Promise<string> {
642
+ const maxTokens = Math.floor(0.5 * reserveTokens); // Smaller budget for turn prefix
643
+
644
+ const transformedMessages = convertToLlm(messages);
645
+ const summarizationMessages = [
646
+ ...transformedMessages,
647
+ {
648
+ role: "user" as const,
649
+ content: [{ type: "text" as const, text: TURN_PREFIX_SUMMARIZATION_PROMPT }],
650
+ timestamp: Date.now(),
651
+ },
652
+ ];
653
+
654
+ const response = await complete(model, { messages: summarizationMessages }, { maxTokens, signal, apiKey });
655
+
656
+ if (response.stopReason === "error") {
657
+ throw new Error(`Turn prefix summarization failed: ${response.errorMessage || "Unknown error"}`);
658
+ }
659
+
660
+ return response.content
661
+ .filter((c): c is { type: "text"; text: string } => c.type === "text")
662
+ .map((c) => c.text)
663
+ .join("\n");
664
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Compaction and summarization utilities.
3
+ */
4
+
5
+ export * from "./branch-summarization";
6
+ export * from "./compaction";
7
+ export * from "./utils";