@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
package/dist/tui/tui.js
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TUI 主类(设计 §12.1、§12.2,主屏 regular 模式)。[B4]
|
|
3
|
+
*
|
|
4
|
+
* 组件树(自身是纵向 Container)+ 焦点 + 覆盖层 + 差分渲染:
|
|
5
|
+
* 1. `requestRender()` 合并到 `process.nextTick`,两帧最小间隔 16 ms;键盘输入触发的渲染绕过节流。
|
|
6
|
+
* 2. 渲染组件树 → 合成覆盖层 → 提取并剔除 CURSOR_MARKER → 每行截断到宽度、含样式的行尾补重置。
|
|
7
|
+
* 3. 首帧全量;宽度 / 高度变化全量(回到屏幕左上清屏,只画最后一屏)。
|
|
8
|
+
* 4. 否则找首末变化行只重写该区间;首变化行已滚出视口 → 从视口顶清到屏底重画视口。
|
|
9
|
+
* 已滚出终端顶部的历史行不再重绘(它们在终端回滚里)。
|
|
10
|
+
* 5. 每帧写入包在同步输出 `?2026h … ?2026l` 内,按 64 KiB 分块写。
|
|
11
|
+
* 6. 记录硬件光标所在内容行;最后把硬件光标移到获焦组件的光标处(便于输入法候选窗)。
|
|
12
|
+
* 7. `stop()`:光标移到内容末尾下一行、显示光标、交还终端(关括号粘贴、cooked 模式);不清屏。
|
|
13
|
+
*
|
|
14
|
+
* 首帧前把当前行滚到屏幕顶(输出 height-1 个换行再上移),原屏内容进入回滚而不是被清掉;
|
|
15
|
+
* 这样内容起点与屏幕顶对齐,相对光标移动不会越过屏幕顶部。
|
|
16
|
+
*/
|
|
17
|
+
import { CURSOR_MARKER, isFocusable } from "./component.js";
|
|
18
|
+
import { SGR_RESET, truncateToWidth, visibleWidth } from "./ansi.js";
|
|
19
|
+
import { Container } from "./components/container.js";
|
|
20
|
+
import { compositeOverlays } from "./components/overlay.js";
|
|
21
|
+
import { HIDE_CURSOR, SHOW_CURSOR } from "./terminal.js";
|
|
22
|
+
export const SYNC_BEGIN = "\x1b[?2026h";
|
|
23
|
+
export const SYNC_END = "\x1b[?2026l";
|
|
24
|
+
export const WRITE_CHUNK_SIZE = 64 * 1024;
|
|
25
|
+
export const MIN_RENDER_INTERVAL_MS = 16;
|
|
26
|
+
/** 按 64 KiB 分块写;不切开代理对。 */
|
|
27
|
+
class ChunkWriter {
|
|
28
|
+
terminal;
|
|
29
|
+
buffer = "";
|
|
30
|
+
constructor(terminal) {
|
|
31
|
+
this.terminal = terminal;
|
|
32
|
+
}
|
|
33
|
+
push(data) {
|
|
34
|
+
this.buffer += data;
|
|
35
|
+
while (this.buffer.length >= WRITE_CHUNK_SIZE) {
|
|
36
|
+
let end = WRITE_CHUNK_SIZE;
|
|
37
|
+
const code = this.buffer.charCodeAt(end - 1);
|
|
38
|
+
if (code >= 0xd800 && code <= 0xdbff)
|
|
39
|
+
end--;
|
|
40
|
+
this.terminal.write(this.buffer.slice(0, end));
|
|
41
|
+
this.buffer = this.buffer.slice(end);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
flush() {
|
|
45
|
+
if (this.buffer.length > 0)
|
|
46
|
+
this.terminal.write(this.buffer);
|
|
47
|
+
this.buffer = "";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export class TUI extends Container {
|
|
51
|
+
terminal;
|
|
52
|
+
options;
|
|
53
|
+
stats = { renders: 0, fullRedraws: 0 };
|
|
54
|
+
started = false;
|
|
55
|
+
focused = null;
|
|
56
|
+
overlays = [];
|
|
57
|
+
listeners = [];
|
|
58
|
+
minInterval;
|
|
59
|
+
now;
|
|
60
|
+
tickScheduled = false;
|
|
61
|
+
renderGeneration = 0;
|
|
62
|
+
timer = null;
|
|
63
|
+
lastRenderAt = Number.NEGATIVE_INFINITY;
|
|
64
|
+
// 差分状态(内容行坐标)
|
|
65
|
+
prevLines = null;
|
|
66
|
+
prevWidth = 0;
|
|
67
|
+
prevHeight = 0;
|
|
68
|
+
/** 硬件光标所在内容行。 */
|
|
69
|
+
hwRow = 0;
|
|
70
|
+
/** 终端上已存在的内容行数(自上次全量起写到过的最大行 + 1)。 */
|
|
71
|
+
bufferRows = 0;
|
|
72
|
+
/** 可达的最小内容行(更早的行在回滚里或从未画过)。 */
|
|
73
|
+
topLimit = 0;
|
|
74
|
+
cursorShown = null;
|
|
75
|
+
constructor(terminal, options = {}) {
|
|
76
|
+
super();
|
|
77
|
+
this.terminal = terminal;
|
|
78
|
+
this.options = options;
|
|
79
|
+
this.minInterval = options.minRenderIntervalMs ?? MIN_RENDER_INTERVAL_MS;
|
|
80
|
+
this.now = options.now ?? Date.now;
|
|
81
|
+
}
|
|
82
|
+
get isStarted() {
|
|
83
|
+
return this.started;
|
|
84
|
+
}
|
|
85
|
+
start() {
|
|
86
|
+
if (this.started)
|
|
87
|
+
return;
|
|
88
|
+
this.started = true;
|
|
89
|
+
this.terminal.start((data) => this.handleInput(data), () => this.requestRender(true));
|
|
90
|
+
this.requestRender(true);
|
|
91
|
+
}
|
|
92
|
+
/** 画完最后一帧,光标移到内容末尾下一行,交还终端;不清屏。 */
|
|
93
|
+
stop() {
|
|
94
|
+
if (!this.started)
|
|
95
|
+
return;
|
|
96
|
+
this.cancelScheduled();
|
|
97
|
+
this.doRender();
|
|
98
|
+
this.started = false;
|
|
99
|
+
const out = new ChunkWriter(this.terminal);
|
|
100
|
+
if (this.prevLines !== null && this.prevLines.length > 0) {
|
|
101
|
+
out.push(this.moveTo(this.prevLines.length - 1));
|
|
102
|
+
out.push("\r\n");
|
|
103
|
+
}
|
|
104
|
+
out.push(SHOW_CURSOR);
|
|
105
|
+
out.flush();
|
|
106
|
+
this.terminal.stop();
|
|
107
|
+
}
|
|
108
|
+
getFocus() {
|
|
109
|
+
return this.focused;
|
|
110
|
+
}
|
|
111
|
+
setFocus(component) {
|
|
112
|
+
if (this.focused === component)
|
|
113
|
+
return;
|
|
114
|
+
if (this.focused && isFocusable(this.focused))
|
|
115
|
+
this.focused.focused = false;
|
|
116
|
+
this.focused = component;
|
|
117
|
+
if (component && isFocusable(component))
|
|
118
|
+
component.focused = true;
|
|
119
|
+
this.requestRender();
|
|
120
|
+
}
|
|
121
|
+
/** 全局输入监听(先于获焦组件);返回取消函数。 */
|
|
122
|
+
addInputListener(listener) {
|
|
123
|
+
this.listeners.push(listener);
|
|
124
|
+
return () => {
|
|
125
|
+
const index = this.listeners.indexOf(listener);
|
|
126
|
+
if (index !== -1)
|
|
127
|
+
this.listeners.splice(index, 1);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
showOverlay(component, options = {}) {
|
|
131
|
+
const entry = { component, options, previousFocus: this.focused };
|
|
132
|
+
this.overlays.push(entry);
|
|
133
|
+
this.setFocus(component);
|
|
134
|
+
this.requestRender();
|
|
135
|
+
const tui = this;
|
|
136
|
+
return {
|
|
137
|
+
hide() {
|
|
138
|
+
const index = tui.overlays.indexOf(entry);
|
|
139
|
+
if (index === -1)
|
|
140
|
+
return;
|
|
141
|
+
tui.overlays.splice(index, 1);
|
|
142
|
+
if (tui.focused === component)
|
|
143
|
+
tui.setFocus(entry.previousFocus);
|
|
144
|
+
tui.requestRender();
|
|
145
|
+
},
|
|
146
|
+
focus() {
|
|
147
|
+
if (tui.overlays.includes(entry))
|
|
148
|
+
tui.setFocus(component);
|
|
149
|
+
},
|
|
150
|
+
get visible() {
|
|
151
|
+
return tui.overlays.includes(entry);
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
get hasOverlay() {
|
|
156
|
+
return this.overlays.length > 0;
|
|
157
|
+
}
|
|
158
|
+
/** 原始输入:监听器 → 获焦组件 → 立即渲染(绕过节流)。 */
|
|
159
|
+
handleInput(data) {
|
|
160
|
+
for (const listener of [...this.listeners]) {
|
|
161
|
+
if (listener(data) === true) {
|
|
162
|
+
this.requestRender(true);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
this.focused?.handleInput?.(data);
|
|
167
|
+
this.requestRender(true);
|
|
168
|
+
}
|
|
169
|
+
/** 请求渲染;同一 tick 内合并。immediate 用于键盘输入与 resize,不受 16 ms 节流。 */
|
|
170
|
+
requestRender(immediate = false) {
|
|
171
|
+
if (!this.started || this.tickScheduled)
|
|
172
|
+
return;
|
|
173
|
+
const elapsed = this.now() - this.lastRenderAt;
|
|
174
|
+
if (immediate || elapsed >= this.minInterval) {
|
|
175
|
+
if (this.timer !== null) {
|
|
176
|
+
clearTimeout(this.timer);
|
|
177
|
+
this.timer = null;
|
|
178
|
+
}
|
|
179
|
+
this.tickScheduled = true;
|
|
180
|
+
const generation = ++this.renderGeneration;
|
|
181
|
+
process.nextTick(() => {
|
|
182
|
+
if (generation !== this.renderGeneration)
|
|
183
|
+
return;
|
|
184
|
+
this.tickScheduled = false;
|
|
185
|
+
this.doRender();
|
|
186
|
+
});
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (this.timer !== null)
|
|
190
|
+
return;
|
|
191
|
+
this.timer = setTimeout(() => {
|
|
192
|
+
this.timer = null;
|
|
193
|
+
this.requestRender(true);
|
|
194
|
+
}, this.minInterval - elapsed);
|
|
195
|
+
}
|
|
196
|
+
/** 同步渲染一帧(测试与需要立刻落屏的场合)。 */
|
|
197
|
+
renderNow() {
|
|
198
|
+
this.cancelScheduled();
|
|
199
|
+
this.doRender();
|
|
200
|
+
}
|
|
201
|
+
/** 丢弃差分状态,下一帧按 resize 方式全量重画(例如外部程序写乱了屏幕)。 */
|
|
202
|
+
forceFullRedraw() {
|
|
203
|
+
if (this.prevLines !== null)
|
|
204
|
+
this.prevWidth = -1;
|
|
205
|
+
this.requestRender(true);
|
|
206
|
+
}
|
|
207
|
+
cancelScheduled() {
|
|
208
|
+
if (this.timer !== null) {
|
|
209
|
+
clearTimeout(this.timer);
|
|
210
|
+
this.timer = null;
|
|
211
|
+
}
|
|
212
|
+
this.tickScheduled = false;
|
|
213
|
+
this.renderGeneration++;
|
|
214
|
+
}
|
|
215
|
+
/** 组件树 + 覆盖层 → 规范化行与光标位置。 */
|
|
216
|
+
compose(width, height) {
|
|
217
|
+
let lines = this.render(width);
|
|
218
|
+
if (this.overlays.length > 0)
|
|
219
|
+
lines = compositeOverlays(lines, this.overlays, width, height);
|
|
220
|
+
let cursor = null;
|
|
221
|
+
const out = new Array(lines.length);
|
|
222
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
223
|
+
let line = lines[i];
|
|
224
|
+
const at = line.indexOf(CURSOR_MARKER);
|
|
225
|
+
if (at !== -1) {
|
|
226
|
+
if (cursor === null)
|
|
227
|
+
cursor = { row: i, col: visibleWidth(line.slice(0, at)) };
|
|
228
|
+
line = line.split(CURSOR_MARKER).join("");
|
|
229
|
+
}
|
|
230
|
+
if (visibleWidth(line) > width)
|
|
231
|
+
line = truncateToWidth(line, width, "");
|
|
232
|
+
if (line.includes("\x1b") && !line.endsWith(SGR_RESET))
|
|
233
|
+
line += SGR_RESET;
|
|
234
|
+
out[i] = line;
|
|
235
|
+
}
|
|
236
|
+
return { lines: out, cursor };
|
|
237
|
+
}
|
|
238
|
+
viewportTop(height) {
|
|
239
|
+
return Math.max(this.topLimit, this.bufferRows - height);
|
|
240
|
+
}
|
|
241
|
+
/** 从 hwRow 移到内容行 row 的行首;越过已有行时用换行(会滚屏)。 */
|
|
242
|
+
moveTo(row) {
|
|
243
|
+
let seq = "";
|
|
244
|
+
if (row < this.hwRow) {
|
|
245
|
+
seq += `\x1b[${this.hwRow - row}A`;
|
|
246
|
+
}
|
|
247
|
+
else if (row > this.hwRow) {
|
|
248
|
+
const lastExisting = Math.max(this.hwRow, this.bufferRows - 1);
|
|
249
|
+
const down = Math.min(row, lastExisting) - this.hwRow;
|
|
250
|
+
if (down > 0)
|
|
251
|
+
seq += `\x1b[${down}B`;
|
|
252
|
+
const extra = row - lastExisting;
|
|
253
|
+
if (extra > 0) {
|
|
254
|
+
seq += "\r\n".repeat(extra);
|
|
255
|
+
this.bufferRows = row + 1;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
this.hwRow = row;
|
|
259
|
+
return seq + "\r";
|
|
260
|
+
}
|
|
261
|
+
doRender() {
|
|
262
|
+
if (!this.started)
|
|
263
|
+
return;
|
|
264
|
+
const width = this.terminal.columns;
|
|
265
|
+
const height = this.terminal.rows;
|
|
266
|
+
const { lines, cursor } = this.compose(width, height);
|
|
267
|
+
const out = new ChunkWriter(this.terminal);
|
|
268
|
+
out.push(SYNC_BEGIN);
|
|
269
|
+
const prev = this.prevLines;
|
|
270
|
+
if (prev === null) {
|
|
271
|
+
this.fullFirst(out, lines, height);
|
|
272
|
+
}
|
|
273
|
+
else if (width !== this.prevWidth || height !== this.prevHeight) {
|
|
274
|
+
this.fullResize(out, lines, height);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
this.diff(out, prev, lines, height);
|
|
278
|
+
}
|
|
279
|
+
this.placeCursor(out, cursor, lines.length, height);
|
|
280
|
+
out.push(SYNC_END);
|
|
281
|
+
out.flush();
|
|
282
|
+
this.prevLines = lines;
|
|
283
|
+
this.prevWidth = width;
|
|
284
|
+
this.prevHeight = height;
|
|
285
|
+
this.lastRenderAt = this.now();
|
|
286
|
+
this.stats.renders++;
|
|
287
|
+
}
|
|
288
|
+
writeRows(out, lines, from) {
|
|
289
|
+
for (let i = from; i < lines.length; i++) {
|
|
290
|
+
if (i > from)
|
|
291
|
+
out.push("\r\n");
|
|
292
|
+
out.push("\x1b[2K" + lines[i]);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
fullFirst(out, lines, height) {
|
|
296
|
+
this.stats.fullRedraws++;
|
|
297
|
+
if (this.options.anchorToTop !== false && height > 1) {
|
|
298
|
+
out.push("\r" + "\n".repeat(height - 1) + `\x1b[${height - 1}A`);
|
|
299
|
+
}
|
|
300
|
+
out.push("\r");
|
|
301
|
+
this.writeRows(out, lines, 0);
|
|
302
|
+
this.topLimit = 0;
|
|
303
|
+
this.bufferRows = Math.max(1, lines.length);
|
|
304
|
+
this.hwRow = Math.max(0, lines.length - 1);
|
|
305
|
+
}
|
|
306
|
+
fullResize(out, lines, height) {
|
|
307
|
+
this.stats.fullRedraws++;
|
|
308
|
+
const start = Math.max(0, lines.length - height);
|
|
309
|
+
out.push("\x1b[H\x1b[2J");
|
|
310
|
+
this.writeRows(out, lines, start);
|
|
311
|
+
this.topLimit = start;
|
|
312
|
+
this.bufferRows = Math.max(start + 1, lines.length);
|
|
313
|
+
this.hwRow = Math.max(start, lines.length - 1);
|
|
314
|
+
}
|
|
315
|
+
/** 首变化行在视口之上:从视口顶清到屏底,重画视口(更早的行留在回滚里)。 */
|
|
316
|
+
fullViewport(out, lines, height) {
|
|
317
|
+
this.stats.fullRedraws++;
|
|
318
|
+
const top = this.viewportTop(height);
|
|
319
|
+
out.push(this.moveTo(top) + "\x1b[J");
|
|
320
|
+
const start = lines.length > top ? top : Math.max(0, lines.length - height);
|
|
321
|
+
// 内容缩到视口顶以上:视口顶的屏幕行改为对应 start
|
|
322
|
+
this.writeRows(out, lines, start);
|
|
323
|
+
this.topLimit = start;
|
|
324
|
+
this.bufferRows = Math.max(start + 1, lines.length);
|
|
325
|
+
this.hwRow = Math.max(start, lines.length - 1);
|
|
326
|
+
}
|
|
327
|
+
diff(out, prev, lines, height) {
|
|
328
|
+
let first = -1;
|
|
329
|
+
let last = -1;
|
|
330
|
+
const max = Math.max(prev.length, lines.length);
|
|
331
|
+
for (let i = 0; i < max; i++) {
|
|
332
|
+
if ((prev[i] ?? "") !== (lines[i] ?? "") || i >= prev.length !== i >= lines.length) {
|
|
333
|
+
if (first === -1)
|
|
334
|
+
first = i;
|
|
335
|
+
last = i;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (first === -1)
|
|
339
|
+
return;
|
|
340
|
+
if (first < this.viewportTop(height)) {
|
|
341
|
+
this.fullViewport(out, lines, height);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const renderEnd = Math.min(last, lines.length - 1);
|
|
345
|
+
for (let i = first; i <= renderEnd; i++)
|
|
346
|
+
out.push(this.moveTo(i) + "\x1b[2K" + lines[i]);
|
|
347
|
+
// 内容变短:清掉多出来的旧行(它们仍在屏幕上)
|
|
348
|
+
for (let i = Math.max(first, lines.length); i < prev.length; i++) {
|
|
349
|
+
out.push(this.moveTo(i) + "\x1b[2K");
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
placeCursor(out, cursor, total, height) {
|
|
353
|
+
const visible = cursor !== null && cursor.row < total && cursor.row >= this.viewportTop(height);
|
|
354
|
+
if (visible) {
|
|
355
|
+
out.push(this.moveTo(cursor.row) + `\x1b[${cursor.col + 1}G`);
|
|
356
|
+
}
|
|
357
|
+
const show = visible && this.options.showHardwareCursor === true;
|
|
358
|
+
if (this.cursorShown !== show) {
|
|
359
|
+
out.push(show ? SHOW_CURSOR : HIDE_CURSOR);
|
|
360
|
+
this.cursorShown = show;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 极简终端屏幕模拟(供 MemoryTerminal 还原屏幕、做帧黄金测试)。[B4]
|
|
3
|
+
*
|
|
4
|
+
* 只实现 TUI 实际会写出的子集:可打印字素(按列宽占格,含延迟换行语义)、`\r` `\n` `\b`、
|
|
5
|
+
* CSI A/B/C/D/E/F/G/H/f/J/K/m、`?25` `?2004` `?2026` 模式开关;OSC / APC / DCS 跳过;样式不记录。
|
|
6
|
+
* 滚出屏幕顶部的行进入回滚区(scrollback)。改尺寸不做重排:行截断 / 补齐,高度缩小时顶部行进回滚。
|
|
7
|
+
*/
|
|
8
|
+
export interface ScreenModes {
|
|
9
|
+
cursorVisible: boolean;
|
|
10
|
+
bracketedPaste: boolean;
|
|
11
|
+
/** 当前未闭合的同步输出层数(?2026h 计 +1,?2026l 归零)。 */
|
|
12
|
+
synchronized: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare class VirtualScreen {
|
|
15
|
+
private cols;
|
|
16
|
+
private rows;
|
|
17
|
+
private grid;
|
|
18
|
+
private history;
|
|
19
|
+
private x;
|
|
20
|
+
private y;
|
|
21
|
+
private pendingWrap;
|
|
22
|
+
readonly modes: ScreenModes;
|
|
23
|
+
constructor(cols: number, rows: number);
|
|
24
|
+
get columns(): number;
|
|
25
|
+
get height(): number;
|
|
26
|
+
get cursor(): {
|
|
27
|
+
row: number;
|
|
28
|
+
col: number;
|
|
29
|
+
};
|
|
30
|
+
/** 可见屏幕各行(右侧空白去掉)。 */
|
|
31
|
+
viewport(): string[];
|
|
32
|
+
/** 已滚出顶部的行(旧 → 新)。 */
|
|
33
|
+
scrollback(): string[];
|
|
34
|
+
/** 回滚 + 屏幕,去掉屏幕底部的空行。 */
|
|
35
|
+
transcript(): string[];
|
|
36
|
+
resize(cols: number, rows: number): void;
|
|
37
|
+
write(data: string): void;
|
|
38
|
+
private blankRow;
|
|
39
|
+
private lineFeed;
|
|
40
|
+
private print;
|
|
41
|
+
private control;
|
|
42
|
+
private escape;
|
|
43
|
+
private privateMode;
|
|
44
|
+
private eraseLine;
|
|
45
|
+
private eraseDisplay;
|
|
46
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 极简终端屏幕模拟(供 MemoryTerminal 还原屏幕、做帧黄金测试)。[B4]
|
|
3
|
+
*
|
|
4
|
+
* 只实现 TUI 实际会写出的子集:可打印字素(按列宽占格,含延迟换行语义)、`\r` `\n` `\b`、
|
|
5
|
+
* CSI A/B/C/D/E/F/G/H/f/J/K/m、`?25` `?2004` `?2026` 模式开关;OSC / APC / DCS 跳过;样式不记录。
|
|
6
|
+
* 滚出屏幕顶部的行进入回滚区(scrollback)。改尺寸不做重排:行截断 / 补齐,高度缩小时顶部行进回滚。
|
|
7
|
+
*/
|
|
8
|
+
import { escapeLengthAt, graphemeWidth } from "./ansi.js";
|
|
9
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
10
|
+
export class VirtualScreen {
|
|
11
|
+
cols;
|
|
12
|
+
rows;
|
|
13
|
+
grid = [];
|
|
14
|
+
history = [];
|
|
15
|
+
x = 0;
|
|
16
|
+
y = 0;
|
|
17
|
+
pendingWrap = false;
|
|
18
|
+
modes = { cursorVisible: true, bracketedPaste: false, synchronized: false };
|
|
19
|
+
constructor(cols, rows) {
|
|
20
|
+
this.cols = cols;
|
|
21
|
+
this.rows = rows;
|
|
22
|
+
for (let i = 0; i < rows; i++)
|
|
23
|
+
this.grid.push(this.blankRow());
|
|
24
|
+
}
|
|
25
|
+
get columns() {
|
|
26
|
+
return this.cols;
|
|
27
|
+
}
|
|
28
|
+
get height() {
|
|
29
|
+
return this.rows;
|
|
30
|
+
}
|
|
31
|
+
get cursor() {
|
|
32
|
+
return { row: this.y, col: this.x };
|
|
33
|
+
}
|
|
34
|
+
/** 可见屏幕各行(右侧空白去掉)。 */
|
|
35
|
+
viewport() {
|
|
36
|
+
return this.grid.map(rowText);
|
|
37
|
+
}
|
|
38
|
+
/** 已滚出顶部的行(旧 → 新)。 */
|
|
39
|
+
scrollback() {
|
|
40
|
+
return [...this.history];
|
|
41
|
+
}
|
|
42
|
+
/** 回滚 + 屏幕,去掉屏幕底部的空行。 */
|
|
43
|
+
transcript() {
|
|
44
|
+
const all = [...this.history, ...this.viewport()];
|
|
45
|
+
while (all.length > 0 && all[all.length - 1] === "")
|
|
46
|
+
all.pop();
|
|
47
|
+
return all;
|
|
48
|
+
}
|
|
49
|
+
resize(cols, rows) {
|
|
50
|
+
for (let i = 0; i < this.grid.length; i++) {
|
|
51
|
+
const row = this.grid[i];
|
|
52
|
+
if (cols < row.length) {
|
|
53
|
+
row.length = cols;
|
|
54
|
+
// 被截断的宽字符首格
|
|
55
|
+
if (cols > 0 && graphemeWidth(row[cols - 1]) === 2)
|
|
56
|
+
row[cols - 1] = " ";
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
while (row.length < cols)
|
|
60
|
+
row.push(" ");
|
|
61
|
+
}
|
|
62
|
+
this.cols = cols;
|
|
63
|
+
if (rows < this.rows) {
|
|
64
|
+
const overflow = Math.max(0, this.y - (rows - 1));
|
|
65
|
+
for (let i = 0; i < overflow; i++)
|
|
66
|
+
this.history.push(rowText(this.grid.shift()));
|
|
67
|
+
this.grid.length = rows;
|
|
68
|
+
this.y -= overflow;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
while (this.grid.length < rows)
|
|
72
|
+
this.grid.push(this.blankRow());
|
|
73
|
+
}
|
|
74
|
+
this.rows = rows;
|
|
75
|
+
this.x = Math.min(this.x, cols - 1);
|
|
76
|
+
this.pendingWrap = false;
|
|
77
|
+
}
|
|
78
|
+
write(data) {
|
|
79
|
+
let i = 0;
|
|
80
|
+
while (i < data.length) {
|
|
81
|
+
const c = data.charCodeAt(i);
|
|
82
|
+
if (c === 0x1b) {
|
|
83
|
+
const len = escapeLengthAt(data, i);
|
|
84
|
+
this.escape(data.slice(i, i + len));
|
|
85
|
+
i += len;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (c < 0x20 || c === 0x7f) {
|
|
89
|
+
this.control(c);
|
|
90
|
+
i++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
let j = i;
|
|
94
|
+
while (j < data.length) {
|
|
95
|
+
const cc = data.charCodeAt(j);
|
|
96
|
+
if (cc === 0x1b || cc < 0x20 || cc === 0x7f)
|
|
97
|
+
break;
|
|
98
|
+
j++;
|
|
99
|
+
}
|
|
100
|
+
for (const { segment } of segmenter.segment(data.slice(i, j)))
|
|
101
|
+
this.print(segment);
|
|
102
|
+
i = j;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
blankRow() {
|
|
106
|
+
return new Array(this.cols).fill(" ");
|
|
107
|
+
}
|
|
108
|
+
lineFeed() {
|
|
109
|
+
if (this.y < this.rows - 1) {
|
|
110
|
+
this.y++;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this.history.push(rowText(this.grid.shift()));
|
|
114
|
+
this.grid.push(this.blankRow());
|
|
115
|
+
}
|
|
116
|
+
print(grapheme) {
|
|
117
|
+
const w = graphemeWidth(grapheme);
|
|
118
|
+
if (w === 0) {
|
|
119
|
+
// 组合字符:附到前一格
|
|
120
|
+
const row = this.grid[this.y];
|
|
121
|
+
const at = Math.max(0, this.pendingWrap ? this.x : this.x - 1);
|
|
122
|
+
row[at] = (row[at] === " " ? "" : row[at]) + grapheme;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (this.pendingWrap || this.x + w > this.cols) {
|
|
126
|
+
this.x = 0;
|
|
127
|
+
this.lineFeed();
|
|
128
|
+
this.pendingWrap = false;
|
|
129
|
+
}
|
|
130
|
+
const row = this.grid[this.y];
|
|
131
|
+
row[this.x] = grapheme;
|
|
132
|
+
if (w === 2 && this.x + 1 < this.cols)
|
|
133
|
+
row[this.x + 1] = "";
|
|
134
|
+
this.x += w;
|
|
135
|
+
if (this.x >= this.cols) {
|
|
136
|
+
this.x = this.cols - 1;
|
|
137
|
+
this.pendingWrap = true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
control(c) {
|
|
141
|
+
switch (c) {
|
|
142
|
+
case 0x0d:
|
|
143
|
+
this.x = 0;
|
|
144
|
+
this.pendingWrap = false;
|
|
145
|
+
break;
|
|
146
|
+
case 0x0a:
|
|
147
|
+
this.lineFeed();
|
|
148
|
+
this.pendingWrap = false;
|
|
149
|
+
break;
|
|
150
|
+
case 0x08:
|
|
151
|
+
this.x = Math.max(0, this.x - 1);
|
|
152
|
+
this.pendingWrap = false;
|
|
153
|
+
break;
|
|
154
|
+
default:
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
escape(seq) {
|
|
159
|
+
if (!seq.startsWith("\x1b["))
|
|
160
|
+
return;
|
|
161
|
+
const final = seq[seq.length - 1];
|
|
162
|
+
const body = seq.slice(2, -1);
|
|
163
|
+
if (body.startsWith("?")) {
|
|
164
|
+
this.privateMode(body.slice(1), final === "h");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const params = body.split(";").map((p) => (p === "" ? undefined : Number(p)));
|
|
168
|
+
const n = params[0] ?? 1;
|
|
169
|
+
this.pendingWrap = false;
|
|
170
|
+
switch (final) {
|
|
171
|
+
case "A":
|
|
172
|
+
this.y = Math.max(0, this.y - n);
|
|
173
|
+
break;
|
|
174
|
+
case "B":
|
|
175
|
+
this.y = Math.min(this.rows - 1, this.y + n);
|
|
176
|
+
break;
|
|
177
|
+
case "C":
|
|
178
|
+
this.x = Math.min(this.cols - 1, this.x + n);
|
|
179
|
+
break;
|
|
180
|
+
case "D":
|
|
181
|
+
this.x = Math.max(0, this.x - n);
|
|
182
|
+
break;
|
|
183
|
+
case "E":
|
|
184
|
+
this.y = Math.min(this.rows - 1, this.y + n);
|
|
185
|
+
this.x = 0;
|
|
186
|
+
break;
|
|
187
|
+
case "F":
|
|
188
|
+
this.y = Math.max(0, this.y - n);
|
|
189
|
+
this.x = 0;
|
|
190
|
+
break;
|
|
191
|
+
case "G":
|
|
192
|
+
this.x = clamp(n - 1, 0, this.cols - 1);
|
|
193
|
+
break;
|
|
194
|
+
case "H":
|
|
195
|
+
case "f":
|
|
196
|
+
this.y = clamp((params[0] ?? 1) - 1, 0, this.rows - 1);
|
|
197
|
+
this.x = clamp((params[1] ?? 1) - 1, 0, this.cols - 1);
|
|
198
|
+
break;
|
|
199
|
+
case "J":
|
|
200
|
+
this.eraseDisplay(params[0] ?? 0);
|
|
201
|
+
break;
|
|
202
|
+
case "K":
|
|
203
|
+
this.eraseLine(params[0] ?? 0);
|
|
204
|
+
break;
|
|
205
|
+
default:
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
privateMode(body, on) {
|
|
210
|
+
for (const p of body.split(";")) {
|
|
211
|
+
if (p === "25")
|
|
212
|
+
this.modes.cursorVisible = on;
|
|
213
|
+
else if (p === "2004")
|
|
214
|
+
this.modes.bracketedPaste = on;
|
|
215
|
+
else if (p === "2026")
|
|
216
|
+
this.modes.synchronized = on;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
eraseLine(mode) {
|
|
220
|
+
const row = this.grid[this.y];
|
|
221
|
+
const [from, to] = mode === 0 ? [this.x, this.cols] : mode === 1 ? [0, this.x + 1] : [0, this.cols];
|
|
222
|
+
for (let i = from; i < to; i++)
|
|
223
|
+
row[i] = " ";
|
|
224
|
+
}
|
|
225
|
+
eraseDisplay(mode) {
|
|
226
|
+
if (mode === 3) {
|
|
227
|
+
this.history = [];
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (mode === 2) {
|
|
231
|
+
for (let r = 0; r < this.rows; r++)
|
|
232
|
+
this.grid[r] = this.blankRow();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (mode === 0) {
|
|
236
|
+
this.eraseLine(0);
|
|
237
|
+
for (let r = this.y + 1; r < this.rows; r++)
|
|
238
|
+
this.grid[r] = this.blankRow();
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
this.eraseLine(1);
|
|
242
|
+
for (let r = 0; r < this.y; r++)
|
|
243
|
+
this.grid[r] = this.blankRow();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function clamp(v, lo, hi) {
|
|
247
|
+
return Math.max(lo, Math.min(hi, v));
|
|
248
|
+
}
|
|
249
|
+
function rowText(row) {
|
|
250
|
+
return row.join("").replace(/ +$/, "");
|
|
251
|
+
}
|
package/dist/tui.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@armadra/agent/tui` 子路径:组件库再导出(设计 D1、§12.1)。[B0] 所有。
|
|
3
|
+
*
|
|
4
|
+
* B0 只导出组件模型契约;B4 完成组件库后在本文件末尾追加各组件的再导出
|
|
5
|
+
* (TUI、ProcessTerminal / MemoryTerminal、Container、Text、Markdown、Editor、SelectList、Box、
|
|
6
|
+
* Spacer、Loader、Overlay、主题与 ansi 工具)——这是 B4 唯一允许改动的 B0 文件。
|
|
7
|
+
*/
|
|
8
|
+
export { CURSOR_MARKER, isFocusable } from "./tui/component.js";
|
|
9
|
+
export type * from "./tui/component.js";
|
|
10
|
+
export { TUI, SYNC_BEGIN, SYNC_END, WRITE_CHUNK_SIZE, MIN_RENDER_INTERVAL_MS, type TuiOptions, type OverlayHandle, type InputListener, type RenderStats, } from "./tui/tui.js";
|
|
11
|
+
export { ProcessTerminal, MemoryTerminal, BRACKETED_PASTE_ON, BRACKETED_PASTE_OFF, SHOW_CURSOR, HIDE_CURSOR, type Terminal, type ProcessTerminalOptions, type MemoryTerminalOptions, } from "./tui/terminal.js";
|
|
12
|
+
export { VirtualScreen, type ScreenModes } from "./tui/vt-screen.js";
|
|
13
|
+
export { StdinBuffer, defaultEscTimeout, type StdinBufferOptions } from "./tui/stdin-buffer.js";
|
|
14
|
+
export { parseKey, matchesKey, normalizeKeyId, isPasteData, unwrapPaste, isPrintableText, PASTE_START, PASTE_END, type KeyEvent, } from "./tui/keys.js";
|
|
15
|
+
export { visibleWidth, truncateToWidth, sliceByColumn, wrapTextWithAnsi, padToWidth, stripAnsi, codePointWidth, graphemeWidth, SGR_RESET, } from "./tui/ansi.js";
|
|
16
|
+
export { createTheme, plainTheme, detectCapabilities, detectColorDepth, colorCode, rgbTo256, rgbTo16, THEME_PALETTES, type ThemeName, type CreateThemeOptions, } from "./tui/theme.js";
|
|
17
|
+
export { Keybindings, defaultKeybindings, DEFAULT_KEYBINDINGS, parseKeybindings, loadKeybindingsFile, isActionId, type ActionId, type KeybindingOverrides, type ParsedKeybindings, } from "./tui/keybindings.js";
|
|
18
|
+
export { Container } from "./tui/components/container.js";
|
|
19
|
+
export { Text, TruncatedText, type TextOptions } from "./tui/components/text.js";
|
|
20
|
+
export { Markdown, parseMarkdown, renderInline, renderBlock, type MarkdownOptions, type MarkdownBlock, type ListItem, } from "./tui/components/markdown.js";
|
|
21
|
+
export { EditorBuffer, type Position, type EditorBufferOptions, } from "./tui/components/editor-buffer.js";
|
|
22
|
+
export { Editor, loadHistoryFile, type EditorOptions, type AutocompleteProvider, type AutocompleteItem, type AutocompleteResult, type AutocompleteContext, } from "./tui/components/editor.js";
|
|
23
|
+
export { PasteStore, shouldCollapse, formatMarker, PASTE_LINE_THRESHOLD, PASTE_CHAR_THRESHOLD, } from "./tui/components/editor-paste.js";
|
|
24
|
+
export { SelectList, filterItems, type SelectItem, type SelectListOptions, } from "./tui/components/select-list.js";
|
|
25
|
+
export { Box, type BoxOptions } from "./tui/components/box.js";
|
|
26
|
+
export { Spacer } from "./tui/components/spacer.js";
|
|
27
|
+
export { Loader, LOADER_FRAMES, formatElapsed, type LoaderOptions, } from "./tui/components/loader.js";
|
|
28
|
+
export { compositeOverlays, type OverlayAnchor, type OverlayOptions, type OverlayLayer, } from "./tui/components/overlay.js";
|
|
29
|
+
export { KeyValue, type KeyValueRow, type KeyValueOptions } from "./tui/components/key-value.js";
|
|
30
|
+
export { Meter, METER_FULL, METER_EMPTY, type MeterOptions } from "./tui/components/meter.js";
|