@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,263 @@
1
+ /**
2
+ * RPC 命令分派表(设计 §13.2、src/rpc.ts 的 33 条命令)。[B6]
3
+ *
4
+ * - 提示类(prompt / steer / follow_up)不等运行结束:会话开始运行(`before_agent_start` /
5
+ * `agent_start`)、入队或被处理后立刻应答 `{ disposition }`;运行中的进展走事件。
6
+ * - 会话类(new_session / switch_session / fork)经组装根 `switchSession`,`ctx.onSessionChanged`
7
+ * 让模式重新订阅事件。
8
+ * - 审批:`RpcApprovals` 是 UI broker(`set_client_capabilities` 声明 approvals 后挂上),
9
+ * `permission_response` 按 requestId 作答;先到的回答暂存,等 broker 被问到时取用。
10
+ * - 密钥不离开进程:`get_available_models` 只回 `hasKey` 与 `keySource`。
11
+ */
12
+ import { formatModelRef } from "../../ai/providers/channels.js";
13
+ import { AgentSessionImpl } from "../../agent/session.js";
14
+ import { getSupportedLevels } from "../../ai/thinking.js";
15
+ import { switchSession } from "../../cli/compose-session.js";
16
+ import { AmaError } from "../../errors.js";
17
+ import { BUILTIN_COMMANDS } from "../commands-core.js";
18
+ /** 审批 broker:等客户端 `permission_response`。 */
19
+ export class RpcApprovals {
20
+ pending = new Map();
21
+ early = new Map();
22
+ ask(request, signal) {
23
+ const ready = this.early.get(request.requestId);
24
+ if (ready !== undefined) {
25
+ this.early.delete(request.requestId);
26
+ return Promise.resolve(ready);
27
+ }
28
+ return new Promise((resolve) => {
29
+ const done = (decision) => {
30
+ this.pending.delete(request.requestId);
31
+ signal.removeEventListener("abort", onAbort);
32
+ resolve(decision);
33
+ };
34
+ const onAbort = () => done(undefined);
35
+ if (signal.aborted)
36
+ return done(undefined);
37
+ signal.addEventListener("abort", onAbort, { once: true });
38
+ this.pending.set(request.requestId, done);
39
+ });
40
+ }
41
+ /** 返回 false = 当前没有在等这个 requestId(已暂存,稍后被问到时生效)。 */
42
+ respond(requestId, decision) {
43
+ const resolve = this.pending.get(requestId);
44
+ if (resolve !== undefined) {
45
+ resolve(decision);
46
+ return true;
47
+ }
48
+ this.early.set(requestId, decision);
49
+ return false;
50
+ }
51
+ /** 关 stdin / 撤下时:全部弃权(会话按无人作答 deny)。 */
52
+ cancelAll() {
53
+ for (const resolve of [...this.pending.values()])
54
+ resolve(undefined);
55
+ this.early.clear();
56
+ }
57
+ }
58
+ function impl(session) {
59
+ if (session instanceof AgentSessionImpl)
60
+ return session;
61
+ throw new AmaError("not_implemented", "该会话不支持此命令");
62
+ }
63
+ /** 开始一次运行:开始 / 入队 / 被处理后即返回,不等运行结束。 */
64
+ async function startRun(ctx, run) {
65
+ const session = ctx.session();
66
+ let settled = false;
67
+ let off = () => undefined;
68
+ const started = new Promise((resolve) => {
69
+ off = session.subscribe((event) => {
70
+ if (event.type === "before_agent_start" || event.type === "agent_start")
71
+ resolve("started");
72
+ });
73
+ });
74
+ const running = run().finally(() => {
75
+ settled = true;
76
+ });
77
+ try {
78
+ const first = await Promise.race([running, started]);
79
+ if (!settled)
80
+ running.catch((error) => ctx.onBackgroundError(error));
81
+ return { disposition: first };
82
+ }
83
+ finally {
84
+ off();
85
+ }
86
+ }
87
+ async function switched(ctx, next, reason) {
88
+ const session = await next;
89
+ ctx.onSessionChanged(session);
90
+ if (session instanceof AgentSessionImpl)
91
+ session.announceStart(reason);
92
+ return { sessionId: session.state.sessionId, sessionFile: session.state.sessionFile };
93
+ }
94
+ async function availableModels(runtime) {
95
+ const out = [];
96
+ for (const provider of runtime.providers.list()) {
97
+ const key = await runtime.providers.resolveApiKey(provider.id);
98
+ const hasKey = key.apiKey !== undefined || !provider.requiresApiKey;
99
+ for (const model of provider.models) {
100
+ const info = {
101
+ provider: provider.id,
102
+ id: model.id,
103
+ name: model.name,
104
+ hasKey,
105
+ keySource: key.apiKey !== undefined ? key.source : "none",
106
+ maxTokens: model.maxTokens,
107
+ reasoning: model.reasoning,
108
+ input: [...model.input],
109
+ };
110
+ if (model.contextWindow !== undefined)
111
+ info.contextWindow = model.contextWindow;
112
+ out.push(info);
113
+ }
114
+ }
115
+ return out;
116
+ }
117
+ function commands(runtime) {
118
+ return [
119
+ ...BUILTIN_COMMANDS.map((c) => ({
120
+ name: c.name,
121
+ description: c.description,
122
+ source: "builtin",
123
+ })),
124
+ ...runtime.resources.prompts.map((p) => ({ name: p.name, source: "template" })),
125
+ ...runtime.resources.skills.map((s) => ({
126
+ name: `skill:${s.name}`,
127
+ description: s.description,
128
+ source: "skill",
129
+ })),
130
+ ];
131
+ }
132
+ export const handlers = {
133
+ prompt: (p, ctx) => startRun(ctx, () => ctx.session().prompt(p.message, {
134
+ ...(p.images !== undefined ? { images: p.images } : {}),
135
+ ...(p.streamingBehavior !== undefined ? { streamingBehavior: p.streamingBehavior } : {}),
136
+ })),
137
+ steer: (p, ctx) => startRun(ctx, () => ctx.session().steer(p.message)),
138
+ follow_up: (p, ctx) => startRun(ctx, () => ctx.session().followUp(p.message)),
139
+ abort: async (_p, ctx) => {
140
+ await ctx.session().abort();
141
+ return {};
142
+ },
143
+ clear_queue: async (_p, ctx) => ctx.session().clearQueue(),
144
+ get_state: async (_p, ctx) => ctx.session().state,
145
+ get_messages: async (_p, ctx) => ({ messages: ctx.session().messages }),
146
+ get_last_assistant_text: async (_p, ctx) => ({ text: ctx.session().getLastAssistantText() }),
147
+ // [W3-C2] 含 `cache`(三态、最近 / 会话命中率、未命中、保温、余量;第三波 §1.10)。
148
+ get_session_stats: async (_p, ctx) => ctx.session().getStats(),
149
+ set_model: async (p, ctx) => {
150
+ await ctx.session().setModel(formatModelRef({
151
+ provider: p.provider,
152
+ id: p.modelId,
153
+ ...(p.channel !== undefined ? { channel: p.channel } : {}),
154
+ }));
155
+ return { model: ctx.session().state.model };
156
+ },
157
+ get_available_models: async (_p, ctx) => ({ models: await availableModels(ctx.runtime) }),
158
+ set_thinking_level: async (p, ctx) => {
159
+ ctx.session().setThinkingLevel(p.level);
160
+ return { level: p.level };
161
+ },
162
+ get_available_thinking_levels: async (_p, ctx) => {
163
+ const ref = ctx.session().state.model;
164
+ const found = ref === undefined ? undefined : ctx.runtime.providers.findModel(formatModelRef(ref));
165
+ return { levels: found?.ok === true ? getSupportedLevels(found.model) : ["off"] };
166
+ },
167
+ set_steering_mode: async (p, ctx) => {
168
+ impl(ctx.session()).agent.steeringQueue.mode = p.mode;
169
+ return { mode: p.mode };
170
+ },
171
+ set_follow_up_mode: async (p, ctx) => {
172
+ impl(ctx.session()).agent.followUpQueue.mode = p.mode;
173
+ return { mode: p.mode };
174
+ },
175
+ compact: async (p, ctx) => ctx.session().compact(p.customInstructions),
176
+ set_auto_compaction: async (p, ctx) => {
177
+ impl(ctx.session()).setAutoCompaction(p.enabled);
178
+ return { enabled: p.enabled };
179
+ },
180
+ set_auto_retry: async (p, ctx) => {
181
+ impl(ctx.session()).setAutoRetry(p.enabled);
182
+ return { enabled: p.enabled };
183
+ },
184
+ abort_retry: async (_p, ctx) => {
185
+ const session = ctx.session();
186
+ const retrying = session.state.isRetrying;
187
+ if (retrying)
188
+ await session.abort();
189
+ return { aborted: retrying };
190
+ },
191
+ new_session: (_p, ctx) => switched(ctx, switchSession(ctx.runtime, { kind: "new" }), "new"),
192
+ switch_session: (p, ctx) => switched(ctx, switchSession(ctx.runtime, { kind: "open", file: p.sessionPath }), "resume"),
193
+ fork: (p, ctx) => switched(ctx, switchSession(ctx.runtime, { kind: "fork", entryId: p.entryId }), "fork"),
194
+ get_entries: async (p, ctx) => impl(ctx.session()).manager.getEntries(p.since),
195
+ get_tree: async (_p, ctx) => ({ tree: impl(ctx.session()).manager.getTree() }),
196
+ set_session_name: async (p, ctx) => {
197
+ impl(ctx.session()).manager.setName(p.name);
198
+ return { name: p.name };
199
+ },
200
+ get_fork_messages: async (_p, ctx) => ({
201
+ messages: impl(ctx.session())
202
+ .manager.branch()
203
+ .flatMap((entry) => entry.type === "message" && entry.message.role === "user"
204
+ ? [
205
+ {
206
+ entryId: entry.id,
207
+ text: typeof entry.message.content === "string"
208
+ ? entry.message.content
209
+ : entry.message.content.map((b) => (b.type === "text" ? b.text : "")).join(""),
210
+ },
211
+ ]
212
+ : []),
213
+ }),
214
+ set_client_capabilities: async (p, ctx) => {
215
+ ctx.capabilities = new Set(p.capabilities);
216
+ const approvals = ctx.capabilities.has("approvals");
217
+ ctx.runtime.approvals.setUiBroker(approvals ? ctx.approvals : undefined);
218
+ if (!approvals)
219
+ ctx.approvals.cancelAll();
220
+ return { capabilities: [...ctx.capabilities] };
221
+ },
222
+ permission_response: async (p, ctx) => ({
223
+ accepted: ctx.approvals.respond(p.requestId, p.decision),
224
+ }),
225
+ get_tools: async (_p, ctx) => {
226
+ const active = new Set(ctx
227
+ .session()
228
+ .getTools()
229
+ .map((t) => t.name));
230
+ return {
231
+ tools: ctx.runtime.tools.list().flatMap((name) => {
232
+ const tool = ctx.runtime.tools.get(name);
233
+ return tool === undefined
234
+ ? []
235
+ : [
236
+ {
237
+ name,
238
+ description: tool.description,
239
+ parameters: tool.parameters,
240
+ permission: tool.permission,
241
+ active: active.has(name),
242
+ },
243
+ ];
244
+ }),
245
+ };
246
+ },
247
+ set_active_tools: async (p, ctx) => {
248
+ ctx.session().setActiveTools(p.names);
249
+ return {
250
+ names: ctx
251
+ .session()
252
+ .getTools()
253
+ .map((t) => t.name),
254
+ };
255
+ },
256
+ set_permission_mode: async (p, ctx) => {
257
+ ctx.session().setPermissionMode(p.mode);
258
+ return { mode: p.mode };
259
+ },
260
+ get_commands: async (_p, ctx) => ({ commands: commands(ctx.runtime) }),
261
+ get_skills: async (_p, ctx) => ({ skills: ctx.runtime.resources.skills }),
262
+ };
263
+ export const RPC_COMMAND_TYPES = Object.keys(handlers);
@@ -0,0 +1,16 @@
1
+ /**
2
+ * JSONL 读写(设计 §13.2)。[B6]
3
+ *
4
+ * - 读:只按 `\n` 切行(不用 readline——它会在 U+2028 / U+2029 等处切),去掉行尾 `\r`,空行跳过;
5
+ * 多字节 UTF-8 跨 chunk 用 StringDecoder 拼接;流结束时把最后一段不带换行的行也交出去。
6
+ * - 写:一行拆成 ≤ 64 KiB 的片依次写入,遇到背压等 `drain`,保证大行(图片、长输出)不被截断、
7
+ * 行与行之间不交错(调用方串行 await)。
8
+ */
9
+ export declare const WRITE_CHUNK_BYTES: number;
10
+ export interface LineReader {
11
+ /** 停止监听(不关闭流)。 */
12
+ close(): void;
13
+ }
14
+ export declare function createLineReader(stream: NodeJS.ReadableStream, onLine: (line: string) => void, onEnd?: () => void): LineReader;
15
+ /** 按 64 KiB 分片写入(背压时等 drain)。 */
16
+ export declare function writeChunked(stream: NodeJS.WritableStream, text: string, chunkBytes?: number): Promise<void>;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * JSONL 读写(设计 §13.2)。[B6]
3
+ *
4
+ * - 读:只按 `\n` 切行(不用 readline——它会在 U+2028 / U+2029 等处切),去掉行尾 `\r`,空行跳过;
5
+ * 多字节 UTF-8 跨 chunk 用 StringDecoder 拼接;流结束时把最后一段不带换行的行也交出去。
6
+ * - 写:一行拆成 ≤ 64 KiB 的片依次写入,遇到背压等 `drain`,保证大行(图片、长输出)不被截断、
7
+ * 行与行之间不交错(调用方串行 await)。
8
+ */
9
+ import { StringDecoder } from "node:string_decoder";
10
+ export const WRITE_CHUNK_BYTES = 64 * 1024;
11
+ export function createLineReader(stream, onLine, onEnd) {
12
+ const decoder = new StringDecoder("utf8");
13
+ let buffer = "";
14
+ const emit = (raw) => {
15
+ const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
16
+ if (line.trim() !== "")
17
+ onLine(line);
18
+ };
19
+ const onData = (chunk) => {
20
+ buffer += typeof chunk === "string" ? chunk : decoder.write(chunk);
21
+ let at = buffer.indexOf("\n");
22
+ while (at >= 0) {
23
+ emit(buffer.slice(0, at));
24
+ buffer = buffer.slice(at + 1);
25
+ at = buffer.indexOf("\n");
26
+ }
27
+ };
28
+ const finish = () => {
29
+ buffer += decoder.end();
30
+ if (buffer !== "")
31
+ emit(buffer);
32
+ buffer = "";
33
+ detach();
34
+ onEnd?.();
35
+ };
36
+ const detach = () => {
37
+ stream.off("data", onData);
38
+ stream.off("end", finish);
39
+ stream.off("close", finish);
40
+ };
41
+ stream.on("data", onData);
42
+ stream.once("end", finish);
43
+ stream.once("close", finish);
44
+ return { close: detach };
45
+ }
46
+ /** 按 64 KiB 分片写入(背压时等 drain)。 */
47
+ export async function writeChunked(stream, text, chunkBytes = WRITE_CHUNK_BYTES) {
48
+ const bytes = Buffer.from(text, "utf8");
49
+ for (let offset = 0; offset < bytes.length; offset += chunkBytes) {
50
+ const piece = bytes.subarray(offset, Math.min(bytes.length, offset + chunkBytes));
51
+ if (!stream.write(piece)) {
52
+ await new Promise((resolve) => stream.once("drain", resolve));
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * RPC 模式(`ama --mode rpc`,设计 §13.2):stdin 读命令、stdout 写响应与事件,每行一个 JSON。[B6]
3
+ *
4
+ * - 启动先发 `hello{protocolVersion, agent, version, capabilities}`,再发当前会话的 session_start。
5
+ * - 命令并发处理(prompt 不阻塞后续命令),响应带回请求的 `id`;解析失败 → `command: "parse"`。
6
+ * - 审批:客户端 `set_client_capabilities{capabilities:["approvals"]}` 之后才挂 UI broker;
7
+ * 之前的 ask 无人作答 → deny。`permission_request` 带 `timeoutMs`,超时由会话 deny 并发
8
+ * `permission_resolved`。
9
+ * - 宿主 `ui.notify` → `{type:"notification", level, message}` 一行(stderr 同时一份)。
10
+ * - stdin 关闭:撤下审批(之后的 ask 无人作答 → deny)、等在途命令与已开始的运行结束、
11
+ * 应答写完 → 退出 0(会话由 runCli dispose)。`printf '{…prompt…}' | ama --mode rpc` 因此能拿到
12
+ * 完整回复;要中断先发 `abort`。SIGINT / SIGTERM:abort 后有序退出,退出码 130 / 143。
13
+ */
14
+ import type { ModeContext } from "../../cli/deps.js";
15
+ import type { Runtime } from "../../cli/runtime.js";
16
+ import { type RpcHello } from "../../rpc.js";
17
+ export interface RpcModeOptions {
18
+ stdin?: NodeJS.ReadableStream;
19
+ stdout?: NodeJS.WritableStream;
20
+ }
21
+ export declare const RPC_CAPABILITIES: RpcHello["capabilities"];
22
+ export declare function runRpcMode(runtime: Runtime, context: ModeContext, options?: RpcModeOptions): Promise<number>;
@@ -0,0 +1,153 @@
1
+ /**
2
+ * RPC 模式(`ama --mode rpc`,设计 §13.2):stdin 读命令、stdout 写响应与事件,每行一个 JSON。[B6]
3
+ *
4
+ * - 启动先发 `hello{protocolVersion, agent, version, capabilities}`,再发当前会话的 session_start。
5
+ * - 命令并发处理(prompt 不阻塞后续命令),响应带回请求的 `id`;解析失败 → `command: "parse"`。
6
+ * - 审批:客户端 `set_client_capabilities{capabilities:["approvals"]}` 之后才挂 UI broker;
7
+ * 之前的 ask 无人作答 → deny。`permission_request` 带 `timeoutMs`,超时由会话 deny 并发
8
+ * `permission_resolved`。
9
+ * - 宿主 `ui.notify` → `{type:"notification", level, message}` 一行(stderr 同时一份)。
10
+ * - stdin 关闭:撤下审批(之后的 ask 无人作答 → deny)、等在途命令与已开始的运行结束、
11
+ * 应答写完 → 退出 0(会话由 runCli dispose)。`printf '{…prompt…}' | ama --mode rpc` 因此能拿到
12
+ * 完整回复;要中断先发 `abort`。SIGINT / SIGTERM:abort 后有序退出,退出码 130 / 143。
13
+ */
14
+ import { AgentSessionImpl } from "../../agent/session.js";
15
+ import { currentSession } from "../../cli/compose-session.js";
16
+ import { ExitCode } from "../../cli/exit-codes.js";
17
+ import { isAmaError } from "../../errors.js";
18
+ import { RPC_PROTOCOL_VERSION } from "../../rpc.js";
19
+ import { AMA_VERSION } from "../../version.js";
20
+ import { toJsonLine, toWireEvent } from "../print/json-event.js";
21
+ import { errorText, onTerminationSignals } from "../shared.js";
22
+ import { RpcApprovals, handlers } from "./commands.js";
23
+ import { createLineReader, writeChunked } from "./jsonl.js";
24
+ export const RPC_CAPABILITIES = ["approvals", "images", "hooks"];
25
+ export async function runRpcMode(runtime, context, options = {}) {
26
+ const stdin = options.stdin ?? process.stdin;
27
+ const stdout = options.stdout ?? process.stdout;
28
+ let writing = Promise.resolve();
29
+ const write = (value) => {
30
+ const line = `${toJsonLine(value)}\n`;
31
+ writing = writing.then(() => writeChunked(stdout, line)).catch(() => undefined);
32
+ return writing;
33
+ };
34
+ const subscribe = (s) => s.subscribe((event) => void write(toWireEvent(event)));
35
+ let session = currentSession(runtime);
36
+ let unsubscribe = subscribe(session);
37
+ const approvals = new RpcApprovals();
38
+ const ctx = {
39
+ runtime,
40
+ session: () => session,
41
+ capabilities: new Set(),
42
+ approvals,
43
+ onSessionChanged(next) {
44
+ unsubscribe();
45
+ session = next;
46
+ unsubscribe = subscribe(next);
47
+ },
48
+ onBackgroundError(error) {
49
+ void write({ type: "notification", level: "error", message: errorText(error) });
50
+ },
51
+ };
52
+ runtime.notifier.set((message, level) => {
53
+ void write({ type: "notification", level, message });
54
+ context.io.stderr(`ama: [${level}] ${message}\n`);
55
+ });
56
+ const hello = {
57
+ type: "hello",
58
+ protocolVersion: RPC_PROTOCOL_VERSION,
59
+ agent: "ama",
60
+ version: AMA_VERSION,
61
+ capabilities: [...RPC_CAPABILITIES],
62
+ };
63
+ void write(hello);
64
+ if (session instanceof AgentSessionImpl)
65
+ session.announceStart("startup");
66
+ const respond = async (command) => {
67
+ const id = typeof command.id === "string" ? { id: command.id } : {};
68
+ const type = command.type;
69
+ const handler = handlers[type];
70
+ if (handler === undefined) {
71
+ await write({
72
+ ...id,
73
+ type: "response",
74
+ command: type,
75
+ success: false,
76
+ error: `未知命令:${String(type)}`,
77
+ code: "invalid_arguments",
78
+ });
79
+ return;
80
+ }
81
+ try {
82
+ const data = await handler(command, ctx);
83
+ await write({
84
+ ...id,
85
+ type: "response",
86
+ command: type,
87
+ success: true,
88
+ ...(data !== undefined ? { data } : {}),
89
+ });
90
+ }
91
+ catch (error) {
92
+ await write({
93
+ ...id,
94
+ type: "response",
95
+ command: type,
96
+ success: false,
97
+ error: errorText(error),
98
+ ...(isAmaError(error) ? { code: error.code } : {}),
99
+ });
100
+ }
101
+ };
102
+ const inflight = new Set();
103
+ const onLine = (line) => {
104
+ let command;
105
+ try {
106
+ command = JSON.parse(line);
107
+ }
108
+ catch (error) {
109
+ void write({
110
+ type: "response",
111
+ command: "parse",
112
+ success: false,
113
+ error: `JSON 解析失败:${errorText(error)}`,
114
+ });
115
+ return;
116
+ }
117
+ if (typeof command !== "object" || command === null || typeof command.type !== "string") {
118
+ void write({ type: "response", command: "parse", success: false, error: "缺少 type" });
119
+ return;
120
+ }
121
+ const task = respond(command).finally(() => inflight.delete(task));
122
+ inflight.add(task);
123
+ };
124
+ return new Promise((resolve) => {
125
+ let finished = false;
126
+ const finish = async (code, abort) => {
127
+ if (finished)
128
+ return;
129
+ finished = true;
130
+ reader.close();
131
+ offSignals();
132
+ stdin.pause?.();
133
+ runtime.approvals.setUiBroker(undefined);
134
+ approvals.cancelAll();
135
+ if (abort)
136
+ await session.abort().catch(() => undefined);
137
+ await Promise.allSettled([...inflight]);
138
+ await session.waitForIdle().catch(() => undefined);
139
+ unsubscribe();
140
+ runtime.notifier.set(undefined);
141
+ await writing;
142
+ resolve(code);
143
+ };
144
+ // stdin 结束:不再有新命令,也没人能回答审批;已开始的运行跑完再退出(要中断先发 abort 或发信号)。
145
+ const reader = createLineReader(stdin, onLine, () => void finish(ExitCode.Ok, false));
146
+ const offSignals = onTerminationSignals((code) => {
147
+ if (finished)
148
+ void session.abort();
149
+ else
150
+ void finish(code, true);
151
+ });
152
+ });
153
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * 会话报告与缓存提示文本(第三波 §1.10)。[W3-C2]
3
+ *
4
+ * line 模式、交互模式与 RPC 共用的纯文本层,不含渲染:
5
+ * - `/session`:会话、模型、消息、用量、上下文 + 「缓存」段(`describeSession`);
6
+ * - `/cache`:只有缓存段(`describeCache`);`/cache fingerprint`:最近一次真实请求的前缀指纹;
7
+ * - 消息区 / stderr 的一行提示:缓存未命中(只提示 ≥ 20k token 或 ≥ $0.10 的那次)、
8
+ * 上下文跨越 70% / 90%(`cache.missNotices` 为 false 时都不提示,统计不受影响)。
9
+ *
10
+ * 两列对齐用 B9a-2 的 `KeyValue`(按足够宽的宽度渲染再去行尾空白,结果是纯文本)。
11
+ */
12
+ import type { AgentSession, SessionEvent } from "../agent/types.js";
13
+ import type { CacheMiss, CacheReporting, WarmerStatus } from "../ai/cache/types.js";
14
+ import { type KeyValueRow } from "../tui/components/key-value.js";
15
+ /** `950`、`38.2k`、`150k`、`1.05M`(比状态栏多一位精度,面板与提示用)。 */
16
+ export declare function formatTokenCount(count: number): string;
17
+ export declare function formatUsd(cost: number | undefined): string;
18
+ export declare function formatPercent(rate: number | undefined): string;
19
+ /** `45s`、`2m 10s`、`7 分钟`(≥ 10 分钟只到分钟)。 */
20
+ export declare function formatDuration(ms: number): string;
21
+ export declare function reportingLabel(reporting: CacheReporting): string;
22
+ /** 未命中原因的短语(提示行括号里那段)。 */
23
+ export declare function missReasonText(miss: Pick<CacheMiss, "reason" | "detail" | "idleMs">): string;
24
+ export declare function shouldNotifyMiss(miss: Pick<CacheMiss, "missedTokens" | "missedCost">): boolean;
25
+ /** `缓存未命中(空闲 7 分钟后):重计费 38.2k token(约 $0.11)`。 */
26
+ export declare function cacheMissNotice(miss: CacheMiss): string;
27
+ /** `上下文已用 72%,约剩 9 回合(按最近 5 回合均值)`;估不出回合时给余量 token。 */
28
+ export declare function contextPressureNotice(event: Extract<SessionEvent, {
29
+ type: "context_pressure";
30
+ }>): string;
31
+ /**
32
+ * 缓存事件 → 消息区 / stderr 的一行提示;不提示时 undefined。`enabled` = `cache.missNotices`。
33
+ * 未命中只提示超过门槛的那次;70% 为 warn、90% 为 error;保温事件不进消息区(状态栏 ♨)。
34
+ */
35
+ export declare function cacheEventNotice(event: SessionEvent, enabled: boolean): {
36
+ level: "warn" | "error";
37
+ text: string;
38
+ } | undefined;
39
+ /** 保温请求发出后的一行(line 模式只在 `AMA_LOG=info|debug` 时写 stderr)。 */
40
+ export declare function warmSentNotice(event: Extract<SessionEvent, {
41
+ type: "cache_warm";
42
+ }>): string | undefined;
43
+ /** 消息区 / stderr 是否提示未命中与上下文余量(`cache.missNotices`,缺省 true)。 */
44
+ export declare function cacheNoticesEnabled(session: AgentSession): boolean;
45
+ export declare function warmStopText(reason: string | undefined): string;
46
+ /** `streaming · 下次 2m 10s · 期望节省 $0.18 ≥ $0.05 · 已发 2 次 $0.01`。 */
47
+ export declare function warmingText(status: WarmerStatus, now: number, minSavingsUsd: number | undefined): string;
48
+ /** 缓存段的键值行(`/session` 与 `/cache` 共用)。 */
49
+ export declare function cacheRows(session: AgentSession, now?: number): KeyValueRow[];
50
+ /** 键值行 → 纯文本(键列对齐、去行尾空白)。 */
51
+ export declare function renderRows(rows: readonly KeyValueRow[], indent?: string): string[];
52
+ export declare function describeCache(session: AgentSession, now?: number): string;
53
+ export declare function describeSession(session: AgentSession, now?: number): string;
54
+ /** `/cache fingerprint`:最近一次真实请求的前缀指纹(system / tools 哈希与模型)。 */
55
+ export declare function describeFingerprint(session: AgentSession): string;