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