@selesai/code 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (884) hide show
  1. package/README.md +198 -0
  2. package/dist/agents/architect.md +216 -0
  3. package/dist/agents/builder.md +119 -0
  4. package/dist/agents/commentator.md +128 -0
  5. package/dist/agents/explorer.md +51 -0
  6. package/dist/agents/recapper.md +24 -0
  7. package/dist/bun/cli.d.ts +3 -0
  8. package/dist/bun/cli.d.ts.map +1 -0
  9. package/dist/bun/cli.js +9 -0
  10. package/dist/bun/cli.js.map +1 -0
  11. package/dist/bun/register-bedrock.d.ts +2 -0
  12. package/dist/bun/register-bedrock.d.ts.map +1 -0
  13. package/dist/bun/register-bedrock.js +4 -0
  14. package/dist/bun/register-bedrock.js.map +1 -0
  15. package/dist/bun/restore-sandbox-env.d.ts +17 -0
  16. package/dist/bun/restore-sandbox-env.d.ts.map +1 -0
  17. package/dist/bun/restore-sandbox-env.js +36 -0
  18. package/dist/bun/restore-sandbox-env.js.map +1 -0
  19. package/dist/cli/args.d.ts +57 -0
  20. package/dist/cli/args.d.ts.map +1 -0
  21. package/dist/cli/args.js +379 -0
  22. package/dist/cli/args.js.map +1 -0
  23. package/dist/cli/config-selector.d.ts +14 -0
  24. package/dist/cli/config-selector.d.ts.map +1 -0
  25. package/dist/cli/config-selector.js +31 -0
  26. package/dist/cli/config-selector.js.map +1 -0
  27. package/dist/cli/file-processor.d.ts +15 -0
  28. package/dist/cli/file-processor.d.ts.map +1 -0
  29. package/dist/cli/file-processor.js +82 -0
  30. package/dist/cli/file-processor.js.map +1 -0
  31. package/dist/cli/initial-message.d.ts +18 -0
  32. package/dist/cli/initial-message.d.ts.map +1 -0
  33. package/dist/cli/initial-message.js +22 -0
  34. package/dist/cli/initial-message.js.map +1 -0
  35. package/dist/cli/list-models.d.ts +9 -0
  36. package/dist/cli/list-models.d.ts.map +1 -0
  37. package/dist/cli/list-models.js +98 -0
  38. package/dist/cli/list-models.js.map +1 -0
  39. package/dist/cli/project-trust.d.ts +10 -0
  40. package/dist/cli/project-trust.d.ts.map +1 -0
  41. package/dist/cli/project-trust.js +48 -0
  42. package/dist/cli/project-trust.js.map +1 -0
  43. package/dist/cli/session-picker.d.ts +10 -0
  44. package/dist/cli/session-picker.d.ts.map +1 -0
  45. package/dist/cli/session-picker.js +36 -0
  46. package/dist/cli/session-picker.js.map +1 -0
  47. package/dist/cli/startup-ui.d.ts +23 -0
  48. package/dist/cli/startup-ui.d.ts.map +1 -0
  49. package/dist/cli/startup-ui.js +172 -0
  50. package/dist/cli/startup-ui.js.map +1 -0
  51. package/dist/cli.d.ts +3 -0
  52. package/dist/cli.d.ts.map +1 -0
  53. package/dist/cli.js +18 -0
  54. package/dist/cli.js.map +1 -0
  55. package/dist/config.d.ts +154 -0
  56. package/dist/config.d.ts.map +1 -0
  57. package/dist/config.js +579 -0
  58. package/dist/config.js.map +1 -0
  59. package/dist/core/agent-session-runtime.d.ts +119 -0
  60. package/dist/core/agent-session-runtime.d.ts.map +1 -0
  61. package/dist/core/agent-session-runtime.js +303 -0
  62. package/dist/core/agent-session-runtime.js.map +1 -0
  63. package/dist/core/agent-session-services.d.ts +88 -0
  64. package/dist/core/agent-session-services.d.ts.map +1 -0
  65. package/dist/core/agent-session-services.js +119 -0
  66. package/dist/core/agent-session-services.js.map +1 -0
  67. package/dist/core/agent-session.d.ts +607 -0
  68. package/dist/core/agent-session.d.ts.map +1 -0
  69. package/dist/core/agent-session.js +2552 -0
  70. package/dist/core/agent-session.js.map +1 -0
  71. package/dist/core/agents.d.ts +53 -0
  72. package/dist/core/agents.d.ts.map +1 -0
  73. package/dist/core/agents.js +238 -0
  74. package/dist/core/agents.js.map +1 -0
  75. package/dist/core/auth-guidance.d.ts +5 -0
  76. package/dist/core/auth-guidance.d.ts.map +1 -0
  77. package/dist/core/auth-guidance.js +21 -0
  78. package/dist/core/auth-guidance.js.map +1 -0
  79. package/dist/core/auth-storage.d.ts +140 -0
  80. package/dist/core/auth-storage.d.ts.map +1 -0
  81. package/dist/core/auth-storage.js +434 -0
  82. package/dist/core/auth-storage.js.map +1 -0
  83. package/dist/core/bash-executor.d.ts +32 -0
  84. package/dist/core/bash-executor.d.ts.map +1 -0
  85. package/dist/core/bash-executor.js +111 -0
  86. package/dist/core/bash-executor.js.map +1 -0
  87. package/dist/core/compaction/branch-summarization.d.ts +92 -0
  88. package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
  89. package/dist/core/compaction/branch-summarization.js +249 -0
  90. package/dist/core/compaction/branch-summarization.js.map +1 -0
  91. package/dist/core/compaction/compaction.d.ts +122 -0
  92. package/dist/core/compaction/compaction.d.ts.map +1 -0
  93. package/dist/core/compaction/compaction.js +625 -0
  94. package/dist/core/compaction/compaction.js.map +1 -0
  95. package/dist/core/compaction/index.d.ts +7 -0
  96. package/dist/core/compaction/index.d.ts.map +1 -0
  97. package/dist/core/compaction/index.js +7 -0
  98. package/dist/core/compaction/index.js.map +1 -0
  99. package/dist/core/compaction/utils.d.ts +38 -0
  100. package/dist/core/compaction/utils.d.ts.map +1 -0
  101. package/dist/core/compaction/utils.js +153 -0
  102. package/dist/core/compaction/utils.js.map +1 -0
  103. package/dist/core/defaults.d.ts +3 -0
  104. package/dist/core/defaults.d.ts.map +1 -0
  105. package/dist/core/defaults.js +2 -0
  106. package/dist/core/defaults.js.map +1 -0
  107. package/dist/core/diagnostics.d.ts +15 -0
  108. package/dist/core/diagnostics.d.ts.map +1 -0
  109. package/dist/core/diagnostics.js +2 -0
  110. package/dist/core/diagnostics.js.map +1 -0
  111. package/dist/core/event-bus.d.ts +9 -0
  112. package/dist/core/event-bus.d.ts.map +1 -0
  113. package/dist/core/event-bus.js +25 -0
  114. package/dist/core/event-bus.js.map +1 -0
  115. package/dist/core/exec.d.ts +29 -0
  116. package/dist/core/exec.d.ts.map +1 -0
  117. package/dist/core/exec.js +75 -0
  118. package/dist/core/exec.js.map +1 -0
  119. package/dist/core/experimental.d.ts +2 -0
  120. package/dist/core/experimental.d.ts.map +1 -0
  121. package/dist/core/experimental.js +4 -0
  122. package/dist/core/experimental.js.map +1 -0
  123. package/dist/core/export-html/ansi-to-html.d.ts +22 -0
  124. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -0
  125. package/dist/core/export-html/ansi-to-html.js +249 -0
  126. package/dist/core/export-html/ansi-to-html.js.map +1 -0
  127. package/dist/core/export-html/index.d.ts +37 -0
  128. package/dist/core/export-html/index.d.ts.map +1 -0
  129. package/dist/core/export-html/index.js +226 -0
  130. package/dist/core/export-html/index.js.map +1 -0
  131. package/dist/core/export-html/template.css +1066 -0
  132. package/dist/core/export-html/template.html +55 -0
  133. package/dist/core/export-html/template.js +1864 -0
  134. package/dist/core/export-html/tool-renderer.d.ts +34 -0
  135. package/dist/core/export-html/tool-renderer.d.ts.map +1 -0
  136. package/dist/core/export-html/tool-renderer.js +108 -0
  137. package/dist/core/export-html/tool-renderer.js.map +1 -0
  138. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  139. package/dist/core/export-html/vendor/marked.min.js +78 -0
  140. package/dist/core/extensions/index.d.ts +12 -0
  141. package/dist/core/extensions/index.d.ts.map +1 -0
  142. package/dist/core/extensions/index.js +9 -0
  143. package/dist/core/extensions/index.js.map +1 -0
  144. package/dist/core/extensions/loader.d.ts +23 -0
  145. package/dist/core/extensions/loader.d.ts.map +1 -0
  146. package/dist/core/extensions/loader.js +531 -0
  147. package/dist/core/extensions/loader.js.map +1 -0
  148. package/dist/core/extensions/runner.d.ts +166 -0
  149. package/dist/core/extensions/runner.d.ts.map +1 -0
  150. package/dist/core/extensions/runner.js +876 -0
  151. package/dist/core/extensions/runner.js.map +1 -0
  152. package/dist/core/extensions/types.d.ts +1209 -0
  153. package/dist/core/extensions/types.d.ts.map +1 -0
  154. package/dist/core/extensions/types.js +45 -0
  155. package/dist/core/extensions/types.js.map +1 -0
  156. package/dist/core/extensions/wrapper.d.ts +20 -0
  157. package/dist/core/extensions/wrapper.d.ts.map +1 -0
  158. package/dist/core/extensions/wrapper.js +22 -0
  159. package/dist/core/extensions/wrapper.js.map +1 -0
  160. package/dist/core/footer-data-provider.d.ts +54 -0
  161. package/dist/core/footer-data-provider.d.ts.map +1 -0
  162. package/dist/core/footer-data-provider.js +338 -0
  163. package/dist/core/footer-data-provider.js.map +1 -0
  164. package/dist/core/http-dispatcher.d.ts +22 -0
  165. package/dist/core/http-dispatcher.d.ts.map +1 -0
  166. package/dist/core/http-dispatcher.js +64 -0
  167. package/dist/core/http-dispatcher.js.map +1 -0
  168. package/dist/core/index.d.ts +13 -0
  169. package/dist/core/index.d.ts.map +1 -0
  170. package/dist/core/index.js +13 -0
  171. package/dist/core/index.js.map +1 -0
  172. package/dist/core/keybindings.d.ts +353 -0
  173. package/dist/core/keybindings.d.ts.map +1 -0
  174. package/dist/core/keybindings.js +295 -0
  175. package/dist/core/keybindings.js.map +1 -0
  176. package/dist/core/messages.d.ts +77 -0
  177. package/dist/core/messages.d.ts.map +1 -0
  178. package/dist/core/messages.js +123 -0
  179. package/dist/core/messages.js.map +1 -0
  180. package/dist/core/model-registry.d.ts +151 -0
  181. package/dist/core/model-registry.d.ts.map +1 -0
  182. package/dist/core/model-registry.js +750 -0
  183. package/dist/core/model-registry.js.map +1 -0
  184. package/dist/core/model-resolver.d.ts +111 -0
  185. package/dist/core/model-resolver.d.ts.map +1 -0
  186. package/dist/core/model-resolver.js +534 -0
  187. package/dist/core/model-resolver.js.map +1 -0
  188. package/dist/core/output-guard.d.ts +7 -0
  189. package/dist/core/output-guard.d.ts.map +1 -0
  190. package/dist/core/output-guard.js +89 -0
  191. package/dist/core/output-guard.js.map +1 -0
  192. package/dist/core/package-manager.d.ts +207 -0
  193. package/dist/core/package-manager.d.ts.map +1 -0
  194. package/dist/core/package-manager.js +2088 -0
  195. package/dist/core/package-manager.js.map +1 -0
  196. package/dist/core/project-trust.d.ts +15 -0
  197. package/dist/core/project-trust.d.ts.map +1 -0
  198. package/dist/core/project-trust.js +59 -0
  199. package/dist/core/project-trust.js.map +1 -0
  200. package/dist/core/prompt-templates.d.ts +53 -0
  201. package/dist/core/prompt-templates.d.ts.map +1 -0
  202. package/dist/core/prompt-templates.js +236 -0
  203. package/dist/core/prompt-templates.js.map +1 -0
  204. package/dist/core/provider-attribution.d.ts +4 -0
  205. package/dist/core/provider-attribution.d.ts.map +1 -0
  206. package/dist/core/provider-attribution.js +82 -0
  207. package/dist/core/provider-attribution.js.map +1 -0
  208. package/dist/core/provider-display-names.d.ts +2 -0
  209. package/dist/core/provider-display-names.d.ts.map +1 -0
  210. package/dist/core/provider-display-names.js +36 -0
  211. package/dist/core/provider-display-names.js.map +1 -0
  212. package/dist/core/resolve-config-value.d.ts +30 -0
  213. package/dist/core/resolve-config-value.d.ts.map +1 -0
  214. package/dist/core/resolve-config-value.js +247 -0
  215. package/dist/core/resolve-config-value.js.map +1 -0
  216. package/dist/core/resource-loader.d.ts +230 -0
  217. package/dist/core/resource-loader.d.ts.map +1 -0
  218. package/dist/core/resource-loader.js +861 -0
  219. package/dist/core/resource-loader.js.map +1 -0
  220. package/dist/core/sdk.d.ts +109 -0
  221. package/dist/core/sdk.d.ts.map +1 -0
  222. package/dist/core/sdk.js +267 -0
  223. package/dist/core/sdk.js.map +1 -0
  224. package/dist/core/session-cwd.d.ts +19 -0
  225. package/dist/core/session-cwd.d.ts.map +1 -0
  226. package/dist/core/session-cwd.js +38 -0
  227. package/dist/core/session-cwd.js.map +1 -0
  228. package/dist/core/session-manager.d.ts +332 -0
  229. package/dist/core/session-manager.d.ts.map +1 -0
  230. package/dist/core/session-manager.js +1230 -0
  231. package/dist/core/session-manager.js.map +1 -0
  232. package/dist/core/settings-manager.d.ts +286 -0
  233. package/dist/core/settings-manager.d.ts.map +1 -0
  234. package/dist/core/settings-manager.js +874 -0
  235. package/dist/core/settings-manager.js.map +1 -0
  236. package/dist/core/skills.d.ts +69 -0
  237. package/dist/core/skills.d.ts.map +1 -0
  238. package/dist/core/skills.js +387 -0
  239. package/dist/core/skills.js.map +1 -0
  240. package/dist/core/slash-commands.d.ts +14 -0
  241. package/dist/core/slash-commands.d.ts.map +1 -0
  242. package/dist/core/slash-commands.js +26 -0
  243. package/dist/core/slash-commands.js.map +1 -0
  244. package/dist/core/source-info.d.ts +18 -0
  245. package/dist/core/source-info.d.ts.map +1 -0
  246. package/dist/core/source-info.js +19 -0
  247. package/dist/core/source-info.js.map +1 -0
  248. package/dist/core/system-prompt.d.ts +31 -0
  249. package/dist/core/system-prompt.d.ts.map +1 -0
  250. package/dist/core/system-prompt.js +128 -0
  251. package/dist/core/system-prompt.js.map +1 -0
  252. package/dist/core/telemetry.d.ts +3 -0
  253. package/dist/core/telemetry.d.ts.map +1 -0
  254. package/dist/core/telemetry.js +9 -0
  255. package/dist/core/telemetry.js.map +1 -0
  256. package/dist/core/timings.d.ts +8 -0
  257. package/dist/core/timings.d.ts.map +1 -0
  258. package/dist/core/timings.js +31 -0
  259. package/dist/core/timings.js.map +1 -0
  260. package/dist/core/tools/bash.d.ts +68 -0
  261. package/dist/core/tools/bash.d.ts.map +1 -0
  262. package/dist/core/tools/bash.js +346 -0
  263. package/dist/core/tools/bash.js.map +1 -0
  264. package/dist/core/tools/edit-diff.d.ts +106 -0
  265. package/dist/core/tools/edit-diff.d.ts.map +1 -0
  266. package/dist/core/tools/edit-diff.js +424 -0
  267. package/dist/core/tools/edit-diff.js.map +1 -0
  268. package/dist/core/tools/edit.d.ts +51 -0
  269. package/dist/core/tools/edit.d.ts.map +1 -0
  270. package/dist/core/tools/edit.js +284 -0
  271. package/dist/core/tools/edit.js.map +1 -0
  272. package/dist/core/tools/file-mutation-queue.d.ts +6 -0
  273. package/dist/core/tools/file-mutation-queue.d.ts.map +1 -0
  274. package/dist/core/tools/file-mutation-queue.js +52 -0
  275. package/dist/core/tools/file-mutation-queue.js.map +1 -0
  276. package/dist/core/tools/find.d.ts +35 -0
  277. package/dist/core/tools/find.d.ts.map +1 -0
  278. package/dist/core/tools/find.js +305 -0
  279. package/dist/core/tools/find.js.map +1 -0
  280. package/dist/core/tools/grep.d.ts +37 -0
  281. package/dist/core/tools/grep.d.ts.map +1 -0
  282. package/dist/core/tools/grep.js +304 -0
  283. package/dist/core/tools/grep.js.map +1 -0
  284. package/dist/core/tools/index.d.ts +40 -0
  285. package/dist/core/tools/index.d.ts.map +1 -0
  286. package/dist/core/tools/index.js +112 -0
  287. package/dist/core/tools/index.js.map +1 -0
  288. package/dist/core/tools/ls.d.ts +37 -0
  289. package/dist/core/tools/ls.d.ts.map +1 -0
  290. package/dist/core/tools/ls.js +167 -0
  291. package/dist/core/tools/ls.js.map +1 -0
  292. package/dist/core/tools/output-accumulator.d.ts +52 -0
  293. package/dist/core/tools/output-accumulator.d.ts.map +1 -0
  294. package/dist/core/tools/output-accumulator.js +184 -0
  295. package/dist/core/tools/output-accumulator.js.map +1 -0
  296. package/dist/core/tools/path-utils.d.ts +10 -0
  297. package/dist/core/tools/path-utils.d.ts.map +1 -0
  298. package/dist/core/tools/path-utils.js +99 -0
  299. package/dist/core/tools/path-utils.js.map +1 -0
  300. package/dist/core/tools/read.d.ts +35 -0
  301. package/dist/core/tools/read.d.ts.map +1 -0
  302. package/dist/core/tools/read.js +289 -0
  303. package/dist/core/tools/read.js.map +1 -0
  304. package/dist/core/tools/render-utils.d.ts +24 -0
  305. package/dist/core/tools/render-utils.d.ts.map +1 -0
  306. package/dist/core/tools/render-utils.js +65 -0
  307. package/dist/core/tools/render-utils.js.map +1 -0
  308. package/dist/core/tools/tool-definition-wrapper.d.ts +14 -0
  309. package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -0
  310. package/dist/core/tools/tool-definition-wrapper.js +34 -0
  311. package/dist/core/tools/tool-definition-wrapper.js.map +1 -0
  312. package/dist/core/tools/truncate.d.ts +70 -0
  313. package/dist/core/tools/truncate.d.ts.map +1 -0
  314. package/dist/core/tools/truncate.js +215 -0
  315. package/dist/core/tools/truncate.js.map +1 -0
  316. package/dist/core/tools/write.d.ts +26 -0
  317. package/dist/core/tools/write.d.ts.map +1 -0
  318. package/dist/core/tools/write.js +197 -0
  319. package/dist/core/tools/write.js.map +1 -0
  320. package/dist/core/trust-manager.d.ts +36 -0
  321. package/dist/core/trust-manager.d.ts.map +1 -0
  322. package/dist/core/trust-manager.js +202 -0
  323. package/dist/core/trust-manager.js.map +1 -0
  324. package/dist/defaults/models.json +3 -0
  325. package/dist/defaults/settings.json +68 -0
  326. package/dist/extensions/copy-turn.ts +125 -0
  327. package/dist/extensions/gitignore-guard.ts +132 -0
  328. package/dist/extensions/hooks/claude-codex-hooks.json +44 -0
  329. package/dist/extensions/hooks/copilot-hooks.json +21 -0
  330. package/dist/extensions/hooks/ponytail-activate.js +91 -0
  331. package/dist/extensions/hooks/ponytail-config.js +122 -0
  332. package/dist/extensions/hooks/ponytail-instructions.js +94 -0
  333. package/dist/extensions/hooks/ponytail-mode-tracker.js +55 -0
  334. package/dist/extensions/hooks/ponytail-runtime.js +68 -0
  335. package/dist/extensions/hooks/ponytail-statusline.ps1 +21 -0
  336. package/dist/extensions/hooks/ponytail-statusline.sh +12 -0
  337. package/dist/extensions/hooks/ponytail-subagent.js +22 -0
  338. package/dist/extensions/package.json +19 -0
  339. package/dist/extensions/pi-extension/index.js +189 -0
  340. package/dist/extensions/pi-extension/package.json +8 -0
  341. package/dist/extensions/pi-extension/test/extension.test.js +167 -0
  342. package/dist/extensions/pi-extension/test/helpers.test.js +92 -0
  343. package/dist/extensions/pi-powerline-footer/CHANGELOG.md +516 -0
  344. package/dist/extensions/pi-powerline-footer/README.md +382 -0
  345. package/dist/extensions/pi-powerline-footer/banner.png +0 -0
  346. package/dist/extensions/pi-powerline-footer/bash-mode/completion.ts +556 -0
  347. package/dist/extensions/pi-powerline-footer/bash-mode/editor.ts +397 -0
  348. package/dist/extensions/pi-powerline-footer/bash-mode/history.ts +151 -0
  349. package/dist/extensions/pi-powerline-footer/bash-mode/shell-session.ts +286 -0
  350. package/dist/extensions/pi-powerline-footer/bash-mode/transcript.ts +108 -0
  351. package/dist/extensions/pi-powerline-footer/bash-mode/types.ts +59 -0
  352. package/dist/extensions/pi-powerline-footer/colors.ts +69 -0
  353. package/dist/extensions/pi-powerline-footer/context-usage.ts +41 -0
  354. package/dist/extensions/pi-powerline-footer/fixed-editor/cluster.ts +113 -0
  355. package/dist/extensions/pi-powerline-footer/fixed-editor/terminal-split.ts +1077 -0
  356. package/dist/extensions/pi-powerline-footer/git-status.ts +212 -0
  357. package/dist/extensions/pi-powerline-footer/icons.ts +181 -0
  358. package/dist/extensions/pi-powerline-footer/index.ts +2817 -0
  359. package/dist/extensions/pi-powerline-footer/package.json +46 -0
  360. package/dist/extensions/pi-powerline-footer/powerline-config.ts +182 -0
  361. package/dist/extensions/pi-powerline-footer/presets.ts +121 -0
  362. package/dist/extensions/pi-powerline-footer/render-scheduler.ts +24 -0
  363. package/dist/extensions/pi-powerline-footer/segments.ts +566 -0
  364. package/dist/extensions/pi-powerline-footer/separators.ts +57 -0
  365. package/dist/extensions/pi-powerline-footer/shortcuts.ts +47 -0
  366. package/dist/extensions/pi-powerline-footer/tests/bash-mode.test.ts +1503 -0
  367. package/dist/extensions/pi-powerline-footer/tests/context-usage.test.ts +38 -0
  368. package/dist/extensions/pi-powerline-footer/tests/custom-items.test.ts +135 -0
  369. package/dist/extensions/pi-powerline-footer/tests/editor-responsiveness.test.ts +180 -0
  370. package/dist/extensions/pi-powerline-footer/tests/fixed-editor.test.ts +1416 -0
  371. package/dist/extensions/pi-powerline-footer/tests/jump-shortcuts.test.ts +213 -0
  372. package/dist/extensions/pi-powerline-footer/tests/stash-shortcut.test.ts +32 -0
  373. package/dist/extensions/pi-powerline-footer/tests/thinking-segment.test.ts +61 -0
  374. package/dist/extensions/pi-powerline-footer/tests/working-vibes.test.ts +226 -0
  375. package/dist/extensions/pi-powerline-footer/theme.example.json +24 -0
  376. package/dist/extensions/pi-powerline-footer/theme.json +12 -0
  377. package/dist/extensions/pi-powerline-footer/theme.ts +227 -0
  378. package/dist/extensions/pi-powerline-footer/types.ts +191 -0
  379. package/dist/extensions/pi-powerline-footer/welcome-dismiss.ts +34 -0
  380. package/dist/extensions/pi-powerline-footer/welcome.ts +611 -0
  381. package/dist/extensions/pi-powerline-footer/working-vibes.ts +695 -0
  382. package/dist/extensions/prototype.ts +713 -0
  383. package/dist/extensions/question.ts +350 -0
  384. package/dist/extensions/rtk.ts +81 -0
  385. package/dist/extensions/tps-tracker.ts +280 -0
  386. package/dist/extensions/undo.ts +292 -0
  387. package/dist/index.d.ts +33 -0
  388. package/dist/index.d.ts.map +1 -0
  389. package/dist/index.js +46 -0
  390. package/dist/index.js.map +1 -0
  391. package/dist/main.d.ts +12 -0
  392. package/dist/main.d.ts.map +1 -0
  393. package/dist/main.js +700 -0
  394. package/dist/main.js.map +1 -0
  395. package/dist/migrations.d.ts +33 -0
  396. package/dist/migrations.d.ts.map +1 -0
  397. package/dist/migrations.js +281 -0
  398. package/dist/migrations.js.map +1 -0
  399. package/dist/modes/index.d.ts +9 -0
  400. package/dist/modes/index.d.ts.map +1 -0
  401. package/dist/modes/index.js +8 -0
  402. package/dist/modes/index.js.map +1 -0
  403. package/dist/modes/interactive/assets/clankolas.png +0 -0
  404. package/dist/modes/interactive/components/armin.d.ts +34 -0
  405. package/dist/modes/interactive/components/armin.d.ts.map +1 -0
  406. package/dist/modes/interactive/components/armin.js +333 -0
  407. package/dist/modes/interactive/components/armin.js.map +1 -0
  408. package/dist/modes/interactive/components/assistant-message.d.ts +20 -0
  409. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
  410. package/dist/modes/interactive/components/assistant-message.js +121 -0
  411. package/dist/modes/interactive/components/assistant-message.js.map +1 -0
  412. package/dist/modes/interactive/components/bash-execution.d.ts +34 -0
  413. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -0
  414. package/dist/modes/interactive/components/bash-execution.js +175 -0
  415. package/dist/modes/interactive/components/bash-execution.js.map +1 -0
  416. package/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
  417. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
  418. package/dist/modes/interactive/components/bordered-loader.js +54 -0
  419. package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
  420. package/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
  421. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
  422. package/dist/modes/interactive/components/branch-summary-message.js +44 -0
  423. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
  424. package/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
  425. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
  426. package/dist/modes/interactive/components/compaction-summary-message.js +45 -0
  427. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -0
  428. package/dist/modes/interactive/components/config-selector.d.ts +71 -0
  429. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
  430. package/dist/modes/interactive/components/config-selector.js +506 -0
  431. package/dist/modes/interactive/components/config-selector.js.map +1 -0
  432. package/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
  433. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -0
  434. package/dist/modes/interactive/components/countdown-timer.js +33 -0
  435. package/dist/modes/interactive/components/countdown-timer.js.map +1 -0
  436. package/dist/modes/interactive/components/custom-editor.d.ts +21 -0
  437. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -0
  438. package/dist/modes/interactive/components/custom-editor.js +70 -0
  439. package/dist/modes/interactive/components/custom-editor.js.map +1 -0
  440. package/dist/modes/interactive/components/custom-message.d.ts +20 -0
  441. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
  442. package/dist/modes/interactive/components/custom-message.js +79 -0
  443. package/dist/modes/interactive/components/custom-message.js.map +1 -0
  444. package/dist/modes/interactive/components/daxnuts.d.ts +23 -0
  445. package/dist/modes/interactive/components/daxnuts.d.ts.map +1 -0
  446. package/dist/modes/interactive/components/daxnuts.js +140 -0
  447. package/dist/modes/interactive/components/daxnuts.js.map +1 -0
  448. package/dist/modes/interactive/components/diff.d.ts +12 -0
  449. package/dist/modes/interactive/components/diff.d.ts.map +1 -0
  450. package/dist/modes/interactive/components/diff.js +133 -0
  451. package/dist/modes/interactive/components/diff.js.map +1 -0
  452. package/dist/modes/interactive/components/dynamic-border.d.ts +15 -0
  453. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -0
  454. package/dist/modes/interactive/components/dynamic-border.js +21 -0
  455. package/dist/modes/interactive/components/dynamic-border.js.map +1 -0
  456. package/dist/modes/interactive/components/earendil-announcement.d.ts +5 -0
  457. package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -0
  458. package/dist/modes/interactive/components/earendil-announcement.js +40 -0
  459. package/dist/modes/interactive/components/earendil-announcement.js.map +1 -0
  460. package/dist/modes/interactive/components/extension-editor.d.ts +20 -0
  461. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
  462. package/dist/modes/interactive/components/extension-editor.js +119 -0
  463. package/dist/modes/interactive/components/extension-editor.js.map +1 -0
  464. package/dist/modes/interactive/components/extension-input.d.ts +23 -0
  465. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
  466. package/dist/modes/interactive/components/extension-input.js +61 -0
  467. package/dist/modes/interactive/components/extension-input.js.map +1 -0
  468. package/dist/modes/interactive/components/extension-selector.d.ts +26 -0
  469. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -0
  470. package/dist/modes/interactive/components/extension-selector.js +83 -0
  471. package/dist/modes/interactive/components/extension-selector.js.map +1 -0
  472. package/dist/modes/interactive/components/first-time-setup.d.ts +25 -0
  473. package/dist/modes/interactive/components/first-time-setup.d.ts.map +1 -0
  474. package/dist/modes/interactive/components/first-time-setup.js +103 -0
  475. package/dist/modes/interactive/components/first-time-setup.js.map +1 -0
  476. package/dist/modes/interactive/components/footer.d.ts +28 -0
  477. package/dist/modes/interactive/components/footer.d.ts.map +1 -0
  478. package/dist/modes/interactive/components/footer.js +221 -0
  479. package/dist/modes/interactive/components/footer.js.map +1 -0
  480. package/dist/modes/interactive/components/index.d.ts +34 -0
  481. package/dist/modes/interactive/components/index.d.ts.map +1 -0
  482. package/dist/modes/interactive/components/index.js +35 -0
  483. package/dist/modes/interactive/components/index.js.map +1 -0
  484. package/dist/modes/interactive/components/keybinding-hints.d.ts +13 -0
  485. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -0
  486. package/dist/modes/interactive/components/keybinding-hints.js +36 -0
  487. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -0
  488. package/dist/modes/interactive/components/login-dialog.d.ts +52 -0
  489. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -0
  490. package/dist/modes/interactive/components/login-dialog.js +179 -0
  491. package/dist/modes/interactive/components/login-dialog.js.map +1 -0
  492. package/dist/modes/interactive/components/model-selector.d.ts +47 -0
  493. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
  494. package/dist/modes/interactive/components/model-selector.js +279 -0
  495. package/dist/modes/interactive/components/model-selector.js.map +1 -0
  496. package/dist/modes/interactive/components/oauth-selector.d.ts +31 -0
  497. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -0
  498. package/dist/modes/interactive/components/oauth-selector.js +165 -0
  499. package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
  500. package/dist/modes/interactive/components/scoped-models-selector.d.ts +42 -0
  501. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
  502. package/dist/modes/interactive/components/scoped-models-selector.js +293 -0
  503. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
  504. package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
  505. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
  506. package/dist/modes/interactive/components/session-selector-search.js +155 -0
  507. package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
  508. package/dist/modes/interactive/components/session-selector.d.ts +95 -0
  509. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
  510. package/dist/modes/interactive/components/session-selector.js +867 -0
  511. package/dist/modes/interactive/components/session-selector.js.map +1 -0
  512. package/dist/modes/interactive/components/settings-selector.d.ts +73 -0
  513. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
  514. package/dist/modes/interactive/components/settings-selector.js +570 -0
  515. package/dist/modes/interactive/components/settings-selector.js.map +1 -0
  516. package/dist/modes/interactive/components/show-images-selector.d.ts +10 -0
  517. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -0
  518. package/dist/modes/interactive/components/show-images-selector.js +39 -0
  519. package/dist/modes/interactive/components/show-images-selector.js.map +1 -0
  520. package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
  521. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
  522. package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
  523. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
  524. package/dist/modes/interactive/components/theme-selector.d.ts +11 -0
  525. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -0
  526. package/dist/modes/interactive/components/theme-selector.js +50 -0
  527. package/dist/modes/interactive/components/theme-selector.js.map +1 -0
  528. package/dist/modes/interactive/components/thinking-selector.d.ts +11 -0
  529. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -0
  530. package/dist/modes/interactive/components/thinking-selector.js +51 -0
  531. package/dist/modes/interactive/components/thinking-selector.js.map +1 -0
  532. package/dist/modes/interactive/components/tool-execution.d.ts +63 -0
  533. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -0
  534. package/dist/modes/interactive/components/tool-execution.js +317 -0
  535. package/dist/modes/interactive/components/tool-execution.js.map +1 -0
  536. package/dist/modes/interactive/components/tree-selector.d.ts +89 -0
  537. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
  538. package/dist/modes/interactive/components/tree-selector.js +1208 -0
  539. package/dist/modes/interactive/components/tree-selector.js.map +1 -0
  540. package/dist/modes/interactive/components/trust-selector.d.ts +23 -0
  541. package/dist/modes/interactive/components/trust-selector.d.ts.map +1 -0
  542. package/dist/modes/interactive/components/trust-selector.js +91 -0
  543. package/dist/modes/interactive/components/trust-selector.js.map +1 -0
  544. package/dist/modes/interactive/components/user-message-selector.d.ts +30 -0
  545. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -0
  546. package/dist/modes/interactive/components/user-message-selector.js +114 -0
  547. package/dist/modes/interactive/components/user-message-selector.js.map +1 -0
  548. package/dist/modes/interactive/components/user-message.d.ts +10 -0
  549. package/dist/modes/interactive/components/user-message.d.ts.map +1 -0
  550. package/dist/modes/interactive/components/user-message.js +29 -0
  551. package/dist/modes/interactive/components/user-message.js.map +1 -0
  552. package/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
  553. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -0
  554. package/dist/modes/interactive/components/visual-truncate.js +33 -0
  555. package/dist/modes/interactive/components/visual-truncate.js.map +1 -0
  556. package/dist/modes/interactive/interactive-mode.d.ts +381 -0
  557. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
  558. package/dist/modes/interactive/interactive-mode.js +4802 -0
  559. package/dist/modes/interactive/interactive-mode.js.map +1 -0
  560. package/dist/modes/interactive/model-search.d.ts +12 -0
  561. package/dist/modes/interactive/model-search.d.ts.map +1 -0
  562. package/dist/modes/interactive/model-search.js +15 -0
  563. package/dist/modes/interactive/model-search.js.map +1 -0
  564. package/dist/modes/interactive/theme/dark.json +86 -0
  565. package/dist/modes/interactive/theme/light.json +85 -0
  566. package/dist/modes/interactive/theme/theme-controller.d.ts +29 -0
  567. package/dist/modes/interactive/theme/theme-controller.d.ts.map +1 -0
  568. package/dist/modes/interactive/theme/theme-controller.js +102 -0
  569. package/dist/modes/interactive/theme/theme-controller.js.map +1 -0
  570. package/dist/modes/interactive/theme/theme-schema.json +336 -0
  571. package/dist/modes/interactive/theme/theme.d.ts +119 -0
  572. package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
  573. package/dist/modes/interactive/theme/theme.js +1056 -0
  574. package/dist/modes/interactive/theme/theme.js.map +1 -0
  575. package/dist/modes/print-mode.d.ts +28 -0
  576. package/dist/modes/print-mode.d.ts.map +1 -0
  577. package/dist/modes/print-mode.js +132 -0
  578. package/dist/modes/print-mode.js.map +1 -0
  579. package/dist/modes/rpc/jsonl.d.ts +17 -0
  580. package/dist/modes/rpc/jsonl.d.ts.map +1 -0
  581. package/dist/modes/rpc/jsonl.js +49 -0
  582. package/dist/modes/rpc/jsonl.js.map +1 -0
  583. package/dist/modes/rpc/rpc-client.d.ts +227 -0
  584. package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
  585. package/dist/modes/rpc/rpc-client.js +467 -0
  586. package/dist/modes/rpc/rpc-client.js.map +1 -0
  587. package/dist/modes/rpc/rpc-mode.d.ts +20 -0
  588. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -0
  589. package/dist/modes/rpc/rpc-mode.js +637 -0
  590. package/dist/modes/rpc/rpc-mode.js.map +1 -0
  591. package/dist/modes/rpc/rpc-types.d.ts +428 -0
  592. package/dist/modes/rpc/rpc-types.d.ts.map +1 -0
  593. package/dist/modes/rpc/rpc-types.js +8 -0
  594. package/dist/modes/rpc/rpc-types.js.map +1 -0
  595. package/dist/package-manager-cli.d.ts +8 -0
  596. package/dist/package-manager-cli.d.ts.map +1 -0
  597. package/dist/package-manager-cli.js +659 -0
  598. package/dist/package-manager-cli.js.map +1 -0
  599. package/dist/skills/grill-me/SKILL.md +10 -0
  600. package/dist/skills/handoff/SKILL.md +15 -0
  601. package/dist/skills/implanger/SKILL.md +68 -0
  602. package/dist/skills/improve-codebase/REFERENCE.md +78 -0
  603. package/dist/skills/improve-codebase/SKILL.md +178 -0
  604. package/dist/skills/planger/SKILL.md +165 -0
  605. package/dist/skills/ponytail/SKILL.md +117 -0
  606. package/dist/skills/ponytail-audit/SKILL.md +41 -0
  607. package/dist/skills/ponytail-debt/SKILL.md +44 -0
  608. package/dist/skills/ponytail-gain/SKILL.md +50 -0
  609. package/dist/skills/ponytail-help/SKILL.md +69 -0
  610. package/dist/skills/ponytail-review/SKILL.md +57 -0
  611. package/dist/skills/selesai-default/SKILL.md +16 -0
  612. package/dist/themes/powerline-footer/theme.json +33 -0
  613. package/dist/utils/ansi.d.ts +2 -0
  614. package/dist/utils/ansi.d.ts.map +1 -0
  615. package/dist/utils/ansi.js +52 -0
  616. package/dist/utils/ansi.js.map +1 -0
  617. package/dist/utils/changelog.d.ts +22 -0
  618. package/dist/utils/changelog.d.ts.map +1 -0
  619. package/dist/utils/changelog.js +165 -0
  620. package/dist/utils/changelog.js.map +1 -0
  621. package/dist/utils/child-process.d.ts +18 -0
  622. package/dist/utils/child-process.d.ts.map +1 -0
  623. package/dist/utils/child-process.js +106 -0
  624. package/dist/utils/child-process.js.map +1 -0
  625. package/dist/utils/clipboard-image.d.ts +11 -0
  626. package/dist/utils/clipboard-image.d.ts.map +1 -0
  627. package/dist/utils/clipboard-image.js +245 -0
  628. package/dist/utils/clipboard-image.js.map +1 -0
  629. package/dist/utils/clipboard-native.d.ts +10 -0
  630. package/dist/utils/clipboard-native.d.ts.map +1 -0
  631. package/dist/utils/clipboard-native.js +20 -0
  632. package/dist/utils/clipboard-native.js.map +1 -0
  633. package/dist/utils/clipboard.d.ts +2 -0
  634. package/dist/utils/clipboard.d.ts.map +1 -0
  635. package/dist/utils/clipboard.js +117 -0
  636. package/dist/utils/clipboard.js.map +1 -0
  637. package/dist/utils/deprecation.d.ts +4 -0
  638. package/dist/utils/deprecation.d.ts.map +1 -0
  639. package/dist/utils/deprecation.js +13 -0
  640. package/dist/utils/deprecation.js.map +1 -0
  641. package/dist/utils/exif-orientation.d.ts +5 -0
  642. package/dist/utils/exif-orientation.d.ts.map +1 -0
  643. package/dist/utils/exif-orientation.js +158 -0
  644. package/dist/utils/exif-orientation.js.map +1 -0
  645. package/dist/utils/frontmatter.d.ts +8 -0
  646. package/dist/utils/frontmatter.d.ts.map +1 -0
  647. package/dist/utils/frontmatter.js +26 -0
  648. package/dist/utils/frontmatter.js.map +1 -0
  649. package/dist/utils/fs-watch.d.ts +5 -0
  650. package/dist/utils/fs-watch.d.ts.map +1 -0
  651. package/dist/utils/fs-watch.js +25 -0
  652. package/dist/utils/fs-watch.js.map +1 -0
  653. package/dist/utils/git.d.ts +26 -0
  654. package/dist/utils/git.d.ts.map +1 -0
  655. package/dist/utils/git.js +195 -0
  656. package/dist/utils/git.js.map +1 -0
  657. package/dist/utils/html.d.ts +7 -0
  658. package/dist/utils/html.d.ts.map +1 -0
  659. package/dist/utils/html.js +40 -0
  660. package/dist/utils/html.js.map +1 -0
  661. package/dist/utils/image-convert.d.ts +9 -0
  662. package/dist/utils/image-convert.d.ts.map +1 -0
  663. package/dist/utils/image-convert.js +39 -0
  664. package/dist/utils/image-convert.js.map +1 -0
  665. package/dist/utils/image-resize-core.d.ts +30 -0
  666. package/dist/utils/image-resize-core.d.ts.map +1 -0
  667. package/dist/utils/image-resize-core.js +124 -0
  668. package/dist/utils/image-resize-core.js.map +1 -0
  669. package/dist/utils/image-resize-worker.d.ts +2 -0
  670. package/dist/utils/image-resize-worker.d.ts.map +1 -0
  671. package/dist/utils/image-resize-worker.js +31 -0
  672. package/dist/utils/image-resize-worker.js.map +1 -0
  673. package/dist/utils/image-resize.d.ts +16 -0
  674. package/dist/utils/image-resize.d.ts.map +1 -0
  675. package/dist/utils/image-resize.js +97 -0
  676. package/dist/utils/image-resize.js.map +1 -0
  677. package/dist/utils/json.d.ts +3 -0
  678. package/dist/utils/json.d.ts.map +1 -0
  679. package/dist/utils/json.js +7 -0
  680. package/dist/utils/json.js.map +1 -0
  681. package/dist/utils/mime.d.ts +3 -0
  682. package/dist/utils/mime.d.ts.map +1 -0
  683. package/dist/utils/mime.js +69 -0
  684. package/dist/utils/mime.js.map +1 -0
  685. package/dist/utils/open-browser.d.ts +9 -0
  686. package/dist/utils/open-browser.d.ts.map +1 -0
  687. package/dist/utils/open-browser.js +22 -0
  688. package/dist/utils/open-browser.js.map +1 -0
  689. package/dist/utils/paths.d.ts +31 -0
  690. package/dist/utils/paths.d.ts.map +1 -0
  691. package/dist/utils/paths.js +92 -0
  692. package/dist/utils/paths.js.map +1 -0
  693. package/dist/utils/photon.d.ts +21 -0
  694. package/dist/utils/photon.d.ts.map +1 -0
  695. package/dist/utils/photon.js +121 -0
  696. package/dist/utils/photon.js.map +1 -0
  697. package/dist/utils/pi-user-agent.d.ts +2 -0
  698. package/dist/utils/pi-user-agent.d.ts.map +1 -0
  699. package/dist/utils/pi-user-agent.js +5 -0
  700. package/dist/utils/pi-user-agent.js.map +1 -0
  701. package/dist/utils/shell.d.ts +31 -0
  702. package/dist/utils/shell.d.ts.map +1 -0
  703. package/dist/utils/shell.js +202 -0
  704. package/dist/utils/shell.js.map +1 -0
  705. package/dist/utils/sleep.d.ts +5 -0
  706. package/dist/utils/sleep.d.ts.map +1 -0
  707. package/dist/utils/sleep.js +17 -0
  708. package/dist/utils/sleep.js.map +1 -0
  709. package/dist/utils/syntax-highlight.d.ts +12 -0
  710. package/dist/utils/syntax-highlight.d.ts.map +1 -0
  711. package/dist/utils/syntax-highlight.js +118 -0
  712. package/dist/utils/syntax-highlight.js.map +1 -0
  713. package/dist/utils/tools-manager.d.ts +3 -0
  714. package/dist/utils/tools-manager.d.ts.map +1 -0
  715. package/dist/utils/tools-manager.js +328 -0
  716. package/dist/utils/tools-manager.js.map +1 -0
  717. package/dist/utils/version-check.d.ts +15 -0
  718. package/dist/utils/version-check.d.ts.map +1 -0
  719. package/dist/utils/version-check.js +52 -0
  720. package/dist/utils/version-check.js.map +1 -0
  721. package/dist/utils/windows-self-update.d.ts +3 -0
  722. package/dist/utils/windows-self-update.d.ts.map +1 -0
  723. package/dist/utils/windows-self-update.js +77 -0
  724. package/dist/utils/windows-self-update.js.map +1 -0
  725. package/docs/compaction.md +396 -0
  726. package/docs/containerization.md +111 -0
  727. package/docs/custom-provider.md +737 -0
  728. package/docs/development.md +71 -0
  729. package/docs/docs.json +156 -0
  730. package/docs/extensions.md +2681 -0
  731. package/docs/images/doom-extension.png +0 -0
  732. package/docs/images/exy.png +0 -0
  733. package/docs/images/interactive-mode.png +0 -0
  734. package/docs/images/tree-view.png +0 -0
  735. package/docs/index.md +82 -0
  736. package/docs/json.md +82 -0
  737. package/docs/keybindings.md +197 -0
  738. package/docs/models.md +495 -0
  739. package/docs/packages.md +227 -0
  740. package/docs/prompt-templates.md +95 -0
  741. package/docs/providers.md +274 -0
  742. package/docs/quickstart.md +165 -0
  743. package/docs/rpc.md +1412 -0
  744. package/docs/sdk.md +1143 -0
  745. package/docs/security.md +59 -0
  746. package/docs/session-format.md +412 -0
  747. package/docs/sessions.md +145 -0
  748. package/docs/settings.md +308 -0
  749. package/docs/shell-aliases.md +13 -0
  750. package/docs/skills.md +231 -0
  751. package/docs/terminal-setup.md +142 -0
  752. package/docs/termux.md +127 -0
  753. package/docs/themes.md +295 -0
  754. package/docs/tmux.md +63 -0
  755. package/docs/tui.md +927 -0
  756. package/docs/usage.md +308 -0
  757. package/docs/windows.md +17 -0
  758. package/examples/README.md +25 -0
  759. package/examples/extensions/README.md +211 -0
  760. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  761. package/examples/extensions/bash-spawn-hook.ts +30 -0
  762. package/examples/extensions/bookmark.ts +50 -0
  763. package/examples/extensions/border-status-editor.ts +150 -0
  764. package/examples/extensions/built-in-tool-renderer.ts +249 -0
  765. package/examples/extensions/claude-rules.ts +86 -0
  766. package/examples/extensions/commands.ts +72 -0
  767. package/examples/extensions/confirm-destructive.ts +59 -0
  768. package/examples/extensions/custom-compaction.ts +127 -0
  769. package/examples/extensions/custom-footer.ts +64 -0
  770. package/examples/extensions/custom-header.ts +73 -0
  771. package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
  772. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  773. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  774. package/examples/extensions/custom-provider-gitlab-duo/index.ts +404 -0
  775. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  776. package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
  777. package/examples/extensions/dirty-repo-guard.ts +56 -0
  778. package/examples/extensions/doom-overlay/README.md +46 -0
  779. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  780. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  781. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  782. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  783. package/examples/extensions/doom-overlay/doom-component.ts +132 -0
  784. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  785. package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
  786. package/examples/extensions/doom-overlay/index.ts +74 -0
  787. package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
  788. package/examples/extensions/dynamic-resources/SKILL.md +8 -0
  789. package/examples/extensions/dynamic-resources/dynamic.json +79 -0
  790. package/examples/extensions/dynamic-resources/dynamic.md +5 -0
  791. package/examples/extensions/dynamic-resources/index.ts +15 -0
  792. package/examples/extensions/dynamic-tools.ts +74 -0
  793. package/examples/extensions/event-bus.ts +43 -0
  794. package/examples/extensions/file-trigger.ts +41 -0
  795. package/examples/extensions/git-checkpoint.ts +53 -0
  796. package/examples/extensions/git-merge-and-resolve.ts +115 -0
  797. package/examples/extensions/github-issue-autocomplete.ts +185 -0
  798. package/examples/extensions/gondolin/index.ts +531 -0
  799. package/examples/extensions/gondolin/package-lock.json +185 -0
  800. package/examples/extensions/gondolin/package.json +19 -0
  801. package/examples/extensions/handoff.ts +191 -0
  802. package/examples/extensions/hello.ts +26 -0
  803. package/examples/extensions/hidden-thinking-label.ts +53 -0
  804. package/examples/extensions/inline-bash.ts +94 -0
  805. package/examples/extensions/input-transform-streaming.ts +39 -0
  806. package/examples/extensions/input-transform.ts +43 -0
  807. package/examples/extensions/interactive-shell.ts +196 -0
  808. package/examples/extensions/mac-system-theme.ts +47 -0
  809. package/examples/extensions/message-renderer.ts +59 -0
  810. package/examples/extensions/minimal-mode.ts +426 -0
  811. package/examples/extensions/modal-editor.ts +85 -0
  812. package/examples/extensions/model-status.ts +31 -0
  813. package/examples/extensions/notify.ts +55 -0
  814. package/examples/extensions/overlay-qa-tests.ts +1450 -0
  815. package/examples/extensions/overlay-test.ts +153 -0
  816. package/examples/extensions/permission-gate.ts +34 -0
  817. package/examples/extensions/pirate.ts +47 -0
  818. package/examples/extensions/plan-mode/README.md +66 -0
  819. package/examples/extensions/plan-mode/index.ts +390 -0
  820. package/examples/extensions/plan-mode/utils.ts +168 -0
  821. package/examples/extensions/preset.ts +436 -0
  822. package/examples/extensions/project-trust.ts +64 -0
  823. package/examples/extensions/prompt-customizer.ts +97 -0
  824. package/examples/extensions/protected-paths.ts +30 -0
  825. package/examples/extensions/provider-payload.ts +18 -0
  826. package/examples/extensions/qna.ts +122 -0
  827. package/examples/extensions/question.ts +285 -0
  828. package/examples/extensions/questionnaire.ts +448 -0
  829. package/examples/extensions/rainbow-editor.ts +88 -0
  830. package/examples/extensions/reload-runtime.ts +37 -0
  831. package/examples/extensions/rpc-demo.ts +118 -0
  832. package/examples/extensions/sandbox/index.ts +321 -0
  833. package/examples/extensions/sandbox/package-lock.json +92 -0
  834. package/examples/extensions/sandbox/package.json +19 -0
  835. package/examples/extensions/send-user-message.ts +97 -0
  836. package/examples/extensions/session-name.ts +27 -0
  837. package/examples/extensions/shutdown-command.ts +63 -0
  838. package/examples/extensions/snake.ts +343 -0
  839. package/examples/extensions/space-invaders.ts +560 -0
  840. package/examples/extensions/ssh.ts +220 -0
  841. package/examples/extensions/status-line.ts +32 -0
  842. package/examples/extensions/structured-output.ts +65 -0
  843. package/examples/extensions/subagent/README.md +175 -0
  844. package/examples/extensions/subagent/agents/planner.md +37 -0
  845. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  846. package/examples/extensions/subagent/agents/scout.md +50 -0
  847. package/examples/extensions/subagent/agents/worker.md +24 -0
  848. package/examples/extensions/subagent/agents.ts +126 -0
  849. package/examples/extensions/subagent/index.ts +1015 -0
  850. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  851. package/examples/extensions/subagent/prompts/implement.md +10 -0
  852. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  853. package/examples/extensions/summarize.ts +206 -0
  854. package/examples/extensions/system-prompt-header.ts +17 -0
  855. package/examples/extensions/tic-tac-toe.ts +1008 -0
  856. package/examples/extensions/timed-confirm.ts +70 -0
  857. package/examples/extensions/titlebar-spinner.ts +58 -0
  858. package/examples/extensions/todo.ts +297 -0
  859. package/examples/extensions/tool-override.ts +144 -0
  860. package/examples/extensions/tools.ts +146 -0
  861. package/examples/extensions/trigger-compact.ts +50 -0
  862. package/examples/extensions/truncated-tool.ts +195 -0
  863. package/examples/extensions/widget-placement.ts +9 -0
  864. package/examples/extensions/with-deps/index.ts +32 -0
  865. package/examples/extensions/with-deps/package-lock.json +31 -0
  866. package/examples/extensions/with-deps/package.json +22 -0
  867. package/examples/extensions/working-indicator.ts +123 -0
  868. package/examples/extensions/working-message-test.ts +25 -0
  869. package/examples/rpc-extension-ui.ts +632 -0
  870. package/examples/sdk/01-minimal.ts +26 -0
  871. package/examples/sdk/02-custom-model.ts +53 -0
  872. package/examples/sdk/03-custom-prompt.ts +75 -0
  873. package/examples/sdk/04-skills.ts +55 -0
  874. package/examples/sdk/05-tools.ts +48 -0
  875. package/examples/sdk/06-extensions.ts +99 -0
  876. package/examples/sdk/07-context-files.ts +47 -0
  877. package/examples/sdk/08-prompt-templates.ts +51 -0
  878. package/examples/sdk/09-api-keys-and-oauth.ts +52 -0
  879. package/examples/sdk/10-settings.ts +53 -0
  880. package/examples/sdk/11-sessions.ts +52 -0
  881. package/examples/sdk/12-full-control.ts +77 -0
  882. package/examples/sdk/13-session-runtime.ts +67 -0
  883. package/examples/sdk/README.md +144 -0
  884. package/package.json +65 -0
