@mastra/couchbase 1.1.0 → 1.1.1
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 +18 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-rag-vector-databases.md +28 -4
- package/dist/docs/references/reference-vectors-couchbase.md +6 -4
- package/dist/index.cjs +376 -483
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +374 -480
- package/dist/index.js.map +1 -1
- package/package.json +17 -16
package/dist/index.cjs
CHANGED
|
@@ -1,488 +1,381 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
euclidean: "l2_norm",
|
|
12
|
-
dotproduct: "dot_product"
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _mastra_core_error = require("@mastra/core/error");
|
|
3
|
+
let _mastra_core_storage = require("@mastra/core/storage");
|
|
4
|
+
let _mastra_core_vector = require("@mastra/core/vector");
|
|
5
|
+
let couchbase = require("couchbase");
|
|
6
|
+
//#region src/vector/index.ts
|
|
7
|
+
const DISTANCE_MAPPING = {
|
|
8
|
+
cosine: "cosine",
|
|
9
|
+
euclidean: "l2_norm",
|
|
10
|
+
dotproduct: "dot_product"
|
|
13
11
|
};
|
|
14
|
-
var CouchbaseVector = class extends
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
domain: error.ErrorDomain.STORAGE,
|
|
379
|
-
category: error.ErrorCategory.USER,
|
|
380
|
-
text: "id is required for Couchbase updateVector",
|
|
381
|
-
details: {}
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
try {
|
|
385
|
-
if (!update.vector && !update.metadata) {
|
|
386
|
-
throw new Error("No updates provided");
|
|
387
|
-
}
|
|
388
|
-
if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) {
|
|
389
|
-
throw new Error("Vector dimension mismatch");
|
|
390
|
-
}
|
|
391
|
-
const collection = await this.getCollection();
|
|
392
|
-
try {
|
|
393
|
-
await collection.get(id);
|
|
394
|
-
} catch (err) {
|
|
395
|
-
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
396
|
-
throw new Error(`Vector with id ${id} does not exist`);
|
|
397
|
-
}
|
|
398
|
-
throw err;
|
|
399
|
-
}
|
|
400
|
-
const specs = [];
|
|
401
|
-
if (update.vector) specs.push(couchbase.MutateInSpec.replace("embedding", update.vector));
|
|
402
|
-
if (update.metadata) specs.push(couchbase.MutateInSpec.replace("metadata", update.metadata));
|
|
403
|
-
await collection.mutateIn(id, specs);
|
|
404
|
-
} catch (error$1) {
|
|
405
|
-
throw new error.MastraError(
|
|
406
|
-
{
|
|
407
|
-
id: storage.createVectorErrorId("COUCHBASE", "UPDATE_VECTOR", "FAILED"),
|
|
408
|
-
domain: error.ErrorDomain.STORAGE,
|
|
409
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
410
|
-
details: {
|
|
411
|
-
...id && { id },
|
|
412
|
-
hasVectorUpdate: !!update.vector,
|
|
413
|
-
hasMetadataUpdate: !!update.metadata
|
|
414
|
-
}
|
|
415
|
-
},
|
|
416
|
-
error$1
|
|
417
|
-
);
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Deletes a vector by its ID.
|
|
422
|
-
* @param indexName - The name of the index containing the vector.
|
|
423
|
-
* @param id - The ID of the vector to delete.
|
|
424
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
425
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
426
|
-
*/
|
|
427
|
-
async deleteVector({ id }) {
|
|
428
|
-
try {
|
|
429
|
-
const collection = await this.getCollection();
|
|
430
|
-
try {
|
|
431
|
-
await collection.get(id);
|
|
432
|
-
} catch (err) {
|
|
433
|
-
if (err.code === 13 || err.message?.includes("document not found")) {
|
|
434
|
-
throw new Error(`Vector with id ${id} does not exist`);
|
|
435
|
-
}
|
|
436
|
-
throw err;
|
|
437
|
-
}
|
|
438
|
-
await collection.remove(id);
|
|
439
|
-
} catch (error$1) {
|
|
440
|
-
throw new error.MastraError(
|
|
441
|
-
{
|
|
442
|
-
id: storage.createVectorErrorId("COUCHBASE", "DELETE_VECTOR", "FAILED"),
|
|
443
|
-
domain: error.ErrorDomain.STORAGE,
|
|
444
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
445
|
-
details: {
|
|
446
|
-
...id && { id }
|
|
447
|
-
}
|
|
448
|
-
},
|
|
449
|
-
error$1
|
|
450
|
-
);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
async deleteVectors({ indexName, filter, ids }) {
|
|
454
|
-
throw new error.MastraError({
|
|
455
|
-
id: storage.createVectorErrorId("COUCHBASE", "DELETE_VECTORS", "NOT_SUPPORTED"),
|
|
456
|
-
text: "deleteVectors is not yet implemented for Couchbase vector store",
|
|
457
|
-
domain: error.ErrorDomain.STORAGE,
|
|
458
|
-
category: error.ErrorCategory.SYSTEM,
|
|
459
|
-
details: {
|
|
460
|
-
indexName,
|
|
461
|
-
...filter && { filter: JSON.stringify(filter) },
|
|
462
|
-
...ids && { idsCount: ids.length }
|
|
463
|
-
}
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
async disconnect() {
|
|
467
|
-
try {
|
|
468
|
-
if (!this.cluster) {
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
await this.cluster.close();
|
|
472
|
-
} catch (error$1) {
|
|
473
|
-
throw new error.MastraError(
|
|
474
|
-
{
|
|
475
|
-
id: storage.createVectorErrorId("COUCHBASE", "DISCONNECT", "FAILED"),
|
|
476
|
-
domain: error.ErrorDomain.STORAGE,
|
|
477
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
478
|
-
},
|
|
479
|
-
error$1
|
|
480
|
-
);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
12
|
+
var CouchbaseVector = class extends _mastra_core_vector.MastraVector {
|
|
13
|
+
clusterPromise;
|
|
14
|
+
cluster;
|
|
15
|
+
bucketName;
|
|
16
|
+
collectionName;
|
|
17
|
+
scopeName;
|
|
18
|
+
collection;
|
|
19
|
+
bucket;
|
|
20
|
+
scope;
|
|
21
|
+
vector_dimension;
|
|
22
|
+
constructor({ id, connectionString, username, password, bucketName, scopeName, collectionName }) {
|
|
23
|
+
super({ id });
|
|
24
|
+
try {
|
|
25
|
+
this.clusterPromise = (0, couchbase.connect)(connectionString, {
|
|
26
|
+
username,
|
|
27
|
+
password,
|
|
28
|
+
configProfile: "wanDevelopment"
|
|
29
|
+
});
|
|
30
|
+
this.cluster = null;
|
|
31
|
+
this.bucketName = bucketName;
|
|
32
|
+
this.collectionName = collectionName;
|
|
33
|
+
this.scopeName = scopeName;
|
|
34
|
+
this.collection = null;
|
|
35
|
+
this.bucket = null;
|
|
36
|
+
this.scope = null;
|
|
37
|
+
this.vector_dimension = null;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw new _mastra_core_error.MastraError({
|
|
40
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "INITIALIZE", "FAILED"),
|
|
41
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
42
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
43
|
+
details: {
|
|
44
|
+
connectionString,
|
|
45
|
+
username,
|
|
46
|
+
bucketName,
|
|
47
|
+
scopeName,
|
|
48
|
+
collectionName
|
|
49
|
+
}
|
|
50
|
+
}, error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async getCollection() {
|
|
54
|
+
if (!this.cluster) this.cluster = await this.clusterPromise;
|
|
55
|
+
if (!this.collection) {
|
|
56
|
+
this.bucket = this.cluster.bucket(this.bucketName);
|
|
57
|
+
this.scope = this.bucket.scope(this.scopeName);
|
|
58
|
+
this.collection = this.scope.collection(this.collectionName);
|
|
59
|
+
}
|
|
60
|
+
return this.collection;
|
|
61
|
+
}
|
|
62
|
+
async createIndex({ indexName, dimension, metric = "dotproduct" }) {
|
|
63
|
+
try {
|
|
64
|
+
await this.getCollection();
|
|
65
|
+
if (!Number.isInteger(dimension) || dimension <= 0) throw new Error("Dimension must be a positive integer");
|
|
66
|
+
await this.scope.searchIndexes().upsertIndex({
|
|
67
|
+
name: indexName,
|
|
68
|
+
sourceName: this.bucketName,
|
|
69
|
+
type: "fulltext-index",
|
|
70
|
+
params: {
|
|
71
|
+
doc_config: {
|
|
72
|
+
docid_prefix_delim: "",
|
|
73
|
+
docid_regexp: "",
|
|
74
|
+
mode: "scope.collection.type_field",
|
|
75
|
+
type_field: "type"
|
|
76
|
+
},
|
|
77
|
+
mapping: {
|
|
78
|
+
default_analyzer: "standard",
|
|
79
|
+
default_datetime_parser: "dateTimeOptional",
|
|
80
|
+
default_field: "_all",
|
|
81
|
+
default_mapping: {
|
|
82
|
+
dynamic: true,
|
|
83
|
+
enabled: false
|
|
84
|
+
},
|
|
85
|
+
default_type: "_default",
|
|
86
|
+
docvalues_dynamic: true,
|
|
87
|
+
index_dynamic: true,
|
|
88
|
+
store_dynamic: true,
|
|
89
|
+
type_field: "_type",
|
|
90
|
+
types: { [`${this.scopeName}.${this.collectionName}`]: {
|
|
91
|
+
dynamic: true,
|
|
92
|
+
enabled: true,
|
|
93
|
+
properties: {
|
|
94
|
+
embedding: {
|
|
95
|
+
enabled: true,
|
|
96
|
+
fields: [{
|
|
97
|
+
dims: dimension,
|
|
98
|
+
index: true,
|
|
99
|
+
name: "embedding",
|
|
100
|
+
similarity: DISTANCE_MAPPING[metric],
|
|
101
|
+
type: "vector",
|
|
102
|
+
vector_index_optimized_for: "recall",
|
|
103
|
+
store: true,
|
|
104
|
+
docvalues: true,
|
|
105
|
+
include_term_vectors: true
|
|
106
|
+
}]
|
|
107
|
+
},
|
|
108
|
+
content: {
|
|
109
|
+
enabled: true,
|
|
110
|
+
fields: [{
|
|
111
|
+
index: true,
|
|
112
|
+
name: "content",
|
|
113
|
+
store: true,
|
|
114
|
+
type: "text"
|
|
115
|
+
}]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} }
|
|
119
|
+
},
|
|
120
|
+
store: {
|
|
121
|
+
indexType: "scorch",
|
|
122
|
+
segmentVersion: 16
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
sourceUuid: "",
|
|
126
|
+
sourceParams: {},
|
|
127
|
+
sourceType: "gocbcore",
|
|
128
|
+
planParams: {
|
|
129
|
+
maxPartitionsPerPIndex: 64,
|
|
130
|
+
indexPartitions: 16,
|
|
131
|
+
numReplicas: 0
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
this.vector_dimension = dimension;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
const message = error?.message || error?.toString();
|
|
137
|
+
if (message && message.toLowerCase().includes("index exists")) {
|
|
138
|
+
await this.validateExistingIndex(indexName, dimension, metric);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
throw new _mastra_core_error.MastraError({
|
|
142
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "CREATE_INDEX", "FAILED"),
|
|
143
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
144
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
145
|
+
details: {
|
|
146
|
+
indexName,
|
|
147
|
+
dimension,
|
|
148
|
+
metric
|
|
149
|
+
}
|
|
150
|
+
}, error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async upsert({ vectors, metadata, ids }) {
|
|
154
|
+
try {
|
|
155
|
+
await this.getCollection();
|
|
156
|
+
if (!vectors || vectors.length === 0) throw new Error("No vectors provided");
|
|
157
|
+
if (this.vector_dimension) {
|
|
158
|
+
for (const vector of vectors) if (!vector || this.vector_dimension !== vector.length) throw new Error("Vector dimension mismatch");
|
|
159
|
+
}
|
|
160
|
+
const pointIds = ids || vectors.map(() => crypto.randomUUID());
|
|
161
|
+
const records = vectors.map((vector, i) => {
|
|
162
|
+
const metadataObj = metadata?.[i] || {};
|
|
163
|
+
const record = {
|
|
164
|
+
embedding: vector,
|
|
165
|
+
metadata: metadataObj
|
|
166
|
+
};
|
|
167
|
+
if (metadataObj.text) record.content = metadataObj.text;
|
|
168
|
+
return record;
|
|
169
|
+
});
|
|
170
|
+
const allPromises = [];
|
|
171
|
+
for (let i = 0; i < records.length; i++) allPromises.push(this.collection.upsert(pointIds[i], records[i]));
|
|
172
|
+
await Promise.all(allPromises);
|
|
173
|
+
return pointIds;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
throw new _mastra_core_error.MastraError({
|
|
176
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "UPSERT", "FAILED"),
|
|
177
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
178
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY
|
|
179
|
+
}, error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async query({ indexName, queryVector, topK = 10, includeVector = false }) {
|
|
183
|
+
if (!queryVector) throw new _mastra_core_error.MastraError({
|
|
184
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "QUERY", "MISSING_VECTOR"),
|
|
185
|
+
text: "queryVector is required for Couchbase queries. Metadata-only queries are not supported by this vector store.",
|
|
186
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
187
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
188
|
+
details: { indexName }
|
|
189
|
+
});
|
|
190
|
+
try {
|
|
191
|
+
await this.getCollection();
|
|
192
|
+
const index_stats = await this.describeIndex({ indexName });
|
|
193
|
+
if (queryVector.length !== index_stats.dimension) throw new Error(`Query vector dimension mismatch. Expected ${index_stats.dimension}, got ${queryVector.length}`);
|
|
194
|
+
let request = couchbase.SearchRequest.create(couchbase.VectorSearch.fromVectorQuery(couchbase.VectorQuery.create("embedding", queryVector).numCandidates(topK)));
|
|
195
|
+
const results = await this.scope.search(indexName, request, { fields: ["*"] });
|
|
196
|
+
if (includeVector) throw new Error("Including vectors in search results is not yet supported by the Couchbase vector store");
|
|
197
|
+
const output = [];
|
|
198
|
+
for (const match of results.rows) {
|
|
199
|
+
const cleanedMetadata = {};
|
|
200
|
+
const fields = match.fields || {};
|
|
201
|
+
for (const key in fields) if (Object.prototype.hasOwnProperty.call(fields, key)) {
|
|
202
|
+
const newKey = key.startsWith("metadata.") ? key.substring(9) : key;
|
|
203
|
+
cleanedMetadata[newKey] = fields[key];
|
|
204
|
+
}
|
|
205
|
+
output.push({
|
|
206
|
+
id: match.id,
|
|
207
|
+
score: match.score || 0,
|
|
208
|
+
metadata: cleanedMetadata
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return output;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
throw new _mastra_core_error.MastraError({
|
|
214
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "QUERY", "FAILED"),
|
|
215
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
216
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
217
|
+
details: {
|
|
218
|
+
indexName,
|
|
219
|
+
topK
|
|
220
|
+
}
|
|
221
|
+
}, error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async listIndexes() {
|
|
225
|
+
try {
|
|
226
|
+
await this.getCollection();
|
|
227
|
+
return (await this.scope.searchIndexes().getAllIndexes())?.map((index) => index.name) || [];
|
|
228
|
+
} catch (error) {
|
|
229
|
+
throw new _mastra_core_error.MastraError({
|
|
230
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "LIST_INDEXES", "FAILED"),
|
|
231
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
232
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY
|
|
233
|
+
}, error);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Retrieves statistics about a vector index.
|
|
238
|
+
*
|
|
239
|
+
* @param {string} indexName - The name of the index to describe
|
|
240
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
241
|
+
*/
|
|
242
|
+
async describeIndex({ indexName }) {
|
|
243
|
+
try {
|
|
244
|
+
await this.getCollection();
|
|
245
|
+
if (!(await this.listIndexes()).includes(indexName)) throw new Error(`Index ${indexName} does not exist`);
|
|
246
|
+
const index = await this.scope.searchIndexes().getIndex(indexName);
|
|
247
|
+
const dimensions = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.dims;
|
|
248
|
+
const count = -1;
|
|
249
|
+
const metric = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]?.similarity;
|
|
250
|
+
return {
|
|
251
|
+
dimension: dimensions,
|
|
252
|
+
count,
|
|
253
|
+
metric: Object.keys(DISTANCE_MAPPING).find((key) => DISTANCE_MAPPING[key] === metric)
|
|
254
|
+
};
|
|
255
|
+
} catch (error) {
|
|
256
|
+
throw new _mastra_core_error.MastraError({
|
|
257
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "DESCRIBE_INDEX", "FAILED"),
|
|
258
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
259
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
260
|
+
details: { indexName }
|
|
261
|
+
}, error);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async deleteIndex({ indexName }) {
|
|
265
|
+
try {
|
|
266
|
+
await this.getCollection();
|
|
267
|
+
if (!(await this.listIndexes()).includes(indexName)) throw new Error(`Index ${indexName} does not exist`);
|
|
268
|
+
await this.scope.searchIndexes().dropIndex(indexName);
|
|
269
|
+
this.vector_dimension = null;
|
|
270
|
+
} catch (error) {
|
|
271
|
+
if (error instanceof _mastra_core_error.MastraError) throw error;
|
|
272
|
+
throw new _mastra_core_error.MastraError({
|
|
273
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "DELETE_INDEX", "FAILED"),
|
|
274
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
275
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
276
|
+
details: { indexName }
|
|
277
|
+
}, error);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
282
|
+
* @param indexName - The name of the index containing the vector.
|
|
283
|
+
* @param id - The ID of the vector to update.
|
|
284
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
285
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
286
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
287
|
+
* @returns A promise that resolves when the update is complete.
|
|
288
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
289
|
+
*/
|
|
290
|
+
async updateVector({ id, update }) {
|
|
291
|
+
if (!id) throw new _mastra_core_error.MastraError({
|
|
292
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "UPDATE_VECTOR", "INVALID_ARGS"),
|
|
293
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
294
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
295
|
+
text: "id is required for Couchbase updateVector",
|
|
296
|
+
details: {}
|
|
297
|
+
});
|
|
298
|
+
try {
|
|
299
|
+
if (!update.vector && !update.metadata) throw new Error("No updates provided");
|
|
300
|
+
if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) throw new Error("Vector dimension mismatch");
|
|
301
|
+
const collection = await this.getCollection();
|
|
302
|
+
try {
|
|
303
|
+
await collection.get(id);
|
|
304
|
+
} catch (err) {
|
|
305
|
+
if (err.code === 13 || err.message?.includes("document not found")) throw new Error(`Vector with id ${id} does not exist`);
|
|
306
|
+
throw err;
|
|
307
|
+
}
|
|
308
|
+
const specs = [];
|
|
309
|
+
if (update.vector) specs.push(couchbase.MutateInSpec.replace("embedding", update.vector));
|
|
310
|
+
if (update.metadata) specs.push(couchbase.MutateInSpec.replace("metadata", update.metadata));
|
|
311
|
+
await collection.mutateIn(id, specs);
|
|
312
|
+
} catch (error) {
|
|
313
|
+
throw new _mastra_core_error.MastraError({
|
|
314
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "UPDATE_VECTOR", "FAILED"),
|
|
315
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
316
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
317
|
+
details: {
|
|
318
|
+
...id && { id },
|
|
319
|
+
hasVectorUpdate: !!update.vector,
|
|
320
|
+
hasMetadataUpdate: !!update.metadata
|
|
321
|
+
}
|
|
322
|
+
}, error);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Deletes a vector by its ID.
|
|
327
|
+
* @param indexName - The name of the index containing the vector.
|
|
328
|
+
* @param id - The ID of the vector to delete.
|
|
329
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
330
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
331
|
+
*/
|
|
332
|
+
async deleteVector({ id }) {
|
|
333
|
+
try {
|
|
334
|
+
const collection = await this.getCollection();
|
|
335
|
+
try {
|
|
336
|
+
await collection.get(id);
|
|
337
|
+
} catch (err) {
|
|
338
|
+
if (err.code === 13 || err.message?.includes("document not found")) throw new Error(`Vector with id ${id} does not exist`);
|
|
339
|
+
throw err;
|
|
340
|
+
}
|
|
341
|
+
await collection.remove(id);
|
|
342
|
+
} catch (error) {
|
|
343
|
+
throw new _mastra_core_error.MastraError({
|
|
344
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "DELETE_VECTOR", "FAILED"),
|
|
345
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
346
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
347
|
+
details: { ...id && { id } }
|
|
348
|
+
}, error);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
async deleteVectors({ indexName, filter, ids }) {
|
|
352
|
+
throw new _mastra_core_error.MastraError({
|
|
353
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "DELETE_VECTORS", "NOT_SUPPORTED"),
|
|
354
|
+
text: "deleteVectors is not yet implemented for Couchbase vector store",
|
|
355
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
356
|
+
category: _mastra_core_error.ErrorCategory.SYSTEM,
|
|
357
|
+
details: {
|
|
358
|
+
indexName,
|
|
359
|
+
...filter && { filter: JSON.stringify(filter) },
|
|
360
|
+
...ids && { idsCount: ids.length }
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
async disconnect() {
|
|
365
|
+
try {
|
|
366
|
+
if (!this.cluster) return;
|
|
367
|
+
await this.cluster.close();
|
|
368
|
+
} catch (error) {
|
|
369
|
+
throw new _mastra_core_error.MastraError({
|
|
370
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("COUCHBASE", "DISCONNECT", "FAILED"),
|
|
371
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
372
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY
|
|
373
|
+
}, error);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
483
376
|
};
|
|
484
|
-
|
|
377
|
+
//#endregion
|
|
485
378
|
exports.CouchbaseVector = CouchbaseVector;
|
|
486
379
|
exports.DISTANCE_MAPPING = DISTANCE_MAPPING;
|
|
487
|
-
|
|
380
|
+
|
|
488
381
|
//# sourceMappingURL=index.cjs.map
|