@bastani/atomic 0.9.8 → 0.9.9-alpha.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 +28 -0
- package/dist/builtin/cursor/CHANGELOG.md +6 -0
- package/dist/builtin/cursor/package.json +2 -2
- package/dist/builtin/intercom/CHANGELOG.md +6 -0
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/CHANGELOG.md +6 -0
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/CHANGELOG.md +6 -0
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/src/runs/background/async-execution-chain.ts +2 -0
- package/dist/builtin/subagents/src/runs/background/async-execution-common.ts +7 -1
- package/dist/builtin/subagents/src/runs/background/async-execution-single.ts +2 -0
- package/dist/builtin/subagents/src/runs/background/async-execution-types.ts +8 -2
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-parallel-runner.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-sequential-step.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/execution-attempt.ts +2 -1
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-async.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-parallel-task.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-resume.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-single.ts +2 -0
- package/dist/builtin/subagents/src/runs/shared/pi-args.ts +6 -0
- package/dist/builtin/subagents/src/shared/types-config.ts +4 -0
- package/dist/builtin/subagents/src/shared/types-depth.ts +29 -2
- package/dist/builtin/web-access/CHANGELOG.md +6 -0
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +22 -0
- package/dist/builtin/workflows/README.md +17 -8
- package/dist/builtin/workflows/builtin/goal-prompts.ts +8 -2
- package/dist/builtin/workflows/builtin/open-claude-design-utils.ts +26 -9
- package/dist/builtin/workflows/builtin/ralph-forked-prompts.ts +1 -1
- package/dist/builtin/workflows/builtin/ralph-reviewer-prompt.ts +8 -3
- package/dist/builtin/workflows/builtin/ralph-runner.ts +2 -0
- package/dist/builtin/workflows/builtin/shared-prompts.ts +24 -1
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/durable/backend.ts +53 -15
- package/dist/builtin/workflows/src/durable/completed-catalog.ts +245 -0
- package/dist/builtin/workflows/src/durable/completed-inspection.ts +213 -0
- package/dist/builtin/workflows/src/durable/dbos-backend.ts +120 -170
- package/dist/builtin/workflows/src/durable/dbos-envelope.ts +84 -8
- package/dist/builtin/workflows/src/durable/dbos-metadata.ts +98 -0
- package/dist/builtin/workflows/src/durable/dbos-tombstone.ts +27 -0
- package/dist/builtin/workflows/src/durable/factory.ts +1 -1
- package/dist/builtin/workflows/src/durable/file-backend.ts +247 -241
- package/dist/builtin/workflows/src/durable/file-lock.ts +153 -0
- package/dist/builtin/workflows/src/durable/file-state.ts +104 -0
- package/dist/builtin/workflows/src/durable/format-version.ts +12 -0
- package/dist/builtin/workflows/src/durable/index.ts +12 -0
- package/dist/builtin/workflows/src/durable/resume-catalog.ts +16 -27
- package/dist/builtin/workflows/src/durable/resume-eligibility.ts +20 -0
- package/dist/builtin/workflows/src/durable/resume-runtime.ts +29 -29
- package/dist/builtin/workflows/src/durable/scoped-backend.ts +20 -3
- package/dist/builtin/workflows/src/durable/stage-primitive.ts +29 -8
- package/dist/builtin/workflows/src/durable/types.ts +3 -0
- package/dist/builtin/workflows/src/engine/primitives/task.ts +1 -0
- package/dist/builtin/workflows/src/engine/run.ts +14 -14
- package/dist/builtin/workflows/src/extension/extension-lifecycle.ts +39 -9
- package/dist/builtin/workflows/src/extension/extension-runtime-state.ts +10 -0
- package/dist/builtin/workflows/src/extension/runtime-durable-resume.ts +133 -0
- package/dist/builtin/workflows/src/extension/runtime.ts +15 -40
- package/dist/builtin/workflows/src/extension/wiring.ts +9 -6
- package/dist/builtin/workflows/src/extension/workflow-durable-resume-command.ts +226 -0
- package/dist/builtin/workflows/src/extension/workflow-prompts.ts +1 -0
- package/dist/builtin/workflows/src/extension/workflow-run-control-command.ts +66 -112
- package/dist/builtin/workflows/src/extension/workflow-schema.ts +5 -5
- package/dist/builtin/workflows/src/extension/workflow-tool-control.ts +16 -1
- package/dist/builtin/workflows/src/runs/foreground/executor-direct-helpers.ts +50 -43
- package/dist/builtin/workflows/src/runs/foreground/executor-direct-output.ts +91 -0
- package/dist/builtin/workflows/src/runs/foreground/executor-direct.ts +95 -46
- package/dist/builtin/workflows/src/runs/foreground/executor-stage-call.ts +3 -1
- package/dist/builtin/workflows/src/runs/foreground/executor-task-prompts.ts +1 -1
- package/dist/builtin/workflows/src/runs/foreground/executor-types.ts +3 -0
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-controller.ts +1 -1
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-options.ts +36 -7
- package/dist/builtin/workflows/src/runs/shared/worktree-cache-lifecycle.ts +25 -0
- package/dist/builtin/workflows/src/runs/shared/worktree-cwd.ts +103 -0
- package/dist/builtin/workflows/src/runs/shared/worktree-generation.ts +57 -0
- package/dist/builtin/workflows/src/runs/shared/worktree-git.ts +134 -8
- package/dist/builtin/workflows/src/runs/shared/worktree.ts +2 -0
- package/dist/builtin/workflows/src/shared/resumable-workflow-notices.ts +43 -0
- package/dist/builtin/workflows/src/shared/timing.ts +4 -0
- package/dist/builtin/workflows/src/shared/types.ts +5 -3
- package/dist/builtin/workflows/src/tui/graph-view-constants.ts +1 -0
- package/dist/builtin/workflows/src/tui/graph-view-input.ts +24 -14
- package/dist/builtin/workflows/src/tui/workflow-resume-selector.ts +43 -13
- package/dist/core/agent-session-auto-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-auto-compaction.js +5 -7
- package/dist/core/agent-session-auto-compaction.js.map +1 -1
- package/dist/core/agent-session-compaction.d.ts +6 -34
- package/dist/core/agent-session-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-compaction.js +95 -233
- package/dist/core/agent-session-compaction.js.map +1 -1
- package/dist/core/agent-session-message-queue.d.ts +0 -4
- package/dist/core/agent-session-message-queue.d.ts.map +1 -1
- package/dist/core/agent-session-message-queue.js +1 -5
- package/dist/core/agent-session-message-queue.js.map +1 -1
- package/dist/core/agent-session-methods.d.ts +5 -6
- package/dist/core/agent-session-methods.d.ts.map +1 -1
- package/dist/core/agent-session-methods.js.map +1 -1
- package/dist/core/agent-session-types.d.ts +2 -12
- package/dist/core/agent-session-types.d.ts.map +1 -1
- package/dist/core/agent-session-types.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +1 -1
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +1 -2
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction-boundary.d.ts +9 -0
- package/dist/core/compaction/compaction-boundary.d.ts.map +1 -0
- package/dist/core/compaction/compaction-boundary.js +115 -0
- package/dist/core/compaction/compaction-boundary.js.map +1 -0
- package/dist/core/compaction/compaction-parameters.d.ts +5 -0
- package/dist/core/compaction/compaction-parameters.d.ts.map +1 -0
- package/dist/core/compaction/compaction-parameters.js +27 -0
- package/dist/core/compaction/compaction-parameters.js.map +1 -0
- package/dist/core/compaction/compaction-runner.d.ts +14 -0
- package/dist/core/compaction/compaction-runner.d.ts.map +1 -0
- package/dist/core/compaction/compaction-runner.js +29 -0
- package/dist/core/compaction/compaction-runner.js.map +1 -0
- package/dist/core/compaction/compaction-types.d.ts +79 -0
- package/dist/core/compaction/compaction-types.d.ts.map +1 -0
- package/dist/core/compaction/compaction-types.js +6 -0
- package/dist/core/compaction/compaction-types.js.map +1 -0
- package/dist/core/compaction/deleted-ranges.d.ts +6 -0
- package/dist/core/compaction/deleted-ranges.d.ts.map +1 -0
- package/dist/core/compaction/deleted-ranges.js +134 -0
- package/dist/core/compaction/deleted-ranges.js.map +1 -0
- package/dist/core/compaction/index.d.ts +7 -1
- package/dist/core/compaction/index.d.ts.map +1 -1
- package/dist/core/compaction/index.js +7 -1
- package/dist/core/compaction/index.js.map +1 -1
- package/dist/core/compaction/range-planner.d.ts +21 -0
- package/dist/core/compaction/range-planner.d.ts.map +1 -0
- package/dist/core/compaction/range-planner.js +145 -0
- package/dist/core/compaction/range-planner.js.map +1 -0
- package/dist/core/compaction/transcript-serialization.d.ts +11 -0
- package/dist/core/compaction/transcript-serialization.d.ts.map +1 -0
- package/dist/core/compaction/transcript-serialization.js +104 -0
- package/dist/core/compaction/transcript-serialization.js.map +1 -0
- package/dist/core/extensions/context-types.d.ts +2 -2
- package/dist/core/extensions/context-types.d.ts.map +1 -1
- package/dist/core/extensions/context-types.js.map +1 -1
- package/dist/core/extensions/event-results.d.ts +2 -2
- package/dist/core/extensions/event-results.d.ts.map +1 -1
- package/dist/core/extensions/event-results.js.map +1 -1
- package/dist/core/extensions/session-events.d.ts +7 -7
- package/dist/core/extensions/session-events.d.ts.map +1 -1
- package/dist/core/extensions/session-events.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/messages.d.ts +4 -0
- package/dist/core/messages.d.ts.map +1 -1
- package/dist/core/messages.js +11 -0
- package/dist/core/messages.js.map +1 -1
- package/dist/core/provider-context-usage.d.ts.map +1 -1
- package/dist/core/provider-context-usage.js +1 -2
- package/dist/core/provider-context-usage.js.map +1 -1
- package/dist/core/session-manager-archive.d.ts +2 -1
- package/dist/core/session-manager-archive.d.ts.map +1 -1
- package/dist/core/session-manager-archive.js +2 -2
- package/dist/core/session-manager-archive.js.map +1 -1
- package/dist/core/session-manager-classification.d.ts +9 -0
- package/dist/core/session-manager-classification.d.ts.map +1 -0
- package/dist/core/session-manager-classification.js +37 -0
- package/dist/core/session-manager-classification.js.map +1 -0
- package/dist/core/session-manager-core.d.ts +4 -4
- package/dist/core/session-manager-core.d.ts.map +1 -1
- package/dist/core/session-manager-core.js +13 -8
- package/dist/core/session-manager-core.js.map +1 -1
- package/dist/core/session-manager-entries.d.ts +4 -3
- package/dist/core/session-manager-entries.d.ts.map +1 -1
- package/dist/core/session-manager-entries.js +12 -10
- package/dist/core/session-manager-entries.js.map +1 -1
- package/dist/core/session-manager-history.d.ts +6 -23
- package/dist/core/session-manager-history.d.ts.map +1 -1
- package/dist/core/session-manager-history.js +23 -256
- package/dist/core/session-manager-history.js.map +1 -1
- package/dist/core/session-manager-list.d.ts.map +1 -1
- package/dist/core/session-manager-list.js +3 -3
- package/dist/core/session-manager-list.js.map +1 -1
- package/dist/core/session-manager-storage.d.ts +1 -1
- package/dist/core/session-manager-storage.d.ts.map +1 -1
- package/dist/core/session-manager-storage.js +3 -2
- package/dist/core/session-manager-storage.js.map +1 -1
- package/dist/core/session-manager-types.d.ts +14 -15
- package/dist/core/session-manager-types.d.ts.map +1 -1
- package/dist/core/session-manager-types.js.map +1 -1
- package/dist/core/session-manager.d.ts +3 -2
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +1 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +1 -1
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/main-session.d.ts +1 -0
- package/dist/main-session.d.ts.map +1 -1
- package/dist/main-session.js +7 -0
- package/dist/main-session.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/chat-message-renderer.d.ts +4 -1
- package/dist/modes/interactive/components/chat-message-renderer.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-message-renderer.js +12 -22
- package/dist/modes/interactive/components/chat-message-renderer.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-events.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-events.js +5 -11
- package/dist/modes/interactive/components/chat-session-host-events.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-rendering.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-rendering.js +3 -0
- package/dist/modes/interactive/components/chat-session-host-rendering.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-state.d.ts +1 -0
- package/dist/modes/interactive/components/chat-session-host-state.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-state.js +1 -0
- package/dist/modes/interactive/components/chat-session-host-state.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-utils.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-utils.js +1 -1
- package/dist/modes/interactive/components/chat-session-host-utils.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.js +1 -0
- package/dist/modes/interactive/components/chat-session-host.js.map +1 -1
- package/dist/modes/interactive/components/compaction-boundary-message.d.ts +20 -0
- package/dist/modes/interactive/components/compaction-boundary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/compaction-boundary-message.js +48 -0
- package/dist/modes/interactive/components/compaction-boundary-message.js.map +1 -0
- package/dist/modes/interactive/components/index.d.ts +1 -1
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -1
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.js +3 -0
- package/dist/modes/interactive/components/session-selector-list.js.map +1 -1
- package/dist/modes/interactive/interactive-agent-events.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-agent-events.js +3 -47
- package/dist/modes/interactive/interactive-agent-events.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-deps.d.ts +2 -2
- package/dist/modes/interactive/interactive-mode-deps.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-deps.js +1 -1
- package/dist/modes/interactive/interactive-mode-deps.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.d.ts +5 -3
- package/dist/modes/interactive/interactive-mode-surface.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.js.map +1 -1
- package/dist/modes/interactive/interactive-render-chat.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-render-chat.js +21 -12
- package/dist/modes/interactive/interactive-render-chat.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +3 -5
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +1 -6
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.js +0 -4
- package/dist/modes/rpc/rpc-command-handler.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +2 -11
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/docs/compaction.md +97 -772
- package/docs/extensions.md +25 -29
- package/docs/json.md +2 -2
- package/docs/rpc.md +32 -24
- package/docs/sdk.md +3 -3
- package/docs/session-format.md +49 -30
- package/docs/sessions.md +5 -3
- package/docs/settings.md +14 -4
- package/docs/usage.md +1 -1
- package/docs/workflows.md +30 -15
- package/examples/extensions/custom-compaction.ts +14 -58
- package/examples/extensions/handoff.ts +2 -3
- package/npm-shrinkwrap.json +23 -23
- package/package.json +2 -2
- package/dist/core/compaction/context-assistant-turns.d.ts +0 -42
- package/dist/core/compaction/context-assistant-turns.d.ts.map +0 -1
- package/dist/core/compaction/context-assistant-turns.js +0 -87
- package/dist/core/compaction/context-assistant-turns.js.map +0 -1
- package/dist/core/compaction/context-compaction-critical.d.ts +0 -15
- package/dist/core/compaction/context-compaction-critical.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-critical.js +0 -57
- package/dist/core/compaction/context-compaction-critical.js.map +0 -1
- package/dist/core/compaction/context-compaction-eviction-alternates.d.ts +0 -18
- package/dist/core/compaction/context-compaction-eviction-alternates.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-eviction-alternates.js +0 -186
- package/dist/core/compaction/context-compaction-eviction-alternates.js.map +0 -1
- package/dist/core/compaction/context-compaction-eviction.d.ts +0 -12
- package/dist/core/compaction/context-compaction-eviction.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-eviction.js +0 -222
- package/dist/core/compaction/context-compaction-eviction.js.map +0 -1
- package/dist/core/compaction/context-compaction-metrics.d.ts +0 -28
- package/dist/core/compaction/context-compaction-metrics.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-metrics.js +0 -107
- package/dist/core/compaction/context-compaction-metrics.js.map +0 -1
- package/dist/core/compaction/context-compaction-prompt.d.ts +0 -9
- package/dist/core/compaction/context-compaction-prompt.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-prompt.js +0 -163
- package/dist/core/compaction/context-compaction-prompt.js.map +0 -1
- package/dist/core/compaction/context-compaction-runner.d.ts +0 -11
- package/dist/core/compaction/context-compaction-runner.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-runner.js +0 -273
- package/dist/core/compaction/context-compaction-runner.js.map +0 -1
- package/dist/core/compaction/context-compaction-strategy.d.ts +0 -5
- package/dist/core/compaction/context-compaction-strategy.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-strategy.js +0 -27
- package/dist/core/compaction/context-compaction-strategy.js.map +0 -1
- package/dist/core/compaction/context-compaction-types.d.ts +0 -75
- package/dist/core/compaction/context-compaction-types.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-types.js +0 -6
- package/dist/core/compaction/context-compaction-types.js.map +0 -1
- package/dist/core/compaction/context-compaction.d.ts +0 -10
- package/dist/core/compaction/context-compaction.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction.js +0 -8
- package/dist/core/compaction/context-compaction.js.map +0 -1
- package/dist/core/compaction/context-deletion-application.d.ts +0 -14
- package/dist/core/compaction/context-deletion-application.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-application.js +0 -261
- package/dist/core/compaction/context-deletion-application.js.map +0 -1
- package/dist/core/compaction/context-deletion-store.d.ts +0 -78
- package/dist/core/compaction/context-deletion-store.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-store.js +0 -162
- package/dist/core/compaction/context-deletion-store.js.map +0 -1
- package/dist/core/compaction/context-deletion-targets.d.ts +0 -43
- package/dist/core/compaction/context-deletion-targets.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-targets.js +0 -292
- package/dist/core/compaction/context-deletion-targets.js.map +0 -1
- package/dist/core/compaction/context-deletion-tool-definitions.d.ts +0 -193
- package/dist/core/compaction/context-deletion-tool-definitions.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-tool-definitions.js +0 -98
- package/dist/core/compaction/context-deletion-tool-definitions.js.map +0 -1
- package/dist/core/compaction/context-deletion-tool-helpers.d.ts +0 -19
- package/dist/core/compaction/context-deletion-tool-helpers.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-tool-helpers.js +0 -210
- package/dist/core/compaction/context-deletion-tool-helpers.js.map +0 -1
- package/dist/core/compaction/context-deletion-tools.d.ts +0 -4
- package/dist/core/compaction/context-deletion-tools.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-tools.js +0 -403
- package/dist/core/compaction/context-deletion-tools.js.map +0 -1
- package/dist/core/compaction/context-transcript-analysis.d.ts +0 -11
- package/dist/core/compaction/context-transcript-analysis.d.ts.map +0 -1
- package/dist/core/compaction/context-transcript-analysis.js +0 -228
- package/dist/core/compaction/context-transcript-analysis.js.map +0 -1
- package/dist/core/session-manager-tool-dependencies.d.ts +0 -10
- package/dist/core/session-manager-tool-dependencies.d.ts.map +0 -1
- package/dist/core/session-manager-tool-dependencies.js +0 -133
- package/dist/core/session-manager-tool-dependencies.js.map +0 -1
- package/dist/modes/interactive/components/context-compaction-summary-message.d.ts +0 -17
- package/dist/modes/interactive/components/context-compaction-summary-message.d.ts.map +0 -1
- package/dist/modes/interactive/components/context-compaction-summary-message.js +0 -83
- package/dist/modes/interactive/components/context-compaction-summary-message.js.map +0 -1
|
@@ -1,403 +0,0 @@
|
|
|
1
|
-
import { CONTEXT_COMPACTION_AUTO_QUERY, } from "./context-compaction-types.js";
|
|
2
|
-
import { createContextCompactionBudgetDetails, createContextDeletionToolResult, countRemainingImageBlocks, finitePositiveNumber, formatErrorMessage, sumRemainingImageTokens, } from "./context-compaction-metrics.js";
|
|
3
|
-
import { getTranscriptCompactionParameters, normalizeContextCompactionParameters } from "./context-compaction-strategy.js";
|
|
4
|
-
import { CONTEXT_COMPACTION_BUDGET_TOOL, CONTEXT_DELETE_TOOL, CONTEXT_DELETE_TOOL_NAME, CONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES, CONTEXT_GREP_DELETE_TOOL, CONTEXT_GREP_DELETE_TOOL_NAME, CONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS, CONTEXT_READ_ENTRY_MAX_CHARS, CONTEXT_READ_ENTRY_TOOL, CONTEXT_READ_ENTRY_TOOL_NAME, CONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS, CONTEXT_SEARCH_DEFAULT_MAX_MATCHES, CONTEXT_SEARCH_MAX_CONTEXT_CHARS, CONTEXT_SEARCH_MAX_MATCHES, CONTEXT_SEARCH_TRANSCRIPT_TOOL, } from "./context-deletion-tool-definitions.js";
|
|
5
|
-
import { computeContextCompactionStats, contextDeletionRequestFromObject, validateContextDeletionRequest } from "./context-deletion-application.js";
|
|
6
|
-
import { canDeleteTarget, deletionRequestFromTargets, getRecentContextEntryIds, isStaleUserImageOnlyEntry, mergeContextDeletionTargets, } from "./context-deletion-targets.js";
|
|
7
|
-
import { createContextDeletionStore } from "./context-deletion-store.js";
|
|
8
|
-
import { addGrepCandidate, assertSafeRegexScan, clampInteger, createGrepMatcher, currentTargetDeleted, filterProtectedGrepCandidates, findMatchIndex, snippetForMatch, textSlice, } from "./context-deletion-tool-helpers.js";
|
|
9
|
-
export function createContextDeletionTool(inputTranscript, options = {}) {
|
|
10
|
-
const contextWindow = finitePositiveNumber(options.contextWindow);
|
|
11
|
-
const parameters = normalizeContextCompactionParameters({ ...getTranscriptCompactionParameters(inputTranscript), ...options }, inputTranscript.parameters?.query ?? CONTEXT_COMPACTION_AUTO_QUERY);
|
|
12
|
-
const transcript = { ...inputTranscript, parameters };
|
|
13
|
-
const store = createContextDeletionStore(transcript);
|
|
14
|
-
let validatedResult;
|
|
15
|
-
function readTargets() {
|
|
16
|
-
return store.readTargets();
|
|
17
|
-
}
|
|
18
|
-
function applyValidatedTargets(additionalTargets) {
|
|
19
|
-
const mergedTargets = mergeContextDeletionTargets(readTargets(), additionalTargets);
|
|
20
|
-
validatedResult = validateContextDeletionRequest(deletionRequestFromTargets(mergedTargets), transcript);
|
|
21
|
-
store.replaceTargets(validatedResult.deletedTargets);
|
|
22
|
-
return validatedResult;
|
|
23
|
-
}
|
|
24
|
-
function currentStats() {
|
|
25
|
-
return validatedResult?.stats ?? computeContextCompactionStats(transcript, readTargets());
|
|
26
|
-
}
|
|
27
|
-
function canDeleteProtectedTarget(target) {
|
|
28
|
-
return canDeleteTarget(transcript, target);
|
|
29
|
-
}
|
|
30
|
-
function shouldGrepDeleteContentBlockAsEntry(entryId, blockCount) {
|
|
31
|
-
if (blockCount <= 1)
|
|
32
|
-
return true;
|
|
33
|
-
const entry = transcript.entries.find((candidate) => candidate.entryId === entryId);
|
|
34
|
-
return entry !== undefined && isStaleUserImageOnlyEntry(transcript, entry);
|
|
35
|
-
}
|
|
36
|
-
const tool = {
|
|
37
|
-
...CONTEXT_DELETE_TOOL,
|
|
38
|
-
label: "context deletion request",
|
|
39
|
-
executionMode: "parallel",
|
|
40
|
-
async execute(_toolCallId, params) {
|
|
41
|
-
return store.transaction(() => {
|
|
42
|
-
const callCount = store.incrementCallCount();
|
|
43
|
-
try {
|
|
44
|
-
const incomingRequest = contextDeletionRequestFromObject(params, `${CONTEXT_DELETE_TOOL_NAME} arguments`);
|
|
45
|
-
const incomingValidated = validateContextDeletionRequest(incomingRequest, transcript);
|
|
46
|
-
const applied = applyValidatedTargets(incomingValidated.deletedTargets);
|
|
47
|
-
store.clearLastError();
|
|
48
|
-
const deletedTargets = readTargets();
|
|
49
|
-
const details = {
|
|
50
|
-
deletions: deletionRequestFromTargets(deletedTargets).deletions,
|
|
51
|
-
deletedTargets,
|
|
52
|
-
stats: applied.stats,
|
|
53
|
-
callCount,
|
|
54
|
-
};
|
|
55
|
-
const text = `Recorded ${incomingValidated.deletedTargets.length} deletion target(s); ${deletedTargets.length} total validated deletion target(s) are selected. Continue calling ${CONTEXT_DELETE_TOOL_NAME} or ${CONTEXT_GREP_DELETE_TOOL_NAME} for additional deletions, or respond done when finished.`;
|
|
56
|
-
return createContextDeletionToolResult(text, details);
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
const message = formatErrorMessage(error);
|
|
60
|
-
store.setLastError(message);
|
|
61
|
-
const deletedTargets = readTargets();
|
|
62
|
-
const details = {
|
|
63
|
-
deletions: deletionRequestFromTargets(deletedTargets).deletions,
|
|
64
|
-
deletedTargets,
|
|
65
|
-
stats: currentStats(),
|
|
66
|
-
callCount,
|
|
67
|
-
error: message,
|
|
68
|
-
};
|
|
69
|
-
return createContextDeletionToolResult(`Error recording context deletion targets: ${message}. No new deletion targets were applied; continue with a corrected tool call.`, details);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
const grepTool = {
|
|
75
|
-
...CONTEXT_GREP_DELETE_TOOL,
|
|
76
|
-
label: "context grep delete",
|
|
77
|
-
executionMode: "parallel",
|
|
78
|
-
async execute(_toolCallId, params) {
|
|
79
|
-
return store.transaction(() => {
|
|
80
|
-
const callCount = store.incrementCallCount();
|
|
81
|
-
const pattern = params.pattern;
|
|
82
|
-
const regex = params.regex === true;
|
|
83
|
-
const caseSensitive = params.caseSensitive === true;
|
|
84
|
-
const target = params.target ?? "entry";
|
|
85
|
-
const maxMatches = params.maxMatches ?? CONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES;
|
|
86
|
-
const candidates = [];
|
|
87
|
-
const matches = [];
|
|
88
|
-
let reportedMatches = matches;
|
|
89
|
-
const skipped = [];
|
|
90
|
-
const seenTargets = new Set();
|
|
91
|
-
try {
|
|
92
|
-
if (regex) {
|
|
93
|
-
assertSafeRegexScan(store.getGrepScanTextLength(target));
|
|
94
|
-
}
|
|
95
|
-
const matcher = createGrepMatcher(pattern, regex, caseSensitive);
|
|
96
|
-
const currentTargets = readTargets();
|
|
97
|
-
const recentEntryIds = getRecentContextEntryIds(transcript);
|
|
98
|
-
if (target === "entry") {
|
|
99
|
-
for (const entry of store.listEntriesForGrep()) {
|
|
100
|
-
if (!matcher.test(entry.text))
|
|
101
|
-
continue;
|
|
102
|
-
const candidate = { kind: "entry", entryId: entry.entry_id };
|
|
103
|
-
if (recentEntryIds.has(candidate.entryId)) {
|
|
104
|
-
skipped.push({ entryId: entry.entry_id, target, reason: "protected_entry", text: entry.text });
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
if (entry.is_protected === 1 && !canDeleteProtectedTarget(candidate)) {
|
|
108
|
-
skipped.push({ entryId: entry.entry_id, target, reason: "protected_entry", text: entry.text });
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
if (currentTargetDeleted(currentTargets, candidate)) {
|
|
112
|
-
skipped.push({ entryId: entry.entry_id, target, reason: "already_deleted", text: entry.text });
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
addGrepCandidate(candidates, matches, seenTargets, candidate, {
|
|
116
|
-
entryId: entry.entry_id,
|
|
117
|
-
target,
|
|
118
|
-
text: entry.text,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
for (const block of store.listContentBlocksForGrep()) {
|
|
124
|
-
if (!matcher.test(block.text))
|
|
125
|
-
continue;
|
|
126
|
-
const candidate = shouldGrepDeleteContentBlockAsEntry(block.entry_id, block.block_count)
|
|
127
|
-
? { kind: "entry", entryId: block.entry_id }
|
|
128
|
-
: { kind: "content_block", entryId: block.entry_id, blockIndex: block.block_index };
|
|
129
|
-
if (recentEntryIds.has(candidate.entryId)) {
|
|
130
|
-
skipped.push({
|
|
131
|
-
entryId: block.entry_id,
|
|
132
|
-
target: candidate.kind,
|
|
133
|
-
...(candidate.kind === "content_block" ? { blockIndex: candidate.blockIndex } : {}),
|
|
134
|
-
reason: "protected_entry",
|
|
135
|
-
text: block.text,
|
|
136
|
-
});
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
if (block.entry_protected === 1 && !canDeleteProtectedTarget(candidate)) {
|
|
140
|
-
skipped.push({
|
|
141
|
-
entryId: block.entry_id,
|
|
142
|
-
target,
|
|
143
|
-
blockIndex: block.block_index,
|
|
144
|
-
reason: "protected_entry",
|
|
145
|
-
text: block.text,
|
|
146
|
-
});
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
if (block.block_protected === 1 && !canDeleteProtectedTarget(candidate)) {
|
|
150
|
-
skipped.push({
|
|
151
|
-
entryId: block.entry_id,
|
|
152
|
-
target,
|
|
153
|
-
blockIndex: block.block_index,
|
|
154
|
-
reason: "protected_block",
|
|
155
|
-
text: block.text,
|
|
156
|
-
});
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
if (currentTargetDeleted(currentTargets, candidate)) {
|
|
160
|
-
skipped.push({
|
|
161
|
-
entryId: block.entry_id,
|
|
162
|
-
target: candidate.kind,
|
|
163
|
-
...(candidate.kind === "content_block" ? { blockIndex: candidate.blockIndex } : {}),
|
|
164
|
-
reason: "already_deleted",
|
|
165
|
-
text: block.text,
|
|
166
|
-
});
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
addGrepCandidate(candidates, matches, seenTargets, candidate, {
|
|
170
|
-
entryId: block.entry_id,
|
|
171
|
-
target: candidate.kind,
|
|
172
|
-
...(candidate.kind === "content_block" ? { blockIndex: candidate.blockIndex } : {}),
|
|
173
|
-
text: block.text,
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
const eligible = filterProtectedGrepCandidates(candidates, matches, currentTargets, transcript, skipped);
|
|
178
|
-
reportedMatches = eligible.matches;
|
|
179
|
-
let applied;
|
|
180
|
-
if (params.expectedMatchCount !== undefined && eligible.candidates.length !== params.expectedMatchCount) {
|
|
181
|
-
skipped.push({ reason: "expected_match_count_mismatch" });
|
|
182
|
-
}
|
|
183
|
-
else if (eligible.candidates.length > maxMatches) {
|
|
184
|
-
skipped.push({ reason: "max_matches_exceeded" });
|
|
185
|
-
}
|
|
186
|
-
else if (eligible.candidates.length > 0) {
|
|
187
|
-
applied = applyValidatedTargets(eligible.candidates);
|
|
188
|
-
}
|
|
189
|
-
store.clearLastError();
|
|
190
|
-
const deletedTargets = readTargets();
|
|
191
|
-
const details = {
|
|
192
|
-
pattern,
|
|
193
|
-
regex,
|
|
194
|
-
caseSensitive,
|
|
195
|
-
target,
|
|
196
|
-
matches: eligible.matches,
|
|
197
|
-
skipped,
|
|
198
|
-
deletedTargets,
|
|
199
|
-
stats: applied?.stats ?? currentStats(),
|
|
200
|
-
callCount,
|
|
201
|
-
};
|
|
202
|
-
const text = `Matched ${eligible.matches.length} deletion target(s), skipped ${skipped.length}, and ${applied ? "applied" : "did not apply"} grep deletion for pattern ${JSON.stringify(pattern)}. Total validated deletion target(s): ${deletedTargets.length}.`;
|
|
203
|
-
return createContextDeletionToolResult(text, details);
|
|
204
|
-
}
|
|
205
|
-
catch (error) {
|
|
206
|
-
const message = formatErrorMessage(error);
|
|
207
|
-
store.setLastError(message);
|
|
208
|
-
const deletedTargets = readTargets();
|
|
209
|
-
const details = {
|
|
210
|
-
pattern,
|
|
211
|
-
regex,
|
|
212
|
-
caseSensitive,
|
|
213
|
-
target,
|
|
214
|
-
matches: reportedMatches,
|
|
215
|
-
skipped,
|
|
216
|
-
deletedTargets,
|
|
217
|
-
stats: currentStats(),
|
|
218
|
-
callCount,
|
|
219
|
-
error: message,
|
|
220
|
-
};
|
|
221
|
-
return createContextDeletionToolResult(`Error applying grep deletion for pattern ${JSON.stringify(pattern)}: ${message}. No new deletion targets were applied; continue with a corrected tool call.`, details);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
},
|
|
225
|
-
};
|
|
226
|
-
const searchTool = {
|
|
227
|
-
...CONTEXT_SEARCH_TRANSCRIPT_TOOL,
|
|
228
|
-
label: "context transcript search",
|
|
229
|
-
executionMode: "parallel",
|
|
230
|
-
async execute(_toolCallId, params) {
|
|
231
|
-
return store.transaction(() => {
|
|
232
|
-
const callCount = store.incrementCallCount();
|
|
233
|
-
const pattern = params.pattern;
|
|
234
|
-
const regex = params.regex === true;
|
|
235
|
-
const caseSensitive = params.caseSensitive === true;
|
|
236
|
-
const target = params.target ?? "entry";
|
|
237
|
-
const maxMatches = clampInteger(params.maxMatches, CONTEXT_SEARCH_DEFAULT_MAX_MATCHES, 1, CONTEXT_SEARCH_MAX_MATCHES);
|
|
238
|
-
const contextChars = clampInteger(params.contextChars, CONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS, 0, CONTEXT_SEARCH_MAX_CONTEXT_CHARS);
|
|
239
|
-
const matches = [];
|
|
240
|
-
let truncated = false;
|
|
241
|
-
try {
|
|
242
|
-
if (regex) {
|
|
243
|
-
assertSafeRegexScan(store.getGrepScanTextLength(target));
|
|
244
|
-
}
|
|
245
|
-
const matcher = createGrepMatcher(pattern, regex, caseSensitive);
|
|
246
|
-
if (target === "entry") {
|
|
247
|
-
for (const entry of store.listEntriesForGrep()) {
|
|
248
|
-
const matchIndex = findMatchIndex(matcher, entry.text);
|
|
249
|
-
if (matchIndex < 0)
|
|
250
|
-
continue;
|
|
251
|
-
if (matches.length >= maxMatches) {
|
|
252
|
-
truncated = true;
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
matches.push({
|
|
256
|
-
entryId: entry.entry_id,
|
|
257
|
-
target,
|
|
258
|
-
matchIndex,
|
|
259
|
-
snippet: snippetForMatch(entry.text, matchIndex, contextChars),
|
|
260
|
-
protected: entry.is_protected === 1,
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
for (const block of store.listContentBlocksForGrep()) {
|
|
266
|
-
const matchIndex = findMatchIndex(matcher, block.text);
|
|
267
|
-
if (matchIndex < 0)
|
|
268
|
-
continue;
|
|
269
|
-
if (matches.length >= maxMatches) {
|
|
270
|
-
truncated = true;
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
matches.push({
|
|
274
|
-
entryId: block.entry_id,
|
|
275
|
-
target,
|
|
276
|
-
blockIndex: block.block_index,
|
|
277
|
-
matchIndex,
|
|
278
|
-
snippet: snippetForMatch(block.text, matchIndex, contextChars),
|
|
279
|
-
protected: block.entry_protected === 1 || block.block_protected === 1,
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
store.clearLastError();
|
|
284
|
-
const details = {
|
|
285
|
-
pattern,
|
|
286
|
-
regex,
|
|
287
|
-
caseSensitive,
|
|
288
|
-
target,
|
|
289
|
-
matches,
|
|
290
|
-
truncated,
|
|
291
|
-
callCount,
|
|
292
|
-
};
|
|
293
|
-
const text = `Found ${matches.length}${truncated ? "+" : ""} ${target} match(es) for ${JSON.stringify(pattern)}. Use ${CONTEXT_READ_ENTRY_TOOL_NAME} with small maxChars to inspect exact content before deleting.`;
|
|
294
|
-
return createContextDeletionToolResult(text, details);
|
|
295
|
-
}
|
|
296
|
-
catch (error) {
|
|
297
|
-
const message = formatErrorMessage(error);
|
|
298
|
-
store.setLastError(message);
|
|
299
|
-
const details = {
|
|
300
|
-
pattern,
|
|
301
|
-
regex,
|
|
302
|
-
caseSensitive,
|
|
303
|
-
target,
|
|
304
|
-
matches,
|
|
305
|
-
truncated,
|
|
306
|
-
callCount,
|
|
307
|
-
error: message,
|
|
308
|
-
};
|
|
309
|
-
return createContextDeletionToolResult(`Error searching transcript for ${JSON.stringify(pattern)}: ${message}. Try a literal pattern or narrower query.`, details);
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
},
|
|
313
|
-
};
|
|
314
|
-
const readEntryTool = {
|
|
315
|
-
...CONTEXT_READ_ENTRY_TOOL,
|
|
316
|
-
label: "context read entry",
|
|
317
|
-
executionMode: "parallel",
|
|
318
|
-
async execute(_toolCallId, params) {
|
|
319
|
-
return store.transaction(() => {
|
|
320
|
-
const callCount = store.incrementCallCount();
|
|
321
|
-
const offset = clampInteger(params.offset, 0, 0, Number.MAX_SAFE_INTEGER);
|
|
322
|
-
const maxChars = clampInteger(params.maxChars, CONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS, 1, CONTEXT_READ_ENTRY_MAX_CHARS);
|
|
323
|
-
try {
|
|
324
|
-
const row = params.blockIndex === undefined
|
|
325
|
-
? store.getEntryForRead(params.entryId)
|
|
326
|
-
: store.getContentBlockForRead(params.entryId, params.blockIndex);
|
|
327
|
-
if (!row) {
|
|
328
|
-
throw new Error(params.blockIndex === undefined
|
|
329
|
-
? `Unknown transcript entry: ${params.entryId}`
|
|
330
|
-
: `Unknown transcript content block: ${params.entryId}:${params.blockIndex}`);
|
|
331
|
-
}
|
|
332
|
-
const text = row.text;
|
|
333
|
-
const slice = textSlice(text, offset, maxChars);
|
|
334
|
-
store.clearLastError();
|
|
335
|
-
const details = {
|
|
336
|
-
entryId: params.entryId,
|
|
337
|
-
...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),
|
|
338
|
-
offset,
|
|
339
|
-
maxChars,
|
|
340
|
-
totalChars: text.length,
|
|
341
|
-
text: slice,
|
|
342
|
-
truncatedBefore: offset > 0,
|
|
343
|
-
truncatedAfter: offset + maxChars < text.length,
|
|
344
|
-
callCount,
|
|
345
|
-
};
|
|
346
|
-
const textResult = `Read ${slice.length} of ${text.length} characters from ${params.blockIndex === undefined ? params.entryId : `${params.entryId}:${params.blockIndex}`}. Keep reads small; increase offset for the next slice if needed.`;
|
|
347
|
-
return createContextDeletionToolResult(textResult, details);
|
|
348
|
-
}
|
|
349
|
-
catch (error) {
|
|
350
|
-
const message = formatErrorMessage(error);
|
|
351
|
-
store.setLastError(message);
|
|
352
|
-
const details = {
|
|
353
|
-
entryId: params.entryId,
|
|
354
|
-
...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),
|
|
355
|
-
offset,
|
|
356
|
-
maxChars,
|
|
357
|
-
totalChars: 0,
|
|
358
|
-
text: "",
|
|
359
|
-
truncatedBefore: false,
|
|
360
|
-
truncatedAfter: false,
|
|
361
|
-
callCount,
|
|
362
|
-
error: message,
|
|
363
|
-
};
|
|
364
|
-
return createContextDeletionToolResult(`Error reading transcript entry: ${message}`, details);
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
},
|
|
368
|
-
};
|
|
369
|
-
const budgetTool = {
|
|
370
|
-
...CONTEXT_COMPACTION_BUDGET_TOOL,
|
|
371
|
-
label: "context compaction budget",
|
|
372
|
-
executionMode: "parallel",
|
|
373
|
-
async execute(_toolCallId) {
|
|
374
|
-
return store.transaction(() => {
|
|
375
|
-
const callCount = store.incrementCallCount();
|
|
376
|
-
store.clearLastError();
|
|
377
|
-
const liveTargets = readTargets(); // recompute image stats each call (issue #1500)
|
|
378
|
-
const imageTokensRemaining = sumRemainingImageTokens(transcript, liveTargets);
|
|
379
|
-
const imageBlocksRemaining = countRemainingImageBlocks(transcript, liveTargets);
|
|
380
|
-
const details = createContextCompactionBudgetDetails(currentStats(), callCount, contextWindow, parameters, imageTokensRemaining, imageBlocksRemaining);
|
|
381
|
-
const windowText = details.contextWindowBeforePercent !== undefined
|
|
382
|
-
? ` Context window fullness: ${details.contextWindowBeforePercent}% before selected deletions, ${details.contextWindowAfterPercent}% after selected deletions.`
|
|
383
|
-
: " Context window size is unknown for this model, so fullness percentages are unavailable.";
|
|
384
|
-
const targetText = details.tokensToDeleteForTarget > 0 ? ` Delete about ${details.tokensToDeleteForTarget} more token(s) to reach the ${details.targetReductionPercent}% reduction target.` : ` The selected deletions meet or exceed the ${details.targetReductionPercent}% reduction target.`;
|
|
385
|
-
const imageText = details.remainingImageTokens > 0 ? ` Images account for ${details.imageTokenPercent}% of remaining context (${details.remainingImageTokens} tokens across ${details.imageBlockCount} block(s)); prefer deleting stale/superseded image content blocks when images dominate.` : "";
|
|
386
|
-
return createContextDeletionToolResult(`Current selected deletions reduce context by ${details.currentReductionPercent}% (${details.deletedTokens} token(s)); tokens after selected deletions: ${details.currentTokensAfter}/${details.tokensBefore}.${windowText}${targetText}${imageText} Keep maximizing useful retained context while aggressively removing low-value blocks.`, details);
|
|
387
|
-
});
|
|
388
|
-
},
|
|
389
|
-
};
|
|
390
|
-
return {
|
|
391
|
-
tool,
|
|
392
|
-
grepTool,
|
|
393
|
-
searchTool,
|
|
394
|
-
readEntryTool,
|
|
395
|
-
budgetTool,
|
|
396
|
-
tools: [tool, grepTool, searchTool, readEntryTool, budgetTool],
|
|
397
|
-
getDeletionRequest: () => deletionRequestFromTargets(readTargets()),
|
|
398
|
-
getValidatedResult: () => validatedResult,
|
|
399
|
-
getLastError: () => store.getLastError(),
|
|
400
|
-
getCallCount: () => store.getCallCount(),
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
//# sourceMappingURL=context-deletion-tools.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context-deletion-tools.js","sourceRoot":"","sources":["../../../src/core/compaction/context-deletion-tools.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,6BAA6B,GAI7B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACN,oCAAoC,EACpC,+BAA+B,EAC/B,yBAAyB,EACzB,oBAAoB,EACpB,kBAAkB,EAClB,uBAAuB,GACvB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,iCAAiC,EAAE,oCAAoC,EAAE,MAAM,kCAA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AAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,UAAU,CAAC;QAC9D,kBAAkB,EAAE,GAAG,EAAE,CAAC,0BAA0B,CAAC,WAAW,EAAE,CAAC;QACnE,kBAAkB,EAAE,GAAG,EAAE,CAAC,eAAe;QACzC,YAAY,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,YAAY,EAAE;QACxC,YAAY,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,YAAY,EAAE;KACxC,CAAC;AACH,CAAC","sourcesContent":["import type { AgentTool } from \"@earendil-works/pi-agent-core\";\nimport type { ContextCompactionStats, ContextDeletionTarget } from \"../session-manager.ts\";\nimport {\n\tCONTEXT_COMPACTION_AUTO_QUERY,\n\ttype CompactableTranscript,\n\ttype ContextCompactionRunOptions,\n\ttype ValidatedContextDeletionResult,\n} from \"./context-compaction-types.ts\";\nimport {\n\tcreateContextCompactionBudgetDetails,\n\tcreateContextDeletionToolResult,\n\tcountRemainingImageBlocks,\n\tfinitePositiveNumber,\n\tformatErrorMessage,\n\tsumRemainingImageTokens,\n} from \"./context-compaction-metrics.ts\";\nimport { getTranscriptCompactionParameters, normalizeContextCompactionParameters } from \"./context-compaction-strategy.ts\";\nimport {\n\tCONTEXT_COMPACTION_BUDGET_TOOL,\n\tCONTEXT_DELETE_TOOL,\n\tCONTEXT_DELETE_TOOL_NAME,\n\tCONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES,\n\tCONTEXT_GREP_DELETE_TOOL,\n\tCONTEXT_GREP_DELETE_TOOL_NAME,\n\tCONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS,\n\tCONTEXT_READ_ENTRY_MAX_CHARS,\n\tCONTEXT_READ_ENTRY_TOOL,\n\tCONTEXT_READ_ENTRY_TOOL_NAME,\n\tCONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS,\n\tCONTEXT_SEARCH_DEFAULT_MAX_MATCHES,\n\tCONTEXT_SEARCH_MAX_CONTEXT_CHARS,\n\tCONTEXT_SEARCH_MAX_MATCHES,\n\tCONTEXT_SEARCH_TRANSCRIPT_TOOL,\n\ttype ContextCompactionBudgetToolDetails,\n\ttype ContextDeletionToolController,\n\ttype ContextDeletionToolDetails,\n\ttype ContextGrepDeletionMatch,\n\ttype ContextGrepDeletionSkipped,\n\ttype ContextGrepDeletionToolDetails,\n\ttype ContextReadEntryToolDetails,\n\ttype ContextTranscriptSearchMatch,\n\ttype ContextTranscriptSearchToolDetails,\n\tContextCompactionBudgetToolParameters,\n\tContextDeleteToolParameters,\n\tContextGrepDeleteToolParameters,\n\tContextReadEntryToolParameters,\n\tContextSearchTranscriptToolParameters,\n} from \"./context-deletion-tool-definitions.ts\";\nimport { computeContextCompactionStats, contextDeletionRequestFromObject, validateContextDeletionRequest } from \"./context-deletion-application.ts\";\nimport {\n\tcanDeleteTarget,\n\tdeletionRequestFromTargets,\n\tgetRecentContextEntryIds,\n\tisStaleUserImageOnlyEntry,\n\tmergeContextDeletionTargets,\n} from \"./context-deletion-targets.ts\";\nimport { createContextDeletionStore } from \"./context-deletion-store.ts\";\nimport {\n\taddGrepCandidate,\n\tassertSafeRegexScan,\n\tclampInteger,\n\tcreateGrepMatcher,\n\tcurrentTargetDeleted,\n\tfilterProtectedGrepCandidates,\n\tfindMatchIndex,\n\tsnippetForMatch,\n\ttextSlice,\n} from \"./context-deletion-tool-helpers.ts\";\n\nexport function createContextDeletionTool(\n\tinputTranscript: CompactableTranscript,\n\toptions: ContextCompactionRunOptions = {},\n): ContextDeletionToolController {\n\tconst contextWindow = finitePositiveNumber(options.contextWindow);\n\tconst parameters = normalizeContextCompactionParameters(\n\t\t{ ...getTranscriptCompactionParameters(inputTranscript), ...options },\n\t\tinputTranscript.parameters?.query ?? CONTEXT_COMPACTION_AUTO_QUERY,\n\t);\n\tconst transcript: CompactableTranscript = { ...inputTranscript, parameters };\n\tconst store = createContextDeletionStore(transcript);\n\tlet validatedResult: ValidatedContextDeletionResult | undefined;\n\n\tfunction readTargets(): ContextDeletionTarget[] {\n\t\treturn store.readTargets();\n\t}\n\n\tfunction applyValidatedTargets(additionalTargets: readonly ContextDeletionTarget[]): ValidatedContextDeletionResult {\n\t\tconst mergedTargets = mergeContextDeletionTargets(readTargets(), additionalTargets);\n\t\tvalidatedResult = validateContextDeletionRequest(deletionRequestFromTargets(mergedTargets), transcript);\n\t\tstore.replaceTargets(validatedResult.deletedTargets);\n\t\treturn validatedResult;\n\t}\n\n\tfunction currentStats(): ContextCompactionStats {\n\t\treturn validatedResult?.stats ?? computeContextCompactionStats(transcript, readTargets());\n\t}\n\n\tfunction canDeleteProtectedTarget(target: ContextDeletionTarget): boolean {\n\t\treturn canDeleteTarget(transcript, target);\n\t}\n\n\tfunction shouldGrepDeleteContentBlockAsEntry(entryId: string, blockCount: number): boolean {\n\t\tif (blockCount <= 1) return true;\n\t\tconst entry = transcript.entries.find((candidate) => candidate.entryId === entryId);\n\t\treturn entry !== undefined && isStaleUserImageOnlyEntry(transcript, entry);\n\t}\n\n\tconst tool: AgentTool<typeof ContextDeleteToolParameters, ContextDeletionToolDetails> = {\n\t\t...CONTEXT_DELETE_TOOL,\n\t\tlabel: \"context deletion request\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\ttry {\n\t\t\t\t\tconst incomingRequest = contextDeletionRequestFromObject(params, `${CONTEXT_DELETE_TOOL_NAME} arguments`);\n\t\t\t\t\tconst incomingValidated = validateContextDeletionRequest(incomingRequest, transcript);\n\t\t\t\t\tconst applied = applyValidatedTargets(incomingValidated.deletedTargets);\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst deletedTargets = readTargets();\n\n\t\t\t\t\tconst details: ContextDeletionToolDetails = {\n\t\t\t\t\t\tdeletions: deletionRequestFromTargets(deletedTargets).deletions,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: applied.stats,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Recorded ${incomingValidated.deletedTargets.length} deletion target(s); ${deletedTargets.length} total validated deletion target(s) are selected. Continue calling ${CONTEXT_DELETE_TOOL_NAME} or ${CONTEXT_GREP_DELETE_TOOL_NAME} for additional deletions, or respond done when finished.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst deletedTargets = readTargets();\n\t\t\t\t\tconst details: ContextDeletionToolDetails = {\n\t\t\t\t\t\tdeletions: deletionRequestFromTargets(deletedTargets).deletions,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error recording context deletion targets: ${message}. No new deletion targets were applied; continue with a corrected tool call.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst grepTool: AgentTool<typeof ContextGrepDeleteToolParameters, ContextGrepDeletionToolDetails> = {\n\t\t...CONTEXT_GREP_DELETE_TOOL,\n\t\tlabel: \"context grep delete\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst pattern = params.pattern;\n\t\t\t\tconst regex = params.regex === true;\n\t\t\t\tconst caseSensitive = params.caseSensitive === true;\n\t\t\t\tconst target = params.target ?? \"entry\";\n\t\t\t\tconst maxMatches = params.maxMatches ?? CONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES;\n\t\t\t\tconst candidates: ContextDeletionTarget[] = [];\n\t\t\t\tconst matches: ContextGrepDeletionMatch[] = [];\n\t\t\t\tlet reportedMatches: ContextGrepDeletionMatch[] = matches;\n\t\t\t\tconst skipped: ContextGrepDeletionSkipped[] = [];\n\t\t\t\tconst seenTargets = new Set<string>();\n\n\t\t\t\ttry {\n\t\t\t\t\tif (regex) {\n\t\t\t\t\t\tassertSafeRegexScan(store.getGrepScanTextLength(target));\n\t\t\t\t\t}\n\t\t\t\t\tconst matcher = createGrepMatcher(pattern, regex, caseSensitive);\n\t\t\t\t\tconst currentTargets = readTargets();\n\t\t\t\t\tconst recentEntryIds = getRecentContextEntryIds(transcript);\n\n\t\t\t\t\tif (target === \"entry\") {\n\t\t\t\t\t\tfor (const entry of store.listEntriesForGrep()) {\n\t\t\t\t\t\t\tif (!matcher.test(entry.text)) continue;\n\t\t\t\t\t\t\tconst candidate: ContextDeletionTarget = { kind: \"entry\", entryId: entry.entry_id };\n\t\t\t\t\t\t\tif (recentEntryIds.has(candidate.entryId)) {\n\t\t\t\t\t\t\t\tskipped.push({ entryId: entry.entry_id, target, reason: \"protected_entry\", text: entry.text });\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (entry.is_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({ entryId: entry.entry_id, target, reason: \"protected_entry\", text: entry.text });\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentTargetDeleted(currentTargets, candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({ entryId: entry.entry_id, target, reason: \"already_deleted\", text: entry.text });\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taddGrepCandidate(candidates, matches, seenTargets, candidate, {\n\t\t\t\t\t\t\t\tentryId: entry.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\ttext: entry.text,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfor (const block of store.listContentBlocksForGrep()) {\n\t\t\t\t\t\t\tif (!matcher.test(block.text)) continue;\n\t\t\t\t\t\t\tconst candidate: ContextDeletionTarget = shouldGrepDeleteContentBlockAsEntry(block.entry_id, block.block_count)\n\t\t\t\t\t\t\t\t? { kind: \"entry\", entryId: block.entry_id }\n\t\t\t\t\t\t\t\t: { kind: \"content_block\", entryId: block.entry_id, blockIndex: block.block_index };\n\t\t\t\t\t\t\tif (recentEntryIds.has(candidate.entryId)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget: candidate.kind,\n\t\t\t\t\t\t\t\t\t...(candidate.kind === \"content_block\" ? { blockIndex: candidate.blockIndex } : {}),\n\t\t\t\t\t\t\t\t\treason: \"protected_entry\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (block.entry_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\t\treason: \"protected_entry\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (block.block_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\t\treason: \"protected_block\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentTargetDeleted(currentTargets, candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget: candidate.kind,\n\t\t\t\t\t\t\t\t\t...(candidate.kind === \"content_block\" ? { blockIndex: candidate.blockIndex } : {}),\n\t\t\t\t\t\t\t\t\treason: \"already_deleted\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taddGrepCandidate(candidates, matches, seenTargets, candidate, {\n\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\ttarget: candidate.kind,\n\t\t\t\t\t\t\t\t...(candidate.kind === \"content_block\" ? { blockIndex: candidate.blockIndex } : {}),\n\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tconst eligible = filterProtectedGrepCandidates(candidates, matches, currentTargets, transcript, skipped);\n\t\t\t\t\treportedMatches = eligible.matches;\n\t\t\t\t\tlet applied: ValidatedContextDeletionResult | undefined;\n\t\t\t\t\tif (params.expectedMatchCount !== undefined && eligible.candidates.length !== params.expectedMatchCount) {\n\t\t\t\t\t\tskipped.push({ reason: \"expected_match_count_mismatch\" });\n\t\t\t\t\t} else if (eligible.candidates.length > maxMatches) {\n\t\t\t\t\t\tskipped.push({ reason: \"max_matches_exceeded\" });\n\t\t\t\t\t} else if (eligible.candidates.length > 0) {\n\t\t\t\t\t\tapplied = applyValidatedTargets(eligible.candidates);\n\t\t\t\t\t}\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst deletedTargets = readTargets();\n\n\t\t\t\t\tconst details: ContextGrepDeletionToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches: eligible.matches,\n\t\t\t\t\t\tskipped,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: applied?.stats ?? currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Matched ${eligible.matches.length} deletion target(s), skipped ${skipped.length}, and ${applied ? \"applied\" : \"did not apply\"} grep deletion for pattern ${JSON.stringify(pattern)}. Total validated deletion target(s): ${deletedTargets.length}.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst deletedTargets = readTargets();\n\t\t\t\t\tconst details: ContextGrepDeletionToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches: reportedMatches,\n\t\t\t\t\t\tskipped,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error applying grep deletion for pattern ${JSON.stringify(pattern)}: ${message}. No new deletion targets were applied; continue with a corrected tool call.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst searchTool: AgentTool<typeof ContextSearchTranscriptToolParameters, ContextTranscriptSearchToolDetails> = {\n\t\t...CONTEXT_SEARCH_TRANSCRIPT_TOOL,\n\t\tlabel: \"context transcript search\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst pattern = params.pattern;\n\t\t\t\tconst regex = params.regex === true;\n\t\t\t\tconst caseSensitive = params.caseSensitive === true;\n\t\t\t\tconst target = params.target ?? \"entry\";\n\t\t\t\tconst maxMatches = clampInteger(params.maxMatches, CONTEXT_SEARCH_DEFAULT_MAX_MATCHES, 1, CONTEXT_SEARCH_MAX_MATCHES);\n\t\t\t\tconst contextChars = clampInteger(\n\t\t\t\t\tparams.contextChars,\n\t\t\t\t\tCONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS,\n\t\t\t\t\t0,\n\t\t\t\t\tCONTEXT_SEARCH_MAX_CONTEXT_CHARS,\n\t\t\t\t);\n\t\t\t\tconst matches: ContextTranscriptSearchMatch[] = [];\n\t\t\t\tlet truncated = false;\n\n\t\t\t\ttry {\n\t\t\t\t\tif (regex) {\n\t\t\t\t\t\tassertSafeRegexScan(store.getGrepScanTextLength(target));\n\t\t\t\t\t}\n\t\t\t\t\tconst matcher = createGrepMatcher(pattern, regex, caseSensitive);\n\t\t\t\t\tif (target === \"entry\") {\n\t\t\t\t\t\tfor (const entry of store.listEntriesForGrep()) {\n\t\t\t\t\t\t\tconst matchIndex = findMatchIndex(matcher, entry.text);\n\t\t\t\t\t\t\tif (matchIndex < 0) continue;\n\t\t\t\t\t\t\tif (matches.length >= maxMatches) {\n\t\t\t\t\t\t\t\ttruncated = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmatches.push({\n\t\t\t\t\t\t\t\tentryId: entry.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\tmatchIndex,\n\t\t\t\t\t\t\t\tsnippet: snippetForMatch(entry.text, matchIndex, contextChars),\n\t\t\t\t\t\t\t\tprotected: entry.is_protected === 1,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfor (const block of store.listContentBlocksForGrep()) {\n\t\t\t\t\t\t\tconst matchIndex = findMatchIndex(matcher, block.text);\n\t\t\t\t\t\t\tif (matchIndex < 0) continue;\n\t\t\t\t\t\t\tif (matches.length >= maxMatches) {\n\t\t\t\t\t\t\t\ttruncated = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmatches.push({\n\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\tmatchIndex,\n\t\t\t\t\t\t\t\tsnippet: snippetForMatch(block.text, matchIndex, contextChars),\n\t\t\t\t\t\t\t\tprotected: block.entry_protected === 1 || block.block_protected === 1,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst details: ContextTranscriptSearchToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\ttruncated,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Found ${matches.length}${truncated ? \"+\" : \"\"} ${target} match(es) for ${JSON.stringify(pattern)}. Use ${CONTEXT_READ_ENTRY_TOOL_NAME} with small maxChars to inspect exact content before deleting.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst details: ContextTranscriptSearchToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\ttruncated,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error searching transcript for ${JSON.stringify(pattern)}: ${message}. Try a literal pattern or narrower query.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst readEntryTool: AgentTool<typeof ContextReadEntryToolParameters, ContextReadEntryToolDetails> = {\n\t\t...CONTEXT_READ_ENTRY_TOOL,\n\t\tlabel: \"context read entry\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst offset = clampInteger(params.offset, 0, 0, Number.MAX_SAFE_INTEGER);\n\t\t\t\tconst maxChars = clampInteger(\n\t\t\t\t\tparams.maxChars,\n\t\t\t\t\tCONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS,\n\t\t\t\t\t1,\n\t\t\t\t\tCONTEXT_READ_ENTRY_MAX_CHARS,\n\t\t\t\t);\n\t\t\t\ttry {\n\t\t\t\t\tconst row =\n\t\t\t\t\t\tparams.blockIndex === undefined\n\t\t\t\t\t\t\t? store.getEntryForRead(params.entryId)\n\t\t\t\t\t\t\t: store.getContentBlockForRead(params.entryId, params.blockIndex);\n\t\t\t\t\tif (!row) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\tparams.blockIndex === undefined\n\t\t\t\t\t\t\t\t? `Unknown transcript entry: ${params.entryId}`\n\t\t\t\t\t\t\t\t: `Unknown transcript content block: ${params.entryId}:${params.blockIndex}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tconst text = row.text;\n\t\t\t\t\tconst slice = textSlice(text, offset, maxChars);\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst details: ContextReadEntryToolDetails = {\n\t\t\t\t\t\tentryId: params.entryId,\n\t\t\t\t\t\t...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),\n\t\t\t\t\t\toffset,\n\t\t\t\t\t\tmaxChars,\n\t\t\t\t\t\ttotalChars: text.length,\n\t\t\t\t\t\ttext: slice,\n\t\t\t\t\t\ttruncatedBefore: offset > 0,\n\t\t\t\t\t\ttruncatedAfter: offset + maxChars < text.length,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst textResult = `Read ${slice.length} of ${text.length} characters from ${params.blockIndex === undefined ? params.entryId : `${params.entryId}:${params.blockIndex}`}. Keep reads small; increase offset for the next slice if needed.`;\n\t\t\t\t\treturn createContextDeletionToolResult(textResult, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst details: ContextReadEntryToolDetails = {\n\t\t\t\t\t\tentryId: params.entryId,\n\t\t\t\t\t\t...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),\n\t\t\t\t\t\toffset,\n\t\t\t\t\t\tmaxChars,\n\t\t\t\t\t\ttotalChars: 0,\n\t\t\t\t\t\ttext: \"\",\n\t\t\t\t\t\ttruncatedBefore: false,\n\t\t\t\t\t\ttruncatedAfter: false,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(`Error reading transcript entry: ${message}`, details);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst budgetTool: AgentTool<typeof ContextCompactionBudgetToolParameters, ContextCompactionBudgetToolDetails> = {\n\t\t...CONTEXT_COMPACTION_BUDGET_TOOL,\n\t\tlabel: \"context compaction budget\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tstore.clearLastError();\n\t\t\t\tconst liveTargets = readTargets(); // recompute image stats each call (issue #1500)\n\t\t\t\tconst imageTokensRemaining = sumRemainingImageTokens(transcript, liveTargets);\n\t\t\t\tconst imageBlocksRemaining = countRemainingImageBlocks(transcript, liveTargets);\n\t\t\t\tconst details = createContextCompactionBudgetDetails(currentStats(), callCount, contextWindow, parameters, imageTokensRemaining, imageBlocksRemaining);\n\t\t\t\tconst windowText =\n\t\t\t\t\tdetails.contextWindowBeforePercent !== undefined\n\t\t\t\t\t\t? ` Context window fullness: ${details.contextWindowBeforePercent}% before selected deletions, ${details.contextWindowAfterPercent}% after selected deletions.`\n\t\t\t\t\t\t: \" Context window size is unknown for this model, so fullness percentages are unavailable.\";\n\t\t\t\tconst targetText = details.tokensToDeleteForTarget > 0 ? ` Delete about ${details.tokensToDeleteForTarget} more token(s) to reach the ${details.targetReductionPercent}% reduction target.` : ` The selected deletions meet or exceed the ${details.targetReductionPercent}% reduction target.`;\n\t\t\t\tconst imageText = details.remainingImageTokens > 0 ? ` Images account for ${details.imageTokenPercent}% of remaining context (${details.remainingImageTokens} tokens across ${details.imageBlockCount} block(s)); prefer deleting stale/superseded image content blocks when images dominate.` : \"\";\n\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t`Current selected deletions reduce context by ${details.currentReductionPercent}% (${details.deletedTokens} token(s)); tokens after selected deletions: ${details.currentTokensAfter}/${details.tokensBefore}.${windowText}${targetText}${imageText} Keep maximizing useful retained context while aggressively removing low-value blocks.`,\n\t\t\t\t\tdetails,\n\t\t\t\t);\n\t\t\t});\n\t\t},\n\t};\n\n\treturn {\n\t\ttool,\n\t\tgrepTool,\n\t\tsearchTool,\n\t\treadEntryTool,\n\t\tbudgetTool,\n\t\ttools: [tool, grepTool, searchTool, readEntryTool, budgetTool],\n\t\tgetDeletionRequest: () => deletionRequestFromTargets(readTargets()),\n\t\tgetValidatedResult: () => validatedResult,\n\t\tgetLastError: () => store.getLastError(),\n\t\tgetCallCount: () => store.getCallCount(),\n\t};\n}\n"]}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
|
-
import { type SessionEntry } from "../session-manager.ts";
|
|
3
|
-
import type { CompactionSettings } from "./compaction.ts";
|
|
4
|
-
import { type ContextCompactionPreparation, type ContextCompactionRunOptions } from "./context-compaction-types.ts";
|
|
5
|
-
export declare function isExcludedFromLlmContext(message: AgentMessage): boolean;
|
|
6
|
-
export declare function hasAssistantError(message: AgentMessage): boolean;
|
|
7
|
-
export declare function hasToolResultError(message: AgentMessage): boolean;
|
|
8
|
-
export declare function hasFailedBashExecution(message: AgentMessage): boolean;
|
|
9
|
-
export declare function autoDetectContextCompactionQuery(pathEntries: readonly SessionEntry[]): string;
|
|
10
|
-
export declare function prepareContextCompaction(pathEntries: SessionEntry[], settings: CompactionSettings, options?: ContextCompactionRunOptions): ContextCompactionPreparation | undefined;
|
|
11
|
-
//# sourceMappingURL=context-transcript-analysis.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context-transcript-analysis.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/context-transcript-analysis.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAIlE,OAAO,EAGN,KAAK,YAAY,EACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE1D,OAAO,EAIN,KAAK,4BAA4B,EACjC,KAAK,2BAA2B,EAChC,MAAM,+BAA+B,CAAC;AA0BvC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAEvE;AAkHD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAEhE;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAEjE;AAED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAErE;AAED,wBAAgB,gCAAgC,CAAC,WAAW,EAAE,SAAS,YAAY,EAAE,GAAG,MAAM,CAW7F;AAiBD,wBAAgB,wBAAwB,CACvC,WAAW,EAAE,YAAY,EAAE,EAC3B,QAAQ,EAAE,kBAAkB,EAC5B,OAAO,GAAE,2BAAgC,GACvC,4BAA4B,GAAG,SAAS,CA6D1C","sourcesContent":["import type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport type { AssistantMessage } from \"@earendil-works/pi-ai/compat\";\nimport { createBranchSummaryMessage, createCustomMessage, messageIsLlmVisible, userLikeContentBlockIsLlmVisible } from \"../messages.ts\";\nimport { normalizeDerivedSessionEntries } from \"../session-entry-normalization.ts\";\nimport {\n\tbuildContextDeletionFilteredPath,\n\tbuildEffectiveContextDeletionFilters,\n\ttype SessionEntry,\n} from \"../session-manager.ts\";\nimport type { CompactionSettings } from \"./compaction.ts\";\nimport { estimateContentBlockTokens, estimateTokens } from \"./compaction.ts\";\nimport {\n\tCONTEXT_COMPACTION_AUTO_QUERY,\n\ttype CompactableContentBlock,\n\ttype CompactableTranscriptEntry,\n\ttype ContextCompactionPreparation,\n\ttype ContextCompactionRunOptions,\n} from \"./context-compaction-types.ts\";\nimport {\n\tnormalizeContextCompactionParameters,\n\tnormalizeContextCompactionQuery,\n} from \"./context-compaction-strategy.ts\";\n\nfunction getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\tif (entry.type === \"message\") {\n\t\treturn entry.message;\n\t}\n\tif (entry.type === \"custom_message\") {\n\t\treturn createCustomMessage(\n\t\t\tentry.customType,\n\t\t\tentry.content,\n\t\t\tentry.display,\n\t\t\tentry.details,\n\t\t\tentry.timestamp,\n\t\t\tentry.excludeFromContext,\n\t\t);\n\t}\n\tif (entry.type === \"branch_summary\" && typeof entry.summary === \"string\") {\n\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t}\n\treturn undefined;\n}\n\nexport function isExcludedFromLlmContext(message: AgentMessage): boolean {\n\treturn !messageIsLlmVisible(message);\n}\n\nfunction getContextEligibleMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\tconst message = getMessageFromEntry(entry);\n\tif (!message || isExcludedFromLlmContext(message)) return undefined;\n\treturn message;\n}\n\nfunction safelySerialize(value: unknown): string {\n\ttry {\n\t\tconst serialized = JSON.stringify(value);\n\t\tif (typeof serialized === \"string\") return serialized;\n\t} catch {\n\t\t// Preserve a finite diagnostic projection for cyclic future payloads.\n\t}\n\treturn \"[unserializable content]\";\n}\n\nfunction textFromUnknownContent(content: unknown): string {\n\tif (typeof content === \"string\") return content;\n\tif (!Array.isArray(content)) return safelySerialize(content);\n\treturn content.map((block) => textFromContentBlock(block)).join(\"\\n\");\n}\n\nfunction textFromContentBlock(block: unknown): string {\n\tif (!block || typeof block !== \"object\") return String(block);\n\tconst record = block as Record<string, unknown>;\n\tif (record.type === \"text\" && typeof record.text === \"string\") return record.text;\n\tif (record.type === \"thinking\" && typeof record.thinking === \"string\") return record.thinking;\n\tif (record.type === \"toolCall\") {\n\t\tconst name = typeof record.name === \"string\" ? record.name : \"tool\";\n\t\tconst id = typeof record.id === \"string\" ? record.id : \"unknown\";\n\t\tconst args = \"arguments\" in record ? safelySerialize(record.arguments) : \"\";\n\t\treturn `toolCall ${id} ${name} ${args}`.trim();\n\t}\n\tif (record.type === \"image\") return \"[image]\";\n\treturn safelySerialize(record);\n}\n\nfunction getToolCallIdFromBlock(block: unknown): string | undefined {\n\tif (!block || typeof block !== \"object\") return undefined;\n\tconst record = block as Record<string, unknown>;\n\tif (record.type !== \"toolCall\") return undefined;\n\treturn typeof record.id === \"string\" ? record.id : undefined;\n}\n\nfunction getToolResultCallId(message: AgentMessage): string | undefined {\n\tif (message.role !== \"toolResult\") return undefined;\n\tconst callId = (message as { toolCallId?: unknown }).toolCallId;\n\treturn typeof callId === \"string\" ? callId : undefined;\n}\n\nfunction contentBlocksForEntry(\n\tentryId: string,\n\tmessage: AgentMessage,\n\tprotectedEntry: boolean,\n\texistingDeletedBlocks: ReadonlySet<number> | undefined,\n): CompactableContentBlock[] {\n\tconst content = (message as { content?: unknown }).content;\n\tif (!Array.isArray(content)) return [];\n\treturn content\n\t\t.map((block, blockIndex): CompactableContentBlock | undefined => {\n\t\t\tif (existingDeletedBlocks?.has(blockIndex)) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tconst type =\n\t\t\t\tblock && typeof block === \"object\" && typeof (block as { type?: unknown }).type === \"string\"\n\t\t\t\t\t? ((block as { type: string }).type)\n\t\t\t\t\t: \"unknown\";\n\t\t\tconst text = textFromContentBlock(block);\n\t\t\tconst llmVisible =\n\t\t\t\tmessage.role === \"user\" || message.role === \"custom\"\n\t\t\t\t\t? userLikeContentBlockIsLlmVisible(block)\n\t\t\t\t\t: true;\n\t\t\treturn {\n\t\t\t\tentryId,\n\t\t\t\tblockIndex,\n\t\t\t\ttype,\n\t\t\t\ttext,\n\t\t\t\ttokenEstimate: estimateContentBlockTokens(block, llmVisible),\n\t\t\t\tprotected: protectedEntry,\n\t\t\t\tllmVisible,\n\t\t\t\ttoolCallId: getToolCallIdFromBlock(block),\n\t\t\t};\n\t\t})\n\t\t.filter((block): block is CompactableContentBlock => block !== undefined);\n}\n\nfunction messageText(message: AgentMessage): string {\n\tswitch (message.role) {\n\t\tcase \"bashExecution\":\n\t\t\treturn `Ran ${message.command}\\n${message.output}`;\n\t\tcase \"branchSummary\":\n\t\t\treturn message.summary;\n\t\tcase \"custom\":\n\t\tcase \"toolResult\":\n\t\tcase \"user\":\n\t\t\treturn textFromUnknownContent(message.content);\n\t\tcase \"assistant\":\n\t\t\treturn textFromUnknownContent(message.content);\n\t\tcase \"compactionSummary\":\n\t\t\t// Legacy summary-compaction message type retained in the upstream AgentMessage union\n\t\t\t// after summary compaction was removed; surface its archival summary text.\n\t\t\treturn message.summary;\n\t\tdefault: {\n\t\t\t// Exhaustiveness guard: adding a new AgentMessage role must fail the build here instead\n\t\t\t// of silently degrading to an empty string.\n\t\t\tconst _exhaustiveCheck: never = message;\n\t\t\tvoid _exhaustiveCheck;\n\t\t\treturn \"\";\n\t\t}\n\t}\n}\n\nexport function hasAssistantError(message: AgentMessage): boolean {\n\treturn message.role === \"assistant\" && (message as AssistantMessage).stopReason === \"error\";\n}\n\nexport function hasToolResultError(message: AgentMessage): boolean {\n\treturn message.role === \"toolResult\" && (message as { isError?: unknown }).isError === true;\n}\n\nexport function hasFailedBashExecution(message: AgentMessage): boolean {\n\treturn message.role === \"bashExecution\" && typeof message.exitCode === \"number\" && message.exitCode !== 0;\n}\n\nexport function autoDetectContextCompactionQuery(pathEntries: readonly SessionEntry[]): string {\n\tconst derivedPathEntries = normalizeDerivedSessionEntries(pathEntries);\n\tfor (let index = derivedPathEntries.length - 1; index >= 0; index--) {\n\t\tconst entry = derivedPathEntries[index];\n\t\tif (entry.type === \"context_compaction\") continue;\n\t\tconst message = getContextEligibleMessageFromEntry(entry);\n\t\tif (!message || message.role !== \"user\") continue;\n\t\tconst text = messageText(message).trim();\n\t\tif (text.length > 0) return normalizeContextCompactionQuery(text, CONTEXT_COMPACTION_AUTO_QUERY);\n\t}\n\treturn CONTEXT_COMPACTION_AUTO_QUERY;\n}\n\nfunction isProtectedEntry(\n\tentry: SessionEntry,\n\tmessage: AgentMessage,\n\trecentEntryIds: ReadonlySet<string>,\n): boolean {\n\tif (recentEntryIds.has(entry.id)) return true;\n\tif (message.role === \"user\") return true;\n\tif (message.role === \"custom\") return true;\n\tif (message.role === \"branchSummary\") return true;\n\tif (hasAssistantError(message) || hasToolResultError(message)) return true;\n\tif (hasFailedBashExecution(message)) return true;\n\tif (entry.type === \"branch_summary\") return true;\n\treturn false;\n}\n\nexport function prepareContextCompaction(\n\tpathEntries: SessionEntry[],\n\tsettings: CompactionSettings,\n\toptions: ContextCompactionRunOptions = {},\n): ContextCompactionPreparation | undefined {\n\tconst derivedPathEntries = normalizeDerivedSessionEntries(pathEntries);\n\tif (derivedPathEntries.length === 0) return undefined;\n\n\tconst effectiveDeletionFilters = buildEffectiveContextDeletionFilters(derivedPathEntries);\n\tconst filteredPathEntries = buildContextDeletionFilteredPath(derivedPathEntries, effectiveDeletionFilters);\n\tconst parameters = normalizeContextCompactionParameters(\n\t\t{ ...settings, ...options },\n\t\tautoDetectContextCompactionQuery(filteredPathEntries),\n\t);\n\tconst rawEntryById = new Map(derivedPathEntries.map((entry) => [entry.id, entry]));\n\tconst messageEntryIds = filteredPathEntries\n\t\t.filter((entry) => entry.type !== \"context_compaction\" && getContextEligibleMessageFromEntry(entry) !== undefined)\n\t\t.map((entry) => entry.id);\n\tconst recentEntryIds = new Set(parameters.preserve_recent > 0 ? messageEntryIds.slice(-parameters.preserve_recent) : []);\n\tconst protectedEntryIds = new Set<string>();\n\tconst entries: CompactableTranscriptEntry[] = [];\n\n\tfor (const entry of filteredPathEntries) {\n\t\tif (entry.type === \"context_compaction\") continue;\n\t\tconst message = getContextEligibleMessageFromEntry(entry);\n\t\tif (!message) continue;\n\t\tconst rawEntry = rawEntryById.get(entry.id) ?? entry;\n\t\tconst protectedEntry = isProtectedEntry(entry, message, recentEntryIds);\n\t\tif (protectedEntry) protectedEntryIds.add(entry.id);\n\t\tconst rawMessage = getContextEligibleMessageFromEntry(rawEntry) ?? message;\n\t\tconst contentBlocks = contentBlocksForEntry(\n\t\t\tentry.id,\n\t\t\trawMessage,\n\t\t\tprotectedEntry,\n\t\t\teffectiveDeletionFilters.deletedContentBlocks.get(entry.id),\n\t\t);\n\t\tconst toolCallIds = contentBlocks.map((block) => block.toolCallId).filter((id): id is string => id !== undefined);\n\t\tconst text = contentBlocks.length > 0 ? contentBlocks.map((block) => block.text).join(\"\\n\") : messageText(message);\n\t\tentries.push({\n\t\t\tentryId: entry.id,\n\t\t\tentryType: entry.type,\n\t\t\trole: message.role,\n\t\t\ttext,\n\t\t\ttokenEstimate: estimateTokens(message),\n\t\t\tprotected: protectedEntry,\n\t\t\tcontentBlocks,\n\t\t\tmessage,\n\t\t\ttoolCallIds,\n\t\t\ttoolResultFor: getToolResultCallId(message),\n\t\t});\n\t}\n\n\tif (entries.length < 2) return undefined;\n\n\treturn {\n\t\tbranchEntries: derivedPathEntries,\n\t\tparameters,\n\t\ttranscript: {\n\t\t\tentries,\n\t\t\tprotectedEntryIds: [...protectedEntryIds],\n\t\t\ttokensBefore: entries.reduce((total, entry) => total + entry.tokenEstimate, 0),\n\t\t\tsettings,\n\t\t\tparameters,\n\t\t},\n\t};\n}\n"]}
|