@odysx/codemind 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (331) hide show
  1. package/README.md +293 -2
  2. package/dist/commands/agents.d.ts +2 -0
  3. package/dist/commands/agents.d.ts.map +1 -0
  4. package/dist/commands/agents.js +91 -0
  5. package/dist/commands/agents.js.map +1 -0
  6. package/dist/commands/auth.d.ts +2 -0
  7. package/dist/commands/auth.d.ts.map +1 -0
  8. package/dist/commands/auth.js +35 -0
  9. package/dist/commands/auth.js.map +1 -0
  10. package/dist/commands/chat.d.ts +2 -0
  11. package/dist/commands/chat.d.ts.map +1 -0
  12. package/dist/commands/chat.js +180 -0
  13. package/dist/commands/chat.js.map +1 -0
  14. package/dist/commands/keys.d.ts +2 -0
  15. package/dist/commands/keys.d.ts.map +1 -0
  16. package/dist/commands/keys.js +81 -0
  17. package/dist/commands/keys.js.map +1 -0
  18. package/dist/commands/local.d.ts +2 -0
  19. package/dist/commands/local.d.ts.map +1 -0
  20. package/dist/commands/local.js +325 -0
  21. package/dist/commands/local.js.map +1 -0
  22. package/dist/commands/mcp.d.ts +2 -0
  23. package/dist/commands/mcp.d.ts.map +1 -0
  24. package/dist/commands/mcp.js +73 -0
  25. package/dist/commands/mcp.js.map +1 -0
  26. package/dist/commands/prompts.d.ts +2 -0
  27. package/dist/commands/prompts.d.ts.map +1 -0
  28. package/dist/commands/prompts.js +59 -0
  29. package/dist/commands/prompts.js.map +1 -0
  30. package/dist/commands/skills.d.ts +2 -0
  31. package/dist/commands/skills.d.ts.map +1 -0
  32. package/dist/commands/skills.js +41 -0
  33. package/dist/commands/skills.js.map +1 -0
  34. package/dist/core/auth/index.d.ts +10 -0
  35. package/dist/core/auth/index.d.ts.map +1 -0
  36. package/dist/core/auth/index.js +82 -0
  37. package/dist/core/auth/index.js.map +1 -0
  38. package/dist/core/config/index.d.ts +47 -0
  39. package/dist/core/config/index.d.ts.map +1 -0
  40. package/dist/core/config/index.js +118 -0
  41. package/dist/core/config/index.js.map +1 -0
  42. package/dist/core/history.d.ts +14 -0
  43. package/dist/core/history.d.ts.map +1 -0
  44. package/dist/core/history.js +80 -0
  45. package/dist/core/history.js.map +1 -0
  46. package/dist/core/markdown-renderer.d.ts +47 -0
  47. package/dist/core/markdown-renderer.d.ts.map +1 -0
  48. package/dist/core/markdown-renderer.js +385 -0
  49. package/dist/core/markdown-renderer.js.map +1 -0
  50. package/dist/core/permissions/index.d.ts +31 -0
  51. package/dist/core/permissions/index.d.ts.map +1 -0
  52. package/dist/core/permissions/index.js +64 -0
  53. package/dist/core/permissions/index.js.map +1 -0
  54. package/dist/core/provider/external.d.ts +28 -0
  55. package/dist/core/provider/external.d.ts.map +1 -0
  56. package/dist/core/provider/external.js +248 -0
  57. package/dist/core/provider/external.js.map +1 -0
  58. package/dist/core/provider/index.d.ts +96 -0
  59. package/dist/core/provider/index.d.ts.map +1 -0
  60. package/dist/core/provider/index.js +423 -0
  61. package/dist/core/provider/index.js.map +1 -0
  62. package/dist/core/registry/index.d.ts +25 -0
  63. package/dist/core/registry/index.d.ts.map +1 -0
  64. package/dist/core/registry/index.js +216 -0
  65. package/dist/core/registry/index.js.map +1 -0
  66. package/dist/core/schema/types.d.ts +93 -0
  67. package/dist/core/schema/types.d.ts.map +1 -0
  68. package/dist/core/schema/types.js +12 -0
  69. package/dist/core/schema/types.js.map +1 -0
  70. package/dist/core/terminal.d.ts +40 -0
  71. package/dist/core/terminal.d.ts.map +1 -0
  72. package/dist/core/terminal.js +134 -0
  73. package/dist/core/terminal.js.map +1 -0
  74. package/dist/core/workspace/index.d.ts +35 -0
  75. package/dist/core/workspace/index.d.ts.map +1 -0
  76. package/dist/core/workspace/index.js +194 -0
  77. package/dist/core/workspace/index.js.map +1 -0
  78. package/dist/index.d.ts +10 -0
  79. package/dist/index.d.ts.map +1 -0
  80. package/dist/index.js +393 -0
  81. package/dist/index.js.map +1 -0
  82. package/dist/ui/app.d.ts +3 -0
  83. package/dist/ui/app.d.ts.map +1 -0
  84. package/dist/ui/app.js +554 -0
  85. package/dist/ui/app.js.map +1 -0
  86. package/dist/ui/commands/cases/catalog.d.ts +11 -0
  87. package/dist/ui/commands/cases/catalog.d.ts.map +1 -0
  88. package/dist/ui/commands/cases/catalog.js +62 -0
  89. package/dist/ui/commands/cases/catalog.js.map +1 -0
  90. package/dist/ui/commands/cases/misc.d.ts +35 -0
  91. package/dist/ui/commands/cases/misc.d.ts.map +1 -0
  92. package/dist/ui/commands/cases/misc.js +245 -0
  93. package/dist/ui/commands/cases/misc.js.map +1 -0
  94. package/dist/ui/commands/cases/tasks.d.ts +13 -0
  95. package/dist/ui/commands/cases/tasks.d.ts.map +1 -0
  96. package/dist/ui/commands/cases/tasks.js +91 -0
  97. package/dist/ui/commands/cases/tasks.js.map +1 -0
  98. package/dist/ui/commands/runCommand.d.ts +22 -0
  99. package/dist/ui/commands/runCommand.d.ts.map +1 -0
  100. package/dist/ui/commands/runCommand.js +136 -0
  101. package/dist/ui/commands/runCommand.js.map +1 -0
  102. package/dist/ui/components/AgentPicker.d.ts +11 -0
  103. package/dist/ui/components/AgentPicker.d.ts.map +1 -0
  104. package/dist/ui/components/AgentPicker.js +7 -0
  105. package/dist/ui/components/AgentPicker.js.map +1 -0
  106. package/dist/ui/components/AuthPicker.d.ts +14 -0
  107. package/dist/ui/components/AuthPicker.d.ts.map +1 -0
  108. package/dist/ui/components/AuthPicker.js +7 -0
  109. package/dist/ui/components/AuthPicker.js.map +1 -0
  110. package/dist/ui/components/Banner.d.ts +8 -0
  111. package/dist/ui/components/Banner.d.ts.map +1 -0
  112. package/dist/ui/components/Banner.js +24 -0
  113. package/dist/ui/components/Banner.js.map +1 -0
  114. package/dist/ui/components/CommandMenu.d.ts +17 -0
  115. package/dist/ui/components/CommandMenu.d.ts.map +1 -0
  116. package/dist/ui/components/CommandMenu.js +21 -0
  117. package/dist/ui/components/CommandMenu.js.map +1 -0
  118. package/dist/ui/components/ErrorBoundary.d.ts +24 -0
  119. package/dist/ui/components/ErrorBoundary.d.ts.map +1 -0
  120. package/dist/ui/components/ErrorBoundary.js +51 -0
  121. package/dist/ui/components/ErrorBoundary.js.map +1 -0
  122. package/dist/ui/components/InputBar.d.ts +28 -0
  123. package/dist/ui/components/InputBar.d.ts.map +1 -0
  124. package/dist/ui/components/InputBar.js +108 -0
  125. package/dist/ui/components/InputBar.js.map +1 -0
  126. package/dist/ui/components/KeysPanel.d.ts +12 -0
  127. package/dist/ui/components/KeysPanel.d.ts.map +1 -0
  128. package/dist/ui/components/KeysPanel.js +25 -0
  129. package/dist/ui/components/KeysPanel.js.map +1 -0
  130. package/dist/ui/components/MessageView.d.ts +29 -0
  131. package/dist/ui/components/MessageView.d.ts.map +1 -0
  132. package/dist/ui/components/MessageView.js +107 -0
  133. package/dist/ui/components/MessageView.js.map +1 -0
  134. package/dist/ui/components/PermissionPrompt.d.ts +10 -0
  135. package/dist/ui/components/PermissionPrompt.d.ts.map +1 -0
  136. package/dist/ui/components/PermissionPrompt.js +4 -0
  137. package/dist/ui/components/PermissionPrompt.js.map +1 -0
  138. package/dist/ui/components/Pickers.d.ts +43 -0
  139. package/dist/ui/components/Pickers.d.ts.map +1 -0
  140. package/dist/ui/components/Pickers.js +33 -0
  141. package/dist/ui/components/Pickers.js.map +1 -0
  142. package/dist/ui/components/SearchOverlay.d.ts +21 -0
  143. package/dist/ui/components/SearchOverlay.d.ts.map +1 -0
  144. package/dist/ui/components/SearchOverlay.js +14 -0
  145. package/dist/ui/components/SearchOverlay.js.map +1 -0
  146. package/dist/ui/components/SessionPicker.d.ts +12 -0
  147. package/dist/ui/components/SessionPicker.d.ts.map +1 -0
  148. package/dist/ui/components/SessionPicker.js +8 -0
  149. package/dist/ui/components/SessionPicker.js.map +1 -0
  150. package/dist/ui/components/WorkingIndicator.d.ts +10 -0
  151. package/dist/ui/components/WorkingIndicator.d.ts.map +1 -0
  152. package/dist/ui/components/WorkingIndicator.js +23 -0
  153. package/dist/ui/components/WorkingIndicator.js.map +1 -0
  154. package/dist/ui/controls/ApiKeyEntry.d.ts +8 -0
  155. package/dist/ui/controls/ApiKeyEntry.d.ts.map +1 -0
  156. package/dist/ui/controls/ApiKeyEntry.js +5 -0
  157. package/dist/ui/controls/ApiKeyEntry.js.map +1 -0
  158. package/dist/ui/controls/MultiSelect.d.ts +18 -0
  159. package/dist/ui/controls/MultiSelect.d.ts.map +1 -0
  160. package/dist/ui/controls/MultiSelect.js +9 -0
  161. package/dist/ui/controls/MultiSelect.js.map +1 -0
  162. package/dist/ui/hooks/useAgentPicker.d.ts +14 -0
  163. package/dist/ui/hooks/useAgentPicker.d.ts.map +1 -0
  164. package/dist/ui/hooks/useAgentPicker.js +53 -0
  165. package/dist/ui/hooks/useAgentPicker.js.map +1 -0
  166. package/dist/ui/hooks/useAuthFlow.d.ts +14 -0
  167. package/dist/ui/hooks/useAuthFlow.d.ts.map +1 -0
  168. package/dist/ui/hooks/useAuthFlow.js +31 -0
  169. package/dist/ui/hooks/useAuthFlow.js.map +1 -0
  170. package/dist/ui/hooks/useKeysFlow.d.ts +17 -0
  171. package/dist/ui/hooks/useKeysFlow.d.ts.map +1 -0
  172. package/dist/ui/hooks/useKeysFlow.js +130 -0
  173. package/dist/ui/hooks/useKeysFlow.js.map +1 -0
  174. package/dist/ui/hooks/useSessionPicker.d.ts +29 -0
  175. package/dist/ui/hooks/useSessionPicker.d.ts.map +1 -0
  176. package/dist/ui/hooks/useSessionPicker.js +58 -0
  177. package/dist/ui/hooks/useSessionPicker.js.map +1 -0
  178. package/dist/ui/hooks/useTerminalSize.d.ts +6 -0
  179. package/dist/ui/hooks/useTerminalSize.d.ts.map +1 -0
  180. package/dist/ui/hooks/useTerminalSize.js +29 -0
  181. package/dist/ui/hooks/useTerminalSize.js.map +1 -0
  182. package/dist/ui/input/keyRouter.d.ts +125 -0
  183. package/dist/ui/input/keyRouter.d.ts.map +1 -0
  184. package/dist/ui/input/keyRouter.js +574 -0
  185. package/dist/ui/input/keyRouter.js.map +1 -0
  186. package/dist/ui/layouts/ScreenLayout.d.ts +98 -0
  187. package/dist/ui/layouts/ScreenLayout.d.ts.map +1 -0
  188. package/dist/ui/layouts/ScreenLayout.js +56 -0
  189. package/dist/ui/layouts/ScreenLayout.js.map +1 -0
  190. package/dist/ui/shared/types.d.ts +143 -0
  191. package/dist/ui/shared/types.d.ts.map +1 -0
  192. package/dist/ui/shared/types.js +38 -0
  193. package/dist/ui/shared/types.js.map +1 -0
  194. package/dist/ui/theme.d.ts +29 -0
  195. package/dist/ui/theme.d.ts.map +1 -0
  196. package/dist/ui/theme.js +54 -0
  197. package/dist/ui/theme.js.map +1 -0
  198. package/dist/ui/utils/markdown.d.ts +74 -0
  199. package/dist/ui/utils/markdown.d.ts.map +1 -0
  200. package/dist/ui/utils/markdown.js +187 -0
  201. package/dist/ui/utils/markdown.js.map +1 -0
  202. package/dist/ui/utils/mentions.d.ts +12 -0
  203. package/dist/ui/utils/mentions.d.ts.map +1 -0
  204. package/dist/ui/utils/mentions.js +28 -0
  205. package/dist/ui/utils/mentions.js.map +1 -0
  206. package/dist/ui/utils/terminal-text.d.ts +33 -0
  207. package/dist/ui/utils/terminal-text.d.ts.map +1 -0
  208. package/dist/ui/utils/terminal-text.js +123 -0
  209. package/dist/ui/utils/terminal-text.js.map +1 -0
  210. package/dist/ui/utils/text.d.ts +72 -0
  211. package/dist/ui/utils/text.d.ts.map +1 -0
  212. package/dist/ui/utils/text.js +171 -0
  213. package/dist/ui/utils/text.js.map +1 -0
  214. package/eslint.config.js +40 -0
  215. package/graphify-out/GRAPH_REPORT.md +140 -0
  216. package/graphify-out/cache/1236b1806943f8eb5c64fece9fb448149370f4a1b52b32f0977ad528d7911291.json +1 -0
  217. package/graphify-out/cache/15cbf641dcc0fd641c18da872214918203a1f64baa7ac8430e4702ef32d18b02.json +1 -0
  218. package/graphify-out/cache/1abc52745bb46f6cc5a528530e2bc1f368c26e348dff85cdde5cad6f275d7177.json +1 -0
  219. package/graphify-out/cache/1c7815c37c13177e98895e272285f6151a8d0807478fc843ec452634b3e9912d.json +1 -0
  220. package/graphify-out/cache/1ec38c39f044f329c78642b61d6394fb8e15d0014335f32947d1f5c7b88c02bc.json +1 -0
  221. package/graphify-out/cache/20c9b57ff76dbb9d57cbd00659b0a42d2b28dd2abacf1ef4143cc67a5b6fe7f4.json +1 -0
  222. package/graphify-out/cache/285e11876884eb619e779d540cd957b33e70f14c67ecc26c9b9755aa9cedd717.json +1 -0
  223. package/graphify-out/cache/2c0a9c49ab51f4e0f07543b4e941bf802b7db31fa607c4cee8e595575be68fe7.json +1 -0
  224. package/graphify-out/cache/3025e6c2756c79ccaadd51d611f25a8b6630a6a11ce1b4e6e261b3e033dc194f.json +1 -0
  225. package/graphify-out/cache/4801a686ddfa0a52824a4fc3d65443cbb0ab9f0b030af67d02fccb520c584a08.json +1 -0
  226. package/graphify-out/cache/4f8fd96f38aa7d0293b4fab60c4581c4f3e68f4533c664c9cd78d1c53bea1cba.json +1 -0
  227. package/graphify-out/cache/5928e562b0f5700184b810e2403f4aa685e2b93c3c584c10460562b514c40fdd.json +1 -0
  228. package/graphify-out/cache/66132486efb0a4cfaeea766448f1f15bb33635808714052e7e24e5b00fbc984f.json +1 -0
  229. package/graphify-out/cache/66ab27669e7ec685130063662af9f30018c267f7639b4af4b0504a98282c383a.json +1 -0
  230. package/graphify-out/cache/6ff6b3dd433b5ffe96e166263a203dd622397e419dc00bb676824988b7defeab.json +1 -0
  231. package/graphify-out/cache/7a00a49932177a6215e2590b1ddfd6f1d8494792f693068e9d427d16b165d370.json +1 -0
  232. package/graphify-out/cache/7b681bc5182f40f707f1c0b16b78af38efffa62a1df315d10878f4de888408ef.json +1 -0
  233. package/graphify-out/cache/81196d44e50ac0b276d826a531ed10c425a528b298e3fe18e8d5a3eec996e9d5.json +1 -0
  234. package/graphify-out/cache/8934e9b32dea6e66fdc34fa1681b287ff1335c389917e0fc6ee2fc573e23e87b.json +1 -0
  235. package/graphify-out/cache/975f6c99807f86b79c80ca40c783618e2080a927eaebe6ea3db37b869f048851.json +1 -0
  236. package/graphify-out/cache/9ab98f08626fd8190462595e38de828293ad184db8df5e1177b05a8fe290faa1.json +1 -0
  237. package/graphify-out/cache/9c434a6796d7932b6e87a908c155c09cb2b8d097fc55f141f0302b6600f94749.json +1 -0
  238. package/graphify-out/cache/ac5cf4c489dc417c1f6ea728d0b6ba12ac7087bc6c2e736697ff3024b4d4e534.json +1 -0
  239. package/graphify-out/cache/ae5083b5c1dca3e5c6af485e7f6eb8a08ff8b382cdecf92ab5e0902053442afe.json +1 -0
  240. package/graphify-out/cache/b274057fc2e0118cf2746d1d8060841122bb2a2b0305566fc3f32d3a51e08bcc.json +1 -0
  241. package/graphify-out/cache/b39beef31da682387f030c0d149f6cd5b3e66effbf936cc8430db15360759dda.json +1 -0
  242. package/graphify-out/cache/bf3c6a36bb72820bc11cbf4a5c49067d1f62013d4686c019a4dbd9951b1e980e.json +1 -0
  243. package/graphify-out/cache/c034e156028d1a58b89bce659726e9a4065ddc4d2b560d6bce7a206fdff8dad3.json +1 -0
  244. package/graphify-out/cache/c619b5bbe8d1f427fbd7816d1e9fcfaf90d7201b020ae6b578166aeb9cb20683.json +1 -0
  245. package/graphify-out/cache/c8db77a9da0b0f601acfa50a620f31155cdda0d0cc0f2285adcea19e77194ee6.json +1 -0
  246. package/graphify-out/cache/ca664791ec7741a2077d9e7c10384142d30e7a3b77b388df719c315da608d57d.json +1 -0
  247. package/graphify-out/cache/cba43a116186bd55c8a63368dea4eee4a8123b2bec0a42df6efb3254f89f9a7a.json +1 -0
  248. package/graphify-out/cache/cd51cbb3dad1cf34417b9761fe2eb184a9f57d23dc86e45630e63bfa2ecafddc.json +1 -0
  249. package/graphify-out/cache/d06738cde193b34a136b94104220c17e0212b814b3e4d7ff399105635cae1a03.json +1 -0
  250. package/graphify-out/cache/d093c19f0169c0398eeb1360895b1c16ecc50abc59f4be8eec3f6fd27c89a6e5.json +1 -0
  251. package/graphify-out/cache/da8f75c438531d232fe114d5cbf2374576ce842a5fb87c7e5a88b78fdb334e5e.json +1 -0
  252. package/graphify-out/cache/e35798fa04ddede548a6953bdea506bd7d0788dfed4e8a8ea6ae77b1b60ef5e0.json +1 -0
  253. package/graphify-out/cache/e5a38c254719000487098782125e15e2e982c07d66b99d359ed1781f30f9fc8c.json +1 -0
  254. package/graphify-out/cache/e6e4c4a5a7200ee0154585b5d6538f699b0a746dbded3164f8391f06ce7cb212.json +1 -0
  255. package/graphify-out/cache/ea8d0838df6309b673b031f616b253af957898ee7289d25a541fa6183b732e4e.json +1 -0
  256. package/graphify-out/cache/ebaee92b0e881efd7a08f3143e2c1803310396b4651d4f77532003c699cc6836.json +1 -0
  257. package/graphify-out/cache/edf8543318a9e60d56d04704078da6c37202ddd6fdfc83144fe2b94fe19b80ef.json +1 -0
  258. package/graphify-out/cache/efca19d3668035f11126fc9eccbeab290abfa94babf3f9a8706de81d17aba263.json +1 -0
  259. package/graphify-out/cache/f1480bb642025ebd6d99986cdd8a7d5726897ec038300e4b095c9c938435dfad.json +1 -0
  260. package/graphify-out/cache/f459f21277f03865f732affb175b1b092954dae5967f27b3cd1864466e3e24f4.json +1 -0
  261. package/graphify-out/cache/f899e37e864010b30f4c196e86082d7bd022a0df032005e0407883ebcec36514.json +1 -0
  262. package/graphify-out/cache/fb3cbe07fbcd97c536cff9c6bc343c35f22d49613f05c53ad30ef67a75c5d93e.json +1 -0
  263. package/graphify-out/cache/fd3dc682b1ca2f5ae03b50d6b0e045ae50f5701c0a21c5a388d77ef482939d3a.json +1 -0
  264. package/graphify-out/graph.html +257 -0
  265. package/graphify-out/graph.json +4396 -0
  266. package/package.json +45 -6
  267. package/scripts/e2e-external.mjs +65 -0
  268. package/scripts/live-agents.mjs +6 -0
  269. package/scripts/live-stream.mjs +51 -0
  270. package/src/commands/agents.ts +91 -0
  271. package/src/commands/auth.ts +39 -0
  272. package/src/commands/chat.ts +198 -0
  273. package/src/commands/keys.ts +83 -0
  274. package/src/commands/local.ts +302 -0
  275. package/src/commands/mcp.ts +74 -0
  276. package/src/commands/prompts.ts +58 -0
  277. package/src/commands/skills.ts +43 -0
  278. package/src/core/auth/index.ts +101 -0
  279. package/src/core/config/index.ts +152 -0
  280. package/src/core/history.ts +79 -0
  281. package/src/core/markdown-renderer.ts +420 -0
  282. package/src/core/permissions/index.ts +99 -0
  283. package/src/core/provider/external.ts +312 -0
  284. package/src/core/provider/index.ts +517 -0
  285. package/src/core/registry/index.ts +250 -0
  286. package/src/core/schema/types.ts +106 -0
  287. package/src/core/terminal.ts +124 -0
  288. package/src/core/workspace/index.ts +216 -0
  289. package/src/index.ts +401 -0
  290. package/src/ui/app.tsx +644 -0
  291. package/src/ui/commands/cases/catalog.ts +76 -0
  292. package/src/ui/commands/cases/misc.ts +293 -0
  293. package/src/ui/commands/cases/tasks.ts +115 -0
  294. package/src/ui/commands/runCommand.ts +178 -0
  295. package/src/ui/components/Banner.tsx +63 -0
  296. package/src/ui/components/CommandMenu.tsx +63 -0
  297. package/src/ui/components/ErrorBoundary.tsx +75 -0
  298. package/src/ui/components/InputBar.tsx +227 -0
  299. package/src/ui/components/KeysPanel.tsx +115 -0
  300. package/src/ui/components/MessageView.tsx +226 -0
  301. package/src/ui/components/Pickers.tsx +234 -0
  302. package/src/ui/components/SearchOverlay.tsx +65 -0
  303. package/src/ui/components/WorkingIndicator.tsx +35 -0
  304. package/src/ui/controls/ApiKeyEntry.tsx +29 -0
  305. package/src/ui/controls/MultiSelect.tsx +55 -0
  306. package/src/ui/hooks/useAgentPicker.ts +64 -0
  307. package/src/ui/hooks/useAuthFlow.ts +47 -0
  308. package/src/ui/hooks/useKeysFlow.ts +157 -0
  309. package/src/ui/hooks/useSessionPicker.ts +84 -0
  310. package/src/ui/hooks/useTerminalSize.ts +36 -0
  311. package/src/ui/input/keyRouter.ts +627 -0
  312. package/src/ui/layouts/ScreenLayout.tsx +202 -0
  313. package/src/ui/shared/types.ts +160 -0
  314. package/src/ui/theme.ts +53 -0
  315. package/src/ui/utils/markdown.ts +255 -0
  316. package/src/ui/utils/mentions.ts +40 -0
  317. package/src/ui/utils/terminal-text.ts +130 -0
  318. package/src/ui/utils/text.ts +174 -0
  319. package/test/commands.test.mjs +127 -0
  320. package/test/config.test.mjs +123 -0
  321. package/test/history.test.mjs +42 -0
  322. package/test/keyrouter.test.mjs +324 -0
  323. package/test/layout.test.mjs +271 -0
  324. package/test/markdown.test.mjs +92 -0
  325. package/test/mentions.test.mjs +51 -0
  326. package/test/permissions.test.mjs +130 -0
  327. package/test/provider.test.mjs +287 -0
  328. package/test/terminal-text.test.mjs +116 -0
  329. package/test/text.test.mjs +134 -0
  330. package/test/workspace.test.mjs +101 -0
  331. package/tsconfig.json +32 -0
