@atomicmemory/core 1.0.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/CHANGELOG.md +27 -0
- package/LICENSE +201 -0
- package/README.md +314 -0
- package/dist/app/bind-ephemeral.d.ts +18 -0
- package/dist/app/bind-ephemeral.js +22 -0
- package/dist/app/cors-headers.d.ts +12 -0
- package/dist/app/cors-headers.js +18 -0
- package/dist/app/create-app.d.ts +25 -0
- package/dist/app/create-app.js +156 -0
- package/dist/app/runtime-config-route-snapshot.d.ts +27 -0
- package/dist/app/runtime-config-route-snapshot.js +27 -0
- package/dist/app/runtime-container.d.ts +281 -0
- package/dist/app/runtime-container.js +297 -0
- package/dist/app/startup-checks.d.ts +28 -0
- package/dist/app/startup-checks.js +45 -0
- package/dist/bin.d.ts +17 -0
- package/dist/bin.js +128 -0
- package/dist/config.d.ts +680 -0
- package/dist/config.js +808 -0
- package/dist/db/agent-trust-repository.d.ts +49 -0
- package/dist/db/agent-trust-repository.js +66 -0
- package/dist/db/belief-edges-repository.d.ts +68 -0
- package/dist/db/belief-edges-repository.js +124 -0
- package/dist/db/claim-repository.d.ts +6 -0
- package/dist/db/claim-repository.js +4 -0
- package/dist/db/contradictions-repository.d.ts +56 -0
- package/dist/db/contradictions-repository.js +88 -0
- package/dist/db/document-chunk-repository.d.ts +48 -0
- package/dist/db/document-chunk-repository.js +145 -0
- package/dist/db/document-chunk-types.d.ts +35 -0
- package/dist/db/document-chunk-types.js +9 -0
- package/dist/db/document-list-cursor.d.ts +45 -0
- package/dist/db/document-list-cursor.js +111 -0
- package/dist/db/document-list-repository.d.ts +103 -0
- package/dist/db/document-list-repository.js +204 -0
- package/dist/db/entity-cards-repository.d.ts +37 -0
- package/dist/db/entity-cards-repository.js +46 -0
- package/dist/db/entity-values-repository.d.ts +26 -0
- package/dist/db/entity-values-repository.js +57 -0
- package/dist/db/link-repository.d.ts +30 -0
- package/dist/db/link-repository.js +54 -0
- package/dist/db/memory-repository.d.ts +163 -0
- package/dist/db/memory-repository.js +232 -0
- package/dist/db/migrate.d.ts +6 -0
- package/dist/db/migrate.js +36 -0
- package/dist/db/mmr.d.ts +14 -0
- package/dist/db/mmr.js +57 -0
- package/dist/db/passport-feed-repository.d.ts +91 -0
- package/dist/db/passport-feed-repository.js +198 -0
- package/dist/db/pg-episode-store.d.ts +19 -0
- package/dist/db/pg-episode-store.js +17 -0
- package/dist/db/pg-link-store.d.ts +17 -0
- package/dist/db/pg-link-store.js +14 -0
- package/dist/db/pg-memory-store.d.ts +68 -0
- package/dist/db/pg-memory-store.js +53 -0
- package/dist/db/pg-recap-store.d.ts +13 -0
- package/dist/db/pg-recap-store.js +19 -0
- package/dist/db/pg-representation-store.d.ts +17 -0
- package/dist/db/pg-representation-store.js +17 -0
- package/dist/db/pg-search-store.d.ts +29 -0
- package/dist/db/pg-search-store.js +47 -0
- package/dist/db/pool.d.ts +5 -0
- package/dist/db/pool.js +21 -0
- package/dist/db/ppr.d.ts +56 -0
- package/dist/db/ppr.js +178 -0
- package/dist/db/query-helpers.d.ts +44 -0
- package/dist/db/query-helpers.js +60 -0
- package/dist/db/raw-doc-artifact-sync.d.ts +128 -0
- package/dist/db/raw-doc-artifact-sync.js +259 -0
- package/dist/db/raw-document-blob-repository.d.ts +148 -0
- package/dist/db/raw-document-blob-repository.js +300 -0
- package/dist/db/raw-document-repository.d.ts +104 -0
- package/dist/db/raw-document-repository.js +410 -0
- package/dist/db/raw-document-status-repository.d.ts +122 -0
- package/dist/db/raw-document-status-repository.js +183 -0
- package/dist/db/raw-document-types.d.ts +236 -0
- package/dist/db/raw-document-types.js +10 -0
- package/dist/db/raw-storage-reconciliation-repository.d.ts +110 -0
- package/dist/db/raw-storage-reconciliation-repository.js +200 -0
- package/dist/db/reflection-jobs-repository.d.ts +33 -0
- package/dist/db/reflection-jobs-repository.js +48 -0
- package/dist/db/reflections-repository.d.ts +41 -0
- package/dist/db/reflections-repository.js +83 -0
- package/dist/db/repository-claims.d.ts +141 -0
- package/dist/db/repository-claims.js +376 -0
- package/dist/db/repository-deferred-audn.d.ts +33 -0
- package/dist/db/repository-deferred-audn.js +69 -0
- package/dist/db/repository-document-delete.d.ts +53 -0
- package/dist/db/repository-document-delete.js +156 -0
- package/dist/db/repository-entities.d.ts +114 -0
- package/dist/db/repository-entities.js +317 -0
- package/dist/db/repository-entity-attributes.d.ts +41 -0
- package/dist/db/repository-entity-attributes.js +65 -0
- package/dist/db/repository-entity-graph.d.ts +32 -0
- package/dist/db/repository-entity-graph.js +87 -0
- package/dist/db/repository-first-mentions.d.ts +41 -0
- package/dist/db/repository-first-mentions.js +79 -0
- package/dist/db/repository-lessons.d.ts +51 -0
- package/dist/db/repository-lessons.js +90 -0
- package/dist/db/repository-links.d.ts +26 -0
- package/dist/db/repository-links.js +105 -0
- package/dist/db/repository-observation.d.ts +26 -0
- package/dist/db/repository-observation.js +51 -0
- package/dist/db/repository-read.d.ts +56 -0
- package/dist/db/repository-read.js +271 -0
- package/dist/db/repository-recaps.d.ts +59 -0
- package/dist/db/repository-recaps.js +158 -0
- package/dist/db/repository-representations.d.ts +48 -0
- package/dist/db/repository-representations.js +162 -0
- package/dist/db/repository-temporal-state.d.ts +35 -0
- package/dist/db/repository-temporal-state.js +46 -0
- package/dist/db/repository-tll.d.ts +88 -0
- package/dist/db/repository-tll.js +179 -0
- package/dist/db/repository-types.d.ts +313 -0
- package/dist/db/repository-types.js +142 -0
- package/dist/db/repository-user-profiles.d.ts +17 -0
- package/dist/db/repository-user-profiles.js +28 -0
- package/dist/db/repository-vector-search.d.ts +33 -0
- package/dist/db/repository-vector-search.js +373 -0
- package/dist/db/repository-wipe.d.ts +34 -0
- package/dist/db/repository-wipe.js +94 -0
- package/dist/db/repository-write.d.ts +61 -0
- package/dist/db/repository-write.js +279 -0
- package/dist/db/schema.sql +1355 -0
- package/dist/db/storage-artifact-delete-tx.d.ts +56 -0
- package/dist/db/storage-artifact-delete-tx.js +123 -0
- package/dist/db/storage-artifact-providers.d.ts +21 -0
- package/dist/db/storage-artifact-providers.js +21 -0
- package/dist/db/storage-artifact-recovery-repository.d.ts +66 -0
- package/dist/db/storage-artifact-recovery-repository.js +58 -0
- package/dist/db/storage-artifact-repository.d.ts +329 -0
- package/dist/db/storage-artifact-repository.js +497 -0
- package/dist/db/stores.d.ts +220 -0
- package/dist/db/stores.js +12 -0
- package/dist/db/summaries-repository.d.ts +74 -0
- package/dist/db/summaries-repository.js +125 -0
- package/dist/eval/beam-10m-loader.d.ts +98 -0
- package/dist/eval/beam-10m-loader.js +128 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/middleware/require-bearer.d.ts +27 -0
- package/dist/middleware/require-bearer.js +60 -0
- package/dist/middleware/validate-response.d.ts +33 -0
- package/dist/middleware/validate-response.js +55 -0
- package/dist/middleware/validate.d.ts +43 -0
- package/dist/middleware/validate.js +85 -0
- package/dist/routes/agents.d.ts +13 -0
- package/dist/routes/agents.js +89 -0
- package/dist/routes/document-response-formatters.d.ts +98 -0
- package/dist/routes/document-response-formatters.js +243 -0
- package/dist/routes/documents.d.ts +74 -0
- package/dist/routes/documents.js +425 -0
- package/dist/routes/memories.d.ts +29 -0
- package/dist/routes/memories.js +725 -0
- package/dist/routes/memory-response-formatters.d.ts +179 -0
- package/dist/routes/memory-response-formatters.js +210 -0
- package/dist/routes/public-raw-storage-metadata.d.ts +54 -0
- package/dist/routes/public-raw-storage-metadata.js +56 -0
- package/dist/routes/reflect.d.ts +14 -0
- package/dist/routes/reflect.js +19 -0
- package/dist/routes/response-schema-map.d.ts +14 -0
- package/dist/routes/response-schema-map.js +69 -0
- package/dist/routes/route-errors.d.ts +12 -0
- package/dist/routes/route-errors.js +30 -0
- package/dist/routes/storage-error-handlers.d.ts +34 -0
- package/dist/routes/storage-error-handlers.js +185 -0
- package/dist/routes/storage-response-formatters.d.ts +44 -0
- package/dist/routes/storage-response-formatters.js +155 -0
- package/dist/routes/storage.d.ts +38 -0
- package/dist/routes/storage.js +369 -0
- package/dist/routes/upstream-provider-errors.d.ts +19 -0
- package/dist/routes/upstream-provider-errors.js +95 -0
- package/dist/schemas/agents.d.ts +79 -0
- package/dist/schemas/agents.js +126 -0
- package/dist/schemas/common.d.ts +110 -0
- package/dist/schemas/common.js +190 -0
- package/dist/schemas/document-list-responses.d.ts +102 -0
- package/dist/schemas/document-list-responses.js +87 -0
- package/dist/schemas/document-list-schemas.d.ts +123 -0
- package/dist/schemas/document-list-schemas.js +174 -0
- package/dist/schemas/document-response-schemas.d.ts +610 -0
- package/dist/schemas/document-response-schemas.js +264 -0
- package/dist/schemas/document-status-envelope.d.ts +48 -0
- package/dist/schemas/document-status-envelope.js +54 -0
- package/dist/schemas/documents.d.ts +292 -0
- package/dist/schemas/documents.js +449 -0
- package/dist/schemas/errors.d.ts +75 -0
- package/dist/schemas/errors.js +105 -0
- package/dist/schemas/memories.d.ts +378 -0
- package/dist/schemas/memories.js +542 -0
- package/dist/schemas/openapi.d.ts +24 -0
- package/dist/schemas/openapi.js +1038 -0
- package/dist/schemas/response-scalars.d.ts +10 -0
- package/dist/schemas/response-scalars.js +10 -0
- package/dist/schemas/responses.d.ts +536 -0
- package/dist/schemas/responses.js +350 -0
- package/dist/schemas/search-response-parts.d.ts +97 -0
- package/dist/schemas/search-response-parts.js +103 -0
- package/dist/schemas/storage-schemas.d.ts +175 -0
- package/dist/schemas/storage-schemas.js +277 -0
- package/dist/schemas/zod-setup.d.ts +15 -0
- package/dist/schemas/zod-setup.js +17 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.js +57 -0
- package/dist/services/abstract-query-policy.d.ts +13 -0
- package/dist/services/abstract-query-policy.js +50 -0
- package/dist/services/affinity-clustering.d.ts +66 -0
- package/dist/services/affinity-clustering.js +125 -0
- package/dist/services/agentic-retrieval.d.ts +38 -0
- package/dist/services/agentic-retrieval.js +126 -0
- package/dist/services/answer-format.d.ts +56 -0
- package/dist/services/answer-format.js +118 -0
- package/dist/services/answer-rescue.d.ts +72 -0
- package/dist/services/answer-rescue.js +177 -0
- package/dist/services/answer-verifier.d.ts +24 -0
- package/dist/services/answer-verifier.js +73 -0
- package/dist/services/api-retry.d.ts +6 -0
- package/dist/services/api-retry.js +41 -0
- package/dist/services/assistant-turn-filter.d.ts +20 -0
- package/dist/services/assistant-turn-filter.js +69 -0
- package/dist/services/atomicmem-uri.d.ts +33 -0
- package/dist/services/atomicmem-uri.js +86 -0
- package/dist/services/audit-events.d.ts +54 -0
- package/dist/services/audit-events.js +56 -0
- package/dist/services/chunked-extraction.d.ts +21 -0
- package/dist/services/chunked-extraction.js +108 -0
- package/dist/services/claim-slotting.d.ts +27 -0
- package/dist/services/claim-slotting.js +38 -0
- package/dist/services/claude-code-llm.d.ts +19 -0
- package/dist/services/claude-code-llm.js +96 -0
- package/dist/services/composite-dedup.d.ts +50 -0
- package/dist/services/composite-dedup.js +153 -0
- package/dist/services/composite-grouping.d.ts +41 -0
- package/dist/services/composite-grouping.js +111 -0
- package/dist/services/composite-staleness.d.ts +20 -0
- package/dist/services/composite-staleness.js +50 -0
- package/dist/services/conciseness-preference.d.ts +14 -0
- package/dist/services/conciseness-preference.js +42 -0
- package/dist/services/conflict-policy.d.ts +20 -0
- package/dist/services/conflict-policy.js +335 -0
- package/dist/services/consensus-extraction.d.ts +39 -0
- package/dist/services/consensus-extraction.js +147 -0
- package/dist/services/consensus-validation.d.ts +52 -0
- package/dist/services/consensus-validation.js +206 -0
- package/dist/services/consolidation-service.d.ts +60 -0
- package/dist/services/consolidation-service.js +171 -0
- package/dist/services/content-detection.d.ts +18 -0
- package/dist/services/content-detection.js +25 -0
- package/dist/services/contradiction-surfacing.d.ts +62 -0
- package/dist/services/contradiction-surfacing.js +111 -0
- package/dist/services/cost-telemetry.d.ts +39 -0
- package/dist/services/cost-telemetry.js +58 -0
- package/dist/services/counter-evidence.d.ts +34 -0
- package/dist/services/counter-evidence.js +92 -0
- package/dist/services/current-state-ranking.d.ts +21 -0
- package/dist/services/current-state-ranking.js +152 -0
- package/dist/services/deferred-audn.d.ts +47 -0
- package/dist/services/deferred-audn.js +162 -0
- package/dist/services/document-chunker.d.ts +50 -0
- package/dist/services/document-chunker.js +153 -0
- package/dist/services/document-failure-markers.d.ts +91 -0
- package/dist/services/document-failure-markers.js +305 -0
- package/dist/services/document-indexer.d.ts +122 -0
- package/dist/services/document-indexer.js +405 -0
- package/dist/services/document-service.d.ts +245 -0
- package/dist/services/document-service.js +325 -0
- package/dist/services/document-upload-artifact-sync.d.ts +80 -0
- package/dist/services/document-upload-artifact-sync.js +162 -0
- package/dist/services/document-upload-beta2-recovery.d.ts +72 -0
- package/dist/services/document-upload-beta2-recovery.js +94 -0
- package/dist/services/document-upload.d.ts +44 -0
- package/dist/services/document-upload.js +353 -0
- package/dist/services/embedding.d.ts +57 -0
- package/dist/services/embedding.js +416 -0
- package/dist/services/entity-attribute-extractor.d.ts +34 -0
- package/dist/services/entity-attribute-extractor.js +117 -0
- package/dist/services/entity-card-synthesis.d.ts +54 -0
- package/dist/services/entity-card-synthesis.js +92 -0
- package/dist/services/entity-dedup.d.ts +9 -0
- package/dist/services/entity-dedup.js +14 -0
- package/dist/services/entity-graph.d.ts +17 -0
- package/dist/services/entity-graph.js +135 -0
- package/dist/services/entropy-gate.d.ts +52 -0
- package/dist/services/entropy-gate.js +56 -0
- package/dist/services/episode-fetcher.d.ts +47 -0
- package/dist/services/episode-fetcher.js +128 -0
- package/dist/services/event-anchor-facts.d.ts +8 -0
- package/dist/services/event-anchor-facts.js +205 -0
- package/dist/services/event-chain-detector.d.ts +52 -0
- package/dist/services/event-chain-detector.js +83 -0
- package/dist/services/extraction-cache.d.ts +9 -0
- package/dist/services/extraction-cache.js +54 -0
- package/dist/services/extraction-enrichment.d.ts +9 -0
- package/dist/services/extraction-enrichment.js +223 -0
- package/dist/services/extraction.d.ts +69 -0
- package/dist/services/extraction.js +596 -0
- package/dist/services/fact-normalization.d.ts +12 -0
- package/dist/services/fact-normalization.js +248 -0
- package/dist/services/filecoin-observability.d.ts +127 -0
- package/dist/services/filecoin-observability.js +200 -0
- package/dist/services/first-mention-service.d.ts +76 -0
- package/dist/services/first-mention-service.js +186 -0
- package/dist/services/hierarchical-retrieval.d.ts +49 -0
- package/dist/services/hierarchical-retrieval.js +50 -0
- package/dist/services/ingest-fact-pipeline.d.ts +32 -0
- package/dist/services/ingest-fact-pipeline.js +212 -0
- package/dist/services/ingest-post-write.d.ts +50 -0
- package/dist/services/ingest-post-write.js +117 -0
- package/dist/services/ingest-trace.d.ts +32 -0
- package/dist/services/ingest-trace.js +60 -0
- package/dist/services/input-sanitizer.d.ts +41 -0
- package/dist/services/input-sanitizer.js +135 -0
- package/dist/services/iterative-retrieval.d.ts +26 -0
- package/dist/services/iterative-retrieval.js +139 -0
- package/dist/services/keyword-expansion.d.ts +10 -0
- package/dist/services/keyword-expansion.js +26 -0
- package/dist/services/lesson-service.d.ts +68 -0
- package/dist/services/lesson-service.js +178 -0
- package/dist/services/literal-extractor.d.ts +16 -0
- package/dist/services/literal-extractor.js +74 -0
- package/dist/services/literal-list-protection.d.ts +17 -0
- package/dist/services/literal-list-protection.js +134 -0
- package/dist/services/literal-query-expansion.d.ts +20 -0
- package/dist/services/literal-query-expansion.js +181 -0
- package/dist/services/llm.d.ts +61 -0
- package/dist/services/llm.js +265 -0
- package/dist/services/memcell-projection.d.ts +17 -0
- package/dist/services/memcell-projection.js +41 -0
- package/dist/services/memory-audn.d.ts +43 -0
- package/dist/services/memory-audn.js +419 -0
- package/dist/services/memory-crud.d.ts +93 -0
- package/dist/services/memory-crud.js +255 -0
- package/dist/services/memory-ingest.d.ts +21 -0
- package/dist/services/memory-ingest.js +249 -0
- package/dist/services/memory-lifecycle.d.ts +75 -0
- package/dist/services/memory-lifecycle.js +108 -0
- package/dist/services/memory-lineage.d.ts +181 -0
- package/dist/services/memory-lineage.js +232 -0
- package/dist/services/memory-network.d.ts +40 -0
- package/dist/services/memory-network.js +75 -0
- package/dist/services/memory-search-types.d.ts +25 -0
- package/dist/services/memory-search-types.js +10 -0
- package/dist/services/memory-search.d.ts +48 -0
- package/dist/services/memory-search.js +505 -0
- package/dist/services/memory-service-types.d.ts +371 -0
- package/dist/services/memory-service-types.js +8 -0
- package/dist/services/memory-service.d.ts +152 -0
- package/dist/services/memory-service.js +225 -0
- package/dist/services/memory-storage.d.ts +33 -0
- package/dist/services/memory-storage.js +328 -0
- package/dist/services/msr-aggregator.d.ts +38 -0
- package/dist/services/msr-aggregator.js +97 -0
- package/dist/services/msr-detector.d.ts +35 -0
- package/dist/services/msr-detector.js +65 -0
- package/dist/services/namespace-retrieval.d.ts +60 -0
- package/dist/services/namespace-retrieval.js +180 -0
- package/dist/services/observation-date-extraction.d.ts +12 -0
- package/dist/services/observation-date-extraction.js +50 -0
- package/dist/services/observation-service.d.ts +27 -0
- package/dist/services/observation-service.js +84 -0
- package/dist/services/packaging-observability.d.ts +29 -0
- package/dist/services/packaging-observability.js +146 -0
- package/dist/services/query-expansion.d.ts +83 -0
- package/dist/services/query-expansion.js +242 -0
- package/dist/services/query-keyword-matches.d.ts +6 -0
- package/dist/services/query-keyword-matches.js +56 -0
- package/dist/services/query-term-visibility.d.ts +28 -0
- package/dist/services/query-term-visibility.js +100 -0
- package/dist/services/quick-extraction.d.ts +25 -0
- package/dist/services/quick-extraction.js +431 -0
- package/dist/services/quoted-entity-extraction.d.ts +10 -0
- package/dist/services/quoted-entity-extraction.js +161 -0
- package/dist/services/raw-storage-reconciler-backoff.d.ts +8 -0
- package/dist/services/raw-storage-reconciler-backoff.js +14 -0
- package/dist/services/raw-storage-reconciler-scheduler.d.ts +29 -0
- package/dist/services/raw-storage-reconciler-scheduler.js +43 -0
- package/dist/services/raw-storage-reconciler.d.ts +71 -0
- package/dist/services/raw-storage-reconciler.js +278 -0
- package/dist/services/recap-builder.d.ts +49 -0
- package/dist/services/recap-builder.js +157 -0
- package/dist/services/reflect-jobs.d.ts +23 -0
- package/dist/services/reflect-jobs.js +36 -0
- package/dist/services/reflect-prompts.d.ts +71 -0
- package/dist/services/reflect-prompts.js +99 -0
- package/dist/services/reflect-retrieval.d.ts +33 -0
- package/dist/services/reflect-retrieval.js +30 -0
- package/dist/services/reflect.d.ts +49 -0
- package/dist/services/reflect.js +84 -0
- package/dist/services/relative-temporal.d.ts +14 -0
- package/dist/services/relative-temporal.js +163 -0
- package/dist/services/relevance-policy.d.ts +37 -0
- package/dist/services/relevance-policy.js +109 -0
- package/dist/services/rerank.d.ts +32 -0
- package/dist/services/rerank.js +118 -0
- package/dist/services/reranker.d.ts +20 -0
- package/dist/services/reranker.js +99 -0
- package/dist/services/retrieval-channel-rules.d.ts +34 -0
- package/dist/services/retrieval-channel-rules.js +41 -0
- package/dist/services/retrieval-config-overlay.d.ts +36 -0
- package/dist/services/retrieval-config-overlay.js +44 -0
- package/dist/services/retrieval-format.d.ts +119 -0
- package/dist/services/retrieval-format.js +559 -0
- package/dist/services/retrieval-policy.d.ts +69 -0
- package/dist/services/retrieval-policy.js +275 -0
- package/dist/services/retrieval-profiles.d.ts +37 -0
- package/dist/services/retrieval-profiles.js +90 -0
- package/dist/services/retrieval-side-effects.d.ts +14 -0
- package/dist/services/retrieval-side-effects.js +26 -0
- package/dist/services/retrieval-trace.d.ts +108 -0
- package/dist/services/retrieval-trace.js +147 -0
- package/dist/services/rrf-fusion.d.ts +18 -0
- package/dist/services/rrf-fusion.js +34 -0
- package/dist/services/search-pipeline.d.ts +71 -0
- package/dist/services/search-pipeline.js +788 -0
- package/dist/services/session-date.d.ts +20 -0
- package/dist/services/session-date.js +61 -0
- package/dist/services/session-packaging.d.ts +53 -0
- package/dist/services/session-packaging.js +182 -0
- package/dist/services/session-summary-generator.d.ts +53 -0
- package/dist/services/session-summary-generator.js +134 -0
- package/dist/services/specialists/cr-specialist.d.ts +52 -0
- package/dist/services/specialists/cr-specialist.js +121 -0
- package/dist/services/specialists/dispatch.d.ts +53 -0
- package/dist/services/specialists/dispatch.js +102 -0
- package/dist/services/specialists/ie-ku-specialist.d.ts +37 -0
- package/dist/services/specialists/ie-ku-specialist.js +63 -0
- package/dist/services/specialists/msr-specialist.d.ts +61 -0
- package/dist/services/specialists/msr-specialist.js +162 -0
- package/dist/services/specialists/tr-specialist.d.ts +37 -0
- package/dist/services/specialists/tr-specialist.js +146 -0
- package/dist/services/storage-key-prefix.d.ts +42 -0
- package/dist/services/storage-key-prefix.js +45 -0
- package/dist/services/storage-put-recovery.d.ts +71 -0
- package/dist/services/storage-put-recovery.js +269 -0
- package/dist/services/storage-service-errors.d.ts +124 -0
- package/dist/services/storage-service-errors.js +189 -0
- package/dist/services/storage-service.d.ts +176 -0
- package/dist/services/storage-service.js +423 -0
- package/dist/services/subject-aware-ranking.d.ts +19 -0
- package/dist/services/subject-aware-ranking.js +161 -0
- package/dist/services/supplemental-extraction.d.ts +7 -0
- package/dist/services/supplemental-extraction.js +116 -0
- package/dist/services/tbc-execution.d.ts +49 -0
- package/dist/services/tbc-execution.js +284 -0
- package/dist/services/temporal-classifier.d.ts +56 -0
- package/dist/services/temporal-classifier.js +94 -0
- package/dist/services/temporal-endpoint-evidence.d.ts +12 -0
- package/dist/services/temporal-endpoint-evidence.js +313 -0
- package/dist/services/temporal-fingerprint.d.ts +6 -0
- package/dist/services/temporal-fingerprint.js +12 -0
- package/dist/services/temporal-format.d.ts +9 -0
- package/dist/services/temporal-format.js +21 -0
- package/dist/services/temporal-intent.d.ts +39 -0
- package/dist/services/temporal-intent.js +78 -0
- package/dist/services/temporal-query-constraints.d.ts +16 -0
- package/dist/services/temporal-query-constraints.js +107 -0
- package/dist/services/temporal-query-expansion.d.ts +14 -0
- package/dist/services/temporal-query-expansion.js +131 -0
- package/dist/services/temporal-rerank.d.ts +22 -0
- package/dist/services/temporal-rerank.js +47 -0
- package/dist/services/temporal-result-protection.d.ts +7 -0
- package/dist/services/temporal-result-protection.js +60 -0
- package/dist/services/temporal-state-write.d.ts +57 -0
- package/dist/services/temporal-state-write.js +45 -0
- package/dist/services/tiered-context.d.ts +87 -0
- package/dist/services/tiered-context.js +214 -0
- package/dist/services/tiered-loading.d.ts +88 -0
- package/dist/services/tiered-loading.js +263 -0
- package/dist/services/timeline-pack.d.ts +36 -0
- package/dist/services/timeline-pack.js +50 -0
- package/dist/services/timing.d.ts +13 -0
- package/dist/services/timing.js +72 -0
- package/dist/services/tll-augmentation.d.ts +20 -0
- package/dist/services/tll-augmentation.js +125 -0
- package/dist/services/tll-retrieval.d.ts +55 -0
- package/dist/services/tll-retrieval.js +101 -0
- package/dist/services/topic-abstraction.d.ts +36 -0
- package/dist/services/topic-abstraction.js +105 -0
- package/dist/services/trust-scoring.d.ts +43 -0
- package/dist/services/trust-scoring.js +89 -0
- package/dist/services/typed-belief-calculus.d.ts +126 -0
- package/dist/services/typed-belief-calculus.js +204 -0
- package/dist/services/upload-config.d.ts +34 -0
- package/dist/services/upload-config.js +23 -0
- package/dist/services/upload-decision.d.ts +65 -0
- package/dist/services/upload-decision.js +98 -0
- package/dist/services/upload-helpers.d.ts +107 -0
- package/dist/services/upload-helpers.js +148 -0
- package/dist/services/user-profile-builder.d.ts +22 -0
- package/dist/services/user-profile-builder.js +109 -0
- package/dist/services/voyage-embedding.d.ts +22 -0
- package/dist/services/voyage-embedding.js +77 -0
- package/dist/services/write-security.d.ts +31 -0
- package/dist/services/write-security.js +64 -0
- package/dist/storage/artifact-public-redaction.d.ts +34 -0
- package/dist/storage/artifact-public-redaction.js +83 -0
- package/dist/storage/cleanup.d.ts +103 -0
- package/dist/storage/cleanup.js +138 -0
- package/dist/storage/codec-factory.d.ts +17 -0
- package/dist/storage/codec-factory.js +33 -0
- package/dist/storage/codecs/aes-gcm-codec.d.ts +44 -0
- package/dist/storage/codecs/aes-gcm-codec.js +108 -0
- package/dist/storage/codecs/noop-codec.d.ts +16 -0
- package/dist/storage/codecs/noop-codec.js +23 -0
- package/dist/storage/factory.d.ts +44 -0
- package/dist/storage/factory.js +99 -0
- package/dist/storage/filecoin-cid-validation.d.ts +82 -0
- package/dist/storage/filecoin-cid-validation.js +122 -0
- package/dist/storage/filecoin-public-metadata.d.ts +73 -0
- package/dist/storage/filecoin-public-metadata.js +110 -0
- package/dist/storage/local-fs-store.d.ts +39 -0
- package/dist/storage/local-fs-store.js +145 -0
- package/dist/storage/pointer-uri-allowlist.d.ts +38 -0
- package/dist/storage/pointer-uri-allowlist.js +70 -0
- package/dist/storage/provider-metadata-projection.d.ts +27 -0
- package/dist/storage/provider-metadata-projection.js +68 -0
- package/dist/storage/providers/filecoin/backend.d.ts +42 -0
- package/dist/storage/providers/filecoin/backend.js +250 -0
- package/dist/storage/providers/filecoin/config.d.ts +70 -0
- package/dist/storage/providers/filecoin/config.js +275 -0
- package/dist/storage/providers/filecoin/errors.d.ts +45 -0
- package/dist/storage/providers/filecoin/errors.js +56 -0
- package/dist/storage/providers/filecoin/filecoin-pin-car.d.ts +78 -0
- package/dist/storage/providers/filecoin/filecoin-pin-car.js +155 -0
- package/dist/storage/providers/filecoin/filecoin-pin-client.d.ts +92 -0
- package/dist/storage/providers/filecoin/filecoin-pin-client.js +199 -0
- package/dist/storage/providers/filecoin/filecoin-pin-mapping.d.ts +58 -0
- package/dist/storage/providers/filecoin/filecoin-pin-mapping.js +103 -0
- package/dist/storage/providers/filecoin/filecoin-pin-timeout.d.ts +30 -0
- package/dist/storage/providers/filecoin/filecoin-pin-timeout.js +53 -0
- package/dist/storage/providers/filecoin/filecoin-pin-vendor.d.ts +111 -0
- package/dist/storage/providers/filecoin/filecoin-pin-vendor.js +87 -0
- package/dist/storage/providers/filecoin/hints.d.ts +71 -0
- package/dist/storage/providers/filecoin/hints.js +123 -0
- package/dist/storage/providers/filecoin/index.d.ts +51 -0
- package/dist/storage/providers/filecoin/index.js +103 -0
- package/dist/storage/providers/filecoin/ipfs-cid.d.ts +50 -0
- package/dist/storage/providers/filecoin/ipfs-cid.js +64 -0
- package/dist/storage/providers/filecoin/metadata.d.ts +72 -0
- package/dist/storage/providers/filecoin/metadata.js +137 -0
- package/dist/storage/providers/filecoin/piece-cid.d.ts +48 -0
- package/dist/storage/providers/filecoin/piece-cid.js +57 -0
- package/dist/storage/providers/filecoin/provider-client.d.ts +234 -0
- package/dist/storage/providers/filecoin/provider-client.js +27 -0
- package/dist/storage/providers/filecoin/readiness.d.ts +62 -0
- package/dist/storage/providers/filecoin/readiness.js +85 -0
- package/dist/storage/providers/filecoin/retriever.d.ts +82 -0
- package/dist/storage/providers/filecoin/retriever.js +63 -0
- package/dist/storage/providers/filecoin/skeleton-client.d.ts +36 -0
- package/dist/storage/providers/filecoin/skeleton-client.js +55 -0
- package/dist/storage/providers/filecoin/synapse-client.d.ts +169 -0
- package/dist/storage/providers/filecoin/synapse-client.js +343 -0
- package/dist/storage/providers/filecoin/synapse-construction.d.ts +26 -0
- package/dist/storage/providers/filecoin/synapse-construction.js +47 -0
- package/dist/storage/providers/filecoin/synapse-error-mapping.d.ts +23 -0
- package/dist/storage/providers/filecoin/synapse-error-mapping.js +49 -0
- package/dist/storage/providers/filecoin/synapse-readiness.d.ts +37 -0
- package/dist/storage/providers/filecoin/synapse-readiness.js +231 -0
- package/dist/storage/providers/filecoin/uri.d.ts +49 -0
- package/dist/storage/providers/filecoin/uri.js +84 -0
- package/dist/storage/providers/filecoin/verified-fetch-lifecycle.d.ts +77 -0
- package/dist/storage/providers/filecoin/verified-fetch-lifecycle.js +196 -0
- package/dist/storage/providers/filecoin/verified-fetch-retriever.d.ts +54 -0
- package/dist/storage/providers/filecoin/verified-fetch-retriever.js +81 -0
- package/dist/storage/providers/filecoin/verified-fetch-vendor.d.ts +71 -0
- package/dist/storage/providers/filecoin/verified-fetch-vendor.js +94 -0
- package/dist/storage/raw-content-codec.d.ts +89 -0
- package/dist/storage/raw-content-codec.js +47 -0
- package/dist/storage/raw-content-store-backend-adapter.d.ts +28 -0
- package/dist/storage/raw-content-store-backend-adapter.js +67 -0
- package/dist/storage/raw-content-store.d.ts +228 -0
- package/dist/storage/raw-content-store.js +27 -0
- package/dist/storage/s3-store.d.ts +42 -0
- package/dist/storage/s3-store.js +181 -0
- package/dist/storage/storage-backend-registry.d.ts +58 -0
- package/dist/storage/storage-backend-registry.js +56 -0
- package/dist/storage/storage-backend.d.ts +82 -0
- package/dist/storage/storage-backend.js +14 -0
- package/dist/storage/storage-capabilities.d.ts +56 -0
- package/dist/storage/storage-capabilities.js +170 -0
- package/dist/storage/store-registry.d.ts +67 -0
- package/dist/storage/store-registry.js +77 -0
- package/dist/vector-math.d.ts +15 -0
- package/dist/vector-math.js +31 -0
- package/dist/xml-escape.d.ts +5 -0
- package/dist/xml-escape.js +7 -0
- package/openapi.json +15395 -0
- package/openapi.yaml +10794 -0
- package/package.json +119 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repository for typed cross-agent memory links.
|
|
3
|
+
* Stores and queries endorsement, contradiction, elaboration, and correction
|
|
4
|
+
* links between memories from different agents.
|
|
5
|
+
*/
|
|
6
|
+
import pg from 'pg';
|
|
7
|
+
export type LinkType = 'similarity' | 'endorsement' | 'contradiction' | 'elaboration' | 'correction';
|
|
8
|
+
export interface MemoryLink {
|
|
9
|
+
id: string;
|
|
10
|
+
source_memory_id: string;
|
|
11
|
+
target_memory_id: string;
|
|
12
|
+
link_type: LinkType;
|
|
13
|
+
source_agent_id: string | null;
|
|
14
|
+
target_agent_id: string | null;
|
|
15
|
+
created_at: Date;
|
|
16
|
+
}
|
|
17
|
+
export interface StoreLinkInput {
|
|
18
|
+
sourceMemoryId: string;
|
|
19
|
+
targetMemoryId: string;
|
|
20
|
+
linkType: LinkType;
|
|
21
|
+
sourceAgentId?: string | null;
|
|
22
|
+
targetAgentId?: string | null;
|
|
23
|
+
}
|
|
24
|
+
export declare class LinkRepository {
|
|
25
|
+
private pool;
|
|
26
|
+
constructor(pool: pg.Pool);
|
|
27
|
+
storeLink(input: StoreLinkInput): Promise<string>;
|
|
28
|
+
getLinksForMemory(memoryId: string): Promise<MemoryLink[]>;
|
|
29
|
+
getLinksForMemories(memoryIds: string[]): Promise<Map<string, MemoryLink[]>>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repository for typed cross-agent memory links.
|
|
3
|
+
* Stores and queries endorsement, contradiction, elaboration, and correction
|
|
4
|
+
* links between memories from different agents.
|
|
5
|
+
*/
|
|
6
|
+
export class LinkRepository {
|
|
7
|
+
pool;
|
|
8
|
+
constructor(pool) {
|
|
9
|
+
this.pool = pool;
|
|
10
|
+
}
|
|
11
|
+
async storeLink(input) {
|
|
12
|
+
const result = await this.pool.query(`INSERT INTO memory_links (source_memory_id, target_memory_id, link_type, source_agent_id, target_agent_id)
|
|
13
|
+
VALUES ($1, $2, $3, $4, $5)
|
|
14
|
+
ON CONFLICT (source_memory_id, target_memory_id, link_type) DO NOTHING
|
|
15
|
+
RETURNING id`, [
|
|
16
|
+
input.sourceMemoryId,
|
|
17
|
+
input.targetMemoryId,
|
|
18
|
+
input.linkType,
|
|
19
|
+
input.sourceAgentId ?? null,
|
|
20
|
+
input.targetAgentId ?? null,
|
|
21
|
+
]);
|
|
22
|
+
return result.rows[0]?.id ?? '';
|
|
23
|
+
}
|
|
24
|
+
async getLinksForMemory(memoryId) {
|
|
25
|
+
const result = await this.pool.query(`SELECT * FROM memory_links
|
|
26
|
+
WHERE source_memory_id = $1 OR target_memory_id = $1
|
|
27
|
+
ORDER BY created_at DESC`, [memoryId]);
|
|
28
|
+
return result.rows;
|
|
29
|
+
}
|
|
30
|
+
async getLinksForMemories(memoryIds) {
|
|
31
|
+
if (memoryIds.length === 0)
|
|
32
|
+
return new Map();
|
|
33
|
+
const result = await this.pool.query(`SELECT * FROM memory_links
|
|
34
|
+
WHERE source_memory_id = ANY($1::uuid[]) OR target_memory_id = ANY($1::uuid[])
|
|
35
|
+
ORDER BY created_at DESC`, [memoryIds]);
|
|
36
|
+
const linksByMemory = new Map();
|
|
37
|
+
for (const row of result.rows) {
|
|
38
|
+
appendLink(linksByMemory, row.source_memory_id, row);
|
|
39
|
+
if (row.target_memory_id !== row.source_memory_id) {
|
|
40
|
+
appendLink(linksByMemory, row.target_memory_id, row);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return linksByMemory;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function appendLink(map, memoryId, link) {
|
|
47
|
+
const existing = map.get(memoryId);
|
|
48
|
+
if (existing) {
|
|
49
|
+
existing.push(link);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
map.set(memoryId, [link]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated Phase 5 — use domain-facing store interfaces from `stores.ts` instead.
|
|
3
|
+
*
|
|
4
|
+
* This facade remains for two reasons:
|
|
5
|
+
* 1. `getPool()` is still needed by PPR and deferred-audn (raw pool access).
|
|
6
|
+
* 2. External helper modules (iterative-retrieval, agentic-retrieval, query-expansion,
|
|
7
|
+
* etc.) still type their params as MemoryRepository. Those will be migrated
|
|
8
|
+
* file-by-file to accept SearchStore/EntityStore.
|
|
9
|
+
*
|
|
10
|
+
* Remove this file once all consumers use stores.* and pool is exposed directly.
|
|
11
|
+
*/
|
|
12
|
+
import pg from 'pg';
|
|
13
|
+
import type { AgentScope, CanonicalMemoryObjectLineage, StoreMemoryInput } from './repository-types.js';
|
|
14
|
+
import { type MemoryLink } from './repository-links.js';
|
|
15
|
+
import { type StoreAtomicFactInput, type StoreForesightInput } from './repository-representations.js';
|
|
16
|
+
export type { AgentScope, AtomicFactRow, CanonicalMemoryObjectRow, EpisodeRow, ForesightRow, MemoryMetadata, MemoryRow, SearchResult, WorkspaceContext, } from './repository-types.js';
|
|
17
|
+
export declare class MemoryRepository {
|
|
18
|
+
private pool;
|
|
19
|
+
/**
|
|
20
|
+
* Optional Phase-3 raw-content adapter, threaded through `deleteAll`
|
|
21
|
+
* so the wipe path also cleans up managed-blob bytes. `null` for
|
|
22
|
+
* pointer-only deployments and most tests; no fallback.
|
|
23
|
+
*/
|
|
24
|
+
private readonly rawContentStore;
|
|
25
|
+
/**
|
|
26
|
+
* Phase 4a per-row dispatch registry. When supplied, the `deleteAll`
|
|
27
|
+
* wipe path routes each blob to the adapter registered for its
|
|
28
|
+
* `storage_provider` — historical legacy rows (e.g. `local-fs://`
|
|
29
|
+
* on a Filecoin-active deployment) go to the registered legacy
|
|
30
|
+
* adapter. When absent, `repository-write.deleteAll` falls back to
|
|
31
|
+
* `singleStoreRegistry(rawContentStore)` (the pre-Phase-4a behavior)
|
|
32
|
+
* so existing test/back-compat constructions stay working.
|
|
33
|
+
*/
|
|
34
|
+
private readonly storeRegistry;
|
|
35
|
+
constructor(pool: pg.Pool, options?: {
|
|
36
|
+
rawContentStore?: import('../storage/raw-content-store.js').RawContentStore | null;
|
|
37
|
+
storeRegistry?: import('../storage/store-registry.js').RawContentStoreRegistry;
|
|
38
|
+
});
|
|
39
|
+
getPool(): import("pg").Pool;
|
|
40
|
+
storeEpisode(input: {
|
|
41
|
+
userId: string;
|
|
42
|
+
content: string;
|
|
43
|
+
sourceSite: string;
|
|
44
|
+
sourceUrl?: string;
|
|
45
|
+
sessionId?: string;
|
|
46
|
+
workspaceId?: string;
|
|
47
|
+
agentId?: string;
|
|
48
|
+
}): Promise<string>;
|
|
49
|
+
storeEpisodeWithClient(client: pg.PoolClient, input: {
|
|
50
|
+
userId: string;
|
|
51
|
+
content: string;
|
|
52
|
+
sourceSite: string;
|
|
53
|
+
sourceUrl?: string;
|
|
54
|
+
sessionId?: string;
|
|
55
|
+
workspaceId?: string;
|
|
56
|
+
agentId?: string;
|
|
57
|
+
}): Promise<string>;
|
|
58
|
+
storeCanonicalMemoryObject(input: {
|
|
59
|
+
userId: string;
|
|
60
|
+
objectFamily: 'ingested_fact';
|
|
61
|
+
payloadFormat?: string;
|
|
62
|
+
canonicalPayload: {
|
|
63
|
+
factText: string;
|
|
64
|
+
factType: string;
|
|
65
|
+
headline: string;
|
|
66
|
+
keywords: string[];
|
|
67
|
+
};
|
|
68
|
+
provenance: {
|
|
69
|
+
episodeId: string | null;
|
|
70
|
+
sourceSite: string;
|
|
71
|
+
sourceUrl: string;
|
|
72
|
+
};
|
|
73
|
+
observedAt?: Date;
|
|
74
|
+
lineage: CanonicalMemoryObjectLineage;
|
|
75
|
+
}): Promise<string>;
|
|
76
|
+
getEpisode(id: string): Promise<import("./repository-types.js").EpisodeRow | null>;
|
|
77
|
+
storeMemory(input: StoreMemoryInput): Promise<string>;
|
|
78
|
+
storeMemoryWithClient(client: pg.PoolClient, input: StoreMemoryInput): Promise<string>;
|
|
79
|
+
getMemory(id: string, userId?: string): Promise<import("./repository-types.js").MemoryRow | null>;
|
|
80
|
+
getMemoryIncludingDeleted(id: string, userId?: string): Promise<import("./repository-types.js").MemoryRow | null>;
|
|
81
|
+
getMemoryIncludingDeletedWithClient(client: pg.PoolClient, id: string, userId?: string): Promise<import("./repository-types.js").MemoryRow | null>;
|
|
82
|
+
listMemories(userId: string, limit?: number, offset?: number, sourceSite?: string, episodeId?: string): Promise<import("./repository-types.js").MemoryRow[]>;
|
|
83
|
+
/** Fetch the text and timestamp for all non-deleted memories for a user's
|
|
84
|
+
* conversation. memories.column is `content`, not `text` — alias to the
|
|
85
|
+
* caller's expected shape.
|
|
86
|
+
*
|
|
87
|
+
* BEAM/AMB note: AMB encodes BEAM conversation_id INTO user_id, so each
|
|
88
|
+
* ingest creates a fresh episode_id but conversation memories accumulate
|
|
89
|
+
* under the same user_id. Match on user_id only so the reflect worker sees
|
|
90
|
+
* ALL memories from the active conversation, not just the last ingest. */
|
|
91
|
+
findByConversation(userId: string, conversationId: string): Promise<Array<{
|
|
92
|
+
id: string;
|
|
93
|
+
text: string;
|
|
94
|
+
observedAt: Date;
|
|
95
|
+
}>>;
|
|
96
|
+
listMemoriesInWorkspace(workspaceId: string, limit?: number, offset?: number, callerAgentId?: string): Promise<import("./repository-types.js").MemoryRow[]>;
|
|
97
|
+
getMemoryInWorkspace(id: string, workspaceId: string, callerAgentId?: string): Promise<import("./repository-types.js").MemoryRow | null>;
|
|
98
|
+
softDeleteMemoryInWorkspace(id: string, workspaceId: string): Promise<void>;
|
|
99
|
+
getMemoryStats(userId: string): Promise<{
|
|
100
|
+
count: number;
|
|
101
|
+
avgImportance: number;
|
|
102
|
+
sourceDistribution: Record<string, number>;
|
|
103
|
+
}>;
|
|
104
|
+
findMemoriesByNamespace(userId: string, namespace: string, limit?: number): Promise<import("./repository-types.js").MemoryRow[]>;
|
|
105
|
+
searchSimilar(userId: string, queryEmbedding: number[], limit: number, sourceSite?: string, referenceTime?: Date): Promise<import("./repository-types.js").SearchResult[]>;
|
|
106
|
+
searchHybrid(userId: string, queryText: string, queryEmbedding: number[], limit: number, sourceSite?: string, referenceTime?: Date): Promise<import("./repository-types.js").SearchResult[]>;
|
|
107
|
+
searchKeyword(userId: string, queryText: string, limit: number, sourceSite?: string): Promise<import("./repository-types.js").SearchResult[]>;
|
|
108
|
+
searchAtomicFactsHybrid(userId: string, queryText: string, queryEmbedding: number[], limit: number, sourceSite?: string, referenceTime?: Date): Promise<import("./repository-types.js").SearchResult[]>;
|
|
109
|
+
findNearDuplicates(userId: string, embedding: number[], threshold: number, limit?: number): Promise<import("./repository-vector-search.js").CandidateRow[]>;
|
|
110
|
+
findNearDuplicatesWithClient(client: pg.PoolClient, userId: string, embedding: number[], threshold: number, limit?: number): Promise<import("./repository-vector-search.js").CandidateRow[]>;
|
|
111
|
+
findKeywordCandidates(userId: string, keywords: string[], limit?: number, includeExpired?: boolean): Promise<any[]>;
|
|
112
|
+
findKeywordCandidatesWithClient(client: pg.PoolClient, userId: string, keywords: string[], limit?: number): Promise<any[]>;
|
|
113
|
+
findTopicCandidates(userId: string, queryEmbedding: number[], limit: number): Promise<{
|
|
114
|
+
id: string;
|
|
115
|
+
content: string;
|
|
116
|
+
topic_abstraction: string;
|
|
117
|
+
importance: number;
|
|
118
|
+
similarity: number;
|
|
119
|
+
}[]>;
|
|
120
|
+
updateMemoryContent(userId: string, id: string, content: string, embedding: number[], importance: number, keywords?: string, trustScore?: number): Promise<void>;
|
|
121
|
+
updateMemoryContentWithClient(client: pg.PoolClient, userId: string, id: string, content: string, embedding: number[], importance: number, keywords?: string, trustScore?: number): Promise<void>;
|
|
122
|
+
updateMemoryMetadata(userId: string, id: string, metadata: Record<string, unknown>): Promise<void>;
|
|
123
|
+
softDeleteMemory(userId: string, id: string): Promise<void>;
|
|
124
|
+
softDeleteMemoryWithClient(client: pg.PoolClient, userId: string, id: string): Promise<void>;
|
|
125
|
+
/** Mark memory as temporally expired (contradicted/superseded). Preserved for temporal queries. */
|
|
126
|
+
expireMemory(userId: string, id: string): Promise<void>;
|
|
127
|
+
expireMemoryWithClient(client: pg.PoolClient, userId: string, id: string): Promise<void>;
|
|
128
|
+
touchMemory(id: string): Promise<void>;
|
|
129
|
+
countMemories(userId?: string): Promise<number>;
|
|
130
|
+
countNeedsClarification(userId: string): Promise<number>;
|
|
131
|
+
updateOpinionConfidence(userId: string, memoryId: string, newConfidence: number): Promise<void>;
|
|
132
|
+
backdateMemories(ids: string[], timestamp: Date): Promise<void>;
|
|
133
|
+
storeAtomicFacts(facts: StoreAtomicFactInput[]): Promise<string[]>;
|
|
134
|
+
storeForesight(entries: StoreForesightInput[]): Promise<string[]>;
|
|
135
|
+
replaceAtomicFactsForMemory(userId: string, parentMemoryId: string, facts: StoreAtomicFactInput[]): Promise<string[]>;
|
|
136
|
+
replaceForesightForMemory(userId: string, parentMemoryId: string, entries: StoreForesightInput[]): Promise<string[]>;
|
|
137
|
+
listAtomicFactsForMemory(userId: string, parentMemoryId: string): Promise<import("./repository-types.js").AtomicFactRow[]>;
|
|
138
|
+
listForesightForMemory(userId: string, parentMemoryId: string): Promise<import("./repository-types.js").ForesightRow[]>;
|
|
139
|
+
deleteBySource(userId: string, sourceSite: string): Promise<import("./repository-document-delete.js").DeleteBySourceResult>;
|
|
140
|
+
deleteAll(userId?: string): Promise<void>;
|
|
141
|
+
createLinks(links: MemoryLink[]): Promise<number>;
|
|
142
|
+
createLinksWithClient(client: pg.PoolClient, links: MemoryLink[]): Promise<number>;
|
|
143
|
+
findLinkCandidates(userId: string, embedding: number[], threshold: number, excludeId: string, limit?: number): Promise<{
|
|
144
|
+
id: string;
|
|
145
|
+
similarity: number;
|
|
146
|
+
}[]>;
|
|
147
|
+
findLinkCandidatesWithClient(client: pg.PoolClient, userId: string, embedding: number[], threshold: number, excludeId: string, limit?: number): Promise<{
|
|
148
|
+
id: string;
|
|
149
|
+
similarity: number;
|
|
150
|
+
}[]>;
|
|
151
|
+
findLinkedMemoryIds(memoryIds: string[], excludeIds: Set<string>, limit: number): Promise<string[]>;
|
|
152
|
+
fetchMemoriesByIds(userId: string, ids: string[], queryEmbedding: number[], referenceTime?: Date, includeExpired?: boolean): Promise<import("./repository-types.js").SearchResult[]>;
|
|
153
|
+
findTemporalNeighbors(userId: string, anchorTimestamps: Date[], queryEmbedding: number[], windowMinutes: number, excludeIds: Set<string>, limit: number, referenceTime?: Date): Promise<import("./repository-types.js").SearchResult[]>;
|
|
154
|
+
countLinks(): Promise<number>;
|
|
155
|
+
/**
|
|
156
|
+
* Workspace-scoped vector search with agent filtering and visibility enforcement.
|
|
157
|
+
*/
|
|
158
|
+
searchSimilarInWorkspace(workspaceId: string, queryEmbedding: number[], limit: number, agentScope?: AgentScope, callerAgentId?: string, referenceTime?: Date): Promise<import("./repository-types.js").SearchResult[]>;
|
|
159
|
+
/**
|
|
160
|
+
* Find near-duplicate memories within a workspace for AUDN conflict detection.
|
|
161
|
+
*/
|
|
162
|
+
findNearDuplicatesInWorkspace(workspaceId: string, embedding: number[], threshold: number, limit?: number, agentScope?: AgentScope, callerAgentId?: string): Promise<import("./repository-vector-search.js").CandidateRow[]>;
|
|
163
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated Phase 5 — use domain-facing store interfaces from `stores.ts` instead.
|
|
3
|
+
*
|
|
4
|
+
* This facade remains for two reasons:
|
|
5
|
+
* 1. `getPool()` is still needed by PPR and deferred-audn (raw pool access).
|
|
6
|
+
* 2. External helper modules (iterative-retrieval, agentic-retrieval, query-expansion,
|
|
7
|
+
* etc.) still type their params as MemoryRepository. Those will be migrated
|
|
8
|
+
* file-by-file to accept SearchStore/EntityStore.
|
|
9
|
+
*
|
|
10
|
+
* Remove this file once all consumers use stores.* and pool is exposed directly.
|
|
11
|
+
*/
|
|
12
|
+
import { countMemories, countNeedsClarification, findKeywordCandidates, findNearDuplicates, findNearDuplicatesInWorkspace, findTemporalNeighbors, findTopicCandidates, getEpisode, getMemory, getMemoryWithClient, getMemoryStats, listMemories, listMemoriesInWorkspace, getMemoryInWorkspace, searchHybridSimilar, searchKeywordSimilar, searchSimilar, searchSimilarInWorkspace, } from './repository-read.js';
|
|
13
|
+
import { backdateMemories, deleteAll, expireMemory, expireMemoryWithClient, softDeleteMemory, softDeleteMemoryInWorkspace, softDeleteMemoryWithClient, storeCanonicalMemoryObject, storeEpisode, storeEpisodeWithClient, storeMemory, storeMemoryWithClient, touchMemory, updateMemoryContent, updateMemoryContentWithClient, updateMemoryMetadata, updateOpinionConfidence, } from './repository-write.js';
|
|
14
|
+
import { deleteBySource } from './repository-document-delete.js';
|
|
15
|
+
import { createLinks, countLinks, fetchMemoriesByIds, findLinkCandidates, findLinkCandidatesWithClient, findLinkedMemoryIds, } from './repository-links.js';
|
|
16
|
+
import { listAtomicFactsForMemory, listForesightForMemory, replaceAtomicFactsForMemory, replaceForesightForMemory, searchAtomicFactsHybrid, storeAtomicFacts, storeForesight, } from './repository-representations.js';
|
|
17
|
+
export class MemoryRepository {
|
|
18
|
+
pool;
|
|
19
|
+
/**
|
|
20
|
+
* Optional Phase-3 raw-content adapter, threaded through `deleteAll`
|
|
21
|
+
* so the wipe path also cleans up managed-blob bytes. `null` for
|
|
22
|
+
* pointer-only deployments and most tests; no fallback.
|
|
23
|
+
*/
|
|
24
|
+
rawContentStore;
|
|
25
|
+
/**
|
|
26
|
+
* Phase 4a per-row dispatch registry. When supplied, the `deleteAll`
|
|
27
|
+
* wipe path routes each blob to the adapter registered for its
|
|
28
|
+
* `storage_provider` — historical legacy rows (e.g. `local-fs://`
|
|
29
|
+
* on a Filecoin-active deployment) go to the registered legacy
|
|
30
|
+
* adapter. When absent, `repository-write.deleteAll` falls back to
|
|
31
|
+
* `singleStoreRegistry(rawContentStore)` (the pre-Phase-4a behavior)
|
|
32
|
+
* so existing test/back-compat constructions stay working.
|
|
33
|
+
*/
|
|
34
|
+
storeRegistry;
|
|
35
|
+
constructor(pool, options = {}) {
|
|
36
|
+
this.pool = pool;
|
|
37
|
+
this.rawContentStore = options.rawContentStore ?? null;
|
|
38
|
+
this.storeRegistry = options.storeRegistry;
|
|
39
|
+
}
|
|
40
|
+
getPool() {
|
|
41
|
+
return this.pool;
|
|
42
|
+
}
|
|
43
|
+
async storeEpisode(input) {
|
|
44
|
+
return storeEpisode(this.pool, input);
|
|
45
|
+
}
|
|
46
|
+
async storeEpisodeWithClient(client, input) {
|
|
47
|
+
return storeEpisodeWithClient(client, input);
|
|
48
|
+
}
|
|
49
|
+
async storeCanonicalMemoryObject(input) {
|
|
50
|
+
return storeCanonicalMemoryObject(this.pool, input);
|
|
51
|
+
}
|
|
52
|
+
async getEpisode(id) {
|
|
53
|
+
return getEpisode(this.pool, id);
|
|
54
|
+
}
|
|
55
|
+
async storeMemory(input) {
|
|
56
|
+
return storeMemory(this.pool, input);
|
|
57
|
+
}
|
|
58
|
+
async storeMemoryWithClient(client, input) {
|
|
59
|
+
return storeMemoryWithClient(client, input);
|
|
60
|
+
}
|
|
61
|
+
async getMemory(id, userId) {
|
|
62
|
+
return getMemory(this.pool, id, userId, false);
|
|
63
|
+
}
|
|
64
|
+
async getMemoryIncludingDeleted(id, userId) {
|
|
65
|
+
return getMemory(this.pool, id, userId, true);
|
|
66
|
+
}
|
|
67
|
+
async getMemoryIncludingDeletedWithClient(client, id, userId) {
|
|
68
|
+
return getMemoryWithClient(client, id, userId, true);
|
|
69
|
+
}
|
|
70
|
+
async listMemories(userId, limit = 20, offset = 0, sourceSite, episodeId) {
|
|
71
|
+
return listMemories(this.pool, userId, limit, offset, sourceSite, episodeId);
|
|
72
|
+
}
|
|
73
|
+
/** Fetch the text and timestamp for all non-deleted memories for a user's
|
|
74
|
+
* conversation. memories.column is `content`, not `text` — alias to the
|
|
75
|
+
* caller's expected shape.
|
|
76
|
+
*
|
|
77
|
+
* BEAM/AMB note: AMB encodes BEAM conversation_id INTO user_id, so each
|
|
78
|
+
* ingest creates a fresh episode_id but conversation memories accumulate
|
|
79
|
+
* under the same user_id. Match on user_id only so the reflect worker sees
|
|
80
|
+
* ALL memories from the active conversation, not just the last ingest. */
|
|
81
|
+
async findByConversation(userId, conversationId) {
|
|
82
|
+
void conversationId;
|
|
83
|
+
const { rows } = await this.pool.query(`SELECT id, content, observed_at
|
|
84
|
+
FROM memories
|
|
85
|
+
WHERE user_id = $1 AND deleted_at IS NULL
|
|
86
|
+
ORDER BY observed_at ASC`, [userId]);
|
|
87
|
+
return rows.map(r => ({ id: r.id, text: r.content, observedAt: r.observed_at }));
|
|
88
|
+
}
|
|
89
|
+
async listMemoriesInWorkspace(workspaceId, limit = 20, offset = 0, callerAgentId) {
|
|
90
|
+
return listMemoriesInWorkspace(this.pool, workspaceId, limit, offset, callerAgentId);
|
|
91
|
+
}
|
|
92
|
+
async getMemoryInWorkspace(id, workspaceId, callerAgentId) {
|
|
93
|
+
return getMemoryInWorkspace(this.pool, id, workspaceId, callerAgentId);
|
|
94
|
+
}
|
|
95
|
+
async softDeleteMemoryInWorkspace(id, workspaceId) {
|
|
96
|
+
return softDeleteMemoryInWorkspace(this.pool, id, workspaceId);
|
|
97
|
+
}
|
|
98
|
+
async getMemoryStats(userId) {
|
|
99
|
+
return getMemoryStats(this.pool, userId);
|
|
100
|
+
}
|
|
101
|
+
async findMemoriesByNamespace(userId, namespace, limit = 20) {
|
|
102
|
+
const { listMemoriesByNamespace } = await import('./repository-read.js');
|
|
103
|
+
return listMemoriesByNamespace(this.pool, userId, namespace, limit);
|
|
104
|
+
}
|
|
105
|
+
async searchSimilar(userId, queryEmbedding, limit, sourceSite, referenceTime) {
|
|
106
|
+
return searchSimilar(this.pool, userId, queryEmbedding, limit, sourceSite, referenceTime);
|
|
107
|
+
}
|
|
108
|
+
async searchHybrid(userId, queryText, queryEmbedding, limit, sourceSite, referenceTime) {
|
|
109
|
+
return searchHybridSimilar(this.pool, userId, queryText, queryEmbedding, limit, sourceSite, referenceTime);
|
|
110
|
+
}
|
|
111
|
+
async searchKeyword(userId, queryText, limit, sourceSite) {
|
|
112
|
+
return searchKeywordSimilar(this.pool, userId, queryText, limit, sourceSite);
|
|
113
|
+
}
|
|
114
|
+
async searchAtomicFactsHybrid(userId, queryText, queryEmbedding, limit, sourceSite, referenceTime) {
|
|
115
|
+
return searchAtomicFactsHybrid(this.pool, userId, queryText, queryEmbedding, limit, sourceSite, referenceTime);
|
|
116
|
+
}
|
|
117
|
+
async findNearDuplicates(userId, embedding, threshold, limit = 3) {
|
|
118
|
+
return findNearDuplicates(this.pool, userId, embedding, threshold, limit);
|
|
119
|
+
}
|
|
120
|
+
async findNearDuplicatesWithClient(client, userId, embedding, threshold, limit = 3) {
|
|
121
|
+
return findNearDuplicates(client, userId, embedding, threshold, limit);
|
|
122
|
+
}
|
|
123
|
+
async findKeywordCandidates(userId, keywords, limit = 5, includeExpired = false) {
|
|
124
|
+
return findKeywordCandidates(this.pool, userId, keywords, limit, includeExpired);
|
|
125
|
+
}
|
|
126
|
+
async findKeywordCandidatesWithClient(client, userId, keywords, limit = 5) {
|
|
127
|
+
return findKeywordCandidates(client, userId, keywords, limit);
|
|
128
|
+
}
|
|
129
|
+
async findTopicCandidates(userId, queryEmbedding, limit) {
|
|
130
|
+
return findTopicCandidates(this.pool, userId, queryEmbedding, limit);
|
|
131
|
+
}
|
|
132
|
+
async updateMemoryContent(userId, id, content, embedding, importance, keywords, trustScore) {
|
|
133
|
+
return updateMemoryContent(this.pool, userId, id, content, embedding, importance, keywords, trustScore);
|
|
134
|
+
}
|
|
135
|
+
async updateMemoryContentWithClient(client, userId, id, content, embedding, importance, keywords, trustScore) {
|
|
136
|
+
return updateMemoryContentWithClient(client, userId, id, content, embedding, importance, keywords, trustScore);
|
|
137
|
+
}
|
|
138
|
+
async updateMemoryMetadata(userId, id, metadata) {
|
|
139
|
+
return updateMemoryMetadata(this.pool, userId, id, metadata);
|
|
140
|
+
}
|
|
141
|
+
async softDeleteMemory(userId, id) {
|
|
142
|
+
return softDeleteMemory(this.pool, userId, id);
|
|
143
|
+
}
|
|
144
|
+
async softDeleteMemoryWithClient(client, userId, id) {
|
|
145
|
+
return softDeleteMemoryWithClient(client, userId, id);
|
|
146
|
+
}
|
|
147
|
+
/** Mark memory as temporally expired (contradicted/superseded). Preserved for temporal queries. */
|
|
148
|
+
async expireMemory(userId, id) {
|
|
149
|
+
return expireMemory(this.pool, userId, id);
|
|
150
|
+
}
|
|
151
|
+
async expireMemoryWithClient(client, userId, id) {
|
|
152
|
+
return expireMemoryWithClient(client, userId, id);
|
|
153
|
+
}
|
|
154
|
+
async touchMemory(id) {
|
|
155
|
+
return touchMemory(this.pool, id);
|
|
156
|
+
}
|
|
157
|
+
async countMemories(userId) {
|
|
158
|
+
return countMemories(this.pool, userId);
|
|
159
|
+
}
|
|
160
|
+
async countNeedsClarification(userId) {
|
|
161
|
+
return countNeedsClarification(this.pool, userId);
|
|
162
|
+
}
|
|
163
|
+
async updateOpinionConfidence(userId, memoryId, newConfidence) {
|
|
164
|
+
return updateOpinionConfidence(this.pool, userId, memoryId, newConfidence);
|
|
165
|
+
}
|
|
166
|
+
async backdateMemories(ids, timestamp) {
|
|
167
|
+
return backdateMemories(this.pool, ids, timestamp);
|
|
168
|
+
}
|
|
169
|
+
async storeAtomicFacts(facts) {
|
|
170
|
+
return storeAtomicFacts(this.pool, facts);
|
|
171
|
+
}
|
|
172
|
+
async storeForesight(entries) {
|
|
173
|
+
return storeForesight(this.pool, entries);
|
|
174
|
+
}
|
|
175
|
+
async replaceAtomicFactsForMemory(userId, parentMemoryId, facts) {
|
|
176
|
+
return replaceAtomicFactsForMemory(this.pool, userId, parentMemoryId, facts);
|
|
177
|
+
}
|
|
178
|
+
async replaceForesightForMemory(userId, parentMemoryId, entries) {
|
|
179
|
+
return replaceForesightForMemory(this.pool, userId, parentMemoryId, entries);
|
|
180
|
+
}
|
|
181
|
+
async listAtomicFactsForMemory(userId, parentMemoryId) {
|
|
182
|
+
return listAtomicFactsForMemory(this.pool, userId, parentMemoryId);
|
|
183
|
+
}
|
|
184
|
+
async listForesightForMemory(userId, parentMemoryId) {
|
|
185
|
+
return listForesightForMemory(this.pool, userId, parentMemoryId);
|
|
186
|
+
}
|
|
187
|
+
async deleteBySource(userId, sourceSite) {
|
|
188
|
+
return deleteBySource(this.pool, userId, sourceSite);
|
|
189
|
+
}
|
|
190
|
+
async deleteAll(userId) {
|
|
191
|
+
return deleteAll(this.pool, userId, {
|
|
192
|
+
rawContentStore: this.rawContentStore,
|
|
193
|
+
storeRegistry: this.storeRegistry,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
async createLinks(links) {
|
|
197
|
+
return createLinks(this.pool, links);
|
|
198
|
+
}
|
|
199
|
+
async createLinksWithClient(client, links) {
|
|
200
|
+
return createLinks(client, links);
|
|
201
|
+
}
|
|
202
|
+
async findLinkCandidates(userId, embedding, threshold, excludeId, limit = 10) {
|
|
203
|
+
return findLinkCandidates(this.pool, userId, embedding, threshold, excludeId, limit);
|
|
204
|
+
}
|
|
205
|
+
async findLinkCandidatesWithClient(client, userId, embedding, threshold, excludeId, limit = 10) {
|
|
206
|
+
return findLinkCandidatesWithClient(client, userId, embedding, threshold, excludeId, limit);
|
|
207
|
+
}
|
|
208
|
+
async findLinkedMemoryIds(memoryIds, excludeIds, limit) {
|
|
209
|
+
return findLinkedMemoryIds(this.pool, memoryIds, excludeIds, limit);
|
|
210
|
+
}
|
|
211
|
+
async fetchMemoriesByIds(userId, ids, queryEmbedding, referenceTime, includeExpired = false) {
|
|
212
|
+
return fetchMemoriesByIds(this.pool, userId, ids, queryEmbedding, referenceTime, includeExpired);
|
|
213
|
+
}
|
|
214
|
+
async findTemporalNeighbors(userId, anchorTimestamps, queryEmbedding, windowMinutes, excludeIds, limit, referenceTime) {
|
|
215
|
+
return findTemporalNeighbors(this.pool, userId, anchorTimestamps, queryEmbedding, windowMinutes, excludeIds, limit, referenceTime);
|
|
216
|
+
}
|
|
217
|
+
async countLinks() {
|
|
218
|
+
return countLinks(this.pool);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Workspace-scoped vector search with agent filtering and visibility enforcement.
|
|
222
|
+
*/
|
|
223
|
+
async searchSimilarInWorkspace(workspaceId, queryEmbedding, limit, agentScope = 'all', callerAgentId, referenceTime) {
|
|
224
|
+
return searchSimilarInWorkspace(this.pool, workspaceId, queryEmbedding, limit, agentScope, callerAgentId, referenceTime);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Find near-duplicate memories within a workspace for AUDN conflict detection.
|
|
228
|
+
*/
|
|
229
|
+
async findNearDuplicatesInWorkspace(workspaceId, embedding, threshold, limit = 3, agentScope = 'all', callerAgentId) {
|
|
230
|
+
return findNearDuplicatesInWorkspace(this.pool, workspaceId, embedding, threshold, limit, agentScope, callerAgentId);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run schema migration against the configured database.
|
|
3
|
+
* Replaces {{EMBEDDING_DIMENSIONS}} in schema.sql with the configured value.
|
|
4
|
+
* Usage: pnpm migrate (uses .env) or pnpm migrate:test (uses .env.test)
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
import { resolve, dirname } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { pool } from './pool.js';
|
|
10
|
+
import { config } from '../config.js';
|
|
11
|
+
import { resolveEmbeddingDimensions } from '../services/embedding.js';
|
|
12
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
function stripVectorIndexes(sql) {
|
|
14
|
+
if (!config.skipVectorIndexes)
|
|
15
|
+
return sql;
|
|
16
|
+
// Matches both CREATE INDEX and CREATE INDEX IF NOT EXISTS forms — schema.sql
|
|
17
|
+
// uses the idempotent form so it can re-run on every startup without data loss.
|
|
18
|
+
return sql.replace(/CREATE INDEX (IF NOT EXISTS )?idx_[a-z_]+_embedding ON [a-z_]+\n USING hnsw \(embedding vector_cosine_ops\)\n WITH \(m = 16, ef_construction = 200\);(\n\n|\n?$)/g, '');
|
|
19
|
+
}
|
|
20
|
+
async function migrate() {
|
|
21
|
+
const envDims = process.env.EMBEDDING_DIMENSIONS ? parseInt(process.env.EMBEDDING_DIMENSIONS, 10) : null;
|
|
22
|
+
const embeddingDimensions = envDims ?? await resolveEmbeddingDimensions();
|
|
23
|
+
console.log(`[migrate] Resolved dimensions: ${embeddingDimensions} (from env: ${envDims})`);
|
|
24
|
+
const schemaPath = resolve(__dirname, 'schema.sql');
|
|
25
|
+
const rawSql = readFileSync(schemaPath, 'utf-8');
|
|
26
|
+
const dimensionedSql = rawSql.replace(/\{\{EMBEDDING_DIMENSIONS\}\}/g, String(embeddingDimensions));
|
|
27
|
+
const sql = stripVectorIndexes(dimensionedSql);
|
|
28
|
+
console.log(`Running migration (embedding dimensions: ${embeddingDimensions}, vector indexes: ${config.skipVectorIndexes ? 'off' : 'on'})...`);
|
|
29
|
+
await pool.query(sql);
|
|
30
|
+
console.log('Migration complete.');
|
|
31
|
+
await pool.end();
|
|
32
|
+
}
|
|
33
|
+
migrate().catch((err) => {
|
|
34
|
+
console.error('Migration failed:', err);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
});
|
package/dist/db/mmr.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maximal Marginal Relevance (MMR) reranking.
|
|
3
|
+
*
|
|
4
|
+
* Greedily selects results that balance relevance to the query against
|
|
5
|
+
* redundancy with already-selected results. This improves diversity in
|
|
6
|
+
* the top-K, which is critical for multi-hop queries that need different
|
|
7
|
+
* types of facts to compose a complete answer.
|
|
8
|
+
*
|
|
9
|
+
* score_mmr(d) = lambda * score(d) - (1 - lambda) * max(sim(d, selected_i))
|
|
10
|
+
*
|
|
11
|
+
* Source: Carbonell & Goldstein (1998), Phase 2 roadmap item.
|
|
12
|
+
*/
|
|
13
|
+
import type { SearchResult } from './repository-types.js';
|
|
14
|
+
export declare function applyMMR(candidates: SearchResult[], queryEmbedding: number[], limit: number, lambda: number): SearchResult[];
|
package/dist/db/mmr.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maximal Marginal Relevance (MMR) reranking.
|
|
3
|
+
*
|
|
4
|
+
* Greedily selects results that balance relevance to the query against
|
|
5
|
+
* redundancy with already-selected results. This improves diversity in
|
|
6
|
+
* the top-K, which is critical for multi-hop queries that need different
|
|
7
|
+
* types of facts to compose a complete answer.
|
|
8
|
+
*
|
|
9
|
+
* score_mmr(d) = lambda * score(d) - (1 - lambda) * max(sim(d, selected_i))
|
|
10
|
+
*
|
|
11
|
+
* Source: Carbonell & Goldstein (1998), Phase 2 roadmap item.
|
|
12
|
+
*/
|
|
13
|
+
import { cosineSimilarity } from '../vector-math.js';
|
|
14
|
+
export function applyMMR(candidates, queryEmbedding, limit, lambda) {
|
|
15
|
+
if (candidates.length <= 1)
|
|
16
|
+
return candidates.slice(0, limit);
|
|
17
|
+
const selected = [];
|
|
18
|
+
const remaining = new Set(candidates.map((_, i) => i));
|
|
19
|
+
const bestIndex = selectHighestScore(candidates);
|
|
20
|
+
selected.push(candidates[bestIndex]);
|
|
21
|
+
remaining.delete(bestIndex);
|
|
22
|
+
while (selected.length < limit && remaining.size > 0) {
|
|
23
|
+
const bestCandidate = selectBestMmrCandidate(candidates, selected, remaining, lambda);
|
|
24
|
+
if (bestCandidate === -1)
|
|
25
|
+
break;
|
|
26
|
+
selected.push(candidates[bestCandidate]);
|
|
27
|
+
remaining.delete(bestCandidate);
|
|
28
|
+
}
|
|
29
|
+
return selected;
|
|
30
|
+
}
|
|
31
|
+
/** Find the index of the candidate with the highest relevance score. */
|
|
32
|
+
function selectHighestScore(candidates) {
|
|
33
|
+
return candidates.reduce((best, current, i) => (current.score > candidates[best].score ? i : best), 0);
|
|
34
|
+
}
|
|
35
|
+
/** Select the remaining candidate with the best MMR score, or -1 if none. */
|
|
36
|
+
function selectBestMmrCandidate(candidates, selected, remaining, lambda) {
|
|
37
|
+
let bestMmrScore = -Infinity;
|
|
38
|
+
let bestCandidate = -1;
|
|
39
|
+
for (const candidateIndex of remaining) {
|
|
40
|
+
const mmrScore = computeMmrScore(candidates[candidateIndex], selected, lambda);
|
|
41
|
+
if (mmrScore > bestMmrScore) {
|
|
42
|
+
bestMmrScore = mmrScore;
|
|
43
|
+
bestCandidate = candidateIndex;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return bestCandidate;
|
|
47
|
+
}
|
|
48
|
+
/** Compute MMR score: balance relevance against redundancy with selected set. */
|
|
49
|
+
function computeMmrScore(candidate, selected, lambda) {
|
|
50
|
+
let maxRedundancy = 0;
|
|
51
|
+
for (const selectedResult of selected) {
|
|
52
|
+
const redundancy = cosineSimilarity(candidate.embedding, selectedResult.embedding);
|
|
53
|
+
if (redundancy > maxRedundancy)
|
|
54
|
+
maxRedundancy = redundancy;
|
|
55
|
+
}
|
|
56
|
+
return lambda * candidate.score - (1 - lambda) * maxRedundancy;
|
|
57
|
+
}
|