rird 2.1.231 → 2.3.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 (381) hide show
  1. package/AGENTS.md +86 -0
  2. package/COMPLETED_TEST_SUITE.txt +280 -0
  3. package/Dockerfile +18 -0
  4. package/README.md +397 -6
  5. package/RIRD_ERROR_HANDLING_SUMMARY.md +307 -0
  6. package/TESTING.md +512 -0
  7. package/TEST_IMPLEMENTATION_REPORT.md +463 -0
  8. package/TEST_SUITE.md +307 -0
  9. package/TEST_SUMMARY.txt +380 -0
  10. package/bin/rird-perf.js +37 -0
  11. package/bin/rird.js +43 -8
  12. package/bunfig.toml +4 -0
  13. package/create-wrapper.ps1 +51 -0
  14. package/docs/ARCHITECTURE.md +768 -0
  15. package/docs/CLI_REFERENCE.md +681 -0
  16. package/docs/DOCUMENTATION_MANIFEST.md +392 -0
  17. package/docs/INDEX.md +295 -0
  18. package/docs/PRODUCTION_SETUP.md +633 -0
  19. package/docs/TROUBLESHOOTING.md +914 -0
  20. package/facebook_ads_library.png +0 -0
  21. package/nul +0 -0
  22. package/nul`nif +0 -0
  23. package/package.json +104 -15
  24. package/parsers-config.ts +239 -0
  25. package/rird-1.0.199.tgz +0 -0
  26. package/rird-1.0.205.tgz +0 -0
  27. package/script/build-windows.ts +56 -0
  28. package/script/build.ts +165 -0
  29. package/{postinstall.mjs → script/postinstall.mjs} +47 -68
  30. package/script/publish-registries.ts +187 -0
  31. package/script/publish.ts +85 -0
  32. package/script/schema.ts +47 -0
  33. package/src/acp/README.md +164 -0
  34. package/src/acp/agent.ts +1063 -0
  35. package/src/acp/session.ts +101 -0
  36. package/src/acp/types.ts +22 -0
  37. package/src/agent/agent.ts +367 -0
  38. package/src/agent/generate.txt +75 -0
  39. package/src/agent/prompt/compaction.txt +12 -0
  40. package/src/agent/prompt/explore.txt +18 -0
  41. package/src/agent/prompt/summary.txt +10 -0
  42. package/src/agent/prompt/title.txt +36 -0
  43. package/src/auth/index.ts +70 -0
  44. package/src/bun/index.ts +114 -0
  45. package/src/bus/bus-event.ts +43 -0
  46. package/src/bus/global.ts +10 -0
  47. package/src/bus/index.ts +105 -0
  48. package/src/cli/bootstrap.ts +17 -0
  49. package/src/cli/cmd/acp.ts +104 -0
  50. package/src/cli/cmd/activate.ts +50 -0
  51. package/src/cli/cmd/agent.ts +256 -0
  52. package/src/cli/cmd/auth.ts +412 -0
  53. package/src/cli/cmd/cmd.ts +7 -0
  54. package/src/cli/cmd/debug/config.ts +15 -0
  55. package/src/cli/cmd/debug/file.ts +91 -0
  56. package/src/cli/cmd/debug/index.ts +43 -0
  57. package/src/cli/cmd/debug/lsp.ts +48 -0
  58. package/src/cli/cmd/debug/ripgrep.ts +83 -0
  59. package/src/cli/cmd/debug/scrap.ts +15 -0
  60. package/src/cli/cmd/debug/skill.ts +15 -0
  61. package/src/cli/cmd/debug/snapshot.ts +48 -0
  62. package/src/cli/cmd/export.ts +88 -0
  63. package/src/cli/cmd/generate.ts +38 -0
  64. package/src/cli/cmd/github.ts +1400 -0
  65. package/src/cli/cmd/import.ts +98 -0
  66. package/src/cli/cmd/mcp.ts +654 -0
  67. package/src/cli/cmd/models.ts +68 -0
  68. package/src/cli/cmd/pr.ts +112 -0
  69. package/src/cli/cmd/run.ts +434 -0
  70. package/src/cli/cmd/serve.ts +31 -0
  71. package/src/cli/cmd/session.ts +106 -0
  72. package/src/cli/cmd/stats.ts +298 -0
  73. package/src/cli/cmd/tui/app.tsx +694 -0
  74. package/src/cli/cmd/tui/attach.ts +30 -0
  75. package/src/cli/cmd/tui/component/border.tsx +21 -0
  76. package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
  77. package/src/cli/cmd/tui/component/dialog-command.tsx +124 -0
  78. package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
  79. package/src/cli/cmd/tui/component/dialog-model.tsx +236 -0
  80. package/src/cli/cmd/tui/component/dialog-provider.tsx +240 -0
  81. package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
  82. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
  83. package/src/cli/cmd/tui/component/dialog-stash.tsx +86 -0
  84. package/src/cli/cmd/tui/component/dialog-status.tsx +162 -0
  85. package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
  86. package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
  87. package/src/cli/cmd/tui/component/did-you-know.tsx +85 -0
  88. package/src/cli/cmd/tui/component/logo.tsx +48 -0
  89. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +574 -0
  90. package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
  91. package/src/cli/cmd/tui/component/prompt/index.tsx +1087 -0
  92. package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
  93. package/src/cli/cmd/tui/component/tips.ts +27 -0
  94. package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
  95. package/src/cli/cmd/tui/context/args.tsx +14 -0
  96. package/src/cli/cmd/tui/context/directory.ts +13 -0
  97. package/src/cli/cmd/tui/context/exit.tsx +23 -0
  98. package/src/cli/cmd/tui/context/helper.tsx +25 -0
  99. package/src/cli/cmd/tui/context/keybind.tsx +101 -0
  100. package/src/cli/cmd/tui/context/kv.tsx +49 -0
  101. package/src/cli/cmd/tui/context/local.tsx +345 -0
  102. package/src/cli/cmd/tui/context/prompt.tsx +18 -0
  103. package/src/cli/cmd/tui/context/route.tsx +46 -0
  104. package/src/cli/cmd/tui/context/sdk.tsx +74 -0
  105. package/src/cli/cmd/tui/context/sync.tsx +372 -0
  106. package/src/cli/cmd/tui/context/theme/aura.json +69 -0
  107. package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
  108. package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
  109. package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
  110. package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
  111. package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
  112. package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
  113. package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
  114. package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
  115. package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
  116. package/src/cli/cmd/tui/context/theme/github.json +233 -0
  117. package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
  118. package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
  119. package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -0
  120. package/src/cli/cmd/tui/context/theme/material.json +235 -0
  121. package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
  122. package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
  123. package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
  124. package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
  125. package/src/cli/cmd/tui/context/theme/nord.json +223 -0
  126. package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
  127. package/src/cli/cmd/tui/context/theme/orng.json +245 -0
  128. package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
  129. package/src/cli/cmd/tui/context/theme/rird.json +245 -0
  130. package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
  131. package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
  132. package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
  133. package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
  134. package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
  135. package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
  136. package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
  137. package/src/cli/cmd/tui/context/theme.tsx +1109 -0
  138. package/src/cli/cmd/tui/event.ts +40 -0
  139. package/src/cli/cmd/tui/hooks/use-safe-terminal-dimensions.ts +12 -0
  140. package/src/cli/cmd/tui/routes/home.tsx +138 -0
  141. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
  142. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
  143. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
  144. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
  145. package/src/cli/cmd/tui/routes/session/footer.tsx +88 -0
  146. package/src/cli/cmd/tui/routes/session/header.tsx +125 -0
  147. package/src/cli/cmd/tui/routes/session/index.tsx +1876 -0
  148. package/src/cli/cmd/tui/routes/session/sidebar.tsx +320 -0
  149. package/src/cli/cmd/tui/spawn.ts +60 -0
  150. package/src/cli/cmd/tui/thread.ts +142 -0
  151. package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
  152. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
  153. package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
  154. package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
  155. package/src/cli/cmd/tui/ui/dialog-select.tsx +333 -0
  156. package/src/cli/cmd/tui/ui/dialog.tsx +171 -0
  157. package/src/cli/cmd/tui/ui/spinner.ts +368 -0
  158. package/src/cli/cmd/tui/ui/toast.tsx +100 -0
  159. package/src/cli/cmd/tui/util/clipboard.ts +127 -0
  160. package/src/cli/cmd/tui/util/editor.ts +32 -0
  161. package/src/cli/cmd/tui/util/terminal.ts +146 -0
  162. package/src/cli/cmd/tui/worker.ts +63 -0
  163. package/src/cli/cmd/uninstall.ts +344 -0
  164. package/src/cli/cmd/upgrade.ts +127 -0
  165. package/src/cli/cmd/web.ts +84 -0
  166. package/src/cli/error.ts +69 -0
  167. package/src/cli/ui.ts +101 -0
  168. package/src/cli/upgrade.ts +28 -0
  169. package/src/command/index.ts +80 -0
  170. package/src/command/template/initialize.txt +10 -0
  171. package/src/command/template/review.txt +97 -0
  172. package/src/config/config.ts +994 -0
  173. package/src/config/markdown.ts +41 -0
  174. package/src/env/index.ts +26 -0
  175. package/src/file/ignore.ts +83 -0
  176. package/src/file/index.ts +328 -0
  177. package/src/file/ripgrep.ts +393 -0
  178. package/src/file/time.ts +64 -0
  179. package/src/file/watcher.ts +103 -0
  180. package/src/flag/flag.ts +84 -0
  181. package/src/format/formatter.ts +315 -0
  182. package/src/format/index.ts +137 -0
  183. package/src/global/index.ts +101 -0
  184. package/src/id/id.ts +73 -0
  185. package/src/ide/index.ts +76 -0
  186. package/src/index.ts +297 -0
  187. package/src/index.ts.backup +271 -0
  188. package/src/installation/index.ts +258 -0
  189. package/src/lib/IMPLEMENTATION_NOTES.md +345 -0
  190. package/src/lib/error-handler.ts +225 -0
  191. package/src/lib/error-testing-guide.md +258 -0
  192. package/src/lib/errors.ts +285 -0
  193. package/src/lib/performance.ts +70 -0
  194. package/src/lib/telemetry.ts +282 -0
  195. package/src/lsp/client.ts +229 -0
  196. package/src/lsp/index.ts +485 -0
  197. package/src/lsp/language.ts +116 -0
  198. package/src/lsp/server.ts +1895 -0
  199. package/src/mcp/auth.ts +135 -0
  200. package/src/mcp/index.ts +1117 -0
  201. package/src/mcp/intent-analyzer.ts +376 -0
  202. package/src/mcp/oauth-callback.ts +200 -0
  203. package/src/mcp/oauth-provider.ts +154 -0
  204. package/src/patch/index.ts +632 -0
  205. package/src/permission/index.ts +199 -0
  206. package/src/plugin/index.ts +91 -0
  207. package/src/project/bootstrap.ts +33 -0
  208. package/src/project/instance.ts +78 -0
  209. package/src/project/project.ts +236 -0
  210. package/src/project/state.ts +65 -0
  211. package/src/project/vcs.ts +76 -0
  212. package/src/provider/auth.ts +143 -0
  213. package/src/provider/models-macro.ts +55 -0
  214. package/src/provider/models.ts +161 -0
  215. package/src/provider/provider.ts +1109 -0
  216. package/src/provider/sdk/openai-compatible/src/README.md +5 -0
  217. package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
  218. package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
  219. package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
  220. package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
  221. package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
  222. package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
  223. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
  224. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
  225. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
  226. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
  227. package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
  228. package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
  229. package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
  230. package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
  231. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
  232. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
  233. package/src/provider/transform.ts +455 -0
  234. package/src/pty/index.ts +231 -0
  235. package/src/security/guardrails.test.ts +341 -0
  236. package/src/security/guardrails.ts +570 -0
  237. package/src/security/index.ts +19 -0
  238. package/src/server/error.ts +36 -0
  239. package/src/server/project.ts +79 -0
  240. package/src/server/server.ts +2641 -0
  241. package/src/server/tui.ts +71 -0
  242. package/src/session/compaction.ts +228 -0
  243. package/src/session/index.ts +464 -0
  244. package/src/session/llm.ts +201 -0
  245. package/src/session/message-v2.ts +695 -0
  246. package/src/session/message.ts +189 -0
  247. package/src/session/processor.ts +409 -0
  248. package/src/session/prompt/act-switch.txt +5 -0
  249. package/src/session/prompt/anthropic-20250930.txt +166 -0
  250. package/src/session/prompt/anthropic.txt +63 -0
  251. package/src/session/prompt/anthropic_spoof.txt +1 -0
  252. package/src/session/prompt/beast.txt +76 -0
  253. package/src/session/prompt/codex.txt +304 -0
  254. package/src/session/prompt/copilot-gpt-5.txt +137 -0
  255. package/src/session/prompt/gemini.txt +62 -0
  256. package/src/session/prompt/max-steps.txt +16 -0
  257. package/src/session/prompt/plan-reminder-anthropic.txt +35 -0
  258. package/src/session/prompt/plan.txt +24 -0
  259. package/src/session/prompt/polaris.txt +88 -0
  260. package/src/session/prompt/qwen.txt +59 -0
  261. package/src/session/prompt.ts +1552 -0
  262. package/src/session/retry.ts +86 -0
  263. package/src/session/revert.ts +108 -0
  264. package/src/session/sensitive-filter.test.ts +327 -0
  265. package/src/session/sensitive-filter.ts +466 -0
  266. package/src/session/status.ts +76 -0
  267. package/src/session/summary.ts +209 -0
  268. package/src/session/system.ts +122 -0
  269. package/src/session/todo.ts +37 -0
  270. package/src/share/share-next.ts +222 -0
  271. package/src/share/share.ts +87 -0
  272. package/src/shell/shell.ts +67 -0
  273. package/src/skill/index.ts +1 -0
  274. package/src/skill/skill.ts +83 -0
  275. package/src/snapshot/index.ts +197 -0
  276. package/src/storage/storage.ts +226 -0
  277. package/src/tests/agent.test.ts +308 -0
  278. package/src/tests/build-guards.test.ts +267 -0
  279. package/src/tests/config.test.ts +664 -0
  280. package/src/tests/tool-registry.test.ts +589 -0
  281. package/src/tool/bash.ts +314 -0
  282. package/src/tool/bash.txt +158 -0
  283. package/src/tool/batch.ts +175 -0
  284. package/src/tool/batch.txt +24 -0
  285. package/src/tool/codesearch.ts +184 -0
  286. package/src/tool/codesearch.txt +12 -0
  287. package/src/tool/edit.ts +675 -0
  288. package/src/tool/edit.txt +10 -0
  289. package/src/tool/glob.ts +65 -0
  290. package/src/tool/glob.txt +6 -0
  291. package/src/tool/grep.ts +121 -0
  292. package/src/tool/grep.txt +8 -0
  293. package/src/tool/invalid.ts +17 -0
  294. package/src/tool/ls.ts +110 -0
  295. package/src/tool/ls.txt +1 -0
  296. package/src/tool/lsp-diagnostics.ts +26 -0
  297. package/src/tool/lsp-diagnostics.txt +1 -0
  298. package/src/tool/lsp-hover.ts +31 -0
  299. package/src/tool/lsp-hover.txt +1 -0
  300. package/src/tool/lsp.ts +87 -0
  301. package/src/tool/lsp.txt +19 -0
  302. package/src/tool/multiedit.ts +46 -0
  303. package/src/tool/multiedit.txt +41 -0
  304. package/src/tool/patch.ts +233 -0
  305. package/src/tool/patch.txt +1 -0
  306. package/src/tool/read.ts +219 -0
  307. package/src/tool/read.txt +12 -0
  308. package/src/tool/registry.ts +162 -0
  309. package/src/tool/skill.ts +100 -0
  310. package/src/tool/task.ts +136 -0
  311. package/src/tool/task.txt +51 -0
  312. package/src/tool/todo.ts +39 -0
  313. package/src/tool/todoread.txt +14 -0
  314. package/src/tool/todowrite.txt +167 -0
  315. package/src/tool/tool.ts +71 -0
  316. package/src/tool/webfetch.ts +198 -0
  317. package/src/tool/webfetch.txt +13 -0
  318. package/src/tool/websearch.ts +268 -0
  319. package/src/tool/websearch.txt +13 -0
  320. package/src/tool/write.ts +110 -0
  321. package/src/tool/write.txt +8 -0
  322. package/src/util/archive.ts +16 -0
  323. package/src/util/color.ts +19 -0
  324. package/src/util/context.ts +25 -0
  325. package/src/util/defer.ts +12 -0
  326. package/src/util/eventloop.ts +20 -0
  327. package/src/util/filesystem.ts +83 -0
  328. package/src/util/fn.ts +11 -0
  329. package/src/util/iife.ts +3 -0
  330. package/src/util/keybind.ts +102 -0
  331. package/src/util/lazy.ts +11 -0
  332. package/src/util/license.ts +362 -0
  333. package/src/util/locale.ts +81 -0
  334. package/src/util/lock.ts +98 -0
  335. package/src/util/log.ts +180 -0
  336. package/src/util/queue.ts +32 -0
  337. package/src/util/rpc.ts +42 -0
  338. package/src/util/scrap.ts +10 -0
  339. package/src/util/signal.ts +12 -0
  340. package/src/util/timeout.ts +14 -0
  341. package/src/util/token.ts +7 -0
  342. package/src/util/wildcard.ts +54 -0
  343. package/sst-env.d.ts +9 -0
  344. package/test/agent/agent.test.ts +146 -0
  345. package/test/bun.test.ts +53 -0
  346. package/test/cli/cmd/acp.test.ts +144 -0
  347. package/test/cli/cmd/run.test.ts +250 -0
  348. package/test/cli/github-remote.test.ts +80 -0
  349. package/test/config/agent-color.test.ts +66 -0
  350. package/test/config/config.test.ts +536 -0
  351. package/test/config/markdown.test.ts +89 -0
  352. package/test/file/ignore.test.ts +10 -0
  353. package/test/fixture/fixture.ts +37 -0
  354. package/test/fixture/lsp/fake-lsp-server.js +77 -0
  355. package/test/helpers.ts +172 -0
  356. package/test/ide/ide.test.ts +82 -0
  357. package/test/installation/installation.test.ts +143 -0
  358. package/test/keybind.test.ts +421 -0
  359. package/test/lsp/client.test.ts +95 -0
  360. package/test/mcp/headers.test.ts +153 -0
  361. package/test/patch/patch.test.ts +348 -0
  362. package/test/preload.ts +57 -0
  363. package/test/project/project.test.ts +74 -0
  364. package/test/provider/provider.test.ts +74 -0
  365. package/test/provider/transform.test.ts +411 -0
  366. package/test/session/retry.test.ts +111 -0
  367. package/test/session/session.test.ts +71 -0
  368. package/test/skill/skill.test.ts +131 -0
  369. package/test/snapshot/snapshot.test.ts +940 -0
  370. package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
  371. package/test/tool/bash.test.ts +434 -0
  372. package/test/tool/grep.test.ts +108 -0
  373. package/test/tool/patch.test.ts +259 -0
  374. package/test/tool/read.test.ts +42 -0
  375. package/test/util/iife.test.ts +36 -0
  376. package/test/util/lazy.test.ts +50 -0
  377. package/test/util/license.test.ts +235 -0
  378. package/test/util/timeout.test.ts +21 -0
  379. package/test/util/wildcard.test.ts +55 -0
  380. package/tsconfig.json +16 -0
  381. package/update-versions.ps1 +65 -0
@@ -0,0 +1,1087 @@
1
+ import { BoxRenderable, TextareaRenderable, MouseEvent, PasteEvent, t, dim, fg, type KeyBinding } from "@opentui/core"
2
+ import { createEffect, createMemo, type JSX, onMount, createSignal, onCleanup, Show, Switch, Match } from "solid-js"
3
+ import "opentui-spinner/solid"
4
+ import { useLocal } from "@tui/context/local"
5
+ import { useTheme } from "@tui/context/theme"
6
+ import { EmptyBorder } from "@tui/component/border"
7
+ import { useSDK } from "@tui/context/sdk"
8
+ import { useRoute } from "@tui/context/route"
9
+ import { useSync } from "@tui/context/sync"
10
+ import { Identifier } from "@/id/id"
11
+ import { createStore, produce } from "solid-js/store"
12
+ import { useKeybind } from "@tui/context/keybind"
13
+ import { Keybind } from "@/util/keybind"
14
+ import { usePromptHistory, type PromptInfo } from "./history"
15
+ import { usePromptStash } from "./stash"
16
+ import { DialogStash } from "../dialog-stash"
17
+ import { type AutocompleteRef, Autocomplete } from "./autocomplete"
18
+ import { useCommandDialog } from "../dialog-command"
19
+ import { useRenderer } from "@opentui/solid"
20
+ import { Editor } from "@tui/util/editor"
21
+ import { useExit } from "../../context/exit"
22
+ import { Clipboard } from "../../util/clipboard"
23
+ import type { FilePart } from "@opencode-ai/sdk/v2"
24
+ import { TuiEvent } from "../../event"
25
+ import { iife } from "@/util/iife"
26
+ import { Locale } from "@/util/locale"
27
+ import { createColors, createFrames } from "../../ui/spinner.ts"
28
+ import { useDialog } from "@tui/ui/dialog"
29
+ import { DialogProvider as DialogProviderConnect } from "../dialog-provider"
30
+ import { DialogAlert } from "../../ui/dialog-alert"
31
+ import { useToast } from "../../ui/toast"
32
+
33
+ export type PromptProps = {
34
+ sessionID?: string
35
+ disabled?: boolean
36
+ onSubmit?: () => void
37
+ ref?: (ref: PromptRef) => void
38
+ hint?: JSX.Element
39
+ showPlaceholder?: boolean
40
+ }
41
+
42
+ export type PromptRef = {
43
+ focused: boolean
44
+ current: PromptInfo
45
+ set(prompt: PromptInfo): void
46
+ reset(): void
47
+ blur(): void
48
+ focus(): void
49
+ submit(): void
50
+ }
51
+
52
+ const PLACEHOLDERS = ["Book a flight to New York", "Find and message my contacts", "Fill out this form for me"]
53
+
54
+ const TEXTAREA_ACTIONS = [
55
+ "submit",
56
+ "newline",
57
+ "move-left",
58
+ "move-right",
59
+ "move-up",
60
+ "move-down",
61
+ "select-left",
62
+ "select-right",
63
+ "select-up",
64
+ "select-down",
65
+ "line-home",
66
+ "line-end",
67
+ "select-line-home",
68
+ "select-line-end",
69
+ "visual-line-home",
70
+ "visual-line-end",
71
+ "select-visual-line-home",
72
+ "select-visual-line-end",
73
+ "buffer-home",
74
+ "buffer-end",
75
+ "select-buffer-home",
76
+ "select-buffer-end",
77
+ "delete-line",
78
+ "delete-to-line-end",
79
+ "delete-to-line-start",
80
+ "backspace",
81
+ "delete",
82
+ "undo",
83
+ "redo",
84
+ "word-forward",
85
+ "word-backward",
86
+ "select-word-forward",
87
+ "select-word-backward",
88
+ "delete-word-forward",
89
+ "delete-word-backward",
90
+ ] as const
91
+
92
+ function mapTextareaKeybindings(
93
+ keybinds: Record<string, Keybind.Info[]>,
94
+ action: (typeof TEXTAREA_ACTIONS)[number],
95
+ ): KeyBinding[] {
96
+ const configKey = `input_${action.replace(/-/g, "_")}`
97
+ const bindings = keybinds[configKey]
98
+ if (!bindings) return []
99
+ return bindings.map((binding) => ({
100
+ name: binding.name,
101
+ ctrl: binding.ctrl || undefined,
102
+ meta: binding.meta || undefined,
103
+ shift: binding.shift || undefined,
104
+ super: binding.super || undefined,
105
+ action,
106
+ }))
107
+ }
108
+
109
+ export function Prompt(props: PromptProps) {
110
+ let input: TextareaRenderable
111
+ let anchor: BoxRenderable
112
+ let autocomplete: AutocompleteRef
113
+
114
+ const keybind = useKeybind()
115
+ const local = useLocal()
116
+ const sdk = useSDK()
117
+ const route = useRoute()
118
+ const sync = useSync()
119
+ const dialog = useDialog()
120
+ const toast = useToast()
121
+ const status = createMemo(() => sync.data.session_status?.[props.sessionID ?? ""] ?? { type: "idle" })
122
+ const history = usePromptHistory()
123
+ const stash = usePromptStash()
124
+ const command = useCommandDialog()
125
+ const renderer = useRenderer()
126
+ const { theme, syntax } = useTheme()
127
+
128
+ function promptModelWarning() {
129
+ toast.show({
130
+ variant: "warning",
131
+ message: "Connect a provider to send prompts",
132
+ duration: 3000,
133
+ })
134
+ if (sync.data.provider.length === 0) {
135
+ dialog.replace(() => <DialogProviderConnect />)
136
+ }
137
+ }
138
+
139
+ const textareaKeybindings = createMemo(() => {
140
+ const keybinds = keybind.all
141
+
142
+ return [
143
+ { name: "return", action: "submit" },
144
+ { name: "return", meta: true, action: "newline" },
145
+ ...TEXTAREA_ACTIONS.flatMap((action) => mapTextareaKeybindings(keybinds, action)),
146
+ ] satisfies KeyBinding[]
147
+ })
148
+
149
+ const fileStyleId = syntax().getStyleId("extmark.file")!
150
+ const agentStyleId = syntax().getStyleId("extmark.agent")!
151
+ const pasteStyleId = syntax().getStyleId("extmark.paste")!
152
+ let promptPartTypeId: number
153
+
154
+ sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
155
+ input.insertText(evt.properties.text)
156
+ setTimeout(() => {
157
+ input.getLayoutNode().markDirty()
158
+ input.gotoBufferEnd()
159
+ renderer.requestRender()
160
+ }, 0)
161
+ })
162
+
163
+ createEffect(() => {
164
+ if (props.disabled) input.cursorColor = theme.backgroundElement
165
+ if (!props.disabled) input.cursorColor = theme.text
166
+ })
167
+
168
+ const [store, setStore] = createStore<{
169
+ prompt: PromptInfo
170
+ mode: "normal" | "shell"
171
+ extmarkToPartIndex: Map<number, number>
172
+ interrupt: number
173
+ placeholder: number
174
+ }>({
175
+ placeholder: Math.floor(Math.random() * PLACEHOLDERS.length),
176
+ prompt: {
177
+ input: "",
178
+ parts: [],
179
+ },
180
+ mode: "normal",
181
+ extmarkToPartIndex: new Map(),
182
+ interrupt: 0,
183
+ })
184
+
185
+ command.register(() => {
186
+ return [
187
+ {
188
+ title: "Clear prompt",
189
+ value: "prompt.clear",
190
+ category: "Prompt",
191
+ disabled: true,
192
+ onSelect: (dialog) => {
193
+ input.extmarks.clear()
194
+ input.clear()
195
+ dialog.clear()
196
+ },
197
+ },
198
+ {
199
+ title: "Submit prompt",
200
+ value: "prompt.submit",
201
+ disabled: true,
202
+ keybind: "input_submit",
203
+ category: "Prompt",
204
+ onSelect: (dialog) => {
205
+ if (!input.focused) return
206
+ submit()
207
+ dialog.clear()
208
+ },
209
+ },
210
+ {
211
+ title: "Paste",
212
+ value: "prompt.paste",
213
+ disabled: true,
214
+ keybind: "input_paste",
215
+ category: "Prompt",
216
+ onSelect: async () => {
217
+ const content = await Clipboard.read()
218
+ if (content?.mime.startsWith("image/")) {
219
+ await pasteImage({
220
+ filename: "clipboard",
221
+ mime: content.mime,
222
+ content: content.data,
223
+ })
224
+ }
225
+ },
226
+ },
227
+ {
228
+ title: "Interrupt session",
229
+ value: "session.interrupt",
230
+ keybind: "session_interrupt",
231
+ disabled: status().type === "idle",
232
+ category: "Session",
233
+ onSelect: (dialog) => {
234
+ if (autocomplete.visible) return
235
+ if (!input.focused) return
236
+ // TODO: this should be its own command
237
+ if (store.mode === "shell") {
238
+ setStore("mode", "normal")
239
+ return
240
+ }
241
+ if (!props.sessionID) return
242
+
243
+ setStore("interrupt", store.interrupt + 1)
244
+
245
+ setTimeout(() => {
246
+ setStore("interrupt", 0)
247
+ }, 5000)
248
+
249
+ if (store.interrupt >= 2) {
250
+ sdk.client.session.abort({
251
+ sessionID: props.sessionID,
252
+ })
253
+ setStore("interrupt", 0)
254
+ }
255
+ dialog.clear()
256
+ },
257
+ },
258
+ {
259
+ title: "Open editor",
260
+ category: "Session",
261
+ keybind: "editor_open",
262
+ value: "prompt.editor",
263
+ onSelect: async (dialog, trigger) => {
264
+ dialog.clear()
265
+
266
+ // replace summarized text parts with the actual text
267
+ const text = store.prompt.parts
268
+ .filter((p) => p.type === "text")
269
+ .reduce((acc, p) => {
270
+ if (!p.source) return acc
271
+ return acc.replace(p.source.text.value, p.text)
272
+ }, store.prompt.input)
273
+
274
+ const nonTextParts = store.prompt.parts.filter((p) => p.type !== "text")
275
+
276
+ const value = trigger === "prompt" ? "" : text
277
+ const content = await Editor.open({ value, renderer })
278
+ if (!content) return
279
+
280
+ input.setText(content)
281
+
282
+ // Update positions for nonTextParts based on their location in new content
283
+ // Filter out parts whose virtual text was deleted
284
+ // this handles a case where the user edits the text in the editor
285
+ // such that the virtual text moves around or is deleted
286
+ const updatedNonTextParts = nonTextParts
287
+ .map((part) => {
288
+ let virtualText = ""
289
+ if (part.type === "file" && part.source?.text) {
290
+ virtualText = part.source.text.value
291
+ } else if (part.type === "agent" && part.source) {
292
+ virtualText = part.source.value
293
+ }
294
+
295
+ if (!virtualText) return part
296
+
297
+ const newStart = content.indexOf(virtualText)
298
+ // if the virtual text is deleted, remove the part
299
+ if (newStart === -1) return null
300
+
301
+ const newEnd = newStart + virtualText.length
302
+
303
+ if (part.type === "file" && part.source?.text) {
304
+ return {
305
+ ...part,
306
+ source: {
307
+ ...part.source,
308
+ text: {
309
+ ...part.source.text,
310
+ start: newStart,
311
+ end: newEnd,
312
+ },
313
+ },
314
+ }
315
+ }
316
+
317
+ if (part.type === "agent" && part.source) {
318
+ return {
319
+ ...part,
320
+ source: {
321
+ ...part.source,
322
+ start: newStart,
323
+ end: newEnd,
324
+ },
325
+ }
326
+ }
327
+
328
+ return part
329
+ })
330
+ .filter((part) => part !== null)
331
+
332
+ setStore("prompt", {
333
+ input: content,
334
+ // keep only the non-text parts because the text parts were
335
+ // already expanded inline
336
+ parts: updatedNonTextParts,
337
+ })
338
+ restoreExtmarksFromParts(updatedNonTextParts)
339
+ input.cursorOffset = Bun.stringWidth(content)
340
+ },
341
+ },
342
+ ]
343
+ })
344
+
345
+ createEffect(() => {
346
+ input.focus()
347
+ })
348
+
349
+ onMount(() => {
350
+ promptPartTypeId = input.extmarks.registerType("prompt-part")
351
+ })
352
+
353
+ function restoreExtmarksFromParts(parts: PromptInfo["parts"]) {
354
+ input.extmarks.clear()
355
+ setStore("extmarkToPartIndex", new Map())
356
+
357
+ parts.forEach((part, partIndex) => {
358
+ let start = 0
359
+ let end = 0
360
+ let virtualText = ""
361
+ let styleId: number | undefined
362
+
363
+ if (part.type === "file" && part.source?.text) {
364
+ start = part.source.text.start
365
+ end = part.source.text.end
366
+ virtualText = part.source.text.value
367
+ styleId = fileStyleId
368
+ } else if (part.type === "agent" && part.source) {
369
+ start = part.source.start
370
+ end = part.source.end
371
+ virtualText = part.source.value
372
+ styleId = agentStyleId
373
+ } else if (part.type === "text" && part.source?.text) {
374
+ start = part.source.text.start
375
+ end = part.source.text.end
376
+ virtualText = part.source.text.value
377
+ styleId = pasteStyleId
378
+ }
379
+
380
+ if (virtualText) {
381
+ const extmarkId = input.extmarks.create({
382
+ start,
383
+ end,
384
+ virtual: true,
385
+ styleId,
386
+ typeId: promptPartTypeId,
387
+ })
388
+ setStore("extmarkToPartIndex", (map: Map<number, number>) => {
389
+ const newMap = new Map(map)
390
+ newMap.set(extmarkId, partIndex)
391
+ return newMap
392
+ })
393
+ }
394
+ })
395
+ }
396
+
397
+ function syncExtmarksWithPromptParts() {
398
+ const allExtmarks = input.extmarks.getAllForTypeId(promptPartTypeId)
399
+ setStore(
400
+ produce((draft) => {
401
+ const newMap = new Map<number, number>()
402
+ const newParts: typeof draft.prompt.parts = []
403
+
404
+ for (const extmark of allExtmarks) {
405
+ const partIndex = draft.extmarkToPartIndex.get(extmark.id)
406
+ if (partIndex !== undefined) {
407
+ const part = draft.prompt.parts[partIndex]
408
+ if (part) {
409
+ if (part.type === "agent" && part.source) {
410
+ part.source.start = extmark.start
411
+ part.source.end = extmark.end
412
+ } else if (part.type === "file" && part.source?.text) {
413
+ part.source.text.start = extmark.start
414
+ part.source.text.end = extmark.end
415
+ } else if (part.type === "text" && part.source?.text) {
416
+ part.source.text.start = extmark.start
417
+ part.source.text.end = extmark.end
418
+ }
419
+ newMap.set(extmark.id, newParts.length)
420
+ newParts.push(part)
421
+ }
422
+ }
423
+ }
424
+
425
+ draft.extmarkToPartIndex = newMap
426
+ draft.prompt.parts = newParts
427
+ }),
428
+ )
429
+ }
430
+
431
+ command.register(() => [
432
+ {
433
+ title: "Stash prompt",
434
+ value: "prompt.stash",
435
+ category: "Prompt",
436
+ disabled: !store.prompt.input,
437
+ onSelect: (dialog) => {
438
+ if (!store.prompt.input) return
439
+ stash.push({
440
+ input: store.prompt.input,
441
+ parts: store.prompt.parts,
442
+ })
443
+ input.extmarks.clear()
444
+ input.clear()
445
+ setStore("prompt", { input: "", parts: [] })
446
+ setStore("extmarkToPartIndex", new Map())
447
+ dialog.clear()
448
+ },
449
+ },
450
+ {
451
+ title: "Stash pop",
452
+ value: "prompt.stash.pop",
453
+ category: "Prompt",
454
+ disabled: stash.list().length === 0,
455
+ onSelect: (dialog) => {
456
+ const entry = stash.pop()
457
+ if (entry) {
458
+ input.setText(entry.input)
459
+ setStore("prompt", { input: entry.input, parts: entry.parts })
460
+ restoreExtmarksFromParts(entry.parts)
461
+ input.gotoBufferEnd()
462
+ }
463
+ dialog.clear()
464
+ },
465
+ },
466
+ {
467
+ title: "Stash list",
468
+ value: "prompt.stash.list",
469
+ category: "Prompt",
470
+ disabled: stash.list().length === 0,
471
+ onSelect: (dialog) => {
472
+ dialog.replace(() => (
473
+ <DialogStash
474
+ onSelect={(entry) => {
475
+ input.setText(entry.input)
476
+ setStore("prompt", { input: entry.input, parts: entry.parts })
477
+ restoreExtmarksFromParts(entry.parts)
478
+ input.gotoBufferEnd()
479
+ }}
480
+ />
481
+ ))
482
+ },
483
+ },
484
+ ])
485
+
486
+ props.ref?.({
487
+ get focused() {
488
+ return input.focused
489
+ },
490
+ get current() {
491
+ return store.prompt
492
+ },
493
+ focus() {
494
+ input.focus()
495
+ },
496
+ blur() {
497
+ input.blur()
498
+ },
499
+ set(prompt) {
500
+ input.setText(prompt.input)
501
+ setStore("prompt", prompt)
502
+ restoreExtmarksFromParts(prompt.parts)
503
+ input.gotoBufferEnd()
504
+ },
505
+ reset() {
506
+ input.clear()
507
+ input.extmarks.clear()
508
+ setStore("prompt", {
509
+ input: "",
510
+ parts: [],
511
+ })
512
+ setStore("extmarkToPartIndex", new Map())
513
+ },
514
+ submit() {
515
+ submit()
516
+ },
517
+ })
518
+
519
+ async function submit() {
520
+ if (props.disabled) return
521
+ if (autocomplete?.visible) return
522
+ if (!store.prompt.input) return
523
+ const trimmed = store.prompt.input.trim()
524
+ if (trimmed === "exit" || trimmed === "quit" || trimmed === ":q") {
525
+ exit()
526
+ return
527
+ }
528
+ const selectedModel = local.model.current()
529
+ if (!selectedModel) {
530
+ promptModelWarning()
531
+ return
532
+ }
533
+ const sessionID = props.sessionID
534
+ ? props.sessionID
535
+ : await (async () => {
536
+ const sessionID = await sdk.client.session.create({}).then((x) => x.data!.id)
537
+ return sessionID
538
+ })()
539
+ const messageID = Identifier.ascending("message")
540
+ let inputText = store.prompt.input
541
+
542
+ // Expand pasted text inline before submitting
543
+ const allExtmarks = input.extmarks.getAllForTypeId(promptPartTypeId)
544
+ const sortedExtmarks = allExtmarks.sort((a: { start: number }, b: { start: number }) => b.start - a.start)
545
+
546
+ for (const extmark of sortedExtmarks) {
547
+ const partIndex = store.extmarkToPartIndex.get(extmark.id)
548
+ if (partIndex !== undefined) {
549
+ const part = store.prompt.parts[partIndex]
550
+ if (part?.type === "text" && part.text) {
551
+ const before = inputText.slice(0, extmark.start)
552
+ const after = inputText.slice(extmark.end)
553
+ inputText = before + part.text + after
554
+ }
555
+ }
556
+ }
557
+
558
+ // Filter out text parts (pasted content) since they're now expanded inline
559
+ const nonTextParts = store.prompt.parts.filter((part) => part.type !== "text")
560
+
561
+ // Capture mode before it gets reset
562
+ const currentMode = store.mode
563
+
564
+ if (store.mode === "shell") {
565
+ sdk.client.session.shell({
566
+ sessionID,
567
+ agent: local.agent.current().name,
568
+ model: {
569
+ providerID: selectedModel.providerID,
570
+ modelID: selectedModel.modelID,
571
+ },
572
+ command: inputText,
573
+ })
574
+ setStore("mode", "normal")
575
+ } else if (
576
+ inputText.startsWith("/") &&
577
+ iife(() => {
578
+ const command = inputText.split(" ")[0].slice(1)
579
+ console.log(command)
580
+ return sync.data.command.some((x) => x.name === command)
581
+ })
582
+ ) {
583
+ let [command, ...args] = inputText.split(" ")
584
+ sdk.client.session.command({
585
+ sessionID,
586
+ command: command.slice(1),
587
+ arguments: args.join(" "),
588
+ agent: local.agent.current().name,
589
+ model: `${selectedModel.providerID}/${selectedModel.modelID}`,
590
+ messageID,
591
+ })
592
+ } else {
593
+ sdk.client.session.prompt({
594
+ sessionID,
595
+ ...selectedModel,
596
+ messageID,
597
+ agent: local.agent.current().name,
598
+ model: selectedModel,
599
+ parts: [
600
+ {
601
+ id: Identifier.ascending("part"),
602
+ type: "text",
603
+ text: inputText,
604
+ },
605
+ ...nonTextParts.map((x) => ({
606
+ id: Identifier.ascending("part"),
607
+ ...x,
608
+ })),
609
+ ],
610
+ })
611
+ }
612
+ history.append({
613
+ ...store.prompt,
614
+ mode: currentMode,
615
+ })
616
+ input.extmarks.clear()
617
+ setStore("prompt", {
618
+ input: "",
619
+ parts: [],
620
+ })
621
+ setStore("extmarkToPartIndex", new Map())
622
+ props.onSubmit?.()
623
+
624
+ // temporary hack to make sure the message is sent
625
+ if (!props.sessionID)
626
+ setTimeout(() => {
627
+ route.navigate({
628
+ type: "session",
629
+ sessionID,
630
+ })
631
+ }, 50)
632
+ input.clear()
633
+ }
634
+ const exit = useExit()
635
+
636
+ function pasteText(text: string, virtualText: string) {
637
+ const currentOffset = input.visualCursor.offset
638
+ const extmarkStart = currentOffset
639
+ const extmarkEnd = extmarkStart + virtualText.length
640
+
641
+ input.insertText(virtualText + " ")
642
+
643
+ const extmarkId = input.extmarks.create({
644
+ start: extmarkStart,
645
+ end: extmarkEnd,
646
+ virtual: true,
647
+ styleId: pasteStyleId,
648
+ typeId: promptPartTypeId,
649
+ })
650
+
651
+ setStore(
652
+ produce((draft) => {
653
+ const partIndex = draft.prompt.parts.length
654
+ draft.prompt.parts.push({
655
+ type: "text" as const,
656
+ text,
657
+ source: {
658
+ text: {
659
+ start: extmarkStart,
660
+ end: extmarkEnd,
661
+ value: virtualText,
662
+ },
663
+ },
664
+ })
665
+ draft.extmarkToPartIndex.set(extmarkId, partIndex)
666
+ }),
667
+ )
668
+ }
669
+
670
+ async function pasteImage(file: { filename?: string; content: string; mime: string }) {
671
+ const currentOffset = input.visualCursor.offset
672
+ const extmarkStart = currentOffset
673
+ const count = store.prompt.parts.filter((x) => x.type === "file").length
674
+ const virtualText = `[Image ${count + 1}]`
675
+ const extmarkEnd = extmarkStart + virtualText.length
676
+ const textToInsert = virtualText + " "
677
+
678
+ input.insertText(textToInsert)
679
+
680
+ const extmarkId = input.extmarks.create({
681
+ start: extmarkStart,
682
+ end: extmarkEnd,
683
+ virtual: true,
684
+ styleId: pasteStyleId,
685
+ typeId: promptPartTypeId,
686
+ })
687
+
688
+ const part: Omit<FilePart, "id" | "messageID" | "sessionID"> = {
689
+ type: "file" as const,
690
+ mime: file.mime,
691
+ filename: file.filename,
692
+ url: `data:${file.mime};base64,${file.content}`,
693
+ source: {
694
+ type: "file",
695
+ path: file.filename ?? "",
696
+ text: {
697
+ start: extmarkStart,
698
+ end: extmarkEnd,
699
+ value: virtualText,
700
+ },
701
+ },
702
+ }
703
+ setStore(
704
+ produce((draft) => {
705
+ const partIndex = draft.prompt.parts.length
706
+ draft.prompt.parts.push(part)
707
+ draft.extmarkToPartIndex.set(extmarkId, partIndex)
708
+ }),
709
+ )
710
+ return
711
+ }
712
+
713
+ const highlight = createMemo(() => {
714
+ if (keybind.leader) return theme.border
715
+ if (store.mode === "shell") return theme.primary
716
+ return local.agent.color(local.agent.current().name)
717
+ })
718
+
719
+ const spinnerDef = createMemo(() => {
720
+ const color = local.agent.color(local.agent.current().name)
721
+ return {
722
+ frames: createFrames({
723
+ color,
724
+ style: "blocks",
725
+ inactiveFactor: 0.6,
726
+ // enableFading: false,
727
+ minAlpha: 0.3,
728
+ }),
729
+ color: createColors({
730
+ color,
731
+ style: "blocks",
732
+ inactiveFactor: 0.6,
733
+ // enableFading: false,
734
+ minAlpha: 0.3,
735
+ }),
736
+ }
737
+ })
738
+
739
+ return (
740
+ <>
741
+ <Autocomplete
742
+ sessionID={props.sessionID}
743
+ ref={(r) => (autocomplete = r)}
744
+ anchor={() => anchor}
745
+ input={() => input}
746
+ setPrompt={(cb) => {
747
+ setStore("prompt", produce(cb))
748
+ }}
749
+ setExtmark={(partIndex, extmarkId) => {
750
+ setStore("extmarkToPartIndex", (map: Map<number, number>) => {
751
+ const newMap = new Map(map)
752
+ newMap.set(extmarkId, partIndex)
753
+ return newMap
754
+ })
755
+ }}
756
+ value={store.prompt.input}
757
+ fileStyleId={fileStyleId}
758
+ agentStyleId={agentStyleId}
759
+ promptPartTypeId={() => promptPartTypeId}
760
+ />
761
+ <box ref={(r) => (anchor = r)}>
762
+ <box
763
+ border={["left"]}
764
+ borderColor={highlight()}
765
+ customBorderChars={{
766
+ ...EmptyBorder,
767
+ vertical: "|",
768
+ bottomLeft: "_", }}
769
+ >
770
+ <box
771
+ paddingLeft={2}
772
+ paddingRight={1}
773
+ paddingTop={1}
774
+ flexShrink={0}
775
+ backgroundColor={theme.backgroundElement}
776
+ flexGrow={1}
777
+ >
778
+ <textarea
779
+ placeholder={props.sessionID ? undefined : `Ask anything... "${PLACEHOLDERS[store.placeholder]}"`}
780
+ textColor={keybind.leader ? theme.textMuted : theme.text}
781
+ focusedTextColor={keybind.leader ? theme.textMuted : theme.text}
782
+ minHeight={1}
783
+ maxHeight={6}
784
+ onContentChange={() => {
785
+ const value = input.plainText
786
+ setStore("prompt", "input", value)
787
+ autocomplete.onInput(value)
788
+ syncExtmarksWithPromptParts()
789
+ }}
790
+ keyBindings={textareaKeybindings()}
791
+ onKeyDown={async (e) => {
792
+ if (props.disabled) {
793
+ e.preventDefault()
794
+ return
795
+ }
796
+ // Handle clipboard paste (Ctrl+V) - check for images first on Windows
797
+ // This is needed because Windows terminal doesn't properly send image data
798
+ // through bracketed paste, so we need to intercept the keypress and
799
+ // directly read from clipboard before the terminal handles it
800
+ if (keybind.match("input_paste", e)) {
801
+ const content = await Clipboard.read()
802
+ if (content?.mime.startsWith("image/")) {
803
+ e.preventDefault()
804
+ await pasteImage({
805
+ filename: "clipboard",
806
+ mime: content.mime,
807
+ content: content.data,
808
+ })
809
+ return
810
+ }
811
+ // If no image, let the default paste behavior continue
812
+ }
813
+ if (keybind.match("input_clear", e) && store.prompt.input !== "") {
814
+ input.clear()
815
+ input.extmarks.clear()
816
+ setStore("prompt", {
817
+ input: "",
818
+ parts: [],
819
+ })
820
+ setStore("extmarkToPartIndex", new Map())
821
+ return
822
+ }
823
+ if (keybind.match("app_exit", e)) {
824
+ if (store.prompt.input === "") {
825
+ await exit()
826
+ // Don't preventDefault - let textarea potentially handle the event
827
+ e.preventDefault()
828
+ return
829
+ }
830
+ }
831
+ if (e.name === "!" && input.visualCursor.offset === 0) {
832
+ setStore("mode", "shell")
833
+ e.preventDefault()
834
+ return
835
+ }
836
+ if (store.mode === "shell") {
837
+ if ((e.name === "backspace" && input.visualCursor.offset === 0) || e.name === "escape") {
838
+ setStore("mode", "normal")
839
+ e.preventDefault()
840
+ return
841
+ }
842
+ }
843
+ if (store.mode === "normal") autocomplete.onKeyDown(e)
844
+ if (!autocomplete.visible) {
845
+ if (
846
+ (keybind.match("history_previous", e) && input.cursorOffset === 0) ||
847
+ (keybind.match("history_next", e) && input.cursorOffset === input.plainText.length)
848
+ ) {
849
+ const direction = keybind.match("history_previous", e) ? -1 : 1
850
+ const item = history.move(direction, input.plainText)
851
+
852
+ if (item) {
853
+ input.setText(item.input)
854
+ setStore("prompt", item)
855
+ setStore("mode", item.mode ?? "normal")
856
+ restoreExtmarksFromParts(item.parts)
857
+ e.preventDefault()
858
+ if (direction === -1) input.cursorOffset = 0
859
+ if (direction === 1) input.cursorOffset = input.plainText.length
860
+ }
861
+ return
862
+ }
863
+
864
+ if (keybind.match("history_previous", e) && input.visualCursor.visualRow === 0) input.cursorOffset = 0
865
+ if (keybind.match("history_next", e) && input.visualCursor.visualRow === input.height - 1)
866
+ input.cursorOffset = input.plainText.length
867
+ }
868
+ }}
869
+ onSubmit={submit}
870
+ onPaste={async (event: PasteEvent) => {
871
+ if (props.disabled) {
872
+ event.preventDefault()
873
+ return
874
+ }
875
+
876
+ // Normalize line endings at the boundary
877
+ // Windows ConPTY/Terminal often sends CR-only newlines in bracketed paste
878
+ // Replace CRLF first, then any remaining CR
879
+ const normalizedText = event.text.replace(/\r\n/g, "\n").replace(/\r/g, "\n")
880
+ const pastedContent = normalizedText.trim()
881
+ if (!pastedContent) {
882
+ command.trigger("prompt.paste")
883
+ return
884
+ }
885
+
886
+ // trim ' from the beginning and end of the pasted content. just
887
+ // ' and nothing else
888
+ const filepath = pastedContent.replace(/^'+|'+$/g, "").replace(/\\ /g, " ")
889
+ const isUrl = /^(https?):\/\//.test(filepath)
890
+ if (!isUrl) {
891
+ try {
892
+ const file = Bun.file(filepath)
893
+ // Handle SVG as raw text content, not as base64 image
894
+ if (file.type === "image/svg+xml") {
895
+ event.preventDefault()
896
+ const content = await file.text().catch(() => {})
897
+ if (content) {
898
+ pasteText(content, `[SVG: ${file.name ?? "image"}]`)
899
+ return
900
+ }
901
+ }
902
+ if (file.type.startsWith("image/")) {
903
+ event.preventDefault()
904
+ const content = await file
905
+ .arrayBuffer()
906
+ .then((buffer) => Buffer.from(buffer).toString("base64"))
907
+ .catch(() => {})
908
+ if (content) {
909
+ await pasteImage({
910
+ filename: file.name,
911
+ mime: file.type,
912
+ content,
913
+ })
914
+ return
915
+ }
916
+ }
917
+ } catch {}
918
+ }
919
+
920
+ const lineCount = (pastedContent.match(/\n/g)?.length ?? 0) + 1
921
+ if (
922
+ (lineCount >= 3 || pastedContent.length > 150) &&
923
+ !sync.data.config.experimental?.disable_paste_summary
924
+ ) {
925
+ event.preventDefault()
926
+ pasteText(pastedContent, `[Pasted ~${lineCount} lines]`)
927
+ return
928
+ }
929
+
930
+ // Force layout update and render for the pasted content
931
+ setTimeout(() => {
932
+ input.getLayoutNode().markDirty()
933
+ input.gotoBufferEnd()
934
+ renderer.requestRender()
935
+ }, 0)
936
+ }}
937
+ ref={(r: TextareaRenderable) => {
938
+ input = r
939
+ setTimeout(() => {
940
+ input.cursorColor = theme.text
941
+ }, 0)
942
+ }}
943
+ onMouseDown={(r: MouseEvent) => r.target?.focus()}
944
+ focusedBackgroundColor={theme.backgroundElement}
945
+ cursorColor={theme.text}
946
+ syntaxStyle={syntax()}
947
+ />
948
+ <box flexDirection="row" flexShrink={0} paddingTop={1} gap={1}>
949
+ <text fg={highlight()}>
950
+ {store.mode === "shell" ? "Shell" : Locale.titlecase(local.agent.current().name)}{" "}
951
+ </text>
952
+ <Show when={store.mode === "normal"}>
953
+ <box flexDirection="row" gap={1}>
954
+ <text flexShrink={0} fg={keybind.leader ? theme.textMuted : theme.text}>
955
+ {local.model.parsed().model}
956
+ </text>
957
+ <text fg={theme.textMuted}>{local.model.parsed().provider}</text>
958
+ </box>
959
+ </Show>
960
+ </box>
961
+ </box>
962
+ </box>
963
+ <box
964
+ height={1}
965
+ border={["left"]}
966
+ borderColor={highlight()}
967
+ customBorderChars={{
968
+ ...EmptyBorder,
969
+ vertical: theme.backgroundElement.a !== 0 ? "_" : " ",
970
+ }}
971
+ >
972
+ <box
973
+ height={1}
974
+ border={["bottom"]}
975
+ borderColor={theme.backgroundElement}
976
+ customBorderChars={
977
+ theme.backgroundElement.a !== 0
978
+ ? {
979
+ ...EmptyBorder,
980
+ horizontal: "^",
981
+ }
982
+ : {
983
+ ...EmptyBorder,
984
+ horizontal: " ",
985
+ }
986
+ }
987
+ />
988
+ </box>
989
+ <box flexDirection="row" justifyContent="space-between">
990
+ <Show when={status().type !== "idle"} fallback={<text />}>
991
+ <box
992
+ flexDirection="row"
993
+ gap={1}
994
+ flexGrow={1}
995
+ justifyContent={status().type === "retry" ? "space-between" : "flex-start"}
996
+ >
997
+ <box flexShrink={0} flexDirection="row" gap={1}>
998
+ {/* @ts-ignore // SpinnerOptions doesn't support marginLeft */}
999
+ <spinner marginLeft={1} color={spinnerDef().color} frames={spinnerDef().frames} interval={40} />
1000
+ <box flexDirection="row" gap={1} flexShrink={0}>
1001
+ {(() => {
1002
+ const retry = createMemo(() => {
1003
+ const s = status()
1004
+ if (s.type !== "retry") return
1005
+ return s
1006
+ })
1007
+ const message = createMemo(() => {
1008
+ const r = retry()
1009
+ if (!r) return
1010
+ if (r.message.length > 80) return r.message.slice(0, 80) + "..."
1011
+ return r.message
1012
+ })
1013
+ const isTruncated = createMemo(() => {
1014
+ const r = retry()
1015
+ if (!r) return false
1016
+ return r.message.length > 120
1017
+ })
1018
+ const [seconds, setSeconds] = createSignal(0)
1019
+ onMount(() => {
1020
+ const timer = setInterval(() => {
1021
+ const next = retry()?.next
1022
+ if (next) setSeconds(Math.round((next - Date.now()) / 1000))
1023
+ }, 1000)
1024
+
1025
+ onCleanup(() => {
1026
+ clearInterval(timer)
1027
+ })
1028
+ })
1029
+ const handleMessageClick = () => {
1030
+ const r = retry()
1031
+ if (!r) return
1032
+ if (isTruncated()) {
1033
+ DialogAlert.show(dialog, "Retry Error", r.message)
1034
+ }
1035
+ }
1036
+
1037
+ const retryText = () => {
1038
+ const r = retry()
1039
+ if (!r) return ""
1040
+ const baseMessage = message()
1041
+ const truncatedHint = isTruncated() ? " (click to expand)" : ""
1042
+ const retryInfo = ` [retrying ${seconds() > 0 ? `in ${seconds()}s ` : ""}attempt #${r.attempt}]`
1043
+ return baseMessage + truncatedHint + retryInfo
1044
+ }
1045
+
1046
+ return (
1047
+ <Show when={retry()}>
1048
+ <box onMouseUp={handleMessageClick}>
1049
+ <text fg={theme.error}>{retryText()}</text>
1050
+ </box>
1051
+ </Show>
1052
+ )
1053
+ })()}
1054
+ </box>
1055
+ </box>
1056
+ <text fg={store.interrupt > 0 ? theme.primary : theme.text}>
1057
+ esc{" "}
1058
+ <span style={{ fg: store.interrupt > 0 ? theme.primary : theme.textMuted }}>
1059
+ {store.interrupt > 0 ? "again to interrupt" : "interrupt"}
1060
+ </span>
1061
+ </text>
1062
+ </box>
1063
+ </Show>
1064
+ <Show when={status().type !== "retry"}>
1065
+ <box gap={2} flexDirection="row">
1066
+ <Switch>
1067
+ <Match when={store.mode === "normal"}>
1068
+ <text fg={theme.text}>
1069
+ {keybind.print("agent_cycle")} <span style={{ fg: theme.textMuted }}>switch agent</span>
1070
+ </text>
1071
+ <text fg={theme.text}>
1072
+ {keybind.print("command_list")} <span style={{ fg: theme.textMuted }}>commands</span>
1073
+ </text>
1074
+ </Match>
1075
+ <Match when={store.mode === "shell"}>
1076
+ <text fg={theme.text}>
1077
+ esc <span style={{ fg: theme.textMuted }}>exit shell mode</span>
1078
+ </text>
1079
+ </Match>
1080
+ </Switch>
1081
+ </box>
1082
+ </Show>
1083
+ </box>
1084
+ </box>
1085
+ </>
1086
+ )
1087
+ }