@uncarved/jade 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (436) hide show
  1. package/bin/jade.js +5 -4
  2. package/dist/api.d.ts +266 -0
  3. package/dist/api.js +38 -0
  4. package/dist/api.js.map +1 -0
  5. package/dist/config.d.ts +13 -32
  6. package/dist/config.js +39 -121
  7. package/dist/config.js.map +1 -1
  8. package/dist/fetch.d.ts +20 -0
  9. package/dist/fetch.js +190 -0
  10. package/dist/fetch.js.map +1 -0
  11. package/dist/hash.d.ts +2 -0
  12. package/dist/hash.js +42 -0
  13. package/dist/hash.js.map +1 -0
  14. package/dist/host.d.ts +71 -0
  15. package/dist/host.js +400 -0
  16. package/dist/host.js.map +1 -0
  17. package/dist/index.d.ts +11 -28
  18. package/dist/index.js +11 -28
  19. package/dist/index.js.map +1 -1
  20. package/dist/install.d.ts +78 -0
  21. package/dist/install.js +292 -0
  22. package/dist/install.js.map +1 -0
  23. package/dist/main.d.ts +1 -2
  24. package/dist/main.js +156 -1223
  25. package/dist/main.js.map +1 -1
  26. package/dist/manifest.d.ts +23 -0
  27. package/dist/manifest.js +159 -0
  28. package/dist/manifest.js.map +1 -0
  29. package/dist/membrane.d.ts +33 -0
  30. package/dist/membrane.js +140 -0
  31. package/dist/membrane.js.map +1 -0
  32. package/dist/remote.d.ts +10 -0
  33. package/dist/remote.js +292 -0
  34. package/dist/remote.js.map +1 -0
  35. package/dist/rescue.d.ts +10 -0
  36. package/dist/rescue.js +216 -0
  37. package/dist/rescue.js.map +1 -0
  38. package/dist/resolve.d.ts +34 -0
  39. package/dist/resolve.js +178 -0
  40. package/dist/resolve.js.map +1 -0
  41. package/dist/rpc.d.ts +26 -0
  42. package/dist/rpc.js +144 -0
  43. package/dist/rpc.js.map +1 -0
  44. package/dist/secrets.d.ts +12 -0
  45. package/dist/secrets.js +125 -0
  46. package/dist/secrets.js.map +1 -0
  47. package/dist/session.d.ts +45 -0
  48. package/dist/session.js +401 -0
  49. package/dist/session.js.map +1 -0
  50. package/dist/source.d.ts +12 -0
  51. package/dist/source.js +72 -0
  52. package/dist/source.js.map +1 -0
  53. package/dist/state.d.ts +37 -0
  54. package/dist/state.js +93 -0
  55. package/dist/state.js.map +1 -0
  56. package/dist/update.d.ts +55 -0
  57. package/dist/update.js +258 -0
  58. package/dist/update.js.map +1 -0
  59. package/package.json +12 -9
  60. package/plugins/approval/index.ts +297 -0
  61. package/plugins/approval/plugin.json +18 -0
  62. package/{src/plugins → plugins/approval}/policy.ts +21 -84
  63. package/plugins/approval/review.ts +192 -0
  64. package/plugins/ask/index.ts +159 -0
  65. package/plugins/ask/plugin.json +8 -0
  66. package/plugins/budget/index.ts +38 -0
  67. package/plugins/budget/plugin.json +13 -0
  68. package/{src/plugins → plugins/checks}/checks.ts +68 -39
  69. package/plugins/checks/index.ts +158 -0
  70. package/plugins/checks/plugin.json +14 -0
  71. package/plugins/checks/project.ts +81 -0
  72. package/plugins/commands/index.ts +76 -0
  73. package/plugins/commands/plugin.json +7 -0
  74. package/{src/plugins → plugins/compact}/compact.ts +112 -38
  75. package/plugins/compact/index.ts +115 -0
  76. package/plugins/compact/plugin.json +17 -0
  77. package/plugins/compact/style.ts +17 -0
  78. package/plugins/continue/index.ts +58 -0
  79. package/plugins/continue/plugin.json +13 -0
  80. package/{src/ui → plugins/fs}/diff.ts +3 -4
  81. package/{src/tools → plugins/fs}/fs.ts +64 -46
  82. package/{src/tools → plugins/fs}/grep.ts +4 -3
  83. package/plugins/fs/index.ts +34 -0
  84. package/plugins/fs/plugin.json +7 -0
  85. package/plugins/fs/renderers.ts +138 -0
  86. package/plugins/fs/style.ts +18 -0
  87. package/plugins/fs/sys.ts +81 -0
  88. package/plugins/keys/index.ts +53 -0
  89. package/plugins/keys/plugin.json +7 -0
  90. package/plugins/mcp/index.ts +192 -0
  91. package/{src → plugins/mcp}/mcp.ts +32 -32
  92. package/plugins/mcp/plugin.json +7 -0
  93. package/plugins/mcp/project.ts +93 -0
  94. package/plugins/memory/index.ts +105 -0
  95. package/{src → plugins/memory}/memory.ts +20 -2
  96. package/plugins/memory/notes.ts +43 -0
  97. package/plugins/memory/plugin.json +7 -0
  98. package/plugins/memory/style.ts +17 -0
  99. package/plugins/mentions/attach.ts +127 -0
  100. package/plugins/mentions/index.ts +79 -0
  101. package/plugins/mentions/plugin.json +7 -0
  102. package/plugins/mentions/style.ts +17 -0
  103. package/plugins/models/config.ts +53 -0
  104. package/plugins/models/configure.ts +455 -0
  105. package/plugins/models/context.ts +32 -0
  106. package/plugins/models/index.ts +198 -0
  107. package/plugins/models/plugin.json +8 -0
  108. package/{src → plugins/models}/provider.ts +23 -27
  109. package/plugins/models/saved.ts +94 -0
  110. package/plugins/modes/index.ts +222 -0
  111. package/plugins/modes/plugin.json +9 -0
  112. package/plugins/plugins/index.ts +38 -0
  113. package/plugins/plugins/plugin.json +7 -0
  114. package/plugins/print/index.ts +142 -0
  115. package/plugins/print/plugin.json +8 -0
  116. package/plugins/prompts/index.ts +83 -0
  117. package/plugins/prompts/plugin.json +8 -0
  118. package/plugins/prompts/prompts.ts +253 -0
  119. package/plugins/prompts/sys.ts +72 -0
  120. package/plugins/renderers/index.ts +78 -0
  121. package/plugins/renderers/plugin.json +7 -0
  122. package/plugins/repeats/index.ts +165 -0
  123. package/plugins/repeats/plugin.json +6 -0
  124. package/plugins/schedule/cron.ts +74 -0
  125. package/plugins/schedule/index.ts +292 -0
  126. package/plugins/schedule/plugin.json +13 -0
  127. package/plugins/schedule/store.ts +149 -0
  128. package/plugins/scratch/index.ts +31 -0
  129. package/plugins/scratch/plugin.json +7 -0
  130. package/plugins/screenshot/index.ts +40 -0
  131. package/plugins/screenshot/plugin.json +7 -0
  132. package/plugins/screenshot/sys.ts +84 -0
  133. package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
  134. package/plugins/sessions/index.ts +148 -0
  135. package/plugins/sessions/plugin.json +8 -0
  136. package/plugins/shell/index.ts +33 -0
  137. package/plugins/shell/plugin.json +17 -0
  138. package/plugins/shell/renderers.ts +45 -0
  139. package/plugins/shell/service.ts +44 -0
  140. package/{src/tools → plugins/shell}/shell.ts +36 -21
  141. package/{src → plugins/shell}/sys.ts +19 -43
  142. package/plugins/skills/index.ts +96 -0
  143. package/plugins/skills/plugin.json +7 -0
  144. package/plugins/skills/project.ts +49 -0
  145. package/{src → plugins/skills}/skills.ts +4 -11
  146. package/plugins/suggest/index.ts +286 -0
  147. package/plugins/suggest/plugin.json +18 -0
  148. package/plugins/task/index.ts +167 -0
  149. package/plugins/task/plugin.json +15 -0
  150. package/plugins/task/style.ts +17 -0
  151. package/plugins/task/subagent.ts +257 -0
  152. package/{src/tools/todo.ts → plugins/todo/index.ts} +33 -7
  153. package/plugins/todo/plugin.json +7 -0
  154. package/{src/ui → plugins/tui}/editor.ts +53 -1
  155. package/plugins/tui/frontend.ts +596 -0
  156. package/plugins/tui/index.ts +37 -0
  157. package/{src/ui → plugins/tui}/markdown.ts +258 -219
  158. package/plugins/tui/mentions.ts +113 -0
  159. package/plugins/tui/plugin.json +10 -0
  160. package/plugins/tui/render.ts +67 -0
  161. package/plugins/tui/screen.ts +268 -0
  162. package/{src/ui → plugins/tui}/transcript.ts +179 -111
  163. package/{src/ui/tui.tsx → plugins/tui/tui.ts} +684 -418
  164. package/{src/tools → plugins/undo}/checkpoint.ts +21 -60
  165. package/plugins/undo/index.ts +139 -0
  166. package/plugins/undo/notes.ts +43 -0
  167. package/plugins/undo/plugin.json +7 -0
  168. package/plugins/undo/style.ts +17 -0
  169. package/plugins/update/index.ts +40 -0
  170. package/plugins/update/plugin.json +13 -0
  171. package/plugins/web/env.ts +78 -0
  172. package/plugins/web/index.ts +133 -0
  173. package/plugins/web/plugin.json +15 -0
  174. package/plugins/web/renderers.ts +11 -0
  175. package/plugins/web/web.ts +713 -0
  176. package/plugins-dist/approval/index.js +271 -0
  177. package/plugins-dist/approval/plugin.json +24 -0
  178. package/{dist/plugins → plugins-dist/approval}/policy.js +15 -65
  179. package/plugins-dist/approval/review.js +176 -0
  180. package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
  181. package/plugins-dist/ask/index.js +145 -0
  182. package/plugins-dist/ask/plugin.json +12 -0
  183. package/plugins-dist/budget/index.js +37 -0
  184. package/plugins-dist/budget/plugin.json +15 -0
  185. package/{dist/plugins → plugins-dist/checks}/checks.js +65 -34
  186. package/plugins-dist/checks/index.js +150 -0
  187. package/plugins-dist/checks/plugin.json +21 -0
  188. package/plugins-dist/checks/project.js +76 -0
  189. package/plugins-dist/commands/index.js +33 -0
  190. package/plugins-dist/commands/plugin.json +9 -0
  191. package/{dist/plugins → plugins-dist/compact}/compact.js +95 -33
  192. package/plugins-dist/compact/index.js +100 -0
  193. package/plugins-dist/compact/plugin.json +25 -0
  194. package/plugins-dist/compact/style.js +14 -0
  195. package/plugins-dist/continue/index.js +57 -0
  196. package/plugins-dist/continue/plugin.json +15 -0
  197. package/{dist/ui → plugins-dist/fs}/diff.js +3 -5
  198. package/{dist/tools → plugins-dist/fs}/fs.js +62 -46
  199. package/{dist/tools → plugins-dist/fs}/grep.js +4 -4
  200. package/plugins-dist/fs/index.js +36 -0
  201. package/plugins-dist/fs/plugin.json +10 -0
  202. package/plugins-dist/fs/renderers.js +132 -0
  203. package/plugins-dist/fs/style.js +15 -0
  204. package/plugins-dist/fs/sys.js +54 -0
  205. package/plugins-dist/keys/index.js +36 -0
  206. package/plugins-dist/keys/plugin.json +9 -0
  207. package/plugins-dist/mcp/index.js +187 -0
  208. package/{dist → plugins-dist/mcp}/mcp.js +34 -30
  209. package/plugins-dist/mcp/plugin.json +12 -0
  210. package/plugins-dist/mcp/project.js +90 -0
  211. package/plugins-dist/memory/index.js +94 -0
  212. package/{dist → plugins-dist/memory}/memory.js +21 -3
  213. package/plugins-dist/memory/notes.js +36 -0
  214. package/plugins-dist/memory/plugin.json +11 -0
  215. package/plugins-dist/memory/style.js +14 -0
  216. package/plugins-dist/mentions/attach.js +120 -0
  217. package/plugins-dist/mentions/index.js +69 -0
  218. package/plugins-dist/mentions/plugin.json +9 -0
  219. package/plugins-dist/mentions/style.js +14 -0
  220. package/plugins-dist/models/config.js +53 -0
  221. package/plugins-dist/models/configure.js +415 -0
  222. package/plugins-dist/models/context.js +27 -0
  223. package/plugins-dist/models/index.js +167 -0
  224. package/plugins-dist/models/plugin.json +14 -0
  225. package/{dist → plugins-dist/models}/provider.js +22 -19
  226. package/plugins-dist/models/saved.js +80 -0
  227. package/plugins-dist/modes/index.js +190 -0
  228. package/plugins-dist/modes/plugin.json +18 -0
  229. package/plugins-dist/plugins/index.js +37 -0
  230. package/plugins-dist/plugins/plugin.json +10 -0
  231. package/plugins-dist/print/index.js +153 -0
  232. package/plugins-dist/print/plugin.json +14 -0
  233. package/plugins-dist/prompts/index.js +57 -0
  234. package/plugins-dist/prompts/plugin.json +13 -0
  235. package/plugins-dist/prompts/prompts.js +227 -0
  236. package/plugins-dist/prompts/sys.js +46 -0
  237. package/plugins-dist/renderers/index.js +36 -0
  238. package/plugins-dist/renderers/plugin.json +9 -0
  239. package/plugins-dist/repeats/index.js +165 -0
  240. package/plugins-dist/repeats/plugin.json +6 -0
  241. package/plugins-dist/schedule/cron.js +67 -0
  242. package/plugins-dist/schedule/index.js +281 -0
  243. package/plugins-dist/schedule/plugin.json +16 -0
  244. package/plugins-dist/schedule/store.js +125 -0
  245. package/plugins-dist/scratch/index.js +26 -0
  246. package/plugins-dist/scratch/plugin.json +10 -0
  247. package/plugins-dist/screenshot/index.js +35 -0
  248. package/plugins-dist/screenshot/plugin.json +11 -0
  249. package/plugins-dist/screenshot/sys.js +76 -0
  250. package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
  251. package/plugins-dist/sessions/index.js +136 -0
  252. package/plugins-dist/sessions/plugin.json +12 -0
  253. package/plugins-dist/shell/index.js +33 -0
  254. package/plugins-dist/shell/plugin.json +19 -0
  255. package/plugins-dist/shell/renderers.js +40 -0
  256. package/plugins-dist/shell/service.js +29 -0
  257. package/{dist/tools → plugins-dist/shell}/shell.js +36 -23
  258. package/{dist → plugins-dist/shell}/sys.js +20 -44
  259. package/plugins-dist/skills/index.js +90 -0
  260. package/plugins-dist/skills/plugin.json +12 -0
  261. package/plugins-dist/skills/project.js +47 -0
  262. package/{dist → plugins-dist/skills}/skills.js +3 -7
  263. package/plugins-dist/suggest/index.js +282 -0
  264. package/plugins-dist/suggest/plugin.json +23 -0
  265. package/plugins-dist/task/index.js +151 -0
  266. package/plugins-dist/task/plugin.json +23 -0
  267. package/plugins-dist/task/style.js +14 -0
  268. package/plugins-dist/task/subagent.js +208 -0
  269. package/{dist/tools/todo.js → plugins-dist/todo/index.js} +29 -7
  270. package/plugins-dist/todo/plugin.json +9 -0
  271. package/{dist/ui → plugins-dist/tui}/editor.js +53 -2
  272. package/plugins-dist/tui/frontend.js +586 -0
  273. package/{dist → plugins-dist/tui}/history.js +0 -1
  274. package/plugins-dist/tui/index.js +21 -0
  275. package/{dist/ui → plugins-dist/tui}/markdown.js +57 -20
  276. package/plugins-dist/tui/mentions.js +115 -0
  277. package/plugins-dist/tui/plugin.json +25 -0
  278. package/plugins-dist/tui/render.js +61 -0
  279. package/plugins-dist/tui/screen.js +201 -0
  280. package/{dist/ui → plugins-dist/tui}/transcript.js +179 -111
  281. package/{dist/ui → plugins-dist/tui}/tui.js +484 -101
  282. package/{dist/tools → plugins-dist/undo}/checkpoint.js +21 -55
  283. package/plugins-dist/undo/index.js +140 -0
  284. package/plugins-dist/undo/notes.js +36 -0
  285. package/plugins-dist/undo/plugin.json +9 -0
  286. package/plugins-dist/undo/style.js +14 -0
  287. package/plugins-dist/update/index.js +34 -0
  288. package/plugins-dist/update/plugin.json +15 -0
  289. package/plugins-dist/web/env.js +80 -0
  290. package/plugins-dist/web/index.js +124 -0
  291. package/plugins-dist/web/plugin.json +21 -0
  292. package/plugins-dist/web/renderers.js +7 -0
  293. package/plugins-dist/web/web.js +660 -0
  294. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  295. package/sdk/python/jade_plugin.py +380 -0
  296. package/src/api.ts +298 -0
  297. package/src/config.ts +32 -134
  298. package/src/fetch.ts +216 -0
  299. package/src/hash.ts +39 -0
  300. package/src/host.ts +429 -0
  301. package/src/index.ts +23 -28
  302. package/src/install.ts +355 -0
  303. package/src/main.ts +152 -1263
  304. package/src/manifest.ts +160 -0
  305. package/src/membrane.ts +142 -0
  306. package/src/remote.ts +288 -0
  307. package/src/rescue.ts +217 -0
  308. package/src/resolve.ts +199 -0
  309. package/src/rpc.ts +144 -0
  310. package/src/secrets.ts +122 -0
  311. package/src/session.ts +458 -0
  312. package/src/source.ts +83 -0
  313. package/src/state.ts +120 -0
  314. package/src/update.ts +265 -0
  315. package/LICENSE +0 -21
  316. package/README.md +0 -198
  317. package/dist/commands.d.ts +0 -30
  318. package/dist/commands.js +0 -106
  319. package/dist/commands.js.map +0 -1
  320. package/dist/context.d.ts +0 -17
  321. package/dist/context.js +0 -53
  322. package/dist/context.js.map +0 -1
  323. package/dist/distro.d.ts +0 -104
  324. package/dist/distro.js +0 -312
  325. package/dist/distro.js.map +0 -1
  326. package/dist/history.d.ts +0 -10
  327. package/dist/history.js.map +0 -1
  328. package/dist/mcp.d.ts +0 -114
  329. package/dist/mcp.js.map +0 -1
  330. package/dist/memory.d.ts +0 -19
  331. package/dist/memory.js.map +0 -1
  332. package/dist/mentions.d.ts +0 -35
  333. package/dist/mentions.js +0 -130
  334. package/dist/mentions.js.map +0 -1
  335. package/dist/plugin-cli.d.ts +0 -16
  336. package/dist/plugin-cli.js +0 -251
  337. package/dist/plugin-cli.js.map +0 -1
  338. package/dist/plugins/budget.d.ts +0 -9
  339. package/dist/plugins/budget.js +0 -30
  340. package/dist/plugins/budget.js.map +0 -1
  341. package/dist/plugins/builtins.d.ts +0 -10
  342. package/dist/plugins/builtins.js +0 -22
  343. package/dist/plugins/builtins.js.map +0 -1
  344. package/dist/plugins/checks.d.ts +0 -70
  345. package/dist/plugins/checks.js.map +0 -1
  346. package/dist/plugins/compact.d.ts +0 -26
  347. package/dist/plugins/compact.js.map +0 -1
  348. package/dist/plugins/core.d.ts +0 -42
  349. package/dist/plugins/core.js +0 -190
  350. package/dist/plugins/core.js.map +0 -1
  351. package/dist/plugins/install.d.ts +0 -68
  352. package/dist/plugins/install.js +0 -377
  353. package/dist/plugins/install.js.map +0 -1
  354. package/dist/plugins/loader.d.ts +0 -182
  355. package/dist/plugins/loader.js +0 -253
  356. package/dist/plugins/loader.js.map +0 -1
  357. package/dist/plugins/policy.d.ts +0 -96
  358. package/dist/plugins/policy.js.map +0 -1
  359. package/dist/plugins/prompts.d.ts +0 -38
  360. package/dist/plugins/prompts.js +0 -196
  361. package/dist/plugins/prompts.js.map +0 -1
  362. package/dist/plugins/repeats.d.ts +0 -9
  363. package/dist/plugins/repeats.js +0 -90
  364. package/dist/plugins/repeats.js.map +0 -1
  365. package/dist/plugins/review.d.ts +0 -16
  366. package/dist/plugins/review.js +0 -92
  367. package/dist/plugins/review.js.map +0 -1
  368. package/dist/plugins/schema.d.ts +0 -2
  369. package/dist/plugins/schema.js.map +0 -1
  370. package/dist/provider.d.ts +0 -111
  371. package/dist/provider.js.map +0 -1
  372. package/dist/sessions.d.ts +0 -22
  373. package/dist/sessions.js +0 -53
  374. package/dist/sessions.js.map +0 -1
  375. package/dist/skills.d.ts +0 -37
  376. package/dist/skills.js.map +0 -1
  377. package/dist/sys.d.ts +0 -48
  378. package/dist/sys.js.map +0 -1
  379. package/dist/tools/checkpoint.d.ts +0 -43
  380. package/dist/tools/checkpoint.js.map +0 -1
  381. package/dist/tools/fs.d.ts +0 -35
  382. package/dist/tools/fs.js.map +0 -1
  383. package/dist/tools/grep.d.ts +0 -6
  384. package/dist/tools/grep.js.map +0 -1
  385. package/dist/tools/screenshot.d.ts +0 -15
  386. package/dist/tools/screenshot.js.map +0 -1
  387. package/dist/tools/shell.d.ts +0 -55
  388. package/dist/tools/shell.js.map +0 -1
  389. package/dist/tools/task.d.ts +0 -35
  390. package/dist/tools/task.js +0 -89
  391. package/dist/tools/task.js.map +0 -1
  392. package/dist/tools/todo.d.ts +0 -10
  393. package/dist/tools/todo.js.map +0 -1
  394. package/dist/tools/web.d.ts +0 -23
  395. package/dist/tools/web.js +0 -426
  396. package/dist/tools/web.js.map +0 -1
  397. package/dist/ui/configure.d.ts +0 -20
  398. package/dist/ui/configure.js +0 -378
  399. package/dist/ui/configure.js.map +0 -1
  400. package/dist/ui/diff.d.ts +0 -20
  401. package/dist/ui/diff.js.map +0 -1
  402. package/dist/ui/editor.d.ts +0 -33
  403. package/dist/ui/editor.js.map +0 -1
  404. package/dist/ui/markdown.d.ts +0 -17
  405. package/dist/ui/markdown.js.map +0 -1
  406. package/dist/ui/render.d.ts +0 -15
  407. package/dist/ui/render.js +0 -94
  408. package/dist/ui/render.js.map +0 -1
  409. package/dist/ui/screen.d.ts +0 -98
  410. package/dist/ui/screen.js +0 -108
  411. package/dist/ui/screen.js.map +0 -1
  412. package/dist/ui/transcript.d.ts +0 -33
  413. package/dist/ui/transcript.js.map +0 -1
  414. package/dist/ui/tui.d.ts +0 -45
  415. package/dist/ui/tui.js.map +0 -1
  416. package/src/commands.ts +0 -120
  417. package/src/context.ts +0 -64
  418. package/src/distro.ts +0 -400
  419. package/src/mentions.ts +0 -135
  420. package/src/plugin-cli.ts +0 -239
  421. package/src/plugins/budget.ts +0 -37
  422. package/src/plugins/builtins.ts +0 -31
  423. package/src/plugins/core.ts +0 -217
  424. package/src/plugins/install.ts +0 -429
  425. package/src/plugins/loader.ts +0 -386
  426. package/src/plugins/prompts.ts +0 -234
  427. package/src/plugins/repeats.ts +0 -96
  428. package/src/plugins/review.ts +0 -109
  429. package/src/sessions.ts +0 -61
  430. package/src/tools/task.ts +0 -109
  431. package/src/tools/web.ts +0 -451
  432. package/src/ui/configure.ts +0 -408
  433. package/src/ui/render.ts +0 -83
  434. package/src/ui/screen.ts +0 -169
  435. /package/{src/plugins → plugins/approval}/schema.ts +0 -0
  436. /package/{src → plugins/tui}/history.ts +0 -0
