@uncarved/jade 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (450) hide show
  1. package/bin/jade.js +5 -27
  2. package/dist/api.d.ts +266 -0
  3. package/dist/api.js +38 -0
  4. package/dist/api.js.map +1 -0
  5. package/dist/config.d.ts +13 -32
  6. package/dist/config.js +39 -121
  7. package/dist/config.js.map +1 -1
  8. package/dist/fetch.d.ts +20 -0
  9. package/dist/fetch.js +190 -0
  10. package/dist/fetch.js.map +1 -0
  11. package/dist/hash.d.ts +2 -0
  12. package/dist/hash.js +42 -0
  13. package/dist/hash.js.map +1 -0
  14. package/dist/host.d.ts +71 -0
  15. package/dist/host.js +400 -0
  16. package/dist/host.js.map +1 -0
  17. package/dist/index.d.ts +11 -30
  18. package/dist/index.js +11 -30
  19. package/dist/index.js.map +1 -1
  20. package/dist/install.d.ts +78 -0
  21. package/dist/install.js +292 -0
  22. package/dist/install.js.map +1 -0
  23. package/dist/main.d.ts +1 -2
  24. package/dist/main.js +159 -1362
  25. package/dist/main.js.map +1 -1
  26. package/dist/manifest.d.ts +23 -0
  27. package/dist/manifest.js +159 -0
  28. package/dist/manifest.js.map +1 -0
  29. package/dist/membrane.d.ts +33 -0
  30. package/dist/membrane.js +140 -0
  31. package/dist/membrane.js.map +1 -0
  32. package/dist/remote.d.ts +10 -0
  33. package/dist/remote.js +292 -0
  34. package/dist/remote.js.map +1 -0
  35. package/dist/rescue.d.ts +10 -0
  36. package/dist/rescue.js +216 -0
  37. package/dist/rescue.js.map +1 -0
  38. package/dist/resolve.d.ts +34 -0
  39. package/dist/resolve.js +178 -0
  40. package/dist/resolve.js.map +1 -0
  41. package/dist/rpc.d.ts +26 -0
  42. package/dist/rpc.js +144 -0
  43. package/dist/rpc.js.map +1 -0
  44. package/dist/secrets.d.ts +12 -0
  45. package/dist/secrets.js +125 -0
  46. package/dist/secrets.js.map +1 -0
  47. package/dist/session.d.ts +45 -0
  48. package/dist/session.js +401 -0
  49. package/dist/session.js.map +1 -0
  50. package/dist/source.d.ts +12 -0
  51. package/dist/source.js +72 -0
  52. package/dist/source.js.map +1 -0
  53. package/dist/state.d.ts +37 -0
  54. package/dist/state.js +93 -0
  55. package/dist/state.js.map +1 -0
  56. package/dist/update.d.ts +2 -0
  57. package/dist/update.js +6 -3
  58. package/dist/update.js.map +1 -1
  59. package/package.json +10 -8
  60. package/plugins/approval/index.ts +297 -0
  61. package/plugins/approval/plugin.json +18 -0
  62. package/{src/plugins → plugins/approval}/policy.ts +21 -100
  63. package/{src/plugins → plugins/approval}/review.ts +62 -16
  64. package/plugins/ask/index.ts +159 -0
  65. package/plugins/ask/plugin.json +8 -0
  66. package/plugins/budget/index.ts +38 -0
  67. package/plugins/budget/plugin.json +13 -0
  68. package/{src/plugins → plugins/checks}/checks.ts +48 -38
  69. package/plugins/checks/index.ts +158 -0
  70. package/plugins/checks/plugin.json +14 -0
  71. package/plugins/checks/project.ts +81 -0
  72. package/plugins/commands/index.ts +76 -0
  73. package/plugins/commands/plugin.json +7 -0
  74. package/{src/plugins → plugins/compact}/compact.ts +73 -38
  75. package/plugins/compact/index.ts +115 -0
  76. package/plugins/compact/plugin.json +17 -0
  77. package/plugins/compact/style.ts +17 -0
  78. package/plugins/continue/index.ts +58 -0
  79. package/plugins/continue/plugin.json +13 -0
  80. package/{src/ui → plugins/fs}/diff.ts +2 -2
  81. package/{src/tools → plugins/fs}/fs.ts +19 -42
  82. package/{src/tools → plugins/fs}/grep.ts +1 -1
  83. package/plugins/fs/index.ts +34 -0
  84. package/plugins/fs/plugin.json +7 -0
  85. package/{src/ui → plugins/fs}/renderers.ts +25 -70
  86. package/plugins/fs/style.ts +18 -0
  87. package/plugins/fs/sys.ts +81 -0
  88. package/plugins/keys/index.ts +53 -0
  89. package/plugins/keys/plugin.json +7 -0
  90. package/plugins/mcp/index.ts +192 -0
  91. package/{src → plugins/mcp}/mcp.ts +32 -32
  92. package/plugins/mcp/plugin.json +7 -0
  93. package/plugins/mcp/project.ts +93 -0
  94. package/plugins/memory/index.ts +105 -0
  95. package/{src → plugins/memory}/memory.ts +20 -2
  96. package/plugins/memory/notes.ts +43 -0
  97. package/plugins/memory/plugin.json +7 -0
  98. package/plugins/memory/style.ts +17 -0
  99. package/plugins/mentions/attach.ts +127 -0
  100. package/plugins/mentions/index.ts +79 -0
  101. package/plugins/mentions/plugin.json +7 -0
  102. package/plugins/mentions/style.ts +17 -0
  103. package/plugins/models/config.ts +53 -0
  104. package/plugins/models/configure.ts +455 -0
  105. package/plugins/models/context.ts +32 -0
  106. package/plugins/models/index.ts +198 -0
  107. package/plugins/models/plugin.json +8 -0
  108. package/{src → plugins/models}/provider.ts +16 -119
  109. package/plugins/models/saved.ts +94 -0
  110. package/plugins/modes/index.ts +222 -0
  111. package/plugins/modes/plugin.json +9 -0
  112. package/plugins/plugins/index.ts +38 -0
  113. package/plugins/plugins/plugin.json +7 -0
  114. package/plugins/print/index.ts +142 -0
  115. package/plugins/print/plugin.json +8 -0
  116. package/plugins/prompts/index.ts +83 -0
  117. package/plugins/prompts/plugin.json +8 -0
  118. package/plugins/prompts/prompts.ts +253 -0
  119. package/plugins/prompts/sys.ts +72 -0
  120. package/plugins/renderers/index.ts +78 -0
  121. package/plugins/renderers/plugin.json +7 -0
  122. package/plugins/repeats/index.ts +165 -0
  123. package/plugins/repeats/plugin.json +6 -0
  124. package/plugins/schedule/cron.ts +74 -0
  125. package/plugins/schedule/index.ts +292 -0
  126. package/plugins/schedule/plugin.json +13 -0
  127. package/plugins/schedule/store.ts +149 -0
  128. package/plugins/scratch/index.ts +31 -0
  129. package/plugins/scratch/plugin.json +7 -0
  130. package/plugins/screenshot/index.ts +40 -0
  131. package/plugins/screenshot/plugin.json +7 -0
  132. package/plugins/screenshot/sys.ts +84 -0
  133. package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
  134. package/plugins/sessions/index.ts +148 -0
  135. package/plugins/sessions/plugin.json +8 -0
  136. package/plugins/shell/index.ts +33 -0
  137. package/plugins/shell/plugin.json +17 -0
  138. package/plugins/shell/renderers.ts +45 -0
  139. package/plugins/shell/service.ts +44 -0
  140. package/{src/tools → plugins/shell}/shell.ts +31 -20
  141. package/{src → plugins/shell}/sys.ts +2 -43
  142. package/plugins/skills/index.ts +96 -0
  143. package/plugins/skills/plugin.json +7 -0
  144. package/plugins/skills/project.ts +49 -0
  145. package/{src → plugins/skills}/skills.ts +4 -11
  146. package/plugins/suggest/index.ts +286 -0
  147. package/plugins/suggest/plugin.json +18 -0
  148. package/plugins/task/index.ts +167 -0
  149. package/plugins/task/plugin.json +15 -0
  150. package/plugins/task/style.ts +17 -0
  151. package/plugins/task/subagent.ts +257 -0
  152. package/{src/tools/todo.ts → plugins/todo/index.ts} +33 -7
  153. package/plugins/todo/plugin.json +7 -0
  154. package/{src/ui → plugins/tui}/editor.ts +3 -0
  155. package/plugins/tui/frontend.ts +596 -0
  156. package/plugins/tui/index.ts +37 -0
  157. package/plugins/tui/mentions.ts +113 -0
  158. package/plugins/tui/plugin.json +10 -0
  159. package/{src/ui → plugins/tui}/render.ts +8 -2
  160. package/{src/ui → plugins/tui}/screen.ts +8 -3
  161. package/{src/ui → plugins/tui}/transcript.ts +74 -99
  162. package/{src/ui/tui.tsx → plugins/tui/tui.ts} +358 -387
  163. package/{src/tools → plugins/undo}/checkpoint.ts +21 -60
  164. package/plugins/undo/index.ts +139 -0
  165. package/plugins/undo/notes.ts +43 -0
  166. package/plugins/undo/plugin.json +7 -0
  167. package/plugins/undo/style.ts +17 -0
  168. package/plugins/update/index.ts +40 -0
  169. package/plugins/update/plugin.json +13 -0
  170. package/plugins/web/env.ts +78 -0
  171. package/plugins/web/index.ts +133 -0
  172. package/plugins/web/plugin.json +15 -0
  173. package/plugins/web/renderers.ts +11 -0
  174. package/{src/tools → plugins/web}/web.ts +58 -26
  175. package/plugins-dist/approval/index.js +271 -0
  176. package/plugins-dist/approval/plugin.json +24 -0
  177. package/{dist/plugins → plugins-dist/approval}/policy.js +15 -81
  178. package/{dist/plugins → plugins-dist/approval}/review.js +56 -10
  179. package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
  180. package/plugins-dist/ask/index.js +145 -0
  181. package/plugins-dist/ask/plugin.json +12 -0
  182. package/plugins-dist/budget/index.js +37 -0
  183. package/plugins-dist/budget/plugin.json +15 -0
  184. package/{dist/plugins → plugins-dist/checks}/checks.js +46 -35
  185. package/plugins-dist/checks/index.js +150 -0
  186. package/plugins-dist/checks/plugin.json +21 -0
  187. package/plugins-dist/checks/project.js +76 -0
  188. package/plugins-dist/commands/index.js +33 -0
  189. package/plugins-dist/commands/plugin.json +9 -0
  190. package/{dist/plugins → plugins-dist/compact}/compact.js +51 -33
  191. package/plugins-dist/compact/index.js +100 -0
  192. package/plugins-dist/compact/plugin.json +25 -0
  193. package/plugins-dist/compact/style.js +14 -0
  194. package/plugins-dist/continue/index.js +57 -0
  195. package/plugins-dist/continue/plugin.json +15 -0
  196. package/{dist/ui → plugins-dist/fs}/diff.js +2 -3
  197. package/{dist/tools → plugins-dist/fs}/fs.js +17 -42
  198. package/{dist/tools → plugins-dist/fs}/grep.js +1 -2
  199. package/plugins-dist/fs/index.js +36 -0
  200. package/plugins-dist/fs/plugin.json +10 -0
  201. package/{dist/ui → plugins-dist/fs}/renderers.js +24 -61
  202. package/plugins-dist/fs/style.js +15 -0
  203. package/plugins-dist/fs/sys.js +54 -0
  204. package/plugins-dist/keys/index.js +36 -0
  205. package/plugins-dist/keys/plugin.json +9 -0
  206. package/plugins-dist/mcp/index.js +187 -0
  207. package/{dist → plugins-dist/mcp}/mcp.js +34 -30
  208. package/plugins-dist/mcp/plugin.json +12 -0
  209. package/plugins-dist/mcp/project.js +90 -0
  210. package/plugins-dist/memory/index.js +94 -0
  211. package/{dist → plugins-dist/memory}/memory.js +21 -3
  212. package/plugins-dist/memory/notes.js +36 -0
  213. package/plugins-dist/memory/plugin.json +11 -0
  214. package/plugins-dist/memory/style.js +14 -0
  215. package/plugins-dist/mentions/attach.js +120 -0
  216. package/plugins-dist/mentions/index.js +69 -0
  217. package/plugins-dist/mentions/plugin.json +9 -0
  218. package/plugins-dist/mentions/style.js +14 -0
  219. package/plugins-dist/models/config.js +53 -0
  220. package/plugins-dist/models/configure.js +415 -0
  221. package/plugins-dist/models/context.js +27 -0
  222. package/plugins-dist/models/index.js +167 -0
  223. package/plugins-dist/models/plugin.json +14 -0
  224. package/{dist → plugins-dist/models}/provider.js +18 -81
  225. package/plugins-dist/models/saved.js +80 -0
  226. package/plugins-dist/modes/index.js +190 -0
  227. package/plugins-dist/modes/plugin.json +18 -0
  228. package/plugins-dist/plugins/index.js +37 -0
  229. package/plugins-dist/plugins/plugin.json +10 -0
  230. package/plugins-dist/print/index.js +153 -0
  231. package/plugins-dist/print/plugin.json +14 -0
  232. package/plugins-dist/prompts/index.js +57 -0
  233. package/plugins-dist/prompts/plugin.json +13 -0
  234. package/plugins-dist/prompts/prompts.js +227 -0
  235. package/plugins-dist/prompts/sys.js +46 -0
  236. package/plugins-dist/renderers/index.js +36 -0
  237. package/plugins-dist/renderers/plugin.json +9 -0
  238. package/plugins-dist/repeats/index.js +165 -0
  239. package/plugins-dist/repeats/plugin.json +6 -0
  240. package/plugins-dist/schedule/cron.js +67 -0
  241. package/plugins-dist/schedule/index.js +281 -0
  242. package/plugins-dist/schedule/plugin.json +16 -0
  243. package/plugins-dist/schedule/store.js +125 -0
  244. package/plugins-dist/scratch/index.js +26 -0
  245. package/plugins-dist/scratch/plugin.json +10 -0
  246. package/plugins-dist/screenshot/index.js +35 -0
  247. package/plugins-dist/screenshot/plugin.json +11 -0
  248. package/plugins-dist/screenshot/sys.js +76 -0
  249. package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
  250. package/plugins-dist/sessions/index.js +136 -0
  251. package/plugins-dist/sessions/plugin.json +12 -0
  252. package/plugins-dist/shell/index.js +33 -0
  253. package/plugins-dist/shell/plugin.json +19 -0
  254. package/plugins-dist/shell/renderers.js +40 -0
  255. package/plugins-dist/shell/service.js +29 -0
  256. package/{dist/tools → plugins-dist/shell}/shell.js +31 -22
  257. package/{dist → plugins-dist/shell}/sys.js +2 -43
  258. package/plugins-dist/skills/index.js +90 -0
  259. package/plugins-dist/skills/plugin.json +12 -0
  260. package/plugins-dist/skills/project.js +47 -0
  261. package/{dist → plugins-dist/skills}/skills.js +3 -7
  262. package/plugins-dist/suggest/index.js +282 -0
  263. package/plugins-dist/suggest/plugin.json +23 -0
  264. package/plugins-dist/task/index.js +151 -0
  265. package/plugins-dist/task/plugin.json +23 -0
  266. package/plugins-dist/task/style.js +14 -0
  267. package/plugins-dist/task/subagent.js +208 -0
  268. package/{dist/tools/todo.js → plugins-dist/todo/index.js} +29 -7
  269. package/plugins-dist/todo/plugin.json +9 -0
  270. package/{dist/ui → plugins-dist/tui}/editor.js +3 -1
  271. package/plugins-dist/tui/frontend.js +586 -0
  272. package/{dist → plugins-dist/tui}/history.js +0 -1
  273. package/plugins-dist/tui/index.js +21 -0
  274. package/{dist/ui → plugins-dist/tui}/markdown.js +0 -1
  275. package/plugins-dist/tui/mentions.js +115 -0
  276. package/plugins-dist/tui/plugin.json +25 -0
  277. package/{dist/ui → plugins-dist/tui}/render.js +7 -3
  278. package/{dist/ui → plugins-dist/tui}/screen.js +6 -2
  279. package/{dist/ui → plugins-dist/tui}/transcript.js +76 -98
  280. package/{dist/ui → plugins-dist/tui}/tui.js +185 -53
  281. package/{dist/tools → plugins-dist/undo}/checkpoint.js +21 -55
  282. package/plugins-dist/undo/index.js +140 -0
  283. package/plugins-dist/undo/notes.js +36 -0
  284. package/plugins-dist/undo/plugin.json +9 -0
  285. package/plugins-dist/undo/style.js +14 -0
  286. package/plugins-dist/update/index.js +34 -0
  287. package/plugins-dist/update/plugin.json +15 -0
  288. package/plugins-dist/web/env.js +80 -0
  289. package/plugins-dist/web/index.js +124 -0
  290. package/plugins-dist/web/plugin.json +21 -0
  291. package/plugins-dist/web/renderers.js +7 -0
  292. package/{dist/tools → plugins-dist/web}/web.js +50 -24
  293. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  294. package/sdk/python/jade_plugin.py +380 -0
  295. package/src/api.ts +298 -0
  296. package/src/config.ts +32 -134
  297. package/src/fetch.ts +216 -0
  298. package/src/hash.ts +39 -0
  299. package/src/host.ts +429 -0
  300. package/src/index.ts +23 -30
  301. package/src/install.ts +355 -0
  302. package/src/main.ts +155 -1387
  303. package/src/manifest.ts +160 -0
  304. package/src/membrane.ts +142 -0
  305. package/src/remote.ts +288 -0
  306. package/src/rescue.ts +217 -0
  307. package/src/resolve.ts +199 -0
  308. package/src/rpc.ts +144 -0
  309. package/src/secrets.ts +122 -0
  310. package/src/session.ts +458 -0
  311. package/src/source.ts +83 -0
  312. package/src/state.ts +120 -0
  313. package/src/update.ts +265 -262
  314. package/LICENSE +0 -21
  315. package/README.md +0 -204
  316. package/dist/commands.d.ts +0 -50
  317. package/dist/commands.js +0 -126
  318. package/dist/commands.js.map +0 -1
  319. package/dist/context.d.ts +0 -17
  320. package/dist/context.js +0 -53
  321. package/dist/context.js.map +0 -1
  322. package/dist/distro.d.ts +0 -141
  323. package/dist/distro.js +0 -462
  324. package/dist/distro.js.map +0 -1
  325. package/dist/history.d.ts +0 -10
  326. package/dist/history.js.map +0 -1
  327. package/dist/mcp.d.ts +0 -114
  328. package/dist/mcp.js.map +0 -1
  329. package/dist/memory.d.ts +0 -19
  330. package/dist/memory.js.map +0 -1
  331. package/dist/mentions.d.ts +0 -35
  332. package/dist/mentions.js +0 -130
  333. package/dist/mentions.js.map +0 -1
  334. package/dist/plugin-cli.d.ts +0 -16
  335. package/dist/plugin-cli.js +0 -294
  336. package/dist/plugin-cli.js.map +0 -1
  337. package/dist/plugins/budget.d.ts +0 -9
  338. package/dist/plugins/budget.js +0 -30
  339. package/dist/plugins/budget.js.map +0 -1
  340. package/dist/plugins/builtins.d.ts +0 -10
  341. package/dist/plugins/builtins.js +0 -25
  342. package/dist/plugins/builtins.js.map +0 -1
  343. package/dist/plugins/checks.d.ts +0 -71
  344. package/dist/plugins/checks.js.map +0 -1
  345. package/dist/plugins/compact.d.ts +0 -26
  346. package/dist/plugins/compact.js.map +0 -1
  347. package/dist/plugins/core.d.ts +0 -47
  348. package/dist/plugins/core.js +0 -229
  349. package/dist/plugins/core.js.map +0 -1
  350. package/dist/plugins/host.d.ts +0 -52
  351. package/dist/plugins/host.js +0 -115
  352. package/dist/plugins/host.js.map +0 -1
  353. package/dist/plugins/install.d.ts +0 -68
  354. package/dist/plugins/install.js +0 -377
  355. package/dist/plugins/install.js.map +0 -1
  356. package/dist/plugins/loader.d.ts +0 -410
  357. package/dist/plugins/loader.js +0 -407
  358. package/dist/plugins/loader.js.map +0 -1
  359. package/dist/plugins/policy.d.ts +0 -98
  360. package/dist/plugins/policy.js.map +0 -1
  361. package/dist/plugins/prompts.d.ts +0 -43
  362. package/dist/plugins/prompts.js +0 -284
  363. package/dist/plugins/prompts.js.map +0 -1
  364. package/dist/plugins/repeats.d.ts +0 -16
  365. package/dist/plugins/repeats.js +0 -151
  366. package/dist/plugins/repeats.js.map +0 -1
  367. package/dist/plugins/review.d.ts +0 -18
  368. package/dist/plugins/review.js.map +0 -1
  369. package/dist/plugins/schema.d.ts +0 -2
  370. package/dist/plugins/schema.js.map +0 -1
  371. package/dist/plugins/sessions.d.ts +0 -7
  372. package/dist/plugins/sessions.js +0 -110
  373. package/dist/plugins/sessions.js.map +0 -1
  374. package/dist/provider.d.ts +0 -165
  375. package/dist/provider.js.map +0 -1
  376. package/dist/session-lifecycle.d.ts +0 -61
  377. package/dist/session-lifecycle.js +0 -167
  378. package/dist/session-lifecycle.js.map +0 -1
  379. package/dist/session-scope.d.ts +0 -53
  380. package/dist/session-scope.js +0 -116
  381. package/dist/session-scope.js.map +0 -1
  382. package/dist/sessions.d.ts +0 -28
  383. package/dist/sessions.js +0 -73
  384. package/dist/sessions.js.map +0 -1
  385. package/dist/skills.d.ts +0 -37
  386. package/dist/skills.js.map +0 -1
  387. package/dist/sys.d.ts +0 -51
  388. package/dist/sys.js.map +0 -1
  389. package/dist/tools/ask.d.ts +0 -11
  390. package/dist/tools/ask.js +0 -62
  391. package/dist/tools/ask.js.map +0 -1
  392. package/dist/tools/checkpoint.d.ts +0 -43
  393. package/dist/tools/checkpoint.js.map +0 -1
  394. package/dist/tools/fs.d.ts +0 -39
  395. package/dist/tools/fs.js.map +0 -1
  396. package/dist/tools/grep.d.ts +0 -6
  397. package/dist/tools/grep.js.map +0 -1
  398. package/dist/tools/screenshot.d.ts +0 -15
  399. package/dist/tools/screenshot.js.map +0 -1
  400. package/dist/tools/shell.d.ts +0 -55
  401. package/dist/tools/shell.js.map +0 -1
  402. package/dist/tools/task.d.ts +0 -39
  403. package/dist/tools/task.js +0 -117
  404. package/dist/tools/task.js.map +0 -1
  405. package/dist/tools/todo.d.ts +0 -10
  406. package/dist/tools/todo.js.map +0 -1
  407. package/dist/tools/web.d.ts +0 -53
  408. package/dist/tools/web.js.map +0 -1
  409. package/dist/ui/configure.d.ts +0 -20
  410. package/dist/ui/configure.js +0 -447
  411. package/dist/ui/configure.js.map +0 -1
  412. package/dist/ui/diff.d.ts +0 -20
  413. package/dist/ui/diff.js.map +0 -1
  414. package/dist/ui/editor.d.ts +0 -41
  415. package/dist/ui/editor.js.map +0 -1
  416. package/dist/ui/markdown.d.ts +0 -22
  417. package/dist/ui/markdown.js.map +0 -1
  418. package/dist/ui/render.d.ts +0 -14
  419. package/dist/ui/render.js.map +0 -1
  420. package/dist/ui/renderers.d.ts +0 -8
  421. package/dist/ui/renderers.js.map +0 -1
  422. package/dist/ui/screen.d.ts +0 -118
  423. package/dist/ui/screen.js.map +0 -1
  424. package/dist/ui/transcript.d.ts +0 -46
  425. package/dist/ui/transcript.js.map +0 -1
  426. package/dist/ui/tui.d.ts +0 -47
  427. package/dist/ui/tui.js.map +0 -1
  428. package/src/commands.ts +0 -144
  429. package/src/context.ts +0 -64
  430. package/src/distro.ts +0 -577
  431. package/src/mentions.ts +0 -135
  432. package/src/plugin-cli.ts +0 -278
  433. package/src/plugins/budget.ts +0 -37
  434. package/src/plugins/builtins.ts +0 -34
  435. package/src/plugins/core.ts +0 -257
  436. package/src/plugins/host.ts +0 -146
  437. package/src/plugins/install.ts +0 -429
  438. package/src/plugins/loader.ts +0 -742
  439. package/src/plugins/prompts.ts +0 -327
  440. package/src/plugins/repeats.ts +0 -157
  441. package/src/plugins/sessions.ts +0 -109
  442. package/src/session-lifecycle.ts +0 -194
  443. package/src/session-scope.ts +0 -135
  444. package/src/sessions.ts +0 -78
  445. package/src/tools/ask.ts +0 -72
  446. package/src/tools/task.ts +0 -149
  447. package/src/ui/configure.ts +0 -471
  448. /package/{src/plugins → plugins/approval}/schema.ts +0 -0
  449. /package/{src → plugins/tui}/history.ts +0 -0
  450. /package/{src/ui → plugins/tui}/markdown.ts +0 -0
