@imdigitalashish/zpi 0.1.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 +2801 -0
- package/README.md +95 -0
- package/dist/cli/args.d.ts +47 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/args.js +293 -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 +571 -0
- package/dist/core/agent-session.d.ts.map +1 -0
- package/dist/core/agent-session.js +2353 -0
- package/dist/core/agent-session.js.map +1 -0
- package/dist/core/auth-storage.d.ts +129 -0
- package/dist/core/auth-storage.d.ts.map +1 -0
- package/dist/core/auth-storage.js +394 -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 +971 -0
- package/dist/core/export-html/template.html +54 -0
- package/dist/core/export-html/template.js +1586 -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 +402 -0
- package/dist/core/extensions/loader.js.map +1 -0
- package/dist/core/extensions/runner.d.ts +146 -0
- package/dist/core/extensions/runner.d.ts.map +1 -0
- package/dist/core/extensions/runner.js +626 -0
- package/dist/core/extensions/runner.js.map +1 -0
- package/dist/core/extensions/types.d.ts +984 -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/memory.d.ts +64 -0
- package/dist/core/memory.d.ts.map +1 -0
- package/dist/core/memory.js +247 -0
- package/dist/core/memory.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 +149 -0
- package/dist/core/messages.js.map +1 -0
- package/dist/core/model-registry.d.ts +102 -0
- package/dist/core/model-registry.d.ts.map +1 -0
- package/dist/core/model-registry.js +515 -0
- package/dist/core/model-registry.js.map +1 -0
- package/dist/core/model-resolver.d.ts +104 -0
- package/dist/core/model-resolver.d.ts.map +1 -0
- package/dist/core/model-resolver.js +403 -0
- package/dist/core/model-resolver.js.map +1 -0
- package/dist/core/package-manager.d.ts +151 -0
- package/dist/core/package-manager.d.ts.map +1 -0
- package/dist/core/package-manager.js +1426 -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 +238 -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 +230 -0
- package/dist/core/settings-manager.d.ts.map +1 -0
- package/dist/core/settings-manager.js +656 -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 +364 -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 +23 -0
- package/dist/core/slash-commands.js.map +1 -0
- package/dist/core/system-prompt.d.ts +26 -0
- package/dist/core/system-prompt.d.ts.map +1 -0
- package/dist/core/system-prompt.js +150 -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 +102 -0
- package/dist/core/tools/index.d.ts.map +1 -0
- package/dist/core/tools/index.js +71 -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/memory.d.ts +45 -0
- package/dist/core/tools/memory.d.ts.map +1 -0
- package/dist/core/tools/memory.js +346 -0
- package/dist/core/tools/memory.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 +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +8 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +651 -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 +96 -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 +213 -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 +271 -0
- package/dist/modes/interactive/components/model-selector.js.map +1 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts +19 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/oauth-selector.js +97 -0
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts +49 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/scoped-models-selector.js +275 -0
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
- package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
- package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
- package/dist/modes/interactive/components/session-selector-search.js +155 -0
- package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
- package/dist/modes/interactive/components/session-selector.d.ts +95 -0
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/session-selector.js +851 -0
- package/dist/modes/interactive/components/session-selector.js.map +1 -0
- package/dist/modes/interactive/components/settings-selector.d.ts +58 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/settings-selector.js +299 -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 +636 -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 +316 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
- package/dist/modes/interactive/interactive-mode.js +3848 -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 +101 -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 +511 -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 +26 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +163 -0
- package/dist/utils/git.js.map +1 -0
- package/dist/utils/image-convert.d.ts +9 -0
- package/dist/utils/image-convert.d.ts.map +1 -0
- package/dist/utils/image-convert.js +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 +207 -0
- package/dist/utils/tools-manager.js.map +1 -0
- package/docs/compaction.md +390 -0
- package/docs/custom-provider.md +548 -0
- package/docs/development.md +69 -0
- package/docs/extensions.md +1935 -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 +293 -0
- package/docs/packages.md +209 -0
- package/docs/prompt-templates.md +67 -0
- package/docs/providers.md +186 -0
- package/docs/rpc.md +1317 -0
- package/docs/sdk.md +968 -0
- package/docs/session.md +412 -0
- package/docs/settings.md +223 -0
- package/docs/shell-aliases.md +13 -0
- package/docs/skills.md +231 -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 +203 -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/reload-runtime.ts +37 -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 +964 -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 +51 -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 +96 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bash command execution with streaming support and cancellation.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a unified bash execution implementation used by:
|
|
5
|
+
* - AgentSession.executeBash() for interactive and RPC modes
|
|
6
|
+
* - Direct calls from modes that need bash execution
|
|
7
|
+
*/
|
|
8
|
+
import { randomBytes } from "node:crypto";
|
|
9
|
+
import { createWriteStream } from "node:fs";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { spawn } from "child_process";
|
|
13
|
+
import stripAnsi from "strip-ansi";
|
|
14
|
+
import { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from "../utils/shell.js";
|
|
15
|
+
import { DEFAULT_MAX_BYTES, truncateTail } from "./tools/truncate.js";
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// Implementation
|
|
18
|
+
// ============================================================================
|
|
19
|
+
/**
|
|
20
|
+
* Execute a bash command with optional streaming and cancellation support.
|
|
21
|
+
*
|
|
22
|
+
* Features:
|
|
23
|
+
* - Streams sanitized output via onChunk callback
|
|
24
|
+
* - Writes large output to temp file for later retrieval
|
|
25
|
+
* - Supports cancellation via AbortSignal
|
|
26
|
+
* - Sanitizes output (strips ANSI, removes binary garbage, normalizes newlines)
|
|
27
|
+
* - Truncates output if it exceeds the default max bytes
|
|
28
|
+
*
|
|
29
|
+
* @param command - The bash command to execute
|
|
30
|
+
* @param options - Optional streaming callback and abort signal
|
|
31
|
+
* @returns Promise resolving to execution result
|
|
32
|
+
*/
|
|
33
|
+
export function executeBash(command, options) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const { shell, args } = getShellConfig();
|
|
36
|
+
const child = spawn(shell, [...args, command], {
|
|
37
|
+
detached: true,
|
|
38
|
+
env: getShellEnv(),
|
|
39
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
40
|
+
});
|
|
41
|
+
// Track sanitized output for truncation
|
|
42
|
+
const outputChunks = [];
|
|
43
|
+
let outputBytes = 0;
|
|
44
|
+
const maxOutputBytes = DEFAULT_MAX_BYTES * 2;
|
|
45
|
+
// Temp file for large output
|
|
46
|
+
let tempFilePath;
|
|
47
|
+
let tempFileStream;
|
|
48
|
+
let totalBytes = 0;
|
|
49
|
+
// Handle abort signal
|
|
50
|
+
const abortHandler = () => {
|
|
51
|
+
if (child.pid) {
|
|
52
|
+
killProcessTree(child.pid);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
if (options?.signal) {
|
|
56
|
+
if (options.signal.aborted) {
|
|
57
|
+
// Already aborted, don't even start
|
|
58
|
+
child.kill();
|
|
59
|
+
resolve({
|
|
60
|
+
output: "",
|
|
61
|
+
exitCode: undefined,
|
|
62
|
+
cancelled: true,
|
|
63
|
+
truncated: false,
|
|
64
|
+
});
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
options.signal.addEventListener("abort", abortHandler, { once: true });
|
|
68
|
+
}
|
|
69
|
+
const decoder = new TextDecoder();
|
|
70
|
+
const handleData = (data) => {
|
|
71
|
+
totalBytes += data.length;
|
|
72
|
+
// Sanitize once at the source: strip ANSI, replace binary garbage, normalize newlines
|
|
73
|
+
const text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\r/g, "");
|
|
74
|
+
// Start writing to temp file if exceeds threshold
|
|
75
|
+
if (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {
|
|
76
|
+
const id = randomBytes(8).toString("hex");
|
|
77
|
+
tempFilePath = join(tmpdir(), `pi-bash-${id}.log`);
|
|
78
|
+
tempFileStream = createWriteStream(tempFilePath);
|
|
79
|
+
// Write already-buffered chunks to temp file
|
|
80
|
+
for (const chunk of outputChunks) {
|
|
81
|
+
tempFileStream.write(chunk);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (tempFileStream) {
|
|
85
|
+
tempFileStream.write(text);
|
|
86
|
+
}
|
|
87
|
+
// Keep rolling buffer of sanitized text
|
|
88
|
+
outputChunks.push(text);
|
|
89
|
+
outputBytes += text.length;
|
|
90
|
+
while (outputBytes > maxOutputBytes && outputChunks.length > 1) {
|
|
91
|
+
const removed = outputChunks.shift();
|
|
92
|
+
outputBytes -= removed.length;
|
|
93
|
+
}
|
|
94
|
+
// Stream to callback if provided
|
|
95
|
+
if (options?.onChunk) {
|
|
96
|
+
options.onChunk(text);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
child.stdout?.on("data", handleData);
|
|
100
|
+
child.stderr?.on("data", handleData);
|
|
101
|
+
child.on("close", (code) => {
|
|
102
|
+
// Clean up abort listener
|
|
103
|
+
if (options?.signal) {
|
|
104
|
+
options.signal.removeEventListener("abort", abortHandler);
|
|
105
|
+
}
|
|
106
|
+
if (tempFileStream) {
|
|
107
|
+
tempFileStream.end();
|
|
108
|
+
}
|
|
109
|
+
// Combine buffered chunks for truncation (already sanitized)
|
|
110
|
+
const fullOutput = outputChunks.join("");
|
|
111
|
+
const truncationResult = truncateTail(fullOutput);
|
|
112
|
+
// code === null means killed (cancelled)
|
|
113
|
+
const cancelled = code === null;
|
|
114
|
+
resolve({
|
|
115
|
+
output: truncationResult.truncated ? truncationResult.content : fullOutput,
|
|
116
|
+
exitCode: cancelled ? undefined : code,
|
|
117
|
+
cancelled,
|
|
118
|
+
truncated: truncationResult.truncated,
|
|
119
|
+
fullOutputPath: tempFilePath,
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
child.on("error", (err) => {
|
|
123
|
+
// Clean up abort listener
|
|
124
|
+
if (options?.signal) {
|
|
125
|
+
options.signal.removeEventListener("abort", abortHandler);
|
|
126
|
+
}
|
|
127
|
+
if (tempFileStream) {
|
|
128
|
+
tempFileStream.end();
|
|
129
|
+
}
|
|
130
|
+
reject(err);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Execute a bash command using custom BashOperations.
|
|
136
|
+
* Used for remote execution (SSH, containers, etc.).
|
|
137
|
+
*/
|
|
138
|
+
export async function executeBashWithOperations(command, cwd, operations, options) {
|
|
139
|
+
const outputChunks = [];
|
|
140
|
+
let outputBytes = 0;
|
|
141
|
+
const maxOutputBytes = DEFAULT_MAX_BYTES * 2;
|
|
142
|
+
let tempFilePath;
|
|
143
|
+
let tempFileStream;
|
|
144
|
+
let totalBytes = 0;
|
|
145
|
+
const decoder = new TextDecoder();
|
|
146
|
+
const onData = (data) => {
|
|
147
|
+
totalBytes += data.length;
|
|
148
|
+
// Sanitize: strip ANSI, replace binary garbage, normalize newlines
|
|
149
|
+
const text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\r/g, "");
|
|
150
|
+
// Start writing to temp file if exceeds threshold
|
|
151
|
+
if (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {
|
|
152
|
+
const id = randomBytes(8).toString("hex");
|
|
153
|
+
tempFilePath = join(tmpdir(), `pi-bash-${id}.log`);
|
|
154
|
+
tempFileStream = createWriteStream(tempFilePath);
|
|
155
|
+
for (const chunk of outputChunks) {
|
|
156
|
+
tempFileStream.write(chunk);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (tempFileStream) {
|
|
160
|
+
tempFileStream.write(text);
|
|
161
|
+
}
|
|
162
|
+
// Keep rolling buffer
|
|
163
|
+
outputChunks.push(text);
|
|
164
|
+
outputBytes += text.length;
|
|
165
|
+
while (outputBytes > maxOutputBytes && outputChunks.length > 1) {
|
|
166
|
+
const removed = outputChunks.shift();
|
|
167
|
+
outputBytes -= removed.length;
|
|
168
|
+
}
|
|
169
|
+
// Stream to callback
|
|
170
|
+
if (options?.onChunk) {
|
|
171
|
+
options.onChunk(text);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
try {
|
|
175
|
+
const result = await operations.exec(command, cwd, {
|
|
176
|
+
onData,
|
|
177
|
+
signal: options?.signal,
|
|
178
|
+
});
|
|
179
|
+
if (tempFileStream) {
|
|
180
|
+
tempFileStream.end();
|
|
181
|
+
}
|
|
182
|
+
const fullOutput = outputChunks.join("");
|
|
183
|
+
const truncationResult = truncateTail(fullOutput);
|
|
184
|
+
const cancelled = options?.signal?.aborted ?? false;
|
|
185
|
+
return {
|
|
186
|
+
output: truncationResult.truncated ? truncationResult.content : fullOutput,
|
|
187
|
+
exitCode: cancelled ? undefined : (result.exitCode ?? undefined),
|
|
188
|
+
cancelled,
|
|
189
|
+
truncated: truncationResult.truncated,
|
|
190
|
+
fullOutputPath: tempFilePath,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
if (tempFileStream) {
|
|
195
|
+
tempFileStream.end();
|
|
196
|
+
}
|
|
197
|
+
// Check if it was an abort
|
|
198
|
+
if (options?.signal?.aborted) {
|
|
199
|
+
const fullOutput = outputChunks.join("");
|
|
200
|
+
const truncationResult = truncateTail(fullOutput);
|
|
201
|
+
return {
|
|
202
|
+
output: truncationResult.truncated ? truncationResult.content : fullOutput,
|
|
203
|
+
exitCode: undefined,
|
|
204
|
+
cancelled: true,
|
|
205
|
+
truncated: truncationResult.truncated,
|
|
206
|
+
fullOutputPath: tempFilePath,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
throw err;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=bash-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bash-executor.js","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAoB,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAqB,KAAK,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,cAA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AO;YACN,MAAM,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU;YAC1E,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,IAAI,SAAS,CAAC;YAChE,SAAS;YACT,SAAS,EAAE,gBAAgB,CAAC,SAAS;YACrC,cAAc,EAAE,YAAY;SAC5B,CAAC;IACH,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,IAAI,cAAc,EAAE,CAAC;YACpB,cAAc,CAAC,GAAG,EAAE,CAAC;QACtB,CAAC;QAED,2BAA2B;QAC3B,IAAI,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;YAC9B,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACzC,MAAM,gBAAgB,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC;YAClD,OAAO;gBACN,MAAM,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU;gBAC1E,QAAQ,EAAE,SAAS;gBACnB,SAAS,EAAE,IAAI;gBACf,SAAS,EAAE,gBAAgB,CAAC,SAAS;gBACrC,cAAc,EAAE,YAAY;aAC5B,CAAC;QACH,CAAC;QAED,MAAM,GAAG,CAAC;IACX,CAAC;AAAA,CACD","sourcesContent":["/**\r\n * Bash command execution with streaming support and cancellation.\r\n *\r\n * This module provides a unified bash execution implementation used by:\r\n * - AgentSession.executeBash() for interactive and RPC modes\r\n * - Direct calls from modes that need bash execution\r\n */\r\n\r\nimport { randomBytes } from \"node:crypto\";\r\nimport { createWriteStream, type WriteStream } from \"node:fs\";\r\nimport { tmpdir } from \"node:os\";\r\nimport { join } from \"node:path\";\r\nimport { type ChildProcess, spawn } from \"child_process\";\r\nimport stripAnsi from \"strip-ansi\";\r\nimport { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from \"../utils/shell.js\";\r\nimport type { BashOperations } from \"./tools/bash.js\";\r\nimport { DEFAULT_MAX_BYTES, truncateTail } from \"./tools/truncate.js\";\r\n\r\n// ============================================================================\r\n// Types\r\n// ============================================================================\r\n\r\nexport interface BashExecutorOptions {\r\n\t/** Callback for streaming output chunks (already sanitized) */\r\n\tonChunk?: (chunk: string) => void;\r\n\t/** AbortSignal for cancellation */\r\n\tsignal?: AbortSignal;\r\n}\r\n\r\nexport interface BashResult {\r\n\t/** Combined stdout + stderr output (sanitized, possibly truncated) */\r\n\toutput: string;\r\n\t/** Process exit code (undefined if killed/cancelled) */\r\n\texitCode: number | undefined;\r\n\t/** Whether the command was cancelled via signal */\r\n\tcancelled: boolean;\r\n\t/** Whether the output was truncated */\r\n\ttruncated: boolean;\r\n\t/** Path to temp file containing full output (if output exceeded truncation threshold) */\r\n\tfullOutputPath?: string;\r\n}\r\n\r\n// ============================================================================\r\n// Implementation\r\n// ============================================================================\r\n\r\n/**\r\n * Execute a bash command with optional streaming and cancellation support.\r\n *\r\n * Features:\r\n * - Streams sanitized output via onChunk callback\r\n * - Writes large output to temp file for later retrieval\r\n * - Supports cancellation via AbortSignal\r\n * - Sanitizes output (strips ANSI, removes binary garbage, normalizes newlines)\r\n * - Truncates output if it exceeds the default max bytes\r\n *\r\n * @param command - The bash command to execute\r\n * @param options - Optional streaming callback and abort signal\r\n * @returns Promise resolving to execution result\r\n */\r\nexport function executeBash(command: string, options?: BashExecutorOptions): Promise<BashResult> {\r\n\treturn new Promise((resolve, reject) => {\r\n\t\tconst { shell, args } = getShellConfig();\r\n\t\tconst child: ChildProcess = spawn(shell, [...args, command], {\r\n\t\t\tdetached: true,\r\n\t\t\tenv: getShellEnv(),\r\n\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\r\n\t\t});\r\n\r\n\t\t// Track sanitized output for truncation\r\n\t\tconst outputChunks: string[] = [];\r\n\t\tlet outputBytes = 0;\r\n\t\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\r\n\r\n\t\t// Temp file for large output\r\n\t\tlet tempFilePath: string | undefined;\r\n\t\tlet tempFileStream: WriteStream | undefined;\r\n\t\tlet totalBytes = 0;\r\n\r\n\t\t// Handle abort signal\r\n\t\tconst abortHandler = () => {\r\n\t\t\tif (child.pid) {\r\n\t\t\t\tkillProcessTree(child.pid);\r\n\t\t\t}\r\n\t\t};\r\n\r\n\t\tif (options?.signal) {\r\n\t\t\tif (options.signal.aborted) {\r\n\t\t\t\t// Already aborted, don't even start\r\n\t\t\t\tchild.kill();\r\n\t\t\t\tresolve({\r\n\t\t\t\t\toutput: \"\",\r\n\t\t\t\t\texitCode: undefined,\r\n\t\t\t\t\tcancelled: true,\r\n\t\t\t\t\ttruncated: false,\r\n\t\t\t\t});\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\toptions.signal.addEventListener(\"abort\", abortHandler, { once: true });\r\n\t\t}\r\n\r\n\t\tconst decoder = new TextDecoder();\r\n\r\n\t\tconst handleData = (data: Buffer) => {\r\n\t\t\ttotalBytes += data.length;\r\n\r\n\t\t\t// Sanitize once at the source: strip ANSI, replace binary garbage, normalize newlines\r\n\t\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\r\n\r\n\t\t\t// Start writing to temp file if exceeds threshold\r\n\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\r\n\t\t\t\tconst id = randomBytes(8).toString(\"hex\");\r\n\t\t\t\ttempFilePath = join(tmpdir(), `pi-bash-${id}.log`);\r\n\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\r\n\t\t\t\t// Write already-buffered chunks to temp file\r\n\t\t\t\tfor (const chunk of outputChunks) {\r\n\t\t\t\t\ttempFileStream.write(chunk);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tif (tempFileStream) {\r\n\t\t\t\ttempFileStream.write(text);\r\n\t\t\t}\r\n\r\n\t\t\t// Keep rolling buffer of sanitized text\r\n\t\t\toutputChunks.push(text);\r\n\t\t\toutputBytes += text.length;\r\n\t\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\r\n\t\t\t\tconst removed = outputChunks.shift()!;\r\n\t\t\t\toutputBytes -= removed.length;\r\n\t\t\t}\r\n\r\n\t\t\t// Stream to callback if provided\r\n\t\t\tif (options?.onChunk) {\r\n\t\t\t\toptions.onChunk(text);\r\n\t\t\t}\r\n\t\t};\r\n\r\n\t\tchild.stdout?.on(\"data\", handleData);\r\n\t\tchild.stderr?.on(\"data\", handleData);\r\n\r\n\t\tchild.on(\"close\", (code) => {\r\n\t\t\t// Clean up abort listener\r\n\t\t\tif (options?.signal) {\r\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\r\n\t\t\t}\r\n\r\n\t\t\tif (tempFileStream) {\r\n\t\t\t\ttempFileStream.end();\r\n\t\t\t}\r\n\r\n\t\t\t// Combine buffered chunks for truncation (already sanitized)\r\n\t\t\tconst fullOutput = outputChunks.join(\"\");\r\n\t\t\tconst truncationResult = truncateTail(fullOutput);\r\n\r\n\t\t\t// code === null means killed (cancelled)\r\n\t\t\tconst cancelled = code === null;\r\n\r\n\t\t\tresolve({\r\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\r\n\t\t\t\texitCode: cancelled ? undefined : code,\r\n\t\t\t\tcancelled,\r\n\t\t\t\ttruncated: truncationResult.truncated,\r\n\t\t\t\tfullOutputPath: tempFilePath,\r\n\t\t\t});\r\n\t\t});\r\n\r\n\t\tchild.on(\"error\", (err) => {\r\n\t\t\t// Clean up abort listener\r\n\t\t\tif (options?.signal) {\r\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\r\n\t\t\t}\r\n\r\n\t\t\tif (tempFileStream) {\r\n\t\t\t\ttempFileStream.end();\r\n\t\t\t}\r\n\r\n\t\t\treject(err);\r\n\t\t});\r\n\t});\r\n}\r\n\r\n/**\r\n * Execute a bash command using custom BashOperations.\r\n * Used for remote execution (SSH, containers, etc.).\r\n */\r\nexport async function executeBashWithOperations(\r\n\tcommand: string,\r\n\tcwd: string,\r\n\toperations: BashOperations,\r\n\toptions?: BashExecutorOptions,\r\n): Promise<BashResult> {\r\n\tconst outputChunks: string[] = [];\r\n\tlet outputBytes = 0;\r\n\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\r\n\r\n\tlet tempFilePath: string | undefined;\r\n\tlet tempFileStream: WriteStream | undefined;\r\n\tlet totalBytes = 0;\r\n\r\n\tconst decoder = new TextDecoder();\r\n\r\n\tconst onData = (data: Buffer) => {\r\n\t\ttotalBytes += data.length;\r\n\r\n\t\t// Sanitize: strip ANSI, replace binary garbage, normalize newlines\r\n\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\r\n\r\n\t\t// Start writing to temp file if exceeds threshold\r\n\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\r\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\r\n\t\t\ttempFilePath = join(tmpdir(), `pi-bash-${id}.log`);\r\n\t\t\ttempFileStream = createWriteStream(tempFilePath);\r\n\t\t\tfor (const chunk of outputChunks) {\r\n\t\t\t\ttempFileStream.write(chunk);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif (tempFileStream) {\r\n\t\t\ttempFileStream.write(text);\r\n\t\t}\r\n\r\n\t\t// Keep rolling buffer\r\n\t\toutputChunks.push(text);\r\n\t\toutputBytes += text.length;\r\n\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\r\n\t\t\tconst removed = outputChunks.shift()!;\r\n\t\t\toutputBytes -= removed.length;\r\n\t\t}\r\n\r\n\t\t// Stream to callback\r\n\t\tif (options?.onChunk) {\r\n\t\t\toptions.onChunk(text);\r\n\t\t}\r\n\t};\r\n\r\n\ttry {\r\n\t\tconst result = await operations.exec(command, cwd, {\r\n\t\t\tonData,\r\n\t\t\tsignal: options?.signal,\r\n\t\t});\r\n\r\n\t\tif (tempFileStream) {\r\n\t\t\ttempFileStream.end();\r\n\t\t}\r\n\r\n\t\tconst fullOutput = outputChunks.join(\"\");\r\n\t\tconst truncationResult = truncateTail(fullOutput);\r\n\t\tconst cancelled = options?.signal?.aborted ?? false;\r\n\r\n\t\treturn {\r\n\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\r\n\t\t\texitCode: cancelled ? undefined : (result.exitCode ?? undefined),\r\n\t\t\tcancelled,\r\n\t\t\ttruncated: truncationResult.truncated,\r\n\t\t\tfullOutputPath: tempFilePath,\r\n\t\t};\r\n\t} catch (err) {\r\n\t\tif (tempFileStream) {\r\n\t\t\ttempFileStream.end();\r\n\t\t}\r\n\r\n\t\t// Check if it was an abort\r\n\t\tif (options?.signal?.aborted) {\r\n\t\t\tconst fullOutput = outputChunks.join(\"\");\r\n\t\t\tconst truncationResult = truncateTail(fullOutput);\r\n\t\t\treturn {\r\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\r\n\t\t\t\texitCode: undefined,\r\n\t\t\t\tcancelled: true,\r\n\t\t\t\ttruncated: truncationResult.truncated,\r\n\t\t\t\tfullOutputPath: tempFilePath,\r\n\t\t\t};\r\n\t\t}\r\n\r\n\t\tthrow err;\r\n\t}\r\n}\r\n"]}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branch summarization for tree navigation.
|
|
3
|
+
*
|
|
4
|
+
* When navigating to a different point in the session tree, this generates
|
|
5
|
+
* a summary of the branch being left so context isn't lost.
|
|
6
|
+
*/
|
|
7
|
+
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
|
8
|
+
import type { Model } from "@mariozechner/pi-ai";
|
|
9
|
+
import type { ReadonlySessionManager, SessionEntry } from "../session-manager.js";
|
|
10
|
+
import { type FileOperations } from "./utils.js";
|
|
11
|
+
export interface BranchSummaryResult {
|
|
12
|
+
summary?: string;
|
|
13
|
+
readFiles?: string[];
|
|
14
|
+
modifiedFiles?: string[];
|
|
15
|
+
aborted?: boolean;
|
|
16
|
+
error?: string;
|
|
17
|
+
}
|
|
18
|
+
/** Details stored in BranchSummaryEntry.details for file tracking */
|
|
19
|
+
export interface BranchSummaryDetails {
|
|
20
|
+
readFiles: string[];
|
|
21
|
+
modifiedFiles: string[];
|
|
22
|
+
}
|
|
23
|
+
export type { FileOperations } from "./utils.js";
|
|
24
|
+
export interface BranchPreparation {
|
|
25
|
+
/** Messages extracted for summarization, in chronological order */
|
|
26
|
+
messages: AgentMessage[];
|
|
27
|
+
/** File operations extracted from tool calls */
|
|
28
|
+
fileOps: FileOperations;
|
|
29
|
+
/** Total estimated tokens in messages */
|
|
30
|
+
totalTokens: number;
|
|
31
|
+
}
|
|
32
|
+
export interface CollectEntriesResult {
|
|
33
|
+
/** Entries to summarize, in chronological order */
|
|
34
|
+
entries: SessionEntry[];
|
|
35
|
+
/** Common ancestor between old and new position, if any */
|
|
36
|
+
commonAncestorId: string | null;
|
|
37
|
+
}
|
|
38
|
+
export interface GenerateBranchSummaryOptions {
|
|
39
|
+
/** Model to use for summarization */
|
|
40
|
+
model: Model<any>;
|
|
41
|
+
/** API key for the model */
|
|
42
|
+
apiKey: string;
|
|
43
|
+
/** Abort signal for cancellation */
|
|
44
|
+
signal: AbortSignal;
|
|
45
|
+
/** Optional custom instructions for summarization */
|
|
46
|
+
customInstructions?: string;
|
|
47
|
+
/** If true, customInstructions replaces the default prompt instead of being appended */
|
|
48
|
+
replaceInstructions?: boolean;
|
|
49
|
+
/** Tokens reserved for prompt + LLM response (default 16384) */
|
|
50
|
+
reserveTokens?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Collect entries that should be summarized when navigating from one position to another.
|
|
54
|
+
*
|
|
55
|
+
* Walks from oldLeafId back to the common ancestor with targetId, collecting entries
|
|
56
|
+
* along the way. Does NOT stop at compaction boundaries - those are included and their
|
|
57
|
+
* summaries become context.
|
|
58
|
+
*
|
|
59
|
+
* @param session - Session manager (read-only access)
|
|
60
|
+
* @param oldLeafId - Current position (where we're navigating from)
|
|
61
|
+
* @param targetId - Target position (where we're navigating to)
|
|
62
|
+
* @returns Entries to summarize and the common ancestor
|
|
63
|
+
*/
|
|
64
|
+
export declare function collectEntriesForBranchSummary(session: ReadonlySessionManager, oldLeafId: string | null, targetId: string): CollectEntriesResult;
|
|
65
|
+
/**
|
|
66
|
+
* Prepare entries for summarization with token budget.
|
|
67
|
+
*
|
|
68
|
+
* Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.
|
|
69
|
+
* This ensures we keep the most recent context when the branch is too long.
|
|
70
|
+
*
|
|
71
|
+
* Also collects file operations from:
|
|
72
|
+
* - Tool calls in assistant messages
|
|
73
|
+
* - Existing branch_summary entries' details (for cumulative tracking)
|
|
74
|
+
*
|
|
75
|
+
* @param entries - Entries in chronological order
|
|
76
|
+
* @param tokenBudget - Maximum tokens to include (0 = no limit)
|
|
77
|
+
*/
|
|
78
|
+
export declare function prepareBranchEntries(entries: SessionEntry[], tokenBudget?: number): BranchPreparation;
|
|
79
|
+
/**
|
|
80
|
+
* Generate a summary of abandoned branch entries.
|
|
81
|
+
*
|
|
82
|
+
* @param entries - Session entries to summarize (chronological order)
|
|
83
|
+
* @param options - Generation options
|
|
84
|
+
*/
|
|
85
|
+
export declare function generateBranchSummary(entries: SessionEntry[], options: GenerateBranchSummaryOptions): Promise<BranchSummaryResult>;
|
|
86
|
+
//# sourceMappingURL=branch-summarization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"branch-summarization.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/branch-summarization.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAQjD,OAAO,K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B,EAC/B,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,QAAQ,EAAE,MAAM,GACd,oBAAoB,CAkCtB;AAmCD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,WAAW,GAAE,MAAU,GAAG,iBAAiB,CAoDxG;AAwCD;;;;;GAKG;AACH,wBAAsB,qBAAqB,CAC1C,OAAO,EAAE,YAAY,EAAE,EACvB,OAAO,EAAE,4BAA4B,GACnC,OAAO,CAAC,mBAAmB,CAAC,CAqE9B","sourcesContent":["/**\r\n * Branch summarization for tree navigation.\r\n *\r\n * When navigating to a different point in the session tree, this generates\r\n * a summary of the branch being left so context isn't lost.\r\n */\r\n\r\nimport type { AgentMessage } from \"@mariozechner/pi-agent-core\";\r\nimport type { Model } from \"@mariozechner/pi-ai\";\r\nimport { completeSimple } from \"@mariozechner/pi-ai\";\r\nimport {\r\n\tconvertToLlm,\r\n\tcreateBranchSummaryMessage,\r\n\tcreateCompactionSummaryMessage,\r\n\tcreateCustomMessage,\r\n} from \"../messages.js\";\r\nimport type { ReadonlySessionManager, SessionEntry } from \"../session-manager.js\";\r\nimport { estimateTokens } from \"./compaction.js\";\r\nimport {\r\n\tcomputeFileLists,\r\n\tcreateFileOps,\r\n\textractFileOpsFromMessage,\r\n\ttype FileOperations,\r\n\tformatFileOperations,\r\n\tSUMMARIZATION_SYSTEM_PROMPT,\r\n\tserializeConversation,\r\n} from \"./utils.js\";\r\n\r\n// ============================================================================\r\n// Types\r\n// ============================================================================\r\n\r\nexport interface BranchSummaryResult {\r\n\tsummary?: string;\r\n\treadFiles?: string[];\r\n\tmodifiedFiles?: string[];\r\n\taborted?: boolean;\r\n\terror?: string;\r\n}\r\n\r\n/** Details stored in BranchSummaryEntry.details for file tracking */\r\nexport interface BranchSummaryDetails {\r\n\treadFiles: string[];\r\n\tmodifiedFiles: string[];\r\n}\r\n\r\nexport type { FileOperations } from \"./utils.js\";\r\n\r\nexport interface BranchPreparation {\r\n\t/** Messages extracted for summarization, in chronological order */\r\n\tmessages: AgentMessage[];\r\n\t/** File operations extracted from tool calls */\r\n\tfileOps: FileOperations;\r\n\t/** Total estimated tokens in messages */\r\n\ttotalTokens: number;\r\n}\r\n\r\nexport interface CollectEntriesResult {\r\n\t/** Entries to summarize, in chronological order */\r\n\tentries: SessionEntry[];\r\n\t/** Common ancestor between old and new position, if any */\r\n\tcommonAncestorId: string | null;\r\n}\r\n\r\nexport interface GenerateBranchSummaryOptions {\r\n\t/** Model to use for summarization */\r\n\tmodel: Model<any>;\r\n\t/** API key for the model */\r\n\tapiKey: string;\r\n\t/** Abort signal for cancellation */\r\n\tsignal: AbortSignal;\r\n\t/** Optional custom instructions for summarization */\r\n\tcustomInstructions?: string;\r\n\t/** If true, customInstructions replaces the default prompt instead of being appended */\r\n\treplaceInstructions?: boolean;\r\n\t/** Tokens reserved for prompt + LLM response (default 16384) */\r\n\treserveTokens?: number;\r\n}\r\n\r\n// ============================================================================\r\n// Entry Collection\r\n// ============================================================================\r\n\r\n/**\r\n * Collect entries that should be summarized when navigating from one position to another.\r\n *\r\n * Walks from oldLeafId back to the common ancestor with targetId, collecting entries\r\n * along the way. Does NOT stop at compaction boundaries - those are included and their\r\n * summaries become context.\r\n *\r\n * @param session - Session manager (read-only access)\r\n * @param oldLeafId - Current position (where we're navigating from)\r\n * @param targetId - Target position (where we're navigating to)\r\n * @returns Entries to summarize and the common ancestor\r\n */\r\nexport function collectEntriesForBranchSummary(\r\n\tsession: ReadonlySessionManager,\r\n\toldLeafId: string | null,\r\n\ttargetId: string,\r\n): CollectEntriesResult {\r\n\t// If no old position, nothing to summarize\r\n\tif (!oldLeafId) {\r\n\t\treturn { entries: [], commonAncestorId: null };\r\n\t}\r\n\r\n\t// Find common ancestor (deepest node that's on both paths)\r\n\tconst oldPath = new Set(session.getBranch(oldLeafId).map((e) => e.id));\r\n\tconst targetPath = session.getBranch(targetId);\r\n\r\n\t// targetPath is root-first, so iterate backwards to find deepest common ancestor\r\n\tlet commonAncestorId: string | null = null;\r\n\tfor (let i = targetPath.length - 1; i >= 0; i--) {\r\n\t\tif (oldPath.has(targetPath[i].id)) {\r\n\t\t\tcommonAncestorId = targetPath[i].id;\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\r\n\t// Collect entries from old leaf back to common ancestor\r\n\tconst entries: SessionEntry[] = [];\r\n\tlet current: string | null = oldLeafId;\r\n\r\n\twhile (current && current !== commonAncestorId) {\r\n\t\tconst entry = session.getEntry(current);\r\n\t\tif (!entry) break;\r\n\t\tentries.push(entry);\r\n\t\tcurrent = entry.parentId;\r\n\t}\r\n\r\n\t// Reverse to get chronological order\r\n\tentries.reverse();\r\n\r\n\treturn { entries, commonAncestorId };\r\n}\r\n\r\n// ============================================================================\r\n// Entry to Message Conversion\r\n// ============================================================================\r\n\r\n/**\r\n * Extract AgentMessage from a session entry.\r\n * Similar to getMessageFromEntry in compaction.ts but also handles compaction entries.\r\n */\r\nfunction getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\r\n\tswitch (entry.type) {\r\n\t\tcase \"message\":\r\n\t\t\t// Skip tool results - context is in assistant's tool call\r\n\t\t\tif (entry.message.role === \"toolResult\") return undefined;\r\n\t\t\treturn entry.message;\r\n\r\n\t\tcase \"custom_message\":\r\n\t\t\treturn createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp);\r\n\r\n\t\tcase \"branch_summary\":\r\n\t\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\r\n\r\n\t\tcase \"compaction\":\r\n\t\t\treturn createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);\r\n\r\n\t\t// These don't contribute to conversation content\r\n\t\tcase \"thinking_level_change\":\r\n\t\tcase \"model_change\":\r\n\t\tcase \"custom\":\r\n\t\tcase \"label\":\r\n\t\t\treturn undefined;\r\n\t}\r\n}\r\n\r\n/**\r\n * Prepare entries for summarization with token budget.\r\n *\r\n * Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.\r\n * This ensures we keep the most recent context when the branch is too long.\r\n *\r\n * Also collects file operations from:\r\n * - Tool calls in assistant messages\r\n * - Existing branch_summary entries' details (for cumulative tracking)\r\n *\r\n * @param entries - Entries in chronological order\r\n * @param tokenBudget - Maximum tokens to include (0 = no limit)\r\n */\r\nexport function prepareBranchEntries(entries: SessionEntry[], tokenBudget: number = 0): BranchPreparation {\r\n\tconst messages: AgentMessage[] = [];\r\n\tconst fileOps = createFileOps();\r\n\tlet totalTokens = 0;\r\n\r\n\t// First pass: collect file ops from ALL entries (even if they don't fit in token budget)\r\n\t// This ensures we capture cumulative file tracking from nested branch summaries\r\n\t// Only extract from pi-generated summaries (fromHook !== true), not extension-generated ones\r\n\tfor (const entry of entries) {\r\n\t\tif (entry.type === \"branch_summary\" && !entry.fromHook && entry.details) {\r\n\t\t\tconst details = entry.details as BranchSummaryDetails;\r\n\t\t\tif (Array.isArray(details.readFiles)) {\r\n\t\t\t\tfor (const f of details.readFiles) fileOps.read.add(f);\r\n\t\t\t}\r\n\t\t\tif (Array.isArray(details.modifiedFiles)) {\r\n\t\t\t\t// Modified files go into both edited and written for proper deduplication\r\n\t\t\t\tfor (const f of details.modifiedFiles) {\r\n\t\t\t\t\tfileOps.edited.add(f);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Second pass: walk from newest to oldest, adding messages until token budget\r\n\tfor (let i = entries.length - 1; i >= 0; i--) {\r\n\t\tconst entry = entries[i];\r\n\t\tconst message = getMessageFromEntry(entry);\r\n\t\tif (!message) continue;\r\n\r\n\t\t// Extract file ops from assistant messages (tool calls)\r\n\t\textractFileOpsFromMessage(message, fileOps);\r\n\r\n\t\tconst tokens = estimateTokens(message);\r\n\r\n\t\t// Check budget before adding\r\n\t\tif (tokenBudget > 0 && totalTokens + tokens > tokenBudget) {\r\n\t\t\t// If this is a summary entry, try to fit it anyway as it's important context\r\n\t\t\tif (entry.type === \"compaction\" || entry.type === \"branch_summary\") {\r\n\t\t\t\tif (totalTokens < tokenBudget * 0.9) {\r\n\t\t\t\t\tmessages.unshift(message);\r\n\t\t\t\t\ttotalTokens += tokens;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// Stop - we've hit the budget\r\n\t\t\tbreak;\r\n\t\t}\r\n\r\n\t\tmessages.unshift(message);\r\n\t\ttotalTokens += tokens;\r\n\t}\r\n\r\n\treturn { messages, fileOps, totalTokens };\r\n}\r\n\r\n// ============================================================================\r\n// Summary Generation\r\n// ============================================================================\r\n\r\nconst BRANCH_SUMMARY_PREAMBLE = `The user explored a different conversation branch before returning here.\r\nSummary of that exploration:\r\n\r\n`;\r\n\r\nconst BRANCH_SUMMARY_PROMPT = `Create a structured summary of this conversation branch for context when returning later.\r\n\r\nUse this EXACT format:\r\n\r\n## Goal\r\n[What was the user trying to accomplish in this branch?]\r\n\r\n## Constraints & Preferences\r\n- [Any constraints, preferences, or requirements mentioned]\r\n- [Or \"(none)\" if none were mentioned]\r\n\r\n## Progress\r\n### Done\r\n- [x] [Completed tasks/changes]\r\n\r\n### In Progress\r\n- [ ] [Work that was started but not finished]\r\n\r\n### Blocked\r\n- [Issues preventing progress, if any]\r\n\r\n## Key Decisions\r\n- **[Decision]**: [Brief rationale]\r\n\r\n## Next Steps\r\n1. [What should happen next to continue this work]\r\n\r\nKeep each section concise. Preserve exact file paths, function names, and error messages.`;\r\n\r\n/**\r\n * Generate a summary of abandoned branch entries.\r\n *\r\n * @param entries - Session entries to summarize (chronological order)\r\n * @param options - Generation options\r\n */\r\nexport async function generateBranchSummary(\r\n\tentries: SessionEntry[],\r\n\toptions: GenerateBranchSummaryOptions,\r\n): Promise<BranchSummaryResult> {\r\n\tconst { model, apiKey, signal, customInstructions, replaceInstructions, reserveTokens = 16384 } = options;\r\n\r\n\t// Token budget = context window minus reserved space for prompt + response\r\n\tconst contextWindow = model.contextWindow || 128000;\r\n\tconst tokenBudget = contextWindow - reserveTokens;\r\n\r\n\tconst { messages, fileOps } = prepareBranchEntries(entries, tokenBudget);\r\n\r\n\tif (messages.length === 0) {\r\n\t\treturn { summary: \"No content to summarize\" };\r\n\t}\r\n\r\n\t// Transform to LLM-compatible messages, then serialize to text\r\n\t// Serialization prevents the model from treating it as a conversation to continue\r\n\tconst llmMessages = convertToLlm(messages);\r\n\tconst conversationText = serializeConversation(llmMessages);\r\n\r\n\t// Build prompt\r\n\tlet instructions: string;\r\n\tif (replaceInstructions && customInstructions) {\r\n\t\tinstructions = customInstructions;\r\n\t} else if (customInstructions) {\r\n\t\tinstructions = `${BRANCH_SUMMARY_PROMPT}\\n\\nAdditional focus: ${customInstructions}`;\r\n\t} else {\r\n\t\tinstructions = BRANCH_SUMMARY_PROMPT;\r\n\t}\r\n\tconst promptText = `<conversation>\\n${conversationText}\\n</conversation>\\n\\n${instructions}`;\r\n\r\n\tconst summarizationMessages = [\r\n\t\t{\r\n\t\t\trole: \"user\" as const,\r\n\t\t\tcontent: [{ type: \"text\" as const, text: promptText }],\r\n\t\t\ttimestamp: Date.now(),\r\n\t\t},\r\n\t];\r\n\r\n\t// Call LLM for summarization\r\n\tconst response = await completeSimple(\r\n\t\tmodel,\r\n\t\t{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },\r\n\t\t{ apiKey, signal, maxTokens: 2048 },\r\n\t);\r\n\r\n\t// Check if aborted or errored\r\n\tif (response.stopReason === \"aborted\") {\r\n\t\treturn { aborted: true };\r\n\t}\r\n\tif (response.stopReason === \"error\") {\r\n\t\treturn { error: response.errorMessage || \"Summarization failed\" };\r\n\t}\r\n\r\n\tlet summary = response.content\r\n\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\r\n\t\t.map((c) => c.text)\r\n\t\t.join(\"\\n\");\r\n\r\n\t// Prepend preamble to provide context about the branch summary\r\n\tsummary = BRANCH_SUMMARY_PREAMBLE + summary;\r\n\r\n\t// Compute file lists and append to summary\r\n\tconst { readFiles, modifiedFiles } = computeFileLists(fileOps);\r\n\tsummary += formatFileOperations(readFiles, modifiedFiles);\r\n\r\n\treturn {\r\n\t\tsummary: summary || \"No summary generated\",\r\n\t\treadFiles,\r\n\t\tmodifiedFiles,\r\n\t};\r\n}\r\n"]}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branch summarization for tree navigation.
|
|
3
|
+
*
|
|
4
|
+
* When navigating to a different point in the session tree, this generates
|
|
5
|
+
* a summary of the branch being left so context isn't lost.
|
|
6
|
+
*/
|
|
7
|
+
import { completeSimple } from "@mariozechner/pi-ai";
|
|
8
|
+
import { convertToLlm, createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage, } from "../messages.js";
|
|
9
|
+
import { estimateTokens } from "./compaction.js";
|
|
10
|
+
import { computeFileLists, createFileOps, extractFileOpsFromMessage, formatFileOperations, SUMMARIZATION_SYSTEM_PROMPT, serializeConversation, } from "./utils.js";
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// Entry Collection
|
|
13
|
+
// ============================================================================
|
|
14
|
+
/**
|
|
15
|
+
* Collect entries that should be summarized when navigating from one position to another.
|
|
16
|
+
*
|
|
17
|
+
* Walks from oldLeafId back to the common ancestor with targetId, collecting entries
|
|
18
|
+
* along the way. Does NOT stop at compaction boundaries - those are included and their
|
|
19
|
+
* summaries become context.
|
|
20
|
+
*
|
|
21
|
+
* @param session - Session manager (read-only access)
|
|
22
|
+
* @param oldLeafId - Current position (where we're navigating from)
|
|
23
|
+
* @param targetId - Target position (where we're navigating to)
|
|
24
|
+
* @returns Entries to summarize and the common ancestor
|
|
25
|
+
*/
|
|
26
|
+
export function collectEntriesForBranchSummary(session, oldLeafId, targetId) {
|
|
27
|
+
// If no old position, nothing to summarize
|
|
28
|
+
if (!oldLeafId) {
|
|
29
|
+
return { entries: [], commonAncestorId: null };
|
|
30
|
+
}
|
|
31
|
+
// Find common ancestor (deepest node that's on both paths)
|
|
32
|
+
const oldPath = new Set(session.getBranch(oldLeafId).map((e) => e.id));
|
|
33
|
+
const targetPath = session.getBranch(targetId);
|
|
34
|
+
// targetPath is root-first, so iterate backwards to find deepest common ancestor
|
|
35
|
+
let commonAncestorId = null;
|
|
36
|
+
for (let i = targetPath.length - 1; i >= 0; i--) {
|
|
37
|
+
if (oldPath.has(targetPath[i].id)) {
|
|
38
|
+
commonAncestorId = targetPath[i].id;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Collect entries from old leaf back to common ancestor
|
|
43
|
+
const entries = [];
|
|
44
|
+
let current = oldLeafId;
|
|
45
|
+
while (current && current !== commonAncestorId) {
|
|
46
|
+
const entry = session.getEntry(current);
|
|
47
|
+
if (!entry)
|
|
48
|
+
break;
|
|
49
|
+
entries.push(entry);
|
|
50
|
+
current = entry.parentId;
|
|
51
|
+
}
|
|
52
|
+
// Reverse to get chronological order
|
|
53
|
+
entries.reverse();
|
|
54
|
+
return { entries, commonAncestorId };
|
|
55
|
+
}
|
|
56
|
+
// ============================================================================
|
|
57
|
+
// Entry to Message Conversion
|
|
58
|
+
// ============================================================================
|
|
59
|
+
/**
|
|
60
|
+
* Extract AgentMessage from a session entry.
|
|
61
|
+
* Similar to getMessageFromEntry in compaction.ts but also handles compaction entries.
|
|
62
|
+
*/
|
|
63
|
+
function getMessageFromEntry(entry) {
|
|
64
|
+
switch (entry.type) {
|
|
65
|
+
case "message":
|
|
66
|
+
// Skip tool results - context is in assistant's tool call
|
|
67
|
+
if (entry.message.role === "toolResult")
|
|
68
|
+
return undefined;
|
|
69
|
+
return entry.message;
|
|
70
|
+
case "custom_message":
|
|
71
|
+
return createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp);
|
|
72
|
+
case "branch_summary":
|
|
73
|
+
return createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);
|
|
74
|
+
case "compaction":
|
|
75
|
+
return createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);
|
|
76
|
+
// These don't contribute to conversation content
|
|
77
|
+
case "thinking_level_change":
|
|
78
|
+
case "model_change":
|
|
79
|
+
case "custom":
|
|
80
|
+
case "label":
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Prepare entries for summarization with token budget.
|
|
86
|
+
*
|
|
87
|
+
* Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.
|
|
88
|
+
* This ensures we keep the most recent context when the branch is too long.
|
|
89
|
+
*
|
|
90
|
+
* Also collects file operations from:
|
|
91
|
+
* - Tool calls in assistant messages
|
|
92
|
+
* - Existing branch_summary entries' details (for cumulative tracking)
|
|
93
|
+
*
|
|
94
|
+
* @param entries - Entries in chronological order
|
|
95
|
+
* @param tokenBudget - Maximum tokens to include (0 = no limit)
|
|
96
|
+
*/
|
|
97
|
+
export function prepareBranchEntries(entries, tokenBudget = 0) {
|
|
98
|
+
const messages = [];
|
|
99
|
+
const fileOps = createFileOps();
|
|
100
|
+
let totalTokens = 0;
|
|
101
|
+
// First pass: collect file ops from ALL entries (even if they don't fit in token budget)
|
|
102
|
+
// This ensures we capture cumulative file tracking from nested branch summaries
|
|
103
|
+
// Only extract from pi-generated summaries (fromHook !== true), not extension-generated ones
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (entry.type === "branch_summary" && !entry.fromHook && entry.details) {
|
|
106
|
+
const details = entry.details;
|
|
107
|
+
if (Array.isArray(details.readFiles)) {
|
|
108
|
+
for (const f of details.readFiles)
|
|
109
|
+
fileOps.read.add(f);
|
|
110
|
+
}
|
|
111
|
+
if (Array.isArray(details.modifiedFiles)) {
|
|
112
|
+
// Modified files go into both edited and written for proper deduplication
|
|
113
|
+
for (const f of details.modifiedFiles) {
|
|
114
|
+
fileOps.edited.add(f);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// Second pass: walk from newest to oldest, adding messages until token budget
|
|
120
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
121
|
+
const entry = entries[i];
|
|
122
|
+
const message = getMessageFromEntry(entry);
|
|
123
|
+
if (!message)
|
|
124
|
+
continue;
|
|
125
|
+
// Extract file ops from assistant messages (tool calls)
|
|
126
|
+
extractFileOpsFromMessage(message, fileOps);
|
|
127
|
+
const tokens = estimateTokens(message);
|
|
128
|
+
// Check budget before adding
|
|
129
|
+
if (tokenBudget > 0 && totalTokens + tokens > tokenBudget) {
|
|
130
|
+
// If this is a summary entry, try to fit it anyway as it's important context
|
|
131
|
+
if (entry.type === "compaction" || entry.type === "branch_summary") {
|
|
132
|
+
if (totalTokens < tokenBudget * 0.9) {
|
|
133
|
+
messages.unshift(message);
|
|
134
|
+
totalTokens += tokens;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Stop - we've hit the budget
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
messages.unshift(message);
|
|
141
|
+
totalTokens += tokens;
|
|
142
|
+
}
|
|
143
|
+
return { messages, fileOps, totalTokens };
|
|
144
|
+
}
|
|
145
|
+
// ============================================================================
|
|
146
|
+
// Summary Generation
|
|
147
|
+
// ============================================================================
|
|
148
|
+
const BRANCH_SUMMARY_PREAMBLE = `The user explored a different conversation branch before returning here.
|
|
149
|
+
Summary of that exploration:
|
|
150
|
+
|
|
151
|
+
`;
|
|
152
|
+
const BRANCH_SUMMARY_PROMPT = `Create a structured summary of this conversation branch for context when returning later.
|
|
153
|
+
|
|
154
|
+
Use this EXACT format:
|
|
155
|
+
|
|
156
|
+
## Goal
|
|
157
|
+
[What was the user trying to accomplish in this branch?]
|
|
158
|
+
|
|
159
|
+
## Constraints & Preferences
|
|
160
|
+
- [Any constraints, preferences, or requirements mentioned]
|
|
161
|
+
- [Or "(none)" if none were mentioned]
|
|
162
|
+
|
|
163
|
+
## Progress
|
|
164
|
+
### Done
|
|
165
|
+
- [x] [Completed tasks/changes]
|
|
166
|
+
|
|
167
|
+
### In Progress
|
|
168
|
+
- [ ] [Work that was started but not finished]
|
|
169
|
+
|
|
170
|
+
### Blocked
|
|
171
|
+
- [Issues preventing progress, if any]
|
|
172
|
+
|
|
173
|
+
## Key Decisions
|
|
174
|
+
- **[Decision]**: [Brief rationale]
|
|
175
|
+
|
|
176
|
+
## Next Steps
|
|
177
|
+
1. [What should happen next to continue this work]
|
|
178
|
+
|
|
179
|
+
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
180
|
+
/**
|
|
181
|
+
* Generate a summary of abandoned branch entries.
|
|
182
|
+
*
|
|
183
|
+
* @param entries - Session entries to summarize (chronological order)
|
|
184
|
+
* @param options - Generation options
|
|
185
|
+
*/
|
|
186
|
+
export async function generateBranchSummary(entries, options) {
|
|
187
|
+
const { model, apiKey, signal, customInstructions, replaceInstructions, reserveTokens = 16384 } = options;
|
|
188
|
+
// Token budget = context window minus reserved space for prompt + response
|
|
189
|
+
const contextWindow = model.contextWindow || 128000;
|
|
190
|
+
const tokenBudget = contextWindow - reserveTokens;
|
|
191
|
+
const { messages, fileOps } = prepareBranchEntries(entries, tokenBudget);
|
|
192
|
+
if (messages.length === 0) {
|
|
193
|
+
return { summary: "No content to summarize" };
|
|
194
|
+
}
|
|
195
|
+
// Transform to LLM-compatible messages, then serialize to text
|
|
196
|
+
// Serialization prevents the model from treating it as a conversation to continue
|
|
197
|
+
const llmMessages = convertToLlm(messages);
|
|
198
|
+
const conversationText = serializeConversation(llmMessages);
|
|
199
|
+
// Build prompt
|
|
200
|
+
let instructions;
|
|
201
|
+
if (replaceInstructions && customInstructions) {
|
|
202
|
+
instructions = customInstructions;
|
|
203
|
+
}
|
|
204
|
+
else if (customInstructions) {
|
|
205
|
+
instructions = `${BRANCH_SUMMARY_PROMPT}\n\nAdditional focus: ${customInstructions}`;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
instructions = BRANCH_SUMMARY_PROMPT;
|
|
209
|
+
}
|
|
210
|
+
const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${instructions}`;
|
|
211
|
+
const summarizationMessages = [
|
|
212
|
+
{
|
|
213
|
+
role: "user",
|
|
214
|
+
content: [{ type: "text", text: promptText }],
|
|
215
|
+
timestamp: Date.now(),
|
|
216
|
+
},
|
|
217
|
+
];
|
|
218
|
+
// Call LLM for summarization
|
|
219
|
+
const response = await completeSimple(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, { apiKey, signal, maxTokens: 2048 });
|
|
220
|
+
// Check if aborted or errored
|
|
221
|
+
if (response.stopReason === "aborted") {
|
|
222
|
+
return { aborted: true };
|
|
223
|
+
}
|
|
224
|
+
if (response.stopReason === "error") {
|
|
225
|
+
return { error: response.errorMessage || "Summarization failed" };
|
|
226
|
+
}
|
|
227
|
+
let summary = response.content
|
|
228
|
+
.filter((c) => c.type === "text")
|
|
229
|
+
.map((c) => c.text)
|
|
230
|
+
.join("\n");
|
|
231
|
+
// Prepend preamble to provide context about the branch summary
|
|
232
|
+
summary = BRANCH_SUMMARY_PREAMBLE + summary;
|
|
233
|
+
// Compute file lists and append to summary
|
|
234
|
+
const { readFiles, modifiedFiles } = computeFileLists(fileOps);
|
|
235
|
+
summary += formatFileOperations(readFiles, modifiedFiles);
|
|
236
|
+
return {
|
|
237
|
+
summary: summary || "No summary generated",
|
|
238
|
+
readFiles,
|
|
239
|
+
modifiedFiles,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
//# sourceMappingURL=branch-summarization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"branch-summarization.js","sourceRoot":"","sources":["../../../src/core/compaction/branch-summarization.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EACN,YAAY,EACZ,0BAA0B,EAC1B,8BAA8B,EAC9B,mBAAmB,GACnB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CA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,CAAC,OAAO;SAC5B,MAAM,CAAC,CAAC,CAAC,EAAuC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC;SACrE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;SAClB,IAAI,CAAC,IAAI,CAAC,CAAC;IAEb,+DAA+D;IAC/D,OAAO,GAAG,uBAAuB,GAAG,OAAO,CAAC;IAE5C,2CAA2C;IAC3C,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAC/D,OAAO,IAAI,oBAAoB,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;IAE1D,OAAO;QACN,OAAO,EAAE,OAAO,IAAI,sBAAsB;QAC1C,SAAS;QACT,aAAa;KACb,CAAC;AAAA,CACF","sourcesContent":["/**\r\n * Branch summarization for tree navigation.\r\n *\r\n * When navigating to a different point in the session tree, this generates\r\n * a summary of the branch being left so context isn't lost.\r\n */\r\n\r\nimport type { AgentMessage } from \"@mariozechner/pi-agent-core\";\r\nimport type { Model } from \"@mariozechner/pi-ai\";\r\nimport { completeSimple } from \"@mariozechner/pi-ai\";\r\nimport {\r\n\tconvertToLlm,\r\n\tcreateBranchSummaryMessage,\r\n\tcreateCompactionSummaryMessage,\r\n\tcreateCustomMessage,\r\n} from \"../messages.js\";\r\nimport type { ReadonlySessionManager, SessionEntry } from \"../session-manager.js\";\r\nimport { estimateTokens } from \"./compaction.js\";\r\nimport {\r\n\tcomputeFileLists,\r\n\tcreateFileOps,\r\n\textractFileOpsFromMessage,\r\n\ttype FileOperations,\r\n\tformatFileOperations,\r\n\tSUMMARIZATION_SYSTEM_PROMPT,\r\n\tserializeConversation,\r\n} from \"./utils.js\";\r\n\r\n// ============================================================================\r\n// Types\r\n// ============================================================================\r\n\r\nexport interface BranchSummaryResult {\r\n\tsummary?: string;\r\n\treadFiles?: string[];\r\n\tmodifiedFiles?: string[];\r\n\taborted?: boolean;\r\n\terror?: string;\r\n}\r\n\r\n/** Details stored in BranchSummaryEntry.details for file tracking */\r\nexport interface BranchSummaryDetails {\r\n\treadFiles: string[];\r\n\tmodifiedFiles: string[];\r\n}\r\n\r\nexport type { FileOperations } from \"./utils.js\";\r\n\r\nexport interface BranchPreparation {\r\n\t/** Messages extracted for summarization, in chronological order */\r\n\tmessages: AgentMessage[];\r\n\t/** File operations extracted from tool calls */\r\n\tfileOps: FileOperations;\r\n\t/** Total estimated tokens in messages */\r\n\ttotalTokens: number;\r\n}\r\n\r\nexport interface CollectEntriesResult {\r\n\t/** Entries to summarize, in chronological order */\r\n\tentries: SessionEntry[];\r\n\t/** Common ancestor between old and new position, if any */\r\n\tcommonAncestorId: string | null;\r\n}\r\n\r\nexport interface GenerateBranchSummaryOptions {\r\n\t/** Model to use for summarization */\r\n\tmodel: Model<any>;\r\n\t/** API key for the model */\r\n\tapiKey: string;\r\n\t/** Abort signal for cancellation */\r\n\tsignal: AbortSignal;\r\n\t/** Optional custom instructions for summarization */\r\n\tcustomInstructions?: string;\r\n\t/** If true, customInstructions replaces the default prompt instead of being appended */\r\n\treplaceInstructions?: boolean;\r\n\t/** Tokens reserved for prompt + LLM response (default 16384) */\r\n\treserveTokens?: number;\r\n}\r\n\r\n// ============================================================================\r\n// Entry Collection\r\n// ============================================================================\r\n\r\n/**\r\n * Collect entries that should be summarized when navigating from one position to another.\r\n *\r\n * Walks from oldLeafId back to the common ancestor with targetId, collecting entries\r\n * along the way. Does NOT stop at compaction boundaries - those are included and their\r\n * summaries become context.\r\n *\r\n * @param session - Session manager (read-only access)\r\n * @param oldLeafId - Current position (where we're navigating from)\r\n * @param targetId - Target position (where we're navigating to)\r\n * @returns Entries to summarize and the common ancestor\r\n */\r\nexport function collectEntriesForBranchSummary(\r\n\tsession: ReadonlySessionManager,\r\n\toldLeafId: string | null,\r\n\ttargetId: string,\r\n): CollectEntriesResult {\r\n\t// If no old position, nothing to summarize\r\n\tif (!oldLeafId) {\r\n\t\treturn { entries: [], commonAncestorId: null };\r\n\t}\r\n\r\n\t// Find common ancestor (deepest node that's on both paths)\r\n\tconst oldPath = new Set(session.getBranch(oldLeafId).map((e) => e.id));\r\n\tconst targetPath = session.getBranch(targetId);\r\n\r\n\t// targetPath is root-first, so iterate backwards to find deepest common ancestor\r\n\tlet commonAncestorId: string | null = null;\r\n\tfor (let i = targetPath.length - 1; i >= 0; i--) {\r\n\t\tif (oldPath.has(targetPath[i].id)) {\r\n\t\t\tcommonAncestorId = targetPath[i].id;\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\r\n\t// Collect entries from old leaf back to common ancestor\r\n\tconst entries: SessionEntry[] = [];\r\n\tlet current: string | null = oldLeafId;\r\n\r\n\twhile (current && current !== commonAncestorId) {\r\n\t\tconst entry = session.getEntry(current);\r\n\t\tif (!entry) break;\r\n\t\tentries.push(entry);\r\n\t\tcurrent = entry.parentId;\r\n\t}\r\n\r\n\t// Reverse to get chronological order\r\n\tentries.reverse();\r\n\r\n\treturn { entries, commonAncestorId };\r\n}\r\n\r\n// ============================================================================\r\n// Entry to Message Conversion\r\n// ============================================================================\r\n\r\n/**\r\n * Extract AgentMessage from a session entry.\r\n * Similar to getMessageFromEntry in compaction.ts but also handles compaction entries.\r\n */\r\nfunction getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\r\n\tswitch (entry.type) {\r\n\t\tcase \"message\":\r\n\t\t\t// Skip tool results - context is in assistant's tool call\r\n\t\t\tif (entry.message.role === \"toolResult\") return undefined;\r\n\t\t\treturn entry.message;\r\n\r\n\t\tcase \"custom_message\":\r\n\t\t\treturn createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp);\r\n\r\n\t\tcase \"branch_summary\":\r\n\t\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\r\n\r\n\t\tcase \"compaction\":\r\n\t\t\treturn createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);\r\n\r\n\t\t// These don't contribute to conversation content\r\n\t\tcase \"thinking_level_change\":\r\n\t\tcase \"model_change\":\r\n\t\tcase \"custom\":\r\n\t\tcase \"label\":\r\n\t\t\treturn undefined;\r\n\t}\r\n}\r\n\r\n/**\r\n * Prepare entries for summarization with token budget.\r\n *\r\n * Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.\r\n * This ensures we keep the most recent context when the branch is too long.\r\n *\r\n * Also collects file operations from:\r\n * - Tool calls in assistant messages\r\n * - Existing branch_summary entries' details (for cumulative tracking)\r\n *\r\n * @param entries - Entries in chronological order\r\n * @param tokenBudget - Maximum tokens to include (0 = no limit)\r\n */\r\nexport function prepareBranchEntries(entries: SessionEntry[], tokenBudget: number = 0): BranchPreparation {\r\n\tconst messages: AgentMessage[] = [];\r\n\tconst fileOps = createFileOps();\r\n\tlet totalTokens = 0;\r\n\r\n\t// First pass: collect file ops from ALL entries (even if they don't fit in token budget)\r\n\t// This ensures we capture cumulative file tracking from nested branch summaries\r\n\t// Only extract from pi-generated summaries (fromHook !== true), not extension-generated ones\r\n\tfor (const entry of entries) {\r\n\t\tif (entry.type === \"branch_summary\" && !entry.fromHook && entry.details) {\r\n\t\t\tconst details = entry.details as BranchSummaryDetails;\r\n\t\t\tif (Array.isArray(details.readFiles)) {\r\n\t\t\t\tfor (const f of details.readFiles) fileOps.read.add(f);\r\n\t\t\t}\r\n\t\t\tif (Array.isArray(details.modifiedFiles)) {\r\n\t\t\t\t// Modified files go into both edited and written for proper deduplication\r\n\t\t\t\tfor (const f of details.modifiedFiles) {\r\n\t\t\t\t\tfileOps.edited.add(f);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Second pass: walk from newest to oldest, adding messages until token budget\r\n\tfor (let i = entries.length - 1; i >= 0; i--) {\r\n\t\tconst entry = entries[i];\r\n\t\tconst message = getMessageFromEntry(entry);\r\n\t\tif (!message) continue;\r\n\r\n\t\t// Extract file ops from assistant messages (tool calls)\r\n\t\textractFileOpsFromMessage(message, fileOps);\r\n\r\n\t\tconst tokens = estimateTokens(message);\r\n\r\n\t\t// Check budget before adding\r\n\t\tif (tokenBudget > 0 && totalTokens + tokens > tokenBudget) {\r\n\t\t\t// If this is a summary entry, try to fit it anyway as it's important context\r\n\t\t\tif (entry.type === \"compaction\" || entry.type === \"branch_summary\") {\r\n\t\t\t\tif (totalTokens < tokenBudget * 0.9) {\r\n\t\t\t\t\tmessages.unshift(message);\r\n\t\t\t\t\ttotalTokens += tokens;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// Stop - we've hit the budget\r\n\t\t\tbreak;\r\n\t\t}\r\n\r\n\t\tmessages.unshift(message);\r\n\t\ttotalTokens += tokens;\r\n\t}\r\n\r\n\treturn { messages, fileOps, totalTokens };\r\n}\r\n\r\n// ============================================================================\r\n// Summary Generation\r\n// ============================================================================\r\n\r\nconst BRANCH_SUMMARY_PREAMBLE = `The user explored a different conversation branch before returning here.\r\nSummary of that exploration:\r\n\r\n`;\r\n\r\nconst BRANCH_SUMMARY_PROMPT = `Create a structured summary of this conversation branch for context when returning later.\r\n\r\nUse this EXACT format:\r\n\r\n## Goal\r\n[What was the user trying to accomplish in this branch?]\r\n\r\n## Constraints & Preferences\r\n- [Any constraints, preferences, or requirements mentioned]\r\n- [Or \"(none)\" if none were mentioned]\r\n\r\n## Progress\r\n### Done\r\n- [x] [Completed tasks/changes]\r\n\r\n### In Progress\r\n- [ ] [Work that was started but not finished]\r\n\r\n### Blocked\r\n- [Issues preventing progress, if any]\r\n\r\n## Key Decisions\r\n- **[Decision]**: [Brief rationale]\r\n\r\n## Next Steps\r\n1. [What should happen next to continue this work]\r\n\r\nKeep each section concise. Preserve exact file paths, function names, and error messages.`;\r\n\r\n/**\r\n * Generate a summary of abandoned branch entries.\r\n *\r\n * @param entries - Session entries to summarize (chronological order)\r\n * @param options - Generation options\r\n */\r\nexport async function generateBranchSummary(\r\n\tentries: SessionEntry[],\r\n\toptions: GenerateBranchSummaryOptions,\r\n): Promise<BranchSummaryResult> {\r\n\tconst { model, apiKey, signal, customInstructions, replaceInstructions, reserveTokens = 16384 } = options;\r\n\r\n\t// Token budget = context window minus reserved space for prompt + response\r\n\tconst contextWindow = model.contextWindow || 128000;\r\n\tconst tokenBudget = contextWindow - reserveTokens;\r\n\r\n\tconst { messages, fileOps } = prepareBranchEntries(entries, tokenBudget);\r\n\r\n\tif (messages.length === 0) {\r\n\t\treturn { summary: \"No content to summarize\" };\r\n\t}\r\n\r\n\t// Transform to LLM-compatible messages, then serialize to text\r\n\t// Serialization prevents the model from treating it as a conversation to continue\r\n\tconst llmMessages = convertToLlm(messages);\r\n\tconst conversationText = serializeConversation(llmMessages);\r\n\r\n\t// Build prompt\r\n\tlet instructions: string;\r\n\tif (replaceInstructions && customInstructions) {\r\n\t\tinstructions = customInstructions;\r\n\t} else if (customInstructions) {\r\n\t\tinstructions = `${BRANCH_SUMMARY_PROMPT}\\n\\nAdditional focus: ${customInstructions}`;\r\n\t} else {\r\n\t\tinstructions = BRANCH_SUMMARY_PROMPT;\r\n\t}\r\n\tconst promptText = `<conversation>\\n${conversationText}\\n</conversation>\\n\\n${instructions}`;\r\n\r\n\tconst summarizationMessages = [\r\n\t\t{\r\n\t\t\trole: \"user\" as const,\r\n\t\t\tcontent: [{ type: \"text\" as const, text: promptText }],\r\n\t\t\ttimestamp: Date.now(),\r\n\t\t},\r\n\t];\r\n\r\n\t// Call LLM for summarization\r\n\tconst response = await completeSimple(\r\n\t\tmodel,\r\n\t\t{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },\r\n\t\t{ apiKey, signal, maxTokens: 2048 },\r\n\t);\r\n\r\n\t// Check if aborted or errored\r\n\tif (response.stopReason === \"aborted\") {\r\n\t\treturn { aborted: true };\r\n\t}\r\n\tif (response.stopReason === \"error\") {\r\n\t\treturn { error: response.errorMessage || \"Summarization failed\" };\r\n\t}\r\n\r\n\tlet summary = response.content\r\n\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\r\n\t\t.map((c) => c.text)\r\n\t\t.join(\"\\n\");\r\n\r\n\t// Prepend preamble to provide context about the branch summary\r\n\tsummary = BRANCH_SUMMARY_PREAMBLE + summary;\r\n\r\n\t// Compute file lists and append to summary\r\n\tconst { readFiles, modifiedFiles } = computeFileLists(fileOps);\r\n\tsummary += formatFileOperations(readFiles, modifiedFiles);\r\n\r\n\treturn {\r\n\t\tsummary: summary || \"No summary generated\",\r\n\t\treadFiles,\r\n\t\tmodifiedFiles,\r\n\t};\r\n}\r\n"]}
|