@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
@@ -0,0 +1,213 @@
1
+ // jade/models:按 $JADE_HOME/.env 和环境变量选模型服务(和旧 jade 的变量相同),外面包一层重试和限速。
2
+ // provider@1 给核心建会话用;models@1 给其他插件(压缩、子 Agent、提示词)调用模型、查看当前配置。
3
+ //
4
+ // /model 改了配置之后不用重开会话:provider@1 给核心的是一个转发到当前 provider 的壳(设计第 10 节),
5
+ // 下一次请求就用新的。
6
+
7
+ import path from "node:path";
8
+ import type { ModelRequest, ModelResponse, Provider } from "uncarved";
9
+ import { definePlugin, type UI } from "../../src/api.ts";
10
+ import type { Commands } from "../commands/index.ts";
11
+ import { contextWindow } from "./context.ts";
12
+ import { envWithFile, hasProvider, parseTokens } from "./config.ts";
13
+ import { configurator } from "./configure.ts";
14
+ import { isBuiltinKind, listModels, MODEL_KEY, providerFromEnv, providerInfo, providerKind, RateGate, rpmFromEnv, visionFromEnv, withRetry } from "./provider.ts";
15
+ import { readSavedServices, type Env } from "./saved.ts";
16
+
17
+ /** models@1 */
18
+ export interface Models {
19
+ /** 当前配置的服务和模型;没有配置时 undefined。via 是给人看的服务说明,例如 "the Anthropic API" */
20
+ current(): {
21
+ kind: string;
22
+ model: string;
23
+ via: string;
24
+ /** 上下文窗口(token) */
25
+ contextWindow: number;
26
+ /** 模型能不能看图片(JADE_VISION;不设时按服务推断) */
27
+ vision: boolean;
28
+ /** 每次回复的输出上限(JADE_MAX_TOKENS,含 .env 里的);不设或写错时没有,用服务的默认值 */
29
+ maxTokens?: number;
30
+ } | undefined;
31
+ /**
32
+ * 完成一次请求,带重试和限速。默认用当前配置的模型(限速和会话共用);
33
+ * model 是 /model 保存过的模型名($JADE_HOME/models.json),用它和它自己的设置,找不到时报错
34
+ */
35
+ complete(request: ModelRequest, options: { signal: AbortSignal; onDelta?: (delta: import("uncarved").Delta) => void; model?: string }): Promise<ModelResponse>;
36
+ /** 服务提供的模型列表;不支持或出错时为空 */
37
+ list(): Promise<string[]>;
38
+ /** /model 保存过的模型名(complete 的 model 能用的那些),最近用过的在前 */
39
+ saved?(): Promise<string[]>;
40
+ /** 配置换了(/model)时调用,返回取消订阅。前端用它更新状态栏 */
41
+ subscribe?(fn: () => void): () => void;
42
+ }
43
+
44
+ export default definePlugin({
45
+ async process(ctx) {
46
+ const envFile = path.join(ctx.home, ".env");
47
+ const modelsFile = path.join(ctx.home, "models.json");
48
+ // 当前的模型配置。改配置时整份换掉(updateEnvFile 改的是这一份,apply 再从文件读一遍)
49
+ let env = await envWithFile(envFile);
50
+ const ui = ctx.service<UI>("ui@1");
51
+ const watchers = new Set<() => void>();
52
+ const notify = () => {
53
+ for (const fn of watchers) {
54
+ try {
55
+ fn();
56
+ } catch {
57
+ // 订阅者的错不连累模型配置
58
+ }
59
+ }
60
+ };
61
+
62
+ const notConfigured = () =>
63
+ new Error(
64
+ `no model is configured. Run /model in a session, or put one of these in ${envFile} or the shell:\n` +
65
+ " ANTHROPIC_API_KEY=... (Claude)\n" +
66
+ " OPENAI_API_KEY=... (OpenAI)\n" +
67
+ " OPENAI_COMPAT_BASE_URL=... and OPENAI_COMPAT_MODEL=... (an OpenAI-compatible service)",
68
+ );
69
+
70
+ let made: Promise<Provider> | undefined;
71
+ // 用创建时的这份配置:之后换了配置,新的一份自己建 provider
72
+ const build = (snapshot: Env): Promise<Provider> => {
73
+ return (async () => {
74
+ const gate = new RateGate({ rpm: rpmFromEnv(snapshot), onLearn: (rpm) => ui?.notify(`rate limited: spacing requests to ${rpm} per minute from now on`, "warning") });
75
+ return withRetry(await providerFromEnv(snapshot), {
76
+ gate,
77
+ onRetry: ({ attempt, retries, delayMs, error }) =>
78
+ ui?.notify(`model request failed (${error.message}); retry ${attempt} of ${retries} in ${Math.round(delayMs / 1000)}s`, "warning"),
79
+ });
80
+ })();
81
+ };
82
+
83
+ // 第一次用时才创建:配置写错时错误出现在这一轮里,而不是让插件激活失败、连会话都打不开
84
+ const provider = (): Promise<Provider> => {
85
+ if (!hasProvider(env)) return Promise.reject(notConfigured());
86
+ if (made) return made;
87
+ const p = build(env);
88
+ made = p;
89
+ // 创建失败时下次重试(例如密钥刚写进 .env)
90
+ p.catch(() => {
91
+ if (made === p) made = undefined;
92
+ });
93
+ return p;
94
+ };
95
+
96
+ /** 保存过的模型的 provider,按配置缓存:models.json 里的密钥或设置改了就另建一份 */
97
+ const savedProviders = new Map<string, Promise<Provider>>();
98
+ const saved = async (name: string): Promise<Provider> => {
99
+ for (const s of await readSavedServices(modelsFile)) {
100
+ const m = s.models.find((x) => x.name === name);
101
+ const kind = providerKind(s.env);
102
+ if (!m || !isBuiltinKind(kind)) continue;
103
+ const snapshot: Env = { ...s.env, ...m.settings, JADE_PROVIDER: kind, [MODEL_KEY[kind]]: m.name };
104
+ const key = JSON.stringify(snapshot);
105
+ const cached = savedProviders.get(key);
106
+ if (cached) return cached;
107
+ const p = build(snapshot);
108
+ savedProviders.set(key, p);
109
+ // 创建失败时下次重试
110
+ p.catch(() => savedProviders.delete(key));
111
+ return p;
112
+ }
113
+ // 只写在 .env 里、没保存过的当前模型
114
+ if (hasProvider(env) && providerInfo(env).model === name) return provider();
115
+ throw new Error(`model ${JSON.stringify(name)} is not one of the models saved by /model (${modelsFile})`);
116
+ };
117
+
118
+ const current: Provider["complete"] = (request, options) => provider().then((p) => p.complete(request, options));
119
+ const complete: Models["complete"] = (request, { model, ...options }) => (model ? saved(model).then((p) => p.complete(request, options)) : current(request, options));
120
+
121
+ // 转发的 provider:会话创建时固定的是这个壳,/model 之后下一次请求就用新的配置
122
+ ctx.provide("provider@1", { id: "jade/models", complete: current } satisfies Provider);
123
+
124
+ ctx.provide("models@1", {
125
+ current() {
126
+ if (!hasProvider(env)) return undefined;
127
+ const info = providerInfo(env);
128
+ let vision = true;
129
+ try {
130
+ vision = visionFromEnv(env);
131
+ } catch {
132
+ // 写错的 JADE_VISION 在创建 provider 时报错,这里当作不能看
133
+ vision = false;
134
+ }
135
+ let maxTokens: number | undefined;
136
+ try {
137
+ maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
138
+ } catch {
139
+ // 写错的在创建 provider 时报错
140
+ }
141
+ return {
142
+ kind: info.kind,
143
+ model: info.model,
144
+ via: via(info.kind, info.baseURL),
145
+ contextWindow: contextWindow(info.model, env),
146
+ vision,
147
+ ...(maxTokens === undefined ? {} : { maxTokens }),
148
+ };
149
+ },
150
+ complete,
151
+ list: () => listModels(env),
152
+ async saved() {
153
+ const names = (await readSavedServices(modelsFile)).flatMap((s) => (isBuiltinKind(providerKind(s.env)) ? s.models.map((m) => m.name) : []));
154
+ return [...new Set(names)];
155
+ },
156
+ subscribe(fn: () => void) {
157
+ watchers.add(fn);
158
+ return () => void watchers.delete(fn);
159
+ },
160
+ } satisfies Models);
161
+
162
+ /** /model 写完配置之后:重新读 .env、换掉 provider;配置不完整时抛出,旧的配置和 provider 继续用 */
163
+ const apply = async () => {
164
+ const next = await envWithFile(envFile);
165
+ const before = { env, provider: made };
166
+ env = next;
167
+ made = undefined;
168
+ try {
169
+ await provider();
170
+ } catch (e) {
171
+ env = before.env;
172
+ made = before.provider;
173
+ throw e;
174
+ }
175
+ notify();
176
+ };
177
+
178
+ ctx.service<Commands>("commands@1")?.register({
179
+ name: "model",
180
+ args: "[name]",
181
+ description: "switch models, or add one with its provider, API key and limits",
182
+ // 只改配置,不碰会话:模型工作时也立即执行
183
+ instant: true,
184
+ async run(args, c) {
185
+ const configure = configurator({
186
+ envFile,
187
+ modelsFile,
188
+ env: () => env,
189
+ apply,
190
+ report: (text) => c.print(text),
191
+ ui: c.ui,
192
+ signal: c.signal,
193
+ });
194
+ await configure.model(args.trim() || undefined);
195
+ },
196
+ });
197
+ },
198
+ });
199
+
200
+ function via(kind: string, baseURL: string | undefined): string {
201
+ const host = baseURL ? safeHost(baseURL) : undefined;
202
+ if (kind === "anthropic") return host && host !== "api.anthropic.com" ? `the Anthropic API at ${host}` : "the Anthropic API";
203
+ if (kind === "openai") return host && host !== "api.openai.com" ? `the OpenAI Responses API at ${host}` : "the OpenAI API";
204
+ return host ? `an OpenAI-compatible API at ${host}` : `the ${kind} provider`;
205
+ }
206
+
207
+ function safeHost(url: string): string | undefined {
208
+ try {
209
+ return new URL(url).host;
210
+ } catch {
211
+ return undefined;
212
+ }
213
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "id": "jade/models",
3
+ "version": "0.2.0",
4
+ "api": "^1",
5
+ "entry": "./index.ts",
6
+ "provides": ["provider@1", "models@1"],
7
+ "optional": ["ui@1", "commands@1"]
8
+ }
@@ -184,72 +184,10 @@ type Env = Record<string, string | undefined>;
184
184
 
