@openadapter/koda 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (357) hide show
  1. package/CHANGELOG.md +4448 -0
  2. package/README.md +665 -0
  3. package/dist/bun/cli.d.ts +2 -0
  4. package/dist/bun/cli.js +2 -0
  5. package/dist/bun/register-bedrock.d.ts +1 -0
  6. package/dist/bun/register-bedrock.js +1 -0
  7. package/dist/bun/restore-sandbox-env.d.ts +12 -0
  8. package/dist/bun/restore-sandbox-env.js +1 -0
  9. package/dist/cli/args.d.ts +55 -0
  10. package/dist/cli/args.js +167 -0
  11. package/dist/cli/config-selector.d.ts +13 -0
  12. package/dist/cli/config-selector.js +1 -0
  13. package/dist/cli/file-processor.d.ts +14 -0
  14. package/dist/cli/file-processor.js +7 -0
  15. package/dist/cli/import-sessions.d.ts +34 -0
  16. package/dist/cli/import-sessions.js +6 -0
  17. package/dist/cli/initial-message.d.ts +17 -0
  18. package/dist/cli/initial-message.js +1 -0
  19. package/dist/cli/list-models.d.ts +8 -0
  20. package/dist/cli/list-models.js +2 -0
  21. package/dist/cli/openadapter-setup.d.ts +29 -0
  22. package/dist/cli/openadapter-setup.js +3 -0
  23. package/dist/cli/session-picker.d.ts +8 -0
  24. package/dist/cli/session-picker.js +1 -0
  25. package/dist/cli.d.ts +2 -0
  26. package/dist/cli.js +2 -0
  27. package/dist/config.d.ts +92 -0
  28. package/dist/config.js +1 -0
  29. package/dist/core/agent-session-runtime.d.ts +116 -0
  30. package/dist/core/agent-session-runtime.js +1 -0
  31. package/dist/core/agent-session-services.d.ts +86 -0
  32. package/dist/core/agent-session-services.js +1 -0
  33. package/dist/core/agent-session.d.ts +747 -0
  34. package/dist/core/agent-session.js +32 -0
  35. package/dist/core/auth-guidance.d.ts +4 -0
  36. package/dist/core/auth-guidance.js +8 -0
  37. package/dist/core/auth-storage.d.ts +140 -0
  38. package/dist/core/auth-storage.js +1 -0
  39. package/dist/core/bash-executor.d.ts +31 -0
  40. package/dist/core/bash-executor.js +1 -0
  41. package/dist/core/compaction/branch-summarization.d.ts +89 -0
  42. package/dist/core/compaction/branch-summarization.js +38 -0
  43. package/dist/core/compaction/compaction.d.ts +120 -0
  44. package/dist/core/compaction/compaction.js +104 -0
  45. package/dist/core/compaction/index.d.ts +6 -0
  46. package/dist/core/compaction/index.js +1 -0
  47. package/dist/core/compaction/utils.d.ts +37 -0
  48. package/dist/core/compaction/utils.js +19 -0
  49. package/dist/core/defaults.d.ts +2 -0
  50. package/dist/core/defaults.js +1 -0
  51. package/dist/core/diagnostics.d.ts +14 -0
  52. package/dist/core/diagnostics.js +0 -0
  53. package/dist/core/event-bus.d.ts +8 -0
  54. package/dist/core/event-bus.js +1 -0
  55. package/dist/core/exec.d.ts +28 -0
  56. package/dist/core/exec.js +1 -0
  57. package/dist/core/export-html/ansi-to-html.d.ts +21 -0
  58. package/dist/core/export-html/ansi-to-html.js +1 -0
  59. package/dist/core/export-html/index.d.ts +36 -0
  60. package/dist/core/export-html/index.js +2 -0
  61. package/dist/core/export-html/template.css +1066 -0
  62. package/dist/core/export-html/template.html +55 -0
  63. package/dist/core/export-html/template.js +72 -0
  64. package/dist/core/export-html/tool-renderer.d.ts +33 -0
  65. package/dist/core/export-html/tool-renderer.js +1 -0
  66. package/dist/core/export-html/vendor/highlight.min.js +8 -0
  67. package/dist/core/export-html/vendor/marked.min.js +56 -0
  68. package/dist/core/extensions/index.d.ts +11 -0
  69. package/dist/core/extensions/index.js +1 -0
  70. package/dist/core/extensions/loader.d.ts +23 -0
  71. package/dist/core/extensions/loader.js +1 -0
  72. package/dist/core/extensions/runner.d.ts +160 -0
  73. package/dist/core/extensions/runner.js +1 -0
  74. package/dist/core/extensions/types.d.ts +1180 -0
  75. package/dist/core/extensions/types.js +1 -0
  76. package/dist/core/extensions/wrapper.d.ts +19 -0
  77. package/dist/core/extensions/wrapper.js +1 -0
  78. package/dist/core/footer-data-provider.d.ts +53 -0
  79. package/dist/core/footer-data-provider.js +1 -0
  80. package/dist/core/http-dispatcher.d.ts +20 -0
  81. package/dist/core/http-dispatcher.js +1 -0
  82. package/dist/core/index.d.ts +11 -0
  83. package/dist/core/index.js +1 -0
  84. package/dist/core/keybindings.d.ts +352 -0
  85. package/dist/core/keybindings.js +1 -0
  86. package/dist/core/messages.d.ts +76 -0
  87. package/dist/core/messages.js +17 -0
  88. package/dist/core/model-registry.d.ts +149 -0
  89. package/dist/core/model-registry.js +9 -0
  90. package/dist/core/model-resolver.d.ts +109 -0
  91. package/dist/core/model-resolver.js +1 -0
  92. package/dist/core/output-guard.d.ts +6 -0
  93. package/dist/core/output-guard.js +1 -0
  94. package/dist/core/package-manager.d.ts +203 -0
  95. package/dist/core/package-manager.js +3 -0
  96. package/dist/core/prompt-templates.d.ts +51 -0
  97. package/dist/core/prompt-templates.js +2 -0
  98. package/dist/core/provider-attribution.d.ts +3 -0
  99. package/dist/core/provider-attribution.js +1 -0
  100. package/dist/core/provider-display-names.d.ts +1 -0
  101. package/dist/core/provider-display-names.js +1 -0
  102. package/dist/core/resolve-config-value.d.ts +30 -0
  103. package/dist/core/resolve-config-value.js +1 -0
  104. package/dist/core/resource-loader.d.ts +193 -0
  105. package/dist/core/resource-loader.js +1 -0
  106. package/dist/core/sdk.d.ts +108 -0
  107. package/dist/core/sdk.js +1 -0
  108. package/dist/core/session-cwd.d.ts +18 -0
  109. package/dist/core/session-cwd.js +7 -0
  110. package/dist/core/session-manager.d.ts +331 -0
  111. package/dist/core/session-manager.js +11 -0
  112. package/dist/core/settings-manager.d.ts +265 -0
  113. package/dist/core/settings-manager.js +1 -0
  114. package/dist/core/skills.d.ts +59 -0
  115. package/dist/core/skills.js +4 -0
  116. package/dist/core/slash-commands.d.ts +13 -0
  117. package/dist/core/slash-commands.js +1 -0
  118. package/dist/core/source-info.d.ts +17 -0
  119. package/dist/core/source-info.js +1 -0
  120. package/dist/core/system-prompt.d.ts +27 -0
  121. package/dist/core/system-prompt.js +52 -0
  122. package/dist/core/telemetry.d.ts +2 -0
  123. package/dist/core/telemetry.js +1 -0
  124. package/dist/core/timings.d.ts +7 -0
  125. package/dist/core/timings.js +3 -0
  126. package/dist/core/tools/bash.d.ts +67 -0
  127. package/dist/core/tools/bash.js +18 -0
  128. package/dist/core/tools/edit-diff.d.ts +86 -0
  129. package/dist/core/tools/edit-diff.js +16 -0
  130. package/dist/core/tools/edit.d.ts +50 -0
  131. package/dist/core/tools/edit.js +2 -0
  132. package/dist/core/tools/file-mutation-queue.d.ts +5 -0
  133. package/dist/core/tools/file-mutation-queue.js +1 -0
  134. package/dist/core/tools/find.d.ts +34 -0
  135. package/dist/core/tools/find.js +13 -0
  136. package/dist/core/tools/grep.d.ts +36 -0
  137. package/dist/core/tools/grep.js +13 -0
  138. package/dist/core/tools/index.d.ts +39 -0
  139. package/dist/core/tools/index.js +1 -0
  140. package/dist/core/tools/ls.d.ts +36 -0
  141. package/dist/core/tools/ls.js +9 -0
  142. package/dist/core/tools/output-accumulator.d.ts +51 -0
  143. package/dist/core/tools/output-accumulator.js +4 -0
  144. package/dist/core/tools/path-utils.d.ts +9 -0
  145. package/dist/core/tools/path-utils.js +1 -0
  146. package/dist/core/tools/read.d.ts +34 -0
  147. package/dist/core/tools/read.js +22 -0
  148. package/dist/core/tools/render-utils.d.ts +23 -0
  149. package/dist/core/tools/render-utils.js +4 -0
  150. package/dist/core/tools/tool-definition-wrapper.d.ts +13 -0
  151. package/dist/core/tools/tool-definition-wrapper.js +1 -0
  152. package/dist/core/tools/truncate.d.ts +69 -0
  153. package/dist/core/tools/truncate.js +5 -0
  154. package/dist/core/tools/write.d.ts +25 -0
  155. package/dist/core/tools/write.js +13 -0
  156. package/dist/index.d.ts +30 -0
  157. package/dist/index.js +1 -0
  158. package/dist/main.d.ts +11 -0
  159. package/dist/main.js +1 -0
  160. package/dist/migrations.d.ts +32 -0
  161. package/dist/migrations.js +8 -0
  162. package/dist/modes/index.d.ts +8 -0
  163. package/dist/modes/index.js +1 -0
  164. package/dist/modes/interactive/assets/clankolas.png +0 -0
  165. package/dist/modes/interactive/components/armin.d.ts +33 -0
  166. package/dist/modes/interactive/components/armin.js +1 -0
  167. package/dist/modes/interactive/components/assistant-message.d.ts +19 -0
  168. package/dist/modes/interactive/components/assistant-message.js +1 -0
  169. package/dist/modes/interactive/components/bash-execution.d.ts +33 -0
  170. package/dist/modes/interactive/components/bash-execution.js +13 -0
  171. package/dist/modes/interactive/components/bordered-loader.d.ts +15 -0
  172. package/dist/modes/interactive/components/bordered-loader.js +1 -0
  173. package/dist/modes/interactive/components/branch-summary-message.d.ts +15 -0
  174. package/dist/modes/interactive/components/branch-summary-message.js +3 -0
  175. package/dist/modes/interactive/components/compaction-summary-message.d.ts +15 -0
  176. package/dist/modes/interactive/components/compaction-summary-message.js +3 -0
  177. package/dist/modes/interactive/components/config-selector.d.ts +70 -0
  178. package/dist/modes/interactive/components/config-selector.js +1 -0
  179. package/dist/modes/interactive/components/countdown-timer.d.ts +13 -0
  180. package/dist/modes/interactive/components/countdown-timer.js +1 -0
  181. package/dist/modes/interactive/components/custom-editor.d.ts +20 -0
  182. package/dist/modes/interactive/components/custom-editor.js +1 -0
  183. package/dist/modes/interactive/components/custom-message.d.ts +19 -0
  184. package/dist/modes/interactive/components/custom-message.js +2 -0
  185. package/dist/modes/interactive/components/daxnuts.d.ts +22 -0
  186. package/dist/modes/interactive/components/daxnuts.js +1 -0
  187. package/dist/modes/interactive/components/diff.d.ts +11 -0
  188. package/dist/modes/interactive/components/diff.js +3 -0
  189. package/dist/modes/interactive/components/dynamic-border.d.ts +14 -0
  190. package/dist/modes/interactive/components/dynamic-border.js +1 -0
  191. package/dist/modes/interactive/components/earendil-announcement.d.ts +4 -0
  192. package/dist/modes/interactive/components/earendil-announcement.js +1 -0
  193. package/dist/modes/interactive/components/extension-editor.d.ts +19 -0
  194. package/dist/modes/interactive/components/extension-editor.js +3 -0
  195. package/dist/modes/interactive/components/extension-input.d.ts +22 -0
  196. package/dist/modes/interactive/components/extension-input.js +2 -0
  197. package/dist/modes/interactive/components/extension-selector.d.ts +25 -0
  198. package/dist/modes/interactive/components/extension-selector.js +2 -0
  199. package/dist/modes/interactive/components/footer.d.ts +27 -0
  200. package/dist/modes/interactive/components/footer.js +1 -0
  201. package/dist/modes/interactive/components/index.d.ts +31 -0
  202. package/dist/modes/interactive/components/index.js +1 -0
  203. package/dist/modes/interactive/components/keybinding-hints.d.ts +12 -0
  204. package/dist/modes/interactive/components/keybinding-hints.js +1 -0
  205. package/dist/modes/interactive/components/login-dialog.d.ts +51 -0
  206. package/dist/modes/interactive/components/login-dialog.js +1 -0
  207. package/dist/modes/interactive/components/model-selector.d.ts +46 -0
  208. package/dist/modes/interactive/components/model-selector.js +2 -0
  209. package/dist/modes/interactive/components/oauth-selector.d.ts +30 -0
  210. package/dist/modes/interactive/components/oauth-selector.js +1 -0
  211. package/dist/modes/interactive/components/scoped-models-selector.d.ts +41 -0
  212. package/dist/modes/interactive/components/scoped-models-selector.js +1 -0
  213. package/dist/modes/interactive/components/session-selector-search.d.ts +22 -0
  214. package/dist/modes/interactive/components/session-selector-search.js +1 -0
  215. package/dist/modes/interactive/components/session-selector.d.ts +95 -0
  216. package/dist/modes/interactive/components/session-selector.js +2 -0
  217. package/dist/modes/interactive/components/settings-selector.d.ts +68 -0
  218. package/dist/modes/interactive/components/settings-selector.js +1 -0
  219. package/dist/modes/interactive/components/show-images-selector.d.ts +9 -0
  220. package/dist/modes/interactive/components/show-images-selector.js +1 -0
  221. package/dist/modes/interactive/components/skill-invocation-message.d.ts +16 -0
  222. package/dist/modes/interactive/components/skill-invocation-message.js +3 -0
  223. package/dist/modes/interactive/components/theme-selector.d.ts +10 -0
  224. package/dist/modes/interactive/components/theme-selector.js +1 -0
  225. package/dist/modes/interactive/components/thinking-selector.d.ts +10 -0
  226. package/dist/modes/interactive/components/thinking-selector.js +1 -0
  227. package/dist/modes/interactive/components/tool-execution.d.ts +62 -0
  228. package/dist/modes/interactive/components/tool-execution.js +4 -0
  229. package/dist/modes/interactive/components/tree-selector.d.ts +88 -0
  230. package/dist/modes/interactive/components/tree-selector.js +1 -0
  231. package/dist/modes/interactive/components/user-message-selector.d.ts +29 -0
  232. package/dist/modes/interactive/components/user-message-selector.js +1 -0
  233. package/dist/modes/interactive/components/user-message.d.ts +9 -0
  234. package/dist/modes/interactive/components/user-message.js +1 -0
  235. package/dist/modes/interactive/components/visual-truncate.d.ts +23 -0
  236. package/dist/modes/interactive/components/visual-truncate.js +1 -0
  237. package/dist/modes/interactive/interactive-mode.d.ts +417 -0
  238. package/dist/modes/interactive/interactive-mode.js +116 -0
  239. package/dist/modes/interactive/theme/dark.json +86 -0
  240. package/dist/modes/interactive/theme/light.json +85 -0
  241. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  242. package/dist/modes/interactive/theme/theme.d.ts +101 -0
  243. package/dist/modes/interactive/theme/theme.js +18 -0
  244. package/dist/modes/print-mode.d.ts +27 -0
  245. package/dist/modes/print-mode.js +4 -0
  246. package/dist/modes/rpc/jsonl.d.ts +16 -0
  247. package/dist/modes/rpc/jsonl.js +3 -0
  248. package/dist/modes/rpc/rpc-client.d.ts +226 -0
  249. package/dist/modes/rpc/rpc-client.js +1 -0
  250. package/dist/modes/rpc/rpc-mode.d.ts +19 -0
  251. package/dist/modes/rpc/rpc-mode.js +1 -0
  252. package/dist/modes/rpc/rpc-types.d.ts +419 -0
  253. package/dist/modes/rpc/rpc-types.js +0 -0
  254. package/dist/package-manager-cli.d.ts +3 -0
  255. package/dist/package-manager-cli.js +49 -0
  256. package/dist/utils/ansi.d.ts +1 -0
  257. package/dist/utils/ansi.js +1 -0
  258. package/dist/utils/auto-update.d.ts +13 -0
  259. package/dist/utils/auto-update.js +1 -0
  260. package/dist/utils/changelog.d.ts +20 -0
  261. package/dist/utils/changelog.js +4 -0
  262. package/dist/utils/child-process.d.ts +14 -0
  263. package/dist/utils/child-process.js +1 -0
  264. package/dist/utils/clipboard-image.d.ts +10 -0
  265. package/dist/utils/clipboard-image.js +1 -0
  266. package/dist/utils/clipboard-native.d.ts +9 -0
  267. package/dist/utils/clipboard-native.js +1 -0
  268. package/dist/utils/clipboard.d.ts +1 -0
  269. package/dist/utils/clipboard.js +1 -0
  270. package/dist/utils/deprecation.d.ts +3 -0
  271. package/dist/utils/deprecation.js +1 -0
  272. package/dist/utils/exif-orientation.d.ts +4 -0
  273. package/dist/utils/exif-orientation.js +1 -0
  274. package/dist/utils/frontmatter.d.ts +7 -0
  275. package/dist/utils/frontmatter.js +4 -0
  276. package/dist/utils/fs-watch.d.ts +4 -0
  277. package/dist/utils/fs-watch.js +1 -0
  278. package/dist/utils/git.d.ts +25 -0
  279. package/dist/utils/git.js +1 -0
  280. package/dist/utils/html.d.ts +6 -0
  281. package/dist/utils/html.js +1 -0
  282. package/dist/utils/image-convert.d.ts +8 -0
  283. package/dist/utils/image-convert.js +1 -0
  284. package/dist/utils/image-resize-core.d.ts +29 -0
  285. package/dist/utils/image-resize-core.js +1 -0
  286. package/dist/utils/image-resize-worker.d.ts +1 -0
  287. package/dist/utils/image-resize-worker.js +1 -0
  288. package/dist/utils/image-resize.d.ts +15 -0
  289. package/dist/utils/image-resize.js +1 -0
  290. package/dist/utils/json.d.ts +2 -0
  291. package/dist/utils/json.js +1 -0
  292. package/dist/utils/koda-user-agent.d.ts +1 -0
  293. package/dist/utils/koda-user-agent.js +1 -0
  294. package/dist/utils/mime.d.ts +2 -0
  295. package/dist/utils/mime.js +1 -0
  296. package/dist/utils/paths.d.ts +30 -0
  297. package/dist/utils/paths.js +1 -0
  298. package/dist/utils/photon.d.ts +20 -0
  299. package/dist/utils/photon.js +1 -0
  300. package/dist/utils/shell.d.ts +29 -0
  301. package/dist/utils/shell.js +8 -0
  302. package/dist/utils/sleep.d.ts +4 -0
  303. package/dist/utils/sleep.js +1 -0
  304. package/dist/utils/syntax-highlight.d.ts +11 -0
  305. package/dist/utils/syntax-highlight.js +2 -0
  306. package/dist/utils/tools-manager.d.ts +2 -0
  307. package/dist/utils/tools-manager.js +1 -0
  308. package/dist/utils/version-check.d.ts +14 -0
  309. package/dist/utils/version-check.js +1 -0
  310. package/dist/utils/windows-self-update.d.ts +2 -0
  311. package/dist/utils/windows-self-update.js +1 -0
  312. package/docs/compaction.md +394 -0
  313. package/docs/custom-provider.md +736 -0
  314. package/docs/development.md +71 -0
  315. package/docs/docs.json +148 -0
  316. package/docs/extensions.md +2626 -0
  317. package/docs/images/doom-extension.png +0 -0
  318. package/docs/images/exy.png +0 -0
  319. package/docs/images/interactive-mode.png +0 -0
  320. package/docs/images/tree-view.png +0 -0
  321. package/docs/index.md +80 -0
  322. package/docs/json.md +82 -0
  323. package/docs/keybindings.md +197 -0
  324. package/docs/models.md +493 -0
  325. package/docs/packages.md +226 -0
  326. package/docs/prompt-templates.md +88 -0
  327. package/docs/providers.md +253 -0
  328. package/docs/quickstart.md +165 -0
  329. package/docs/rpc.md +1408 -0
  330. package/docs/sdk.md +1137 -0
  331. package/docs/session-format.md +412 -0
  332. package/docs/sessions.md +145 -0
  333. package/docs/settings.md +281 -0
  334. package/docs/shell-aliases.md +13 -0
  335. package/docs/skills.md +231 -0
  336. package/docs/terminal-setup.md +114 -0
  337. package/docs/termux.md +127 -0
  338. package/docs/themes.md +295 -0
  339. package/docs/tmux.md +61 -0
  340. package/docs/tui.md +927 -0
  341. package/docs/usage.md +288 -0
  342. package/docs/windows.md +17 -0
  343. package/npm-shrinkwrap.json +1792 -0
  344. package/openadapter/extensions/koda-ask.js +12 -0
  345. package/openadapter/extensions/koda-bg.js +14 -0
  346. package/openadapter/extensions/koda-commands.mjs +15 -0
  347. package/openadapter/extensions/koda-help.js +8 -0
  348. package/openadapter/extensions/koda-memory.js +16 -0
  349. package/openadapter/extensions/koda-status.js +1 -0
  350. package/openadapter/extensions/koda-todo.js +4 -0
  351. package/openadapter/extensions/koda-vision.js +4 -0
  352. package/openadapter/extensions/koda-web.js +7 -0
  353. package/openadapter/setup.mjs +173 -0
  354. package/openadapter/skills/code-review/SKILL.md +22 -0
  355. package/openadapter/skills/debugging/SKILL.md +28 -0
  356. package/openadapter/skills/frontend/SKILL.md +38 -0
  357. package/package.json +108 -0
