@armadra/agent 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (407) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/LICENSE +21 -0
  3. package/README.md +426 -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 +511 -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 +118 -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 +104 -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 +484 -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 +303 -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 +102 -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/registry.d.ts +83 -0
  101. package/dist/ai/providers/registry.js +305 -0
  102. package/dist/ai/sse.d.ts +32 -0
  103. package/dist/ai/sse.js +140 -0
  104. package/dist/ai/thinking.d.ts +42 -0
  105. package/dist/ai/thinking.js +101 -0
  106. package/dist/ai/types.d.ts +428 -0
  107. package/dist/ai/types.js +19 -0
  108. package/dist/bundle/ama-sandbox.cjs +324 -0
  109. package/dist/bundle/ama.cjs +25770 -0
  110. package/dist/bundle.d.ts +9 -0
  111. package/dist/bundle.js +15 -0
  112. package/dist/cli/args.d.ts +82 -0
  113. package/dist/cli/args.js +381 -0
  114. package/dist/cli/bootstrap.d.ts +18 -0
  115. package/dist/cli/bootstrap.js +398 -0
  116. package/dist/cli/compose-providers.d.ts +37 -0
  117. package/dist/cli/compose-providers.js +104 -0
  118. package/dist/cli/compose-session.d.ts +69 -0
  119. package/dist/cli/compose-session.js +370 -0
  120. package/dist/cli/compose-store.d.ts +24 -0
  121. package/dist/cli/compose-store.js +125 -0
  122. package/dist/cli/compose.d.ts +106 -0
  123. package/dist/cli/compose.js +206 -0
  124. package/dist/cli/default-model.d.ts +16 -0
  125. package/dist/cli/default-model.js +24 -0
  126. package/dist/cli/deps.d.ts +205 -0
  127. package/dist/cli/deps.js +12 -0
  128. package/dist/cli/exit-codes.d.ts +29 -0
  129. package/dist/cli/exit-codes.js +40 -0
  130. package/dist/cli/main.d.ts +26 -0
  131. package/dist/cli/main.js +165 -0
  132. package/dist/cli/runtime.d.ts +90 -0
  133. package/dist/cli/runtime.js +8 -0
  134. package/dist/cli/startup-screen.d.ts +15 -0
  135. package/dist/cli/startup-screen.js +56 -0
  136. package/dist/cli/startup-steps.d.ts +35 -0
  137. package/dist/cli/startup-steps.js +197 -0
  138. package/dist/cli/subcommands/auth.d.ts +12 -0
  139. package/dist/cli/subcommands/auth.js +91 -0
  140. package/dist/cli/subcommands/config.d.ts +44 -0
  141. package/dist/cli/subcommands/config.js +201 -0
  142. package/dist/cli/subcommands/context.d.ts +21 -0
  143. package/dist/cli/subcommands/context.js +41 -0
  144. package/dist/cli/subcommands/doctor.d.ts +11 -0
  145. package/dist/cli/subcommands/doctor.js +246 -0
  146. package/dist/cli/subcommands/models-cache-probe.d.ts +34 -0
  147. package/dist/cli/subcommands/models-cache-probe.js +190 -0
  148. package/dist/cli/subcommands/models-discover.d.ts +40 -0
  149. package/dist/cli/subcommands/models-discover.js +204 -0
  150. package/dist/cli/subcommands/models.d.ts +40 -0
  151. package/dist/cli/subcommands/models.js +144 -0
  152. package/dist/cli/subcommands/sessions.d.ts +11 -0
  153. package/dist/cli/subcommands/sessions.js +100 -0
  154. package/dist/codemode/capability.d.ts +37 -0
  155. package/dist/codemode/capability.js +55 -0
  156. package/dist/codemode/declarations.d.ts +44 -0
  157. package/dist/codemode/declarations.js +111 -0
  158. package/dist/codemode/host-side.d.ts +59 -0
  159. package/dist/codemode/host-side.js +253 -0
  160. package/dist/codemode/modes.d.ts +23 -0
  161. package/dist/codemode/modes.js +40 -0
  162. package/dist/codemode/protocol.d.ts +91 -0
  163. package/dist/codemode/protocol.js +114 -0
  164. package/dist/codemode/sandbox-entry.d.ts +25 -0
  165. package/dist/codemode/sandbox-entry.js +323 -0
  166. package/dist/codemode/store.d.ts +26 -0
  167. package/dist/codemode/store.js +53 -0
  168. package/dist/codemode/tool.d.ts +105 -0
  169. package/dist/codemode/tool.js +283 -0
  170. package/dist/compaction/branch-summary.d.ts +30 -0
  171. package/dist/compaction/branch-summary.js +79 -0
  172. package/dist/compaction/breaker.d.ts +32 -0
  173. package/dist/compaction/breaker.js +65 -0
  174. package/dist/compaction/cut-point.d.ts +23 -0
  175. package/dist/compaction/cut-point.js +70 -0
  176. package/dist/compaction/estimate.d.ts +30 -0
  177. package/dist/compaction/estimate.js +114 -0
  178. package/dist/compaction/prune-tier.d.ts +31 -0
  179. package/dist/compaction/prune-tier.js +83 -0
  180. package/dist/compaction/serialize.d.ts +20 -0
  181. package/dist/compaction/serialize.js +115 -0
  182. package/dist/compaction/summarize-tier.d.ts +111 -0
  183. package/dist/compaction/summarize-tier.js +305 -0
  184. package/dist/config/auth-file.d.ts +39 -0
  185. package/dist/config/auth-file.js +92 -0
  186. package/dist/config/context-files.d.ts +30 -0
  187. package/dist/config/context-files.js +77 -0
  188. package/dist/config/load.d.ts +45 -0
  189. package/dist/config/load.js +97 -0
  190. package/dist/config/merge.d.ts +73 -0
  191. package/dist/config/merge.js +248 -0
  192. package/dist/config/paths.d.ts +38 -0
  193. package/dist/config/paths.js +108 -0
  194. package/dist/config/profile.d.ts +27 -0
  195. package/dist/config/profile.js +65 -0
  196. package/dist/config/schema.d.ts +34 -0
  197. package/dist/config/schema.js +413 -0
  198. package/dist/config/trust.d.ts +42 -0
  199. package/dist/config/trust.js +122 -0
  200. package/dist/config/types.d.ts +172 -0
  201. package/dist/config/types.js +27 -0
  202. package/dist/config/write.d.ts +10 -0
  203. package/dist/config/write.js +25 -0
  204. package/dist/errors.d.ts +28 -0
  205. package/dist/errors.js +30 -0
  206. package/dist/hooks/config.d.ts +49 -0
  207. package/dist/hooks/config.js +102 -0
  208. package/dist/hooks/dispatcher.d.ts +41 -0
  209. package/dist/hooks/dispatcher.js +104 -0
  210. package/dist/hooks/matcher.d.ts +22 -0
  211. package/dist/hooks/matcher.js +108 -0
  212. package/dist/hooks/protocol.d.ts +33 -0
  213. package/dist/hooks/protocol.js +240 -0
  214. package/dist/hooks/runner.d.ts +30 -0
  215. package/dist/hooks/runner.js +143 -0
  216. package/dist/hooks/types.d.ts +147 -0
  217. package/dist/hooks/types.js +20 -0
  218. package/dist/host/api-impl.d.ts +75 -0
  219. package/dist/host/api-impl.js +173 -0
  220. package/dist/host/loader.d.ts +31 -0
  221. package/dist/host/loader.js +147 -0
  222. package/dist/host/types.d.ts +169 -0
  223. package/dist/host/types.js +14 -0
  224. package/dist/host.d.ts +5 -0
  225. package/dist/host.js +4 -0
  226. package/dist/index.d.ts +41 -0
  227. package/dist/index.js +28 -0
  228. package/dist/modes/commands-core.d.ts +44 -0
  229. package/dist/modes/commands-core.js +171 -0
  230. package/dist/modes/interactive/approval-dialog.d.ts +48 -0
  231. package/dist/modes/interactive/approval-dialog.js +202 -0
  232. package/dist/modes/interactive/commands.d.ts +41 -0
  233. package/dist/modes/interactive/commands.js +202 -0
  234. package/dist/modes/interactive/completion.d.ts +53 -0
  235. package/dist/modes/interactive/completion.js +157 -0
  236. package/dist/modes/interactive/interactive-mode.d.ts +46 -0
  237. package/dist/modes/interactive/interactive-mode.js +487 -0
  238. package/dist/modes/interactive/key-dispatch.d.ts +29 -0
  239. package/dist/modes/interactive/key-dispatch.js +107 -0
  240. package/dist/modes/interactive/line/line-editor.d.ts +50 -0
  241. package/dist/modes/interactive/line/line-editor.js +232 -0
  242. package/dist/modes/interactive/line/line-mode.d.ts +20 -0
  243. package/dist/modes/interactive/line/line-mode.js +216 -0
  244. package/dist/modes/interactive/line/line-render.d.ts +35 -0
  245. package/dist/modes/interactive/line/line-render.js +171 -0
  246. package/dist/modes/interactive/line/paste-state.d.ts +26 -0
  247. package/dist/modes/interactive/line/paste-state.js +72 -0
  248. package/dist/modes/interactive/message-view.d.ts +77 -0
  249. package/dist/modes/interactive/message-view.js +269 -0
  250. package/dist/modes/interactive/pickers.d.ts +42 -0
  251. package/dist/modes/interactive/pickers.js +115 -0
  252. package/dist/modes/interactive/startup-ui.d.ts +28 -0
  253. package/dist/modes/interactive/startup-ui.js +238 -0
  254. package/dist/modes/interactive/status-bar.d.ts +48 -0
  255. package/dist/modes/interactive/status-bar.js +139 -0
  256. package/dist/modes/interactive/tool-view.d.ts +95 -0
  257. package/dist/modes/interactive/tool-view.js +344 -0
  258. package/dist/modes/print/json-event.d.ts +15 -0
  259. package/dist/modes/print/json-event.js +39 -0
  260. package/dist/modes/print/print-mode.d.ts +15 -0
  261. package/dist/modes/print/print-mode.js +96 -0
  262. package/dist/modes/rpc/commands.d.ts +40 -0
  263. package/dist/modes/rpc/commands.js +258 -0
  264. package/dist/modes/rpc/jsonl.d.ts +16 -0
  265. package/dist/modes/rpc/jsonl.js +55 -0
  266. package/dist/modes/rpc/rpc-mode.d.ts +22 -0
  267. package/dist/modes/rpc/rpc-mode.js +153 -0
  268. package/dist/modes/session-report.d.ts +55 -0
  269. package/dist/modes/session-report.js +278 -0
  270. package/dist/modes/shared.d.ts +13 -0
  271. package/dist/modes/shared.js +52 -0
  272. package/dist/modes/startup-ui-text.d.ts +16 -0
  273. package/dist/modes/startup-ui-text.js +103 -0
  274. package/dist/permissions/broker.d.ts +47 -0
  275. package/dist/permissions/broker.js +107 -0
  276. package/dist/permissions/dangerous.d.ts +47 -0
  277. package/dist/permissions/dangerous.js +370 -0
  278. package/dist/permissions/pipeline.d.ts +51 -0
  279. package/dist/permissions/pipeline.js +181 -0
  280. package/dist/permissions/preview.d.ts +43 -0
  281. package/dist/permissions/preview.js +463 -0
  282. package/dist/permissions/rules.d.ts +58 -0
  283. package/dist/permissions/rules.js +249 -0
  284. package/dist/permissions/types.d.ts +100 -0
  285. package/dist/permissions/types.js +18 -0
  286. package/dist/rpc.d.ts +171 -0
  287. package/dist/rpc.js +13 -0
  288. package/dist/sdk.d.ts +98 -0
  289. package/dist/sdk.js +195 -0
  290. package/dist/session/manager.d.ts +70 -0
  291. package/dist/session/manager.js +278 -0
  292. package/dist/session/migrate.d.ts +19 -0
  293. package/dist/session/migrate.js +53 -0
  294. package/dist/session/projection.d.ts +50 -0
  295. package/dist/session/projection.js +180 -0
  296. package/dist/session/store.d.ts +47 -0
  297. package/dist/session/store.js +210 -0
  298. package/dist/session/tree.d.ts +21 -0
  299. package/dist/session/tree.js +88 -0
  300. package/dist/session/types.d.ts +199 -0
  301. package/dist/session/types.js +18 -0
  302. package/dist/skills/discover.d.ts +60 -0
  303. package/dist/skills/discover.js +160 -0
  304. package/dist/skills/expand.d.ts +35 -0
  305. package/dist/skills/expand.js +46 -0
  306. package/dist/skills/frontmatter.d.ts +27 -0
  307. package/dist/skills/frontmatter.js +157 -0
  308. package/dist/skills/index-prompt.d.ts +13 -0
  309. package/dist/skills/index-prompt.js +42 -0
  310. package/dist/skills/templates.d.ts +52 -0
  311. package/dist/skills/templates.js +177 -0
  312. package/dist/tools/bash.d.ts +49 -0
  313. package/dist/tools/bash.js +222 -0
  314. package/dist/tools/edit-fuzzy.d.ts +36 -0
  315. package/dist/tools/edit-fuzzy.js +129 -0
  316. package/dist/tools/edit.d.ts +38 -0
  317. package/dist/tools/edit.js +241 -0
  318. package/dist/tools/file-mutex.d.ts +8 -0
  319. package/dist/tools/file-mutex.js +29 -0
  320. package/dist/tools/glob.d.ts +44 -0
  321. package/dist/tools/glob.js +217 -0
  322. package/dist/tools/grep.d.ts +39 -0
  323. package/dist/tools/grep.js +179 -0
  324. package/dist/tools/ignore.d.ts +40 -0
  325. package/dist/tools/ignore.js +174 -0
  326. package/dist/tools/ls.d.ts +21 -0
  327. package/dist/tools/ls.js +96 -0
  328. package/dist/tools/output-accumulator.d.ts +48 -0
  329. package/dist/tools/output-accumulator.js +106 -0
  330. package/dist/tools/paths.d.ts +20 -0
  331. package/dist/tools/paths.js +58 -0
  332. package/dist/tools/presets.d.ts +71 -0
  333. package/dist/tools/presets.js +128 -0
  334. package/dist/tools/process-tree.d.ts +35 -0
  335. package/dist/tools/process-tree.js +125 -0
  336. package/dist/tools/read.d.ts +28 -0
  337. package/dist/tools/read.js +181 -0
  338. package/dist/tools/registry.d.ts +43 -0
  339. package/dist/tools/registry.js +106 -0
  340. package/dist/tools/shell.d.ts +38 -0
  341. package/dist/tools/shell.js +91 -0
  342. package/dist/tools/task.d.ts +29 -0
  343. package/dist/tools/task.js +107 -0
  344. package/dist/tools/todo.d.ts +28 -0
  345. package/dist/tools/todo.js +107 -0
  346. package/dist/tools/truncate.d.ts +44 -0
  347. package/dist/tools/truncate.js +175 -0
  348. package/dist/tools/types.d.ts +135 -0
  349. package/dist/tools/types.js +19 -0
  350. package/dist/tools/write.d.ts +20 -0
  351. package/dist/tools/write.js +84 -0
  352. package/dist/tui/ansi.d.ts +29 -0
  353. package/dist/tui/ansi.js +420 -0
  354. package/dist/tui/component.d.ts +38 -0
  355. package/dist/tui/component.js +13 -0
  356. package/dist/tui/components/box.d.ts +26 -0
  357. package/dist/tui/components/box.js +65 -0
  358. package/dist/tui/components/container.d.ts +14 -0
  359. package/dist/tui/components/container.js +39 -0
  360. package/dist/tui/components/editor-buffer.d.ts +67 -0
  361. package/dist/tui/components/editor-buffer.js +300 -0
  362. package/dist/tui/components/editor-paste.d.ts +29 -0
  363. package/dist/tui/components/editor-paste.js +64 -0
  364. package/dist/tui/components/editor.d.ts +114 -0
  365. package/dist/tui/components/editor.js +530 -0
  366. package/dist/tui/components/key-value.d.ts +29 -0
  367. package/dist/tui/components/key-value.js +48 -0
  368. package/dist/tui/components/loader.d.ts +35 -0
  369. package/dist/tui/components/loader.js +71 -0
  370. package/dist/tui/components/markdown.d.ts +90 -0
  371. package/dist/tui/components/markdown.js +462 -0
  372. package/dist/tui/components/meter.d.ts +29 -0
  373. package/dist/tui/components/meter.js +47 -0
  374. package/dist/tui/components/overlay.d.ts +24 -0
  375. package/dist/tui/components/overlay.js +52 -0
  376. package/dist/tui/components/select-list.d.ts +57 -0
  377. package/dist/tui/components/select-list.js +179 -0
  378. package/dist/tui/components/spacer.d.ts +11 -0
  379. package/dist/tui/components/spacer.js +16 -0
  380. package/dist/tui/components/text.d.ts +30 -0
  381. package/dist/tui/components/text.js +65 -0
  382. package/dist/tui/keybindings.d.ts +66 -0
  383. package/dist/tui/keybindings.js +120 -0
  384. package/dist/tui/keys.d.ts +38 -0
  385. package/dist/tui/keys.js +275 -0
  386. package/dist/tui/stdin-buffer.d.ts +41 -0
  387. package/dist/tui/stdin-buffer.js +206 -0
  388. package/dist/tui/terminal.d.ts +79 -0
  389. package/dist/tui/terminal.js +153 -0
  390. package/dist/tui/theme.d.ts +31 -0
  391. package/dist/tui/theme.js +199 -0
  392. package/dist/tui/tui.d.ts +103 -0
  393. package/dist/tui/tui.js +363 -0
  394. package/dist/tui/vt-screen.d.ts +46 -0
  395. package/dist/tui/vt-screen.js +251 -0
  396. package/dist/tui.d.ts +30 -0
  397. package/dist/tui.js +31 -0
  398. package/dist/version.d.ts +8 -0
  399. package/dist/version.js +24 -0
  400. package/docs/codemode.md +102 -0
  401. package/docs/hooks.md +196 -0
  402. package/docs/host-api.md +161 -0
  403. package/docs/providers.md +316 -0
  404. package/docs/rpc.md +253 -0
  405. package/docs/session-format.md +119 -0
  406. package/docs/tui.md +168 -0
  407. package/package.json +101 -4
