@bastani/atomic 0.9.38-alpha.5 → 0.9.38-alpha.7
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 +70 -0
- package/README.md +8 -6
- package/dist/builtin/intercom/CHANGELOG.md +9 -0
- package/dist/builtin/intercom/broker/broker.ts +6 -4
- package/dist/builtin/intercom/broker/delivered-message-cache.ts +50 -4
- package/dist/builtin/intercom/broker/send-handler.ts +6 -4
- package/dist/builtin/intercom/index.bundle.mjs +78 -33
- package/dist/builtin/intercom/package.json +2 -2
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/CHANGELOG.md +6 -0
- package/dist/builtin/subagents/package.json +3 -3
- package/dist/builtin/subagents/src/extension/index.bundle.mjs +140 -145
- package/dist/builtin/web-access/CHANGELOG.md +7 -0
- package/dist/builtin/web-access/index.bundle.mjs +53 -15
- package/dist/builtin/web-access/package.json +2 -2
- package/dist/builtin/workflows/CHANGELOG.md +48 -0
- package/dist/builtin/workflows/builtin/{chunk-q7dx7kh7.js → chunk-85mx6wcf.js} +6 -6
- package/dist/builtin/workflows/builtin/{chunk-xg7kkgf8.js → chunk-8kkv04hz.js} +15 -15
- package/dist/builtin/workflows/builtin/{chunk-na3wpwzh.js → chunk-8x61e0s7.js} +396 -363
- package/dist/builtin/workflows/builtin/{chunk-qrrkfg9v.js → chunk-y45tkftg.js} +7 -7
- package/dist/builtin/workflows/builtin/goal.js +2 -2
- package/dist/builtin/workflows/builtin/index.js +4 -4
- package/dist/builtin/workflows/builtin/open-claude-design.js +1 -1
- package/dist/builtin/workflows/builtin/ralph.js +2 -2
- package/dist/builtin/workflows/package.json +2 -2
- package/dist/builtin/workflows/src/authoring.d.ts +3 -8
- package/dist/builtin/workflows/src/durable/journal/conformance.d.ts +9 -1
- package/dist/builtin/workflows/src/durable/journal/internal.d.ts +1 -0
- package/dist/builtin/workflows/src/durable/journal/lease-expiry.d.ts +2 -0
- package/dist/builtin/workflows/src/durable/journal/memory-run-journal.d.ts +9 -1
- package/dist/builtin/workflows/src/durable/journal/resolution.d.ts +19 -8
- package/dist/builtin/workflows/src/durable/journal/sql-run-journal.d.ts +45 -0
- package/dist/builtin/workflows/src/durable/journal/sqlite-run-journal.d.ts +5 -47
- package/dist/builtin/workflows/src/durable/journal/types.d.ts +9 -114
- package/dist/builtin/workflows/src/durable/journal/values.d.ts +2 -0
- package/dist/builtin/workflows/src/extension/index.bundle.mjs +6963 -6181
- package/dist/builtin/workflows/src/index.js +9078 -8395
- package/dist/builtin/workflows/src/shared/authoring-contract-stage.d.ts +8 -8
- package/dist/builtin/workflows/src/shared/authoring-contract-ui.d.ts +0 -14
- package/dist/cli/args.d.ts +3 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +35 -5
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/session-picker.d.ts +2 -2
- package/dist/cli/session-picker.d.ts.map +1 -1
- package/dist/cli/session-picker.js +5 -5
- package/dist/cli/session-picker.js.map +1 -1
- package/dist/core/agent-session-auto-compaction.d.ts +1 -1
- package/dist/core/agent-session-auto-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-auto-compaction.js +2 -2
- package/dist/core/agent-session-auto-compaction.js.map +1 -1
- package/dist/core/agent-session-bash.d.ts +2 -3
- package/dist/core/agent-session-bash.d.ts.map +1 -1
- package/dist/core/agent-session-bash.js +18 -10
- package/dist/core/agent-session-bash.js.map +1 -1
- package/dist/core/agent-session-boundaries.d.ts +6 -6
- package/dist/core/agent-session-boundaries.d.ts.map +1 -1
- package/dist/core/agent-session-boundaries.js +22 -22
- package/dist/core/agent-session-boundaries.js.map +1 -1
- package/dist/core/agent-session-compaction.js +2 -2
- package/dist/core/agent-session-compaction.js.map +1 -1
- package/dist/core/agent-session-custom-message-commit.d.ts.map +1 -1
- package/dist/core/agent-session-custom-message-commit.js +10 -12
- package/dist/core/agent-session-custom-message-commit.js.map +1 -1
- package/dist/core/agent-session-events.d.ts.map +1 -1
- package/dist/core/agent-session-events.js +26 -14
- package/dist/core/agent-session-events.js.map +1 -1
- package/dist/core/agent-session-export.js +1 -1
- package/dist/core/agent-session-export.js.map +1 -1
- package/dist/core/agent-session-extension-bindings.d.ts +2 -2
- package/dist/core/agent-session-extension-bindings.d.ts.map +1 -1
- package/dist/core/agent-session-extension-bindings.js +75 -59
- package/dist/core/agent-session-extension-bindings.js.map +1 -1
- package/dist/core/agent-session-message-queue.d.ts +2 -2
- package/dist/core/agent-session-message-queue.d.ts.map +1 -1
- package/dist/core/agent-session-message-queue.js +17 -8
- package/dist/core/agent-session-message-queue.js.map +1 -1
- package/dist/core/agent-session-methods.d.ts +15 -15
- 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-models.d.ts +2 -2
- package/dist/core/agent-session-models.d.ts.map +1 -1
- package/dist/core/agent-session-models.js +59 -42
- package/dist/core/agent-session-models.js.map +1 -1
- package/dist/core/agent-session-persistent-custom-messages.d.ts +4 -4
- package/dist/core/agent-session-persistent-custom-messages.d.ts.map +1 -1
- package/dist/core/agent-session-persistent-custom-messages.js +16 -17
- package/dist/core/agent-session-persistent-custom-messages.js.map +1 -1
- package/dist/core/agent-session-prompt.d.ts.map +1 -1
- package/dist/core/agent-session-prompt.js +6 -8
- package/dist/core/agent-session-prompt.js.map +1 -1
- package/dist/core/agent-session-retry.d.ts.map +1 -1
- package/dist/core/agent-session-retry.js +17 -15
- package/dist/core/agent-session-retry.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts +7 -8
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +88 -100
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts +1 -1
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +2 -2
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session-summary.js +1 -1
- package/dist/core/agent-session-summary.js.map +1 -1
- package/dist/core/agent-session-tree.d.ts +1 -1
- package/dist/core/agent-session-tree.d.ts.map +1 -1
- package/dist/core/agent-session-tree.js +7 -7
- package/dist/core/agent-session-tree.js.map +1 -1
- package/dist/core/cache-warmer.js +1 -1
- package/dist/core/cache-warmer.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +1 -4
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +0 -4
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/pi-summary-fallback.d.ts.map +1 -1
- package/dist/core/compaction/pi-summary-fallback.js +1 -3
- package/dist/core/compaction/pi-summary-fallback.js.map +1 -1
- package/dist/core/export-html/index.js +1 -1
- package/dist/core/export-html/index.js.map +1 -1
- package/dist/core/export-html/template-js/entries-navigation.js +1 -1
- package/dist/core/export-html/template.js +1 -1
- package/dist/core/extensions/api-types.d.ts +12 -12
- package/dist/core/extensions/api-types.d.ts.map +1 -1
- package/dist/core/extensions/api-types.js.map +1 -1
- package/dist/core/extensions/context-types.d.ts +4 -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/extension-work.d.ts +1 -0
- package/dist/core/extensions/extension-work.d.ts.map +1 -1
- package/dist/core/extensions/extension-work.js +5 -1
- package/dist/core/extensions/extension-work.js.map +1 -1
- package/dist/core/extensions/loader-api.d.ts.map +1 -1
- package/dist/core/extensions/loader-api.js +40 -42
- package/dist/core/extensions/loader-api.js.map +1 -1
- package/dist/core/extensions/loader-host-modules.d.ts +1 -1
- package/dist/core/extensions/loader-host-modules.d.ts.map +1 -1
- package/dist/core/extensions/loader-host-modules.js +6 -2
- package/dist/core/extensions/loader-host-modules.js.map +1 -1
- package/dist/core/extensions/loader-rollback.d.ts.map +1 -1
- package/dist/core/extensions/loader-rollback.js +1 -0
- package/dist/core/extensions/loader-rollback.js.map +1 -1
- package/dist/core/extensions/loader-virtual-modules.d.ts.map +1 -1
- package/dist/core/extensions/loader-virtual-modules.js +1 -0
- package/dist/core/extensions/loader-virtual-modules.js.map +1 -1
- package/dist/core/extensions/runner-context.d.ts +1 -0
- package/dist/core/extensions/runner-context.d.ts.map +1 -1
- package/dist/core/extensions/runner-context.js +10 -3
- package/dist/core/extensions/runner-context.js.map +1 -1
- package/dist/core/extensions/runner-handlers.d.ts +1 -1
- package/dist/core/extensions/runner-handlers.d.ts.map +1 -1
- package/dist/core/extensions/runner-handlers.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +3 -2
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +20 -12
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/runtime-types.d.ts +7 -7
- package/dist/core/extensions/runtime-types.d.ts.map +1 -1
- package/dist/core/extensions/runtime-types.js.map +1 -1
- package/dist/core/extensions/session-events.d.ts +5 -5
- package/dist/core/extensions/session-events.d.ts.map +1 -1
- package/dist/core/extensions/session-events.js.map +1 -1
- package/dist/core/fast-model-routing.d.ts.map +1 -1
- package/dist/core/fast-model-routing.js +1 -0
- package/dist/core/fast-model-routing.js.map +1 -1
- package/dist/core/intercom-runtime-group.js +1 -1
- package/dist/core/intercom-runtime-group.js.map +1 -1
- package/dist/core/model-config.d.ts +765 -0
- package/dist/core/model-config.d.ts.map +1 -1
- package/dist/core/model-config.js +46 -24
- package/dist/core/model-config.js.map +1 -1
- package/dist/core/model-resolver-defaults.js +1 -1
- package/dist/core/model-resolver-defaults.js.map +1 -1
- package/dist/core/provider-attribution.js +1 -1
- package/dist/core/provider-attribution.js.map +1 -1
- package/dist/core/resource-loader-reload.js +1 -1
- package/dist/core/resource-loader-reload.js.map +1 -1
- package/dist/core/resource-loader-types.d.ts +1 -1
- package/dist/core/resource-loader-types.d.ts.map +1 -1
- package/dist/core/resource-loader-types.js.map +1 -1
- package/dist/core/sdk-types.d.ts +4 -0
- package/dist/core/sdk-types.d.ts.map +1 -1
- package/dist/core/sdk-types.js.map +1 -1
- package/dist/core/sdk.d.ts +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +50 -20
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-lifecycle-work.d.ts +3 -0
- package/dist/core/session-lifecycle-work.d.ts.map +1 -1
- package/dist/core/session-lifecycle-work.js +2 -0
- package/dist/core/session-lifecycle-work.js.map +1 -1
- package/dist/core/session-manager-archive.d.ts +2 -16
- package/dist/core/session-manager-archive.d.ts.map +1 -1
- package/dist/core/session-manager-archive.js +8 -104
- package/dist/core/session-manager-archive.js.map +1 -1
- package/dist/core/session-manager-core.d.ts +53 -51
- package/dist/core/session-manager-core.d.ts.map +1 -1
- package/dist/core/session-manager-core.js +547 -307
- package/dist/core/session-manager-core.js.map +1 -1
- package/dist/core/session-manager-entries.d.ts.map +1 -1
- package/dist/core/session-manager-entries.js +5 -10
- package/dist/core/session-manager-entries.js.map +1 -1
- package/dist/core/session-manager-errors.d.ts +10 -0
- package/dist/core/session-manager-errors.d.ts.map +1 -0
- package/dist/core/session-manager-errors.js +27 -0
- package/dist/core/session-manager-errors.js.map +1 -0
- package/dist/core/session-manager-list.d.ts +1 -0
- package/dist/core/session-manager-list.d.ts.map +1 -1
- package/dist/core/session-manager-list.js +21 -14
- package/dist/core/session-manager-list.js.map +1 -1
- package/dist/core/session-manager-storage.d.ts +3 -13
- package/dist/core/session-manager-storage.d.ts.map +1 -1
- package/dist/core/session-manager-storage.js +28 -69
- package/dist/core/session-manager-storage.js.map +1 -1
- package/dist/core/session-manager-types.d.ts +3 -11
- 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-validation.d.ts.map +1 -1
- package/dist/core/session-manager-validation.js +3 -2
- package/dist/core/session-manager-validation.js.map +1 -1
- package/dist/core/session-manager.d.ts +2 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +2 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/session-mutation-queue.d.ts +3 -0
- package/dist/core/session-mutation-queue.d.ts.map +1 -0
- package/dist/core/session-mutation-queue.js +14 -0
- package/dist/core/session-mutation-queue.js.map +1 -0
- package/dist/core/session-resume-args.d.ts +4 -0
- package/dist/core/session-resume-args.d.ts.map +1 -0
- package/dist/core/session-resume-args.js +9 -0
- package/dist/core/session-resume-args.js.map +1 -0
- package/dist/core/session-storage.d.ts +26 -0
- package/dist/core/session-storage.d.ts.map +1 -0
- package/dist/core/session-storage.js +155 -0
- package/dist/core/session-storage.js.map +1 -0
- package/dist/core/session-store-list.d.ts +4 -0
- package/dist/core/session-store-list.d.ts.map +1 -0
- package/dist/core/session-store-list.js +28 -0
- package/dist/core/session-store-list.js.map +1 -0
- package/dist/core/settings-manager-basic-accessors.d.ts +0 -1
- package/dist/core/settings-manager-basic-accessors.d.ts.map +1 -1
- package/dist/core/settings-manager-basic-accessors.js +0 -5
- package/dist/core/settings-manager-basic-accessors.js.map +1 -1
- package/dist/core/settings-merge.d.ts.map +1 -1
- package/dist/core/settings-merge.js +8 -2
- package/dist/core/settings-merge.js.map +1 -1
- package/dist/core/settings-schema.d.ts +74 -8
- package/dist/core/settings-schema.d.ts.map +1 -1
- package/dist/core/settings-schema.js +29 -29
- package/dist/core/settings-schema.js.map +1 -1
- package/dist/core/storage/errors.d.ts +1 -0
- package/dist/core/storage/errors.d.ts.map +1 -1
- package/dist/core/storage/errors.js.map +1 -1
- package/dist/core/storage/index.d.ts +5 -1
- package/dist/core/storage/index.d.ts.map +1 -1
- package/dist/core/storage/index.js +4 -1
- package/dist/core/storage/index.js.map +1 -1
- package/dist/core/storage/node-sqlite.d.ts +2 -0
- package/dist/core/storage/node-sqlite.d.ts.map +1 -1
- package/dist/core/storage/node-sqlite.js +47 -6
- package/dist/core/storage/node-sqlite.js.map +1 -1
- package/dist/core/storage/provider.d.ts +58 -0
- package/dist/core/storage/provider.d.ts.map +1 -0
- package/dist/core/storage/provider.js +243 -0
- package/dist/core/storage/provider.js.map +1 -0
- package/dist/core/storage/run-journal.d.ts +93 -0
- package/dist/core/storage/run-journal.d.ts.map +1 -0
- package/dist/core/storage/run-journal.js +15 -0
- package/dist/core/storage/run-journal.js.map +1 -0
- package/dist/core/storage/selection.d.ts +16 -0
- package/dist/core/storage/selection.d.ts.map +1 -0
- package/dist/core/storage/selection.js +87 -0
- package/dist/core/storage/selection.js.map +1 -0
- package/dist/core/storage/session/jsonl-session-store.d.ts +11 -2
- package/dist/core/storage/session/jsonl-session-store.d.ts.map +1 -1
- package/dist/core/storage/session/jsonl-session-store.js +127 -54
- package/dist/core/storage/session/jsonl-session-store.js.map +1 -1
- package/dist/core/storage/session/legacy-format.d.ts +18 -0
- package/dist/core/storage/session/legacy-format.d.ts.map +1 -0
- package/dist/core/storage/session/legacy-format.js +63 -0
- package/dist/core/storage/session/legacy-format.js.map +1 -0
- package/dist/core/storage/session/memory-session-store.d.ts +3 -1
- package/dist/core/storage/session/memory-session-store.d.ts.map +1 -1
- package/dist/core/storage/session/memory-session-store.js +9 -2
- package/dist/core/storage/session/memory-session-store.js.map +1 -1
- package/dist/core/storage/session/sql-session-store.d.ts +4 -1
- package/dist/core/storage/session/sql-session-store.d.ts.map +1 -1
- package/dist/core/storage/session/sql-session-store.js +25 -6
- package/dist/core/storage/session/sql-session-store.js.map +1 -1
- package/dist/core/storage/session/types.d.ts +4 -0
- package/dist/core/storage/session/types.d.ts.map +1 -1
- package/dist/core/storage/session/types.js.map +1 -1
- package/dist/core/storage/session/validation.js +1 -1
- package/dist/core/storage/session/validation.js.map +1 -1
- package/dist/core/storage/testing.d.ts +2 -0
- package/dist/core/storage/testing.d.ts.map +1 -1
- package/dist/core/storage/testing.js +45 -0
- package/dist/core/storage/testing.js.map +1 -1
- package/dist/core/tasks/completion.d.ts.map +1 -1
- package/dist/core/tasks/completion.js +2 -4
- package/dist/core/tasks/completion.js.map +1 -1
- package/dist/core/tools/bash-session-environment.d.ts.map +1 -1
- package/dist/core/tools/bash-session-environment.js +0 -2
- package/dist/core/tools/bash-session-environment.js.map +1 -1
- package/dist/core/tools/read-url.d.ts.map +1 -1
- package/dist/core/tools/read-url.js +6 -1
- package/dist/core/tools/read-url.js.map +1 -1
- package/dist/core/tools/todos-model.js +3 -3
- package/dist/core/tools/todos-model.js.map +1 -1
- package/dist/core/tools/todos-mutations.js +2 -2
- package/dist/core/tools/todos-mutations.js.map +1 -1
- package/dist/core/tools/todos-render.js +1 -1
- package/dist/core/tools/todos-render.js.map +1 -1
- package/dist/core/tools/todos-storage.js +2 -2
- package/dist/core/tools/todos-storage.js.map +1 -1
- package/dist/extensions/codemode/execute.d.ts.map +1 -1
- package/dist/extensions/codemode/execute.js +14 -1
- package/dist/extensions/codemode/execute.js.map +1 -1
- package/dist/extensions/codemode/tool.d.ts +1 -1
- package/dist/extensions/codemode/tool.d.ts.map +1 -1
- package/dist/extensions/codemode/tool.js.map +1 -1
- package/dist/extensions/herdr/index.d.ts.map +1 -1
- package/dist/extensions/herdr/index.js +6 -8
- package/dist/extensions/herdr/index.js.map +1 -1
- package/dist/extensions/mcp/index.js +1 -1
- package/dist/extensions/mcp/index.js.map +1 -1
- package/dist/extensions/mcp/tools.d.ts.map +1 -1
- package/dist/extensions/mcp/tools.js +6 -1
- package/dist/extensions/mcp/tools.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/main-session.d.ts +12 -2
- package/dist/main-session.d.ts.map +1 -1
- package/dist/main-session.js +148 -53
- package/dist/main-session.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +97 -42
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.d.ts +3 -1
- package/dist/modes/interactive/components/chat-session-host.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.js +16 -7
- package/dist/modes/interactive/components/chat-session-host.js.map +1 -1
- package/dist/modes/interactive/components/extension-input.d.ts +1 -1
- package/dist/modes/interactive/components/extension-input.d.ts.map +1 -1
- package/dist/modes/interactive/components/extension-input.js +2 -2
- package/dist/modes/interactive/components/extension-input.js.map +1 -1
- package/dist/modes/interactive/components/host-session-picker.js +1 -1
- package/dist/modes/interactive/components/host-session-picker.js.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.js +4 -1
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
- package/dist/modes/interactive/components/session-selector-delete.d.ts +1 -1
- package/dist/modes/interactive/components/session-selector-delete.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector-delete.js.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.d.ts +12 -9
- package/dist/modes/interactive/components/session-selector-list.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.js +27 -25
- package/dist/modes/interactive/components/session-selector-list.js.map +1 -1
- package/dist/modes/interactive/components/session-selector.d.ts +8 -4
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector.js +27 -18
- package/dist/modes/interactive/components/session-selector.js.map +1 -1
- package/dist/modes/interactive/components/tree-selector-component.d.ts +5 -1
- package/dist/modes/interactive/components/tree-selector-component.d.ts.map +1 -1
- package/dist/modes/interactive/components/tree-selector-component.js +20 -3
- package/dist/modes/interactive/components/tree-selector-component.js.map +1 -1
- package/dist/modes/interactive/interactive-bash-compact.js +1 -1
- package/dist/modes/interactive/interactive-bash-compact.js.map +1 -1
- package/dist/modes/interactive/interactive-deferred-startup.js +2 -2
- package/dist/modes/interactive/interactive-deferred-startup.js.map +1 -1
- package/dist/modes/interactive/interactive-editor-actions.js +2 -2
- package/dist/modes/interactive/interactive-editor-actions.js.map +1 -1
- package/dist/modes/interactive/interactive-extension-runtime.js +2 -0
- package/dist/modes/interactive/interactive-extension-runtime.js.map +1 -1
- package/dist/modes/interactive/interactive-input-handling.js +6 -4
- package/dist/modes/interactive/interactive-input-handling.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-helpers.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-helpers.js +7 -3
- package/dist/modes/interactive/interactive-mode-helpers.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.d.ts +5 -5
- 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-model-routing.js +12 -7
- package/dist/modes/interactive/interactive-model-routing.js.map +1 -1
- package/dist/modes/interactive/interactive-selectors.js +2 -2
- package/dist/modes/interactive/interactive-selectors.js.map +1 -1
- package/dist/modes/interactive/interactive-session-routing.js +38 -14
- package/dist/modes/interactive/interactive-session-routing.js.map +1 -1
- package/dist/modes/interactive/interactive-slash-commands.js +5 -3
- package/dist/modes/interactive/interactive-slash-commands.js.map +1 -1
- package/dist/modes/interactive-engine/create-isolated-runtime.d.ts.map +1 -1
- package/dist/modes/interactive-engine/create-isolated-runtime.js +8 -1
- package/dist/modes/interactive-engine/create-isolated-runtime.js.map +1 -1
- package/dist/modes/interactive-engine/engine-args.d.ts.map +1 -1
- package/dist/modes/interactive-engine/engine-args.js +11 -4
- package/dist/modes/interactive-engine/engine-args.js.map +1 -1
- package/dist/modes/interactive-engine/isolated-runtime.d.ts +1 -1
- package/dist/modes/interactive-engine/isolated-runtime.d.ts.map +1 -1
- package/dist/modes/interactive-engine/isolated-runtime.js +99 -74
- package/dist/modes/interactive-engine/isolated-runtime.js.map +1 -1
- package/dist/modes/rpc/rpc-bash-request-owners.js +1 -1
- package/dist/modes/rpc/rpc-bash-request-owners.js.map +1 -1
- package/dist/modes/rpc/rpc-client-api.d.ts +1 -1
- package/dist/modes/rpc/rpc-client-api.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client-api.js +2 -2
- package/dist/modes/rpc/rpc-client-api.js.map +1 -1
- package/dist/modes/rpc/rpc-client-process.d.ts +2 -2
- package/dist/modes/rpc/rpc-client-process.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client-process.js +20 -7
- package/dist/modes/rpc/rpc-client-process.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +4 -1
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +6 -2
- 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 +12 -9
- package/dist/modes/rpc/rpc-command-handler.js.map +1 -1
- package/dist/modes/rpc/rpc-provider-auth.js +2 -2
- package/dist/modes/rpc/rpc-provider-auth.js.map +1 -1
- package/dist/modes/rpc/rpc-terminal-drain.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-terminal-drain.js +11 -2
- package/dist/modes/rpc/rpc-terminal-drain.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +6 -2
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/utils/canonical-path.d.ts +5 -0
- package/dist/utils/canonical-path.d.ts.map +1 -0
- package/dist/utils/canonical-path.js +26 -0
- package/dist/utils/canonical-path.js.map +1 -0
- package/dist/utils/paths.d.ts +1 -7
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/paths.js +1 -15
- package/dist/utils/paths.js.map +1 -1
- package/docs/codemode.md +2 -0
- package/docs/compaction.md +2 -2
- package/docs/custom-provider/oauth.md +2 -2
- package/docs/custom-provider/override.md +3 -3
- package/docs/custom-provider/registration.md +7 -7
- package/docs/custom-provider/streaming.md +1 -1
- package/docs/custom-provider.md +5 -5
- package/docs/docs.json +1 -0
- package/docs/environment-variables.md +10 -2
- package/docs/extensions/api-reference.md +40 -19
- package/docs/extensions/authoring.md +1 -1
- package/docs/extensions/events.md +8 -8
- package/docs/extensions.md +2 -2
- package/docs/getting-started/installation.md +1 -2
- package/docs/models/reference.md +2 -0
- package/docs/models-configuration.md +206 -0
- package/docs/models.md +11 -1
- package/docs/providers.md +4 -4
- package/docs/quickstart.md +2 -1
- package/docs/reference/cli.md +7 -3
- package/docs/rpc/protocol.md +10 -4
- package/docs/sdk/reference.md +15 -13
- package/docs/sdk/web-server.md +5 -6
- package/docs/sdk.md +56 -2
- package/docs/session-format.md +35 -28
- package/docs/sessions.md +50 -17
- package/docs/settings.md +44 -7
- package/docs/storage.md +346 -2
- package/docs/usage.md +3 -2
- package/docs/workflows/api-reference.md +80 -16
- package/docs/workflows/operations.md +6 -4
- package/docs/workflows/reliable-design.md +16 -16
- package/examples/extensions/bookmark.ts +2 -2
- package/examples/extensions/handoff.ts +2 -2
- package/examples/extensions/plan-mode/index.ts +15 -13
- package/examples/extensions/preset.ts +8 -8
- package/examples/extensions/session-name.ts +1 -1
- package/examples/extensions/snake.ts +3 -1
- package/examples/extensions/space-invaders.ts +3 -1
- package/examples/extensions/tic-tac-toe.ts +80 -80
- package/examples/sdk/11-sessions.ts +5 -5
- package/examples/sdk/13-session-runtime.ts +7 -7
- package/examples/sdk/README.md +1 -1
- package/npm-shrinkwrap.json +53 -53
- package/package.json +7 -6
- package/schemas/models.schema.json +62 -29
- package/schemas/settings.schema.json +137 -53
- package/dist/builtin/workflows/builtin/chunk-h5fn47y5.js +0 -8
- package/dist/builtin/workflows/src/durable/journal/node-sqlite.d.ts +0 -20
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
1
2
|
import type { SqliteDatabase } from "./database.js";
|
|
2
3
|
export declare function openNodeSqliteDatabase(path: string, { busyTimeoutMs }?: {
|
|
3
4
|
busyTimeoutMs?: number;
|
|
4
5
|
}): Promise<SqliteDatabase>;
|
|
6
|
+
export declare function wrapNodeSqliteDatabase(connection: DatabaseSync): SqliteDatabase;
|
|
5
7
|
//# sourceMappingURL=node-sqlite.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-sqlite.d.ts","sourceRoot":"","sources":["../../../src/core/storage/node-sqlite.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"node-sqlite.d.ts","sourceRoot":"","sources":["../../../src/core/storage/node-sqlite.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,cAAc,EAA6D,MAAM,eAAe,CAAC;AAuM/G,wBAAsB,sBAAsB,CAC3C,IAAI,EAAE,MAAM,EACZ,EAAE,aAAoB,EAAE,GAAE;IAAE,aAAa,CAAC,EAAE,MAAM,CAAA;CAAO,GACvD,OAAO,CAAC,cAAc,CAAC,CA2BzB;AAED,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,YAAY,GAAG,cAAc,CAW/E"}
|
|
@@ -1,6 +1,29 @@
|
|
|
1
|
-
import { mkdir } from "node:fs/promises";
|
|
1
|
+
import { mkdir, realpath } from "node:fs/promises";
|
|
2
2
|
import { dirname } from "node:path";
|
|
3
3
|
import { StorageAdapterLoadError, StorageInvalidTextError, StorageRollbackError, StorageTransactionClosedError, StorageUnavailableError, StoreClosedError, } from "./errors.js";
|
|
4
|
+
// Share gates across module reloads as well as adapter instances. Only live queues retain file identities.
|
|
5
|
+
const TRANSACTION_GATES = Symbol.for("atomic-storage/node-sqlite-transaction-gates@1");
|
|
6
|
+
const gateBag = globalThis;
|
|
7
|
+
const gates = gateBag[TRANSACTION_GATES] ?? { files: new Map(), connections: new WeakMap() };
|
|
8
|
+
gateBag[TRANSACTION_GATES] = gates;
|
|
9
|
+
function gateTransaction(identity, operation) {
|
|
10
|
+
const prior = typeof identity === "string" ? gates.files.get(identity) : gates.connections.get(identity);
|
|
11
|
+
const result = (prior ?? Promise.resolve()).then(operation);
|
|
12
|
+
const settled = result.then(() => { }, () => { });
|
|
13
|
+
if (typeof identity === "string")
|
|
14
|
+
gates.files.set(identity, settled);
|
|
15
|
+
else
|
|
16
|
+
gates.connections.set(identity, settled);
|
|
17
|
+
void settled.then(() => {
|
|
18
|
+
if (typeof identity === "string") {
|
|
19
|
+
if (gates.files.get(identity) === settled)
|
|
20
|
+
gates.files.delete(identity);
|
|
21
|
+
}
|
|
22
|
+
else if (gates.connections.get(identity) === settled)
|
|
23
|
+
gates.connections.delete(identity);
|
|
24
|
+
});
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
4
27
|
/** Copies binary parameters so queued work binds the bytes the caller passed, not later buffer contents. */
|
|
5
28
|
function snapshotParams(params) {
|
|
6
29
|
return params.map((value) => (value instanceof Uint8Array ? new Uint8Array(value) : value));
|
|
@@ -33,7 +56,11 @@ class NodeSqliteDatabase {
|
|
|
33
56
|
closing = false;
|
|
34
57
|
closePromise;
|
|
35
58
|
executor;
|
|
36
|
-
|
|
59
|
+
ownsConnection;
|
|
60
|
+
transactionIdentity;
|
|
61
|
+
constructor(connection, descriptor, ownsConnection = true, transactionIdentity = connection) {
|
|
62
|
+
this.transactionIdentity = transactionIdentity;
|
|
63
|
+
this.ownsConnection = ownsConnection;
|
|
37
64
|
this.connection = connection;
|
|
38
65
|
this.descriptor = descriptor;
|
|
39
66
|
this.executor = this.createExecutor(() => true);
|
|
@@ -95,7 +122,7 @@ class NodeSqliteDatabase {
|
|
|
95
122
|
return this.enqueue(() => this.executor.all(sql, ...bound));
|
|
96
123
|
}
|
|
97
124
|
transaction(callback) {
|
|
98
|
-
return this.enqueue(async () => {
|
|
125
|
+
return this.enqueue(() => gateTransaction(this.transactionIdentity, async () => {
|
|
99
126
|
await this.executor.exec("BEGIN IMMEDIATE");
|
|
100
127
|
let active = true;
|
|
101
128
|
const transaction = this.createExecutor(() => active);
|
|
@@ -115,13 +142,14 @@ class NodeSqliteDatabase {
|
|
|
115
142
|
}
|
|
116
143
|
throw cause;
|
|
117
144
|
}
|
|
118
|
-
});
|
|
145
|
+
}));
|
|
119
146
|
}
|
|
120
147
|
close() {
|
|
121
148
|
if (!this.closePromise) {
|
|
122
149
|
this.closing = true;
|
|
123
150
|
this.closePromise = this.tail.then(() => this.driver(() => {
|
|
124
|
-
this.
|
|
151
|
+
if (this.ownsConnection)
|
|
152
|
+
this.connection.close();
|
|
125
153
|
finalizeReleasedStatements();
|
|
126
154
|
}));
|
|
127
155
|
}
|
|
@@ -146,7 +174,9 @@ export async function openNodeSqliteDatabase(path, { busyTimeoutMs = 5000 } = {}
|
|
|
146
174
|
connection = new Database(path);
|
|
147
175
|
connection.exec(`PRAGMA busy_timeout=${busyTimeoutMs}`);
|
|
148
176
|
connection.exec("PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON");
|
|
149
|
-
|
|
177
|
+
const location = connection.location();
|
|
178
|
+
const identity = location === null ? connection : await realpath(location);
|
|
179
|
+
return new NodeSqliteDatabase(connection, descriptor, true, identity);
|
|
150
180
|
}
|
|
151
181
|
catch (cause) {
|
|
152
182
|
try {
|
|
@@ -158,4 +188,15 @@ export async function openNodeSqliteDatabase(path, { busyTimeoutMs = 5000 } = {}
|
|
|
158
188
|
throw new StorageUnavailableError(descriptor, cause);
|
|
159
189
|
}
|
|
160
190
|
}
|
|
191
|
+
export function wrapNodeSqliteDatabase(connection) {
|
|
192
|
+
const descriptor = { adapter: "node-sqlite", location: ":memory:" };
|
|
193
|
+
try {
|
|
194
|
+
descriptor.location = connection.location() || ":memory:";
|
|
195
|
+
connection.exec("PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON");
|
|
196
|
+
return new NodeSqliteDatabase(connection, descriptor, false);
|
|
197
|
+
}
|
|
198
|
+
catch (cause) {
|
|
199
|
+
throw new StorageUnavailableError(descriptor, cause);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
161
202
|
//# sourceMappingURL=node-sqlite.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-sqlite.js","sourceRoot":"","sources":["../../../src/core/storage/node-sqlite.ts"],"names":[],"mapp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C,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;IAC5D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,IAAI,uBAAuB,CAAC,aAAa,EAAE,KAA4B,CAAC,CAAC;IAChF,CAAC;IACD,IAAI,UAAoC,CAAC;IACzC,IAAI,CAAC;QACJ,IAAI,IAAI,KAAK,UAAU;YAAE,MAAM,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACzE,UAAU,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC;QAChC,UAAU,CAAC,IAAI,CAAC,uBAAuB,aAAa,EAAE,CAAC,CAAC;QACxD,UAAU,CAAC,IAAI,CAAC,0EAA0E,CAAC,CAAC;QAC5F,OAAO,IAAI,kBAAkB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IACvD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,CAAC;YACJ,UAAU,EAAE,KAAK,EAAE,CAAC;QACrB,CAAC;QAAC,MAAM,CAAC;YACR,sDAAsD;QACvD,CAAC;QACD,MAAM,IAAI,uBAAuB,CAAC,UAAU,EAAE,KAA4B,CAAC,CAAC;IAC7E,CAAC;AACF,CAAC","sourcesContent":["import { mkdir } from \"node:fs/promises\";\nimport { dirname } from \"node:path\";\nimport type { DatabaseSync } from \"node:sqlite\";\nimport type { SqliteDatabase, SqliteExecutor, SqliteRow, SqliteValue, StorageDescriptor } from \"./database.js\";\nimport {\n\tStorageAdapterLoadError,\n\ttype StorageFailureCause,\n\tStorageInvalidTextError,\n\tStorageRollbackError,\n\tStorageTransactionClosedError,\n\tStorageUnavailableError,\n\tStoreClosedError,\n} from \"./errors.js\";\n\n/** Copies binary parameters so queued work binds the bytes the caller passed, not later buffer contents. */\nfunction snapshotParams(params: readonly SqliteValue[]): SqliteValue[] {\n\treturn params.map((value) => (value instanceof Uint8Array ? new Uint8Array(value) : value));\n}\n\n/**\n * Bun's `DatabaseSync.close()` defers closing the file until garbage collection finalizes every statement the\n * connection prepared, so the database, WAL and shared-memory files stay open (and locked on Windows). The adapter\n * keeps no statement references, so a synchronous collection finalizes them and releases the files. Node finalizes\n * statements inside `close()` and has no `Bun` global.\n */\nfunction finalizeReleasedStatements(): void {\n\t(globalThis as { Bun?: { gc(force: boolean): void } }).Bun?.gc(true);\n}\n\nfunction normalizeRow(row: Record<string, SqliteValue>): SqliteRow {\n\treturn Object.fromEntries(\n\t\tObject.entries(row).map(([key, value]) => [\n\t\t\tkey,\n\t\t\ttypeof value === \"bigint\" &&\n\t\t\tvalue >= -BigInt(Number.MAX_SAFE_INTEGER) &&\n\t\t\tvalue <= BigInt(Number.MAX_SAFE_INTEGER)\n\t\t\t\t? Number(value)\n\t\t\t\t: value instanceof Uint8Array\n\t\t\t\t\t? new Uint8Array(value)\n\t\t\t\t\t: value,\n\t\t]),\n\t);\n}\n\nclass NodeSqliteDatabase implements SqliteDatabase {\n\treadonly descriptor: StorageDescriptor;\n\tprivate readonly connection: DatabaseSync;\n\tprivate tail: Promise<void> = Promise.resolve();\n\tprivate closing = false;\n\tprivate closePromise?: Promise<void>;\n\tprivate readonly executor: SqliteExecutor;\n\n\tconstructor(connection: DatabaseSync, descriptor: StorageDescriptor) {\n\t\tthis.connection = connection;\n\t\tthis.descriptor = descriptor;\n\t\tthis.executor = this.createExecutor(() => true);\n\t}\n\n\tprivate driver<T>(operation: () => T): T {\n\t\ttry {\n\t\t\treturn operation();\n\t\t} catch (cause) {\n\t\t\tthrow new StorageUnavailableError(this.descriptor, cause as StorageFailureCause);\n\t\t}\n\t}\n\n\tprivate createExecutor(active: () => boolean): SqliteExecutor {\n\t\tconst execute = async <T>(operation: () => T, params: readonly SqliteValue[] = []): Promise<T> => {\n\t\t\tif (!active()) throw new StorageTransactionClosedError();\n\t\t\tconst invalidIndex = params.findIndex((value) => typeof value === \"string\" && value.includes(\"\\0\"));\n\t\t\tif (invalidIndex !== -1) throw new StorageInvalidTextError(invalidIndex);\n\t\t\treturn this.driver(operation);\n\t\t};\n\t\tconst statement = (sql: string) => {\n\t\t\tconst prepared = this.connection.prepare(sql);\n\t\t\tprepared.setReadBigInts(true);\n\t\t\treturn prepared;\n\t\t};\n\t\treturn {\n\t\t\texec: (sql) => execute(() => this.connection.exec(sql)),\n\t\t\trun: (sql, ...params) => execute(() => ({ changes: Number(statement(sql).run(...params).changes) }), params),\n\t\t\tget: <T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]) =>\n\t\t\t\texecute(() => {\n\t\t\t\t\tconst row = statement(sql).get(...params);\n\t\t\t\t\treturn row === undefined ? undefined : (normalizeRow(row) as T);\n\t\t\t\t}, params),\n\t\t\tall: <T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]) =>\n\t\t\t\texecute(\n\t\t\t\t\t() =>\n\t\t\t\t\t\tstatement(sql)\n\t\t\t\t\t\t\t.all(...params)\n\t\t\t\t\t\t\t.map((row) => normalizeRow(row) as T),\n\t\t\t\t\tparams,\n\t\t\t\t),\n\t\t};\n\t}\n\n\tprivate enqueue<T>(operation: () => Promise<T>): Promise<T> {\n\t\tif (this.closing) return Promise.reject(new StoreClosedError(this.descriptor));\n\t\tconst result = this.tail.then(operation);\n\t\tthis.tail = result.then(\n\t\t\t() => {},\n\t\t\t() => {},\n\t\t);\n\t\treturn result;\n\t}\n\n\texec(sql: string): Promise<void> {\n\t\treturn this.enqueue(() => this.executor.exec(sql));\n\t}\n\n\trun(sql: string, ...params: readonly SqliteValue[]) {\n\t\tconst bound = snapshotParams(params);\n\t\treturn this.enqueue(() => this.executor.run(sql, ...bound));\n\t}\n\n\tget<T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]): Promise<T | undefined> {\n\t\tconst bound = snapshotParams(params);\n\t\treturn this.enqueue(() => this.executor.get<T>(sql, ...bound));\n\t}\n\n\tall<T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]): Promise<T[]> {\n\t\tconst bound = snapshotParams(params);\n\t\treturn this.enqueue(() => this.executor.all<T>(sql, ...bound));\n\t}\n\n\ttransaction<T>(callback: (transaction: SqliteExecutor) => Promise<T>): Promise<T> {\n\t\treturn this.enqueue(async () => {\n\t\t\tawait this.executor.exec(\"BEGIN IMMEDIATE\");\n\t\t\tlet active = true;\n\t\t\tconst transaction = this.createExecutor(() => active);\n\t\t\ttry {\n\t\t\t\tconst result = await callback(transaction);\n\t\t\t\tactive = false;\n\t\t\t\tawait this.executor.exec(\"COMMIT\");\n\t\t\t\treturn result;\n\t\t\t} catch (cause) {\n\t\t\t\tactive = false;\n\t\t\t\ttry {\n\t\t\t\t\tawait this.executor.exec(\"ROLLBACK\");\n\t\t\t\t} catch (rollbackCause) {\n\t\t\t\t\tthrow new StorageRollbackError(cause as StorageFailureCause, rollbackCause as StorageFailureCause);\n\t\t\t\t}\n\t\t\t\tthrow cause;\n\t\t\t}\n\t\t});\n\t}\n\n\tclose(): Promise<void> {\n\t\tif (!this.closePromise) {\n\t\t\tthis.closing = true;\n\t\t\tthis.closePromise = this.tail.then(() =>\n\t\t\t\tthis.driver(() => {\n\t\t\t\t\tthis.connection.close();\n\t\t\t\t\tfinalizeReleasedStatements();\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\t\treturn this.closePromise;\n\t}\n}\n\nexport async function openNodeSqliteDatabase(\n\tpath: string,\n\t{ busyTimeoutMs = 5000 }: { busyTimeoutMs?: number } = {},\n): Promise<SqliteDatabase> {\n\tconst descriptor = { adapter: \"node-sqlite\", location: path };\n\t// Load node:sqlite on first open: importing it prints Node's experimental warning, which every importer of the\n\t// package entry point would otherwise see.\n\tlet Database: typeof DatabaseSync;\n\ttry {\n\t\t({ DatabaseSync: Database } = await import(\"node:sqlite\"));\n\t} catch (cause) {\n\t\tthrow new StorageAdapterLoadError(\"node:sqlite\", cause as StorageFailureCause);\n\t}\n\tlet connection: DatabaseSync | undefined;\n\ttry {\n\t\tif (path !== \":memory:\") await mkdir(dirname(path), { recursive: true });\n\t\tconnection = new Database(path);\n\t\tconnection.exec(`PRAGMA busy_timeout=${busyTimeoutMs}`);\n\t\tconnection.exec(\"PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON\");\n\t\treturn new NodeSqliteDatabase(connection, descriptor);\n\t} catch (cause) {\n\t\ttry {\n\t\t\tconnection?.close();\n\t\t} catch {\n\t\t\t// Preserve the opening failure if cleanup also fails.\n\t\t}\n\t\tthrow new StorageUnavailableError(descriptor, cause as StorageFailureCause);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"node-sqlite.js","sourceRoot":"","sources":["../../../src/core/storage/node-sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EACN,uBAAuB,EAEvB,uBAAuB,EACvB,oBAAoB,EACpB,6BAA6B,EAC7B,uBAAuB,EACvB,gBAAgB,GAChB,MAAM,aAAa,CAAC;AAOrB,2GAA2G;AAC3G,MAAM,iBAAiB,GAAG,MAAM,CAAC,GAAG,CAAC,gDAAgD,CAAC,CAAC;AACvF,MAAM,OAAO,GAAG,UAA8E,CAAC;AAC/F,MAAM,KAAK,GAAqB,OAAO,CAAC,iBAAiB,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,EAAE,EAAE,WAAW,EAAE,IAAI,OAAO,EAAE,EAAE,CAAC;AAC/G,OAAO,CAAC,iBAAiB,CAAC,GAAG,KAAK,CAAC;AAEnC,SAAS,eAAe,CAAI,QAA+B,EAAE,SAA2B;IACvF,MAAM,KAAK,GAAG,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,GA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path } from \"node:fs/promises\";\nimport { dirname } from \"node:path\";\nimport type { DatabaseSync } from \"node:sqlite\";\nimport type { SqliteDatabase, SqliteExecutor, SqliteRow, SqliteValue, StorageDescriptor } from \"./database.js\";\nimport {\n\tStorageAdapterLoadError,\n\ttype StorageFailureCause,\n\tStorageInvalidTextError,\n\tStorageRollbackError,\n\tStorageTransactionClosedError,\n\tStorageUnavailableError,\n\tStoreClosedError,\n} from \"./errors.js\";\n\ninterface TransactionGates {\n\treadonly files: Map<string, Promise<void>>;\n\treadonly connections: WeakMap<DatabaseSync, Promise<void>>;\n}\n\n// Share gates across module reloads as well as adapter instances. Only live queues retain file identities.\nconst TRANSACTION_GATES = Symbol.for(\"atomic-storage/node-sqlite-transaction-gates@1\");\nconst gateBag = globalThis as typeof globalThis & Record<symbol, TransactionGates | undefined>;\nconst gates: TransactionGates = gateBag[TRANSACTION_GATES] ?? { files: new Map(), connections: new WeakMap() };\ngateBag[TRANSACTION_GATES] = gates;\n\nfunction gateTransaction<T>(identity: string | DatabaseSync, operation: () => Promise<T>): Promise<T> {\n\tconst prior = typeof identity === \"string\" ? gates.files.get(identity) : gates.connections.get(identity);\n\tconst result = (prior ?? Promise.resolve()).then(operation);\n\tconst settled = result.then(\n\t\t() => {},\n\t\t() => {},\n\t);\n\tif (typeof identity === \"string\") gates.files.set(identity, settled);\n\telse gates.connections.set(identity, settled);\n\tvoid settled.then(() => {\n\t\tif (typeof identity === \"string\") {\n\t\t\tif (gates.files.get(identity) === settled) gates.files.delete(identity);\n\t\t} else if (gates.connections.get(identity) === settled) gates.connections.delete(identity);\n\t});\n\treturn result;\n}\n\n/** Copies binary parameters so queued work binds the bytes the caller passed, not later buffer contents. */\nfunction snapshotParams(params: readonly SqliteValue[]): SqliteValue[] {\n\treturn params.map((value) => (value instanceof Uint8Array ? new Uint8Array(value) : value));\n}\n\n/**\n * Bun's `DatabaseSync.close()` defers closing the file until garbage collection finalizes every statement the\n * connection prepared, so the database, WAL and shared-memory files stay open (and locked on Windows). The adapter\n * keeps no statement references, so a synchronous collection finalizes them and releases the files. Node finalizes\n * statements inside `close()` and has no `Bun` global.\n */\nfunction finalizeReleasedStatements(): void {\n\t(globalThis as { Bun?: { gc(force: boolean): void } }).Bun?.gc(true);\n}\n\nfunction normalizeRow(row: Record<string, SqliteValue>): SqliteRow {\n\treturn Object.fromEntries(\n\t\tObject.entries(row).map(([key, value]) => [\n\t\t\tkey,\n\t\t\ttypeof value === \"bigint\" &&\n\t\t\tvalue >= -BigInt(Number.MAX_SAFE_INTEGER) &&\n\t\t\tvalue <= BigInt(Number.MAX_SAFE_INTEGER)\n\t\t\t\t? Number(value)\n\t\t\t\t: value instanceof Uint8Array\n\t\t\t\t\t? new Uint8Array(value)\n\t\t\t\t\t: value,\n\t\t]),\n\t);\n}\n\nclass NodeSqliteDatabase implements SqliteDatabase {\n\treadonly descriptor: StorageDescriptor;\n\tprivate readonly connection: DatabaseSync;\n\tprivate tail: Promise<void> = Promise.resolve();\n\tprivate closing = false;\n\tprivate closePromise?: Promise<void>;\n\tprivate readonly executor: SqliteExecutor;\n\n\tprivate readonly ownsConnection: boolean;\n\tprivate readonly transactionIdentity: string | DatabaseSync;\n\n\tconstructor(\n\t\tconnection: DatabaseSync,\n\t\tdescriptor: StorageDescriptor,\n\t\townsConnection = true,\n\t\ttransactionIdentity: string | DatabaseSync = connection,\n\t) {\n\t\tthis.transactionIdentity = transactionIdentity;\n\t\tthis.ownsConnection = ownsConnection;\n\t\tthis.connection = connection;\n\t\tthis.descriptor = descriptor;\n\t\tthis.executor = this.createExecutor(() => true);\n\t}\n\n\tprivate driver<T>(operation: () => T): T {\n\t\ttry {\n\t\t\treturn operation();\n\t\t} catch (cause) {\n\t\t\tthrow new StorageUnavailableError(this.descriptor, cause as StorageFailureCause);\n\t\t}\n\t}\n\n\tprivate createExecutor(active: () => boolean): SqliteExecutor {\n\t\tconst execute = async <T>(operation: () => T, params: readonly SqliteValue[] = []): Promise<T> => {\n\t\t\tif (!active()) throw new StorageTransactionClosedError();\n\t\t\tconst invalidIndex = params.findIndex((value) => typeof value === \"string\" && value.includes(\"\\0\"));\n\t\t\tif (invalidIndex !== -1) throw new StorageInvalidTextError(invalidIndex);\n\t\t\treturn this.driver(operation);\n\t\t};\n\t\tconst statement = (sql: string) => {\n\t\t\tconst prepared = this.connection.prepare(sql);\n\t\t\tprepared.setReadBigInts(true);\n\t\t\treturn prepared;\n\t\t};\n\t\treturn {\n\t\t\texec: (sql) => execute(() => this.connection.exec(sql)),\n\t\t\trun: (sql, ...params) => execute(() => ({ changes: Number(statement(sql).run(...params).changes) }), params),\n\t\t\tget: <T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]) =>\n\t\t\t\texecute(() => {\n\t\t\t\t\tconst row = statement(sql).get(...params);\n\t\t\t\t\treturn row === undefined ? undefined : (normalizeRow(row) as T);\n\t\t\t\t}, params),\n\t\t\tall: <T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]) =>\n\t\t\t\texecute(\n\t\t\t\t\t() =>\n\t\t\t\t\t\tstatement(sql)\n\t\t\t\t\t\t\t.all(...params)\n\t\t\t\t\t\t\t.map((row) => normalizeRow(row) as T),\n\t\t\t\t\tparams,\n\t\t\t\t),\n\t\t};\n\t}\n\n\tprivate enqueue<T>(operation: () => Promise<T>): Promise<T> {\n\t\tif (this.closing) return Promise.reject(new StoreClosedError(this.descriptor));\n\t\tconst result = this.tail.then(operation);\n\t\tthis.tail = result.then(\n\t\t\t() => {},\n\t\t\t() => {},\n\t\t);\n\t\treturn result;\n\t}\n\n\texec(sql: string): Promise<void> {\n\t\treturn this.enqueue(() => this.executor.exec(sql));\n\t}\n\n\trun(sql: string, ...params: readonly SqliteValue[]) {\n\t\tconst bound = snapshotParams(params);\n\t\treturn this.enqueue(() => this.executor.run(sql, ...bound));\n\t}\n\n\tget<T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]): Promise<T | undefined> {\n\t\tconst bound = snapshotParams(params);\n\t\treturn this.enqueue(() => this.executor.get<T>(sql, ...bound));\n\t}\n\n\tall<T extends SqliteRow>(sql: string, ...params: readonly SqliteValue[]): Promise<T[]> {\n\t\tconst bound = snapshotParams(params);\n\t\treturn this.enqueue(() => this.executor.all<T>(sql, ...bound));\n\t}\n\n\ttransaction<T>(callback: (transaction: SqliteExecutor) => Promise<T>): Promise<T> {\n\t\treturn this.enqueue(() =>\n\t\t\tgateTransaction(this.transactionIdentity, async () => {\n\t\t\t\tawait this.executor.exec(\"BEGIN IMMEDIATE\");\n\t\t\t\tlet active = true;\n\t\t\t\tconst transaction = this.createExecutor(() => active);\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await callback(transaction);\n\t\t\t\t\tactive = false;\n\t\t\t\t\tawait this.executor.exec(\"COMMIT\");\n\t\t\t\t\treturn result;\n\t\t\t\t} catch (cause) {\n\t\t\t\t\tactive = false;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait this.executor.exec(\"ROLLBACK\");\n\t\t\t\t\t} catch (rollbackCause) {\n\t\t\t\t\t\tthrow new StorageRollbackError(cause as StorageFailureCause, rollbackCause as StorageFailureCause);\n\t\t\t\t\t}\n\t\t\t\t\tthrow cause;\n\t\t\t\t}\n\t\t\t}),\n\t\t);\n\t}\n\n\tclose(): Promise<void> {\n\t\tif (!this.closePromise) {\n\t\t\tthis.closing = true;\n\t\t\tthis.closePromise = this.tail.then(() =>\n\t\t\t\tthis.driver(() => {\n\t\t\t\t\tif (this.ownsConnection) this.connection.close();\n\t\t\t\t\tfinalizeReleasedStatements();\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\t\treturn this.closePromise;\n\t}\n}\n\nexport async function openNodeSqliteDatabase(\n\tpath: string,\n\t{ busyTimeoutMs = 5000 }: { busyTimeoutMs?: number } = {},\n): Promise<SqliteDatabase> {\n\tconst descriptor = { adapter: \"node-sqlite\", location: path };\n\t// Load node:sqlite on first open: importing it prints Node's experimental warning, which every importer of the\n\t// package entry point would otherwise see.\n\tlet Database: typeof DatabaseSync;\n\ttry {\n\t\t({ DatabaseSync: Database } = await import(\"node:sqlite\"));\n\t} catch (cause) {\n\t\tthrow new StorageAdapterLoadError(\"node:sqlite\", cause as StorageFailureCause);\n\t}\n\tlet connection: DatabaseSync | undefined;\n\ttry {\n\t\tif (path !== \":memory:\") await mkdir(dirname(path), { recursive: true });\n\t\tconnection = new Database(path);\n\t\tconnection.exec(`PRAGMA busy_timeout=${busyTimeoutMs}`);\n\t\tconnection.exec(\"PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON\");\n\t\tconst location = connection.location();\n\t\tconst identity = location === null ? connection : await realpath(location);\n\t\treturn new NodeSqliteDatabase(connection, descriptor, true, identity);\n\t} catch (cause) {\n\t\ttry {\n\t\t\tconnection?.close();\n\t\t} catch {\n\t\t\t// Preserve the opening failure if cleanup also fails.\n\t\t}\n\t\tthrow new StorageUnavailableError(descriptor, cause as StorageFailureCause);\n\t}\n}\n\nexport function wrapNodeSqliteDatabase(connection: DatabaseSync): SqliteDatabase {\n\tconst descriptor = { adapter: \"node-sqlite\", location: \":memory:\" };\n\ttry {\n\t\tdescriptor.location = connection.location() || \":memory:\";\n\t\tconnection.exec(\n\t\t\t\"PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON\",\n\t\t);\n\t\treturn new NodeSqliteDatabase(connection, descriptor, false);\n\t} catch (cause) {\n\t\tthrow new StorageUnavailableError(descriptor, cause as StorageFailureCause);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SqliteDatabase, SqliteValue, StorageDescriptor } from "./database.js";
|
|
2
|
+
import type { RunJournal } from "./run-journal.js";
|
|
3
|
+
import type { SessionStore } from "./session/types.js";
|
|
4
|
+
export type SecretRef = {
|
|
5
|
+
readonly env: string;
|
|
6
|
+
} | {
|
|
7
|
+
readonly file: string;
|
|
8
|
+
};
|
|
9
|
+
export type AdapterOptions = Readonly<Record<string, SqliteValue | boolean>>;
|
|
10
|
+
export type StorageSelection = {
|
|
11
|
+
readonly kind: "memory";
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: "jsonl";
|
|
14
|
+
readonly dir: string;
|
|
15
|
+
} | {
|
|
16
|
+
readonly kind: "sqlite";
|
|
17
|
+
readonly path: string;
|
|
18
|
+
readonly prefix?: string;
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: "database";
|
|
21
|
+
readonly database: SqliteDatabase;
|
|
22
|
+
readonly prefix?: string;
|
|
23
|
+
} | {
|
|
24
|
+
readonly kind: "adapter";
|
|
25
|
+
readonly module: string;
|
|
26
|
+
readonly options?: AdapterOptions;
|
|
27
|
+
readonly secrets?: Readonly<Record<string, SecretRef>>;
|
|
28
|
+
readonly prefix?: string;
|
|
29
|
+
};
|
|
30
|
+
export interface StorageOpenContext {
|
|
31
|
+
readonly agentDir: string;
|
|
32
|
+
readonly domain: "sessions" | "journal";
|
|
33
|
+
readonly signal?: AbortSignal;
|
|
34
|
+
}
|
|
35
|
+
export interface StorageAdapterModule {
|
|
36
|
+
readonly atomicStorageAdapter: 1;
|
|
37
|
+
openDatabase?(options: AdapterOptions, context: StorageOpenContext): Promise<SqliteDatabase>;
|
|
38
|
+
openSessionStore?(options: AdapterOptions, context: StorageOpenContext, storage: {
|
|
39
|
+
readonly prefix?: string;
|
|
40
|
+
}): Promise<SessionStore>;
|
|
41
|
+
openRunJournal?(options: AdapterOptions, context: StorageOpenContext, storage: {
|
|
42
|
+
readonly prefix?: string;
|
|
43
|
+
}): Promise<RunJournal>;
|
|
44
|
+
}
|
|
45
|
+
export interface JournalFactories {
|
|
46
|
+
memory(descriptor: StorageDescriptor): RunJournal;
|
|
47
|
+
sql(database: SqliteDatabase, options: {
|
|
48
|
+
readonly prefix?: string;
|
|
49
|
+
}): Promise<RunJournal>;
|
|
50
|
+
}
|
|
51
|
+
export declare function storageSelectionKey(selection: StorageSelection, agentDir?: string): string;
|
|
52
|
+
export declare function loadStorageAdapter(module: string, context: StorageOpenContext): Promise<StorageAdapterModule>;
|
|
53
|
+
export interface StorageProvider {
|
|
54
|
+
openSessionStore(selection: StorageSelection, context: StorageOpenContext): Promise<SessionStore>;
|
|
55
|
+
openRunJournal(selection: StorageSelection, context: StorageOpenContext): Promise<RunJournal>;
|
|
56
|
+
}
|
|
57
|
+
export declare const StorageProvider: new (journals?: JournalFactories) => StorageProvider;
|
|
58
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../src/core/storage/provider.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAUpF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,MAAM,MAAM,SAAS,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAC7E,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;AAC7E,MAAM,MAAM,gBAAgB,GACzB;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;CAAE,GAC3B;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAC5E;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC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AE,cAAc,EAAE,OAAO,EAAE;QAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC1F;AAeD,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,gBAAgB,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAqB1F;AAED,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAoBnH;AAmDD,MAAM,WAAW,eAAe;IAC/B,gBAAgB,CAAC,SAAS,EAAE,gBAAgB,EAAE,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAClG,cAAc,CAAC,SAAS,EAAE,gBAAgB,EAAE,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC9F;AAED,eAAO,MAAM,eAAe,EAAE,KAAK,QAAQ,CAAC,EAAE,gBAAgB,KAAK,eA+JlE,CAAC"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { createJiti } from "jiti";
|
|
5
|
+
import { createSessionCwdMatcher } from "../../utils/canonical-path.js";
|
|
6
|
+
import { StorageAdapterLoadError, StorageConfigError, StorageError, StorageUnavailableError, } from "./errors.js";
|
|
7
|
+
import { openNodeSqliteDatabase } from "./node-sqlite.js";
|
|
8
|
+
import { validateStoragePrefix } from "./prefix.js";
|
|
9
|
+
import { selectStorage } from "./selection.js";
|
|
10
|
+
const databaseIds = new WeakMap();
|
|
11
|
+
let nextDatabaseId = 0;
|
|
12
|
+
function storageOptionKey(value) {
|
|
13
|
+
if (value instanceof Uint8Array)
|
|
14
|
+
return ["bytes", Array.from(value)];
|
|
15
|
+
if (value === null)
|
|
16
|
+
return ["null", null];
|
|
17
|
+
if (typeof value === "number")
|
|
18
|
+
return ["number", Object.is(value, -0) ? "-0" : String(value)];
|
|
19
|
+
if (typeof value === "bigint")
|
|
20
|
+
return ["bigint", String(value)];
|
|
21
|
+
return [typeof value, value];
|
|
22
|
+
}
|
|
23
|
+
export function storageSelectionKey(selection, agentDir) {
|
|
24
|
+
if (selection.kind === "database") {
|
|
25
|
+
let id = databaseIds.get(selection.database);
|
|
26
|
+
if (id === undefined) {
|
|
27
|
+
id = ++nextDatabaseId;
|
|
28
|
+
databaseIds.set(selection.database, id);
|
|
29
|
+
}
|
|
30
|
+
return JSON.stringify(["database", id, selection.prefix ?? ""]);
|
|
31
|
+
}
|
|
32
|
+
if (selection.kind === "sqlite") {
|
|
33
|
+
const path = selection.path === "" || selection.path === ":memory:" ? selection.path : resolve(selection.path);
|
|
34
|
+
return JSON.stringify(["sqlite", path, selection.prefix ?? ""]);
|
|
35
|
+
}
|
|
36
|
+
if (selection.kind === "adapter") {
|
|
37
|
+
const options = Object.entries(selection.options ?? {})
|
|
38
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
39
|
+
.map(([name, value]) => [name, storageOptionKey(value)]);
|
|
40
|
+
const secrets = Object.entries(selection.secrets ?? {}).sort(([a], [b]) => a.localeCompare(b));
|
|
41
|
+
return JSON.stringify(["adapter", agentDir, selection.module, options, secrets, selection.prefix ?? ""]);
|
|
42
|
+
}
|
|
43
|
+
return JSON.stringify(selection);
|
|
44
|
+
}
|
|
45
|
+
export async function loadStorageAdapter(module, context) {
|
|
46
|
+
try {
|
|
47
|
+
const loader = createJiti(pathToFileURL(join(context.agentDir, "storage-adapter.js")).href, {
|
|
48
|
+
interopDefault: true,
|
|
49
|
+
});
|
|
50
|
+
const adapter = await loader.import(module, { default: true });
|
|
51
|
+
if (adapter === null ||
|
|
52
|
+
typeof adapter !== "object" ||
|
|
53
|
+
adapter.atomicStorageAdapter !== 1 ||
|
|
54
|
+
[adapter.openDatabase, adapter.openSessionStore, adapter.openRunJournal].some((hook) => hook !== undefined && typeof hook !== "function")) {
|
|
55
|
+
throw new StorageConfigError("sdk", "adapter", "invalid-module-contract");
|
|
56
|
+
}
|
|
57
|
+
return adapter;
|
|
58
|
+
}
|
|
59
|
+
catch (cause) {
|
|
60
|
+
throw new StorageAdapterLoadError(module, cause);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function adapterOptions(selection, context) {
|
|
64
|
+
const options = { ...selection.options };
|
|
65
|
+
for (const [name, ref] of Object.entries(selection.secrets ?? {})) {
|
|
66
|
+
let value;
|
|
67
|
+
if ("env" in ref) {
|
|
68
|
+
const environment = process.env[ref.env];
|
|
69
|
+
if (environment === undefined)
|
|
70
|
+
throw new StorageConfigError("sdk", `secrets.${name}`, "secret-env-missing");
|
|
71
|
+
value = environment;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
try {
|
|
75
|
+
value = await readFile(isAbsolute(ref.file) ? ref.file : join(dirname(context.agentDir), ref.file), "utf8");
|
|
76
|
+
}
|
|
77
|
+
catch (cause) {
|
|
78
|
+
throw new StorageUnavailableError({ adapter: selection.module, location: ref.file }, cause);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
Object.defineProperty(options, name, { value, enumerable: true, writable: true, configurable: true });
|
|
82
|
+
}
|
|
83
|
+
return options;
|
|
84
|
+
}
|
|
85
|
+
function withIdempotentClose(store, release) {
|
|
86
|
+
const close = store.close.bind(store);
|
|
87
|
+
let closing;
|
|
88
|
+
store.close = () => {
|
|
89
|
+
closing ??= (async () => {
|
|
90
|
+
try {
|
|
91
|
+
await close();
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
await release?.();
|
|
95
|
+
}
|
|
96
|
+
})();
|
|
97
|
+
return closing;
|
|
98
|
+
};
|
|
99
|
+
return store;
|
|
100
|
+
}
|
|
101
|
+
const sharedDatabases = new Map();
|
|
102
|
+
export const StorageProvider = class {
|
|
103
|
+
journals;
|
|
104
|
+
databases = sharedDatabases;
|
|
105
|
+
constructor(journals) {
|
|
106
|
+
this.journals = journals;
|
|
107
|
+
}
|
|
108
|
+
async openSessionStore(selection, context) {
|
|
109
|
+
return this.open(selection, context, {
|
|
110
|
+
memory: async () => new (await import("./session/memory-session-store.js")).MemorySessionStore(createSessionCwdMatcher),
|
|
111
|
+
jsonl: async (dir) => (await import("./session/jsonl-session-store.js")).JsonlSessionStore.open(dir, { exactDirectory: true }),
|
|
112
|
+
direct: (adapter, options, storage) => adapter.openSessionStore?.(options, context, storage),
|
|
113
|
+
sql: async (database, options) => (await import("./session/sql-session-store.js")).SqlSessionStore.open(database, {
|
|
114
|
+
...options,
|
|
115
|
+
cwdMatcher: createSessionCwdMatcher,
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async openRunJournal(selection, context) {
|
|
120
|
+
const journals = this.journals;
|
|
121
|
+
if (!journals)
|
|
122
|
+
throw new StorageConfigError("sdk", "journal", "adapter-domain-unsupported");
|
|
123
|
+
return this.open(selection, context, {
|
|
124
|
+
memory: () => journals.memory({ adapter: "memory", location: ":memory:" }),
|
|
125
|
+
direct: (adapter, options, storage) => adapter.openRunJournal?.(options, context, storage),
|
|
126
|
+
sql: async (database, options) => {
|
|
127
|
+
const journal = await journals.sql(database, options);
|
|
128
|
+
const createRun = journal.createRun.bind(journal);
|
|
129
|
+
journal.createRun = (header) => createRun({ ...header, storage: { ...database.descriptor } });
|
|
130
|
+
return journal;
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async open(selection, context, factories) {
|
|
135
|
+
if (selection === null || typeof selection !== "object" || Array.isArray(selection)) {
|
|
136
|
+
throw new StorageConfigError("sdk", "storage", "selection-object-required");
|
|
137
|
+
}
|
|
138
|
+
if (!["memory", "jsonl", "sqlite", "database", "adapter"].includes(selection.kind)) {
|
|
139
|
+
throw new StorageConfigError("sdk", "storage", "invalid-kind");
|
|
140
|
+
}
|
|
141
|
+
const selector = { memory: undefined, jsonl: "dir", sqlite: "path", database: "database", adapter: "module" }[selection.kind];
|
|
142
|
+
if (["path", "dir", "database", "module", "memory", "adapter"].some((key) => key in selection && key !== selector)) {
|
|
143
|
+
throw new StorageConfigError("sdk", "storage", "multiple-selection-forms");
|
|
144
|
+
}
|
|
145
|
+
if (selection.kind === "sqlite" && typeof selection.path !== "string") {
|
|
146
|
+
throw new StorageConfigError("sdk", "storage", "invalid-path");
|
|
147
|
+
}
|
|
148
|
+
if (selection.kind === "adapter" && (typeof selection.module !== "string" || selection.module.length === 0)) {
|
|
149
|
+
throw new StorageConfigError("sdk", "storage", "invalid-adapter");
|
|
150
|
+
}
|
|
151
|
+
if (selection.kind === "database" && !("database" in selection)) {
|
|
152
|
+
throw new StorageConfigError("sdk", "storage", "async-database-required");
|
|
153
|
+
}
|
|
154
|
+
if (selection.kind === "sqlite" || selection.kind === "database" || selection.kind === "adapter") {
|
|
155
|
+
selectStorage(selection.kind === "adapter" ? { ...selection, adapter: selection.module } : selection, "sdk", "storage", "");
|
|
156
|
+
}
|
|
157
|
+
if (selection.kind === "memory")
|
|
158
|
+
return withIdempotentClose(await factories.memory());
|
|
159
|
+
if (selection.kind === "jsonl") {
|
|
160
|
+
if (typeof selection.dir !== "string")
|
|
161
|
+
throw new StorageConfigError("sdk", "dir", "invalid-dir");
|
|
162
|
+
if (factories.jsonl)
|
|
163
|
+
return withIdempotentClose(await factories.jsonl(selection.dir));
|
|
164
|
+
throw new StorageConfigError("sdk", "dir", "adapter-domain-unsupported");
|
|
165
|
+
}
|
|
166
|
+
validateStoragePrefix(selection.prefix);
|
|
167
|
+
let openDatabase;
|
|
168
|
+
if (selection.kind === "adapter") {
|
|
169
|
+
const adapter = await loadStorageAdapter(selection.module, context);
|
|
170
|
+
const options = await adapterOptions(selection, context);
|
|
171
|
+
try {
|
|
172
|
+
const direct = factories.direct(adapter, options, { prefix: selection.prefix });
|
|
173
|
+
if (direct !== undefined)
|
|
174
|
+
return withIdempotentClose(await direct);
|
|
175
|
+
}
|
|
176
|
+
catch (cause) {
|
|
177
|
+
if (cause instanceof StorageError)
|
|
178
|
+
throw cause;
|
|
179
|
+
throw new StorageUnavailableError({ adapter: selection.module, location: selection.module }, cause);
|
|
180
|
+
}
|
|
181
|
+
if (!adapter.openDatabase)
|
|
182
|
+
throw new StorageConfigError("sdk", "adapter", "adapter-domain-unsupported");
|
|
183
|
+
const hook = adapter.openDatabase.bind(adapter);
|
|
184
|
+
openDatabase = async () => {
|
|
185
|
+
try {
|
|
186
|
+
return await hook(options, context);
|
|
187
|
+
}
|
|
188
|
+
catch (cause) {
|
|
189
|
+
if (cause instanceof StorageError)
|
|
190
|
+
throw cause;
|
|
191
|
+
throw new StorageUnavailableError({ adapter: selection.module, location: selection.module }, cause);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
else if (selection.kind === "database") {
|
|
196
|
+
return withIdempotentClose(await factories.sql(selection.database, { prefix: selection.prefix }));
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
openDatabase = () => openNodeSqliteDatabase(selection.path);
|
|
200
|
+
}
|
|
201
|
+
const key = storageSelectionKey({ ...selection, prefix: undefined }, context.agentDir);
|
|
202
|
+
let shared = this.databases.get(key);
|
|
203
|
+
if (!shared) {
|
|
204
|
+
shared = { ready: openDatabase(), references: 0 };
|
|
205
|
+
this.databases.set(key, shared);
|
|
206
|
+
}
|
|
207
|
+
shared.references++;
|
|
208
|
+
const entry = shared;
|
|
209
|
+
let database;
|
|
210
|
+
try {
|
|
211
|
+
database = await entry.ready;
|
|
212
|
+
}
|
|
213
|
+
catch (cause) {
|
|
214
|
+
if (this.databases.get(key) === entry)
|
|
215
|
+
this.databases.delete(key);
|
|
216
|
+
throw cause;
|
|
217
|
+
}
|
|
218
|
+
const release = async () => {
|
|
219
|
+
if (--entry.references === 0) {
|
|
220
|
+
if (this.databases.get(key) === entry)
|
|
221
|
+
this.databases.delete(key);
|
|
222
|
+
await database.close();
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
try {
|
|
226
|
+
return withIdempotentClose(await factories.sql(database, { prefix: selection.prefix }), release);
|
|
227
|
+
}
|
|
228
|
+
catch (cause) {
|
|
229
|
+
try {
|
|
230
|
+
await release();
|
|
231
|
+
}
|
|
232
|
+
catch (cleanupCause) {
|
|
233
|
+
const openingError = cause instanceof Error
|
|
234
|
+
? cause
|
|
235
|
+
: new StorageUnavailableError(database.descriptor, cause);
|
|
236
|
+
Object.defineProperty(openingError, "cleanupCause", { value: cleanupCause, configurable: true });
|
|
237
|
+
throw openingError;
|
|
238
|
+
}
|
|
239
|
+
throw cause;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../../src/core/storage/provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AAExE,OAAO,EACN,uBAAuB,EACvB,kBAAkB,EAClB,YAAY,EAEZ,uBAAuB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAyC/C,MAAM,WAAW,GAAG,IAAI,OAAO,EAA0B,CAAC;AAC1D,IAAI,cAAc,GAAG,CAAC,CAAC;AAEvB,SAAS,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;YACjB,CAAC;YAAC,OAAO,YAAY,EAAE,CAAC;gBACvB,MAAM,YAAY,GACjB,KAAK,YAAY,KAAK;oBACrB,CAAC,CAAC,KAAK;oBACP,CAAC,CAAC,IAAI,uBAAuB,CAAC,QAAQ,CAAC,UAAU,EAAE,KAA4B,CAAC,CAAC;gBACnF,MAAM,CAAC,cAAc,CAAC,YAAY,EAAE,cAAc,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;gBACjG,MAAM,YAAY,CAAC;YACpB,CAAC;YACD,MAAM,KAAK,CAAC;QACb,CAAC;IACF,CAAC;CACD,CAAC","sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { dirname, isAbsolute, join, resolve } from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport { createJiti } from \"jiti\";\nimport { createSessionCwdMatcher } from \"../../utils/canonical-path.ts\";\nimport type { SqliteDatabase, SqliteValue, StorageDescriptor } from \"./database.js\";\nimport {\n\tStorageAdapterLoadError,\n\tStorageConfigError,\n\tStorageError,\n\ttype StorageFailureCause,\n\tStorageUnavailableError,\n} from \"./errors.js\";\nimport { openNodeSqliteDatabase } from \"./node-sqlite.js\";\nimport { validateStoragePrefix } from \"./prefix.js\";\nimport type { RunJournal } from \"./run-journal.js\";\nimport { selectStorage } from \"./selection.js\";\nimport type { SessionStore } from \"./session/types.js\";\n\nexport type SecretRef = { readonly env: string } | { readonly file: string };\nexport type AdapterOptions = Readonly<Record<string, SqliteValue | boolean>>;\nexport type StorageSelection =\n\t| { readonly kind: \"memory\" }\n\t| { readonly kind: \"jsonl\"; readonly dir: string }\n\t| { readonly kind: \"sqlite\"; readonly path: string; readonly prefix?: string }\n\t| { readonly kind: \"database\"; readonly database: SqliteDatabase; readonly prefix?: string }\n\t| {\n\t\t\treadonly kind: \"adapter\";\n\t\t\treadonly module: string;\n\t\t\treadonly options?: AdapterOptions;\n\t\t\treadonly secrets?: Readonly<Record<string, SecretRef>>;\n\t\t\treadonly prefix?: string;\n\t };\nexport interface StorageOpenContext {\n\treadonly agentDir: string;\n\treadonly domain: \"sessions\" | \"journal\";\n\treadonly signal?: AbortSignal;\n}\nexport interface StorageAdapterModule {\n\treadonly atomicStorageAdapter: 1;\n\topenDatabase?(options: AdapterOptions, context: StorageOpenContext): Promise<SqliteDatabase>;\n\topenSessionStore?(\n\t\toptions: AdapterOptions,\n\t\tcontext: StorageOpenContext,\n\t\tstorage: { readonly prefix?: string },\n\t): Promise<SessionStore>;\n\topenRunJournal?(\n\t\toptions: AdapterOptions,\n\t\tcontext: StorageOpenContext,\n\t\tstorage: { readonly prefix?: string },\n\t): Promise<RunJournal>;\n}\nexport interface JournalFactories {\n\tmemory(descriptor: StorageDescriptor): RunJournal;\n\tsql(database: SqliteDatabase, options: { readonly prefix?: string }): Promise<RunJournal>;\n}\n\nconst databaseIds = new WeakMap<SqliteDatabase, number>();\nlet nextDatabaseId = 0;\n\nfunction storageOptionKey(\n\tvalue: SqliteValue | boolean,\n): readonly [string, string | boolean | readonly number[] | null] {\n\tif (value instanceof Uint8Array) return [\"bytes\", Array.from(value)];\n\tif (value === null) return [\"null\", null];\n\tif (typeof value === \"number\") return [\"number\", Object.is(value, -0) ? \"-0\" : String(value)];\n\tif (typeof value === \"bigint\") return [\"bigint\", String(value)];\n\treturn [typeof value, value];\n}\n\nexport function storageSelectionKey(selection: StorageSelection, agentDir?: string): string {\n\tif (selection.kind === \"database\") {\n\t\tlet id = databaseIds.get(selection.database);\n\t\tif (id === undefined) {\n\t\t\tid = ++nextDatabaseId;\n\t\t\tdatabaseIds.set(selection.database, id);\n\t\t}\n\t\treturn JSON.stringify([\"database\", id, selection.prefix ?? \"\"]);\n\t}\n\tif (selection.kind === \"sqlite\") {\n\t\tconst path = selection.path === \"\" || selection.path === \":memory:\" ? selection.path : resolve(selection.path);\n\t\treturn JSON.stringify([\"sqlite\", path, selection.prefix ?? \"\"]);\n\t}\n\tif (selection.kind === \"adapter\") {\n\t\tconst options = Object.entries(selection.options ?? {})\n\t\t\t.sort(([a], [b]) => a.localeCompare(b))\n\t\t\t.map(([name, value]) => [name, storageOptionKey(value)]);\n\t\tconst secrets = Object.entries(selection.secrets ?? {}).sort(([a], [b]) => a.localeCompare(b));\n\t\treturn JSON.stringify([\"adapter\", agentDir, selection.module, options, secrets, selection.prefix ?? \"\"]);\n\t}\n\treturn JSON.stringify(selection);\n}\n\nexport async function loadStorageAdapter(module: string, context: StorageOpenContext): Promise<StorageAdapterModule> {\n\ttry {\n\t\tconst loader = createJiti(pathToFileURL(join(context.agentDir, \"storage-adapter.js\")).href, {\n\t\t\tinteropDefault: true,\n\t\t});\n\t\tconst adapter = await loader.import<StorageAdapterModule>(module, { default: true });\n\t\tif (\n\t\t\tadapter === null ||\n\t\t\ttypeof adapter !== \"object\" ||\n\t\t\tadapter.atomicStorageAdapter !== 1 ||\n\t\t\t[adapter.openDatabase, adapter.openSessionStore, adapter.openRunJournal].some(\n\t\t\t\t(hook) => hook !== undefined && typeof hook !== \"function\",\n\t\t\t)\n\t\t) {\n\t\t\tthrow new StorageConfigError(\"sdk\", \"adapter\", \"invalid-module-contract\");\n\t\t}\n\t\treturn adapter;\n\t} catch (cause) {\n\t\tthrow new StorageAdapterLoadError(module, cause as StorageFailureCause);\n\t}\n}\n\nasync function adapterOptions(\n\tselection: Extract<StorageSelection, { kind: \"adapter\" }>,\n\tcontext: StorageOpenContext,\n): Promise<AdapterOptions> {\n\tconst options = { ...selection.options };\n\tfor (const [name, ref] of Object.entries(selection.secrets ?? {})) {\n\t\tlet value: string;\n\t\tif (\"env\" in ref) {\n\t\t\tconst environment = process.env[ref.env];\n\t\t\tif (environment === undefined) throw new StorageConfigError(\"sdk\", `secrets.${name}`, \"secret-env-missing\");\n\t\t\tvalue = environment;\n\t\t} else {\n\t\t\ttry {\n\t\t\t\tvalue = await readFile(isAbsolute(ref.file) ? ref.file : join(dirname(context.agentDir), ref.file), \"utf8\");\n\t\t\t} catch (cause) {\n\t\t\t\tthrow new StorageUnavailableError(\n\t\t\t\t\t{ adapter: selection.module, location: ref.file },\n\t\t\t\t\tcause as StorageFailureCause,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tObject.defineProperty(options, name, { value, enumerable: true, writable: true, configurable: true });\n\t}\n\treturn options;\n}\n\nfunction withIdempotentClose<T extends { close(): Promise<void> }>(store: T, release?: () => Promise<void>): T {\n\tconst close = store.close.bind(store);\n\tlet closing: Promise<void> | undefined;\n\tstore.close = () => {\n\t\tclosing ??= (async () => {\n\t\t\ttry {\n\t\t\t\tawait close();\n\t\t\t} finally {\n\t\t\t\tawait release?.();\n\t\t\t}\n\t\t})();\n\t\treturn closing;\n\t};\n\treturn store;\n}\n\ninterface SharedDatabase {\n\treadonly ready: Promise<SqliteDatabase>;\n\treferences: number;\n}\n\nconst sharedDatabases = new Map<string, SharedDatabase>();\n\nexport interface StorageProvider {\n\topenSessionStore(selection: StorageSelection, context: StorageOpenContext): Promise<SessionStore>;\n\topenRunJournal(selection: StorageSelection, context: StorageOpenContext): Promise<RunJournal>;\n}\n\nexport const StorageProvider: new (journals?: JournalFactories) => StorageProvider = class {\n\tprivate readonly journals?: JournalFactories;\n\tprivate readonly databases = sharedDatabases;\n\n\tconstructor(journals?: JournalFactories) {\n\t\tthis.journals = journals;\n\t}\n\n\tasync openSessionStore(selection: StorageSelection, context: StorageOpenContext): Promise<SessionStore> {\n\t\treturn this.open<SessionStore>(selection, context, {\n\t\t\tmemory: async () =>\n\t\t\t\tnew (await import(\"./session/memory-session-store.js\")).MemorySessionStore(createSessionCwdMatcher),\n\t\t\tjsonl: async (dir) =>\n\t\t\t\t(await import(\"./session/jsonl-session-store.js\")).JsonlSessionStore.open(dir, { exactDirectory: true }),\n\t\t\tdirect: (adapter, options, storage) => adapter.openSessionStore?.(options, context, storage),\n\t\t\tsql: async (database, options) =>\n\t\t\t\t(await import(\"./session/sql-session-store.js\")).SqlSessionStore.open(database, {\n\t\t\t\t\t...options,\n\t\t\t\t\tcwdMatcher: createSessionCwdMatcher,\n\t\t\t\t}),\n\t\t});\n\t}\n\n\tasync openRunJournal(selection: StorageSelection, context: StorageOpenContext): Promise<RunJournal> {\n\t\tconst journals = this.journals;\n\t\tif (!journals) throw new StorageConfigError(\"sdk\", \"journal\", \"adapter-domain-unsupported\");\n\t\treturn this.open<RunJournal>(selection, context, {\n\t\t\tmemory: () => journals.memory({ adapter: \"memory\", location: \":memory:\" }),\n\t\t\tdirect: (adapter, options, storage) => adapter.openRunJournal?.(options, context, storage),\n\t\t\tsql: async (database, options) => {\n\t\t\t\tconst journal = await journals.sql(database, options);\n\t\t\t\tconst createRun = journal.createRun.bind(journal);\n\t\t\t\tjournal.createRun = (header) => createRun({ ...header, storage: { ...database.descriptor } });\n\t\t\t\treturn journal;\n\t\t\t},\n\t\t});\n\t}\n\n\tprivate async open<T extends { close(): Promise<void> }>(\n\t\tselection: StorageSelection,\n\t\tcontext: StorageOpenContext,\n\t\tfactories: {\n\t\t\tmemory(): T | Promise<T>;\n\t\t\tjsonl?(dir: string): Promise<T>;\n\t\t\tdirect(\n\t\t\t\tadapter: StorageAdapterModule,\n\t\t\t\toptions: AdapterOptions,\n\t\t\t\tstorage: { readonly prefix?: string },\n\t\t\t): Promise<T> | undefined;\n\t\t\tsql(database: SqliteDatabase, options: { readonly prefix?: string }): Promise<T>;\n\t\t},\n\t): Promise<T> {\n\t\tif (selection === null || typeof selection !== \"object\" || Array.isArray(selection)) {\n\t\t\tthrow new StorageConfigError(\"sdk\", \"storage\", \"selection-object-required\");\n\t\t}\n\t\tif (![\"memory\", \"jsonl\", \"sqlite\", \"database\", \"adapter\"].includes(selection.kind)) {\n\t\t\tthrow new StorageConfigError(\"sdk\", \"storage\", \"invalid-kind\");\n\t\t}\n\t\tconst selector = { memory: undefined, jsonl: \"dir\", sqlite: \"path\", database: \"database\", adapter: \"module\" }[\n\t\t\tselection.kind\n\t\t];\n\t\tif (\n\t\t\t[\"path\", \"dir\", \"database\", \"module\", \"memory\", \"adapter\"].some((key) => key in selection && key !== selector)\n\t\t) {\n\t\t\tthrow new StorageConfigError(\"sdk\", \"storage\", \"multiple-selection-forms\");\n\t\t}\n\t\tif (selection.kind === \"sqlite\" && typeof selection.path !== \"string\") {\n\t\t\tthrow new StorageConfigError(\"sdk\", \"storage\", \"invalid-path\");\n\t\t}\n\t\tif (selection.kind === \"adapter\" && (typeof selection.module !== \"string\" || selection.module.length === 0)) {\n\t\t\tthrow new StorageConfigError(\"sdk\", \"storage\", \"invalid-adapter\");\n\t\t}\n\t\tif (selection.kind === \"database\" && !(\"database\" in selection)) {\n\t\t\tthrow new StorageConfigError(\"sdk\", \"storage\", \"async-database-required\");\n\t\t}\n\t\tif (selection.kind === \"sqlite\" || selection.kind === \"database\" || selection.kind === \"adapter\") {\n\t\t\tselectStorage(\n\t\t\t\tselection.kind === \"adapter\" ? { ...selection, adapter: selection.module } : selection,\n\t\t\t\t\"sdk\",\n\t\t\t\t\"storage\",\n\t\t\t\t\"\",\n\t\t\t);\n\t\t}\n\t\tif (selection.kind === \"memory\") return withIdempotentClose(await factories.memory());\n\t\tif (selection.kind === \"jsonl\") {\n\t\t\tif (typeof selection.dir !== \"string\") throw new StorageConfigError(\"sdk\", \"dir\", \"invalid-dir\");\n\t\t\tif (factories.jsonl) return withIdempotentClose(await factories.jsonl(selection.dir));\n\t\t\tthrow new StorageConfigError(\"sdk\", \"dir\", \"adapter-domain-unsupported\");\n\t\t}\n\t\tvalidateStoragePrefix(selection.prefix);\n\t\tlet openDatabase: () => Promise<SqliteDatabase>;\n\t\tif (selection.kind === \"adapter\") {\n\t\t\tconst adapter = await loadStorageAdapter(selection.module, context);\n\t\t\tconst options = await adapterOptions(selection, context);\n\t\t\ttry {\n\t\t\t\tconst direct = factories.direct(adapter, options, { prefix: selection.prefix });\n\t\t\t\tif (direct !== undefined) return withIdempotentClose(await direct);\n\t\t\t} catch (cause) {\n\t\t\t\tif (cause instanceof StorageError) throw cause;\n\t\t\t\tthrow new StorageUnavailableError(\n\t\t\t\t\t{ adapter: selection.module, location: selection.module },\n\t\t\t\t\tcause as StorageFailureCause,\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!adapter.openDatabase) throw new StorageConfigError(\"sdk\", \"adapter\", \"adapter-domain-unsupported\");\n\t\t\tconst hook = adapter.openDatabase.bind(adapter);\n\t\t\topenDatabase = async () => {\n\t\t\t\ttry {\n\t\t\t\t\treturn await hook(options, context);\n\t\t\t\t} catch (cause) {\n\t\t\t\t\tif (cause instanceof StorageError) throw cause;\n\t\t\t\t\tthrow new StorageUnavailableError(\n\t\t\t\t\t\t{ adapter: selection.module, location: selection.module },\n\t\t\t\t\t\tcause as StorageFailureCause,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t};\n\t\t} else if (selection.kind === \"database\") {\n\t\t\treturn withIdempotentClose(await factories.sql(selection.database, { prefix: selection.prefix }));\n\t\t} else {\n\t\t\topenDatabase = () => openNodeSqliteDatabase(selection.path);\n\t\t}\n\t\tconst key = storageSelectionKey({ ...selection, prefix: undefined }, context.agentDir);\n\t\tlet shared = this.databases.get(key);\n\t\tif (!shared) {\n\t\t\tshared = { ready: openDatabase(), references: 0 };\n\t\t\tthis.databases.set(key, shared);\n\t\t}\n\t\tshared.references++;\n\t\tconst entry = shared;\n\t\tlet database: SqliteDatabase;\n\t\ttry {\n\t\t\tdatabase = await entry.ready;\n\t\t} catch (cause) {\n\t\t\tif (this.databases.get(key) === entry) this.databases.delete(key);\n\t\t\tthrow cause;\n\t\t}\n\t\tconst release = async () => {\n\t\t\tif (--entry.references === 0) {\n\t\t\t\tif (this.databases.get(key) === entry) this.databases.delete(key);\n\t\t\t\tawait database.close();\n\t\t\t}\n\t\t};\n\t\ttry {\n\t\t\treturn withIdempotentClose(await factories.sql(database, { prefix: selection.prefix }), release);\n\t\t} catch (cause) {\n\t\t\ttry {\n\t\t\t\tawait release();\n\t\t\t} catch (cleanupCause) {\n\t\t\t\tconst openingError =\n\t\t\t\t\tcause instanceof Error\n\t\t\t\t\t\t? cause\n\t\t\t\t\t\t: new StorageUnavailableError(database.descriptor, cause as StorageFailureCause);\n\t\t\t\tObject.defineProperty(openingError, \"cleanupCause\", { value: cleanupCause, configurable: true });\n\t\t\t\tthrow openingError;\n\t\t\t}\n\t\t\tthrow cause;\n\t\t}\n\t}\n};\n"]}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { StorageDescriptor } from "./database.js";
|
|
2
|
+
export declare const RUN_EVENT_TYPES: {
|
|
3
|
+
readonly runStarted: "RunStarted";
|
|
4
|
+
readonly runFinished: "RunFinished";
|
|
5
|
+
readonly statusChanged: "StatusChanged";
|
|
6
|
+
readonly checkpointRecorded: "CheckpointRecorded";
|
|
7
|
+
readonly promptAsked: "PromptAsked";
|
|
8
|
+
readonly promptReleased: "PromptReleased";
|
|
9
|
+
readonly controlRequested: "ControlRequested";
|
|
10
|
+
readonly messageQueued: "MessageQueued";
|
|
11
|
+
readonly messageDelivered: "MessageDelivered";
|
|
12
|
+
readonly messageSettled: "MessageSettled";
|
|
13
|
+
readonly timerScheduled: "TimerScheduled";
|
|
14
|
+
readonly summaryRecorded: "SummaryRecorded";
|
|
15
|
+
};
|
|
16
|
+
export type RunEventType = (typeof RUN_EVENT_TYPES)[keyof typeof RUN_EVENT_TYPES];
|
|
17
|
+
export type JournalSerializableValue = string | number | boolean | null | readonly JournalSerializableValue[] | {
|
|
18
|
+
readonly [key: string]: JournalSerializableValue | undefined;
|
|
19
|
+
};
|
|
20
|
+
export interface RunHeader {
|
|
21
|
+
readonly runId: string;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly cwd: string;
|
|
24
|
+
readonly createdAt: number;
|
|
25
|
+
readonly status: string;
|
|
26
|
+
readonly inputs?: JournalSerializableValue;
|
|
27
|
+
readonly storage?: StorageDescriptor;
|
|
28
|
+
}
|
|
29
|
+
export type RunSummary = RunHeader;
|
|
30
|
+
type EventOf<Type extends string, Payload = JournalSerializableValue> = {
|
|
31
|
+
readonly type: Type;
|
|
32
|
+
readonly payload: Payload;
|
|
33
|
+
};
|
|
34
|
+
export type RunEvent = EventOf<"RunStarted" | "RunFinished"> | EventOf<"StatusChanged", {
|
|
35
|
+
readonly status: string;
|
|
36
|
+
readonly [key: string]: JournalSerializableValue;
|
|
37
|
+
}> | EventOf<"CheckpointRecorded", {
|
|
38
|
+
readonly kind: "tool" | "ui" | "stage" | "task" | "boundary";
|
|
39
|
+
readonly [key: string]: JournalSerializableValue;
|
|
40
|
+
}> | EventOf<"PromptAsked" | "PromptReleased" | "ControlRequested" | "MessageQueued" | "MessageDelivered" | "MessageSettled" | "TimerScheduled" | "SummaryRecorded">;
|
|
41
|
+
export interface Lease {
|
|
42
|
+
readonly runId: string;
|
|
43
|
+
readonly holderId: string;
|
|
44
|
+
readonly epoch: number;
|
|
45
|
+
}
|
|
46
|
+
export type LeaseState = Lease & {
|
|
47
|
+
readonly renewedAt: number;
|
|
48
|
+
readonly ageMs: number;
|
|
49
|
+
};
|
|
50
|
+
export type LeaseOutcome = {
|
|
51
|
+
readonly status: "acquired";
|
|
52
|
+
readonly lease: Lease;
|
|
53
|
+
} | {
|
|
54
|
+
readonly status: "held";
|
|
55
|
+
readonly holderId: string | null;
|
|
56
|
+
readonly epoch: number;
|
|
57
|
+
};
|
|
58
|
+
export interface JournalEntry {
|
|
59
|
+
readonly runId: string;
|
|
60
|
+
readonly seq: number;
|
|
61
|
+
readonly epoch: number;
|
|
62
|
+
readonly key?: string;
|
|
63
|
+
readonly event: RunEvent;
|
|
64
|
+
readonly at: number;
|
|
65
|
+
}
|
|
66
|
+
export type AppendOutcome = {
|
|
67
|
+
readonly status: "appended" | "exists";
|
|
68
|
+
readonly entry: JournalEntry;
|
|
69
|
+
};
|
|
70
|
+
export interface RunJournal {
|
|
71
|
+
createRun(header: RunHeader): Promise<"created" | "exists">;
|
|
72
|
+
getRun(runId: string): Promise<RunSummary | undefined>;
|
|
73
|
+
listRuns(filter?: {
|
|
74
|
+
cwd?: string;
|
|
75
|
+
}): Promise<readonly RunSummary[]>;
|
|
76
|
+
acquire(runId: string, holderId: string): Promise<LeaseOutcome>;
|
|
77
|
+
takeover(runId: string, holderId: string, expectedHolder: string): Promise<LeaseOutcome>;
|
|
78
|
+
takeoverExpired(runId: string, holderId: string, staleAfterMs: number): Promise<LeaseOutcome>;
|
|
79
|
+
leaseState(runId: string): Promise<LeaseState | undefined>;
|
|
80
|
+
renew(lease: Lease): Promise<void>;
|
|
81
|
+
release(lease: Lease): Promise<void>;
|
|
82
|
+
append(lease: Lease, event: RunEvent, options?: {
|
|
83
|
+
key?: string;
|
|
84
|
+
expectSeq?: number;
|
|
85
|
+
}): Promise<AppendOutcome>;
|
|
86
|
+
read(runId: string, fromSeq?: number): AsyncIterable<JournalEntry>;
|
|
87
|
+
readKey(runId: string, key: string): Promise<JournalEntry | undefined>;
|
|
88
|
+
deleteOwnedRun(lease: Lease, expectSeq?: number): Promise<void>;
|
|
89
|
+
deleteRun(runId: string): Promise<void>;
|
|
90
|
+
close(): Promise<void>;
|
|
91
|
+
}
|
|
92
|
+
export {};
|
|
93
|
+
//# sourceMappingURL=run-journal.d.ts.map
|