@uncarved/jade 0.1.3 → 0.2.1

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 (455) hide show
  1. package/README.md +161 -150
  2. package/bin/jade.js +14 -25
  3. package/dist/api.d.ts +266 -0
  4. package/dist/api.js +38 -0
  5. package/dist/api.js.map +1 -0
  6. package/dist/config.d.ts +13 -32
  7. package/dist/config.js +39 -121
  8. package/dist/config.js.map +1 -1
  9. package/dist/fetch.d.ts +20 -0
  10. package/dist/fetch.js +190 -0
  11. package/dist/fetch.js.map +1 -0
  12. package/dist/hash.d.ts +2 -0
  13. package/dist/hash.js +42 -0
  14. package/dist/hash.js.map +1 -0
  15. package/dist/host.d.ts +71 -0
  16. package/dist/host.js +400 -0
  17. package/dist/host.js.map +1 -0
  18. package/dist/index.d.ts +11 -30
  19. package/dist/index.js +11 -30
  20. package/dist/index.js.map +1 -1
  21. package/dist/install.d.ts +78 -0
  22. package/dist/install.js +292 -0
  23. package/dist/install.js.map +1 -0
  24. package/dist/main.d.ts +1 -2
  25. package/dist/main.js +160 -1362
  26. package/dist/main.js.map +1 -1
  27. package/dist/manifest.d.ts +23 -0
  28. package/dist/manifest.js +159 -0
  29. package/dist/manifest.js.map +1 -0
  30. package/dist/membrane.d.ts +33 -0
  31. package/dist/membrane.js +140 -0
  32. package/dist/membrane.js.map +1 -0
  33. package/dist/remote.d.ts +10 -0
  34. package/dist/remote.js +292 -0
  35. package/dist/remote.js.map +1 -0
  36. package/dist/rescue.d.ts +10 -0
  37. package/dist/rescue.js +216 -0
  38. package/dist/rescue.js.map +1 -0
  39. package/dist/resolve.d.ts +34 -0
  40. package/dist/resolve.js +178 -0
  41. package/dist/resolve.js.map +1 -0
  42. package/dist/rpc.d.ts +26 -0
  43. package/dist/rpc.js +144 -0
  44. package/dist/rpc.js.map +1 -0
  45. package/dist/secrets.d.ts +12 -0
  46. package/dist/secrets.js +125 -0
  47. package/dist/secrets.js.map +1 -0
  48. package/dist/session.d.ts +45 -0
  49. package/dist/session.js +401 -0
  50. package/dist/session.js.map +1 -0
  51. package/dist/source.d.ts +12 -0
  52. package/dist/source.js +72 -0
  53. package/dist/source.js.map +1 -0
  54. package/dist/state.d.ts +37 -0
  55. package/dist/state.js +93 -0
  56. package/dist/state.js.map +1 -0
  57. package/dist/update.d.ts +2 -0
  58. package/dist/update.js +6 -3
  59. package/dist/update.js.map +1 -1
  60. package/package.json +10 -8
  61. package/plugins/approval/index.ts +311 -0
  62. package/plugins/approval/plugin.json +22 -0
  63. package/{src/plugins → plugins/approval}/policy.ts +45 -104
  64. package/{src/plugins → plugins/approval}/review.ts +62 -16
  65. package/plugins/ask/index.ts +159 -0
  66. package/plugins/ask/plugin.json +8 -0
  67. package/plugins/budget/index.ts +38 -0
  68. package/plugins/budget/plugin.json +13 -0
  69. package/{src/plugins → plugins/checks}/checks.ts +48 -38
  70. package/plugins/checks/index.ts +158 -0
  71. package/plugins/checks/plugin.json +14 -0
  72. package/plugins/checks/project.ts +81 -0
  73. package/plugins/commands/index.ts +76 -0
  74. package/plugins/commands/plugin.json +7 -0
  75. package/{src/plugins → plugins/compact}/compact.ts +80 -39
  76. package/plugins/compact/index.ts +115 -0
  77. package/plugins/compact/plugin.json +17 -0
  78. package/plugins/compact/style.ts +17 -0
  79. package/plugins/continue/index.ts +58 -0
  80. package/plugins/continue/plugin.json +13 -0
  81. package/{src/ui → plugins/fs}/diff.ts +2 -2
  82. package/{src/tools → plugins/fs}/fs.ts +89 -46
  83. package/{src/tools → plugins/fs}/grep.ts +36 -8
  84. package/plugins/fs/index.ts +37 -0
  85. package/plugins/fs/plugin.json +7 -0
  86. package/{src/ui → plugins/fs}/renderers.ts +25 -70
  87. package/plugins/fs/style.ts +18 -0
  88. package/plugins/fs/sys.ts +81 -0
  89. package/plugins/keys/index.ts +53 -0
  90. package/plugins/keys/plugin.json +7 -0
  91. package/plugins/mcp/index.ts +192 -0
  92. package/{src → plugins/mcp}/mcp.ts +32 -32
  93. package/plugins/mcp/plugin.json +7 -0
  94. package/plugins/mcp/project.ts +93 -0
  95. package/plugins/memory/index.ts +105 -0
  96. package/{src → plugins/memory}/memory.ts +20 -2
  97. package/plugins/memory/notes.ts +43 -0
  98. package/plugins/memory/plugin.json +7 -0
  99. package/plugins/memory/style.ts +17 -0
  100. package/plugins/mentions/attach.ts +127 -0
  101. package/plugins/mentions/index.ts +99 -0
  102. package/plugins/mentions/plugin.json +7 -0
  103. package/plugins/mentions/style.ts +17 -0
  104. package/plugins/models/config.ts +53 -0
  105. package/plugins/models/configure.ts +455 -0
  106. package/plugins/models/context.ts +32 -0
  107. package/plugins/models/index.ts +213 -0
  108. package/plugins/models/plugin.json +8 -0
  109. package/{src → plugins/models}/provider.ts +16 -119
  110. package/plugins/models/saved.ts +94 -0
  111. package/plugins/modes/index.ts +222 -0
  112. package/plugins/modes/plugin.json +9 -0
  113. package/plugins/plugins/index.ts +38 -0
  114. package/plugins/plugins/plugin.json +7 -0
  115. package/plugins/print/index.ts +160 -0
  116. package/plugins/print/plugin.json +8 -0
  117. package/plugins/prompts/index.ts +111 -0
  118. package/plugins/prompts/plugin.json +8 -0
  119. package/plugins/prompts/prompts.ts +258 -0
  120. package/plugins/prompts/results.ts +63 -0
  121. package/plugins/prompts/sys.ts +84 -0
  122. package/plugins/renderers/index.ts +78 -0
  123. package/plugins/renderers/plugin.json +7 -0
  124. package/plugins/repeats/index.ts +165 -0
  125. package/plugins/repeats/plugin.json +6 -0
  126. package/plugins/schedule/cron.ts +74 -0
  127. package/plugins/schedule/index.ts +292 -0
  128. package/plugins/schedule/plugin.json +13 -0
  129. package/plugins/schedule/store.ts +149 -0
  130. package/plugins/scratch/index.ts +31 -0
  131. package/plugins/scratch/plugin.json +7 -0
  132. package/plugins/screenshot/index.ts +40 -0
  133. package/plugins/screenshot/plugin.json +7 -0
  134. package/plugins/screenshot/sys.ts +84 -0
  135. package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
  136. package/plugins/sessions/index.ts +148 -0
  137. package/plugins/sessions/plugin.json +8 -0
  138. package/plugins/shell/index.ts +33 -0
  139. package/plugins/shell/plugin.json +17 -0
  140. package/plugins/shell/renderers.ts +45 -0
  141. package/plugins/shell/service.ts +44 -0
  142. package/{src/tools → plugins/shell}/shell.ts +31 -20
  143. package/{src → plugins/shell}/sys.ts +2 -43
  144. package/plugins/skills/index.ts +96 -0
  145. package/plugins/skills/plugin.json +7 -0
  146. package/plugins/skills/project.ts +49 -0
  147. package/{src → plugins/skills}/skills.ts +4 -11
  148. package/plugins/suggest/index.ts +286 -0
  149. package/plugins/suggest/plugin.json +18 -0
  150. package/plugins/task/index.ts +168 -0
  151. package/plugins/task/plugin.json +15 -0
  152. package/plugins/task/style.ts +17 -0
  153. package/plugins/task/subagent.ts +343 -0
  154. package/plugins/todo/index.ts +93 -0
  155. package/plugins/todo/plugin.json +7 -0
  156. package/{src/ui → plugins/tui}/editor.ts +3 -0
  157. package/plugins/tui/frontend.ts +619 -0
  158. package/plugins/tui/index.ts +37 -0
  159. package/plugins/tui/mentions.ts +153 -0
  160. package/plugins/tui/plugin.json +10 -0
  161. package/{src/ui → plugins/tui}/render.ts +8 -2
  162. package/{src/ui → plugins/tui}/screen.ts +8 -3
  163. package/{src/ui → plugins/tui}/transcript.ts +79 -100
  164. package/{src/ui/tui.tsx → plugins/tui/tui.ts} +360 -392
  165. package/plugins/tui/vendor.ts +16 -0
  166. package/{src/tools → plugins/undo}/checkpoint.ts +45 -60
  167. package/plugins/undo/index.ts +143 -0
  168. package/plugins/undo/notes.ts +43 -0
  169. package/plugins/undo/plugin.json +7 -0
  170. package/plugins/undo/style.ts +17 -0
  171. package/plugins/update/index.ts +40 -0
  172. package/plugins/update/plugin.json +13 -0
  173. package/plugins/web/env.ts +78 -0
  174. package/plugins/web/index.ts +133 -0
  175. package/plugins/web/plugin.json +15 -0
  176. package/plugins/web/renderers.ts +11 -0
  177. package/{src/tools → plugins/web}/web.ts +64 -31
  178. package/plugins-dist/approval/index.js +287 -0
  179. package/plugins-dist/approval/plugin.json +28 -0
  180. package/{dist/plugins → plugins-dist/approval}/policy.js +41 -85
  181. package/{dist/plugins → plugins-dist/approval}/review.js +56 -10
  182. package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
  183. package/plugins-dist/ask/index.js +145 -0
  184. package/plugins-dist/ask/plugin.json +12 -0
  185. package/plugins-dist/budget/index.js +37 -0
  186. package/plugins-dist/budget/plugin.json +15 -0
  187. package/{dist/plugins → plugins-dist/checks}/checks.js +46 -35
  188. package/plugins-dist/checks/index.js +150 -0
  189. package/plugins-dist/checks/plugin.json +21 -0
  190. package/plugins-dist/checks/project.js +76 -0
  191. package/plugins-dist/commands/index.js +33 -0
  192. package/plugins-dist/commands/plugin.json +9 -0
  193. package/{dist/plugins → plugins-dist/compact}/compact.js +58 -34
  194. package/plugins-dist/compact/index.js +100 -0
  195. package/plugins-dist/compact/plugin.json +25 -0
  196. package/plugins-dist/compact/style.js +14 -0
  197. package/plugins-dist/continue/index.js +57 -0
  198. package/plugins-dist/continue/plugin.json +15 -0
  199. package/{dist/ui → plugins-dist/fs}/diff.js +2 -3
  200. package/{dist/tools → plugins-dist/fs}/fs.js +82 -46
  201. package/{dist/tools → plugins-dist/fs}/grep.js +36 -11
  202. package/plugins-dist/fs/index.js +39 -0
  203. package/plugins-dist/fs/plugin.json +10 -0
  204. package/{dist/ui → plugins-dist/fs}/renderers.js +24 -61
  205. package/plugins-dist/fs/style.js +15 -0
  206. package/plugins-dist/fs/sys.js +54 -0
  207. package/plugins-dist/keys/index.js +36 -0
  208. package/plugins-dist/keys/plugin.json +9 -0
  209. package/plugins-dist/mcp/index.js +187 -0
  210. package/{dist → plugins-dist/mcp}/mcp.js +34 -30
  211. package/plugins-dist/mcp/plugin.json +12 -0
  212. package/plugins-dist/mcp/project.js +90 -0
  213. package/plugins-dist/memory/index.js +94 -0
  214. package/{dist → plugins-dist/memory}/memory.js +21 -3
  215. package/plugins-dist/memory/notes.js +36 -0
  216. package/plugins-dist/memory/plugin.json +11 -0
  217. package/plugins-dist/memory/style.js +14 -0
  218. package/plugins-dist/mentions/attach.js +120 -0
  219. package/plugins-dist/mentions/index.js +87 -0
  220. package/plugins-dist/mentions/plugin.json +9 -0
  221. package/plugins-dist/mentions/style.js +14 -0
  222. package/plugins-dist/models/config.js +53 -0
  223. package/plugins-dist/models/configure.js +415 -0
  224. package/plugins-dist/models/context.js +27 -0
  225. package/plugins-dist/models/index.js +181 -0
  226. package/plugins-dist/models/plugin.json +14 -0
  227. package/{dist → plugins-dist/models}/provider.js +18 -81
  228. package/plugins-dist/models/saved.js +80 -0
  229. package/plugins-dist/modes/index.js +190 -0
  230. package/plugins-dist/modes/plugin.json +18 -0
  231. package/plugins-dist/plugins/index.js +37 -0
  232. package/plugins-dist/plugins/plugin.json +10 -0
  233. package/plugins-dist/print/index.js +172 -0
  234. package/plugins-dist/print/plugin.json +14 -0
  235. package/plugins-dist/prompts/index.js +85 -0
  236. package/plugins-dist/prompts/plugin.json +13 -0
  237. package/plugins-dist/prompts/prompts.js +231 -0
  238. package/plugins-dist/prompts/results.js +60 -0
  239. package/plugins-dist/prompts/sys.js +58 -0
  240. package/plugins-dist/renderers/index.js +36 -0
  241. package/plugins-dist/renderers/plugin.json +9 -0
  242. package/plugins-dist/repeats/index.js +165 -0
  243. package/plugins-dist/repeats/plugin.json +6 -0
  244. package/plugins-dist/schedule/cron.js +67 -0
  245. package/plugins-dist/schedule/index.js +281 -0
  246. package/plugins-dist/schedule/plugin.json +16 -0
  247. package/plugins-dist/schedule/store.js +125 -0
  248. package/plugins-dist/scratch/index.js +26 -0
  249. package/plugins-dist/scratch/plugin.json +10 -0
  250. package/plugins-dist/screenshot/index.js +35 -0
  251. package/plugins-dist/screenshot/plugin.json +11 -0
  252. package/plugins-dist/screenshot/sys.js +76 -0
  253. package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
  254. package/plugins-dist/sessions/index.js +136 -0
  255. package/plugins-dist/sessions/plugin.json +12 -0
  256. package/plugins-dist/shell/index.js +33 -0
  257. package/plugins-dist/shell/plugin.json +19 -0
  258. package/plugins-dist/shell/renderers.js +40 -0
  259. package/plugins-dist/shell/service.js +29 -0
  260. package/{dist/tools → plugins-dist/shell}/shell.js +31 -22
  261. package/{dist → plugins-dist/shell}/sys.js +2 -43
  262. package/plugins-dist/skills/index.js +90 -0
  263. package/plugins-dist/skills/plugin.json +12 -0
  264. package/plugins-dist/skills/project.js +47 -0
  265. package/{dist → plugins-dist/skills}/skills.js +3 -7
  266. package/plugins-dist/suggest/index.js +282 -0
  267. package/plugins-dist/suggest/plugin.json +23 -0
  268. package/plugins-dist/task/index.js +152 -0
  269. package/plugins-dist/task/plugin.json +23 -0
  270. package/plugins-dist/task/style.js +14 -0
  271. package/plugins-dist/task/subagent.js +284 -0
  272. package/{dist/tools/todo.js → plugins-dist/todo/index.js} +31 -9
  273. package/plugins-dist/todo/plugin.json +9 -0
  274. package/{dist/ui → plugins-dist/tui}/editor.js +3 -1
  275. package/plugins-dist/tui/frontend.js +609 -0
  276. package/{dist → plugins-dist/tui}/history.js +0 -1
  277. package/plugins-dist/tui/index.js +21 -0
  278. package/{dist/ui → plugins-dist/tui}/markdown.js +0 -1
  279. package/plugins-dist/tui/mentions.js +153 -0
  280. package/plugins-dist/tui/plugin.json +25 -0
  281. package/{dist/ui → plugins-dist/tui}/render.js +7 -3
  282. package/{dist/ui → plugins-dist/tui}/screen.js +6 -2
  283. package/{dist/ui → plugins-dist/tui}/transcript.js +79 -99
  284. package/{dist/ui → plugins-dist/tui}/tui.js +185 -54
  285. package/plugins-dist/tui/vendor.js +13822 -0
  286. package/{dist/tools → plugins-dist/undo}/checkpoint.js +48 -55
  287. package/plugins-dist/undo/index.js +143 -0
  288. package/plugins-dist/undo/notes.js +36 -0
  289. package/plugins-dist/undo/plugin.json +9 -0
  290. package/plugins-dist/undo/style.js +14 -0
  291. package/plugins-dist/update/index.js +34 -0
  292. package/plugins-dist/update/plugin.json +15 -0
  293. package/plugins-dist/web/env.js +80 -0
  294. package/plugins-dist/web/index.js +124 -0
  295. package/plugins-dist/web/plugin.json +21 -0
  296. package/plugins-dist/web/renderers.js +7 -0
  297. package/{dist/tools → plugins-dist/web}/web.js +56 -29
  298. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  299. package/sdk/python/jade_plugin.py +380 -0
  300. package/src/api.ts +298 -0
  301. package/src/config.ts +32 -134
  302. package/src/fetch.ts +216 -0
  303. package/src/hash.ts +39 -0
  304. package/src/host.ts +429 -0
  305. package/src/index.ts +23 -30
  306. package/src/install.ts +355 -0
  307. package/src/main.ts +156 -1387
  308. package/src/manifest.ts +160 -0
  309. package/src/membrane.ts +142 -0
  310. package/src/remote.ts +288 -0
  311. package/src/rescue.ts +217 -0
  312. package/src/resolve.ts +199 -0
  313. package/src/rpc.ts +144 -0
  314. package/src/secrets.ts +122 -0
  315. package/src/session.ts +458 -0
  316. package/src/source.ts +83 -0
  317. package/src/state.ts +120 -0
  318. package/src/update.ts +265 -262
  319. package/LICENSE +0 -21
  320. package/dist/commands.d.ts +0 -50
  321. package/dist/commands.js +0 -126
  322. package/dist/commands.js.map +0 -1
  323. package/dist/context.d.ts +0 -17
  324. package/dist/context.js +0 -53
  325. package/dist/context.js.map +0 -1
  326. package/dist/distro.d.ts +0 -141
  327. package/dist/distro.js +0 -462
  328. package/dist/distro.js.map +0 -1
  329. package/dist/history.d.ts +0 -10
  330. package/dist/history.js.map +0 -1
  331. package/dist/mcp.d.ts +0 -114
  332. package/dist/mcp.js.map +0 -1
  333. package/dist/memory.d.ts +0 -19
  334. package/dist/memory.js.map +0 -1
  335. package/dist/mentions.d.ts +0 -35
  336. package/dist/mentions.js +0 -130
  337. package/dist/mentions.js.map +0 -1
  338. package/dist/plugin-cli.d.ts +0 -16
  339. package/dist/plugin-cli.js +0 -294
  340. package/dist/plugin-cli.js.map +0 -1
  341. package/dist/plugins/budget.d.ts +0 -9
  342. package/dist/plugins/budget.js +0 -30
  343. package/dist/plugins/budget.js.map +0 -1
  344. package/dist/plugins/builtins.d.ts +0 -10
  345. package/dist/plugins/builtins.js +0 -25
  346. package/dist/plugins/builtins.js.map +0 -1
  347. package/dist/plugins/checks.d.ts +0 -71
  348. package/dist/plugins/checks.js.map +0 -1
  349. package/dist/plugins/compact.d.ts +0 -26
  350. package/dist/plugins/compact.js.map +0 -1
  351. package/dist/plugins/core.d.ts +0 -47
  352. package/dist/plugins/core.js +0 -229
  353. package/dist/plugins/core.js.map +0 -1
  354. package/dist/plugins/host.d.ts +0 -52
  355. package/dist/plugins/host.js +0 -115
  356. package/dist/plugins/host.js.map +0 -1
  357. package/dist/plugins/install.d.ts +0 -68
  358. package/dist/plugins/install.js +0 -377
  359. package/dist/plugins/install.js.map +0 -1
  360. package/dist/plugins/loader.d.ts +0 -410
  361. package/dist/plugins/loader.js +0 -407
  362. package/dist/plugins/loader.js.map +0 -1
  363. package/dist/plugins/policy.d.ts +0 -98
  364. package/dist/plugins/policy.js.map +0 -1
  365. package/dist/plugins/prompts.d.ts +0 -43
  366. package/dist/plugins/prompts.js +0 -284
  367. package/dist/plugins/prompts.js.map +0 -1
  368. package/dist/plugins/repeats.d.ts +0 -16
  369. package/dist/plugins/repeats.js +0 -151
  370. package/dist/plugins/repeats.js.map +0 -1
  371. package/dist/plugins/review.d.ts +0 -18
  372. package/dist/plugins/review.js.map +0 -1
  373. package/dist/plugins/schema.d.ts +0 -2
  374. package/dist/plugins/schema.js.map +0 -1
  375. package/dist/plugins/sessions.d.ts +0 -7
  376. package/dist/plugins/sessions.js +0 -110
  377. package/dist/plugins/sessions.js.map +0 -1
  378. package/dist/provider.d.ts +0 -165
  379. package/dist/provider.js.map +0 -1
  380. package/dist/session-lifecycle.d.ts +0 -61
  381. package/dist/session-lifecycle.js +0 -167
  382. package/dist/session-lifecycle.js.map +0 -1
  383. package/dist/session-scope.d.ts +0 -53
  384. package/dist/session-scope.js +0 -116
  385. package/dist/session-scope.js.map +0 -1
  386. package/dist/sessions.d.ts +0 -28
  387. package/dist/sessions.js +0 -73
  388. package/dist/sessions.js.map +0 -1
  389. package/dist/skills.d.ts +0 -37
  390. package/dist/skills.js.map +0 -1
  391. package/dist/sys.d.ts +0 -51
  392. package/dist/sys.js.map +0 -1
  393. package/dist/tools/ask.d.ts +0 -11
  394. package/dist/tools/ask.js +0 -62
  395. package/dist/tools/ask.js.map +0 -1
  396. package/dist/tools/checkpoint.d.ts +0 -43
  397. package/dist/tools/checkpoint.js.map +0 -1
  398. package/dist/tools/fs.d.ts +0 -39
  399. package/dist/tools/fs.js.map +0 -1
  400. package/dist/tools/grep.d.ts +0 -6
  401. package/dist/tools/grep.js.map +0 -1
  402. package/dist/tools/screenshot.d.ts +0 -15
  403. package/dist/tools/screenshot.js.map +0 -1
  404. package/dist/tools/shell.d.ts +0 -55
  405. package/dist/tools/shell.js.map +0 -1
  406. package/dist/tools/task.d.ts +0 -39
  407. package/dist/tools/task.js +0 -117
  408. package/dist/tools/task.js.map +0 -1
  409. package/dist/tools/todo.d.ts +0 -10
  410. package/dist/tools/todo.js.map +0 -1
  411. package/dist/tools/web.d.ts +0 -53
  412. package/dist/tools/web.js.map +0 -1
  413. package/dist/ui/configure.d.ts +0 -20
  414. package/dist/ui/configure.js +0 -447
  415. package/dist/ui/configure.js.map +0 -1
  416. package/dist/ui/diff.d.ts +0 -20
  417. package/dist/ui/diff.js.map +0 -1
  418. package/dist/ui/editor.d.ts +0 -41
  419. package/dist/ui/editor.js.map +0 -1
  420. package/dist/ui/markdown.d.ts +0 -22
  421. package/dist/ui/markdown.js.map +0 -1
  422. package/dist/ui/render.d.ts +0 -14
  423. package/dist/ui/render.js.map +0 -1
  424. package/dist/ui/renderers.d.ts +0 -8
  425. package/dist/ui/renderers.js.map +0 -1
  426. package/dist/ui/screen.d.ts +0 -118
  427. package/dist/ui/screen.js.map +0 -1
  428. package/dist/ui/transcript.d.ts +0 -46
  429. package/dist/ui/transcript.js.map +0 -1
  430. package/dist/ui/tui.d.ts +0 -47
  431. package/dist/ui/tui.js.map +0 -1
  432. package/src/commands.ts +0 -144
  433. package/src/context.ts +0 -64
  434. package/src/distro.ts +0 -577
  435. package/src/mentions.ts +0 -135
  436. package/src/plugin-cli.ts +0 -278
  437. package/src/plugins/budget.ts +0 -37
  438. package/src/plugins/builtins.ts +0 -34
  439. package/src/plugins/core.ts +0 -257
  440. package/src/plugins/host.ts +0 -146
  441. package/src/plugins/install.ts +0 -429
  442. package/src/plugins/loader.ts +0 -742
  443. package/src/plugins/prompts.ts +0 -327
  444. package/src/plugins/repeats.ts +0 -157
  445. package/src/plugins/sessions.ts +0 -109
  446. package/src/session-lifecycle.ts +0 -194
  447. package/src/session-scope.ts +0 -135
  448. package/src/sessions.ts +0 -78
  449. package/src/tools/ask.ts +0 -72
  450. package/src/tools/task.ts +0 -149
  451. package/src/tools/todo.ts +0 -67
  452. package/src/ui/configure.ts +0 -471
  453. /package/{src/plugins → plugins/approval}/schema.ts +0 -0
  454. /package/{src → plugins/tui}/history.ts +0 -0
  455. /package/{src/ui → plugins/tui}/markdown.ts +0 -0