@@ -0,0 +1,46 @@
1
+ /**
2
+ * 交互模式装配(设计 §12、实施计划 §3)。[B7]
3
+ *
4
+ * 布局(主屏模式,自上而下):消息区(启动画面 + 对话,超出屏幕的部分进终端回滚)→ 空行 →
5
+ * 排队消息 → 运行指示(Loader)→ 编辑器 → 提示行 → 状态栏。审批对话框是底部覆盖层,选择器居中。
6
+ *
7
+ * - 终端:缺省 `ProcessTerminal`;stdin / stdout 不是 TTY 或进入 raw 模式失败 → 抛
8
+ * `AmaError("terminal_init_failed")`,bootstrap 降级为 line 模式。
9
+ * - 事件:订阅当前会话的 `SessionEvent` 驱动消息区 / 工具视图 / 状态栏;`/new /resume /fork` 之后
10
+ * 重新订阅、重画消息区并 `announceStart`。
11
+ * - 晚绑定:`runtime.approvals.setUiBroker`(审批对话框)与 `runtime.notifier.set`(宿主通知进消息区),
12
+ * 退出时撤下。
13
+ * - 键位:Enter 发送(运行中 = steer),其余应用级键位见 `key-dispatch.ts`。
14
+ * - 启动画面按 `ui.quietStartup`:normal 标题 + 模型 / 信任 / 资源清单,header 只有标题,silent 不输出。
15
+ */
16
+ import type { AgentSession } from "../../agent/types.js";
17
+ import type { ModeContext } from "../../cli/deps.js";
18
+ import type { Runtime } from "../../cli/runtime.js";
19
+ import { Editor, Keybindings, TUI, type Terminal, type Theme } from "../../tui.js";
20
+ import { MessageView } from "./message-view.js";
21
+ import { StatusBar } from "./status-bar.js";
22
+ import { ToolTracker } from "./tool-view.js";
23
+ export interface InteractiveModeOptions {
24
+ /** 缺省 `ProcessTerminal`(测试注入 MemoryTerminal)。 */
25
+ terminal?: Terminal;
26
+ theme?: Theme;
27
+ keybindings?: Keybindings;
28
+ /** Loader 计时与相对时间(测试注入)。 */
29
+ now?: () => number;
30
+ /** Loader 帧间隔;测试设很大让帧固定。 */
31
+ spinnerIntervalMs?: number;
32
+ /** 输入历史文件;false 不读写(缺省 `<dataDir>/history`)。 */
33
+ historyFile?: string | false;
34
+ /** 界面就绪后回调(测试驱动用)。 */
35
+ onReady?(handle: InteractiveHandle): void;
36
+ }
37
+ export interface InteractiveHandle {
38
+ readonly tui: TUI;
39
+ readonly editor: Editor;
40
+ readonly view: MessageView;
41
+ readonly tools: ToolTracker;
42
+ readonly status: StatusBar;
43
+ session(): AgentSession;
44
+ exit(code: number): void;
45
+ }
46
+ export declare function runInteractiveMode(runtime: Runtime, context: ModeContext, options?: InteractiveModeOptions): Promise<number>;
@@ -0,0 +1,487 @@
1
+ /**
2
+ * 交互模式装配(设计 §12、实施计划 §3)。[B7]
3
+ *
4
+ * 布局(主屏模式,自上而下):消息区(启动画面 + 对话,超出屏幕的部分进终端回滚)→ 空行 →
5
+ * 排队消息 → 运行指示(Loader)→ 编辑器 → 提示行 → 状态栏。审批对话框是底部覆盖层,选择器居中。
6
+ *
7
+ * - 终端:缺省 `ProcessTerminal`;stdin / stdout 不是 TTY 或进入 raw 模式失败 → 抛
8
+ * `AmaError("terminal_init_failed")`,bootstrap 降级为 line 模式。
9
+ * - 事件:订阅当前会话的 `SessionEvent` 驱动消息区 / 工具视图 / 状态栏;`/new /resume /fork` 之后
10
+ * 重新订阅、重画消息区并 `announceStart`。
11
+ * - 晚绑定:`runtime.approvals.setUiBroker`(审批对话框)与 `runtime.notifier.set`(宿主通知进消息区),
12
+ * 退出时撤下。
13
+ * - 键位:Enter 发送(运行中 = steer),其余应用级键位见 `key-dispatch.ts`。
14
+ * - 启动画面按 `ui.quietStartup`:normal 标题 + 模型 / 信任 / 资源清单,header 只有标题,silent 不输出。
15
+ */
16
+ import { join } from "node:path";
17
+ import { AgentSessionImpl } from "../../agent/session.js";
18
+ import { currentSession, switchSession } from "../../cli/compose-session.js";
19
+ import { buildStartupScreen } from "../../cli/startup-screen.js";
20
+ import { KEYBINDINGS_FILE } from "../../config/paths.js";
21
+ import { AmaError, isAmaError } from "../../errors.js";
22
+ import { detectSandboxCapability } from "../../codemode/capability.js";
23
+ import { effectiveCodemodeMode } from "../../tools/presets.js";
24
+ import { Container, Editor, Keybindings, Loader, ProcessTerminal, Spacer, TUI, Text, createTheme, detectCapabilities, loadKeybindingsFile, truncateToWidth, } from "../../tui.js";
25
+ import { cacheEventNotice, cacheNoticesEnabled } from "../session-report.js";
26
+ import { onTerminationSignals } from "../shared.js";
27
+ import { ApprovalDialogBroker, approvalOutcomeText } from "./approval-dialog.js";
28
+ import { ALL_COMMANDS, runInteractiveCommand } from "./commands.js";
29
+ import { InteractiveCompletion } from "./completion.js";
30
+ import { createKeyDispatch } from "./key-dispatch.js";
31
+ import { MessageView } from "./message-view.js";
32
+ import { openPicker } from "./pickers.js";
33
+ import { StatusBar } from "./status-bar.js";
34
+ import { ToolTracker } from "./tool-view.js";
35
+ const HINT_MS = 2500;
36
+ const QUEUE_PREVIEW = 3;
37
+ function processTerminal() {
38
+ const stdin = process.stdin;
39
+ if (stdin.isTTY !== true || typeof stdin.setRawMode !== "function" || !process.stdout.isTTY) {
40
+ throw new AmaError("terminal_init_failed", "stdin / stdout 不是终端");
41
+ }
42
+ return new ProcessTerminal();
43
+ }
44
+ function loadKeys(runtime, warn) {
45
+ const parsed = loadKeybindingsFile(join(runtime.paths.configDir, KEYBINDINGS_FILE));
46
+ for (const w of parsed.warnings)
47
+ warn(`keybindings.json:${w}`);
48
+ return new Keybindings(parsed.overrides);
49
+ }
50
+ /** 一行提示;空时不占行。 */
51
+ class HintLine {
52
+ text = "";
53
+ setText(text) {
54
+ this.text = text;
55
+ }
56
+ render(width) {
57
+ return this.text === "" ? [] : [truncateToWidth(this.text, width)];
58
+ }
59
+ invalidate() { }
60
+ }
61
+ function oneLine(text) {
62
+ return text.replace(/\s+/g, " ").trim();
63
+ }
64
+ export function runInteractiveMode(runtime, context, options = {}) {
65
+ const terminal = options.terminal ?? processTerminal();
66
+ const ui = runtime.config.ui ?? {};
67
+ const theme = options.theme ??
68
+ createTheme(ui.theme ?? "dark", {
69
+ caps: detectCapabilities(context.io.env, context.io.stdoutIsTTY),
70
+ });
71
+ const now = options.now ?? Date.now;
72
+ const startupWarnings = [];
73
+ const keys = options.keybindings ?? loadKeys(runtime, (m) => startupWarnings.push(m));
74
+ let session = currentSession(runtime);
75
+ const tui = new TUI(terminal);
76
+ const view = new MessageView({
77
+ theme,
78
+ ...(ui.showThinking !== undefined ? { showThinking: ui.showThinking } : {}),
79
+ ...(ui.markdown !== undefined ? { markdown: ui.markdown } : {}),
80
+ });
81
+ const tools = new ToolTracker({
82
+ theme,
83
+ cwd: session.state.cwd,
84
+ getTool: (name) => session.getTools().find((tool) => tool.name === name),
85
+ });
86
+ const queueText = new Text("");
87
+ const loaderSlot = new Container();
88
+ const loader = new Loader(() => tui.requestRender(), {
89
+ theme,
90
+ message: "工作中" + theme.fg("dim", " · Esc 中断"),
91
+ now,
92
+ ...(options.spinnerIntervalMs !== undefined ? { intervalMs: options.spinnerIntervalMs } : {}),
93
+ });
94
+ const hint = new HintLine();
95
+ let sandboxStrict;
96
+ const status = new StatusBar({
97
+ session: () => session,
98
+ preset: () => runtime.config.tools?.preset ?? "default",
99
+ hostStatus: () => runtime.host?.status(),
100
+ codemode: () => {
101
+ const mode = effectiveCodemodeMode(runtime.config);
102
+ const active = session.getTools().some((tool) => tool.name === "codemode");
103
+ return mode === "off" || !active ? undefined : mode;
104
+ },
105
+ sandboxStrict: () => (sandboxStrict ??= detectSandboxCapability().strict),
106
+ }, theme);
107
+ const historyFile = options.historyFile === false
108
+ ? undefined
109
+ : (options.historyFile ?? join(runtime.paths.dataDir, "history"));
110
+ const completion = new InteractiveCompletion({
111
+ commands: () => ALL_COMMANDS,
112
+ prompts: () => runtime.resources.prompts,
113
+ skills: () => runtime.resources.skills,
114
+ cwd: () => session.state.cwd,
115
+ now,
116
+ });
117
+ const editor = new Editor({
118
+ theme,
119
+ keybindings: keys,
120
+ maxVisibleLines: 8,
121
+ autocomplete: completion,
122
+ requestRender: () => tui.requestRender(),
123
+ ...(historyFile !== undefined ? { historyFile } : {}),
124
+ onSubmit: (text) => submit(text, "enter"),
125
+ });
126
+ tui.addChild(view);
127
+ tui.addChild(new Spacer());
128
+ tui.addChild(queueText);
129
+ tui.addChild(loaderSlot);
130
+ tui.addChild(editor);
131
+ tui.addChild(hint);
132
+ tui.addChild(status);
133
+ // ---- 小工具 ---------------------------------------------------------------
134
+ let finished = false;
135
+ let running = false;
136
+ let compacting = false;
137
+ let hintTimer;
138
+ let resolveExit = () => undefined;
139
+ const render = () => tui.requestRender();
140
+ const notice = (level, text) => {
141
+ view.addNotice(level, text);
142
+ render();
143
+ };
144
+ const showHint = (text) => {
145
+ hint.setText(text === "" ? "" : theme.fg("dim", text));
146
+ if (hintTimer !== undefined)
147
+ clearTimeout(hintTimer);
148
+ hintTimer = undefined;
149
+ if (text !== "") {
150
+ hintTimer = setTimeout(() => {
151
+ hint.setText("");
152
+ render();
153
+ }, HINT_MS);
154
+ hintTimer.unref?.();
155
+ }
156
+ render();
157
+ };
158
+ const syncLoader = () => {
159
+ const active = running || compacting;
160
+ if (active && loaderSlot.children.length === 0) {
161
+ loader.setMessage((compacting && !running ? "压缩上下文" : "工作中") + theme.fg("dim", " · Esc 中断"));
162
+ loaderSlot.addChild(loader);
163
+ loader.start();
164
+ }
165
+ else if (!active && loaderSlot.children.length > 0) {
166
+ loader.stop();
167
+ loaderSlot.clear();
168
+ }
169
+ render();
170
+ };
171
+ const setQueue = (steering, followUp) => {
172
+ status.setQueue(steering.length, followUp.length);
173
+ const rows = [
174
+ ...steering.map((t) => `↳ steer ${oneLine(t)}`),
175
+ ...followUp.map((t) => `↳ followUp ${oneLine(t)}`),
176
+ ];
177
+ const shown = rows.slice(-QUEUE_PREVIEW);
178
+ if (rows.length > shown.length)
179
+ shown.unshift(`… 另 ${rows.length - shown.length} 条`);
180
+ if (rows.length > 0)
181
+ shown.push("Alt+↑ 取回 · Esc 全部回填并中断");
182
+ queueText.setText(shown.map((l) => theme.fg("dim", l)).join("\n"));
183
+ render();
184
+ };
185
+ // ---- 会话事件 -------------------------------------------------------------
186
+ const onEvent = (event) => {
187
+ switch (event.type) {
188
+ case "agent_start":
189
+ running = true;
190
+ syncLoader();
191
+ return;
192
+ case "agent_settled":
193
+ running = false;
194
+ if (event.warning !== undefined)
195
+ view.addNotice("warn", event.warning);
196
+ status.refresh();
197
+ syncLoader();
198
+ return;
199
+ case "message_start": {
200
+ const message = event.message;
201
+ if (message.role === "user")
202
+ view.addUser(message);
203
+ else if (message.role === "assistant")
204
+ view.startAssistant(message);
205
+ else if (message.role === "custom" && message.display) {
206
+ view.addNotice("info", typeof message.content === "string" ? message.content : "");
207
+ }
208
+ render();
209
+ return;
210
+ }
211
+ case "message_update":
212
+ view.updateAssistant(event.message);
213
+ render();
214
+ return;
215
+ case "message_end":
216
+ if (event.message.role === "assistant") {
217
+ view.endAssistant(event.message);
218
+ status.refresh();
219
+ }
220
+ render();
221
+ return;
222
+ case "tool_execution_start": {
223
+ const started = tools.start(event);
224
+ if (started.topLevel)
225
+ view.add(started.view);
226
+ render();
227
+ return;
228
+ }
229
+ case "tool_execution_update":
230
+ tools.update(event.toolCallId, event.partial);
231
+ render();
232
+ return;
233
+ case "tool_execution_end":
234
+ tools.end(event.toolCallId, event.result, event.isError);
235
+ render();
236
+ return;
237
+ case "queue_update":
238
+ setQueue(event.steering, event.followUp);
239
+ return;
240
+ case "compaction_start":
241
+ compacting = true;
242
+ syncLoader();
243
+ return;
244
+ case "compaction_end":
245
+ compacting = false;
246
+ if (event.result !== undefined)
247
+ view.addCompaction(event.result);
248
+ else if (event.error !== undefined)
249
+ view.addNotice("error", `压缩失败:${event.error}`);
250
+ else if (event.aborted)
251
+ view.addNotice("info", "压缩已取消");
252
+ status.refresh();
253
+ syncLoader();
254
+ return;
255
+ case "auto_retry_start":
256
+ view.addRetry(event.attempt, event.maxAttempts, event.delayMs, event.errorMessage);
257
+ render();
258
+ return;
259
+ case "auto_retry_end":
260
+ if (!event.success)
261
+ view.addRetryFailed(event.finalError);
262
+ render();
263
+ return;
264
+ case "cache_miss":
265
+ case "context_pressure": {
266
+ const shown = cacheEventNotice(event, cacheNoticesEnabled(session));
267
+ if (shown !== undefined)
268
+ view.addNotice(shown.level, shown.text);
269
+ status.refresh();
270
+ render();
271
+ return;
272
+ }
273
+ case "cache_warm":
274
+ case "permission_mode_changed":
275
+ case "model_changed":
276
+ case "thinking_level_changed":
277
+ case "session_changed":
278
+ status.refresh();
279
+ render();
280
+ return;
281
+ default:
282
+ return;
283
+ }
284
+ };
285
+ let unsubscribe = session.subscribe(onEvent);
286
+ const startup = buildStartupScreen(runtime);
287
+ /** 清空消息区(切换会话、/tree):只留标题行。 */
288
+ const resetView = () => {
289
+ view.reset();
290
+ view.addHeader(startup.slice(0, 1));
291
+ };
292
+ const replay = () => {
293
+ view.replay(session.messages, {
294
+ tool: (call, result) => tools.completed(call.id, call.name, call.arguments, result === undefined
295
+ ? { content: "(没有结果)", isError: true }
296
+ : {
297
+ content: result.content,
298
+ isError: result.isError,
299
+ ...(result.details !== undefined ? { details: result.details } : {}),
300
+ }, result?.isError ?? true),
301
+ });
302
+ };
303
+ const rebind = (next, reason) => {
304
+ unsubscribe();
305
+ session = next;
306
+ unsubscribe = next.subscribe(onEvent);
307
+ resetView();
308
+ tools.clear();
309
+ replay();
310
+ running = false;
311
+ compacting = false;
312
+ setQueue([], []);
313
+ status.refresh();
314
+ syncLoader();
315
+ if (next instanceof AgentSessionImpl)
316
+ next.announceStart(reason);
317
+ };
318
+ // ---- 提交与命令 -----------------------------------------------------------
319
+ /**
320
+ * 空闲时发提示。agent_settled 之后会话周期还要一个微任务才结束,所以先 waitForIdle;
321
+ * 仍然撞上运行中(极少)就排到本轮之后。
322
+ */
323
+ const startPrompt = (text) => {
324
+ const target = session;
325
+ void target
326
+ .waitForIdle()
327
+ .then(() => target.prompt(text))
328
+ .catch((error) => {
329
+ if (isAmaError(error) && error.code === "busy") {
330
+ void target.followUp(text).catch(() => undefined);
331
+ return;
332
+ }
333
+ if (isAmaError(error) && error.code === "prompt_blocked")
334
+ view.addHookBlocked(error.message);
335
+ else
336
+ notice("error", error instanceof Error ? error.message : String(error));
337
+ render();
338
+ });
339
+ };
340
+ const commandUi = {
341
+ runtime,
342
+ session: () => session,
343
+ async switchSession(request) {
344
+ const next = await switchSession(runtime, request);
345
+ rebind(next, request.kind === "new" ? "new" : request.kind === "fork" ? "fork" : "resume");
346
+ return next;
347
+ },
348
+ pick: (spec) => openPicker({
349
+ theme,
350
+ keybindings: keys,
351
+ showOverlay: (c, o) => tui.showOverlay(c, o),
352
+ columns: () => terminal.columns,
353
+ }, spec),
354
+ notice,
355
+ setEditorText: (text) => {
356
+ editor.setText(text);
357
+ render();
358
+ },
359
+ prompt: (text) => startPrompt(text),
360
+ reload: () => {
361
+ resetView();
362
+ tools.clear();
363
+ replay();
364
+ status.refresh();
365
+ render();
366
+ },
367
+ exit: (code) => exit(code),
368
+ now,
369
+ };
370
+ const runCommand = (line) => runInteractiveCommand(line, commandUi).finally(() => {
371
+ status.refresh();
372
+ render();
373
+ });
374
+ function submit(text, via) {
375
+ if (text.trim() === "")
376
+ return;
377
+ if (text.trimStart().startsWith("/")) {
378
+ void runCommand(text.trim()).then((handled) => {
379
+ if (!handled)
380
+ send(text, via);
381
+ });
382
+ return;
383
+ }
384
+ send(text, via);
385
+ }
386
+ function send(text, via) {
387
+ if (!running)
388
+ startPrompt(text);
389
+ else if (via === "followUp")
390
+ void session.followUp(text).catch(() => undefined);
391
+ else
392
+ void session.steer(text).catch(() => undefined);
393
+ }
394
+ // ---- 键位 -----------------------------------------------------------------
395
+ tui.addInputListener(createKeyDispatch({
396
+ keys,
397
+ editor,
398
+ tools,
399
+ status,
400
+ session: () => session,
401
+ inactive: () => finished || tui.hasOverlay,
402
+ busy: () => running || compacting,
403
+ now,
404
+ showHint,
405
+ submit: (text, via) => submit(text, via),
406
+ runCommand: (line) => void runCommand(line),
407
+ exit: (code) => exit(code),
408
+ }));
409
+ // ---- 晚绑定 ---------------------------------------------------------------
410
+ const broker = new ApprovalDialogBroker({
411
+ theme,
412
+ keybindings: keys,
413
+ cwd: session.state.cwd,
414
+ permissionMode: () => session.state.permissionMode,
415
+ showOverlay: (component) => tui.showOverlay(component, { anchor: "bottom" }),
416
+ onOpen: () => {
417
+ editor.disableSubmit = true;
418
+ render();
419
+ },
420
+ onClose: () => {
421
+ editor.disableSubmit = false;
422
+ render();
423
+ },
424
+ report: (request, outcome) => {
425
+ if (outcome === "deny" || outcome === "cancelled") {
426
+ notice(outcome === "deny" ? "info" : "warn", approvalOutcomeText(request, outcome));
427
+ }
428
+ },
429
+ });
430
+ // ---- 启动与退出 -----------------------------------------------------------
431
+ let offSignals = () => undefined;
432
+ function exit(code) {
433
+ if (finished)
434
+ return;
435
+ finished = true;
436
+ if (hintTimer !== undefined)
437
+ clearTimeout(hintTimer);
438
+ hint.setText("");
439
+ loader.stop();
440
+ loaderSlot.clear();
441
+ offSignals();
442
+ runtime.approvals.setUiBroker(undefined);
443
+ runtime.notifier.set(undefined);
444
+ tui.stop();
445
+ const active = session;
446
+ void active
447
+ .abort()
448
+ .catch(() => undefined)
449
+ .finally(() => {
450
+ unsubscribe();
451
+ resolveExit(code);
452
+ });
453
+ }
454
+ view.addHeader(startup);
455
+ for (const warning of startupWarnings)
456
+ view.addNotice("warn", warning);
457
+ replay();
458
+ status.refresh();
459
+ return new Promise((resolve, reject) => {
460
+ resolveExit = resolve;
461
+ try {
462
+ tui.start();
463
+ }
464
+ catch (error) {
465
+ unsubscribe();
466
+ reject(new AmaError("terminal_init_failed", `终端初始化失败:${error instanceof Error ? error.message : String(error)}`, { cause: error }));
467
+ return;
468
+ }
469
+ runtime.approvals.setUiBroker(broker);
470
+ runtime.notifier.set((message, level) => notice(level, message));
471
+ offSignals = onTerminationSignals((code) => exit(code));
472
+ tui.setFocus(editor);
473
+ options.onReady?.({
474
+ tui,
475
+ editor,
476
+ view,
477
+ tools,
478
+ status,
479
+ session: () => session,
480
+ exit,
481
+ });
482
+ if (context.prompt !== undefined && context.prompt.trim() !== "") {
483
+ editor.addToHistory(context.prompt);
484
+ submit(context.prompt, "enter");
485
+ }
486
+ });
487
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * 交互模式的应用级键位分派(设计 §12)。[B7]
3
+ *
4
+ * 编辑器之前的输入监听:Ctrl+C 清空输入 / 再按退出、Esc 中断(clearQueue 回填编辑器后 abort)、
5
+ * Ctrl+D 空输入退出、Alt+Enter followUp、Alt+↑ 取回最后一条排队消息、Shift+Tab 循环权限模式、
6
+ * Ctrl+O 展开工具输出、Ctrl+L 模型、Ctrl+T 思考级别。键位由 `keybindings.json` 覆盖。
7
+ */
8
+ import type { AgentSession } from "../../agent/types.js";
9
+ import type { Editor, Keybindings } from "../../tui.js";
10
+ import type { StatusBar } from "./status-bar.js";
11
+ import type { ToolTracker } from "./tool-view.js";
12
+ export interface KeyDispatchDeps {
13
+ keys: Keybindings;
14
+ editor: Editor;
15
+ tools: ToolTracker;
16
+ status: StatusBar;
17
+ session(): AgentSession;
18
+ /** 已退出或有覆盖层(审批 / 选择器)时不处理。 */
19
+ inactive(): boolean;
20
+ /** 运行或压缩中(Esc 才中断)。 */
21
+ busy(): boolean;
22
+ now(): number;
23
+ showHint(text: string): void;
24
+ submit(text: string, via: "followUp"): void;
25
+ runCommand(line: string): void;
26
+ exit(code: number): void;
27
+ }
28
+ /** 返回输入监听器:已处理返回 true,交给编辑器返回 false。 */
29
+ export declare function createKeyDispatch(deps: KeyDispatchDeps): (data: string) => boolean;
@@ -0,0 +1,107 @@
1
+ /**
2
+ * 交互模式的应用级键位分派(设计 §12)。[B7]
3
+ *
4
+ * 编辑器之前的输入监听:Ctrl+C 清空输入 / 再按退出、Esc 中断(clearQueue 回填编辑器后 abort)、
5
+ * Ctrl+D 空输入退出、Alt+Enter followUp、Alt+↑ 取回最后一条排队消息、Shift+Tab 循环权限模式、
6
+ * Ctrl+O 展开工具输出、Ctrl+L 模型、Ctrl+T 思考级别。键位由 `keybindings.json` 覆盖。
7
+ */
8
+ import { ExitCode } from "../../cli/exit-codes.js";
9
+ import { PERMISSION_MODES_STRICT_FIRST } from "../../permissions/types.js";
10
+ const DOUBLE_CTRL_C_MS = 1500;
11
+ /** 返回输入监听器:已处理返回 true,交给编辑器返回 false。 */
12
+ export function createKeyDispatch(deps) {
13
+ const { keys, editor, tools, status } = deps;
14
+ let ctrlCArmedAt = Number.NEGATIVE_INFINITY;
15
+ const interrupt = () => {
16
+ const session = deps.session();
17
+ const queued = session.clearQueue();
18
+ const restore = [...queued.steering, ...queued.followUp];
19
+ if (restore.length > 0) {
20
+ const current = editor.getText();
21
+ editor.setText([...restore, ...(current.trim() !== "" ? [current] : [])].join("\n"));
22
+ }
23
+ void session.abort().catch(() => undefined);
24
+ };
25
+ const dequeue = () => {
26
+ const session = deps.session();
27
+ const queued = session.clearQueue();
28
+ const steering = [...queued.steering];
29
+ const followUp = [...queued.followUp];
30
+ const last = followUp.length > 0 ? followUp.pop() : steering.pop();
31
+ if (last === undefined)
32
+ return;
33
+ for (const text of steering)
34
+ void session.steer(text).catch(() => undefined);
35
+ for (const text of followUp)
36
+ void session.followUp(text).catch(() => undefined);
37
+ const current = editor.getText();
38
+ editor.setText(current.trim() === "" ? last : `${last}\n${current}`);
39
+ };
40
+ const cyclePermission = () => {
41
+ const session = deps.session();
42
+ const modes = PERMISSION_MODES_STRICT_FIRST;
43
+ const index = modes.indexOf(session.state.permissionMode);
44
+ const next = modes[(index + 1) % modes.length];
45
+ session.setPermissionMode(next);
46
+ status.refresh();
47
+ deps.showHint(`权限模式:${next}`);
48
+ };
49
+ return (data) => {
50
+ if (deps.inactive())
51
+ return false;
52
+ const is = (action) => keys.matches(data, action);
53
+ if (is("app.clear")) {
54
+ if (!editor.isEmpty()) {
55
+ editor.clear();
56
+ ctrlCArmedAt = deps.now();
57
+ deps.showHint("已清空输入 · 再按 Ctrl+C 退出");
58
+ }
59
+ else if (deps.now() - ctrlCArmedAt < DOUBLE_CTRL_C_MS) {
60
+ deps.exit(ExitCode.Sigint);
61
+ }
62
+ else {
63
+ ctrlCArmedAt = deps.now();
64
+ deps.showHint("再按一次 Ctrl+C 退出");
65
+ }
66
+ return true;
67
+ }
68
+ ctrlCArmedAt = Number.NEGATIVE_INFINITY;
69
+ if (is("app.interrupt") && !editor.isCompletionOpen && deps.busy()) {
70
+ interrupt();
71
+ deps.showHint("已中断");
72
+ return true;
73
+ }
74
+ if (is("app.exit") && editor.isEmpty()) {
75
+ deps.exit(ExitCode.Ok);
76
+ return true;
77
+ }
78
+ if (is("app.message.followUp")) {
79
+ const text = editor.takeSubmission();
80
+ if (text !== null)
81
+ deps.submit(text, "followUp");
82
+ return true;
83
+ }
84
+ if (is("app.message.dequeue")) {
85
+ dequeue();
86
+ return true;
87
+ }
88
+ if (is("app.permission.cycle")) {
89
+ cyclePermission();
90
+ return true;
91
+ }
92
+ if (is("app.tools.expand")) {
93
+ const expanded = tools.toggleExpanded();
94
+ deps.showHint(expanded ? "工具输出:展开" : "工具输出:折叠");
95
+ return true;
96
+ }
97
+ if (is("app.model.select")) {
98
+ deps.runCommand("/model");
99
+ return true;
100
+ }
101
+ if (is("app.thinking.select")) {
102
+ deps.runCommand("/thinking");
103
+ return true;
104
+ }
105
+ return false;
106
+ };
107
+ }