@armadra/agent 0.0.0-stage → 0.3.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 (433) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/LICENSE +21 -0
  3. package/README.md +470 -2
  4. package/dist/agent/agent.d.ts +56 -0
  5. package/dist/agent/agent.js +129 -0
  6. package/dist/agent/loop.d.ts +51 -0
  7. package/dist/agent/loop.js +158 -0
  8. package/dist/agent/queue.d.ts +25 -0
  9. package/dist/agent/queue.js +55 -0
  10. package/dist/agent/retry.d.ts +22 -0
  11. package/dist/agent/retry.js +92 -0
  12. package/dist/agent/schema.d.ts +27 -0
  13. package/dist/agent/schema.js +186 -0
  14. package/dist/agent/session-cache.d.ts +100 -0
  15. package/dist/agent/session-cache.js +514 -0
  16. package/dist/agent/session-compaction.d.ts +49 -0
  17. package/dist/agent/session-compaction.js +251 -0
  18. package/dist/agent/session-core.d.ts +103 -0
  19. package/dist/agent/session-core.js +7 -0
  20. package/dist/agent/session-run.d.ts +55 -0
  21. package/dist/agent/session-run.js +246 -0
  22. package/dist/agent/session-state.d.ts +39 -0
  23. package/dist/agent/session-state.js +119 -0
  24. package/dist/agent/session-subagent.d.ts +54 -0
  25. package/dist/agent/session-subagent.js +157 -0
  26. package/dist/agent/session-sync.d.ts +20 -0
  27. package/dist/agent/session-sync.js +111 -0
  28. package/dist/agent/session-tools.d.ts +24 -0
  29. package/dist/agent/session-tools.js +277 -0
  30. package/dist/agent/session.d.ts +117 -0
  31. package/dist/agent/session.js +485 -0
  32. package/dist/agent/system-prompt.d.ts +49 -0
  33. package/dist/agent/system-prompt.js +121 -0
  34. package/dist/agent/tool-runner.d.ts +64 -0
  35. package/dist/agent/tool-runner.js +309 -0
  36. package/dist/agent/transform.d.ts +26 -0
  37. package/dist/agent/transform.js +171 -0
  38. package/dist/agent/types.d.ts +378 -0
  39. package/dist/agent/types.js +21 -0
  40. package/dist/ai/apis/anthropic-messages.d.ts +26 -0
  41. package/dist/ai/apis/anthropic-messages.js +214 -0
  42. package/dist/ai/apis/anthropic-request.d.ts +42 -0
  43. package/dist/ai/apis/anthropic-request.js +282 -0
  44. package/dist/ai/apis/api.d.ts +28 -0
  45. package/dist/ai/apis/api.js +102 -0
  46. package/dist/ai/apis/cache-params.d.ts +48 -0
  47. package/dist/ai/apis/cache-params.js +163 -0
  48. package/dist/ai/apis/google-generative-ai.d.ts +32 -0
  49. package/dist/ai/apis/google-generative-ai.js +249 -0
  50. package/dist/ai/apis/google-request.d.ts +40 -0
  51. package/dist/ai/apis/google-request.js +255 -0
  52. package/dist/ai/apis/openai-compat.d.ts +35 -0
  53. package/dist/ai/apis/openai-compat.js +142 -0
  54. package/dist/ai/apis/openai-completions.d.ts +28 -0
  55. package/dist/ai/apis/openai-completions.js +246 -0
  56. package/dist/ai/apis/openai-request.d.ts +35 -0
  57. package/dist/ai/apis/openai-request.js +303 -0
  58. package/dist/ai/apis/openai-responses-request.d.ts +52 -0
  59. package/dist/ai/apis/openai-responses-request.js +266 -0
  60. package/dist/ai/apis/openai-responses.d.ts +28 -0
  61. package/dist/ai/apis/openai-responses.js +304 -0
  62. package/dist/ai/apis/shared.d.ts +56 -0
  63. package/dist/ai/apis/shared.js +179 -0
  64. package/dist/ai/cache/economics.d.ts +24 -0
  65. package/dist/ai/cache/economics.js +69 -0
  66. package/dist/ai/cache/fingerprint.d.ts +17 -0
  67. package/dist/ai/cache/fingerprint.js +39 -0
  68. package/dist/ai/cache/miss.d.ts +49 -0
  69. package/dist/ai/cache/miss.js +96 -0
  70. package/dist/ai/cache/reporting.d.ts +39 -0
  71. package/dist/ai/cache/reporting.js +103 -0
  72. package/dist/ai/cache/types.d.ts +94 -0
  73. package/dist/ai/cache/types.js +19 -0
  74. package/dist/ai/cache/warmer.d.ts +94 -0
  75. package/dist/ai/cache/warmer.js +224 -0
  76. package/dist/ai/context.d.ts +35 -0
  77. package/dist/ai/context.js +114 -0
  78. package/dist/ai/cost.d.ts +21 -0
  79. package/dist/ai/cost.js +60 -0
  80. package/dist/ai/event-stream.d.ts +33 -0
  81. package/dist/ai/event-stream.js +101 -0
  82. package/dist/ai/fake/fake-provider.d.ts +68 -0
  83. package/dist/ai/fake/fake-provider.js +278 -0
  84. package/dist/ai/fake/fake-script.d.ts +69 -0
  85. package/dist/ai/fake/fake-script.js +142 -0
  86. package/dist/ai/http.d.ts +54 -0
  87. package/dist/ai/http.js +212 -0
  88. package/dist/ai/json-partial.d.ts +17 -0
  89. package/dist/ai/json-partial.js +243 -0
  90. package/dist/ai/overflow.d.ts +22 -0
  91. package/dist/ai/overflow.js +61 -0
  92. package/dist/ai/providers/auth.d.ts +71 -0
  93. package/dist/ai/providers/auth.js +202 -0
  94. package/dist/ai/providers/builtin.d.ts +23 -0
  95. package/dist/ai/providers/builtin.js +148 -0
  96. package/dist/ai/providers/catalog-data.d.ts +5 -0
  97. package/dist/ai/providers/catalog-data.js +19 -0
  98. package/dist/ai/providers/catalog.d.ts +28 -0
  99. package/dist/ai/providers/catalog.js +166 -0
  100. package/dist/ai/providers/channels.d.ts +51 -0
  101. package/dist/ai/providers/channels.js +96 -0
  102. package/dist/ai/providers/enrich.d.ts +34 -0
  103. package/dist/ai/providers/enrich.js +86 -0
  104. package/dist/ai/providers/models-dev-cache.d.ts +53 -0
  105. package/dist/ai/providers/models-dev-cache.js +147 -0
  106. package/dist/ai/providers/models-dev.d.ts +99 -0
  107. package/dist/ai/providers/models-dev.js +315 -0
  108. package/dist/ai/providers/registry.d.ts +114 -0
  109. package/dist/ai/providers/registry.js +458 -0
  110. package/dist/ai/sse.d.ts +32 -0
  111. package/dist/ai/sse.js +140 -0
  112. package/dist/ai/thinking.d.ts +42 -0
  113. package/dist/ai/thinking.js +101 -0
  114. package/dist/ai/types.d.ts +448 -0
  115. package/dist/ai/types.js +19 -0
  116. package/dist/bundle/ama-sandbox.cjs +324 -0
  117. package/dist/bundle/ama.cjs +27837 -0
  118. package/dist/bundle.d.ts +9 -0
  119. package/dist/bundle.js +15 -0
  120. package/dist/cli/args.d.ts +85 -0
  121. package/dist/cli/args.js +408 -0
  122. package/dist/cli/bootstrap.d.ts +18 -0
  123. package/dist/cli/bootstrap.js +399 -0
  124. package/dist/cli/compose-providers.d.ts +37 -0
  125. package/dist/cli/compose-providers.js +107 -0
  126. package/dist/cli/compose-session.d.ts +72 -0
  127. package/dist/cli/compose-session.js +370 -0
  128. package/dist/cli/compose-store.d.ts +24 -0
  129. package/dist/cli/compose-store.js +125 -0
  130. package/dist/cli/compose.d.ts +108 -0
  131. package/dist/cli/compose.js +221 -0
  132. package/dist/cli/default-model.d.ts +16 -0
  133. package/dist/cli/default-model.js +24 -0
  134. package/dist/cli/deps.d.ts +207 -0
  135. package/dist/cli/deps.js +12 -0
  136. package/dist/cli/exit-codes.d.ts +29 -0
  137. package/dist/cli/exit-codes.js +40 -0
  138. package/dist/cli/main.d.ts +27 -0
  139. package/dist/cli/main.js +177 -0
  140. package/dist/cli/runtime.d.ts +90 -0
  141. package/dist/cli/runtime.js +8 -0
  142. package/dist/cli/startup-screen.d.ts +15 -0
  143. package/dist/cli/startup-screen.js +56 -0
  144. package/dist/cli/startup-steps.d.ts +35 -0
  145. package/dist/cli/startup-steps.js +202 -0
  146. package/dist/cli/subcommands/auth.d.ts +12 -0
  147. package/dist/cli/subcommands/auth.js +91 -0
  148. package/dist/cli/subcommands/config.d.ts +53 -0
  149. package/dist/cli/subcommands/config.js +277 -0
  150. package/dist/cli/subcommands/context.d.ts +21 -0
  151. package/dist/cli/subcommands/context.js +42 -0
  152. package/dist/cli/subcommands/doctor.d.ts +11 -0
  153. package/dist/cli/subcommands/doctor.js +246 -0
  154. package/dist/cli/subcommands/init.d.ts +6 -0
  155. package/dist/cli/subcommands/init.js +22 -0
  156. package/dist/cli/subcommands/model-meta.d.ts +16 -0
  157. package/dist/cli/subcommands/model-meta.js +54 -0
  158. package/dist/cli/subcommands/models-cache-probe.d.ts +34 -0
  159. package/dist/cli/subcommands/models-cache-probe.js +190 -0
  160. package/dist/cli/subcommands/models-discover.d.ts +46 -0
  161. package/dist/cli/subcommands/models-discover.js +232 -0
  162. package/dist/cli/subcommands/models.d.ts +40 -0
  163. package/dist/cli/subcommands/models.js +152 -0
  164. package/dist/cli/subcommands/providers-list.d.ts +8 -0
  165. package/dist/cli/subcommands/providers-list.js +97 -0
  166. package/dist/cli/subcommands/providers-plan.d.ts +79 -0
  167. package/dist/cli/subcommands/providers-plan.js +215 -0
  168. package/dist/cli/subcommands/providers.d.ts +27 -0
  169. package/dist/cli/subcommands/providers.js +433 -0
  170. package/dist/cli/subcommands/sessions.d.ts +11 -0
  171. package/dist/cli/subcommands/sessions.js +100 -0
  172. package/dist/codemode/capability.d.ts +37 -0
  173. package/dist/codemode/capability.js +55 -0
  174. package/dist/codemode/declarations.d.ts +44 -0
  175. package/dist/codemode/declarations.js +111 -0
  176. package/dist/codemode/host-side.d.ts +59 -0
  177. package/dist/codemode/host-side.js +253 -0
  178. package/dist/codemode/modes.d.ts +23 -0
  179. package/dist/codemode/modes.js +40 -0
  180. package/dist/codemode/protocol.d.ts +91 -0
  181. package/dist/codemode/protocol.js +114 -0
  182. package/dist/codemode/sandbox-entry.d.ts +25 -0
  183. package/dist/codemode/sandbox-entry.js +323 -0
  184. package/dist/codemode/store.d.ts +26 -0
  185. package/dist/codemode/store.js +53 -0
  186. package/dist/codemode/tool.d.ts +105 -0
  187. package/dist/codemode/tool.js +283 -0
  188. package/dist/compaction/branch-summary.d.ts +30 -0
  189. package/dist/compaction/branch-summary.js +79 -0
  190. package/dist/compaction/breaker.d.ts +32 -0
  191. package/dist/compaction/breaker.js +65 -0
  192. package/dist/compaction/cut-point.d.ts +23 -0
  193. package/dist/compaction/cut-point.js +70 -0
  194. package/dist/compaction/estimate.d.ts +30 -0
  195. package/dist/compaction/estimate.js +114 -0
  196. package/dist/compaction/prune-tier.d.ts +31 -0
  197. package/dist/compaction/prune-tier.js +83 -0
  198. package/dist/compaction/serialize.d.ts +20 -0
  199. package/dist/compaction/serialize.js +115 -0
  200. package/dist/compaction/summarize-tier.d.ts +111 -0
  201. package/dist/compaction/summarize-tier.js +305 -0
  202. package/dist/config/auth-file.d.ts +39 -0
  203. package/dist/config/auth-file.js +92 -0
  204. package/dist/config/context-files.d.ts +30 -0
  205. package/dist/config/context-files.js +77 -0
  206. package/dist/config/init.d.ts +30 -0
  207. package/dist/config/init.js +92 -0
  208. package/dist/config/json-schema.d.ts +14 -0
  209. package/dist/config/json-schema.js +182 -0
  210. package/dist/config/load.d.ts +45 -0
  211. package/dist/config/load.js +97 -0
  212. package/dist/config/merge.d.ts +73 -0
  213. package/dist/config/merge.js +248 -0
  214. package/dist/config/paths.d.ts +38 -0
  215. package/dist/config/paths.js +108 -0
  216. package/dist/config/profile.d.ts +27 -0
  217. package/dist/config/profile.js +65 -0
  218. package/dist/config/schema.d.ts +34 -0
  219. package/dist/config/schema.js +476 -0
  220. package/dist/config/trust.d.ts +42 -0
  221. package/dist/config/trust.js +122 -0
  222. package/dist/config/types.d.ts +190 -0
  223. package/dist/config/types.js +29 -0
  224. package/dist/config/write.d.ts +10 -0
  225. package/dist/config/write.js +25 -0
  226. package/dist/errors.d.ts +28 -0
  227. package/dist/errors.js +30 -0
  228. package/dist/hooks/config.d.ts +49 -0
  229. package/dist/hooks/config.js +102 -0
  230. package/dist/hooks/dispatcher.d.ts +41 -0
  231. package/dist/hooks/dispatcher.js +104 -0
  232. package/dist/hooks/matcher.d.ts +22 -0
  233. package/dist/hooks/matcher.js +108 -0
  234. package/dist/hooks/protocol.d.ts +33 -0
  235. package/dist/hooks/protocol.js +240 -0
  236. package/dist/hooks/runner.d.ts +30 -0
  237. package/dist/hooks/runner.js +143 -0
  238. package/dist/hooks/types.d.ts +147 -0
  239. package/dist/hooks/types.js +20 -0
  240. package/dist/host/api-impl.d.ts +75 -0
  241. package/dist/host/api-impl.js +173 -0
  242. package/dist/host/loader.d.ts +31 -0
  243. package/dist/host/loader.js +147 -0
  244. package/dist/host/types.d.ts +169 -0
  245. package/dist/host/types.js +14 -0
  246. package/dist/host.d.ts +5 -0
  247. package/dist/host.js +4 -0
  248. package/dist/index.d.ts +41 -0
  249. package/dist/index.js +28 -0
  250. package/dist/modes/commands-core.d.ts +44 -0
  251. package/dist/modes/commands-core.js +171 -0
  252. package/dist/modes/image-input.d.ts +27 -0
  253. package/dist/modes/image-input.js +78 -0
  254. package/dist/modes/interactive/approval-dialog.d.ts +48 -0
  255. package/dist/modes/interactive/approval-dialog.js +202 -0
  256. package/dist/modes/interactive/commands.d.ts +41 -0
  257. package/dist/modes/interactive/commands.js +201 -0
  258. package/dist/modes/interactive/completion.d.ts +53 -0
  259. package/dist/modes/interactive/completion.js +157 -0
  260. package/dist/modes/interactive/interactive-mode.d.ts +46 -0
  261. package/dist/modes/interactive/interactive-mode.js +491 -0
  262. package/dist/modes/interactive/key-dispatch.d.ts +29 -0
  263. package/dist/modes/interactive/key-dispatch.js +107 -0
  264. package/dist/modes/interactive/line/line-editor.d.ts +50 -0
  265. package/dist/modes/interactive/line/line-editor.js +232 -0
  266. package/dist/modes/interactive/line/line-mode.d.ts +20 -0
  267. package/dist/modes/interactive/line/line-mode.js +218 -0
  268. package/dist/modes/interactive/line/line-render.d.ts +35 -0
  269. package/dist/modes/interactive/line/line-render.js +171 -0
  270. package/dist/modes/interactive/line/paste-state.d.ts +26 -0
  271. package/dist/modes/interactive/line/paste-state.js +72 -0
  272. package/dist/modes/interactive/message-view.d.ts +77 -0
  273. package/dist/modes/interactive/message-view.js +269 -0
  274. package/dist/modes/interactive/pickers.d.ts +42 -0
  275. package/dist/modes/interactive/pickers.js +115 -0
  276. package/dist/modes/interactive/startup-ui.d.ts +33 -0
  277. package/dist/modes/interactive/startup-ui.js +268 -0
  278. package/dist/modes/interactive/status-bar.d.ts +48 -0
  279. package/dist/modes/interactive/status-bar.js +139 -0
  280. package/dist/modes/interactive/tool-view.d.ts +95 -0
  281. package/dist/modes/interactive/tool-view.js +344 -0
  282. package/dist/modes/print/json-event.d.ts +15 -0
  283. package/dist/modes/print/json-event.js +39 -0
  284. package/dist/modes/print/print-mode.d.ts +17 -0
  285. package/dist/modes/print/print-mode.js +107 -0
  286. package/dist/modes/rpc/commands.d.ts +40 -0
  287. package/dist/modes/rpc/commands.js +263 -0
  288. package/dist/modes/rpc/jsonl.d.ts +16 -0
  289. package/dist/modes/rpc/jsonl.js +55 -0
  290. package/dist/modes/rpc/rpc-mode.d.ts +22 -0
  291. package/dist/modes/rpc/rpc-mode.js +153 -0
  292. package/dist/modes/session-report.d.ts +55 -0
  293. package/dist/modes/session-report.js +278 -0
  294. package/dist/modes/shared.d.ts +13 -0
  295. package/dist/modes/shared.js +52 -0
  296. package/dist/modes/startup-ui-text.d.ts +16 -0
  297. package/dist/modes/startup-ui-text.js +103 -0
  298. package/dist/permissions/broker.d.ts +47 -0
  299. package/dist/permissions/broker.js +107 -0
  300. package/dist/permissions/dangerous.d.ts +47 -0
  301. package/dist/permissions/dangerous.js +370 -0
  302. package/dist/permissions/pipeline.d.ts +51 -0
  303. package/dist/permissions/pipeline.js +181 -0
  304. package/dist/permissions/preview.d.ts +43 -0
  305. package/dist/permissions/preview.js +463 -0
  306. package/dist/permissions/rules.d.ts +58 -0
  307. package/dist/permissions/rules.js +249 -0
  308. package/dist/permissions/types.d.ts +100 -0
  309. package/dist/permissions/types.js +18 -0
  310. package/dist/rpc.d.ts +173 -0
  311. package/dist/rpc.js +13 -0
  312. package/dist/sdk.d.ts +98 -0
  313. package/dist/sdk.js +195 -0
  314. package/dist/session/manager.d.ts +70 -0
  315. package/dist/session/manager.js +278 -0
  316. package/dist/session/migrate.d.ts +19 -0
  317. package/dist/session/migrate.js +53 -0
  318. package/dist/session/projection.d.ts +50 -0
  319. package/dist/session/projection.js +184 -0
  320. package/dist/session/store.d.ts +47 -0
  321. package/dist/session/store.js +210 -0
  322. package/dist/session/tree.d.ts +21 -0
  323. package/dist/session/tree.js +88 -0
  324. package/dist/session/types.d.ts +201 -0
  325. package/dist/session/types.js +18 -0
  326. package/dist/skills/discover.d.ts +60 -0
  327. package/dist/skills/discover.js +160 -0
  328. package/dist/skills/expand.d.ts +35 -0
  329. package/dist/skills/expand.js +46 -0
  330. package/dist/skills/frontmatter.d.ts +27 -0
  331. package/dist/skills/frontmatter.js +157 -0
  332. package/dist/skills/index-prompt.d.ts +13 -0
  333. package/dist/skills/index-prompt.js +42 -0
  334. package/dist/skills/templates.d.ts +52 -0
  335. package/dist/skills/templates.js +177 -0
  336. package/dist/tools/bash.d.ts +49 -0
  337. package/dist/tools/bash.js +222 -0
  338. package/dist/tools/edit-fuzzy.d.ts +36 -0
  339. package/dist/tools/edit-fuzzy.js +129 -0
  340. package/dist/tools/edit.d.ts +38 -0
  341. package/dist/tools/edit.js +241 -0
  342. package/dist/tools/file-mutex.d.ts +8 -0
  343. package/dist/tools/file-mutex.js +29 -0
  344. package/dist/tools/glob.d.ts +44 -0
  345. package/dist/tools/glob.js +217 -0
  346. package/dist/tools/grep.d.ts +39 -0
  347. package/dist/tools/grep.js +179 -0
  348. package/dist/tools/ignore.d.ts +40 -0
  349. package/dist/tools/ignore.js +174 -0
  350. package/dist/tools/image-file.d.ts +31 -0
  351. package/dist/tools/image-file.js +114 -0
  352. package/dist/tools/ls.d.ts +21 -0
  353. package/dist/tools/ls.js +96 -0
  354. package/dist/tools/output-accumulator.d.ts +48 -0
  355. package/dist/tools/output-accumulator.js +106 -0
  356. package/dist/tools/paths.d.ts +20 -0
  357. package/dist/tools/paths.js +58 -0
  358. package/dist/tools/presets.d.ts +71 -0
  359. package/dist/tools/presets.js +128 -0
  360. package/dist/tools/process-tree.d.ts +35 -0
  361. package/dist/tools/process-tree.js +125 -0
  362. package/dist/tools/read.d.ts +24 -0
  363. package/dist/tools/read.js +132 -0
  364. package/dist/tools/registry.d.ts +43 -0
  365. package/dist/tools/registry.js +106 -0
  366. package/dist/tools/shell.d.ts +38 -0
  367. package/dist/tools/shell.js +91 -0
  368. package/dist/tools/task.d.ts +29 -0
  369. package/dist/tools/task.js +107 -0
  370. package/dist/tools/todo.d.ts +28 -0
  371. package/dist/tools/todo.js +107 -0
  372. package/dist/tools/truncate.d.ts +44 -0
  373. package/dist/tools/truncate.js +175 -0
  374. package/dist/tools/types.d.ts +135 -0
  375. package/dist/tools/types.js +19 -0
  376. package/dist/tools/write.d.ts +20 -0
  377. package/dist/tools/write.js +84 -0
  378. package/dist/tui/ansi.d.ts +29 -0
  379. package/dist/tui/ansi.js +420 -0
  380. package/dist/tui/component.d.ts +38 -0
  381. package/dist/tui/component.js +13 -0
  382. package/dist/tui/components/box.d.ts +26 -0
  383. package/dist/tui/components/box.js +65 -0
  384. package/dist/tui/components/container.d.ts +14 -0
  385. package/dist/tui/components/container.js +39 -0
  386. package/dist/tui/components/editor-buffer.d.ts +67 -0
  387. package/dist/tui/components/editor-buffer.js +300 -0
  388. package/dist/tui/components/editor-paste.d.ts +29 -0
  389. package/dist/tui/components/editor-paste.js +64 -0
  390. package/dist/tui/components/editor.d.ts +114 -0
  391. package/dist/tui/components/editor.js +530 -0
  392. package/dist/tui/components/key-value.d.ts +29 -0
  393. package/dist/tui/components/key-value.js +48 -0
  394. package/dist/tui/components/loader.d.ts +35 -0
  395. package/dist/tui/components/loader.js +71 -0
  396. package/dist/tui/components/markdown.d.ts +90 -0
  397. package/dist/tui/components/markdown.js +462 -0
  398. package/dist/tui/components/meter.d.ts +29 -0
  399. package/dist/tui/components/meter.js +47 -0
  400. package/dist/tui/components/overlay.d.ts +24 -0
  401. package/dist/tui/components/overlay.js +52 -0
  402. package/dist/tui/components/select-list.d.ts +57 -0
  403. package/dist/tui/components/select-list.js +179 -0
  404. package/dist/tui/components/spacer.d.ts +11 -0
  405. package/dist/tui/components/spacer.js +16 -0
  406. package/dist/tui/components/text.d.ts +30 -0
  407. package/dist/tui/components/text.js +65 -0
  408. package/dist/tui/keybindings.d.ts +66 -0
  409. package/dist/tui/keybindings.js +120 -0
  410. package/dist/tui/keys.d.ts +38 -0
  411. package/dist/tui/keys.js +275 -0
  412. package/dist/tui/stdin-buffer.d.ts +41 -0
  413. package/dist/tui/stdin-buffer.js +206 -0
  414. package/dist/tui/terminal.d.ts +79 -0
  415. package/dist/tui/terminal.js +153 -0
  416. package/dist/tui/theme.d.ts +31 -0
  417. package/dist/tui/theme.js +199 -0
  418. package/dist/tui/tui.d.ts +103 -0
  419. package/dist/tui/tui.js +363 -0
  420. package/dist/tui/vt-screen.d.ts +46 -0
  421. package/dist/tui/vt-screen.js +251 -0
  422. package/dist/tui.d.ts +30 -0
  423. package/dist/tui.js +31 -0
  424. package/dist/version.d.ts +8 -0
  425. package/dist/version.js +24 -0
  426. package/docs/codemode.md +102 -0
  427. package/docs/hooks.md +196 -0
  428. package/docs/host-api.md +161 -0
  429. package/docs/providers.md +531 -0
  430. package/docs/rpc.md +253 -0
  431. package/docs/session-format.md +119 -0
  432. package/docs/tui.md +170 -0
  433. package/package.json +101 -4
