@openadapter/koda 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (357) hide show
  1. package/CHANGELOG.md +4448 -0
  2. package/README.md +97 -0
  3. package/dist/bun/cli.d.ts +2 -0
  4. package/dist/bun/cli.js +2 -0
  5. package/dist/bun/register-bedrock.d.ts +1 -0
  6. package/dist/bun/register-bedrock.js +1 -0
  7. package/dist/bun/restore-sandbox-env.d.ts +12 -0
  8. package/dist/bun/restore-sandbox-env.js +1 -0
  9. package/dist/cli/args.d.ts +55 -0
  10. package/dist/cli/args.js +167 -0
  11. package/dist/cli/config-selector.d.ts +13 -0
  12. package/dist/cli/config-selector.js +1 -0
  13. package/dist/cli/file-processor.d.ts +14 -0
  14. package/dist/cli/file-processor.js +7 -0
  15. package/dist/cli/import-sessions.d.ts +34 -0
  16. package/dist/cli/import-sessions.js +6 -0
  17. package/dist/cli/initial-message.d.ts +17 -0
  18. package/dist/cli/initial-message.js +1 -0
  19. package/dist/cli/list-models.d.ts +8 -0
  20. package/dist/cli/list-models.js +2 -0
  21. package/dist/cli/openadapter-setup.d.ts +35 -0
  22. package/dist/cli/openadapter-setup.js +4 -0
  23. package/dist/cli/session-picker.d.ts +8 -0
  24. package/dist/cli/session-picker.js +1 -0
  25. package/dist/cli.d.ts +2 -0
  26. package/dist/cli.js +2 -0
  27. package/dist/config.d.ts +92 -0
  28. package/dist/config.js +1 -0
  29. package/dist/core/agent-session-runtime.d.ts +116 -0
  30. package/dist/core/agent-session-runtime.js +1 -0
  31. package/dist/core/agent-session-services.d.ts +86 -0
  32. package/dist/core/agent-session-services.js +1 -0
  33. package/dist/core/agent-session.d.ts +752 -0
  34. package/dist/core/agent-session.js +32 -0
  35. package/dist/core/auth-guidance.d.ts +4 -0
  36. package/dist/core/auth-guidance.js +8 -0
  37. package/dist/core/auth-storage.d.ts +140 -0
  38. package/dist/core/auth-storage.js +1 -0
  39. package/dist/core/bash-executor.d.ts +31 -0
  40. package/dist/core/bash-executor.js +1 -0
  41. package/dist/core/compaction/branch-summarization.d.ts +89 -0
  42. package/dist/core/compaction/branch-summarization.js +38 -0
  43. package/dist/core/compaction/compaction.d.ts +120 -0
  44. package/dist/core/compaction/compaction.js +104 -0
  45. package/dist/core/compaction/index.d.ts +6 -0
  46. package/dist/core/compaction/index.js +1 -0
  47. package/dist/core/compaction/utils.d.ts +37 -0
  48. package/dist/core/compaction/utils.js +19 -0
  49. package/dist/core/defaults.d.ts +2 -0
  50. package/dist/core/defaults.js +1 -0
  51. package/dist/core/diagnostics.d.ts +14 -0
  52. package/dist/core/diagnostics.js +0 -0
  53. package/dist/core/event-bus.d.ts +8 -0
  54. package/dist/core/event-bus.js +1 -0
  55. package/dist/core/exec.d.ts +28 -0
  56. package/dist/core/exec.js +1 -0
  57. package/dist/core/export-html/ansi-to-html.d.ts +21 -0
  58. package/dist/core/export-html/ansi-to-html.js +1 -0
  59. package/dist/core/export-html/index.d.ts +36 -0
  60. package/dist/core/export-html/index.js +2 -0
  61. package/dist/core/export-html/template.css +1066 -0
  62. package/dist/core/export-html/template.html +55 -0
  63. package/dist/core/export-html/template.js +72 -0
  64. package/dist/core/export-html/tool-renderer.d.ts +33 -0
  65. package/dist/core/export-html/tool-renderer.js +1 -0
  66. package/dist/core/export-html/vendor/highlight.min.js +8 -0
  67. package/dist/core/export-html/vendor/marked.min.js +56 -0
  68. package/dist/core/extensions/index.d.ts +11 -0
  69. package/dist/core/extensions/index.js +1 -0
  70. package/dist/core/extensions/loader.d.ts +23 -0
  71. package/dist/core/extensions/loader.js +1 -0
  72. package/dist/core/extensions/runner.d.ts +160 -0
  73. package/dist/core/extensions/runner.js +1 -0
  74. package/dist/core/extensions/types.d.ts +1180 -0
  75. package/dist/core/extensions/types.js +1 -0
  76. package/dist/core/extensions/wrapper.d.ts +19 -0
  77. package/dist/core/extensions/wrapper.js +1 -0
  78. package/dist/core/footer-data-provider.d.ts +53 -0
  79. package/dist/core/footer-data-provider.js +1 -0
  80. package/dist/core/http-dispatcher.d.ts +20 -0
  81. package/dist/core/http-dispatcher.js +1 -0
  82. package/dist/core/index.d.ts +11 -0
  83. package/dist/core/index.js +1 -0
  84. package/dist/core/keybindings.d.ts +352 -0
  85. package/dist/core/keybindings.js +1 -0
  86. package/dist/core/messages.d.ts +76 -0
  87. package/dist/core/messages.js +17 -0
  88. package/dist/core/model-registry.d.ts +149 -0
  89. package/dist/core/model-registry.js +9 -0
  90. package/dist/core/model-resolver.d.ts +109 -0
  91. package/dist/core/model-resolver.js +1 -0
  92. package/dist/core/output-guard.d.ts +6 -0
  93. package/dist/core/output-guard.js +1 -0
  94. package/dist/core/package-manager.d.ts +203 -0
  95. package/dist/core/package-manager.js +3 -0
  96. package/dist/core/prompt-templates.d.ts +51 -0
  97. package/dist/core/prompt-templates.js +2 -0
  98. package/dist/core/provider-attribution.d.ts +3 -0
  99. package/dist/core/provider-attribution.js +1 -0
  100. package/dist/core/provider-display-names.d.ts +1 -0
  101. package/dist/core/provider-display-names.js +1 -0
  102. package/dist/core/resolve-config-value.d.ts +30 -0
  103. package/dist/core/resolve-config-value.js +1 -0
  104. package/dist/core/resource-loader.d.ts +193 -0
  105. package/dist/core/resource-loader.js +1 -0
  106. package/dist/core/sdk.d.ts +108 -0
  107. package/dist/core/sdk.js +1 -0
  108. package/dist/core/session-cwd.d.ts +18 -0
  109. package/dist/core/session-cwd.js +7 -0
  110. package/dist/core/session-manager.d.ts +331 -0
  111. package/dist/core/session-manager.js +11 -0
  112. package/dist/core/settings-manager.d.ts +265 -0
  113. package/dist/core/settings-manager.js +1 -0
  114. package/dist/core/skills.d.ts +59 -0
  115. package/dist/core/skills.js +4 -0
  116. package/dist/core/slash-commands.d.ts +13 -0
  117. package/dist/core/slash-commands.js +1 -0
  118. package/dist/core/source-info.d.ts +17 -0
  119. package/dist/core/source-info.js +1 -0
  120. package/dist/core/system-prompt.d.ts +27 -0
  121. package/dist/core/system-prompt.js +52 -0
  122. package/dist/core/telemetry.d.ts +2 -0
  123. package/dist/core/telemetry.js +1 -0
  124. package/dist/core/timings.d.ts +7 -0
  125. package/dist/core/timings.js +3 -0
  126. package/dist/core/tools/bash.d.ts +67 -0
  127. package/dist/core/tools/bash.js +18 -0
  128. package/dist/core/tools/edit-diff.d.ts +86 -0
  129. package/dist/core/tools/edit-diff.js +16 -0
  130. package/dist/core/tools/edit.d.ts +50 -0
  131. package/dist/core/tools/edit.js +2 -0
  132. package/dist/core/tools/file-mutation-queue.d.ts +5 -0
  133. package/dist/core/tools/file-mutation-queue.js +1 -0
  134. package/dist/core/tools/find.d.ts +34 -0
  135. package/dist/core/tools/find.js +13 -0
  136. package/dist/core/tools/grep.d.ts +36 -0
  137. package/dist/core/tools/grep.js +13 -0
  138. package/dist/core/tools/index.d.ts +39 -0
  139. package/dist/core/tools/index.js +1 -0
  140. package/dist/core/tools/ls.d.ts +36 -0
  141. package/dist/core/tools/ls.js +9 -0
  142. package/dist/core/tools/output-accumulator.d.ts +51 -0
  143. package/dist/core/tools/output-accumulator.js +4 -0
  144. package/dist/core/tools/path-utils.d.ts +9 -0
  145. package/dist/core/tools/path-utils.js +1 -0
  146. package/dist/core/tools/read.d.ts +34 -0
  147. package/dist/core/tools/read.js +22 -0
  148. package/dist/core/tools/render-utils.d.ts +23 -0
  149. package/dist/core/tools/render-utils.js +4 -0
  150. package/dist/core/tools/tool-definition-wrapper.d.ts +13 -0
  151. package/dist/core/tools/tool-definition-wrapper.js +1 -0
  152. package/dist/core/tools/truncate.d.ts +69 -0
  153. package/dist/core/tools/truncate.js +5 -0
  154. package/dist/core/tools/write.d.ts +25 -0
  155. package/dist/core/tools/write.js +13 -0
  156. package/dist/index.d.ts +30 -0
  157. package/dist/index.js +1 -0
  158. package/dist/main.d.ts +11 -0
  159. package/dist/main.js +1 -0
  160. package/dist/migrations.d.ts +32 -0
  161. package/dist/migrations.js +8 -0
  162. package/dist/modes/index.d.ts +8 -0
  163. package/dist/modes/index.js +1 -0
  164. package/dist/modes/interactive/assets/clankolas.png +0 -0
  165. package/dist/modes/interactive/components/armin.d.ts +33 -0
  166. package/dist/modes/interactive/components/armin.js +1 -0
  167. package/dist/modes/interactive/components/assistant-message.d.ts +19 -0
  168. package/dist/modes/interactive/components/assistant-message.js +1 -0
  169. package/dist/modes/interactive/components/bash-execution.d.ts +33 -0
  170. package/dist/modes/interactive/components/bash-execution.js +13 -0
  171. package/dist/modes/interactive/components/bordered-loader.d.ts +15 -0
  172. package/dist/modes/interactive/components/bordered-loader.js +1 -0
  173. package/dist/modes/interactive/components/branch-summary-message.d.ts +15 -0
  174. package/dist/modes/interactive/components/branch-summary-message.js +3 -0
  175. package/dist/modes/interactive/components/compaction-summary-message.d.ts +15 -0
  176. package/dist/modes/interactive/components/compaction-summary-message.js +3 -0
  177. package/dist/modes/interactive/components/config-selector.d.ts +70 -0
  178. package/dist/modes/interactive/components/config-selector.js +1 -0
  179. package/dist/modes/interactive/components/countdown-timer.d.ts +13 -0
  180. package/dist/modes/interactive/components/countdown-timer.js +1 -0
  181. package/dist/modes/interactive/components/custom-editor.d.ts +20 -0
  182. package/dist/modes/interactive/components/custom-editor.js +1 -0
  183. package/dist/modes/interactive/components/custom-message.d.ts +19 -0
  184. package/dist/modes/interactive/components/custom-message.js +2 -0
  185. package/dist/modes/interactive/components/daxnuts.d.ts +22 -0
  186. package/dist/modes/interactive/components/daxnuts.js +1 -0
  187. package/dist/modes/interactive/components/diff.d.ts +11 -0
  188. package/dist/modes/interactive/components/diff.js +3 -0
  189. package/dist/modes/interactive/components/dynamic-border.d.ts +14 -0
  190. package/dist/modes/interactive/components/dynamic-border.js +1 -0
  191. package/dist/modes/interactive/components/earendil-announcement.d.ts +4 -0
  192. package/dist/modes/interactive/components/earendil-announcement.js +1 -0
  193. package/dist/modes/interactive/components/extension-editor.d.ts +19 -0
  194. package/dist/modes/interactive/components/extension-editor.js +3 -0
  195. package/dist/modes/interactive/components/extension-input.d.ts +22 -0
  196. package/dist/modes/interactive/components/extension-input.js +2 -0
  197. package/dist/modes/interactive/components/extension-selector.d.ts +25 -0
  198. package/dist/modes/interactive/components/extension-selector.js +2 -0
  199. package/dist/modes/interactive/components/footer.d.ts +27 -0
  200. package/dist/modes/interactive/components/footer.js +1 -0
  201. package/dist/modes/interactive/components/index.d.ts +31 -0
  202. package/dist/modes/interactive/components/index.js +1 -0
  203. package/dist/modes/interactive/components/keybinding-hints.d.ts +12 -0
  204. package/dist/modes/interactive/components/keybinding-hints.js +1 -0
  205. package/dist/modes/interactive/components/login-dialog.d.ts +51 -0
  206. package/dist/modes/interactive/components/login-dialog.js +1 -0
  207. package/dist/modes/interactive/components/model-selector.d.ts +46 -0
  208. package/dist/modes/interactive/components/model-selector.js +2 -0
  209. package/dist/modes/interactive/components/oauth-selector.d.ts +30 -0
  210. package/dist/modes/interactive/components/oauth-selector.js +1 -0
  211. package/dist/modes/interactive/components/scoped-models-selector.d.ts +41 -0
  212. package/dist/modes/interactive/components/scoped-models-selector.js +1 -0
  213. package/dist/modes/interactive/components/session-selector-search.d.ts +22 -0
  214. package/dist/modes/interactive/components/session-selector-search.js +1 -0
  215. package/dist/modes/interactive/components/session-selector.d.ts +95 -0
  216. package/dist/modes/interactive/components/session-selector.js +2 -0
  217. package/dist/modes/interactive/components/settings-selector.d.ts +69 -0
  218. package/dist/modes/interactive/components/settings-selector.js +1 -0
  219. package/dist/modes/interactive/components/show-images-selector.d.ts +9 -0
  220. package/dist/modes/interactive/components/show-images-selector.js +1 -0
  221. package/dist/modes/interactive/components/skill-invocation-message.d.ts +16 -0
  222. package/dist/modes/interactive/components/skill-invocation-message.js +3 -0
  223. package/dist/modes/interactive/components/theme-selector.d.ts +10 -0
  224. package/dist/modes/interactive/components/theme-selector.js +1 -0
  225. package/dist/modes/interactive/components/thinking-selector.d.ts +10 -0
  226. package/dist/modes/interactive/components/thinking-selector.js +1 -0
  227. package/dist/modes/interactive/components/tool-execution.d.ts +62 -0
  228. package/dist/modes/interactive/components/tool-execution.js +4 -0
  229. package/dist/modes/interactive/components/tree-selector.d.ts +88 -0
  230. package/dist/modes/interactive/components/tree-selector.js +1 -0
  231. package/dist/modes/interactive/components/user-message-selector.d.ts +29 -0
  232. package/dist/modes/interactive/components/user-message-selector.js +1 -0
  233. package/dist/modes/interactive/components/user-message.d.ts +9 -0
  234. package/dist/modes/interactive/components/user-message.js +1 -0
  235. package/dist/modes/interactive/components/visual-truncate.d.ts +23 -0
  236. package/dist/modes/interactive/components/visual-truncate.js +1 -0
  237. package/dist/modes/interactive/interactive-mode.d.ts +420 -0
  238. package/dist/modes/interactive/interactive-mode.js +116 -0
  239. package/dist/modes/interactive/theme/dark.json +86 -0
  240. package/dist/modes/interactive/theme/light.json +85 -0
  241. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  242. package/dist/modes/interactive/theme/theme.d.ts +101 -0
  243. package/dist/modes/interactive/theme/theme.js +18 -0
  244. package/dist/modes/print-mode.d.ts +27 -0
  245. package/dist/modes/print-mode.js +4 -0
  246. package/dist/modes/rpc/jsonl.d.ts +16 -0
  247. package/dist/modes/rpc/jsonl.js +3 -0
  248. package/dist/modes/rpc/rpc-client.d.ts +226 -0
  249. package/dist/modes/rpc/rpc-client.js +1 -0
  250. package/dist/modes/rpc/rpc-mode.d.ts +19 -0
  251. package/dist/modes/rpc/rpc-mode.js +1 -0
  252. package/dist/modes/rpc/rpc-types.d.ts +419 -0
  253. package/dist/modes/rpc/rpc-types.js +0 -0
  254. package/dist/package-manager-cli.d.ts +3 -0
  255. package/dist/package-manager-cli.js +49 -0
  256. package/dist/utils/ansi.d.ts +1 -0
  257. package/dist/utils/ansi.js +1 -0
  258. package/dist/utils/auto-update.d.ts +13 -0
  259. package/dist/utils/auto-update.js +1 -0
  260. package/dist/utils/changelog.d.ts +20 -0
  261. package/dist/utils/changelog.js +4 -0
  262. package/dist/utils/child-process.d.ts +14 -0
  263. package/dist/utils/child-process.js +1 -0
  264. package/dist/utils/clipboard-image.d.ts +10 -0
  265. package/dist/utils/clipboard-image.js +1 -0
  266. package/dist/utils/clipboard-native.d.ts +9 -0
  267. package/dist/utils/clipboard-native.js +1 -0
  268. package/dist/utils/clipboard.d.ts +1 -0
  269. package/dist/utils/clipboard.js +1 -0
  270. package/dist/utils/deprecation.d.ts +3 -0
  271. package/dist/utils/deprecation.js +1 -0
  272. package/dist/utils/exif-orientation.d.ts +4 -0
  273. package/dist/utils/exif-orientation.js +1 -0
  274. package/dist/utils/frontmatter.d.ts +7 -0
  275. package/dist/utils/frontmatter.js +4 -0
  276. package/dist/utils/fs-watch.d.ts +4 -0
  277. package/dist/utils/fs-watch.js +1 -0
  278. package/dist/utils/git.d.ts +25 -0
  279. package/dist/utils/git.js +1 -0
  280. package/dist/utils/html.d.ts +6 -0
  281. package/dist/utils/html.js +1 -0
  282. package/dist/utils/image-convert.d.ts +8 -0
  283. package/dist/utils/image-convert.js +1 -0
  284. package/dist/utils/image-resize-core.d.ts +29 -0
  285. package/dist/utils/image-resize-core.js +1 -0
  286. package/dist/utils/image-resize-worker.d.ts +1 -0
  287. package/dist/utils/image-resize-worker.js +1 -0
  288. package/dist/utils/image-resize.d.ts +15 -0
  289. package/dist/utils/image-resize.js +1 -0
  290. package/dist/utils/json.d.ts +2 -0
  291. package/dist/utils/json.js +1 -0
  292. package/dist/utils/koda-user-agent.d.ts +1 -0
  293. package/dist/utils/koda-user-agent.js +1 -0
  294. package/dist/utils/mime.d.ts +2 -0
  295. package/dist/utils/mime.js +1 -0
  296. package/dist/utils/paths.d.ts +30 -0
  297. package/dist/utils/paths.js +1 -0
  298. package/dist/utils/photon.d.ts +20 -0
  299. package/dist/utils/photon.js +1 -0
  300. package/dist/utils/shell.d.ts +29 -0
  301. package/dist/utils/shell.js +8 -0
  302. package/dist/utils/sleep.d.ts +4 -0
  303. package/dist/utils/sleep.js +1 -0
  304. package/dist/utils/syntax-highlight.d.ts +11 -0
  305. package/dist/utils/syntax-highlight.js +2 -0
  306. package/dist/utils/tools-manager.d.ts +2 -0
  307. package/dist/utils/tools-manager.js +1 -0
  308. package/dist/utils/version-check.d.ts +14 -0
  309. package/dist/utils/version-check.js +1 -0
  310. package/dist/utils/windows-self-update.d.ts +2 -0
  311. package/dist/utils/windows-self-update.js +1 -0
  312. package/docs/compaction.md +394 -0
  313. package/docs/custom-provider.md +736 -0
  314. package/docs/development.md +71 -0
  315. package/docs/docs.json +148 -0
  316. package/docs/extensions.md +2626 -0
  317. package/docs/images/doom-extension.png +0 -0
  318. package/docs/images/exy.png +0 -0
  319. package/docs/images/interactive-mode.png +0 -0
  320. package/docs/images/tree-view.png +0 -0
  321. package/docs/index.md +80 -0
  322. package/docs/json.md +82 -0
  323. package/docs/keybindings.md +197 -0
  324. package/docs/models.md +493 -0
  325. package/docs/packages.md +226 -0
  326. package/docs/prompt-templates.md +88 -0
  327. package/docs/providers.md +253 -0
  328. package/docs/quickstart.md +165 -0
  329. package/docs/rpc.md +1408 -0
  330. package/docs/sdk.md +1137 -0
  331. package/docs/session-format.md +412 -0
  332. package/docs/sessions.md +145 -0
  333. package/docs/settings.md +281 -0
  334. package/docs/shell-aliases.md +13 -0
  335. package/docs/skills.md +231 -0
  336. package/docs/terminal-setup.md +114 -0
  337. package/docs/termux.md +127 -0
  338. package/docs/themes.md +295 -0
  339. package/docs/tmux.md +61 -0
  340. package/docs/tui.md +927 -0
  341. package/docs/usage.md +288 -0
  342. package/docs/windows.md +17 -0
  343. package/npm-shrinkwrap.json +1792 -0
  344. package/openadapter/extensions/koda-ask.js +12 -0
  345. package/openadapter/extensions/koda-bg.js +14 -0
  346. package/openadapter/extensions/koda-commands.mjs +15 -0
  347. package/openadapter/extensions/koda-help.js +8 -0
  348. package/openadapter/extensions/koda-memory.js +16 -0
  349. package/openadapter/extensions/koda-status.js +1 -0
  350. package/openadapter/extensions/koda-todo.js +4 -0
  351. package/openadapter/extensions/koda-vision.js +4 -0
  352. package/openadapter/extensions/koda-web.js +7 -0
  353. package/openadapter/setup.mjs +175 -0
  354. package/openadapter/skills/code-review/SKILL.md +22 -0
  355. package/openadapter/skills/debugging/SKILL.md +28 -0
  356. package/openadapter/skills/frontend/SKILL.md +38 -0
  357. package/package.json +108 -0
