@mastra/couchbase 0.0.0-vnext-inngest-20250508131921 → 0.0.0-vnext-20251119160359
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 +705 -2
- package/LICENSE.md +11 -42
- package/README.md +11 -1
- package/dist/index.cjs +317 -112
- 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 +316 -111
- package/dist/index.js.map +1 -0
- package/dist/vector/index.d.ts +63 -0
- package/dist/vector/index.d.ts.map +1 -0
- package/package.json +36 -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,48 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
17
18
|
bucket;
|
|
18
19
|
scope;
|
|
19
20
|
vector_dimension;
|
|
20
|
-
constructor(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
21
|
+
constructor({
|
|
22
|
+
id,
|
|
23
|
+
connectionString,
|
|
24
|
+
username,
|
|
25
|
+
password,
|
|
26
|
+
bucketName,
|
|
27
|
+
scopeName,
|
|
28
|
+
collectionName
|
|
29
|
+
}) {
|
|
30
|
+
super({ id });
|
|
31
|
+
try {
|
|
32
|
+
this.clusterPromise = connect(connectionString, {
|
|
33
|
+
username,
|
|
34
|
+
password,
|
|
35
|
+
configProfile: "wanDevelopment"
|
|
36
|
+
});
|
|
37
|
+
this.cluster = null;
|
|
38
|
+
this.bucketName = bucketName;
|
|
39
|
+
this.collectionName = collectionName;
|
|
40
|
+
this.scopeName = scopeName;
|
|
41
|
+
this.collection = null;
|
|
42
|
+
this.bucket = null;
|
|
43
|
+
this.scope = null;
|
|
44
|
+
this.vector_dimension = null;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw new MastraError(
|
|
47
|
+
{
|
|
48
|
+
id: "COUCHBASE_VECTOR_INITIALIZE_FAILED",
|
|
49
|
+
domain: ErrorDomain.STORAGE,
|
|
50
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
51
|
+
details: {
|
|
52
|
+
connectionString,
|
|
53
|
+
username,
|
|
54
|
+
password,
|
|
55
|
+
bucketName,
|
|
56
|
+
scopeName,
|
|
57
|
+
collectionName
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
error
|
|
61
|
+
);
|
|
62
|
+
}
|
|
42
63
|
}
|
|
43
64
|
async getCollection() {
|
|
44
65
|
if (!this.cluster) {
|
|
@@ -51,13 +72,12 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
51
72
|
}
|
|
52
73
|
return this.collection;
|
|
53
74
|
}
|
|
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
|
-
}
|
|
75
|
+
async createIndex({ indexName, dimension, metric = "dotproduct" }) {
|
|
60
76
|
try {
|
|
77
|
+
await this.getCollection();
|
|
78
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
79
|
+
throw new Error("Dimension must be a positive integer");
|
|
80
|
+
}
|
|
61
81
|
await this.scope.searchIndexes().upsertIndex({
|
|
62
82
|
name: indexName,
|
|
63
83
|
sourceName: this.bucketName,
|
|
@@ -144,106 +164,291 @@ var CouchbaseVector = class extends MastraVector {
|
|
|
144
164
|
await this.validateExistingIndex(indexName, dimension, metric);
|
|
145
165
|
return;
|
|
146
166
|
}
|
|
147
|
-
throw
|
|
167
|
+
throw new MastraError(
|
|
168
|
+
{
|
|
169
|
+
id: "COUCHBASE_VECTOR_CREATE_INDEX_FAILED",
|
|
170
|
+
domain: ErrorDomain.STORAGE,
|
|
171
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
172
|
+
details: {
|
|
173
|
+
indexName,
|
|
174
|
+
dimension,
|
|
175
|
+
metric
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
error
|
|
179
|
+
);
|
|
148
180
|
}
|
|
149
181
|
}
|
|
150
|
-
async upsert(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
182
|
+
async upsert({ vectors, metadata, ids }) {
|
|
183
|
+
try {
|
|
184
|
+
await this.getCollection();
|
|
185
|
+
if (!vectors || vectors.length === 0) {
|
|
186
|
+
throw new Error("No vectors provided");
|
|
187
|
+
}
|
|
188
|
+
if (this.vector_dimension) {
|
|
189
|
+
for (const vector of vectors) {
|
|
190
|
+
if (!vector || this.vector_dimension !== vector.length) {
|
|
191
|
+
throw new Error("Vector dimension mismatch");
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const pointIds = ids || vectors.map(() => crypto.randomUUID());
|
|
196
|
+
const records = vectors.map((vector, i) => {
|
|
197
|
+
const metadataObj = metadata?.[i] || {};
|
|
198
|
+
const record = {
|
|
199
|
+
embedding: vector,
|
|
200
|
+
metadata: metadataObj
|
|
201
|
+
};
|
|
202
|
+
if (metadataObj.text) {
|
|
203
|
+
record.content = metadataObj.text;
|
|
204
|
+
}
|
|
205
|
+
return record;
|
|
206
|
+
});
|
|
207
|
+
const allPromises = [];
|
|
208
|
+
for (let i = 0; i < records.length; i++) {
|
|
209
|
+
allPromises.push(this.collection.upsert(pointIds[i], records[i]));
|
|
210
|
+
}
|
|
211
|
+
await Promise.all(allPromises);
|
|
212
|
+
return pointIds;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
throw new MastraError(
|
|
215
|
+
{
|
|
216
|
+
id: "COUCHBASE_VECTOR_UPSERT_FAILED",
|
|
217
|
+
domain: ErrorDomain.STORAGE,
|
|
218
|
+
category: ErrorCategory.THIRD_PARTY
|
|
219
|
+
},
|
|
220
|
+
error
|
|
221
|
+
);
|
|
155
222
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
223
|
+
}
|
|
224
|
+
async query({ indexName, queryVector, topK = 10, includeVector = false }) {
|
|
225
|
+
try {
|
|
226
|
+
await this.getCollection();
|
|
227
|
+
const index_stats = await this.describeIndex({ indexName });
|
|
228
|
+
if (queryVector.length !== index_stats.dimension) {
|
|
229
|
+
throw new Error(
|
|
230
|
+
`Query vector dimension mismatch. Expected ${index_stats.dimension}, got ${queryVector.length}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
let request = SearchRequest.create(
|
|
234
|
+
VectorSearch.fromVectorQuery(VectorQuery.create("embedding", queryVector).numCandidates(topK))
|
|
235
|
+
);
|
|
236
|
+
const results = await this.scope.search(indexName, request, {
|
|
237
|
+
fields: ["*"]
|
|
238
|
+
});
|
|
239
|
+
if (includeVector) {
|
|
240
|
+
throw new Error("Including vectors in search results is not yet supported by the Couchbase vector store");
|
|
241
|
+
}
|
|
242
|
+
const output = [];
|
|
243
|
+
for (const match of results.rows) {
|
|
244
|
+
const cleanedMetadata = {};
|
|
245
|
+
const fields = match.fields || {};
|
|
246
|
+
for (const key in fields) {
|
|
247
|
+
if (Object.prototype.hasOwnProperty.call(fields, key)) {
|
|
248
|
+
const newKey = key.startsWith("metadata.") ? key.substring("metadata.".length) : key;
|
|
249
|
+
cleanedMetadata[newKey] = fields[key];
|
|
250
|
+
}
|
|
160
251
|
}
|
|
252
|
+
output.push({
|
|
253
|
+
id: match.id,
|
|
254
|
+
score: match.score || 0,
|
|
255
|
+
metadata: cleanedMetadata
|
|
256
|
+
// Use the cleaned metadata object
|
|
257
|
+
});
|
|
161
258
|
}
|
|
259
|
+
return output;
|
|
260
|
+
} catch (error) {
|
|
261
|
+
throw new MastraError(
|
|
262
|
+
{
|
|
263
|
+
id: "COUCHBASE_VECTOR_QUERY_FAILED",
|
|
264
|
+
domain: ErrorDomain.STORAGE,
|
|
265
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
266
|
+
details: {
|
|
267
|
+
indexName,
|
|
268
|
+
topK
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
error
|
|
272
|
+
);
|
|
162
273
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
274
|
+
}
|
|
275
|
+
async listIndexes() {
|
|
276
|
+
try {
|
|
277
|
+
await this.getCollection();
|
|
278
|
+
const indexes = await this.scope.searchIndexes().getAllIndexes();
|
|
279
|
+
return indexes?.map((index) => index.name) || [];
|
|
280
|
+
} catch (error) {
|
|
281
|
+
throw new MastraError(
|
|
282
|
+
{
|
|
283
|
+
id: "COUCHBASE_VECTOR_LIST_INDEXES_FAILED",
|
|
284
|
+
domain: ErrorDomain.STORAGE,
|
|
285
|
+
category: ErrorCategory.THIRD_PARTY
|
|
286
|
+
},
|
|
287
|
+
error
|
|
288
|
+
);
|
|
178
289
|
}
|
|
179
|
-
await Promise.all(allPromises);
|
|
180
|
-
return pointIds;
|
|
181
290
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
291
|
+
/**
|
|
292
|
+
* Retrieves statistics about a vector index.
|
|
293
|
+
*
|
|
294
|
+
* @param {string} indexName - The name of the index to describe
|
|
295
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
296
|
+
*/
|
|
297
|
+
async describeIndex({ indexName }) {
|
|
298
|
+
try {
|
|
299
|
+
await this.getCollection();
|
|
300
|
+
if (!(await this.listIndexes()).includes(indexName)) {
|
|
301
|
+
throw new Error(`Index ${indexName} does not exist`);
|
|
302
|
+
}
|
|
303
|
+
const index = await this.scope.searchIndexes().getIndex(indexName);
|
|
304
|
+
const dimensions = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.dims;
|
|
305
|
+
const count = -1;
|
|
306
|
+
const metric = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.similarity;
|
|
307
|
+
return {
|
|
308
|
+
dimension: dimensions,
|
|
309
|
+
count,
|
|
310
|
+
metric: Object.keys(DISTANCE_MAPPING).find(
|
|
311
|
+
(key) => DISTANCE_MAPPING[key] === metric
|
|
312
|
+
)
|
|
313
|
+
};
|
|
314
|
+
} catch (error) {
|
|
315
|
+
throw new MastraError(
|
|
316
|
+
{
|
|
317
|
+
id: "COUCHBASE_VECTOR_DESCRIBE_INDEX_FAILED",
|
|
318
|
+
domain: ErrorDomain.STORAGE,
|
|
319
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
320
|
+
details: {
|
|
321
|
+
indexName
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
error
|
|
325
|
+
);
|
|
188
326
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
327
|
+
}
|
|
328
|
+
async deleteIndex({ indexName }) {
|
|
329
|
+
try {
|
|
330
|
+
await this.getCollection();
|
|
331
|
+
if (!(await this.listIndexes()).includes(indexName)) {
|
|
332
|
+
throw new Error(`Index ${indexName} does not exist`);
|
|
333
|
+
}
|
|
334
|
+
await this.scope.searchIndexes().dropIndex(indexName);
|
|
335
|
+
this.vector_dimension = null;
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (error instanceof MastraError) {
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
throw new MastraError(
|
|
341
|
+
{
|
|
342
|
+
id: "COUCHBASE_VECTOR_DELETE_INDEX_FAILED",
|
|
343
|
+
domain: ErrorDomain.STORAGE,
|
|
344
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
345
|
+
details: {
|
|
346
|
+
indexName
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
error
|
|
350
|
+
);
|
|
197
351
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
355
|
+
* @param indexName - The name of the index containing the vector.
|
|
356
|
+
* @param id - The ID of the vector to update.
|
|
357
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
358
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
359
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
360
|
+
* @returns A promise that resolves when the update is complete.
|
|
361
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
362
|
+
*/
|
|
363
|
+
async updateVector({ id, update }) {
|
|
364
|
+
try {
|
|
365
|
+
if (!update.vector && !update.metadata) {
|
|
366
|
+
throw new Error("No updates provided");
|
|
367
|
+
}
|
|
368
|
+
if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) {
|
|
369
|
+
throw new Error("Vector dimension mismatch");
|
|
370
|
+
}
|
|
371
|
+
const collection = await this.getCollection();
|
|
372
|
+
try {
|
|
373
|
+
await collection.get(id);
|
|
374
|
+
} catch (err) {
|
|
375
|
+
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
376
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
206
377
|
}
|
|
378
|
+
throw err;
|
|
207
379
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
380
|
+
const specs = [];
|
|
381
|
+
if (update.vector) specs.push(MutateInSpec.replace("embedding", update.vector));
|
|
382
|
+
if (update.metadata) specs.push(MutateInSpec.replace("metadata", update.metadata));
|
|
383
|
+
await collection.mutateIn(id, specs);
|
|
384
|
+
} catch (error) {
|
|
385
|
+
throw new MastraError(
|
|
386
|
+
{
|
|
387
|
+
id: "COUCHBASE_VECTOR_UPDATE_FAILED",
|
|
388
|
+
domain: ErrorDomain.STORAGE,
|
|
389
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
390
|
+
details: {
|
|
391
|
+
id,
|
|
392
|
+
hasVectorUpdate: !!update.vector,
|
|
393
|
+
hasMetadataUpdate: !!update.metadata
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
error
|
|
397
|
+
);
|
|
214
398
|
}
|
|
215
|
-
return output;
|
|
216
399
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
400
|
+
/**
|
|
401
|
+
* Deletes a vector by its ID.
|
|
402
|
+
* @param indexName - The name of the index containing the vector.
|
|
403
|
+
* @param id - The ID of the vector to delete.
|
|
404
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
405
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
406
|
+
*/
|
|
407
|
+
async deleteVector({ id }) {
|
|
408
|
+
try {
|
|
409
|
+
const collection = await this.getCollection();
|
|
410
|
+
try {
|
|
411
|
+
await collection.get(id);
|
|
412
|
+
} catch (err) {
|
|
413
|
+
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
414
|
+
throw new Error(`Vector with id ${id} does not exist`);
|
|
415
|
+
}
|
|
416
|
+
throw err;
|
|
417
|
+
}
|
|
418
|
+
await collection.remove(id);
|
|
419
|
+
} catch (error) {
|
|
420
|
+
throw new MastraError(
|
|
421
|
+
{
|
|
422
|
+
id: "COUCHBASE_VECTOR_DELETE_FAILED",
|
|
423
|
+
domain: ErrorDomain.STORAGE,
|
|
424
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
425
|
+
details: {
|
|
426
|
+
id
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
error
|
|
430
|
+
);
|
|
226
431
|
}
|
|
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
432
|
}
|
|
239
|
-
async
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
433
|
+
async disconnect() {
|
|
434
|
+
try {
|
|
435
|
+
if (!this.cluster) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
await this.cluster.close();
|
|
439
|
+
} catch (error) {
|
|
440
|
+
throw new MastraError(
|
|
441
|
+
{
|
|
442
|
+
id: "COUCHBASE_VECTOR_DISCONNECT_FAILED",
|
|
443
|
+
domain: ErrorDomain.STORAGE,
|
|
444
|
+
category: ErrorCategory.THIRD_PARTY
|
|
445
|
+
},
|
|
446
|
+
error
|
|
447
|
+
);
|
|
243
448
|
}
|
|
244
|
-
await this.scope.searchIndexes().dropIndex(indexName);
|
|
245
|
-
this.vector_dimension = null;
|
|
246
449
|
}
|
|
247
450
|
};
|
|
248
451
|
|
|
249
452
|
export { CouchbaseVector, DISTANCE_MAPPING };
|
|
453
|
+
//# sourceMappingURL=index.js.map
|
|
454
|
+
//# 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;AAAA,IACV,EAAA;AAAA,IACA,gBAAA;AAAA,IACA,QAAA;AAAA,IACA,QAAA;AAAA,IACA,UAAA;AAAA,IACA,SAAA;AAAA,IACA;AAAA,GACF,EAA2C;AACzC,IAAA,KAAA,CAAM,EAAE,IAAI,CAAA;AAEZ,IAAA,IAAI;AACF,MAAA,IAAA,CAAK,cAAA,GAAiB,QAAQ,gBAAA,EAAkB;AAAA,QAC9C,QAAA;AAAA,QACA,QAAA;AAAA,QACA,aAAA,EAAe;AAAA,OA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EA,MAAM,UAAA,GAAa;AACjB,IAAA,IAAI;AACF,MAAA,IAAI,CAAC,KAAK,OAAA,EAAS;AACjB,QAAA;AAAA,MACF;AACA,MAAA,MAAM,IAAA,CAAK,QAAQ,KAAA,EAAM;AAAA,IAC3B,SAAS,KAAA,EAAO;AACd,MAAA,MAAM,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({\n id,\n connectionString,\n username,\n password,\n bucketName,\n scopeName,\n collectionName,\n }: CouchbaseVectorParams & { id: string }) {\n super({ id });\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,63 @@
|
|
|
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({ id, connectionString, username, password, bucketName, scopeName, collectionName, }: CouchbaseVectorParams & {
|
|
26
|
+
id: string;
|
|
27
|
+
});
|
|
28
|
+
getCollection(): Promise<Collection>;
|
|
29
|
+
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
30
|
+
upsert({ vectors, metadata, ids }: UpsertVectorParams): Promise<string[]>;
|
|
31
|
+
query({ indexName, queryVector, topK, includeVector }: QueryVectorParams): Promise<QueryResult[]>;
|
|
32
|
+
listIndexes(): Promise<string[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Retrieves statistics about a vector index.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} indexName - The name of the index to describe
|
|
37
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
38
|
+
*/
|
|
39
|
+
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
40
|
+
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
43
|
+
* @param indexName - The name of the index containing the vector.
|
|
44
|
+
* @param id - The ID of the vector to update.
|
|
45
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
46
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
47
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
48
|
+
* @returns A promise that resolves when the update is complete.
|
|
49
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
50
|
+
*/
|
|
51
|
+
updateVector({ id, update }: UpdateVectorParams): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Deletes a vector by its ID.
|
|
54
|
+
* @param indexName - The name of the index containing the vector.
|
|
55
|
+
* @param id - The ID of the vector to delete.
|
|
56
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
57
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
58
|
+
*/
|
|
59
|
+
deleteVector({ id }: DeleteVectorParams): Promise<void>;
|
|
60
|
+
disconnect(): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
|
63
|
+
//# 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,YA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