vectra 0.10.0 → 0.11.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.
Files changed (65) hide show
  1. package/lib/FileFetcher.d.ts +5 -0
  2. package/lib/FileFetcher.d.ts.map +1 -0
  3. package/lib/FileFetcher.js +69 -0
  4. package/lib/FileFetcher.js.map +1 -0
  5. package/lib/GPT3Tokenizer.d.ts +9 -0
  6. package/lib/GPT3Tokenizer.d.ts.map +1 -0
  7. package/lib/GPT3Tokenizer.js +17 -0
  8. package/lib/GPT3Tokenizer.js.map +1 -0
  9. package/lib/ItemSelector.d.ts +41 -0
  10. package/lib/ItemSelector.d.ts.map +1 -0
  11. package/lib/ItemSelector.js +168 -0
  12. package/lib/ItemSelector.js.map +1 -0
  13. package/lib/LocalDocument.d.ts +54 -0
  14. package/lib/LocalDocument.d.ts.map +1 -0
  15. package/lib/LocalDocument.js +146 -0
  16. package/lib/LocalDocument.js.map +1 -0
  17. package/lib/LocalDocumentIndex.d.ts +132 -0
  18. package/lib/LocalDocumentIndex.d.ts.map +1 -0
  19. package/lib/LocalDocumentIndex.js +446 -0
  20. package/lib/LocalDocumentIndex.js.map +1 -0
  21. package/lib/LocalDocumentResult.d.ts +45 -0
  22. package/lib/LocalDocumentResult.d.ts.map +1 -0
  23. package/lib/LocalDocumentResult.js +328 -0
  24. package/lib/LocalDocumentResult.js.map +1 -0
  25. package/lib/LocalIndex.d.ts +139 -0
  26. package/lib/LocalIndex.d.ts.map +1 -0
  27. package/lib/LocalIndex.js +476 -0
  28. package/lib/LocalIndex.js.map +1 -0
  29. package/lib/OpenAIEmbeddings.d.ts +126 -0
  30. package/lib/OpenAIEmbeddings.d.ts.map +1 -0
  31. package/lib/OpenAIEmbeddings.js +174 -0
  32. package/lib/OpenAIEmbeddings.js.map +1 -0
  33. package/lib/TextSplitter.d.ts +20 -0
  34. package/lib/TextSplitter.d.ts.map +1 -0
  35. package/lib/TextSplitter.js +537 -0
  36. package/lib/TextSplitter.js.map +1 -0
  37. package/lib/WebFetcher.d.ts +15 -0
  38. package/lib/WebFetcher.d.ts.map +1 -0
  39. package/lib/WebFetcher.js +224 -0
  40. package/lib/WebFetcher.js.map +1 -0
  41. package/lib/index.d.ts +12 -0
  42. package/lib/index.d.ts.map +1 -0
  43. package/lib/index.js +28 -0
  44. package/lib/index.js.map +1 -0
  45. package/lib/internals/Colorize.d.ts +14 -0
  46. package/lib/internals/Colorize.d.ts.map +1 -0
  47. package/lib/internals/Colorize.js +64 -0
  48. package/lib/internals/Colorize.js.map +1 -0
  49. package/lib/internals/index.d.ts +3 -0
  50. package/lib/internals/index.d.ts.map +1 -0
  51. package/lib/internals/index.js +19 -0
  52. package/lib/internals/index.js.map +1 -0
  53. package/lib/internals/types.d.ts +43 -0
  54. package/lib/internals/types.d.ts.map +1 -0
  55. package/lib/internals/types.js +3 -0
  56. package/lib/internals/types.js.map +1 -0
  57. package/lib/types.d.ts +146 -0
  58. package/lib/types.d.ts.map +1 -0
  59. package/lib/types.js +3 -0
  60. package/lib/types.js.map +1 -0
  61. package/lib/vectra-cli.d.ts +2 -0
  62. package/lib/vectra-cli.d.ts.map +1 -0
  63. package/lib/vectra-cli.js +314 -0
  64. package/lib/vectra-cli.js.map +1 -0
  65. package/package.json +22 -19