185
185
  export const isBuiltinKind = (kind: string): kind is ProviderKind => (PROVIDER_KINDS as readonly string[]).includes(kind);
186
186
 
187
- /** 插件提供的模型服务(JadePlugin.providers),用 JADE_PROVIDER=<kind> 选择 */
188
- export interface ProviderDef {
189
- /** JADE_PROVIDER 的值:字母、数字、-、_,不能和内置的 openai-compat、openai、anthropic 重名 */
190
- kind: string;
191
- /** 当前配置的模型名(状态栏、/model 显示,上下文窗口按它推断);env 是 jade 的环境变量(含 $JADE_HOME/.env) */
192
- model(env: Env): string;
193
- /** 系统提示词里告诉模型它经什么服务运行,例如 "the Foo API at api.foo.com";不给时是 "the <kind> provider" */
194
- via?(env: Env): string;
195
- /** 按 env 创建 Provider(uncarved 的接口);缺配置时抛错,信息直接显示给用户。jade 在外面包一层重试和限速 */
196
- create(env: Env): Provider | Promise<Provider>;
197
- /** /model 里列出可选的模型;不给时 /model 不能列出模型 */
198
- listModels?(env: Env): Promise<string[]>;
199
- /** 服务的配置项:/model 添加这个服务时依次询问,写进 $JADE_HOME/.env;不给时只能手动写 .env */
200
- settings?: ProviderSetting[];
201
- }
202
-
203
- /** 插件服务的一个配置项,对应一个环境变量 */
204
- export interface ProviderSetting {
205
- /** 环境变量名,大写字母、数字、_,例如 "FOO_API_KEY";不能是 JADE_ 开头的 */
206
- env: string;
207
- /** /model 里显示的名字,例如 "API key" */
208
- label: string;
209
- /** 输入时遮住(密钥);留空时沿用已有的值 */
210
- secret?: boolean;
211
- /** 这一项是模型名:/model 从 listModels 里选、换模型时只改它。最多一项 */
212
- model?: boolean;
213
- /** 不能为空 */
214
- required?: boolean;
215
- /** 输入框里的提示文字 */
216
- placeholder?: string;
217
- }
218
-
219
- export const SETTING_ENV = /^[A-Z][A-Z0-9_]*$/;
220
-
221
- export const PROVIDER_KIND = /^[a-z0-9][a-z0-9_-]*$/i;
222
-
223
- /** 插件注册的服务;只在启动时注册一次,整个进程共用 */
224
- const plugged = new Map<string, { plugin: string; def: ProviderDef }>();
225
-
226
- /** 注册插件提供的服务;返回要告诉用户的问题(名字不合法、和内置的或先注册的重名) */
227
- export function registerProviders(list: { plugin: string; def: ProviderDef }[]): string[] {
228
- const warnings: string[] = [];
229
- for (const { plugin, def } of list) {
230
- const kind = def.kind;
231
- const taken = plugged.get(kind);
232
- if (isBuiltinKind(kind)) warnings.push(`plugin ${plugin}: provider ${kind} is ignored: it is a built-in provider`);
233
- else if (taken) warnings.push(`plugin ${plugin}: provider ${kind} is ignored: plugin ${taken.plugin} already provides it`);
234
- else plugged.set(kind, { plugin, def });
235
- }
236
- return warnings;
237
- }
238
-
239
- /** JADE_PROVIDER 选的服务由哪个插件提供;内置的或没有插件提供时 undefined */
240
- export function pluginProvider(kind: string): { plugin: string; def: ProviderDef } | undefined {
241
- return plugged.get(kind);
242
- }
243
-
244
- /** 注册了的插件服务,按注册顺序 */
245
- export function pluginProviders(): { plugin: string; def: ProviderDef }[] {
246
- return [...plugged.values()];
247
- }
248
-
249
- /** 测试用 */
250
- export function clearProviders(): void {
251
- plugged.clear();
252
- }
187
+ /** 每种内置服务的模型名、密钥、地址各放在哪个环境变量里 */
188
+ export const MODEL_KEY: Record<ProviderKind, string> = { "openai-compat": "OPENAI_COMPAT_MODEL", openai: "OPENAI_MODEL", anthropic: "ANTHROPIC_MODEL" };
189
+ export const KEY_KEY: Record<ProviderKind, string> = { "openai-compat": "OPENAI_COMPAT_API_KEY", openai: "OPENAI_API_KEY", anthropic: "ANTHROPIC_API_KEY" };
190
+ export const URL_KEY: Record<ProviderKind, string> = { "openai-compat": "OPENAI_COMPAT_BASE_URL", openai: "OPENAI_BASE_URL", anthropic: "ANTHROPIC_BASE_URL" };
253
191
 
