@vandeepunk/pi-coding-agent 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2564 -0
- package/README.md +555 -0
- package/dist/cli/args.d.ts +47 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/args.js +286 -0
- package/dist/cli/args.js.map +1 -0
- package/dist/cli/config-selector.d.ts +14 -0
- package/dist/cli/config-selector.d.ts.map +1 -0
- package/dist/cli/config-selector.js +31 -0
- package/dist/cli/config-selector.js.map +1 -0
- package/dist/cli/file-processor.d.ts +15 -0
- package/dist/cli/file-processor.d.ts.map +1 -0
- package/dist/cli/file-processor.js +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 +34 -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 +11 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +68 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +203 -0
- package/dist/config.js.map +1 -0
- package/dist/core/agent-session.d.ts +574 -0
- package/dist/core/agent-session.d.ts.map +1 -0
- package/dist/core/agent-session.js +2260 -0
- package/dist/core/agent-session.js.map +1 -0
- package/dist/core/auth-storage.d.ts +102 -0
- package/dist/core/auth-storage.d.ts.map +1 -0
- package/dist/core/auth-storage.js +282 -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 +212 -0
- package/dist/core/bash-executor.js.map +1 -0
- package/dist/core/compaction/branch-summarization.d.ts +86 -0
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
- package/dist/core/compaction/branch-summarization.js +242 -0
- package/dist/core/compaction/branch-summarization.js.map +1 -0
- package/dist/core/compaction/compaction.d.ts +121 -0
- package/dist/core/compaction/compaction.d.ts.map +1 -0
- package/dist/core/compaction/compaction.js +607 -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/defaults.d.ts +3 -0
- package/dist/core/defaults.d.ts.map +1 -0
- package/dist/core/defaults.js +2 -0
- package/dist/core/defaults.js.map +1 -0
- package/dist/core/diagnostics.d.ts +15 -0
- package/dist/core/diagnostics.d.ts.map +1 -0
- package/dist/core/diagnostics.js +2 -0
- package/dist/core/diagnostics.js.map +1 -0
- package/dist/core/event-bus.d.ts +9 -0
- package/dist/core/event-bus.d.ts.map +1 -0
- package/dist/core/event-bus.js +25 -0
- package/dist/core/event-bus.js.map +1 -0
- package/dist/core/exec.d.ts +29 -0
- package/dist/core/exec.d.ts.map +1 -0
- package/dist/core/exec.js +71 -0
- package/dist/core/exec.js.map +1 -0
- package/dist/core/export-html/ansi-to-html.d.ts +22 -0
- package/dist/core/export-html/ansi-to-html.d.ts.map +1 -0
- package/dist/core/export-html/ansi-to-html.js +249 -0
- package/dist/core/export-html/ansi-to-html.js.map +1 -0
- package/dist/core/export-html/index.d.ts +34 -0
- package/dist/core/export-html/index.d.ts.map +1 -0
- package/dist/core/export-html/index.js +222 -0
- package/dist/core/export-html/index.js.map +1 -0
- package/dist/core/export-html/template.css +909 -0
- package/dist/core/export-html/template.html +54 -0
- package/dist/core/export-html/template.js +1549 -0
- package/dist/core/export-html/tool-renderer.d.ts +35 -0
- package/dist/core/export-html/tool-renderer.d.ts.map +1 -0
- package/dist/core/export-html/tool-renderer.js +57 -0
- package/dist/core/export-html/tool-renderer.js.map +1 -0
- package/dist/core/export-html/vendor/highlight.min.js +1213 -0
- package/dist/core/export-html/vendor/marked.min.js +6 -0
- package/dist/core/extensions/index.d.ts +11 -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 +400 -0
- package/dist/core/extensions/loader.js.map +1 -0
- package/dist/core/extensions/runner.d.ts +129 -0
- package/dist/core/extensions/runner.d.ts.map +1 -0
- package/dist/core/extensions/runner.js +576 -0
- package/dist/core/extensions/runner.js.map +1 -0
- package/dist/core/extensions/types.d.ts +928 -0
- package/dist/core/extensions/types.d.ts.map +1 -0
- package/dist/core/extensions/types.js +35 -0
- package/dist/core/extensions/types.js.map +1 -0
- package/dist/core/extensions/wrapper.d.ts +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 +32 -0
- package/dist/core/footer-data-provider.d.ts.map +1 -0
- package/dist/core/footer-data-provider.js +134 -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 +55 -0
- package/dist/core/keybindings.d.ts.map +1 -0
- package/dist/core/keybindings.js +153 -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 +100 -0
- package/dist/core/model-registry.d.ts.map +1 -0
- package/dist/core/model-registry.js +419 -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 +313 -0
- package/dist/core/model-resolver.js.map +1 -0
- package/dist/core/package-manager.d.ts +131 -0
- package/dist/core/package-manager.d.ts.map +1 -0
- package/dist/core/package-manager.js +1290 -0
- package/dist/core/package-manager.js.map +1 -0
- package/dist/core/prompt-templates.d.ts +50 -0
- package/dist/core/prompt-templates.d.ts.map +1 -0
- package/dist/core/prompt-templates.js +251 -0
- package/dist/core/prompt-templates.js.map +1 -0
- package/dist/core/resolve-config-value.d.ts +17 -0
- package/dist/core/resolve-config-value.d.ts.map +1 -0
- package/dist/core/resolve-config-value.js +59 -0
- package/dist/core/resolve-config-value.js.map +1 -0
- package/dist/core/resource-loader.d.ts +184 -0
- package/dist/core/resource-loader.d.ts.map +1 -0
- package/dist/core/resource-loader.js +673 -0
- package/dist/core/resource-loader.js.map +1 -0
- package/dist/core/sdk.d.ts +90 -0
- package/dist/core/sdk.d.ts.map +1 -0
- package/dist/core/sdk.js +234 -0
- package/dist/core/sdk.js.map +1 -0
- package/dist/core/session-manager.d.ts +323 -0
- package/dist/core/session-manager.d.ts.map +1 -0
- package/dist/core/session-manager.js +1091 -0
- package/dist/core/session-manager.js.map +1 -0
- package/dist/core/settings-manager.d.ts +187 -0
- package/dist/core/settings-manager.d.ts.map +1 -0
- package/dist/core/settings-manager.js +552 -0
- package/dist/core/settings-manager.js.map +1 -0
- package/dist/core/skills.d.ts +58 -0
- package/dist/core/skills.d.ts.map +1 -0
- package/dist/core/skills.js +310 -0
- package/dist/core/skills.js.map +1 -0
- package/dist/core/slash-commands.d.ts +15 -0
- package/dist/core/slash-commands.d.ts.map +1 -0
- package/dist/core/slash-commands.js +21 -0
- package/dist/core/slash-commands.js.map +1 -0
- package/dist/core/system-prompt.d.ts +24 -0
- package/dist/core/system-prompt.d.ts.map +1 -0
- package/dist/core/system-prompt.js +137 -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 +55 -0
- package/dist/core/tools/bash.d.ts.map +1 -0
- package/dist/core/tools/bash.js +242 -0
- package/dist/core/tools/bash.js.map +1 -0
- package/dist/core/tools/edit-diff.d.ts +63 -0
- package/dist/core/tools/edit-diff.d.ts.map +1 -0
- package/dist/core/tools/edit-diff.js +243 -0
- package/dist/core/tools/edit-diff.js.map +1 -0
- package/dist/core/tools/edit.d.ts +39 -0
- package/dist/core/tools/edit.d.ts.map +1 -0
- package/dist/core/tools/edit.js +146 -0
- package/dist/core/tools/edit.js.map +1 -0
- package/dist/core/tools/find.d.ts +39 -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 +45 -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 +73 -0
- package/dist/core/tools/index.d.ts.map +1 -0
- package/dist/core/tools/index.js +61 -0
- package/dist/core/tools/index.js.map +1 -0
- package/dist/core/tools/ls.d.ts +40 -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 +81 -0
- package/dist/core/tools/path-utils.js.map +1 -0
- package/dist/core/tools/read.d.ts +39 -0
- package/dist/core/tools/read.d.ts.map +1 -0
- package/dist/core/tools/read.js +166 -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 +29 -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 +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -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 +623 -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 +16 -0
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/assistant-message.js +91 -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 +162 -0
- package/dist/modes/interactive/components/bash-execution.js.map +1 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
- package/dist/modes/interactive/components/bordered-loader.js +51 -0
- package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/branch-summary-message.js +44 -0
- package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/compaction-summary-message.js +45 -0
- package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -0
- package/dist/modes/interactive/components/config-selector.d.ts +71 -0
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/config-selector.js +479 -0
- package/dist/modes/interactive/components/config-selector.js.map +1 -0
- package/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
- package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -0
- package/dist/modes/interactive/components/countdown-timer.js +33 -0
- package/dist/modes/interactive/components/countdown-timer.js.map +1 -0
- package/dist/modes/interactive/components/custom-editor.d.ts +21 -0
- package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -0
- package/dist/modes/interactive/components/custom-editor.js +70 -0
- package/dist/modes/interactive/components/custom-editor.js.map +1 -0
- package/dist/modes/interactive/components/custom-message.d.ts +20 -0
- package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/custom-message.js +79 -0
- package/dist/modes/interactive/components/custom-message.js.map +1 -0
- package/dist/modes/interactive/components/daxnuts.d.ts +23 -0
- package/dist/modes/interactive/components/daxnuts.d.ts.map +1 -0
- package/dist/modes/interactive/components/daxnuts.js +140 -0
- package/dist/modes/interactive/components/daxnuts.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 +17 -0
- package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-editor.js +102 -0
- package/dist/modes/interactive/components/extension-editor.js.map +1 -0
- package/dist/modes/interactive/components/extension-input.d.ts +23 -0
- package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-input.js +61 -0
- package/dist/modes/interactive/components/extension-input.js.map +1 -0
- package/dist/modes/interactive/components/extension-selector.d.ts +24 -0
- package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/extension-selector.js +78 -0
- package/dist/modes/interactive/components/extension-selector.js.map +1 -0
- package/dist/modes/interactive/components/footer.d.ts +26 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -0
- package/dist/modes/interactive/components/footer.js +220 -0
- package/dist/modes/interactive/components/footer.js.map +1 -0
- package/dist/modes/interactive/components/index.d.ts +32 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -0
- package/dist/modes/interactive/components/index.js +33 -0
- package/dist/modes/interactive/components/index.js.map +1 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts +41 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -0
- package/dist/modes/interactive/components/keybinding-hints.js +61 -0
- package/dist/modes/interactive/components/keybinding-hints.js.map +1 -0
- package/dist/modes/interactive/components/login-dialog.d.ts +42 -0
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -0
- package/dist/modes/interactive/components/login-dialog.js +145 -0
- package/dist/modes/interactive/components/login-dialog.js.map +1 -0
- package/dist/modes/interactive/components/model-selector.d.ts +47 -0
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/model-selector.js +266 -0
- package/dist/modes/interactive/components/model-selector.js.map +1 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts +19 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/oauth-selector.js +97 -0
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts +49 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.js +270 -0
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
- package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
- package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
- package/dist/modes/interactive/components/session-selector-search.js +155 -0
- package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
- package/dist/modes/interactive/components/session-selector.d.ts +95 -0
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/session-selector.js +851 -0
- package/dist/modes/interactive/components/session-selector.js.map +1 -0
- package/dist/modes/interactive/components/settings-selector.d.ts +53 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/settings-selector.js +277 -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/skill-invocation-message.d.ts +17 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
- package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
- package/dist/modes/interactive/components/theme-selector.d.ts +11 -0
- package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/theme-selector.js +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 +621 -0
- package/dist/modes/interactive/components/tool-execution.js.map +1 -0
- package/dist/modes/interactive/components/tree-selector.d.ts +68 -0
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/tree-selector.js +934 -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 +313 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
- package/dist/modes/interactive/interactive-mode.js +3664 -0
- package/dist/modes/interactive/interactive-mode.js.map +1 -0
- package/dist/modes/interactive/theme/dark.json +85 -0
- package/dist/modes/interactive/theme/light.json +84 -0
- package/dist/modes/interactive/theme/theme-schema.json +335 -0
- package/dist/modes/interactive/theme/theme.d.ts +78 -0
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
- package/dist/modes/interactive/theme/theme.js +944 -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 +98 -0
- package/dist/modes/print-mode.js.map +1 -0
- package/dist/modes/rpc/rpc-client.d.ts +217 -0
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
- package/dist/modes/rpc/rpc-client.js +405 -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 +500 -0
- package/dist/modes/rpc/rpc-mode.js.map +1 -0
- package/dist/modes/rpc/rpc-types.d.ts +409 -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 +162 -0
- package/dist/utils/clipboard-image.js.map +1 -0
- package/dist/utils/clipboard-native.d.ts +7 -0
- package/dist/utils/clipboard-native.d.ts.map +1 -0
- package/dist/utils/clipboard-native.js +14 -0
- package/dist/utils/clipboard-native.js.map +1 -0
- package/dist/utils/clipboard.d.ts +2 -0
- package/dist/utils/clipboard.d.ts.map +1 -0
- package/dist/utils/clipboard.js +67 -0
- package/dist/utils/clipboard.js.map +1 -0
- package/dist/utils/frontmatter.d.ts +8 -0
- package/dist/utils/frontmatter.d.ts.map +1 -0
- package/dist/utils/frontmatter.js +26 -0
- package/dist/utils/frontmatter.js.map +1 -0
- package/dist/utils/git.d.ts +2 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +6 -0
- package/dist/utils/git.js.map +1 -0
- package/dist/utils/image-convert.d.ts +9 -0
- package/dist/utils/image-convert.d.ts.map +1 -0
- package/dist/utils/image-convert.js +35 -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 +181 -0
- package/dist/utils/image-resize.js.map +1 -0
- package/dist/utils/mime.d.ts +2 -0
- package/dist/utils/mime.d.ts.map +1 -0
- package/dist/utils/mime.js +26 -0
- package/dist/utils/mime.js.map +1 -0
- package/dist/utils/photon.d.ts +21 -0
- package/dist/utils/photon.d.ts.map +1 -0
- package/dist/utils/photon.js +121 -0
- package/dist/utils/photon.js.map +1 -0
- package/dist/utils/shell.d.ts +26 -0
- package/dist/utils/shell.d.ts.map +1 -0
- package/dist/utils/shell.js +186 -0
- package/dist/utils/shell.js.map +1 -0
- package/dist/utils/sleep.d.ts +5 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/sleep.js +17 -0
- package/dist/utils/sleep.js.map +1 -0
- package/dist/utils/tools-manager.d.ts +3 -0
- package/dist/utils/tools-manager.d.ts.map +1 -0
- package/dist/utils/tools-manager.js +201 -0
- package/dist/utils/tools-manager.js.map +1 -0
- package/docs/compaction.md +390 -0
- package/docs/custom-provider.md +539 -0
- package/docs/development.md +69 -0
- package/docs/extensions.md +1827 -0
- package/docs/images/doom-extension.png +0 -0
- package/docs/images/exy.png +0 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/json.md +79 -0
- package/docs/keybindings.md +174 -0
- package/docs/models.md +254 -0
- package/docs/packages.md +191 -0
- package/docs/prompt-templates.md +67 -0
- package/docs/providers.md +168 -0
- package/docs/rpc.md +1311 -0
- package/docs/sdk.md +957 -0
- package/docs/session.md +412 -0
- package/docs/settings.md +221 -0
- package/docs/shell-aliases.md +13 -0
- package/docs/skills.md +227 -0
- package/docs/terminal-setup.md +70 -0
- package/docs/termux.md +127 -0
- package/docs/themes.md +295 -0
- package/docs/tree.md +219 -0
- package/docs/tui.md +887 -0
- package/docs/windows.md +17 -0
- package/examples/README.md +25 -0
- package/examples/extensions/README.md +202 -0
- package/examples/extensions/antigravity-image-gen.ts +413 -0
- package/examples/extensions/auto-commit-on-exit.ts +49 -0
- package/examples/extensions/bash-spawn-hook.ts +30 -0
- package/examples/extensions/bookmark.ts +50 -0
- package/examples/extensions/claude-rules.ts +86 -0
- package/examples/extensions/commands.ts +72 -0
- package/examples/extensions/confirm-destructive.ts +59 -0
- package/examples/extensions/custom-compaction.ts +114 -0
- package/examples/extensions/custom-footer.ts +64 -0
- package/examples/extensions/custom-header.ts +73 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
- package/examples/extensions/custom-provider-anthropic/package.json +19 -0
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +349 -0
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
- package/examples/extensions/custom-provider-qwen-cli/index.ts +345 -0
- package/examples/extensions/custom-provider-qwen-cli/package.json +16 -0
- package/examples/extensions/dirty-repo-guard.ts +56 -0
- package/examples/extensions/doom-overlay/README.md +46 -0
- package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
- package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
- package/examples/extensions/doom-overlay/doom/build.sh +152 -0
- package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
- package/examples/extensions/doom-overlay/doom-component.ts +132 -0
- package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
- package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
- package/examples/extensions/doom-overlay/index.ts +74 -0
- package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
- package/examples/extensions/dynamic-resources/SKILL.md +8 -0
- package/examples/extensions/dynamic-resources/dynamic.json +79 -0
- package/examples/extensions/dynamic-resources/dynamic.md +5 -0
- package/examples/extensions/dynamic-resources/index.ts +15 -0
- package/examples/extensions/event-bus.ts +43 -0
- package/examples/extensions/file-trigger.ts +41 -0
- package/examples/extensions/git-checkpoint.ts +53 -0
- package/examples/extensions/handoff.ts +150 -0
- package/examples/extensions/hello.ts +25 -0
- package/examples/extensions/inline-bash.ts +94 -0
- package/examples/extensions/input-transform.ts +43 -0
- package/examples/extensions/interactive-shell.ts +196 -0
- package/examples/extensions/mac-system-theme.ts +47 -0
- package/examples/extensions/message-renderer.ts +59 -0
- package/examples/extensions/minimal-mode.ts +426 -0
- package/examples/extensions/modal-editor.ts +85 -0
- package/examples/extensions/model-status.ts +31 -0
- package/examples/extensions/notify.ts +55 -0
- package/examples/extensions/overlay-qa-tests.ts +881 -0
- package/examples/extensions/overlay-test.ts +150 -0
- package/examples/extensions/permission-gate.ts +34 -0
- package/examples/extensions/pirate.ts +47 -0
- package/examples/extensions/plan-mode/README.md +65 -0
- package/examples/extensions/plan-mode/index.ts +340 -0
- package/examples/extensions/plan-mode/utils.ts +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 +264 -0
- package/examples/extensions/questionnaire.ts +427 -0
- package/examples/extensions/rainbow-editor.ts +88 -0
- package/examples/extensions/rpc-demo.ts +124 -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/session-name.ts +27 -0
- package/examples/extensions/shutdown-command.ts +63 -0
- package/examples/extensions/snake.ts +343 -0
- package/examples/extensions/space-invaders.ts +560 -0
- package/examples/extensions/ssh.ts +220 -0
- package/examples/extensions/status-line.ts +40 -0
- package/examples/extensions/subagent/README.md +172 -0
- package/examples/extensions/subagent/agents/planner.md +37 -0
- package/examples/extensions/subagent/agents/reviewer.md +35 -0
- package/examples/extensions/subagent/agents/scout.md +50 -0
- package/examples/extensions/subagent/agents/worker.md +24 -0
- package/examples/extensions/subagent/agents.ts +127 -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/system-prompt-header.ts +17 -0
- package/examples/extensions/timed-confirm.ts +70 -0
- package/examples/extensions/titlebar-spinner.ts +58 -0
- package/examples/extensions/todo.ts +299 -0
- package/examples/extensions/tool-override.ts +143 -0
- package/examples/extensions/tools.ts +146 -0
- package/examples/extensions/trigger-compact.ts +40 -0
- package/examples/extensions/truncated-tool.ts +192 -0
- package/examples/extensions/widget-placement.ts +17 -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/rpc-extension-ui.ts +632 -0
- package/examples/sdk/01-minimal.ts +22 -0
- package/examples/sdk/02-custom-model.ts +49 -0
- package/examples/sdk/03-custom-prompt.ts +55 -0
- package/examples/sdk/04-skills.ts +46 -0
- package/examples/sdk/05-tools.ts +56 -0
- package/examples/sdk/06-extensions.ts +88 -0
- package/examples/sdk/07-context-files.ts +40 -0
- package/examples/sdk/08-prompt-templates.ts +47 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
- package/examples/sdk/10-settings.ts +38 -0
- package/examples/sdk/11-sessions.ts +48 -0
- package/examples/sdk/12-full-control.ts +82 -0
- package/examples/sdk/README.md +144 -0
- package/package.json +97 -0
|
@@ -0,0 +1,1827 @@
|
|
|
1
|
+
> pi can create extensions. Ask it to build one for your use case.
|
|
2
|
+
|
|
3
|
+
# Extensions
|
|
4
|
+
|
|
5
|
+
Extensions are TypeScript modules that extend pi's behavior. They can subscribe to lifecycle events, register custom tools callable by the LLM, add commands, and more.
|
|
6
|
+
|
|
7
|
+
> **Placement for /reload:** Put extensions in `~/.pi/agent/extensions/` (global) or `.pi/extensions/` (project-local) for auto-discovery. Use `pi -e ./path.ts` only for quick tests. Extensions in auto-discovered locations can be hot-reloaded with `/reload`.
|
|
8
|
+
|
|
9
|
+
**Key capabilities:**
|
|
10
|
+
- **Custom tools** - Register tools the LLM can call via `pi.registerTool()`
|
|
11
|
+
- **Event interception** - Block or modify tool calls, inject context, customize compaction
|
|
12
|
+
- **User interaction** - Prompt users via `ctx.ui` (select, confirm, input, notify)
|
|
13
|
+
- **Custom UI components** - Full TUI components with keyboard input via `ctx.ui.custom()` for complex interactions
|
|
14
|
+
- **Custom commands** - Register commands like `/mycommand` via `pi.registerCommand()`
|
|
15
|
+
- **Session persistence** - Store state that survives restarts via `pi.appendEntry()`
|
|
16
|
+
- **Custom rendering** - Control how tool calls/results and messages appear in TUI
|
|
17
|
+
|
|
18
|
+
**Example use cases:**
|
|
19
|
+
- Permission gates (confirm before `rm -rf`, `sudo`, etc.)
|
|
20
|
+
- Git checkpointing (stash at each turn, restore on branch)
|
|
21
|
+
- Path protection (block writes to `.env`, `node_modules/`)
|
|
22
|
+
- Custom compaction (summarize conversation your way)
|
|
23
|
+
- Conversation summaries (see `summarize.ts` example)
|
|
24
|
+
- Interactive tools (questions, wizards, custom dialogs)
|
|
25
|
+
- Stateful tools (todo lists, connection pools)
|
|
26
|
+
- External integrations (file watchers, webhooks, CI triggers)
|
|
27
|
+
- Games while you wait (see `snake.ts` example)
|
|
28
|
+
|
|
29
|
+
See [examples/extensions/](../examples/extensions/) for working implementations.
|
|
30
|
+
|
|
31
|
+
## Table of Contents
|
|
32
|
+
|
|
33
|
+
- [Quick Start](#quick-start)
|
|
34
|
+
- [Extension Locations](#extension-locations)
|
|
35
|
+
- [Available Imports](#available-imports)
|
|
36
|
+
- [Writing an Extension](#writing-an-extension)
|
|
37
|
+
- [Extension Styles](#extension-styles)
|
|
38
|
+
- [Events](#events)
|
|
39
|
+
- [Lifecycle Overview](#lifecycle-overview)
|
|
40
|
+
- [Session Events](#session-events)
|
|
41
|
+
- [Agent Events](#agent-events)
|
|
42
|
+
- [Tool Events](#tool-events)
|
|
43
|
+
- [ExtensionContext](#extensioncontext)
|
|
44
|
+
- [ExtensionCommandContext](#extensioncommandcontext)
|
|
45
|
+
- [ExtensionAPI Methods](#extensionapi-methods)
|
|
46
|
+
- [State Management](#state-management)
|
|
47
|
+
- [Custom Tools](#custom-tools)
|
|
48
|
+
- [Custom UI](#custom-ui)
|
|
49
|
+
- [Error Handling](#error-handling)
|
|
50
|
+
- [Mode Behavior](#mode-behavior)
|
|
51
|
+
- [Examples Reference](#examples-reference)
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
Create `~/.pi/agent/extensions/my-extension.ts`:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
59
|
+
import { Type } from "@sinclair/typebox";
|
|
60
|
+
|
|
61
|
+
export default function (pi: ExtensionAPI) {
|
|
62
|
+
// React to events
|
|
63
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
64
|
+
ctx.ui.notify("Extension loaded!", "info");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
68
|
+
if (event.toolName === "bash" && event.input.command?.includes("rm -rf")) {
|
|
69
|
+
const ok = await ctx.ui.confirm("Dangerous!", "Allow rm -rf?");
|
|
70
|
+
if (!ok) return { block: true, reason: "Blocked by user" };
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Register a custom tool
|
|
75
|
+
pi.registerTool({
|
|
76
|
+
name: "greet",
|
|
77
|
+
label: "Greet",
|
|
78
|
+
description: "Greet someone by name",
|
|
79
|
+
parameters: Type.Object({
|
|
80
|
+
name: Type.String({ description: "Name to greet" }),
|
|
81
|
+
}),
|
|
82
|
+
async execute(toolCallId, params, signal, onUpdate, ctx) {
|
|
83
|
+
return {
|
|
84
|
+
content: [{ type: "text", text: `Hello, ${params.name}!` }],
|
|
85
|
+
details: {},
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Register a command
|
|
91
|
+
pi.registerCommand("hello", {
|
|
92
|
+
description: "Say hello",
|
|
93
|
+
handler: async (args, ctx) => {
|
|
94
|
+
ctx.ui.notify(`Hello ${args || "world"}!`, "info");
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Test with `--extension` (or `-e`) flag:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
pi -e ./my-extension.ts
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Extension Locations
|
|
107
|
+
|
|
108
|
+
> **Security:** Extensions run with your full system permissions and can execute arbitrary code. Only install from sources you trust.
|
|
109
|
+
|
|
110
|
+
Extensions are auto-discovered from:
|
|
111
|
+
|
|
112
|
+
| Location | Scope |
|
|
113
|
+
|----------|-------|
|
|
114
|
+
| `~/.pi/agent/extensions/*.ts` | Global (all projects) |
|
|
115
|
+
| `~/.pi/agent/extensions/*/index.ts` | Global (subdirectory) |
|
|
116
|
+
| `.pi/extensions/*.ts` | Project-local |
|
|
117
|
+
| `.pi/extensions/*/index.ts` | Project-local (subdirectory) |
|
|
118
|
+
|
|
119
|
+
Additional paths via `settings.json`:
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"packages": [
|
|
124
|
+
"npm:@foo/bar@1.0.0",
|
|
125
|
+
"git:github.com/user/repo@v1"
|
|
126
|
+
],
|
|
127
|
+
"extensions": [
|
|
128
|
+
"/path/to/local/extension.ts",
|
|
129
|
+
"/path/to/local/extension/dir"
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
To share extensions via npm or git as pi packages, see [packages.md](packages.md).
|
|
135
|
+
|
|
136
|
+
## Available Imports
|
|
137
|
+
|
|
138
|
+
| Package | Purpose |
|
|
139
|
+
|---------|---------|
|
|
140
|
+
| `@mariozechner/pi-coding-agent` | Extension types (`ExtensionAPI`, `ExtensionContext`, events) |
|
|
141
|
+
| `@sinclair/typebox` | Schema definitions for tool parameters |
|
|
142
|
+
| `@mariozechner/pi-ai` | AI utilities (`StringEnum` for Google-compatible enums) |
|
|
143
|
+
| `@mariozechner/pi-tui` | TUI components for custom rendering |
|
|
144
|
+
|
|
145
|
+
npm dependencies work too. Add a `package.json` next to your extension (or in a parent directory), run `npm install`, and imports from `node_modules/` are resolved automatically.
|
|
146
|
+
|
|
147
|
+
Node.js built-ins (`node:fs`, `node:path`, etc.) are also available.
|
|
148
|
+
|
|
149
|
+
## Writing an Extension
|
|
150
|
+
|
|
151
|
+
An extension exports a default function that receives `ExtensionAPI`:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
155
|
+
|
|
156
|
+
export default function (pi: ExtensionAPI) {
|
|
157
|
+
// Subscribe to events
|
|
158
|
+
pi.on("event_name", async (event, ctx) => {
|
|
159
|
+
// ctx.ui for user interaction
|
|
160
|
+
const ok = await ctx.ui.confirm("Title", "Are you sure?");
|
|
161
|
+
ctx.ui.notify("Done!", "success");
|
|
162
|
+
ctx.ui.setStatus("my-ext", "Processing..."); // Footer status
|
|
163
|
+
ctx.ui.setWidget("my-ext", ["Line 1", "Line 2"]); // Widget above editor (default)
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Register tools, commands, shortcuts, flags
|
|
167
|
+
pi.registerTool({ ... });
|
|
168
|
+
pi.registerCommand("name", { ... });
|
|
169
|
+
pi.registerShortcut("ctrl+x", { ... });
|
|
170
|
+
pi.registerFlag("my-flag", { ... });
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Extensions are loaded via [jiti](https://github.com/unjs/jiti), so TypeScript works without compilation.
|
|
175
|
+
|
|
176
|
+
### Extension Styles
|
|
177
|
+
|
|
178
|
+
**Single file** - simplest, for small extensions:
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
~/.pi/agent/extensions/
|
|
182
|
+
└── my-extension.ts
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**Directory with index.ts** - for multi-file extensions:
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
~/.pi/agent/extensions/
|
|
189
|
+
└── my-extension/
|
|
190
|
+
├── index.ts # Entry point (exports default function)
|
|
191
|
+
├── tools.ts # Helper module
|
|
192
|
+
└── utils.ts # Helper module
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
**Package with dependencies** - for extensions that need npm packages:
|
|
196
|
+
|
|
197
|
+
```
|
|
198
|
+
~/.pi/agent/extensions/
|
|
199
|
+
└── my-extension/
|
|
200
|
+
├── package.json # Declares dependencies and entry points
|
|
201
|
+
├── package-lock.json
|
|
202
|
+
├── node_modules/ # After npm install
|
|
203
|
+
└── src/
|
|
204
|
+
└── index.ts
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
```json
|
|
208
|
+
// package.json
|
|
209
|
+
{
|
|
210
|
+
"name": "my-extension",
|
|
211
|
+
"dependencies": {
|
|
212
|
+
"zod": "^3.0.0",
|
|
213
|
+
"chalk": "^5.0.0"
|
|
214
|
+
},
|
|
215
|
+
"pi": {
|
|
216
|
+
"extensions": ["./src/index.ts"]
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Run `npm install` in the extension directory, then imports from `node_modules/` work automatically.
|
|
222
|
+
|
|
223
|
+
## Events
|
|
224
|
+
|
|
225
|
+
### Lifecycle Overview
|
|
226
|
+
|
|
227
|
+
```
|
|
228
|
+
pi starts
|
|
229
|
+
│
|
|
230
|
+
└─► session_start
|
|
231
|
+
│
|
|
232
|
+
▼
|
|
233
|
+
user sends prompt ─────────────────────────────────────────┐
|
|
234
|
+
│ │
|
|
235
|
+
├─► (extension commands checked first, bypass if found) │
|
|
236
|
+
├─► input (can intercept, transform, or handle) │
|
|
237
|
+
├─► (skill/template expansion if not handled) │
|
|
238
|
+
├─► before_agent_start (can inject message, modify system prompt)
|
|
239
|
+
├─► agent_start │
|
|
240
|
+
│ │
|
|
241
|
+
│ ┌─── turn (repeats while LLM calls tools) ───┐ │
|
|
242
|
+
│ │ │ │
|
|
243
|
+
│ ├─► turn_start │ │
|
|
244
|
+
│ ├─► context (can modify messages) │ │
|
|
245
|
+
│ │ │ │
|
|
246
|
+
│ │ LLM responds, may call tools: │ │
|
|
247
|
+
│ │ ├─► tool_call (can block) │ │
|
|
248
|
+
│ │ │ tool executes │ │
|
|
249
|
+
│ │ └─► tool_result (can modify) │ │
|
|
250
|
+
│ │ │ │
|
|
251
|
+
│ └─► turn_end │ │
|
|
252
|
+
│ │
|
|
253
|
+
└─► agent_end │
|
|
254
|
+
│
|
|
255
|
+
user sends another prompt ◄────────────────────────────────┘
|
|
256
|
+
|
|
257
|
+
/new (new session) or /resume (switch session)
|
|
258
|
+
├─► session_before_switch (can cancel)
|
|
259
|
+
└─► session_switch
|
|
260
|
+
|
|
261
|
+
/fork
|
|
262
|
+
├─► session_before_fork (can cancel)
|
|
263
|
+
└─► session_fork
|
|
264
|
+
|
|
265
|
+
/compact or auto-compaction
|
|
266
|
+
├─► session_before_compact (can cancel or customize)
|
|
267
|
+
└─► session_compact
|
|
268
|
+
|
|
269
|
+
/tree navigation
|
|
270
|
+
├─► session_before_tree (can cancel or customize)
|
|
271
|
+
└─► session_tree
|
|
272
|
+
|
|
273
|
+
/model or Ctrl+P (model selection/cycling)
|
|
274
|
+
└─► model_select
|
|
275
|
+
|
|
276
|
+
exit (Ctrl+C, Ctrl+D)
|
|
277
|
+
└─► session_shutdown
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Session Events
|
|
281
|
+
|
|
282
|
+
See [session.md](session.md) for session storage internals and the SessionManager API.
|
|
283
|
+
|
|
284
|
+
#### session_start
|
|
285
|
+
|
|
286
|
+
Fired on initial session load.
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
290
|
+
ctx.ui.notify(`Session: ${ctx.sessionManager.getSessionFile() ?? "ephemeral"}`, "info");
|
|
291
|
+
});
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
#### session_before_switch / session_switch
|
|
295
|
+
|
|
296
|
+
Fired when starting a new session (`/new`) or switching sessions (`/resume`).
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
pi.on("session_before_switch", async (event, ctx) => {
|
|
300
|
+
// event.reason - "new" or "resume"
|
|
301
|
+
// event.targetSessionFile - session we're switching to (only for "resume")
|
|
302
|
+
|
|
303
|
+
if (event.reason === "new") {
|
|
304
|
+
const ok = await ctx.ui.confirm("Clear?", "Delete all messages?");
|
|
305
|
+
if (!ok) return { cancel: true };
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
pi.on("session_switch", async (event, ctx) => {
|
|
310
|
+
// event.reason - "new" or "resume"
|
|
311
|
+
// event.previousSessionFile - session we came from
|
|
312
|
+
});
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
#### session_before_fork / session_fork
|
|
316
|
+
|
|
317
|
+
Fired when forking via `/fork`.
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
pi.on("session_before_fork", async (event, ctx) => {
|
|
321
|
+
// event.entryId - ID of the entry being forked from
|
|
322
|
+
return { cancel: true }; // Cancel fork
|
|
323
|
+
// OR
|
|
324
|
+
return { skipConversationRestore: true }; // Fork but don't rewind messages
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
pi.on("session_fork", async (event, ctx) => {
|
|
328
|
+
// event.previousSessionFile - previous session file
|
|
329
|
+
});
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
#### session_before_compact / session_compact
|
|
333
|
+
|
|
334
|
+
Fired on compaction. See [compaction.md](compaction.md) for details.
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
pi.on("session_before_compact", async (event, ctx) => {
|
|
338
|
+
const { preparation, branchEntries, customInstructions, signal } = event;
|
|
339
|
+
|
|
340
|
+
// Cancel:
|
|
341
|
+
return { cancel: true };
|
|
342
|
+
|
|
343
|
+
// Custom summary:
|
|
344
|
+
return {
|
|
345
|
+
compaction: {
|
|
346
|
+
summary: "...",
|
|
347
|
+
firstKeptEntryId: preparation.firstKeptEntryId,
|
|
348
|
+
tokensBefore: preparation.tokensBefore,
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
pi.on("session_compact", async (event, ctx) => {
|
|
354
|
+
// event.compactionEntry - the saved compaction
|
|
355
|
+
// event.fromExtension - whether extension provided it
|
|
356
|
+
});
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
#### session_before_tree / session_tree
|
|
360
|
+
|
|
361
|
+
Fired on `/tree` navigation. See [tree.md](tree.md) for tree navigation concepts.
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
pi.on("session_before_tree", async (event, ctx) => {
|
|
365
|
+
const { preparation, signal } = event;
|
|
366
|
+
return { cancel: true };
|
|
367
|
+
// OR provide custom summary:
|
|
368
|
+
return { summary: { summary: "...", details: {} } };
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
pi.on("session_tree", async (event, ctx) => {
|
|
372
|
+
// event.newLeafId, oldLeafId, summaryEntry, fromExtension
|
|
373
|
+
});
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
#### session_shutdown
|
|
377
|
+
|
|
378
|
+
Fired on exit (Ctrl+C, Ctrl+D, SIGTERM).
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
pi.on("session_shutdown", async (_event, ctx) => {
|
|
382
|
+
// Cleanup, save state, etc.
|
|
383
|
+
});
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
### Agent Events
|
|
387
|
+
|
|
388
|
+
#### before_agent_start
|
|
389
|
+
|
|
390
|
+
Fired after user submits prompt, before agent loop. Can inject a message and/or modify the system prompt.
|
|
391
|
+
|
|
392
|
+
```typescript
|
|
393
|
+
pi.on("before_agent_start", async (event, ctx) => {
|
|
394
|
+
// event.prompt - user's prompt text
|
|
395
|
+
// event.images - attached images (if any)
|
|
396
|
+
// event.systemPrompt - current system prompt
|
|
397
|
+
|
|
398
|
+
return {
|
|
399
|
+
// Inject a persistent message (stored in session, sent to LLM)
|
|
400
|
+
message: {
|
|
401
|
+
customType: "my-extension",
|
|
402
|
+
content: "Additional context for the LLM",
|
|
403
|
+
display: true,
|
|
404
|
+
},
|
|
405
|
+
// Replace the system prompt for this turn (chained across extensions)
|
|
406
|
+
systemPrompt: event.systemPrompt + "\n\nExtra instructions for this turn...",
|
|
407
|
+
};
|
|
408
|
+
});
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
#### agent_start / agent_end
|
|
412
|
+
|
|
413
|
+
Fired once per user prompt.
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
pi.on("agent_start", async (_event, ctx) => {});
|
|
417
|
+
|
|
418
|
+
pi.on("agent_end", async (event, ctx) => {
|
|
419
|
+
// event.messages - messages from this prompt
|
|
420
|
+
});
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
#### turn_start / turn_end
|
|
424
|
+
|
|
425
|
+
Fired for each turn (one LLM response + tool calls).
|
|
426
|
+
|
|
427
|
+
```typescript
|
|
428
|
+
pi.on("turn_start", async (event, ctx) => {
|
|
429
|
+
// event.turnIndex, event.timestamp
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
pi.on("turn_end", async (event, ctx) => {
|
|
433
|
+
// event.turnIndex, event.message, event.toolResults
|
|
434
|
+
});
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
#### context
|
|
438
|
+
|
|
439
|
+
Fired before each LLM call. Modify messages non-destructively. See [session.md](session.md) for message types.
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
pi.on("context", async (event, ctx) => {
|
|
443
|
+
// event.messages - deep copy, safe to modify
|
|
444
|
+
const filtered = event.messages.filter(m => !shouldPrune(m));
|
|
445
|
+
return { messages: filtered };
|
|
446
|
+
});
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
### Model Events
|
|
450
|
+
|
|
451
|
+
#### model_select
|
|
452
|
+
|
|
453
|
+
Fired when the model changes via `/model` command, model cycling (`Ctrl+P`), or session restore.
|
|
454
|
+
|
|
455
|
+
```typescript
|
|
456
|
+
pi.on("model_select", async (event, ctx) => {
|
|
457
|
+
// event.model - newly selected model
|
|
458
|
+
// event.previousModel - previous model (undefined if first selection)
|
|
459
|
+
// event.source - "set" | "cycle" | "restore"
|
|
460
|
+
|
|
461
|
+
const prev = event.previousModel
|
|
462
|
+
? `${event.previousModel.provider}/${event.previousModel.id}`
|
|
463
|
+
: "none";
|
|
464
|
+
const next = `${event.model.provider}/${event.model.id}`;
|
|
465
|
+
|
|
466
|
+
ctx.ui.notify(`Model changed (${event.source}): ${prev} -> ${next}`, "info");
|
|
467
|
+
});
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
Use this to update UI elements (status bars, footers) or perform model-specific initialization when the active model changes.
|
|
471
|
+
|
|
472
|
+
### Tool Events
|
|
473
|
+
|
|
474
|
+
#### tool_call
|
|
475
|
+
|
|
476
|
+
Fired before tool executes. **Can block.** Use `isToolCallEventType` to narrow and get typed inputs.
|
|
477
|
+
|
|
478
|
+
```typescript
|
|
479
|
+
import { isToolCallEventType } from "@mariozechner/pi-coding-agent";
|
|
480
|
+
|
|
481
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
482
|
+
// event.toolName - "bash", "read", "write", "edit", etc.
|
|
483
|
+
// event.toolCallId
|
|
484
|
+
// event.input - tool parameters
|
|
485
|
+
|
|
486
|
+
// Built-in tools: no type params needed
|
|
487
|
+
if (isToolCallEventType("bash", event)) {
|
|
488
|
+
// event.input is { command: string; timeout?: number }
|
|
489
|
+
if (event.input.command.includes("rm -rf")) {
|
|
490
|
+
return { block: true, reason: "Dangerous command" };
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (isToolCallEventType("read", event)) {
|
|
495
|
+
// event.input is { path: string; offset?: number; limit?: number }
|
|
496
|
+
console.log(`Reading: ${event.input.path}`);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
#### Typing custom tool input
|
|
502
|
+
|
|
503
|
+
Custom tools should export their input type:
|
|
504
|
+
|
|
505
|
+
```typescript
|
|
506
|
+
// my-extension.ts
|
|
507
|
+
export type MyToolInput = Static<typeof myToolSchema>;
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
Use `isToolCallEventType` with explicit type parameters:
|
|
511
|
+
|
|
512
|
+
```typescript
|
|
513
|
+
import { isToolCallEventType } from "@mariozechner/pi-coding-agent";
|
|
514
|
+
import type { MyToolInput } from "my-extension";
|
|
515
|
+
|
|
516
|
+
pi.on("tool_call", (event) => {
|
|
517
|
+
if (isToolCallEventType<"my_tool", MyToolInput>("my_tool", event)) {
|
|
518
|
+
event.input.action; // typed
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
#### tool_result
|
|
524
|
+
|
|
525
|
+
Fired after tool executes. **Can modify result.**
|
|
526
|
+
|
|
527
|
+
```typescript
|
|
528
|
+
import { isBashToolResult } from "@mariozechner/pi-coding-agent";
|
|
529
|
+
|
|
530
|
+
pi.on("tool_result", async (event, ctx) => {
|
|
531
|
+
// event.toolName, event.toolCallId, event.input
|
|
532
|
+
// event.content, event.details, event.isError
|
|
533
|
+
|
|
534
|
+
if (isBashToolResult(event)) {
|
|
535
|
+
// event.details is typed as BashToolDetails
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// Modify result:
|
|
539
|
+
return { content: [...], details: {...}, isError: false };
|
|
540
|
+
});
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
### User Bash Events
|
|
544
|
+
|
|
545
|
+
#### user_bash
|
|
546
|
+
|
|
547
|
+
Fired when user executes `!` or `!!` commands. **Can intercept.**
|
|
548
|
+
|
|
549
|
+
```typescript
|
|
550
|
+
pi.on("user_bash", (event, ctx) => {
|
|
551
|
+
// event.command - the bash command
|
|
552
|
+
// event.excludeFromContext - true if !! prefix
|
|
553
|
+
// event.cwd - working directory
|
|
554
|
+
|
|
555
|
+
// Option 1: Provide custom operations (e.g., SSH)
|
|
556
|
+
return { operations: remoteBashOps };
|
|
557
|
+
|
|
558
|
+
// Option 2: Full replacement - return result directly
|
|
559
|
+
return { result: { output: "...", exitCode: 0, cancelled: false, truncated: false } };
|
|
560
|
+
});
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
### Input Events
|
|
564
|
+
|
|
565
|
+
#### input
|
|
566
|
+
|
|
567
|
+
Fired when user input is received, after extension commands are checked but before skill and template expansion. The event sees the raw input text, so `/skill:foo` and `/template` are not yet expanded.
|
|
568
|
+
|
|
569
|
+
**Processing order:**
|
|
570
|
+
1. Extension commands (`/cmd`) checked first - if found, handler runs and input event is skipped
|
|
571
|
+
2. `input` event fires - can intercept, transform, or handle
|
|
572
|
+
3. If not handled: skill commands (`/skill:name`) expanded to skill content
|
|
573
|
+
4. If not handled: prompt templates (`/template`) expanded to template content
|
|
574
|
+
5. Agent processing begins (`before_agent_start`, etc.)
|
|
575
|
+
|
|
576
|
+
```typescript
|
|
577
|
+
pi.on("input", async (event, ctx) => {
|
|
578
|
+
// event.text - raw input (before skill/template expansion)
|
|
579
|
+
// event.images - attached images, if any
|
|
580
|
+
// event.source - "interactive" (typed), "rpc" (API), or "extension" (via sendUserMessage)
|
|
581
|
+
|
|
582
|
+
// Transform: rewrite input before expansion
|
|
583
|
+
if (event.text.startsWith("?quick "))
|
|
584
|
+
return { action: "transform", text: `Respond briefly: ${event.text.slice(7)}` };
|
|
585
|
+
|
|
586
|
+
// Handle: respond without LLM (extension shows its own feedback)
|
|
587
|
+
if (event.text === "ping") {
|
|
588
|
+
ctx.ui.notify("pong", "info");
|
|
589
|
+
return { action: "handled" };
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// Route by source: skip processing for extension-injected messages
|
|
593
|
+
if (event.source === "extension") return { action: "continue" };
|
|
594
|
+
|
|
595
|
+
// Intercept skill commands before expansion
|
|
596
|
+
if (event.text.startsWith("/skill:")) {
|
|
597
|
+
// Could transform, block, or let pass through
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return { action: "continue" }; // Default: pass through to expansion
|
|
601
|
+
});
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
**Results:**
|
|
605
|
+
- `continue` - pass through unchanged (default if handler returns nothing)
|
|
606
|
+
- `transform` - modify text/images, then continue to expansion
|
|
607
|
+
- `handled` - skip agent entirely (first handler to return this wins)
|
|
608
|
+
|
|
609
|
+
Transforms chain across handlers. See [input-transform.ts](../examples/extensions/input-transform.ts).
|
|
610
|
+
|
|
611
|
+
## ExtensionContext
|
|
612
|
+
|
|
613
|
+
Every handler receives `ctx: ExtensionContext`:
|
|
614
|
+
|
|
615
|
+
### ctx.ui
|
|
616
|
+
|
|
617
|
+
UI methods for user interaction. See [Custom UI](#custom-ui) for full details.
|
|
618
|
+
|
|
619
|
+
### ctx.hasUI
|
|
620
|
+
|
|
621
|
+
`false` in print mode (`-p`), JSON mode, and RPC mode. Always check before using `ctx.ui`.
|
|
622
|
+
|
|
623
|
+
### ctx.cwd
|
|
624
|
+
|
|
625
|
+
Current working directory.
|
|
626
|
+
|
|
627
|
+
### ctx.sessionManager
|
|
628
|
+
|
|
629
|
+
Read-only access to session state. See [session.md](session.md) for the full SessionManager API and entry types.
|
|
630
|
+
|
|
631
|
+
```typescript
|
|
632
|
+
ctx.sessionManager.getEntries() // All entries
|
|
633
|
+
ctx.sessionManager.getBranch() // Current branch
|
|
634
|
+
ctx.sessionManager.getLeafId() // Current leaf entry ID
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
### ctx.modelRegistry / ctx.model
|
|
638
|
+
|
|
639
|
+
Access to models and API keys.
|
|
640
|
+
|
|
641
|
+
### ctx.isIdle() / ctx.abort() / ctx.hasPendingMessages()
|
|
642
|
+
|
|
643
|
+
Control flow helpers.
|
|
644
|
+
|
|
645
|
+
### ctx.shutdown()
|
|
646
|
+
|
|
647
|
+
Request a graceful shutdown of pi.
|
|
648
|
+
|
|
649
|
+
- **Interactive mode:** Deferred until the agent becomes idle (after processing all queued steering and follow-up messages).
|
|
650
|
+
- **RPC mode:** Deferred until the next idle state (after completing the current command response, when waiting for the next command).
|
|
651
|
+
- **Print mode:** No-op. The process exits automatically when all prompts are processed.
|
|
652
|
+
|
|
653
|
+
Emits `session_shutdown` event to all extensions before exiting. Available in all contexts (event handlers, tools, commands, shortcuts).
|
|
654
|
+
|
|
655
|
+
```typescript
|
|
656
|
+
pi.on("tool_call", (event, ctx) => {
|
|
657
|
+
if (isFatal(event.input)) {
|
|
658
|
+
ctx.shutdown();
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
### ctx.getContextUsage()
|
|
664
|
+
|
|
665
|
+
Returns current context usage for the active model. Uses last assistant usage when available, then estimates tokens for trailing messages.
|
|
666
|
+
|
|
667
|
+
```typescript
|
|
668
|
+
const usage = ctx.getContextUsage();
|
|
669
|
+
if (usage && usage.tokens > 100_000) {
|
|
670
|
+
// ...
|
|
671
|
+
}
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
### ctx.compact()
|
|
675
|
+
|
|
676
|
+
Trigger compaction without awaiting completion. Use `onComplete` and `onError` for follow-up actions.
|
|
677
|
+
|
|
678
|
+
```typescript
|
|
679
|
+
ctx.compact({
|
|
680
|
+
customInstructions: "Focus on recent changes",
|
|
681
|
+
onComplete: (result) => {
|
|
682
|
+
ctx.ui.notify("Compaction completed", "info");
|
|
683
|
+
},
|
|
684
|
+
onError: (error) => {
|
|
685
|
+
ctx.ui.notify(`Compaction failed: ${error.message}`, "error");
|
|
686
|
+
},
|
|
687
|
+
});
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
### ctx.getSystemPrompt()
|
|
691
|
+
|
|
692
|
+
Returns the current effective system prompt. This includes any modifications made by `before_agent_start` handlers for the current turn.
|
|
693
|
+
|
|
694
|
+
```typescript
|
|
695
|
+
pi.on("before_agent_start", (event, ctx) => {
|
|
696
|
+
const prompt = ctx.getSystemPrompt();
|
|
697
|
+
console.log(`System prompt length: ${prompt.length}`);
|
|
698
|
+
});
|
|
699
|
+
```
|
|
700
|
+
|
|
701
|
+
## ExtensionCommandContext
|
|
702
|
+
|
|
703
|
+
Command handlers receive `ExtensionCommandContext`, which extends `ExtensionContext` with session control methods. These are only available in commands because they can deadlock if called from event handlers.
|
|
704
|
+
|
|
705
|
+
### ctx.waitForIdle()
|
|
706
|
+
|
|
707
|
+
Wait for the agent to finish streaming:
|
|
708
|
+
|
|
709
|
+
```typescript
|
|
710
|
+
pi.registerCommand("my-cmd", {
|
|
711
|
+
handler: async (args, ctx) => {
|
|
712
|
+
await ctx.waitForIdle();
|
|
713
|
+
// Agent is now idle, safe to modify session
|
|
714
|
+
},
|
|
715
|
+
});
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
### ctx.newSession(options?)
|
|
719
|
+
|
|
720
|
+
Create a new session:
|
|
721
|
+
|
|
722
|
+
```typescript
|
|
723
|
+
const result = await ctx.newSession({
|
|
724
|
+
parentSession: ctx.sessionManager.getSessionFile(),
|
|
725
|
+
setup: async (sm) => {
|
|
726
|
+
sm.appendMessage({
|
|
727
|
+
role: "user",
|
|
728
|
+
content: [{ type: "text", text: "Context from previous session..." }],
|
|
729
|
+
timestamp: Date.now(),
|
|
730
|
+
});
|
|
731
|
+
},
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
if (result.cancelled) {
|
|
735
|
+
// An extension cancelled the new session
|
|
736
|
+
}
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
### ctx.fork(entryId)
|
|
740
|
+
|
|
741
|
+
Fork from a specific entry, creating a new session file:
|
|
742
|
+
|
|
743
|
+
```typescript
|
|
744
|
+
const result = await ctx.fork("entry-id-123");
|
|
745
|
+
if (!result.cancelled) {
|
|
746
|
+
// Now in the forked session
|
|
747
|
+
}
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
### ctx.navigateTree(targetId, options?)
|
|
751
|
+
|
|
752
|
+
Navigate to a different point in the session tree:
|
|
753
|
+
|
|
754
|
+
```typescript
|
|
755
|
+
const result = await ctx.navigateTree("entry-id-456", {
|
|
756
|
+
summarize: true,
|
|
757
|
+
customInstructions: "Focus on error handling changes",
|
|
758
|
+
replaceInstructions: false, // true = replace default prompt entirely
|
|
759
|
+
label: "review-checkpoint",
|
|
760
|
+
});
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
Options:
|
|
764
|
+
- `summarize`: Whether to generate a summary of the abandoned branch
|
|
765
|
+
- `customInstructions`: Custom instructions for the summarizer
|
|
766
|
+
- `replaceInstructions`: If true, `customInstructions` replaces the default prompt instead of being appended
|
|
767
|
+
- `label`: Label to attach to the branch summary entry (or target entry if not summarizing)
|
|
768
|
+
|
|
769
|
+
## ExtensionAPI Methods
|
|
770
|
+
|
|
771
|
+
### pi.on(event, handler)
|
|
772
|
+
|
|
773
|
+
Subscribe to events. See [Events](#events) for event types and return values.
|
|
774
|
+
|
|
775
|
+
### pi.registerTool(definition)
|
|
776
|
+
|
|
777
|
+
Register a custom tool callable by the LLM. See [Custom Tools](#custom-tools) for full details.
|
|
778
|
+
|
|
779
|
+
```typescript
|
|
780
|
+
import { Type } from "@sinclair/typebox";
|
|
781
|
+
import { StringEnum } from "@mariozechner/pi-ai";
|
|
782
|
+
|
|
783
|
+
pi.registerTool({
|
|
784
|
+
name: "my_tool",
|
|
785
|
+
label: "My Tool",
|
|
786
|
+
description: "What this tool does",
|
|
787
|
+
parameters: Type.Object({
|
|
788
|
+
action: StringEnum(["list", "add"] as const),
|
|
789
|
+
text: Type.Optional(Type.String()),
|
|
790
|
+
}),
|
|
791
|
+
|
|
792
|
+
async execute(toolCallId, params, signal, onUpdate, ctx) {
|
|
793
|
+
// Stream progress
|
|
794
|
+
onUpdate?.({ content: [{ type: "text", text: "Working..." }] });
|
|
795
|
+
|
|
796
|
+
return {
|
|
797
|
+
content: [{ type: "text", text: "Done" }],
|
|
798
|
+
details: { result: "..." },
|
|
799
|
+
};
|
|
800
|
+
},
|
|
801
|
+
|
|
802
|
+
// Optional: Custom rendering
|
|
803
|
+
renderCall(args, theme) { ... },
|
|
804
|
+
renderResult(result, options, theme) { ... },
|
|
805
|
+
});
|
|
806
|
+
```
|
|
807
|
+
|
|
808
|
+
### pi.sendMessage(message, options?)
|
|
809
|
+
|
|
810
|
+
Inject a custom message into the session.
|
|
811
|
+
|
|
812
|
+
```typescript
|
|
813
|
+
pi.sendMessage({
|
|
814
|
+
customType: "my-extension",
|
|
815
|
+
content: "Message text",
|
|
816
|
+
display: true,
|
|
817
|
+
details: { ... },
|
|
818
|
+
}, {
|
|
819
|
+
triggerTurn: true,
|
|
820
|
+
deliverAs: "steer",
|
|
821
|
+
});
|
|
822
|
+
```
|
|
823
|
+
|
|
824
|
+
**Options:**
|
|
825
|
+
- `deliverAs` - Delivery mode:
|
|
826
|
+
- `"steer"` (default) - Interrupts streaming. Delivered after current tool finishes, remaining tools skipped.
|
|
827
|
+
- `"followUp"` - Waits for agent to finish. Delivered only when agent has no more tool calls.
|
|
828
|
+
- `"nextTurn"` - Queued for next user prompt. Does not interrupt or trigger anything.
|
|
829
|
+
- `triggerTurn: true` - If agent is idle, trigger an LLM response immediately. Only applies to `"steer"` and `"followUp"` modes (ignored for `"nextTurn"`).
|
|
830
|
+
|
|
831
|
+
### pi.sendUserMessage(content, options?)
|
|
832
|
+
|
|
833
|
+
Send a user message to the agent. Unlike `sendMessage()` which sends custom messages, this sends an actual user message that appears as if typed by the user. Always triggers a turn.
|
|
834
|
+
|
|
835
|
+
```typescript
|
|
836
|
+
// Simple text message
|
|
837
|
+
pi.sendUserMessage("What is 2+2?");
|
|
838
|
+
|
|
839
|
+
// With content array (text + images)
|
|
840
|
+
pi.sendUserMessage([
|
|
841
|
+
{ type: "text", text: "Describe this image:" },
|
|
842
|
+
{ type: "image", source: { type: "base64", mediaType: "image/png", data: "..." } },
|
|
843
|
+
]);
|
|
844
|
+
|
|
845
|
+
// During streaming - must specify delivery mode
|
|
846
|
+
pi.sendUserMessage("Focus on error handling", { deliverAs: "steer" });
|
|
847
|
+
pi.sendUserMessage("And then summarize", { deliverAs: "followUp" });
|
|
848
|
+
```
|
|
849
|
+
|
|
850
|
+
**Options:**
|
|
851
|
+
- `deliverAs` - Required when agent is streaming:
|
|
852
|
+
- `"steer"` - Interrupts after current tool, remaining tools skipped
|
|
853
|
+
- `"followUp"` - Waits for agent to finish all tools
|
|
854
|
+
|
|
855
|
+
When not streaming, the message is sent immediately and triggers a new turn. When streaming without `deliverAs`, throws an error.
|
|
856
|
+
|
|
857
|
+
See [send-user-message.ts](../examples/extensions/send-user-message.ts) for a complete example.
|
|
858
|
+
|
|
859
|
+
### pi.appendEntry(customType, data?)
|
|
860
|
+
|
|
861
|
+
Persist extension state (does NOT participate in LLM context).
|
|
862
|
+
|
|
863
|
+
```typescript
|
|
864
|
+
pi.appendEntry("my-state", { count: 42 });
|
|
865
|
+
|
|
866
|
+
// Restore on reload
|
|
867
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
868
|
+
for (const entry of ctx.sessionManager.getEntries()) {
|
|
869
|
+
if (entry.type === "custom" && entry.customType === "my-state") {
|
|
870
|
+
// Reconstruct from entry.data
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
### pi.setSessionName(name)
|
|
877
|
+
|
|
878
|
+
Set the session display name (shown in session selector instead of first message).
|
|
879
|
+
|
|
880
|
+
```typescript
|
|
881
|
+
pi.setSessionName("Refactor auth module");
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
### pi.getSessionName()
|
|
885
|
+
|
|
886
|
+
Get the current session name, if set.
|
|
887
|
+
|
|
888
|
+
```typescript
|
|
889
|
+
const name = pi.getSessionName();
|
|
890
|
+
if (name) {
|
|
891
|
+
console.log(`Session: ${name}`);
|
|
892
|
+
}
|
|
893
|
+
```
|
|
894
|
+
|
|
895
|
+
### pi.setLabel(entryId, label)
|
|
896
|
+
|
|
897
|
+
Set or clear a label on an entry. Labels are user-defined markers for bookmarking and navigation (shown in `/tree` selector).
|
|
898
|
+
|
|
899
|
+
```typescript
|
|
900
|
+
// Set a label
|
|
901
|
+
pi.setLabel(entryId, "checkpoint-before-refactor");
|
|
902
|
+
|
|
903
|
+
// Clear a label
|
|
904
|
+
pi.setLabel(entryId, undefined);
|
|
905
|
+
|
|
906
|
+
// Read labels via sessionManager
|
|
907
|
+
const label = ctx.sessionManager.getLabel(entryId);
|
|
908
|
+
```
|
|
909
|
+
|
|
910
|
+
Labels persist in the session and survive restarts. Use them to mark important points (turns, checkpoints) in the conversation tree.
|
|
911
|
+
|
|
912
|
+
### pi.registerCommand(name, options)
|
|
913
|
+
|
|
914
|
+
Register a command.
|
|
915
|
+
|
|
916
|
+
```typescript
|
|
917
|
+
pi.registerCommand("stats", {
|
|
918
|
+
description: "Show session statistics",
|
|
919
|
+
handler: async (args, ctx) => {
|
|
920
|
+
const count = ctx.sessionManager.getEntries().length;
|
|
921
|
+
ctx.ui.notify(`${count} entries`, "info");
|
|
922
|
+
}
|
|
923
|
+
});
|
|
924
|
+
```
|
|
925
|
+
|
|
926
|
+
Optional: add argument auto-completion for `/command ...`:
|
|
927
|
+
|
|
928
|
+
```typescript
|
|
929
|
+
import type { AutocompleteItem } from "@mariozechner/pi-tui";
|
|
930
|
+
|
|
931
|
+
pi.registerCommand("deploy", {
|
|
932
|
+
description: "Deploy to an environment",
|
|
933
|
+
getArgumentCompletions: (prefix: string): AutocompleteItem[] | null => {
|
|
934
|
+
const envs = ["dev", "staging", "prod"];
|
|
935
|
+
const items = envs.map((e) => ({ value: e, label: e }));
|
|
936
|
+
const filtered = items.filter((i) => i.value.startsWith(prefix));
|
|
937
|
+
return filtered.length > 0 ? filtered : null;
|
|
938
|
+
},
|
|
939
|
+
handler: async (args, ctx) => {
|
|
940
|
+
ctx.ui.notify(`Deploying: ${args}`, "info");
|
|
941
|
+
},
|
|
942
|
+
});
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
### pi.getCommands()
|
|
946
|
+
|
|
947
|
+
Get the slash commands available for invocation via `prompt` in the current session. Includes extension commands, prompt templates, and skill commands.
|
|
948
|
+
The list matches the RPC `get_commands` ordering: extensions first, then templates, then skills.
|
|
949
|
+
|
|
950
|
+
```typescript
|
|
951
|
+
const commands = pi.getCommands();
|
|
952
|
+
const bySource = commands.filter((command) => command.source === "extension");
|
|
953
|
+
```
|
|
954
|
+
|
|
955
|
+
Each entry has this shape:
|
|
956
|
+
|
|
957
|
+
```typescript
|
|
958
|
+
{
|
|
959
|
+
name: string; // Command name without the leading slash
|
|
960
|
+
description?: string;
|
|
961
|
+
source: "extension" | "prompt" | "skill";
|
|
962
|
+
location?: "user" | "project" | "path"; // For templates and skills
|
|
963
|
+
path?: string; // Files backing templates, skills, and extensions
|
|
964
|
+
}
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
Built-in interactive commands (like `/model` and `/settings`) are not included here. They are handled only in interactive
|
|
968
|
+
mode and would not execute if sent via `prompt`.
|
|
969
|
+
|
|
970
|
+
### pi.registerMessageRenderer(customType, renderer)
|
|
971
|
+
|
|
972
|
+
Register a custom TUI renderer for messages with your `customType`. See [Custom UI](#custom-ui).
|
|
973
|
+
|
|
974
|
+
### pi.registerShortcut(shortcut, options)
|
|
975
|
+
|
|
976
|
+
Register a keyboard shortcut. See [keybindings.md](keybindings.md) for the shortcut format and built-in keybindings.
|
|
977
|
+
|
|
978
|
+
```typescript
|
|
979
|
+
pi.registerShortcut("ctrl+shift+p", {
|
|
980
|
+
description: "Toggle plan mode",
|
|
981
|
+
handler: async (ctx) => {
|
|
982
|
+
ctx.ui.notify("Toggled!");
|
|
983
|
+
},
|
|
984
|
+
});
|
|
985
|
+
```
|
|
986
|
+
|
|
987
|
+
### pi.registerFlag(name, options)
|
|
988
|
+
|
|
989
|
+
Register a CLI flag.
|
|
990
|
+
|
|
991
|
+
```typescript
|
|
992
|
+
pi.registerFlag("plan", {
|
|
993
|
+
description: "Start in plan mode",
|
|
994
|
+
type: "boolean",
|
|
995
|
+
default: false,
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
// Check value
|
|
999
|
+
if (pi.getFlag("--plan")) {
|
|
1000
|
+
// Plan mode enabled
|
|
1001
|
+
}
|
|
1002
|
+
```
|
|
1003
|
+
|
|
1004
|
+
### pi.exec(command, args, options?)
|
|
1005
|
+
|
|
1006
|
+
Execute a shell command.
|
|
1007
|
+
|
|
1008
|
+
```typescript
|
|
1009
|
+
const result = await pi.exec("git", ["status"], { signal, timeout: 5000 });
|
|
1010
|
+
// result.stdout, result.stderr, result.code, result.killed
|
|
1011
|
+
```
|
|
1012
|
+
|
|
1013
|
+
### pi.getActiveTools() / pi.getAllTools() / pi.setActiveTools(names)
|
|
1014
|
+
|
|
1015
|
+
Manage active tools.
|
|
1016
|
+
|
|
1017
|
+
```typescript
|
|
1018
|
+
const active = pi.getActiveTools(); // ["read", "bash", "edit", "write"]
|
|
1019
|
+
const all = pi.getAllTools(); // [{ name: "read", description: "Read file contents..." }, ...]
|
|
1020
|
+
const names = all.map(t => t.name); // Just names if needed
|
|
1021
|
+
pi.setActiveTools(["read", "bash"]); // Switch to read-only
|
|
1022
|
+
```
|
|
1023
|
+
|
|
1024
|
+
### pi.setModel(model)
|
|
1025
|
+
|
|
1026
|
+
Set the current model. Returns `false` if no API key is available for the model. See [models.md](models.md) for configuring custom models.
|
|
1027
|
+
|
|
1028
|
+
```typescript
|
|
1029
|
+
const model = ctx.modelRegistry.find("anthropic", "claude-sonnet-4-5");
|
|
1030
|
+
if (model) {
|
|
1031
|
+
const success = await pi.setModel(model);
|
|
1032
|
+
if (!success) {
|
|
1033
|
+
ctx.ui.notify("No API key for this model", "error");
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
```
|
|
1037
|
+
|
|
1038
|
+
### pi.getThinkingLevel() / pi.setThinkingLevel(level)
|
|
1039
|
+
|
|
1040
|
+
Get or set the thinking level. Level is clamped to model capabilities (non-reasoning models always use "off").
|
|
1041
|
+
|
|
1042
|
+
```typescript
|
|
1043
|
+
const current = pi.getThinkingLevel(); // "off" | "minimal" | "low" | "medium" | "high" | "xhigh"
|
|
1044
|
+
pi.setThinkingLevel("high");
|
|
1045
|
+
```
|
|
1046
|
+
|
|
1047
|
+
### pi.events
|
|
1048
|
+
|
|
1049
|
+
Shared event bus for communication between extensions:
|
|
1050
|
+
|
|
1051
|
+
```typescript
|
|
1052
|
+
pi.events.on("my:event", (data) => { ... });
|
|
1053
|
+
pi.events.emit("my:event", { ... });
|
|
1054
|
+
```
|
|
1055
|
+
|
|
1056
|
+
### pi.registerProvider(name, config)
|
|
1057
|
+
|
|
1058
|
+
Register or override a model provider dynamically. Useful for proxies, custom endpoints, or team-wide model configurations.
|
|
1059
|
+
|
|
1060
|
+
```typescript
|
|
1061
|
+
// Register a new provider with custom models
|
|
1062
|
+
pi.registerProvider("my-proxy", {
|
|
1063
|
+
baseUrl: "https://proxy.example.com",
|
|
1064
|
+
apiKey: "PROXY_API_KEY", // env var name or literal
|
|
1065
|
+
api: "anthropic-messages",
|
|
1066
|
+
models: [
|
|
1067
|
+
{
|
|
1068
|
+
id: "claude-sonnet-4-20250514",
|
|
1069
|
+
name: "Claude 4 Sonnet (proxy)",
|
|
1070
|
+
reasoning: false,
|
|
1071
|
+
input: ["text", "image"],
|
|
1072
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
1073
|
+
contextWindow: 200000,
|
|
1074
|
+
maxTokens: 16384
|
|
1075
|
+
}
|
|
1076
|
+
]
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
// Override baseUrl for an existing provider (keeps all models)
|
|
1080
|
+
pi.registerProvider("anthropic", {
|
|
1081
|
+
baseUrl: "https://proxy.example.com"
|
|
1082
|
+
});
|
|
1083
|
+
|
|
1084
|
+
// Register provider with OAuth support for /login
|
|
1085
|
+
pi.registerProvider("corporate-ai", {
|
|
1086
|
+
baseUrl: "https://ai.corp.com",
|
|
1087
|
+
api: "openai-responses",
|
|
1088
|
+
models: [...],
|
|
1089
|
+
oauth: {
|
|
1090
|
+
name: "Corporate AI (SSO)",
|
|
1091
|
+
async login(callbacks) {
|
|
1092
|
+
// Custom OAuth flow
|
|
1093
|
+
callbacks.onAuth({ url: "https://sso.corp.com/..." });
|
|
1094
|
+
const code = await callbacks.onPrompt({ message: "Enter code:" });
|
|
1095
|
+
return { refresh: code, access: code, expires: Date.now() + 3600000 };
|
|
1096
|
+
},
|
|
1097
|
+
async refreshToken(credentials) {
|
|
1098
|
+
// Refresh logic
|
|
1099
|
+
return credentials;
|
|
1100
|
+
},
|
|
1101
|
+
getApiKey(credentials) {
|
|
1102
|
+
return credentials.access;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
});
|
|
1106
|
+
```
|
|
1107
|
+
|
|
1108
|
+
**Config options:**
|
|
1109
|
+
- `baseUrl` - API endpoint URL. Required when defining models.
|
|
1110
|
+
- `apiKey` - API key or environment variable name. Required when defining models (unless `oauth` provided).
|
|
1111
|
+
- `api` - API type: `"anthropic-messages"`, `"openai-completions"`, `"openai-responses"`, etc.
|
|
1112
|
+
- `headers` - Custom headers to include in requests.
|
|
1113
|
+
- `authHeader` - If true, adds `Authorization: Bearer` header automatically.
|
|
1114
|
+
- `models` - Array of model definitions. If provided, replaces all existing models for this provider.
|
|
1115
|
+
- `oauth` - OAuth provider config for `/login` support. When provided, the provider appears in the login menu.
|
|
1116
|
+
- `streamSimple` - Custom streaming implementation for non-standard APIs.
|
|
1117
|
+
|
|
1118
|
+
See [custom-provider.md](custom-provider.md) for advanced topics: custom streaming APIs, OAuth details, model definition reference.
|
|
1119
|
+
|
|
1120
|
+
## State Management
|
|
1121
|
+
|
|
1122
|
+
Extensions with state should store it in tool result `details` for proper branching support:
|
|
1123
|
+
|
|
1124
|
+
```typescript
|
|
1125
|
+
export default function (pi: ExtensionAPI) {
|
|
1126
|
+
let items: string[] = [];
|
|
1127
|
+
|
|
1128
|
+
// Reconstruct state from session
|
|
1129
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
1130
|
+
items = [];
|
|
1131
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
1132
|
+
if (entry.type === "message" && entry.message.role === "toolResult") {
|
|
1133
|
+
if (entry.message.toolName === "my_tool") {
|
|
1134
|
+
items = entry.message.details?.items ?? [];
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
});
|
|
1139
|
+
|
|
1140
|
+
pi.registerTool({
|
|
1141
|
+
name: "my_tool",
|
|
1142
|
+
// ...
|
|
1143
|
+
async execute(toolCallId, params, signal, onUpdate, ctx) {
|
|
1144
|
+
items.push("new item");
|
|
1145
|
+
return {
|
|
1146
|
+
content: [{ type: "text", text: "Added" }],
|
|
1147
|
+
details: { items: [...items] }, // Store for reconstruction
|
|
1148
|
+
};
|
|
1149
|
+
},
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
```
|
|
1153
|
+
|
|
1154
|
+
## Custom Tools
|
|
1155
|
+
|
|
1156
|
+
Register tools the LLM can call via `pi.registerTool()`. Tools appear in the system prompt and can have custom rendering.
|
|
1157
|
+
|
|
1158
|
+
Note: Some models are idiots and include the @ prefix in tool path arguments. Built-in tools strip a leading @ before resolving paths. If your custom tool accepts a path, normalize a leading @ as well.
|
|
1159
|
+
|
|
1160
|
+
### Tool Definition
|
|
1161
|
+
|
|
1162
|
+
```typescript
|
|
1163
|
+
import { Type } from "@sinclair/typebox";
|
|
1164
|
+
import { StringEnum } from "@mariozechner/pi-ai";
|
|
1165
|
+
import { Text } from "@mariozechner/pi-tui";
|
|
1166
|
+
|
|
1167
|
+
pi.registerTool({
|
|
1168
|
+
name: "my_tool",
|
|
1169
|
+
label: "My Tool",
|
|
1170
|
+
description: "What this tool does (shown to LLM)",
|
|
1171
|
+
parameters: Type.Object({
|
|
1172
|
+
action: StringEnum(["list", "add"] as const), // Use StringEnum for Google compatibility
|
|
1173
|
+
text: Type.Optional(Type.String()),
|
|
1174
|
+
}),
|
|
1175
|
+
|
|
1176
|
+
async execute(toolCallId, params, signal, onUpdate, ctx) {
|
|
1177
|
+
// Check for cancellation
|
|
1178
|
+
if (signal?.aborted) {
|
|
1179
|
+
return { content: [{ type: "text", text: "Cancelled" }] };
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
// Stream progress updates
|
|
1183
|
+
onUpdate?.({
|
|
1184
|
+
content: [{ type: "text", text: "Working..." }],
|
|
1185
|
+
details: { progress: 50 },
|
|
1186
|
+
});
|
|
1187
|
+
|
|
1188
|
+
// Run commands via pi.exec (captured from extension closure)
|
|
1189
|
+
const result = await pi.exec("some-command", [], { signal });
|
|
1190
|
+
|
|
1191
|
+
// Return result
|
|
1192
|
+
return {
|
|
1193
|
+
content: [{ type: "text", text: "Done" }], // Sent to LLM
|
|
1194
|
+
details: { data: result }, // For rendering & state
|
|
1195
|
+
};
|
|
1196
|
+
},
|
|
1197
|
+
|
|
1198
|
+
// Optional: Custom rendering
|
|
1199
|
+
renderCall(args, theme) { ... },
|
|
1200
|
+
renderResult(result, options, theme) { ... },
|
|
1201
|
+
});
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1204
|
+
**Important:** Use `StringEnum` from `@mariozechner/pi-ai` for string enums. `Type.Union`/`Type.Literal` doesn't work with Google's API.
|
|
1205
|
+
|
|
1206
|
+
### Overriding Built-in Tools
|
|
1207
|
+
|
|
1208
|
+
Extensions can override built-in tools (`read`, `bash`, `edit`, `write`, `grep`, `find`, `ls`) by registering a tool with the same name. Interactive mode displays a warning when this happens.
|
|
1209
|
+
|
|
1210
|
+
```bash
|
|
1211
|
+
# Extension's read tool replaces built-in read
|
|
1212
|
+
pi -e ./tool-override.ts
|
|
1213
|
+
```
|
|
1214
|
+
|
|
1215
|
+
Alternatively, use `--no-tools` to start without any built-in tools:
|
|
1216
|
+
```bash
|
|
1217
|
+
# No built-in tools, only extension tools
|
|
1218
|
+
pi --no-tools -e ./my-extension.ts
|
|
1219
|
+
```
|
|
1220
|
+
|
|
1221
|
+
See [examples/extensions/tool-override.ts](../examples/extensions/tool-override.ts) for a complete example that overrides `read` with logging and access control.
|
|
1222
|
+
|
|
1223
|
+
**Rendering:** If your override doesn't provide custom `renderCall`/`renderResult` functions, the built-in renderer is used automatically (syntax highlighting, diffs, etc.). This lets you wrap built-in tools for logging or access control without reimplementing the UI.
|
|
1224
|
+
|
|
1225
|
+
**Your implementation must match the exact result shape**, including the `details` type. The UI and session logic depend on these shapes for rendering and state tracking.
|
|
1226
|
+
|
|
1227
|
+
Built-in tool implementations:
|
|
1228
|
+
- [read.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/tools/read.ts) - `ReadToolDetails`
|
|
1229
|
+
- [bash.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/tools/bash.ts) - `BashToolDetails`
|
|
1230
|
+
- [edit.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/tools/edit.ts)
|
|
1231
|
+
- [write.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/tools/write.ts)
|
|
1232
|
+
- [grep.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/tools/grep.ts) - `GrepToolDetails`
|
|
1233
|
+
- [find.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/tools/find.ts) - `FindToolDetails`
|
|
1234
|
+
- [ls.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/tools/ls.ts) - `LsToolDetails`
|
|
1235
|
+
|
|
1236
|
+
### Remote Execution
|
|
1237
|
+
|
|
1238
|
+
Built-in tools support pluggable operations for delegating to remote systems (SSH, containers, etc.):
|
|
1239
|
+
|
|
1240
|
+
```typescript
|
|
1241
|
+
import { createReadTool, createBashTool, type ReadOperations } from "@mariozechner/pi-coding-agent";
|
|
1242
|
+
|
|
1243
|
+
// Create tool with custom operations
|
|
1244
|
+
const remoteRead = createReadTool(cwd, {
|
|
1245
|
+
operations: {
|
|
1246
|
+
readFile: (path) => sshExec(remote, `cat ${path}`),
|
|
1247
|
+
access: (path) => sshExec(remote, `test -r ${path}`).then(() => {}),
|
|
1248
|
+
}
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
// Register, checking flag at execution time
|
|
1252
|
+
pi.registerTool({
|
|
1253
|
+
...remoteRead,
|
|
1254
|
+
async execute(id, params, signal, onUpdate, _ctx) {
|
|
1255
|
+
const ssh = getSshConfig();
|
|
1256
|
+
if (ssh) {
|
|
1257
|
+
const tool = createReadTool(cwd, { operations: createRemoteOps(ssh) });
|
|
1258
|
+
return tool.execute(id, params, signal, onUpdate);
|
|
1259
|
+
}
|
|
1260
|
+
return localRead.execute(id, params, signal, onUpdate);
|
|
1261
|
+
},
|
|
1262
|
+
});
|
|
1263
|
+
```
|
|
1264
|
+
|
|
1265
|
+
**Operations interfaces:** `ReadOperations`, `WriteOperations`, `EditOperations`, `BashOperations`, `LsOperations`, `GrepOperations`, `FindOperations`
|
|
1266
|
+
|
|
1267
|
+
The bash tool also supports a spawn hook to adjust the command, cwd, or env before execution:
|
|
1268
|
+
|
|
1269
|
+
```typescript
|
|
1270
|
+
import { createBashTool } from "@mariozechner/pi-coding-agent";
|
|
1271
|
+
|
|
1272
|
+
const bashTool = createBashTool(cwd, {
|
|
1273
|
+
spawnHook: ({ command, cwd, env }) => ({
|
|
1274
|
+
command: `source ~/.profile\n${command}`,
|
|
1275
|
+
cwd: `/mnt/sandbox${cwd}`,
|
|
1276
|
+
env: { ...env, CI: "1" },
|
|
1277
|
+
}),
|
|
1278
|
+
});
|
|
1279
|
+
```
|
|
1280
|
+
|
|
1281
|
+
See [examples/extensions/ssh.ts](../examples/extensions/ssh.ts) for a complete SSH example with `--ssh` flag.
|
|
1282
|
+
|
|
1283
|
+
### Output Truncation
|
|
1284
|
+
|
|
1285
|
+
**Tools MUST truncate their output** to avoid overwhelming the LLM context. Large outputs can cause:
|
|
1286
|
+
- Context overflow errors (prompt too long)
|
|
1287
|
+
- Compaction failures
|
|
1288
|
+
- Degraded model performance
|
|
1289
|
+
|
|
1290
|
+
The built-in limit is **50KB** (~10k tokens) and **2000 lines**, whichever is hit first. Use the exported truncation utilities:
|
|
1291
|
+
|
|
1292
|
+
```typescript
|
|
1293
|
+
import {
|
|
1294
|
+
truncateHead, // Keep first N lines/bytes (good for file reads, search results)
|
|
1295
|
+
truncateTail, // Keep last N lines/bytes (good for logs, command output)
|
|
1296
|
+
truncateLine, // Truncate a single line to maxBytes with ellipsis
|
|
1297
|
+
formatSize, // Human-readable size (e.g., "50KB", "1.5MB")
|
|
1298
|
+
DEFAULT_MAX_BYTES, // 50KB
|
|
1299
|
+
DEFAULT_MAX_LINES, // 2000
|
|
1300
|
+
} from "@mariozechner/pi-coding-agent";
|
|
1301
|
+
|
|
1302
|
+
async execute(toolCallId, params, signal, onUpdate, ctx) {
|
|
1303
|
+
const output = await runCommand();
|
|
1304
|
+
|
|
1305
|
+
// Apply truncation
|
|
1306
|
+
const truncation = truncateHead(output, {
|
|
1307
|
+
maxLines: DEFAULT_MAX_LINES,
|
|
1308
|
+
maxBytes: DEFAULT_MAX_BYTES,
|
|
1309
|
+
});
|
|
1310
|
+
|
|
1311
|
+
let result = truncation.content;
|
|
1312
|
+
|
|
1313
|
+
if (truncation.truncated) {
|
|
1314
|
+
// Write full output to temp file
|
|
1315
|
+
const tempFile = writeTempFile(output);
|
|
1316
|
+
|
|
1317
|
+
// Inform the LLM where to find complete output
|
|
1318
|
+
result += `\n\n[Output truncated: ${truncation.outputLines} of ${truncation.totalLines} lines`;
|
|
1319
|
+
result += ` (${formatSize(truncation.outputBytes)} of ${formatSize(truncation.totalBytes)}).`;
|
|
1320
|
+
result += ` Full output saved to: ${tempFile}]`;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
return { content: [{ type: "text", text: result }] };
|
|
1324
|
+
}
|
|
1325
|
+
```
|
|
1326
|
+
|
|
1327
|
+
**Key points:**
|
|
1328
|
+
- Use `truncateHead` for content where the beginning matters (search results, file reads)
|
|
1329
|
+
- Use `truncateTail` for content where the end matters (logs, command output)
|
|
1330
|
+
- Always inform the LLM when output is truncated and where to find the full version
|
|
1331
|
+
- Document the truncation limits in your tool's description
|
|
1332
|
+
|
|
1333
|
+
See [examples/extensions/truncated-tool.ts](../examples/extensions/truncated-tool.ts) for a complete example wrapping `rg` (ripgrep) with proper truncation.
|
|
1334
|
+
|
|
1335
|
+
### Multiple Tools
|
|
1336
|
+
|
|
1337
|
+
One extension can register multiple tools with shared state:
|
|
1338
|
+
|
|
1339
|
+
```typescript
|
|
1340
|
+
export default function (pi: ExtensionAPI) {
|
|
1341
|
+
let connection = null;
|
|
1342
|
+
|
|
1343
|
+
pi.registerTool({ name: "db_connect", ... });
|
|
1344
|
+
pi.registerTool({ name: "db_query", ... });
|
|
1345
|
+
pi.registerTool({ name: "db_close", ... });
|
|
1346
|
+
|
|
1347
|
+
pi.on("session_shutdown", async () => {
|
|
1348
|
+
connection?.close();
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
```
|
|
1352
|
+
|
|
1353
|
+
### Custom Rendering
|
|
1354
|
+
|
|
1355
|
+
Tools can provide `renderCall` and `renderResult` for custom TUI display. See [tui.md](tui.md) for the full component API and [tool-execution.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/modes/interactive/components/tool-execution.ts) for how built-in tools render.
|
|
1356
|
+
|
|
1357
|
+
Tool output is wrapped in a `Box` that handles padding and background. Your render methods return `Component` instances (typically `Text`).
|
|
1358
|
+
|
|
1359
|
+
#### renderCall
|
|
1360
|
+
|
|
1361
|
+
Renders the tool call (before/during execution):
|
|
1362
|
+
|
|
1363
|
+
```typescript
|
|
1364
|
+
import { Text } from "@mariozechner/pi-tui";
|
|
1365
|
+
|
|
1366
|
+
renderCall(args, theme) {
|
|
1367
|
+
let text = theme.fg("toolTitle", theme.bold("my_tool "));
|
|
1368
|
+
text += theme.fg("muted", args.action);
|
|
1369
|
+
if (args.text) {
|
|
1370
|
+
text += " " + theme.fg("dim", `"${args.text}"`);
|
|
1371
|
+
}
|
|
1372
|
+
return new Text(text, 0, 0); // 0,0 padding - Box handles it
|
|
1373
|
+
}
|
|
1374
|
+
```
|
|
1375
|
+
|
|
1376
|
+
#### renderResult
|
|
1377
|
+
|
|
1378
|
+
Renders the tool result:
|
|
1379
|
+
|
|
1380
|
+
```typescript
|
|
1381
|
+
renderResult(result, { expanded, isPartial }, theme) {
|
|
1382
|
+
// Handle streaming
|
|
1383
|
+
if (isPartial) {
|
|
1384
|
+
return new Text(theme.fg("warning", "Processing..."), 0, 0);
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
// Handle errors
|
|
1388
|
+
if (result.details?.error) {
|
|
1389
|
+
return new Text(theme.fg("error", `Error: ${result.details.error}`), 0, 0);
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
// Normal result - support expanded view (Ctrl+O)
|
|
1393
|
+
let text = theme.fg("success", "✓ Done");
|
|
1394
|
+
if (expanded && result.details?.items) {
|
|
1395
|
+
for (const item of result.details.items) {
|
|
1396
|
+
text += "\n " + theme.fg("dim", item);
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
return new Text(text, 0, 0);
|
|
1400
|
+
}
|
|
1401
|
+
```
|
|
1402
|
+
|
|
1403
|
+
#### Keybinding Hints
|
|
1404
|
+
|
|
1405
|
+
Use `keyHint()` to display keybinding hints that respect user's keybinding configuration:
|
|
1406
|
+
|
|
1407
|
+
```typescript
|
|
1408
|
+
import { keyHint } from "@mariozechner/pi-coding-agent";
|
|
1409
|
+
|
|
1410
|
+
renderResult(result, { expanded }, theme) {
|
|
1411
|
+
let text = theme.fg("success", "✓ Done");
|
|
1412
|
+
if (!expanded) {
|
|
1413
|
+
text += ` (${keyHint("expandTools", "to expand")})`;
|
|
1414
|
+
}
|
|
1415
|
+
return new Text(text, 0, 0);
|
|
1416
|
+
}
|
|
1417
|
+
```
|
|
1418
|
+
|
|
1419
|
+
Available functions:
|
|
1420
|
+
- `keyHint(action, description)` - Editor actions (e.g., `"expandTools"`, `"selectConfirm"`)
|
|
1421
|
+
- `appKeyHint(keybindings, action, description)` - App actions (requires `KeybindingsManager`)
|
|
1422
|
+
- `editorKey(action)` - Get raw key string for editor action
|
|
1423
|
+
- `rawKeyHint(key, description)` - Format a raw key string
|
|
1424
|
+
|
|
1425
|
+
#### Best Practices
|
|
1426
|
+
|
|
1427
|
+
- Use `Text` with padding `(0, 0)` - the Box handles padding
|
|
1428
|
+
- Use `\n` for multi-line content
|
|
1429
|
+
- Handle `isPartial` for streaming progress
|
|
1430
|
+
- Support `expanded` for detail on demand
|
|
1431
|
+
- Keep default view compact
|
|
1432
|
+
|
|
1433
|
+
#### Fallback
|
|
1434
|
+
|
|
1435
|
+
If `renderCall`/`renderResult` is not defined or throws:
|
|
1436
|
+
- `renderCall`: Shows tool name
|
|
1437
|
+
- `renderResult`: Shows raw text from `content`
|
|
1438
|
+
|
|
1439
|
+
## Custom UI
|
|
1440
|
+
|
|
1441
|
+
Extensions can interact with users via `ctx.ui` methods and customize how messages/tools render.
|
|
1442
|
+
|
|
1443
|
+
**For custom components, see [tui.md](tui.md)** which has copy-paste patterns for:
|
|
1444
|
+
- Selection dialogs (SelectList)
|
|
1445
|
+
- Async operations with cancel (BorderedLoader)
|
|
1446
|
+
- Settings toggles (SettingsList)
|
|
1447
|
+
- Status indicators (setStatus)
|
|
1448
|
+
- Working message during streaming (setWorkingMessage)
|
|
1449
|
+
- Widgets above/below editor (setWidget)
|
|
1450
|
+
- Custom footers (setFooter)
|
|
1451
|
+
|
|
1452
|
+
### Dialogs
|
|
1453
|
+
|
|
1454
|
+
```typescript
|
|
1455
|
+
// Select from options
|
|
1456
|
+
const choice = await ctx.ui.select("Pick one:", ["A", "B", "C"]);
|
|
1457
|
+
|
|
1458
|
+
// Confirm dialog
|
|
1459
|
+
const ok = await ctx.ui.confirm("Delete?", "This cannot be undone");
|
|
1460
|
+
|
|
1461
|
+
// Text input
|
|
1462
|
+
const name = await ctx.ui.input("Name:", "placeholder");
|
|
1463
|
+
|
|
1464
|
+
// Multi-line editor
|
|
1465
|
+
const text = await ctx.ui.editor("Edit:", "prefilled text");
|
|
1466
|
+
|
|
1467
|
+
// Notification (non-blocking)
|
|
1468
|
+
ctx.ui.notify("Done!", "info"); // "info" | "warning" | "error"
|
|
1469
|
+
```
|
|
1470
|
+
|
|
1471
|
+
#### Timed Dialogs with Countdown
|
|
1472
|
+
|
|
1473
|
+
Dialogs support a `timeout` option that auto-dismisses with a live countdown display:
|
|
1474
|
+
|
|
1475
|
+
```typescript
|
|
1476
|
+
// Dialog shows "Title (5s)" → "Title (4s)" → ... → auto-dismisses at 0
|
|
1477
|
+
const confirmed = await ctx.ui.confirm(
|
|
1478
|
+
"Timed Confirmation",
|
|
1479
|
+
"This dialog will auto-cancel in 5 seconds. Confirm?",
|
|
1480
|
+
{ timeout: 5000 }
|
|
1481
|
+
);
|
|
1482
|
+
|
|
1483
|
+
if (confirmed) {
|
|
1484
|
+
// User confirmed
|
|
1485
|
+
} else {
|
|
1486
|
+
// User cancelled or timed out
|
|
1487
|
+
}
|
|
1488
|
+
```
|
|
1489
|
+
|
|
1490
|
+
**Return values on timeout:**
|
|
1491
|
+
- `select()` returns `undefined`
|
|
1492
|
+
- `confirm()` returns `false`
|
|
1493
|
+
- `input()` returns `undefined`
|
|
1494
|
+
|
|
1495
|
+
#### Manual Dismissal with AbortSignal
|
|
1496
|
+
|
|
1497
|
+
For more control (e.g., to distinguish timeout from user cancel), use `AbortSignal`:
|
|
1498
|
+
|
|
1499
|
+
```typescript
|
|
1500
|
+
const controller = new AbortController();
|
|
1501
|
+
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
1502
|
+
|
|
1503
|
+
const confirmed = await ctx.ui.confirm(
|
|
1504
|
+
"Timed Confirmation",
|
|
1505
|
+
"This dialog will auto-cancel in 5 seconds. Confirm?",
|
|
1506
|
+
{ signal: controller.signal }
|
|
1507
|
+
);
|
|
1508
|
+
|
|
1509
|
+
clearTimeout(timeoutId);
|
|
1510
|
+
|
|
1511
|
+
if (confirmed) {
|
|
1512
|
+
// User confirmed
|
|
1513
|
+
} else if (controller.signal.aborted) {
|
|
1514
|
+
// Dialog timed out
|
|
1515
|
+
} else {
|
|
1516
|
+
// User cancelled (pressed Escape or selected "No")
|
|
1517
|
+
}
|
|
1518
|
+
```
|
|
1519
|
+
|
|
1520
|
+
See [examples/extensions/timed-confirm.ts](../examples/extensions/timed-confirm.ts) for complete examples.
|
|
1521
|
+
|
|
1522
|
+
### Widgets, Status, and Footer
|
|
1523
|
+
|
|
1524
|
+
```typescript
|
|
1525
|
+
// Status in footer (persistent until cleared)
|
|
1526
|
+
ctx.ui.setStatus("my-ext", "Processing...");
|
|
1527
|
+
ctx.ui.setStatus("my-ext", undefined); // Clear
|
|
1528
|
+
|
|
1529
|
+
// Working message (shown during streaming)
|
|
1530
|
+
ctx.ui.setWorkingMessage("Thinking deeply...");
|
|
1531
|
+
ctx.ui.setWorkingMessage(); // Restore default
|
|
1532
|
+
|
|
1533
|
+
// Widget above editor (default)
|
|
1534
|
+
ctx.ui.setWidget("my-widget", ["Line 1", "Line 2"]);
|
|
1535
|
+
// Widget below editor
|
|
1536
|
+
ctx.ui.setWidget("my-widget", ["Line 1", "Line 2"], { placement: "belowEditor" });
|
|
1537
|
+
ctx.ui.setWidget("my-widget", (tui, theme) => new Text(theme.fg("accent", "Custom"), 0, 0));
|
|
1538
|
+
ctx.ui.setWidget("my-widget", undefined); // Clear
|
|
1539
|
+
|
|
1540
|
+
// Custom footer (replaces built-in footer entirely)
|
|
1541
|
+
ctx.ui.setFooter((tui, theme) => ({
|
|
1542
|
+
render(width) { return [theme.fg("dim", "Custom footer")]; },
|
|
1543
|
+
invalidate() {},
|
|
1544
|
+
}));
|
|
1545
|
+
ctx.ui.setFooter(undefined); // Restore built-in footer
|
|
1546
|
+
|
|
1547
|
+
// Terminal title
|
|
1548
|
+
ctx.ui.setTitle("pi - my-project");
|
|
1549
|
+
|
|
1550
|
+
// Editor text
|
|
1551
|
+
ctx.ui.setEditorText("Prefill text");
|
|
1552
|
+
const current = ctx.ui.getEditorText();
|
|
1553
|
+
|
|
1554
|
+
// Tool output expansion
|
|
1555
|
+
const wasExpanded = ctx.ui.getToolsExpanded();
|
|
1556
|
+
ctx.ui.setToolsExpanded(true);
|
|
1557
|
+
ctx.ui.setToolsExpanded(wasExpanded);
|
|
1558
|
+
|
|
1559
|
+
// Custom editor (vim mode, emacs mode, etc.)
|
|
1560
|
+
ctx.ui.setEditorComponent((tui, theme, keybindings) => new VimEditor(tui, theme, keybindings));
|
|
1561
|
+
ctx.ui.setEditorComponent(undefined); // Restore default editor
|
|
1562
|
+
|
|
1563
|
+
// Theme management (see themes.md for creating themes)
|
|
1564
|
+
const themes = ctx.ui.getAllThemes(); // [{ name: "dark", path: "/..." | undefined }, ...]
|
|
1565
|
+
const lightTheme = ctx.ui.getTheme("light"); // Load without switching
|
|
1566
|
+
const result = ctx.ui.setTheme("light"); // Switch by name
|
|
1567
|
+
if (!result.success) {
|
|
1568
|
+
ctx.ui.notify(`Failed: ${result.error}`, "error");
|
|
1569
|
+
}
|
|
1570
|
+
ctx.ui.setTheme(lightTheme!); // Or switch by Theme object
|
|
1571
|
+
ctx.ui.theme.fg("accent", "styled text"); // Access current theme
|
|
1572
|
+
```
|
|
1573
|
+
|
|
1574
|
+
### Custom Components
|
|
1575
|
+
|
|
1576
|
+
For complex UI, use `ctx.ui.custom()`. This temporarily replaces the editor with your component until `done()` is called:
|
|
1577
|
+
|
|
1578
|
+
```typescript
|
|
1579
|
+
import { Text, Component } from "@mariozechner/pi-tui";
|
|
1580
|
+
|
|
1581
|
+
const result = await ctx.ui.custom<boolean>((tui, theme, keybindings, done) => {
|
|
1582
|
+
const text = new Text("Press Enter to confirm, Escape to cancel", 1, 1);
|
|
1583
|
+
|
|
1584
|
+
text.onKey = (key) => {
|
|
1585
|
+
if (key === "return") done(true);
|
|
1586
|
+
if (key === "escape") done(false);
|
|
1587
|
+
return true;
|
|
1588
|
+
};
|
|
1589
|
+
|
|
1590
|
+
return text;
|
|
1591
|
+
});
|
|
1592
|
+
|
|
1593
|
+
if (result) {
|
|
1594
|
+
// User pressed Enter
|
|
1595
|
+
}
|
|
1596
|
+
```
|
|
1597
|
+
|
|
1598
|
+
The callback receives:
|
|
1599
|
+
- `tui` - TUI instance (for screen dimensions, focus management)
|
|
1600
|
+
- `theme` - Current theme for styling
|
|
1601
|
+
- `keybindings` - App keybinding manager (for checking shortcuts)
|
|
1602
|
+
- `done(value)` - Call to close component and return value
|
|
1603
|
+
|
|
1604
|
+
See [tui.md](tui.md) for the full component API.
|
|
1605
|
+
|
|
1606
|
+
#### Overlay Mode (Experimental)
|
|
1607
|
+
|
|
1608
|
+
Pass `{ overlay: true }` to render the component as a floating modal on top of existing content, without clearing the screen:
|
|
1609
|
+
|
|
1610
|
+
```typescript
|
|
1611
|
+
const result = await ctx.ui.custom<string | null>(
|
|
1612
|
+
(tui, theme, keybindings, done) => new MyOverlayComponent({ onClose: done }),
|
|
1613
|
+
{ overlay: true }
|
|
1614
|
+
);
|
|
1615
|
+
```
|
|
1616
|
+
|
|
1617
|
+
For advanced positioning (anchors, margins, percentages, responsive visibility), pass `overlayOptions`. Use `onHandle` to control visibility programmatically:
|
|
1618
|
+
|
|
1619
|
+
```typescript
|
|
1620
|
+
const result = await ctx.ui.custom<string | null>(
|
|
1621
|
+
(tui, theme, keybindings, done) => new MyOverlayComponent({ onClose: done }),
|
|
1622
|
+
{
|
|
1623
|
+
overlay: true,
|
|
1624
|
+
overlayOptions: { anchor: "top-right", width: "50%", margin: 2 },
|
|
1625
|
+
onHandle: (handle) => { /* handle.setHidden(true/false) */ }
|
|
1626
|
+
}
|
|
1627
|
+
);
|
|
1628
|
+
```
|
|
1629
|
+
|
|
1630
|
+
See [tui.md](tui.md) for the full `OverlayOptions` API and [overlay-qa-tests.ts](../examples/extensions/overlay-qa-tests.ts) for examples.
|
|
1631
|
+
|
|
1632
|
+
### Custom Editor
|
|
1633
|
+
|
|
1634
|
+
Replace the main input editor with a custom implementation (vim mode, emacs mode, etc.):
|
|
1635
|
+
|
|
1636
|
+
```typescript
|
|
1637
|
+
import { CustomEditor, type ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
1638
|
+
import { matchesKey } from "@mariozechner/pi-tui";
|
|
1639
|
+
|
|
1640
|
+
class VimEditor extends CustomEditor {
|
|
1641
|
+
private mode: "normal" | "insert" = "insert";
|
|
1642
|
+
|
|
1643
|
+
handleInput(data: string): void {
|
|
1644
|
+
if (matchesKey(data, "escape") && this.mode === "insert") {
|
|
1645
|
+
this.mode = "normal";
|
|
1646
|
+
return;
|
|
1647
|
+
}
|
|
1648
|
+
if (this.mode === "normal" && data === "i") {
|
|
1649
|
+
this.mode = "insert";
|
|
1650
|
+
return;
|
|
1651
|
+
}
|
|
1652
|
+
super.handleInput(data); // App keybindings + text editing
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
export default function (pi: ExtensionAPI) {
|
|
1657
|
+
pi.on("session_start", (_event, ctx) => {
|
|
1658
|
+
ctx.ui.setEditorComponent((_tui, theme, keybindings) =>
|
|
1659
|
+
new VimEditor(theme, keybindings)
|
|
1660
|
+
);
|
|
1661
|
+
});
|
|
1662
|
+
}
|
|
1663
|
+
```
|
|
1664
|
+
|
|
1665
|
+
**Key points:**
|
|
1666
|
+
- Extend `CustomEditor` (not base `Editor`) to get app keybindings (escape to abort, ctrl+d, model switching)
|
|
1667
|
+
- Call `super.handleInput(data)` for keys you don't handle
|
|
1668
|
+
- Factory receives `theme` and `keybindings` from the app
|
|
1669
|
+
- Pass `undefined` to restore default: `ctx.ui.setEditorComponent(undefined)`
|
|
1670
|
+
|
|
1671
|
+
See [tui.md](tui.md) Pattern 7 for a complete example with mode indicator.
|
|
1672
|
+
|
|
1673
|
+
### Message Rendering
|
|
1674
|
+
|
|
1675
|
+
Register a custom renderer for messages with your `customType`:
|
|
1676
|
+
|
|
1677
|
+
```typescript
|
|
1678
|
+
import { Text } from "@mariozechner/pi-tui";
|
|
1679
|
+
|
|
1680
|
+
pi.registerMessageRenderer("my-extension", (message, options, theme) => {
|
|
1681
|
+
const { expanded } = options;
|
|
1682
|
+
let text = theme.fg("accent", `[${message.customType}] `);
|
|
1683
|
+
text += message.content;
|
|
1684
|
+
|
|
1685
|
+
if (expanded && message.details) {
|
|
1686
|
+
text += "\n" + theme.fg("dim", JSON.stringify(message.details, null, 2));
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
return new Text(text, 0, 0);
|
|
1690
|
+
});
|
|
1691
|
+
```
|
|
1692
|
+
|
|
1693
|
+
Messages are sent via `pi.sendMessage()`:
|
|
1694
|
+
|
|
1695
|
+
```typescript
|
|
1696
|
+
pi.sendMessage({
|
|
1697
|
+
customType: "my-extension", // Matches registerMessageRenderer
|
|
1698
|
+
content: "Status update",
|
|
1699
|
+
display: true, // Show in TUI
|
|
1700
|
+
details: { ... }, // Available in renderer
|
|
1701
|
+
});
|
|
1702
|
+
```
|
|
1703
|
+
|
|
1704
|
+
### Theme Colors
|
|
1705
|
+
|
|
1706
|
+
All render functions receive a `theme` object. See [themes.md](themes.md) for creating custom themes and the full color palette.
|
|
1707
|
+
|
|
1708
|
+
```typescript
|
|
1709
|
+
// Foreground colors
|
|
1710
|
+
theme.fg("toolTitle", text) // Tool names
|
|
1711
|
+
theme.fg("accent", text) // Highlights
|
|
1712
|
+
theme.fg("success", text) // Success (green)
|
|
1713
|
+
theme.fg("error", text) // Errors (red)
|
|
1714
|
+
theme.fg("warning", text) // Warnings (yellow)
|
|
1715
|
+
theme.fg("muted", text) // Secondary text
|
|
1716
|
+
theme.fg("dim", text) // Tertiary text
|
|
1717
|
+
|
|
1718
|
+
// Text styles
|
|
1719
|
+
theme.bold(text)
|
|
1720
|
+
theme.italic(text)
|
|
1721
|
+
theme.strikethrough(text)
|
|
1722
|
+
```
|
|
1723
|
+
|
|
1724
|
+
For syntax highlighting in custom tool renderers:
|
|
1725
|
+
|
|
1726
|
+
```typescript
|
|
1727
|
+
import { highlightCode, getLanguageFromPath } from "@mariozechner/pi-coding-agent";
|
|
1728
|
+
|
|
1729
|
+
// Highlight code with explicit language
|
|
1730
|
+
const highlighted = highlightCode("const x = 1;", "typescript", theme);
|
|
1731
|
+
|
|
1732
|
+
// Auto-detect language from file path
|
|
1733
|
+
const lang = getLanguageFromPath("/path/to/file.rs"); // "rust"
|
|
1734
|
+
const highlighted = highlightCode(code, lang, theme);
|
|
1735
|
+
```
|
|
1736
|
+
|
|
1737
|
+
## Error Handling
|
|
1738
|
+
|
|
1739
|
+
- Extension errors are logged, agent continues
|
|
1740
|
+
- `tool_call` errors block the tool (fail-safe)
|
|
1741
|
+
- Tool `execute` errors are reported to the LLM with `isError: true`
|
|
1742
|
+
|
|
1743
|
+
## Mode Behavior
|
|
1744
|
+
|
|
1745
|
+
| Mode | UI Methods | Notes |
|
|
1746
|
+
|------|-----------|-------|
|
|
1747
|
+
| Interactive | Full TUI | Normal operation |
|
|
1748
|
+
| RPC (`--mode rpc`) | JSON protocol | Host handles UI, see [rpc.md](rpc.md) |
|
|
1749
|
+
| JSON (`--mode json`) | No-op | Event stream to stdout, see [json.md](json.md) |
|
|
1750
|
+
| Print (`-p`) | No-op | Extensions run but can't prompt |
|
|
1751
|
+
|
|
1752
|
+
In non-interactive modes, check `ctx.hasUI` before using UI methods.
|
|
1753
|
+
|
|
1754
|
+
## Examples Reference
|
|
1755
|
+
|
|
1756
|
+
All examples in [examples/extensions/](../examples/extensions/).
|
|
1757
|
+
|
|
1758
|
+
| Example | Description | Key APIs |
|
|
1759
|
+
|---------|-------------|----------|
|
|
1760
|
+
| **Tools** |||
|
|
1761
|
+
| `hello.ts` | Minimal tool registration | `registerTool` |
|
|
1762
|
+
| `question.ts` | Tool with user interaction | `registerTool`, `ui.select` |
|
|
1763
|
+
| `questionnaire.ts` | Multi-step wizard tool | `registerTool`, `ui.custom` |
|
|
1764
|
+
| `todo.ts` | Stateful tool with persistence | `registerTool`, `appendEntry`, `renderResult`, session events |
|
|
1765
|
+
| `truncated-tool.ts` | Output truncation example | `registerTool`, `truncateHead` |
|
|
1766
|
+
| `tool-override.ts` | Override built-in read tool | `registerTool` (same name as built-in) |
|
|
1767
|
+
| **Commands** |||
|
|
1768
|
+
| `pirate.ts` | Modify system prompt per-turn | `registerCommand`, `before_agent_start` |
|
|
1769
|
+
| `summarize.ts` | Conversation summary command | `registerCommand`, `ui.custom` |
|
|
1770
|
+
| `handoff.ts` | Cross-provider model handoff | `registerCommand`, `ui.editor`, `ui.custom` |
|
|
1771
|
+
| `qna.ts` | Q&A with custom UI | `registerCommand`, `ui.custom`, `setEditorText` |
|
|
1772
|
+
| `send-user-message.ts` | Inject user messages | `registerCommand`, `sendUserMessage` |
|
|
1773
|
+
| `shutdown-command.ts` | Graceful shutdown command | `registerCommand`, `shutdown()` |
|
|
1774
|
+
| **Events & Gates** |||
|
|
1775
|
+
| `permission-gate.ts` | Block dangerous commands | `on("tool_call")`, `ui.confirm` |
|
|
1776
|
+
| `protected-paths.ts` | Block writes to specific paths | `on("tool_call")` |
|
|
1777
|
+
| `confirm-destructive.ts` | Confirm session changes | `on("session_before_switch")`, `on("session_before_fork")` |
|
|
1778
|
+
| `dirty-repo-guard.ts` | Warn on dirty git repo | `on("session_before_*")`, `exec` |
|
|
1779
|
+
| `input-transform.ts` | Transform user input | `on("input")` |
|
|
1780
|
+
| `model-status.ts` | React to model changes | `on("model_select")`, `setStatus` |
|
|
1781
|
+
| `system-prompt-header.ts` | Display system prompt info | `on("agent_start")`, `getSystemPrompt` |
|
|
1782
|
+
| `claude-rules.ts` | Load rules from files | `on("session_start")`, `on("before_agent_start")` |
|
|
1783
|
+
| `file-trigger.ts` | File watcher triggers messages | `sendMessage` |
|
|
1784
|
+
| **Compaction & Sessions** |||
|
|
1785
|
+
| `custom-compaction.ts` | Custom compaction summary | `on("session_before_compact")` |
|
|
1786
|
+
| `trigger-compact.ts` | Trigger compaction manually | `compact()` |
|
|
1787
|
+
| `git-checkpoint.ts` | Git stash on turns | `on("turn_end")`, `on("session_fork")`, `exec` |
|
|
1788
|
+
| `auto-commit-on-exit.ts` | Commit on shutdown | `on("session_shutdown")`, `exec` |
|
|
1789
|
+
| **UI Components** |||
|
|
1790
|
+
| `status-line.ts` | Footer status indicator | `setStatus`, session events |
|
|
1791
|
+
| `custom-footer.ts` | Replace footer entirely | `registerCommand`, `setFooter` |
|
|
1792
|
+
| `custom-header.ts` | Replace startup header | `on("session_start")`, `setHeader` |
|
|
1793
|
+
| `modal-editor.ts` | Vim-style modal editor | `setEditorComponent`, `CustomEditor` |
|
|
1794
|
+
| `rainbow-editor.ts` | Custom editor styling | `setEditorComponent` |
|
|
1795
|
+
| `widget-placement.ts` | Widget above/below editor | `setWidget` |
|
|
1796
|
+
| `overlay-test.ts` | Overlay components | `ui.custom` with overlay options |
|
|
1797
|
+
| `overlay-qa-tests.ts` | Comprehensive overlay tests | `ui.custom`, all overlay options |
|
|
1798
|
+
| `notify.ts` | Simple notifications | `ui.notify` |
|
|
1799
|
+
| `timed-confirm.ts` | Dialogs with timeout | `ui.confirm` with timeout/signal |
|
|
1800
|
+
| `mac-system-theme.ts` | Auto-switch theme | `setTheme`, `exec` |
|
|
1801
|
+
| **Complex Extensions** |||
|
|
1802
|
+
| `plan-mode/` | Full plan mode implementation | All event types, `registerCommand`, `registerShortcut`, `registerFlag`, `setStatus`, `setWidget`, `sendMessage`, `setActiveTools` |
|
|
1803
|
+
| `preset.ts` | Saveable presets (model, tools, thinking) | `registerCommand`, `registerShortcut`, `registerFlag`, `setModel`, `setActiveTools`, `setThinkingLevel`, `appendEntry` |
|
|
1804
|
+
| `tools.ts` | Toggle tools on/off UI | `registerCommand`, `setActiveTools`, `SettingsList`, session events |
|
|
1805
|
+
| **Remote & Sandbox** |||
|
|
1806
|
+
| `ssh.ts` | SSH remote execution | `registerFlag`, `on("user_bash")`, `on("before_agent_start")`, tool operations |
|
|
1807
|
+
| `interactive-shell.ts` | Persistent shell session | `on("user_bash")` |
|
|
1808
|
+
| `sandbox/` | Sandboxed tool execution | Tool operations |
|
|
1809
|
+
| `subagent/` | Spawn sub-agents | `registerTool`, `exec` |
|
|
1810
|
+
| **Games** |||
|
|
1811
|
+
| `snake.ts` | Snake game | `registerCommand`, `ui.custom`, keyboard handling |
|
|
1812
|
+
| `space-invaders.ts` | Space Invaders game | `registerCommand`, `ui.custom` |
|
|
1813
|
+
| `doom-overlay/` | Doom in overlay | `ui.custom` with overlay |
|
|
1814
|
+
| **Providers** |||
|
|
1815
|
+
| `custom-provider-anthropic/` | Custom Anthropic proxy | `registerProvider` |
|
|
1816
|
+
| `custom-provider-gitlab-duo/` | GitLab Duo integration | `registerProvider` with OAuth |
|
|
1817
|
+
| **Messages & Communication** |||
|
|
1818
|
+
| `message-renderer.ts` | Custom message rendering | `registerMessageRenderer`, `sendMessage` |
|
|
1819
|
+
| `event-bus.ts` | Inter-extension events | `pi.events` |
|
|
1820
|
+
| **Session Metadata** |||
|
|
1821
|
+
| `session-name.ts` | Name sessions for selector | `setSessionName`, `getSessionName` |
|
|
1822
|
+
| `bookmark.ts` | Bookmark entries for /tree | `setLabel` |
|
|
1823
|
+
| **Misc** |||
|
|
1824
|
+
| `antigravity-image-gen.ts` | Image generation tool | `registerTool`, Google Antigravity |
|
|
1825
|
+
| `inline-bash.ts` | Inline bash in tool calls | `on("tool_call")` |
|
|
1826
|
+
| `bash-spawn-hook.ts` | Adjust bash command, cwd, and env before execution | `createBashTool`, `spawnHook` |
|
|
1827
|
+
| `with-deps/` | Extension with npm dependencies | Package structure with `package.json` |
|