vectra 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,392 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.LocalIndex = void 0;
32
+ const fs = __importStar(require("fs/promises"));
33
+ const path = __importStar(require("path"));
34
+ const uuid_1 = require("uuid");
35
+ const ItemSelector_1 = require("./ItemSelector");
36
+ /**
37
+ * Local vector index instance.
38
+ * @remarks
39
+ * This class is used to create, update, and query a local vector index.
40
+ * Each index is a folder on disk containing an index.json file and an optional set of metadata files.
41
+ */
42
+ class LocalIndex {
43
+ /**
44
+ * Creates a new instance of LocalIndex.
45
+ * @param folderPath - Path to the index folder
46
+ */
47
+ constructor(folderPath) {
48
+ this._folderPath = folderPath;
49
+ }
50
+ /**
51
+ * Begins an update to the index.
52
+ * @remarks
53
+ * This method loads the index into memory and prepares it for updates.
54
+ */
55
+ beginUpdate() {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ if (this._update) {
58
+ throw new Error('Update already in progress');
59
+ }
60
+ yield this.loadIndexData();
61
+ this._update = Object.assign({}, this._data);
62
+ });
63
+ }
64
+ /**
65
+ * Cancels an update to the index.
66
+ * @remarks
67
+ * This method discards any changes made to the index since the update began.
68
+ */
69
+ cancelUpdate() {
70
+ this._update = undefined;
71
+ }
72
+ /**
73
+ * Creates a new index.
74
+ * @remarks
75
+ * This method creates a new folder on disk containing an index.json file.
76
+ * @param config - Index configuration
77
+ */
78
+ createIndex(config = { version: 1 }) {
79
+ var _a;
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ // Delete if exists
82
+ if (yield this.isIndexCreated()) {
83
+ if (config.deleteIfExists) {
84
+ yield this.deleteIndex();
85
+ }
86
+ else {
87
+ throw new Error('Index already exists');
88
+ }
89
+ }
90
+ try {
91
+ // Create folder for index
92
+ yield fs.mkdir(this._folderPath, { recursive: true });
93
+ // Initialize index.json file
94
+ this._data = {
95
+ version: config.version,
96
+ metadata_config: (_a = config.metadata_config) !== null && _a !== void 0 ? _a : {},
97
+ items: []
98
+ };
99
+ yield fs.writeFile(path.join(this._folderPath, 'index.json'), JSON.stringify(this._data));
100
+ }
101
+ catch (err) {
102
+ yield this.deleteIndex();
103
+ throw new Error('Error creating index');
104
+ }
105
+ });
106
+ }
107
+ /**
108
+ * Deletes the index.
109
+ * @remarks
110
+ * This method deletes the index folder from disk.
111
+ */
112
+ deleteIndex() {
113
+ this._data = undefined;
114
+ return fs.rm(this._folderPath, {
115
+ recursive: true,
116
+ maxRetries: 3
117
+ });
118
+ }
119
+ /**
120
+ * Deletes an item from the index.
121
+ * @param id - Item id
122
+ */
123
+ deleteItem(id) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ if (this._update) {
126
+ const index = this._update.items.findIndex(i => i.id === id);
127
+ if (index >= 0) {
128
+ this._update.items.splice(index, 1);
129
+ }
130
+ }
131
+ else {
132
+ yield this.beginUpdate();
133
+ const index = this._update.items.findIndex(i => i.id === id);
134
+ if (index >= 0) {
135
+ this._update.items.splice(index, 1);
136
+ }
137
+ yield this.endUpdate();
138
+ }
139
+ });
140
+ }
141
+ /**
142
+ * Ends an update to the index.
143
+ * @remarks
144
+ * This method saves the index to disk.
145
+ */
146
+ endUpdate() {
147
+ return __awaiter(this, void 0, void 0, function* () {
148
+ if (!this._update) {
149
+ throw new Error('No update in progress');
150
+ }
151
+ try {
152
+ // Save index
153
+ yield fs.writeFile(path.join(this._folderPath, 'index.json'), JSON.stringify(this._update));
154
+ this._data = this._update;
155
+ this._update = undefined;
156
+ }
157
+ catch (err) {
158
+ throw new Error(`Error saving index: ${err.toString()}`);
159
+ }
160
+ });
161
+ }
162
+ /**
163
+ * Loads an index from disk and returns its stats.
164
+ * @returns Index stats
165
+ */
166
+ getIndexStats() {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ yield this.loadIndexData();
169
+ return {
170
+ version: this._data.version,
171
+ metadata_config: this._data.metadata_config,
172
+ items: this._data.items.length
173
+ };
174
+ });
175
+ }
176
+ /**
177
+ * Returns an item from the index given its ID.
178
+ * @param id Item id
179
+ * @returns Item or undefined if not found
180
+ */
181
+ getItem(id) {
182
+ return __awaiter(this, void 0, void 0, function* () {
183
+ yield this.loadIndexData();
184
+ return this._data.items.find(i => i.id === id);
185
+ });
186
+ }
187
+ /**
188
+ * Adds an item to the index.
189
+ * @remarks
190
+ * A new update is started if one is not already in progress. If an item with the same ID
191
+ * already exists, an error will be thrown.
192
+ * @param item Item to insert
193
+ * @returns Inserted item
194
+ */
195
+ insertItem(item) {
196
+ return __awaiter(this, void 0, void 0, function* () {
197
+ if (this._update) {
198
+ return yield this.addItemToUpdate(item, true);
199
+ }
200
+ else {
201
+ yield this.beginUpdate();
202
+ const newItem = yield this.addItemToUpdate(item, true);
203
+ yield this.endUpdate();
204
+ return newItem;
205
+ }
206
+ });
207
+ }
208
+ /**
209
+ * Returns true if the index exists.
210
+ */
211
+ isIndexCreated() {
212
+ return __awaiter(this, void 0, void 0, function* () {
213
+ try {
214
+ yield fs.access(path.join(this._folderPath, 'index.json'));
215
+ return true;
216
+ }
217
+ catch (err) {
218
+ return false;
219
+ }
220
+ });
221
+ }
222
+ /**
223
+ * Returns all items in the index.
224
+ * @remarks
225
+ * This method loads the index into memory and returns all its items. A copy of the items
226
+ * array is returned so no modifications should be made to the array.
227
+ * @returns All items in the index
228
+ */
229
+ listItems() {
230
+ return __awaiter(this, void 0, void 0, function* () {
231
+ yield this.loadIndexData();
232
+ return this._data.items.slice();
233
+ });
234
+ }
235
+ /**
236
+ * Returns all items in the index matching the filter.
237
+ * @remarks
238
+ * This method loads the index into memory and returns all its items matching the filter.
239
+ * @param filter Filter to apply
240
+ * @returns Items matching the filter
241
+ */
242
+ listItemsByMetadata(filter) {
243
+ return __awaiter(this, void 0, void 0, function* () {
244
+ yield this.loadIndexData();
245
+ return this._data.items.filter(i => ItemSelector_1.ItemSelector.select(i.metadata, filter));
246
+ });
247
+ }
248
+ /**
249
+ * Finds the top k items in the index that are most similar to the vector.
250
+ * @remarks
251
+ * This method loads the index into memory and returns the top k items that are most similar.
252
+ * An optional filter can be applied to the metadata of the items.
253
+ * @param vector Vector to query against
254
+ * @param topK Number of items to return
255
+ * @param filter Optional filter to apply
256
+ * @returns Similar items to the vector that matches the filter
257
+ */
258
+ queryItems(vector, topK, filter) {
259
+ return __awaiter(this, void 0, void 0, function* () {
260
+ yield this.loadIndexData();
261
+ // Filter items
262
+ let items = this._data.items;
263
+ if (filter) {
264
+ items = items.filter(i => ItemSelector_1.ItemSelector.select(i.metadata, filter));
265
+ }
266
+ // Calculate distances
267
+ const norm = ItemSelector_1.ItemSelector.normalize(vector);
268
+ const distances = [];
269
+ for (let i = 0; i < items.length; i++) {
270
+ const item = items[i];
271
+ const distance = ItemSelector_1.ItemSelector.normalizedCosineSimilarity(vector, norm, item.vector, item.norm);
272
+ distances.push({ index: i, distance: distance });
273
+ }
274
+ // Sort by distance DESCENDING
275
+ distances.sort((a, b) => b.distance - a.distance);
276
+ // Find top k
277
+ const top = distances.slice(0, topK).map(d => {
278
+ return {
279
+ item: Object.assign({}, items[d.index]),
280
+ score: d.distance
281
+ };
282
+ });
283
+ // Load external metadata
284
+ for (const item of top) {
285
+ if (item.item.metadataFile) {
286
+ const metadataPath = path.join(this._folderPath, item.item.metadataFile);
287
+ const metadata = yield fs.readFile(metadataPath);
288
+ item.item.metadata = JSON.parse(metadata.toString());
289
+ }
290
+ }
291
+ return top;
292
+ });
293
+ }
294
+ /**
295
+ * Adds or replaces an item in the index.
296
+ * @remarks
297
+ * A new update is started if one is not already in progress. If an item with the same ID
298
+ * already exists, it will be replaced.
299
+ * @param item Item to insert or replace
300
+ * @returns Upserted item
301
+ */
302
+ upsertItem(item) {
303
+ return __awaiter(this, void 0, void 0, function* () {
304
+ if (this._update) {
305
+ return yield this.addItemToUpdate(item, false);
306
+ }
307
+ else {
308
+ yield this.beginUpdate();
309
+ const newItem = yield this.addItemToUpdate(item, false);
310
+ yield this.endUpdate();
311
+ return newItem;
312
+ }
313
+ });
314
+ }
315
+ loadIndexData() {
316
+ return __awaiter(this, void 0, void 0, function* () {
317
+ if (this._data) {
318
+ return;
319
+ }
320
+ if (!(yield this.isIndexCreated())) {
321
+ throw new Error('Index does not exist');
322
+ }
323
+ const data = yield fs.readFile(path.join(this._folderPath, 'index.json'));
324
+ this._data = JSON.parse(data.toString());
325
+ });
326
+ }
327
+ addItemToUpdate(item, unique) {
328
+ var _a;
329
+ return __awaiter(this, void 0, void 0, function* () {
330
+ // Ensure vector is provided
331
+ if (!item.vector) {
332
+ throw new Error('Vector is required');
333
+ }
334
+ // Ensure unique
335
+ const id = (_a = item.id) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)();
336
+ if (unique) {
337
+ const existing = this._update.items.find(i => i.id === id);
338
+ if (existing) {
339
+ throw new Error(`Item with id ${id} already exists`);
340
+ }
341
+ }
342
+ // Check for indexed metadata
343
+ let metadata = {};
344
+ let metadataFile;
345
+ if (this._update.metadata_config.indexed && this._update.metadata_config.indexed.length > 0 && item.metadata) {
346
+ // Copy only indexed metadata
347
+ for (const key of this._update.metadata_config.indexed) {
348
+ if (item.metadata && item.metadata[key]) {
349
+ metadata[key] = item.metadata[key];
350
+ }
351
+ }
352
+ // Save remaining metadata to disk
353
+ metadataFile = `${uuid_1.v4}.json`;
354
+ const metadataPath = path.join(this._folderPath, metadataFile);
355
+ yield fs.writeFile(metadataPath, JSON.stringify(item.metadata));
356
+ }
357
+ else if (item.metadata) {
358
+ metadata = item.metadata;
359
+ }
360
+ // Create new item
361
+ const newItem = {
362
+ id: id,
363
+ metadata: metadata,
364
+ vector: item.vector,
365
+ norm: ItemSelector_1.ItemSelector.normalize(item.vector)
366
+ };
367
+ if (metadataFile) {
368
+ newItem.metadataFile = metadataFile;
369
+ }
370
+ // Add item to index
371
+ if (!unique) {
372
+ const existing = this._update.items.find(i => i.id === id);
373
+ if (existing) {
374
+ existing.metadata = newItem.metadata;
375
+ existing.vector = newItem.vector;
376
+ existing.metadataFile = newItem.metadataFile;
377
+ return existing;
378
+ }
379
+ else {
380
+ this._update.items.push(newItem);
381
+ return newItem;
382
+ }
383
+ }
384
+ else {
385
+ this._update.items.push(newItem);
386
+ return newItem;
387
+ }
388
+ });
389
+ }
390
+ }
391
+ exports.LocalIndex = LocalIndex;
392
+ //# sourceMappingURL=LocalIndex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalIndex.js","sourceRoot":"","sources":["../src/LocalIndex.ts"],"names":[],"mappings":";;;;;;;;;;;;;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I,CAAC,MAAM;gBACnB,IAAI,EAAE,2BAAY,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC;aAC5C,CAAC;YACF,IAAI,YAAY,EAAE;gBACd,OAAO,CAAC,YAAY,GAAG,YAAY,CAAC;aACvC;YAED,oBAAoB;YACpB,IAAI,CAAC,MAAM,EAAE;gBACT,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBAC5D,IAAI,QAAQ,EAAE;oBACV,QAAQ,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;oBACrC,QAAQ,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;oBACjC,QAAQ,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;oBAC7C,OAAO,QAAQ,CAAC;iBACnB;qBAAM;oBACH,IAAI,CAAC,OAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;oBAClC,OAAO,OAAO,CAAC;iBAClB;aACJ;iBAAM;gBACH,IAAI,CAAC,OAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAClC,OAAO,OAAO,CAAC;aAClB;;KACJ;CACJ;AAzVD,gCAyVC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './ItemSelector';
2
+ export * from './LocalIndex';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./ItemSelector"), exports);
14
+ __exportStar(require("./LocalIndex"), exports);
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAA+B;AAC/B,+CAA6B"}
@@ -0,0 +1,5 @@
1
+ export declare function dotProduct(arr1: number[], arr2: number[]): number;
2
+ export declare function normalize(arr: number[]): number;
3
+ export declare function cosineSimilarity(arr1: number[], arr2: number[]): number;
4
+ export declare function normalizedCosineSimilarity(arr1: number[], norm1: number, arr2: number[], norm2: number): number;
5
+ //# sourceMappingURL=utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AACA,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,UAUxD;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,UAUtC;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,UAG9D;AAED,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,UAGtG"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizedCosineSimilarity = exports.cosineSimilarity = exports.normalize = exports.dotProduct = void 0;
4
+ function dotProduct(arr1, arr2) {
5
+ // Initialize a variable to store the sum of the products
6
+ let sum = 0;
7
+ // Loop through the elements of the arrays
8
+ for (let i = 0; i < arr1.length; i++) {
9
+ // Multiply the corresponding elements and add them to the sum
10
+ sum += arr1[i] * arr2[i];
11
+ }
12
+ // Return the sum
13
+ return sum;
14
+ }
15
+ exports.dotProduct = dotProduct;
16
+ function normalize(arr) {
17
+ // Initialize a variable to store the sum of the squares
18
+ let sum = 0;
19
+ // Loop through the elements of the array
20
+ for (let i = 0; i < arr.length; i++) {
21
+ // Square the element and add it to the sum
22
+ sum += arr[i] * arr[i];
23
+ }
24
+ // Return the square root of the sum
25
+ return Math.sqrt(sum);
26
+ }
27
+ exports.normalize = normalize;
28
+ function cosineSimilarity(arr1, arr2) {
29
+ // Return the quotient of the dot product and the product of the norms
30
+ return dotProduct(arr1, arr2) / (normalize(arr1) * normalize(arr2));
31
+ }
32
+ exports.cosineSimilarity = cosineSimilarity;
33
+ function normalizedCosineSimilarity(arr1, norm1, arr2, norm2) {
34
+ // Return the quotient of the dot product and the product of the norms
35
+ return dotProduct(arr1, arr2) / (norm1 * norm2);
36
+ }
37
+ exports.normalizedCosineSimilarity = normalizedCosineSimilarity;
38
+ //# sourceMappingURL=utilities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":";;;AACA,SAAgB,UAAU,CAAC,IAAc,EAAE,IAAc;IACrD,yDAAyD;IACzD,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,0CAA0C;IAC1C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAClC,8DAA8D;QAC9D,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;KAC5B;IACD,iBAAiB;IACjB,OAAO,GAAG,CAAC;AACf,CAAC;AAVD,gCAUC;AAED,SAAgB,SAAS,CAAC,GAAa;IACnC,wDAAwD;IACxD,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,yCAAyC;IACzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACjC,2CAA2C;QAC3C,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;KAC1B;IACD,oCAAoC;IACpC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC1B,CAAC;AAVD,8BAUC;AAED,SAAgB,gBAAgB,CAAC,IAAc,EAAE,IAAc;IAC3D,sEAAsE;IACtE,OAAO,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AACxE,CAAC;AAHD,4CAGC;AAED,SAAgB,0BAA0B,CAAC,IAAc,EAAE,KAAa,EAAE,IAAc,EAAE,KAAa;IACnG,sEAAsE;IACtE,OAAO,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;AACpD,CAAC;AAHD,gEAGC"}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "vectra",
3
+ "author": "Steven Ickman",
4
+ "description": "A vector database that uses the local file system for storage.",
5
+ "version": "0.1.0",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "gpt"
9
+ ],
10
+ "bugs": {
11
+ "url": "https://github.com/Stevenic/vectra/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/Stevenic/vectra.git"
16
+ },
17
+ "main": "./lib/index.js",
18
+ "types": "./lib/index.d.ts",
19
+ "typesVersions": {
20
+ "<3.9": {
21
+ "*": [
22
+ "_ts3.4/*"
23
+ ]
24
+ }
25
+ },
26
+ "dependencies": {
27
+ "uuid": "^8.3.2"
28
+ },
29
+ "resolutions": {
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^14.14.31",
33
+ "@types/uuid": "^8.3.0"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc -b",
37
+ "build-docs": "typedoc --theme markdown --entryPoint botbuilder-m365 --excludePrivate --includeDeclarations --ignoreCompilerErrors --module amd --out ..\\..\\doc\\botbuilder-ai .\\lib\\index.d.ts --hideGenerator --name \"Bot Builder SDK - AI\" --readme none",
38
+ "build:rollup": "yarn clean && yarn build && api-extractor run --verbose --local",
39
+ "clean": "rimraf _ts3.4 lib tsconfig.tsbuildinfo node_modules",
40
+ "depcheck": "depcheck --config ../../.depcheckrc",
41
+ "lint": "eslint **/src/**/*.{j,t}s{,x} --fix --no-error-on-unmatched-pattern",
42
+ "test": "npm-run-all build test:mocha",
43
+ "test:mocha": "nyc mocha tests",
44
+ "test:compat": "api-extractor run --verbose"
45
+ },
46
+ "files": [
47
+ "_ts3.4",
48
+ "lib",
49
+ "src"
50
+ ]
51
+ }