@vaclav-synacek/pi-coding-agent-termux 0.45.7
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 +1961 -0
- package/README.md +1392 -0
- package/dist/cli/args.d.ts +42 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/args.js +248 -0
- package/dist/cli/args.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 +79 -0
- package/dist/cli/file-processor.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 +32 -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 +10 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +61 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +141 -0
- package/dist/config.js.map +1 -0
- package/dist/core/agent-session.d.ts +523 -0
- package/dist/core/agent-session.d.ts.map +1 -0
- package/dist/core/agent-session.js +1795 -0
- package/dist/core/agent-session.js.map +1 -0
- package/dist/core/auth-storage.d.ts +112 -0
- package/dist/core/auth-storage.d.ts.map +1 -0
- package/dist/core/auth-storage.js +297 -0
- package/dist/core/auth-storage.js.map +1 -0
- package/dist/core/bash-executor.d.ts +47 -0
- package/dist/core/bash-executor.d.ts.map +1 -0
- package/dist/core/bash-executor.js +211 -0
- package/dist/core/bash-executor.js.map +1 -0
- package/dist/core/compaction/branch-summarization.d.ts +84 -0
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
- package/dist/core/compaction/branch-summarization.js +235 -0
- package/dist/core/compaction/branch-summarization.js.map +1 -0
- package/dist/core/compaction/compaction.d.ts +110 -0
- package/dist/core/compaction/compaction.d.ts.map +1 -0
- package/dist/core/compaction/compaction.js +559 -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 +35 -0
- package/dist/core/compaction/utils.d.ts.map +1 -0
- package/dist/core/compaction/utils.js +138 -0
- package/dist/core/compaction/utils.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 +71 -0
- package/dist/core/exec.js.map +1 -0
- package/dist/core/export-html/index.d.ts +17 -0
- package/dist/core/export-html/index.d.ts.map +1 -0
- package/dist/core/export-html/index.js +193 -0
- package/dist/core/export-html/index.js.map +1 -0
- package/dist/core/export-html/template.css +910 -0
- package/dist/core/export-html/template.html +54 -0
- package/dist/core/export-html/template.js +1329 -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 +10 -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 +383 -0
- package/dist/core/extensions/loader.js.map +1 -0
- package/dist/core/extensions/runner.d.ts +89 -0
- package/dist/core/extensions/runner.d.ts.map +1 -0
- package/dist/core/extensions/runner.js +406 -0
- package/dist/core/extensions/runner.js.map +1 -0
- package/dist/core/extensions/types.d.ts +654 -0
- package/dist/core/extensions/types.d.ts.map +1 -0
- package/dist/core/extensions/types.js +32 -0
- package/dist/core/extensions/types.js.map +1 -0
- package/dist/core/extensions/wrapper.d.ts +27 -0
- package/dist/core/extensions/wrapper.d.ts.map +1 -0
- package/dist/core/extensions/wrapper.js +102 -0
- package/dist/core/extensions/wrapper.js.map +1 -0
- package/dist/core/footer-data-provider.d.ts +25 -0
- package/dist/core/footer-data-provider.d.ts.map +1 -0
- package/dist/core/footer-data-provider.js +121 -0
- package/dist/core/footer-data-provider.js.map +1 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +9 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/keybindings.d.ts +59 -0
- package/dist/core/keybindings.d.ts.map +1 -0
- package/dist/core/keybindings.js +151 -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 +57 -0
- package/dist/core/model-registry.d.ts.map +1 -0
- package/dist/core/model-registry.js +314 -0
- package/dist/core/model-registry.js.map +1 -0
- package/dist/core/model-resolver.d.ts +76 -0
- package/dist/core/model-resolver.d.ts.map +1 -0
- package/dist/core/model-resolver.js +308 -0
- package/dist/core/model-resolver.js.map +1 -0
- package/dist/core/prompt-templates.d.ts +40 -0
- package/dist/core/prompt-templates.d.ts.map +1 -0
- package/dist/core/prompt-templates.js +197 -0
- package/dist/core/prompt-templates.js.map +1 -0
- package/dist/core/sdk.d.ts +181 -0
- package/dist/core/sdk.d.ts.map +1 -0
- package/dist/core/sdk.js +466 -0
- package/dist/core/sdk.js.map +1 -0
- package/dist/core/session-manager.d.ts +313 -0
- package/dist/core/session-manager.d.ts.map +1 -0
- package/dist/core/session-manager.js +996 -0
- package/dist/core/session-manager.js.map +1 -0
- package/dist/core/settings-manager.d.ts +138 -0
- package/dist/core/settings-manager.d.ts.map +1 -0
- package/dist/core/settings-manager.js +327 -0
- package/dist/core/settings-manager.js.map +1 -0
- package/dist/core/skills.d.ts +50 -0
- package/dist/core/skills.d.ts.map +1 -0
- package/dist/core/skills.js +338 -0
- package/dist/core/skills.js.map +1 -0
- package/dist/core/system-prompt.d.ts +48 -0
- package/dist/core/system-prompt.d.ts.map +1 -0
- package/dist/core/system-prompt.js +224 -0
- package/dist/core/system-prompt.js.map +1 -0
- package/dist/core/timings.d.ts +7 -0
- package/dist/core/timings.d.ts.map +1 -0
- package/dist/core/timings.js +25 -0
- package/dist/core/timings.js.map +1 -0
- package/dist/core/tools/bash.d.ts +42 -0
- package/dist/core/tools/bash.d.ts.map +1 -0
- package/dist/core/tools/bash.js +223 -0
- package/dist/core/tools/bash.js.map +1 -0
- package/dist/core/tools/edit-diff.d.ts +33 -0
- package/dist/core/tools/edit-diff.d.ts.map +1 -0
- package/dist/core/tools/edit-diff.js +171 -0
- package/dist/core/tools/edit-diff.js.map +1 -0
- package/dist/core/tools/edit.d.ts +37 -0
- package/dist/core/tools/edit.d.ts.map +1 -0
- package/dist/core/tools/edit.js +143 -0
- package/dist/core/tools/edit.js.map +1 -0
- package/dist/core/tools/find.d.ts +37 -0
- package/dist/core/tools/find.d.ts.map +1 -0
- package/dist/core/tools/find.js +206 -0
- package/dist/core/tools/find.js.map +1 -0
- package/dist/core/tools/grep.d.ts +43 -0
- package/dist/core/tools/grep.d.ts.map +1 -0
- package/dist/core/tools/grep.js +239 -0
- package/dist/core/tools/grep.js.map +1 -0
- package/dist/core/tools/index.d.ts +70 -0
- package/dist/core/tools/index.d.ts.map +1 -0
- package/dist/core/tools/index.js +56 -0
- package/dist/core/tools/index.js.map +1 -0
- package/dist/core/tools/ls.d.ts +38 -0
- package/dist/core/tools/ls.d.ts.map +1 -0
- package/dist/core/tools/ls.js +118 -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 +53 -0
- package/dist/core/tools/path-utils.js.map +1 -0
- package/dist/core/tools/read.d.ts +37 -0
- package/dist/core/tools/read.d.ts.map +1 -0
- package/dist/core/tools/read.js +165 -0
- package/dist/core/tools/read.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 +27 -0
- package/dist/core/tools/write.d.ts.map +1 -0
- package/dist/core/tools/write.js +78 -0
- package/dist/core/tools/write.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -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 +354 -0
- package/dist/main.js.map +1 -0
- package/dist/migrations.d.ts +33 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/migrations.js +261 -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 +15 -0
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/assistant-message.js +89 -0
- package/dist/modes/interactive/components/assistant-message.js.map +1 -0
- package/dist/modes/interactive/components/bash-execution.d.ts +35 -0
- package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -0
- package/dist/modes/interactive/components/bash-execution.js +161 -0
- package/dist/modes/interactive/components/bash-execution.js.map +1 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts +12 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
- package/dist/modes/interactive/components/bordered-loader.js +30 -0
- package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/branch-summary-message.js +39 -0
- package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/compaction-summary-message.js +40 -0
- package/dist/modes/interactive/components/compaction-summary-message.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 +69 -0
- package/dist/modes/interactive/components/custom-editor.js.map +1 -0
- package/dist/modes/interactive/components/custom-message.d.ts +19 -0
- package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/custom-message.js +84 -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 +15 -0
- package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-editor.js +96 -0
- package/dist/modes/interactive/components/extension-editor.js.map +1 -0
- package/dist/modes/interactive/components/extension-input.d.ts +20 -0
- package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-input.js +51 -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 +73 -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 +207 -0
- package/dist/modes/interactive/components/footer.js.map +1 -0
- package/dist/modes/interactive/components/index.d.ts +29 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -0
- package/dist/modes/interactive/components/index.js +30 -0
- package/dist/modes/interactive/components/index.js.map +1 -0
- package/dist/modes/interactive/components/login-dialog.d.ts +39 -0
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -0
- package/dist/modes/interactive/components/login-dialog.js +135 -0
- package/dist/modes/interactive/components/login-dialog.js.map +1 -0
- package/dist/modes/interactive/components/model-selector.d.ts +35 -0
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/model-selector.js +211 -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 +98 -0
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts +46 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.js +258 -0
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
- package/dist/modes/interactive/components/session-selector.d.ts +44 -0
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/session-selector.js +311 -0
- package/dist/modes/interactive/components/session-selector.js.map +1 -0
- package/dist/modes/interactive/components/settings-selector.d.ts +43 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/settings-selector.js +219 -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 +35 -0
- package/dist/modes/interactive/components/show-images-selector.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 +46 -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 +47 -0
- package/dist/modes/interactive/components/thinking-selector.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts +70 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -0
- package/dist/modes/interactive/components/tool-execution.js +606 -0
- package/dist/modes/interactive/components/tool-execution.js.map +1 -0
- package/dist/modes/interactive/components/tree-selector.d.ts +52 -0
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/tree-selector.js +745 -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 +8 -0
- package/dist/modes/interactive/components/user-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/user-message.js +16 -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 +261 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
- package/dist/modes/interactive/interactive-mode.js +3194 -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 +308 -0
- package/dist/modes/interactive/theme/theme.d.ts +71 -0
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
- package/dist/modes/interactive/theme/theme.js +893 -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 +140 -0
- package/dist/modes/print-mode.js.map +1 -0
- package/dist/modes/rpc/rpc-client.d.ts +209 -0
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
- package/dist/modes/rpc/rpc-client.js +392 -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 +486 -0
- package/dist/modes/rpc/rpc-mode.js.map +1 -0
- package/dist/modes/rpc/rpc-types.d.ts +372 -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/clipboard-image.d.ts +11 -0
- package/dist/utils/clipboard-image.d.ts.map +1 -0
- package/dist/utils/clipboard-image.js +129 -0
- package/dist/utils/clipboard-image.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 +73 -0
- package/dist/utils/clipboard.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 +31 -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 +188 -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/shell.d.ts +26 -0
- package/dist/utils/shell.d.ts.map +1 -0
- package/dist/utils/shell.js +151 -0
- package/dist/utils/shell.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 +187 -0
- package/dist/utils/tools-manager.js.map +1 -0
- package/dist/utils/vips.d.ts +11 -0
- package/dist/utils/vips.d.ts.map +1 -0
- package/dist/utils/vips.js +35 -0
- package/dist/utils/vips.js.map +1 -0
- package/docs/compaction.md +388 -0
- package/docs/extensions.md +1524 -0
- package/docs/rpc.md +1046 -0
- package/docs/sdk.md +1024 -0
- package/docs/session.md +255 -0
- package/docs/skills.md +317 -0
- package/docs/theme.md +617 -0
- package/docs/tree.md +201 -0
- package/docs/tui.md +797 -0
- package/examples/README.md +24 -0
- package/examples/extensions/README.md +168 -0
- package/examples/extensions/auto-commit-on-exit.ts +49 -0
- package/examples/extensions/chalk-logger.ts +26 -0
- package/examples/extensions/claude-rules.ts +86 -0
- package/examples/extensions/confirm-destructive.ts +59 -0
- package/examples/extensions/custom-compaction.ts +114 -0
- package/examples/extensions/custom-footer.ts +64 -0
- package/examples/extensions/custom-header.ts +72 -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/file-trigger.ts +41 -0
- package/examples/extensions/git-checkpoint.ts +53 -0
- package/examples/extensions/handoff.ts +150 -0
- package/examples/extensions/hello.ts +25 -0
- package/examples/extensions/interactive-shell.ts +196 -0
- package/examples/extensions/mac-system-theme.ts +47 -0
- package/examples/extensions/modal-editor.ts +85 -0
- package/examples/extensions/model-status.ts +31 -0
- package/examples/extensions/notify.ts +25 -0
- package/examples/extensions/overlay-qa-tests.ts +881 -0
- package/examples/extensions/overlay-test.ts +145 -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 +168 -0
- package/examples/extensions/preset.ts +398 -0
- package/examples/extensions/protected-paths.ts +30 -0
- package/examples/extensions/qna.ts +119 -0
- package/examples/extensions/question.ts +277 -0
- package/examples/extensions/questionnaire.ts +427 -0
- package/examples/extensions/rainbow-editor.ts +95 -0
- package/examples/extensions/sandbox/index.ts +318 -0
- package/examples/extensions/sandbox/package-lock.json +92 -0
- package/examples/extensions/sandbox/package.json +19 -0
- package/examples/extensions/send-user-message.ts +97 -0
- package/examples/extensions/shutdown-command.ts +63 -0
- package/examples/extensions/snake.ts +343 -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 +156 -0
- package/examples/extensions/subagent/index.ts +963 -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 +195 -0
- package/examples/extensions/timed-confirm.ts +70 -0
- package/examples/extensions/todo.ts +299 -0
- package/examples/extensions/tool-override.ts +143 -0
- package/examples/extensions/tools.ts +146 -0
- package/examples/extensions/truncated-tool.ts +192 -0
- package/examples/extensions/with-deps/index.ts +36 -0
- package/examples/extensions/with-deps/package-lock.json +31 -0
- package/examples/extensions/with-deps/package.json +22 -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 +44 -0
- package/examples/sdk/04-skills.ts +47 -0
- package/examples/sdk/05-tools.ts +56 -0
- package/examples/sdk/06-extensions.ts +79 -0
- package/examples/sdk/07-context-files.ts +36 -0
- package/examples/sdk/08-prompt-templates.ts +42 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
- package/examples/sdk/10-settings.ts +38 -0
- package/examples/sdk/11-sessions.ts +48 -0
- package/examples/sdk/12-full-control.ts +72 -0
- package/examples/sdk/README.md +150 -0
- package/package.json +88 -0
|
@@ -0,0 +1,3194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive mode for the coding agent.
|
|
3
|
+
* Handles TUI rendering and user interaction, delegating business logic to AgentSession.
|
|
4
|
+
*/
|
|
5
|
+
import * as crypto from "node:crypto";
|
|
6
|
+
import * as fs from "node:fs";
|
|
7
|
+
import * as os from "node:os";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
import { getOAuthProviders, } from "@mariozechner/pi-ai";
|
|
10
|
+
import { CombinedAutocompleteProvider, Container, fuzzyFilter, getEditorKeybindings, Loader, Markdown, matchesKey, ProcessTerminal, Spacer, Text, TruncatedText, TUI, visibleWidth, } from "@mariozechner/pi-tui";
|
|
11
|
+
import { spawn, spawnSync } from "child_process";
|
|
12
|
+
import { APP_NAME, getAuthPath, getDebugLogPath, isBunBinary, VERSION } from "../../config.js";
|
|
13
|
+
import { FooterDataProvider } from "../../core/footer-data-provider.js";
|
|
14
|
+
import { KeybindingsManager } from "../../core/keybindings.js";
|
|
15
|
+
import { createCompactionSummaryMessage } from "../../core/messages.js";
|
|
16
|
+
import { resolveModelScope } from "../../core/model-resolver.js";
|
|
17
|
+
import { SessionManager } from "../../core/session-manager.js";
|
|
18
|
+
import { loadProjectContextFiles } from "../../core/system-prompt.js";
|
|
19
|
+
import { getChangelogPath, getNewEntries, parseChangelog } from "../../utils/changelog.js";
|
|
20
|
+
import { copyToClipboard } from "../../utils/clipboard.js";
|
|
21
|
+
import { extensionForImageMimeType, readClipboardImage } from "../../utils/clipboard-image.js";
|
|
22
|
+
import { ensureTool } from "../../utils/tools-manager.js";
|
|
23
|
+
import { ArminComponent } from "./components/armin.js";
|
|
24
|
+
import { AssistantMessageComponent } from "./components/assistant-message.js";
|
|
25
|
+
import { BashExecutionComponent } from "./components/bash-execution.js";
|
|
26
|
+
import { BorderedLoader } from "./components/bordered-loader.js";
|
|
27
|
+
import { BranchSummaryMessageComponent } from "./components/branch-summary-message.js";
|
|
28
|
+
import { CompactionSummaryMessageComponent } from "./components/compaction-summary-message.js";
|
|
29
|
+
import { CustomEditor } from "./components/custom-editor.js";
|
|
30
|
+
import { CustomMessageComponent } from "./components/custom-message.js";
|
|
31
|
+
import { DynamicBorder } from "./components/dynamic-border.js";
|
|
32
|
+
import { ExtensionEditorComponent } from "./components/extension-editor.js";
|
|
33
|
+
import { ExtensionInputComponent } from "./components/extension-input.js";
|
|
34
|
+
import { ExtensionSelectorComponent } from "./components/extension-selector.js";
|
|
35
|
+
import { FooterComponent } from "./components/footer.js";
|
|
36
|
+
import { LoginDialogComponent } from "./components/login-dialog.js";
|
|
37
|
+
import { ModelSelectorComponent } from "./components/model-selector.js";
|
|
38
|
+
import { OAuthSelectorComponent } from "./components/oauth-selector.js";
|
|
39
|
+
import { ScopedModelsSelectorComponent } from "./components/scoped-models-selector.js";
|
|
40
|
+
import { SessionSelectorComponent } from "./components/session-selector.js";
|
|
41
|
+
import { SettingsSelectorComponent } from "./components/settings-selector.js";
|
|
42
|
+
import { ToolExecutionComponent } from "./components/tool-execution.js";
|
|
43
|
+
import { TreeSelectorComponent } from "./components/tree-selector.js";
|
|
44
|
+
import { UserMessageComponent } from "./components/user-message.js";
|
|
45
|
+
import { UserMessageSelectorComponent } from "./components/user-message-selector.js";
|
|
46
|
+
import { getAvailableThemes, getAvailableThemesWithPaths, getEditorTheme, getMarkdownTheme, getThemeByName, initTheme, onThemeChange, setTheme, setThemeInstance, Theme, theme, } from "./theme/theme.js";
|
|
47
|
+
function isExpandable(obj) {
|
|
48
|
+
return typeof obj === "object" && obj !== null && "setExpanded" in obj && typeof obj.setExpanded === "function";
|
|
49
|
+
}
|
|
50
|
+
export class InteractiveMode {
|
|
51
|
+
options;
|
|
52
|
+
session;
|
|
53
|
+
ui;
|
|
54
|
+
chatContainer;
|
|
55
|
+
pendingMessagesContainer;
|
|
56
|
+
statusContainer;
|
|
57
|
+
defaultEditor;
|
|
58
|
+
editor;
|
|
59
|
+
autocompleteProvider;
|
|
60
|
+
fdPath;
|
|
61
|
+
editorContainer;
|
|
62
|
+
footer;
|
|
63
|
+
footerDataProvider;
|
|
64
|
+
keybindings;
|
|
65
|
+
version;
|
|
66
|
+
isInitialized = false;
|
|
67
|
+
onInputCallback;
|
|
68
|
+
loadingAnimation = undefined;
|
|
69
|
+
defaultWorkingMessage = "Working... (esc to interrupt)";
|
|
70
|
+
lastSigintTime = 0;
|
|
71
|
+
lastEscapeTime = 0;
|
|
72
|
+
changelogMarkdown = undefined;
|
|
73
|
+
// Status line tracking (for mutating immediately-sequential status updates)
|
|
74
|
+
lastStatusSpacer = undefined;
|
|
75
|
+
lastStatusText = undefined;
|
|
76
|
+
// Streaming message tracking
|
|
77
|
+
streamingComponent = undefined;
|
|
78
|
+
streamingMessage = undefined;
|
|
79
|
+
// Tool execution tracking: toolCallId -> component
|
|
80
|
+
pendingTools = new Map();
|
|
81
|
+
// Tool output expansion state
|
|
82
|
+
toolOutputExpanded = false;
|
|
83
|
+
// Thinking block visibility state
|
|
84
|
+
hideThinkingBlock = false;
|
|
85
|
+
// Skill commands: command name -> skill file path
|
|
86
|
+
skillCommands = new Map();
|
|
87
|
+
// Agent subscription unsubscribe function
|
|
88
|
+
unsubscribe;
|
|
89
|
+
// Track if editor is in bash mode (text starts with !)
|
|
90
|
+
isBashMode = false;
|
|
91
|
+
// Track current bash execution component
|
|
92
|
+
bashComponent = undefined;
|
|
93
|
+
// Track pending bash components (shown in pending area, moved to chat on submit)
|
|
94
|
+
pendingBashComponents = [];
|
|
95
|
+
// Auto-compaction state
|
|
96
|
+
autoCompactionLoader = undefined;
|
|
97
|
+
autoCompactionEscapeHandler;
|
|
98
|
+
// Auto-retry state
|
|
99
|
+
retryLoader = undefined;
|
|
100
|
+
retryEscapeHandler;
|
|
101
|
+
// Messages queued while compaction is running
|
|
102
|
+
compactionQueuedMessages = [];
|
|
103
|
+
// Shutdown state
|
|
104
|
+
shutdownRequested = false;
|
|
105
|
+
// Extension UI state
|
|
106
|
+
extensionSelector = undefined;
|
|
107
|
+
extensionInput = undefined;
|
|
108
|
+
extensionEditor = undefined;
|
|
109
|
+
// Extension widgets (components rendered above the editor)
|
|
110
|
+
extensionWidgets = new Map();
|
|
111
|
+
widgetContainer;
|
|
112
|
+
// Custom footer from extension (undefined = use built-in footer)
|
|
113
|
+
customFooter = undefined;
|
|
114
|
+
// Built-in header (logo + keybinding hints + changelog)
|
|
115
|
+
builtInHeader = undefined;
|
|
116
|
+
// Custom header from extension (undefined = use built-in header)
|
|
117
|
+
customHeader = undefined;
|
|
118
|
+
// Convenience accessors
|
|
119
|
+
get agent() {
|
|
120
|
+
return this.session.agent;
|
|
121
|
+
}
|
|
122
|
+
get sessionManager() {
|
|
123
|
+
return this.session.sessionManager;
|
|
124
|
+
}
|
|
125
|
+
get settingsManager() {
|
|
126
|
+
return this.session.settingsManager;
|
|
127
|
+
}
|
|
128
|
+
constructor(session, options = {}) {
|
|
129
|
+
this.options = options;
|
|
130
|
+
this.session = session;
|
|
131
|
+
this.version = VERSION;
|
|
132
|
+
this.ui = new TUI(new ProcessTerminal());
|
|
133
|
+
this.chatContainer = new Container();
|
|
134
|
+
this.pendingMessagesContainer = new Container();
|
|
135
|
+
this.statusContainer = new Container();
|
|
136
|
+
this.widgetContainer = new Container();
|
|
137
|
+
this.keybindings = KeybindingsManager.create();
|
|
138
|
+
this.defaultEditor = new CustomEditor(getEditorTheme(), this.keybindings);
|
|
139
|
+
this.editor = this.defaultEditor;
|
|
140
|
+
this.editorContainer = new Container();
|
|
141
|
+
this.editorContainer.addChild(this.editor);
|
|
142
|
+
this.footerDataProvider = new FooterDataProvider();
|
|
143
|
+
this.footer = new FooterComponent(session, this.footerDataProvider);
|
|
144
|
+
this.footer.setAutoCompactEnabled(session.autoCompactionEnabled);
|
|
145
|
+
// Load hide thinking block setting
|
|
146
|
+
this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
|
|
147
|
+
// Initialize theme with watcher for interactive mode
|
|
148
|
+
initTheme(this.settingsManager.getTheme(), true);
|
|
149
|
+
}
|
|
150
|
+
setupAutocomplete(fdPath) {
|
|
151
|
+
// Define commands for autocomplete
|
|
152
|
+
const slashCommands = [
|
|
153
|
+
{ name: "settings", description: "Open settings menu" },
|
|
154
|
+
{
|
|
155
|
+
name: "model",
|
|
156
|
+
description: "Select model (opens selector UI)",
|
|
157
|
+
getArgumentCompletions: (prefix) => {
|
|
158
|
+
// Get available models (scoped or from registry)
|
|
159
|
+
const models = this.session.scopedModels.length > 0
|
|
160
|
+
? this.session.scopedModels.map((s) => s.model)
|
|
161
|
+
: this.session.modelRegistry.getAvailable();
|
|
162
|
+
if (models.length === 0)
|
|
163
|
+
return null;
|
|
164
|
+
// Create items with provider/id format
|
|
165
|
+
const items = models.map((m) => ({
|
|
166
|
+
id: m.id,
|
|
167
|
+
provider: m.provider,
|
|
168
|
+
label: `${m.provider}/${m.id}`,
|
|
169
|
+
}));
|
|
170
|
+
// Fuzzy filter by model ID + provider (allows "opus anthropic" to match)
|
|
171
|
+
const filtered = fuzzyFilter(items, prefix, (item) => `${item.id} ${item.provider}`);
|
|
172
|
+
if (filtered.length === 0)
|
|
173
|
+
return null;
|
|
174
|
+
return filtered.map((item) => ({
|
|
175
|
+
value: item.label,
|
|
176
|
+
label: item.id,
|
|
177
|
+
description: item.provider,
|
|
178
|
+
}));
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
{ name: "scoped-models", description: "Enable/disable models for Ctrl+P cycling" },
|
|
182
|
+
{ name: "export", description: "Export session to HTML file" },
|
|
183
|
+
{ name: "share", description: "Share session as a secret GitHub gist" },
|
|
184
|
+
{ name: "copy", description: "Copy last agent message to clipboard" },
|
|
185
|
+
{ name: "name", description: "Set session display name" },
|
|
186
|
+
{ name: "session", description: "Show session info and stats" },
|
|
187
|
+
{ name: "changelog", description: "Show changelog entries" },
|
|
188
|
+
{ name: "hotkeys", description: "Show all keyboard shortcuts" },
|
|
189
|
+
{ name: "fork", description: "Create a new fork from a previous message" },
|
|
190
|
+
{ name: "tree", description: "Navigate session tree (switch branches)" },
|
|
191
|
+
{ name: "login", description: "Login with OAuth provider" },
|
|
192
|
+
{ name: "logout", description: "Logout from OAuth provider" },
|
|
193
|
+
{ name: "new", description: "Start a new session" },
|
|
194
|
+
{ name: "compact", description: "Manually compact the session context" },
|
|
195
|
+
{ name: "resume", description: "Resume a different session" },
|
|
196
|
+
];
|
|
197
|
+
// Convert prompt templates to SlashCommand format for autocomplete
|
|
198
|
+
const templateCommands = this.session.promptTemplates.map((cmd) => ({
|
|
199
|
+
name: cmd.name,
|
|
200
|
+
description: cmd.description,
|
|
201
|
+
}));
|
|
202
|
+
// Convert extension commands to SlashCommand format
|
|
203
|
+
const extensionCommands = (this.session.extensionRunner?.getRegisteredCommands() ?? []).map((cmd) => ({
|
|
204
|
+
name: cmd.name,
|
|
205
|
+
description: cmd.description ?? "(extension command)",
|
|
206
|
+
}));
|
|
207
|
+
// Build skill commands from session.skills (if enabled)
|
|
208
|
+
this.skillCommands.clear();
|
|
209
|
+
const skillCommandList = [];
|
|
210
|
+
if (this.settingsManager.getEnableSkillCommands()) {
|
|
211
|
+
for (const skill of this.session.skills) {
|
|
212
|
+
const commandName = `skill:${skill.name}`;
|
|
213
|
+
this.skillCommands.set(commandName, skill.filePath);
|
|
214
|
+
skillCommandList.push({ name: commandName, description: skill.description });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// Setup autocomplete
|
|
218
|
+
this.autocompleteProvider = new CombinedAutocompleteProvider([...slashCommands, ...templateCommands, ...extensionCommands, ...skillCommandList], process.cwd(), fdPath);
|
|
219
|
+
this.defaultEditor.setAutocompleteProvider(this.autocompleteProvider);
|
|
220
|
+
}
|
|
221
|
+
rebuildAutocomplete() {
|
|
222
|
+
this.setupAutocomplete(this.fdPath);
|
|
223
|
+
}
|
|
224
|
+
async init() {
|
|
225
|
+
if (this.isInitialized)
|
|
226
|
+
return;
|
|
227
|
+
// Load changelog (only show new entries, skip for resumed sessions)
|
|
228
|
+
this.changelogMarkdown = this.getChangelogForDisplay();
|
|
229
|
+
// Setup autocomplete with fd tool for file path completion
|
|
230
|
+
this.fdPath = await ensureTool("fd");
|
|
231
|
+
this.setupAutocomplete(this.fdPath);
|
|
232
|
+
// Add header with keybindings from config
|
|
233
|
+
const logo = theme.bold(theme.fg("accent", APP_NAME)) + theme.fg("dim", ` v${this.version}`);
|
|
234
|
+
// Format keybinding for startup display (lowercase, compact)
|
|
235
|
+
const formatStartupKey = (keys) => {
|
|
236
|
+
const keyArray = Array.isArray(keys) ? keys : [keys];
|
|
237
|
+
return keyArray.join("/");
|
|
238
|
+
};
|
|
239
|
+
const kb = this.keybindings;
|
|
240
|
+
const interrupt = formatStartupKey(kb.getKeys("interrupt"));
|
|
241
|
+
const clear = formatStartupKey(kb.getKeys("clear"));
|
|
242
|
+
const exit = formatStartupKey(kb.getKeys("exit"));
|
|
243
|
+
const suspend = formatStartupKey(kb.getKeys("suspend"));
|
|
244
|
+
const deleteToLineEnd = formatStartupKey(getEditorKeybindings().getKeys("deleteToLineEnd"));
|
|
245
|
+
const cycleThinkingLevel = formatStartupKey(kb.getKeys("cycleThinkingLevel"));
|
|
246
|
+
const cycleModelForward = formatStartupKey(kb.getKeys("cycleModelForward"));
|
|
247
|
+
const cycleModelBackward = formatStartupKey(kb.getKeys("cycleModelBackward"));
|
|
248
|
+
const selectModel = formatStartupKey(kb.getKeys("selectModel"));
|
|
249
|
+
const expandTools = formatStartupKey(kb.getKeys("expandTools"));
|
|
250
|
+
const toggleThinking = formatStartupKey(kb.getKeys("toggleThinking"));
|
|
251
|
+
const externalEditor = formatStartupKey(kb.getKeys("externalEditor"));
|
|
252
|
+
const followUp = formatStartupKey(kb.getKeys("followUp"));
|
|
253
|
+
const dequeue = formatStartupKey(kb.getKeys("dequeue"));
|
|
254
|
+
const instructions = theme.fg("dim", interrupt) +
|
|
255
|
+
theme.fg("muted", " to interrupt") +
|
|
256
|
+
"\n" +
|
|
257
|
+
theme.fg("dim", clear) +
|
|
258
|
+
theme.fg("muted", " to clear") +
|
|
259
|
+
"\n" +
|
|
260
|
+
theme.fg("dim", `${clear} twice`) +
|
|
261
|
+
theme.fg("muted", " to exit") +
|
|
262
|
+
"\n" +
|
|
263
|
+
theme.fg("dim", exit) +
|
|
264
|
+
theme.fg("muted", " to exit (empty)") +
|
|
265
|
+
"\n" +
|
|
266
|
+
theme.fg("dim", suspend) +
|
|
267
|
+
theme.fg("muted", " to suspend") +
|
|
268
|
+
"\n" +
|
|
269
|
+
theme.fg("dim", deleteToLineEnd) +
|
|
270
|
+
theme.fg("muted", " to delete to end") +
|
|
271
|
+
"\n" +
|
|
272
|
+
theme.fg("dim", cycleThinkingLevel) +
|
|
273
|
+
theme.fg("muted", " to cycle thinking") +
|
|
274
|
+
"\n" +
|
|
275
|
+
theme.fg("dim", `${cycleModelForward}/${cycleModelBackward}`) +
|
|
276
|
+
theme.fg("muted", " to cycle models") +
|
|
277
|
+
"\n" +
|
|
278
|
+
theme.fg("dim", selectModel) +
|
|
279
|
+
theme.fg("muted", " to select model") +
|
|
280
|
+
"\n" +
|
|
281
|
+
theme.fg("dim", expandTools) +
|
|
282
|
+
theme.fg("muted", " to expand tools") +
|
|
283
|
+
"\n" +
|
|
284
|
+
theme.fg("dim", toggleThinking) +
|
|
285
|
+
theme.fg("muted", " to toggle thinking") +
|
|
286
|
+
"\n" +
|
|
287
|
+
theme.fg("dim", externalEditor) +
|
|
288
|
+
theme.fg("muted", " for external editor") +
|
|
289
|
+
"\n" +
|
|
290
|
+
theme.fg("dim", "/") +
|
|
291
|
+
theme.fg("muted", " for commands") +
|
|
292
|
+
"\n" +
|
|
293
|
+
theme.fg("dim", "!") +
|
|
294
|
+
theme.fg("muted", " to run bash") +
|
|
295
|
+
"\n" +
|
|
296
|
+
theme.fg("dim", "!!") +
|
|
297
|
+
theme.fg("muted", " to run bash (no context)") +
|
|
298
|
+
"\n" +
|
|
299
|
+
theme.fg("dim", followUp) +
|
|
300
|
+
theme.fg("muted", " to queue follow-up") +
|
|
301
|
+
"\n" +
|
|
302
|
+
theme.fg("dim", dequeue) +
|
|
303
|
+
theme.fg("muted", " to edit all queued messages") +
|
|
304
|
+
"\n" +
|
|
305
|
+
theme.fg("dim", "ctrl+v") +
|
|
306
|
+
theme.fg("muted", " to paste image") +
|
|
307
|
+
"\n" +
|
|
308
|
+
theme.fg("dim", "drop files") +
|
|
309
|
+
theme.fg("muted", " to attach");
|
|
310
|
+
this.builtInHeader = new Text(`${logo}\n${instructions}`, 1, 0);
|
|
311
|
+
// Setup UI layout
|
|
312
|
+
this.ui.addChild(new Spacer(1));
|
|
313
|
+
this.ui.addChild(this.builtInHeader);
|
|
314
|
+
this.ui.addChild(new Spacer(1));
|
|
315
|
+
// Add changelog if provided
|
|
316
|
+
if (this.changelogMarkdown) {
|
|
317
|
+
this.ui.addChild(new DynamicBorder());
|
|
318
|
+
if (this.settingsManager.getCollapseChangelog()) {
|
|
319
|
+
const versionMatch = this.changelogMarkdown.match(/##\s+\[?(\d+\.\d+\.\d+)\]?/);
|
|
320
|
+
const latestVersion = versionMatch ? versionMatch[1] : this.version;
|
|
321
|
+
const condensedText = `Updated to v${latestVersion}. Use ${theme.bold("/changelog")} to view full changelog.`;
|
|
322
|
+
this.ui.addChild(new Text(condensedText, 1, 0));
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
this.ui.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
|
|
326
|
+
this.ui.addChild(new Spacer(1));
|
|
327
|
+
this.ui.addChild(new Markdown(this.changelogMarkdown.trim(), 1, 0, getMarkdownTheme()));
|
|
328
|
+
this.ui.addChild(new Spacer(1));
|
|
329
|
+
}
|
|
330
|
+
this.ui.addChild(new DynamicBorder());
|
|
331
|
+
}
|
|
332
|
+
this.ui.addChild(this.chatContainer);
|
|
333
|
+
this.ui.addChild(this.pendingMessagesContainer);
|
|
334
|
+
this.ui.addChild(this.statusContainer);
|
|
335
|
+
this.ui.addChild(this.widgetContainer);
|
|
336
|
+
this.renderWidgets(); // Initialize with default spacer
|
|
337
|
+
this.ui.addChild(this.editorContainer);
|
|
338
|
+
this.ui.addChild(this.footer);
|
|
339
|
+
this.ui.setFocus(this.editor);
|
|
340
|
+
this.setupKeyHandlers();
|
|
341
|
+
this.setupEditorSubmitHandler();
|
|
342
|
+
// Start the UI
|
|
343
|
+
this.ui.start();
|
|
344
|
+
this.isInitialized = true;
|
|
345
|
+
// Set terminal title
|
|
346
|
+
const cwdBasename = path.basename(process.cwd());
|
|
347
|
+
this.ui.terminal.setTitle(`pi - ${cwdBasename}`);
|
|
348
|
+
// Initialize extensions with TUI-based UI context
|
|
349
|
+
await this.initExtensions();
|
|
350
|
+
// Subscribe to agent events
|
|
351
|
+
this.subscribeToAgent();
|
|
352
|
+
// Set up theme file watcher
|
|
353
|
+
onThemeChange(() => {
|
|
354
|
+
this.ui.invalidate();
|
|
355
|
+
this.updateEditorBorderColor();
|
|
356
|
+
this.ui.requestRender();
|
|
357
|
+
});
|
|
358
|
+
// Set up git branch watcher (uses provider instead of footer)
|
|
359
|
+
this.footerDataProvider.onBranchChange(() => {
|
|
360
|
+
this.ui.requestRender();
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Run the interactive mode. This is the main entry point.
|
|
365
|
+
* Initializes the UI, shows warnings, processes initial messages, and starts the interactive loop.
|
|
366
|
+
*/
|
|
367
|
+
async run() {
|
|
368
|
+
await this.init();
|
|
369
|
+
// Start version check asynchronously
|
|
370
|
+
this.checkForNewVersion().then((newVersion) => {
|
|
371
|
+
if (newVersion) {
|
|
372
|
+
this.showNewVersionNotification(newVersion);
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
this.renderInitialMessages();
|
|
376
|
+
// Show startup warnings
|
|
377
|
+
const { migratedProviders, modelFallbackMessage, initialMessage, initialImages, initialMessages } = this.options;
|
|
378
|
+
if (migratedProviders && migratedProviders.length > 0) {
|
|
379
|
+
this.showWarning(`Migrated credentials to auth.json: ${migratedProviders.join(", ")}`);
|
|
380
|
+
}
|
|
381
|
+
const modelsJsonError = this.session.modelRegistry.getError();
|
|
382
|
+
if (modelsJsonError) {
|
|
383
|
+
this.showError(`models.json error: ${modelsJsonError}`);
|
|
384
|
+
}
|
|
385
|
+
if (modelFallbackMessage) {
|
|
386
|
+
this.showWarning(modelFallbackMessage);
|
|
387
|
+
}
|
|
388
|
+
// Process initial messages
|
|
389
|
+
if (initialMessage) {
|
|
390
|
+
try {
|
|
391
|
+
await this.session.prompt(initialMessage, { images: initialImages });
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
395
|
+
this.showError(errorMessage);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (initialMessages) {
|
|
399
|
+
for (const message of initialMessages) {
|
|
400
|
+
try {
|
|
401
|
+
await this.session.prompt(message);
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
405
|
+
this.showError(errorMessage);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
// Main interactive loop
|
|
410
|
+
while (true) {
|
|
411
|
+
const userInput = await this.getUserInput();
|
|
412
|
+
try {
|
|
413
|
+
await this.session.prompt(userInput);
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
417
|
+
this.showError(errorMessage);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Check npm registry for a newer version.
|
|
423
|
+
*/
|
|
424
|
+
async checkForNewVersion() {
|
|
425
|
+
if (process.env.PI_SKIP_VERSION_CHECK)
|
|
426
|
+
return undefined;
|
|
427
|
+
try {
|
|
428
|
+
const response = await fetch("https://registry.npmjs.org/@vaclav-synacek/pi-coding-agent-termux/latest");
|
|
429
|
+
if (!response.ok)
|
|
430
|
+
return undefined;
|
|
431
|
+
const data = (await response.json());
|
|
432
|
+
const latestVersion = data.version;
|
|
433
|
+
if (latestVersion && latestVersion !== this.version) {
|
|
434
|
+
return latestVersion;
|
|
435
|
+
}
|
|
436
|
+
return undefined;
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Get changelog entries to display on startup.
|
|
444
|
+
* Only shows new entries since last seen version, skips for resumed sessions.
|
|
445
|
+
*/
|
|
446
|
+
getChangelogForDisplay() {
|
|
447
|
+
// Skip changelog for resumed/continued sessions (already have messages)
|
|
448
|
+
if (this.session.state.messages.length > 0) {
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
const lastVersion = this.settingsManager.getLastChangelogVersion();
|
|
452
|
+
const changelogPath = getChangelogPath();
|
|
453
|
+
const entries = parseChangelog(changelogPath);
|
|
454
|
+
if (!lastVersion) {
|
|
455
|
+
// Fresh install - just record the version, don't show changelog
|
|
456
|
+
this.settingsManager.setLastChangelogVersion(VERSION);
|
|
457
|
+
return undefined;
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
const newEntries = getNewEntries(entries, lastVersion);
|
|
461
|
+
if (newEntries.length > 0) {
|
|
462
|
+
this.settingsManager.setLastChangelogVersion(VERSION);
|
|
463
|
+
return newEntries.map((e) => e.content).join("\n\n");
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return undefined;
|
|
467
|
+
}
|
|
468
|
+
// =========================================================================
|
|
469
|
+
// Extension System
|
|
470
|
+
// =========================================================================
|
|
471
|
+
/**
|
|
472
|
+
* Initialize the extension system with TUI-based UI context.
|
|
473
|
+
*/
|
|
474
|
+
async initExtensions() {
|
|
475
|
+
// Show loaded project context files
|
|
476
|
+
const contextFiles = loadProjectContextFiles();
|
|
477
|
+
if (contextFiles.length > 0) {
|
|
478
|
+
const contextList = contextFiles.map((f) => theme.fg("dim", ` ${f.path}`)).join("\n");
|
|
479
|
+
this.chatContainer.addChild(new Text(theme.fg("muted", "Loaded context:\n") + contextList, 0, 0));
|
|
480
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
481
|
+
}
|
|
482
|
+
// Show loaded skills (already discovered by SDK)
|
|
483
|
+
const skills = this.session.skills;
|
|
484
|
+
if (skills.length > 0) {
|
|
485
|
+
const skillList = skills.map((s) => theme.fg("dim", ` ${s.filePath}`)).join("\n");
|
|
486
|
+
this.chatContainer.addChild(new Text(theme.fg("muted", "Loaded skills:\n") + skillList, 0, 0));
|
|
487
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
488
|
+
}
|
|
489
|
+
// Show skill warnings if any
|
|
490
|
+
const skillWarnings = this.session.skillWarnings;
|
|
491
|
+
if (skillWarnings.length > 0) {
|
|
492
|
+
const warningList = skillWarnings.map((w) => theme.fg("warning", ` ${w.skillPath}: ${w.message}`)).join("\n");
|
|
493
|
+
this.chatContainer.addChild(new Text(theme.fg("warning", "Skill warnings:\n") + warningList, 0, 0));
|
|
494
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
495
|
+
}
|
|
496
|
+
const extensionRunner = this.session.extensionRunner;
|
|
497
|
+
if (!extensionRunner) {
|
|
498
|
+
return; // No extensions loaded
|
|
499
|
+
}
|
|
500
|
+
// Create extension UI context
|
|
501
|
+
const uiContext = this.createExtensionUIContext();
|
|
502
|
+
extensionRunner.initialize(
|
|
503
|
+
// ExtensionActions - for pi.* API
|
|
504
|
+
{
|
|
505
|
+
sendMessage: (message, options) => {
|
|
506
|
+
const wasStreaming = this.session.isStreaming;
|
|
507
|
+
this.session
|
|
508
|
+
.sendCustomMessage(message, options)
|
|
509
|
+
.then(() => {
|
|
510
|
+
if (!wasStreaming && message.display) {
|
|
511
|
+
this.rebuildChatFromMessages();
|
|
512
|
+
}
|
|
513
|
+
})
|
|
514
|
+
.catch((err) => {
|
|
515
|
+
this.showError(`Extension sendMessage failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
516
|
+
});
|
|
517
|
+
},
|
|
518
|
+
sendUserMessage: (content, options) => {
|
|
519
|
+
this.session.sendUserMessage(content, options).catch((err) => {
|
|
520
|
+
this.showError(`Extension sendUserMessage failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
521
|
+
});
|
|
522
|
+
},
|
|
523
|
+
appendEntry: (customType, data) => {
|
|
524
|
+
this.sessionManager.appendCustomEntry(customType, data);
|
|
525
|
+
},
|
|
526
|
+
setSessionName: (name) => {
|
|
527
|
+
this.sessionManager.appendSessionInfo(name);
|
|
528
|
+
},
|
|
529
|
+
getSessionName: () => {
|
|
530
|
+
return this.sessionManager.getSessionName();
|
|
531
|
+
},
|
|
532
|
+
getActiveTools: () => this.session.getActiveToolNames(),
|
|
533
|
+
getAllTools: () => this.session.getAllTools(),
|
|
534
|
+
setActiveTools: (toolNames) => this.session.setActiveToolsByName(toolNames),
|
|
535
|
+
setModel: async (model) => {
|
|
536
|
+
const key = await this.session.modelRegistry.getApiKey(model);
|
|
537
|
+
if (!key)
|
|
538
|
+
return false;
|
|
539
|
+
await this.session.setModel(model);
|
|
540
|
+
return true;
|
|
541
|
+
},
|
|
542
|
+
getThinkingLevel: () => this.session.thinkingLevel,
|
|
543
|
+
setThinkingLevel: (level) => this.session.setThinkingLevel(level),
|
|
544
|
+
},
|
|
545
|
+
// ExtensionContextActions - for ctx.* in event handlers
|
|
546
|
+
{
|
|
547
|
+
getModel: () => this.session.model,
|
|
548
|
+
isIdle: () => !this.session.isStreaming,
|
|
549
|
+
abort: () => this.session.abort(),
|
|
550
|
+
hasPendingMessages: () => this.session.pendingMessageCount > 0,
|
|
551
|
+
shutdown: () => {
|
|
552
|
+
this.shutdownRequested = true;
|
|
553
|
+
},
|
|
554
|
+
},
|
|
555
|
+
// ExtensionCommandContextActions - for ctx.* in command handlers
|
|
556
|
+
{
|
|
557
|
+
waitForIdle: () => this.session.agent.waitForIdle(),
|
|
558
|
+
newSession: async (options) => {
|
|
559
|
+
if (this.loadingAnimation) {
|
|
560
|
+
this.loadingAnimation.stop();
|
|
561
|
+
this.loadingAnimation = undefined;
|
|
562
|
+
}
|
|
563
|
+
this.statusContainer.clear();
|
|
564
|
+
const success = await this.session.newSession({ parentSession: options?.parentSession });
|
|
565
|
+
if (!success) {
|
|
566
|
+
return { cancelled: true };
|
|
567
|
+
}
|
|
568
|
+
if (options?.setup) {
|
|
569
|
+
await options.setup(this.sessionManager);
|
|
570
|
+
}
|
|
571
|
+
this.chatContainer.clear();
|
|
572
|
+
this.pendingMessagesContainer.clear();
|
|
573
|
+
this.compactionQueuedMessages = [];
|
|
574
|
+
this.streamingComponent = undefined;
|
|
575
|
+
this.streamingMessage = undefined;
|
|
576
|
+
this.pendingTools.clear();
|
|
577
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
578
|
+
this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
|
|
579
|
+
this.ui.requestRender();
|
|
580
|
+
return { cancelled: false };
|
|
581
|
+
},
|
|
582
|
+
fork: async (entryId) => {
|
|
583
|
+
const result = await this.session.fork(entryId);
|
|
584
|
+
if (result.cancelled) {
|
|
585
|
+
return { cancelled: true };
|
|
586
|
+
}
|
|
587
|
+
this.chatContainer.clear();
|
|
588
|
+
this.renderInitialMessages();
|
|
589
|
+
this.editor.setText(result.selectedText);
|
|
590
|
+
this.showStatus("Forked to new session");
|
|
591
|
+
return { cancelled: false };
|
|
592
|
+
},
|
|
593
|
+
navigateTree: async (targetId, options) => {
|
|
594
|
+
const result = await this.session.navigateTree(targetId, { summarize: options?.summarize });
|
|
595
|
+
if (result.cancelled) {
|
|
596
|
+
return { cancelled: true };
|
|
597
|
+
}
|
|
598
|
+
this.chatContainer.clear();
|
|
599
|
+
this.renderInitialMessages();
|
|
600
|
+
if (result.editorText) {
|
|
601
|
+
this.editor.setText(result.editorText);
|
|
602
|
+
}
|
|
603
|
+
this.showStatus("Navigated to selected point");
|
|
604
|
+
return { cancelled: false };
|
|
605
|
+
},
|
|
606
|
+
}, uiContext);
|
|
607
|
+
// Subscribe to extension errors
|
|
608
|
+
extensionRunner.onError((error) => {
|
|
609
|
+
this.showExtensionError(error.extensionPath, error.error, error.stack);
|
|
610
|
+
});
|
|
611
|
+
// Set up extension-registered shortcuts
|
|
612
|
+
this.setupExtensionShortcuts(extensionRunner);
|
|
613
|
+
// Show loaded extensions
|
|
614
|
+
const extensionPaths = extensionRunner.getExtensionPaths();
|
|
615
|
+
if (extensionPaths.length > 0) {
|
|
616
|
+
const extList = extensionPaths.map((p) => theme.fg("dim", ` ${p}`)).join("\n");
|
|
617
|
+
this.chatContainer.addChild(new Text(theme.fg("muted", "Loaded extensions:\n") + extList, 0, 0));
|
|
618
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
619
|
+
}
|
|
620
|
+
// Emit session_start event
|
|
621
|
+
await extensionRunner.emit({
|
|
622
|
+
type: "session_start",
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Get a registered tool definition by name (for custom rendering).
|
|
627
|
+
*/
|
|
628
|
+
getRegisteredToolDefinition(toolName) {
|
|
629
|
+
const tools = this.session.extensionRunner?.getAllRegisteredTools() ?? [];
|
|
630
|
+
const registeredTool = tools.find((t) => t.definition.name === toolName);
|
|
631
|
+
return registeredTool?.definition;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Set up keyboard shortcuts registered by extensions.
|
|
635
|
+
*/
|
|
636
|
+
setupExtensionShortcuts(extensionRunner) {
|
|
637
|
+
const shortcuts = extensionRunner.getShortcuts();
|
|
638
|
+
if (shortcuts.size === 0)
|
|
639
|
+
return;
|
|
640
|
+
// Create a context for shortcut handlers
|
|
641
|
+
const createContext = () => ({
|
|
642
|
+
ui: this.createExtensionUIContext(),
|
|
643
|
+
hasUI: true,
|
|
644
|
+
cwd: process.cwd(),
|
|
645
|
+
sessionManager: this.sessionManager,
|
|
646
|
+
modelRegistry: this.session.modelRegistry,
|
|
647
|
+
model: this.session.model,
|
|
648
|
+
isIdle: () => !this.session.isStreaming,
|
|
649
|
+
abort: () => this.session.abort(),
|
|
650
|
+
hasPendingMessages: () => this.session.pendingMessageCount > 0,
|
|
651
|
+
shutdown: () => {
|
|
652
|
+
this.shutdownRequested = true;
|
|
653
|
+
},
|
|
654
|
+
});
|
|
655
|
+
// Set up the extension shortcut handler on the default editor
|
|
656
|
+
this.defaultEditor.onExtensionShortcut = (data) => {
|
|
657
|
+
for (const [shortcutStr, shortcut] of shortcuts) {
|
|
658
|
+
// Cast to KeyId - extension shortcuts use the same format
|
|
659
|
+
if (matchesKey(data, shortcutStr)) {
|
|
660
|
+
// Run handler async, don't block input
|
|
661
|
+
Promise.resolve(shortcut.handler(createContext())).catch((err) => {
|
|
662
|
+
this.showError(`Shortcut handler error: ${err instanceof Error ? err.message : String(err)}`);
|
|
663
|
+
});
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return false;
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Set extension status text in the footer.
|
|
672
|
+
*/
|
|
673
|
+
setExtensionStatus(key, text) {
|
|
674
|
+
this.footerDataProvider.setExtensionStatus(key, text);
|
|
675
|
+
this.ui.requestRender();
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Set an extension widget (string array or custom component).
|
|
679
|
+
*/
|
|
680
|
+
setExtensionWidget(key, content) {
|
|
681
|
+
// Dispose and remove existing widget
|
|
682
|
+
const existing = this.extensionWidgets.get(key);
|
|
683
|
+
if (existing?.dispose)
|
|
684
|
+
existing.dispose();
|
|
685
|
+
if (content === undefined) {
|
|
686
|
+
this.extensionWidgets.delete(key);
|
|
687
|
+
}
|
|
688
|
+
else if (Array.isArray(content)) {
|
|
689
|
+
// Wrap string array in a Container with Text components
|
|
690
|
+
const container = new Container();
|
|
691
|
+
for (const line of content.slice(0, InteractiveMode.MAX_WIDGET_LINES)) {
|
|
692
|
+
container.addChild(new Text(line, 1, 0));
|
|
693
|
+
}
|
|
694
|
+
if (content.length > InteractiveMode.MAX_WIDGET_LINES) {
|
|
695
|
+
container.addChild(new Text(theme.fg("muted", "... (widget truncated)"), 1, 0));
|
|
696
|
+
}
|
|
697
|
+
this.extensionWidgets.set(key, container);
|
|
698
|
+
}
|
|
699
|
+
else {
|
|
700
|
+
// Factory function - create component
|
|
701
|
+
const component = content(this.ui, theme);
|
|
702
|
+
this.extensionWidgets.set(key, component);
|
|
703
|
+
}
|
|
704
|
+
this.renderWidgets();
|
|
705
|
+
}
|
|
706
|
+
// Maximum total widget lines to prevent viewport overflow
|
|
707
|
+
static MAX_WIDGET_LINES = 10;
|
|
708
|
+
/**
|
|
709
|
+
* Render all extension widgets to the widget container.
|
|
710
|
+
*/
|
|
711
|
+
renderWidgets() {
|
|
712
|
+
if (!this.widgetContainer)
|
|
713
|
+
return;
|
|
714
|
+
this.widgetContainer.clear();
|
|
715
|
+
if (this.extensionWidgets.size === 0) {
|
|
716
|
+
this.widgetContainer.addChild(new Spacer(1));
|
|
717
|
+
this.ui.requestRender();
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
this.widgetContainer.addChild(new Spacer(1));
|
|
721
|
+
for (const [_key, component] of this.extensionWidgets) {
|
|
722
|
+
this.widgetContainer.addChild(component);
|
|
723
|
+
}
|
|
724
|
+
this.ui.requestRender();
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Set a custom footer component, or restore the built-in footer.
|
|
728
|
+
*/
|
|
729
|
+
setExtensionFooter(factory) {
|
|
730
|
+
// Dispose existing custom footer
|
|
731
|
+
if (this.customFooter?.dispose) {
|
|
732
|
+
this.customFooter.dispose();
|
|
733
|
+
}
|
|
734
|
+
// Remove current footer from UI
|
|
735
|
+
if (this.customFooter) {
|
|
736
|
+
this.ui.removeChild(this.customFooter);
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
this.ui.removeChild(this.footer);
|
|
740
|
+
}
|
|
741
|
+
if (factory) {
|
|
742
|
+
// Create and add custom footer, passing the data provider
|
|
743
|
+
this.customFooter = factory(this.ui, theme, this.footerDataProvider);
|
|
744
|
+
this.ui.addChild(this.customFooter);
|
|
745
|
+
}
|
|
746
|
+
else {
|
|
747
|
+
// Restore built-in footer
|
|
748
|
+
this.customFooter = undefined;
|
|
749
|
+
this.ui.addChild(this.footer);
|
|
750
|
+
}
|
|
751
|
+
this.ui.requestRender();
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Set a custom header component, or restore the built-in header.
|
|
755
|
+
*/
|
|
756
|
+
setExtensionHeader(factory) {
|
|
757
|
+
// Header may not be initialized yet if called during early initialization
|
|
758
|
+
if (!this.builtInHeader) {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
// Dispose existing custom header
|
|
762
|
+
if (this.customHeader?.dispose) {
|
|
763
|
+
this.customHeader.dispose();
|
|
764
|
+
}
|
|
765
|
+
// Remove current header from UI
|
|
766
|
+
if (this.customHeader) {
|
|
767
|
+
this.ui.removeChild(this.customHeader);
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
this.ui.removeChild(this.builtInHeader);
|
|
771
|
+
}
|
|
772
|
+
if (factory) {
|
|
773
|
+
// Create and add custom header at position 1 (after initial spacer)
|
|
774
|
+
this.customHeader = factory(this.ui, theme);
|
|
775
|
+
this.ui.children.splice(1, 0, this.customHeader);
|
|
776
|
+
}
|
|
777
|
+
else {
|
|
778
|
+
// Restore built-in header at position 1
|
|
779
|
+
this.customHeader = undefined;
|
|
780
|
+
this.ui.children.splice(1, 0, this.builtInHeader);
|
|
781
|
+
}
|
|
782
|
+
this.ui.requestRender();
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Create the ExtensionUIContext for extensions.
|
|
786
|
+
*/
|
|
787
|
+
createExtensionUIContext() {
|
|
788
|
+
return {
|
|
789
|
+
select: (title, options, opts) => this.showExtensionSelector(title, options, opts),
|
|
790
|
+
confirm: (title, message, opts) => this.showExtensionConfirm(title, message, opts),
|
|
791
|
+
input: (title, placeholder, opts) => this.showExtensionInput(title, placeholder, opts),
|
|
792
|
+
notify: (message, type) => this.showExtensionNotify(message, type),
|
|
793
|
+
setStatus: (key, text) => this.setExtensionStatus(key, text),
|
|
794
|
+
setWorkingMessage: (message) => {
|
|
795
|
+
if (this.loadingAnimation) {
|
|
796
|
+
this.loadingAnimation.setMessage(message ?? this.defaultWorkingMessage);
|
|
797
|
+
}
|
|
798
|
+
},
|
|
799
|
+
setWidget: (key, content) => this.setExtensionWidget(key, content),
|
|
800
|
+
setFooter: (factory) => this.setExtensionFooter(factory),
|
|
801
|
+
setHeader: (factory) => this.setExtensionHeader(factory),
|
|
802
|
+
setTitle: (title) => this.ui.terminal.setTitle(title),
|
|
803
|
+
custom: (factory, options) => this.showExtensionCustom(factory, options),
|
|
804
|
+
setEditorText: (text) => this.editor.setText(text),
|
|
805
|
+
getEditorText: () => this.editor.getText(),
|
|
806
|
+
editor: (title, prefill) => this.showExtensionEditor(title, prefill),
|
|
807
|
+
setEditorComponent: (factory) => this.setCustomEditorComponent(factory),
|
|
808
|
+
get theme() {
|
|
809
|
+
return theme;
|
|
810
|
+
},
|
|
811
|
+
getAllThemes: () => getAvailableThemesWithPaths(),
|
|
812
|
+
getTheme: (name) => getThemeByName(name),
|
|
813
|
+
setTheme: (themeOrName) => {
|
|
814
|
+
if (themeOrName instanceof Theme) {
|
|
815
|
+
setThemeInstance(themeOrName);
|
|
816
|
+
this.ui.requestRender();
|
|
817
|
+
return { success: true };
|
|
818
|
+
}
|
|
819
|
+
const result = setTheme(themeOrName, true);
|
|
820
|
+
if (result.success) {
|
|
821
|
+
this.ui.requestRender();
|
|
822
|
+
}
|
|
823
|
+
return result;
|
|
824
|
+
},
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Show a selector for extensions.
|
|
829
|
+
*/
|
|
830
|
+
showExtensionSelector(title, options, opts) {
|
|
831
|
+
return new Promise((resolve) => {
|
|
832
|
+
if (opts?.signal?.aborted) {
|
|
833
|
+
resolve(undefined);
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
const onAbort = () => {
|
|
837
|
+
this.hideExtensionSelector();
|
|
838
|
+
resolve(undefined);
|
|
839
|
+
};
|
|
840
|
+
opts?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
841
|
+
this.extensionSelector = new ExtensionSelectorComponent(title, options, (option) => {
|
|
842
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
843
|
+
this.hideExtensionSelector();
|
|
844
|
+
resolve(option);
|
|
845
|
+
}, () => {
|
|
846
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
847
|
+
this.hideExtensionSelector();
|
|
848
|
+
resolve(undefined);
|
|
849
|
+
}, { tui: this.ui, timeout: opts?.timeout });
|
|
850
|
+
this.editorContainer.clear();
|
|
851
|
+
this.editorContainer.addChild(this.extensionSelector);
|
|
852
|
+
this.ui.setFocus(this.extensionSelector);
|
|
853
|
+
this.ui.requestRender();
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Hide the extension selector.
|
|
858
|
+
*/
|
|
859
|
+
hideExtensionSelector() {
|
|
860
|
+
this.extensionSelector?.dispose();
|
|
861
|
+
this.editorContainer.clear();
|
|
862
|
+
this.editorContainer.addChild(this.editor);
|
|
863
|
+
this.extensionSelector = undefined;
|
|
864
|
+
this.ui.setFocus(this.editor);
|
|
865
|
+
this.ui.requestRender();
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Show a confirmation dialog for extensions.
|
|
869
|
+
*/
|
|
870
|
+
async showExtensionConfirm(title, message, opts) {
|
|
871
|
+
const result = await this.showExtensionSelector(`${title}\n${message}`, ["Yes", "No"], opts);
|
|
872
|
+
return result === "Yes";
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Show a text input for extensions.
|
|
876
|
+
*/
|
|
877
|
+
showExtensionInput(title, placeholder, opts) {
|
|
878
|
+
return new Promise((resolve) => {
|
|
879
|
+
if (opts?.signal?.aborted) {
|
|
880
|
+
resolve(undefined);
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
const onAbort = () => {
|
|
884
|
+
this.hideExtensionInput();
|
|
885
|
+
resolve(undefined);
|
|
886
|
+
};
|
|
887
|
+
opts?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
888
|
+
this.extensionInput = new ExtensionInputComponent(title, placeholder, (value) => {
|
|
889
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
890
|
+
this.hideExtensionInput();
|
|
891
|
+
resolve(value);
|
|
892
|
+
}, () => {
|
|
893
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
894
|
+
this.hideExtensionInput();
|
|
895
|
+
resolve(undefined);
|
|
896
|
+
}, { tui: this.ui, timeout: opts?.timeout });
|
|
897
|
+
this.editorContainer.clear();
|
|
898
|
+
this.editorContainer.addChild(this.extensionInput);
|
|
899
|
+
this.ui.setFocus(this.extensionInput);
|
|
900
|
+
this.ui.requestRender();
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Hide the extension input.
|
|
905
|
+
*/
|
|
906
|
+
hideExtensionInput() {
|
|
907
|
+
this.extensionInput?.dispose();
|
|
908
|
+
this.editorContainer.clear();
|
|
909
|
+
this.editorContainer.addChild(this.editor);
|
|
910
|
+
this.extensionInput = undefined;
|
|
911
|
+
this.ui.setFocus(this.editor);
|
|
912
|
+
this.ui.requestRender();
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Show a multi-line editor for extensions (with Ctrl+G support).
|
|
916
|
+
*/
|
|
917
|
+
showExtensionEditor(title, prefill) {
|
|
918
|
+
return new Promise((resolve) => {
|
|
919
|
+
this.extensionEditor = new ExtensionEditorComponent(this.ui, title, prefill, (value) => {
|
|
920
|
+
this.hideExtensionEditor();
|
|
921
|
+
resolve(value);
|
|
922
|
+
}, () => {
|
|
923
|
+
this.hideExtensionEditor();
|
|
924
|
+
resolve(undefined);
|
|
925
|
+
});
|
|
926
|
+
this.editorContainer.clear();
|
|
927
|
+
this.editorContainer.addChild(this.extensionEditor);
|
|
928
|
+
this.ui.setFocus(this.extensionEditor);
|
|
929
|
+
this.ui.requestRender();
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* Hide the extension editor.
|
|
934
|
+
*/
|
|
935
|
+
hideExtensionEditor() {
|
|
936
|
+
this.editorContainer.clear();
|
|
937
|
+
this.editorContainer.addChild(this.editor);
|
|
938
|
+
this.extensionEditor = undefined;
|
|
939
|
+
this.ui.setFocus(this.editor);
|
|
940
|
+
this.ui.requestRender();
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Set a custom editor component from an extension.
|
|
944
|
+
* Pass undefined to restore the default editor.
|
|
945
|
+
*/
|
|
946
|
+
setCustomEditorComponent(factory) {
|
|
947
|
+
// Save text from current editor before switching
|
|
948
|
+
const currentText = this.editor.getText();
|
|
949
|
+
this.editorContainer.clear();
|
|
950
|
+
if (factory) {
|
|
951
|
+
// Create the custom editor with tui, theme, and keybindings
|
|
952
|
+
const newEditor = factory(this.ui, getEditorTheme(), this.keybindings);
|
|
953
|
+
// Wire up callbacks from the default editor
|
|
954
|
+
newEditor.onSubmit = this.defaultEditor.onSubmit;
|
|
955
|
+
newEditor.onChange = this.defaultEditor.onChange;
|
|
956
|
+
// Copy text from previous editor
|
|
957
|
+
newEditor.setText(currentText);
|
|
958
|
+
// Copy appearance settings if supported
|
|
959
|
+
if (newEditor.borderColor !== undefined) {
|
|
960
|
+
newEditor.borderColor = this.defaultEditor.borderColor;
|
|
961
|
+
}
|
|
962
|
+
// Set autocomplete if supported
|
|
963
|
+
if (newEditor.setAutocompleteProvider && this.autocompleteProvider) {
|
|
964
|
+
newEditor.setAutocompleteProvider(this.autocompleteProvider);
|
|
965
|
+
}
|
|
966
|
+
// If extending CustomEditor, copy app-level handlers
|
|
967
|
+
// Use duck typing since instanceof fails across jiti module boundaries
|
|
968
|
+
const customEditor = newEditor;
|
|
969
|
+
if ("actionHandlers" in customEditor && customEditor.actionHandlers instanceof Map) {
|
|
970
|
+
customEditor.onEscape = this.defaultEditor.onEscape;
|
|
971
|
+
customEditor.onCtrlD = this.defaultEditor.onCtrlD;
|
|
972
|
+
customEditor.onPasteImage = this.defaultEditor.onPasteImage;
|
|
973
|
+
customEditor.onExtensionShortcut = this.defaultEditor.onExtensionShortcut;
|
|
974
|
+
// Copy action handlers (clear, suspend, model switching, etc.)
|
|
975
|
+
for (const [action, handler] of this.defaultEditor.actionHandlers) {
|
|
976
|
+
customEditor.actionHandlers.set(action, handler);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
this.editor = newEditor;
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
// Restore default editor with text from custom editor
|
|
983
|
+
this.defaultEditor.setText(currentText);
|
|
984
|
+
this.editor = this.defaultEditor;
|
|
985
|
+
}
|
|
986
|
+
this.editorContainer.addChild(this.editor);
|
|
987
|
+
this.ui.setFocus(this.editor);
|
|
988
|
+
this.ui.requestRender();
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Show a notification for extensions.
|
|
992
|
+
*/
|
|
993
|
+
showExtensionNotify(message, type) {
|
|
994
|
+
if (type === "error") {
|
|
995
|
+
this.showError(message);
|
|
996
|
+
}
|
|
997
|
+
else if (type === "warning") {
|
|
998
|
+
this.showWarning(message);
|
|
999
|
+
}
|
|
1000
|
+
else {
|
|
1001
|
+
this.showStatus(message);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
/** Show a custom component with keyboard focus. Overlay mode renders on top of existing content. */
|
|
1005
|
+
async showExtensionCustom(factory, options) {
|
|
1006
|
+
const savedText = this.editor.getText();
|
|
1007
|
+
const isOverlay = options?.overlay ?? false;
|
|
1008
|
+
const restoreEditor = () => {
|
|
1009
|
+
this.editorContainer.clear();
|
|
1010
|
+
this.editorContainer.addChild(this.editor);
|
|
1011
|
+
this.editor.setText(savedText);
|
|
1012
|
+
this.ui.setFocus(this.editor);
|
|
1013
|
+
this.ui.requestRender();
|
|
1014
|
+
};
|
|
1015
|
+
return new Promise((resolve, reject) => {
|
|
1016
|
+
let component;
|
|
1017
|
+
let closed = false;
|
|
1018
|
+
const close = (result) => {
|
|
1019
|
+
if (closed)
|
|
1020
|
+
return;
|
|
1021
|
+
closed = true;
|
|
1022
|
+
if (isOverlay)
|
|
1023
|
+
this.ui.hideOverlay();
|
|
1024
|
+
else
|
|
1025
|
+
restoreEditor();
|
|
1026
|
+
// Note: both branches above already call requestRender
|
|
1027
|
+
resolve(result);
|
|
1028
|
+
try {
|
|
1029
|
+
component?.dispose?.();
|
|
1030
|
+
}
|
|
1031
|
+
catch {
|
|
1032
|
+
/* ignore dispose errors */
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
Promise.resolve(factory(this.ui, theme, this.keybindings, close))
|
|
1036
|
+
.then((c) => {
|
|
1037
|
+
if (closed)
|
|
1038
|
+
return;
|
|
1039
|
+
component = c;
|
|
1040
|
+
if (isOverlay) {
|
|
1041
|
+
// Resolve overlay options - can be static or dynamic function
|
|
1042
|
+
const resolveOptions = () => {
|
|
1043
|
+
if (options?.overlayOptions) {
|
|
1044
|
+
const opts = typeof options.overlayOptions === "function"
|
|
1045
|
+
? options.overlayOptions()
|
|
1046
|
+
: options.overlayOptions;
|
|
1047
|
+
return opts;
|
|
1048
|
+
}
|
|
1049
|
+
// Fallback: use component's width property if available
|
|
1050
|
+
const w = component.width;
|
|
1051
|
+
return w ? { width: w } : undefined;
|
|
1052
|
+
};
|
|
1053
|
+
const handle = this.ui.showOverlay(component, resolveOptions());
|
|
1054
|
+
// Expose handle to caller for visibility control
|
|
1055
|
+
options?.onHandle?.(handle);
|
|
1056
|
+
}
|
|
1057
|
+
else {
|
|
1058
|
+
this.editorContainer.clear();
|
|
1059
|
+
this.editorContainer.addChild(component);
|
|
1060
|
+
this.ui.setFocus(component);
|
|
1061
|
+
this.ui.requestRender();
|
|
1062
|
+
}
|
|
1063
|
+
})
|
|
1064
|
+
.catch((err) => {
|
|
1065
|
+
if (closed)
|
|
1066
|
+
return;
|
|
1067
|
+
if (!isOverlay)
|
|
1068
|
+
restoreEditor();
|
|
1069
|
+
reject(err);
|
|
1070
|
+
});
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Show an extension error in the UI.
|
|
1075
|
+
*/
|
|
1076
|
+
showExtensionError(extensionPath, error, stack) {
|
|
1077
|
+
const errorMsg = `Extension "${extensionPath}" error: ${error}`;
|
|
1078
|
+
const errorText = new Text(theme.fg("error", errorMsg), 1, 0);
|
|
1079
|
+
this.chatContainer.addChild(errorText);
|
|
1080
|
+
if (stack) {
|
|
1081
|
+
// Show stack trace in dim color, indented
|
|
1082
|
+
const stackLines = stack
|
|
1083
|
+
.split("\n")
|
|
1084
|
+
.slice(1) // Skip first line (duplicates error message)
|
|
1085
|
+
.map((line) => theme.fg("dim", ` ${line.trim()}`))
|
|
1086
|
+
.join("\n");
|
|
1087
|
+
if (stackLines) {
|
|
1088
|
+
this.chatContainer.addChild(new Text(stackLines, 1, 0));
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
this.ui.requestRender();
|
|
1092
|
+
}
|
|
1093
|
+
// =========================================================================
|
|
1094
|
+
// Key Handlers
|
|
1095
|
+
// =========================================================================
|
|
1096
|
+
setupKeyHandlers() {
|
|
1097
|
+
// Set up handlers on defaultEditor - they use this.editor for text access
|
|
1098
|
+
// so they work correctly regardless of which editor is active
|
|
1099
|
+
this.defaultEditor.onEscape = () => {
|
|
1100
|
+
if (this.loadingAnimation) {
|
|
1101
|
+
this.restoreQueuedMessagesToEditor({ abort: true });
|
|
1102
|
+
}
|
|
1103
|
+
else if (this.session.isBashRunning) {
|
|
1104
|
+
this.session.abortBash();
|
|
1105
|
+
}
|
|
1106
|
+
else if (this.isBashMode) {
|
|
1107
|
+
this.editor.setText("");
|
|
1108
|
+
this.isBashMode = false;
|
|
1109
|
+
this.updateEditorBorderColor();
|
|
1110
|
+
}
|
|
1111
|
+
else if (!this.editor.getText().trim()) {
|
|
1112
|
+
// Double-escape with empty editor triggers /tree or /fork based on setting
|
|
1113
|
+
const now = Date.now();
|
|
1114
|
+
if (now - this.lastEscapeTime < 500) {
|
|
1115
|
+
if (this.settingsManager.getDoubleEscapeAction() === "tree") {
|
|
1116
|
+
this.showTreeSelector();
|
|
1117
|
+
}
|
|
1118
|
+
else {
|
|
1119
|
+
this.showUserMessageSelector();
|
|
1120
|
+
}
|
|
1121
|
+
this.lastEscapeTime = 0;
|
|
1122
|
+
}
|
|
1123
|
+
else {
|
|
1124
|
+
this.lastEscapeTime = now;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
};
|
|
1128
|
+
// Register app action handlers
|
|
1129
|
+
this.defaultEditor.onAction("clear", () => this.handleCtrlC());
|
|
1130
|
+
this.defaultEditor.onCtrlD = () => this.handleCtrlD();
|
|
1131
|
+
this.defaultEditor.onAction("suspend", () => this.handleCtrlZ());
|
|
1132
|
+
this.defaultEditor.onAction("cycleThinkingLevel", () => this.cycleThinkingLevel());
|
|
1133
|
+
this.defaultEditor.onAction("cycleModelForward", () => this.cycleModel("forward"));
|
|
1134
|
+
this.defaultEditor.onAction("cycleModelBackward", () => this.cycleModel("backward"));
|
|
1135
|
+
// Global debug handler on TUI (works regardless of focus)
|
|
1136
|
+
this.ui.onDebug = () => this.handleDebugCommand();
|
|
1137
|
+
this.defaultEditor.onAction("selectModel", () => this.showModelSelector());
|
|
1138
|
+
this.defaultEditor.onAction("expandTools", () => this.toggleToolOutputExpansion());
|
|
1139
|
+
this.defaultEditor.onAction("toggleThinking", () => this.toggleThinkingBlockVisibility());
|
|
1140
|
+
this.defaultEditor.onAction("externalEditor", () => this.openExternalEditor());
|
|
1141
|
+
this.defaultEditor.onAction("followUp", () => this.handleFollowUp());
|
|
1142
|
+
this.defaultEditor.onAction("dequeue", () => this.handleDequeue());
|
|
1143
|
+
this.defaultEditor.onChange = (text) => {
|
|
1144
|
+
const wasBashMode = this.isBashMode;
|
|
1145
|
+
this.isBashMode = text.trimStart().startsWith("!");
|
|
1146
|
+
if (wasBashMode !== this.isBashMode) {
|
|
1147
|
+
this.updateEditorBorderColor();
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
// Handle clipboard image paste (triggered on Ctrl+V)
|
|
1151
|
+
this.defaultEditor.onPasteImage = () => {
|
|
1152
|
+
this.handleClipboardImagePaste();
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
async handleClipboardImagePaste() {
|
|
1156
|
+
try {
|
|
1157
|
+
const image = await readClipboardImage();
|
|
1158
|
+
if (!image) {
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
// Write to temp file
|
|
1162
|
+
const tmpDir = os.tmpdir();
|
|
1163
|
+
const ext = extensionForImageMimeType(image.mimeType) ?? "png";
|
|
1164
|
+
const fileName = `pi-clipboard-${crypto.randomUUID()}.${ext}`;
|
|
1165
|
+
const filePath = path.join(tmpDir, fileName);
|
|
1166
|
+
fs.writeFileSync(filePath, Buffer.from(image.bytes));
|
|
1167
|
+
// Insert file path directly
|
|
1168
|
+
this.editor.insertTextAtCursor?.(filePath);
|
|
1169
|
+
this.ui.requestRender();
|
|
1170
|
+
}
|
|
1171
|
+
catch {
|
|
1172
|
+
// Silently ignore clipboard errors (may not have permission, etc.)
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
setupEditorSubmitHandler() {
|
|
1176
|
+
this.defaultEditor.onSubmit = async (text) => {
|
|
1177
|
+
text = text.trim();
|
|
1178
|
+
if (!text)
|
|
1179
|
+
return;
|
|
1180
|
+
// Handle commands
|
|
1181
|
+
if (text === "/settings") {
|
|
1182
|
+
this.showSettingsSelector();
|
|
1183
|
+
this.editor.setText("");
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
if (text === "/scoped-models") {
|
|
1187
|
+
this.editor.setText("");
|
|
1188
|
+
await this.showModelsSelector();
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
if (text === "/model" || text.startsWith("/model ")) {
|
|
1192
|
+
const searchTerm = text.startsWith("/model ") ? text.slice(7).trim() : undefined;
|
|
1193
|
+
this.editor.setText("");
|
|
1194
|
+
await this.handleModelCommand(searchTerm);
|
|
1195
|
+
return;
|
|
1196
|
+
}
|
|
1197
|
+
if (text.startsWith("/export")) {
|
|
1198
|
+
await this.handleExportCommand(text);
|
|
1199
|
+
this.editor.setText("");
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
if (text === "/share") {
|
|
1203
|
+
await this.handleShareCommand();
|
|
1204
|
+
this.editor.setText("");
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
if (text === "/copy") {
|
|
1208
|
+
this.handleCopyCommand();
|
|
1209
|
+
this.editor.setText("");
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
if (text === "/name" || text.startsWith("/name ")) {
|
|
1213
|
+
this.handleNameCommand(text);
|
|
1214
|
+
this.editor.setText("");
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
if (text === "/session") {
|
|
1218
|
+
this.handleSessionCommand();
|
|
1219
|
+
this.editor.setText("");
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
if (text === "/changelog") {
|
|
1223
|
+
this.handleChangelogCommand();
|
|
1224
|
+
this.editor.setText("");
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
if (text === "/hotkeys") {
|
|
1228
|
+
this.handleHotkeysCommand();
|
|
1229
|
+
this.editor.setText("");
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
if (text === "/fork") {
|
|
1233
|
+
this.showUserMessageSelector();
|
|
1234
|
+
this.editor.setText("");
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
1237
|
+
if (text === "/tree") {
|
|
1238
|
+
this.showTreeSelector();
|
|
1239
|
+
this.editor.setText("");
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
if (text === "/login") {
|
|
1243
|
+
this.showOAuthSelector("login");
|
|
1244
|
+
this.editor.setText("");
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1247
|
+
if (text === "/logout") {
|
|
1248
|
+
this.showOAuthSelector("logout");
|
|
1249
|
+
this.editor.setText("");
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
if (text === "/new") {
|
|
1253
|
+
this.editor.setText("");
|
|
1254
|
+
await this.handleClearCommand();
|
|
1255
|
+
return;
|
|
1256
|
+
}
|
|
1257
|
+
if (text === "/compact" || text.startsWith("/compact ")) {
|
|
1258
|
+
const customInstructions = text.startsWith("/compact ") ? text.slice(9).trim() : undefined;
|
|
1259
|
+
this.editor.setText("");
|
|
1260
|
+
await this.handleCompactCommand(customInstructions);
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
if (text === "/debug") {
|
|
1264
|
+
this.handleDebugCommand();
|
|
1265
|
+
this.editor.setText("");
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
if (text === "/arminsayshi") {
|
|
1269
|
+
this.handleArminSaysHi();
|
|
1270
|
+
this.editor.setText("");
|
|
1271
|
+
return;
|
|
1272
|
+
}
|
|
1273
|
+
if (text === "/resume") {
|
|
1274
|
+
this.showSessionSelector();
|
|
1275
|
+
this.editor.setText("");
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
if (text === "/quit" || text === "/exit") {
|
|
1279
|
+
this.editor.setText("");
|
|
1280
|
+
await this.shutdown();
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
// Handle skill commands (/skill:name [args])
|
|
1284
|
+
if (text.startsWith("/skill:")) {
|
|
1285
|
+
const spaceIndex = text.indexOf(" ");
|
|
1286
|
+
const commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
|
|
1287
|
+
const args = spaceIndex === -1 ? "" : text.slice(spaceIndex + 1).trim();
|
|
1288
|
+
const skillPath = this.skillCommands.get(commandName);
|
|
1289
|
+
if (skillPath) {
|
|
1290
|
+
this.editor.addToHistory?.(text);
|
|
1291
|
+
this.editor.setText("");
|
|
1292
|
+
await this.handleSkillCommand(skillPath, args);
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
// Handle bash command (! for normal, !! for excluded from context)
|
|
1297
|
+
if (text.startsWith("!")) {
|
|
1298
|
+
const isExcluded = text.startsWith("!!");
|
|
1299
|
+
const command = isExcluded ? text.slice(2).trim() : text.slice(1).trim();
|
|
1300
|
+
if (command) {
|
|
1301
|
+
if (this.session.isBashRunning) {
|
|
1302
|
+
this.showWarning("A bash command is already running. Press Esc to cancel it first.");
|
|
1303
|
+
this.editor.setText(text);
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
this.editor.addToHistory?.(text);
|
|
1307
|
+
await this.handleBashCommand(command, isExcluded);
|
|
1308
|
+
this.isBashMode = false;
|
|
1309
|
+
this.updateEditorBorderColor();
|
|
1310
|
+
return;
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
// Queue input during compaction (extension commands execute immediately)
|
|
1314
|
+
if (this.session.isCompacting) {
|
|
1315
|
+
if (this.isExtensionCommand(text)) {
|
|
1316
|
+
this.editor.addToHistory?.(text);
|
|
1317
|
+
this.editor.setText("");
|
|
1318
|
+
await this.session.prompt(text);
|
|
1319
|
+
}
|
|
1320
|
+
else {
|
|
1321
|
+
this.queueCompactionMessage(text, "steer");
|
|
1322
|
+
}
|
|
1323
|
+
return;
|
|
1324
|
+
}
|
|
1325
|
+
// If streaming, use prompt() with steer behavior
|
|
1326
|
+
// This handles extension commands (execute immediately), prompt template expansion, and queueing
|
|
1327
|
+
if (this.session.isStreaming) {
|
|
1328
|
+
this.editor.addToHistory?.(text);
|
|
1329
|
+
this.editor.setText("");
|
|
1330
|
+
await this.session.prompt(text, { streamingBehavior: "steer" });
|
|
1331
|
+
this.updatePendingMessagesDisplay();
|
|
1332
|
+
this.ui.requestRender();
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
// Normal message submission
|
|
1336
|
+
// First, move any pending bash components to chat
|
|
1337
|
+
this.flushPendingBashComponents();
|
|
1338
|
+
if (this.onInputCallback) {
|
|
1339
|
+
this.onInputCallback(text);
|
|
1340
|
+
}
|
|
1341
|
+
this.editor.addToHistory?.(text);
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
subscribeToAgent() {
|
|
1345
|
+
this.unsubscribe = this.session.subscribe(async (event) => {
|
|
1346
|
+
await this.handleEvent(event);
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
async handleEvent(event) {
|
|
1350
|
+
if (!this.isInitialized) {
|
|
1351
|
+
await this.init();
|
|
1352
|
+
}
|
|
1353
|
+
this.footer.invalidate();
|
|
1354
|
+
switch (event.type) {
|
|
1355
|
+
case "agent_start":
|
|
1356
|
+
// Restore main escape handler if retry handler is still active
|
|
1357
|
+
// (retry success event fires later, but we need main handler now)
|
|
1358
|
+
if (this.retryEscapeHandler) {
|
|
1359
|
+
this.defaultEditor.onEscape = this.retryEscapeHandler;
|
|
1360
|
+
this.retryEscapeHandler = undefined;
|
|
1361
|
+
}
|
|
1362
|
+
if (this.retryLoader) {
|
|
1363
|
+
this.retryLoader.stop();
|
|
1364
|
+
this.retryLoader = undefined;
|
|
1365
|
+
}
|
|
1366
|
+
if (this.loadingAnimation) {
|
|
1367
|
+
this.loadingAnimation.stop();
|
|
1368
|
+
}
|
|
1369
|
+
this.statusContainer.clear();
|
|
1370
|
+
this.loadingAnimation = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), this.defaultWorkingMessage);
|
|
1371
|
+
this.statusContainer.addChild(this.loadingAnimation);
|
|
1372
|
+
this.ui.requestRender();
|
|
1373
|
+
break;
|
|
1374
|
+
case "message_start":
|
|
1375
|
+
if (event.message.role === "custom") {
|
|
1376
|
+
this.addMessageToChat(event.message);
|
|
1377
|
+
this.ui.requestRender();
|
|
1378
|
+
}
|
|
1379
|
+
else if (event.message.role === "user") {
|
|
1380
|
+
this.addMessageToChat(event.message);
|
|
1381
|
+
this.updatePendingMessagesDisplay();
|
|
1382
|
+
this.ui.requestRender();
|
|
1383
|
+
}
|
|
1384
|
+
else if (event.message.role === "assistant") {
|
|
1385
|
+
this.streamingComponent = new AssistantMessageComponent(undefined, this.hideThinkingBlock);
|
|
1386
|
+
this.streamingMessage = event.message;
|
|
1387
|
+
this.chatContainer.addChild(this.streamingComponent);
|
|
1388
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
1389
|
+
this.ui.requestRender();
|
|
1390
|
+
}
|
|
1391
|
+
break;
|
|
1392
|
+
case "message_update":
|
|
1393
|
+
if (this.streamingComponent && event.message.role === "assistant") {
|
|
1394
|
+
this.streamingMessage = event.message;
|
|
1395
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
1396
|
+
for (const content of this.streamingMessage.content) {
|
|
1397
|
+
if (content.type === "toolCall") {
|
|
1398
|
+
if (!this.pendingTools.has(content.id)) {
|
|
1399
|
+
this.chatContainer.addChild(new Text("", 0, 0));
|
|
1400
|
+
const component = new ToolExecutionComponent(content.name, content.arguments, {
|
|
1401
|
+
showImages: this.settingsManager.getShowImages(),
|
|
1402
|
+
}, this.getRegisteredToolDefinition(content.name), this.ui);
|
|
1403
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
1404
|
+
this.chatContainer.addChild(component);
|
|
1405
|
+
this.pendingTools.set(content.id, component);
|
|
1406
|
+
}
|
|
1407
|
+
else {
|
|
1408
|
+
const component = this.pendingTools.get(content.id);
|
|
1409
|
+
if (component) {
|
|
1410
|
+
component.updateArgs(content.arguments);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
this.ui.requestRender();
|
|
1416
|
+
}
|
|
1417
|
+
break;
|
|
1418
|
+
case "message_end":
|
|
1419
|
+
if (event.message.role === "user")
|
|
1420
|
+
break;
|
|
1421
|
+
if (this.streamingComponent && event.message.role === "assistant") {
|
|
1422
|
+
this.streamingMessage = event.message;
|
|
1423
|
+
let errorMessage;
|
|
1424
|
+
if (this.streamingMessage.stopReason === "aborted") {
|
|
1425
|
+
const retryAttempt = this.session.retryAttempt;
|
|
1426
|
+
errorMessage =
|
|
1427
|
+
retryAttempt > 0
|
|
1428
|
+
? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
|
|
1429
|
+
: "Operation aborted";
|
|
1430
|
+
this.streamingMessage.errorMessage = errorMessage;
|
|
1431
|
+
}
|
|
1432
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
1433
|
+
if (this.streamingMessage.stopReason === "aborted" || this.streamingMessage.stopReason === "error") {
|
|
1434
|
+
if (!errorMessage) {
|
|
1435
|
+
errorMessage = this.streamingMessage.errorMessage || "Error";
|
|
1436
|
+
}
|
|
1437
|
+
for (const [, component] of this.pendingTools.entries()) {
|
|
1438
|
+
component.updateResult({
|
|
1439
|
+
content: [{ type: "text", text: errorMessage }],
|
|
1440
|
+
isError: true,
|
|
1441
|
+
});
|
|
1442
|
+
}
|
|
1443
|
+
this.pendingTools.clear();
|
|
1444
|
+
}
|
|
1445
|
+
else {
|
|
1446
|
+
// Args are now complete - trigger diff computation for edit tools
|
|
1447
|
+
for (const [, component] of this.pendingTools.entries()) {
|
|
1448
|
+
component.setArgsComplete();
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
this.streamingComponent = undefined;
|
|
1452
|
+
this.streamingMessage = undefined;
|
|
1453
|
+
this.footer.invalidate();
|
|
1454
|
+
}
|
|
1455
|
+
this.ui.requestRender();
|
|
1456
|
+
break;
|
|
1457
|
+
case "tool_execution_start": {
|
|
1458
|
+
if (!this.pendingTools.has(event.toolCallId)) {
|
|
1459
|
+
const component = new ToolExecutionComponent(event.toolName, event.args, {
|
|
1460
|
+
showImages: this.settingsManager.getShowImages(),
|
|
1461
|
+
}, this.getRegisteredToolDefinition(event.toolName), this.ui);
|
|
1462
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
1463
|
+
this.chatContainer.addChild(component);
|
|
1464
|
+
this.pendingTools.set(event.toolCallId, component);
|
|
1465
|
+
this.ui.requestRender();
|
|
1466
|
+
}
|
|
1467
|
+
break;
|
|
1468
|
+
}
|
|
1469
|
+
case "tool_execution_update": {
|
|
1470
|
+
const component = this.pendingTools.get(event.toolCallId);
|
|
1471
|
+
if (component) {
|
|
1472
|
+
component.updateResult({ ...event.partialResult, isError: false }, true);
|
|
1473
|
+
this.ui.requestRender();
|
|
1474
|
+
}
|
|
1475
|
+
break;
|
|
1476
|
+
}
|
|
1477
|
+
case "tool_execution_end": {
|
|
1478
|
+
const component = this.pendingTools.get(event.toolCallId);
|
|
1479
|
+
if (component) {
|
|
1480
|
+
component.updateResult({ ...event.result, isError: event.isError });
|
|
1481
|
+
this.pendingTools.delete(event.toolCallId);
|
|
1482
|
+
this.ui.requestRender();
|
|
1483
|
+
}
|
|
1484
|
+
break;
|
|
1485
|
+
}
|
|
1486
|
+
case "agent_end":
|
|
1487
|
+
if (this.loadingAnimation) {
|
|
1488
|
+
this.loadingAnimation.stop();
|
|
1489
|
+
this.loadingAnimation = undefined;
|
|
1490
|
+
this.statusContainer.clear();
|
|
1491
|
+
}
|
|
1492
|
+
if (this.streamingComponent) {
|
|
1493
|
+
this.chatContainer.removeChild(this.streamingComponent);
|
|
1494
|
+
this.streamingComponent = undefined;
|
|
1495
|
+
this.streamingMessage = undefined;
|
|
1496
|
+
}
|
|
1497
|
+
this.pendingTools.clear();
|
|
1498
|
+
await this.checkShutdownRequested();
|
|
1499
|
+
this.ui.requestRender();
|
|
1500
|
+
break;
|
|
1501
|
+
case "auto_compaction_start": {
|
|
1502
|
+
// Keep editor active; submissions are queued during compaction.
|
|
1503
|
+
// Set up escape to abort auto-compaction
|
|
1504
|
+
this.autoCompactionEscapeHandler = this.defaultEditor.onEscape;
|
|
1505
|
+
this.defaultEditor.onEscape = () => {
|
|
1506
|
+
this.session.abortCompaction();
|
|
1507
|
+
};
|
|
1508
|
+
// Show compacting indicator with reason
|
|
1509
|
+
this.statusContainer.clear();
|
|
1510
|
+
const reasonText = event.reason === "overflow" ? "Context overflow detected, " : "";
|
|
1511
|
+
this.autoCompactionLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), `${reasonText}Auto-compacting... (esc to cancel)`);
|
|
1512
|
+
this.statusContainer.addChild(this.autoCompactionLoader);
|
|
1513
|
+
this.ui.requestRender();
|
|
1514
|
+
break;
|
|
1515
|
+
}
|
|
1516
|
+
case "auto_compaction_end": {
|
|
1517
|
+
// Restore escape handler
|
|
1518
|
+
if (this.autoCompactionEscapeHandler) {
|
|
1519
|
+
this.defaultEditor.onEscape = this.autoCompactionEscapeHandler;
|
|
1520
|
+
this.autoCompactionEscapeHandler = undefined;
|
|
1521
|
+
}
|
|
1522
|
+
// Stop loader
|
|
1523
|
+
if (this.autoCompactionLoader) {
|
|
1524
|
+
this.autoCompactionLoader.stop();
|
|
1525
|
+
this.autoCompactionLoader = undefined;
|
|
1526
|
+
this.statusContainer.clear();
|
|
1527
|
+
}
|
|
1528
|
+
// Handle result
|
|
1529
|
+
if (event.aborted) {
|
|
1530
|
+
this.showStatus("Auto-compaction cancelled");
|
|
1531
|
+
}
|
|
1532
|
+
else if (event.result) {
|
|
1533
|
+
// Rebuild chat to show compacted state
|
|
1534
|
+
this.chatContainer.clear();
|
|
1535
|
+
this.rebuildChatFromMessages();
|
|
1536
|
+
// Add compaction component at bottom so user sees it without scrolling
|
|
1537
|
+
this.addMessageToChat({
|
|
1538
|
+
role: "compactionSummary",
|
|
1539
|
+
tokensBefore: event.result.tokensBefore,
|
|
1540
|
+
summary: event.result.summary,
|
|
1541
|
+
timestamp: Date.now(),
|
|
1542
|
+
});
|
|
1543
|
+
this.footer.invalidate();
|
|
1544
|
+
}
|
|
1545
|
+
void this.flushCompactionQueue({ willRetry: event.willRetry });
|
|
1546
|
+
this.ui.requestRender();
|
|
1547
|
+
break;
|
|
1548
|
+
}
|
|
1549
|
+
case "auto_retry_start": {
|
|
1550
|
+
// Set up escape to abort retry
|
|
1551
|
+
this.retryEscapeHandler = this.defaultEditor.onEscape;
|
|
1552
|
+
this.defaultEditor.onEscape = () => {
|
|
1553
|
+
this.session.abortRetry();
|
|
1554
|
+
};
|
|
1555
|
+
// Show retry indicator
|
|
1556
|
+
this.statusContainer.clear();
|
|
1557
|
+
const delaySeconds = Math.round(event.delayMs / 1000);
|
|
1558
|
+
this.retryLoader = new Loader(this.ui, (spinner) => theme.fg("warning", spinner), (text) => theme.fg("muted", text), `Retrying (${event.attempt}/${event.maxAttempts}) in ${delaySeconds}s... (esc to cancel)`);
|
|
1559
|
+
this.statusContainer.addChild(this.retryLoader);
|
|
1560
|
+
this.ui.requestRender();
|
|
1561
|
+
break;
|
|
1562
|
+
}
|
|
1563
|
+
case "auto_retry_end": {
|
|
1564
|
+
// Restore escape handler
|
|
1565
|
+
if (this.retryEscapeHandler) {
|
|
1566
|
+
this.defaultEditor.onEscape = this.retryEscapeHandler;
|
|
1567
|
+
this.retryEscapeHandler = undefined;
|
|
1568
|
+
}
|
|
1569
|
+
// Stop loader
|
|
1570
|
+
if (this.retryLoader) {
|
|
1571
|
+
this.retryLoader.stop();
|
|
1572
|
+
this.retryLoader = undefined;
|
|
1573
|
+
this.statusContainer.clear();
|
|
1574
|
+
}
|
|
1575
|
+
// Show error only on final failure (success shows normal response)
|
|
1576
|
+
if (!event.success) {
|
|
1577
|
+
this.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || "Unknown error"}`);
|
|
1578
|
+
}
|
|
1579
|
+
this.ui.requestRender();
|
|
1580
|
+
break;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
/** Extract text content from a user message */
|
|
1585
|
+
getUserMessageText(message) {
|
|
1586
|
+
if (message.role !== "user")
|
|
1587
|
+
return "";
|
|
1588
|
+
const textBlocks = typeof message.content === "string"
|
|
1589
|
+
? [{ type: "text", text: message.content }]
|
|
1590
|
+
: message.content.filter((c) => c.type === "text");
|
|
1591
|
+
return textBlocks.map((c) => c.text).join("");
|
|
1592
|
+
}
|
|
1593
|
+
/**
|
|
1594
|
+
* Show a status message in the chat.
|
|
1595
|
+
*
|
|
1596
|
+
* If multiple status messages are emitted back-to-back (without anything else being added to the chat),
|
|
1597
|
+
* we update the previous status line instead of appending new ones to avoid log spam.
|
|
1598
|
+
*/
|
|
1599
|
+
showStatus(message) {
|
|
1600
|
+
const children = this.chatContainer.children;
|
|
1601
|
+
const last = children.length > 0 ? children[children.length - 1] : undefined;
|
|
1602
|
+
const secondLast = children.length > 1 ? children[children.length - 2] : undefined;
|
|
1603
|
+
if (last && secondLast && last === this.lastStatusText && secondLast === this.lastStatusSpacer) {
|
|
1604
|
+
this.lastStatusText.setText(theme.fg("dim", message));
|
|
1605
|
+
this.ui.requestRender();
|
|
1606
|
+
return;
|
|
1607
|
+
}
|
|
1608
|
+
const spacer = new Spacer(1);
|
|
1609
|
+
const text = new Text(theme.fg("dim", message), 1, 0);
|
|
1610
|
+
this.chatContainer.addChild(spacer);
|
|
1611
|
+
this.chatContainer.addChild(text);
|
|
1612
|
+
this.lastStatusSpacer = spacer;
|
|
1613
|
+
this.lastStatusText = text;
|
|
1614
|
+
this.ui.requestRender();
|
|
1615
|
+
}
|
|
1616
|
+
addMessageToChat(message, options) {
|
|
1617
|
+
switch (message.role) {
|
|
1618
|
+
case "bashExecution": {
|
|
1619
|
+
const component = new BashExecutionComponent(message.command, this.ui, message.excludeFromContext);
|
|
1620
|
+
if (message.output) {
|
|
1621
|
+
component.appendOutput(message.output);
|
|
1622
|
+
}
|
|
1623
|
+
component.setComplete(message.exitCode, message.cancelled, message.truncated ? { truncated: true } : undefined, message.fullOutputPath);
|
|
1624
|
+
this.chatContainer.addChild(component);
|
|
1625
|
+
break;
|
|
1626
|
+
}
|
|
1627
|
+
case "custom": {
|
|
1628
|
+
if (message.display) {
|
|
1629
|
+
const renderer = this.session.extensionRunner?.getMessageRenderer(message.customType);
|
|
1630
|
+
this.chatContainer.addChild(new CustomMessageComponent(message, renderer));
|
|
1631
|
+
}
|
|
1632
|
+
break;
|
|
1633
|
+
}
|
|
1634
|
+
case "compactionSummary": {
|
|
1635
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1636
|
+
const component = new CompactionSummaryMessageComponent(message);
|
|
1637
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
1638
|
+
this.chatContainer.addChild(component);
|
|
1639
|
+
break;
|
|
1640
|
+
}
|
|
1641
|
+
case "branchSummary": {
|
|
1642
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1643
|
+
const component = new BranchSummaryMessageComponent(message);
|
|
1644
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
1645
|
+
this.chatContainer.addChild(component);
|
|
1646
|
+
break;
|
|
1647
|
+
}
|
|
1648
|
+
case "user": {
|
|
1649
|
+
const textContent = this.getUserMessageText(message);
|
|
1650
|
+
if (textContent) {
|
|
1651
|
+
const userComponent = new UserMessageComponent(textContent);
|
|
1652
|
+
this.chatContainer.addChild(userComponent);
|
|
1653
|
+
if (options?.populateHistory) {
|
|
1654
|
+
this.editor.addToHistory?.(textContent);
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
break;
|
|
1658
|
+
}
|
|
1659
|
+
case "assistant": {
|
|
1660
|
+
const assistantComponent = new AssistantMessageComponent(message, this.hideThinkingBlock);
|
|
1661
|
+
this.chatContainer.addChild(assistantComponent);
|
|
1662
|
+
break;
|
|
1663
|
+
}
|
|
1664
|
+
case "toolResult": {
|
|
1665
|
+
// Tool results are rendered inline with tool calls, handled separately
|
|
1666
|
+
break;
|
|
1667
|
+
}
|
|
1668
|
+
default: {
|
|
1669
|
+
const _exhaustive = message;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
/**
|
|
1674
|
+
* Render session context to chat. Used for initial load and rebuild after compaction.
|
|
1675
|
+
* @param sessionContext Session context to render
|
|
1676
|
+
* @param options.updateFooter Update footer state
|
|
1677
|
+
* @param options.populateHistory Add user messages to editor history
|
|
1678
|
+
*/
|
|
1679
|
+
renderSessionContext(sessionContext, options = {}) {
|
|
1680
|
+
this.pendingTools.clear();
|
|
1681
|
+
if (options.updateFooter) {
|
|
1682
|
+
this.footer.invalidate();
|
|
1683
|
+
this.updateEditorBorderColor();
|
|
1684
|
+
}
|
|
1685
|
+
for (const message of sessionContext.messages) {
|
|
1686
|
+
// Assistant messages need special handling for tool calls
|
|
1687
|
+
if (message.role === "assistant") {
|
|
1688
|
+
this.addMessageToChat(message);
|
|
1689
|
+
// Render tool call components
|
|
1690
|
+
for (const content of message.content) {
|
|
1691
|
+
if (content.type === "toolCall") {
|
|
1692
|
+
const component = new ToolExecutionComponent(content.name, content.arguments, { showImages: this.settingsManager.getShowImages() }, this.getRegisteredToolDefinition(content.name), this.ui);
|
|
1693
|
+
component.setExpanded(this.toolOutputExpanded);
|
|
1694
|
+
this.chatContainer.addChild(component);
|
|
1695
|
+
if (message.stopReason === "aborted" || message.stopReason === "error") {
|
|
1696
|
+
let errorMessage;
|
|
1697
|
+
if (message.stopReason === "aborted") {
|
|
1698
|
+
const retryAttempt = this.session.retryAttempt;
|
|
1699
|
+
errorMessage =
|
|
1700
|
+
retryAttempt > 0
|
|
1701
|
+
? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}`
|
|
1702
|
+
: "Operation aborted";
|
|
1703
|
+
}
|
|
1704
|
+
else {
|
|
1705
|
+
errorMessage = message.errorMessage || "Error";
|
|
1706
|
+
}
|
|
1707
|
+
component.updateResult({ content: [{ type: "text", text: errorMessage }], isError: true });
|
|
1708
|
+
}
|
|
1709
|
+
else {
|
|
1710
|
+
this.pendingTools.set(content.id, component);
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
else if (message.role === "toolResult") {
|
|
1716
|
+
// Match tool results to pending tool components
|
|
1717
|
+
const component = this.pendingTools.get(message.toolCallId);
|
|
1718
|
+
if (component) {
|
|
1719
|
+
component.updateResult(message);
|
|
1720
|
+
this.pendingTools.delete(message.toolCallId);
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
else {
|
|
1724
|
+
// All other messages use standard rendering
|
|
1725
|
+
this.addMessageToChat(message, options);
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
this.pendingTools.clear();
|
|
1729
|
+
this.ui.requestRender();
|
|
1730
|
+
}
|
|
1731
|
+
renderInitialMessages() {
|
|
1732
|
+
// Get aligned messages and entries from session context
|
|
1733
|
+
const context = this.sessionManager.buildSessionContext();
|
|
1734
|
+
this.renderSessionContext(context, {
|
|
1735
|
+
updateFooter: true,
|
|
1736
|
+
populateHistory: true,
|
|
1737
|
+
});
|
|
1738
|
+
// Show compaction info if session was compacted
|
|
1739
|
+
const allEntries = this.sessionManager.getEntries();
|
|
1740
|
+
const compactionCount = allEntries.filter((e) => e.type === "compaction").length;
|
|
1741
|
+
if (compactionCount > 0) {
|
|
1742
|
+
const times = compactionCount === 1 ? "1 time" : `${compactionCount} times`;
|
|
1743
|
+
this.showStatus(`Session compacted ${times}`);
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
async getUserInput() {
|
|
1747
|
+
return new Promise((resolve) => {
|
|
1748
|
+
this.onInputCallback = (text) => {
|
|
1749
|
+
this.onInputCallback = undefined;
|
|
1750
|
+
resolve(text);
|
|
1751
|
+
};
|
|
1752
|
+
});
|
|
1753
|
+
}
|
|
1754
|
+
rebuildChatFromMessages() {
|
|
1755
|
+
this.chatContainer.clear();
|
|
1756
|
+
const context = this.sessionManager.buildSessionContext();
|
|
1757
|
+
this.renderSessionContext(context);
|
|
1758
|
+
}
|
|
1759
|
+
// =========================================================================
|
|
1760
|
+
// Key handlers
|
|
1761
|
+
// =========================================================================
|
|
1762
|
+
handleCtrlC() {
|
|
1763
|
+
const now = Date.now();
|
|
1764
|
+
if (now - this.lastSigintTime < 500) {
|
|
1765
|
+
void this.shutdown();
|
|
1766
|
+
}
|
|
1767
|
+
else {
|
|
1768
|
+
this.clearEditor();
|
|
1769
|
+
this.lastSigintTime = now;
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
handleCtrlD() {
|
|
1773
|
+
// Only called when editor is empty (enforced by CustomEditor)
|
|
1774
|
+
void this.shutdown();
|
|
1775
|
+
}
|
|
1776
|
+
/**
|
|
1777
|
+
* Gracefully shutdown the agent.
|
|
1778
|
+
* Emits shutdown event to extensions, then exits.
|
|
1779
|
+
*/
|
|
1780
|
+
isShuttingDown = false;
|
|
1781
|
+
async shutdown() {
|
|
1782
|
+
if (this.isShuttingDown)
|
|
1783
|
+
return;
|
|
1784
|
+
this.isShuttingDown = true;
|
|
1785
|
+
// Emit shutdown event to extensions
|
|
1786
|
+
const extensionRunner = this.session.extensionRunner;
|
|
1787
|
+
if (extensionRunner?.hasHandlers("session_shutdown")) {
|
|
1788
|
+
await extensionRunner.emit({
|
|
1789
|
+
type: "session_shutdown",
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
this.stop();
|
|
1793
|
+
process.exit(0);
|
|
1794
|
+
}
|
|
1795
|
+
/**
|
|
1796
|
+
* Check if shutdown was requested and perform shutdown if so.
|
|
1797
|
+
*/
|
|
1798
|
+
async checkShutdownRequested() {
|
|
1799
|
+
if (!this.shutdownRequested)
|
|
1800
|
+
return;
|
|
1801
|
+
await this.shutdown();
|
|
1802
|
+
}
|
|
1803
|
+
handleCtrlZ() {
|
|
1804
|
+
// Set up handler to restore TUI when resumed
|
|
1805
|
+
process.once("SIGCONT", () => {
|
|
1806
|
+
this.ui.start();
|
|
1807
|
+
this.ui.requestRender(true);
|
|
1808
|
+
});
|
|
1809
|
+
// Stop the TUI (restore terminal to normal mode)
|
|
1810
|
+
this.ui.stop();
|
|
1811
|
+
// Send SIGTSTP to process group (pid=0 means all processes in group)
|
|
1812
|
+
process.kill(0, "SIGTSTP");
|
|
1813
|
+
}
|
|
1814
|
+
async handleFollowUp() {
|
|
1815
|
+
const text = this.editor.getText().trim();
|
|
1816
|
+
if (!text)
|
|
1817
|
+
return;
|
|
1818
|
+
// Queue input during compaction (extension commands execute immediately)
|
|
1819
|
+
if (this.session.isCompacting) {
|
|
1820
|
+
if (this.isExtensionCommand(text)) {
|
|
1821
|
+
this.editor.addToHistory?.(text);
|
|
1822
|
+
this.editor.setText("");
|
|
1823
|
+
await this.session.prompt(text);
|
|
1824
|
+
}
|
|
1825
|
+
else {
|
|
1826
|
+
this.queueCompactionMessage(text, "followUp");
|
|
1827
|
+
}
|
|
1828
|
+
return;
|
|
1829
|
+
}
|
|
1830
|
+
// Alt+Enter queues a follow-up message (waits until agent finishes)
|
|
1831
|
+
// This handles extension commands (execute immediately), prompt template expansion, and queueing
|
|
1832
|
+
if (this.session.isStreaming) {
|
|
1833
|
+
this.editor.addToHistory?.(text);
|
|
1834
|
+
this.editor.setText("");
|
|
1835
|
+
await this.session.prompt(text, { streamingBehavior: "followUp" });
|
|
1836
|
+
this.updatePendingMessagesDisplay();
|
|
1837
|
+
this.ui.requestRender();
|
|
1838
|
+
}
|
|
1839
|
+
// If not streaming, Alt+Enter acts like regular Enter (trigger onSubmit)
|
|
1840
|
+
else if (this.editor.onSubmit) {
|
|
1841
|
+
this.editor.onSubmit(text);
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
handleDequeue() {
|
|
1845
|
+
const restored = this.restoreQueuedMessagesToEditor();
|
|
1846
|
+
if (restored === 0) {
|
|
1847
|
+
this.showStatus("No queued messages to restore");
|
|
1848
|
+
}
|
|
1849
|
+
else {
|
|
1850
|
+
this.showStatus(`Restored ${restored} queued message${restored > 1 ? "s" : ""} to editor`);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
updateEditorBorderColor() {
|
|
1854
|
+
if (this.isBashMode) {
|
|
1855
|
+
this.editor.borderColor = theme.getBashModeBorderColor();
|
|
1856
|
+
}
|
|
1857
|
+
else {
|
|
1858
|
+
const level = this.session.thinkingLevel || "off";
|
|
1859
|
+
this.editor.borderColor = theme.getThinkingBorderColor(level);
|
|
1860
|
+
}
|
|
1861
|
+
this.ui.requestRender();
|
|
1862
|
+
}
|
|
1863
|
+
cycleThinkingLevel() {
|
|
1864
|
+
const newLevel = this.session.cycleThinkingLevel();
|
|
1865
|
+
if (newLevel === undefined) {
|
|
1866
|
+
this.showStatus("Current model does not support thinking");
|
|
1867
|
+
}
|
|
1868
|
+
else {
|
|
1869
|
+
this.footer.invalidate();
|
|
1870
|
+
this.updateEditorBorderColor();
|
|
1871
|
+
this.showStatus(`Thinking level: ${newLevel}`);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
async cycleModel(direction) {
|
|
1875
|
+
try {
|
|
1876
|
+
const result = await this.session.cycleModel(direction);
|
|
1877
|
+
if (result === undefined) {
|
|
1878
|
+
const msg = this.session.scopedModels.length > 0 ? "Only one model in scope" : "Only one model available";
|
|
1879
|
+
this.showStatus(msg);
|
|
1880
|
+
}
|
|
1881
|
+
else {
|
|
1882
|
+
this.footer.invalidate();
|
|
1883
|
+
this.updateEditorBorderColor();
|
|
1884
|
+
const thinkingStr = result.model.reasoning && result.thinkingLevel !== "off" ? ` (thinking: ${result.thinkingLevel})` : "";
|
|
1885
|
+
this.showStatus(`Switched to ${result.model.name || result.model.id}${thinkingStr}`);
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
catch (error) {
|
|
1889
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
toggleToolOutputExpansion() {
|
|
1893
|
+
this.toolOutputExpanded = !this.toolOutputExpanded;
|
|
1894
|
+
for (const child of this.chatContainer.children) {
|
|
1895
|
+
if (isExpandable(child)) {
|
|
1896
|
+
child.setExpanded(this.toolOutputExpanded);
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
this.ui.requestRender();
|
|
1900
|
+
}
|
|
1901
|
+
toggleThinkingBlockVisibility() {
|
|
1902
|
+
this.hideThinkingBlock = !this.hideThinkingBlock;
|
|
1903
|
+
this.settingsManager.setHideThinkingBlock(this.hideThinkingBlock);
|
|
1904
|
+
// Rebuild chat from session messages
|
|
1905
|
+
this.chatContainer.clear();
|
|
1906
|
+
this.rebuildChatFromMessages();
|
|
1907
|
+
// If streaming, re-add the streaming component with updated visibility and re-render
|
|
1908
|
+
if (this.streamingComponent && this.streamingMessage) {
|
|
1909
|
+
this.streamingComponent.setHideThinkingBlock(this.hideThinkingBlock);
|
|
1910
|
+
this.streamingComponent.updateContent(this.streamingMessage);
|
|
1911
|
+
this.chatContainer.addChild(this.streamingComponent);
|
|
1912
|
+
}
|
|
1913
|
+
this.showStatus(`Thinking blocks: ${this.hideThinkingBlock ? "hidden" : "visible"}`);
|
|
1914
|
+
}
|
|
1915
|
+
openExternalEditor() {
|
|
1916
|
+
// Determine editor (respect $VISUAL, then $EDITOR)
|
|
1917
|
+
const editorCmd = process.env.VISUAL || process.env.EDITOR;
|
|
1918
|
+
if (!editorCmd) {
|
|
1919
|
+
this.showWarning("No editor configured. Set $VISUAL or $EDITOR environment variable.");
|
|
1920
|
+
return;
|
|
1921
|
+
}
|
|
1922
|
+
const currentText = this.editor.getExpandedText?.() ?? this.editor.getText();
|
|
1923
|
+
const tmpFile = path.join(os.tmpdir(), `pi-editor-${Date.now()}.pi.md`);
|
|
1924
|
+
try {
|
|
1925
|
+
// Write current content to temp file
|
|
1926
|
+
fs.writeFileSync(tmpFile, currentText, "utf-8");
|
|
1927
|
+
// Stop TUI to release terminal
|
|
1928
|
+
this.ui.stop();
|
|
1929
|
+
// Split by space to support editor arguments (e.g., "code --wait")
|
|
1930
|
+
const [editor, ...editorArgs] = editorCmd.split(" ");
|
|
1931
|
+
// Spawn editor synchronously with inherited stdio for interactive editing
|
|
1932
|
+
const result = spawnSync(editor, [...editorArgs, tmpFile], {
|
|
1933
|
+
stdio: "inherit",
|
|
1934
|
+
});
|
|
1935
|
+
// On successful exit (status 0), replace editor content
|
|
1936
|
+
if (result.status === 0) {
|
|
1937
|
+
const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
|
|
1938
|
+
this.editor.setText(newContent);
|
|
1939
|
+
}
|
|
1940
|
+
// On non-zero exit, keep original text (no action needed)
|
|
1941
|
+
}
|
|
1942
|
+
finally {
|
|
1943
|
+
// Clean up temp file
|
|
1944
|
+
try {
|
|
1945
|
+
fs.unlinkSync(tmpFile);
|
|
1946
|
+
}
|
|
1947
|
+
catch {
|
|
1948
|
+
// Ignore cleanup errors
|
|
1949
|
+
}
|
|
1950
|
+
// Restart TUI
|
|
1951
|
+
this.ui.start();
|
|
1952
|
+
// Force full re-render since external editor uses alternate screen
|
|
1953
|
+
this.ui.requestRender(true);
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
// =========================================================================
|
|
1957
|
+
// UI helpers
|
|
1958
|
+
// =========================================================================
|
|
1959
|
+
clearEditor() {
|
|
1960
|
+
this.editor.setText("");
|
|
1961
|
+
this.ui.requestRender();
|
|
1962
|
+
}
|
|
1963
|
+
showError(errorMessage) {
|
|
1964
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1965
|
+
this.chatContainer.addChild(new Text(theme.fg("error", `Error: ${errorMessage}`), 1, 0));
|
|
1966
|
+
this.ui.requestRender();
|
|
1967
|
+
}
|
|
1968
|
+
showWarning(warningMessage) {
|
|
1969
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1970
|
+
this.chatContainer.addChild(new Text(theme.fg("warning", `Warning: ${warningMessage}`), 1, 0));
|
|
1971
|
+
this.ui.requestRender();
|
|
1972
|
+
}
|
|
1973
|
+
showNewVersionNotification(newVersion) {
|
|
1974
|
+
const updateInstruction = isBunBinary
|
|
1975
|
+
? theme.fg("muted", `New version ${newVersion} is available. Download from: `) +
|
|
1976
|
+
theme.fg("accent", "https://github.com/badlogic/pi-mono/releases/latest")
|
|
1977
|
+
: theme.fg("muted", `New version ${newVersion} is available. Run: `) +
|
|
1978
|
+
theme.fg("accent", "npm install -g @vaclav-synacek/pi-coding-agent-termux");
|
|
1979
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1980
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
1981
|
+
this.chatContainer.addChild(new Text(`${theme.bold(theme.fg("warning", "Update Available"))}\n${updateInstruction}`, 1, 0));
|
|
1982
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
1983
|
+
this.ui.requestRender();
|
|
1984
|
+
}
|
|
1985
|
+
updatePendingMessagesDisplay() {
|
|
1986
|
+
this.pendingMessagesContainer.clear();
|
|
1987
|
+
const steeringMessages = [
|
|
1988
|
+
...this.session.getSteeringMessages(),
|
|
1989
|
+
...this.compactionQueuedMessages.filter((msg) => msg.mode === "steer").map((msg) => msg.text),
|
|
1990
|
+
];
|
|
1991
|
+
const followUpMessages = [
|
|
1992
|
+
...this.session.getFollowUpMessages(),
|
|
1993
|
+
...this.compactionQueuedMessages.filter((msg) => msg.mode === "followUp").map((msg) => msg.text),
|
|
1994
|
+
];
|
|
1995
|
+
if (steeringMessages.length > 0 || followUpMessages.length > 0) {
|
|
1996
|
+
this.pendingMessagesContainer.addChild(new Spacer(1));
|
|
1997
|
+
for (const message of steeringMessages) {
|
|
1998
|
+
const text = theme.fg("dim", `Steering: ${message}`);
|
|
1999
|
+
this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
|
|
2000
|
+
}
|
|
2001
|
+
for (const message of followUpMessages) {
|
|
2002
|
+
const text = theme.fg("dim", `Follow-up: ${message}`);
|
|
2003
|
+
this.pendingMessagesContainer.addChild(new TruncatedText(text, 1, 0));
|
|
2004
|
+
}
|
|
2005
|
+
const dequeueHint = this.getAppKeyDisplay("dequeue");
|
|
2006
|
+
const hintText = theme.fg("dim", `↳ ${dequeueHint} to edit all queued messages`);
|
|
2007
|
+
this.pendingMessagesContainer.addChild(new TruncatedText(hintText, 1, 0));
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
restoreQueuedMessagesToEditor(options) {
|
|
2011
|
+
const { steering, followUp } = this.session.clearQueue();
|
|
2012
|
+
const allQueued = [...steering, ...followUp];
|
|
2013
|
+
if (allQueued.length === 0) {
|
|
2014
|
+
this.updatePendingMessagesDisplay();
|
|
2015
|
+
if (options?.abort) {
|
|
2016
|
+
this.agent.abort();
|
|
2017
|
+
}
|
|
2018
|
+
return 0;
|
|
2019
|
+
}
|
|
2020
|
+
const queuedText = allQueued.join("\n\n");
|
|
2021
|
+
const currentText = options?.currentText ?? this.editor.getText();
|
|
2022
|
+
const combinedText = [queuedText, currentText].filter((t) => t.trim()).join("\n\n");
|
|
2023
|
+
this.editor.setText(combinedText);
|
|
2024
|
+
this.updatePendingMessagesDisplay();
|
|
2025
|
+
if (options?.abort) {
|
|
2026
|
+
this.agent.abort();
|
|
2027
|
+
}
|
|
2028
|
+
return allQueued.length;
|
|
2029
|
+
}
|
|
2030
|
+
queueCompactionMessage(text, mode) {
|
|
2031
|
+
this.compactionQueuedMessages.push({ text, mode });
|
|
2032
|
+
this.editor.addToHistory?.(text);
|
|
2033
|
+
this.editor.setText("");
|
|
2034
|
+
this.updatePendingMessagesDisplay();
|
|
2035
|
+
this.showStatus("Queued message for after compaction");
|
|
2036
|
+
}
|
|
2037
|
+
isExtensionCommand(text) {
|
|
2038
|
+
if (!text.startsWith("/"))
|
|
2039
|
+
return false;
|
|
2040
|
+
const extensionRunner = this.session.extensionRunner;
|
|
2041
|
+
if (!extensionRunner)
|
|
2042
|
+
return false;
|
|
2043
|
+
const spaceIndex = text.indexOf(" ");
|
|
2044
|
+
const commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
|
|
2045
|
+
return !!extensionRunner.getCommand(commandName);
|
|
2046
|
+
}
|
|
2047
|
+
async flushCompactionQueue(options) {
|
|
2048
|
+
if (this.compactionQueuedMessages.length === 0) {
|
|
2049
|
+
return;
|
|
2050
|
+
}
|
|
2051
|
+
const queuedMessages = [...this.compactionQueuedMessages];
|
|
2052
|
+
this.compactionQueuedMessages = [];
|
|
2053
|
+
this.updatePendingMessagesDisplay();
|
|
2054
|
+
const restoreQueue = (error) => {
|
|
2055
|
+
this.session.clearQueue();
|
|
2056
|
+
this.compactionQueuedMessages = queuedMessages;
|
|
2057
|
+
this.updatePendingMessagesDisplay();
|
|
2058
|
+
this.showError(`Failed to send queued message${queuedMessages.length > 1 ? "s" : ""}: ${error instanceof Error ? error.message : String(error)}`);
|
|
2059
|
+
};
|
|
2060
|
+
try {
|
|
2061
|
+
if (options?.willRetry) {
|
|
2062
|
+
// When retry is pending, queue messages for the retry turn
|
|
2063
|
+
for (const message of queuedMessages) {
|
|
2064
|
+
if (this.isExtensionCommand(message.text)) {
|
|
2065
|
+
await this.session.prompt(message.text);
|
|
2066
|
+
}
|
|
2067
|
+
else if (message.mode === "followUp") {
|
|
2068
|
+
await this.session.followUp(message.text);
|
|
2069
|
+
}
|
|
2070
|
+
else {
|
|
2071
|
+
await this.session.steer(message.text);
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
this.updatePendingMessagesDisplay();
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
// Find first non-extension-command message to use as prompt
|
|
2078
|
+
const firstPromptIndex = queuedMessages.findIndex((message) => !this.isExtensionCommand(message.text));
|
|
2079
|
+
if (firstPromptIndex === -1) {
|
|
2080
|
+
// All extension commands - execute them all
|
|
2081
|
+
for (const message of queuedMessages) {
|
|
2082
|
+
await this.session.prompt(message.text);
|
|
2083
|
+
}
|
|
2084
|
+
return;
|
|
2085
|
+
}
|
|
2086
|
+
// Execute any extension commands before the first prompt
|
|
2087
|
+
const preCommands = queuedMessages.slice(0, firstPromptIndex);
|
|
2088
|
+
const firstPrompt = queuedMessages[firstPromptIndex];
|
|
2089
|
+
const rest = queuedMessages.slice(firstPromptIndex + 1);
|
|
2090
|
+
for (const message of preCommands) {
|
|
2091
|
+
await this.session.prompt(message.text);
|
|
2092
|
+
}
|
|
2093
|
+
// Send first prompt (starts streaming)
|
|
2094
|
+
const promptPromise = this.session.prompt(firstPrompt.text).catch((error) => {
|
|
2095
|
+
restoreQueue(error);
|
|
2096
|
+
});
|
|
2097
|
+
// Queue remaining messages
|
|
2098
|
+
for (const message of rest) {
|
|
2099
|
+
if (this.isExtensionCommand(message.text)) {
|
|
2100
|
+
await this.session.prompt(message.text);
|
|
2101
|
+
}
|
|
2102
|
+
else if (message.mode === "followUp") {
|
|
2103
|
+
await this.session.followUp(message.text);
|
|
2104
|
+
}
|
|
2105
|
+
else {
|
|
2106
|
+
await this.session.steer(message.text);
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
this.updatePendingMessagesDisplay();
|
|
2110
|
+
void promptPromise;
|
|
2111
|
+
}
|
|
2112
|
+
catch (error) {
|
|
2113
|
+
restoreQueue(error);
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
/** Move pending bash components from pending area to chat */
|
|
2117
|
+
flushPendingBashComponents() {
|
|
2118
|
+
for (const component of this.pendingBashComponents) {
|
|
2119
|
+
this.pendingMessagesContainer.removeChild(component);
|
|
2120
|
+
this.chatContainer.addChild(component);
|
|
2121
|
+
}
|
|
2122
|
+
this.pendingBashComponents = [];
|
|
2123
|
+
}
|
|
2124
|
+
// =========================================================================
|
|
2125
|
+
// Selectors
|
|
2126
|
+
// =========================================================================
|
|
2127
|
+
/**
|
|
2128
|
+
* Shows a selector component in place of the editor.
|
|
2129
|
+
* @param create Factory that receives a `done` callback and returns the component and focus target
|
|
2130
|
+
*/
|
|
2131
|
+
showSelector(create) {
|
|
2132
|
+
const done = () => {
|
|
2133
|
+
this.editorContainer.clear();
|
|
2134
|
+
this.editorContainer.addChild(this.editor);
|
|
2135
|
+
this.ui.setFocus(this.editor);
|
|
2136
|
+
};
|
|
2137
|
+
const { component, focus } = create(done);
|
|
2138
|
+
this.editorContainer.clear();
|
|
2139
|
+
this.editorContainer.addChild(component);
|
|
2140
|
+
this.ui.setFocus(focus);
|
|
2141
|
+
this.ui.requestRender();
|
|
2142
|
+
}
|
|
2143
|
+
showSettingsSelector() {
|
|
2144
|
+
this.showSelector((done) => {
|
|
2145
|
+
const selector = new SettingsSelectorComponent({
|
|
2146
|
+
autoCompact: this.session.autoCompactionEnabled,
|
|
2147
|
+
showImages: this.settingsManager.getShowImages(),
|
|
2148
|
+
autoResizeImages: this.settingsManager.getImageAutoResize(),
|
|
2149
|
+
blockImages: this.settingsManager.getBlockImages(),
|
|
2150
|
+
enableSkillCommands: this.settingsManager.getEnableSkillCommands(),
|
|
2151
|
+
steeringMode: this.session.steeringMode,
|
|
2152
|
+
followUpMode: this.session.followUpMode,
|
|
2153
|
+
thinkingLevel: this.session.thinkingLevel,
|
|
2154
|
+
availableThinkingLevels: this.session.getAvailableThinkingLevels(),
|
|
2155
|
+
currentTheme: this.settingsManager.getTheme() || "dark",
|
|
2156
|
+
availableThemes: getAvailableThemes(),
|
|
2157
|
+
hideThinkingBlock: this.hideThinkingBlock,
|
|
2158
|
+
collapseChangelog: this.settingsManager.getCollapseChangelog(),
|
|
2159
|
+
doubleEscapeAction: this.settingsManager.getDoubleEscapeAction(),
|
|
2160
|
+
}, {
|
|
2161
|
+
onAutoCompactChange: (enabled) => {
|
|
2162
|
+
this.session.setAutoCompactionEnabled(enabled);
|
|
2163
|
+
this.footer.setAutoCompactEnabled(enabled);
|
|
2164
|
+
},
|
|
2165
|
+
onShowImagesChange: (enabled) => {
|
|
2166
|
+
this.settingsManager.setShowImages(enabled);
|
|
2167
|
+
for (const child of this.chatContainer.children) {
|
|
2168
|
+
if (child instanceof ToolExecutionComponent) {
|
|
2169
|
+
child.setShowImages(enabled);
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
},
|
|
2173
|
+
onAutoResizeImagesChange: (enabled) => {
|
|
2174
|
+
this.settingsManager.setImageAutoResize(enabled);
|
|
2175
|
+
},
|
|
2176
|
+
onBlockImagesChange: (blocked) => {
|
|
2177
|
+
this.settingsManager.setBlockImages(blocked);
|
|
2178
|
+
},
|
|
2179
|
+
onEnableSkillCommandsChange: (enabled) => {
|
|
2180
|
+
this.settingsManager.setEnableSkillCommands(enabled);
|
|
2181
|
+
this.rebuildAutocomplete();
|
|
2182
|
+
},
|
|
2183
|
+
onSteeringModeChange: (mode) => {
|
|
2184
|
+
this.session.setSteeringMode(mode);
|
|
2185
|
+
},
|
|
2186
|
+
onFollowUpModeChange: (mode) => {
|
|
2187
|
+
this.session.setFollowUpMode(mode);
|
|
2188
|
+
},
|
|
2189
|
+
onThinkingLevelChange: (level) => {
|
|
2190
|
+
this.session.setThinkingLevel(level);
|
|
2191
|
+
this.footer.invalidate();
|
|
2192
|
+
this.updateEditorBorderColor();
|
|
2193
|
+
},
|
|
2194
|
+
onThemeChange: (themeName) => {
|
|
2195
|
+
const result = setTheme(themeName, true);
|
|
2196
|
+
this.settingsManager.setTheme(themeName);
|
|
2197
|
+
this.ui.invalidate();
|
|
2198
|
+
if (!result.success) {
|
|
2199
|
+
this.showError(`Failed to load theme "${themeName}": ${result.error}\nFell back to dark theme.`);
|
|
2200
|
+
}
|
|
2201
|
+
},
|
|
2202
|
+
onThemePreview: (themeName) => {
|
|
2203
|
+
const result = setTheme(themeName, true);
|
|
2204
|
+
if (result.success) {
|
|
2205
|
+
this.ui.invalidate();
|
|
2206
|
+
this.ui.requestRender();
|
|
2207
|
+
}
|
|
2208
|
+
},
|
|
2209
|
+
onHideThinkingBlockChange: (hidden) => {
|
|
2210
|
+
this.hideThinkingBlock = hidden;
|
|
2211
|
+
this.settingsManager.setHideThinkingBlock(hidden);
|
|
2212
|
+
for (const child of this.chatContainer.children) {
|
|
2213
|
+
if (child instanceof AssistantMessageComponent) {
|
|
2214
|
+
child.setHideThinkingBlock(hidden);
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
this.chatContainer.clear();
|
|
2218
|
+
this.rebuildChatFromMessages();
|
|
2219
|
+
},
|
|
2220
|
+
onCollapseChangelogChange: (collapsed) => {
|
|
2221
|
+
this.settingsManager.setCollapseChangelog(collapsed);
|
|
2222
|
+
},
|
|
2223
|
+
onDoubleEscapeActionChange: (action) => {
|
|
2224
|
+
this.settingsManager.setDoubleEscapeAction(action);
|
|
2225
|
+
},
|
|
2226
|
+
onCancel: () => {
|
|
2227
|
+
done();
|
|
2228
|
+
this.ui.requestRender();
|
|
2229
|
+
},
|
|
2230
|
+
});
|
|
2231
|
+
return { component: selector, focus: selector.getSettingsList() };
|
|
2232
|
+
});
|
|
2233
|
+
}
|
|
2234
|
+
async handleModelCommand(searchTerm) {
|
|
2235
|
+
if (!searchTerm) {
|
|
2236
|
+
this.showModelSelector();
|
|
2237
|
+
return;
|
|
2238
|
+
}
|
|
2239
|
+
const model = await this.findExactModelMatch(searchTerm);
|
|
2240
|
+
if (model) {
|
|
2241
|
+
try {
|
|
2242
|
+
await this.session.setModel(model);
|
|
2243
|
+
this.footer.invalidate();
|
|
2244
|
+
this.updateEditorBorderColor();
|
|
2245
|
+
this.showStatus(`Model: ${model.id}`);
|
|
2246
|
+
}
|
|
2247
|
+
catch (error) {
|
|
2248
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
2249
|
+
}
|
|
2250
|
+
return;
|
|
2251
|
+
}
|
|
2252
|
+
this.showModelSelector(searchTerm);
|
|
2253
|
+
}
|
|
2254
|
+
async findExactModelMatch(searchTerm) {
|
|
2255
|
+
const term = searchTerm.trim();
|
|
2256
|
+
if (!term)
|
|
2257
|
+
return undefined;
|
|
2258
|
+
let targetProvider;
|
|
2259
|
+
let targetModelId = "";
|
|
2260
|
+
if (term.includes("/")) {
|
|
2261
|
+
const parts = term.split("/", 2);
|
|
2262
|
+
targetProvider = parts[0]?.trim().toLowerCase();
|
|
2263
|
+
targetModelId = parts[1]?.trim().toLowerCase() ?? "";
|
|
2264
|
+
}
|
|
2265
|
+
else {
|
|
2266
|
+
targetModelId = term.toLowerCase();
|
|
2267
|
+
}
|
|
2268
|
+
if (!targetModelId)
|
|
2269
|
+
return undefined;
|
|
2270
|
+
const models = await this.getModelCandidates();
|
|
2271
|
+
const exactMatches = models.filter((item) => {
|
|
2272
|
+
const idMatch = item.id.toLowerCase() === targetModelId;
|
|
2273
|
+
const providerMatch = !targetProvider || item.provider.toLowerCase() === targetProvider;
|
|
2274
|
+
return idMatch && providerMatch;
|
|
2275
|
+
});
|
|
2276
|
+
return exactMatches.length === 1 ? exactMatches[0] : undefined;
|
|
2277
|
+
}
|
|
2278
|
+
async getModelCandidates() {
|
|
2279
|
+
if (this.session.scopedModels.length > 0) {
|
|
2280
|
+
return this.session.scopedModels.map((scoped) => scoped.model);
|
|
2281
|
+
}
|
|
2282
|
+
this.session.modelRegistry.refresh();
|
|
2283
|
+
try {
|
|
2284
|
+
return await this.session.modelRegistry.getAvailable();
|
|
2285
|
+
}
|
|
2286
|
+
catch {
|
|
2287
|
+
return [];
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
showModelSelector(initialSearchInput) {
|
|
2291
|
+
this.showSelector((done) => {
|
|
2292
|
+
const selector = new ModelSelectorComponent(this.ui, this.session.model, this.settingsManager, this.session.modelRegistry, this.session.scopedModels, async (model) => {
|
|
2293
|
+
try {
|
|
2294
|
+
await this.session.setModel(model);
|
|
2295
|
+
this.footer.invalidate();
|
|
2296
|
+
this.updateEditorBorderColor();
|
|
2297
|
+
done();
|
|
2298
|
+
this.showStatus(`Model: ${model.id}`);
|
|
2299
|
+
}
|
|
2300
|
+
catch (error) {
|
|
2301
|
+
done();
|
|
2302
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
2303
|
+
}
|
|
2304
|
+
}, () => {
|
|
2305
|
+
done();
|
|
2306
|
+
this.ui.requestRender();
|
|
2307
|
+
}, initialSearchInput);
|
|
2308
|
+
return { component: selector, focus: selector };
|
|
2309
|
+
});
|
|
2310
|
+
}
|
|
2311
|
+
async showModelsSelector() {
|
|
2312
|
+
// Get all available models
|
|
2313
|
+
this.session.modelRegistry.refresh();
|
|
2314
|
+
const allModels = this.session.modelRegistry.getAvailable();
|
|
2315
|
+
if (allModels.length === 0) {
|
|
2316
|
+
this.showStatus("No models available");
|
|
2317
|
+
return;
|
|
2318
|
+
}
|
|
2319
|
+
// Check if session has scoped models (from previous session-only changes or CLI --models)
|
|
2320
|
+
const sessionScopedModels = this.session.scopedModels;
|
|
2321
|
+
const hasSessionScope = sessionScopedModels.length > 0;
|
|
2322
|
+
// Build enabled model IDs from session state or settings
|
|
2323
|
+
const enabledModelIds = new Set();
|
|
2324
|
+
let hasFilter = false;
|
|
2325
|
+
if (hasSessionScope) {
|
|
2326
|
+
// Use current session's scoped models
|
|
2327
|
+
for (const sm of sessionScopedModels) {
|
|
2328
|
+
enabledModelIds.add(`${sm.model.provider}/${sm.model.id}`);
|
|
2329
|
+
}
|
|
2330
|
+
hasFilter = true;
|
|
2331
|
+
}
|
|
2332
|
+
else {
|
|
2333
|
+
// Fall back to settings
|
|
2334
|
+
const patterns = this.settingsManager.getEnabledModels();
|
|
2335
|
+
if (patterns !== undefined && patterns.length > 0) {
|
|
2336
|
+
hasFilter = true;
|
|
2337
|
+
const scopedModels = await resolveModelScope(patterns, this.session.modelRegistry);
|
|
2338
|
+
for (const sm of scopedModels) {
|
|
2339
|
+
enabledModelIds.add(`${sm.model.provider}/${sm.model.id}`);
|
|
2340
|
+
}
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
// Track current enabled state (session-only until persisted)
|
|
2344
|
+
const currentEnabledIds = new Set(enabledModelIds);
|
|
2345
|
+
let currentHasFilter = hasFilter;
|
|
2346
|
+
// Helper to update session's scoped models (session-only, no persist)
|
|
2347
|
+
const updateSessionModels = async (enabledIds) => {
|
|
2348
|
+
if (enabledIds.size > 0 && enabledIds.size < allModels.length) {
|
|
2349
|
+
// Use current session thinking level, not settings default
|
|
2350
|
+
const currentThinkingLevel = this.session.thinkingLevel;
|
|
2351
|
+
const newScopedModels = await resolveModelScope(Array.from(enabledIds), this.session.modelRegistry);
|
|
2352
|
+
this.session.setScopedModels(newScopedModels.map((sm) => ({
|
|
2353
|
+
model: sm.model,
|
|
2354
|
+
thinkingLevel: sm.thinkingLevel ?? currentThinkingLevel,
|
|
2355
|
+
})));
|
|
2356
|
+
}
|
|
2357
|
+
else {
|
|
2358
|
+
// All enabled or none enabled = no filter
|
|
2359
|
+
this.session.setScopedModels([]);
|
|
2360
|
+
}
|
|
2361
|
+
};
|
|
2362
|
+
this.showSelector((done) => {
|
|
2363
|
+
const selector = new ScopedModelsSelectorComponent({
|
|
2364
|
+
allModels,
|
|
2365
|
+
enabledModelIds: currentEnabledIds,
|
|
2366
|
+
hasEnabledModelsFilter: currentHasFilter,
|
|
2367
|
+
}, {
|
|
2368
|
+
onModelToggle: async (modelId, enabled) => {
|
|
2369
|
+
if (enabled) {
|
|
2370
|
+
currentEnabledIds.add(modelId);
|
|
2371
|
+
}
|
|
2372
|
+
else {
|
|
2373
|
+
currentEnabledIds.delete(modelId);
|
|
2374
|
+
}
|
|
2375
|
+
currentHasFilter = true;
|
|
2376
|
+
await updateSessionModels(currentEnabledIds);
|
|
2377
|
+
},
|
|
2378
|
+
onEnableAll: async (allModelIds) => {
|
|
2379
|
+
currentEnabledIds.clear();
|
|
2380
|
+
for (const id of allModelIds) {
|
|
2381
|
+
currentEnabledIds.add(id);
|
|
2382
|
+
}
|
|
2383
|
+
currentHasFilter = false;
|
|
2384
|
+
await updateSessionModels(currentEnabledIds);
|
|
2385
|
+
},
|
|
2386
|
+
onClearAll: async () => {
|
|
2387
|
+
currentEnabledIds.clear();
|
|
2388
|
+
currentHasFilter = true;
|
|
2389
|
+
await updateSessionModels(currentEnabledIds);
|
|
2390
|
+
},
|
|
2391
|
+
onToggleProvider: async (_provider, modelIds, enabled) => {
|
|
2392
|
+
for (const id of modelIds) {
|
|
2393
|
+
if (enabled) {
|
|
2394
|
+
currentEnabledIds.add(id);
|
|
2395
|
+
}
|
|
2396
|
+
else {
|
|
2397
|
+
currentEnabledIds.delete(id);
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
currentHasFilter = true;
|
|
2401
|
+
await updateSessionModels(currentEnabledIds);
|
|
2402
|
+
},
|
|
2403
|
+
onPersist: (enabledIds) => {
|
|
2404
|
+
// Persist to settings
|
|
2405
|
+
const newPatterns = enabledIds.length === allModels.length
|
|
2406
|
+
? undefined // All enabled = clear filter
|
|
2407
|
+
: enabledIds;
|
|
2408
|
+
this.settingsManager.setEnabledModels(newPatterns);
|
|
2409
|
+
this.showStatus("Model selection saved to settings");
|
|
2410
|
+
},
|
|
2411
|
+
onCancel: () => {
|
|
2412
|
+
done();
|
|
2413
|
+
this.ui.requestRender();
|
|
2414
|
+
},
|
|
2415
|
+
});
|
|
2416
|
+
return { component: selector, focus: selector };
|
|
2417
|
+
});
|
|
2418
|
+
}
|
|
2419
|
+
showUserMessageSelector() {
|
|
2420
|
+
const userMessages = this.session.getUserMessagesForForking();
|
|
2421
|
+
if (userMessages.length === 0) {
|
|
2422
|
+
this.showStatus("No messages to fork from");
|
|
2423
|
+
return;
|
|
2424
|
+
}
|
|
2425
|
+
this.showSelector((done) => {
|
|
2426
|
+
const selector = new UserMessageSelectorComponent(userMessages.map((m) => ({ id: m.entryId, text: m.text })), async (entryId) => {
|
|
2427
|
+
const result = await this.session.fork(entryId);
|
|
2428
|
+
if (result.cancelled) {
|
|
2429
|
+
// Extension cancelled the fork
|
|
2430
|
+
done();
|
|
2431
|
+
this.ui.requestRender();
|
|
2432
|
+
return;
|
|
2433
|
+
}
|
|
2434
|
+
this.chatContainer.clear();
|
|
2435
|
+
this.renderInitialMessages();
|
|
2436
|
+
this.editor.setText(result.selectedText);
|
|
2437
|
+
done();
|
|
2438
|
+
this.showStatus("Branched to new session");
|
|
2439
|
+
}, () => {
|
|
2440
|
+
done();
|
|
2441
|
+
this.ui.requestRender();
|
|
2442
|
+
});
|
|
2443
|
+
return { component: selector, focus: selector.getMessageList() };
|
|
2444
|
+
});
|
|
2445
|
+
}
|
|
2446
|
+
showTreeSelector(initialSelectedId) {
|
|
2447
|
+
const tree = this.sessionManager.getTree();
|
|
2448
|
+
const realLeafId = this.sessionManager.getLeafId();
|
|
2449
|
+
// Find the visible leaf for display (skip metadata entries like labels)
|
|
2450
|
+
let visibleLeafId = realLeafId;
|
|
2451
|
+
while (visibleLeafId) {
|
|
2452
|
+
const entry = this.sessionManager.getEntry(visibleLeafId);
|
|
2453
|
+
if (!entry)
|
|
2454
|
+
break;
|
|
2455
|
+
if (entry.type !== "label" && entry.type !== "custom")
|
|
2456
|
+
break;
|
|
2457
|
+
visibleLeafId = entry.parentId ?? null;
|
|
2458
|
+
}
|
|
2459
|
+
if (tree.length === 0) {
|
|
2460
|
+
this.showStatus("No entries in session");
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
this.showSelector((done) => {
|
|
2464
|
+
const selector = new TreeSelectorComponent(tree, visibleLeafId, this.ui.terminal.rows, async (entryId) => {
|
|
2465
|
+
// Selecting the visible leaf is a no-op (already there)
|
|
2466
|
+
if (entryId === visibleLeafId) {
|
|
2467
|
+
done();
|
|
2468
|
+
this.showStatus("Already at this point");
|
|
2469
|
+
return;
|
|
2470
|
+
}
|
|
2471
|
+
// Ask about summarization
|
|
2472
|
+
done(); // Close selector first
|
|
2473
|
+
// Loop until user makes a complete choice or cancels to tree
|
|
2474
|
+
let wantsSummary = false;
|
|
2475
|
+
let customInstructions;
|
|
2476
|
+
while (true) {
|
|
2477
|
+
const summaryChoice = await this.showExtensionSelector("Summarize branch?", [
|
|
2478
|
+
"No summary",
|
|
2479
|
+
"Summarize",
|
|
2480
|
+
"Summarize with custom prompt",
|
|
2481
|
+
]);
|
|
2482
|
+
if (summaryChoice === undefined) {
|
|
2483
|
+
// User pressed escape - re-show tree selector with same selection
|
|
2484
|
+
this.showTreeSelector(entryId);
|
|
2485
|
+
return;
|
|
2486
|
+
}
|
|
2487
|
+
wantsSummary = summaryChoice !== "No summary";
|
|
2488
|
+
if (summaryChoice === "Summarize with custom prompt") {
|
|
2489
|
+
customInstructions = await this.showExtensionEditor("Custom summarization instructions");
|
|
2490
|
+
if (customInstructions === undefined) {
|
|
2491
|
+
// User cancelled - loop back to summary selector
|
|
2492
|
+
continue;
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
// User made a complete choice
|
|
2496
|
+
break;
|
|
2497
|
+
}
|
|
2498
|
+
// Set up escape handler and loader if summarizing
|
|
2499
|
+
let summaryLoader;
|
|
2500
|
+
const originalOnEscape = this.defaultEditor.onEscape;
|
|
2501
|
+
if (wantsSummary) {
|
|
2502
|
+
this.defaultEditor.onEscape = () => {
|
|
2503
|
+
this.session.abortBranchSummary();
|
|
2504
|
+
};
|
|
2505
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2506
|
+
summaryLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), "Summarizing branch... (esc to cancel)");
|
|
2507
|
+
this.statusContainer.addChild(summaryLoader);
|
|
2508
|
+
this.ui.requestRender();
|
|
2509
|
+
}
|
|
2510
|
+
try {
|
|
2511
|
+
const result = await this.session.navigateTree(entryId, {
|
|
2512
|
+
summarize: wantsSummary,
|
|
2513
|
+
customInstructions,
|
|
2514
|
+
});
|
|
2515
|
+
if (result.aborted) {
|
|
2516
|
+
// Summarization aborted - re-show tree selector with same selection
|
|
2517
|
+
this.showStatus("Branch summarization cancelled");
|
|
2518
|
+
this.showTreeSelector(entryId);
|
|
2519
|
+
return;
|
|
2520
|
+
}
|
|
2521
|
+
if (result.cancelled) {
|
|
2522
|
+
this.showStatus("Navigation cancelled");
|
|
2523
|
+
return;
|
|
2524
|
+
}
|
|
2525
|
+
// Update UI
|
|
2526
|
+
this.chatContainer.clear();
|
|
2527
|
+
this.renderInitialMessages();
|
|
2528
|
+
if (result.editorText) {
|
|
2529
|
+
this.editor.setText(result.editorText);
|
|
2530
|
+
}
|
|
2531
|
+
this.showStatus("Navigated to selected point");
|
|
2532
|
+
}
|
|
2533
|
+
catch (error) {
|
|
2534
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
2535
|
+
}
|
|
2536
|
+
finally {
|
|
2537
|
+
if (summaryLoader) {
|
|
2538
|
+
summaryLoader.stop();
|
|
2539
|
+
this.statusContainer.clear();
|
|
2540
|
+
}
|
|
2541
|
+
this.defaultEditor.onEscape = originalOnEscape;
|
|
2542
|
+
}
|
|
2543
|
+
}, () => {
|
|
2544
|
+
done();
|
|
2545
|
+
this.ui.requestRender();
|
|
2546
|
+
}, (entryId, label) => {
|
|
2547
|
+
this.sessionManager.appendLabelChange(entryId, label);
|
|
2548
|
+
this.ui.requestRender();
|
|
2549
|
+
}, initialSelectedId);
|
|
2550
|
+
return { component: selector, focus: selector };
|
|
2551
|
+
});
|
|
2552
|
+
}
|
|
2553
|
+
showSessionSelector() {
|
|
2554
|
+
this.showSelector((done) => {
|
|
2555
|
+
const selector = new SessionSelectorComponent((onProgress) => SessionManager.list(this.sessionManager.getCwd(), this.sessionManager.getSessionDir(), onProgress), SessionManager.listAll, async (sessionPath) => {
|
|
2556
|
+
done();
|
|
2557
|
+
await this.handleResumeSession(sessionPath);
|
|
2558
|
+
}, () => {
|
|
2559
|
+
done();
|
|
2560
|
+
this.ui.requestRender();
|
|
2561
|
+
}, () => {
|
|
2562
|
+
void this.shutdown();
|
|
2563
|
+
}, () => this.ui.requestRender());
|
|
2564
|
+
return { component: selector, focus: selector.getSessionList() };
|
|
2565
|
+
});
|
|
2566
|
+
}
|
|
2567
|
+
async handleResumeSession(sessionPath) {
|
|
2568
|
+
// Stop loading animation
|
|
2569
|
+
if (this.loadingAnimation) {
|
|
2570
|
+
this.loadingAnimation.stop();
|
|
2571
|
+
this.loadingAnimation = undefined;
|
|
2572
|
+
}
|
|
2573
|
+
this.statusContainer.clear();
|
|
2574
|
+
// Clear UI state
|
|
2575
|
+
this.pendingMessagesContainer.clear();
|
|
2576
|
+
this.compactionQueuedMessages = [];
|
|
2577
|
+
this.streamingComponent = undefined;
|
|
2578
|
+
this.streamingMessage = undefined;
|
|
2579
|
+
this.pendingTools.clear();
|
|
2580
|
+
// Switch session via AgentSession (emits extension session events)
|
|
2581
|
+
await this.session.switchSession(sessionPath);
|
|
2582
|
+
// Clear and re-render the chat
|
|
2583
|
+
this.chatContainer.clear();
|
|
2584
|
+
this.renderInitialMessages();
|
|
2585
|
+
this.showStatus("Resumed session");
|
|
2586
|
+
}
|
|
2587
|
+
async showOAuthSelector(mode) {
|
|
2588
|
+
if (mode === "logout") {
|
|
2589
|
+
const providers = this.session.modelRegistry.authStorage.list();
|
|
2590
|
+
const loggedInProviders = providers.filter((p) => this.session.modelRegistry.authStorage.get(p)?.type === "oauth");
|
|
2591
|
+
if (loggedInProviders.length === 0) {
|
|
2592
|
+
this.showStatus("No OAuth providers logged in. Use /login first.");
|
|
2593
|
+
return;
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
this.showSelector((done) => {
|
|
2597
|
+
const selector = new OAuthSelectorComponent(mode, this.session.modelRegistry.authStorage, async (providerId) => {
|
|
2598
|
+
done();
|
|
2599
|
+
if (mode === "login") {
|
|
2600
|
+
await this.showLoginDialog(providerId);
|
|
2601
|
+
}
|
|
2602
|
+
else {
|
|
2603
|
+
// Logout flow
|
|
2604
|
+
const providerInfo = getOAuthProviders().find((p) => p.id === providerId);
|
|
2605
|
+
const providerName = providerInfo?.name || providerId;
|
|
2606
|
+
try {
|
|
2607
|
+
this.session.modelRegistry.authStorage.logout(providerId);
|
|
2608
|
+
this.session.modelRegistry.refresh();
|
|
2609
|
+
this.showStatus(`Logged out of ${providerName}`);
|
|
2610
|
+
}
|
|
2611
|
+
catch (error) {
|
|
2612
|
+
this.showError(`Logout failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
2613
|
+
}
|
|
2614
|
+
}
|
|
2615
|
+
}, () => {
|
|
2616
|
+
done();
|
|
2617
|
+
this.ui.requestRender();
|
|
2618
|
+
});
|
|
2619
|
+
return { component: selector, focus: selector };
|
|
2620
|
+
});
|
|
2621
|
+
}
|
|
2622
|
+
async showLoginDialog(providerId) {
|
|
2623
|
+
const providerInfo = getOAuthProviders().find((p) => p.id === providerId);
|
|
2624
|
+
const providerName = providerInfo?.name || providerId;
|
|
2625
|
+
// Providers that use callback servers (can paste redirect URL)
|
|
2626
|
+
const usesCallbackServer = providerId === "openai-codex" || providerId === "google-gemini-cli" || providerId === "google-antigravity";
|
|
2627
|
+
// Create login dialog component
|
|
2628
|
+
const dialog = new LoginDialogComponent(this.ui, providerId, (_success, _message) => {
|
|
2629
|
+
// Completion handled below
|
|
2630
|
+
});
|
|
2631
|
+
// Show dialog in editor container
|
|
2632
|
+
this.editorContainer.clear();
|
|
2633
|
+
this.editorContainer.addChild(dialog);
|
|
2634
|
+
this.ui.setFocus(dialog);
|
|
2635
|
+
this.ui.requestRender();
|
|
2636
|
+
// Promise for manual code input (racing with callback server)
|
|
2637
|
+
let manualCodeResolve;
|
|
2638
|
+
let manualCodeReject;
|
|
2639
|
+
const manualCodePromise = new Promise((resolve, reject) => {
|
|
2640
|
+
manualCodeResolve = resolve;
|
|
2641
|
+
manualCodeReject = reject;
|
|
2642
|
+
});
|
|
2643
|
+
// Restore editor helper
|
|
2644
|
+
const restoreEditor = () => {
|
|
2645
|
+
this.editorContainer.clear();
|
|
2646
|
+
this.editorContainer.addChild(this.editor);
|
|
2647
|
+
this.ui.setFocus(this.editor);
|
|
2648
|
+
this.ui.requestRender();
|
|
2649
|
+
};
|
|
2650
|
+
try {
|
|
2651
|
+
await this.session.modelRegistry.authStorage.login(providerId, {
|
|
2652
|
+
onAuth: (info) => {
|
|
2653
|
+
dialog.showAuth(info.url, info.instructions);
|
|
2654
|
+
if (usesCallbackServer) {
|
|
2655
|
+
// Show input for manual paste, racing with callback
|
|
2656
|
+
dialog
|
|
2657
|
+
.showManualInput("Paste redirect URL below, or complete login in browser:")
|
|
2658
|
+
.then((value) => {
|
|
2659
|
+
if (value && manualCodeResolve) {
|
|
2660
|
+
manualCodeResolve(value);
|
|
2661
|
+
manualCodeResolve = undefined;
|
|
2662
|
+
}
|
|
2663
|
+
})
|
|
2664
|
+
.catch(() => {
|
|
2665
|
+
if (manualCodeReject) {
|
|
2666
|
+
manualCodeReject(new Error("Login cancelled"));
|
|
2667
|
+
manualCodeReject = undefined;
|
|
2668
|
+
}
|
|
2669
|
+
});
|
|
2670
|
+
}
|
|
2671
|
+
else if (providerId === "github-copilot") {
|
|
2672
|
+
// GitHub Copilot polls after onAuth
|
|
2673
|
+
dialog.showWaiting("Waiting for browser authentication...");
|
|
2674
|
+
}
|
|
2675
|
+
// For Anthropic: onPrompt is called immediately after
|
|
2676
|
+
},
|
|
2677
|
+
onPrompt: async (prompt) => {
|
|
2678
|
+
return dialog.showPrompt(prompt.message, prompt.placeholder);
|
|
2679
|
+
},
|
|
2680
|
+
onProgress: (message) => {
|
|
2681
|
+
dialog.showProgress(message);
|
|
2682
|
+
},
|
|
2683
|
+
onManualCodeInput: () => manualCodePromise,
|
|
2684
|
+
signal: dialog.signal,
|
|
2685
|
+
});
|
|
2686
|
+
// Success
|
|
2687
|
+
restoreEditor();
|
|
2688
|
+
this.session.modelRegistry.refresh();
|
|
2689
|
+
this.showStatus(`Logged in to ${providerName}. Credentials saved to ${getAuthPath()}`);
|
|
2690
|
+
}
|
|
2691
|
+
catch (error) {
|
|
2692
|
+
restoreEditor();
|
|
2693
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
2694
|
+
if (errorMsg !== "Login cancelled") {
|
|
2695
|
+
this.showError(`Failed to login to ${providerName}: ${errorMsg}`);
|
|
2696
|
+
}
|
|
2697
|
+
}
|
|
2698
|
+
}
|
|
2699
|
+
// =========================================================================
|
|
2700
|
+
// Command handlers
|
|
2701
|
+
// =========================================================================
|
|
2702
|
+
async handleExportCommand(text) {
|
|
2703
|
+
const parts = text.split(/\s+/);
|
|
2704
|
+
const outputPath = parts.length > 1 ? parts[1] : undefined;
|
|
2705
|
+
try {
|
|
2706
|
+
const filePath = await this.session.exportToHtml(outputPath);
|
|
2707
|
+
this.showStatus(`Session exported to: ${filePath}`);
|
|
2708
|
+
}
|
|
2709
|
+
catch (error) {
|
|
2710
|
+
this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
async handleShareCommand() {
|
|
2714
|
+
// Check if gh is available and logged in
|
|
2715
|
+
try {
|
|
2716
|
+
const authResult = spawnSync("gh", ["auth", "status"], { encoding: "utf-8" });
|
|
2717
|
+
if (authResult.status !== 0) {
|
|
2718
|
+
this.showError("GitHub CLI is not logged in. Run 'gh auth login' first.");
|
|
2719
|
+
return;
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
catch {
|
|
2723
|
+
this.showError("GitHub CLI (gh) is not installed. Install it from https://cli.github.com/");
|
|
2724
|
+
return;
|
|
2725
|
+
}
|
|
2726
|
+
// Export to a temp file
|
|
2727
|
+
const tmpFile = path.join(os.tmpdir(), "session.html");
|
|
2728
|
+
try {
|
|
2729
|
+
await this.session.exportToHtml(tmpFile);
|
|
2730
|
+
}
|
|
2731
|
+
catch (error) {
|
|
2732
|
+
this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
2733
|
+
return;
|
|
2734
|
+
}
|
|
2735
|
+
// Show cancellable loader, replacing the editor
|
|
2736
|
+
const loader = new BorderedLoader(this.ui, theme, "Creating gist...");
|
|
2737
|
+
this.editorContainer.clear();
|
|
2738
|
+
this.editorContainer.addChild(loader);
|
|
2739
|
+
this.ui.setFocus(loader);
|
|
2740
|
+
this.ui.requestRender();
|
|
2741
|
+
const restoreEditor = () => {
|
|
2742
|
+
loader.dispose();
|
|
2743
|
+
this.editorContainer.clear();
|
|
2744
|
+
this.editorContainer.addChild(this.editor);
|
|
2745
|
+
this.ui.setFocus(this.editor);
|
|
2746
|
+
try {
|
|
2747
|
+
fs.unlinkSync(tmpFile);
|
|
2748
|
+
}
|
|
2749
|
+
catch {
|
|
2750
|
+
// Ignore cleanup errors
|
|
2751
|
+
}
|
|
2752
|
+
};
|
|
2753
|
+
// Create a secret gist asynchronously
|
|
2754
|
+
let proc = null;
|
|
2755
|
+
loader.onAbort = () => {
|
|
2756
|
+
proc?.kill();
|
|
2757
|
+
restoreEditor();
|
|
2758
|
+
this.showStatus("Share cancelled");
|
|
2759
|
+
};
|
|
2760
|
+
try {
|
|
2761
|
+
const result = await new Promise((resolve) => {
|
|
2762
|
+
proc = spawn("gh", ["gist", "create", "--public=false", tmpFile]);
|
|
2763
|
+
let stdout = "";
|
|
2764
|
+
let stderr = "";
|
|
2765
|
+
proc.stdout?.on("data", (data) => {
|
|
2766
|
+
stdout += data.toString();
|
|
2767
|
+
});
|
|
2768
|
+
proc.stderr?.on("data", (data) => {
|
|
2769
|
+
stderr += data.toString();
|
|
2770
|
+
});
|
|
2771
|
+
proc.on("close", (code) => resolve({ stdout, stderr, code }));
|
|
2772
|
+
});
|
|
2773
|
+
if (loader.signal.aborted)
|
|
2774
|
+
return;
|
|
2775
|
+
restoreEditor();
|
|
2776
|
+
if (result.code !== 0) {
|
|
2777
|
+
const errorMsg = result.stderr?.trim() || "Unknown error";
|
|
2778
|
+
this.showError(`Failed to create gist: ${errorMsg}`);
|
|
2779
|
+
return;
|
|
2780
|
+
}
|
|
2781
|
+
// Extract gist ID from the URL returned by gh
|
|
2782
|
+
// gh returns something like: https://gist.github.com/username/GIST_ID
|
|
2783
|
+
const gistUrl = result.stdout?.trim();
|
|
2784
|
+
const gistId = gistUrl?.split("/").pop();
|
|
2785
|
+
if (!gistId) {
|
|
2786
|
+
this.showError("Failed to parse gist ID from gh output");
|
|
2787
|
+
return;
|
|
2788
|
+
}
|
|
2789
|
+
// Create the preview URL
|
|
2790
|
+
const previewUrl = `https://buildwithpi.ai/session?${gistId}`;
|
|
2791
|
+
this.showStatus(`Share URL: ${previewUrl}\nGist: ${gistUrl}`);
|
|
2792
|
+
}
|
|
2793
|
+
catch (error) {
|
|
2794
|
+
if (!loader.signal.aborted) {
|
|
2795
|
+
restoreEditor();
|
|
2796
|
+
this.showError(`Failed to create gist: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
}
|
|
2800
|
+
handleCopyCommand() {
|
|
2801
|
+
const text = this.session.getLastAssistantText();
|
|
2802
|
+
if (!text) {
|
|
2803
|
+
this.showError("No agent messages to copy yet.");
|
|
2804
|
+
return;
|
|
2805
|
+
}
|
|
2806
|
+
try {
|
|
2807
|
+
copyToClipboard(text);
|
|
2808
|
+
this.showStatus("Copied last agent message to clipboard");
|
|
2809
|
+
}
|
|
2810
|
+
catch (error) {
|
|
2811
|
+
this.showError(error instanceof Error ? error.message : String(error));
|
|
2812
|
+
}
|
|
2813
|
+
}
|
|
2814
|
+
handleNameCommand(text) {
|
|
2815
|
+
const name = text.replace(/^\/name\s*/, "").trim();
|
|
2816
|
+
if (!name) {
|
|
2817
|
+
const currentName = this.sessionManager.getSessionName();
|
|
2818
|
+
if (currentName) {
|
|
2819
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2820
|
+
this.chatContainer.addChild(new Text(theme.fg("dim", `Session name: ${currentName}`), 1, 0));
|
|
2821
|
+
}
|
|
2822
|
+
else {
|
|
2823
|
+
this.showWarning("Usage: /name <name>");
|
|
2824
|
+
}
|
|
2825
|
+
this.ui.requestRender();
|
|
2826
|
+
return;
|
|
2827
|
+
}
|
|
2828
|
+
this.sessionManager.appendSessionInfo(name);
|
|
2829
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2830
|
+
this.chatContainer.addChild(new Text(theme.fg("dim", `Session name set: ${name}`), 1, 0));
|
|
2831
|
+
this.ui.requestRender();
|
|
2832
|
+
}
|
|
2833
|
+
handleSessionCommand() {
|
|
2834
|
+
const stats = this.session.getSessionStats();
|
|
2835
|
+
const sessionName = this.sessionManager.getSessionName();
|
|
2836
|
+
let info = `${theme.bold("Session Info")}\n\n`;
|
|
2837
|
+
if (sessionName) {
|
|
2838
|
+
info += `${theme.fg("dim", "Name:")} ${sessionName}\n`;
|
|
2839
|
+
}
|
|
2840
|
+
info += `${theme.fg("dim", "File:")} ${stats.sessionFile ?? "In-memory"}\n`;
|
|
2841
|
+
info += `${theme.fg("dim", "ID:")} ${stats.sessionId}\n\n`;
|
|
2842
|
+
info += `${theme.bold("Messages")}\n`;
|
|
2843
|
+
info += `${theme.fg("dim", "User:")} ${stats.userMessages}\n`;
|
|
2844
|
+
info += `${theme.fg("dim", "Assistant:")} ${stats.assistantMessages}\n`;
|
|
2845
|
+
info += `${theme.fg("dim", "Tool Calls:")} ${stats.toolCalls}\n`;
|
|
2846
|
+
info += `${theme.fg("dim", "Tool Results:")} ${stats.toolResults}\n`;
|
|
2847
|
+
info += `${theme.fg("dim", "Total:")} ${stats.totalMessages}\n\n`;
|
|
2848
|
+
info += `${theme.bold("Tokens")}\n`;
|
|
2849
|
+
info += `${theme.fg("dim", "Input:")} ${stats.tokens.input.toLocaleString()}\n`;
|
|
2850
|
+
info += `${theme.fg("dim", "Output:")} ${stats.tokens.output.toLocaleString()}\n`;
|
|
2851
|
+
if (stats.tokens.cacheRead > 0) {
|
|
2852
|
+
info += `${theme.fg("dim", "Cache Read:")} ${stats.tokens.cacheRead.toLocaleString()}\n`;
|
|
2853
|
+
}
|
|
2854
|
+
if (stats.tokens.cacheWrite > 0) {
|
|
2855
|
+
info += `${theme.fg("dim", "Cache Write:")} ${stats.tokens.cacheWrite.toLocaleString()}\n`;
|
|
2856
|
+
}
|
|
2857
|
+
info += `${theme.fg("dim", "Total:")} ${stats.tokens.total.toLocaleString()}\n`;
|
|
2858
|
+
if (stats.cost > 0) {
|
|
2859
|
+
info += `\n${theme.bold("Cost")}\n`;
|
|
2860
|
+
info += `${theme.fg("dim", "Total:")} ${stats.cost.toFixed(4)}`;
|
|
2861
|
+
}
|
|
2862
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2863
|
+
this.chatContainer.addChild(new Text(info, 1, 0));
|
|
2864
|
+
this.ui.requestRender();
|
|
2865
|
+
}
|
|
2866
|
+
async handleSkillCommand(skillPath, args) {
|
|
2867
|
+
try {
|
|
2868
|
+
const content = fs.readFileSync(skillPath, "utf-8");
|
|
2869
|
+
// Strip YAML frontmatter if present
|
|
2870
|
+
const body = content.replace(/^---\n[\s\S]*?\n---\n/, "").trim();
|
|
2871
|
+
const message = args ? `${body}\n\n---\n\nUser: ${args}` : body;
|
|
2872
|
+
await this.session.prompt(message);
|
|
2873
|
+
}
|
|
2874
|
+
catch (err) {
|
|
2875
|
+
this.showError(`Failed to load skill: ${err instanceof Error ? err.message : String(err)}`);
|
|
2876
|
+
}
|
|
2877
|
+
}
|
|
2878
|
+
handleChangelogCommand() {
|
|
2879
|
+
const changelogPath = getChangelogPath();
|
|
2880
|
+
const allEntries = parseChangelog(changelogPath);
|
|
2881
|
+
const changelogMarkdown = allEntries.length > 0
|
|
2882
|
+
? allEntries
|
|
2883
|
+
.reverse()
|
|
2884
|
+
.map((e) => e.content)
|
|
2885
|
+
.join("\n\n")
|
|
2886
|
+
: "No changelog entries found.";
|
|
2887
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2888
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
2889
|
+
this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
|
|
2890
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
2891
|
+
this.chatContainer.addChild(new Markdown(changelogMarkdown, 1, 1, getMarkdownTheme()));
|
|
2892
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
2893
|
+
this.ui.requestRender();
|
|
2894
|
+
}
|
|
2895
|
+
/**
|
|
2896
|
+
* Format keybindings for display (e.g., "ctrl+c" -> "Ctrl+C").
|
|
2897
|
+
*/
|
|
2898
|
+
formatKeyDisplay(keys) {
|
|
2899
|
+
const keyArray = Array.isArray(keys) ? keys : [keys];
|
|
2900
|
+
return keyArray
|
|
2901
|
+
.map((key) => key
|
|
2902
|
+
.split("+")
|
|
2903
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
2904
|
+
.join("+"))
|
|
2905
|
+
.join("/");
|
|
2906
|
+
}
|
|
2907
|
+
/**
|
|
2908
|
+
* Get display string for an app keybinding action.
|
|
2909
|
+
*/
|
|
2910
|
+
getAppKeyDisplay(action) {
|
|
2911
|
+
const display = this.keybindings.getDisplayString(action);
|
|
2912
|
+
return this.formatKeyDisplay(display);
|
|
2913
|
+
}
|
|
2914
|
+
/**
|
|
2915
|
+
* Get display string for an editor keybinding action.
|
|
2916
|
+
*/
|
|
2917
|
+
getEditorKeyDisplay(action) {
|
|
2918
|
+
const keys = getEditorKeybindings().getKeys(action);
|
|
2919
|
+
return this.formatKeyDisplay(keys);
|
|
2920
|
+
}
|
|
2921
|
+
handleHotkeysCommand() {
|
|
2922
|
+
// Navigation keybindings
|
|
2923
|
+
const cursorWordLeft = this.getEditorKeyDisplay("cursorWordLeft");
|
|
2924
|
+
const cursorWordRight = this.getEditorKeyDisplay("cursorWordRight");
|
|
2925
|
+
const cursorLineStart = this.getEditorKeyDisplay("cursorLineStart");
|
|
2926
|
+
const cursorLineEnd = this.getEditorKeyDisplay("cursorLineEnd");
|
|
2927
|
+
// Editing keybindings
|
|
2928
|
+
const submit = this.getEditorKeyDisplay("submit");
|
|
2929
|
+
const newLine = this.getEditorKeyDisplay("newLine");
|
|
2930
|
+
const deleteWordBackward = this.getEditorKeyDisplay("deleteWordBackward");
|
|
2931
|
+
const deleteToLineStart = this.getEditorKeyDisplay("deleteToLineStart");
|
|
2932
|
+
const deleteToLineEnd = this.getEditorKeyDisplay("deleteToLineEnd");
|
|
2933
|
+
const tab = this.getEditorKeyDisplay("tab");
|
|
2934
|
+
// App keybindings
|
|
2935
|
+
const interrupt = this.getAppKeyDisplay("interrupt");
|
|
2936
|
+
const clear = this.getAppKeyDisplay("clear");
|
|
2937
|
+
const exit = this.getAppKeyDisplay("exit");
|
|
2938
|
+
const suspend = this.getAppKeyDisplay("suspend");
|
|
2939
|
+
const cycleThinkingLevel = this.getAppKeyDisplay("cycleThinkingLevel");
|
|
2940
|
+
const cycleModelForward = this.getAppKeyDisplay("cycleModelForward");
|
|
2941
|
+
const expandTools = this.getAppKeyDisplay("expandTools");
|
|
2942
|
+
const toggleThinking = this.getAppKeyDisplay("toggleThinking");
|
|
2943
|
+
const externalEditor = this.getAppKeyDisplay("externalEditor");
|
|
2944
|
+
const followUp = this.getAppKeyDisplay("followUp");
|
|
2945
|
+
const dequeue = this.getAppKeyDisplay("dequeue");
|
|
2946
|
+
let hotkeys = `
|
|
2947
|
+
**Navigation**
|
|
2948
|
+
| Key | Action |
|
|
2949
|
+
|-----|--------|
|
|
2950
|
+
| \`Arrow keys\` | Move cursor / browse history (Up when empty) |
|
|
2951
|
+
| \`${cursorWordLeft}\` / \`${cursorWordRight}\` | Move by word |
|
|
2952
|
+
| \`${cursorLineStart}\` | Start of line |
|
|
2953
|
+
| \`${cursorLineEnd}\` | End of line |
|
|
2954
|
+
|
|
2955
|
+
**Editing**
|
|
2956
|
+
| Key | Action |
|
|
2957
|
+
|-----|--------|
|
|
2958
|
+
| \`${submit}\` | Send message |
|
|
2959
|
+
| \`${newLine}\` | New line${process.platform === "win32" ? " (Ctrl+Enter on Windows Terminal)" : ""} |
|
|
2960
|
+
| \`${deleteWordBackward}\` | Delete word backwards |
|
|
2961
|
+
| \`${deleteToLineStart}\` | Delete to start of line |
|
|
2962
|
+
| \`${deleteToLineEnd}\` | Delete to end of line |
|
|
2963
|
+
|
|
2964
|
+
**Other**
|
|
2965
|
+
| Key | Action |
|
|
2966
|
+
|-----|--------|
|
|
2967
|
+
| \`${tab}\` | Path completion / accept autocomplete |
|
|
2968
|
+
| \`${interrupt}\` | Cancel autocomplete / abort streaming |
|
|
2969
|
+
| \`${clear}\` | Clear editor (first) / exit (second) |
|
|
2970
|
+
| \`${exit}\` | Exit (when editor is empty) |
|
|
2971
|
+
| \`${suspend}\` | Suspend to background |
|
|
2972
|
+
| \`${cycleThinkingLevel}\` | Cycle thinking level |
|
|
2973
|
+
| \`${cycleModelForward}\` | Cycle models |
|
|
2974
|
+
| \`${expandTools}\` | Toggle tool output expansion |
|
|
2975
|
+
| \`${toggleThinking}\` | Toggle thinking block visibility |
|
|
2976
|
+
| \`${externalEditor}\` | Edit message in external editor |
|
|
2977
|
+
| \`${followUp}\` | Queue follow-up message |
|
|
2978
|
+
| \`${dequeue}\` | Restore queued messages |
|
|
2979
|
+
| \`Ctrl+V\` | Paste image from clipboard |
|
|
2980
|
+
| \`/\` | Slash commands |
|
|
2981
|
+
| \`!\` | Run bash command |
|
|
2982
|
+
| \`!!\` | Run bash command (excluded from context) |
|
|
2983
|
+
`;
|
|
2984
|
+
// Add extension-registered shortcuts
|
|
2985
|
+
const extensionRunner = this.session.extensionRunner;
|
|
2986
|
+
if (extensionRunner) {
|
|
2987
|
+
const shortcuts = extensionRunner.getShortcuts();
|
|
2988
|
+
if (shortcuts.size > 0) {
|
|
2989
|
+
hotkeys += `
|
|
2990
|
+
**Extensions**
|
|
2991
|
+
| Key | Action |
|
|
2992
|
+
|-----|--------|
|
|
2993
|
+
`;
|
|
2994
|
+
for (const [key, shortcut] of shortcuts) {
|
|
2995
|
+
const description = shortcut.description ?? shortcut.extensionPath;
|
|
2996
|
+
hotkeys += `| \`${key}\` | ${description} |\n`;
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
}
|
|
3000
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3001
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
3002
|
+
this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "Keyboard Shortcuts")), 1, 0));
|
|
3003
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3004
|
+
this.chatContainer.addChild(new Markdown(hotkeys.trim(), 1, 1, getMarkdownTheme()));
|
|
3005
|
+
this.chatContainer.addChild(new DynamicBorder());
|
|
3006
|
+
this.ui.requestRender();
|
|
3007
|
+
}
|
|
3008
|
+
async handleClearCommand() {
|
|
3009
|
+
// Stop loading animation
|
|
3010
|
+
if (this.loadingAnimation) {
|
|
3011
|
+
this.loadingAnimation.stop();
|
|
3012
|
+
this.loadingAnimation = undefined;
|
|
3013
|
+
}
|
|
3014
|
+
this.statusContainer.clear();
|
|
3015
|
+
// New session via session (emits extension session events)
|
|
3016
|
+
await this.session.newSession();
|
|
3017
|
+
// Clear UI state
|
|
3018
|
+
this.chatContainer.clear();
|
|
3019
|
+
this.pendingMessagesContainer.clear();
|
|
3020
|
+
this.compactionQueuedMessages = [];
|
|
3021
|
+
this.streamingComponent = undefined;
|
|
3022
|
+
this.streamingMessage = undefined;
|
|
3023
|
+
this.pendingTools.clear();
|
|
3024
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3025
|
+
this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
|
|
3026
|
+
this.ui.requestRender();
|
|
3027
|
+
}
|
|
3028
|
+
handleDebugCommand() {
|
|
3029
|
+
const width = this.ui.terminal.columns;
|
|
3030
|
+
const allLines = this.ui.render(width);
|
|
3031
|
+
const debugLogPath = getDebugLogPath();
|
|
3032
|
+
const debugData = [
|
|
3033
|
+
`Debug output at ${new Date().toISOString()}`,
|
|
3034
|
+
`Terminal width: ${width}`,
|
|
3035
|
+
`Total lines: ${allLines.length}`,
|
|
3036
|
+
"",
|
|
3037
|
+
"=== All rendered lines with visible widths ===",
|
|
3038
|
+
...allLines.map((line, idx) => {
|
|
3039
|
+
const vw = visibleWidth(line);
|
|
3040
|
+
const escaped = JSON.stringify(line);
|
|
3041
|
+
return `[${idx}] (w=${vw}) ${escaped}`;
|
|
3042
|
+
}),
|
|
3043
|
+
"",
|
|
3044
|
+
"=== Agent messages (JSONL) ===",
|
|
3045
|
+
...this.session.messages.map((msg) => JSON.stringify(msg)),
|
|
3046
|
+
"",
|
|
3047
|
+
].join("\n");
|
|
3048
|
+
fs.mkdirSync(path.dirname(debugLogPath), { recursive: true });
|
|
3049
|
+
fs.writeFileSync(debugLogPath, debugData);
|
|
3050
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3051
|
+
this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ Debug log written")}\n${theme.fg("muted", debugLogPath)}`, 1, 1));
|
|
3052
|
+
this.ui.requestRender();
|
|
3053
|
+
}
|
|
3054
|
+
handleArminSaysHi() {
|
|
3055
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3056
|
+
this.chatContainer.addChild(new ArminComponent(this.ui));
|
|
3057
|
+
this.ui.requestRender();
|
|
3058
|
+
}
|
|
3059
|
+
async handleBashCommand(command, excludeFromContext = false) {
|
|
3060
|
+
const extensionRunner = this.session.extensionRunner;
|
|
3061
|
+
// Emit user_bash event to let extensions intercept
|
|
3062
|
+
const eventResult = extensionRunner
|
|
3063
|
+
? await extensionRunner.emitUserBash({
|
|
3064
|
+
type: "user_bash",
|
|
3065
|
+
command,
|
|
3066
|
+
excludeFromContext,
|
|
3067
|
+
cwd: process.cwd(),
|
|
3068
|
+
})
|
|
3069
|
+
: undefined;
|
|
3070
|
+
// If extension returned a full result, use it directly
|
|
3071
|
+
if (eventResult?.result) {
|
|
3072
|
+
const result = eventResult.result;
|
|
3073
|
+
// Create UI component for display
|
|
3074
|
+
this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
|
|
3075
|
+
if (this.session.isStreaming) {
|
|
3076
|
+
this.pendingMessagesContainer.addChild(this.bashComponent);
|
|
3077
|
+
this.pendingBashComponents.push(this.bashComponent);
|
|
3078
|
+
}
|
|
3079
|
+
else {
|
|
3080
|
+
this.chatContainer.addChild(this.bashComponent);
|
|
3081
|
+
}
|
|
3082
|
+
// Show output and complete
|
|
3083
|
+
if (result.output) {
|
|
3084
|
+
this.bashComponent.appendOutput(result.output);
|
|
3085
|
+
}
|
|
3086
|
+
this.bashComponent.setComplete(result.exitCode, result.cancelled, result.truncated ? { truncated: true, content: result.output } : undefined, result.fullOutputPath);
|
|
3087
|
+
// Record the result in session
|
|
3088
|
+
this.session.recordBashResult(command, result, { excludeFromContext });
|
|
3089
|
+
this.bashComponent = undefined;
|
|
3090
|
+
this.ui.requestRender();
|
|
3091
|
+
return;
|
|
3092
|
+
}
|
|
3093
|
+
// Normal execution path (possibly with custom operations)
|
|
3094
|
+
const isDeferred = this.session.isStreaming;
|
|
3095
|
+
this.bashComponent = new BashExecutionComponent(command, this.ui, excludeFromContext);
|
|
3096
|
+
if (isDeferred) {
|
|
3097
|
+
// Show in pending area when agent is streaming
|
|
3098
|
+
this.pendingMessagesContainer.addChild(this.bashComponent);
|
|
3099
|
+
this.pendingBashComponents.push(this.bashComponent);
|
|
3100
|
+
}
|
|
3101
|
+
else {
|
|
3102
|
+
// Show in chat immediately when agent is idle
|
|
3103
|
+
this.chatContainer.addChild(this.bashComponent);
|
|
3104
|
+
}
|
|
3105
|
+
this.ui.requestRender();
|
|
3106
|
+
try {
|
|
3107
|
+
const result = await this.session.executeBash(command, (chunk) => {
|
|
3108
|
+
if (this.bashComponent) {
|
|
3109
|
+
this.bashComponent.appendOutput(chunk);
|
|
3110
|
+
this.ui.requestRender();
|
|
3111
|
+
}
|
|
3112
|
+
}, { excludeFromContext, operations: eventResult?.operations });
|
|
3113
|
+
if (this.bashComponent) {
|
|
3114
|
+
this.bashComponent.setComplete(result.exitCode, result.cancelled, result.truncated ? { truncated: true, content: result.output } : undefined, result.fullOutputPath);
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
catch (error) {
|
|
3118
|
+
if (this.bashComponent) {
|
|
3119
|
+
this.bashComponent.setComplete(undefined, false);
|
|
3120
|
+
}
|
|
3121
|
+
this.showError(`Bash command failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
3122
|
+
}
|
|
3123
|
+
this.bashComponent = undefined;
|
|
3124
|
+
this.ui.requestRender();
|
|
3125
|
+
}
|
|
3126
|
+
async handleCompactCommand(customInstructions) {
|
|
3127
|
+
const entries = this.sessionManager.getEntries();
|
|
3128
|
+
const messageCount = entries.filter((e) => e.type === "message").length;
|
|
3129
|
+
if (messageCount < 2) {
|
|
3130
|
+
this.showWarning("Nothing to compact (no messages yet)");
|
|
3131
|
+
return;
|
|
3132
|
+
}
|
|
3133
|
+
await this.executeCompaction(customInstructions, false);
|
|
3134
|
+
}
|
|
3135
|
+
async executeCompaction(customInstructions, isAuto = false) {
|
|
3136
|
+
// Stop loading animation
|
|
3137
|
+
if (this.loadingAnimation) {
|
|
3138
|
+
this.loadingAnimation.stop();
|
|
3139
|
+
this.loadingAnimation = undefined;
|
|
3140
|
+
}
|
|
3141
|
+
this.statusContainer.clear();
|
|
3142
|
+
// Set up escape handler during compaction
|
|
3143
|
+
const originalOnEscape = this.defaultEditor.onEscape;
|
|
3144
|
+
this.defaultEditor.onEscape = () => {
|
|
3145
|
+
this.session.abortCompaction();
|
|
3146
|
+
};
|
|
3147
|
+
// Show compacting status
|
|
3148
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
3149
|
+
const label = isAuto ? "Auto-compacting context... (esc to cancel)" : "Compacting context... (esc to cancel)";
|
|
3150
|
+
const compactingLoader = new Loader(this.ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), label);
|
|
3151
|
+
this.statusContainer.addChild(compactingLoader);
|
|
3152
|
+
this.ui.requestRender();
|
|
3153
|
+
try {
|
|
3154
|
+
const result = await this.session.compact(customInstructions);
|
|
3155
|
+
// Rebuild UI
|
|
3156
|
+
this.rebuildChatFromMessages();
|
|
3157
|
+
// Add compaction component at bottom so user sees it without scrolling
|
|
3158
|
+
const msg = createCompactionSummaryMessage(result.summary, result.tokensBefore, new Date().toISOString());
|
|
3159
|
+
this.addMessageToChat(msg);
|
|
3160
|
+
this.footer.invalidate();
|
|
3161
|
+
}
|
|
3162
|
+
catch (error) {
|
|
3163
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
3164
|
+
if (message === "Compaction cancelled" || (error instanceof Error && error.name === "AbortError")) {
|
|
3165
|
+
this.showError("Compaction cancelled");
|
|
3166
|
+
}
|
|
3167
|
+
else {
|
|
3168
|
+
this.showError(`Compaction failed: ${message}`);
|
|
3169
|
+
}
|
|
3170
|
+
}
|
|
3171
|
+
finally {
|
|
3172
|
+
compactingLoader.stop();
|
|
3173
|
+
this.statusContainer.clear();
|
|
3174
|
+
this.defaultEditor.onEscape = originalOnEscape;
|
|
3175
|
+
}
|
|
3176
|
+
void this.flushCompactionQueue({ willRetry: false });
|
|
3177
|
+
}
|
|
3178
|
+
stop() {
|
|
3179
|
+
if (this.loadingAnimation) {
|
|
3180
|
+
this.loadingAnimation.stop();
|
|
3181
|
+
this.loadingAnimation = undefined;
|
|
3182
|
+
}
|
|
3183
|
+
this.footer.dispose();
|
|
3184
|
+
this.footerDataProvider.dispose();
|
|
3185
|
+
if (this.unsubscribe) {
|
|
3186
|
+
this.unsubscribe();
|
|
3187
|
+
}
|
|
3188
|
+
if (this.isInitialized) {
|
|
3189
|
+
this.ui.stop();
|
|
3190
|
+
this.isInitialized = false;
|
|
3191
|
+
}
|
|
3192
|
+
}
|
|
3193
|
+
}
|
|
3194
|
+
//# sourceMappingURL=interactive-mode.js.map
|