@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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-manager-archive.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-archive.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"session-manager-archive.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-archive.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EACX,SAAS,EAET,iBAAiB,EACjB,YAAY,EAEZ,uBAAuB,EACvB,MAAM,4BAA4B,CAAC;AAGpC,wBAAgB,oBAAoB,CACnC,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,OAAO,EAAE,SAAS,EAAE,EACpB,KAAK,SAAY,GACf,MAAM,GAAG,SAAS,CAOpB;AAED,MAAM,WAAW,yBAAyB;IACzC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,mBAAmB,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,EAAE,uBAAuB,CAAC;IACnC,IAAI,EAAE,YAAY,EAAE,CAAC;IACrB,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,mBAAmB,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjD;AAED,MAAM,WAAW,oBAAoB;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,WAAW,EAAE,SAAS,EAAE,CAAC;IACzB,iBAAiB,EAAE,OAAO,CAAC;IAC3B,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAsDD,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,yBAAyB,GAAG,oBAAoB,CAwCjG;AAED,MAAM,WAAW,iBAAiB;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,mBAAmB,CAClC,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,iBAAiB,GACzB,iBAAiB,CA2CnB","sourcesContent":["import { join } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport {\n\tcreateLabelEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n} from \"./session-manager-entries.ts\";\nimport { getDefaultSessionDir } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntry,\n\tensureDirectory,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tFileEntry,\n\tLabelEntry,\n\tNewSessionOptions,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId, generateId } from \"./session-manager-validation.ts\";\n\nexport function createBackupSnapshot(\n\tsessionFile: string | undefined,\n\tentries: FileEntry[],\n\tlabel = \"compact\",\n): string | undefined {\n\tif (!sessionFile) return undefined;\n\tconst safeLabel = label.replace(/[^a-z0-9_-]/gi, \"-\").replace(/-+/g, \"-\").replace(/^-|-$/g, \"\") || \"backup\";\n\tconst timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n\tconst backupPath = `${sessionFile}.${timestamp}.${safeLabel}.bak`;\n\twriteSessionEntries(backupPath, entries);\n\treturn backupPath;\n}\n\nexport interface BranchedSessionStateInput {\n\tleafId: string;\n\tpersist: boolean;\n\tsessionDir: string;\n\tcwd: string;\n\tpreviousSessionFile: string | undefined;\n\tworkflow?: SessionWorkflowMetadata;\n\tpath: SessionEntry[];\n\tlabelsById: ReadonlyMap<string, string>;\n\tlabelTimestampsById: ReadonlyMap<string, string>;\n}\n\nexport interface BranchedSessionState {\n\tsessionId: string;\n\tsessionFile: string | undefined;\n\tfileEntries: FileEntry[];\n\tshouldRewriteFile: boolean;\n\tflushed?: boolean;\n}\n\nfunction collectLabelsForPath(\n\tpathEntryIds: ReadonlySet<string>,\n\tlabelsById: ReadonlyMap<string, string>,\n\tlabelTimestampsById: ReadonlyMap<string, string>,\n): Array<{ targetId: string; label: string; timestamp: string }> {\n\tconst labelsToWrite: Array<{ targetId: string; label: string; timestamp: string }> = [];\n\tfor (const [targetId, label] of labelsById) {\n\t\tif (pathEntryIds.has(targetId)) {\n\t\t\tlabelsToWrite.push({ targetId, label, timestamp: labelTimestampsById.get(targetId)! });\n\t\t}\n\t}\n\treturn labelsToWrite;\n}\n\nfunction buildPersistedLabelEntries(\n\tpathEntryIds: Set<string>,\n\tlastEntryId: string | null,\n\tlabelsToWrite: Array<{ targetId: string; label: string; timestamp: string }>,\n): LabelEntry[] {\n\tlet parentId = lastEntryId;\n\tconst labelEntries: LabelEntry[] = [];\n\tfor (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {\n\t\tconst labelEntry = createLabelEntry(targetId, label, new Set(pathEntryIds), parentId, labelTimestamp);\n\t\tpathEntryIds.add(labelEntry.id);\n\t\tlabelEntries.push(labelEntry);\n\t\tparentId = labelEntry.id;\n\t}\n\treturn labelEntries;\n}\n\nfunction buildInMemoryLabelEntries(\n\tpathEntryIds: Set<string>,\n\tlastEntryId: string | null,\n\tlabelsToWrite: Array<{ targetId: string; label: string; timestamp: string }>,\n): LabelEntry[] {\n\tconst labelEntries: LabelEntry[] = [];\n\tlet parentId = lastEntryId;\n\tfor (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {\n\t\tconst labelEntry: LabelEntry = {\n\t\t\ttype: \"label\",\n\t\t\tid: generateId(new Set([...pathEntryIds, ...labelEntries.map((e) => e.id)])),\n\t\t\tparentId,\n\t\t\ttimestamp: labelTimestamp,\n\t\t\ttargetId,\n\t\t\tlabel,\n\t\t};\n\t\tlabelEntries.push(labelEntry);\n\t\tparentId = labelEntry.id;\n\t}\n\treturn labelEntries;\n}\n\nexport function createBranchedSessionState(input: BranchedSessionStateInput): BranchedSessionState {\n\tif (input.path.length === 0) {\n\t\tthrow new Error(`Entry ${input.leafId} not found`);\n\t}\n\n\t// Filter out LabelEntry from path - we'll recreate them from the resolved map\n\tconst pathWithoutLabels = input.path.filter((entry) => entry.type !== \"label\");\n\tconst newSessionId = createSessionId();\n\tconst timestamp = new Date().toISOString();\n\tconst newSessionFile = createSessionFilePath(input.sessionDir, timestamp, newSessionId);\n\tconst header: SessionHeader = createSessionHeader(\n\t\tnewSessionId,\n\t\tinput.cwd,\n\t\ttimestamp,\n\t\tinput.persist ? input.previousSessionFile : undefined,\n\t\tinput.workflow !== undefined,\n\t\tinput.workflow,\n\t);\n\n\t// Collect labels for entries in the path\n\tconst pathEntryIds = new Set(pathWithoutLabels.map((entry) => entry.id));\n\tconst labelsToWrite = collectLabelsForPath(pathEntryIds, input.labelsById, input.labelTimestampsById);\n\tconst lastEntryId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;\n\tconst labelEntries = input.persist\n\t\t? buildPersistedLabelEntries(pathEntryIds, lastEntryId, labelsToWrite)\n\t\t: buildInMemoryLabelEntries(pathEntryIds, lastEntryId, labelsToWrite);\n\tconst fileEntries: FileEntry[] = [header, ...pathWithoutLabels, ...labelEntries];\n\n\tif (!input.persist) {\n\t\treturn { sessionId: newSessionId, sessionFile: undefined, fileEntries, shouldRewriteFile: false };\n\t}\n\n\tconst shouldRewriteFile = hasAssistantMessage(fileEntries);\n\treturn {\n\t\tsessionId: newSessionId,\n\t\tsessionFile: newSessionFile,\n\t\tfileEntries,\n\t\tshouldRewriteFile,\n\t\tflushed: shouldRewriteFile,\n\t};\n}\n\nexport interface ForkedSessionFile {\n\tcwd: string;\n\tsessionDir: string;\n\tsessionFile: string;\n}\n\nexport function forkSessionFromFile(\n\tsourcePath: string,\n\ttargetCwd: string,\n\tsessionDir?: string,\n\toptions?: NewSessionOptions,\n): ForkedSessionFile {\n\tconst resolvedSourcePath = resolvePath(sourcePath);\n\tconst resolvedTargetCwd = resolvePath(targetCwd);\n\tconst sourceEntries = loadEntriesFromFile(resolvedSourcePath);\n\tif (sourceEntries.length === 0) {\n\t\tthrow new Error(`Cannot fork: source session file is empty or invalid: ${resolvedSourcePath}`);\n\t}\n\n\tconst sourceHeader = sourceEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\tif (!sourceHeader) {\n\t\tthrow new Error(`Cannot fork: source session has no header: ${resolvedSourcePath}`);\n\t}\n\n\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(resolvedTargetCwd);\n\tensureDirectory(dir);\n\n\t// Create new session file with new ID but forked content\n\tif (options?.id !== undefined) {\n\t\tassertValidSessionId(options.id);\n\t}\n\tconst newSessionId = options?.id ?? createSessionId();\n\tconst timestamp = new Date().toISOString();\n\tconst newSessionFile = join(dir, `${timestamp.replace(/[:.]/g, \"-\")}_${newSessionId}.jsonl`);\n\n\t// Write new header pointing to source as parent, with updated cwd\n\tconst newHeader: SessionHeader = createSessionHeader(\n\t\tnewSessionId,\n\t\tresolvedTargetCwd,\n\t\ttimestamp,\n\t\tresolvedSourcePath,\n\t\toptions?.internal,\n\t\toptions?.workflow,\n\t);\n\tappendSessionEntry(newSessionFile, newHeader);\n\n\t// Copy all non-header entries from source\n\tfor (const entry of sourceEntries) {\n\t\tif (entry.type !== \"session\") {\n\t\t\tappendSessionEntry(newSessionFile, entry);\n\t\t}\n\t}\n\n\treturn { cwd: resolvedTargetCwd, sessionDir: dir, sessionFile: newSessionFile };\n}\n"]}
|
|
@@ -59,7 +59,7 @@ export function createBranchedSessionState(input) {
|
|
|
59
59
|
const newSessionId = createSessionId();
|
|
60
60
|
const timestamp = new Date().toISOString();
|
|
61
61
|
const newSessionFile = createSessionFilePath(input.sessionDir, timestamp, newSessionId);
|
|
62
|
-
const header = createSessionHeader(newSessionId, input.cwd, timestamp, input.persist ? input.previousSessionFile : undefined);
|
|
62
|
+
const header = createSessionHeader(newSessionId, input.cwd, timestamp, input.persist ? input.previousSessionFile : undefined, input.workflow !== undefined, input.workflow);
|
|
63
63
|
// Collect labels for entries in the path
|
|
64
64
|
const pathEntryIds = new Set(pathWithoutLabels.map((entry) => entry.id));
|
|
65
65
|
const labelsToWrite = collectLabelsForPath(pathEntryIds, input.labelsById, input.labelTimestampsById);
|
|
@@ -101,7 +101,7 @@ export function forkSessionFromFile(sourcePath, targetCwd, sessionDir, options)
|
|
|
101
101
|
const timestamp = new Date().toISOString();
|
|
102
102
|
const newSessionFile = join(dir, `${timestamp.replace(/[:.]/g, "-")}_${newSessionId}.jsonl`);
|
|
103
103
|
// Write new header pointing to source as parent, with updated cwd
|
|
104
|
-
const newHeader = createSessionHeader(newSessionId, resolvedTargetCwd, timestamp, resolvedSourcePath);
|
|
104
|
+
const newHeader = createSessionHeader(newSessionId, resolvedTargetCwd, timestamp, resolvedSourcePath, options?.internal, options?.workflow);
|
|
105
105
|
appendSessionEntry(newSessionFile, newHeader);
|
|
106
106
|
// Copy all non-header entries from source
|
|
107
107
|
for (const entry of sourceEntries) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-manager-archive.js","sourceRoot":"","sources":["../../src/core/session-manager-archive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EACN,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AA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,CAAC,CAAC;IAClC,CAAC;IACD,MAAM,YAAY,GAAG,OAAO,EAAE,EAAE,IAAI,eAAe,EAAE,CAAC;IACtD,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC3C,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,IAAI,YAAY,QAAQ,CAAC,CAAC;IAE7F,kEAAkE;IAClE,MAAM,SAAS,GAAkB,mBAAmB,CAAC,YAAY,EAAE,iBAAiB,EAAE,SAAS,EAAE,kBAAkB,CAAC,CAAC;IACrH,kBAAkB,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;IAE9C,0CAA0C;IAC1C,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE,CAAC;QACnC,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC9B,kBAAkB,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;QAC3C,CAAC;IACF,CAAC;IAED,OAAO,EAAE,GAAG,EAAE,iBAAiB,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,cAAc,EAAE,CAAC;AACjF,CAAC","sourcesContent":["import { join } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport {\n\tcreateLabelEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n} from \"./session-manager-entries.ts\";\nimport { getDefaultSessionDir } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntry,\n\tensureDirectory,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type { FileEntry, LabelEntry, NewSessionOptions, SessionEntry, SessionHeader } from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId, generateId } from \"./session-manager-validation.ts\";\n\nexport function createBackupSnapshot(\n\tsessionFile: string | undefined,\n\tentries: FileEntry[],\n\tlabel = \"compact\",\n): string | undefined {\n\tif (!sessionFile) return undefined;\n\tconst safeLabel = label.replace(/[^a-z0-9_-]/gi, \"-\").replace(/-+/g, \"-\").replace(/^-|-$/g, \"\") || \"backup\";\n\tconst timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n\tconst backupPath = `${sessionFile}.${timestamp}.${safeLabel}.bak`;\n\twriteSessionEntries(backupPath, entries);\n\treturn backupPath;\n}\n\nexport interface BranchedSessionStateInput {\n\tleafId: string;\n\tpersist: boolean;\n\tsessionDir: string;\n\tcwd: string;\n\tpreviousSessionFile: string | undefined;\n\tpath: SessionEntry[];\n\tlabelsById: ReadonlyMap<string, string>;\n\tlabelTimestampsById: ReadonlyMap<string, string>;\n}\n\nexport interface BranchedSessionState {\n\tsessionId: string;\n\tsessionFile: string | undefined;\n\tfileEntries: FileEntry[];\n\tshouldRewriteFile: boolean;\n\tflushed?: boolean;\n}\n\nfunction collectLabelsForPath(\n\tpathEntryIds: ReadonlySet<string>,\n\tlabelsById: ReadonlyMap<string, string>,\n\tlabelTimestampsById: ReadonlyMap<string, string>,\n): Array<{ targetId: string; label: string; timestamp: string }> {\n\tconst labelsToWrite: Array<{ targetId: string; label: string; timestamp: string }> = [];\n\tfor (const [targetId, label] of labelsById) {\n\t\tif (pathEntryIds.has(targetId)) {\n\t\t\tlabelsToWrite.push({ targetId, label, timestamp: labelTimestampsById.get(targetId)! });\n\t\t}\n\t}\n\treturn labelsToWrite;\n}\n\nfunction buildPersistedLabelEntries(\n\tpathEntryIds: Set<string>,\n\tlastEntryId: string | null,\n\tlabelsToWrite: Array<{ targetId: string; label: string; timestamp: string }>,\n): LabelEntry[] {\n\tlet parentId = lastEntryId;\n\tconst labelEntries: LabelEntry[] = [];\n\tfor (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {\n\t\tconst labelEntry = createLabelEntry(targetId, label, new Set(pathEntryIds), parentId, labelTimestamp);\n\t\tpathEntryIds.add(labelEntry.id);\n\t\tlabelEntries.push(labelEntry);\n\t\tparentId = labelEntry.id;\n\t}\n\treturn labelEntries;\n}\n\nfunction buildInMemoryLabelEntries(\n\tpathEntryIds: Set<string>,\n\tlastEntryId: string | null,\n\tlabelsToWrite: Array<{ targetId: string; label: string; timestamp: string }>,\n): LabelEntry[] {\n\tconst labelEntries: LabelEntry[] = [];\n\tlet parentId = lastEntryId;\n\tfor (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {\n\t\tconst labelEntry: LabelEntry = {\n\t\t\ttype: \"label\",\n\t\t\tid: generateId(new Set([...pathEntryIds, ...labelEntries.map((e) => e.id)])),\n\t\t\tparentId,\n\t\t\ttimestamp: labelTimestamp,\n\t\t\ttargetId,\n\t\t\tlabel,\n\t\t};\n\t\tlabelEntries.push(labelEntry);\n\t\tparentId = labelEntry.id;\n\t}\n\treturn labelEntries;\n}\n\nexport function createBranchedSessionState(input: BranchedSessionStateInput): BranchedSessionState {\n\tif (input.path.length === 0) {\n\t\tthrow new Error(`Entry ${input.leafId} not found`);\n\t}\n\n\t// Filter out LabelEntry from path - we'll recreate them from the resolved map\n\tconst pathWithoutLabels = input.path.filter((entry) => entry.type !== \"label\");\n\tconst newSessionId = createSessionId();\n\tconst timestamp = new Date().toISOString();\n\tconst newSessionFile = createSessionFilePath(input.sessionDir, timestamp, newSessionId);\n\tconst header: SessionHeader = createSessionHeader(\n\t\tnewSessionId,\n\t\tinput.cwd,\n\t\ttimestamp,\n\t\tinput.persist ? input.previousSessionFile : undefined,\n\t);\n\n\t// Collect labels for entries in the path\n\tconst pathEntryIds = new Set(pathWithoutLabels.map((entry) => entry.id));\n\tconst labelsToWrite = collectLabelsForPath(pathEntryIds, input.labelsById, input.labelTimestampsById);\n\tconst lastEntryId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;\n\tconst labelEntries = input.persist\n\t\t? buildPersistedLabelEntries(pathEntryIds, lastEntryId, labelsToWrite)\n\t\t: buildInMemoryLabelEntries(pathEntryIds, lastEntryId, labelsToWrite);\n\tconst fileEntries: FileEntry[] = [header, ...pathWithoutLabels, ...labelEntries];\n\n\tif (!input.persist) {\n\t\treturn { sessionId: newSessionId, sessionFile: undefined, fileEntries, shouldRewriteFile: false };\n\t}\n\n\tconst shouldRewriteFile = hasAssistantMessage(fileEntries);\n\treturn {\n\t\tsessionId: newSessionId,\n\t\tsessionFile: newSessionFile,\n\t\tfileEntries,\n\t\tshouldRewriteFile,\n\t\tflushed: shouldRewriteFile,\n\t};\n}\n\nexport interface ForkedSessionFile {\n\tcwd: string;\n\tsessionDir: string;\n\tsessionFile: string;\n}\n\nexport function forkSessionFromFile(\n\tsourcePath: string,\n\ttargetCwd: string,\n\tsessionDir?: string,\n\toptions?: NewSessionOptions,\n): ForkedSessionFile {\n\tconst resolvedSourcePath = resolvePath(sourcePath);\n\tconst resolvedTargetCwd = resolvePath(targetCwd);\n\tconst sourceEntries = loadEntriesFromFile(resolvedSourcePath);\n\tif (sourceEntries.length === 0) {\n\t\tthrow new Error(`Cannot fork: source session file is empty or invalid: ${resolvedSourcePath}`);\n\t}\n\n\tconst sourceHeader = sourceEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\tif (!sourceHeader) {\n\t\tthrow new Error(`Cannot fork: source session has no header: ${resolvedSourcePath}`);\n\t}\n\n\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(resolvedTargetCwd);\n\tensureDirectory(dir);\n\n\t// Create new session file with new ID but forked content\n\tif (options?.id !== undefined) {\n\t\tassertValidSessionId(options.id);\n\t}\n\tconst newSessionId = options?.id ?? createSessionId();\n\tconst timestamp = new Date().toISOString();\n\tconst newSessionFile = join(dir, `${timestamp.replace(/[:.]/g, \"-\")}_${newSessionId}.jsonl`);\n\n\t// Write new header pointing to source as parent, with updated cwd\n\tconst newHeader: SessionHeader = createSessionHeader(newSessionId, resolvedTargetCwd, timestamp, resolvedSourcePath);\n\tappendSessionEntry(newSessionFile, newHeader);\n\n\t// Copy all non-header entries from source\n\tfor (const entry of sourceEntries) {\n\t\tif (entry.type !== \"session\") {\n\t\t\tappendSessionEntry(newSessionFile, entry);\n\t\t}\n\t}\n\n\treturn { cwd: resolvedTargetCwd, sessionDir: dir, sessionFile: newSessionFile };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"session-manager-archive.js","sourceRoot":"","sources":["../../src/core/session-manager-archive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EACN,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AAStC,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAEpG,MAAM,UAAU,oBAAoB,CACnC,WAA+B,EAC/B,OAAoB,EACpB,KAAK,GAAG,SAAS;IAEjB,IAAI,CAAC,WAAW;QAAE,OAAO,SAAS,CAAC;IACnC,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,QAAQ,CAAC;IAC5G,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;IACjE,MAAM,UAAU,GAAG,GAAG,WAAW,IAAI,SAAS,IAAI,SAAS,MAAM,CAAC;IAClE,mBAAmB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACzC,OAAO,UAAU,CAAC;AACnB,CAAC;AAsBD,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AAI,KAAK,SAAS,EAAE,CAAC;YAC9B,kBAAkB,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;QAC3C,CAAC;IACF,CAAC;IAED,OAAO,EAAE,GAAG,EAAE,iBAAiB,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,cAAc,EAAE,CAAC;AACjF,CAAC","sourcesContent":["import { join } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport {\n\tcreateLabelEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n} from \"./session-manager-entries.ts\";\nimport { getDefaultSessionDir } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntry,\n\tensureDirectory,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tFileEntry,\n\tLabelEntry,\n\tNewSessionOptions,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId, generateId } from \"./session-manager-validation.ts\";\n\nexport function createBackupSnapshot(\n\tsessionFile: string | undefined,\n\tentries: FileEntry[],\n\tlabel = \"compact\",\n): string | undefined {\n\tif (!sessionFile) return undefined;\n\tconst safeLabel = label.replace(/[^a-z0-9_-]/gi, \"-\").replace(/-+/g, \"-\").replace(/^-|-$/g, \"\") || \"backup\";\n\tconst timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n\tconst backupPath = `${sessionFile}.${timestamp}.${safeLabel}.bak`;\n\twriteSessionEntries(backupPath, entries);\n\treturn backupPath;\n}\n\nexport interface BranchedSessionStateInput {\n\tleafId: string;\n\tpersist: boolean;\n\tsessionDir: string;\n\tcwd: string;\n\tpreviousSessionFile: string | undefined;\n\tworkflow?: SessionWorkflowMetadata;\n\tpath: SessionEntry[];\n\tlabelsById: ReadonlyMap<string, string>;\n\tlabelTimestampsById: ReadonlyMap<string, string>;\n}\n\nexport interface BranchedSessionState {\n\tsessionId: string;\n\tsessionFile: string | undefined;\n\tfileEntries: FileEntry[];\n\tshouldRewriteFile: boolean;\n\tflushed?: boolean;\n}\n\nfunction collectLabelsForPath(\n\tpathEntryIds: ReadonlySet<string>,\n\tlabelsById: ReadonlyMap<string, string>,\n\tlabelTimestampsById: ReadonlyMap<string, string>,\n): Array<{ targetId: string; label: string; timestamp: string }> {\n\tconst labelsToWrite: Array<{ targetId: string; label: string; timestamp: string }> = [];\n\tfor (const [targetId, label] of labelsById) {\n\t\tif (pathEntryIds.has(targetId)) {\n\t\t\tlabelsToWrite.push({ targetId, label, timestamp: labelTimestampsById.get(targetId)! });\n\t\t}\n\t}\n\treturn labelsToWrite;\n}\n\nfunction buildPersistedLabelEntries(\n\tpathEntryIds: Set<string>,\n\tlastEntryId: string | null,\n\tlabelsToWrite: Array<{ targetId: string; label: string; timestamp: string }>,\n): LabelEntry[] {\n\tlet parentId = lastEntryId;\n\tconst labelEntries: LabelEntry[] = [];\n\tfor (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {\n\t\tconst labelEntry = createLabelEntry(targetId, label, new Set(pathEntryIds), parentId, labelTimestamp);\n\t\tpathEntryIds.add(labelEntry.id);\n\t\tlabelEntries.push(labelEntry);\n\t\tparentId = labelEntry.id;\n\t}\n\treturn labelEntries;\n}\n\nfunction buildInMemoryLabelEntries(\n\tpathEntryIds: Set<string>,\n\tlastEntryId: string | null,\n\tlabelsToWrite: Array<{ targetId: string; label: string; timestamp: string }>,\n): LabelEntry[] {\n\tconst labelEntries: LabelEntry[] = [];\n\tlet parentId = lastEntryId;\n\tfor (const { targetId, label, timestamp: labelTimestamp } of labelsToWrite) {\n\t\tconst labelEntry: LabelEntry = {\n\t\t\ttype: \"label\",\n\t\t\tid: generateId(new Set([...pathEntryIds, ...labelEntries.map((e) => e.id)])),\n\t\t\tparentId,\n\t\t\ttimestamp: labelTimestamp,\n\t\t\ttargetId,\n\t\t\tlabel,\n\t\t};\n\t\tlabelEntries.push(labelEntry);\n\t\tparentId = labelEntry.id;\n\t}\n\treturn labelEntries;\n}\n\nexport function createBranchedSessionState(input: BranchedSessionStateInput): BranchedSessionState {\n\tif (input.path.length === 0) {\n\t\tthrow new Error(`Entry ${input.leafId} not found`);\n\t}\n\n\t// Filter out LabelEntry from path - we'll recreate them from the resolved map\n\tconst pathWithoutLabels = input.path.filter((entry) => entry.type !== \"label\");\n\tconst newSessionId = createSessionId();\n\tconst timestamp = new Date().toISOString();\n\tconst newSessionFile = createSessionFilePath(input.sessionDir, timestamp, newSessionId);\n\tconst header: SessionHeader = createSessionHeader(\n\t\tnewSessionId,\n\t\tinput.cwd,\n\t\ttimestamp,\n\t\tinput.persist ? input.previousSessionFile : undefined,\n\t\tinput.workflow !== undefined,\n\t\tinput.workflow,\n\t);\n\n\t// Collect labels for entries in the path\n\tconst pathEntryIds = new Set(pathWithoutLabels.map((entry) => entry.id));\n\tconst labelsToWrite = collectLabelsForPath(pathEntryIds, input.labelsById, input.labelTimestampsById);\n\tconst lastEntryId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;\n\tconst labelEntries = input.persist\n\t\t? buildPersistedLabelEntries(pathEntryIds, lastEntryId, labelsToWrite)\n\t\t: buildInMemoryLabelEntries(pathEntryIds, lastEntryId, labelsToWrite);\n\tconst fileEntries: FileEntry[] = [header, ...pathWithoutLabels, ...labelEntries];\n\n\tif (!input.persist) {\n\t\treturn { sessionId: newSessionId, sessionFile: undefined, fileEntries, shouldRewriteFile: false };\n\t}\n\n\tconst shouldRewriteFile = hasAssistantMessage(fileEntries);\n\treturn {\n\t\tsessionId: newSessionId,\n\t\tsessionFile: newSessionFile,\n\t\tfileEntries,\n\t\tshouldRewriteFile,\n\t\tflushed: shouldRewriteFile,\n\t};\n}\n\nexport interface ForkedSessionFile {\n\tcwd: string;\n\tsessionDir: string;\n\tsessionFile: string;\n}\n\nexport function forkSessionFromFile(\n\tsourcePath: string,\n\ttargetCwd: string,\n\tsessionDir?: string,\n\toptions?: NewSessionOptions,\n): ForkedSessionFile {\n\tconst resolvedSourcePath = resolvePath(sourcePath);\n\tconst resolvedTargetCwd = resolvePath(targetCwd);\n\tconst sourceEntries = loadEntriesFromFile(resolvedSourcePath);\n\tif (sourceEntries.length === 0) {\n\t\tthrow new Error(`Cannot fork: source session file is empty or invalid: ${resolvedSourcePath}`);\n\t}\n\n\tconst sourceHeader = sourceEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\tif (!sourceHeader) {\n\t\tthrow new Error(`Cannot fork: source session has no header: ${resolvedSourcePath}`);\n\t}\n\n\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(resolvedTargetCwd);\n\tensureDirectory(dir);\n\n\t// Create new session file with new ID but forked content\n\tif (options?.id !== undefined) {\n\t\tassertValidSessionId(options.id);\n\t}\n\tconst newSessionId = options?.id ?? createSessionId();\n\tconst timestamp = new Date().toISOString();\n\tconst newSessionFile = join(dir, `${timestamp.replace(/[:.]/g, \"-\")}_${newSessionId}.jsonl`);\n\n\t// Write new header pointing to source as parent, with updated cwd\n\tconst newHeader: SessionHeader = createSessionHeader(\n\t\tnewSessionId,\n\t\tresolvedTargetCwd,\n\t\ttimestamp,\n\t\tresolvedSourcePath,\n\t\toptions?.internal,\n\t\toptions?.workflow,\n\t);\n\tappendSessionEntry(newSessionFile, newHeader);\n\n\t// Copy all non-header entries from source\n\tfor (const entry of sourceEntries) {\n\t\tif (entry.type !== \"session\") {\n\t\t\tappendSessionEntry(newSessionFile, entry);\n\t\t}\n\t}\n\n\treturn { cwd: resolvedTargetCwd, sessionDir: dir, sessionFile: newSessionFile };\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SessionHeader, SessionWorkflowMetadata } from "./session-manager-types.ts";
|
|
2
|
+
export declare const WORKFLOW_SESSION_METADATA_ENV = "ATOMIC_WORKFLOW_SESSION_METADATA";
|
|
3
|
+
/** Returns valid workflow ownership metadata without rewriting its string values. */
|
|
4
|
+
export declare function validSessionWorkflowMetadata(value: unknown): SessionWorkflowMetadata | undefined;
|
|
5
|
+
/** A session is hidden from normal history only when both ownership markers are valid. */
|
|
6
|
+
export declare function classifiedWorkflowMetadata(header: Pick<SessionHeader, "internal" | "workflow"> | null | undefined): SessionWorkflowMetadata | undefined;
|
|
7
|
+
export declare function isClassifiedWorkflowSession(header: Pick<SessionHeader, "internal" | "workflow"> | null | undefined): boolean;
|
|
8
|
+
export declare function workflowSessionMetadataFromEnv(env?: Record<string, string | undefined>): SessionWorkflowMetadata | undefined;
|
|
9
|
+
//# sourceMappingURL=session-manager-classification.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-manager-classification.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-classification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAEzF,eAAO,MAAM,6BAA6B,qCAAqC,CAAC;AAMhF,qFAAqF;AACrF,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,OAAO,GAAG,uBAAuB,GAAG,SAAS,CAWhG;AAED,0FAA0F;AAC1F,wBAAgB,0BAA0B,CACzC,MAAM,EAAE,IAAI,CAAC,aAAa,EAAE,UAAU,GAAG,UAAU,CAAC,GAAG,IAAI,GAAG,SAAS,GACrE,uBAAuB,GAAG,SAAS,CAGrC;AAED,wBAAgB,2BAA2B,CAC1C,MAAM,EAAE,IAAI,CAAC,aAAa,EAAE,UAAU,GAAG,UAAU,CAAC,GAAG,IAAI,GAAG,SAAS,GACrE,OAAO,CAET;AAED,wBAAgB,8BAA8B,CAC7C,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,GACnD,uBAAuB,GAAG,SAAS,CAQrC","sourcesContent":["import type { SessionHeader, SessionWorkflowMetadata } from \"./session-manager-types.ts\";\n\nexport const WORKFLOW_SESSION_METADATA_ENV = \"ATOMIC_WORKFLOW_SESSION_METADATA\";\n\nfunction isNonEmptyString(value: unknown): value is string {\n\treturn typeof value === \"string\" && value.trim().length > 0;\n}\n\n/** Returns valid workflow ownership metadata without rewriting its string values. */\nexport function validSessionWorkflowMetadata(value: unknown): SessionWorkflowMetadata | undefined {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n\tconst metadata = value as Record<string, unknown>;\n\tif (\n\t\t!isNonEmptyString(metadata.runId)\n\t\t|| !isNonEmptyString(metadata.stageId)\n\t\t|| !isNonEmptyString(metadata.stageName)\n\t) {\n\t\treturn undefined;\n\t}\n\treturn value as SessionWorkflowMetadata;\n}\n\n/** A session is hidden from normal history only when both ownership markers are valid. */\nexport function classifiedWorkflowMetadata(\n\theader: Pick<SessionHeader, \"internal\" | \"workflow\"> | null | undefined,\n): SessionWorkflowMetadata | undefined {\n\tif (header?.internal !== true) return undefined;\n\treturn validSessionWorkflowMetadata(header.workflow);\n}\n\nexport function isClassifiedWorkflowSession(\n\theader: Pick<SessionHeader, \"internal\" | \"workflow\"> | null | undefined,\n): boolean {\n\treturn classifiedWorkflowMetadata(header) !== undefined;\n}\n\nexport function workflowSessionMetadataFromEnv(\n\tenv: Record<string, string | undefined> = process.env,\n): SessionWorkflowMetadata | undefined {\n\tconst serialized = env[WORKFLOW_SESSION_METADATA_ENV];\n\tif (!serialized) return undefined;\n\ttry {\n\t\treturn validSessionWorkflowMetadata(JSON.parse(serialized));\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const WORKFLOW_SESSION_METADATA_ENV = "ATOMIC_WORKFLOW_SESSION_METADATA";
|
|
2
|
+
function isNonEmptyString(value) {
|
|
3
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
4
|
+
}
|
|
5
|
+
/** Returns valid workflow ownership metadata without rewriting its string values. */
|
|
6
|
+
export function validSessionWorkflowMetadata(value) {
|
|
7
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
8
|
+
return undefined;
|
|
9
|
+
const metadata = value;
|
|
10
|
+
if (!isNonEmptyString(metadata.runId)
|
|
11
|
+
|| !isNonEmptyString(metadata.stageId)
|
|
12
|
+
|| !isNonEmptyString(metadata.stageName)) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
/** A session is hidden from normal history only when both ownership markers are valid. */
|
|
18
|
+
export function classifiedWorkflowMetadata(header) {
|
|
19
|
+
if (header?.internal !== true)
|
|
20
|
+
return undefined;
|
|
21
|
+
return validSessionWorkflowMetadata(header.workflow);
|
|
22
|
+
}
|
|
23
|
+
export function isClassifiedWorkflowSession(header) {
|
|
24
|
+
return classifiedWorkflowMetadata(header) !== undefined;
|
|
25
|
+
}
|
|
26
|
+
export function workflowSessionMetadataFromEnv(env = process.env) {
|
|
27
|
+
const serialized = env[WORKFLOW_SESSION_METADATA_ENV];
|
|
28
|
+
if (!serialized)
|
|
29
|
+
return undefined;
|
|
30
|
+
try {
|
|
31
|
+
return validSessionWorkflowMetadata(JSON.parse(serialized));
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=session-manager-classification.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-manager-classification.js","sourceRoot":"","sources":["../../src/core/session-manager-classification.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,6BAA6B,GAAG,kCAAkC,CAAC;AAEhF,SAAS,gBAAgB,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;AAC7D,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,4BAA4B,CAAC,KAAc;IAC1D,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAC1F,MAAM,QAAQ,GAAG,KAAgC,CAAC;IAClD,IACC,CAAC,gBAAgB,CAAC,QAAQ,CAAC,KAAK,CAAC;WAC9B,CAAC,gBAAgB,CAAC,QAAQ,CAAC,OAAO,CAAC;WACnC,CAAC,gBAAgB,CAAC,QAAQ,CAAC,SAAS,CAAC,EACvC,CAAC;QACF,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,OAAO,KAAgC,CAAC;AACzC,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,0BAA0B,CACzC,MAAuE;IAEvE,IAAI,MAAM,EAAE,QAAQ,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IAChD,OAAO,4BAA4B,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACtD,CAAC;AAED,MAAM,UAAU,2BAA2B,CAC1C,MAAuE;IAEvE,OAAO,0BAA0B,CAAC,MAAM,CAAC,KAAK,SAAS,CAAC;AACzD,CAAC;AAED,MAAM,UAAU,8BAA8B,CAC7C,GAAG,GAAuC,OAAO,CAAC,GAAG;IAErD,MAAM,UAAU,GAAG,GAAG,CAAC,6BAA6B,CAAC,CAAC;IACtD,IAAI,CAAC,UAAU;QAAE,OAAO,SAAS,CAAC;IAClC,IAAI,CAAC;QACJ,OAAO,4BAA4B,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC;IAC7D,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,SAAS,CAAC;IAClB,CAAC;AACF,CAAC","sourcesContent":["import type { SessionHeader, SessionWorkflowMetadata } from \"./session-manager-types.ts\";\n\nexport const WORKFLOW_SESSION_METADATA_ENV = \"ATOMIC_WORKFLOW_SESSION_METADATA\";\n\nfunction isNonEmptyString(value: unknown): value is string {\n\treturn typeof value === \"string\" && value.trim().length > 0;\n}\n\n/** Returns valid workflow ownership metadata without rewriting its string values. */\nexport function validSessionWorkflowMetadata(value: unknown): SessionWorkflowMetadata | undefined {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n\tconst metadata = value as Record<string, unknown>;\n\tif (\n\t\t!isNonEmptyString(metadata.runId)\n\t\t|| !isNonEmptyString(metadata.stageId)\n\t\t|| !isNonEmptyString(metadata.stageName)\n\t) {\n\t\treturn undefined;\n\t}\n\treturn value as SessionWorkflowMetadata;\n}\n\n/** A session is hidden from normal history only when both ownership markers are valid. */\nexport function classifiedWorkflowMetadata(\n\theader: Pick<SessionHeader, \"internal\" | \"workflow\"> | null | undefined,\n): SessionWorkflowMetadata | undefined {\n\tif (header?.internal !== true) return undefined;\n\treturn validSessionWorkflowMetadata(header.workflow);\n}\n\nexport function isClassifiedWorkflowSession(\n\theader: Pick<SessionHeader, \"internal\" | \"workflow\"> | null | undefined,\n): boolean {\n\treturn classifiedWorkflowMetadata(header) !== undefined;\n}\n\nexport function workflowSessionMetadataFromEnv(\n\tenv: Record<string, string | undefined> = process.env,\n): SessionWorkflowMetadata | undefined {\n\tconst serialized = env[WORKFLOW_SESSION_METADATA_ENV];\n\tif (!serialized) return undefined;\n\ttry {\n\t\treturn validSessionWorkflowMetadata(JSON.parse(serialized));\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ImageContent, Message, TextContent } from "@earendil-works/pi-ai/compat";
|
|
2
|
+
import type { VerbatimCompactionDetails } from "./compaction/compaction-types.js";
|
|
2
3
|
import type { BashExecutionMessage, CustomMessage } from "./messages.ts";
|
|
3
|
-
import type {
|
|
4
|
+
import type { NewSessionOptions, SessionContext, SessionEntry, SessionHeader, SessionInfo, SessionListProgress, SessionTreeNode, SessionWorkflowMetadata } from "./session-manager-types.ts";
|
|
4
5
|
/** Manages conversation sessions as append-only trees stored in JSONL files. Each session entry has an id and parentId forming a tree structure. The "leaf" pointer tracks the current position. Appending creates a child of the current leaf. Branching moves the leaf to an earlier entry, allowing new branches without modifying history. Use buildSessionContext() to get the resolved message list for the LLM, which applies context-deletion filtering and follows the path from root to current leaf. */
|
|
5
6
|
export declare class SessionManager {
|
|
6
7
|
private sessionId;
|
|
@@ -18,7 +19,7 @@ export declare class SessionManager {
|
|
|
18
19
|
/** Switch to a different session file (used for resume and branching) */
|
|
19
20
|
setSessionFile(sessionFile: string): void;
|
|
20
21
|
newSession(options?: NewSessionOptions): string | undefined;
|
|
21
|
-
/** Mark the session
|
|
22
|
+
/** Mark the session as workflow-owned, repairing malformed markers while preserving valid ownership. */
|
|
22
23
|
markSessionInternal(workflow?: SessionWorkflowMetadata): void;
|
|
23
24
|
private _buildIndex;
|
|
24
25
|
private _rewriteFile;
|
|
@@ -38,8 +39,7 @@ export declare class SessionManager {
|
|
|
38
39
|
appendContextWindowChange(contextWindow: number): string;
|
|
39
40
|
/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */
|
|
40
41
|
appendModelChange(provider: string, modelId: string): string;
|
|
41
|
-
|
|
42
|
-
appendContextCompaction(deletedTargets: ContextDeletionTarget[], protectedEntryIds: string[], stats: ContextCompactionStats, backupPath?: string): string;
|
|
42
|
+
appendCompaction(compactedText: string, firstKeptEntryId: string, tokensBefore: number, details: VerbatimCompactionDetails): string;
|
|
43
43
|
/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */
|
|
44
44
|
writeBackupSnapshot(label?: string): string | undefined;
|
|
45
45
|
/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-manager-core.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAIvF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAmCzE,OAAO,KAAK,EAEX,sBAAsB,EACtB,qBAAqB,EAErB,iBAAiB,EACjB,c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,GAAG,GAAE,MAAsB,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,cAAc,CAExF;IAED,8EAA8E;IAC9E,MAAM,CAAC,QAAQ,CACd,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,iBAAiB,GACzB,cAAc,CAGhB;IAED,+GAA+G;IAC/G,OAAa,IAAI,CAChB,GAAG,EAAE,MAAM,EACX,UAAU,CAAC,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,mBAAmB,EAChC,OAAO,CAAC,EAAE;QAAE,eAAe,CAAC,EAAE,OAAO,CAAA;KAAE,GACrC,OAAO,CAAC,WAAW,EAAE,CAAC,CAExB;IAED,sHAAsH;IACtH,OAAa,OAAO,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC/E,OAAa,OAAO,CACnB,UAAU,CAAC,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,mBAAmB,EAChC,OAAO,CAAC,EAAE;QAAE,eAAe,CAAC,EAAE,OAAO,CAAA;KAAE,GACrC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CAQ1B","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai/compat\";\nimport { existsSync, statSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateContextCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tContextCompactionStats,\n\tContextDeletionTarget,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/** Manages conversation sessions as append-only trees stored in JSONL files. Each session entry has an id and parentId forming a tree structure. The \"leaf\" pointer tracks the current position. Appending creates a child of the current leaf. Branching moves the leaf to an earlier entry, allowing new branches without modifying history. Use buildSessionContext() to get the resolved message list for the LLM, which applies context-deletion filtering and follows the path from root to current leaf. */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty, initialize it with a valid session header. If it was non-empty but did not parse as a pi session, fail without modifying it.\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tif (statSync(explicitPath).size > 0) {\n\t\t\t\t\tthrow new Error(`Session file is not a valid pi session: ${explicitPath}`);\n\t\t\t\t}\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(\n\t\t\tthis.sessionId,\n\t\t\tthis.cwd,\n\t\t\ttimestamp,\n\t\t\toptions?.parentSession,\n\t\t\toptions?.internal,\n\t\t\toptions?.workflow,\n\t\t);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.labelTimestampsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\t/** Mark the session header as internal (e.g. workflow stage). Preserves an existing full marker on reattach. */\n\tmarkSessionInternal(workflow?: SessionWorkflowMetadata): void {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!header || (header.internal && header.workflow)) return;\n\t\theader.internal = true;\n\t\tif (workflow) header.workflow = workflow;\n\t\tif (this.flushed) this._rewriteFile();\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append logical deletion metadata for deletion-only context compaction. */\n\tappendContextCompaction(\n\t\tdeletedTargets: ContextDeletionTarget[],\n\t\tprotectedEntryIds: string[],\n\t\tstats: ContextCompactionStats,\n\t\tbackupPath?: string,\n\t): string {\n\t\tconst entry = createContextCompactionEntry(deletedTargets, protectedEntryIds, stats, backupPath, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session (skips internal workflow sessions unless `includeInternal: true`). */\n\tstatic continueRecent(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: { includeInternal?: boolean },\n\t): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(\n\t\t\tdir,\n\t\t\tfilterCwd ? cwd : undefined,\n\t\t\toptions?.includeInternal === true,\n\t\t);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd(), options?: NewSessionOptions): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false, options);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List sessions for a directory. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async list(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress, options?.includeInternal === true);\n\t}\n\n\t/** List sessions across all directories. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress, options?.includeInternal === true);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"session-manager-core.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAIvF,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAuCzE,OAAO,KAAK,EAGX,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,WAAW,EACX,mBAAmB,EACnB,eAAe,EACf,uBAAuB,EACvB,MAAM,4BAA4B,CAAC;AAGpC,qfAAqf;AACrf,qBAAa,cAAc;IAC1B,OAAO,CAAC,SAAS,CAAc;IAC/B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,OAAO,CAAkB;IACjC,OAAO,CAAC,WAAW,CAAmB;IACtC,OAAO,CAAC,IAAI,CAAwC;IACpD,OAAO,CAAC,UAAU,CAAkC;IACpD,OAAO,CAAC,mBAAmB,CAAkC;IAC7D,OAAO,CAAC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U,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC/E,OAAa,OAAO,CACnB,UAAU,CAAC,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,mBAAmB,EAChC,OAAO,CAAC,EAAE;QAAE,eAAe,CAAC,EAAE,OAAO,CAAA;KAAE,GACrC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CAQ1B","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai/compat\";\nimport { existsSync, statSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { VerbatimCompactionDetails } from \"./compaction/compaction-types.js\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tclassifiedWorkflowMetadata,\n\tvalidSessionWorkflowMetadata,\n} from \"./session-manager-classification.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/** Manages conversation sessions as append-only trees stored in JSONL files. Each session entry has an id and parentId forming a tree structure. The \"leaf\" pointer tracks the current position. Appending creates a child of the current leaf. Branching moves the leaf to an earlier entry, allowing new branches without modifying history. Use buildSessionContext() to get the resolved message list for the LLM, which applies context-deletion filtering and follows the path from root to current leaf. */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty, initialize it with a valid session header. If it was non-empty but did not parse as a pi session, fail without modifying it.\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tif (statSync(explicitPath).size > 0) {\n\t\t\t\t\tthrow new Error(`Session file is not a valid pi session: ${explicitPath}`);\n\t\t\t\t}\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(\n\t\t\tthis.sessionId,\n\t\t\tthis.cwd,\n\t\t\ttimestamp,\n\t\t\toptions?.parentSession,\n\t\t\toptions?.internal,\n\t\t\toptions?.workflow,\n\t\t);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.labelTimestampsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\t/** Mark the session as workflow-owned, repairing malformed markers while preserving valid ownership. */\n\tmarkSessionInternal(workflow?: SessionWorkflowMetadata): void {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!header || classifiedWorkflowMetadata(header)) return;\n\t\tconst validWorkflow = validSessionWorkflowMetadata(workflow);\n\t\tif (!validWorkflow) return;\n\t\theader.internal = true;\n\t\theader.workflow = validWorkflow;\n\t\tif (this.flushed) this._rewriteFile();\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\tappendCompaction(compactedText: string, firstKeptEntryId: string, tokensBefore: number, details: VerbatimCompactionDetails): string {\n\t\tif (!this.byId.has(firstKeptEntryId)) throw new Error(`Entry ${firstKeptEntryId} not found`);\n\t\tconst entry = createCompactionEntry(compactedText, firstKeptEntryId, tokensBefore, details, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tworkflow: classifiedWorkflowMetadata(this.getHeader()),\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session (skips internal workflow sessions unless `includeInternal: true`). */\n\tstatic continueRecent(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: { includeInternal?: boolean },\n\t): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(\n\t\t\tdir,\n\t\t\tfilterCwd ? cwd : undefined,\n\t\t\toptions?.includeInternal === true,\n\t\t);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd(), options?: NewSessionOptions): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false, options);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List sessions for a directory. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async list(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress, options?.includeInternal === true);\n\t}\n\n\t/** List sessions across all directories. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress, options?.includeInternal === true);\n\t}\n}\n"]}
|
|
@@ -2,7 +2,8 @@ import { existsSync, statSync } from "fs";
|
|
|
2
2
|
import { resolve } from "path";
|
|
3
3
|
import { normalizePath, resolvePath } from "../utils/paths.js";
|
|
4
4
|
import { createBackupSnapshot, createBranchedSessionState, forkSessionFromFile, } from "./session-manager-archive.js";
|
|
5
|
-
import {
|
|
5
|
+
import { classifiedWorkflowMetadata, validSessionWorkflowMetadata, } from "./session-manager-classification.js";
|
|
6
|
+
import { createBranchSummaryEntry, createCompactionEntry, createContextWindowChangeEntry, createCustomEntry, createCustomMessageEntry, createLabelEntry, getEntriesWithoutHeader, createMessageEntry, createModelChangeEntry, createSessionFilePath, createSessionHeader, createSessionInfoEntry, createThinkingLevelChangeEntry, getLatestSessionName, } from "./session-manager-entries.js";
|
|
6
7
|
import { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from "./session-manager-history.js";
|
|
7
8
|
import { listAllSessions, listProjectSessions } from "./session-manager-list.js";
|
|
8
9
|
import { migrateToCurrentVersion } from "./session-manager-migrations.js";
|
|
@@ -81,14 +82,16 @@ export class SessionManager {
|
|
|
81
82
|
}
|
|
82
83
|
return this.sessionFile;
|
|
83
84
|
}
|
|
84
|
-
/** Mark the session
|
|
85
|
+
/** Mark the session as workflow-owned, repairing malformed markers while preserving valid ownership. */
|
|
85
86
|
markSessionInternal(workflow) {
|
|
86
87
|
const header = this.fileEntries.find((entry) => entry.type === "session");
|
|
87
|
-
if (!header || (header
|
|
88
|
+
if (!header || classifiedWorkflowMetadata(header))
|
|
89
|
+
return;
|
|
90
|
+
const validWorkflow = validSessionWorkflowMetadata(workflow);
|
|
91
|
+
if (!validWorkflow)
|
|
88
92
|
return;
|
|
89
93
|
header.internal = true;
|
|
90
|
-
|
|
91
|
-
header.workflow = workflow;
|
|
94
|
+
header.workflow = validWorkflow;
|
|
92
95
|
if (this.flushed)
|
|
93
96
|
this._rewriteFile();
|
|
94
97
|
}
|
|
@@ -168,9 +171,10 @@ export class SessionManager {
|
|
|
168
171
|
this._appendEntry(entry);
|
|
169
172
|
return entry.id;
|
|
170
173
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
+
appendCompaction(compactedText, firstKeptEntryId, tokensBefore, details) {
|
|
175
|
+
if (!this.byId.has(firstKeptEntryId))
|
|
176
|
+
throw new Error(`Entry ${firstKeptEntryId} not found`);
|
|
177
|
+
const entry = createCompactionEntry(compactedText, firstKeptEntryId, tokensBefore, details, this.byId, this.leafId);
|
|
174
178
|
this._appendEntry(entry);
|
|
175
179
|
return entry.id;
|
|
176
180
|
}
|
|
@@ -292,6 +296,7 @@ export class SessionManager {
|
|
|
292
296
|
sessionDir: this.getSessionDir(),
|
|
293
297
|
cwd: this.cwd,
|
|
294
298
|
previousSessionFile: this.sessionFile,
|
|
299
|
+
workflow: classifiedWorkflowMetadata(this.getHeader()),
|
|
295
300
|
path: this.getBranch(leafId),
|
|
296
301
|
labelsById: this.labelsById,
|
|
297
302
|
labelTimestampsById: this.labelTimestampsById,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-manager-core.js","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EACN,oBAAoB,EACpB,0BAA0B,EAC1B,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACN,wBAAwB,EACxB,4BAA4B,EAC5B,8BAA8B,EAC9B,iBAAiB,EACjB,wBAAwB,EACxB,gBAAgB,EAChB,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,mBAAmB,EACnB,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/pi-ai/compat\";\nimport { existsSync, statSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateContextCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tContextCompactionStats,\n\tContextDeletionTarget,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/** Manages conversation sessions as append-only trees stored in JSONL files. Each session entry has an id and parentId forming a tree structure. The \"leaf\" pointer tracks the current position. Appending creates a child of the current leaf. Branching moves the leaf to an earlier entry, allowing new branches without modifying history. Use buildSessionContext() to get the resolved message list for the LLM, which applies context-deletion filtering and follows the path from root to current leaf. */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty, initialize it with a valid session header. If it was non-empty but did not parse as a pi session, fail without modifying it.\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tif (statSync(explicitPath).size > 0) {\n\t\t\t\t\tthrow new Error(`Session file is not a valid pi session: ${explicitPath}`);\n\t\t\t\t}\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(\n\t\t\tthis.sessionId,\n\t\t\tthis.cwd,\n\t\t\ttimestamp,\n\t\t\toptions?.parentSession,\n\t\t\toptions?.internal,\n\t\t\toptions?.workflow,\n\t\t);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.labelTimestampsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\t/** Mark the session header as internal (e.g. workflow stage). Preserves an existing full marker on reattach. */\n\tmarkSessionInternal(workflow?: SessionWorkflowMetadata): void {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!header || (header.internal && header.workflow)) return;\n\t\theader.internal = true;\n\t\tif (workflow) header.workflow = workflow;\n\t\tif (this.flushed) this._rewriteFile();\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append logical deletion metadata for deletion-only context compaction. */\n\tappendContextCompaction(\n\t\tdeletedTargets: ContextDeletionTarget[],\n\t\tprotectedEntryIds: string[],\n\t\tstats: ContextCompactionStats,\n\t\tbackupPath?: string,\n\t): string {\n\t\tconst entry = createContextCompactionEntry(deletedTargets, protectedEntryIds, stats, backupPath, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session (skips internal workflow sessions unless `includeInternal: true`). */\n\tstatic continueRecent(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: { includeInternal?: boolean },\n\t): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(\n\t\t\tdir,\n\t\t\tfilterCwd ? cwd : undefined,\n\t\t\toptions?.includeInternal === true,\n\t\t);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd(), options?: NewSessionOptions): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false, options);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List sessions for a directory. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async list(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress, options?.includeInternal === true);\n\t}\n\n\t/** List sessions across all directories. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress, options?.includeInternal === true);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"session-manager-core.js","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAG/D,OAAO,EACN,oBAAoB,EACpB,0BAA0B,EAC1B,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACN,0BAA0B,EAC1B,4BAA4B,GAC5B,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AAC,KAAK,CAAC,IAAI,CAChB,GAAW,EACX,UAAmB,EACnB,UAAgC,EAChC,OAAuC;QAEvC,OAAO,mBAAmB,CAAC,GAAG,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC,CAAC;IAC5F,CAAC;IASD,MAAM,CAAC,KAAK,CAAC,OAAO,CACnB,sBAAqD,EACrD,UAAgC,EAChC,OAAuC;QAEvC,OAAO,eAAe,CAAC,sBAAsB,EAAE,UAAU,EAAE,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC,CAAC;IAC/F,CAAC;CACD","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai/compat\";\nimport { existsSync, statSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { VerbatimCompactionDetails } from \"./compaction/compaction-types.js\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tclassifiedWorkflowMetadata,\n\tvalidSessionWorkflowMetadata,\n} from \"./session-manager-classification.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/** Manages conversation sessions as append-only trees stored in JSONL files. Each session entry has an id and parentId forming a tree structure. The \"leaf\" pointer tracks the current position. Appending creates a child of the current leaf. Branching moves the leaf to an earlier entry, allowing new branches without modifying history. Use buildSessionContext() to get the resolved message list for the LLM, which applies context-deletion filtering and follows the path from root to current leaf. */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty, initialize it with a valid session header. If it was non-empty but did not parse as a pi session, fail without modifying it.\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tif (statSync(explicitPath).size > 0) {\n\t\t\t\t\tthrow new Error(`Session file is not a valid pi session: ${explicitPath}`);\n\t\t\t\t}\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(\n\t\t\tthis.sessionId,\n\t\t\tthis.cwd,\n\t\t\ttimestamp,\n\t\t\toptions?.parentSession,\n\t\t\toptions?.internal,\n\t\t\toptions?.workflow,\n\t\t);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.labelTimestampsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\t/** Mark the session as workflow-owned, repairing malformed markers while preserving valid ownership. */\n\tmarkSessionInternal(workflow?: SessionWorkflowMetadata): void {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!header || classifiedWorkflowMetadata(header)) return;\n\t\tconst validWorkflow = validSessionWorkflowMetadata(workflow);\n\t\tif (!validWorkflow) return;\n\t\theader.internal = true;\n\t\theader.workflow = validWorkflow;\n\t\tif (this.flushed) this._rewriteFile();\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\tappendCompaction(compactedText: string, firstKeptEntryId: string, tokensBefore: number, details: VerbatimCompactionDetails): string {\n\t\tif (!this.byId.has(firstKeptEntryId)) throw new Error(`Entry ${firstKeptEntryId} not found`);\n\t\tconst entry = createCompactionEntry(compactedText, firstKeptEntryId, tokensBefore, details, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tworkflow: classifiedWorkflowMetadata(this.getHeader()),\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session (skips internal workflow sessions unless `includeInternal: true`). */\n\tstatic continueRecent(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: { includeInternal?: boolean },\n\t): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(\n\t\t\tdir,\n\t\t\tfilterCwd ? cwd : undefined,\n\t\t\toptions?.includeInternal === true,\n\t\t);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd(), options?: NewSessionOptions): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false, options);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List sessions for a directory. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async list(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress, options?.includeInternal === true);\n\t}\n\n\t/** List sessions across all directories. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress, options?.includeInternal === true);\n\t}\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ImageContent, Message, TextContent } from "@earendil-works/pi-ai/compat";
|
|
2
2
|
import type { BashExecutionMessage, CustomMessage } from "./messages.ts";
|
|
3
|
-
import
|
|
3
|
+
import type { VerbatimCompactionDetails } from "./compaction/compaction-types.js";
|
|
4
|
+
import { type BranchSummaryEntry, type CompactionEntry, type ContextWindowChangeEntry, type CustomEntry, type CustomMessageEntry, type FileEntry, type LabelEntry, type ModelChangeEntry, type SessionEntry, type SessionHeader, type SessionInfoEntry, type SessionMessageEntry, type SessionWorkflowMetadata, type ThinkingLevelChangeEntry } from "./session-manager-types.ts";
|
|
4
5
|
export declare function createSessionHeader(id: string, cwd: string, timestamp?: string, parentSession?: string, internal?: boolean, workflow?: SessionWorkflowMetadata): SessionHeader;
|
|
5
6
|
export declare function createSessionFilePath(sessionDir: string, timestamp: string, sessionId: string): string;
|
|
6
7
|
export declare function createMessageEntry(message: Message | CustomMessage | BashExecutionMessage, byId: {
|
|
@@ -15,9 +16,9 @@ export declare function createContextWindowChangeEntry(contextWindow: number, by
|
|
|
15
16
|
export declare function createModelChangeEntry(provider: string, modelId: string, byId: {
|
|
16
17
|
has(id: string): boolean;
|
|
17
18
|
}, parentId: string | null): ModelChangeEntry;
|
|
18
|
-
export declare function
|
|
19
|
+
export declare function createCompactionEntry(compactedText: string, firstKeptEntryId: string, tokensBefore: number, details: VerbatimCompactionDetails, byId: {
|
|
19
20
|
has(id: string): boolean;
|
|
20
|
-
}, parentId: string | null):
|
|
21
|
+
}, parentId: string | null): CompactionEntry<VerbatimCompactionDetails>;
|
|
21
22
|
export declare function createCustomEntry(customType: string, data: unknown, byId: {
|
|
22
23
|
has(id: string): boolean;
|
|
23
24
|
}, parentId: string | null): CustomEntry;
|