254
192
  /** JADE_EFFORT 可取的值;openai-compat 没有统一的字段,用 OPENAI_COMPAT_EXTRA_BODY 自己设 */
255
193
  export const EFFORTS: Record<ProviderKind, readonly string[] | undefined> = {
@@ -285,7 +223,6 @@ export function idleTimeoutFromEnv(env: Env = process.env): number | undefined {
285
223
  */
286
224
  export function visionFromEnv(env: Env = process.env): boolean {
287
225
  const text = env.JADE_VISION?.trim().toLowerCase();
288
- // 插件的服务也算不能看:不知道它的模型支不支持
289
226
  if (!text) return providerKind(env) === "openai" || providerKind(env) === "anthropic";
290
227
  if (["1", "true", "on", "yes"].includes(text)) return true;
291
228
  if (["0", "false", "off", "no"].includes(text)) return false;
@@ -297,7 +234,6 @@ export function effortFromEnv(env: Env = process.env): string | undefined {
297
234
  const effort = env.JADE_EFFORT;
298
235
  if (!effort) return undefined;
299
236
  const kind = providerKind(env);
300
- // 插件的服务自己从 env 里读、自己检查
301
237
  if (!isBuiltinKind(kind)) return effort;
302
238
  const allowed = EFFORTS[kind];
303
239
  if (!allowed) throw new Error(`JADE_EFFORT is not supported for the ${kind} provider; set it in OPENAI_COMPAT_EXTRA_BODY instead`);
@@ -305,11 +241,7 @@ export function effortFromEnv(env: Env = process.env): string | undefined {
305
241
  return effort;
306
242
  }
307
243
 
308
- /** JADE_PROVIDER 指定时用它;否则按 OPENAI_API_KEY、OPENAI_COMPAT_BASE_URL、ANTHROPIC_API_KEY 的顺序 */
309
- /**
310
- * 当前配置的服务:内置的三种之一,或者 JADE_PROVIDER 写的插件服务的名字。
311
- * 写了没有插件提供的名字时也原样返回(插件可能还没加载),创建 provider 时才报错
312
- */
244
+ /** JADE_PROVIDER 指定时用它;否则按 OPENAI_API_KEY、OPENAI_COMPAT_BASE_URL、ANTHROPIC_API_KEY 的顺序。不认识的名字原样返回,创建时报错 */
313
245
  export function providerKind(env: Env = process.env): string {
314
246
  const named = env.JADE_PROVIDER;
315
247
  if (named) return named;
@@ -329,17 +261,7 @@ export function providerInfo(env: Env = process.env): ProviderInfo {
329
261
  const kind = providerKind(env);
330
262
  if (kind === "openai") return { kind, model: env.OPENAI_MODEL ?? "gpt-5", baseURL: env.OPENAI_BASE_URL ?? "https://api.openai.com/v1" };
331
263
  if (kind === "openai-compat") return { kind, model: env.OPENAI_COMPAT_MODEL ?? "", baseURL: env.OPENAI_COMPAT_BASE_URL };
332
- if (kind === "anthropic") return { kind, model: env.ANTHROPIC_MODEL ?? "claude-opus-5-5", baseURL: env.ANTHROPIC_BASE_URL };
333
- return { kind, model: pluginModel(kind, env) };
334
- }
335
-
336
- /** 插件服务的模型名;插件还没注册或出错时为空 */
337
- function pluginModel(kind: string, env: Env): string {
338
- try {
339
- return String(plugged.get(kind)?.def.model(env) ?? "");
340
- } catch {
341
- return "";
342
- }
264
+ return { kind, model: env.ANTHROPIC_MODEL ?? "claude-opus-5-5", baseURL: env.ANTHROPIC_BASE_URL };
343
265
  }
344
266
 
345
267
  /**
@@ -357,20 +279,7 @@ function pluginModel(kind: string, env: Env): string {
357
279
  */
358
280
  export async function providerFromEnv(env: Env = process.env): Promise<Provider> {
359
281
  const kind = providerKind(env);
360
- if (!isBuiltinKind(kind)) {
361
- const found = plugged.get(kind);
362
- if (!found) {
363
- const all = [...PROVIDER_KINDS, ...plugged.keys()];
364
- throw new Error(`JADE_PROVIDER must be one of ${all.join(", ")}, got ${JSON.stringify(kind)} (no plugin provides it)`);
365
- }
366
- // 限速、空闲超时这些 jade 自己的设置写错时同样在这里报错。空闲超时只对 OpenAI 和 OpenAI 兼容服务生效(见 README),
367
- // 插件的服务要用的话自己从 env 读
368
- rpmFromEnv(env);
369
- idleTimeoutFromEnv(env);
370
- const made = await found.def.create(env);
371
- if (!made || typeof made.complete !== "function") throw new Error(`plugin ${found.plugin}: provider ${kind} must return an object with a complete function`);
372
- return made;
373
- }
282
+ if (!isBuiltinKind(kind)) throw new Error(`JADE_PROVIDER must be one of ${PROVIDER_KINDS.join(", ")}, got ${JSON.stringify(kind)}`);
374
283
  const maxTokens = env.JADE_MAX_TOKENS ? parseTokens("JADE_MAX_TOKENS", env.JADE_MAX_TOKENS) : undefined;
375
284
  const effort = effortFromEnv(env);
376
285
  // 写错时在这里就报错,不等到第一次请求
@@ -407,32 +316,20 @@ export async function providerFromEnv(env: Env = process.env): Promise<Provider>
407
316
  });
408
317
  }
409
318
  const { anthropicProvider } = await import("@uncarved/provider-anthropic");
410
- return anthropicProvider({ model: env.ANTHROPIC_MODEL, effort: (effort ?? "high") as "low" | "medium" | "high" | "xhigh" | "max", maxTokens });
411
- }
412
-
413
- /** 可以在会话中途换掉的 provider:/model 改配置后,压缩、子 Agent 等持有它的地方都跟着换 */
414
- export interface SwitchableProvider extends Provider {
415
- set(next: Provider): void;
416
- }
417
-
418
- export function switchable(initial: Provider): SwitchableProvider {
419
- let current = initial;
420
- return {
421
- id: "switchable",
422
- set: (next) => {
423
- current = next;
424
- },
425
- complete: (request, options) => current.complete(request, options),
426
- };
319
+ // 地址和密钥从 env 取,不让 SDK 自己读 process.env:ctx.complete 指定的保存过的模型,env 不是 process.env
320
+ return anthropicProvider({
321
+ apiKey: env.ANTHROPIC_API_KEY,
322
+ baseURL: env.ANTHROPIC_BASE_URL ?? "https://api.anthropic.com",
323
+ model: env.ANTHROPIC_MODEL,
324
+ effort: (effort ?? "high") as "low" | "medium" | "high" | "xhigh" | "max",
325
+ maxTokens,
326
+ });
427
327
  }
428
328
 
429
329
  /** 服务提供的模型列表;不支持或出错时返回空数组 */
430
330
  export async function listModels(env: Env = process.env, fetchImpl: typeof fetch = fetch): Promise<string[]> {
431
331
  const kind = providerKind(env);
432
- if (!isBuiltinKind(kind)) {
433
- const list = plugged.get(kind)?.def.listModels;
434
- return list ? list(env).then((m) => (Array.isArray(m) ? m.filter((x) => typeof x === "string") : []), () => []) : [];
435
- }
332
+ if (!isBuiltinKind(kind)) return [];
436
333
  const info = providerInfo(env);
437
334
  const base = (info.baseURL ?? "https://api.anthropic.com").replace(/\/$/, "");
438
335
  const headers: Record<string, string> =
@@ -0,0 +1,94 @@
1
+ // 保存过的模型:$JADE_HOME/models.json。同一个服务(类型、地址、密钥都相同)只存一份,下面是在它上面用过的模型,
2
+ // 每个模型带自己的设置(输出上限、思考力度、上下文窗口)。.env 里始终是当前用的那一个,这里记着用过的全部,
3
+ // /model 可以切回去。
4
+
5
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
6
+ import path from "node:path";
7
+
8
+ export type Env = Record<string, string | undefined>;
9
+
10
+ /** 每个模型自己的设置:切换模型时一起换掉 */
11
+ export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION"];
12
+
13
+ export interface SavedModel {
14
+ name: string;
15
+ /** 这个模型自己的设置变量 */
16
+ settings: Record<string, string>;
17
+ }
18
+
19
+ export interface SavedService {
20
+ /** 服务的变量:JADE_PROVIDER、地址、密钥等 */
21
+ env: Record<string, string>;
22
+ /** 最近用过的在前 */
23
+ models: SavedModel[];
24
+ }
25
+
26
+ const strings = (v: unknown): Record<string, string> =>
27
+ typeof v === "object" && v !== null ? (Object.fromEntries(Object.entries(v).filter(([, x]) => typeof x === "string")) as Record<string, string>) : {};
28
+
29
+ /** 读不了或格式不对时返回空数组 */
30
+ export async function readSavedServices(file: string): Promise<SavedService[]> {
31
+ try {
32
+ const data = JSON.parse(await readFile(file, "utf8")) as { services?: unknown };
33
+ if (!Array.isArray(data.services)) return [];
34
+ return data.services.flatMap((raw: unknown) => {
35
+ const s = raw as { env?: unknown; models?: unknown } | null;
36
+ if (!s || !Array.isArray(s.models)) return [];
37
+ const models = s.models.flatMap((m: unknown) => {
38
+ const name = (m as { name?: unknown } | null)?.name;
39
+ return typeof name === "string" && name ? [{ name, settings: strings((m as { settings?: unknown }).settings) }] : [];
40
+ });
41
+ return models.length ? [{ env: strings(s.env), models }] : [];
42
+ });
43
+ } catch {
44
+ return [];
45
+ }
46
+ }
47
+
48
+ export async function writeSavedServices(file: string, services: SavedService[]): Promise<void> {
49
+ await mkdir(path.dirname(file), { recursive: true });
50
+ // 里面有 API 密钥
51
+ await writeFile(file, JSON.stringify({ services }, null, 2) + "\n", { mode: 0o600 });
52
+ }
53
+
54
+ /**
55
+ * 改写配置文件里的变量:已有的行原地替换,没有的追加在末尾,值为 undefined 的删除;注释和其他行保持不变。
56
+ * 同时更新 env,让改动在本次运行中生效
57
+ */
58
+ export async function updateEnvFile(file: string, updates: Env, env: Env): Promise<void> {
59
+ let text = "";
60
+ try {
61
+ text = await readFile(file, "utf8");
62
+ } catch {
63
+ // 还没有配置文件
64
+ }
65
+ const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
66
+ const out: string[] = [];
67
+ const written = new Set<string>();
68
+ for (const raw of lines) {
69
+ const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
70
+ if (!key || !(key in updates)) {
71
+ out.push(raw);
72
+ continue;
73
+ }
74
+ // 同一个键出现多次时只保留第一处
75
+ const value = updates[key];
76
+ if (value !== undefined && !written.has(key)) out.push(format(key, value));
77
+ written.add(key);
78
+ }
79
+ for (const [key, value] of Object.entries(updates)) {
80
+ if (value !== undefined && !written.has(key)) out.push(format(key, value));
81
+ }
82
+ await mkdir(path.dirname(file), { recursive: true });
83
+ await writeFile(file, out.join("\n") + "\n");
84
+ for (const [key, value] of Object.entries(updates)) {
85
+ if (value === undefined) delete env[key];
86
+ else env[key] = value;
87
+ }
88
+ }
89
+
90
+ function format(key: string, value: string): string {
91
+ if (!/[\s#"']/.test(value)) return `${key}=${value}`;
92
+ // 单引号里的内容原样读回(引号、反斜杠都不转义);只有换行要用双引号里的 \n
93
+ return value.includes("\n") ? `${key}="${value.replaceAll("\n", "\\n")}"` : `${key}='${value}'`;
94
+ }