@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,219 +1,258 @@
1
- // 终端里显示模型回复的 Markdown:标题、粗体、斜体、行内代码、链接、列表、引用、分隔线、代码块和表格。
2
- // 回复是流式到达的,按完整的行逐行处理:代码块要记住是否在块里,表格要攒齐所有行才能算列宽,
3
- // 所以做成有状态的 markdownStream。表格和分隔线跟终端宽度有关,交出去的是按宽度现排的函数。
4
-
5
- import stringWidth from "string-width";
6
- import wrapAnsi from "wrap-ansi";
7
- import { bold, cyan, dim, italic, strike, underline } from "./screen.ts";
8
-
9
- /** 排好版的一行,或者按显示宽度现排的若干行(表格、分隔线) */
10
- export type MdLine = string | { render(width: number): string[] };
11
-
12
- export interface MarkdownStream {
13
- /** 送进一个完整的行,返回可以显示的行;表格的行先攒着,等表格结束 */
14
- push(line: string): MdLine[];
15
- /** 回复结束:交出攒着的内容,恢复初始状态 */
16
- flush(): MdLine[];
17
- /** 攒着还没交出的内容,流式输出时先显示在底部 */
18
- pending(): MdLine[];
19
- }
20
-
21
- type Align = "left" | "center" | "right";
22
-
23
- const FENCE = /^\s*(`{3,}|~{3,})\s*([^`\s]*)/;
24
- const RULE = /^\s{0,3}([-*_])(\s*\1){2,}\s*$/;
25
- const DELIMITER = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
26
-
27
- export function markdownStream(): MarkdownStream {
28
- /** 所在代码块的开头标记(``` 或 ~~~),不在代码块里时为 undefined */
29
- let fence: string | undefined;
30
- /** 带 | 的一行:下一行是分隔行时它是表头,否则是普通的一行 */
31
- let candidate: string | undefined;
32
- let table: { align: Align[]; rows: string[][] } | undefined;
33
-
34
- const endTable = (): MdLine[] => {
35
- const out: MdLine[] = [];
36
- if (table) out.push(tableLine(table.rows, table.align));
37
- else if (candidate !== undefined) out.push(renderLine(candidate));
38
- table = undefined;
39
- candidate = undefined;
40
- return out;
41
- };
42
-
43
- function push(line: string): MdLine[] {
44
- if (fence) {
45
- const m = line.match(FENCE);
46
- if (m && m[1]![0] === fence[0] && m[1]!.length >= fence.length && !m[2]) {
47
- fence = undefined;
48
- return [];
49
- }
50
- return [` ${line}`];
51
- }
52
- if (table) {
53
- if (line.includes("|") && line.trim()) {
54
- table.rows.push(cells(line));
55
- return [];
56
- }
57
- return [...endTable(), ...push(line)];
58
- }
59
- if (candidate !== undefined) {
60
- if (DELIMITER.test(line) && line.includes("-")) {
61
- const header = cells(candidate);
62
- const align = cells(line).map((c): Align => (c.startsWith(":") && c.endsWith(":") ? "center" : c.endsWith(":") ? "right" : "left"));
63
- while (align.length < header.length) align.push("left");
64
- table = { align, rows: [header] };
65
- candidate = undefined;
66
- return [];
67
- }
68
- return [...endTable(), ...push(line)];
69
- }
70
- const m = line.match(FENCE);
71
- if (m) {
72
- fence = m[1]!;
73
- return m[2] ? [dim(` ${m[2]}`)] : [];
74
- }
75
- if (splitCells(line).length > 1) {
76
- candidate = line;
77
- return [];
78
- }
79
- return [renderLine(line)];
80
- }
81
-
82
- return {
83
- push,
84
- flush() {
85
- const out = endTable();
86
- fence = undefined;
87
- return out;
88
- },
89
- pending: () => (table ? [tableLine(table.rows, table.align)] : candidate !== undefined ? [renderLine(candidate)] : []),
90
- };
91
- }
92
-
93
- /** 整段 Markdown 排好版(测试和一次性显示用) */
94
- export function renderMarkdown(text: string, width: number): string[] {
95
- const md = markdownStream();
96
- const out = [...text.split("\n").flatMap((l) => md.push(l)), ...md.flush()];
97
- return out.flatMap((l) => (typeof l === "string" ? [l] : l.render(width)));
98
- }
99
-
100
- /** 代码块和表格以外的一行 */
101
- function renderLine(line: string): MdLine {
102
- if (RULE.test(line)) return { render: (width) => [dim("─".repeat(Math.max(1, Math.min(width, 80))))] };
103
- const heading = line.match(/^\s{0,3}(#{1,6})\s+(.*?)(\s+#+)?\s*$/);
104
- if (heading) {
105
- const text = inline(heading[2]!);
106
- return heading[1]!.length === 1 ? bold(underline(text)) : bold(text);
107
- }
108
- const quote = line.match(/^\s*>\s?(.*)$/);
109
- if (quote) return dim("│ ") + inline(quote[1]!);
110
- const item = line.match(/^(\s*)([-*+])\s+(\[[ xX]\]\s+)?(.*)$/);
111
- if (item) {
112
- const box = item[3] ? (item[3][1] === " " ? "☐ " : "☑ ") : "";
113
- return `${item[1]}${box ? "" : cyan("• ")}${box}${inline(item[4]!)}`;
114
- }
115
- const ordered = line.match(/^(\s*)(\d+[.)])\s+(.*)$/);
116
- if (ordered) return `${ordered[1]}${cyan(ordered[2]!)} ${inline(ordered[3]!)}`;
117
- return inline(line);
118
- }
119
-
120
- /** 行内格式:`代码`、**粗体**、*斜体*、~~删除线~~、[链接](url)、反斜杠转义。代码里的内容原样显示 */
121
- export function inline(text: string): string {
122
- return text
123
- .split(/(`+[^`]*?`+)/)
124
- .map((part, i) => {
125
- if (i % 2 === 1) return cyan(part.replace(/^`+ ?|`+$/g, "").replace(/ $/, ""));
126
- return part
127
- .replace(/\\([\\`*_{}[\]()#+\-.!|~<>])/g, (_, c: string) => `\0${c.charCodeAt(0)}\0`)
128
- .replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g, (_, label: string, url: string) =>
129
- label === url ? underline(url) : `${underline(label)}${dim(` (${url})`)}`,
130
- )
131
- .replace(/<(https?:\/\/[^>\s]+)>/g, (_, url: string) => underline(url))
132
- .replace(/\*\*(?=\S)(.+?)(?<=\S)\*\*|(?<![\w])__(?=\S)(.+?)(?<=\S)__(?![\w])/g, (_, a?: string, b?: string) => bold(a ?? b!))
133
- .replace(/(?<![*\w])\*(?=[^\s*])(.+?)(?<=[^\s*])\*(?![*\w])|(?<![_\w])_(?=[^\s_])(.+?)(?<=[^\s_])_(?![_\w])/g, (_, a?: string, b?: string) => italic(a ?? b!))
134
- .replace(/~~(?=\S)(.+?)(?<=\S)~~/g, (_, s: string) => strike(s))
135
- .replace(/\0(\d+)\0/g, (_, c: string) => String.fromCharCode(Number(c)));
136
- })
137
- .join("");
138
- }
139
-
140
- /** 按没转义、不在行内代码里的 | 拆开一行 */
141
- function splitCells(line: string): string[] {
142
- const out: string[] = [];
143
- let cell = "";
144
- let code = false;
145
- for (let i = 0; i < line.length; i++) {
146
- const c = line[i]!;
147
- if (c === "\\" && line[i + 1] === "|") {
148
- cell += "|";
149
- i++;
150
- } else if (c === "`") {
151
- code = !code;
152
- cell += c;
153
- } else if (c === "|" && !code) {
154
- out.push(cell);
155
- cell = "";
156
- } else cell += c;
157
- }
158
- out.push(cell);
159
- return out;
160
- }
161
-
162
- /** 表格一行的各个单元格,去掉两端的 | */
163
- function cells(line: string): string[] {
164
- const parts = splitCells(line.trim());
165
- if (parts.length > 1 && parts[0]!.trim() === "") parts.shift();
166
- if (parts.length > 1 && parts.at(-1)!.trim() === "") parts.pop();
167
- return parts.map((c) => c.trim());
168
- }
169
-
170
- function tableLine(rows: string[][], align: Align[]): MdLine {
171
- const shown = rows.map((r, i) => r.map((c) => (i === 0 ? bold(inline(c)) : inline(c))));
172
- return { render: (width) => renderTable(shown, align, width) };
173
- }
174
-
175
- /**
176
- * 画带边框的表格。放不下时按比例收窄较宽的列、单元格内折行;
177
- * 窄到每列连 3 列都分不到时,改成每行一段「表头: 值」
178
- */
179
- function renderTable(rows: string[][], align: Align[], width: number): string[] {
180
- const n = Math.max(...rows.map((r) => r.length));
181
- const grid = rows.map((r) => Array.from({ length: n }, (_, i) => r[i] ?? ""));
182
- const natural = Array.from({ length: n }, (_, i) => Math.max(1, ...grid.map((r) => stringWidth(r[i]!))));
183
- const room = width - (3 * n + 1);
184
- if (room < 3 * n) {
185
- const [head, ...body] = grid;
186
- return body.flatMap((r, k) => [
187
- ...(k ? [""] : []),
188
- ...r.flatMap((c, i) => wrapAnsi(`${head![i]}${dim(":")} ${c}`, Math.max(1, width), { hard: true }).split("\n")),
189
- ]);
190
- }
191
- // 从最窄的列开始,能放下的列按原宽,放不下的平分剩下的宽度
192
- const widths = [...natural];
193
- if (natural.reduce((a, b) => a + b, 0) > room) {
194
- let left = room;
195
- let open = n;
196
- for (const i of [...natural.keys()].sort((a, b) => natural[a]! - natural[b]!)) {
197
- const share = Math.floor(left / open);
198
- widths[i] = Math.min(natural[i]!, share);
199
- left -= widths[i]!;
200
- open--;
201
- }
202
- }
203
- const pad = (s: string, w: number, a: Align) => {
204
- const gap = w - stringWidth(s);
205
- if (a === "right") return " ".repeat(gap) + s;
206
- if (a === "center") return " ".repeat(Math.floor(gap / 2)) + s + " ".repeat(Math.ceil(gap / 2));
207
- return s + " ".repeat(gap);
208
- };
209
- const border = (l: string, m: string, r: string) => dim(l + widths.map((w) => "─".repeat(w + 2)).join(m) + r);
210
- const row = (r: string[]) => {
211
- const parts = r.map((c, i) => wrapAnsi(c, widths[i]!, { hard: true, trim: true }).split("\n"));
212
- const height = Math.max(...parts.map((p) => p.length));
213
- return Array.from({ length: height }, (_, k) =>
214
- dim("│ ") + parts.map((p, i) => pad(p[k] ?? "", widths[i]!, align[i] ?? "left")).join(dim(" │ ")) + dim(" │"),
215
- );
216
- };
217
- const [head, ...body] = grid;
218
- return [border("┌", "┬", "┐"), ...row(head!), border("├", "┼", "┤"), ...body.flatMap(row), border("└", "┴", "┘")];
219
- }
1
+ // 终端里显示模型回复的 Markdown:标题、粗体、斜体、行内代码、链接、列表、引用、分隔线、代码块和表格。
2
+ // 回复是流式到达的,按完整的行逐行处理:代码块要记住是否在块里,表格要攒齐所有行才能算列宽,
3
+ // 所以做成有状态的 markdownStream。表格和分隔线跟终端宽度有关,交出去的是按宽度现排的函数。
4
+
5
+ import stringWidth from "string-width";
6
+ import wrapAnsi from "wrap-ansi";
7
+ import { bold, cyan, dim, italic, strike, underline } from "./screen.ts";
8
+
9
+ /** 排好版的一行,或者按显示宽度现排的若干行(表格、分隔线) */
10
+ export type MdLine = string | { render(width: number): string[] };
11
+
12
+ export interface MarkdownStream {
13
+ /** 送进一个完整的行,返回可以显示的行;表格的行先攒着,等表格结束 */
14
+ push(line: string): MdLine[];
15
+ /** 回复结束:交出攒着的内容,恢复初始状态 */
16
+ flush(): MdLine[];
17
+ /** 攒着还没交出的内容,流式输出时先显示在底部 */
18
+ pending(): MdLine[];
19
+ }
20
+
21
+ type Align = "left" | "center" | "right";
22
+
23
+ const FENCE = /^\s*(`{3,}|~{3,})\s*([^`\s]*)/;
24
+ const RULE = /^\s{0,3}([-*_])(\s*\1){2,}\s*$/;
25
+ const DELIMITER = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
26
+
27
+ export function markdownStream(): MarkdownStream {
28
+ /** 所在代码块的开头标记(``` 或 ~~~),不在代码块里时为 undefined */
29
+ let fence: string | undefined;
30
+ /** 带 | 的一行:下一行是分隔行时它是表头,否则是普通的一行 */
31
+ let candidate: string | undefined;
32
+ let table: { align: Align[]; rows: string[][] } | undefined;
33
+
34
+ const endTable = (): MdLine[] => {
35
+ const out: MdLine[] = [];
36
+ if (table) out.push(tableLine(table.rows, table.align));
37
+ else if (candidate !== undefined) out.push(renderLine(candidate));
38
+ table = undefined;
39
+ candidate = undefined;
40
+ return out;
41
+ };
42
+
43
+ function push(line: string): MdLine[] {
44
+ if (fence) {
45
+ const m = line.match(FENCE);
46
+ if (m && m[1]![0] === fence[0] && m[1]!.length >= fence.length && !m[2]) {
47
+ fence = undefined;
48
+ return [];
49
+ }
50
+ // 左边一条竖线和正文分开;长行折行后续行也带竖线
51
+ return [hanging(dim("┃ "), line, dim("┃ "))];
52
+ }
53
+ if (table) {
54
+ if (line.includes("|") && line.trim()) {
55
+ table.rows.push(cells(line));
56
+ return [];
57
+ }
58
+ return [...endTable(), ...push(line)];
59
+ }
60
+ if (candidate !== undefined) {
61
+ if (DELIMITER.test(line) && line.includes("-")) {
62
+ const header = cells(candidate);
63
+ const align = cells(line).map((c): Align => (c.startsWith(":") && c.endsWith(":") ? "center" : c.endsWith(":") ? "right" : "left"));
64
+ while (align.length < header.length) align.push("left");
65
+ table = { align, rows: [header] };
66
+ candidate = undefined;
67
+ return [];
68
+ }
69
+ return [...endTable(), ...push(line)];
70
+ }
71
+ const m = line.match(FENCE);
72
+ if (m) {
73
+ fence = m[1]!;
74
+ return m[2] ? [dim(`┃ ${m[2]}`)] : [];
75
+ }
76
+ if (splitCells(line).length > 1) {
77
+ candidate = line;
78
+ return [];
79
+ }
80
+ return [renderLine(line)];
81
+ }
82
+
83
+ return {
84
+ push,
85
+ flush() {
86
+ const out = endTable();
87
+ fence = undefined;
88
+ return out;
89
+ },
90
+ pending: () => (table ? [tableLine(table.rows, table.align)] : candidate !== undefined ? [renderLine(candidate)] : []),
91
+ };
92
+ }
93
+
94
+ /** 整段 Markdown 排好版(测试和一次性显示用) */
95
+ export function renderMarkdown(text: string, width: number): string[] {
96
+ const md = markdownStream();
97
+ const out = [...text.split("\n").flatMap((l) => md.push(l)), ...md.flush()];
98
+ return out.flatMap((l) => (typeof l === "string" ? [l] : l.render(width)));
99
+ }
100
+
101
+ /** 代码块和表格以外的一行 */
102
+ function renderLine(line: string): MdLine {
103
+ if (RULE.test(line)) return { render: (width) => [dim("─".repeat(Math.max(1, Math.min(width, 80))))] };
104
+ const heading = line.match(/^\s{0,3}(#{1,6})\s+(.*?)(\s+#+)?\s*$/);
105
+ if (heading) {
106
+ const text = inline(heading[2]!);
107
+ return heading[1]!.length === 1 ? bold(underline(text)) : bold(text);
108
+ }
109
+ const quote = line.match(/^\s*>\s?(.*)$/);
110
+ if (quote) return hanging(dim("│ "), inline(quote[1]!), dim("│ "));
111
+ const item = line.match(/^(\s*)([-*+])\s+(\[[ xX]\]\s+)?(.*)$/);
112
+ // 列表项折行时续行和正文对齐
113
+ if (item) {
114
+ const box = item[3] ? (item[3][1] === " " ? "☐ " : "☑ ") : "";
115
+ return hanging(`${item[1]}${box ? "" : cyan("• ")}${box}`, inline(item[4]!));
116
+ }
117
+ const ordered = line.match(/^(\s*)(\d+[.)])\s+(.*)$/);
118
+ if (ordered) return hanging(`${ordered[1]}${cyan(ordered[2]!)} `, inline(ordered[3]!));
119
+ return inline(line);
120
+ }
121
+
122
+ /**
123
+ * 行内格式:`代码`、**粗体**、*斜体*、~~删除线~~、[链接](url)、反斜杠转义。代码里的内容原样显示。
124
+ * 行内代码先换成占位符,粗体、斜体处理完再换回来,"**用 `foo()` 实现**" 这样跨过代码的粗体才认得出
125
+ */
126
+ export function inline(text: string): string {
127
+ const codes: string[] = [];
128
+ return text
129
+ .replace(/`+[^`]*?`+/g, (code) => `\x01${codes.push(cyan(code.replace(/^`+ ?|`+$/g, "").replace(/ $/, ""))) - 1}\x01`)
130
+ .replace(/\\([\\`*_{}[\]()#+\-.!|~<>])/g, (_, c: string) => `\0${c.charCodeAt(0)}\0`)
131
+ .replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g, (_, label: string, url: string) =>
132
+ label === url ? underline(url) : `${underline(label)}${dim(` (${url})`)}`,
133
+ )
134
+ .replace(/<(https?:\/\/[^>\s]+)>/g, (_, url: string) => underline(url))
135
+ .replace(/\*\*(?=\S)(.+?)(?<=\S)\*\*|(?<![\w])__(?=\S)(.+?)(?<=\S)__(?![\w])/g, (_, a?: string, b?: string) => bold(a ?? b!))
136
+ .replace(/(?<![*\w])\*(?=[^\s*])(.+?)(?<=[^\s*])\*(?![*\w])|(?<![_\w])_(?=[^\s_])(.+?)(?<=[^\s_])_(?![_\w])/g, (_, a?: string, b?: string) => italic(a ?? b!))
137
+ .replace(/~~(?=\S)(.+?)(?<=\S)~~/g, (_, s: string) => strike(s))
138
+ .replace(/\0(\d+)\0/g, (_, c: string) => String.fromCharCode(Number(c)))
139
+ .replace(/\x01(\d+)\x01/g, (_, i: string) => codes[Number(i)]!);
140
+ }
141
+
142
+ /** 制表符换成空格,按 stop 列一个制表位对齐(按显示宽度算,忽略颜色)。终端按 8 列显示 tab,按 0 列算宽度会对不齐、超宽 */
143
+ export function expandTabs(text: string, stop = 4): string {
144
+ if (!text.includes("\t")) return text;
145
+ return text
146
+ .split("\n")
147
+ .map((line) => {
148
+ if (!line.includes("\t")) return line;
149
+ let out = "";
150
+ let col = 0;
151
+ const parts = line.split("\t");
152
+ parts.forEach((p, i) => {
153
+ out += p;
154
+ col += stringWidth(p);
155
+ if (i < parts.length - 1) {
156
+ const n = stop - (col % stop);
157
+ out += " ".repeat(n);
158
+ col += n;
159
+ }
160
+ });
161
+ return out;
162
+ })
163
+ .join("\n");
164
+ }
165
+
166
+ /** 折行时续行缩进到 head 之后(列表项的正文、代码块的竖线);rest 是续行开头显示的内容,宽度和 head 相同 */
167
+ function hanging(head: string, body: string, rest = " ".repeat(stringWidth(head))): MdLine {
168
+ return {
169
+ render: (width) => {
170
+ const room = Math.max(1, width - stringWidth(head));
171
+ return wrapAnsi(expandTabs(body), room, { hard: true, trim: false })
172
+ .split("\n")
173
+ // 折行处留下的行尾空格看不见,去掉免得多占一列
174
+ .map((r, i) => (i === 0 ? head : rest) + r.trimEnd());
175
+ },
176
+ };
177
+ }
178
+
179
+ /** 按没转义、不在行内代码里的 | 拆开一行 */
180
+ function splitCells(line: string): string[] {
181
+ const out: string[] = [];
182
+ let cell = "";
183
+ let code = false;
184
+ for (let i = 0; i < line.length; i++) {
185
+ const c = line[i]!;
186
+ if (c === "\\" && line[i + 1] === "|") {
187
+ cell += "|";
188
+ i++;
189
+ } else if (c === "`") {
190
+ code = !code;
191
+ cell += c;
192
+ } else if (c === "|" && !code) {
193
+ out.push(cell);
194
+ cell = "";
195
+ } else cell += c;
196
+ }
197
+ out.push(cell);
198
+ return out;
199
+ }
200
+
201
+ /** 表格一行的各个单元格,去掉两端的 | */
202
+ function cells(line: string): string[] {
203
+ const parts = splitCells(line.trim());
204
+ if (parts.length > 1 && parts[0]!.trim() === "") parts.shift();
205
+ if (parts.length > 1 && parts.at(-1)!.trim() === "") parts.pop();
206
+ return parts.map((c) => c.trim());
207
+ }
208
+
209
+ function tableLine(rows: string[][], align: Align[]): MdLine {
210
+ const shown = rows.map((r, i) => r.map((c) => (i === 0 ? bold(inline(c)) : inline(c))));
211
+ return { render: (width) => renderTable(shown, align, width) };
212
+ }
213
+
214
+ /**
215
+ * 画带边框的表格。放不下时按比例收窄较宽的列、单元格内折行;
216
+ * 窄到每列连 3 列都分不到时,改成每行一段「表头: 值」
217
+ */
218
+ function renderTable(rows: string[][], align: Align[], width: number): string[] {
219
+ const n = Math.max(...rows.map((r) => r.length));
220
+ const grid = rows.map((r) => Array.from({ length: n }, (_, i) => r[i] ?? ""));
221
+ const natural = Array.from({ length: n }, (_, i) => Math.max(1, ...grid.map((r) => stringWidth(r[i]!))));
222
+ const room = width - (3 * n + 1);
223
+ if (room < 3 * n) {
224
+ const [head, ...body] = grid;
225
+ return body.flatMap((r, k) => [
226
+ ...(k ? [""] : []),
227
+ ...r.flatMap((c, i) => wrapAnsi(`${head![i]}${dim(":")} ${c}`, Math.max(1, width), { hard: true }).split("\n")),
228
+ ]);
229
+ }
230
+ // 从最窄的列开始,能放下的列按原宽,放不下的平分剩下的宽度
231
+ const widths = [...natural];
232
+ if (natural.reduce((a, b) => a + b, 0) > room) {
233
+ let left = room;
234
+ let open = n;
235
+ for (const i of [...natural.keys()].sort((a, b) => natural[a]! - natural[b]!)) {
236
+ const share = Math.floor(left / open);
237
+ widths[i] = Math.min(natural[i]!, share);
238
+ left -= widths[i]!;
239
+ open--;
240
+ }
241
+ }
242
+ const pad = (s: string, w: number, a: Align) => {
243
+ const gap = w - stringWidth(s);
244
+ if (a === "right") return " ".repeat(gap) + s;
245
+ if (a === "center") return " ".repeat(Math.floor(gap / 2)) + s + " ".repeat(Math.ceil(gap / 2));
246
+ return s + " ".repeat(gap);
247
+ };
248
+ const border = (l: string, m: string, r: string) => dim(l + widths.map((w) => "─".repeat(w + 2)).join(m) + r);
249
+ const row = (r: string[]) => {
250
+ const parts = r.map((c, i) => wrapAnsi(c, widths[i]!, { hard: true, trim: true }).split("\n"));
251
+ const height = Math.max(...parts.map((p) => p.length));
252
+ return Array.from({ length: height }, (_, k) =>
253
+ dim("│ ") + parts.map((p, i) => pad(p[k] ?? "", widths[i]!, align[i] ?? "left")).join(dim(" │ ")) + dim(" │"),
254
+ );
255
+ };
256
+ const [head, ...body] = grid;
257
+ return [border("┌", "┬", "┐"), ...row(head!), border("├", "┼", "┤"), ...body.flatMap(row), border("└", "┴", "┘")];
258
+ }
@@ -0,0 +1,113 @@
1
+ // @ 引用文件的补全:输入框里打 @ 时找出光标处正在输入的路径、给候选路径排序。
2
+ // 候选从工作目录来(git 仓库里只看版本控制的,和 glob 一样),发送时附上文件内容由 jade/mentions 负责。
3
+
4
+ import { execFile } from "node:child_process";
5
+ import { readdir } from "node:fs/promises";
6
+ import path from "node:path";
7
+
8
+ /** 候选(文件加目录)最多这么多,再多也只是拖慢补全 */
9
+ const MAX_CANDIDATES = 20_000;
10
+
11
+ /** 光标正在输入的 @ 引用:@ 的下标和已经输入的部分;不在引用里时返回 undefined */
12
+ export function mentionAt(value: string, cursor: number): { start: number; query: string } | undefined {
13
+ const m = /(?:^|\s)@("?[^\s"]*)$/.exec(value.slice(0, cursor));
14
+ if (!m) return undefined;
15
+ return { start: cursor - m[1]!.length - 1, query: m[1]!.replace(/^"/, "") };
16
+ }
17
+
18
+ /** 补全后的文本:@ 引用加一个空格,含空白的路径加引号 */
19
+ export const mentionText = (p: string) => (/\s/.test(p) ? `@"${p}"` : `@${p}`);
20
+
21
+ /**
22
+ * 按 query 给候选路径排序:文件名以 query 开头的最前,其次文件名包含、路径包含,
23
+ * 最后是按顺序出现 query 每个字符的模糊匹配;同一档里短的在前
24
+ */
25
+ export function rankPaths(paths: string[], query: string, limit: number): string[] {
26
+ const q = query.toLowerCase().replaceAll("\\", "/");
27
+ if (!q) return paths.filter((p) => !p.slice(0, -1).includes("/")).slice(0, limit);
28
+ const scored: { p: string; score: number }[] = [];
29
+ for (const p of paths) {
30
+ const lower = p.toLowerCase();
31
+ const base = lower.replace(/\/$/, "").split("/").pop()!;
32
+ let score: number;
33
+ if (lower.startsWith(q)) score = 0;
34
+ else if (base.startsWith(q)) score = 1;
35
+ else if (base.includes(q)) score = 2;
36
+ else if (lower.includes(q)) score = 3;
37
+ else if (subsequence(lower, q)) score = 4;
38
+ else continue;
39
+ scored.push({ p, score });
40
+ }
41
+ scored.sort((a, b) => a.score - b.score || a.p.length - b.p.length || a.p.localeCompare(b.p));
42
+ return scored.slice(0, limit).map((s) => s.p);
43
+ }
44
+
45
+ function subsequence(text: string, q: string): boolean {
46
+ let i = 0;
47
+ for (const ch of text) if (ch === q[i] && ++i === q.length) return true;
48
+ return false;
49
+ }
50
+
51
+ /**
52
+ * @ 补全的候选:工作目录里的文件(git 仓库里只看版本控制的)和它们所在的目录,目录以 / 结尾。
53
+ * 5 秒还没列完就先给已经找到的(大仓库第一次列得慢,不该让输入框卡住)
54
+ */
55
+ export async function mentionCandidates(cwd: string, signal: AbortSignal = AbortSignal.timeout(5000)): Promise<string[]> {
56
+ const files: string[] = [];
57
+ const dirs = new Set<string>();
58
+ try {
59
+ for await (const full of await listFiles(cwd, signal)) {
60
+ const rel = display(cwd, full);
61
+ files.push(rel);
62
+ for (let i = rel.lastIndexOf("/"); i > 0; i = rel.lastIndexOf("/", i - 1)) {
63
+ const dir = rel.slice(0, i + 1);
64
+ if (dirs.has(dir)) break;
65
+ dirs.add(dir);
66
+ }
67
+ if (files.length + dirs.size >= MAX_CANDIDATES) break;
68
+ }
69
+ } catch {
70
+ // 列到一半被中止(超时):用已经找到的
71
+ }
72
+ return [...dirs, ...files];
73
+ }
74
+
75
+ // 以下和 jade/fs 的 listFiles、display 相同:插件之间不共享代码(只能共享类型),所以这里留一份
76
+
77
+ const SKIP_DIRS = new Set([".git", "node_modules"]);
78
+
79
+ /** 显示用:工作目录内的路径用相对形式,统一为正斜杠 */
80
+ function display(cwd: string, full: string): string {
81
+ const rel = path.relative(cwd, full);
82
+ const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
83
+ return shown.split(path.sep).join("/");
84
+ }
85
+
86
+ /** git 仓库里的文件(版本控制的和没忽略的新文件);不是仓库、没有 git 时 undefined */
87
+ function gitFiles(root: string): Promise<string[] | undefined> {
88
+ return new Promise((resolve) => {
89
+ execFile("git", ["ls-files", "-z", "--cached", "--others", "--exclude-standard"], { cwd: root, maxBuffer: 256 * 1024 * 1024, windowsHide: true }, (err, out) =>
90
+ // 合并冲突中的文件会出现多次
91
+ resolve(err ? undefined : [...new Set(out.split("\0").filter(Boolean))]),
92
+ );
93
+ });
94
+ }
95
+
96
+ async function listFiles(root: string, signal: AbortSignal): Promise<Iterable<string> | AsyncIterable<string>> {
97
+ const tracked = await gitFiles(root);
98
+ if (!tracked) return walk(root, signal);
99
+ return tracked.filter((rel) => !rel.split("/").some((seg) => SKIP_DIRS.has(seg))).map((rel) => path.join(root, rel));
100
+ }
101
+
102
+ async function* walk(dir: string, signal: AbortSignal): AsyncIterable<string> {
103
+ signal.throwIfAborted();
104
+ const entries = (await readdir(dir, { withFileTypes: true })).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
105
+ for (const e of entries) {
106
+ const full = path.join(dir, e.name);
107
+ if (e.isDirectory()) {
108
+ if (!SKIP_DIRS.has(e.name)) yield* walk(full, signal);
109
+ } else if (e.isFile()) {
110
+ yield full;
111
+ }
112
+ }
113
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "id": "jade/tui",
3
+ "version": "0.1.0",
4
+ "api": "^1",
5
+ "entry": "./index.ts",
6
+ "provides": ["frontend@1", "ui@1"],
7
+ "requires": ["commands@1", "keys@1", "renderers@1"],
8
+ "optional": ["modes@1", "models@1", "compact@1", "sessions@1"],
9
+ "frontend": { "name": "tui", "default": true }
10
+ }