indusagi-coding-agent 0.1.38 → 0.1.39

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 (489) hide show
  1. package/CHANGELOG.md +13 -7
  2. package/README.md +2 -4
  3. package/dist/cli/args.d.ts +6 -128
  4. package/dist/cli/args.d.ts.map +1 -1
  5. package/dist/cli/args.js +277 -472
  6. package/dist/cli/args.js.map +1 -1
  7. package/dist/cli/config-selector.d.ts +9 -62
  8. package/dist/cli/config-selector.d.ts.map +1 -1
  9. package/dist/cli/config-selector.js +31 -146
  10. package/dist/cli/config-selector.js.map +1 -1
  11. package/dist/cli/file-processor.d.ts +1 -69
  12. package/dist/cli/file-processor.d.ts.map +1 -1
  13. package/dist/cli/file-processor.js +36 -267
  14. package/dist/cli/file-processor.js.map +1 -1
  15. package/dist/cli/list-models.d.ts +0 -60
  16. package/dist/cli/list-models.d.ts.map +1 -1
  17. package/dist/cli/list-models.js +53 -246
  18. package/dist/cli/list-models.js.map +1 -1
  19. package/dist/cli/session-picker.d.ts +1 -72
  20. package/dist/cli/session-picker.d.ts.map +1 -1
  21. package/dist/cli/session-picker.js +57 -231
  22. package/dist/cli/session-picker.js.map +1 -1
  23. package/dist/cli.js +25 -31
  24. package/dist/cli.js.map +1 -1
  25. package/dist/config.d.ts +0 -42
  26. package/dist/config.d.ts.map +1 -1
  27. package/dist/config.js +62 -97
  28. package/dist/config.js.map +1 -1
  29. package/dist/core/agent-session.d.ts +23 -0
  30. package/dist/core/agent-session.d.ts.map +1 -1
  31. package/dist/core/agent-session.js +78 -66
  32. package/dist/core/agent-session.js.map +1 -1
  33. package/dist/core/auth-storage.d.ts +4 -95
  34. package/dist/core/auth-storage.d.ts.map +1 -1
  35. package/dist/core/auth-storage.js +233 -288
  36. package/dist/core/auth-storage.js.map +1 -1
  37. package/dist/core/bash-executor.d.ts +0 -323
  38. package/dist/core/bash-executor.d.ts.map +1 -1
  39. package/dist/core/bash-executor.js +126 -359
  40. package/dist/core/bash-executor.js.map +1 -1
  41. package/dist/core/compaction/branch-summarization.d.ts +3 -3
  42. package/dist/core/compaction/branch-summarization.js +16 -16
  43. package/dist/core/compaction/branch-summarization.js.map +1 -1
  44. package/dist/core/compaction/compaction.d.ts +3 -3
  45. package/dist/core/compaction/compaction.js +40 -40
  46. package/dist/core/compaction/compaction.js.map +1 -1
  47. package/dist/core/compaction/index.d.ts +32 -4
  48. package/dist/core/compaction/index.d.ts.map +1 -1
  49. package/dist/core/compaction/index.js +30 -4
  50. package/dist/core/compaction/index.js.map +1 -1
  51. package/dist/core/compaction/utils.d.ts +1 -19
  52. package/dist/core/compaction/utils.d.ts.map +1 -1
  53. package/dist/core/compaction/utils.js +92 -113
  54. package/dist/core/compaction/utils.js.map +1 -1
  55. package/dist/core/discover-packages.d.ts +0 -4
  56. package/dist/core/discover-packages.d.ts.map +1 -1
  57. package/dist/core/discover-packages.js +41 -44
  58. package/dist/core/discover-packages.js.map +1 -1
  59. package/dist/core/event-bus.d.ts +1 -147
  60. package/dist/core/event-bus.d.ts.map +1 -1
  61. package/dist/core/event-bus.js +17 -106
  62. package/dist/core/event-bus.js.map +1 -1
  63. package/dist/core/exec.d.ts +0 -16
  64. package/dist/core/exec.d.ts.map +1 -1
  65. package/dist/core/exec.js +18 -27
  66. package/dist/core/exec.js.map +1 -1
  67. package/dist/core/export-html/ansi-to-html.ts +262 -0
  68. package/dist/core/export-html/index.ts +433 -0
  69. package/dist/core/export-html/template.html +48 -26
  70. package/dist/core/export-html/tool-renderer.d.ts +0 -21
  71. package/dist/core/export-html/tool-renderer.d.ts.map +1 -1
  72. package/dist/core/export-html/tool-renderer.js +35 -51
  73. package/dist/core/export-html/tool-renderer.js.map +1 -1
  74. package/dist/core/export-html/tool-renderer.ts +80 -0
  75. package/dist/core/export-html/vendor/highlight.min.js +401 -370
  76. package/dist/core/export-html/vendor/marked.min.js +71 -3
  77. package/dist/core/extensions/index.d.ts +7 -4
  78. package/dist/core/extensions/index.d.ts.map +1 -1
  79. package/dist/core/extensions/index.js +17 -3
  80. package/dist/core/extensions/index.js.map +1 -1
  81. package/dist/core/extensions/loader.d.ts +0 -6
  82. package/dist/core/extensions/loader.d.ts.map +1 -1
  83. package/dist/core/extensions/loader.js +60 -56
  84. package/dist/core/extensions/loader.js.map +1 -1
  85. package/dist/core/extensions/runner.d.ts +3 -0
  86. package/dist/core/extensions/runner.d.ts.map +1 -1
  87. package/dist/core/extensions/runner.js +48 -80
  88. package/dist/core/extensions/runner.js.map +1 -1
  89. package/dist/core/extensions/types.d.ts +50 -23
  90. package/dist/core/extensions/types.d.ts.map +1 -1
  91. package/dist/core/footer-data-provider.d.ts +2 -15
  92. package/dist/core/footer-data-provider.d.ts.map +1 -1
  93. package/dist/core/footer-data-provider.js +21 -33
  94. package/dist/core/footer-data-provider.js.map +1 -1
  95. package/dist/core/hooks/loader.js +2 -2
  96. package/dist/core/hooks/loader.js.map +1 -1
  97. package/dist/core/index.d.ts +29 -10
  98. package/dist/core/index.d.ts.map +1 -1
  99. package/dist/core/index.js +29 -10
  100. package/dist/core/index.js.map +1 -1
  101. package/dist/core/keybindings.d.ts +2 -179
  102. package/dist/core/keybindings.d.ts.map +1 -1
  103. package/dist/core/keybindings.js +64 -238
  104. package/dist/core/keybindings.js.map +1 -1
  105. package/dist/core/model-registry.d.ts +26 -181
  106. package/dist/core/model-registry.d.ts.map +1 -1
  107. package/dist/core/model-registry.js +228 -407
  108. package/dist/core/model-registry.js.map +1 -1
  109. package/dist/core/model-resolver.d.ts +0 -139
  110. package/dist/core/model-resolver.d.ts.map +1 -1
  111. package/dist/core/model-resolver.js +34 -215
  112. package/dist/core/model-resolver.js.map +1 -1
  113. package/dist/core/package-manager.d.ts +25 -57
  114. package/dist/core/package-manager.d.ts.map +1 -1
  115. package/dist/core/package-manager.js +326 -964
  116. package/dist/core/package-manager.js.map +1 -1
  117. package/dist/core/prompt-templates.d.ts +2 -34
  118. package/dist/core/prompt-templates.d.ts.map +1 -1
  119. package/dist/core/prompt-templates.js +122 -170
  120. package/dist/core/prompt-templates.js.map +1 -1
  121. package/dist/core/resource-loader.d.ts +19 -12
  122. package/dist/core/resource-loader.d.ts.map +1 -1
  123. package/dist/core/resource-loader.js +353 -467
  124. package/dist/core/resource-loader.js.map +1 -1
  125. package/dist/core/sdk.d.ts +2 -61
  126. package/dist/core/sdk.d.ts.map +1 -1
  127. package/dist/core/sdk.js +184 -252
  128. package/dist/core/sdk.js.map +1 -1
  129. package/dist/core/session-manager.d.ts +447 -1
  130. package/dist/core/session-manager.d.ts.map +1 -1
  131. package/dist/core/session-manager.js +1176 -1
  132. package/dist/core/session-manager.js.map +1 -1
  133. package/dist/core/settings-manager.d.ts +9 -12
  134. package/dist/core/settings-manager.d.ts.map +1 -1
  135. package/dist/core/settings-manager.js +170 -398
  136. package/dist/core/settings-manager.js.map +1 -1
  137. package/dist/core/skills.d.ts +2 -27
  138. package/dist/core/skills.d.ts.map +1 -1
  139. package/dist/core/skills.js +149 -212
  140. package/dist/core/skills.js.map +1 -1
  141. package/dist/core/subagents.d.ts +2 -2
  142. package/dist/core/subagents.d.ts.map +1 -1
  143. package/dist/core/subagents.js +21 -14
  144. package/dist/core/subagents.js.map +1 -1
  145. package/dist/core/system-prompt.d.ts +0 -11
  146. package/dist/core/system-prompt.d.ts.map +1 -1
  147. package/dist/core/system-prompt.js +168 -139
  148. package/dist/core/system-prompt.js.map +1 -1
  149. package/dist/core/timings.d.ts +1 -4
  150. package/dist/core/timings.d.ts.map +1 -1
  151. package/dist/core/timings.js +34 -18
  152. package/dist/core/timings.js.map +1 -1
  153. package/dist/core/todo-store.d.ts +20 -0
  154. package/dist/core/todo-store.d.ts.map +1 -0
  155. package/dist/core/todo-store.js +60 -0
  156. package/dist/core/todo-store.js.map +1 -0
  157. package/dist/core/tools/bash.d.ts +2 -0
  158. package/dist/core/tools/bash.d.ts.map +1 -0
  159. package/dist/core/tools/bash.js +2 -0
  160. package/dist/core/tools/bash.js.map +1 -0
  161. package/dist/core/tools/bg-process.d.ts +1 -6
  162. package/dist/core/tools/bg-process.d.ts.map +1 -1
  163. package/dist/core/tools/bg-process.js +4 -18
  164. package/dist/core/tools/bg-process.js.map +1 -1
  165. package/dist/core/tools/edit-diff.d.ts +9 -0
  166. package/dist/core/tools/edit-diff.d.ts.map +1 -0
  167. package/dist/core/tools/edit-diff.js +2 -0
  168. package/dist/core/tools/edit-diff.js.map +1 -0
  169. package/dist/core/tools/edit.d.ts +2 -0
  170. package/dist/core/tools/edit.d.ts.map +1 -0
  171. package/dist/core/tools/edit.js +2 -0
  172. package/dist/core/tools/edit.js.map +1 -0
  173. package/dist/core/tools/find.d.ts +2 -0
  174. package/dist/core/tools/find.d.ts.map +1 -0
  175. package/dist/core/tools/find.js +2 -0
  176. package/dist/core/tools/find.js.map +1 -0
  177. package/dist/core/tools/grep.d.ts +2 -0
  178. package/dist/core/tools/grep.d.ts.map +1 -0
  179. package/dist/core/tools/grep.js +2 -0
  180. package/dist/core/tools/grep.js.map +1 -0
  181. package/dist/core/tools/index.d.ts +7 -1
  182. package/dist/core/tools/index.d.ts.map +1 -1
  183. package/dist/core/tools/ls.d.ts +2 -0
  184. package/dist/core/tools/ls.d.ts.map +1 -0
  185. package/dist/core/tools/ls.js +2 -0
  186. package/dist/core/tools/ls.js.map +1 -0
  187. package/dist/core/tools/path-utils.d.ts +2 -0
  188. package/dist/core/tools/path-utils.d.ts.map +1 -0
  189. package/dist/core/tools/path-utils.js +2 -0
  190. package/dist/core/tools/path-utils.js.map +1 -0
  191. package/dist/core/tools/read.d.ts +2 -0
  192. package/dist/core/tools/read.d.ts.map +1 -0
  193. package/dist/core/tools/read.js +2 -0
  194. package/dist/core/tools/read.js.map +1 -0
  195. package/dist/core/tools/registry.d.ts +0 -15
  196. package/dist/core/tools/registry.d.ts.map +1 -1
  197. package/dist/core/tools/registry.js +13 -37
  198. package/dist/core/tools/registry.js.map +1 -1
  199. package/dist/core/tools/task.d.ts +17 -23
  200. package/dist/core/tools/task.d.ts.map +1 -1
  201. package/dist/core/tools/task.js +43 -82
  202. package/dist/core/tools/task.js.map +1 -1
  203. package/dist/core/tools/todo.d.ts +17 -20
  204. package/dist/core/tools/todo.d.ts.map +1 -1
  205. package/dist/core/tools/todo.js +79 -58
  206. package/dist/core/tools/todo.js.map +1 -1
  207. package/dist/core/tools/truncate.d.ts +2 -0
  208. package/dist/core/tools/truncate.d.ts.map +1 -0
  209. package/dist/core/tools/truncate.js +2 -0
  210. package/dist/core/tools/truncate.js.map +1 -0
  211. package/dist/core/tools/webfetch.d.ts +2 -0
  212. package/dist/core/tools/webfetch.d.ts.map +1 -0
  213. package/dist/core/tools/webfetch.js +2 -0
  214. package/dist/core/tools/webfetch.js.map +1 -0
  215. package/dist/core/tools/websearch.d.ts +2 -0
  216. package/dist/core/tools/websearch.d.ts.map +1 -0
  217. package/dist/core/tools/websearch.js +2 -0
  218. package/dist/core/tools/websearch.js.map +1 -0
  219. package/dist/core/tools/write.d.ts +2 -0
  220. package/dist/core/tools/write.d.ts.map +1 -0
  221. package/dist/core/tools/write.js +2 -0
  222. package/dist/core/tools/write.js.map +1 -0
  223. package/dist/index.d.ts +35 -29
  224. package/dist/index.d.ts.map +1 -1
  225. package/dist/index.js +33 -41
  226. package/dist/index.js.map +1 -1
  227. package/dist/main.d.ts.map +1 -1
  228. package/dist/main.js +237 -225
  229. package/dist/main.js.map +1 -1
  230. package/dist/migrations.d.ts +0 -25
  231. package/dist/migrations.d.ts.map +1 -1
  232. package/dist/migrations.js +129 -180
  233. package/dist/migrations.js.map +1 -1
  234. package/dist/modes/index.d.ts +13 -6
  235. package/dist/modes/index.d.ts.map +1 -1
  236. package/dist/modes/index.js +11 -5
  237. package/dist/modes/index.js.map +1 -1
  238. package/dist/modes/interactive/components/armin.d.ts +8 -23
  239. package/dist/modes/interactive/components/armin.d.ts.map +1 -1
  240. package/dist/modes/interactive/components/armin.js +217 -266
  241. package/dist/modes/interactive/components/armin.js.map +1 -1
  242. package/dist/modes/interactive/components/assistant-message.d.ts +2 -168
  243. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  244. package/dist/modes/interactive/components/assistant-message.js +61 -216
  245. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  246. package/dist/modes/interactive/components/bash-execution.d.ts +6 -313
  247. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  248. package/dist/modes/interactive/components/bash-execution.js +86 -403
  249. package/dist/modes/interactive/components/bash-execution.js.map +1 -1
  250. package/dist/modes/interactive/components/bordered-loader.d.ts +1 -3
  251. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -1
  252. package/dist/modes/interactive/components/bordered-loader.js +59 -29
  253. package/dist/modes/interactive/components/bordered-loader.js.map +1 -1
  254. package/dist/modes/interactive/components/branch-summary-message.d.ts +3 -3
  255. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -1
  256. package/dist/modes/interactive/components/branch-summary-message.js +30 -17
  257. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -1
  258. package/dist/modes/interactive/components/compaction-summary-message.d.ts +3 -3
  259. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -1
  260. package/dist/modes/interactive/components/compaction-summary-message.js +35 -18
  261. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -1
  262. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
  263. package/dist/modes/interactive/components/config-selector.js +60 -68
  264. package/dist/modes/interactive/components/config-selector.js.map +1 -1
  265. package/dist/modes/interactive/components/countdown-timer.d.ts +2 -6
  266. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -1
  267. package/dist/modes/interactive/components/countdown-timer.js +32 -18
  268. package/dist/modes/interactive/components/countdown-timer.js.map +1 -1
  269. package/dist/modes/interactive/components/custom-editor.d.ts +1 -5
  270. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
  271. package/dist/modes/interactive/components/custom-editor.js +45 -37
  272. package/dist/modes/interactive/components/custom-editor.js.map +1 -1
  273. package/dist/modes/interactive/components/custom-message.d.ts +5 -6
  274. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
  275. package/dist/modes/interactive/components/custom-message.js +43 -53
  276. package/dist/modes/interactive/components/custom-message.js.map +1 -1
  277. package/dist/modes/interactive/components/diff.d.ts +0 -3
  278. package/dist/modes/interactive/components/diff.d.ts.map +1 -1
  279. package/dist/modes/interactive/components/diff.js +101 -108
  280. package/dist/modes/interactive/components/diff.js.map +1 -1
  281. package/dist/modes/interactive/components/dynamic-border.d.ts +7 -2
  282. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -1
  283. package/dist/modes/interactive/components/dynamic-border.js +24 -4
  284. package/dist/modes/interactive/components/dynamic-border.js.map +1 -1
  285. package/dist/modes/interactive/components/extension-editor.d.ts +5 -9
  286. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -1
  287. package/dist/modes/interactive/components/extension-editor.js +43 -46
  288. package/dist/modes/interactive/components/extension-editor.js.map +1 -1
  289. package/dist/modes/interactive/components/extension-input.d.ts +4 -9
  290. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -1
  291. package/dist/modes/interactive/components/extension-input.js +45 -28
  292. package/dist/modes/interactive/components/extension-input.js.map +1 -1
  293. package/dist/modes/interactive/components/extension-selector.d.ts +7 -12
  294. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -1
  295. package/dist/modes/interactive/components/extension-selector.js +54 -32
  296. package/dist/modes/interactive/components/extension-selector.js.map +1 -1
  297. package/dist/modes/interactive/components/footer.d.ts +2 -11
  298. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  299. package/dist/modes/interactive/components/footer.js +135 -183
  300. package/dist/modes/interactive/components/footer.js.map +1 -1
  301. package/dist/modes/interactive/components/index.d.ts +79 -30
  302. package/dist/modes/interactive/components/index.d.ts.map +1 -1
  303. package/dist/modes/interactive/components/index.js +73 -31
  304. package/dist/modes/interactive/components/index.js.map +1 -1
  305. package/dist/modes/interactive/components/keybinding-hints.d.ts +0 -18
  306. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -1
  307. package/dist/modes/interactive/components/keybinding-hints.js +24 -31
  308. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -1
  309. package/dist/modes/interactive/components/login-dialog.d.ts +11 -24
  310. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  311. package/dist/modes/interactive/components/login-dialog.js +89 -86
  312. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  313. package/dist/modes/interactive/components/model-selector.d.ts +15 -19
  314. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
  315. package/dist/modes/interactive/components/model-selector.js +104 -157
  316. package/dist/modes/interactive/components/model-selector.js.map +1 -1
  317. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  318. package/dist/modes/interactive/components/oauth-selector.js +5 -5
  319. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  320. package/dist/modes/interactive/components/scoped-models-selector.d.ts +12 -22
  321. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  322. package/dist/modes/interactive/components/scoped-models-selector.js +111 -132
  323. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  324. package/dist/modes/interactive/components/session-selector-search.d.ts +3 -3
  325. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -1
  326. package/dist/modes/interactive/components/session-selector-search.js +92 -103
  327. package/dist/modes/interactive/components/session-selector-search.js.map +1 -1
  328. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  329. package/dist/modes/interactive/components/session-selector.js +28 -39
  330. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  331. package/dist/modes/interactive/components/settings-selector.d.ts +1 -1
  332. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  333. package/dist/modes/interactive/components/settings-selector.js +111 -203
  334. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  335. package/dist/modes/interactive/components/show-images-selector.d.ts +1 -1
  336. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -1
  337. package/dist/modes/interactive/components/show-images-selector.js +17 -19
  338. package/dist/modes/interactive/components/show-images-selector.js.map +1 -1
  339. package/dist/modes/interactive/components/skill-invocation-message.d.ts +3 -3
  340. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -1
  341. package/dist/modes/interactive/components/skill-invocation-message.js +29 -19
  342. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -1
  343. package/dist/modes/interactive/components/theme-selector.d.ts +2 -2
  344. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -1
  345. package/dist/modes/interactive/components/theme-selector.js +20 -25
  346. package/dist/modes/interactive/components/theme-selector.js.map +1 -1
  347. package/dist/modes/interactive/components/thinking-selector.d.ts +1 -1
  348. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -1
  349. package/dist/modes/interactive/components/thinking-selector.js +19 -20
  350. package/dist/modes/interactive/components/thinking-selector.js.map +1 -1
  351. package/dist/modes/interactive/components/tool-execution.d.ts +12 -10
  352. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  353. package/dist/modes/interactive/components/tool-execution.js +14 -8
  354. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  355. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
  356. package/dist/modes/interactive/components/tree-selector.js +53 -60
  357. package/dist/modes/interactive/components/tree-selector.js.map +1 -1
  358. package/dist/modes/interactive/components/user-message-selector.d.ts +3 -9
  359. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -1
  360. package/dist/modes/interactive/components/user-message-selector.js +57 -68
  361. package/dist/modes/interactive/components/user-message-selector.js.map +1 -1
  362. package/dist/modes/interactive/components/visual-truncate.d.ts +0 -12
  363. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -1
  364. package/dist/modes/interactive/components/visual-truncate.js +14 -22
  365. package/dist/modes/interactive/components/visual-truncate.js.map +1 -1
  366. package/dist/modes/interactive/interactive-mode.d.ts +6 -0
  367. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  368. package/dist/modes/interactive/interactive-mode.js +118 -113
  369. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  370. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  371. package/dist/modes/interactive/theme/theme.js +189 -39
  372. package/dist/modes/interactive/theme/theme.js.map +1 -1
  373. package/dist/modes/rpc/rpc-client.d.ts +8 -1
  374. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  375. package/dist/modes/rpc/rpc-client.js +88 -59
  376. package/dist/modes/rpc/rpc-client.js.map +1 -1
  377. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  378. package/dist/modes/rpc/rpc-mode.js +24 -46
  379. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  380. package/dist/modes/rpc/rpc-types.d.ts +75 -409
  381. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  382. package/dist/modes/rpc/rpc-types.js +21 -360
  383. package/dist/modes/rpc/rpc-types.js.map +1 -1
  384. package/dist/observability/core/config.d.ts +28 -0
  385. package/dist/observability/core/config.d.ts.map +1 -0
  386. package/dist/observability/core/config.js +150 -0
  387. package/dist/observability/core/config.js.map +1 -0
  388. package/dist/observability/core/event-bus.d.ts +15 -0
  389. package/dist/observability/core/event-bus.d.ts.map +1 -0
  390. package/dist/observability/core/event-bus.js +37 -0
  391. package/dist/observability/core/event-bus.js.map +1 -0
  392. package/dist/observability/core/index.d.ts +12 -0
  393. package/dist/observability/core/index.d.ts.map +1 -0
  394. package/dist/observability/core/index.js +14 -0
  395. package/dist/observability/core/index.js.map +1 -0
  396. package/dist/observability/core/observability.d.ts +63 -0
  397. package/dist/observability/core/observability.d.ts.map +1 -0
  398. package/dist/observability/core/observability.js +127 -0
  399. package/dist/observability/core/observability.js.map +1 -0
  400. package/dist/observability/core/span.d.ts +37 -0
  401. package/dist/observability/core/span.d.ts.map +1 -0
  402. package/dist/observability/core/span.js +90 -0
  403. package/dist/observability/core/span.js.map +1 -0
  404. package/dist/observability/core/tracer.d.ts +22 -0
  405. package/dist/observability/core/tracer.d.ts.map +1 -0
  406. package/dist/observability/core/tracer.js +79 -0
  407. package/dist/observability/core/tracer.js.map +1 -0
  408. package/dist/observability/core/types.d.ts +155 -0
  409. package/dist/observability/core/types.d.ts.map +1 -0
  410. package/dist/observability/core/types.js +38 -0
  411. package/dist/observability/core/types.js.map +1 -0
  412. package/dist/observability/exporters/base-exporter.d.ts +16 -0
  413. package/dist/observability/exporters/base-exporter.d.ts.map +1 -0
  414. package/dist/observability/exporters/base-exporter.js +26 -0
  415. package/dist/observability/exporters/base-exporter.js.map +1 -0
  416. package/dist/observability/exporters/console-exporter.d.ts +22 -0
  417. package/dist/observability/exporters/console-exporter.d.ts.map +1 -0
  418. package/dist/observability/exporters/console-exporter.js +80 -0
  419. package/dist/observability/exporters/console-exporter.js.map +1 -0
  420. package/dist/observability/exporters/file-exporter.d.ts +31 -0
  421. package/dist/observability/exporters/file-exporter.d.ts.map +1 -0
  422. package/dist/observability/exporters/file-exporter.js +120 -0
  423. package/dist/observability/exporters/file-exporter.js.map +1 -0
  424. package/dist/observability/exporters/index.d.ts +12 -0
  425. package/dist/observability/exporters/index.d.ts.map +1 -0
  426. package/dist/observability/exporters/index.js +11 -0
  427. package/dist/observability/exporters/index.js.map +1 -0
  428. package/dist/observability/exporters/langfuse-exporter.d.ts +27 -0
  429. package/dist/observability/exporters/langfuse-exporter.d.ts.map +1 -0
  430. package/dist/observability/exporters/langfuse-exporter.js +146 -0
  431. package/dist/observability/exporters/langfuse-exporter.js.map +1 -0
  432. package/dist/observability/exporters/sentry-exporter.d.ts +22 -0
  433. package/dist/observability/exporters/sentry-exporter.d.ts.map +1 -0
  434. package/dist/observability/exporters/sentry-exporter.js +121 -0
  435. package/dist/observability/exporters/sentry-exporter.js.map +1 -0
  436. package/dist/observability/index.d.ts +3 -0
  437. package/dist/observability/index.d.ts.map +1 -0
  438. package/dist/observability/index.js +3 -0
  439. package/dist/observability/index.js.map +1 -0
  440. package/dist/utils/changelog.d.ts +1 -2
  441. package/dist/utils/changelog.d.ts.map +1 -1
  442. package/dist/utils/changelog.js +53 -61
  443. package/dist/utils/changelog.js.map +1 -1
  444. package/dist/utils/clipboard-image.d.ts.map +1 -1
  445. package/dist/utils/clipboard-image.js +77 -83
  446. package/dist/utils/clipboard-image.js.map +1 -1
  447. package/dist/utils/clipboard.d.ts.map +1 -1
  448. package/dist/utils/clipboard.js +62 -49
  449. package/dist/utils/clipboard.js.map +1 -1
  450. package/dist/utils/image-convert.d.ts +6 -6
  451. package/dist/utils/image-convert.d.ts.map +1 -1
  452. package/dist/utils/image-convert.js +29 -23
  453. package/dist/utils/image-convert.js.map +1 -1
  454. package/dist/utils/image-resize.d.ts +0 -17
  455. package/dist/utils/image-resize.d.ts.map +1 -1
  456. package/dist/utils/image-resize.js +100 -138
  457. package/dist/utils/image-resize.js.map +1 -1
  458. package/dist/utils/mime.d.ts +1 -0
  459. package/dist/utils/mime.d.ts.map +1 -1
  460. package/dist/utils/mime.js +35 -15
  461. package/dist/utils/mime.js.map +1 -1
  462. package/dist/utils/photon.d.ts +4 -15
  463. package/dist/utils/photon.d.ts.map +1 -1
  464. package/dist/utils/photon.js +71 -60
  465. package/dist/utils/photon.js.map +1 -1
  466. package/dist/utils/shell.d.ts +4 -21
  467. package/dist/utils/shell.d.ts.map +1 -1
  468. package/dist/utils/shell.js +101 -124
  469. package/dist/utils/shell.js.map +1 -1
  470. package/dist/utils/sleep.d.ts +1 -1
  471. package/dist/utils/sleep.d.ts.map +1 -1
  472. package/dist/utils/sleep.js +32 -8
  473. package/dist/utils/sleep.js.map +1 -1
  474. package/dist/utils/tools-manager.d.ts +4 -2
  475. package/dist/utils/tools-manager.d.ts.map +1 -1
  476. package/dist/utils/tools-manager.js +96 -122
  477. package/dist/utils/tools-manager.js.map +1 -1
  478. package/docs/PI_MONO_MIT_REMOVAL_GUIDE.md +2132 -0
  479. package/docs/SAME_TO_SAME_PARITY_REPORT.md +312 -0
  480. package/examples/README.md +12 -0
  481. package/package.json +60 -88
  482. package/LICENSE.md +0 -22
  483. package/dist/modes/interactive/theme/dark.json +0 -85
  484. package/dist/modes/interactive/theme/light.json +0 -84
  485. package/dist/modes/interactive/theme/theme-schema.json +0 -335
  486. package/docs/FEATURES.md +0 -306
  487. package/docs/MCP.md +0 -341
  488. package/docs/MEMORY.md +0 -443
  489. package/examples/mcp-servers.example.json +0 -50
