@tomsun28/pizza 0.0.9 → 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/README.md +20 -143
- package/README.zh-CN.md +36 -0
- package/dist/bun/cli.d.ts +2 -0
- package/dist/bun/cli.js +6 -0
- package/dist/bun/register-bedrock.d.ts +1 -0
- package/dist/bun/register-bedrock.js +3 -0
- package/dist/cli/args.d.ts +48 -0
- package/dist/cli/args.js +330 -0
- package/dist/cli/config-selector.d.ts +13 -0
- package/dist/cli/config-selector.js +30 -0
- package/dist/cli/file-processor.d.ts +14 -0
- package/dist/cli/file-processor.js +82 -0
- package/dist/cli/initial-message.d.ts +17 -0
- package/dist/cli/initial-message.js +21 -0
- package/dist/cli/list-models.d.ts +8 -0
- package/dist/cli/list-models.js +96 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +68 -0
- package/dist/config.d.ts +79 -0
- package/dist/config.js +223 -0
- package/dist/core/agent/index.d.ts +6 -0
- package/dist/core/agent/index.js +6 -0
- package/dist/core/agent/types.d.ts +202 -0
- package/dist/core/agent/types.js +51 -0
- package/dist/core/auth-storage.d.ts +135 -0
- package/dist/core/auth-storage.js +417 -0
- package/dist/core/bash-executor.d.ts +31 -0
- package/dist/core/bash-executor.js +107 -0
- package/dist/core/compaction/branch-summarization.d.ts +87 -0
- package/dist/core/compaction/branch-summarization.js +242 -0
- package/dist/core/compaction/compaction-engine.d.ts +40 -0
- package/dist/core/compaction/compaction-engine.js +292 -0
- package/dist/core/compaction/compaction.d.ts +106 -0
- package/dist/core/compaction/compaction.js +295 -0
- package/dist/core/compaction/index.d.ts +7 -0
- package/dist/core/compaction/index.js +7 -0
- package/dist/core/compaction/utils.d.ts +37 -0
- package/dist/core/compaction/utils.js +152 -0
- package/dist/core/defaults.d.ts +2 -0
- package/dist/core/defaults.js +1 -0
- package/dist/core/diagnostics.d.ts +14 -0
- package/dist/core/diagnostics.js +1 -0
- package/dist/core/event-bus.d.ts +8 -0
- package/dist/core/event-bus.js +24 -0
- package/dist/core/event-store/events.d.ts +465 -0
- package/dist/core/event-store/events.js +6 -0
- package/dist/core/event-store/index.d.ts +10 -0
- package/dist/core/event-store/index.js +10 -0
- package/dist/core/event-store/sqlite-store.d.ts +27 -0
- package/dist/core/event-store/sqlite-store.js +275 -0
- package/dist/core/event-store/store.d.ts +91 -0
- package/dist/core/event-store/store.js +7 -0
- package/dist/core/event-store/thread-scoped-store.d.ts +29 -0
- package/dist/core/event-store/thread-scoped-store.js +50 -0
- package/dist/core/event-store/types.d.ts +70 -0
- package/dist/core/event-store/types.js +7 -0
- package/dist/core/event-store/workspace.d.ts +37 -0
- package/dist/core/event-store/workspace.js +71 -0
- package/dist/core/exec.d.ts +28 -0
- package/dist/core/exec.js +74 -0
- package/dist/core/export-html/ansi-to-html.d.ts +21 -0
- package/dist/core/export-html/ansi-to-html.js +248 -0
- package/dist/core/export-html/index.d.ts +29 -0
- package/dist/core/export-html/index.js +186 -0
- package/dist/core/export-html/template.css +1017 -0
- package/dist/core/export-html/template.html +55 -0
- package/dist/core/export-html/template.js +1742 -0
- package/dist/core/export-html/tool-renderer.d.ts +39 -0
- package/dist/core/export-html/tool-renderer.js +94 -0
- package/dist/core/export-html/vendor/highlight.min.js +1213 -0
- package/dist/core/export-html/vendor/marked.min.js +6 -0
- package/dist/core/extensions/index.d.ts +12 -0
- package/dist/core/extensions/index.js +9 -0
- package/dist/core/extensions/loader.d.ts +24 -0
- package/dist/core/extensions/loader.js +492 -0
- package/dist/core/extensions/runner.d.ts +167 -0
- package/dist/core/extensions/runner.js +898 -0
- package/dist/core/extensions/session-context.d.ts +67 -0
- package/dist/core/extensions/session-context.js +272 -0
- package/dist/core/extensions/types.d.ts +1147 -0
- package/dist/core/extensions/types.js +44 -0
- package/dist/core/extensions/wrapper.d.ts +19 -0
- package/dist/core/extensions/wrapper.js +21 -0
- package/dist/core/footer-data-provider.d.ts +47 -0
- package/dist/core/footer-data-provider.js +321 -0
- package/dist/core/goal/index.d.ts +3 -0
- package/dist/core/goal/index.js +1 -0
- package/dist/core/goal/scheduler.d.ts +83 -0
- package/dist/core/goal/scheduler.js +154 -0
- package/dist/core/goal/types.d.ts +182 -0
- package/dist/core/goal/types.js +11 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.js +8 -0
- package/dist/core/intent/classifier.d.ts +53 -0
- package/dist/core/intent/classifier.js +353 -0
- package/dist/core/intent/index.d.ts +8 -0
- package/dist/core/intent/index.js +8 -0
- package/dist/core/intent/tool-adapter.d.ts +31 -0
- package/dist/core/intent/tool-adapter.js +168 -0
- package/dist/core/intent/types.d.ts +71 -0
- package/dist/core/intent/types.js +6 -0
- package/dist/core/keybindings.d.ts +297 -0
- package/dist/core/keybindings.js +250 -0
- package/dist/core/messages.d.ts +30 -0
- package/dist/core/messages.js +134 -0
- package/dist/core/model-registry.d.ts +131 -0
- package/dist/core/model-registry.js +634 -0
- package/dist/core/model-resolver.d.ts +109 -0
- package/dist/core/model-resolver.js +486 -0
- package/dist/core/output-guard.d.ts +5 -0
- package/dist/core/output-guard.js +58 -0
- package/dist/core/package-manager.d.ts +196 -0
- package/dist/core/package-manager.js +1873 -0
- package/dist/core/projection/boundary-inferrer.d.ts +58 -0
- package/dist/core/projection/boundary-inferrer.js +122 -0
- package/dist/core/projection/event-to-message.d.ts +35 -0
- package/dist/core/projection/event-to-message.js +253 -0
- package/dist/core/projection/goal-projection.d.ts +67 -0
- package/dist/core/projection/goal-projection.js +339 -0
- package/dist/core/projection/index.d.ts +13 -0
- package/dist/core/projection/index.js +13 -0
- package/dist/core/projection/session-manager.d.ts +100 -0
- package/dist/core/projection/session-manager.js +331 -0
- package/dist/core/projection/session-projection.d.ts +58 -0
- package/dist/core/projection/session-projection.js +257 -0
- package/dist/core/projection/task-history.d.ts +13 -0
- package/dist/core/projection/task-history.js +61 -0
- package/dist/core/projection/timeline-projection.d.ts +82 -0
- package/dist/core/projection/timeline-projection.js +276 -0
- package/dist/core/projection/types.d.ts +69 -0
- package/dist/core/projection/types.js +6 -0
- package/dist/core/prompt-templates.d.ts +51 -0
- package/dist/core/prompt-templates.js +249 -0
- package/dist/core/resolve-config-value.d.ts +22 -0
- package/dist/core/resolve-config-value.js +125 -0
- package/dist/core/resource-loader.d.ts +193 -0
- package/dist/core/resource-loader.js +685 -0
- package/dist/core/runtime/ai-client.d.ts +12 -0
- package/dist/core/runtime/ai-client.js +153 -0
- package/dist/core/runtime/checkpoint.d.ts +12 -0
- package/dist/core/runtime/checkpoint.js +78 -0
- package/dist/core/runtime/index.d.ts +12 -0
- package/dist/core/runtime/index.js +12 -0
- package/dist/core/runtime/llm-types.d.ts +99 -0
- package/dist/core/runtime/llm-types.js +6 -0
- package/dist/core/runtime/local-runtime.d.ts +23 -0
- package/dist/core/runtime/local-runtime.js +59 -0
- package/dist/core/runtime/policies.d.ts +72 -0
- package/dist/core/runtime/policies.js +37 -0
- package/dist/core/runtime/reactor.d.ts +213 -0
- package/dist/core/runtime/reactor.js +992 -0
- package/dist/core/runtime/runtime.d.ts +191 -0
- package/dist/core/runtime/runtime.js +449 -0
- package/dist/core/runtime/types.d.ts +42 -0
- package/dist/core/runtime/types.js +1 -0
- package/dist/core/sdk.d.ts +12 -0
- package/dist/core/sdk.js +10 -0
- package/dist/core/session-context-builder.d.ts +9 -0
- package/dist/core/session-context-builder.js +87 -0
- package/dist/core/session-cwd.d.ts +18 -0
- package/dist/core/session-cwd.js +36 -0
- package/dist/core/session-facade-factory.d.ts +79 -0
- package/dist/core/session-facade-factory.js +460 -0
- package/dist/core/session-facade.d.ts +57 -0
- package/dist/core/session-facade.js +91 -0
- package/dist/core/session-listing.d.ts +30 -0
- package/dist/core/session-listing.js +168 -0
- package/dist/core/session-reader.d.ts +19 -0
- package/dist/core/session-reader.js +101 -0
- package/dist/core/session-ref.d.ts +7 -0
- package/dist/core/session-ref.js +25 -0
- package/dist/core/session-services.d.ts +51 -0
- package/dist/core/session-services.js +90 -0
- package/dist/core/session-stats.d.ts +43 -0
- package/dist/core/session-stats.js +41 -0
- package/dist/core/settings-manager.d.ts +236 -0
- package/dist/core/settings-manager.js +706 -0
- package/dist/core/skill-block-parser.d.ts +12 -0
- package/dist/core/skill-block-parser.js +15 -0
- package/dist/core/skills.d.ts +59 -0
- package/dist/core/skills.js +408 -0
- package/dist/core/slash-commands.d.ts +13 -0
- package/dist/core/slash-commands.js +17 -0
- package/dist/core/source-info.d.ts +17 -0
- package/dist/core/source-info.js +18 -0
- package/dist/core/system-prompt.d.ts +27 -0
- package/dist/core/system-prompt.js +179 -0
- package/dist/core/timings.d.ts +7 -0
- package/dist/core/timings.js +30 -0
- package/dist/core/tools/bash.d.ts +95 -0
- package/dist/core/tools/bash.js +513 -0
- package/dist/core/tools/builtin-commands.d.ts +75 -0
- package/dist/core/tools/builtin-commands.js +503 -0
- package/dist/core/tools/compat-commands.d.ts +12 -0
- package/dist/core/tools/compat-commands.js +1280 -0
- package/dist/core/tools/edit-diff.d.ts +59 -0
- package/dist/core/tools/edit-diff.js +263 -0
- package/dist/core/tools/edit.d.ts +49 -0
- package/dist/core/tools/edit.js +340 -0
- package/dist/core/tools/file-mutation-queue.d.ts +5 -0
- package/dist/core/tools/file-mutation-queue.js +36 -0
- package/dist/core/tools/find.d.ts +34 -0
- package/dist/core/tools/find.js +459 -0
- package/dist/core/tools/grep.d.ts +36 -0
- package/dist/core/tools/grep.js +422 -0
- package/dist/core/tools/index.d.ts +70 -0
- package/dist/core/tools/index.js +129 -0
- package/dist/core/tools/line-anchors.d.ts +33 -0
- package/dist/core/tools/line-anchors.js +151 -0
- package/dist/core/tools/ls.d.ts +36 -0
- package/dist/core/tools/ls.js +268 -0
- package/dist/core/tools/path-utils.d.ts +7 -0
- package/dist/core/tools/path-utils.js +80 -0
- package/dist/core/tools/read.d.ts +35 -0
- package/dist/core/tools/read.js +244 -0
- package/dist/core/tools/render-utils.d.ts +20 -0
- package/dist/core/tools/render-utils.js +48 -0
- package/dist/core/tools/session-split.d.ts +9 -0
- package/dist/core/tools/session-split.js +77 -0
- package/dist/core/tools/tool-definition-wrapper.d.ts +13 -0
- package/dist/core/tools/tool-definition-wrapper.js +32 -0
- package/dist/core/tools/truncate.d.ts +69 -0
- package/dist/core/tools/truncate.js +204 -0
- package/dist/core/tools/write.d.ts +25 -0
- package/dist/core/tools/write.js +211 -0
- package/dist/core/types/session-types.d.ts +123 -0
- package/dist/core/types/session-types.js +8 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +38 -0
- package/dist/main.d.ts +11 -2
- package/dist/main.js +577 -149
- package/dist/modes/event-mapper.d.ts +115 -0
- package/dist/modes/event-mapper.js +200 -0
- package/dist/modes/gui/server.d.ts +14 -0
- package/dist/modes/gui/server.js +1032 -0
- package/dist/modes/index.d.ts +10 -0
- package/dist/modes/index.js +9 -0
- package/dist/modes/interactive/assets/clankolas.png +0 -0
- package/dist/modes/interactive/components/armin.d.ts +33 -0
- package/dist/modes/interactive/components/armin.js +328 -0
- package/dist/modes/interactive/components/assistant-message.d.ts +19 -0
- package/dist/modes/interactive/components/assistant-message.js +115 -0
- package/dist/modes/interactive/components/bash-execution.d.ts +33 -0
- package/dist/modes/interactive/components/bash-execution.js +169 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts +15 -0
- package/dist/modes/interactive/components/bordered-loader.js +50 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/branch-summary-message.js +41 -0
- package/dist/modes/interactive/components/brand-header.d.ts +33 -0
- package/dist/modes/interactive/components/brand-header.js +235 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/compaction-summary-message.js +42 -0
- package/dist/modes/interactive/components/config-selector.d.ts +70 -0
- package/dist/modes/interactive/components/config-selector.js +469 -0
- package/dist/modes/interactive/components/countdown-timer.d.ts +13 -0
- package/dist/modes/interactive/components/countdown-timer.js +27 -0
- package/dist/modes/interactive/components/custom-editor.d.ts +27 -0
- package/dist/modes/interactive/components/custom-editor.js +94 -0
- package/dist/modes/interactive/components/custom-message.d.ts +19 -0
- package/dist/modes/interactive/components/custom-message.js +73 -0
- package/dist/modes/interactive/components/daxnuts.d.ts +22 -0
- package/dist/modes/interactive/components/daxnuts.js +137 -0
- package/dist/modes/interactive/components/diff.d.ts +11 -0
- package/dist/modes/interactive/components/diff.js +132 -0
- package/dist/modes/interactive/components/dynamic-border.d.ts +14 -0
- package/dist/modes/interactive/components/dynamic-border.js +19 -0
- package/dist/modes/interactive/components/earendil-announcement.d.ts +4 -0
- package/dist/modes/interactive/components/earendil-announcement.js +39 -0
- package/dist/modes/interactive/components/extension-editor.d.ts +19 -0
- package/dist/modes/interactive/components/extension-editor.js +105 -0
- package/dist/modes/interactive/components/extension-input.d.ts +22 -0
- package/dist/modes/interactive/components/extension-input.js +54 -0
- package/dist/modes/interactive/components/extension-selector.d.ts +23 -0
- package/dist/modes/interactive/components/extension-selector.js +70 -0
- package/dist/modes/interactive/components/footer.d.ts +55 -0
- package/dist/modes/interactive/components/footer.js +207 -0
- package/dist/modes/interactive/components/index.d.ts +31 -0
- package/dist/modes/interactive/components/index.js +32 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts +7 -0
- package/dist/modes/interactive/components/keybinding-hints.js +21 -0
- package/dist/modes/interactive/components/login-dialog.d.ts +41 -0
- package/dist/modes/interactive/components/login-dialog.js +138 -0
- package/dist/modes/interactive/components/model-selector.d.ts +32 -0
- package/dist/modes/interactive/components/model-selector.js +209 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts +18 -0
- package/dist/modes/interactive/components/oauth-selector.js +91 -0
- package/dist/modes/interactive/components/settings-selector.d.ts +57 -0
- package/dist/modes/interactive/components/settings-selector.js +299 -0
- package/dist/modes/interactive/components/show-images-selector.d.ts +9 -0
- package/dist/modes/interactive/components/show-images-selector.js +37 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +16 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +44 -0
- package/dist/modes/interactive/components/theme-selector.d.ts +10 -0
- package/dist/modes/interactive/components/theme-selector.js +47 -0
- package/dist/modes/interactive/components/thinking-selector.d.ts +10 -0
- package/dist/modes/interactive/components/thinking-selector.js +50 -0
- package/dist/modes/interactive/components/timeline-view.d.ts +31 -0
- package/dist/modes/interactive/components/timeline-view.js +81 -0
- package/dist/modes/interactive/components/tool-execution.d.ts +64 -0
- package/dist/modes/interactive/components/tool-execution.js +305 -0
- package/dist/modes/interactive/components/tree-selector.d.ts +88 -0
- package/dist/modes/interactive/components/tree-selector.js +1072 -0
- package/dist/modes/interactive/components/user-message-selector.d.ts +29 -0
- package/dist/modes/interactive/components/user-message-selector.js +110 -0
- package/dist/modes/interactive/components/user-message.d.ts +9 -0
- package/dist/modes/interactive/components/user-message.js +27 -0
- package/dist/modes/interactive/components/visual-truncate.d.ts +23 -0
- package/dist/modes/interactive/components/visual-truncate.js +32 -0
- package/dist/modes/interactive/interactive-mode.d.ts +394 -0
- package/dist/modes/interactive/interactive-mode.js +4116 -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 +80 -0
- package/dist/modes/interactive/theme/theme.js +973 -0
- package/dist/modes/print-mode.d.ts +27 -0
- package/dist/modes/print-mode.js +102 -0
- package/dist/modes/rpc/jsonl.d.ts +16 -0
- package/dist/modes/rpc/jsonl.js +48 -0
- package/dist/modes/rpc/rpc-client.d.ts +219 -0
- package/dist/modes/rpc/rpc-client.js +405 -0
- package/dist/modes/rpc/rpc-mode.d.ts +19 -0
- package/dist/modes/rpc/rpc-mode.js +381 -0
- package/dist/modes/rpc/rpc-types.d.ts +406 -0
- package/dist/modes/rpc/rpc-types.js +7 -0
- package/dist/package-manager-cli.d.ts +3 -0
- package/dist/package-manager-cli.js +233 -0
- package/dist/utils/changelog.d.ts +20 -0
- package/dist/utils/changelog.js +86 -0
- package/dist/utils/child-process.d.ts +10 -0
- package/dist/utils/child-process.js +77 -0
- package/dist/utils/clipboard-image.d.ts +10 -0
- package/dist/utils/clipboard-image.js +244 -0
- package/dist/utils/clipboard-native.d.ts +7 -0
- package/dist/utils/clipboard-native.js +13 -0
- package/dist/utils/clipboard.d.ts +1 -0
- package/dist/utils/clipboard.js +77 -0
- package/dist/utils/exif-orientation.d.ts +4 -0
- package/dist/utils/exif-orientation.js +157 -0
- package/dist/utils/frontmatter.d.ts +7 -0
- package/dist/utils/frontmatter.js +25 -0
- package/dist/utils/git.d.ts +25 -0
- package/dist/utils/git.js +162 -0
- package/dist/utils/image-convert.d.ts +8 -0
- package/dist/utils/image-convert.js +38 -0
- package/dist/utils/image-resize.d.ts +35 -0
- package/dist/utils/image-resize.js +136 -0
- package/dist/utils/mime.d.ts +1 -0
- package/dist/utils/mime.js +25 -0
- package/dist/utils/path-shims.d.ts +1 -0
- package/dist/utils/path-shims.js +90 -0
- package/dist/utils/paths.d.ts +6 -0
- package/dist/utils/paths.js +18 -0
- package/dist/utils/photon.d.ts +20 -0
- package/dist/utils/photon.js +120 -0
- package/dist/utils/shell.d.ts +29 -0
- package/dist/utils/shell.js +189 -0
- package/dist/utils/sleep.d.ts +4 -0
- package/dist/utils/sleep.js +16 -0
- package/dist/utils/tools-manager.d.ts +3 -0
- package/dist/utils/tools-manager.js +101 -0
- package/dist/vendor/bin/darwin-arm64/fd +0 -0
- package/dist/vendor/bin/darwin-arm64/rg +0 -0
- package/dist/vendor/bin/darwin-x64/fd +0 -0
- package/dist/vendor/bin/darwin-x64/rg +0 -0
- package/dist/vendor/bin/linux-arm64/fd +0 -0
- package/dist/vendor/bin/linux-arm64/rg +0 -0
- package/dist/vendor/bin/linux-x64/fd +0 -0
- package/dist/vendor/bin/linux-x64/rg +0 -0
- package/dist/vendor/bin/win32-arm64/fd.exe +0 -0
- package/dist/vendor/bin/win32-arm64/rg.exe +0 -0
- package/dist/vendor/bin/win32-x64/fd.exe +0 -0
- package/dist/vendor/bin/win32-x64/rg.exe +0 -0
- package/package.json +90 -26
- package/dist/extension/core.d.ts +0 -69
- package/dist/extension/core.js +0 -270
- package/dist/extension/core.js.map +0 -1
- package/dist/extension/index.d.ts +0 -25
- package/dist/extension/index.js +0 -303
- package/dist/extension/index.js.map +0 -1
- package/dist/main.js.map +0 -1
- package/themes/pizza.json +0 -87
|
@@ -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 "@earendil-works/pi-ai/compat";
|
|
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
|
+
case "session_info":
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Prepare entries for summarization with token budget.
|
|
87
|
+
*
|
|
88
|
+
* Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.
|
|
89
|
+
* This ensures we keep the most recent context when the branch is too long.
|
|
90
|
+
*
|
|
91
|
+
* Also collects file operations from:
|
|
92
|
+
* - Tool calls in assistant messages
|
|
93
|
+
* - Existing branch_summary entries' details (for cumulative tracking)
|
|
94
|
+
*
|
|
95
|
+
* @param entries - Entries in chronological order
|
|
96
|
+
* @param tokenBudget - Maximum tokens to include (0 = no limit)
|
|
97
|
+
*/
|
|
98
|
+
export function prepareBranchEntries(entries, tokenBudget = 0) {
|
|
99
|
+
const messages = [];
|
|
100
|
+
const fileOps = createFileOps();
|
|
101
|
+
let totalTokens = 0;
|
|
102
|
+
// First pass: collect file ops from ALL entries (even if they don't fit in token budget)
|
|
103
|
+
// This ensures we capture cumulative file tracking from nested branch summaries
|
|
104
|
+
// Only extract from Pizza-generated summaries (fromHook !== true), not extension-generated ones
|
|
105
|
+
for (const entry of entries) {
|
|
106
|
+
if (entry.type === "branch_summary" && !entry.fromHook && entry.details) {
|
|
107
|
+
const details = entry.details;
|
|
108
|
+
if (Array.isArray(details.readFiles)) {
|
|
109
|
+
for (const f of details.readFiles)
|
|
110
|
+
fileOps.read.add(f);
|
|
111
|
+
}
|
|
112
|
+
if (Array.isArray(details.modifiedFiles)) {
|
|
113
|
+
// Modified files go into both edited and written for proper deduplication
|
|
114
|
+
for (const f of details.modifiedFiles) {
|
|
115
|
+
fileOps.edited.add(f);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Second pass: walk from newest to oldest, adding messages until token budget
|
|
121
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
122
|
+
const entry = entries[i];
|
|
123
|
+
const message = getMessageFromEntry(entry);
|
|
124
|
+
if (!message)
|
|
125
|
+
continue;
|
|
126
|
+
// Extract file ops from assistant messages (tool calls)
|
|
127
|
+
extractFileOpsFromMessage(message, fileOps);
|
|
128
|
+
const tokens = estimateTokens(message);
|
|
129
|
+
// Check budget before adding
|
|
130
|
+
if (tokenBudget > 0 && totalTokens + tokens > tokenBudget) {
|
|
131
|
+
// If this is a summary entry, try to fit it anyway as it's important context
|
|
132
|
+
if (entry.type === "compaction" || entry.type === "branch_summary") {
|
|
133
|
+
if (totalTokens < tokenBudget * 0.9) {
|
|
134
|
+
messages.unshift(message);
|
|
135
|
+
totalTokens += tokens;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Stop - we've hit the budget
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
messages.unshift(message);
|
|
142
|
+
totalTokens += tokens;
|
|
143
|
+
}
|
|
144
|
+
return { messages, fileOps, totalTokens };
|
|
145
|
+
}
|
|
146
|
+
// ============================================================================
|
|
147
|
+
// Summary Generation
|
|
148
|
+
// ============================================================================
|
|
149
|
+
const BRANCH_SUMMARY_PREAMBLE = `The user explored a different conversation branch before returning here.
|
|
150
|
+
Summary of that exploration:
|
|
151
|
+
|
|
152
|
+
`;
|
|
153
|
+
const BRANCH_SUMMARY_PROMPT = `Create a structured summary of this conversation branch for context when returning later.
|
|
154
|
+
|
|
155
|
+
Use this EXACT format:
|
|
156
|
+
|
|
157
|
+
## Goal
|
|
158
|
+
[What was the user trying to accomplish in this branch?]
|
|
159
|
+
|
|
160
|
+
## Constraints & Preferences
|
|
161
|
+
- [Any constraints, preferences, or requirements mentioned]
|
|
162
|
+
- [Or "(none)" if none were mentioned]
|
|
163
|
+
|
|
164
|
+
## Progress
|
|
165
|
+
### Done
|
|
166
|
+
- [x] [Completed tasks/changes]
|
|
167
|
+
|
|
168
|
+
### In Progress
|
|
169
|
+
- [ ] [Work that was started but not finished]
|
|
170
|
+
|
|
171
|
+
### Blocked
|
|
172
|
+
- [Issues preventing progress, if any]
|
|
173
|
+
|
|
174
|
+
## Key Decisions
|
|
175
|
+
- **[Decision]**: [Brief rationale]
|
|
176
|
+
|
|
177
|
+
## Next Steps
|
|
178
|
+
1. [What should happen next to continue this work]
|
|
179
|
+
|
|
180
|
+
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
181
|
+
/**
|
|
182
|
+
* Generate a summary of abandoned branch entries.
|
|
183
|
+
*
|
|
184
|
+
* @param entries - Session entries to summarize (chronological order)
|
|
185
|
+
* @param options - Generation options
|
|
186
|
+
*/
|
|
187
|
+
export async function generateBranchSummary(entries, options) {
|
|
188
|
+
const { model, apiKey, headers, signal, customInstructions, replaceInstructions, reserveTokens = 16384 } = options;
|
|
189
|
+
// Token budget = context window minus reserved space for prompt + response
|
|
190
|
+
const contextWindow = model.contextWindow || 128000;
|
|
191
|
+
const tokenBudget = contextWindow - reserveTokens;
|
|
192
|
+
const { messages, fileOps } = prepareBranchEntries(entries, tokenBudget);
|
|
193
|
+
if (messages.length === 0) {
|
|
194
|
+
return { summary: "No content to summarize" };
|
|
195
|
+
}
|
|
196
|
+
// Transform to LLM-compatible messages, then serialize to text
|
|
197
|
+
// Serialization prevents the model from treating it as a conversation to continue
|
|
198
|
+
const llmMessages = convertToLlm(messages);
|
|
199
|
+
const conversationText = serializeConversation(llmMessages);
|
|
200
|
+
// Build prompt
|
|
201
|
+
let instructions;
|
|
202
|
+
if (replaceInstructions && customInstructions) {
|
|
203
|
+
instructions = customInstructions;
|
|
204
|
+
}
|
|
205
|
+
else if (customInstructions) {
|
|
206
|
+
instructions = `${BRANCH_SUMMARY_PROMPT}\n\nAdditional focus: ${customInstructions}`;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
instructions = BRANCH_SUMMARY_PROMPT;
|
|
210
|
+
}
|
|
211
|
+
const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${instructions}`;
|
|
212
|
+
const summarizationMessages = [
|
|
213
|
+
{
|
|
214
|
+
role: "user",
|
|
215
|
+
content: [{ type: "text", text: promptText }],
|
|
216
|
+
timestamp: Date.now(),
|
|
217
|
+
},
|
|
218
|
+
];
|
|
219
|
+
// Call LLM for summarization
|
|
220
|
+
const response = await completeSimple(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, { apiKey, headers, signal, maxTokens: 2048 });
|
|
221
|
+
// Check if aborted or errored
|
|
222
|
+
if (response.stopReason === "aborted") {
|
|
223
|
+
return { aborted: true };
|
|
224
|
+
}
|
|
225
|
+
if (response.stopReason === "error") {
|
|
226
|
+
return { error: response.errorMessage || "Summarization failed" };
|
|
227
|
+
}
|
|
228
|
+
let summary = response.content
|
|
229
|
+
.filter((c) => c.type === "text")
|
|
230
|
+
.map((c) => c.text)
|
|
231
|
+
.join("\n");
|
|
232
|
+
// Prepend preamble to provide context about the branch summary
|
|
233
|
+
summary = BRANCH_SUMMARY_PREAMBLE + summary;
|
|
234
|
+
// Compute file lists and append to summary
|
|
235
|
+
const { readFiles, modifiedFiles } = computeFileLists(fileOps);
|
|
236
|
+
summary += formatFileOperations(readFiles, modifiedFiles);
|
|
237
|
+
return {
|
|
238
|
+
summary: summary || "No summary generated",
|
|
239
|
+
readFiles,
|
|
240
|
+
modifiedFiles,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event-sourced compaction engine.
|
|
3
|
+
*
|
|
4
|
+
* Builds compaction summaries directly from EventStore projections. It never
|
|
5
|
+
* mutates or deletes old events; callers record the returned first_kept_event_id
|
|
6
|
+
* on COMPACTION_END so projections can ignore the summarized prefix.
|
|
7
|
+
*/
|
|
8
|
+
import type { EventBase } from "../event-store/types.js";
|
|
9
|
+
import type { EventStore } from "../event-store/store.js";
|
|
10
|
+
import type { SessionProjection } from "../projection/session-projection.js";
|
|
11
|
+
import type { LLMClient, ModelConfig } from "../runtime/llm-types.js";
|
|
12
|
+
import type { CompactionOutcome, CompactionPolicy, CompactionReason } from "../runtime/policies.js";
|
|
13
|
+
export interface CompactionEngineSettings {
|
|
14
|
+
/** Estimated active model context window. */
|
|
15
|
+
contextWindow?: number;
|
|
16
|
+
/** Reserve this many tokens for future turns. */
|
|
17
|
+
reserveTokens?: number;
|
|
18
|
+
/** Keep roughly this many recent tokens verbatim after compaction. */
|
|
19
|
+
keepRecentTokens?: number;
|
|
20
|
+
/** Optional custom threshold. Defaults to (contextWindow - reserveTokens) / contextWindow. */
|
|
21
|
+
threshold?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface CompactionEngineConfig {
|
|
24
|
+
store: EventStore;
|
|
25
|
+
projection: SessionProjection;
|
|
26
|
+
llmClient: LLMClient;
|
|
27
|
+
model: ModelConfig;
|
|
28
|
+
systemPrompt?: string;
|
|
29
|
+
settings?: CompactionEngineSettings;
|
|
30
|
+
}
|
|
31
|
+
export declare class CompactionEngine implements CompactionPolicy {
|
|
32
|
+
private config;
|
|
33
|
+
constructor(config: CompactionEngineConfig);
|
|
34
|
+
estimateContextTokens(): number;
|
|
35
|
+
contextWindow(): number;
|
|
36
|
+
threshold(): number;
|
|
37
|
+
isOverflow(lastAssistantMessageEvent: EventBase | undefined): boolean;
|
|
38
|
+
compact(reason: CompactionReason, signal: AbortSignal): Promise<CompactionOutcome>;
|
|
39
|
+
private _generateSummary;
|
|
40
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event-sourced compaction engine.
|
|
3
|
+
*
|
|
4
|
+
* Builds compaction summaries directly from EventStore projections. It never
|
|
5
|
+
* mutates or deletes old events; callers record the returned first_kept_event_id
|
|
6
|
+
* on COMPACTION_END so projections can ignore the summarized prefix.
|
|
7
|
+
*/
|
|
8
|
+
import { convertToLlm } from "../messages.js";
|
|
9
|
+
import { eventToMessage } from "../projection/event-to-message.js";
|
|
10
|
+
import { serializeConversation, SUMMARIZATION_SYSTEM_PROMPT } from "./utils.js";
|
|
11
|
+
const DEFAULT_CONTEXT_WINDOW = 128000;
|
|
12
|
+
const DEFAULT_RESERVE_TOKENS = 16384;
|
|
13
|
+
const DEFAULT_KEEP_RECENT_TOKENS = 20000;
|
|
14
|
+
const SUMMARIZATION_PROMPT = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.
|
|
15
|
+
|
|
16
|
+
Use this EXACT format:
|
|
17
|
+
|
|
18
|
+
## Goal
|
|
19
|
+
[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
|
|
20
|
+
|
|
21
|
+
## Constraints & Preferences
|
|
22
|
+
- [Any constraints, preferences, or requirements mentioned by user]
|
|
23
|
+
- [Or "(none)" if none were mentioned]
|
|
24
|
+
|
|
25
|
+
## Progress
|
|
26
|
+
### Done
|
|
27
|
+
- [x] [Completed tasks/changes]
|
|
28
|
+
|
|
29
|
+
### In Progress
|
|
30
|
+
- [ ] [Current work]
|
|
31
|
+
|
|
32
|
+
### Blocked
|
|
33
|
+
- [Issues preventing progress, if any]
|
|
34
|
+
|
|
35
|
+
## Key Decisions
|
|
36
|
+
- **[Decision]**: [Brief rationale]
|
|
37
|
+
|
|
38
|
+
## Next Steps
|
|
39
|
+
1. [Ordered list of what should happen next]
|
|
40
|
+
|
|
41
|
+
## Critical Context
|
|
42
|
+
- [Any data, examples, or references needed to continue]
|
|
43
|
+
- [Or "(none)" if not applicable]
|
|
44
|
+
|
|
45
|
+
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
46
|
+
const UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.
|
|
47
|
+
|
|
48
|
+
Update the existing structured summary with new information. RULES:
|
|
49
|
+
- PRESERVE all existing information from the previous summary
|
|
50
|
+
- ADD new progress, decisions, and context from the new messages
|
|
51
|
+
- UPDATE the Progress section: move items from "In Progress" to "Done" when completed
|
|
52
|
+
- UPDATE "Next Steps" based on what was accomplished
|
|
53
|
+
- PRESERVE exact file paths, function names, and error messages
|
|
54
|
+
- If something is no longer relevant, you may remove it
|
|
55
|
+
|
|
56
|
+
Use this EXACT format:
|
|
57
|
+
|
|
58
|
+
## Goal
|
|
59
|
+
[Preserve existing goals, add new ones if the task expanded]
|
|
60
|
+
|
|
61
|
+
## Constraints & Preferences
|
|
62
|
+
- [Preserve existing, add new ones discovered]
|
|
63
|
+
|
|
64
|
+
## Progress
|
|
65
|
+
### Done
|
|
66
|
+
- [x] [Include previously done items AND newly completed items]
|
|
67
|
+
|
|
68
|
+
### In Progress
|
|
69
|
+
- [ ] [Current work - update based on progress]
|
|
70
|
+
|
|
71
|
+
### Blocked
|
|
72
|
+
- [Current blockers - remove if resolved]
|
|
73
|
+
|
|
74
|
+
## Key Decisions
|
|
75
|
+
- **[Decision]**: [Brief rationale] (preserve all previous, add new)
|
|
76
|
+
|
|
77
|
+
## Next Steps
|
|
78
|
+
1. [Update based on current state]
|
|
79
|
+
|
|
80
|
+
## Critical Context
|
|
81
|
+
- [Preserve important context, add new if needed]
|
|
82
|
+
|
|
83
|
+
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
84
|
+
export class CompactionEngine {
|
|
85
|
+
constructor(config) {
|
|
86
|
+
this.config = config;
|
|
87
|
+
}
|
|
88
|
+
estimateContextTokens() {
|
|
89
|
+
return estimateMessagesTokens(this.config.projection.buildContext().messages);
|
|
90
|
+
}
|
|
91
|
+
contextWindow() {
|
|
92
|
+
return this.config.settings?.contextWindow ?? DEFAULT_CONTEXT_WINDOW;
|
|
93
|
+
}
|
|
94
|
+
threshold() {
|
|
95
|
+
if (this.config.settings?.threshold !== undefined)
|
|
96
|
+
return this.config.settings.threshold;
|
|
97
|
+
const window = this.contextWindow();
|
|
98
|
+
if (!Number.isFinite(window) || window <= 0)
|
|
99
|
+
return 1;
|
|
100
|
+
const reserve = this.config.settings?.reserveTokens ?? DEFAULT_RESERVE_TOKENS;
|
|
101
|
+
return Math.max(0, Math.min(1, (window - reserve) / window));
|
|
102
|
+
}
|
|
103
|
+
isOverflow(lastAssistantMessageEvent) {
|
|
104
|
+
if (!lastAssistantMessageEvent || lastAssistantMessageEvent.type !== "AGENT_MESSAGE_END")
|
|
105
|
+
return false;
|
|
106
|
+
const payload = lastAssistantMessageEvent.payload;
|
|
107
|
+
if (payload.stop_reason !== "error")
|
|
108
|
+
return false;
|
|
109
|
+
return /context.?length|context.?window|maximum.?context|token.?limit|too.?many.?tokens/i.test(payload.error_message ?? "");
|
|
110
|
+
}
|
|
111
|
+
async compact(reason, signal) {
|
|
112
|
+
const built = this.config.projection.buildContext();
|
|
113
|
+
const pairs = built.events
|
|
114
|
+
.map((event) => {
|
|
115
|
+
const message = eventToMessage(event);
|
|
116
|
+
return message ? { event, message } : undefined;
|
|
117
|
+
})
|
|
118
|
+
.filter((pair) => pair !== undefined);
|
|
119
|
+
const previousCompactionIndex = findLastIndex(pairs, (pair) => pair.event.type === "COMPACTION_END");
|
|
120
|
+
const previousSummary = previousCompactionIndex >= 0 ? pairs[previousCompactionIndex].message.summary : undefined;
|
|
121
|
+
const startIndex = previousCompactionIndex >= 0 ? previousCompactionIndex + 1 : 0;
|
|
122
|
+
const compactablePairs = pairs.slice(startIndex);
|
|
123
|
+
if (compactablePairs.length < 2) {
|
|
124
|
+
throw new Error("Nothing to compact");
|
|
125
|
+
}
|
|
126
|
+
const cutIndex = findCutIndex(compactablePairs, this.config.settings?.keepRecentTokens ?? DEFAULT_KEEP_RECENT_TOKENS);
|
|
127
|
+
if (cutIndex <= 0 || cutIndex >= compactablePairs.length) {
|
|
128
|
+
throw new Error("Nothing to compact");
|
|
129
|
+
}
|
|
130
|
+
const messagesToSummarize = compactablePairs.slice(0, cutIndex).map((pair) => pair.message);
|
|
131
|
+
const keptPairs = compactablePairs.slice(cutIndex);
|
|
132
|
+
const firstKeptEvent = keptPairs[0]?.event;
|
|
133
|
+
if (!firstKeptEvent) {
|
|
134
|
+
throw new Error("Unable to determine first kept event");
|
|
135
|
+
}
|
|
136
|
+
const tokensBefore = estimateMessagesTokens(built.messages);
|
|
137
|
+
const summary = await this._generateSummary(messagesToSummarize, previousSummary, signal);
|
|
138
|
+
const tokensAfter = estimateMessageTokens({
|
|
139
|
+
role: "compactionSummary",
|
|
140
|
+
summary,
|
|
141
|
+
tokensBefore,
|
|
142
|
+
timestamp: Date.now(),
|
|
143
|
+
}) + estimateMessagesTokens(keptPairs.map((pair) => pair.message));
|
|
144
|
+
return {
|
|
145
|
+
summary,
|
|
146
|
+
first_kept_event_id: firstKeptEvent.event_id,
|
|
147
|
+
tokens_before: tokensBefore,
|
|
148
|
+
tokens_after: tokensAfter,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
async _generateSummary(messages, previousSummary, signal) {
|
|
152
|
+
const conversationText = serializeConversation(convertToLlm(messages));
|
|
153
|
+
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
154
|
+
if (previousSummary) {
|
|
155
|
+
promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
|
|
156
|
+
}
|
|
157
|
+
promptText += previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
|
|
158
|
+
const response = await this.config.llmClient.complete({
|
|
159
|
+
messages: [
|
|
160
|
+
{
|
|
161
|
+
role: "user",
|
|
162
|
+
content: [{ type: "text", text: promptText }],
|
|
163
|
+
timestamp: Date.now(),
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
systemPrompt: this.config.systemPrompt ?? SUMMARIZATION_SYSTEM_PROMPT,
|
|
167
|
+
model: this.config.model,
|
|
168
|
+
tools: [],
|
|
169
|
+
signal,
|
|
170
|
+
});
|
|
171
|
+
if (response.stopReason === "error") {
|
|
172
|
+
throw new Error(`Summarization failed: ${response.errorMessage ?? "Unknown error"}`);
|
|
173
|
+
}
|
|
174
|
+
const summary = extractText(response);
|
|
175
|
+
if (!summary.trim()) {
|
|
176
|
+
throw new Error("Summarization produced an empty summary");
|
|
177
|
+
}
|
|
178
|
+
return summary;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function findCutIndex(pairs, keepRecentTokens) {
|
|
182
|
+
const validCutIndexes = pairs
|
|
183
|
+
.map((pair, index) => (isValidCutMessage(pair.message) ? index : -1))
|
|
184
|
+
.filter((index) => index >= 0);
|
|
185
|
+
if (validCutIndexes.length === 0)
|
|
186
|
+
return -1;
|
|
187
|
+
let accumulatedTokens = 0;
|
|
188
|
+
let cutIndex = validCutIndexes[0];
|
|
189
|
+
for (let i = pairs.length - 1; i >= 0; i--) {
|
|
190
|
+
accumulatedTokens += estimateMessageTokens(pairs[i].message);
|
|
191
|
+
if (accumulatedTokens >= keepRecentTokens) {
|
|
192
|
+
cutIndex = validCutIndexes.find((index) => index >= i) ?? validCutIndexes[validCutIndexes.length - 1];
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return cutIndex;
|
|
197
|
+
}
|
|
198
|
+
function isValidCutMessage(message) {
|
|
199
|
+
return (message.role === "user" ||
|
|
200
|
+
message.role === "bashExecution" ||
|
|
201
|
+
message.role === "custom" ||
|
|
202
|
+
message.role === "branchSummary");
|
|
203
|
+
}
|
|
204
|
+
function extractText(response) {
|
|
205
|
+
return response.content
|
|
206
|
+
.filter((block) => block.type === "text" && typeof block.text === "string")
|
|
207
|
+
.map((block) => block.text)
|
|
208
|
+
.join("\n");
|
|
209
|
+
}
|
|
210
|
+
function estimateMessagesTokens(messages) {
|
|
211
|
+
const usageInfo = findLastAssistantUsage(messages);
|
|
212
|
+
if (!usageInfo) {
|
|
213
|
+
return messages.reduce((sum, message) => sum + estimateMessageTokens(message), 0);
|
|
214
|
+
}
|
|
215
|
+
let trailingTokens = 0;
|
|
216
|
+
for (let i = usageInfo.index + 1; i < messages.length; i++) {
|
|
217
|
+
trailingTokens += estimateMessageTokens(messages[i]);
|
|
218
|
+
}
|
|
219
|
+
return usageInfo.totalTokens + trailingTokens;
|
|
220
|
+
}
|
|
221
|
+
function findLastAssistantUsage(messages) {
|
|
222
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
223
|
+
const message = messages[i];
|
|
224
|
+
if (message.role !== "assistant" || !message.usage)
|
|
225
|
+
continue;
|
|
226
|
+
if (message.stopReason === "aborted" || message.stopReason === "error")
|
|
227
|
+
continue;
|
|
228
|
+
const usage = message.usage;
|
|
229
|
+
return {
|
|
230
|
+
totalTokens: usage.totalTokens ??
|
|
231
|
+
(usage.input ?? 0) + (usage.output ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0),
|
|
232
|
+
index: i,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
function estimateMessageTokens(message) {
|
|
238
|
+
let chars = 0;
|
|
239
|
+
switch (message.role) {
|
|
240
|
+
case "user":
|
|
241
|
+
case "custom":
|
|
242
|
+
case "toolResult":
|
|
243
|
+
chars = estimateContentChars(message.content);
|
|
244
|
+
break;
|
|
245
|
+
case "assistant":
|
|
246
|
+
for (const block of message.content) {
|
|
247
|
+
if (block.type === "text") {
|
|
248
|
+
chars += block.text.length;
|
|
249
|
+
}
|
|
250
|
+
else if (block.type === "thinking") {
|
|
251
|
+
chars += block.thinking.length;
|
|
252
|
+
}
|
|
253
|
+
else if (block.type === "toolCall") {
|
|
254
|
+
chars += block.name.length + JSON.stringify(block.arguments).length;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
case "bashExecution":
|
|
259
|
+
chars = message.command.length + message.output.length;
|
|
260
|
+
break;
|
|
261
|
+
case "branchSummary":
|
|
262
|
+
case "compactionSummary":
|
|
263
|
+
chars = message.summary.length;
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
return Math.ceil(chars / 4);
|
|
267
|
+
}
|
|
268
|
+
function estimateContentChars(content) {
|
|
269
|
+
if (typeof content === "string")
|
|
270
|
+
return content.length;
|
|
271
|
+
if (!Array.isArray(content))
|
|
272
|
+
return 0;
|
|
273
|
+
let chars = 0;
|
|
274
|
+
for (const block of content) {
|
|
275
|
+
if (!block || typeof block !== "object" || !("type" in block))
|
|
276
|
+
continue;
|
|
277
|
+
if (block.type === "text" && "text" in block && typeof block.text === "string") {
|
|
278
|
+
chars += block.text.length;
|
|
279
|
+
}
|
|
280
|
+
else if (block.type === "image") {
|
|
281
|
+
chars += 4800;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return chars;
|
|
285
|
+
}
|
|
286
|
+
function findLastIndex(items, predicate) {
|
|
287
|
+
for (let i = items.length - 1; i >= 0; i--) {
|
|
288
|
+
if (predicate(items[i]))
|
|
289
|
+
return i;
|
|
290
|
+
}
|
|
291
|
+
return -1;
|
|
292
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context compaction for long sessions.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions for compaction logic. The session manager handles I/O,
|
|
5
|
+
* and after compaction the session is reloaded.
|
|
6
|
+
*/
|
|
7
|
+
import type { AgentMessage } from "../agent/types.js";
|
|
8
|
+
import type { Usage } from "@earendil-works/pi-ai/compat";
|
|
9
|
+
import type { SessionEntry } from "../types/session-types.js";
|
|
10
|
+
import type { FileOperations } from "./utils.js";
|
|
11
|
+
/** Details stored in CompactionEntry.details for file tracking */
|
|
12
|
+
export interface CompactionDetails {
|
|
13
|
+
readFiles: string[];
|
|
14
|
+
modifiedFiles: string[];
|
|
15
|
+
}
|
|
16
|
+
/** Result from compact() - SessionManager adds uuid/parentUuid when saving */
|
|
17
|
+
export interface CompactionResult<T = unknown> {
|
|
18
|
+
summary: string;
|
|
19
|
+
firstKeptEntryId: string;
|
|
20
|
+
tokensBefore: number;
|
|
21
|
+
/** Extension-specific data (e.g., ArtifactIndex, version markers for structured compaction) */
|
|
22
|
+
details?: T;
|
|
23
|
+
}
|
|
24
|
+
export interface CompactionSettings {
|
|
25
|
+
enabled: boolean;
|
|
26
|
+
reserveTokens: number;
|
|
27
|
+
keepRecentTokens: number;
|
|
28
|
+
}
|
|
29
|
+
export declare const DEFAULT_COMPACTION_SETTINGS: CompactionSettings;
|
|
30
|
+
/**
|
|
31
|
+
* Calculate total context tokens from usage.
|
|
32
|
+
* Uses the native totalTokens field when available, falls back to computing from components.
|
|
33
|
+
*/
|
|
34
|
+
export declare function calculateContextTokens(usage: Usage): number;
|
|
35
|
+
/**
|
|
36
|
+
* Find the last non-aborted assistant message usage from session entries.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getLastAssistantUsage(entries: SessionEntry[]): Usage | undefined;
|
|
39
|
+
export interface ContextUsageEstimate {
|
|
40
|
+
tokens: number;
|
|
41
|
+
usageTokens: number;
|
|
42
|
+
trailingTokens: number;
|
|
43
|
+
lastUsageIndex: number | null;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Estimate context tokens from messages, using the last assistant usage when available.
|
|
47
|
+
* If there are messages after the last usage, estimate their tokens with estimateTokens.
|
|
48
|
+
*/
|
|
49
|
+
export declare function estimateContextTokens(messages: AgentMessage[]): ContextUsageEstimate;
|
|
50
|
+
/**
|
|
51
|
+
* Check if compaction should trigger based on context usage.
|
|
52
|
+
*/
|
|
53
|
+
export declare function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Estimate token count for a message using chars/4 heuristic.
|
|
56
|
+
* This is conservative (overestimates tokens).
|
|
57
|
+
*/
|
|
58
|
+
export declare function estimateTokens(message: AgentMessage): number;
|
|
59
|
+
/**
|
|
60
|
+
* Find the user message (or bashExecution) that starts the turn containing the given entry index.
|
|
61
|
+
* Returns -1 if no turn start found before the index.
|
|
62
|
+
* BashExecutionMessage is treated like a user message for turn boundaries.
|
|
63
|
+
*/
|
|
64
|
+
export declare function findTurnStartIndex(entries: SessionEntry[], entryIndex: number, startIndex: number): number;
|
|
65
|
+
export interface CutPointResult {
|
|
66
|
+
/** Index of first entry to keep */
|
|
67
|
+
firstKeptEntryIndex: number;
|
|
68
|
+
/** Index of user message that starts the turn being split, or -1 if not splitting */
|
|
69
|
+
turnStartIndex: number;
|
|
70
|
+
/** Whether this cut splits a turn (cut point is not a user message) */
|
|
71
|
+
isSplitTurn: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Find the cut point in session entries that keeps approximately `keepRecentTokens`.
|
|
75
|
+
*
|
|
76
|
+
* Algorithm: Walk backwards from newest, accumulating estimated message sizes.
|
|
77
|
+
* Stop when we've accumulated >= keepRecentTokens. Cut at that point.
|
|
78
|
+
*
|
|
79
|
+
* Can cut at user OR assistant messages (never tool results). When cutting at an
|
|
80
|
+
* assistant message with tool calls, its tool results come after and will be kept.
|
|
81
|
+
*
|
|
82
|
+
* Returns CutPointResult with:
|
|
83
|
+
* - firstKeptEntryIndex: the entry index to start keeping from
|
|
84
|
+
* - turnStartIndex: if cutting mid-turn, the user message that started that turn
|
|
85
|
+
* - isSplitTurn: whether we're cutting in the middle of a turn
|
|
86
|
+
*
|
|
87
|
+
* Only considers entries between `startIndex` and `endIndex` (exclusive).
|
|
88
|
+
*/
|
|
89
|
+
export declare function findCutPoint(entries: SessionEntry[], startIndex: number, endIndex: number, keepRecentTokens: number): CutPointResult;
|
|
90
|
+
export interface CompactionPreparation {
|
|
91
|
+
/** UUID of first entry to keep */
|
|
92
|
+
firstKeptEntryId: string;
|
|
93
|
+
/** Messages that will be summarized and discarded */
|
|
94
|
+
messagesToSummarize: AgentMessage[];
|
|
95
|
+
/** Messages that will be turned into turn prefix summary (if splitting) */
|
|
96
|
+
turnPrefixMessages: AgentMessage[];
|
|
97
|
+
/** Whether this is a split turn (cut point in middle of turn) */
|
|
98
|
+
isSplitTurn: boolean;
|
|
99
|
+
tokensBefore: number;
|
|
100
|
+
/** Summary from previous compaction, for iterative update */
|
|
101
|
+
previousSummary?: string;
|
|
102
|
+
/** File operations extracted from messagesToSummarize */
|
|
103
|
+
fileOps: FileOperations;
|
|
104
|
+
/** Compaction settions from settings.jsonl */
|
|
105
|
+
settings: CompactionSettings;
|
|
106
|
+
}
|