@@ -1,6 +1,8 @@
1
1
  // 终端界面(Ink):全屏(备用屏幕缓冲区)分三块——顶部标题(版本、模型、目录),中间是对话,
2
2
  // 自己滚动(滚轮、PgUp/PgDn),底部固定显示运行状态、排队的消息、输入框(或对话框)和状态栏。
3
3
  // 运行中也能输入:提交的内容由宿主决定立即执行还是排队。
4
+ //
5
+ // 不用 JSX:插件以 .ts 源码加载,Node 的类型剥离不处理 .tsx,所以组件用 createElement(h)写。
4
6
 
5
7
  import { writeSync } from "node:fs";
6
8
  import {
@@ -16,6 +18,9 @@ import {
16
18
  type Key,
17
19
  } from "ink";
18
20
  import {
21
+ createContext,
22
+ createElement as h,
23
+ useContext,
19
24
  useEffect,
20
25
  useReducer,
21
26
  useRef,
@@ -24,9 +29,9 @@ import {
24
29
  } from "react";
25
30
  import stringWidth from "string-width";
26
31
  import wrapAnsi from "wrap-ansi";
27
- import { mentionAt, mentionText, rankPaths } from "../mentions.ts";
28
- import { edit, empty, layout, type EditState } from "./editor.ts";
29
- import { markdownStream, type MdLine } from "./markdown.ts";
32
+ import { mentionAt, mentionText, rankPaths } from "./mentions.ts";
33
+ import { charWidth, edit, empty, layout, moveRow, offsetAt, type EditState } from "./editor.ts";
34
+ import { expandTabs, markdownStream, type MdLine } from "./markdown.ts";
30
35
  import {
31
36
  dim,
32
37
  type Choice,
@@ -50,12 +55,28 @@ export interface TuiHandlers {
50
55
  submit(text: string): void;
51
56
  /** 运行中按 Esc 或 Ctrl+C */
52
57
  interrupt(): void;
58
+ /** 输入框第一行按 ↑:把排队的消息放回输入框;没有排队的消息时返回 false,↑ 照常翻历史 */
59
+ unqueue?(): boolean;
53
60
  /** 空闲时连按两次 Ctrl+C */
54
61
  exit(): void;
55
62
  /** 输入 @ 时补全用的路径(目录以 / 结尾);每次开始输入一个新的 @ 引用时取一次 */
56
63
  files?(): Promise<string[]>;
57
64
  /** Shift+Tab:切换到下一个权限模式 */
58
65
  cycleMode?(): void;
66
+ /** 输入框有焦点时按下 jade 自己不用的 ctrl+<字母>、alt+<字母或数字>(见 keySpec);返回 true 表示有插件处理了 */
67
+ key?(spec: string): boolean;
68
+ }
69
+
70
+ /** 输入框交给 tuiScreen 的读写入口(Prompt 每次渲染时更新) */
71
+ type DraftBridge = { current?: { get(): string; apply(f: (s: EditState) => EditState): void } };
72
+
73
+ /** 按键写成 "ctrl+g"、"alt+v"(小写);别的键返回 undefined */
74
+ function keySpec(input: string, key: Key): string | undefined {
75
+ if (input.length !== 1) return undefined;
76
+ const c = input.toLowerCase();
77
+ if (key.ctrl && !key.meta && /^[a-z]$/.test(c)) return `ctrl+${c}`;
78
+ if (key.meta && !key.ctrl && /^[a-z0-9]$/.test(c)) return `alt+${c}`;
79
+ return undefined;
59
80
  }
60
81
 
61
82
  type Dialog =
@@ -88,14 +109,16 @@ interface UiState {
88
109
  queued: string[];
89
110
  dialogs: Dialog[];
90
111
  status: StatusInfo;
112
+ /** 插件设置的灰色提示(ctx.input.hint):输入框为空时显示,Tab 或 → 接受 */
113
+ hint?: string;
91
114
  }
92
115
 
93
116
  /** 对话最多保留这么多行(折行前),超出时丢掉最早的一段 */
94
117
  const MAX_LINES = 10_000;
95
118
 
96
- /** 按显示宽度折行,保留颜色;每一项是屏幕上的一行 */
119
+ /** 按显示宽度折行,保留颜色;每一项是屏幕上的一行。制表符先换成空格:按 0 列算宽度会对不齐、超宽 */
97
120
  const wrap = (text: string, width: number) =>
98
- wrapAnsi(text, width, { hard: true, trim: false }).split("\n");
121
+ wrapAnsi(expandTabs(text), width, { hard: true, trim: false }).split("\n");
99
122
 
100
123
  /**
101
124
  * 对话里的一项:普通的一行,或者可以点击展开、收起的一段。
@@ -336,11 +359,18 @@ class Store {
336
359
  this.#owners = [];
337
360
  this.#named.clear();
338
361
  this.update(
339
- { version: this.state.version + 1, live: undefined, progress: [], scroll: 0 },
362
+ { version: this.state.version + 1, live: undefined, progress: [], scroll: 0, hint: undefined },
340
363
  true,
341
364
  );
342
365
  }
343
366
 
367
+ /** 展开或收起最后一个可折叠的项(Ctrl+O);start、view 同 toggle */
368
+ toggleLatest(start: number, view: number) {
369
+ for (let r = this.#owners.length - 1; r >= 0; r--) {
370
+ if (foldable(this.#owners[r]!, this.#width)) return this.toggle(r, start, view);
371
+ }
372
+ }
373
+
344
374
  /** 往上(正数)或往下滚 n 行,不超过 max */
345
375
  scrollBy(n: number, max: number) {
346
376
  const scroll = Math.max(0, Math.min(max, this.state.scroll + n));
@@ -352,7 +382,8 @@ export interface TuiOptions {
352
382
  status: StatusInfo;
353
383
  /** 标题里显示的版本号 */
354
384
  version: string;
355
- commands: CommandInfo[];
385
+ /** 斜杠命令菜单的内容;命令随会话的插件增减时传函数,每次显示菜单时取最新的 */
386
+ commands: CommandInfo[] | (() => CommandInfo[]);
356
387
  handlers: TuiHandlers;
357
388
  /** 之前提交过的输入(旧的在前),↑/↓ 和 Ctrl+R 用 */
358
389
  history?: string[];
@@ -360,9 +391,12 @@ export interface TuiOptions {
360
391
  io?: { stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream };
361
392
  }
362
393
 
363
- /** 开关鼠标事件上报(SGR 格式),用来接收滚轮和点击;开着时终端里选中文字要按住 Shift */
364
- const MOUSE_ON = "\x1b[?1000h\x1b[?1006h";
365
- const MOUSE_OFF = "\x1b[?1006l\x1b[?1000l";
394
+ /**
395
+ * 开关鼠标事件上报(SGR 格式),用来接收滚轮、点击和按住左键拖动(1002)。
396
+ * 开着时终端自己不再处理点击,所以对话区的文字由 jade 自己选中和复制(见 Selection)
397
+ */
398
+ const MOUSE_ON = "\x1b[?1000h\x1b[?1002h\x1b[?1006h";
399
+ const MOUSE_OFF = "\x1b[?1006l\x1b[?1002l\x1b[?1000l";
366
400
  /**
367
401
  * 光标样式:闪烁的竖线(DECSCUSR 5,再加上 ATT610 的"开始闪烁"给只认它的终端),一眼能看出输入框有焦点;
368
402
  * 退出时还原成终端自己的默认样式。光标什么时候显示仍由 Ink 管(只有输入框或输入对话框有焦点时才显示)
@@ -370,9 +404,30 @@ const MOUSE_OFF = "\x1b[?1006l\x1b[?1000l";
370
404
  const CURSOR_BLINK = "\x1b[5 q\x1b[?12h";
371
405
  const CURSOR_RESET = "\x1b[0 q";
372
406
 
407
+ /** 工作超过这么久后结束,或者这时弹出要用户选择的对话框,用户多半去做别的了:提醒一下 */
408
+ const NOTIFY_AFTER = 10_000;
409
+ /** 支持 OSC 9 桌面通知的终端;其他终端只响铃(不认识的 OSC 9 在有的终端里会弹别的东西) */
410
+ const OSC9_TERMINALS = new Set(["iTerm.app", "WezTerm", "ghostty"]);
411
+
412
+ /** 响铃,支持的终端再发一条桌面通知。JADE_NOTIFY=off 关闭 */
413
+ function notifier(stdout: NodeJS.WriteStream, env = process.env): (message: string) => void {
414
+ if (env.JADE_NOTIFY === "off") return () => {};
415
+ const osc9 = OSC9_TERMINALS.has(env.TERM_PROGRAM ?? "");
416
+ return (message) =>
417
+ stdout.write(osc9 ? `\x1b]9;jade: ${message.replace(/[\x00-\x1f\x7f]/g, " ")}\x07` : "\x07");
418
+ }
419
+
373
420
  export function tuiScreen(opts: TuiOptions): Screen {
374
421
  const stdout = opts.io?.stdout ?? process.stdout;
375
422
  const store = new Store(opts.status);
423
+ const draft: DraftBridge = {};
424
+ const notify = notifier(stdout);
425
+ /** 已经工作了很久(见 NOTIFY_AFTER) */
426
+ const longBusy = () => {
427
+ const since = store.state.busy?.since;
428
+ return since !== undefined && Date.now() - since >= NOTIFY_AFTER;
429
+ };
430
+ let doneTimer: ReturnType<typeof setTimeout> | undefined;
376
431
 
377
432
  /**
378
433
  * 正在进行的思考:一开始就作为一项放进对话(可以点击展开),之后的增量攒起来,每隔一小段时间改写一次那一项。
@@ -412,7 +467,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
412
467
  const lines = live.text.split("\n");
413
468
  const rest = all ? "" : lines.pop()!;
414
469
  if (all && lines.at(-1) === "") lines.pop();
415
- appendMd(lines.flatMap((l) => md.push(l)));
470
+ appendMd(lines.flatMap((l) => md.push(expandTabs(l))));
416
471
  if (all) {
417
472
  appendMd(md.flush());
418
473
  md = markdownStream();
@@ -420,11 +475,15 @@ export function tuiScreen(opts: TuiOptions): Screen {
420
475
  store.update({ live: rest || !all ? { text: rest, pending: md.pending() } : undefined });
421
476
  };
422
477
 
423
- const ask = <T,>(dialog: Dialog) =>
478
+ /** signal 中止时关掉对话框,返回 undefined */
479
+ const ask = <T>(dialog: Dialog, signal?: AbortSignal) =>
424
480
  new Promise<T>((resolve) => {
481
+ if (signal?.aborted) return resolve(undefined as T);
482
+ const close = () => d.resolve(undefined);
425
483
  const d = {
426
484
  ...dialog,
427
485
  resolve: (v: unknown) => {
486
+ signal?.removeEventListener("abort", close);
428
487
  store.update(
429
488
  { dialogs: store.state.dialogs.filter((x) => x !== d) },
430
489
  true,
@@ -432,20 +491,24 @@ export function tuiScreen(opts: TuiOptions): Screen {
432
491
  resolve(v as T);
433
492
  },
434
493
  } as Dialog;
494
+ signal?.addEventListener("abort", close, { once: true });
435
495
  store.update({ dialogs: [...store.state.dialogs, d] }, true);
436
496
  });
437
497
 
438
498
  // 切到备用屏幕后光标还停在原来的位置(不少终端不会自动归位),Ink 从那里往下画第一帧会让屏幕滚动,
439
499
  // 之后每帧只重写变化的行,整个界面就错位了。所以先自己切过去、清屏、把光标移到左上角
440
500
  stdout.write("\x1b[?1049h\x1b[2J\x1b[H");
501
+ const commands = opts.commands;
441
502
  const instance = render(
442
- <App
443
- store={store}
444
- version={opts.version}
445
- commands={opts.commands}
446
- handlers={opts.handlers}
447
- history={opts.history ?? []}
448
- />,
503
+ h(App, {
504
+ store,
505
+ copy: (text: string) => void stdout.write(osc52(text)),
506
+ version: opts.version,
507
+ commands: typeof commands === "function" ? commands : () => commands,
508
+ handlers: opts.handlers,
509
+ history: opts.history ?? [],
510
+ draft,
511
+ }),
449
512
  {
450
513
  exitOnCtrlC: false,
451
514
  alternateScreen: true,
@@ -526,9 +589,15 @@ export function tuiScreen(opts: TuiOptions): Screen {
526
589
  },
527
590
  busy(label) {
528
591
  const current = store.state.busy;
529
- if (label === undefined) store.update({ busy: undefined });
530
- else
531
- store.update({ busy: { label, since: current?.since ?? Date.now() } });
592
+ if (label === undefined) {
593
+ // 稍等一下再提醒:排队的消息接着开始时(马上又 busy)不算结束
594
+ if (longBusy()) doneTimer ??= setTimeout(() => notify("done"), 300);
595
+ store.update({ busy: undefined });
596
+ return;
597
+ }
598
+ clearTimeout(doneTimer);
599
+ doneTimer = undefined;
600
+ store.update({ busy: { label, since: current?.since ?? Date.now() } });
532
601
  },
533
602
  status(info) {
534
603
  store.update({ status: { ...store.state.status, ...info } });
@@ -536,21 +605,38 @@ export function tuiScreen(opts: TuiOptions): Screen {
536
605
  queued(items) {
537
606
  store.update({ queued: [...items] });
538
607
  },
539
- select: <T,>(title: string, choices: Choice<T>[], o?: { body?: string }) =>
540
- ask<T | undefined>({
541
- kind: "select",
542
- title,
543
- body: o?.body,
544
- choices: choices as Choice<unknown>[],
545
- resolve: () => {},
546
- }),
547
- input: (title, o) =>
548
- ask<string | undefined>({
549
- kind: "input",
550
- title,
551
- ...o,
552
- resolve: () => {},
553
- }),
608
+ draft: {
609
+ text: () => draft.current?.get() ?? "",
610
+ insert: (text) => draft.current?.apply((s) => edit.insert(s, text.replace(/\r\n?/g, "\n"))),
611
+ set: (text) => {
612
+ const value = text.replace(/\r\n?/g, "\n");
613
+ draft.current?.apply(() => ({ value, cursor: value.length }));
614
+ },
615
+ hint: (text) => store.update({ hint: text ? text.replace(/\r\n?/g, "\n") : undefined }),
616
+ },
617
+ select: <T>(title: string, choices: Choice<T>[], o?: { body?: string; signal?: AbortSignal }) => {
618
+ if (longBusy()) notify(title);
619
+ return ask<T | undefined>(
620
+ {
621
+ kind: "select",
622
+ title,
623
+ body: o?.body,
624
+ choices: choices as Choice<unknown>[],
625
+ resolve: () => {},
626
+ },
627
+ o?.signal,
628
+ );
629
+ },
630
+ input: (title, { signal, ...o } = {}) =>
631
+ ask<string | undefined>(
632
+ {
633
+ kind: "input",
634
+ title,
635
+ ...o,
636
+ resolve: () => {},
637
+ },
638
+ signal,
639
+ ),
554
640
  clear() {
555
641
  clearTimeout(thinking?.timer);
556
642
  thinking = undefined;
@@ -558,6 +644,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
558
644
  store.clear();
559
645
  },
560
646
  async close() {
647
+ clearTimeout(doneTimer);
561
648
  flushLive(true);
562
649
  store.update({ live: undefined, busy: undefined, progress: [] }, true);
563
650
  // 等最后一帧画完再卸载;卸载时 Ink 退出备用屏幕
@@ -576,8 +663,103 @@ export function tuiScreen(opts: TuiOptions): Screen {
576
663
  * SGR 鼠标事件(Ink 去掉了开头的 ESC):按钮号、列、行(从 1 开始),M 是按下、m 是松开。
577
664
  * 按钮号去掉 Shift/Alt/Ctrl 位后 0 是左键,64 是滚轮向上,65 向下
578
665
  */
579
- const MOUSE = /^\[<(\d+);\d+;(\d+)([Mm])$/;
666
+ const MOUSE = /^\[<(\d+);(\d+);(\d+)([Mm])$/;
667
+ /** 解析鼠标事件:button 是去掉 Shift/Alt/Ctrl 位的按钮号,press 表示按下;x、y 是屏幕上的列和行(从 0 开始) */
668
+ function mouseEvent(input: string): { button: number; press: boolean; x: number; y: number } | undefined {
669
+ const m = MOUSE.exec(input);
670
+ if (!m) return undefined;
671
+ return { button: Number(m[1]) & ~0b11100, press: m[4] === "M", x: Number(m[2]) - 1, y: Number(m[3]) - 1 };
672
+ }
580
673
  const WHEEL_LINES = 3;
674
+
675
+ /** 按住左键拖动时的按钮号(左键 0 加上移动位 32) */
676
+ const DRAG = 32;
677
+ /** "已复制"的提示显示多久 */
678
+ const COPIED_MS = 2000;
679
+
680
+ // ---------- 选中文字 ----------
681
+
682
+ /** 对话区里的位置:row 是整个对话(已完成的加上正在输出的)的第几行,col 按显示宽度(中文占两列) */
683
+ type Point = { row: number; col: number };
684
+ /** anchor 是按下左键的位置,focus 是拖到的位置;两端都包含在内 */
685
+ type Selection = { anchor: Point; focus: Point };
686
+
687
+ const same = (a: Point, b: Point) => a.row === b.row && a.col === b.col;
688
+
689
+ /** 第 row 行选中的列 [from, to);这一行不在选区里时 undefined */
690
+ function selectedCols(s: Selection, row: number): [number, number] | undefined {
691
+ const { anchor: a, focus: f } = s;
692
+ const [first, last] = a.row < f.row || (a.row === f.row && a.col <= f.col) ? [a, f] : [f, a];
693
+ if (row < first.row || row > last.row) return undefined;
694
+ return [row === first.row ? first.col : 0, row === last.row ? last.col + 1 : Infinity];
695
+ }
696
+
697
+ /** ANSI 转义序列(CSI,和以 BEL 或 ST 结束的 OSC,例如超链接) */
698
+ const ANSI = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/y;
699
+ const INVERSE = "\x1b[7m";
700
+ const INVERSE_OFF = "\x1b[27m";
701
+
702
+ /**
703
+ * 逐个走过一行里的字符:visit 收到每个字符和它是否落在列 [from, to) 里(宽字符跨进去一半也算)。
704
+ * 转义序列原样交给 escape
705
+ */
706
+ function walk(line: string, from: number, to: number, visit: (ch: string, inside: boolean) => void, escape?: (seq: string) => void) {
707
+ let col = 0;
708
+ for (let i = 0; i < line.length; ) {
709
+ ANSI.lastIndex = i;
710
+ const m = ANSI.exec(line);
711
+ if (m) {
712
+ escape?.(m[0]);
713
+ i += m[0].length;
714
+ continue;
715
+ }
716
+ const ch = String.fromCodePoint(line.codePointAt(i)!);
717
+ const w = charWidth(ch);
718
+ visit(ch, col < to && col + Math.max(w, 1) > from);
719
+ col += w;
720
+ i += ch.length;
721
+ }
722
+ }
723
+
724
+ /** 给一行里选中的部分加上反色;中间的转义序列可能关掉反色,之后重新打开 */
725
+ function highlight(line: string, from: number, to: number): string {
726
+ let out = "";
727
+ let on = false;
728
+ walk(
729
+ line,
730
+ from,
731
+ to,
732
+ (ch, inside) => {
733
+ if (inside !== on) out += inside ? INVERSE : INVERSE_OFF;
734
+ on = inside;
735
+ out += ch;
736
+ },
737
+ (seq) => (out += seq + (on ? INVERSE : "")),
738
+ );
739
+ return out + (on ? INVERSE_OFF : "");
740
+ }
741
+
742
+ /** 选中的纯文字:每行去掉行尾空白,用换行连起来(自动折行的地方也是换行) */
743
+ function selectedText(s: Selection, row: (i: number) => string | undefined): string {
744
+ const lines: string[] = [];
745
+ const { anchor: a, focus: f } = s;
746
+ for (let r = Math.min(a.row, f.row); r <= Math.max(a.row, f.row); r++) {
747
+ const [from, to] = selectedCols(s, r)!;
748
+ let text = "";
749
+ walk(row(r) ?? "", from, to, (ch, inside) => void (inside && (text += ch)));
750
+ lines.push(text.trimEnd());
751
+ }
752
+ return lines.join("\n");
753
+ }
754
+
755
+ /** OSC 52:让终端把文字放进系统剪贴板(Windows Terminal、iTerm2、kitty、WezTerm 等支持) */
756
+ const osc52 = (text: string) => `\x1b]52;c;${Buffer.from(text, "utf8").toString("base64")}\x07`;
757
+
758
+ /**
759
+ * 输入框和对话框用:有选中的文字时 Ctrl+C 只取消选中(很多人选完习惯按 Ctrl+C 复制),
760
+ * 不中断模型、不清空输入、不取消对话框
761
+ */
762
+ const SelectionContext = createContext<{ active(): boolean; clear(): void }>({ active: () => false, clear: () => {} });
581
763
  /** 终端矮于这么多行时标题只占一行 */
582
764
  const COMPACT_ROWS = 24;
583
765
 
@@ -590,16 +772,21 @@ const roomFor = (rows: number, max: number) =>
590
772
 
591
773
  function App({
592
774
  store,
775
+ copy,
593
776
  version,
594
777
  commands,
595
778
  handlers,
596
779
  history,
780
+ draft,
597
781
  }: {
598
782
  store: Store;
783
+ /** 把选中的文字放进剪贴板 */
784
+ copy(text: string): void;
599
785
  version: string;
600
- commands: CommandInfo[];
786
+ commands: () => CommandInfo[];
601
787
  handlers: TuiHandlers;
602
788
  history: string[];
789
+ draft: DraftBridge;
603
790
  }) {
604
791
  const state = useSyncExternalStore(store.subscribe, store.get);
605
792
  const dialog = state.dialogs[0];
@@ -642,11 +829,42 @@ function App({
642
829
  ];
643
830
  const page = Math.max(1, view - 2);
644
831
 
832
+ // 选中文字:按下左键记下位置,拖动时更新,松开时复制;点一下(没拖动)还是展开或收起那一项。
833
+ // 几个事件可能在同一块输入里连着到达,来不及重画,所以另外用 ref 记着最新的值
834
+ const [selection, setSelectionState] = useState<Selection>();
835
+ const selected = useRef<Selection>(undefined);
836
+ const pressed = useRef<Point>(undefined);
837
+ const setSelection = (s: Selection | undefined) => {
838
+ selected.current = s;
839
+ setSelectionState(s);
840
+ };
841
+ const [selectionApi] = useState(() => ({ active: () => selected.current !== undefined, clear: () => setSelection(undefined) }));
842
+ // 宽度变了,行重新折过,原来的位置对不上了
843
+ useEffect(() => setSelection(undefined), [width]);
844
+ const rowAt = (i: number) => (i < done.length ? done[i] : live[i - done.length]);
845
+ // 复制之后在输入框下面提示一会儿
846
+ const [copied, setCopied] = useState<string>();
847
+ const copiedTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
848
+ useEffect(() => () => clearTimeout(copiedTimer.current), []);
849
+ const copySelection = (s: Selection) => {
850
+ const text = selectedText(s, rowAt);
851
+ copy(text);
852
+ clearTimeout(copiedTimer.current);
853
+ setCopied(`Copied ${[...text].length} characters to the clipboard`);
854
+ copiedTimer.current = setTimeout(() => setCopied(undefined), COPIED_MS);
855
+ };
856
+
645
857
  // Ink 增量渲染只用相对光标移动,第一帧起点一旦偏了(启动时终端尺寸还没稳定、窗口缩放、别的输出插进来),
646
- // 之后每帧都会画错行。启动后、尺寸变化后和按 Ctrl+L 时让一帧比终端高,Ink 会清屏从左上角整帧重画(包在同步输出里),下一帧再恢复
858
+ // 之后每帧都会画错行。尺寸变化后和按 Ctrl+L 时让一帧比终端高,Ink 会清屏从左上角整帧重画(包在同步输出里),下一帧再恢复。
859
+ // 启动时不重画:第一帧之前已经清屏、光标在左上角;这两次清屏在不支持同步输出的终端上(Windows 上常见)看得出闪一下
647
860
  const [redraws, setRedraws] = useState(0);
648
861
  const [repaint, setRepaint] = useState(false);
862
+ const mounted = useRef(false);
649
863
  useEffect(() => {
864
+ if (!mounted.current) {
865
+ mounted.current = true;
866
+ return;
867
+ }
650
868
  const t = setTimeout(() => setRepaint(true), 100);
651
869
  return () => clearTimeout(t);
652
870
  }, [columns, rows, redraws]);
@@ -659,19 +877,38 @@ function App({
659
877
 
660
878
  useInput((input, key) => {
661
879
  if (key.ctrl && input === "l") return setRedraws((n) => n + 1);
662
- const mouse = MOUSE.exec(input);
880
+ // 展开或收起最近的一项(工具结果、思考、检查报告),鼠标点不了时也能看完整内容
881
+ if (key.ctrl && input === "o") return store.toggleLatest(start, view);
882
+ const mouse = mouseEvent(input);
663
883
  if (mouse) {
664
- const button = Number(mouse[1]) & ~0b11100;
665
- if (button === 64) store.scrollBy(WHEEL_LINES, max);
666
- else if (button === 65) store.scrollBy(-WHEEL_LINES, max);
667
- else if (button === 0 && mouse[3] === "M") {
668
- // 点在对话区里已经完成的输出上:展开或收起那一项
669
- const i = Number(mouse[2]) - 1 - top;
670
- if (i >= 0 && i < shown.length && start + i < done.length)
671
- store.toggle(start + i, start, view);
884
+ // 对话区里的位置;clamp 时拖出对话区停在第一行或最后一行,否则在对话区外返回 undefined
885
+ const at = (clamp: boolean): Point | undefined => {
886
+ const i = mouse.y - top;
887
+ if (!clamp && (i < 0 || i >= shown.length)) return undefined;
888
+ return { row: start + Math.min(Math.max(i, 0), shown.length - 1), col: Math.max(0, mouse.x - 1) };
889
+ };
890
+ if (mouse.button === 64) store.scrollBy(WHEEL_LINES, max);
891
+ else if (mouse.button === 65) store.scrollBy(-WHEEL_LINES, max);
892
+ else if (mouse.button === 0 && mouse.press) {
893
+ if (selected.current) setSelection(undefined);
894
+ pressed.current = at(false);
895
+ } else if (mouse.button === DRAG && pressed.current) {
896
+ setSelection({ anchor: pressed.current, focus: at(true)! });
897
+ } else if (mouse.button === 0 && !mouse.press && pressed.current) {
898
+ const p = pressed.current;
899
+ pressed.current = undefined;
900
+ const s = selected.current;
901
+ if (s && !same(s.anchor, s.focus)) copySelection(s);
902
+ else {
903
+ if (s) setSelection(undefined);
904
+ // 点在对话区里已经完成的输出上:展开或收起那一项
905
+ if (p.row < done.length) store.toggle(p.row, start, view);
906
+ }
672
907
  }
673
908
  return;
674
909
  }
910
+ // 按别的键取消选中;Ctrl+C 由输入框和对话框处理(见 SelectionContext)
911
+ if (selected.current && !(key.ctrl && input === "c")) setSelection(undefined);
675
912
  // 选择框自己用 PgUp/PgDn 翻选项
676
913
  if (dialog?.kind === "select") return;
677
914
  if (key.pageUp) store.scrollBy(page, max);
@@ -683,70 +920,75 @@ function App({
683
920
  // 总高度要比终端少一行,否则 Ink 认为输出占满了屏幕,每帧清屏重画。
684
921
  // 重画那一帧上下各垫一行空白:终端把顶上那行滚出去后,看到的和平时逐行一样,所以不会闪。
685
922
  // 只做纵向裁剪:每行都已按宽度折好或截断,横向裁剪会让 Ink 每帧对每一行再切一遍 ANSI 字符串
686
- return (
687
- <Box
688
- flexDirection="column"
689
- width={columns}
690
- height={repaint ? rows + 1 : Math.max(1, rows - 1)}
691
- paddingY={repaint ? 1 : 0}
692
- overflowY="hidden"
693
- >
694
- {/* 垫的空白放在外层:里面这层在重画那一帧和平时一样高,对话区的 top(相对父节点)不变,点击换算的行号不会差一行 */}
695
- <Box flexDirection="column" flexGrow={1} overflowY="hidden">
696
- <Header
697
- status={state.status}
698
- version={version}
699
- compact={rows < COMPACT_ROWS}
700
- />
701
- <Box
702
- ref={ref}
703
- flexDirection="column"
704
- flexGrow={1}
705
- flexShrink={1}
706
- flexBasis={0}
707
- overflowY="hidden"
708
- paddingX={1}
709
- >
710
- {shown.map((l, i) => (
711
- <Text key={i} wrap="truncate-end">
712
- {l || " "}
713
- </Text>
714
- ))}
715
- {scrolled ? (
716
- <Text dimColor wrap="truncate-end">
717
- {`── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──`}
718
- </Text>
719
- ) : null}
720
- </Box>
721
- <Box flexDirection="column" flexShrink={0}>
722
- {state.busy && !dialog ? (
723
- <Spinner label={state.busy.label} since={state.busy.since} />
724
- ) : null}
725
- {state.queued.map((q, i) => (
726
- <Text key={i} dimColor wrap="truncate-end">
727
- {" ↳ queued: "}
728
- {q.replace(/\s+/g, " ")}
729
- </Text>
730
- ))}
731
- {dialog ? (
732
- dialog.kind === "select" ? (
733
- <SelectDialog key={dialogKey(dialog)} dialog={dialog} />
734
- ) : (
735
- <InputDialog key={dialogKey(dialog)} dialog={dialog} />
736
- )
737
- ) : null}
738
- <Prompt
739
- commands={commands}
740
- handlers={handlers}
741
- history={history}
742
- busy={Boolean(state.busy)}
743
- active={!dialog}
744
- onSubmit={() => store.scrollBy(-max, max)}
745
- />
746
- <StatusBar status={state.status} width={Math.max(10, columns - 2)} />
747
- </Box>
748
- </Box>
749
- </Box>
923
+ return h(
924
+ SelectionContext.Provider,
925
+ { value: selectionApi },
926
+ h(
927
+ Box,
928
+ {
929
+ flexDirection: "column",
930
+ width: columns,
931
+ height: repaint ? rows + 1 : Math.max(1, rows - 1),
932
+ paddingY: repaint ? 1 : 0,
933
+ overflowY: "hidden",
934
+ },
935
+ // 垫的空白放在外层:里面这层在重画那一帧和平时一样高,对话区的 top(相对父节点)不变,点击换算的行号不会差一行
936
+ h(
937
+ Box,
938
+ { flexDirection: "column", flexGrow: 1, overflowY: "hidden" },
939
+ h(Header, { status: state.status, version, compact: rows < COMPACT_ROWS }),
940
+ h(
941
+ Box,
942
+ {
943
+ ref,
944
+ flexDirection: "column",
945
+ flexGrow: 1,
946
+ flexShrink: 1,
947
+ flexBasis: 0,
948
+ overflowY: "hidden",
949
+ paddingX: 1,
950
+ },
951
+ shown.map((l, i) => {
952
+ const cols = selection && selectedCols(selection, start + i);
953
+ return h(Text, { key: i, wrap: "truncate-end" }, cols ? highlight(l || " ", ...cols) : l || " ");
954
+ }),
955
+ scrolled
956
+ ? h(
957
+ Text,
958
+ { dimColor: true, wrap: "truncate-end" },
959
+ `── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──`,
960
+ )
961
+ : null,
962
+ ),
963
+ h(
964
+ Box,
965
+ { flexDirection: "column", flexShrink: 0 },
966
+ state.busy && !dialog ? h(Spinner, { label: state.busy.label, since: state.busy.since }) : null,
967
+ state.queued.map((q, i) =>
968
+ h(Text, { key: i, dimColor: true, wrap: "truncate-end" }, " ↳ queued: ", q.replace(/\s+/g, " ")),
969
+ ),
970
+ dialog
971
+ ? dialog.kind === "select"
972
+ ? h(SelectDialog, { key: dialogKey(dialog), dialog })
973
+ : h(InputDialog, { key: dialogKey(dialog), dialog })
974
+ : null,
975
+ h(Prompt, {
976
+ commands,
977
+ handlers,
978
+ history,
979
+ bridge: draft,
980
+ hint: state.hint,
981
+ onHintDone: () => store.update({ hint: undefined }),
982
+ busy: Boolean(state.busy),
983
+ queued: state.queued.length > 0,
984
+ active: !dialog,
985
+ onSubmit: () => store.scrollBy(-max, max),
986
+ flash: copied,
987
+ }),
988
+ h(StatusBar, { status: state.status, width: Math.max(10, columns - 2) }),
989
+ ),
990
+ ),
991
+ ),
750
992
  );
751
993
  }
752
994
 
@@ -758,18 +1000,12 @@ const SHINE = "#b6f2d4";
758
1000
  * 每个字符上下两个像素;眼睛是玉上挖空的两格,左上角一点高光让它看起来像玉
759
1001
  */
760
1002
  function Bot() {
761
- return (
762
- <Box flexDirection="column" flexShrink={0} marginRight={2}>
763
- <Text color={JADE}>
764
- {" ▄"}
765
- <Text color={SHINE} backgroundColor={JADE}>
766
- ▄
767
- </Text>
768
- {"████▄"}
769
- </Text>
770
- <Text color={JADE}>██ ███ ██</Text>
771
- <Text color={JADE}>▀███████▀</Text>
772
- </Box>
1003
+ return h(
1004
+ Box,
1005
+ { flexDirection: "column", flexShrink: 0, marginRight: 2 },
1006
+ h(Text, { color: JADE }, " ▄", h(Text, { color: SHINE, backgroundColor: JADE }, "▄"), "████▄"),
1007
+ h(Text, { color: JADE }, "██ ███ ██"),
1008
+ h(Text, { color: JADE }, "▀███████▀"),
773
1009
  );
774
1010
  }
775
1011
 
@@ -784,49 +1020,37 @@ function Header({
784
1020
  compact: boolean;
785
1021
  }) {
786
1022
  const { columns } = useScreenSize();
1023
+ // 模型 · 目录 (分支)
1024
+ const where = [
1025
+ h(Text, { key: "model", color: "cyan" }, status.model),
1026
+ h(Text, { key: "sep", dimColor: true }, " · "),
1027
+ h(Text, { key: "cwd" }, status.cwd),
1028
+ status.branch ? h(Text, { key: "branch", color: "magenta" }, ` (${status.branch})`) : null,
1029
+ ];
1030
+ const name = h(Text, { key: "name", color: "cyan", bold: true }, "jade");
787
1031
  if (compact)
788
- return (
789
- <Box flexShrink={0} paddingX={1}>
790
- <Text wrap="truncate-end">
791
- <Text color="cyan" bold>
792
- jade
793
- </Text>
794
- <Text dimColor> · </Text>
795
- <Text color="cyan">{status.model}</Text>
796
- <Text dimColor> · </Text>
797
- <Text>{status.cwd}</Text>
798
- {status.branch ? (
799
- <Text color="magenta"> ({status.branch})</Text>
800
- ) : null}
801
- </Text>
802
- </Box>
1032
+ return h(
1033
+ Box,
1034
+ { flexShrink: 0, paddingX: 1 },
1035
+ h(Text, { wrap: "truncate-end" }, name, h(Text, { key: "dot", dimColor: true }, " · "), ...where),
803
1036
  );
804
- return (
805
- <Box flexShrink={0} borderStyle="round" borderColor="cyan" paddingX={1}>
806
- {columns >= 50 ? <Bot /> : null}
807
- <Box flexDirection="column" flexGrow={1} flexShrink={1}>
808
- <Text wrap="truncate-end">
809
- <Text color="cyan" bold>
810
- jade
811
- </Text>
812
- <Text dimColor> v{version}</Text>
813
- {status.session ? (
814
- <Text dimColor>{` · session ${status.session}`}</Text>
815
- ) : null}
816
- </Text>
817
- <Text wrap="truncate-end">
818
- <Text color="cyan">{status.model}</Text>
819
- <Text dimColor> · </Text>
820
- <Text>{status.cwd}</Text>
821
- {status.branch ? (
822
- <Text color="magenta"> ({status.branch})</Text>
823
- ) : null}
824
- </Text>
825
- <Text dimColor wrap="truncate-end">
826
- /help for commands · @ to attach files
827
- </Text>
828
- </Box>
829
- </Box>
1037
+ return h(
1038
+ Box,
1039
+ { flexShrink: 0, borderStyle: "round", borderColor: "cyan", paddingX: 1 },
1040
+ columns >= 50 ? h(Bot) : null,
1041
+ h(
1042
+ Box,
1043
+ { flexDirection: "column", flexGrow: 1, flexShrink: 1 },
1044
+ h(
1045
+ Text,
1046
+ { wrap: "truncate-end" },
1047
+ name,
1048
+ h(Text, { dimColor: true }, ` v${version}`),
1049
+ status.session ? h(Text, { dimColor: true }, ` · session ${status.session}`) : null,
1050
+ ),
1051
+ h(Text, { wrap: "truncate-end" }, ...where),
1052
+ h(Text, { dimColor: true, wrap: "truncate-end" }, "/help for commands · @ to attach files"),
1053
+ ),
830
1054
  );
831
1055
  }
832
1056
 
@@ -843,14 +1067,16 @@ const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "
843
1067
  function Spinner({ label, since }: { label: string; since: number }) {
844
1068
  const now = Date.now();
845
1069
  const seconds = Math.floor((now - since) / 1000);
846
- return (
847
- <Box marginTop={1}>
848
- <Text wrap="truncate-end">
849
- <Text color="cyan">{FRAMES[Math.floor(now / 100) % FRAMES.length]} </Text>
850
- <Text>{label}… </Text>
851
- <Text dimColor>({seconds}s · esc to interrupt)</Text>
852
- </Text>
853
- </Box>
1070
+ return h(
1071
+ Box,
1072
+ { marginTop: 1 },
1073
+ h(
1074
+ Text,
1075
+ { wrap: "truncate-end" },
1076
+ h(Text, { color: "cyan" }, `${FRAMES[Math.floor(now / 100) % FRAMES.length]} `),
1077
+ h(Text, null, `${label}… `),
1078
+ h(Text, { dimColor: true }, `(${seconds}s · esc to interrupt)`),
1079
+ ),
854
1080
  );
855
1081
  }
856
1082
 
@@ -942,28 +1168,27 @@ function StatusBar({ status, width }: { status: StatusInfo; width: number }) {
942
1168
  },
943
1169
  ]
944
1170
  : []),
1171
+ // 插件的段落:比提示晚、比缓存早去掉
1172
+ ...(status.plugins ?? []).map((p) => ({ text: p.text, dropAt: 7.5, dim: true })),
945
1173
  {
946
- text: "PgUp/PgDn scroll · click ▸ to expand · shift+drag to select",
947
- short: "click ▸ to expand",
1174
+ text: "PgUp/PgDn scroll · click ▸ or ctrl+o to expand · drag to select and copy",
1175
+ short: "ctrl+o to expand",
948
1176
  shortAt: 10,
949
1177
  dropAt: 9,
950
1178
  dim: true,
951
1179
  },
952
1180
  ];
953
1181
  const shown = fitSegments(segments, width);
954
- return (
955
- <Box paddingX={1}>
956
- <Text wrap="truncate-end">
957
- {shown.map((s, i) => (
958
- <Text key={i}>
959
- {i ? <Text dimColor> · </Text> : null}
960
- <Text color={s.color} dimColor={s.dim}>
961
- {s.text}
962
- </Text>
963
- </Text>
964
- ))}
965
- </Text>
966
- </Box>
1182
+ return h(
1183
+ Box,
1184
+ { paddingX: 1 },
1185
+ h(
1186
+ Text,
1187
+ { wrap: "truncate-end" },
1188
+ shown.map((s, i) =>
1189
+ h(Text, { key: i }, i ? h(Text, { dimColor: true }, " · ") : null, h(Text, { color: s.color, dimColor: s.dim }, s.text)),
1190
+ ),
1191
+ ),
967
1192
  );
968
1193
  }
969
1194
 
@@ -971,11 +1196,7 @@ function StatusBar({ status, width }: { status: StatusInfo; width: number }) {
971
1196
  function Hint({ variants, width }: { variants: string[]; width: number }) {
972
1197
  const text =
973
1198
  variants.find((v) => stringWidth(v) <= width) ?? variants.at(-1)!;
974
- return (
975
- <Text dimColor wrap="truncate-end">
976
- {text}
977
- </Text>
978
- );
1199
+ return h(Text, { dimColor: true, wrap: "truncate-end" }, text);
979
1200
  }
980
1201
 
981
1202
  const tokens = (n: number) =>
@@ -1011,6 +1232,8 @@ function editKey(s: EditState, input: string, key: Key): EditState | undefined {
1011
1232
  if (key.backspace) return key.meta ? edit.deleteWord(s) : edit.backspace(s);
1012
1233
  if (key.delete || (key.ctrl && input === "d")) return edit.del(s);
1013
1234
  if (key.ctrl && input === "w") return edit.deleteWord(s);
1235
+ if (key.meta && !key.ctrl && input === "f") return edit.wordRight(s);
1236
+ if (key.meta && !key.ctrl && input === "b") return edit.wordLeft(s);
1014
1237
  if (key.ctrl && input === "u") return edit.killBefore(s);
1015
1238
  if (key.ctrl && input === "k") return edit.killAfter(s);
1016
1239
  if (
@@ -1034,6 +1257,9 @@ function editKey(s: EditState, input: string, key: Key): EditState | undefined {
1034
1257
  return text ? edit.insert(s, text) : undefined;
1035
1258
  }
1036
1259
 
1260
+ /** 可编辑文本在屏幕上的位置:第一个字符的列 x、第一行可见行的行 y、可见的是折行后的第 first 行起的 rows 行 */
1261
+ type Placement = { x: number; y: number; first: number; rows: number } | undefined;
1262
+
1037
1263
  /** 渲染可编辑文本并把终端光标放到输入位置(输入法的候选框跟着它) */
1038
1264
  function EditableText({
1039
1265
  state,
@@ -1043,7 +1269,10 @@ function EditableText({
1043
1269
  placeholder,
1044
1270
  active,
1045
1271
  maxRows = Infinity,
1272
+ place,
1046
1273
  }: {
1274
+ /** 每次渲染时写入文字的位置,点击换算光标用 */
1275
+ place?: { current: Placement };
1047
1276
  state: EditState;
1048
1277
  width: number;
1049
1278
  prefix: string;
@@ -1081,6 +1310,7 @@ function EditableText({
1081
1310
  ? origin(ref.current)
1082
1311
  : undefined;
1083
1312
  const position = at && { x: at.x + prefix.length + col, y: at.y + row };
1313
+ if (place) place.current = at && { x: at.x + prefix.length, y: at.y, first, rows: rows.length };
1084
1314
  const [, recheck] = useReducer((n: number) => n + 1, 0);
1085
1315
  useEffect(() => {
1086
1316
  if (!at || !ref.current) return;
@@ -1088,29 +1318,24 @@ function EditableText({
1088
1318
  if (now.x !== at.x || now.y !== at.y) recheck();
1089
1319
  });
1090
1320
 
1091
- return (
1092
- <Box ref={ref} flexDirection="column">
1093
- {active ? <CursorAt position={position} /> : null}
1094
- {state.value === "" && placeholder ? (
1095
- <Text wrap="truncate-end">
1096
- <Text color="cyan">{prefix}</Text>
1097
- <Text dimColor>{placeholder}</Text>
1098
- </Text>
1099
- ) : (
1100
- rows.map((r, i) => (
1101
- <Text key={first + i} wrap="truncate-end">
1102
- <Text color="cyan">
1103
- {first + i === 0
1104
- ? prefix
1105
- : i === 0
1106
- ? "↑".padEnd(prefix.length)
1107
- : " ".repeat(prefix.length)}
1108
- </Text>
1109
- {r}
1110
- </Text>
1111
- ))
1112
- )}
1113
- </Box>
1321
+ return h(
1322
+ Box,
1323
+ { ref, flexDirection: "column" },
1324
+ active ? h(CursorAt, { position }) : null,
1325
+ state.value === "" && placeholder
1326
+ ? h(Text, { wrap: "truncate-end" }, h(Text, { color: "cyan" }, prefix), h(Text, { dimColor: true }, placeholder))
1327
+ : rows.map((r, i) =>
1328
+ h(
1329
+ Text,
1330
+ { key: first + i, wrap: "truncate-end" },
1331
+ h(
1332
+ Text,
1333
+ { color: "cyan" },
1334
+ first + i === 0 ? prefix : i === 0 ? "↑".padEnd(prefix.length) : " ".repeat(prefix.length),
1335
+ ),
1336
+ r,
1337
+ ),
1338
+ ),
1114
1339
  );
1115
1340
  }
1116
1341
 
@@ -1139,18 +1364,35 @@ function Prompt({
1139
1364
  commands,
1140
1365
  handlers,
1141
1366
  history: initial,
1367
+ bridge,
1368
+ hint,
1369
+ onHintDone,
1142
1370
  busy,
1371
+ queued,
1143
1372
  active,
1144
1373
  onSubmit,
1374
+ flash,
1145
1375
  }: {
1146
- commands: CommandInfo[];
1376
+ /** 当前的命令(随会话的插件增减),每次算菜单时取 */
1377
+ commands: () => CommandInfo[];
1147
1378
  handlers: TuiHandlers;
1148
1379
  history: string[];
1380
+ /** 让 tuiScreen 的 draft 读写输入框 */
1381
+ bridge: DraftBridge;
1382
+ /** 插件设置的灰色提示 */
1383
+ hint?: string;
1384
+ /** 提示被接受了,或者用户开始打字了:去掉它 */
1385
+ onHintDone(): void;
1149
1386
  busy: boolean;
1387
+ /** 有排队等待发送的消息 */
1388
+ queued: boolean;
1150
1389
  active: boolean;
1151
1390
  /** 提交后对话区回到底部 */
1152
1391
  onSubmit(): void;
1392
+ /** 临时显示在输入框右上角的提示(例如"已复制") */
1393
+ flash?: string;
1153
1394
  }) {
1395
+ const selection = useContext(SelectionContext);
1154
1396
  const [current, setState] = useLive<EditState>(empty);
1155
1397
  const [getSelected, setSelected] = useLive(0);
1156
1398
  const [getNotice, setNotice] = useLive<string | undefined>(undefined);
@@ -1159,7 +1401,12 @@ function Prompt({
1159
1401
  const browsing = useRef(-1);
1160
1402
  const draft = useRef("");
1161
1403
  const lastCtrlC = useRef(0);
1404
+ const lastEsc = useRef(0);
1405
+ /** 输入框文字在屏幕上的位置,点击时换算光标位置用(见 EditableText 的 place) */
1406
+ const place = useRef<Placement>(undefined);
1162
1407
  const { columns, rows } = useScreenSize();
1408
+ /** 输入框里文字的宽度:边框、内边距和前面的 "› " 之外 */
1409
+ const textWidth = Math.max(10, columns - 5) - 2;
1163
1410
  /** 补全菜单、搜索结果最多显示几项;输入框最多几行 */
1164
1411
  const menuRows = roomFor(rows, MENTION_ROWS);
1165
1412
  const inputRows = roomFor(rows, 10);
@@ -1175,7 +1422,7 @@ function Prompt({
1175
1422
  >(undefined);
1176
1423
 
1177
1424
  const usageOf = (c: CommandInfo) => `/${c.name}${c.args ? ` ${c.args}` : ""}`;
1178
- const column = Math.max(...commands.map((c) => usageOf(c).length)) + 2;
1425
+ const column = Math.max(...commands().map((c) => usageOf(c).length)) + 2;
1179
1426
 
1180
1427
  /** 由最新的输入算出补全菜单和搜索结果;渲染和按键处理都用它 */
1181
1428
  const derive = () => {
@@ -1190,7 +1437,7 @@ function Prompt({
1190
1437
  const naming =
1191
1438
  !recalled && state.value.startsWith("/") && !/\s/.test(state.value);
1192
1439
  const matches = naming
1193
- ? commands.filter((c) => `/${c.name}`.startsWith(state.value))
1440
+ ? commands().filter((c) => `/${c.name}`.startsWith(state.value))
1194
1441
  : [];
1195
1442
  const pick = matches[Math.min(selected, matches.length - 1)];
1196
1443
  // @ 引用:光标处正在输入的路径和补全候选
@@ -1252,22 +1499,36 @@ function Prompt({
1252
1499
  const firstMatch = around(Math.max(0, matches.indexOf(pick!)), matches.length, menuRows);
1253
1500
  const firstFound = around(Math.max(0, found.indexOf(file!)), found.length, menuRows);
1254
1501
 
1502
+ /** 提示只在空闲、输入框为空、没有在搜索历史时显示,也只有这时能接受 */
1503
+ const hinting = () => hint !== undefined && !busy && current().value === "" && !getSearch();
1255
1504
  const set = (s: EditState) => {
1505
+ // 用户开始打字(或者插件改了输入框):提示作废
1506
+ if (s.value && hint !== undefined) onHintDone();
1256
1507
  setState(s);
1257
1508
  setSelected(0);
1258
1509
  setNotice(undefined);
1510
+ lastEsc.current = 0;
1259
1511
  };
1260
- const submit = (text: string) => {
1261
- if (!text.trim()) return;
1512
+ const remember = (text: string) => {
1262
1513
  history.current = [
1263
1514
  ...history.current.filter((h) => h !== text),
1264
1515
  text,
1265
1516
  ].slice(-100);
1266
1517
  browsing.current = -1;
1518
+ };
1519
+ const submit = (text: string) => {
1520
+ if (!text.trim()) return;
1521
+ remember(text);
1267
1522
  set(empty);
1268
1523
  onSubmit();
1269
1524
  handlers.submit(text);
1270
1525
  };
1526
+ /** 清空输入;清掉的内容放进(本次运行的)历史,按 ↑ 能找回来 */
1527
+ const discard = () => {
1528
+ const text = current().value;
1529
+ if (text.trim()) remember(text);
1530
+ set(empty);
1531
+ };
1271
1532
  /** 用选中的路径替换正在输入的 @ 引用;目录不加空格,接着补全里面的文件 */
1272
1533
  const complete = (p: string) => {
1273
1534
  const { state, mention } = derive();
@@ -1280,6 +1541,7 @@ function Prompt({
1280
1541
  cursor: mention!.start + text.length,
1281
1542
  });
1282
1543
  };
1544
+ bridge.current = { get: () => current().value, apply: (f) => set(f(current())) };
1283
1545
  const recall = (i: number) => {
1284
1546
  browsing.current = i;
1285
1547
  const value =
@@ -1336,6 +1598,7 @@ function Prompt({
1336
1598
 
1337
1599
  useInput(
1338
1600
  (input, key) => {
1601
+ if (key.ctrl && input === "c" && selection.active()) return selection.clear();
1339
1602
  const search = getSearch();
1340
1603
  if (search) return searchKey(search, input, key);
1341
1604
  const state = current();
@@ -1345,14 +1608,30 @@ function Prompt({
1345
1608
  : undefined;
1346
1609
  if (key.ctrl && input === "c") {
1347
1610
  if (busy) return handlers.interrupt();
1348
- if (state.value) return set(empty);
1611
+ if (state.value) return discard();
1349
1612
  if (Date.now() - lastCtrlC.current < 2000) return handlers.exit();
1350
1613
  lastCtrlC.current = Date.now();
1351
1614
  return setNotice("Press Ctrl+C again to exit");
1352
1615
  }
1353
1616
  if (key.escape) {
1354
1617
  if (busy) return handlers.interrupt();
1355
- return set(empty);
1618
+ if (!state.value) return;
1619
+ // 误按一下 Esc 不清掉写了半天的内容:连按两次才清空
1620
+ if (Date.now() - lastEsc.current < 1500) return discard();
1621
+ setNotice("Press Esc again to clear the input");
1622
+ lastEsc.current = Date.now();
1623
+ return;
1624
+ }
1625
+ // 点击输入框里的文字:光标移到点的位置
1626
+ const mouse = mouseEvent(input);
1627
+ if (mouse) {
1628
+ const at = place.current;
1629
+ if (!at || mouse.button !== 0 || !mouse.press) return;
1630
+ const row = mouse.y - at.y;
1631
+ if (row < 0 || row >= at.rows) return;
1632
+ const cursor = offsetAt(state.value, textWidth, at.first + row, mouse.x - at.x);
1633
+ if (cursor !== undefined) setState({ ...state, cursor });
1634
+ return;
1356
1635
  }
1357
1636
  // 文字和回车在同一块里到达(输入很快时终端可能合并):插入文字后发送
1358
1637
  if (
@@ -1374,6 +1653,12 @@ function Prompt({
1374
1653
  if (file) return complete(file);
1375
1654
  return submit(state.value.trim());
1376
1655
  }
1656
+ // Tab 或 →:把提示变成输入框里的文字,接着可以改
1657
+ if (((key.tab && !key.shift) || key.rightArrow) && hinting()) {
1658
+ onHintDone();
1659
+ browsing.current = -1;
1660
+ return set({ value: hint!, cursor: hint!.length });
1661
+ }
1377
1662
  if (key.tab && key.shift) return handlers.cycleMode?.();
1378
1663
  if (key.tab) {
1379
1664
  const { pick, file } = derive();
@@ -1388,7 +1673,14 @@ function Prompt({
1388
1673
  if (key.upArrow) {
1389
1674
  const { menu, selected } = derive();
1390
1675
  if (menu) return setSelected((selected - 1 + menu) % menu);
1391
- if (!edit.onFirstLine(state)) return setState(edit.up(state));
1676
+ // 按折行后的屏幕行移动:一大段没有换行的文字也能上下走,到了第一行才翻历史
1677
+ const up = moveRow(state, textWidth, -1);
1678
+ if (up) return setState(up);
1679
+ // 先取回排队的消息,取完了再翻历史
1680
+ if (handlers.unqueue?.()) {
1681
+ browsing.current = -1;
1682
+ return;
1683
+ }
1392
1684
  if (browsing.current + 1 < history.current.length) {
1393
1685
  if (browsing.current === -1) draft.current = state.value;
1394
1686
  recall(browsing.current + 1);
@@ -1398,10 +1690,13 @@ function Prompt({
1398
1690
  if (key.downArrow) {
1399
1691
  const { menu, selected } = derive();
1400
1692
  if (menu) return setSelected((selected + 1) % menu);
1401
- if (!edit.onLastLine(state)) return setState(edit.down(state));
1693
+ const down = moveRow(state, textWidth, 1);
1694
+ if (down) return setState(down);
1402
1695
  if (browsing.current >= 0) recall(browsing.current - 1);
1403
1696
  return;
1404
1697
  }
1698
+ const spec = keySpec(input, key);
1699
+ if (spec && handlers.key?.(spec)) return;
1405
1700
  const next = editKey(state, input, key);
1406
1701
  if (next) set(next);
1407
1702
  },
@@ -1409,125 +1704,116 @@ function Prompt({
1409
1704
  );
1410
1705
 
1411
1706
  // 对话框打开时隐藏但不卸载,输入到一半的内容和历史都保留
1412
- return (
1413
- <Box
1414
- flexDirection="column"
1415
- marginTop={1}
1416
- display={active ? "flex" : "none"}
1417
- >
1418
- <Box
1419
- borderStyle="round"
1420
- borderColor={busy ? "gray" : "cyan"}
1421
- paddingX={1}
1422
- >
1423
- {search ? (
1424
- <EditableText
1425
- state={{ value: search.query, cursor: search.query.length }}
1426
- width={Math.max(10, columns - 5)}
1427
- prefix="search history› "
1428
- placeholder="type to filter"
1429
- active={active}
1430
- maxRows={inputRows}
1431
- />
1432
- ) : (
1433
- <EditableText
1434
- state={state}
1435
- width={Math.max(10, columns - 5)}
1436
- prefix="› "
1437
- placeholder={
1438
- busy
1439
- ? "Type to queue a message · esc to interrupt"
1440
- : columns >= 70
1441
- ? "Message jade · / commands · @ files · ↑ or ctrl+r for history"
1442
- : "Message jade · / commands · @ files"
1443
- }
1444
- active={active}
1445
- maxRows={inputRows}
1446
- />
1447
- )}
1448
- </Box>
1449
- {search ? (
1450
- <Box flexDirection="column" paddingX={2}>
1451
- {results.length ? (
1452
- results.slice(first, first + menuRows).map((h, i) => {
1453
- const on = first + i === hit;
1454
- return (
1455
- <Text
1456
- key={first + i}
1457
- wrap="truncate-end"
1458
- color={on ? "cyan" : undefined}
1459
- bold={on}
1460
- >
1461
- {on ? "❯ " : " "}
1462
- {h.replace(/\s*\n\s*/g, " ⏎ ")}
1463
- </Text>
1707
+ return h(
1708
+ Box,
1709
+ { flexDirection: "column", marginTop: 1, display: active ? "flex" : "none" },
1710
+ h(
1711
+ Box,
1712
+ { borderStyle: "round", borderColor: busy ? "gray" : "cyan", paddingX: 1 },
1713
+ search
1714
+ ? h(EditableText, {
1715
+ state: { value: search.query, cursor: search.query.length },
1716
+ width: Math.max(10, columns - 5),
1717
+ prefix: "search history› ",
1718
+ placeholder: "type to filter",
1719
+ active,
1720
+ maxRows: inputRows,
1721
+ })
1722
+ : h(EditableText, {
1723
+ state,
1724
+ width: Math.max(10, columns - 5),
1725
+ prefix: "› ",
1726
+ placeholder: hinting()
1727
+ ? hintLine(hint!)
1728
+ : queued
1729
+ ? "↑ to edit queued messages · esc to interrupt"
1730
+ : busy
1731
+ ? "Type to queue a message · esc to interrupt"
1732
+ : columns >= 70
1733
+ ? "Message jade · / commands · @ files · ↑ or ctrl+r for history"
1734
+ : "Message jade · / commands · @ files",
1735
+ active,
1736
+ maxRows: inputRows,
1737
+ place,
1738
+ }),
1739
+ ),
1740
+ search
1741
+ ? h(
1742
+ Box,
1743
+ { flexDirection: "column", paddingX: 2 },
1744
+ results.length
1745
+ ? results.slice(first, first + menuRows).map((item, i) => {
1746
+ const on = first + i === hit;
1747
+ return h(
1748
+ Text,
1749
+ { key: first + i, wrap: "truncate-end", color: on ? "cyan" : undefined, bold: on },
1750
+ on ? "❯ " : " ",
1751
+ item.replace(/\s*\n\s*/g, " ⏎ "),
1752
+ );
1753
+ })
1754
+ : h(Text, { dimColor: true }, " no match"),
1755
+ h(Hint, {
1756
+ width: hintWidth,
1757
+ variants: [" ↑↓ or ctrl+r select · enter/tab put in the input · esc cancel", " ↑↓ select · enter use · esc cancel"],
1758
+ }),
1759
+ )
1760
+ : matches.length
1761
+ ? h(
1762
+ Box,
1763
+ { flexDirection: "column", paddingX: 2 },
1764
+ matches.slice(firstMatch, firstMatch + menuRows).map((c) => {
1765
+ const on = c === pick;
1766
+ return h(
1767
+ Text,
1768
+ { key: c.name, wrap: "truncate-end" },
1769
+ h(Text, { color: on ? "cyan" : undefined, bold: on }, on ? "❯ " : " ", usageOf(c).padEnd(column)),
1770
+ h(Text, { dimColor: true }, c.about),
1464
1771
  );
1465
- })
1466
- ) : (
1467
- <Text dimColor> no match</Text>
1468
- )}
1469
- <Hint
1470
- width={hintWidth}
1471
- variants={[
1472
- " ↑↓ or ctrl+r select · enter/tab put in the input · esc cancel",
1473
- " ↑↓ select · enter use · esc cancel",
1474
- ]}
1475
- />
1476
- </Box>
1477
- ) : matches.length ? (
1478
- <Box flexDirection="column" paddingX={2}>
1479
- {matches.slice(firstMatch, firstMatch + menuRows).map((c) => {
1480
- const on = c === pick;
1481
- return (
1482
- <Text key={c.name} wrap="truncate-end">
1483
- <Text color={on ? "cyan" : undefined} bold={on}>
1484
- {on ? "❯ " : " "}
1485
- {usageOf(c).padEnd(column)}
1486
- </Text>
1487
- <Text dimColor>{c.about}</Text>
1488
- </Text>
1489
- );
1490
- })}
1491
- <Hint
1492
- width={hintWidth}
1493
- variants={[
1494
- ` ↑↓ select · enter run · tab complete${matches.length > menuRows ? ` · ${matches.length} commands` : ""}`,
1495
- " ↑↓ · enter run · tab complete",
1496
- ]}
1497
- />
1498
- </Box>
1499
- ) : found.length ? (
1500
- <Box flexDirection="column" paddingX={2}>
1501
- {found.slice(firstFound, firstFound + menuRows).map((p) => {
1502
- const on = p === file;
1503
- return (
1504
- <Text
1505
- key={p}
1506
- wrap="truncate-middle"
1507
- color={on ? "cyan" : p.endsWith("/") ? "blue" : undefined}
1508
- bold={on}
1509
- >
1510
- {on ? "❯ " : " "}
1511
- {p}
1512
- </Text>
1513
- );
1514
- })}
1515
- <Hint
1516
- width={hintWidth}
1517
- variants={[
1518
- " ↑↓ select · enter/tab insert the path · the file's content is sent with the message",
1519
- " ↑↓ select · enter/tab insert the path",
1520
- " ↑↓ · enter insert",
1521
- ]}
1522
- />
1523
- </Box>
1524
- ) : notice ? (
1525
- <Text color="yellow"> {notice}</Text>
1526
- ) : null}
1527
- </Box>
1772
+ }),
1773
+ h(Hint, {
1774
+ width: hintWidth,
1775
+ variants: [
1776
+ ` ↑↓ select · enter run · tab complete${matches.length > menuRows ? ` · ${matches.length} commands` : ""}`,
1777
+ " ↑↓ · enter run · tab complete",
1778
+ ],
1779
+ }),
1780
+ )
1781
+ : found.length
1782
+ ? h(
1783
+ Box,
1784
+ { flexDirection: "column", paddingX: 2 },
1785
+ found.slice(firstFound, firstFound + menuRows).map((p) => {
1786
+ const on = p === file;
1787
+ return h(
1788
+ Text,
1789
+ { key: p, wrap: "truncate-middle", color: on ? "cyan" : p.endsWith("/") ? "blue" : undefined, bold: on },
1790
+ on ? "❯ " : " ",
1791
+ p,
1792
+ );
1793
+ }),
1794
+ h(Hint, {
1795
+ width: hintWidth,
1796
+ variants: [
1797
+ " ↑↓ select · enter/tab insert the path · the file's content is sent with the message",
1798
+ " ↑↓ select · enter/tab insert the path",
1799
+ " ↑↓ · enter insert",
1800
+ ],
1801
+ }),
1802
+ )
1803
+ : notice
1804
+ ? h(Text, { color: "yellow" }, ` ${notice}`)
1805
+ : null,
1806
+ // 压在输入框上边框的右边,不占行,出现和消失时下面的内容不跳
1807
+ flash ? h(Box, { position: "absolute", top: 0, right: 2 }, h(Text, { color: "green" }, ` ${flash} `)) : null,
1528
1808
  );
1529
1809
  }
1530
1810
 
1811
+ /** 提示在输入框里只显示第一行,后面还有时加 … */
1812
+ const hintLine = (hint: string) => {
1813
+ const [first, ...rest] = hint.split("\n");
1814
+ return `${first}${rest.length ? " …" : ""}`;
1815
+ };
1816
+
1531
1817
  /** @ 补全最多显示几项 */
1532
1818
  const MENTION_ROWS = 8;
1533
1819
 
@@ -1546,6 +1832,7 @@ function SelectDialog({
1546
1832
  }: {
1547
1833
  dialog: Extract<Dialog, { kind: "select" }>;
1548
1834
  }) {
1835
+ const selection = useContext(SelectionContext);
1549
1836
  const [getSelected, setSelected] = useLive(0);
1550
1837
  const [getQuery, setQuery] = useLive("");
1551
1838
  const { rows, columns } = useScreenSize();
@@ -1563,17 +1850,16 @@ function SelectDialog({
1563
1850
  const selected = Math.min(getSelected(), Math.max(0, n - 1));
1564
1851
  // 终端矮时少显示几项,加上预览一起不超过终端的高度
1565
1852
  const visible = roomFor(rows, VISIBLE);
1566
- // 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画
1567
- const lines = dialog.body?.split("\n") ?? [];
1853
+ // 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画,底部的选项和提示也会被裁掉。
1854
+ // 按折行后的屏幕行数截断(一行长代码会占好几行),每一行单独显示、不再折行
1855
+ const lines = !dialog.body ? [] : wrap(dialog.body, Math.max(1, columns - 4));
1568
1856
  const room = Math.max(3, rows - Math.min(n, visible) - 16);
1569
1857
  const body =
1570
1858
  lines.length > room
1571
- ? [
1572
- ...lines.slice(0, room),
1573
- dim(`… ${lines.length - room} more lines`),
1574
- ].join("\n")
1575
- : dialog.body;
1859
+ ? [...lines.slice(0, room - 1), dim(`… ${lines.length - room + 1} more lines`)]
1860
+ : lines;
1576
1861
  useInput((input, key) => {
1862
+ if (key.ctrl && input === "c" && selection.active()) return selection.clear();
1577
1863
  const items = filtered();
1578
1864
  const n = items.length;
1579
1865
  const selected = Math.min(getSelected(), Math.max(0, n - 1));
@@ -1612,57 +1898,41 @@ function SelectDialog({
1612
1898
  });
1613
1899
  // 选项多时只显示选中项附近的一段
1614
1900
  const start = around(selected, n, visible);
1615
- return (
1616
- <Box
1617
- flexDirection="column"
1618
- borderStyle="round"
1619
- borderColor="yellow"
1620
- paddingX={1}
1621
- marginTop={1}
1622
- >
1623
- <Text bold wrap="truncate-end">
1624
- {dialog.title}
1625
- </Text>
1626
- {body ? <Text>{body}</Text> : null}
1627
- {filtering ? (
1628
- <Text wrap="truncate-end">
1629
- <Text color="cyan">filter› </Text>
1630
- {query ? (
1631
- <Text>{query}</Text>
1632
- ) : (
1633
- <Text dimColor>type to filter {dialog.choices.length} items</Text>
1634
- )}
1635
- </Text>
1636
- ) : null}
1637
- <Box flexDirection="column" marginTop={filtering ? 0 : 1}>
1638
- {start > 0 ? <Text dimColor> ↑ {start} more</Text> : null}
1639
- {n === 0 ? <Text dimColor> no match</Text> : null}
1640
- {items.slice(start, start + visible).map(({ c, i }, j) => {
1641
- const on = start + j === selected;
1642
- return (
1643
- <Text key={i} wrap="truncate-end">
1644
- <Text color={on ? "cyan" : undefined} bold={on}>
1645
- {on ? "❯ " : " "}
1646
- {filtering ? "" : `${i + 1}. `}
1647
- {c.label}
1648
- </Text>
1649
- {c.hint ? <Text dimColor> {c.hint}</Text> : null}
1650
- </Text>
1651
- );
1652
- })}
1653
- {start + visible < n ? (
1654
- <Text dimColor> ↓ {n - start - visible} more</Text>
1655
- ) : null}
1656
- </Box>
1657
- <Hint
1658
- width={Math.max(10, columns - 4)}
1659
- variants={
1660
- filtering
1661
- ? ["↑↓ select · type to filter · enter confirm · esc clear/cancel", "↑↓ · enter · esc"]
1662
- : ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"]
1663
- }
1664
- />
1665
- </Box>
1901
+ return h(
1902
+ Box,
1903
+ { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1 },
1904
+ h(Text, { bold: true, wrap: "truncate-end" }, dialog.title),
1905
+ body.map((l, i) => h(Text, { key: i, wrap: "truncate-end" }, l || " ")),
1906
+ filtering
1907
+ ? h(
1908
+ Text,
1909
+ { wrap: "truncate-end" },
1910
+ h(Text, { color: "cyan" }, "filter› "),
1911
+ query ? h(Text, null, query) : h(Text, { dimColor: true }, `type to filter ${dialog.choices.length} items`),
1912
+ )
1913
+ : null,
1914
+ h(
1915
+ Box,
1916
+ { flexDirection: "column", marginTop: filtering ? 0 : 1 },
1917
+ start > 0 ? h(Text, { dimColor: true }, ` ↑ ${start} more`) : null,
1918
+ n === 0 ? h(Text, { dimColor: true }, " no match") : null,
1919
+ items.slice(start, start + visible).map(({ c, i }, j) => {
1920
+ const on = start + j === selected;
1921
+ return h(
1922
+ Text,
1923
+ { key: i, wrap: "truncate-end" },
1924
+ h(Text, { color: on ? "cyan" : undefined, bold: on }, on ? "❯ " : " ", filtering ? "" : `${i + 1}. `, c.label),
1925
+ c.hint ? h(Text, { dimColor: true }, ` ${c.hint}`) : null,
1926
+ );
1927
+ }),
1928
+ start + visible < n ? h(Text, { dimColor: true }, ` ↓ ${n - start - visible} more`) : null,
1929
+ ),
1930
+ h(Hint, {
1931
+ width: Math.max(10, columns - 4),
1932
+ variants: filtering
1933
+ ? ["↑↓ select · type to filter · enter confirm · esc clear/cancel", "↑↓ · enter · esc"]
1934
+ : ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"],
1935
+ }),
1666
1936
  );
1667
1937
  }
1668
1938
 
@@ -1671,6 +1941,7 @@ function InputDialog({
1671
1941
  }: {
1672
1942
  dialog: Extract<Dialog, { kind: "input" }>;
1673
1943
  }) {
1944
+ const selection = useContext(SelectionContext);
1674
1945
  const initial = dialog.initial ?? "";
1675
1946
  const [current, setState] = useLive<EditState>({
1676
1947
  value: initial,
@@ -1682,6 +1953,7 @@ function InputDialog({
1682
1953
  setState(edit.insert(current(), text.replace(/\r?\n/g, ""))),
1683
1954
  );
1684
1955
  useInput((input, key) => {
1956
+ if (key.ctrl && input === "c" && selection.active()) return selection.clear();
1685
1957
  const state = current();
1686
1958
  if (key.return) return dialog.resolve(state.value.trim());
1687
1959
  if (key.escape || (key.ctrl && input === "c"))
@@ -1689,24 +1961,18 @@ function InputDialog({
1689
1961
  const next = editKey(state, input, key);
1690
1962
  if (next) setState(next);
1691
1963
  });
1692
- return (
1693
- <Box
1694
- flexDirection="column"
1695
- borderStyle="round"
1696
- borderColor="yellow"
1697
- paddingX={1}
1698
- marginTop={1}
1699
- >
1700
- <Text bold>{dialog.title}</Text>
1701
- <EditableText
1702
- state={state}
1703
- width={Math.max(10, columns - 6)}
1704
- prefix="› "
1705
- mask={dialog.mask}
1706
- placeholder={dialog.placeholder}
1707
- active
1708
- />
1709
- <Text dimColor>enter confirm · esc cancel</Text>
1710
- </Box>
1964
+ return h(
1965
+ Box,
1966
+ { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1 },
1967
+ h(Text, { bold: true }, dialog.title),
1968
+ h(EditableText, {
1969
+ state,
1970
+ width: Math.max(10, columns - 6),
1971
+ prefix: "› ",
1972
+ mask: dialog.mask,
1973
+ placeholder: dialog.placeholder,
1974
+ active: true,
1975
+ }),
1976
+ h(Text, { dimColor: true }, "enter confirm · esc cancel"),
1711
1977
  );
1712
1978
  }