@zukhruf/retrieval 0.0.0-stage → 0.8.77

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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +467 -2
  3. package/dist/batch-retriever.d.ts +9 -0
  4. package/dist/batch-retriever.d.ts.map +1 -0
  5. package/dist/batch-retriever.js +2 -0
  6. package/dist/batch-retriever.js.map +1 -0
  7. package/dist/filter.d.ts +13 -0
  8. package/dist/filter.d.ts.map +1 -0
  9. package/dist/filter.js +2 -0
  10. package/dist/filter.js.map +1 -0
  11. package/dist/fusion.d.ts +7 -0
  12. package/dist/fusion.d.ts.map +1 -0
  13. package/dist/fusion.js +2 -0
  14. package/dist/fusion.js.map +1 -0
  15. package/dist/index.d.ts +12 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +3 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/model-mismatch-error.d.ts +18 -0
  20. package/dist/model-mismatch-error.d.ts.map +1 -0
  21. package/dist/model-mismatch-error.js +18 -0
  22. package/dist/model-mismatch-error.js.map +1 -0
  23. package/dist/passage-store.d.ts +12 -0
  24. package/dist/passage-store.d.ts.map +1 -0
  25. package/dist/passage-store.js +2 -0
  26. package/dist/passage-store.js.map +1 -0
  27. package/dist/passage.d.ts +13 -0
  28. package/dist/passage.d.ts.map +1 -0
  29. package/dist/passage.js +2 -0
  30. package/dist/passage.js.map +1 -0
  31. package/dist/postprocessor.d.ts +11 -0
  32. package/dist/postprocessor.d.ts.map +1 -0
  33. package/dist/postprocessor.js +2 -0
  34. package/dist/postprocessor.js.map +1 -0
  35. package/dist/retrieve-options.d.ts +10 -0
  36. package/dist/retrieve-options.d.ts.map +1 -0
  37. package/dist/retrieve-options.js +2 -0
  38. package/dist/retrieve-options.js.map +1 -0
  39. package/dist/retriever.d.ts +10 -0
  40. package/dist/retriever.d.ts.map +1 -0
  41. package/dist/retriever.js +2 -0
  42. package/dist/retriever.js.map +1 -0
  43. package/dist/scored-passage.d.ts +9 -0
  44. package/dist/scored-passage.d.ts.map +1 -0
  45. package/dist/scored-passage.js +2 -0
  46. package/dist/scored-passage.js.map +1 -0
  47. package/dist/vector-missing-error.d.ts +10 -0
  48. package/dist/vector-missing-error.d.ts.map +1 -0
  49. package/dist/vector-missing-error.js +13 -0
  50. package/dist/vector-missing-error.js.map +1 -0
  51. package/package.json +45 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ezzabuzaid
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,468 @@
1
- # Temporary Holding Version
1
+ # @zukhruf/retrieval
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > Design on paper, for review: no code exists yet. Ezz approved the API on 2026-11-23 ("Approve, write the paper"). He ruled on each ADR the same day; all are accepted (backlog #5399). Each example below shows the planned API.
4
+
5
+ This package is the query side of retrieval-augmented generation (RAG). A query goes in. The best passages come out, best first, each with a score that is higher for a better passage. The package never cuts or embeds documents: elt writes the passages, their vectors with the model identity, and a keyword index ([ADR 0001](./docs/adr/0001-the-package-is-the-query-side-of-rag-and-reads-a-passage-contract-that-elt-writes.md)).
6
+
7
+ The port of the package is `Retriever`. The engine retrievers read that passage contract with the SQL of SQLite, Postgres or SQL Server. A host can implement `Retriever` over any other source, such as a web search. Composite retrievers fuse retrievers or rewrite the query, and postprocessors rerank, cut, diversify, group, reorder and widen the result.
8
+
9
+ The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
10
+
11
+ ## Use it
12
+
13
+ ```sh
14
+ npm install @zukhruf/retrieval @zukhruf/vectors ai @ai-sdk/openai
15
+ ```
16
+
17
+ ```ts
18
+ // Planned API.
19
+ import { DatabaseSync } from 'node:sqlite';
20
+
21
+ import { openai } from '@ai-sdk/openai';
22
+
23
+ import { SqliteEmbeddingRetriever } from '@zukhruf/retrieval/sqlite';
24
+ import { install } from '@zukhruf/vectors/sqlite';
25
+
26
+ const database = new DatabaseSync('knowledge.db'); // elt wrote the passage contract here
27
+ install(database); // or load sqlite-vec: the SQL is the same
28
+
29
+ const retriever = new SqliteEmbeddingRetriever({
30
+ database,
31
+ passages: 'docs_passages',
32
+ vectors: 'docs_passage_vectors',
33
+ model: openai.embedding('text-embedding-3-small'),
34
+ });
35
+
36
+ const results = await retriever.retrieve('how do I rotate a key?');
37
+ // 10 scored passages, best first: { passage: { id, text, source, seq, start, end, metadata }, score }
38
+ ```
39
+
40
+ ## Search passages by meaning
41
+
42
+ An embedding retriever embeds the query with its AI SDK model and sends one SQL query to the engine. The engine ranks the passages by cosine similarity: `vec_distance_cosine` on SQLite, pgvector's `<=>` on Postgres, `VECTOR_DISTANCE` or elt's `marts.<name>_nearest` on SQL Server ([ADR 0003](./docs/adr/0003-an-embedding-retriever-runs-the-vector-search-in-the-sql-of-its-engine-one-query-or-a-batch.md)). The score is `1 - cosine distance`.
43
+
44
+ ```ts
45
+ // Planned API.
46
+ import { PgvectorEmbeddingRetriever } from '@zukhruf/retrieval/postgres';
47
+
48
+ const retriever = new PgvectorEmbeddingRetriever({
49
+ session: {
50
+ query: (sql, parameters) => pool.query(sql, parameters).then((r) => r.rows),
51
+ },
52
+ passages: 'docs_passages',
53
+ vectors: 'docs_passage_vectors',
54
+ model,
55
+ });
56
+
57
+ const results = await retriever.retrieve('rotate a key', {
58
+ topK: 5,
59
+ abortSignal,
60
+ });
61
+ ```
62
+
63
+ The retriever reads the model identity of the stored vectors first. When its query model has another identity, the call fails with `ModelMismatchError` and sends no search ([ADR 0010](./docs/adr/0010-an-embedding-retriever-refuses-a-query-model-whose-identity-differs-from-the-stored-vectors.md)).
64
+
65
+ ## Search by keywords
66
+
67
+ A keyword retriever queries the keyword index that elt writes: FTS5 on SQLite, `tsvector` on Postgres, full-text on SQL Server. It quotes each word of the query, so a word of the user is never an operator of the engine. Its score is higher for a better match on every engine; `SqliteBM25Retriever` negates `bm25()` ([ADR 0004](./docs/adr/0004-a-keyword-retriever-queries-the-keyword-index-that-elt-writes.md)).
68
+
69
+ ```ts
70
+ // Planned API.
71
+ import { SqliteBM25Retriever } from '@zukhruf/retrieval/sqlite';
72
+
73
+ const keywords = new SqliteBM25Retriever({
74
+ database,
75
+ passages: 'docs_passages',
76
+ index: 'docs_passages_fts',
77
+ operator: 'OR', // the default is 'AND', the implicit operator of FTS5
78
+ });
79
+
80
+ const results = await keywords.retrieve('rotate signing key');
81
+ ```
82
+
83
+ ## Combine searches
84
+
85
+ `FusionRetriever` asks each retriever for `rankWindowSize` passages, fuses the lists on `Passage.id` and gives the best `topK`. The default fusion is reciprocal rank fusion with k 60 and ranks from 1; each retriever has a weight of 1 ([ADR 0007](./docs/adr/0007-reciprocal-rank-fusion-is-the-default-fusion-with-k-60-and-ranks-from-1.md)).
86
+
87
+ ```ts
88
+ // Planned API.
89
+ import {
90
+ FusionRetriever,
91
+ MultiQueryRetriever,
92
+ relativeScoreFusion,
93
+ } from '@zukhruf/retrieval';
94
+
95
+ const hybrid = new FusionRetriever({
96
+ retrievers: [meaning, keywords],
97
+ rankWindowSize: 50,
98
+ });
99
+
100
+ const minMax = new FusionRetriever({
101
+ retrievers: [meaning, keywords],
102
+ fusion: relativeScoreFusion(),
103
+ weights: [0.75, 0.25],
104
+ });
105
+
106
+ // The language model writes 4 versions of the query; reciprocalRankFusion() fuses their lists, as in FusionRetriever.
107
+ const expanded = new MultiQueryRetriever({
108
+ retriever: hybrid,
109
+ model: languageModel,
110
+ });
111
+ ```
112
+
113
+ `HydeRetriever { retriever, model }` asks the language model for an answer and searches with that answer as the query.
114
+
115
+ ## Rerank and post-process the results
116
+
117
+ `PostprocessingRetriever` runs a list of postprocessors, in order, on the result of its retriever ([ADR 0008](./docs/adr/0008-a-search-is-composed-of-retrievers-that-wrap-retrievers-and-a-list-of-postprocessors.md)).
118
+
119
+ ```ts
120
+ // Planned API.
121
+ import {
122
+ Collapse,
123
+ ContextWindow,
124
+ LongContextReorder,
125
+ MaximalMarginalRelevance,
126
+ PostprocessingRetriever,
127
+ Rerank,
128
+ SimilarityCutoff,
129
+ } from '@zukhruf/retrieval';
130
+ import { SqlitePassageStore } from '@zukhruf/retrieval/sqlite';
131
+
132
+ const search = new PostprocessingRetriever({
133
+ retriever: hybrid,
134
+ postprocessors: [
135
+ new Rerank({ model: rerankingModel, topN: 20 }), // AI SDK rerank
136
+ new SimilarityCutoff({ threshold: 0.3 }),
137
+ new MaximalMarginalRelevance({ lambda: 0.5, topK: 8 }), // lambda 1 = relevance only
138
+ new Collapse({ field: 'source', size: 1 }), // one passage per source
139
+ new ContextWindow({
140
+ store: new SqlitePassageStore({ database, passages: 'docs_passages' }),
141
+ windowSize: 3,
142
+ }),
143
+ new LongContextReorder(),
144
+ ],
145
+ });
146
+
147
+ const results = await search.retrieve('rotate a key', {
148
+ topK: 50,
149
+ includeVectors: true,
150
+ });
151
+ ```
152
+
153
+ `MaximalMarginalRelevance` compares the vectors of the passages, so its retriever runs with `includeVectors: true`.
154
+
155
+ ## Search many queries at once
156
+
157
+ Batch search is part of vector search. Each embedding retriever has `retrieveMany` beside `retrieve`. It embeds the queries in one model call and runs one SQL query: `CROSS JOIN LATERAL` on Postgres. The SQL Server form, `CROSS APPLY` over `VECTOR_SEARCH`, is to be probed, and the SQLite form waits on a cost probe (#5422) ([ADR 0003](./docs/adr/0003-an-embedding-retriever-runs-the-vector-search-in-the-sql-of-its-engine-one-query-or-a-batch.md)).
158
+
159
+ ```ts
160
+ // Planned API.
161
+ const lists = await retriever.retrieveMany(['rotate a key', 'revoke a token'], {
162
+ topK: 5,
163
+ });
164
+ // lists[0] is the result of 'rotate a key', lists[1] of 'revoke a token'
165
+ ```
166
+
167
+ ## Filter by metadata
168
+
169
+ A filter has Haystack's shape: AND, OR and NOT over `== != > >= < <= in "not in"` ([ADR 0009](./docs/adr/0009-a-filter-has-the-shape-of-haystacks-filters.md)). A field is a passage field (`source`, `seq`, `start`, `end`) or a metadata column, `metadata.<column>`. An engine retriever runs the filter as SQL `WHERE` in the same query.
170
+
171
+ ```ts
172
+ // Planned API.
173
+ const results = await retriever.retrieve('rotate a key', {
174
+ filter: {
175
+ operator: 'AND',
176
+ conditions: [
177
+ { field: 'metadata.product', operator: '==', value: 'vault' },
178
+ { field: 'metadata.year', operator: '>=', value: 2025 },
179
+ ],
180
+ },
181
+ });
182
+ ```
183
+
184
+ On Postgres, pgvector filters after the approximate index scan, so a filtered call can give fewer than `topK` passages ([pgvector README](https://github.com/pgvector/pgvector/blob/master/README.md)).
185
+
186
+ ## Errors
187
+
188
+ | Error | When |
189
+ | -------------------- | -------------------------------------------------------------------------------------------- |
190
+ | `ModelMismatchError` | The query model of an embedding retriever has another model identity than the stored vectors |
191
+ | `VectorMissingError` | `MaximalMarginalRelevance` gets a passage without a vector: the call did not ask for vectors |
192
+
193
+ An error of the model, of the engine or of the reranking model reaches the caller as it is. An aborted call rejects with the reason of its `abortSignal`.
194
+
195
+ ## What the package does not do
196
+
197
+ - **It does not cut or embed documents.** elt loads, cuts and embeds them, and writes the passage contract (elt #5401). Cutting text moves to its own zukhruf package (#5414) ([ADR 0001](./docs/adr/0001-the-package-is-the-query-side-of-rag-and-reads-a-passage-contract-that-elt-writes.md)).
198
+ - **It does not ship adapters for live sources.** A web search or the API of a product is an adapter of the host that implements `Retriever` ([ADR 0002](./docs/adr/0002-a-retriever-is-a-port-for-any-source.md)).
199
+ - **It does not make agent tools.** An AI SDK tool over a retriever is the consumer's code.
200
+ - **It does not write the keyword index or the vector index.** elt writes both; this package only queries them ([ADR 0004](./docs/adr/0004-a-keyword-retriever-queries-the-keyword-index-that-elt-writes.md)).
201
+
202
+ ## Who uses it
203
+
204
+ No consumer uses it yet. These moves are forced onto it after it ships.
205
+
206
+ | Consumer | How | Record |
207
+ | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
208
+ | deepagents | `@deepagents/retrieval` goes; toolbox `search_content_tool` and the orchestrator move to this package and elt | #5406 |
209
+ | deepagents | `ContextStore.searchMessages` (FTS5, `ts_rank`, `CONTAINSTABLE`) moves to the keyword retrievers, with one score direction | #5417, #4884 |
210
+ | April | the TB2 explorer moves to an elt push source, the Vectorizer and `SqliteEmbeddingRetriever`; its hand collapse of passages to documents becomes `Collapse` | #5407 |
211
+ | text2sql | the two `toFtsQuery` copies move to the keyword retrievers | #5410 |
212
+ | datahub | the planned third FTS5 BM25 place uses the keyword retrievers; its staff search fusion uses the fusions | #3451, #3426 |
213
+ | Meddit | its evidence layer moves onto the package after it ships | #5378 |
214
+ | `@zukhruf/mteb` | keeps its MTEB port of `HybridSearch`; it does not move | #5400 |
215
+
216
+ ## The same concepts in other systems
217
+
218
+ | Concept | Other systems |
219
+ | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
220
+ | `Passage` | LlamaIndex `TextNode` ([schema.py](https://raw.githubusercontent.com/run-llama/llama_index/main/llama-index-core/llama_index/core/schema.py)); LangChain `Document` ([document.ts](https://raw.githubusercontent.com/langchain-ai/langchainjs/main/libs/langchain-core/src/documents/document.ts)); Haystack `Document` ([document.py](https://raw.githubusercontent.com/deepset-ai/haystack/main/haystack/dataclasses/document.py)) |
221
+ | `ScoredPassage` | LlamaIndex `NodeWithScore`; .NET `VectorSearchResult<TRecord>{Record, Score}` ([vector search](https://learn.microsoft.com/en-us/dotnet/ai/vector-stores/vector-search)) |
222
+ | `Retriever.retrieve` | LlamaIndex `BaseRetriever.retrieve` ([base_retriever.py](https://raw.githubusercontent.com/run-llama/llama_index/main/llama-index-core/llama_index/core/base/base_retriever.py)); LangChain `BaseRetriever` ([retrievers/index.ts](https://raw.githubusercontent.com/langchain-ai/langchainjs/main/libs/langchain-core/src/retrievers/index.ts)); Semantic Kernel `ITextSearch` ([text search](https://learn.microsoft.com/en-us/semantic-kernel/concepts/text-search/text-search-abstractions)) |
223
+ | engine retrievers | Haystack's retrievers for each document store ([retrievers](https://docs.haystack.deepset.ai/docs/retrievers)) |
224
+ | `topK`, `includeVectors` | Haystack `top_k` = 10 ([document_store.py](https://raw.githubusercontent.com/deepset-ai/haystack/main/haystack/document_stores/in_memory/document_store.py)), `return_embedding`; Mastra `topK` = 10 ([pg](https://mastra.ai/reference/vectors/pg)); .NET `IncludeVectors` ([VectorSearchOptions](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.vectordata.vectorsearchoptions-1)) |
225
+ | `Filter` | Haystack filters ([metadata filtering](https://docs.haystack.deepset.ai/docs/metadata-filtering)); LlamaIndex `MetadataFilters` ([types.py](https://raw.githubusercontent.com/run-llama/llama_index/main/llama-index-core/llama_index/core/vector_stores/types.py)); Qdrant `filter` ([search](https://qdrant.tech/documentation/search/search/)) |
226
+ | `FusionRetriever` | LlamaIndex `QueryFusionRetriever` ([fusion_retriever.py](https://raw.githubusercontent.com/run-llama/llama_index/main/llama-index-core/llama_index/core/retrievers/fusion_retriever.py)); LangChain `EnsembleRetriever` ([ensemble.ts](https://raw.githubusercontent.com/langchain-ai/langchainjs/main/libs/langchain-classic/src/retrievers/ensemble.ts)); Elasticsearch `rrf` ([rrf retriever](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/retrievers/rrf-retriever)); Qdrant `prefetch` ([hybrid queries](https://qdrant.tech/documentation/concepts/hybrid-queries/)) |
227
+ | score fusions | Weaviate `relativeScoreFusion` ([hybrid](https://docs.weaviate.io/weaviate/search/hybrid)); OpenSearch `min_max` ([normalization processor](https://docs.opensearch.org/latest/search-plugins/search-pipelines/normalization-processor/)); Haystack `distribution_based_rank_fusion` ([document_joiner.py](https://raw.githubusercontent.com/deepset-ai/haystack/main/haystack/components/joiners/document_joiner.py)); Qdrant `dbsf` |
228
+ | `MultiQueryRetriever`, HyDE | LangChain `MultiQueryRetriever` ([multi_query.ts](https://raw.githubusercontent.com/langchain-ai/langchainjs/main/libs/langchain-classic/src/retrievers/multi_query.ts)), `HydeRetriever` ([HyDE](https://docs.langchain.com/oss/javascript/integrations/retrievers/hyde)) |
229
+ | `PostprocessingRetriever` | LangChain `ContextualCompressionRetriever` ([contextual_compression.ts](https://raw.githubusercontent.com/langchain-ai/langchainjs/main/libs/langchain-classic/src/retrievers/contextual_compression.ts)); LlamaIndex `node_postprocessors` ([types.py](https://raw.githubusercontent.com/run-llama/llama_index/main/llama-index-core/llama_index/core/postprocessor/types.py)) |
230
+ | `MaximalMarginalRelevance` | Elasticsearch `diversify` `lambda` ([diversify retriever](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/retrievers/diversify-retriever)); Weaviate `balance` ([1.39](https://weaviate.io/blog/weaviate-1-39-release)); Qdrant `mmr` `diversity`, 1 = diverse ([search relevance](https://qdrant.tech/documentation/search/search-relevance/)) |
231
+ | `Collapse` | Elasticsearch `collapse` ([standard retriever](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/retrievers/standard-retriever)); Vespa `collapsesize` ([query API](https://docs.vespa.ai/en/reference/api/query.html)); Qdrant `query_points_groups` ([search](https://qdrant.tech/documentation/search/search/)) |
232
+ | `ContextWindow` | Haystack `SentenceWindowRetriever` ([sentence_window_retriever.py](https://raw.githubusercontent.com/deepset-ai/haystack/main/haystack/components/retrievers/sentence_window_retriever.py)); LlamaIndex `PrevNextNodePostprocessor` |
233
+ | `LongContextReorder` | LangChain `LongContextReorder` ([reference](https://reference.langchain.com/python/langchain-community/document_transformers/long_context_reorder/LongContextReorder)); Haystack `LostInTheMiddleRanker` ([rankers](https://docs.haystack.deepset.ai/docs/rankers)) |
234
+ | hybrid search in SQL | Alex Garcia's sqlite-vec recipe, RRF k 60 in SQL ([hybrid search](https://alexgarcia.xyz/blog/2024/sqlite-vec-hybrid-search/index.html)) |
235
+ | passage contract | pgai `create_vectorizer` ([API reference](https://raw.githubusercontent.com/timescale/pgai/main/docs/vectorizer/api-reference.md)); Airbyte pgvector ([destination](https://docs.airbyte.com/integrations/destinations/pgvector)) |
236
+
237
+ ## Why not an npm package
238
+
239
+ Checked on 2026-11-23 with `npm pack` of each candidate and no install. The requirements: **R1** takes AI SDK 7 models; **R2** reads tables that exist, on SQLite, Postgres and SQL Server; **R3** has the features (fusion, rerank, MMR, grouping); **R4** takes an `AbortSignal`. No candidate passes all four. R2 decides: every store creates its own tables, and none searches vectors in SQL Server. Node 26.13.0 has no built-in module for retrieval, reranking, BM25 or fusion.
240
+
241
+ | Package | R1 | R2 | R3 | R4 |
242
+ | -------------------------------------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | --------------------------------- |
243
+ | `llamaindex` 0.12.1 | fails: only `@llamaindex/vercel`, peer `ai` ^5 | fails: `@llamaindex/postgres` creates fixed columns; no `node:sqlite` or SQL Server store | in part: HYBRID and MMR modes, a cutoff, a reranker; no RRF | fails: `retrieve` takes no signal |
244
+ | `@langchain/core`, `/classic` | fails: no `ai` import | in part: `PGVectorStore` maps a table, with metadata in one JSONB column; no SQL Server store | in part: RRF `c` 60, multi-query, HyDE, compression, MMR; no score fusion or collapse | passes: `RunnableConfig.signal` |
245
+ | `@mastra/rag` 2.8.1 | in part: own model types; `rerank` is not AI SDK `RerankingModel` | fails: `@mastra/pg` and `@mastra/libsql` create their tables; `@mastra/mssql` has no vector API | fails: no RRF or MMR | fails: no `AbortSignal` |
246
+ | `@orama/orama` 3.1.18 | fails: own embedding plugins | fails: an in-memory index of its own, no SQL | in part: a hybrid mode | fails: no `AbortSignal` |
247
+ | `@convex-dev/rag` 0.7.9 | passes: peer `ai` ^7 | fails: it chunks and embeds into Convex storage | in part: RRF, filters, neighbour chunks; no rerank or MMR | fails: no `AbortSignal` |
248
+ | `@reaatech/hybrid-rag-retrieval` 0.1.1 | fails: depends on `openai`, no `ai` import | fails: depends on Qdrant | in part: RRF, own BM25, a reranker | fails: no signal |
249
+
250
+ ## API
251
+
252
+ ```ts
253
+ // @zukhruf/retrieval (planned)
254
+ import type { EmbeddingModel, LanguageModel, RerankingModel } from 'ai';
255
+
256
+ type Passage = {
257
+ readonly id: string;
258
+ readonly text: string;
259
+ readonly source: string;
260
+ readonly seq: number;
261
+ readonly start: number;
262
+ readonly end: number;
263
+ readonly metadata: Readonly<Record<string, unknown>>;
264
+ };
265
+ type ScoredPassage = {
266
+ readonly passage: Passage;
267
+ readonly score: number;
268
+ readonly vector?: readonly number[];
269
+ }; // higher is better
270
+
271
+ type Filter =
272
+ | {
273
+ readonly operator: 'AND' | 'OR' | 'NOT';
274
+ readonly conditions: readonly Filter[];
275
+ }
276
+ | {
277
+ readonly field: string; // 'source' | 'seq' | 'start' | 'end' | `metadata.${string}`
278
+ readonly operator:
279
+ '==' | '!=' | '>' | '>=' | '<' | '<=' | 'in' | 'not in';
280
+ readonly value: unknown;
281
+ };
282
+
283
+ type RetrieveOptions = {
284
+ readonly topK?: number; // 10
285
+ readonly filter?: Filter;
286
+ readonly includeVectors?: boolean; // false
287
+ readonly abortSignal?: AbortSignal;
288
+ };
289
+
290
+ interface Retriever {
291
+ retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
292
+ }
293
+ interface BatchRetriever extends Retriever {
294
+ retrieveMany(
295
+ queries: readonly string[],
296
+ options?: RetrieveOptions,
297
+ ): Promise<ScoredPassage[][]>; // in the order of the queries
298
+ }
299
+
300
+ interface Fusion {
301
+ fuse(
302
+ lists: readonly (readonly ScoredPassage[])[],
303
+ weights: readonly number[],
304
+ ): ScoredPassage[];
305
+ }
306
+ function reciprocalRankFusion(options?: { k?: number }): Fusion; // k 60, ranks from 1
307
+ function relativeScoreFusion(): Fusion; // min-max
308
+ function distributionBasedScoreFusion(): Fusion; // mean ± 3σ
309
+
310
+ class FusionRetriever implements Retriever {
311
+ constructor(options: {
312
+ retrievers: readonly Retriever[];
313
+ fusion?: Fusion; // reciprocalRankFusion({ k: 60 })
314
+ weights?: readonly number[]; // 1 each
315
+ rankWindowSize?: number; // the topK of the call
316
+ });
317
+ }
318
+ class MultiQueryRetriever implements Retriever {
319
+ constructor(options: {
320
+ retriever: Retriever;
321
+ model: LanguageModel;
322
+ queryCount?: number; // 4
323
+ fusion?: Fusion; // reciprocalRankFusion({ k: 60 })
324
+ });
325
+ }
326
+ class HydeRetriever implements Retriever {
327
+ constructor(options: { retriever: Retriever; model: LanguageModel });
328
+ }
329
+
330
+ interface Postprocessor {
331
+ postprocess(
332
+ passages: readonly ScoredPassage[],
333
+ query: string,
334
+ options?: { abortSignal?: AbortSignal },
335
+ ): Promise<ScoredPassage[]>;
336
+ }
337
+ class PostprocessingRetriever implements Retriever {
338
+ constructor(options: {
339
+ retriever: Retriever;
340
+ postprocessors: readonly Postprocessor[];
341
+ });
342
+ }
343
+ class Rerank implements Postprocessor {
344
+ constructor(options: { model: RerankingModel; topN?: number }); // every passage when topN is absent
345
+ }
346
+ class SimilarityCutoff implements Postprocessor {
347
+ constructor(options: { threshold: number }); // keeps score >= threshold
348
+ }
349
+ class MaximalMarginalRelevance implements Postprocessor {
350
+ constructor(options: { lambda?: number; topK: number }); // 0.5; 1 = relevance only
351
+ }
352
+ class Collapse implements Postprocessor {
353
+ constructor(options?: { field?: string; size?: number }); // 'source', 1
354
+ }
355
+ class LongContextReorder implements Postprocessor {}
356
+ class ContextWindow implements Postprocessor {
357
+ constructor(options: { store: PassageStore; windowSize?: number }); // 3
358
+ }
359
+
360
+ interface PassageStore {
361
+ get(ids: readonly string[]): Promise<ReadonlyMap<string, Passage>>;
362
+ neighbours(options: {
363
+ source: string;
364
+ seq: number;
365
+ windowSize: number;
366
+ }): Promise<Passage[]>;
367
+ }
368
+
369
+ class ModelMismatchError extends Error {
370
+ readonly stored: { provider: string; modelId: string; dimensions: number };
371
+ readonly query: { provider: string; modelId: string; dimensions: number };
372
+ }
373
+ class VectorMissingError extends Error {
374
+ readonly passageId: string;
375
+ }
376
+
377
+ // @zukhruf/retrieval/sqlite
378
+ class SqliteEmbeddingRetriever implements BatchRetriever {
379
+ constructor(options: {
380
+ database: DatabaseSync;
381
+ passages: string;
382
+ vectors: string;
383
+ model: EmbeddingModel;
384
+ });
385
+ }
386
+ class SqliteBM25Retriever implements Retriever {
387
+ constructor(options: {
388
+ database: DatabaseSync;
389
+ passages: string;
390
+ index: string;
391
+ operator?: 'AND' | 'OR';
392
+ }); // 'AND'
393
+ }
394
+ class SqlitePassageStore implements PassageStore {
395
+ constructor(options: { database: DatabaseSync; passages: string });
396
+ }
397
+
398
+ // @zukhruf/retrieval/postgres
399
+ type PostgresSession = {
400
+ query(
401
+ sql: string,
402
+ parameters: readonly unknown[],
403
+ ): Promise<readonly Record<string, unknown>[]>;
404
+ };
405
+ class PgvectorEmbeddingRetriever implements BatchRetriever {
406
+ constructor(options: {
407
+ session: PostgresSession;
408
+ passages: string;
409
+ vectors: string;
410
+ model: EmbeddingModel;
411
+ });
412
+ }
413
+ class PgvectorKeywordRetriever implements Retriever {
414
+ constructor(options: {
415
+ session: PostgresSession;
416
+ passages: string;
417
+ index: string;
418
+ operator?: 'AND' | 'OR';
419
+ });
420
+ }
421
+ class PostgresPassageStore implements PassageStore {
422
+ constructor(options: { session: PostgresSession; passages: string });
423
+ }
424
+
425
+ // @zukhruf/retrieval/sqlserver
426
+ type SqlServerSession = {
427
+ query(
428
+ sql: string,
429
+ parameters: Readonly<Record<string, unknown>>,
430
+ ): Promise<readonly Record<string, unknown>[]>;
431
+ };
432
+ class SqlServerEmbeddingRetriever implements BatchRetriever {
433
+ constructor(options: {
434
+ session: SqlServerSession;
435
+ passages: string;
436
+ vectors: string;
437
+ model: EmbeddingModel;
438
+ });
439
+ }
440
+ class SqlServerKeywordRetriever implements Retriever {
441
+ constructor(options: {
442
+ session: SqlServerSession;
443
+ passages: string;
444
+ index: string;
445
+ operator?: 'AND' | 'OR';
446
+ });
447
+ }
448
+ class SqlServerPassageStore implements PassageStore {
449
+ constructor(options: { session: SqlServerSession; passages: string });
450
+ }
451
+ ```
452
+
453
+ ## Documentation
454
+
455
+ **Decisions**
456
+
457
+ - [ADR 0001](./docs/adr/0001-the-package-is-the-query-side-of-rag-and-reads-a-passage-contract-that-elt-writes.md): the package is the query side of RAG and reads a passage contract that elt writes.
458
+ - [ADR 0002](./docs/adr/0002-a-retriever-is-a-port-for-any-source.md): a retriever is a port for any source.
459
+ - [ADR 0003](./docs/adr/0003-an-embedding-retriever-runs-the-vector-search-in-the-sql-of-its-engine-one-query-or-a-batch.md): an embedding retriever runs the vector search in the SQL of its engine, one query or a batch.
460
+ - [ADR 0004](./docs/adr/0004-a-keyword-retriever-queries-the-keyword-index-that-elt-writes.md): a keyword retriever queries the keyword index that elt writes.
461
+ - [ADR 0005](./docs/adr/0005-a-score-is-higher-for-a-better-passage.md): a score is higher for a better passage.
462
+ - [ADR 0006](./docs/adr/0006-a-retriever-gives-10-passages-when-the-caller-gives-no-topk.md): a retriever gives 10 passages when the caller gives no topK.
463
+ - [ADR 0007](./docs/adr/0007-reciprocal-rank-fusion-is-the-default-fusion-with-k-60-and-ranks-from-1.md): reciprocal rank fusion is the default fusion, with k 60 and ranks from 1.
464
+ - [ADR 0008](./docs/adr/0008-a-search-is-composed-of-retrievers-that-wrap-retrievers-and-a-list-of-postprocessors.md): a search is composed of retrievers that wrap retrievers and a list of postprocessors.
465
+ - [ADR 0009](./docs/adr/0009-a-filter-has-the-shape-of-haystacks-filters.md): a filter has the shape of Haystack's filters.
466
+ - [ADR 0010](./docs/adr/0010-an-embedding-retriever-refuses-a-query-model-whose-identity-differs-from-the-stored-vectors.md): an embedding retriever refuses a query model whose identity differs from the stored vectors.
467
+ - [ADR 0011](./docs/adr/0011-the-package-is-in-the-base-layer.md): the package is in the base layer.
468
+ - [ADR 0012](./docs/adr/0012-the-package-is-zukhruf-retrieval-and-its-port-is-retriever-with-retrieve-after-go.md): the package is @zukhruf/retrieval, and its port is Retriever with retrieve, after Go.
@@ -0,0 +1,9 @@
1
+ import type { RetrieveOptions } from './retrieve-options.ts';
2
+ import type { Retriever } from './retriever.ts';
3
+ import type { ScoredPassage } from './scored-passage.ts';
4
+ /** A retriever that also searches many queries in one call (ADR 0003). */
5
+ export interface BatchRetriever extends Retriever {
6
+ /** Gives one result list for each query, in the order of the queries. */
7
+ retrieveMany(queries: readonly string[], options?: RetrieveOptions): Promise<ScoredPassage[][]>;
8
+ }
9
+ //# sourceMappingURL=batch-retriever.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"batch-retriever.d.ts","sourceRoot":"","sources":["../src/batch-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,0EAA0E;AAC1E,MAAM,WAAW,cAAe,SAAQ,SAAS;IAC/C,yEAAyE;IACzE,YAAY,CACV,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;CAC/B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=batch-retriever.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"batch-retriever.js","sourceRoot":"","sources":["../src/batch-retriever.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A condition on passage fields and metadata columns, in the shape of
3
+ * Haystack's filters (ADR 0009). An engine retriever runs it as SQL `WHERE`.
4
+ */
5
+ export type Filter = {
6
+ readonly operator: 'AND' | 'OR' | 'NOT';
7
+ readonly conditions: readonly Filter[];
8
+ } | {
9
+ readonly field: 'source' | 'seq' | 'start' | 'end' | `metadata.${string}`;
10
+ readonly operator: '==' | '!=' | '>' | '>=' | '<' | '<=' | 'in' | 'not in';
11
+ readonly value: unknown;
12
+ };
13
+ //# sourceMappingURL=filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.d.ts","sourceRoot":"","sources":["../src/filter.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,MAAM,GACd;IACE,QAAQ,CAAC,QAAQ,EAAE,KAAK,GAAG,IAAI,GAAG,KAAK,CAAC;IACxC,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;CACxC,GACD;IACE,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,KAAK,GAAG,OAAO,GAAG,KAAK,GAAG,YAAY,MAAM,EAAE,CAAC;IAC1E,QAAQ,CAAC,QAAQ,EACf,IAAI,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,QAAQ,CAAC;IAC1D,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB,CAAC"}
package/dist/filter.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.js","sourceRoot":"","sources":["../src/filter.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import type { ScoredPassage } from './scored-passage.ts';
2
+ /** Combines the result lists of several retrievers into one list, on `Passage.id` (ADR 0007). */
3
+ export interface Fusion {
4
+ /** `weights` has one weight for each list, in the order of the lists. */
5
+ fuse(lists: readonly (readonly ScoredPassage[])[], weights: readonly number[]): ScoredPassage[];
6
+ }
7
+ //# sourceMappingURL=fusion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fusion.d.ts","sourceRoot":"","sources":["../src/fusion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,iGAAiG;AACjG,MAAM,WAAW,MAAM;IACrB,yEAAyE;IACzE,IAAI,CACF,KAAK,EAAE,SAAS,CAAC,SAAS,aAAa,EAAE,CAAC,EAAE,EAC5C,OAAO,EAAE,SAAS,MAAM,EAAE,GACzB,aAAa,EAAE,CAAC;CACpB"}
package/dist/fusion.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=fusion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fusion.js","sourceRoot":"","sources":["../src/fusion.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ export type { BatchRetriever } from './batch-retriever.ts';
2
+ export type { Filter } from './filter.ts';
3
+ export type { Fusion } from './fusion.ts';
4
+ export { ModelMismatchError } from './model-mismatch-error.ts';
5
+ export type { PassageStore } from './passage-store.ts';
6
+ export type { Passage } from './passage.ts';
7
+ export type { Postprocessor } from './postprocessor.ts';
8
+ export type { RetrieveOptions } from './retrieve-options.ts';
9
+ export type { Retriever } from './retriever.ts';
10
+ export type { ScoredPassage } from './scored-passage.ts';
11
+ export { VectorMissingError } from './vector-missing-error.ts';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,YAAY,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { ModelMismatchError } from './model-mismatch-error.js';
2
+ export { VectorMissingError } from './vector-missing-error.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAO/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
@@ -0,0 +1,18 @@
1
+ type ModelIdentity = {
2
+ readonly provider: string;
3
+ readonly modelId: string;
4
+ readonly dimensions: number;
5
+ };
6
+ /**
7
+ * The query model of an embedding retriever has another model identity than
8
+ * the model that made the stored vectors, so their vectors do not compare
9
+ * (ADR 0010).
10
+ */
11
+ export declare class ModelMismatchError extends Error {
12
+ readonly name = "ModelMismatchError";
13
+ readonly stored: ModelIdentity;
14
+ readonly query: ModelIdentity;
15
+ constructor(stored: ModelIdentity, query: ModelIdentity);
16
+ }
17
+ export {};
18
+ //# sourceMappingURL=model-mismatch-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-mismatch-error.d.ts","sourceRoot":"","sources":["../src/model-mismatch-error.ts"],"names":[],"mappings":"AAAA,KAAK,aAAa,GAAG;IACnB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,SAAkB,IAAI,wBAAwB;IAC9C,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAE9B,YAAY,MAAM,EAAE,aAAa,EAAE,KAAK,EAAE,aAAa,EAMtD;CACF"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The query model of an embedding retriever has another model identity than
3
+ * the model that made the stored vectors, so their vectors do not compare
4
+ * (ADR 0010).
5
+ */
6
+ export class ModelMismatchError extends Error {
7
+ name = 'ModelMismatchError';
8
+ stored;
9
+ query;
10
+ constructor(stored, query) {
11
+ super(`The query model ${describe(query)} is not the model of the stored vectors, ${describe(stored)}`);
12
+ this.stored = stored;
13
+ this.query = query;
14
+ }
15
+ }
16
+ /** The form of elt's Embedding rule: `openai/text-embedding-3-small (1536 dimensions)`. */
17
+ const describe = ({ provider, modelId, dimensions }) => `${provider}/${modelId} (${dimensions} dimensions)`;
18
+ //# sourceMappingURL=model-mismatch-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-mismatch-error.js","sourceRoot":"","sources":["../src/model-mismatch-error.ts"],"names":[],"mappings":"AAMA;;;;GAIG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzB,IAAI,GAAG,oBAAoB,CAAC;IACrC,MAAM,CAAgB;IACtB,KAAK,CAAgB;IAE9B,YAAY,MAAqB,EAAE,KAAoB;QACrD,KAAK,CACH,mBAAmB,QAAQ,CAAC,KAAK,CAAC,4CAA4C,QAAQ,CAAC,MAAM,CAAC,EAAE,CACjG,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF;AAED,2FAA2F;AAC3F,MAAM,QAAQ,GAAG,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAiB,EAAU,EAAE,CAC5E,GAAG,QAAQ,IAAI,OAAO,KAAK,UAAU,cAAc,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { Passage } from './passage.ts';
2
+ /** Reads passages of the passage contract by id, and the neighbours of a passage in its source. */
3
+ export interface PassageStore {
4
+ /** Gives each passage that the store holds; an id it does not hold is absent. */
5
+ get(ids: readonly string[]): Promise<ReadonlyMap<string, Passage>>;
6
+ neighbours(options: {
7
+ readonly source: string;
8
+ readonly seq: number;
9
+ readonly windowSize: number;
10
+ }): Promise<Passage[]>;
11
+ }
12
+ //# sourceMappingURL=passage-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"passage-store.d.ts","sourceRoot":"","sources":["../src/passage-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,mGAAmG;AACnG,MAAM,WAAW,YAAY;IAC3B,iFAAiF;IACjF,GAAG,CAAC,GAAG,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACnE,UAAU,CAAC,OAAO,EAAE;QAClB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;KAC7B,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;CACxB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=passage-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"passage-store.js","sourceRoot":"","sources":["../src/passage-store.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ /** One part of a source that a search gives back (CONTEXT.md, "Passage"). */
2
+ export type Passage = {
3
+ readonly id: string;
4
+ readonly text: string;
5
+ /** The key of the row or file that the passage comes from. */
6
+ readonly source: string;
7
+ /** The position of the passage in its source. */
8
+ readonly seq: number;
9
+ readonly start: number;
10
+ readonly end: number;
11
+ readonly metadata: Readonly<Record<string, unknown>>;
12
+ };
13
+ //# sourceMappingURL=passage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"passage.d.ts","sourceRoot":"","sources":["../src/passage.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,8DAA8D;IAC9D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iDAAiD;IACjD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACtD,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=passage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"passage.js","sourceRoot":"","sources":["../src/passage.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ import type { ScoredPassage } from './scored-passage.ts';
2
+ /**
3
+ * Changes a result list after the search: it reranks, cuts, diversifies,
4
+ * groups, reorders or widens the passages (ADR 0008).
5
+ */
6
+ export interface Postprocessor {
7
+ postprocess(passages: readonly ScoredPassage[], query: string, options?: {
8
+ readonly abortSignal?: AbortSignal;
9
+ }): Promise<ScoredPassage[]>;
10
+ }
11
+ //# sourceMappingURL=postprocessor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postprocessor.d.ts","sourceRoot":"","sources":["../src/postprocessor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,WAAW,CACT,QAAQ,EAAE,SAAS,aAAa,EAAE,EAClC,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAA;KAAE,GAC/C,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;CAC7B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=postprocessor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postprocessor.js","sourceRoot":"","sources":["../src/postprocessor.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ import type { Filter } from './filter.ts';
2
+ export type RetrieveOptions = {
3
+ /** The most passages that the call gives: 10 when absent (ADR 0006). */
4
+ readonly topK?: number;
5
+ readonly filter?: Filter;
6
+ /** Gives the vector of each passage: false when absent. */
7
+ readonly includeVectors?: boolean;
8
+ readonly abortSignal?: AbortSignal;
9
+ };
10
+ //# sourceMappingURL=retrieve-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrieve-options.d.ts","sourceRoot":"","sources":["../src/retrieve-options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,MAAM,eAAe,GAAG;IAC5B,wEAAwE;IACxE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,2DAA2D;IAC3D,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAClC,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;CACpC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=retrieve-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrieve-options.js","sourceRoot":"","sources":["../src/retrieve-options.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ import type { RetrieveOptions } from './retrieve-options.ts';
2
+ import type { ScoredPassage } from './scored-passage.ts';
3
+ /**
4
+ * The port of the package: any source can implement it (ADR 0002). It gives
5
+ * scored passages, best first.
6
+ */
7
+ export interface Retriever {
8
+ retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
9
+ }
10
+ //# sourceMappingURL=retriever.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retriever.d.ts","sourceRoot":"","sources":["../src/retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;CAC9E"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=retriever.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retriever.js","sourceRoot":"","sources":["../src/retriever.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ import type { Passage } from './passage.ts';
2
+ /** A passage with its score, and its vector when the call asks for it. */
3
+ export type ScoredPassage = {
4
+ readonly passage: Passage;
5
+ /** Higher is better, for every retriever (ADR 0005). */
6
+ readonly score: number;
7
+ readonly vector?: readonly number[];
8
+ };
9
+ //# sourceMappingURL=scored-passage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scored-passage.d.ts","sourceRoot":"","sources":["../src/scored-passage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,0EAA0E;AAC1E,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,wDAAwD;IACxD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACrC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=scored-passage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scored-passage.js","sourceRoot":"","sources":["../src/scored-passage.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A postprocessor that compares vectors got a passage without one: the call
3
+ * did not ask for vectors with `includeVectors: true`.
4
+ */
5
+ export declare class VectorMissingError extends Error {
6
+ readonly name = "VectorMissingError";
7
+ readonly passageId: string;
8
+ constructor(passageId: string);
9
+ }
10
+ //# sourceMappingURL=vector-missing-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-missing-error.d.ts","sourceRoot":"","sources":["../src/vector-missing-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,SAAkB,IAAI,wBAAwB;IAC9C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,YAAY,SAAS,EAAE,MAAM,EAK5B;CACF"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A postprocessor that compares vectors got a passage without one: the call
3
+ * did not ask for vectors with `includeVectors: true`.
4
+ */
5
+ export class VectorMissingError extends Error {
6
+ name = 'VectorMissingError';
7
+ passageId;
8
+ constructor(passageId) {
9
+ super(`The passage ${passageId} has no vector: retrieve with includeVectors: true`);
10
+ this.passageId = passageId;
11
+ }
12
+ }
13
+ //# sourceMappingURL=vector-missing-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-missing-error.js","sourceRoot":"","sources":["../src/vector-missing-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzB,IAAI,GAAG,oBAAoB,CAAC;IACrC,SAAS,CAAS;IAE3B,YAAY,SAAiB;QAC3B,KAAK,CACH,eAAe,SAAS,oDAAoD,CAC7E,CAAC;QACF,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@zukhruf/retrieval",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.8.77",
4
+ "description": "The query side of RAG: retrievers that search a passage table by meaning or keywords in SQLite, Postgres or SQL Server, fuse lists, rerank and post-process, and give the best passages first with a score that is higher for a better passage.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/JanuaryLabs/zukhruf",
9
+ "directory": "packages/retrieval"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/JanuaryLabs/zukhruf/issues"
13
+ },
14
+ "homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/retrieval#readme",
15
+ "keywords": [
16
+ "retrieval",
17
+ "rag",
18
+ "retriever",
19
+ "vector-search",
20
+ "bm25",
21
+ "reciprocal-rank-fusion",
22
+ "rerank",
23
+ "ai-sdk",
24
+ "sqlite",
25
+ "postgres",
26
+ "sql-server"
27
+ ],
28
+ "type": "module",
29
+ "exports": {
30
+ "./package.json": "./package.json",
31
+ ".": {
32
+ "@zukhruf/source": "./src/index.ts",
33
+ "types": "./dist/index.d.ts",
34
+ "import": "./dist/index.js",
35
+ "default": "./dist/index.js"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "!dist/spec/**",
41
+ "!**/*.tsbuildinfo",
42
+ "!**/*.test.*"
43
+ ],
44
+ "publishConfig": {
45
+ "access": "public"
46
+ }
47
+ }