@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
package/src/session.ts ADDED
@@ -0,0 +1,458 @@
1
+ // 会话:uncarved Runtime 的创建、恢复、切换(4.2),会话级作用域,afterTurn / send(4.3),兜底关卡(第 7 节)。
2
+
3
+ import path from "node:path";
4
+ import { JsonlLogStore } from "@uncarved/store-jsonl";
5
+ import {
6
+ checkCompat,
7
+ readAll,
8
+ definePlugin as kernelPlugin,
9
+ Runtime,
10
+ type GateResult,
11
+ type Layer,
12
+ type LocalHookSpec,
13
+ type LogEvent,
14
+ type LogStore,
15
+ type Plugin,
16
+ type Provider,
17
+ type RemoteHookSpec,
18
+ type RuntimeNotice,
19
+ type State,
20
+ type Tool,
21
+ type UserInput,
22
+ } from "uncarved";
23
+ import {
24
+ API_VERSION,
25
+ PluginError,
26
+ ServiceUnavailable,
27
+ StaleContext,
28
+ type OpenOptions,
29
+ type PluginModule,
30
+ type PluginSource,
31
+ type Secrets,
32
+ type Session,
33
+ type SessionContext,
34
+ type SessionManager,
35
+ type SessionUI,
36
+ type UI,
37
+ } from "./api.ts";
38
+ import { ambient, invoke, Scope, type Owner } from "./membrane.ts";
39
+ import { scopedSecrets } from "./secrets.ts";
40
+
41
+ /** 核心自己交给内核的插件:兜底关卡 */
42
+ export const CORE_PLUGIN_ID = "jade/core";
43
+
44
+ /** 兜底关卡拒绝时给模型看的话 */
45
+ export const NO_APPROVAL_REASON =
46
+ "No permission plugin is active in this session, so only read-only tools may run. Ask the user to enable an approval plugin.";
47
+
48
+ /** 会话管理器向宿主要的东西 */
49
+ export interface SessionHost {
50
+ home: string;
51
+ cwd: string;
52
+ config(source: PluginSource): Readonly<Record<string, unknown>>;
53
+ /** 当前激活的插件,按激活顺序 */
54
+ active(): { source: PluginSource; module: PluginModule; owner: Owner; mark?: (p: Plugin) => Plugin }[];
55
+ /** 以 owner 的身份取服务;核心自己取时 owner 是核心 */
56
+ service<T extends object>(name: string, caller: Owner): T | undefined;
57
+ /** 服务的提供者 id */
58
+ provider(name: string): string | undefined;
59
+ /** 一个插件的密钥(第 8 节);会话 ctx 和进程 ctx 共享同一份 */
60
+ secrets(plugin: string): Secrets;
61
+ core: Owner;
62
+ /** 插件的会话作用域:宿主隔离插件时一并关闭 */
63
+ track(plugin: string, scope: Scope): void;
64
+ untrack(plugin: string, scope: Scope): void;
65
+ fault(plugin: string, reason: string): void;
66
+ report(message: string): void;
67
+ }
68
+
69
+ const KERNEL_LAYER: Record<PluginSource["layer"], Layer> = { bundled: "distro", user: "user", project: "project" };
70
+
71
+ interface Part {
72
+ source: PluginSource;
73
+ owner: Owner;
74
+ gates: number;
75
+ kernel: Plugin;
76
+ }
77
+
78
+ class LiveSession implements Session {
79
+ readonly id: string;
80
+ readonly parts: Part[] = [];
81
+ rt!: Runtime;
82
+ #queue: { input: UserInput; resolve: () => void; reject: (e: unknown) => void }[] = [];
83
+ #after: { fn: () => void | Promise<void>; owner: Owner }[] = [];
84
+ #pump: Promise<void> | undefined;
85
+ /** 离开时的原因;有了以后不再接受输入 */
86
+ #left: Error | undefined;
87
+ #report: (m: string) => void;
88
+
89
+ readonly #store: LogStore;
90
+
91
+ constructor(id: string, store: LogStore, report: (m: string) => void) {
92
+ this.id = id;
93
+ this.#store = store;
94
+ this.#report = report;
95
+ }
96
+
97
+ history(): Promise<LogEvent[]> {
98
+ return readAll(this.#store, this.id);
99
+ }
100
+
101
+ get state(): State {
102
+ return this.rt.state;
103
+ }
104
+
105
+ send(input: UserInput): Promise<void> {
106
+ if (this.#left) return Promise.reject(this.#left);
107
+ return new Promise((resolve, reject) => {
108
+ this.#queue.push({ input, resolve, reject });
109
+ this.#kick();
110
+ });
111
+ }
112
+
113
+ afterTurn(fn: () => void | Promise<void>, owner: Owner): void {
114
+ if (this.#left) return;
115
+ this.#after.push({ fn, owner });
116
+ this.#kick();
117
+ }
118
+
119
+ /** 离开会话:还没开始的输入全部拒绝、afterTurn 不再执行,再中止正在跑的一轮。之后的 send 直接拒绝 */
120
+ leave(): void {
121
+ this.#left ??= new Error(`session ${this.id} was closed before this input ran`);
122
+ for (const job of this.#queue.splice(0)) job.reject(this.#left);
123
+ this.#after.length = 0;
124
+ this.rt.abort(this.#left);
125
+ }
126
+
127
+ async idle(): Promise<void> {
128
+ while (this.#pump) await this.#pump;
129
+ }
130
+
131
+ recover(): Promise<void> {
132
+ return this.rt.recover();
133
+ }
134
+
135
+ abort(reason?: unknown): void {
136
+ this.rt.abort(reason);
137
+ }
138
+
139
+ subscribe(fn: (n: RuntimeNotice) => void): () => void {
140
+ return this.rt.subscribe(fn);
141
+ }
142
+
143
+ #kick(): void {
144
+ this.#pump ??= this.#run().finally(() => {
145
+ this.#pump = undefined;
146
+ // #run 决定退出到这里之间排进来的输入:那时 #pump 还在,send 没有启动新的处理
147
+ if (this.#queue.length > 0 || this.#after.length > 0) this.#kick();
148
+ });
149
+ }
150
+
151
+ /** 一次处理一条输入;每轮之后执行 afterTurn,它们可能又排进新的输入 */
152
+ async #run(): Promise<void> {
153
+ await null; // 让调用方先拿到 send 的 Promise
154
+ for (;;) {
155
+ const job = this.#queue.shift();
156
+ if (job) {
157
+ try {
158
+ await this.rt.send(job.input);
159
+ job.resolve();
160
+ } catch (e) {
161
+ job.reject(e);
162
+ }
163
+ }
164
+ if (this.#after.length > 0) await this.#runAfter();
165
+ else if (!job) return;
166
+ }
167
+ }
168
+
169
+ async #runAfter(): Promise<void> {
170
+ const batch = this.#after.splice(0);
171
+ for (const { fn, owner } of batch) {
172
+ if (owner.scope.closed) continue;
173
+ try {
174
+ await invoke(owner, "afterTurn", fn);
175
+ } catch (e) {
176
+ this.#report(e instanceof Error ? e.message : String(e));
177
+ }
178
+ }
179
+ }
180
+ }
181
+
182
+ export class Sessions implements SessionManager {
183
+ readonly #host: SessionHost;
184
+ #current: LiveSession | undefined;
185
+ #store: LogStore | undefined;
186
+ /** 换会话串行进行 */
187
+ #switching: Promise<unknown> = Promise.resolve();
188
+
189
+ constructor(host: SessionHost) {
190
+ this.#host = host;
191
+ }
192
+
193
+ get current(): Session | undefined {
194
+ return this.#current;
195
+ }
196
+
197
+ /**
198
+ * 新建或恢复一个会话,成功后它成为当前会话,旧会话离开(detach,仍可恢复)。
199
+ * 是一个事务:任何一步失败,新会话的作用域全部撤销,旧会话不受影响(4.2)。
200
+ * 单个插件的 session() 失败不算:这个插件不参加本会话,会话照常打开
201
+ */
202
+ open(opts: OpenOptions = {}): Promise<Session> {
203
+ const run = this.#switching.then(() => this.#open(opts));
204
+ this.#switching = run.catch(() => {});
205
+ return run;
206
+ }
207
+
208
+ /** 当前会话离开(detach,仍可恢复) */
209
+ async close(): Promise<void> {
210
+ await this.#switching;
211
+ const old = this.#current;
212
+ this.#current = undefined;
213
+ if (old) await this.#leave(old);
214
+ }
215
+
216
+ async #open(opts: OpenOptions): Promise<Session> {
217
+ const host = this.#host;
218
+ const provider = host.service<Provider>("provider@1", host.core);
219
+ if (!provider) throw new ServiceUnavailable("provider@1", "(none): no active plugin provides a model");
220
+ const store = host.service<LogStore>("store@1", host.core) ?? (this.#store ??= new JsonlLogStore(path.join(host.home, "sessions")));
221
+
222
+ const s = new LiveSession(opts.resume ?? crypto.randomUUID(), store, host.report);
223
+ try {
224
+ for (const p of host.active()) if (p.module.session) await this.#join(s, p);
225
+ const plugins = [this.#corePlugin(s), ...s.parts.map((p) => p.kernel)];
226
+ s.rt = opts.resume
227
+ ? await Runtime.resume({ store, provider, plugins, sessionId: s.id })
228
+ : await Runtime.create({ store, provider, plugins, sessionId: s.id });
229
+ } catch (e) {
230
+ await this.#closeParts(s);
231
+ throw e;
232
+ }
233
+ s.rt.subscribe((n) => {
234
+ if (n.type === "hook-error") host.report(`plugin ${n.info.plugin} failed at ${n.info.point}: ${n.info.error instanceof Error ? n.info.error.message : String(n.info.error)}`);
235
+ });
236
+ // 提交:新会话成为当前会话,然后旧会话离开
237
+ const old = this.#current;
238
+ this.#current = s;
239
+ if (old) await this.#leave(old);
240
+ return s;
241
+ }
242
+
243
+ /** 一个插件加入会话:执行它的 session(),失败就撤销它这次的注册 */
244
+ async #join(s: LiveSession, p: { source: PluginSource; module: PluginModule; owner: Owner; mark?: (p: Plugin) => Plugin }): Promise<void> {
245
+ const host = this.#host;
246
+ const { manifest, layer } = p.source;
247
+ const id = manifest.id;
248
+ const scope = new Scope(id);
249
+ const owner: Owner = { plugin: id, scope };
250
+ const tools: Tool[] = [];
251
+ const hooks: (LocalHookSpec | RemoteHookSpec)[] = [];
252
+ let open = true;
253
+ const declared = (name: string, lists: (readonly string[] | undefined)[]) => {
254
+ if (scope.closed) throw new StaleContext(id);
255
+ if (!lists.some((l) => l?.includes(name))) throw new Error(`plugin ${id}: ${name} is not declared in plugin.json`);
256
+ };
257
+ // 前端给这个插件在这个会话里的状态栏段落和输入框(4.3)。界面的东西出问题不连累插件:调用失败当作没有
258
+ const sessionUI = this.#sessionUI(s, id, owner);
259
+ if (sessionUI) {
260
+ try {
261
+ scope.add(() => sessionUI.dispose());
262
+ } catch {
263
+ // 作用域已经关了:没有要清理的
264
+ }
265
+ }
266
+ const uiCall = (fn: () => void) => {
267
+ try {
268
+ fn();
269
+ } catch {
270
+ // 前端不在了(例如它的作用域已关闭)
271
+ }
272
+ };
273
+ const ctx: SessionContext = {
274
+ plugin: id,
275
+ signal: scope.signal,
276
+ cwd: host.cwd,
277
+ home: host.home,
278
+ config: host.config(p.source),
279
+ secrets: scopedSecrets(host.secrets(id), () => scope.closed, id),
280
+ ...(sessionUI
281
+ ? {
282
+ status: (text: string | undefined) => uiCall(() => sessionUI.status(text === undefined ? undefined : String(text))),
283
+ ...(sessionUI.draft ? { input: sessionUI.draft } : {}),
284
+ }
285
+ : {}),
286
+ require: <T extends object>(name: string): T => {
287
+ declared(name, [manifest.requires]);
288
+ const h = host.service<T>(name, owner);
289
+ if (!h) throw new ServiceUnavailable(name, "(none)");
290
+ return h;
291
+ },
292
+ service: <T extends object>(name: string): T | undefined => {
293
+ declared(name, [manifest.requires, manifest.optional]);
294
+ return host.service<T>(name, owner);
295
+ },
296
+ effect: async (start) => {
297
+ if (scope.closed) throw new StaleContext(id);
298
+ const d = await ambient.run(owner, start);
299
+ if (typeof d === "function") scope.add(d);
300
+ },
301
+ session: {
302
+ id: s.id,
303
+ afterTurn: (fn) => {
304
+ if (scope.closed) throw new StaleContext(id);
305
+ s.afterTurn(fn, owner);
306
+ },
307
+ send: (input) => {
308
+ if (scope.closed) throw new StaleContext(id);
309
+ if (layer === "project") throw new Error(`plugin ${id}: project plugins cannot send input on the user's behalf`);
310
+ return s.send(input);
311
+ },
312
+ abort: (reason) => {
313
+ if (scope.closed) throw new StaleContext(id);
314
+ if (layer === "project") throw new Error(`plugin ${id}: project plugins cannot stop turns on the user's behalf`);
315
+ s.abort(new Error(reason ?? `stopped by plugin ${id}`));
316
+ },
317
+ },
318
+ kernel: {
319
+ tool: (t) => {
320
+ if (!open) throw new Error(`plugin ${id}: tools can only be registered while session() runs`);
321
+ tools.push(t);
322
+ },
323
+ hook: (h) => {
324
+ if (!open) throw new Error(`plugin ${id}: hooks can only be registered while session() runs`);
325
+ hooks.push(h);
326
+ },
327
+ },
328
+ };
329
+ host.track(id, scope);
330
+ try {
331
+ await invoke(owner, "session", () => p.module.session!(ctx));
332
+ } catch (e) {
333
+ open = false;
334
+ host.untrack(id, scope);
335
+ await scope.close();
336
+ const reason = e instanceof PluginError && e.plugin === id ? String((e.cause as Error)?.message ?? e.message) : e instanceof Error ? e.message : String(e);
337
+ host.report(`plugin ${id} did not join session ${s.id}: ${reason}`);
338
+ host.fault(id, reason);
339
+ return;
340
+ }
341
+ open = false;
342
+ // 内核构造 Runtime 时会因为重名工具、不合法的 hook 拒绝整个会话:提前检查,只排除这一个插件
343
+ const taken = new Set(s.parts.flatMap((x) => x.kernel.tools?.map((t) => t.name) ?? []));
344
+ const clash = tools.find((t) => taken.has(t.name));
345
+ const problem = clash
346
+ ? `tool ${clash.name} is already registered by ${s.parts.find((x) => x.kernel.tools?.some((t) => t.name === clash.name))!.source.manifest.id}`
347
+ : compatProblem(id, manifest.version, hooks);
348
+ if (problem) {
349
+ host.untrack(id, scope);
350
+ await scope.close();
351
+ host.report(`plugin ${id} did not join session ${s.id}: ${problem}`);
352
+ return;
353
+ }
354
+ const part: Part = {
355
+ source: p.source,
356
+ owner,
357
+ gates: hooks.filter((h) => h.kind === "gate" && h.point === "tool.gate").length,
358
+ kernel: this.#confirmed(p, kernelPlugin({
359
+ id,
360
+ version: manifest.version,
361
+ layer: KERNEL_LAYER[layer],
362
+ // 写日志的能力调用时才检查,推不出来(definePlugin 只补 hook 用到的);jade 的插件都可以写
363
+ needs: ["log:record", "log:emit"],
364
+ tools: tools.map((t) => liveTool(t, owner)),
365
+ hooks: hooks.map((h) => liveHook(h, owner)),
366
+ })),
367
+ };
368
+ s.parts.push(part);
369
+ }
370
+
371
+ /** 项目层插件交给内核的插件要用确认时拿到的 mark 标记,否则内核拒绝整个会话 */
372
+ #confirmed(p: { source: PluginSource; mark?: (p: Plugin) => Plugin }, plugin: Plugin): Plugin {
373
+ if (p.source.layer !== "project") return plugin;
374
+ if (!p.mark) throw new Error(`plugin ${p.source.manifest.id}: project plugin was not confirmed`);
375
+ return p.mark(plugin);
376
+ }
377
+
378
+ /**
379
+ * 前端给这个插件在这个会话里的状态栏段落和输入框(4.3)。没有 ui@1、或前端不提供会话界面时没有;
380
+ * 获取失败也只当作没有:界面上的东西坏了不该让插件加入不了会话
381
+ */
382
+ #sessionUI(s: LiveSession, plugin: string, owner: Owner): SessionUI | undefined {
383
+ try {
384
+ const ui = this.#host.service<UI>("ui@1", owner);
385
+ return ui?.session?.({ session: s.id, plugin });
386
+ } catch {
387
+ return undefined;
388
+ }
389
+ }
390
+
391
+ /**
392
+ * 兜底关卡(第 7 节):approval@1 的提供者没有在本会话里注册仍然有效的 tool.gate 时,拒绝非只读工具。
393
+ * 只认审批插件:其他插件的 gate(例如拒绝重复调用)全部弃权时内核会放行,不能让兜底关卡因此让开。
394
+ * 项目层的提供者也不算:克隆的仓库不能放宽审批
395
+ */
396
+ #corePlugin(s: LiveSession): Plugin {
397
+ const gate = (call: { tool: { effect: string } }): GateResult => {
398
+ const approval = this.#host.provider("approval@1");
399
+ const guarded = s.parts.some(
400
+ (p) => p.gates > 0 && !p.owner.scope.closed && p.source.manifest.id === approval && p.source.layer !== "project",
401
+ );
402
+ return guarded || call.tool.effect === "read" ? { decision: "abstain" } : { decision: "deny", reason: NO_APPROVAL_REASON };
403
+ };
404
+ return kernelPlugin({ id: CORE_PLUGIN_ID, version: API_VERSION, layer: "distro", hooks: [{ kind: "gate", point: "tool.gate", fn: gate }] });
405
+ }
406
+
407
+ /** 离开会话:中止正在跑的工作,等它停下,detach,关闭插件的会话作用域 */
408
+ async #leave(s: LiveSession): Promise<void> {
409
+ s.leave();
410
+ await s.idle();
411
+ try {
412
+ await s.rt.detach();
413
+ } catch (e) {
414
+ this.#host.report(`session ${s.id}: detach failed: ${e instanceof Error ? e.message : String(e)}`);
415
+ }
416
+ await this.#closeParts(s);
417
+ }
418
+
419
+ async #closeParts(s: LiveSession): Promise<void> {
420
+ for (const p of [...s.parts].reverse()) {
421
+ this.#host.untrack(p.owner.plugin, p.owner.scope);
422
+ for (const e of await p.owner.scope.close()) this.#host.report(e instanceof Error ? e.message : String(e));
423
+ }
424
+ }
425
+ }
426
+
427
+ function compatProblem(id: string, version: string, hooks: (LocalHookSpec | RemoteHookSpec)[]): string | undefined {
428
+ try {
429
+ checkCompat(kernelPlugin({ id, version, hooks }));
430
+ return undefined;
431
+ } catch (e) {
432
+ return e instanceof Error ? e.message : String(e);
433
+ }
434
+ }
435
+
436
+ /** 插件的工具:以插件身份执行;插件被隔离后返回错误结果(由内核转成 tool_result) */
437
+ function liveTool(t: Tool, owner: Owner): Tool {
438
+ return { ...t, execute: (input, ctx) => invoke(owner, `tool ${t.name}`, () => t.execute(input, ctx)) };
439
+ }
440
+
441
+ /**
442
+ * 插件的 hook:以插件身份执行。插件被隔离(作用域关闭)后这个 hook 视为不存在:gate 弃权、transform 不改、
443
+ * observe 不做事。审批插件被隔离时,兜底关卡随之生效,所以弃权不会放行写操作
444
+ */
445
+ function liveHook(h: LocalHookSpec | RemoteHookSpec, owner: Owner): LocalHookSpec | RemoteHookSpec {
446
+ if ("remote" in h && h.remote) {
447
+ // 进程外:transform 回复空对象表示不改
448
+ const call = h.call;
449
+ const gone = h.kind === "gate" ? { decision: "abstain" } : h.kind === "transform" ? {} : undefined;
450
+ return { ...h, call: (payload, ctx) => (owner.scope.closed ? Promise.resolve(gone) : ambient.run(owner, () => call(payload, ctx))) };
451
+ }
452
+ const fn = (h as LocalHookSpec).fn as (payload: unknown, ctx: unknown) => unknown;
453
+ const wrapped = (payload: unknown, ctx: unknown) => {
454
+ if (owner.scope.closed) return h.kind === "gate" ? { decision: "abstain" } : undefined;
455
+ return ambient.run(owner, () => fn(payload, ctx));
456
+ };
457
+ return { ...h, fn: wrapped } as LocalHookSpec;
458
+ }
package/src/source.ts ADDED
@@ -0,0 +1,83 @@
1
+ // 插件来源的识别(5.4):本地目录、npm 包、git 仓库。
2
+ // 只判断来源,不下载,也不执行插件代码;下载在 fetch.ts。
3
+ //
4
+ // 可以写:
5
+ // - 本地目录:相对或绝对路径;
6
+ // - npm:包名(可以带 @版本)、npm:<包管理器认识的写法>、本地或远程的 .tgz;
7
+ // - git:https://…、git@…、ssh://…、git+…、github:user/repo、user/repo,可以带 #分支、tag 或提交。
8
+
9
+ import { stat } from "node:fs/promises";
10
+ import os from "node:os";
11
+ import path from "node:path";
12
+
13
+ export type SourceKind = "git" | "npm" | "local";
14
+
15
+ export interface PluginSourceSpec {
16
+ kind: SourceKind;
17
+ /** git:仓库地址;npm:交给包管理器的写法;local:绝对路径 */
18
+ location: string;
19
+ /** git 的分支、tag 或提交 */
20
+ ref?: string;
21
+ /** 用户写的原文,记进安装记录 */
22
+ spec: string;
23
+ }
24
+
25
+ /** 识别一个来源。本地路径在这里就已经解析成绝对路径;不检查插件的 plugin.json */
26
+ export async function parseSource(text: string, cwd: string): Promise<PluginSourceSpec> {
27
+ const spec = text.trim();
28
+ if (!spec) throw new Error("no plugin source given");
29
+
30
+ if (spec.startsWith("npm:")) {
31
+ const inner = spec.slice(4);
32
+ // npm: 里也可以写本地路径(npm install ./dir、file:./dir);相对路径要按 jade 的工作目录解析,而不是下载目录
33
+ const location = /^(\.|\/|[a-zA-Z]:[\\/]|file:)/.test(inner) ? path.resolve(cwd, expandHome(inner.replace(/^file:/, ""))) : inner;
34
+ return { kind: "npm", location, spec };
35
+ }
36
+
37
+ const [base, ref] = splitRef(spec);
38
+ const git = (location: string): PluginSourceSpec => ({ kind: "git", location, ...(ref ? { ref } : {}), spec });
39
+ const npm = (location = spec): PluginSourceSpec => ({ kind: "npm", location, spec });
40
+
41
+ for (const [prefix, host] of [
42
+ ["github:", "github.com"],
43
+ ["gitlab:", "gitlab.com"],
44
+ ["bitbucket:", "bitbucket.org"],
45
+ ] as const) {
46
+ if (base.startsWith(prefix)) return git(`https://${host}/${base.slice(prefix.length).replace(/\.git$/, "")}.git`);
47
+ }
48
+ if (base.startsWith("git+")) return git(base.slice(4));
49
+ if (/^(git@|ssh:\/\/|git:\/\/)/.test(base)) return git(base);
50
+ if (/^https?:\/\//.test(base)) {
51
+ // 直接的包下载地址按 npm 包处理;其余 URL 都是仓库
52
+ if (/\.(tgz|tar\.gz)$/i.test(base)) return npm(spec);
53
+ return git(base);
54
+ }
55
+
56
+ const local = path.resolve(cwd, expandHome(spec));
57
+ const info = await stat(local).catch(() => undefined);
58
+ if (info?.isDirectory()) return { kind: "local", location: local, spec };
59
+ if (info?.isFile()) {
60
+ if (/\.(tgz|tar\.gz)$/i.test(local)) return { kind: "npm", location: local, spec };
61
+ throw new Error(`${spec} is a file; give a plugin directory, a .tgz package, a git URL or an npm package`);
62
+ }
63
+
64
+ // user/repo:本地没有这个路径时按 GitHub 仓库处理(npm 的包名不含 /,带作用域的以 @ 开头)
65
+ if (/^[\w.-]+\/[\w.-]+$/.test(base) && !base.startsWith(".")) {
66
+ return git(`https://github.com/${base.replace(/\.git$/, "")}.git`);
67
+ }
68
+
69
+ if (/^(\.|\/|[a-zA-Z]:[\\/])/.test(spec)) throw new Error(`${spec}: no such directory`);
70
+ return npm();
71
+ }
72
+
73
+ /** "url#ref" 拆成两部分;# 在开头(如 #foo)不算 */
74
+ function splitRef(s: string): [string, string | undefined] {
75
+ const i = s.lastIndexOf("#");
76
+ return i > 0 ? [s.slice(0, i), s.slice(i + 1) || undefined] : [s, undefined];
77
+ }
78
+
79
+ function expandHome(p: string): string {
80
+ if (p === "~") return os.homedir();
81
+ if (p.startsWith("~/") || p.startsWith("~\\")) return path.join(os.homedir(), p.slice(2));
82
+ return p;
83
+ }
package/src/state.ts ADDED
@@ -0,0 +1,120 @@
1
+ // $JADE_HOME 下的两份状态:plugins.json(用户停用的插件)和 state/faults.json(连续崩溃的记录,5.4)。
2
+
3
+ import { randomBytes } from "node:crypto";
4
+ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
5
+ import path from "node:path";
6
+ import type { TrustRecord, TrustStore } from "uncarved";
7
+
8
+ /** 连续几次启动出故障后自动停用 */
9
+ export const FAULT_LIMIT = 3;
10
+
11
+ export interface PluginsConfig {
12
+ disabled: string[];
13
+ }
14
+
15
+ export interface Faults {
16
+ /** 插件 id → 连续出故障的启动次数和最近一次的原因 */
17
+ streaks: Record<string, { count: number; last: string }>;
18
+ /** 因连续崩溃被自动停用的插件 */
19
+ autoDisabled: string[];
20
+ /** 归不到任何插件的错误,最近的若干条(5.3) */
21
+ unattributed: { at: string; error: string }[];
22
+ }
23
+
24
+ const UNATTRIBUTED_KEEP = 20;
25
+
26
+ export const pluginsPath = (home: string) => path.join(home, "plugins.json");
27
+ export const faultsPath = (home: string) => path.join(home, "state", "faults.json");
28
+
29
+ async function readJson(file: string): Promise<unknown> {
30
+ try {
31
+ return JSON.parse(await readFile(file, "utf8"));
32
+ } catch (e) {
33
+ if ((e as NodeJS.ErrnoException).code === "ENOENT") return undefined;
34
+ throw new Error(`${file}: ${e instanceof Error ? e.message : String(e)}`);
35
+ }
36
+ }
37
+
38
+ /** 先写临时文件再改名,写到一半退出不会留下坏文件 */
39
+ async function writeJson(file: string, value: unknown): Promise<void> {
40
+ await mkdir(path.dirname(file), { recursive: true });
41
+ const tmp = `${file}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
42
+ await writeFile(tmp, JSON.stringify(value, null, 2) + "\n");
43
+ await rename(tmp, file);
44
+ }
45
+
46
+ const strings = (v: unknown) => (Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : []);
47
+
48
+ export async function readPluginsConfig(home: string): Promise<PluginsConfig> {
49
+ const raw = (await readJson(pluginsPath(home))) as Record<string, unknown> | undefined;
50
+ return { disabled: strings(raw?.disabled) };
51
+ }
52
+
53
+ export async function writePluginsConfig(home: string, config: PluginsConfig): Promise<void> {
54
+ await writeJson(pluginsPath(home), config);
55
+ }
56
+
57
+ export async function readFaults(home: string): Promise<Faults> {
58
+ const raw = (await readJson(faultsPath(home))) as Partial<Faults> | undefined;
59
+ return {
60
+ streaks: raw?.streaks && typeof raw.streaks === "object" ? raw.streaks : {},
61
+ autoDisabled: strings(raw?.autoDisabled),
62
+ unattributed: Array.isArray(raw?.unattributed) ? raw.unattributed.slice(-UNATTRIBUTED_KEEP) : [],
63
+ };
64
+ }
65
+
66
+ export async function writeFaults(home: string, faults: Faults): Promise<void> {
67
+ faults.unattributed = faults.unattributed.slice(-UNATTRIBUTED_KEEP);
68
+ await writeJson(faultsPath(home), faults);
69
+ }
70
+
71
+ /** 记一次故障;本次启动里同一个插件只记一次。达到上限时自动停用,返回 true */
72
+ export function countFault(faults: Faults, id: string, reason: string): boolean {
73
+ const s = faults.streaks[id] ?? { count: 0, last: "" };
74
+ s.count += 1;
75
+ s.last = reason;
76
+ faults.streaks[id] = s;
77
+ if (s.count >= FAULT_LIMIT && !faults.autoDisabled.includes(id)) {
78
+ faults.autoDisabled.push(id);
79
+ return true;
80
+ }
81
+ return false;
82
+ }
83
+
84
+ /** 清掉一个插件的连续崩溃记录(更新后给它一次干净的机会),不动 plugins.json 里的停用记录 */
85
+ export async function forgetFaults(home: string, id: string): Promise<void> {
86
+ const faults = await readFaults(home);
87
+ let changed = false;
88
+ if (faults.streaks[id]) {
89
+ delete faults.streaks[id];
90
+ changed = true;
91
+ }
92
+ if (faults.autoDisabled.includes(id)) {
93
+ faults.autoDisabled = faults.autoDisabled.filter((d) => d !== id);
94
+ changed = true;
95
+ }
96
+ if (changed) await writeFaults(home, faults);
97
+ }
98
+
99
+ /** 项目层插件的确认记录(第 7 节),存在 $JADE_HOME/state/trust.json,按插件目录内容的哈希 */
100
+ export class FileTrustStore implements TrustStore {
101
+ readonly #file: string;
102
+ constructor(file: string) {
103
+ this.#file = file;
104
+ }
105
+
106
+ async has(hash: string): Promise<boolean> {
107
+ return (await this.#read()).some((r) => r.hash === hash);
108
+ }
109
+
110
+ async add(record: TrustRecord): Promise<void> {
111
+ const records = (await this.#read()).filter((r) => r.hash !== record.hash);
112
+ records.push(record);
113
+ await writeJson(this.#file, { records });
114
+ }
115
+
116
+ async #read(): Promise<TrustRecord[]> {
117
+ const raw = (await readJson(this.#file)) as { records?: unknown } | undefined;
118
+ return Array.isArray(raw?.records) ? raw.records.filter((r): r is TrustRecord => typeof r?.hash === "string") : [];
119
+ }
120
+ }