@@ -0,0 +1,4802 @@
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 { getProviders, } from "@earendil-works/pi-ai/compat";
10
+ import { CombinedAutocompleteProvider, Container, fuzzyFilter, getCapabilities, hyperlink, Loader, Markdown, matchesKey, ProcessTerminal, Spacer, setKeybindings, Text, TruncatedText, TUI, visibleWidth, } from "@earendil-works/pi-tui";
11
+ import chalk from "chalk";
12
+ import { spawn, spawnSync } from "child_process";
13
+ import { APP_NAME, APP_TITLE, CONFIG_DIR_NAME, getAgentDir, getAuthPath, getDebugLogPath, getDocsPath, getShareViewerUrl, VERSION, } from "../../config.js";
14
+ import { parseSkillBlock } from "../../core/agent-session.js";
15
+ import { SessionImportFileNotFoundError } from "../../core/agent-session-runtime.js";
16
+ import { FooterDataProvider } from "../../core/footer-data-provider.js";
17
+ import { configureHttpDispatcher, formatHttpIdleTimeoutMs } from "../../core/http-dispatcher.js";
18
+ import { KeybindingsManager } from "../../core/keybindings.js";
19
+ import { createCompactionSummaryMessage } from "../../core/messages.js";
20
+ import { defaultModelPerProvider, findExactModelReferenceMatch, resolveModelScope } from "../../core/model-resolver.js";
21
+ import { DefaultPackageManager } from "../../core/package-manager.js";
22
+ import { BUILT_IN_PROVIDER_DISPLAY_NAMES } from "../../core/provider-display-names.js";
23
+ import { formatMissingSessionCwdPrompt, MissingSessionCwdError } from "../../core/session-cwd.js";
24
+ import { SessionManager } from "../../core/session-manager.js";
25
+ import { BUILTIN_SLASH_COMMANDS } from "../../core/slash-commands.js";
26
+ import { isInstallTelemetryEnabled } from "../../core/telemetry.js";
27
+ import { hasTrustRequiringProjectResources, ProjectTrustStore } from "../../core/trust-manager.js";
28
+ import { getChangelogPath, getNewEntries, normalizeChangelogLinks, parseChangelog } from "../../utils/changelog.js";
29
+ import { copyToClipboard } from "../../utils/clipboard.js";
30
+ import { extensionForImageMimeType, readClipboardImage } from "../../utils/clipboard-image.js";
31
+ import { parseGitUrl } from "../../utils/git.js";
32
+ import { getCwdRelativePath } from "../../utils/paths.js";
33
+ import { getPiUserAgent } from "../../utils/pi-user-agent.js";
34
+ import { killTrackedDetachedChildren } from "../../utils/shell.js";
35
+ import { ensureTool } from "../../utils/tools-manager.js";
36
+ import { checkForNewPiVersion } from "../../utils/version-check.js";
37
+ import { ArminComponent } from "./components/armin.js";
38
+ import { AssistantMessageComponent } from "./components/assistant-message.js";
39
+ import { BashExecutionComponent } from "./components/bash-execution.js";
40
+ import { BorderedLoader } from "./components/bordered-loader.js";
41
+ import { BranchSummaryMessageComponent } from "./components/branch-summary-message.js";
42
+ import { CompactionSummaryMessageComponent } from "./components/compaction-summary-message.js";
43
+ import { CountdownTimer } from "./components/countdown-timer.js";
44
+ import { CustomEditor } from "./components/custom-editor.js";
45
+ import { CustomMessageComponent } from "./components/custom-message.js";
46
+ import { DaxnutsComponent } from "./components/daxnuts.js";
47
+ import { DynamicBorder } from "./components/dynamic-border.js";
48
+ import { EarendilAnnouncementComponent } from "./components/earendil-announcement.js";
49
+ import { ExtensionEditorComponent } from "./components/extension-editor.js";
50
+ import { ExtensionInputComponent } from "./components/extension-input.js";
51
+ import { ExtensionSelectorComponent } from "./components/extension-selector.js";
52
+ import { FooterComponent } from "./components/footer.js";
53
+ import { formatKeyText, keyDisplayText, keyHint, keyText, rawKeyHint } from "./components/keybinding-hints.js";
54
+ import { LoginDialogComponent } from "./components/login-dialog.js";
55
+ import { ModelSelectorComponent } from "./components/model-selector.js";
56
+ import { OAuthSelectorComponent } from "./components/oauth-selector.js";
57
+ import { ScopedModelsSelectorComponent } from "./components/scoped-models-selector.js";
58
+ import { SessionSelectorComponent } from "./components/session-selector.js";
59
+ import { SettingsSelectorComponent } from "./components/settings-selector.js";
60
+ import { SkillInvocationMessageComponent } from "./components/skill-invocation-message.js";
61
+ import { ToolExecutionComponent } from "./components/tool-execution.js";
62
+ import { TreeSelectorComponent } from "./components/tree-selector.js";
63
+ import { TrustSelectorComponent } from "./components/trust-selector.js";
64
+ import { UserMessageComponent } from "./components/user-message.js";
65
+ import { UserMessageSelectorComponent } from "./components/user-message-selector.js";
66
+ import { getModelSearchText } from "./model-search.js";
67
+ import { getAvailableThemes, getAvailableThemesWithPaths, getEditorTheme, getMarkdownTheme, getThemeByName, onThemeChange, setRegisteredThemes, stopThemeWatcher, Theme, theme, } from "./theme/theme.js";
68
+ import { InteractiveThemeController } from "./theme/theme-controller.js";
69
+ function isExpandable(obj) {
70
+ return typeof obj === "object" && obj !== null && "setExpanded" in obj && typeof obj.setExpanded === "function";
71
+ }
72
+ class ExpandableText extends Text {
73
+ getCollapsedText;
74
+ getExpandedText;
75
+ constructor(getCollapsedText, getExpandedText, expanded = false, paddingX = 0, paddingY = 0) {
76
+ super(expanded ? getExpandedText() : getCollapsedText(), paddingX, paddingY);
77
+ this.getCollapsedText = getCollapsedText;
78
+ this.getExpandedText = getExpandedText;
79
+ }
80
+ setExpanded(expanded) {
81
+ this.setText(expanded ? this.getExpandedText() : this.getCollapsedText());
82
+ }
83
+ }
84
+ const DEAD_TERMINAL_ERROR_CODES = new Set(["EIO", "EPIPE", "ENOTCONN"]);
85
+ function isDeadTerminalError(error) {
86
+ if (!error || typeof error !== "object" || !("code" in error)) {
87
+ return false;
88
+ }
89
+ const code = error.code;
90
+ return code !== undefined && DEAD_TERMINAL_ERROR_CODES.has(code);
91
+ }
92
+ const ANTHROPIC_SUBSCRIPTION_AUTH_WARNING = "Anthropic subscription auth is active. Third-party harness usage draws from extra usage and is billed per token, not your Claude plan limits. Manage extra usage at https://claude.ai/settings/usage.";
93
+ function isAnthropicSubscriptionAuthKey(apiKey) {
94
+ return typeof apiKey === "string" && apiKey.startsWith("sk-ant-oat");
95
+ }
96
+ function isUnknownModel(model) {
97
+ return !!model && model.provider === "unknown" && model.id === "unknown" && model.api === "unknown";
98
+ }
99
+ function quoteIfNeeded(value) {
100
+ if (value.length > 0 && !/[^a-zA-Z0-9_\-./~:@]/.test(value)) {
101
+ return value;
102
+ }
103
+ return `'${value.replace(/'/g, `'\\''`)}'`;
104
+ }
105
+ export function formatResumeCommand(sessionManager) {
106
+ if (!process.stdout.isTTY)
107
+ return undefined;
108
+ if (!sessionManager.isPersisted())
109
+ return undefined;
110
+ const sessionFile = sessionManager.getSessionFile();
111
+ if (!sessionFile || !fs.existsSync(sessionFile))
112
+ return undefined;
113
+ const args = [APP_NAME];
114
+ if (!sessionManager.usesDefaultSessionDir()) {
115
+ args.push("--session-dir", quoteIfNeeded(sessionManager.getSessionDir()));
116
+ }
117
+ args.push("--session", sessionManager.getSessionId());
118
+ return args.join(" ");
119
+ }
120
+ function hasDefaultModelProvider(providerId) {
121
+ return providerId in defaultModelPerProvider;
122
+ }
123
+ const BEDROCK_PROVIDER_ID = "amazon-bedrock";
124
+ const BUILT_IN_MODEL_PROVIDERS = new Set(getProviders());
125
+ export function isApiKeyLoginProvider(providerId, oauthProviderIds, builtInProviderIds = BUILT_IN_MODEL_PROVIDERS) {
126
+ if (BUILT_IN_PROVIDER_DISPLAY_NAMES[providerId]) {
127
+ return true;
128
+ }
129
+ if (builtInProviderIds.has(providerId)) {
130
+ return false;
131
+ }
132
+ return !oauthProviderIds.has(providerId);
133
+ }
134
+ export class InteractiveMode {
135
+ runtimeHost;
136
+ ui;
137
+ chatContainer;
138
+ pendingMessagesContainer;
139
+ statusContainer;
140
+ defaultEditor;
141
+ editor;
142
+ editorComponentFactory;
143
+ autocompleteProvider;
144
+ autocompleteProviderWrappers = [];
145
+ fdPath;
146
+ editorContainer;
147
+ footer;
148
+ footerDataProvider;
149
+ // Stored so the same manager can be injected into custom editors, selectors, and extension UI.
150
+ keybindings;
151
+ version;
152
+ isInitialized = false;
153
+ onInputCallback;
154
+ pendingUserInputs = [];
155
+ loadingAnimation = undefined;
156
+ workingMessage = undefined;
157
+ workingVisible = true;
158
+ workingIndicatorOptions = undefined;
159
+ defaultWorkingMessage = "Working...";
160
+ defaultHiddenThinkingLabel = "Thinking...";
161
+ hiddenThinkingLabel = this.defaultHiddenThinkingLabel;
162
+ lastSigintTime = 0;
163
+ lastEscapeTime = 0;
164
+ changelogMarkdown = undefined;
165
+ startupNoticesShown = false;
166
+ anthropicSubscriptionWarningShown = false;
167
+ // Status line tracking (for mutating immediately-sequential status updates)
168
+ lastStatusSpacer = undefined;
169
+ lastStatusText = undefined;
170
+ // Streaming message tracking
171
+ streamingComponent = undefined;
172
+ streamingMessage = undefined;
173
+ // Tool execution tracking: toolCallId -> component
174
+ pendingTools = new Map();
175
+ // Tool output expansion state
176
+ toolOutputExpanded = false;
177
+ // Thinking block visibility state
178
+ hideThinkingBlock = false;
179
+ // Skill commands: command name -> skill file path
180
+ skillCommands = new Map();
181
+ // Agent subscription unsubscribe function
182
+ unsubscribe;
183
+ signalCleanupHandlers = [];
184
+ // Track if editor is in bash mode (text starts with !)
185
+ isBashMode = false;
186
+ // Track current bash execution component
187
+ bashComponent = undefined;
188
+ // Track pending bash components (shown in pending area, moved to chat on submit)
189
+ pendingBashComponents = [];
190
+ // Auto-compaction state
191
+ autoCompactionLoader = undefined;
192
+ autoCompactionEscapeHandler;
193
+ // Auto-retry state
194
+ retryLoader = undefined;
195
+ retryCountdown = undefined;
196
+ retryEscapeHandler;
197
+ // Messages queued while compaction is running
198
+ compactionQueuedMessages = [];
199
+ // Shutdown state
200
+ shutdownRequested = false;
201
+ // Extension UI state
202
+ extensionSelector = undefined;
203
+ extensionInput = undefined;
204
+ extensionEditor = undefined;
205
+ extensionTerminalInputUnsubscribers = new Set();
206
+ // Extension widgets (components rendered above/below the editor)
207
+ extensionWidgetsAbove = new Map();
208
+ extensionWidgetsBelow = new Map();
209
+ widgetContainerAbove;
210
+ widgetContainerBelow;
211
+ // Custom footer from extension (undefined = use built-in footer)
212
+ customFooter = undefined;
213
+ // Header container that holds the built-in or custom header
214
+ headerContainer;
215
+ // Built-in header (logo + keybinding hints + changelog)
216
+ builtInHeader = undefined;
217
+ // Custom header from extension (undefined = use built-in header)
218
+ customHeader = undefined;
219
+ options;
220
+ autoTrustOnReloadCwd;
221
+ themeController;
222
+ // Convenience accessors
223
+ get session() {
224
+ return this.runtimeHost.session;
225
+ }
226
+ get agent() {
227
+ return this.session.agent;
228
+ }
229
+ get sessionManager() {
230
+ return this.session.sessionManager;
231
+ }
232
+ get settingsManager() {
233
+ return this.session.settingsManager;
234
+ }
235
+ constructor(runtimeHost, options = {}) {
236
+ this.runtimeHost = runtimeHost;
237
+ this.options = options;
238
+ this.autoTrustOnReloadCwd = options.autoTrustOnReloadCwd;
239
+ this.runtimeHost.setBeforeSessionInvalidate(() => {
240
+ this.resetExtensionUI();
241
+ });
242
+ this.runtimeHost.setRebindSession(async () => {
243
+ await this.rebindCurrentSession({ renderBeforeBind: true });
244
+ });
245
+ this.version = VERSION;
246
+ this.ui = new TUI(new ProcessTerminal(), this.settingsManager.getShowHardwareCursor());
247
+ this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
248
+ this.headerContainer = new Container();
249
+ this.chatContainer = new Container();
250
+ this.pendingMessagesContainer = new Container();
251
+ this.statusContainer = new Container();
252
+ this.widgetContainerAbove = new Container();
253
+ this.widgetContainerBelow = new Container();
254
+ this.keybindings = KeybindingsManager.create();
255
+ setKeybindings(this.keybindings);
256
+ const editorPaddingX = this.settingsManager.getEditorPaddingX();
257
+ const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
258
+ this.defaultEditor = new CustomEditor(this.ui, getEditorTheme(), this.keybindings, {
259
+ paddingX: editorPaddingX,
260
+ autocompleteMaxVisible,
261
+ });
262
+ this.editor = this.defaultEditor;
263
+ this.editorContainer = new Container();
264
+ this.editorContainer.addChild(this.editor);
265
+ this.footerDataProvider = new FooterDataProvider(this.sessionManager.getCwd());
266
+ this.footer = new FooterComponent(this.session, this.footerDataProvider);
267
+ this.footer.setAutoCompactEnabled(this.session.autoCompactionEnabled);
268
+ // Load hide thinking block setting
269
+ this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
270
+ // Register themes from resource loader and initialize
271
+ setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
272
+ this.themeController = new InteractiveThemeController(this.ui, this.settingsManager, (message) => this.showError(message), () => this.updateEditorBorderColor());
273
+ }
274
+ getAutocompleteSourceTag(sourceInfo) {
275
+ if (!sourceInfo) {
276
+ return undefined;
277
+ }
278
+ const scopePrefix = sourceInfo.scope === "user" ? "u" : sourceInfo.scope === "project" ? "p" : "t";
279
+ const source = sourceInfo.source.trim();
280
+ if (source === "auto" || source === "local" || source === "cli") {
281
+ return scopePrefix;
282
+ }
283
+ if (source.startsWith("npm:")) {
284
+ return `${scopePrefix}:${source}`;
285
+ }
286
+ const gitSource = parseGitUrl(source);
287
+ if (gitSource) {
288
+ const ref = gitSource.ref ? `@${gitSource.ref}` : "";
289
+ return `${scopePrefix}:git:${gitSource.host}/${gitSource.path}${ref}`;
290
+ }
291
+ return scopePrefix;
292
+ }
293
+ prefixAutocompleteDescription(description, sourceInfo) {
294
+ const sourceTag = this.getAutocompleteSourceTag(sourceInfo);
295
+ if (!sourceTag) {
296
+ return description;
297
+ }
298
+ return description ? `[${sourceTag}] ${description}` : `[${sourceTag}]`;
299
+ }
300
+ getBuiltInCommandConflictDiagnostics(extensionRunner) {
301
+ const builtinNames = new Set(BUILTIN_SLASH_COMMANDS.map((command) => command.name));
302
+ return extensionRunner
303
+ .getRegisteredCommands()
304
+ .filter((command) => builtinNames.has(command.name))
305
+ .map((command) => ({
306
+ type: "warning",
307
+ message: command.invocationName === command.name
308
+ ? `Extension command '/${command.name}' conflicts with built-in interactive command. Skipping in autocomplete.`
309
+ : `Extension command '/${command.name}' conflicts with built-in interactive command. Available as '/${command.invocationName}'.`,
310
+ path: command.sourceInfo.path,
311
+ }));
312
+ }
313
+ createBaseAutocompleteProvider() {
314
+ // Define commands for autocomplete
315
+ const slashCommands = BUILTIN_SLASH_COMMANDS.map((command) => ({
316
+ name: command.name,
317
+ description: command.description,
318
+ }));
319
+ const modelCommand = slashCommands.find((command) => command.name === "model");
320
+ if (modelCommand) {
321
+ modelCommand.getArgumentCompletions = (prefix) => {
322
+ // Get available models (scoped or from registry)
323
+ const models = this.session.scopedModels.length > 0
324
+ ? this.session.scopedModels.map((s) => s.model)
325
+ : this.session.modelRegistry.getAvailable();
326
+ if (models.length === 0)
327
+ return null;
328
+ // Create items with provider/id format
329
+ const items = models.map((m) => ({
330
+ id: m.id,
331
+ provider: m.provider,
332
+ name: m.name,
333
+ label: `${m.provider}/${m.id}`,
334
+ }));
335
+ // Fuzzy filter by model ID + provider in either order.
336
+ const filtered = fuzzyFilter(items, prefix, getModelSearchText);
337
+ if (filtered.length === 0)
338
+ return null;
339
+ return filtered.map((item) => ({
340
+ value: item.label,
341
+ label: item.id,
342
+ description: item.provider,
343
+ }));
344
+ };
345
+ }
346
+ // Convert prompt templates to SlashCommand format for autocomplete
347
+ const templateCommands = this.session.promptTemplates.map((cmd) => ({
348
+ name: cmd.name,
349
+ description: this.prefixAutocompleteDescription(cmd.description, cmd.sourceInfo),
350
+ ...(cmd.argumentHint && { argumentHint: cmd.argumentHint }),
351
+ }));
352
+ // Convert extension commands to SlashCommand format
353
+ const builtinCommandNames = new Set(slashCommands.map((c) => c.name));
354
+ const extensionCommands = this.session.extensionRunner
355
+ .getRegisteredCommands()
356
+ .filter((cmd) => !builtinCommandNames.has(cmd.name))
357
+ .map((cmd) => ({
358
+ name: cmd.invocationName,
359
+ description: this.prefixAutocompleteDescription(cmd.description, cmd.sourceInfo),
360
+ getArgumentCompletions: cmd.getArgumentCompletions,
361
+ }));
362
+ // Build skill commands from session.skills (if enabled)
363
+ this.skillCommands.clear();
364
+ const skillCommandList = [];
365
+ if (this.settingsManager.getEnableSkillCommands()) {
366
+ for (const skill of this.session.resourceLoader.getSkills().skills) {
367
+ const commandName = `skill:${skill.name}`;
368
+ this.skillCommands.set(commandName, skill.filePath);
369
+ skillCommandList.push({
370
+ name: commandName,
371
+ description: this.prefixAutocompleteDescription(skill.description, skill.sourceInfo),
372
+ });
373
+ }
374
+ }
375
+ return new CombinedAutocompleteProvider([...slashCommands, ...templateCommands, ...extensionCommands, ...skillCommandList], this.sessionManager.getCwd(), this.fdPath);
376
+ }
377
+ setupAutocompleteProvider() {
378
+ let provider = this.createBaseAutocompleteProvider();
379
+ const triggerCharacters = [];
380
+ for (const wrapProvider of this.autocompleteProviderWrappers) {
381
+ provider = wrapProvider(provider);
382
+ triggerCharacters.push(...(provider.triggerCharacters ?? []));
383
+ }
384
+ if (triggerCharacters.length > 0) {
385
+ provider.triggerCharacters = [...new Set(triggerCharacters)];
386
+ }
387
+ this.autocompleteProvider = provider;
388
+ this.defaultEditor.setAutocompleteProvider(provider);
389
+ if (this.editor !== this.defaultEditor) {
390
+ this.editor.setAutocompleteProvider?.(provider);
391
+ }
392
+ }
393
+ showStartupNoticesIfNeeded() {
394
+ if (this.startupNoticesShown) {
395
+ return;
396
+ }
397
+ this.startupNoticesShown = true;
398
+ if (!this.changelogMarkdown) {
399
+ return;
400
+ }
401
+ if (this.chatContainer.children.length > 0) {
402
+ this.chatContainer.addChild(new Spacer(1));
403
+ }
404
+ this.chatContainer.addChild(new DynamicBorder());
405
+ if (this.settingsManager.getCollapseChangelog()) {
406
+ const versionMatch = this.changelogMarkdown.match(/##\s+\[?(\d+\.\d+\.\d+)\]?/);
407
+ const latestVersion = versionMatch ? versionMatch[1] : this.version;
408
+ const condensedText = `Updated to v${latestVersion}. Use ${theme.bold("/changelog")} to view full changelog.`;
409
+ this.chatContainer.addChild(new Text(condensedText, 1, 0));
410
+ }
411
+ else {
412
+ this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
413
+ this.chatContainer.addChild(new Spacer(1));
414
+ this.chatContainer.addChild(new Markdown(this.changelogMarkdown.trim(), 1, 0, this.getMarkdownThemeWithSettings()));
415
+ this.chatContainer.addChild(new Spacer(1));
416
+ }
417
+ this.chatContainer.addChild(new DynamicBorder());
418
+ }
419
+ async init() {
420
+ if (this.isInitialized)
421
+ return;
422
+ this.registerSignalHandlers();
423
+ // Load changelog (only show new entries, skip for resumed sessions)
424
+ this.changelogMarkdown = this.getChangelogForDisplay();
425
+ // Ensure fd and rg are available (downloads if missing, adds to PATH via getBinDir)
426
+ // Both are needed: fd for autocomplete, rg for grep tool and bash commands
427
+ const [fdPath] = await Promise.all([ensureTool("fd"), ensureTool("rg")]);
428
+ this.fdPath = fdPath;
429
+ if (this.session.scopedModels.length > 0 && (this.options.verbose || !this.settingsManager.getQuietStartup())) {
430
+ const modelList = this.session.scopedModels
431
+ .map((sm) => {
432
+ const thinkingStr = sm.thinkingLevel ? `:${sm.thinkingLevel}` : "";
433
+ return `${sm.model.id}${thinkingStr}`;
434
+ })
435
+ .join(", ");
436
+ const cycleKeys = this.keybindings.getKeys("app.model.cycleForward");
437
+ const cycleHint = cycleKeys.length > 0
438
+ ? theme.fg("muted", ` (${formatKeyText(cycleKeys.join("/"), { capitalize: true })} to cycle)`)
439
+ : "";
440
+ console.log(theme.fg("dim", `Model scope: ${modelList}${cycleHint}`));
441
+ }
442
+ // Add header container as first child. Populate it after detectThemeIfUnset.
443
+ this.ui.addChild(this.headerContainer);
444
+ this.ui.addChild(this.chatContainer);
445
+ this.ui.addChild(this.pendingMessagesContainer);
446
+ this.ui.addChild(this.statusContainer);
447
+ this.renderWidgets(); // Initialize with default spacer
448
+ this.ui.addChild(this.widgetContainerAbove);
449
+ this.ui.addChild(this.editorContainer);
450
+ this.ui.addChild(this.widgetContainerBelow);
451
+ this.ui.addChild(this.footer);
452
+ this.ui.setFocus(this.editor);
453
+ this.setupKeyHandlers();
454
+ this.setupEditorSubmitHandler();
455
+ // Start the UI before initializing extensions so session_start handlers can use interactive dialogs
456
+ this.ui.start();
457
+ this.isInitialized = true;
458
+ await this.themeController.applyFromSettings();
459
+ // Add header with keybindings from config (unless silenced)
460
+ if (this.options.verbose || !this.settingsManager.getQuietStartup()) {
461
+ const logo = theme.bold(theme.fg("accent", APP_NAME)) + theme.fg("dim", ` v${this.version}`);
462
+ // Build startup instructions using keybinding hint helpers
463
+ const hint = (keybinding, description) => keyHint(keybinding, description);
464
+ const expandedInstructions = [
465
+ hint("app.interrupt", "to interrupt"),
466
+ hint("app.clear", "to clear"),
467
+ rawKeyHint(`${keyText("app.clear")} twice`, "to exit"),
468
+ hint("app.exit", "to exit (empty)"),
469
+ hint("app.suspend", "to suspend"),
470
+ keyHint("tui.editor.deleteToLineEnd", "to delete to end"),
471
+ hint("app.thinking.cycle", "to cycle thinking level"),
472
+ rawKeyHint(`${keyText("app.model.cycleForward")}/${keyText("app.model.cycleBackward")}`, "to cycle models"),
473
+ hint("app.model.select", "to select model"),
474
+ hint("app.tools.expand", "to expand tools"),
475
+ hint("app.thinking.toggle", "to expand thinking"),
476
+ hint("app.editor.external", "for external editor"),
477
+ rawKeyHint("/", "for commands"),
478
+ rawKeyHint("!", "to run bash"),
479
+ rawKeyHint("!!", "to run bash (no context)"),
480
+ hint("app.message.followUp", "to queue follow-up"),
481
+ hint("app.message.dequeue", "to edit all queued messages"),
482
+ hint("app.clipboard.pasteImage", "to paste image"),
483
+ rawKeyHint("drop files", "to attach"),
484
+ ].join("\n");
485
+ const compactInstructions = [
486
+ hint("app.interrupt", "interrupt"),
487
+ rawKeyHint(`${keyText("app.clear")}/${keyText("app.exit")}`, "clear/exit"),
488
+ rawKeyHint("/", "commands"),
489
+ rawKeyHint("!", "bash"),
490
+ hint("app.tools.expand", "more"),
491
+ ].join(theme.fg("muted", " · "));
492
+ const compactOnboarding = theme.fg("dim", `Press ${keyText("app.tools.expand")} to show full startup help and loaded resources.`);
493
+ const onboarding = theme.fg("dim", `Pi can explain its own features and look up its docs. Ask it how to use or extend Pi.`);
494
+ this.builtInHeader = new ExpandableText(() => `${logo}\n${compactInstructions}\n${compactOnboarding}\n\n${onboarding}`, () => `${logo}\n${expandedInstructions}\n\n${onboarding}`, this.getStartupExpansionState(), 1, 0);
495
+ // Setup UI layout
496
+ this.headerContainer.addChild(new Spacer(1));
497
+ this.headerContainer.addChild(this.builtInHeader);
498
+ this.headerContainer.addChild(new Spacer(1));
499
+ }
500
+ else {
501
+ // Minimal header when silenced
502
+ this.builtInHeader = new Text("", 0, 0);
503
+ this.headerContainer.addChild(this.builtInHeader);
504
+ }
505
+ this.ui.requestRender();
506
+ // Initialize extensions first so resources are shown before messages
507
+ await this.rebindCurrentSession();
508
+ // Render initial messages AFTER showing loaded resources
509
+ this.renderInitialMessages();
510
+ // Set up theme file watcher
511
+ onThemeChange(() => {
512
+ this.ui.invalidate();
513
+ this.updateEditorBorderColor();
514
+ this.ui.requestRender();
515
+ });
516
+ // Set up git branch watcher (uses provider instead of footer)
517
+ this.footerDataProvider.onBranchChange(() => {
518
+ this.ui.requestRender();
519
+ });
520
+ // Initialize available provider count for footer display
521
+ await this.updateAvailableProviderCount();
522
+ }
523
+ /**
524
+ * Update terminal title with session name and cwd.
525
+ */
526
+ updateTerminalTitle() {
527
+ const cwdBasename = path.basename(this.sessionManager.getCwd());
528
+ const sessionName = this.sessionManager.getSessionName();
529
+ if (sessionName) {
530
+ this.ui.terminal.setTitle(`${APP_TITLE} - ${sessionName} - ${cwdBasename}`);
531
+ }
532
+ else {
533
+ this.ui.terminal.setTitle(`${APP_TITLE} - ${cwdBasename}`);
534
+ }
535
+ }
536
+ /**
537
+ * Run the interactive mode. This is the main entry point.
538
+ * Initializes the UI, shows warnings, processes initial messages, and starts the interactive loop.
539
+ */
540
+ async run() {
541
+ await this.init();
542
+ // Start version check asynchronously
543
+ checkForNewPiVersion(this.version).then((newRelease) => {
544
+ if (newRelease) {
545
+ this.showNewVersionNotification(newRelease);
546
+ }
547
+ });
548
+ // Start package update check asynchronously
549
+ this.checkForPackageUpdates().then((updates) => {
550
+ if (updates.length > 0) {
551
+ this.showPackageUpdateNotification(updates);
552
+ }
553
+ });
554
+ // Check tmux keyboard setup asynchronously
555
+ this.checkTmuxKeyboardSetup().then((warning) => {
556
+ if (warning) {
557
+ this.showWarning(warning);
558
+ }
559
+ });
560
+ // Show startup warnings
561
+ const { migratedProviders, modelFallbackMessage, initialMessage, initialImages, initialMessages } = this.options;
562
+ if (migratedProviders && migratedProviders.length > 0) {
563
+ this.showWarning(`Migrated credentials to auth.json: ${migratedProviders.join(", ")}`);
564
+ }
565
+ const modelsJsonError = this.session.modelRegistry.getError();
566
+ if (modelsJsonError) {
567
+ this.showError(`models.json error: ${modelsJsonError}`);
568
+ }
569
+ if (modelFallbackMessage) {
570
+ this.showWarning(modelFallbackMessage);
571
+ }
572
+ void this.maybeWarnAboutAnthropicSubscriptionAuth();
573
+ // Process initial messages
574
+ if (initialMessage) {
575
+ try {
576
+ await this.session.prompt(initialMessage, { images: initialImages });
577
+ }
578
+ catch (error) {
579
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
580
+ this.showError(errorMessage);
581
+ }
582
+ }
583
+ if (initialMessages) {
584
+ for (const message of initialMessages) {
585
+ try {
586
+ await this.session.prompt(message);
587
+ }
588
+ catch (error) {
589
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
590
+ this.showError(errorMessage);
591
+ }
592
+ }
593
+ }
594
+ // Main interactive loop
595
+ while (true) {
596
+ const userInput = await this.getUserInput();
597
+ try {
598
+ await this.session.prompt(userInput);
599
+ }
600
+ catch (error) {
601
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
602
+ this.showError(errorMessage);
603
+ }
604
+ }
605
+ }
606
+ async checkForPackageUpdates() {
607
+ if (process.env.PI_OFFLINE) {
608
+ return [];
609
+ }
610
+ try {
611
+ const packageManager = new DefaultPackageManager({
612
+ cwd: this.sessionManager.getCwd(),
613
+ agentDir: getAgentDir(),
614
+ settingsManager: this.settingsManager,
615
+ });
616
+ const updates = await packageManager.checkForAvailableUpdates();
617
+ return updates.map((update) => update.displayName);
618
+ }
619
+ catch {
620
+ return [];
621
+ }
622
+ }
623
+ async checkTmuxKeyboardSetup() {
624
+ if (!process.env.TMUX)
625
+ return undefined;
626
+ const runTmuxShow = (option) => {
627
+ return new Promise((resolve) => {
628
+ const proc = spawn("tmux", ["show", "-gv", option], {
629
+ stdio: ["ignore", "pipe", "ignore"],
630
+ });
631
+ let stdout = "";
632
+ const timer = setTimeout(() => {
633
+ proc.kill();
634
+ resolve(undefined);
635
+ }, 2000);
636
+ proc.stdout?.on("data", (data) => {
637
+ stdout += data.toString();
638
+ });
639
+ proc.on("error", () => {
640
+ clearTimeout(timer);
641
+ resolve(undefined);
642
+ });
643
+ proc.on("close", (code) => {
644
+ clearTimeout(timer);
645
+ resolve(code === 0 ? stdout.trim() : undefined);
646
+ });
647
+ });
648
+ };
649
+ const [extendedKeys, extendedKeysFormat] = await Promise.all([
650
+ runTmuxShow("extended-keys"),
651
+ runTmuxShow("extended-keys-format"),
652
+ ]);
653
+ // If we couldn't query tmux (timeout, sandbox, etc.), don't warn
654
+ if (extendedKeys === undefined)
655
+ return undefined;
656
+ if (extendedKeys !== "on" && extendedKeys !== "always") {
657
+ return "tmux extended-keys is off. Modified Enter keys may not work. Add `set -g extended-keys on` to ~/.tmux.conf and restart tmux.";
658
+ }
659
+ if (extendedKeysFormat === "xterm") {
660
+ return "tmux extended-keys-format is xterm. Pi works best with csi-u. Add `set -g extended-keys-format csi-u` to ~/.tmux.conf and restart tmux.";
661
+ }
662
+ return undefined;
663
+ }
664
+ /**
665
+ * Get changelog entries to display on startup.
666
+ * Only shows new entries since last seen version, skips for resumed sessions.
667
+ */
668
+ getChangelogForDisplay() {
669
+ // Skip changelog for resumed/continued sessions (already have messages)
670
+ if (this.session.state.messages.length > 0) {
671
+ return undefined;
672
+ }
673
+ const lastVersion = this.settingsManager.getLastChangelogVersion();
674
+ const changelogPath = getChangelogPath();
675
+ const entries = parseChangelog(changelogPath);
676
+ if (!lastVersion) {
677
+ // Fresh install - record the version, send telemetry, don't show changelog
678
+ this.settingsManager.setLastChangelogVersion(VERSION);
679
+ this.reportInstallTelemetry(VERSION);
680
+ return undefined;
681
+ }
682
+ const newEntries = getNewEntries(entries, lastVersion);
683
+ if (newEntries.length > 0) {
684
+ this.settingsManager.setLastChangelogVersion(VERSION);
685
+ this.reportInstallTelemetry(VERSION);
686
+ return newEntries.map((e) => normalizeChangelogLinks(e.content, e)).join("\n\n");
687
+ }
688
+ return undefined;
689
+ }
690
+ reportInstallTelemetry(version) {
691
+ if (process.env.PI_OFFLINE) {
692
+ return;
693
+ }
694
+ if (!isInstallTelemetryEnabled(this.settingsManager)) {
695
+ return;
696
+ }
697
+ void fetch(`https://pi.dev/api/report-install?version=${encodeURIComponent(version)}`, {
698
+ headers: {
699
+ "User-Agent": getPiUserAgent(version),
700
+ },
701
+ signal: AbortSignal.timeout(5000),
702
+ })
703
+ .then(() => undefined)
704
+ .catch(() => undefined);
705
+ }
706
+ getMarkdownThemeWithSettings() {
707
+ return {
708
+ ...getMarkdownTheme(),
709
+ codeBlockIndent: this.settingsManager.getCodeBlockIndent(),
710
+ };
711
+ }
712
+ // =========================================================================
713
+ // Extension System
714
+ // =========================================================================
715
+ formatDisplayPath(p) {
716
+ const home = os.homedir();
717
+ let result = p;
718
+ // Replace home directory with ~
719
+ if (result.startsWith(home)) {
720
+ result = `~${result.slice(home.length)}`;
721
+ }
722
+ return result;
723
+ }
724
+ formatExtensionDisplayPath(path) {
725
+ let result = this.formatDisplayPath(path);
726
+ result = result.replace(/\/index\.ts$/, "").replace(/\/index\.js$/, "");
727
+ return result;
728
+ }
729
+ formatContextPath(p) {
730
+ const cwd = path.resolve(this.sessionManager.getCwd());
731
+ const absolutePath = path.isAbsolute(p) ? path.resolve(p) : path.resolve(cwd, p);
732
+ const relativePath = getCwdRelativePath(absolutePath, cwd);
733
+ if (relativePath !== undefined) {
734
+ return relativePath;
735
+ }
736
+ return this.formatDisplayPath(absolutePath);
737
+ }
738
+ getStartupExpansionState() {
739
+ return this.options.verbose || this.toolOutputExpanded;
740
+ }
741
+ /**
742
+ * Get a short path relative to the package root for display.
743
+ */
744
+ getShortPath(fullPath, sourceInfo) {
745
+ const baseDir = sourceInfo?.baseDir;
746
+ if (baseDir && this.isPackageSource(sourceInfo)) {
747
+ const relativePath = path.relative(path.resolve(baseDir), path.resolve(fullPath));
748
+ if (relativePath &&
749
+ relativePath !== "." &&
750
+ !relativePath.startsWith("..") &&
751
+ !relativePath.startsWith(`..${path.sep}`) &&
752
+ !path.isAbsolute(relativePath)) {
753
+ return relativePath.replace(/\\/g, "/");
754
+ }
755
+ }
756
+ const source = sourceInfo?.source ?? "";
757
+ const npmMatch = fullPath.match(/node_modules\/(@?[^/]+(?:\/[^/]+)?)\/(.*)/);
758
+ if (npmMatch && source.startsWith("npm:")) {
759
+ return npmMatch[2];
760
+ }
761
+ const gitMatch = fullPath.match(/git\/[^/]+\/[^/]+\/(.*)/);
762
+ if (gitMatch && source.startsWith("git:")) {
763
+ return gitMatch[1];
764
+ }
765
+ return this.formatDisplayPath(fullPath);
766
+ }
767
+ getCompactPathLabel(resourcePath, sourceInfo) {
768
+ const shortPath = this.getShortPath(resourcePath, sourceInfo);
769
+ const normalizedPath = shortPath.replace(/\\/g, "/");
770
+ const segments = normalizedPath.split("/").filter((segment) => segment.length > 0 && segment !== "~");
771
+ if (segments.length > 0) {
772
+ return segments[segments.length - 1];
773
+ }
774
+ return shortPath;
775
+ }
776
+ getCompactPackageSourceLabel(sourceInfo) {
777
+ const source = sourceInfo?.source ?? "";
778
+ if (source.startsWith("npm:")) {
779
+ return source.slice("npm:".length) || source;
780
+ }
781
+ const gitSource = parseGitUrl(source);
782
+ if (gitSource) {
783
+ return gitSource.path || source;
784
+ }
785
+ return source;
786
+ }
787
+ getCompactExtensionLabel(resourcePath, sourceInfo) {
788
+ if (!this.isPackageSource(sourceInfo)) {
789
+ return this.getCompactPathLabel(resourcePath, sourceInfo);
790
+ }
791
+ const sourceLabel = this.getCompactPackageSourceLabel(sourceInfo);
792
+ if (!sourceLabel) {
793
+ return this.getCompactPathLabel(resourcePath, sourceInfo);
794
+ }
795
+ const shortPath = this.getShortPath(resourcePath, sourceInfo).replace(/\\/g, "/");
796
+ const packagePath = shortPath.startsWith("extensions/") ? shortPath.slice("extensions/".length) : shortPath;
797
+ const parsedPath = path.posix.parse(packagePath);
798
+ if (parsedPath.name === "index") {
799
+ return !parsedPath.dir || parsedPath.dir === "." ? sourceLabel : `${sourceLabel}:${parsedPath.dir}`;
800
+ }
801
+ return `${sourceLabel}:${packagePath}`;
802
+ }
803
+ getCompactDisplayPathSegments(resourcePath) {
804
+ return this.formatDisplayPath(resourcePath)
805
+ .replace(/\\/g, "/")
806
+ .split("/")
807
+ .filter((segment) => segment.length > 0 && segment !== "~");
808
+ }
809
+ getCompactNonPackageExtensionLabel(resourcePath, index, allPaths) {
810
+ const segments = allPaths[index]?.segments;
811
+ if (!segments || segments.length === 0) {
812
+ return this.getCompactPathLabel(resourcePath);
813
+ }
814
+ for (let segmentCount = 1; segmentCount <= segments.length; segmentCount += 1) {
815
+ const candidate = segments.slice(-segmentCount).join("/");
816
+ const isUnique = allPaths.every((item, itemIndex) => {
817
+ if (itemIndex === index) {
818
+ return true;
819
+ }
820
+ return item.segments.slice(-segmentCount).join("/") !== candidate;
821
+ });
822
+ if (isUnique) {
823
+ return candidate;
824
+ }
825
+ }
826
+ return segments.join("/");
827
+ }
828
+ getCompactExtensionLabels(extensions) {
829
+ const nonPackageExtensions = extensions
830
+ .map((extension) => {
831
+ const segments = this.getCompactDisplayPathSegments(extension.path);
832
+ const lastSegment = segments[segments.length - 1];
833
+ if (segments.length > 1 && (lastSegment === "index.ts" || lastSegment === "index.js")) {
834
+ segments.pop();
835
+ }
836
+ return {
837
+ path: extension.path,
838
+ sourceInfo: extension.sourceInfo,
839
+ segments,
840
+ };
841
+ })
842
+ .filter((extension) => !this.isPackageSource(extension.sourceInfo));
843
+ return extensions.map((extension) => {
844
+ if (this.isPackageSource(extension.sourceInfo)) {
845
+ return this.getCompactExtensionLabel(extension.path, extension.sourceInfo);
846
+ }
847
+ const nonPackageIndex = nonPackageExtensions.findIndex((item) => item.path === extension.path);
848
+ if (nonPackageIndex === -1) {
849
+ return this.getCompactPathLabel(extension.path, extension.sourceInfo);
850
+ }
851
+ return this.getCompactNonPackageExtensionLabel(extension.path, nonPackageIndex, nonPackageExtensions);
852
+ });
853
+ }
854
+ getDisplaySourceInfo(sourceInfo) {
855
+ const source = sourceInfo?.source ?? "local";
856
+ const scope = sourceInfo?.scope ?? "project";
857
+ if (source === "local") {
858
+ if (scope === "user") {
859
+ return { label: "user", color: "muted" };
860
+ }
861
+ if (scope === "project") {
862
+ return { label: "project", color: "muted" };
863
+ }
864
+ if (scope === "temporary") {
865
+ return { label: "path", scopeLabel: "temp", color: "muted" };
866
+ }
867
+ return { label: "path", color: "muted" };
868
+ }
869
+ if (source === "cli") {
870
+ return { label: "path", scopeLabel: scope === "temporary" ? "temp" : undefined, color: "muted" };
871
+ }
872
+ const scopeLabel = scope === "user" ? "user" : scope === "project" ? "project" : scope === "temporary" ? "temp" : undefined;
873
+ return { label: source, scopeLabel, color: "accent" };
874
+ }
875
+ getScopeGroup(sourceInfo) {
876
+ const source = sourceInfo?.source ?? "local";
877
+ const scope = sourceInfo?.scope ?? "project";
878
+ if (source === "cli" || scope === "temporary")
879
+ return "path";
880
+ if (scope === "user")
881
+ return "user";
882
+ if (scope === "project")
883
+ return "project";
884
+ return "path";
885
+ }
886
+ isPackageSource(sourceInfo) {
887
+ const source = sourceInfo?.source ?? "";
888
+ return source.startsWith("npm:") || source.startsWith("git:");
889
+ }
890
+ buildScopeGroups(items) {
891
+ const groups = {
892
+ user: { scope: "user", paths: [], packages: new Map() },
893
+ project: { scope: "project", paths: [], packages: new Map() },
894
+ path: { scope: "path", paths: [], packages: new Map() },
895
+ };
896
+ for (const item of items) {
897
+ const groupKey = this.getScopeGroup(item.sourceInfo);
898
+ const group = groups[groupKey];
899
+ const source = item.sourceInfo?.source ?? "local";
900
+ if (this.isPackageSource(item.sourceInfo)) {
901
+ const list = group.packages.get(source) ?? [];
902
+ list.push(item);
903
+ group.packages.set(source, list);
904
+ }
905
+ else {
906
+ group.paths.push(item);
907
+ }
908
+ }
909
+ return [groups.project, groups.user, groups.path].filter((group) => group.paths.length > 0 || group.packages.size > 0);
910
+ }
911
+ formatScopeGroups(groups, options) {
912
+ const lines = [];
913
+ for (const group of groups) {
914
+ lines.push(` ${theme.fg("accent", group.scope)}`);
915
+ const sortedPaths = [...group.paths].sort((a, b) => a.path.localeCompare(b.path));
916
+ for (const item of sortedPaths) {
917
+ lines.push(theme.fg("dim", ` ${options.formatPath(item)}`));
918
+ }
919
+ const sortedPackages = Array.from(group.packages.entries()).sort(([a], [b]) => a.localeCompare(b));
920
+ for (const [source, items] of sortedPackages) {
921
+ lines.push(` ${theme.fg("mdLink", source)}`);
922
+ const sortedPackagePaths = [...items].sort((a, b) => a.path.localeCompare(b.path));
923
+ for (const item of sortedPackagePaths) {
924
+ lines.push(theme.fg("dim", ` ${options.formatPackagePath(item, source)}`));
925
+ }
926
+ }
927
+ }
928
+ return lines.join("\n");
929
+ }
930
+ findSourceInfoForPath(p, sourceInfos) {
931
+ const exact = sourceInfos.get(p);
932
+ if (exact)
933
+ return exact;
934
+ let current = p;
935
+ while (current.includes("/")) {
936
+ current = current.substring(0, current.lastIndexOf("/"));
937
+ const parent = sourceInfos.get(current);
938
+ if (parent)
939
+ return parent;
940
+ }
941
+ return undefined;
942
+ }
943
+ formatPathWithSource(p, sourceInfo) {
944
+ if (sourceInfo) {
945
+ const shortPath = this.getShortPath(p, sourceInfo);
946
+ const { label, scopeLabel } = this.getDisplaySourceInfo(sourceInfo);
947
+ const labelText = scopeLabel ? `${label} (${scopeLabel})` : label;
948
+ return `${labelText} ${shortPath}`;
949
+ }
950
+ return this.formatDisplayPath(p);
951
+ }
952
+ formatDiagnostics(diagnostics, sourceInfos) {
953
+ const lines = [];
954
+ // Group collision diagnostics by name
955
+ const collisions = new Map();
956
+ const otherDiagnostics = [];
957
+ for (const d of diagnostics) {
958
+ if (d.type === "collision" && d.collision) {
959
+ const list = collisions.get(d.collision.name) ?? [];
960
+ list.push(d);
961
+ collisions.set(d.collision.name, list);
962
+ }
963
+ else {
964
+ otherDiagnostics.push(d);
965
+ }
966
+ }
967
+ // Format collision diagnostics grouped by name
968
+ for (const [name, collisionList] of collisions) {
969
+ const first = collisionList[0]?.collision;
970
+ if (!first)
971
+ continue;
972
+ lines.push(theme.fg("warning", ` "${name}" collision:`));
973
+ lines.push(theme.fg("dim", ` ${theme.fg("success", "✓")} ${this.formatPathWithSource(first.winnerPath, this.findSourceInfoForPath(first.winnerPath, sourceInfos))}`));
974
+ for (const d of collisionList) {
975
+ if (d.collision) {
976
+ lines.push(theme.fg("dim", ` ${theme.fg("warning", "✗")} ${this.formatPathWithSource(d.collision.loserPath, this.findSourceInfoForPath(d.collision.loserPath, sourceInfos))} (skipped)`));
977
+ }
978
+ }
979
+ }
980
+ for (const d of otherDiagnostics) {
981
+ if (d.path) {
982
+ const formattedPath = this.formatPathWithSource(d.path, this.findSourceInfoForPath(d.path, sourceInfos));
983
+ lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${formattedPath}`));
984
+ lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${d.message}`));
985
+ }
986
+ else {
987
+ lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${d.message}`));
988
+ }
989
+ }
990
+ return lines.join("\n");
991
+ }
992
+ showLoadedResources(options) {
993
+ const showListing = options?.force || this.options.verbose || !this.settingsManager.getQuietStartup();
994
+ const showDiagnostics = showListing || options?.showDiagnosticsWhenQuiet === true;
995
+ if (!showListing && !showDiagnostics) {
996
+ return;
997
+ }
998
+ const sectionHeader = (name, color = "mdHeading") => theme.fg(color, `[${name}]`);
999
+ const formatCompactList = (items, options) => {
1000
+ const labels = items.map((item) => item.trim()).filter((item) => item.length > 0);
1001
+ if (options?.sort !== false) {
1002
+ labels.sort((a, b) => a.localeCompare(b));
1003
+ }
1004
+ return theme.fg("dim", ` ${labels.join(", ")}`);
1005
+ };
1006
+ const addLoadedSection = (name, collapsedBody, expandedBody = collapsedBody, color = "mdHeading") => {
1007
+ const section = new ExpandableText(() => `${sectionHeader(name, color)}\n${collapsedBody}`, () => `${sectionHeader(name, color)}\n${expandedBody}`, this.getStartupExpansionState(), 0, 0);
1008
+ this.chatContainer.addChild(section);
1009
+ this.chatContainer.addChild(new Spacer(1));
1010
+ };
1011
+ const skillsResult = this.session.resourceLoader.getSkills();
1012
+ const promptsResult = this.session.resourceLoader.getPrompts();
1013
+ const themesResult = this.session.resourceLoader.getThemes();
1014
+ const extensions = options?.extensions ??
1015
+ this.session.resourceLoader.getExtensions().extensions.map((extension) => ({
1016
+ path: extension.path,
1017
+ sourceInfo: extension.sourceInfo,
1018
+ }));
1019
+ const sourceInfos = new Map();
1020
+ for (const extension of extensions) {
1021
+ if (extension.sourceInfo) {
1022
+ sourceInfos.set(extension.path, extension.sourceInfo);
1023
+ }
1024
+ }
1025
+ for (const skill of skillsResult.skills) {
1026
+ if (skill.sourceInfo) {
1027
+ sourceInfos.set(skill.filePath, skill.sourceInfo);
1028
+ }
1029
+ }
1030
+ for (const prompt of promptsResult.prompts) {
1031
+ if (prompt.sourceInfo) {
1032
+ sourceInfos.set(prompt.filePath, prompt.sourceInfo);
1033
+ }
1034
+ }
1035
+ for (const loadedTheme of themesResult.themes) {
1036
+ if (loadedTheme.sourcePath && loadedTheme.sourceInfo) {
1037
+ sourceInfos.set(loadedTheme.sourcePath, loadedTheme.sourceInfo);
1038
+ }
1039
+ }
1040
+ if (showListing) {
1041
+ const contextFiles = this.session.resourceLoader.getAgentsFiles().agentsFiles;
1042
+ if (contextFiles.length > 0) {
1043
+ this.chatContainer.addChild(new Spacer(1));
1044
+ const contextList = contextFiles
1045
+ .map((f) => theme.fg("dim", ` ${this.formatDisplayPath(f.path)}`))
1046
+ .join("\n");
1047
+ const contextCompactList = formatCompactList(contextFiles.map((contextFile) => this.formatContextPath(contextFile.path)), { sort: false });
1048
+ addLoadedSection("Context", contextCompactList, contextList);
1049
+ }
1050
+ const skills = skillsResult.skills;
1051
+ if (skills.length > 0) {
1052
+ const groups = this.buildScopeGroups(skills.map((skill) => ({ path: skill.filePath, sourceInfo: skill.sourceInfo })));
1053
+ const skillList = this.formatScopeGroups(groups, {
1054
+ formatPath: (item) => this.formatDisplayPath(item.path),
1055
+ formatPackagePath: (item) => this.getShortPath(item.path, item.sourceInfo),
1056
+ });
1057
+ const skillCompactList = formatCompactList(skills.map((skill) => skill.name));
1058
+ addLoadedSection("Skills", skillCompactList, skillList);
1059
+ }
1060
+ const templates = this.session.promptTemplates;
1061
+ if (templates.length > 0) {
1062
+ const groups = this.buildScopeGroups(templates.map((template) => ({ path: template.filePath, sourceInfo: template.sourceInfo })));
1063
+ const templateByPath = new Map(templates.map((t) => [t.filePath, t]));
1064
+ const templateList = this.formatScopeGroups(groups, {
1065
+ formatPath: (item) => {
1066
+ const template = templateByPath.get(item.path);
1067
+ return template ? `/${template.name}` : this.formatDisplayPath(item.path);
1068
+ },
1069
+ formatPackagePath: (item) => {
1070
+ const template = templateByPath.get(item.path);
1071
+ return template ? `/${template.name}` : this.formatDisplayPath(item.path);
1072
+ },
1073
+ });
1074
+ const promptCompactList = formatCompactList(templates.map((template) => `/${template.name}`));
1075
+ addLoadedSection("Prompts", promptCompactList, templateList);
1076
+ }
1077
+ if (extensions.length > 0) {
1078
+ const groups = this.buildScopeGroups(extensions);
1079
+ const extList = this.formatScopeGroups(groups, {
1080
+ formatPath: (item) => this.formatExtensionDisplayPath(item.path),
1081
+ formatPackagePath: (item) => this.formatExtensionDisplayPath(this.getShortPath(item.path, item.sourceInfo)),
1082
+ });
1083
+ const extensionCompactList = formatCompactList(this.getCompactExtensionLabels(extensions));
1084
+ addLoadedSection("Extensions", extensionCompactList, extList, "mdHeading");
1085
+ }
1086
+ // Show loaded themes (excluding built-in)
1087
+ const loadedThemes = themesResult.themes;
1088
+ const customThemes = loadedThemes.filter((t) => t.sourcePath);
1089
+ if (customThemes.length > 0) {
1090
+ const groups = this.buildScopeGroups(customThemes.map((loadedTheme) => ({
1091
+ path: loadedTheme.sourcePath,
1092
+ sourceInfo: loadedTheme.sourceInfo,
1093
+ })));
1094
+ const themeList = this.formatScopeGroups(groups, {
1095
+ formatPath: (item) => this.formatDisplayPath(item.path),
1096
+ formatPackagePath: (item) => this.getShortPath(item.path, item.sourceInfo),
1097
+ });
1098
+ const themeCompactList = formatCompactList(customThemes.map((loadedTheme) => loadedTheme.name ?? this.getCompactPathLabel(loadedTheme.sourcePath, loadedTheme.sourceInfo)));
1099
+ addLoadedSection("Themes", themeCompactList, themeList);
1100
+ }
1101
+ }
1102
+ if (showDiagnostics) {
1103
+ const skillDiagnostics = skillsResult.diagnostics;
1104
+ if (skillDiagnostics.length > 0) {
1105
+ const warningLines = this.formatDiagnostics(skillDiagnostics, sourceInfos);
1106
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Skill conflicts]")}\n${warningLines}`, 0, 0));
1107
+ this.chatContainer.addChild(new Spacer(1));
1108
+ }
1109
+ const promptDiagnostics = promptsResult.diagnostics;
1110
+ if (promptDiagnostics.length > 0) {
1111
+ const warningLines = this.formatDiagnostics(promptDiagnostics, sourceInfos);
1112
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Prompt conflicts]")}\n${warningLines}`, 0, 0));
1113
+ this.chatContainer.addChild(new Spacer(1));
1114
+ }
1115
+ const extensionDiagnostics = [];
1116
+ const extensionErrors = this.session.resourceLoader.getExtensions().errors;
1117
+ if (extensionErrors.length > 0) {
1118
+ for (const error of extensionErrors) {
1119
+ extensionDiagnostics.push({ type: "error", message: error.error, path: error.path });
1120
+ }
1121
+ }
1122
+ const commandDiagnostics = this.session.extensionRunner.getCommandDiagnostics();
1123
+ extensionDiagnostics.push(...commandDiagnostics);
1124
+ extensionDiagnostics.push(...this.getBuiltInCommandConflictDiagnostics(this.session.extensionRunner));
1125
+ const shortcutDiagnostics = this.session.extensionRunner.getShortcutDiagnostics();
1126
+ extensionDiagnostics.push(...shortcutDiagnostics);
1127
+ if (extensionDiagnostics.length > 0) {
1128
+ const warningLines = this.formatDiagnostics(extensionDiagnostics, sourceInfos);
1129
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Extension issues]")}\n${warningLines}`, 0, 0));
1130
+ this.chatContainer.addChild(new Spacer(1));
1131
+ }
1132
+ const themeDiagnostics = themesResult.diagnostics;
1133
+ if (themeDiagnostics.length > 0) {
1134
+ const warningLines = this.formatDiagnostics(themeDiagnostics, sourceInfos);
1135
+ this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Theme conflicts]")}\n${warningLines}`, 0, 0));
1136
+ this.chatContainer.addChild(new Spacer(1));
1137
+ }
1138
+ }
1139
+ }
1140
+ /**
1141
+ * Initialize the extension system with TUI-based UI context.
1142
+ */
1143
+ async bindCurrentSessionExtensions() {
1144
+ const uiContext = this.createExtensionUIContext();
1145
+ await this.session.bindExtensions({
1146
+ uiContext,
1147
+ mode: "tui",
1148
+ abortHandler: () => {
1149
+ this.restoreQueuedMessagesToEditor({ abort: true });
1150
+ },
1151
+ commandContextActions: {
1152
+ waitForIdle: () => this.session.agent.waitForIdle(),
1153
+ newSession: async (options) => {
1154
+ if (this.loadingAnimation) {
1155
+ this.loadingAnimation.stop();
1156
+ this.loadingAnimation = undefined;
1157
+ }
1158
+ this.statusContainer.clear();
1159
+ try {
1160
+ return await this.runtimeHost.newSession(options);
1161
+ }
1162
+ catch (error) {
1163
+ return this.handleFatalRuntimeError("Failed to create session", error);
1164
+ }
1165
+ },
1166
+ fork: async (entryId, options) => {
1167
+ try {
1168
+ const result = await this.runtimeHost.fork(entryId, options);
1169
+ if (!result.cancelled) {
1170
+ this.editor.setText(result.selectedText ?? "");
1171
+ this.showStatus("Forked to new session");
1172
+ }
1173
+ return { cancelled: result.cancelled };
1174
+ }
1175
+ catch (error) {
1176
+ return this.handleFatalRuntimeError("Failed to fork session", error);
1177
+ }
1178
+ },
1179
+ navigateTree: async (targetId, options) => {
1180
+ const result = await this.session.navigateTree(targetId, {
1181
+ summarize: options?.summarize,
1182
+ customInstructions: options?.customInstructions,
1183
+ replaceInstructions: options?.replaceInstructions,
1184
+ label: options?.label,
1185
+ });
1186
+ if (result.cancelled) {
1187
+ return { cancelled: true };
1188
+ }
1189
+ this.chatContainer.clear();
1190
+ this.renderInitialMessages();
1191
+ if (result.editorText && !this.editor.getText().trim()) {
1192
+ this.editor.setText(result.editorText);
1193
+ }
1194
+ this.showStatus("Navigated to selected point");
1195
+ void this.flushCompactionQueue({ willRetry: false });
1196
+ return { cancelled: false };
1197
+ },
1198
+ switchSession: async (sessionPath, options) => {
1199
+ return this.handleResumeSession(sessionPath, options);
1200
+ },
1201
+ reload: async () => {
1202
+ await this.handleReloadCommand();
1203
+ },
1204
+ },
1205
+ shutdownHandler: () => {
1206
+ this.shutdownRequested = true;
1207
+ if (!this.session.isStreaming) {
1208
+ void this.shutdown();
1209
+ }
1210
+ },
1211
+ onError: (error) => {
1212
+ this.showExtensionError(error.extensionPath, error.error, error.stack);
1213
+ },
1214
+ });
1215
+ setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
1216
+ this.setupAutocompleteProvider();
1217
+ const extensionRunner = this.session.extensionRunner;
1218
+ this.setupExtensionShortcuts(extensionRunner);
1219
+ this.showLoadedResources({ force: false, showDiagnosticsWhenQuiet: true });
1220
+ this.showStartupNoticesIfNeeded();
1221
+ }
1222
+ applyRuntimeSettings() {
1223
+ configureHttpDispatcher(this.settingsManager.getHttpIdleTimeoutMs());
1224
+ this.footer.setSession(this.session);
1225
+ this.footer.setAutoCompactEnabled(this.session.autoCompactionEnabled);
1226
+ this.footerDataProvider.setCwd(this.sessionManager.getCwd());
1227
+ this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
1228
+ this.ui.setShowHardwareCursor(this.settingsManager.getShowHardwareCursor());
1229
+ this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
1230
+ const editorPaddingX = this.settingsManager.getEditorPaddingX();
1231
+ const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
1232
+ this.defaultEditor.setPaddingX(editorPaddingX);
1233
+ this.defaultEditor.setAutocompleteMaxVisible(autocompleteMaxVisible);
1234
+ if (this.editor !== this.defaultEditor) {
1235
+ this.editor.setPaddingX?.(editorPaddingX);
1236
+ this.editor.setAutocompleteMaxVisible?.(autocompleteMaxVisible);
1237
+ }
1238
+ }
1239
+ async rebindCurrentSession(options = {}) {
1240
+ this.unsubscribe?.();
1241
+ this.unsubscribe = undefined;
1242
+ this.applyRuntimeSettings();
1243
+ if (options.renderBeforeBind) {
1244
+ this.renderCurrentSessionState();
1245
+ this.subscribeToAgent();
1246
+ await this.bindCurrentSessionExtensions();
1247
+ }
1248
+ else {
1249
+ await this.bindCurrentSessionExtensions();
1250
+ this.subscribeToAgent();
1251
+ }
1252
+ await this.updateAvailableProviderCount();
1253
+ this.updateEditorBorderColor();
1254
+ this.updateTerminalTitle();
1255
+ }
1256
+ async handleFatalRuntimeError(prefix, error) {
1257
+ const message = error instanceof Error ? error.message : String(error);
1258
+ this.showError(`${prefix}: ${message}`);
1259
+ stopThemeWatcher();
1260
+ this.stop();
1261
+ process.exit(1);
1262
+ }
1263
+ renderCurrentSessionState() {
1264
+ this.chatContainer.clear();
1265
+ this.pendingMessagesContainer.clear();
1266
+ this.compactionQueuedMessages = [];
1267
+ this.streamingComponent = undefined;
1268
+ this.streamingMessage = undefined;
1269
+ this.pendingTools.clear();
1270
+ this.renderInitialMessages();
1271
+ }
1272
+ /**
1273
+ * Get a registered tool definition by name (for custom rendering).
1274
+ */
1275
+ getRegisteredToolDefinition(toolName) {
1276
+ return this.session.getToolDefinition(toolName);
1277
+ }
1278
+ /**
1279
+ * Set up keyboard shortcuts registered by extensions.
1280
+ */
1281
+ setupExtensionShortcuts(extensionRunner) {
1282
+ const shortcuts = extensionRunner.getShortcuts(this.keybindings.getEffectiveConfig());
1283
+ if (shortcuts.size === 0)
1284
+ return;
1285
+ // Create a context for shortcut handlers
1286
+ const createContext = () => ({
1287
+ ui: this.createExtensionUIContext(),
1288
+ mode: "tui",
1289
+ hasUI: true,
1290
+ cwd: this.sessionManager.getCwd(),
1291
+ sessionManager: this.sessionManager,
1292
+ modelRegistry: this.session.modelRegistry,
1293
+ model: this.session.model,
1294
+ isIdle: () => !this.session.isStreaming,
1295
+ isProjectTrusted: () => this.settingsManager.isProjectTrusted(),
1296
+ signal: this.session.agent.signal,
1297
+ abort: () => {
1298
+ this.restoreQueuedMessagesToEditor({ abort: true });
1299
+ },
1300
+ hasPendingMessages: () => this.session.pendingMessageCount > 0,
1301
+ shutdown: () => {
1302
+ this.shutdownRequested = true;
1303
+ },
1304
+ getContextUsage: () => this.session.getContextUsage(),
1305
+ compact: (options) => {
1306
+ void (async () => {
1307
+ try {
1308
+ const result = await this.session.compact(options?.customInstructions);
1309
+ options?.onComplete?.(result);
1310
+ }
1311
+ catch (error) {
1312
+ const err = error instanceof Error ? error : new Error(String(error));
1313
+ options?.onError?.(err);
1314
+ }
1315
+ })();
1316
+ },
1317
+ getSystemPrompt: () => this.session.systemPrompt,
1318
+ });
1319
+ // Set up the extension shortcut handler on the default editor
1320
+ this.defaultEditor.onExtensionShortcut = (data) => {
1321
+ for (const [shortcutStr, shortcut] of shortcuts) {
1322
+ // Cast to KeyId - extension shortcuts use the same format
1323
+ if (matchesKey(data, shortcutStr)) {
1324
+ // Run handler async, don't block input
1325
+ Promise.resolve(shortcut.handler(createContext())).catch((err) => {
1326
+ this.showError(`Shortcut handler error: ${err instanceof Error ? err.message : String(err)}`);
1327
+ });
1328
+ return true;
1329
+ }
1330
+ }
1331
+ return false;
1332
+ };
1333
+ }
1334
+ /**
1335
+ * Set extension status text in the footer.
1336
+ */
1337
+ setExtensionStatus(key, text) {
1338
+ this.footerDataProvider.setExtensionStatus(key, text);
1339
+ this.ui.requestRender();
1340
+ }
1341
+ getWorkingLoaderMessage() {
1342
+ return this.workingMessage ?? this.defaultWorkingMessage;
1343
+ }
1344
+ createWorkingLoader() {
1345
+ return new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), this.getWorkingLoaderMessage(), this.workingIndicatorOptions);
1346
+ }
1347
+ stopWorkingLoader() {
1348
+ if (this.loadingAnimation) {
1349
+ this.loadingAnimation.stop();
1350
+ this.loadingAnimation = undefined;
1351
+ }
1352
+ this.statusContainer.clear();
1353
+ }
1354
+ setWorkingVisible(visible) {
1355
+ this.workingVisible = visible;
1356
+ if (!visible) {
1357
+ this.stopWorkingLoader();
1358
+ this.ui.requestRender();
1359
+ return;
1360
+ }
1361
+ if (this.session.isStreaming && !this.loadingAnimation) {
1362
+ this.statusContainer.clear();
1363
+ this.loadingAnimation = this.createWorkingLoader();
1364
+ this.statusContainer.addChild(this.loadingAnimation);
1365
+ }
1366
+ this.ui.requestRender();
1367
+ }
1368
+ setWorkingIndicator(options) {
1369
+ this.workingIndicatorOptions = options;
1370
+ this.loadingAnimation?.setIndicator(options);
1371
+ this.ui.requestRender();
1372
+ }
1373
+ setHiddenThinkingLabel(label) {
1374
+ this.hiddenThinkingLabel = label ?? this.defaultHiddenThinkingLabel;
1375
+ for (const child of this.chatContainer.children) {
1376
+ if (child instanceof AssistantMessageComponent) {
1377
+ child.setHiddenThinkingLabel(this.hiddenThinkingLabel);
1378
+ }
1379
+ }
1380
+ if (this.streamingComponent) {
1381
+ this.streamingComponent.setHiddenThinkingLabel(this.hiddenThinkingLabel);
1382
+ }
1383
+ this.ui.requestRender();
1384
+ }
1385
+ /**
1386
+ * Set an extension widget (string array or custom component).
1387
+ */
1388
+ setExtensionWidget(key, content, options) {
1389
+ const placement = options?.placement ?? "aboveEditor";
1390
+ const removeExisting = (map) => {
1391
+ const existing = map.get(key);
1392
+ if (existing?.dispose)
1393
+ existing.dispose();
1394
+ map.delete(key);
1395
+ };
1396
+ removeExisting(this.extensionWidgetsAbove);
1397
+ removeExisting(this.extensionWidgetsBelow);
1398
+ if (content === undefined) {
1399
+ this.renderWidgets();
1400
+ return;
1401
+ }
1402
+ let component;
1403
+ if (Array.isArray(content)) {
1404
+ // Wrap string array in a Container with Text components
1405
+ const container = new Container();
1406
+ for (const line of content.slice(0, InteractiveMode.MAX_WIDGET_LINES)) {
1407
+ container.addChild(new Text(line, 1, 0));
1408
+ }
1409
+ if (content.length > InteractiveMode.MAX_WIDGET_LINES) {
1410
+ container.addChild(new Text(theme.fg("muted", "... (widget truncated)"), 1, 0));
1411
+ }
1412
+ component = container;
1413
+ }
1414
+ else {
1415
+ // Factory function - create component
1416
+ component = content(this.ui, theme);
1417
+ }
1418
+ const targetMap = placement === "belowEditor" ? this.extensionWidgetsBelow : this.extensionWidgetsAbove;
1419
+ targetMap.set(key, component);
1420
+ this.renderWidgets();
1421
+ }
1422
+ clearExtensionWidgets() {
1423
+ for (const widget of this.extensionWidgetsAbove.values()) {
1424
+ widget.dispose?.();
1425
+ }
1426
+ for (const widget of this.extensionWidgetsBelow.values()) {
1427
+ widget.dispose?.();
1428
+ }
1429
+ this.extensionWidgetsAbove.clear();
1430
+ this.extensionWidgetsBelow.clear();
1431
+ this.renderWidgets();
1432
+ }
1433
+ resetExtensionUI() {
1434
+ if (this.extensionSelector) {
1435
+ this.hideExtensionSelector();
1436
+ }
1437
+ if (this.extensionInput) {
1438
+ this.hideExtensionInput();
1439
+ }
1440
+ if (this.extensionEditor) {
1441
+ this.hideExtensionEditor();
1442
+ }
1443
+ this.ui.hideOverlay();
1444
+ this.clearExtensionTerminalInputListeners();
1445
+ this.setExtensionFooter(undefined);
1446
+ this.setExtensionHeader(undefined);
1447
+ this.clearExtensionWidgets();
1448
+ this.footerDataProvider.clearExtensionStatuses();
1449
+ this.footer.invalidate();
1450
+ this.autocompleteProviderWrappers = [];
1451
+ this.setCustomEditorComponent(undefined);
1452
+ this.setupAutocompleteProvider();
1453
+ this.defaultEditor.onExtensionShortcut = undefined;
1454
+ this.updateTerminalTitle();
1455
+ this.workingMessage = undefined;
1456
+ this.workingVisible = true;
1457
+ this.setWorkingIndicator();
1458
+ if (this.loadingAnimation) {
1459
+ this.loadingAnimation.setMessage(`${this.defaultWorkingMessage} (${keyText("app.interrupt")} to interrupt)`);
1460
+ }
1461
+ this.setHiddenThinkingLabel();
1462
+ }
1463
+ // Maximum total widget lines to prevent viewport overflow
1464
+ static MAX_WIDGET_LINES = 10;
1465
+ /**
1466
+ * Render all extension widgets to the widget container.
1467
+ */
1468
+ renderWidgets() {
1469
+ if (!this.widgetContainerAbove || !this.widgetContainerBelow)
1470
+ return;
1471
+ this.renderWidgetContainer(this.widgetContainerAbove, this.extensionWidgetsAbove, true, true);
1472
+ this.renderWidgetContainer(this.widgetContainerBelow, this.extensionWidgetsBelow, false, false);
1473
+ this.ui.requestRender();
1474
+ }
1475
+ renderWidgetContainer(container, widgets, spacerWhenEmpty, leadingSpacer) {
1476
+ container.clear();
1477
+ if (widgets.size === 0) {
1478
+ if (spacerWhenEmpty) {
1479
+ container.addChild(new Spacer(1));
1480
+ }
1481
+ return;
1482
+ }
1483
+ if (leadingSpacer) {
1484
+ container.addChild(new Spacer(1));
1485
+ }
1486
+ for (const component of widgets.values()) {
1487
+ container.addChild(component);
1488
+ }
1489
+ }
1490
+ /**
1491
+ * Set a custom footer component, or restore the built-in footer.
1492
+ */
1493
+ setExtensionFooter(factory) {
1494
+ // Dispose existing custom footer
1495
+ if (this.customFooter?.dispose) {
1496
+ this.customFooter.dispose();
1497
+ }
1498
+ // Remove current footer from UI
1499
+ if (this.customFooter) {
1500
+ this.ui.removeChild(this.customFooter);
1501
+ }
1502
+ else {
1503
+ this.ui.removeChild(this.footer);
1504
+ }
1505
+ if (factory) {
1506
+ // Create and add custom footer, passing the data provider
1507
+ this.customFooter = factory(this.ui, theme, this.footerDataProvider);
1508
+ this.ui.addChild(this.customFooter);
1509
+ }
1510
+ else {
1511
+ // Restore built-in footer
1512
+ this.customFooter = undefined;
1513
+ this.ui.addChild(this.footer);
1514
+ }
1515
+ this.ui.requestRender();
1516
+ }
1517
+ /**
1518
+ * Set a custom header component, or restore the built-in header.
1519
+ */
1520
+ setExtensionHeader(factory) {
1521
+ // Header may not be initialized yet if called during early initialization
1522
+ if (!this.builtInHeader) {
1523
+ return;
1524
+ }
1525
+ // Dispose existing custom header
1526
+ if (this.customHeader?.dispose) {
1527
+ this.customHeader.dispose();
1528
+ }
1529
+ // Find the index of the current header in the header container
1530
+ const currentHeader = this.customHeader || this.builtInHeader;
1531
+ const index = this.headerContainer.children.indexOf(currentHeader);
1532
+ if (factory) {
1533
+ // Create and add custom header
1534
+ this.customHeader = factory(this.ui, theme);
1535
+ if (isExpandable(this.customHeader)) {
1536
+ this.customHeader.setExpanded(this.toolOutputExpanded);
1537
+ }
1538
+ if (index !== -1) {
1539
+ this.headerContainer.children[index] = this.customHeader;
1540
+ }
1541
+ else {
1542
+ // If not found (e.g. builtInHeader was never added), add at the top
1543
+ this.headerContainer.children.unshift(this.customHeader);
1544
+ }
1545
+ }
1546
+ else {
1547
+ // Restore built-in header
1548
+ this.customHeader = undefined;
1549
+ if (isExpandable(this.builtInHeader)) {
1550
+ this.builtInHeader.setExpanded(this.toolOutputExpanded);
1551
+ }
1552
+ if (index !== -1) {
1553
+ this.headerContainer.children[index] = this.builtInHeader;
1554
+ }
1555
+ }
1556
+ this.ui.requestRender();
1557
+ }
1558
+ addExtensionTerminalInputListener(handler) {
1559
+ const unsubscribe = this.ui.addInputListener(handler);
1560
+ this.extensionTerminalInputUnsubscribers.add(unsubscribe);
1561
+ return () => {
1562
+ unsubscribe();
1563
+ this.extensionTerminalInputUnsubscribers.delete(unsubscribe);
1564
+ };
1565
+ }
1566
+ clearExtensionTerminalInputListeners() {
1567
+ for (const unsubscribe of this.extensionTerminalInputUnsubscribers) {
1568
+ unsubscribe();
1569
+ }
1570
+ this.extensionTerminalInputUnsubscribers.clear();
1571
+ }
1572
+ /**
1573
+ * Create the ExtensionUIContext for extensions.
1574
+ */
1575
+ createProjectTrustContext(cwd) {
1576
+ const ui = this.createExtensionUIContext();
1577
+ return {
1578
+ cwd,
1579
+ mode: "tui",
1580
+ hasUI: true,
1581
+ ui: {
1582
+ select: ui.select,
1583
+ confirm: ui.confirm,
1584
+ input: ui.input,
1585
+ notify: ui.notify,
1586
+ },
1587
+ };
1588
+ }
1589
+ createExtensionUIContext() {
1590
+ return {
1591
+ select: (title, options, opts) => this.showExtensionSelector(title, options, opts),
1592
+ confirm: (title, message, opts) => this.showExtensionConfirm(title, message, opts),
1593
+ input: (title, placeholder, opts) => this.showExtensionInput(title, placeholder, opts),
1594
+ notify: (message, type) => this.showExtensionNotify(message, type),
1595
+ onTerminalInput: (handler) => this.addExtensionTerminalInputListener(handler),
1596
+ setStatus: (key, text) => this.setExtensionStatus(key, text),
1597
+ setWorkingMessage: (message) => {
1598
+ this.workingMessage = message;
1599
+ if (this.loadingAnimation) {
1600
+ this.loadingAnimation.setMessage(message ?? this.defaultWorkingMessage);
1601
+ }
1602
+ },
1603
+ setWorkingVisible: (visible) => this.setWorkingVisible(visible),
1604
+ setWorkingIndicator: (options) => this.setWorkingIndicator(options),
1605
+ setHiddenThinkingLabel: (label) => this.setHiddenThinkingLabel(label),
1606
+ setWidget: (key, content, options) => this.setExtensionWidget(key, content, options),
1607
+ setFooter: (factory) => this.setExtensionFooter(factory),
1608
+ setHeader: (factory) => this.setExtensionHeader(factory),
1609
+ setTitle: (title) => this.ui.terminal.setTitle(title),
1610
+ custom: (factory, options) => this.showExtensionCustom(factory, options),
1611
+ pasteToEditor: (text) => this.editor.handleInput(`\x1b[200~${text}\x1b[201~`),
1612
+ setEditorText: (text) => this.editor.setText(text),
1613
+ getEditorText: () => this.editor.getExpandedText?.() ?? this.editor.getText(),
1614
+ editor: (title, prefill) => this.showExtensionEditor(title, prefill),
1615
+ addAutocompleteProvider: (factory) => {
1616
+ this.autocompleteProviderWrappers.push(factory);
1617
+ this.setupAutocompleteProvider();
1618
+ },
1619
+ setEditorComponent: (factory) => this.setCustomEditorComponent(factory),
1620
+ getEditorComponent: () => this.editorComponentFactory,
1621
+ get theme() {
1622
+ return theme;
1623
+ },
1624
+ getAllThemes: () => getAvailableThemesWithPaths(),
1625
+ getTheme: (name) => getThemeByName(name),
1626
+ setTheme: (themeOrName) => {
1627
+ if (themeOrName instanceof Theme) {
1628
+ return this.themeController.setThemeInstance(themeOrName);
1629
+ }
1630
+ const result = this.themeController.setThemeName(themeOrName);
1631
+ if (result.success) {
1632
+ if (this.settingsManager.getTheme() !== themeOrName) {
1633
+ this.settingsManager.setTheme(themeOrName);
1634
+ }
1635
+ }
1636
+ return result;
1637
+ },
1638
+ getToolsExpanded: () => this.toolOutputExpanded,
1639
+ setToolsExpanded: (expanded) => this.setToolsExpanded(expanded),
1640
+ };
1641
+ }
1642
+ /**
1643
+ * Show a selector for extensions.
1644
+ */
1645
+ showExtensionSelector(title, options, opts) {
1646
+ return new Promise((resolve) => {
1647
+ if (opts?.signal?.aborted) {
1648
+ resolve(undefined);
1649
+ return;
1650
+ }
1651
+ const onAbort = () => {
1652
+ this.hideExtensionSelector();
1653
+ resolve(undefined);
1654
+ };
1655
+ opts?.signal?.addEventListener("abort", onAbort, { once: true });
1656
+ this.extensionSelector = new ExtensionSelectorComponent(title, options, (option) => {
1657
+ opts?.signal?.removeEventListener("abort", onAbort);
1658
+ this.hideExtensionSelector();
1659
+ resolve(option);
1660
+ }, () => {
1661
+ opts?.signal?.removeEventListener("abort", onAbort);
1662
+ this.hideExtensionSelector();
1663
+ resolve(undefined);
1664
+ }, { tui: this.ui, timeout: opts?.timeout, onToggleToolsExpanded: () => this.toggleToolOutputExpansion() });
1665
+ this.editorContainer.clear();
1666
+ this.editorContainer.addChild(this.extensionSelector);
1667
+ this.ui.setFocus(this.extensionSelector);
1668
+ this.ui.requestRender();
1669
+ });
1670
+ }
1671
+ /**
1672
+ * Hide the extension selector.
1673
+ */
1674
+ hideExtensionSelector() {
1675
+ this.extensionSelector?.dispose();
1676
+ this.editorContainer.clear();
1677
+ this.editorContainer.addChild(this.editor);
1678
+ this.extensionSelector = undefined;
1679
+ this.ui.setFocus(this.editor);
1680
+ this.ui.requestRender();
1681
+ }
1682
+ /**
1683
+ * Show a confirmation dialog for extensions.
1684
+ */
1685
+ async showExtensionConfirm(title, message, opts) {
1686
+ const result = await this.showExtensionSelector(`${title}\n${message}`, ["Yes", "No"], opts);
1687
+ return result === "Yes";
1688
+ }
1689
+ async promptForMissingSessionCwd(error) {
1690
+ const confirmed = await this.showExtensionConfirm("Session cwd not found", formatMissingSessionCwdPrompt(error.issue));
1691
+ return confirmed ? error.issue.fallbackCwd : undefined;
1692
+ }
1693
+ /**
1694
+ * Show a text input for extensions.
1695
+ */
1696
+ showExtensionInput(title, placeholder, opts) {
1697
+ return new Promise((resolve) => {
1698
+ if (opts?.signal?.aborted) {
1699
+ resolve(undefined);
1700
+ return;
1701
+ }
1702
+ const onAbort = () => {
1703
+ this.hideExtensionInput();
1704
+ resolve(undefined);
1705
+ };
1706
+ opts?.signal?.addEventListener("abort", onAbort, { once: true });
1707
+ this.extensionInput = new ExtensionInputComponent(title, placeholder, (value) => {
1708
+ opts?.signal?.removeEventListener("abort", onAbort);
1709
+ this.hideExtensionInput();
1710
+ resolve(value);
1711
+ }, () => {
1712
+ opts?.signal?.removeEventListener("abort", onAbort);
1713
+ this.hideExtensionInput();
1714
+ resolve(undefined);
1715
+ }, { tui: this.ui, timeout: opts?.timeout });
1716
+ this.editorContainer.clear();
1717
+ this.editorContainer.addChild(this.extensionInput);
1718
+ this.ui.setFocus(this.extensionInput);
1719
+ this.ui.requestRender();
1720
+ });
1721
+ }
1722
+ /**
1723
+ * Hide the extension input.
1724
+ */
1725
+ hideExtensionInput() {
1726
+ this.extensionInput?.dispose();
1727
+ this.editorContainer.clear();
1728
+ this.editorContainer.addChild(this.editor);
1729
+ this.extensionInput = undefined;
1730
+ this.ui.setFocus(this.editor);
1731
+ this.ui.requestRender();
1732
+ }
1733
+ /**
1734
+ * Show a multi-line editor for extensions (with Ctrl+G support).
1735
+ */
1736
+ showExtensionEditor(title, prefill) {
1737
+ return new Promise((resolve) => {
1738
+ this.extensionEditor = new ExtensionEditorComponent(this.ui, this.keybindings, title, prefill, (value) => {
1739
+ this.hideExtensionEditor();
1740
+ resolve(value);
1741
+ }, () => {
1742
+ this.hideExtensionEditor();
1743
+ resolve(undefined);
1744
+ });
1745
+ this.editorContainer.clear();
1746
+ this.editorContainer.addChild(this.extensionEditor);
1747
+ this.ui.setFocus(this.extensionEditor);
1748
+ this.ui.requestRender();
1749
+ });
1750
+ }
1751
+ /**
1752
+ * Hide the extension editor.
1753
+ */
1754
+ hideExtensionEditor() {
1755
+ this.editorContainer.clear();
1756
+ this.editorContainer.addChild(this.editor);
1757
+ this.extensionEditor = undefined;
1758
+ this.ui.setFocus(this.editor);
1759
+ this.ui.requestRender();
1760
+ }
1761
+ /**
1762
+ * Set a custom editor component from an extension.
1763
+ * Pass undefined to restore the default editor.
1764
+ */
1765
+ setCustomEditorComponent(factory) {
1766
+ this.editorComponentFactory = factory;
1767
+ // Save text from current editor before switching
1768
+ const currentText = this.editor.getText();
1769
+ this.editorContainer.clear();
1770
+ if (factory) {
1771
+ // Create the custom editor with tui, theme, and keybindings
1772
+ const newEditor = factory(this.ui, getEditorTheme(), this.keybindings);
1773
+ // Wire up callbacks from the default editor
1774
+ newEditor.onSubmit = this.defaultEditor.onSubmit;
1775
+ newEditor.onChange = this.defaultEditor.onChange;
1776
+ // Copy text from previous editor
1777
+ newEditor.setText(currentText);
1778
+ // Copy appearance settings if supported
1779
+ if (newEditor.borderColor !== undefined) {
1780
+ newEditor.borderColor = this.defaultEditor.borderColor;
1781
+ }
1782
+ if (newEditor.setPaddingX !== undefined) {
1783
+ newEditor.setPaddingX(this.defaultEditor.getPaddingX());
1784
+ }
1785
+ // Set autocomplete if supported
1786
+ if (newEditor.setAutocompleteProvider && this.autocompleteProvider) {
1787
+ newEditor.setAutocompleteProvider(this.autocompleteProvider);
1788
+ }
1789
+ // If extending CustomEditor, copy app-level handlers
1790
+ // Use duck typing since instanceof fails across jiti module boundaries
1791
+ const customEditor = newEditor;
1792
+ if ("actionHandlers" in customEditor && customEditor.actionHandlers instanceof Map) {
1793
+ if (!customEditor.onEscape) {
1794
+ customEditor.onEscape = () => this.defaultEditor.onEscape?.();
1795
+ }
1796
+ if (!customEditor.onCtrlD) {
1797
+ customEditor.onCtrlD = () => this.defaultEditor.onCtrlD?.();
1798
+ }
1799
+ if (!customEditor.onPasteImage) {
1800
+ customEditor.onPasteImage = () => this.defaultEditor.onPasteImage?.();
1801
+ }
1802
+ if (!customEditor.onExtensionShortcut) {
1803
+ customEditor.onExtensionShortcut = (data) => this.defaultEditor.onExtensionShortcut?.(data);
1804
+ }
1805
+ // Copy action handlers (clear, suspend, model switching, etc.)
1806
+ for (const [action, handler] of this.defaultEditor.actionHandlers) {
1807
+ customEditor.actionHandlers.set(action, handler);
1808
+ }
1809
+ }
1810
+ this.editor = newEditor;
1811
+ }
1812
+ else {
1813
+ // Restore default editor with text from custom editor
1814
+ this.defaultEditor.setText(currentText);
1815
+ this.editor = this.defaultEditor;
1816
+ }
1817
+ this.editorContainer.addChild(this.editor);
1818
+ this.ui.setFocus(this.editor);
1819
+ this.ui.requestRender();
1820
+ }
1821
+ /**
1822
+ * Show a notification for extensions.
1823
+ */
1824
+ showExtensionNotify(message, type) {
1825
+ if (type === "error") {
1826
+ this.showError(message);
1827
+ }
1828
+ else if (type === "warning") {
1829
+ this.showWarning(message);
1830
+ }
1831
+ else {
1832
+ this.showStatus(message);
1833
+ }
1834
+ }
1835
+ /** Show a custom component with keyboard focus. Overlay mode renders on top of existing content. */
1836
+ async showExtensionCustom(factory, options) {
1837
+ const savedText = this.editor.getText();
1838
+ const isOverlay = options?.overlay ?? false;
1839
+ const restoreEditor = () => {
1840
+ this.editorContainer.clear();
1841
+ this.editorContainer.addChild(this.editor);
1842
+ this.editor.setText(savedText);
1843
+ this.ui.setFocus(this.editor);
1844
+ this.ui.requestRender();
1845
+ };
1846
+ return new Promise((resolve, reject) => {
1847
+ let component;
1848
+ let closed = false;
1849
+ const close = (result) => {
1850
+ if (closed)
1851
+ return;
1852
+ closed = true;
1853
+ if (isOverlay)
1854
+ this.ui.hideOverlay();
1855
+ else
1856
+ restoreEditor();
1857
+ // Note: both branches above already call requestRender
1858
+ resolve(result);
1859
+ try {
1860
+ component?.dispose?.();
1861
+ }
1862
+ catch {
1863
+ /* ignore dispose errors */
1864
+ }
1865
+ };
1866
+ Promise.resolve(factory(this.ui, theme, this.keybindings, close))
1867
+ .then((c) => {
1868
+ if (closed)
1869
+ return;
1870
+ component = c;
1871
+ if (isOverlay) {
1872
+ // Resolve overlay options - can be static or dynamic function
1873
+ const resolveOptions = () => {
1874
+ if (options?.overlayOptions) {
1875
+ const opts = typeof options.overlayOptions === "function"
1876
+ ? options.overlayOptions()
1877
+ : options.overlayOptions;
1878
+ return opts;
1879
+ }
1880
+ // Fallback: use component's width property if available
1881
+ const w = component.width;
1882
+ return w ? { width: w } : undefined;
1883
+ };
1884
+ const handle = this.ui.showOverlay(component, resolveOptions());
1885
+ // Expose handle to caller for visibility control
1886
+ options?.onHandle?.(handle);
1887
+ }
1888
+ else {
1889
+ this.editorContainer.clear();
1890
+ this.editorContainer.addChild(component);
1891
+ this.ui.setFocus(component);
1892
+ this.ui.requestRender();
1893
+ }
1894
+ })
1895
+ .catch((err) => {
1896
+ if (closed)
1897
+ return;
1898
+ if (!isOverlay)
1899
+ restoreEditor();
1900
+ reject(err);
1901
+ });
1902
+ });
1903
+ }
1904
+ /**
1905
+ * Show an extension error in the UI.
1906
+ */
1907
+ showExtensionError(extensionPath, error, stack) {
1908
+ const errorMsg = `Extension "${extensionPath}" error: ${error}`;
1909
+ const errorText = new Text(theme.fg("error", errorMsg), 1, 0);
1910
+ this.chatContainer.addChild(errorText);
1911
+ if (stack) {
1912
+ // Show stack trace in dim color, indented
1913
+ const stackLines = stack
1914
+ .split("\n")
1915
+ .slice(1) // Skip first line (duplicates error message)
1916
+ .map((line) => theme.fg("dim", ` ${line.trim()}`))
1917
+ .join("\n");
1918
+ if (stackLines) {
1919
+ this.chatContainer.addChild(new Text(stackLines, 1, 0));
1920
+ }
1921
+ }
1922
+ this.ui.requestRender();
1923
+ }
1924
+ // =========================================================================
1925
+ // Key Handlers
1926
+ // =========================================================================
1927
+ setupKeyHandlers() {
1928
+ // Set up handlers on defaultEditor - they use this.editor for text access
1929
+ // so they work correctly regardless of which editor is active
1930
+ this.defaultEditor.onEscape = () => {
1931
+ if (this.session.isStreaming) {
1932
+ this.restoreQueuedMessagesToEditor({ abort: true });
1933
+ }
1934
+ else if (this.session.isBashRunning) {
1935
+ this.session.abortBash();
1936
+ }
1937
+ else if (this.isBashMode) {
1938
+ this.editor.setText("");
1939
+ this.isBashMode = false;
1940
+ this.updateEditorBorderColor();
1941
+ }
1942
+ else if (!this.editor.getText().trim()) {
1943
+ // Double-escape with empty editor triggers /tree, /fork, or nothing based on setting
1944
+ const action = this.settingsManager.getDoubleEscapeAction();
1945
+ if (action !== "none") {
1946
+ const now = Date.now();
1947
+ if (now - this.lastEscapeTime < 500) {
1948
+ if (action === "tree") {
1949
+ this.showTreeSelector();
1950
+ }
1951
+ else {
1952
+ this.showUserMessageSelector();
1953
+ }
1954
+ this.lastEscapeTime = 0;
1955
+ }
1956
+ else {
1957
+ this.lastEscapeTime = now;
1958
+ }
1959
+ }
1960
+ }
1961
+ };
1962
+ // Register app action handlers
1963
+ this.defaultEditor.onAction("app.clear", () => this.handleCtrlC());
1964
+ this.defaultEditor.onCtrlD = () => this.handleCtrlD();
1965
+ this.defaultEditor.onAction("app.suspend", () => this.handleCtrlZ());
1966
+ this.defaultEditor.onAction("app.thinking.cycle", () => this.cycleThinkingLevel());
1967
+ this.defaultEditor.onAction("app.model.cycleForward", () => this.cycleModel("forward"));
1968
+ this.defaultEditor.onAction("app.model.cycleBackward", () => this.cycleModel("backward"));
1969
+ // Global debug handler on TUI (works regardless of focus)
1970
+ this.ui.onDebug = () => this.handleDebugCommand();
1971
+ this.defaultEditor.onAction("app.model.select", () => this.showModelSelector());
1972
+ this.defaultEditor.onAction("app.tools.expand", () => this.toggleToolOutputExpansion());
1973
+ this.defaultEditor.onAction("app.thinking.toggle", () => this.toggleThinkingBlockVisibility());
1974
+ this.defaultEditor.onAction("app.editor.external", () => this.openExternalEditor());
1975
+ this.defaultEditor.onAction("app.message.followUp", () => this.handleFollowUp());
1976
+ this.defaultEditor.onAction("app.message.dequeue", () => this.handleDequeue());
1977
+ this.defaultEditor.onAction("app.session.new", () => this.handleClearCommand());
1978
+ this.defaultEditor.onAction("app.session.tree", () => this.showTreeSelector());
1979
+ this.defaultEditor.onAction("app.session.fork", () => this.showUserMessageSelector());
1980
+ this.defaultEditor.onAction("app.session.resume", () => this.showSessionSelector());
1981
+ this.defaultEditor.onChange = (text) => {
1982
+ const wasBashMode = this.isBashMode;
1983
+ this.isBashMode = text.trimStart().startsWith("!");
1984
+ if (wasBashMode !== this.isBashMode) {
1985
+ this.updateEditorBorderColor();
1986
+ }
1987
+ };
1988
+ // Handle clipboard image paste (triggered on Ctrl+V)
1989
+ this.defaultEditor.onPasteImage = () => {
1990
+ this.handleClipboardImagePaste();
1991
+ };
1992
+ }
1993
+ async handleClipboardImagePaste() {
1994
+ try {
1995
+ const image = await readClipboardImage();
1996
+ if (!image) {
1997
+ return;
1998
+ }
1999
+ // Write to temp file
2000
+ const tmpDir = os.tmpdir();
2001
+ const ext = extensionForImageMimeType(image.mimeType) ?? "png";
2002
+ const fileName = `pi-clipboard-${crypto.randomUUID()}.${ext}`;
2003
+ const filePath = path.join(tmpDir, fileName);
2004
+ fs.writeFileSync(filePath, Buffer.from(image.bytes));
2005
+ // Insert file path directly
2006
+ this.editor.insertTextAtCursor?.(filePath);
2007
+ this.ui.requestRender();
2008
+ }
2009
+ catch {
2010
+ // Silently ignore clipboard errors (may not have permission, etc.)
2011
+ }
2012
+ }
2013
+ setupEditorSubmitHandler() {
2014
+ this.defaultEditor.onSubmit = async (text) => {
2015
+ text = text.trim();
2016
+ if (!text)
2017
+ return;
2018
+ // Handle commands
2019
+ if (text === "/settings") {
2020
+ this.showSettingsSelector();
2021
+ this.editor.setText("");
2022
+ return;
2023
+ }
2024
+ if (text === "/scoped-models") {
2025
+ this.editor.setText("");
2026
+ await this.showModelsSelector();
2027
+ return;
2028
+ }
2029
+ if (text === "/model" || text.startsWith("/model ")) {
2030
+ const searchTerm = text.startsWith("/model ") ? text.slice(7).trim() : undefined;
2031
+ this.editor.setText("");
2032
+ await this.handleModelCommand(searchTerm);
2033
+ return;
2034
+ }
2035
+ if (text === "/export" || text.startsWith("/export ")) {
2036
+ await this.handleExportCommand(text);
2037
+ this.editor.setText("");
2038
+ return;
2039
+ }
2040
+ if (text === "/import" || text.startsWith("/import ")) {
2041
+ await this.handleImportCommand(text);
2042
+ this.editor.setText("");
2043
+ return;
2044
+ }
2045
+ if (text === "/share") {
2046
+ await this.handleShareCommand();
2047
+ this.editor.setText("");
2048
+ return;
2049
+ }
2050
+ if (text === "/copy") {
2051
+ await this.handleCopyCommand();
2052
+ this.editor.setText("");
2053
+ return;
2054
+ }
2055
+ if (text === "/name" || text.startsWith("/name ")) {
2056
+ this.handleNameCommand(text);
2057
+ this.editor.setText("");
2058
+ return;
2059
+ }
2060
+ if (text === "/session") {
2061
+ this.handleSessionCommand();
2062
+ this.editor.setText("");
2063
+ return;
2064
+ }
2065
+ if (text === "/changelog") {
2066
+ this.handleChangelogCommand();
2067
+ this.editor.setText("");
2068
+ return;
2069
+ }
2070
+ if (text === "/hotkeys") {
2071
+ this.handleHotkeysCommand();
2072
+ this.editor.setText("");
2073
+ return;
2074
+ }
2075
+ if (text === "/fork") {
2076
+ this.showUserMessageSelector();
2077
+ this.editor.setText("");
2078
+ return;
2079
+ }
2080
+ if (text === "/clone") {
2081
+ this.editor.setText("");
2082
+ await this.handleCloneCommand();
2083
+ return;
2084
+ }
2085
+ if (text === "/tree") {
2086
+ this.showTreeSelector();
2087
+ this.editor.setText("");
2088
+ return;
2089
+ }
2090
+ if (text === "/trust") {
2091
+ this.showTrustSelector();
2092
+ this.editor.setText("");
2093
+ return;
2094
+ }
2095
+ if (text === "/login") {
2096
+ this.showOAuthSelector("login");
2097
+ this.editor.setText("");
2098
+ return;
2099
+ }
2100
+ if (text === "/logout") {
2101
+ this.showOAuthSelector("logout");
2102
+ this.editor.setText("");
2103
+ return;
2104
+ }
2105
+ if (text === "/new") {
2106
+ this.editor.setText("");
2107
+ await this.handleClearCommand();
2108
+ return;
2109
+ }
2110
+ if (text === "/compact" || text.startsWith("/compact ")) {
2111
+ const customInstructions = text.startsWith("/compact ") ? text.slice(9).trim() : undefined;
2112
+ this.editor.setText("");
2113
+ await this.handleCompactCommand(customInstructions);
2114
+ return;
2115
+ }
2116
+ if (text === "/reload") {
2117
+ this.editor.setText("");
2118
+ await this.handleReloadCommand();
2119
+ return;
2120
+ }
2121
+ if (text === "/debug") {
2122
+ this.handleDebugCommand();
2123
+ this.editor.setText("");
2124
+ return;
2125
+ }
2126
+ if (text === "/arminsayshi") {
2127
+ this.handleArminSaysHi();
2128
+ this.editor.setText("");
2129
+ return;
2130
+ }
2131
+ if (text === "/dementedelves") {
2132
+ this.handleDementedDelves();
2133
+ this.editor.setText("");
2134
+ return;
2135
+ }
2136
+ if (text === "/resume") {
2137
+ this.showSessionSelector();
2138
+ this.editor.setText("");
2139
+ return;
2140
+ }
2141
+ if (text === "/quit") {
2142
+ this.editor.setText("");
2143
+ await this.shutdown();
2144
+ return;
2145
+ }
2146
+ // Handle bash command (! for normal, !! for excluded from context)
2147
+ if (text.startsWith("!")) {
2148
+ const isExcluded = text.startsWith("!!");
2149
+ const command = isExcluded ? text.slice(2).trim() : text.slice(1).trim();
2150
+ if (command) {
2151
+ if (this.session.isBashRunning) {
2152
+ this.showWarning("A bash command is already running. Press Esc to cancel it first.");
2153
+ this.editor.setText(text);
2154
+ return;
2155
+ }
2156
+ this.editor.addToHistory?.(text);
2157
+ await this.handleBashCommand(command, isExcluded);
2158
+ this.isBashMode = false;
2159
+ this.updateEditorBorderColor();
2160
+ return;
2161
+ }
2162
+ }
2163
+ // Queue input during compaction (extension commands execute immediately)
2164
+ if (this.session.isCompacting) {
2165
+ if (this.isExtensionCommand(text)) {
2166
+ this.editor.addToHistory?.(text);
2167
+ this.editor.setText("");
2168
+ await this.session.prompt(text);
2169
+ }
2170
+ else {
2171
+ this.queueCompactionMessage(text, "steer");
2172
+ }
2173
+ return;
2174
+ }
2175
+ // If streaming, use prompt() with steer behavior
2176
+ // This handles extension commands (execute immediately), prompt template expansion, and queueing
2177
+ if (this.session.isStreaming) {
2178
+ this.editor.addToHistory?.(text);
2179
+ this.editor.setText("");
2180
+ await this.session.prompt(text, { streamingBehavior: "steer" });
2181
+ this.updatePendingMessagesDisplay();
2182
+ this.ui.requestRender();
2183
+ return;
2184
+ }
2185
+ // Normal message submission
2186
+ // First, move any pending bash components to chat
2187
+ this.flushPendingBashComponents();
2188
+ if (this.onInputCallback) {
2189
+ this.onInputCallback(text);
2190
+ }
2191
+ else {
2192
+ this.pendingUserInputs.push(text);
2193
+ }
2194
+ this.editor.addToHistory?.(text);
2195
+ };
2196
+ }
2197
+ subscribeToAgent() {
2198
+ this.unsubscribe = this.session.subscribe(async (event) => {
2199
+ await this.handleEvent(event);
2200
+ });
2201
+ }
2202
+ async handleEvent(event) {
2203
+ if (!this.isInitialized) {
2204
+ await this.init();
2205
+ }
2206
+ this.footer.invalidate();
2207
+ switch (event.type) {
2208
+ case "agent_start":
2209
+ this.pendingTools.clear();
2210
+ if (this.settingsManager.getShowTerminalProgress()) {
2211
+ this.ui.terminal.setProgress(true);
2212
+ }
2213
+ // Restore main escape handler if retry handler is still active
2214
+ // (retry success event fires later, but we need main handler now)
2215
+ if (this.retryEscapeHandler) {
2216
+ this.defaultEditor.onEscape = this.retryEscapeHandler;
2217
+ this.retryEscapeHandler = undefined;
2218
+ }
2219
+ if (this.retryCountdown) {
2220
+ this.retryCountdown.dispose();
2221
+ this.retryCountdown = undefined;
2222
+ }
2223
+ if (this.retryLoader) {
2224
+ this.retryLoader.stop();
2225
+ this.retryLoader = undefined;
2226
+ }
2227
+ this.stopWorkingLoader();
2228
+ if (this.workingVisible) {
2229
+ this.loadingAnimation = this.createWorkingLoader();
2230
+ this.statusContainer.addChild(this.loadingAnimation);
2231
+ }
2232
+ this.ui.requestRender();
2233
+ break;
2234
+ case "queue_update":
2235
+ this.updatePendingMessagesDisplay();
2236
+ this.ui.requestRender();
2237
+ break;
2238
+ case "session_info_changed":
2239
+ this.updateTerminalTitle();
2240
+ this.footer.invalidate();
2241
+ this.ui.requestRender();
2242
+ break;
2243
+ case "thinking_level_changed":
2244
+ this.footer.invalidate();
2245
+ this.updateEditorBorderColor();
2246
+ break;
2247
+ case "message_start":
2248
+ if (event.message.role === "custom") {
2249
+ this.addMessageToChat(event.message);
2250
+ this.ui.requestRender();
2251
+ }
2252
+ else if (event.message.role === "user") {
2253
+ this.addMessageToChat(event.message);
2254
+ this.updatePendingMessagesDisplay();
2255
+ this.ui.requestRender();
2256
+ }
2257
+ else if (event.message.role === "assistant") {
2258
+ this.streamingComponent = new AssistantMessageComponent(undefined, this.hideThinkingBlock, this.getMarkdownThemeWithSettings(), this.hiddenThinkingLabel);
2259
+ this.streamingMessage = event.message;
2260
+ this.chatContainer.addChild(this.streamingComponent);
2261
+ this.streamingComponent.updateContent(this.streamingMessage);
2262
+ this.ui.requestRender();
2263
+ }
2264
+ break;
2265
+ case "message_update":
2266
+ if (this.streamingComponent && event.message.role === "assistant") {
2267
+ this.streamingMessage = event.message;
2268
+ this.streamingComponent.updateContent(this.streamingMessage);
2269
+ for (const content of this.streamingMessage.content) {
2270
+ if (content.type === "toolCall") {
2271
+ if (!this.pendingTools.has(content.id)) {
2272
+ const component = new ToolExecutionComponent(content.name, content.id, content.arguments, {
2273
+ showImages: this.settingsManager.getShowImages(),
2274
+ imageWidthCells: this.settingsManager.getImageWidthCells(),
2275
+ }, this.getRegisteredToolDefinition(content.name), this.ui, this.sessionManager.getCwd());
2276
+ component.setExpanded(this.toolOutputExpanded);
2277
+ this.chatContainer.addChild(component);
2278
+ this.pendingTools.set(content.id, component);
2279
+ }
2280
+ else {
2281
+ const component = this.pendingTools.get(content.id);
2282
+ if (component) {
2283
+ component.updateArgs(content.arguments);
2284
+ }
2285
+ }
2286
+ }
2287
+ }
2288
+ this.ui.requestRender();
2289
+ }
2290
+ break;
2291
+ case "message_end":
2292
+ if (event.message.role === "user")
2293
+ break;
2294
+ if (this.streamingComponent && event.message.role === "assistant") {
2295
+ this.streamingMessage = event.message;
2296
+ let errorMessage;
2297
+ if (this.streamingMessage.stopReason === "aborted") {
2298
+ const retryAttempt = this.session.retryAttempt;
2299
+ errorMessage =
2300
+ retryAttempt > 0
2301
+ ? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
2302
+ : "Operation aborted";
2303
+ this.streamingMessage.errorMessage = errorMessage;
2304
+ }
2305
+ this.streamingComponent.updateContent(this.streamingMessage);
2306
+ if (this.streamingMessage.stopReason === "aborted" || this.streamingMessage.stopReason === "error") {
2307
+ if (!errorMessage) {
2308
+ errorMessage = this.streamingMessage.errorMessage || "Error";
2309
+ }
2310
+ for (const [, component] of this.pendingTools.entries()) {
2311
+ component.updateResult({
2312
+ content: [{ type: "text", text: errorMessage }],
2313
+ isError: true,
2314
+ });
2315
+ }
2316
+ this.pendingTools.clear();
2317
+ }
2318
+ else {
2319
+ // Args are now complete - trigger diff computation for edit tools
2320
+ for (const [, component] of this.pendingTools.entries()) {
2321
+ component.setArgsComplete();
2322
+ }
2323
+ }
2324
+ this.streamingComponent = undefined;
2325
+ this.streamingMessage = undefined;
2326
+ this.footer.invalidate();
2327
+ }
2328
+ this.ui.requestRender();
2329
+ break;
2330
+ case "tool_execution_start": {
2331
+ let component = this.pendingTools.get(event.toolCallId);
2332
+ if (!component) {
2333
+ component = new ToolExecutionComponent(event.toolName, event.toolCallId, event.args, {
2334
+ showImages: this.settingsManager.getShowImages(),
2335
+ imageWidthCells: this.settingsManager.getImageWidthCells(),
2336
+ }, this.getRegisteredToolDefinition(event.toolName), this.ui, this.sessionManager.getCwd());
2337
+ component.setExpanded(this.toolOutputExpanded);
2338
+ this.chatContainer.addChild(component);
2339
+ this.pendingTools.set(event.toolCallId, component);
2340
+ }
2341
+ component.markExecutionStarted();
2342
+ this.ui.requestRender();
2343
+ break;
2344
+ }
2345
+ case "tool_execution_update": {
2346
+ const component = this.pendingTools.get(event.toolCallId);
2347
+ if (component) {
2348
+ component.updateResult({ ...event.partialResult, isError: false }, true);
2349
+ this.ui.requestRender();
2350
+ }
2351
+ break;
2352
+ }
2353
+ case "tool_execution_end": {
2354
+ const component = this.pendingTools.get(event.toolCallId);
2355
+ if (component) {
2356
+ component.updateResult({ ...event.result, isError: event.isError });
2357
+ this.pendingTools.delete(event.toolCallId);
2358
+ this.ui.requestRender();
2359
+ }
2360
+ break;
2361
+ }
2362
+ case "agent_end":
2363
+ if (this.settingsManager.getShowTerminalProgress()) {
2364
+ this.ui.terminal.setProgress(false);
2365
+ }
2366
+ if (this.loadingAnimation) {
2367
+ this.loadingAnimation.stop();
2368
+ this.loadingAnimation = undefined;
2369
+ this.statusContainer.clear();
2370
+ }
2371
+ if (this.streamingComponent) {
2372
+ this.chatContainer.removeChild(this.streamingComponent);
2373
+ this.streamingComponent = undefined;
2374
+ this.streamingMessage = undefined;
2375
+ }
2376
+ this.pendingTools.clear();
2377
+ await this.checkShutdownRequested();
2378
+ this.ui.requestRender();
2379
+ break;
2380
+ case "compaction_start": {
2381
+ if (this.settingsManager.getShowTerminalProgress()) {
2382
+ this.ui.terminal.setProgress(true);
2383
+ }
2384
+ // Keep editor active; submissions are queued during compaction.
2385
+ this.autoCompactionEscapeHandler = this.defaultEditor.onEscape;
2386
+ this.defaultEditor.onEscape = () => {
2387
+ this.session.abortCompaction();
2388
+ };
2389
+ this.statusContainer.clear();
2390
+ const cancelHint = `(${keyText("app.interrupt")} to cancel)`;
2391
+ const label = event.reason === "manual"
2392
+ ? `Compacting context... ${cancelHint}`
2393
+ : `${event.reason === "overflow" ? "Context overflow detected, " : ""}Auto-compacting... ${cancelHint}`;
2394
+ this.autoCompactionLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), label);
2395
+ this.statusContainer.addChild(this.autoCompactionLoader);
2396
+ this.ui.requestRender();
2397
+ break;
2398
+ }
2399
+ case "compaction_end": {
2400
+ if (this.settingsManager.getShowTerminalProgress()) {
2401
+ this.ui.terminal.setProgress(false);
2402
+ }
2403
+ if (this.autoCompactionEscapeHandler) {
2404
+ this.defaultEditor.onEscape = this.autoCompactionEscapeHandler;
2405
+ this.autoCompactionEscapeHandler = undefined;
2406
+ }
2407
+ if (this.autoCompactionLoader) {
2408
+ this.autoCompactionLoader.stop();
2409
+ this.autoCompactionLoader = undefined;
2410
+ this.statusContainer.clear();
2411
+ }
2412
+ if (event.aborted) {
2413
+ if (event.reason === "manual") {
2414
+ this.showError("Compaction cancelled");
2415
+ }
2416
+ else {
2417
+ this.showStatus("Auto-compaction cancelled");
2418
+ }
2419
+ }
2420
+ else if (event.result) {
2421
+ this.chatContainer.clear();
2422
+ this.rebuildChatFromMessages();
2423
+ this.addMessageToChat(createCompactionSummaryMessage(event.result.summary, event.result.tokensBefore, new Date().toISOString()));
2424
+ this.footer.invalidate();
2425
+ }
2426
+ else if (event.errorMessage) {
2427
+ if (event.reason === "manual") {
2428
+ this.showError(event.errorMessage);
2429
+ }
2430
+ else {
2431
+ this.chatContainer.addChild(new Spacer(1));
2432
+ this.chatContainer.addChild(new Text(theme.fg("error", event.errorMessage), 1, 0));
2433
+ }
2434
+ }
2435
+ void this.flushCompactionQueue({ willRetry: event.willRetry });
2436
+ this.ui.requestRender();
2437
+ break;
2438
+ }
2439
+ case "auto_retry_start": {
2440
+ // Set up escape to abort retry
2441
+ this.retryEscapeHandler = this.defaultEditor.onEscape;
2442
+ this.defaultEditor.onEscape = () => {
2443
+ this.session.abortRetry();
2444
+ };
2445
+ // Show retry indicator
2446
+ this.statusContainer.clear();
2447
+ this.retryCountdown?.dispose();
2448
+ const retryMessage = (seconds) => `Retrying (${event.attempt}/${event.maxAttempts}) in ${seconds}s... (${keyText("app.interrupt")} to cancel)`;
2449
+ this.retryLoader = new Loader(this.ui, (spinner) => theme.fg("warning", spinner), (text) => theme.fg("muted", text), retryMessage(Math.ceil(event.delayMs / 1000)));
2450
+ this.retryCountdown = new CountdownTimer(event.delayMs, this.ui, (seconds) => {
2451
+ this.retryLoader?.setMessage(retryMessage(seconds));
2452
+ }, () => {
2453
+ this.retryCountdown = undefined;
2454
+ });
2455
+ this.statusContainer.addChild(this.retryLoader);
2456
+ this.ui.requestRender();
2457
+ break;
2458
+ }
2459
+ case "auto_retry_end": {
2460
+ // Restore escape handler
2461
+ if (this.retryEscapeHandler) {
2462
+ this.defaultEditor.onEscape = this.retryEscapeHandler;
2463
+ this.retryEscapeHandler = undefined;
2464
+ }
2465
+ if (this.retryCountdown) {
2466
+ this.retryCountdown.dispose();
2467
+ this.retryCountdown = undefined;
2468
+ }
2469
+ // Stop loader
2470
+ if (this.retryLoader) {
2471
+ this.retryLoader.stop();
2472
+ this.retryLoader = undefined;
2473
+ this.statusContainer.clear();
2474
+ }
2475
+ // Show error only on final failure (success shows normal response)
2476
+ if (!event.success) {
2477
+ this.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || "Unknown error"}`);
2478
+ }
2479
+ this.ui.requestRender();
2480
+ break;
2481
+ }
2482
+ }
2483
+ }
2484
+ /** Extract text content from a user message */
2485
+ getUserMessageText(message) {
2486
+ if (message.role !== "user")
2487
+ return "";
2488
+ const textBlocks = typeof message.content === "string"
2489
+ ? [{ type: "text", text: message.content }]
2490
+ : message.content.filter((c) => c.type === "text");
2491
+ return textBlocks.map((c) => c.text).join("");
2492
+ }
2493
+ /**
2494
+ * Show a status message in the chat.
2495
+ *
2496
+ * If multiple status messages are emitted back-to-back (without anything else being added to the chat),
2497
+ * we update the previous status line instead of appending new ones to avoid log spam.
2498
+ */
2499
+ showStatus(message) {
2500
+ const children = this.chatContainer.children;
2501
+ const last = children.length > 0 ? children[children.length - 1] : undefined;
2502
+ const secondLast = children.length > 1 ? children[children.length - 2] : undefined;
2503
+ if (last && secondLast && last === this.lastStatusText && secondLast === this.lastStatusSpacer) {
2504
+ this.lastStatusText.setText(theme.fg("dim", message));
2505
+ this.ui.requestRender();
2506
+ return;
2507
+ }
2508
+ const spacer = new Spacer(1);
2509
+ const text = new Text(theme.fg("dim", message), 1, 0);
2510
+ this.chatContainer.addChild(spacer);
2511
+ this.chatContainer.addChild(text);
2512
+ this.lastStatusSpacer = spacer;
2513
+ this.lastStatusText = text;
2514
+ this.ui.requestRender();
2515
+ }
2516
+ addMessageToChat(message, options) {
2517
+ switch (message.role) {
2518
+ case "bashExecution": {
2519
+ const component = new BashExecutionComponent(message.command, this.ui, message.excludeFromContext);
2520
+ if (message.output) {
2521
+ component.appendOutput(message.output);
2522
+ }
2523
+ component.setComplete(message.exitCode, message.cancelled, message.truncated ? { truncated: true } : undefined, message.fullOutputPath);
2524
+ this.chatContainer.addChild(component);
2525
+ break;
2526
+ }
2527
+ case "custom": {
2528
+ if (message.display) {
2529
+ const renderer = this.session.extensionRunner.getMessageRenderer(message.customType);
2530
+ const component = new CustomMessageComponent(message, renderer, this.getMarkdownThemeWithSettings());
2531
+ component.setExpanded(this.toolOutputExpanded);
2532
+ this.chatContainer.addChild(component);
2533
+ }
2534
+ break;
2535
+ }
2536
+ case "compactionSummary": {
2537
+ this.chatContainer.addChild(new Spacer(1));
2538
+ const component = new CompactionSummaryMessageComponent(message, this.getMarkdownThemeWithSettings());
2539
+ component.setExpanded(this.toolOutputExpanded);
2540
+ this.chatContainer.addChild(component);
2541
+ break;
2542
+ }
2543
+ case "branchSummary": {
2544
+ this.chatContainer.addChild(new Spacer(1));
2545
+ const component = new BranchSummaryMessageComponent(message, this.getMarkdownThemeWithSettings());
2546
+ component.setExpanded(this.toolOutputExpanded);
2547
+ this.chatContainer.addChild(component);
2548
+ break;
2549
+ }
2550
+ case "user": {
2551
+ const textContent = this.getUserMessageText(message);
2552
+ if (textContent) {
2553
+ if (this.chatContainer.children.length > 0) {
2554
+ this.chatContainer.addChild(new Spacer(1));
2555
+ }
2556
+ const skillBlock = parseSkillBlock(textContent);
2557
+ if (skillBlock) {
2558
+ // Render skill block (collapsible)
2559
+ const component = new SkillInvocationMessageComponent(skillBlock, this.getMarkdownThemeWithSettings());
2560
+ component.setExpanded(this.toolOutputExpanded);
2561
+ this.chatContainer.addChild(component);
2562
+ // Render user message separately if present
2563
+ if (skillBlock.userMessage) {
2564
+ this.chatContainer.addChild(new Spacer(1));
2565
+ const userComponent = new UserMessageComponent(skillBlock.userMessage, this.getMarkdownThemeWithSettings());
2566
+ this.chatContainer.addChild(userComponent);
2567
+ }
2568
+ }
2569
+ else {
2570
+ const userComponent = new UserMessageComponent(textContent, this.getMarkdownThemeWithSettings());
2571
+ this.chatContainer.addChild(userComponent);
2572
+ }
2573
+ if (options?.populateHistory) {
2574
+ this.editor.addToHistory?.(textContent);
2575
+ }
2576
+ }
2577
+ break;
2578
+ }
2579
+ case "assistant": {
2580
+ const assistantComponent = new AssistantMessageComponent(message, this.hideThinkingBlock, this.getMarkdownThemeWithSettings(), this.hiddenThinkingLabel);
2581
+ this.chatContainer.addChild(assistantComponent);
2582
+ break;
2583
+ }
2584
+ case "toolResult": {
2585
+ // Tool results are rendered inline with tool calls, handled separately
2586
+ break;
2587
+ }
2588
+ default: {
2589
+ const _exhaustive = message;
2590
+ }
2591
+ }
2592
+ }
2593
+ /**
2594
+ * Render session context to chat. Used for initial load and rebuild after compaction.
2595
+ * @param sessionContext Session context to render
2596
+ * @param options.updateFooter Update footer state
2597
+ * @param options.populateHistory Add user messages to editor history
2598
+ */
2599
+ renderSessionContext(sessionContext, options = {}) {
2600
+ this.pendingTools.clear();
2601
+ const renderedPendingTools = new Map();
2602
+ if (options.updateFooter) {
2603
+ this.footer.invalidate();
2604
+ this.updateEditorBorderColor();
2605
+ }
2606
+ for (const message of sessionContext.messages) {
2607
+ // Assistant messages need special handling for tool calls
2608
+ if (message.role === "assistant") {
2609
+ this.addMessageToChat(message);
2610
+ // Render tool call components
2611
+ for (const content of message.content) {
2612
+ if (content.type === "toolCall") {
2613
+ const component = new ToolExecutionComponent(content.name, content.id, content.arguments, {
2614
+ showImages: this.settingsManager.getShowImages(),
2615
+ imageWidthCells: this.settingsManager.getImageWidthCells(),
2616
+ }, this.getRegisteredToolDefinition(content.name), this.ui, this.sessionManager.getCwd());
2617
+ component.setExpanded(this.toolOutputExpanded);
2618
+ this.chatContainer.addChild(component);
2619
+ if (message.stopReason === "aborted" || message.stopReason === "error") {
2620
+ let errorMessage;
2621
+ if (message.stopReason === "aborted") {
2622
+ const retryAttempt = this.session.retryAttempt;
2623
+ errorMessage =
2624
+ retryAttempt > 0
2625
+ ? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
2626
+ : "Operation aborted";
2627
+ }
2628
+ else {
2629
+ errorMessage = message.errorMessage || "Error";
2630
+ }
2631
+ component.updateResult({ content: [{ type: "text", text: errorMessage }], isError: true });
2632
+ }
2633
+ else {
2634
+ renderedPendingTools.set(content.id, component);
2635
+ }
2636
+ }
2637
+ }
2638
+ }
2639
+ else if (message.role === "toolResult") {
2640
+ // Match tool results to pending tool components
2641
+ const component = renderedPendingTools.get(message.toolCallId);
2642
+ if (component) {
2643
+ component.updateResult(message);
2644
+ renderedPendingTools.delete(message.toolCallId);
2645
+ }
2646
+ }
2647
+ else {
2648
+ // All other messages use standard rendering
2649
+ this.addMessageToChat(message, options);
2650
+ }
2651
+ }
2652
+ for (const [toolCallId, component] of renderedPendingTools) {
2653
+ this.pendingTools.set(toolCallId, component);
2654
+ }
2655
+ this.ui.requestRender();
2656
+ }
2657
+ renderInitialMessages() {
2658
+ // Get aligned messages and entries from session context
2659
+ const context = this.sessionManager.buildSessionContext();
2660
+ this.renderSessionContext(context, {
2661
+ updateFooter: true,
2662
+ populateHistory: true,
2663
+ });
2664
+ this.renderProjectTrustWarningIfNeeded();
2665
+ // Show compaction info if session was compacted
2666
+ const allEntries = this.sessionManager.getEntries();
2667
+ const compactionCount = allEntries.filter((e) => e.type === "compaction").length;
2668
+ if (compactionCount > 0) {
2669
+ const times = compactionCount === 1 ? "1 time" : `${compactionCount} times`;
2670
+ this.showStatus(`Session compacted ${times}`);
2671
+ }
2672
+ }
2673
+ renderProjectTrustWarningIfNeeded() {
2674
+ if (this.settingsManager.isProjectTrusted() || !hasTrustRequiringProjectResources(this.sessionManager.getCwd())) {
2675
+ return;
2676
+ }
2677
+ if (this.chatContainer.children.length > 0) {
2678
+ this.chatContainer.addChild(new Spacer(1));
2679
+ }
2680
+ this.chatContainer.addChild(new Text(theme.fg("warning", `This project is not trusted. Project ${CONFIG_DIR_NAME} resources and packages are ignored. Use /trust to save a trust decision, then restart pi.`), 1, 0));
2681
+ }
2682
+ async getUserInput() {
2683
+ const queuedInput = this.pendingUserInputs.shift();
2684
+ if (queuedInput !== undefined) {
2685
+ return queuedInput;
2686
+ }
2687
+ return new Promise((resolve) => {
2688
+ this.onInputCallback = (text) => {
2689
+ this.onInputCallback = undefined;
2690
+ resolve(text);
2691
+ };
2692
+ });
2693
+ }
2694
+ rebuildChatFromMessages() {
2695
+ this.chatContainer.clear();
2696
+ const context = this.sessionManager.buildSessionContext();
2697
+ this.renderSessionContext(context);
2698
+ }
2699
+ // =========================================================================
2700
+ // Key handlers
2701
+ // =========================================================================
2702
+ handleCtrlC() {
2703
+ const now = Date.now();
2704
+ if (now - this.lastSigintTime < 500) {
2705
+ void this.shutdown();
2706
+ }
2707
+ else {
2708
+ this.clearEditor();
2709
+ this.lastSigintTime = now;
2710
+ }
2711
+ }
2712
+ handleCtrlD() {
2713
+ // Only called when editor is empty (enforced by CustomEditor)
2714
+ void this.shutdown();
2715
+ }
2716
+ /**
2717
+ * Gracefully shutdown the agent.
2718
+ * Stops the TUI before emitting shutdown events so extension UI cleanup cannot
2719
+ * repaint the final frame while the process is exiting.
2720
+ */
2721
+ isShuttingDown = false;
2722
+ async shutdown(options) {
2723
+ if (this.isShuttingDown)
2724
+ return;
2725
+ this.isShuttingDown = true;
2726
+ // Keep signal handlers registered until terminal cleanup has completed.
2727
+ // `signal-exit` checks the listener list during the same SIGTERM/SIGHUP
2728
+ // dispatch and re-sends the signal if only its own listeners remain.
2729
+ if (options?.fromSignal) {
2730
+ // Signal-triggered shutdown (SIGTERM/SIGHUP). Emit extension cleanup
2731
+ // (session_shutdown) BEFORE touching the terminal. Extension teardown
2732
+ // such as removing sockets does not write to the tty, so it must not be
2733
+ // skipped if a later terminal-restore write fails on a dead or stalled
2734
+ // terminal. If the terminal is gone, the restore writes below emit EIO,
2735
+ // which the stdout/stderr error handler turns into emergencyTerminalExit;
2736
+ // the render loop is already idle, so this cannot hot-spin (see #4144).
2737
+ await this.runtimeHost.dispose();
2738
+ this.themeController.disableAutoSync();
2739
+ await this.ui.terminal.drainInput(1000);
2740
+ this.stop();
2741
+ process.exit(0);
2742
+ }
2743
+ // Interactive quit (Ctrl+D, Ctrl+C, /quit, extension shutdown()). Stop the
2744
+ // TUI before emitting shutdown events so extension UI cleanup cannot repaint
2745
+ // the final frame while the process is exiting.
2746
+ // Drain any in-flight Kitty key release events before stopping.
2747
+ // This prevents escape sequences from leaking to the parent shell over slow SSH.
2748
+ this.themeController.disableAutoSync();
2749
+ await this.ui.terminal.drainInput(1000);
2750
+ this.stop();
2751
+ await this.runtimeHost.dispose();
2752
+ const resumeCommand = formatResumeCommand(this.sessionManager);
2753
+ if (resumeCommand) {
2754
+ process.stdout.write(`${chalk.dim("To resume this session:")} ${resumeCommand}\n`);
2755
+ }
2756
+ process.exit(0);
2757
+ }
2758
+ emergencyTerminalExit() {
2759
+ this.isShuttingDown = true;
2760
+ this.unregisterSignalHandlers();
2761
+ killTrackedDetachedChildren();
2762
+ // The terminal is gone. Do not run normal shutdown because TUI and
2763
+ // extension cleanup can write restore sequences and re-trigger EIO.
2764
+ process.exit(129);
2765
+ }
2766
+ /**
2767
+ * Last-resort handler for uncaught exceptions. The TUI puts stdin into raw
2768
+ * mode and hides the cursor; without this handler, an uncaught throw from
2769
+ * anywhere (e.g. an extension's async `ChildProcess.on("exit")` callback)
2770
+ * tears down the process while leaving the terminal in raw mode with no
2771
+ * cursor, requiring `stty sane && reset` to recover.
2772
+ *
2773
+ * Unlike emergencyTerminalExit, the terminal is still alive here, so we
2774
+ * call ui.stop() to restore cooked mode, the cursor, and disable bracketed
2775
+ * paste / Kitty / modifyOtherKeys sequences.
2776
+ */
2777
+ uncaughtCrash(error) {
2778
+ if (this.isShuttingDown) {
2779
+ process.exit(1);
2780
+ }
2781
+ this.isShuttingDown = true;
2782
+ try {
2783
+ this.unregisterSignalHandlers();
2784
+ }
2785
+ catch { }
2786
+ try {
2787
+ killTrackedDetachedChildren();
2788
+ }
2789
+ catch { }
2790
+ try {
2791
+ this.ui.stop();
2792
+ }
2793
+ catch { }
2794
+ console.error("pi exiting due to uncaughtException:");
2795
+ console.error(error);
2796
+ process.exit(1);
2797
+ }
2798
+ /**
2799
+ * Check if shutdown was requested and perform shutdown if so.
2800
+ */
2801
+ async checkShutdownRequested() {
2802
+ if (!this.shutdownRequested)
2803
+ return;
2804
+ await this.shutdown();
2805
+ }
2806
+ registerSignalHandlers() {
2807
+ this.unregisterSignalHandlers();
2808
+ const signals = ["SIGTERM"];
2809
+ if (process.platform !== "win32") {
2810
+ signals.push("SIGHUP");
2811
+ }
2812
+ for (const signal of signals) {
2813
+ const handler = () => {
2814
+ // SIGHUP no longer hard-exits: graceful shutdown emits session_shutdown
2815
+ // first, then attempts terminal restore. A genuinely dead terminal
2816
+ // surfaces as an EIO on the restore writes, which the stdout/stderr
2817
+ // error handler converts into emergencyTerminalExit (see #4144, #5080).
2818
+ killTrackedDetachedChildren();
2819
+ void this.shutdown({ fromSignal: true });
2820
+ };
2821
+ process.prependListener(signal, handler);
2822
+ this.signalCleanupHandlers.push(() => process.off(signal, handler));
2823
+ }
2824
+ const terminalErrorHandler = (error) => {
2825
+ if (isDeadTerminalError(error)) {
2826
+ this.emergencyTerminalExit();
2827
+ }
2828
+ throw error;
2829
+ };
2830
+ process.stdout.on("error", terminalErrorHandler);
2831
+ process.stderr.on("error", terminalErrorHandler);
2832
+ this.signalCleanupHandlers.push(() => process.stdout.off("error", terminalErrorHandler));
2833
+ this.signalCleanupHandlers.push(() => process.stderr.off("error", terminalErrorHandler));
2834
+ // Restore the terminal before the process dies on any uncaught throw.
2835
+ // Without this, an unhandled exception from extension code (or anywhere
2836
+ // in pi) leaves the terminal in raw mode with no cursor.
2837
+ const uncaughtExceptionHandler = (error) => this.uncaughtCrash(error);
2838
+ process.prependListener("uncaughtException", uncaughtExceptionHandler);
2839
+ this.signalCleanupHandlers.push(() => process.off("uncaughtException", uncaughtExceptionHandler));
2840
+ }
2841
+ unregisterSignalHandlers() {
2842
+ for (const cleanup of this.signalCleanupHandlers) {
2843
+ cleanup();
2844
+ }
2845
+ this.signalCleanupHandlers = [];
2846
+ }
2847
+ handleCtrlZ() {
2848
+ if (process.platform === "win32") {
2849
+ this.showStatus("Suspend to background is not supported on Windows");
2850
+ return;
2851
+ }
2852
+ // Keep the event loop alive while suspended. Without this, stopping the TUI
2853
+ // can leave Node with no ref'ed handles, causing the process to exit on fg
2854
+ // before the SIGCONT handler gets a chance to restore the terminal.
2855
+ const suspendKeepAlive = setInterval(() => { }, 2 ** 30);
2856
+ // Ignore SIGINT while suspended so Ctrl+C in the terminal does not
2857
+ // kill the backgrounded process. The handler is removed on resume.
2858
+ const ignoreSigint = () => { };
2859
+ process.on("SIGINT", ignoreSigint);
2860
+ // Set up handler to restore TUI when resumed
2861
+ process.once("SIGCONT", () => {
2862
+ clearInterval(suspendKeepAlive);
2863
+ process.removeListener("SIGINT", ignoreSigint);
2864
+ this.ui.start();
2865
+ this.ui.requestRender(true);
2866
+ });
2867
+ try {
2868
+ // Stop the TUI (restore terminal to normal mode)
2869
+ this.ui.stop();
2870
+ // Send SIGTSTP to process group (pid=0 means all processes in group)
2871
+ process.kill(0, "SIGTSTP");
2872
+ }
2873
+ catch (error) {
2874
+ clearInterval(suspendKeepAlive);
2875
+ process.removeListener("SIGINT", ignoreSigint);
2876
+ throw error;
2877
+ }
2878
+ }
2879
+ async handleFollowUp() {
2880
+ const text = (this.editor.getExpandedText?.() ?? this.editor.getText()).trim();
2881
+ if (!text)
2882
+ return;
2883
+ // Queue input during compaction (extension commands execute immediately)
2884
+ if (this.session.isCompacting) {
2885
+ if (this.isExtensionCommand(text)) {
2886
+ this.editor.addToHistory?.(text);
2887
+ this.editor.setText("");
2888
+ await this.session.prompt(text);
2889
+ }
2890
+ else {
2891
+ this.queueCompactionMessage(text, "followUp");
2892
+ }
2893
+ return;
2894
+ }
2895
+ // Alt+Enter queues a follow-up message (waits until agent finishes)
2896
+ // This handles extension commands (execute immediately), prompt template expansion, and queueing
2897
+ if (this.session.isStreaming) {
2898
+ this.editor.addToHistory?.(text);
2899
+ this.editor.setText("");
2900
+ await this.session.prompt(text, { streamingBehavior: "followUp" });
2901
+ this.updatePendingMessagesDisplay();
2902
+ this.ui.requestRender();
2903
+ }
2904
+ // If not streaming, Alt+Enter acts like regular Enter (trigger onSubmit)
2905
+ else if (this.editor.onSubmit) {
2906
+ this.editor.setText("");
2907
+ this.editor.onSubmit(text);
2908
+ }
2909
+ }
2910
+ handleDequeue() {
2911
+ const restored = this.restoreQueuedMessagesToEditor();
2912
+ if (restored === 0) {
2913
+ this.showStatus("No queued messages to restore");
2914
+ }
2915
+ else {
2916
+ this.showStatus(`Restored ${restored} queued message${restored > 1 ? "s" : ""} to editor`);
2917
+ }
2918
+ }
2919
+ updateEditorBorderColor() {
2920
+ if (this.isBashMode) {
2921
+ this.editor.borderColor = theme.getBashModeBorderColor();
2922
+ }
2923
+ else {
2924
+ const level = this.session.thinkingLevel || "off";
2925
+ this.editor.borderColor = theme.getThinkingBorderColor(level);
2926
+ }
2927
+ this.ui.requestRender();
2928
+ }
2929
+ cycleThinkingLevel() {
2930
+ const newLevel = this.session.cycleThinkingLevel();
2931
+ if (newLevel === undefined) {
2932
+ this.showStatus("Current model does not support thinking");
2933
+ }
2934
+ else {
2935
+ this.footer.invalidate();
2936
+ this.updateEditorBorderColor();
2937
+ this.showStatus(`Thinking level: ${newLevel}`);
2938
+ }
2939
+ }
2940
+ async cycleModel(direction) {
2941
+ try {
2942
+ const result = await this.session.cycleModel(direction);
2943
+ if (result === undefined) {
2944
+ const msg = this.session.scopedModels.length > 0 ? "Only one model in scope" : "Only one model available";
2945
+ this.showStatus(msg);
2946
+ }
2947
+ else {
2948
+ this.footer.invalidate();
2949
+ this.updateEditorBorderColor();
2950
+ const thinkingStr = result.model.reasoning && result.thinkingLevel !== "off" ? ` (thinking: ${result.thinkingLevel})` : "";
2951
+ this.showStatus(`Switched to ${result.model.name || result.model.id}${thinkingStr}`);
2952
+ void this.maybeWarnAboutAnthropicSubscriptionAuth(result.model);
2953
+ }
2954
+ }
2955
+ catch (error) {
2956
+ this.showError(error instanceof Error ? error.message : String(error));
2957
+ }
2958
+ }
2959
+ toggleToolOutputExpansion() {
2960
+ this.setToolsExpanded(!this.toolOutputExpanded);
2961
+ }
2962
+ setToolsExpanded(expanded) {
2963
+ this.toolOutputExpanded = expanded;
2964
+ const activeHeader = this.customHeader ?? this.builtInHeader;
2965
+ if (isExpandable(activeHeader)) {
2966
+ activeHeader.setExpanded(expanded);
2967
+ }
2968
+ for (const child of this.chatContainer.children) {
2969
+ if (isExpandable(child)) {
2970
+ child.setExpanded(expanded);
2971
+ }
2972
+ }
2973
+ this.ui.requestRender();
2974
+ }
2975
+ toggleThinkingBlockVisibility() {
2976
+ this.hideThinkingBlock = !this.hideThinkingBlock;
2977
+ this.settingsManager.setHideThinkingBlock(this.hideThinkingBlock);
2978
+ // Rebuild chat from session messages
2979
+ this.chatContainer.clear();
2980
+ this.rebuildChatFromMessages();
2981
+ // If streaming, re-add the streaming component with updated visibility and re-render
2982
+ if (this.streamingComponent && this.streamingMessage) {
2983
+ this.streamingComponent.setHideThinkingBlock(this.hideThinkingBlock);
2984
+ this.streamingComponent.updateContent(this.streamingMessage);
2985
+ this.chatContainer.addChild(this.streamingComponent);
2986
+ }
2987
+ this.showStatus(`Thinking blocks: ${this.hideThinkingBlock ? "hidden" : "visible"}`);
2988
+ }
2989
+ async openExternalEditor() {
2990
+ // Determine editor (respect $VISUAL, then $EDITOR)
2991
+ const editorCmd = process.env.VISUAL || process.env.EDITOR;
2992
+ if (!editorCmd) {
2993
+ this.showWarning("No editor configured. Set $VISUAL or $EDITOR environment variable.");
2994
+ return;
2995
+ }
2996
+ const currentText = this.editor.getExpandedText?.() ?? this.editor.getText();
2997
+ const tmpFile = path.join(os.tmpdir(), `pi-editor-${Date.now()}.pi.md`);
2998
+ try {
2999
+ // Write current content to temp file
3000
+ fs.writeFileSync(tmpFile, currentText, "utf-8");
3001
+ // Stop TUI to release terminal
3002
+ this.ui.stop();
3003
+ // Split by space to support editor arguments (e.g., "code --wait")
3004
+ const [editor, ...editorArgs] = editorCmd.split(" ");
3005
+ process.stdout.write(`Launching external editor: ${editorCmd}\nPi will resume when the editor exits.\n`);
3006
+ // Do not use spawnSync here. On Windows, synchronous child_process calls can keep
3007
+ // Node/libuv's console input read active after ui.stop() pauses stdin, racing
3008
+ // vim/nvim for the console input buffer until Ctrl+C cancels the pending read.
3009
+ const status = await new Promise((resolve) => {
3010
+ const child = spawn(editor, [...editorArgs, tmpFile], {
3011
+ stdio: "inherit",
3012
+ shell: process.platform === "win32",
3013
+ });
3014
+ child.on("error", () => resolve(null));
3015
+ child.on("close", (code) => resolve(code));
3016
+ });
3017
+ // On successful exit (status 0), replace editor content
3018
+ if (status === 0) {
3019
+ const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
3020
+ this.editor.setText(newContent);
3021
+ }
3022
+ // On non-zero exit, keep original text (no action needed)
3023
+ }
3024
+ finally {
3025
+ // Clean up temp file
3026
+ try {
3027
+ fs.unlinkSync(tmpFile);
3028
+ }
3029
+ catch {
3030
+ // Ignore cleanup errors
3031
+ }
3032
+ // Restart TUI
3033
+ this.ui.start();
3034
+ // Force full re-render since external editor uses alternate screen
3035
+ this.ui.requestRender(true);
3036
+ }
3037
+ }
3038
+ // =========================================================================
3039
+ // UI helpers
3040
+ // =========================================================================
3041
+ clearEditor() {
3042
+ this.editor.setText("");
3043
+ this.ui.requestRender();
3044
+ }
3045
+ showError(errorMessage) {
3046
+ this.chatContainer.addChild(new Spacer(1));
3047
+ this.chatContainer.addChild(new Text(theme.fg("error", `Error: ${errorMessage}`), 1, 0));
3048
+ this.ui.requestRender();
3049
+ }
3050
+ showWarning(warningMessage) {
3051
+ this.chatContainer.addChild(new Spacer(1));
3052
+ this.chatContainer.addChild(new Text(theme.fg("warning", `Warning: ${warningMessage}`), 1, 0));
3053
+ this.ui.requestRender();
3054
+ }
3055
+ showNewVersionNotification(release) {
3056
+ const action = theme.fg("accent", `${APP_NAME} update`);
3057
+ const updateInstruction = theme.fg("muted", `New version ${release.version} is available. Run `) + action;
3058
+ const changelogUrl = "https://pi.dev/changelog";
3059
+ const changelogLink = getCapabilities().hyperlinks
3060
+ ? hyperlink(theme.fg("accent", changelogUrl), changelogUrl)
3061
+ : theme.fg("accent", changelogUrl);
3062
+ const changelogLine = theme.fg("muted", "Changelog: ") + changelogLink;
3063
+ const note = release.note?.trim();
3064
+ this.chatContainer.addChild(new Spacer(1));
3065
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
3066
+ this.chatContainer.addChild(new Text(`${theme.bold(theme.fg("warning", "Update Available"))}\n${updateInstruction}`, 1, 0));
3067
+ if (note) {
3068
+ this.chatContainer.addChild(new Spacer(1));
3069
+ this.chatContainer.addChild(new Markdown(note, 1, 0, this.getMarkdownThemeWithSettings(), {
3070
+ color: (text) => theme.fg("muted", text),
3071
+ }));
3072
+ this.chatContainer.addChild(new Spacer(1));
3073
+ }
3074
+ this.chatContainer.addChild(new Text(changelogLine, 1, 0));
3075
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
3076
+ this.ui.requestRender();
3077
+ }
3078
+ showPackageUpdateNotification(packages) {
3079
+ const action = theme.fg("accent", `${APP_NAME} update --extensions`);
3080
+ const updateInstruction = theme.fg("muted", "Package updates are available. Run ") + action;
3081
+ const packageLines = packages.map((pkg) => `- ${pkg}`).join("\n");
3082
+ this.chatContainer.addChild(new Spacer(1));
3083
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
3084
+ this.chatContainer.addChild(new Text(`${theme.bold(theme.fg("warning", "Package Updates Available"))}\n${updateInstruction}\n${theme.fg("muted", "Packages:")}\n${packageLines}`, 1, 0));
3085
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
3086
+ this.ui.requestRender();
3087
+ }
3088
+ /**
3089
+ * Get all queued messages (read-only).
3090
+ * Combines session queue and compaction queue.
3091
+ */
3092
+ getAllQueuedMessages() {
3093
+ return {
3094
+ steering: [
3095
+ ...this.session.getSteeringMessages(),
3096
+ ...this.compactionQueuedMessages.filter((msg) => msg.mode === "steer").map((msg) => msg.text),
3097
+ ],
3098
+ followUp: [
3099
+ ...this.session.getFollowUpMessages(),
3100
+ ...this.compactionQueuedMessages.filter((msg) => msg.mode === "followUp").map((msg) => msg.text),
3101
+ ],
3102
+ };
3103
+ }
3104
+ /**
3105
+ * Clear all queued messages and return their contents.
3106
+ * Clears both session queue and compaction queue.
3107
+ */
3108
+ clearAllQueues() {
3109
+ const { steering, followUp } = this.session.clearQueue();
3110
+ const compactionSteering = this.compactionQueuedMessages
3111
+ .filter((msg) => msg.mode === "steer")
3112
+ .map((msg) => msg.text);
3113
+ const compactionFollowUp = this.compactionQueuedMessages
3114
+ .filter((msg) => msg.mode === "followUp")
3115
+ .map((msg) => msg.text);
3116
+ this.compactionQueuedMessages = [];
3117
+ return {
3118
+ steering: [...steering, ...compactionSteering],
3119
+ followUp: [...followUp, ...compactionFollowUp],
3120
+ };
3121
+ }
3122
+ updatePendingMessagesDisplay() {
3123
+ this.pendingMessagesContainer.clear();
3124
+ const { steering: steeringMessages, followUp: followUpMessages } = this.getAllQueuedMessages();
3125
+ if (steeringMessages.length > 0 || followUpMessages.length > 0) {
3126
+ this.pendingMessagesContainer.addChild(new Spacer(1));
3127
+ for (const message of steeringMessages) {
3128
+ const text = theme.fg("dim", `Steering: ${message}`);
3129
+ this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
3130
+ }
3131
+ for (const message of followUpMessages) {
3132
+ const text = theme.fg("dim", `Follow-up: ${message}`);
3133
+ this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
3134
+ }
3135
+ const dequeueHint = this.getAppKeyDisplay("app.message.dequeue");
3136
+ const hintText = theme.fg("dim", `↳ ${dequeueHint} to edit all queued messages`);
3137
+ this.pendingMessagesContainer.addChild(new TruncatedText(hintText, 1, 0));
3138
+ }
3139
+ }
3140
+ restoreQueuedMessagesToEditor(options) {
3141
+ const { steering, followUp } = this.clearAllQueues();
3142
+ const allQueued = [...steering, ...followUp];
3143
+ if (allQueued.length === 0) {
3144
+ this.updatePendingMessagesDisplay();
3145
+ if (options?.abort) {
3146
+ this.agent.abort();
3147
+ }
3148
+ return 0;
3149
+ }
3150
+ const queuedText = allQueued.join("\n\n");
3151
+ const currentText = options?.currentText ?? this.editor.getText();
3152
+ const combinedText = [queuedText, currentText].filter((t) => t.trim()).join("\n\n");
3153
+ this.editor.setText(combinedText);
3154
+ this.updatePendingMessagesDisplay();
3155
+ if (options?.abort) {
3156
+ this.agent.abort();
3157
+ }
3158
+ return allQueued.length;
3159
+ }
3160
+ queueCompactionMessage(text, mode) {
3161
+ this.compactionQueuedMessages.push({ text, mode });
3162
+ this.editor.addToHistory?.(text);
3163
+ this.editor.setText("");
3164
+ this.updatePendingMessagesDisplay();
3165
+ this.showStatus("Queued message for after compaction");
3166
+ }
3167
+ isExtensionCommand(text) {
3168
+ if (!text.startsWith("/"))
3169
+ return false;
3170
+ const extensionRunner = this.session.extensionRunner;
3171
+ const spaceIndex = text.indexOf(" ");
3172
+ const commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
3173
+ return !!extensionRunner.getCommand(commandName);
3174
+ }
3175
+ async flushCompactionQueue(options) {
3176
+ if (this.compactionQueuedMessages.length === 0) {
3177
+ return;
3178
+ }
3179
+ const queuedMessages = [...this.compactionQueuedMessages];
3180
+ this.compactionQueuedMessages = [];
3181
+ this.updatePendingMessagesDisplay();
3182
+ const restoreQueue = (error) => {
3183
+ this.session.clearQueue();
3184
+ this.compactionQueuedMessages = queuedMessages;
3185
+ this.updatePendingMessagesDisplay();
3186
+ this.showError(`Failed to send queued message${queuedMessages.length > 1 ? "s" : ""}: ${error instanceof Error ? error.message : String(error)}`);
3187
+ };
3188
+ try {
3189
+ if (options?.willRetry) {
3190
+ // When retry is pending, queue messages for the retry turn
3191
+ for (const message of queuedMessages) {
3192
+ if (this.isExtensionCommand(message.text)) {
3193
+ await this.session.prompt(message.text);
3194
+ }
3195
+ else if (message.mode === "followUp") {
3196
+ await this.session.followUp(message.text);
3197
+ }
3198
+ else {
3199
+ await this.session.steer(message.text);
3200
+ }
3201
+ }
3202
+ this.updatePendingMessagesDisplay();
3203
+ return;
3204
+ }
3205
+ // Find first non-extension-command message to use as prompt
3206
+ const firstPromptIndex = queuedMessages.findIndex((message) => !this.isExtensionCommand(message.text));
3207
+ if (firstPromptIndex === -1) {
3208
+ // All extension commands - execute them all
3209
+ for (const message of queuedMessages) {
3210
+ await this.session.prompt(message.text);
3211
+ }
3212
+ return;
3213
+ }
3214
+ // Execute any extension commands before the first prompt
3215
+ const preCommands = queuedMessages.slice(0, firstPromptIndex);
3216
+ const firstPrompt = queuedMessages[firstPromptIndex];
3217
+ const rest = queuedMessages.slice(firstPromptIndex + 1);
3218
+ for (const message of preCommands) {
3219
+ await this.session.prompt(message.text);
3220
+ }
3221
+ // Send first prompt (starts streaming)
3222
+ const promptPromise = this.session.prompt(firstPrompt.text).catch((error) => {
3223
+ restoreQueue(error);
3224
+ });
3225
+ // Queue remaining messages
3226
+ for (const message of rest) {
3227
+ if (this.isExtensionCommand(message.text)) {
3228
+ await this.session.prompt(message.text);
3229
+ }
3230
+ else if (message.mode === "followUp") {
3231
+ await this.session.followUp(message.text);
3232
+ }
3233
+ else {
3234
+ await this.session.steer(message.text);
3235
+ }
3236
+ }
3237
+ this.updatePendingMessagesDisplay();
3238
+ void promptPromise;
3239
+ }
3240
+ catch (error) {
3241
+ restoreQueue(error);
3242
+ }
3243
+ }
3244
+ /** Move pending bash components from pending area to chat */
3245
+ flushPendingBashComponents() {
3246
+ for (const component of this.pendingBashComponents) {
3247
+ this.pendingMessagesContainer.removeChild(component);
3248
+ this.chatContainer.addChild(component);
3249
+ }
3250
+ this.pendingBashComponents = [];
3251
+ }
3252
+ // =========================================================================
3253
+ // Selectors
3254
+ // =========================================================================
3255
+ /**
3256
+ * Shows a selector component in place of the editor.
3257
+ * @param create Factory that receives a `done` callback and returns the component and focus target
3258
+ */
3259
+ showSelector(create) {
3260
+ const done = () => {
3261
+ this.editorContainer.clear();
3262
+ this.editorContainer.addChild(this.editor);
3263
+ this.ui.setFocus(this.editor);
3264
+ };
3265
+ const { component, focus } = create(done);
3266
+ this.editorContainer.clear();
3267
+ this.editorContainer.addChild(component);
3268
+ this.ui.setFocus(focus);
3269
+ this.ui.requestRender();
3270
+ }
3271
+ showSettingsSelector() {
3272
+ this.showSelector((done) => {
3273
+ const selector = new SettingsSelectorComponent({
3274
+ autoCompact: this.session.autoCompactionEnabled,
3275
+ showImages: this.settingsManager.getShowImages(),
3276
+ imageWidthCells: this.settingsManager.getImageWidthCells(),
3277
+ autoResizeImages: this.settingsManager.getImageAutoResize(),
3278
+ blockImages: this.settingsManager.getBlockImages(),
3279
+ enableSkillCommands: this.settingsManager.getEnableSkillCommands(),
3280
+ steeringMode: this.session.steeringMode,
3281
+ followUpMode: this.session.followUpMode,
3282
+ transport: this.settingsManager.getTransport(),
3283
+ httpIdleTimeoutMs: this.settingsManager.getHttpIdleTimeoutMs(),
3284
+ thinkingLevel: this.session.thinkingLevel,
3285
+ availableThinkingLevels: this.session.getAvailableThinkingLevels(),
3286
+ currentTheme: this.settingsManager.getThemeSetting() || "dark",
3287
+ terminalTheme: this.themeController.getTerminalTheme(),
3288
+ availableThemes: getAvailableThemes(),
3289
+ hideThinkingBlock: this.hideThinkingBlock,
3290
+ collapseChangelog: this.settingsManager.getCollapseChangelog(),
3291
+ enableInstallTelemetry: this.settingsManager.getEnableInstallTelemetry(),
3292
+ doubleEscapeAction: this.settingsManager.getDoubleEscapeAction(),
3293
+ treeFilterMode: this.settingsManager.getTreeFilterMode(),
3294
+ showHardwareCursor: this.settingsManager.getShowHardwareCursor(),
3295
+ defaultProjectTrust: this.settingsManager.getDefaultProjectTrust(),
3296
+ editorPaddingX: this.settingsManager.getEditorPaddingX(),
3297
+ autocompleteMaxVisible: this.settingsManager.getAutocompleteMaxVisible(),
3298
+ quietStartup: this.settingsManager.getQuietStartup(),
3299
+ clearOnShrink: this.settingsManager.getClearOnShrink(),
3300
+ showTerminalProgress: this.settingsManager.getShowTerminalProgress(),
3301
+ warnings: this.settingsManager.getWarnings(),
3302
+ }, {
3303
+ onAutoCompactChange: (enabled) => {
3304
+ this.session.setAutoCompactionEnabled(enabled);
3305
+ this.footer.setAutoCompactEnabled(enabled);
3306
+ },
3307
+ onShowImagesChange: (enabled) => {
3308
+ this.settingsManager.setShowImages(enabled);
3309
+ for (const child of this.chatContainer.children) {
3310
+ if (child instanceof ToolExecutionComponent) {
3311
+ child.setShowImages(enabled);
3312
+ }
3313
+ }
3314
+ },
3315
+ onImageWidthCellsChange: (width) => {
3316
+ this.settingsManager.setImageWidthCells(width);
3317
+ for (const child of this.chatContainer.children) {
3318
+ if (child instanceof ToolExecutionComponent) {
3319
+ child.setImageWidthCells(width);
3320
+ }
3321
+ }
3322
+ },
3323
+ onAutoResizeImagesChange: (enabled) => {
3324
+ this.settingsManager.setImageAutoResize(enabled);
3325
+ },
3326
+ onBlockImagesChange: (blocked) => {
3327
+ this.settingsManager.setBlockImages(blocked);
3328
+ },
3329
+ onEnableSkillCommandsChange: (enabled) => {
3330
+ this.settingsManager.setEnableSkillCommands(enabled);
3331
+ this.setupAutocompleteProvider();
3332
+ },
3333
+ onSteeringModeChange: (mode) => {
3334
+ this.session.setSteeringMode(mode);
3335
+ },
3336
+ onFollowUpModeChange: (mode) => {
3337
+ this.session.setFollowUpMode(mode);
3338
+ },
3339
+ onTransportChange: (transport) => {
3340
+ this.settingsManager.setTransport(transport);
3341
+ this.session.agent.transport = transport;
3342
+ },
3343
+ onHttpIdleTimeoutMsChange: (timeoutMs) => {
3344
+ this.settingsManager.setHttpIdleTimeoutMs(timeoutMs);
3345
+ configureHttpDispatcher(timeoutMs);
3346
+ this.showStatus(`HTTP idle timeout: ${formatHttpIdleTimeoutMs(timeoutMs)}`);
3347
+ },
3348
+ onThinkingLevelChange: (level) => {
3349
+ this.session.setThinkingLevel(level);
3350
+ this.footer.invalidate();
3351
+ this.updateEditorBorderColor();
3352
+ },
3353
+ onThemeChange: (themeSetting) => {
3354
+ this.settingsManager.setTheme(themeSetting);
3355
+ void this.themeController.applyFromSettings();
3356
+ },
3357
+ onThemePreview: (themeName) => this.themeController.preview(themeName),
3358
+ onHideThinkingBlockChange: (hidden) => {
3359
+ this.hideThinkingBlock = hidden;
3360
+ this.settingsManager.setHideThinkingBlock(hidden);
3361
+ for (const child of this.chatContainer.children) {
3362
+ if (child instanceof AssistantMessageComponent) {
3363
+ child.setHideThinkingBlock(hidden);
3364
+ }
3365
+ }
3366
+ this.chatContainer.clear();
3367
+ this.rebuildChatFromMessages();
3368
+ },
3369
+ onCollapseChangelogChange: (collapsed) => {
3370
+ this.settingsManager.setCollapseChangelog(collapsed);
3371
+ },
3372
+ onEnableInstallTelemetryChange: (enabled) => {
3373
+ this.settingsManager.setEnableInstallTelemetry(enabled);
3374
+ },
3375
+ onQuietStartupChange: (enabled) => {
3376
+ this.settingsManager.setQuietStartup(enabled);
3377
+ },
3378
+ onDefaultProjectTrustChange: (defaultProjectTrust) => {
3379
+ this.settingsManager.setDefaultProjectTrust(defaultProjectTrust);
3380
+ },
3381
+ onDoubleEscapeActionChange: (action) => {
3382
+ this.settingsManager.setDoubleEscapeAction(action);
3383
+ },
3384
+ onTreeFilterModeChange: (mode) => {
3385
+ this.settingsManager.setTreeFilterMode(mode);
3386
+ },
3387
+ onShowHardwareCursorChange: (enabled) => {
3388
+ this.settingsManager.setShowHardwareCursor(enabled);
3389
+ this.ui.setShowHardwareCursor(enabled);
3390
+ },
3391
+ onEditorPaddingXChange: (padding) => {
3392
+ this.settingsManager.setEditorPaddingX(padding);
3393
+ this.defaultEditor.setPaddingX(padding);
3394
+ if (this.editor !== this.defaultEditor && this.editor.setPaddingX !== undefined) {
3395
+ this.editor.setPaddingX(padding);
3396
+ }
3397
+ },
3398
+ onAutocompleteMaxVisibleChange: (maxVisible) => {
3399
+ this.settingsManager.setAutocompleteMaxVisible(maxVisible);
3400
+ this.defaultEditor.setAutocompleteMaxVisible(maxVisible);
3401
+ if (this.editor !== this.defaultEditor && this.editor.setAutocompleteMaxVisible !== undefined) {
3402
+ this.editor.setAutocompleteMaxVisible(maxVisible);
3403
+ }
3404
+ },
3405
+ onClearOnShrinkChange: (enabled) => {
3406
+ this.settingsManager.setClearOnShrink(enabled);
3407
+ this.ui.setClearOnShrink(enabled);
3408
+ },
3409
+ onShowTerminalProgressChange: (enabled) => {
3410
+ this.settingsManager.setShowTerminalProgress(enabled);
3411
+ },
3412
+ onWarningsChange: (warnings) => {
3413
+ this.settingsManager.setWarnings(warnings);
3414
+ },
3415
+ onCancel: () => {
3416
+ done();
3417
+ this.ui.requestRender();
3418
+ },
3419
+ });
3420
+ return { component: selector, focus: selector.getSettingsList() };
3421
+ });
3422
+ }
3423
+ async handleModelCommand(searchTerm) {
3424
+ if (!searchTerm) {
3425
+ this.showModelSelector();
3426
+ return;
3427
+ }
3428
+ const model = await this.findExactModelMatch(searchTerm);
3429
+ if (model) {
3430
+ try {
3431
+ await this.session.setModel(model);
3432
+ this.footer.invalidate();
3433
+ this.updateEditorBorderColor();
3434
+ this.showStatus(`Model: ${model.id}`);
3435
+ void this.maybeWarnAboutAnthropicSubscriptionAuth(model);
3436
+ this.checkDaxnutsEasterEgg(model);
3437
+ }
3438
+ catch (error) {
3439
+ this.showError(error instanceof Error ? error.message : String(error));
3440
+ }
3441
+ return;
3442
+ }
3443
+ this.showModelSelector(searchTerm);
3444
+ }
3445
+ async findExactModelMatch(searchTerm) {
3446
+ const models = await this.getModelCandidates();
3447
+ return findExactModelReferenceMatch(searchTerm, models);
3448
+ }
3449
+ async getModelCandidates() {
3450
+ if (this.session.scopedModels.length > 0) {
3451
+ return this.session.scopedModels.map((scoped) => scoped.model);
3452
+ }
3453
+ this.session.modelRegistry.refresh();
3454
+ try {
3455
+ return await this.session.modelRegistry.getAvailable();
3456
+ }
3457
+ catch {
3458
+ return [];
3459
+ }
3460
+ }
3461
+ /** Update the footer's available provider count from current model candidates */
3462
+ async updateAvailableProviderCount() {
3463
+ const models = await this.getModelCandidates();
3464
+ const uniqueProviders = new Set(models.map((m) => m.provider));
3465
+ this.footerDataProvider.setAvailableProviderCount(uniqueProviders.size);
3466
+ }
3467
+ async maybeWarnAboutAnthropicSubscriptionAuth(model = this.session.model) {
3468
+ if (this.settingsManager.getWarnings().anthropicExtraUsage === false) {
3469
+ return;
3470
+ }
3471
+ if (this.anthropicSubscriptionWarningShown) {
3472
+ return;
3473
+ }
3474
+ if (!model || model.provider !== "anthropic") {
3475
+ return;
3476
+ }
3477
+ const storedCredential = this.session.modelRegistry.authStorage.get("anthropic");
3478
+ if (storedCredential?.type === "oauth") {
3479
+ this.anthropicSubscriptionWarningShown = true;
3480
+ this.showWarning(ANTHROPIC_SUBSCRIPTION_AUTH_WARNING);
3481
+ return;
3482
+ }
3483
+ try {
3484
+ const apiKey = await this.session.modelRegistry.getApiKeyForProvider(model.provider);
3485
+ if (!isAnthropicSubscriptionAuthKey(apiKey)) {
3486
+ return;
3487
+ }
3488
+ this.anthropicSubscriptionWarningShown = true;
3489
+ this.showWarning(ANTHROPIC_SUBSCRIPTION_AUTH_WARNING);
3490
+ }
3491
+ catch {
3492
+ // Ignore auth lookup failures for warning-only checks.
3493
+ }
3494
+ }
3495
+ maybeSaveImplicitProjectTrustAfterReload() {
3496
+ const cwd = this.sessionManager.getCwd();
3497
+ if (this.autoTrustOnReloadCwd !== cwd) {
3498
+ return false;
3499
+ }
3500
+ if (!this.settingsManager.isProjectTrusted() || !hasTrustRequiringProjectResources(cwd)) {
3501
+ return false;
3502
+ }
3503
+ const trustStore = new ProjectTrustStore(this.runtimeHost.services.agentDir);
3504
+ try {
3505
+ if (trustStore.get(cwd) !== null) {
3506
+ this.autoTrustOnReloadCwd = undefined;
3507
+ return false;
3508
+ }
3509
+ trustStore.set(cwd, true);
3510
+ this.autoTrustOnReloadCwd = undefined;
3511
+ return true;
3512
+ }
3513
+ catch (error) {
3514
+ this.showWarning(`Could not save project trust after reload: ${error instanceof Error ? error.message : String(error)}`);
3515
+ return false;
3516
+ }
3517
+ }
3518
+ showTrustSelector() {
3519
+ const cwd = this.sessionManager.getCwd();
3520
+ const trustStore = new ProjectTrustStore(this.runtimeHost.services.agentDir);
3521
+ const savedDecision = trustStore.getEntry(cwd);
3522
+ this.showSelector((done) => {
3523
+ const selector = new TrustSelectorComponent({
3524
+ cwd,
3525
+ savedDecision,
3526
+ projectTrusted: this.settingsManager.isProjectTrusted(),
3527
+ onSelect: (selection) => {
3528
+ trustStore.setMany(selection.updates);
3529
+ done();
3530
+ this.showStatus(`Saved trust decision: ${selection.trusted ? "trusted" : "untrusted"}. Restart pi for this to take effect.`);
3531
+ },
3532
+ onCancel: () => {
3533
+ done();
3534
+ this.ui.requestRender();
3535
+ },
3536
+ });
3537
+ return { component: selector, focus: selector };
3538
+ });
3539
+ }
3540
+ showModelSelector(initialSearchInput) {
3541
+ this.showSelector((done) => {
3542
+ const selector = new ModelSelectorComponent(this.ui, this.session.model, this.settingsManager, this.session.modelRegistry, this.session.scopedModels, async (model) => {
3543
+ try {
3544
+ await this.session.setModel(model);
3545
+ this.footer.invalidate();
3546
+ this.updateEditorBorderColor();
3547
+ done();
3548
+ this.showStatus(`Model: ${model.id}`);
3549
+ void this.maybeWarnAboutAnthropicSubscriptionAuth(model);
3550
+ this.checkDaxnutsEasterEgg(model);
3551
+ }
3552
+ catch (error) {
3553
+ done();
3554
+ this.showError(error instanceof Error ? error.message : String(error));
3555
+ }
3556
+ }, () => {
3557
+ done();
3558
+ this.ui.requestRender();
3559
+ }, initialSearchInput);
3560
+ return { component: selector, focus: selector };
3561
+ });
3562
+ }
3563
+ async showModelsSelector() {
3564
+ // Get all available models
3565
+ this.session.modelRegistry.refresh();
3566
+ const allModels = this.session.modelRegistry.getAvailable();
3567
+ if (allModels.length === 0) {
3568
+ this.showStatus("No models available");
3569
+ return;
3570
+ }
3571
+ // Check if session has scoped models (from previous session-only changes or CLI --models)
3572
+ const sessionScopedModels = this.session.scopedModels;
3573
+ const hasSessionScope = sessionScopedModels.length > 0;
3574
+ // Build enabled model IDs from session state or settings
3575
+ let currentEnabledIds = null;
3576
+ if (hasSessionScope) {
3577
+ // Use current session's scoped models
3578
+ currentEnabledIds = sessionScopedModels.map((scoped) => `${scoped.model.provider}/${scoped.model.id}`);
3579
+ }
3580
+ else {
3581
+ // Fall back to settings
3582
+ const patterns = this.settingsManager.getEnabledModels();
3583
+ if (patterns !== undefined && patterns.length > 0) {
3584
+ const scopedModels = await resolveModelScope(patterns, this.session.modelRegistry);
3585
+ currentEnabledIds = scopedModels.map((scoped) => `${scoped.model.provider}/${scoped.model.id}`);
3586
+ }
3587
+ }
3588
+ // Helper to update session's scoped models (session-only, no persist)
3589
+ const updateSessionModels = async (enabledIds) => {
3590
+ currentEnabledIds = enabledIds === null ? null : [...enabledIds];
3591
+ if (enabledIds && enabledIds.length > 0 && enabledIds.length < allModels.length) {
3592
+ const newScopedModels = await resolveModelScope(enabledIds, this.session.modelRegistry);
3593
+ this.session.setScopedModels(newScopedModels.map((sm) => ({
3594
+ model: sm.model,
3595
+ thinkingLevel: sm.thinkingLevel,
3596
+ })));
3597
+ }
3598
+ else {
3599
+ // All enabled or none enabled = no filter
3600
+ this.session.setScopedModels([]);
3601
+ }
3602
+ await this.updateAvailableProviderCount();
3603
+ this.ui.requestRender();
3604
+ };
3605
+ this.showSelector((done) => {
3606
+ const selector = new ScopedModelsSelectorComponent({
3607
+ allModels,
3608
+ enabledModelIds: currentEnabledIds,
3609
+ }, {
3610
+ onChange: async (enabledIds) => {
3611
+ await updateSessionModels(enabledIds);
3612
+ },
3613
+ onPersist: (enabledIds) => {
3614
+ // Persist to settings
3615
+ const newPatterns = enabledIds === null || enabledIds.length === allModels.length
3616
+ ? undefined // All enabled = clear filter
3617
+ : enabledIds;
3618
+ this.settingsManager.setEnabledModels(newPatterns ? [...newPatterns] : undefined);
3619
+ this.showStatus("Model selection saved to settings");
3620
+ },
3621
+ onCancel: () => {
3622
+ done();
3623
+ this.ui.requestRender();
3624
+ },
3625
+ });
3626
+ return { component: selector, focus: selector };
3627
+ });
3628
+ }
3629
+ showUserMessageSelector() {
3630
+ const userMessages = this.session.getUserMessagesForForking();
3631
+ if (userMessages.length === 0) {
3632
+ this.showStatus("No messages to fork from");
3633
+ return;
3634
+ }
3635
+ const initialSelectedId = userMessages[userMessages.length - 1]?.entryId;
3636
+ this.showSelector((done) => {
3637
+ const selector = new UserMessageSelectorComponent(userMessages.map((m) => ({ id: m.entryId, text: m.text })), async (entryId) => {
3638
+ try {
3639
+ const result = await this.runtimeHost.fork(entryId);
3640
+ if (result.cancelled) {
3641
+ done();
3642
+ this.ui.requestRender();
3643
+ return;
3644
+ }
3645
+ this.editor.setText(result.selectedText ?? "");
3646
+ done();
3647
+ this.showStatus("Forked to new session");
3648
+ }
3649
+ catch (error) {
3650
+ done();
3651
+ this.showError(error instanceof Error ? error.message : String(error));
3652
+ }
3653
+ }, () => {
3654
+ done();
3655
+ this.ui.requestRender();
3656
+ }, initialSelectedId);
3657
+ return { component: selector, focus: selector.getMessageList() };
3658
+ });
3659
+ }
3660
+ async handleCloneCommand() {
3661
+ const leafId = this.sessionManager.getLeafId();
3662
+ if (!leafId) {
3663
+ this.showStatus("Nothing to clone yet");
3664
+ return;
3665
+ }
3666
+ try {
3667
+ const result = await this.runtimeHost.fork(leafId, { position: "at" });
3668
+ if (result.cancelled) {
3669
+ this.ui.requestRender();
3670
+ return;
3671
+ }
3672
+ this.editor.setText("");
3673
+ this.showStatus("Cloned to new session");
3674
+ }
3675
+ catch (error) {
3676
+ this.showError(error instanceof Error ? error.message : String(error));
3677
+ }
3678
+ }
3679
+ showTreeSelector(initialSelectedId) {
3680
+ const tree = this.sessionManager.getTree();
3681
+ const realLeafId = this.sessionManager.getLeafId();
3682
+ const initialFilterMode = this.settingsManager.getTreeFilterMode();
3683
+ if (tree.length === 0) {
3684
+ this.showStatus("No entries in session");
3685
+ return;
3686
+ }
3687
+ this.showSelector((done) => {
3688
+ const selector = new TreeSelectorComponent(tree, realLeafId, this.ui.terminal.rows, async (entryId) => {
3689
+ // Selecting the current leaf is a no-op (already there)
3690
+ if (entryId === realLeafId) {
3691
+ done();
3692
+ this.showStatus("Already at this point");
3693
+ return;
3694
+ }
3695
+ // Ask about summarization
3696
+ done(); // Close selector first
3697
+ // Loop until user makes a complete choice or cancels to tree
3698
+ let wantsSummary = false;
3699
+ let customInstructions;
3700
+ // Check if we should skip the prompt (user preference to always default to no summary)
3701
+ if (!this.settingsManager.getBranchSummarySkipPrompt()) {
3702
+ while (true) {
3703
+ const summaryChoice = await this.showExtensionSelector("Summarize branch?", [
3704
+ "No summary",
3705
+ "Summarize",
3706
+ "Summarize with custom prompt",
3707
+ ]);
3708
+ if (summaryChoice === undefined) {
3709
+ // User pressed escape - re-show tree selector with same selection
3710
+ this.showTreeSelector(entryId);
3711
+ return;
3712
+ }
3713
+ wantsSummary = summaryChoice !== "No summary";
3714
+ if (summaryChoice === "Summarize with custom prompt") {
3715
+ customInstructions = await this.showExtensionEditor("Custom summarization instructions");
3716
+ if (customInstructions === undefined) {
3717
+ // User cancelled - loop back to summary selector
3718
+ continue;
3719
+ }
3720
+ }
3721
+ // User made a complete choice
3722
+ break;
3723
+ }
3724
+ }
3725
+ // Set up escape handler and loader if summarizing
3726
+ let summaryLoader;
3727
+ const originalOnEscape = this.defaultEditor.onEscape;
3728
+ if (wantsSummary) {
3729
+ this.defaultEditor.onEscape = () => {
3730
+ this.session.abortBranchSummary();
3731
+ };
3732
+ this.chatContainer.addChild(new Spacer(1));
3733
+ summaryLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), `Summarizing branch... (${keyText("app.interrupt")} to cancel)`);
3734
+ this.statusContainer.addChild(summaryLoader);
3735
+ this.ui.requestRender();
3736
+ }
3737
+ try {
3738
+ const result = await this.session.navigateTree(entryId, {
3739
+ summarize: wantsSummary,
3740
+ customInstructions,
3741
+ });
3742
+ if (result.aborted) {
3743
+ // Summarization aborted - re-show tree selector with same selection
3744
+ this.showStatus("Branch summarization cancelled");
3745
+ this.showTreeSelector(entryId);
3746
+ return;
3747
+ }
3748
+ if (result.cancelled) {
3749
+ this.showStatus("Navigation cancelled");
3750
+ return;
3751
+ }
3752
+ // Update UI
3753
+ this.chatContainer.clear();
3754
+ this.renderInitialMessages();
3755
+ if (result.editorText && !this.editor.getText().trim()) {
3756
+ this.editor.setText(result.editorText);
3757
+ }
3758
+ this.showStatus("Navigated to selected point");
3759
+ void this.flushCompactionQueue({ willRetry: false });
3760
+ }
3761
+ catch (error) {
3762
+ this.showError(error instanceof Error ? error.message : String(error));
3763
+ }
3764
+ finally {
3765
+ if (summaryLoader) {
3766
+ summaryLoader.stop();
3767
+ this.statusContainer.clear();
3768
+ }
3769
+ this.defaultEditor.onEscape = originalOnEscape;
3770
+ }
3771
+ }, () => {
3772
+ done();
3773
+ this.ui.requestRender();
3774
+ }, (entryId, label) => {
3775
+ this.sessionManager.appendLabelChange(entryId, label);
3776
+ this.ui.requestRender();
3777
+ }, initialSelectedId, initialFilterMode);
3778
+ return { component: selector, focus: selector };
3779
+ });
3780
+ }
3781
+ showSessionSelector() {
3782
+ this.showSelector((done) => {
3783
+ const selector = new SessionSelectorComponent((onProgress) => SessionManager.list(this.sessionManager.getCwd(), this.sessionManager.getSessionDir(), onProgress), (onProgress) => this.sessionManager.usesDefaultSessionDir()
3784
+ ? SessionManager.listAll(onProgress)
3785
+ : SessionManager.listAll(this.sessionManager.getSessionDir(), onProgress), async (sessionPath) => {
3786
+ done();
3787
+ await this.handleResumeSession(sessionPath);
3788
+ }, () => {
3789
+ done();
3790
+ this.ui.requestRender();
3791
+ }, () => {
3792
+ void this.shutdown();
3793
+ }, () => this.ui.requestRender(), {
3794
+ renameSession: async (sessionFilePath, nextName) => {
3795
+ const next = (nextName ?? "").trim();
3796
+ if (!next)
3797
+ return;
3798
+ const mgr = SessionManager.open(sessionFilePath);
3799
+ mgr.appendSessionInfo(next);
3800
+ },
3801
+ showRenameHint: true,
3802
+ keybindings: this.keybindings,
3803
+ }, this.sessionManager.getSessionFile());
3804
+ return { component: selector, focus: selector };
3805
+ });
3806
+ }
3807
+ async handleResumeSession(sessionPath, options) {
3808
+ if (this.loadingAnimation) {
3809
+ this.loadingAnimation.stop();
3810
+ this.loadingAnimation = undefined;
3811
+ }
3812
+ this.statusContainer.clear();
3813
+ try {
3814
+ const result = await this.runtimeHost.switchSession(sessionPath, {
3815
+ withSession: options?.withSession,
3816
+ projectTrustContextFactory: (cwd) => this.createProjectTrustContext(cwd),
3817
+ });
3818
+ if (result.cancelled) {
3819
+ return result;
3820
+ }
3821
+ this.showStatus("Resumed session");
3822
+ return result;
3823
+ }
3824
+ catch (error) {
3825
+ if (error instanceof MissingSessionCwdError) {
3826
+ const selectedCwd = await this.promptForMissingSessionCwd(error);
3827
+ if (!selectedCwd) {
3828
+ this.showStatus("Resume cancelled");
3829
+ return { cancelled: true };
3830
+ }
3831
+ const result = await this.runtimeHost.switchSession(sessionPath, {
3832
+ cwdOverride: selectedCwd,
3833
+ withSession: options?.withSession,
3834
+ projectTrustContextFactory: (cwd) => this.createProjectTrustContext(cwd),
3835
+ });
3836
+ if (result.cancelled) {
3837
+ return result;
3838
+ }
3839
+ this.showStatus("Resumed session in current cwd");
3840
+ return result;
3841
+ }
3842
+ return this.handleFatalRuntimeError("Failed to resume session", error);
3843
+ }
3844
+ }
3845
+ getLoginProviderOptions(authType) {
3846
+ const authStorage = this.session.modelRegistry.authStorage;
3847
+ const oauthProviders = authStorage.getOAuthProviders();
3848
+ const oauthProviderIds = new Set(oauthProviders.map((provider) => provider.id));
3849
+ const options = oauthProviders.map((provider) => ({
3850
+ id: provider.id,
3851
+ name: provider.name,
3852
+ authType: "oauth",
3853
+ }));
3854
+ const modelProviders = new Set(this.session.modelRegistry.getAll().map((model) => model.provider));
3855
+ for (const providerId of modelProviders) {
3856
+ if (!isApiKeyLoginProvider(providerId, oauthProviderIds)) {
3857
+ continue;
3858
+ }
3859
+ options.push({
3860
+ id: providerId,
3861
+ name: this.session.modelRegistry.getProviderDisplayName(providerId),
3862
+ authType: "api_key",
3863
+ });
3864
+ }
3865
+ const filteredOptions = authType ? options.filter((option) => option.authType === authType) : options;
3866
+ return filteredOptions.sort((a, b) => a.name.localeCompare(b.name));
3867
+ }
3868
+ getLogoutProviderOptions() {
3869
+ const authStorage = this.session.modelRegistry.authStorage;
3870
+ const options = [];
3871
+ for (const providerId of authStorage.list()) {
3872
+ const credential = authStorage.get(providerId);
3873
+ if (!credential) {
3874
+ continue;
3875
+ }
3876
+ options.push({
3877
+ id: providerId,
3878
+ name: this.session.modelRegistry.getProviderDisplayName(providerId),
3879
+ authType: credential.type,
3880
+ });
3881
+ }
3882
+ return options.sort((a, b) => a.name.localeCompare(b.name));
3883
+ }
3884
+ showLoginAuthTypeSelector() {
3885
+ const subscriptionLabel = "Use a subscription";
3886
+ const apiKeyLabel = "Use an API key";
3887
+ this.showSelector((done) => {
3888
+ const selector = new ExtensionSelectorComponent("Select authentication method:", [subscriptionLabel, apiKeyLabel], (option) => {
3889
+ done();
3890
+ const authType = option === subscriptionLabel ? "oauth" : "api_key";
3891
+ this.showLoginProviderSelector(authType);
3892
+ }, () => {
3893
+ done();
3894
+ this.ui.requestRender();
3895
+ });
3896
+ return { component: selector, focus: selector };
3897
+ });
3898
+ }
3899
+ showLoginProviderSelector(authType) {
3900
+ const providerOptions = this.getLoginProviderOptions(authType);
3901
+ if (providerOptions.length === 0) {
3902
+ this.showStatus(authType === "oauth" ? "No subscription providers available." : "No API key providers available.");
3903
+ return;
3904
+ }
3905
+ this.showSelector((done) => {
3906
+ const selector = new OAuthSelectorComponent("login", this.session.modelRegistry.authStorage, providerOptions, async (providerId) => {
3907
+ done();
3908
+ const providerOption = providerOptions.find((provider) => provider.id === providerId);
3909
+ if (!providerOption) {
3910
+ return;
3911
+ }
3912
+ if (providerOption.authType === "oauth") {
3913
+ await this.showLoginDialog(providerOption.id, providerOption.name);
3914
+ }
3915
+ else if (providerOption.id === BEDROCK_PROVIDER_ID) {
3916
+ this.showBedrockSetupDialog(providerOption.id, providerOption.name);
3917
+ }
3918
+ else {
3919
+ await this.showApiKeyLoginDialog(providerOption.id, providerOption.name);
3920
+ }
3921
+ }, () => {
3922
+ done();
3923
+ this.showLoginAuthTypeSelector();
3924
+ }, (providerId) => this.session.modelRegistry.getProviderAuthStatus(providerId));
3925
+ return { component: selector, focus: selector };
3926
+ });
3927
+ }
3928
+ async showOAuthSelector(mode) {
3929
+ if (mode === "login") {
3930
+ this.showLoginAuthTypeSelector();
3931
+ return;
3932
+ }
3933
+ const providerOptions = this.getLogoutProviderOptions();
3934
+ if (providerOptions.length === 0) {
3935
+ this.showStatus("No stored credentials to remove. /logout only removes credentials saved by /login; environment variables and models.json config are unchanged.");
3936
+ return;
3937
+ }
3938
+ this.showSelector((done) => {
3939
+ const selector = new OAuthSelectorComponent(mode, this.session.modelRegistry.authStorage, providerOptions, async (providerId) => {
3940
+ done();
3941
+ const providerOption = providerOptions.find((provider) => provider.id === providerId);
3942
+ if (!providerOption) {
3943
+ return;
3944
+ }
3945
+ try {
3946
+ this.session.modelRegistry.authStorage.logout(providerOption.id);
3947
+ this.session.modelRegistry.refresh();
3948
+ await this.updateAvailableProviderCount();
3949
+ const message = providerOption.authType === "oauth"
3950
+ ? `Logged out of ${providerOption.name}`
3951
+ : `Removed stored API key for ${providerOption.name}. Environment variables and models.json config are unchanged.`;
3952
+ this.showStatus(message);
3953
+ }
3954
+ catch (error) {
3955
+ this.showError(`Logout failed: ${error instanceof Error ? error.message : String(error)}`);
3956
+ }
3957
+ }, () => {
3958
+ done();
3959
+ this.ui.requestRender();
3960
+ });
3961
+ return { component: selector, focus: selector };
3962
+ });
3963
+ }
3964
+ async completeProviderAuthentication(providerId, providerName, authType, previousModel) {
3965
+ this.session.modelRegistry.refresh();
3966
+ const actionLabel = authType === "oauth" ? `Logged in to ${providerName}` : `Saved API key for ${providerName}`;
3967
+ let selectedModel;
3968
+ let selectionError;
3969
+ if (isUnknownModel(previousModel)) {
3970
+ const availableModels = this.session.modelRegistry.getAvailable();
3971
+ const providerModels = availableModels.filter((model) => model.provider === providerId);
3972
+ if (!hasDefaultModelProvider(providerId)) {
3973
+ selectionError = `${actionLabel}, but no default model is configured for provider "${providerId}". Use /model to select a model.`;
3974
+ }
3975
+ else if (providerModels.length === 0) {
3976
+ selectionError = `${actionLabel}, but no models are available for that provider. Use /model to select a model.`;
3977
+ }
3978
+ else {
3979
+ const defaultModelId = defaultModelPerProvider[providerId];
3980
+ selectedModel = providerModels.find((model) => model.id === defaultModelId);
3981
+ if (!selectedModel) {
3982
+ selectionError = `${actionLabel}, but its default model "${defaultModelId}" is not available. Use /model to select a model.`;
3983
+ }
3984
+ else {
3985
+ try {
3986
+ await this.session.setModel(selectedModel);
3987
+ }
3988
+ catch (error) {
3989
+ selectedModel = undefined;
3990
+ const errorMessage = error instanceof Error ? error.message : String(error);
3991
+ selectionError = `${actionLabel}, but selecting its default model failed: ${errorMessage}. Use /model to select a model.`;
3992
+ }
3993
+ }
3994
+ }
3995
+ }
3996
+ await this.updateAvailableProviderCount();
3997
+ this.footer.invalidate();
3998
+ this.updateEditorBorderColor();
3999
+ if (selectedModel) {
4000
+ this.showStatus(`${actionLabel}. Selected ${selectedModel.id}. Credentials saved to ${getAuthPath()}`);
4001
+ void this.maybeWarnAboutAnthropicSubscriptionAuth(selectedModel);
4002
+ this.checkDaxnutsEasterEgg(selectedModel);
4003
+ }
4004
+ else {
4005
+ this.showStatus(`${actionLabel}. Credentials saved to ${getAuthPath()}`);
4006
+ if (selectionError) {
4007
+ this.showError(selectionError);
4008
+ }
4009
+ else {
4010
+ void this.maybeWarnAboutAnthropicSubscriptionAuth();
4011
+ }
4012
+ }
4013
+ }
4014
+ showBedrockSetupDialog(providerId, providerName) {
4015
+ const restoreEditor = () => {
4016
+ this.editorContainer.clear();
4017
+ this.editorContainer.addChild(this.editor);
4018
+ this.ui.setFocus(this.editor);
4019
+ this.ui.requestRender();
4020
+ };
4021
+ const dialog = new LoginDialogComponent(this.ui, providerId, () => restoreEditor(), providerName, "Amazon Bedrock setup");
4022
+ dialog.showInfo([
4023
+ theme.fg("text", "Amazon Bedrock uses AWS credentials instead of a single API key."),
4024
+ theme.fg("text", "Configure an AWS profile, IAM keys, bearer token, or role-based credentials."),
4025
+ theme.fg("muted", "See:"),
4026
+ theme.fg("accent", ` ${path.join(getDocsPath(), "providers.md")}`),
4027
+ ]);
4028
+ this.editorContainer.clear();
4029
+ this.editorContainer.addChild(dialog);
4030
+ this.ui.setFocus(dialog);
4031
+ this.ui.requestRender();
4032
+ }
4033
+ async showApiKeyLoginDialog(providerId, providerName) {
4034
+ const previousModel = this.session.model;
4035
+ const dialog = new LoginDialogComponent(this.ui, providerId, (_success, _message) => {
4036
+ // Completion handled below
4037
+ }, providerName);
4038
+ this.editorContainer.clear();
4039
+ this.editorContainer.addChild(dialog);
4040
+ this.ui.setFocus(dialog);
4041
+ this.ui.requestRender();
4042
+ const restoreEditor = () => {
4043
+ this.editorContainer.clear();
4044
+ this.editorContainer.addChild(this.editor);
4045
+ this.ui.setFocus(this.editor);
4046
+ this.ui.requestRender();
4047
+ };
4048
+ try {
4049
+ const apiKey = (await dialog.showPrompt("Enter API key:")).trim();
4050
+ if (!apiKey) {
4051
+ throw new Error("API key cannot be empty.");
4052
+ }
4053
+ this.session.modelRegistry.authStorage.set(providerId, { type: "api_key", key: apiKey });
4054
+ restoreEditor();
4055
+ await this.completeProviderAuthentication(providerId, providerName, "api_key", previousModel);
4056
+ }
4057
+ catch (error) {
4058
+ restoreEditor();
4059
+ const errorMsg = error instanceof Error ? error.message : String(error);
4060
+ if (errorMsg !== "Login cancelled") {
4061
+ this.showError(`Failed to save API key for ${providerName}: ${errorMsg}`);
4062
+ }
4063
+ }
4064
+ }
4065
+ showOAuthLoginSelect(dialog, prompt) {
4066
+ return new Promise((resolve) => {
4067
+ const restoreDialog = () => {
4068
+ this.editorContainer.clear();
4069
+ this.editorContainer.addChild(dialog);
4070
+ this.ui.setFocus(dialog);
4071
+ this.ui.requestRender();
4072
+ };
4073
+ const labels = prompt.options.map((option) => option.label);
4074
+ const selector = new ExtensionSelectorComponent(prompt.message, labels, (optionLabel) => {
4075
+ restoreDialog();
4076
+ resolve(prompt.options.find((option) => option.label === optionLabel)?.id);
4077
+ }, () => {
4078
+ restoreDialog();
4079
+ resolve(undefined);
4080
+ });
4081
+ this.editorContainer.clear();
4082
+ this.editorContainer.addChild(selector);
4083
+ this.ui.setFocus(selector);
4084
+ this.ui.requestRender();
4085
+ });
4086
+ }
4087
+ async showLoginDialog(providerId, providerName) {
4088
+ const providerInfo = this.session.modelRegistry.authStorage
4089
+ .getOAuthProviders()
4090
+ .find((provider) => provider.id === providerId);
4091
+ const previousModel = this.session.model;
4092
+ // Providers that use callback servers (can paste redirect URL)
4093
+ const usesCallbackServer = providerInfo?.usesCallbackServer ?? false;
4094
+ // Create login dialog component
4095
+ const dialog = new LoginDialogComponent(this.ui, providerId, (_success, _message) => {
4096
+ // Completion handled below
4097
+ }, providerName);
4098
+ // Show dialog in editor container
4099
+ this.editorContainer.clear();
4100
+ this.editorContainer.addChild(dialog);
4101
+ this.ui.setFocus(dialog);
4102
+ this.ui.requestRender();
4103
+ // Promise for manual code input (racing with callback server)
4104
+ let manualCodeResolve;
4105
+ let manualCodeReject;
4106
+ const manualCodePromise = new Promise((resolve, reject) => {
4107
+ manualCodeResolve = resolve;
4108
+ manualCodeReject = reject;
4109
+ });
4110
+ // Restore editor helper
4111
+ const restoreEditor = () => {
4112
+ this.editorContainer.clear();
4113
+ this.editorContainer.addChild(this.editor);
4114
+ this.ui.setFocus(this.editor);
4115
+ this.ui.requestRender();
4116
+ };
4117
+ try {
4118
+ await this.session.modelRegistry.authStorage.login(providerId, {
4119
+ onAuth: (info) => {
4120
+ dialog.showAuth(info.url, info.instructions);
4121
+ if (usesCallbackServer) {
4122
+ // Show input for manual paste, racing with callback
4123
+ dialog
4124
+ .showManualInput("Paste redirect URL below, or complete login in browser:")
4125
+ .then((value) => {
4126
+ if (value && manualCodeResolve) {
4127
+ manualCodeResolve(value);
4128
+ manualCodeResolve = undefined;
4129
+ }
4130
+ })
4131
+ .catch(() => {
4132
+ if (manualCodeReject) {
4133
+ manualCodeReject(new Error("Login cancelled"));
4134
+ manualCodeReject = undefined;
4135
+ }
4136
+ });
4137
+ }
4138
+ // For Anthropic: onPrompt is called immediately after
4139
+ },
4140
+ onDeviceCode: (info) => {
4141
+ dialog.showDeviceCode(info);
4142
+ dialog.showWaiting("Waiting for authentication...");
4143
+ },
4144
+ onPrompt: async (prompt) => {
4145
+ return dialog.showPrompt(prompt.message, prompt.placeholder);
4146
+ },
4147
+ onProgress: (message) => {
4148
+ dialog.showProgress(message);
4149
+ },
4150
+ onSelect: (prompt) => this.showOAuthLoginSelect(dialog, prompt),
4151
+ onManualCodeInput: () => manualCodePromise,
4152
+ signal: dialog.signal,
4153
+ });
4154
+ // Success
4155
+ restoreEditor();
4156
+ await this.completeProviderAuthentication(providerId, providerName, "oauth", previousModel);
4157
+ }
4158
+ catch (error) {
4159
+ restoreEditor();
4160
+ const errorMsg = error instanceof Error ? error.message : String(error);
4161
+ if (errorMsg !== "Login cancelled") {
4162
+ this.showError(`Failed to login to ${providerName}: ${errorMsg}`);
4163
+ }
4164
+ }
4165
+ }
4166
+ // =========================================================================
4167
+ // Command handlers
4168
+ // =========================================================================
4169
+ async handleReloadCommand() {
4170
+ if (this.session.isStreaming) {
4171
+ this.showWarning("Wait for the current response to finish before reloading.");
4172
+ return;
4173
+ }
4174
+ if (this.session.isCompacting) {
4175
+ this.showWarning("Wait for compaction to finish before reloading.");
4176
+ return;
4177
+ }
4178
+ this.resetExtensionUI();
4179
+ const reloadBox = new Container();
4180
+ const borderColor = (s) => theme.fg("border", s);
4181
+ reloadBox.addChild(new DynamicBorder(borderColor));
4182
+ reloadBox.addChild(new Spacer(1));
4183
+ reloadBox.addChild(new Text(theme.fg("muted", "Reloading keybindings, extensions, skills, prompts, themes..."), 1, 0));
4184
+ reloadBox.addChild(new Spacer(1));
4185
+ reloadBox.addChild(new DynamicBorder(borderColor));
4186
+ const previousEditor = this.editor;
4187
+ this.editorContainer.clear();
4188
+ this.editorContainer.addChild(reloadBox);
4189
+ this.ui.setFocus(reloadBox);
4190
+ this.ui.requestRender(true);
4191
+ await new Promise((resolve) => process.nextTick(resolve));
4192
+ const dismissReloadBox = (editor) => {
4193
+ this.editorContainer.clear();
4194
+ this.editorContainer.addChild(editor);
4195
+ this.ui.setFocus(editor);
4196
+ this.ui.requestRender();
4197
+ };
4198
+ let chatRestoredBeforeSessionStart = false;
4199
+ let reloadBoxDismissed = false;
4200
+ const restoreChatBeforeSessionStart = () => {
4201
+ if (chatRestoredBeforeSessionStart) {
4202
+ return;
4203
+ }
4204
+ this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
4205
+ this.rebuildChatFromMessages();
4206
+ chatRestoredBeforeSessionStart = true;
4207
+ };
4208
+ try {
4209
+ await this.session.reload({ beforeSessionStart: restoreChatBeforeSessionStart });
4210
+ restoreChatBeforeSessionStart();
4211
+ configureHttpDispatcher(this.settingsManager.getHttpIdleTimeoutMs());
4212
+ this.keybindings.reload();
4213
+ const activeHeader = this.customHeader ?? this.builtInHeader;
4214
+ if (isExpandable(activeHeader)) {
4215
+ activeHeader.setExpanded(this.toolOutputExpanded);
4216
+ }
4217
+ setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
4218
+ await this.themeController.applyFromSettings();
4219
+ const editorPaddingX = this.settingsManager.getEditorPaddingX();
4220
+ const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
4221
+ this.defaultEditor.setPaddingX(editorPaddingX);
4222
+ this.defaultEditor.setAutocompleteMaxVisible(autocompleteMaxVisible);
4223
+ if (this.editor !== this.defaultEditor) {
4224
+ this.editor.setPaddingX?.(editorPaddingX);
4225
+ this.editor.setAutocompleteMaxVisible?.(autocompleteMaxVisible);
4226
+ }
4227
+ this.ui.setShowHardwareCursor(this.settingsManager.getShowHardwareCursor());
4228
+ this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
4229
+ this.setupAutocompleteProvider();
4230
+ const runner = this.session.extensionRunner;
4231
+ this.setupExtensionShortcuts(runner);
4232
+ this.showLoadedResources({
4233
+ force: false,
4234
+ showDiagnosticsWhenQuiet: true,
4235
+ });
4236
+ const savedImplicitProjectTrust = this.maybeSaveImplicitProjectTrustAfterReload();
4237
+ const modelsJsonError = this.session.modelRegistry.getError();
4238
+ if (modelsJsonError) {
4239
+ this.showError(`models.json error: ${modelsJsonError}`);
4240
+ }
4241
+ this.showStatus(savedImplicitProjectTrust
4242
+ ? "Reloaded keybindings, extensions, skills, prompts, themes; saved project trust"
4243
+ : "Reloaded keybindings, extensions, skills, prompts, themes");
4244
+ dismissReloadBox(this.editor);
4245
+ reloadBoxDismissed = true;
4246
+ }
4247
+ catch (error) {
4248
+ if (!reloadBoxDismissed) {
4249
+ dismissReloadBox(previousEditor);
4250
+ }
4251
+ this.showError(`Reload failed: ${error instanceof Error ? error.message : String(error)}`);
4252
+ }
4253
+ }
4254
+ async handleExportCommand(text) {
4255
+ const outputPath = this.getPathCommandArgument(text, "/export");
4256
+ try {
4257
+ if (outputPath?.endsWith(".jsonl")) {
4258
+ const filePath = this.session.exportToJsonl(outputPath);
4259
+ this.showStatus(`Session exported to: ${filePath}`);
4260
+ }
4261
+ else {
4262
+ const filePath = await this.session.exportToHtml(outputPath);
4263
+ this.showStatus(`Session exported to: ${filePath}`);
4264
+ }
4265
+ }
4266
+ catch (error) {
4267
+ this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
4268
+ }
4269
+ }
4270
+ getPathCommandArgument(text, command) {
4271
+ if (text === command) {
4272
+ return undefined;
4273
+ }
4274
+ if (!text.startsWith(`${command} `)) {
4275
+ return undefined;
4276
+ }
4277
+ const argsString = text.slice(command.length + 1).trimStart();
4278
+ if (!argsString) {
4279
+ return undefined;
4280
+ }
4281
+ const firstChar = argsString[0];
4282
+ if (firstChar === '"' || firstChar === "'") {
4283
+ const closingQuoteIndex = argsString.indexOf(firstChar, 1);
4284
+ if (closingQuoteIndex < 0) {
4285
+ return undefined;
4286
+ }
4287
+ return argsString.slice(1, closingQuoteIndex);
4288
+ }
4289
+ const firstWhitespaceIndex = argsString.search(/\s/);
4290
+ if (firstWhitespaceIndex < 0) {
4291
+ return argsString;
4292
+ }
4293
+ return argsString.slice(0, firstWhitespaceIndex);
4294
+ }
4295
+ async handleImportCommand(text) {
4296
+ const inputPath = this.getPathCommandArgument(text, "/import");
4297
+ if (!inputPath) {
4298
+ this.showError("Usage: /import <path.jsonl>");
4299
+ return;
4300
+ }
4301
+ const confirmed = await this.showExtensionConfirm("Import session", `Replace current session with ${inputPath}?`);
4302
+ if (!confirmed) {
4303
+ this.showStatus("Import cancelled");
4304
+ return;
4305
+ }
4306
+ try {
4307
+ if (this.loadingAnimation) {
4308
+ this.loadingAnimation.stop();
4309
+ this.loadingAnimation = undefined;
4310
+ }
4311
+ this.statusContainer.clear();
4312
+ const result = await this.runtimeHost.importFromJsonl(inputPath);
4313
+ if (result.cancelled) {
4314
+ this.showStatus("Import cancelled");
4315
+ return;
4316
+ }
4317
+ this.showStatus(`Session imported from: ${inputPath}`);
4318
+ }
4319
+ catch (error) {
4320
+ if (error instanceof MissingSessionCwdError) {
4321
+ const selectedCwd = await this.promptForMissingSessionCwd(error);
4322
+ if (!selectedCwd) {
4323
+ this.showStatus("Import cancelled");
4324
+ return;
4325
+ }
4326
+ const result = await this.runtimeHost.importFromJsonl(inputPath, selectedCwd);
4327
+ if (result.cancelled) {
4328
+ this.showStatus("Import cancelled");
4329
+ return;
4330
+ }
4331
+ this.showStatus(`Session imported from: ${inputPath}`);
4332
+ return;
4333
+ }
4334
+ if (error instanceof SessionImportFileNotFoundError) {
4335
+ this.showError(`Failed to import session: ${error.message}`);
4336
+ return;
4337
+ }
4338
+ await this.handleFatalRuntimeError("Failed to import session", error);
4339
+ }
4340
+ }
4341
+ async handleShareCommand() {
4342
+ // Check if gh is available and logged in
4343
+ try {
4344
+ const authResult = spawnSync("gh", ["auth", "status"], { encoding: "utf-8" });
4345
+ if (authResult.status !== 0) {
4346
+ this.showError("GitHub CLI is not logged in. Run 'gh auth login' first.");
4347
+ return;
4348
+ }
4349
+ }
4350
+ catch {
4351
+ this.showError("GitHub CLI (gh) is not installed. Install it from https://cli.github.com/");
4352
+ return;
4353
+ }
4354
+ // Export to a temp file
4355
+ const tmpFile = path.join(os.tmpdir(), "session.html");
4356
+ try {
4357
+ await this.session.exportToHtml(tmpFile);
4358
+ }
4359
+ catch (error) {
4360
+ this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
4361
+ return;
4362
+ }
4363
+ // Show cancellable loader, replacing the editor
4364
+ const loader = new BorderedLoader(this.ui, theme, "Creating gist...");
4365
+ this.editorContainer.clear();
4366
+ this.editorContainer.addChild(loader);
4367
+ this.ui.setFocus(loader);
4368
+ this.ui.requestRender();
4369
+ const restoreEditor = () => {
4370
+ loader.dispose();
4371
+ this.editorContainer.clear();
4372
+ this.editorContainer.addChild(this.editor);
4373
+ this.ui.setFocus(this.editor);
4374
+ try {
4375
+ fs.unlinkSync(tmpFile);
4376
+ }
4377
+ catch {
4378
+ // Ignore cleanup errors
4379
+ }
4380
+ };
4381
+ // Create a secret gist asynchronously
4382
+ let proc = null;
4383
+ loader.onAbort = () => {
4384
+ proc?.kill();
4385
+ restoreEditor();
4386
+ this.showStatus("Share cancelled");
4387
+ };
4388
+ try {
4389
+ const result = await new Promise((resolve) => {
4390
+ proc = spawn("gh", ["gist", "create", "--public=false", tmpFile]);
4391
+ let stdout = "";
4392
+ let stderr = "";
4393
+ proc.stdout?.on("data", (data) => {
4394
+ stdout += data.toString();
4395
+ });
4396
+ proc.stderr?.on("data", (data) => {
4397
+ stderr += data.toString();
4398
+ });
4399
+ proc.on("close", (code) => resolve({ stdout, stderr, code }));
4400
+ });
4401
+ if (loader.signal.aborted)
4402
+ return;
4403
+ restoreEditor();
4404
+ if (result.code !== 0) {
4405
+ const errorMsg = result.stderr?.trim() || "Unknown error";
4406
+ this.showError(`Failed to create gist: ${errorMsg}`);
4407
+ return;
4408
+ }
4409
+ // Extract gist ID from the URL returned by gh
4410
+ // gh returns something like: https://gist.github.com/username/GIST_ID
4411
+ const gistUrl = result.stdout?.trim();
4412
+ const gistId = gistUrl?.split("/").pop();
4413
+ if (!gistId) {
4414
+ this.showError("Failed to parse gist ID from gh output");
4415
+ return;
4416
+ }
4417
+ // Create the preview URL
4418
+ const previewUrl = getShareViewerUrl(gistId);
4419
+ this.showStatus(`Share URL: ${previewUrl}\nGist: ${gistUrl}`);
4420
+ }
4421
+ catch (error) {
4422
+ if (!loader.signal.aborted) {
4423
+ restoreEditor();
4424
+ this.showError(`Failed to create gist: ${error instanceof Error ? error.message : "Unknown error"}`);
4425
+ }
4426
+ }
4427
+ }
4428
+ async handleCopyCommand() {
4429
+ const text = this.session.getLastAssistantText();
4430
+ if (!text) {
4431
+ this.showError("No agent messages to copy yet.");
4432
+ return;
4433
+ }
4434
+ try {
4435
+ await copyToClipboard(text);
4436
+ this.showStatus("Copied last agent message to clipboard");
4437
+ }
4438
+ catch (error) {
4439
+ this.showError(error instanceof Error ? error.message : String(error));
4440
+ }
4441
+ }
4442
+ handleNameCommand(text) {
4443
+ const name = text.replace(/^\/name\s*/, "").trim();
4444
+ if (!name) {
4445
+ const currentName = this.sessionManager.getSessionName();
4446
+ if (currentName) {
4447
+ this.chatContainer.addChild(new Spacer(1));
4448
+ this.chatContainer.addChild(new Text(theme.fg("dim", `Session name: ${currentName}`), 1, 0));
4449
+ }
4450
+ else {
4451
+ this.showWarning("Usage: /name <name>");
4452
+ }
4453
+ this.ui.requestRender();
4454
+ return;
4455
+ }
4456
+ this.session.setSessionName(name);
4457
+ const sessionName = this.sessionManager.getSessionName();
4458
+ if (sessionName !== name) {
4459
+ this.showWarning(`Session name was normalized from ${JSON.stringify(name)} to ${JSON.stringify(sessionName)}`);
4460
+ }
4461
+ this.chatContainer.addChild(new Spacer(1));
4462
+ this.chatContainer.addChild(new Text(theme.fg("dim", `Session name set: ${sessionName ?? name}`), 1, 0));
4463
+ this.ui.requestRender();
4464
+ }
4465
+ handleSessionCommand() {
4466
+ const stats = this.session.getSessionStats();
4467
+ const sessionName = this.sessionManager.getSessionName();
4468
+ let info = `${theme.bold("Session Info")}\n\n`;
4469
+ if (sessionName) {
4470
+ info += `${theme.fg("dim", "Name:")} ${sessionName}\n`;
4471
+ }
4472
+ info += `${theme.fg("dim", "File:")} ${stats.sessionFile ?? "In-memory"}\n`;
4473
+ info += `${theme.fg("dim", "ID:")} ${stats.sessionId}\n\n`;
4474
+ info += `${theme.bold("Messages")}\n`;
4475
+ info += `${theme.fg("dim", "User:")} ${stats.userMessages}\n`;
4476
+ info += `${theme.fg("dim", "Assistant:")} ${stats.assistantMessages}\n`;
4477
+ info += `${theme.fg("dim", "Tool Calls:")} ${stats.toolCalls}\n`;
4478
+ info += `${theme.fg("dim", "Tool Results:")} ${stats.toolResults}\n`;
4479
+ info += `${theme.fg("dim", "Total:")} ${stats.totalMessages}\n\n`;
4480
+ info += `${theme.bold("Tokens")}\n`;
4481
+ info += `${theme.fg("dim", "Input:")} ${stats.tokens.input.toLocaleString()}\n`;
4482
+ info += `${theme.fg("dim", "Output:")} ${stats.tokens.output.toLocaleString()}\n`;
4483
+ if (stats.tokens.cacheRead > 0) {
4484
+ info += `${theme.fg("dim", "Cache Read:")} ${stats.tokens.cacheRead.toLocaleString()}\n`;
4485
+ }
4486
+ if (stats.tokens.cacheWrite > 0) {
4487
+ info += `${theme.fg("dim", "Cache Write:")} ${stats.tokens.cacheWrite.toLocaleString()}\n`;
4488
+ }
4489
+ info += `${theme.fg("dim", "Total:")} ${stats.tokens.total.toLocaleString()}\n`;
4490
+ if (stats.cost > 0) {
4491
+ info += `\n${theme.bold("Cost")}\n`;
4492
+ info += `${theme.fg("dim", "Total:")} ${stats.cost.toFixed(4)}`;
4493
+ }
4494
+ this.chatContainer.addChild(new Spacer(1));
4495
+ this.chatContainer.addChild(new Text(info, 1, 0));
4496
+ this.ui.requestRender();
4497
+ }
4498
+ handleChangelogCommand() {
4499
+ const changelogPath = getChangelogPath();
4500
+ const allEntries = parseChangelog(changelogPath);
4501
+ const changelogMarkdown = allEntries.length > 0
4502
+ ? allEntries
4503
+ .reverse()
4504
+ .map((e) => normalizeChangelogLinks(e.content, e))
4505
+ .join("\n\n")
4506
+ : "No changelog entries found.";
4507
+ this.chatContainer.addChild(new Spacer(1));
4508
+ this.chatContainer.addChild(new DynamicBorder());
4509
+ this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
4510
+ this.chatContainer.addChild(new Spacer(1));
4511
+ this.chatContainer.addChild(new Markdown(changelogMarkdown, 1, 1, this.getMarkdownThemeWithSettings()));
4512
+ this.chatContainer.addChild(new DynamicBorder());
4513
+ this.ui.requestRender();
4514
+ }
4515
+ /**
4516
+ * Get capitalized display string for an app keybinding action.
4517
+ */
4518
+ getAppKeyDisplay(action) {
4519
+ return keyDisplayText(action);
4520
+ }
4521
+ /**
4522
+ * Get capitalized display string for an editor keybinding action.
4523
+ */
4524
+ getEditorKeyDisplay(action) {
4525
+ return keyDisplayText(action);
4526
+ }
4527
+ handleHotkeysCommand() {
4528
+ // Navigation keybindings
4529
+ const cursorUp = this.getEditorKeyDisplay("tui.editor.cursorUp");
4530
+ const cursorDown = this.getEditorKeyDisplay("tui.editor.cursorDown");
4531
+ const cursorLeft = this.getEditorKeyDisplay("tui.editor.cursorLeft");
4532
+ const cursorRight = this.getEditorKeyDisplay("tui.editor.cursorRight");
4533
+ const cursorWordLeft = this.getEditorKeyDisplay("tui.editor.cursorWordLeft");
4534
+ const cursorWordRight = this.getEditorKeyDisplay("tui.editor.cursorWordRight");
4535
+ const cursorLineStart = this.getEditorKeyDisplay("tui.editor.cursorLineStart");
4536
+ const cursorLineEnd = this.getEditorKeyDisplay("tui.editor.cursorLineEnd");
4537
+ const jumpForward = this.getEditorKeyDisplay("tui.editor.jumpForward");
4538
+ const jumpBackward = this.getEditorKeyDisplay("tui.editor.jumpBackward");
4539
+ const pageUp = this.getEditorKeyDisplay("tui.editor.pageUp");
4540
+ const pageDown = this.getEditorKeyDisplay("tui.editor.pageDown");
4541
+ // Editing keybindings
4542
+ const submit = this.getEditorKeyDisplay("tui.input.submit");
4543
+ const newLine = this.getEditorKeyDisplay("tui.input.newLine");
4544
+ const deleteWordBackward = this.getEditorKeyDisplay("tui.editor.deleteWordBackward");
4545
+ const deleteWordForward = this.getEditorKeyDisplay("tui.editor.deleteWordForward");
4546
+ const deleteToLineStart = this.getEditorKeyDisplay("tui.editor.deleteToLineStart");
4547
+ const deleteToLineEnd = this.getEditorKeyDisplay("tui.editor.deleteToLineEnd");
4548
+ const yank = this.getEditorKeyDisplay("tui.editor.yank");
4549
+ const yankPop = this.getEditorKeyDisplay("tui.editor.yankPop");
4550
+ const undo = this.getEditorKeyDisplay("tui.editor.undo");
4551
+ const tab = this.getEditorKeyDisplay("tui.input.tab");
4552
+ // App keybindings
4553
+ const interrupt = this.getAppKeyDisplay("app.interrupt");
4554
+ const clear = this.getAppKeyDisplay("app.clear");
4555
+ const exit = this.getAppKeyDisplay("app.exit");
4556
+ const suspend = this.getAppKeyDisplay("app.suspend");
4557
+ const cycleThinkingLevel = this.getAppKeyDisplay("app.thinking.cycle");
4558
+ const cycleModelForward = this.getAppKeyDisplay("app.model.cycleForward");
4559
+ const selectModel = this.getAppKeyDisplay("app.model.select");
4560
+ const expandTools = this.getAppKeyDisplay("app.tools.expand");
4561
+ const toggleThinking = this.getAppKeyDisplay("app.thinking.toggle");
4562
+ const externalEditor = this.getAppKeyDisplay("app.editor.external");
4563
+ const cycleModelBackward = this.getAppKeyDisplay("app.model.cycleBackward");
4564
+ const followUp = this.getAppKeyDisplay("app.message.followUp");
4565
+ const dequeue = this.getAppKeyDisplay("app.message.dequeue");
4566
+ const pasteImage = this.getAppKeyDisplay("app.clipboard.pasteImage");
4567
+ let hotkeys = `
4568
+ **Navigation**
4569
+ | Key | Action |
4570
+ |-----|--------|
4571
+ | \`${cursorUp}\` / \`${cursorDown}\` / \`${cursorLeft}\` / \`${cursorRight}\` | Move cursor / browse history |
4572
+ | \`${cursorWordLeft}\` / \`${cursorWordRight}\` | Move by word |
4573
+ | \`${cursorLineStart}\` | Start of line |
4574
+ | \`${cursorLineEnd}\` | End of line |
4575
+ | \`${jumpForward}\` | Jump forward to character |
4576
+ | \`${jumpBackward}\` | Jump backward to character |
4577
+ | \`${pageUp}\` / \`${pageDown}\` | Scroll by page |
4578
+
4579
+ **Editing**
4580
+ | Key | Action |
4581
+ |-----|--------|
4582
+ | \`${submit}\` | Send message |
4583
+ | \`${newLine}\` | New line${process.platform === "win32" ? " (Ctrl+Enter on Windows Terminal)" : ""} |
4584
+ | \`${deleteWordBackward}\` | Delete word backwards |
4585
+ | \`${deleteWordForward}\` | Delete word forwards |
4586
+ | \`${deleteToLineStart}\` | Delete to start of line |
4587
+ | \`${deleteToLineEnd}\` | Delete to end of line |
4588
+ | \`${yank}\` | Paste the most-recently-deleted text |
4589
+ | \`${yankPop}\` | Cycle through the deleted text after pasting |
4590
+ | \`${undo}\` | Undo |
4591
+
4592
+ **Other**
4593
+ | Key | Action |
4594
+ |-----|--------|
4595
+ | \`${tab}\` | Path completion / accept autocomplete |
4596
+ | \`${interrupt}\` | Cancel autocomplete / abort streaming |
4597
+ | \`${clear}\` | Clear editor (first) / exit (second) |
4598
+ | \`${exit}\` | Exit (when editor is empty) |
4599
+ | \`${suspend}\` | Suspend to background |
4600
+ | \`${cycleThinkingLevel}\` | Cycle thinking level |
4601
+ | \`${cycleModelForward}\` / \`${cycleModelBackward}\` | Cycle models |
4602
+ | \`${selectModel}\` | Open model selector |
4603
+ | \`${expandTools}\` | Toggle tool output expansion |
4604
+ | \`${toggleThinking}\` | Toggle thinking block visibility |
4605
+ | \`${externalEditor}\` | Edit message in external editor |
4606
+ | \`${followUp}\` | Queue follow-up message |
4607
+ | \`${dequeue}\` | Restore queued messages |
4608
+ | \`${pasteImage}\` | Paste image from clipboard |
4609
+ | \`/\` | Slash commands |
4610
+ | \`!\` | Run bash command |
4611
+ | \`!!\` | Run bash command (excluded from context) |
4612
+ `;
4613
+ // Add extension-registered shortcuts
4614
+ const extensionRunner = this.session.extensionRunner;
4615
+ const shortcuts = extensionRunner.getShortcuts(this.keybindings.getEffectiveConfig());
4616
+ if (shortcuts.size > 0) {
4617
+ hotkeys += `
4618
+ **Extensions**
4619
+ | Key | Action |
4620
+ |-----|--------|
4621
+ `;
4622
+ for (const [key, shortcut] of shortcuts) {
4623
+ const description = shortcut.description ?? shortcut.extensionPath;
4624
+ const keyDisplay = formatKeyText(key, { capitalize: true });
4625
+ hotkeys += `| \`${keyDisplay}\` | ${description} |\n`;
4626
+ }
4627
+ }
4628
+ this.chatContainer.addChild(new Spacer(1));
4629
+ this.chatContainer.addChild(new DynamicBorder());
4630
+ this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "Keyboard Shortcuts")), 1, 0));
4631
+ this.chatContainer.addChild(new Spacer(1));
4632
+ this.chatContainer.addChild(new Markdown(hotkeys.trim(), 1, 1, this.getMarkdownThemeWithSettings()));
4633
+ this.chatContainer.addChild(new DynamicBorder());
4634
+ this.ui.requestRender();
4635
+ }
4636
+ async handleClearCommand() {
4637
+ if (this.loadingAnimation) {
4638
+ this.loadingAnimation.stop();
4639
+ this.loadingAnimation = undefined;
4640
+ }
4641
+ this.statusContainer.clear();
4642
+ try {
4643
+ const result = await this.runtimeHost.newSession();
4644
+ if (result.cancelled) {
4645
+ return;
4646
+ }
4647
+ this.chatContainer.addChild(new Spacer(1));
4648
+ this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
4649
+ this.ui.requestRender();
4650
+ }
4651
+ catch (error) {
4652
+ await this.handleFatalRuntimeError("Failed to create session", error);
4653
+ }
4654
+ }
4655
+ handleDebugCommand() {
4656
+ const width = this.ui.terminal.columns;
4657
+ const height = this.ui.terminal.rows;
4658
+ const allLines = this.ui.render(width);
4659
+ const debugLogPath = getDebugLogPath();
4660
+ const debugData = [
4661
+ `Debug output at ${new Date().toISOString()}`,
4662
+ `Terminal: ${width}x${height}`,
4663
+ `Total lines: ${allLines.length}`,
4664
+ "",
4665
+ "=== All rendered lines with visible widths ===",
4666
+ ...allLines.map((line, idx) => {
4667
+ const vw = visibleWidth(line);
4668
+ const escaped = JSON.stringify(line);
4669
+ return `[${idx}] (w=${vw}) ${escaped}`;
4670
+ }),
4671
+ "",
4672
+ "=== Agent messages (JSONL) ===",
4673
+ ...this.session.messages.map((msg) => JSON.stringify(msg)),
4674
+ "",
4675
+ ].join("\n");
4676
+ fs.mkdirSync(path.dirname(debugLogPath), { recursive: true });
4677
+ fs.writeFileSync(debugLogPath, debugData);
4678
+ this.chatContainer.addChild(new Spacer(1));
4679
+ this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ Debug log written")}\n${theme.fg("muted", debugLogPath)}`, 1, 1));
4680
+ this.ui.requestRender();
4681
+ }
4682
+ handleArminSaysHi() {
4683
+ this.chatContainer.addChild(new Spacer(1));
4684
+ this.chatContainer.addChild(new ArminComponent(this.ui));
4685
+ this.ui.requestRender();
4686
+ }
4687
+ handleDementedDelves() {
4688
+ this.chatContainer.addChild(new Spacer(1));
4689
+ this.chatContainer.addChild(new EarendilAnnouncementComponent());
4690
+ this.ui.requestRender();
4691
+ }
4692
+ handleDaxnuts() {
4693
+ this.chatContainer.addChild(new Spacer(1));
4694
+ this.chatContainer.addChild(new DaxnutsComponent(this.ui));
4695
+ this.ui.requestRender();
4696
+ }
4697
+ checkDaxnutsEasterEgg(model) {
4698
+ if (model.provider === "opencode" && model.id.toLowerCase().includes("kimi-k2.5")) {
4699
+ this.handleDaxnuts();
4700
+ }
4701
+ }
4702
+ async handleBashCommand(command, excludeFromContext = false) {
4703
+ const extensionRunner = this.session.extensionRunner;
4704
+ // Emit user_bash event to let extensions intercept
4705
+ const eventResult = await extensionRunner.emitUserBash({
4706
+ type: "user_bash",
4707
+ command,
4708
+ excludeFromContext,
4709
+ cwd: this.sessionManager.getCwd(),
4710
+ });
4711
+ // If extension returned a full result, use it directly
4712
+ if (eventResult?.result) {
4713
+ const result = eventResult.result;
4714
+ // Create UI component for display
4715
+ this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
4716
+ if (this.session.isStreaming) {
4717
+ this.pendingMessagesContainer.addChild(this.bashComponent);
4718
+ this.pendingBashComponents.push(this.bashComponent);
4719
+ }
4720
+ else {
4721
+ this.chatContainer.addChild(this.bashComponent);
4722
+ }
4723
+ // Show output and complete
4724
+ if (result.output) {
4725
+ this.bashComponent.appendOutput(result.output);
4726
+ }
4727
+ this.bashComponent.setComplete(result.exitCode, result.cancelled, result.truncated ? { truncated: true, content: result.output } : undefined, result.fullOutputPath);
4728
+ // Record the result in session
4729
+ this.session.recordBashResult(command, result, { excludeFromContext });
4730
+ this.bashComponent = undefined;
4731
+ this.ui.requestRender();
4732
+ return;
4733
+ }
4734
+ // Normal execution path (possibly with custom operations)
4735
+ const isDeferred = this.session.isStreaming;
4736
+ this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
4737
+ if (isDeferred) {
4738
+ // Show in pending area when agent is streaming
4739
+ this.pendingMessagesContainer.addChild(this.bashComponent);
4740
+ this.pendingBashComponents.push(this.bashComponent);
4741
+ }
4742
+ else {
4743
+ // Show in chat immediately when agent is idle
4744
+ this.chatContainer.addChild(this.bashComponent);
4745
+ }
4746
+ this.ui.requestRender();
4747
+ try {
4748
+ const result = await this.session.executeBash(command, (chunk) => {
4749
+ if (this.bashComponent) {
4750
+ this.bashComponent.appendOutput(chunk);
4751
+ this.ui.requestRender();
4752
+ }
4753
+ }, { excludeFromContext, operations: eventResult?.operations });
4754
+ if (this.bashComponent) {
4755
+ this.bashComponent.setComplete(result.exitCode, result.cancelled, result.truncated ? { truncated: true, content: result.output } : undefined, result.fullOutputPath);
4756
+ }
4757
+ }
4758
+ catch (error) {
4759
+ if (this.bashComponent) {
4760
+ this.bashComponent.setComplete(undefined, false);
4761
+ }
4762
+ this.showError(`Bash command failed: ${error instanceof Error ? error.message : "Unknown error"}`);
4763
+ }
4764
+ this.bashComponent = undefined;
4765
+ this.ui.requestRender();
4766
+ }
4767
+ async handleCompactCommand(customInstructions) {
4768
+ if (this.loadingAnimation) {
4769
+ this.loadingAnimation.stop();
4770
+ this.loadingAnimation = undefined;
4771
+ }
4772
+ this.statusContainer.clear();
4773
+ try {
4774
+ await this.session.compact(customInstructions);
4775
+ }
4776
+ catch {
4777
+ // Ignore, will be emitted as an event
4778
+ }
4779
+ }
4780
+ stop() {
4781
+ if (this.settingsManager.getShowTerminalProgress()) {
4782
+ this.ui.terminal.setProgress(false);
4783
+ }
4784
+ if (this.loadingAnimation) {
4785
+ this.loadingAnimation.stop();
4786
+ this.loadingAnimation = undefined;
4787
+ }
4788
+ this.themeController.disableAutoSync();
4789
+ this.clearExtensionTerminalInputListeners();
4790
+ this.footer.dispose();
4791
+ this.footerDataProvider.dispose();
4792
+ if (this.unsubscribe) {
4793
+ this.unsubscribe();
4794
+ }
4795
+ if (this.isInitialized) {
4796
+ this.ui.stop();
4797
+ this.isInitialized = false;
4798
+ }
4799
+ this.unregisterSignalHandlers();
4800
+ }
4801
+ }
4802
+ //# sourceMappingURL=interactive-mode.js.map