@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.
- package/CHANGELOG.md +37 -0
- package/LICENSE +21 -0
- package/README.md +470 -2
- package/dist/agent/agent.d.ts +56 -0
- package/dist/agent/agent.js +129 -0
- package/dist/agent/loop.d.ts +51 -0
- package/dist/agent/loop.js +158 -0
- package/dist/agent/queue.d.ts +25 -0
- package/dist/agent/queue.js +55 -0
- package/dist/agent/retry.d.ts +22 -0
- package/dist/agent/retry.js +92 -0
- package/dist/agent/schema.d.ts +27 -0
- package/dist/agent/schema.js +186 -0
- package/dist/agent/session-cache.d.ts +100 -0
- package/dist/agent/session-cache.js +514 -0
- package/dist/agent/session-compaction.d.ts +49 -0
- package/dist/agent/session-compaction.js +251 -0
- package/dist/agent/session-core.d.ts +103 -0
- package/dist/agent/session-core.js +7 -0
- package/dist/agent/session-run.d.ts +55 -0
- package/dist/agent/session-run.js +246 -0
- package/dist/agent/session-state.d.ts +39 -0
- package/dist/agent/session-state.js +119 -0
- package/dist/agent/session-subagent.d.ts +54 -0
- package/dist/agent/session-subagent.js +157 -0
- package/dist/agent/session-sync.d.ts +20 -0
- package/dist/agent/session-sync.js +111 -0
- package/dist/agent/session-tools.d.ts +24 -0
- package/dist/agent/session-tools.js +277 -0
- package/dist/agent/session.d.ts +117 -0
- package/dist/agent/session.js +485 -0
- package/dist/agent/system-prompt.d.ts +49 -0
- package/dist/agent/system-prompt.js +121 -0
- package/dist/agent/tool-runner.d.ts +64 -0
- package/dist/agent/tool-runner.js +309 -0
- package/dist/agent/transform.d.ts +26 -0
- package/dist/agent/transform.js +171 -0
- package/dist/agent/types.d.ts +378 -0
- package/dist/agent/types.js +21 -0
- package/dist/ai/apis/anthropic-messages.d.ts +26 -0
- package/dist/ai/apis/anthropic-messages.js +214 -0
- package/dist/ai/apis/anthropic-request.d.ts +42 -0
- package/dist/ai/apis/anthropic-request.js +282 -0
- package/dist/ai/apis/api.d.ts +28 -0
- package/dist/ai/apis/api.js +102 -0
- package/dist/ai/apis/cache-params.d.ts +48 -0
- package/dist/ai/apis/cache-params.js +163 -0
- package/dist/ai/apis/google-generative-ai.d.ts +32 -0
- package/dist/ai/apis/google-generative-ai.js +249 -0
- package/dist/ai/apis/google-request.d.ts +40 -0
- package/dist/ai/apis/google-request.js +255 -0
- package/dist/ai/apis/openai-compat.d.ts +35 -0
- package/dist/ai/apis/openai-compat.js +142 -0
- package/dist/ai/apis/openai-completions.d.ts +28 -0
- package/dist/ai/apis/openai-completions.js +246 -0
- package/dist/ai/apis/openai-request.d.ts +35 -0
- package/dist/ai/apis/openai-request.js +303 -0
- package/dist/ai/apis/openai-responses-request.d.ts +52 -0
- package/dist/ai/apis/openai-responses-request.js +266 -0
- package/dist/ai/apis/openai-responses.d.ts +28 -0
- package/dist/ai/apis/openai-responses.js +304 -0
- package/dist/ai/apis/shared.d.ts +56 -0
- package/dist/ai/apis/shared.js +179 -0
- package/dist/ai/cache/economics.d.ts +24 -0
- package/dist/ai/cache/economics.js +69 -0
- package/dist/ai/cache/fingerprint.d.ts +17 -0
- package/dist/ai/cache/fingerprint.js +39 -0
- package/dist/ai/cache/miss.d.ts +49 -0
- package/dist/ai/cache/miss.js +96 -0
- package/dist/ai/cache/reporting.d.ts +39 -0
- package/dist/ai/cache/reporting.js +103 -0
- package/dist/ai/cache/types.d.ts +94 -0
- package/dist/ai/cache/types.js +19 -0
- package/dist/ai/cache/warmer.d.ts +94 -0
- package/dist/ai/cache/warmer.js +224 -0
- package/dist/ai/context.d.ts +35 -0
- package/dist/ai/context.js +114 -0
- package/dist/ai/cost.d.ts +21 -0
- package/dist/ai/cost.js +60 -0
- package/dist/ai/event-stream.d.ts +33 -0
- package/dist/ai/event-stream.js +101 -0
- package/dist/ai/fake/fake-provider.d.ts +68 -0
- package/dist/ai/fake/fake-provider.js +278 -0
- package/dist/ai/fake/fake-script.d.ts +69 -0
- package/dist/ai/fake/fake-script.js +142 -0
- package/dist/ai/http.d.ts +54 -0
- package/dist/ai/http.js +212 -0
- package/dist/ai/json-partial.d.ts +17 -0
- package/dist/ai/json-partial.js +243 -0
- package/dist/ai/overflow.d.ts +22 -0
- package/dist/ai/overflow.js +61 -0
- package/dist/ai/providers/auth.d.ts +71 -0
- package/dist/ai/providers/auth.js +202 -0
- package/dist/ai/providers/builtin.d.ts +23 -0
- package/dist/ai/providers/builtin.js +148 -0
- package/dist/ai/providers/catalog-data.d.ts +5 -0
- package/dist/ai/providers/catalog-data.js +19 -0
- package/dist/ai/providers/catalog.d.ts +28 -0
- package/dist/ai/providers/catalog.js +166 -0
- package/dist/ai/providers/channels.d.ts +51 -0
- package/dist/ai/providers/channels.js +96 -0
- package/dist/ai/providers/enrich.d.ts +34 -0
- package/dist/ai/providers/enrich.js +86 -0
- package/dist/ai/providers/models-dev-cache.d.ts +53 -0
- package/dist/ai/providers/models-dev-cache.js +147 -0
- package/dist/ai/providers/models-dev.d.ts +99 -0
- package/dist/ai/providers/models-dev.js +315 -0
- package/dist/ai/providers/registry.d.ts +114 -0
- package/dist/ai/providers/registry.js +458 -0
- package/dist/ai/sse.d.ts +32 -0
- package/dist/ai/sse.js +140 -0
- package/dist/ai/thinking.d.ts +42 -0
- package/dist/ai/thinking.js +101 -0
- package/dist/ai/types.d.ts +448 -0
- package/dist/ai/types.js +19 -0
- package/dist/bundle/ama-sandbox.cjs +324 -0
- package/dist/bundle/ama.cjs +27837 -0
- package/dist/bundle.d.ts +9 -0
- package/dist/bundle.js +15 -0
- package/dist/cli/args.d.ts +85 -0
- package/dist/cli/args.js +408 -0
- package/dist/cli/bootstrap.d.ts +18 -0
- package/dist/cli/bootstrap.js +399 -0
- package/dist/cli/compose-providers.d.ts +37 -0
- package/dist/cli/compose-providers.js +107 -0
- package/dist/cli/compose-session.d.ts +72 -0
- package/dist/cli/compose-session.js +370 -0
- package/dist/cli/compose-store.d.ts +24 -0
- package/dist/cli/compose-store.js +125 -0
- package/dist/cli/compose.d.ts +108 -0
- package/dist/cli/compose.js +221 -0
- package/dist/cli/default-model.d.ts +16 -0
- package/dist/cli/default-model.js +24 -0
- package/dist/cli/deps.d.ts +207 -0
- package/dist/cli/deps.js +12 -0
- package/dist/cli/exit-codes.d.ts +29 -0
- package/dist/cli/exit-codes.js +40 -0
- package/dist/cli/main.d.ts +27 -0
- package/dist/cli/main.js +177 -0
- package/dist/cli/runtime.d.ts +90 -0
- package/dist/cli/runtime.js +8 -0
- package/dist/cli/startup-screen.d.ts +15 -0
- package/dist/cli/startup-screen.js +56 -0
- package/dist/cli/startup-steps.d.ts +35 -0
- package/dist/cli/startup-steps.js +202 -0
- package/dist/cli/subcommands/auth.d.ts +12 -0
- package/dist/cli/subcommands/auth.js +91 -0
- package/dist/cli/subcommands/config.d.ts +53 -0
- package/dist/cli/subcommands/config.js +277 -0
- package/dist/cli/subcommands/context.d.ts +21 -0
- package/dist/cli/subcommands/context.js +42 -0
- package/dist/cli/subcommands/doctor.d.ts +11 -0
- package/dist/cli/subcommands/doctor.js +246 -0
- package/dist/cli/subcommands/init.d.ts +6 -0
- package/dist/cli/subcommands/init.js +22 -0
- package/dist/cli/subcommands/model-meta.d.ts +16 -0
- package/dist/cli/subcommands/model-meta.js +54 -0
- package/dist/cli/subcommands/models-cache-probe.d.ts +34 -0
- package/dist/cli/subcommands/models-cache-probe.js +190 -0
- package/dist/cli/subcommands/models-discover.d.ts +46 -0
- package/dist/cli/subcommands/models-discover.js +232 -0
- package/dist/cli/subcommands/models.d.ts +40 -0
- package/dist/cli/subcommands/models.js +152 -0
- package/dist/cli/subcommands/providers-list.d.ts +8 -0
- package/dist/cli/subcommands/providers-list.js +97 -0
- package/dist/cli/subcommands/providers-plan.d.ts +79 -0
- package/dist/cli/subcommands/providers-plan.js +215 -0
- package/dist/cli/subcommands/providers.d.ts +27 -0
- package/dist/cli/subcommands/providers.js +433 -0
- package/dist/cli/subcommands/sessions.d.ts +11 -0
- package/dist/cli/subcommands/sessions.js +100 -0
- package/dist/codemode/capability.d.ts +37 -0
- package/dist/codemode/capability.js +55 -0
- package/dist/codemode/declarations.d.ts +44 -0
- package/dist/codemode/declarations.js +111 -0
- package/dist/codemode/host-side.d.ts +59 -0
- package/dist/codemode/host-side.js +253 -0
- package/dist/codemode/modes.d.ts +23 -0
- package/dist/codemode/modes.js +40 -0
- package/dist/codemode/protocol.d.ts +91 -0
- package/dist/codemode/protocol.js +114 -0
- package/dist/codemode/sandbox-entry.d.ts +25 -0
- package/dist/codemode/sandbox-entry.js +323 -0
- package/dist/codemode/store.d.ts +26 -0
- package/dist/codemode/store.js +53 -0
- package/dist/codemode/tool.d.ts +105 -0
- package/dist/codemode/tool.js +283 -0
- package/dist/compaction/branch-summary.d.ts +30 -0
- package/dist/compaction/branch-summary.js +79 -0
- package/dist/compaction/breaker.d.ts +32 -0
- package/dist/compaction/breaker.js +65 -0
- package/dist/compaction/cut-point.d.ts +23 -0
- package/dist/compaction/cut-point.js +70 -0
- package/dist/compaction/estimate.d.ts +30 -0
- package/dist/compaction/estimate.js +114 -0
- package/dist/compaction/prune-tier.d.ts +31 -0
- package/dist/compaction/prune-tier.js +83 -0
- package/dist/compaction/serialize.d.ts +20 -0
- package/dist/compaction/serialize.js +115 -0
- package/dist/compaction/summarize-tier.d.ts +111 -0
- package/dist/compaction/summarize-tier.js +305 -0
- package/dist/config/auth-file.d.ts +39 -0
- package/dist/config/auth-file.js +92 -0
- package/dist/config/context-files.d.ts +30 -0
- package/dist/config/context-files.js +77 -0
- package/dist/config/init.d.ts +30 -0
- package/dist/config/init.js +92 -0
- package/dist/config/json-schema.d.ts +14 -0
- package/dist/config/json-schema.js +182 -0
- package/dist/config/load.d.ts +45 -0
- package/dist/config/load.js +97 -0
- package/dist/config/merge.d.ts +73 -0
- package/dist/config/merge.js +248 -0
- package/dist/config/paths.d.ts +38 -0
- package/dist/config/paths.js +108 -0
- package/dist/config/profile.d.ts +27 -0
- package/dist/config/profile.js +65 -0
- package/dist/config/schema.d.ts +34 -0
- package/dist/config/schema.js +476 -0
- package/dist/config/trust.d.ts +42 -0
- package/dist/config/trust.js +122 -0
- package/dist/config/types.d.ts +190 -0
- package/dist/config/types.js +29 -0
- package/dist/config/write.d.ts +10 -0
- package/dist/config/write.js +25 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +30 -0
- package/dist/hooks/config.d.ts +49 -0
- package/dist/hooks/config.js +102 -0
- package/dist/hooks/dispatcher.d.ts +41 -0
- package/dist/hooks/dispatcher.js +104 -0
- package/dist/hooks/matcher.d.ts +22 -0
- package/dist/hooks/matcher.js +108 -0
- package/dist/hooks/protocol.d.ts +33 -0
- package/dist/hooks/protocol.js +240 -0
- package/dist/hooks/runner.d.ts +30 -0
- package/dist/hooks/runner.js +143 -0
- package/dist/hooks/types.d.ts +147 -0
- package/dist/hooks/types.js +20 -0
- package/dist/host/api-impl.d.ts +75 -0
- package/dist/host/api-impl.js +173 -0
- package/dist/host/loader.d.ts +31 -0
- package/dist/host/loader.js +147 -0
- package/dist/host/types.d.ts +169 -0
- package/dist/host/types.js +14 -0
- package/dist/host.d.ts +5 -0
- package/dist/host.js +4 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +28 -0
- package/dist/modes/commands-core.d.ts +44 -0
- package/dist/modes/commands-core.js +171 -0
- package/dist/modes/image-input.d.ts +27 -0
- package/dist/modes/image-input.js +78 -0
- package/dist/modes/interactive/approval-dialog.d.ts +48 -0
- package/dist/modes/interactive/approval-dialog.js +202 -0
- package/dist/modes/interactive/commands.d.ts +41 -0
- package/dist/modes/interactive/commands.js +201 -0
- package/dist/modes/interactive/completion.d.ts +53 -0
- package/dist/modes/interactive/completion.js +157 -0
- package/dist/modes/interactive/interactive-mode.d.ts +46 -0
- package/dist/modes/interactive/interactive-mode.js +491 -0
- package/dist/modes/interactive/key-dispatch.d.ts +29 -0
- package/dist/modes/interactive/key-dispatch.js +107 -0
- package/dist/modes/interactive/line/line-editor.d.ts +50 -0
- package/dist/modes/interactive/line/line-editor.js +232 -0
- package/dist/modes/interactive/line/line-mode.d.ts +20 -0
- package/dist/modes/interactive/line/line-mode.js +218 -0
- package/dist/modes/interactive/line/line-render.d.ts +35 -0
- package/dist/modes/interactive/line/line-render.js +171 -0
- package/dist/modes/interactive/line/paste-state.d.ts +26 -0
- package/dist/modes/interactive/line/paste-state.js +72 -0
- package/dist/modes/interactive/message-view.d.ts +77 -0
- package/dist/modes/interactive/message-view.js +269 -0
- package/dist/modes/interactive/pickers.d.ts +42 -0
- package/dist/modes/interactive/pickers.js +115 -0
- package/dist/modes/interactive/startup-ui.d.ts +33 -0
- package/dist/modes/interactive/startup-ui.js +268 -0
- package/dist/modes/interactive/status-bar.d.ts +48 -0
- package/dist/modes/interactive/status-bar.js +139 -0
- package/dist/modes/interactive/tool-view.d.ts +95 -0
- package/dist/modes/interactive/tool-view.js +344 -0
- package/dist/modes/print/json-event.d.ts +15 -0
- package/dist/modes/print/json-event.js +39 -0
- package/dist/modes/print/print-mode.d.ts +17 -0
- package/dist/modes/print/print-mode.js +107 -0
- package/dist/modes/rpc/commands.d.ts +40 -0
- package/dist/modes/rpc/commands.js +263 -0
- package/dist/modes/rpc/jsonl.d.ts +16 -0
- package/dist/modes/rpc/jsonl.js +55 -0
- package/dist/modes/rpc/rpc-mode.d.ts +22 -0
- package/dist/modes/rpc/rpc-mode.js +153 -0
- package/dist/modes/session-report.d.ts +55 -0
- package/dist/modes/session-report.js +278 -0
- package/dist/modes/shared.d.ts +13 -0
- package/dist/modes/shared.js +52 -0
- package/dist/modes/startup-ui-text.d.ts +16 -0
- package/dist/modes/startup-ui-text.js +103 -0
- package/dist/permissions/broker.d.ts +47 -0
- package/dist/permissions/broker.js +107 -0
- package/dist/permissions/dangerous.d.ts +47 -0
- package/dist/permissions/dangerous.js +370 -0
- package/dist/permissions/pipeline.d.ts +51 -0
- package/dist/permissions/pipeline.js +181 -0
- package/dist/permissions/preview.d.ts +43 -0
- package/dist/permissions/preview.js +463 -0
- package/dist/permissions/rules.d.ts +58 -0
- package/dist/permissions/rules.js +249 -0
- package/dist/permissions/types.d.ts +100 -0
- package/dist/permissions/types.js +18 -0
- package/dist/rpc.d.ts +173 -0
- package/dist/rpc.js +13 -0
- package/dist/sdk.d.ts +98 -0
- package/dist/sdk.js +195 -0
- package/dist/session/manager.d.ts +70 -0
- package/dist/session/manager.js +278 -0
- package/dist/session/migrate.d.ts +19 -0
- package/dist/session/migrate.js +53 -0
- package/dist/session/projection.d.ts +50 -0
- package/dist/session/projection.js +184 -0
- package/dist/session/store.d.ts +47 -0
- package/dist/session/store.js +210 -0
- package/dist/session/tree.d.ts +21 -0
- package/dist/session/tree.js +88 -0
- package/dist/session/types.d.ts +201 -0
- package/dist/session/types.js +18 -0
- package/dist/skills/discover.d.ts +60 -0
- package/dist/skills/discover.js +160 -0
- package/dist/skills/expand.d.ts +35 -0
- package/dist/skills/expand.js +46 -0
- package/dist/skills/frontmatter.d.ts +27 -0
- package/dist/skills/frontmatter.js +157 -0
- package/dist/skills/index-prompt.d.ts +13 -0
- package/dist/skills/index-prompt.js +42 -0
- package/dist/skills/templates.d.ts +52 -0
- package/dist/skills/templates.js +177 -0
- package/dist/tools/bash.d.ts +49 -0
- package/dist/tools/bash.js +222 -0
- package/dist/tools/edit-fuzzy.d.ts +36 -0
- package/dist/tools/edit-fuzzy.js +129 -0
- package/dist/tools/edit.d.ts +38 -0
- package/dist/tools/edit.js +241 -0
- package/dist/tools/file-mutex.d.ts +8 -0
- package/dist/tools/file-mutex.js +29 -0
- package/dist/tools/glob.d.ts +44 -0
- package/dist/tools/glob.js +217 -0
- package/dist/tools/grep.d.ts +39 -0
- package/dist/tools/grep.js +179 -0
- package/dist/tools/ignore.d.ts +40 -0
- package/dist/tools/ignore.js +174 -0
- package/dist/tools/image-file.d.ts +31 -0
- package/dist/tools/image-file.js +114 -0
- package/dist/tools/ls.d.ts +21 -0
- package/dist/tools/ls.js +96 -0
- package/dist/tools/output-accumulator.d.ts +48 -0
- package/dist/tools/output-accumulator.js +106 -0
- package/dist/tools/paths.d.ts +20 -0
- package/dist/tools/paths.js +58 -0
- package/dist/tools/presets.d.ts +71 -0
- package/dist/tools/presets.js +128 -0
- package/dist/tools/process-tree.d.ts +35 -0
- package/dist/tools/process-tree.js +125 -0
- package/dist/tools/read.d.ts +24 -0
- package/dist/tools/read.js +132 -0
- package/dist/tools/registry.d.ts +43 -0
- package/dist/tools/registry.js +106 -0
- package/dist/tools/shell.d.ts +38 -0
- package/dist/tools/shell.js +91 -0
- package/dist/tools/task.d.ts +29 -0
- package/dist/tools/task.js +107 -0
- package/dist/tools/todo.d.ts +28 -0
- package/dist/tools/todo.js +107 -0
- package/dist/tools/truncate.d.ts +44 -0
- package/dist/tools/truncate.js +175 -0
- package/dist/tools/types.d.ts +135 -0
- package/dist/tools/types.js +19 -0
- package/dist/tools/write.d.ts +20 -0
- package/dist/tools/write.js +84 -0
- package/dist/tui/ansi.d.ts +29 -0
- package/dist/tui/ansi.js +420 -0
- package/dist/tui/component.d.ts +38 -0
- package/dist/tui/component.js +13 -0
- package/dist/tui/components/box.d.ts +26 -0
- package/dist/tui/components/box.js +65 -0
- package/dist/tui/components/container.d.ts +14 -0
- package/dist/tui/components/container.js +39 -0
- package/dist/tui/components/editor-buffer.d.ts +67 -0
- package/dist/tui/components/editor-buffer.js +300 -0
- package/dist/tui/components/editor-paste.d.ts +29 -0
- package/dist/tui/components/editor-paste.js +64 -0
- package/dist/tui/components/editor.d.ts +114 -0
- package/dist/tui/components/editor.js +530 -0
- package/dist/tui/components/key-value.d.ts +29 -0
- package/dist/tui/components/key-value.js +48 -0
- package/dist/tui/components/loader.d.ts +35 -0
- package/dist/tui/components/loader.js +71 -0
- package/dist/tui/components/markdown.d.ts +90 -0
- package/dist/tui/components/markdown.js +462 -0
- package/dist/tui/components/meter.d.ts +29 -0
- package/dist/tui/components/meter.js +47 -0
- package/dist/tui/components/overlay.d.ts +24 -0
- package/dist/tui/components/overlay.js +52 -0
- package/dist/tui/components/select-list.d.ts +57 -0
- package/dist/tui/components/select-list.js +179 -0
- package/dist/tui/components/spacer.d.ts +11 -0
- package/dist/tui/components/spacer.js +16 -0
- package/dist/tui/components/text.d.ts +30 -0
- package/dist/tui/components/text.js +65 -0
- package/dist/tui/keybindings.d.ts +66 -0
- package/dist/tui/keybindings.js +120 -0
- package/dist/tui/keys.d.ts +38 -0
- package/dist/tui/keys.js +275 -0
- package/dist/tui/stdin-buffer.d.ts +41 -0
- package/dist/tui/stdin-buffer.js +206 -0
- package/dist/tui/terminal.d.ts +79 -0
- package/dist/tui/terminal.js +153 -0
- package/dist/tui/theme.d.ts +31 -0
- package/dist/tui/theme.js +199 -0
- package/dist/tui/tui.d.ts +103 -0
- package/dist/tui/tui.js +363 -0
- package/dist/tui/vt-screen.d.ts +46 -0
- package/dist/tui/vt-screen.js +251 -0
- package/dist/tui.d.ts +30 -0
- package/dist/tui.js +31 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.js +24 -0
- package/docs/codemode.md +102 -0
- package/docs/hooks.md +196 -0
- package/docs/host-api.md +161 -0
- package/docs/providers.md +531 -0
- package/docs/rpc.md +253 -0
- package/docs/session-format.md +119 -0
- package/docs/tui.md +170 -0
- 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,491 @@
|
|
|
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 { promptImages, sessionModel } from "../image-input.js";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { AgentSessionImpl } from "../../agent/session.js";
|
|
19
|
+
import { currentSession, switchSession } from "../../cli/compose-session.js";
|
|
20
|
+
import { buildStartupScreen } from "../../cli/startup-screen.js";
|
|
21
|
+
import { KEYBINDINGS_FILE } from "../../config/paths.js";
|
|
22
|
+
import { AmaError, isAmaError } from "../../errors.js";
|
|
23
|
+
import { detectSandboxCapability } from "../../codemode/capability.js";
|
|
24
|
+
import { effectiveCodemodeMode } from "../../tools/presets.js";
|
|
25
|
+
import { Container, Editor, Keybindings, Loader, ProcessTerminal, Spacer, TUI, Text, createTheme, detectCapabilities, loadKeybindingsFile, truncateToWidth, } from "../../tui.js";
|
|
26
|
+
import { cacheEventNotice, cacheNoticesEnabled } from "../session-report.js";
|
|
27
|
+
import { onTerminationSignals } from "../shared.js";
|
|
28
|
+
import { ApprovalDialogBroker, approvalOutcomeText } from "./approval-dialog.js";
|
|
29
|
+
import { ALL_COMMANDS, runInteractiveCommand } from "./commands.js";
|
|
30
|
+
import { InteractiveCompletion } from "./completion.js";
|
|
31
|
+
import { createKeyDispatch } from "./key-dispatch.js";
|
|
32
|
+
import { MessageView } from "./message-view.js";
|
|
33
|
+
import { openPicker } from "./pickers.js";
|
|
34
|
+
import { StatusBar } from "./status-bar.js";
|
|
35
|
+
import { ToolTracker } from "./tool-view.js";
|
|
36
|
+
const HINT_MS = 2500;
|
|
37
|
+
const QUEUE_PREVIEW = 3;
|
|
38
|
+
function processTerminal() {
|
|
39
|
+
const stdin = process.stdin;
|
|
40
|
+
if (stdin.isTTY !== true || typeof stdin.setRawMode !== "function" || !process.stdout.isTTY) {
|
|
41
|
+
throw new AmaError("terminal_init_failed", "stdin / stdout 不是终端");
|
|
42
|
+
}
|
|
43
|
+
return new ProcessTerminal();
|
|
44
|
+
}
|
|
45
|
+
function loadKeys(runtime, warn) {
|
|
46
|
+
const parsed = loadKeybindingsFile(join(runtime.paths.configDir, KEYBINDINGS_FILE));
|
|
47
|
+
for (const w of parsed.warnings)
|
|
48
|
+
warn(`keybindings.json:${w}`);
|
|
49
|
+
return new Keybindings(parsed.overrides);
|
|
50
|
+
}
|
|
51
|
+
/** 一行提示;空时不占行。 */
|
|
52
|
+
class HintLine {
|
|
53
|
+
text = "";
|
|
54
|
+
setText(text) {
|
|
55
|
+
this.text = text;
|
|
56
|
+
}
|
|
57
|
+
render(width) {
|
|
58
|
+
return this.text === "" ? [] : [truncateToWidth(this.text, width)];
|
|
59
|
+
}
|
|
60
|
+
invalidate() { }
|
|
61
|
+
}
|
|
62
|
+
function oneLine(text) {
|
|
63
|
+
return text.replace(/\s+/g, " ").trim();
|
|
64
|
+
}
|
|
65
|
+
export function runInteractiveMode(runtime, context, options = {}) {
|
|
66
|
+
const terminal = options.terminal ?? processTerminal();
|
|
67
|
+
const ui = runtime.config.ui ?? {};
|
|
68
|
+
const theme = options.theme ??
|
|
69
|
+
createTheme(ui.theme ?? "dark", {
|
|
70
|
+
caps: detectCapabilities(context.io.env, context.io.stdoutIsTTY),
|
|
71
|
+
});
|
|
72
|
+
const now = options.now ?? Date.now;
|
|
73
|
+
const startupWarnings = [];
|
|
74
|
+
const keys = options.keybindings ?? loadKeys(runtime, (m) => startupWarnings.push(m));
|
|
75
|
+
let session = currentSession(runtime);
|
|
76
|
+
const tui = new TUI(terminal);
|
|
77
|
+
const view = new MessageView({
|
|
78
|
+
theme,
|
|
79
|
+
...(ui.showThinking !== undefined ? { showThinking: ui.showThinking } : {}),
|
|
80
|
+
...(ui.markdown !== undefined ? { markdown: ui.markdown } : {}),
|
|
81
|
+
});
|
|
82
|
+
const tools = new ToolTracker({
|
|
83
|
+
theme,
|
|
84
|
+
cwd: session.state.cwd,
|
|
85
|
+
getTool: (name) => session.getTools().find((tool) => tool.name === name),
|
|
86
|
+
});
|
|
87
|
+
const queueText = new Text("");
|
|
88
|
+
const loaderSlot = new Container();
|
|
89
|
+
const loader = new Loader(() => tui.requestRender(), {
|
|
90
|
+
theme,
|
|
91
|
+
message: "工作中" + theme.fg("dim", " · Esc 中断"),
|
|
92
|
+
now,
|
|
93
|
+
...(options.spinnerIntervalMs !== undefined ? { intervalMs: options.spinnerIntervalMs } : {}),
|
|
94
|
+
});
|
|
95
|
+
const hint = new HintLine();
|
|
96
|
+
let sandboxStrict;
|
|
97
|
+
const status = new StatusBar({
|
|
98
|
+
session: () => session,
|
|
99
|
+
preset: () => runtime.config.tools?.preset ?? "default",
|
|
100
|
+
hostStatus: () => runtime.host?.status(),
|
|
101
|
+
codemode: () => {
|
|
102
|
+
const mode = effectiveCodemodeMode(runtime.config);
|
|
103
|
+
const active = session.getTools().some((tool) => tool.name === "codemode");
|
|
104
|
+
return mode === "off" || !active ? undefined : mode;
|
|
105
|
+
},
|
|
106
|
+
sandboxStrict: () => (sandboxStrict ??= detectSandboxCapability().strict),
|
|
107
|
+
}, theme);
|
|
108
|
+
const historyFile = options.historyFile === false
|
|
109
|
+
? undefined
|
|
110
|
+
: (options.historyFile ?? join(runtime.paths.dataDir, "history"));
|
|
111
|
+
const completion = new InteractiveCompletion({
|
|
112
|
+
commands: () => ALL_COMMANDS,
|
|
113
|
+
prompts: () => runtime.resources.prompts,
|
|
114
|
+
skills: () => runtime.resources.skills,
|
|
115
|
+
cwd: () => session.state.cwd,
|
|
116
|
+
now,
|
|
117
|
+
});
|
|
118
|
+
const editor = new Editor({
|
|
119
|
+
theme,
|
|
120
|
+
keybindings: keys,
|
|
121
|
+
maxVisibleLines: 8,
|
|
122
|
+
autocomplete: completion,
|
|
123
|
+
requestRender: () => tui.requestRender(),
|
|
124
|
+
...(historyFile !== undefined ? { historyFile } : {}),
|
|
125
|
+
onSubmit: (text) => submit(text, "enter"),
|
|
126
|
+
});
|
|
127
|
+
tui.addChild(view);
|
|
128
|
+
tui.addChild(new Spacer());
|
|
129
|
+
tui.addChild(queueText);
|
|
130
|
+
tui.addChild(loaderSlot);
|
|
131
|
+
tui.addChild(editor);
|
|
132
|
+
tui.addChild(hint);
|
|
133
|
+
tui.addChild(status);
|
|
134
|
+
// ---- 小工具 ---------------------------------------------------------------
|
|
135
|
+
let finished = false;
|
|
136
|
+
let running = false;
|
|
137
|
+
let compacting = false;
|
|
138
|
+
let hintTimer;
|
|
139
|
+
let resolveExit = () => undefined;
|
|
140
|
+
const render = () => tui.requestRender();
|
|
141
|
+
const notice = (level, text) => {
|
|
142
|
+
view.addNotice(level, text);
|
|
143
|
+
render();
|
|
144
|
+
};
|
|
145
|
+
const showHint = (text) => {
|
|
146
|
+
hint.setText(text === "" ? "" : theme.fg("dim", text));
|
|
147
|
+
if (hintTimer !== undefined)
|
|
148
|
+
clearTimeout(hintTimer);
|
|
149
|
+
hintTimer = undefined;
|
|
150
|
+
if (text !== "") {
|
|
151
|
+
hintTimer = setTimeout(() => {
|
|
152
|
+
hint.setText("");
|
|
153
|
+
render();
|
|
154
|
+
}, HINT_MS);
|
|
155
|
+
hintTimer.unref?.();
|
|
156
|
+
}
|
|
157
|
+
render();
|
|
158
|
+
};
|
|
159
|
+
const syncLoader = () => {
|
|
160
|
+
const active = running || compacting;
|
|
161
|
+
if (active && loaderSlot.children.length === 0) {
|
|
162
|
+
loader.setMessage((compacting && !running ? "压缩上下文" : "工作中") + theme.fg("dim", " · Esc 中断"));
|
|
163
|
+
loaderSlot.addChild(loader);
|
|
164
|
+
loader.start();
|
|
165
|
+
}
|
|
166
|
+
else if (!active && loaderSlot.children.length > 0) {
|
|
167
|
+
loader.stop();
|
|
168
|
+
loaderSlot.clear();
|
|
169
|
+
}
|
|
170
|
+
render();
|
|
171
|
+
};
|
|
172
|
+
const setQueue = (steering, followUp) => {
|
|
173
|
+
status.setQueue(steering.length, followUp.length);
|
|
174
|
+
const rows = [
|
|
175
|
+
...steering.map((t) => `↳ steer ${oneLine(t)}`),
|
|
176
|
+
...followUp.map((t) => `↳ followUp ${oneLine(t)}`),
|
|
177
|
+
];
|
|
178
|
+
const shown = rows.slice(-QUEUE_PREVIEW);
|
|
179
|
+
if (rows.length > shown.length)
|
|
180
|
+
shown.unshift(`… 另 ${rows.length - shown.length} 条`);
|
|
181
|
+
if (rows.length > 0)
|
|
182
|
+
shown.push("Alt+↑ 取回 · Esc 全部回填并中断");
|
|
183
|
+
queueText.setText(shown.map((l) => theme.fg("dim", l)).join("\n"));
|
|
184
|
+
render();
|
|
185
|
+
};
|
|
186
|
+
// ---- 会话事件 -------------------------------------------------------------
|
|
187
|
+
const onEvent = (event) => {
|
|
188
|
+
switch (event.type) {
|
|
189
|
+
case "agent_start":
|
|
190
|
+
running = true;
|
|
191
|
+
syncLoader();
|
|
192
|
+
return;
|
|
193
|
+
case "agent_settled":
|
|
194
|
+
running = false;
|
|
195
|
+
if (event.warning !== undefined)
|
|
196
|
+
view.addNotice("warn", event.warning);
|
|
197
|
+
status.refresh();
|
|
198
|
+
syncLoader();
|
|
199
|
+
return;
|
|
200
|
+
case "message_start": {
|
|
201
|
+
const message = event.message;
|
|
202
|
+
if (message.role === "user")
|
|
203
|
+
view.addUser(message);
|
|
204
|
+
else if (message.role === "assistant")
|
|
205
|
+
view.startAssistant(message);
|
|
206
|
+
else if (message.role === "custom" && message.display) {
|
|
207
|
+
view.addNotice("info", typeof message.content === "string" ? message.content : "");
|
|
208
|
+
}
|
|
209
|
+
render();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
case "message_update":
|
|
213
|
+
view.updateAssistant(event.message);
|
|
214
|
+
render();
|
|
215
|
+
return;
|
|
216
|
+
case "message_end":
|
|
217
|
+
if (event.message.role === "assistant") {
|
|
218
|
+
view.endAssistant(event.message);
|
|
219
|
+
status.refresh();
|
|
220
|
+
}
|
|
221
|
+
render();
|
|
222
|
+
return;
|
|
223
|
+
case "tool_execution_start": {
|
|
224
|
+
const started = tools.start(event);
|
|
225
|
+
if (started.topLevel)
|
|
226
|
+
view.add(started.view);
|
|
227
|
+
render();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
case "tool_execution_update":
|
|
231
|
+
tools.update(event.toolCallId, event.partial);
|
|
232
|
+
render();
|
|
233
|
+
return;
|
|
234
|
+
case "tool_execution_end":
|
|
235
|
+
tools.end(event.toolCallId, event.result, event.isError);
|
|
236
|
+
render();
|
|
237
|
+
return;
|
|
238
|
+
case "queue_update":
|
|
239
|
+
setQueue(event.steering, event.followUp);
|
|
240
|
+
return;
|
|
241
|
+
case "compaction_start":
|
|
242
|
+
compacting = true;
|
|
243
|
+
syncLoader();
|
|
244
|
+
return;
|
|
245
|
+
case "compaction_end":
|
|
246
|
+
compacting = false;
|
|
247
|
+
if (event.result !== undefined)
|
|
248
|
+
view.addCompaction(event.result);
|
|
249
|
+
else if (event.error !== undefined)
|
|
250
|
+
view.addNotice("error", `压缩失败:${event.error}`);
|
|
251
|
+
else if (event.aborted)
|
|
252
|
+
view.addNotice("info", "压缩已取消");
|
|
253
|
+
status.refresh();
|
|
254
|
+
syncLoader();
|
|
255
|
+
return;
|
|
256
|
+
case "auto_retry_start":
|
|
257
|
+
view.addRetry(event.attempt, event.maxAttempts, event.delayMs, event.errorMessage);
|
|
258
|
+
render();
|
|
259
|
+
return;
|
|
260
|
+
case "auto_retry_end":
|
|
261
|
+
if (!event.success)
|
|
262
|
+
view.addRetryFailed(event.finalError);
|
|
263
|
+
render();
|
|
264
|
+
return;
|
|
265
|
+
case "cache_miss":
|
|
266
|
+
case "context_pressure": {
|
|
267
|
+
const shown = cacheEventNotice(event, cacheNoticesEnabled(session));
|
|
268
|
+
if (shown !== undefined)
|
|
269
|
+
view.addNotice(shown.level, shown.text);
|
|
270
|
+
status.refresh();
|
|
271
|
+
render();
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
case "cache_warm":
|
|
275
|
+
case "permission_mode_changed":
|
|
276
|
+
case "model_changed":
|
|
277
|
+
case "thinking_level_changed":
|
|
278
|
+
case "session_changed":
|
|
279
|
+
status.refresh();
|
|
280
|
+
render();
|
|
281
|
+
return;
|
|
282
|
+
default:
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
let unsubscribe = session.subscribe(onEvent);
|
|
287
|
+
const startup = buildStartupScreen(runtime);
|
|
288
|
+
/** 清空消息区(切换会话、/tree):只留标题行。 */
|
|
289
|
+
const resetView = () => {
|
|
290
|
+
view.reset();
|
|
291
|
+
view.addHeader(startup.slice(0, 1));
|
|
292
|
+
};
|
|
293
|
+
const replay = () => {
|
|
294
|
+
view.replay(session.messages, {
|
|
295
|
+
tool: (call, result) => tools.completed(call.id, call.name, call.arguments, result === undefined
|
|
296
|
+
? { content: "(没有结果)", isError: true }
|
|
297
|
+
: {
|
|
298
|
+
content: result.content,
|
|
299
|
+
isError: result.isError,
|
|
300
|
+
...(result.details !== undefined ? { details: result.details } : {}),
|
|
301
|
+
}, result?.isError ?? true),
|
|
302
|
+
});
|
|
303
|
+
};
|
|
304
|
+
const rebind = (next, reason) => {
|
|
305
|
+
unsubscribe();
|
|
306
|
+
session = next;
|
|
307
|
+
unsubscribe = next.subscribe(onEvent);
|
|
308
|
+
resetView();
|
|
309
|
+
tools.clear();
|
|
310
|
+
replay();
|
|
311
|
+
running = false;
|
|
312
|
+
compacting = false;
|
|
313
|
+
setQueue([], []);
|
|
314
|
+
status.refresh();
|
|
315
|
+
syncLoader();
|
|
316
|
+
if (next instanceof AgentSessionImpl)
|
|
317
|
+
next.announceStart(reason);
|
|
318
|
+
};
|
|
319
|
+
// ---- 提交与命令 -----------------------------------------------------------
|
|
320
|
+
/**
|
|
321
|
+
* 空闲时发提示。agent_settled 之后会话周期还要一个微任务才结束,所以先 waitForIdle;
|
|
322
|
+
* 仍然撞上运行中(极少)就排到本轮之后。
|
|
323
|
+
*/
|
|
324
|
+
const startPrompt = (text) => {
|
|
325
|
+
const target = session;
|
|
326
|
+
void target
|
|
327
|
+
.waitForIdle()
|
|
328
|
+
.then(async () => {
|
|
329
|
+
const images = await promptImages(text, [], target.state.cwd, sessionModel(runtime.providers, target));
|
|
330
|
+
return target.prompt(text, images.length > 0 ? { images } : {});
|
|
331
|
+
})
|
|
332
|
+
.catch((error) => {
|
|
333
|
+
if (isAmaError(error) && error.code === "busy") {
|
|
334
|
+
void target.followUp(text).catch(() => undefined);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (isAmaError(error) && error.code === "prompt_blocked")
|
|
338
|
+
view.addHookBlocked(error.message);
|
|
339
|
+
else
|
|
340
|
+
notice("error", error instanceof Error ? error.message : String(error));
|
|
341
|
+
render();
|
|
342
|
+
});
|
|
343
|
+
};
|
|
344
|
+
const commandUi = {
|
|
345
|
+
runtime,
|
|
346
|
+
session: () => session,
|
|
347
|
+
async switchSession(request) {
|
|
348
|
+
const next = await switchSession(runtime, request);
|
|
349
|
+
rebind(next, request.kind === "new" ? "new" : request.kind === "fork" ? "fork" : "resume");
|
|
350
|
+
return next;
|
|
351
|
+
},
|
|
352
|
+
pick: (spec) => openPicker({
|
|
353
|
+
theme,
|
|
354
|
+
keybindings: keys,
|
|
355
|
+
showOverlay: (c, o) => tui.showOverlay(c, o),
|
|
356
|
+
columns: () => terminal.columns,
|
|
357
|
+
}, spec),
|
|
358
|
+
notice,
|
|
359
|
+
setEditorText: (text) => {
|
|
360
|
+
editor.setText(text);
|
|
361
|
+
render();
|
|
362
|
+
},
|
|
363
|
+
prompt: (text) => startPrompt(text),
|
|
364
|
+
reload: () => {
|
|
365
|
+
resetView();
|
|
366
|
+
tools.clear();
|
|
367
|
+
replay();
|
|
368
|
+
status.refresh();
|
|
369
|
+
render();
|
|
370
|
+
},
|
|
371
|
+
exit: (code) => exit(code),
|
|
372
|
+
now,
|
|
373
|
+
};
|
|
374
|
+
const runCommand = (line) => runInteractiveCommand(line, commandUi).finally(() => {
|
|
375
|
+
status.refresh();
|
|
376
|
+
render();
|
|
377
|
+
});
|
|
378
|
+
function submit(text, via) {
|
|
379
|
+
if (text.trim() === "")
|
|
380
|
+
return;
|
|
381
|
+
if (text.trimStart().startsWith("/")) {
|
|
382
|
+
void runCommand(text.trim()).then((handled) => {
|
|
383
|
+
if (!handled)
|
|
384
|
+
send(text, via);
|
|
385
|
+
});
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
send(text, via);
|
|
389
|
+
}
|
|
390
|
+
function send(text, via) {
|
|
391
|
+
if (!running)
|
|
392
|
+
startPrompt(text);
|
|
393
|
+
else if (via === "followUp")
|
|
394
|
+
void session.followUp(text).catch(() => undefined);
|
|
395
|
+
else
|
|
396
|
+
void session.steer(text).catch(() => undefined);
|
|
397
|
+
}
|
|
398
|
+
// ---- 键位 -----------------------------------------------------------------
|
|
399
|
+
tui.addInputListener(createKeyDispatch({
|
|
400
|
+
keys,
|
|
401
|
+
editor,
|
|
402
|
+
tools,
|
|
403
|
+
status,
|
|
404
|
+
session: () => session,
|
|
405
|
+
inactive: () => finished || tui.hasOverlay,
|
|
406
|
+
busy: () => running || compacting,
|
|
407
|
+
now,
|
|
408
|
+
showHint,
|
|
409
|
+
submit: (text, via) => submit(text, via),
|
|
410
|
+
runCommand: (line) => void runCommand(line),
|
|
411
|
+
exit: (code) => exit(code),
|
|
412
|
+
}));
|
|
413
|
+
// ---- 晚绑定 ---------------------------------------------------------------
|
|
414
|
+
const broker = new ApprovalDialogBroker({
|
|
415
|
+
theme,
|
|
416
|
+
keybindings: keys,
|
|
417
|
+
cwd: session.state.cwd,
|
|
418
|
+
permissionMode: () => session.state.permissionMode,
|
|
419
|
+
showOverlay: (component) => tui.showOverlay(component, { anchor: "bottom" }),
|
|
420
|
+
onOpen: () => {
|
|
421
|
+
editor.disableSubmit = true;
|
|
422
|
+
render();
|
|
423
|
+
},
|
|
424
|
+
onClose: () => {
|
|
425
|
+
editor.disableSubmit = false;
|
|
426
|
+
render();
|
|
427
|
+
},
|
|
428
|
+
report: (request, outcome) => {
|
|
429
|
+
if (outcome === "deny" || outcome === "cancelled") {
|
|
430
|
+
notice(outcome === "deny" ? "info" : "warn", approvalOutcomeText(request, outcome));
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
});
|
|
434
|
+
// ---- 启动与退出 -----------------------------------------------------------
|
|
435
|
+
let offSignals = () => undefined;
|
|
436
|
+
function exit(code) {
|
|
437
|
+
if (finished)
|
|
438
|
+
return;
|
|
439
|
+
finished = true;
|
|
440
|
+
if (hintTimer !== undefined)
|
|
441
|
+
clearTimeout(hintTimer);
|
|
442
|
+
hint.setText("");
|
|
443
|
+
loader.stop();
|
|
444
|
+
loaderSlot.clear();
|
|
445
|
+
offSignals();
|
|
446
|
+
runtime.approvals.setUiBroker(undefined);
|
|
447
|
+
runtime.notifier.set(undefined);
|
|
448
|
+
tui.stop();
|
|
449
|
+
const active = session;
|
|
450
|
+
void active
|
|
451
|
+
.abort()
|
|
452
|
+
.catch(() => undefined)
|
|
453
|
+
.finally(() => {
|
|
454
|
+
unsubscribe();
|
|
455
|
+
resolveExit(code);
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
view.addHeader(startup);
|
|
459
|
+
for (const warning of startupWarnings)
|
|
460
|
+
view.addNotice("warn", warning);
|
|
461
|
+
replay();
|
|
462
|
+
status.refresh();
|
|
463
|
+
return new Promise((resolve, reject) => {
|
|
464
|
+
resolveExit = resolve;
|
|
465
|
+
try {
|
|
466
|
+
tui.start();
|
|
467
|
+
}
|
|
468
|
+
catch (error) {
|
|
469
|
+
unsubscribe();
|
|
470
|
+
reject(new AmaError("terminal_init_failed", `终端初始化失败:${error instanceof Error ? error.message : String(error)}`, { cause: error }));
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
runtime.approvals.setUiBroker(broker);
|
|
474
|
+
runtime.notifier.set((message, level) => notice(level, message));
|
|
475
|
+
offSignals = onTerminationSignals((code) => exit(code));
|
|
476
|
+
tui.setFocus(editor);
|
|
477
|
+
options.onReady?.({
|
|
478
|
+
tui,
|
|
479
|
+
editor,
|
|
480
|
+
view,
|
|
481
|
+
tools,
|
|
482
|
+
status,
|
|
483
|
+
session: () => session,
|
|
484
|
+
exit,
|
|
485
|
+
});
|
|
486
|
+
if (context.prompt !== undefined && context.prompt.trim() !== "") {
|
|
487
|
+
editor.addToHistory(context.prompt);
|
|
488
|
+
submit(context.prompt, "enter");
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
}
|
|
@@ -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
|
+
}
|