@mastra/couchbase 0.0.0-vnext-inngest-20250508122351 → 0.0.0-vnext-20251104230439
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 +674 -2
- package/LICENSE.md +11 -42
- package/README.md +11 -1
- package/dist/index.cjs +308 -111
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +307 -110
- package/dist/index.js.map +1 -0
- package/dist/vector/index.d.ts +61 -0
- package/dist/vector/index.d.ts.map +1 -0
- package/package.json +33 -16
- package/dist/_tsup-dts-rollup.d.cts +0 -39
- package/dist/_tsup-dts-rollup.d.ts +0 -39
- package/dist/index.d.cts +0 -2
- package/docker-compose.yaml +0 -21
- package/eslint.config.js +0 -6
- package/scripts/start-docker.js +0 -14
- package/scripts/stop-docker.js +0 -7
- package/src/index.ts +0 -1
- package/src/vector/index.integration.test.ts +0 -619
- package/src/vector/index.ts +0 -287
- package/src/vector/index.unit.test.ts +0 -737
- package/tsconfig.json +0 -5
- package/vitest.config.ts +0 -11
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
1
2
|
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
-
import { connect, SearchRequest, VectorSearch, VectorQuery } from 'couchbase';
|
|
3
|
+
import { connect, SearchRequest, VectorSearch, VectorQuery, MutateInSpec } from 'couchbase';
|
|
3
4
|
|
|
4
5
|
// src/vector/index.ts
|
|
5
6
|
var DISTANCE_MAPPING = {
|
|
@@ -17,28 +18,40 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
17
18
|
bucket;
|
|
18
19
|
scope;
|
|
19
20
|
vector_dimension;
|
|
20
|
-
constructor(
|
|
21
|
+
constructor({ connectionString, username, password, bucketName, scopeName, collectionName }) {
|
|
21
22
|
super();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
23
|
+
try {
|
|
24
|
+
this.clusterPromise = connect(connectionString, {
|
|
25
|
+
username,
|
|
26
|
+
password,
|
|
27
|
+
configProfile: "wanDevelopment"
|
|
28
|
+
});
|
|
29
|
+
this.cluster = null;
|
|
30
|
+
this.bucketName = bucketName;
|
|
31
|
+
this.collectionName = collectionName;
|
|
32
|
+
this.scopeName = scopeName;
|
|
33
|
+
this.collection = null;
|
|
34
|
+
this.bucket = null;
|
|
35
|
+
this.scope = null;
|
|
36
|
+
this.vector_dimension = null;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new MastraError(
|
|
39
|
+
{
|
|
40
|
+
id: "COUCHBASE_VECTOR_INITIALIZE_FAILED",
|
|
41
|
+
domain: ErrorDomain.STORAGE,
|
|
42
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
43
|
+
details: {
|
|
44
|
+
connectionString,
|
|
45
|
+
username,
|
|
46
|
+
password,
|
|
47
|
+
bucketName,
|
|
48
|
+
scopeName,
|
|
49
|
+
collectionName
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
error
|
|
53
|
+
);
|
|
54
|
+
}
|
|
42
55
|
}
|
|
43
56
|
async getCollection() {
|
|
44
57
|
if (!this.cluster) {
|
|
@@ -51,13 +64,12 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
51
64
|
}
|
|
52
65
|
return this.collection;
|
|
53
66
|
}
|
|
54
|
-
async createIndex(
|
|
55
|
-
const { indexName, dimension, metric = "dotproduct" } = params;
|
|
56
|
-
await this.getCollection();
|
|
57
|
-
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
58
|
-
throw new Error("Dimension must be a positive integer");
|
|
59
|
-
}
|
|
67
|
+
async createIndex({ indexName, dimension, metric = "dotproduct" }) {
|
|
60
68
|
try {
|
|
69
|
+
await this.getCollection();
|
|
70
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
71
|
+
throw new Error("Dimension must be a positive integer");
|
|
72
|
+
}
|
|
61
73
|
await this.scope.searchIndexes().upsertIndex({
|
|
62
74
|
name: indexName,
|
|
63
75
|
sourceName: this.bucketName,
|
|
@@ -144,106 +156,291 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
144
156
|
await this.validateExistingIndex(indexName, dimension, metric);
|
|
145
157
|
return;
|
|
146
158
|
}
|
|
147
|
-
throw
|
|
159
|
+
throw new MastraError(
|
|
160
|
+
{
|
|
161
|
+
id: "COUCHBASE_VECTOR_CREATE_INDEX_FAILED",
|
|
162
|
+
domain: ErrorDomain.STORAGE,
|
|
163
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
164
|
+
details: {
|
|
165
|
+
indexName,
|
|
166
|
+
dimension,
|
|
167
|
+
metric
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
error
|
|
171
|
+
);
|
|
148
172
|
}
|
|
149
173
|
}
|
|
150
|
-
async upsert(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
174
|
+
async upsert({ vectors, metadata, ids }) {
|
|
175
|
+
try {
|
|
176
|
+
await this.getCollection();
|
|
177
|
+
if (!vectors || vectors.length === 0) {
|
|
178
|
+
throw new Error("No vectors provided");
|
|
179
|
+
}
|
|
180
|
+
if (this.vector_dimension) {
|
|
181
|
+
for (const vector of vectors) {
|
|
182
|
+
if (!vector || this.vector_dimension !== vector.length) {
|
|
183
|
+
throw new Error("Vector dimension mismatch");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const pointIds = ids || vectors.map(() => crypto.randomUUID());
|
|
188
|
+
const records = vectors.map((vector, i) => {
|
|
189
|
+
const metadataObj = metadata?.[i] || {};
|
|
190
|
+
const record = {
|
|
191
|
+
embedding: vector,
|
|
192
|
+
metadata: metadataObj
|
|
193
|
+
};
|
|
194
|
+
if (metadataObj.text) {
|
|
195
|
+
record.content = metadataObj.text;
|
|
196
|
+
}
|
|
197
|
+
return record;
|
|
198
|
+
});
|
|
199
|
+
const allPromises = [];
|
|
200
|
+
for (let i = 0; i < records.length; i++) {
|
|
201
|
+
allPromises.push(this.collection.upsert(pointIds[i], records[i]));
|
|
202
|
+
}
|
|
203
|
+
await Promise.all(allPromises);
|
|
204
|
+
return pointIds;
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw new MastraError(
|
|
207
|
+
{
|
|
208
|
+
id: "COUCHBASE_VECTOR_UPSERT_FAILED",
|
|
209
|
+
domain: ErrorDomain.STORAGE,
|
|
210
|
+
category: ErrorCategory.THIRD_PARTY
|
|
211
|
+
},
|
|
212
|
+
error
|
|
213
|
+
);
|
|
155
214
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
215
|
+
}
|
|
216
|
+
async query({ indexName, queryVector, topK = 10, includeVector = false }) {
|
|
217
|
+
try {
|
|
218
|
+
await this.getCollection();
|
|
219
|
+
const index_stats = await this.describeIndex({ indexName });
|
|
220
|
+
if (queryVector.length !== index_stats.dimension) {
|
|
221
|
+
throw new Error(
|
|
222
|
+
`Query vector dimension mismatch. Expected ${index_stats.dimension}, got ${queryVector.length}`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
let request = SearchRequest.create(
|
|
226
|
+
VectorSearch.fromVectorQuery(VectorQuery.create("embedding", queryVector).numCandidates(topK))
|
|
227
|
+
);
|
|
228
|
+
const results = await this.scope.search(indexName, request, {
|
|
229
|
+
fields: ["*"]
|
|
230
|
+
});
|
|
231
|
+
if (includeVector) {
|
|
232
|
+
throw new Error("Including vectors in search results is not yet supported by the Couchbase vector store");
|
|
233
|
+
}
|
|
234
|
+
const output = [];
|
|
235
|
+
for (const match of results.rows) {
|
|
236
|
+
const cleanedMetadata = {};
|
|
237
|
+
const fields = match.fields || {};
|
|
238
|
+
for (const key in fields) {
|
|
239
|
+
if (Object.prototype.hasOwnProperty.call(fields, key)) {
|
|
240
|
+
const newKey = key.startsWith("metadata.") ? key.substring("metadata.".length) : key;
|
|
241
|
+
cleanedMetadata[newKey] = fields[key];
|
|
242
|
+
}
|
|
160
243
|
}
|
|
244
|
+
output.push({
|
|
245
|
+
id: match.id,
|
|
246
|
+
score: match.score || 0,
|
|
247
|
+
metadata: cleanedMetadata
|
|
248
|
+
// Use the cleaned metadata object
|
|
249
|
+
});
|
|
161
250
|
}
|
|
251
|
+
return output;
|
|
252
|
+
} catch (error) {
|
|
253
|
+
throw new MastraError(
|
|
254
|
+
{
|
|
255
|
+
id: "COUCHBASE_VECTOR_QUERY_FAILED",
|
|
256
|
+
domain: ErrorDomain.STORAGE,
|
|
257
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
258
|
+
details: {
|
|
259
|
+
indexName,
|
|
260
|
+
topK
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
error
|
|
264
|
+
);
|
|
162
265
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
266
|
+
}
|
|
267
|
+
async listIndexes() {
|
|
268
|
+
try {
|
|
269
|
+
await this.getCollection();
|
|
270
|
+
const indexes = await this.scope.searchIndexes().getAllIndexes();
|
|
271
|
+
return indexes?.map((index) => index.name) || [];
|
|
272
|
+
} catch (error) {
|
|
273
|
+
throw new MastraError(
|
|
274
|
+
{
|
|
275
|
+
id: "COUCHBASE_VECTOR_LIST_INDEXES_FAILED",
|
|
276
|
+
domain: ErrorDomain.STORAGE,
|
|
277
|
+
category: ErrorCategory.THIRD_PARTY
|
|
278
|
+
},
|
|
279
|
+
error
|
|
280
|
+
);
|
|
178
281
|
}
|
|
179
|
-
await Promise.all(allPromises);
|
|
180
|
-
return pointIds;
|
|
181
282
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Retrieves statistics about a vector index.
|
|
285
|
+
*
|
|
286
|
+
* @param {string} indexName - The name of the index to describe
|
|
287
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
288
|
+
*/
|
|
289
|
+
async describeIndex({ indexName }) {
|
|
290
|
+
try {
|
|
291
|
+
await this.getCollection();
|
|
292
|
+
if (!(await this.listIndexes()).includes(indexName)) {
|
|
293
|
+
throw new Error(`Index ${indexName} does not exist`);
|
|
294
|
+
}
|
|
295
|
+
const index = await this.scope.searchIndexes().getIndex(indexName);
|
|
296
|
+
const dimensions = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.dims;
|
|
297
|
+
const count = -1;
|
|
298
|
+
const metric = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.similarity;
|
|
299
|
+
return {
|
|
300
|
+
dimension: dimensions,
|
|
301
|
+
count,
|
|
302
|
+
metric: Object.keys(DISTANCE_MAPPING).find(
|
|
303
|
+
(key) => DISTANCE_MAPPING[key] === metric
|
|
304
|
+
)
|
|
305
|
+
};
|
|
306
|
+
} catch (error) {
|
|
307
|
+
throw new MastraError(
|
|
308
|
+
{
|
|
309
|
+
id: "COUCHBASE_VECTOR_DESCRIBE_INDEX_FAILED",
|
|
310
|
+
domain: ErrorDomain.STORAGE,
|
|
311
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
312
|
+
details: {
|
|
313
|
+
indexName
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
error
|
|
317
|
+
);
|
|
188
318
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
319
|
+
}
|
|
320
|
+
async deleteIndex({ indexName }) {
|
|
321
|
+
try {
|
|
322
|
+
await this.getCollection();
|
|
323
|
+
if (!(await this.listIndexes()).includes(indexName)) {
|
|
324
|
+
throw new Error(`Index ${indexName} does not exist`);
|
|
325
|
+
}
|
|
326
|
+
await this.scope.searchIndexes().dropIndex(indexName);
|
|
327
|
+
this.vector_dimension = null;
|
|
328
|
+
} catch (error) {
|
|
329
|
+
if (error instanceof MastraError) {
|
|
330
|
+
throw error;
|
|
331
|
+
}
|
|
332
|
+
throw new MastraError(
|
|
333
|
+
{
|
|
334
|
+
id: "COUCHBASE_VECTOR_DELETE_INDEX_FAILED",
|
|
335
|
+
domain: ErrorDomain.STORAGE,
|
|
336
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
337
|
+
details: {
|
|
338
|
+
indexName
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
error
|
|
342
|
+
);
|
|
197
343
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
347
|
+
* @param indexName - The name of the index containing the vector.
|
|
348
|
+
* @param id - The ID of the vector to update.
|
|
349
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
350
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
351
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
352
|
+
* @returns A promise that resolves when the update is complete.
|
|
353
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
354
|
+
*/
|
|
355
|
+
async updateVector({ id, update }) {
|
|
356
|
+
try {
|
|
357
|
+
if (!update.vector && !update.metadata) {
|
|
358
|
+
throw new Error("No updates provided");
|
|
359
|
+
}
|
|
360
|
+
if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) {
|
|
361
|
+
throw new Error("Vector dimension mismatch");
|
|
362
|
+
}
|
|
363
|
+
const collection = await this.getCollection();
|
|
364
|
+
try {
|
|
365
|
+
await collection.get(id);
|
|
366
|
+
} catch (err) {
|
|
367
|
+
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
368
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
206
369
|
}
|
|
370
|
+
throw err;
|
|
207
371
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
372
|
+
const specs = [];
|
|
373
|
+
if (update.vector) specs.push(MutateInSpec.replace("embedding", update.vector));
|
|
374
|
+
if (update.metadata) specs.push(MutateInSpec.replace("metadata", update.metadata));
|
|
375
|
+
await collection.mutateIn(id, specs);
|
|
376
|
+
} catch (error) {
|
|
377
|
+
throw new MastraError(
|
|
378
|
+
{
|
|
379
|
+
id: "COUCHBASE_VECTOR_UPDATE_FAILED",
|
|
380
|
+
domain: ErrorDomain.STORAGE,
|
|
381
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
382
|
+
details: {
|
|
383
|
+
id,
|
|
384
|
+
hasVectorUpdate: !!update.vector,
|
|
385
|
+
hasMetadataUpdate: !!update.metadata
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
error
|
|
389
|
+
);
|
|
214
390
|
}
|
|
215
|
-
return output;
|
|
216
391
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
392
|
+
/**
|
|
393
|
+
* Deletes a vector by its ID.
|
|
394
|
+
* @param indexName - The name of the index containing the vector.
|
|
395
|
+
* @param id - The ID of the vector to delete.
|
|
396
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
397
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
398
|
+
*/
|
|
399
|
+
async deleteVector({ id }) {
|
|
400
|
+
try {
|
|
401
|
+
const collection = await this.getCollection();
|
|
402
|
+
try {
|
|
403
|
+
await collection.get(id);
|
|
404
|
+
} catch (err) {
|
|
405
|
+
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
406
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
407
|
+
}
|
|
408
|
+
throw err;
|
|
409
|
+
}
|
|
410
|
+
await collection.remove(id);
|
|
411
|
+
} catch (error) {
|
|
412
|
+
throw new MastraError(
|
|
413
|
+
{
|
|
414
|
+
id: "COUCHBASE_VECTOR_DELETE_FAILED",
|
|
415
|
+
domain: ErrorDomain.STORAGE,
|
|
416
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
417
|
+
details: {
|
|
418
|
+
id
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
error
|
|
422
|
+
);
|
|
226
423
|
}
|
|
227
|
-
const index = await this.scope.searchIndexes().getIndex(indexName);
|
|
228
|
-
const dimensions = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.dims;
|
|
229
|
-
const count = -1;
|
|
230
|
-
const metric = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.similarity;
|
|
231
|
-
return {
|
|
232
|
-
dimension: dimensions,
|
|
233
|
-
count,
|
|
234
|
-
metric: Object.keys(DISTANCE_MAPPING).find(
|
|
235
|
-
(key) => DISTANCE_MAPPING[key] === metric
|
|
236
|
-
)
|
|
237
|
-
};
|
|
238
424
|
}
|
|
239
|
-
async
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
425
|
+
async disconnect() {
|
|
426
|
+
try {
|
|
427
|
+
if (!this.cluster) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
await this.cluster.close();
|
|
431
|
+
} catch (error) {
|
|
432
|
+
throw new MastraError(
|
|
433
|
+
{
|
|
434
|
+
id: "COUCHBASE_VECTOR_DISCONNECT_FAILED",
|
|
435
|
+
domain: ErrorDomain.STORAGE,
|
|
436
|
+
category: ErrorCategory.THIRD_PARTY
|
|
437
|
+
},
|
|
438
|
+
error
|
|
439
|
+
);
|
|
243
440
|
}
|
|
244
|
-
await this.scope.searchIndexes().dropIndex(indexName);
|
|
245
|
-
this.vector_dimension = null;
|
|
246
441
|
}
|
|
247
442
|
};
|
|
248
443
|
|
|
249
444
|
export { CouchbaseVector, DISTANCE_MAPPING };
|
|
445
|
+
//# sourceMappingURL=index.js.map
|
|
446
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vector/index.ts"],"names":[],"mappings":";;;;;AAkBO,IAAM,gBAAA,GAA0D;AAAA,EACrE,MAAA,EAAQ,QAAA;AAAA,EACR,SAAA,EAAW,SAAA;AAAA,EACX,UAAA,EAAY;AACd;AAWO,IAAM,eAAA,GAAN,cAA8B,YAAA,CAAa;AAAA,EACxC,cAAA;AAAA,EACA,OAAA;AAAA,EACA,UAAA;AAAA,EACA,cAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EACA,MAAA;AAAA,EACA,KAAA;AAAA,EACA,gBAAA;AAAA,EAER,WAAA,CAAY,EAAE,gBAAA,EAAkB,QAAA,EAAU,UAAU,UAAA,EAAY,SAAA,EAAW,gBAAe,EAA0B;AAClH,IAAA,KAAA,EAAM;AAEN,IAAA,IAAI;AACF,MAAA,IAAA,CAAK,cAAA,GAAiB,QAAQ,gBAAA,EAAkB;AAAA,QAC9C,QAAA;AAAA,QACA,QAAA;AAAA,QACA,aAAA,EAAe;AAAA,OAChB,CAAA;AACD,MAAA,IAAA,CAAK,OAAA,GAAU,IAAA;AACf,MAAA,IAAA,CAAK,UAAA,GAAa,UAAA;AAClB,MAAA,IAAA,CAAK,cAAA,GAAiB,cAAA;AACtB,MAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AACjB,MAAA,IAAA,C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AM,IAAI,WAAA;AAAA,QACR;AAAA,UACE,EAAA,EAAI,oCAAA;AAAA,UACJ,QAAQ,WAAA,CAAY,OAAA;AAAA,UACpB,UAAU,aAAA,CAAc;AAAA,SAC1B;AAAA,QACA;AAAA,OACF;AAAA,IACF;AAAA,EACF;AACF","file":"index.js","sourcesContent":["import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport { MastraVector } from '@mastra/core/vector';\nimport type {\n QueryResult,\n IndexStats,\n CreateIndexParams,\n UpsertVectorParams,\n QueryVectorParams,\n DescribeIndexParams,\n DeleteIndexParams,\n DeleteVectorParams,\n UpdateVectorParams,\n} from '@mastra/core/vector';\nimport type { Bucket, Cluster, Collection, Scope } from 'couchbase';\nimport { MutateInSpec, connect, SearchRequest, VectorQuery, VectorSearch } from 'couchbase';\n\ntype MastraMetric = 'cosine' | 'euclidean' | 'dotproduct';\ntype CouchbaseMetric = 'cosine' | 'l2_norm' | 'dot_product';\nexport const DISTANCE_MAPPING: Record<MastraMetric, CouchbaseMetric> = {\n cosine: 'cosine',\n euclidean: 'l2_norm',\n dotproduct: 'dot_product',\n};\n\nexport type CouchbaseVectorParams = {\n connectionString: string;\n username: string;\n password: string;\n bucketName: string;\n scopeName: string;\n collectionName: string;\n};\n\nexport class CouchbaseVector extends MastraVector {\n private clusterPromise: Promise<Cluster>;\n private cluster: Cluster;\n private bucketName: string;\n private collectionName: string;\n private scopeName: string;\n private collection: Collection;\n private bucket: Bucket;\n private scope: Scope;\n private vector_dimension: number;\n\n constructor({ connectionString, username, password, bucketName, scopeName, collectionName }: CouchbaseVectorParams) {\n super();\n\n try {\n this.clusterPromise = connect(connectionString, {\n username,\n password,\n configProfile: 'wanDevelopment',\n });\n this.cluster = null as unknown as Cluster;\n this.bucketName = bucketName;\n this.collectionName = collectionName;\n this.scopeName = scopeName;\n this.collection = null as unknown as Collection;\n this.bucket = null as unknown as Bucket;\n this.scope = null as unknown as Scope;\n this.vector_dimension = null as unknown as number;\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_INITIALIZE_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n connectionString,\n username,\n password,\n bucketName,\n scopeName,\n collectionName,\n },\n },\n error,\n );\n }\n }\n\n async getCollection() {\n if (!this.cluster) {\n this.cluster = await this.clusterPromise;\n }\n\n if (!this.collection) {\n this.bucket = this.cluster.bucket(this.bucketName);\n this.scope = this.bucket.scope(this.scopeName);\n this.collection = this.scope.collection(this.collectionName);\n }\n\n return this.collection;\n }\n\n async createIndex({ indexName, dimension, metric = 'dotproduct' as MastraMetric }: CreateIndexParams): Promise<void> {\n try {\n await this.getCollection();\n\n if (!Number.isInteger(dimension) || dimension <= 0) {\n throw new Error('Dimension must be a positive integer');\n }\n\n await this.scope.searchIndexes().upsertIndex({\n name: indexName,\n sourceName: this.bucketName,\n type: 'fulltext-index',\n params: {\n doc_config: {\n docid_prefix_delim: '',\n docid_regexp: '',\n mode: 'scope.collection.type_field',\n type_field: 'type',\n },\n mapping: {\n default_analyzer: 'standard',\n default_datetime_parser: 'dateTimeOptional',\n default_field: '_all',\n default_mapping: {\n dynamic: true,\n enabled: false,\n },\n default_type: '_default',\n docvalues_dynamic: true, // [Doc](https://docs.couchbase.com/server/current/search/search-index-params.html#params) mentions this attribute is required for vector search to return the indexed field\n index_dynamic: true,\n store_dynamic: true, // [Doc](https://docs.couchbase.com/server/current/search/search-index-params.html#params) mentions this attribute is required for vector search to return the indexed field\n type_field: '_type',\n types: {\n [`${this.scopeName}.${this.collectionName}`]: {\n dynamic: true,\n enabled: true,\n properties: {\n embedding: {\n enabled: true,\n fields: [\n {\n dims: dimension,\n index: true,\n name: 'embedding',\n similarity: DISTANCE_MAPPING[metric],\n type: 'vector',\n vector_index_optimized_for: 'recall',\n store: true, // CHANGED due to https://docs.couchbase.com/server/current/search/search-index-params.html#fields\n docvalues: true, // CHANGED due to https://docs.couchbase.com/server/current/search/search-index-params.html#fields\n include_term_vectors: true, // CHANGED due to https://docs.couchbase.com/server/current/search/search-index-params.html#fields\n },\n ],\n },\n content: {\n enabled: true,\n fields: [\n {\n index: true,\n name: 'content',\n store: true,\n type: 'text',\n },\n ],\n },\n },\n },\n },\n },\n store: {\n indexType: 'scorch',\n segmentVersion: 16,\n },\n },\n sourceUuid: '',\n sourceParams: {},\n sourceType: 'gocbcore',\n planParams: {\n maxPartitionsPerPIndex: 64,\n indexPartitions: 16,\n numReplicas: 0,\n },\n });\n this.vector_dimension = dimension;\n } catch (error: any) {\n // Check for 'already exists' error (Couchbase may throw a 400 or 409, or have a message)\n const message = error?.message || error?.toString();\n if (message && message.toLowerCase().includes('index exists')) {\n // Fetch index info and check dimension\n await this.validateExistingIndex(indexName, dimension, metric);\n return;\n }\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_CREATE_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n dimension,\n metric,\n },\n },\n error,\n );\n }\n }\n\n async upsert({ vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {\n try {\n await this.getCollection();\n\n if (!vectors || vectors.length === 0) {\n throw new Error('No vectors provided');\n }\n if (this.vector_dimension) {\n for (const vector of vectors) {\n if (!vector || this.vector_dimension !== vector.length) {\n throw new Error('Vector dimension mismatch');\n }\n }\n }\n\n const pointIds = ids || vectors.map(() => crypto.randomUUID());\n const records = vectors.map((vector, i) => {\n const metadataObj = metadata?.[i] || {};\n const record: Record<string, any> = {\n embedding: vector,\n metadata: metadataObj,\n };\n // If metadata has a text field, save it as content\n if (metadataObj.text) {\n record.content = metadataObj.text;\n }\n return record;\n });\n\n const allPromises = [];\n for (let i = 0; i < records.length; i++) {\n allPromises.push(this.collection.upsert(pointIds[i]!, records[i]));\n }\n await Promise.all(allPromises);\n\n return pointIds;\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_UPSERT_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n },\n error,\n );\n }\n }\n\n async query({ indexName, queryVector, topK = 10, includeVector = false }: QueryVectorParams): Promise<QueryResult[]> {\n try {\n await this.getCollection();\n\n const index_stats = await this.describeIndex({ indexName });\n if (queryVector.length !== index_stats.dimension) {\n throw new Error(\n `Query vector dimension mismatch. Expected ${index_stats.dimension}, got ${queryVector.length}`,\n );\n }\n\n let request = SearchRequest.create(\n VectorSearch.fromVectorQuery(VectorQuery.create('embedding', queryVector).numCandidates(topK)),\n );\n const results = await this.scope.search(indexName, request, {\n fields: ['*'],\n });\n\n if (includeVector) {\n throw new Error('Including vectors in search results is not yet supported by the Couchbase vector store');\n }\n const output = [];\n for (const match of results.rows) {\n const cleanedMetadata: Record<string, any> = {};\n const fields = (match.fields as Record<string, any>) || {}; // Ensure fields is an object\n for (const key in fields) {\n if (Object.prototype.hasOwnProperty.call(fields, key)) {\n const newKey = key.startsWith('metadata.') ? key.substring('metadata.'.length) : key;\n cleanedMetadata[newKey] = fields[key];\n }\n }\n output.push({\n id: match.id as string,\n score: (match.score as number) || 0,\n metadata: cleanedMetadata, // Use the cleaned metadata object\n });\n }\n return output;\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_QUERY_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n topK,\n },\n },\n error,\n );\n }\n }\n\n async listIndexes(): Promise<string[]> {\n try {\n await this.getCollection();\n const indexes = await this.scope.searchIndexes().getAllIndexes();\n return indexes?.map(index => index.name) || [];\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_LIST_INDEXES_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n },\n error,\n );\n }\n }\n\n /**\n * Retrieves statistics about a vector index.\n *\n * @param {string} indexName - The name of the index to describe\n * @returns A promise that resolves to the index statistics including dimension, count and metric\n */\n async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {\n try {\n await this.getCollection();\n if (!(await this.listIndexes()).includes(indexName)) {\n throw new Error(`Index ${indexName} does not exist`);\n }\n const index = await this.scope.searchIndexes().getIndex(indexName);\n const dimensions =\n index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]\n ?.dims;\n const count = -1; // Not added support yet for adding a count of documents covered by an index\n const metric = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding\n ?.fields?.[0]?.similarity as CouchbaseMetric;\n return {\n dimension: dimensions,\n count: count,\n metric: Object.keys(DISTANCE_MAPPING).find(\n key => DISTANCE_MAPPING[key as MastraMetric] === metric,\n ) as MastraMetric,\n };\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_DESCRIBE_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n },\n },\n error,\n );\n }\n }\n\n async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {\n try {\n await this.getCollection();\n if (!(await this.listIndexes()).includes(indexName)) {\n throw new Error(`Index ${indexName} does not exist`);\n }\n await this.scope.searchIndexes().dropIndex(indexName);\n this.vector_dimension = null as unknown as number;\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_DELETE_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n },\n },\n error,\n );\n }\n }\n\n /**\n * Updates a vector by its ID with the provided vector and/or metadata.\n * @param indexName - The name of the index containing the vector.\n * @param id - The ID of the vector to update.\n * @param update - An object containing the vector and/or metadata to update.\n * @param update.vector - An optional array of numbers representing the new vector.\n * @param update.metadata - An optional record containing the new metadata.\n * @returns A promise that resolves when the update is complete.\n * @throws Will throw an error if no updates are provided or if the update operation fails.\n */\n async updateVector({ id, update }: UpdateVectorParams): Promise<void> {\n try {\n if (!update.vector && !update.metadata) {\n throw new Error('No updates provided');\n }\n if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) {\n throw new Error('Vector dimension mismatch');\n }\n const collection = await this.getCollection();\n\n // Check if document exists\n try {\n await collection.get(id);\n } catch (err: any) {\n if (err.code === 13 || err.message?.includes('document not found')) {\n throw new Error(`Vector with id ${id} does not exist`);\n }\n throw err;\n }\n\n const specs: MutateInSpec[] = [];\n if (update.vector) specs.push(MutateInSpec.replace('embedding', update.vector));\n if (update.metadata) specs.push(MutateInSpec.replace('metadata', update.metadata));\n\n await collection.mutateIn(id, specs);\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_UPDATE_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n id,\n hasVectorUpdate: !!update.vector,\n hasMetadataUpdate: !!update.metadata,\n },\n },\n error,\n );\n }\n }\n\n /**\n * Deletes a vector by its ID.\n * @param indexName - The name of the index containing the vector.\n * @param id - The ID of the vector to delete.\n * @returns A promise that resolves when the deletion is complete.\n * @throws Will throw an error if the deletion operation fails.\n */\n async deleteVector({ id }: DeleteVectorParams): Promise<void> {\n try {\n const collection = await this.getCollection();\n\n // Check if document exists\n try {\n await collection.get(id);\n } catch (err: any) {\n if (err.code === 13 || err.message?.includes('document not found')) {\n throw new Error(`Vector with id ${id} does not exist`);\n }\n throw err;\n }\n\n await collection.remove(id);\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_DELETE_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n id,\n },\n },\n error,\n );\n }\n }\n\n async disconnect() {\n try {\n if (!this.cluster) {\n return;\n }\n await this.cluster.close();\n } catch (error) {\n throw new MastraError(\n {\n id: 'COUCHBASE_VECTOR_DISCONNECT_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n },\n error,\n );\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
+
import type { QueryResult, IndexStats, CreateIndexParams, UpsertVectorParams, QueryVectorParams, DescribeIndexParams, DeleteIndexParams, DeleteVectorParams, UpdateVectorParams } from '@mastra/core/vector';
|
|
3
|
+
import type { Collection } from 'couchbase';
|
|
4
|
+
type MastraMetric = 'cosine' | 'euclidean' | 'dotproduct';
|
|
5
|
+
type CouchbaseMetric = 'cosine' | 'l2_norm' | 'dot_product';
|
|
6
|
+
export declare const DISTANCE_MAPPING: Record<MastraMetric, CouchbaseMetric>;
|
|
7
|
+
export type CouchbaseVectorParams = {
|
|
8
|
+
connectionString: string;
|
|
9
|
+
username: string;
|
|
10
|
+
password: string;
|
|
11
|
+
bucketName: string;
|
|
12
|
+
scopeName: string;
|
|
13
|
+
collectionName: string;
|
|
14
|
+
};
|
|
15
|
+
export declare class CouchbaseVector extends MastraVector {
|
|
16
|
+
private clusterPromise;
|
|
17
|
+
private cluster;
|
|
18
|
+
private bucketName;
|
|
19
|
+
private collectionName;
|
|
20
|
+
private scopeName;
|
|
21
|
+
private collection;
|
|
22
|
+
private bucket;
|
|
23
|
+
private scope;
|
|
24
|
+
private vector_dimension;
|
|
25
|
+
constructor({ connectionString, username, password, bucketName, scopeName, collectionName }: CouchbaseVectorParams);
|
|
26
|
+
getCollection(): Promise<Collection>;
|
|
27
|
+
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
28
|
+
upsert({ vectors, metadata, ids }: UpsertVectorParams): Promise<string[]>;
|
|
29
|
+
query({ indexName, queryVector, topK, includeVector }: QueryVectorParams): Promise<QueryResult[]>;
|
|
30
|
+
listIndexes(): Promise<string[]>;
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves statistics about a vector index.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} indexName - The name of the index to describe
|
|
35
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
36
|
+
*/
|
|
37
|
+
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
38
|
+
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
41
|
+
* @param indexName - The name of the index containing the vector.
|
|
42
|
+
* @param id - The ID of the vector to update.
|
|
43
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
44
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
45
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
46
|
+
* @returns A promise that resolves when the update is complete.
|
|
47
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
48
|
+
*/
|
|
49
|
+
updateVector({ id, update }: UpdateVectorParams): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Deletes a vector by its ID.
|
|
52
|
+
* @param indexName - The name of the index containing the vector.
|
|
53
|
+
* @param id - The ID of the vector to delete.
|
|
54
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
55
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
56
|
+
*/
|
|
57
|
+
deleteVector({ id }: DeleteVectorParams): Promise<void>;
|
|
58
|
+
disconnect(): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
export {};
|
|
61
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/vector/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EACnB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAmB,UAAU,EAAS,MAAM,WAAW,CAAC;AAGpE,KAAK,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