@@ -0,0 +1,331 @@
1
+ import { type AgentMessage } from "@openadapter/koda-agent-core";
2
+ import type { ImageContent, Message, TextContent } from "@openadapter/koda-ai";
3
+ import { type BashExecutionMessage, type CustomMessage } from "./messages.ts";
4
+ export declare const CURRENT_SESSION_VERSION = 3;
5
+ export interface SessionHeader {
6
+ type: "session";
7
+ version?: number;
8
+ id: string;
9
+ timestamp: string;
10
+ cwd: string;
11
+ parentSession?: string;
12
+ }
13
+ export interface NewSessionOptions {
14
+ id?: string;
15
+ parentSession?: string;
16
+ }
17
+ export interface SessionEntryBase {
18
+ type: string;
19
+ id: string;
20
+ parentId: string | null;
21
+ timestamp: string;
22
+ }
23
+ export interface SessionMessageEntry extends SessionEntryBase {
24
+ type: "message";
25
+ message: AgentMessage;
26
+ }
27
+ export interface ThinkingLevelChangeEntry extends SessionEntryBase {
28
+ type: "thinking_level_change";
29
+ thinkingLevel: string;
30
+ }
31
+ export interface ModelChangeEntry extends SessionEntryBase {
32
+ type: "model_change";
33
+ provider: string;
34
+ modelId: string;
35
+ }
36
+ export interface CompactionEntry<T = unknown> extends SessionEntryBase {
37
+ type: "compaction";
38
+ summary: string;
39
+ firstKeptEntryId: string;
40
+ tokensBefore: number;
41
+ /** Extension-specific data (e.g., ArtifactIndex, version markers for structured compaction) */
42
+ details?: T;
43
+ /** True if generated by an extension, undefined/false if pi-generated (backward compatible) */
44
+ fromHook?: boolean;
45
+ }
46
+ export interface BranchSummaryEntry<T = unknown> extends SessionEntryBase {
47
+ type: "branch_summary";
48
+ fromId: string;
49
+ summary: string;
50
+ /** Extension-specific data (not sent to LLM) */
51
+ details?: T;
52
+ /** True if generated by an extension, false if pi-generated */
53
+ fromHook?: boolean;
54
+ }
55
+ /**
56
+ * Custom entry for extensions to store extension-specific data in the session.
57
+ * Use customType to identify your extension's entries.
58
+ *
59
+ * Purpose: Persist extension state across session reloads. On reload, extensions can
60
+ * scan entries for their customType and reconstruct internal state.
61
+ *
62
+ * Does NOT participate in LLM context (ignored by buildSessionContext).
63
+ * For injecting content into context, see CustomMessageEntry.
64
+ */
65
+ export interface CustomEntry<T = unknown> extends SessionEntryBase {
66
+ type: "custom";
67
+ customType: string;
68
+ data?: T;
69
+ }
70
+ /** Label entry for user-defined bookmarks/markers on entries. */
71
+ export interface LabelEntry extends SessionEntryBase {
72
+ type: "label";
73
+ targetId: string;
74
+ label: string | undefined;
75
+ }
76
+ /** Session metadata entry (e.g., user-defined display name). */
77
+ export interface SessionInfoEntry extends SessionEntryBase {
78
+ type: "session_info";
79
+ name?: string;
80
+ }
81
+ /**
82
+ * Custom message entry for extensions to inject messages into LLM context.
83
+ * Use customType to identify your extension's entries.
84
+ *
85
+ * Unlike CustomEntry, this DOES participate in LLM context.
86
+ * The content is converted to a user message in buildSessionContext().
87
+ * Use details for extension-specific metadata (not sent to LLM).
88
+ *
89
+ * display controls TUI rendering:
90
+ * - false: hidden entirely
91
+ * - true: rendered with distinct styling (different from user messages)
92
+ */
93
+ export interface CustomMessageEntry<T = unknown> extends SessionEntryBase {
94
+ type: "custom_message";
95
+ customType: string;
96
+ content: string | (TextContent | ImageContent)[];
97
+ details?: T;
98
+ display: boolean;
99
+ }
100
+ /** Session entry - has id/parentId for tree structure (returned by "read" methods in SessionManager) */
101
+ export type SessionEntry = SessionMessageEntry | ThinkingLevelChangeEntry | ModelChangeEntry | CompactionEntry | BranchSummaryEntry | CustomEntry | CustomMessageEntry | LabelEntry | SessionInfoEntry;
102
+ /** Raw file entry (includes header) */
103
+ export type FileEntry = SessionHeader | SessionEntry;
104
+ /** Tree node for getTree() - defensive copy of session structure */
105
+ export interface SessionTreeNode {
106
+ entry: SessionEntry;
107
+ children: SessionTreeNode[];
108
+ /** Resolved label for this entry, if any */
109
+ label?: string;
110
+ /** Timestamp of the latest label change for this entry, if any */
111
+ labelTimestamp?: string;
112
+ }
113
+ export interface SessionContext {
114
+ messages: AgentMessage[];
115
+ thinkingLevel: string;
116
+ model: {
117
+ provider: string;
118
+ modelId: string;
119
+ } | null;
120
+ }
121
+ export interface SessionInfo {
122
+ path: string;
123
+ id: string;
124
+ /** Working directory where the session was started. Empty string for old sessions. */
125
+ cwd: string;
126
+ /** User-defined display name from session_info entries. */
127
+ name?: string;
128
+ /** Path to the parent session (if this session was forked). */
129
+ parentSessionPath?: string;
130
+ created: Date;
131
+ modified: Date;
132
+ messageCount: number;
133
+ firstMessage: string;
134
+ allMessagesText: string;
135
+ }
136
+ export type ReadonlySessionManager = Pick<SessionManager, "getCwd" | "getSessionDir" | "getSessionId" | "getSessionFile" | "getLeafId" | "getLeafEntry" | "getEntry" | "getLabel" | "getBranch" | "getHeader" | "getEntries" | "getTree" | "getSessionName">;
137
+ export declare function assertValidSessionId(id: string): void;
138
+ /** Exported for testing */
139
+ export declare function migrateSessionEntries(entries: FileEntry[]): void;
140
+ /** Exported for compaction.test.ts */
141
+ export declare function parseSessionEntries(content: string): FileEntry[];
142
+ export declare function getLatestCompactionEntry(entries: SessionEntry[]): CompactionEntry | null;
143
+ /**
144
+ * Build the session context from entries using tree traversal.
145
+ * If leafId is provided, walks from that entry to root.
146
+ * Handles compaction and branch summaries along the path.
147
+ */
148
+ export declare function buildSessionContext(entries: SessionEntry[], leafId?: string | null, byId?: Map<string, SessionEntry>): SessionContext;
149
+ export declare function getDefaultSessionDir(cwd: string, agentDir?: string): string;
150
+ /** Exported for testing */
151
+ export declare function loadEntriesFromFile(filePath: string): FileEntry[];
152
+ /** Exported for testing */
153
+ export declare function findMostRecentSession(sessionDir: string, cwd?: string): string | null;
154
+ export type SessionListProgress = (loaded: number, total: number) => void;
155
+ /**
156
+ * Manages conversation sessions as append-only trees stored in JSONL files.
157
+ *
158
+ * Each session entry has an id and parentId forming a tree structure. The "leaf"
159
+ * pointer tracks the current position. Appending creates a child of the current leaf.
160
+ * Branching moves the leaf to an earlier entry, allowing new branches without
161
+ * modifying history.
162
+ *
163
+ * Use buildSessionContext() to get the resolved message list for the LLM, which
164
+ * handles compaction summaries and follows the path from root to current leaf.
165
+ */
166
+ export declare class SessionManager {
167
+ private sessionId;
168
+ private sessionFile;
169
+ private sessionDir;
170
+ private cwd;
171
+ private persist;
172
+ private flushed;
173
+ private fileEntries;
174
+ private byId;
175
+ private labelsById;
176
+ private labelTimestampsById;
177
+ private leafId;
178
+ private constructor();
179
+ /** Switch to a different session file (used for resume and branching) */
180
+ setSessionFile(sessionFile: string): void;
181
+ newSession(options?: NewSessionOptions): string | undefined;
182
+ private _buildIndex;
183
+ private _rewriteFile;
184
+ isPersisted(): boolean;
185
+ getCwd(): string;
186
+ getSessionDir(): string;
187
+ usesDefaultSessionDir(): boolean;
188
+ getSessionId(): string;
189
+ getSessionFile(): string | undefined;
190
+ _persist(entry: SessionEntry): void;
191
+ private _appendEntry;
192
+ /** Append a message as child of current leaf, then advance leaf. Returns entry id.
193
+ * Does not allow writing CompactionSummaryMessage and BranchSummaryMessage directly.
194
+ * Reason: we want these to be top-level entries in the session, not message session entries,
195
+ * so it is easier to find them.
196
+ * These need to be appended via appendCompaction() and appendBranchSummary() methods.
197
+ */
198
+ appendMessage(message: Message | CustomMessage | BashExecutionMessage): string;
199
+ /** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */
200
+ appendThinkingLevelChange(thinkingLevel: string): string;
201
+ /** Append a model change as child of current leaf, then advance leaf. Returns entry id. */
202
+ appendModelChange(provider: string, modelId: string): string;
203
+ /** Append a compaction summary as child of current leaf, then advance leaf. Returns entry id. */
204
+ appendCompaction<T = unknown>(summary: string, firstKeptEntryId: string, tokensBefore: number, details?: T, fromHook?: boolean): string;
205
+ /** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */
206
+ appendCustomEntry(customType: string, data?: unknown): string;
207
+ /** Append a session info entry (e.g., display name). Returns entry id. */
208
+ appendSessionInfo(name: string): string;
209
+ /** Get the current session name from the latest session_info entry, if any. */
210
+ getSessionName(): string | undefined;
211
+ /**
212
+ * Append a custom message entry (for extensions) that participates in LLM context.
213
+ * @param customType Extension identifier for filtering on reload
214
+ * @param content Message content (string or TextContent/ImageContent array)
215
+ * @param display Whether to show in TUI (true = styled display, false = hidden)
216
+ * @param details Optional extension-specific metadata (not sent to LLM)
217
+ * @returns Entry id
218
+ */
219
+ appendCustomMessageEntry<T = unknown>(customType: string, content: string | (TextContent | ImageContent)[], display: boolean, details?: T): string;
220
+ getLeafId(): string | null;
221
+ getLeafEntry(): SessionEntry | undefined;
222
+ getEntry(id: string): SessionEntry | undefined;
223
+ /**
224
+ * Get all direct children of an entry.
225
+ */
226
+ getChildren(parentId: string): SessionEntry[];
227
+ /**
228
+ * Get the label for an entry, if any.
229
+ */
230
+ getLabel(id: string): string | undefined;
231
+ /**
232
+ * Set or clear a label on an entry.
233
+ * Labels are user-defined markers for bookmarking/navigation.
234
+ * Pass undefined or empty string to clear the label.
235
+ */
236
+ appendLabelChange(targetId: string, label: string | undefined): string;
237
+ /**
238
+ * Walk from entry to root, returning all entries in path order.
239
+ * Includes all entry types (messages, compaction, model changes, etc.).
240
+ * Use buildSessionContext() to get the resolved messages for the LLM.
241
+ */
242
+ getBranch(fromId?: string): SessionEntry[];
243
+ /**
244
+ * Build the session context (what gets sent to the LLM).
245
+ * Uses tree traversal from current leaf.
246
+ */
247
+ buildSessionContext(): SessionContext;
248
+ /**
249
+ * Get session header.
250
+ */
251
+ getHeader(): SessionHeader | null;
252
+ /**
253
+ * Get all session entries (excludes header). Returns a shallow copy.
254
+ * The session is append-only: use appendXXX() to add entries, branch() to
255
+ * change the leaf pointer. Entries cannot be modified or deleted.
256
+ */
257
+ getEntries(): SessionEntry[];
258
+ /**
259
+ * Get the session as a tree structure. Returns a shallow defensive copy of all entries.
260
+ * A well-formed session has exactly one root (first entry with parentId === null).
261
+ * Orphaned entries (broken parent chain) are also returned as roots.
262
+ */
263
+ getTree(): SessionTreeNode[];
264
+ /**
265
+ * Start a new branch from an earlier entry.
266
+ * Moves the leaf pointer to the specified entry. The next appendXXX() call
267
+ * will create a child of that entry, forming a new branch. Existing entries
268
+ * are not modified or deleted.
269
+ */
270
+ branch(branchFromId: string): void;
271
+ /**
272
+ * Reset the leaf pointer to null (before any entries).
273
+ * The next appendXXX() call will create a new root entry (parentId = null).
274
+ * Use this when navigating to re-edit the first user message.
275
+ */
276
+ resetLeaf(): void;
277
+ /**
278
+ * Start a new branch with a summary of the abandoned path.
279
+ * Same as branch(), but also appends a branch_summary entry that captures
280
+ * context from the abandoned conversation path.
281
+ */
282
+ branchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string;
283
+ /**
284
+ * Create a new session file containing only the path from root to the specified leaf.
285
+ * Useful for extracting a single conversation path from a branched session.
286
+ * Returns the new session file path, or undefined if not persisting.
287
+ */
288
+ createBranchedSession(leafId: string): string | undefined;
289
+ /**
290
+ * Create a new session.
291
+ * @param cwd Working directory (stored in session header)
292
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
293
+ */
294
+ static create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager;
295
+ /**
296
+ * Open a specific session file.
297
+ * @param path Path to session file
298
+ * @param sessionDir Optional session directory for /new or /branch. If omitted, derives from file's parent.
299
+ * @param cwdOverride Optional cwd override instead of the session header cwd.
300
+ */
301
+ static open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager;
302
+ /**
303
+ * Continue the most recent session, or create new if none.
304
+ * @param cwd Working directory
305
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
306
+ */
307
+ static continueRecent(cwd: string, sessionDir?: string): SessionManager;
308
+ /** Create an in-memory session (no file persistence) */
309
+ static inMemory(cwd?: string): SessionManager;
310
+ /**
311
+ * Fork a session from another project directory into the current project.
312
+ * Creates a new session in the target cwd with the full history from the source session.
313
+ * @param sourcePath Path to the source session file
314
+ * @param targetCwd Target working directory (where the new session will be stored)
315
+ * @param sessionDir Optional session directory. If omitted, uses default for targetCwd.
316
+ */
317
+ static forkFrom(sourcePath: string, targetCwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager;
318
+ /**
319
+ * List all sessions for a directory.
320
+ * @param cwd Working directory (used to compute default session directory)
321
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
322
+ * @param onProgress Optional callback for progress updates (loaded, total)
323
+ */
324
+ static list(cwd: string, sessionDir?: string, onProgress?: SessionListProgress): Promise<SessionInfo[]>;
325
+ /**
326
+ * List all sessions across all project directories.
327
+ * @param onProgress Optional callback for progress updates (loaded, total)
328
+ */
329
+ static listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;
330
+ static listAll(sessionDir?: string, onProgress?: SessionListProgress): Promise<SessionInfo[]>;
331
+ }
@@ -0,0 +1,11 @@
1
+ var q=Object.defineProperty;var h=(r,e)=>q(r,"name",{value:e,configurable:!0});import{uuidv7 as G}from"@openadapter/koda-agent-core";import{randomUUID as j}from"crypto";import{appendFileSync as M,closeSync as v,createReadStream as Q,existsSync as T,mkdirSync as $,openSync as x,readdirSync as Y,readSync as J,statSync as ee,writeFileSync as A}from"fs";import{readdir as k,stat as te}from"fs/promises";import{join as E,resolve as se}from"path";import{createInterface as ne}from"readline";import{StringDecoder as ie}from"string_decoder";import{getAgentDir as W,getSessionsDir as re}from"../config.js";import{normalizePath as S,resolvePath as w}from"../utils/paths.js";import{createBranchSummaryMessage as oe,createCompactionSummaryMessage as ae,createCustomMessage as le}from"./messages.js";const F=3;function C(){return G()}h(C,"createSessionId");function z(r){if(!/^[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$/.test(r))throw new Error("Session id must be non-empty, contain only alphanumeric characters, '-', '_', and '.', and start and end with an alphanumeric character")}h(z,"assertValidSessionId");function I(r){for(let e=0;e<100;e++){const t=j().slice(0,8);if(!r.has(t))return t}return j()}h(I,"generateId");function de(r){const e=new Set;let t=null;for(const s of r){if(s.type==="session"){s.version=2;continue}if(s.id=I(e),s.parentId=t,t=s.id,s.type==="compaction"){const n=s;if(typeof n.firstKeptEntryIndex=="number"){const i=r[n.firstKeptEntryIndex];i&&i.type!=="session"&&(n.firstKeptEntryId=i.id),delete n.firstKeptEntryIndex}}}}h(de,"migrateV1ToV2");function fe(r){for(const e of r){if(e.type==="session"){e.version=3;continue}if(e.type==="message"){const t=e;t.message&&t.message.role==="hookMessage"&&(t.message.role="custom")}}}h(fe,"migrateV2ToV3");function H(r){const t=r.find(s=>s.type==="session")?.version??1;return t>=F?!1:(t<2&&de(r),t<3&&fe(r),!0)}h(H,"migrateToCurrentVersion");function Be(r){H(r)}h(Be,"migrateSessionEntries");function Oe(r){const e=[],t=r.trim().split(`
2
+ `);for(const s of t)if(s.trim())try{const n=JSON.parse(s);e.push(n)}catch{}return e}h(Oe,"parseSessionEntries");function Me(r){for(let e=r.length-1;e>=0;e--)if(r[e].type==="compaction")return r[e];return null}h(Me,"getLatestCompactionEntry");function ce(r,e,t){if(!t){t=new Map;for(const o of r)t.set(o.id,o)}let s;if(e===null)return{messages:[],thinkingLevel:"off",model:null};if(e&&(s=t.get(e)),s||(s=r[r.length-1]),!s)return{messages:[],thinkingLevel:"off",model:null};const n=[];let i=s;for(;i;)n.unshift(i),i=i.parentId?t.get(i.parentId):void 0;let l="off",a=null,f=null;for(const o of n)o.type==="thinking_level_change"?l=o.thinkingLevel:o.type==="model_change"?a={provider:o.provider,modelId:o.modelId}:o.type==="message"&&o.message.role==="assistant"?a={provider:o.message.provider,modelId:o.message.model}:o.type==="compaction"&&(f=o);const c=[],u=h(o=>{o.type==="message"?c.push(o.message):o.type==="custom_message"?c.push(le(o.customType,o.content,o.display,o.details,o.timestamp)):o.type==="branch_summary"&&o.summary&&c.push(oe(o.summary,o.fromId,o.timestamp))},"appendMessage");if(f){c.push(ae(f.summary,f.tokensBefore,f.timestamp));const o=n.findIndex(p=>p.type==="compaction"&&p.id===f.id);let y=!1;for(let p=0;p<o;p++){const d=n[p];d.id===f.firstKeptEntryId&&(y=!0),y&&u(d)}for(let p=o+1;p<n.length;p++){const d=n[p];u(d)}}else for(const o of n)u(o);return{messages:c,thinkingLevel:l,model:a}}h(ce,"buildSessionContext");function N(r,e=W()){const t=w(r),s=w(e),n=`--${t.replace(/^[/\\]/,"").replace(/[/\\:]/g,"-")}--`;return E(s,"sessions",n)}h(N,"getDefaultSessionDirPath");function B(r,e=W()){const t=N(r,e);return T(t)||$(t,{recursive:!0}),t}h(B,"getDefaultSessionDir");const ue=1024*1024;function L(r){if(!r.trim())return null;try{return JSON.parse(r)}catch{return null}}h(L,"parseSessionEntryLine");function R(r){const e=S(r);if(!T(e))return[];const t=[],s=x(e,"r");try{const i=new ie("utf8"),l=Buffer.allocUnsafe(ue);let a="";for(;;){const c=J(s,l,0,l.length,null);if(c===0)break;a+=i.write(l.subarray(0,c));let u=0,o=a.indexOf(`
3
+ `,u);for(;o!==-1;){const y=L(a.slice(u,o));y&&t.push(y),u=o+1,o=a.indexOf(`
4
+ `,u)}a=a.slice(u)}a+=i.end();const f=L(a);f&&t.push(f)}finally{v(s)}if(t.length===0)return t;const n=t[0];return n.type!=="session"||typeof n.id!="string"?[]:t}h(R,"loadEntriesFromFile");function he(r){try{const e=x(r,"r"),t=Buffer.alloc(512),s=J(e,t,0,512,0);v(e);const n=t.toString("utf8",0,s).split(`
5
+ `)[0];if(!n)return null;const i=JSON.parse(n);return i.type!=="session"||typeof i.id!="string"?null:i}catch{return null}}h(he,"readSessionHeader");function pe(r){const e=r.cwd;return typeof e=="string"?e:void 0}h(pe,"getSessionHeaderCwd");function P(r,e){return r!==void 0&&r!==""&&w(r)===e}h(P,"sessionCwdMatches");function me(r,e){const t=S(r),s=e?w(e):void 0;try{return Y(t).filter(i=>i.endsWith(".jsonl")).map(i=>E(t,i)).map(i=>({path:i,header:he(i)})).filter(i=>i.header!==null&&(!s||P(pe(i.header),s))).map(({path:i})=>({path:i,mtime:ee(i).mtime})).sort((i,l)=>l.mtime.getTime()-i.mtime.getTime())[0]?.path||null}catch{return null}}h(me,"findMostRecentSession");function V(r){return typeof r.role=="string"&&"content"in r}h(V,"isMessageWithContent");function ye(r){const e=r.content;return typeof e=="string"?e:e.filter(t=>t.type==="text").map(t=>t.text).join(" ")}h(ye,"extractTextContent");function ge(r){const e=r.message;if(!V(e)||e.role!=="user"&&e.role!=="assistant")return;const t=e.timestamp;if(typeof t=="number")return t;const s=new Date(r.timestamp).getTime();return Number.isNaN(s)?void 0:s}h(ge,"getMessageActivityTime");async function Ie(r){try{const e=await te(r);let t=null,s=0,n="";const i=[];let l,a;const f=ne({input:Q(r,{encoding:"utf8"}),crlfDelay:1/0});for await(const p of f){const d=L(p);if(!d)continue;if(!t){if(d.type!=="session")return null;t=d;continue}if(d.type==="session_info"&&(l=d.name?.trim()||void 0),d.type!=="message")continue;s++;const m=ge(d);typeof m=="number"&&(a=Math.max(a??0,m));const g=d.message;if(!V(g)||g.role!=="user"&&g.role!=="assistant")continue;const b=ye(g);b&&(i.push(b),!n&&g.role==="user"&&(n=b))}if(!t)return null;const c=typeof t.cwd=="string"?t.cwd:"",u=t.parentSession,o=typeof t.timestamp=="string"?new Date(t.timestamp).getTime():NaN,y=typeof a=="number"&&a>0?new Date(a):Number.isNaN(o)?e.mtime:new Date(o);return{path:r,id:t.id,cwd:c,name:l,parentSessionPath:u,created:new Date(t.timestamp),modified:y,messageCount:s,firstMessage:n||"(no messages)",allMessagesText:i.join(" ")}}catch{return null}}h(Ie,"buildSessionInfo");const we=10;async function K(r,e){const t=new Array(r.length).fill(null),s=new Set;let n=0;const i=h(()=>{const l=n++,a=r[l];if(!a)return;let f;f=Ie(a).then(c=>{t[l]=c}).catch(()=>{t[l]=null}).finally(()=>{s.delete(f),e()}),s.add(f)},"startNext");for(;n<r.length||s.size>0;){for(;n<r.length&&s.size<we;)i();s.size>0&&await Promise.race(s)}return t}h(K,"buildSessionInfosWithConcurrency");async function U(r,e,t=0,s){const n=[];if(!T(r))return n;try{const l=(await k(r)).filter(u=>u.endsWith(".jsonl")).map(u=>E(r,u)),a=s??l.length;let f=0;const c=await K(l,()=>{f++,e?.(t+f,a)});for(const u of c)u&&n.push(u)}catch{}return n}h(U,"listSessionsFromDir");class D{static{h(this,"SessionManager")}sessionId="";sessionFile;sessionDir;cwd;persist;flushed=!1;fileEntries=[];byId=new Map;labelsById=new Map;labelTimestampsById=new Map;leafId=null;constructor(e,t,s,n,i){this.cwd=w(e),this.sessionDir=S(t),this.persist=n,n&&this.sessionDir&&!T(this.sessionDir)&&$(this.sessionDir,{recursive:!0}),s?this.setSessionFile(s):this.newSession(i)}setSessionFile(e){if(this.sessionFile=w(e),T(this.sessionFile)){if(this.fileEntries=R(this.sessionFile),this.fileEntries.length===0){const s=this.sessionFile;this.newSession(),this.sessionFile=s,this._rewriteFile(),this.flushed=!0;return}const t=this.fileEntries.find(s=>s.type==="session");this.sessionId=t?.id??C(),H(this.fileEntries)&&this._rewriteFile(),this._buildIndex(),this.flushed=!0}else{const t=this.sessionFile;this.newSession(),this.sessionFile=t}}newSession(e){e?.id!==void 0&&z(e.id),this.sessionId=e?.id??C();const t=new Date().toISOString(),s={type:"session",version:F,id:this.sessionId,timestamp:t,cwd:this.cwd,parentSession:e?.parentSession};if(this.fileEntries=[s],this.byId.clear(),this.labelsById.clear(),this.leafId=null,this.flushed=!1,this.persist){const n=t.replace(/[:.]/g,"-");this.sessionFile=E(this.getSessionDir(),`${n}_${this.sessionId}.jsonl`)}return this.sessionFile}_buildIndex(){this.byId.clear(),this.labelsById.clear(),this.labelTimestampsById.clear(),this.leafId=null;for(const e of this.fileEntries)e.type!=="session"&&(this.byId.set(e.id,e),this.leafId=e.id,e.type==="label"&&(e.label?(this.labelsById.set(e.targetId,e.label),this.labelTimestampsById.set(e.targetId,e.timestamp)):(this.labelsById.delete(e.targetId),this.labelTimestampsById.delete(e.targetId))))}_rewriteFile(){if(!this.persist||!this.sessionFile)return;const e=x(this.sessionFile,"w");try{for(const t of this.fileEntries)A(e,`${JSON.stringify(t)}
6
+ `)}finally{v(e)}}isPersisted(){return this.persist}getCwd(){return this.cwd}getSessionDir(){return this.sessionDir}usesDefaultSessionDir(){return this.sessionDir===N(this.cwd)}getSessionId(){return this.sessionId}getSessionFile(){return this.sessionFile}_persist(e){if(!this.persist||!this.sessionFile)return;if(!this.fileEntries.some(s=>s.type==="message"&&s.message.role==="assistant")){this.flushed?M(this.sessionFile,`${JSON.stringify(e)}
7
+ `):this.flushed=!1;return}if(this.flushed)M(this.sessionFile,`${JSON.stringify(e)}
8
+ `);else{const s=x(this.sessionFile,"wx");try{for(const n of this.fileEntries)A(s,`${JSON.stringify(n)}
9
+ `)}finally{v(s)}this.flushed=!0}}_appendEntry(e){this.fileEntries.push(e),this.byId.set(e.id,e),this.leafId=e.id,this._persist(e)}appendMessage(e){const t={type:"message",id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString(),message:e};return this._appendEntry(t),t.id}appendThinkingLevelChange(e){const t={type:"thinking_level_change",id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString(),thinkingLevel:e};return this._appendEntry(t),t.id}appendModelChange(e,t){const s={type:"model_change",id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString(),provider:e,modelId:t};return this._appendEntry(s),s.id}appendCompaction(e,t,s,n,i){const l={type:"compaction",id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString(),summary:e,firstKeptEntryId:t,tokensBefore:s,details:n,fromHook:i};return this._appendEntry(l),l.id}appendCustomEntry(e,t){const s={type:"custom",customType:e,data:t,id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString()};return this._appendEntry(s),s.id}appendSessionInfo(e){const t={type:"session_info",id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString(),name:e.trim()};return this._appendEntry(t),t.id}getSessionName(){const e=this.getEntries();for(let t=e.length-1;t>=0;t--){const s=e[t];if(s.type==="session_info")return s.name?.trim()||void 0}}appendCustomMessageEntry(e,t,s,n){const i={type:"custom_message",customType:e,content:t,display:s,details:n,id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString()};return this._appendEntry(i),i.id}getLeafId(){return this.leafId}getLeafEntry(){return this.leafId?this.byId.get(this.leafId):void 0}getEntry(e){return this.byId.get(e)}getChildren(e){const t=[];for(const s of this.byId.values())s.parentId===e&&t.push(s);return t}getLabel(e){return this.labelsById.get(e)}appendLabelChange(e,t){if(!this.byId.has(e))throw new Error(`Entry ${e} not found`);const s={type:"label",id:I(this.byId),parentId:this.leafId,timestamp:new Date().toISOString(),targetId:e,label:t};return this._appendEntry(s),t?(this.labelsById.set(e,t),this.labelTimestampsById.set(e,s.timestamp)):(this.labelsById.delete(e),this.labelTimestampsById.delete(e)),s.id}getBranch(e){const t=[],s=e??this.leafId;let n=s?this.byId.get(s):void 0;for(;n;)t.unshift(n),n=n.parentId?this.byId.get(n.parentId):void 0;return t}buildSessionContext(){return ce(this.getEntries(),this.leafId,this.byId)}getHeader(){const e=this.fileEntries.find(t=>t.type==="session");return e||null}getEntries(){return this.fileEntries.filter(e=>e.type!=="session")}getTree(){const e=this.getEntries(),t=new Map,s=[];for(const i of e){const l=this.labelsById.get(i.id),a=this.labelTimestampsById.get(i.id);t.set(i.id,{entry:i,children:[],label:l,labelTimestamp:a})}for(const i of e){const l=t.get(i.id);if(i.parentId===null||i.parentId===i.id)s.push(l);else{const a=t.get(i.parentId);a?a.children.push(l):s.push(l)}}const n=[...s];for(;n.length>0;){const i=n.pop();i.children.sort((l,a)=>new Date(l.entry.timestamp).getTime()-new Date(a.entry.timestamp).getTime()),n.push(...i.children)}return s}branch(e){if(!this.byId.has(e))throw new Error(`Entry ${e} not found`);this.leafId=e}resetLeaf(){this.leafId=null}branchWithSummary(e,t,s,n){if(e!==null&&!this.byId.has(e))throw new Error(`Entry ${e} not found`);this.leafId=e;const i={type:"branch_summary",id:I(this.byId),parentId:e,timestamp:new Date().toISOString(),fromId:e??"root",summary:t,details:s,fromHook:n};return this._appendEntry(i),i.id}createBranchedSession(e){const t=this.sessionFile,s=this.getBranch(e);if(s.length===0)throw new Error(`Entry ${e} not found`);const n=s.filter(d=>d.type!=="label"),i=C(),l=new Date().toISOString(),a=l.replace(/[:.]/g,"-"),f=E(this.getSessionDir(),`${a}_${i}.jsonl`),c={type:"session",version:F,id:i,timestamp:l,cwd:this.cwd,parentSession:this.persist?t:void 0},u=new Set(n.map(d=>d.id)),o=[];for(const[d,m]of this.labelsById)u.has(d)&&o.push({targetId:d,label:m,timestamp:this.labelTimestampsById.get(d)});if(this.persist){let m=n[n.length-1]?.id||null;const g=[];for(const{targetId:_,label:Z,timestamp:X}of o){const O={type:"label",id:I(new Set(u)),parentId:m,timestamp:X,targetId:_,label:Z};u.add(O.id),g.push(O),m=O.id}return this.fileEntries=[c,...n,...g],this.sessionId=i,this.sessionFile=f,this._buildIndex(),this.fileEntries.some(_=>_.type==="message"&&_.message.role==="assistant")?(this._rewriteFile(),this.flushed=!0):this.flushed=!1,f}const y=[];let p=n[n.length-1]?.id||null;for(const{targetId:d,label:m,timestamp:g}of o){const b={type:"label",id:I(new Set([...u,...y.map(_=>_.id)])),parentId:p,timestamp:g,targetId:d,label:m};y.push(b),p=b.id}this.fileEntries=[c,...n,...y],this.sessionId=i,this._buildIndex()}static create(e,t,s){const n=t?S(t):B(e);return new D(e,n,void 0,!0,s)}static open(e,t,s){const n=w(e),l=R(n).find(c=>c.type==="session"),a=s??l?.cwd??process.cwd(),f=t?S(t):se(n,"..");return new D(a,f,n,!0)}static continueRecent(e,t){const s=t?S(t):B(e),n=t!==void 0&&s!==N(e),i=me(s,n?e:void 0);return i?new D(e,s,i,!0):new D(e,s,void 0,!0)}static inMemory(e=process.cwd()){return new D(e,"",void 0,!1)}static forkFrom(e,t,s,n){const i=w(e),l=w(t),a=R(i);if(a.length===0)throw new Error(`Cannot fork: source session file is empty or invalid: ${i}`);if(!a.find(m=>m.type==="session"))throw new Error(`Cannot fork: source session has no header: ${i}`);const c=s?S(s):B(l);T(c)||$(c,{recursive:!0}),n?.id!==void 0&&z(n.id);const u=n?.id??C(),o=new Date().toISOString(),y=o.replace(/[:.]/g,"-"),p=E(c,`${y}_${u}.jsonl`);A(p,`${JSON.stringify({type:"session",version:F,id:u,timestamp:o,cwd:l,parentSession:i})}
10
+ `,{flag:"wx"});for(const m of a)m.type!=="session"&&M(p,`${JSON.stringify(m)}
11
+ `);return new D(l,c,p,!0)}static async list(e,t,s){const n=t?S(t):B(e),i=t!==void 0&&n!==N(e),l=w(e),a=(await U(n,s)).filter(f=>!i||P(f.cwd,l));return a.sort((f,c)=>c.modified.getTime()-f.modified.getTime()),a}static async listAll(e,t){const s=typeof e=="string"?S(e):void 0,n=typeof e=="function"?e:t;if(s){const l=await U(s,n);return l.sort((a,f)=>f.modified.getTime()-a.modified.getTime()),l}const i=re();try{if(!T(i))return[];const a=(await k(i,{withFileTypes:!0})).filter(d=>d.isDirectory()).map(d=>E(i,d.name));let f=0;const c=[];for(const d of a)try{const m=(await k(d)).filter(g=>g.endsWith(".jsonl"));c.push(m.map(g=>E(d,g))),f+=m.length}catch{c.push([])}let u=0;const o=[],y=c.flat(),p=await K(y,()=>{u++,n?.(u,f)});for(const d of p)d&&o.push(d);return o.sort((d,m)=>m.modified.getTime()-d.modified.getTime()),o}catch{return[]}}}export{F as CURRENT_SESSION_VERSION,D as SessionManager,z as assertValidSessionId,ce as buildSessionContext,me as findMostRecentSession,B as getDefaultSessionDir,Me as getLatestCompactionEntry,R as loadEntriesFromFile,Be as migrateSessionEntries,Oe as parseSessionEntries};
@@ -0,0 +1,265 @@
1
+ import type { Transport } from "@openadapter/koda-ai";
2
+ export interface CompactionSettings {
3
+ enabled?: boolean;
4
+ reserveTokens?: number;
5
+ keepRecentTokens?: number;
6
+ }
7
+ export interface BranchSummarySettings {
8
+ reserveTokens?: number;
9
+ skipPrompt?: boolean;
10
+ }
11
+ export interface ProviderRetrySettings {
12
+ timeoutMs?: number;
13
+ maxRetries?: number;
14
+ maxRetryDelayMs?: number;
15
+ }
16
+ export interface RetrySettings {
17
+ enabled?: boolean;
18
+ maxRetries?: number;
19
+ baseDelayMs?: number;
20
+ provider?: ProviderRetrySettings;
21
+ }
22
+ export interface TerminalSettings {
23
+ showImages?: boolean;
24
+ imageWidthCells?: number;
25
+ clearOnShrink?: boolean;
26
+ showTerminalProgress?: boolean;
27
+ }
28
+ export interface ImageSettings {
29
+ autoResize?: boolean;
30
+ blockImages?: boolean;
31
+ }
32
+ export interface ThinkingBudgetsSettings {
33
+ minimal?: number;
34
+ low?: number;
35
+ medium?: number;
36
+ high?: number;
37
+ }
38
+ export interface MarkdownSettings {
39
+ codeBlockIndent?: string;
40
+ }
41
+ export interface WarningSettings {
42
+ anthropicExtraUsage?: boolean;
43
+ }
44
+ export type TransportSetting = Transport;
45
+ /**
46
+ * Package source for npm/git packages.
47
+ * - String form: load all resources from the package
48
+ * - Object form: filter which resources to load
49
+ */
50
+ export type PackageSource = string | {
51
+ source: string;
52
+ extensions?: string[];
53
+ skills?: string[];
54
+ prompts?: string[];
55
+ themes?: string[];
56
+ };
57
+ export interface Settings {
58
+ lastChangelogVersion?: string;
59
+ defaultProvider?: string;
60
+ defaultModel?: string;
61
+ defaultThinkingLevel?: "off" | "minimal" | "low" | "medium" | "high" | "xhigh";
62
+ transport?: TransportSetting;
63
+ steeringMode?: "all" | "one-at-a-time";
64
+ followUpMode?: "all" | "one-at-a-time";
65
+ theme?: string;
66
+ compaction?: CompactionSettings;
67
+ branchSummary?: BranchSummarySettings;
68
+ retry?: RetrySettings;
69
+ hideThinkingBlock?: boolean;
70
+ shellPath?: string;
71
+ quietStartup?: boolean;
72
+ shellCommandPrefix?: string;
73
+ npmCommand?: string[];
74
+ collapseChangelog?: boolean;
75
+ enableInstallTelemetry?: boolean;
76
+ packages?: PackageSource[];
77
+ extensions?: string[];
78
+ skills?: string[];
79
+ prompts?: string[];
80
+ themes?: string[];
81
+ enableSkillCommands?: boolean;
82
+ terminal?: TerminalSettings;
83
+ images?: ImageSettings;
84
+ enabledModels?: string[];
85
+ doubleEscapeAction?: "fork" | "tree" | "none";
86
+ treeFilterMode?: "default" | "no-tools" | "user-only" | "labeled-only" | "all";
87
+ thinkingBudgets?: ThinkingBudgetsSettings;
88
+ editorPaddingX?: number;
89
+ autocompleteMaxVisible?: number;
90
+ showHardwareCursor?: boolean;
91
+ markdown?: MarkdownSettings;
92
+ warnings?: WarningSettings;
93
+ sessionDir?: string;
94
+ httpIdleTimeoutMs?: number;
95
+ websocketConnectTimeoutMs?: number;
96
+ }
97
+ export type SettingsScope = "global" | "project";
98
+ export interface SettingsStorage {
99
+ withLock(scope: SettingsScope, fn: (current: string | undefined) => string | undefined): void;
100
+ }
101
+ export interface SettingsError {
102
+ scope: SettingsScope;
103
+ error: Error;
104
+ }
105
+ export declare class FileSettingsStorage implements SettingsStorage {
106
+ private globalSettingsPath;
107
+ private projectSettingsPath;
108
+ constructor(cwd: string, agentDir: string);
109
+ private acquireLockSyncWithRetry;
110
+ withLock(scope: SettingsScope, fn: (current: string | undefined) => string | undefined): void;
111
+ }
112
+ export declare class InMemorySettingsStorage implements SettingsStorage {
113
+ private global;
114
+ private project;
115
+ withLock(scope: SettingsScope, fn: (current: string | undefined) => string | undefined): void;
116
+ }
117
+ export declare class SettingsManager {
118
+ private storage;
119
+ private globalSettings;
120
+ private projectSettings;
121
+ private settings;
122
+ private modifiedFields;
123
+ private modifiedNestedFields;
124
+ private modifiedProjectFields;
125
+ private modifiedProjectNestedFields;
126
+ private globalSettingsLoadError;
127
+ private projectSettingsLoadError;
128
+ private writeQueue;
129
+ private errors;
130
+ private constructor();
131
+ /** Create a SettingsManager that loads from files */
132
+ static create(cwd: string, agentDir?: string): SettingsManager;
133
+ /** Create a SettingsManager from an arbitrary storage backend */
134
+ static fromStorage(storage: SettingsStorage): SettingsManager;
135
+ /** Create an in-memory SettingsManager (no file I/O) */
136
+ static inMemory(settings?: Partial<Settings>): SettingsManager;
137
+ private static loadFromStorage;
138
+ private static tryLoadFromStorage;
139
+ /** Migrate old settings format to new format */
140
+ private static migrateSettings;
141
+ getGlobalSettings(): Settings;
142
+ getProjectSettings(): Settings;
143
+ reload(): Promise<void>;
144
+ /** Apply additional overrides on top of current settings */
145
+ applyOverrides(overrides: Partial<Settings>): void;
146
+ /** Mark a global field as modified during this session */
147
+ private markModified;
148
+ /** Mark a project field as modified during this session */
149
+ private markProjectModified;
150
+ private recordError;
151
+ private clearModifiedScope;
152
+ private enqueueWrite;
153
+ private cloneModifiedNestedFields;
154
+ private persistScopedSettings;
155
+ private save;
156
+ private saveProjectSettings;
157
+ flush(): Promise<void>;
158
+ drainErrors(): SettingsError[];
159
+ getLastChangelogVersion(): string | undefined;
160
+ setLastChangelogVersion(version: string): void;
161
+ getSessionDir(): string | undefined;
162
+ getDefaultProvider(): string | undefined;
163
+ getDefaultModel(): string | undefined;
164
+ setDefaultProvider(provider: string): void;
165
+ setDefaultModel(modelId: string): void;
166
+ setDefaultModelAndProvider(provider: string, modelId: string): void;
167
+ getSteeringMode(): "all" | "one-at-a-time";
168
+ setSteeringMode(mode: "all" | "one-at-a-time"): void;
169
+ getFollowUpMode(): "all" | "one-at-a-time";
170
+ setFollowUpMode(mode: "all" | "one-at-a-time"): void;
171
+ getTheme(): string | undefined;
172
+ setTheme(theme: string): void;
173
+ getDefaultThinkingLevel(): "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | undefined;
174
+ setDefaultThinkingLevel(level: "off" | "minimal" | "low" | "medium" | "high" | "xhigh"): void;
175
+ getTransport(): TransportSetting;
176
+ setTransport(transport: TransportSetting): void;
177
+ getCompactionEnabled(): boolean;
178
+ setCompactionEnabled(enabled: boolean): void;
179
+ getCompactionReserveTokens(): number;
180
+ getCompactionKeepRecentTokens(): number;
181
+ getCompactionSettings(): {
182
+ enabled: boolean;
183
+ reserveTokens: number;
184
+ keepRecentTokens: number;
185
+ };
186
+ getBranchSummarySettings(): {
187
+ reserveTokens: number;
188
+ skipPrompt: boolean;
189
+ };
190
+ getBranchSummarySkipPrompt(): boolean;
191
+ getRetryEnabled(): boolean;
192
+ setRetryEnabled(enabled: boolean): void;
193
+ getRetrySettings(): {
194
+ enabled: boolean;
195
+ maxRetries: number;
196
+ baseDelayMs: number;
197
+ };
198
+ getHttpIdleTimeoutMs(): number;
199
+ setHttpIdleTimeoutMs(timeoutMs: number): void;
200
+ getProviderRetrySettings(): {
201
+ timeoutMs?: number;
202
+ maxRetries?: number;
203
+ maxRetryDelayMs: number;
204
+ };
205
+ getWebSocketConnectTimeoutMs(): number | undefined;
206
+ getHideThinkingBlock(): boolean;
207
+ setHideThinkingBlock(hide: boolean): void;
208
+ getShellPath(): string | undefined;
209
+ setShellPath(path: string | undefined): void;
210
+ getQuietStartup(): boolean;
211
+ setQuietStartup(quiet: boolean): void;
212
+ getShellCommandPrefix(): string | undefined;
213
+ setShellCommandPrefix(prefix: string | undefined): void;
214
+ getNpmCommand(): string[] | undefined;
215
+ setNpmCommand(command: string[] | undefined): void;
216
+ getCollapseChangelog(): boolean;
217
+ setCollapseChangelog(collapse: boolean): void;
218
+ getEnableInstallTelemetry(): boolean;
219
+ setEnableInstallTelemetry(enabled: boolean): void;
220
+ getPackages(): PackageSource[];
221
+ setPackages(packages: PackageSource[]): void;
222
+ setProjectPackages(packages: PackageSource[]): void;
223
+ getExtensionPaths(): string[];
224
+ setExtensionPaths(paths: string[]): void;
225
+ setProjectExtensionPaths(paths: string[]): void;
226
+ getSkillPaths(): string[];
227
+ setSkillPaths(paths: string[]): void;
228
+ setProjectSkillPaths(paths: string[]): void;
229
+ getPromptTemplatePaths(): string[];
230
+ setPromptTemplatePaths(paths: string[]): void;
231
+ setProjectPromptTemplatePaths(paths: string[]): void;
232
+ getThemePaths(): string[];
233
+ setThemePaths(paths: string[]): void;
234
+ setProjectThemePaths(paths: string[]): void;
235
+ getEnableSkillCommands(): boolean;
236
+ setEnableSkillCommands(enabled: boolean): void;
237
+ getThinkingBudgets(): ThinkingBudgetsSettings | undefined;
238
+ getShowImages(): boolean;
239
+ setShowImages(show: boolean): void;
240
+ getImageWidthCells(): number;
241
+ setImageWidthCells(width: number): void;
242
+ getClearOnShrink(): boolean;
243
+ setClearOnShrink(enabled: boolean): void;
244
+ getShowTerminalProgress(): boolean;
245
+ setShowTerminalProgress(enabled: boolean): void;
246
+ getImageAutoResize(): boolean;
247
+ setImageAutoResize(enabled: boolean): void;
248
+ getBlockImages(): boolean;
249
+ setBlockImages(blocked: boolean): void;
250
+ getEnabledModels(): string[] | undefined;
251
+ setEnabledModels(patterns: string[] | undefined): void;
252
+ getDoubleEscapeAction(): "fork" | "tree" | "none";
253
+ setDoubleEscapeAction(action: "fork" | "tree" | "none"): void;
254
+ getTreeFilterMode(): "default" | "no-tools" | "user-only" | "labeled-only" | "all";
255
+ setTreeFilterMode(mode: "default" | "no-tools" | "user-only" | "labeled-only" | "all"): void;
256
+ getShowHardwareCursor(): boolean;
257
+ setShowHardwareCursor(enabled: boolean): void;
258
+ getEditorPaddingX(): number;
259
+ setEditorPaddingX(padding: number): void;
260
+ getAutocompleteMaxVisible(): number;
261
+ setAutocompleteMaxVisible(maxVisible: number): void;
262
+ getCodeBlockIndent(): string;
263
+ getWarnings(): WarningSettings;
264
+ setWarnings(warnings: WarningSettings): void;
265
+ }