package/docs/tui.md ADDED
@@ -0,0 +1,927 @@
1
+ > pi can create TUI components. Ask it to build one for your use case.
2
+
3
+ # TUI Components
4
+
5
+ Extensions and custom tools can render custom TUI components for interactive user interfaces. This page covers the component system and available building blocks.
6
+
7
+ **Source:** [`@openadapter/koda-tui`](https://github.com/earendil-works/pi-mono/tree/main/packages/tui)
8
+
9
+ ## Component Interface
10
+
11
+ All components implement:
12
+
13
+ ```typescript
14
+ interface Component {
15
+ render(width: number): string[];
16
+ handleInput?(data: string): void;
17
+ wantsKeyRelease?: boolean;
18
+ invalidate(): void;
19
+ }
20
+ ```
21
+
22
+ | Method | Description |
23
+ |--------|-------------|
24
+ | `render(width)` | Return array of strings (one per line). Each line **must not exceed `width`**. |
25
+ | `handleInput?(data)` | Receive keyboard input when component has focus. |
26
+ | `wantsKeyRelease?` | If true, component receives key release events (Kitty protocol). Default: false. |
27
+ | `invalidate()` | Clear cached render state. Called on theme changes. |
28
+
29
+ The TUI appends a full SGR reset and OSC 8 reset at the end of each rendered line. Styles do not carry across lines. If you emit multi-line text with styling, reapply styles per line or use `wrapTextWithAnsi()` so styles are preserved for each wrapped line.
30
+
31
+ ## Focusable Interface (IME Support)
32
+
33
+ Components that display a text cursor and need IME (Input Method Editor) support should implement the `Focusable` interface:
34
+
35
+ ```typescript
36
+ import { CURSOR_MARKER, type Component, type Focusable } from "@openadapter/koda-tui";
37
+
38
+ class MyInput implements Component, Focusable {
39
+ focused: boolean = false; // Set by TUI when focus changes
40
+
41
+ render(width: number): string[] {
42
+ const marker = this.focused ? CURSOR_MARKER : "";
43
+ // Emit marker right before the fake cursor
44
+ return [`> ${beforeCursor}${marker}\x1b[7m${atCursor}\x1b[27m${afterCursor}`];
45
+ }
46
+ }
47
+ ```
48
+
49
+ When a `Focusable` component has focus, TUI:
50
+ 1. Sets `focused = true` on the component
51
+ 2. Scans rendered output for `CURSOR_MARKER` (a zero-width APC escape sequence)
52
+ 3. Positions the hardware terminal cursor at that location
53
+ 4. Shows the hardware cursor only when `showHardwareCursor` is enabled
54
+
55
+ The cursor remains hidden by default. This keeps the fake cursor rendering, while still positioning the hardware cursor for terminals that track IME candidate windows with hidden cursors. Some terminals require a visible hardware cursor for IME positioning; enable it with `showHardwareCursor`, `setShowHardwareCursor(true)`, or `PI_HARDWARE_CURSOR=1`. The `Editor` and `Input` built-in components already implement this interface.
56
+
57
+ ### Container Components with Embedded Inputs
58
+
59
+ When a container component (dialog, selector, etc.) contains an `Input` or `Editor` child, the container must implement `Focusable` and propagate the focus state to the child. Otherwise, the hardware cursor won't be positioned correctly for IME input.
60
+
61
+ ```typescript
62
+ import { Container, type Focusable, Input } from "@openadapter/koda-tui";
63
+
64
+ class SearchDialog extends Container implements Focusable {
65
+ private searchInput: Input;
66
+
67
+ // Focusable implementation - propagate to child input for IME cursor positioning
68
+ private _focused = false;
69
+ get focused(): boolean {
70
+ return this._focused;
71
+ }
72
+ set focused(value: boolean) {
73
+ this._focused = value;
74
+ this.searchInput.focused = value;
75
+ }
76
+
77
+ constructor() {
78
+ super();
79
+ this.searchInput = new Input();
80
+ this.addChild(this.searchInput);
81
+ }
82
+ }
83
+ ```
84
+
85
+ Without this propagation, typing with an IME (Chinese, Japanese, Korean, etc.) will show the candidate window in the wrong position on screen.
86
+
87
+ ## Using Components
88
+
89
+ **In extensions** via `ctx.ui.custom()`:
90
+
91
+ ```typescript
92
+ pi.on("session_start", async (_event, ctx) => {
93
+ const handle = ctx.ui.custom(myComponent);
94
+ // handle.requestRender() - trigger re-render
95
+ // handle.close() - restore normal UI
96
+ });
97
+ ```
98
+
99
+ **In custom tools** via `pi.ui.custom()`:
100
+
101
+ ```typescript
102
+ async execute(toolCallId, params, onUpdate, ctx, signal) {
103
+ const handle = pi.ui.custom(myComponent);
104
+ // ...
105
+ handle.close();
106
+ }
107
+ ```
108
+
109
+ ## Overlays
110
+
111
+ Overlays render components on top of existing content without clearing the screen. Pass `{ overlay: true }` to `ctx.ui.custom()`:
112
+
113
+ ```typescript
114
+ const result = await ctx.ui.custom<string | null>(
115
+ (tui, theme, keybindings, done) => new MyDialog({ onClose: done }),
116
+ { overlay: true }
117
+ );
118
+ ```
119
+
120
+ For positioning and sizing, use `overlayOptions`:
121
+
122
+ ```typescript
123
+ const result = await ctx.ui.custom<string | null>(
124
+ (tui, theme, keybindings, done) => new SidePanel({ onClose: done }),
125
+ {
126
+ overlay: true,
127
+ overlayOptions: {
128
+ // Size: number or percentage string
129
+ width: "50%", // 50% of terminal width
130
+ minWidth: 40, // minimum 40 columns
131
+ maxHeight: "80%", // max 80% of terminal height
132
+
133
+ // Position: anchor-based (default: "center")
134
+ anchor: "right-center", // 9 positions: center, top-left, top-center, etc.
135
+ offsetX: -2, // offset from anchor
136
+ offsetY: 0,
137
+
138
+ // Or percentage/absolute positioning
139
+ row: "25%", // 25% from top
140
+ col: 10, // column 10
141
+
142
+ // Margins
143
+ margin: 2, // all sides, or { top, right, bottom, left }
144
+
145
+ // Responsive: hide on narrow terminals
146
+ visible: (termWidth, termHeight) => termWidth >= 80,
147
+ },
148
+ // Get handle for programmatic focus and visibility control
149
+ onHandle: (handle) => {
150
+ // handle.focus() - focus this overlay and bring it to the visual front
151
+ // handle.unfocus() - release input to normal fallback
152
+ // handle.unfocus({ target }) - release input to a specific component or null
153
+ // handle.setHidden(true/false) - toggle visibility
154
+ // handle.hide() - permanently remove
155
+ },
156
+ }
157
+ );
158
+ ```
159
+
160
+ ### Overlay Focus
161
+
162
+ A focused visible overlay keeps input ownership across temporary non-overlay UI. If an overlay opens another `ctx.ui.custom()` component without `{ overlay: true }`, that replacement UI receives input while it is active; when it closes, the focused overlay can reclaim input.
163
+
164
+ Use `handle.unfocus()` when a visible overlay should stop owning input and let TUI fall back to another visible capturing overlay or the previous focus target. Use `handle.unfocus({ target })` when a specific component should receive input while the overlay stays visible. Passing `{ target: null }` intentionally leaves no focused component until focus is set again.
165
+
166
+ ### Overlay Lifecycle
167
+
168
+ Overlay components are disposed when closed. Don't reuse references - create fresh instances:
169
+
170
+ ```typescript
171
+ // Wrong - stale reference
172
+ let menu: MenuComponent;
173
+ await ctx.ui.custom((_, __, ___, done) => {
174
+ menu = new MenuComponent(done);
175
+ return menu;
176
+ }, { overlay: true });
177
+ setActiveComponent(menu); // Disposed
178
+
179
+ // Correct - re-call to re-show
180
+ const showMenu = () => ctx.ui.custom((_, __, ___, done) =>
181
+ new MenuComponent(done), { overlay: true });
182
+
183
+ await showMenu(); // First show
184
+ await showMenu(); // "Back" = just call again
185
+ ```
186
+
187
+ See [overlay-qa-tests.ts](../examples/extensions/overlay-qa-tests.ts) for comprehensive examples covering anchors, margins, stacking, responsive visibility, and animation.
188
+
189
+ ## Built-in Components
190
+
191
+ Import from `@openadapter/koda-tui`:
192
+
193
+ ```typescript
194
+ import { Text, Box, Container, Spacer, Markdown } from "@openadapter/koda-tui";
195
+ ```
196
+
197
+ ### Text
198
+
199
+ Multi-line text with word wrapping.
200
+
201
+ ```typescript
202
+ const text = new Text(
203
+ "Hello World", // content
204
+ 1, // paddingX (default: 1)
205
+ 1, // paddingY (default: 1)
206
+ (s) => bgGray(s) // optional background function
207
+ );
208
+ text.setText("Updated");
209
+ ```
210
+
211
+ ### Box
212
+
213
+ Container with padding and background color.
214
+
215
+ ```typescript
216
+ const box = new Box(
217
+ 1, // paddingX
218
+ 1, // paddingY
219
+ (s) => bgGray(s) // background function
220
+ );
221
+ box.addChild(new Text("Content", 0, 0));
222
+ box.setBgFn((s) => bgBlue(s));
223
+ ```
224
+
225
+ ### Container
226
+
227
+ Groups child components vertically.
228
+
229
+ ```typescript
230
+ const container = new Container();
231
+ container.addChild(component1);
232
+ container.addChild(component2);
233
+ container.removeChild(component1);
234
+ ```
235
+
236
+ ### Spacer
237
+
238
+ Empty vertical space.
239
+
240
+ ```typescript
241
+ const spacer = new Spacer(2); // 2 empty lines
242
+ ```
243
+
244
+ ### Markdown
245
+
246
+ Renders markdown with syntax highlighting.
247
+
248
+ ```typescript
249
+ const md = new Markdown(
250
+ "# Title\n\nSome **bold** text",
251
+ 1, // paddingX
252
+ 1, // paddingY
253
+ theme // MarkdownTheme (see below)
254
+ );
255
+ md.setText("Updated markdown");
256
+ ```
257
+
258
+ ### Image
259
+
260
+ Renders images in supported terminals (Kitty, iTerm2, Ghostty, WezTerm).
261
+
262
+ ```typescript
263
+ const image = new Image(
264
+ base64Data, // base64-encoded image
265
+ "image/png", // MIME type
266
+ theme, // ImageTheme
267
+ { maxWidthCells: 80, maxHeightCells: 24 }
268
+ );
269
+ ```
270
+
271
+ ## Keyboard Input
272
+
273
+ Use `matchesKey()` for key detection:
274
+
275
+ ```typescript
276
+ import { matchesKey, Key } from "@openadapter/koda-tui";
277
+
278
+ handleInput(data: string) {
279
+ if (matchesKey(data, Key.up)) {
280
+ this.selectedIndex--;
281
+ } else if (matchesKey(data, Key.enter)) {
282
+ this.onSelect?.(this.selectedIndex);
283
+ } else if (matchesKey(data, Key.escape)) {
284
+ this.onCancel?.();
285
+ } else if (matchesKey(data, Key.ctrl("c"))) {
286
+ // Ctrl+C
287
+ }
288
+ }
289
+ ```
290
+
291
+ **Key identifiers** (use `Key.*` for autocomplete, or string literals):
292
+ - Basic keys: `Key.enter`, `Key.escape`, `Key.tab`, `Key.space`, `Key.backspace`, `Key.delete`, `Key.home`, `Key.end`
293
+ - Arrow keys: `Key.up`, `Key.down`, `Key.left`, `Key.right`
294
+ - With modifiers: `Key.ctrl("c")`, `Key.shift("tab")`, `Key.alt("left")`, `Key.ctrlShift("p")`
295
+ - String format also works: `"enter"`, `"ctrl+c"`, `"shift+tab"`, `"ctrl+shift+p"`
296
+
297
+ ## Line Width
298
+
299
+ **Critical:** Each line from `render()` must not exceed the `width` parameter.
300
+
301
+ ```typescript
302
+ import { visibleWidth, truncateToWidth } from "@openadapter/koda-tui";
303
+
304
+ render(width: number): string[] {
305
+ // Truncate long lines
306
+ return [truncateToWidth(this.text, width)];
307
+ }
308
+ ```
309
+
310
+ Utilities:
311
+ - `visibleWidth(str)` - Get display width (ignores ANSI codes)
312
+ - `truncateToWidth(str, width, ellipsis?)` - Truncate with optional ellipsis
313
+ - `wrapTextWithAnsi(str, width)` - Word wrap preserving ANSI codes
314
+
315
+ ## Creating Custom Components
316
+
317
+ Example: Interactive selector
318
+
319
+ ```typescript
320
+ import {
321
+ matchesKey, Key,
322
+ truncateToWidth, visibleWidth
323
+ } from "@openadapter/koda-tui";
324
+
325
+ class MySelector {
326
+ private items: string[];
327
+ private selected = 0;
328
+ private cachedWidth?: number;
329
+ private cachedLines?: string[];
330
+
331
+ public onSelect?: (item: string) => void;
332
+ public onCancel?: () => void;
333
+
334
+ constructor(items: string[]) {
335
+ this.items = items;
336
+ }
337
+
338
+ handleInput(data: string): void {
339
+ if (matchesKey(data, Key.up) && this.selected > 0) {
340
+ this.selected--;
341
+ this.invalidate();
342
+ } else if (matchesKey(data, Key.down) && this.selected < this.items.length - 1) {
343
+ this.selected++;
344
+ this.invalidate();
345
+ } else if (matchesKey(data, Key.enter)) {
346
+ this.onSelect?.(this.items[this.selected]);
347
+ } else if (matchesKey(data, Key.escape)) {
348
+ this.onCancel?.();
349
+ }
350
+ }
351
+
352
+ render(width: number): string[] {
353
+ if (this.cachedLines && this.cachedWidth === width) {
354
+ return this.cachedLines;
355
+ }
356
+
357
+ this.cachedLines = this.items.map((item, i) => {
358
+ const prefix = i === this.selected ? "> " : " ";
359
+ return truncateToWidth(prefix + item, width);
360
+ });
361
+ this.cachedWidth = width;
362
+ return this.cachedLines;
363
+ }
364
+
365
+ invalidate(): void {
366
+ this.cachedWidth = undefined;
367
+ this.cachedLines = undefined;
368
+ }
369
+ }
370
+ ```
371
+
372
+ Usage in an extension:
373
+
374
+ ```typescript
375
+ pi.registerCommand("pick", {
376
+ description: "Pick an item",
377
+ handler: async (args, ctx) => {
378
+ const items = ["Option A", "Option B", "Option C"];
379
+ const selector = new MySelector(items);
380
+
381
+ let handle: { close: () => void; requestRender: () => void };
382
+
383
+ await new Promise<void>((resolve) => {
384
+ selector.onSelect = (item) => {
385
+ ctx.ui.notify(`Selected: ${item}`, "info");
386
+ handle.close();
387
+ resolve();
388
+ };
389
+ selector.onCancel = () => {
390
+ handle.close();
391
+ resolve();
392
+ };
393
+ handle = ctx.ui.custom(selector);
394
+ });
395
+ }
396
+ });
397
+ ```
398
+
399
+ ## Theming
400
+
401
+ Components accept theme objects for styling.
402
+
403
+ **In `renderCall`/`renderResult`**, use the `theme` parameter:
404
+
405
+ ```typescript
406
+ renderResult(result, options, theme, context) {
407
+ // Use theme.fg() for foreground colors
408
+ return new Text(theme.fg("success", "Done!"), 0, 0);
409
+
410
+ // Use theme.bg() for background colors
411
+ const styled = theme.bg("toolPendingBg", theme.fg("accent", "text"));
412
+ }
413
+ ```
414
+
415
+ **Foreground colors** (`theme.fg(color, text)`):
416
+
417
+ | Category | Colors |
418
+ |----------|--------|
419
+ | General | `text`, `accent`, `muted`, `dim` |
420
+ | Status | `success`, `error`, `warning` |
421
+ | Borders | `border`, `borderAccent`, `borderMuted` |
422
+ | Messages | `userMessageText`, `customMessageText`, `customMessageLabel` |
423
+ | Tools | `toolTitle`, `toolOutput` |
424
+ | Diffs | `toolDiffAdded`, `toolDiffRemoved`, `toolDiffContext` |
425
+ | Markdown | `mdHeading`, `mdLink`, `mdLinkUrl`, `mdCode`, `mdCodeBlock`, `mdCodeBlockBorder`, `mdQuote`, `mdQuoteBorder`, `mdHr`, `mdListBullet` |
426
+ | Syntax | `syntaxComment`, `syntaxKeyword`, `syntaxFunction`, `syntaxVariable`, `syntaxString`, `syntaxNumber`, `syntaxType`, `syntaxOperator`, `syntaxPunctuation` |
427
+ | Thinking | `thinkingOff`, `thinkingMinimal`, `thinkingLow`, `thinkingMedium`, `thinkingHigh`, `thinkingXhigh` |
428
+ | Modes | `bashMode` |
429
+
430
+ **Background colors** (`theme.bg(color, text)`):
431
+
432
+ `selectedBg`, `userMessageBg`, `customMessageBg`, `toolPendingBg`, `toolSuccessBg`, `toolErrorBg`
433
+
434
+ **For Markdown**, use `getMarkdownTheme()`:
435
+
436
+ ```typescript
437
+ import { getMarkdownTheme } from "@openadapter/koda";
438
+ import { Markdown } from "@openadapter/koda-tui";
439
+
440
+ renderResult(result, options, theme, context) {
441
+ const mdTheme = getMarkdownTheme();
442
+ return new Markdown(result.details.markdown, 0, 0, mdTheme);
443
+ }
444
+ ```
445
+
446
+ **For custom components**, define your own theme interface:
447
+
448
+ ```typescript
449
+ interface MyTheme {
450
+ selected: (s: string) => string;
451
+ normal: (s: string) => string;
452
+ }
453
+ ```
454
+
455
+ ## Debug logging
456
+
457
+ Set `PI_TUI_WRITE_LOG` to capture the raw ANSI stream written to stdout.
458
+
459
+ ```bash
460
+ PI_TUI_WRITE_LOG=/tmp/tui-ansi.log npx tsx packages/tui/test/chat-simple.ts
461
+ ```
462
+
463
+ ## Performance
464
+
465
+ Cache rendered output when possible:
466
+
467
+ ```typescript
468
+ class CachedComponent {
469
+ private cachedWidth?: number;
470
+ private cachedLines?: string[];
471
+
472
+ render(width: number): string[] {
473
+ if (this.cachedLines && this.cachedWidth === width) {
474
+ return this.cachedLines;
475
+ }
476
+ // ... compute lines ...
477
+ this.cachedWidth = width;
478
+ this.cachedLines = lines;
479
+ return lines;
480
+ }
481
+
482
+ invalidate(): void {
483
+ this.cachedWidth = undefined;
484
+ this.cachedLines = undefined;
485
+ }
486
+ }
487
+ ```
488
+
489
+ Call `invalidate()` when state changes, then `handle.requestRender()` to trigger re-render.
490
+
491
+ ## Invalidation and Theme Changes
492
+
493
+ When the theme changes, the TUI calls `invalidate()` on all components to clear their caches. Components must properly implement `invalidate()` to ensure theme changes take effect.
494
+
495
+ ### The Problem
496
+
497
+ If a component pre-bakes theme colors into strings (via `theme.fg()`, `theme.bg()`, etc.) and caches them, the cached strings contain ANSI escape codes from the old theme. Simply clearing the render cache isn't enough if the component stores the themed content separately.
498
+
499
+ **Wrong approach** (theme colors won't update):
500
+
501
+ ```typescript
502
+ class BadComponent extends Container {
503
+ private content: Text;
504
+
505
+ constructor(message: string, theme: Theme) {
506
+ super();
507
+ // Pre-baked theme colors stored in Text component
508
+ this.content = new Text(theme.fg("accent", message), 1, 0);
509
+ this.addChild(this.content);
510
+ }
511
+ // No invalidate override - parent's invalidate only clears
512
+ // child render caches, not the pre-baked content
513
+ }
514
+ ```
515
+
516
+ ### The Solution
517
+
518
+ Components that build content with theme colors must rebuild that content when `invalidate()` is called:
519
+
520
+ ```typescript
521
+ class GoodComponent extends Container {
522
+ private message: string;
523
+ private content: Text;
524
+
525
+ constructor(message: string) {
526
+ super();
527
+ this.message = message;
528
+ this.content = new Text("", 1, 0);
529
+ this.addChild(this.content);
530
+ this.updateDisplay();
531
+ }
532
+
533
+ private updateDisplay(): void {
534
+ // Rebuild content with current theme
535
+ this.content.setText(theme.fg("accent", this.message));
536
+ }
537
+
538
+ override invalidate(): void {
539
+ super.invalidate(); // Clear child caches
540
+ this.updateDisplay(); // Rebuild with new theme
541
+ }
542
+ }
543
+ ```
544
+
545
+ ### Pattern: Rebuild on Invalidate
546
+
547
+ For components with complex content:
548
+
549
+ ```typescript
550
+ class ComplexComponent extends Container {
551
+ private data: SomeData;
552
+
553
+ constructor(data: SomeData) {
554
+ super();
555
+ this.data = data;
556
+ this.rebuild();
557
+ }
558
+
559
+ private rebuild(): void {
560
+ this.clear(); // Remove all children
561
+
562
+ // Build UI with current theme
563
+ this.addChild(new Text(theme.fg("accent", theme.bold("Title")), 1, 0));
564
+ this.addChild(new Spacer(1));
565
+
566
+ for (const item of this.data.items) {
567
+ const color = item.active ? "success" : "muted";
568
+ this.addChild(new Text(theme.fg(color, item.label), 1, 0));
569
+ }
570
+ }
571
+
572
+ override invalidate(): void {
573
+ super.invalidate();
574
+ this.rebuild();
575
+ }
576
+ }
577
+ ```
578
+
579
+ ### When This Matters
580
+
581
+ This pattern is needed when:
582
+
583
+ 1. **Pre-baking theme colors** - Using `theme.fg()` or `theme.bg()` to create styled strings stored in child components
584
+ 2. **Syntax highlighting** - Using `highlightCode()` which applies theme-based syntax colors
585
+ 3. **Complex layouts** - Building child component trees that embed theme colors
586
+
587
+ This pattern is NOT needed when:
588
+
589
+ 1. **Using theme callbacks** - Passing functions like `(text) => theme.fg("accent", text)` that are called during render
590
+ 2. **Simple containers** - Just grouping other components without adding themed content
591
+ 3. **Stateless render** - Computing themed output fresh in every `render()` call (no caching)
592
+
593
+ ## Common Patterns
594
+
595
+ These patterns cover the most common UI needs in extensions. **Copy these patterns instead of building from scratch.**
596
+
597
+ ### Pattern 1: Selection Dialog (SelectList)
598
+
599
+ For letting users pick from a list of options. Use `SelectList` from `@openadapter/koda-tui` with `DynamicBorder` for framing.
600
+
601
+ ```typescript
602
+ import type { ExtensionAPI } from "@openadapter/koda";
603
+ import { DynamicBorder } from "@openadapter/koda";
604
+ import { Container, type SelectItem, SelectList, Text } from "@openadapter/koda-tui";
605
+
606
+ pi.registerCommand("pick", {
607
+ handler: async (_args, ctx) => {
608
+ const items: SelectItem[] = [
609
+ { value: "opt1", label: "Option 1", description: "First option" },
610
+ { value: "opt2", label: "Option 2", description: "Second option" },
611
+ { value: "opt3", label: "Option 3" }, // description is optional
612
+ ];
613
+
614
+ const result = await ctx.ui.custom<string | null>((tui, theme, _kb, done) => {
615
+ const container = new Container();
616
+
617
+ // Top border
618
+ container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s)));
619
+
620
+ // Title
621
+ container.addChild(new Text(theme.fg("accent", theme.bold("Pick an Option")), 1, 0));
622
+
623
+ // SelectList with theme
624
+ const selectList = new SelectList(items, Math.min(items.length, 10), {
625
+ selectedPrefix: (t) => theme.fg("accent", t),
626
+ selectedText: (t) => theme.fg("accent", t),
627
+ description: (t) => theme.fg("muted", t),
628
+ scrollInfo: (t) => theme.fg("dim", t),
629
+ noMatch: (t) => theme.fg("warning", t),
630
+ });
631
+ selectList.onSelect = (item) => done(item.value);
632
+ selectList.onCancel = () => done(null);
633
+ container.addChild(selectList);
634
+
635
+ // Help text
636
+ container.addChild(new Text(theme.fg("dim", "↑↓ navigate • enter select • esc cancel"), 1, 0));
637
+
638
+ // Bottom border
639
+ container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s)));
640
+
641
+ return {
642
+ render: (w) => container.render(w),
643
+ invalidate: () => container.invalidate(),
644
+ handleInput: (data) => { selectList.handleInput(data); tui.requestRender(); },
645
+ };
646
+ });
647
+
648
+ if (result) {
649
+ ctx.ui.notify(`Selected: ${result}`, "info");
650
+ }
651
+ },
652
+ });
653
+ ```
654
+
655
+ **Examples:** [preset.ts](../examples/extensions/preset.ts), [tools.ts](../examples/extensions/tools.ts)
656
+
657
+ ### Pattern 2: Async Operation with Cancel (BorderedLoader)
658
+
659
+ For operations that take time and should be cancellable. `BorderedLoader` shows a spinner and handles escape to cancel.
660
+
661
+ ```typescript
662
+ import { BorderedLoader } from "@openadapter/koda";
663
+
664
+ pi.registerCommand("fetch", {
665
+ handler: async (_args, ctx) => {
666
+ const result = await ctx.ui.custom<string | null>((tui, theme, _kb, done) => {
667
+ const loader = new BorderedLoader(tui, theme, "Fetching data...");
668
+ loader.onAbort = () => done(null);
669
+
670
+ // Do async work
671
+ fetchData(loader.signal)
672
+ .then((data) => done(data))
673
+ .catch(() => done(null));
674
+
675
+ return loader;
676
+ });
677
+
678
+ if (result === null) {
679
+ ctx.ui.notify("Cancelled", "info");
680
+ } else {
681
+ ctx.ui.setEditorText(result);
682
+ }
683
+ },
684
+ });
685
+ ```
686
+
687
+ **Examples:** [qna.ts](../examples/extensions/qna.ts), [handoff.ts](../examples/extensions/handoff.ts)
688
+
689
+ ### Pattern 3: Settings/Toggles (SettingsList)
690
+
691
+ For toggling multiple settings. Use `SettingsList` from `@openadapter/koda-tui` with `getSettingsListTheme()`.
692
+
693
+ ```typescript
694
+ import { getSettingsListTheme } from "@openadapter/koda";
695
+ import { Container, type SettingItem, SettingsList, Text } from "@openadapter/koda-tui";
696
+
697
+ pi.registerCommand("settings", {
698
+ handler: async (_args, ctx) => {
699
+ const items: SettingItem[] = [
700
+ { id: "verbose", label: "Verbose mode", currentValue: "off", values: ["on", "off"] },
701
+ { id: "color", label: "Color output", currentValue: "on", values: ["on", "off"] },
702
+ ];
703
+
704
+ await ctx.ui.custom((_tui, theme, _kb, done) => {
705
+ const container = new Container();
706
+ container.addChild(new Text(theme.fg("accent", theme.bold("Settings")), 1, 1));
707
+
708
+ const settingsList = new SettingsList(
709
+ items,
710
+ Math.min(items.length + 2, 15),
711
+ getSettingsListTheme(),
712
+ (id, newValue) => {
713
+ // Handle value change
714
+ ctx.ui.notify(`${id} = ${newValue}`, "info");
715
+ },
716
+ () => done(undefined), // On close
717
+ { enableSearch: true }, // Optional: enable fuzzy search by label
718
+ );
719
+ container.addChild(settingsList);
720
+
721
+ return {
722
+ render: (w) => container.render(w),
723
+ invalidate: () => container.invalidate(),
724
+ handleInput: (data) => settingsList.handleInput?.(data),
725
+ };
726
+ });
727
+ },
728
+ });
729
+ ```
730
+
731
+ **Examples:** [tools.ts](../examples/extensions/tools.ts)
732
+
733
+ ### Pattern 4: Persistent Status Indicator
734
+
735
+ Show status in the footer that persists across renders. Good for mode indicators.
736
+
737
+ ```typescript
738
+ // Set status (shown in footer)
739
+ ctx.ui.setStatus("my-ext", ctx.ui.theme.fg("accent", "● active"));
740
+
741
+ // Clear status
742
+ ctx.ui.setStatus("my-ext", undefined);
743
+ ```
744
+
745
+ **Examples:** [status-line.ts](../examples/extensions/status-line.ts), [plan-mode.ts](../examples/extensions/plan-mode.ts), [preset.ts](../examples/extensions/preset.ts)
746
+
747
+ ### Pattern 4b: Working Indicator Customization
748
+
749
+ Customize the inline working indicator shown while pi is streaming a response.
750
+
751
+ ```typescript
752
+ // Static indicator
753
+ ctx.ui.setWorkingIndicator({ frames: [ctx.ui.theme.fg("accent", "●")] });
754
+
755
+ // Custom animated indicator
756
+ ctx.ui.setWorkingIndicator({
757
+ frames: [
758
+ ctx.ui.theme.fg("dim", "·"),
759
+ ctx.ui.theme.fg("muted", "•"),
760
+ ctx.ui.theme.fg("accent", "●"),
761
+ ctx.ui.theme.fg("muted", "•"),
762
+ ],
763
+ intervalMs: 120,
764
+ });
765
+
766
+ // Hide the indicator entirely
767
+ ctx.ui.setWorkingIndicator({ frames: [] });
768
+
769
+ // Restore pi's default spinner
770
+ ctx.ui.setWorkingIndicator();
771
+ ```
772
+
773
+ This only affects the normal streaming working indicator. Compaction and retry loaders keep their built-in styling. Custom frames are rendered verbatim, so extensions must add their own colors when needed.
774
+
775
+ **Examples:** [working-indicator.ts](../examples/extensions/working-indicator.ts)
776
+
777
+ ### Pattern 5: Widgets Above/Below Editor
778
+
779
+ Show persistent content above or below the input editor. Good for todo lists, progress.
780
+
781
+ ```typescript
782
+ // Simple string array (above editor by default)
783
+ ctx.ui.setWidget("my-widget", ["Line 1", "Line 2"]);
784
+
785
+ // Render below the editor
786
+ ctx.ui.setWidget("my-widget", ["Line 1", "Line 2"], { placement: "belowEditor" });
787
+
788
+ // Or with theme
789
+ ctx.ui.setWidget("my-widget", (_tui, theme) => {
790
+ const lines = items.map((item, i) =>
791
+ item.done
792
+ ? theme.fg("success", "✓ ") + theme.fg("muted", item.text)
793
+ : theme.fg("dim", "○ ") + item.text
794
+ );
795
+ return {
796
+ render: () => lines,
797
+ invalidate: () => {},
798
+ };
799
+ });
800
+
801
+ // Clear
802
+ ctx.ui.setWidget("my-widget", undefined);
803
+ ```
804
+
805
+ **Examples:** [plan-mode.ts](../examples/extensions/plan-mode.ts)
806
+
807
+ ### Pattern 6: Custom Footer
808
+
809
+ Replace the footer. `footerData` exposes data not otherwise accessible to extensions.
810
+
811
+ ```typescript
812
+ ctx.ui.setFooter((tui, theme, footerData) => ({
813
+ invalidate() {},
814
+ render(width: number): string[] {
815
+ // footerData.getGitBranch(): string | null
816
+ // footerData.getExtensionStatuses(): ReadonlyMap<string, string>
817
+ return [`${ctx.model?.id} (${footerData.getGitBranch() || "no git"})`];
818
+ },
819
+ dispose: footerData.onBranchChange(() => tui.requestRender()), // reactive
820
+ }));
821
+
822
+ ctx.ui.setFooter(undefined); // restore default
823
+ ```
824
+
825
+ Token stats available via `ctx.sessionManager.getBranch()` and `ctx.model`.
826
+
827
+ **Examples:** [custom-footer.ts](../examples/extensions/custom-footer.ts)
828
+
829
+ ### Pattern 7: Custom Editor (vim mode, etc.)
830
+
831
+ Replace the main input editor with a custom implementation. Useful for modal editing (vim), different keybindings (emacs), or specialized input handling.
832
+
833
+ ```typescript
834
+ import { CustomEditor, type ExtensionAPI } from "@openadapter/koda";
835
+ import { matchesKey, truncateToWidth } from "@openadapter/koda-tui";
836
+
837
+ type Mode = "normal" | "insert";
838
+
839
+ class VimEditor extends CustomEditor {
840
+ private mode: Mode = "insert";
841
+
842
+ handleInput(data: string): void {
843
+ // Escape: switch to normal mode, or pass through for app handling
844
+ if (matchesKey(data, "escape")) {
845
+ if (this.mode === "insert") {
846
+ this.mode = "normal";
847
+ return;
848
+ }
849
+ // In normal mode, escape aborts agent (handled by CustomEditor)
850
+ super.handleInput(data);
851
+ return;
852
+ }
853
+
854
+ // Insert mode: pass everything to CustomEditor
855
+ if (this.mode === "insert") {
856
+ super.handleInput(data);
857
+ return;
858
+ }
859
+
860
+ // Normal mode: vim-style navigation
861
+ switch (data) {
862
+ case "i": this.mode = "insert"; return;
863
+ case "h": super.handleInput("\x1b[D"); return; // Left
864
+ case "j": super.handleInput("\x1b[B"); return; // Down
865
+ case "k": super.handleInput("\x1b[A"); return; // Up
866
+ case "l": super.handleInput("\x1b[C"); return; // Right
867
+ }
868
+ // Pass unhandled keys to super (ctrl+c, etc.), but filter printable chars
869
+ if (data.length === 1 && data.charCodeAt(0) >= 32) return;
870
+ super.handleInput(data);
871
+ }
872
+
873
+ render(width: number): string[] {
874
+ const lines = super.render(width);
875
+ // Add mode indicator to bottom border (use truncateToWidth for ANSI-safe truncation)
876
+ if (lines.length > 0) {
877
+ const label = this.mode === "normal" ? " NORMAL " : " INSERT ";
878
+ const lastLine = lines[lines.length - 1]!;
879
+ // Pass "" as ellipsis to avoid adding "..." when truncating
880
+ lines[lines.length - 1] = truncateToWidth(lastLine, width - label.length, "") + label;
881
+ }
882
+ return lines;
883
+ }
884
+ }
885
+
886
+ export default function (pi: ExtensionAPI) {
887
+ pi.on("session_start", (_event, ctx) => {
888
+ // Factory receives theme and keybindings from the app
889
+ ctx.ui.setEditorComponent((tui, theme, keybindings) =>
890
+ new VimEditor(theme, keybindings)
891
+ );
892
+ });
893
+ }
894
+ ```
895
+
896
+ **Key points:**
897
+
898
+ - **Extend `CustomEditor`** (not base `Editor`) to get app keybindings (escape to abort, ctrl+d to exit, model switching, etc.)
899
+ - **Call `super.handleInput(data)`** for keys you don't handle
900
+ - **Factory pattern**: `setEditorComponent` receives a factory function that gets `tui`, `theme`, and `keybindings`
901
+ - **Pass `undefined`** to restore the default editor: `ctx.ui.setEditorComponent(undefined)`
902
+
903
+ **Examples:** [modal-editor.ts](../examples/extensions/modal-editor.ts)
904
+
905
+ ## Key Rules
906
+
907
+ 1. **Always use theme from callback** - Don't import theme directly. Use `theme` from the `ctx.ui.custom((tui, theme, keybindings, done) => ...)` callback.
908
+
909
+ 2. **Always type DynamicBorder color param** - Write `(s: string) => theme.fg("accent", s)`, not `(s) => theme.fg("accent", s)`.
910
+
911
+ 3. **Call tui.requestRender() after state changes** - In `handleInput`, call `tui.requestRender()` after updating state.
912
+
913
+ 4. **Return the three-method object** - Custom components need `{ render, invalidate, handleInput }`.
914
+
915
+ 5. **Use existing components** - `SelectList`, `SettingsList`, `BorderedLoader` cover 90% of cases. Don't rebuild them.
916
+
917
+ ## Examples
918
+
919
+ - **Selection UI**: [examples/extensions/preset.ts](../examples/extensions/preset.ts) - SelectList with DynamicBorder framing
920
+ - **Async with cancel**: [examples/extensions/qna.ts](../examples/extensions/qna.ts) - BorderedLoader for LLM calls
921
+ - **Settings toggles**: [examples/extensions/tools.ts](../examples/extensions/tools.ts) - SettingsList for tool enable/disable
922
+ - **Status indicators**: [examples/extensions/plan-mode.ts](../examples/extensions/plan-mode.ts) - setStatus and setWidget
923
+ - **Working indicator**: [examples/extensions/working-indicator.ts](../examples/extensions/working-indicator.ts) - setWorkingIndicator
924
+ - **Custom footer**: [examples/extensions/custom-footer.ts](../examples/extensions/custom-footer.ts) - setFooter with stats
925
+ - **Custom editor**: [examples/extensions/modal-editor.ts](../examples/extensions/modal-editor.ts) - Vim-like modal editing
926
+ - **Snake game**: [examples/extensions/snake.ts](../examples/extensions/snake.ts) - Full game with keyboard input, game loop
927
+ - **Custom tool rendering**: [examples/extensions/todo.ts](../examples/extensions/todo.ts) - renderCall and renderResult