@vandeepunk/pi-coding-agent 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (595) hide show
  1. package/CHANGELOG.md +2564 -0
  2. package/README.md +555 -0
  3. package/dist/cli/args.d.ts +47 -0
  4. package/dist/cli/args.d.ts.map +1 -0
  5. package/dist/cli/args.js +286 -0
  6. package/dist/cli/args.js.map +1 -0
  7. package/dist/cli/config-selector.d.ts +14 -0
  8. package/dist/cli/config-selector.d.ts.map +1 -0
  9. package/dist/cli/config-selector.js +31 -0
  10. package/dist/cli/config-selector.js.map +1 -0
  11. package/dist/cli/file-processor.d.ts +15 -0
  12. package/dist/cli/file-processor.d.ts.map +1 -0
  13. package/dist/cli/file-processor.js +79 -0
  14. package/dist/cli/file-processor.js.map +1 -0
  15. package/dist/cli/list-models.d.ts +9 -0
  16. package/dist/cli/list-models.d.ts.map +1 -0
  17. package/dist/cli/list-models.js +92 -0
  18. package/dist/cli/list-models.js.map +1 -0
  19. package/dist/cli/session-picker.d.ts +9 -0
  20. package/dist/cli/session-picker.d.ts.map +1 -0
  21. package/dist/cli/session-picker.js +34 -0
  22. package/dist/cli/session-picker.js.map +1 -0
  23. package/dist/cli.d.ts +3 -0
  24. package/dist/cli.d.ts.map +1 -0
  25. package/dist/cli.js +11 -0
  26. package/dist/cli.js.map +1 -0
  27. package/dist/config.d.ts +68 -0
  28. package/dist/config.d.ts.map +1 -0
  29. package/dist/config.js +203 -0
  30. package/dist/config.js.map +1 -0
  31. package/dist/core/agent-session.d.ts +574 -0
  32. package/dist/core/agent-session.d.ts.map +1 -0
  33. package/dist/core/agent-session.js +2260 -0
  34. package/dist/core/agent-session.js.map +1 -0
  35. package/dist/core/auth-storage.d.ts +102 -0
  36. package/dist/core/auth-storage.d.ts.map +1 -0
  37. package/dist/core/auth-storage.js +282 -0
  38. package/dist/core/auth-storage.js.map +1 -0
  39. package/dist/core/bash-executor.d.ts +47 -0
  40. package/dist/core/bash-executor.d.ts.map +1 -0
  41. package/dist/core/bash-executor.js +212 -0
  42. package/dist/core/bash-executor.js.map +1 -0
  43. package/dist/core/compaction/branch-summarization.d.ts +86 -0
  44. package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
  45. package/dist/core/compaction/branch-summarization.js +242 -0
  46. package/dist/core/compaction/branch-summarization.js.map +1 -0
  47. package/dist/core/compaction/compaction.d.ts +121 -0
  48. package/dist/core/compaction/compaction.d.ts.map +1 -0
  49. package/dist/core/compaction/compaction.js +607 -0
  50. package/dist/core/compaction/compaction.js.map +1 -0
  51. package/dist/core/compaction/index.d.ts +7 -0
  52. package/dist/core/compaction/index.d.ts.map +1 -0
  53. package/dist/core/compaction/index.js +7 -0
  54. package/dist/core/compaction/index.js.map +1 -0
  55. package/dist/core/compaction/utils.d.ts +35 -0
  56. package/dist/core/compaction/utils.d.ts.map +1 -0
  57. package/dist/core/compaction/utils.js +138 -0
  58. package/dist/core/compaction/utils.js.map +1 -0
  59. package/dist/core/defaults.d.ts +3 -0
  60. package/dist/core/defaults.d.ts.map +1 -0
  61. package/dist/core/defaults.js +2 -0
  62. package/dist/core/defaults.js.map +1 -0
  63. package/dist/core/diagnostics.d.ts +15 -0
  64. package/dist/core/diagnostics.d.ts.map +1 -0
  65. package/dist/core/diagnostics.js +2 -0
  66. package/dist/core/diagnostics.js.map +1 -0
  67. package/dist/core/event-bus.d.ts +9 -0
  68. package/dist/core/event-bus.d.ts.map +1 -0
  69. package/dist/core/event-bus.js +25 -0
  70. package/dist/core/event-bus.js.map +1 -0
  71. package/dist/core/exec.d.ts +29 -0
  72. package/dist/core/exec.d.ts.map +1 -0
  73. package/dist/core/exec.js +71 -0
  74. package/dist/core/exec.js.map +1 -0
  75. package/dist/core/export-html/ansi-to-html.d.ts +22 -0
  76. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -0
  77. package/dist/core/export-html/ansi-to-html.js +249 -0
  78. package/dist/core/export-html/ansi-to-html.js.map +1 -0
  79. package/dist/core/export-html/index.d.ts +34 -0
  80. package/dist/core/export-html/index.d.ts.map +1 -0
  81. package/dist/core/export-html/index.js +222 -0
  82. package/dist/core/export-html/index.js.map +1 -0
  83. package/dist/core/export-html/template.css +909 -0
  84. package/dist/core/export-html/template.html +54 -0
  85. package/dist/core/export-html/template.js +1549 -0
  86. package/dist/core/export-html/tool-renderer.d.ts +35 -0
  87. package/dist/core/export-html/tool-renderer.d.ts.map +1 -0
  88. package/dist/core/export-html/tool-renderer.js +57 -0
  89. package/dist/core/export-html/tool-renderer.js.map +1 -0
  90. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  91. package/dist/core/export-html/vendor/marked.min.js +6 -0
  92. package/dist/core/extensions/index.d.ts +11 -0
  93. package/dist/core/extensions/index.d.ts.map +1 -0
  94. package/dist/core/extensions/index.js +9 -0
  95. package/dist/core/extensions/index.js.map +1 -0
  96. package/dist/core/extensions/loader.d.ts +25 -0
  97. package/dist/core/extensions/loader.d.ts.map +1 -0
  98. package/dist/core/extensions/loader.js +400 -0
  99. package/dist/core/extensions/loader.js.map +1 -0
  100. package/dist/core/extensions/runner.d.ts +129 -0
  101. package/dist/core/extensions/runner.d.ts.map +1 -0
  102. package/dist/core/extensions/runner.js +576 -0
  103. package/dist/core/extensions/runner.js.map +1 -0
  104. package/dist/core/extensions/types.d.ts +928 -0
  105. package/dist/core/extensions/types.d.ts.map +1 -0
  106. package/dist/core/extensions/types.js +35 -0
  107. package/dist/core/extensions/types.js.map +1 -0
  108. package/dist/core/extensions/wrapper.d.ts +27 -0
  109. package/dist/core/extensions/wrapper.d.ts.map +1 -0
  110. package/dist/core/extensions/wrapper.js +102 -0
  111. package/dist/core/extensions/wrapper.js.map +1 -0
  112. package/dist/core/footer-data-provider.d.ts +32 -0
  113. package/dist/core/footer-data-provider.d.ts.map +1 -0
  114. package/dist/core/footer-data-provider.js +134 -0
  115. package/dist/core/footer-data-provider.js.map +1 -0
  116. package/dist/core/index.d.ts +9 -0
  117. package/dist/core/index.d.ts.map +1 -0
  118. package/dist/core/index.js +9 -0
  119. package/dist/core/index.js.map +1 -0
  120. package/dist/core/keybindings.d.ts +55 -0
  121. package/dist/core/keybindings.d.ts.map +1 -0
  122. package/dist/core/keybindings.js +153 -0
  123. package/dist/core/keybindings.js.map +1 -0
  124. package/dist/core/messages.d.ts +77 -0
  125. package/dist/core/messages.d.ts.map +1 -0
  126. package/dist/core/messages.js +123 -0
  127. package/dist/core/messages.js.map +1 -0
  128. package/dist/core/model-registry.d.ts +100 -0
  129. package/dist/core/model-registry.d.ts.map +1 -0
  130. package/dist/core/model-registry.js +419 -0
  131. package/dist/core/model-registry.js.map +1 -0
  132. package/dist/core/model-resolver.d.ts +76 -0
  133. package/dist/core/model-resolver.d.ts.map +1 -0
  134. package/dist/core/model-resolver.js +313 -0
  135. package/dist/core/model-resolver.js.map +1 -0
  136. package/dist/core/package-manager.d.ts +131 -0
  137. package/dist/core/package-manager.d.ts.map +1 -0
  138. package/dist/core/package-manager.js +1290 -0
  139. package/dist/core/package-manager.js.map +1 -0
  140. package/dist/core/prompt-templates.d.ts +50 -0
  141. package/dist/core/prompt-templates.d.ts.map +1 -0
  142. package/dist/core/prompt-templates.js +251 -0
  143. package/dist/core/prompt-templates.js.map +1 -0
  144. package/dist/core/resolve-config-value.d.ts +17 -0
  145. package/dist/core/resolve-config-value.d.ts.map +1 -0
  146. package/dist/core/resolve-config-value.js +59 -0
  147. package/dist/core/resolve-config-value.js.map +1 -0
  148. package/dist/core/resource-loader.d.ts +184 -0
  149. package/dist/core/resource-loader.d.ts.map +1 -0
  150. package/dist/core/resource-loader.js +673 -0
  151. package/dist/core/resource-loader.js.map +1 -0
  152. package/dist/core/sdk.d.ts +90 -0
  153. package/dist/core/sdk.d.ts.map +1 -0
  154. package/dist/core/sdk.js +234 -0
  155. package/dist/core/sdk.js.map +1 -0
  156. package/dist/core/session-manager.d.ts +323 -0
  157. package/dist/core/session-manager.d.ts.map +1 -0
  158. package/dist/core/session-manager.js +1091 -0
  159. package/dist/core/session-manager.js.map +1 -0
  160. package/dist/core/settings-manager.d.ts +187 -0
  161. package/dist/core/settings-manager.d.ts.map +1 -0
  162. package/dist/core/settings-manager.js +552 -0
  163. package/dist/core/settings-manager.js.map +1 -0
  164. package/dist/core/skills.d.ts +58 -0
  165. package/dist/core/skills.d.ts.map +1 -0
  166. package/dist/core/skills.js +310 -0
  167. package/dist/core/skills.js.map +1 -0
  168. package/dist/core/slash-commands.d.ts +15 -0
  169. package/dist/core/slash-commands.d.ts.map +1 -0
  170. package/dist/core/slash-commands.js +21 -0
  171. package/dist/core/slash-commands.js.map +1 -0
  172. package/dist/core/system-prompt.d.ts +24 -0
  173. package/dist/core/system-prompt.d.ts.map +1 -0
  174. package/dist/core/system-prompt.js +137 -0
  175. package/dist/core/system-prompt.js.map +1 -0
  176. package/dist/core/timings.d.ts +7 -0
  177. package/dist/core/timings.d.ts.map +1 -0
  178. package/dist/core/timings.js +25 -0
  179. package/dist/core/timings.js.map +1 -0
  180. package/dist/core/tools/bash.d.ts +55 -0
  181. package/dist/core/tools/bash.d.ts.map +1 -0
  182. package/dist/core/tools/bash.js +242 -0
  183. package/dist/core/tools/bash.js.map +1 -0
  184. package/dist/core/tools/edit-diff.d.ts +63 -0
  185. package/dist/core/tools/edit-diff.d.ts.map +1 -0
  186. package/dist/core/tools/edit-diff.js +243 -0
  187. package/dist/core/tools/edit-diff.js.map +1 -0
  188. package/dist/core/tools/edit.d.ts +39 -0
  189. package/dist/core/tools/edit.d.ts.map +1 -0
  190. package/dist/core/tools/edit.js +146 -0
  191. package/dist/core/tools/edit.js.map +1 -0
  192. package/dist/core/tools/find.d.ts +39 -0
  193. package/dist/core/tools/find.d.ts.map +1 -0
  194. package/dist/core/tools/find.js +206 -0
  195. package/dist/core/tools/find.js.map +1 -0
  196. package/dist/core/tools/grep.d.ts +45 -0
  197. package/dist/core/tools/grep.d.ts.map +1 -0
  198. package/dist/core/tools/grep.js +239 -0
  199. package/dist/core/tools/grep.js.map +1 -0
  200. package/dist/core/tools/index.d.ts +73 -0
  201. package/dist/core/tools/index.d.ts.map +1 -0
  202. package/dist/core/tools/index.js +61 -0
  203. package/dist/core/tools/index.js.map +1 -0
  204. package/dist/core/tools/ls.d.ts +40 -0
  205. package/dist/core/tools/ls.d.ts.map +1 -0
  206. package/dist/core/tools/ls.js +118 -0
  207. package/dist/core/tools/ls.js.map +1 -0
  208. package/dist/core/tools/path-utils.d.ts +8 -0
  209. package/dist/core/tools/path-utils.d.ts.map +1 -0
  210. package/dist/core/tools/path-utils.js +81 -0
  211. package/dist/core/tools/path-utils.js.map +1 -0
  212. package/dist/core/tools/read.d.ts +39 -0
  213. package/dist/core/tools/read.d.ts.map +1 -0
  214. package/dist/core/tools/read.js +166 -0
  215. package/dist/core/tools/read.js.map +1 -0
  216. package/dist/core/tools/truncate.d.ts +70 -0
  217. package/dist/core/tools/truncate.d.ts.map +1 -0
  218. package/dist/core/tools/truncate.js +205 -0
  219. package/dist/core/tools/truncate.js.map +1 -0
  220. package/dist/core/tools/write.d.ts +29 -0
  221. package/dist/core/tools/write.d.ts.map +1 -0
  222. package/dist/core/tools/write.js +78 -0
  223. package/dist/core/tools/write.js.map +1 -0
  224. package/dist/index.d.ts +27 -0
  225. package/dist/index.d.ts.map +1 -0
  226. package/dist/index.js +42 -0
  227. package/dist/index.js.map +1 -0
  228. package/dist/main.d.ts +8 -0
  229. package/dist/main.d.ts.map +1 -0
  230. package/dist/main.js +623 -0
  231. package/dist/main.js.map +1 -0
  232. package/dist/migrations.d.ts +33 -0
  233. package/dist/migrations.d.ts.map +1 -0
  234. package/dist/migrations.js +261 -0
  235. package/dist/migrations.js.map +1 -0
  236. package/dist/modes/index.d.ts +9 -0
  237. package/dist/modes/index.d.ts.map +1 -0
  238. package/dist/modes/index.js +8 -0
  239. package/dist/modes/index.js.map +1 -0
  240. package/dist/modes/interactive/components/armin.d.ts +34 -0
  241. package/dist/modes/interactive/components/armin.d.ts.map +1 -0
  242. package/dist/modes/interactive/components/armin.js +333 -0
  243. package/dist/modes/interactive/components/armin.js.map +1 -0
  244. package/dist/modes/interactive/components/assistant-message.d.ts +16 -0
  245. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
  246. package/dist/modes/interactive/components/assistant-message.js +91 -0
  247. package/dist/modes/interactive/components/assistant-message.js.map +1 -0
  248. package/dist/modes/interactive/components/bash-execution.d.ts +35 -0
  249. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -0
  250. package/dist/modes/interactive/components/bash-execution.js +162 -0
  251. package/dist/modes/interactive/components/bash-execution.js.map +1 -0
  252. package/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
  253. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
  254. package/dist/modes/interactive/components/bordered-loader.js +51 -0
  255. package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
  256. package/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
  257. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
  258. package/dist/modes/interactive/components/branch-summary-message.js +44 -0
  259. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
  260. package/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
  261. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
  262. package/dist/modes/interactive/components/compaction-summary-message.js +45 -0
  263. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -0
  264. package/dist/modes/interactive/components/config-selector.d.ts +71 -0
  265. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
  266. package/dist/modes/interactive/components/config-selector.js +479 -0
  267. package/dist/modes/interactive/components/config-selector.js.map +1 -0
  268. package/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
  269. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -0
  270. package/dist/modes/interactive/components/countdown-timer.js +33 -0
  271. package/dist/modes/interactive/components/countdown-timer.js.map +1 -0
  272. package/dist/modes/interactive/components/custom-editor.d.ts +21 -0
  273. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -0
  274. package/dist/modes/interactive/components/custom-editor.js +70 -0
  275. package/dist/modes/interactive/components/custom-editor.js.map +1 -0
  276. package/dist/modes/interactive/components/custom-message.d.ts +20 -0
  277. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
  278. package/dist/modes/interactive/components/custom-message.js +79 -0
  279. package/dist/modes/interactive/components/custom-message.js.map +1 -0
  280. package/dist/modes/interactive/components/daxnuts.d.ts +23 -0
  281. package/dist/modes/interactive/components/daxnuts.d.ts.map +1 -0
  282. package/dist/modes/interactive/components/daxnuts.js +140 -0
  283. package/dist/modes/interactive/components/daxnuts.js.map +1 -0
  284. package/dist/modes/interactive/components/diff.d.ts +12 -0
  285. package/dist/modes/interactive/components/diff.d.ts.map +1 -0
  286. package/dist/modes/interactive/components/diff.js +133 -0
  287. package/dist/modes/interactive/components/diff.js.map +1 -0
  288. package/dist/modes/interactive/components/dynamic-border.d.ts +15 -0
  289. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -0
  290. package/dist/modes/interactive/components/dynamic-border.js +21 -0
  291. package/dist/modes/interactive/components/dynamic-border.js.map +1 -0
  292. package/dist/modes/interactive/components/extension-editor.d.ts +17 -0
  293. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
  294. package/dist/modes/interactive/components/extension-editor.js +102 -0
  295. package/dist/modes/interactive/components/extension-editor.js.map +1 -0
  296. package/dist/modes/interactive/components/extension-input.d.ts +23 -0
  297. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
  298. package/dist/modes/interactive/components/extension-input.js +61 -0
  299. package/dist/modes/interactive/components/extension-input.js.map +1 -0
  300. package/dist/modes/interactive/components/extension-selector.d.ts +24 -0
  301. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -0
  302. package/dist/modes/interactive/components/extension-selector.js +78 -0
  303. package/dist/modes/interactive/components/extension-selector.js.map +1 -0
  304. package/dist/modes/interactive/components/footer.d.ts +26 -0
  305. package/dist/modes/interactive/components/footer.d.ts.map +1 -0
  306. package/dist/modes/interactive/components/footer.js +220 -0
  307. package/dist/modes/interactive/components/footer.js.map +1 -0
  308. package/dist/modes/interactive/components/index.d.ts +32 -0
  309. package/dist/modes/interactive/components/index.d.ts.map +1 -0
  310. package/dist/modes/interactive/components/index.js +33 -0
  311. package/dist/modes/interactive/components/index.js.map +1 -0
  312. package/dist/modes/interactive/components/keybinding-hints.d.ts +41 -0
  313. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -0
  314. package/dist/modes/interactive/components/keybinding-hints.js +61 -0
  315. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -0
  316. package/dist/modes/interactive/components/login-dialog.d.ts +42 -0
  317. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -0
  318. package/dist/modes/interactive/components/login-dialog.js +145 -0
  319. package/dist/modes/interactive/components/login-dialog.js.map +1 -0
  320. package/dist/modes/interactive/components/model-selector.d.ts +47 -0
  321. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
  322. package/dist/modes/interactive/components/model-selector.js +266 -0
  323. package/dist/modes/interactive/components/model-selector.js.map +1 -0
  324. package/dist/modes/interactive/components/oauth-selector.d.ts +19 -0
  325. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -0
  326. package/dist/modes/interactive/components/oauth-selector.js +97 -0
  327. package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
  328. package/dist/modes/interactive/components/scoped-models-selector.d.ts +49 -0
  329. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
  330. package/dist/modes/interactive/components/scoped-models-selector.js +270 -0
  331. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
  332. package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
  333. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
  334. package/dist/modes/interactive/components/session-selector-search.js +155 -0
  335. package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
  336. package/dist/modes/interactive/components/session-selector.d.ts +95 -0
  337. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
  338. package/dist/modes/interactive/components/session-selector.js +851 -0
  339. package/dist/modes/interactive/components/session-selector.js.map +1 -0
  340. package/dist/modes/interactive/components/settings-selector.d.ts +53 -0
  341. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
  342. package/dist/modes/interactive/components/settings-selector.js +277 -0
  343. package/dist/modes/interactive/components/settings-selector.js.map +1 -0
  344. package/dist/modes/interactive/components/show-images-selector.d.ts +10 -0
  345. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -0
  346. package/dist/modes/interactive/components/show-images-selector.js +35 -0
  347. package/dist/modes/interactive/components/show-images-selector.js.map +1 -0
  348. package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
  349. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
  350. package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
  351. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
  352. package/dist/modes/interactive/components/theme-selector.d.ts +11 -0
  353. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -0
  354. package/dist/modes/interactive/components/theme-selector.js +46 -0
  355. package/dist/modes/interactive/components/theme-selector.js.map +1 -0
  356. package/dist/modes/interactive/components/thinking-selector.d.ts +11 -0
  357. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -0
  358. package/dist/modes/interactive/components/thinking-selector.js +47 -0
  359. package/dist/modes/interactive/components/thinking-selector.js.map +1 -0
  360. package/dist/modes/interactive/components/tool-execution.d.ts +70 -0
  361. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -0
  362. package/dist/modes/interactive/components/tool-execution.js +621 -0
  363. package/dist/modes/interactive/components/tool-execution.js.map +1 -0
  364. package/dist/modes/interactive/components/tree-selector.d.ts +68 -0
  365. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
  366. package/dist/modes/interactive/components/tree-selector.js +934 -0
  367. package/dist/modes/interactive/components/tree-selector.js.map +1 -0
  368. package/dist/modes/interactive/components/user-message-selector.d.ts +30 -0
  369. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -0
  370. package/dist/modes/interactive/components/user-message-selector.js +113 -0
  371. package/dist/modes/interactive/components/user-message-selector.js.map +1 -0
  372. package/dist/modes/interactive/components/user-message.d.ts +8 -0
  373. package/dist/modes/interactive/components/user-message.d.ts.map +1 -0
  374. package/dist/modes/interactive/components/user-message.js +16 -0
  375. package/dist/modes/interactive/components/user-message.js.map +1 -0
  376. package/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
  377. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -0
  378. package/dist/modes/interactive/components/visual-truncate.js +33 -0
  379. package/dist/modes/interactive/components/visual-truncate.js.map +1 -0
  380. package/dist/modes/interactive/interactive-mode.d.ts +313 -0
  381. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
  382. package/dist/modes/interactive/interactive-mode.js +3664 -0
  383. package/dist/modes/interactive/interactive-mode.js.map +1 -0
  384. package/dist/modes/interactive/theme/dark.json +85 -0
  385. package/dist/modes/interactive/theme/light.json +84 -0
  386. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  387. package/dist/modes/interactive/theme/theme.d.ts +78 -0
  388. package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
  389. package/dist/modes/interactive/theme/theme.js +944 -0
  390. package/dist/modes/interactive/theme/theme.js.map +1 -0
  391. package/dist/modes/print-mode.d.ts +28 -0
  392. package/dist/modes/print-mode.d.ts.map +1 -0
  393. package/dist/modes/print-mode.js +98 -0
  394. package/dist/modes/print-mode.js.map +1 -0
  395. package/dist/modes/rpc/rpc-client.d.ts +217 -0
  396. package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
  397. package/dist/modes/rpc/rpc-client.js +405 -0
  398. package/dist/modes/rpc/rpc-client.js.map +1 -0
  399. package/dist/modes/rpc/rpc-mode.d.ts +20 -0
  400. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -0
  401. package/dist/modes/rpc/rpc-mode.js +500 -0
  402. package/dist/modes/rpc/rpc-mode.js.map +1 -0
  403. package/dist/modes/rpc/rpc-types.d.ts +409 -0
  404. package/dist/modes/rpc/rpc-types.d.ts.map +1 -0
  405. package/dist/modes/rpc/rpc-types.js +8 -0
  406. package/dist/modes/rpc/rpc-types.js.map +1 -0
  407. package/dist/utils/changelog.d.ts +21 -0
  408. package/dist/utils/changelog.d.ts.map +1 -0
  409. package/dist/utils/changelog.js +87 -0
  410. package/dist/utils/changelog.js.map +1 -0
  411. package/dist/utils/clipboard-image.d.ts +11 -0
  412. package/dist/utils/clipboard-image.d.ts.map +1 -0
  413. package/dist/utils/clipboard-image.js +162 -0
  414. package/dist/utils/clipboard-image.js.map +1 -0
  415. package/dist/utils/clipboard-native.d.ts +7 -0
  416. package/dist/utils/clipboard-native.d.ts.map +1 -0
  417. package/dist/utils/clipboard-native.js +14 -0
  418. package/dist/utils/clipboard-native.js.map +1 -0
  419. package/dist/utils/clipboard.d.ts +2 -0
  420. package/dist/utils/clipboard.d.ts.map +1 -0
  421. package/dist/utils/clipboard.js +67 -0
  422. package/dist/utils/clipboard.js.map +1 -0
  423. package/dist/utils/frontmatter.d.ts +8 -0
  424. package/dist/utils/frontmatter.d.ts.map +1 -0
  425. package/dist/utils/frontmatter.js +26 -0
  426. package/dist/utils/frontmatter.js.map +1 -0
  427. package/dist/utils/git.d.ts +2 -0
  428. package/dist/utils/git.d.ts.map +1 -0
  429. package/dist/utils/git.js +6 -0
  430. package/dist/utils/git.js.map +1 -0
  431. package/dist/utils/image-convert.d.ts +9 -0
  432. package/dist/utils/image-convert.d.ts.map +1 -0
  433. package/dist/utils/image-convert.js +35 -0
  434. package/dist/utils/image-convert.js.map +1 -0
  435. package/dist/utils/image-resize.d.ts +36 -0
  436. package/dist/utils/image-resize.d.ts.map +1 -0
  437. package/dist/utils/image-resize.js +181 -0
  438. package/dist/utils/image-resize.js.map +1 -0
  439. package/dist/utils/mime.d.ts +2 -0
  440. package/dist/utils/mime.d.ts.map +1 -0
  441. package/dist/utils/mime.js +26 -0
  442. package/dist/utils/mime.js.map +1 -0
  443. package/dist/utils/photon.d.ts +21 -0
  444. package/dist/utils/photon.d.ts.map +1 -0
  445. package/dist/utils/photon.js +121 -0
  446. package/dist/utils/photon.js.map +1 -0
  447. package/dist/utils/shell.d.ts +26 -0
  448. package/dist/utils/shell.d.ts.map +1 -0
  449. package/dist/utils/shell.js +186 -0
  450. package/dist/utils/shell.js.map +1 -0
  451. package/dist/utils/sleep.d.ts +5 -0
  452. package/dist/utils/sleep.d.ts.map +1 -0
  453. package/dist/utils/sleep.js +17 -0
  454. package/dist/utils/sleep.js.map +1 -0
  455. package/dist/utils/tools-manager.d.ts +3 -0
  456. package/dist/utils/tools-manager.d.ts.map +1 -0
  457. package/dist/utils/tools-manager.js +201 -0
  458. package/dist/utils/tools-manager.js.map +1 -0
  459. package/docs/compaction.md +390 -0
  460. package/docs/custom-provider.md +539 -0
  461. package/docs/development.md +69 -0
  462. package/docs/extensions.md +1827 -0
  463. package/docs/images/doom-extension.png +0 -0
  464. package/docs/images/exy.png +0 -0
  465. package/docs/images/interactive-mode.png +0 -0
  466. package/docs/images/tree-view.png +0 -0
  467. package/docs/json.md +79 -0
  468. package/docs/keybindings.md +174 -0
  469. package/docs/models.md +254 -0
  470. package/docs/packages.md +191 -0
  471. package/docs/prompt-templates.md +67 -0
  472. package/docs/providers.md +168 -0
  473. package/docs/rpc.md +1311 -0
  474. package/docs/sdk.md +957 -0
  475. package/docs/session.md +412 -0
  476. package/docs/settings.md +221 -0
  477. package/docs/shell-aliases.md +13 -0
  478. package/docs/skills.md +227 -0
  479. package/docs/terminal-setup.md +70 -0
  480. package/docs/termux.md +127 -0
  481. package/docs/themes.md +295 -0
  482. package/docs/tree.md +219 -0
  483. package/docs/tui.md +887 -0
  484. package/docs/windows.md +17 -0
  485. package/examples/README.md +25 -0
  486. package/examples/extensions/README.md +202 -0
  487. package/examples/extensions/antigravity-image-gen.ts +413 -0
  488. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  489. package/examples/extensions/bash-spawn-hook.ts +30 -0
  490. package/examples/extensions/bookmark.ts +50 -0
  491. package/examples/extensions/claude-rules.ts +86 -0
  492. package/examples/extensions/commands.ts +72 -0
  493. package/examples/extensions/confirm-destructive.ts +59 -0
  494. package/examples/extensions/custom-compaction.ts +114 -0
  495. package/examples/extensions/custom-footer.ts +64 -0
  496. package/examples/extensions/custom-header.ts +73 -0
  497. package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
  498. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  499. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  500. package/examples/extensions/custom-provider-gitlab-duo/index.ts +349 -0
  501. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  502. package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
  503. package/examples/extensions/custom-provider-qwen-cli/index.ts +345 -0
  504. package/examples/extensions/custom-provider-qwen-cli/package.json +16 -0
  505. package/examples/extensions/dirty-repo-guard.ts +56 -0
  506. package/examples/extensions/doom-overlay/README.md +46 -0
  507. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  508. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  509. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  510. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  511. package/examples/extensions/doom-overlay/doom-component.ts +132 -0
  512. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  513. package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
  514. package/examples/extensions/doom-overlay/index.ts +74 -0
  515. package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
  516. package/examples/extensions/dynamic-resources/SKILL.md +8 -0
  517. package/examples/extensions/dynamic-resources/dynamic.json +79 -0
  518. package/examples/extensions/dynamic-resources/dynamic.md +5 -0
  519. package/examples/extensions/dynamic-resources/index.ts +15 -0
  520. package/examples/extensions/event-bus.ts +43 -0
  521. package/examples/extensions/file-trigger.ts +41 -0
  522. package/examples/extensions/git-checkpoint.ts +53 -0
  523. package/examples/extensions/handoff.ts +150 -0
  524. package/examples/extensions/hello.ts +25 -0
  525. package/examples/extensions/inline-bash.ts +94 -0
  526. package/examples/extensions/input-transform.ts +43 -0
  527. package/examples/extensions/interactive-shell.ts +196 -0
  528. package/examples/extensions/mac-system-theme.ts +47 -0
  529. package/examples/extensions/message-renderer.ts +59 -0
  530. package/examples/extensions/minimal-mode.ts +426 -0
  531. package/examples/extensions/modal-editor.ts +85 -0
  532. package/examples/extensions/model-status.ts +31 -0
  533. package/examples/extensions/notify.ts +55 -0
  534. package/examples/extensions/overlay-qa-tests.ts +881 -0
  535. package/examples/extensions/overlay-test.ts +150 -0
  536. package/examples/extensions/permission-gate.ts +34 -0
  537. package/examples/extensions/pirate.ts +47 -0
  538. package/examples/extensions/plan-mode/README.md +65 -0
  539. package/examples/extensions/plan-mode/index.ts +340 -0
  540. package/examples/extensions/plan-mode/utils.ts +168 -0
  541. package/examples/extensions/preset.ts +398 -0
  542. package/examples/extensions/protected-paths.ts +30 -0
  543. package/examples/extensions/qna.ts +119 -0
  544. package/examples/extensions/question.ts +264 -0
  545. package/examples/extensions/questionnaire.ts +427 -0
  546. package/examples/extensions/rainbow-editor.ts +88 -0
  547. package/examples/extensions/rpc-demo.ts +124 -0
  548. package/examples/extensions/sandbox/index.ts +318 -0
  549. package/examples/extensions/sandbox/package-lock.json +92 -0
  550. package/examples/extensions/sandbox/package.json +19 -0
  551. package/examples/extensions/send-user-message.ts +97 -0
  552. package/examples/extensions/session-name.ts +27 -0
  553. package/examples/extensions/shutdown-command.ts +63 -0
  554. package/examples/extensions/snake.ts +343 -0
  555. package/examples/extensions/space-invaders.ts +560 -0
  556. package/examples/extensions/ssh.ts +220 -0
  557. package/examples/extensions/status-line.ts +40 -0
  558. package/examples/extensions/subagent/README.md +172 -0
  559. package/examples/extensions/subagent/agents/planner.md +37 -0
  560. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  561. package/examples/extensions/subagent/agents/scout.md +50 -0
  562. package/examples/extensions/subagent/agents/worker.md +24 -0
  563. package/examples/extensions/subagent/agents.ts +127 -0
  564. package/examples/extensions/subagent/index.ts +963 -0
  565. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  566. package/examples/extensions/subagent/prompts/implement.md +10 -0
  567. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  568. package/examples/extensions/summarize.ts +195 -0
  569. package/examples/extensions/system-prompt-header.ts +17 -0
  570. package/examples/extensions/timed-confirm.ts +70 -0
  571. package/examples/extensions/titlebar-spinner.ts +58 -0
  572. package/examples/extensions/todo.ts +299 -0
  573. package/examples/extensions/tool-override.ts +143 -0
  574. package/examples/extensions/tools.ts +146 -0
  575. package/examples/extensions/trigger-compact.ts +40 -0
  576. package/examples/extensions/truncated-tool.ts +192 -0
  577. package/examples/extensions/widget-placement.ts +17 -0
  578. package/examples/extensions/with-deps/index.ts +36 -0
  579. package/examples/extensions/with-deps/package-lock.json +31 -0
  580. package/examples/extensions/with-deps/package.json +22 -0
  581. package/examples/rpc-extension-ui.ts +632 -0
  582. package/examples/sdk/01-minimal.ts +22 -0
  583. package/examples/sdk/02-custom-model.ts +49 -0
  584. package/examples/sdk/03-custom-prompt.ts +55 -0
  585. package/examples/sdk/04-skills.ts +46 -0
  586. package/examples/sdk/05-tools.ts +56 -0
  587. package/examples/sdk/06-extensions.ts +88 -0
  588. package/examples/sdk/07-context-files.ts +40 -0
  589. package/examples/sdk/08-prompt-templates.ts +47 -0
  590. package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
  591. package/examples/sdk/10-settings.ts +38 -0
  592. package/examples/sdk/11-sessions.ts +48 -0
  593. package/examples/sdk/12-full-control.ts +82 -0
  594. package/examples/sdk/README.md +144 -0
  595. package/package.json +97 -0