@@ -1,2 +1,1177 @@
1
- export * from "indusagi/agent";
1
+ /**
2
+ * Session Manager - Persistent session storage and retrieval
3
+ *
4
+ * Purpose:
5
+ * Manages agent session persistence using JSONL (JSON Lines) format for append-only,
6
+ * transaction-safe storage. Handles session loading, saving, branching, and compaction.
7
+ *
8
+ * Based on conversation persistence patterns from Anthropic's Claude API
9
+ * and JSONL formats from common chat applications (ChatGPT, Claude.ai).
10
+ * Refactored for agent session branching and compaction.
11
+ *
12
+ * Architecture:
13
+ * - Session File Format: JSONL (one JSON object per line)
14
+ * - Storage Location: ~/.indusagi/sessions/ (or INDUSAGI_DIR/sessions/)
15
+ * - Entry Types: Messages, model changes, thinking level changes, compaction markers
16
+ * - Concurrency: Append-only prevents write conflicts
17
+ * - Recovery: Can rebuild state from session entry log
18
+ *
19
+ * Key Features:
20
+ * - Append-only session files for safe concurrent access
21
+ * - Session branching (fork a session at any point)
22
+ * - Entry types for different session events
23
+ * - Compaction metadata tracking
24
+ * - Custom entry support for extensions
25
+ * - Session validation and recovery
26
+ * - Efficient file operations (streaming read for large sessions)
27
+ *
28
+ * Session File Format:
29
+ * ```
30
+ * {"type":"session","version":3,"id":"abc123","timestamp":"2025-02-21T...","cwd":"/path","parentSession":"parent-id"}
31
+ * {"type":"message","id":"msg1","parentId":null,"timestamp":"2025-02-21T...","message":{...}}
32
+ * {"type":"message","id":"msg2","parentId":"msg1","timestamp":"2025-02-21T...","message":{...}}
33
+ * {"type":"model_change","id":"mc1","parentId":"msg2","timestamp":"2025-02-21T...","provider":"openai","modelId":"gpt-4"}
34
+ * {"type":"thinking_level_change","id":"tlc1","parentId":"mc1","timestamp":"2025-02-21T...","thinkingLevel":"high"}
35
+ * {"type":"compaction","id":"comp1","parentId":"tlc1","timestamp":"2025-02-21T...","summary":"...","firstKeptEntryId":"msg2","tokensBefore":5000}
36
+ * ```
37
+ *
38
+ * Entry Types:
39
+ * - "message" - Agent or user message
40
+ * - "model_change" - Model was switched
41
+ * - "thinking_level_change" - Thinking level was adjusted
42
+ * - "compaction" - Session was compacted with summary
43
+ * - "branch_summary" - Branch point summary
44
+ * - "custom" - Extension-specific data
45
+ *
46
+ * Session State Reconstruction:
47
+ * 1. Load session header (first line)
48
+ * 2. Read all entries sequentially
49
+ * 3. Build message list from "message" entries
50
+ * 4. Track latest "model_change" for current model
51
+ * 5. Track latest "thinking_level_change" for current level
52
+ * 6. Use "compaction" markers to know what was summarized
53
+ * 7. Ignore "branch_summary" and "custom" entries from context
54
+ *
55
+ * Session Branching:
56
+ * ```typescript
57
+ * // Load existing session
58
+ * const session = await loadSession("original-id");
59
+ *
60
+ * // Create branch at current point
61
+ * const branchId = randomUUID();
62
+ * const newSession = createNewSession(branchId, { parentSession: "original-id" });
63
+ *
64
+ * // Messages after branch are separate from original
65
+ * ```
66
+ *
67
+ * Compaction Process:
68
+ * - Summarize old messages into summary text
69
+ * - Write CompactionEntry with summary and metadata
70
+ * - Keep all entries for recovery
71
+ * - LLM context uses summary instead of original messages
72
+ *
73
+ * Large File Handling:
74
+ * - Sessions can be > 1GB
75
+ * - Use streaming/chunked reads for file access
76
+ * - Append operations are efficient (seek to EOF + write)
77
+ * - Supports pagination for large message lists
78
+ *
79
+ * Error Handling:
80
+ * - Malformed entries: Logged and skipped
81
+ * - Incomplete files: Recovery via last valid entry
82
+ * - Missing sessions: Returns undefined
83
+ * - File permission errors: Throws with context
84
+ *
85
+ * Usage Examples:
86
+ * ```typescript
87
+ * // Create new session
88
+ * const sessionId = randomUUID();
89
+ * const header = createSessionHeader(sessionId, process.cwd());
90
+ * await saveSessionEntry(sessionId, header);
91
+ *
92
+ * // Add messages
93
+ * const message: AgentMessage = { role: "user", content: "..." };
94
+ * await saveSessionEntry(sessionId, {
95
+ * type: "message",
96
+ * id: randomUUID(),
97
+ * parentId: null,
98
+ * timestamp: new Date().toISOString(),
99
+ * message
100
+ * });
101
+ *
102
+ * // Load session
103
+ * const loaded = await loadSession(sessionId);
104
+ * console.log(loaded.messages.length);
105
+ *
106
+ * // List all sessions
107
+ * const all = await listSessions();
108
+ * ```
109
+ *
110
+ * File Locations:
111
+ * - Session directory: ~/.indusagi/sessions/
112
+ * - Session file: ~/.indusagi/sessions/{sessionId}.jsonl
113
+ * - Can override with INDUSAGI_DIR environment variable
114
+ *
115
+ * Thread Safety:
116
+ * - JSONL format allows append-only writes
117
+ * - No explicit locking needed
118
+ * - Multiple processes can append safely
119
+ * - Reads may see partial entries (handle gracefully)
120
+ *
121
+ * Based on session manager from indusagi-agent
122
+ * Original: https://github.com/varunisrani/indusagi-ts/vendor/indusagi-agent/src/session-manager.ts
123
+ */
124
+ import { randomUUID } from "crypto";
125
+ import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, statSync, writeFileSync, } from "fs";
126
+ import { readdir, readFile, stat } from "fs/promises";
127
+ import { join, resolve } from "path";
128
+ import { getAgentDir as getDefaultAgentDir, getSessionsDir } from "../config.js";
129
+ import { createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage, } from "./messages.js";
130
+ export const CURRENT_SESSION_VERSION = 3;
131
+ /** Generate a unique short ID (8 hex chars, collision-checked) */
132
+ function generateId(byId) {
133
+ for (let i = 0; i < 100; i++) {
134
+ const id = randomUUID().slice(0, 8);
135
+ if (!byId.has(id))
136
+ return id;
137
+ }
138
+ // Fallback to full UUID if somehow we have collisions
139
+ return randomUUID();
140
+ }
141
+ function readSessionEntries(lines) {
142
+ const entries = [];
143
+ for (const line of lines) {
144
+ if (!line.trim())
145
+ continue;
146
+ try {
147
+ entries.push(JSON.parse(line));
148
+ }
149
+ catch {
150
+ // Skip malformed lines.
151
+ }
152
+ }
153
+ return entries;
154
+ }
155
+ function readSessionHeader(entries) {
156
+ const header = entries[0];
157
+ if (!header || header.type !== "session" || typeof header.id !== "string") {
158
+ return undefined;
159
+ }
160
+ return header;
161
+ }
162
+ function buildEntryIndex(entries) {
163
+ const byId = new Map();
164
+ const labelsById = new Map();
165
+ let leafId = null;
166
+ for (const entry of entries) {
167
+ if (entry.type === "session")
168
+ continue;
169
+ byId.set(entry.id, entry);
170
+ leafId = entry.id;
171
+ if (entry.type === "label") {
172
+ if (entry.label)
173
+ labelsById.set(entry.targetId, entry.label);
174
+ else
175
+ labelsById.delete(entry.targetId);
176
+ }
177
+ }
178
+ return { byId, labelsById, leafId };
179
+ }
180
+ function resolveBranchPath(byId, fromId) {
181
+ const path = [];
182
+ let current = fromId ? byId.get(fromId) : undefined;
183
+ while (current) {
184
+ path.unshift(current);
185
+ current = current.parentId ? byId.get(current.parentId) : undefined;
186
+ }
187
+ return path;
188
+ }
189
+ function buildSessionTree(entries, labelsById) {
190
+ const nodeMap = new Map();
191
+ const roots = [];
192
+ for (const entry of entries) {
193
+ nodeMap.set(entry.id, { entry, children: [], label: labelsById.get(entry.id) });
194
+ }
195
+ for (const entry of entries) {
196
+ const node = nodeMap.get(entry.id);
197
+ if (entry.parentId === null || entry.parentId === entry.id) {
198
+ roots.push(node);
199
+ continue;
200
+ }
201
+ const parent = nodeMap.get(entry.parentId);
202
+ if (parent)
203
+ parent.children.push(node);
204
+ else
205
+ roots.push(node);
206
+ }
207
+ const stack = [...roots];
208
+ while (stack.length > 0) {
209
+ const node = stack.pop();
210
+ node.children.sort((a, b) => new Date(a.entry.timestamp).getTime() - new Date(b.entry.timestamp).getTime());
211
+ stack.push(...node.children);
212
+ }
213
+ return roots;
214
+ }
215
+ function appendSessionEntry(sessionFile, entry) {
216
+ appendFileSync(sessionFile, `${JSON.stringify(entry)}\n`);
217
+ }
218
+ /** Migrate v1 → v2: add id/parentId tree structure. Mutates in place. */
219
+ function migrateV1ToV2(entries) {
220
+ const ids = new Set();
221
+ let prevId = null;
222
+ for (const entry of entries) {
223
+ if (entry.type === "session") {
224
+ entry.version = 2;
225
+ continue;
226
+ }
227
+ entry.id = generateId(ids);
228
+ entry.parentId = prevId;
229
+ prevId = entry.id;
230
+ // Convert firstKeptEntryIndex to firstKeptEntryId for compaction
231
+ if (entry.type === "compaction") {
232
+ const comp = entry;
233
+ if (typeof comp.firstKeptEntryIndex === "number") {
234
+ const targetEntry = entries[comp.firstKeptEntryIndex];
235
+ if (targetEntry && targetEntry.type !== "session") {
236
+ comp.firstKeptEntryId = targetEntry.id;
237
+ }
238
+ delete comp.firstKeptEntryIndex;
239
+ }
240
+ }
241
+ }
242
+ }
243
+ /** Migrate v2 → v3: rename hookMessage role to custom. Mutates in place. */
244
+ function migrateV2ToV3(entries) {
245
+ for (const entry of entries) {
246
+ if (entry.type === "session") {
247
+ entry.version = 3;
248
+ continue;
249
+ }
250
+ // Update message entries with hookMessage role
251
+ if (entry.type === "message") {
252
+ const msgEntry = entry;
253
+ if (msgEntry.message && msgEntry.message.role === "hookMessage") {
254
+ msgEntry.message.role = "custom";
255
+ }
256
+ }
257
+ }
258
+ }
259
+ /**
260
+ * Run all necessary migrations to bring entries to current version.
261
+ * Mutates entries in place. Returns true if any migration was applied.
262
+ */
263
+ function migrateToCurrentVersion(entries) {
264
+ const header = entries.find((e) => e.type === "session");
265
+ const version = header?.version ?? 1;
266
+ if (version >= CURRENT_SESSION_VERSION)
267
+ return false;
268
+ if (version < 2)
269
+ migrateV1ToV2(entries);
270
+ if (version < 3)
271
+ migrateV2ToV3(entries);
272
+ return true;
273
+ }
274
+ /** Exported for testing */
275
+ export function migrateSessionEntries(entries) {
276
+ migrateToCurrentVersion(entries);
277
+ }
278
+ /** Exported for compaction.test.ts */
279
+ export function parseSessionEntries(content) {
280
+ return readSessionEntries(content.trim().split("\n"));
281
+ }
282
+ export function getLatestCompactionEntry(entries) {
283
+ for (let i = entries.length - 1; i >= 0; i--) {
284
+ if (entries[i].type === "compaction") {
285
+ return entries[i];
286
+ }
287
+ }
288
+ return null;
289
+ }
290
+ /**
291
+ * Build the session context from entries using tree traversal.
292
+ * If leafId is provided, walks from that entry to root.
293
+ * Handles compaction and branch summaries along the path.
294
+ */
295
+ export function buildSessionContext(entries, leafId, byId) {
296
+ // Build uuid index if not available
297
+ if (!byId) {
298
+ byId = new Map();
299
+ for (const entry of entries) {
300
+ byId.set(entry.id, entry);
301
+ }
302
+ }
303
+ // Find leaf
304
+ let leaf;
305
+ if (leafId === null) {
306
+ // Explicitly null - return no messages (navigated to before first entry)
307
+ return { messages: [], thinkingLevel: "off", model: null };
308
+ }
309
+ if (leafId) {
310
+ leaf = byId.get(leafId);
311
+ }
312
+ if (!leaf) {
313
+ // Fallback to last entry (when leafId is undefined)
314
+ leaf = entries[entries.length - 1];
315
+ }
316
+ if (!leaf) {
317
+ return { messages: [], thinkingLevel: "off", model: null };
318
+ }
319
+ // Walk from leaf to root, collecting path
320
+ const path = resolveBranchPath(byId, leaf.id);
321
+ // Extract settings and find compaction
322
+ let thinkingLevel = "off";
323
+ let model = null;
324
+ let compaction = null;
325
+ for (const entry of path) {
326
+ if (entry.type === "thinking_level_change") {
327
+ thinkingLevel = entry.thinkingLevel;
328
+ }
329
+ else if (entry.type === "model_change") {
330
+ model = { provider: entry.provider, modelId: entry.modelId };
331
+ }
332
+ else if (entry.type === "message" && entry.message.role === "assistant") {
333
+ model = { provider: entry.message.provider, modelId: entry.message.model };
334
+ }
335
+ else if (entry.type === "compaction") {
336
+ compaction = entry;
337
+ }
338
+ }
339
+ // Build messages and collect corresponding entries
340
+ // When there's a compaction, we need to:
341
+ // 1. Emit summary first (entry = compaction)
342
+ // 2. Emit kept messages (from firstKeptEntryId up to compaction)
343
+ // 3. Emit messages after compaction
344
+ const messages = [];
345
+ const appendMessage = (entry) => {
346
+ if (entry.type === "message") {
347
+ messages.push(entry.message);
348
+ }
349
+ else if (entry.type === "custom_message") {
350
+ messages.push(createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp));
351
+ }
352
+ else if (entry.type === "branch_summary" && entry.summary) {
353
+ messages.push(createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp));
354
+ }
355
+ };
356
+ if (compaction) {
357
+ // Emit summary first
358
+ messages.push(createCompactionSummaryMessage(compaction.summary, compaction.tokensBefore, compaction.timestamp));
359
+ // Find compaction index in path
360
+ const compactionIdx = path.findIndex((e) => e.type === "compaction" && e.id === compaction.id);
361
+ // Emit kept messages (before compaction, starting from firstKeptEntryId)
362
+ let foundFirstKept = false;
363
+ for (let i = 0; i < compactionIdx; i++) {
364
+ const entry = path[i];
365
+ if (entry.id === compaction.firstKeptEntryId) {
366
+ foundFirstKept = true;
367
+ }
368
+ if (foundFirstKept) {
369
+ appendMessage(entry);
370
+ }
371
+ }
372
+ // Emit messages after compaction
373
+ for (let i = compactionIdx + 1; i < path.length; i++) {
374
+ const entry = path[i];
375
+ appendMessage(entry);
376
+ }
377
+ }
378
+ else {
379
+ // No compaction - emit all messages, handle branch summaries and custom messages
380
+ for (const entry of path) {
381
+ appendMessage(entry);
382
+ }
383
+ }
384
+ return { messages, thinkingLevel, model };
385
+ }
386
+ /**
387
+ * Compute the default session directory for a cwd.
388
+ * Encodes cwd into a safe directory name under ~/.indusagi/agent/sessions/.
389
+ */
390
+ function getDefaultSessionDir(cwd) {
391
+ const safePath = `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
392
+ const sessionDir = join(getDefaultAgentDir(), "sessions", safePath);
393
+ if (!existsSync(sessionDir)) {
394
+ mkdirSync(sessionDir, { recursive: true });
395
+ }
396
+ return sessionDir;
397
+ }
398
+ /** Exported for testing */
399
+ export function loadEntriesFromFile(filePath) {
400
+ if (!existsSync(filePath))
401
+ return [];
402
+ const entries = readSessionEntries(readFileSync(filePath, "utf8").trim().split("\n"));
403
+ return readSessionHeader(entries) ? entries : [];
404
+ }
405
+ function isValidSessionFile(filePath) {
406
+ try {
407
+ const fd = openSync(filePath, "r");
408
+ const buffer = Buffer.alloc(512);
409
+ const bytesRead = readSync(fd, buffer, 0, 512, 0);
410
+ closeSync(fd);
411
+ const firstLine = buffer.toString("utf8", 0, bytesRead).split("\n")[0];
412
+ if (!firstLine)
413
+ return false;
414
+ const header = JSON.parse(firstLine);
415
+ return header.type === "session" && typeof header.id === "string";
416
+ }
417
+ catch {
418
+ return false;
419
+ }
420
+ }
421
+ /** Exported for testing */
422
+ export function findMostRecentSession(sessionDir) {
423
+ try {
424
+ const files = readdirSync(sessionDir)
425
+ .filter((f) => f.endsWith(".jsonl"))
426
+ .map((f) => join(sessionDir, f))
427
+ .filter(isValidSessionFile)
428
+ .map((path) => ({ path, mtime: statSync(path).mtime }))
429
+ .sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
430
+ return files[0]?.path || null;
431
+ }
432
+ catch {
433
+ return null;
434
+ }
435
+ }
436
+ function isMessageWithContent(message) {
437
+ return typeof message.role === "string" && "content" in message;
438
+ }
439
+ function extractTextContent(message) {
440
+ const content = message.content;
441
+ if (typeof content === "string") {
442
+ return content;
443
+ }
444
+ return content
445
+ .filter((block) => block.type === "text")
446
+ .map((block) => block.text)
447
+ .join(" ");
448
+ }
449
+ function getLastActivityTime(entries) {
450
+ let lastActivityTime;
451
+ for (const entry of entries) {
452
+ if (entry.type !== "message")
453
+ continue;
454
+ const message = entry.message;
455
+ if (!isMessageWithContent(message))
456
+ continue;
457
+ if (message.role !== "user" && message.role !== "assistant")
458
+ continue;
459
+ const msgTimestamp = message.timestamp;
460
+ if (typeof msgTimestamp === "number") {
461
+ lastActivityTime = Math.max(lastActivityTime ?? 0, msgTimestamp);
462
+ continue;
463
+ }
464
+ const entryTimestamp = entry.timestamp;
465
+ if (typeof entryTimestamp === "string") {
466
+ const t = new Date(entryTimestamp).getTime();
467
+ if (!Number.isNaN(t)) {
468
+ lastActivityTime = Math.max(lastActivityTime ?? 0, t);
469
+ }
470
+ }
471
+ }
472
+ return lastActivityTime;
473
+ }
474
+ function getSessionModifiedDate(entries, header, statsMtime) {
475
+ const lastActivityTime = getLastActivityTime(entries);
476
+ if (typeof lastActivityTime === "number" && lastActivityTime > 0) {
477
+ return new Date(lastActivityTime);
478
+ }
479
+ const headerTime = typeof header.timestamp === "string" ? new Date(header.timestamp).getTime() : NaN;
480
+ return !Number.isNaN(headerTime) ? new Date(headerTime) : statsMtime;
481
+ }
482
+ async function buildSessionInfo(filePath) {
483
+ try {
484
+ const content = await readFile(filePath, "utf8");
485
+ const entries = readSessionEntries(content.trim().split("\n"));
486
+ if (entries.length === 0)
487
+ return null;
488
+ const header = readSessionHeader(entries);
489
+ if (!header)
490
+ return null;
491
+ const stats = await stat(filePath);
492
+ let messageCount = 0;
493
+ let firstMessage = "";
494
+ const allMessages = [];
495
+ let name;
496
+ for (const entry of entries) {
497
+ // Extract session name (use latest)
498
+ if (entry.type === "session_info") {
499
+ const infoEntry = entry;
500
+ if (infoEntry.name) {
501
+ name = infoEntry.name.trim();
502
+ }
503
+ }
504
+ if (entry.type !== "message")
505
+ continue;
506
+ messageCount++;
507
+ const message = entry.message;
508
+ if (!isMessageWithContent(message))
509
+ continue;
510
+ if (message.role !== "user" && message.role !== "assistant")
511
+ continue;
512
+ const textContent = extractTextContent(message);
513
+ if (!textContent)
514
+ continue;
515
+ allMessages.push(textContent);
516
+ if (!firstMessage && message.role === "user") {
517
+ firstMessage = textContent;
518
+ }
519
+ }
520
+ const cwd = typeof header.cwd === "string" ? header.cwd : "";
521
+ const modified = getSessionModifiedDate(entries, header, stats.mtime);
522
+ return {
523
+ path: filePath,
524
+ id: header.id,
525
+ cwd,
526
+ name,
527
+ created: new Date(header.timestamp),
528
+ modified,
529
+ lastModified: modified.getTime(),
530
+ messageCount,
531
+ firstMessage: firstMessage || "(no messages)",
532
+ allMessagesText: allMessages.join(" "),
533
+ size: stats.size,
534
+ };
535
+ }
536
+ catch {
537
+ return null;
538
+ }
539
+ }
540
+ async function listSessionsFromDir(dir, onProgress, progressOffset = 0, progressTotal) {
541
+ const sessions = [];
542
+ if (!existsSync(dir)) {
543
+ return sessions;
544
+ }
545
+ try {
546
+ const dirEntries = await readdir(dir);
547
+ const files = dirEntries.filter((f) => f.endsWith(".jsonl")).map((f) => join(dir, f));
548
+ const total = progressTotal ?? files.length;
549
+ let loaded = 0;
550
+ const results = await Promise.all(files.map(async (file) => {
551
+ const info = await buildSessionInfo(file);
552
+ loaded++;
553
+ onProgress?.(progressOffset + loaded, total);
554
+ return info;
555
+ }));
556
+ for (const info of results) {
557
+ if (info) {
558
+ sessions.push(info);
559
+ }
560
+ }
561
+ }
562
+ catch {
563
+ // Return empty list on error
564
+ }
565
+ return sessions;
566
+ }
567
+ /**
568
+ * Manages conversation sessions as append-only trees stored in JSONL files.
569
+ *
570
+ * Each session entry has an id and parentId forming a tree structure. The "leaf"
571
+ * pointer tracks the current position. Appending creates a child of the current leaf.
572
+ * Branching moves the leaf to an earlier entry, allowing new branches without
573
+ * modifying history.
574
+ *
575
+ * Use buildSessionContext() to get the resolved message list for the LLM, which
576
+ * handles compaction summaries and follows the path from root to current leaf.
577
+ */
578
+ export class SessionManager {
579
+ constructor(cwd, sessionDir, sessionFile, persist) {
580
+ this.sessionId = "";
581
+ this.flushed = false;
582
+ this.fileEntries = [];
583
+ this.byId = new Map();
584
+ this.labelsById = new Map();
585
+ this.leafId = null;
586
+ this.cwd = cwd;
587
+ this.sessionDir = sessionDir;
588
+ this.persist = persist;
589
+ if (persist && sessionDir && !existsSync(sessionDir)) {
590
+ mkdirSync(sessionDir, { recursive: true });
591
+ }
592
+ if (sessionFile) {
593
+ this.setSessionFile(sessionFile);
594
+ }
595
+ else {
596
+ this.newSession();
597
+ }
598
+ }
599
+ /** Switch to a different session file (used for resume and branching) */
600
+ setSessionFile(sessionFile) {
601
+ this.sessionFile = resolve(sessionFile);
602
+ if (existsSync(this.sessionFile)) {
603
+ this.fileEntries = loadEntriesFromFile(this.sessionFile);
604
+ if (this.fileEntries.length === 0) {
605
+ const explicitPath = this.sessionFile;
606
+ this.newSession();
607
+ this.sessionFile = explicitPath;
608
+ this._rewriteFile();
609
+ this.flushed = true;
610
+ return;
611
+ }
612
+ const header = readSessionHeader(this.fileEntries);
613
+ this.sessionId = header?.id ?? randomUUID();
614
+ if (migrateToCurrentVersion(this.fileEntries)) {
615
+ this._rewriteFile();
616
+ }
617
+ this._buildIndex();
618
+ this.flushed = true;
619
+ }
620
+ else {
621
+ const explicitPath = this.sessionFile;
622
+ this.newSession();
623
+ this.sessionFile = explicitPath; // preserve explicit path from --session flag
624
+ }
625
+ }
626
+ newSession(options) {
627
+ this.sessionId = randomUUID();
628
+ const timestamp = new Date().toISOString();
629
+ const header = {
630
+ type: "session",
631
+ version: CURRENT_SESSION_VERSION,
632
+ id: this.sessionId,
633
+ timestamp,
634
+ cwd: this.cwd,
635
+ parentSession: options?.parentSession,
636
+ };
637
+ this.fileEntries = [header];
638
+ this.byId.clear();
639
+ this.labelsById.clear();
640
+ this.leafId = null;
641
+ this.flushed = false;
642
+ if (this.persist) {
643
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
644
+ this.sessionFile = join(this.getSessionDir(), `${fileTimestamp}_${this.sessionId}.jsonl`);
645
+ }
646
+ return this.sessionFile;
647
+ }
648
+ _buildIndex() {
649
+ const index = buildEntryIndex(this.fileEntries);
650
+ this.byId = index.byId;
651
+ this.labelsById = index.labelsById;
652
+ this.leafId = index.leafId;
653
+ }
654
+ _rewriteFile() {
655
+ if (!this.persist || !this.sessionFile)
656
+ return;
657
+ const content = `${this.fileEntries.map((e) => JSON.stringify(e)).join("\n")}\n`;
658
+ writeFileSync(this.sessionFile, content);
659
+ }
660
+ isPersisted() {
661
+ return this.persist;
662
+ }
663
+ getCwd() {
664
+ return this.cwd;
665
+ }
666
+ getSessionDir() {
667
+ return this.sessionDir;
668
+ }
669
+ getSessionId() {
670
+ return this.sessionId;
671
+ }
672
+ getSessionFile() {
673
+ return this.sessionFile;
674
+ }
675
+ _persist(entry) {
676
+ if (!this.persist || !this.sessionFile)
677
+ return;
678
+ const hasAssistant = this.fileEntries.some((e) => e.type === "message" && e.message.role === "assistant");
679
+ if (!hasAssistant)
680
+ return;
681
+ if (!this.flushed) {
682
+ for (const fileEntry of this.fileEntries) {
683
+ appendSessionEntry(this.sessionFile, fileEntry);
684
+ }
685
+ this.flushed = true;
686
+ }
687
+ else {
688
+ appendSessionEntry(this.sessionFile, entry);
689
+ }
690
+ }
691
+ _appendEntry(entry) {
692
+ this.fileEntries.push(entry);
693
+ this.byId.set(entry.id, entry);
694
+ this.leafId = entry.id;
695
+ this._persist(entry);
696
+ }
697
+ /** Append a message as child of current leaf, then advance leaf. Returns entry id.
698
+ * Does not allow writing CompactionSummaryMessage and BranchSummaryMessage directly.
699
+ * Reason: we want these to be top-level entries in the session, not message session entries,
700
+ * so it is easier to find them.
701
+ * These need to be appended via appendCompaction() and appendBranchSummary() methods.
702
+ */
703
+ appendMessage(message) {
704
+ const entry = {
705
+ type: "message",
706
+ id: generateId(this.byId),
707
+ parentId: this.leafId,
708
+ timestamp: new Date().toISOString(),
709
+ message,
710
+ };
711
+ this._appendEntry(entry);
712
+ return entry.id;
713
+ }
714
+ /** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */
715
+ appendThinkingLevelChange(thinkingLevel) {
716
+ const entry = {
717
+ type: "thinking_level_change",
718
+ id: generateId(this.byId),
719
+ parentId: this.leafId,
720
+ timestamp: new Date().toISOString(),
721
+ thinkingLevel,
722
+ };
723
+ this._appendEntry(entry);
724
+ return entry.id;
725
+ }
726
+ /** Append a model change as child of current leaf, then advance leaf. Returns entry id. */
727
+ appendModelChange(provider, modelId) {
728
+ const entry = {
729
+ type: "model_change",
730
+ id: generateId(this.byId),
731
+ parentId: this.leafId,
732
+ timestamp: new Date().toISOString(),
733
+ provider,
734
+ modelId,
735
+ };
736
+ this._appendEntry(entry);
737
+ return entry.id;
738
+ }
739
+ /** Append a compaction summary as child of current leaf, then advance leaf. Returns entry id. */
740
+ appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromHook) {
741
+ const entry = {
742
+ type: "compaction",
743
+ id: generateId(this.byId),
744
+ parentId: this.leafId,
745
+ timestamp: new Date().toISOString(),
746
+ summary,
747
+ firstKeptEntryId,
748
+ tokensBefore,
749
+ details,
750
+ fromHook,
751
+ };
752
+ this._appendEntry(entry);
753
+ return entry.id;
754
+ }
755
+ /** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */
756
+ appendCustomEntry(customType, data) {
757
+ const entry = {
758
+ type: "custom",
759
+ customType,
760
+ data,
761
+ id: generateId(this.byId),
762
+ parentId: this.leafId,
763
+ timestamp: new Date().toISOString(),
764
+ };
765
+ this._appendEntry(entry);
766
+ return entry.id;
767
+ }
768
+ /** Append a session info entry (e.g., display name). Returns entry id. */
769
+ appendSessionInfo(name) {
770
+ const entry = {
771
+ type: "session_info",
772
+ id: generateId(this.byId),
773
+ parentId: this.leafId,
774
+ timestamp: new Date().toISOString(),
775
+ name: name.trim(),
776
+ };
777
+ this._appendEntry(entry);
778
+ return entry.id;
779
+ }
780
+ /** Get the current session name from the latest session_info entry, if any. */
781
+ getSessionName() {
782
+ // Walk entries in reverse to find the latest session_info with a name
783
+ const entries = this.getEntries();
784
+ for (let i = entries.length - 1; i >= 0; i--) {
785
+ const entry = entries[i];
786
+ if (entry.type === "session_info" && entry.name) {
787
+ return entry.name;
788
+ }
789
+ }
790
+ return undefined;
791
+ }
792
+ /**
793
+ * Append a custom message entry (for extensions) that participates in LLM context.
794
+ * @param customType Extension identifier for filtering on reload
795
+ * @param content Message content (string or TextContent/ImageContent array)
796
+ * @param display Whether to show in TUI (true = styled display, false = hidden)
797
+ * @param details Optional extension-specific metadata (not sent to LLM)
798
+ * @returns Entry id
799
+ */
800
+ appendCustomMessageEntry(customType, content, display, details) {
801
+ const entry = {
802
+ type: "custom_message",
803
+ customType,
804
+ content,
805
+ display,
806
+ details,
807
+ id: generateId(this.byId),
808
+ parentId: this.leafId,
809
+ timestamp: new Date().toISOString(),
810
+ };
811
+ this._appendEntry(entry);
812
+ return entry.id;
813
+ }
814
+ // =========================================================================
815
+ // Tree Traversal
816
+ // =========================================================================
817
+ getLeafId() {
818
+ return this.leafId;
819
+ }
820
+ getLeafEntry() {
821
+ return this.leafId ? this.byId.get(this.leafId) : undefined;
822
+ }
823
+ getEntry(id) {
824
+ return this.byId.get(id);
825
+ }
826
+ /**
827
+ * Get all direct children of an entry.
828
+ */
829
+ getChildren(parentId) {
830
+ const children = [];
831
+ for (const entry of this.byId.values()) {
832
+ if (entry.parentId === parentId) {
833
+ children.push(entry);
834
+ }
835
+ }
836
+ return children;
837
+ }
838
+ /**
839
+ * Get the label for an entry, if any.
840
+ */
841
+ getLabel(id) {
842
+ return this.labelsById.get(id);
843
+ }
844
+ /**
845
+ * Set or clear a label on an entry.
846
+ * Labels are user-defined markers for bookmarking/navigation.
847
+ * Pass undefined or empty string to clear the label.
848
+ */
849
+ appendLabelChange(targetId, label) {
850
+ if (!this.byId.has(targetId)) {
851
+ throw new Error(`Entry ${targetId} not found`);
852
+ }
853
+ const entry = {
854
+ type: "label",
855
+ id: generateId(this.byId),
856
+ parentId: this.leafId,
857
+ timestamp: new Date().toISOString(),
858
+ targetId,
859
+ label,
860
+ };
861
+ this._appendEntry(entry);
862
+ if (label) {
863
+ this.labelsById.set(targetId, label);
864
+ }
865
+ else {
866
+ this.labelsById.delete(targetId);
867
+ }
868
+ return entry.id;
869
+ }
870
+ /**
871
+ * Walk from entry to root, returning all entries in path order.
872
+ * Includes all entry types (messages, compaction, model changes, etc.).
873
+ * Use buildSessionContext() to get the resolved messages for the LLM.
874
+ */
875
+ getBranch(fromId) {
876
+ return resolveBranchPath(this.byId, fromId ?? this.leafId);
877
+ }
878
+ /**
879
+ * Build the session context (what gets sent to the LLM).
880
+ * Uses tree traversal from current leaf.
881
+ */
882
+ buildSessionContext() {
883
+ return buildSessionContext(this.getEntries(), this.leafId, this.byId);
884
+ }
885
+ /**
886
+ * Get session header.
887
+ */
888
+ getHeader() {
889
+ const h = this.fileEntries.find((e) => e.type === "session");
890
+ return h ? h : null;
891
+ }
892
+ /**
893
+ * Get all session entries (excludes header). Returns a shallow copy.
894
+ * The session is append-only: use appendXXX() to add entries, branch() to
895
+ * change the leaf pointer. Entries cannot be modified or deleted.
896
+ */
897
+ getEntries() {
898
+ return this.fileEntries.filter((e) => e.type !== "session");
899
+ }
900
+ /**
901
+ * Get the session as a tree structure. Returns a shallow defensive copy of all entries.
902
+ * A well-formed session has exactly one root (first entry with parentId === null).
903
+ * Orphaned entries (broken parent chain) are also returned as roots.
904
+ */
905
+ getTree() {
906
+ return buildSessionTree(this.getEntries(), this.labelsById);
907
+ }
908
+ // =========================================================================
909
+ // Branching
910
+ // =========================================================================
911
+ /**
912
+ * Start a new branch from an earlier entry.
913
+ * Moves the leaf pointer to the specified entry. The next appendXXX() call
914
+ * will create a child of that entry, forming a new branch. Existing entries
915
+ * are not modified or deleted.
916
+ */
917
+ branch(branchFromId) {
918
+ if (!this.byId.has(branchFromId)) {
919
+ throw new Error(`Entry ${branchFromId} not found`);
920
+ }
921
+ this.leafId = branchFromId;
922
+ }
923
+ /**
924
+ * Reset the leaf pointer to null (before any entries).
925
+ * The next appendXXX() call will create a new root entry (parentId = null).
926
+ * Use this when navigating to re-edit the first user message.
927
+ */
928
+ resetLeaf() {
929
+ this.leafId = null;
930
+ }
931
+ /**
932
+ * Start a new branch with a summary of the abandoned path.
933
+ * Same as branch(), but also appends a branch_summary entry that captures
934
+ * context from the abandoned conversation path.
935
+ */
936
+ branchWithSummary(branchFromId, summary, details, fromHook) {
937
+ if (branchFromId !== null && !this.byId.has(branchFromId)) {
938
+ throw new Error(`Entry ${branchFromId} not found`);
939
+ }
940
+ this.leafId = branchFromId;
941
+ const entry = {
942
+ type: "branch_summary",
943
+ id: generateId(this.byId),
944
+ parentId: branchFromId,
945
+ timestamp: new Date().toISOString(),
946
+ fromId: branchFromId ?? "root",
947
+ summary,
948
+ details,
949
+ fromHook,
950
+ };
951
+ this._appendEntry(entry);
952
+ return entry.id;
953
+ }
954
+ /**
955
+ * Create a new session file containing only the path from root to the specified leaf.
956
+ * Useful for extracting a single conversation path from a branched session.
957
+ * Returns the new session file path, or undefined if not persisting.
958
+ */
959
+ createBranchedSession(leafId) {
960
+ const path = this.getBranch(leafId);
961
+ if (path.length === 0) {
962
+ throw new Error(`Entry ${leafId} not found`);
963
+ }
964
+ // Filter out LabelEntry from path - we'll recreate them from the resolved map
965
+ const pathWithoutLabels = path.filter((e) => e.type !== "label");
966
+ const newSessionId = randomUUID();
967
+ const timestamp = new Date().toISOString();
968
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
969
+ const newSessionFile = join(this.getSessionDir(), `${fileTimestamp}_${newSessionId}.jsonl`);
970
+ const header = {
971
+ type: "session",
972
+ version: CURRENT_SESSION_VERSION,
973
+ id: newSessionId,
974
+ timestamp,
975
+ cwd: this.cwd,
976
+ parentSession: this.persist ? this.sessionFile : undefined,
977
+ };
978
+ // Collect labels for entries in the path
979
+ const pathEntryIds = new Set(pathWithoutLabels.map((e) => e.id));
980
+ const labelsToWrite = [];
981
+ for (const [targetId, label] of this.labelsById) {
982
+ if (pathEntryIds.has(targetId)) {
983
+ labelsToWrite.push({ targetId, label });
984
+ }
985
+ }
986
+ if (this.persist) {
987
+ appendSessionEntry(newSessionFile, header);
988
+ for (const entry of pathWithoutLabels) {
989
+ appendSessionEntry(newSessionFile, entry);
990
+ }
991
+ // Write fresh label entries at the end
992
+ const lastEntryId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;
993
+ let parentId = lastEntryId;
994
+ const labelEntries = [];
995
+ for (const { targetId, label } of labelsToWrite) {
996
+ const labelEntry = {
997
+ type: "label",
998
+ id: generateId(new Set(pathEntryIds)),
999
+ parentId,
1000
+ timestamp: new Date().toISOString(),
1001
+ targetId,
1002
+ label,
1003
+ };
1004
+ appendSessionEntry(newSessionFile, labelEntry);
1005
+ pathEntryIds.add(labelEntry.id);
1006
+ labelEntries.push(labelEntry);
1007
+ parentId = labelEntry.id;
1008
+ }
1009
+ this.fileEntries = [header, ...pathWithoutLabels, ...labelEntries];
1010
+ this.sessionId = newSessionId;
1011
+ this._buildIndex();
1012
+ return newSessionFile;
1013
+ }
1014
+ // In-memory mode: replace current session with the path + labels
1015
+ const labelEntries = [];
1016
+ let parentId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;
1017
+ for (const { targetId, label } of labelsToWrite) {
1018
+ const labelEntry = {
1019
+ type: "label",
1020
+ id: generateId(new Set([...pathEntryIds, ...labelEntries.map((e) => e.id)])),
1021
+ parentId,
1022
+ timestamp: new Date().toISOString(),
1023
+ targetId,
1024
+ label,
1025
+ };
1026
+ labelEntries.push(labelEntry);
1027
+ parentId = labelEntry.id;
1028
+ }
1029
+ this.fileEntries = [header, ...pathWithoutLabels, ...labelEntries];
1030
+ this.sessionId = newSessionId;
1031
+ this._buildIndex();
1032
+ return undefined;
1033
+ }
1034
+ /**
1035
+ * Create a new session.
1036
+ * @param cwd Working directory (stored in session header)
1037
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.indusagi/agent/sessions/<encoded-cwd>/).
1038
+ */
1039
+ static create(cwd, sessionDir) {
1040
+ const dir = sessionDir ?? getDefaultSessionDir(cwd);
1041
+ return new SessionManager(cwd, dir, undefined, true);
1042
+ }
1043
+ /**
1044
+ * Open a specific session file.
1045
+ * @param path Path to session file
1046
+ * @param sessionDir Optional session directory for /new or /branch. If omitted, derives from file's parent.
1047
+ */
1048
+ static open(path, sessionDir) {
1049
+ // Extract cwd from session header if possible, otherwise use process.cwd()
1050
+ const entries = loadEntriesFromFile(path);
1051
+ const header = entries.find((e) => e.type === "session");
1052
+ const cwd = header?.cwd ?? process.cwd();
1053
+ // If no sessionDir provided, derive from file's parent directory
1054
+ const dir = sessionDir ?? resolve(path, "..");
1055
+ return new SessionManager(cwd, dir, path, true);
1056
+ }
1057
+ /**
1058
+ * Continue the most recent session, or create new if none.
1059
+ * @param cwd Working directory
1060
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.indusagi/agent/sessions/<encoded-cwd>/).
1061
+ */
1062
+ static continueRecent(cwd, sessionDir) {
1063
+ const dir = sessionDir ?? getDefaultSessionDir(cwd);
1064
+ const mostRecent = findMostRecentSession(dir);
1065
+ if (mostRecent) {
1066
+ return new SessionManager(cwd, dir, mostRecent, true);
1067
+ }
1068
+ return new SessionManager(cwd, dir, undefined, true);
1069
+ }
1070
+ /** Create an in-memory session (no file persistence) */
1071
+ static inMemory(cwd = process.cwd()) {
1072
+ return new SessionManager(cwd, "", undefined, false);
1073
+ }
1074
+ /**
1075
+ * Fork a session from another project directory into the current project.
1076
+ * Creates a new session in the target cwd with the full history from the source session.
1077
+ * @param sourcePath Path to the source session file
1078
+ * @param targetCwd Target working directory (where the new session will be stored)
1079
+ * @param sessionDir Optional session directory. If omitted, uses default for targetCwd.
1080
+ */
1081
+ static forkFrom(sourcePath, targetCwd, sessionDir) {
1082
+ const sourceEntries = loadEntriesFromFile(sourcePath);
1083
+ if (sourceEntries.length === 0) {
1084
+ throw new Error(`Cannot fork: source session file is empty or invalid: ${sourcePath}`);
1085
+ }
1086
+ const sourceHeader = sourceEntries.find((e) => e.type === "session");
1087
+ if (!sourceHeader) {
1088
+ throw new Error(`Cannot fork: source session has no header: ${sourcePath}`);
1089
+ }
1090
+ const dir = sessionDir ?? getDefaultSessionDir(targetCwd);
1091
+ if (!existsSync(dir)) {
1092
+ mkdirSync(dir, { recursive: true });
1093
+ }
1094
+ // Create new session file with new ID but forked content
1095
+ const newSessionId = randomUUID();
1096
+ const timestamp = new Date().toISOString();
1097
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
1098
+ const newSessionFile = join(dir, `${fileTimestamp}_${newSessionId}.jsonl`);
1099
+ // Write new header pointing to source as parent, with updated cwd
1100
+ const newHeader = {
1101
+ type: "session",
1102
+ version: CURRENT_SESSION_VERSION,
1103
+ id: newSessionId,
1104
+ timestamp,
1105
+ cwd: targetCwd,
1106
+ parentSession: sourcePath,
1107
+ };
1108
+ appendSessionEntry(newSessionFile, newHeader);
1109
+ // Copy all non-header entries from source
1110
+ for (const entry of sourceEntries) {
1111
+ if (entry.type !== "session") {
1112
+ appendSessionEntry(newSessionFile, entry);
1113
+ }
1114
+ }
1115
+ return new SessionManager(targetCwd, dir, newSessionFile, true);
1116
+ }
1117
+ /**
1118
+ * List all sessions for a directory.
1119
+ * @param cwd Working directory (used to compute default session directory)
1120
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.indusagi/agent/sessions/<encoded-cwd>/).
1121
+ * @param onProgress Optional callback for progress updates (loaded, total)
1122
+ */
1123
+ static async list(cwd, sessionDir, onProgress) {
1124
+ const dir = sessionDir ?? getDefaultSessionDir(cwd);
1125
+ const sessions = await listSessionsFromDir(dir, onProgress);
1126
+ sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
1127
+ return sessions;
1128
+ }
1129
+ /**
1130
+ * List all sessions across all project directories.
1131
+ * @param onProgress Optional callback for progress updates (loaded, total)
1132
+ */
1133
+ static async listAll(onProgress) {
1134
+ const sessionsDir = getSessionsDir();
1135
+ try {
1136
+ if (!existsSync(sessionsDir)) {
1137
+ return [];
1138
+ }
1139
+ const entries = await readdir(sessionsDir, { withFileTypes: true });
1140
+ const dirs = entries.filter((e) => e.isDirectory()).map((e) => join(sessionsDir, e.name));
1141
+ // Count total files first for accurate progress
1142
+ let totalFiles = 0;
1143
+ const dirFiles = [];
1144
+ for (const dir of dirs) {
1145
+ try {
1146
+ const files = (await readdir(dir)).filter((f) => f.endsWith(".jsonl"));
1147
+ dirFiles.push(files.map((f) => join(dir, f)));
1148
+ totalFiles += files.length;
1149
+ }
1150
+ catch {
1151
+ dirFiles.push([]);
1152
+ }
1153
+ }
1154
+ // Process all files with progress tracking
1155
+ let loaded = 0;
1156
+ const sessions = [];
1157
+ const allFiles = dirFiles.flat();
1158
+ const results = await Promise.all(allFiles.map(async (file) => {
1159
+ const info = await buildSessionInfo(file);
1160
+ loaded++;
1161
+ onProgress?.(loaded, totalFiles);
1162
+ return info;
1163
+ }));
1164
+ for (const info of results) {
1165
+ if (info) {
1166
+ sessions.push(info);
1167
+ }
1168
+ }
1169
+ sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
1170
+ return sessions;
1171
+ }
1172
+ catch {
1173
+ return [];
1174
+ }
1175
+ }
1176
+ }
2
1177
  //# sourceMappingURL=session-manager.js.map