@@ -0,0 +1,39 @@
1
+ /**
2
+ * SessionState 快照与统计(设计 §1.2 session-state.ts、§12.6 状态栏)。[B2]
3
+ *
4
+ * 统计口径:活动分支上的全部条目(不是投影)——被压缩 / 剔除的消息仍计入用量与成本;
5
+ * 压缩与分支摘要请求的 usage 也计入;`usage` 条目(缓存保温等不进上下文的请求,第三波 §1.7)
6
+ * 计入 token 与费用但不算消息。上下文 % 用投影感知估算(§9)。
7
+ */
8
+ import type { Model, ModelThinkingLevel } from "../ai/types.js";
9
+ import type { PermissionMode } from "../permissions/types.js";
10
+ import type { SessionManager } from "../session/manager.js";
11
+ import type { SessionEntry } from "../session/types.js";
12
+ import type { Agent } from "./agent.js";
13
+ import type { SessionCacheStats, SessionState, SessionStats } from "./types.js";
14
+ export interface StateInput {
15
+ agent: Agent;
16
+ manager: SessionManager;
17
+ model: Model;
18
+ thinkingLevel: ModelThinkingLevel;
19
+ permissionMode: PermissionMode;
20
+ isCompacting: boolean;
21
+ isRetrying: boolean;
22
+ autoCompaction: boolean;
23
+ autoRetry: boolean;
24
+ }
25
+ export declare function buildSessionState(input: StateInput): SessionState;
26
+ export interface StatsInput {
27
+ sessionId: string;
28
+ sessionFile: string | undefined;
29
+ branch: readonly SessionEntry[];
30
+ contextTokens: number | undefined;
31
+ contextWindow: number | undefined;
32
+ /** [W3-C1b] 会话层缓存控制器的统计(未接线时缺省)。 */
33
+ cache?: SessionCacheStats;
34
+ }
35
+ export declare function computeStats(input: StatsInput): SessionStats;
36
+ /** 缓存命中率 = cacheRead /(input + cacheRead + cacheWrite);分母为 0 → undefined(设计 §9.1)。 */
37
+ export declare function cacheHitRate(tokens: Pick<SessionStats["tokens"], "input" | "cacheRead" | "cacheWrite">): number | undefined;
38
+ /** 最后一条有文本的助手消息(error / aborted 也算,供 UI 显示失败原因前的部分输出)。 */
39
+ export declare function lastAssistantText(branch: readonly SessionEntry[]): string | null;
@@ -0,0 +1,119 @@
1
+ /**
2
+ * SessionState 快照与统计(设计 §1.2 session-state.ts、§12.6 状态栏)。[B2]
3
+ *
4
+ * 统计口径:活动分支上的全部条目(不是投影)——被压缩 / 剔除的消息仍计入用量与成本;
5
+ * 压缩与分支摘要请求的 usage 也计入;`usage` 条目(缓存保温等不进上下文的请求,第三波 §1.7)
6
+ * 计入 token 与费用但不算消息。上下文 % 用投影感知估算(§9)。
7
+ */
8
+ import { modelRefOf } from "../ai/providers/channels.js";
9
+ export function buildSessionState(input) {
10
+ const { agent, manager, model } = input;
11
+ return {
12
+ isStreaming: agent.isRunning,
13
+ isCompacting: input.isCompacting,
14
+ isRetrying: input.isRetrying,
15
+ model: modelRefOf(model),
16
+ thinkingLevel: input.thinkingLevel,
17
+ permissionMode: input.permissionMode,
18
+ sessionId: manager.id,
19
+ sessionFile: manager.file(),
20
+ cwd: manager.cwd,
21
+ sessionName: manager.name(),
22
+ messageCount: agent.messages.filter((message) => message.role !== "system").length,
23
+ pendingMessageCount: agent.steeringQueue.size + agent.followUpQueue.size,
24
+ steeringMode: agent.steeringQueue.mode,
25
+ followUpMode: agent.followUpQueue.mode,
26
+ autoCompaction: input.autoCompaction,
27
+ autoRetry: input.autoRetry,
28
+ };
29
+ }
30
+ function addUsage(totals, usage) {
31
+ totals.input += usage.input;
32
+ totals.output += usage.output;
33
+ totals.cacheRead += usage.cacheRead;
34
+ totals.cacheWrite += usage.cacheWrite;
35
+ totals.total +=
36
+ usage.totalTokens > 0
37
+ ? usage.totalTokens
38
+ : usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
39
+ }
40
+ export function computeStats(input) {
41
+ const tokens = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
42
+ let userMessages = 0;
43
+ let assistantMessages = 0;
44
+ let toolCalls = 0;
45
+ let toolResults = 0;
46
+ let cost = 0;
47
+ const addCost = (usage) => {
48
+ if (cost === undefined)
49
+ return;
50
+ cost = usage.cost === undefined ? undefined : cost + usage.cost.total;
51
+ };
52
+ for (const entry of input.branch) {
53
+ if (entry.type === "message") {
54
+ const { message } = entry;
55
+ if (message.role === "user")
56
+ userMessages++;
57
+ else if (message.role === "toolResult")
58
+ toolResults++;
59
+ else if (message.role === "assistant") {
60
+ assistantMessages++;
61
+ toolCalls += message.content.filter((block) => block.type === "toolCall").length;
62
+ addUsage(tokens, message.usage);
63
+ addCost(message.usage);
64
+ }
65
+ }
66
+ else if (entry.type === "usage") {
67
+ addUsage(tokens, entry.usage);
68
+ addCost(entry.usage);
69
+ }
70
+ else if ((entry.type === "compaction" || entry.type === "branch_summary") && entry.usage) {
71
+ addUsage(tokens, entry.usage);
72
+ addCost(entry.usage);
73
+ }
74
+ }
75
+ const contextPercent = input.contextWindow === undefined ||
76
+ input.contextTokens === undefined ||
77
+ input.contextWindow <= 0
78
+ ? undefined
79
+ : Math.min(100, Math.round((input.contextTokens / input.contextWindow) * 1000) / 10);
80
+ const stats = {
81
+ sessionId: input.sessionId,
82
+ sessionFile: input.sessionFile,
83
+ userMessages,
84
+ assistantMessages,
85
+ toolCalls,
86
+ toolResults,
87
+ tokens,
88
+ cost,
89
+ contextTokens: input.contextTokens,
90
+ contextWindow: input.contextWindow,
91
+ contextPercent,
92
+ };
93
+ const rate = cacheHitRate(tokens);
94
+ if (rate !== undefined)
95
+ stats.cacheHitRate = rate;
96
+ if (input.cache !== undefined)
97
+ stats.cache = input.cache;
98
+ return stats;
99
+ }
100
+ /** 缓存命中率 = cacheRead /(input + cacheRead + cacheWrite);分母为 0 → undefined(设计 §9.1)。 */
101
+ export function cacheHitRate(tokens) {
102
+ const denominator = tokens.input + tokens.cacheRead + tokens.cacheWrite;
103
+ return denominator > 0 ? tokens.cacheRead / denominator : undefined;
104
+ }
105
+ /** 最后一条有文本的助手消息(error / aborted 也算,供 UI 显示失败原因前的部分输出)。 */
106
+ export function lastAssistantText(branch) {
107
+ for (let i = branch.length - 1; i >= 0; i--) {
108
+ const entry = branch[i];
109
+ if (entry?.type !== "message" || entry.message.role !== "assistant")
110
+ continue;
111
+ const text = entry.message.content
112
+ .filter((block) => block.type === "text")
113
+ .map((block) => block.text)
114
+ .join("");
115
+ if (text.trim() !== "")
116
+ return text;
117
+ }
118
+ return null;
119
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * 子 Agent(设计 §5.2 task、D14):`ToolContext.spawnSubagent` 的实现,供 B3 的 task 工具调用。[B2]
3
+ *
4
+ * - 同进程新会话:独立 JSONL(头的 parentSession 指回父文件;父为内存会话则子也在内存),首条
5
+ * `custom{customType:"ama.task"}` 记父 toolCallId。
6
+ * - 深度 ≤ 1(子会话 depth 1、无 spawnSubagent);并发 ≤ 4(排队等待)。
7
+ * - 工具子集缺省 = 父的活动集,总是去掉 task;继承父的权限管线、Hook 与系统提示静态部分;
8
+ * broker 包一层,请求带 `context{depth, parentToolCallId}`,审批事件转发到父会话。
9
+ * - 父 abort 级联;结果 = 子的最后助手文本 + 用量 + 子会话文件。
10
+ * - [W3-C1b] 子会话有自己的缓存控制器(统计独立、未命中不进父链,`warmSubagents` 为 false 时
11
+ * 不保温);结果带 `cache{hitRate, reBilledTokens}`,并汇总进父会话的「子任务」统计。
12
+ */
13
+ import { SessionManager } from "../session/manager.js";
14
+ import type { SubagentRequest, SubagentResult } from "../tools/types.js";
15
+ import type { AgentSessionOptions } from "./session-core.js";
16
+ import type { SessionEvent, SessionStats } from "./types.js";
17
+ export declare const DEFAULT_SUBAGENT_CONCURRENCY = 4;
18
+ export declare const DEFAULT_SUBAGENT_MAX_TURNS = 30;
19
+ export declare const TASK_CUSTOM_TYPE = "ama.task";
20
+ /** 计数信号量。 */
21
+ export declare class SubagentPool {
22
+ private readonly limit;
23
+ private running;
24
+ private readonly waiters;
25
+ constructor(limit: number);
26
+ acquire(signal: AbortSignal): Promise<void>;
27
+ release(): void;
28
+ }
29
+ export interface SubagentParent {
30
+ readonly options: AgentSessionOptions;
31
+ readonly manager: SessionManager;
32
+ readonly cwd: string;
33
+ readonly depth: number;
34
+ childBase(): Pick<AgentSessionOptions, "model" | "thinkingLevel" | "activeTools" | "system">;
35
+ /** 子会话的审批事件转发给父会话的订阅者(RPC 客户端 / TUI 据此作答)。 */
36
+ emit?(event: SessionEvent): void;
37
+ /** [W3-C1b] 父会话的缓存控制器:汇总子会话的命中与重计费。 */
38
+ readonly cache?: {
39
+ addSubagent(tokens: {
40
+ cacheRead: number;
41
+ prompt: number;
42
+ }, reBilledTokens: number): void;
43
+ };
44
+ }
45
+ export interface ChildSession {
46
+ readonly manager: SessionManager;
47
+ prompt(text: string): Promise<unknown>;
48
+ abort(): Promise<void>;
49
+ subscribe(listener: (event: SessionEvent) => void): () => void;
50
+ getStats(): SessionStats;
51
+ getLastAssistantText(): string | null;
52
+ dispose(): Promise<void>;
53
+ }
54
+ export declare function runSubagent(parent: SubagentParent, request: SubagentRequest, pool: SubagentPool, createChild: (options: AgentSessionOptions) => ChildSession): Promise<SubagentResult>;
@@ -0,0 +1,157 @@
1
+ /**
2
+ * 子 Agent(设计 §5.2 task、D14):`ToolContext.spawnSubagent` 的实现,供 B3 的 task 工具调用。[B2]
3
+ *
4
+ * - 同进程新会话:独立 JSONL(头的 parentSession 指回父文件;父为内存会话则子也在内存),首条
5
+ * `custom{customType:"ama.task"}` 记父 toolCallId。
6
+ * - 深度 ≤ 1(子会话 depth 1、无 spawnSubagent);并发 ≤ 4(排队等待)。
7
+ * - 工具子集缺省 = 父的活动集,总是去掉 task;继承父的权限管线、Hook 与系统提示静态部分;
8
+ * broker 包一层,请求带 `context{depth, parentToolCallId}`,审批事件转发到父会话。
9
+ * - 父 abort 级联;结果 = 子的最后助手文本 + 用量 + 子会话文件。
10
+ * - [W3-C1b] 子会话有自己的缓存控制器(统计独立、未命中不进父链,`warmSubagents` 为 false 时
11
+ * 不保温);结果带 `cache{hitRate, reBilledTokens}`,并汇总进父会话的「子任务」统计。
12
+ */
13
+ import { AmaError } from "../errors.js";
14
+ import { SessionManager } from "../session/manager.js";
15
+ import { ZERO_USAGE } from "./loop.js";
16
+ export const DEFAULT_SUBAGENT_CONCURRENCY = 4;
17
+ export const DEFAULT_SUBAGENT_MAX_TURNS = 30;
18
+ export const TASK_CUSTOM_TYPE = "ama.task";
19
+ /** 计数信号量。 */
20
+ export class SubagentPool {
21
+ limit;
22
+ running = 0;
23
+ waiters = [];
24
+ constructor(limit) {
25
+ this.limit = limit;
26
+ }
27
+ async acquire(signal) {
28
+ while (this.running >= this.limit) {
29
+ if (signal.aborted)
30
+ throw new AmaError("aborted", "aborted");
31
+ await new Promise((resolve) => this.waiters.push(resolve));
32
+ }
33
+ this.running++;
34
+ }
35
+ release() {
36
+ this.running--;
37
+ this.waiters.shift()?.();
38
+ }
39
+ }
40
+ /** 子会话的 broker:请求带上发起方上下文(对话框标 `[task]`),其余原样交给父链。 */
41
+ function brokersForChild(brokers, context) {
42
+ return (brokers ?? []).map((broker) => ({
43
+ ask: (request, signal) => broker.ask({ ...request, context }, signal),
44
+ }));
45
+ }
46
+ function failed(text) {
47
+ return { text, usage: { ...ZERO_USAGE }, stopReason: "error", isError: true };
48
+ }
49
+ export async function runSubagent(parent, request, pool, createChild) {
50
+ if (parent.depth > 0)
51
+ return failed("subagents cannot spawn subagents");
52
+ await pool.acquire(request.signal);
53
+ try {
54
+ const base = parent.childBase();
55
+ let model = base.model;
56
+ if (request.model !== undefined) {
57
+ const lookup = parent.options.providers.findModel(request.model);
58
+ if (!lookup.ok)
59
+ return failed(`unknown model ${request.model}`);
60
+ model = lookup.model;
61
+ }
62
+ const parentFile = parent.manager.file();
63
+ const headerOptions = parentFile === undefined ? {} : { parentSession: parentFile };
64
+ const dir = parent.manager.directory();
65
+ const childManager = dir === undefined
66
+ ? SessionManager.inMemory(parent.cwd, headerOptions)
67
+ : SessionManager.create(dir, parent.cwd, headerOptions);
68
+ childManager.append({
69
+ type: "custom",
70
+ customType: TASK_CUSTOM_TYPE,
71
+ data: {
72
+ parentToolCallId: request.parentToolCallId,
73
+ description: request.description,
74
+ parentSession: parentFile,
75
+ },
76
+ });
77
+ const available = new Set((parent.options.tools ?? []).map((tool) => tool.name));
78
+ const names = (request.tools ?? base.activeTools ?? [...available]).filter((name) => name !== "task" && available.has(name));
79
+ const child = createChild({
80
+ ...parent.options,
81
+ ...base,
82
+ sessionManager: childManager,
83
+ model,
84
+ thinkingLevel: request.thinkingLevel ?? base.thinkingLevel ?? "off",
85
+ activeTools: names,
86
+ brokers: brokersForChild(parent.options.brokers, {
87
+ depth: parent.depth + 1,
88
+ parentToolCallId: request.parentToolCallId,
89
+ }),
90
+ depth: parent.depth + 1,
91
+ maxTurns: request.maxTurns ?? DEFAULT_SUBAGENT_MAX_TURNS,
92
+ subagents: false,
93
+ });
94
+ const onAbort = () => void child.abort();
95
+ request.signal.addEventListener("abort", onAbort, { once: true });
96
+ const unsubscribe = child.subscribe((event) => {
97
+ if (event.type === "tool_execution_start")
98
+ request.onUpdate?.(`[task] ${event.toolName}`);
99
+ else if (event.type === "permission_request" || event.type === "permission_resolved")
100
+ parent.emit?.(event);
101
+ });
102
+ let error;
103
+ try {
104
+ if (request.signal.aborted)
105
+ throw new AmaError("aborted", "aborted");
106
+ await child.prompt(request.prompt);
107
+ }
108
+ catch (caught) {
109
+ error = caught instanceof Error ? caught.message : String(caught);
110
+ }
111
+ finally {
112
+ request.signal.removeEventListener("abort", onAbort);
113
+ unsubscribe();
114
+ }
115
+ const stats = child.getStats();
116
+ const last = child.manager
117
+ .branch()
118
+ .findLast((entry) => entry.type === "message" && entry.message.role === "assistant");
119
+ const stopReason = error !== undefined
120
+ ? "error"
121
+ : last?.type === "message" && last.message.role === "assistant"
122
+ ? last.message.stopReason
123
+ : "stop";
124
+ const result = {
125
+ text: child.getLastAssistantText() ?? error ?? "",
126
+ usage: {
127
+ input: stats.tokens.input,
128
+ output: stats.tokens.output,
129
+ cacheRead: stats.tokens.cacheRead,
130
+ cacheWrite: stats.tokens.cacheWrite,
131
+ totalTokens: stats.tokens.total,
132
+ },
133
+ stopReason,
134
+ isError: error !== undefined || stopReason === "error" || stopReason === "aborted",
135
+ };
136
+ const cache = stats.cache;
137
+ if (cache !== undefined) {
138
+ result.cache = { reBilledTokens: cache.reBilledTokens };
139
+ if (cache.hitRate !== undefined)
140
+ result.cache.hitRate = cache.hitRate;
141
+ // 不报缓存的子会话不进命中率分母(同父会话口径)
142
+ const reported = cache.reporting === "reported";
143
+ const { input, cacheRead, cacheWrite } = stats.tokens;
144
+ parent.cache?.addSubagent(reported
145
+ ? { cacheRead, prompt: input + cacheRead + cacheWrite }
146
+ : { cacheRead: 0, prompt: 0 }, cache.reBilledTokens);
147
+ }
148
+ const childFile = childManager.file();
149
+ if (childFile !== undefined)
150
+ result.sessionFile = childFile;
151
+ await child.dispose();
152
+ return result;
153
+ }
154
+ finally {
155
+ pool.release();
156
+ }
157
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 会话的落盘与同步辅助(设计 §8、§11.1 第 16 步)。[B2]
3
+ *
4
+ * - `persistMessage`:Agent 的 message_end → 会话条目(LLM 消息 → `message`;custom → `custom_message`)。
5
+ * - `recordModelState`:路径上最近的 model_change / thinking_level_change 与当前不同则补记。
6
+ * - `syncSystemMessage`:目标系统提示 + 工具表与转录重放的状态比对,落全量(首条)或补丁;
7
+ * 中途只追加工具时 tools / rules 节保持已发送的文本(前缀稳定,§9.1)。
8
+ * - `runHookWithEvents`:调 HookDispatcher(公共字段按本会话的 depth / sessionId / sessionFile 覆盖),
9
+ * 逐条发 hook_executed;Hook 自身出错记 warning、按无决策处理。
10
+ */
11
+ import type { Model, ModelThinkingLevel } from "../ai/types.js";
12
+ import type { HookEvent, HookEventPayload, HookOutcome } from "../hooks/types.js";
13
+ import type { AgentMessage } from "../session/types.js";
14
+ import type { ToolDefinition } from "../tools/types.js";
15
+ import type { SessionCore } from "./session-core.js";
16
+ import { type SystemPromptInput } from "./system-prompt.js";
17
+ export declare function persistMessage(core: SessionCore, message: AgentMessage): void;
18
+ export declare function recordModelState(core: SessionCore, model: Model, thinkingLevel: ModelThinkingLevel): void;
19
+ export declare function syncSystemMessage(core: SessionCore, input: Omit<SystemPromptInput, "tools" | "cwd">, tools: readonly ToolDefinition[]): void;
20
+ export declare function runHookWithEvents(core: Pick<SessionCore, "options" | "emit" | "log" | "depth" | "manager">, event: HookEvent, payload: HookEventPayload, signal?: AbortSignal): Promise<HookOutcome | undefined>;
@@ -0,0 +1,111 @@
1
+ /**
2
+ * 会话的落盘与同步辅助(设计 §8、§11.1 第 16 步)。[B2]
3
+ *
4
+ * - `persistMessage`:Agent 的 message_end → 会话条目(LLM 消息 → `message`;custom → `custom_message`)。
5
+ * - `recordModelState`:路径上最近的 model_change / thinking_level_change 与当前不同则补记。
6
+ * - `syncSystemMessage`:目标系统提示 + 工具表与转录重放的状态比对,落全量(首条)或补丁;
7
+ * 中途只追加工具时 tools / rules 节保持已发送的文本(前缀稳定,§9.1)。
8
+ * - `runHookWithEvents`:调 HookDispatcher(公共字段按本会话的 depth / sessionId / sessionFile 覆盖),
9
+ * 逐条发 hook_executed;Hook 自身出错记 warning、按无决策处理。
10
+ */
11
+ import { assembleSections, currentSystemState, definedSections, diffSystem, toolDecls, } from "./system-prompt.js";
12
+ export function persistMessage(core, message) {
13
+ if (message.role === "custom") {
14
+ const input = {
15
+ type: "custom_message",
16
+ customType: message.customType,
17
+ content: message.content,
18
+ display: message.display,
19
+ };
20
+ if (message.details !== undefined)
21
+ input.details = message.details;
22
+ core.appendEntry(input);
23
+ }
24
+ else if (message.role !== "compactionSummary" && message.role !== "branchSummary") {
25
+ core.appendEntry({ type: "message", message });
26
+ }
27
+ }
28
+ export function recordModelState(core, model, thinkingLevel) {
29
+ let provider;
30
+ let modelId;
31
+ let channel;
32
+ let thinking;
33
+ for (const entry of core.manager.branch()) {
34
+ if (entry.type === "model_change") {
35
+ provider = entry.provider;
36
+ modelId = entry.modelId;
37
+ channel = entry.channel;
38
+ }
39
+ else if (entry.type === "thinking_level_change")
40
+ thinking = entry.thinkingLevel;
41
+ }
42
+ if (provider !== model.provider || modelId !== model.id || channel !== model.channel) {
43
+ core.appendEntry({
44
+ type: "model_change",
45
+ provider: model.provider,
46
+ modelId: model.id,
47
+ ...(model.channel !== undefined ? { channel: model.channel } : {}),
48
+ });
49
+ }
50
+ if (thinking !== thinkingLevel) {
51
+ core.appendEntry({ type: "thinking_level_change", thinkingLevel });
52
+ }
53
+ }
54
+ export function syncSystemMessage(core, input, tools) {
55
+ recordModelState(core, core.model(), core.thinkingLevel());
56
+ const sections = definedSections(assembleSections({ ...input, tools, cwd: core.cwd }));
57
+ const current = currentSystemState(core.agent.messages);
58
+ if (current !== undefined)
59
+ keepSectionsOnToolAppend(current, sections, tools);
60
+ const patch = diffSystem(current, sections, toolDecls(tools));
61
+ if (patch === undefined)
62
+ return;
63
+ core.appendEntry({ type: "message", message: patch });
64
+ core.agent.messages.push(patch);
65
+ }
66
+ /**
67
+ * 会话中途只**追加**工具(宿主晚注册、setActiveTools 加名字)时,保留已发送的 tools / rules 节:
68
+ * 新工具的声明经请求工具表末尾追加,system 文本不变,前缀缓存不失效(设计 §9.1)。
69
+ * 有工具被移除时照常重写这两节。
70
+ */
71
+ function keepSectionsOnToolAppend(current, sections, tools) {
72
+ const now = new Set(tools.map((tool) => tool.name));
73
+ if (current.tools.some((tool) => !now.has(tool.name)))
74
+ return;
75
+ for (const name of ["tools", "rules"]) {
76
+ const sent = current.sections[name];
77
+ if (sent === undefined)
78
+ delete sections[name];
79
+ else
80
+ sections[name] = sent;
81
+ }
82
+ }
83
+ export async function runHookWithEvents(core, event, payload, signal) {
84
+ const hooks = core.options.hooks;
85
+ if (hooks === undefined || !hooks.has(event, payload.toolName))
86
+ return undefined;
87
+ try {
88
+ // 公共字段按本会话覆盖:子 Agent 的 depth / sessionId / sessionFile 不再沿用主会话的。
89
+ const outcome = await hooks.run(event, payload, signal, {
90
+ depth: core.depth,
91
+ sessionId: core.manager.id,
92
+ sessionFile: core.manager.file(),
93
+ });
94
+ for (const result of outcome.results) {
95
+ core.emit({
96
+ type: "hook_executed",
97
+ event: result.event,
98
+ command: result.command,
99
+ exitCode: result.exitCode,
100
+ durationMs: result.durationMs,
101
+ });
102
+ }
103
+ for (const warning of outcome.warnings)
104
+ core.log("warn", warning);
105
+ return outcome;
106
+ }
107
+ catch (error) {
108
+ core.log("warn", `${event} hook failed: ${String(error)}`);
109
+ return undefined;
110
+ }
111
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 会话侧的工具装配(设计 §5.1、§6.3):tool-runner 的注入项。[B2]
3
+ *
4
+ * 一次工具调用的门禁顺序(§6.3):schema 校验(tool-runner)→ 命令式 Hook PreToolUse(deny / block
5
+ * 一票否决;updatedInput 替换输入;allow / ask 作为 hookDecision 交给管线)→ 权限管线(B3,注入)→
6
+ * ask 时走审批链(宿主 broker → UI broker → 无人值守 deny;超时 deny)→ 执行 → PostToolUse
7
+ * (additionalContext 追加到结果末尾;block 把结果改为错误)。
8
+ *
9
+ * (W3-B9a-2)ask 时请求带 `context{depth, readFiles}` 与执行前预览 `preview`(只读、有上限,
10
+ * 见 permissions/preview.ts);预览出错或为空则缺省,不影响审批。`permission_request` 事件同样带
11
+ * `preview`,RPC 客户端可直接显示。
12
+ */
13
+ import type { ToolCallBlock } from "../ai/types.js";
14
+ import type { ApprovalDecision, ApprovalRequest } from "../permissions/types.js";
15
+ import type { ToolContext, ToolResult } from "../tools/types.js";
16
+ import type { SessionCore } from "./session-core.js";
17
+ import { type ToolRunnerOptions } from "./tool-runner.js";
18
+ import type { NestedCallInfo, ToolCallGate, ToolCallGateContext } from "./types.js";
19
+ /** 审批链:依次问每个 broker,第一个给出决定的为准;全部弃权 → deny。 */
20
+ export declare function requestApproval(core: SessionCore, request: ApprovalRequest, signal: AbortSignal): Promise<ApprovalDecision>;
21
+ export declare function gateToolCall(core: SessionCore, call: ToolCallBlock, ctx: ToolCallGateContext): Promise<ToolCallGate>;
22
+ export declare function afterToolCall(core: SessionCore, call: ToolCallBlock, result: ToolResult, parent?: NestedCallInfo): Promise<ToolResult>;
23
+ export declare function createToolContext(core: SessionCore, runner: ToolRunnerOptions, call: ToolCallBlock, signal: AbortSignal, onUpdate: (partial: string) => void): ToolContext;
24
+ export declare function createToolRunnerOptions(core: SessionCore): ToolRunnerOptions;