@draht/coding-agent 2026.5.12 → 2026.7.7-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 +47 -0
- package/README.md +1 -4
- package/bin/draht-tools.cjs +3756 -1
- package/dist/bun/cli.d.ts.map +1 -1
- package/dist/bun/cli.js +4 -2
- package/dist/bun/cli.js.map +1 -1
- package/dist/bun/register-bedrock.d.ts.map +1 -1
- package/dist/bun/register-bedrock.js +1 -1
- package/dist/bun/register-bedrock.js.map +1 -1
- package/dist/bun/restore-sandbox-env.d.ts +17 -0
- package/dist/bun/restore-sandbox-env.d.ts.map +1 -0
- package/dist/bun/restore-sandbox-env.js +36 -0
- package/dist/bun/restore-sandbox-env.js.map +1 -0
- package/dist/cli/args.d.ts +9 -4
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +91 -38
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/config-selector.d.ts +5 -3
- package/dist/cli/config-selector.d.ts.map +1 -1
- package/dist/cli/config-selector.js +1 -1
- package/dist/cli/config-selector.js.map +1 -1
- package/dist/cli/file-processor.d.ts.map +1 -1
- package/dist/cli/file-processor.js +13 -26
- package/dist/cli/file-processor.js.map +1 -1
- package/dist/cli/initial-message.d.ts +1 -1
- package/dist/cli/initial-message.d.ts.map +1 -1
- package/dist/cli/initial-message.js.map +1 -1
- package/dist/cli/list-models.d.ts +1 -1
- package/dist/cli/list-models.d.ts.map +1 -1
- package/dist/cli/list-models.js +7 -1
- package/dist/cli/list-models.js.map +1 -1
- package/dist/cli/project-trust.d.ts +10 -0
- package/dist/cli/project-trust.d.ts.map +1 -0
- package/dist/cli/project-trust.js +48 -0
- package/dist/cli/project-trust.js.map +1 -0
- package/dist/cli/session-picker.d.ts +3 -2
- package/dist/cli/session-picker.d.ts.map +1 -1
- package/dist/cli/session-picker.js +5 -4
- package/dist/cli/session-picker.js.map +1 -1
- package/dist/cli/startup-ui.d.ts +20 -0
- package/dist/cli/startup-ui.d.ts.map +1 -0
- package/dist/cli/startup-ui.js +184 -0
- package/dist/cli/startup-ui.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +7 -4
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +27 -13
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +284 -63
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts +45 -9
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +100 -33
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts +6 -4
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +7 -4
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +55 -33
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +560 -492
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-guidance.d.ts +5 -0
- package/dist/core/auth-guidance.d.ts.map +1 -0
- package/dist/core/auth-guidance.js +21 -0
- package/dist/core/auth-guidance.js.map +1 -0
- package/dist/core/auth-storage.d.ts +21 -13
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +48 -28
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/bash-executor.d.ts +1 -15
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +10 -24
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/builtins/subagent.d.ts.map +1 -1
- package/dist/core/builtins/subagent.js +1 -2
- package/dist/core/builtins/subagent.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +9 -5
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +10 -4
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts +8 -7
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +56 -47
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/compaction/index.d.ts +3 -3
- package/dist/core/compaction/index.d.ts.map +1 -1
- package/dist/core/compaction/index.js.map +1 -1
- package/dist/core/compaction/utils.d.ts +1 -1
- package/dist/core/compaction/utils.d.ts.map +1 -1
- package/dist/core/compaction/utils.js +1 -1
- package/dist/core/compaction/utils.js.map +1 -1
- package/dist/core/exec.d.ts.map +1 -1
- package/dist/core/exec.js.map +1 -1
- package/dist/core/experimental.d.ts +2 -0
- package/dist/core/experimental.d.ts.map +1 -0
- package/dist/core/experimental.js +4 -0
- package/dist/core/experimental.js.map +1 -0
- package/dist/core/export-html/ansi-to-html.d.ts.map +1 -1
- package/dist/core/export-html/ansi-to-html.js +1 -1
- package/dist/core/export-html/ansi-to-html.js.map +1 -1
- package/dist/core/export-html/index.d.ts +1 -1
- package/dist/core/export-html/index.d.ts.map +1 -1
- package/dist/core/export-html/index.js +13 -11
- package/dist/core/export-html/index.js.map +1 -1
- package/dist/core/export-html/template.css +69 -4
- package/dist/core/export-html/template.js +231 -57
- package/dist/core/export-html/tool-renderer.d.ts +0 -6
- package/dist/core/export-html/tool-renderer.d.ts.map +1 -1
- package/dist/core/export-html/tool-renderer.js +15 -2
- package/dist/core/export-html/tool-renderer.js.map +1 -1
- package/dist/core/export-html/vendor/marked.min.js +75 -3
- package/dist/core/extensions/index.d.ts +8 -8
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts +5 -6
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +98 -43
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +31 -12
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +248 -33
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +191 -36
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/extensions/wrapper.d.ts.map +1 -1
- package/dist/core/extensions/wrapper.js +2 -2
- package/dist/core/extensions/wrapper.js.map +1 -1
- package/dist/core/footer-data-provider.d.ts +7 -1
- package/dist/core/footer-data-provider.d.ts.map +1 -1
- package/dist/core/footer-data-provider.js +78 -54
- package/dist/core/footer-data-provider.js.map +1 -1
- package/dist/core/http-dispatcher.d.ts +22 -0
- package/dist/core/http-dispatcher.d.ts.map +1 -0
- package/dist/core/http-dispatcher.js +91 -0
- package/dist/core/http-dispatcher.js.map +1 -0
- package/dist/core/index.d.ts +9 -8
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +2 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/keybindings.d.ts +67 -2
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +59 -7
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/model-registry.d.ts +24 -5
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +188 -51
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts +12 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +84 -39
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/output-guard.d.ts +1 -0
- package/dist/core/output-guard.d.ts.map +1 -1
- package/dist/core/output-guard.js +52 -22
- package/dist/core/output-guard.js.map +1 -1
- package/dist/core/package-manager.d.ts +22 -4
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +455 -157
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/project-trust.d.ts +15 -0
- package/dist/core/project-trust.d.ts.map +1 -0
- package/dist/core/project-trust.js +59 -0
- package/dist/core/project-trust.js.map +1 -0
- package/dist/core/prompt-templates.d.ts +13 -11
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +67 -76
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/provider-attribution.d.ts +4 -0
- package/dist/core/provider-attribution.d.ts.map +1 -0
- package/dist/core/provider-attribution.js +72 -0
- package/dist/core/provider-attribution.js.map +1 -0
- package/dist/core/provider-display-names.d.ts +2 -0
- package/dist/core/provider-display-names.d.ts.map +1 -0
- package/dist/core/provider-display-names.js +36 -0
- package/dist/core/provider-display-names.js.map +1 -0
- package/dist/core/resolve-config-value.d.ts +13 -6
- package/dist/core/resolve-config-value.d.ts.map +1 -1
- package/dist/core/resolve-config-value.js +143 -22
- package/dist/core/resolve-config-value.js.map +1 -1
- package/dist/core/resource-loader.d.ts +36 -15
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +178 -79
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/sdk.d.ts +34 -18
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +47 -16
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts +32 -14
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +366 -209
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +62 -6
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +230 -40
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts +10 -10
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +13 -35
- package/dist/core/skills.js.map +1 -1
- package/dist/core/slash-commands.d.ts +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +7 -4
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/core/source-info.d.ts +1 -1
- package/dist/core/source-info.d.ts.map +1 -1
- package/dist/core/source-info.js.map +1 -1
- package/dist/core/system-prompt.d.ts +4 -4
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +20 -16
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/telemetry.d.ts +3 -0
- package/dist/core/telemetry.d.ts.map +1 -0
- package/dist/core/telemetry.js +9 -0
- package/dist/core/telemetry.js.map +1 -0
- package/dist/core/timings.d.ts +4 -2
- package/dist/core/timings.d.ts.map +1 -1
- package/dist/core/timings.js +24 -14
- package/dist/core/timings.js.map +1 -1
- package/dist/core/tools/bash.d.ts +11 -16
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +187 -178
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit-diff.d.ts +26 -5
- package/dist/core/tools/edit-diff.d.ts.map +1 -1
- package/dist/core/tools/edit-diff.js +110 -23
- package/dist/core/tools/edit-diff.js.map +1 -1
- package/dist/core/tools/edit.d.ts +22 -24
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +201 -113
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/file-mutation-queue.d.ts.map +1 -1
- package/dist/core/tools/file-mutation-queue.js +27 -12
- package/dist/core/tools/file-mutation-queue.js.map +1 -1
- package/dist/core/tools/find.d.ts +7 -18
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +148 -101
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts +11 -30
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +23 -12
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/index.d.ts +26 -101
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/index.js +78 -52
- package/dist/core/tools/index.js.map +1 -1
- package/dist/core/tools/ls.d.ts +6 -15
- package/dist/core/tools/ls.d.ts.map +1 -1
- package/dist/core/tools/ls.js +12 -17
- package/dist/core/tools/ls.js.map +1 -1
- package/dist/core/tools/output-accumulator.d.ts +52 -0
- package/dist/core/tools/output-accumulator.d.ts.map +1 -0
- package/dist/core/tools/output-accumulator.js +184 -0
- package/dist/core/tools/output-accumulator.js.map +1 -0
- package/dist/core/tools/path-utils.d.ts +2 -0
- package/dist/core/tools/path-utils.d.ts.map +1 -1
- package/dist/core/tools/path-utils.js +40 -22
- package/dist/core/tools/path-utils.js.map +1 -1
- package/dist/core/tools/read.d.ts +7 -18
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +103 -52
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/render-utils.d.ts +5 -2
- package/dist/core/tools/render-utils.d.ts.map +1 -1
- package/dist/core/tools/render-utils.js +18 -2
- package/dist/core/tools/render-utils.js.map +1 -1
- package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -1
- package/dist/core/tools/tool-definition-wrapper.js +2 -0
- package/dist/core/tools/tool-definition-wrapper.js.map +1 -1
- package/dist/core/tools/truncate.d.ts.map +1 -1
- package/dist/core/tools/truncate.js +12 -2
- package/dist/core/tools/truncate.js.map +1 -1
- package/dist/core/tools/write.d.ts +5 -14
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +27 -46
- package/dist/core/tools/write.js.map +1 -1
- package/dist/core/trust-manager.d.ts +36 -0
- package/dist/core/trust-manager.d.ts.map +1 -0
- package/dist/core/trust-manager.js +202 -0
- package/dist/core/trust-manager.js.map +1 -0
- package/dist/index.d.ts +33 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -9
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts +5 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +208 -93
- package/dist/main.js.map +1 -1
- package/dist/migrations.d.ts +2 -2
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +4 -4
- package/dist/migrations.js.map +1 -1
- package/dist/modes/index.d.ts +5 -5
- package/dist/modes/index.d.ts.map +1 -1
- package/dist/modes/index.js.map +1 -1
- package/dist/modes/interactive/assets/clankolas.png +0 -0
- package/dist/modes/interactive/components/assistant-message.d.ts +5 -1
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +37 -15
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/bash-execution.d.ts +1 -1
- package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/bash-execution.js +3 -3
- package/dist/modes/interactive/components/bash-execution.js.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.js +3 -0
- package/dist/modes/interactive/components/bordered-loader.js.map +1 -1
- package/dist/modes/interactive/components/config-selector.d.ts +36 -5
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/config-selector.js +318 -36
- package/dist/modes/interactive/components/config-selector.js.map +1 -1
- package/dist/modes/interactive/components/countdown-timer.d.ts +2 -2
- package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -1
- package/dist/modes/interactive/components/countdown-timer.js +2 -2
- package/dist/modes/interactive/components/countdown-timer.js.map +1 -1
- package/dist/modes/interactive/components/custom-editor.d.ts +1 -1
- package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
- package/dist/modes/interactive/components/custom-editor.js.map +1 -1
- package/dist/modes/interactive/components/custom-entry.d.ts +19 -0
- package/dist/modes/interactive/components/custom-entry.d.ts.map +1 -0
- package/dist/modes/interactive/components/custom-entry.js +52 -0
- package/dist/modes/interactive/components/custom-entry.js.map +1 -0
- package/dist/modes/interactive/components/custom-message.d.ts +2 -2
- package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/custom-message.js.map +1 -1
- package/dist/modes/interactive/components/diff.d.ts.map +1 -1
- package/dist/modes/interactive/components/diff.js.map +1 -1
- package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -1
- package/dist/modes/interactive/components/earendil-announcement.js.map +1 -1
- package/dist/modes/interactive/components/extension-editor.d.ts +3 -1
- package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -1
- package/dist/modes/interactive/components/extension-editor.js +26 -9
- package/dist/modes/interactive/components/extension-editor.js.map +1 -1
- package/dist/modes/interactive/components/extension-input.d.ts.map +1 -1
- package/dist/modes/interactive/components/extension-input.js.map +1 -1
- package/dist/modes/interactive/components/extension-selector.d.ts +2 -0
- package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/extension-selector.js +8 -3
- package/dist/modes/interactive/components/extension-selector.js.map +1 -1
- package/dist/modes/interactive/components/first-time-setup.d.ts +25 -0
- package/dist/modes/interactive/components/first-time-setup.d.ts.map +1 -0
- package/dist/modes/interactive/components/first-time-setup.js +103 -0
- package/dist/modes/interactive/components/first-time-setup.js.map +1 -0
- package/dist/modes/interactive/components/footer.d.ts +4 -3
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +27 -7
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/index.d.ts +33 -31
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +2 -0
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/keybinding-hints.d.ts +5 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -1
- package/dist/modes/interactive/components/keybinding-hints.js +19 -5
- package/dist/modes/interactive/components/keybinding-hints.js.map +1 -1
- package/dist/modes/interactive/components/login-dialog.d.ts +12 -2
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
- package/dist/modes/interactive/components/login-dialog.js +45 -11
- package/dist/modes/interactive/components/login-dialog.js.map +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts +2 -2
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +10 -6
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts +18 -6
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.js +93 -25
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.d.ts +5 -12
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.js +49 -31
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
- package/dist/modes/interactive/components/session-selector.d.ts +2 -2
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector.js +33 -14
- package/dist/modes/interactive/components/session-selector.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +17 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +301 -20
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +1 -1
- package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -1
- package/dist/modes/interactive/components/status-indicator.d.ts +28 -0
- package/dist/modes/interactive/components/status-indicator.d.ts.map +1 -0
- package/dist/modes/interactive/components/status-indicator.js +60 -0
- package/dist/modes/interactive/components/status-indicator.js.map +1 -0
- package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/thinking-selector.js +1 -1
- package/dist/modes/interactive/components/thinking-selector.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts +6 -1
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +57 -14
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/components/tree-selector.d.ts +1 -1
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/tree-selector.js +137 -13
- package/dist/modes/interactive/components/tree-selector.js.map +1 -1
- package/dist/modes/interactive/components/trust-selector.d.ts +23 -0
- package/dist/modes/interactive/components/trust-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/trust-selector.js +91 -0
- package/dist/modes/interactive/components/trust-selector.js.map +1 -0
- package/dist/modes/interactive/components/user-message-selector.d.ts +2 -2
- package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/user-message-selector.js +8 -7
- package/dist/modes/interactive/components/user-message-selector.js.map +1 -1
- package/dist/modes/interactive/components/user-message.d.ts +6 -1
- package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/user-message.js +22 -8
- package/dist/modes/interactive/components/user-message.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +76 -19
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +1294 -494
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/model-search.d.ts +12 -0
- package/dist/modes/interactive/model-search.d.ts.map +1 -0
- package/dist/modes/interactive/model-search.js +15 -0
- package/dist/modes/interactive/model-search.js.map +1 -0
- package/dist/modes/interactive/theme/dark.json +5 -4
- package/dist/modes/interactive/theme/light.json +5 -4
- package/dist/modes/interactive/theme/theme-controller.d.ts +29 -0
- package/dist/modes/interactive/theme/theme-controller.d.ts.map +1 -0
- package/dist/modes/interactive/theme/theme-controller.js +102 -0
- package/dist/modes/interactive/theme/theme-controller.js.map +1 -0
- package/dist/modes/interactive/theme/theme-schema.json +2 -1
- package/dist/modes/interactive/theme/theme.d.ts +41 -3
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +183 -106
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +40 -20
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +30 -5
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +87 -8
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts +2 -2
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +113 -31
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +42 -4
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/package-manager-cli.d.ts +6 -2
- package/dist/package-manager-cli.d.ts.map +1 -1
- package/dist/package-manager-cli.js +504 -25
- package/dist/package-manager-cli.js.map +1 -1
- package/dist/prompts/agents/build.md +38 -0
- package/dist/prompts/agents/plan.md +36 -0
- package/dist/prompts/agents/verify.md +36 -4
- package/dist/prompts/commands/brainstorm.md +162 -0
- package/dist/prompts/commands/discuss-phase.md +1 -1
- package/dist/prompts/commands/execute-phase.md +4 -1
- package/dist/prompts/commands/fix.md +12 -0
- package/dist/prompts/commands/init-project.md +1 -0
- package/dist/prompts/commands/map-codebase.md +15 -1
- package/dist/prompts/commands/new-project.md +1 -0
- package/dist/prompts/commands/next-milestone.md +2 -0
- package/dist/prompts/commands/orchestrate.md +4 -4
- package/dist/prompts/commands/plan-phase.md +5 -0
- package/dist/prompts/commands/quick.md +4 -0
- package/dist/prompts/commands/review.md +4 -1
- package/dist/prompts/commands/verify-work.md +10 -8
- package/dist/rpc-entry.d.ts +3 -0
- package/dist/rpc-entry.d.ts.map +1 -0
- package/dist/rpc-entry.js +10 -0
- package/dist/rpc-entry.js.map +1 -0
- package/dist/utils/ansi.d.ts +2 -0
- package/dist/utils/ansi.d.ts.map +1 -0
- package/dist/utils/ansi.js +52 -0
- package/dist/utils/ansi.js.map +1 -0
- package/dist/utils/changelog.d.ts +2 -1
- package/dist/utils/changelog.d.ts.map +1 -1
- package/dist/utils/changelog.js +78 -0
- package/dist/utils/changelog.js.map +1 -1
- package/dist/utils/child-process.d.ts +12 -5
- package/dist/utils/child-process.d.ts.map +1 -1
- package/dist/utils/child-process.js +33 -5
- package/dist/utils/child-process.js.map +1 -1
- package/dist/utils/clipboard-image.d.ts.map +1 -1
- package/dist/utils/clipboard-image.js +2 -2
- package/dist/utils/clipboard-image.js.map +1 -1
- package/dist/utils/clipboard-native.d.ts +3 -1
- package/dist/utils/clipboard-native.d.ts.map +1 -1
- package/dist/utils/clipboard-native.js +14 -8
- package/dist/utils/clipboard-native.js.map +1 -1
- package/dist/utils/clipboard.d.ts.map +1 -1
- package/dist/utils/clipboard.js +84 -45
- package/dist/utils/clipboard.js.map +1 -1
- package/dist/utils/deprecation.d.ts +4 -0
- package/dist/utils/deprecation.d.ts.map +1 -0
- package/dist/utils/deprecation.js +13 -0
- package/dist/utils/deprecation.js.map +1 -0
- package/dist/utils/exif-orientation.d.ts +1 -1
- package/dist/utils/exif-orientation.d.ts.map +1 -1
- package/dist/utils/exif-orientation.js.map +1 -1
- package/dist/utils/fs-watch.d.ts +5 -0
- package/dist/utils/fs-watch.d.ts.map +1 -0
- package/dist/utils/fs-watch.js +25 -0
- package/dist/utils/fs-watch.js.map +1 -0
- package/dist/utils/git.d.ts.map +1 -1
- package/dist/utils/git.js +54 -22
- package/dist/utils/git.js.map +1 -1
- package/dist/utils/html.d.ts +7 -0
- package/dist/utils/html.d.ts.map +1 -0
- package/dist/utils/html.js +40 -0
- package/dist/utils/html.js.map +1 -0
- package/dist/utils/image-convert.d.ts +1 -0
- package/dist/utils/image-convert.d.ts.map +1 -1
- package/dist/utils/image-convert.js +21 -15
- package/dist/utils/image-convert.js.map +1 -1
- package/dist/utils/image-process.d.ts +18 -0
- package/dist/utils/image-process.d.ts.map +1 -0
- package/dist/utils/image-process.js +83 -0
- package/dist/utils/image-process.js.map +1 -0
- package/dist/utils/image-resize-core.d.ts +30 -0
- package/dist/utils/image-resize-core.d.ts.map +1 -0
- package/dist/utils/image-resize-core.js +124 -0
- package/dist/utils/image-resize-core.js.map +1 -0
- package/dist/utils/image-resize-worker.d.ts +2 -0
- package/dist/utils/image-resize-worker.d.ts.map +1 -0
- package/dist/utils/image-resize-worker.js +31 -0
- package/dist/utils/image-resize-worker.js.map +1 -0
- package/dist/utils/image-resize.d.ts +7 -27
- package/dist/utils/image-resize.d.ts.map +1 -1
- package/dist/utils/image-resize.js +75 -115
- package/dist/utils/image-resize.js.map +1 -1
- package/dist/utils/json.d.ts +3 -0
- package/dist/utils/json.d.ts.map +1 -0
- package/dist/utils/json.js +7 -0
- package/dist/utils/json.js.map +1 -0
- package/dist/utils/mime.d.ts +1 -0
- package/dist/utils/mime.d.ts.map +1 -1
- package/dist/utils/mime.js +100 -16
- package/dist/utils/mime.js.map +1 -1
- package/dist/utils/open-browser.d.ts +9 -0
- package/dist/utils/open-browser.d.ts.map +1 -0
- package/dist/utils/open-browser.js +22 -0
- package/dist/utils/open-browser.js.map +1 -0
- package/dist/utils/paths.d.ts +25 -1
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/paths.js +75 -2
- package/dist/utils/paths.js.map +1 -1
- package/dist/utils/pi-user-agent.d.ts +2 -0
- package/dist/utils/pi-user-agent.d.ts.map +1 -0
- package/dist/utils/pi-user-agent.js +5 -0
- package/dist/utils/pi-user-agent.js.map +1 -0
- package/dist/utils/shell.d.ts +11 -6
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +42 -26
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/syntax-highlight.d.ts +12 -0
- package/dist/utils/syntax-highlight.d.ts.map +1 -0
- package/dist/utils/syntax-highlight.js +118 -0
- package/dist/utils/syntax-highlight.js.map +1 -0
- package/dist/utils/tools-manager.d.ts.map +1 -1
- package/dist/utils/tools-manager.js +86 -10
- package/dist/utils/tools-manager.js.map +1 -1
- package/dist/utils/version-check.d.ts +15 -0
- package/dist/utils/version-check.d.ts.map +1 -0
- package/dist/utils/version-check.js +59 -0
- package/dist/utils/version-check.js.map +1 -0
- package/dist/utils/windows-self-update.d.ts +3 -0
- package/dist/utils/windows-self-update.d.ts.map +1 -0
- package/dist/utils/windows-self-update.js +77 -0
- package/dist/utils/windows-self-update.js.map +1 -0
- package/docs/compaction.md +12 -12
- package/docs/containerization.md +111 -0
- package/docs/custom-provider.md +187 -47
- package/docs/development.md +5 -5
- package/docs/docs.json +156 -0
- package/docs/extensions.md +520 -79
- package/docs/index.md +82 -0
- package/docs/json.md +4 -4
- package/docs/keybindings.md +24 -2
- package/docs/models.md +119 -17
- package/docs/packages.md +19 -11
- package/docs/prompt-templates.md +30 -2
- package/docs/providers.md +85 -23
- package/docs/quickstart.md +165 -0
- package/docs/rpc.md +99 -10
- package/docs/sdk.md +118 -50
- package/docs/security.md +55 -0
- package/docs/{session.md → session-format.md} +29 -19
- package/docs/sessions.md +145 -0
- package/docs/settings.md +71 -11
- package/docs/skills.md +16 -17
- package/docs/terminal-setup.md +44 -2
- package/docs/termux.md +4 -4
- package/docs/themes.md +3 -3
- package/docs/tmux.md +4 -2
- package/docs/tui.md +44 -4
- package/docs/usage.md +305 -0
- package/examples/extensions/README.md +1 -0
- package/examples/extensions/border-status-editor.ts +145 -0
- package/examples/extensions/built-in-tool-renderer.ts +3 -0
- package/examples/extensions/custom-compaction.ts +2 -1
- package/examples/extensions/custom-header.ts +1 -1
- package/examples/extensions/custom-provider-anthropic/index.ts +2 -2
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +2 -2
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +63 -8
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +2 -2
- package/examples/extensions/doom-overlay/doom-component.ts +2 -2
- package/examples/extensions/doom-overlay/index.ts +1 -1
- package/examples/extensions/dynamic-resources/dynamic.json +1 -1
- package/examples/extensions/dynamic-tools.ts +2 -2
- package/examples/extensions/entry-renderer.ts +41 -0
- package/examples/extensions/git-merge-and-resolve.ts +115 -0
- package/examples/extensions/github-issue-autocomplete.ts +185 -0
- package/examples/extensions/gondolin/index.ts +531 -0
- package/examples/extensions/gondolin/package-lock.json +185 -0
- package/examples/extensions/gondolin/package.json +19 -0
- package/examples/extensions/handoff.ts +52 -14
- package/examples/extensions/input-transform-streaming.ts +39 -0
- package/examples/extensions/interactive-shell.ts +1 -1
- package/examples/extensions/notify.ts +2 -9
- package/examples/extensions/overlay-qa-tests.ts +240 -138
- package/examples/extensions/overlay-test.ts +7 -4
- package/examples/extensions/plan-mode/index.ts +87 -37
- package/examples/extensions/plan-mode/utils.ts +1 -1
- package/examples/extensions/preset.ts +46 -7
- package/examples/extensions/project-trust.ts +64 -0
- package/examples/extensions/prompt-customizer.ts +97 -0
- package/examples/extensions/provider-payload.ts +8 -4
- package/examples/extensions/qna.ts +3 -3
- package/examples/extensions/question.ts +34 -20
- package/examples/extensions/questionnaire.ts +43 -30
- package/examples/extensions/reload-runtime.ts +1 -1
- package/examples/extensions/sandbox/index.ts +6 -6
- package/examples/extensions/sandbox/package-lock.json +5 -5
- package/examples/extensions/sandbox/package.json +2 -2
- package/examples/extensions/shutdown-command.ts +1 -1
- package/examples/extensions/snake.ts +1 -1
- package/examples/extensions/space-invaders.ts +1 -1
- package/examples/extensions/structured-output.ts +65 -0
- package/examples/extensions/subagent/agents.ts +2 -2
- package/examples/extensions/subagent/index.ts +58 -29
- package/examples/extensions/summarize.ts +3 -2
- package/examples/extensions/tic-tac-toe.ts +1008 -0
- package/examples/extensions/titlebar-spinner.ts +2 -2
- package/examples/extensions/todo.ts +2 -2
- package/examples/extensions/tool-override.ts +1 -1
- package/examples/extensions/tools.ts +5 -0
- package/examples/extensions/truncated-tool.ts +1 -1
- package/examples/extensions/with-deps/index.ts +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +3 -3
- package/examples/extensions/working-indicator.ts +123 -0
- package/examples/extensions/working-message-test.ts +25 -0
- package/examples/sdk/01-minimal.ts +14 -10
- package/examples/sdk/02-custom-model.ts +13 -9
- package/examples/sdk/03-custom-prompt.ts +32 -17
- package/examples/sdk/04-skills.ts +5 -2
- package/examples/sdk/05-tools.ts +23 -31
- package/examples/sdk/06-extensions.ts +14 -8
- package/examples/sdk/07-context-files.ts +5 -3
- package/examples/sdk/08-prompt-templates.ts +5 -2
- package/examples/sdk/09-api-keys-and-oauth.ts +8 -4
- package/examples/sdk/10-settings.ts +8 -6
- package/examples/sdk/11-sessions.ts +4 -0
- package/examples/sdk/12-full-control.ts +15 -19
- package/package.json +22 -16
- package/prompts/agents/build.md +38 -0
- package/prompts/agents/plan.md +36 -0
- package/prompts/agents/verify.md +36 -4
- package/prompts/commands/brainstorm.md +162 -0
- package/prompts/commands/discuss-phase.md +1 -1
- package/prompts/commands/execute-phase.md +4 -1
- package/prompts/commands/fix.md +12 -0
- package/prompts/commands/init-project.md +1 -0
- package/prompts/commands/map-codebase.md +15 -1
- package/prompts/commands/new-project.md +1 -0
- package/prompts/commands/next-milestone.md +2 -0
- package/prompts/commands/orchestrate.md +4 -4
- package/prompts/commands/plan-phase.md +5 -0
- package/prompts/commands/quick.md +4 -0
- package/prompts/commands/review.md +4 -1
- package/prompts/commands/verify-work.md +10 -8
|
@@ -1,10 +1,22 @@
|
|
|
1
|
+
import { uuidv7 } from "@draht/agent-core";
|
|
1
2
|
import { randomUUID } from "crypto";
|
|
2
|
-
import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readdirSync,
|
|
3
|
-
import { readdir,
|
|
3
|
+
import { appendFileSync, closeSync, createReadStream, existsSync, mkdirSync, openSync, readdirSync, readSync, statSync, writeFileSync, } from "fs";
|
|
4
|
+
import { readdir, stat } from "fs/promises";
|
|
4
5
|
import { join, resolve } from "path";
|
|
6
|
+
import { createInterface } from "readline";
|
|
7
|
+
import { StringDecoder } from "string_decoder";
|
|
5
8
|
import { getAgentDir as getDefaultAgentDir, getSessionsDir } from "../config.js";
|
|
9
|
+
import { normalizePath, resolvePath } from "../utils/paths.js";
|
|
6
10
|
import { createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage, } from "./messages.js";
|
|
7
11
|
export const CURRENT_SESSION_VERSION = 3;
|
|
12
|
+
function createSessionId() {
|
|
13
|
+
return uuidv7();
|
|
14
|
+
}
|
|
15
|
+
export function assertValidSessionId(id) {
|
|
16
|
+
if (!/^[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$/.test(id)) {
|
|
17
|
+
throw new Error("Session id must be non-empty, contain only alphanumeric characters, '-', '_', and '.', and start and end with an alphanumeric character");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
8
20
|
/** Generate a unique short ID (8 hex chars, collision-checked) */
|
|
9
21
|
function generateId(byId) {
|
|
10
22
|
for (let i = 0; i < 100; i++) {
|
|
@@ -100,46 +112,40 @@ export function getLatestCompactionEntry(entries) {
|
|
|
100
112
|
}
|
|
101
113
|
return null;
|
|
102
114
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// Build uuid index if not available
|
|
110
|
-
if (!byId) {
|
|
111
|
-
byId = new Map();
|
|
112
|
-
for (const entry of entries) {
|
|
113
|
-
byId.set(entry.id, entry);
|
|
114
|
-
}
|
|
115
|
+
function buildEntryIndex(entries, byId) {
|
|
116
|
+
if (byId)
|
|
117
|
+
return byId;
|
|
118
|
+
const index = new Map();
|
|
119
|
+
for (const entry of entries) {
|
|
120
|
+
index.set(entry.id, entry);
|
|
115
121
|
}
|
|
116
|
-
|
|
122
|
+
return index;
|
|
123
|
+
}
|
|
124
|
+
function buildSessionPath(entries, leafId, byId) {
|
|
125
|
+
const index = buildEntryIndex(entries, byId);
|
|
117
126
|
let leaf;
|
|
118
127
|
if (leafId === null) {
|
|
119
|
-
|
|
120
|
-
return { messages: [], thinkingLevel: "off", model: null };
|
|
128
|
+
return [];
|
|
121
129
|
}
|
|
122
130
|
if (leafId) {
|
|
123
|
-
leaf =
|
|
131
|
+
leaf = index.get(leafId);
|
|
124
132
|
}
|
|
133
|
+
leaf ??= entries[entries.length - 1];
|
|
125
134
|
if (!leaf) {
|
|
126
|
-
|
|
127
|
-
leaf = entries[entries.length - 1];
|
|
128
|
-
}
|
|
129
|
-
if (!leaf) {
|
|
130
|
-
return { messages: [], thinkingLevel: "off", model: null };
|
|
135
|
+
return [];
|
|
131
136
|
}
|
|
132
|
-
// Walk from leaf to root, collecting path
|
|
133
137
|
const path = [];
|
|
134
138
|
let current = leaf;
|
|
135
139
|
while (current) {
|
|
136
|
-
path.
|
|
137
|
-
current = current.parentId ?
|
|
140
|
+
path.push(current);
|
|
141
|
+
current = current.parentId ? index.get(current.parentId) : undefined;
|
|
138
142
|
}
|
|
139
|
-
|
|
143
|
+
path.reverse();
|
|
144
|
+
return path;
|
|
145
|
+
}
|
|
146
|
+
function getSessionContextSettings(path) {
|
|
140
147
|
let thinkingLevel = "off";
|
|
141
148
|
let model = null;
|
|
142
|
-
let compaction = null;
|
|
143
149
|
for (const entry of path) {
|
|
144
150
|
if (entry.type === "thinking_level_change") {
|
|
145
151
|
thinkingLevel = entry.thinkingLevel;
|
|
@@ -150,86 +156,148 @@ export function buildSessionContext(entries, leafId, byId) {
|
|
|
150
156
|
else if (entry.type === "message" && entry.message.role === "assistant") {
|
|
151
157
|
model = { provider: entry.message.provider, modelId: entry.message.model };
|
|
152
158
|
}
|
|
153
|
-
else if (entry.type === "compaction") {
|
|
154
|
-
compaction = entry;
|
|
155
|
-
}
|
|
156
159
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
messages.push(createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp));
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
if (compaction) {
|
|
175
|
-
// Emit summary first
|
|
176
|
-
messages.push(createCompactionSummaryMessage(compaction.summary, compaction.tokensBefore, compaction.timestamp));
|
|
177
|
-
// Find compaction index in path
|
|
178
|
-
const compactionIdx = path.findIndex((e) => e.type === "compaction" && e.id === compaction.id);
|
|
179
|
-
// Emit kept messages (before compaction, starting from firstKeptEntryId)
|
|
180
|
-
let foundFirstKept = false;
|
|
181
|
-
for (let i = 0; i < compactionIdx; i++) {
|
|
182
|
-
const entry = path[i];
|
|
183
|
-
if (entry.id === compaction.firstKeptEntryId) {
|
|
184
|
-
foundFirstKept = true;
|
|
185
|
-
}
|
|
186
|
-
if (foundFirstKept) {
|
|
187
|
-
appendMessage(entry);
|
|
188
|
-
}
|
|
160
|
+
return { thinkingLevel, model };
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Project one selected session entry into LLM/runtime messages.
|
|
164
|
+
* Plain custom entries are display/state entries and do not participate in context.
|
|
165
|
+
*/
|
|
166
|
+
export function sessionEntryToContextMessages(entry) {
|
|
167
|
+
if (entry.type === "message") {
|
|
168
|
+
const message = entry.message;
|
|
169
|
+
// Session files are parsed without validation; old versions, forks, or
|
|
170
|
+
// hand-edited files can contain messages with null/missing content.
|
|
171
|
+
if ((message.role === "user" || message.role === "assistant" || message.role === "toolResult") &&
|
|
172
|
+
message.content == null) {
|
|
173
|
+
return [{ ...message, content: [] }];
|
|
189
174
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
175
|
+
return [message];
|
|
176
|
+
}
|
|
177
|
+
if (entry.type === "custom_message") {
|
|
178
|
+
return [
|
|
179
|
+
createCustomMessage(entry.customType, entry.content ?? [], entry.display, entry.details, entry.timestamp),
|
|
180
|
+
];
|
|
181
|
+
}
|
|
182
|
+
if (entry.type === "branch_summary" && entry.summary) {
|
|
183
|
+
return [createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp)];
|
|
184
|
+
}
|
|
185
|
+
if (entry.type === "compaction") {
|
|
186
|
+
return [createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp)];
|
|
187
|
+
}
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Build the active, compaction-aware session entry list.
|
|
192
|
+
*
|
|
193
|
+
* This follows the current leaf path. If the path contains compaction entries,
|
|
194
|
+
* the latest compaction is represented by the compaction entry itself, followed
|
|
195
|
+
* by the kept entries starting at firstKeptEntryId and all entries after the
|
|
196
|
+
* compaction entry. Older summarized entries are omitted.
|
|
197
|
+
*/
|
|
198
|
+
export function buildContextEntries(entries, leafId, byId) {
|
|
199
|
+
const path = buildSessionPath(entries, leafId, byId);
|
|
200
|
+
let compaction = null;
|
|
201
|
+
for (const entry of path) {
|
|
202
|
+
if (entry.type === "compaction") {
|
|
203
|
+
compaction = entry;
|
|
194
204
|
}
|
|
195
205
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
206
|
+
if (!compaction) {
|
|
207
|
+
return path;
|
|
208
|
+
}
|
|
209
|
+
const compactionIdx = path.findIndex((entry) => entry.id === compaction.id);
|
|
210
|
+
if (compactionIdx < 0) {
|
|
211
|
+
return path;
|
|
212
|
+
}
|
|
213
|
+
const contextEntries = [compaction];
|
|
214
|
+
let foundFirstKept = false;
|
|
215
|
+
for (let i = 0; i < compactionIdx; i++) {
|
|
216
|
+
const entry = path[i];
|
|
217
|
+
if (entry.id === compaction.firstKeptEntryId) {
|
|
218
|
+
foundFirstKept = true;
|
|
219
|
+
}
|
|
220
|
+
if (foundFirstKept) {
|
|
221
|
+
contextEntries.push(entry);
|
|
200
222
|
}
|
|
201
223
|
}
|
|
224
|
+
contextEntries.push(...path.slice(compactionIdx + 1));
|
|
225
|
+
return contextEntries;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Build the session context from entries using tree traversal.
|
|
229
|
+
* If leafId is provided, walks from that entry to root.
|
|
230
|
+
* Handles compaction and branch summaries along the path.
|
|
231
|
+
*/
|
|
232
|
+
export function buildSessionContext(entries, leafId, byId) {
|
|
233
|
+
const path = buildSessionPath(entries, leafId, byId);
|
|
234
|
+
const { thinkingLevel, model } = getSessionContextSettings(path);
|
|
235
|
+
const messages = buildContextEntries(entries, leafId, byId).flatMap(sessionEntryToContextMessages);
|
|
202
236
|
return { messages, thinkingLevel, model };
|
|
203
237
|
}
|
|
204
238
|
/**
|
|
205
239
|
* Compute the default session directory for a cwd.
|
|
206
|
-
* Encodes cwd into a safe directory name under ~/.
|
|
240
|
+
* Encodes cwd into a safe directory name under ~/.pi/agent/sessions/.
|
|
207
241
|
*/
|
|
242
|
+
function getDefaultSessionDirPath(cwd, agentDir = getDefaultAgentDir()) {
|
|
243
|
+
const resolvedCwd = resolvePath(cwd);
|
|
244
|
+
const resolvedAgentDir = resolvePath(agentDir);
|
|
245
|
+
const safePath = `--${resolvedCwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
|
|
246
|
+
return join(resolvedAgentDir, "sessions", safePath);
|
|
247
|
+
}
|
|
208
248
|
export function getDefaultSessionDir(cwd, agentDir = getDefaultAgentDir()) {
|
|
209
|
-
const
|
|
210
|
-
const sessionDir = join(agentDir, "sessions", safePath);
|
|
249
|
+
const sessionDir = getDefaultSessionDirPath(cwd, agentDir);
|
|
211
250
|
if (!existsSync(sessionDir)) {
|
|
212
251
|
mkdirSync(sessionDir, { recursive: true });
|
|
213
252
|
}
|
|
214
253
|
return sessionDir;
|
|
215
254
|
}
|
|
255
|
+
const SESSION_READ_BUFFER_SIZE = 1024 * 1024;
|
|
256
|
+
function parseSessionEntryLine(line) {
|
|
257
|
+
if (!line.trim())
|
|
258
|
+
return null;
|
|
259
|
+
try {
|
|
260
|
+
return JSON.parse(line);
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
// Skip malformed lines
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
216
267
|
/** Exported for testing */
|
|
217
268
|
export function loadEntriesFromFile(filePath) {
|
|
218
|
-
|
|
269
|
+
const resolvedFilePath = normalizePath(filePath);
|
|
270
|
+
if (!existsSync(resolvedFilePath))
|
|
219
271
|
return [];
|
|
220
|
-
const content = readFileSync(filePath, "utf8");
|
|
221
272
|
const entries = [];
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
273
|
+
const fd = openSync(resolvedFilePath, "r");
|
|
274
|
+
try {
|
|
275
|
+
const decoder = new StringDecoder("utf8");
|
|
276
|
+
const buffer = Buffer.allocUnsafe(SESSION_READ_BUFFER_SIZE);
|
|
277
|
+
let pending = "";
|
|
278
|
+
while (true) {
|
|
279
|
+
const bytesRead = readSync(fd, buffer, 0, buffer.length, null);
|
|
280
|
+
if (bytesRead === 0)
|
|
281
|
+
break;
|
|
282
|
+
pending += decoder.write(buffer.subarray(0, bytesRead));
|
|
283
|
+
let lineStart = 0;
|
|
284
|
+
let newlineIndex = pending.indexOf("\n", lineStart);
|
|
285
|
+
while (newlineIndex !== -1) {
|
|
286
|
+
const entry = parseSessionEntryLine(pending.slice(lineStart, newlineIndex));
|
|
287
|
+
if (entry)
|
|
288
|
+
entries.push(entry);
|
|
289
|
+
lineStart = newlineIndex + 1;
|
|
290
|
+
newlineIndex = pending.indexOf("\n", lineStart);
|
|
291
|
+
}
|
|
292
|
+
pending = pending.slice(lineStart);
|
|
232
293
|
}
|
|
294
|
+
pending += decoder.end();
|
|
295
|
+
const finalEntry = parseSessionEntryLine(pending);
|
|
296
|
+
if (finalEntry)
|
|
297
|
+
entries.push(finalEntry);
|
|
298
|
+
}
|
|
299
|
+
finally {
|
|
300
|
+
closeSync(fd);
|
|
233
301
|
}
|
|
234
302
|
// Validate session header
|
|
235
303
|
if (entries.length === 0)
|
|
@@ -240,7 +308,7 @@ export function loadEntriesFromFile(filePath) {
|
|
|
240
308
|
}
|
|
241
309
|
return entries;
|
|
242
310
|
}
|
|
243
|
-
function
|
|
311
|
+
function readSessionHeader(filePath) {
|
|
244
312
|
try {
|
|
245
313
|
const fd = openSync(filePath, "r");
|
|
246
314
|
const buffer = Buffer.alloc(512);
|
|
@@ -248,22 +316,36 @@ function isValidSessionFile(filePath) {
|
|
|
248
316
|
closeSync(fd);
|
|
249
317
|
const firstLine = buffer.toString("utf8", 0, bytesRead).split("\n")[0];
|
|
250
318
|
if (!firstLine)
|
|
251
|
-
return
|
|
319
|
+
return null;
|
|
252
320
|
const header = JSON.parse(firstLine);
|
|
253
|
-
|
|
321
|
+
if (header.type !== "session" || typeof header.id !== "string") {
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
return header;
|
|
254
325
|
}
|
|
255
326
|
catch {
|
|
256
|
-
return
|
|
327
|
+
return null;
|
|
257
328
|
}
|
|
258
329
|
}
|
|
330
|
+
function getSessionHeaderCwd(header) {
|
|
331
|
+
const cwd = header.cwd;
|
|
332
|
+
return typeof cwd === "string" ? cwd : undefined;
|
|
333
|
+
}
|
|
334
|
+
function sessionCwdMatches(cwd, resolvedCwd) {
|
|
335
|
+
return cwd !== undefined && cwd !== "" && resolvePath(cwd) === resolvedCwd;
|
|
336
|
+
}
|
|
259
337
|
/** Exported for testing */
|
|
260
|
-
export function findMostRecentSession(sessionDir) {
|
|
338
|
+
export function findMostRecentSession(sessionDir, cwd) {
|
|
339
|
+
const resolvedSessionDir = normalizePath(sessionDir);
|
|
340
|
+
const resolvedCwd = cwd ? resolvePath(cwd) : undefined;
|
|
261
341
|
try {
|
|
262
|
-
const files = readdirSync(
|
|
342
|
+
const files = readdirSync(resolvedSessionDir)
|
|
263
343
|
.filter((f) => f.endsWith(".jsonl"))
|
|
264
|
-
.map((f) => join(
|
|
265
|
-
.
|
|
266
|
-
.
|
|
344
|
+
.map((f) => join(resolvedSessionDir, f))
|
|
345
|
+
.map((path) => ({ path, header: readSessionHeader(path) }))
|
|
346
|
+
.filter((file) => file.header !== null &&
|
|
347
|
+
(!resolvedCwd || sessionCwdMatches(getSessionHeaderCwd(file.header), resolvedCwd)))
|
|
348
|
+
.map(({ path }) => ({ path, mtime: statSync(path).mtime }))
|
|
267
349
|
.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
|
|
268
350
|
return files[0]?.path || null;
|
|
269
351
|
}
|
|
@@ -284,73 +366,53 @@ function extractTextContent(message) {
|
|
|
284
366
|
.map((block) => block.text)
|
|
285
367
|
.join(" ");
|
|
286
368
|
}
|
|
287
|
-
function
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
continue;
|
|
297
|
-
const msgTimestamp = message.timestamp;
|
|
298
|
-
if (typeof msgTimestamp === "number") {
|
|
299
|
-
lastActivityTime = Math.max(lastActivityTime ?? 0, msgTimestamp);
|
|
300
|
-
continue;
|
|
301
|
-
}
|
|
302
|
-
const entryTimestamp = entry.timestamp;
|
|
303
|
-
if (typeof entryTimestamp === "string") {
|
|
304
|
-
const t = new Date(entryTimestamp).getTime();
|
|
305
|
-
if (!Number.isNaN(t)) {
|
|
306
|
-
lastActivityTime = Math.max(lastActivityTime ?? 0, t);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
return lastActivityTime;
|
|
311
|
-
}
|
|
312
|
-
function getSessionModifiedDate(entries, header, statsMtime) {
|
|
313
|
-
const lastActivityTime = getLastActivityTime(entries);
|
|
314
|
-
if (typeof lastActivityTime === "number" && lastActivityTime > 0) {
|
|
315
|
-
return new Date(lastActivityTime);
|
|
369
|
+
function getMessageActivityTime(entry) {
|
|
370
|
+
const message = entry.message;
|
|
371
|
+
if (!isMessageWithContent(message))
|
|
372
|
+
return undefined;
|
|
373
|
+
if (message.role !== "user" && message.role !== "assistant")
|
|
374
|
+
return undefined;
|
|
375
|
+
const msgTimestamp = message.timestamp;
|
|
376
|
+
if (typeof msgTimestamp === "number") {
|
|
377
|
+
return msgTimestamp;
|
|
316
378
|
}
|
|
317
|
-
const
|
|
318
|
-
return
|
|
379
|
+
const t = new Date(entry.timestamp).getTime();
|
|
380
|
+
return Number.isNaN(t) ? undefined : t;
|
|
319
381
|
}
|
|
320
382
|
async function buildSessionInfo(filePath) {
|
|
321
383
|
try {
|
|
322
|
-
const content = await readFile(filePath, "utf8");
|
|
323
|
-
const entries = [];
|
|
324
|
-
const lines = content.trim().split("\n");
|
|
325
|
-
for (const line of lines) {
|
|
326
|
-
if (!line.trim())
|
|
327
|
-
continue;
|
|
328
|
-
try {
|
|
329
|
-
entries.push(JSON.parse(line));
|
|
330
|
-
}
|
|
331
|
-
catch {
|
|
332
|
-
// Skip malformed lines
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
if (entries.length === 0)
|
|
336
|
-
return null;
|
|
337
|
-
const header = entries[0];
|
|
338
|
-
if (header.type !== "session")
|
|
339
|
-
return null;
|
|
340
384
|
const stats = await stat(filePath);
|
|
385
|
+
let header = null;
|
|
341
386
|
let messageCount = 0;
|
|
342
387
|
let firstMessage = "";
|
|
343
388
|
const allMessages = [];
|
|
344
389
|
let name;
|
|
345
|
-
|
|
390
|
+
let lastActivityTime;
|
|
391
|
+
const rl = createInterface({
|
|
392
|
+
input: createReadStream(filePath, { encoding: "utf8" }),
|
|
393
|
+
crlfDelay: Infinity,
|
|
394
|
+
});
|
|
395
|
+
for await (const line of rl) {
|
|
396
|
+
const entry = parseSessionEntryLine(line);
|
|
397
|
+
if (!entry)
|
|
398
|
+
continue;
|
|
399
|
+
if (!header) {
|
|
400
|
+
if (entry.type !== "session")
|
|
401
|
+
return null;
|
|
402
|
+
header = entry;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
346
405
|
// Extract session name (use latest, including explicit clears)
|
|
347
406
|
if (entry.type === "session_info") {
|
|
348
|
-
|
|
349
|
-
name = infoEntry.name?.trim() || undefined;
|
|
407
|
+
name = entry.name?.trim() || undefined;
|
|
350
408
|
}
|
|
351
409
|
if (entry.type !== "message")
|
|
352
410
|
continue;
|
|
353
411
|
messageCount++;
|
|
412
|
+
const activityTime = getMessageActivityTime(entry);
|
|
413
|
+
if (typeof activityTime === "number") {
|
|
414
|
+
lastActivityTime = Math.max(lastActivityTime ?? 0, activityTime);
|
|
415
|
+
}
|
|
354
416
|
const message = entry.message;
|
|
355
417
|
if (!isMessageWithContent(message))
|
|
356
418
|
continue;
|
|
@@ -364,9 +426,16 @@ async function buildSessionInfo(filePath) {
|
|
|
364
426
|
firstMessage = textContent;
|
|
365
427
|
}
|
|
366
428
|
}
|
|
429
|
+
if (!header)
|
|
430
|
+
return null;
|
|
367
431
|
const cwd = typeof header.cwd === "string" ? header.cwd : "";
|
|
368
432
|
const parentSessionPath = header.parentSession;
|
|
369
|
-
const
|
|
433
|
+
const headerTime = typeof header.timestamp === "string" ? new Date(header.timestamp).getTime() : NaN;
|
|
434
|
+
const modified = typeof lastActivityTime === "number" && lastActivityTime > 0
|
|
435
|
+
? new Date(lastActivityTime)
|
|
436
|
+
: !Number.isNaN(headerTime)
|
|
437
|
+
? new Date(headerTime)
|
|
438
|
+
: stats.mtime;
|
|
370
439
|
return {
|
|
371
440
|
path: filePath,
|
|
372
441
|
id: header.id,
|
|
@@ -384,6 +453,40 @@ async function buildSessionInfo(filePath) {
|
|
|
384
453
|
return null;
|
|
385
454
|
}
|
|
386
455
|
}
|
|
456
|
+
const MAX_CONCURRENT_SESSION_INFO_LOADS = 10;
|
|
457
|
+
async function buildSessionInfosWithConcurrency(files, onLoaded) {
|
|
458
|
+
const results = new Array(files.length).fill(null);
|
|
459
|
+
const inFlight = new Set();
|
|
460
|
+
let nextIndex = 0;
|
|
461
|
+
const startNext = () => {
|
|
462
|
+
const index = nextIndex++;
|
|
463
|
+
const file = files[index];
|
|
464
|
+
if (!file)
|
|
465
|
+
return;
|
|
466
|
+
let task;
|
|
467
|
+
task = buildSessionInfo(file)
|
|
468
|
+
.then((info) => {
|
|
469
|
+
results[index] = info;
|
|
470
|
+
})
|
|
471
|
+
.catch(() => {
|
|
472
|
+
results[index] = null;
|
|
473
|
+
})
|
|
474
|
+
.finally(() => {
|
|
475
|
+
inFlight.delete(task);
|
|
476
|
+
onLoaded();
|
|
477
|
+
});
|
|
478
|
+
inFlight.add(task);
|
|
479
|
+
};
|
|
480
|
+
while (nextIndex < files.length || inFlight.size > 0) {
|
|
481
|
+
while (nextIndex < files.length && inFlight.size < MAX_CONCURRENT_SESSION_INFO_LOADS) {
|
|
482
|
+
startNext();
|
|
483
|
+
}
|
|
484
|
+
if (inFlight.size > 0) {
|
|
485
|
+
await Promise.race(inFlight);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return results;
|
|
489
|
+
}
|
|
387
490
|
async function listSessionsFromDir(dir, onProgress, progressOffset = 0, progressTotal) {
|
|
388
491
|
const sessions = [];
|
|
389
492
|
if (!existsSync(dir)) {
|
|
@@ -394,12 +497,10 @@ async function listSessionsFromDir(dir, onProgress, progressOffset = 0, progress
|
|
|
394
497
|
const files = dirEntries.filter((f) => f.endsWith(".jsonl")).map((f) => join(dir, f));
|
|
395
498
|
const total = progressTotal ?? files.length;
|
|
396
499
|
let loaded = 0;
|
|
397
|
-
const results = await
|
|
398
|
-
const info = await buildSessionInfo(file);
|
|
500
|
+
const results = await buildSessionInfosWithConcurrency(files, () => {
|
|
399
501
|
loaded++;
|
|
400
502
|
onProgress?.(progressOffset + loaded, total);
|
|
401
|
-
|
|
402
|
-
}));
|
|
503
|
+
});
|
|
403
504
|
for (const info of results) {
|
|
404
505
|
if (info) {
|
|
405
506
|
sessions.push(info);
|
|
@@ -434,29 +535,32 @@ export class SessionManager {
|
|
|
434
535
|
labelsById = new Map();
|
|
435
536
|
labelTimestampsById = new Map();
|
|
436
537
|
leafId = null;
|
|
437
|
-
constructor(cwd, sessionDir, sessionFile, persist) {
|
|
438
|
-
this.cwd = cwd;
|
|
439
|
-
this.sessionDir = sessionDir;
|
|
538
|
+
constructor(cwd, sessionDir, sessionFile, persist, newSessionOptions) {
|
|
539
|
+
this.cwd = resolvePath(cwd);
|
|
540
|
+
this.sessionDir = normalizePath(sessionDir);
|
|
440
541
|
this.persist = persist;
|
|
441
|
-
if (persist && sessionDir && !existsSync(sessionDir)) {
|
|
442
|
-
mkdirSync(sessionDir, { recursive: true });
|
|
542
|
+
if (persist && this.sessionDir && !existsSync(this.sessionDir)) {
|
|
543
|
+
mkdirSync(this.sessionDir, { recursive: true });
|
|
443
544
|
}
|
|
444
545
|
if (sessionFile) {
|
|
445
546
|
this.setSessionFile(sessionFile);
|
|
446
547
|
}
|
|
447
548
|
else {
|
|
448
|
-
this.newSession();
|
|
549
|
+
this.newSession(newSessionOptions);
|
|
449
550
|
}
|
|
450
551
|
}
|
|
451
552
|
/** Switch to a different session file (used for resume and branching) */
|
|
452
553
|
setSessionFile(sessionFile) {
|
|
453
|
-
this.sessionFile =
|
|
554
|
+
this.sessionFile = resolvePath(sessionFile);
|
|
454
555
|
if (existsSync(this.sessionFile)) {
|
|
455
556
|
this.fileEntries = loadEntriesFromFile(this.sessionFile);
|
|
456
|
-
// If file was empty
|
|
457
|
-
//
|
|
557
|
+
// If file was empty, initialize it with a valid session header. If it was
|
|
558
|
+
// non-empty but did not parse as a pi session, fail without modifying it.
|
|
458
559
|
if (this.fileEntries.length === 0) {
|
|
459
560
|
const explicitPath = this.sessionFile;
|
|
561
|
+
if (statSync(explicitPath).size > 0) {
|
|
562
|
+
throw new Error(`Session file is not a valid pi session: ${explicitPath}`);
|
|
563
|
+
}
|
|
460
564
|
this.newSession();
|
|
461
565
|
this.sessionFile = explicitPath;
|
|
462
566
|
this._rewriteFile();
|
|
@@ -464,7 +568,7 @@ export class SessionManager {
|
|
|
464
568
|
return;
|
|
465
569
|
}
|
|
466
570
|
const header = this.fileEntries.find((e) => e.type === "session");
|
|
467
|
-
this.sessionId = header?.id ??
|
|
571
|
+
this.sessionId = header?.id ?? createSessionId();
|
|
468
572
|
if (migrateToCurrentVersion(this.fileEntries)) {
|
|
469
573
|
this._rewriteFile();
|
|
470
574
|
}
|
|
@@ -478,7 +582,10 @@ export class SessionManager {
|
|
|
478
582
|
}
|
|
479
583
|
}
|
|
480
584
|
newSession(options) {
|
|
481
|
-
|
|
585
|
+
if (options?.id !== undefined) {
|
|
586
|
+
assertValidSessionId(options.id);
|
|
587
|
+
}
|
|
588
|
+
this.sessionId = options?.id ?? createSessionId();
|
|
482
589
|
const timestamp = new Date().toISOString();
|
|
483
590
|
const header = {
|
|
484
591
|
type: "session",
|
|
@@ -491,6 +598,7 @@ export class SessionManager {
|
|
|
491
598
|
this.fileEntries = [header];
|
|
492
599
|
this.byId.clear();
|
|
493
600
|
this.labelsById.clear();
|
|
601
|
+
this.labelTimestampsById.clear();
|
|
494
602
|
this.leafId = null;
|
|
495
603
|
this.flushed = false;
|
|
496
604
|
if (this.persist) {
|
|
@@ -524,8 +632,15 @@ export class SessionManager {
|
|
|
524
632
|
_rewriteFile() {
|
|
525
633
|
if (!this.persist || !this.sessionFile)
|
|
526
634
|
return;
|
|
527
|
-
const
|
|
528
|
-
|
|
635
|
+
const fd = openSync(this.sessionFile, "w");
|
|
636
|
+
try {
|
|
637
|
+
for (const entry of this.fileEntries) {
|
|
638
|
+
writeFileSync(fd, `${JSON.stringify(entry)}\n`);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
finally {
|
|
642
|
+
closeSync(fd);
|
|
643
|
+
}
|
|
529
644
|
}
|
|
530
645
|
isPersisted() {
|
|
531
646
|
return this.persist;
|
|
@@ -536,6 +651,9 @@ export class SessionManager {
|
|
|
536
651
|
getSessionDir() {
|
|
537
652
|
return this.sessionDir;
|
|
538
653
|
}
|
|
654
|
+
usesDefaultSessionDir() {
|
|
655
|
+
return this.sessionDir === getDefaultSessionDirPath(this.cwd);
|
|
656
|
+
}
|
|
539
657
|
getSessionId() {
|
|
540
658
|
return this.sessionId;
|
|
541
659
|
}
|
|
@@ -547,13 +665,24 @@ export class SessionManager {
|
|
|
547
665
|
return;
|
|
548
666
|
const hasAssistant = this.fileEntries.some((e) => e.type === "message" && e.message.role === "assistant");
|
|
549
667
|
if (!hasAssistant) {
|
|
550
|
-
|
|
551
|
-
|
|
668
|
+
if (this.flushed) {
|
|
669
|
+
appendFileSync(this.sessionFile, `${JSON.stringify(entry)}\n`);
|
|
670
|
+
}
|
|
671
|
+
else {
|
|
672
|
+
// Mark as not flushed so when assistant arrives, all entries get written
|
|
673
|
+
this.flushed = false;
|
|
674
|
+
}
|
|
552
675
|
return;
|
|
553
676
|
}
|
|
554
677
|
if (!this.flushed) {
|
|
555
|
-
|
|
556
|
-
|
|
678
|
+
const fd = openSync(this.sessionFile, "wx");
|
|
679
|
+
try {
|
|
680
|
+
for (const e of this.fileEntries) {
|
|
681
|
+
writeFileSync(fd, `${JSON.stringify(e)}\n`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
finally {
|
|
685
|
+
closeSync(fd);
|
|
557
686
|
}
|
|
558
687
|
this.flushed = true;
|
|
559
688
|
}
|
|
@@ -640,12 +769,13 @@ export class SessionManager {
|
|
|
640
769
|
}
|
|
641
770
|
/** Append a session info entry (e.g., display name). Returns entry id. */
|
|
642
771
|
appendSessionInfo(name) {
|
|
772
|
+
const sanitizedName = name.replace(/[\r\n]+/g, " ").trim();
|
|
643
773
|
const entry = {
|
|
644
774
|
type: "session_info",
|
|
645
775
|
id: generateId(this.byId),
|
|
646
776
|
parentId: this.leafId,
|
|
647
777
|
timestamp: new Date().toISOString(),
|
|
648
|
-
name:
|
|
778
|
+
name: sanitizedName,
|
|
649
779
|
};
|
|
650
780
|
this._appendEntry(entry);
|
|
651
781
|
return entry.id;
|
|
@@ -753,11 +883,19 @@ export class SessionManager {
|
|
|
753
883
|
const startId = fromId ?? this.leafId;
|
|
754
884
|
let current = startId ? this.byId.get(startId) : undefined;
|
|
755
885
|
while (current) {
|
|
756
|
-
path.
|
|
886
|
+
path.push(current);
|
|
757
887
|
current = current.parentId ? this.byId.get(current.parentId) : undefined;
|
|
758
888
|
}
|
|
889
|
+
path.reverse();
|
|
759
890
|
return path;
|
|
760
891
|
}
|
|
892
|
+
/**
|
|
893
|
+
* Build the active, compaction-aware entry list for context/rendering.
|
|
894
|
+
* Uses tree traversal from current leaf.
|
|
895
|
+
*/
|
|
896
|
+
buildContextEntries() {
|
|
897
|
+
return buildContextEntries(this.getEntries(), this.leafId, this.byId);
|
|
898
|
+
}
|
|
761
899
|
/**
|
|
762
900
|
* Build the session context (what gets sent to the LLM).
|
|
763
901
|
* Uses tree traversal from current leaf.
|
|
@@ -879,9 +1017,18 @@ export class SessionManager {
|
|
|
879
1017
|
if (path.length === 0) {
|
|
880
1018
|
throw new Error(`Entry ${leafId} not found`);
|
|
881
1019
|
}
|
|
882
|
-
// Filter out LabelEntry from path - we'll recreate them from the resolved map
|
|
883
|
-
|
|
884
|
-
|
|
1020
|
+
// Filter out LabelEntry from path - we'll recreate them from the resolved map.
|
|
1021
|
+
// Because labels are real tree entries, later entries can be children of labels;
|
|
1022
|
+
// removing labels requires re-chaining the retained path to avoid orphaned subtrees.
|
|
1023
|
+
const pathWithoutLabels = [];
|
|
1024
|
+
let pathParentId = null;
|
|
1025
|
+
for (const entry of path) {
|
|
1026
|
+
if (entry.type === "label")
|
|
1027
|
+
continue;
|
|
1028
|
+
pathWithoutLabels.push({ ...entry, parentId: pathParentId });
|
|
1029
|
+
pathParentId = entry.id;
|
|
1030
|
+
}
|
|
1031
|
+
const newSessionId = createSessionId();
|
|
885
1032
|
const timestamp = new Date().toISOString();
|
|
886
1033
|
const fileTimestamp = timestamp.replace(/[:.]/g, "-");
|
|
887
1034
|
const newSessionFile = join(this.getSessionDir(), `${fileTimestamp}_${newSessionId}.jsonl`);
|
|
@@ -961,11 +1108,11 @@ export class SessionManager {
|
|
|
961
1108
|
/**
|
|
962
1109
|
* Create a new session.
|
|
963
1110
|
* @param cwd Working directory (stored in session header)
|
|
964
|
-
* @param sessionDir Optional session directory. If omitted, uses default (~/.
|
|
1111
|
+
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
|
965
1112
|
*/
|
|
966
|
-
static create(cwd, sessionDir) {
|
|
967
|
-
const dir = sessionDir
|
|
968
|
-
return new SessionManager(cwd, dir, undefined, true);
|
|
1113
|
+
static create(cwd, sessionDir, options) {
|
|
1114
|
+
const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);
|
|
1115
|
+
return new SessionManager(cwd, dir, undefined, true, options);
|
|
969
1116
|
}
|
|
970
1117
|
/**
|
|
971
1118
|
* Open a specific session file.
|
|
@@ -974,30 +1121,32 @@ export class SessionManager {
|
|
|
974
1121
|
* @param cwdOverride Optional cwd override instead of the session header cwd.
|
|
975
1122
|
*/
|
|
976
1123
|
static open(path, sessionDir, cwdOverride) {
|
|
1124
|
+
const resolvedPath = resolvePath(path);
|
|
977
1125
|
// Extract cwd from session header if possible, otherwise use process.cwd()
|
|
978
|
-
const entries = loadEntriesFromFile(
|
|
1126
|
+
const entries = loadEntriesFromFile(resolvedPath);
|
|
979
1127
|
const header = entries.find((e) => e.type === "session");
|
|
980
1128
|
const cwd = cwdOverride ?? header?.cwd ?? process.cwd();
|
|
981
1129
|
// If no sessionDir provided, derive from file's parent directory
|
|
982
|
-
const dir = sessionDir
|
|
983
|
-
return new SessionManager(cwd, dir,
|
|
1130
|
+
const dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, "..");
|
|
1131
|
+
return new SessionManager(cwd, dir, resolvedPath, true);
|
|
984
1132
|
}
|
|
985
1133
|
/**
|
|
986
1134
|
* Continue the most recent session, or create new if none.
|
|
987
1135
|
* @param cwd Working directory
|
|
988
|
-
* @param sessionDir Optional session directory. If omitted, uses default (~/.
|
|
1136
|
+
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
|
989
1137
|
*/
|
|
990
1138
|
static continueRecent(cwd, sessionDir) {
|
|
991
|
-
const dir = sessionDir
|
|
992
|
-
const
|
|
1139
|
+
const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);
|
|
1140
|
+
const filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);
|
|
1141
|
+
const mostRecent = findMostRecentSession(dir, filterCwd ? cwd : undefined);
|
|
993
1142
|
if (mostRecent) {
|
|
994
1143
|
return new SessionManager(cwd, dir, mostRecent, true);
|
|
995
1144
|
}
|
|
996
1145
|
return new SessionManager(cwd, dir, undefined, true);
|
|
997
1146
|
}
|
|
998
1147
|
/** Create an in-memory session (no file persistence) */
|
|
999
|
-
static inMemory(cwd = process.cwd()) {
|
|
1000
|
-
return new SessionManager(cwd, "", undefined, false);
|
|
1148
|
+
static inMemory(cwd = process.cwd(), options) {
|
|
1149
|
+
return new SessionManager(cwd, "", undefined, false, options);
|
|
1001
1150
|
}
|
|
1002
1151
|
/**
|
|
1003
1152
|
* Fork a session from another project directory into the current project.
|
|
@@ -1006,21 +1155,26 @@ export class SessionManager {
|
|
|
1006
1155
|
* @param targetCwd Target working directory (where the new session will be stored)
|
|
1007
1156
|
* @param sessionDir Optional session directory. If omitted, uses default for targetCwd.
|
|
1008
1157
|
*/
|
|
1009
|
-
static forkFrom(sourcePath, targetCwd, sessionDir) {
|
|
1010
|
-
const
|
|
1158
|
+
static forkFrom(sourcePath, targetCwd, sessionDir, options) {
|
|
1159
|
+
const resolvedSourcePath = resolvePath(sourcePath);
|
|
1160
|
+
const resolvedTargetCwd = resolvePath(targetCwd);
|
|
1161
|
+
const sourceEntries = loadEntriesFromFile(resolvedSourcePath);
|
|
1011
1162
|
if (sourceEntries.length === 0) {
|
|
1012
|
-
throw new Error(`Cannot fork: source session file is empty or invalid: ${
|
|
1163
|
+
throw new Error(`Cannot fork: source session file is empty or invalid: ${resolvedSourcePath}`);
|
|
1013
1164
|
}
|
|
1014
1165
|
const sourceHeader = sourceEntries.find((e) => e.type === "session");
|
|
1015
1166
|
if (!sourceHeader) {
|
|
1016
|
-
throw new Error(`Cannot fork: source session has no header: ${
|
|
1167
|
+
throw new Error(`Cannot fork: source session has no header: ${resolvedSourcePath}`);
|
|
1017
1168
|
}
|
|
1018
|
-
const dir = sessionDir
|
|
1169
|
+
const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(resolvedTargetCwd);
|
|
1019
1170
|
if (!existsSync(dir)) {
|
|
1020
1171
|
mkdirSync(dir, { recursive: true });
|
|
1021
1172
|
}
|
|
1022
1173
|
// Create new session file with new ID but forked content
|
|
1023
|
-
|
|
1174
|
+
if (options?.id !== undefined) {
|
|
1175
|
+
assertValidSessionId(options.id);
|
|
1176
|
+
}
|
|
1177
|
+
const newSessionId = options?.id ?? createSessionId();
|
|
1024
1178
|
const timestamp = new Date().toISOString();
|
|
1025
1179
|
const fileTimestamp = timestamp.replace(/[:.]/g, "-");
|
|
1026
1180
|
const newSessionFile = join(dir, `${fileTimestamp}_${newSessionId}.jsonl`);
|
|
@@ -1030,35 +1184,40 @@ export class SessionManager {
|
|
|
1030
1184
|
version: CURRENT_SESSION_VERSION,
|
|
1031
1185
|
id: newSessionId,
|
|
1032
1186
|
timestamp,
|
|
1033
|
-
cwd:
|
|
1034
|
-
parentSession:
|
|
1187
|
+
cwd: resolvedTargetCwd,
|
|
1188
|
+
parentSession: resolvedSourcePath,
|
|
1035
1189
|
};
|
|
1036
|
-
|
|
1190
|
+
writeFileSync(newSessionFile, `${JSON.stringify(newHeader)}\n`, { flag: "wx" });
|
|
1037
1191
|
// Copy all non-header entries from source
|
|
1038
1192
|
for (const entry of sourceEntries) {
|
|
1039
1193
|
if (entry.type !== "session") {
|
|
1040
1194
|
appendFileSync(newSessionFile, `${JSON.stringify(entry)}\n`);
|
|
1041
1195
|
}
|
|
1042
1196
|
}
|
|
1043
|
-
return new SessionManager(
|
|
1197
|
+
return new SessionManager(resolvedTargetCwd, dir, newSessionFile, true);
|
|
1044
1198
|
}
|
|
1045
1199
|
/**
|
|
1046
1200
|
* List all sessions for a directory.
|
|
1047
1201
|
* @param cwd Working directory (used to compute default session directory)
|
|
1048
|
-
* @param sessionDir Optional session directory. If omitted, uses default (~/.
|
|
1202
|
+
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
|
1049
1203
|
* @param onProgress Optional callback for progress updates (loaded, total)
|
|
1050
1204
|
*/
|
|
1051
1205
|
static async list(cwd, sessionDir, onProgress) {
|
|
1052
|
-
const dir = sessionDir
|
|
1053
|
-
const
|
|
1206
|
+
const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);
|
|
1207
|
+
const filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);
|
|
1208
|
+
const resolvedCwd = resolvePath(cwd);
|
|
1209
|
+
const sessions = (await listSessionsFromDir(dir, onProgress)).filter((session) => !filterCwd || sessionCwdMatches(session.cwd, resolvedCwd));
|
|
1054
1210
|
sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
|
1055
1211
|
return sessions;
|
|
1056
1212
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1213
|
+
static async listAll(sessionDirOrOnProgress, onProgress) {
|
|
1214
|
+
const customSessionDir = typeof sessionDirOrOnProgress === "string" ? normalizePath(sessionDirOrOnProgress) : undefined;
|
|
1215
|
+
const progress = typeof sessionDirOrOnProgress === "function" ? sessionDirOrOnProgress : onProgress;
|
|
1216
|
+
if (customSessionDir) {
|
|
1217
|
+
const sessions = await listSessionsFromDir(customSessionDir, progress);
|
|
1218
|
+
sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
|
1219
|
+
return sessions;
|
|
1220
|
+
}
|
|
1062
1221
|
const sessionsDir = getSessionsDir();
|
|
1063
1222
|
try {
|
|
1064
1223
|
if (!existsSync(sessionsDir)) {
|
|
@@ -1083,12 +1242,10 @@ export class SessionManager {
|
|
|
1083
1242
|
let loaded = 0;
|
|
1084
1243
|
const sessions = [];
|
|
1085
1244
|
const allFiles = dirFiles.flat();
|
|
1086
|
-
const results = await
|
|
1087
|
-
const info = await buildSessionInfo(file);
|
|
1245
|
+
const results = await buildSessionInfosWithConcurrency(allFiles, () => {
|
|
1088
1246
|
loaded++;
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
}));
|
|
1247
|
+
progress?.(loaded, totalFiles);
|
|
1248
|
+
});
|
|
1092
1249
|
for (const info of results) {
|
|
1093
1250
|
if (info) {
|
|
1094
1251
|
sessions.push(info);
|