@jungjaehoon/mama-core 2.4.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -239
- package/db/migrations/030-case-first-memory-substrate.sql +13 -98
- package/db/migrations/034-add-connector-event-scope-columns.sql +9 -13
- package/db/migrations/039-add-connector-event-operator-ingest-seq.sql +9 -95
- package/db/migrations/062-refresh-connector-event-sequences.sql +9 -113
- package/db/migrations/063-refresh-legacy-connector-event-updates.sql +9 -32
- package/db/migrations/067-add-connector-event-source-entity-id.sql +9 -13
- package/db/migrations/069-create-registry-nodes.sql +52 -0
- package/db/migrations/070-record-identity.sql +35 -0
- package/db/migrations/071-service-operation-origins.sql +187 -0
- package/db/migrations/072-observation-versions.sql +29 -0
- package/db/migrations/073-registry-corrections.sql +45 -0
- package/db/migrations/074-work-graph-ref-kinds.sql +39 -0
- package/db/migrations/075-agent-judgments.sql +58 -0
- package/db/migrations/076-scoped-checkpoint-bindings.sql +15 -0
- package/db/migrations/077-legacy-record-kind.sql +6 -0
- package/db/migrations/078-source-ingest-commands.sql +18 -0
- package/db/migrations/079-twin-edge-relations.sql +43 -0
- package/db/migrations/080-restore-duration-days.sql +7 -0
- package/db/migrations/081-mailbox.sql +61 -0
- package/db/migrations/082-commitments-row-id.sql +12 -0
- package/db/migrations/083-observation-is-the-evidence.sql +49 -0
- package/db/migrations/084-scope-kind-is-not-the-cores-to-list.sql +34 -0
- package/db/migrations/085-a-migration-has-a-source.sql +37 -0
- package/db/migrations/086-the-mailbox-is-generic.sql +110 -0
- package/db/migrations/087-mailbox-payload.sql +5 -0
- package/db/migrations/088-native-input-journal.sql +14 -0
- package/db/migrations/089-native-turn-receipt-lookup.sql +3 -0
- package/db/migrations/090-model-run-native-inputs.sql +29 -0
- package/db/migrations/091-native-turn-results.sql +11 -0
- package/db/migrations/092-restore-decision-fts.sql +34 -0
- package/db/migrations/093-native-turn-primary-kind.sql +14 -0
- package/db/migrations/094-commitment-run-provenance.sql +9 -0
- package/db/migrations/095-model-run-native-input-identity.sql +39 -0
- package/db/migrations/096-drop-connector-event-index-fts.sql +9 -0
- package/db/migrations/097-native-input-json-validity.sql +37 -0
- package/db/migrations/098-workflow-memory-kind.sql +105 -0
- package/db/migrations/099-commitment-revision-graph.sql +2 -0
- package/dist/action-contracts.d.ts +212 -0
- package/dist/action-contracts.js +10 -0
- package/dist/api/catalog.d.ts +118 -0
- package/dist/api/catalog.js +1756 -0
- package/dist/api/dispatch.d.ts +76 -0
- package/dist/api/dispatch.js +372 -0
- package/dist/canonicalize.d.ts +2 -2
- package/dist/canonicalize.js +3 -3
- package/dist/client/client.d.ts +33 -0
- package/dist/client/client.js +123 -0
- package/dist/client/ipc.d.ts +81 -0
- package/dist/client/ipc.js +314 -0
- package/dist/db-adapter/base-adapter.d.ts +6 -66
- package/dist/db-adapter/base-adapter.js +6 -6
- package/dist/db-adapter/index.d.ts +5 -5
- package/dist/db-adapter/index.js +2 -5
- package/dist/db-adapter/node-sqlite-adapter.d.ts +85 -15
- package/dist/db-adapter/node-sqlite-adapter.js +1985 -570
- package/dist/db-manager.d.ts +53 -97
- package/dist/db-manager.js +121 -603
- package/dist/embedding/embedder.d.ts +132 -0
- package/dist/embedding/embedder.js +299 -0
- package/dist/identity/principal-repository.d.ts +2 -2
- package/dist/index.d.ts +43 -58
- package/dist/index.js +97 -145
- package/dist/{context-compile/channel-grant.d.ts → knowledge/access.d.ts} +12 -17
- package/dist/knowledge/access.js +663 -0
- package/dist/knowledge/case-errors.d.ts +31 -0
- package/dist/knowledge/case-errors.js +40 -0
- package/dist/{cases/search-rollup.d.ts → knowledge/case-search-rollup.d.ts} +5 -4
- package/dist/{cases/search-rollup.js → knowledge/case-search-rollup.js} +6 -8
- package/dist/{cases/store.d.ts → knowledge/case-store.d.ts} +3 -6
- package/dist/{cases/store.js → knowledge/case-store.js} +7 -42
- package/dist/{cases/timeline-range.d.ts → knowledge/case-timeline-range.d.ts} +3 -3
- package/dist/{cases/timeline-range.js → knowledge/case-timeline-range.js} +47 -25
- package/dist/{cases/types.d.ts → knowledge/case-types.d.ts} +8 -2
- package/dist/{cases/types.js → knowledge/case-types.js} +1 -1
- package/dist/knowledge/commitment-revision-migration.d.ts +4 -0
- package/dist/knowledge/commitment-revision-migration.js +50 -0
- package/dist/knowledge/commitments.d.ts +192 -0
- package/dist/knowledge/commitments.js +386 -0
- package/dist/knowledge/decision-edges.d.ts +70 -0
- package/dist/knowledge/decision-edges.js +123 -0
- package/dist/{agent-graph/types.d.ts → knowledge/graph-query.d.ts} +80 -70
- package/dist/knowledge/graph-query.js +1585 -0
- package/dist/knowledge/identity.d.ts +16 -0
- package/dist/knowledge/identity.js +22 -0
- package/dist/knowledge/index.d.ts +47 -0
- package/dist/knowledge/index.js +90 -0
- package/dist/knowledge/judgment-edge.d.ts +6 -0
- package/dist/knowledge/judgment-edge.js +22 -0
- package/dist/knowledge/judgments.d.ts +118 -0
- package/dist/knowledge/judgments.js +802 -0
- package/dist/knowledge/observations.d.ts +160 -0
- package/dist/knowledge/observations.js +449 -0
- package/dist/knowledge/question-type.d.ts +34 -0
- package/dist/knowledge/question-type.js +70 -0
- package/dist/{search → knowledge}/ranker-features.js +1 -1
- package/dist/{search → knowledge}/search-quality.d.ts +0 -1
- package/dist/knowledge/search.d.ts +38 -0
- package/dist/knowledge/search.js +91 -0
- package/dist/knowledge/source-ingest.d.ts +65 -0
- package/dist/knowledge/source-ingest.js +174 -0
- package/dist/{edges/types.d.ts → knowledge/twin-edge-types.d.ts} +31 -4
- package/dist/{edges/types.js → knowledge/twin-edge-types.js} +13 -2
- package/dist/knowledge/work-dates.d.ts +49 -0
- package/dist/knowledge/work-dates.js +120 -0
- package/dist/mama-api.d.ts +114 -778
- package/dist/mama-api.js +313 -2461
- package/dist/memory/api.d.ts +264 -24
- package/dist/memory/api.js +1633 -843
- package/dist/memory/bootstrap-builder.d.ts +2 -1
- package/dist/memory/bootstrap-builder.js +5 -5
- package/dist/memory/channel-summary-state-store.d.ts +3 -2
- package/dist/memory/channel-summary-state-store.js +53 -51
- package/dist/memory/channel-summary-store.d.ts +5 -3
- package/dist/memory/channel-summary-store.js +3 -8
- package/dist/memory/dashboard-read.d.ts +46 -0
- package/dist/memory/dashboard-read.js +94 -0
- package/dist/memory/event-store.d.ts +3 -3
- package/dist/memory/event-store.js +3 -10
- package/dist/memory/finding-store.d.ts +3 -2
- package/dist/memory/finding-store.js +2 -7
- package/dist/memory/graph-read.d.ts +52 -0
- package/dist/memory/graph-read.js +130 -0
- package/dist/memory/judgment-types.d.ts +394 -0
- package/dist/memory/judgment-types.js +16 -0
- package/dist/memory/provenance-audit.d.ts +5 -2
- package/dist/memory/provenance-audit.js +8 -14
- package/dist/memory/provenance-live.d.ts +141 -0
- package/dist/memory/provenance-live.js +409 -0
- package/dist/memory/provenance-query.d.ts +5 -4
- package/dist/memory/provenance-query.js +21 -29
- package/dist/memory/provenance-resolver.d.ts +191 -0
- package/dist/memory/provenance-resolver.js +155 -0
- package/dist/memory/provenance.d.ts +1 -11
- package/dist/memory/provenance.js +2 -33
- package/dist/memory/recall-sanitize.d.ts +55 -0
- package/dist/memory/recall-sanitize.js +144 -0
- package/dist/memory/secret-filter.d.ts +32 -0
- package/dist/memory/secret-filter.js +126 -0
- package/dist/memory/truth-store.d.ts +2 -1
- package/dist/memory/truth-store.js +1 -4
- package/dist/memory/types.d.ts +74 -24
- package/dist/memory/types.js +75 -5
- package/dist/memory/write-adapters.d.ts +98 -0
- package/dist/memory/write-adapters.js +242 -0
- package/dist/operations/owner-action-effects.d.ts +53 -0
- package/dist/operations/owner-action-effects.js +96 -0
- package/dist/provenance/source-ref.d.ts +1 -1
- package/dist/provenance/source-ref.js +0 -2
- package/dist/registry/corrections.d.ts +31 -0
- package/dist/registry/corrections.js +332 -0
- package/dist/registry/record-identity.d.ts +49 -0
- package/dist/registry/record-identity.js +133 -0
- package/dist/registry/store.d.ts +101 -0
- package/dist/registry/store.js +395 -0
- package/dist/registry/types.d.ts +28 -0
- package/dist/registry/types.js +5 -0
- package/dist/relevance-scorer.js +2 -2
- package/dist/runtime/agent-event-bus.d.ts +50 -0
- package/dist/runtime/agent-event-bus.js +101 -0
- package/dist/runtime/drivers/claude-cli-wrapper.d.ts +152 -0
- package/dist/runtime/drivers/claude-cli-wrapper.js +375 -0
- package/dist/runtime/drivers/claude-effort.d.ts +4 -0
- package/dist/runtime/drivers/claude-effort.js +16 -0
- package/dist/runtime/drivers/cli-arg-redaction.d.ts +2 -0
- package/dist/runtime/drivers/cli-arg-redaction.js +18 -0
- package/dist/runtime/drivers/cli-secret-redaction.d.ts +17 -0
- package/dist/runtime/drivers/cli-secret-redaction.js +126 -0
- package/dist/runtime/drivers/codex-app-server-process.d.ts +277 -0
- package/dist/runtime/drivers/codex-app-server-process.js +2342 -0
- package/dist/runtime/drivers/codex-auxiliary-tools.d.ts +17 -0
- package/dist/runtime/drivers/codex-auxiliary-tools.js +267 -0
- package/dist/runtime/drivers/codex-home.d.ts +20 -0
- package/dist/runtime/drivers/codex-home.js +674 -0
- package/dist/runtime/drivers/codex-thread-registry.d.ts +63 -0
- package/dist/runtime/drivers/codex-thread-registry.js +433 -0
- package/dist/runtime/drivers/persistent-cli-adapter.d.ts +160 -0
- package/dist/runtime/drivers/persistent-cli-adapter.js +396 -0
- package/dist/runtime/drivers/persistent-cli-process.d.ts +425 -0
- package/dist/runtime/drivers/persistent-cli-process.js +1529 -0
- package/dist/runtime/drivers/types.d.ts +456 -0
- package/dist/runtime/drivers/types.js +110 -0
- package/dist/runtime/mailbox.d.ts +223 -0
- package/dist/runtime/mailbox.js +582 -0
- package/dist/runtime/model-run-store.d.ts +28 -0
- package/dist/{model-runs/store.js → runtime/model-run-store.js} +81 -41
- package/dist/{model-runs/types.d.ts → runtime/model-run-types.d.ts} +33 -4
- package/dist/{model-runs/types.js → runtime/model-run-types.js} +1 -1
- package/dist/runtime/native-effect-observer.d.ts +22 -0
- package/dist/runtime/native-effect-observer.js +103 -0
- package/dist/runtime/native-input-journal.d.ts +46 -0
- package/dist/runtime/native-input-journal.js +308 -0
- package/dist/runtime/native-prompt.d.ts +118 -0
- package/dist/runtime/native-prompt.js +496 -0
- package/dist/runtime/native-session.d.ts +10 -0
- package/dist/{agent-graph/index.js → runtime/native-session.js} +9 -6
- package/dist/runtime/native-tool-trace-observer.d.ts +5 -0
- package/dist/runtime/native-tool-trace-observer.js +65 -0
- package/dist/runtime/native-turn.d.ts +272 -0
- package/dist/runtime/native-turn.js +1041 -0
- package/dist/runtime/operations.d.ts +236 -0
- package/dist/runtime/operations.js +565 -0
- package/dist/runtime/post-tool-handler.d.ts +74 -0
- package/dist/runtime/post-tool-handler.js +137 -0
- package/dist/runtime/prompt-layers.d.ts +126 -0
- package/dist/runtime/prompt-layers.js +239 -0
- package/dist/runtime/runtime-process.d.ts +102 -0
- package/dist/runtime/runtime-process.js +206 -0
- package/dist/runtime/runtime.d.ts +278 -0
- package/dist/runtime/runtime.js +563 -0
- package/dist/runtime/session-pool.d.ts +176 -0
- package/dist/runtime/session-pool.js +369 -0
- package/dist/runtime/stimulus-payload.d.ts +5 -0
- package/dist/runtime/stimulus-payload.js +50 -0
- package/dist/runtime/subagent-bridge.d.ts +24 -0
- package/dist/{agent-graph/types.js → runtime/subagent-bridge.js} +1 -1
- package/dist/runtime/text-completion.d.ts +22 -0
- package/dist/{agent-situation/types.js → runtime/text-completion.js} +1 -1
- package/dist/runtime/token-estimator.d.ts +25 -0
- package/dist/runtime/token-estimator.js +75 -0
- package/dist/runtime/tool-trace-store.d.ts +22 -0
- package/dist/{model-runs → runtime}/tool-trace-store.js +46 -27
- package/dist/runtime/trace-summary.d.ts +5 -0
- package/dist/runtime/trace-summary.js +74 -0
- package/dist/runtime/turn-text.d.ts +23 -0
- package/dist/runtime/turn-text.js +41 -0
- package/dist/storage/database.d.ts +67 -0
- package/dist/storage/database.js +158 -0
- package/package.json +20 -34
- package/dist/agent-graph/alias-write.d.ts +0 -3
- package/dist/agent-graph/alias-write.js +0 -234
- package/dist/agent-graph/entity-resolve.d.ts +0 -3
- package/dist/agent-graph/entity-resolve.js +0 -271
- package/dist/agent-graph/errors.d.ts +0 -4
- package/dist/agent-graph/errors.js +0 -11
- package/dist/agent-graph/graph-query.d.ts +0 -5
- package/dist/agent-graph/graph-query.js +0 -360
- package/dist/agent-graph/index.d.ts +0 -6
- package/dist/agent-situation/builder.d.ts +0 -8
- package/dist/agent-situation/builder.js +0 -239
- package/dist/agent-situation/cache-key.d.ts +0 -4
- package/dist/agent-situation/cache-key.js +0 -64
- package/dist/agent-situation/index.d.ts +0 -7
- package/dist/agent-situation/index.js +0 -28
- package/dist/agent-situation/packet-store.d.ts +0 -27
- package/dist/agent-situation/packet-store.js +0 -228
- package/dist/agent-situation/ranking-policy.d.ts +0 -18
- package/dist/agent-situation/ranking-policy.js +0 -75
- package/dist/agent-situation/source-readers.d.ts +0 -52
- package/dist/agent-situation/source-readers.js +0 -300
- package/dist/agent-situation/types.d.ts +0 -195
- package/dist/cases/wiki-page-index.d.ts +0 -37
- package/dist/cases/wiki-page-index.js +0 -201
- package/dist/config-loader.d.ts +0 -68
- package/dist/config-loader.js +0 -239
- package/dist/connectors/event-index.d.ts +0 -24
- package/dist/connectors/event-index.js +0 -245
- package/dist/connectors/raw-query.d.ts +0 -40
- package/dist/connectors/raw-query.js +0 -412
- package/dist/connectors/types.d.ts +0 -117
- package/dist/connectors/types.js +0 -3
- package/dist/context-compile/boundary-defaults.d.ts +0 -12
- package/dist/context-compile/boundary-defaults.js +0 -105
- package/dist/context-compile/channel-grant.js +0 -66
- package/dist/context-compile/compiler-policy.d.ts +0 -39
- package/dist/context-compile/compiler-policy.js +0 -147
- package/dist/context-compile/compiler.d.ts +0 -18
- package/dist/context-compile/compiler.js +0 -404
- package/dist/context-compile/index.d.ts +0 -10
- package/dist/context-compile/index.js +0 -26
- package/dist/context-compile/packet-store.d.ts +0 -8
- package/dist/context-compile/packet-store.js +0 -239
- package/dist/context-compile/ref.d.ts +0 -7
- package/dist/context-compile/ref.js +0 -102
- package/dist/context-compile/source-readers.d.ts +0 -70
- package/dist/context-compile/source-readers.js +0 -840
- package/dist/context-compile/types.d.ts +0 -155
- package/dist/context-compile/types.js +0 -11
- package/dist/context-compile/visibility.d.ts +0 -21
- package/dist/context-compile/visibility.js +0 -224
- package/dist/decision-tracker.d.ts +0 -206
- package/dist/decision-tracker.js +0 -481
- package/dist/edges/ref-validation.d.ts +0 -7
- package/dist/edges/ref-validation.js +0 -321
- package/dist/edges/store.d.ts +0 -8
- package/dist/edges/store.js +0 -129
- package/dist/embedding-client.d.ts +0 -28
- package/dist/embedding-client.js +0 -53
- package/dist/embedding-server/index.d.ts +0 -67
- package/dist/embedding-server/index.js +0 -405
- package/dist/embedding-server/mobile/auth.d.ts +0 -44
- package/dist/embedding-server/mobile/auth.js +0 -150
- package/dist/embedding-server/mobile/daemon.d.ts +0 -129
- package/dist/embedding-server/mobile/daemon.js +0 -313
- package/dist/embedding-server/mobile/output-parser.d.ts +0 -115
- package/dist/embedding-server/mobile/output-parser.js +0 -241
- package/dist/embedding-server/mobile/session-api.d.ts +0 -57
- package/dist/embedding-server/mobile/session-api.js +0 -261
- package/dist/embedding-server/mobile/session-manager.d.ts +0 -138
- package/dist/embedding-server/mobile/session-manager.js +0 -378
- package/dist/embedding-server/mobile/websocket-handler.d.ts +0 -137
- package/dist/embedding-server/mobile/websocket-handler.js +0 -486
- package/dist/embeddings.d.ts +0 -68
- package/dist/embeddings.js +0 -244
- package/dist/entities/audit-metrics.d.ts +0 -68
- package/dist/entities/audit-metrics.js +0 -124
- package/dist/entities/candidate-generator.d.ts +0 -8
- package/dist/entities/candidate-generator.js +0 -24
- package/dist/entities/entity-linked-decision-counts.d.ts +0 -2
- package/dist/entities/entity-linked-decision-counts.js +0 -29
- package/dist/entities/entity-list.d.ts +0 -22
- package/dist/entities/entity-list.js +0 -3
- package/dist/entities/entity-orphan-list.d.ts +0 -17
- package/dist/entities/entity-orphan-list.js +0 -3
- package/dist/entities/entity-search.d.ts +0 -20
- package/dist/entities/entity-search.js +0 -3
- package/dist/entities/errors.d.ts +0 -55
- package/dist/entities/errors.js +0 -100
- package/dist/entities/lineage-store.d.ts +0 -28
- package/dist/entities/lineage-store.js +0 -204
- package/dist/entities/normalization.d.ts +0 -14
- package/dist/entities/normalization.js +0 -63
- package/dist/entities/policy-store.d.ts +0 -8
- package/dist/entities/policy-store.js +0 -75
- package/dist/entities/policy-types.d.ts +0 -72
- package/dist/entities/policy-types.js +0 -59
- package/dist/entities/projection.d.ts +0 -8
- package/dist/entities/projection.js +0 -82
- package/dist/entities/provenance-query.d.ts +0 -32
- package/dist/entities/provenance-query.js +0 -3
- package/dist/entities/read-identity.d.ts +0 -31
- package/dist/entities/read-identity.js +0 -264
- package/dist/entities/recall-bridge.d.ts +0 -5
- package/dist/entities/recall-bridge.js +0 -171
- package/dist/entities/resolution-engine.d.ts +0 -8
- package/dist/entities/resolution-engine.js +0 -3
- package/dist/entities/rollback-preview.d.ts +0 -35
- package/dist/entities/rollback-preview.js +0 -3
- package/dist/entities/source-locator.d.ts +0 -3
- package/dist/entities/source-locator.js +0 -32
- package/dist/entities/store.d.ts +0 -69
- package/dist/entities/store.js +0 -544
- package/dist/entities/types.d.ts +0 -166
- package/dist/entities/types.js +0 -30
- package/dist/memory/extraction-prompt.d.ts +0 -4
- package/dist/memory/extraction-prompt.js +0 -89
- package/dist/memory/scope-backfill.d.ts +0 -14
- package/dist/memory/scope-backfill.js +0 -3
- package/dist/memory/scope-store.d.ts +0 -4
- package/dist/memory/scope-store.js +0 -9
- package/dist/memory-store.d.ts +0 -87
- package/dist/memory-store.js +0 -78
- package/dist/model-runs/store.d.ts +0 -13
- package/dist/model-runs/tool-trace-store.d.ts +0 -8
- package/dist/notification-manager.d.ts +0 -7
- package/dist/notification-manager.js +0 -12
- package/dist/outcome-tracker.d.ts +0 -151
- package/dist/outcome-tracker.js +0 -271
- package/dist/query-intent.d.ts +0 -23
- package/dist/query-intent.js +0 -114
- package/dist/runtime/trusted-provenance.d.ts +0 -3
- package/dist/runtime/trusted-provenance.js +0 -6
- package/dist/search/question-type.d.ts +0 -5
- package/dist/search/question-type.js +0 -41
- package/dist/test-utils.d.ts +0 -80
- package/dist/test-utils.js +0 -218
- package/dist/tier-validator.d.ts +0 -43
- package/dist/tier-validator.js +0 -159
- package/dist/time-formatter.d.ts +0 -25
- package/dist/time-formatter.js +0 -93
- /package/dist/{search → knowledge}/feedback-store.d.ts +0 -0
- /package/dist/{search → knowledge}/feedback-store.js +0 -0
- /package/dist/{search → knowledge}/ranker-features.d.ts +0 -0
- /package/dist/{search → knowledge}/ranker-rescore.d.ts +0 -0
- /package/dist/{search → knowledge}/ranker-rescore.js +0 -0
- /package/dist/{search → knowledge}/ranker-trainer.d.ts +0 -0
- /package/dist/{search → knowledge}/ranker-trainer.js +0 -0
- /package/dist/{search → knowledge}/search-quality.js +0 -0
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mapConnectorEventIndexRecord = mapConnectorEventIndexRecord;
|
|
4
|
-
exports.connectorEventIndexId = connectorEventIndexId;
|
|
5
|
-
exports.connectorEventContentHash = connectorEventContentHash;
|
|
6
|
-
exports.upsertConnectorEventIndex = upsertConnectorEventIndex;
|
|
7
|
-
exports.getConnectorEventIndexRecord = getConnectorEventIndexRecord;
|
|
8
|
-
exports.readConnectorCursor = readConnectorCursor;
|
|
9
|
-
exports.deleteExpiredConnectorEvents = deleteExpiredConnectorEvents;
|
|
10
|
-
const node_crypto_1 = require("node:crypto");
|
|
11
|
-
const canonicalize_js_1 = require("../canonicalize.js");
|
|
12
|
-
function nowIso() {
|
|
13
|
-
return new Date().toISOString();
|
|
14
|
-
}
|
|
15
|
-
function eventDateFromMs(ms) {
|
|
16
|
-
if (ms === null || !Number.isFinite(ms)) {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
return new Date(ms).toISOString().slice(0, 10);
|
|
20
|
-
}
|
|
21
|
-
function normalizeTimestampMs(input) {
|
|
22
|
-
const timestamp = input.source_timestamp_ms ?? input.event_datetime;
|
|
23
|
-
if (typeof timestamp !== 'number' || !Number.isFinite(timestamp)) {
|
|
24
|
-
throw new Error('connector_event_index.source_timestamp_ms must be a finite number.');
|
|
25
|
-
}
|
|
26
|
-
return Math.floor(timestamp);
|
|
27
|
-
}
|
|
28
|
-
function normalizeEventDatetime(input) {
|
|
29
|
-
if (input.event_datetime === null || input.event_datetime === undefined) {
|
|
30
|
-
return normalizeTimestampMs(input);
|
|
31
|
-
}
|
|
32
|
-
if (!Number.isFinite(input.event_datetime)) {
|
|
33
|
-
throw new Error('connector_event_index.event_datetime must be a finite number when provided.');
|
|
34
|
-
}
|
|
35
|
-
return Math.floor(input.event_datetime);
|
|
36
|
-
}
|
|
37
|
-
function normalizeMetadataJson(input) {
|
|
38
|
-
if (input.metadata_json !== undefined) {
|
|
39
|
-
return input.metadata_json;
|
|
40
|
-
}
|
|
41
|
-
if (input.metadata === undefined || input.metadata === null) {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
return (0, canonicalize_js_1.canonicalizeJSON)(input.metadata);
|
|
45
|
-
}
|
|
46
|
-
function toBuffer(value) {
|
|
47
|
-
if (Buffer.isBuffer(value)) {
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
50
|
-
if (value instanceof Uint8Array) {
|
|
51
|
-
return Buffer.from(value);
|
|
52
|
-
}
|
|
53
|
-
throw new Error('connector_event_index.content_hash must be a 32-byte Buffer.');
|
|
54
|
-
}
|
|
55
|
-
function mapConnectorEventIndexRow(row) {
|
|
56
|
-
return {
|
|
57
|
-
event_index_id: String(row.event_index_id),
|
|
58
|
-
source_connector: String(row.source_connector),
|
|
59
|
-
source_type: String(row.source_type),
|
|
60
|
-
source_id: String(row.source_id),
|
|
61
|
-
source_locator: row.source_locator === null ? null : String(row.source_locator),
|
|
62
|
-
channel: row.channel === null ? null : String(row.channel),
|
|
63
|
-
author: row.author === null ? null : String(row.author),
|
|
64
|
-
title: row.title === null ? null : String(row.title),
|
|
65
|
-
content: String(row.content),
|
|
66
|
-
event_datetime: typeof row.event_datetime === 'number' && Number.isFinite(row.event_datetime)
|
|
67
|
-
? row.event_datetime
|
|
68
|
-
: null,
|
|
69
|
-
event_date: row.event_date === null ? null : String(row.event_date),
|
|
70
|
-
source_timestamp_ms: Number(row.source_timestamp_ms),
|
|
71
|
-
source_cursor: row.source_cursor === null ? null : String(row.source_cursor),
|
|
72
|
-
tenant_id: row.tenant_id === null ? null : String(row.tenant_id),
|
|
73
|
-
project_id: row.project_id === null ? null : String(row.project_id),
|
|
74
|
-
memory_scope_kind: row.memory_scope_kind === null ? null : String(row.memory_scope_kind),
|
|
75
|
-
memory_scope_id: row.memory_scope_id === null ? null : String(row.memory_scope_id),
|
|
76
|
-
metadata_json: row.metadata_json === null ? null : String(row.metadata_json),
|
|
77
|
-
artifact_locator: row.artifact_locator === null ? null : String(row.artifact_locator),
|
|
78
|
-
artifact_title: row.artifact_title === null ? null : String(row.artifact_title),
|
|
79
|
-
content_hash: toBuffer(row.content_hash),
|
|
80
|
-
operator_ingest_seq: Number(row.operator_ingest_seq),
|
|
81
|
-
operator_observation_seq: Number(row.operator_observation_seq),
|
|
82
|
-
indexed_at: String(row.indexed_at),
|
|
83
|
-
updated_at: String(row.updated_at),
|
|
84
|
-
expires_at: row.expires_at === null ? null : String(row.expires_at),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
function mapConnectorEventIndexRecord(row) {
|
|
88
|
-
return mapConnectorEventIndexRow(row);
|
|
89
|
-
}
|
|
90
|
-
function mapConnectorCursorRow(row) {
|
|
91
|
-
return {
|
|
92
|
-
connector_name: String(row.connector_name),
|
|
93
|
-
last_seen_timestamp_ms: Number(row.last_seen_timestamp_ms),
|
|
94
|
-
last_seen_source_id: String(row.last_seen_source_id),
|
|
95
|
-
last_sweep_at: row.last_sweep_at === null ? null : String(row.last_sweep_at),
|
|
96
|
-
last_success_at: row.last_success_at === null ? null : String(row.last_success_at),
|
|
97
|
-
last_error: row.last_error === null ? null : String(row.last_error),
|
|
98
|
-
last_error_at: row.last_error_at === null ? null : String(row.last_error_at),
|
|
99
|
-
indexed_count: Number(row.indexed_count),
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
function connectorEventIndexId(sourceConnector, sourceId) {
|
|
103
|
-
const digest = (0, node_crypto_1.createHash)('sha1').update(`${sourceConnector}\0${sourceId}`, 'utf8').digest('hex');
|
|
104
|
-
return `evt_${digest.slice(0, 16)}`;
|
|
105
|
-
}
|
|
106
|
-
function connectorEventContentHash(input) {
|
|
107
|
-
return (0, node_crypto_1.createHash)('sha256')
|
|
108
|
-
.update((0, canonicalize_js_1.canonicalizeJSON)({
|
|
109
|
-
source_connector: input.source_connector,
|
|
110
|
-
source_id: input.source_id,
|
|
111
|
-
content: input.content,
|
|
112
|
-
event_datetime: input.event_datetime ?? null,
|
|
113
|
-
}), 'utf8')
|
|
114
|
-
.digest();
|
|
115
|
-
}
|
|
116
|
-
function upsertConnectorEventIndex(adapter, input) {
|
|
117
|
-
const sourceTimestampMs = normalizeTimestampMs(input);
|
|
118
|
-
const eventDatetime = normalizeEventDatetime(input);
|
|
119
|
-
const eventDate = input.event_date ?? eventDateFromMs(eventDatetime);
|
|
120
|
-
const timestamp = input.updated_at ?? nowIso();
|
|
121
|
-
const eventIndexId = connectorEventIndexId(input.source_connector, input.source_id);
|
|
122
|
-
const contentHash = input.content_hash
|
|
123
|
-
? Buffer.from(input.content_hash)
|
|
124
|
-
: connectorEventContentHash({
|
|
125
|
-
source_connector: input.source_connector,
|
|
126
|
-
source_id: input.source_id,
|
|
127
|
-
content: input.content,
|
|
128
|
-
event_datetime: eventDatetime,
|
|
129
|
-
});
|
|
130
|
-
if (contentHash.byteLength !== 32) {
|
|
131
|
-
throw new Error('connector_event_index.content_hash must be exactly 32 bytes.');
|
|
132
|
-
}
|
|
133
|
-
const metadataJson = normalizeMetadataJson(input);
|
|
134
|
-
return adapter.transaction(() => {
|
|
135
|
-
adapter
|
|
136
|
-
.prepare(`
|
|
137
|
-
INSERT INTO connector_event_index (
|
|
138
|
-
event_index_id, source_connector, source_type, source_id, source_entity_id,
|
|
139
|
-
source_locator, channel, author, title, content, event_datetime, event_date,
|
|
140
|
-
source_timestamp_ms, source_cursor, tenant_id, project_id, memory_scope_kind,
|
|
141
|
-
memory_scope_id, metadata_json, artifact_locator, artifact_title, content_hash,
|
|
142
|
-
indexed_at, updated_at, expires_at
|
|
143
|
-
)
|
|
144
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
145
|
-
ON CONFLICT(source_connector, source_id) DO UPDATE SET
|
|
146
|
-
source_type = excluded.source_type,
|
|
147
|
-
source_entity_id = excluded.source_entity_id,
|
|
148
|
-
source_locator = excluded.source_locator,
|
|
149
|
-
channel = excluded.channel,
|
|
150
|
-
author = excluded.author,
|
|
151
|
-
title = excluded.title,
|
|
152
|
-
content = excluded.content,
|
|
153
|
-
event_datetime = excluded.event_datetime,
|
|
154
|
-
event_date = excluded.event_date,
|
|
155
|
-
source_timestamp_ms = excluded.source_timestamp_ms,
|
|
156
|
-
source_cursor = excluded.source_cursor,
|
|
157
|
-
tenant_id = excluded.tenant_id,
|
|
158
|
-
project_id = excluded.project_id,
|
|
159
|
-
memory_scope_kind = excluded.memory_scope_kind,
|
|
160
|
-
memory_scope_id = excluded.memory_scope_id,
|
|
161
|
-
metadata_json = excluded.metadata_json,
|
|
162
|
-
artifact_locator = excluded.artifact_locator,
|
|
163
|
-
artifact_title = excluded.artifact_title,
|
|
164
|
-
content_hash = excluded.content_hash,
|
|
165
|
-
updated_at = excluded.updated_at,
|
|
166
|
-
expires_at = excluded.expires_at,
|
|
167
|
-
operator_ingest_seq = CASE
|
|
168
|
-
WHEN connector_event_index.content_hash IS NOT excluded.content_hash
|
|
169
|
-
OR connector_event_index.metadata_json IS NOT excluded.metadata_json
|
|
170
|
-
OR connector_event_index.source_timestamp_ms IS NOT excluded.source_timestamp_ms
|
|
171
|
-
OR connector_event_index.source_type IS NOT excluded.source_type
|
|
172
|
-
OR connector_event_index.channel IS NOT excluded.channel
|
|
173
|
-
THEN NULL
|
|
174
|
-
ELSE connector_event_index.operator_ingest_seq
|
|
175
|
-
END,
|
|
176
|
-
operator_observation_seq = CASE
|
|
177
|
-
WHEN connector_event_index.content_hash IS NOT excluded.content_hash
|
|
178
|
-
OR connector_event_index.metadata_json IS NOT excluded.metadata_json
|
|
179
|
-
OR connector_event_index.source_timestamp_ms IS NOT excluded.source_timestamp_ms
|
|
180
|
-
OR connector_event_index.source_type IS NOT excluded.source_type
|
|
181
|
-
OR connector_event_index.channel IS NOT excluded.channel
|
|
182
|
-
THEN NULL
|
|
183
|
-
ELSE connector_event_index.operator_observation_seq
|
|
184
|
-
END
|
|
185
|
-
`)
|
|
186
|
-
.run(eventIndexId, input.source_connector, input.source_type, input.source_id, input.source_entity_id ?? input.source_id, input.source_locator ?? null, input.channel ?? null, input.author ?? null, input.title ?? null, input.content, eventDatetime, eventDate, sourceTimestampMs, input.source_cursor ?? null, input.tenant_id ?? null, input.project_id ?? null, input.memory_scope_kind ?? null, input.memory_scope_id ?? null, metadataJson, input.artifact_locator ?? null, input.artifact_title ?? null, contentHash, input.indexed_at ?? timestamp, timestamp, input.expires_at ?? null);
|
|
187
|
-
const saved = getConnectorEventIndexRecord(adapter, input.source_connector, input.source_id);
|
|
188
|
-
if (!saved) {
|
|
189
|
-
throw new Error(`Failed to read connector_event_index row after upsert: ${input.source_connector}/${input.source_id}`);
|
|
190
|
-
}
|
|
191
|
-
return saved;
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
function getConnectorEventIndexRecord(adapter, sourceConnector, sourceId) {
|
|
195
|
-
const row = adapter
|
|
196
|
-
.prepare(`
|
|
197
|
-
SELECT *
|
|
198
|
-
FROM connector_event_index
|
|
199
|
-
WHERE source_connector = ?
|
|
200
|
-
AND source_id = ?
|
|
201
|
-
LIMIT 1
|
|
202
|
-
`)
|
|
203
|
-
.get(sourceConnector, sourceId);
|
|
204
|
-
return row ? mapConnectorEventIndexRow(row) : null;
|
|
205
|
-
}
|
|
206
|
-
function readConnectorCursor(adapter, connectorName) {
|
|
207
|
-
const row = adapter
|
|
208
|
-
.prepare(`
|
|
209
|
-
SELECT *
|
|
210
|
-
FROM connector_event_index_cursors
|
|
211
|
-
WHERE connector_name = ?
|
|
212
|
-
LIMIT 1
|
|
213
|
-
`)
|
|
214
|
-
.get(connectorName);
|
|
215
|
-
return row ? mapConnectorCursorRow(row) : null;
|
|
216
|
-
}
|
|
217
|
-
function deleteExpiredConnectorEvents(adapter, input) {
|
|
218
|
-
if (!Number.isFinite(input.nowMs)) {
|
|
219
|
-
throw new Error('deleteExpiredConnectorEvents.nowMs must be a finite number.');
|
|
220
|
-
}
|
|
221
|
-
if (!Number.isFinite(input.retentionMs) || input.retentionMs < 0) {
|
|
222
|
-
throw new Error('deleteExpiredConnectorEvents.retentionMs must be a non-negative finite number.');
|
|
223
|
-
}
|
|
224
|
-
const cutoffMs = input.nowMs - input.retentionMs;
|
|
225
|
-
const result = input.connectorName
|
|
226
|
-
? adapter
|
|
227
|
-
.prepare(`
|
|
228
|
-
DELETE FROM connector_event_index
|
|
229
|
-
WHERE source_connector = ?
|
|
230
|
-
AND event_datetime IS NOT NULL
|
|
231
|
-
AND event_datetime < ?
|
|
232
|
-
AND artifact_locator IS NULL
|
|
233
|
-
`)
|
|
234
|
-
.run(input.connectorName, cutoffMs)
|
|
235
|
-
: adapter
|
|
236
|
-
.prepare(`
|
|
237
|
-
DELETE FROM connector_event_index
|
|
238
|
-
WHERE event_datetime IS NOT NULL
|
|
239
|
-
AND event_datetime < ?
|
|
240
|
-
AND artifact_locator IS NULL
|
|
241
|
-
`)
|
|
242
|
-
.run(cutoffMs);
|
|
243
|
-
return { rows_deleted: result.changes };
|
|
244
|
-
}
|
|
245
|
-
//# sourceMappingURL=event-index.js.map
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { DatabaseAdapter } from '../db-manager.js';
|
|
2
|
-
import type { RawSearchHit, RawDocument, RawSearchInput, RawSearchResult, RawSearchScopeFilter } from './types.js';
|
|
3
|
-
type RawQueryAdapter = Pick<DatabaseAdapter, 'prepare'>;
|
|
4
|
-
interface RawWindowInput {
|
|
5
|
-
connectors?: string[];
|
|
6
|
-
scopes?: RawSearchScopeFilter[];
|
|
7
|
-
before?: number;
|
|
8
|
-
after?: number;
|
|
9
|
-
}
|
|
10
|
-
export declare function searchRaw(adapter: RawQueryAdapter, input: RawSearchInput): RawSearchResult;
|
|
11
|
-
export declare function searchAllRaw(adapter: RawQueryAdapter, input: RawSearchInput): RawSearchResult;
|
|
12
|
-
export declare function getRawById(adapter: RawQueryAdapter, rawId: string, visibility: Pick<RawSearchInput, 'connectors' | 'scopes'>): RawDocument | null;
|
|
13
|
-
export declare function getRawWindow(adapter: RawQueryAdapter, rawId: string, input: RawWindowInput): {
|
|
14
|
-
target: RawSearchHit;
|
|
15
|
-
items: RawSearchHit[];
|
|
16
|
-
} | null;
|
|
17
|
-
export interface RawHistoryInput {
|
|
18
|
-
/** The entity whose revisions to read. Provide this OR `rawId`. */
|
|
19
|
-
entityId?: string;
|
|
20
|
-
/**
|
|
21
|
-
* A raw event id (event_index_id) to anchor on: its entity is resolved under the SAME
|
|
22
|
-
* visibility, then that entity's revisions are returned. An anchor the caller may not see
|
|
23
|
-
* resolves to nothing, so a rawId cannot widen what the caller can read.
|
|
24
|
-
*/
|
|
25
|
-
rawId?: string;
|
|
26
|
-
connectors?: string[];
|
|
27
|
-
scopes?: RawSearchScopeFilter[];
|
|
28
|
-
fromMs?: number;
|
|
29
|
-
toMs?: number;
|
|
30
|
-
limit?: number;
|
|
31
|
-
cursor?: string;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Chronological change history of one upstream entity (rows sharing source_entity_id), bounded by
|
|
35
|
-
* the SAME connector/scope visibility as search - a citation must not out-read reading. Oldest first,
|
|
36
|
-
* cursor-paged. Rows with a NULL source_entity_id (no revision grouping) never match an entity query.
|
|
37
|
-
*/
|
|
38
|
-
export declare function getRawHistory(adapter: RawQueryAdapter, input: RawHistoryInput): RawSearchResult;
|
|
39
|
-
export type { RawSearchHit, RawSearchInput, RawSearchResult, RawSearchScopeFilter, } from './types.js';
|
|
40
|
-
//# sourceMappingURL=raw-query.d.ts.map
|
|
@@ -1,412 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.searchRaw = searchRaw;
|
|
4
|
-
exports.searchAllRaw = searchAllRaw;
|
|
5
|
-
exports.getRawById = getRawById;
|
|
6
|
-
exports.getRawWindow = getRawWindow;
|
|
7
|
-
exports.getRawHistory = getRawHistory;
|
|
8
|
-
const types_js_1 = require("../memory/types.js");
|
|
9
|
-
const event_index_js_1 = require("./event-index.js");
|
|
10
|
-
const DEFAULT_RAW_LIMIT = 25;
|
|
11
|
-
const MAX_RAW_LIMIT = 100;
|
|
12
|
-
const DEFAULT_WINDOW_SIZE = 5;
|
|
13
|
-
const MAX_WINDOW_SIZE = 50;
|
|
14
|
-
const VALID_SCOPE_KINDS = new Set(types_js_1.MEMORY_SCOPE_KINDS);
|
|
15
|
-
function placeholders(values) {
|
|
16
|
-
if (values.length === 0) {
|
|
17
|
-
throw new Error('Cannot build SQL placeholders for an empty list.');
|
|
18
|
-
}
|
|
19
|
-
return values.map(() => '?').join(', ');
|
|
20
|
-
}
|
|
21
|
-
function normalizeLimit(value) {
|
|
22
|
-
if (value === undefined) {
|
|
23
|
-
return DEFAULT_RAW_LIMIT;
|
|
24
|
-
}
|
|
25
|
-
return Math.min(MAX_RAW_LIMIT, Math.max(0, Math.floor(value)));
|
|
26
|
-
}
|
|
27
|
-
function normalizeWindowSize(value) {
|
|
28
|
-
if (value === undefined) {
|
|
29
|
-
return DEFAULT_WINDOW_SIZE;
|
|
30
|
-
}
|
|
31
|
-
return Math.min(MAX_WINDOW_SIZE, Math.max(0, Math.floor(value)));
|
|
32
|
-
}
|
|
33
|
-
function escapeFtsQuery(query) {
|
|
34
|
-
return `"${query.replace(/"/g, '""')}"`;
|
|
35
|
-
}
|
|
36
|
-
function encodeCursor(row) {
|
|
37
|
-
return Buffer.from(JSON.stringify({
|
|
38
|
-
rank: row.rank,
|
|
39
|
-
timestampMs: row.source_timestamp_ms,
|
|
40
|
-
rawId: row.event_index_id,
|
|
41
|
-
}), 'utf8').toString('base64url');
|
|
42
|
-
}
|
|
43
|
-
function decodeCursor(cursor) {
|
|
44
|
-
if (!cursor) {
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
try {
|
|
48
|
-
const parsed = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
|
|
49
|
-
if (typeof parsed.rank !== 'number' ||
|
|
50
|
-
!Number.isFinite(parsed.rank) ||
|
|
51
|
-
typeof parsed.timestampMs !== 'number' ||
|
|
52
|
-
!Number.isFinite(parsed.timestampMs) ||
|
|
53
|
-
typeof parsed.rawId !== 'string' ||
|
|
54
|
-
parsed.rawId.length === 0) {
|
|
55
|
-
throw new Error('invalid shape');
|
|
56
|
-
}
|
|
57
|
-
return {
|
|
58
|
-
rank: parsed.rank,
|
|
59
|
-
timestampMs: parsed.timestampMs,
|
|
60
|
-
rawId: parsed.rawId,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
catch (err) {
|
|
64
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
65
|
-
throw new Error(`Invalid raw search cursor: ${message}`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
function normalizeConnectors(connectors) {
|
|
69
|
-
return [...new Set((connectors ?? []).map((connector) => connector.trim()).filter(Boolean))];
|
|
70
|
-
}
|
|
71
|
-
function normalizeScopes(scopes) {
|
|
72
|
-
return (scopes ?? [])
|
|
73
|
-
.map((scope) => {
|
|
74
|
-
const kind = scope.kind.trim();
|
|
75
|
-
if (!VALID_SCOPE_KINDS.has(kind)) {
|
|
76
|
-
throw new Error(`Invalid raw search scope kind: ${kind}`);
|
|
77
|
-
}
|
|
78
|
-
return { kind: kind, id: scope.id.trim() };
|
|
79
|
-
})
|
|
80
|
-
.filter((scope) => scope.id.length > 0);
|
|
81
|
-
}
|
|
82
|
-
function appendFilters(clauses, params, input, alias = 'e') {
|
|
83
|
-
const connectors = normalizeConnectors(input.connectors);
|
|
84
|
-
if (connectors.length > 0) {
|
|
85
|
-
clauses.push(`${alias}.source_connector IN (${placeholders(connectors)})`);
|
|
86
|
-
params.push(...connectors);
|
|
87
|
-
}
|
|
88
|
-
const scopes = normalizeScopes(input.scopes);
|
|
89
|
-
if (scopes.length > 0) {
|
|
90
|
-
clauses.push(`(${scopes.map(() => `(${alias}.memory_scope_kind = ? AND ${alias}.memory_scope_id = ?)`).join(' OR ')})`);
|
|
91
|
-
for (const scope of scopes) {
|
|
92
|
-
params.push(scope.kind, scope.id);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (input.fromMs !== undefined) {
|
|
96
|
-
clauses.push(`${alias}.source_timestamp_ms >= ?`);
|
|
97
|
-
params.push(input.fromMs);
|
|
98
|
-
}
|
|
99
|
-
if (input.toMs !== undefined) {
|
|
100
|
-
clauses.push(`${alias}.source_timestamp_ms <= ?`);
|
|
101
|
-
params.push(input.toMs);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
function appendCursorFilter(clauses, params, cursor) {
|
|
105
|
-
if (!cursor) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
clauses.push(`(rank > ? OR (rank = ? AND source_timestamp_ms < ?) OR ` +
|
|
109
|
-
`(rank = ? AND source_timestamp_ms = ? AND event_index_id > ?))`);
|
|
110
|
-
params.push(cursor.rank, cursor.rank, cursor.timestampMs, cursor.rank, cursor.timestampMs, cursor.rawId);
|
|
111
|
-
}
|
|
112
|
-
function parseMetadata(metadataJson) {
|
|
113
|
-
if (!metadataJson) {
|
|
114
|
-
return {};
|
|
115
|
-
}
|
|
116
|
-
try {
|
|
117
|
-
const parsed = JSON.parse(metadataJson);
|
|
118
|
-
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
|
119
|
-
? parsed
|
|
120
|
-
: {};
|
|
121
|
-
}
|
|
122
|
-
catch {
|
|
123
|
-
return {};
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
function contentPreview(content) {
|
|
127
|
-
const compact = content.replace(/\s+/g, ' ').trim();
|
|
128
|
-
return compact.length > 240 ? `${compact.slice(0, 237)}...` : compact;
|
|
129
|
-
}
|
|
130
|
-
function scoreFromRank(rank) {
|
|
131
|
-
return Number.isFinite(rank) ? 1 / (1 + Math.exp(rank)) : 0;
|
|
132
|
-
}
|
|
133
|
-
function timestampToIso(timestampMs) {
|
|
134
|
-
if (timestampMs === null || !Number.isFinite(timestampMs)) {
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
|
-
return new Date(timestampMs).toISOString();
|
|
138
|
-
}
|
|
139
|
-
function toRawHit(row) {
|
|
140
|
-
const record = (0, event_index_js_1.mapConnectorEventIndexRecord)(row);
|
|
141
|
-
return {
|
|
142
|
-
raw_id: record.event_index_id,
|
|
143
|
-
connector: record.source_connector,
|
|
144
|
-
source_id: record.source_id,
|
|
145
|
-
channel_id: record.channel,
|
|
146
|
-
author_label: record.author,
|
|
147
|
-
created_at: timestampToIso(record.event_datetime ?? record.source_timestamp_ms),
|
|
148
|
-
content_preview: contentPreview(record.content),
|
|
149
|
-
score: scoreFromRank(Number(row.rank)),
|
|
150
|
-
source_ref: record.source_locator ?? record.artifact_locator,
|
|
151
|
-
metadata: parseMetadata(record.metadata_json),
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
function runSearch(adapter, input) {
|
|
155
|
-
const query = input.query.trim();
|
|
156
|
-
if (query.length === 0) {
|
|
157
|
-
return { hits: [], next_cursor: null };
|
|
158
|
-
}
|
|
159
|
-
const limit = normalizeLimit(input.limit);
|
|
160
|
-
if (limit === 0) {
|
|
161
|
-
return { hits: [], next_cursor: null };
|
|
162
|
-
}
|
|
163
|
-
const params = [escapeFtsQuery(query)];
|
|
164
|
-
const clauses = [];
|
|
165
|
-
appendFilters(clauses, params, input);
|
|
166
|
-
const cursor = decodeCursor(input.cursor);
|
|
167
|
-
const outerClauses = [];
|
|
168
|
-
const outerParams = [];
|
|
169
|
-
appendCursorFilter(outerClauses, outerParams, cursor);
|
|
170
|
-
const whereSql = clauses.length > 0 ? `AND ${clauses.join(' AND ')}` : '';
|
|
171
|
-
const cursorSql = outerClauses.length > 0 ? `WHERE ${outerClauses.join(' AND ')}` : '';
|
|
172
|
-
const rows = adapter
|
|
173
|
-
.prepare(`
|
|
174
|
-
WITH ranked AS (
|
|
175
|
-
SELECT e.*, bm25(connector_event_index_fts) AS rank
|
|
176
|
-
FROM connector_event_index_fts
|
|
177
|
-
JOIN connector_event_index e
|
|
178
|
-
ON e.event_index_id = connector_event_index_fts.event_index_id
|
|
179
|
-
WHERE connector_event_index_fts MATCH ?
|
|
180
|
-
${whereSql}
|
|
181
|
-
)
|
|
182
|
-
SELECT *
|
|
183
|
-
FROM ranked
|
|
184
|
-
${cursorSql}
|
|
185
|
-
ORDER BY rank ASC, source_timestamp_ms DESC, event_index_id ASC
|
|
186
|
-
LIMIT ?
|
|
187
|
-
`)
|
|
188
|
-
.all(...params, ...outerParams, limit + 1);
|
|
189
|
-
const pageRows = rows.slice(0, limit);
|
|
190
|
-
const nextRow = rows.length > limit ? pageRows[pageRows.length - 1] : undefined;
|
|
191
|
-
return {
|
|
192
|
-
hits: pageRows.map(toRawHit),
|
|
193
|
-
next_cursor: nextRow
|
|
194
|
-
? encodeCursor({
|
|
195
|
-
rank: Number(nextRow.rank),
|
|
196
|
-
source_timestamp_ms: Number(nextRow.source_timestamp_ms),
|
|
197
|
-
event_index_id: String(nextRow.event_index_id),
|
|
198
|
-
})
|
|
199
|
-
: null,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
function searchRaw(adapter, input) {
|
|
203
|
-
const connectors = normalizeConnectors(input.connectors);
|
|
204
|
-
if (connectors.length !== 1) {
|
|
205
|
-
throw new Error('searchRaw requires exactly one connector filter.');
|
|
206
|
-
}
|
|
207
|
-
return runSearch(adapter, { ...input, connectors });
|
|
208
|
-
}
|
|
209
|
-
function searchAllRaw(adapter, input) {
|
|
210
|
-
return runSearch(adapter, input);
|
|
211
|
-
}
|
|
212
|
-
function getRawById(adapter, rawId, visibility) {
|
|
213
|
-
const params = [rawId];
|
|
214
|
-
const clauses = ['e.event_index_id = ?'];
|
|
215
|
-
appendFilters(clauses, params, { query: '*', ...visibility });
|
|
216
|
-
const row = adapter
|
|
217
|
-
.prepare(`
|
|
218
|
-
SELECT e.*, 0 AS rank
|
|
219
|
-
FROM connector_event_index e
|
|
220
|
-
WHERE ${clauses.join(' AND ')}
|
|
221
|
-
LIMIT 1
|
|
222
|
-
`)
|
|
223
|
-
.get(...params);
|
|
224
|
-
return row ? { ...toRawHit(row), content: String(row.content) } : null;
|
|
225
|
-
}
|
|
226
|
-
function getRawWindow(adapter, rawId, input) {
|
|
227
|
-
const targetParams = [rawId];
|
|
228
|
-
const targetClauses = ['e.event_index_id = ?'];
|
|
229
|
-
appendFilters(targetClauses, targetParams, {
|
|
230
|
-
query: '*',
|
|
231
|
-
connectors: input.connectors,
|
|
232
|
-
scopes: input.scopes,
|
|
233
|
-
});
|
|
234
|
-
const targetRow = adapter
|
|
235
|
-
.prepare(`
|
|
236
|
-
SELECT e.*, 0 AS rank
|
|
237
|
-
FROM connector_event_index e
|
|
238
|
-
WHERE ${targetClauses.join(' AND ')}
|
|
239
|
-
LIMIT 1
|
|
240
|
-
`)
|
|
241
|
-
.get(...targetParams);
|
|
242
|
-
if (!targetRow) {
|
|
243
|
-
return null;
|
|
244
|
-
}
|
|
245
|
-
const target = (0, event_index_js_1.mapConnectorEventIndexRecord)(targetRow);
|
|
246
|
-
const before = normalizeWindowSize(input.before);
|
|
247
|
-
const after = normalizeWindowSize(input.after);
|
|
248
|
-
const beforeRows = beforeWindowRows(adapter, target, input, before);
|
|
249
|
-
const afterRows = afterWindowRows(adapter, target, input, after);
|
|
250
|
-
const targetHit = toRawHit(targetRow);
|
|
251
|
-
return {
|
|
252
|
-
target: targetHit,
|
|
253
|
-
items: [...beforeRows.reverse().map(toRawHit), targetHit, ...afterRows.map(toRawHit)],
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
function beforeWindowRows(adapter, target, input, limit) {
|
|
257
|
-
if (limit === 0) {
|
|
258
|
-
return [];
|
|
259
|
-
}
|
|
260
|
-
const params = [target.source_connector, target.channel, target.source_timestamp_ms];
|
|
261
|
-
const clauses = [
|
|
262
|
-
'e.source_connector = ?',
|
|
263
|
-
target.channel === null ? 'e.channel IS ?' : 'e.channel = ?',
|
|
264
|
-
'e.source_timestamp_ms < ?',
|
|
265
|
-
];
|
|
266
|
-
appendFilters(clauses, params, {
|
|
267
|
-
query: '*',
|
|
268
|
-
connectors: input.connectors,
|
|
269
|
-
scopes: input.scopes,
|
|
270
|
-
});
|
|
271
|
-
return adapter
|
|
272
|
-
.prepare(`
|
|
273
|
-
SELECT e.*, 0 AS rank
|
|
274
|
-
FROM connector_event_index e
|
|
275
|
-
WHERE ${clauses.join(' AND ')}
|
|
276
|
-
ORDER BY e.source_timestamp_ms DESC, e.event_index_id DESC
|
|
277
|
-
LIMIT ?
|
|
278
|
-
`)
|
|
279
|
-
.all(...params, limit);
|
|
280
|
-
}
|
|
281
|
-
function afterWindowRows(adapter, target, input, limit) {
|
|
282
|
-
if (limit === 0) {
|
|
283
|
-
return [];
|
|
284
|
-
}
|
|
285
|
-
const params = [target.source_connector, target.channel, target.source_timestamp_ms];
|
|
286
|
-
const clauses = [
|
|
287
|
-
'e.source_connector = ?',
|
|
288
|
-
target.channel === null ? 'e.channel IS ?' : 'e.channel = ?',
|
|
289
|
-
'e.source_timestamp_ms > ?',
|
|
290
|
-
];
|
|
291
|
-
appendFilters(clauses, params, {
|
|
292
|
-
query: '*',
|
|
293
|
-
connectors: input.connectors,
|
|
294
|
-
scopes: input.scopes,
|
|
295
|
-
});
|
|
296
|
-
return adapter
|
|
297
|
-
.prepare(`
|
|
298
|
-
SELECT e.*, 0 AS rank
|
|
299
|
-
FROM connector_event_index e
|
|
300
|
-
WHERE ${clauses.join(' AND ')}
|
|
301
|
-
ORDER BY e.source_timestamp_ms ASC, e.event_index_id ASC
|
|
302
|
-
LIMIT ?
|
|
303
|
-
`)
|
|
304
|
-
.all(...params, limit);
|
|
305
|
-
}
|
|
306
|
-
// The entity a raw event belongs to, plus its connector. source_entity_id is unique only WITHIN a
|
|
307
|
-
// connector, so history must carry the connector too or two connectors sharing an id would merge.
|
|
308
|
-
function resolveEntityAnchor(adapter, rawId, visibility) {
|
|
309
|
-
const clauses = ['e.event_index_id = ?'];
|
|
310
|
-
const params = [rawId];
|
|
311
|
-
appendFilters(clauses, params, { query: '*', ...visibility });
|
|
312
|
-
const row = adapter
|
|
313
|
-
.prepare(`SELECT source_entity_id, source_connector FROM connector_event_index e WHERE ${clauses.join(' AND ')} LIMIT 1`)
|
|
314
|
-
.get(...params);
|
|
315
|
-
return row &&
|
|
316
|
-
typeof row.source_entity_id === 'string' &&
|
|
317
|
-
row.source_entity_id.length > 0 &&
|
|
318
|
-
typeof row.source_connector === 'string' &&
|
|
319
|
-
row.source_connector.length > 0
|
|
320
|
-
? { entityId: row.source_entity_id, connector: row.source_connector }
|
|
321
|
-
: null;
|
|
322
|
-
}
|
|
323
|
-
function singleConnector(connectors) {
|
|
324
|
-
const normalized = normalizeConnectors(connectors);
|
|
325
|
-
return normalized.length === 1 ? normalized[0] : null;
|
|
326
|
-
}
|
|
327
|
-
function encodeHistoryCursor(row) {
|
|
328
|
-
return Buffer.from(JSON.stringify({ timestampMs: row.source_timestamp_ms, rawId: row.event_index_id }), 'utf8').toString('base64url');
|
|
329
|
-
}
|
|
330
|
-
function decodeHistoryCursor(cursor) {
|
|
331
|
-
if (!cursor) {
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
try {
|
|
335
|
-
const parsed = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
|
|
336
|
-
if (typeof parsed.timestampMs !== 'number' ||
|
|
337
|
-
!Number.isFinite(parsed.timestampMs) ||
|
|
338
|
-
typeof parsed.rawId !== 'string' ||
|
|
339
|
-
parsed.rawId.length === 0) {
|
|
340
|
-
throw new Error('invalid shape');
|
|
341
|
-
}
|
|
342
|
-
return { timestampMs: parsed.timestampMs, rawId: parsed.rawId };
|
|
343
|
-
}
|
|
344
|
-
catch (err) {
|
|
345
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
346
|
-
throw new Error(`Invalid raw history cursor: ${message}`);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Chronological change history of one upstream entity (rows sharing source_entity_id), bounded by
|
|
351
|
-
* the SAME connector/scope visibility as search - a citation must not out-read reading. Oldest first,
|
|
352
|
-
* cursor-paged. Rows with a NULL source_entity_id (no revision grouping) never match an entity query.
|
|
353
|
-
*/
|
|
354
|
-
function getRawHistory(adapter, input) {
|
|
355
|
-
// Revision identity is connector-scoped. The rawId path takes the anchor's own connector; the
|
|
356
|
-
// entityId path requires the caller to name exactly one connector, since an entity id alone is
|
|
357
|
-
// ambiguous across connectors.
|
|
358
|
-
const trimmedEntityId = input.entityId?.trim() ?? '';
|
|
359
|
-
const anchor = trimmedEntityId.length > 0
|
|
360
|
-
? (() => {
|
|
361
|
-
const connector = singleConnector(input.connectors);
|
|
362
|
-
return connector ? { entityId: trimmedEntityId, connector } : null;
|
|
363
|
-
})()
|
|
364
|
-
: input.rawId && input.rawId.trim().length > 0
|
|
365
|
-
? resolveEntityAnchor(adapter, input.rawId.trim(), {
|
|
366
|
-
connectors: input.connectors,
|
|
367
|
-
scopes: input.scopes,
|
|
368
|
-
})
|
|
369
|
-
: null;
|
|
370
|
-
if (!anchor) {
|
|
371
|
-
return { hits: [], next_cursor: null };
|
|
372
|
-
}
|
|
373
|
-
const limit = normalizeLimit(input.limit);
|
|
374
|
-
if (limit === 0) {
|
|
375
|
-
return { hits: [], next_cursor: null };
|
|
376
|
-
}
|
|
377
|
-
const clauses = ['e.source_entity_id = ?'];
|
|
378
|
-
const params = [anchor.entityId];
|
|
379
|
-
appendFilters(clauses, params, {
|
|
380
|
-
query: '*',
|
|
381
|
-
connectors: [anchor.connector],
|
|
382
|
-
scopes: input.scopes,
|
|
383
|
-
fromMs: input.fromMs,
|
|
384
|
-
toMs: input.toMs,
|
|
385
|
-
});
|
|
386
|
-
const cursor = decodeHistoryCursor(input.cursor);
|
|
387
|
-
if (cursor) {
|
|
388
|
-
clauses.push('(e.source_timestamp_ms > ? OR (e.source_timestamp_ms = ? AND e.event_index_id > ?))');
|
|
389
|
-
params.push(cursor.timestampMs, cursor.timestampMs, cursor.rawId);
|
|
390
|
-
}
|
|
391
|
-
const rows = adapter
|
|
392
|
-
.prepare(`
|
|
393
|
-
SELECT e.*, 0 AS rank
|
|
394
|
-
FROM connector_event_index e
|
|
395
|
-
WHERE ${clauses.join(' AND ')}
|
|
396
|
-
ORDER BY e.source_timestamp_ms ASC, e.event_index_id ASC
|
|
397
|
-
LIMIT ?
|
|
398
|
-
`)
|
|
399
|
-
.all(...params, limit + 1);
|
|
400
|
-
const pageRows = rows.slice(0, limit);
|
|
401
|
-
const nextRow = rows.length > limit ? pageRows[pageRows.length - 1] : undefined;
|
|
402
|
-
return {
|
|
403
|
-
hits: pageRows.map(toRawHit),
|
|
404
|
-
next_cursor: nextRow
|
|
405
|
-
? encodeHistoryCursor({
|
|
406
|
-
source_timestamp_ms: Number(nextRow.source_timestamp_ms),
|
|
407
|
-
event_index_id: String(nextRow.event_index_id),
|
|
408
|
-
})
|
|
409
|
-
: null,
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
//# sourceMappingURL=raw-query.js.map
|