@mastra/couchbase 0.0.0-vector-sources-20250516175436 → 0.0.0-vector-extension-schema-20250922130418
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 +470 -2
- package/LICENSE.md +11 -42
- package/README.md +11 -1
- package/dist/index.cjs +286 -168
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +284 -166
- 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 -86
- package/dist/_tsup-dts-rollup.d.ts +0 -86
- package/dist/index.d.cts +0 -3
- 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 -733
- package/src/vector/index.ts +0 -423
- package/src/vector/index.unit.test.ts +0 -737
- package/tsconfig.json +0 -5
- package/vitest.config.ts +0 -11
package/src/vector/index.ts
DELETED
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
-
import type {
|
|
3
|
-
QueryResult,
|
|
4
|
-
IndexStats,
|
|
5
|
-
CreateIndexParams,
|
|
6
|
-
UpsertVectorParams,
|
|
7
|
-
QueryVectorParams,
|
|
8
|
-
DescribeIndexParams,
|
|
9
|
-
ParamsToArgs,
|
|
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
|
-
/**
|
|
46
|
-
* @deprecated Passing parameters as positional arguments is deprecated.
|
|
47
|
-
* Use the object parameter instead. This signature will be removed on May 20th, 2025.
|
|
48
|
-
*/
|
|
49
|
-
constructor(
|
|
50
|
-
connectionString: string,
|
|
51
|
-
username: string,
|
|
52
|
-
password: string,
|
|
53
|
-
bucketName: string,
|
|
54
|
-
scopeName: string,
|
|
55
|
-
collectionName: string,
|
|
56
|
-
);
|
|
57
|
-
constructor(params: CouchbaseVectorParams);
|
|
58
|
-
constructor(
|
|
59
|
-
paramsOrConnectionString: CouchbaseVectorParams | string,
|
|
60
|
-
username?: string,
|
|
61
|
-
password?: string,
|
|
62
|
-
bucketName?: string,
|
|
63
|
-
scopeName?: string,
|
|
64
|
-
collectionName?: string,
|
|
65
|
-
) {
|
|
66
|
-
let connectionString_: string,
|
|
67
|
-
username_: string,
|
|
68
|
-
password_: string,
|
|
69
|
-
bucketName_: string,
|
|
70
|
-
scopeName_: string,
|
|
71
|
-
collectionName_: string;
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
typeof paramsOrConnectionString === 'object' &&
|
|
75
|
-
paramsOrConnectionString !== null &&
|
|
76
|
-
'connectionString' in paramsOrConnectionString
|
|
77
|
-
) {
|
|
78
|
-
// Object params (preferred)
|
|
79
|
-
connectionString_ = paramsOrConnectionString.connectionString as string;
|
|
80
|
-
username_ = paramsOrConnectionString.username;
|
|
81
|
-
password_ = paramsOrConnectionString.password;
|
|
82
|
-
bucketName_ = paramsOrConnectionString.bucketName;
|
|
83
|
-
scopeName_ = paramsOrConnectionString.scopeName;
|
|
84
|
-
collectionName_ = paramsOrConnectionString.collectionName;
|
|
85
|
-
} else {
|
|
86
|
-
// Positional args (deprecated)
|
|
87
|
-
if (arguments.length > 1) {
|
|
88
|
-
console.warn(
|
|
89
|
-
'Deprecation Warning: CouchbaseVector constructor positional arguments are deprecated. Please use a single object parameter instead. This signature will be removed on May 20th, 2025.',
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
connectionString_ = paramsOrConnectionString as string;
|
|
93
|
-
username_ = username!;
|
|
94
|
-
password_ = password!;
|
|
95
|
-
bucketName_ = bucketName!;
|
|
96
|
-
scopeName_ = scopeName!;
|
|
97
|
-
collectionName_ = collectionName!;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
super();
|
|
101
|
-
|
|
102
|
-
const baseClusterPromise = connect(connectionString_, {
|
|
103
|
-
username: username_,
|
|
104
|
-
password: password_,
|
|
105
|
-
configProfile: 'wanDevelopment',
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
const telemetry = this.__getTelemetry();
|
|
109
|
-
this.clusterPromise =
|
|
110
|
-
telemetry?.traceClass(baseClusterPromise, {
|
|
111
|
-
spanNamePrefix: 'couchbase-vector',
|
|
112
|
-
attributes: {
|
|
113
|
-
'vector.type': 'couchbase',
|
|
114
|
-
},
|
|
115
|
-
}) ?? baseClusterPromise;
|
|
116
|
-
this.cluster = null as unknown as Cluster;
|
|
117
|
-
this.bucketName = bucketName_;
|
|
118
|
-
this.collectionName = collectionName_;
|
|
119
|
-
this.scopeName = scopeName_;
|
|
120
|
-
this.collection = null as unknown as Collection;
|
|
121
|
-
this.bucket = null as unknown as Bucket;
|
|
122
|
-
this.scope = null as unknown as Scope;
|
|
123
|
-
this.vector_dimension = null as unknown as number;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async getCollection() {
|
|
127
|
-
if (!this.cluster) {
|
|
128
|
-
this.cluster = await this.clusterPromise;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (!this.collection) {
|
|
132
|
-
this.bucket = this.cluster.bucket(this.bucketName);
|
|
133
|
-
this.scope = this.bucket.scope(this.scopeName);
|
|
134
|
-
this.collection = this.scope.collection(this.collectionName);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return this.collection;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
async createIndex(params: CreateIndexParams): Promise<void> {
|
|
141
|
-
const { indexName, dimension, metric = 'dotproduct' as MastraMetric } = params;
|
|
142
|
-
await this.getCollection();
|
|
143
|
-
|
|
144
|
-
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
145
|
-
throw new Error('Dimension must be a positive integer');
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
await this.scope.searchIndexes().upsertIndex({
|
|
150
|
-
name: indexName,
|
|
151
|
-
sourceName: this.bucketName,
|
|
152
|
-
type: 'fulltext-index',
|
|
153
|
-
params: {
|
|
154
|
-
doc_config: {
|
|
155
|
-
docid_prefix_delim: '',
|
|
156
|
-
docid_regexp: '',
|
|
157
|
-
mode: 'scope.collection.type_field',
|
|
158
|
-
type_field: 'type',
|
|
159
|
-
},
|
|
160
|
-
mapping: {
|
|
161
|
-
default_analyzer: 'standard',
|
|
162
|
-
default_datetime_parser: 'dateTimeOptional',
|
|
163
|
-
default_field: '_all',
|
|
164
|
-
default_mapping: {
|
|
165
|
-
dynamic: true,
|
|
166
|
-
enabled: false,
|
|
167
|
-
},
|
|
168
|
-
default_type: '_default',
|
|
169
|
-
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
|
|
170
|
-
index_dynamic: true,
|
|
171
|
-
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
|
|
172
|
-
type_field: '_type',
|
|
173
|
-
types: {
|
|
174
|
-
[`${this.scopeName}.${this.collectionName}`]: {
|
|
175
|
-
dynamic: true,
|
|
176
|
-
enabled: true,
|
|
177
|
-
properties: {
|
|
178
|
-
embedding: {
|
|
179
|
-
enabled: true,
|
|
180
|
-
fields: [
|
|
181
|
-
{
|
|
182
|
-
dims: dimension,
|
|
183
|
-
index: true,
|
|
184
|
-
name: 'embedding',
|
|
185
|
-
similarity: DISTANCE_MAPPING[metric],
|
|
186
|
-
type: 'vector',
|
|
187
|
-
vector_index_optimized_for: 'recall',
|
|
188
|
-
store: true, // CHANGED due to https://docs.couchbase.com/server/current/search/search-index-params.html#fields
|
|
189
|
-
docvalues: true, // CHANGED due to https://docs.couchbase.com/server/current/search/search-index-params.html#fields
|
|
190
|
-
include_term_vectors: true, // CHANGED due to https://docs.couchbase.com/server/current/search/search-index-params.html#fields
|
|
191
|
-
},
|
|
192
|
-
],
|
|
193
|
-
},
|
|
194
|
-
content: {
|
|
195
|
-
enabled: true,
|
|
196
|
-
fields: [
|
|
197
|
-
{
|
|
198
|
-
index: true,
|
|
199
|
-
name: 'content',
|
|
200
|
-
store: true,
|
|
201
|
-
type: 'text',
|
|
202
|
-
},
|
|
203
|
-
],
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
},
|
|
208
|
-
},
|
|
209
|
-
store: {
|
|
210
|
-
indexType: 'scorch',
|
|
211
|
-
segmentVersion: 16,
|
|
212
|
-
},
|
|
213
|
-
},
|
|
214
|
-
sourceUuid: '',
|
|
215
|
-
sourceParams: {},
|
|
216
|
-
sourceType: 'gocbcore',
|
|
217
|
-
planParams: {
|
|
218
|
-
maxPartitionsPerPIndex: 64,
|
|
219
|
-
indexPartitions: 16,
|
|
220
|
-
numReplicas: 0,
|
|
221
|
-
},
|
|
222
|
-
});
|
|
223
|
-
this.vector_dimension = dimension;
|
|
224
|
-
} catch (error: any) {
|
|
225
|
-
// Check for 'already exists' error (Couchbase may throw a 400 or 409, or have a message)
|
|
226
|
-
const message = error?.message || error?.toString();
|
|
227
|
-
if (message && message.toLowerCase().includes('index exists')) {
|
|
228
|
-
// Fetch index info and check dimension
|
|
229
|
-
await this.validateExistingIndex(indexName, dimension, metric);
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
throw error;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
async upsert(params: UpsertVectorParams): Promise<string[]> {
|
|
237
|
-
const { vectors, metadata, ids } = params;
|
|
238
|
-
await this.getCollection();
|
|
239
|
-
|
|
240
|
-
if (!vectors || vectors.length === 0) {
|
|
241
|
-
throw new Error('No vectors provided');
|
|
242
|
-
}
|
|
243
|
-
if (this.vector_dimension) {
|
|
244
|
-
for (const vector of vectors) {
|
|
245
|
-
if (!vector || this.vector_dimension !== vector.length) {
|
|
246
|
-
throw new Error('Vector dimension mismatch');
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const pointIds = ids || vectors.map(() => crypto.randomUUID());
|
|
252
|
-
const records = vectors.map((vector, i) => {
|
|
253
|
-
const metadataObj = metadata?.[i] || {};
|
|
254
|
-
const record: Record<string, any> = {
|
|
255
|
-
embedding: vector,
|
|
256
|
-
metadata: metadataObj,
|
|
257
|
-
};
|
|
258
|
-
// If metadata has a text field, save it as content
|
|
259
|
-
if (metadataObj.text) {
|
|
260
|
-
record.content = metadataObj.text;
|
|
261
|
-
}
|
|
262
|
-
return record;
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
const allPromises = [];
|
|
266
|
-
for (let i = 0; i < records.length; i++) {
|
|
267
|
-
allPromises.push(this.collection.upsert(pointIds[i]!, records[i]));
|
|
268
|
-
}
|
|
269
|
-
await Promise.all(allPromises);
|
|
270
|
-
|
|
271
|
-
return pointIds;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
async query(params: QueryVectorParams): Promise<QueryResult[]> {
|
|
275
|
-
const { indexName, queryVector, topK = 10, includeVector = false } = params;
|
|
276
|
-
|
|
277
|
-
await this.getCollection();
|
|
278
|
-
|
|
279
|
-
const index_stats = await this.describeIndex(indexName);
|
|
280
|
-
if (queryVector.length !== index_stats.dimension) {
|
|
281
|
-
throw new Error(`Query vector dimension mismatch. Expected ${index_stats.dimension}, got ${queryVector.length}`);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
let request = SearchRequest.create(
|
|
285
|
-
VectorSearch.fromVectorQuery(VectorQuery.create('embedding', queryVector).numCandidates(topK)),
|
|
286
|
-
);
|
|
287
|
-
const results = await this.scope.search(indexName, request, {
|
|
288
|
-
fields: ['*'],
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
if (includeVector) {
|
|
292
|
-
throw new Error('Including vectors in search results is not yet supported by the Couchbase vector store');
|
|
293
|
-
}
|
|
294
|
-
const output = [];
|
|
295
|
-
for (const match of results.rows) {
|
|
296
|
-
const cleanedMetadata: Record<string, any> = {};
|
|
297
|
-
const fields = (match.fields as Record<string, any>) || {}; // Ensure fields is an object
|
|
298
|
-
for (const key in fields) {
|
|
299
|
-
if (Object.prototype.hasOwnProperty.call(fields, key)) {
|
|
300
|
-
const newKey = key.startsWith('metadata.') ? key.substring('metadata.'.length) : key;
|
|
301
|
-
cleanedMetadata[newKey] = fields[key];
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
output.push({
|
|
305
|
-
id: match.id as string,
|
|
306
|
-
score: (match.score as number) || 0,
|
|
307
|
-
metadata: cleanedMetadata, // Use the cleaned metadata object
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
return output;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
async listIndexes(): Promise<string[]> {
|
|
314
|
-
await this.getCollection();
|
|
315
|
-
const indexes = await this.scope.searchIndexes().getAllIndexes();
|
|
316
|
-
return indexes?.map(index => index.name) || [];
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Retrieves statistics about a vector index.
|
|
321
|
-
*
|
|
322
|
-
* @param params - The parameters for describing an index
|
|
323
|
-
* @param params.indexName - The name of the index to describe
|
|
324
|
-
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
325
|
-
*/
|
|
326
|
-
async describeIndex(...args: ParamsToArgs<DescribeIndexParams>): Promise<IndexStats> {
|
|
327
|
-
const params = this.normalizeArgs<DescribeIndexParams>('describeIndex', args);
|
|
328
|
-
const { indexName } = params;
|
|
329
|
-
await this.getCollection();
|
|
330
|
-
if (!(await this.listIndexes()).includes(indexName)) {
|
|
331
|
-
throw new Error(`Index ${indexName} does not exist`);
|
|
332
|
-
}
|
|
333
|
-
const index = await this.scope.searchIndexes().getIndex(indexName);
|
|
334
|
-
const dimensions =
|
|
335
|
-
index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding?.fields?.[0]
|
|
336
|
-
?.dims;
|
|
337
|
-
const count = -1; // Not added support yet for adding a count of documents covered by an index
|
|
338
|
-
const metric = index.params.mapping?.types?.[`${this.scopeName}.${this.collectionName}`]?.properties?.embedding
|
|
339
|
-
?.fields?.[0]?.similarity as CouchbaseMetric;
|
|
340
|
-
return {
|
|
341
|
-
dimension: dimensions,
|
|
342
|
-
count: count,
|
|
343
|
-
metric: Object.keys(DISTANCE_MAPPING).find(
|
|
344
|
-
key => DISTANCE_MAPPING[key as MastraMetric] === metric,
|
|
345
|
-
) as MastraMetric,
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
async deleteIndex(...args: ParamsToArgs<DeleteIndexParams>): Promise<void> {
|
|
350
|
-
const params = this.normalizeArgs<DeleteIndexParams>('deleteIndex', args);
|
|
351
|
-
const { indexName } = params;
|
|
352
|
-
await this.getCollection();
|
|
353
|
-
if (!(await this.listIndexes()).includes(indexName)) {
|
|
354
|
-
throw new Error(`Index ${indexName} does not exist`);
|
|
355
|
-
}
|
|
356
|
-
await this.scope.searchIndexes().dropIndex(indexName);
|
|
357
|
-
this.vector_dimension = null as unknown as number;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
362
|
-
* @param indexName - The name of the index containing the vector.
|
|
363
|
-
* @param id - The ID of the vector to update.
|
|
364
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
365
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
366
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
367
|
-
* @returns A promise that resolves when the update is complete.
|
|
368
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
369
|
-
*/
|
|
370
|
-
async updateVector(...args: ParamsToArgs<UpdateVectorParams>): Promise<void> {
|
|
371
|
-
const params = this.normalizeArgs<UpdateVectorParams>('updateVector', args);
|
|
372
|
-
const { id, update } = params;
|
|
373
|
-
if (!update.vector && !update.metadata) {
|
|
374
|
-
throw new Error('No updates provided');
|
|
375
|
-
}
|
|
376
|
-
if (update.vector && this.vector_dimension && update.vector.length !== this.vector_dimension) {
|
|
377
|
-
throw new Error('Vector dimension mismatch');
|
|
378
|
-
}
|
|
379
|
-
const collection = await this.getCollection();
|
|
380
|
-
|
|
381
|
-
// Check if document exists
|
|
382
|
-
try {
|
|
383
|
-
await collection.get(id);
|
|
384
|
-
} catch (err: any) {
|
|
385
|
-
if (err.code === 13 || err.message?.includes('document not found')) {
|
|
386
|
-
throw new Error(`Vector with id ${id} does not exist`);
|
|
387
|
-
}
|
|
388
|
-
throw err;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
const specs: MutateInSpec[] = [];
|
|
392
|
-
if (update.vector) specs.push(MutateInSpec.replace('embedding', update.vector));
|
|
393
|
-
if (update.metadata) specs.push(MutateInSpec.replace('metadata', update.metadata));
|
|
394
|
-
|
|
395
|
-
await collection.mutateIn(id, specs);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* Deletes a vector by its ID.
|
|
400
|
-
* @param indexName - The name of the index containing the vector.
|
|
401
|
-
* @param id - The ID of the vector to delete.
|
|
402
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
403
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
404
|
-
*/
|
|
405
|
-
async deleteVector(...args: ParamsToArgs<DeleteVectorParams>): Promise<void> {
|
|
406
|
-
const params = this.normalizeArgs<DeleteVectorParams>('deleteVector', args);
|
|
407
|
-
|
|
408
|
-
const { id } = params;
|
|
409
|
-
const collection = await this.getCollection();
|
|
410
|
-
|
|
411
|
-
// Check if document exists
|
|
412
|
-
try {
|
|
413
|
-
await collection.get(id);
|
|
414
|
-
} catch (err: any) {
|
|
415
|
-
if (err.code === 13 || err.message?.includes('document not found')) {
|
|
416
|
-
throw new Error(`Vector with id ${id} does not exist`);
|
|
417
|
-
}
|
|
418
|
-
throw err;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
await collection.remove(id);
|
|
422
|
-
}
|
|
423
|
-
}
|