@@ -0,0 +1,3664 @@
1
+ /**
2
+ * Interactive mode for the coding agent.
3
+ * Handles TUI rendering and user interaction, delegating business logic to AgentSession.
4
+ */
5
+ import * as crypto from "node:crypto";
6
+ import * as fs from "node:fs";
7
+ import * as os from "node:os";
8
+ import * as path from "node:path";
9
+ import { getOAuthProviders, } from "@mariozechner/pi-ai";
10
+ import { CombinedAutocompleteProvider, Container, fuzzyFilter, Loader, Markdown, matchesKey, ProcessTerminal, Spacer, Text, TruncatedText, TUI, visibleWidth, } from "@mariozechner/pi-tui";
11
+ import { spawn, spawnSync } from "child_process";
12
+ import { APP_NAME, getAuthPath, getDebugLogPath, getShareViewerUrl, getUpdateInstruction, VERSION, } from "../../config.js";
13
+ import { parseSkillBlock } from "../../core/agent-session.js";
14
+ import { FooterDataProvider } from "../../core/footer-data-provider.js";
15
+ import { KeybindingsManager } from "../../core/keybindings.js";
16
+ import { createCompactionSummaryMessage } from "../../core/messages.js";
17
+ import { resolveModelScope } from "../../core/model-resolver.js";
18
+ import { SessionManager } from "../../core/session-manager.js";
19
+ import { BUILTIN_SLASH_COMMANDS } from "../../core/slash-commands.js";
20
+ import { getChangelogPath, getNewEntries, parseChangelog } from "../../utils/changelog.js";
21
+ import { copyToClipboard } from "../../utils/clipboard.js";
22
+ import { extensionForImageMimeType, readClipboardImage } from "../../utils/clipboard-image.js";
23
+ import { ensureTool } from "../../utils/tools-manager.js";
24
+ import { ArminComponent } from "./components/armin.js";
25
+ import { AssistantMessageComponent } from "./components/assistant-message.js";
26
+ import { BashExecutionComponent } from "./components/bash-execution.js";
27
+ import { BorderedLoader } from "./components/bordered-loader.js";
28
+ import { BranchSummaryMessageComponent } from "./components/branch-summary-message.js";
29
+ import { CompactionSummaryMessageComponent } from "./components/compaction-summary-message.js";
30
+ import { CustomEditor } from "./components/custom-editor.js";
31
+ import { CustomMessageComponent } from "./components/custom-message.js";
32
+ import { DaxnutsComponent } from "./components/daxnuts.js";
33
+ import { DynamicBorder } from "./components/dynamic-border.js";
34
+ import { ExtensionEditorComponent } from "./components/extension-editor.js";
35
+ import { ExtensionInputComponent } from "./components/extension-input.js";
36
+ import { ExtensionSelectorComponent } from "./components/extension-selector.js";
37
+ import { FooterComponent } from "./components/footer.js";
38
+ import { appKey, appKeyHint, editorKey, keyHint, rawKeyHint } from "./components/keybinding-hints.js";
39
+ import { LoginDialogComponent } from "./components/login-dialog.js";
40
+ import { ModelSelectorComponent } from "./components/model-selector.js";
41
+ import { OAuthSelectorComponent } from "./components/oauth-selector.js";
42
+ import { ScopedModelsSelectorComponent } from "./components/scoped-models-selector.js";
43
+ import { SessionSelectorComponent } from "./components/session-selector.js";
44
+ import { SettingsSelectorComponent } from "./components/settings-selector.js";
45
+ import { SkillInvocationMessageComponent } from "./components/skill-invocation-message.js";
46
+ import { ToolExecutionComponent } from "./components/tool-execution.js";
47
+ import { TreeSelectorComponent } from "./components/tree-selector.js";
48
+ import { UserMessageComponent } from "./components/user-message.js";
49
+ import { UserMessageSelectorComponent } from "./components/user-message-selector.js";
50
+ import { getAvailableThemes, getAvailableThemesWithPaths, getEditorTheme, getMarkdownTheme, getThemeByName, initTheme, onThemeChange, setRegisteredThemes, setTheme, setThemeInstance, Theme, theme, } from "./theme/theme.js";
51
+ function isExpandable(obj) {
52
+ return typeof obj === "object" && obj !== null && "setExpanded" in obj && typeof obj.setExpanded === "function";
53
+ }
54
+ export class InteractiveMode {
55
+ options;
56
+ session;
57
+ ui;
58
+ chatContainer;
59
+ pendingMessagesContainer;
60
+ statusContainer;
61
+ defaultEditor;
62
+ editor;
63
+ autocompleteProvider;
64
+ fdPath;
65
+ editorContainer;
66
+ footer;
67
+ footerDataProvider;
68
+ keybindings;
69
+ version;
70
+ isInitialized = false;
71
+ onInputCallback;
72
+ loadingAnimation = undefined;
73
+ pendingWorkingMessage = undefined;
74
+ defaultWorkingMessage = "Working...";
75
+ lastSigintTime = 0;
76
+ lastEscapeTime = 0;
77
+ changelogMarkdown = undefined;
78
+ // Status line tracking (for mutating immediately-sequential status updates)
79
+ lastStatusSpacer = undefined;
80
+ lastStatusText = undefined;
81
+ // Streaming message tracking
82
+ streamingComponent = undefined;
83
+ streamingMessage = undefined;
84
+ // Tool execution tracking: toolCallId -> component
85
+ pendingTools = new Map();
86
+ // Tool output expansion state
87
+ toolOutputExpanded = false;
88
+ // Thinking block visibility state
89
+ hideThinkingBlock = false;
90
+ // Skill commands: command name -> skill file path
91
+ skillCommands = new Map();
92
+ // Agent subscription unsubscribe function
93
+ unsubscribe;
94
+ // Track if editor is in bash mode (text starts with !)
95
+ isBashMode = false;
96
+ // Track current bash execution component
97
+ bashComponent = undefined;
98
+ // Track pending bash components (shown in pending area, moved to chat on submit)
99
+ pendingBashComponents = [];
100
+ // Auto-compaction state
101
+ autoCompactionLoader = undefined;
102
+ autoCompactionEscapeHandler;
103
+ // Auto-retry state
104
+ retryLoader = undefined;
105
+ retryEscapeHandler;
106
+ // Messages queued while compaction is running
107
+ compactionQueuedMessages = [];
108
+ // Shutdown state
109
+ shutdownRequested = false;
110
+ // Extension UI state
111
+ extensionSelector = undefined;
112
+ extensionInput = undefined;
113
+ extensionEditor = undefined;
114
+ // Extension widgets (components rendered above/below the editor)
115
+ extensionWidgetsAbove = new Map();
116
+ extensionWidgetsBelow = new Map();
117
+ widgetContainerAbove;
118
+ widgetContainerBelow;
119
+ // Custom footer from extension (undefined = use built-in footer)
120
+ customFooter = undefined;
121
+ // Header container that holds the built-in or custom header
122
+ headerContainer;
123
+ // Built-in header (logo + keybinding hints + changelog)
124
+ builtInHeader = undefined;
125
+ // Custom header from extension (undefined = use built-in header)
126
+ customHeader = undefined;
127
+ // Convenience accessors
128
+ get agent() {
129
+ return this.session.agent;
130
+ }
131
+ get sessionManager() {
132
+ return this.session.sessionManager;
133
+ }
134
+ get settingsManager() {
135
+ return this.session.settingsManager;
136
+ }
137
+ constructor(session, options = {}) {
138
+ this.options = options;
139
+ this.session = session;
140
+ this.version = VERSION;
141
+ this.ui = new TUI(new ProcessTerminal(), this.settingsManager.getShowHardwareCursor());
142
+ this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
143
+ this.headerContainer = new Container();
144
+ this.chatContainer = new Container();
145
+ this.pendingMessagesContainer = new Container();
146
+ this.statusContainer = new Container();
147
+ this.widgetContainerAbove = new Container();
148
+ this.widgetContainerBelow = new Container();
149
+ this.keybindings = KeybindingsManager.create();
150
+ const editorPaddingX = this.settingsManager.getEditorPaddingX();
151
+ const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
152
+ this.defaultEditor = new CustomEditor(this.ui, getEditorTheme(), this.keybindings, {
153
+ paddingX: editorPaddingX,
154
+ autocompleteMaxVisible,
155
+ });
156
+ this.editor = this.defaultEditor;
157
+ this.editorContainer = new Container();
158
+ this.editorContainer.addChild(this.editor);
159
+ this.footerDataProvider = new FooterDataProvider();
160
+ this.footer = new FooterComponent(session, this.footerDataProvider);
161
+ this.footer.setAutoCompactEnabled(session.autoCompactionEnabled);
162
+ // Load hide thinking block setting
163
+ this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
164
+ // Register themes from resource loader and initialize
165
+ setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
166
+ initTheme(this.settingsManager.getTheme(), true);
167
+ }
168
+ setupAutocomplete(fdPath) {
169
+ // Define commands for autocomplete
170
+ const slashCommands = BUILTIN_SLASH_COMMANDS.map((command) => ({
171
+ name: command.name,
172
+ description: command.description,
173
+ }));
174
+ const modelCommand = slashCommands.find((command) => command.name === "model");
175
+ if (modelCommand) {
176
+ modelCommand.getArgumentCompletions = (prefix) => {
177
+ // Get available models (scoped or from registry)
178
+ const models = this.session.scopedModels.length > 0
179
+ ? this.session.scopedModels.map((s) => s.model)
180
+ : this.session.modelRegistry.getAvailable();
181
+ if (models.length === 0)
182
+ return null;
183
+ // Create items with provider/id format
184
+ const items = models.map((m) => ({
185
+ id: m.id,
186
+ provider: m.provider,
187
+ label: `${m.provider}/${m.id}`,
188
+ }));
189
+ // Fuzzy filter by model ID + provider (allows "opus anthropic" to match)
190
+ const filtered = fuzzyFilter(items, prefix, (item) => `${item.id} ${item.provider}`);
191
+ if (filtered.length === 0)
192
+ return null;
193
+ return filtered.map((item) => ({
194
+ value: item.label,
195
+ label: item.id,
196
+ description: item.provider,
197
+ }));
198
+ };
199
+ }
200
+ // Convert prompt templates to SlashCommand format for autocomplete
201
+ const templateCommands = this.session.promptTemplates.map((cmd) => ({
202
+ name: cmd.name,
203
+ description: cmd.description,
204
+ }));
205
+ // Convert extension commands to SlashCommand format
206
+ const builtinCommandNames = new Set(slashCommands.map((c) => c.name));
207
+ const extensionCommands = (this.session.extensionRunner?.getRegisteredCommands(builtinCommandNames) ?? []).map((cmd) => ({
208
+ name: cmd.name,
209
+ description: cmd.description ?? "(extension command)",
210
+ getArgumentCompletions: cmd.getArgumentCompletions,
211
+ }));
212
+ // Build skill commands from session.skills (if enabled)
213
+ this.skillCommands.clear();
214
+ const skillCommandList = [];
215
+ if (this.settingsManager.getEnableSkillCommands()) {
216
+ for (const skill of this.session.resourceLoader.getSkills().skills) {
217
+ const commandName = `skill:${skill.name}`;
218
+ this.skillCommands.set(commandName, skill.filePath);
219
+ skillCommandList.push({ name: commandName, description: skill.description });
220
+ }
221
+ }
222
+ // Setup autocomplete
223
+ this.autocompleteProvider = new CombinedAutocompleteProvider([...slashCommands, ...templateCommands, ...extensionCommands, ...skillCommandList], process.cwd(), fdPath);
224
+ this.defaultEditor.setAutocompleteProvider(this.autocompleteProvider);
225
+ }
226
+ rebuildAutocomplete() {
227
+ this.setupAutocomplete(this.fdPath);
228
+ }
229
+ async init() {
230
+ if (this.isInitialized)
231
+ return;
232
+ // Load changelog (only show new entries, skip for resumed sessions)
233
+ this.changelogMarkdown = this.getChangelogForDisplay();
234
+ // Setup autocomplete with fd tool for file path completion
235
+ this.fdPath = await ensureTool("fd");
236
+ this.setupAutocomplete(this.fdPath);
237
+ // Add header container as first child
238
+ this.ui.addChild(this.headerContainer);
239
+ // Add header with keybindings from config (unless silenced)
240
+ if (this.options.verbose || !this.settingsManager.getQuietStartup()) {
241
+ const logo = theme.bold(theme.fg("accent", APP_NAME)) + theme.fg("dim", ` v${this.version}`);
242
+ // Build startup instructions using keybinding hint helpers
243
+ const kb = this.keybindings;
244
+ const hint = (action, desc) => appKeyHint(kb, action, desc);
245
+ const instructions = [
246
+ hint("interrupt", "to interrupt"),
247
+ hint("clear", "to clear"),
248
+ rawKeyHint(`${appKey(kb, "clear")} twice`, "to exit"),
249
+ hint("exit", "to exit (empty)"),
250
+ hint("suspend", "to suspend"),
251
+ keyHint("deleteToLineEnd", "to delete to end"),
252
+ hint("cycleThinkingLevel", "to cycle thinking level"),
253
+ rawKeyHint(`${appKey(kb, "cycleModelForward")}/${appKey(kb, "cycleModelBackward")}`, "to cycle models"),
254
+ hint("selectModel", "to select model"),
255
+ hint("expandTools", "to expand tools"),
256
+ hint("toggleThinking", "to expand thinking"),
257
+ hint("externalEditor", "for external editor"),
258
+ rawKeyHint("/", "for commands"),
259
+ rawKeyHint("!", "to run bash"),
260
+ rawKeyHint("!!", "to run bash (no context)"),
261
+ hint("followUp", "to queue follow-up"),
262
+ hint("dequeue", "to edit all queued messages"),
263
+ hint("pasteImage", "to paste image"),
264
+ rawKeyHint("drop files", "to attach"),
265
+ ].join("\n");
266
+ this.builtInHeader = new Text(`${logo}\n${instructions}`, 1, 0);
267
+ // Setup UI layout
268
+ this.headerContainer.addChild(new Spacer(1));
269
+ this.headerContainer.addChild(this.builtInHeader);
270
+ this.headerContainer.addChild(new Spacer(1));
271
+ // Add changelog if provided
272
+ if (this.changelogMarkdown) {
273
+ this.headerContainer.addChild(new DynamicBorder());
274
+ if (this.settingsManager.getCollapseChangelog()) {
275
+ const versionMatch = this.changelogMarkdown.match(/##\s+\[?(\d+\.\d+\.\d+)\]?/);
276
+ const latestVersion = versionMatch ? versionMatch[1] : this.version;
277
+ const condensedText = `Updated to v${latestVersion}. Use ${theme.bold("/changelog")} to view full changelog.`;
278
+ this.headerContainer.addChild(new Text(condensedText, 1, 0));
279
+ }
280
+ else {
281
+ this.headerContainer.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
282
+ this.headerContainer.addChild(new Spacer(1));
283
+ this.headerContainer.addChild(new Markdown(this.changelogMarkdown.trim(), 1, 0, this.getMarkdownThemeWithSettings()));
284
+ this.headerContainer.addChild(new Spacer(1));
285
+ }
286
+ this.headerContainer.addChild(new DynamicBorder());
287
+ }
288
+ }
289
+ else {
290
+ // Minimal header when silenced
291
+ this.builtInHeader = new Text("", 0, 0);
292
+ this.headerContainer.addChild(this.builtInHeader);
293
+ if (this.changelogMarkdown) {
294
+ // Still show changelog notification even in silent mode
295
+ this.headerContainer.addChild(new Spacer(1));
296
+ const versionMatch = this.changelogMarkdown.match(/##\s+\[?(\d+\.\d+\.\d+)\]?/);
297
+ const latestVersion = versionMatch ? versionMatch[1] : this.version;
298
+ const condensedText = `Updated to v${latestVersion}. Use ${theme.bold("/changelog")} to view full changelog.`;
299
+ this.headerContainer.addChild(new Text(condensedText, 1, 0));
300
+ }
301
+ }
302
+ this.ui.addChild(this.chatContainer);
303
+ this.ui.addChild(this.pendingMessagesContainer);
304
+ this.ui.addChild(this.statusContainer);
305
+ this.renderWidgets(); // Initialize with default spacer
306
+ this.ui.addChild(this.widgetContainerAbove);
307
+ this.ui.addChild(this.editorContainer);
308
+ this.ui.addChild(this.widgetContainerBelow);
309
+ this.ui.addChild(this.footer);
310
+ this.ui.setFocus(this.editor);
311
+ this.setupKeyHandlers();
312
+ this.setupEditorSubmitHandler();
313
+ // Initialize extensions first so resources are shown before messages
314
+ await this.initExtensions();
315
+ // Render initial messages AFTER showing loaded resources
316
+ this.renderInitialMessages();
317
+ // Start the UI
318
+ this.ui.start();
319
+ this.isInitialized = true;
320
+ // Set terminal title
321
+ this.updateTerminalTitle();
322
+ // Subscribe to agent events
323
+ this.subscribeToAgent();
324
+ // Set up theme file watcher
325
+ onThemeChange(() => {
326
+ this.ui.invalidate();
327
+ this.updateEditorBorderColor();
328
+ this.ui.requestRender();
329
+ });
330
+ // Set up git branch watcher (uses provider instead of footer)
331
+ this.footerDataProvider.onBranchChange(() => {
332
+ this.ui.requestRender();
333
+ });
334
+ // Initialize available provider count for footer display
335
+ await this.updateAvailableProviderCount();
336
+ }
337
+ /**
338
+ * Update terminal title with session name and cwd.
339
+ */
340
+ updateTerminalTitle() {
341
+ const cwdBasename = path.basename(process.cwd());
342
+ const sessionName = this.sessionManager.getSessionName();
343
+ if (sessionName) {
344
+ this.ui.terminal.setTitle(`π - ${sessionName} - ${cwdBasename}`);
345
+ }
346
+ else {
347
+ this.ui.terminal.setTitle(`π - ${cwdBasename}`);
348
+ }
349
+ }
350
+ /**
351
+ * Run the interactive mode. This is the main entry point.
352
+ * Initializes the UI, shows warnings, processes initial messages, and starts the interactive loop.
353
+ */
354
+ async run() {
355
+ await this.init();
356
+ // Start version check asynchronously
357
+ this.checkForNewVersion().then((newVersion) => {
358
+ if (newVersion) {
359
+ this.showNewVersionNotification(newVersion);
360
+ }
361
+ });
362
+ // Show startup warnings
363
+ const { migratedProviders, modelFallbackMessage, initialMessage, initialImages, initialMessages } = this.options;
364
+ if (migratedProviders && migratedProviders.length > 0) {
365
+ this.showWarning(`Migrated credentials to auth.json: ${migratedProviders.join(", ")}`);
366
+ }
367
+ const modelsJsonError = this.session.modelRegistry.getError();
368
+ if (modelsJsonError) {
369
+ this.showError(`models.json error: ${modelsJsonError}`);
370
+ }
371
+ if (modelFallbackMessage) {
372
+ this.showWarning(modelFallbackMessage);
373
+ }
374
+ // Process initial messages
375
+ if (initialMessage) {
376
+ try {
377
+ await this.session.prompt(initialMessage, { images: initialImages });
378
+ }
379
+ catch (error) {
380
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
381
+ this.showError(errorMessage);
382
+ }
383
+ }
384
+ if (initialMessages) {
385
+ for (const message of initialMessages) {
386
+ try {
387
+ await this.session.prompt(message);
388
+ }
389
+ catch (error) {
390
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
391
+ this.showError(errorMessage);
392
+ }
393
+ }
394
+ }
395
+ // Main interactive loop
396
+ while (true) {
397
+ const userInput = await this.getUserInput();
398
+ try {
399
+ await this.session.prompt(userInput);
400
+ }
401
+ catch (error) {
402
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
403
+ this.showError(errorMessage);
404
+ }
405
+ }
406
+ }
407
+ /**
408
+ * Check npm registry for a newer version.
409
+ */
410
+ async checkForNewVersion() {
411
+ if (process.env.PI_SKIP_VERSION_CHECK)
412
+ return undefined;
413
+ try {
414
+ const response = await fetch("https://registry.npmjs.org/@mariozechner/pi-coding-agent/latest");
415
+ if (!response.ok)
416
+ return undefined;
417
+ const data = (await response.json());
418
+ const latestVersion = data.version;
419
+ if (latestVersion && latestVersion !== this.version) {
420
+ return latestVersion;
421
+ }
422
+ return undefined;
423
+ }
424
+ catch {
425
+ return undefined;
426
+ }
427
+ }
428
+ /**
429
+ * Get changelog entries to display on startup.
430
+ * Only shows new entries since last seen version, skips for resumed sessions.
431
+ */
432
+ getChangelogForDisplay() {
433
+ // Skip changelog for resumed/continued sessions (already have messages)
434
+ if (this.session.state.messages.length > 0) {
435
+ return undefined;
436
+ }
437
+ const lastVersion = this.settingsManager.getLastChangelogVersion();
438
+ const changelogPath = getChangelogPath();
439
+ const entries = parseChangelog(changelogPath);
440
+ if (!lastVersion) {
441
+ // Fresh install - just record the version, don't show changelog
442
+ this.settingsManager.setLastChangelogVersion(VERSION);
443
+ return undefined;
444
+ }
445
+ else {
446
+ const newEntries = getNewEntries(entries, lastVersion);
447
+ if (newEntries.length > 0) {
448
+ this.settingsManager.setLastChangelogVersion(VERSION);
449
+ return newEntries.map((e) => e.content).join("\n\n");
450
+ }
451
+ }
452
+ return undefined;
453
+ }
454
+ getMarkdownThemeWithSettings() {
455
+ return {
456
+ ...getMarkdownTheme(),
457
+ codeBlockIndent: this.settingsManager.getCodeBlockIndent(),
458
+ };
459
+ }
460
+ // =========================================================================
461
+ // Extension System
462
+ // =========================================================================
463
+ formatDisplayPath(p) {
464
+ const home = os.homedir();
465
+ let result = p;
466
+ // Replace home directory with ~
467
+ if (result.startsWith(home)) {
468
+ result = `~${result.slice(home.length)}`;
469
+ }
470
+ return result;
471
+ }
472
+ /**
473
+ * Get a short path relative to the package root for display.
474
+ */
475
+ getShortPath(fullPath, source) {
476
+ // For npm packages, show path relative to node_modules/pkg/
477
+ const npmMatch = fullPath.match(/node_modules\/(@?[^/]+(?:\/[^/]+)?)\/(.*)/);
478
+ if (npmMatch && source.startsWith("npm:")) {
479
+ return npmMatch[2];
480
+ }
481
+ // For git packages, show path relative to repo root
482
+ const gitMatch = fullPath.match(/git\/[^/]+\/[^/]+\/(.*)/);
483
+ if (gitMatch && source.startsWith("git:")) {
484
+ return gitMatch[1];
485
+ }
486
+ // For local/auto, just use formatDisplayPath
487
+ return this.formatDisplayPath(fullPath);
488
+ }
489
+ getDisplaySourceInfo(source, scope) {
490
+ if (source === "local") {
491
+ if (scope === "user") {
492
+ return { label: "user", color: "muted" };
493
+ }
494
+ if (scope === "project") {
495
+ return { label: "project", color: "muted" };
496
+ }
497
+ if (scope === "temporary") {
498
+ return { label: "path", scopeLabel: "temp", color: "muted" };
499
+ }
500
+ return { label: "path", color: "muted" };
501
+ }
502
+ if (source === "cli") {
503
+ return { label: "path", scopeLabel: scope === "temporary" ? "temp" : undefined, color: "muted" };
504
+ }
505
+ const scopeLabel = scope === "user" ? "user" : scope === "project" ? "project" : scope === "temporary" ? "temp" : undefined;
506
+ return { label: source, scopeLabel, color: "accent" };
507
+ }
508
+ getScopeGroup(source, scope) {
509
+ if (source === "cli" || scope === "temporary")
510
+ return "path";
511
+ if (scope === "user")
512
+ return "user";
513
+ if (scope === "project")
514
+ return "project";
515
+ return "path";
516
+ }
517
+ isPackageSource(source) {
518
+ return source.startsWith("npm:") || source.startsWith("git:");
519
+ }
520
+ buildScopeGroups(paths, metadata) {
521
+ const groups = {
522
+ user: { scope: "user", paths: [], packages: new Map() },
523
+ project: { scope: "project", paths: [], packages: new Map() },
524
+ path: { scope: "path", paths: [], packages: new Map() },
525
+ };
526
+ for (const p of paths) {
527
+ const meta = this.findMetadata(p, metadata);
528
+ const source = meta?.source ?? "local";
529
+ const scope = meta?.scope ?? "project";
530
+ const groupKey = this.getScopeGroup(source, scope);
531
+ const group = groups[groupKey];
532
+ if (this.isPackageSource(source)) {
533
+ const list = group.packages.get(source) ?? [];
534
+ list.push(p);
535
+ group.packages.set(source, list);
536
+ }
537
+ else {
538
+ group.paths.push(p);
539
+ }
540
+ }
541
+ return [groups.user, groups.project, groups.path].filter((group) => group.paths.length > 0 || group.packages.size > 0);
542
+ }
543
+ formatScopeGroups(groups, options) {
544
+ const lines = [];
545
+ for (const group of groups) {
546
+ lines.push(` ${theme.fg("accent", group.scope)}`);
547
+ const sortedPaths = [...group.paths].sort((a, b) => a.localeCompare(b));
548
+ for (const p of sortedPaths) {
549
+ lines.push(theme.fg("dim", ` ${options.formatPath(p)}`));
550
+ }
551
+ const sortedPackages = Array.from(group.packages.entries()).sort(([a], [b]) => a.localeCompare(b));
552
+ for (const [source, paths] of sortedPackages) {
553
+ lines.push(` ${theme.fg("mdLink", source)}`);
554
+ const sortedPackagePaths = [...paths].sort((a, b) => a.localeCompare(b));
555
+ for (const p of sortedPackagePaths) {
556
+ lines.push(theme.fg("dim", ` ${options.formatPackagePath(p, source)}`));
557
+ }
558
+ }
559
+ }
560
+ return lines.join("\n");
561
+ }
562
+ /**
563
+ * Find metadata for a path, checking parent directories if exact match fails.
564
+ * Package manager stores metadata for directories, but we display file paths.
565
+ */
566
+ findMetadata(p, metadata) {
567
+ // Try exact match first
568
+ const exact = metadata.get(p);
569
+ if (exact)
570
+ return exact;
571
+ // Try parent directories (package manager stores directory paths)
572
+ let current = p;
573
+ while (current.includes("/")) {
574
+ current = current.substring(0, current.lastIndexOf("/"));
575
+ const parent = metadata.get(current);
576
+ if (parent)
577
+ return parent;
578
+ }
579
+ return undefined;
580
+ }
581
+ /**
582
+ * Format a path with its source/scope info from metadata.
583
+ */
584
+ formatPathWithSource(p, metadata) {
585
+ const meta = this.findMetadata(p, metadata);
586
+ if (meta) {
587
+ const shortPath = this.getShortPath(p, meta.source);
588
+ const { label, scopeLabel } = this.getDisplaySourceInfo(meta.source, meta.scope);
589
+ const labelText = scopeLabel ? `${label} (${scopeLabel})` : label;
590
+ return `${labelText} ${shortPath}`;
591
+ }
592
+ return this.formatDisplayPath(p);
593
+ }
594
+ /**
595
+ * Format resource diagnostics with nice collision display using metadata.
596
+ */
597
+ formatDiagnostics(diagnostics, metadata) {
598
+ const lines = [];
599
+ // Group collision diagnostics by name
600
+ const collisions = new Map();
601
+ const otherDiagnostics = [];
602
+ for (const d of diagnostics) {
603
+ if (d.type === "collision" && d.collision) {
604
+ const list = collisions.get(d.collision.name) ?? [];
605
+ list.push(d);
606
+ collisions.set(d.collision.name, list);
607
+ }
608
+ else {
609
+ otherDiagnostics.push(d);
610
+ }
611
+ }
612
+ // Format collision diagnostics grouped by name
613
+ for (const [name, collisionList] of collisions) {
614
+ const first = collisionList[0]?.collision;
615
+ if (!first)
616
+ continue;
617
+ lines.push(theme.fg("warning", ` "${name}" collision:`));
618
+ // Show winner
619
+ lines.push(theme.fg("dim", ` ${theme.fg("success", "✓")} ${this.formatPathWithSource(first.winnerPath, metadata)}`));
620
+ // Show all losers
621
+ for (const d of collisionList) {
622
+ if (d.collision) {
623
+ lines.push(theme.fg("dim", ` ${theme.fg("warning", "✗")} ${this.formatPathWithSource(d.collision.loserPath, metadata)} (skipped)`));
624
+ }
625
+ }
626
+ }
627
+ // Format other diagnostics (skill name collisions, parse errors, etc.)
628
+ for (const d of otherDiagnostics) {
629
+ if (d.path) {
630
+ // Use metadata-aware formatting for paths
631
+ const sourceInfo = this.formatPathWithSource(d.path, metadata);
632
+ lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${sourceInfo}`));
633
+ lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${d.message}`));
634
+ }
635
+ else {
636
+ lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${d.message}`));
637
+ }
638
+ }
639
+ return lines.join("\n");
640
+ }
641
+ showLoadedResources(options) {
642
+ const shouldShow = options?.force || this.options.verbose || !this.settingsManager.getQuietStartup();
643
+ if (!shouldShow) {
644
+ return;
645
+ }
646
+ const metadata = this.session.resourceLoader.getPathMetadata();
647
+ const sectionHeader = (name, color = "mdHeading") => theme.fg(color, `[${name}]`);
648
+ const contextFiles = this.session.resourceLoader.getAgentsFiles().agentsFiles;
649
+ if (contextFiles.length > 0) {
650
+ this.chatContainer.addChild(new Spacer(1));
651
+ const contextList = contextFiles.map((f) => theme.fg("dim", ` ${this.formatDisplayPath(f.path)}`)).join("\n");
652
+ this.chatContainer.addChild(new Text(`${sectionHeader("Context")}\n${contextList}`, 0, 0));
653
+ this.chatContainer.addChild(new Spacer(1));
654
+ }
655
+ const skills = this.session.resourceLoader.getSkills().skills;
656
+ if (skills.length > 0) {
657
+ const skillPaths = skills.map((s) => s.filePath);
658
+ const groups = this.buildScopeGroups(skillPaths, metadata);
659
+ const skillList = this.formatScopeGroups(groups, {
660
+ formatPath: (p) => this.formatDisplayPath(p),
661
+ formatPackagePath: (p, source) => this.getShortPath(p, source),
662
+ });
663
+ this.chatContainer.addChild(new Text(`${sectionHeader("Skills")}\n${skillList}`, 0, 0));
664
+ this.chatContainer.addChild(new Spacer(1));
665
+ }
666
+ const skillDiagnostics = this.session.resourceLoader.getSkills().diagnostics;
667
+ if (skillDiagnostics.length > 0) {
668
+ const warningLines = this.formatDiagnostics(skillDiagnostics, metadata);
669
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Skill conflicts]")}\n${warningLines}`, 0, 0));
670
+ this.chatContainer.addChild(new Spacer(1));
671
+ }
672
+ const templates = this.session.promptTemplates;
673
+ if (templates.length > 0) {
674
+ const templatePaths = templates.map((t) => t.filePath);
675
+ const groups = this.buildScopeGroups(templatePaths, metadata);
676
+ const templateByPath = new Map(templates.map((t) => [t.filePath, t]));
677
+ const templateList = this.formatScopeGroups(groups, {
678
+ formatPath: (p) => {
679
+ const template = templateByPath.get(p);
680
+ return template ? `/${template.name}` : this.formatDisplayPath(p);
681
+ },
682
+ formatPackagePath: (p) => {
683
+ const template = templateByPath.get(p);
684
+ return template ? `/${template.name}` : this.formatDisplayPath(p);
685
+ },
686
+ });
687
+ this.chatContainer.addChild(new Text(`${sectionHeader("Prompts")}\n${templateList}`, 0, 0));
688
+ this.chatContainer.addChild(new Spacer(1));
689
+ }
690
+ const promptDiagnostics = this.session.resourceLoader.getPrompts().diagnostics;
691
+ if (promptDiagnostics.length > 0) {
692
+ const warningLines = this.formatDiagnostics(promptDiagnostics, metadata);
693
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Prompt conflicts]")}\n${warningLines}`, 0, 0));
694
+ this.chatContainer.addChild(new Spacer(1));
695
+ }
696
+ const extensionPaths = options?.extensionPaths ?? [];
697
+ if (extensionPaths.length > 0) {
698
+ const groups = this.buildScopeGroups(extensionPaths, metadata);
699
+ const extList = this.formatScopeGroups(groups, {
700
+ formatPath: (p) => this.formatDisplayPath(p),
701
+ formatPackagePath: (p, source) => this.getShortPath(p, source),
702
+ });
703
+ this.chatContainer.addChild(new Text(`${sectionHeader("Extensions", "mdHeading")}\n${extList}`, 0, 0));
704
+ this.chatContainer.addChild(new Spacer(1));
705
+ }
706
+ const extensionDiagnostics = [];
707
+ const extensionErrors = this.session.resourceLoader.getExtensions().errors;
708
+ if (extensionErrors.length > 0) {
709
+ for (const error of extensionErrors) {
710
+ extensionDiagnostics.push({ type: "error", message: error.error, path: error.path });
711
+ }
712
+ }
713
+ const commandDiagnostics = this.session.extensionRunner?.getCommandDiagnostics() ?? [];
714
+ extensionDiagnostics.push(...commandDiagnostics);
715
+ const shortcutDiagnostics = this.session.extensionRunner?.getShortcutDiagnostics() ?? [];
716
+ extensionDiagnostics.push(...shortcutDiagnostics);
717
+ if (extensionDiagnostics.length > 0) {
718
+ const warningLines = this.formatDiagnostics(extensionDiagnostics, metadata);
719
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Extension issues]")}\n${warningLines}`, 0, 0));
720
+ this.chatContainer.addChild(new Spacer(1));
721
+ }
722
+ // Show loaded themes (excluding built-in)
723
+ const loadedThemes = this.session.resourceLoader.getThemes().themes;
724
+ const customThemes = loadedThemes.filter((t) => t.sourcePath);
725
+ if (customThemes.length > 0) {
726
+ const themePaths = customThemes.map((t) => t.sourcePath);
727
+ const groups = this.buildScopeGroups(themePaths, metadata);
728
+ const themeList = this.formatScopeGroups(groups, {
729
+ formatPath: (p) => this.formatDisplayPath(p),
730
+ formatPackagePath: (p, source) => this.getShortPath(p, source),
731
+ });
732
+ this.chatContainer.addChild(new Text(`${sectionHeader("Themes")}\n${themeList}`, 0, 0));
733
+ this.chatContainer.addChild(new Spacer(1));
734
+ }
735
+ const themeDiagnostics = this.session.resourceLoader.getThemes().diagnostics;
736
+ if (themeDiagnostics.length > 0) {
737
+ const warningLines = this.formatDiagnostics(themeDiagnostics, metadata);
738
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Theme conflicts]")}\n${warningLines}`, 0, 0));
739
+ this.chatContainer.addChild(new Spacer(1));
740
+ }
741
+ }
742
+ /**
743
+ * Initialize the extension system with TUI-based UI context.
744
+ */
745
+ async initExtensions() {
746
+ const uiContext = this.createExtensionUIContext();
747
+ await this.session.bindExtensions({
748
+ uiContext,
749
+ commandContextActions: {
750
+ waitForIdle: () => this.session.agent.waitForIdle(),
751
+ newSession: async (options) => {
752
+ if (this.loadingAnimation) {
753
+ this.loadingAnimation.stop();
754
+ this.loadingAnimation = undefined;
755
+ }
756
+ this.statusContainer.clear();
757
+ // Delegate to AgentSession (handles setup + agent state sync)
758
+ const success = await this.session.newSession(options);
759
+ if (!success) {
760
+ return { cancelled: true };
761
+ }
762
+ // Clear UI state
763
+ this.chatContainer.clear();
764
+ this.pendingMessagesContainer.clear();
765
+ this.compactionQueuedMessages = [];
766
+ this.streamingComponent = undefined;
767
+ this.streamingMessage = undefined;
768
+ this.pendingTools.clear();
769
+ // Render any messages added via setup, or show empty session
770
+ this.renderInitialMessages();
771
+ this.ui.requestRender();
772
+ return { cancelled: false };
773
+ },
774
+ fork: async (entryId) => {
775
+ const result = await this.session.fork(entryId);
776
+ if (result.cancelled) {
777
+ return { cancelled: true };
778
+ }
779
+ this.chatContainer.clear();
780
+ this.renderInitialMessages();
781
+ this.editor.setText(result.selectedText);
782
+ this.showStatus("Forked to new session");
783
+ return { cancelled: false };
784
+ },
785
+ navigateTree: async (targetId, options) => {
786
+ const result = await this.session.navigateTree(targetId, {
787
+ summarize: options?.summarize,
788
+ customInstructions: options?.customInstructions,
789
+ replaceInstructions: options?.replaceInstructions,
790
+ label: options?.label,
791
+ });
792
+ if (result.cancelled) {
793
+ return { cancelled: true };
794
+ }
795
+ this.chatContainer.clear();
796
+ this.renderInitialMessages();
797
+ if (result.editorText && !this.editor.getText().trim()) {
798
+ this.editor.setText(result.editorText);
799
+ }
800
+ this.showStatus("Navigated to selected point");
801
+ return { cancelled: false };
802
+ },
803
+ switchSession: async (sessionPath) => {
804
+ await this.handleResumeSession(sessionPath);
805
+ return { cancelled: false };
806
+ },
807
+ },
808
+ shutdownHandler: () => {
809
+ this.shutdownRequested = true;
810
+ },
811
+ onError: (error) => {
812
+ this.showExtensionError(error.extensionPath, error.error, error.stack);
813
+ },
814
+ });
815
+ setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
816
+ this.rebuildAutocomplete();
817
+ const extensionRunner = this.session.extensionRunner;
818
+ if (!extensionRunner) {
819
+ this.showLoadedResources({ extensionPaths: [], force: false });
820
+ return;
821
+ }
822
+ this.setupExtensionShortcuts(extensionRunner);
823
+ this.showLoadedResources({ extensionPaths: extensionRunner.getExtensionPaths(), force: false });
824
+ }
825
+ /**
826
+ * Get a registered tool definition by name (for custom rendering).
827
+ */
828
+ getRegisteredToolDefinition(toolName) {
829
+ const tools = this.session.extensionRunner?.getAllRegisteredTools() ?? [];
830
+ const registeredTool = tools.find((t) => t.definition.name === toolName);
831
+ return registeredTool?.definition;
832
+ }
833
+ /**
834
+ * Set up keyboard shortcuts registered by extensions.
835
+ */
836
+ setupExtensionShortcuts(extensionRunner) {
837
+ const shortcuts = extensionRunner.getShortcuts(this.keybindings.getEffectiveConfig());
838
+ if (shortcuts.size === 0)
839
+ return;
840
+ // Create a context for shortcut handlers
841
+ const createContext = () => ({
842
+ ui: this.createExtensionUIContext(),
843
+ hasUI: true,
844
+ cwd: process.cwd(),
845
+ sessionManager: this.sessionManager,
846
+ modelRegistry: this.session.modelRegistry,
847
+ model: this.session.model,
848
+ isIdle: () => !this.session.isStreaming,
849
+ abort: () => this.session.abort(),
850
+ hasPendingMessages: () => this.session.pendingMessageCount > 0,
851
+ shutdown: () => {
852
+ this.shutdownRequested = true;
853
+ },
854
+ getContextUsage: () => this.session.getContextUsage(),
855
+ compact: (options) => {
856
+ void (async () => {
857
+ try {
858
+ const result = await this.executeCompaction(options?.customInstructions, false);
859
+ if (result) {
860
+ options?.onComplete?.(result);
861
+ }
862
+ }
863
+ catch (error) {
864
+ const err = error instanceof Error ? error : new Error(String(error));
865
+ options?.onError?.(err);
866
+ }
867
+ })();
868
+ },
869
+ getSystemPrompt: () => this.session.systemPrompt,
870
+ });
871
+ // Set up the extension shortcut handler on the default editor
872
+ this.defaultEditor.onExtensionShortcut = (data) => {
873
+ for (const [shortcutStr, shortcut] of shortcuts) {
874
+ // Cast to KeyId - extension shortcuts use the same format
875
+ if (matchesKey(data, shortcutStr)) {
876
+ // Run handler async, don't block input
877
+ Promise.resolve(shortcut.handler(createContext())).catch((err) => {
878
+ this.showError(`Shortcut handler error: ${err instanceof Error ? err.message : String(err)}`);
879
+ });
880
+ return true;
881
+ }
882
+ }
883
+ return false;
884
+ };
885
+ }
886
+ /**
887
+ * Set extension status text in the footer.
888
+ */
889
+ setExtensionStatus(key, text) {
890
+ this.footerDataProvider.setExtensionStatus(key, text);
891
+ this.ui.requestRender();
892
+ }
893
+ /**
894
+ * Set an extension widget (string array or custom component).
895
+ */
896
+ setExtensionWidget(key, content, options) {
897
+ const placement = options?.placement ?? "aboveEditor";
898
+ const removeExisting = (map) => {
899
+ const existing = map.get(key);
900
+ if (existing?.dispose)
901
+ existing.dispose();
902
+ map.delete(key);
903
+ };
904
+ removeExisting(this.extensionWidgetsAbove);
905
+ removeExisting(this.extensionWidgetsBelow);
906
+ if (content === undefined) {
907
+ this.renderWidgets();
908
+ return;
909
+ }
910
+ let component;
911
+ if (Array.isArray(content)) {
912
+ // Wrap string array in a Container with Text components
913
+ const container = new Container();
914
+ for (const line of content.slice(0, InteractiveMode.MAX_WIDGET_LINES)) {
915
+ container.addChild(new Text(line, 1, 0));
916
+ }
917
+ if (content.length > InteractiveMode.MAX_WIDGET_LINES) {
918
+ container.addChild(new Text(theme.fg("muted", "... (widget truncated)"), 1, 0));
919
+ }
920
+ component = container;
921
+ }
922
+ else {
923
+ // Factory function - create component
924
+ component = content(this.ui, theme);
925
+ }
926
+ const targetMap = placement === "belowEditor" ? this.extensionWidgetsBelow : this.extensionWidgetsAbove;
927
+ targetMap.set(key, component);
928
+ this.renderWidgets();
929
+ }
930
+ clearExtensionWidgets() {
931
+ for (const widget of this.extensionWidgetsAbove.values()) {
932
+ widget.dispose?.();
933
+ }
934
+ for (const widget of this.extensionWidgetsBelow.values()) {
935
+ widget.dispose?.();
936
+ }
937
+ this.extensionWidgetsAbove.clear();
938
+ this.extensionWidgetsBelow.clear();
939
+ this.renderWidgets();
940
+ }
941
+ resetExtensionUI() {
942
+ if (this.extensionSelector) {
943
+ this.hideExtensionSelector();
944
+ }
945
+ if (this.extensionInput) {
946
+ this.hideExtensionInput();
947
+ }
948
+ if (this.extensionEditor) {
949
+ this.hideExtensionEditor();
950
+ }
951
+ this.ui.hideOverlay();
952
+ this.setExtensionFooter(undefined);
953
+ this.setExtensionHeader(undefined);
954
+ this.clearExtensionWidgets();
955
+ this.footerDataProvider.clearExtensionStatuses();
956
+ this.footer.invalidate();
957
+ this.setCustomEditorComponent(undefined);
958
+ this.defaultEditor.onExtensionShortcut = undefined;
959
+ this.updateTerminalTitle();
960
+ if (this.loadingAnimation) {
961
+ this.loadingAnimation.setMessage(`${this.defaultWorkingMessage} (${appKey(this.keybindings, "interrupt")} to interrupt)`);
962
+ }
963
+ }
964
+ // Maximum total widget lines to prevent viewport overflow
965
+ static MAX_WIDGET_LINES = 10;
966
+ /**
967
+ * Render all extension widgets to the widget container.
968
+ */
969
+ renderWidgets() {
970
+ if (!this.widgetContainerAbove || !this.widgetContainerBelow)
971
+ return;
972
+ this.renderWidgetContainer(this.widgetContainerAbove, this.extensionWidgetsAbove, true, true);
973
+ this.renderWidgetContainer(this.widgetContainerBelow, this.extensionWidgetsBelow, false, false);
974
+ this.ui.requestRender();
975
+ }
976
+ renderWidgetContainer(container, widgets, spacerWhenEmpty, leadingSpacer) {
977
+ container.clear();
978
+ if (widgets.size === 0) {
979
+ if (spacerWhenEmpty) {
980
+ container.addChild(new Spacer(1));
981
+ }
982
+ return;
983
+ }
984
+ if (leadingSpacer) {
985
+ container.addChild(new Spacer(1));
986
+ }
987
+ for (const component of widgets.values()) {
988
+ container.addChild(component);
989
+ }
990
+ }
991
+ /**
992
+ * Set a custom footer component, or restore the built-in footer.
993
+ */
994
+ setExtensionFooter(factory) {
995
+ // Dispose existing custom footer
996
+ if (this.customFooter?.dispose) {
997
+ this.customFooter.dispose();
998
+ }
999
+ // Remove current footer from UI
1000
+ if (this.customFooter) {
1001
+ this.ui.removeChild(this.customFooter);
1002
+ }
1003
+ else {
1004
+ this.ui.removeChild(this.footer);
1005
+ }
1006
+ if (factory) {
1007
+ // Create and add custom footer, passing the data provider
1008
+ this.customFooter = factory(this.ui, theme, this.footerDataProvider);
1009
+ this.ui.addChild(this.customFooter);
1010
+ }
1011
+ else {
1012
+ // Restore built-in footer
1013
+ this.customFooter = undefined;
1014
+ this.ui.addChild(this.footer);
1015
+ }
1016
+ this.ui.requestRender();
1017
+ }
1018
+ /**
1019
+ * Set a custom header component, or restore the built-in header.
1020
+ */
1021
+ setExtensionHeader(factory) {
1022
+ // Header may not be initialized yet if called during early initialization
1023
+ if (!this.builtInHeader) {
1024
+ return;
1025
+ }
1026
+ // Dispose existing custom header
1027
+ if (this.customHeader?.dispose) {
1028
+ this.customHeader.dispose();
1029
+ }
1030
+ // Find the index of the current header in the header container
1031
+ const currentHeader = this.customHeader || this.builtInHeader;
1032
+ const index = this.headerContainer.children.indexOf(currentHeader);
1033
+ if (factory) {
1034
+ // Create and add custom header
1035
+ this.customHeader = factory(this.ui, theme);
1036
+ if (index !== -1) {
1037
+ this.headerContainer.children[index] = this.customHeader;
1038
+ }
1039
+ else {
1040
+ // If not found (e.g. builtInHeader was never added), add at the top
1041
+ this.headerContainer.children.unshift(this.customHeader);
1042
+ }
1043
+ }
1044
+ else {
1045
+ // Restore built-in header
1046
+ this.customHeader = undefined;
1047
+ if (index !== -1) {
1048
+ this.headerContainer.children[index] = this.builtInHeader;
1049
+ }
1050
+ }
1051
+ this.ui.requestRender();
1052
+ }
1053
+ /**
1054
+ * Create the ExtensionUIContext for extensions.
1055
+ */
1056
+ createExtensionUIContext() {
1057
+ return {
1058
+ select: (title, options, opts) => this.showExtensionSelector(title, options, opts),
1059
+ confirm: (title, message, opts) => this.showExtensionConfirm(title, message, opts),
1060
+ input: (title, placeholder, opts) => this.showExtensionInput(title, placeholder, opts),
1061
+ notify: (message, type) => this.showExtensionNotify(message, type),
1062
+ setStatus: (key, text) => this.setExtensionStatus(key, text),
1063
+ setWorkingMessage: (message) => {
1064
+ if (this.loadingAnimation) {
1065
+ if (message) {
1066
+ this.loadingAnimation.setMessage(message);
1067
+ }
1068
+ else {
1069
+ this.loadingAnimation.setMessage(`${this.defaultWorkingMessage} (${appKey(this.keybindings, "interrupt")} to interrupt)`);
1070
+ }
1071
+ }
1072
+ else {
1073
+ // Queue message for when loadingAnimation is created (handles agent_start race)
1074
+ this.pendingWorkingMessage = message;
1075
+ }
1076
+ },
1077
+ setWidget: (key, content, options) => this.setExtensionWidget(key, content, options),
1078
+ setFooter: (factory) => this.setExtensionFooter(factory),
1079
+ setHeader: (factory) => this.setExtensionHeader(factory),
1080
+ setTitle: (title) => this.ui.terminal.setTitle(title),
1081
+ custom: (factory, options) => this.showExtensionCustom(factory, options),
1082
+ setEditorText: (text) => this.editor.setText(text),
1083
+ getEditorText: () => this.editor.getText(),
1084
+ editor: (title, prefill) => this.showExtensionEditor(title, prefill),
1085
+ setEditorComponent: (factory) => this.setCustomEditorComponent(factory),
1086
+ get theme() {
1087
+ return theme;
1088
+ },
1089
+ getAllThemes: () => getAvailableThemesWithPaths(),
1090
+ getTheme: (name) => getThemeByName(name),
1091
+ setTheme: (themeOrName) => {
1092
+ if (themeOrName instanceof Theme) {
1093
+ setThemeInstance(themeOrName);
1094
+ this.ui.requestRender();
1095
+ return { success: true };
1096
+ }
1097
+ const result = setTheme(themeOrName, true);
1098
+ if (result.success) {
1099
+ this.ui.requestRender();
1100
+ }
1101
+ return result;
1102
+ },
1103
+ getToolsExpanded: () => this.toolOutputExpanded,
1104
+ setToolsExpanded: (expanded) => this.setToolsExpanded(expanded),
1105
+ };
1106
+ }
1107
+ /**
1108
+ * Show a selector for extensions.
1109
+ */
1110
+ showExtensionSelector(title, options, opts) {
1111
+ return new Promise((resolve) => {
1112
+ if (opts?.signal?.aborted) {
1113
+ resolve(undefined);
1114
+ return;
1115
+ }
1116
+ const onAbort = () => {
1117
+ this.hideExtensionSelector();
1118
+ resolve(undefined);
1119
+ };
1120
+ opts?.signal?.addEventListener("abort", onAbort, { once: true });
1121
+ this.extensionSelector = new ExtensionSelectorComponent(title, options, (option) => {
1122
+ opts?.signal?.removeEventListener("abort", onAbort);
1123
+ this.hideExtensionSelector();
1124
+ resolve(option);
1125
+ }, () => {
1126
+ opts?.signal?.removeEventListener("abort", onAbort);
1127
+ this.hideExtensionSelector();
1128
+ resolve(undefined);
1129
+ }, { tui: this.ui, timeout: opts?.timeout });
1130
+ this.editorContainer.clear();
1131
+ this.editorContainer.addChild(this.extensionSelector);
1132
+ this.ui.setFocus(this.extensionSelector);
1133
+ this.ui.requestRender();
1134
+ });
1135
+ }
1136
+ /**
1137
+ * Hide the extension selector.
1138
+ */
1139
+ hideExtensionSelector() {
1140
+ this.extensionSelector?.dispose();
1141
+ this.editorContainer.clear();
1142
+ this.editorContainer.addChild(this.editor);
1143
+ this.extensionSelector = undefined;
1144
+ this.ui.setFocus(this.editor);
1145
+ this.ui.requestRender();
1146
+ }
1147
+ /**
1148
+ * Show a confirmation dialog for extensions.
1149
+ */
1150
+ async showExtensionConfirm(title, message, opts) {
1151
+ const result = await this.showExtensionSelector(`${title}\n${message}`, ["Yes", "No"], opts);
1152
+ return result === "Yes";
1153
+ }
1154
+ /**
1155
+ * Show a text input for extensions.
1156
+ */
1157
+ showExtensionInput(title, placeholder, opts) {
1158
+ return new Promise((resolve) => {
1159
+ if (opts?.signal?.aborted) {
1160
+ resolve(undefined);
1161
+ return;
1162
+ }
1163
+ const onAbort = () => {
1164
+ this.hideExtensionInput();
1165
+ resolve(undefined);
1166
+ };
1167
+ opts?.signal?.addEventListener("abort", onAbort, { once: true });
1168
+ this.extensionInput = new ExtensionInputComponent(title, placeholder, (value) => {
1169
+ opts?.signal?.removeEventListener("abort", onAbort);
1170
+ this.hideExtensionInput();
1171
+ resolve(value);
1172
+ }, () => {
1173
+ opts?.signal?.removeEventListener("abort", onAbort);
1174
+ this.hideExtensionInput();
1175
+ resolve(undefined);
1176
+ }, { tui: this.ui, timeout: opts?.timeout });
1177
+ this.editorContainer.clear();
1178
+ this.editorContainer.addChild(this.extensionInput);
1179
+ this.ui.setFocus(this.extensionInput);
1180
+ this.ui.requestRender();
1181
+ });
1182
+ }
1183
+ /**
1184
+ * Hide the extension input.
1185
+ */
1186
+ hideExtensionInput() {
1187
+ this.extensionInput?.dispose();
1188
+ this.editorContainer.clear();
1189
+ this.editorContainer.addChild(this.editor);
1190
+ this.extensionInput = undefined;
1191
+ this.ui.setFocus(this.editor);
1192
+ this.ui.requestRender();
1193
+ }
1194
+ /**
1195
+ * Show a multi-line editor for extensions (with Ctrl+G support).
1196
+ */
1197
+ showExtensionEditor(title, prefill) {
1198
+ return new Promise((resolve) => {
1199
+ this.extensionEditor = new ExtensionEditorComponent(this.ui, this.keybindings, title, prefill, (value) => {
1200
+ this.hideExtensionEditor();
1201
+ resolve(value);
1202
+ }, () => {
1203
+ this.hideExtensionEditor();
1204
+ resolve(undefined);
1205
+ });
1206
+ this.editorContainer.clear();
1207
+ this.editorContainer.addChild(this.extensionEditor);
1208
+ this.ui.setFocus(this.extensionEditor);
1209
+ this.ui.requestRender();
1210
+ });
1211
+ }
1212
+ /**
1213
+ * Hide the extension editor.
1214
+ */
1215
+ hideExtensionEditor() {
1216
+ this.editorContainer.clear();
1217
+ this.editorContainer.addChild(this.editor);
1218
+ this.extensionEditor = undefined;
1219
+ this.ui.setFocus(this.editor);
1220
+ this.ui.requestRender();
1221
+ }
1222
+ /**
1223
+ * Set a custom editor component from an extension.
1224
+ * Pass undefined to restore the default editor.
1225
+ */
1226
+ setCustomEditorComponent(factory) {
1227
+ // Save text from current editor before switching
1228
+ const currentText = this.editor.getText();
1229
+ this.editorContainer.clear();
1230
+ if (factory) {
1231
+ // Create the custom editor with tui, theme, and keybindings
1232
+ const newEditor = factory(this.ui, getEditorTheme(), this.keybindings);
1233
+ // Wire up callbacks from the default editor
1234
+ newEditor.onSubmit = this.defaultEditor.onSubmit;
1235
+ newEditor.onChange = this.defaultEditor.onChange;
1236
+ // Copy text from previous editor
1237
+ newEditor.setText(currentText);
1238
+ // Copy appearance settings if supported
1239
+ if (newEditor.borderColor !== undefined) {
1240
+ newEditor.borderColor = this.defaultEditor.borderColor;
1241
+ }
1242
+ if (newEditor.setPaddingX !== undefined) {
1243
+ newEditor.setPaddingX(this.defaultEditor.getPaddingX());
1244
+ }
1245
+ // Set autocomplete if supported
1246
+ if (newEditor.setAutocompleteProvider && this.autocompleteProvider) {
1247
+ newEditor.setAutocompleteProvider(this.autocompleteProvider);
1248
+ }
1249
+ // If extending CustomEditor, copy app-level handlers
1250
+ // Use duck typing since instanceof fails across jiti module boundaries
1251
+ const customEditor = newEditor;
1252
+ if ("actionHandlers" in customEditor && customEditor.actionHandlers instanceof Map) {
1253
+ customEditor.onEscape = this.defaultEditor.onEscape;
1254
+ customEditor.onCtrlD = this.defaultEditor.onCtrlD;
1255
+ customEditor.onPasteImage = this.defaultEditor.onPasteImage;
1256
+ customEditor.onExtensionShortcut = (data) => this.defaultEditor.onExtensionShortcut?.(data);
1257
+ // Copy action handlers (clear, suspend, model switching, etc.)
1258
+ for (const [action, handler] of this.defaultEditor.actionHandlers) {
1259
+ customEditor.actionHandlers.set(action, handler);
1260
+ }
1261
+ }
1262
+ this.editor = newEditor;
1263
+ }
1264
+ else {
1265
+ // Restore default editor with text from custom editor
1266
+ this.defaultEditor.setText(currentText);
1267
+ this.editor = this.defaultEditor;
1268
+ }
1269
+ this.editorContainer.addChild(this.editor);
1270
+ this.ui.setFocus(this.editor);
1271
+ this.ui.requestRender();
1272
+ }
1273
+ /**
1274
+ * Show a notification for extensions.
1275
+ */
1276
+ showExtensionNotify(message, type) {
1277
+ if (type === "error") {
1278
+ this.showError(message);
1279
+ }
1280
+ else if (type === "warning") {
1281
+ this.showWarning(message);
1282
+ }
1283
+ else {
1284
+ this.showStatus(message);
1285
+ }
1286
+ }
1287
+ /** Show a custom component with keyboard focus. Overlay mode renders on top of existing content. */
1288
+ async showExtensionCustom(factory, options) {
1289
+ const savedText = this.editor.getText();
1290
+ const isOverlay = options?.overlay ?? false;
1291
+ const restoreEditor = () => {
1292
+ this.editorContainer.clear();
1293
+ this.editorContainer.addChild(this.editor);
1294
+ this.editor.setText(savedText);
1295
+ this.ui.setFocus(this.editor);
1296
+ this.ui.requestRender();
1297
+ };
1298
+ return new Promise((resolve, reject) => {
1299
+ let component;
1300
+ let closed = false;
1301
+ const close = (result) => {
1302
+ if (closed)
1303
+ return;
1304
+ closed = true;
1305
+ if (isOverlay)
1306
+ this.ui.hideOverlay();
1307
+ else
1308
+ restoreEditor();
1309
+ // Note: both branches above already call requestRender
1310
+ resolve(result);
1311
+ try {
1312
+ component?.dispose?.();
1313
+ }
1314
+ catch {
1315
+ /* ignore dispose errors */
1316
+ }
1317
+ };
1318
+ Promise.resolve(factory(this.ui, theme, this.keybindings, close))
1319
+ .then((c) => {
1320
+ if (closed)
1321
+ return;
1322
+ component = c;
1323
+ if (isOverlay) {
1324
+ // Resolve overlay options - can be static or dynamic function
1325
+ const resolveOptions = () => {
1326
+ if (options?.overlayOptions) {
1327
+ const opts = typeof options.overlayOptions === "function"
1328
+ ? options.overlayOptions()
1329
+ : options.overlayOptions;
1330
+ return opts;
1331
+ }
1332
+ // Fallback: use component's width property if available
1333
+ const w = component.width;
1334
+ return w ? { width: w } : undefined;
1335
+ };
1336
+ const handle = this.ui.showOverlay(component, resolveOptions());
1337
+ // Expose handle to caller for visibility control
1338
+ options?.onHandle?.(handle);
1339
+ }
1340
+ else {
1341
+ this.editorContainer.clear();
1342
+ this.editorContainer.addChild(component);
1343
+ this.ui.setFocus(component);
1344
+ this.ui.requestRender();
1345
+ }
1346
+ })
1347
+ .catch((err) => {
1348
+ if (closed)
1349
+ return;
1350
+ if (!isOverlay)
1351
+ restoreEditor();
1352
+ reject(err);
1353
+ });
1354
+ });
1355
+ }
1356
+ /**
1357
+ * Show an extension error in the UI.
1358
+ */
1359
+ showExtensionError(extensionPath, error, stack) {
1360
+ const errorMsg = `Extension "${extensionPath}" error: ${error}`;
1361
+ const errorText = new Text(theme.fg("error", errorMsg), 1, 0);
1362
+ this.chatContainer.addChild(errorText);
1363
+ if (stack) {
1364
+ // Show stack trace in dim color, indented
1365
+ const stackLines = stack
1366
+ .split("\n")
1367
+ .slice(1) // Skip first line (duplicates error message)
1368
+ .map((line) => theme.fg("dim", ` ${line.trim()}`))
1369
+ .join("\n");
1370
+ if (stackLines) {
1371
+ this.chatContainer.addChild(new Text(stackLines, 1, 0));
1372
+ }
1373
+ }
1374
+ this.ui.requestRender();
1375
+ }
1376
+ // =========================================================================
1377
+ // Key Handlers
1378
+ // =========================================================================
1379
+ setupKeyHandlers() {
1380
+ // Set up handlers on defaultEditor - they use this.editor for text access
1381
+ // so they work correctly regardless of which editor is active
1382
+ this.defaultEditor.onEscape = () => {
1383
+ if (this.loadingAnimation) {
1384
+ this.restoreQueuedMessagesToEditor({ abort: true });
1385
+ }
1386
+ else if (this.session.isBashRunning) {
1387
+ this.session.abortBash();
1388
+ }
1389
+ else if (this.isBashMode) {
1390
+ this.editor.setText("");
1391
+ this.isBashMode = false;
1392
+ this.updateEditorBorderColor();
1393
+ }
1394
+ else if (!this.editor.getText().trim()) {
1395
+ // Double-escape with empty editor triggers /tree, /fork, or nothing based on setting
1396
+ const action = this.settingsManager.getDoubleEscapeAction();
1397
+ if (action !== "none") {
1398
+ const now = Date.now();
1399
+ if (now - this.lastEscapeTime < 500) {
1400
+ if (action === "tree") {
1401
+ this.showTreeSelector();
1402
+ }
1403
+ else {
1404
+ this.showUserMessageSelector();
1405
+ }
1406
+ this.lastEscapeTime = 0;
1407
+ }
1408
+ else {
1409
+ this.lastEscapeTime = now;
1410
+ }
1411
+ }
1412
+ }
1413
+ };
1414
+ // Register app action handlers
1415
+ this.defaultEditor.onAction("clear", () => this.handleCtrlC());
1416
+ this.defaultEditor.onCtrlD = () => this.handleCtrlD();
1417
+ this.defaultEditor.onAction("suspend", () => this.handleCtrlZ());
1418
+ this.defaultEditor.onAction("cycleThinkingLevel", () => this.cycleThinkingLevel());
1419
+ this.defaultEditor.onAction("cycleModelForward", () => this.cycleModel("forward"));
1420
+ this.defaultEditor.onAction("cycleModelBackward", () => this.cycleModel("backward"));
1421
+ // Global debug handler on TUI (works regardless of focus)
1422
+ this.ui.onDebug = () => this.handleDebugCommand();
1423
+ this.defaultEditor.onAction("selectModel", () => this.showModelSelector());
1424
+ this.defaultEditor.onAction("expandTools", () => this.toggleToolOutputExpansion());
1425
+ this.defaultEditor.onAction("toggleThinking", () => this.toggleThinkingBlockVisibility());
1426
+ this.defaultEditor.onAction("externalEditor", () => this.openExternalEditor());
1427
+ this.defaultEditor.onAction("followUp", () => this.handleFollowUp());
1428
+ this.defaultEditor.onAction("dequeue", () => this.handleDequeue());
1429
+ this.defaultEditor.onAction("newSession", () => this.handleClearCommand());
1430
+ this.defaultEditor.onAction("tree", () => this.showTreeSelector());
1431
+ this.defaultEditor.onAction("fork", () => this.showUserMessageSelector());
1432
+ this.defaultEditor.onAction("resume", () => this.showSessionSelector());
1433
+ this.defaultEditor.onChange = (text) => {
1434
+ const wasBashMode = this.isBashMode;
1435
+ this.isBashMode = text.trimStart().startsWith("!");
1436
+ if (wasBashMode !== this.isBashMode) {
1437
+ this.updateEditorBorderColor();
1438
+ }
1439
+ };
1440
+ // Handle clipboard image paste (triggered on Ctrl+V)
1441
+ this.defaultEditor.onPasteImage = () => {
1442
+ this.handleClipboardImagePaste();
1443
+ };
1444
+ }
1445
+ async handleClipboardImagePaste() {
1446
+ try {
1447
+ const image = await readClipboardImage();
1448
+ if (!image) {
1449
+ return;
1450
+ }
1451
+ // Write to temp file
1452
+ const tmpDir = os.tmpdir();
1453
+ const ext = extensionForImageMimeType(image.mimeType) ?? "png";
1454
+ const fileName = `pi-clipboard-${crypto.randomUUID()}.${ext}`;
1455
+ const filePath = path.join(tmpDir, fileName);
1456
+ fs.writeFileSync(filePath, Buffer.from(image.bytes));
1457
+ // Insert file path directly
1458
+ this.editor.insertTextAtCursor?.(filePath);
1459
+ this.ui.requestRender();
1460
+ }
1461
+ catch {
1462
+ // Silently ignore clipboard errors (may not have permission, etc.)
1463
+ }
1464
+ }
1465
+ setupEditorSubmitHandler() {
1466
+ this.defaultEditor.onSubmit = async (text) => {
1467
+ text = text.trim();
1468
+ if (!text)
1469
+ return;
1470
+ // Handle commands
1471
+ if (text === "/settings") {
1472
+ this.showSettingsSelector();
1473
+ this.editor.setText("");
1474
+ return;
1475
+ }
1476
+ if (text === "/scoped-models") {
1477
+ this.editor.setText("");
1478
+ await this.showModelsSelector();
1479
+ return;
1480
+ }
1481
+ if (text === "/model" || text.startsWith("/model ")) {
1482
+ const searchTerm = text.startsWith("/model ") ? text.slice(7).trim() : undefined;
1483
+ this.editor.setText("");
1484
+ await this.handleModelCommand(searchTerm);
1485
+ return;
1486
+ }
1487
+ if (text.startsWith("/export")) {
1488
+ await this.handleExportCommand(text);
1489
+ this.editor.setText("");
1490
+ return;
1491
+ }
1492
+ if (text === "/share") {
1493
+ await this.handleShareCommand();
1494
+ this.editor.setText("");
1495
+ return;
1496
+ }
1497
+ if (text === "/copy") {
1498
+ this.handleCopyCommand();
1499
+ this.editor.setText("");
1500
+ return;
1501
+ }
1502
+ if (text === "/name" || text.startsWith("/name ")) {
1503
+ this.handleNameCommand(text);
1504
+ this.editor.setText("");
1505
+ return;
1506
+ }
1507
+ if (text === "/session") {
1508
+ this.handleSessionCommand();
1509
+ this.editor.setText("");
1510
+ return;
1511
+ }
1512
+ if (text === "/changelog") {
1513
+ this.handleChangelogCommand();
1514
+ this.editor.setText("");
1515
+ return;
1516
+ }
1517
+ if (text === "/hotkeys") {
1518
+ this.handleHotkeysCommand();
1519
+ this.editor.setText("");
1520
+ return;
1521
+ }
1522
+ if (text === "/fork") {
1523
+ this.showUserMessageSelector();
1524
+ this.editor.setText("");
1525
+ return;
1526
+ }
1527
+ if (text === "/tree") {
1528
+ this.showTreeSelector();
1529
+ this.editor.setText("");
1530
+ return;
1531
+ }
1532
+ if (text === "/login") {
1533
+ this.showOAuthSelector("login");
1534
+ this.editor.setText("");
1535
+ return;
1536
+ }
1537
+ if (text === "/logout") {
1538
+ this.showOAuthSelector("logout");
1539
+ this.editor.setText("");
1540
+ return;
1541
+ }
1542
+ if (text === "/new") {
1543
+ this.editor.setText("");
1544
+ await this.handleClearCommand();
1545
+ return;
1546
+ }
1547
+ if (text === "/compact" || text.startsWith("/compact ")) {
1548
+ const customInstructions = text.startsWith("/compact ") ? text.slice(9).trim() : undefined;
1549
+ this.editor.setText("");
1550
+ await this.handleCompactCommand(customInstructions);
1551
+ return;
1552
+ }
1553
+ if (text === "/reload") {
1554
+ this.editor.setText("");
1555
+ await this.handleReloadCommand();
1556
+ return;
1557
+ }
1558
+ if (text === "/debug") {
1559
+ this.handleDebugCommand();
1560
+ this.editor.setText("");
1561
+ return;
1562
+ }
1563
+ if (text === "/arminsayshi") {
1564
+ this.handleArminSaysHi();
1565
+ this.editor.setText("");
1566
+ return;
1567
+ }
1568
+ if (text === "/resume") {
1569
+ this.showSessionSelector();
1570
+ this.editor.setText("");
1571
+ return;
1572
+ }
1573
+ if (text === "/quit" || text === "/exit") {
1574
+ this.editor.setText("");
1575
+ await this.shutdown();
1576
+ return;
1577
+ }
1578
+ // Handle bash command (! for normal, !! for excluded from context)
1579
+ if (text.startsWith("!")) {
1580
+ const isExcluded = text.startsWith("!!");
1581
+ const command = isExcluded ? text.slice(2).trim() : text.slice(1).trim();
1582
+ if (command) {
1583
+ if (this.session.isBashRunning) {
1584
+ this.showWarning("A bash command is already running. Press Esc to cancel it first.");
1585
+ this.editor.setText(text);
1586
+ return;
1587
+ }
1588
+ this.editor.addToHistory?.(text);
1589
+ await this.handleBashCommand(command, isExcluded);
1590
+ this.isBashMode = false;
1591
+ this.updateEditorBorderColor();
1592
+ return;
1593
+ }
1594
+ }
1595
+ // Queue input during compaction (extension commands execute immediately)
1596
+ if (this.session.isCompacting) {
1597
+ if (this.isExtensionCommand(text)) {
1598
+ this.editor.addToHistory?.(text);
1599
+ this.editor.setText("");
1600
+ await this.session.prompt(text);
1601
+ }
1602
+ else {
1603
+ this.queueCompactionMessage(text, "steer");
1604
+ }
1605
+ return;
1606
+ }
1607
+ // If streaming, use prompt() with steer behavior
1608
+ // This handles extension commands (execute immediately), prompt template expansion, and queueing
1609
+ if (this.session.isStreaming) {
1610
+ this.editor.addToHistory?.(text);
1611
+ this.editor.setText("");
1612
+ await this.session.prompt(text, { streamingBehavior: "steer" });
1613
+ this.updatePendingMessagesDisplay();
1614
+ this.ui.requestRender();
1615
+ return;
1616
+ }
1617
+ // Normal message submission
1618
+ // First, move any pending bash components to chat
1619
+ this.flushPendingBashComponents();
1620
+ if (this.onInputCallback) {
1621
+ this.onInputCallback(text);
1622
+ }
1623
+ this.editor.addToHistory?.(text);
1624
+ };
1625
+ }
1626
+ subscribeToAgent() {
1627
+ this.unsubscribe = this.session.subscribe(async (event) => {
1628
+ await this.handleEvent(event);
1629
+ });
1630
+ }
1631
+ async handleEvent(event) {
1632
+ if (!this.isInitialized) {
1633
+ await this.init();
1634
+ }
1635
+ this.footer.invalidate();
1636
+ switch (event.type) {
1637
+ case "agent_start":
1638
+ // Restore main escape handler if retry handler is still active
1639
+ // (retry success event fires later, but we need main handler now)
1640
+ if (this.retryEscapeHandler) {
1641
+ this.defaultEditor.onEscape = this.retryEscapeHandler;
1642
+ this.retryEscapeHandler = undefined;
1643
+ }
1644
+ if (this.retryLoader) {
1645
+ this.retryLoader.stop();
1646
+ this.retryLoader = undefined;
1647
+ }
1648
+ if (this.loadingAnimation) {
1649
+ this.loadingAnimation.stop();
1650
+ }
1651
+ this.statusContainer.clear();
1652
+ this.loadingAnimation = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), this.defaultWorkingMessage);
1653
+ this.statusContainer.addChild(this.loadingAnimation);
1654
+ // Apply any pending working message queued before loader existed
1655
+ if (this.pendingWorkingMessage !== undefined) {
1656
+ if (this.pendingWorkingMessage) {
1657
+ this.loadingAnimation.setMessage(this.pendingWorkingMessage);
1658
+ }
1659
+ this.pendingWorkingMessage = undefined;
1660
+ }
1661
+ this.ui.requestRender();
1662
+ break;
1663
+ case "message_start":
1664
+ if (event.message.role === "custom") {
1665
+ this.addMessageToChat(event.message);
1666
+ this.ui.requestRender();
1667
+ }
1668
+ else if (event.message.role === "user") {
1669
+ this.addMessageToChat(event.message);
1670
+ this.updatePendingMessagesDisplay();
1671
+ this.ui.requestRender();
1672
+ }
1673
+ else if (event.message.role === "assistant") {
1674
+ this.streamingComponent = new AssistantMessageComponent(undefined, this.hideThinkingBlock, this.getMarkdownThemeWithSettings());
1675
+ this.streamingMessage = event.message;
1676
+ this.chatContainer.addChild(this.streamingComponent);
1677
+ this.streamingComponent.updateContent(this.streamingMessage);
1678
+ this.ui.requestRender();
1679
+ }
1680
+ break;
1681
+ case "message_update":
1682
+ if (this.streamingComponent && event.message.role === "assistant") {
1683
+ this.streamingMessage = event.message;
1684
+ this.streamingComponent.updateContent(this.streamingMessage);
1685
+ for (const content of this.streamingMessage.content) {
1686
+ if (content.type === "toolCall") {
1687
+ if (!this.pendingTools.has(content.id)) {
1688
+ this.chatContainer.addChild(new Text("", 0, 0));
1689
+ const component = new ToolExecutionComponent(content.name, content.arguments, {
1690
+ showImages: this.settingsManager.getShowImages(),
1691
+ }, this.getRegisteredToolDefinition(content.name), this.ui);
1692
+ component.setExpanded(this.toolOutputExpanded);
1693
+ this.chatContainer.addChild(component);
1694
+ this.pendingTools.set(content.id, component);
1695
+ }
1696
+ else {
1697
+ const component = this.pendingTools.get(content.id);
1698
+ if (component) {
1699
+ component.updateArgs(content.arguments);
1700
+ }
1701
+ }
1702
+ }
1703
+ }
1704
+ this.ui.requestRender();
1705
+ }
1706
+ break;
1707
+ case "message_end":
1708
+ if (event.message.role === "user")
1709
+ break;
1710
+ if (this.streamingComponent && event.message.role === "assistant") {
1711
+ this.streamingMessage = event.message;
1712
+ let errorMessage;
1713
+ if (this.streamingMessage.stopReason === "aborted") {
1714
+ const retryAttempt = this.session.retryAttempt;
1715
+ errorMessage =
1716
+ retryAttempt > 0
1717
+ ? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
1718
+ : "Operation aborted";
1719
+ this.streamingMessage.errorMessage = errorMessage;
1720
+ }
1721
+ this.streamingComponent.updateContent(this.streamingMessage);
1722
+ if (this.streamingMessage.stopReason === "aborted" || this.streamingMessage.stopReason === "error") {
1723
+ if (!errorMessage) {
1724
+ errorMessage = this.streamingMessage.errorMessage || "Error";
1725
+ }
1726
+ for (const [, component] of this.pendingTools.entries()) {
1727
+ component.updateResult({
1728
+ content: [{ type: "text", text: errorMessage }],
1729
+ isError: true,
1730
+ });
1731
+ }
1732
+ this.pendingTools.clear();
1733
+ }
1734
+ else {
1735
+ // Args are now complete - trigger diff computation for edit tools
1736
+ for (const [, component] of this.pendingTools.entries()) {
1737
+ component.setArgsComplete();
1738
+ }
1739
+ }
1740
+ this.streamingComponent = undefined;
1741
+ this.streamingMessage = undefined;
1742
+ this.footer.invalidate();
1743
+ }
1744
+ this.ui.requestRender();
1745
+ break;
1746
+ case "tool_execution_start": {
1747
+ if (!this.pendingTools.has(event.toolCallId)) {
1748
+ const component = new ToolExecutionComponent(event.toolName, event.args, {
1749
+ showImages: this.settingsManager.getShowImages(),
1750
+ }, this.getRegisteredToolDefinition(event.toolName), this.ui);
1751
+ component.setExpanded(this.toolOutputExpanded);
1752
+ this.chatContainer.addChild(component);
1753
+ this.pendingTools.set(event.toolCallId, component);
1754
+ this.ui.requestRender();
1755
+ }
1756
+ break;
1757
+ }
1758
+ case "tool_execution_update": {
1759
+ const component = this.pendingTools.get(event.toolCallId);
1760
+ if (component) {
1761
+ component.updateResult({ ...event.partialResult, isError: false }, true);
1762
+ this.ui.requestRender();
1763
+ }
1764
+ break;
1765
+ }
1766
+ case "tool_execution_end": {
1767
+ const component = this.pendingTools.get(event.toolCallId);
1768
+ if (component) {
1769
+ component.updateResult({ ...event.result, isError: event.isError });
1770
+ this.pendingTools.delete(event.toolCallId);
1771
+ this.ui.requestRender();
1772
+ }
1773
+ break;
1774
+ }
1775
+ case "agent_end":
1776
+ if (this.loadingAnimation) {
1777
+ this.loadingAnimation.stop();
1778
+ this.loadingAnimation = undefined;
1779
+ this.statusContainer.clear();
1780
+ }
1781
+ if (this.streamingComponent) {
1782
+ this.chatContainer.removeChild(this.streamingComponent);
1783
+ this.streamingComponent = undefined;
1784
+ this.streamingMessage = undefined;
1785
+ }
1786
+ this.pendingTools.clear();
1787
+ await this.checkShutdownRequested();
1788
+ this.ui.requestRender();
1789
+ break;
1790
+ case "auto_compaction_start": {
1791
+ // Keep editor active; submissions are queued during compaction.
1792
+ // Set up escape to abort auto-compaction
1793
+ this.autoCompactionEscapeHandler = this.defaultEditor.onEscape;
1794
+ this.defaultEditor.onEscape = () => {
1795
+ this.session.abortCompaction();
1796
+ };
1797
+ // Show compacting indicator with reason
1798
+ this.statusContainer.clear();
1799
+ const reasonText = event.reason === "overflow" ? "Context overflow detected, " : "";
1800
+ this.autoCompactionLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), `${reasonText}Auto-compacting... (${appKey(this.keybindings, "interrupt")} to cancel)`);
1801
+ this.statusContainer.addChild(this.autoCompactionLoader);
1802
+ this.ui.requestRender();
1803
+ break;
1804
+ }
1805
+ case "auto_compaction_end": {
1806
+ // Restore escape handler
1807
+ if (this.autoCompactionEscapeHandler) {
1808
+ this.defaultEditor.onEscape = this.autoCompactionEscapeHandler;
1809
+ this.autoCompactionEscapeHandler = undefined;
1810
+ }
1811
+ // Stop loader
1812
+ if (this.autoCompactionLoader) {
1813
+ this.autoCompactionLoader.stop();
1814
+ this.autoCompactionLoader = undefined;
1815
+ this.statusContainer.clear();
1816
+ }
1817
+ // Handle result
1818
+ if (event.aborted) {
1819
+ this.showStatus("Auto-compaction cancelled");
1820
+ }
1821
+ else if (event.result) {
1822
+ // Rebuild chat to show compacted state
1823
+ this.chatContainer.clear();
1824
+ this.rebuildChatFromMessages();
1825
+ // Add compaction component at bottom so user sees it without scrolling
1826
+ this.addMessageToChat({
1827
+ role: "compactionSummary",
1828
+ tokensBefore: event.result.tokensBefore,
1829
+ summary: event.result.summary,
1830
+ timestamp: Date.now(),
1831
+ });
1832
+ this.footer.invalidate();
1833
+ }
1834
+ else if (event.errorMessage) {
1835
+ // Compaction failed (e.g., quota exceeded, API error)
1836
+ this.chatContainer.addChild(new Spacer(1));
1837
+ this.chatContainer.addChild(new Text(theme.fg("error", event.errorMessage), 1, 0));
1838
+ }
1839
+ void this.flushCompactionQueue({ willRetry: event.willRetry });
1840
+ this.ui.requestRender();
1841
+ break;
1842
+ }
1843
+ case "auto_retry_start": {
1844
+ // Set up escape to abort retry
1845
+ this.retryEscapeHandler = this.defaultEditor.onEscape;
1846
+ this.defaultEditor.onEscape = () => {
1847
+ this.session.abortRetry();
1848
+ };
1849
+ // Show retry indicator
1850
+ this.statusContainer.clear();
1851
+ const delaySeconds = Math.round(event.delayMs / 1000);
1852
+ this.retryLoader = new Loader(this.ui, (spinner) => theme.fg("warning", spinner), (text) => theme.fg("muted", text), `Retrying (${event.attempt}/${event.maxAttempts}) in ${delaySeconds}s... (${appKey(this.keybindings, "interrupt")} to cancel)`);
1853
+ this.statusContainer.addChild(this.retryLoader);
1854
+ this.ui.requestRender();
1855
+ break;
1856
+ }
1857
+ case "auto_retry_end": {
1858
+ // Restore escape handler
1859
+ if (this.retryEscapeHandler) {
1860
+ this.defaultEditor.onEscape = this.retryEscapeHandler;
1861
+ this.retryEscapeHandler = undefined;
1862
+ }
1863
+ // Stop loader
1864
+ if (this.retryLoader) {
1865
+ this.retryLoader.stop();
1866
+ this.retryLoader = undefined;
1867
+ this.statusContainer.clear();
1868
+ }
1869
+ // Show error only on final failure (success shows normal response)
1870
+ if (!event.success) {
1871
+ this.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || "Unknown error"}`);
1872
+ }
1873
+ this.ui.requestRender();
1874
+ break;
1875
+ }
1876
+ }
1877
+ }
1878
+ /** Extract text content from a user message */
1879
+ getUserMessageText(message) {
1880
+ if (message.role !== "user")
1881
+ return "";
1882
+ const textBlocks = typeof message.content === "string"
1883
+ ? [{ type: "text", text: message.content }]
1884
+ : message.content.filter((c) => c.type === "text");
1885
+ return textBlocks.map((c) => c.text).join("");
1886
+ }
1887
+ /**
1888
+ * Show a status message in the chat.
1889
+ *
1890
+ * If multiple status messages are emitted back-to-back (without anything else being added to the chat),
1891
+ * we update the previous status line instead of appending new ones to avoid log spam.
1892
+ */
1893
+ showStatus(message) {
1894
+ const children = this.chatContainer.children;
1895
+ const last = children.length > 0 ? children[children.length - 1] : undefined;
1896
+ const secondLast = children.length > 1 ? children[children.length - 2] : undefined;
1897
+ if (last && secondLast && last === this.lastStatusText && secondLast === this.lastStatusSpacer) {
1898
+ this.lastStatusText.setText(theme.fg("dim", message));
1899
+ this.ui.requestRender();
1900
+ return;
1901
+ }
1902
+ const spacer = new Spacer(1);
1903
+ const text = new Text(theme.fg("dim", message), 1, 0);
1904
+ this.chatContainer.addChild(spacer);
1905
+ this.chatContainer.addChild(text);
1906
+ this.lastStatusSpacer = spacer;
1907
+ this.lastStatusText = text;
1908
+ this.ui.requestRender();
1909
+ }
1910
+ addMessageToChat(message, options) {
1911
+ switch (message.role) {
1912
+ case "bashExecution": {
1913
+ const component = new BashExecutionComponent(message.command, this.ui, message.excludeFromContext);
1914
+ if (message.output) {
1915
+ component.appendOutput(message.output);
1916
+ }
1917
+ component.setComplete(message.exitCode, message.cancelled, message.truncated ? { truncated: true } : undefined, message.fullOutputPath);
1918
+ this.chatContainer.addChild(component);
1919
+ break;
1920
+ }
1921
+ case "custom": {
1922
+ if (message.display) {
1923
+ const renderer = this.session.extensionRunner?.getMessageRenderer(message.customType);
1924
+ const component = new CustomMessageComponent(message, renderer, this.getMarkdownThemeWithSettings());
1925
+ component.setExpanded(this.toolOutputExpanded);
1926
+ this.chatContainer.addChild(component);
1927
+ }
1928
+ break;
1929
+ }
1930
+ case "compactionSummary": {
1931
+ this.chatContainer.addChild(new Spacer(1));
1932
+ const component = new CompactionSummaryMessageComponent(message, this.getMarkdownThemeWithSettings());
1933
+ component.setExpanded(this.toolOutputExpanded);
1934
+ this.chatContainer.addChild(component);
1935
+ break;
1936
+ }
1937
+ case "branchSummary": {
1938
+ this.chatContainer.addChild(new Spacer(1));
1939
+ const component = new BranchSummaryMessageComponent(message, this.getMarkdownThemeWithSettings());
1940
+ component.setExpanded(this.toolOutputExpanded);
1941
+ this.chatContainer.addChild(component);
1942
+ break;
1943
+ }
1944
+ case "user": {
1945
+ const textContent = this.getUserMessageText(message);
1946
+ if (textContent) {
1947
+ const skillBlock = parseSkillBlock(textContent);
1948
+ if (skillBlock) {
1949
+ // Render skill block (collapsible)
1950
+ this.chatContainer.addChild(new Spacer(1));
1951
+ const component = new SkillInvocationMessageComponent(skillBlock, this.getMarkdownThemeWithSettings());
1952
+ component.setExpanded(this.toolOutputExpanded);
1953
+ this.chatContainer.addChild(component);
1954
+ // Render user message separately if present
1955
+ if (skillBlock.userMessage) {
1956
+ const userComponent = new UserMessageComponent(skillBlock.userMessage, this.getMarkdownThemeWithSettings());
1957
+ this.chatContainer.addChild(userComponent);
1958
+ }
1959
+ }
1960
+ else {
1961
+ const userComponent = new UserMessageComponent(textContent, this.getMarkdownThemeWithSettings());
1962
+ this.chatContainer.addChild(userComponent);
1963
+ }
1964
+ if (options?.populateHistory) {
1965
+ this.editor.addToHistory?.(textContent);
1966
+ }
1967
+ }
1968
+ break;
1969
+ }
1970
+ case "assistant": {
1971
+ const assistantComponent = new AssistantMessageComponent(message, this.hideThinkingBlock, this.getMarkdownThemeWithSettings());
1972
+ this.chatContainer.addChild(assistantComponent);
1973
+ break;
1974
+ }
1975
+ case "toolResult": {
1976
+ // Tool results are rendered inline with tool calls, handled separately
1977
+ break;
1978
+ }
1979
+ default: {
1980
+ const _exhaustive = message;
1981
+ }
1982
+ }
1983
+ }
1984
+ /**
1985
+ * Render session context to chat. Used for initial load and rebuild after compaction.
1986
+ * @param sessionContext Session context to render
1987
+ * @param options.updateFooter Update footer state
1988
+ * @param options.populateHistory Add user messages to editor history
1989
+ */
1990
+ renderSessionContext(sessionContext, options = {}) {
1991
+ this.pendingTools.clear();
1992
+ if (options.updateFooter) {
1993
+ this.footer.invalidate();
1994
+ this.updateEditorBorderColor();
1995
+ }
1996
+ for (const message of sessionContext.messages) {
1997
+ // Assistant messages need special handling for tool calls
1998
+ if (message.role === "assistant") {
1999
+ this.addMessageToChat(message);
2000
+ // Render tool call components
2001
+ for (const content of message.content) {
2002
+ if (content.type === "toolCall") {
2003
+ const component = new ToolExecutionComponent(content.name, content.arguments, { showImages: this.settingsManager.getShowImages() }, this.getRegisteredToolDefinition(content.name), this.ui);
2004
+ component.setExpanded(this.toolOutputExpanded);
2005
+ this.chatContainer.addChild(component);
2006
+ if (message.stopReason === "aborted" || message.stopReason === "error") {
2007
+ let errorMessage;
2008
+ if (message.stopReason === "aborted") {
2009
+ const retryAttempt = this.session.retryAttempt;
2010
+ errorMessage =
2011
+ retryAttempt > 0
2012
+ ? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
2013
+ : "Operation aborted";
2014
+ }
2015
+ else {
2016
+ errorMessage = message.errorMessage || "Error";
2017
+ }
2018
+ component.updateResult({ content: [{ type: "text", text: errorMessage }], isError: true });
2019
+ }
2020
+ else {
2021
+ this.pendingTools.set(content.id, component);
2022
+ }
2023
+ }
2024
+ }
2025
+ }
2026
+ else if (message.role === "toolResult") {
2027
+ // Match tool results to pending tool components
2028
+ const component = this.pendingTools.get(message.toolCallId);
2029
+ if (component) {
2030
+ component.updateResult(message);
2031
+ this.pendingTools.delete(message.toolCallId);
2032
+ }
2033
+ }
2034
+ else {
2035
+ // All other messages use standard rendering
2036
+ this.addMessageToChat(message, options);
2037
+ }
2038
+ }
2039
+ this.pendingTools.clear();
2040
+ this.ui.requestRender();
2041
+ }
2042
+ renderInitialMessages() {
2043
+ // Get aligned messages and entries from session context
2044
+ const context = this.sessionManager.buildSessionContext();
2045
+ this.renderSessionContext(context, {
2046
+ updateFooter: true,
2047
+ populateHistory: true,
2048
+ });
2049
+ // Show compaction info if session was compacted
2050
+ const allEntries = this.sessionManager.getEntries();
2051
+ const compactionCount = allEntries.filter((e) => e.type === "compaction").length;
2052
+ if (compactionCount > 0) {
2053
+ const times = compactionCount === 1 ? "1 time" : `${compactionCount} times`;
2054
+ this.showStatus(`Session compacted ${times}`);
2055
+ }
2056
+ }
2057
+ async getUserInput() {
2058
+ return new Promise((resolve) => {
2059
+ this.onInputCallback = (text) => {
2060
+ this.onInputCallback = undefined;
2061
+ resolve(text);
2062
+ };
2063
+ });
2064
+ }
2065
+ rebuildChatFromMessages() {
2066
+ this.chatContainer.clear();
2067
+ const context = this.sessionManager.buildSessionContext();
2068
+ this.renderSessionContext(context);
2069
+ }
2070
+ // =========================================================================
2071
+ // Key handlers
2072
+ // =========================================================================
2073
+ handleCtrlC() {
2074
+ const now = Date.now();
2075
+ if (now - this.lastSigintTime < 500) {
2076
+ void this.shutdown();
2077
+ }
2078
+ else {
2079
+ this.clearEditor();
2080
+ this.lastSigintTime = now;
2081
+ }
2082
+ }
2083
+ handleCtrlD() {
2084
+ // Only called when editor is empty (enforced by CustomEditor)
2085
+ void this.shutdown();
2086
+ }
2087
+ /**
2088
+ * Gracefully shutdown the agent.
2089
+ * Emits shutdown event to extensions, then exits.
2090
+ */
2091
+ isShuttingDown = false;
2092
+ async shutdown() {
2093
+ if (this.isShuttingDown)
2094
+ return;
2095
+ this.isShuttingDown = true;
2096
+ // Emit shutdown event to extensions
2097
+ const extensionRunner = this.session.extensionRunner;
2098
+ if (extensionRunner?.hasHandlers("session_shutdown")) {
2099
+ await extensionRunner.emit({
2100
+ type: "session_shutdown",
2101
+ });
2102
+ }
2103
+ // Wait for any pending renders to complete
2104
+ // requestRender() uses process.nextTick(), so we wait one tick
2105
+ await new Promise((resolve) => process.nextTick(resolve));
2106
+ // Drain any in-flight Kitty key release events before stopping.
2107
+ // This prevents escape sequences from leaking to the parent shell over slow SSH.
2108
+ await this.ui.terminal.drainInput(1000);
2109
+ this.stop();
2110
+ process.exit(0);
2111
+ }
2112
+ /**
2113
+ * Check if shutdown was requested and perform shutdown if so.
2114
+ */
2115
+ async checkShutdownRequested() {
2116
+ if (!this.shutdownRequested)
2117
+ return;
2118
+ await this.shutdown();
2119
+ }
2120
+ handleCtrlZ() {
2121
+ // Set up handler to restore TUI when resumed
2122
+ process.once("SIGCONT", () => {
2123
+ this.ui.start();
2124
+ this.ui.requestRender(true);
2125
+ });
2126
+ // Stop the TUI (restore terminal to normal mode)
2127
+ this.ui.stop();
2128
+ // Send SIGTSTP to process group (pid=0 means all processes in group)
2129
+ process.kill(0, "SIGTSTP");
2130
+ }
2131
+ async handleFollowUp() {
2132
+ const text = (this.editor.getExpandedText?.() ?? this.editor.getText()).trim();
2133
+ if (!text)
2134
+ return;
2135
+ // Queue input during compaction (extension commands execute immediately)
2136
+ if (this.session.isCompacting) {
2137
+ if (this.isExtensionCommand(text)) {
2138
+ this.editor.addToHistory?.(text);
2139
+ this.editor.setText("");
2140
+ await this.session.prompt(text);
2141
+ }
2142
+ else {
2143
+ this.queueCompactionMessage(text, "followUp");
2144
+ }
2145
+ return;
2146
+ }
2147
+ // Alt+Enter queues a follow-up message (waits until agent finishes)
2148
+ // This handles extension commands (execute immediately), prompt template expansion, and queueing
2149
+ if (this.session.isStreaming) {
2150
+ this.editor.addToHistory?.(text);
2151
+ this.editor.setText("");
2152
+ await this.session.prompt(text, { streamingBehavior: "followUp" });
2153
+ this.updatePendingMessagesDisplay();
2154
+ this.ui.requestRender();
2155
+ }
2156
+ // If not streaming, Alt+Enter acts like regular Enter (trigger onSubmit)
2157
+ else if (this.editor.onSubmit) {
2158
+ this.editor.onSubmit(text);
2159
+ }
2160
+ }
2161
+ handleDequeue() {
2162
+ const restored = this.restoreQueuedMessagesToEditor();
2163
+ if (restored === 0) {
2164
+ this.showStatus("No queued messages to restore");
2165
+ }
2166
+ else {
2167
+ this.showStatus(`Restored ${restored} queued message${restored > 1 ? "s" : ""} to editor`);
2168
+ }
2169
+ }
2170
+ updateEditorBorderColor() {
2171
+ if (this.isBashMode) {
2172
+ this.editor.borderColor = theme.getBashModeBorderColor();
2173
+ }
2174
+ else {
2175
+ const level = this.session.thinkingLevel || "off";
2176
+ this.editor.borderColor = theme.getThinkingBorderColor(level);
2177
+ }
2178
+ this.ui.requestRender();
2179
+ }
2180
+ cycleThinkingLevel() {
2181
+ const newLevel = this.session.cycleThinkingLevel();
2182
+ if (newLevel === undefined) {
2183
+ this.showStatus("Current model does not support thinking");
2184
+ }
2185
+ else {
2186
+ this.footer.invalidate();
2187
+ this.updateEditorBorderColor();
2188
+ this.showStatus(`Thinking level: ${newLevel}`);
2189
+ }
2190
+ }
2191
+ async cycleModel(direction) {
2192
+ try {
2193
+ const result = await this.session.cycleModel(direction);
2194
+ if (result === undefined) {
2195
+ const msg = this.session.scopedModels.length > 0 ? "Only one model in scope" : "Only one model available";
2196
+ this.showStatus(msg);
2197
+ }
2198
+ else {
2199
+ this.footer.invalidate();
2200
+ this.updateEditorBorderColor();
2201
+ const thinkingStr = result.model.reasoning && result.thinkingLevel !== "off" ? ` (thinking: ${result.thinkingLevel})` : "";
2202
+ this.showStatus(`Switched to ${result.model.name || result.model.id}${thinkingStr}`);
2203
+ }
2204
+ }
2205
+ catch (error) {
2206
+ this.showError(error instanceof Error ? error.message : String(error));
2207
+ }
2208
+ }
2209
+ toggleToolOutputExpansion() {
2210
+ this.setToolsExpanded(!this.toolOutputExpanded);
2211
+ }
2212
+ setToolsExpanded(expanded) {
2213
+ this.toolOutputExpanded = expanded;
2214
+ for (const child of this.chatContainer.children) {
2215
+ if (isExpandable(child)) {
2216
+ child.setExpanded(expanded);
2217
+ }
2218
+ }
2219
+ this.ui.requestRender();
2220
+ }
2221
+ toggleThinkingBlockVisibility() {
2222
+ this.hideThinkingBlock = !this.hideThinkingBlock;
2223
+ this.settingsManager.setHideThinkingBlock(this.hideThinkingBlock);
2224
+ // Rebuild chat from session messages
2225
+ this.chatContainer.clear();
2226
+ this.rebuildChatFromMessages();
2227
+ // If streaming, re-add the streaming component with updated visibility and re-render
2228
+ if (this.streamingComponent && this.streamingMessage) {
2229
+ this.streamingComponent.setHideThinkingBlock(this.hideThinkingBlock);
2230
+ this.streamingComponent.updateContent(this.streamingMessage);
2231
+ this.chatContainer.addChild(this.streamingComponent);
2232
+ }
2233
+ this.showStatus(`Thinking blocks: ${this.hideThinkingBlock ? "hidden" : "visible"}`);
2234
+ }
2235
+ openExternalEditor() {
2236
+ // Determine editor (respect $VISUAL, then $EDITOR)
2237
+ const editorCmd = process.env.VISUAL || process.env.EDITOR;
2238
+ if (!editorCmd) {
2239
+ this.showWarning("No editor configured. Set $VISUAL or $EDITOR environment variable.");
2240
+ return;
2241
+ }
2242
+ const currentText = this.editor.getExpandedText?.() ?? this.editor.getText();
2243
+ const tmpFile = path.join(os.tmpdir(), `pi-editor-${Date.now()}.pi.md`);
2244
+ try {
2245
+ // Write current content to temp file
2246
+ fs.writeFileSync(tmpFile, currentText, "utf-8");
2247
+ // Stop TUI to release terminal
2248
+ this.ui.stop();
2249
+ // Split by space to support editor arguments (e.g., "code --wait")
2250
+ const [editor, ...editorArgs] = editorCmd.split(" ");
2251
+ // Spawn editor synchronously with inherited stdio for interactive editing
2252
+ const result = spawnSync(editor, [...editorArgs, tmpFile], {
2253
+ stdio: "inherit",
2254
+ });
2255
+ // On successful exit (status 0), replace editor content
2256
+ if (result.status === 0) {
2257
+ const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
2258
+ this.editor.setText(newContent);
2259
+ }
2260
+ // On non-zero exit, keep original text (no action needed)
2261
+ }
2262
+ finally {
2263
+ // Clean up temp file
2264
+ try {
2265
+ fs.unlinkSync(tmpFile);
2266
+ }
2267
+ catch {
2268
+ // Ignore cleanup errors
2269
+ }
2270
+ // Restart TUI
2271
+ this.ui.start();
2272
+ // Force full re-render since external editor uses alternate screen
2273
+ this.ui.requestRender(true);
2274
+ }
2275
+ }
2276
+ // =========================================================================
2277
+ // UI helpers
2278
+ // =========================================================================
2279
+ clearEditor() {
2280
+ this.editor.setText("");
2281
+ this.ui.requestRender();
2282
+ }
2283
+ showError(errorMessage) {
2284
+ this.chatContainer.addChild(new Spacer(1));
2285
+ this.chatContainer.addChild(new Text(theme.fg("error", `Error: ${errorMessage}`), 1, 0));
2286
+ this.ui.requestRender();
2287
+ }
2288
+ showWarning(warningMessage) {
2289
+ this.chatContainer.addChild(new Spacer(1));
2290
+ this.chatContainer.addChild(new Text(theme.fg("warning", `Warning: ${warningMessage}`), 1, 0));
2291
+ this.ui.requestRender();
2292
+ }
2293
+ showNewVersionNotification(newVersion) {
2294
+ const action = theme.fg("accent", getUpdateInstruction("@mariozechner/pi-coding-agent"));
2295
+ const updateInstruction = theme.fg("muted", `New version ${newVersion} is available. `) + action;
2296
+ const changelogUrl = theme.fg("accent", "https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/CHANGELOG.md");
2297
+ const changelogLine = theme.fg("muted", "Changelog: ") + changelogUrl;
2298
+ this.chatContainer.addChild(new Spacer(1));
2299
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
2300
+ this.chatContainer.addChild(new Text(`${theme.bold(theme.fg("warning", "Update Available"))}\n${updateInstruction}\n${changelogLine}`, 1, 0));
2301
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
2302
+ this.ui.requestRender();
2303
+ }
2304
+ /**
2305
+ * Get all queued messages (read-only).
2306
+ * Combines session queue and compaction queue.
2307
+ */
2308
+ getAllQueuedMessages() {
2309
+ return {
2310
+ steering: [
2311
+ ...this.session.getSteeringMessages(),
2312
+ ...this.compactionQueuedMessages.filter((msg) => msg.mode === "steer").map((msg) => msg.text),
2313
+ ],
2314
+ followUp: [
2315
+ ...this.session.getFollowUpMessages(),
2316
+ ...this.compactionQueuedMessages.filter((msg) => msg.mode === "followUp").map((msg) => msg.text),
2317
+ ],
2318
+ };
2319
+ }
2320
+ /**
2321
+ * Clear all queued messages and return their contents.
2322
+ * Clears both session queue and compaction queue.
2323
+ */
2324
+ clearAllQueues() {
2325
+ const { steering, followUp } = this.session.clearQueue();
2326
+ const compactionSteering = this.compactionQueuedMessages
2327
+ .filter((msg) => msg.mode === "steer")
2328
+ .map((msg) => msg.text);
2329
+ const compactionFollowUp = this.compactionQueuedMessages
2330
+ .filter((msg) => msg.mode === "followUp")
2331
+ .map((msg) => msg.text);
2332
+ this.compactionQueuedMessages = [];
2333
+ return {
2334
+ steering: [...steering, ...compactionSteering],
2335
+ followUp: [...followUp, ...compactionFollowUp],
2336
+ };
2337
+ }
2338
+ updatePendingMessagesDisplay() {
2339
+ this.pendingMessagesContainer.clear();
2340
+ const { steering: steeringMessages, followUp: followUpMessages } = this.getAllQueuedMessages();
2341
+ if (steeringMessages.length > 0 || followUpMessages.length > 0) {
2342
+ this.pendingMessagesContainer.addChild(new Spacer(1));
2343
+ for (const message of steeringMessages) {
2344
+ const text = theme.fg("dim", `Steering: ${message}`);
2345
+ this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
2346
+ }
2347
+ for (const message of followUpMessages) {
2348
+ const text = theme.fg("dim", `Follow-up: ${message}`);
2349
+ this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
2350
+ }
2351
+ const dequeueHint = this.getAppKeyDisplay("dequeue");
2352
+ const hintText = theme.fg("dim", `↳ ${dequeueHint} to edit all queued messages`);
2353
+ this.pendingMessagesContainer.addChild(new TruncatedText(hintText, 1, 0));
2354
+ }
2355
+ }
2356
+ restoreQueuedMessagesToEditor(options) {
2357
+ const { steering, followUp } = this.clearAllQueues();
2358
+ const allQueued = [...steering, ...followUp];
2359
+ if (allQueued.length === 0) {
2360
+ this.updatePendingMessagesDisplay();
2361
+ if (options?.abort) {
2362
+ this.agent.abort();
2363
+ }
2364
+ return 0;
2365
+ }
2366
+ const queuedText = allQueued.join("\n\n");
2367
+ const currentText = options?.currentText ?? this.editor.getText();
2368
+ const combinedText = [queuedText, currentText].filter((t) => t.trim()).join("\n\n");
2369
+ this.editor.setText(combinedText);
2370
+ this.updatePendingMessagesDisplay();
2371
+ if (options?.abort) {
2372
+ this.agent.abort();
2373
+ }
2374
+ return allQueued.length;
2375
+ }
2376
+ queueCompactionMessage(text, mode) {
2377
+ this.compactionQueuedMessages.push({ text, mode });
2378
+ this.editor.addToHistory?.(text);
2379
+ this.editor.setText("");
2380
+ this.updatePendingMessagesDisplay();
2381
+ this.showStatus("Queued message for after compaction");
2382
+ }
2383
+ isExtensionCommand(text) {
2384
+ if (!text.startsWith("/"))
2385
+ return false;
2386
+ const extensionRunner = this.session.extensionRunner;
2387
+ if (!extensionRunner)
2388
+ return false;
2389
+ const spaceIndex = text.indexOf(" ");
2390
+ const commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
2391
+ return !!extensionRunner.getCommand(commandName);
2392
+ }
2393
+ async flushCompactionQueue(options) {
2394
+ if (this.compactionQueuedMessages.length === 0) {
2395
+ return;
2396
+ }
2397
+ const queuedMessages = [...this.compactionQueuedMessages];
2398
+ this.compactionQueuedMessages = [];
2399
+ this.updatePendingMessagesDisplay();
2400
+ const restoreQueue = (error) => {
2401
+ this.session.clearQueue();
2402
+ this.compactionQueuedMessages = queuedMessages;
2403
+ this.updatePendingMessagesDisplay();
2404
+ this.showError(`Failed to send queued message${queuedMessages.length > 1 ? "s" : ""}: ${error instanceof Error ? error.message : String(error)}`);
2405
+ };
2406
+ try {
2407
+ if (options?.willRetry) {
2408
+ // When retry is pending, queue messages for the retry turn
2409
+ for (const message of queuedMessages) {
2410
+ if (this.isExtensionCommand(message.text)) {
2411
+ await this.session.prompt(message.text);
2412
+ }
2413
+ else if (message.mode === "followUp") {
2414
+ await this.session.followUp(message.text);
2415
+ }
2416
+ else {
2417
+ await this.session.steer(message.text);
2418
+ }
2419
+ }
2420
+ this.updatePendingMessagesDisplay();
2421
+ return;
2422
+ }
2423
+ // Find first non-extension-command message to use as prompt
2424
+ const firstPromptIndex = queuedMessages.findIndex((message) => !this.isExtensionCommand(message.text));
2425
+ if (firstPromptIndex === -1) {
2426
+ // All extension commands - execute them all
2427
+ for (const message of queuedMessages) {
2428
+ await this.session.prompt(message.text);
2429
+ }
2430
+ return;
2431
+ }
2432
+ // Execute any extension commands before the first prompt
2433
+ const preCommands = queuedMessages.slice(0, firstPromptIndex);
2434
+ const firstPrompt = queuedMessages[firstPromptIndex];
2435
+ const rest = queuedMessages.slice(firstPromptIndex + 1);
2436
+ for (const message of preCommands) {
2437
+ await this.session.prompt(message.text);
2438
+ }
2439
+ // Send first prompt (starts streaming)
2440
+ const promptPromise = this.session.prompt(firstPrompt.text).catch((error) => {
2441
+ restoreQueue(error);
2442
+ });
2443
+ // Queue remaining messages
2444
+ for (const message of rest) {
2445
+ if (this.isExtensionCommand(message.text)) {
2446
+ await this.session.prompt(message.text);
2447
+ }
2448
+ else if (message.mode === "followUp") {
2449
+ await this.session.followUp(message.text);
2450
+ }
2451
+ else {
2452
+ await this.session.steer(message.text);
2453
+ }
2454
+ }
2455
+ this.updatePendingMessagesDisplay();
2456
+ void promptPromise;
2457
+ }
2458
+ catch (error) {
2459
+ restoreQueue(error);
2460
+ }
2461
+ }
2462
+ /** Move pending bash components from pending area to chat */
2463
+ flushPendingBashComponents() {
2464
+ for (const component of this.pendingBashComponents) {
2465
+ this.pendingMessagesContainer.removeChild(component);
2466
+ this.chatContainer.addChild(component);
2467
+ }
2468
+ this.pendingBashComponents = [];
2469
+ }
2470
+ // =========================================================================
2471
+ // Selectors
2472
+ // =========================================================================
2473
+ /**
2474
+ * Shows a selector component in place of the editor.
2475
+ * @param create Factory that receives a `done` callback and returns the component and focus target
2476
+ */
2477
+ showSelector(create) {
2478
+ const done = () => {
2479
+ this.editorContainer.clear();
2480
+ this.editorContainer.addChild(this.editor);
2481
+ this.ui.setFocus(this.editor);
2482
+ };
2483
+ const { component, focus } = create(done);
2484
+ this.editorContainer.clear();
2485
+ this.editorContainer.addChild(component);
2486
+ this.ui.setFocus(focus);
2487
+ this.ui.requestRender();
2488
+ }
2489
+ showSettingsSelector() {
2490
+ this.showSelector((done) => {
2491
+ const selector = new SettingsSelectorComponent({
2492
+ autoCompact: this.session.autoCompactionEnabled,
2493
+ showImages: this.settingsManager.getShowImages(),
2494
+ autoResizeImages: this.settingsManager.getImageAutoResize(),
2495
+ blockImages: this.settingsManager.getBlockImages(),
2496
+ enableSkillCommands: this.settingsManager.getEnableSkillCommands(),
2497
+ steeringMode: this.session.steeringMode,
2498
+ followUpMode: this.session.followUpMode,
2499
+ thinkingLevel: this.session.thinkingLevel,
2500
+ availableThinkingLevels: this.session.getAvailableThinkingLevels(),
2501
+ currentTheme: this.settingsManager.getTheme() || "dark",
2502
+ availableThemes: getAvailableThemes(),
2503
+ hideThinkingBlock: this.hideThinkingBlock,
2504
+ collapseChangelog: this.settingsManager.getCollapseChangelog(),
2505
+ doubleEscapeAction: this.settingsManager.getDoubleEscapeAction(),
2506
+ showHardwareCursor: this.settingsManager.getShowHardwareCursor(),
2507
+ editorPaddingX: this.settingsManager.getEditorPaddingX(),
2508
+ autocompleteMaxVisible: this.settingsManager.getAutocompleteMaxVisible(),
2509
+ quietStartup: this.settingsManager.getQuietStartup(),
2510
+ clearOnShrink: this.settingsManager.getClearOnShrink(),
2511
+ }, {
2512
+ onAutoCompactChange: (enabled) => {
2513
+ this.session.setAutoCompactionEnabled(enabled);
2514
+ this.footer.setAutoCompactEnabled(enabled);
2515
+ },
2516
+ onShowImagesChange: (enabled) => {
2517
+ this.settingsManager.setShowImages(enabled);
2518
+ for (const child of this.chatContainer.children) {
2519
+ if (child instanceof ToolExecutionComponent) {
2520
+ child.setShowImages(enabled);
2521
+ }
2522
+ }
2523
+ },
2524
+ onAutoResizeImagesChange: (enabled) => {
2525
+ this.settingsManager.setImageAutoResize(enabled);
2526
+ },
2527
+ onBlockImagesChange: (blocked) => {
2528
+ this.settingsManager.setBlockImages(blocked);
2529
+ },
2530
+ onEnableSkillCommandsChange: (enabled) => {
2531
+ this.settingsManager.setEnableSkillCommands(enabled);
2532
+ this.rebuildAutocomplete();
2533
+ },
2534
+ onSteeringModeChange: (mode) => {
2535
+ this.session.setSteeringMode(mode);
2536
+ },
2537
+ onFollowUpModeChange: (mode) => {
2538
+ this.session.setFollowUpMode(mode);
2539
+ },
2540
+ onThinkingLevelChange: (level) => {
2541
+ this.session.setThinkingLevel(level);
2542
+ this.footer.invalidate();
2543
+ this.updateEditorBorderColor();
2544
+ },
2545
+ onThemeChange: (themeName) => {
2546
+ const result = setTheme(themeName, true);
2547
+ this.settingsManager.setTheme(themeName);
2548
+ this.ui.invalidate();
2549
+ if (!result.success) {
2550
+ this.showError(`Failed to load theme "${themeName}": ${result.error}\nFell back to dark theme.`);
2551
+ }
2552
+ },
2553
+ onThemePreview: (themeName) => {
2554
+ const result = setTheme(themeName, true);
2555
+ if (result.success) {
2556
+ this.ui.invalidate();
2557
+ this.ui.requestRender();
2558
+ }
2559
+ },
2560
+ onHideThinkingBlockChange: (hidden) => {
2561
+ this.hideThinkingBlock = hidden;
2562
+ this.settingsManager.setHideThinkingBlock(hidden);
2563
+ for (const child of this.chatContainer.children) {
2564
+ if (child instanceof AssistantMessageComponent) {
2565
+ child.setHideThinkingBlock(hidden);
2566
+ }
2567
+ }
2568
+ this.chatContainer.clear();
2569
+ this.rebuildChatFromMessages();
2570
+ },
2571
+ onCollapseChangelogChange: (collapsed) => {
2572
+ this.settingsManager.setCollapseChangelog(collapsed);
2573
+ },
2574
+ onQuietStartupChange: (enabled) => {
2575
+ this.settingsManager.setQuietStartup(enabled);
2576
+ },
2577
+ onDoubleEscapeActionChange: (action) => {
2578
+ this.settingsManager.setDoubleEscapeAction(action);
2579
+ },
2580
+ onShowHardwareCursorChange: (enabled) => {
2581
+ this.settingsManager.setShowHardwareCursor(enabled);
2582
+ this.ui.setShowHardwareCursor(enabled);
2583
+ },
2584
+ onEditorPaddingXChange: (padding) => {
2585
+ this.settingsManager.setEditorPaddingX(padding);
2586
+ this.defaultEditor.setPaddingX(padding);
2587
+ if (this.editor !== this.defaultEditor && this.editor.setPaddingX !== undefined) {
2588
+ this.editor.setPaddingX(padding);
2589
+ }
2590
+ },
2591
+ onAutocompleteMaxVisibleChange: (maxVisible) => {
2592
+ this.settingsManager.setAutocompleteMaxVisible(maxVisible);
2593
+ this.defaultEditor.setAutocompleteMaxVisible(maxVisible);
2594
+ if (this.editor !== this.defaultEditor && this.editor.setAutocompleteMaxVisible !== undefined) {
2595
+ this.editor.setAutocompleteMaxVisible(maxVisible);
2596
+ }
2597
+ },
2598
+ onClearOnShrinkChange: (enabled) => {
2599
+ this.settingsManager.setClearOnShrink(enabled);
2600
+ this.ui.setClearOnShrink(enabled);
2601
+ },
2602
+ onCancel: () => {
2603
+ done();
2604
+ this.ui.requestRender();
2605
+ },
2606
+ });
2607
+ return { component: selector, focus: selector.getSettingsList() };
2608
+ });
2609
+ }
2610
+ async handleModelCommand(searchTerm) {
2611
+ if (!searchTerm) {
2612
+ this.showModelSelector();
2613
+ return;
2614
+ }
2615
+ const model = await this.findExactModelMatch(searchTerm);
2616
+ if (model) {
2617
+ try {
2618
+ await this.session.setModel(model);
2619
+ this.footer.invalidate();
2620
+ this.updateEditorBorderColor();
2621
+ this.showStatus(`Model: ${model.id}`);
2622
+ this.checkDaxnutsEasterEgg(model);
2623
+ }
2624
+ catch (error) {
2625
+ this.showError(error instanceof Error ? error.message : String(error));
2626
+ }
2627
+ return;
2628
+ }
2629
+ this.showModelSelector(searchTerm);
2630
+ }
2631
+ async findExactModelMatch(searchTerm) {
2632
+ const term = searchTerm.trim();
2633
+ if (!term)
2634
+ return undefined;
2635
+ let targetProvider;
2636
+ let targetModelId = "";
2637
+ if (term.includes("/")) {
2638
+ const parts = term.split("/", 2);
2639
+ targetProvider = parts[0]?.trim().toLowerCase();
2640
+ targetModelId = parts[1]?.trim().toLowerCase() ?? "";
2641
+ }
2642
+ else {
2643
+ targetModelId = term.toLowerCase();
2644
+ }
2645
+ if (!targetModelId)
2646
+ return undefined;
2647
+ const models = await this.getModelCandidates();
2648
+ const exactMatches = models.filter((item) => {
2649
+ const idMatch = item.id.toLowerCase() === targetModelId;
2650
+ const providerMatch = !targetProvider || item.provider.toLowerCase() === targetProvider;
2651
+ return idMatch && providerMatch;
2652
+ });
2653
+ return exactMatches.length === 1 ? exactMatches[0] : undefined;
2654
+ }
2655
+ async getModelCandidates() {
2656
+ if (this.session.scopedModels.length > 0) {
2657
+ return this.session.scopedModels.map((scoped) => scoped.model);
2658
+ }
2659
+ this.session.modelRegistry.refresh();
2660
+ try {
2661
+ return await this.session.modelRegistry.getAvailable();
2662
+ }
2663
+ catch {
2664
+ return [];
2665
+ }
2666
+ }
2667
+ /** Update the footer's available provider count from current model candidates */
2668
+ async updateAvailableProviderCount() {
2669
+ const models = await this.getModelCandidates();
2670
+ const uniqueProviders = new Set(models.map((m) => m.provider));
2671
+ this.footerDataProvider.setAvailableProviderCount(uniqueProviders.size);
2672
+ }
2673
+ showModelSelector(initialSearchInput) {
2674
+ this.showSelector((done) => {
2675
+ const selector = new ModelSelectorComponent(this.ui, this.session.model, this.settingsManager, this.session.modelRegistry, this.session.scopedModels, async (model) => {
2676
+ try {
2677
+ await this.session.setModel(model);
2678
+ this.footer.invalidate();
2679
+ this.updateEditorBorderColor();
2680
+ done();
2681
+ this.showStatus(`Model: ${model.id}`);
2682
+ this.checkDaxnutsEasterEgg(model);
2683
+ }
2684
+ catch (error) {
2685
+ done();
2686
+ this.showError(error instanceof Error ? error.message : String(error));
2687
+ }
2688
+ }, () => {
2689
+ done();
2690
+ this.ui.requestRender();
2691
+ }, initialSearchInput);
2692
+ return { component: selector, focus: selector };
2693
+ });
2694
+ }
2695
+ async showModelsSelector() {
2696
+ // Get all available models
2697
+ this.session.modelRegistry.refresh();
2698
+ const allModels = this.session.modelRegistry.getAvailable();
2699
+ if (allModels.length === 0) {
2700
+ this.showStatus("No models available");
2701
+ return;
2702
+ }
2703
+ // Check if session has scoped models (from previous session-only changes or CLI --models)
2704
+ const sessionScopedModels = this.session.scopedModels;
2705
+ const hasSessionScope = sessionScopedModels.length > 0;
2706
+ // Build enabled model IDs from session state or settings
2707
+ const enabledModelIds = new Set();
2708
+ let hasFilter = false;
2709
+ if (hasSessionScope) {
2710
+ // Use current session's scoped models
2711
+ for (const sm of sessionScopedModels) {
2712
+ enabledModelIds.add(`${sm.model.provider}/${sm.model.id}`);
2713
+ }
2714
+ hasFilter = true;
2715
+ }
2716
+ else {
2717
+ // Fall back to settings
2718
+ const patterns = this.settingsManager.getEnabledModels();
2719
+ if (patterns !== undefined && patterns.length > 0) {
2720
+ hasFilter = true;
2721
+ const scopedModels = await resolveModelScope(patterns, this.session.modelRegistry);
2722
+ for (const sm of scopedModels) {
2723
+ enabledModelIds.add(`${sm.model.provider}/${sm.model.id}`);
2724
+ }
2725
+ }
2726
+ }
2727
+ // Track current enabled state (session-only until persisted)
2728
+ const currentEnabledIds = new Set(enabledModelIds);
2729
+ let currentHasFilter = hasFilter;
2730
+ // Helper to update session's scoped models (session-only, no persist)
2731
+ const updateSessionModels = async (enabledIds) => {
2732
+ if (enabledIds.size > 0 && enabledIds.size < allModels.length) {
2733
+ // Use current session thinking level, not settings default
2734
+ const currentThinkingLevel = this.session.thinkingLevel;
2735
+ const newScopedModels = await resolveModelScope(Array.from(enabledIds), this.session.modelRegistry);
2736
+ this.session.setScopedModels(newScopedModels.map((sm) => ({
2737
+ model: sm.model,
2738
+ thinkingLevel: sm.thinkingLevel ?? currentThinkingLevel,
2739
+ })));
2740
+ }
2741
+ else {
2742
+ // All enabled or none enabled = no filter
2743
+ this.session.setScopedModels([]);
2744
+ }
2745
+ await this.updateAvailableProviderCount();
2746
+ this.ui.requestRender();
2747
+ };
2748
+ this.showSelector((done) => {
2749
+ const selector = new ScopedModelsSelectorComponent({
2750
+ allModels,
2751
+ enabledModelIds: currentEnabledIds,
2752
+ hasEnabledModelsFilter: currentHasFilter,
2753
+ }, {
2754
+ onModelToggle: async (modelId, enabled) => {
2755
+ if (enabled) {
2756
+ currentEnabledIds.add(modelId);
2757
+ }
2758
+ else {
2759
+ currentEnabledIds.delete(modelId);
2760
+ }
2761
+ currentHasFilter = true;
2762
+ await updateSessionModels(currentEnabledIds);
2763
+ },
2764
+ onEnableAll: async (allModelIds) => {
2765
+ currentEnabledIds.clear();
2766
+ for (const id of allModelIds) {
2767
+ currentEnabledIds.add(id);
2768
+ }
2769
+ currentHasFilter = false;
2770
+ await updateSessionModels(currentEnabledIds);
2771
+ },
2772
+ onClearAll: async () => {
2773
+ currentEnabledIds.clear();
2774
+ currentHasFilter = true;
2775
+ await updateSessionModels(currentEnabledIds);
2776
+ },
2777
+ onToggleProvider: async (_provider, modelIds, enabled) => {
2778
+ for (const id of modelIds) {
2779
+ if (enabled) {
2780
+ currentEnabledIds.add(id);
2781
+ }
2782
+ else {
2783
+ currentEnabledIds.delete(id);
2784
+ }
2785
+ }
2786
+ currentHasFilter = true;
2787
+ await updateSessionModels(currentEnabledIds);
2788
+ },
2789
+ onPersist: (enabledIds) => {
2790
+ // Persist to settings
2791
+ const newPatterns = enabledIds.length === allModels.length
2792
+ ? undefined // All enabled = clear filter
2793
+ : enabledIds;
2794
+ this.settingsManager.setEnabledModels(newPatterns);
2795
+ this.showStatus("Model selection saved to settings");
2796
+ },
2797
+ onCancel: () => {
2798
+ done();
2799
+ this.ui.requestRender();
2800
+ },
2801
+ });
2802
+ return { component: selector, focus: selector };
2803
+ });
2804
+ }
2805
+ showUserMessageSelector() {
2806
+ const userMessages = this.session.getUserMessagesForForking();
2807
+ if (userMessages.length === 0) {
2808
+ this.showStatus("No messages to fork from");
2809
+ return;
2810
+ }
2811
+ this.showSelector((done) => {
2812
+ const selector = new UserMessageSelectorComponent(userMessages.map((m) => ({ id: m.entryId, text: m.text })), async (entryId) => {
2813
+ const result = await this.session.fork(entryId);
2814
+ if (result.cancelled) {
2815
+ // Extension cancelled the fork
2816
+ done();
2817
+ this.ui.requestRender();
2818
+ return;
2819
+ }
2820
+ this.chatContainer.clear();
2821
+ this.renderInitialMessages();
2822
+ this.editor.setText(result.selectedText);
2823
+ done();
2824
+ this.showStatus("Branched to new session");
2825
+ }, () => {
2826
+ done();
2827
+ this.ui.requestRender();
2828
+ });
2829
+ return { component: selector, focus: selector.getMessageList() };
2830
+ });
2831
+ }
2832
+ showTreeSelector(initialSelectedId) {
2833
+ const tree = this.sessionManager.getTree();
2834
+ const realLeafId = this.sessionManager.getLeafId();
2835
+ if (tree.length === 0) {
2836
+ this.showStatus("No entries in session");
2837
+ return;
2838
+ }
2839
+ this.showSelector((done) => {
2840
+ const selector = new TreeSelectorComponent(tree, realLeafId, this.ui.terminal.rows, async (entryId) => {
2841
+ // Selecting the current leaf is a no-op (already there)
2842
+ if (entryId === realLeafId) {
2843
+ done();
2844
+ this.showStatus("Already at this point");
2845
+ return;
2846
+ }
2847
+ // Ask about summarization
2848
+ done(); // Close selector first
2849
+ // Loop until user makes a complete choice or cancels to tree
2850
+ let wantsSummary = false;
2851
+ let customInstructions;
2852
+ while (true) {
2853
+ const summaryChoice = await this.showExtensionSelector("Summarize branch?", [
2854
+ "No summary",
2855
+ "Summarize",
2856
+ "Summarize with custom prompt",
2857
+ ]);
2858
+ if (summaryChoice === undefined) {
2859
+ // User pressed escape - re-show tree selector with same selection
2860
+ this.showTreeSelector(entryId);
2861
+ return;
2862
+ }
2863
+ wantsSummary = summaryChoice !== "No summary";
2864
+ if (summaryChoice === "Summarize with custom prompt") {
2865
+ customInstructions = await this.showExtensionEditor("Custom summarization instructions");
2866
+ if (customInstructions === undefined) {
2867
+ // User cancelled - loop back to summary selector
2868
+ continue;
2869
+ }
2870
+ }
2871
+ // User made a complete choice
2872
+ break;
2873
+ }
2874
+ // Set up escape handler and loader if summarizing
2875
+ let summaryLoader;
2876
+ const originalOnEscape = this.defaultEditor.onEscape;
2877
+ if (wantsSummary) {
2878
+ this.defaultEditor.onEscape = () => {
2879
+ this.session.abortBranchSummary();
2880
+ };
2881
+ this.chatContainer.addChild(new Spacer(1));
2882
+ summaryLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), `Summarizing branch... (${appKey(this.keybindings, "interrupt")} to cancel)`);
2883
+ this.statusContainer.addChild(summaryLoader);
2884
+ this.ui.requestRender();
2885
+ }
2886
+ try {
2887
+ const result = await this.session.navigateTree(entryId, {
2888
+ summarize: wantsSummary,
2889
+ customInstructions,
2890
+ });
2891
+ if (result.aborted) {
2892
+ // Summarization aborted - re-show tree selector with same selection
2893
+ this.showStatus("Branch summarization cancelled");
2894
+ this.showTreeSelector(entryId);
2895
+ return;
2896
+ }
2897
+ if (result.cancelled) {
2898
+ this.showStatus("Navigation cancelled");
2899
+ return;
2900
+ }
2901
+ // Update UI
2902
+ this.chatContainer.clear();
2903
+ this.renderInitialMessages();
2904
+ if (result.editorText && !this.editor.getText().trim()) {
2905
+ this.editor.setText(result.editorText);
2906
+ }
2907
+ this.showStatus("Navigated to selected point");
2908
+ }
2909
+ catch (error) {
2910
+ this.showError(error instanceof Error ? error.message : String(error));
2911
+ }
2912
+ finally {
2913
+ if (summaryLoader) {
2914
+ summaryLoader.stop();
2915
+ this.statusContainer.clear();
2916
+ }
2917
+ this.defaultEditor.onEscape = originalOnEscape;
2918
+ }
2919
+ }, () => {
2920
+ done();
2921
+ this.ui.requestRender();
2922
+ }, (entryId, label) => {
2923
+ this.sessionManager.appendLabelChange(entryId, label);
2924
+ this.ui.requestRender();
2925
+ }, initialSelectedId);
2926
+ return { component: selector, focus: selector };
2927
+ });
2928
+ }
2929
+ showSessionSelector() {
2930
+ this.showSelector((done) => {
2931
+ const selector = new SessionSelectorComponent((onProgress) => SessionManager.list(this.sessionManager.getCwd(), this.sessionManager.getSessionDir(), onProgress), SessionManager.listAll, async (sessionPath) => {
2932
+ done();
2933
+ await this.handleResumeSession(sessionPath);
2934
+ }, () => {
2935
+ done();
2936
+ this.ui.requestRender();
2937
+ }, () => {
2938
+ void this.shutdown();
2939
+ }, () => this.ui.requestRender(), {
2940
+ renameSession: async (sessionFilePath, nextName) => {
2941
+ const next = (nextName ?? "").trim();
2942
+ if (!next)
2943
+ return;
2944
+ const mgr = SessionManager.open(sessionFilePath);
2945
+ mgr.appendSessionInfo(next);
2946
+ },
2947
+ showRenameHint: true,
2948
+ keybindings: this.keybindings,
2949
+ }, this.sessionManager.getSessionFile());
2950
+ return { component: selector, focus: selector };
2951
+ });
2952
+ }
2953
+ async handleResumeSession(sessionPath) {
2954
+ // Stop loading animation
2955
+ if (this.loadingAnimation) {
2956
+ this.loadingAnimation.stop();
2957
+ this.loadingAnimation = undefined;
2958
+ }
2959
+ this.statusContainer.clear();
2960
+ // Clear UI state
2961
+ this.pendingMessagesContainer.clear();
2962
+ this.compactionQueuedMessages = [];
2963
+ this.streamingComponent = undefined;
2964
+ this.streamingMessage = undefined;
2965
+ this.pendingTools.clear();
2966
+ // Switch session via AgentSession (emits extension session events)
2967
+ await this.session.switchSession(sessionPath);
2968
+ // Clear and re-render the chat
2969
+ this.chatContainer.clear();
2970
+ this.renderInitialMessages();
2971
+ this.showStatus("Resumed session");
2972
+ }
2973
+ async showOAuthSelector(mode) {
2974
+ if (mode === "logout") {
2975
+ const providers = this.session.modelRegistry.authStorage.list();
2976
+ const loggedInProviders = providers.filter((p) => this.session.modelRegistry.authStorage.get(p)?.type === "oauth");
2977
+ if (loggedInProviders.length === 0) {
2978
+ this.showStatus("No OAuth providers logged in. Use /login first.");
2979
+ return;
2980
+ }
2981
+ }
2982
+ this.showSelector((done) => {
2983
+ const selector = new OAuthSelectorComponent(mode, this.session.modelRegistry.authStorage, async (providerId) => {
2984
+ done();
2985
+ if (mode === "login") {
2986
+ await this.showLoginDialog(providerId);
2987
+ }
2988
+ else {
2989
+ // Logout flow
2990
+ const providerInfo = getOAuthProviders().find((p) => p.id === providerId);
2991
+ const providerName = providerInfo?.name || providerId;
2992
+ try {
2993
+ this.session.modelRegistry.authStorage.logout(providerId);
2994
+ this.session.modelRegistry.refresh();
2995
+ await this.updateAvailableProviderCount();
2996
+ this.showStatus(`Logged out of ${providerName}`);
2997
+ }
2998
+ catch (error) {
2999
+ this.showError(`Logout failed: ${error instanceof Error ? error.message : String(error)}`);
3000
+ }
3001
+ }
3002
+ }, () => {
3003
+ done();
3004
+ this.ui.requestRender();
3005
+ });
3006
+ return { component: selector, focus: selector };
3007
+ });
3008
+ }
3009
+ async showLoginDialog(providerId) {
3010
+ const providerInfo = getOAuthProviders().find((p) => p.id === providerId);
3011
+ const providerName = providerInfo?.name || providerId;
3012
+ // Providers that use callback servers (can paste redirect URL)
3013
+ const usesCallbackServer = providerInfo?.usesCallbackServer ?? false;
3014
+ // Create login dialog component
3015
+ const dialog = new LoginDialogComponent(this.ui, providerId, (_success, _message) => {
3016
+ // Completion handled below
3017
+ });
3018
+ // Show dialog in editor container
3019
+ this.editorContainer.clear();
3020
+ this.editorContainer.addChild(dialog);
3021
+ this.ui.setFocus(dialog);
3022
+ this.ui.requestRender();
3023
+ // Promise for manual code input (racing with callback server)
3024
+ let manualCodeResolve;
3025
+ let manualCodeReject;
3026
+ const manualCodePromise = new Promise((resolve, reject) => {
3027
+ manualCodeResolve = resolve;
3028
+ manualCodeReject = reject;
3029
+ });
3030
+ // Restore editor helper
3031
+ const restoreEditor = () => {
3032
+ this.editorContainer.clear();
3033
+ this.editorContainer.addChild(this.editor);
3034
+ this.ui.setFocus(this.editor);
3035
+ this.ui.requestRender();
3036
+ };
3037
+ try {
3038
+ await this.session.modelRegistry.authStorage.login(providerId, {
3039
+ onAuth: (info) => {
3040
+ dialog.showAuth(info.url, info.instructions);
3041
+ if (usesCallbackServer) {
3042
+ // Show input for manual paste, racing with callback
3043
+ dialog
3044
+ .showManualInput("Paste redirect URL below, or complete login in browser:")
3045
+ .then((value) => {
3046
+ if (value && manualCodeResolve) {
3047
+ manualCodeResolve(value);
3048
+ manualCodeResolve = undefined;
3049
+ }
3050
+ })
3051
+ .catch(() => {
3052
+ if (manualCodeReject) {
3053
+ manualCodeReject(new Error("Login cancelled"));
3054
+ manualCodeReject = undefined;
3055
+ }
3056
+ });
3057
+ }
3058
+ else if (providerId === "github-copilot") {
3059
+ // GitHub Copilot polls after onAuth
3060
+ dialog.showWaiting("Waiting for browser authentication...");
3061
+ }
3062
+ // For Anthropic: onPrompt is called immediately after
3063
+ },
3064
+ onPrompt: async (prompt) => {
3065
+ return dialog.showPrompt(prompt.message, prompt.placeholder);
3066
+ },
3067
+ onProgress: (message) => {
3068
+ dialog.showProgress(message);
3069
+ },
3070
+ onManualCodeInput: () => manualCodePromise,
3071
+ signal: dialog.signal,
3072
+ });
3073
+ // Success
3074
+ restoreEditor();
3075
+ this.session.modelRegistry.refresh();
3076
+ await this.updateAvailableProviderCount();
3077
+ this.showStatus(`Logged in to ${providerName}. Credentials saved to ${getAuthPath()}`);
3078
+ }
3079
+ catch (error) {
3080
+ restoreEditor();
3081
+ const errorMsg = error instanceof Error ? error.message : String(error);
3082
+ if (errorMsg !== "Login cancelled") {
3083
+ this.showError(`Failed to login to ${providerName}: ${errorMsg}`);
3084
+ }
3085
+ }
3086
+ }
3087
+ // =========================================================================
3088
+ // Command handlers
3089
+ // =========================================================================
3090
+ async handleReloadCommand() {
3091
+ if (this.session.isStreaming) {
3092
+ this.showWarning("Wait for the current response to finish before reloading.");
3093
+ return;
3094
+ }
3095
+ if (this.session.isCompacting) {
3096
+ this.showWarning("Wait for compaction to finish before reloading.");
3097
+ return;
3098
+ }
3099
+ this.resetExtensionUI();
3100
+ const loader = new BorderedLoader(this.ui, theme, "Reloading extensions, skills, prompts, themes...", {
3101
+ cancellable: false,
3102
+ });
3103
+ const previousEditor = this.editor;
3104
+ this.editorContainer.clear();
3105
+ this.editorContainer.addChild(loader);
3106
+ this.ui.setFocus(loader);
3107
+ this.ui.requestRender();
3108
+ const dismissLoader = (editor) => {
3109
+ loader.dispose();
3110
+ this.editorContainer.clear();
3111
+ this.editorContainer.addChild(editor);
3112
+ this.ui.setFocus(editor);
3113
+ this.ui.requestRender();
3114
+ };
3115
+ try {
3116
+ await this.session.reload();
3117
+ setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
3118
+ this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
3119
+ const themeName = this.settingsManager.getTheme();
3120
+ const themeResult = themeName ? setTheme(themeName, true) : { success: true };
3121
+ if (!themeResult.success) {
3122
+ this.showError(`Failed to load theme "${themeName}": ${themeResult.error}\nFell back to dark theme.`);
3123
+ }
3124
+ const editorPaddingX = this.settingsManager.getEditorPaddingX();
3125
+ const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
3126
+ this.defaultEditor.setPaddingX(editorPaddingX);
3127
+ this.defaultEditor.setAutocompleteMaxVisible(autocompleteMaxVisible);
3128
+ if (this.editor !== this.defaultEditor) {
3129
+ this.editor.setPaddingX?.(editorPaddingX);
3130
+ this.editor.setAutocompleteMaxVisible?.(autocompleteMaxVisible);
3131
+ }
3132
+ this.ui.setShowHardwareCursor(this.settingsManager.getShowHardwareCursor());
3133
+ this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
3134
+ this.rebuildAutocomplete();
3135
+ const runner = this.session.extensionRunner;
3136
+ if (runner) {
3137
+ this.setupExtensionShortcuts(runner);
3138
+ }
3139
+ this.rebuildChatFromMessages();
3140
+ dismissLoader(this.editor);
3141
+ this.showLoadedResources({ extensionPaths: runner?.getExtensionPaths() ?? [], force: true });
3142
+ const modelsJsonError = this.session.modelRegistry.getError();
3143
+ if (modelsJsonError) {
3144
+ this.showError(`models.json error: ${modelsJsonError}`);
3145
+ }
3146
+ this.showStatus("Reloaded extensions, skills, prompts, themes");
3147
+ }
3148
+ catch (error) {
3149
+ dismissLoader(previousEditor);
3150
+ this.showError(`Reload failed: ${error instanceof Error ? error.message : String(error)}`);
3151
+ }
3152
+ }
3153
+ async handleExportCommand(text) {
3154
+ const parts = text.split(/\s+/);
3155
+ const outputPath = parts.length > 1 ? parts[1] : undefined;
3156
+ try {
3157
+ const filePath = await this.session.exportToHtml(outputPath);
3158
+ this.showStatus(`Session exported to: ${filePath}`);
3159
+ }
3160
+ catch (error) {
3161
+ this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
3162
+ }
3163
+ }
3164
+ async handleShareCommand() {
3165
+ // Check if gh is available and logged in
3166
+ try {
3167
+ const authResult = spawnSync("gh", ["auth", "status"], { encoding: "utf-8" });
3168
+ if (authResult.status !== 0) {
3169
+ this.showError("GitHub CLI is not logged in. Run 'gh auth login' first.");
3170
+ return;
3171
+ }
3172
+ }
3173
+ catch {
3174
+ this.showError("GitHub CLI (gh) is not installed. Install it from https://cli.github.com/");
3175
+ return;
3176
+ }
3177
+ // Export to a temp file
3178
+ const tmpFile = path.join(os.tmpdir(), "session.html");
3179
+ try {
3180
+ await this.session.exportToHtml(tmpFile);
3181
+ }
3182
+ catch (error) {
3183
+ this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
3184
+ return;
3185
+ }
3186
+ // Show cancellable loader, replacing the editor
3187
+ const loader = new BorderedLoader(this.ui, theme, "Creating gist...");
3188
+ this.editorContainer.clear();
3189
+ this.editorContainer.addChild(loader);
3190
+ this.ui.setFocus(loader);
3191
+ this.ui.requestRender();
3192
+ const restoreEditor = () => {
3193
+ loader.dispose();
3194
+ this.editorContainer.clear();
3195
+ this.editorContainer.addChild(this.editor);
3196
+ this.ui.setFocus(this.editor);
3197
+ try {
3198
+ fs.unlinkSync(tmpFile);
3199
+ }
3200
+ catch {
3201
+ // Ignore cleanup errors
3202
+ }
3203
+ };
3204
+ // Create a secret gist asynchronously
3205
+ let proc = null;
3206
+ loader.onAbort = () => {
3207
+ proc?.kill();
3208
+ restoreEditor();
3209
+ this.showStatus("Share cancelled");
3210
+ };
3211
+ try {
3212
+ const result = await new Promise((resolve) => {
3213
+ proc = spawn("gh", ["gist", "create", "--public=false", tmpFile]);
3214
+ let stdout = "";
3215
+ let stderr = "";
3216
+ proc.stdout?.on("data", (data) => {
3217
+ stdout += data.toString();
3218
+ });
3219
+ proc.stderr?.on("data", (data) => {
3220
+ stderr += data.toString();
3221
+ });
3222
+ proc.on("close", (code) => resolve({ stdout, stderr, code }));
3223
+ });
3224
+ if (loader.signal.aborted)
3225
+ return;
3226
+ restoreEditor();
3227
+ if (result.code !== 0) {
3228
+ const errorMsg = result.stderr?.trim() || "Unknown error";
3229
+ this.showError(`Failed to create gist: ${errorMsg}`);
3230
+ return;
3231
+ }
3232
+ // Extract gist ID from the URL returned by gh
3233
+ // gh returns something like: https://gist.github.com/username/GIST_ID
3234
+ const gistUrl = result.stdout?.trim();
3235
+ const gistId = gistUrl?.split("/").pop();
3236
+ if (!gistId) {
3237
+ this.showError("Failed to parse gist ID from gh output");
3238
+ return;
3239
+ }
3240
+ // Create the preview URL
3241
+ const previewUrl = getShareViewerUrl(gistId);
3242
+ this.showStatus(`Share URL: ${previewUrl}\nGist: ${gistUrl}`);
3243
+ }
3244
+ catch (error) {
3245
+ if (!loader.signal.aborted) {
3246
+ restoreEditor();
3247
+ this.showError(`Failed to create gist: ${error instanceof Error ? error.message : "Unknown error"}`);
3248
+ }
3249
+ }
3250
+ }
3251
+ handleCopyCommand() {
3252
+ const text = this.session.getLastAssistantText();
3253
+ if (!text) {
3254
+ this.showError("No agent messages to copy yet.");
3255
+ return;
3256
+ }
3257
+ try {
3258
+ copyToClipboard(text);
3259
+ this.showStatus("Copied last agent message to clipboard");
3260
+ }
3261
+ catch (error) {
3262
+ this.showError(error instanceof Error ? error.message : String(error));
3263
+ }
3264
+ }
3265
+ handleNameCommand(text) {
3266
+ const name = text.replace(/^\/name\s*/, "").trim();
3267
+ if (!name) {
3268
+ const currentName = this.sessionManager.getSessionName();
3269
+ if (currentName) {
3270
+ this.chatContainer.addChild(new Spacer(1));
3271
+ this.chatContainer.addChild(new Text(theme.fg("dim", `Session name: ${currentName}`), 1, 0));
3272
+ }
3273
+ else {
3274
+ this.showWarning("Usage: /name <name>");
3275
+ }
3276
+ this.ui.requestRender();
3277
+ return;
3278
+ }
3279
+ this.sessionManager.appendSessionInfo(name);
3280
+ this.updateTerminalTitle();
3281
+ this.chatContainer.addChild(new Spacer(1));
3282
+ this.chatContainer.addChild(new Text(theme.fg("dim", `Session name set: ${name}`), 1, 0));
3283
+ this.ui.requestRender();
3284
+ }
3285
+ handleSessionCommand() {
3286
+ const stats = this.session.getSessionStats();
3287
+ const sessionName = this.sessionManager.getSessionName();
3288
+ let info = `${theme.bold("Session Info")}\n\n`;
3289
+ if (sessionName) {
3290
+ info += `${theme.fg("dim", "Name:")} ${sessionName}\n`;
3291
+ }
3292
+ info += `${theme.fg("dim", "File:")} ${stats.sessionFile ?? "In-memory"}\n`;
3293
+ info += `${theme.fg("dim", "ID:")} ${stats.sessionId}\n\n`;
3294
+ info += `${theme.bold("Messages")}\n`;
3295
+ info += `${theme.fg("dim", "User:")} ${stats.userMessages}\n`;
3296
+ info += `${theme.fg("dim", "Assistant:")} ${stats.assistantMessages}\n`;
3297
+ info += `${theme.fg("dim", "Tool Calls:")} ${stats.toolCalls}\n`;
3298
+ info += `${theme.fg("dim", "Tool Results:")} ${stats.toolResults}\n`;
3299
+ info += `${theme.fg("dim", "Total:")} ${stats.totalMessages}\n\n`;
3300
+ info += `${theme.bold("Tokens")}\n`;
3301
+ info += `${theme.fg("dim", "Input:")} ${stats.tokens.input.toLocaleString()}\n`;
3302
+ info += `${theme.fg("dim", "Output:")} ${stats.tokens.output.toLocaleString()}\n`;
3303
+ if (stats.tokens.cacheRead > 0) {
3304
+ info += `${theme.fg("dim", "Cache Read:")} ${stats.tokens.cacheRead.toLocaleString()}\n`;
3305
+ }
3306
+ if (stats.tokens.cacheWrite > 0) {
3307
+ info += `${theme.fg("dim", "Cache Write:")} ${stats.tokens.cacheWrite.toLocaleString()}\n`;
3308
+ }
3309
+ info += `${theme.fg("dim", "Total:")} ${stats.tokens.total.toLocaleString()}\n`;
3310
+ if (stats.cost > 0) {
3311
+ info += `\n${theme.bold("Cost")}\n`;
3312
+ info += `${theme.fg("dim", "Total:")} ${stats.cost.toFixed(4)}`;
3313
+ }
3314
+ this.chatContainer.addChild(new Spacer(1));
3315
+ this.chatContainer.addChild(new Text(info, 1, 0));
3316
+ this.ui.requestRender();
3317
+ }
3318
+ handleChangelogCommand() {
3319
+ const changelogPath = getChangelogPath();
3320
+ const allEntries = parseChangelog(changelogPath);
3321
+ const changelogMarkdown = allEntries.length > 0
3322
+ ? allEntries
3323
+ .reverse()
3324
+ .map((e) => e.content)
3325
+ .join("\n\n")
3326
+ : "No changelog entries found.";
3327
+ this.chatContainer.addChild(new Spacer(1));
3328
+ this.chatContainer.addChild(new DynamicBorder());
3329
+ this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
3330
+ this.chatContainer.addChild(new Spacer(1));
3331
+ this.chatContainer.addChild(new Markdown(changelogMarkdown, 1, 1, this.getMarkdownThemeWithSettings()));
3332
+ this.chatContainer.addChild(new DynamicBorder());
3333
+ this.ui.requestRender();
3334
+ }
3335
+ /**
3336
+ * Capitalize keybinding for display (e.g., "ctrl+c" -> "Ctrl+C").
3337
+ */
3338
+ capitalizeKey(key) {
3339
+ return key
3340
+ .split("/")
3341
+ .map((k) => k
3342
+ .split("+")
3343
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
3344
+ .join("+"))
3345
+ .join("/");
3346
+ }
3347
+ /**
3348
+ * Get capitalized display string for an app keybinding action.
3349
+ */
3350
+ getAppKeyDisplay(action) {
3351
+ return this.capitalizeKey(appKey(this.keybindings, action));
3352
+ }
3353
+ /**
3354
+ * Get capitalized display string for an editor keybinding action.
3355
+ */
3356
+ getEditorKeyDisplay(action) {
3357
+ return this.capitalizeKey(editorKey(action));
3358
+ }
3359
+ handleHotkeysCommand() {
3360
+ // Navigation keybindings
3361
+ const cursorWordLeft = this.getEditorKeyDisplay("cursorWordLeft");
3362
+ const cursorWordRight = this.getEditorKeyDisplay("cursorWordRight");
3363
+ const cursorLineStart = this.getEditorKeyDisplay("cursorLineStart");
3364
+ const cursorLineEnd = this.getEditorKeyDisplay("cursorLineEnd");
3365
+ const jumpForward = this.getEditorKeyDisplay("jumpForward");
3366
+ const jumpBackward = this.getEditorKeyDisplay("jumpBackward");
3367
+ const pageUp = this.getEditorKeyDisplay("pageUp");
3368
+ const pageDown = this.getEditorKeyDisplay("pageDown");
3369
+ // Editing keybindings
3370
+ const submit = this.getEditorKeyDisplay("submit");
3371
+ const newLine = this.getEditorKeyDisplay("newLine");
3372
+ const deleteWordBackward = this.getEditorKeyDisplay("deleteWordBackward");
3373
+ const deleteWordForward = this.getEditorKeyDisplay("deleteWordForward");
3374
+ const deleteToLineStart = this.getEditorKeyDisplay("deleteToLineStart");
3375
+ const deleteToLineEnd = this.getEditorKeyDisplay("deleteToLineEnd");
3376
+ const yank = this.getEditorKeyDisplay("yank");
3377
+ const yankPop = this.getEditorKeyDisplay("yankPop");
3378
+ const undo = this.getEditorKeyDisplay("undo");
3379
+ const tab = this.getEditorKeyDisplay("tab");
3380
+ // App keybindings
3381
+ const interrupt = this.getAppKeyDisplay("interrupt");
3382
+ const clear = this.getAppKeyDisplay("clear");
3383
+ const exit = this.getAppKeyDisplay("exit");
3384
+ const suspend = this.getAppKeyDisplay("suspend");
3385
+ const cycleThinkingLevel = this.getAppKeyDisplay("cycleThinkingLevel");
3386
+ const cycleModelForward = this.getAppKeyDisplay("cycleModelForward");
3387
+ const selectModel = this.getAppKeyDisplay("selectModel");
3388
+ const expandTools = this.getAppKeyDisplay("expandTools");
3389
+ const toggleThinking = this.getAppKeyDisplay("toggleThinking");
3390
+ const externalEditor = this.getAppKeyDisplay("externalEditor");
3391
+ const followUp = this.getAppKeyDisplay("followUp");
3392
+ const dequeue = this.getAppKeyDisplay("dequeue");
3393
+ let hotkeys = `
3394
+ **Navigation**
3395
+ | Key | Action |
3396
+ |-----|--------|
3397
+ | \`Arrow keys\` | Move cursor / browse history (Up when empty) |
3398
+ | \`${cursorWordLeft}\` / \`${cursorWordRight}\` | Move by word |
3399
+ | \`${cursorLineStart}\` | Start of line |
3400
+ | \`${cursorLineEnd}\` | End of line |
3401
+ | \`${jumpForward}\` | Jump forward to character |
3402
+ | \`${jumpBackward}\` | Jump backward to character |
3403
+ | \`${pageUp}\` / \`${pageDown}\` | Scroll by page |
3404
+
3405
+ **Editing**
3406
+ | Key | Action |
3407
+ |-----|--------|
3408
+ | \`${submit}\` | Send message |
3409
+ | \`${newLine}\` | New line${process.platform === "win32" ? " (Ctrl+Enter on Windows Terminal)" : ""} |
3410
+ | \`${deleteWordBackward}\` | Delete word backwards |
3411
+ | \`${deleteWordForward}\` | Delete word forwards |
3412
+ | \`${deleteToLineStart}\` | Delete to start of line |
3413
+ | \`${deleteToLineEnd}\` | Delete to end of line |
3414
+ | \`${yank}\` | Paste the most-recently-deleted text |
3415
+ | \`${yankPop}\` | Cycle through the deleted text after pasting |
3416
+ | \`${undo}\` | Undo |
3417
+
3418
+ **Other**
3419
+ | Key | Action |
3420
+ |-----|--------|
3421
+ | \`${tab}\` | Path completion / accept autocomplete |
3422
+ | \`${interrupt}\` | Cancel autocomplete / abort streaming |
3423
+ | \`${clear}\` | Clear editor (first) / exit (second) |
3424
+ | \`${exit}\` | Exit (when editor is empty) |
3425
+ | \`${suspend}\` | Suspend to background |
3426
+ | \`${cycleThinkingLevel}\` | Cycle thinking level |
3427
+ | \`${cycleModelForward}\` | Cycle models |
3428
+ | \`${selectModel}\` | Open model selector |
3429
+ | \`${expandTools}\` | Toggle tool output expansion |
3430
+ | \`${toggleThinking}\` | Toggle thinking block visibility |
3431
+ | \`${externalEditor}\` | Edit message in external editor |
3432
+ | \`${followUp}\` | Queue follow-up message |
3433
+ | \`${dequeue}\` | Restore queued messages |
3434
+ | \`Ctrl+V\` | Paste image from clipboard |
3435
+ | \`/\` | Slash commands |
3436
+ | \`!\` | Run bash command |
3437
+ | \`!!\` | Run bash command (excluded from context) |
3438
+ `;
3439
+ // Add extension-registered shortcuts
3440
+ const extensionRunner = this.session.extensionRunner;
3441
+ if (extensionRunner) {
3442
+ const shortcuts = extensionRunner.getShortcuts(this.keybindings.getEffectiveConfig());
3443
+ if (shortcuts.size > 0) {
3444
+ hotkeys += `
3445
+ **Extensions**
3446
+ | Key | Action |
3447
+ |-----|--------|
3448
+ `;
3449
+ for (const [key, shortcut] of shortcuts) {
3450
+ const description = shortcut.description ?? shortcut.extensionPath;
3451
+ const keyDisplay = key.replace(/\b\w/g, (c) => c.toUpperCase());
3452
+ hotkeys += `| \`${keyDisplay}\` | ${description} |\n`;
3453
+ }
3454
+ }
3455
+ }
3456
+ this.chatContainer.addChild(new Spacer(1));
3457
+ this.chatContainer.addChild(new DynamicBorder());
3458
+ this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "Keyboard Shortcuts")), 1, 0));
3459
+ this.chatContainer.addChild(new Spacer(1));
3460
+ this.chatContainer.addChild(new Markdown(hotkeys.trim(), 1, 1, this.getMarkdownThemeWithSettings()));
3461
+ this.chatContainer.addChild(new DynamicBorder());
3462
+ this.ui.requestRender();
3463
+ }
3464
+ async handleClearCommand() {
3465
+ // Stop loading animation
3466
+ if (this.loadingAnimation) {
3467
+ this.loadingAnimation.stop();
3468
+ this.loadingAnimation = undefined;
3469
+ }
3470
+ this.statusContainer.clear();
3471
+ // New session via session (emits extension session events)
3472
+ await this.session.newSession();
3473
+ // Clear UI state
3474
+ this.chatContainer.clear();
3475
+ this.pendingMessagesContainer.clear();
3476
+ this.compactionQueuedMessages = [];
3477
+ this.streamingComponent = undefined;
3478
+ this.streamingMessage = undefined;
3479
+ this.pendingTools.clear();
3480
+ this.chatContainer.addChild(new Spacer(1));
3481
+ this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
3482
+ this.ui.requestRender();
3483
+ }
3484
+ handleDebugCommand() {
3485
+ const width = this.ui.terminal.columns;
3486
+ const height = this.ui.terminal.rows;
3487
+ const allLines = this.ui.render(width);
3488
+ const debugLogPath = getDebugLogPath();
3489
+ const debugData = [
3490
+ `Debug output at ${new Date().toISOString()}`,
3491
+ `Terminal: ${width}x${height}`,
3492
+ `Total lines: ${allLines.length}`,
3493
+ "",
3494
+ "=== All rendered lines with visible widths ===",
3495
+ ...allLines.map((line, idx) => {
3496
+ const vw = visibleWidth(line);
3497
+ const escaped = JSON.stringify(line);
3498
+ return `[${idx}] (w=${vw}) ${escaped}`;
3499
+ }),
3500
+ "",
3501
+ "=== Agent messages (JSONL) ===",
3502
+ ...this.session.messages.map((msg) => JSON.stringify(msg)),
3503
+ "",
3504
+ ].join("\n");
3505
+ fs.mkdirSync(path.dirname(debugLogPath), { recursive: true });
3506
+ fs.writeFileSync(debugLogPath, debugData);
3507
+ this.chatContainer.addChild(new Spacer(1));
3508
+ this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ Debug log written")}\n${theme.fg("muted", debugLogPath)}`, 1, 1));
3509
+ this.ui.requestRender();
3510
+ }
3511
+ handleArminSaysHi() {
3512
+ this.chatContainer.addChild(new Spacer(1));
3513
+ this.chatContainer.addChild(new ArminComponent(this.ui));
3514
+ this.ui.requestRender();
3515
+ }
3516
+ handleDaxnuts() {
3517
+ this.chatContainer.addChild(new Spacer(1));
3518
+ this.chatContainer.addChild(new DaxnutsComponent(this.ui));
3519
+ this.ui.requestRender();
3520
+ }
3521
+ checkDaxnutsEasterEgg(model) {
3522
+ if (model.provider === "opencode" && model.id.toLowerCase().includes("kimi-k2.5")) {
3523
+ this.handleDaxnuts();
3524
+ }
3525
+ }
3526
+ async handleBashCommand(command, excludeFromContext = false) {
3527
+ const extensionRunner = this.session.extensionRunner;
3528
+ // Emit user_bash event to let extensions intercept
3529
+ const eventResult = extensionRunner
3530
+ ? await extensionRunner.emitUserBash({
3531
+ type: "user_bash",
3532
+ command,
3533
+ excludeFromContext,
3534
+ cwd: process.cwd(),
3535
+ })
3536
+ : undefined;
3537
+ // If extension returned a full result, use it directly
3538
+ if (eventResult?.result) {
3539
+ const result = eventResult.result;
3540
+ // Create UI component for display
3541
+ this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
3542
+ if (this.session.isStreaming) {
3543
+ this.pendingMessagesContainer.addChild(this.bashComponent);
3544
+ this.pendingBashComponents.push(this.bashComponent);
3545
+ }
3546
+ else {
3547
+ this.chatContainer.addChild(this.bashComponent);
3548
+ }
3549
+ // Show output and complete
3550
+ if (result.output) {
3551
+ this.bashComponent.appendOutput(result.output);
3552
+ }
3553
+ this.bashComponent.setComplete(result.exitCode, result.cancelled, result.truncated ? { truncated: true, content: result.output } : undefined, result.fullOutputPath);
3554
+ // Record the result in session
3555
+ this.session.recordBashResult(command, result, { excludeFromContext });
3556
+ this.bashComponent = undefined;
3557
+ this.ui.requestRender();
3558
+ return;
3559
+ }
3560
+ // Normal execution path (possibly with custom operations)
3561
+ const isDeferred = this.session.isStreaming;
3562
+ this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
3563
+ if (isDeferred) {
3564
+ // Show in pending area when agent is streaming
3565
+ this.pendingMessagesContainer.addChild(this.bashComponent);
3566
+ this.pendingBashComponents.push(this.bashComponent);
3567
+ }
3568
+ else {
3569
+ // Show in chat immediately when agent is idle
3570
+ this.chatContainer.addChild(this.bashComponent);
3571
+ }
3572
+ this.ui.requestRender();
3573
+ try {
3574
+ const result = await this.session.executeBash(command, (chunk) => {
3575
+ if (this.bashComponent) {
3576
+ this.bashComponent.appendOutput(chunk);
3577
+ this.ui.requestRender();
3578
+ }
3579
+ }, { excludeFromContext, operations: eventResult?.operations });
3580
+ if (this.bashComponent) {
3581
+ this.bashComponent.setComplete(result.exitCode, result.cancelled, result.truncated ? { truncated: true, content: result.output } : undefined, result.fullOutputPath);
3582
+ }
3583
+ }
3584
+ catch (error) {
3585
+ if (this.bashComponent) {
3586
+ this.bashComponent.setComplete(undefined, false);
3587
+ }
3588
+ this.showError(`Bash command failed: ${error instanceof Error ? error.message : "Unknown error"}`);
3589
+ }
3590
+ this.bashComponent = undefined;
3591
+ this.ui.requestRender();
3592
+ }
3593
+ async handleCompactCommand(customInstructions) {
3594
+ const entries = this.sessionManager.getEntries();
3595
+ const messageCount = entries.filter((e) => e.type === "message").length;
3596
+ if (messageCount < 2) {
3597
+ this.showWarning("Nothing to compact (no messages yet)");
3598
+ return;
3599
+ }
3600
+ await this.executeCompaction(customInstructions, false);
3601
+ }
3602
+ async executeCompaction(customInstructions, isAuto = false) {
3603
+ // Stop loading animation
3604
+ if (this.loadingAnimation) {
3605
+ this.loadingAnimation.stop();
3606
+ this.loadingAnimation = undefined;
3607
+ }
3608
+ this.statusContainer.clear();
3609
+ // Set up escape handler during compaction
3610
+ const originalOnEscape = this.defaultEditor.onEscape;
3611
+ this.defaultEditor.onEscape = () => {
3612
+ this.session.abortCompaction();
3613
+ };
3614
+ // Show compacting status
3615
+ this.chatContainer.addChild(new Spacer(1));
3616
+ const cancelHint = `(${appKey(this.keybindings, "interrupt")} to cancel)`;
3617
+ const label = isAuto ? `Auto-compacting context... ${cancelHint}` : `Compacting context... ${cancelHint}`;
3618
+ const compactingLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), label);
3619
+ this.statusContainer.addChild(compactingLoader);
3620
+ this.ui.requestRender();
3621
+ let result;
3622
+ try {
3623
+ result = await this.session.compact(customInstructions);
3624
+ // Rebuild UI
3625
+ this.rebuildChatFromMessages();
3626
+ // Add compaction component at bottom so user sees it without scrolling
3627
+ const msg = createCompactionSummaryMessage(result.summary, result.tokensBefore, new Date().toISOString());
3628
+ this.addMessageToChat(msg);
3629
+ this.footer.invalidate();
3630
+ }
3631
+ catch (error) {
3632
+ const message = error instanceof Error ? error.message : String(error);
3633
+ if (message === "Compaction cancelled" || (error instanceof Error && error.name === "AbortError")) {
3634
+ this.showError("Compaction cancelled");
3635
+ }
3636
+ else {
3637
+ this.showError(`Compaction failed: ${message}`);
3638
+ }
3639
+ }
3640
+ finally {
3641
+ compactingLoader.stop();
3642
+ this.statusContainer.clear();
3643
+ this.defaultEditor.onEscape = originalOnEscape;
3644
+ }
3645
+ void this.flushCompactionQueue({ willRetry: false });
3646
+ return result;
3647
+ }
3648
+ stop() {
3649
+ if (this.loadingAnimation) {
3650
+ this.loadingAnimation.stop();
3651
+ this.loadingAnimation = undefined;
3652
+ }
3653
+ this.footer.dispose();
3654
+ this.footerDataProvider.dispose();
3655
+ if (this.unsubscribe) {
3656
+ this.unsubscribe();
3657
+ }
3658
+ if (this.isInitialized) {
3659
+ this.ui.stop();
3660
+ this.isInitialized = false;
3661
+ }
3662
+ }
3663
+ }
3664
+ //# sourceMappingURL=interactive-mode.js.map