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