@mastra/couchbase 0.11.4 → 0.11.7-alpha.0

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