package/src/fetch.ts ADDED
@@ -0,0 +1,216 @@
1
+ // 把插件来源下载到 staging 目录(5.4):
2
+ // - git:clone(可以指定分支、tag 或提交),删掉 .git;
3
+ // - npm:用包管理器把包安装到一个空目录,再把包目录复制过来;
4
+ // - 本地目录:复制(不复制 node_modules 和 .git)。
5
+ //
6
+ // 下载完检查 plugin.json,安装 package.json 里的 dependencies(--ignore-scripts,不执行插件的代码),
7
+ // 然后返回插件目录、提交、清单和内容哈希。清理 staging 是调用方的事。
8
+
9
+ import { execFile } from "node:child_process";
10
+ import { accessSync, constants, existsSync, statSync } from "node:fs";
11
+ import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
12
+ import path from "node:path";
13
+ import type { Manifest } from "./api.ts";
14
+ import { treeHash } from "./hash.ts";
15
+ import { parseManifest } from "./manifest.ts";
16
+ import type { PluginSourceSpec } from "./source.ts";
17
+
18
+ export interface Fetched {
19
+ /** 插件目录(staging 里的某个子目录) */
20
+ dir: string;
21
+ /** git 安装的提交 */
22
+ commit?: string;
23
+ manifest: Manifest;
24
+ /** 插件源码的哈希,安装/更新时记进安装记录 */
25
+ hash: string;
26
+ }
27
+
28
+ export async function fetchPlugin(
29
+ source: PluginSourceSpec,
30
+ staging: string,
31
+ subdir?: string,
32
+ log?: (text: string) => void,
33
+ ): Promise<Fetched> {
34
+ await mkdir(staging, { recursive: true });
35
+ let root: string;
36
+ let commit: string | undefined;
37
+ if (source.kind === "git") {
38
+ ({ root, commit } = await fetchGit(source, staging, log));
39
+ } else if (source.kind === "npm") {
40
+ root = await fetchNpm(source.location, staging, log);
41
+ } else {
42
+ root = path.join(staging, "local");
43
+ log?.(`copying ${source.location}`);
44
+ await cp(source.location, root, {
45
+ recursive: true,
46
+ filter: (src) => !/[\\/](node_modules|\.git)$/.test(src),
47
+ });
48
+ }
49
+
50
+ const dir = subdir ? inSubdir(root, subdir) : root;
51
+ const manifest = await readPluginManifest(dir);
52
+ const hash = await treeHash(dir);
53
+ await installDependencies(dir, log);
54
+ return { dir, commit, manifest, hash };
55
+ }
56
+
57
+ /** 读一个插件目录的 plugin.json;不是插件时给出能看懂的错误 */
58
+ export async function readPluginManifest(dir: string): Promise<Manifest> {
59
+ let raw: unknown;
60
+ try {
61
+ raw = JSON.parse(await readFile(path.join(dir, "plugin.json"), "utf8"));
62
+ } catch (e) {
63
+ throw new Error(`${dir}: not a jade plugin (${e instanceof Error ? e.message : String(e)})`);
64
+ }
65
+ return parseManifest(raw);
66
+ }
67
+
68
+ /** 把 --path 限制在来源目录里,不能跑到外面去 */
69
+ export function inSubdir(root: string, subdir: string): string {
70
+ const full = path.resolve(root, subdir);
71
+ const rel = path.relative(root, full);
72
+ if (rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
73
+ throw new Error(`--path ${subdir} points outside the source`);
74
+ }
75
+ return full;
76
+ }
77
+
78
+ // ---------- git ----------
79
+
80
+ async function fetchGit(source: PluginSourceSpec, staging: string, log?: (text: string) => void): Promise<{ root: string; commit: string }> {
81
+ const root = path.join(staging, "repo");
82
+ log?.(`cloning ${source.location}${source.ref ? ` (${source.ref})` : ""}`);
83
+ try {
84
+ await run("git", ["clone", "--depth", "1", ...(source.ref ? ["--branch", source.ref] : []), "--", source.location, root], staging);
85
+ } catch (err) {
86
+ // --branch 不认提交哈希:完整克隆后再切过去
87
+ if (!source.ref) throw err;
88
+ await rm(root, { recursive: true, force: true });
89
+ await run("git", ["clone", "--", source.location, root], staging);
90
+ await run("git", ["checkout", "--detach", source.ref], root);
91
+ }
92
+ const commit = (await run("git", ["rev-parse", "HEAD"], root)).trim();
93
+ await rm(path.join(root, ".git"), { recursive: true, force: true });
94
+ return { root, commit };
95
+ }
96
+
97
+ // ---------- npm ----------
98
+
99
+ async function fetchNpm(spec: string, staging: string, log?: (text: string) => void): Promise<string> {
100
+ const work = path.join(staging, "npm");
101
+ await mkdir(work, { recursive: true });
102
+ await writeFile(path.join(work, "package.json"), JSON.stringify({ name: "jade-plugin-download", private: true }));
103
+ log?.(`downloading ${spec}`);
104
+ const pm = packageManager();
105
+ await run(pm.cmd, [...pm.prefix, ...pm.add(spec)], work);
106
+
107
+ const deps = Object.keys((JSON.parse(await readFile(path.join(work, "package.json"), "utf8")).dependencies ?? {}) as Record<string, string>);
108
+ if (deps.length !== 1) throw new Error(`could not tell which package ${spec} installed`);
109
+ const root = path.join(staging, "package");
110
+ // 包的依赖装在外层的 node_modules 里;复制过去之后在插件目录里重新安装
111
+ await cp(path.join(work, "node_modules", deps[0]!), root, { recursive: true, dereference: true });
112
+ // bun 在包目录里留下的标记,不属于插件
113
+ await rm(path.join(root, ".bun-tag"), { force: true });
114
+ await rm(work, { recursive: true, force: true });
115
+ return root;
116
+ }
117
+
118
+ async function installDependencies(dir: string, log?: (text: string) => void): Promise<void> {
119
+ let pkg: { dependencies?: Record<string, string> };
120
+ try {
121
+ pkg = JSON.parse(await readFile(path.join(dir, "package.json"), "utf8"));
122
+ } catch {
123
+ return;
124
+ }
125
+ if (!pkg.dependencies || Object.keys(pkg.dependencies).length === 0) return;
126
+ log?.("installing dependencies");
127
+ const pm = packageManager();
128
+ await run(pm.cmd, [...pm.prefix, ...pm.install], dir);
129
+ }
130
+
131
+ interface PackageManager {
132
+ cmd: string;
133
+ /** 放在子命令前面的参数(npm 的 CLI 脚本路径) */
134
+ prefix: string[];
135
+ add(spec: string): string[];
136
+ install: string[];
137
+ }
138
+
139
+ /** 跑在 Bun 下用 bun 自己;跑在 Node 下用 npm(Windows 上的 npm.cmd 不能直接执行,用 node 运行 npm 的 CLI 脚本) */
140
+ function packageManager(): PackageManager {
141
+ if (process.versions.bun) {
142
+ return {
143
+ cmd: process.execPath,
144
+ prefix: [],
145
+ add: (spec) => ["add", "--ignore-scripts", spec],
146
+ install: ["install", "--production", "--ignore-scripts"],
147
+ };
148
+ }
149
+ const npm = npmCommand();
150
+ if (!npm) throw new Error("npm was not found; it is needed to install plugins from npm or plugins with dependencies");
151
+ const quiet = ["--ignore-scripts", "--no-audit", "--no-fund", "--no-package-lock", "--loglevel=error"];
152
+ return {
153
+ cmd: npm[0]!,
154
+ prefix: npm.slice(1),
155
+ add: (spec) => ["install", ...quiet, spec],
156
+ install: ["install", "--omit=dev", ...quiet],
157
+ };
158
+ }
159
+
160
+ /** npm 的 node 脚本;没有 npm 时返回 undefined */
161
+ export function npmCommand(): string[] | undefined {
162
+ const dirs = [path.dirname(process.execPath)];
163
+ const onPath = which(process.platform === "win32" ? "npm.cmd" : "npm");
164
+ if (onPath) dirs.push(path.dirname(onPath));
165
+ for (const dir of dirs) {
166
+ for (const cli of [
167
+ path.join(dir, "node_modules", "npm", "bin", "npm-cli.js"),
168
+ path.join(dir, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"),
169
+ ]) {
170
+ if (existsSync(cli)) return [process.execPath, cli];
171
+ }
172
+ }
173
+ // POSIX 上 npm 本身就是可执行的脚本
174
+ if (onPath && process.platform !== "win32") return [onPath];
175
+ return undefined;
176
+ }
177
+
178
+ /** 在 PATH 里找一个可执行文件;Windows 上按 PATHEXT 补扩展名 */
179
+ export function which(bin: string): string | undefined {
180
+ const win = process.platform === "win32";
181
+ const exts = win ? ["", ...(process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
182
+ const dirs = (process.env.PATH ?? process.env.Path ?? "").split(path.delimiter);
183
+ for (const raw of dirs) {
184
+ const dir = raw.replace(/^"(.*)"$/, "$1");
185
+ if (!dir) continue;
186
+ for (const ext of exts) {
187
+ if (win && ext === "" && !path.extname(bin)) continue;
188
+ const file = path.join(dir, bin + ext);
189
+ try {
190
+ if (!statSync(file).isFile()) continue;
191
+ if (!win) accessSync(file, constants.X_OK);
192
+ return file;
193
+ } catch {
194
+ // 继续找
195
+ }
196
+ }
197
+ }
198
+ return undefined;
199
+ }
200
+
201
+ function run(cmd: string, args: string[], cwd: string): Promise<string> {
202
+ return new Promise((resolve, reject) => {
203
+ execFile(
204
+ cmd,
205
+ args,
206
+ { cwd, windowsHide: true, maxBuffer: 16 * 1024 * 1024, env: { ...process.env, GIT_TERMINAL_PROMPT: "0" } },
207
+ (err, stdout, stderr) => {
208
+ if (!err) return resolve(stdout);
209
+ const detail = `${stderr || stdout}`.trim().split(/\r?\n/).slice(-6).join("\n");
210
+ const missing = (err as NodeJS.ErrnoException).code === "ENOENT";
211
+ const name = args[0]?.endsWith(".js") ? `npm ${args[1]}` : `${path.basename(cmd)} ${args[0]}`;
212
+ reject(new Error(missing ? `${cmd} is not installed or not on PATH` : `${name} failed${detail ? `:\n${detail}` : ""}`));
213
+ },
214
+ );
215
+ });
216
+ }
package/src/hash.ts ADDED
@@ -0,0 +1,39 @@
1
+ // 插件目录内容的哈希:安装时记下来,更新时用来判断装好的插件有没有被手动改过。
2
+ //
3
+ // 和内核的 hashDir 不同,这里只算插件自己的源码:
4
+ // - node_modules 是包装出来的,不参与;
5
+ // - .git 不参与;
6
+ // - 包管理器生成的 lock 文件不参与(更新时会被重新生成)。
7
+
8
+ import { createHash } from "node:crypto";
9
+ import { readFile, readdir, readlink } from "node:fs/promises";
10
+ import path from "node:path";
11
+
12
+ const SKIP_DIRS: ReadonlySet<string> = new Set(["node_modules", ".git"]);
13
+ const SKIP_FILES: ReadonlySet<string> = new Set(["package-lock.json", "bun.lock", "bun.lockb", "yarn.lock", "pnpm-lock.yaml"]);
14
+
15
+ /** 按相对路径排序,依次计入路径、长度、内容;和 hashDir 的格式一致,但不跟随符号链接(只计入链接指向的路径) */
16
+ export async function treeHash(root: string): Promise<string> {
17
+ const entries = await readdir(root, { recursive: true, withFileTypes: true });
18
+ const files: string[] = [];
19
+ const links: string[] = [];
20
+ for (const e of entries) {
21
+ const abs = path.join(e.parentPath ?? root, e.name);
22
+ const rel = path.relative(root, abs).split(path.sep).join("/");
23
+ const parts = rel.split("/");
24
+ if (parts.some((p) => SKIP_DIRS.has(p))) continue;
25
+ if (SKIP_FILES.has(parts.at(-1) ?? "")) continue;
26
+ if (e.isFile()) files.push(rel);
27
+ else if (e.isSymbolicLink()) links.push(rel);
28
+ }
29
+ files.sort();
30
+ links.sort();
31
+ const h = createHash("sha256");
32
+ for (const rel of files) {
33
+ const content = await readFile(path.join(root, rel));
34
+ h.update(`${rel}\0${content.length}\0`).update(content);
35
+ }
36
+ // 符号链接不跟随,只计入它指向哪里:链接被换成指向别处时也看得出来。没有链接时哈希和以前一样
37
+ for (const rel of links) h.update(`${rel}\0->\0${await readlink(path.join(root, rel))}\0`);
38
+ return h.digest("hex");
39
+ }
package/src/host.ts ADDED
@@ -0,0 +1,429 @@
1
+ // 进程级的插件宿主:发现、解析、激活、服务注册、故障隔离、关闭(3.1、4.1、5.3、5.4)。
2
+
3
+ import path from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ import { importProjectModule, UntrustedPlugin, type Plugin, type TrustOptions } from "uncarved";
6
+ import {
7
+ API_VERSION,
8
+ PluginError,
9
+ ServiceUnavailable,
10
+ StaleContext,
11
+ type Caller,
12
+ type Disposer,
13
+ type Jade,
14
+ type PluginLayer,
15
+ type PluginModule,
16
+ type PluginSource,
17
+ type ProcessContext,
18
+ type Secrets,
19
+ } from "./api.ts";
20
+ import { pluginConfig, readConfigFile, type ConfigFile } from "./config.ts";
21
+ import { discover, type BrokenSource, type LayerRoot } from "./manifest.ts";
22
+ import { ambient, invoke, Scope, serviceHandle, type Owner } from "./membrane.ts";
23
+ import { startRemote } from "./remote.ts";
24
+ import { resolve, type Plan, type PlanState } from "./resolve.ts";
25
+ import { scopedSecrets, SecretsStore } from "./secrets.ts";
26
+ import { Sessions } from "./session.ts";
27
+ import { countFault, readFaults, readPluginsConfig, writeFaults, type Faults } from "./state.ts";
28
+
29
+ /** untrusted:项目层插件没有得到用户确认,没有执行它的任何代码 */
30
+ export type PluginState = PlanState | "broken" | "failed" | "untrusted" | "quarantined";
31
+
32
+ export interface PluginInfo {
33
+ id?: string;
34
+ version?: string;
35
+ layer: PluginLayer;
36
+ dir: string;
37
+ state: PluginState;
38
+ reason?: string;
39
+ }
40
+
41
+ export interface HostOptions {
42
+ roots: LayerRoot[];
43
+ /** $JADE_HOME:plugins.json、state/faults.json 所在目录 */
44
+ home: string;
45
+ /** 工作目录,默认 process.cwd() */
46
+ cwd?: string;
47
+ /** 配置文件(第 8 节):用户层默认 $JADE_HOME/config.json;项目层不写就没有 */
48
+ config?: { user?: string; project?: string };
49
+ flags?: readonly string[];
50
+ safe?: boolean;
51
+ /** 项目层插件的确认(第 7 节):确认记录和向用户提问的方式。不提供时不加载带代码的项目层插件 */
52
+ trust?: TrustOptions;
53
+ /** 要告诉用户的事(插件失败、被隔离、自动停用……),由前端或 CLI 显示 */
54
+ report?: (message: string) => void;
55
+ }
56
+
57
+ /** 核心自己的身份:核心通过服务句柄调用插件(例如 frontend@1)时的调用方 */
58
+ const CORE_ID = "jade";
59
+
60
+ /** 核心自己提供的诊断服务(3.4),名字要和 api.ts 的 Jade 一致 */
61
+ export const JADE_SERVICE = "jade@1";
62
+
63
+ interface Active {
64
+ source: PluginSource;
65
+ module: PluginModule;
66
+ /** 进程级作用域 */
67
+ owner: Owner;
68
+ /** 会话级作用域:隔离插件时一并关闭 */
69
+ sessions: Set<Scope>;
70
+ /** 项目层插件:把它交给内核的插件标记为已确认(内核拒绝未经确认的项目层插件) */
71
+ mark?: (p: Plugin) => Plugin;
72
+ }
73
+
74
+ /** 状态在启动时由 plugins.json、faults.json 和清单算出(不执行插件代码),之后只由宿主改动 */
75
+ export async function planPlugins(opts: Pick<HostOptions, "roots" | "home" | "flags" | "safe">): Promise<{ plan: Plan; broken: BrokenSource[]; faults: Faults }> {
76
+ const [{ sources, broken }, config, faults] = await Promise.all([discover(opts.roots), readPluginsConfig(opts.home), readFaults(opts.home)]);
77
+ const disabled = new Set([...config.disabled, ...faults.autoDisabled]);
78
+ const plan = resolve(sources, { apiVersion: API_VERSION, disabled, safe: opts.safe, flags: opts.flags, coreServices: [JADE_SERVICE] });
79
+ // 区分"用户停用"和"连续崩溃后自动停用"
80
+ for (const e of plan.entries) {
81
+ if (e.state === "disabled" && e.reason !== "safe mode" && !config.disabled.includes(e.source.manifest.id)) {
82
+ e.reason = `disabled after ${faults.streaks[e.source.manifest.id]?.count ?? "repeated"} faulty launches (last: ${faults.streaks[e.source.manifest.id]?.last ?? "unknown"}); run "jade plugins enable ${e.source.manifest.id}" to retry`;
83
+ }
84
+ }
85
+ return { plan, broken, faults };
86
+ }
87
+
88
+ export class PluginHost {
89
+ readonly plan: Plan;
90
+ readonly #opts: HostOptions;
91
+ readonly #broken: BrokenSource[];
92
+ readonly #faults: Faults;
93
+ /** 本次启动里已经记过故障的插件 */
94
+ readonly #faulted = new Set<string>();
95
+ readonly #status = new Map<string, { state: PluginState; reason?: string }>();
96
+ readonly #active: Active[] = [];
97
+ readonly #services = new Map<string, { impl: object | ((caller: Caller) => object); provider: Owner }>();
98
+ /** 按调用方创建的服务实例:调用方作用域 → 服务名 → 实例 */
99
+ readonly #perCaller = new WeakMap<Scope, Map<string, object>>();
100
+ readonly #core: Owner = { plugin: CORE_ID, scope: new Scope(CORE_ID) };
101
+ /** 每个插件自己的密钥;进程级和会话级 ctx 共享同一个实例 */
102
+ readonly #secrets: SecretsStore;
103
+ #config: { user: ConfigFile; project: ConfigFile } = { user: {}, project: {} };
104
+ readonly #pluginConfigs = new Map<string, Readonly<Record<string, unknown>>>();
105
+ #sessions: Sessions | undefined;
106
+ #writing: Promise<void> = Promise.resolve();
107
+
108
+ private constructor(opts: HostOptions, plan: Plan, broken: BrokenSource[], faults: Faults) {
109
+ this.#opts = opts;
110
+ this.plan = plan;
111
+ this.#broken = broken;
112
+ this.#faults = faults;
113
+ this.#secrets = new SecretsStore(opts.home);
114
+ for (const e of plan.entries) this.#status.set(e.source.dir, { state: e.state, ...(e.reason ? { reason: e.reason } : {}) });
115
+ // 核心自己的诊断服务:插件在激活阶段就能取(`/plugins` 用它)
116
+ this.#services.set(JADE_SERVICE, {
117
+ impl: {
118
+ plugins: () => this.status().map((p) => ({ ...p })),
119
+ services: () => [...this.#services].map(([service, e]) => ({ service, provider: e.provider.plugin })).sort((a, b) => a.service.localeCompare(b.service)),
120
+ } satisfies Jade,
121
+ provider: this.#core,
122
+ });
123
+ }
124
+
125
+ /** 解析并按顺序激活全部插件。单个插件失败不影响其他插件,不抛出 */
126
+ static async start(opts: HostOptions): Promise<PluginHost> {
127
+ const { plan, broken, faults } = await planPlugins(opts);
128
+ const host = new PluginHost(opts, plan, broken, faults);
129
+ const [user, project] = await Promise.all([
130
+ readConfigFile(opts.config?.user ?? path.join(opts.home, "config.json")),
131
+ opts.config?.project ? readConfigFile(opts.config.project) : { config: {}, error: undefined },
132
+ ]);
133
+ host.#config = { user: user.config, project: project.config };
134
+ for (const e of [user.error, project.error]) if (e) host.#report(e);
135
+ for (const w of plan.warnings) host.#report(w);
136
+ // 缺依赖、成环而没加载的插件:例如停用 jade/shell 之后 jade/checks 也不会运行,不能悄悄发生
137
+ for (const e of plan.entries) if (e.state === "skipped" && !e.expected) host.#report(`plugin ${e.source.manifest.id} is not loaded: ${e.reason}`);
138
+ for (const b of broken) host.#report(`plugin at ${b.dir}: ${b.error}`);
139
+ for (const source of plan.order) await host.#activate(source);
140
+ await host.#saveFaults();
141
+ return host;
142
+ }
143
+
144
+ /** 每个插件的状态和原因,包括读不出清单的目录 */
145
+ status(): PluginInfo[] {
146
+ const infos: PluginInfo[] = this.plan.entries.map((e) => {
147
+ const s = this.#status.get(e.source.dir)!;
148
+ return { id: e.source.manifest.id, version: e.source.manifest.version, layer: e.source.layer, dir: e.source.dir, state: s.state, ...(s.reason ? { reason: s.reason } : {}) };
149
+ });
150
+ for (const b of this.#broken) infos.push({ layer: b.layer, dir: b.dir, state: "broken", reason: b.error });
151
+ return infos;
152
+ }
153
+
154
+ /** 核心取服务(例如 frontend@1、provider@1)。没有提供者时返回 undefined */
155
+ service<T extends object>(name: string): T | undefined {
156
+ return this.#handle<T>(name, this.#core);
157
+ }
158
+
159
+ /** 一个插件的密钥;会话级 ctx 也用它,和进程级共享同一份 */
160
+ secrets(plugin: string): Secrets {
161
+ return this.#secrets.for(plugin);
162
+ }
163
+
164
+ /**
165
+ * 处理没人接住的异常(5.3)。归属顺序:AsyncLocalStorage → 错误栈里的插件目录 → 未归属。
166
+ * 能归到插件的隔离它;未归属的记下来,进程不退出
167
+ */
168
+ handleUncaught(error: unknown): void {
169
+ const plugin = ambient.getStore()?.plugin ?? (error instanceof PluginError ? error.plugin : undefined) ?? this.#blame(error);
170
+ const message = error instanceof Error ? error.message : String(error);
171
+ if (plugin && plugin !== CORE_ID) {
172
+ void this.quarantine(plugin, message);
173
+ return;
174
+ }
175
+ this.#faults.unattributed.push({ at: new Date().toISOString(), error: error instanceof Error ? (error.stack ?? message) : message });
176
+ this.#report(`unhandled error not attributable to any plugin: ${message}`);
177
+ void this.#saveFaults();
178
+ }
179
+
180
+ /** 安装进程级的异常处理器,返回卸载函数 */
181
+ installProcessHandlers(): () => void {
182
+ const handler = (e: unknown) => this.handleUncaught(e);
183
+ process.on("uncaughtException", handler);
184
+ process.on("unhandledRejection", handler);
185
+ return () => {
186
+ process.off("uncaughtException", handler);
187
+ process.off("unhandledRejection", handler);
188
+ };
189
+ }
190
+
191
+ /** 隔离一个插件:关闭它的作用域、撤销它的注册,它提供的服务随之失效(5.3) */
192
+ async quarantine(id: string, reason: string): Promise<void> {
193
+ const i = this.#active.findIndex((a) => a.source.manifest.id === id);
194
+ if (i === -1) return;
195
+ const [a] = this.#active.splice(i, 1);
196
+ this.#status.set(a!.source.dir, { state: "quarantined", reason });
197
+ this.#report(`plugin ${id} was stopped after an error: ${reason}`);
198
+ this.#fault(id, reason);
199
+ // 先会话级、再进程级:会话里的东西可能用着进程级的服务
200
+ for (const s of [...a!.sessions, a!.owner.scope]) {
201
+ for (const e of await s.close()) this.#report(String(e instanceof Error ? e.message : e));
202
+ }
203
+ await this.#saveFaults();
204
+ }
205
+
206
+ /** 会话:新建、恢复、切换(4.2) */
207
+ get sessions(): Sessions {
208
+ return (this.#sessions ??= new Sessions({
209
+ home: this.#opts.home,
210
+ cwd: this.#cwd,
211
+ config: (source) => this.#configOf(source),
212
+ core: this.#core,
213
+ active: () => this.#active,
214
+ service: (name, caller) => this.#handle(name, caller),
215
+ provider: (name) => this.plan.providers.get(name),
216
+ secrets: (plugin) => this.#secrets.for(plugin),
217
+ track: (plugin, scope) => this.#active.find((a) => a.source.manifest.id === plugin)?.sessions.add(scope),
218
+ untrack: (plugin, scope) => this.#active.find((a) => a.source.manifest.id === plugin)?.sessions.delete(scope),
219
+ fault: (plugin, reason) => {
220
+ this.#fault(plugin, reason);
221
+ void this.#saveFaults();
222
+ },
223
+ report: (m) => this.#report(m),
224
+ }));
225
+ }
226
+
227
+ /**
228
+ * 逆序关闭全部插件。这次启动里跑过、没出故障的插件,连续故障计数清零;
229
+ * 没有加载的(自动停用、用户停用、安全模式)不算,保留它们的记录和最后一次的错误
230
+ */
231
+ async close(): Promise<void> {
232
+ if (this.#sessions) await this.#sessions.close();
233
+ await this.#core.scope.close();
234
+ const ran = new Set(this.#active.map((a) => a.source.manifest.id));
235
+ while (this.#active.length > 0) {
236
+ const a = this.#active.pop()!;
237
+ for (const e of await a.owner.scope.close()) this.#report(String(e instanceof Error ? e.message : e));
238
+ }
239
+ for (const id of Object.keys(this.#faults.streaks)) if (ran.has(id) && !this.#faulted.has(id)) delete this.#faults.streaks[id];
240
+ await this.#saveFaults();
241
+ }
242
+
243
+ // ---------- 激活 ----------
244
+
245
+ async #activate(source: PluginSource): Promise<void> {
246
+ const { manifest, dir } = source;
247
+ const id = manifest.id;
248
+ // 必需服务的提供者激活失败:不激活,原因精确到服务
249
+ for (const need of manifest.requires ?? []) {
250
+ const provider = this.plan.providers.get(need);
251
+ if (provider && !this.#services.has(need)) {
252
+ this.#status.set(dir, { state: "skipped", reason: `requires ${need}: its provider ${provider} failed to activate` });
253
+ return;
254
+ }
255
+ }
256
+ const scope = new Scope(id);
257
+ const owner: Owner = { plugin: id, scope };
258
+ try {
259
+ const { module, mark } = await this.#load(source, owner);
260
+ if (module.process) await invoke(owner, "process", () => module.process!(this.#processContext(source, owner)));
261
+ const missing = (manifest.provides ?? []).filter((s) => this.#services.get(s)?.provider !== owner);
262
+ if (missing.length > 0) throw new Error(`declares ${missing.join(", ")} in provides but did not provide it`);
263
+ this.#active.push({ source, module, owner, sessions: new Set(), ...(mark ? { mark } : {}) });
264
+ } catch (e) {
265
+ // 事务性:已经做的注册全部撤销(4.1)
266
+ await scope.close();
267
+ const reason = e instanceof PluginError && e.plugin === id ? String((e.cause as Error)?.message ?? e.message) : e instanceof Error ? e.message : String(e);
268
+ // 用户没有确认不是插件的故障,不计入连续崩溃
269
+ const untrusted = e instanceof UntrustedPlugin;
270
+ this.#status.set(dir, { state: untrusted ? "untrusted" : "failed", reason });
271
+ this.#report(`plugin ${id} ${untrusted ? "was not loaded" : "failed to activate"}: ${reason}`);
272
+ if (!untrusted) this.#fault(id, reason);
273
+ }
274
+ }
275
+
276
+ async #load(source: PluginSource, owner: Owner): Promise<{ module: PluginModule; mark?: (p: Plugin) => Plugin }> {
277
+ const { manifest, dir } = source;
278
+ if (manifest.runtime) {
279
+ // 进程外的项目层插件:内核的确认只能检查 JS 的 import,管不住另一种语言的代码从目录外加载什么
280
+ if (source.layer === "project") throw new Error(`out-of-process plugins cannot be loaded from a project yet; put it in ${path.join(this.#opts.home, "plugins")}`);
281
+ const module = await startRemote(source, owner, { report: (m) => this.#report(m), exited: (reason) => void this.quarantine(manifest.id, reason) });
282
+ return { module };
283
+ }
284
+ let mod: { default?: unknown };
285
+ let mark: ((p: Plugin) => Plugin) | undefined;
286
+ if (source.layer === "project") {
287
+ // 项目层:内核负责确认(哈希、import 不出目录、请求确认、导入),之前不执行任何代码(第 7 节)
288
+ const trust = this.#opts.trust;
289
+ if (!trust) throw new UntrustedPlugin(dir, "project plugins need confirmation, which this jade cannot ask for");
290
+ // 模块顶层代码也归这个插件(顶层启动的定时器出错时能找到它)
291
+ const loaded = await ambient.run(owner, () => importProjectModule(dir, { ...trust, entry: manifest.entry! }));
292
+ mod = loaded.module as { default?: unknown };
293
+ mark = loaded.mark;
294
+ } else {
295
+ const file = path.resolve(dir, manifest.entry!);
296
+ const rel = path.relative(dir, file);
297
+ if (rel.startsWith("..") || path.isAbsolute(rel)) throw new Error(`entry ${manifest.entry} is outside the plugin directory`);
298
+ mod = (await ambient.run(owner, () => import(pathToFileURL(file).href))) as { default?: unknown };
299
+ }
300
+ const m = mod.default;
301
+ if (typeof m !== "object" || m === null) throw new Error(`entry ${manifest.entry} must default-export definePlugin({ ... })`);
302
+ return { module: m as PluginModule, ...(mark ? { mark } : {}) };
303
+ }
304
+
305
+ #processContext(source: PluginSource, owner: Owner): ProcessContext {
306
+ const { manifest } = source;
307
+ const id = manifest.id;
308
+ // 每个方法都先检查:作用域关闭后旧 ctx 一律抛 StaleContext(4.1)
309
+ const declared = (name: string, lists: (readonly string[] | undefined)[]) => {
310
+ if (owner.scope.closed) throw new StaleContext(id);
311
+ if (!lists.some((l) => l?.includes(name))) throw new Error(`plugin ${id}: ${name} is not declared in plugin.json`);
312
+ };
313
+ return {
314
+ plugin: id,
315
+ signal: owner.scope.signal,
316
+ cwd: this.#cwd,
317
+ home: this.#opts.home,
318
+ config: this.#configOf(source),
319
+ secrets: scopedSecrets(this.#secrets.for(id), () => owner.scope.closed, id),
320
+ require: <T extends object>(name: string): T => {
321
+ declared(name, [manifest.requires]);
322
+ const h = this.#handle<T>(name, owner);
323
+ if (!h) throw new ServiceUnavailable(name, this.plan.providers.get(name) ?? "(none)");
324
+ return h;
325
+ },
326
+ service: <T extends object>(name: string): T | undefined => {
327
+ declared(name, [manifest.requires, manifest.optional]);
328
+ return this.#handle<T>(name, owner);
329
+ },
330
+ provide: (name: string, impl: object | ((caller: Caller) => object)): void => {
331
+ declared(name, [manifest.provides]);
332
+ if (this.plan.providers.get(name) !== id) throw new Error(`plugin ${id}: ${name} is provided by ${this.plan.providers.get(name)}`);
333
+ if (this.#services.has(name)) throw new Error(`plugin ${id}: ${name} is already provided`);
334
+ owner.scope.add(() => {
335
+ if (this.#services.get(name)?.provider === owner) this.#services.delete(name);
336
+ });
337
+ this.#services.set(name, { impl, provider: owner });
338
+ },
339
+ effect: async (start): Promise<void> => {
340
+ if (owner.scope.closed) throw new StaleContext(id);
341
+ // 插件调用自己的代码,不过膜:膜会把返回的清理函数也包起来,作用域关闭时它就被当成过期调用拒绝了。
342
+ // Scope.close 自己以插件身份执行清理函数、接住错误
343
+ const d = (await ambient.run(owner, start)) as Disposer | void;
344
+ if (typeof d === "function") owner.scope.add(d);
345
+ },
346
+ };
347
+ }
348
+
349
+ #handle<T extends object>(name: string, caller: Owner): T | undefined {
350
+ const entry = this.#services.get(name);
351
+ if (!entry) return undefined;
352
+ const { impl, provider } = entry;
353
+ const isLive = () => this.#services.get(name)?.provider === provider && !provider.scope.closed;
354
+ if (typeof impl !== "function") return serviceHandle<T>(name, impl, provider, caller, isLive);
355
+
356
+ let mine = this.#perCaller.get(caller.scope);
357
+ if (!mine) this.#perCaller.set(caller.scope, (mine = new Map()));
358
+ let instance = mine.get(name);
359
+ if (!instance) {
360
+ const info: Caller = {
361
+ plugin: caller.plugin,
362
+ layer: caller === this.#core ? "core" : (this.plan.entries.find((e) => e.source.manifest.id === caller.plugin)?.source.layer ?? "core"),
363
+ signal: caller.scope.signal,
364
+ onDispose: (d) => caller.scope.add(d, provider),
365
+ };
366
+ try {
367
+ instance = ambient.run(provider, () => impl(info));
368
+ } catch (e) {
369
+ throw new PluginError(provider.plugin, `${name} for ${caller.plugin}`, e);
370
+ }
371
+ if (typeof instance !== "object" || instance === null) throw new PluginError(provider.plugin, name, new Error("the service factory must return an object"));
372
+ mine.set(name, instance);
373
+ }
374
+ return serviceHandle<T>(name, instance, provider, caller, isLive);
375
+ }
376
+
377
+ get #cwd(): string {
378
+ return this.#opts.cwd ?? process.cwd();
379
+ }
380
+
381
+ /** 插件的配置;项目层设置了不允许设置的键时告诉用户一次 */
382
+ #configOf(source: PluginSource): Readonly<Record<string, unknown>> {
383
+ const id = source.manifest.id;
384
+ let c = this.#pluginConfigs.get(id);
385
+ if (!c) {
386
+ const { config, ignored } = pluginConfig(source, this.#config.user, this.#config.project);
387
+ if (ignored.length > 0) this.#report(`project config for ${id}: ${ignored.join(", ")} can only be set in your own config.json; ignored`);
388
+ this.#pluginConfigs.set(id, (c = config));
389
+ }
390
+ return c;
391
+ }
392
+
393
+ // ---------- 故障 ----------
394
+
395
+ /** 错误栈里第一个落在某个激活插件目录下的帧(5.3 第 2 步) */
396
+ #blame(error: unknown): string | undefined {
397
+ const stack = error instanceof Error ? error.stack : undefined;
398
+ if (!stack) return undefined;
399
+ const norm = (p: string) => p.replaceAll("\\", "/").toLowerCase();
400
+ let decoded = stack;
401
+ try {
402
+ decoded = decodeURIComponent(stack); // file:// URL 里的空格等被编码过
403
+ } catch {}
404
+ const frames = norm(decoded);
405
+ let best: { id: string; at: number } | undefined;
406
+ for (const a of this.#active) {
407
+ const at = frames.indexOf(norm(a.source.dir) + "/");
408
+ if (at !== -1 && (!best || at < best.at)) best = { id: a.source.manifest.id, at };
409
+ }
410
+ return best?.id;
411
+ }
412
+
413
+ #fault(id: string, reason: string): void {
414
+ if (this.#faulted.has(id)) return;
415
+ this.#faulted.add(id);
416
+ if (countFault(this.#faults, id, reason)) {
417
+ this.#report(`plugin ${id} faulted in ${this.#faults.streaks[id]!.count} launches in a row and will be disabled from the next launch; run "jade plugins enable ${id}" to retry`);
418
+ }
419
+ }
420
+
421
+ #saveFaults(): Promise<void> {
422
+ this.#writing = this.#writing.then(() => writeFaults(this.#opts.home, this.#faults)).catch((e) => this.#report(`cannot save faults: ${e instanceof Error ? e.message : e}`));
423
+ return this.#writing;
424
+ }
425
+
426
+ #report(message: string): void {
427
+ this.#opts.report?.(message);
428
+ }
429
+ }