@mastra/mongodb 0.0.0-working-memory-per-user-20250620163010 → 0.0.0-zod-v4-compat-part-2-20250822105954
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 +251 -3
- package/LICENSE.md +11 -42
- package/dist/index.cjs +1846 -505
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -7
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1815 -474
- package/dist/index.js.map +1 -0
- package/dist/storage/MongoDBConnector.d.ts +23 -0
- package/dist/storage/MongoDBConnector.d.ts.map +1 -0
- package/dist/storage/connectors/MongoDBConnector.d.ts +23 -0
- package/dist/storage/connectors/MongoDBConnector.d.ts.map +1 -0
- package/dist/storage/connectors/base.d.ts +6 -0
- package/dist/storage/connectors/base.d.ts.map +1 -0
- package/dist/storage/domains/legacy-evals/index.d.ts +18 -0
- package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +80 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/operations/index.d.ts +38 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +41 -0
- package/dist/storage/domains/scores/index.d.ts.map +1 -0
- package/dist/storage/domains/traces/index.d.ts +18 -0
- package/dist/storage/domains/traces/index.d.ts.map +1 -0
- package/dist/storage/domains/utils.d.ts +8 -0
- package/dist/storage/domains/utils.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +33 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +178 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/types.d.ts +11 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/vector/filter.d.ts +21 -0
- package/dist/vector/filter.d.ts.map +1 -0
- package/dist/vector/index.d.ts +78 -0
- package/dist/vector/index.d.ts.map +1 -0
- package/dist/vector/prompt.d.ts +6 -0
- package/dist/vector/prompt.d.ts.map +1 -0
- package/docker-compose.yaml +1 -1
- package/package.json +10 -10
- package/src/index.ts +1 -0
- package/src/storage/MongoDBConnector.ts +93 -0
- package/src/storage/connectors/MongoDBConnector.ts +93 -0
- package/src/storage/connectors/base.ts +7 -0
- package/src/storage/domains/legacy-evals/index.ts +193 -0
- package/src/storage/domains/memory/index.ts +741 -0
- package/src/storage/domains/operations/index.ts +155 -0
- package/src/storage/domains/scores/index.ts +379 -0
- package/src/storage/domains/traces/index.ts +142 -0
- package/src/storage/domains/utils.ts +43 -0
- package/src/storage/domains/workflows/index.ts +196 -0
- package/src/storage/index.test.ts +27 -989
- package/src/storage/index.ts +241 -605
- package/src/storage/types.ts +14 -0
- package/src/vector/filter.test.ts +40 -30
- package/src/vector/filter.ts +25 -4
- package/src/vector/index.test.ts +48 -3
- package/src/vector/index.ts +301 -131
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +1 -1
- package/tsup.config.ts +22 -0
- package/dist/_tsup-dts-rollup.d.cts +0 -274
- package/dist/_tsup-dts-rollup.d.ts +0 -274
- package/dist/index.d.cts +0 -7
package/src/vector/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
|
|
1
2
|
import { MastraVector } from '@mastra/core/vector';
|
|
2
3
|
import type {
|
|
3
4
|
QueryResult,
|
|
@@ -10,20 +11,20 @@ import type {
|
|
|
10
11
|
DeleteVectorParams,
|
|
11
12
|
UpdateVectorParams,
|
|
12
13
|
} from '@mastra/core/vector';
|
|
13
|
-
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
14
14
|
import { MongoClient } from 'mongodb';
|
|
15
15
|
import type { MongoClientOptions, Document, Db, Collection } from 'mongodb';
|
|
16
16
|
import { v4 as uuidv4 } from 'uuid';
|
|
17
17
|
|
|
18
18
|
import { MongoDBFilterTranslator } from './filter';
|
|
19
|
+
import type { MongoDBVectorFilter } from './filter';
|
|
19
20
|
|
|
20
21
|
// Define necessary types and interfaces
|
|
21
22
|
export interface MongoDBUpsertVectorParams extends UpsertVectorParams {
|
|
22
23
|
documents?: string[];
|
|
23
24
|
}
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
documentFilter?:
|
|
26
|
+
interface MongoDBQueryVectorParams extends QueryVectorParams<MongoDBVectorFilter> {
|
|
27
|
+
documentFilter?: MongoDBVectorFilter;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export interface MongoDBIndexReadyParams {
|
|
@@ -41,7 +42,7 @@ interface MongoDBDocument extends Document {
|
|
|
41
42
|
[key: string]: any; // Index signature for additional properties
|
|
42
43
|
}
|
|
43
44
|
// The MongoDBVector class
|
|
44
|
-
export class MongoDBVector extends MastraVector {
|
|
45
|
+
export class MongoDBVector extends MastraVector<MongoDBVectorFilter> {
|
|
45
46
|
private client: MongoClient;
|
|
46
47
|
private db: Db;
|
|
47
48
|
private collections: Map<string, Collection<MongoDBDocument>>;
|
|
@@ -64,38 +65,78 @@ export class MongoDBVector extends MastraVector {
|
|
|
64
65
|
|
|
65
66
|
// Public methods
|
|
66
67
|
async connect(): Promise<void> {
|
|
67
|
-
|
|
68
|
+
try {
|
|
69
|
+
await this.client.connect();
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new MastraError(
|
|
72
|
+
{
|
|
73
|
+
id: 'STORAGE_MONGODB_VECTOR_CONNECT_FAILED',
|
|
74
|
+
domain: ErrorDomain.STORAGE,
|
|
75
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
76
|
+
},
|
|
77
|
+
error,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
68
80
|
}
|
|
69
81
|
|
|
70
82
|
async disconnect(): Promise<void> {
|
|
71
|
-
|
|
83
|
+
try {
|
|
84
|
+
await this.client.close();
|
|
85
|
+
} catch (error) {
|
|
86
|
+
throw new MastraError(
|
|
87
|
+
{
|
|
88
|
+
id: 'STORAGE_MONGODB_VECTOR_DISCONNECT_FAILED',
|
|
89
|
+
domain: ErrorDomain.STORAGE,
|
|
90
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
91
|
+
},
|
|
92
|
+
error,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
72
95
|
}
|
|
73
96
|
|
|
74
97
|
async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
98
|
+
let mongoMetric;
|
|
99
|
+
try {
|
|
100
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
101
|
+
throw new Error('Dimension must be a positive integer');
|
|
102
|
+
}
|
|
78
103
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
104
|
+
mongoMetric = this.mongoMetricMap[metric];
|
|
105
|
+
if (!mongoMetric) {
|
|
106
|
+
throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
throw new MastraError(
|
|
110
|
+
{
|
|
111
|
+
id: 'STORAGE_MONGODB_VECTOR_CREATE_INDEX_INVALID_ARGS',
|
|
112
|
+
domain: ErrorDomain.STORAGE,
|
|
113
|
+
category: ErrorCategory.USER,
|
|
114
|
+
details: {
|
|
115
|
+
indexName,
|
|
116
|
+
dimension,
|
|
117
|
+
metric,
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
error,
|
|
121
|
+
);
|
|
82
122
|
}
|
|
83
123
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
await this.db.
|
|
88
|
-
|
|
89
|
-
|
|
124
|
+
let collection;
|
|
125
|
+
try {
|
|
126
|
+
// Check if collection exists
|
|
127
|
+
const collectionExists = await this.db.listCollections({ name: indexName }).hasNext();
|
|
128
|
+
if (!collectionExists) {
|
|
129
|
+
await this.db.createCollection(indexName);
|
|
130
|
+
}
|
|
131
|
+
collection = await this.getCollection(indexName);
|
|
90
132
|
|
|
91
|
-
|
|
133
|
+
const indexNameInternal = `${indexName}_vector_index`;
|
|
92
134
|
|
|
93
|
-
|
|
94
|
-
|
|
135
|
+
const embeddingField = this.embeddingFieldName;
|
|
136
|
+
const numDimensions = dimension;
|
|
95
137
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
await (collection as any).createSearchIndex({
|
|
138
|
+
// Create search indexes
|
|
139
|
+
await collection.createSearchIndex({
|
|
99
140
|
definition: {
|
|
100
141
|
fields: [
|
|
101
142
|
{
|
|
@@ -104,19 +145,53 @@ export class MongoDBVector extends MastraVector {
|
|
|
104
145
|
numDimensions: numDimensions,
|
|
105
146
|
similarity: mongoMetric,
|
|
106
147
|
},
|
|
148
|
+
{
|
|
149
|
+
type: 'filter',
|
|
150
|
+
path: '_id',
|
|
151
|
+
},
|
|
107
152
|
],
|
|
108
153
|
},
|
|
109
154
|
name: indexNameInternal,
|
|
110
155
|
type: 'vectorSearch',
|
|
111
156
|
});
|
|
157
|
+
await collection.createSearchIndex({
|
|
158
|
+
definition: {
|
|
159
|
+
mappings: {
|
|
160
|
+
dynamic: true,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
name: `${indexName}_search_index`,
|
|
164
|
+
type: 'search',
|
|
165
|
+
});
|
|
112
166
|
} catch (error: any) {
|
|
113
167
|
if (error.codeName !== 'IndexAlreadyExists') {
|
|
114
|
-
throw
|
|
168
|
+
throw new MastraError(
|
|
169
|
+
{
|
|
170
|
+
id: 'STORAGE_MONGODB_VECTOR_CREATE_INDEX_FAILED',
|
|
171
|
+
domain: ErrorDomain.STORAGE,
|
|
172
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
173
|
+
},
|
|
174
|
+
error,
|
|
175
|
+
);
|
|
115
176
|
}
|
|
116
177
|
}
|
|
117
178
|
|
|
118
|
-
|
|
119
|
-
|
|
179
|
+
try {
|
|
180
|
+
// Store the dimension and metric in a special metadata document
|
|
181
|
+
await collection?.updateOne({ _id: '__index_metadata__' }, { $set: { dimension, metric } }, { upsert: true });
|
|
182
|
+
} catch (error) {
|
|
183
|
+
throw new MastraError(
|
|
184
|
+
{
|
|
185
|
+
id: 'STORAGE_MONGODB_VECTOR_CREATE_INDEX_FAILED_STORE_METADATA',
|
|
186
|
+
domain: ErrorDomain.STORAGE,
|
|
187
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
188
|
+
details: {
|
|
189
|
+
indexName,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
error,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
120
195
|
}
|
|
121
196
|
|
|
122
197
|
/**
|
|
@@ -149,55 +224,68 @@ export class MongoDBVector extends MastraVector {
|
|
|
149
224
|
}
|
|
150
225
|
|
|
151
226
|
async upsert({ indexName, vectors, metadata, ids, documents }: MongoDBUpsertVectorParams): Promise<string[]> {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
this.collectionForValidation = collection;
|
|
227
|
+
try {
|
|
228
|
+
const collection = await this.getCollection(indexName);
|
|
155
229
|
|
|
156
|
-
|
|
157
|
-
const stats = await this.describeIndex({ indexName });
|
|
230
|
+
this.collectionForValidation = collection;
|
|
158
231
|
|
|
159
|
-
|
|
160
|
-
|
|
232
|
+
// Get index stats to check dimension
|
|
233
|
+
const stats = await this.describeIndex({ indexName });
|
|
161
234
|
|
|
162
|
-
|
|
163
|
-
|
|
235
|
+
// Validate vector dimensions
|
|
236
|
+
await this.validateVectorDimensions(vectors, stats.dimension);
|
|
164
237
|
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
const meta = metadata?.[idx] || {};
|
|
168
|
-
const doc = documents?.[idx];
|
|
238
|
+
// Generate IDs if not provided
|
|
239
|
+
const generatedIds = ids || vectors.map(() => uuidv4());
|
|
169
240
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return acc;
|
|
175
|
-
},
|
|
176
|
-
{} as Record<string, any>,
|
|
177
|
-
);
|
|
241
|
+
const operations = vectors.map((vector, idx) => {
|
|
242
|
+
const id = generatedIds[idx];
|
|
243
|
+
const meta = metadata?.[idx] || {};
|
|
244
|
+
const doc = documents?.[idx];
|
|
178
245
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
246
|
+
// Normalize metadata - convert Date objects to ISO strings
|
|
247
|
+
const normalizedMeta = Object.keys(meta).reduce(
|
|
248
|
+
(acc, key) => {
|
|
249
|
+
acc[key] = meta[key] instanceof Date ? meta[key].toISOString() : meta[key];
|
|
250
|
+
return acc;
|
|
251
|
+
},
|
|
252
|
+
{} as Record<string, any>,
|
|
253
|
+
);
|
|
186
254
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
255
|
+
const updateDoc: Partial<MongoDBDocument> = {
|
|
256
|
+
[this.embeddingFieldName]: vector,
|
|
257
|
+
[this.metadataFieldName]: normalizedMeta,
|
|
258
|
+
};
|
|
259
|
+
if (doc !== undefined) {
|
|
260
|
+
updateDoc[this.documentFieldName] = doc;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
updateOne: {
|
|
265
|
+
filter: { _id: id }, // '_id' is a string as per MongoDBDocument interface
|
|
266
|
+
update: { $set: updateDoc },
|
|
267
|
+
upsert: true,
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
});
|
|
195
271
|
|
|
196
|
-
|
|
272
|
+
await collection.bulkWrite(operations);
|
|
197
273
|
|
|
198
|
-
|
|
274
|
+
return generatedIds;
|
|
275
|
+
} catch (error) {
|
|
276
|
+
throw new MastraError(
|
|
277
|
+
{
|
|
278
|
+
id: 'STORAGE_MONGODB_VECTOR_UPSERT_FAILED',
|
|
279
|
+
domain: ErrorDomain.STORAGE,
|
|
280
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
281
|
+
details: {
|
|
282
|
+
indexName,
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
error,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
199
288
|
}
|
|
200
|
-
|
|
201
289
|
async query({
|
|
202
290
|
indexName,
|
|
203
291
|
queryVector,
|
|
@@ -206,51 +294,63 @@ export class MongoDBVector extends MastraVector {
|
|
|
206
294
|
includeVector = false,
|
|
207
295
|
documentFilter,
|
|
208
296
|
}: MongoDBQueryVectorParams): Promise<QueryResult[]> {
|
|
209
|
-
|
|
210
|
-
|
|
297
|
+
try {
|
|
298
|
+
const collection = await this.getCollection(indexName, true);
|
|
299
|
+
const indexNameInternal = `${indexName}_vector_index`;
|
|
300
|
+
|
|
301
|
+
// Transform the filters using MongoDBFilterTranslator
|
|
302
|
+
const mongoFilter = this.transformFilter(filter);
|
|
303
|
+
const documentMongoFilter = documentFilter ? { [this.documentFieldName]: documentFilter } : {};
|
|
304
|
+
|
|
305
|
+
// Combine the filters
|
|
306
|
+
let combinedFilter: any = {};
|
|
307
|
+
if (Object.keys(mongoFilter).length > 0 && Object.keys(documentMongoFilter).length > 0) {
|
|
308
|
+
combinedFilter = { $and: [mongoFilter, documentMongoFilter] };
|
|
309
|
+
} else if (Object.keys(mongoFilter).length > 0) {
|
|
310
|
+
combinedFilter = mongoFilter;
|
|
311
|
+
} else if (Object.keys(documentMongoFilter).length > 0) {
|
|
312
|
+
combinedFilter = documentMongoFilter;
|
|
313
|
+
}
|
|
211
314
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
combinedFilter = { $and: [mongoFilter, documentMongoFilter] };
|
|
220
|
-
} else if (Object.keys(mongoFilter).length > 0) {
|
|
221
|
-
combinedFilter = mongoFilter;
|
|
222
|
-
} else if (Object.keys(documentMongoFilter).length > 0) {
|
|
223
|
-
combinedFilter = documentMongoFilter;
|
|
224
|
-
}
|
|
315
|
+
const vectorSearch: Document = {
|
|
316
|
+
index: indexNameInternal,
|
|
317
|
+
queryVector: queryVector,
|
|
318
|
+
path: this.embeddingFieldName,
|
|
319
|
+
numCandidates: 100,
|
|
320
|
+
limit: topK,
|
|
321
|
+
};
|
|
225
322
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
323
|
+
if (Object.keys(combinedFilter).length > 0) {
|
|
324
|
+
// pre-filter for candidate document IDs
|
|
325
|
+
const candidateIds = await collection
|
|
326
|
+
.aggregate([{ $match: combinedFilter }, { $project: { _id: 1 } }])
|
|
327
|
+
.map(doc => doc._id)
|
|
328
|
+
.toArray();
|
|
329
|
+
|
|
330
|
+
if (candidateIds.length > 0) {
|
|
331
|
+
vectorSearch.filter = { _id: { $in: candidateIds } };
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Build the aggregation pipeline
|
|
336
|
+
const pipeline = [
|
|
337
|
+
{
|
|
338
|
+
$vectorSearch: vectorSearch,
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
$set: { score: { $meta: 'vectorSearchScore' } },
|
|
235
342
|
},
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
_id: 1,
|
|
245
|
-
score: 1,
|
|
246
|
-
metadata: `$${this.metadataFieldName}`,
|
|
247
|
-
document: `$${this.documentFieldName}`,
|
|
248
|
-
...(includeVector && { vector: `$${this.embeddingFieldName}` }),
|
|
343
|
+
{
|
|
344
|
+
$project: {
|
|
345
|
+
_id: 1,
|
|
346
|
+
score: 1,
|
|
347
|
+
metadata: `$${this.metadataFieldName}`,
|
|
348
|
+
document: `$${this.documentFieldName}`,
|
|
349
|
+
...(includeVector && { vector: `$${this.embeddingFieldName}` }),
|
|
350
|
+
},
|
|
249
351
|
},
|
|
250
|
-
|
|
251
|
-
];
|
|
352
|
+
];
|
|
252
353
|
|
|
253
|
-
try {
|
|
254
354
|
const results = await collection.aggregate(pipeline).toArray();
|
|
255
355
|
|
|
256
356
|
return results.map((result: any) => ({
|
|
@@ -261,14 +361,34 @@ export class MongoDBVector extends MastraVector {
|
|
|
261
361
|
document: result.document,
|
|
262
362
|
}));
|
|
263
363
|
} catch (error) {
|
|
264
|
-
|
|
265
|
-
|
|
364
|
+
throw new MastraError(
|
|
365
|
+
{
|
|
366
|
+
id: 'STORAGE_MONGODB_VECTOR_QUERY_FAILED',
|
|
367
|
+
domain: ErrorDomain.STORAGE,
|
|
368
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
369
|
+
details: {
|
|
370
|
+
indexName,
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
error,
|
|
374
|
+
);
|
|
266
375
|
}
|
|
267
376
|
}
|
|
268
377
|
|
|
269
378
|
async listIndexes(): Promise<string[]> {
|
|
270
|
-
|
|
271
|
-
|
|
379
|
+
try {
|
|
380
|
+
const collections = await this.db.listCollections().toArray();
|
|
381
|
+
return collections.map(col => col.name);
|
|
382
|
+
} catch (error) {
|
|
383
|
+
throw new MastraError(
|
|
384
|
+
{
|
|
385
|
+
id: 'STORAGE_MONGODB_VECTOR_LIST_INDEXES_FAILED',
|
|
386
|
+
domain: ErrorDomain.STORAGE,
|
|
387
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
388
|
+
},
|
|
389
|
+
error,
|
|
390
|
+
);
|
|
391
|
+
}
|
|
272
392
|
}
|
|
273
393
|
|
|
274
394
|
/**
|
|
@@ -278,31 +398,59 @@ export class MongoDBVector extends MastraVector {
|
|
|
278
398
|
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
279
399
|
*/
|
|
280
400
|
async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
|
|
281
|
-
|
|
401
|
+
try {
|
|
402
|
+
const collection = await this.getCollection(indexName, true);
|
|
282
403
|
|
|
283
|
-
|
|
284
|
-
|
|
404
|
+
// Get the count of documents, excluding the metadata document
|
|
405
|
+
const count = await collection.countDocuments({ _id: { $ne: '__index_metadata__' } });
|
|
285
406
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
407
|
+
// Retrieve the dimension and metric from the metadata document
|
|
408
|
+
const metadataDoc = await collection.findOne({ _id: '__index_metadata__' });
|
|
409
|
+
const dimension = metadataDoc?.dimension || 0;
|
|
410
|
+
const metric = metadataDoc?.metric || 'cosine';
|
|
290
411
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
412
|
+
return {
|
|
413
|
+
dimension,
|
|
414
|
+
count,
|
|
415
|
+
metric: metric as 'cosine' | 'euclidean' | 'dotproduct',
|
|
416
|
+
};
|
|
417
|
+
} catch (error) {
|
|
418
|
+
throw new MastraError(
|
|
419
|
+
{
|
|
420
|
+
id: 'STORAGE_MONGODB_VECTOR_DESCRIBE_INDEX_FAILED',
|
|
421
|
+
domain: ErrorDomain.STORAGE,
|
|
422
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
423
|
+
details: {
|
|
424
|
+
indexName,
|
|
425
|
+
},
|
|
426
|
+
},
|
|
427
|
+
error,
|
|
428
|
+
);
|
|
429
|
+
}
|
|
296
430
|
}
|
|
297
431
|
|
|
298
432
|
async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
|
|
299
433
|
const collection = await this.getCollection(indexName, false); // Do not throw error if collection doesn't exist
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
434
|
+
try {
|
|
435
|
+
if (collection) {
|
|
436
|
+
await collection.drop();
|
|
437
|
+
this.collections.delete(indexName);
|
|
438
|
+
} else {
|
|
439
|
+
// Optionally, you can log or handle the case where the collection doesn't exist
|
|
440
|
+
throw new Error(`Index (Collection) "${indexName}" does not exist`);
|
|
441
|
+
}
|
|
442
|
+
} catch (error) {
|
|
443
|
+
throw new MastraError(
|
|
444
|
+
{
|
|
445
|
+
id: 'STORAGE_MONGODB_VECTOR_DELETE_INDEX_FAILED',
|
|
446
|
+
domain: ErrorDomain.STORAGE,
|
|
447
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
448
|
+
details: {
|
|
449
|
+
indexName,
|
|
450
|
+
},
|
|
451
|
+
},
|
|
452
|
+
error,
|
|
453
|
+
);
|
|
306
454
|
}
|
|
307
455
|
}
|
|
308
456
|
|
|
@@ -347,7 +495,18 @@ export class MongoDBVector extends MastraVector {
|
|
|
347
495
|
|
|
348
496
|
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
349
497
|
} catch (error: any) {
|
|
350
|
-
throw new
|
|
498
|
+
throw new MastraError(
|
|
499
|
+
{
|
|
500
|
+
id: 'STORAGE_MONGODB_VECTOR_UPDATE_VECTOR_FAILED',
|
|
501
|
+
domain: ErrorDomain.STORAGE,
|
|
502
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
503
|
+
details: {
|
|
504
|
+
indexName,
|
|
505
|
+
id,
|
|
506
|
+
},
|
|
507
|
+
},
|
|
508
|
+
error,
|
|
509
|
+
);
|
|
351
510
|
}
|
|
352
511
|
}
|
|
353
512
|
|
|
@@ -363,7 +522,18 @@ export class MongoDBVector extends MastraVector {
|
|
|
363
522
|
const collection = await this.getCollection(indexName, true);
|
|
364
523
|
await collection.deleteOne({ _id: id });
|
|
365
524
|
} catch (error: any) {
|
|
366
|
-
throw new
|
|
525
|
+
throw new MastraError(
|
|
526
|
+
{
|
|
527
|
+
id: 'STORAGE_MONGODB_VECTOR_DELETE_VECTOR_FAILED',
|
|
528
|
+
domain: ErrorDomain.STORAGE,
|
|
529
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
530
|
+
details: {
|
|
531
|
+
indexName,
|
|
532
|
+
id,
|
|
533
|
+
},
|
|
534
|
+
},
|
|
535
|
+
error,
|
|
536
|
+
);
|
|
367
537
|
}
|
|
368
538
|
}
|
|
369
539
|
|
|
@@ -413,7 +583,7 @@ export class MongoDBVector extends MastraVector {
|
|
|
413
583
|
await collection.updateOne({ _id: '__index_metadata__' }, { $set: { dimension } }, { upsert: true });
|
|
414
584
|
}
|
|
415
585
|
|
|
416
|
-
private transformFilter(filter?:
|
|
586
|
+
private transformFilter(filter?: MongoDBVectorFilter) {
|
|
417
587
|
const translator = new MongoDBFilterTranslator();
|
|
418
588
|
if (!filter) return {};
|
|
419
589
|
return translator.translate(filter);
|
package/tsconfig.json
CHANGED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { promisify } from 'util';
|
|
3
|
+
import { defineConfig } from 'tsup';
|
|
4
|
+
|
|
5
|
+
const exec = promisify(spawn);
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
entry: ['src/index.ts'],
|
|
9
|
+
format: ['esm', 'cjs'],
|
|
10
|
+
clean: true,
|
|
11
|
+
dts: false,
|
|
12
|
+
splitting: true,
|
|
13
|
+
treeshake: {
|
|
14
|
+
preset: 'smallest',
|
|
15
|
+
},
|
|
16
|
+
sourcemap: true,
|
|
17
|
+
onSuccess: async () => {
|
|
18
|
+
await exec('pnpm', ['tsc', '-p', 'tsconfig.build.json'], {
|
|
19
|
+
stdio: 'inherit',
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
});
|