@@ -0,0 +1,627 @@
1
+ /**
2
+ * Global keyboard router. One function, checked top-to-bottom — the ORDER is
3
+ * behavior-critical (most-specific overlay first, base editing last):
4
+ * pendingOp → keys modes → sessions picker → agent picker →
5
+ * auth picker → api-key entry → streaming → base editing/menu/history.
6
+ */
7
+ import type { RefObject } from "react";
8
+ import { AUTH_OPTIONS, KEY_SCOPES } from "../shared/types.js";
9
+ import type {
10
+ AgentOption,
11
+ ChatMessage,
12
+ KeysState,
13
+ PendingOp,
14
+ } from "../shared/types.js";
15
+ import type { AuthMode } from "../hooks/useAuthFlow.js";
16
+ import {
17
+ toChars, moveLeft, moveRight, insertAt, deleteBefore, deleteAt, wordAt, wordBounds,
18
+ lineStart, lineEnd, moveVertical,
19
+ } from "../utils/text.js";
20
+ import type { SearchRow } from "../components/SearchOverlay.js";
21
+
22
+ export interface KeyRouterDeps {
23
+ /* current overlay states */
24
+ /** Folder trust gate. Nothing else is interactive until it is resolved. */
25
+ trust: { pending: boolean; index: number };
26
+ setTrust: React.Dispatch<React.SetStateAction<{ pending: boolean; index: number }>>;
27
+ /** Ctrl+/ command palette and Ctrl+R history search. null = closed. */
28
+ search: { mode: "commands" | "history" | null; query: string; index: number };
29
+ commandRows: SearchRow[];
30
+ historyRows: SearchRow[];
31
+ pendingOp: PendingOp | null;
32
+ keys: KeysState;
33
+ sessionPick: { open: boolean; busy: boolean; rows: Array<{ session_id: string }>; index: number };
34
+ agentPick: { open: boolean; busy: boolean; options: AgentOption[]; index: number };
35
+ authMode: AuthMode;
36
+ authIndex: number;
37
+ authBusy: boolean;
38
+ apiKeyDraft: string;
39
+ streaming: boolean;
40
+ input: string;
41
+ /** Caret position within `input` (character index). */
42
+ cursor: number;
43
+ history: string[];
44
+ historyIndex: number;
45
+ menuOpen: boolean;
46
+ filteredCommands: Array<{ name: string }>;
47
+ clampedMenuIndex: number;
48
+ menuWordLength: number;
49
+
50
+ /* setters & actions */
51
+ setSearch: React.Dispatch<React.SetStateAction<{ mode: "commands" | "history" | null; query: string; index: number }>>;
52
+ trustFolder: () => void;
53
+ setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>;
54
+ setPendingOp: (op: PendingOp | null) => void;
55
+ setKeys: React.Dispatch<React.SetStateAction<KeysState>>;
56
+ openKeysPanel: () => Promise<void>;
57
+ createKey: (name: string, scopes: string[]) => Promise<void>;
58
+ resumeSession: (sid: string) => Promise<void>;
59
+ setSessionPick: React.Dispatch<React.SetStateAction<{ open: boolean; busy: boolean; rows: Array<{ session_id: string }>; index: number }>>;
60
+ setAgentId: (id: string) => void;
61
+ setAgentPick: React.Dispatch<React.SetStateAction<{ open: boolean; busy: boolean; options: AgentOption[]; index: number }>>;
62
+ setAuthMode: React.Dispatch<React.SetStateAction<AuthMode>>;
63
+ setAuthIndex: React.Dispatch<React.SetStateAction<number>>;
64
+ setApiKeyDraft: React.Dispatch<React.SetStateAction<string>>;
65
+ submitApiKey: (keyVal: string) => Promise<void>;
66
+ runLocal: (cliArgs: string[]) => Promise<void>;
67
+ revokeSelected: () => Promise<void>;
68
+ showUsage: () => Promise<void>;
69
+ /** Set the persisted local active-key label from the selected row. */
70
+ selectKey: (row: KeysState["rows"][number]) => void;
71
+ submit: () => Promise<void> | void;
72
+ setMenuIndex: React.Dispatch<React.SetStateAction<number>>;
73
+ setInput: React.Dispatch<React.SetStateAction<string>>;
74
+ setCursor: React.Dispatch<React.SetStateAction<number>>;
75
+ setHistory: React.Dispatch<React.SetStateAction<string[]>>;
76
+ setHistoryIndex: React.Dispatch<React.SetStateAction<number>>;
77
+ historyDraftRef: RefObject<string>;
78
+ abortRef: RefObject<AbortController | null>;
79
+ queuedSubmitRef: RefObject<string | null>;
80
+ runCommand: (raw: string) => void;
81
+ requestExit: () => void;
82
+ }
83
+
84
+ type InkKey = {
85
+ upArrow: boolean; downArrow: boolean; leftArrow?: boolean; rightArrow?: boolean;
86
+ return: boolean; escape: boolean;
87
+ backspace?: boolean; delete?: boolean; tab?: boolean; ctrl?: boolean; meta?: boolean;
88
+ shift?: boolean;
89
+ };
90
+
91
+ /**
92
+ * Insert a line break at the caret instead of submitting. Two triggers:
93
+ * - Shift/Alt+Enter, on terminals that report the modifier;
94
+ * - a trailing `\` at the end of the line (works everywhere, like a shell).
95
+ * Returns true when the key was consumed.
96
+ */
97
+ const tryNewline = (
98
+ key: InkKey,
99
+ d: KeyRouterDeps,
100
+ ): boolean => {
101
+ // End of the *current line*, not end of the draft: a trailing "\" starts a
102
+ // continuation on the line being edited, as in a shell.
103
+ const atLineEnd = d.cursor >= lineEnd(d.input, d.cursor);
104
+ if (key.shift || key.meta) {
105
+ const next = insertAt(d.input, d.cursor, "\n");
106
+ d.setInput(next.text);
107
+ d.setCursor(() => next.cursor);
108
+ d.setMenuIndex(0);
109
+ return true;
110
+ }
111
+ // Trailing backslash continuation: swap it for the line break.
112
+ const chars = toChars(d.input);
113
+ if (atLineEnd && chars[d.cursor - 1] === "\\") {
114
+ chars.splice(d.cursor - 1, 1, "\n");
115
+ const text = chars.join("");
116
+ d.setInput(text);
117
+ d.setCursor(() => d.cursor);
118
+ d.setMenuIndex(0);
119
+ return true;
120
+ }
121
+ return false;
122
+ };
123
+
124
+ /**
125
+ * Alt+Enter on xterm-style terminals arrives as `ESC CR`, which Ink reports as
126
+ * chunk "\r" with `return` and `meta` both false — it is neither a submit nor
127
+ * a recognised newline, so it fell through to the insert branch and stored a
128
+ * raw CR. Treat it as the line break the user actually pressed.
129
+ */
130
+ const isAltEnter = (chunk: string, key: InkKey): boolean =>
131
+ (chunk === "\r" || chunk === "\n") && !key.return && !key.ctrl;
132
+
133
+ export const handleKey = (
134
+ chunk: string,
135
+ key: InkKey,
136
+ d: KeyRouterDeps,
137
+ ): void => {
138
+ // The trust gate owns every key until the user answers. Esc/Ctrl+C and "2"
139
+ // all mean decline (and therefore exit) here — there is no partially-trusted
140
+ // mode to retreat into.
141
+ if (d.trust.pending) {
142
+ if (key.upArrow) {
143
+ d.setTrust((t) => ({ ...t, index: 0 }));
144
+ } else if (key.downArrow) {
145
+ d.setTrust((t) => ({ ...t, index: 1 }));
146
+ } else if (chunk === "1") {
147
+ d.trustFolder();
148
+ } else if (chunk === "2" || key.escape) {
149
+ d.requestExit();
150
+ } else if (key.return) {
151
+ if (d.trust.index === 0) d.trustFolder();
152
+ else d.requestExit();
153
+ }
154
+ return;
155
+ }
156
+
157
+ // Search overlays own every key. Rows are ranked in app.tsx (single source
158
+ // of truth for the matcher) so the highlighted index always addresses the
159
+ // same row that is painted.
160
+ if (d.search.mode) {
161
+ const rows = d.search.mode === "commands" ? d.commandRows : d.historyRows;
162
+ const close = () => d.setSearch({ mode: null, query: "", index: 0 });
163
+ if (key.escape || (key.ctrl && (chunk === "/" || chunk === "r"))) {
164
+ close();
165
+ return;
166
+ }
167
+ if (key.upArrow) {
168
+ d.setSearch((s) => ({ ...s, index: Math.max(0, s.index - 1) }));
169
+ return;
170
+ }
171
+ if (key.downArrow) {
172
+ d.setSearch((s) => ({ ...s, index: Math.min(Math.max(0, rows.length - 1), s.index + 1) }));
173
+ return;
174
+ }
175
+ if (key.return) {
176
+ const row = rows[d.search.index];
177
+ if (row) {
178
+ if (d.search.mode === "commands") {
179
+ d.setInput("");
180
+ d.setCursor(() => 0);
181
+ d.runCommand(row.key);
182
+ } else {
183
+ d.setInput(row.label);
184
+ d.setCursor(toChars(row.label).length);
185
+ }
186
+ }
187
+ close();
188
+ return;
189
+ }
190
+ if (key.delete || key.backspace || chunk === "\b") {
191
+ d.setSearch((s) => ({ ...s, query: toChars(s.query).slice(0, -1).join(""), index: 0 }));
192
+ return;
193
+ }
194
+ if (chunk && !key.ctrl && !key.meta) {
195
+ d.setSearch((s) => ({ ...s, query: s.query + chunk, index: 0 }));
196
+ }
197
+ return;
198
+ }
199
+
200
+ // Ctrl+C is checked before every overlay. Each overlay branch below returns
201
+ // unconditionally, so without this the key is swallowed while a picker, the
202
+ // /keys panel, or the permission prompt is up — leaving no keyboard way out.
203
+ // It keeps its usual meaning: clear the draft, or exit when it is empty.
204
+ if (key.ctrl && chunk === "c") {
205
+ if (d.input) {
206
+ d.setInput("");
207
+ d.setCursor(() => 0);
208
+ } else {
209
+ d.requestExit();
210
+ }
211
+ return;
212
+ }
213
+ // y/N permission prompt intercepts everything while a risky op is pending
214
+ if (d.pendingOp) {
215
+ const yes = chunk === "y" || chunk === "Y";
216
+ const no = chunk === "n" || chunk === "N" || key.escape;
217
+ if (yes) {
218
+ const op = d.pendingOp;
219
+ d.setMessages((prev) => [...prev, { role: "system", text: `✓ approved — running ${op.kind}` }]);
220
+ if (op.onApproved) op.onApproved();
221
+ else void d.runLocal([...op.cliArgs, "--yes"]);
222
+ d.setPendingOp(null);
223
+ } else if (no) {
224
+ d.setMessages((prev) => [...prev, { role: "system", text: "✗ denied" }]);
225
+ d.pendingOp.onDenied?.();
226
+ d.setPendingOp(null);
227
+ }
228
+ return;
229
+ }
230
+
231
+ // /keys flow intercepts all input when active
232
+ if (d.keys.mode === "created") {
233
+ if (key.return || key.escape) {
234
+ d.setKeys((k) => ({ ...k, mode: "closed", createdKey: null }));
235
+ void d.openKeysPanel();
236
+ }
237
+ return;
238
+ }
239
+
240
+ if (d.keys.mode === "create-name") {
241
+ if (key.return) {
242
+ const name = d.keys.draftName.trim();
243
+ if (!name) return;
244
+ // Name captured — advance to the scopes multi-select (`chat` pre-checked).
245
+ d.setKeys((k) => ({ ...k, mode: "create-scopes", scopeCursor: 0, scopeSelection: new Set(["chat"]) }));
246
+ } else if (key.escape) {
247
+ d.setKeys((k) => ({ ...k, mode: "closed", draftName: "" }));
248
+ } else if (key.backspace || key.delete) {
249
+ d.setKeys((k) => ({ ...k, draftName: k.draftName.slice(0, -1) }));
250
+ } else if (chunk && !key.ctrl && !key.meta) {
251
+ d.setKeys((k) => ({ ...k, draftName: k.draftName + chunk }));
252
+ }
253
+ return;
254
+ }
255
+
256
+ // Scopes multi-select: up/down move the cursor, space toggles the focused
257
+ // scope, enter confirms (creates the key with chosen scopes), esc cancels.
258
+ if (d.keys.mode === "create-scopes") {
259
+ // Creating a key is a network round-trip; without this guard a second
260
+ // Enter lands while the first request is in flight and mints a duplicate.
261
+ if (d.keys.busy) return;
262
+ if (key.upArrow) {
263
+ d.setKeys((k) => ({ ...k, scopeCursor: Math.max(0, k.scopeCursor - 1) }));
264
+ } else if (key.downArrow) {
265
+ d.setKeys((k) => ({ ...k, scopeCursor: Math.min(KEY_SCOPES.length - 1, k.scopeCursor + 1) }));
266
+ } else if (chunk === " ") {
267
+ d.setKeys((k) => {
268
+ const id = KEY_SCOPES[k.scopeCursor]?.id;
269
+ if (!id) return k;
270
+ const next = new Set(k.scopeSelection);
271
+ if (next.has(id)) {
272
+ next.delete(id);
273
+ } else {
274
+ next.add(id);
275
+ }
276
+ return { ...k, scopeSelection: next };
277
+ });
278
+ } else if (key.return) {
279
+ const scopes = KEY_SCOPES.filter((s) => d.keys.scopeSelection.has(s.id)).map((s) => s.id);
280
+ if (scopes.length === 0) return; // require at least one scope
281
+ void d.createKey(d.keys.draftName.trim(), scopes);
282
+ } else if (key.escape) {
283
+ d.setKeys((k) => ({ ...k, mode: "menu" }));
284
+ }
285
+ return;
286
+ }
287
+
288
+ // Revocation is irreversible, so it always goes through an explicit y/N
289
+ // step — a stray `r` must never delete a live production key.
290
+ if (d.keys.mode === "confirm-revoke") {
291
+ if (chunk === "y" || chunk === "Y") {
292
+ d.setKeys((k) => ({ ...k, mode: "menu" }));
293
+ void d.revokeSelected();
294
+ } else if (chunk === "n" || chunk === "N" || key.escape || key.return) {
295
+ d.setKeys((k) => ({ ...k, mode: "menu" }));
296
+ }
297
+ return;
298
+ }
299
+
300
+ if (d.keys.mode === "menu") {
301
+ if (d.keys.busy) return;
302
+ if (key.upArrow) {
303
+ d.setKeys((k) => ({ ...k, selectedIndex: Math.max(0, k.selectedIndex - 1) }));
304
+ } else if (key.downArrow) {
305
+ d.setKeys((k) => ({ ...k, selectedIndex: Math.min(Math.max(0, k.rows.length - 1), k.selectedIndex + 1) }));
306
+ } else if (chunk === "a") {
307
+ d.setKeys((k) => ({ ...k, mode: "create-name", draftName: "" }));
308
+ } else if (chunk === "r") {
309
+ if (d.keys.rows[d.keys.selectedIndex]) {
310
+ d.setKeys((k) => ({ ...k, mode: "confirm-revoke" }));
311
+ }
312
+ } else if (chunk === "u") {
313
+ void d.showUsage();
314
+ } else if (chunk === "s") {
315
+ const row = d.keys.rows[d.keys.selectedIndex];
316
+ if (row) d.selectKey(row);
317
+ } else if (key.escape) {
318
+ d.setKeys((k) => ({ ...k, mode: "closed" }));
319
+ }
320
+ return;
321
+ }
322
+
323
+ // /sessions picker intercepts all input when open
324
+ if (d.sessionPick.open) {
325
+ if (d.sessionPick.busy) return;
326
+ if (key.upArrow) {
327
+ d.setSessionPick((p) => ({ ...p, index: Math.max(0, p.index - 1) }));
328
+ } else if (key.downArrow) {
329
+ d.setSessionPick((p) => ({
330
+ ...p,
331
+ index: Math.min(Math.max(0, p.rows.length - 1), p.index + 1),
332
+ }));
333
+ } else if (key.return) {
334
+ // Guard the index: a throw inside the input handler reaches the
335
+ // process-level uncaughtException path and exits the CLI.
336
+ const row = d.sessionPick.rows[d.sessionPick.index];
337
+ if (row) void d.resumeSession(row.session_id);
338
+ } else if (key.escape) {
339
+ d.setSessionPick({ open: false, busy: false, rows: [], index: 0 });
340
+ }
341
+ return;
342
+ }
343
+
344
+ // Agent picker intercepts all input when open
345
+ if (d.agentPick.open) {
346
+ if (d.agentPick.busy) return;
347
+ if (key.upArrow) {
348
+ d.setAgentPick((p) => ({ ...p, index: Math.max(0, p.index - 1) }));
349
+ } else if (key.downArrow) {
350
+ d.setAgentPick((p) => ({ ...p, index: Math.min(p.options.length - 1, p.index + 1) }));
351
+ } else if (key.return) {
352
+ const choice = d.agentPick.options[d.agentPick.index];
353
+ if (choice) {
354
+ d.setAgentId(choice.id);
355
+ d.setMessages((prev) => [...prev, { role: "system", text: `✓ Agent switched to: ${choice.label}` }]);
356
+ }
357
+ d.setAgentPick((p) => ({ ...p, open: false }));
358
+ } else if (key.escape) {
359
+ d.setAgentPick((p) => ({ ...p, open: false }));
360
+ }
361
+ return;
362
+ }
363
+
364
+ // Auth flow intercepts all input when active
365
+ if (d.authMode === "picker") {
366
+ if (key.upArrow) {
367
+ d.setAuthIndex((i) => Math.max(0, i - 1));
368
+ } else if (key.downArrow) {
369
+ d.setAuthIndex((i) => Math.min(AUTH_OPTIONS.length - 1, i + 1));
370
+ } else if (key.return) {
371
+ const choice = AUTH_OPTIONS[d.authIndex];
372
+ if (choice.id === "api-key") {
373
+ d.setApiKeyDraft("");
374
+ d.setAuthMode("api-key");
375
+ } else if (choice.id === "keys") {
376
+ d.setAuthMode("closed");
377
+ void d.openKeysPanel();
378
+ } else {
379
+ d.setMessages((prev) => [
380
+ ...prev,
381
+ { role: "system", text: "Account sign-in is not yet implemented — use an API key for now." },
382
+ ]);
383
+ d.setAuthMode("closed");
384
+ }
385
+ } else if (key.escape) {
386
+ d.setAuthMode("closed");
387
+ }
388
+ return;
389
+ }
390
+
391
+ // API key entry mode: capture characters, mask in render, submit on Enter
392
+ if (d.authMode === "api-key") {
393
+ if (d.authBusy) return;
394
+ if (key.return) {
395
+ const keyVal = d.apiKeyDraft.trim();
396
+ if (!keyVal) return;
397
+ void d.submitApiKey(keyVal);
398
+ } else if (key.escape) {
399
+ d.setAuthMode("closed");
400
+ d.setApiKeyDraft("");
401
+ } else if (key.backspace || key.delete) {
402
+ d.setApiKeyDraft((prev) => prev.slice(0, -1));
403
+ } else if (chunk && !key.ctrl && !key.meta) {
404
+ d.setApiKeyDraft((prev) => prev + chunk);
405
+ }
406
+ return;
407
+ }
408
+
409
+ // Palette / history search. Checked after every modal overlay so a picker
410
+ // keeps its own keys, but before streaming so a chord still works mid-reply.
411
+ if (key.ctrl && (chunk === "/" || chunk === "r")) {
412
+ d.setSearch({ mode: chunk === "/" ? "commands" : "history", query: "", index: 0 });
413
+ return;
414
+ }
415
+
416
+ // While streaming, esc interrupts and Enter queues the typed CHAT input —
417
+ // slash commands still dispatch immediately. Everything else falls through
418
+ // to the normal editor, so the next prompt can be composed mid-reply.
419
+ if (d.streaming) {
420
+ if (key.escape) {
421
+ d.abortRef.current?.abort();
422
+ // Interrupting must not then send the prompt the user had queued before
423
+ // deciding to stop; without this there was no way to cancel a queued
424
+ // message at all — it was sent by sendTurn's finally block.
425
+ if (d.queuedSubmitRef.current) {
426
+ d.queuedSubmitRef.current = null;
427
+ d.setMessages((prev) => [
428
+ ...prev,
429
+ { role: "system", text: "✋ interrupted — queued prompt discarded" },
430
+ ]);
431
+ }
432
+ return;
433
+ }
434
+ if (key.return) {
435
+ // Shift/Alt+Enter still starts a new line instead of queueing.
436
+ if (!d.menuOpen && tryNewline(key, d)) return;
437
+ // A completed "/command" runs right away; only plain chat queues.
438
+ const typed = d.input.trim();
439
+ const isCommand = /^\/\S+$/.test(typed);
440
+ if (isCommand && !typed.includes(" ")) {
441
+ // Route through the same menu-less path: dispatch and clear.
442
+ d.setInput("");
443
+ d.setMenuIndex(0);
444
+ d.runCommand(typed);
445
+ return;
446
+ }
447
+ if (!typed) return; // Enter on an empty line is a no-op, not a queue.
448
+ d.queuedSubmitRef.current = typed;
449
+ d.setInput("");
450
+ d.setMessages((prev) => [...prev, { role: "system", text: "⏳ queued — will send after the current reply" }]);
451
+ return;
452
+ }
453
+ }
454
+
455
+ // Ctrl+D on an empty line exits; otherwise it deletes forward (readline).
456
+ if (key.ctrl && chunk === "d") {
457
+ if (!d.input) {
458
+ d.requestExit();
459
+ return;
460
+ }
461
+ const next = deleteAt(d.input, d.cursor);
462
+ d.setInput(next.text);
463
+ d.setCursor(next.cursor);
464
+ return;
465
+ }
466
+
467
+ // Ctrl+U clears to the start of the current line (readline); Ctrl+W deletes
468
+ // the previous word.
469
+ if (key.ctrl && chunk === "u") {
470
+ const chars = toChars(d.input);
471
+ const start = lineStart(d.input, d.cursor);
472
+ d.setInput(chars.slice(0, start).concat(chars.slice(d.cursor)).join(""));
473
+ d.setCursor(() => start);
474
+ return;
475
+ }
476
+ if (key.ctrl && chunk === "w") {
477
+ const chars = toChars(d.input);
478
+ const caret = Math.min(d.cursor, chars.length);
479
+ let at = caret;
480
+ while (at > 0 && /\s/.test(chars[at - 1])) at--;
481
+ while (at > 0 && !/\s/.test(chars[at - 1])) at--;
482
+ d.setInput(chars.slice(0, at).concat(chars.slice(caret)).join(""));
483
+ d.setCursor(() => at);
484
+ return;
485
+ }
486
+
487
+ // Ctrl+A / Ctrl+E jump to the start / end of the (current) line.
488
+ if (key.ctrl && chunk === "a") {
489
+ d.setCursor((c) => lineStart(d.input, c));
490
+ return;
491
+ }
492
+ if (key.ctrl && chunk === "e") {
493
+ d.setCursor((c) => lineEnd(d.input, c));
494
+ return;
495
+ }
496
+
497
+ // Cursor movement. Word-jump modifiers are checked before the plain arrows
498
+ // (Ink reports ctrl+left as both ctrl and leftArrow). With the slash menu
499
+ // open, ↑/↓ stay reserved for it below.
500
+ if (key.ctrl && key.leftArrow) {
501
+ d.setCursor((c) => {
502
+ const chars = toChars(d.input);
503
+ let at = Math.max(0, Math.min(c, chars.length));
504
+ while (at > 0 && /\s/.test(chars[at - 1])) at--;
505
+ while (at > 0 && !/\s/.test(chars[at - 1])) at--;
506
+ return at;
507
+ });
508
+ return;
509
+ }
510
+ if (key.ctrl && key.rightArrow) {
511
+ d.setCursor((c) => {
512
+ const chars = toChars(d.input);
513
+ let at = Math.max(0, Math.min(c, chars.length));
514
+ while (at < chars.length && !/\s/.test(chars[at])) at++;
515
+ while (at < chars.length && /\s/.test(chars[at])) at++;
516
+ return at;
517
+ });
518
+ return;
519
+ }
520
+ if (key.leftArrow) {
521
+ d.setCursor((c) => moveLeft(c));
522
+ return;
523
+ }
524
+ if (key.rightArrow) {
525
+ d.setCursor((c) => moveRight(d.input, c));
526
+ return;
527
+ }
528
+
529
+ if (key.upArrow && d.menuOpen && d.filteredCommands.length > 0) {
530
+ d.setMenuIndex((i) => Math.max(0, i - 1));
531
+ return;
532
+ }
533
+ if (key.downArrow && d.menuOpen && d.filteredCommands.length > 0) {
534
+ d.setMenuIndex((i) => Math.min(d.filteredCommands.length - 1, i + 1));
535
+ return;
536
+ }
537
+ // Multiline input: ↑/↓ first walk the wrapped lines, and only fall through
538
+ // to history once the caret is on the first / last one (editor convention).
539
+ if (key.upArrow && !d.menuOpen && d.input.includes("\n") && lineStart(d.input, d.cursor) > 0) {
540
+ d.setCursor((c) => moveVertical(d.input, c, -1));
541
+ return;
542
+ }
543
+ if (key.downArrow && !d.menuOpen && d.input.includes("\n") && lineEnd(d.input, d.cursor) < d.input.length) {
544
+ d.setCursor((c) => moveVertical(d.input, c, 1));
545
+ return;
546
+ }
547
+ // History recall — ↑ walks backwards through past inputs, ↓ forwards.
548
+ if (key.upArrow && !d.menuOpen && d.history.length > 0) {
549
+ if (d.historyIndex === -1) d.historyDraftRef.current = d.input;
550
+ const next = Math.min(d.history.length - 1, d.historyIndex + 1);
551
+ const recalled = d.history[d.history.length - 1 - next];
552
+ d.setHistoryIndex(next);
553
+ d.setInput(recalled);
554
+ d.setCursor(toChars(recalled).length);
555
+ return;
556
+ }
557
+ if (key.downArrow && !d.menuOpen && d.historyIndex !== -1) {
558
+ const prev = d.historyIndex - 1;
559
+ const recalled = prev === -1 ? d.historyDraftRef.current : d.history[d.history.length - 1 - prev];
560
+ d.setHistoryIndex(prev);
561
+ d.setInput(recalled);
562
+ d.setCursor(toChars(recalled).length);
563
+ return;
564
+ }
565
+ if (key.tab && d.menuOpen && d.filteredCommands.length > 0) {
566
+ const cmd = d.filteredCommands[d.clampedMenuIndex];
567
+ // Replace the whole "/partial" token with the full command, leaving any
568
+ // surrounding prompt text untouched. The token spans the caret, not up to
569
+ // it: completing "/he|lp" must not append the remainder as arguments. All
570
+ // indices are code points so an emoji elsewhere can't skew the caret.
571
+ const chars = toChars(d.input);
572
+ const { start, end } = wordBounds(d.input, d.cursor);
573
+ const head = chars.slice(0, start).join("");
574
+ const rest = chars.slice(end).join("");
575
+ const completed = `${head}/${cmd.name}${rest}`;
576
+ d.setInput(completed);
577
+ d.setCursor(start + cmd.name.length + 1);
578
+ d.setMenuIndex(0);
579
+ return;
580
+ }
581
+ if (key.return) {
582
+ // Shift/Alt+Enter and a trailing "\" insert a line break; plain Enter sends.
583
+ if (!d.menuOpen && tryNewline(key, d)) return;
584
+ // With the slash-menu open, Enter runs the highlighted command directly:
585
+ // commands are statements, not message content.
586
+ if (d.menuOpen && d.filteredCommands.length > 0) {
587
+ const cmd = d.filteredCommands[d.clampedMenuIndex];
588
+ // Arguments typed after the command name are preserved; bare "/" yields
589
+ // none. Measured from the end of the token for the same reason as Tab.
590
+ const chars = toChars(d.input);
591
+ const { end } = wordBounds(d.input, d.cursor);
592
+ const afterWord = chars.slice(end).join("").trim();
593
+ d.setInput("");
594
+ d.setCursor(() => 0);
595
+ d.setMenuIndex(0);
596
+ d.runCommand(`/${cmd.name}${afterWord ? ` ${afterWord}` : ""}`);
597
+ return;
598
+ }
599
+ d.setHistoryIndex(-1);
600
+ void d.submit();
601
+ return;
602
+ }
603
+ // Backspace. Ink 5's keypress parser maps the standard Backspace byte 0x7f
604
+ // to `name:"delete"`, so `key.delete` is what arrives for the key labelled
605
+ // Backspace on every common terminal; the real Delete key sends CSI 3~ but
606
+ // is indistinguishable here. Forward-delete therefore lives on Ctrl+D
607
+ // (readline's binding) and this branch deletes backwards.
608
+ if (key.delete || key.backspace || chunk === "\b") {
609
+ const next = deleteBefore(d.input, d.cursor);
610
+ d.setInput(next.text);
611
+ d.setCursor(() => next.cursor);
612
+ d.setMenuIndex(0);
613
+ return;
614
+ }
615
+ // Raw NUL: a terminal that sent a bare ESC gets its remainder delivered as
616
+ // this chunk. Swallow it — inserting "\0" is never what the user meant.
617
+ if (chunk === "\0") return;
618
+ if (chunk && !key.ctrl && !key.meta) {
619
+ // Paste arrives as one chunk; insert it whole at the caret. Normalize line
620
+ // endings first: a CRLF paste would otherwise store a bare CR, which is
621
+ // not a line break to the editor and gets sent to the model verbatim.
622
+ const next = insertAt(d.input, d.cursor, chunk.replace(/\r\n?/g, "\n"));
623
+ d.setInput(next.text);
624
+ d.setCursor(() => next.cursor);
625
+ d.setMenuIndex(0);
626
+ }
627
+ };