@@ -1,194 +0,0 @@
1
- // 会话的生命周期:开始、离开、换会话(/clear、/resume)、重新加载、退出。每个会话一套插件实例(SessionScope)和一个运行时。
2
- //
3
- // 换会话是事务(docs/design/jade-plugin-lifecycle.md 第 5 节):
4
- // 1. prepare:新的一套在 preparing 里创建,工厂函数这时运行;
5
- // 2. 打开新的运行时;失败就回滚:只收掉新的一套,当前的一套一直是 active,不用恢复任何东西;
6
- // 3. commit:关闭旧的一套(ctx.signal 中止),会话收尾(detach),换成新的,激活新的;旧的一套在后台 dispose。
7
- // 退出前 settled() 等所有后台 dispose 完成。
8
- //
9
- // 这里不碰界面:清屏、状态栏的会话号、提示由调用方(main.ts)在每一步之后做。
10
-
11
- import type { Plugin, Runtime } from "uncarved";
12
- import type { Jade } from "./distro.ts";
13
- import type { SessionScope } from "./session-scope.ts";
14
-
15
- export interface LifecycleDeps<Mode> {
16
- /** 创建一套新的插件实例,挂在一个 preparing 的作用域上 */
17
- prepare(mode: Mode | undefined): Promise<SessionScope>;
18
- /** 打开运行时:有 id 时恢复那个会话,没有时开始新会话 */
19
- open(plugins: Plugin[], id?: string): Promise<Runtime>;
20
- /** 离开会话之后调用:会话没用过就删掉它的日志;返回是否删掉了 */
21
- discard(sessionId: string): Promise<boolean>;
22
- /** 要告诉用户的问题(插件 dispose 出错、超时等) */
23
- warn(list: string[]): void;
24
- /** 会话收尾失败:仍然继续离开 */
25
- error(err: unknown): void;
26
- }
27
-
28
- /** 当前会话:一起换 */
29
- export interface Current {
30
- scope: SessionScope;
31
- jade: Jade;
32
- rt: Runtime;
33
- sessionId: string;
34
- }
35
-
36
- export class SessionLifecycle<Mode = string> {
37
- readonly #deps: LifecycleDeps<Mode>;
38
- #scope: SessionScope;
39
- #rt: Runtime | undefined;
40
- #sessionId = "";
41
- /**
42
- * 当前会话已经离开(当前的一套已关闭、会话已收尾)。正常时离开后马上换成新的;
43
- * 只有 reload 收尾之后两次打开都失败时会停在这里,之后 leave 不再收尾一次,reload、switchTo 可以再试
44
- */
45
- #left = false;
46
- /** 后台进行中的 dispose:换会话时不等它(新会话已经能用了),退出前等完(各自限时,见 DISPOSE_TIMEOUT_MS) */
47
- readonly #disposals = new Set<Promise<void>>();
48
-
49
- private constructor(deps: LifecycleDeps<Mode>, scope: SessionScope) {
50
- this.#deps = deps;
51
- this.#scope = scope;
52
- }
53
-
54
- /**
55
- * 启动的第一步:创建第一套插件实例(还没激活)。之后宿主可以先读 jade(命令、警告、服务),再调用 begin 打开会话
56
- */
57
- static async prepare<Mode>(deps: LifecycleDeps<Mode>, mode?: Mode): Promise<SessionLifecycle<Mode>> {
58
- return new SessionLifecycle(deps, await deps.prepare(mode));
59
- }
60
-
61
- get scope(): SessionScope {
62
- return this.#scope;
63
- }
64
-
65
- get jade(): Jade {
66
- return this.#scope.jade;
67
- }
68
-
69
- /** 当前会话;begin 之前不能用 */
70
- get current(): Current {
71
- if (!this.#rt) throw new Error("the session has not begun");
72
- return { scope: this.#scope, jade: this.#scope.jade, rt: this.#rt, sessionId: this.#sessionId };
73
- }
74
-
75
- /** 启动的第二步:打开第一个会话并激活插件。失败时收掉这套实例再抛出 */
76
- async begin(id?: string): Promise<Current> {
77
- try {
78
- this.#commit(this.#scope, await this.#deps.open(this.jade.plugins, id));
79
- } catch (err) {
80
- this.jade.shutdown();
81
- this.#deps.warn(await this.#scope.dispose());
82
- throw err;
83
- }
84
- return this.current;
85
- }
86
-
87
- /**
88
- * 离开当前会话但保留它(退出、换会话时):先关闭作用域(进行中的快捷键、插件的 ctx.signal 中止),再会话收尾。
89
- * 会话收尾(session.detached 的 hook)用的还是这套实例,之后才在后台 dispose。返回会话是否保留了下来
90
- */
91
- async leave(reason = "the session ended"): Promise<boolean> {
92
- if (this.#left) return true;
93
- const { sessionId } = this.current;
94
- if (!(await this.#detach(reason))) return true;
95
- try {
96
- return !(await this.#deps.discard(sessionId));
97
- } catch (err) {
98
- this.#deps.error(err);
99
- return true;
100
- }
101
- }
102
-
103
- /** 当前会话是否已经离开(reload 收尾后没能打开会话时);这时要 switchTo 或 reload 才能继续 */
104
- get left(): boolean {
105
- return this.#left;
106
- }
107
-
108
- /**
109
- * 换成另一个会话:id 为空时开始新会话(/clear),否则恢复它(/resume)。
110
- * 打开失败时回滚并抛出,当前会话不受影响;成功时返回旧会话的 id 和它是否保留了下来
111
- */
112
- async switchTo(id: string | undefined, mode: Mode | undefined): Promise<{ previous: string; kept: boolean }> {
113
- // 同一个会话不能同时由两个运行时打开:先打开再收尾旧的,会把 session.detached 写进新运行时正在用的日志
114
- // (没用过的会话还会被 discard 删掉)。内置的 /resume 已经挡住,这里挡住插件直接调用 session.resume
115
- if (id !== undefined && id === this.#sessionId && !this.#left) throw new Error(`${id} is the current session`);
116
- const next = await this.#deps.prepare(mode);
117
- let rt: Runtime;
118
- try {
119
- rt = await this.#deps.open(next.jade.plugins, id);
120
- } catch (err) {
121
- // 回滚:新的一套从没激活过,收掉它就行;当前的一套一直是 active
122
- next.jade.shutdown();
123
- this.#deps.warn(await next.dispose());
124
- throw err;
125
- }
126
- const previous = this.#sessionId;
127
- const kept = await this.leave();
128
- this.#commit(next, rt);
129
- return { previous, kept };
130
- }
131
-
132
- /**
133
- * 用新的插件实例继续当前会话:工具和系统提示词按现在的配置重新生成,对话内容不变。
134
- * 同一个会话要先 detach 才能用新的实例恢复,所以先关掉当前的一套;恢复失败时改为开始新会话,返回那个错误。
135
- * 新会话也开不了时抛出:这时当前会话已经离开(left),可以再 reload、switchTo;leave 不会再收尾一次
136
- */
137
- async reload(mode: Mode | undefined): Promise<{ error?: unknown }> {
138
- const next = await this.#deps.prepare(mode);
139
- const { sessionId } = this.current;
140
- if (!this.#left) await this.#detach("the session was reloaded");
141
- let error: unknown;
142
- let opened: Runtime;
143
- try {
144
- opened = await this.#deps.open(next.jade.plugins, sessionId);
145
- } catch (err) {
146
- error = err;
147
- try {
148
- opened = await this.#deps.open(next.jade.plugins);
149
- } catch (again) {
150
- next.jade.shutdown();
151
- this.#deps.warn(await next.dispose());
152
- throw again;
153
- }
154
- }
155
- this.#commit(next, opened);
156
- return error === undefined ? {} : { error };
157
- }
158
-
159
- /** 退出前:等后台的 dispose 都完成 */
160
- async settled(): Promise<void> {
161
- await Promise.all(this.#disposals);
162
- }
163
-
164
- #commit(scope: SessionScope, rt: Runtime) {
165
- this.#scope = scope;
166
- this.#rt = rt;
167
- this.#sessionId = rt.state.sessionId;
168
- this.#left = false;
169
- scope.activate();
170
- }
171
-
172
- /** 离开当前会话:关闭当前的一套,会话收尾,在后台 dispose。返回收尾是否成功(失败时已经报告) */
173
- async #detach(reason: string): Promise<boolean> {
174
- const { scope, rt } = this.current;
175
- this.#left = true;
176
- scope.close(reason);
177
- scope.jade.shutdown();
178
- try {
179
- await rt.detach();
180
- return true;
181
- } catch (err) {
182
- this.#deps.error(err);
183
- return false;
184
- } finally {
185
- this.#disposeInBackground(scope);
186
- }
187
- }
188
-
189
- #disposeInBackground(scope: SessionScope) {
190
- const done = scope.dispose().then((w) => this.#deps.warn(w));
191
- this.#disposals.add(done);
192
- void done.finally(() => this.#disposals.delete(done));
193
- }
194
- }
@@ -1,135 +0,0 @@
1
- // 会话级的插件作用域(SessionScope):一套插件实例,加上绑定到这套实例的 ctx.status、ctx.input、ctx.send、ctx.signal。
2
- //
3
- // preparing ──activate──▶ active ──close──▶ closing ──dispose──▶ closed
4
- // └──────────────────close──────────────────┘
5
- //
6
- // 换会话是一个事务:先在 preparing 里创建新的一套(工厂函数这时运行),成功了才关闭旧的、激活新的;
7
- // 失败了只关闭新的,旧的一直是 active,不用恢复任何东西。见 docs/design/jade-plugin-lifecycle.md 第 3.2、5 节。
8
-
9
- import type { Jade } from "./distro.ts";
10
-
11
- export type ScopeState = "preparing" | "active" | "closing" | "closed";
12
-
13
- /** 宿主的界面:作用域经它显示状态栏、改输入框、替用户发送、提示问题 */
14
- export interface ScopeHost {
15
- /** 换掉状态栏上插件的段落(按插件的 id) */
16
- showStatus(segments: { plugin: string; text: string }[]): void;
17
- /** 终端界面的输入框;-p 时没有 */
18
- input?: { text(): string; insert(text: string): void; set(text: string): void };
19
- /** 替用户提交一行;-p 时没有 */
20
- send?(plugin: string, text: string): void;
21
- warn(text: string): void;
22
- /** 状态栏一段的整理(去掉控制字符、截断);返回空字符串表示去掉 */
23
- tidy?(text: string): string;
24
- }
25
-
26
- export class SessionScope {
27
- #state: ScopeState = "preparing";
28
- readonly #host: ScopeHost;
29
- readonly #abort = new AbortController();
30
- /** 插件设置的状态栏段落;preparing 时先存着,activate 时显示 */
31
- readonly #status = new Map<string, string>();
32
- /** 已经提示过过期调用的插件,每个只提示一次 */
33
- readonly #warned = new Set<string>();
34
- #jade: Jade | undefined;
35
- #disposing: Promise<string[]> | undefined;
36
-
37
- constructor(host: ScopeHost) {
38
- this.#host = host;
39
- }
40
-
41
- get state(): ScopeState {
42
- return this.#state;
43
- }
44
-
45
- /** 关闭时中止:交给插件的 ctx.signal、快捷键的 signal 都是它 */
46
- get signal(): AbortSignal {
47
- return this.#abort.signal;
48
- }
49
-
50
- /** 这个作用域的插件实例;attach 之前不能用 */
51
- get jade(): Jade {
52
- if (!this.#jade) throw new Error("the session scope has no plugins yet");
53
- return this.#jade;
54
- }
55
-
56
- /** 交给 createJade 的回调:插件的 ctx 都经过这里,按作用域的状态决定是否生效 */
57
- get hooks() {
58
- const host = this.#host;
59
- return {
60
- signal: this.signal,
61
- onStatus: (plugin: string, text: string | undefined) => this.#setStatus(plugin, text),
62
- input: host.input && {
63
- text: (plugin?: string) => (this.#live(plugin, "ctx.input") ? host.input!.text() : ""),
64
- insert: (text: string, plugin?: string) => void (this.#live(plugin, "ctx.input") && host.input!.insert(String(text))),
65
- set: (text: string, plugin?: string) => void (this.#live(plugin, "ctx.input") && host.input!.set(String(text))),
66
- },
67
- send: host.send && ((plugin: string, text: string) => void (this.#live(plugin, "ctx.send") && host.send!(plugin, text))),
68
- };
69
- }
70
-
71
- /** 创建好插件实例后挂上 */
72
- attach(jade: Jade): void {
73
- this.#jade = jade;
74
- }
75
-
76
- /** preparing → active:显示插件在准备期间设置的状态 */
77
- activate(): void {
78
- if (this.#state !== "preparing") throw new Error(`cannot activate the session scope: it is ${this.#state}`);
79
- this.#state = "active";
80
- this.#show();
81
- }
82
-
83
- /**
84
- * → closing:中止 signal(进行中的快捷键等随之中止),之后 ctx.status、ctx.input、ctx.send 不再生效;
85
- * dispose 完成(closed)之后还调用的才提示,那是插件忘了在 dispose 里停掉的定时器、监听。
86
- * 不清状态栏:接着激活的作用域会换掉它;回滚时没激活过,也就没显示过
87
- */
88
- close(reason: string): void {
89
- if (this.#state === "closing" || this.#state === "closed") return;
90
- this.#state = "closing";
91
- this.#abort.abort(new Error(reason));
92
- }
93
-
94
- /** 调用插件的 dispose(见 Jade.dispose),→ closed。先 close;只执行一次,返回要告诉用户的问题 */
95
- dispose(): Promise<string[]> {
96
- this.close("the session ended");
97
- return (this.#disposing ??= (this.#jade ? this.#jade.dispose() : Promise.resolve([])).then((warnings) => {
98
- this.#state = "closed";
99
- return warnings;
100
- }));
101
- }
102
-
103
- #setStatus(plugin: string, text: string | undefined) {
104
- const line = text === undefined ? "" : (this.#host.tidy ?? ((t: string) => t.trim()))(String(text));
105
- if (this.#state === "closing" || this.#state === "closed") {
106
- // closing 时插件还没轮到 dispose(会话收尾还在进行),定时器再触发一次是正常的;dispose 里去掉自己的段落也正常
107
- if (line && this.#state === "closed") this.#stale(plugin, "ctx.status", "after its session ended");
108
- return;
109
- }
110
- if (line) this.#status.set(plugin, line);
111
- else this.#status.delete(plugin);
112
- if (this.#state === "active") this.#show();
113
- }
114
-
115
- #show() {
116
- this.#host.showStatus([...this.#status].map(([plugin, text]) => ({ plugin, text })));
117
- }
118
-
119
- /** 是否 active。preparing 和 closed 时提示一次;closing 时插件还没轮到 dispose,不提示 */
120
- #live(plugin: string | undefined, what: string): boolean {
121
- if (this.#state === "active") return true;
122
- if (this.#state === "preparing") this.#stale(plugin, what, "before its session started");
123
- else if (this.#state === "closed") this.#stale(plugin, what, "after its session ended");
124
- return false;
125
- }
126
-
127
- #stale(plugin: string | undefined, what: string, when: string) {
128
- const key = plugin ?? "";
129
- if (this.#warned.has(key)) return;
130
- this.#warned.add(key);
131
- const who = plugin ? `plugin ${plugin}` : "a plugin";
132
- const fix = when.startsWith("after") ? "; stop timers and listeners in dispose, or when ctx.signal aborts" : "";
133
- this.#host.warn(`${who} used ${what} ${when}, so it was ignored${fix}`);
134
- }
135
- }
package/src/sessions.ts DELETED
@@ -1,78 +0,0 @@
1
- // 会话索引:LogStore 接口只有 append/read,没有"列出会话",由发行版自己记一份。
2
- // 只记顶层会话;子 Agent 的会话通过日志里的 child 引用找到。
3
-
4
- import { appendFile, mkdir, readFile, rm } from "node:fs/promises";
5
- import path from "node:path";
6
- import { readAll, type LogStore } from "uncarved";
7
-
8
- export interface SessionEntry {
9
- id: string;
10
- cwd: string;
11
- title: string;
12
- at: string;
13
- }
14
-
15
- export class SessionIndex {
16
- readonly file: string;
17
-
18
- constructor(file: string) {
19
- this.file = file;
20
- }
21
-
22
- async add(entry: SessionEntry): Promise<void> {
23
- await mkdir(path.dirname(this.file), { recursive: true });
24
- await appendFile(this.file, JSON.stringify(entry) + "\n");
25
- }
26
-
27
- /** 最近使用的在前;同一个会话多次记录时取最后一次的时间、第一次的标题(之后的消息常常只是"继续") */
28
- async list(cwd?: string): Promise<SessionEntry[]> {
29
- let text: string;
30
- try {
31
- text = await readFile(this.file, "utf8");
32
- } catch {
33
- return [];
34
- }
35
- const latest = new Map<string, SessionEntry>();
36
- for (const line of text.split("\n")) {
37
- if (!line.trim()) continue;
38
- try {
39
- const e = JSON.parse(line) as SessionEntry;
40
- const first = latest.get(e.id);
41
- latest.delete(e.id);
42
- latest.set(e.id, first ? { ...e, title: first.title } : e);
43
- } catch {
44
- // 末尾未写完的一行
45
- }
46
- }
47
- return [...latest.values()].filter((e) => !cwd || path.resolve(e.cwd) === path.resolve(cwd)).reverse();
48
- }
49
- }
50
-
51
- /**
52
- * 会话从来没收到过输入(打开 jade 什么都没说就退出、/clear 后马上又 /clear)时删掉它的日志,
53
- * 否则每次启动都留下一个只有 session.started 和 session.detached 的文件。只认 JsonlLogStore 的布局:dir/<id>.jsonl。
54
- * 返回是否删掉了
55
- */
56
- export async function discardIfEmpty(store: LogStore & { dir: string }, sessionId: string): Promise<boolean> {
57
- const events = await readAll(store, sessionId);
58
- if (events.some((e) => e.type === "input.received")) return false;
59
- await rm(path.join(store.dir, `${sessionId}.jsonl`), { force: true });
60
- return true;
61
- }
62
-
63
- /** id 等于 arg 或以它开头的唯一一个会话;都不是时原样返回,交给 Runtime.resume 去找 */
64
- export function matchSession(entries: { id: string }[], arg: string): string {
65
- const hits = entries.filter((s) => s.id.startsWith(arg));
66
- return hits.find((s) => s.id === arg)?.id ?? (hits.length === 1 ? hits[0]!.id : arg);
67
- }
68
-
69
- /** 显示用的相对时间 */
70
- export function ago(iso: string, now = Date.now()): string {
71
- const s = (now - Date.parse(iso)) / 1000;
72
- if (!Number.isFinite(s)) return iso;
73
- if (s < 60) return "just now";
74
- if (s < 3600) return `${Math.floor(s / 60)} min ago`;
75
- if (s < 86400) return `${Math.floor(s / 3600)} h ago`;
76
- if (s < 86400 * 7) return `${Math.floor(s / 86400)} d ago`;
77
- return iso.slice(0, 10);
78
- }
package/src/tools/ask.ts DELETED
@@ -1,72 +0,0 @@
1
- // ask_user 工具:模型拿不准、而答案会改变它要做的事时,先问用户再动手。
2
- //
3
- // 每个问题给几个选项让用户选,也可以自己输入回答。怎么显示由宿主决定(ask);
4
- // 不能交互(-p、非终端)时直接返回"用户不在",让模型按合理的假设继续并说明假设。
5
-
6
- import type { Tool } from "uncarved";
7
-
8
- export interface Question {
9
- question: string;
10
- options: { label: string; description?: string }[];
11
- }
12
-
13
- /** 宿主问用户:每个问题一个回答;用户取消时返回 undefined */
14
- export type AskUser = (questions: Question[], signal: AbortSignal) => Promise<string[] | undefined>;
15
-
16
- const MAX_QUESTIONS = 4;
17
-
18
- export function askUserTool(ask: AskUser): Tool {
19
- return {
20
- name: "ask_user",
21
- description:
22
- "Ask the user one to four questions and wait for the answers. Use it before starting work when the request is ambiguous in a way that changes what you would do " +
23
- "(scope, approach, a trade-off or preference only the user can decide). Do not ask about things you can find out from the code, docs or tools, " +
24
- "or that have a sensible default (pick it and say so). Each question offers 2-4 options; the user can also type their own answer. " +
25
- "Put your recommended option first.",
26
- inputSchema: {
27
- type: "object",
28
- properties: {
29
- questions: {
30
- type: "array",
31
- items: {
32
- type: "object",
33
- properties: {
34
- question: { type: "string", description: "The complete question, ending with a question mark" },
35
- options: {
36
- type: "array",
37
- items: {
38
- type: "object",
39
- properties: {
40
- label: { type: "string", description: "1-5 words" },
41
- description: { type: "string", description: "What choosing it means, trade-offs" },
42
- },
43
- required: ["label"],
44
- additionalProperties: false,
45
- },
46
- },
47
- },
48
- required: ["question", "options"],
49
- additionalProperties: false,
50
- },
51
- },
52
- },
53
- required: ["questions"],
54
- additionalProperties: false,
55
- },
56
- // 不碰文件和进程:所有模式(包括 plan)都不用批准
57
- effect: "read",
58
- concurrency: "exclusive",
59
- idempotent: false,
60
- async execute(input, ctx) {
61
- const { questions } = input as { questions: Question[] };
62
- if (questions.length === 0 || questions.length > MAX_QUESTIONS) return { content: `questions must have 1-${MAX_QUESTIONS} items`, isError: true };
63
- const bad = questions.find((q) => q.options.length < 2 || q.options.length > 4);
64
- if (bad) return { content: `each question needs 2-4 options ("${bad.question}" has ${bad.options.length})`, isError: true };
65
- const answers = await ask(questions, ctx.signal);
66
- if (answers === undefined) {
67
- return { content: "The user dismissed the questions without answering. Do not ask again: proceed with your best judgment and state your assumptions, or end your turn." };
68
- }
69
- return { content: questions.map((q, i) => `Q: ${q.question}\nA: ${answers[i] ?? "(no answer)"}`).join("\n\n") };
70
- },
71
- };
72
- }
package/src/tools/task.ts DELETED
@@ -1,149 +0,0 @@
1
- // task 工具:启动一个只读的子 Agent。子 Agent 是一个普通会话,用日志引用和父会话关联(3.4、第六章)。
2
- // 子 Agent 调用的工具通过 ctx.progress 推给宿主显示;需要用户批准的调用(例如 web_fetch)交给宿主的 approve。
3
-
4
- import { Runtime, type EventDataMap, type LogStore, type Message, type Plugin, type Provider, type Tool } from "uncarved";
5
-
6
- /** ctx.progress 推送的内容 */
7
- export type TaskProgress =
8
- | { kind: "task-start"; sessionId: string; description: string }
9
- /** 子 Agent 在等模型回复 */
10
- | { kind: "task-model" }
11
- | { kind: "task-tool"; name: string; input: unknown; decision: string };
12
-
13
- /** 子 Agent 的调用要用户批准时,宿主给出的回答 */
14
- export interface SubagentApproval {
15
- approved: boolean;
16
- reason?: string;
17
- }
18
-
19
- export type ApproveSubagent = (call: { toolUseId: string; name: string; input: unknown }, signal: AbortSignal) => Promise<SubagentApproval>;
20
-
21
- /** 一个子会话的控制:wrapUp 为 true 时步数上限收紧到一步,只让它写报告 */
22
- export interface SubagentControl {
23
- wrapUp: boolean;
24
- }
25
-
26
- export interface TaskOptions {
27
- store: LogStore;
28
- provider: Provider;
29
- /** 每个子会话一套新的插件实例;步数插件读取 control */
30
- plugins: (control: SubagentControl) => Plugin[];
31
- /** 不给时一律拒绝 */
32
- approve?: ApproveSubagent;
33
- }
34
-
35
- const NOBODY: SubagentApproval = { approved: false, reason: "Nobody can approve this action for a sub-agent. Report what you need instead." };
36
-
37
- /** 用完步数还没写报告时追加的消息 */
38
- const WRAP_UP =
39
- "You have reached your step limit. Do not call any more tools: write your report now from what you have found so far, " +
40
- "and say what you could not check.";
41
-
42
- /** 返回给主会话的报告上限,免得一个子 Agent 撑满主会话的上下文 */
43
- const MAX_REPORT_CHARS = 20_000;
44
-
45
- export function taskTool(opts: TaskOptions): Tool {
46
- return {
47
- name: "task",
48
- description:
49
- "Delegate a self-contained research task to a sub-agent with read-only tools (read_file, list_dir, glob, grep, and web_fetch / web_search when available). " +
50
- "Use it for broad searches across many files, or web research across several pages, so that only the conclusion comes back. " +
51
- "The sub-agent sees nothing of this conversation: the prompt must contain everything it needs. " +
52
- "Several task calls in one response run in parallel.",
53
- inputSchema: {
54
- type: "object",
55
- properties: {
56
- description: { type: "string", description: "3-6 word label" },
57
- prompt: { type: "string", description: "Complete instructions, including what to report back" },
58
- },
59
- required: ["description", "prompt"],
60
- additionalProperties: false,
61
- },
62
- effect: "read",
63
- concurrency: "safe",
64
- // 子 Agent 只读,重跑只是多花 token
65
- idempotent: true,
66
- async execute(input, ctx) {
67
- const { description, prompt } = input as { description: string; prompt: string };
68
- const control: SubagentControl = { wrapUp: false };
69
- const child = await Runtime.create({
70
- store: opts.store,
71
- provider: opts.provider,
72
- plugins: opts.plugins(control),
73
- parent: ctx.parent,
74
- });
75
- const sessionId = child.state.sessionId;
76
- const progress = (p: TaskProgress) => ctx.progress(p);
77
- progress({ kind: "task-start", sessionId, description });
78
- const names = new Map<string, string>();
79
- child.subscribe((n) => {
80
- if (n.type !== "event") return;
81
- if (n.event.type === "model.requested") {
82
- progress({ kind: "task-model" });
83
- } else if (n.event.type === "model.responded") {
84
- for (const b of (n.event.data as EventDataMap["model.responded"]).content) if (b.kind === "tool_use") names.set(b.id, b.name);
85
- } else if (n.event.type === "tool.decided") {
86
- const d = n.event.data as EventDataMap["tool.decided"];
87
- progress({ kind: "task-tool", name: names.get(d.toolUseId) ?? "?", input: d.input, decision: d.decision });
88
- }
89
- });
90
- const forward = () => child.abort(ctx.signal.reason);
91
- ctx.signal.addEventListener("abort", forward, { once: true });
92
- let failure: unknown;
93
- const turn = async (text: string) => {
94
- await child.send({ kind: "message", content: [{ kind: "text", text, origin: { by: "runtime" }, trust: "trusted" }] });
95
- while (child.state.phase === "awaiting") {
96
- for (const c of child.state.batch.filter((x) => x.decision === "ask")) {
97
- const answer = opts.approve ? await opts.approve(c, ctx.signal) : NOBODY;
98
- await child.send({ kind: "approval", toolUseId: c.toolUseId, approved: answer.approved, reason: answer.reason });
99
- }
100
- }
101
- };
102
- try {
103
- await turn(prompt);
104
- // 步数用完时最后一条回复通常只有工具调用:再要一次报告,否则前面的调研全白做了
105
- if (!lastText(child.state.messages)) {
106
- control.wrapUp = true;
107
- await turn(WRAP_UP);
108
- }
109
- } catch (err) {
110
- failure = err;
111
- } finally {
112
- ctx.signal.removeEventListener("abort", forward);
113
- // 中断时也结束子会话,不留下没有结尾的日志
114
- await child.close().catch(() => {});
115
- }
116
- if (ctx.signal.aborted) throw failure ?? ctx.signal.reason;
117
-
118
- const ref = { sessionId, seq: child.state.lastSeq };
119
- // 还是没有报告时,把它一路上说过的话都带回去
120
- const text = clip(lastText(child.state.messages) || allText(child.state.messages));
121
- if (failure) {
122
- const msg = failure instanceof Error ? failure.message : String(failure);
123
- return { content: `Sub-agent failed: ${msg}${text ? `\nLast output:\n${text}` : ""}`, isError: true, child: ref };
124
- }
125
- return { content: text || "(sub-agent returned no text)", isError: !text, child: ref };
126
- },
127
- };
128
- }
129
-
130
- const textOf = (m: Message) => m.content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("\n").trim();
131
-
132
- /** 最后一条回复的正文 */
133
- function lastText(messages: Message[]): string {
134
- const last = messages.findLast((m) => m.role === "assistant");
135
- return last ? textOf(last) : "";
136
- }
137
-
138
- /** 所有回复的正文 */
139
- function allText(messages: Message[]): string {
140
- return messages
141
- .filter((m) => m.role === "assistant")
142
- .map(textOf)
143
- .filter(Boolean)
144
- .join("\n\n");
145
- }
146
-
147
- function clip(s: string): string {
148
- return s.length > MAX_REPORT_CHARS ? `${s.slice(0, MAX_REPORT_CHARS)}\n… (report truncated: ${s.length - MAX_REPORT_CHARS} more characters)` : s;
149
- }