@lenylvt/pi-coding-agent 0.64.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 +3331 -0
- package/README.md +48 -0
- package/dist/bun/cli.d.ts +3 -0
- package/dist/bun/cli.d.ts.map +1 -0
- package/dist/bun/cli.js +5 -0
- package/dist/bun/cli.js.map +1 -0
- package/dist/cli/args.d.ts +49 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/args.js +292 -0
- package/dist/cli/args.js.map +1 -0
- package/dist/cli/config-selector.d.ts +14 -0
- package/dist/cli/config-selector.d.ts.map +1 -0
- package/dist/cli/config-selector.js +31 -0
- package/dist/cli/config-selector.js.map +1 -0
- package/dist/cli/file-processor.d.ts +15 -0
- package/dist/cli/file-processor.d.ts.map +1 -0
- package/dist/cli/file-processor.js +83 -0
- package/dist/cli/file-processor.js.map +1 -0
- package/dist/cli/initial-message.d.ts +18 -0
- package/dist/cli/initial-message.d.ts.map +1 -0
- package/dist/cli/initial-message.js +22 -0
- package/dist/cli/initial-message.js.map +1 -0
- package/dist/cli/list-models.d.ts +9 -0
- package/dist/cli/list-models.d.ts.map +1 -0
- package/dist/cli/list-models.js +92 -0
- package/dist/cli/list-models.js.map +1 -0
- package/dist/cli/session-picker.d.ts +9 -0
- package/dist/cli/session-picker.d.ts.map +1 -0
- package/dist/cli/session-picker.js +35 -0
- package/dist/cli/session-picker.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +14 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +69 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +178 -0
- package/dist/config.js.map +1 -0
- package/dist/core/agent-session.d.ts +622 -0
- package/dist/core/agent-session.d.ts.map +1 -0
- package/dist/core/agent-session.js +2688 -0
- package/dist/core/agent-session.js.map +1 -0
- package/dist/core/auth-storage.d.ts +132 -0
- package/dist/core/auth-storage.d.ts.map +1 -0
- package/dist/core/auth-storage.js +422 -0
- package/dist/core/auth-storage.js.map +1 -0
- package/dist/core/bash-executor.d.ts +46 -0
- package/dist/core/bash-executor.d.ts.map +1 -0
- package/dist/core/bash-executor.js +113 -0
- package/dist/core/bash-executor.js.map +1 -0
- package/dist/core/compaction/branch-summarization.d.ts +88 -0
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
- package/dist/core/compaction/branch-summarization.js +243 -0
- package/dist/core/compaction/branch-summarization.js.map +1 -0
- package/dist/core/compaction/compaction.d.ts +121 -0
- package/dist/core/compaction/compaction.d.ts.map +1 -0
- package/dist/core/compaction/compaction.js +613 -0
- package/dist/core/compaction/compaction.js.map +1 -0
- package/dist/core/compaction/index.d.ts +7 -0
- package/dist/core/compaction/index.d.ts.map +1 -0
- package/dist/core/compaction/index.js +7 -0
- package/dist/core/compaction/index.js.map +1 -0
- package/dist/core/compaction/utils.d.ts +38 -0
- package/dist/core/compaction/utils.d.ts.map +1 -0
- package/dist/core/compaction/utils.js +153 -0
- package/dist/core/compaction/utils.js.map +1 -0
- package/dist/core/defaults.d.ts +3 -0
- package/dist/core/defaults.d.ts.map +1 -0
- package/dist/core/defaults.js +2 -0
- package/dist/core/defaults.js.map +1 -0
- package/dist/core/diagnostics.d.ts +15 -0
- package/dist/core/diagnostics.d.ts.map +1 -0
- package/dist/core/diagnostics.js +2 -0
- package/dist/core/diagnostics.js.map +1 -0
- package/dist/core/event-bus.d.ts +9 -0
- package/dist/core/event-bus.d.ts.map +1 -0
- package/dist/core/event-bus.js +25 -0
- package/dist/core/event-bus.js.map +1 -0
- package/dist/core/exec.d.ts +29 -0
- package/dist/core/exec.d.ts.map +1 -0
- package/dist/core/exec.js +75 -0
- package/dist/core/exec.js.map +1 -0
- package/dist/core/export-html/ansi-to-html.d.ts +22 -0
- package/dist/core/export-html/ansi-to-html.d.ts.map +1 -0
- package/dist/core/export-html/ansi-to-html.js +249 -0
- package/dist/core/export-html/ansi-to-html.js.map +1 -0
- package/dist/core/export-html/index.d.ts +37 -0
- package/dist/core/export-html/index.d.ts.map +1 -0
- package/dist/core/export-html/index.js +224 -0
- package/dist/core/export-html/index.js.map +1 -0
- package/dist/core/export-html/template.css +1001 -0
- package/dist/core/export-html/template.html +55 -0
- package/dist/core/export-html/template.js +1690 -0
- package/dist/core/export-html/tool-renderer.d.ts +38 -0
- package/dist/core/export-html/tool-renderer.d.ts.map +1 -0
- package/dist/core/export-html/tool-renderer.js +95 -0
- package/dist/core/export-html/tool-renderer.js.map +1 -0
- package/dist/core/export-html/vendor/highlight.min.js +1213 -0
- package/dist/core/export-html/vendor/marked.min.js +6 -0
- package/dist/core/extensions/index.d.ts +12 -0
- package/dist/core/extensions/index.d.ts.map +1 -0
- package/dist/core/extensions/index.js +9 -0
- package/dist/core/extensions/index.js.map +1 -0
- package/dist/core/extensions/loader.d.ts +25 -0
- package/dist/core/extensions/loader.d.ts.map +1 -0
- package/dist/core/extensions/loader.js +436 -0
- package/dist/core/extensions/loader.js.map +1 -0
- package/dist/core/extensions/runner.d.ts +148 -0
- package/dist/core/extensions/runner.d.ts.map +1 -0
- package/dist/core/extensions/runner.js +700 -0
- package/dist/core/extensions/runner.js.map +1 -0
- package/dist/core/extensions/types.d.ts +1085 -0
- package/dist/core/extensions/types.d.ts.map +1 -0
- package/dist/core/extensions/types.js +35 -0
- package/dist/core/extensions/types.js.map +1 -0
- package/dist/core/extensions/wrapper.d.ts +20 -0
- package/dist/core/extensions/wrapper.d.ts.map +1 -0
- package/dist/core/extensions/wrapper.js +22 -0
- package/dist/core/extensions/wrapper.js.map +1 -0
- package/dist/core/footer-data-provider.d.ts +44 -0
- package/dist/core/footer-data-provider.d.ts.map +1 -0
- package/dist/core/footer-data-provider.js +252 -0
- package/dist/core/footer-data-provider.js.map +1 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +10 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/keybindings.d.ts +275 -0
- package/dist/core/keybindings.d.ts.map +1 -0
- package/dist/core/keybindings.js +241 -0
- package/dist/core/keybindings.js.map +1 -0
- package/dist/core/messages.d.ts +77 -0
- package/dist/core/messages.d.ts.map +1 -0
- package/dist/core/messages.js +123 -0
- package/dist/core/messages.js.map +1 -0
- package/dist/core/model-registry.d.ts +132 -0
- package/dist/core/model-registry.d.ts.map +1 -0
- package/dist/core/model-registry.js +565 -0
- package/dist/core/model-registry.js.map +1 -0
- package/dist/core/model-resolver.d.ts +110 -0
- package/dist/core/model-resolver.d.ts.map +1 -0
- package/dist/core/model-resolver.js +464 -0
- package/dist/core/model-resolver.js.map +1 -0
- package/dist/core/output-guard.d.ts +6 -0
- package/dist/core/output-guard.d.ts.map +1 -0
- package/dist/core/output-guard.js +59 -0
- package/dist/core/output-guard.js.map +1 -0
- package/dist/core/package-manager.d.ts +172 -0
- package/dist/core/package-manager.d.ts.map +1 -0
- package/dist/core/package-manager.js +1801 -0
- package/dist/core/package-manager.js.map +1 -0
- package/dist/core/prompt-templates.d.ts +51 -0
- package/dist/core/prompt-templates.d.ts.map +1 -0
- package/dist/core/prompt-templates.js +249 -0
- package/dist/core/prompt-templates.js.map +1 -0
- package/dist/core/resolve-config-value.d.ts +23 -0
- package/dist/core/resolve-config-value.d.ts.map +1 -0
- package/dist/core/resolve-config-value.js +126 -0
- package/dist/core/resolve-config-value.js.map +1 -0
- package/dist/core/resource-loader.d.ts +185 -0
- package/dist/core/resource-loader.d.ts.map +1 -0
- package/dist/core/resource-loader.js +698 -0
- package/dist/core/resource-loader.js.map +1 -0
- package/dist/core/sdk.d.ts +90 -0
- package/dist/core/sdk.d.ts.map +1 -0
- package/dist/core/sdk.js +233 -0
- package/dist/core/sdk.js.map +1 -0
- package/dist/core/self-update.d.ts +3 -0
- package/dist/core/self-update.d.ts.map +1 -0
- package/dist/core/self-update.js +46 -0
- package/dist/core/self-update.js.map +1 -0
- package/dist/core/session-manager.d.ts +329 -0
- package/dist/core/session-manager.d.ts.map +1 -0
- package/dist/core/session-manager.js +1097 -0
- package/dist/core/session-manager.js.map +1 -0
- package/dist/core/settings-manager.d.ts +237 -0
- package/dist/core/settings-manager.d.ts.map +1 -0
- package/dist/core/settings-manager.js +708 -0
- package/dist/core/settings-manager.js.map +1 -0
- package/dist/core/skills.d.ts +60 -0
- package/dist/core/skills.d.ts.map +1 -0
- package/dist/core/skills.js +409 -0
- package/dist/core/skills.js.map +1 -0
- package/dist/core/slash-commands.d.ts +14 -0
- package/dist/core/slash-commands.d.ts.map +1 -0
- package/dist/core/slash-commands.js +22 -0
- package/dist/core/slash-commands.js.map +1 -0
- package/dist/core/source-info.d.ts +18 -0
- package/dist/core/source-info.d.ts.map +1 -0
- package/dist/core/source-info.js +19 -0
- package/dist/core/source-info.js.map +1 -0
- package/dist/core/system-prompt.d.ts +28 -0
- package/dist/core/system-prompt.d.ts.map +1 -0
- package/dist/core/system-prompt.js +116 -0
- package/dist/core/system-prompt.js.map +1 -0
- package/dist/core/timings.d.ts +8 -0
- package/dist/core/timings.d.ts.map +1 -0
- package/dist/core/timings.js +31 -0
- package/dist/core/timings.js.map +1 -0
- package/dist/core/tools/bash.d.ts +73 -0
- package/dist/core/tools/bash.d.ts.map +1 -0
- package/dist/core/tools/bash.js +342 -0
- package/dist/core/tools/bash.js.map +1 -0
- package/dist/core/tools/edit-diff.d.ts +85 -0
- package/dist/core/tools/edit-diff.d.ts.map +1 -0
- package/dist/core/tools/edit-diff.js +337 -0
- package/dist/core/tools/edit-diff.js.map +1 -0
- package/dist/core/tools/edit.d.ts +53 -0
- package/dist/core/tools/edit.d.ts.map +1 -0
- package/dist/core/tools/edit.js +196 -0
- package/dist/core/tools/edit.js.map +1 -0
- package/dist/core/tools/file-mutation-queue.d.ts +6 -0
- package/dist/core/tools/file-mutation-queue.d.ts.map +1 -0
- package/dist/core/tools/file-mutation-queue.js +37 -0
- package/dist/core/tools/file-mutation-queue.js.map +1 -0
- package/dist/core/tools/find.d.ts +46 -0
- package/dist/core/tools/find.d.ts.map +1 -0
- package/dist/core/tools/find.js +258 -0
- package/dist/core/tools/find.js.map +1 -0
- package/dist/core/tools/grep.d.ts +56 -0
- package/dist/core/tools/grep.d.ts.map +1 -0
- package/dist/core/tools/grep.js +293 -0
- package/dist/core/tools/grep.js.map +1 -0
- package/dist/core/tools/index.d.ts +115 -0
- package/dist/core/tools/index.d.ts.map +1 -0
- package/dist/core/tools/index.js +86 -0
- package/dist/core/tools/index.js.map +1 -0
- package/dist/core/tools/ls.d.ts +46 -0
- package/dist/core/tools/ls.d.ts.map +1 -0
- package/dist/core/tools/ls.js +172 -0
- package/dist/core/tools/ls.js.map +1 -0
- package/dist/core/tools/path-utils.d.ts +8 -0
- package/dist/core/tools/path-utils.d.ts.map +1 -0
- package/dist/core/tools/path-utils.js +81 -0
- package/dist/core/tools/path-utils.js.map +1 -0
- package/dist/core/tools/read.d.ts +46 -0
- package/dist/core/tools/read.d.ts.map +1 -0
- package/dist/core/tools/read.js +225 -0
- package/dist/core/tools/read.js.map +1 -0
- package/dist/core/tools/render-utils.d.ts +21 -0
- package/dist/core/tools/render-utils.d.ts.map +1 -0
- package/dist/core/tools/render-utils.js +49 -0
- package/dist/core/tools/render-utils.js.map +1 -0
- package/dist/core/tools/tool-definition-wrapper.d.ts +14 -0
- package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -0
- package/dist/core/tools/tool-definition-wrapper.js +32 -0
- package/dist/core/tools/tool-definition-wrapper.js.map +1 -0
- package/dist/core/tools/truncate.d.ts +70 -0
- package/dist/core/tools/truncate.d.ts.map +1 -0
- package/dist/core/tools/truncate.js +205 -0
- package/dist/core/tools/truncate.js.map +1 -0
- package/dist/core/tools/write.d.ts +35 -0
- package/dist/core/tools/write.d.ts.map +1 -0
- package/dist/core/tools/write.js +216 -0
- package/dist/core/tools/write.js.map +1 -0
- package/dist/core/version-check.d.ts +2 -0
- package/dist/core/version-check.d.ts.map +1 -0
- package/dist/core/version-check.js +73 -0
- package/dist/core/version-check.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +8 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +811 -0
- package/dist/main.js.map +1 -0
- package/dist/migrations.d.ts +32 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/migrations.js +258 -0
- package/dist/migrations.js.map +1 -0
- package/dist/modes/index.d.ts +9 -0
- package/dist/modes/index.d.ts.map +1 -0
- package/dist/modes/index.js +8 -0
- package/dist/modes/index.js.map +1 -0
- package/dist/modes/interactive/components/armin.d.ts +34 -0
- package/dist/modes/interactive/components/armin.d.ts.map +1 -0
- package/dist/modes/interactive/components/armin.js +333 -0
- package/dist/modes/interactive/components/armin.js.map +1 -0
- package/dist/modes/interactive/components/assistant-message.d.ts +18 -0
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/assistant-message.js +107 -0
- package/dist/modes/interactive/components/assistant-message.js.map +1 -0
- package/dist/modes/interactive/components/bash-execution.d.ts +34 -0
- package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -0
- package/dist/modes/interactive/components/bash-execution.js +175 -0
- package/dist/modes/interactive/components/bash-execution.js.map +1 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
- package/dist/modes/interactive/components/bordered-loader.js +51 -0
- package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/branch-summary-message.js +44 -0
- package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/compaction-summary-message.js +45 -0
- package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -0
- package/dist/modes/interactive/components/config-selector.d.ts +71 -0
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/config-selector.js +479 -0
- package/dist/modes/interactive/components/config-selector.js.map +1 -0
- package/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
- package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -0
- package/dist/modes/interactive/components/countdown-timer.js +33 -0
- package/dist/modes/interactive/components/countdown-timer.js.map +1 -0
- package/dist/modes/interactive/components/custom-editor.d.ts +21 -0
- package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -0
- package/dist/modes/interactive/components/custom-editor.js +70 -0
- package/dist/modes/interactive/components/custom-editor.js.map +1 -0
- package/dist/modes/interactive/components/custom-message.d.ts +20 -0
- package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/custom-message.js +79 -0
- package/dist/modes/interactive/components/custom-message.js.map +1 -0
- package/dist/modes/interactive/components/diff.d.ts +12 -0
- package/dist/modes/interactive/components/diff.d.ts.map +1 -0
- package/dist/modes/interactive/components/diff.js +133 -0
- package/dist/modes/interactive/components/diff.js.map +1 -0
- package/dist/modes/interactive/components/dynamic-border.d.ts +15 -0
- package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -0
- package/dist/modes/interactive/components/dynamic-border.js +21 -0
- package/dist/modes/interactive/components/dynamic-border.js.map +1 -0
- package/dist/modes/interactive/components/extension-editor.d.ts +20 -0
- package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-editor.js +111 -0
- package/dist/modes/interactive/components/extension-editor.js.map +1 -0
- package/dist/modes/interactive/components/extension-input.d.ts +23 -0
- package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-input.js +61 -0
- package/dist/modes/interactive/components/extension-input.js.map +1 -0
- package/dist/modes/interactive/components/extension-selector.d.ts +24 -0
- package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-selector.js +78 -0
- package/dist/modes/interactive/components/extension-selector.js.map +1 -0
- package/dist/modes/interactive/components/footer.d.ts +26 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -0
- package/dist/modes/interactive/components/footer.js +198 -0
- package/dist/modes/interactive/components/footer.js.map +1 -0
- package/dist/modes/interactive/components/index.d.ts +31 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -0
- package/dist/modes/interactive/components/index.js +32 -0
- package/dist/modes/interactive/components/index.js.map +1 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts +8 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -0
- package/dist/modes/interactive/components/keybinding-hints.js +22 -0
- package/dist/modes/interactive/components/keybinding-hints.js.map +1 -0
- package/dist/modes/interactive/components/login-dialog.d.ts +42 -0
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -0
- package/dist/modes/interactive/components/login-dialog.js +145 -0
- package/dist/modes/interactive/components/login-dialog.js.map +1 -0
- package/dist/modes/interactive/components/model-selector.d.ts +47 -0
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/model-selector.js +275 -0
- package/dist/modes/interactive/components/model-selector.js.map +1 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts +19 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/oauth-selector.js +97 -0
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts +49 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.js +275 -0
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
- package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
- package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
- package/dist/modes/interactive/components/session-selector-search.js +155 -0
- package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
- package/dist/modes/interactive/components/session-selector.d.ts +95 -0
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/session-selector.js +848 -0
- package/dist/modes/interactive/components/session-selector.js.map +1 -0
- package/dist/modes/interactive/components/settings-selector.d.ts +58 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/settings-selector.js +301 -0
- package/dist/modes/interactive/components/settings-selector.js.map +1 -0
- package/dist/modes/interactive/components/show-images-selector.d.ts +10 -0
- package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/show-images-selector.js +39 -0
- package/dist/modes/interactive/components/show-images-selector.js.map +1 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
- package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
- package/dist/modes/interactive/components/theme-selector.d.ts +11 -0
- package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/theme-selector.js +50 -0
- package/dist/modes/interactive/components/theme-selector.js.map +1 -0
- package/dist/modes/interactive/components/thinking-selector.d.ts +11 -0
- package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/thinking-selector.js +51 -0
- package/dist/modes/interactive/components/thinking-selector.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts +58 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -0
- package/dist/modes/interactive/components/tool-execution.js +274 -0
- package/dist/modes/interactive/components/tool-execution.js.map +1 -0
- package/dist/modes/interactive/components/tree-selector.d.ts +87 -0
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/tree-selector.js +1051 -0
- package/dist/modes/interactive/components/tree-selector.js.map +1 -0
- package/dist/modes/interactive/components/user-message-selector.d.ts +30 -0
- package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/user-message-selector.js +113 -0
- package/dist/modes/interactive/components/user-message-selector.js.map +1 -0
- package/dist/modes/interactive/components/user-message.d.ts +9 -0
- package/dist/modes/interactive/components/user-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/user-message.js +28 -0
- package/dist/modes/interactive/components/user-message.js.map +1 -0
- package/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
- package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -0
- package/dist/modes/interactive/components/visual-truncate.js +33 -0
- package/dist/modes/interactive/components/visual-truncate.js.map +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts +311 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
- package/dist/modes/interactive/interactive-mode.js +3798 -0
- package/dist/modes/interactive/interactive-mode.js.map +1 -0
- package/dist/modes/interactive/theme/dark.json +85 -0
- package/dist/modes/interactive/theme/light.json +84 -0
- package/dist/modes/interactive/theme/theme-schema.json +335 -0
- package/dist/modes/interactive/theme/theme.d.ts +81 -0
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
- package/dist/modes/interactive/theme/theme.js +975 -0
- package/dist/modes/interactive/theme/theme.js.map +1 -0
- package/dist/modes/print-mode.d.ts +28 -0
- package/dist/modes/print-mode.d.ts.map +1 -0
- package/dist/modes/print-mode.js +107 -0
- package/dist/modes/print-mode.js.map +1 -0
- package/dist/modes/rpc/jsonl.d.ts +17 -0
- package/dist/modes/rpc/jsonl.d.ts.map +1 -0
- package/dist/modes/rpc/jsonl.js +49 -0
- package/dist/modes/rpc/jsonl.js.map +1 -0
- package/dist/modes/rpc/rpc-client.d.ts +217 -0
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
- package/dist/modes/rpc/rpc-client.js +401 -0
- package/dist/modes/rpc/rpc-client.js.map +1 -0
- package/dist/modes/rpc/rpc-mode.d.ts +20 -0
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -0
- package/dist/modes/rpc/rpc-mode.js +522 -0
- package/dist/modes/rpc/rpc-mode.js.map +1 -0
- package/dist/modes/rpc/rpc-types.d.ts +408 -0
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -0
- package/dist/modes/rpc/rpc-types.js +8 -0
- package/dist/modes/rpc/rpc-types.js.map +1 -0
- package/dist/utils/changelog.d.ts +21 -0
- package/dist/utils/changelog.d.ts.map +1 -0
- package/dist/utils/changelog.js +87 -0
- package/dist/utils/changelog.js.map +1 -0
- package/dist/utils/child-process.d.ts +11 -0
- package/dist/utils/child-process.d.ts.map +1 -0
- package/dist/utils/child-process.js +78 -0
- package/dist/utils/child-process.js.map +1 -0
- package/dist/utils/clipboard-image.d.ts +11 -0
- package/dist/utils/clipboard-image.d.ts.map +1 -0
- package/dist/utils/clipboard-image.js +245 -0
- package/dist/utils/clipboard-image.js.map +1 -0
- package/dist/utils/clipboard-native.d.ts +8 -0
- package/dist/utils/clipboard-native.d.ts.map +1 -0
- package/dist/utils/clipboard-native.js +14 -0
- package/dist/utils/clipboard-native.js.map +1 -0
- package/dist/utils/clipboard.d.ts +2 -0
- package/dist/utils/clipboard.d.ts.map +1 -0
- package/dist/utils/clipboard.js +78 -0
- package/dist/utils/clipboard.js.map +1 -0
- package/dist/utils/exif-orientation.d.ts +5 -0
- package/dist/utils/exif-orientation.d.ts.map +1 -0
- package/dist/utils/exif-orientation.js +158 -0
- package/dist/utils/exif-orientation.js.map +1 -0
- package/dist/utils/frontmatter.d.ts +8 -0
- package/dist/utils/frontmatter.d.ts.map +1 -0
- package/dist/utils/frontmatter.js +26 -0
- package/dist/utils/frontmatter.js.map +1 -0
- package/dist/utils/git.d.ts +26 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +163 -0
- package/dist/utils/git.js.map +1 -0
- package/dist/utils/image-convert.d.ts +9 -0
- package/dist/utils/image-convert.d.ts.map +1 -0
- package/dist/utils/image-convert.js +39 -0
- package/dist/utils/image-convert.js.map +1 -0
- package/dist/utils/image-resize.d.ts +36 -0
- package/dist/utils/image-resize.d.ts.map +1 -0
- package/dist/utils/image-resize.js +137 -0
- package/dist/utils/image-resize.js.map +1 -0
- package/dist/utils/mime.d.ts +2 -0
- package/dist/utils/mime.d.ts.map +1 -0
- package/dist/utils/mime.js +26 -0
- package/dist/utils/mime.js.map +1 -0
- package/dist/utils/photon.d.ts +21 -0
- package/dist/utils/photon.d.ts.map +1 -0
- package/dist/utils/photon.js +121 -0
- package/dist/utils/photon.js.map +1 -0
- package/dist/utils/shell.d.ts +26 -0
- package/dist/utils/shell.d.ts.map +1 -0
- package/dist/utils/shell.js +186 -0
- package/dist/utils/shell.js.map +1 -0
- package/dist/utils/sleep.d.ts +5 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/sleep.js +17 -0
- package/dist/utils/sleep.js.map +1 -0
- package/dist/utils/tools-manager.d.ts +3 -0
- package/dist/utils/tools-manager.d.ts.map +1 -0
- package/dist/utils/tools-manager.js +252 -0
- package/dist/utils/tools-manager.js.map +1 -0
- package/docs/compaction.md +394 -0
- package/docs/custom-provider.md +327 -0
- package/docs/extensions.md +2229 -0
- package/docs/images/doom-extension.png +0 -0
- package/docs/images/exy.png +0 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/json.md +82 -0
- package/docs/keybindings.md +173 -0
- package/docs/models.md +197 -0
- package/docs/packages.md +218 -0
- package/docs/prompt-templates.md +67 -0
- package/docs/providers.md +98 -0
- package/docs/release.md +40 -0
- package/docs/rpc.md +1377 -0
- package/docs/sdk.md +971 -0
- package/docs/session.md +412 -0
- package/docs/settings.md +149 -0
- package/docs/shell-aliases.md +13 -0
- package/docs/skills.md +232 -0
- package/docs/terminal-setup.md +106 -0
- package/docs/termux.md +127 -0
- package/docs/themes.md +295 -0
- package/docs/tmux.md +61 -0
- package/docs/tree.md +228 -0
- package/docs/tui.md +887 -0
- package/docs/windows.md +17 -0
- package/examples/README.md +25 -0
- package/examples/extensions/README.md +204 -0
- package/examples/extensions/auto-commit-on-exit.ts +49 -0
- package/examples/extensions/bash-spawn-hook.ts +30 -0
- package/examples/extensions/bookmark.ts +50 -0
- package/examples/extensions/built-in-tool-renderer.ts +246 -0
- package/examples/extensions/claude-rules.ts +86 -0
- package/examples/extensions/commands.ts +72 -0
- package/examples/extensions/confirm-destructive.ts +59 -0
- package/examples/extensions/custom-compaction.ts +127 -0
- package/examples/extensions/custom-footer.ts +64 -0
- package/examples/extensions/custom-header.ts +73 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
- package/examples/extensions/custom-provider-anthropic/package.json +19 -0
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +349 -0
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
- package/examples/extensions/dirty-repo-guard.ts +56 -0
- package/examples/extensions/doom-overlay/README.md +46 -0
- package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
- package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
- package/examples/extensions/doom-overlay/doom/build.sh +152 -0
- package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
- package/examples/extensions/doom-overlay/doom-component.ts +132 -0
- package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
- package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
- package/examples/extensions/doom-overlay/index.ts +74 -0
- package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
- package/examples/extensions/dynamic-resources/SKILL.md +8 -0
- package/examples/extensions/dynamic-resources/dynamic.json +79 -0
- package/examples/extensions/dynamic-resources/dynamic.md +5 -0
- package/examples/extensions/dynamic-resources/index.ts +15 -0
- package/examples/extensions/dynamic-tools.ts +74 -0
- package/examples/extensions/event-bus.ts +43 -0
- package/examples/extensions/file-trigger.ts +41 -0
- package/examples/extensions/git-checkpoint.ts +53 -0
- package/examples/extensions/handoff.ts +153 -0
- package/examples/extensions/hello.ts +25 -0
- package/examples/extensions/hidden-thinking-label.ts +57 -0
- package/examples/extensions/inline-bash.ts +94 -0
- package/examples/extensions/input-transform.ts +43 -0
- package/examples/extensions/interactive-shell.ts +196 -0
- package/examples/extensions/mac-system-theme.ts +47 -0
- package/examples/extensions/message-renderer.ts +59 -0
- package/examples/extensions/minimal-mode.ts +426 -0
- package/examples/extensions/modal-editor.ts +85 -0
- package/examples/extensions/model-status.ts +31 -0
- package/examples/extensions/notify.ts +55 -0
- package/examples/extensions/overlay-qa-tests.ts +1348 -0
- package/examples/extensions/overlay-test.ts +150 -0
- package/examples/extensions/permission-gate.ts +34 -0
- package/examples/extensions/pirate.ts +47 -0
- package/examples/extensions/plan-mode/README.md +65 -0
- package/examples/extensions/plan-mode/index.ts +340 -0
- package/examples/extensions/plan-mode/utils.ts +166 -0
- package/examples/extensions/preset.ts +397 -0
- package/examples/extensions/protected-paths.ts +30 -0
- package/examples/extensions/provider-payload.ts +14 -0
- package/examples/extensions/qna.ts +122 -0
- package/examples/extensions/question.ts +264 -0
- package/examples/extensions/questionnaire.ts +427 -0
- package/examples/extensions/rainbow-editor.ts +88 -0
- package/examples/extensions/reload-runtime.ts +37 -0
- package/examples/extensions/rpc-demo.ts +124 -0
- package/examples/extensions/sandbox/index.ts +321 -0
- package/examples/extensions/sandbox/package.json +19 -0
- package/examples/extensions/send-user-message.ts +97 -0
- package/examples/extensions/session-name.ts +27 -0
- package/examples/extensions/shutdown-command.ts +63 -0
- package/examples/extensions/snake.ts +343 -0
- package/examples/extensions/space-invaders.ts +560 -0
- package/examples/extensions/ssh.ts +220 -0
- package/examples/extensions/status-line.ts +40 -0
- package/examples/extensions/subagent/README.md +172 -0
- package/examples/extensions/subagent/agents/planner.md +37 -0
- package/examples/extensions/subagent/agents/reviewer.md +35 -0
- package/examples/extensions/subagent/agents/scout.md +50 -0
- package/examples/extensions/subagent/agents/worker.md +24 -0
- package/examples/extensions/subagent/agents.ts +126 -0
- package/examples/extensions/subagent/index.ts +986 -0
- package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
- package/examples/extensions/subagent/prompts/implement.md +10 -0
- package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
- package/examples/extensions/summarize.ts +206 -0
- package/examples/extensions/system-prompt-header.ts +17 -0
- package/examples/extensions/timed-confirm.ts +70 -0
- package/examples/extensions/titlebar-spinner.ts +58 -0
- package/examples/extensions/todo.ts +299 -0
- package/examples/extensions/tool-override.ts +144 -0
- package/examples/extensions/tools.ts +146 -0
- package/examples/extensions/trigger-compact.ts +50 -0
- package/examples/extensions/truncated-tool.ts +195 -0
- package/examples/extensions/widget-placement.ts +17 -0
- package/examples/extensions/with-deps/index.ts +32 -0
- package/examples/extensions/with-deps/package.json +22 -0
- package/examples/rpc-extension-ui.ts +632 -0
- package/examples/sdk/01-minimal.ts +22 -0
- package/examples/sdk/02-custom-model.ts +49 -0
- package/examples/sdk/03-custom-prompt.ts +55 -0
- package/examples/sdk/04-skills.ts +52 -0
- package/examples/sdk/05-tools.ts +56 -0
- package/examples/sdk/06-extensions.ts +88 -0
- package/examples/sdk/07-context-files.ts +40 -0
- package/examples/sdk/08-prompt-templates.ts +48 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
- package/examples/sdk/10-settings.ts +51 -0
- package/examples/sdk/11-sessions.ts +48 -0
- package/examples/sdk/12-full-control.ts +81 -0
- package/examples/sdk/README.md +144 -0
- package/package.json +95 -0
- package/src/bun/cli.ts +5 -0
- package/src/cli/args.ts +309 -0
- package/src/cli/config-selector.ts +52 -0
- package/src/cli/file-processor.ts +100 -0
- package/src/cli/initial-message.ts +43 -0
- package/src/cli/list-models.ts +104 -0
- package/src/cli/session-picker.ts +52 -0
- package/src/cli.ts +16 -0
- package/src/config.ts +214 -0
- package/src/core/agent-session.ts +3279 -0
- package/src/core/auth-storage.ts +488 -0
- package/src/core/bash-executor.ts +158 -0
- package/src/core/compaction/branch-summarization.ts +355 -0
- package/src/core/compaction/compaction.ts +823 -0
- package/src/core/compaction/index.ts +7 -0
- package/src/core/compaction/utils.ts +170 -0
- package/src/core/defaults.ts +3 -0
- package/src/core/diagnostics.ts +15 -0
- package/src/core/event-bus.ts +33 -0
- package/src/core/exec.ts +107 -0
- package/src/core/export-html/ansi-to-html.ts +258 -0
- package/src/core/export-html/index.ts +314 -0
- package/src/core/export-html/template.css +1001 -0
- package/src/core/export-html/template.html +55 -0
- package/src/core/export-html/template.js +1690 -0
- package/src/core/export-html/tool-renderer.ts +156 -0
- package/src/core/export-html/vendor/highlight.min.js +1213 -0
- package/src/core/export-html/vendor/marked.min.js +6 -0
- package/src/core/extensions/index.ts +168 -0
- package/src/core/extensions/loader.ts +557 -0
- package/src/core/extensions/runner.ts +915 -0
- package/src/core/extensions/types.ts +1461 -0
- package/src/core/extensions/wrapper.ts +27 -0
- package/src/core/footer-data-provider.ts +273 -0
- package/src/core/index.ts +61 -0
- package/src/core/keybindings.ts +302 -0
- package/src/core/messages.ts +195 -0
- package/src/core/model-registry.ts +766 -0
- package/src/core/model-resolver.ts +606 -0
- package/src/core/output-guard.ts +74 -0
- package/src/core/package-manager.ts +2222 -0
- package/src/core/prompt-templates.ts +294 -0
- package/src/core/resolve-config-value.ts +142 -0
- package/src/core/resource-loader.ts +886 -0
- package/src/core/sdk.ts +360 -0
- package/src/core/self-update.ts +57 -0
- package/src/core/session-manager.ts +1410 -0
- package/src/core/settings-manager.ts +973 -0
- package/src/core/skills.ts +508 -0
- package/src/core/slash-commands.ts +37 -0
- package/src/core/source-info.ts +40 -0
- package/src/core/system-prompt.ts +168 -0
- package/src/core/timings.ts +31 -0
- package/src/core/tools/bash.ts +431 -0
- package/src/core/tools/edit-diff.ts +445 -0
- package/src/core/tools/edit.ts +307 -0
- package/src/core/tools/file-mutation-queue.ts +39 -0
- package/src/core/tools/find.ts +314 -0
- package/src/core/tools/grep.ts +375 -0
- package/src/core/tools/index.ts +193 -0
- package/src/core/tools/ls.ts +233 -0
- package/src/core/tools/path-utils.ts +94 -0
- package/src/core/tools/read.ts +269 -0
- package/src/core/tools/render-utils.ts +64 -0
- package/src/core/tools/tool-definition-wrapper.ts +43 -0
- package/src/core/tools/truncate.ts +265 -0
- package/src/core/tools/write.ts +285 -0
- package/src/core/version-check.ts +85 -0
- package/src/index.ts +348 -0
- package/src/main.ts +958 -0
- package/src/migrations.ts +290 -0
- package/src/modes/index.ts +9 -0
- package/src/modes/interactive/components/armin.ts +382 -0
- package/src/modes/interactive/components/assistant-message.ts +130 -0
- package/src/modes/interactive/components/bash-execution.ts +218 -0
- package/src/modes/interactive/components/bordered-loader.ts +66 -0
- package/src/modes/interactive/components/branch-summary-message.ts +58 -0
- package/src/modes/interactive/components/compaction-summary-message.ts +59 -0
- package/src/modes/interactive/components/config-selector.ts +592 -0
- package/src/modes/interactive/components/countdown-timer.ts +38 -0
- package/src/modes/interactive/components/custom-editor.ts +80 -0
- package/src/modes/interactive/components/custom-message.ts +99 -0
- package/src/modes/interactive/components/diff.ts +147 -0
- package/src/modes/interactive/components/dynamic-border.ts +25 -0
- package/src/modes/interactive/components/extension-editor.ts +147 -0
- package/src/modes/interactive/components/extension-input.ts +87 -0
- package/src/modes/interactive/components/extension-selector.ts +107 -0
- package/src/modes/interactive/components/footer.ts +216 -0
- package/src/modes/interactive/components/index.ts +31 -0
- package/src/modes/interactive/components/keybinding-hints.ts +24 -0
- package/src/modes/interactive/components/login-dialog.ts +178 -0
- package/src/modes/interactive/components/model-selector.ts +328 -0
- package/src/modes/interactive/components/oauth-selector.ts +121 -0
- package/src/modes/interactive/components/scoped-models-selector.ts +346 -0
- package/src/modes/interactive/components/session-selector-search.ts +194 -0
- package/src/modes/interactive/components/session-selector.ts +1010 -0
- package/src/modes/interactive/components/settings-selector.ts +432 -0
- package/src/modes/interactive/components/show-images-selector.ts +50 -0
- package/src/modes/interactive/components/skill-invocation-message.ts +55 -0
- package/src/modes/interactive/components/theme-selector.ts +67 -0
- package/src/modes/interactive/components/thinking-selector.ts +74 -0
- package/src/modes/interactive/components/tool-execution.ts +328 -0
- package/src/modes/interactive/components/tree-selector.ts +1197 -0
- package/src/modes/interactive/components/user-message-selector.ts +143 -0
- package/src/modes/interactive/components/user-message.ts +33 -0
- package/src/modes/interactive/components/visual-truncate.ts +50 -0
- package/src/modes/interactive/interactive-mode.ts +4501 -0
- package/src/modes/interactive/theme/dark.json +85 -0
- package/src/modes/interactive/theme/light.json +84 -0
- package/src/modes/interactive/theme/theme-schema.json +335 -0
- package/src/modes/interactive/theme/theme.ts +1137 -0
- package/src/modes/print-mode.ts +131 -0
- package/src/modes/rpc/jsonl.ts +58 -0
- package/src/modes/rpc/rpc-client.ts +505 -0
- package/src/modes/rpc/rpc-mode.ts +654 -0
- package/src/modes/rpc/rpc-types.ts +262 -0
- package/src/utils/changelog.ts +99 -0
- package/src/utils/child-process.ts +86 -0
- package/src/utils/clipboard-image.ts +300 -0
- package/src/utils/clipboard-native.ts +22 -0
- package/src/utils/clipboard.ts +81 -0
- package/src/utils/exif-orientation.ts +183 -0
- package/src/utils/frontmatter.ts +39 -0
- package/src/utils/git.ts +192 -0
- package/src/utils/image-convert.ts +41 -0
- package/src/utils/image-resize.ts +176 -0
- package/src/utils/mime.ts +30 -0
- package/src/utils/photon.ts +139 -0
- package/src/utils/shell.ts +202 -0
- package/src/utils/sleep.ts +18 -0
- package/src/utils/tools-manager.ts +287 -0
|
@@ -0,0 +1,4501 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive mode for the coding agent.
|
|
3
|
+
* Handles TUI rendering and user interaction, delegating business logic to AgentSession.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as crypto from "node:crypto";
|
|
7
|
+
import * as fs from "node:fs";
|
|
8
|
+
import * as os from "node:os";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import type { AgentMessage } from "@lenylvt/pi-agent-core";
|
|
11
|
+
import type { AssistantMessage, ImageContent, Message, Model, OAuthProviderId } from "@lenylvt/pi-ai";
|
|
12
|
+
import type {
|
|
13
|
+
AutocompleteItem,
|
|
14
|
+
EditorComponent,
|
|
15
|
+
EditorTheme,
|
|
16
|
+
Keybinding,
|
|
17
|
+
KeyId,
|
|
18
|
+
MarkdownTheme,
|
|
19
|
+
OverlayHandle,
|
|
20
|
+
OverlayOptions,
|
|
21
|
+
SlashCommand,
|
|
22
|
+
} from "@lenylvt/pi-tui";
|
|
23
|
+
import {
|
|
24
|
+
CombinedAutocompleteProvider,
|
|
25
|
+
type Component,
|
|
26
|
+
Container,
|
|
27
|
+
fuzzyFilter,
|
|
28
|
+
Loader,
|
|
29
|
+
Markdown,
|
|
30
|
+
matchesKey,
|
|
31
|
+
ProcessTerminal,
|
|
32
|
+
Spacer,
|
|
33
|
+
setKeybindings,
|
|
34
|
+
Text,
|
|
35
|
+
TruncatedText,
|
|
36
|
+
TUI,
|
|
37
|
+
visibleWidth,
|
|
38
|
+
} from "@lenylvt/pi-tui";
|
|
39
|
+
import { spawn, spawnSync } from "child_process";
|
|
40
|
+
import { APP_NAME, getAgentDir, getAuthPath, getDebugLogPath, getUpdateInstruction, VERSION } from "../../config.js";
|
|
41
|
+
import { type AgentSession, type AgentSessionEvent, parseSkillBlock } from "../../core/agent-session.js";
|
|
42
|
+
import type {
|
|
43
|
+
ExtensionContext,
|
|
44
|
+
ExtensionRunner,
|
|
45
|
+
ExtensionUIContext,
|
|
46
|
+
ExtensionUIDialogOptions,
|
|
47
|
+
ExtensionWidgetOptions,
|
|
48
|
+
} from "../../core/extensions/index.js";
|
|
49
|
+
import { FooterDataProvider, type ReadonlyFooterDataProvider } from "../../core/footer-data-provider.js";
|
|
50
|
+
import { type AppKeybinding, KeybindingsManager } from "../../core/keybindings.js";
|
|
51
|
+
import { createCompactionSummaryMessage } from "../../core/messages.js";
|
|
52
|
+
import { findExactModelReferenceMatch, resolveModelScope } from "../../core/model-resolver.js";
|
|
53
|
+
import { DefaultPackageManager } from "../../core/package-manager.js";
|
|
54
|
+
import type { ResourceDiagnostic } from "../../core/resource-loader.js";
|
|
55
|
+
import { type SessionContext, SessionManager } from "../../core/session-manager.js";
|
|
56
|
+
import { BUILTIN_SLASH_COMMANDS } from "../../core/slash-commands.js";
|
|
57
|
+
import type { SourceInfo } from "../../core/source-info.js";
|
|
58
|
+
import type { TruncationResult } from "../../core/tools/truncate.js";
|
|
59
|
+
import { checkForPublishedCliUpdate } from "../../core/version-check.js";
|
|
60
|
+
import { getChangelogPath, getNewEntries, parseChangelog } from "../../utils/changelog.js";
|
|
61
|
+
import { copyToClipboard } from "../../utils/clipboard.js";
|
|
62
|
+
import { extensionForImageMimeType, readClipboardImage } from "../../utils/clipboard-image.js";
|
|
63
|
+
import { parseGitUrl } from "../../utils/git.js";
|
|
64
|
+
import { ensureTool } from "../../utils/tools-manager.js";
|
|
65
|
+
import { ArminComponent } from "./components/armin.js";
|
|
66
|
+
import { AssistantMessageComponent } from "./components/assistant-message.js";
|
|
67
|
+
import { BashExecutionComponent } from "./components/bash-execution.js";
|
|
68
|
+
import { BorderedLoader } from "./components/bordered-loader.js";
|
|
69
|
+
import { BranchSummaryMessageComponent } from "./components/branch-summary-message.js";
|
|
70
|
+
import { CompactionSummaryMessageComponent } from "./components/compaction-summary-message.js";
|
|
71
|
+
import { CustomEditor } from "./components/custom-editor.js";
|
|
72
|
+
import { CustomMessageComponent } from "./components/custom-message.js";
|
|
73
|
+
import { DynamicBorder } from "./components/dynamic-border.js";
|
|
74
|
+
import { ExtensionEditorComponent } from "./components/extension-editor.js";
|
|
75
|
+
import { ExtensionInputComponent } from "./components/extension-input.js";
|
|
76
|
+
import { ExtensionSelectorComponent } from "./components/extension-selector.js";
|
|
77
|
+
import { FooterComponent } from "./components/footer.js";
|
|
78
|
+
import { keyHint, keyText, rawKeyHint } from "./components/keybinding-hints.js";
|
|
79
|
+
import { LoginDialogComponent } from "./components/login-dialog.js";
|
|
80
|
+
import { ModelSelectorComponent } from "./components/model-selector.js";
|
|
81
|
+
import { OAuthSelectorComponent } from "./components/oauth-selector.js";
|
|
82
|
+
import { ScopedModelsSelectorComponent } from "./components/scoped-models-selector.js";
|
|
83
|
+
import { SessionSelectorComponent } from "./components/session-selector.js";
|
|
84
|
+
import { SettingsSelectorComponent } from "./components/settings-selector.js";
|
|
85
|
+
import { SkillInvocationMessageComponent } from "./components/skill-invocation-message.js";
|
|
86
|
+
import { ToolExecutionComponent } from "./components/tool-execution.js";
|
|
87
|
+
import { TreeSelectorComponent } from "./components/tree-selector.js";
|
|
88
|
+
import { UserMessageComponent } from "./components/user-message.js";
|
|
89
|
+
import { UserMessageSelectorComponent } from "./components/user-message-selector.js";
|
|
90
|
+
import {
|
|
91
|
+
getAvailableThemes,
|
|
92
|
+
getAvailableThemesWithPaths,
|
|
93
|
+
getEditorTheme,
|
|
94
|
+
getMarkdownTheme,
|
|
95
|
+
getThemeByName,
|
|
96
|
+
initTheme,
|
|
97
|
+
onThemeChange,
|
|
98
|
+
setRegisteredThemes,
|
|
99
|
+
setTheme,
|
|
100
|
+
setThemeInstance,
|
|
101
|
+
Theme,
|
|
102
|
+
type ThemeColor,
|
|
103
|
+
theme,
|
|
104
|
+
} from "./theme/theme.js";
|
|
105
|
+
|
|
106
|
+
/** Interface for components that can be expanded/collapsed */
|
|
107
|
+
interface Expandable {
|
|
108
|
+
setExpanded(expanded: boolean): void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isExpandable(obj: unknown): obj is Expandable {
|
|
112
|
+
return typeof obj === "object" && obj !== null && "setExpanded" in obj && typeof obj.setExpanded === "function";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type CompactionQueuedMessage = {
|
|
116
|
+
text: string;
|
|
117
|
+
mode: "steer" | "followUp";
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Options for InteractiveMode initialization.
|
|
122
|
+
*/
|
|
123
|
+
export interface InteractiveModeOptions {
|
|
124
|
+
/** Providers that were migrated to auth.json (shows warning) */
|
|
125
|
+
migratedProviders?: string[];
|
|
126
|
+
/** Warning message if session model couldn't be restored */
|
|
127
|
+
modelFallbackMessage?: string;
|
|
128
|
+
/** Initial message to send on startup (can include @file content) */
|
|
129
|
+
initialMessage?: string;
|
|
130
|
+
/** Images to attach to the initial message */
|
|
131
|
+
initialImages?: ImageContent[];
|
|
132
|
+
/** Additional messages to send after the initial message */
|
|
133
|
+
initialMessages?: string[];
|
|
134
|
+
/** Force verbose startup (overrides quietStartup setting) */
|
|
135
|
+
verbose?: boolean;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export class InteractiveMode {
|
|
139
|
+
private session: AgentSession;
|
|
140
|
+
private ui: TUI;
|
|
141
|
+
private chatContainer: Container;
|
|
142
|
+
private pendingMessagesContainer: Container;
|
|
143
|
+
private statusContainer: Container;
|
|
144
|
+
private defaultEditor: CustomEditor;
|
|
145
|
+
private editor: EditorComponent;
|
|
146
|
+
private autocompleteProvider: CombinedAutocompleteProvider | undefined;
|
|
147
|
+
private fdPath: string | undefined;
|
|
148
|
+
private editorContainer: Container;
|
|
149
|
+
private footer: FooterComponent;
|
|
150
|
+
private footerDataProvider: FooterDataProvider;
|
|
151
|
+
// Stored so the same manager can be injected into custom editors, selectors, and extension UI.
|
|
152
|
+
private keybindings: KeybindingsManager;
|
|
153
|
+
private version: string;
|
|
154
|
+
private isInitialized = false;
|
|
155
|
+
private onInputCallback?: (text: string) => void;
|
|
156
|
+
private loadingAnimation: Loader | undefined = undefined;
|
|
157
|
+
private pendingWorkingMessage: string | undefined = undefined;
|
|
158
|
+
private readonly defaultWorkingMessage = "Working...";
|
|
159
|
+
private readonly defaultHiddenThinkingLabel = "Thinking...";
|
|
160
|
+
private hiddenThinkingLabel = this.defaultHiddenThinkingLabel;
|
|
161
|
+
|
|
162
|
+
private lastSigintTime = 0;
|
|
163
|
+
private lastEscapeTime = 0;
|
|
164
|
+
private changelogMarkdown: string | undefined = undefined;
|
|
165
|
+
|
|
166
|
+
// Status line tracking (for mutating immediately-sequential status updates)
|
|
167
|
+
private lastStatusSpacer: Spacer | undefined = undefined;
|
|
168
|
+
private lastStatusText: Text | undefined = undefined;
|
|
169
|
+
|
|
170
|
+
// Streaming message tracking
|
|
171
|
+
private streamingComponent: AssistantMessageComponent | undefined = undefined;
|
|
172
|
+
private streamingMessage: AssistantMessage | undefined = undefined;
|
|
173
|
+
|
|
174
|
+
// Tool execution tracking: toolCallId -> component
|
|
175
|
+
private pendingTools = new Map<string, ToolExecutionComponent>();
|
|
176
|
+
|
|
177
|
+
// Tool output expansion state
|
|
178
|
+
private toolOutputExpanded = false;
|
|
179
|
+
|
|
180
|
+
// Thinking block visibility state
|
|
181
|
+
private hideThinkingBlock = false;
|
|
182
|
+
|
|
183
|
+
// Skill commands: command name -> skill file path
|
|
184
|
+
private skillCommands = new Map<string, string>();
|
|
185
|
+
|
|
186
|
+
// Agent subscription unsubscribe function
|
|
187
|
+
private unsubscribe?: () => void;
|
|
188
|
+
|
|
189
|
+
// Track if editor is in bash mode (text starts with !)
|
|
190
|
+
private isBashMode = false;
|
|
191
|
+
|
|
192
|
+
// Track current bash execution component
|
|
193
|
+
private bashComponent: BashExecutionComponent | undefined = undefined;
|
|
194
|
+
|
|
195
|
+
// Track pending bash components (shown in pending area, moved to chat on submit)
|
|
196
|
+
private pendingBashComponents: BashExecutionComponent[] = [];
|
|
197
|
+
|
|
198
|
+
// Auto-compaction state
|
|
199
|
+
private autoCompactionLoader: Loader | undefined = undefined;
|
|
200
|
+
private autoCompactionEscapeHandler?: () => void;
|
|
201
|
+
|
|
202
|
+
// Auto-retry state
|
|
203
|
+
private retryLoader: Loader | undefined = undefined;
|
|
204
|
+
private retryEscapeHandler?: () => void;
|
|
205
|
+
|
|
206
|
+
// Messages queued while compaction is running
|
|
207
|
+
private compactionQueuedMessages: CompactionQueuedMessage[] = [];
|
|
208
|
+
|
|
209
|
+
// Shutdown state
|
|
210
|
+
private shutdownRequested = false;
|
|
211
|
+
|
|
212
|
+
// Extension UI state
|
|
213
|
+
private extensionSelector: ExtensionSelectorComponent | undefined = undefined;
|
|
214
|
+
private extensionInput: ExtensionInputComponent | undefined = undefined;
|
|
215
|
+
private extensionEditor: ExtensionEditorComponent | undefined = undefined;
|
|
216
|
+
private extensionTerminalInputUnsubscribers = new Set<() => void>();
|
|
217
|
+
|
|
218
|
+
// Extension widgets (components rendered above/below the editor)
|
|
219
|
+
private extensionWidgetsAbove = new Map<string, Component & { dispose?(): void }>();
|
|
220
|
+
private extensionWidgetsBelow = new Map<string, Component & { dispose?(): void }>();
|
|
221
|
+
private widgetContainerAbove!: Container;
|
|
222
|
+
private widgetContainerBelow!: Container;
|
|
223
|
+
|
|
224
|
+
// Custom footer from extension (undefined = use built-in footer)
|
|
225
|
+
private customFooter: (Component & { dispose?(): void }) | undefined = undefined;
|
|
226
|
+
|
|
227
|
+
// Header container that holds the built-in or custom header
|
|
228
|
+
private headerContainer: Container;
|
|
229
|
+
|
|
230
|
+
// Built-in header (logo + keybinding hints + changelog)
|
|
231
|
+
private builtInHeader: Component | undefined = undefined;
|
|
232
|
+
|
|
233
|
+
// Custom header from extension (undefined = use built-in header)
|
|
234
|
+
private customHeader: (Component & { dispose?(): void }) | undefined = undefined;
|
|
235
|
+
|
|
236
|
+
// Convenience accessors
|
|
237
|
+
private get agent() {
|
|
238
|
+
return this.session.agent;
|
|
239
|
+
}
|
|
240
|
+
private get sessionManager() {
|
|
241
|
+
return this.session.sessionManager;
|
|
242
|
+
}
|
|
243
|
+
private get settingsManager() {
|
|
244
|
+
return this.session.settingsManager;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
constructor(
|
|
248
|
+
session: AgentSession,
|
|
249
|
+
private options: InteractiveModeOptions = {},
|
|
250
|
+
) {
|
|
251
|
+
this.session = session;
|
|
252
|
+
this.version = VERSION;
|
|
253
|
+
this.ui = new TUI(new ProcessTerminal(), this.settingsManager.getShowHardwareCursor());
|
|
254
|
+
this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
|
|
255
|
+
this.headerContainer = new Container();
|
|
256
|
+
this.chatContainer = new Container();
|
|
257
|
+
this.pendingMessagesContainer = new Container();
|
|
258
|
+
this.statusContainer = new Container();
|
|
259
|
+
this.widgetContainerAbove = new Container();
|
|
260
|
+
this.widgetContainerBelow = new Container();
|
|
261
|
+
this.keybindings = KeybindingsManager.create();
|
|
262
|
+
setKeybindings(this.keybindings);
|
|
263
|
+
const editorPaddingX = this.settingsManager.getEditorPaddingX();
|
|
264
|
+
const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
|
|
265
|
+
this.defaultEditor = new CustomEditor(this.ui, getEditorTheme(), this.keybindings, {
|
|
266
|
+
paddingX: editorPaddingX,
|
|
267
|
+
autocompleteMaxVisible,
|
|
268
|
+
});
|
|
269
|
+
this.editor = this.defaultEditor;
|
|
270
|
+
this.editorContainer = new Container();
|
|
271
|
+
this.editorContainer.addChild(this.editor as Component);
|
|
272
|
+
this.footerDataProvider = new FooterDataProvider();
|
|
273
|
+
this.footer = new FooterComponent(session, this.footerDataProvider);
|
|
274
|
+
this.footer.setAutoCompactEnabled(session.autoCompactionEnabled);
|
|
275
|
+
|
|
276
|
+
// Load hide thinking block setting
|
|
277
|
+
this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
|
|
278
|
+
|
|
279
|
+
// Register themes from resource loader and initialize
|
|
280
|
+
setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
|
|
281
|
+
initTheme(this.settingsManager.getTheme(), true);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private getAutocompleteSourceTag(sourceInfo?: SourceInfo): string | undefined {
|
|
285
|
+
if (!sourceInfo) {
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const scopePrefix = sourceInfo.scope === "user" ? "u" : sourceInfo.scope === "project" ? "p" : "t";
|
|
290
|
+
const source = sourceInfo.source.trim();
|
|
291
|
+
|
|
292
|
+
if (source === "auto" || source === "local" || source === "cli") {
|
|
293
|
+
return scopePrefix;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (source.startsWith("bun:")) {
|
|
297
|
+
return `${scopePrefix}:${source}`;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const gitSource = parseGitUrl(source);
|
|
301
|
+
if (gitSource) {
|
|
302
|
+
const ref = gitSource.ref ? `@${gitSource.ref}` : "";
|
|
303
|
+
return `${scopePrefix}:git:${gitSource.host}/${gitSource.path}${ref}`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return scopePrefix;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private prefixAutocompleteDescription(description: string | undefined, sourceInfo?: SourceInfo): string | undefined {
|
|
310
|
+
const sourceTag = this.getAutocompleteSourceTag(sourceInfo);
|
|
311
|
+
if (!sourceTag) {
|
|
312
|
+
return description;
|
|
313
|
+
}
|
|
314
|
+
return description ? `[${sourceTag}] ${description}` : `[${sourceTag}]`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private getBuiltInCommandConflictDiagnostics(extensionRunner: ExtensionRunner | undefined): ResourceDiagnostic[] {
|
|
318
|
+
if (!extensionRunner) {
|
|
319
|
+
return [];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const builtinNames = new Set(BUILTIN_SLASH_COMMANDS.map((command) => command.name));
|
|
323
|
+
return extensionRunner
|
|
324
|
+
.getRegisteredCommands()
|
|
325
|
+
.filter((command) => builtinNames.has(command.name))
|
|
326
|
+
.map((command) => ({
|
|
327
|
+
type: "warning" as const,
|
|
328
|
+
message:
|
|
329
|
+
command.invocationName === command.name
|
|
330
|
+
? `Extension command '/${command.name}' conflicts with built-in interactive command. Skipping in autocomplete.`
|
|
331
|
+
: `Extension command '/${command.name}' conflicts with built-in interactive command. Available as '/${command.invocationName}'.`,
|
|
332
|
+
path: command.sourceInfo.path,
|
|
333
|
+
}));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private setupAutocomplete(fdPath: string | undefined): void {
|
|
337
|
+
// Define commands for autocomplete
|
|
338
|
+
const slashCommands: SlashCommand[] = BUILTIN_SLASH_COMMANDS.map((command) => ({
|
|
339
|
+
name: command.name,
|
|
340
|
+
description: command.description,
|
|
341
|
+
}));
|
|
342
|
+
|
|
343
|
+
const modelCommand = slashCommands.find((command) => command.name === "model");
|
|
344
|
+
if (modelCommand) {
|
|
345
|
+
modelCommand.getArgumentCompletions = (prefix: string): AutocompleteItem[] | null => {
|
|
346
|
+
// Get available models (scoped or from registry)
|
|
347
|
+
const models =
|
|
348
|
+
this.session.scopedModels.length > 0
|
|
349
|
+
? this.session.scopedModels.map((s) => s.model)
|
|
350
|
+
: this.session.modelRegistry.getAvailable();
|
|
351
|
+
|
|
352
|
+
if (models.length === 0) return null;
|
|
353
|
+
|
|
354
|
+
// Create items with provider/id format
|
|
355
|
+
const items = models.map((m) => ({
|
|
356
|
+
id: m.id,
|
|
357
|
+
provider: m.provider,
|
|
358
|
+
label: `${m.provider}/${m.id}`,
|
|
359
|
+
}));
|
|
360
|
+
|
|
361
|
+
// Fuzzy filter by model ID + provider (allows "opus anthropic" to match)
|
|
362
|
+
const filtered = fuzzyFilter(items, prefix, (item) => `${item.id} ${item.provider}`);
|
|
363
|
+
|
|
364
|
+
if (filtered.length === 0) return null;
|
|
365
|
+
|
|
366
|
+
return filtered.map((item) => ({
|
|
367
|
+
value: item.label,
|
|
368
|
+
label: item.id,
|
|
369
|
+
description: item.provider,
|
|
370
|
+
}));
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Convert prompt templates to SlashCommand format for autocomplete
|
|
375
|
+
const templateCommands: SlashCommand[] = this.session.promptTemplates.map((cmd) => ({
|
|
376
|
+
name: cmd.name,
|
|
377
|
+
description: this.prefixAutocompleteDescription(cmd.description, cmd.sourceInfo),
|
|
378
|
+
}));
|
|
379
|
+
|
|
380
|
+
// Convert extension commands to SlashCommand format
|
|
381
|
+
const builtinCommandNames = new Set(slashCommands.map((c) => c.name));
|
|
382
|
+
const extensionCommands: SlashCommand[] = (
|
|
383
|
+
this.session.extensionRunner?.getRegisteredCommands().filter((cmd) => !builtinCommandNames.has(cmd.name)) ?? []
|
|
384
|
+
).map((cmd) => ({
|
|
385
|
+
name: cmd.invocationName,
|
|
386
|
+
description: this.prefixAutocompleteDescription(cmd.description, cmd.sourceInfo),
|
|
387
|
+
getArgumentCompletions: cmd.getArgumentCompletions,
|
|
388
|
+
}));
|
|
389
|
+
|
|
390
|
+
// Build skill commands from session.skills (if enabled)
|
|
391
|
+
this.skillCommands.clear();
|
|
392
|
+
const skillCommandList: SlashCommand[] = [];
|
|
393
|
+
if (this.settingsManager.getEnableSkillCommands()) {
|
|
394
|
+
for (const skill of this.session.resourceLoader.getSkills().skills) {
|
|
395
|
+
const commandName = `skill:${skill.name}`;
|
|
396
|
+
this.skillCommands.set(commandName, skill.filePath);
|
|
397
|
+
skillCommandList.push({
|
|
398
|
+
name: commandName,
|
|
399
|
+
description: this.prefixAutocompleteDescription(skill.description, skill.sourceInfo),
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Setup autocomplete
|
|
405
|
+
this.autocompleteProvider = new CombinedAutocompleteProvider(
|
|
406
|
+
[...slashCommands, ...templateCommands, ...extensionCommands, ...skillCommandList],
|
|
407
|
+
process.cwd(),
|
|
408
|
+
fdPath,
|
|
409
|
+
);
|
|
410
|
+
this.defaultEditor.setAutocompleteProvider(this.autocompleteProvider);
|
|
411
|
+
if (this.editor !== this.defaultEditor) {
|
|
412
|
+
this.editor.setAutocompleteProvider?.(this.autocompleteProvider);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async init(): Promise<void> {
|
|
417
|
+
if (this.isInitialized) return;
|
|
418
|
+
|
|
419
|
+
// Load changelog (only show new entries, skip for resumed sessions)
|
|
420
|
+
this.changelogMarkdown = this.getChangelogForDisplay();
|
|
421
|
+
|
|
422
|
+
// Ensure fd and rg are available (downloads if missing, adds to PATH via getBinDir)
|
|
423
|
+
// Both are needed: fd for autocomplete, rg for grep tool and bash commands
|
|
424
|
+
const [fdPath] = await Promise.all([ensureTool("fd"), ensureTool("rg")]);
|
|
425
|
+
this.fdPath = fdPath;
|
|
426
|
+
|
|
427
|
+
// Add header container as first child
|
|
428
|
+
this.ui.addChild(this.headerContainer);
|
|
429
|
+
|
|
430
|
+
// Add header with keybindings from config (unless silenced)
|
|
431
|
+
if (this.options.verbose || !this.settingsManager.getQuietStartup()) {
|
|
432
|
+
const logo = theme.bold(theme.fg("accent", APP_NAME)) + theme.fg("dim", ` v${this.version}`);
|
|
433
|
+
|
|
434
|
+
// Build startup instructions using keybinding hint helpers
|
|
435
|
+
const hint = (keybinding: AppKeybinding, description: string) => keyHint(keybinding, description);
|
|
436
|
+
|
|
437
|
+
const instructions = [
|
|
438
|
+
hint("app.interrupt", "to interrupt"),
|
|
439
|
+
hint("app.clear", "to clear"),
|
|
440
|
+
rawKeyHint(`${keyText("app.clear")} twice`, "to exit"),
|
|
441
|
+
hint("app.exit", "to exit (empty)"),
|
|
442
|
+
hint("app.suspend", "to suspend"),
|
|
443
|
+
keyHint("tui.editor.deleteToLineEnd", "to delete to end"),
|
|
444
|
+
hint("app.thinking.cycle", "to cycle thinking level"),
|
|
445
|
+
rawKeyHint(`${keyText("app.model.cycleForward")}/${keyText("app.model.cycleBackward")}`, "to cycle models"),
|
|
446
|
+
hint("app.model.select", "to select model"),
|
|
447
|
+
hint("app.tools.expand", "to expand tools"),
|
|
448
|
+
hint("app.thinking.toggle", "to expand thinking"),
|
|
449
|
+
hint("app.editor.external", "for external editor"),
|
|
450
|
+
rawKeyHint("/", "for commands"),
|
|
451
|
+
rawKeyHint("!", "to run bash"),
|
|
452
|
+
rawKeyHint("!!", "to run bash (no context)"),
|
|
453
|
+
hint("app.message.followUp", "to queue follow-up"),
|
|
454
|
+
hint("app.message.dequeue", "to edit all queued messages"),
|
|
455
|
+
hint("app.clipboard.pasteImage", "to paste image"),
|
|
456
|
+
rawKeyHint("drop files", "to attach"),
|
|
457
|
+
].join("\n");
|
|
458
|
+
const onboarding = theme.fg(
|
|
459
|
+
"dim",
|
|
460
|
+
`Pi can explain its own features and look up its docs. Ask it how to use or extend Pi.`,
|
|
461
|
+
);
|
|
462
|
+
this.builtInHeader = new Text(`${logo}\n${instructions}\n\n${onboarding}`, 1, 0);
|
|
463
|
+
|
|
464
|
+
// Setup UI layout
|
|
465
|
+
this.headerContainer.addChild(new Spacer(1));
|
|
466
|
+
this.headerContainer.addChild(this.builtInHeader);
|
|
467
|
+
this.headerContainer.addChild(new Spacer(1));
|
|
468
|
+
|
|
469
|
+
// Add changelog if provided
|
|
470
|
+
if (this.changelogMarkdown) {
|
|
471
|
+
this.headerContainer.addChild(new DynamicBorder());
|
|
472
|
+
if (this.settingsManager.getCollapseChangelog()) {
|
|
473
|
+
const versionMatch = this.changelogMarkdown.match(/##\s+\[?(\d+\.\d+\.\d+)\]?/);
|
|
474
|
+
const latestVersion = versionMatch ? versionMatch[1] : this.version;
|
|
475
|
+
const condensedText = `Updated to v${latestVersion}. Use ${theme.bold("/changelog")} to view full changelog.`;
|
|
476
|
+
this.headerContainer.addChild(new Text(condensedText, 1, 0));
|
|
477
|
+
} else {
|
|
478
|
+
this.headerContainer.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
|
|
479
|
+
this.headerContainer.addChild(new Spacer(1));
|
|
480
|
+
this.headerContainer.addChild(
|
|
481
|
+
new Markdown(this.changelogMarkdown.trim(), 1, 0, this.getMarkdownThemeWithSettings()),
|
|
482
|
+
);
|
|
483
|
+
this.headerContainer.addChild(new Spacer(1));
|
|
484
|
+
}
|
|
485
|
+
this.headerContainer.addChild(new DynamicBorder());
|
|
486
|
+
}
|
|
487
|
+
} else {
|
|
488
|
+
// Minimal header when silenced
|
|
489
|
+
this.builtInHeader = new Text("", 0, 0);
|
|
490
|
+
this.headerContainer.addChild(this.builtInHeader);
|
|
491
|
+
if (this.changelogMarkdown) {
|
|
492
|
+
// Still show changelog notification even in silent mode
|
|
493
|
+
this.headerContainer.addChild(new Spacer(1));
|
|
494
|
+
const versionMatch = this.changelogMarkdown.match(/##\s+\[?(\d+\.\d+\.\d+)\]?/);
|
|
495
|
+
const latestVersion = versionMatch ? versionMatch[1] : this.version;
|
|
496
|
+
const condensedText = `Updated to v${latestVersion}. Use ${theme.bold("/changelog")} to view full changelog.`;
|
|
497
|
+
this.headerContainer.addChild(new Text(condensedText, 1, 0));
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
this.ui.addChild(this.chatContainer);
|
|
502
|
+
this.ui.addChild(this.pendingMessagesContainer);
|
|
503
|
+
this.ui.addChild(this.statusContainer);
|
|
504
|
+
this.renderWidgets(); // Initialize with default spacer
|
|
505
|
+
this.ui.addChild(this.widgetContainerAbove);
|
|
506
|
+
this.ui.addChild(this.editorContainer);
|
|
507
|
+
this.ui.addChild(this.widgetContainerBelow);
|
|
508
|
+
this.ui.addChild(this.footer);
|
|
509
|
+
this.ui.setFocus(this.editor);
|
|
510
|
+
|
|
511
|
+
this.setupKeyHandlers();
|
|
512
|
+
this.setupEditorSubmitHandler();
|
|
513
|
+
|
|
514
|
+
// Start the UI before initializing extensions so session_start handlers can use interactive dialogs
|
|
515
|
+
this.ui.start();
|
|
516
|
+
this.isInitialized = true;
|
|
517
|
+
|
|
518
|
+
// Initialize extensions first so resources are shown before messages
|
|
519
|
+
await this.initExtensions();
|
|
520
|
+
|
|
521
|
+
// Render initial messages AFTER showing loaded resources
|
|
522
|
+
this.renderInitialMessages();
|
|
523
|
+
|
|
524
|
+
// Set terminal title
|
|
525
|
+
this.updateTerminalTitle();
|
|
526
|
+
|
|
527
|
+
// Subscribe to agent events
|
|
528
|
+
this.subscribeToAgent();
|
|
529
|
+
|
|
530
|
+
// Set up theme file watcher
|
|
531
|
+
onThemeChange(() => {
|
|
532
|
+
this.ui.invalidate();
|
|
533
|
+
this.updateEditorBorderColor();
|
|
534
|
+
this.ui.requestRender();
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
// Set up git branch watcher (uses provider instead of footer)
|
|
538
|
+
this.footerDataProvider.onBranchChange(() => {
|
|
539
|
+
this.ui.requestRender();
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
// Initialize available provider count for footer display
|
|
543
|
+
await this.updateAvailableProviderCount();
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Update terminal title with session name and cwd.
|
|
548
|
+
*/
|
|
549
|
+
private updateTerminalTitle(): void {
|
|
550
|
+
const cwdBasename = path.basename(process.cwd());
|
|
551
|
+
const sessionName = this.sessionManager.getSessionName();
|
|
552
|
+
if (sessionName) {
|
|
553
|
+
this.ui.terminal.setTitle(`π - ${sessionName} - ${cwdBasename}`);
|
|
554
|
+
} else {
|
|
555
|
+
this.ui.terminal.setTitle(`π - ${cwdBasename}`);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Run the interactive mode. This is the main entry point.
|
|
561
|
+
* Initializes the UI, shows warnings, processes initial messages, and starts the interactive loop.
|
|
562
|
+
*/
|
|
563
|
+
async run(): Promise<void> {
|
|
564
|
+
await this.init();
|
|
565
|
+
|
|
566
|
+
// Start version check asynchronously
|
|
567
|
+
this.checkForNewVersion().then((newVersion) => {
|
|
568
|
+
if (newVersion) {
|
|
569
|
+
this.showNewVersionNotification(newVersion);
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
// Start package update check asynchronously
|
|
574
|
+
this.checkForPackageUpdates().then((updates) => {
|
|
575
|
+
if (updates.length > 0) {
|
|
576
|
+
this.showPackageUpdateNotification(updates);
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
// Check tmux keyboard setup asynchronously
|
|
581
|
+
this.checkTmuxKeyboardSetup().then((warning) => {
|
|
582
|
+
if (warning) {
|
|
583
|
+
this.showWarning(warning);
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
// Show startup warnings
|
|
588
|
+
const { migratedProviders, modelFallbackMessage, initialMessage, initialImages, initialMessages } = this.options;
|
|
589
|
+
|
|
590
|
+
if (migratedProviders && migratedProviders.length > 0) {
|
|
591
|
+
this.showWarning(`Migrated credentials to auth.json: ${migratedProviders.join(", ")}`);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const modelsJsonError = this.session.modelRegistry.getError();
|
|
595
|
+
if (modelsJsonError) {
|
|
596
|
+
this.showError(`models.json error: ${modelsJsonError}`);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (modelFallbackMessage) {
|
|
600
|
+
this.showWarning(modelFallbackMessage);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// Process initial messages
|
|
604
|
+
if (initialMessage) {
|
|
605
|
+
try {
|
|
606
|
+
await this.session.prompt(initialMessage, { images: initialImages });
|
|
607
|
+
} catch (error: unknown) {
|
|
608
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
609
|
+
this.showError(errorMessage);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (initialMessages) {
|
|
614
|
+
for (const message of initialMessages) {
|
|
615
|
+
try {
|
|
616
|
+
await this.session.prompt(message);
|
|
617
|
+
} catch (error: unknown) {
|
|
618
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
619
|
+
this.showError(errorMessage);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// Main interactive loop
|
|
625
|
+
while (true) {
|
|
626
|
+
const userInput = await this.getUserInput();
|
|
627
|
+
try {
|
|
628
|
+
await this.session.prompt(userInput);
|
|
629
|
+
} catch (error: unknown) {
|
|
630
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
631
|
+
this.showError(errorMessage);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Check the published registry for a newer version.
|
|
638
|
+
*/
|
|
639
|
+
private async checkForNewVersion(): Promise<string | undefined> {
|
|
640
|
+
return checkForPublishedCliUpdate(this.version);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
private async checkForPackageUpdates(): Promise<string[]> {
|
|
644
|
+
if (process.env.PI_OFFLINE) {
|
|
645
|
+
return [];
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
try {
|
|
649
|
+
const packageManager = new DefaultPackageManager({
|
|
650
|
+
cwd: process.cwd(),
|
|
651
|
+
agentDir: getAgentDir(),
|
|
652
|
+
settingsManager: this.settingsManager,
|
|
653
|
+
});
|
|
654
|
+
const updates = await packageManager.checkForAvailableUpdates();
|
|
655
|
+
return updates.map((update) => update.displayName);
|
|
656
|
+
} catch {
|
|
657
|
+
return [];
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
private async checkTmuxKeyboardSetup(): Promise<string | undefined> {
|
|
662
|
+
if (!process.env.TMUX) return undefined;
|
|
663
|
+
|
|
664
|
+
const runTmuxShow = (option: string): Promise<string | undefined> => {
|
|
665
|
+
return new Promise((resolve) => {
|
|
666
|
+
const proc = spawn("tmux", ["show", "-gv", option], {
|
|
667
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
668
|
+
});
|
|
669
|
+
let stdout = "";
|
|
670
|
+
const timer = setTimeout(() => {
|
|
671
|
+
proc.kill();
|
|
672
|
+
resolve(undefined);
|
|
673
|
+
}, 2000);
|
|
674
|
+
|
|
675
|
+
proc.stdout?.on("data", (data) => {
|
|
676
|
+
stdout += data.toString();
|
|
677
|
+
});
|
|
678
|
+
proc.on("error", () => {
|
|
679
|
+
clearTimeout(timer);
|
|
680
|
+
resolve(undefined);
|
|
681
|
+
});
|
|
682
|
+
proc.on("close", (code) => {
|
|
683
|
+
clearTimeout(timer);
|
|
684
|
+
resolve(code === 0 ? stdout.trim() : undefined);
|
|
685
|
+
});
|
|
686
|
+
});
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
const [extendedKeys, extendedKeysFormat] = await Promise.all([
|
|
690
|
+
runTmuxShow("extended-keys"),
|
|
691
|
+
runTmuxShow("extended-keys-format"),
|
|
692
|
+
]);
|
|
693
|
+
|
|
694
|
+
// If we couldn't query tmux (timeout, sandbox, etc.), don't warn
|
|
695
|
+
if (extendedKeys === undefined) return undefined;
|
|
696
|
+
|
|
697
|
+
if (extendedKeys !== "on" && extendedKeys !== "always") {
|
|
698
|
+
return "tmux extended-keys is off. Modified Enter keys may not work. Add `set -g extended-keys on` to ~/.tmux.conf and restart tmux.";
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
if (extendedKeysFormat === "xterm") {
|
|
702
|
+
return "tmux extended-keys-format is xterm. Pi works best with csi-u. Add `set -g extended-keys-format csi-u` to ~/.tmux.conf and restart tmux.";
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
return undefined;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Get changelog entries to display on startup.
|
|
710
|
+
* Only shows new entries since last seen version, skips for resumed sessions.
|
|
711
|
+
*/
|
|
712
|
+
private getChangelogForDisplay(): string | undefined {
|
|
713
|
+
// Skip changelog for resumed/continued sessions (already have messages)
|
|
714
|
+
if (this.session.state.messages.length > 0) {
|
|
715
|
+
return undefined;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const lastVersion = this.settingsManager.getLastChangelogVersion();
|
|
719
|
+
const changelogPath = getChangelogPath();
|
|
720
|
+
const entries = parseChangelog(changelogPath);
|
|
721
|
+
|
|
722
|
+
if (!lastVersion) {
|
|
723
|
+
// Fresh install - just record the version, don't show changelog
|
|
724
|
+
this.settingsManager.setLastChangelogVersion(VERSION);
|
|
725
|
+
return undefined;
|
|
726
|
+
} else {
|
|
727
|
+
const newEntries = getNewEntries(entries, lastVersion);
|
|
728
|
+
if (newEntries.length > 0) {
|
|
729
|
+
this.settingsManager.setLastChangelogVersion(VERSION);
|
|
730
|
+
return newEntries.map((e) => e.content).join("\n\n");
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
return undefined;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
private getMarkdownThemeWithSettings(): MarkdownTheme {
|
|
738
|
+
return {
|
|
739
|
+
...getMarkdownTheme(),
|
|
740
|
+
codeBlockIndent: this.settingsManager.getCodeBlockIndent(),
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// =========================================================================
|
|
745
|
+
// Extension System
|
|
746
|
+
// =========================================================================
|
|
747
|
+
|
|
748
|
+
private formatDisplayPath(p: string): string {
|
|
749
|
+
const home = os.homedir();
|
|
750
|
+
let result = p;
|
|
751
|
+
|
|
752
|
+
// Replace home directory with ~
|
|
753
|
+
if (result.startsWith(home)) {
|
|
754
|
+
result = `~${result.slice(home.length)}`;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
return result;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Get a short path relative to the package root for display.
|
|
762
|
+
*/
|
|
763
|
+
private getShortPath(fullPath: string, sourceInfo?: SourceInfo): string {
|
|
764
|
+
const source = sourceInfo?.source ?? "";
|
|
765
|
+
const packageMatch = fullPath.match(/node_modules\/(@?[^/]+(?:\/[^/]+)?)\/(.*)/);
|
|
766
|
+
if (packageMatch && source.startsWith("bun:")) {
|
|
767
|
+
return packageMatch[2];
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
const gitMatch = fullPath.match(/git\/[^/]+\/[^/]+\/(.*)/);
|
|
771
|
+
if (gitMatch && source.startsWith("git:")) {
|
|
772
|
+
return gitMatch[1];
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
return this.formatDisplayPath(fullPath);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
private getDisplaySourceInfo(sourceInfo?: SourceInfo): {
|
|
779
|
+
label: string;
|
|
780
|
+
scopeLabel?: string;
|
|
781
|
+
color: "accent" | "muted";
|
|
782
|
+
} {
|
|
783
|
+
const source = sourceInfo?.source ?? "local";
|
|
784
|
+
const scope = sourceInfo?.scope ?? "project";
|
|
785
|
+
if (source === "local") {
|
|
786
|
+
if (scope === "user") {
|
|
787
|
+
return { label: "user", color: "muted" };
|
|
788
|
+
}
|
|
789
|
+
if (scope === "project") {
|
|
790
|
+
return { label: "project", color: "muted" };
|
|
791
|
+
}
|
|
792
|
+
if (scope === "temporary") {
|
|
793
|
+
return { label: "path", scopeLabel: "temp", color: "muted" };
|
|
794
|
+
}
|
|
795
|
+
return { label: "path", color: "muted" };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
if (source === "cli") {
|
|
799
|
+
return {
|
|
800
|
+
label: "path",
|
|
801
|
+
scopeLabel: scope === "temporary" ? "temp" : undefined,
|
|
802
|
+
color: "muted",
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
const scopeLabel =
|
|
807
|
+
scope === "user" ? "user" : scope === "project" ? "project" : scope === "temporary" ? "temp" : undefined;
|
|
808
|
+
return { label: source, scopeLabel, color: "accent" };
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
private getScopeGroup(sourceInfo?: SourceInfo): "user" | "project" | "path" {
|
|
812
|
+
const source = sourceInfo?.source ?? "local";
|
|
813
|
+
const scope = sourceInfo?.scope ?? "project";
|
|
814
|
+
if (source === "cli" || scope === "temporary") return "path";
|
|
815
|
+
if (scope === "user") return "user";
|
|
816
|
+
if (scope === "project") return "project";
|
|
817
|
+
return "path";
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
private isPackageSource(sourceInfo?: SourceInfo): boolean {
|
|
821
|
+
const source = sourceInfo?.source ?? "";
|
|
822
|
+
return source.startsWith("bun:") || source.startsWith("git:");
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
private buildScopeGroups(items: Array<{ path: string; sourceInfo?: SourceInfo }>): Array<{
|
|
826
|
+
scope: "user" | "project" | "path";
|
|
827
|
+
paths: Array<{ path: string; sourceInfo?: SourceInfo }>;
|
|
828
|
+
packages: Map<string, Array<{ path: string; sourceInfo?: SourceInfo }>>;
|
|
829
|
+
}> {
|
|
830
|
+
const groups: Record<
|
|
831
|
+
"user" | "project" | "path",
|
|
832
|
+
{
|
|
833
|
+
scope: "user" | "project" | "path";
|
|
834
|
+
paths: Array<{ path: string; sourceInfo?: SourceInfo }>;
|
|
835
|
+
packages: Map<string, Array<{ path: string; sourceInfo?: SourceInfo }>>;
|
|
836
|
+
}
|
|
837
|
+
> = {
|
|
838
|
+
user: { scope: "user", paths: [], packages: new Map() },
|
|
839
|
+
project: { scope: "project", paths: [], packages: new Map() },
|
|
840
|
+
path: { scope: "path", paths: [], packages: new Map() },
|
|
841
|
+
};
|
|
842
|
+
|
|
843
|
+
for (const item of items) {
|
|
844
|
+
const groupKey = this.getScopeGroup(item.sourceInfo);
|
|
845
|
+
const group = groups[groupKey];
|
|
846
|
+
const source = item.sourceInfo?.source ?? "local";
|
|
847
|
+
|
|
848
|
+
if (this.isPackageSource(item.sourceInfo)) {
|
|
849
|
+
const list = group.packages.get(source) ?? [];
|
|
850
|
+
list.push(item);
|
|
851
|
+
group.packages.set(source, list);
|
|
852
|
+
} else {
|
|
853
|
+
group.paths.push(item);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
return [groups.project, groups.user, groups.path].filter(
|
|
858
|
+
(group) => group.paths.length > 0 || group.packages.size > 0,
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
private formatScopeGroups(
|
|
863
|
+
groups: Array<{
|
|
864
|
+
scope: "user" | "project" | "path";
|
|
865
|
+
paths: Array<{ path: string; sourceInfo?: SourceInfo }>;
|
|
866
|
+
packages: Map<string, Array<{ path: string; sourceInfo?: SourceInfo }>>;
|
|
867
|
+
}>,
|
|
868
|
+
options: {
|
|
869
|
+
formatPath: (item: { path: string; sourceInfo?: SourceInfo }) => string;
|
|
870
|
+
formatPackagePath: (item: { path: string; sourceInfo?: SourceInfo }, source: string) => string;
|
|
871
|
+
},
|
|
872
|
+
): string {
|
|
873
|
+
const lines: string[] = [];
|
|
874
|
+
|
|
875
|
+
for (const group of groups) {
|
|
876
|
+
lines.push(` ${theme.fg("accent", group.scope)}`);
|
|
877
|
+
|
|
878
|
+
const sortedPaths = [...group.paths].sort((a, b) => a.path.localeCompare(b.path));
|
|
879
|
+
for (const item of sortedPaths) {
|
|
880
|
+
lines.push(theme.fg("dim", ` ${options.formatPath(item)}`));
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
const sortedPackages = Array.from(group.packages.entries()).sort(([a], [b]) => a.localeCompare(b));
|
|
884
|
+
for (const [source, items] of sortedPackages) {
|
|
885
|
+
lines.push(` ${theme.fg("mdLink", source)}`);
|
|
886
|
+
const sortedPackagePaths = [...items].sort((a, b) => a.path.localeCompare(b.path));
|
|
887
|
+
for (const item of sortedPackagePaths) {
|
|
888
|
+
lines.push(theme.fg("dim", ` ${options.formatPackagePath(item, source)}`));
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
return lines.join("\n");
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
private findSourceInfoForPath(p: string, sourceInfos: Map<string, SourceInfo>): SourceInfo | undefined {
|
|
897
|
+
const exact = sourceInfos.get(p);
|
|
898
|
+
if (exact) return exact;
|
|
899
|
+
|
|
900
|
+
let current = p;
|
|
901
|
+
while (current.includes("/")) {
|
|
902
|
+
current = current.substring(0, current.lastIndexOf("/"));
|
|
903
|
+
const parent = sourceInfos.get(current);
|
|
904
|
+
if (parent) return parent;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
return undefined;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
private formatPathWithSource(p: string, sourceInfo?: SourceInfo): string {
|
|
911
|
+
if (sourceInfo) {
|
|
912
|
+
const shortPath = this.getShortPath(p, sourceInfo);
|
|
913
|
+
const { label, scopeLabel } = this.getDisplaySourceInfo(sourceInfo);
|
|
914
|
+
const labelText = scopeLabel ? `${label} (${scopeLabel})` : label;
|
|
915
|
+
return `${labelText} ${shortPath}`;
|
|
916
|
+
}
|
|
917
|
+
return this.formatDisplayPath(p);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
private formatDiagnostics(diagnostics: readonly ResourceDiagnostic[], sourceInfos: Map<string, SourceInfo>): string {
|
|
921
|
+
const lines: string[] = [];
|
|
922
|
+
|
|
923
|
+
// Group collision diagnostics by name
|
|
924
|
+
const collisions = new Map<string, ResourceDiagnostic[]>();
|
|
925
|
+
const otherDiagnostics: ResourceDiagnostic[] = [];
|
|
926
|
+
|
|
927
|
+
for (const d of diagnostics) {
|
|
928
|
+
if (d.type === "collision" && d.collision) {
|
|
929
|
+
const list = collisions.get(d.collision.name) ?? [];
|
|
930
|
+
list.push(d);
|
|
931
|
+
collisions.set(d.collision.name, list);
|
|
932
|
+
} else {
|
|
933
|
+
otherDiagnostics.push(d);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
// Format collision diagnostics grouped by name
|
|
938
|
+
for (const [name, collisionList] of collisions) {
|
|
939
|
+
const first = collisionList[0]?.collision;
|
|
940
|
+
if (!first) continue;
|
|
941
|
+
lines.push(theme.fg("warning", ` "${name}" collision:`));
|
|
942
|
+
lines.push(
|
|
943
|
+
theme.fg(
|
|
944
|
+
"dim",
|
|
945
|
+
` ${theme.fg("success", "✓")} ${this.formatPathWithSource(first.winnerPath, this.findSourceInfoForPath(first.winnerPath, sourceInfos))}`,
|
|
946
|
+
),
|
|
947
|
+
);
|
|
948
|
+
for (const d of collisionList) {
|
|
949
|
+
if (d.collision) {
|
|
950
|
+
lines.push(
|
|
951
|
+
theme.fg(
|
|
952
|
+
"dim",
|
|
953
|
+
` ${theme.fg("warning", "✗")} ${this.formatPathWithSource(d.collision.loserPath, this.findSourceInfoForPath(d.collision.loserPath, sourceInfos))} (skipped)`,
|
|
954
|
+
),
|
|
955
|
+
);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
for (const d of otherDiagnostics) {
|
|
961
|
+
if (d.path) {
|
|
962
|
+
const formattedPath = this.formatPathWithSource(d.path, this.findSourceInfoForPath(d.path, sourceInfos));
|
|
963
|
+
lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${formattedPath}`));
|
|
964
|
+
lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${d.message}`));
|
|
965
|
+
} else {
|
|
966
|
+
lines.push(theme.fg(d.type === "error" ? "error" : "warning", ` ${d.message}`));
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
return lines.join("\n");
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
private showLoadedResources(options?: {
|
|
974
|
+
extensions?: Array<{ path: string; sourceInfo?: SourceInfo }>;
|
|
975
|
+
force?: boolean;
|
|
976
|
+
showDiagnosticsWhenQuiet?: boolean;
|
|
977
|
+
}): void {
|
|
978
|
+
const showListing = options?.force || this.options.verbose || !this.settingsManager.getQuietStartup();
|
|
979
|
+
const showDiagnostics = showListing || options?.showDiagnosticsWhenQuiet === true;
|
|
980
|
+
if (!showListing && !showDiagnostics) {
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
const sectionHeader = (name: string, color: ThemeColor = "mdHeading") => theme.fg(color, `[${name}]`);
|
|
985
|
+
|
|
986
|
+
const skillsResult = this.session.resourceLoader.getSkills();
|
|
987
|
+
const promptsResult = this.session.resourceLoader.getPrompts();
|
|
988
|
+
const themesResult = this.session.resourceLoader.getThemes();
|
|
989
|
+
const extensions =
|
|
990
|
+
options?.extensions ??
|
|
991
|
+
this.session.resourceLoader.getExtensions().extensions.map((extension) => ({
|
|
992
|
+
path: extension.path,
|
|
993
|
+
sourceInfo: extension.sourceInfo,
|
|
994
|
+
}));
|
|
995
|
+
const sourceInfos = new Map<string, SourceInfo>();
|
|
996
|
+
for (const extension of extensions) {
|
|
997
|
+
if (extension.sourceInfo) {
|
|
998
|
+
sourceInfos.set(extension.path, extension.sourceInfo);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
for (const skill of skillsResult.skills) {
|
|
1002
|
+
if (skill.sourceInfo) {
|
|
1003
|
+
sourceInfos.set(skill.filePath, skill.sourceInfo);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
for (const prompt of promptsResult.prompts) {
|
|
1007
|
+
if (prompt.sourceInfo) {
|
|
1008
|
+
sourceInfos.set(prompt.filePath, prompt.sourceInfo);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
for (const loadedTheme of themesResult.themes) {
|
|
1012
|
+
if (loadedTheme.sourcePath && loadedTheme.sourceInfo) {
|
|
1013
|
+
sourceInfos.set(loadedTheme.sourcePath, loadedTheme.sourceInfo);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
if (showListing) {
|
|
1018
|
+
const contextFiles = this.session.resourceLoader.getAgentsFiles().agentsFiles;
|
|
1019
|
+
if (contextFiles.length > 0) {
|
|
1020
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1021
|
+
const contextList = contextFiles
|
|
1022
|
+
.map((f) => theme.fg("dim", ` ${this.formatDisplayPath(f.path)}`))
|
|
1023
|
+
.join("\n");
|
|
1024
|
+
this.chatContainer.addChild(new Text(`${sectionHeader("Context")}\n${contextList}`, 0, 0));
|
|
1025
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
const skills = skillsResult.skills;
|
|
1029
|
+
if (skills.length > 0) {
|
|
1030
|
+
const groups = this.buildScopeGroups(
|
|
1031
|
+
skills.map((skill) => ({
|
|
1032
|
+
path: skill.filePath,
|
|
1033
|
+
sourceInfo: skill.sourceInfo,
|
|
1034
|
+
})),
|
|
1035
|
+
);
|
|
1036
|
+
const skillList = this.formatScopeGroups(groups, {
|
|
1037
|
+
formatPath: (item) => this.formatDisplayPath(item.path),
|
|
1038
|
+
formatPackagePath: (item) => this.getShortPath(item.path, item.sourceInfo),
|
|
1039
|
+
});
|
|
1040
|
+
this.chatContainer.addChild(new Text(`${sectionHeader("Skills")}\n${skillList}`, 0, 0));
|
|
1041
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
const templates = this.session.promptTemplates;
|
|
1045
|
+
if (templates.length > 0) {
|
|
1046
|
+
const groups = this.buildScopeGroups(
|
|
1047
|
+
templates.map((template) => ({
|
|
1048
|
+
path: template.filePath,
|
|
1049
|
+
sourceInfo: template.sourceInfo,
|
|
1050
|
+
})),
|
|
1051
|
+
);
|
|
1052
|
+
const templateByPath = new Map(templates.map((t) => [t.filePath, t]));
|
|
1053
|
+
const templateList = this.formatScopeGroups(groups, {
|
|
1054
|
+
formatPath: (item) => {
|
|
1055
|
+
const template = templateByPath.get(item.path);
|
|
1056
|
+
return template ? `/${template.name}` : this.formatDisplayPath(item.path);
|
|
1057
|
+
},
|
|
1058
|
+
formatPackagePath: (item) => {
|
|
1059
|
+
const template = templateByPath.get(item.path);
|
|
1060
|
+
return template ? `/${template.name}` : this.formatDisplayPath(item.path);
|
|
1061
|
+
},
|
|
1062
|
+
});
|
|
1063
|
+
this.chatContainer.addChild(new Text(`${sectionHeader("Prompts")}\n${templateList}`, 0, 0));
|
|
1064
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
if (extensions.length > 0) {
|
|
1068
|
+
const groups = this.buildScopeGroups(extensions);
|
|
1069
|
+
const extList = this.formatScopeGroups(groups, {
|
|
1070
|
+
formatPath: (item) => this.formatDisplayPath(item.path),
|
|
1071
|
+
formatPackagePath: (item) => this.getShortPath(item.path, item.sourceInfo),
|
|
1072
|
+
});
|
|
1073
|
+
this.chatContainer.addChild(new Text(`${sectionHeader("Extensions", "mdHeading")}\n${extList}`, 0, 0));
|
|
1074
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// Show loaded themes (excluding built-in)
|
|
1078
|
+
const loadedThemes = themesResult.themes;
|
|
1079
|
+
const customThemes = loadedThemes.filter((t) => t.sourcePath);
|
|
1080
|
+
if (customThemes.length > 0) {
|
|
1081
|
+
const groups = this.buildScopeGroups(
|
|
1082
|
+
customThemes.map((loadedTheme) => ({
|
|
1083
|
+
path: loadedTheme.sourcePath!,
|
|
1084
|
+
sourceInfo: loadedTheme.sourceInfo,
|
|
1085
|
+
})),
|
|
1086
|
+
);
|
|
1087
|
+
const themeList = this.formatScopeGroups(groups, {
|
|
1088
|
+
formatPath: (item) => this.formatDisplayPath(item.path),
|
|
1089
|
+
formatPackagePath: (item) => this.getShortPath(item.path, item.sourceInfo),
|
|
1090
|
+
});
|
|
1091
|
+
this.chatContainer.addChild(new Text(`${sectionHeader("Themes")}\n${themeList}`, 0, 0));
|
|
1092
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
if (showDiagnostics) {
|
|
1097
|
+
const skillDiagnostics = skillsResult.diagnostics;
|
|
1098
|
+
if (skillDiagnostics.length > 0) {
|
|
1099
|
+
const warningLines = this.formatDiagnostics(skillDiagnostics, sourceInfos);
|
|
1100
|
+
this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Skill conflicts]")}\n${warningLines}`, 0, 0));
|
|
1101
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
const promptDiagnostics = promptsResult.diagnostics;
|
|
1105
|
+
if (promptDiagnostics.length > 0) {
|
|
1106
|
+
const warningLines = this.formatDiagnostics(promptDiagnostics, sourceInfos);
|
|
1107
|
+
this.chatContainer.addChild(
|
|
1108
|
+
new Text(`${theme.fg("warning", "[Prompt conflicts]")}\n${warningLines}`, 0, 0),
|
|
1109
|
+
);
|
|
1110
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
const extensionDiagnostics: ResourceDiagnostic[] = [];
|
|
1114
|
+
const extensionErrors = this.session.resourceLoader.getExtensions().errors;
|
|
1115
|
+
if (extensionErrors.length > 0) {
|
|
1116
|
+
for (const error of extensionErrors) {
|
|
1117
|
+
extensionDiagnostics.push({
|
|
1118
|
+
type: "error",
|
|
1119
|
+
message: error.error,
|
|
1120
|
+
path: error.path,
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
const commandDiagnostics = this.session.extensionRunner?.getCommandDiagnostics() ?? [];
|
|
1126
|
+
extensionDiagnostics.push(...commandDiagnostics);
|
|
1127
|
+
extensionDiagnostics.push(...this.getBuiltInCommandConflictDiagnostics(this.session.extensionRunner));
|
|
1128
|
+
|
|
1129
|
+
const shortcutDiagnostics = this.session.extensionRunner?.getShortcutDiagnostics() ?? [];
|
|
1130
|
+
extensionDiagnostics.push(...shortcutDiagnostics);
|
|
1131
|
+
|
|
1132
|
+
if (extensionDiagnostics.length > 0) {
|
|
1133
|
+
const warningLines = this.formatDiagnostics(extensionDiagnostics, sourceInfos);
|
|
1134
|
+
this.chatContainer.addChild(
|
|
1135
|
+
new Text(`${theme.fg("warning", "[Extension issues]")}\n${warningLines}`, 0, 0),
|
|
1136
|
+
);
|
|
1137
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
const themeDiagnostics = themesResult.diagnostics;
|
|
1141
|
+
if (themeDiagnostics.length > 0) {
|
|
1142
|
+
const warningLines = this.formatDiagnostics(themeDiagnostics, sourceInfos);
|
|
1143
|
+
this.chatContainer.addChild(new Text(`${theme.fg("warning", "[Theme conflicts]")}\n${warningLines}`, 0, 0));
|
|
1144
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/**
|
|
1150
|
+
* Initialize the extension system with TUI-based UI context.
|
|
1151
|
+
*/
|
|
1152
|
+
private async initExtensions(): Promise<void> {
|
|
1153
|
+
const uiContext = this.createExtensionUIContext();
|
|
1154
|
+
await this.session.bindExtensions({
|
|
1155
|
+
uiContext,
|
|
1156
|
+
commandContextActions: {
|
|
1157
|
+
waitForIdle: () => this.session.agent.waitForIdle(),
|
|
1158
|
+
newSession: async (options) => {
|
|
1159
|
+
if (this.loadingAnimation) {
|
|
1160
|
+
this.loadingAnimation.stop();
|
|
1161
|
+
this.loadingAnimation = undefined;
|
|
1162
|
+
}
|
|
1163
|
+
this.statusContainer.clear();
|
|
1164
|
+
|
|
1165
|
+
// Delegate to AgentSession (handles setup + agent state sync)
|
|
1166
|
+
const success = await this.session.newSession(options);
|
|
1167
|
+
if (!success) {
|
|
1168
|
+
return { cancelled: true };
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
// Clear UI state
|
|
1172
|
+
this.chatContainer.clear();
|
|
1173
|
+
this.pendingMessagesContainer.clear();
|
|
1174
|
+
this.compactionQueuedMessages = [];
|
|
1175
|
+
this.streamingComponent = undefined;
|
|
1176
|
+
this.streamingMessage = undefined;
|
|
1177
|
+
this.pendingTools.clear();
|
|
1178
|
+
|
|
1179
|
+
// Render any messages added via setup, or show empty session
|
|
1180
|
+
this.renderInitialMessages();
|
|
1181
|
+
this.ui.requestRender();
|
|
1182
|
+
|
|
1183
|
+
return { cancelled: false };
|
|
1184
|
+
},
|
|
1185
|
+
fork: async (entryId) => {
|
|
1186
|
+
const result = await this.session.fork(entryId);
|
|
1187
|
+
if (result.cancelled) {
|
|
1188
|
+
return { cancelled: true };
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
this.chatContainer.clear();
|
|
1192
|
+
this.renderInitialMessages();
|
|
1193
|
+
this.editor.setText(result.selectedText);
|
|
1194
|
+
this.showStatus("Forked to new session");
|
|
1195
|
+
|
|
1196
|
+
return { cancelled: false };
|
|
1197
|
+
},
|
|
1198
|
+
navigateTree: async (targetId, options) => {
|
|
1199
|
+
const result = await this.session.navigateTree(targetId, {
|
|
1200
|
+
summarize: options?.summarize,
|
|
1201
|
+
customInstructions: options?.customInstructions,
|
|
1202
|
+
replaceInstructions: options?.replaceInstructions,
|
|
1203
|
+
label: options?.label,
|
|
1204
|
+
});
|
|
1205
|
+
if (result.cancelled) {
|
|
1206
|
+
return { cancelled: true };
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
this.chatContainer.clear();
|
|
1210
|
+
this.renderInitialMessages();
|
|
1211
|
+
if (result.editorText && !this.editor.getText().trim()) {
|
|
1212
|
+
this.editor.setText(result.editorText);
|
|
1213
|
+
}
|
|
1214
|
+
this.showStatus("Navigated to selected point");
|
|
1215
|
+
|
|
1216
|
+
return { cancelled: false };
|
|
1217
|
+
},
|
|
1218
|
+
switchSession: async (sessionPath) => {
|
|
1219
|
+
await this.handleResumeSession(sessionPath);
|
|
1220
|
+
return { cancelled: false };
|
|
1221
|
+
},
|
|
1222
|
+
reload: async () => {
|
|
1223
|
+
await this.handleReloadCommand();
|
|
1224
|
+
},
|
|
1225
|
+
},
|
|
1226
|
+
shutdownHandler: () => {
|
|
1227
|
+
this.shutdownRequested = true;
|
|
1228
|
+
if (!this.session.isStreaming) {
|
|
1229
|
+
void this.shutdown();
|
|
1230
|
+
}
|
|
1231
|
+
},
|
|
1232
|
+
onError: (error) => {
|
|
1233
|
+
this.showExtensionError(error.extensionPath, error.error, error.stack);
|
|
1234
|
+
},
|
|
1235
|
+
});
|
|
1236
|
+
|
|
1237
|
+
setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
|
|
1238
|
+
this.setupAutocomplete(this.fdPath);
|
|
1239
|
+
|
|
1240
|
+
const extensionRunner = this.session.extensionRunner;
|
|
1241
|
+
if (!extensionRunner) {
|
|
1242
|
+
this.showLoadedResources({ extensions: [], force: false });
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
this.setupExtensionShortcuts(extensionRunner);
|
|
1247
|
+
this.showLoadedResources({ force: false });
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
/**
|
|
1251
|
+
* Get a registered tool definition by name (for custom rendering).
|
|
1252
|
+
*/
|
|
1253
|
+
private getRegisteredToolDefinition(toolName: string) {
|
|
1254
|
+
return this.session.getToolDefinition(toolName);
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* Set up keyboard shortcuts registered by extensions.
|
|
1259
|
+
*/
|
|
1260
|
+
private setupExtensionShortcuts(extensionRunner: ExtensionRunner): void {
|
|
1261
|
+
const shortcuts = extensionRunner.getShortcuts(this.keybindings.getEffectiveConfig());
|
|
1262
|
+
if (shortcuts.size === 0) return;
|
|
1263
|
+
|
|
1264
|
+
// Create a context for shortcut handlers
|
|
1265
|
+
const createContext = (): ExtensionContext => ({
|
|
1266
|
+
ui: this.createExtensionUIContext(),
|
|
1267
|
+
hasUI: true,
|
|
1268
|
+
cwd: process.cwd(),
|
|
1269
|
+
sessionManager: this.sessionManager,
|
|
1270
|
+
modelRegistry: this.session.modelRegistry,
|
|
1271
|
+
model: this.session.model,
|
|
1272
|
+
isIdle: () => !this.session.isStreaming,
|
|
1273
|
+
signal: this.session.agent.signal,
|
|
1274
|
+
abort: () => this.session.abort(),
|
|
1275
|
+
hasPendingMessages: () => this.session.pendingMessageCount > 0,
|
|
1276
|
+
shutdown: () => {
|
|
1277
|
+
this.shutdownRequested = true;
|
|
1278
|
+
},
|
|
1279
|
+
getContextUsage: () => this.session.getContextUsage(),
|
|
1280
|
+
compact: (options) => {
|
|
1281
|
+
void (async () => {
|
|
1282
|
+
try {
|
|
1283
|
+
const result = await this.session.compact(options?.customInstructions);
|
|
1284
|
+
options?.onComplete?.(result);
|
|
1285
|
+
} catch (error) {
|
|
1286
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
1287
|
+
options?.onError?.(err);
|
|
1288
|
+
}
|
|
1289
|
+
})();
|
|
1290
|
+
},
|
|
1291
|
+
getSystemPrompt: () => this.session.systemPrompt,
|
|
1292
|
+
});
|
|
1293
|
+
|
|
1294
|
+
// Set up the extension shortcut handler on the default editor
|
|
1295
|
+
this.defaultEditor.onExtensionShortcut = (data: string) => {
|
|
1296
|
+
for (const [shortcutStr, shortcut] of shortcuts) {
|
|
1297
|
+
// Cast to KeyId - extension shortcuts use the same format
|
|
1298
|
+
if (matchesKey(data, shortcutStr as KeyId)) {
|
|
1299
|
+
// Run handler async, don't block input
|
|
1300
|
+
Promise.resolve(shortcut.handler(createContext())).catch((err) => {
|
|
1301
|
+
this.showError(`Shortcut handler error: ${err instanceof Error ? err.message : String(err)}`);
|
|
1302
|
+
});
|
|
1303
|
+
return true;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
return false;
|
|
1307
|
+
};
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* Set extension status text in the footer.
|
|
1312
|
+
*/
|
|
1313
|
+
private setExtensionStatus(key: string, text: string | undefined): void {
|
|
1314
|
+
this.footerDataProvider.setExtensionStatus(key, text);
|
|
1315
|
+
this.ui.requestRender();
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
private setHiddenThinkingLabel(label?: string): void {
|
|
1319
|
+
this.hiddenThinkingLabel = label ?? this.defaultHiddenThinkingLabel;
|
|
1320
|
+
for (const child of this.chatContainer.children) {
|
|
1321
|
+
if (child instanceof AssistantMessageComponent) {
|
|
1322
|
+
child.setHiddenThinkingLabel(this.hiddenThinkingLabel);
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
if (this.streamingComponent) {
|
|
1326
|
+
this.streamingComponent.setHiddenThinkingLabel(this.hiddenThinkingLabel);
|
|
1327
|
+
}
|
|
1328
|
+
this.ui.requestRender();
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
/**
|
|
1332
|
+
* Set an extension widget (string array or custom component).
|
|
1333
|
+
*/
|
|
1334
|
+
private setExtensionWidget(
|
|
1335
|
+
key: string,
|
|
1336
|
+
content: string[] | ((tui: TUI, thm: Theme) => Component & { dispose?(): void }) | undefined,
|
|
1337
|
+
options?: ExtensionWidgetOptions,
|
|
1338
|
+
): void {
|
|
1339
|
+
const placement = options?.placement ?? "aboveEditor";
|
|
1340
|
+
const removeExisting = (map: Map<string, Component & { dispose?(): void }>) => {
|
|
1341
|
+
const existing = map.get(key);
|
|
1342
|
+
if (existing?.dispose) existing.dispose();
|
|
1343
|
+
map.delete(key);
|
|
1344
|
+
};
|
|
1345
|
+
|
|
1346
|
+
removeExisting(this.extensionWidgetsAbove);
|
|
1347
|
+
removeExisting(this.extensionWidgetsBelow);
|
|
1348
|
+
|
|
1349
|
+
if (content === undefined) {
|
|
1350
|
+
this.renderWidgets();
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
let component: Component & { dispose?(): void };
|
|
1355
|
+
|
|
1356
|
+
if (Array.isArray(content)) {
|
|
1357
|
+
// Wrap string array in a Container with Text components
|
|
1358
|
+
const container = new Container();
|
|
1359
|
+
for (const line of content.slice(0, InteractiveMode.MAX_WIDGET_LINES)) {
|
|
1360
|
+
container.addChild(new Text(line, 1, 0));
|
|
1361
|
+
}
|
|
1362
|
+
if (content.length > InteractiveMode.MAX_WIDGET_LINES) {
|
|
1363
|
+
container.addChild(new Text(theme.fg("muted", "... (widget truncated)"), 1, 0));
|
|
1364
|
+
}
|
|
1365
|
+
component = container;
|
|
1366
|
+
} else {
|
|
1367
|
+
// Factory function - create component
|
|
1368
|
+
component = content(this.ui, theme);
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
const targetMap = placement === "belowEditor" ? this.extensionWidgetsBelow : this.extensionWidgetsAbove;
|
|
1372
|
+
targetMap.set(key, component);
|
|
1373
|
+
this.renderWidgets();
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
private clearExtensionWidgets(): void {
|
|
1377
|
+
for (const widget of this.extensionWidgetsAbove.values()) {
|
|
1378
|
+
widget.dispose?.();
|
|
1379
|
+
}
|
|
1380
|
+
for (const widget of this.extensionWidgetsBelow.values()) {
|
|
1381
|
+
widget.dispose?.();
|
|
1382
|
+
}
|
|
1383
|
+
this.extensionWidgetsAbove.clear();
|
|
1384
|
+
this.extensionWidgetsBelow.clear();
|
|
1385
|
+
this.renderWidgets();
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
private resetExtensionUI(): void {
|
|
1389
|
+
if (this.extensionSelector) {
|
|
1390
|
+
this.hideExtensionSelector();
|
|
1391
|
+
}
|
|
1392
|
+
if (this.extensionInput) {
|
|
1393
|
+
this.hideExtensionInput();
|
|
1394
|
+
}
|
|
1395
|
+
if (this.extensionEditor) {
|
|
1396
|
+
this.hideExtensionEditor();
|
|
1397
|
+
}
|
|
1398
|
+
this.ui.hideOverlay();
|
|
1399
|
+
this.clearExtensionTerminalInputListeners();
|
|
1400
|
+
this.setExtensionFooter(undefined);
|
|
1401
|
+
this.setExtensionHeader(undefined);
|
|
1402
|
+
this.clearExtensionWidgets();
|
|
1403
|
+
this.footerDataProvider.clearExtensionStatuses();
|
|
1404
|
+
this.footer.invalidate();
|
|
1405
|
+
this.setCustomEditorComponent(undefined);
|
|
1406
|
+
this.defaultEditor.onExtensionShortcut = undefined;
|
|
1407
|
+
this.updateTerminalTitle();
|
|
1408
|
+
if (this.loadingAnimation) {
|
|
1409
|
+
this.loadingAnimation.setMessage(`${this.defaultWorkingMessage} (${keyText("app.interrupt")} to interrupt)`);
|
|
1410
|
+
}
|
|
1411
|
+
this.setHiddenThinkingLabel();
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
// Maximum total widget lines to prevent viewport overflow
|
|
1415
|
+
private static readonly MAX_WIDGET_LINES = 10;
|
|
1416
|
+
|
|
1417
|
+
/**
|
|
1418
|
+
* Render all extension widgets to the widget container.
|
|
1419
|
+
*/
|
|
1420
|
+
private renderWidgets(): void {
|
|
1421
|
+
if (!this.widgetContainerAbove || !this.widgetContainerBelow) return;
|
|
1422
|
+
this.renderWidgetContainer(this.widgetContainerAbove, this.extensionWidgetsAbove, true, true);
|
|
1423
|
+
this.renderWidgetContainer(this.widgetContainerBelow, this.extensionWidgetsBelow, false, false);
|
|
1424
|
+
this.ui.requestRender();
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
private renderWidgetContainer(
|
|
1428
|
+
container: Container,
|
|
1429
|
+
widgets: Map<string, Component & { dispose?(): void }>,
|
|
1430
|
+
spacerWhenEmpty: boolean,
|
|
1431
|
+
leadingSpacer: boolean,
|
|
1432
|
+
): void {
|
|
1433
|
+
container.clear();
|
|
1434
|
+
|
|
1435
|
+
if (widgets.size === 0) {
|
|
1436
|
+
if (spacerWhenEmpty) {
|
|
1437
|
+
container.addChild(new Spacer(1));
|
|
1438
|
+
}
|
|
1439
|
+
return;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
if (leadingSpacer) {
|
|
1443
|
+
container.addChild(new Spacer(1));
|
|
1444
|
+
}
|
|
1445
|
+
for (const component of widgets.values()) {
|
|
1446
|
+
container.addChild(component);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
/**
|
|
1451
|
+
* Set a custom footer component, or restore the built-in footer.
|
|
1452
|
+
*/
|
|
1453
|
+
private setExtensionFooter(
|
|
1454
|
+
factory:
|
|
1455
|
+
| ((tui: TUI, thm: Theme, footerData: ReadonlyFooterDataProvider) => Component & { dispose?(): void })
|
|
1456
|
+
| undefined,
|
|
1457
|
+
): void {
|
|
1458
|
+
// Dispose existing custom footer
|
|
1459
|
+
if (this.customFooter?.dispose) {
|
|
1460
|
+
this.customFooter.dispose();
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
// Remove current footer from UI
|
|
1464
|
+
if (this.customFooter) {
|
|
1465
|
+
this.ui.removeChild(this.customFooter);
|
|
1466
|
+
} else {
|
|
1467
|
+
this.ui.removeChild(this.footer);
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
if (factory) {
|
|
1471
|
+
// Create and add custom footer, passing the data provider
|
|
1472
|
+
this.customFooter = factory(this.ui, theme, this.footerDataProvider);
|
|
1473
|
+
this.ui.addChild(this.customFooter);
|
|
1474
|
+
} else {
|
|
1475
|
+
// Restore built-in footer
|
|
1476
|
+
this.customFooter = undefined;
|
|
1477
|
+
this.ui.addChild(this.footer);
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
this.ui.requestRender();
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
/**
|
|
1484
|
+
* Set a custom header component, or restore the built-in header.
|
|
1485
|
+
*/
|
|
1486
|
+
private setExtensionHeader(factory: ((tui: TUI, thm: Theme) => Component & { dispose?(): void }) | undefined): void {
|
|
1487
|
+
// Header may not be initialized yet if called during early initialization
|
|
1488
|
+
if (!this.builtInHeader) {
|
|
1489
|
+
return;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
// Dispose existing custom header
|
|
1493
|
+
if (this.customHeader?.dispose) {
|
|
1494
|
+
this.customHeader.dispose();
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
// Find the index of the current header in the header container
|
|
1498
|
+
const currentHeader = this.customHeader || this.builtInHeader;
|
|
1499
|
+
const index = this.headerContainer.children.indexOf(currentHeader);
|
|
1500
|
+
|
|
1501
|
+
if (factory) {
|
|
1502
|
+
// Create and add custom header
|
|
1503
|
+
this.customHeader = factory(this.ui, theme);
|
|
1504
|
+
if (index !== -1) {
|
|
1505
|
+
this.headerContainer.children[index] = this.customHeader;
|
|
1506
|
+
} else {
|
|
1507
|
+
// If not found (e.g. builtInHeader was never added), add at the top
|
|
1508
|
+
this.headerContainer.children.unshift(this.customHeader);
|
|
1509
|
+
}
|
|
1510
|
+
} else {
|
|
1511
|
+
// Restore built-in header
|
|
1512
|
+
this.customHeader = undefined;
|
|
1513
|
+
if (index !== -1) {
|
|
1514
|
+
this.headerContainer.children[index] = this.builtInHeader;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
this.ui.requestRender();
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
private addExtensionTerminalInputListener(
|
|
1522
|
+
handler: (data: string) => { consume?: boolean; data?: string } | undefined,
|
|
1523
|
+
): () => void {
|
|
1524
|
+
const unsubscribe = this.ui.addInputListener(handler);
|
|
1525
|
+
this.extensionTerminalInputUnsubscribers.add(unsubscribe);
|
|
1526
|
+
return () => {
|
|
1527
|
+
unsubscribe();
|
|
1528
|
+
this.extensionTerminalInputUnsubscribers.delete(unsubscribe);
|
|
1529
|
+
};
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
private clearExtensionTerminalInputListeners(): void {
|
|
1533
|
+
for (const unsubscribe of this.extensionTerminalInputUnsubscribers) {
|
|
1534
|
+
unsubscribe();
|
|
1535
|
+
}
|
|
1536
|
+
this.extensionTerminalInputUnsubscribers.clear();
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
/**
|
|
1540
|
+
* Create the ExtensionUIContext for extensions.
|
|
1541
|
+
*/
|
|
1542
|
+
private createExtensionUIContext(): ExtensionUIContext {
|
|
1543
|
+
return {
|
|
1544
|
+
select: (title, options, opts) => this.showExtensionSelector(title, options, opts),
|
|
1545
|
+
confirm: (title, message, opts) => this.showExtensionConfirm(title, message, opts),
|
|
1546
|
+
input: (title, placeholder, opts) => this.showExtensionInput(title, placeholder, opts),
|
|
1547
|
+
notify: (message, type) => this.showExtensionNotify(message, type),
|
|
1548
|
+
onTerminalInput: (handler) => this.addExtensionTerminalInputListener(handler),
|
|
1549
|
+
setStatus: (key, text) => this.setExtensionStatus(key, text),
|
|
1550
|
+
setWorkingMessage: (message) => {
|
|
1551
|
+
if (this.loadingAnimation) {
|
|
1552
|
+
if (message) {
|
|
1553
|
+
this.loadingAnimation.setMessage(message);
|
|
1554
|
+
} else {
|
|
1555
|
+
this.loadingAnimation.setMessage(
|
|
1556
|
+
`${this.defaultWorkingMessage} (${keyText("app.interrupt")} to interrupt)`,
|
|
1557
|
+
);
|
|
1558
|
+
}
|
|
1559
|
+
} else {
|
|
1560
|
+
// Queue message for when loadingAnimation is created (handles agent_start race)
|
|
1561
|
+
this.pendingWorkingMessage = message;
|
|
1562
|
+
}
|
|
1563
|
+
},
|
|
1564
|
+
setHiddenThinkingLabel: (label) => this.setHiddenThinkingLabel(label),
|
|
1565
|
+
setWidget: (key, content, options) => this.setExtensionWidget(key, content, options),
|
|
1566
|
+
setFooter: (factory) => this.setExtensionFooter(factory),
|
|
1567
|
+
setHeader: (factory) => this.setExtensionHeader(factory),
|
|
1568
|
+
setTitle: (title) => this.ui.terminal.setTitle(title),
|
|
1569
|
+
custom: (factory, options) => this.showExtensionCustom(factory, options),
|
|
1570
|
+
pasteToEditor: (text) => this.editor.handleInput(`\x1b[200~${text}\x1b[201~`),
|
|
1571
|
+
setEditorText: (text) => this.editor.setText(text),
|
|
1572
|
+
getEditorText: () => this.editor.getExpandedText?.() ?? this.editor.getText(),
|
|
1573
|
+
editor: (title, prefill) => this.showExtensionEditor(title, prefill),
|
|
1574
|
+
setEditorComponent: (factory) => this.setCustomEditorComponent(factory),
|
|
1575
|
+
get theme() {
|
|
1576
|
+
return theme;
|
|
1577
|
+
},
|
|
1578
|
+
getAllThemes: () => getAvailableThemesWithPaths(),
|
|
1579
|
+
getTheme: (name) => getThemeByName(name),
|
|
1580
|
+
setTheme: (themeOrName) => {
|
|
1581
|
+
if (themeOrName instanceof Theme) {
|
|
1582
|
+
setThemeInstance(themeOrName);
|
|
1583
|
+
this.ui.requestRender();
|
|
1584
|
+
return { success: true };
|
|
1585
|
+
}
|
|
1586
|
+
const result = setTheme(themeOrName, true);
|
|
1587
|
+
if (result.success) {
|
|
1588
|
+
if (this.settingsManager.getTheme() !== themeOrName) {
|
|
1589
|
+
this.settingsManager.setTheme(themeOrName);
|
|
1590
|
+
}
|
|
1591
|
+
this.ui.requestRender();
|
|
1592
|
+
}
|
|
1593
|
+
return result;
|
|
1594
|
+
},
|
|
1595
|
+
getToolsExpanded: () => this.toolOutputExpanded,
|
|
1596
|
+
setToolsExpanded: (expanded) => this.setToolsExpanded(expanded),
|
|
1597
|
+
};
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
/**
|
|
1601
|
+
* Show a selector for extensions.
|
|
1602
|
+
*/
|
|
1603
|
+
private showExtensionSelector(
|
|
1604
|
+
title: string,
|
|
1605
|
+
options: string[],
|
|
1606
|
+
opts?: ExtensionUIDialogOptions,
|
|
1607
|
+
): Promise<string | undefined> {
|
|
1608
|
+
return new Promise((resolve) => {
|
|
1609
|
+
if (opts?.signal?.aborted) {
|
|
1610
|
+
resolve(undefined);
|
|
1611
|
+
return;
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
const onAbort = () => {
|
|
1615
|
+
this.hideExtensionSelector();
|
|
1616
|
+
resolve(undefined);
|
|
1617
|
+
};
|
|
1618
|
+
opts?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
1619
|
+
|
|
1620
|
+
this.extensionSelector = new ExtensionSelectorComponent(
|
|
1621
|
+
title,
|
|
1622
|
+
options,
|
|
1623
|
+
(option) => {
|
|
1624
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
1625
|
+
this.hideExtensionSelector();
|
|
1626
|
+
resolve(option);
|
|
1627
|
+
},
|
|
1628
|
+
() => {
|
|
1629
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
1630
|
+
this.hideExtensionSelector();
|
|
1631
|
+
resolve(undefined);
|
|
1632
|
+
},
|
|
1633
|
+
{ tui: this.ui, timeout: opts?.timeout },
|
|
1634
|
+
);
|
|
1635
|
+
|
|
1636
|
+
this.editorContainer.clear();
|
|
1637
|
+
this.editorContainer.addChild(this.extensionSelector);
|
|
1638
|
+
this.ui.setFocus(this.extensionSelector);
|
|
1639
|
+
this.ui.requestRender();
|
|
1640
|
+
});
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
/**
|
|
1644
|
+
* Hide the extension selector.
|
|
1645
|
+
*/
|
|
1646
|
+
private hideExtensionSelector(): void {
|
|
1647
|
+
this.extensionSelector?.dispose();
|
|
1648
|
+
this.editorContainer.clear();
|
|
1649
|
+
this.editorContainer.addChild(this.editor);
|
|
1650
|
+
this.extensionSelector = undefined;
|
|
1651
|
+
this.ui.setFocus(this.editor);
|
|
1652
|
+
this.ui.requestRender();
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
/**
|
|
1656
|
+
* Show a confirmation dialog for extensions.
|
|
1657
|
+
*/
|
|
1658
|
+
private async showExtensionConfirm(
|
|
1659
|
+
title: string,
|
|
1660
|
+
message: string,
|
|
1661
|
+
opts?: ExtensionUIDialogOptions,
|
|
1662
|
+
): Promise<boolean> {
|
|
1663
|
+
const result = await this.showExtensionSelector(`${title}\n${message}`, ["Yes", "No"], opts);
|
|
1664
|
+
return result === "Yes";
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
/**
|
|
1668
|
+
* Show a text input for extensions.
|
|
1669
|
+
*/
|
|
1670
|
+
private showExtensionInput(
|
|
1671
|
+
title: string,
|
|
1672
|
+
placeholder?: string,
|
|
1673
|
+
opts?: ExtensionUIDialogOptions,
|
|
1674
|
+
): Promise<string | undefined> {
|
|
1675
|
+
return new Promise((resolve) => {
|
|
1676
|
+
if (opts?.signal?.aborted) {
|
|
1677
|
+
resolve(undefined);
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
const onAbort = () => {
|
|
1682
|
+
this.hideExtensionInput();
|
|
1683
|
+
resolve(undefined);
|
|
1684
|
+
};
|
|
1685
|
+
opts?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
1686
|
+
|
|
1687
|
+
this.extensionInput = new ExtensionInputComponent(
|
|
1688
|
+
title,
|
|
1689
|
+
placeholder,
|
|
1690
|
+
(value) => {
|
|
1691
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
1692
|
+
this.hideExtensionInput();
|
|
1693
|
+
resolve(value);
|
|
1694
|
+
},
|
|
1695
|
+
() => {
|
|
1696
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
1697
|
+
this.hideExtensionInput();
|
|
1698
|
+
resolve(undefined);
|
|
1699
|
+
},
|
|
1700
|
+
{ tui: this.ui, timeout: opts?.timeout },
|
|
1701
|
+
);
|
|
1702
|
+
|
|
1703
|
+
this.editorContainer.clear();
|
|
1704
|
+
this.editorContainer.addChild(this.extensionInput);
|
|
1705
|
+
this.ui.setFocus(this.extensionInput);
|
|
1706
|
+
this.ui.requestRender();
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* Hide the extension input.
|
|
1712
|
+
*/
|
|
1713
|
+
private hideExtensionInput(): void {
|
|
1714
|
+
this.extensionInput?.dispose();
|
|
1715
|
+
this.editorContainer.clear();
|
|
1716
|
+
this.editorContainer.addChild(this.editor);
|
|
1717
|
+
this.extensionInput = undefined;
|
|
1718
|
+
this.ui.setFocus(this.editor);
|
|
1719
|
+
this.ui.requestRender();
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
/**
|
|
1723
|
+
* Show a multi-line editor for extensions (with Ctrl+G support).
|
|
1724
|
+
*/
|
|
1725
|
+
private showExtensionEditor(title: string, prefill?: string): Promise<string | undefined> {
|
|
1726
|
+
return new Promise((resolve) => {
|
|
1727
|
+
this.extensionEditor = new ExtensionEditorComponent(
|
|
1728
|
+
this.ui,
|
|
1729
|
+
this.keybindings,
|
|
1730
|
+
title,
|
|
1731
|
+
prefill,
|
|
1732
|
+
(value) => {
|
|
1733
|
+
this.hideExtensionEditor();
|
|
1734
|
+
resolve(value);
|
|
1735
|
+
},
|
|
1736
|
+
() => {
|
|
1737
|
+
this.hideExtensionEditor();
|
|
1738
|
+
resolve(undefined);
|
|
1739
|
+
},
|
|
1740
|
+
);
|
|
1741
|
+
|
|
1742
|
+
this.editorContainer.clear();
|
|
1743
|
+
this.editorContainer.addChild(this.extensionEditor);
|
|
1744
|
+
this.ui.setFocus(this.extensionEditor);
|
|
1745
|
+
this.ui.requestRender();
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
/**
|
|
1750
|
+
* Hide the extension editor.
|
|
1751
|
+
*/
|
|
1752
|
+
private hideExtensionEditor(): void {
|
|
1753
|
+
this.editorContainer.clear();
|
|
1754
|
+
this.editorContainer.addChild(this.editor);
|
|
1755
|
+
this.extensionEditor = undefined;
|
|
1756
|
+
this.ui.setFocus(this.editor);
|
|
1757
|
+
this.ui.requestRender();
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
/**
|
|
1761
|
+
* Set a custom editor component from an extension.
|
|
1762
|
+
* Pass undefined to restore the default editor.
|
|
1763
|
+
*/
|
|
1764
|
+
private setCustomEditorComponent(
|
|
1765
|
+
factory: ((tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) => EditorComponent) | undefined,
|
|
1766
|
+
): void {
|
|
1767
|
+
// Save text from current editor before switching
|
|
1768
|
+
const currentText = this.editor.getText();
|
|
1769
|
+
|
|
1770
|
+
this.editorContainer.clear();
|
|
1771
|
+
|
|
1772
|
+
if (factory) {
|
|
1773
|
+
// Create the custom editor with tui, theme, and keybindings
|
|
1774
|
+
const newEditor = factory(this.ui, getEditorTheme(), this.keybindings);
|
|
1775
|
+
|
|
1776
|
+
// Wire up callbacks from the default editor
|
|
1777
|
+
newEditor.onSubmit = this.defaultEditor.onSubmit;
|
|
1778
|
+
newEditor.onChange = this.defaultEditor.onChange;
|
|
1779
|
+
|
|
1780
|
+
// Copy text from previous editor
|
|
1781
|
+
newEditor.setText(currentText);
|
|
1782
|
+
|
|
1783
|
+
// Copy appearance settings if supported
|
|
1784
|
+
if (newEditor.borderColor !== undefined) {
|
|
1785
|
+
newEditor.borderColor = this.defaultEditor.borderColor;
|
|
1786
|
+
}
|
|
1787
|
+
if (newEditor.setPaddingX !== undefined) {
|
|
1788
|
+
newEditor.setPaddingX(this.defaultEditor.getPaddingX());
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
// Set autocomplete if supported
|
|
1792
|
+
if (newEditor.setAutocompleteProvider && this.autocompleteProvider) {
|
|
1793
|
+
newEditor.setAutocompleteProvider(this.autocompleteProvider);
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
// If extending CustomEditor, copy app-level handlers
|
|
1797
|
+
// Use duck typing since instanceof fails across jiti module boundaries
|
|
1798
|
+
const customEditor = newEditor as unknown as Record<string, unknown>;
|
|
1799
|
+
if ("actionHandlers" in customEditor && customEditor.actionHandlers instanceof Map) {
|
|
1800
|
+
if (!customEditor.onEscape) {
|
|
1801
|
+
customEditor.onEscape = () => this.defaultEditor.onEscape?.();
|
|
1802
|
+
}
|
|
1803
|
+
if (!customEditor.onCtrlD) {
|
|
1804
|
+
customEditor.onCtrlD = () => this.defaultEditor.onCtrlD?.();
|
|
1805
|
+
}
|
|
1806
|
+
if (!customEditor.onPasteImage) {
|
|
1807
|
+
customEditor.onPasteImage = () => this.defaultEditor.onPasteImage?.();
|
|
1808
|
+
}
|
|
1809
|
+
if (!customEditor.onExtensionShortcut) {
|
|
1810
|
+
customEditor.onExtensionShortcut = (data: string) => this.defaultEditor.onExtensionShortcut?.(data);
|
|
1811
|
+
}
|
|
1812
|
+
// Copy action handlers (clear, suspend, model switching, etc.)
|
|
1813
|
+
for (const [action, handler] of this.defaultEditor.actionHandlers) {
|
|
1814
|
+
(customEditor.actionHandlers as Map<string, () => void>).set(action, handler);
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
this.editor = newEditor;
|
|
1819
|
+
} else {
|
|
1820
|
+
// Restore default editor with text from custom editor
|
|
1821
|
+
this.defaultEditor.setText(currentText);
|
|
1822
|
+
this.editor = this.defaultEditor;
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
this.editorContainer.addChild(this.editor as Component);
|
|
1826
|
+
this.ui.setFocus(this.editor as Component);
|
|
1827
|
+
this.ui.requestRender();
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
/**
|
|
1831
|
+
* Show a notification for extensions.
|
|
1832
|
+
*/
|
|
1833
|
+
private showExtensionNotify(message: string, type?: "info" | "warning" | "error"): void {
|
|
1834
|
+
if (type === "error") {
|
|
1835
|
+
this.showError(message);
|
|
1836
|
+
} else if (type === "warning") {
|
|
1837
|
+
this.showWarning(message);
|
|
1838
|
+
} else {
|
|
1839
|
+
this.showStatus(message);
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
/** Show a custom component with keyboard focus. Overlay mode renders on top of existing content. */
|
|
1844
|
+
private async showExtensionCustom<T>(
|
|
1845
|
+
factory: (
|
|
1846
|
+
tui: TUI,
|
|
1847
|
+
theme: Theme,
|
|
1848
|
+
keybindings: KeybindingsManager,
|
|
1849
|
+
done: (result: T) => void,
|
|
1850
|
+
) => (Component & { dispose?(): void }) | Promise<Component & { dispose?(): void }>,
|
|
1851
|
+
options?: {
|
|
1852
|
+
overlay?: boolean;
|
|
1853
|
+
overlayOptions?: OverlayOptions | (() => OverlayOptions);
|
|
1854
|
+
onHandle?: (handle: OverlayHandle) => void;
|
|
1855
|
+
},
|
|
1856
|
+
): Promise<T> {
|
|
1857
|
+
const savedText = this.editor.getText();
|
|
1858
|
+
const isOverlay = options?.overlay ?? false;
|
|
1859
|
+
|
|
1860
|
+
const restoreEditor = () => {
|
|
1861
|
+
this.editorContainer.clear();
|
|
1862
|
+
this.editorContainer.addChild(this.editor);
|
|
1863
|
+
this.editor.setText(savedText);
|
|
1864
|
+
this.ui.setFocus(this.editor);
|
|
1865
|
+
this.ui.requestRender();
|
|
1866
|
+
};
|
|
1867
|
+
|
|
1868
|
+
return new Promise((resolve, reject) => {
|
|
1869
|
+
let component: Component & { dispose?(): void };
|
|
1870
|
+
let closed = false;
|
|
1871
|
+
|
|
1872
|
+
const close = (result: T) => {
|
|
1873
|
+
if (closed) return;
|
|
1874
|
+
closed = true;
|
|
1875
|
+
if (isOverlay) this.ui.hideOverlay();
|
|
1876
|
+
else restoreEditor();
|
|
1877
|
+
// Note: both branches above already call requestRender
|
|
1878
|
+
resolve(result);
|
|
1879
|
+
try {
|
|
1880
|
+
component?.dispose?.();
|
|
1881
|
+
} catch {
|
|
1882
|
+
/* ignore dispose errors */
|
|
1883
|
+
}
|
|
1884
|
+
};
|
|
1885
|
+
|
|
1886
|
+
Promise.resolve(factory(this.ui, theme, this.keybindings, close))
|
|
1887
|
+
.then((c) => {
|
|
1888
|
+
if (closed) return;
|
|
1889
|
+
component = c;
|
|
1890
|
+
if (isOverlay) {
|
|
1891
|
+
// Resolve overlay options - can be static or dynamic function
|
|
1892
|
+
const resolveOptions = (): OverlayOptions | undefined => {
|
|
1893
|
+
if (options?.overlayOptions) {
|
|
1894
|
+
const opts =
|
|
1895
|
+
typeof options.overlayOptions === "function"
|
|
1896
|
+
? options.overlayOptions()
|
|
1897
|
+
: options.overlayOptions;
|
|
1898
|
+
return opts;
|
|
1899
|
+
}
|
|
1900
|
+
// Fallback: use component's width property if available
|
|
1901
|
+
const w = (component as { width?: number }).width;
|
|
1902
|
+
return w ? { width: w } : undefined;
|
|
1903
|
+
};
|
|
1904
|
+
const handle = this.ui.showOverlay(component, resolveOptions());
|
|
1905
|
+
// Expose handle to caller for visibility control
|
|
1906
|
+
options?.onHandle?.(handle);
|
|
1907
|
+
} else {
|
|
1908
|
+
this.editorContainer.clear();
|
|
1909
|
+
this.editorContainer.addChild(component);
|
|
1910
|
+
this.ui.setFocus(component);
|
|
1911
|
+
this.ui.requestRender();
|
|
1912
|
+
}
|
|
1913
|
+
})
|
|
1914
|
+
.catch((err) => {
|
|
1915
|
+
if (closed) return;
|
|
1916
|
+
if (!isOverlay) restoreEditor();
|
|
1917
|
+
reject(err);
|
|
1918
|
+
});
|
|
1919
|
+
});
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
/**
|
|
1923
|
+
* Show an extension error in the UI.
|
|
1924
|
+
*/
|
|
1925
|
+
private showExtensionError(extensionPath: string, error: string, stack?: string): void {
|
|
1926
|
+
const errorMsg = `Extension "${extensionPath}" error: ${error}`;
|
|
1927
|
+
const errorText = new Text(theme.fg("error", errorMsg), 1, 0);
|
|
1928
|
+
this.chatContainer.addChild(errorText);
|
|
1929
|
+
if (stack) {
|
|
1930
|
+
// Show stack trace in dim color, indented
|
|
1931
|
+
const stackLines = stack
|
|
1932
|
+
.split("\n")
|
|
1933
|
+
.slice(1) // Skip first line (duplicates error message)
|
|
1934
|
+
.map((line) => theme.fg("dim", ` ${line.trim()}`))
|
|
1935
|
+
.join("\n");
|
|
1936
|
+
if (stackLines) {
|
|
1937
|
+
this.chatContainer.addChild(new Text(stackLines, 1, 0));
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
this.ui.requestRender();
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
// =========================================================================
|
|
1944
|
+
// Key Handlers
|
|
1945
|
+
// =========================================================================
|
|
1946
|
+
|
|
1947
|
+
private setupKeyHandlers(): void {
|
|
1948
|
+
// Set up handlers on defaultEditor - they use this.editor for text access
|
|
1949
|
+
// so they work correctly regardless of which editor is active
|
|
1950
|
+
this.defaultEditor.onEscape = () => {
|
|
1951
|
+
if (this.loadingAnimation) {
|
|
1952
|
+
this.restoreQueuedMessagesToEditor({ abort: true });
|
|
1953
|
+
} else if (this.session.isBashRunning) {
|
|
1954
|
+
this.session.abortBash();
|
|
1955
|
+
} else if (this.isBashMode) {
|
|
1956
|
+
this.editor.setText("");
|
|
1957
|
+
this.isBashMode = false;
|
|
1958
|
+
this.updateEditorBorderColor();
|
|
1959
|
+
} else if (!this.editor.getText().trim()) {
|
|
1960
|
+
// Double-escape with empty editor triggers /tree, /fork, or nothing based on setting
|
|
1961
|
+
const action = this.settingsManager.getDoubleEscapeAction();
|
|
1962
|
+
if (action !== "none") {
|
|
1963
|
+
const now = Date.now();
|
|
1964
|
+
if (now - this.lastEscapeTime < 500) {
|
|
1965
|
+
if (action === "tree") {
|
|
1966
|
+
this.showTreeSelector();
|
|
1967
|
+
} else {
|
|
1968
|
+
this.showUserMessageSelector();
|
|
1969
|
+
}
|
|
1970
|
+
this.lastEscapeTime = 0;
|
|
1971
|
+
} else {
|
|
1972
|
+
this.lastEscapeTime = now;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
};
|
|
1977
|
+
|
|
1978
|
+
// Register app action handlers
|
|
1979
|
+
this.defaultEditor.onAction("app.clear", () => this.handleCtrlC());
|
|
1980
|
+
this.defaultEditor.onCtrlD = () => this.handleCtrlD();
|
|
1981
|
+
this.defaultEditor.onAction("app.suspend", () => this.handleCtrlZ());
|
|
1982
|
+
this.defaultEditor.onAction("app.thinking.cycle", () => this.cycleThinkingLevel());
|
|
1983
|
+
this.defaultEditor.onAction("app.model.cycleForward", () => this.cycleModel("forward"));
|
|
1984
|
+
this.defaultEditor.onAction("app.model.cycleBackward", () => this.cycleModel("backward"));
|
|
1985
|
+
|
|
1986
|
+
// Global debug handler on TUI (works regardless of focus)
|
|
1987
|
+
this.ui.onDebug = () => this.handleDebugCommand();
|
|
1988
|
+
this.defaultEditor.onAction("app.model.select", () => this.showModelSelector());
|
|
1989
|
+
this.defaultEditor.onAction("app.tools.expand", () => this.toggleToolOutputExpansion());
|
|
1990
|
+
this.defaultEditor.onAction("app.thinking.toggle", () => this.toggleThinkingBlockVisibility());
|
|
1991
|
+
this.defaultEditor.onAction("app.editor.external", () => this.openExternalEditor());
|
|
1992
|
+
this.defaultEditor.onAction("app.message.followUp", () => this.handleFollowUp());
|
|
1993
|
+
this.defaultEditor.onAction("app.message.dequeue", () => this.handleDequeue());
|
|
1994
|
+
this.defaultEditor.onAction("app.session.new", () => this.handleClearCommand());
|
|
1995
|
+
this.defaultEditor.onAction("app.session.tree", () => this.showTreeSelector());
|
|
1996
|
+
this.defaultEditor.onAction("app.session.fork", () => this.showUserMessageSelector());
|
|
1997
|
+
this.defaultEditor.onAction("app.session.resume", () => this.showSessionSelector());
|
|
1998
|
+
|
|
1999
|
+
this.defaultEditor.onChange = (text: string) => {
|
|
2000
|
+
const wasBashMode = this.isBashMode;
|
|
2001
|
+
this.isBashMode = text.trimStart().startsWith("!");
|
|
2002
|
+
if (wasBashMode !== this.isBashMode) {
|
|
2003
|
+
this.updateEditorBorderColor();
|
|
2004
|
+
}
|
|
2005
|
+
};
|
|
2006
|
+
|
|
2007
|
+
// Handle clipboard image paste (triggered on Ctrl+V)
|
|
2008
|
+
this.defaultEditor.onPasteImage = () => {
|
|
2009
|
+
this.handleClipboardImagePaste();
|
|
2010
|
+
};
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
private async handleClipboardImagePaste(): Promise<void> {
|
|
2014
|
+
try {
|
|
2015
|
+
const image = await readClipboardImage();
|
|
2016
|
+
if (!image) {
|
|
2017
|
+
return;
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
// Write to temp file
|
|
2021
|
+
const tmpDir = os.tmpdir();
|
|
2022
|
+
const ext = extensionForImageMimeType(image.mimeType) ?? "png";
|
|
2023
|
+
const fileName = `pi-clipboard-${crypto.randomUUID()}.${ext}`;
|
|
2024
|
+
const filePath = path.join(tmpDir, fileName);
|
|
2025
|
+
fs.writeFileSync(filePath, Buffer.from(image.bytes));
|
|
2026
|
+
|
|
2027
|
+
// Insert file path directly
|
|
2028
|
+
this.editor.insertTextAtCursor?.(filePath);
|
|
2029
|
+
this.ui.requestRender();
|
|
2030
|
+
} catch {
|
|
2031
|
+
// Silently ignore clipboard errors (may not have permission, etc.)
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
private setupEditorSubmitHandler(): void {
|
|
2036
|
+
this.defaultEditor.onSubmit = async (text: string) => {
|
|
2037
|
+
text = text.trim();
|
|
2038
|
+
if (!text) return;
|
|
2039
|
+
|
|
2040
|
+
// Handle commands
|
|
2041
|
+
if (text === "/settings") {
|
|
2042
|
+
this.showSettingsSelector();
|
|
2043
|
+
this.editor.setText("");
|
|
2044
|
+
return;
|
|
2045
|
+
}
|
|
2046
|
+
if (text === "/scoped-models") {
|
|
2047
|
+
this.editor.setText("");
|
|
2048
|
+
await this.showModelsSelector();
|
|
2049
|
+
return;
|
|
2050
|
+
}
|
|
2051
|
+
if (text === "/model" || text.startsWith("/model ")) {
|
|
2052
|
+
const searchTerm = text.startsWith("/model ") ? text.slice(7).trim() : undefined;
|
|
2053
|
+
this.editor.setText("");
|
|
2054
|
+
await this.handleModelCommand(searchTerm);
|
|
2055
|
+
return;
|
|
2056
|
+
}
|
|
2057
|
+
if (text.startsWith("/export")) {
|
|
2058
|
+
await this.handleExportCommand(text);
|
|
2059
|
+
this.editor.setText("");
|
|
2060
|
+
return;
|
|
2061
|
+
}
|
|
2062
|
+
if (text.startsWith("/import")) {
|
|
2063
|
+
await this.handleImportCommand(text);
|
|
2064
|
+
this.editor.setText("");
|
|
2065
|
+
return;
|
|
2066
|
+
}
|
|
2067
|
+
if (text === "/copy") {
|
|
2068
|
+
await this.handleCopyCommand();
|
|
2069
|
+
this.editor.setText("");
|
|
2070
|
+
return;
|
|
2071
|
+
}
|
|
2072
|
+
if (text === "/name" || text.startsWith("/name ")) {
|
|
2073
|
+
this.handleNameCommand(text);
|
|
2074
|
+
this.editor.setText("");
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
if (text === "/session") {
|
|
2078
|
+
this.handleSessionCommand();
|
|
2079
|
+
this.editor.setText("");
|
|
2080
|
+
return;
|
|
2081
|
+
}
|
|
2082
|
+
if (text === "/changelog") {
|
|
2083
|
+
this.handleChangelogCommand();
|
|
2084
|
+
this.editor.setText("");
|
|
2085
|
+
return;
|
|
2086
|
+
}
|
|
2087
|
+
if (text === "/hotkeys") {
|
|
2088
|
+
this.handleHotkeysCommand();
|
|
2089
|
+
this.editor.setText("");
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
if (text === "/fork") {
|
|
2093
|
+
this.showUserMessageSelector();
|
|
2094
|
+
this.editor.setText("");
|
|
2095
|
+
return;
|
|
2096
|
+
}
|
|
2097
|
+
if (text === "/tree") {
|
|
2098
|
+
this.showTreeSelector();
|
|
2099
|
+
this.editor.setText("");
|
|
2100
|
+
return;
|
|
2101
|
+
}
|
|
2102
|
+
if (text === "/login") {
|
|
2103
|
+
this.showOAuthSelector("login");
|
|
2104
|
+
this.editor.setText("");
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
if (text === "/logout") {
|
|
2108
|
+
this.showOAuthSelector("logout");
|
|
2109
|
+
this.editor.setText("");
|
|
2110
|
+
return;
|
|
2111
|
+
}
|
|
2112
|
+
if (text === "/new") {
|
|
2113
|
+
this.editor.setText("");
|
|
2114
|
+
await this.handleClearCommand();
|
|
2115
|
+
return;
|
|
2116
|
+
}
|
|
2117
|
+
if (text === "/compact" || text.startsWith("/compact ")) {
|
|
2118
|
+
const customInstructions = text.startsWith("/compact ") ? text.slice(9).trim() : undefined;
|
|
2119
|
+
this.editor.setText("");
|
|
2120
|
+
await this.handleCompactCommand(customInstructions);
|
|
2121
|
+
return;
|
|
2122
|
+
}
|
|
2123
|
+
if (text === "/reload") {
|
|
2124
|
+
this.editor.setText("");
|
|
2125
|
+
await this.handleReloadCommand();
|
|
2126
|
+
return;
|
|
2127
|
+
}
|
|
2128
|
+
if (text === "/debug") {
|
|
2129
|
+
this.handleDebugCommand();
|
|
2130
|
+
this.editor.setText("");
|
|
2131
|
+
return;
|
|
2132
|
+
}
|
|
2133
|
+
if (text === "/arminsayshi") {
|
|
2134
|
+
this.handleArminSaysHi();
|
|
2135
|
+
this.editor.setText("");
|
|
2136
|
+
return;
|
|
2137
|
+
}
|
|
2138
|
+
if (text === "/resume") {
|
|
2139
|
+
this.showSessionSelector();
|
|
2140
|
+
this.editor.setText("");
|
|
2141
|
+
return;
|
|
2142
|
+
}
|
|
2143
|
+
if (text === "/quit") {
|
|
2144
|
+
this.editor.setText("");
|
|
2145
|
+
await this.shutdown();
|
|
2146
|
+
return;
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
// Handle bash command (! for normal, !! for excluded from context)
|
|
2150
|
+
if (text.startsWith("!")) {
|
|
2151
|
+
const isExcluded = text.startsWith("!!");
|
|
2152
|
+
const command = isExcluded ? text.slice(2).trim() : text.slice(1).trim();
|
|
2153
|
+
if (command) {
|
|
2154
|
+
if (this.session.isBashRunning) {
|
|
2155
|
+
this.showWarning("A bash command is already running. Press Esc to cancel it first.");
|
|
2156
|
+
this.editor.setText(text);
|
|
2157
|
+
return;
|
|
2158
|
+
}
|
|
2159
|
+
this.editor.addToHistory?.(text);
|
|
2160
|
+
await this.handleBashCommand(command, isExcluded);
|
|
2161
|
+
this.isBashMode = false;
|
|
2162
|
+
this.updateEditorBorderColor();
|
|
2163
|
+
return;
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2167
|
+
// Queue input during compaction (extension commands execute immediately)
|
|
2168
|
+
if (this.session.isCompacting) {
|
|
2169
|
+
if (this.isExtensionCommand(text)) {
|
|
2170
|
+
this.editor.addToHistory?.(text);
|
|
2171
|
+
this.editor.setText("");
|
|
2172
|
+
await this.session.prompt(text);
|
|
2173
|
+
} else {
|
|
2174
|
+
this.queueCompactionMessage(text, "steer");
|
|
2175
|
+
}
|
|
2176
|
+
return;
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
// If streaming, use prompt() with steer behavior
|
|
2180
|
+
// This handles extension commands (execute immediately), prompt template expansion, and queueing
|
|
2181
|
+
if (this.session.isStreaming) {
|
|
2182
|
+
this.editor.addToHistory?.(text);
|
|
2183
|
+
this.editor.setText("");
|
|
2184
|
+
await this.session.prompt(text, { streamingBehavior: "steer" });
|
|
2185
|
+
this.updatePendingMessagesDisplay();
|
|
2186
|
+
this.ui.requestRender();
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
// Normal message submission
|
|
2191
|
+
// First, move any pending bash components to chat
|
|
2192
|
+
this.flushPendingBashComponents();
|
|
2193
|
+
|
|
2194
|
+
if (this.onInputCallback) {
|
|
2195
|
+
this.onInputCallback(text);
|
|
2196
|
+
}
|
|
2197
|
+
this.editor.addToHistory?.(text);
|
|
2198
|
+
};
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
private subscribeToAgent(): void {
|
|
2202
|
+
this.unsubscribe = this.session.subscribe(async (event) => {
|
|
2203
|
+
await this.handleEvent(event);
|
|
2204
|
+
});
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
private async handleEvent(event: AgentSessionEvent): Promise<void> {
|
|
2208
|
+
if (!this.isInitialized) {
|
|
2209
|
+
await this.init();
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
this.footer.invalidate();
|
|
2213
|
+
|
|
2214
|
+
switch (event.type) {
|
|
2215
|
+
case "agent_start":
|
|
2216
|
+
// Restore main escape handler if retry handler is still active
|
|
2217
|
+
// (retry success event fires later, but we need main handler now)
|
|
2218
|
+
if (this.retryEscapeHandler) {
|
|
2219
|
+
this.defaultEditor.onEscape = this.retryEscapeHandler;
|
|
2220
|
+
this.retryEscapeHandler = undefined;
|
|
2221
|
+
}
|
|
2222
|
+
if (this.retryLoader) {
|
|
2223
|
+
this.retryLoader.stop();
|
|
2224
|
+
this.retryLoader = undefined;
|
|
2225
|
+
}
|
|
2226
|
+
if (this.loadingAnimation) {
|
|
2227
|
+
this.loadingAnimation.stop();
|
|
2228
|
+
}
|
|
2229
|
+
this.statusContainer.clear();
|
|
2230
|
+
this.loadingAnimation = new Loader(
|
|
2231
|
+
this.ui,
|
|
2232
|
+
(spinner) => theme.fg("accent", spinner),
|
|
2233
|
+
(text) => theme.fg("muted", text),
|
|
2234
|
+
this.defaultWorkingMessage,
|
|
2235
|
+
);
|
|
2236
|
+
this.statusContainer.addChild(this.loadingAnimation);
|
|
2237
|
+
// Apply any pending working message queued before loader existed
|
|
2238
|
+
if (this.pendingWorkingMessage !== undefined) {
|
|
2239
|
+
if (this.pendingWorkingMessage) {
|
|
2240
|
+
this.loadingAnimation.setMessage(this.pendingWorkingMessage);
|
|
2241
|
+
}
|
|
2242
|
+
this.pendingWorkingMessage = undefined;
|
|
2243
|
+
}
|
|
2244
|
+
this.ui.requestRender();
|
|
2245
|
+
break;
|
|
2246
|
+
|
|
2247
|
+
case "queue_update":
|
|
2248
|
+
this.updatePendingMessagesDisplay();
|
|
2249
|
+
this.ui.requestRender();
|
|
2250
|
+
break;
|
|
2251
|
+
|
|
2252
|
+
case "message_start":
|
|
2253
|
+
if (event.message.role === "custom") {
|
|
2254
|
+
this.addMessageToChat(event.message);
|
|
2255
|
+
this.ui.requestRender();
|
|
2256
|
+
} else if (event.message.role === "user") {
|
|
2257
|
+
this.addMessageToChat(event.message);
|
|
2258
|
+
this.updatePendingMessagesDisplay();
|
|
2259
|
+
this.ui.requestRender();
|
|
2260
|
+
} else if (event.message.role === "assistant") {
|
|
2261
|
+
this.streamingComponent = new AssistantMessageComponent(
|
|
2262
|
+
undefined,
|
|
2263
|
+
this.hideThinkingBlock,
|
|
2264
|
+
this.getMarkdownThemeWithSettings(),
|
|
2265
|
+
this.hiddenThinkingLabel,
|
|
2266
|
+
);
|
|
2267
|
+
this.streamingMessage = event.message;
|
|
2268
|
+
this.chatContainer.addChild(this.streamingComponent);
|
|
2269
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
2270
|
+
this.ui.requestRender();
|
|
2271
|
+
}
|
|
2272
|
+
break;
|
|
2273
|
+
|
|
2274
|
+
case "message_update":
|
|
2275
|
+
if (this.streamingComponent && event.message.role === "assistant") {
|
|
2276
|
+
this.streamingMessage = event.message;
|
|
2277
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
2278
|
+
|
|
2279
|
+
for (const content of this.streamingMessage.content) {
|
|
2280
|
+
if (content.type === "toolCall") {
|
|
2281
|
+
if (!this.pendingTools.has(content.id)) {
|
|
2282
|
+
const component = new ToolExecutionComponent(
|
|
2283
|
+
content.name,
|
|
2284
|
+
content.id,
|
|
2285
|
+
content.arguments,
|
|
2286
|
+
{
|
|
2287
|
+
showImages: this.settingsManager.getShowImages(),
|
|
2288
|
+
},
|
|
2289
|
+
this.getRegisteredToolDefinition(content.name),
|
|
2290
|
+
this.ui,
|
|
2291
|
+
);
|
|
2292
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
2293
|
+
this.chatContainer.addChild(component);
|
|
2294
|
+
this.pendingTools.set(content.id, component);
|
|
2295
|
+
} else {
|
|
2296
|
+
const component = this.pendingTools.get(content.id);
|
|
2297
|
+
if (component) {
|
|
2298
|
+
component.updateArgs(content.arguments);
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
this.ui.requestRender();
|
|
2304
|
+
}
|
|
2305
|
+
break;
|
|
2306
|
+
|
|
2307
|
+
case "message_end":
|
|
2308
|
+
if (event.message.role === "user") break;
|
|
2309
|
+
if (this.streamingComponent && event.message.role === "assistant") {
|
|
2310
|
+
this.streamingMessage = event.message;
|
|
2311
|
+
let errorMessage: string | undefined;
|
|
2312
|
+
if (this.streamingMessage.stopReason === "aborted") {
|
|
2313
|
+
const retryAttempt = this.session.retryAttempt;
|
|
2314
|
+
errorMessage =
|
|
2315
|
+
retryAttempt > 0
|
|
2316
|
+
? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
|
|
2317
|
+
: "Operation aborted";
|
|
2318
|
+
this.streamingMessage.errorMessage = errorMessage;
|
|
2319
|
+
}
|
|
2320
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
2321
|
+
|
|
2322
|
+
if (this.streamingMessage.stopReason === "aborted" || this.streamingMessage.stopReason === "error") {
|
|
2323
|
+
if (!errorMessage) {
|
|
2324
|
+
errorMessage = this.streamingMessage.errorMessage || "Error";
|
|
2325
|
+
}
|
|
2326
|
+
for (const [, component] of this.pendingTools.entries()) {
|
|
2327
|
+
component.updateResult({
|
|
2328
|
+
content: [{ type: "text", text: errorMessage }],
|
|
2329
|
+
isError: true,
|
|
2330
|
+
});
|
|
2331
|
+
}
|
|
2332
|
+
this.pendingTools.clear();
|
|
2333
|
+
} else {
|
|
2334
|
+
// Args are now complete - trigger diff computation for edit tools
|
|
2335
|
+
for (const [, component] of this.pendingTools.entries()) {
|
|
2336
|
+
component.setArgsComplete();
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
this.streamingComponent = undefined;
|
|
2340
|
+
this.streamingMessage = undefined;
|
|
2341
|
+
this.footer.invalidate();
|
|
2342
|
+
}
|
|
2343
|
+
this.ui.requestRender();
|
|
2344
|
+
break;
|
|
2345
|
+
|
|
2346
|
+
case "tool_execution_start": {
|
|
2347
|
+
let component = this.pendingTools.get(event.toolCallId);
|
|
2348
|
+
if (!component) {
|
|
2349
|
+
component = new ToolExecutionComponent(
|
|
2350
|
+
event.toolName,
|
|
2351
|
+
event.toolCallId,
|
|
2352
|
+
event.args,
|
|
2353
|
+
{
|
|
2354
|
+
showImages: this.settingsManager.getShowImages(),
|
|
2355
|
+
},
|
|
2356
|
+
this.getRegisteredToolDefinition(event.toolName),
|
|
2357
|
+
this.ui,
|
|
2358
|
+
);
|
|
2359
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
2360
|
+
this.chatContainer.addChild(component);
|
|
2361
|
+
this.pendingTools.set(event.toolCallId, component);
|
|
2362
|
+
}
|
|
2363
|
+
component.markExecutionStarted();
|
|
2364
|
+
this.ui.requestRender();
|
|
2365
|
+
break;
|
|
2366
|
+
}
|
|
2367
|
+
|
|
2368
|
+
case "tool_execution_update": {
|
|
2369
|
+
const component = this.pendingTools.get(event.toolCallId);
|
|
2370
|
+
if (component) {
|
|
2371
|
+
component.updateResult({ ...event.partialResult, isError: false }, true);
|
|
2372
|
+
this.ui.requestRender();
|
|
2373
|
+
}
|
|
2374
|
+
break;
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
case "tool_execution_end": {
|
|
2378
|
+
const component = this.pendingTools.get(event.toolCallId);
|
|
2379
|
+
if (component) {
|
|
2380
|
+
component.updateResult({ ...event.result, isError: event.isError });
|
|
2381
|
+
this.pendingTools.delete(event.toolCallId);
|
|
2382
|
+
this.ui.requestRender();
|
|
2383
|
+
}
|
|
2384
|
+
break;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
case "agent_end":
|
|
2388
|
+
if (this.loadingAnimation) {
|
|
2389
|
+
this.loadingAnimation.stop();
|
|
2390
|
+
this.loadingAnimation = undefined;
|
|
2391
|
+
this.statusContainer.clear();
|
|
2392
|
+
}
|
|
2393
|
+
if (this.streamingComponent) {
|
|
2394
|
+
this.chatContainer.removeChild(this.streamingComponent);
|
|
2395
|
+
this.streamingComponent = undefined;
|
|
2396
|
+
this.streamingMessage = undefined;
|
|
2397
|
+
}
|
|
2398
|
+
this.pendingTools.clear();
|
|
2399
|
+
|
|
2400
|
+
await this.checkShutdownRequested();
|
|
2401
|
+
|
|
2402
|
+
this.ui.requestRender();
|
|
2403
|
+
break;
|
|
2404
|
+
|
|
2405
|
+
case "compaction_start": {
|
|
2406
|
+
// Keep editor active; submissions are queued during compaction.
|
|
2407
|
+
this.autoCompactionEscapeHandler = this.defaultEditor.onEscape;
|
|
2408
|
+
this.defaultEditor.onEscape = () => {
|
|
2409
|
+
this.session.abortCompaction();
|
|
2410
|
+
};
|
|
2411
|
+
this.statusContainer.clear();
|
|
2412
|
+
const cancelHint = `(${keyText("app.interrupt")} to cancel)`;
|
|
2413
|
+
const label =
|
|
2414
|
+
event.reason === "manual"
|
|
2415
|
+
? `Compacting context... ${cancelHint}`
|
|
2416
|
+
: `${event.reason === "overflow" ? "Context overflow detected, " : ""}Auto-compacting... ${cancelHint}`;
|
|
2417
|
+
this.autoCompactionLoader = new Loader(
|
|
2418
|
+
this.ui,
|
|
2419
|
+
(spinner) => theme.fg("accent", spinner),
|
|
2420
|
+
(text) => theme.fg("muted", text),
|
|
2421
|
+
label,
|
|
2422
|
+
);
|
|
2423
|
+
this.statusContainer.addChild(this.autoCompactionLoader);
|
|
2424
|
+
this.ui.requestRender();
|
|
2425
|
+
break;
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
case "compaction_end": {
|
|
2429
|
+
if (this.autoCompactionEscapeHandler) {
|
|
2430
|
+
this.defaultEditor.onEscape = this.autoCompactionEscapeHandler;
|
|
2431
|
+
this.autoCompactionEscapeHandler = undefined;
|
|
2432
|
+
}
|
|
2433
|
+
if (this.autoCompactionLoader) {
|
|
2434
|
+
this.autoCompactionLoader.stop();
|
|
2435
|
+
this.autoCompactionLoader = undefined;
|
|
2436
|
+
this.statusContainer.clear();
|
|
2437
|
+
}
|
|
2438
|
+
if (event.aborted) {
|
|
2439
|
+
if (event.reason === "manual") {
|
|
2440
|
+
this.showError("Compaction cancelled");
|
|
2441
|
+
} else {
|
|
2442
|
+
this.showStatus("Auto-compaction cancelled");
|
|
2443
|
+
}
|
|
2444
|
+
} else if (event.result) {
|
|
2445
|
+
this.chatContainer.clear();
|
|
2446
|
+
this.rebuildChatFromMessages();
|
|
2447
|
+
this.addMessageToChat(
|
|
2448
|
+
createCompactionSummaryMessage(
|
|
2449
|
+
event.result.summary,
|
|
2450
|
+
event.result.tokensBefore,
|
|
2451
|
+
new Date().toISOString(),
|
|
2452
|
+
),
|
|
2453
|
+
);
|
|
2454
|
+
this.footer.invalidate();
|
|
2455
|
+
} else if (event.errorMessage) {
|
|
2456
|
+
if (event.reason === "manual") {
|
|
2457
|
+
this.showError(event.errorMessage);
|
|
2458
|
+
} else {
|
|
2459
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2460
|
+
this.chatContainer.addChild(new Text(theme.fg("error", event.errorMessage), 1, 0));
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
void this.flushCompactionQueue({ willRetry: event.willRetry });
|
|
2464
|
+
this.ui.requestRender();
|
|
2465
|
+
break;
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
case "auto_retry_start": {
|
|
2469
|
+
// Set up escape to abort retry
|
|
2470
|
+
this.retryEscapeHandler = this.defaultEditor.onEscape;
|
|
2471
|
+
this.defaultEditor.onEscape = () => {
|
|
2472
|
+
this.session.abortRetry();
|
|
2473
|
+
};
|
|
2474
|
+
// Show retry indicator
|
|
2475
|
+
this.statusContainer.clear();
|
|
2476
|
+
const delaySeconds = Math.round(event.delayMs / 1000);
|
|
2477
|
+
this.retryLoader = new Loader(
|
|
2478
|
+
this.ui,
|
|
2479
|
+
(spinner) => theme.fg("warning", spinner),
|
|
2480
|
+
(text) => theme.fg("muted", text),
|
|
2481
|
+
`Retrying (${event.attempt}/${event.maxAttempts}) in ${delaySeconds}s... (${keyText("app.interrupt")} to cancel)`,
|
|
2482
|
+
);
|
|
2483
|
+
this.statusContainer.addChild(this.retryLoader);
|
|
2484
|
+
this.ui.requestRender();
|
|
2485
|
+
break;
|
|
2486
|
+
}
|
|
2487
|
+
|
|
2488
|
+
case "auto_retry_end": {
|
|
2489
|
+
// Restore escape handler
|
|
2490
|
+
if (this.retryEscapeHandler) {
|
|
2491
|
+
this.defaultEditor.onEscape = this.retryEscapeHandler;
|
|
2492
|
+
this.retryEscapeHandler = undefined;
|
|
2493
|
+
}
|
|
2494
|
+
// Stop loader
|
|
2495
|
+
if (this.retryLoader) {
|
|
2496
|
+
this.retryLoader.stop();
|
|
2497
|
+
this.retryLoader = undefined;
|
|
2498
|
+
this.statusContainer.clear();
|
|
2499
|
+
}
|
|
2500
|
+
// Show error only on final failure (success shows normal response)
|
|
2501
|
+
if (!event.success) {
|
|
2502
|
+
this.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || "Unknown error"}`);
|
|
2503
|
+
}
|
|
2504
|
+
this.ui.requestRender();
|
|
2505
|
+
break;
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2509
|
+
|
|
2510
|
+
/** Extract text content from a user message */
|
|
2511
|
+
private getUserMessageText(message: Message): string {
|
|
2512
|
+
if (message.role !== "user") return "";
|
|
2513
|
+
const textBlocks =
|
|
2514
|
+
typeof message.content === "string"
|
|
2515
|
+
? [{ type: "text", text: message.content }]
|
|
2516
|
+
: message.content.filter((c: { type: string }) => c.type === "text");
|
|
2517
|
+
return textBlocks.map((c) => (c as { text: string }).text).join("");
|
|
2518
|
+
}
|
|
2519
|
+
|
|
2520
|
+
/**
|
|
2521
|
+
* Show a status message in the chat.
|
|
2522
|
+
*
|
|
2523
|
+
* If multiple status messages are emitted back-to-back (without anything else being added to the chat),
|
|
2524
|
+
* we update the previous status line instead of appending new ones to avoid log spam.
|
|
2525
|
+
*/
|
|
2526
|
+
private showStatus(message: string): void {
|
|
2527
|
+
const children = this.chatContainer.children;
|
|
2528
|
+
const last = children.length > 0 ? children[children.length - 1] : undefined;
|
|
2529
|
+
const secondLast = children.length > 1 ? children[children.length - 2] : undefined;
|
|
2530
|
+
|
|
2531
|
+
if (last && secondLast && last === this.lastStatusText && secondLast === this.lastStatusSpacer) {
|
|
2532
|
+
this.lastStatusText.setText(theme.fg("dim", message));
|
|
2533
|
+
this.ui.requestRender();
|
|
2534
|
+
return;
|
|
2535
|
+
}
|
|
2536
|
+
|
|
2537
|
+
const spacer = new Spacer(1);
|
|
2538
|
+
const text = new Text(theme.fg("dim", message), 1, 0);
|
|
2539
|
+
this.chatContainer.addChild(spacer);
|
|
2540
|
+
this.chatContainer.addChild(text);
|
|
2541
|
+
this.lastStatusSpacer = spacer;
|
|
2542
|
+
this.lastStatusText = text;
|
|
2543
|
+
this.ui.requestRender();
|
|
2544
|
+
}
|
|
2545
|
+
|
|
2546
|
+
private addMessageToChat(message: AgentMessage, options?: { populateHistory?: boolean }): void {
|
|
2547
|
+
switch (message.role) {
|
|
2548
|
+
case "bashExecution": {
|
|
2549
|
+
const component = new BashExecutionComponent(message.command, this.ui, message.excludeFromContext);
|
|
2550
|
+
if (message.output) {
|
|
2551
|
+
component.appendOutput(message.output);
|
|
2552
|
+
}
|
|
2553
|
+
component.setComplete(
|
|
2554
|
+
message.exitCode,
|
|
2555
|
+
message.cancelled,
|
|
2556
|
+
message.truncated ? ({ truncated: true } as TruncationResult) : undefined,
|
|
2557
|
+
message.fullOutputPath,
|
|
2558
|
+
);
|
|
2559
|
+
this.chatContainer.addChild(component);
|
|
2560
|
+
break;
|
|
2561
|
+
}
|
|
2562
|
+
case "custom": {
|
|
2563
|
+
if (message.display) {
|
|
2564
|
+
const renderer = this.session.extensionRunner?.getMessageRenderer(message.customType);
|
|
2565
|
+
const component = new CustomMessageComponent(message, renderer, this.getMarkdownThemeWithSettings());
|
|
2566
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
2567
|
+
this.chatContainer.addChild(component);
|
|
2568
|
+
}
|
|
2569
|
+
break;
|
|
2570
|
+
}
|
|
2571
|
+
case "compactionSummary": {
|
|
2572
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2573
|
+
const component = new CompactionSummaryMessageComponent(message, this.getMarkdownThemeWithSettings());
|
|
2574
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
2575
|
+
this.chatContainer.addChild(component);
|
|
2576
|
+
break;
|
|
2577
|
+
}
|
|
2578
|
+
case "branchSummary": {
|
|
2579
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2580
|
+
const component = new BranchSummaryMessageComponent(message, this.getMarkdownThemeWithSettings());
|
|
2581
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
2582
|
+
this.chatContainer.addChild(component);
|
|
2583
|
+
break;
|
|
2584
|
+
}
|
|
2585
|
+
case "user": {
|
|
2586
|
+
const textContent = this.getUserMessageText(message);
|
|
2587
|
+
if (textContent) {
|
|
2588
|
+
const skillBlock = parseSkillBlock(textContent);
|
|
2589
|
+
if (skillBlock) {
|
|
2590
|
+
// Render skill block (collapsible)
|
|
2591
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2592
|
+
const component = new SkillInvocationMessageComponent(
|
|
2593
|
+
skillBlock,
|
|
2594
|
+
this.getMarkdownThemeWithSettings(),
|
|
2595
|
+
);
|
|
2596
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
2597
|
+
this.chatContainer.addChild(component);
|
|
2598
|
+
// Render user message separately if present
|
|
2599
|
+
if (skillBlock.userMessage) {
|
|
2600
|
+
const userComponent = new UserMessageComponent(
|
|
2601
|
+
skillBlock.userMessage,
|
|
2602
|
+
this.getMarkdownThemeWithSettings(),
|
|
2603
|
+
);
|
|
2604
|
+
this.chatContainer.addChild(userComponent);
|
|
2605
|
+
}
|
|
2606
|
+
} else {
|
|
2607
|
+
const userComponent = new UserMessageComponent(textContent, this.getMarkdownThemeWithSettings());
|
|
2608
|
+
this.chatContainer.addChild(userComponent);
|
|
2609
|
+
}
|
|
2610
|
+
if (options?.populateHistory) {
|
|
2611
|
+
this.editor.addToHistory?.(textContent);
|
|
2612
|
+
}
|
|
2613
|
+
}
|
|
2614
|
+
break;
|
|
2615
|
+
}
|
|
2616
|
+
case "assistant": {
|
|
2617
|
+
const assistantComponent = new AssistantMessageComponent(
|
|
2618
|
+
message,
|
|
2619
|
+
this.hideThinkingBlock,
|
|
2620
|
+
this.getMarkdownThemeWithSettings(),
|
|
2621
|
+
this.hiddenThinkingLabel,
|
|
2622
|
+
);
|
|
2623
|
+
this.chatContainer.addChild(assistantComponent);
|
|
2624
|
+
break;
|
|
2625
|
+
}
|
|
2626
|
+
case "toolResult": {
|
|
2627
|
+
// Tool results are rendered inline with tool calls, handled separately
|
|
2628
|
+
break;
|
|
2629
|
+
}
|
|
2630
|
+
default: {
|
|
2631
|
+
const _exhaustive: never = message;
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
}
|
|
2635
|
+
|
|
2636
|
+
/**
|
|
2637
|
+
* Render session context to chat. Used for initial load and rebuild after compaction.
|
|
2638
|
+
* @param sessionContext Session context to render
|
|
2639
|
+
* @param options.updateFooter Update footer state
|
|
2640
|
+
* @param options.populateHistory Add user messages to editor history
|
|
2641
|
+
*/
|
|
2642
|
+
private renderSessionContext(
|
|
2643
|
+
sessionContext: SessionContext,
|
|
2644
|
+
options: { updateFooter?: boolean; populateHistory?: boolean } = {},
|
|
2645
|
+
): void {
|
|
2646
|
+
this.pendingTools.clear();
|
|
2647
|
+
|
|
2648
|
+
if (options.updateFooter) {
|
|
2649
|
+
this.footer.invalidate();
|
|
2650
|
+
this.updateEditorBorderColor();
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
for (const message of sessionContext.messages) {
|
|
2654
|
+
// Assistant messages need special handling for tool calls
|
|
2655
|
+
if (message.role === "assistant") {
|
|
2656
|
+
this.addMessageToChat(message);
|
|
2657
|
+
// Render tool call components
|
|
2658
|
+
for (const content of message.content) {
|
|
2659
|
+
if (content.type === "toolCall") {
|
|
2660
|
+
const component = new ToolExecutionComponent(
|
|
2661
|
+
content.name,
|
|
2662
|
+
content.id,
|
|
2663
|
+
content.arguments,
|
|
2664
|
+
{ showImages: this.settingsManager.getShowImages() },
|
|
2665
|
+
this.getRegisteredToolDefinition(content.name),
|
|
2666
|
+
this.ui,
|
|
2667
|
+
);
|
|
2668
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
2669
|
+
this.chatContainer.addChild(component);
|
|
2670
|
+
|
|
2671
|
+
if (message.stopReason === "aborted" || message.stopReason === "error") {
|
|
2672
|
+
let errorMessage: string;
|
|
2673
|
+
if (message.stopReason === "aborted") {
|
|
2674
|
+
const retryAttempt = this.session.retryAttempt;
|
|
2675
|
+
errorMessage =
|
|
2676
|
+
retryAttempt > 0
|
|
2677
|
+
? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
|
|
2678
|
+
: "Operation aborted";
|
|
2679
|
+
} else {
|
|
2680
|
+
errorMessage = message.errorMessage || "Error";
|
|
2681
|
+
}
|
|
2682
|
+
component.updateResult({
|
|
2683
|
+
content: [{ type: "text", text: errorMessage }],
|
|
2684
|
+
isError: true,
|
|
2685
|
+
});
|
|
2686
|
+
} else {
|
|
2687
|
+
this.pendingTools.set(content.id, component);
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
} else if (message.role === "toolResult") {
|
|
2692
|
+
// Match tool results to pending tool components
|
|
2693
|
+
const component = this.pendingTools.get(message.toolCallId);
|
|
2694
|
+
if (component) {
|
|
2695
|
+
component.updateResult(message);
|
|
2696
|
+
this.pendingTools.delete(message.toolCallId);
|
|
2697
|
+
}
|
|
2698
|
+
} else {
|
|
2699
|
+
// All other messages use standard rendering
|
|
2700
|
+
this.addMessageToChat(message, options);
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
|
|
2704
|
+
this.pendingTools.clear();
|
|
2705
|
+
this.ui.requestRender();
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2708
|
+
renderInitialMessages(): void {
|
|
2709
|
+
// Get aligned messages and entries from session context
|
|
2710
|
+
const context = this.sessionManager.buildSessionContext();
|
|
2711
|
+
this.renderSessionContext(context, {
|
|
2712
|
+
updateFooter: true,
|
|
2713
|
+
populateHistory: true,
|
|
2714
|
+
});
|
|
2715
|
+
|
|
2716
|
+
// Show compaction info if session was compacted
|
|
2717
|
+
const allEntries = this.sessionManager.getEntries();
|
|
2718
|
+
const compactionCount = allEntries.filter((e) => e.type === "compaction").length;
|
|
2719
|
+
if (compactionCount > 0) {
|
|
2720
|
+
const times = compactionCount === 1 ? "1 time" : `${compactionCount} times`;
|
|
2721
|
+
this.showStatus(`Session compacted ${times}`);
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
|
|
2725
|
+
async getUserInput(): Promise<string> {
|
|
2726
|
+
return new Promise((resolve) => {
|
|
2727
|
+
this.onInputCallback = (text: string) => {
|
|
2728
|
+
this.onInputCallback = undefined;
|
|
2729
|
+
resolve(text);
|
|
2730
|
+
};
|
|
2731
|
+
});
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
private rebuildChatFromMessages(): void {
|
|
2735
|
+
this.chatContainer.clear();
|
|
2736
|
+
const context = this.sessionManager.buildSessionContext();
|
|
2737
|
+
this.renderSessionContext(context);
|
|
2738
|
+
}
|
|
2739
|
+
|
|
2740
|
+
// =========================================================================
|
|
2741
|
+
// Key handlers
|
|
2742
|
+
// =========================================================================
|
|
2743
|
+
|
|
2744
|
+
private handleCtrlC(): void {
|
|
2745
|
+
const now = Date.now();
|
|
2746
|
+
if (now - this.lastSigintTime < 500) {
|
|
2747
|
+
void this.shutdown();
|
|
2748
|
+
} else {
|
|
2749
|
+
this.clearEditor();
|
|
2750
|
+
this.lastSigintTime = now;
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
|
|
2754
|
+
private handleCtrlD(): void {
|
|
2755
|
+
// Only called when editor is empty (enforced by CustomEditor)
|
|
2756
|
+
void this.shutdown();
|
|
2757
|
+
}
|
|
2758
|
+
|
|
2759
|
+
/**
|
|
2760
|
+
* Gracefully shutdown the agent.
|
|
2761
|
+
* Emits shutdown event to extensions, then exits.
|
|
2762
|
+
*/
|
|
2763
|
+
private isShuttingDown = false;
|
|
2764
|
+
|
|
2765
|
+
private async shutdown(): Promise<void> {
|
|
2766
|
+
if (this.isShuttingDown) return;
|
|
2767
|
+
this.isShuttingDown = true;
|
|
2768
|
+
|
|
2769
|
+
// Emit shutdown event to extensions
|
|
2770
|
+
const extensionRunner = this.session.extensionRunner;
|
|
2771
|
+
if (extensionRunner?.hasHandlers("session_shutdown")) {
|
|
2772
|
+
await extensionRunner.emit({
|
|
2773
|
+
type: "session_shutdown",
|
|
2774
|
+
});
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
// Wait for any pending renders to complete
|
|
2778
|
+
// requestRender() uses process.nextTick(), so we wait one tick
|
|
2779
|
+
await new Promise((resolve) => process.nextTick(resolve));
|
|
2780
|
+
|
|
2781
|
+
// Drain any in-flight Kitty key release events before stopping.
|
|
2782
|
+
// This prevents escape sequences from leaking to the parent shell over slow SSH.
|
|
2783
|
+
await this.ui.terminal.drainInput(1000);
|
|
2784
|
+
|
|
2785
|
+
this.stop();
|
|
2786
|
+
process.exit(0);
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
/**
|
|
2790
|
+
* Check if shutdown was requested and perform shutdown if so.
|
|
2791
|
+
*/
|
|
2792
|
+
private async checkShutdownRequested(): Promise<void> {
|
|
2793
|
+
if (!this.shutdownRequested) return;
|
|
2794
|
+
await this.shutdown();
|
|
2795
|
+
}
|
|
2796
|
+
|
|
2797
|
+
private handleCtrlZ(): void {
|
|
2798
|
+
// Keep the event loop alive while suspended. Without this, stopping the TUI
|
|
2799
|
+
// can leave Node with no ref'ed handles, causing the process to exit on fg
|
|
2800
|
+
// before the SIGCONT handler gets a chance to restore the terminal.
|
|
2801
|
+
const suspendKeepAlive = setInterval(() => {}, 2 ** 30);
|
|
2802
|
+
|
|
2803
|
+
// Ignore SIGINT while suspended so Ctrl+C in the terminal does not
|
|
2804
|
+
// kill the backgrounded process. The handler is removed on resume.
|
|
2805
|
+
const ignoreSigint = () => {};
|
|
2806
|
+
process.on("SIGINT", ignoreSigint);
|
|
2807
|
+
|
|
2808
|
+
// Set up handler to restore TUI when resumed
|
|
2809
|
+
process.once("SIGCONT", () => {
|
|
2810
|
+
clearInterval(suspendKeepAlive);
|
|
2811
|
+
process.removeListener("SIGINT", ignoreSigint);
|
|
2812
|
+
this.ui.start();
|
|
2813
|
+
this.ui.requestRender(true);
|
|
2814
|
+
});
|
|
2815
|
+
|
|
2816
|
+
try {
|
|
2817
|
+
// Stop the TUI (restore terminal to normal mode)
|
|
2818
|
+
this.ui.stop();
|
|
2819
|
+
|
|
2820
|
+
// Send SIGTSTP to process group (pid=0 means all processes in group)
|
|
2821
|
+
process.kill(0, "SIGTSTP");
|
|
2822
|
+
} catch (error) {
|
|
2823
|
+
clearInterval(suspendKeepAlive);
|
|
2824
|
+
process.removeListener("SIGINT", ignoreSigint);
|
|
2825
|
+
throw error;
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
private async handleFollowUp(): Promise<void> {
|
|
2830
|
+
const text = (this.editor.getExpandedText?.() ?? this.editor.getText()).trim();
|
|
2831
|
+
if (!text) return;
|
|
2832
|
+
|
|
2833
|
+
// Queue input during compaction (extension commands execute immediately)
|
|
2834
|
+
if (this.session.isCompacting) {
|
|
2835
|
+
if (this.isExtensionCommand(text)) {
|
|
2836
|
+
this.editor.addToHistory?.(text);
|
|
2837
|
+
this.editor.setText("");
|
|
2838
|
+
await this.session.prompt(text);
|
|
2839
|
+
} else {
|
|
2840
|
+
this.queueCompactionMessage(text, "followUp");
|
|
2841
|
+
}
|
|
2842
|
+
return;
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2845
|
+
// Alt+Enter queues a follow-up message (waits until agent finishes)
|
|
2846
|
+
// This handles extension commands (execute immediately), prompt template expansion, and queueing
|
|
2847
|
+
if (this.session.isStreaming) {
|
|
2848
|
+
this.editor.addToHistory?.(text);
|
|
2849
|
+
this.editor.setText("");
|
|
2850
|
+
await this.session.prompt(text, { streamingBehavior: "followUp" });
|
|
2851
|
+
this.updatePendingMessagesDisplay();
|
|
2852
|
+
this.ui.requestRender();
|
|
2853
|
+
}
|
|
2854
|
+
// If not streaming, Alt+Enter acts like regular Enter (trigger onSubmit)
|
|
2855
|
+
else if (this.editor.onSubmit) {
|
|
2856
|
+
this.editor.onSubmit(text);
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
private handleDequeue(): void {
|
|
2861
|
+
const restored = this.restoreQueuedMessagesToEditor();
|
|
2862
|
+
if (restored === 0) {
|
|
2863
|
+
this.showStatus("No queued messages to restore");
|
|
2864
|
+
} else {
|
|
2865
|
+
this.showStatus(`Restored ${restored} queued message${restored > 1 ? "s" : ""} to editor`);
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
|
|
2869
|
+
private updateEditorBorderColor(): void {
|
|
2870
|
+
if (this.isBashMode) {
|
|
2871
|
+
this.editor.borderColor = theme.getBashModeBorderColor();
|
|
2872
|
+
} else {
|
|
2873
|
+
const level = this.session.thinkingLevel || "off";
|
|
2874
|
+
this.editor.borderColor = theme.getThinkingBorderColor(level);
|
|
2875
|
+
}
|
|
2876
|
+
this.ui.requestRender();
|
|
2877
|
+
}
|
|
2878
|
+
|
|
2879
|
+
private cycleThinkingLevel(): void {
|
|
2880
|
+
const newLevel = this.session.cycleThinkingLevel();
|
|
2881
|
+
if (newLevel === undefined) {
|
|
2882
|
+
this.showStatus("Current model does not support thinking");
|
|
2883
|
+
} else {
|
|
2884
|
+
this.footer.invalidate();
|
|
2885
|
+
this.updateEditorBorderColor();
|
|
2886
|
+
this.showStatus(`Thinking level: ${newLevel}`);
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
|
|
2890
|
+
private async cycleModel(direction: "forward" | "backward"): Promise<void> {
|
|
2891
|
+
try {
|
|
2892
|
+
const result = await this.session.cycleModel(direction);
|
|
2893
|
+
if (result === undefined) {
|
|
2894
|
+
const msg = this.session.scopedModels.length > 0 ? "Only one model in scope" : "Only one model available";
|
|
2895
|
+
this.showStatus(msg);
|
|
2896
|
+
} else {
|
|
2897
|
+
this.footer.invalidate();
|
|
2898
|
+
this.updateEditorBorderColor();
|
|
2899
|
+
const thinkingStr =
|
|
2900
|
+
result.model.reasoning && result.thinkingLevel !== "off" ? ` (thinking: ${result.thinkingLevel})` : "";
|
|
2901
|
+
this.showStatus(`Switched to ${result.model.name || result.model.id}${thinkingStr}`);
|
|
2902
|
+
}
|
|
2903
|
+
} catch (error) {
|
|
2904
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
|
|
2908
|
+
private toggleToolOutputExpansion(): void {
|
|
2909
|
+
this.setToolsExpanded(!this.toolOutputExpanded);
|
|
2910
|
+
}
|
|
2911
|
+
|
|
2912
|
+
private setToolsExpanded(expanded: boolean): void {
|
|
2913
|
+
this.toolOutputExpanded = expanded;
|
|
2914
|
+
for (const child of this.chatContainer.children) {
|
|
2915
|
+
if (isExpandable(child)) {
|
|
2916
|
+
child.setExpanded(expanded);
|
|
2917
|
+
}
|
|
2918
|
+
}
|
|
2919
|
+
this.ui.requestRender();
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
private toggleThinkingBlockVisibility(): void {
|
|
2923
|
+
this.hideThinkingBlock = !this.hideThinkingBlock;
|
|
2924
|
+
this.settingsManager.setHideThinkingBlock(this.hideThinkingBlock);
|
|
2925
|
+
|
|
2926
|
+
// Rebuild chat from session messages
|
|
2927
|
+
this.chatContainer.clear();
|
|
2928
|
+
this.rebuildChatFromMessages();
|
|
2929
|
+
|
|
2930
|
+
// If streaming, re-add the streaming component with updated visibility and re-render
|
|
2931
|
+
if (this.streamingComponent && this.streamingMessage) {
|
|
2932
|
+
this.streamingComponent.setHideThinkingBlock(this.hideThinkingBlock);
|
|
2933
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
2934
|
+
this.chatContainer.addChild(this.streamingComponent);
|
|
2935
|
+
}
|
|
2936
|
+
|
|
2937
|
+
this.showStatus(`Thinking blocks: ${this.hideThinkingBlock ? "hidden" : "visible"}`);
|
|
2938
|
+
}
|
|
2939
|
+
|
|
2940
|
+
private openExternalEditor(): void {
|
|
2941
|
+
// Determine editor (respect $VISUAL, then $EDITOR)
|
|
2942
|
+
const editorCmd = process.env.VISUAL || process.env.EDITOR;
|
|
2943
|
+
if (!editorCmd) {
|
|
2944
|
+
this.showWarning("No editor configured. Set $VISUAL or $EDITOR environment variable.");
|
|
2945
|
+
return;
|
|
2946
|
+
}
|
|
2947
|
+
|
|
2948
|
+
const currentText = this.editor.getExpandedText?.() ?? this.editor.getText();
|
|
2949
|
+
const tmpFile = path.join(os.tmpdir(), `pi-editor-${Date.now()}.pi.md`);
|
|
2950
|
+
|
|
2951
|
+
try {
|
|
2952
|
+
// Write current content to temp file
|
|
2953
|
+
fs.writeFileSync(tmpFile, currentText, "utf-8");
|
|
2954
|
+
|
|
2955
|
+
// Stop TUI to release terminal
|
|
2956
|
+
this.ui.stop();
|
|
2957
|
+
|
|
2958
|
+
// Split by space to support editor arguments (e.g., "code --wait")
|
|
2959
|
+
const [editor, ...editorArgs] = editorCmd.split(" ");
|
|
2960
|
+
|
|
2961
|
+
// Spawn editor synchronously with inherited stdio for interactive editing
|
|
2962
|
+
const result = spawnSync(editor, [...editorArgs, tmpFile], {
|
|
2963
|
+
stdio: "inherit",
|
|
2964
|
+
shell: process.platform === "win32",
|
|
2965
|
+
});
|
|
2966
|
+
|
|
2967
|
+
// On successful exit (status 0), replace editor content
|
|
2968
|
+
if (result.status === 0) {
|
|
2969
|
+
const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
|
|
2970
|
+
this.editor.setText(newContent);
|
|
2971
|
+
}
|
|
2972
|
+
// On non-zero exit, keep original text (no action needed)
|
|
2973
|
+
} finally {
|
|
2974
|
+
// Clean up temp file
|
|
2975
|
+
try {
|
|
2976
|
+
fs.unlinkSync(tmpFile);
|
|
2977
|
+
} catch {
|
|
2978
|
+
// Ignore cleanup errors
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2981
|
+
// Restart TUI
|
|
2982
|
+
this.ui.start();
|
|
2983
|
+
// Force full re-render since external editor uses alternate screen
|
|
2984
|
+
this.ui.requestRender(true);
|
|
2985
|
+
}
|
|
2986
|
+
}
|
|
2987
|
+
|
|
2988
|
+
// =========================================================================
|
|
2989
|
+
// UI helpers
|
|
2990
|
+
// =========================================================================
|
|
2991
|
+
|
|
2992
|
+
clearEditor(): void {
|
|
2993
|
+
this.editor.setText("");
|
|
2994
|
+
this.ui.requestRender();
|
|
2995
|
+
}
|
|
2996
|
+
|
|
2997
|
+
showError(errorMessage: string): void {
|
|
2998
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2999
|
+
this.chatContainer.addChild(new Text(theme.fg("error", `Error: ${errorMessage}`), 1, 0));
|
|
3000
|
+
this.ui.requestRender();
|
|
3001
|
+
}
|
|
3002
|
+
|
|
3003
|
+
showWarning(warningMessage: string): void {
|
|
3004
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3005
|
+
this.chatContainer.addChild(new Text(theme.fg("warning", `Warning: ${warningMessage}`), 1, 0));
|
|
3006
|
+
this.ui.requestRender();
|
|
3007
|
+
}
|
|
3008
|
+
|
|
3009
|
+
showNewVersionNotification(newVersion: string): void {
|
|
3010
|
+
const action = theme.fg("accent", getUpdateInstruction());
|
|
3011
|
+
const updateInstruction = theme.fg("muted", `New version ${newVersion} is available. `) + action;
|
|
3012
|
+
|
|
3013
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3014
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
3015
|
+
this.chatContainer.addChild(
|
|
3016
|
+
new Text(`${theme.bold(theme.fg("warning", "Update Available"))}\n${updateInstruction}`, 1, 0),
|
|
3017
|
+
);
|
|
3018
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
3019
|
+
this.ui.requestRender();
|
|
3020
|
+
}
|
|
3021
|
+
|
|
3022
|
+
showPackageUpdateNotification(packages: string[]): void {
|
|
3023
|
+
const action = theme.fg("accent", `${APP_NAME} update`);
|
|
3024
|
+
const updateInstruction = theme.fg("muted", "Package updates are available. Run ") + action;
|
|
3025
|
+
const packageLines = packages.map((pkg) => `- ${pkg}`).join("\n");
|
|
3026
|
+
|
|
3027
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3028
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
3029
|
+
this.chatContainer.addChild(
|
|
3030
|
+
new Text(
|
|
3031
|
+
`${theme.bold(theme.fg("warning", "Package Updates Available"))}\n${updateInstruction}\n${theme.fg("muted", "Packages:")}\n${packageLines}`,
|
|
3032
|
+
1,
|
|
3033
|
+
0,
|
|
3034
|
+
),
|
|
3035
|
+
);
|
|
3036
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
3037
|
+
this.ui.requestRender();
|
|
3038
|
+
}
|
|
3039
|
+
|
|
3040
|
+
/**
|
|
3041
|
+
* Get all queued messages (read-only).
|
|
3042
|
+
* Combines session queue and compaction queue.
|
|
3043
|
+
*/
|
|
3044
|
+
private getAllQueuedMessages(): { steering: string[]; followUp: string[] } {
|
|
3045
|
+
return {
|
|
3046
|
+
steering: [
|
|
3047
|
+
...this.session.getSteeringMessages(),
|
|
3048
|
+
...this.compactionQueuedMessages.filter((msg) => msg.mode === "steer").map((msg) => msg.text),
|
|
3049
|
+
],
|
|
3050
|
+
followUp: [
|
|
3051
|
+
...this.session.getFollowUpMessages(),
|
|
3052
|
+
...this.compactionQueuedMessages.filter((msg) => msg.mode === "followUp").map((msg) => msg.text),
|
|
3053
|
+
],
|
|
3054
|
+
};
|
|
3055
|
+
}
|
|
3056
|
+
|
|
3057
|
+
/**
|
|
3058
|
+
* Clear all queued messages and return their contents.
|
|
3059
|
+
* Clears both session queue and compaction queue.
|
|
3060
|
+
*/
|
|
3061
|
+
private clearAllQueues(): { steering: string[]; followUp: string[] } {
|
|
3062
|
+
const { steering, followUp } = this.session.clearQueue();
|
|
3063
|
+
const compactionSteering = this.compactionQueuedMessages
|
|
3064
|
+
.filter((msg) => msg.mode === "steer")
|
|
3065
|
+
.map((msg) => msg.text);
|
|
3066
|
+
const compactionFollowUp = this.compactionQueuedMessages
|
|
3067
|
+
.filter((msg) => msg.mode === "followUp")
|
|
3068
|
+
.map((msg) => msg.text);
|
|
3069
|
+
this.compactionQueuedMessages = [];
|
|
3070
|
+
return {
|
|
3071
|
+
steering: [...steering, ...compactionSteering],
|
|
3072
|
+
followUp: [...followUp, ...compactionFollowUp],
|
|
3073
|
+
};
|
|
3074
|
+
}
|
|
3075
|
+
|
|
3076
|
+
private updatePendingMessagesDisplay(): void {
|
|
3077
|
+
this.pendingMessagesContainer.clear();
|
|
3078
|
+
const { steering: steeringMessages, followUp: followUpMessages } = this.getAllQueuedMessages();
|
|
3079
|
+
if (steeringMessages.length > 0 || followUpMessages.length > 0) {
|
|
3080
|
+
this.pendingMessagesContainer.addChild(new Spacer(1));
|
|
3081
|
+
for (const message of steeringMessages) {
|
|
3082
|
+
const text = theme.fg("dim", `Steering: ${message}`);
|
|
3083
|
+
this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
|
|
3084
|
+
}
|
|
3085
|
+
for (const message of followUpMessages) {
|
|
3086
|
+
const text = theme.fg("dim", `Follow-up: ${message}`);
|
|
3087
|
+
this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
|
|
3088
|
+
}
|
|
3089
|
+
const dequeueHint = this.getAppKeyDisplay("app.message.dequeue");
|
|
3090
|
+
const hintText = theme.fg("dim", `↳ ${dequeueHint} to edit all queued messages`);
|
|
3091
|
+
this.pendingMessagesContainer.addChild(new TruncatedText(hintText, 1, 0));
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3094
|
+
|
|
3095
|
+
private restoreQueuedMessagesToEditor(options?: { abort?: boolean; currentText?: string }): number {
|
|
3096
|
+
const { steering, followUp } = this.clearAllQueues();
|
|
3097
|
+
const allQueued = [...steering, ...followUp];
|
|
3098
|
+
if (allQueued.length === 0) {
|
|
3099
|
+
this.updatePendingMessagesDisplay();
|
|
3100
|
+
if (options?.abort) {
|
|
3101
|
+
this.agent.abort();
|
|
3102
|
+
}
|
|
3103
|
+
return 0;
|
|
3104
|
+
}
|
|
3105
|
+
const queuedText = allQueued.join("\n\n");
|
|
3106
|
+
const currentText = options?.currentText ?? this.editor.getText();
|
|
3107
|
+
const combinedText = [queuedText, currentText].filter((t) => t.trim()).join("\n\n");
|
|
3108
|
+
this.editor.setText(combinedText);
|
|
3109
|
+
this.updatePendingMessagesDisplay();
|
|
3110
|
+
if (options?.abort) {
|
|
3111
|
+
this.agent.abort();
|
|
3112
|
+
}
|
|
3113
|
+
return allQueued.length;
|
|
3114
|
+
}
|
|
3115
|
+
|
|
3116
|
+
private queueCompactionMessage(text: string, mode: "steer" | "followUp"): void {
|
|
3117
|
+
this.compactionQueuedMessages.push({ text, mode });
|
|
3118
|
+
this.editor.addToHistory?.(text);
|
|
3119
|
+
this.editor.setText("");
|
|
3120
|
+
this.updatePendingMessagesDisplay();
|
|
3121
|
+
this.showStatus("Queued message for after compaction");
|
|
3122
|
+
}
|
|
3123
|
+
|
|
3124
|
+
private isExtensionCommand(text: string): boolean {
|
|
3125
|
+
if (!text.startsWith("/")) return false;
|
|
3126
|
+
|
|
3127
|
+
const extensionRunner = this.session.extensionRunner;
|
|
3128
|
+
if (!extensionRunner) return false;
|
|
3129
|
+
|
|
3130
|
+
const spaceIndex = text.indexOf(" ");
|
|
3131
|
+
const commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
|
|
3132
|
+
return !!extensionRunner.getCommand(commandName);
|
|
3133
|
+
}
|
|
3134
|
+
|
|
3135
|
+
private async flushCompactionQueue(options?: { willRetry?: boolean }): Promise<void> {
|
|
3136
|
+
if (this.compactionQueuedMessages.length === 0) {
|
|
3137
|
+
return;
|
|
3138
|
+
}
|
|
3139
|
+
|
|
3140
|
+
const queuedMessages = [...this.compactionQueuedMessages];
|
|
3141
|
+
this.compactionQueuedMessages = [];
|
|
3142
|
+
this.updatePendingMessagesDisplay();
|
|
3143
|
+
|
|
3144
|
+
const restoreQueue = (error: unknown) => {
|
|
3145
|
+
this.session.clearQueue();
|
|
3146
|
+
this.compactionQueuedMessages = queuedMessages;
|
|
3147
|
+
this.updatePendingMessagesDisplay();
|
|
3148
|
+
this.showError(
|
|
3149
|
+
`Failed to send queued message${queuedMessages.length > 1 ? "s" : ""}: ${
|
|
3150
|
+
error instanceof Error ? error.message : String(error)
|
|
3151
|
+
}`,
|
|
3152
|
+
);
|
|
3153
|
+
};
|
|
3154
|
+
|
|
3155
|
+
try {
|
|
3156
|
+
if (options?.willRetry) {
|
|
3157
|
+
// When retry is pending, queue messages for the retry turn
|
|
3158
|
+
for (const message of queuedMessages) {
|
|
3159
|
+
if (this.isExtensionCommand(message.text)) {
|
|
3160
|
+
await this.session.prompt(message.text);
|
|
3161
|
+
} else if (message.mode === "followUp") {
|
|
3162
|
+
await this.session.followUp(message.text);
|
|
3163
|
+
} else {
|
|
3164
|
+
await this.session.steer(message.text);
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
this.updatePendingMessagesDisplay();
|
|
3168
|
+
return;
|
|
3169
|
+
}
|
|
3170
|
+
|
|
3171
|
+
// Find first non-extension-command message to use as prompt
|
|
3172
|
+
const firstPromptIndex = queuedMessages.findIndex((message) => !this.isExtensionCommand(message.text));
|
|
3173
|
+
if (firstPromptIndex === -1) {
|
|
3174
|
+
// All extension commands - execute them all
|
|
3175
|
+
for (const message of queuedMessages) {
|
|
3176
|
+
await this.session.prompt(message.text);
|
|
3177
|
+
}
|
|
3178
|
+
return;
|
|
3179
|
+
}
|
|
3180
|
+
|
|
3181
|
+
// Execute any extension commands before the first prompt
|
|
3182
|
+
const preCommands = queuedMessages.slice(0, firstPromptIndex);
|
|
3183
|
+
const firstPrompt = queuedMessages[firstPromptIndex];
|
|
3184
|
+
const rest = queuedMessages.slice(firstPromptIndex + 1);
|
|
3185
|
+
|
|
3186
|
+
for (const message of preCommands) {
|
|
3187
|
+
await this.session.prompt(message.text);
|
|
3188
|
+
}
|
|
3189
|
+
|
|
3190
|
+
// Send first prompt (starts streaming)
|
|
3191
|
+
const promptPromise = this.session.prompt(firstPrompt.text).catch((error) => {
|
|
3192
|
+
restoreQueue(error);
|
|
3193
|
+
});
|
|
3194
|
+
|
|
3195
|
+
// Queue remaining messages
|
|
3196
|
+
for (const message of rest) {
|
|
3197
|
+
if (this.isExtensionCommand(message.text)) {
|
|
3198
|
+
await this.session.prompt(message.text);
|
|
3199
|
+
} else if (message.mode === "followUp") {
|
|
3200
|
+
await this.session.followUp(message.text);
|
|
3201
|
+
} else {
|
|
3202
|
+
await this.session.steer(message.text);
|
|
3203
|
+
}
|
|
3204
|
+
}
|
|
3205
|
+
this.updatePendingMessagesDisplay();
|
|
3206
|
+
void promptPromise;
|
|
3207
|
+
} catch (error) {
|
|
3208
|
+
restoreQueue(error);
|
|
3209
|
+
}
|
|
3210
|
+
}
|
|
3211
|
+
|
|
3212
|
+
/** Move pending bash components from pending area to chat */
|
|
3213
|
+
private flushPendingBashComponents(): void {
|
|
3214
|
+
for (const component of this.pendingBashComponents) {
|
|
3215
|
+
this.pendingMessagesContainer.removeChild(component);
|
|
3216
|
+
this.chatContainer.addChild(component);
|
|
3217
|
+
}
|
|
3218
|
+
this.pendingBashComponents = [];
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3221
|
+
// =========================================================================
|
|
3222
|
+
// Selectors
|
|
3223
|
+
// =========================================================================
|
|
3224
|
+
|
|
3225
|
+
/**
|
|
3226
|
+
* Shows a selector component in place of the editor.
|
|
3227
|
+
* @param create Factory that receives a `done` callback and returns the component and focus target
|
|
3228
|
+
*/
|
|
3229
|
+
private showSelector(create: (done: () => void) => { component: Component; focus: Component }): void {
|
|
3230
|
+
const done = () => {
|
|
3231
|
+
this.editorContainer.clear();
|
|
3232
|
+
this.editorContainer.addChild(this.editor);
|
|
3233
|
+
this.ui.setFocus(this.editor);
|
|
3234
|
+
};
|
|
3235
|
+
const { component, focus } = create(done);
|
|
3236
|
+
this.editorContainer.clear();
|
|
3237
|
+
this.editorContainer.addChild(component);
|
|
3238
|
+
this.ui.setFocus(focus);
|
|
3239
|
+
this.ui.requestRender();
|
|
3240
|
+
}
|
|
3241
|
+
|
|
3242
|
+
private showSettingsSelector(): void {
|
|
3243
|
+
this.showSelector((done) => {
|
|
3244
|
+
const selector = new SettingsSelectorComponent(
|
|
3245
|
+
{
|
|
3246
|
+
autoCompact: this.session.autoCompactionEnabled,
|
|
3247
|
+
showImages: this.settingsManager.getShowImages(),
|
|
3248
|
+
autoResizeImages: this.settingsManager.getImageAutoResize(),
|
|
3249
|
+
blockImages: this.settingsManager.getBlockImages(),
|
|
3250
|
+
enableSkillCommands: this.settingsManager.getEnableSkillCommands(),
|
|
3251
|
+
steeringMode: this.session.steeringMode,
|
|
3252
|
+
followUpMode: this.session.followUpMode,
|
|
3253
|
+
transport: this.settingsManager.getTransport(),
|
|
3254
|
+
thinkingLevel: this.session.thinkingLevel,
|
|
3255
|
+
availableThinkingLevels: this.session.getAvailableThinkingLevels(),
|
|
3256
|
+
currentTheme: this.settingsManager.getTheme() || "dark",
|
|
3257
|
+
availableThemes: getAvailableThemes(),
|
|
3258
|
+
hideThinkingBlock: this.hideThinkingBlock,
|
|
3259
|
+
collapseChangelog: this.settingsManager.getCollapseChangelog(),
|
|
3260
|
+
doubleEscapeAction: this.settingsManager.getDoubleEscapeAction(),
|
|
3261
|
+
treeFilterMode: this.settingsManager.getTreeFilterMode(),
|
|
3262
|
+
showHardwareCursor: this.settingsManager.getShowHardwareCursor(),
|
|
3263
|
+
editorPaddingX: this.settingsManager.getEditorPaddingX(),
|
|
3264
|
+
autocompleteMaxVisible: this.settingsManager.getAutocompleteMaxVisible(),
|
|
3265
|
+
quietStartup: this.settingsManager.getQuietStartup(),
|
|
3266
|
+
clearOnShrink: this.settingsManager.getClearOnShrink(),
|
|
3267
|
+
},
|
|
3268
|
+
{
|
|
3269
|
+
onAutoCompactChange: (enabled) => {
|
|
3270
|
+
this.session.setAutoCompactionEnabled(enabled);
|
|
3271
|
+
this.footer.setAutoCompactEnabled(enabled);
|
|
3272
|
+
},
|
|
3273
|
+
onShowImagesChange: (enabled) => {
|
|
3274
|
+
this.settingsManager.setShowImages(enabled);
|
|
3275
|
+
for (const child of this.chatContainer.children) {
|
|
3276
|
+
if (child instanceof ToolExecutionComponent) {
|
|
3277
|
+
child.setShowImages(enabled);
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
},
|
|
3281
|
+
onAutoResizeImagesChange: (enabled) => {
|
|
3282
|
+
this.settingsManager.setImageAutoResize(enabled);
|
|
3283
|
+
},
|
|
3284
|
+
onBlockImagesChange: (blocked) => {
|
|
3285
|
+
this.settingsManager.setBlockImages(blocked);
|
|
3286
|
+
},
|
|
3287
|
+
onEnableSkillCommandsChange: (enabled) => {
|
|
3288
|
+
this.settingsManager.setEnableSkillCommands(enabled);
|
|
3289
|
+
this.setupAutocomplete(this.fdPath);
|
|
3290
|
+
},
|
|
3291
|
+
onSteeringModeChange: (mode) => {
|
|
3292
|
+
this.session.setSteeringMode(mode);
|
|
3293
|
+
},
|
|
3294
|
+
onFollowUpModeChange: (mode) => {
|
|
3295
|
+
this.session.setFollowUpMode(mode);
|
|
3296
|
+
},
|
|
3297
|
+
onTransportChange: (transport) => {
|
|
3298
|
+
this.settingsManager.setTransport(transport);
|
|
3299
|
+
this.session.agent.setTransport(transport);
|
|
3300
|
+
},
|
|
3301
|
+
onThinkingLevelChange: (level) => {
|
|
3302
|
+
this.session.setThinkingLevel(level);
|
|
3303
|
+
this.footer.invalidate();
|
|
3304
|
+
this.updateEditorBorderColor();
|
|
3305
|
+
},
|
|
3306
|
+
onThemeChange: (themeName) => {
|
|
3307
|
+
const result = setTheme(themeName, true);
|
|
3308
|
+
this.settingsManager.setTheme(themeName);
|
|
3309
|
+
this.ui.invalidate();
|
|
3310
|
+
if (!result.success) {
|
|
3311
|
+
this.showError(`Failed to load theme "${themeName}": ${result.error}\nFell back to dark theme.`);
|
|
3312
|
+
}
|
|
3313
|
+
},
|
|
3314
|
+
onThemePreview: (themeName) => {
|
|
3315
|
+
const result = setTheme(themeName, true);
|
|
3316
|
+
if (result.success) {
|
|
3317
|
+
this.ui.invalidate();
|
|
3318
|
+
this.ui.requestRender();
|
|
3319
|
+
}
|
|
3320
|
+
},
|
|
3321
|
+
onHideThinkingBlockChange: (hidden) => {
|
|
3322
|
+
this.hideThinkingBlock = hidden;
|
|
3323
|
+
this.settingsManager.setHideThinkingBlock(hidden);
|
|
3324
|
+
for (const child of this.chatContainer.children) {
|
|
3325
|
+
if (child instanceof AssistantMessageComponent) {
|
|
3326
|
+
child.setHideThinkingBlock(hidden);
|
|
3327
|
+
}
|
|
3328
|
+
}
|
|
3329
|
+
this.chatContainer.clear();
|
|
3330
|
+
this.rebuildChatFromMessages();
|
|
3331
|
+
},
|
|
3332
|
+
onCollapseChangelogChange: (collapsed) => {
|
|
3333
|
+
this.settingsManager.setCollapseChangelog(collapsed);
|
|
3334
|
+
},
|
|
3335
|
+
onQuietStartupChange: (enabled) => {
|
|
3336
|
+
this.settingsManager.setQuietStartup(enabled);
|
|
3337
|
+
},
|
|
3338
|
+
onDoubleEscapeActionChange: (action) => {
|
|
3339
|
+
this.settingsManager.setDoubleEscapeAction(action);
|
|
3340
|
+
},
|
|
3341
|
+
onTreeFilterModeChange: (mode) => {
|
|
3342
|
+
this.settingsManager.setTreeFilterMode(mode);
|
|
3343
|
+
},
|
|
3344
|
+
onShowHardwareCursorChange: (enabled) => {
|
|
3345
|
+
this.settingsManager.setShowHardwareCursor(enabled);
|
|
3346
|
+
this.ui.setShowHardwareCursor(enabled);
|
|
3347
|
+
},
|
|
3348
|
+
onEditorPaddingXChange: (padding) => {
|
|
3349
|
+
this.settingsManager.setEditorPaddingX(padding);
|
|
3350
|
+
this.defaultEditor.setPaddingX(padding);
|
|
3351
|
+
if (this.editor !== this.defaultEditor && this.editor.setPaddingX !== undefined) {
|
|
3352
|
+
this.editor.setPaddingX(padding);
|
|
3353
|
+
}
|
|
3354
|
+
},
|
|
3355
|
+
onAutocompleteMaxVisibleChange: (maxVisible) => {
|
|
3356
|
+
this.settingsManager.setAutocompleteMaxVisible(maxVisible);
|
|
3357
|
+
this.defaultEditor.setAutocompleteMaxVisible(maxVisible);
|
|
3358
|
+
if (this.editor !== this.defaultEditor && this.editor.setAutocompleteMaxVisible !== undefined) {
|
|
3359
|
+
this.editor.setAutocompleteMaxVisible(maxVisible);
|
|
3360
|
+
}
|
|
3361
|
+
},
|
|
3362
|
+
onClearOnShrinkChange: (enabled) => {
|
|
3363
|
+
this.settingsManager.setClearOnShrink(enabled);
|
|
3364
|
+
this.ui.setClearOnShrink(enabled);
|
|
3365
|
+
},
|
|
3366
|
+
onCancel: () => {
|
|
3367
|
+
done();
|
|
3368
|
+
this.ui.requestRender();
|
|
3369
|
+
},
|
|
3370
|
+
},
|
|
3371
|
+
);
|
|
3372
|
+
return { component: selector, focus: selector.getSettingsList() };
|
|
3373
|
+
});
|
|
3374
|
+
}
|
|
3375
|
+
|
|
3376
|
+
private async handleModelCommand(searchTerm?: string): Promise<void> {
|
|
3377
|
+
if (!searchTerm) {
|
|
3378
|
+
this.showModelSelector();
|
|
3379
|
+
return;
|
|
3380
|
+
}
|
|
3381
|
+
|
|
3382
|
+
const model = await this.findExactModelMatch(searchTerm);
|
|
3383
|
+
if (model) {
|
|
3384
|
+
try {
|
|
3385
|
+
await this.session.setModel(model);
|
|
3386
|
+
this.footer.invalidate();
|
|
3387
|
+
this.updateEditorBorderColor();
|
|
3388
|
+
this.showStatus(`Model: ${model.id}`);
|
|
3389
|
+
} catch (error) {
|
|
3390
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
3391
|
+
}
|
|
3392
|
+
return;
|
|
3393
|
+
}
|
|
3394
|
+
|
|
3395
|
+
this.showModelSelector(searchTerm);
|
|
3396
|
+
}
|
|
3397
|
+
|
|
3398
|
+
private async findExactModelMatch(searchTerm: string): Promise<Model<any> | undefined> {
|
|
3399
|
+
const models = await this.getModelCandidates();
|
|
3400
|
+
return findExactModelReferenceMatch(searchTerm, models);
|
|
3401
|
+
}
|
|
3402
|
+
|
|
3403
|
+
private async getModelCandidates(): Promise<Model<any>[]> {
|
|
3404
|
+
if (this.session.scopedModels.length > 0) {
|
|
3405
|
+
return this.session.scopedModels.map((scoped) => scoped.model);
|
|
3406
|
+
}
|
|
3407
|
+
|
|
3408
|
+
this.session.modelRegistry.refresh();
|
|
3409
|
+
try {
|
|
3410
|
+
return await this.session.modelRegistry.getAvailable();
|
|
3411
|
+
} catch {
|
|
3412
|
+
return [];
|
|
3413
|
+
}
|
|
3414
|
+
}
|
|
3415
|
+
|
|
3416
|
+
/** Update the footer's available provider count from current model candidates */
|
|
3417
|
+
private async updateAvailableProviderCount(): Promise<void> {
|
|
3418
|
+
const models = await this.getModelCandidates();
|
|
3419
|
+
const uniqueProviders = new Set(models.map((m) => m.provider));
|
|
3420
|
+
this.footerDataProvider.setAvailableProviderCount(uniqueProviders.size);
|
|
3421
|
+
}
|
|
3422
|
+
|
|
3423
|
+
private showModelSelector(initialSearchInput?: string): void {
|
|
3424
|
+
this.showSelector((done) => {
|
|
3425
|
+
const selector = new ModelSelectorComponent(
|
|
3426
|
+
this.ui,
|
|
3427
|
+
this.session.model,
|
|
3428
|
+
this.settingsManager,
|
|
3429
|
+
this.session.modelRegistry,
|
|
3430
|
+
this.session.scopedModels,
|
|
3431
|
+
async (model) => {
|
|
3432
|
+
try {
|
|
3433
|
+
await this.session.setModel(model);
|
|
3434
|
+
this.footer.invalidate();
|
|
3435
|
+
this.updateEditorBorderColor();
|
|
3436
|
+
done();
|
|
3437
|
+
this.showStatus(`Model: ${model.id}`);
|
|
3438
|
+
} catch (error) {
|
|
3439
|
+
done();
|
|
3440
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
3441
|
+
}
|
|
3442
|
+
},
|
|
3443
|
+
() => {
|
|
3444
|
+
done();
|
|
3445
|
+
this.ui.requestRender();
|
|
3446
|
+
},
|
|
3447
|
+
initialSearchInput,
|
|
3448
|
+
);
|
|
3449
|
+
return { component: selector, focus: selector };
|
|
3450
|
+
});
|
|
3451
|
+
}
|
|
3452
|
+
|
|
3453
|
+
private async showModelsSelector(): Promise<void> {
|
|
3454
|
+
// Get all available models
|
|
3455
|
+
this.session.modelRegistry.refresh();
|
|
3456
|
+
const allModels = this.session.modelRegistry.getAvailable();
|
|
3457
|
+
|
|
3458
|
+
if (allModels.length === 0) {
|
|
3459
|
+
this.showStatus("No models available");
|
|
3460
|
+
return;
|
|
3461
|
+
}
|
|
3462
|
+
|
|
3463
|
+
// Check if session has scoped models (from previous session-only changes or CLI --models)
|
|
3464
|
+
const sessionScopedModels = this.session.scopedModels;
|
|
3465
|
+
const hasSessionScope = sessionScopedModels.length > 0;
|
|
3466
|
+
|
|
3467
|
+
// Build enabled model IDs from session state or settings
|
|
3468
|
+
const enabledModelIds = new Set<string>();
|
|
3469
|
+
let hasFilter = false;
|
|
3470
|
+
|
|
3471
|
+
if (hasSessionScope) {
|
|
3472
|
+
// Use current session's scoped models
|
|
3473
|
+
for (const sm of sessionScopedModels) {
|
|
3474
|
+
enabledModelIds.add(`${sm.model.provider}/${sm.model.id}`);
|
|
3475
|
+
}
|
|
3476
|
+
hasFilter = true;
|
|
3477
|
+
} else {
|
|
3478
|
+
// Fall back to settings
|
|
3479
|
+
const patterns = this.settingsManager.getEnabledModels();
|
|
3480
|
+
if (patterns !== undefined && patterns.length > 0) {
|
|
3481
|
+
hasFilter = true;
|
|
3482
|
+
const scopedModels = await resolveModelScope(patterns, this.session.modelRegistry);
|
|
3483
|
+
for (const sm of scopedModels) {
|
|
3484
|
+
enabledModelIds.add(`${sm.model.provider}/${sm.model.id}`);
|
|
3485
|
+
}
|
|
3486
|
+
}
|
|
3487
|
+
}
|
|
3488
|
+
|
|
3489
|
+
// Track current enabled state (session-only until persisted)
|
|
3490
|
+
const currentEnabledIds = new Set(enabledModelIds);
|
|
3491
|
+
let currentHasFilter = hasFilter;
|
|
3492
|
+
|
|
3493
|
+
// Helper to update session's scoped models (session-only, no persist)
|
|
3494
|
+
const updateSessionModels = async (enabledIds: Set<string>) => {
|
|
3495
|
+
if (enabledIds.size > 0 && enabledIds.size < allModels.length) {
|
|
3496
|
+
const newScopedModels = await resolveModelScope(Array.from(enabledIds), this.session.modelRegistry);
|
|
3497
|
+
this.session.setScopedModels(
|
|
3498
|
+
newScopedModels.map((sm) => ({
|
|
3499
|
+
model: sm.model,
|
|
3500
|
+
thinkingLevel: sm.thinkingLevel,
|
|
3501
|
+
})),
|
|
3502
|
+
);
|
|
3503
|
+
} else {
|
|
3504
|
+
// All enabled or none enabled = no filter
|
|
3505
|
+
this.session.setScopedModels([]);
|
|
3506
|
+
}
|
|
3507
|
+
await this.updateAvailableProviderCount();
|
|
3508
|
+
this.ui.requestRender();
|
|
3509
|
+
};
|
|
3510
|
+
|
|
3511
|
+
this.showSelector((done) => {
|
|
3512
|
+
const selector = new ScopedModelsSelectorComponent(
|
|
3513
|
+
{
|
|
3514
|
+
allModels,
|
|
3515
|
+
enabledModelIds: currentEnabledIds,
|
|
3516
|
+
hasEnabledModelsFilter: currentHasFilter,
|
|
3517
|
+
},
|
|
3518
|
+
{
|
|
3519
|
+
onModelToggle: async (modelId, enabled) => {
|
|
3520
|
+
if (enabled) {
|
|
3521
|
+
currentEnabledIds.add(modelId);
|
|
3522
|
+
} else {
|
|
3523
|
+
currentEnabledIds.delete(modelId);
|
|
3524
|
+
}
|
|
3525
|
+
currentHasFilter = true;
|
|
3526
|
+
await updateSessionModels(currentEnabledIds);
|
|
3527
|
+
},
|
|
3528
|
+
onEnableAll: async (allModelIds) => {
|
|
3529
|
+
currentEnabledIds.clear();
|
|
3530
|
+
for (const id of allModelIds) {
|
|
3531
|
+
currentEnabledIds.add(id);
|
|
3532
|
+
}
|
|
3533
|
+
currentHasFilter = false;
|
|
3534
|
+
await updateSessionModels(currentEnabledIds);
|
|
3535
|
+
},
|
|
3536
|
+
onClearAll: async () => {
|
|
3537
|
+
currentEnabledIds.clear();
|
|
3538
|
+
currentHasFilter = true;
|
|
3539
|
+
await updateSessionModels(currentEnabledIds);
|
|
3540
|
+
},
|
|
3541
|
+
onToggleProvider: async (_provider, modelIds, enabled) => {
|
|
3542
|
+
for (const id of modelIds) {
|
|
3543
|
+
if (enabled) {
|
|
3544
|
+
currentEnabledIds.add(id);
|
|
3545
|
+
} else {
|
|
3546
|
+
currentEnabledIds.delete(id);
|
|
3547
|
+
}
|
|
3548
|
+
}
|
|
3549
|
+
currentHasFilter = true;
|
|
3550
|
+
await updateSessionModels(currentEnabledIds);
|
|
3551
|
+
},
|
|
3552
|
+
onPersist: (enabledIds) => {
|
|
3553
|
+
// Persist to settings
|
|
3554
|
+
const newPatterns =
|
|
3555
|
+
enabledIds.length === allModels.length
|
|
3556
|
+
? undefined // All enabled = clear filter
|
|
3557
|
+
: enabledIds;
|
|
3558
|
+
this.settingsManager.setEnabledModels(newPatterns);
|
|
3559
|
+
this.showStatus("Model selection saved to settings");
|
|
3560
|
+
},
|
|
3561
|
+
onCancel: () => {
|
|
3562
|
+
done();
|
|
3563
|
+
this.ui.requestRender();
|
|
3564
|
+
},
|
|
3565
|
+
},
|
|
3566
|
+
);
|
|
3567
|
+
return { component: selector, focus: selector };
|
|
3568
|
+
});
|
|
3569
|
+
}
|
|
3570
|
+
|
|
3571
|
+
private showUserMessageSelector(): void {
|
|
3572
|
+
const userMessages = this.session.getUserMessagesForForking();
|
|
3573
|
+
|
|
3574
|
+
if (userMessages.length === 0) {
|
|
3575
|
+
this.showStatus("No messages to fork from");
|
|
3576
|
+
return;
|
|
3577
|
+
}
|
|
3578
|
+
|
|
3579
|
+
this.showSelector((done) => {
|
|
3580
|
+
const selector = new UserMessageSelectorComponent(
|
|
3581
|
+
userMessages.map((m) => ({ id: m.entryId, text: m.text })),
|
|
3582
|
+
async (entryId) => {
|
|
3583
|
+
const result = await this.session.fork(entryId);
|
|
3584
|
+
if (result.cancelled) {
|
|
3585
|
+
// Extension cancelled the fork
|
|
3586
|
+
done();
|
|
3587
|
+
this.ui.requestRender();
|
|
3588
|
+
return;
|
|
3589
|
+
}
|
|
3590
|
+
|
|
3591
|
+
this.chatContainer.clear();
|
|
3592
|
+
this.renderInitialMessages();
|
|
3593
|
+
this.editor.setText(result.selectedText);
|
|
3594
|
+
done();
|
|
3595
|
+
this.showStatus("Branched to new session");
|
|
3596
|
+
},
|
|
3597
|
+
() => {
|
|
3598
|
+
done();
|
|
3599
|
+
this.ui.requestRender();
|
|
3600
|
+
},
|
|
3601
|
+
);
|
|
3602
|
+
return { component: selector, focus: selector.getMessageList() };
|
|
3603
|
+
});
|
|
3604
|
+
}
|
|
3605
|
+
|
|
3606
|
+
private showTreeSelector(initialSelectedId?: string): void {
|
|
3607
|
+
const tree = this.sessionManager.getTree();
|
|
3608
|
+
const realLeafId = this.sessionManager.getLeafId();
|
|
3609
|
+
const initialFilterMode = this.settingsManager.getTreeFilterMode();
|
|
3610
|
+
|
|
3611
|
+
if (tree.length === 0) {
|
|
3612
|
+
this.showStatus("No entries in session");
|
|
3613
|
+
return;
|
|
3614
|
+
}
|
|
3615
|
+
|
|
3616
|
+
this.showSelector((done) => {
|
|
3617
|
+
const selector = new TreeSelectorComponent(
|
|
3618
|
+
tree,
|
|
3619
|
+
realLeafId,
|
|
3620
|
+
this.ui.terminal.rows,
|
|
3621
|
+
async (entryId) => {
|
|
3622
|
+
// Selecting the current leaf is a no-op (already there)
|
|
3623
|
+
if (entryId === realLeafId) {
|
|
3624
|
+
done();
|
|
3625
|
+
this.showStatus("Already at this point");
|
|
3626
|
+
return;
|
|
3627
|
+
}
|
|
3628
|
+
|
|
3629
|
+
// Ask about summarization
|
|
3630
|
+
done(); // Close selector first
|
|
3631
|
+
|
|
3632
|
+
// Loop until user makes a complete choice or cancels to tree
|
|
3633
|
+
let wantsSummary = false;
|
|
3634
|
+
let customInstructions: string | undefined;
|
|
3635
|
+
|
|
3636
|
+
// Check if we should skip the prompt (user preference to always default to no summary)
|
|
3637
|
+
if (!this.settingsManager.getBranchSummarySkipPrompt()) {
|
|
3638
|
+
while (true) {
|
|
3639
|
+
const summaryChoice = await this.showExtensionSelector("Summarize branch?", [
|
|
3640
|
+
"No summary",
|
|
3641
|
+
"Summarize",
|
|
3642
|
+
"Summarize with custom prompt",
|
|
3643
|
+
]);
|
|
3644
|
+
|
|
3645
|
+
if (summaryChoice === undefined) {
|
|
3646
|
+
// User pressed escape - re-show tree selector with same selection
|
|
3647
|
+
this.showTreeSelector(entryId);
|
|
3648
|
+
return;
|
|
3649
|
+
}
|
|
3650
|
+
|
|
3651
|
+
wantsSummary = summaryChoice !== "No summary";
|
|
3652
|
+
|
|
3653
|
+
if (summaryChoice === "Summarize with custom prompt") {
|
|
3654
|
+
customInstructions = await this.showExtensionEditor("Custom summarization instructions");
|
|
3655
|
+
if (customInstructions === undefined) {
|
|
3656
|
+
// User cancelled - loop back to summary selector
|
|
3657
|
+
continue;
|
|
3658
|
+
}
|
|
3659
|
+
}
|
|
3660
|
+
|
|
3661
|
+
// User made a complete choice
|
|
3662
|
+
break;
|
|
3663
|
+
}
|
|
3664
|
+
}
|
|
3665
|
+
|
|
3666
|
+
// Set up escape handler and loader if summarizing
|
|
3667
|
+
let summaryLoader: Loader | undefined;
|
|
3668
|
+
const originalOnEscape = this.defaultEditor.onEscape;
|
|
3669
|
+
|
|
3670
|
+
if (wantsSummary) {
|
|
3671
|
+
this.defaultEditor.onEscape = () => {
|
|
3672
|
+
this.session.abortBranchSummary();
|
|
3673
|
+
};
|
|
3674
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3675
|
+
summaryLoader = new Loader(
|
|
3676
|
+
this.ui,
|
|
3677
|
+
(spinner) => theme.fg("accent", spinner),
|
|
3678
|
+
(text) => theme.fg("muted", text),
|
|
3679
|
+
`Summarizing branch... (${keyText("app.interrupt")} to cancel)`,
|
|
3680
|
+
);
|
|
3681
|
+
this.statusContainer.addChild(summaryLoader);
|
|
3682
|
+
this.ui.requestRender();
|
|
3683
|
+
}
|
|
3684
|
+
|
|
3685
|
+
try {
|
|
3686
|
+
const result = await this.session.navigateTree(entryId, {
|
|
3687
|
+
summarize: wantsSummary,
|
|
3688
|
+
customInstructions,
|
|
3689
|
+
});
|
|
3690
|
+
|
|
3691
|
+
if (result.aborted) {
|
|
3692
|
+
// Summarization aborted - re-show tree selector with same selection
|
|
3693
|
+
this.showStatus("Branch summarization cancelled");
|
|
3694
|
+
this.showTreeSelector(entryId);
|
|
3695
|
+
return;
|
|
3696
|
+
}
|
|
3697
|
+
if (result.cancelled) {
|
|
3698
|
+
this.showStatus("Navigation cancelled");
|
|
3699
|
+
return;
|
|
3700
|
+
}
|
|
3701
|
+
|
|
3702
|
+
// Update UI
|
|
3703
|
+
this.chatContainer.clear();
|
|
3704
|
+
this.renderInitialMessages();
|
|
3705
|
+
if (result.editorText && !this.editor.getText().trim()) {
|
|
3706
|
+
this.editor.setText(result.editorText);
|
|
3707
|
+
}
|
|
3708
|
+
this.showStatus("Navigated to selected point");
|
|
3709
|
+
} catch (error) {
|
|
3710
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
3711
|
+
} finally {
|
|
3712
|
+
if (summaryLoader) {
|
|
3713
|
+
summaryLoader.stop();
|
|
3714
|
+
this.statusContainer.clear();
|
|
3715
|
+
}
|
|
3716
|
+
this.defaultEditor.onEscape = originalOnEscape;
|
|
3717
|
+
}
|
|
3718
|
+
},
|
|
3719
|
+
() => {
|
|
3720
|
+
done();
|
|
3721
|
+
this.ui.requestRender();
|
|
3722
|
+
},
|
|
3723
|
+
(entryId, label) => {
|
|
3724
|
+
this.sessionManager.appendLabelChange(entryId, label);
|
|
3725
|
+
this.ui.requestRender();
|
|
3726
|
+
},
|
|
3727
|
+
initialSelectedId,
|
|
3728
|
+
initialFilterMode,
|
|
3729
|
+
);
|
|
3730
|
+
return { component: selector, focus: selector };
|
|
3731
|
+
});
|
|
3732
|
+
}
|
|
3733
|
+
|
|
3734
|
+
private showSessionSelector(): void {
|
|
3735
|
+
this.showSelector((done) => {
|
|
3736
|
+
const selector = new SessionSelectorComponent(
|
|
3737
|
+
(onProgress) =>
|
|
3738
|
+
SessionManager.list(this.sessionManager.getCwd(), this.sessionManager.getSessionDir(), onProgress),
|
|
3739
|
+
SessionManager.listAll,
|
|
3740
|
+
async (sessionPath) => {
|
|
3741
|
+
done();
|
|
3742
|
+
await this.handleResumeSession(sessionPath);
|
|
3743
|
+
},
|
|
3744
|
+
() => {
|
|
3745
|
+
done();
|
|
3746
|
+
this.ui.requestRender();
|
|
3747
|
+
},
|
|
3748
|
+
() => {
|
|
3749
|
+
void this.shutdown();
|
|
3750
|
+
},
|
|
3751
|
+
() => this.ui.requestRender(),
|
|
3752
|
+
{
|
|
3753
|
+
renameSession: async (sessionFilePath: string, nextName: string | undefined) => {
|
|
3754
|
+
const next = (nextName ?? "").trim();
|
|
3755
|
+
if (!next) return;
|
|
3756
|
+
const mgr = SessionManager.open(sessionFilePath);
|
|
3757
|
+
mgr.appendSessionInfo(next);
|
|
3758
|
+
},
|
|
3759
|
+
showRenameHint: true,
|
|
3760
|
+
keybindings: this.keybindings,
|
|
3761
|
+
},
|
|
3762
|
+
|
|
3763
|
+
this.sessionManager.getSessionFile(),
|
|
3764
|
+
);
|
|
3765
|
+
return { component: selector, focus: selector };
|
|
3766
|
+
});
|
|
3767
|
+
}
|
|
3768
|
+
|
|
3769
|
+
private async handleResumeSession(sessionPath: string): Promise<void> {
|
|
3770
|
+
// Stop loading animation
|
|
3771
|
+
if (this.loadingAnimation) {
|
|
3772
|
+
this.loadingAnimation.stop();
|
|
3773
|
+
this.loadingAnimation = undefined;
|
|
3774
|
+
}
|
|
3775
|
+
this.statusContainer.clear();
|
|
3776
|
+
|
|
3777
|
+
// Clear UI state
|
|
3778
|
+
this.pendingMessagesContainer.clear();
|
|
3779
|
+
this.compactionQueuedMessages = [];
|
|
3780
|
+
this.streamingComponent = undefined;
|
|
3781
|
+
this.streamingMessage = undefined;
|
|
3782
|
+
this.pendingTools.clear();
|
|
3783
|
+
|
|
3784
|
+
// Switch session via AgentSession (emits extension session events)
|
|
3785
|
+
await this.session.switchSession(sessionPath);
|
|
3786
|
+
|
|
3787
|
+
// Clear and re-render the chat
|
|
3788
|
+
this.chatContainer.clear();
|
|
3789
|
+
this.renderInitialMessages();
|
|
3790
|
+
this.showStatus("Resumed session");
|
|
3791
|
+
}
|
|
3792
|
+
|
|
3793
|
+
private async showOAuthSelector(mode: "login" | "logout"): Promise<void> {
|
|
3794
|
+
if (mode === "logout") {
|
|
3795
|
+
const providers = this.session.modelRegistry.authStorage.list();
|
|
3796
|
+
const loggedInProviders = providers.filter(
|
|
3797
|
+
(p) => this.session.modelRegistry.authStorage.get(p)?.type === "oauth",
|
|
3798
|
+
);
|
|
3799
|
+
if (loggedInProviders.length === 0) {
|
|
3800
|
+
this.showStatus("No OAuth providers logged in. Use /login first.");
|
|
3801
|
+
return;
|
|
3802
|
+
}
|
|
3803
|
+
}
|
|
3804
|
+
|
|
3805
|
+
this.showSelector((done) => {
|
|
3806
|
+
const selector = new OAuthSelectorComponent(
|
|
3807
|
+
mode,
|
|
3808
|
+
this.session.modelRegistry.authStorage,
|
|
3809
|
+
async (providerId: string) => {
|
|
3810
|
+
done();
|
|
3811
|
+
|
|
3812
|
+
if (mode === "login") {
|
|
3813
|
+
await this.showLoginDialog(providerId);
|
|
3814
|
+
} else {
|
|
3815
|
+
// Logout flow
|
|
3816
|
+
const providerInfo = this.session.modelRegistry.authStorage
|
|
3817
|
+
.getOAuthProviders()
|
|
3818
|
+
.find((p) => p.id === providerId);
|
|
3819
|
+
const providerName = providerInfo?.name || providerId;
|
|
3820
|
+
|
|
3821
|
+
try {
|
|
3822
|
+
this.session.modelRegistry.authStorage.logout(providerId);
|
|
3823
|
+
this.session.modelRegistry.refresh();
|
|
3824
|
+
await this.updateAvailableProviderCount();
|
|
3825
|
+
this.showStatus(`Logged out of ${providerName}`);
|
|
3826
|
+
} catch (error: unknown) {
|
|
3827
|
+
this.showError(`Logout failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
3828
|
+
}
|
|
3829
|
+
}
|
|
3830
|
+
},
|
|
3831
|
+
() => {
|
|
3832
|
+
done();
|
|
3833
|
+
this.ui.requestRender();
|
|
3834
|
+
},
|
|
3835
|
+
);
|
|
3836
|
+
return { component: selector, focus: selector };
|
|
3837
|
+
});
|
|
3838
|
+
}
|
|
3839
|
+
|
|
3840
|
+
private async showLoginDialog(providerId: string): Promise<void> {
|
|
3841
|
+
const providerInfo = this.session.modelRegistry.authStorage.getOAuthProviders().find((p) => p.id === providerId);
|
|
3842
|
+
const providerName = providerInfo?.name || providerId;
|
|
3843
|
+
|
|
3844
|
+
// Providers that use callback servers (can paste redirect URL)
|
|
3845
|
+
const usesCallbackServer = providerInfo?.usesCallbackServer ?? false;
|
|
3846
|
+
|
|
3847
|
+
// Create login dialog component
|
|
3848
|
+
const dialog = new LoginDialogComponent(this.ui, providerId, (_success, _message) => {
|
|
3849
|
+
// Completion handled below
|
|
3850
|
+
});
|
|
3851
|
+
|
|
3852
|
+
// Show dialog in editor container
|
|
3853
|
+
this.editorContainer.clear();
|
|
3854
|
+
this.editorContainer.addChild(dialog);
|
|
3855
|
+
this.ui.setFocus(dialog);
|
|
3856
|
+
this.ui.requestRender();
|
|
3857
|
+
|
|
3858
|
+
// Promise for manual code input (racing with callback server)
|
|
3859
|
+
let manualCodeResolve: ((code: string) => void) | undefined;
|
|
3860
|
+
let manualCodeReject: ((err: Error) => void) | undefined;
|
|
3861
|
+
const manualCodePromise = new Promise<string>((resolve, reject) => {
|
|
3862
|
+
manualCodeResolve = resolve;
|
|
3863
|
+
manualCodeReject = reject;
|
|
3864
|
+
});
|
|
3865
|
+
|
|
3866
|
+
// Restore editor helper
|
|
3867
|
+
const restoreEditor = () => {
|
|
3868
|
+
this.editorContainer.clear();
|
|
3869
|
+
this.editorContainer.addChild(this.editor);
|
|
3870
|
+
this.ui.setFocus(this.editor);
|
|
3871
|
+
this.ui.requestRender();
|
|
3872
|
+
};
|
|
3873
|
+
|
|
3874
|
+
try {
|
|
3875
|
+
await this.session.modelRegistry.authStorage.login(providerId as OAuthProviderId, {
|
|
3876
|
+
onAuth: (info: { url: string; instructions?: string }) => {
|
|
3877
|
+
dialog.showAuth(info.url, info.instructions);
|
|
3878
|
+
|
|
3879
|
+
if (usesCallbackServer) {
|
|
3880
|
+
// Show input for manual paste, racing with callback
|
|
3881
|
+
dialog
|
|
3882
|
+
.showManualInput("Paste redirect URL below, or complete login in browser:")
|
|
3883
|
+
.then((value) => {
|
|
3884
|
+
if (value && manualCodeResolve) {
|
|
3885
|
+
manualCodeResolve(value);
|
|
3886
|
+
manualCodeResolve = undefined;
|
|
3887
|
+
}
|
|
3888
|
+
})
|
|
3889
|
+
.catch(() => {
|
|
3890
|
+
if (manualCodeReject) {
|
|
3891
|
+
manualCodeReject(new Error("Login cancelled"));
|
|
3892
|
+
manualCodeReject = undefined;
|
|
3893
|
+
}
|
|
3894
|
+
});
|
|
3895
|
+
} else if (providerId === "github-copilot") {
|
|
3896
|
+
// GitHub Copilot polls after onAuth
|
|
3897
|
+
dialog.showWaiting("Waiting for browser authentication...");
|
|
3898
|
+
}
|
|
3899
|
+
// For Anthropic: onPrompt is called immediately after
|
|
3900
|
+
},
|
|
3901
|
+
|
|
3902
|
+
onPrompt: async (prompt: { message: string; placeholder?: string }) => {
|
|
3903
|
+
return dialog.showPrompt(prompt.message, prompt.placeholder);
|
|
3904
|
+
},
|
|
3905
|
+
|
|
3906
|
+
onProgress: (message: string) => {
|
|
3907
|
+
dialog.showProgress(message);
|
|
3908
|
+
},
|
|
3909
|
+
|
|
3910
|
+
onManualCodeInput: () => manualCodePromise,
|
|
3911
|
+
|
|
3912
|
+
signal: dialog.signal,
|
|
3913
|
+
});
|
|
3914
|
+
|
|
3915
|
+
// Success
|
|
3916
|
+
restoreEditor();
|
|
3917
|
+
this.session.modelRegistry.refresh();
|
|
3918
|
+
await this.updateAvailableProviderCount();
|
|
3919
|
+
this.showStatus(`Logged in to ${providerName}. Credentials saved to ${getAuthPath()}`);
|
|
3920
|
+
} catch (error: unknown) {
|
|
3921
|
+
restoreEditor();
|
|
3922
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
3923
|
+
if (errorMsg !== "Login cancelled") {
|
|
3924
|
+
this.showError(`Failed to login to ${providerName}: ${errorMsg}`);
|
|
3925
|
+
}
|
|
3926
|
+
}
|
|
3927
|
+
}
|
|
3928
|
+
|
|
3929
|
+
// =========================================================================
|
|
3930
|
+
// Command handlers
|
|
3931
|
+
// =========================================================================
|
|
3932
|
+
|
|
3933
|
+
private async handleReloadCommand(): Promise<void> {
|
|
3934
|
+
if (this.session.isStreaming) {
|
|
3935
|
+
this.showWarning("Wait for the current response to finish before reloading.");
|
|
3936
|
+
return;
|
|
3937
|
+
}
|
|
3938
|
+
if (this.session.isCompacting) {
|
|
3939
|
+
this.showWarning("Wait for compaction to finish before reloading.");
|
|
3940
|
+
return;
|
|
3941
|
+
}
|
|
3942
|
+
|
|
3943
|
+
this.resetExtensionUI();
|
|
3944
|
+
|
|
3945
|
+
const loader = new BorderedLoader(
|
|
3946
|
+
this.ui,
|
|
3947
|
+
theme,
|
|
3948
|
+
"Reloading keybindings, extensions, skills, prompts, themes...",
|
|
3949
|
+
{
|
|
3950
|
+
cancellable: false,
|
|
3951
|
+
},
|
|
3952
|
+
);
|
|
3953
|
+
const previousEditor = this.editor;
|
|
3954
|
+
this.editorContainer.clear();
|
|
3955
|
+
this.editorContainer.addChild(loader);
|
|
3956
|
+
this.ui.setFocus(loader);
|
|
3957
|
+
this.ui.requestRender();
|
|
3958
|
+
|
|
3959
|
+
const dismissLoader = (editor: Component) => {
|
|
3960
|
+
loader.dispose();
|
|
3961
|
+
this.editorContainer.clear();
|
|
3962
|
+
this.editorContainer.addChild(editor);
|
|
3963
|
+
this.ui.setFocus(editor);
|
|
3964
|
+
this.ui.requestRender();
|
|
3965
|
+
};
|
|
3966
|
+
|
|
3967
|
+
try {
|
|
3968
|
+
await this.session.reload();
|
|
3969
|
+
this.keybindings.reload();
|
|
3970
|
+
setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
|
|
3971
|
+
this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
|
|
3972
|
+
const themeName = this.settingsManager.getTheme();
|
|
3973
|
+
const themeResult = themeName ? setTheme(themeName, true) : { success: true };
|
|
3974
|
+
if (!themeResult.success) {
|
|
3975
|
+
this.showError(`Failed to load theme "${themeName}": ${themeResult.error}\nFell back to dark theme.`);
|
|
3976
|
+
}
|
|
3977
|
+
const editorPaddingX = this.settingsManager.getEditorPaddingX();
|
|
3978
|
+
const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
|
|
3979
|
+
this.defaultEditor.setPaddingX(editorPaddingX);
|
|
3980
|
+
this.defaultEditor.setAutocompleteMaxVisible(autocompleteMaxVisible);
|
|
3981
|
+
if (this.editor !== this.defaultEditor) {
|
|
3982
|
+
this.editor.setPaddingX?.(editorPaddingX);
|
|
3983
|
+
this.editor.setAutocompleteMaxVisible?.(autocompleteMaxVisible);
|
|
3984
|
+
}
|
|
3985
|
+
this.ui.setShowHardwareCursor(this.settingsManager.getShowHardwareCursor());
|
|
3986
|
+
this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
|
|
3987
|
+
this.setupAutocomplete(this.fdPath);
|
|
3988
|
+
const runner = this.session.extensionRunner;
|
|
3989
|
+
if (runner) {
|
|
3990
|
+
this.setupExtensionShortcuts(runner);
|
|
3991
|
+
}
|
|
3992
|
+
this.rebuildChatFromMessages();
|
|
3993
|
+
dismissLoader(this.editor as Component);
|
|
3994
|
+
this.showLoadedResources({
|
|
3995
|
+
force: false,
|
|
3996
|
+
showDiagnosticsWhenQuiet: true,
|
|
3997
|
+
});
|
|
3998
|
+
const modelsJsonError = this.session.modelRegistry.getError();
|
|
3999
|
+
if (modelsJsonError) {
|
|
4000
|
+
this.showError(`models.json error: ${modelsJsonError}`);
|
|
4001
|
+
}
|
|
4002
|
+
this.showStatus("Reloaded keybindings, extensions, skills, prompts, themes");
|
|
4003
|
+
} catch (error) {
|
|
4004
|
+
dismissLoader(previousEditor as Component);
|
|
4005
|
+
this.showError(`Reload failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
4006
|
+
}
|
|
4007
|
+
}
|
|
4008
|
+
|
|
4009
|
+
private async handleExportCommand(text: string): Promise<void> {
|
|
4010
|
+
const parts = text.split(/\s+/);
|
|
4011
|
+
const outputPath = parts.length > 1 ? parts[1] : undefined;
|
|
4012
|
+
|
|
4013
|
+
try {
|
|
4014
|
+
if (outputPath?.endsWith(".jsonl")) {
|
|
4015
|
+
const filePath = this.session.exportToJsonl(outputPath);
|
|
4016
|
+
this.showStatus(`Session exported to: ${filePath}`);
|
|
4017
|
+
} else {
|
|
4018
|
+
const filePath = await this.session.exportToHtml(outputPath);
|
|
4019
|
+
this.showStatus(`Session exported to: ${filePath}`);
|
|
4020
|
+
}
|
|
4021
|
+
} catch (error: unknown) {
|
|
4022
|
+
this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
4023
|
+
}
|
|
4024
|
+
}
|
|
4025
|
+
|
|
4026
|
+
private async handleImportCommand(text: string): Promise<void> {
|
|
4027
|
+
const parts = text.split(/\s+/);
|
|
4028
|
+
if (parts.length < 2 || !parts[1]) {
|
|
4029
|
+
this.showError("Usage: /import <path.jsonl>");
|
|
4030
|
+
return;
|
|
4031
|
+
}
|
|
4032
|
+
const inputPath = parts[1];
|
|
4033
|
+
|
|
4034
|
+
const confirmed = await this.showExtensionConfirm("Import session", `Replace current session with ${inputPath}?`);
|
|
4035
|
+
if (!confirmed) {
|
|
4036
|
+
this.showStatus("Import cancelled");
|
|
4037
|
+
return;
|
|
4038
|
+
}
|
|
4039
|
+
|
|
4040
|
+
try {
|
|
4041
|
+
// Stop loading animation
|
|
4042
|
+
if (this.loadingAnimation) {
|
|
4043
|
+
this.loadingAnimation.stop();
|
|
4044
|
+
this.loadingAnimation = undefined;
|
|
4045
|
+
}
|
|
4046
|
+
this.statusContainer.clear();
|
|
4047
|
+
|
|
4048
|
+
// Clear UI state
|
|
4049
|
+
this.pendingMessagesContainer.clear();
|
|
4050
|
+
this.compactionQueuedMessages = [];
|
|
4051
|
+
this.streamingComponent = undefined;
|
|
4052
|
+
this.streamingMessage = undefined;
|
|
4053
|
+
this.pendingTools.clear();
|
|
4054
|
+
|
|
4055
|
+
const success = await this.session.importFromJsonl(inputPath);
|
|
4056
|
+
if (!success) {
|
|
4057
|
+
this.showWarning("Import cancelled");
|
|
4058
|
+
return;
|
|
4059
|
+
}
|
|
4060
|
+
|
|
4061
|
+
// Clear and re-render the chat
|
|
4062
|
+
this.chatContainer.clear();
|
|
4063
|
+
this.renderInitialMessages();
|
|
4064
|
+
this.showStatus(`Session imported from: ${inputPath}`);
|
|
4065
|
+
} catch (error: unknown) {
|
|
4066
|
+
this.showError(`Failed to import session: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
4067
|
+
}
|
|
4068
|
+
}
|
|
4069
|
+
|
|
4070
|
+
private async handleCopyCommand(): Promise<void> {
|
|
4071
|
+
const text = this.session.getLastAssistantText();
|
|
4072
|
+
if (!text) {
|
|
4073
|
+
this.showError("No agent messages to copy yet.");
|
|
4074
|
+
return;
|
|
4075
|
+
}
|
|
4076
|
+
|
|
4077
|
+
try {
|
|
4078
|
+
await copyToClipboard(text);
|
|
4079
|
+
this.showStatus("Copied last agent message to clipboard");
|
|
4080
|
+
} catch (error) {
|
|
4081
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
4082
|
+
}
|
|
4083
|
+
}
|
|
4084
|
+
|
|
4085
|
+
private handleNameCommand(text: string): void {
|
|
4086
|
+
const name = text.replace(/^\/name\s*/, "").trim();
|
|
4087
|
+
if (!name) {
|
|
4088
|
+
const currentName = this.sessionManager.getSessionName();
|
|
4089
|
+
if (currentName) {
|
|
4090
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4091
|
+
this.chatContainer.addChild(new Text(theme.fg("dim", `Session name: ${currentName}`), 1, 0));
|
|
4092
|
+
} else {
|
|
4093
|
+
this.showWarning("Usage: /name <name>");
|
|
4094
|
+
}
|
|
4095
|
+
this.ui.requestRender();
|
|
4096
|
+
return;
|
|
4097
|
+
}
|
|
4098
|
+
|
|
4099
|
+
this.sessionManager.appendSessionInfo(name);
|
|
4100
|
+
this.updateTerminalTitle();
|
|
4101
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4102
|
+
this.chatContainer.addChild(new Text(theme.fg("dim", `Session name set: ${name}`), 1, 0));
|
|
4103
|
+
this.ui.requestRender();
|
|
4104
|
+
}
|
|
4105
|
+
|
|
4106
|
+
private handleSessionCommand(): void {
|
|
4107
|
+
const stats = this.session.getSessionStats();
|
|
4108
|
+
const sessionName = this.sessionManager.getSessionName();
|
|
4109
|
+
|
|
4110
|
+
let info = `${theme.bold("Session Info")}\n\n`;
|
|
4111
|
+
if (sessionName) {
|
|
4112
|
+
info += `${theme.fg("dim", "Name:")} ${sessionName}\n`;
|
|
4113
|
+
}
|
|
4114
|
+
info += `${theme.fg("dim", "File:")} ${stats.sessionFile ?? "In-memory"}\n`;
|
|
4115
|
+
info += `${theme.fg("dim", "ID:")} ${stats.sessionId}\n\n`;
|
|
4116
|
+
info += `${theme.bold("Messages")}\n`;
|
|
4117
|
+
info += `${theme.fg("dim", "User:")} ${stats.userMessages}\n`;
|
|
4118
|
+
info += `${theme.fg("dim", "Assistant:")} ${stats.assistantMessages}\n`;
|
|
4119
|
+
info += `${theme.fg("dim", "Tool Calls:")} ${stats.toolCalls}\n`;
|
|
4120
|
+
info += `${theme.fg("dim", "Tool Results:")} ${stats.toolResults}\n`;
|
|
4121
|
+
info += `${theme.fg("dim", "Total:")} ${stats.totalMessages}\n\n`;
|
|
4122
|
+
info += `${theme.bold("Tokens")}\n`;
|
|
4123
|
+
info += `${theme.fg("dim", "Input:")} ${stats.tokens.input.toLocaleString()}\n`;
|
|
4124
|
+
info += `${theme.fg("dim", "Output:")} ${stats.tokens.output.toLocaleString()}\n`;
|
|
4125
|
+
if (stats.tokens.cacheRead > 0) {
|
|
4126
|
+
info += `${theme.fg("dim", "Cache Read:")} ${stats.tokens.cacheRead.toLocaleString()}\n`;
|
|
4127
|
+
}
|
|
4128
|
+
if (stats.tokens.cacheWrite > 0) {
|
|
4129
|
+
info += `${theme.fg("dim", "Cache Write:")} ${stats.tokens.cacheWrite.toLocaleString()}\n`;
|
|
4130
|
+
}
|
|
4131
|
+
info += `${theme.fg("dim", "Total:")} ${stats.tokens.total.toLocaleString()}\n`;
|
|
4132
|
+
|
|
4133
|
+
if (stats.cost > 0) {
|
|
4134
|
+
info += `\n${theme.bold("Cost")}\n`;
|
|
4135
|
+
info += `${theme.fg("dim", "Total:")} ${stats.cost.toFixed(4)}`;
|
|
4136
|
+
}
|
|
4137
|
+
|
|
4138
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4139
|
+
this.chatContainer.addChild(new Text(info, 1, 0));
|
|
4140
|
+
this.ui.requestRender();
|
|
4141
|
+
}
|
|
4142
|
+
|
|
4143
|
+
private handleChangelogCommand(): void {
|
|
4144
|
+
const changelogPath = getChangelogPath();
|
|
4145
|
+
const allEntries = parseChangelog(changelogPath);
|
|
4146
|
+
|
|
4147
|
+
const changelogMarkdown =
|
|
4148
|
+
allEntries.length > 0
|
|
4149
|
+
? allEntries
|
|
4150
|
+
.reverse()
|
|
4151
|
+
.map((e) => e.content)
|
|
4152
|
+
.join("\n\n")
|
|
4153
|
+
: "No changelog entries found.";
|
|
4154
|
+
|
|
4155
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4156
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
4157
|
+
this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
|
|
4158
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4159
|
+
this.chatContainer.addChild(new Markdown(changelogMarkdown, 1, 1, this.getMarkdownThemeWithSettings()));
|
|
4160
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
4161
|
+
this.ui.requestRender();
|
|
4162
|
+
}
|
|
4163
|
+
|
|
4164
|
+
/**
|
|
4165
|
+
* Capitalize keybinding for display (e.g., "ctrl+c" -> "Ctrl+C").
|
|
4166
|
+
*/
|
|
4167
|
+
private capitalizeKey(key: string): string {
|
|
4168
|
+
return key
|
|
4169
|
+
.split("/")
|
|
4170
|
+
.map((k) =>
|
|
4171
|
+
k
|
|
4172
|
+
.split("+")
|
|
4173
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
4174
|
+
.join("+"),
|
|
4175
|
+
)
|
|
4176
|
+
.join("/");
|
|
4177
|
+
}
|
|
4178
|
+
|
|
4179
|
+
/**
|
|
4180
|
+
* Get capitalized display string for an app keybinding action.
|
|
4181
|
+
*/
|
|
4182
|
+
private getAppKeyDisplay(action: AppKeybinding): string {
|
|
4183
|
+
return this.capitalizeKey(keyText(action));
|
|
4184
|
+
}
|
|
4185
|
+
|
|
4186
|
+
/**
|
|
4187
|
+
* Get capitalized display string for an editor keybinding action.
|
|
4188
|
+
*/
|
|
4189
|
+
private getEditorKeyDisplay(action: Keybinding): string {
|
|
4190
|
+
return this.capitalizeKey(keyText(action));
|
|
4191
|
+
}
|
|
4192
|
+
|
|
4193
|
+
private handleHotkeysCommand(): void {
|
|
4194
|
+
// Navigation keybindings
|
|
4195
|
+
const cursorUp = this.getEditorKeyDisplay("tui.editor.cursorUp");
|
|
4196
|
+
const cursorDown = this.getEditorKeyDisplay("tui.editor.cursorDown");
|
|
4197
|
+
const cursorLeft = this.getEditorKeyDisplay("tui.editor.cursorLeft");
|
|
4198
|
+
const cursorRight = this.getEditorKeyDisplay("tui.editor.cursorRight");
|
|
4199
|
+
const cursorWordLeft = this.getEditorKeyDisplay("tui.editor.cursorWordLeft");
|
|
4200
|
+
const cursorWordRight = this.getEditorKeyDisplay("tui.editor.cursorWordRight");
|
|
4201
|
+
const cursorLineStart = this.getEditorKeyDisplay("tui.editor.cursorLineStart");
|
|
4202
|
+
const cursorLineEnd = this.getEditorKeyDisplay("tui.editor.cursorLineEnd");
|
|
4203
|
+
const jumpForward = this.getEditorKeyDisplay("tui.editor.jumpForward");
|
|
4204
|
+
const jumpBackward = this.getEditorKeyDisplay("tui.editor.jumpBackward");
|
|
4205
|
+
const pageUp = this.getEditorKeyDisplay("tui.editor.pageUp");
|
|
4206
|
+
const pageDown = this.getEditorKeyDisplay("tui.editor.pageDown");
|
|
4207
|
+
|
|
4208
|
+
// Editing keybindings
|
|
4209
|
+
const submit = this.getEditorKeyDisplay("tui.input.submit");
|
|
4210
|
+
const newLine = this.getEditorKeyDisplay("tui.input.newLine");
|
|
4211
|
+
const deleteWordBackward = this.getEditorKeyDisplay("tui.editor.deleteWordBackward");
|
|
4212
|
+
const deleteWordForward = this.getEditorKeyDisplay("tui.editor.deleteWordForward");
|
|
4213
|
+
const deleteToLineStart = this.getEditorKeyDisplay("tui.editor.deleteToLineStart");
|
|
4214
|
+
const deleteToLineEnd = this.getEditorKeyDisplay("tui.editor.deleteToLineEnd");
|
|
4215
|
+
const yank = this.getEditorKeyDisplay("tui.editor.yank");
|
|
4216
|
+
const yankPop = this.getEditorKeyDisplay("tui.editor.yankPop");
|
|
4217
|
+
const undo = this.getEditorKeyDisplay("tui.editor.undo");
|
|
4218
|
+
const tab = this.getEditorKeyDisplay("tui.input.tab");
|
|
4219
|
+
|
|
4220
|
+
// App keybindings
|
|
4221
|
+
const interrupt = this.getAppKeyDisplay("app.interrupt");
|
|
4222
|
+
const clear = this.getAppKeyDisplay("app.clear");
|
|
4223
|
+
const exit = this.getAppKeyDisplay("app.exit");
|
|
4224
|
+
const suspend = this.getAppKeyDisplay("app.suspend");
|
|
4225
|
+
const cycleThinkingLevel = this.getAppKeyDisplay("app.thinking.cycle");
|
|
4226
|
+
const cycleModelForward = this.getAppKeyDisplay("app.model.cycleForward");
|
|
4227
|
+
const selectModel = this.getAppKeyDisplay("app.model.select");
|
|
4228
|
+
const expandTools = this.getAppKeyDisplay("app.tools.expand");
|
|
4229
|
+
const toggleThinking = this.getAppKeyDisplay("app.thinking.toggle");
|
|
4230
|
+
const externalEditor = this.getAppKeyDisplay("app.editor.external");
|
|
4231
|
+
const cycleModelBackward = this.getAppKeyDisplay("app.model.cycleBackward");
|
|
4232
|
+
const followUp = this.getAppKeyDisplay("app.message.followUp");
|
|
4233
|
+
const dequeue = this.getAppKeyDisplay("app.message.dequeue");
|
|
4234
|
+
const pasteImage = this.getAppKeyDisplay("app.clipboard.pasteImage");
|
|
4235
|
+
|
|
4236
|
+
let hotkeys = `
|
|
4237
|
+
**Navigation**
|
|
4238
|
+
| Key | Action |
|
|
4239
|
+
|-----|--------|
|
|
4240
|
+
| \`${cursorUp}\` / \`${cursorDown}\` / \`${cursorLeft}\` / \`${cursorRight}\` | Move cursor / browse history (Up when empty) |
|
|
4241
|
+
| \`${cursorWordLeft}\` / \`${cursorWordRight}\` | Move by word |
|
|
4242
|
+
| \`${cursorLineStart}\` | Start of line |
|
|
4243
|
+
| \`${cursorLineEnd}\` | End of line |
|
|
4244
|
+
| \`${jumpForward}\` | Jump forward to character |
|
|
4245
|
+
| \`${jumpBackward}\` | Jump backward to character |
|
|
4246
|
+
| \`${pageUp}\` / \`${pageDown}\` | Scroll by page |
|
|
4247
|
+
|
|
4248
|
+
**Editing**
|
|
4249
|
+
| Key | Action |
|
|
4250
|
+
|-----|--------|
|
|
4251
|
+
| \`${submit}\` | Send message |
|
|
4252
|
+
| \`${newLine}\` | New line${process.platform === "win32" ? " (Ctrl+Enter on Windows Terminal)" : ""} |
|
|
4253
|
+
| \`${deleteWordBackward}\` | Delete word backwards |
|
|
4254
|
+
| \`${deleteWordForward}\` | Delete word forwards |
|
|
4255
|
+
| \`${deleteToLineStart}\` | Delete to start of line |
|
|
4256
|
+
| \`${deleteToLineEnd}\` | Delete to end of line |
|
|
4257
|
+
| \`${yank}\` | Paste the most-recently-deleted text |
|
|
4258
|
+
| \`${yankPop}\` | Cycle through the deleted text after pasting |
|
|
4259
|
+
| \`${undo}\` | Undo |
|
|
4260
|
+
|
|
4261
|
+
**Other**
|
|
4262
|
+
| Key | Action |
|
|
4263
|
+
|-----|--------|
|
|
4264
|
+
| \`${tab}\` | Path completion / accept autocomplete |
|
|
4265
|
+
| \`${interrupt}\` | Cancel autocomplete / abort streaming |
|
|
4266
|
+
| \`${clear}\` | Clear editor (first) / exit (second) |
|
|
4267
|
+
| \`${exit}\` | Exit (when editor is empty) |
|
|
4268
|
+
| \`${suspend}\` | Suspend to background |
|
|
4269
|
+
| \`${cycleThinkingLevel}\` | Cycle thinking level |
|
|
4270
|
+
| \`${cycleModelForward}\` / \`${cycleModelBackward}\` | Cycle models |
|
|
4271
|
+
| \`${selectModel}\` | Open model selector |
|
|
4272
|
+
| \`${expandTools}\` | Toggle tool output expansion |
|
|
4273
|
+
| \`${toggleThinking}\` | Toggle thinking block visibility |
|
|
4274
|
+
| \`${externalEditor}\` | Edit message in external editor |
|
|
4275
|
+
| \`${followUp}\` | Queue follow-up message |
|
|
4276
|
+
| \`${dequeue}\` | Restore queued messages |
|
|
4277
|
+
| \`${pasteImage}\` | Paste image from clipboard |
|
|
4278
|
+
| \`/\` | Slash commands |
|
|
4279
|
+
| \`!\` | Run bash command |
|
|
4280
|
+
| \`!!\` | Run bash command (excluded from context) |
|
|
4281
|
+
`;
|
|
4282
|
+
|
|
4283
|
+
// Add extension-registered shortcuts
|
|
4284
|
+
const extensionRunner = this.session.extensionRunner;
|
|
4285
|
+
if (extensionRunner) {
|
|
4286
|
+
const shortcuts = extensionRunner.getShortcuts(this.keybindings.getEffectiveConfig());
|
|
4287
|
+
if (shortcuts.size > 0) {
|
|
4288
|
+
hotkeys += `
|
|
4289
|
+
**Extensions**
|
|
4290
|
+
| Key | Action |
|
|
4291
|
+
|-----|--------|
|
|
4292
|
+
`;
|
|
4293
|
+
for (const [key, shortcut] of shortcuts) {
|
|
4294
|
+
const description = shortcut.description ?? shortcut.extensionPath;
|
|
4295
|
+
const keyDisplay = key.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
4296
|
+
hotkeys += `| \`${keyDisplay}\` | ${description} |\n`;
|
|
4297
|
+
}
|
|
4298
|
+
}
|
|
4299
|
+
}
|
|
4300
|
+
|
|
4301
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4302
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
4303
|
+
this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "Keyboard Shortcuts")), 1, 0));
|
|
4304
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4305
|
+
this.chatContainer.addChild(new Markdown(hotkeys.trim(), 1, 1, this.getMarkdownThemeWithSettings()));
|
|
4306
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
4307
|
+
this.ui.requestRender();
|
|
4308
|
+
}
|
|
4309
|
+
|
|
4310
|
+
private async handleClearCommand(): Promise<void> {
|
|
4311
|
+
// Stop loading animation
|
|
4312
|
+
if (this.loadingAnimation) {
|
|
4313
|
+
this.loadingAnimation.stop();
|
|
4314
|
+
this.loadingAnimation = undefined;
|
|
4315
|
+
}
|
|
4316
|
+
this.statusContainer.clear();
|
|
4317
|
+
|
|
4318
|
+
// New session via session (emits extension session events)
|
|
4319
|
+
await this.session.newSession();
|
|
4320
|
+
|
|
4321
|
+
// Clear UI state
|
|
4322
|
+
this.headerContainer.clear();
|
|
4323
|
+
this.chatContainer.clear();
|
|
4324
|
+
this.pendingMessagesContainer.clear();
|
|
4325
|
+
this.compactionQueuedMessages = [];
|
|
4326
|
+
this.streamingComponent = undefined;
|
|
4327
|
+
this.streamingMessage = undefined;
|
|
4328
|
+
this.pendingTools.clear();
|
|
4329
|
+
|
|
4330
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4331
|
+
this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
|
|
4332
|
+
this.ui.requestRender();
|
|
4333
|
+
}
|
|
4334
|
+
|
|
4335
|
+
private handleDebugCommand(): void {
|
|
4336
|
+
const width = this.ui.terminal.columns;
|
|
4337
|
+
const height = this.ui.terminal.rows;
|
|
4338
|
+
const allLines = this.ui.render(width);
|
|
4339
|
+
|
|
4340
|
+
const debugLogPath = getDebugLogPath();
|
|
4341
|
+
const debugData = [
|
|
4342
|
+
`Debug output at ${new Date().toISOString()}`,
|
|
4343
|
+
`Terminal: ${width}x${height}`,
|
|
4344
|
+
`Total lines: ${allLines.length}`,
|
|
4345
|
+
"",
|
|
4346
|
+
"=== All rendered lines with visible widths ===",
|
|
4347
|
+
...allLines.map((line, idx) => {
|
|
4348
|
+
const vw = visibleWidth(line);
|
|
4349
|
+
const escaped = JSON.stringify(line);
|
|
4350
|
+
return `[${idx}] (w=${vw}) ${escaped}`;
|
|
4351
|
+
}),
|
|
4352
|
+
"",
|
|
4353
|
+
"=== Agent messages (JSONL) ===",
|
|
4354
|
+
...this.session.messages.map((msg) => JSON.stringify(msg)),
|
|
4355
|
+
"",
|
|
4356
|
+
].join("\n");
|
|
4357
|
+
|
|
4358
|
+
fs.mkdirSync(path.dirname(debugLogPath), { recursive: true });
|
|
4359
|
+
fs.writeFileSync(debugLogPath, debugData);
|
|
4360
|
+
|
|
4361
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4362
|
+
this.chatContainer.addChild(
|
|
4363
|
+
new Text(`${theme.fg("accent", "✓ Debug log written")}\n${theme.fg("muted", debugLogPath)}`, 1, 1),
|
|
4364
|
+
);
|
|
4365
|
+
this.ui.requestRender();
|
|
4366
|
+
}
|
|
4367
|
+
|
|
4368
|
+
private handleArminSaysHi(): void {
|
|
4369
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
4370
|
+
this.chatContainer.addChild(new ArminComponent(this.ui));
|
|
4371
|
+
this.ui.requestRender();
|
|
4372
|
+
}
|
|
4373
|
+
|
|
4374
|
+
private async handleBashCommand(command: string, excludeFromContext = false): Promise<void> {
|
|
4375
|
+
const extensionRunner = this.session.extensionRunner;
|
|
4376
|
+
|
|
4377
|
+
// Emit user_bash event to let extensions intercept
|
|
4378
|
+
const eventResult = extensionRunner
|
|
4379
|
+
? await extensionRunner.emitUserBash({
|
|
4380
|
+
type: "user_bash",
|
|
4381
|
+
command,
|
|
4382
|
+
excludeFromContext,
|
|
4383
|
+
cwd: process.cwd(),
|
|
4384
|
+
})
|
|
4385
|
+
: undefined;
|
|
4386
|
+
|
|
4387
|
+
// If extension returned a full result, use it directly
|
|
4388
|
+
if (eventResult?.result) {
|
|
4389
|
+
const result = eventResult.result;
|
|
4390
|
+
|
|
4391
|
+
// Create UI component for display
|
|
4392
|
+
this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
|
|
4393
|
+
if (this.session.isStreaming) {
|
|
4394
|
+
this.pendingMessagesContainer.addChild(this.bashComponent);
|
|
4395
|
+
this.pendingBashComponents.push(this.bashComponent);
|
|
4396
|
+
} else {
|
|
4397
|
+
this.chatContainer.addChild(this.bashComponent);
|
|
4398
|
+
}
|
|
4399
|
+
|
|
4400
|
+
// Show output and complete
|
|
4401
|
+
if (result.output) {
|
|
4402
|
+
this.bashComponent.appendOutput(result.output);
|
|
4403
|
+
}
|
|
4404
|
+
this.bashComponent.setComplete(
|
|
4405
|
+
result.exitCode,
|
|
4406
|
+
result.cancelled,
|
|
4407
|
+
result.truncated ? ({ truncated: true, content: result.output } as TruncationResult) : undefined,
|
|
4408
|
+
result.fullOutputPath,
|
|
4409
|
+
);
|
|
4410
|
+
|
|
4411
|
+
// Record the result in session
|
|
4412
|
+
this.session.recordBashResult(command, result, { excludeFromContext });
|
|
4413
|
+
this.bashComponent = undefined;
|
|
4414
|
+
this.ui.requestRender();
|
|
4415
|
+
return;
|
|
4416
|
+
}
|
|
4417
|
+
|
|
4418
|
+
// Normal execution path (possibly with custom operations)
|
|
4419
|
+
const isDeferred = this.session.isStreaming;
|
|
4420
|
+
this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
|
|
4421
|
+
|
|
4422
|
+
if (isDeferred) {
|
|
4423
|
+
// Show in pending area when agent is streaming
|
|
4424
|
+
this.pendingMessagesContainer.addChild(this.bashComponent);
|
|
4425
|
+
this.pendingBashComponents.push(this.bashComponent);
|
|
4426
|
+
} else {
|
|
4427
|
+
// Show in chat immediately when agent is idle
|
|
4428
|
+
this.chatContainer.addChild(this.bashComponent);
|
|
4429
|
+
}
|
|
4430
|
+
this.ui.requestRender();
|
|
4431
|
+
|
|
4432
|
+
try {
|
|
4433
|
+
const result = await this.session.executeBash(
|
|
4434
|
+
command,
|
|
4435
|
+
(chunk) => {
|
|
4436
|
+
if (this.bashComponent) {
|
|
4437
|
+
this.bashComponent.appendOutput(chunk);
|
|
4438
|
+
this.ui.requestRender();
|
|
4439
|
+
}
|
|
4440
|
+
},
|
|
4441
|
+
{ excludeFromContext, operations: eventResult?.operations },
|
|
4442
|
+
);
|
|
4443
|
+
|
|
4444
|
+
if (this.bashComponent) {
|
|
4445
|
+
this.bashComponent.setComplete(
|
|
4446
|
+
result.exitCode,
|
|
4447
|
+
result.cancelled,
|
|
4448
|
+
result.truncated ? ({ truncated: true, content: result.output } as TruncationResult) : undefined,
|
|
4449
|
+
result.fullOutputPath,
|
|
4450
|
+
);
|
|
4451
|
+
}
|
|
4452
|
+
} catch (error) {
|
|
4453
|
+
if (this.bashComponent) {
|
|
4454
|
+
this.bashComponent.setComplete(undefined, false);
|
|
4455
|
+
}
|
|
4456
|
+
this.showError(`Bash command failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
4457
|
+
}
|
|
4458
|
+
|
|
4459
|
+
this.bashComponent = undefined;
|
|
4460
|
+
this.ui.requestRender();
|
|
4461
|
+
}
|
|
4462
|
+
|
|
4463
|
+
private async handleCompactCommand(customInstructions?: string): Promise<void> {
|
|
4464
|
+
const entries = this.sessionManager.getEntries();
|
|
4465
|
+
const messageCount = entries.filter((e) => e.type === "message").length;
|
|
4466
|
+
|
|
4467
|
+
if (messageCount < 2) {
|
|
4468
|
+
this.showWarning("Nothing to compact (no messages yet)");
|
|
4469
|
+
return;
|
|
4470
|
+
}
|
|
4471
|
+
|
|
4472
|
+
if (this.loadingAnimation) {
|
|
4473
|
+
this.loadingAnimation.stop();
|
|
4474
|
+
this.loadingAnimation = undefined;
|
|
4475
|
+
}
|
|
4476
|
+
this.statusContainer.clear();
|
|
4477
|
+
|
|
4478
|
+
try {
|
|
4479
|
+
await this.session.compact(customInstructions);
|
|
4480
|
+
} catch {
|
|
4481
|
+
// Ignore, will be emitted as an event
|
|
4482
|
+
}
|
|
4483
|
+
}
|
|
4484
|
+
|
|
4485
|
+
stop(): void {
|
|
4486
|
+
if (this.loadingAnimation) {
|
|
4487
|
+
this.loadingAnimation.stop();
|
|
4488
|
+
this.loadingAnimation = undefined;
|
|
4489
|
+
}
|
|
4490
|
+
this.clearExtensionTerminalInputListeners();
|
|
4491
|
+
this.footer.dispose();
|
|
4492
|
+
this.footerDataProvider.dispose();
|
|
4493
|
+
if (this.unsubscribe) {
|
|
4494
|
+
this.unsubscribe();
|
|
4495
|
+
}
|
|
4496
|
+
if (this.isInitialized) {
|
|
4497
|
+
this.ui.stop();
|
|
4498
|
+
this.isInitialized = false;
|
|
4499
|
+
}
|
|
4500
|
+
}
|
|
4501
|
+
}
|