@mastra/couchbase 0.0.0-vnext-inngest-20250508131921 → 0.0.0-workflow-deno-20250616115451
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +246 -2
- package/README.md +11 -1
- package/dist/_tsup-dts-rollup.d.cts +47 -6
- package/dist/_tsup-dts-rollup.d.ts +47 -6
- package/dist/index.cjs +70 -11
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +71 -12
- package/docker-compose.yaml +12 -12
- package/package.json +16 -13
- package/src/vector/index.integration.test.ts +130 -16
- package/src/vector/index.ts +94 -20
- package/src/vector/index.unit.test.ts +54 -54
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
-
import { connect, SearchRequest, VectorSearch, VectorQuery } from 'couchbase';
|
|
2
|
+
import { connect, SearchRequest, VectorSearch, VectorQuery, MutateInSpec } from 'couchbase';
|
|
3
3
|
|
|
4
4
|
// src/vector/index.ts
|
|
5
5
|
var DISTANCE_MAPPING = {
|
|
@@ -17,9 +17,9 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
17
17
|
bucket;
|
|
18
18
|
scope;
|
|
19
19
|
vector_dimension;
|
|
20
|
-
constructor(
|
|
20
|
+
constructor({ connectionString, username, password, bucketName, scopeName, collectionName }) {
|
|
21
21
|
super();
|
|
22
|
-
const baseClusterPromise = connect(
|
|
22
|
+
const baseClusterPromise = connect(connectionString, {
|
|
23
23
|
username,
|
|
24
24
|
password,
|
|
25
25
|
configProfile: "wanDevelopment"
|
|
@@ -51,8 +51,7 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
51
51
|
}
|
|
52
52
|
return this.collection;
|
|
53
53
|
}
|
|
54
|
-
async createIndex(
|
|
55
|
-
const { indexName, dimension, metric = "dotproduct" } = params;
|
|
54
|
+
async createIndex({ indexName, dimension, metric = "dotproduct" }) {
|
|
56
55
|
await this.getCollection();
|
|
57
56
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
58
57
|
throw new Error("Dimension must be a positive integer");
|
|
@@ -147,8 +146,7 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
147
146
|
throw error;
|
|
148
147
|
}
|
|
149
148
|
}
|
|
150
|
-
async upsert(
|
|
151
|
-
const { vectors, metadata, ids } = params;
|
|
149
|
+
async upsert({ vectors, metadata, ids }) {
|
|
152
150
|
await this.getCollection();
|
|
153
151
|
if (!vectors || vectors.length === 0) {
|
|
154
152
|
throw new Error("No vectors provided");
|
|
@@ -179,10 +177,9 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
179
177
|
await Promise.all(allPromises);
|
|
180
178
|
return pointIds;
|
|
181
179
|
}
|
|
182
|
-
async query(
|
|
183
|
-
const { indexName, queryVector, topK = 10, includeVector = false } = params;
|
|
180
|
+
async query({ indexName, queryVector, topK = 10, includeVector = false }) {
|
|
184
181
|
await this.getCollection();
|
|
185
|
-
const index_stats = await this.describeIndex(indexName);
|
|
182
|
+
const index_stats = await this.describeIndex({ indexName });
|
|
186
183
|
if (queryVector.length !== index_stats.dimension) {
|
|
187
184
|
throw new Error(`Query vector dimension mismatch. Expected ${index_stats.dimension}, got ${queryVector.length}`);
|
|
188
185
|
}
|
|
@@ -219,7 +216,13 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
219
216
|
const indexes = await this.scope.searchIndexes().getAllIndexes();
|
|
220
217
|
return indexes?.map((index) => index.name) || [];
|
|
221
218
|
}
|
|
222
|
-
|
|
219
|
+
/**
|
|
220
|
+
* Retrieves statistics about a vector index.
|
|
221
|
+
*
|
|
222
|
+
* @param {string} indexName - The name of the index to describe
|
|
223
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
224
|
+
*/
|
|
225
|
+
async describeIndex({ indexName }) {
|
|
223
226
|
await this.getCollection();
|
|
224
227
|
if (!(await this.listIndexes()).includes(indexName)) {
|
|
225
228
|
throw new Error(`Index ${indexName} does not exist`);
|
|
@@ -236,7 +239,7 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
236
239
|
)
|
|
237
240
|
};
|
|
238
241
|
}
|
|
239
|
-
async deleteIndex(indexName) {
|
|
242
|
+
async deleteIndex({ indexName }) {
|
|
240
243
|
await this.getCollection();
|
|
241
244
|
if (!(await this.listIndexes()).includes(indexName)) {
|
|
242
245
|
throw new Error(`Index ${indexName} does not exist`);
|
|
@@ -244,6 +247,62 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
244
247
|
await this.scope.searchIndexes().dropIndex(indexName);
|
|
245
248
|
this.vector_dimension = null;
|
|
246
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
252
|
+
* @param indexName - The name of the index containing the vector.
|
|
253
|
+
* @param id - The ID of the vector to update.
|
|
254
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
255
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
256
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
257
|
+
* @returns A promise that resolves when the update is complete.
|
|
258
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
259
|
+
*/
|
|
260
|
+
async updateVector({ id, update }) {
|
|
261
|
+
if (!update.vector && !update.metadata) {
|
|
262
|
+
throw new Error("No updates provided");
|
|
263
|
+
}
|
|
264
|
+
if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) {
|
|
265
|
+
throw new Error("Vector dimension mismatch");
|
|
266
|
+
}
|
|
267
|
+
const collection = await this.getCollection();
|
|
268
|
+
try {
|
|
269
|
+
await collection.get(id);
|
|
270
|
+
} catch (err) {
|
|
271
|
+
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
272
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
273
|
+
}
|
|
274
|
+
throw err;
|
|
275
|
+
}
|
|
276
|
+
const specs = [];
|
|
277
|
+
if (update.vector) specs.push(MutateInSpec.replace("embedding", update.vector));
|
|
278
|
+
if (update.metadata) specs.push(MutateInSpec.replace("metadata", update.metadata));
|
|
279
|
+
await collection.mutateIn(id, specs);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Deletes a vector by its ID.
|
|
283
|
+
* @param indexName - The name of the index containing the vector.
|
|
284
|
+
* @param id - The ID of the vector to delete.
|
|
285
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
286
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
287
|
+
*/
|
|
288
|
+
async deleteVector({ id }) {
|
|
289
|
+
const collection = await this.getCollection();
|
|
290
|
+
try {
|
|
291
|
+
await collection.get(id);
|
|
292
|
+
} catch (err) {
|
|
293
|
+
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
294
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
295
|
+
}
|
|
296
|
+
throw err;
|
|
297
|
+
}
|
|
298
|
+
await collection.remove(id);
|
|
299
|
+
}
|
|
300
|
+
async disconnect() {
|
|
301
|
+
if (!this.cluster) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
await this.cluster.close();
|
|
305
|
+
}
|
|
247
306
|
};
|
|
248
307
|
|
|
249
308
|
export { CouchbaseVector, DISTANCE_MAPPING };
|
package/docker-compose.yaml
CHANGED
|
@@ -2,20 +2,20 @@ services:
|
|
|
2
2
|
mastra_couchbase_testing:
|
|
3
3
|
image: couchbase:enterprise-7.6.4
|
|
4
4
|
ports:
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
5
|
+
- '8091-8095:8091-8095'
|
|
6
|
+
- '11210:11210'
|
|
7
|
+
- '9102:9102'
|
|
8
8
|
expose:
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
9
|
+
- '8091'
|
|
10
|
+
- '8092'
|
|
11
|
+
- '8093'
|
|
12
|
+
- '8094'
|
|
13
|
+
- '8095'
|
|
14
|
+
- '9102'
|
|
15
|
+
- '11210'
|
|
16
16
|
healthcheck: # checks couchbase server is up
|
|
17
|
-
test: [
|
|
17
|
+
test: ['CMD', 'curl', '-v', 'http://localhost:8091/pools']
|
|
18
18
|
interval: 20s
|
|
19
19
|
timeout: 20s
|
|
20
20
|
retries: 5
|
|
21
|
-
container_name: mastra_couchbase_testing
|
|
21
|
+
container_name: mastra_couchbase_testing
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/couchbase",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-workflow-deno-20250616115451",
|
|
4
4
|
"description": "Couchbase vector store provider for Mastra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -19,20 +19,23 @@
|
|
|
19
19
|
"./package.json": "./package.json"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"couchbase": "^4.
|
|
23
|
-
"@mastra/core": "0.0.0-vnext-inngest-20250508131921"
|
|
22
|
+
"couchbase": "^4.5.0"
|
|
24
23
|
},
|
|
25
24
|
"devDependencies": {
|
|
26
|
-
"@microsoft/api-extractor": "^7.52.
|
|
27
|
-
"@types/node": "^20.
|
|
28
|
-
"@vitest/coverage-v8": "3.
|
|
29
|
-
"@vitest/ui": "3.
|
|
30
|
-
"axios": "^1.
|
|
31
|
-
"eslint": "^9.
|
|
32
|
-
"tsup": "^8.
|
|
33
|
-
"typescript": "^5.8.
|
|
34
|
-
"vitest": "^3.
|
|
35
|
-
"@internal/lint": "0.0.0-
|
|
25
|
+
"@microsoft/api-extractor": "^7.52.8",
|
|
26
|
+
"@types/node": "^20.19.0",
|
|
27
|
+
"@vitest/coverage-v8": "3.2.2",
|
|
28
|
+
"@vitest/ui": "3.2.2",
|
|
29
|
+
"axios": "^1.9.0",
|
|
30
|
+
"eslint": "^9.28.0",
|
|
31
|
+
"tsup": "^8.5.0",
|
|
32
|
+
"typescript": "^5.8.3",
|
|
33
|
+
"vitest": "^3.2.3",
|
|
34
|
+
"@internal/lint": "0.0.0-workflow-deno-20250616115451",
|
|
35
|
+
"@mastra/core": "0.0.0-workflow-deno-20250616115451"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@mastra/core": ">=0.10.4-0 <0.11.0"
|
|
36
39
|
},
|
|
37
40
|
"scripts": {
|
|
38
41
|
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
|
@@ -173,14 +173,14 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
173
173
|
|
|
174
174
|
describe('Connection', () => {
|
|
175
175
|
it('should connect to couchbase', async () => {
|
|
176
|
-
couchbase_client = new CouchbaseVector(
|
|
176
|
+
couchbase_client = new CouchbaseVector({
|
|
177
177
|
connectionString,
|
|
178
178
|
username,
|
|
179
179
|
password,
|
|
180
|
-
test_bucketName,
|
|
181
|
-
test_scopeName,
|
|
182
|
-
test_collectionName,
|
|
183
|
-
);
|
|
180
|
+
bucketName: test_bucketName,
|
|
181
|
+
scopeName: test_scopeName,
|
|
182
|
+
collectionName: test_collectionName,
|
|
183
|
+
});
|
|
184
184
|
expect(couchbase_client).toBeDefined();
|
|
185
185
|
const collection = await couchbase_client.getCollection();
|
|
186
186
|
expect(collection).toBeDefined();
|
|
@@ -211,14 +211,14 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
211
211
|
}, 50000);
|
|
212
212
|
|
|
213
213
|
it('should describe index', async () => {
|
|
214
|
-
const stats = await couchbase_client.describeIndex(test_indexName);
|
|
214
|
+
const stats = await couchbase_client.describeIndex({ indexName: test_indexName });
|
|
215
215
|
expect(stats.dimension).toBe(dimension);
|
|
216
216
|
expect(stats.metric).toBe('euclidean'); // similiarity(=="l2_norm") is mapped to euclidean in couchbase
|
|
217
217
|
expect(typeof stats.count).toBe('number');
|
|
218
218
|
}, 50000);
|
|
219
219
|
|
|
220
220
|
it('should delete index', async () => {
|
|
221
|
-
await couchbase_client.deleteIndex(test_indexName);
|
|
221
|
+
await couchbase_client.deleteIndex({ indexName: test_indexName });
|
|
222
222
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
223
223
|
await expect(scope.searchIndexes().getIndex(test_indexName)).rejects.toThrowError();
|
|
224
224
|
}, 50000);
|
|
@@ -246,7 +246,7 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
246
246
|
}, 50000);
|
|
247
247
|
|
|
248
248
|
afterAll(async () => {
|
|
249
|
-
await couchbase_client.deleteIndex(test_indexName);
|
|
249
|
+
await couchbase_client.deleteIndex({ indexName: test_indexName });
|
|
250
250
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
251
251
|
}, 50000);
|
|
252
252
|
|
|
@@ -380,6 +380,120 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
380
380
|
}),
|
|
381
381
|
).rejects.toThrow('Including vectors in search results is not yet supported by the Couchbase vector store');
|
|
382
382
|
}, 50000);
|
|
383
|
+
|
|
384
|
+
it('should upsert vectors with generated ids', async () => {
|
|
385
|
+
const ids = await couchbase_client.upsert({ indexName: test_indexName, vectors: testVectors });
|
|
386
|
+
expect(ids).toHaveLength(testVectors.length);
|
|
387
|
+
ids.forEach(id => expect(typeof id).toBe('string'));
|
|
388
|
+
|
|
389
|
+
// Count is not supported by Couchbase
|
|
390
|
+
const stats = await couchbase_client.describeIndex({ indexName: test_indexName });
|
|
391
|
+
expect(stats.count).toBe(-1);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('should update existing vectors', async () => {
|
|
395
|
+
// Initial upsert
|
|
396
|
+
await couchbase_client.upsert({
|
|
397
|
+
indexName: test_indexName,
|
|
398
|
+
vectors: testVectors,
|
|
399
|
+
metadata: testMetadata,
|
|
400
|
+
ids: testVectorIds,
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// Update first vector
|
|
404
|
+
const updatedVector = [[0.5, 0.5, 0.0]];
|
|
405
|
+
const updatedMetadata = [{ label: 'updated-x-axis' }];
|
|
406
|
+
await couchbase_client.upsert({
|
|
407
|
+
indexName: test_indexName,
|
|
408
|
+
vectors: updatedVector,
|
|
409
|
+
metadata: updatedMetadata,
|
|
410
|
+
ids: [testVectorIds?.[0]!],
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// Verify update
|
|
414
|
+
const result = await collection.get(testVectorIds?.[0]!);
|
|
415
|
+
expect(result.content.embedding).toEqual(updatedVector[0]);
|
|
416
|
+
expect(result.content.metadata).toEqual(updatedMetadata[0]);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('should update the vector by id', async () => {
|
|
420
|
+
const ids = await couchbase_client.upsert({ indexName: test_indexName, vectors: testVectors });
|
|
421
|
+
expect(ids).toHaveLength(3);
|
|
422
|
+
|
|
423
|
+
const idToBeUpdated = ids[0];
|
|
424
|
+
const newVector = [1, 2, 3];
|
|
425
|
+
const newMetaData = {
|
|
426
|
+
test: 'updates',
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const update = {
|
|
430
|
+
vector: newVector,
|
|
431
|
+
metadata: newMetaData,
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
await couchbase_client.updateVector({ indexName: test_indexName, id: idToBeUpdated, update });
|
|
435
|
+
|
|
436
|
+
const result = await collection.get(idToBeUpdated);
|
|
437
|
+
expect(result.content.embedding).toEqual(newVector);
|
|
438
|
+
expect(result.content.metadata).toEqual(newMetaData);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('should only update the metadata by id', async () => {
|
|
442
|
+
const ids = await couchbase_client.upsert({ indexName: test_indexName, vectors: testVectors });
|
|
443
|
+
expect(ids).toHaveLength(3);
|
|
444
|
+
|
|
445
|
+
const idToBeUpdated = ids[0];
|
|
446
|
+
const newMetaData = {
|
|
447
|
+
test: 'updates',
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const update = {
|
|
451
|
+
metadata: newMetaData,
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
await couchbase_client.updateVector({ indexName: test_indexName, id: idToBeUpdated, update });
|
|
455
|
+
|
|
456
|
+
const result = await collection.get(idToBeUpdated);
|
|
457
|
+
expect(result.content.embedding).toEqual(testVectors[0]);
|
|
458
|
+
expect(result.content.metadata).toEqual(newMetaData);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
it('should only update vector embeddings by id', async () => {
|
|
462
|
+
const ids = await couchbase_client.upsert({ indexName: test_indexName, vectors: testVectors });
|
|
463
|
+
expect(ids).toHaveLength(3);
|
|
464
|
+
|
|
465
|
+
const idToBeUpdated = ids[0];
|
|
466
|
+
const newVector = [1, 2, 3];
|
|
467
|
+
|
|
468
|
+
const update = {
|
|
469
|
+
vector: newVector,
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
await couchbase_client.updateVector({ indexName: test_indexName, id: idToBeUpdated, update });
|
|
473
|
+
|
|
474
|
+
const result = await collection.get(idToBeUpdated);
|
|
475
|
+
expect(result.content.embedding).toEqual(newVector);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it('should throw exception when no updates are given', async () => {
|
|
479
|
+
await expect(couchbase_client.updateVector({ indexName: test_indexName, id: 'id', update: {} })).rejects.toThrow(
|
|
480
|
+
'No updates provided',
|
|
481
|
+
);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it('should delete the vector by id', async () => {
|
|
485
|
+
const ids = await couchbase_client.upsert({ indexName: test_indexName, vectors: testVectors });
|
|
486
|
+
expect(ids).toHaveLength(3);
|
|
487
|
+
const idToBeDeleted = ids[0];
|
|
488
|
+
|
|
489
|
+
await couchbase_client.deleteVector({ indexName: test_indexName, id: idToBeDeleted });
|
|
490
|
+
|
|
491
|
+
try {
|
|
492
|
+
await collection.get(idToBeDeleted);
|
|
493
|
+
} catch (error) {
|
|
494
|
+
expect(error).toBeInstanceOf(Error);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
383
497
|
});
|
|
384
498
|
|
|
385
499
|
describe('Error Cases and Edge Cases', () => {
|
|
@@ -409,7 +523,7 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
409
523
|
expect(allIndexes.find(idx => idx.name === nonExistentIndex)).toBeUndefined();
|
|
410
524
|
|
|
411
525
|
// Now test the couchbase_client method
|
|
412
|
-
await expect(couchbase_client.describeIndex(nonExistentIndex)).rejects.toThrow();
|
|
526
|
+
await expect(couchbase_client.describeIndex({ indexName: nonExistentIndex })).rejects.toThrow();
|
|
413
527
|
}, 50000);
|
|
414
528
|
|
|
415
529
|
it('should throw error when deleting a non-existent index', async () => {
|
|
@@ -420,7 +534,7 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
420
534
|
expect(allIndexes.find(idx => idx.name === nonExistentIndex)).toBeUndefined();
|
|
421
535
|
|
|
422
536
|
// Now test the couchbase_client method
|
|
423
|
-
await expect(couchbase_client.deleteIndex(nonExistentIndex)).rejects.toThrow();
|
|
537
|
+
await expect(couchbase_client.deleteIndex({ indexName: nonExistentIndex })).rejects.toThrow();
|
|
424
538
|
}, 50000);
|
|
425
539
|
|
|
426
540
|
it('should throw error for empty vectors array in upsert', async () => {
|
|
@@ -489,7 +603,7 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
489
603
|
infoSpy.mockRestore();
|
|
490
604
|
warnSpy.mockRestore();
|
|
491
605
|
// Cleanup
|
|
492
|
-
await couchbase_client.deleteIndex(duplicateIndexName);
|
|
606
|
+
await couchbase_client.deleteIndex({ indexName: duplicateIndexName });
|
|
493
607
|
}
|
|
494
608
|
}, 50000);
|
|
495
609
|
});
|
|
@@ -499,7 +613,7 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
499
613
|
const indexes = await couchbase_client.listIndexes();
|
|
500
614
|
if (indexes.length > 0) {
|
|
501
615
|
for (const index of indexes) {
|
|
502
|
-
await couchbase_client.deleteIndex(index);
|
|
616
|
+
await couchbase_client.deleteIndex({ indexName: index });
|
|
503
617
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
504
618
|
}
|
|
505
619
|
}
|
|
@@ -562,7 +676,7 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
562
676
|
expect((couchbase_client as any).vector_dimension).toBe(testDimension);
|
|
563
677
|
|
|
564
678
|
// Delete the index
|
|
565
|
-
await couchbase_client.deleteIndex(testIndexName);
|
|
679
|
+
await couchbase_client.deleteIndex({ indexName: testIndexName });
|
|
566
680
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
567
681
|
|
|
568
682
|
// Verify dimension is reset
|
|
@@ -578,7 +692,7 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
578
692
|
const indexes = await couchbase_client.listIndexes();
|
|
579
693
|
if (indexes.length > 0) {
|
|
580
694
|
for (const index of indexes) {
|
|
581
|
-
await couchbase_client.deleteIndex(index);
|
|
695
|
+
await couchbase_client.deleteIndex({ indexName: index });
|
|
582
696
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
583
697
|
}
|
|
584
698
|
}
|
|
@@ -607,11 +721,11 @@ describe('Integration Testing CouchbaseVector', async () => {
|
|
|
607
721
|
expect(similarityParam).toBe(couchbaseMetric);
|
|
608
722
|
|
|
609
723
|
// Verify through our API
|
|
610
|
-
const stats = await couchbase_client.describeIndex(testIndexName);
|
|
724
|
+
const stats = await couchbase_client.describeIndex({ indexName: testIndexName });
|
|
611
725
|
expect(stats.metric).toBe(mastraMetric);
|
|
612
726
|
|
|
613
727
|
// Clean up
|
|
614
|
-
await couchbase_client.deleteIndex(testIndexName);
|
|
728
|
+
await couchbase_client.deleteIndex({ indexName: testIndexName });
|
|
615
729
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
616
730
|
}
|
|
617
731
|
}, 50000);
|
package/src/vector/index.ts
CHANGED
|
@@ -5,9 +5,13 @@ import type {
|
|
|
5
5
|
CreateIndexParams,
|
|
6
6
|
UpsertVectorParams,
|
|
7
7
|
QueryVectorParams,
|
|
8
|
+
DescribeIndexParams,
|
|
9
|
+
DeleteIndexParams,
|
|
10
|
+
DeleteVectorParams,
|
|
11
|
+
UpdateVectorParams,
|
|
8
12
|
} from '@mastra/core/vector';
|
|
9
13
|
import type { Bucket, Cluster, Collection, Scope } from 'couchbase';
|
|
10
|
-
import { connect, SearchRequest, VectorQuery, VectorSearch } from 'couchbase';
|
|
14
|
+
import { MutateInSpec, connect, SearchRequest, VectorQuery, VectorSearch } from 'couchbase';
|
|
11
15
|
|
|
12
16
|
type MastraMetric = 'cosine' | 'euclidean' | 'dotproduct';
|
|
13
17
|
type CouchbaseMetric = 'cosine' | 'l2_norm' | 'dot_product';
|
|
@@ -17,6 +21,15 @@ export const DISTANCE_MAPPING: Record<MastraMetric, CouchbaseMetric> = {
|
|
|
17
21
|
dotproduct: 'dot_product',
|
|
18
22
|
};
|
|
19
23
|
|
|
24
|
+
export type CouchbaseVectorParams = {
|
|
25
|
+
connectionString: string;
|
|
26
|
+
username: string;
|
|
27
|
+
password: string;
|
|
28
|
+
bucketName: string;
|
|
29
|
+
scopeName: string;
|
|
30
|
+
collectionName: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
20
33
|
export class CouchbaseVector extends MastraVector {
|
|
21
34
|
private clusterPromise: Promise<Cluster>;
|
|
22
35
|
private cluster: Cluster;
|
|
@@ -28,17 +41,10 @@ export class CouchbaseVector extends MastraVector {
|
|
|
28
41
|
private scope: Scope;
|
|
29
42
|
private vector_dimension: number;
|
|
30
43
|
|
|
31
|
-
constructor(
|
|
32
|
-
cnn_string: string,
|
|
33
|
-
username: string,
|
|
34
|
-
password: string,
|
|
35
|
-
bucketName: string,
|
|
36
|
-
scopeName: string,
|
|
37
|
-
collectionName: string,
|
|
38
|
-
) {
|
|
44
|
+
constructor({ connectionString, username, password, bucketName, scopeName, collectionName }: CouchbaseVectorParams) {
|
|
39
45
|
super();
|
|
40
46
|
|
|
41
|
-
const baseClusterPromise = connect(
|
|
47
|
+
const baseClusterPromise = connect(connectionString, {
|
|
42
48
|
username,
|
|
43
49
|
password,
|
|
44
50
|
configProfile: 'wanDevelopment',
|
|
@@ -76,8 +82,7 @@ export class CouchbaseVector extends MastraVector {
|
|
|
76
82
|
return this.collection;
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
async createIndex(
|
|
80
|
-
const { indexName, dimension, metric = 'dotproduct' as MastraMetric } = params;
|
|
85
|
+
async createIndex({ indexName, dimension, metric = 'dotproduct' as MastraMetric }: CreateIndexParams): Promise<void> {
|
|
81
86
|
await this.getCollection();
|
|
82
87
|
|
|
83
88
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
@@ -172,8 +177,7 @@ export class CouchbaseVector extends MastraVector {
|
|
|
172
177
|
}
|
|
173
178
|
}
|
|
174
179
|
|
|
175
|
-
async upsert(
|
|
176
|
-
const { vectors, metadata, ids } = params;
|
|
180
|
+
async upsert({ vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {
|
|
177
181
|
await this.getCollection();
|
|
178
182
|
|
|
179
183
|
if (!vectors || vectors.length === 0) {
|
|
@@ -210,12 +214,10 @@ export class CouchbaseVector extends MastraVector {
|
|
|
210
214
|
return pointIds;
|
|
211
215
|
}
|
|
212
216
|
|
|
213
|
-
async query(
|
|
214
|
-
const { indexName, queryVector, topK = 10, includeVector = false } = params;
|
|
215
|
-
|
|
217
|
+
async query({ indexName, queryVector, topK = 10, includeVector = false }: QueryVectorParams): Promise<QueryResult[]> {
|
|
216
218
|
await this.getCollection();
|
|
217
219
|
|
|
218
|
-
const index_stats = await this.describeIndex(indexName);
|
|
220
|
+
const index_stats = await this.describeIndex({ indexName });
|
|
219
221
|
if (queryVector.length !== index_stats.dimension) {
|
|
220
222
|
throw new Error(`Query vector dimension mismatch. Expected ${index_stats.dimension}, got ${queryVector.length}`);
|
|
221
223
|
}
|
|
@@ -255,7 +257,13 @@ export class CouchbaseVector extends MastraVector {
|
|
|
255
257
|
return indexes?.map(index => index.name) || [];
|
|
256
258
|
}
|
|
257
259
|
|
|
258
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Retrieves statistics about a vector index.
|
|
262
|
+
*
|
|
263
|
+
* @param {string} indexName - The name of the index to describe
|
|
264
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
265
|
+
*/
|
|
266
|
+
async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
|
|
259
267
|
await this.getCollection();
|
|
260
268
|
if (!(await this.listIndexes()).includes(indexName)) {
|
|
261
269
|
throw new Error(`Index ${indexName} does not exist`);
|
|
@@ -276,7 +284,7 @@ export class CouchbaseVector extends MastraVector {
|
|
|
276
284
|
};
|
|
277
285
|
}
|
|
278
286
|
|
|
279
|
-
async deleteIndex(indexName:
|
|
287
|
+
async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
|
|
280
288
|
await this.getCollection();
|
|
281
289
|
if (!(await this.listIndexes()).includes(indexName)) {
|
|
282
290
|
throw new Error(`Index ${indexName} does not exist`);
|
|
@@ -284,4 +292,70 @@ export class CouchbaseVector extends MastraVector {
|
|
|
284
292
|
await this.scope.searchIndexes().dropIndex(indexName);
|
|
285
293
|
this.vector_dimension = null as unknown as number;
|
|
286
294
|
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
298
|
+
* @param indexName - The name of the index containing the vector.
|
|
299
|
+
* @param id - The ID of the vector to update.
|
|
300
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
301
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
302
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
303
|
+
* @returns A promise that resolves when the update is complete.
|
|
304
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
305
|
+
*/
|
|
306
|
+
async updateVector({ id, update }: UpdateVectorParams): Promise<void> {
|
|
307
|
+
if (!update.vector && !update.metadata) {
|
|
308
|
+
throw new Error('No updates provided');
|
|
309
|
+
}
|
|
310
|
+
if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) {
|
|
311
|
+
throw new Error('Vector dimension mismatch');
|
|
312
|
+
}
|
|
313
|
+
const collection = await this.getCollection();
|
|
314
|
+
|
|
315
|
+
// Check if document exists
|
|
316
|
+
try {
|
|
317
|
+
await collection.get(id);
|
|
318
|
+
} catch (err: any) {
|
|
319
|
+
if (err.code === 13 || err.message?.includes('document not found')) {
|
|
320
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
321
|
+
}
|
|
322
|
+
throw err;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const specs: MutateInSpec[] = [];
|
|
326
|
+
if (update.vector) specs.push(MutateInSpec.replace('embedding', update.vector));
|
|
327
|
+
if (update.metadata) specs.push(MutateInSpec.replace('metadata', update.metadata));
|
|
328
|
+
|
|
329
|
+
await collection.mutateIn(id, specs);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Deletes a vector by its ID.
|
|
334
|
+
* @param indexName - The name of the index containing the vector.
|
|
335
|
+
* @param id - The ID of the vector to delete.
|
|
336
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
337
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
338
|
+
*/
|
|
339
|
+
async deleteVector({ id }: DeleteVectorParams): Promise<void> {
|
|
340
|
+
const collection = await this.getCollection();
|
|
341
|
+
|
|
342
|
+
// Check if document exists
|
|
343
|
+
try {
|
|
344
|
+
await collection.get(id);
|
|
345
|
+
} catch (err: any) {
|
|
346
|
+
if (err.code === 13 || err.message?.includes('document not found')) {
|
|
347
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
348
|
+
}
|
|
349
|
+
throw err;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
await collection.remove(id);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async disconnect() {
|
|
356
|
+
if (!this.cluster) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
await this.cluster.close();
|
|
360
|
+
}
|
|
287
361
|
}
|