@handy_wote/pi-coding-agent 0.81.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (863) hide show
  1. package/CHANGELOG.md +5107 -0
  2. package/README.md +694 -0
  3. package/dist/bun/cli.d.ts +3 -0
  4. package/dist/bun/cli.d.ts.map +1 -0
  5. package/dist/bun/cli.js +11 -0
  6. package/dist/bun/cli.js.map +1 -0
  7. package/dist/bun/register-bedrock.d.ts +2 -0
  8. package/dist/bun/register-bedrock.d.ts.map +1 -0
  9. package/dist/bun/register-bedrock.js +4 -0
  10. package/dist/bun/register-bedrock.js.map +1 -0
  11. package/dist/bun/restore-sandbox-env.d.ts +17 -0
  12. package/dist/bun/restore-sandbox-env.d.ts.map +1 -0
  13. package/dist/bun/restore-sandbox-env.js +36 -0
  14. package/dist/bun/restore-sandbox-env.js.map +1 -0
  15. package/dist/cli/args.d.ts +57 -0
  16. package/dist/cli/args.d.ts.map +1 -0
  17. package/dist/cli/args.js +381 -0
  18. package/dist/cli/args.js.map +1 -0
  19. package/dist/cli/config-selector.d.ts +16 -0
  20. package/dist/cli/config-selector.d.ts.map +1 -0
  21. package/dist/cli/config-selector.js +31 -0
  22. package/dist/cli/config-selector.js.map +1 -0
  23. package/dist/cli/file-processor.d.ts +15 -0
  24. package/dist/cli/file-processor.d.ts.map +1 -0
  25. package/dist/cli/file-processor.js +70 -0
  26. package/dist/cli/file-processor.js.map +1 -0
  27. package/dist/cli/initial-message.d.ts +18 -0
  28. package/dist/cli/initial-message.d.ts.map +1 -0
  29. package/dist/cli/initial-message.js +22 -0
  30. package/dist/cli/initial-message.js.map +1 -0
  31. package/dist/cli/list-models.d.ts +9 -0
  32. package/dist/cli/list-models.d.ts.map +1 -0
  33. package/dist/cli/list-models.js +95 -0
  34. package/dist/cli/list-models.js.map +1 -0
  35. package/dist/cli/project-trust.d.ts +10 -0
  36. package/dist/cli/project-trust.d.ts.map +1 -0
  37. package/dist/cli/project-trust.js +48 -0
  38. package/dist/cli/project-trust.js.map +1 -0
  39. package/dist/cli/session-picker.d.ts +10 -0
  40. package/dist/cli/session-picker.d.ts.map +1 -0
  41. package/dist/cli/session-picker.js +36 -0
  42. package/dist/cli/session-picker.js.map +1 -0
  43. package/dist/cli/startup-ui.d.ts +20 -0
  44. package/dist/cli/startup-ui.d.ts.map +1 -0
  45. package/dist/cli/startup-ui.js +184 -0
  46. package/dist/cli/startup-ui.js.map +1 -0
  47. package/dist/cli.d.ts +3 -0
  48. package/dist/cli.d.ts.map +1 -0
  49. package/dist/cli.js +18 -0
  50. package/dist/cli.js.map +1 -0
  51. package/dist/config.d.ts +98 -0
  52. package/dist/config.d.ts.map +1 -0
  53. package/dist/config.js +459 -0
  54. package/dist/config.js.map +1 -0
  55. package/dist/core/agent-session-runtime.d.ts +119 -0
  56. package/dist/core/agent-session-runtime.d.ts.map +1 -0
  57. package/dist/core/agent-session-runtime.js +306 -0
  58. package/dist/core/agent-session-runtime.js.map +1 -0
  59. package/dist/core/agent-session-services.d.ts +85 -0
  60. package/dist/core/agent-session-services.d.ts.map +1 -0
  61. package/dist/core/agent-session-services.js +133 -0
  62. package/dist/core/agent-session-services.js.map +1 -0
  63. package/dist/core/agent-session.d.ts +642 -0
  64. package/dist/core/agent-session.d.ts.map +1 -0
  65. package/dist/core/agent-session.js +2684 -0
  66. package/dist/core/agent-session.js.map +1 -0
  67. package/dist/core/auth-storage.d.ts +56 -0
  68. package/dist/core/auth-storage.d.ts.map +1 -0
  69. package/dist/core/auth-storage.js +232 -0
  70. package/dist/core/auth-storage.js.map +1 -0
  71. package/dist/core/bash-executor.d.ts +32 -0
  72. package/dist/core/bash-executor.d.ts.map +1 -0
  73. package/dist/core/bash-executor.js +111 -0
  74. package/dist/core/bash-executor.js.map +1 -0
  75. package/dist/core/cache-stats.d.ts +49 -0
  76. package/dist/core/cache-stats.d.ts.map +1 -0
  77. package/dist/core/cache-stats.js +101 -0
  78. package/dist/core/cache-stats.js.map +1 -0
  79. package/dist/core/compaction/branch-summarization.d.ts +98 -0
  80. package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
  81. package/dist/core/compaction/branch-summarization.js +246 -0
  82. package/dist/core/compaction/branch-summarization.js.map +1 -0
  83. package/dist/core/compaction/compaction.d.ts +137 -0
  84. package/dist/core/compaction/compaction.d.ts.map +1 -0
  85. package/dist/core/compaction/compaction.js +637 -0
  86. package/dist/core/compaction/compaction.js.map +1 -0
  87. package/dist/core/compaction/index.d.ts +7 -0
  88. package/dist/core/compaction/index.d.ts.map +1 -0
  89. package/dist/core/compaction/index.js +7 -0
  90. package/dist/core/compaction/index.js.map +1 -0
  91. package/dist/core/compaction/utils.d.ts +38 -0
  92. package/dist/core/compaction/utils.d.ts.map +1 -0
  93. package/dist/core/compaction/utils.js +142 -0
  94. package/dist/core/compaction/utils.js.map +1 -0
  95. package/dist/core/defaults.d.ts +3 -0
  96. package/dist/core/defaults.d.ts.map +1 -0
  97. package/dist/core/defaults.js +2 -0
  98. package/dist/core/defaults.js.map +1 -0
  99. package/dist/core/diagnostics.d.ts +15 -0
  100. package/dist/core/diagnostics.d.ts.map +1 -0
  101. package/dist/core/diagnostics.js +2 -0
  102. package/dist/core/diagnostics.js.map +1 -0
  103. package/dist/core/event-bus.d.ts +9 -0
  104. package/dist/core/event-bus.d.ts.map +1 -0
  105. package/dist/core/event-bus.js +25 -0
  106. package/dist/core/event-bus.js.map +1 -0
  107. package/dist/core/exec.d.ts +29 -0
  108. package/dist/core/exec.d.ts.map +1 -0
  109. package/dist/core/exec.js +75 -0
  110. package/dist/core/exec.js.map +1 -0
  111. package/dist/core/experimental.d.ts +2 -0
  112. package/dist/core/experimental.d.ts.map +1 -0
  113. package/dist/core/experimental.js +4 -0
  114. package/dist/core/experimental.js.map +1 -0
  115. package/dist/core/export-html/ansi-to-html.d.ts +22 -0
  116. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -0
  117. package/dist/core/export-html/ansi-to-html.js +249 -0
  118. package/dist/core/export-html/ansi-to-html.js.map +1 -0
  119. package/dist/core/export-html/index.d.ts +37 -0
  120. package/dist/core/export-html/index.d.ts.map +1 -0
  121. package/dist/core/export-html/index.js +226 -0
  122. package/dist/core/export-html/index.js.map +1 -0
  123. package/dist/core/export-html/template.css +1066 -0
  124. package/dist/core/export-html/template.html +55 -0
  125. package/dist/core/export-html/template.js +1864 -0
  126. package/dist/core/export-html/tool-renderer.d.ts +34 -0
  127. package/dist/core/export-html/tool-renderer.d.ts.map +1 -0
  128. package/dist/core/export-html/tool-renderer.js +108 -0
  129. package/dist/core/export-html/tool-renderer.js.map +1 -0
  130. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  131. package/dist/core/export-html/vendor/marked.min.js +78 -0
  132. package/dist/core/extensions/index.d.ts +12 -0
  133. package/dist/core/extensions/index.d.ts.map +1 -0
  134. package/dist/core/extensions/index.js +9 -0
  135. package/dist/core/extensions/index.js.map +1 -0
  136. package/dist/core/extensions/loader.d.ts +23 -0
  137. package/dist/core/extensions/loader.d.ts.map +1 -0
  138. package/dist/core/extensions/loader.js +586 -0
  139. package/dist/core/extensions/loader.js.map +1 -0
  140. package/dist/core/extensions/runner.d.ts +171 -0
  141. package/dist/core/extensions/runner.d.ts.map +1 -0
  142. package/dist/core/extensions/runner.js +953 -0
  143. package/dist/core/extensions/runner.js.map +1 -0
  144. package/dist/core/extensions/types.d.ts +1259 -0
  145. package/dist/core/extensions/types.d.ts.map +1 -0
  146. package/dist/core/extensions/types.js +45 -0
  147. package/dist/core/extensions/types.js.map +1 -0
  148. package/dist/core/extensions/wrapper.d.ts +20 -0
  149. package/dist/core/extensions/wrapper.d.ts.map +1 -0
  150. package/dist/core/extensions/wrapper.js +41 -0
  151. package/dist/core/extensions/wrapper.js.map +1 -0
  152. package/dist/core/footer-data-provider.d.ts +54 -0
  153. package/dist/core/footer-data-provider.d.ts.map +1 -0
  154. package/dist/core/footer-data-provider.js +338 -0
  155. package/dist/core/footer-data-provider.js.map +1 -0
  156. package/dist/core/http-dispatcher.d.ts +22 -0
  157. package/dist/core/http-dispatcher.d.ts.map +1 -0
  158. package/dist/core/http-dispatcher.js +91 -0
  159. package/dist/core/http-dispatcher.js.map +1 -0
  160. package/dist/core/index.d.ts +13 -0
  161. package/dist/core/index.d.ts.map +1 -0
  162. package/dist/core/index.js +13 -0
  163. package/dist/core/index.js.map +1 -0
  164. package/dist/core/keybindings.d.ts +390 -0
  165. package/dist/core/keybindings.d.ts.map +1 -0
  166. package/dist/core/keybindings.js +296 -0
  167. package/dist/core/keybindings.js.map +1 -0
  168. package/dist/core/messages.d.ts +77 -0
  169. package/dist/core/messages.d.ts.map +1 -0
  170. package/dist/core/messages.js +123 -0
  171. package/dist/core/messages.js.map +1 -0
  172. package/dist/core/model-metadata.d.ts +30 -0
  173. package/dist/core/model-metadata.d.ts.map +1 -0
  174. package/dist/core/model-metadata.js +139 -0
  175. package/dist/core/model-metadata.js.map +1 -0
  176. package/dist/core/model-registry.d.ts +48 -0
  177. package/dist/core/model-registry.d.ts.map +1 -0
  178. package/dist/core/model-registry.js +105 -0
  179. package/dist/core/model-registry.js.map +1 -0
  180. package/dist/core/model-resolver.d.ts +121 -0
  181. package/dist/core/model-resolver.d.ts.map +1 -0
  182. package/dist/core/model-resolver.js +534 -0
  183. package/dist/core/model-resolver.js.map +1 -0
  184. package/dist/core/model-runtime.d.ts +84 -0
  185. package/dist/core/model-runtime.d.ts.map +1 -0
  186. package/dist/core/model-runtime.js +453 -0
  187. package/dist/core/model-runtime.js.map +1 -0
  188. package/dist/core/models-store.d.ts +17 -0
  189. package/dist/core/models-store.d.ts.map +1 -0
  190. package/dist/core/models-store.js +45 -0
  191. package/dist/core/models-store.js.map +1 -0
  192. package/dist/core/output-guard.d.ts +7 -0
  193. package/dist/core/output-guard.d.ts.map +1 -0
  194. package/dist/core/output-guard.js +89 -0
  195. package/dist/core/output-guard.js.map +1 -0
  196. package/dist/core/package-manager.d.ts +210 -0
  197. package/dist/core/package-manager.d.ts.map +1 -0
  198. package/dist/core/package-manager.js +2135 -0
  199. package/dist/core/package-manager.js.map +1 -0
  200. package/dist/core/profile-runtime.d.ts +4 -0
  201. package/dist/core/profile-runtime.d.ts.map +1 -0
  202. package/dist/core/profile-runtime.js +57 -0
  203. package/dist/core/profile-runtime.js.map +1 -0
  204. package/dist/core/profiles-store.d.ts +17 -0
  205. package/dist/core/profiles-store.d.ts.map +1 -0
  206. package/dist/core/profiles-store.js +100 -0
  207. package/dist/core/profiles-store.js.map +1 -0
  208. package/dist/core/profiles-types.d.ts +31 -0
  209. package/dist/core/profiles-types.d.ts.map +1 -0
  210. package/dist/core/profiles-types.js +4 -0
  211. package/dist/core/profiles-types.js.map +1 -0
  212. package/dist/core/project-trust.d.ts +15 -0
  213. package/dist/core/project-trust.d.ts.map +1 -0
  214. package/dist/core/project-trust.js +59 -0
  215. package/dist/core/project-trust.js.map +1 -0
  216. package/dist/core/prompt-templates.d.ts +54 -0
  217. package/dist/core/prompt-templates.d.ts.map +1 -0
  218. package/dist/core/prompt-templates.js +236 -0
  219. package/dist/core/prompt-templates.js.map +1 -0
  220. package/dist/core/provider-attribution.d.ts +4 -0
  221. package/dist/core/provider-attribution.d.ts.map +1 -0
  222. package/dist/core/provider-attribution.js +72 -0
  223. package/dist/core/provider-attribution.js.map +1 -0
  224. package/dist/core/radius.d.ts +2 -0
  225. package/dist/core/radius.d.ts.map +1 -0
  226. package/dist/core/radius.js +2 -0
  227. package/dist/core/radius.js.map +1 -0
  228. package/dist/core/resource-loader.d.ts +206 -0
  229. package/dist/core/resource-loader.d.ts.map +1 -0
  230. package/dist/core/resource-loader.js +817 -0
  231. package/dist/core/resource-loader.js.map +1 -0
  232. package/dist/core/sdk.d.ts +106 -0
  233. package/dist/core/sdk.d.ts.map +1 -0
  234. package/dist/core/sdk.js +269 -0
  235. package/dist/core/sdk.js.map +1 -0
  236. package/dist/core/session-cwd.d.ts +19 -0
  237. package/dist/core/session-cwd.d.ts.map +1 -0
  238. package/dist/core/session-cwd.js +38 -0
  239. package/dist/core/session-cwd.js.map +1 -0
  240. package/dist/core/session-manager.d.ts +356 -0
  241. package/dist/core/session-manager.d.ts.map +1 -0
  242. package/dist/core/session-manager.js +1338 -0
  243. package/dist/core/session-manager.js.map +1 -0
  244. package/dist/core/settings-manager.d.ts +297 -0
  245. package/dist/core/settings-manager.d.ts.map +1 -0
  246. package/dist/core/settings-manager.js +902 -0
  247. package/dist/core/settings-manager.js.map +1 -0
  248. package/dist/core/skills.d.ts +60 -0
  249. package/dist/core/skills.d.ts.map +1 -0
  250. package/dist/core/skills.js +387 -0
  251. package/dist/core/skills.js.map +1 -0
  252. package/dist/core/slash-commands.d.ts +15 -0
  253. package/dist/core/slash-commands.d.ts.map +1 -0
  254. package/dist/core/slash-commands.js +25 -0
  255. package/dist/core/slash-commands.js.map +1 -0
  256. package/dist/core/source-info.d.ts +18 -0
  257. package/dist/core/source-info.d.ts.map +1 -0
  258. package/dist/core/source-info.js +19 -0
  259. package/dist/core/source-info.js.map +1 -0
  260. package/dist/core/system-prompt.d.ts +28 -0
  261. package/dist/core/system-prompt.d.ts.map +1 -0
  262. package/dist/core/system-prompt.js +110 -0
  263. package/dist/core/system-prompt.js.map +1 -0
  264. package/dist/core/telemetry.d.ts +3 -0
  265. package/dist/core/telemetry.d.ts.map +1 -0
  266. package/dist/core/telemetry.js +9 -0
  267. package/dist/core/telemetry.js.map +1 -0
  268. package/dist/core/timings.d.ts +10 -0
  269. package/dist/core/timings.d.ts.map +1 -0
  270. package/dist/core/timings.js +41 -0
  271. package/dist/core/timings.js.map +1 -0
  272. package/dist/core/tools/bash.d.ts +68 -0
  273. package/dist/core/tools/bash.d.ts.map +1 -0
  274. package/dist/core/tools/bash.js +361 -0
  275. package/dist/core/tools/bash.js.map +1 -0
  276. package/dist/core/tools/edit-diff.d.ts +106 -0
  277. package/dist/core/tools/edit-diff.d.ts.map +1 -0
  278. package/dist/core/tools/edit-diff.js +424 -0
  279. package/dist/core/tools/edit-diff.js.map +1 -0
  280. package/dist/core/tools/edit.d.ts +51 -0
  281. package/dist/core/tools/edit.d.ts.map +1 -0
  282. package/dist/core/tools/edit.js +284 -0
  283. package/dist/core/tools/edit.js.map +1 -0
  284. package/dist/core/tools/file-mutation-queue.d.ts +6 -0
  285. package/dist/core/tools/file-mutation-queue.d.ts.map +1 -0
  286. package/dist/core/tools/file-mutation-queue.js +52 -0
  287. package/dist/core/tools/file-mutation-queue.js.map +1 -0
  288. package/dist/core/tools/find.d.ts +35 -0
  289. package/dist/core/tools/find.d.ts.map +1 -0
  290. package/dist/core/tools/find.js +305 -0
  291. package/dist/core/tools/find.js.map +1 -0
  292. package/dist/core/tools/grep.d.ts +37 -0
  293. package/dist/core/tools/grep.d.ts.map +1 -0
  294. package/dist/core/tools/grep.js +304 -0
  295. package/dist/core/tools/grep.js.map +1 -0
  296. package/dist/core/tools/index.d.ts +40 -0
  297. package/dist/core/tools/index.d.ts.map +1 -0
  298. package/dist/core/tools/index.js +112 -0
  299. package/dist/core/tools/index.js.map +1 -0
  300. package/dist/core/tools/ls.d.ts +37 -0
  301. package/dist/core/tools/ls.d.ts.map +1 -0
  302. package/dist/core/tools/ls.js +167 -0
  303. package/dist/core/tools/ls.js.map +1 -0
  304. package/dist/core/tools/output-accumulator.d.ts +52 -0
  305. package/dist/core/tools/output-accumulator.d.ts.map +1 -0
  306. package/dist/core/tools/output-accumulator.js +184 -0
  307. package/dist/core/tools/output-accumulator.js.map +1 -0
  308. package/dist/core/tools/path-utils.d.ts +10 -0
  309. package/dist/core/tools/path-utils.d.ts.map +1 -0
  310. package/dist/core/tools/path-utils.js +99 -0
  311. package/dist/core/tools/path-utils.js.map +1 -0
  312. package/dist/core/tools/read.d.ts +35 -0
  313. package/dist/core/tools/read.d.ts.map +1 -0
  314. package/dist/core/tools/read.js +276 -0
  315. package/dist/core/tools/read.js.map +1 -0
  316. package/dist/core/tools/render-utils.d.ts +24 -0
  317. package/dist/core/tools/render-utils.d.ts.map +1 -0
  318. package/dist/core/tools/render-utils.js +65 -0
  319. package/dist/core/tools/render-utils.js.map +1 -0
  320. package/dist/core/tools/tool-definition-wrapper.d.ts +14 -0
  321. package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -0
  322. package/dist/core/tools/tool-definition-wrapper.js +34 -0
  323. package/dist/core/tools/tool-definition-wrapper.js.map +1 -0
  324. package/dist/core/tools/truncate.d.ts +70 -0
  325. package/dist/core/tools/truncate.d.ts.map +1 -0
  326. package/dist/core/tools/truncate.js +215 -0
  327. package/dist/core/tools/truncate.js.map +1 -0
  328. package/dist/core/tools/write.d.ts +26 -0
  329. package/dist/core/tools/write.d.ts.map +1 -0
  330. package/dist/core/tools/write.js +197 -0
  331. package/dist/core/tools/write.js.map +1 -0
  332. package/dist/core/trust-manager.d.ts +36 -0
  333. package/dist/core/trust-manager.d.ts.map +1 -0
  334. package/dist/core/trust-manager.js +202 -0
  335. package/dist/core/trust-manager.js.map +1 -0
  336. package/dist/core/usage-totals.d.ts +19 -0
  337. package/dist/core/usage-totals.d.ts.map +1 -0
  338. package/dist/core/usage-totals.js +52 -0
  339. package/dist/core/usage-totals.js.map +1 -0
  340. package/dist/extensions/index.d.ts +3 -0
  341. package/dist/extensions/index.d.ts.map +1 -0
  342. package/dist/extensions/index.js +3 -0
  343. package/dist/extensions/index.js.map +1 -0
  344. package/dist/extensions/llama/client.d.ts +61 -0
  345. package/dist/extensions/llama/client.d.ts.map +1 -0
  346. package/dist/extensions/llama/client.js +302 -0
  347. package/dist/extensions/llama/client.js.map +1 -0
  348. package/dist/extensions/llama/huggingface.d.ts +23 -0
  349. package/dist/extensions/llama/huggingface.d.ts.map +1 -0
  350. package/dist/extensions/llama/huggingface.js +141 -0
  351. package/dist/extensions/llama/huggingface.js.map +1 -0
  352. package/dist/extensions/llama/index.d.ts +3 -0
  353. package/dist/extensions/llama/index.d.ts.map +1 -0
  354. package/dist/extensions/llama/index.js +208 -0
  355. package/dist/extensions/llama/index.js.map +1 -0
  356. package/dist/extensions/llama/provider.d.ts +10 -0
  357. package/dist/extensions/llama/provider.d.ts.map +1 -0
  358. package/dist/extensions/llama/provider.js +102 -0
  359. package/dist/extensions/llama/provider.js.map +1 -0
  360. package/dist/extensions/llama/ui.d.ts +42 -0
  361. package/dist/extensions/llama/ui.d.ts.map +1 -0
  362. package/dist/extensions/llama/ui.js +416 -0
  363. package/dist/extensions/llama/ui.js.map +1 -0
  364. package/dist/index.d.ts +35 -0
  365. package/dist/index.d.ts.map +1 -0
  366. package/dist/index.js +47 -0
  367. package/dist/index.js.map +1 -0
  368. package/dist/main.d.ts +12 -0
  369. package/dist/main.d.ts.map +1 -0
  370. package/dist/main.js +706 -0
  371. package/dist/main.js.map +1 -0
  372. package/dist/migrations.d.ts +33 -0
  373. package/dist/migrations.d.ts.map +1 -0
  374. package/dist/migrations.js +281 -0
  375. package/dist/migrations.js.map +1 -0
  376. package/dist/modes/index.d.ts +9 -0
  377. package/dist/modes/index.d.ts.map +1 -0
  378. package/dist/modes/index.js +8 -0
  379. package/dist/modes/index.js.map +1 -0
  380. package/dist/modes/interactive/assets/clankolas.png +0 -0
  381. package/dist/modes/interactive/components/armin.d.ts +34 -0
  382. package/dist/modes/interactive/components/armin.d.ts.map +1 -0
  383. package/dist/modes/interactive/components/armin.js +333 -0
  384. package/dist/modes/interactive/components/armin.js.map +1 -0
  385. package/dist/modes/interactive/components/assistant-message.d.ts +22 -0
  386. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
  387. package/dist/modes/interactive/components/assistant-message.js +141 -0
  388. package/dist/modes/interactive/components/assistant-message.js.map +1 -0
  389. package/dist/modes/interactive/components/bash-execution.d.ts +34 -0
  390. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -0
  391. package/dist/modes/interactive/components/bash-execution.js +175 -0
  392. package/dist/modes/interactive/components/bash-execution.js.map +1 -0
  393. package/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
  394. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
  395. package/dist/modes/interactive/components/bordered-loader.js +54 -0
  396. package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
  397. package/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
  398. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
  399. package/dist/modes/interactive/components/branch-summary-message.js +44 -0
  400. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
  401. package/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
  402. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
  403. package/dist/modes/interactive/components/compaction-summary-message.js +45 -0
  404. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -0
  405. package/dist/modes/interactive/components/config-selector.d.ts +102 -0
  406. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
  407. package/dist/modes/interactive/components/config-selector.js +761 -0
  408. package/dist/modes/interactive/components/config-selector.js.map +1 -0
  409. package/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
  410. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -0
  411. package/dist/modes/interactive/components/countdown-timer.js +33 -0
  412. package/dist/modes/interactive/components/countdown-timer.js.map +1 -0
  413. package/dist/modes/interactive/components/custom-editor.d.ts +21 -0
  414. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -0
  415. package/dist/modes/interactive/components/custom-editor.js +70 -0
  416. package/dist/modes/interactive/components/custom-editor.js.map +1 -0
  417. package/dist/modes/interactive/components/custom-entry.d.ts +19 -0
  418. package/dist/modes/interactive/components/custom-entry.d.ts.map +1 -0
  419. package/dist/modes/interactive/components/custom-entry.js +52 -0
  420. package/dist/modes/interactive/components/custom-entry.js.map +1 -0
  421. package/dist/modes/interactive/components/custom-message.d.ts +20 -0
  422. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
  423. package/dist/modes/interactive/components/custom-message.js +79 -0
  424. package/dist/modes/interactive/components/custom-message.js.map +1 -0
  425. package/dist/modes/interactive/components/daxnuts.d.ts +23 -0
  426. package/dist/modes/interactive/components/daxnuts.d.ts.map +1 -0
  427. package/dist/modes/interactive/components/daxnuts.js +140 -0
  428. package/dist/modes/interactive/components/daxnuts.js.map +1 -0
  429. package/dist/modes/interactive/components/diff.d.ts +12 -0
  430. package/dist/modes/interactive/components/diff.d.ts.map +1 -0
  431. package/dist/modes/interactive/components/diff.js +133 -0
  432. package/dist/modes/interactive/components/diff.js.map +1 -0
  433. package/dist/modes/interactive/components/dynamic-border.d.ts +15 -0
  434. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -0
  435. package/dist/modes/interactive/components/dynamic-border.js +21 -0
  436. package/dist/modes/interactive/components/dynamic-border.js.map +1 -0
  437. package/dist/modes/interactive/components/earendil-announcement.d.ts +5 -0
  438. package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -0
  439. package/dist/modes/interactive/components/earendil-announcement.js +40 -0
  440. package/dist/modes/interactive/components/earendil-announcement.js.map +1 -0
  441. package/dist/modes/interactive/components/entity-list-theme.d.ts +3 -0
  442. package/dist/modes/interactive/components/entity-list-theme.d.ts.map +1 -0
  443. package/dist/modes/interactive/components/entity-list-theme.js +17 -0
  444. package/dist/modes/interactive/components/entity-list-theme.js.map +1 -0
  445. package/dist/modes/interactive/components/extension-editor.d.ts +22 -0
  446. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
  447. package/dist/modes/interactive/components/extension-editor.js +128 -0
  448. package/dist/modes/interactive/components/extension-editor.js.map +1 -0
  449. package/dist/modes/interactive/components/extension-input.d.ts +23 -0
  450. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
  451. package/dist/modes/interactive/components/extension-input.js +61 -0
  452. package/dist/modes/interactive/components/extension-input.js.map +1 -0
  453. package/dist/modes/interactive/components/extension-selector.d.ts +22 -0
  454. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -0
  455. package/dist/modes/interactive/components/extension-selector.js +61 -0
  456. package/dist/modes/interactive/components/extension-selector.js.map +1 -0
  457. package/dist/modes/interactive/components/first-time-setup.d.ts +24 -0
  458. package/dist/modes/interactive/components/first-time-setup.d.ts.map +1 -0
  459. package/dist/modes/interactive/components/first-time-setup.js +92 -0
  460. package/dist/modes/interactive/components/first-time-setup.js.map +1 -0
  461. package/dist/modes/interactive/components/footer.d.ts +32 -0
  462. package/dist/modes/interactive/components/footer.d.ts.map +1 -0
  463. package/dist/modes/interactive/components/footer.js +222 -0
  464. package/dist/modes/interactive/components/footer.js.map +1 -0
  465. package/dist/modes/interactive/components/index.d.ts +32 -0
  466. package/dist/modes/interactive/components/index.d.ts.map +1 -0
  467. package/dist/modes/interactive/components/index.js +33 -0
  468. package/dist/modes/interactive/components/index.js.map +1 -0
  469. package/dist/modes/interactive/components/keybinding-hints.d.ts +13 -0
  470. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -0
  471. package/dist/modes/interactive/components/keybinding-hints.js +36 -0
  472. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -0
  473. package/dist/modes/interactive/components/model-selector.d.ts +50 -0
  474. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
  475. package/dist/modes/interactive/components/model-selector.js +240 -0
  476. package/dist/modes/interactive/components/model-selector.js.map +1 -0
  477. package/dist/modes/interactive/components/scoped-models-selector.d.ts +36 -0
  478. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
  479. package/dist/modes/interactive/components/scoped-models-selector.js +222 -0
  480. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
  481. package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
  482. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
  483. package/dist/modes/interactive/components/session-selector-search.js +155 -0
  484. package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
  485. package/dist/modes/interactive/components/session-selector.d.ts +95 -0
  486. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
  487. package/dist/modes/interactive/components/session-selector.js +779 -0
  488. package/dist/modes/interactive/components/session-selector.js.map +1 -0
  489. package/dist/modes/interactive/components/settings-selector.d.ts +77 -0
  490. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
  491. package/dist/modes/interactive/components/settings-selector.js +593 -0
  492. package/dist/modes/interactive/components/settings-selector.js.map +1 -0
  493. package/dist/modes/interactive/components/show-images-selector.d.ts +10 -0
  494. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -0
  495. package/dist/modes/interactive/components/show-images-selector.js +39 -0
  496. package/dist/modes/interactive/components/show-images-selector.js.map +1 -0
  497. package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
  498. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
  499. package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
  500. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
  501. package/dist/modes/interactive/components/status-indicator.d.ts +28 -0
  502. package/dist/modes/interactive/components/status-indicator.d.ts.map +1 -0
  503. package/dist/modes/interactive/components/status-indicator.js +60 -0
  504. package/dist/modes/interactive/components/status-indicator.js.map +1 -0
  505. package/dist/modes/interactive/components/theme-selector.d.ts +11 -0
  506. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -0
  507. package/dist/modes/interactive/components/theme-selector.js +46 -0
  508. package/dist/modes/interactive/components/theme-selector.js.map +1 -0
  509. package/dist/modes/interactive/components/thinking-selector.d.ts +11 -0
  510. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -0
  511. package/dist/modes/interactive/components/thinking-selector.js +47 -0
  512. package/dist/modes/interactive/components/thinking-selector.js.map +1 -0
  513. package/dist/modes/interactive/components/tool-execution.d.ts +63 -0
  514. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -0
  515. package/dist/modes/interactive/components/tool-execution.js +317 -0
  516. package/dist/modes/interactive/components/tool-execution.js.map +1 -0
  517. package/dist/modes/interactive/components/tree-selector.d.ts +94 -0
  518. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
  519. package/dist/modes/interactive/components/tree-selector.js +1245 -0
  520. package/dist/modes/interactive/components/tree-selector.js.map +1 -0
  521. package/dist/modes/interactive/components/trust-selector.d.ts +21 -0
  522. package/dist/modes/interactive/components/trust-selector.d.ts.map +1 -0
  523. package/dist/modes/interactive/components/trust-selector.js +72 -0
  524. package/dist/modes/interactive/components/trust-selector.js.map +1 -0
  525. package/dist/modes/interactive/components/user-message-selector.d.ts +16 -0
  526. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -0
  527. package/dist/modes/interactive/components/user-message-selector.js +53 -0
  528. package/dist/modes/interactive/components/user-message-selector.js.map +1 -0
  529. package/dist/modes/interactive/components/user-message.d.ts +14 -0
  530. package/dist/modes/interactive/components/user-message.d.ts.map +1 -0
  531. package/dist/modes/interactive/components/user-message.js +42 -0
  532. package/dist/modes/interactive/components/user-message.js.map +1 -0
  533. package/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
  534. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -0
  535. package/dist/modes/interactive/components/visual-truncate.js +33 -0
  536. package/dist/modes/interactive/components/visual-truncate.js.map +1 -0
  537. package/dist/modes/interactive/interactive-mode.d.ts +391 -0
  538. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
  539. package/dist/modes/interactive/interactive-mode.js +4996 -0
  540. package/dist/modes/interactive/interactive-mode.js.map +1 -0
  541. package/dist/modes/interactive/model-search.d.ts +12 -0
  542. package/dist/modes/interactive/model-search.d.ts.map +1 -0
  543. package/dist/modes/interactive/model-search.js +15 -0
  544. package/dist/modes/interactive/model-search.js.map +1 -0
  545. package/dist/modes/interactive/theme/dark.json +87 -0
  546. package/dist/modes/interactive/theme/light.json +86 -0
  547. package/dist/modes/interactive/theme/theme-controller.d.ts +29 -0
  548. package/dist/modes/interactive/theme/theme-controller.d.ts.map +1 -0
  549. package/dist/modes/interactive/theme/theme-controller.js +102 -0
  550. package/dist/modes/interactive/theme/theme-controller.js.map +1 -0
  551. package/dist/modes/interactive/theme/theme-schema.json +340 -0
  552. package/dist/modes/interactive/theme/theme.d.ts +120 -0
  553. package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
  554. package/dist/modes/interactive/theme/theme.js +1063 -0
  555. package/dist/modes/interactive/theme/theme.js.map +1 -0
  556. package/dist/modes/print-mode.d.ts +28 -0
  557. package/dist/modes/print-mode.d.ts.map +1 -0
  558. package/dist/modes/print-mode.js +132 -0
  559. package/dist/modes/print-mode.js.map +1 -0
  560. package/dist/modes/rpc/jsonl.d.ts +17 -0
  561. package/dist/modes/rpc/jsonl.d.ts.map +1 -0
  562. package/dist/modes/rpc/jsonl.js +49 -0
  563. package/dist/modes/rpc/jsonl.js.map +1 -0
  564. package/dist/modes/rpc/rpc-client.d.ts +246 -0
  565. package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
  566. package/dist/modes/rpc/rpc-client.js +488 -0
  567. package/dist/modes/rpc/rpc-client.js.map +1 -0
  568. package/dist/modes/rpc/rpc-mode.d.ts +20 -0
  569. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -0
  570. package/dist/modes/rpc/rpc-mode.js +639 -0
  571. package/dist/modes/rpc/rpc-mode.js.map +1 -0
  572. package/dist/modes/rpc/rpc-types.d.ts +457 -0
  573. package/dist/modes/rpc/rpc-types.d.ts.map +1 -0
  574. package/dist/modes/rpc/rpc-types.js +8 -0
  575. package/dist/modes/rpc/rpc-types.js.map +1 -0
  576. package/dist/package-manager-cli.d.ts +8 -0
  577. package/dist/package-manager-cli.d.ts.map +1 -0
  578. package/dist/package-manager-cli.js +775 -0
  579. package/dist/package-manager-cli.js.map +1 -0
  580. package/dist/rpc-entry.d.ts +3 -0
  581. package/dist/rpc-entry.d.ts.map +1 -0
  582. package/dist/rpc-entry.js +10 -0
  583. package/dist/rpc-entry.js.map +1 -0
  584. package/dist/utils/ansi.d.ts +2 -0
  585. package/dist/utils/ansi.d.ts.map +1 -0
  586. package/dist/utils/ansi.js +52 -0
  587. package/dist/utils/ansi.js.map +1 -0
  588. package/dist/utils/changelog.d.ts +22 -0
  589. package/dist/utils/changelog.d.ts.map +1 -0
  590. package/dist/utils/changelog.js +165 -0
  591. package/dist/utils/changelog.js.map +1 -0
  592. package/dist/utils/child-process.d.ts +18 -0
  593. package/dist/utils/child-process.d.ts.map +1 -0
  594. package/dist/utils/child-process.js +106 -0
  595. package/dist/utils/child-process.js.map +1 -0
  596. package/dist/utils/clipboard-image.d.ts +11 -0
  597. package/dist/utils/clipboard-image.d.ts.map +1 -0
  598. package/dist/utils/clipboard-image.js +245 -0
  599. package/dist/utils/clipboard-image.js.map +1 -0
  600. package/dist/utils/clipboard-native.d.ts +11 -0
  601. package/dist/utils/clipboard-native.d.ts.map +1 -0
  602. package/dist/utils/clipboard-native.js +20 -0
  603. package/dist/utils/clipboard-native.js.map +1 -0
  604. package/dist/utils/clipboard.d.ts +4 -0
  605. package/dist/utils/clipboard.d.ts.map +1 -0
  606. package/dist/utils/clipboard.js +130 -0
  607. package/dist/utils/clipboard.js.map +1 -0
  608. package/dist/utils/deprecation.d.ts +4 -0
  609. package/dist/utils/deprecation.d.ts.map +1 -0
  610. package/dist/utils/deprecation.js +13 -0
  611. package/dist/utils/deprecation.js.map +1 -0
  612. package/dist/utils/exif-orientation.d.ts +5 -0
  613. package/dist/utils/exif-orientation.d.ts.map +1 -0
  614. package/dist/utils/exif-orientation.js +158 -0
  615. package/dist/utils/exif-orientation.js.map +1 -0
  616. package/dist/utils/frontmatter.d.ts +8 -0
  617. package/dist/utils/frontmatter.d.ts.map +1 -0
  618. package/dist/utils/frontmatter.js +26 -0
  619. package/dist/utils/frontmatter.js.map +1 -0
  620. package/dist/utils/fs-watch.d.ts +5 -0
  621. package/dist/utils/fs-watch.d.ts.map +1 -0
  622. package/dist/utils/fs-watch.js +25 -0
  623. package/dist/utils/fs-watch.js.map +1 -0
  624. package/dist/utils/git.d.ts +26 -0
  625. package/dist/utils/git.d.ts.map +1 -0
  626. package/dist/utils/git.js +195 -0
  627. package/dist/utils/git.js.map +1 -0
  628. package/dist/utils/html.d.ts +7 -0
  629. package/dist/utils/html.d.ts.map +1 -0
  630. package/dist/utils/html.js +40 -0
  631. package/dist/utils/html.js.map +1 -0
  632. package/dist/utils/image-convert.d.ts +10 -0
  633. package/dist/utils/image-convert.d.ts.map +1 -0
  634. package/dist/utils/image-convert.js +45 -0
  635. package/dist/utils/image-convert.js.map +1 -0
  636. package/dist/utils/image-process.d.ts +18 -0
  637. package/dist/utils/image-process.d.ts.map +1 -0
  638. package/dist/utils/image-process.js +83 -0
  639. package/dist/utils/image-process.js.map +1 -0
  640. package/dist/utils/image-resize-core.d.ts +30 -0
  641. package/dist/utils/image-resize-core.d.ts.map +1 -0
  642. package/dist/utils/image-resize-core.js +124 -0
  643. package/dist/utils/image-resize-core.js.map +1 -0
  644. package/dist/utils/image-resize-worker.d.ts +2 -0
  645. package/dist/utils/image-resize-worker.d.ts.map +1 -0
  646. package/dist/utils/image-resize-worker.js +31 -0
  647. package/dist/utils/image-resize-worker.js.map +1 -0
  648. package/dist/utils/image-resize.d.ts +16 -0
  649. package/dist/utils/image-resize.d.ts.map +1 -0
  650. package/dist/utils/image-resize.js +97 -0
  651. package/dist/utils/image-resize.js.map +1 -0
  652. package/dist/utils/json.d.ts +3 -0
  653. package/dist/utils/json.d.ts.map +1 -0
  654. package/dist/utils/json.js +7 -0
  655. package/dist/utils/json.js.map +1 -0
  656. package/dist/utils/mime.d.ts +3 -0
  657. package/dist/utils/mime.d.ts.map +1 -0
  658. package/dist/utils/mime.js +110 -0
  659. package/dist/utils/mime.js.map +1 -0
  660. package/dist/utils/open-browser.d.ts +9 -0
  661. package/dist/utils/open-browser.d.ts.map +1 -0
  662. package/dist/utils/open-browser.js +22 -0
  663. package/dist/utils/open-browser.js.map +1 -0
  664. package/dist/utils/paths.d.ts +31 -0
  665. package/dist/utils/paths.d.ts.map +1 -0
  666. package/dist/utils/paths.js +92 -0
  667. package/dist/utils/paths.js.map +1 -0
  668. package/dist/utils/photon.d.ts +21 -0
  669. package/dist/utils/photon.d.ts.map +1 -0
  670. package/dist/utils/photon.js +121 -0
  671. package/dist/utils/photon.js.map +1 -0
  672. package/dist/utils/pi-user-agent.d.ts +2 -0
  673. package/dist/utils/pi-user-agent.d.ts.map +1 -0
  674. package/dist/utils/pi-user-agent.js +5 -0
  675. package/dist/utils/pi-user-agent.js.map +1 -0
  676. package/dist/utils/shell.d.ts +31 -0
  677. package/dist/utils/shell.d.ts.map +1 -0
  678. package/dist/utils/shell.js +202 -0
  679. package/dist/utils/shell.js.map +1 -0
  680. package/dist/utils/sleep.d.ts +5 -0
  681. package/dist/utils/sleep.d.ts.map +1 -0
  682. package/dist/utils/sleep.js +17 -0
  683. package/dist/utils/sleep.js.map +1 -0
  684. package/dist/utils/syntax-highlight.d.ts +12 -0
  685. package/dist/utils/syntax-highlight.d.ts.map +1 -0
  686. package/dist/utils/syntax-highlight.js +118 -0
  687. package/dist/utils/syntax-highlight.js.map +1 -0
  688. package/dist/utils/tools-manager.d.ts +3 -0
  689. package/dist/utils/tools-manager.d.ts.map +1 -0
  690. package/dist/utils/tools-manager.js +328 -0
  691. package/dist/utils/tools-manager.js.map +1 -0
  692. package/dist/utils/version-check.d.ts +15 -0
  693. package/dist/utils/version-check.d.ts.map +1 -0
  694. package/dist/utils/version-check.js +59 -0
  695. package/dist/utils/version-check.js.map +1 -0
  696. package/dist/utils/windows-self-update.d.ts +3 -0
  697. package/dist/utils/windows-self-update.d.ts.map +1 -0
  698. package/dist/utils/windows-self-update.js +77 -0
  699. package/dist/utils/windows-self-update.js.map +1 -0
  700. package/docs/compaction.md +401 -0
  701. package/docs/containerization.md +111 -0
  702. package/docs/custom-provider.md +763 -0
  703. package/docs/development.md +71 -0
  704. package/docs/docs.json +156 -0
  705. package/docs/extensions.md +2953 -0
  706. package/docs/images/doom-extension.png +0 -0
  707. package/docs/images/exy.png +0 -0
  708. package/docs/images/interactive-mode.png +0 -0
  709. package/docs/images/tree-view.png +0 -0
  710. package/docs/index.md +83 -0
  711. package/docs/json.md +86 -0
  712. package/docs/keybindings.md +198 -0
  713. package/docs/llama-cpp.md +99 -0
  714. package/docs/models.md +540 -0
  715. package/docs/packages.md +228 -0
  716. package/docs/prompt-templates.md +96 -0
  717. package/docs/providers.md +301 -0
  718. package/docs/quickstart.md +165 -0
  719. package/docs/rpc.md +1559 -0
  720. package/docs/sdk.md +1186 -0
  721. package/docs/security.md +59 -0
  722. package/docs/session-format.md +436 -0
  723. package/docs/sessions.md +145 -0
  724. package/docs/settings.md +319 -0
  725. package/docs/shell-aliases.md +13 -0
  726. package/docs/skills.md +231 -0
  727. package/docs/terminal-setup.md +142 -0
  728. package/docs/termux.md +127 -0
  729. package/docs/themes.md +297 -0
  730. package/docs/tmux.md +63 -0
  731. package/docs/tui.md +942 -0
  732. package/docs/usage.md +311 -0
  733. package/docs/windows.md +17 -0
  734. package/examples/README.md +25 -0
  735. package/examples/extensions/README.md +213 -0
  736. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  737. package/examples/extensions/bash-spawn-hook.ts +30 -0
  738. package/examples/extensions/bookmark.ts +50 -0
  739. package/examples/extensions/border-status-editor.ts +150 -0
  740. package/examples/extensions/built-in-tool-renderer.ts +249 -0
  741. package/examples/extensions/claude-rules.ts +86 -0
  742. package/examples/extensions/commands.ts +72 -0
  743. package/examples/extensions/confirm-destructive.ts +59 -0
  744. package/examples/extensions/custom-compaction.ts +129 -0
  745. package/examples/extensions/custom-footer.ts +64 -0
  746. package/examples/extensions/custom-header.ts +73 -0
  747. package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
  748. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  749. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  750. package/examples/extensions/custom-provider-gitlab-duo/index.ts +404 -0
  751. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  752. package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
  753. package/examples/extensions/dirty-repo-guard.ts +56 -0
  754. package/examples/extensions/doom-overlay/README.md +46 -0
  755. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  756. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  757. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  758. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  759. package/examples/extensions/doom-overlay/doom-component.ts +132 -0
  760. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  761. package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
  762. package/examples/extensions/doom-overlay/index.ts +74 -0
  763. package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
  764. package/examples/extensions/dynamic-resources/SKILL.md +8 -0
  765. package/examples/extensions/dynamic-resources/dynamic.json +79 -0
  766. package/examples/extensions/dynamic-resources/dynamic.md +5 -0
  767. package/examples/extensions/dynamic-resources/index.ts +15 -0
  768. package/examples/extensions/dynamic-tools.ts +74 -0
  769. package/examples/extensions/entry-renderer.ts +41 -0
  770. package/examples/extensions/event-bus.ts +43 -0
  771. package/examples/extensions/file-trigger.ts +41 -0
  772. package/examples/extensions/git-checkpoint.ts +53 -0
  773. package/examples/extensions/git-merge-and-resolve.ts +115 -0
  774. package/examples/extensions/github-issue-autocomplete.ts +185 -0
  775. package/examples/extensions/gondolin/index.ts +531 -0
  776. package/examples/extensions/gondolin/package-lock.json +185 -0
  777. package/examples/extensions/gondolin/package.json +19 -0
  778. package/examples/extensions/handoff.ts +191 -0
  779. package/examples/extensions/hello.ts +26 -0
  780. package/examples/extensions/hidden-thinking-label.ts +53 -0
  781. package/examples/extensions/inline-bash.ts +94 -0
  782. package/examples/extensions/input-transform-streaming.ts +39 -0
  783. package/examples/extensions/input-transform.ts +43 -0
  784. package/examples/extensions/interactive-shell.ts +196 -0
  785. package/examples/extensions/kimi-deferred-tools.ts +61 -0
  786. package/examples/extensions/mac-system-theme.ts +47 -0
  787. package/examples/extensions/message-renderer.ts +59 -0
  788. package/examples/extensions/minimal-mode.ts +426 -0
  789. package/examples/extensions/modal-editor.ts +85 -0
  790. package/examples/extensions/model-status.ts +31 -0
  791. package/examples/extensions/notify.ts +55 -0
  792. package/examples/extensions/overlay-qa-tests.ts +1450 -0
  793. package/examples/extensions/overlay-test.ts +153 -0
  794. package/examples/extensions/permission-gate.ts +34 -0
  795. package/examples/extensions/pirate.ts +47 -0
  796. package/examples/extensions/plan-mode/README.md +66 -0
  797. package/examples/extensions/plan-mode/index.ts +390 -0
  798. package/examples/extensions/plan-mode/utils.ts +168 -0
  799. package/examples/extensions/preset.ts +436 -0
  800. package/examples/extensions/project-trust.ts +64 -0
  801. package/examples/extensions/prompt-customizer.ts +97 -0
  802. package/examples/extensions/protected-paths.ts +30 -0
  803. package/examples/extensions/provider-payload.ts +18 -0
  804. package/examples/extensions/qna.ts +122 -0
  805. package/examples/extensions/question.ts +278 -0
  806. package/examples/extensions/questionnaire.ts +440 -0
  807. package/examples/extensions/rainbow-editor.ts +88 -0
  808. package/examples/extensions/reload-runtime.ts +37 -0
  809. package/examples/extensions/rpc-demo.ts +118 -0
  810. package/examples/extensions/sandbox/index.ts +321 -0
  811. package/examples/extensions/sandbox/package-lock.json +92 -0
  812. package/examples/extensions/sandbox/package.json +19 -0
  813. package/examples/extensions/send-user-message.ts +97 -0
  814. package/examples/extensions/session-name.ts +27 -0
  815. package/examples/extensions/shutdown-command.ts +63 -0
  816. package/examples/extensions/snake.ts +343 -0
  817. package/examples/extensions/space-invaders.ts +560 -0
  818. package/examples/extensions/ssh.ts +220 -0
  819. package/examples/extensions/status-line.ts +32 -0
  820. package/examples/extensions/structured-output.ts +65 -0
  821. package/examples/extensions/subagent/README.md +175 -0
  822. package/examples/extensions/subagent/agents/planner.md +37 -0
  823. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  824. package/examples/extensions/subagent/agents/scout.md +50 -0
  825. package/examples/extensions/subagent/agents/worker.md +24 -0
  826. package/examples/extensions/subagent/agents.ts +126 -0
  827. package/examples/extensions/subagent/index.ts +1015 -0
  828. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  829. package/examples/extensions/subagent/prompts/implement.md +10 -0
  830. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  831. package/examples/extensions/summarize.ts +207 -0
  832. package/examples/extensions/system-prompt-header.ts +17 -0
  833. package/examples/extensions/tic-tac-toe.ts +1008 -0
  834. package/examples/extensions/timed-confirm.ts +70 -0
  835. package/examples/extensions/titlebar-spinner.ts +58 -0
  836. package/examples/extensions/todo.ts +297 -0
  837. package/examples/extensions/tool-override.ts +144 -0
  838. package/examples/extensions/tools.ts +146 -0
  839. package/examples/extensions/trigger-compact.ts +50 -0
  840. package/examples/extensions/truncated-tool.ts +195 -0
  841. package/examples/extensions/widget-placement.ts +9 -0
  842. package/examples/extensions/with-deps/index.ts +32 -0
  843. package/examples/extensions/with-deps/package-lock.json +31 -0
  844. package/examples/extensions/with-deps/package.json +22 -0
  845. package/examples/extensions/working-indicator.ts +123 -0
  846. package/examples/extensions/working-message-test.ts +25 -0
  847. package/examples/rpc-extension-ui.ts +632 -0
  848. package/examples/sdk/01-minimal.ts +26 -0
  849. package/examples/sdk/02-custom-model.ts +50 -0
  850. package/examples/sdk/03-custom-prompt.ts +70 -0
  851. package/examples/sdk/04-skills.ts +55 -0
  852. package/examples/sdk/05-tools.ts +48 -0
  853. package/examples/sdk/06-extensions.ts +94 -0
  854. package/examples/sdk/07-context-files.ts +42 -0
  855. package/examples/sdk/08-prompt-templates.ts +51 -0
  856. package/examples/sdk/09-api-keys-and-oauth.ts +34 -0
  857. package/examples/sdk/10-settings.ts +53 -0
  858. package/examples/sdk/11-sessions.ts +52 -0
  859. package/examples/sdk/12-full-control.ts +72 -0
  860. package/examples/sdk/13-session-runtime.ts +67 -0
  861. package/examples/sdk/README.md +140 -0
  862. package/npm-shrinkwrap.json +1824 -0
  863. package/package.json +100 -0