@@ -0,0 +1,5 @@
1
+ import { TextFetcher } from './types';
2
+ export declare class FileFetcher implements TextFetcher {
3
+ fetch(uri: string, onDocument: (uri: string, text: string, docType?: string | undefined) => Promise<boolean>): Promise<boolean>;
4
+ }
5
+ //# sourceMappingURL=FileFetcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FileFetcher.d.ts","sourceRoot":"","sources":["../src/FileFetcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAItC,qBAAa,WAAY,YAAW,WAAW;IAC9B,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;CAyB/I"}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.FileFetcher = void 0;
36
+ const fs = __importStar(require("fs/promises"));
37
+ const path = __importStar(require("path"));
38
+ class FileFetcher {
39
+ fetch(uri, onDocument) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ // Does path exist and is it a directory?
42
+ let isDirectory;
43
+ try {
44
+ const stat = yield fs.stat(uri);
45
+ isDirectory = stat.isDirectory();
46
+ }
47
+ catch (_a) {
48
+ return true;
49
+ }
50
+ // If directory, read all files and recurse
51
+ if (isDirectory) {
52
+ const files = yield fs.readdir(uri);
53
+ for (const file of files) {
54
+ const filePath = path.join(uri, file);
55
+ yield this.fetch(filePath, onDocument);
56
+ }
57
+ return true;
58
+ }
59
+ else {
60
+ // Read file and call onDocument
61
+ const text = yield fs.readFile(uri, 'utf8');
62
+ const parts = uri.split('.');
63
+ return yield onDocument(uri, text, parts.length > 0 ? parts[parts.length - 1].toLowerCase() : undefined);
64
+ }
65
+ });
66
+ }
67
+ }
68
+ exports.FileFetcher = FileFetcher;
69
+ //# sourceMappingURL=FileFetcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FileFetcher.js","sourceRoot":"","sources":["../src/FileFetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,gDAAkC;AAClC,2CAA6B;AAE7B,MAAa,WAAW;IACP,KAAK,CAAC,GAAW,EAAE,UAAyF;;YACrH,yCAAyC;YACzC,IAAI,WAAoB,CAAC;YACzB,IAAI;gBACA,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAChC,WAAW,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;aACpC;YAAC,WAAM;gBACJ,OAAO,IAAI,CAAC;aACf;YAED,2CAA2C;YAC3C,IAAI,WAAW,EAAE;gBACb,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;gBACpC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;oBACtB,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;oBACtC,MAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;iBAC1C;gBACD,OAAO,IAAI,CAAC;aACf;iBAAM;gBACH,gCAAgC;gBAChC,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;gBAC5C,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAC7B,OAAO,MAAM,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;aAC5G;QACL,CAAC;KAAA;CACJ;AA1BD,kCA0BC"}
@@ -0,0 +1,9 @@
1
+ import { Tokenizer } from "./types";
2
+ /**
3
+ * Tokenizer that uses GPT-3's encoder.
4
+ */
5
+ export declare class GPT3Tokenizer implements Tokenizer {
6
+ decode(tokens: number[]): string;
7
+ encode(text: string): number[];
8
+ }
9
+ //# sourceMappingURL=GPT3Tokenizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GPT3Tokenizer.d.ts","sourceRoot":"","sources":["../src/GPT3Tokenizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAGpC;;GAEG;AACH,qBAAa,aAAc,YAAW,SAAS;IACpC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM;IAIhC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE;CAGxC"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GPT3Tokenizer = void 0;
4
+ const gpt_3_encoder_1 = require("gpt-3-encoder");
5
+ /**
6
+ * Tokenizer that uses GPT-3's encoder.
7
+ */
8
+ class GPT3Tokenizer {
9
+ decode(tokens) {
10
+ return (0, gpt_3_encoder_1.decode)(tokens);
11
+ }
12
+ encode(text) {
13
+ return (0, gpt_3_encoder_1.encode)(text);
14
+ }
15
+ }
16
+ exports.GPT3Tokenizer = GPT3Tokenizer;
17
+ //# sourceMappingURL=GPT3Tokenizer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GPT3Tokenizer.js","sourceRoot":"","sources":["../src/GPT3Tokenizer.ts"],"names":[],"mappings":";;;AACA,iDAA+C;AAE/C;;GAEG;AACH,MAAa,aAAa;IACf,MAAM,CAAC,MAAgB;QAC1B,OAAO,IAAA,sBAAM,EAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAEM,MAAM,CAAC,IAAY;QACtB,OAAO,IAAA,sBAAM,EAAC,IAAI,CAAC,CAAC;IACxB,CAAC;CACJ;AARD,sCAQC"}
@@ -0,0 +1,41 @@
1
+ import { MetadataFilter, MetadataTypes } from './types';
2
+ export declare class ItemSelector {
3
+ /**
4
+ * Returns the similarity between two vectors using the cosine similarity.
5
+ * @param vector1 Vector 1
6
+ * @param vector2 Vector 2
7
+ * @returns Similarity between the two vectors
8
+ */
9
+ static cosineSimilarity(vector1: number[], vector2: number[]): number;
10
+ /**
11
+ * Normalizes a vector.
12
+ * @remarks
13
+ * The norm of a vector is the square root of the sum of the squares of the elements.
14
+ * The LocalIndex pre-normalizes all vectors to improve performance.
15
+ * @param vector Vector to normalize
16
+ * @returns Normalized vector
17
+ */
18
+ static normalize(vector: number[]): number;
19
+ /**
20
+ * Returns the similarity between two vectors using cosine similarity.
21
+ * @remarks
22
+ * The LocalIndex pre-normalizes all vectors to improve performance.
23
+ * This method uses the pre-calculated norms to improve performance.
24
+ * @param vector1 Vector 1
25
+ * @param norm1 Norm of vector 1
26
+ * @param vector2 Vector 2
27
+ * @param norm2 Norm of vector 2
28
+ * @returns Similarity between the two vectors
29
+ */
30
+ static normalizedCosineSimilarity(vector1: number[], norm1: number, vector2: number[], norm2: number): number;
31
+ /**
32
+ * Applies a filter to the metadata of an item.
33
+ * @param metadata Metadata of the item
34
+ * @param filter Filter to apply
35
+ * @returns True if the item matches the filter, false otherwise
36
+ */
37
+ static select(metadata: Record<string, MetadataTypes>, filter: MetadataFilter): boolean;
38
+ private static dotProduct;
39
+ private static metadataFilter;
40
+ }
41
+ //# sourceMappingURL=ItemSelector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ItemSelector.d.ts","sourceRoot":"","sources":["../src/ItemSelector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExD,qBAAa,YAAY;IACrB;;;;;OAKG;WACW,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE;IAKnE;;;;;;;OAOG;WACW,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE;IAYxC;;;;;;;;;;OAUG;WACW,0BAA0B,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM;IAK3G;;;;;OAKG;WACW,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO;IAoC9F,OAAO,CAAC,MAAM,CAAC,UAAU;IAYzB,OAAO,CAAC,MAAM,CAAC,cAAc;CA+DhC"}
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ItemSelector = void 0;
4
+ class ItemSelector {
5
+ /**
6
+ * Returns the similarity between two vectors using the cosine similarity.
7
+ * @param vector1 Vector 1
8
+ * @param vector2 Vector 2
9
+ * @returns Similarity between the two vectors
10
+ */
11
+ static cosineSimilarity(vector1, vector2) {
12
+ // Return the quotient of the dot product and the product of the norms
13
+ return this.dotProduct(vector1, vector2) / (this.normalize(vector1) * this.normalize(vector2));
14
+ }
15
+ /**
16
+ * Normalizes a vector.
17
+ * @remarks
18
+ * The norm of a vector is the square root of the sum of the squares of the elements.
19
+ * The LocalIndex pre-normalizes all vectors to improve performance.
20
+ * @param vector Vector to normalize
21
+ * @returns Normalized vector
22
+ */
23
+ static normalize(vector) {
24
+ // Initialize a variable to store the sum of the squares
25
+ let sum = 0;
26
+ // Loop through the elements of the array
27
+ for (let i = 0; i < vector.length; i++) {
28
+ // Square the element and add it to the sum
29
+ sum += vector[i] * vector[i];
30
+ }
31
+ // Return the square root of the sum
32
+ return Math.sqrt(sum);
33
+ }
34
+ /**
35
+ * Returns the similarity between two vectors using cosine similarity.
36
+ * @remarks
37
+ * The LocalIndex pre-normalizes all vectors to improve performance.
38
+ * This method uses the pre-calculated norms to improve performance.
39
+ * @param vector1 Vector 1
40
+ * @param norm1 Norm of vector 1
41
+ * @param vector2 Vector 2
42
+ * @param norm2 Norm of vector 2
43
+ * @returns Similarity between the two vectors
44
+ */
45
+ static normalizedCosineSimilarity(vector1, norm1, vector2, norm2) {
46
+ // Return the quotient of the dot product and the product of the norms
47
+ return this.dotProduct(vector1, vector2) / (norm1 * norm2);
48
+ }
49
+ /**
50
+ * Applies a filter to the metadata of an item.
51
+ * @param metadata Metadata of the item
52
+ * @param filter Filter to apply
53
+ * @returns True if the item matches the filter, false otherwise
54
+ */
55
+ static select(metadata, filter) {
56
+ if (filter === undefined || filter === null) {
57
+ return true;
58
+ }
59
+ for (const key in filter) {
60
+ switch (key) {
61
+ case '$and':
62
+ if (!filter[key].every((f) => this.select(metadata, f))) {
63
+ return false;
64
+ }
65
+ break;
66
+ case '$or':
67
+ if (!filter[key].some((f) => this.select(metadata, f))) {
68
+ return false;
69
+ }
70
+ break;
71
+ default:
72
+ const value = filter[key];
73
+ if (value === undefined || value === null) {
74
+ return false;
75
+ }
76
+ else if (typeof value == 'object') {
77
+ if (!this.metadataFilter(metadata[key], value)) {
78
+ return false;
79
+ }
80
+ }
81
+ else {
82
+ if (metadata[key] !== value) {
83
+ return false;
84
+ }
85
+ }
86
+ break;
87
+ }
88
+ }
89
+ return true;
90
+ }
91
+ static dotProduct(arr1, arr2) {
92
+ // Initialize a variable to store the sum of the products
93
+ let sum = 0;
94
+ // Loop through the elements of the arrays
95
+ for (let i = 0; i < arr1.length; i++) {
96
+ // Multiply the corresponding elements and add them to the sum
97
+ sum += arr1[i] * arr2[i];
98
+ }
99
+ // Return the sum
100
+ return sum;
101
+ }
102
+ static metadataFilter(value, filter) {
103
+ if (value === undefined || value === null) {
104
+ return false;
105
+ }
106
+ for (const key in filter) {
107
+ switch (key) {
108
+ case '$eq':
109
+ if (value !== filter[key]) {
110
+ return false;
111
+ }
112
+ break;
113
+ case '$ne':
114
+ if (value === filter[key]) {
115
+ return false;
116
+ }
117
+ break;
118
+ case '$gt':
119
+ if (typeof value != 'number' || value <= filter[key]) {
120
+ return false;
121
+ }
122
+ break;
123
+ case '$gte':
124
+ if (typeof value != 'number' || value < filter[key]) {
125
+ return false;
126
+ }
127
+ break;
128
+ case '$lt':
129
+ if (typeof value != 'number' || value >= filter[key]) {
130
+ return false;
131
+ }
132
+ break;
133
+ case '$lte':
134
+ if (typeof value != 'number' || value > filter[key]) {
135
+ return false;
136
+ }
137
+ break;
138
+ case '$in':
139
+ if (typeof value == 'boolean') {
140
+ return false;
141
+ }
142
+ else if (typeof value == 'string' && !filter[key].includes(value)) {
143
+ return false;
144
+ }
145
+ else if (!filter[key].some(val => typeof val == 'string' && val.includes(value))) {
146
+ return false;
147
+ }
148
+ break;
149
+ case '$nin':
150
+ if (typeof value == 'boolean') {
151
+ return false;
152
+ }
153
+ else if (typeof value == 'string' && filter[key].includes(value)) {
154
+ return false;
155
+ }
156
+ else if (filter[key].some(val => typeof val == 'string' && val.includes(value))) {
157
+ return false;
158
+ }
159
+ break;
160
+ default:
161
+ return value === filter[key];
162
+ }
163
+ }
164
+ return true;
165
+ }
166
+ }
167
+ exports.ItemSelector = ItemSelector;
168
+ //# sourceMappingURL=ItemSelector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ItemSelector.js","sourceRoot":"","sources":["../src/ItemSelector.ts"],"names":[],"mappings":";;;AAEA,MAAa,YAAY;IACrB;;;;;OAKG;IACI,MAAM,CAAC,gBAAgB,CAAC,OAAiB,EAAE,OAAiB;QAC/D,sEAAsE;QACtE,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IACnG,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,SAAS,CAAC,MAAgB;QACpC,wDAAwD;QACxD,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,yCAAyC;QACzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACpC,2CAA2C;YAC3C,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;SAChC;QACD,oCAAoC;QACpC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED;;;;;;;;;;OAUG;IACI,MAAM,CAAC,0BAA0B,CAAC,OAAiB,EAAE,KAAa,EAAE,OAAiB,EAAE,KAAa;QACvG,sEAAsE;QACtE,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;IAC/D,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,QAAuC,EAAE,MAAsB;QAChF,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,IAAI,EAAE;YACzC,OAAO,IAAI,CAAC;SACf;QAED,KAAK,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B;yBAAM,IAAG,OAAO,KAAK,IAAI,QAAQ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAC;wBAChE,OAAO,KAAK,CAAA;qBACf;yBAAM,IAAG,CAAC,MAAM,CAAC,GAAG,CAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,GAAG,IAAI,QAAQ,IAAI,GAAG,CAAC,QAAQ,CAAC,KAAe,CAAC,CAAC,EAAC;wBACzF,OAAO,KAAK,CAAA;qBACf;oBACD,MAAM;gBACV,KAAK,MAAM;oBACP,IAAI,OAAO,KAAK,IAAI,SAAS,EAAE;wBAC3B,OAAO,KAAK,CAAC;qBAChB;yBACI,IAAI,OAAO,KAAK,IAAI,QAAQ,IAAI,MAAM,CAAC,GAAG,CAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;wBAC/D,OAAO,KAAK,CAAC;qBAChB;yBACI,IAAI,MAAM,CAAC,GAAG,CAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,GAAG,IAAI,QAAQ,IAAI,GAAG,CAAC,QAAQ,CAAC,KAAe,CAAC,CAAC,EAAE;wBACxF,OAAO,KAAK,CAAC;qBAChB;oBACD,MAAM;gBACV;oBACI,OAAO,KAAK,KAAK,MAAM,CAAC,GAAG,CAAC,CAAC;aACpC;SACJ;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AArKD,oCAqKC"}
@@ -0,0 +1,54 @@
1
+ import { MetadataTypes } from './types';
2
+ import { LocalDocumentIndex } from './LocalDocumentIndex';
3
+ /**
4
+ * Represents an indexed document stored on disk.
5
+ */
6
+ export declare class LocalDocument {
7
+ private readonly _index;
8
+ private readonly _id;
9
+ private readonly _uri;
10
+ private _metadata;
11
+ private _text;
12
+ /**
13
+ * Creates a new `LocalDocument` instance.
14
+ * @param index Parent index that contains the document.
15
+ * @param id ID of the document.
16
+ * @param uri URI of the document.
17
+ */
18
+ constructor(index: LocalDocumentIndex, id: string, uri: string);
19
+ /**
20
+ * Returns the folder path where the document is stored.
21
+ */
22
+ get folderPath(): string;
23
+ /**
24
+ * Returns the ID of the document.
25
+ */
26
+ get id(): string;
27
+ /**
28
+ * Returns the URI of the document.
29
+ */
30
+ get uri(): string;
31
+ /**
32
+ * Returns the length of the document in tokens.
33
+ * @remarks
34
+ * This value will be estimated for documents longer then 40k bytes.
35
+ * @returns Length of the document in tokens.
36
+ */
37
+ getLength(): Promise<number>;
38
+ /**
39
+ * Determines if the document has additional metadata storred on disk.
40
+ * @returns True if the document has metadata; otherwise, false.
41
+ */
42
+ hasMetadata(): Promise<boolean>;
43
+ /**
44
+ * Loads the metadata for the document from disk.
45
+ * @returns Metadata for the document.
46
+ */
47
+ loadMetadata(): Promise<Record<string, MetadataTypes>>;
48
+ /**
49
+ * Loads the text for the document from disk.
50
+ * @returns Text for the document.
51
+ */
52
+ loadText(): Promise<string>;
53
+ }
54
+ //# sourceMappingURL=LocalDocument.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalDocument.d.ts","sourceRoot":"","sources":["../src/LocalDocument.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;GAEG;AACH,qBAAa,aAAa;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqB;IAC5C,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,SAAS,CAAyC;IAC1D,OAAO,CAAC,KAAK,CAAmB;IAEhC;;;;;OAKG;gBACgB,KAAK,EAAE,kBAAkB,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAMrE;;OAEG;IACH,IAAW,UAAU,IAAI,MAAM,CAE9B;IAED;;OAEG;IACH,IAAW,EAAE,IAAI,MAAM,CAEtB;IAED;;OAEG;IACH,IAAW,GAAG,IAAI,MAAM,CAEvB;IAED;;;;;OAKG;IACU,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC;IASzC;;;OAGG;IACU,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAS5C;;;OAGG;IACU,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAC,aAAa,CAAC,CAAC;IAmBlE;;;OAGG;IACU,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC;CAW3C"}
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.LocalDocument = void 0;
36
+ const fs = __importStar(require("fs/promises"));
37
+ const path = __importStar(require("path"));
38
+ /**
39
+ * Represents an indexed document stored on disk.
40
+ */
41
+ class LocalDocument {
42
+ /**
43
+ * Creates a new `LocalDocument` instance.
44
+ * @param index Parent index that contains the document.
45
+ * @param id ID of the document.
46
+ * @param uri URI of the document.
47
+ */
48
+ constructor(index, id, uri) {
49
+ this._index = index;
50
+ this._id = id;
51
+ this._uri = uri;
52
+ }
53
+ /**
54
+ * Returns the folder path where the document is stored.
55
+ */
56
+ get folderPath() {
57
+ return this._index.folderPath;
58
+ }
59
+ /**
60
+ * Returns the ID of the document.
61
+ */
62
+ get id() {
63
+ return this._id;
64
+ }
65
+ /**
66
+ * Returns the URI of the document.
67
+ */
68
+ get uri() {
69
+ return this._uri;
70
+ }
71
+ /**
72
+ * Returns the length of the document in tokens.
73
+ * @remarks
74
+ * This value will be estimated for documents longer then 40k bytes.
75
+ * @returns Length of the document in tokens.
76
+ */
77
+ getLength() {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const text = yield this.loadText();
80
+ if (text.length <= 40000) {
81
+ return this._index.tokenizer.encode(text).length;
82
+ }
83
+ else {
84
+ return Math.ceil(text.length / 4);
85
+ }
86
+ });
87
+ }
88
+ /**
89
+ * Determines if the document has additional metadata storred on disk.
90
+ * @returns True if the document has metadata; otherwise, false.
91
+ */
92
+ hasMetadata() {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ try {
95
+ yield fs.access(path.join(this.folderPath, `${this.id}.json`));
96
+ return true;
97
+ }
98
+ catch (err) {
99
+ return false;
100
+ }
101
+ });
102
+ }
103
+ /**
104
+ * Loads the metadata for the document from disk.
105
+ * @returns Metadata for the document.
106
+ */
107
+ loadMetadata() {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ if (this._metadata == undefined) {
110
+ let json;
111
+ try {
112
+ json = (yield fs.readFile(path.join(this.folderPath, `${this.id}.json`))).toString();
113
+ }
114
+ catch (err) {
115
+ throw new Error(`Error reading metadata for document "${this.uri}": ${err.toString()}`);
116
+ }
117
+ try {
118
+ this._metadata = JSON.parse(json);
119
+ }
120
+ catch (err) {
121
+ throw new Error(`Error parsing metadata for document "${this.uri}": ${err.toString()}`);
122
+ }
123
+ }
124
+ return this._metadata;
125
+ });
126
+ }
127
+ /**
128
+ * Loads the text for the document from disk.
129
+ * @returns Text for the document.
130
+ */
131
+ loadText() {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ if (this._text == undefined) {
134
+ try {
135
+ this._text = (yield fs.readFile(path.join(this.folderPath, `${this.id}.txt`))).toString();
136
+ }
137
+ catch (err) {
138
+ throw new Error(`Error reading text file for document "${this.uri}": ${err.toString()}`);
139
+ }
140
+ }
141
+ return this._text;
142
+ });
143
+ }
144
+ }
145
+ exports.LocalDocument = LocalDocument;
146
+ //# sourceMappingURL=LocalDocument.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalDocument.js","sourceRoot":"","sources":["../src/LocalDocument.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAkC;AAClC,2CAA6B;AAI7B;;GAEG;AACH,MAAa,aAAa;IAOtB;;;;;OAKG;IACH,YAAmB,KAAyB,EAAE,EAAU,EAAE,GAAW;QACjE,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACd,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;IACpB,CAAC;IAED;;OAEG;IACH,IAAW,UAAU;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,IAAW,EAAE;QACT,OAAO,IAAI,CAAC,GA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