@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,476 @@
1
+ /**
2
+ * 配置文件校验(设计 §10、§6.1、§7.3)。[B5]
3
+ *
4
+ * 形状定义在 config/types.ts(B0)与 hooks/types.ts(HookConfig);这里只做校验并再导出。
5
+ * 校验结果是诊断列表:`severity: "error"` 拒绝该文件(启动退出码 3),`"warning"` 只提示
6
+ * (未知字段等)。`path` 是 JSON 指针风格的字段路径(`permission.mode`、`hooks.PreToolUse[0]`)。
7
+ */
8
+ import { HOOK_EVENTS } from "../hooks/types.js";
9
+ import { PERMISSION_MODES_STRICT_FIRST } from "../permissions/types.js";
10
+ import { WARMING_MODES } from "../ai/cache/types.js";
11
+ import { CACHE_RETENTIONS, CHANNEL_NAME_PATTERN, CODEMODE_MODES, TOOLS_PRESETS_STRICT_FIRST, } from "./types.js";
12
+ export { CONFIG_FILE_VERSION } from "./types.js";
13
+ export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh"];
14
+ export const HOOK_TIMEOUT_MAX_MS = 600_000;
15
+ class Checker {
16
+ diagnostics = [];
17
+ error(path, message) {
18
+ this.diagnostics.push({ severity: "error", path, message });
19
+ }
20
+ warn(path, message) {
21
+ this.diagnostics.push({ severity: "warning", path, message });
22
+ }
23
+ object(value, path) {
24
+ if (typeof value === "object" && value !== null && !Array.isArray(value))
25
+ return true;
26
+ this.error(path, "应为对象");
27
+ return false;
28
+ }
29
+ /** 未知字段只警告(前向兼容)。 */
30
+ keys(value, path, allowed) {
31
+ for (const key of Object.keys(value)) {
32
+ if (!allowed.includes(key))
33
+ this.warn(join(path, key), "未知字段,已忽略");
34
+ }
35
+ }
36
+ version(value, path) {
37
+ if (value["version"] !== 1)
38
+ this.error(join(path, "version"), "version 必须为 1");
39
+ }
40
+ string(value, key, path, required = false) {
41
+ const v = value[key];
42
+ if (v === undefined) {
43
+ if (required)
44
+ this.error(join(path, key), "缺少必填字符串");
45
+ return;
46
+ }
47
+ if (typeof v !== "string")
48
+ this.error(join(path, key), "应为字符串");
49
+ }
50
+ boolean(value, key, path) {
51
+ const v = value[key];
52
+ if (v !== undefined && typeof v !== "boolean")
53
+ this.error(join(path, key), "应为布尔值");
54
+ }
55
+ number(value, key, path, min = 0, max = Number.MAX_SAFE_INTEGER) {
56
+ const v = value[key];
57
+ if (v === undefined)
58
+ return;
59
+ if (typeof v !== "number" || !Number.isFinite(v)) {
60
+ this.error(join(path, key), "应为数字");
61
+ }
62
+ else if (v < min || v > max) {
63
+ this.error(join(path, key), `应在 ${min}–${max} 之间`);
64
+ }
65
+ }
66
+ oneOf(value, key, path, choices) {
67
+ const v = value[key];
68
+ if (v === undefined)
69
+ return;
70
+ if (typeof v !== "string" || !choices.includes(v)) {
71
+ this.error(join(path, key), `取值应为 ${choices.join(" | ")}`);
72
+ }
73
+ }
74
+ stringArray(value, key, path) {
75
+ const v = value[key];
76
+ if (v === undefined)
77
+ return;
78
+ if (!Array.isArray(v) || v.some((item) => typeof item !== "string")) {
79
+ this.error(join(path, key), "应为字符串数组");
80
+ }
81
+ }
82
+ stringRecord(value, key, path) {
83
+ const v = value[key];
84
+ if (v === undefined)
85
+ return;
86
+ if (!this.object(v, join(path, key)))
87
+ return;
88
+ for (const [k, item] of Object.entries(v)) {
89
+ if (typeof item !== "string")
90
+ this.error(join(join(path, key), k), "应为字符串");
91
+ }
92
+ }
93
+ }
94
+ function join(path, key) {
95
+ if (typeof key === "number")
96
+ return `${path}[${key}]`;
97
+ return path === "" ? key : `${path}.${key}`;
98
+ }
99
+ export function hasErrors(diagnostics) {
100
+ return diagnostics.some((d) => d.severity === "error");
101
+ }
102
+ // ---------------------------------------------------------------------------
103
+ // config.json
104
+ // ---------------------------------------------------------------------------
105
+ const CONFIG_KEYS = [
106
+ "version",
107
+ "defaultModel",
108
+ "thinkingLevel",
109
+ "providers",
110
+ "permission",
111
+ "compaction",
112
+ "retry",
113
+ "tools",
114
+ "codemode",
115
+ "hooks",
116
+ "ui",
117
+ "skills",
118
+ "cache",
119
+ "$schema",
120
+ ];
121
+ const PROVIDER_KEYS = [
122
+ "name",
123
+ "api",
124
+ "baseUrl",
125
+ "apiKey",
126
+ "envKeys",
127
+ "authHeader",
128
+ "headers",
129
+ "compat",
130
+ "requiresApiKey",
131
+ "channels",
132
+ "defaultChannel",
133
+ "models",
134
+ "modelOverrides",
135
+ ];
136
+ const CHANNEL_KEYS = ["api", "baseUrl", "apiKey", "authHeader", "headers", "compat"];
137
+ /** 第三波 §1.3 的缓存兼容开关(其余 compat 字段按协议各异,只查是对象)。 */
138
+ const CACHE_COMPAT_FLAGS = [
139
+ "sendPromptCacheKey",
140
+ "sendSessionAffinityHeaders",
141
+ "supportsLongCacheRetention",
142
+ "supportsExplicitPromptCacheMode",
143
+ ];
144
+ function checkModels(c, value, path, channels) {
145
+ if (value === undefined)
146
+ return;
147
+ if (!Array.isArray(value)) {
148
+ c.error(path, "应为数组");
149
+ return;
150
+ }
151
+ value.forEach((item, index) => {
152
+ const p = join(path, index);
153
+ if (!c.object(item, p))
154
+ return;
155
+ c.string(item, "id", p, true);
156
+ c.string(item, "name", p);
157
+ c.number(item, "contextWindow", p, 1);
158
+ c.number(item, "maxTokens", p, 1);
159
+ c.boolean(item, "reasoning", p);
160
+ c.string(item, "api", p);
161
+ c.string(item, "baseUrl", p);
162
+ c.stringRecord(item, "headers", p);
163
+ const modelsDev = item["modelsDev"];
164
+ if (modelsDev !== undefined &&
165
+ modelsDev !== false &&
166
+ (typeof modelsDev !== "string" || !/^[^/]+\/.+$/.test(modelsDev))) {
167
+ c.error(join(p, "modelsDev"), `应为 "provider/model" 或 false`);
168
+ }
169
+ c.stringArray(item, "channels", p);
170
+ const used = item["channels"];
171
+ if (Array.isArray(used)) {
172
+ used.forEach((name, i) => {
173
+ if (typeof name !== "string")
174
+ return;
175
+ if (channels === undefined)
176
+ c.error(join(join(p, "channels"), i), "供应商没有 channels");
177
+ else if (!channels.has(name))
178
+ c.error(join(join(p, "channels"), i), `渠道 "${name}" 不存在(可用:${[...channels].join(", ")})`);
179
+ });
180
+ }
181
+ const promptCache = item["promptCache"];
182
+ if (promptCache !== undefined && c.object(promptCache, join(p, "promptCache"))) {
183
+ const pp = join(p, "promptCache");
184
+ c.keys(promptCache, pp, ["short", "long", "minTokens"]);
185
+ for (const key of ["short", "long", "minTokens"])
186
+ c.number(promptCache, key, pp);
187
+ }
188
+ const input = item["input"];
189
+ if (input !== undefined &&
190
+ (!Array.isArray(input) || input.some((m) => m !== "text" && m !== "image"))) {
191
+ c.error(join(p, "input"), `应为 ("text" | "image")[]`);
192
+ }
193
+ });
194
+ }
195
+ function checkCompat(c, compat, path) {
196
+ if (compat === undefined || !c.object(compat, path))
197
+ return;
198
+ for (const key of CACHE_COMPAT_FLAGS)
199
+ c.boolean(compat, key, path);
200
+ c.oneOf(compat, "cacheReporting", path, ["auto", "silent", "reported"]);
201
+ }
202
+ /** 返回合法的渠道名集合;没有 `channels` 时 undefined。 */
203
+ function checkChannels(c, value, path) {
204
+ const channels = value["channels"];
205
+ if (channels === undefined)
206
+ return undefined;
207
+ const cp = join(path, "channels");
208
+ if (!c.object(channels, cp))
209
+ return new Set();
210
+ const names = new Set();
211
+ for (const [name, channel] of Object.entries(channels)) {
212
+ const p = join(cp, name);
213
+ if (!CHANNEL_NAME_PATTERN.test(name)) {
214
+ c.error(p, "渠道名只能含字母、数字、_ 与 -(不含 / 与 @),最长 32");
215
+ continue;
216
+ }
217
+ names.add(name);
218
+ if (!c.object(channel, p))
219
+ continue;
220
+ c.keys(channel, p, CHANNEL_KEYS);
221
+ c.string(channel, "api", p, true);
222
+ c.string(channel, "baseUrl", p, true);
223
+ c.string(channel, "apiKey", p);
224
+ c.stringRecord(channel, "headers", p);
225
+ if (channel["authHeader"] !== undefined)
226
+ c.object(channel["authHeader"], join(p, "authHeader"));
227
+ checkCompat(c, channel["compat"], join(p, "compat"));
228
+ }
229
+ if (names.size === 0 && Object.keys(channels).length === 0)
230
+ c.error(cp, "至少要有一个渠道");
231
+ const preferred = value["defaultChannel"];
232
+ if (preferred !== undefined) {
233
+ if (typeof preferred !== "string")
234
+ c.error(join(path, "defaultChannel"), "应为字符串");
235
+ else if (!names.has(preferred))
236
+ c.error(join(path, "defaultChannel"), `渠道 "${preferred}" 不存在`);
237
+ }
238
+ return names;
239
+ }
240
+ function checkProvider(c, value, path) {
241
+ if (!c.object(value, path))
242
+ return;
243
+ c.keys(value, path, PROVIDER_KEYS);
244
+ for (const key of ["name", "api", "baseUrl", "apiKey"])
245
+ c.string(value, key, path);
246
+ c.stringArray(value, "envKeys", path);
247
+ c.stringRecord(value, "headers", path);
248
+ c.boolean(value, "requiresApiKey", path);
249
+ checkCompat(c, value["compat"], join(path, "compat"));
250
+ if (value["authHeader"] !== undefined)
251
+ c.object(value["authHeader"], join(path, "authHeader"));
252
+ const channels = checkChannels(c, value, path);
253
+ if (channels === undefined && value["defaultChannel"] !== undefined)
254
+ c.error(join(path, "defaultChannel"), "没有 channels 时不能设 defaultChannel");
255
+ checkModels(c, value["models"], join(path, "models"), channels);
256
+ checkModels(c, value["modelOverrides"], join(path, "modelOverrides"), channels);
257
+ }
258
+ function checkSection(c, config, key, allowed, body) {
259
+ const section = config[key];
260
+ if (section === undefined)
261
+ return;
262
+ if (!c.object(section, key))
263
+ return;
264
+ c.keys(section, key, allowed);
265
+ body(section, key);
266
+ }
267
+ export function validateConfig(value) {
268
+ const c = new Checker();
269
+ if (!c.object(value, ""))
270
+ return c.diagnostics;
271
+ c.version(value, "");
272
+ c.keys(value, "", CONFIG_KEYS);
273
+ c.string(value, "defaultModel", "");
274
+ c.oneOf(value, "thinkingLevel", "", THINKING_LEVELS);
275
+ const providers = value["providers"];
276
+ if (providers !== undefined && c.object(providers, "providers")) {
277
+ for (const [id, provider] of Object.entries(providers)) {
278
+ checkProvider(c, provider, join("providers", id));
279
+ }
280
+ }
281
+ checkSection(c, value, "permission", ["mode", "allow", "deny", "builtinDeny"], (s, p) => {
282
+ c.oneOf(s, "mode", p, PERMISSION_MODES_STRICT_FIRST);
283
+ c.stringArray(s, "allow", p);
284
+ c.stringArray(s, "deny", p);
285
+ const builtinDeny = s["builtinDeny"];
286
+ if (builtinDeny !== undefined &&
287
+ typeof builtinDeny !== "boolean" &&
288
+ (!Array.isArray(builtinDeny) || builtinDeny.some((item) => typeof item !== "string"))) {
289
+ c.error(join(p, "builtinDeny"), "应为布尔值或字符串数组");
290
+ }
291
+ });
292
+ checkSection(c, value, "compaction", ["enabled", "reserveTokens", "keepRecentTokens"], (s, p) => {
293
+ c.boolean(s, "enabled", p);
294
+ c.number(s, "reserveTokens", p);
295
+ c.number(s, "keepRecentTokens", p);
296
+ });
297
+ checkSection(c, value, "retry", ["enabled", "maxRetries", "baseDelayMs", "maxDelayMs"], (s, p) => {
298
+ c.boolean(s, "enabled", p);
299
+ c.number(s, "maxRetries", p, 0, 100);
300
+ c.number(s, "baseDelayMs", p);
301
+ c.number(s, "maxDelayMs", p);
302
+ });
303
+ checkSection(c, value, "tools", ["preset", "default", "maxToolResultChars", "bashTimeoutMs", "disabled"], (s, p) => {
304
+ c.oneOf(s, "preset", p, TOOLS_PRESETS_STRICT_FIRST);
305
+ c.stringArray(s, "default", p);
306
+ c.number(s, "maxToolResultChars", p, 1);
307
+ c.number(s, "bashTimeoutMs", p, 1);
308
+ c.stringArray(s, "disabled", p);
309
+ });
310
+ checkSection(c, value, "codemode", ["mode", "inlineBudget", "requireStrict"], (s, p) => {
311
+ c.oneOf(s, "mode", p, CODEMODE_MODES);
312
+ c.number(s, "inlineBudget", p, 0);
313
+ c.boolean(s, "requireStrict", p);
314
+ });
315
+ checkSection(c, value, "hooks", ["timeoutMs"], (s, p) => {
316
+ c.number(s, "timeoutMs", p, 1, HOOK_TIMEOUT_MAX_MS);
317
+ });
318
+ checkSection(c, value, "ui", ["theme", "markdown", "showThinking", "tuiMode", "quietStartup"], (s, p) => {
319
+ c.oneOf(s, "theme", p, ["dark", "light"]);
320
+ c.boolean(s, "markdown", p);
321
+ c.oneOf(s, "showThinking", p, ["full", "collapsed", "hidden"]);
322
+ c.oneOf(s, "tuiMode", p, ["regular"]);
323
+ c.oneOf(s, "quietStartup", p, ["normal", "header", "silent"]);
324
+ });
325
+ checkSection(c, value, "skills", ["dirs"], (s, p) => c.stringArray(s, "dirs", p));
326
+ checkSection(c, value, "cache", ["warming", "retention", "minSavingsUsd", "missNotices", "warmSubagents"], (s, p) => {
327
+ c.oneOf(s, "warming", p, WARMING_MODES);
328
+ c.oneOf(s, "retention", p, CACHE_RETENTIONS);
329
+ c.number(s, "minSavingsUsd", p, 0);
330
+ c.boolean(s, "missNotices", p);
331
+ c.boolean(s, "warmSubagents", p);
332
+ });
333
+ return c.diagnostics;
334
+ }
335
+ // ---------------------------------------------------------------------------
336
+ // auth.json / profile.json / trust.json / hooks.json
337
+ // ---------------------------------------------------------------------------
338
+ export function validateAuthFile(value) {
339
+ const c = new Checker();
340
+ if (!c.object(value, ""))
341
+ return c.diagnostics;
342
+ c.version(value, "");
343
+ c.keys(value, "", ["version", "providers"]);
344
+ const providers = value["providers"];
345
+ if (providers === undefined) {
346
+ c.error("providers", "缺少 providers");
347
+ return c.diagnostics;
348
+ }
349
+ if (!c.object(providers, "providers"))
350
+ return c.diagnostics;
351
+ for (const [id, entry] of Object.entries(providers)) {
352
+ const p = join("providers", id);
353
+ if (!c.object(entry, p))
354
+ continue;
355
+ c.keys(entry, p, ["apiKey", "env", "baseUrl"]);
356
+ c.string(entry, "apiKey", p, true);
357
+ c.stringRecord(entry, "env", p);
358
+ c.string(entry, "baseUrl", p);
359
+ }
360
+ return c.diagnostics;
361
+ }
362
+ export const PROFILE_PATH_FIELDS = [
363
+ "host",
364
+ "hooksFile",
365
+ "authFile",
366
+ "sessionDir",
367
+ "config",
368
+ ];
369
+ export const PROFILE_PATH_LIST_FIELDS = ["instructions", "skillDirs", "promptDirs"];
370
+ export function validateProfile(value) {
371
+ const c = new Checker();
372
+ if (!c.object(value, ""))
373
+ return c.diagnostics;
374
+ c.version(value, "");
375
+ c.keys(value, "", [
376
+ "version",
377
+ ...PROFILE_PATH_FIELDS,
378
+ ...PROFILE_PATH_LIST_FIELDS,
379
+ "authEnv",
380
+ "trustProject",
381
+ "$schema",
382
+ ]);
383
+ for (const key of PROFILE_PATH_FIELDS)
384
+ c.string(value, key, "");
385
+ for (const key of PROFILE_PATH_LIST_FIELDS)
386
+ c.stringArray(value, key, "");
387
+ c.boolean(value, "authEnv", "");
388
+ c.boolean(value, "trustProject", "");
389
+ return c.diagnostics;
390
+ }
391
+ export function validateTrustFile(value) {
392
+ const c = new Checker();
393
+ if (!c.object(value, ""))
394
+ return c.diagnostics;
395
+ c.version(value, "");
396
+ const entries = value["entries"];
397
+ if (!Array.isArray(entries)) {
398
+ c.error("entries", "应为数组");
399
+ return c.diagnostics;
400
+ }
401
+ entries.forEach((entry, index) => {
402
+ const p = join("entries", index);
403
+ if (!c.object(entry, p))
404
+ return;
405
+ c.string(entry, "path", p, true);
406
+ c.string(entry, "at", p, true);
407
+ if (typeof entry["trusted"] !== "boolean")
408
+ c.error(join(p, "trusted"), "应为布尔值");
409
+ });
410
+ return c.diagnostics;
411
+ }
412
+ export function validateHookConfig(value) {
413
+ const c = new Checker();
414
+ if (!c.object(value, ""))
415
+ return c.diagnostics;
416
+ c.version(value, "");
417
+ c.keys(value, "", ["version", "hooks", "$schema"]);
418
+ const hooks = value["hooks"];
419
+ if (hooks === undefined)
420
+ return c.diagnostics;
421
+ if (!c.object(hooks, "hooks"))
422
+ return c.diagnostics;
423
+ for (const [event, groups] of Object.entries(hooks)) {
424
+ const p = join("hooks", event);
425
+ if (!HOOK_EVENTS.includes(event)) {
426
+ c.error(p, `未知事件(可用:${HOOK_EVENTS.join(", ")})`);
427
+ continue;
428
+ }
429
+ if (!Array.isArray(groups)) {
430
+ c.error(p, "应为数组");
431
+ continue;
432
+ }
433
+ groups.forEach((group, gi) => {
434
+ const gp = join(p, gi);
435
+ if (!c.object(group, gp))
436
+ return;
437
+ c.keys(group, gp, ["matcher", "hooks"]);
438
+ c.string(group, "matcher", gp);
439
+ const commands = group["hooks"];
440
+ if (!Array.isArray(commands)) {
441
+ c.error(join(gp, "hooks"), "应为数组");
442
+ return;
443
+ }
444
+ commands.forEach((command, ci) => {
445
+ const cp = join(join(gp, "hooks"), ci);
446
+ if (!c.object(command, cp))
447
+ return;
448
+ c.keys(command, cp, ["type", "command", "timeoutMs"]);
449
+ if (command["type"] !== "command")
450
+ c.error(join(cp, "type"), `type 必须为 "command"`);
451
+ c.string(command, "command", cp, true);
452
+ if (typeof command["command"] === "string" && command["command"].trim() === "") {
453
+ c.error(join(cp, "command"), "命令为空");
454
+ }
455
+ c.number(command, "timeoutMs", cp, 1, HOOK_TIMEOUT_MAX_MS);
456
+ });
457
+ });
458
+ }
459
+ return c.diagnostics;
460
+ }
461
+ /** 校验通过后的类型断言(调用方保证 `hasErrors()` 为 false)。 */
462
+ export function asConfig(value) {
463
+ return value;
464
+ }
465
+ export function asAuthFile(value) {
466
+ return value;
467
+ }
468
+ export function asProfile(value) {
469
+ return value;
470
+ }
471
+ export function asTrustFile(value) {
472
+ return value;
473
+ }
474
+ export function asHookConfig(value) {
475
+ return value;
476
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * 项目信任(设计 §6.1「信任边界」、§7.3)。[B5]
3
+ *
4
+ * - 需要信任:`.ama/hooks.json`、`.ama/skills/`、`.ama/prompts/`、祖先 `.agents/skills/`。
5
+ * 不需要:AGENTS.md、`.ama/config.json`(它只能收紧)。
6
+ * - 决策顺序:`--trust` / `--no-trust` → profile `trustProject: true` → trust.json 中最近祖先的
7
+ * 记录 → 交互模式询问(一次,可记住)→ 非交互缺省不信任。
8
+ * - trust.json 只在用户级目录;写入为原子替换。
9
+ */
10
+ import type { TrustState } from "../cli/runtime.js";
11
+ import type { TrustEntry, TrustFile } from "./types.js";
12
+ export type { TrustState } from "../cli/runtime.js";
13
+ export declare function trustFilePath(configDir: string): string;
14
+ /** 读 trust.json;不存在 → 空表。语法 / 字段错误抛 StartupError(3)。 */
15
+ export declare function readTrustFile(configDir: string): TrustFile;
16
+ /** `dir` 是否等于 `ancestor` 或在其下。 */
17
+ export declare function isWithin(dir: string, ancestor: string, platform?: NodeJS.Platform): boolean;
18
+ /** 最近祖先(路径最长)的记录。 */
19
+ export declare function findTrustEntry(entries: readonly TrustEntry[], cwd: string, platform?: NodeJS.Platform): TrustEntry | undefined;
20
+ /** 记录信任决策(同路径覆盖)。 */
21
+ export declare function recordTrust(configDir: string, path: string, trusted: boolean, now?: Date): void;
22
+ /** cwd 下需要信任才能加载的资源(为空则无需询问)。 */
23
+ export declare function trustGatedResources(cwd: string): string[];
24
+ /** cwd 自身与全部祖先(由内向外)。 */
25
+ export declare function ancestorsOf(cwd: string): string[];
26
+ export interface TrustPromptAnswer {
27
+ trusted: boolean;
28
+ /** 写入 trust.json。 */
29
+ remember: boolean;
30
+ }
31
+ export interface DecideTrustInput {
32
+ cwd: string;
33
+ configDir: string;
34
+ /** `--trust` → true、`--no-trust` → false。 */
35
+ flag?: boolean | undefined;
36
+ /** profile.trustProject。 */
37
+ profileTrust?: boolean | undefined;
38
+ interactive: boolean;
39
+ /** 交互询问;不提供或非交互时跳过。 */
40
+ prompt?: ((cwd: string, resources: readonly string[]) => Promise<TrustPromptAnswer>) | undefined;
41
+ }
42
+ export declare function decideTrust(input: DecideTrustInput): Promise<TrustState>;
@@ -0,0 +1,122 @@
1
+ /**
2
+ * 项目信任(设计 §6.1「信任边界」、§7.3)。[B5]
3
+ *
4
+ * - 需要信任:`.ama/hooks.json`、`.ama/skills/`、`.ama/prompts/`、祖先 `.agents/skills/`。
5
+ * 不需要:AGENTS.md、`.ama/config.json`(它只能收紧)。
6
+ * - 决策顺序:`--trust` / `--no-trust` → profile `trustProject: true` → trust.json 中最近祖先的
7
+ * 记录 → 交互模式询问(一次,可记住)→ 非交互缺省不信任。
8
+ * - trust.json 只在用户级目录;写入为原子替换。
9
+ */
10
+ import { existsSync, mkdirSync, renameSync, statSync, writeFileSync } from "node:fs";
11
+ import { dirname, join, resolve, sep } from "node:path";
12
+ import { loadConfigFile } from "./load.js";
13
+ import { PROJECT_DIR, TRUST_FILE } from "./paths.js";
14
+ import { CONFIG_FILE_VERSION } from "./types.js";
15
+ export function trustFilePath(configDir) {
16
+ return join(configDir, TRUST_FILE);
17
+ }
18
+ /** 读 trust.json;不存在 → 空表。语法 / 字段错误抛 StartupError(3)。 */
19
+ export function readTrustFile(configDir) {
20
+ const loaded = loadConfigFile("trust", trustFilePath(configDir));
21
+ return loaded?.value ?? { version: CONFIG_FILE_VERSION, entries: [] };
22
+ }
23
+ function normalize(path, platform) {
24
+ const abs = resolve(path);
25
+ const trimmed = abs.length > 1 && abs.endsWith(sep) ? abs.slice(0, -1) : abs;
26
+ return platform === "win32" || platform === "darwin" ? trimmed.toLowerCase() : trimmed;
27
+ }
28
+ /** `dir` 是否等于 `ancestor` 或在其下。 */
29
+ export function isWithin(dir, ancestor, platform = process.platform) {
30
+ const d = normalize(dir, platform);
31
+ const a = normalize(ancestor, platform);
32
+ if (d === a)
33
+ return true;
34
+ const prefix = a.endsWith(sep) ? a : a + sep;
35
+ return d.startsWith(prefix);
36
+ }
37
+ /** 最近祖先(路径最长)的记录。 */
38
+ export function findTrustEntry(entries, cwd, platform = process.platform) {
39
+ let best;
40
+ for (const entry of entries) {
41
+ if (!isWithin(cwd, entry.path, platform))
42
+ continue;
43
+ if (best === undefined ||
44
+ normalize(entry.path, platform).length >= normalize(best.path, platform).length) {
45
+ best = entry;
46
+ }
47
+ }
48
+ return best;
49
+ }
50
+ /** 记录信任决策(同路径覆盖)。 */
51
+ export function recordTrust(configDir, path, trusted, now = new Date()) {
52
+ const file = readTrustFile(configDir);
53
+ const abs = resolve(path);
54
+ const entries = file.entries.filter((e) => normalize(e.path, process.platform) !== normalize(abs, process.platform));
55
+ entries.push({ path: abs, trusted, at: now.toISOString() });
56
+ const target = trustFilePath(configDir);
57
+ mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
58
+ const tmp = `${target}.${process.pid}.tmp`;
59
+ writeFileSync(tmp, `${JSON.stringify({ version: CONFIG_FILE_VERSION, entries }, null, 2)}\n`, {
60
+ mode: 0o600,
61
+ });
62
+ renameSync(tmp, target);
63
+ }
64
+ function isDir(path) {
65
+ try {
66
+ return statSync(path).isDirectory();
67
+ }
68
+ catch {
69
+ return false;
70
+ }
71
+ }
72
+ /** cwd 下需要信任才能加载的资源(为空则无需询问)。 */
73
+ export function trustGatedResources(cwd) {
74
+ const found = [];
75
+ const project = join(cwd, PROJECT_DIR);
76
+ if (existsSync(join(project, "hooks.json")))
77
+ found.push(join(project, "hooks.json"));
78
+ for (const sub of ["skills", "prompts"]) {
79
+ if (isDir(join(project, sub)))
80
+ found.push(join(project, sub));
81
+ }
82
+ for (const dir of ancestorsOf(cwd)) {
83
+ const skills = join(dir, ".agents", "skills");
84
+ if (isDir(skills))
85
+ found.push(skills);
86
+ }
87
+ return found;
88
+ }
89
+ /** cwd 自身与全部祖先(由内向外)。 */
90
+ export function ancestorsOf(cwd) {
91
+ const result = [];
92
+ let current = resolve(cwd);
93
+ for (;;) {
94
+ result.push(current);
95
+ const parent = dirname(current);
96
+ if (parent === current)
97
+ break;
98
+ current = parent;
99
+ }
100
+ return result;
101
+ }
102
+ export async function decideTrust(input) {
103
+ if (input.flag !== undefined)
104
+ return { trusted: input.flag, source: "flag" };
105
+ if (input.profileTrust === true)
106
+ return { trusted: true, source: "profile" };
107
+ const entry = findTrustEntry(readTrustFile(input.configDir).entries, input.cwd);
108
+ if (entry !== undefined) {
109
+ return { trusted: entry.trusted, source: "trust-file", matchedPath: entry.path };
110
+ }
111
+ if (input.interactive && input.prompt !== undefined) {
112
+ const resources = trustGatedResources(input.cwd);
113
+ // 没有需要信任的资源就不打扰用户;不信任对结果没有影响。
114
+ if (resources.length === 0)
115
+ return { trusted: false, source: "default" };
116
+ const answer = await input.prompt(input.cwd, resources);
117
+ if (answer.remember)
118
+ recordTrust(input.configDir, input.cwd, answer.trusted);
119
+ return { trusted: answer.trusted, source: "prompt" };
120
+ }
121
+ return { trusted: false, source: "default" };
122
+ }