@@ -0,0 +1,1338 @@
1
+ import { uuidv7 } from "@handy_wote/pi-ai";
2
+ import { randomUUID } from "crypto";
3
+ import { appendFileSync, closeSync, createReadStream, existsSync, mkdirSync, openSync, readdirSync, readSync, statSync, writeFileSync, } from "fs";
4
+ import { readdir, stat } from "fs/promises";
5
+ import { join, resolve } from "path";
6
+ import { createInterface } from "readline";
7
+ import { StringDecoder } from "string_decoder";
8
+ import { getAgentDir as getDefaultAgentDir, getSessionsDir } from "../config.js";
9
+ import { normalizePath, resolvePath } from "../utils/paths.js";
10
+ import { createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage, } from "./messages.js";
11
+ export const CURRENT_SESSION_VERSION = 3;
12
+ function createSessionId() {
13
+ return uuidv7();
14
+ }
15
+ export function assertValidSessionId(id) {
16
+ if (!/^[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$/.test(id)) {
17
+ throw new Error("Session id must be non-empty, contain only alphanumeric characters, '-', '_', and '.', and start and end with an alphanumeric character");
18
+ }
19
+ }
20
+ /** Generate a unique short ID (8 hex chars, collision-checked) */
21
+ function generateId(byId) {
22
+ for (let i = 0; i < 100; i++) {
23
+ const id = randomUUID().slice(0, 8);
24
+ if (!byId.has(id))
25
+ return id;
26
+ }
27
+ // Fallback to full UUID if somehow we have collisions
28
+ return randomUUID();
29
+ }
30
+ /** Migrate v1 → v2: add id/parentId tree structure. Mutates in place. */
31
+ function migrateV1ToV2(entries) {
32
+ const ids = new Set();
33
+ let prevId = null;
34
+ for (const entry of entries) {
35
+ if (entry.type === "session") {
36
+ entry.version = 2;
37
+ continue;
38
+ }
39
+ entry.id = generateId(ids);
40
+ entry.parentId = prevId;
41
+ prevId = entry.id;
42
+ // Convert firstKeptEntryIndex to firstKeptEntryId for compaction
43
+ if (entry.type === "compaction") {
44
+ const comp = entry;
45
+ if (typeof comp.firstKeptEntryIndex === "number") {
46
+ const targetEntry = entries[comp.firstKeptEntryIndex];
47
+ if (targetEntry && targetEntry.type !== "session") {
48
+ comp.firstKeptEntryId = targetEntry.id;
49
+ }
50
+ delete comp.firstKeptEntryIndex;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ /** Migrate v2 → v3: rename hookMessage role to custom. Mutates in place. */
56
+ function migrateV2ToV3(entries) {
57
+ for (const entry of entries) {
58
+ if (entry.type === "session") {
59
+ entry.version = 3;
60
+ continue;
61
+ }
62
+ // Update message entries with hookMessage role
63
+ if (entry.type === "message") {
64
+ const msgEntry = entry;
65
+ if (msgEntry.message && msgEntry.message.role === "hookMessage") {
66
+ msgEntry.message.role = "custom";
67
+ }
68
+ }
69
+ }
70
+ }
71
+ /**
72
+ * Run all necessary migrations to bring entries to current version.
73
+ * Mutates entries in place. Returns true if any migration was applied.
74
+ */
75
+ function migrateToCurrentVersion(entries) {
76
+ const header = entries.find((e) => e.type === "session");
77
+ const version = header?.version ?? 1;
78
+ if (version >= CURRENT_SESSION_VERSION)
79
+ return false;
80
+ if (version < 2)
81
+ migrateV1ToV2(entries);
82
+ if (version < 3)
83
+ migrateV2ToV3(entries);
84
+ return true;
85
+ }
86
+ /** Exported for testing */
87
+ export function migrateSessionEntries(entries) {
88
+ migrateToCurrentVersion(entries);
89
+ }
90
+ /** Exported for compaction.test.ts */
91
+ export function parseSessionEntries(content) {
92
+ const entries = [];
93
+ const lines = content.trim().split("\n");
94
+ for (const line of lines) {
95
+ if (!line.trim())
96
+ continue;
97
+ try {
98
+ const entry = JSON.parse(line);
99
+ entries.push(entry);
100
+ }
101
+ catch {
102
+ // Skip malformed lines
103
+ }
104
+ }
105
+ return entries;
106
+ }
107
+ export function getLatestCompactionEntry(entries) {
108
+ for (let i = entries.length - 1; i >= 0; i--) {
109
+ if (entries[i].type === "compaction") {
110
+ return entries[i];
111
+ }
112
+ }
113
+ return null;
114
+ }
115
+ function buildEntryIndex(entries, byId) {
116
+ if (byId)
117
+ return byId;
118
+ const index = new Map();
119
+ for (const entry of entries) {
120
+ index.set(entry.id, entry);
121
+ }
122
+ return index;
123
+ }
124
+ function buildSessionPath(entries, leafId, byId) {
125
+ const index = buildEntryIndex(entries, byId);
126
+ let leaf;
127
+ if (leafId === null) {
128
+ return [];
129
+ }
130
+ if (leafId) {
131
+ leaf = index.get(leafId);
132
+ }
133
+ leaf ??= entries[entries.length - 1];
134
+ if (!leaf) {
135
+ return [];
136
+ }
137
+ const path = [];
138
+ let current = leaf;
139
+ while (current) {
140
+ path.push(current);
141
+ current = current.parentId ? index.get(current.parentId) : undefined;
142
+ }
143
+ path.reverse();
144
+ return path;
145
+ }
146
+ function getSessionContextSettings(path) {
147
+ let thinkingLevel = "off";
148
+ let model = null;
149
+ for (const entry of path) {
150
+ if (entry.type === "thinking_level_change") {
151
+ thinkingLevel = entry.thinkingLevel;
152
+ }
153
+ else if (entry.type === "model_change") {
154
+ model = { provider: entry.provider, modelId: entry.modelId };
155
+ }
156
+ else if (entry.type === "message" && entry.message.role === "assistant") {
157
+ model = { provider: entry.message.provider, modelId: entry.message.model };
158
+ }
159
+ }
160
+ return { thinkingLevel, model };
161
+ }
162
+ /**
163
+ * Project one selected session entry into LLM/runtime messages.
164
+ * Plain custom entries are display/state entries and do not participate in context.
165
+ */
166
+ export function sessionEntryToContextMessages(entry) {
167
+ if (entry.type === "message") {
168
+ const message = entry.message;
169
+ // Session files are parsed without validation; old versions, forks, or
170
+ // hand-edited files can contain messages with null/missing content.
171
+ if ((message.role === "user" || message.role === "assistant" || message.role === "toolResult") &&
172
+ message.content == null) {
173
+ return [{ ...message, content: [] }];
174
+ }
175
+ return [message];
176
+ }
177
+ if (entry.type === "custom_message") {
178
+ return [
179
+ createCustomMessage(entry.customType, entry.content ?? [], entry.display, entry.details, entry.timestamp),
180
+ ];
181
+ }
182
+ if (entry.type === "branch_summary" && entry.summary) {
183
+ return [createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp)];
184
+ }
185
+ if (entry.type === "compaction") {
186
+ return [createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp)];
187
+ }
188
+ return [];
189
+ }
190
+ /**
191
+ * Build the active, compaction-aware session entry list.
192
+ *
193
+ * This follows the current leaf path. If the path contains compaction entries,
194
+ * the latest compaction is represented by the compaction entry itself, followed
195
+ * by the kept entries starting at firstKeptEntryId and all entries after the
196
+ * compaction entry. Older summarized entries are omitted.
197
+ */
198
+ export function buildContextEntries(entries, leafId, byId) {
199
+ const path = buildSessionPath(entries, leafId, byId);
200
+ let compaction = null;
201
+ for (const entry of path) {
202
+ if (entry.type === "compaction") {
203
+ compaction = entry;
204
+ }
205
+ }
206
+ if (!compaction) {
207
+ return path;
208
+ }
209
+ const compactionIdx = path.findIndex((entry) => entry.id === compaction.id);
210
+ if (compactionIdx < 0) {
211
+ return path;
212
+ }
213
+ const contextEntries = [compaction];
214
+ let foundFirstKept = false;
215
+ for (let i = 0; i < compactionIdx; i++) {
216
+ const entry = path[i];
217
+ if (entry.id === compaction.firstKeptEntryId) {
218
+ foundFirstKept = true;
219
+ }
220
+ if (foundFirstKept) {
221
+ contextEntries.push(entry);
222
+ }
223
+ }
224
+ contextEntries.push(...path.slice(compactionIdx + 1));
225
+ return contextEntries;
226
+ }
227
+ /**
228
+ * Build the session context from entries using tree traversal.
229
+ * If leafId is provided, walks from that entry to root.
230
+ * Handles compaction and branch summaries along the path.
231
+ */
232
+ export function buildSessionContext(entries, leafId, byId) {
233
+ const path = buildSessionPath(entries, leafId, byId);
234
+ const { thinkingLevel, model } = getSessionContextSettings(path);
235
+ const messages = buildContextEntries(entries, leafId, byId).flatMap(sessionEntryToContextMessages);
236
+ return { messages, thinkingLevel, model };
237
+ }
238
+ /**
239
+ * Compute the default session directory for a cwd.
240
+ * Encodes cwd into a safe directory name under ~/.pi/agent/sessions/.
241
+ */
242
+ function getDefaultSessionDirPath(cwd, agentDir = getDefaultAgentDir()) {
243
+ const resolvedCwd = resolvePath(cwd);
244
+ const resolvedAgentDir = resolvePath(agentDir);
245
+ const safePath = `--${resolvedCwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
246
+ return join(resolvedAgentDir, "sessions", safePath);
247
+ }
248
+ export function getDefaultSessionDir(cwd, agentDir = getDefaultAgentDir()) {
249
+ const sessionDir = getDefaultSessionDirPath(cwd, agentDir);
250
+ if (!existsSync(sessionDir)) {
251
+ mkdirSync(sessionDir, { recursive: true });
252
+ }
253
+ return sessionDir;
254
+ }
255
+ const SESSION_READ_BUFFER_SIZE = 1024 * 1024;
256
+ const SESSION_HEADER_READ_BUFFER_SIZE = 4096;
257
+ /** Bound synchronous header discovery while allowing large cwd and custom metadata fields. */
258
+ const MAX_SESSION_HEADER_SCAN_BYTES = 1024 * 1024;
259
+ class SessionHeaderScanLimitError extends Error {
260
+ constructor(filePath) {
261
+ super(`Session header exceeds ${MAX_SESSION_HEADER_SCAN_BYTES}-byte scan limit: ${filePath}`);
262
+ this.name = "SessionHeaderScanLimitError";
263
+ }
264
+ }
265
+ function parseSessionEntryLine(line) {
266
+ if (!line.trim())
267
+ return null;
268
+ try {
269
+ return JSON.parse(line);
270
+ }
271
+ catch {
272
+ // Skip malformed lines
273
+ return null;
274
+ }
275
+ }
276
+ /** Exported for testing */
277
+ export function loadEntriesFromFile(filePath) {
278
+ const resolvedFilePath = normalizePath(filePath);
279
+ if (!existsSync(resolvedFilePath))
280
+ return [];
281
+ const entries = [];
282
+ const fd = openSync(resolvedFilePath, "r");
283
+ try {
284
+ const decoder = new StringDecoder("utf8");
285
+ const buffer = Buffer.allocUnsafe(SESSION_READ_BUFFER_SIZE);
286
+ let pending = "";
287
+ while (true) {
288
+ const bytesRead = readSync(fd, buffer, 0, buffer.length, null);
289
+ if (bytesRead === 0)
290
+ break;
291
+ pending += decoder.write(buffer.subarray(0, bytesRead));
292
+ let lineStart = 0;
293
+ let newlineIndex = pending.indexOf("\n", lineStart);
294
+ while (newlineIndex !== -1) {
295
+ const entry = parseSessionEntryLine(pending.slice(lineStart, newlineIndex));
296
+ if (entry)
297
+ entries.push(entry);
298
+ lineStart = newlineIndex + 1;
299
+ newlineIndex = pending.indexOf("\n", lineStart);
300
+ }
301
+ pending = pending.slice(lineStart);
302
+ }
303
+ pending += decoder.end();
304
+ const finalEntry = parseSessionEntryLine(pending);
305
+ if (finalEntry)
306
+ entries.push(finalEntry);
307
+ }
308
+ finally {
309
+ closeSync(fd);
310
+ }
311
+ // Validate session header
312
+ if (entries.length === 0)
313
+ return entries;
314
+ const header = entries[0];
315
+ if (header.type !== "session" || typeof header.id !== "string") {
316
+ return [];
317
+ }
318
+ return entries;
319
+ }
320
+ /**
321
+ * Inspect a physical line while searching for the first parsed session entry.
322
+ * Blank and malformed lines are skipped to match loadEntriesFromFile().
323
+ * Returns undefined to keep scanning, null for a parsed non-header entry, or the header.
324
+ */
325
+ function parseSessionHeaderCandidate(line) {
326
+ if (!line.trim())
327
+ return undefined;
328
+ const entry = parseSessionEntryLine(line);
329
+ if (!entry)
330
+ return undefined;
331
+ if (entry.type !== "session" || typeof entry.id !== "string")
332
+ return null;
333
+ return entry;
334
+ }
335
+ function readSessionHeader(filePath) {
336
+ const fd = openSync(filePath, "r");
337
+ try {
338
+ const decoder = new StringDecoder("utf8");
339
+ const buffer = Buffer.allocUnsafe(SESSION_HEADER_READ_BUFFER_SIZE);
340
+ const lineChunks = [];
341
+ let scannedBytes = 0;
342
+ while (scannedBytes < MAX_SESSION_HEADER_SCAN_BYTES) {
343
+ const readLength = Math.min(buffer.length, MAX_SESSION_HEADER_SCAN_BYTES - scannedBytes);
344
+ const bytesRead = readSync(fd, buffer, 0, readLength, null);
345
+ if (bytesRead === 0) {
346
+ lineChunks.push(decoder.end());
347
+ return parseSessionHeaderCandidate(lineChunks.join("")) ?? null;
348
+ }
349
+ scannedBytes += bytesRead;
350
+ const chunk = decoder.write(buffer.subarray(0, bytesRead));
351
+ let lineStart = 0;
352
+ let newlineIndex = chunk.indexOf("\n", lineStart);
353
+ while (newlineIndex !== -1) {
354
+ lineChunks.push(chunk.slice(lineStart, newlineIndex));
355
+ const header = parseSessionHeaderCandidate(lineChunks.join(""));
356
+ if (header !== undefined)
357
+ return header;
358
+ lineChunks.length = 0;
359
+ lineStart = newlineIndex + 1;
360
+ newlineIndex = chunk.indexOf("\n", lineStart);
361
+ }
362
+ lineChunks.push(chunk.slice(lineStart));
363
+ }
364
+ // Probe for EOF so a final header without a newline is allowed when it ends
365
+ // exactly at the scan limit. Any additional byte exceeds the bounded scan.
366
+ const probe = Buffer.allocUnsafe(1);
367
+ if (readSync(fd, probe, 0, probe.length, null) === 0) {
368
+ lineChunks.push(decoder.end());
369
+ return parseSessionHeaderCandidate(lineChunks.join("")) ?? null;
370
+ }
371
+ throw new SessionHeaderScanLimitError(filePath);
372
+ }
373
+ finally {
374
+ closeSync(fd);
375
+ }
376
+ }
377
+ function readSessionHeaderForDiscovery(filePath) {
378
+ try {
379
+ return readSessionHeader(filePath);
380
+ }
381
+ catch {
382
+ // Discovery is best-effort: unreadable or oversized files are not sessions,
383
+ // and one corrupt file must not prevent other sessions from being found.
384
+ return null;
385
+ }
386
+ }
387
+ function getSessionHeaderCwd(header) {
388
+ const cwd = header.cwd;
389
+ return typeof cwd === "string" ? cwd : undefined;
390
+ }
391
+ function sessionCwdMatches(cwd, resolvedCwd) {
392
+ return cwd !== undefined && cwd !== "" && resolvePath(cwd) === resolvedCwd;
393
+ }
394
+ /** Exported for testing */
395
+ export function findMostRecentSession(sessionDir, cwd) {
396
+ const resolvedSessionDir = normalizePath(sessionDir);
397
+ const resolvedCwd = cwd ? resolvePath(cwd) : undefined;
398
+ try {
399
+ const files = readdirSync(resolvedSessionDir)
400
+ .filter((f) => f.endsWith(".jsonl"))
401
+ .map((f) => join(resolvedSessionDir, f))
402
+ .map((path) => ({ path, header: readSessionHeaderForDiscovery(path) }))
403
+ .filter((file) => file.header !== null &&
404
+ (!resolvedCwd || sessionCwdMatches(getSessionHeaderCwd(file.header), resolvedCwd)))
405
+ .map(({ path }) => ({ path, mtime: statSync(path).mtime }))
406
+ .sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
407
+ return files[0]?.path || null;
408
+ }
409
+ catch {
410
+ // Directory access and stat races make recent-session discovery unavailable.
411
+ return null;
412
+ }
413
+ }
414
+ function isMessageWithContent(message) {
415
+ return typeof message.role === "string" && "content" in message;
416
+ }
417
+ function extractTextContent(message) {
418
+ const content = message.content;
419
+ if (typeof content === "string") {
420
+ return content;
421
+ }
422
+ return content
423
+ .filter((block) => block.type === "text")
424
+ .map((block) => block.text)
425
+ .join(" ");
426
+ }
427
+ function getMessageActivityTime(entry) {
428
+ const message = entry.message;
429
+ if (!isMessageWithContent(message))
430
+ return undefined;
431
+ if (message.role !== "user" && message.role !== "assistant")
432
+ return undefined;
433
+ const msgTimestamp = message.timestamp;
434
+ if (typeof msgTimestamp === "number") {
435
+ return msgTimestamp;
436
+ }
437
+ const t = new Date(entry.timestamp).getTime();
438
+ return Number.isNaN(t) ? undefined : t;
439
+ }
440
+ async function buildSessionInfo(filePath) {
441
+ try {
442
+ const stats = await stat(filePath);
443
+ let header = null;
444
+ let messageCount = 0;
445
+ let firstMessage = "";
446
+ const allMessages = [];
447
+ let name;
448
+ let lastActivityTime;
449
+ const rl = createInterface({
450
+ input: createReadStream(filePath, { encoding: "utf8" }),
451
+ crlfDelay: Infinity,
452
+ });
453
+ for await (const line of rl) {
454
+ const entry = parseSessionEntryLine(line);
455
+ if (!entry)
456
+ continue;
457
+ if (!header) {
458
+ if (entry.type !== "session")
459
+ return null;
460
+ header = entry;
461
+ continue;
462
+ }
463
+ // Extract session name (use latest, including explicit clears)
464
+ if (entry.type === "session_info") {
465
+ name = entry.name?.trim() || undefined;
466
+ }
467
+ if (entry.type !== "message")
468
+ continue;
469
+ messageCount++;
470
+ const activityTime = getMessageActivityTime(entry);
471
+ if (typeof activityTime === "number") {
472
+ lastActivityTime = Math.max(lastActivityTime ?? 0, activityTime);
473
+ }
474
+ const message = entry.message;
475
+ if (!isMessageWithContent(message))
476
+ continue;
477
+ if (message.role !== "user" && message.role !== "assistant")
478
+ continue;
479
+ const textContent = extractTextContent(message);
480
+ if (!textContent)
481
+ continue;
482
+ allMessages.push(textContent);
483
+ if (!firstMessage && message.role === "user") {
484
+ firstMessage = textContent;
485
+ }
486
+ }
487
+ if (!header)
488
+ return null;
489
+ const cwd = typeof header.cwd === "string" ? header.cwd : "";
490
+ const parentSessionPath = header.parentSession;
491
+ const headerTime = typeof header.timestamp === "string" ? new Date(header.timestamp).getTime() : NaN;
492
+ const modified = typeof lastActivityTime === "number" && lastActivityTime > 0
493
+ ? new Date(lastActivityTime)
494
+ : !Number.isNaN(headerTime)
495
+ ? new Date(headerTime)
496
+ : stats.mtime;
497
+ return {
498
+ path: filePath,
499
+ id: header.id,
500
+ cwd,
501
+ name,
502
+ parentSessionPath,
503
+ created: new Date(header.timestamp),
504
+ modified,
505
+ messageCount,
506
+ firstMessage: firstMessage || "(no messages)",
507
+ allMessagesText: allMessages.join(" "),
508
+ };
509
+ }
510
+ catch {
511
+ return null;
512
+ }
513
+ }
514
+ const MAX_CONCURRENT_SESSION_INFO_LOADS = 10;
515
+ async function buildSessionInfosWithConcurrency(files, onLoaded) {
516
+ const results = new Array(files.length).fill(null);
517
+ const inFlight = new Set();
518
+ let nextIndex = 0;
519
+ const startNext = () => {
520
+ const index = nextIndex++;
521
+ const file = files[index];
522
+ if (!file)
523
+ return;
524
+ let task;
525
+ task = buildSessionInfo(file)
526
+ .then((info) => {
527
+ results[index] = info;
528
+ })
529
+ .catch(() => {
530
+ results[index] = null;
531
+ })
532
+ .finally(() => {
533
+ inFlight.delete(task);
534
+ onLoaded();
535
+ });
536
+ inFlight.add(task);
537
+ };
538
+ while (nextIndex < files.length || inFlight.size > 0) {
539
+ while (nextIndex < files.length && inFlight.size < MAX_CONCURRENT_SESSION_INFO_LOADS) {
540
+ startNext();
541
+ }
542
+ if (inFlight.size > 0) {
543
+ await Promise.race(inFlight);
544
+ }
545
+ }
546
+ return results;
547
+ }
548
+ async function listSessionsFromDir(dir, onProgress, progressOffset = 0, progressTotal) {
549
+ const sessions = [];
550
+ if (!existsSync(dir)) {
551
+ return sessions;
552
+ }
553
+ try {
554
+ const dirEntries = await readdir(dir);
555
+ const files = dirEntries.filter((f) => f.endsWith(".jsonl")).map((f) => join(dir, f));
556
+ const total = progressTotal ?? files.length;
557
+ let loaded = 0;
558
+ const results = await buildSessionInfosWithConcurrency(files, () => {
559
+ loaded++;
560
+ onProgress?.(progressOffset + loaded, total);
561
+ });
562
+ for (const info of results) {
563
+ if (info) {
564
+ sessions.push(info);
565
+ }
566
+ }
567
+ }
568
+ catch {
569
+ // Return empty list on error
570
+ }
571
+ return sessions;
572
+ }
573
+ /**
574
+ * Manages conversation sessions as append-only trees stored in JSONL files.
575
+ *
576
+ * Each session entry has an id and parentId forming a tree structure. The "leaf"
577
+ * pointer tracks the current position. Appending creates a child of the current leaf.
578
+ * Branching moves the leaf to an earlier entry, allowing new branches without
579
+ * modifying history.
580
+ *
581
+ * Use buildSessionContext() to get the resolved message list for the LLM, which
582
+ * handles compaction summaries and follows the path from root to current leaf.
583
+ */
584
+ export class SessionManager {
585
+ sessionId = "";
586
+ sessionFile;
587
+ sessionDir;
588
+ cwd;
589
+ persist;
590
+ flushed = false;
591
+ fileEntries = [];
592
+ byId = new Map();
593
+ labelsById = new Map();
594
+ labelTimestampsById = new Map();
595
+ leafId = null;
596
+ constructor(cwd, sessionDir, sessionFile, persist, newSessionOptions, preloadedFileEntries) {
597
+ this.cwd = resolvePath(cwd);
598
+ this.sessionDir = normalizePath(sessionDir);
599
+ this.persist = persist;
600
+ if (persist && this.sessionDir && !existsSync(this.sessionDir)) {
601
+ mkdirSync(this.sessionDir, { recursive: true });
602
+ }
603
+ if (sessionFile) {
604
+ this._setSessionFile(sessionFile, preloadedFileEntries);
605
+ }
606
+ else {
607
+ this.newSession(newSessionOptions);
608
+ }
609
+ }
610
+ /** Switch to a different session file (used for resume and branching) */
611
+ setSessionFile(sessionFile) {
612
+ this._setSessionFile(sessionFile);
613
+ }
614
+ _setSessionFile(sessionFile, preloadedFileEntries) {
615
+ this.sessionFile = resolvePath(sessionFile);
616
+ if (existsSync(this.sessionFile)) {
617
+ this.fileEntries = preloadedFileEntries ?? loadEntriesFromFile(this.sessionFile);
618
+ // If file was empty, initialize it with a valid session header. If it was
619
+ // non-empty but did not parse as a pi session, fail without modifying it.
620
+ if (this.fileEntries.length === 0) {
621
+ const explicitPath = this.sessionFile;
622
+ if (statSync(explicitPath).size > 0) {
623
+ throw new Error(`Session file is not a valid pi session: ${explicitPath}`);
624
+ }
625
+ this.newSession();
626
+ this.sessionFile = explicitPath;
627
+ this._rewriteFile();
628
+ this.flushed = true;
629
+ return;
630
+ }
631
+ const header = this.fileEntries.find((e) => e.type === "session");
632
+ this.sessionId = header?.id ?? createSessionId();
633
+ if (migrateToCurrentVersion(this.fileEntries)) {
634
+ this._rewriteFile();
635
+ }
636
+ this._buildIndex();
637
+ this.flushed = true;
638
+ }
639
+ else {
640
+ const explicitPath = this.sessionFile;
641
+ this.newSession();
642
+ this.sessionFile = explicitPath; // preserve explicit path from --session flag
643
+ }
644
+ }
645
+ newSession(options) {
646
+ if (options?.id !== undefined) {
647
+ assertValidSessionId(options.id);
648
+ }
649
+ this.sessionId = options?.id ?? createSessionId();
650
+ const timestamp = new Date().toISOString();
651
+ const header = {
652
+ type: "session",
653
+ version: CURRENT_SESSION_VERSION,
654
+ id: this.sessionId,
655
+ timestamp,
656
+ cwd: this.cwd,
657
+ parentSession: options?.parentSession,
658
+ };
659
+ this.fileEntries = [header];
660
+ this.byId.clear();
661
+ this.labelsById.clear();
662
+ this.labelTimestampsById.clear();
663
+ this.leafId = null;
664
+ this.flushed = false;
665
+ if (this.persist) {
666
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
667
+ this.sessionFile = join(this.getSessionDir(), `${fileTimestamp}_${this.sessionId}.jsonl`);
668
+ }
669
+ return this.sessionFile;
670
+ }
671
+ _buildIndex() {
672
+ this.byId.clear();
673
+ this.labelsById.clear();
674
+ this.labelTimestampsById.clear();
675
+ this.leafId = null;
676
+ for (const entry of this.fileEntries) {
677
+ if (entry.type === "session")
678
+ continue;
679
+ this.byId.set(entry.id, entry);
680
+ this.leafId = entry.id;
681
+ if (entry.type === "label") {
682
+ if (entry.label) {
683
+ this.labelsById.set(entry.targetId, entry.label);
684
+ this.labelTimestampsById.set(entry.targetId, entry.timestamp);
685
+ }
686
+ else {
687
+ this.labelsById.delete(entry.targetId);
688
+ this.labelTimestampsById.delete(entry.targetId);
689
+ }
690
+ }
691
+ }
692
+ }
693
+ _rewriteFile() {
694
+ if (!this.persist || !this.sessionFile)
695
+ return;
696
+ const fd = openSync(this.sessionFile, "w");
697
+ try {
698
+ for (const entry of this.fileEntries) {
699
+ writeFileSync(fd, `${JSON.stringify(entry)}\n`);
700
+ }
701
+ }
702
+ finally {
703
+ closeSync(fd);
704
+ }
705
+ }
706
+ isPersisted() {
707
+ return this.persist;
708
+ }
709
+ getCwd() {
710
+ return this.cwd;
711
+ }
712
+ getSessionDir() {
713
+ return this.sessionDir;
714
+ }
715
+ usesDefaultSessionDir() {
716
+ return this.sessionDir === getDefaultSessionDirPath(this.cwd);
717
+ }
718
+ getSessionId() {
719
+ return this.sessionId;
720
+ }
721
+ getSessionFile() {
722
+ return this.sessionFile;
723
+ }
724
+ _persist(entry) {
725
+ if (!this.persist || !this.sessionFile)
726
+ return;
727
+ const hasAssistant = this.fileEntries.some((e) => e.type === "message" && e.message.role === "assistant");
728
+ if (!hasAssistant) {
729
+ if (this.flushed) {
730
+ appendFileSync(this.sessionFile, `${JSON.stringify(entry)}\n`);
731
+ }
732
+ else {
733
+ // Mark as not flushed so when assistant arrives, all entries get written
734
+ this.flushed = false;
735
+ }
736
+ return;
737
+ }
738
+ if (!this.flushed) {
739
+ const fd = openSync(this.sessionFile, "wx");
740
+ try {
741
+ for (const e of this.fileEntries) {
742
+ writeFileSync(fd, `${JSON.stringify(e)}\n`);
743
+ }
744
+ }
745
+ finally {
746
+ closeSync(fd);
747
+ }
748
+ this.flushed = true;
749
+ }
750
+ else {
751
+ appendFileSync(this.sessionFile, `${JSON.stringify(entry)}\n`);
752
+ }
753
+ }
754
+ _appendEntry(entry) {
755
+ this.fileEntries.push(entry);
756
+ this.byId.set(entry.id, entry);
757
+ this.leafId = entry.id;
758
+ this._persist(entry);
759
+ }
760
+ /** Append a message as child of current leaf, then advance leaf. Returns entry id.
761
+ * Does not allow writing CompactionSummaryMessage and BranchSummaryMessage directly.
762
+ * Reason: we want these to be top-level entries in the session, not message session entries,
763
+ * so it is easier to find them.
764
+ * These need to be appended via appendCompaction() and appendBranchSummary() methods.
765
+ */
766
+ appendMessage(message) {
767
+ const entry = {
768
+ type: "message",
769
+ id: generateId(this.byId),
770
+ parentId: this.leafId,
771
+ timestamp: new Date().toISOString(),
772
+ message,
773
+ };
774
+ this._appendEntry(entry);
775
+ return entry.id;
776
+ }
777
+ /** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */
778
+ appendThinkingLevelChange(thinkingLevel) {
779
+ const entry = {
780
+ type: "thinking_level_change",
781
+ id: generateId(this.byId),
782
+ parentId: this.leafId,
783
+ timestamp: new Date().toISOString(),
784
+ thinkingLevel,
785
+ };
786
+ this._appendEntry(entry);
787
+ return entry.id;
788
+ }
789
+ /** Append a model change as child of current leaf, then advance leaf. Returns entry id. */
790
+ appendModelChange(provider, modelId) {
791
+ const entry = {
792
+ type: "model_change",
793
+ id: generateId(this.byId),
794
+ parentId: this.leafId,
795
+ timestamp: new Date().toISOString(),
796
+ provider,
797
+ modelId,
798
+ };
799
+ this._appendEntry(entry);
800
+ return entry.id;
801
+ }
802
+ /** Append a compaction summary as child of current leaf, then advance leaf. Returns entry id. */
803
+ appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromHook, usage) {
804
+ const entry = {
805
+ type: "compaction",
806
+ id: generateId(this.byId),
807
+ parentId: this.leafId,
808
+ timestamp: new Date().toISOString(),
809
+ summary,
810
+ firstKeptEntryId,
811
+ tokensBefore,
812
+ details,
813
+ usage,
814
+ fromHook,
815
+ };
816
+ this._appendEntry(entry);
817
+ return entry.id;
818
+ }
819
+ /** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */
820
+ appendCustomEntry(customType, data) {
821
+ const entry = {
822
+ type: "custom",
823
+ customType,
824
+ data,
825
+ id: generateId(this.byId),
826
+ parentId: this.leafId,
827
+ timestamp: new Date().toISOString(),
828
+ };
829
+ this._appendEntry(entry);
830
+ return entry.id;
831
+ }
832
+ /** Append a session info entry (e.g., display name). Returns entry id. */
833
+ appendSessionInfo(name) {
834
+ const sanitizedName = name.replace(/[\r\n]+/g, " ").trim();
835
+ const entry = {
836
+ type: "session_info",
837
+ id: generateId(this.byId),
838
+ parentId: this.leafId,
839
+ timestamp: new Date().toISOString(),
840
+ name: sanitizedName,
841
+ };
842
+ this._appendEntry(entry);
843
+ return entry.id;
844
+ }
845
+ /** Get the current session name from the latest session_info entry, if any. */
846
+ getSessionName() {
847
+ // Walk entries in reverse to find the latest session_info entry.
848
+ // Empty names explicitly clear the session title.
849
+ const entries = this.getEntries();
850
+ for (let i = entries.length - 1; i >= 0; i--) {
851
+ const entry = entries[i];
852
+ if (entry.type === "session_info") {
853
+ return entry.name?.trim() || undefined;
854
+ }
855
+ }
856
+ return undefined;
857
+ }
858
+ /**
859
+ * Append a custom message entry (for extensions) that participates in LLM context.
860
+ * @param customType Extension identifier for filtering on reload
861
+ * @param content Message content (string or TextContent/ImageContent array)
862
+ * @param display Whether to show in TUI (true = styled display, false = hidden)
863
+ * @param details Optional extension-specific metadata (not sent to LLM)
864
+ * @returns Entry id
865
+ */
866
+ appendCustomMessageEntry(customType, content, display, details) {
867
+ const entry = {
868
+ type: "custom_message",
869
+ customType,
870
+ content,
871
+ display,
872
+ details,
873
+ id: generateId(this.byId),
874
+ parentId: this.leafId,
875
+ timestamp: new Date().toISOString(),
876
+ };
877
+ this._appendEntry(entry);
878
+ return entry.id;
879
+ }
880
+ // =========================================================================
881
+ // Tree Traversal
882
+ // =========================================================================
883
+ getLeafId() {
884
+ return this.leafId;
885
+ }
886
+ getLeafEntry() {
887
+ return this.leafId ? this.byId.get(this.leafId) : undefined;
888
+ }
889
+ getEntry(id) {
890
+ return this.byId.get(id);
891
+ }
892
+ /**
893
+ * Get all direct children of an entry.
894
+ */
895
+ getChildren(parentId) {
896
+ const children = [];
897
+ for (const entry of this.byId.values()) {
898
+ if (entry.parentId === parentId) {
899
+ children.push(entry);
900
+ }
901
+ }
902
+ return children;
903
+ }
904
+ /**
905
+ * Get the label for an entry, if any.
906
+ */
907
+ getLabel(id) {
908
+ return this.labelsById.get(id);
909
+ }
910
+ /**
911
+ * Set or clear a label on an entry.
912
+ * Labels are user-defined markers for bookmarking/navigation.
913
+ * Pass undefined or empty string to clear the label.
914
+ */
915
+ appendLabelChange(targetId, label) {
916
+ if (!this.byId.has(targetId)) {
917
+ throw new Error(`Entry ${targetId} not found`);
918
+ }
919
+ const entry = {
920
+ type: "label",
921
+ id: generateId(this.byId),
922
+ parentId: this.leafId,
923
+ timestamp: new Date().toISOString(),
924
+ targetId,
925
+ label,
926
+ };
927
+ this._appendEntry(entry);
928
+ if (label) {
929
+ this.labelsById.set(targetId, label);
930
+ this.labelTimestampsById.set(targetId, entry.timestamp);
931
+ }
932
+ else {
933
+ this.labelsById.delete(targetId);
934
+ this.labelTimestampsById.delete(targetId);
935
+ }
936
+ return entry.id;
937
+ }
938
+ /**
939
+ * Walk from entry to root, returning all entries in path order.
940
+ * Includes all entry types (messages, compaction, model changes, etc.).
941
+ * Use buildSessionContext() to get the resolved messages for the LLM.
942
+ */
943
+ getBranch(fromId) {
944
+ const path = [];
945
+ const startId = fromId ?? this.leafId;
946
+ let current = startId ? this.byId.get(startId) : undefined;
947
+ while (current) {
948
+ path.push(current);
949
+ current = current.parentId ? this.byId.get(current.parentId) : undefined;
950
+ }
951
+ path.reverse();
952
+ return path;
953
+ }
954
+ /**
955
+ * Build the active, compaction-aware entry list for context/rendering.
956
+ * Uses tree traversal from current leaf.
957
+ */
958
+ buildContextEntries() {
959
+ return buildContextEntries(this.getEntries(), this.leafId, this.byId);
960
+ }
961
+ /**
962
+ * Build the session context (what gets sent to the LLM).
963
+ * Uses tree traversal from current leaf.
964
+ */
965
+ buildSessionContext() {
966
+ return buildSessionContext(this.getEntries(), this.leafId, this.byId);
967
+ }
968
+ /**
969
+ * Get session header.
970
+ */
971
+ getHeader() {
972
+ const h = this.fileEntries.find((e) => e.type === "session");
973
+ return h ? h : null;
974
+ }
975
+ /**
976
+ * Get all session entries (excludes header). Returns a shallow copy.
977
+ * The session is append-only: use appendXXX() to add entries, branch() to
978
+ * change the leaf pointer. Entries cannot be modified or deleted.
979
+ */
980
+ getEntries() {
981
+ return this.fileEntries.filter((e) => e.type !== "session");
982
+ }
983
+ /**
984
+ * Get the session as a tree structure. Returns a shallow defensive copy of all entries.
985
+ * A well-formed session has exactly one root (first entry with parentId === null).
986
+ * Orphaned entries (broken parent chain) are also returned as roots.
987
+ */
988
+ getTree() {
989
+ const entries = this.getEntries();
990
+ const nodeMap = new Map();
991
+ const roots = [];
992
+ // Create nodes with resolved labels
993
+ for (const entry of entries) {
994
+ const label = this.labelsById.get(entry.id);
995
+ const labelTimestamp = this.labelTimestampsById.get(entry.id);
996
+ nodeMap.set(entry.id, { entry, children: [], label, labelTimestamp });
997
+ }
998
+ // Build tree
999
+ for (const entry of entries) {
1000
+ const node = nodeMap.get(entry.id);
1001
+ if (entry.parentId === null || entry.parentId === entry.id) {
1002
+ roots.push(node);
1003
+ }
1004
+ else {
1005
+ const parent = nodeMap.get(entry.parentId);
1006
+ if (parent) {
1007
+ parent.children.push(node);
1008
+ }
1009
+ else {
1010
+ // Orphan - treat as root
1011
+ roots.push(node);
1012
+ }
1013
+ }
1014
+ }
1015
+ // Sort children by timestamp (oldest first, newest at bottom)
1016
+ // Use iterative approach to avoid stack overflow on deep trees
1017
+ const stack = [...roots];
1018
+ while (stack.length > 0) {
1019
+ const node = stack.pop();
1020
+ node.children.sort((a, b) => new Date(a.entry.timestamp).getTime() - new Date(b.entry.timestamp).getTime());
1021
+ stack.push(...node.children);
1022
+ }
1023
+ return roots;
1024
+ }
1025
+ // =========================================================================
1026
+ // Branching
1027
+ // =========================================================================
1028
+ /**
1029
+ * Start a new branch from an earlier entry.
1030
+ * Moves the leaf pointer to the specified entry. The next appendXXX() call
1031
+ * will create a child of that entry, forming a new branch. Existing entries
1032
+ * are not modified or deleted.
1033
+ */
1034
+ branch(branchFromId) {
1035
+ if (!this.byId.has(branchFromId)) {
1036
+ throw new Error(`Entry ${branchFromId} not found`);
1037
+ }
1038
+ this.leafId = branchFromId;
1039
+ }
1040
+ /**
1041
+ * Reset the leaf pointer to null (before any entries).
1042
+ * The next appendXXX() call will create a new root entry (parentId = null).
1043
+ * Use this when navigating to re-edit the first user message.
1044
+ */
1045
+ resetLeaf() {
1046
+ this.leafId = null;
1047
+ }
1048
+ /**
1049
+ * Start a new branch with a summary of the abandoned path.
1050
+ * Same as branch(), but also appends a branch_summary entry that captures
1051
+ * context from the abandoned conversation path.
1052
+ */
1053
+ branchWithSummary(branchFromId, summary, details, fromHook, usage) {
1054
+ if (branchFromId !== null && !this.byId.has(branchFromId)) {
1055
+ throw new Error(`Entry ${branchFromId} not found`);
1056
+ }
1057
+ this.leafId = branchFromId;
1058
+ const entry = {
1059
+ type: "branch_summary",
1060
+ id: generateId(this.byId),
1061
+ parentId: branchFromId,
1062
+ timestamp: new Date().toISOString(),
1063
+ fromId: branchFromId ?? "root",
1064
+ summary,
1065
+ details,
1066
+ usage,
1067
+ fromHook,
1068
+ };
1069
+ this._appendEntry(entry);
1070
+ return entry.id;
1071
+ }
1072
+ /**
1073
+ * Create a new session file containing only the path from root to the specified leaf.
1074
+ * Useful for extracting a single conversation path from a branched session.
1075
+ * Returns the new session file path, or undefined if not persisting.
1076
+ */
1077
+ createBranchedSession(leafId) {
1078
+ const previousSessionFile = this.sessionFile;
1079
+ const path = this.getBranch(leafId);
1080
+ if (path.length === 0) {
1081
+ throw new Error(`Entry ${leafId} not found`);
1082
+ }
1083
+ // Filter out LabelEntry from path - we'll recreate them from the resolved map.
1084
+ // Because labels are real tree entries, later entries can be children of labels;
1085
+ // removing labels requires re-chaining the retained path to avoid orphaned subtrees.
1086
+ const pathWithoutLabels = [];
1087
+ let pathParentId = null;
1088
+ for (const entry of path) {
1089
+ if (entry.type === "label")
1090
+ continue;
1091
+ pathWithoutLabels.push({ ...entry, parentId: pathParentId });
1092
+ pathParentId = entry.id;
1093
+ }
1094
+ const newSessionId = createSessionId();
1095
+ const timestamp = new Date().toISOString();
1096
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
1097
+ const newSessionFile = join(this.getSessionDir(), `${fileTimestamp}_${newSessionId}.jsonl`);
1098
+ const header = {
1099
+ type: "session",
1100
+ version: CURRENT_SESSION_VERSION,
1101
+ id: newSessionId,
1102
+ timestamp,
1103
+ cwd: this.cwd,
1104
+ parentSession: this.persist ? previousSessionFile : undefined,
1105
+ };
1106
+ // Collect labels for entries in the path
1107
+ const pathEntryIds = new Set(pathWithoutLabels.map((e) => e.id));
1108
+ const labelsToWrite = [];
1109
+ for (const [targetId, label] of this.labelsById) {
1110
+ if (pathEntryIds.has(targetId)) {
1111
+ labelsToWrite.push({ targetId, label, timestamp: this.labelTimestampsById.get(targetId) });
1112
+ }
1113
+ }
1114
+ if (this.persist) {
1115
+ // Build label entries
1116
+ const lastEntryId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;
1117
+ let parentId = lastEntryId;
1118
+ const labelEntries = [];
1119
+ for (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {
1120
+ const labelEntry = {
1121
+ type: "label",
1122
+ id: generateId(new Set(pathEntryIds)),
1123
+ parentId,
1124
+ timestamp: labelTimestamp,
1125
+ targetId,
1126
+ label,
1127
+ };
1128
+ pathEntryIds.add(labelEntry.id);
1129
+ labelEntries.push(labelEntry);
1130
+ parentId = labelEntry.id;
1131
+ }
1132
+ this.fileEntries = [header, ...pathWithoutLabels, ...labelEntries];
1133
+ this.sessionId = newSessionId;
1134
+ this.sessionFile = newSessionFile;
1135
+ this._buildIndex();
1136
+ // Only write the file now if it contains an assistant message.
1137
+ // Otherwise defer to _persist(), which creates the file on the
1138
+ // first assistant response, matching the newSession() contract
1139
+ // and avoiding the duplicate-header bug when _persist()'s
1140
+ // no-assistant guard later resets flushed to false.
1141
+ const hasAssistant = this.fileEntries.some((e) => e.type === "message" && e.message.role === "assistant");
1142
+ if (hasAssistant) {
1143
+ this._rewriteFile();
1144
+ this.flushed = true;
1145
+ }
1146
+ else {
1147
+ this.flushed = false;
1148
+ }
1149
+ return newSessionFile;
1150
+ }
1151
+ // In-memory mode: replace current session with the path + labels
1152
+ const labelEntries = [];
1153
+ let parentId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;
1154
+ for (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {
1155
+ const labelEntry = {
1156
+ type: "label",
1157
+ id: generateId(new Set([...pathEntryIds, ...labelEntries.map((e) => e.id)])),
1158
+ parentId,
1159
+ timestamp: labelTimestamp,
1160
+ targetId,
1161
+ label,
1162
+ };
1163
+ labelEntries.push(labelEntry);
1164
+ parentId = labelEntry.id;
1165
+ }
1166
+ this.fileEntries = [header, ...pathWithoutLabels, ...labelEntries];
1167
+ this.sessionId = newSessionId;
1168
+ this._buildIndex();
1169
+ return undefined;
1170
+ }
1171
+ /**
1172
+ * Create a new session.
1173
+ * @param cwd Working directory (stored in session header)
1174
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
1175
+ */
1176
+ static create(cwd, sessionDir, options) {
1177
+ const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);
1178
+ return new SessionManager(cwd, dir, undefined, true, options);
1179
+ }
1180
+ /**
1181
+ * Open a specific session file.
1182
+ * @param path Path to session file
1183
+ * @param sessionDir Optional session directory for /new or /branch. If omitted, derives from file's parent.
1184
+ * @param cwdOverride Optional cwd override instead of the session header cwd.
1185
+ */
1186
+ static open(path, sessionDir, cwdOverride) {
1187
+ const resolvedPath = resolvePath(path);
1188
+ let header = null;
1189
+ let preloadedFileEntries;
1190
+ if (cwdOverride === undefined && existsSync(resolvedPath)) {
1191
+ try {
1192
+ header = readSessionHeader(resolvedPath);
1193
+ }
1194
+ catch (error) {
1195
+ if (!(error instanceof SessionHeaderScanLimitError))
1196
+ throw error;
1197
+ // The bounded scan is only a discovery optimization. A full load remains
1198
+ // authoritative for legacy files with very large headers or prefixes.
1199
+ preloadedFileEntries = loadEntriesFromFile(resolvedPath);
1200
+ const firstEntry = preloadedFileEntries[0];
1201
+ header = firstEntry?.type === "session" ? firstEntry : null;
1202
+ }
1203
+ }
1204
+ const cwd = cwdOverride ?? (header ? getSessionHeaderCwd(header) : undefined) ?? process.cwd();
1205
+ // If no sessionDir provided, derive from file's parent directory
1206
+ const dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, "..");
1207
+ return new SessionManager(cwd, dir, resolvedPath, true, undefined, preloadedFileEntries);
1208
+ }
1209
+ /**
1210
+ * Continue the most recent session, or create new if none.
1211
+ * @param cwd Working directory
1212
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
1213
+ */
1214
+ static continueRecent(cwd, sessionDir) {
1215
+ const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);
1216
+ const filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);
1217
+ const mostRecent = findMostRecentSession(dir, filterCwd ? cwd : undefined);
1218
+ if (mostRecent) {
1219
+ return new SessionManager(cwd, dir, mostRecent, true);
1220
+ }
1221
+ return new SessionManager(cwd, dir, undefined, true);
1222
+ }
1223
+ /** Create an in-memory session (no file persistence) */
1224
+ static inMemory(cwd = process.cwd(), options) {
1225
+ return new SessionManager(cwd, "", undefined, false, options);
1226
+ }
1227
+ /**
1228
+ * Fork a session from another project directory into the current project.
1229
+ * Creates a new session in the target cwd with the full history from the source session.
1230
+ * @param sourcePath Path to the source session file
1231
+ * @param targetCwd Target working directory (where the new session will be stored)
1232
+ * @param sessionDir Optional session directory. If omitted, uses default for targetCwd.
1233
+ */
1234
+ static forkFrom(sourcePath, targetCwd, sessionDir, options) {
1235
+ const resolvedSourcePath = resolvePath(sourcePath);
1236
+ const resolvedTargetCwd = resolvePath(targetCwd);
1237
+ const sourceEntries = loadEntriesFromFile(resolvedSourcePath);
1238
+ if (sourceEntries.length === 0) {
1239
+ throw new Error(`Cannot fork: source session file is empty or invalid: ${resolvedSourcePath}`);
1240
+ }
1241
+ const sourceHeader = sourceEntries.find((e) => e.type === "session");
1242
+ if (!sourceHeader) {
1243
+ throw new Error(`Cannot fork: source session has no header: ${resolvedSourcePath}`);
1244
+ }
1245
+ const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(resolvedTargetCwd);
1246
+ if (!existsSync(dir)) {
1247
+ mkdirSync(dir, { recursive: true });
1248
+ }
1249
+ // Create new session file with new ID but forked content
1250
+ if (options?.id !== undefined) {
1251
+ assertValidSessionId(options.id);
1252
+ }
1253
+ const newSessionId = options?.id ?? createSessionId();
1254
+ const timestamp = new Date().toISOString();
1255
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
1256
+ const newSessionFile = join(dir, `${fileTimestamp}_${newSessionId}.jsonl`);
1257
+ // Write new header pointing to source as parent, with updated cwd
1258
+ const newHeader = {
1259
+ type: "session",
1260
+ version: CURRENT_SESSION_VERSION,
1261
+ id: newSessionId,
1262
+ timestamp,
1263
+ cwd: resolvedTargetCwd,
1264
+ parentSession: resolvedSourcePath,
1265
+ };
1266
+ writeFileSync(newSessionFile, `${JSON.stringify(newHeader)}\n`, { flag: "wx" });
1267
+ // Copy all non-header entries from source
1268
+ for (const entry of sourceEntries) {
1269
+ if (entry.type !== "session") {
1270
+ appendFileSync(newSessionFile, `${JSON.stringify(entry)}\n`);
1271
+ }
1272
+ }
1273
+ return new SessionManager(resolvedTargetCwd, dir, newSessionFile, true);
1274
+ }
1275
+ /**
1276
+ * List all sessions for a directory.
1277
+ * @param cwd Working directory (used to compute default session directory)
1278
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
1279
+ * @param onProgress Optional callback for progress updates (loaded, total)
1280
+ */
1281
+ static async list(cwd, sessionDir, onProgress) {
1282
+ const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);
1283
+ const filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);
1284
+ const resolvedCwd = resolvePath(cwd);
1285
+ const sessions = (await listSessionsFromDir(dir, onProgress)).filter((session) => !filterCwd || sessionCwdMatches(session.cwd, resolvedCwd));
1286
+ sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
1287
+ return sessions;
1288
+ }
1289
+ static async listAll(sessionDirOrOnProgress, onProgress) {
1290
+ const customSessionDir = typeof sessionDirOrOnProgress === "string" ? normalizePath(sessionDirOrOnProgress) : undefined;
1291
+ const progress = typeof sessionDirOrOnProgress === "function" ? sessionDirOrOnProgress : onProgress;
1292
+ if (customSessionDir) {
1293
+ const sessions = await listSessionsFromDir(customSessionDir, progress);
1294
+ sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
1295
+ return sessions;
1296
+ }
1297
+ const sessionsDir = getSessionsDir();
1298
+ try {
1299
+ if (!existsSync(sessionsDir)) {
1300
+ return [];
1301
+ }
1302
+ const entries = await readdir(sessionsDir, { withFileTypes: true });
1303
+ const dirs = entries.filter((e) => e.isDirectory()).map((e) => join(sessionsDir, e.name));
1304
+ // Count total files first for accurate progress
1305
+ let totalFiles = 0;
1306
+ const dirFiles = [];
1307
+ for (const dir of dirs) {
1308
+ try {
1309
+ const files = (await readdir(dir)).filter((f) => f.endsWith(".jsonl"));
1310
+ dirFiles.push(files.map((f) => join(dir, f)));
1311
+ totalFiles += files.length;
1312
+ }
1313
+ catch {
1314
+ dirFiles.push([]);
1315
+ }
1316
+ }
1317
+ // Process all files with progress tracking
1318
+ let loaded = 0;
1319
+ const sessions = [];
1320
+ const allFiles = dirFiles.flat();
1321
+ const results = await buildSessionInfosWithConcurrency(allFiles, () => {
1322
+ loaded++;
1323
+ progress?.(loaded, totalFiles);
1324
+ });
1325
+ for (const info of results) {
1326
+ if (info) {
1327
+ sessions.push(info);
1328
+ }
1329
+ }
1330
+ sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
1331
+ return sessions;
1332
+ }
1333
+ catch {
1334
+ return [];
1335
+ }
1336
+ }
1337
+ }
1338
+ //# sourceMappingURL=session-manager.js.map