@zukhruf/retrieval 0.0.0-stage → 0.8.78

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 (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +468 -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-identity.d.ts +26 -0
  20. package/dist/model-identity.d.ts.map +1 -0
  21. package/dist/model-identity.js +31 -0
  22. package/dist/model-identity.js.map +1 -0
  23. package/dist/model-mismatch-error.d.ts +15 -0
  24. package/dist/model-mismatch-error.d.ts.map +1 -0
  25. package/dist/model-mismatch-error.js +18 -0
  26. package/dist/model-mismatch-error.js.map +1 -0
  27. package/dist/passage-store.d.ts +12 -0
  28. package/dist/passage-store.d.ts.map +1 -0
  29. package/dist/passage-store.js +2 -0
  30. package/dist/passage-store.js.map +1 -0
  31. package/dist/passage.d.ts +13 -0
  32. package/dist/passage.d.ts.map +1 -0
  33. package/dist/passage.js +2 -0
  34. package/dist/passage.js.map +1 -0
  35. package/dist/postprocessor.d.ts +11 -0
  36. package/dist/postprocessor.d.ts.map +1 -0
  37. package/dist/postprocessor.js +2 -0
  38. package/dist/postprocessor.js.map +1 -0
  39. package/dist/retrieve-options.d.ts +10 -0
  40. package/dist/retrieve-options.d.ts.map +1 -0
  41. package/dist/retrieve-options.js +2 -0
  42. package/dist/retrieve-options.js.map +1 -0
  43. package/dist/retriever.d.ts +10 -0
  44. package/dist/retriever.d.ts.map +1 -0
  45. package/dist/retriever.js +2 -0
  46. package/dist/retriever.js.map +1 -0
  47. package/dist/scored-passage.d.ts +9 -0
  48. package/dist/scored-passage.d.ts.map +1 -0
  49. package/dist/scored-passage.js +2 -0
  50. package/dist/scored-passage.js.map +1 -0
  51. package/dist/sqlite/identifier.d.ts +3 -0
  52. package/dist/sqlite/identifier.d.ts.map +1 -0
  53. package/dist/sqlite/identifier.js +3 -0
  54. package/dist/sqlite/identifier.js.map +1 -0
  55. package/dist/sqlite/passage-rows.d.ts +13 -0
  56. package/dist/sqlite/passage-rows.d.ts.map +1 -0
  57. package/dist/sqlite/passage-rows.js +44 -0
  58. package/dist/sqlite/passage-rows.js.map +1 -0
  59. package/dist/sqlite/sqlite-bm25-retriever.d.ts +21 -0
  60. package/dist/sqlite/sqlite-bm25-retriever.d.ts.map +1 -0
  61. package/dist/sqlite/sqlite-bm25-retriever.js +48 -0
  62. package/dist/sqlite/sqlite-bm25-retriever.js.map +1 -0
  63. package/dist/sqlite/sqlite-embedding-retriever.d.ts +23 -0
  64. package/dist/sqlite/sqlite-embedding-retriever.d.ts.map +1 -0
  65. package/dist/sqlite/sqlite-embedding-retriever.js +125 -0
  66. package/dist/sqlite/sqlite-embedding-retriever.js.map +1 -0
  67. package/dist/sqlite/sqlite-passage-store.d.ts +23 -0
  68. package/dist/sqlite/sqlite-passage-store.d.ts.map +1 -0
  69. package/dist/sqlite/sqlite-passage-store.js +32 -0
  70. package/dist/sqlite/sqlite-passage-store.js.map +1 -0
  71. package/dist/sqlite/where.d.ts +13 -0
  72. package/dist/sqlite/where.d.ts.map +1 -0
  73. package/dist/sqlite/where.js +62 -0
  74. package/dist/sqlite/where.js.map +1 -0
  75. package/dist/sqlite.d.ts +4 -0
  76. package/dist/sqlite.d.ts.map +1 -0
  77. package/dist/sqlite.js +4 -0
  78. package/dist/sqlite.js.map +1 -0
  79. package/dist/vector-missing-error.d.ts +10 -0
  80. package/dist/vector-missing-error.d.ts.map +1 -0
  81. package/dist/vector-missing-error.js +13 -0
  82. package/dist/vector-missing-error.js.map +1 -0
  83. package/package.json +58 -4
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The form of elt's Embedding rule, `openai/text-embedding-3-small (1536
3
+ * dimensions)`, which the `model` column of the passage vectors holds.
4
+ */
5
+ export const describe = ({ provider, modelId, dimensions, }) => `${provider}/${modelId} (${dimensions} dimensions)`;
6
+ const storedForm = /^([^/]+)\/(.+) \((\d+) dimensions\)$/;
7
+ /** Reads the identity that the `model` column of the passage vectors holds. */
8
+ export function parseModelIdentity(text) {
9
+ const [, provider, modelId, dimensions] = storedForm.exec(text) ?? [];
10
+ if (provider === undefined ||
11
+ modelId === undefined ||
12
+ dimensions === undefined) {
13
+ throw new TypeError(`The stored model ${JSON.stringify(text)} is not a model identity of the form "provider/model (n dimensions)"`);
14
+ }
15
+ return { provider, modelId, dimensions: Number(dimensions) };
16
+ }
17
+ /**
18
+ * The provider and the model id of a query model, as elt names them: a
19
+ * string model id is `provider/model`, as the AI SDK gateway reads it.
20
+ */
21
+ export function modelName(model) {
22
+ if (typeof model !== 'string') {
23
+ return { provider: model.provider, modelId: model.modelId };
24
+ }
25
+ const [, provider, modelId] = /^([^/]+)\/(.+)$/.exec(model) ?? [];
26
+ if (provider === undefined || modelId === undefined) {
27
+ throw new TypeError(`The model id ${JSON.stringify(model)} names no provider: write "provider/model"`);
28
+ }
29
+ return { provider, modelId };
30
+ }
31
+ //# sourceMappingURL=model-identity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-identity.js","sourceRoot":"","sources":["../src/model-identity.ts"],"names":[],"mappings":"AAgBA;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,EACvB,QAAQ,EACR,OAAO,EACP,UAAU,GACI,EAAU,EAAE,CAC1B,GAAG,QAAQ,IAAI,OAAO,KAAK,UAAU,cAAc,CAAC;AAEtD,MAAM,UAAU,GAAG,sCAAsC,CAAC;AAE1D,+EAA+E;AAC/E,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,MAAM,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACtE,IACE,QAAQ,KAAK,SAAS;QACtB,OAAO,KAAK,SAAS;QACrB,UAAU,KAAK,SAAS,EACxB,CAAC;QACD,MAAM,IAAI,SAAS,CACjB,oBAAoB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,sEAAsE,CAC/G,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;AAC/D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,KAAqB;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;IAC9D,CAAC;IACD,MAAM,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,GAAG,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAClE,IAAI,QAAQ,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACpD,MAAM,IAAI,SAAS,CACjB,gBAAgB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,4CAA4C,CAClF,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { ModelName } from './model-identity.ts';
2
+ /**
3
+ * The query model of an embedding retriever has another provider or model id
4
+ * than the model that made the stored vectors, so their vectors do not
5
+ * compare (ADR 0010). The retriever finds it before the model call. A query
6
+ * vector of other dimensions is a `DimensionMismatchError` of
7
+ * `@zukhruf/vectors`, after the model call.
8
+ */
9
+ export declare class ModelMismatchError extends Error {
10
+ readonly name = "ModelMismatchError";
11
+ readonly stored: ModelName;
12
+ readonly query: ModelName;
13
+ constructor(stored: ModelName, query: ModelName);
14
+ }
15
+ //# 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,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;;;;GAMG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,SAAkB,IAAI,wBAAwB;IAC9C,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAE1B,YAAY,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAM9C;CACF"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The query model of an embedding retriever has another provider or model id
3
+ * than the model that made the stored vectors, so their vectors do not
4
+ * compare (ADR 0010). The retriever finds it before the model call. A query
5
+ * vector of other dimensions is a `DimensionMismatchError` of
6
+ * `@zukhruf/vectors`, after the model call.
7
+ */
8
+ export class ModelMismatchError extends Error {
9
+ name = 'ModelMismatchError';
10
+ stored;
11
+ query;
12
+ constructor(stored, query) {
13
+ super(`The query model ${query.provider}/${query.modelId} is not the model of the stored vectors, ${stored.provider}/${stored.modelId}`);
14
+ this.stored = stored;
15
+ this.query = query;
16
+ }
17
+ }
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":"AAEA;;;;;;GAMG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzB,IAAI,GAAG,oBAAoB,CAAC;IACrC,MAAM,CAAY;IAClB,KAAK,CAAY;IAE1B,YAAY,MAAiB,EAAE,KAAgB;QAC7C,KAAK,CACH,mBAAmB,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,OAAO,4CAA4C,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,OAAO,EAAE,CAClI,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF"}
@@ -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,3 @@
1
+ /** A table or column name as one SQLite identifier: a `"` inside it is doubled, so the name never ends the identifier. */
2
+ export declare const identifier: (name: string) => string;
3
+ //# sourceMappingURL=identifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identifier.d.ts","sourceRoot":"","sources":["../../src/sqlite/identifier.ts"],"names":[],"mappings":"AAAA,0HAA0H;AAC1H,eAAO,MAAM,UAAU,SAAU,MAAM,KAAG,MACP,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** A table or column name as one SQLite identifier: a `"` inside it is doubled, so the name never ends the identifier. */
2
+ export const identifier = (name) => `"${name.replaceAll('"', '""')}"`;
3
+ //# sourceMappingURL=identifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../src/sqlite/identifier.ts"],"names":[],"mappings":"AAAA,0HAA0H;AAC1H,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAU,EAAE,CACjD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { SQLInputValue, SQLOutputValue, StatementSync } from 'node:sqlite';
2
+ import type { Passage } from '../passage.ts';
3
+ /**
4
+ * Runs a statement whose result ends with `p.*`, the columns of the passages,
5
+ * and gives the cells before them and the passage of each row. A metadata
6
+ * column is any other column of the passages, under its own name, even when
7
+ * it has the name of a cell before it.
8
+ */
9
+ export declare function passageRows(statement: StatementSync, cellsBefore: number, parameters: readonly SQLInputValue[]): {
10
+ readonly cells: readonly SQLOutputValue[];
11
+ readonly passage: Passage;
12
+ }[];
13
+ //# sourceMappingURL=passage-rows.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"passage-rows.d.ts","sourceRoot":"","sources":["../../src/sqlite/passage-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEhF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAc7C;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,SAAS,EAAE,aAAa,EACxB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,SAAS,aAAa,EAAE,GACnC;IAAE,QAAQ,CAAC,KAAK,EAAE,SAAS,cAAc,EAAE,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;CAAE,EAAE,CA6B5E"}
@@ -0,0 +1,44 @@
1
+ // `rowid` is the integer key that the keyword index joins on (ADR 0004): a
2
+ // view of the passages exposes it as a column, and it is not metadata.
3
+ const passageColumns = new Set([
4
+ 'rowid',
5
+ 'id',
6
+ 'source',
7
+ 'seq',
8
+ 'start',
9
+ 'end',
10
+ 'text',
11
+ ]);
12
+ /**
13
+ * Runs a statement whose result ends with `p.*`, the columns of the passages,
14
+ * and gives the cells before them and the passage of each row. A metadata
15
+ * column is any other column of the passages, under its own name, even when
16
+ * it has the name of a cell before it.
17
+ */
18
+ export function passageRows(statement, cellsBefore, parameters) {
19
+ statement.setReturnArrays(true);
20
+ const names = statement
21
+ .columns()
22
+ .slice(cellsBefore)
23
+ .map(({ name }) => name);
24
+ // node:sqlite types each row as a record; with return arrays it is an
25
+ // array, whose values are the cells in the order of the columns.
26
+ return statement.all(...parameters).map((record) => {
27
+ const row = Object.values(record);
28
+ const values = row.slice(cellsBefore);
29
+ const cell = (name) => values[names.indexOf(name)];
30
+ return {
31
+ cells: row.slice(0, cellsBefore),
32
+ passage: {
33
+ id: String(cell('id')),
34
+ text: String(cell('text')),
35
+ source: String(cell('source')),
36
+ seq: Number(cell('seq')),
37
+ start: Number(cell('start')),
38
+ end: Number(cell('end')),
39
+ metadata: Object.fromEntries(names.flatMap((name, index) => passageColumns.has(name) ? [] : [[name, values[index]]])),
40
+ },
41
+ };
42
+ });
43
+ }
44
+ //# sourceMappingURL=passage-rows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"passage-rows.js","sourceRoot":"","sources":["../../src/sqlite/passage-rows.ts"],"names":[],"mappings":"AAIA,2EAA2E;AAC3E,uEAAuE;AACvE,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC;IAC7B,OAAO;IACP,IAAI;IACJ,QAAQ;IACR,KAAK;IACL,OAAO;IACP,KAAK;IACL,MAAM;CACP,CAAC,CAAC;AAEH;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACzB,SAAwB,EACxB,WAAmB,EACnB,UAAoC;IAEpC,SAAS,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,KAAK,GAAG,SAAS;SACpB,OAAO,EAAE;SACT,KAAK,CAAC,WAAW,CAAC;SAClB,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;IAC3B,sEAAsE;IACtE,iEAAiE;IACjE,OAAO,SAAS,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;QACjD,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACtC,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QAC3D,OAAO;YACL,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,CAAC;YAChC,OAAO,EAAE;gBACP,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACtB,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAC1B,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;gBAC9B,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACxB,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAC5B,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACxB,QAAQ,EAAE,MAAM,CAAC,WAAW,CAC1B,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAC5B,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CACxD,CACF;aACF;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { DatabaseSync } from 'node:sqlite';
2
+ import type { RetrieveOptions } from '../retrieve-options.ts';
3
+ import type { Retriever } from '../retriever.ts';
4
+ import type { ScoredPassage } from '../scored-passage.ts';
5
+ /**
6
+ * Ranks the passages by FTS5 `bm25()` over the keyword index that elt writes:
7
+ * an FTS5 table with the passages as its external content (ADR 0004). The
8
+ * score is `-bm25()`, so a better match has a higher score (ADR 0005).
9
+ */
10
+ export declare class SqliteBM25Retriever implements Retriever {
11
+ #private;
12
+ constructor(options: {
13
+ readonly database: DatabaseSync;
14
+ readonly passages: string;
15
+ readonly index: string;
16
+ /** How the words of the query join: 'AND', the implicit operator of FTS5, when absent. */
17
+ readonly operator?: 'AND' | 'OR';
18
+ });
19
+ retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
20
+ }
21
+ //# sourceMappingURL=sqlite-bm25-retriever.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-bm25-retriever.d.ts","sourceRoot":"","sources":["../../src/sqlite/sqlite-bm25-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAK1D;;;;GAIG;AACH,qBAAa,mBAAoB,YAAW,SAAS;;IAMnD,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;QAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QACvB,0FAA0F;QAC1F,QAAQ,CAAC,QAAQ,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;KAClC,EAKA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAmB1B;CACF"}
@@ -0,0 +1,48 @@
1
+ import { identifier } from './identifier.js';
2
+ import { passageRows } from './passage-rows.js';
3
+ import { where } from './where.js';
4
+ /**
5
+ * Ranks the passages by FTS5 `bm25()` over the keyword index that elt writes:
6
+ * an FTS5 table with the passages as its external content (ADR 0004). The
7
+ * score is `-bm25()`, so a better match has a higher score (ADR 0005).
8
+ */
9
+ export class SqliteBM25Retriever {
10
+ #database;
11
+ #passages;
12
+ #index;
13
+ #operator;
14
+ constructor(options) {
15
+ this.#database = options.database;
16
+ this.#passages = identifier(options.passages);
17
+ this.#index = identifier(options.index);
18
+ this.#operator = options.operator ?? 'AND';
19
+ }
20
+ async retrieve(query, options = {}) {
21
+ options.abortSignal?.throwIfAborted();
22
+ const words = query.split(/\s+/).filter((word) => word !== '');
23
+ if (words.length === 0)
24
+ return [];
25
+ const filter = where(options.filter);
26
+ const statement = this.#database.prepare(`
27
+ SELECT -bm25(${this.#index}) AS "score", p.*
28
+ FROM ${this.#index} JOIN ${this.#passages} p ON p.rowid = ${this.#index}.rowid
29
+ WHERE ${this.#index} MATCH ? AND ${filter.sql}
30
+ ORDER BY bm25(${this.#index}), p."id"
31
+ LIMIT ?`);
32
+ return passageRows(statement, 1, [
33
+ ftsQuery(words, this.#operator),
34
+ ...filter.parameters,
35
+ options.topK ?? 10,
36
+ ]).map(({ cells: [score], passage }) => ({
37
+ passage,
38
+ score: Number(score),
39
+ }));
40
+ }
41
+ }
42
+ /**
43
+ * Each word as an FTS5 phrase, with a `"` in it doubled, so no word of the
44
+ * query is FTS5 syntax: `-`, `:`, `*`, `NEAR` and `OR` stay words
45
+ * (sqlite.org/fts5.html §3), as text2sql's `toFtsQuery` quotes them (#5410).
46
+ */
47
+ const ftsQuery = (words, operator) => words.map((word) => `"${word.replaceAll('"', '""')}"`).join(` ${operator} `);
48
+ //# sourceMappingURL=sqlite-bm25-retriever.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-bm25-retriever.js","sourceRoot":"","sources":["../../src/sqlite/sqlite-bm25-retriever.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;GAIG;AACH,MAA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C,EAAE,KAAK,EAAE,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;YACvC,OAAO;YACP,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC;SACrB,CAAC,CAAC,CAAC;IACN,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,QAAQ,GAAG,CAAC,KAAwB,EAAE,QAAsB,EAAU,EAAE,CAC5E,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { DatabaseSync } from 'node:sqlite';
2
+ import { type EmbeddingModel } from 'ai';
3
+ import type { BatchRetriever } from '../batch-retriever.ts';
4
+ import type { RetrieveOptions } from '../retrieve-options.ts';
5
+ import type { ScoredPassage } from '../scored-passage.ts';
6
+ /**
7
+ * Ranks the passages by the cosine similarity of their vectors to the query,
8
+ * with `vec_distance_cosine` in the SQL of SQLite (ADR 0003). The host
9
+ * registers that function: it loads sqlite-vec, or calls `install()` of
10
+ * `@zukhruf/vectors/sqlite`.
11
+ */
12
+ export declare class SqliteEmbeddingRetriever implements BatchRetriever {
13
+ #private;
14
+ constructor(options: {
15
+ readonly database: DatabaseSync;
16
+ readonly passages: string;
17
+ readonly vectors: string;
18
+ readonly model: EmbeddingModel;
19
+ });
20
+ retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
21
+ retrieveMany(queries: readonly string[], options?: RetrieveOptions): Promise<ScoredPassage[][]>;
22
+ }
23
+ //# sourceMappingURL=sqlite-embedding-retriever.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-embedding-retriever.d.ts","sourceRoot":"","sources":["../../src/sqlite/sqlite-embedding-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,KAAK,cAAc,EAAoB,MAAM,IAAI,CAAC;AAK3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAQ5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAK1D;;;;;GAKG;AACH,qBAAa,wBAAyB,YAAW,cAAc;;IAM7D,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;QAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;KAChC,EAKA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAU1B;IAEK,YAAY,CAChB,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAU5B;CA6FF"}
@@ -0,0 +1,125 @@
1
+ import { embed, embedMany } from 'ai';
2
+ import { DimensionMismatchError, Vector } from '@zukhruf/vectors';
3
+ import { fromSql, toSql } from '@zukhruf/vectors/sqlite';
4
+ import { describe, modelName, parseModelIdentity, } from '../model-identity.js';
5
+ import { ModelMismatchError } from '../model-mismatch-error.js';
6
+ import { identifier } from './identifier.js';
7
+ import { passageRows } from './passage-rows.js';
8
+ import { where } from './where.js';
9
+ /**
10
+ * Ranks the passages by the cosine similarity of their vectors to the query,
11
+ * with `vec_distance_cosine` in the SQL of SQLite (ADR 0003). The host
12
+ * registers that function: it loads sqlite-vec, or calls `install()` of
13
+ * `@zukhruf/vectors/sqlite`.
14
+ */
15
+ export class SqliteEmbeddingRetriever {
16
+ #database;
17
+ #passages;
18
+ #vectors;
19
+ #model;
20
+ constructor(options) {
21
+ this.#database = options.database;
22
+ this.#passages = identifier(options.passages);
23
+ this.#vectors = identifier(options.vectors);
24
+ this.#model = options.model;
25
+ }
26
+ async retrieve(query, options = {}) {
27
+ const [passages] = await this.#retrieve(1, options, async (abortSignal) => {
28
+ const { embedding } = await embed({
29
+ model: this.#model,
30
+ value: query,
31
+ ...(abortSignal === undefined ? {} : { abortSignal }),
32
+ });
33
+ return [embedding];
34
+ });
35
+ return passages ?? [];
36
+ }
37
+ async retrieveMany(queries, options = {}) {
38
+ if (queries.length === 0)
39
+ return [];
40
+ return this.#retrieve(queries.length, options, async (abortSignal) => {
41
+ const { embeddings } = await embedMany({
42
+ model: this.#model,
43
+ values: [...queries],
44
+ ...(abortSignal === undefined ? {} : { abortSignal }),
45
+ });
46
+ return embeddings;
47
+ });
48
+ }
49
+ async #retrieve(count, options, embedQueries) {
50
+ options.abortSignal?.throwIfAborted();
51
+ const query = modelName(this.#model);
52
+ const stored = this.#storedIdentities();
53
+ const [first] = stored;
54
+ if (first === undefined)
55
+ return Array.from({ length: count }, () => []);
56
+ const sameModel = stored.filter(({ provider, modelId }) => provider === query.provider && modelId === query.modelId);
57
+ const [nearest] = sameModel;
58
+ if (nearest === undefined) {
59
+ throw new ModelMismatchError({ provider: first.provider, modelId: first.modelId }, query);
60
+ }
61
+ const vectors = await embedQueries(options.abortSignal);
62
+ options.abortSignal?.throwIfAborted();
63
+ const dimensions = vectors[0]?.length ?? 0;
64
+ const identity = sameModel.find((candidate) => candidate.dimensions === dimensions);
65
+ if (identity === undefined) {
66
+ throw new DimensionMismatchError(nearest.dimensions, dimensions);
67
+ }
68
+ return this.#search(vectors, identity, options);
69
+ }
70
+ /** The identities of the stored vectors, in the order of their text. */
71
+ #storedIdentities() {
72
+ return this.#database
73
+ .prepare(`SELECT DISTINCT "model" FROM ${this.#vectors} ORDER BY "model"`)
74
+ .all()
75
+ .map(({ model }) => parseModelIdentity(String(model)));
76
+ }
77
+ /**
78
+ * One SQL query for every query vector. It compares a query only with the
79
+ * vectors of its own model identity, and breaks a tie of distance by the
80
+ * passage id, as `VectorSearch` of `@zukhruf/embeddings` does.
81
+ */
82
+ #search(vectors, identity, options) {
83
+ const filter = where(options.filter);
84
+ const statement = this.#database.prepare(`
85
+ WITH "queries" ("query", "vector") AS (VALUES ${vectors.map(() => '(?, ?)').join(', ')}),
86
+ "scored" AS (
87
+ SELECT q."query", v."id", v."vector", vec_distance_cosine(v."vector", q."vector") AS "distance"
88
+ FROM "queries" q CROSS JOIN ${this.#vectors} v JOIN ${this.#passages} p ON p."id" = v."id"
89
+ WHERE v."model" = ? AND ${filter.sql}
90
+ ),
91
+ "ranked" AS (
92
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY "query" ORDER BY "distance", "id") AS "rank" FROM "scored"
93
+ )
94
+ SELECT r."query", r."distance", r."vector", p.*
95
+ FROM "ranked" r JOIN ${this.#passages} p ON p."id" = r."id"
96
+ WHERE r."rank" <= ?
97
+ ORDER BY r."query", r."rank"`);
98
+ const rows = passageRows(statement, 3, [
99
+ ...vectors.flatMap((vector, index) => [
100
+ index,
101
+ toSql(Vector.from(vector)),
102
+ ]),
103
+ describe(identity),
104
+ ...filter.parameters,
105
+ options.topK ?? 10,
106
+ ]);
107
+ const lists = vectors.map(() => []);
108
+ for (const { cells: [query, distance, vector], passage, } of rows) {
109
+ lists[Number(query)]?.push({
110
+ passage,
111
+ score: 1 - Number(distance),
112
+ ...(options.includeVectors === true
113
+ ? { vector: Array.from(fromSql(Vector, blob(vector)).values) }
114
+ : {}),
115
+ });
116
+ }
117
+ return lists;
118
+ }
119
+ }
120
+ function blob(value) {
121
+ if (value instanceof Uint8Array)
122
+ return value;
123
+ throw new TypeError(`A stored vector is a BLOB of float32 values, not ${String(value)}`);
124
+ }
125
+ //# sourceMappingURL=sqlite-embedding-retriever.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-embedding-retriever.js","sourceRoot":"","s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EAAE;oBAC9D,CAAC,CAAC,EAAE,CAAC;aACR,CAAC,CAAC;QACL,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAED,SAAS,IAAI,CAAC,KAAc;IAC1B,IAAI,KAAK,YAAY,UAAU;QAAE,OAAO,KAAK,CAAC;IAC9C,MAAM,IAAI,SAAS,CACjB,oDAAoD,MAAM,CAAC,KAAK,CAAC,EAAE,CACpE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { DatabaseSync } from 'node:sqlite';
2
+ import type { PassageStore } from '../passage-store.ts';
3
+ import type { Passage } from '../passage.ts';
4
+ /** Reads the passages of the passage contract in SQLite by id, and the neighbours of a passage in its source. */
5
+ export declare class SqlitePassageStore implements PassageStore {
6
+ #private;
7
+ constructor(options: {
8
+ readonly database: DatabaseSync;
9
+ readonly passages: string;
10
+ });
11
+ get(ids: readonly string[]): Promise<ReadonlyMap<string, Passage>>;
12
+ /**
13
+ * The passages of the source from `seq - windowSize` to `seq + windowSize`,
14
+ * the passage itself among them, in the order of `seq`, as Haystack's
15
+ * `SentenceWindowRetriever` reads its window.
16
+ */
17
+ neighbours(options: {
18
+ readonly source: string;
19
+ readonly seq: number;
20
+ readonly windowSize: number;
21
+ }): Promise<Passage[]>;
22
+ }
23
+ //# sourceMappingURL=sqlite-passage-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-passage-store.d.ts","sourceRoot":"","sources":["../../src/sqlite/sqlite-passage-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAI7C,iHAAiH;AACjH,qBAAa,kBAAmB,YAAW,YAAY;;IAIrD,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;QAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;KAC3B,EAGA;IAEK,GAAG,CAAC,GAAG,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAUvE;IAED;;;;OAIG;IACG,UAAU,CAAC,OAAO,EAAE;QACxB,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,CASrB;CACF"}
@@ -0,0 +1,32 @@
1
+ import { identifier } from './identifier.js';
2
+ import { passageRows } from './passage-rows.js';
3
+ /** Reads the passages of the passage contract in SQLite by id, and the neighbours of a passage in its source. */
4
+ export class SqlitePassageStore {
5
+ #database;
6
+ #passages;
7
+ constructor(options) {
8
+ this.#database = options.database;
9
+ this.#passages = identifier(options.passages);
10
+ }
11
+ async get(ids) {
12
+ const statement = this.#database.prepare(`SELECT p.* FROM ${this.#passages} p WHERE p."id" IN (SELECT "value" FROM json_each(?))`);
13
+ return new Map(passageRows(statement, 0, [JSON.stringify(ids)]).map(({ passage }) => [
14
+ passage.id,
15
+ passage,
16
+ ]));
17
+ }
18
+ /**
19
+ * The passages of the source from `seq - windowSize` to `seq + windowSize`,
20
+ * the passage itself among them, in the order of `seq`, as Haystack's
21
+ * `SentenceWindowRetriever` reads its window.
22
+ */
23
+ async neighbours(options) {
24
+ const statement = this.#database.prepare(`SELECT p.* FROM ${this.#passages} p WHERE p."source" = ? AND p."seq" BETWEEN ? AND ? ORDER BY p."seq", p."id"`);
25
+ return passageRows(statement, 0, [
26
+ options.source,
27
+ options.seq - options.windowSize,
28
+ options.seq + options.windowSize,
29
+ ]).map(({ passage }) => passage);
30
+ }
31
+ }
32
+ //# sourceMappingURL=sqlite-passage-store.js.map