@mastra/mongodb 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 +1501 -2
- package/LICENSE.md +11 -42
- package/README.md +56 -0
- package/dist/index.cjs +2459 -141
- 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 +2455 -138
- package/dist/index.js.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/memory/index.d.ts +60 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/observability/index.d.ts +43 -0
- package/dist/storage/domains/observability/index.d.ts.map +1 -0
- package/dist/storage/domains/operations/index.d.ts +50 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +50 -0
- package/dist/storage/domains/scores/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 +45 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +198 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/types.d.ts +13 -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 +94 -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/package.json +39 -16
- package/dist/_tsup-dts-rollup.d.cts +0 -96
- package/dist/_tsup-dts-rollup.d.ts +0 -96
- package/dist/index.d.cts +0 -7
- package/docker-compose.yml +0 -8
- package/eslint.config.js +0 -6
- package/src/index.ts +0 -2
- package/src/vector/filter.test.ts +0 -410
- package/src/vector/filter.ts +0 -122
- package/src/vector/index.test.ts +0 -459
- package/src/vector/index.ts +0 -380
- package/src/vector/prompt.ts +0 -97
- package/tsconfig.json +0 -5
- package/vitest.config.ts +0 -11
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var error = require('@mastra/core/error');
|
|
3
4
|
var vector = require('@mastra/core/vector');
|
|
4
5
|
var mongodb = require('mongodb');
|
|
5
6
|
var uuid = require('uuid');
|
|
6
7
|
var filter = require('@mastra/core/vector/filter');
|
|
8
|
+
var storage = require('@mastra/core/storage');
|
|
9
|
+
var agent = require('@mastra/core/agent');
|
|
10
|
+
var evals = require('@mastra/core/evals');
|
|
7
11
|
|
|
8
12
|
// src/vector/index.ts
|
|
13
|
+
|
|
14
|
+
// package.json
|
|
15
|
+
var package_default = {
|
|
16
|
+
version: "1.0.0-beta.0"};
|
|
9
17
|
var MongoDBFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
10
18
|
getSupportedOperators() {
|
|
11
19
|
return {
|
|
@@ -108,37 +116,83 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
108
116
|
euclidean: "euclidean",
|
|
109
117
|
dotproduct: "dotProduct"
|
|
110
118
|
};
|
|
111
|
-
constructor({ uri, dbName, options }) {
|
|
112
|
-
super();
|
|
113
|
-
|
|
119
|
+
constructor({ id, uri, dbName, options }) {
|
|
120
|
+
super({ id });
|
|
121
|
+
const client = new mongodb.MongoClient(uri, {
|
|
122
|
+
...options,
|
|
123
|
+
driverInfo: {
|
|
124
|
+
name: "mastra-vector",
|
|
125
|
+
version: package_default.version
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
this.client = client;
|
|
114
129
|
this.db = this.client.db(dbName);
|
|
115
130
|
this.collections = /* @__PURE__ */ new Map();
|
|
116
131
|
}
|
|
117
132
|
// Public methods
|
|
118
133
|
async connect() {
|
|
119
|
-
|
|
134
|
+
try {
|
|
135
|
+
await this.client.connect();
|
|
136
|
+
} catch (error$1) {
|
|
137
|
+
throw new error.MastraError(
|
|
138
|
+
{
|
|
139
|
+
id: "STORAGE_MONGODB_VECTOR_CONNECT_FAILED",
|
|
140
|
+
domain: error.ErrorDomain.STORAGE,
|
|
141
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
142
|
+
},
|
|
143
|
+
error$1
|
|
144
|
+
);
|
|
145
|
+
}
|
|
120
146
|
}
|
|
121
147
|
async disconnect() {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
148
|
+
try {
|
|
149
|
+
await this.client.close();
|
|
150
|
+
} catch (error$1) {
|
|
151
|
+
throw new error.MastraError(
|
|
152
|
+
{
|
|
153
|
+
id: "STORAGE_MONGODB_VECTOR_DISCONNECT_FAILED",
|
|
154
|
+
domain: error.ErrorDomain.STORAGE,
|
|
155
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
156
|
+
},
|
|
157
|
+
error$1
|
|
158
|
+
);
|
|
132
159
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
160
|
+
}
|
|
161
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
162
|
+
let mongoMetric;
|
|
163
|
+
try {
|
|
164
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
165
|
+
throw new Error("Dimension must be a positive integer");
|
|
166
|
+
}
|
|
167
|
+
mongoMetric = this.mongoMetricMap[metric];
|
|
168
|
+
if (!mongoMetric) {
|
|
169
|
+
throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
|
|
170
|
+
}
|
|
171
|
+
} catch (error$1) {
|
|
172
|
+
throw new error.MastraError(
|
|
173
|
+
{
|
|
174
|
+
id: "STORAGE_MONGODB_VECTOR_CREATE_INDEX_INVALID_ARGS",
|
|
175
|
+
domain: error.ErrorDomain.STORAGE,
|
|
176
|
+
category: error.ErrorCategory.USER,
|
|
177
|
+
details: {
|
|
178
|
+
indexName,
|
|
179
|
+
dimension,
|
|
180
|
+
metric
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
error$1
|
|
184
|
+
);
|
|
136
185
|
}
|
|
137
|
-
|
|
138
|
-
const indexNameInternal = `${indexName}_vector_index`;
|
|
139
|
-
const embeddingField = this.embeddingFieldName;
|
|
140
|
-
const numDimensions = dimension;
|
|
186
|
+
let collection;
|
|
141
187
|
try {
|
|
188
|
+
const collectionExists = await this.db.listCollections({ name: indexName }).hasNext();
|
|
189
|
+
if (!collectionExists) {
|
|
190
|
+
await this.db.createCollection(indexName);
|
|
191
|
+
}
|
|
192
|
+
collection = await this.getCollection(indexName);
|
|
193
|
+
const indexNameInternal = `${indexName}_vector_index`;
|
|
194
|
+
const embeddingField = this.embeddingFieldName;
|
|
195
|
+
const numDimensions = dimension;
|
|
142
196
|
await collection.createSearchIndex({
|
|
143
197
|
definition: {
|
|
144
198
|
fields: [
|
|
@@ -147,20 +201,66 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
147
201
|
path: embeddingField,
|
|
148
202
|
numDimensions,
|
|
149
203
|
similarity: mongoMetric
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
type: "filter",
|
|
207
|
+
path: "_id"
|
|
150
208
|
}
|
|
151
209
|
]
|
|
152
210
|
},
|
|
153
211
|
name: indexNameInternal,
|
|
154
212
|
type: "vectorSearch"
|
|
155
213
|
});
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
214
|
+
await collection.createSearchIndex({
|
|
215
|
+
definition: {
|
|
216
|
+
mappings: {
|
|
217
|
+
dynamic: true
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
name: `${indexName}_search_index`,
|
|
221
|
+
type: "search"
|
|
222
|
+
});
|
|
223
|
+
} catch (error$1) {
|
|
224
|
+
if (error$1.codeName !== "IndexAlreadyExists") {
|
|
225
|
+
throw new error.MastraError(
|
|
226
|
+
{
|
|
227
|
+
id: "STORAGE_MONGODB_VECTOR_CREATE_INDEX_FAILED",
|
|
228
|
+
domain: error.ErrorDomain.STORAGE,
|
|
229
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
230
|
+
},
|
|
231
|
+
error$1
|
|
232
|
+
);
|
|
159
233
|
}
|
|
160
234
|
}
|
|
161
|
-
|
|
235
|
+
try {
|
|
236
|
+
await collection?.updateOne({ _id: "__index_metadata__" }, { $set: { dimension, metric } }, { upsert: true });
|
|
237
|
+
} catch (error$1) {
|
|
238
|
+
throw new error.MastraError(
|
|
239
|
+
{
|
|
240
|
+
id: "STORAGE_MONGODB_VECTOR_CREATE_INDEX_FAILED_STORE_METADATA",
|
|
241
|
+
domain: error.ErrorDomain.STORAGE,
|
|
242
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
243
|
+
details: {
|
|
244
|
+
indexName
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
error$1
|
|
248
|
+
);
|
|
249
|
+
}
|
|
162
250
|
}
|
|
163
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Waits for the index to be ready.
|
|
253
|
+
*
|
|
254
|
+
* @param {string} indexName - The name of the index to wait for
|
|
255
|
+
* @param {number} timeoutMs - The maximum time in milliseconds to wait for the index to be ready (default: 60000)
|
|
256
|
+
* @param {number} checkIntervalMs - The interval in milliseconds at which to check if the index is ready (default: 2000)
|
|
257
|
+
* @returns A promise that resolves when the index is ready
|
|
258
|
+
*/
|
|
259
|
+
async waitForIndexReady({
|
|
260
|
+
indexName,
|
|
261
|
+
timeoutMs = 6e4,
|
|
262
|
+
checkIntervalMs = 2e3
|
|
263
|
+
}) {
|
|
164
264
|
const collection = await this.getCollection(indexName, true);
|
|
165
265
|
const indexNameInternal = `${indexName}_vector_index`;
|
|
166
266
|
const startTime = Date.now();
|
|
@@ -175,83 +275,110 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
175
275
|
}
|
|
176
276
|
throw new Error(`Index "${indexNameInternal}" did not become ready within timeout`);
|
|
177
277
|
}
|
|
178
|
-
async upsert(
|
|
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
|
-
return {
|
|
204
|
-
updateOne: {
|
|
205
|
-
filter: { _id: id },
|
|
206
|
-
// '_id' is a string as per MongoDBDocument interface
|
|
207
|
-
update: { $set: updateDoc },
|
|
208
|
-
upsert: true
|
|
278
|
+
async upsert({ indexName, vectors, metadata, ids, documents }) {
|
|
279
|
+
try {
|
|
280
|
+
const collection = await this.getCollection(indexName);
|
|
281
|
+
this.collectionForValidation = collection;
|
|
282
|
+
const stats = await this.describeIndex({ indexName });
|
|
283
|
+
await this.validateVectorDimensions(vectors, stats.dimension);
|
|
284
|
+
const generatedIds = ids || vectors.map(() => uuid.v4());
|
|
285
|
+
const operations = vectors.map((vector, idx) => {
|
|
286
|
+
const id = generatedIds[idx];
|
|
287
|
+
const meta = metadata?.[idx] || {};
|
|
288
|
+
const doc = documents?.[idx];
|
|
289
|
+
const normalizedMeta = Object.keys(meta).reduce(
|
|
290
|
+
(acc, key) => {
|
|
291
|
+
acc[key] = meta[key] instanceof Date ? meta[key].toISOString() : meta[key];
|
|
292
|
+
return acc;
|
|
293
|
+
},
|
|
294
|
+
{}
|
|
295
|
+
);
|
|
296
|
+
const updateDoc = {
|
|
297
|
+
[this.embeddingFieldName]: vector,
|
|
298
|
+
[this.metadataFieldName]: normalizedMeta
|
|
299
|
+
};
|
|
300
|
+
if (doc !== void 0) {
|
|
301
|
+
updateDoc[this.documentFieldName] = doc;
|
|
209
302
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
303
|
+
return {
|
|
304
|
+
updateOne: {
|
|
305
|
+
filter: { _id: id },
|
|
306
|
+
// '_id' is a string as per MongoDBDocument interface
|
|
307
|
+
update: { $set: updateDoc },
|
|
308
|
+
upsert: true
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
});
|
|
312
|
+
await collection.bulkWrite(operations);
|
|
313
|
+
return generatedIds;
|
|
314
|
+
} catch (error$1) {
|
|
315
|
+
throw new error.MastraError(
|
|
316
|
+
{
|
|
317
|
+
id: "STORAGE_MONGODB_VECTOR_UPSERT_FAILED",
|
|
318
|
+
domain: error.ErrorDomain.STORAGE,
|
|
319
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
320
|
+
details: {
|
|
321
|
+
indexName
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
error$1
|
|
325
|
+
);
|
|
326
|
+
}
|
|
214
327
|
}
|
|
215
|
-
async query(
|
|
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
|
-
$project: {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
...includeVector && { vector: `$${this.embeddingFieldName}` }
|
|
328
|
+
async query({
|
|
329
|
+
indexName,
|
|
330
|
+
queryVector,
|
|
331
|
+
topK = 10,
|
|
332
|
+
filter,
|
|
333
|
+
includeVector = false,
|
|
334
|
+
documentFilter
|
|
335
|
+
}) {
|
|
336
|
+
try {
|
|
337
|
+
const collection = await this.getCollection(indexName, true);
|
|
338
|
+
const indexNameInternal = `${indexName}_vector_index`;
|
|
339
|
+
const mongoFilter = this.transformFilter(filter);
|
|
340
|
+
const documentMongoFilter = documentFilter ? { [this.documentFieldName]: documentFilter } : {};
|
|
341
|
+
const transformedMongoFilter = this.transformMetadataFilter(mongoFilter);
|
|
342
|
+
let combinedFilter = {};
|
|
343
|
+
if (Object.keys(transformedMongoFilter).length > 0 && Object.keys(documentMongoFilter).length > 0) {
|
|
344
|
+
combinedFilter = { $and: [transformedMongoFilter, documentMongoFilter] };
|
|
345
|
+
} else if (Object.keys(transformedMongoFilter).length > 0) {
|
|
346
|
+
combinedFilter = transformedMongoFilter;
|
|
347
|
+
} else if (Object.keys(documentMongoFilter).length > 0) {
|
|
348
|
+
combinedFilter = documentMongoFilter;
|
|
349
|
+
}
|
|
350
|
+
const vectorSearch = {
|
|
351
|
+
index: indexNameInternal,
|
|
352
|
+
queryVector,
|
|
353
|
+
path: this.embeddingFieldName,
|
|
354
|
+
numCandidates: 100,
|
|
355
|
+
limit: topK
|
|
356
|
+
};
|
|
357
|
+
if (Object.keys(combinedFilter).length > 0) {
|
|
358
|
+
const candidateIds = await collection.aggregate([{ $match: combinedFilter }, { $project: { _id: 1 } }]).map((doc) => doc._id).toArray();
|
|
359
|
+
if (candidateIds.length > 0) {
|
|
360
|
+
vectorSearch.filter = { _id: { $in: candidateIds } };
|
|
361
|
+
} else {
|
|
362
|
+
return [];
|
|
251
363
|
}
|
|
252
364
|
}
|
|
253
|
-
|
|
254
|
-
|
|
365
|
+
const pipeline = [
|
|
366
|
+
{
|
|
367
|
+
$vectorSearch: vectorSearch
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
$set: { score: { $meta: "vectorSearchScore" } }
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
$project: {
|
|
374
|
+
_id: 1,
|
|
375
|
+
score: 1,
|
|
376
|
+
metadata: `$${this.metadataFieldName}`,
|
|
377
|
+
document: `$${this.documentFieldName}`,
|
|
378
|
+
...includeVector && { vector: `$${this.embeddingFieldName}` }
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
];
|
|
255
382
|
const results = await collection.aggregate(pipeline).toArray();
|
|
256
383
|
return results.map((result) => ({
|
|
257
384
|
id: result._id,
|
|
@@ -260,63 +387,162 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
260
387
|
vector: includeVector ? result.vector : void 0,
|
|
261
388
|
document: result.document
|
|
262
389
|
}));
|
|
263
|
-
} catch (error) {
|
|
264
|
-
|
|
265
|
-
|
|
390
|
+
} catch (error$1) {
|
|
391
|
+
throw new error.MastraError(
|
|
392
|
+
{
|
|
393
|
+
id: "STORAGE_MONGODB_VECTOR_QUERY_FAILED",
|
|
394
|
+
domain: error.ErrorDomain.STORAGE,
|
|
395
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
396
|
+
details: {
|
|
397
|
+
indexName
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
error$1
|
|
401
|
+
);
|
|
266
402
|
}
|
|
267
403
|
}
|
|
268
404
|
async listIndexes() {
|
|
269
|
-
|
|
270
|
-
|
|
405
|
+
try {
|
|
406
|
+
const collections = await this.db.listCollections().toArray();
|
|
407
|
+
return collections.map((col) => col.name);
|
|
408
|
+
} catch (error$1) {
|
|
409
|
+
throw new error.MastraError(
|
|
410
|
+
{
|
|
411
|
+
id: "STORAGE_MONGODB_VECTOR_LIST_INDEXES_FAILED",
|
|
412
|
+
domain: error.ErrorDomain.STORAGE,
|
|
413
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
414
|
+
},
|
|
415
|
+
error$1
|
|
416
|
+
);
|
|
417
|
+
}
|
|
271
418
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Retrieves statistics about a vector index.
|
|
421
|
+
*
|
|
422
|
+
* @param {string} indexName - The name of the index to describe
|
|
423
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
424
|
+
*/
|
|
425
|
+
async describeIndex({ indexName }) {
|
|
426
|
+
try {
|
|
427
|
+
const collection = await this.getCollection(indexName, true);
|
|
428
|
+
const count = await collection.countDocuments({ _id: { $ne: "__index_metadata__" } });
|
|
429
|
+
const metadataDoc = await collection.findOne({ _id: "__index_metadata__" });
|
|
430
|
+
const dimension = metadataDoc?.dimension || 0;
|
|
431
|
+
const metric = metadataDoc?.metric || "cosine";
|
|
432
|
+
return {
|
|
433
|
+
dimension,
|
|
434
|
+
count,
|
|
435
|
+
metric
|
|
436
|
+
};
|
|
437
|
+
} catch (error$1) {
|
|
438
|
+
throw new error.MastraError(
|
|
439
|
+
{
|
|
440
|
+
id: "STORAGE_MONGODB_VECTOR_DESCRIBE_INDEX_FAILED",
|
|
441
|
+
domain: error.ErrorDomain.STORAGE,
|
|
442
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
443
|
+
details: {
|
|
444
|
+
indexName
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
error$1
|
|
448
|
+
);
|
|
449
|
+
}
|
|
283
450
|
}
|
|
284
|
-
async deleteIndex(indexName) {
|
|
451
|
+
async deleteIndex({ indexName }) {
|
|
285
452
|
const collection = await this.getCollection(indexName, false);
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
453
|
+
try {
|
|
454
|
+
if (collection) {
|
|
455
|
+
await collection.drop();
|
|
456
|
+
this.collections.delete(indexName);
|
|
457
|
+
} else {
|
|
458
|
+
throw new Error(`Index (Collection) "${indexName}" does not exist`);
|
|
459
|
+
}
|
|
460
|
+
} catch (error$1) {
|
|
461
|
+
throw new error.MastraError(
|
|
462
|
+
{
|
|
463
|
+
id: "STORAGE_MONGODB_VECTOR_DELETE_INDEX_FAILED",
|
|
464
|
+
domain: error.ErrorDomain.STORAGE,
|
|
465
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
466
|
+
details: {
|
|
467
|
+
indexName
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
error$1
|
|
471
|
+
);
|
|
291
472
|
}
|
|
292
473
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
474
|
+
/**
|
|
475
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
476
|
+
* @param indexName - The name of the index containing the vector.
|
|
477
|
+
* @param id - The ID of the vector to update.
|
|
478
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
479
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
480
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
481
|
+
* @returns A promise that resolves when the update is complete.
|
|
482
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
483
|
+
*/
|
|
484
|
+
async updateVector({ indexName, id, update }) {
|
|
485
|
+
try {
|
|
486
|
+
if (!update.vector && !update.metadata) {
|
|
487
|
+
throw new Error("No updates provided");
|
|
488
|
+
}
|
|
489
|
+
const collection = await this.getCollection(indexName, true);
|
|
490
|
+
const updateDoc = {};
|
|
491
|
+
if (update.vector) {
|
|
492
|
+
const stats = await this.describeIndex({ indexName });
|
|
493
|
+
await this.validateVectorDimensions([update.vector], stats.dimension);
|
|
494
|
+
updateDoc[this.embeddingFieldName] = update.vector;
|
|
495
|
+
}
|
|
496
|
+
if (update.metadata) {
|
|
497
|
+
const normalizedMeta = Object.keys(update.metadata).reduce(
|
|
498
|
+
(acc, key) => {
|
|
499
|
+
acc[key] = update.metadata[key] instanceof Date ? update.metadata[key].toISOString() : update.metadata[key];
|
|
500
|
+
return acc;
|
|
501
|
+
},
|
|
502
|
+
{}
|
|
503
|
+
);
|
|
504
|
+
updateDoc[this.metadataFieldName] = normalizedMeta;
|
|
505
|
+
}
|
|
506
|
+
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
507
|
+
} catch (error$1) {
|
|
508
|
+
throw new error.MastraError(
|
|
509
|
+
{
|
|
510
|
+
id: "STORAGE_MONGODB_VECTOR_UPDATE_VECTOR_FAILED",
|
|
511
|
+
domain: error.ErrorDomain.STORAGE,
|
|
512
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
513
|
+
details: {
|
|
514
|
+
indexName,
|
|
515
|
+
id
|
|
516
|
+
}
|
|
307
517
|
},
|
|
308
|
-
|
|
518
|
+
error$1
|
|
309
519
|
);
|
|
310
|
-
updateDoc[this.metadataFieldName] = normalizedMeta;
|
|
311
520
|
}
|
|
312
|
-
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
313
521
|
}
|
|
314
|
-
|
|
522
|
+
/**
|
|
523
|
+
* Deletes a vector by its ID.
|
|
524
|
+
* @param indexName - The name of the index containing the vector.
|
|
525
|
+
* @param id - The ID of the vector to delete.
|
|
526
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
527
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
528
|
+
*/
|
|
529
|
+
async deleteVector({ indexName, id }) {
|
|
315
530
|
try {
|
|
316
531
|
const collection = await this.getCollection(indexName, true);
|
|
317
532
|
await collection.deleteOne({ _id: id });
|
|
318
|
-
} catch (error) {
|
|
319
|
-
throw new
|
|
533
|
+
} catch (error$1) {
|
|
534
|
+
throw new error.MastraError(
|
|
535
|
+
{
|
|
536
|
+
id: "STORAGE_MONGODB_VECTOR_DELETE_VECTOR_FAILED",
|
|
537
|
+
domain: error.ErrorDomain.STORAGE,
|
|
538
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
539
|
+
details: {
|
|
540
|
+
indexName,
|
|
541
|
+
id
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
error$1
|
|
545
|
+
);
|
|
320
546
|
}
|
|
321
547
|
}
|
|
322
548
|
// Private methods
|
|
@@ -356,6 +582,2095 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
356
582
|
if (!filter) return {};
|
|
357
583
|
return translator.translate(filter);
|
|
358
584
|
}
|
|
585
|
+
/**
|
|
586
|
+
* Transform metadata field filters to use MongoDB dot notation.
|
|
587
|
+
* Fields that are stored in the metadata subdocument need to be prefixed with 'metadata.'
|
|
588
|
+
* This handles filters from the Memory system which expects direct field access.
|
|
589
|
+
*
|
|
590
|
+
* @param filter - The filter object to transform
|
|
591
|
+
* @returns Transformed filter with metadata fields properly prefixed
|
|
592
|
+
*/
|
|
593
|
+
transformMetadataFilter(filter) {
|
|
594
|
+
if (!filter || typeof filter !== "object") return filter;
|
|
595
|
+
const transformed = {};
|
|
596
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
597
|
+
if (key.startsWith("$")) {
|
|
598
|
+
if (Array.isArray(value)) {
|
|
599
|
+
transformed[key] = value.map((item) => this.transformMetadataFilter(item));
|
|
600
|
+
} else {
|
|
601
|
+
transformed[key] = this.transformMetadataFilter(value);
|
|
602
|
+
}
|
|
603
|
+
} else if (key.startsWith("metadata.")) {
|
|
604
|
+
transformed[key] = value;
|
|
605
|
+
} else if (this.isMetadataField(key)) {
|
|
606
|
+
transformed[`metadata.${key}`] = value;
|
|
607
|
+
} else {
|
|
608
|
+
transformed[key] = value;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return transformed;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Determine if a field should be treated as a metadata field.
|
|
615
|
+
* Common metadata fields include thread_id, resource_id, message_id, and any field
|
|
616
|
+
* that doesn't start with underscore (MongoDB system fields).
|
|
617
|
+
*/
|
|
618
|
+
isMetadataField(key) {
|
|
619
|
+
if (key.startsWith("_")) return false;
|
|
620
|
+
const documentFields = ["_id", this.embeddingFieldName, this.documentFieldName];
|
|
621
|
+
if (documentFields.includes(key)) return false;
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
var MongoDBConnector = class _MongoDBConnector {
|
|
626
|
+
#client;
|
|
627
|
+
#dbName;
|
|
628
|
+
#handler;
|
|
629
|
+
#isConnected;
|
|
630
|
+
#db;
|
|
631
|
+
constructor(options) {
|
|
632
|
+
this.#client = options.client;
|
|
633
|
+
this.#dbName = options.dbName;
|
|
634
|
+
this.#handler = options.handler;
|
|
635
|
+
this.#isConnected = false;
|
|
636
|
+
}
|
|
637
|
+
static fromDatabaseConfig(config) {
|
|
638
|
+
if (!config.url?.trim().length) {
|
|
639
|
+
throw new Error(
|
|
640
|
+
"MongoDBStore: url must be provided and cannot be empty. Passing an empty string may cause fallback to local MongoDB defaults."
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
if (!config.dbName?.trim().length) {
|
|
644
|
+
throw new Error(
|
|
645
|
+
"MongoDBStore: dbName must be provided and cannot be empty. Passing an empty string may cause fallback to local MongoDB defaults."
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
const client = new mongodb.MongoClient(config.url, {
|
|
649
|
+
...config.options,
|
|
650
|
+
driverInfo: {
|
|
651
|
+
name: "mastra-storage",
|
|
652
|
+
version: package_default.version
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
return new _MongoDBConnector({
|
|
656
|
+
client,
|
|
657
|
+
dbName: config.dbName,
|
|
658
|
+
handler: void 0
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
static fromConnectionHandler(handler) {
|
|
662
|
+
return new _MongoDBConnector({
|
|
663
|
+
client: void 0,
|
|
664
|
+
dbName: void 0,
|
|
665
|
+
handler
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
async getConnection() {
|
|
669
|
+
if (this.#client) {
|
|
670
|
+
if (this.#isConnected && this.#db) {
|
|
671
|
+
return this.#db;
|
|
672
|
+
}
|
|
673
|
+
await this.#client.connect();
|
|
674
|
+
this.#db = this.#client.db(this.#dbName);
|
|
675
|
+
this.#isConnected = true;
|
|
676
|
+
return this.#db;
|
|
677
|
+
}
|
|
678
|
+
throw new Error("MongoDBStore: client cannot be empty. Check your MongoDBConnector configuration.");
|
|
679
|
+
}
|
|
680
|
+
async getCollection(collectionName) {
|
|
681
|
+
if (this.#handler) {
|
|
682
|
+
return this.#handler.getCollection(collectionName);
|
|
683
|
+
}
|
|
684
|
+
const db = await this.getConnection();
|
|
685
|
+
return db.collection(collectionName);
|
|
686
|
+
}
|
|
687
|
+
async close() {
|
|
688
|
+
if (this.#client) {
|
|
689
|
+
await this.#client.close();
|
|
690
|
+
this.#isConnected = false;
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if (this.#handler) {
|
|
694
|
+
await this.#handler.close();
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
// src/storage/domains/utils.ts
|
|
700
|
+
function formatDateForMongoDB(date) {
|
|
701
|
+
return typeof date === "string" ? new Date(date) : date;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// src/storage/domains/memory/index.ts
|
|
705
|
+
var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
706
|
+
operations;
|
|
707
|
+
constructor({ operations }) {
|
|
708
|
+
super();
|
|
709
|
+
this.operations = operations;
|
|
710
|
+
}
|
|
711
|
+
parseRow(row) {
|
|
712
|
+
let content = row.content;
|
|
713
|
+
if (typeof content === "string") {
|
|
714
|
+
try {
|
|
715
|
+
content = JSON.parse(content);
|
|
716
|
+
} catch {
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
const result = {
|
|
720
|
+
id: row.id,
|
|
721
|
+
content,
|
|
722
|
+
role: row.role,
|
|
723
|
+
createdAt: formatDateForMongoDB(row.createdAt),
|
|
724
|
+
threadId: row.thread_id,
|
|
725
|
+
resourceId: row.resourceId
|
|
726
|
+
};
|
|
727
|
+
if (row.type && row.type !== "v2") result.type = row.type;
|
|
728
|
+
return result;
|
|
729
|
+
}
|
|
730
|
+
async _getIncludedMessages({
|
|
731
|
+
threadId,
|
|
732
|
+
include
|
|
733
|
+
}) {
|
|
734
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
735
|
+
if (!include) return null;
|
|
736
|
+
const collection = await this.operations.getCollection(storage.TABLE_MESSAGES);
|
|
737
|
+
const includedMessages = [];
|
|
738
|
+
for (const inc of include) {
|
|
739
|
+
const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
|
|
740
|
+
const searchThreadId = inc.threadId || threadId;
|
|
741
|
+
const allMessages = await collection.find({ thread_id: searchThreadId }).sort({ createdAt: 1 }).toArray();
|
|
742
|
+
const targetIndex = allMessages.findIndex((msg) => msg.id === id);
|
|
743
|
+
if (targetIndex === -1) continue;
|
|
744
|
+
const startIndex = Math.max(0, targetIndex - withPreviousMessages);
|
|
745
|
+
const endIndex = Math.min(allMessages.length - 1, targetIndex + withNextMessages);
|
|
746
|
+
for (let i = startIndex; i <= endIndex; i++) {
|
|
747
|
+
includedMessages.push(allMessages[i]);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
const seen = /* @__PURE__ */ new Set();
|
|
751
|
+
const dedupedMessages = includedMessages.filter((msg) => {
|
|
752
|
+
if (seen.has(msg.id)) return false;
|
|
753
|
+
seen.add(msg.id);
|
|
754
|
+
return true;
|
|
755
|
+
});
|
|
756
|
+
return dedupedMessages.map((row) => this.parseRow(row));
|
|
757
|
+
}
|
|
758
|
+
async listMessagesById({ messageIds }) {
|
|
759
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
760
|
+
try {
|
|
761
|
+
const collection = await this.operations.getCollection(storage.TABLE_MESSAGES);
|
|
762
|
+
const rawMessages = await collection.find({ id: { $in: messageIds } }).sort({ createdAt: -1 }).toArray();
|
|
763
|
+
const list = new agent.MessageList().add(
|
|
764
|
+
rawMessages.map(this.parseRow),
|
|
765
|
+
"memory"
|
|
766
|
+
);
|
|
767
|
+
return { messages: list.get.all.db() };
|
|
768
|
+
} catch (error$1) {
|
|
769
|
+
throw new error.MastraError(
|
|
770
|
+
{
|
|
771
|
+
id: "MONGODB_STORE_LIST_MESSAGES_BY_ID_FAILED",
|
|
772
|
+
domain: error.ErrorDomain.STORAGE,
|
|
773
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
774
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
775
|
+
},
|
|
776
|
+
error$1
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
async listMessages(args) {
|
|
781
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
782
|
+
if (!threadId.trim()) {
|
|
783
|
+
throw new error.MastraError(
|
|
784
|
+
{
|
|
785
|
+
id: "STORAGE_MONGODB_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
786
|
+
domain: error.ErrorDomain.STORAGE,
|
|
787
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
788
|
+
details: { threadId }
|
|
789
|
+
},
|
|
790
|
+
new Error("threadId must be a non-empty string")
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
if (page < 0) {
|
|
794
|
+
throw new error.MastraError(
|
|
795
|
+
{
|
|
796
|
+
id: "STORAGE_MONGODB_LIST_MESSAGES_INVALID_PAGE",
|
|
797
|
+
domain: error.ErrorDomain.STORAGE,
|
|
798
|
+
category: error.ErrorCategory.USER,
|
|
799
|
+
details: { page }
|
|
800
|
+
},
|
|
801
|
+
new Error("page must be >= 0")
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
805
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
806
|
+
try {
|
|
807
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
808
|
+
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
809
|
+
const collection = await this.operations.getCollection(storage.TABLE_MESSAGES);
|
|
810
|
+
const query = { thread_id: threadId };
|
|
811
|
+
if (resourceId) {
|
|
812
|
+
query.resourceId = resourceId;
|
|
813
|
+
}
|
|
814
|
+
if (filter?.dateRange?.start) {
|
|
815
|
+
query.createdAt = { ...query.createdAt, $gte: filter.dateRange.start };
|
|
816
|
+
}
|
|
817
|
+
if (filter?.dateRange?.end) {
|
|
818
|
+
query.createdAt = { ...query.createdAt, $lte: filter.dateRange.end };
|
|
819
|
+
}
|
|
820
|
+
const total = await collection.countDocuments(query);
|
|
821
|
+
const messages = [];
|
|
822
|
+
if (perPage !== 0) {
|
|
823
|
+
const sortObj = { [field]: sortOrder };
|
|
824
|
+
let cursor = collection.find(query).sort(sortObj).skip(offset);
|
|
825
|
+
if (perPageInput !== false) {
|
|
826
|
+
cursor = cursor.limit(perPage);
|
|
827
|
+
}
|
|
828
|
+
const dataResult = await cursor.toArray();
|
|
829
|
+
messages.push(...dataResult.map((row) => this.parseRow(row)));
|
|
830
|
+
}
|
|
831
|
+
if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
|
|
832
|
+
return {
|
|
833
|
+
messages: [],
|
|
834
|
+
total: 0,
|
|
835
|
+
page,
|
|
836
|
+
perPage: perPageForResponse,
|
|
837
|
+
hasMore: false
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
const messageIds = new Set(messages.map((m) => m.id));
|
|
841
|
+
if (include && include.length > 0) {
|
|
842
|
+
const includeMessages = await this._getIncludedMessages({ threadId, include });
|
|
843
|
+
if (includeMessages) {
|
|
844
|
+
for (const includeMsg of includeMessages) {
|
|
845
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
846
|
+
messages.push(includeMsg);
|
|
847
|
+
messageIds.add(includeMsg.id);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
853
|
+
let finalMessages = list.get.all.db();
|
|
854
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
855
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
856
|
+
const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
857
|
+
const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
858
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
859
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
860
|
+
}
|
|
861
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
862
|
+
});
|
|
863
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
864
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
865
|
+
const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
|
|
866
|
+
return {
|
|
867
|
+
messages: finalMessages,
|
|
868
|
+
total,
|
|
869
|
+
page,
|
|
870
|
+
perPage: perPageForResponse,
|
|
871
|
+
hasMore
|
|
872
|
+
};
|
|
873
|
+
} catch (error$1) {
|
|
874
|
+
const mastraError = new error.MastraError(
|
|
875
|
+
{
|
|
876
|
+
id: "MONGODB_STORE_LIST_MESSAGES_FAILED",
|
|
877
|
+
domain: error.ErrorDomain.STORAGE,
|
|
878
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
879
|
+
details: {
|
|
880
|
+
threadId,
|
|
881
|
+
resourceId: resourceId ?? ""
|
|
882
|
+
}
|
|
883
|
+
},
|
|
884
|
+
error$1
|
|
885
|
+
);
|
|
886
|
+
this.logger?.error?.(mastraError.toString());
|
|
887
|
+
this.logger?.trackException?.(mastraError);
|
|
888
|
+
return {
|
|
889
|
+
messages: [],
|
|
890
|
+
total: 0,
|
|
891
|
+
page,
|
|
892
|
+
perPage: perPageForResponse,
|
|
893
|
+
hasMore: false
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
async saveMessages({ messages }) {
|
|
898
|
+
if (messages.length === 0) return { messages: [] };
|
|
899
|
+
try {
|
|
900
|
+
const threadId = messages[0]?.threadId;
|
|
901
|
+
if (!threadId) {
|
|
902
|
+
throw new Error("Thread ID is required");
|
|
903
|
+
}
|
|
904
|
+
const collection = await this.operations.getCollection(storage.TABLE_MESSAGES);
|
|
905
|
+
const threadsCollection = await this.operations.getCollection(storage.TABLE_THREADS);
|
|
906
|
+
const messagesToInsert = messages.map((message) => {
|
|
907
|
+
const time = message.createdAt || /* @__PURE__ */ new Date();
|
|
908
|
+
if (!message.threadId) {
|
|
909
|
+
throw new Error(
|
|
910
|
+
"Expected to find a threadId for message, but couldn't find one. An unexpected error has occurred."
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
if (!message.resourceId) {
|
|
914
|
+
throw new Error(
|
|
915
|
+
"Expected to find a resourceId for message, but couldn't find one. An unexpected error has occurred."
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
return {
|
|
919
|
+
updateOne: {
|
|
920
|
+
filter: { id: message.id },
|
|
921
|
+
update: {
|
|
922
|
+
$set: {
|
|
923
|
+
id: message.id,
|
|
924
|
+
thread_id: message.threadId,
|
|
925
|
+
content: typeof message.content === "object" ? JSON.stringify(message.content) : message.content,
|
|
926
|
+
role: message.role,
|
|
927
|
+
type: message.type || "v2",
|
|
928
|
+
createdAt: formatDateForMongoDB(time),
|
|
929
|
+
resourceId: message.resourceId
|
|
930
|
+
}
|
|
931
|
+
},
|
|
932
|
+
upsert: true
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
});
|
|
936
|
+
await Promise.all([
|
|
937
|
+
collection.bulkWrite(messagesToInsert),
|
|
938
|
+
threadsCollection.updateOne({ id: threadId }, { $set: { updatedAt: /* @__PURE__ */ new Date() } })
|
|
939
|
+
]);
|
|
940
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
941
|
+
return { messages: list.get.all.db() };
|
|
942
|
+
} catch (error$1) {
|
|
943
|
+
throw new error.MastraError(
|
|
944
|
+
{
|
|
945
|
+
id: "MONGODB_STORE_SAVE_MESSAGES_FAILED",
|
|
946
|
+
domain: error.ErrorDomain.STORAGE,
|
|
947
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
948
|
+
},
|
|
949
|
+
error$1
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
async updateMessages({
|
|
954
|
+
messages
|
|
955
|
+
}) {
|
|
956
|
+
if (messages.length === 0) {
|
|
957
|
+
return [];
|
|
958
|
+
}
|
|
959
|
+
const messageIds = messages.map((m) => m.id);
|
|
960
|
+
const collection = await this.operations.getCollection(storage.TABLE_MESSAGES);
|
|
961
|
+
const existingMessages = await collection.find({ id: { $in: messageIds } }).toArray();
|
|
962
|
+
const existingMessagesParsed = existingMessages.map((msg) => this.parseRow(msg));
|
|
963
|
+
if (existingMessagesParsed.length === 0) {
|
|
964
|
+
return [];
|
|
965
|
+
}
|
|
966
|
+
const threadIdsToUpdate = /* @__PURE__ */ new Set();
|
|
967
|
+
const bulkOps = [];
|
|
968
|
+
for (const existingMessage of existingMessagesParsed) {
|
|
969
|
+
const updatePayload = messages.find((m) => m.id === existingMessage.id);
|
|
970
|
+
if (!updatePayload) continue;
|
|
971
|
+
const { id, ...fieldsToUpdate } = updatePayload;
|
|
972
|
+
if (Object.keys(fieldsToUpdate).length === 0) continue;
|
|
973
|
+
threadIdsToUpdate.add(existingMessage.threadId);
|
|
974
|
+
if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
|
|
975
|
+
threadIdsToUpdate.add(updatePayload.threadId);
|
|
976
|
+
}
|
|
977
|
+
const updateDoc = {};
|
|
978
|
+
const updatableFields = { ...fieldsToUpdate };
|
|
979
|
+
if (updatableFields.content) {
|
|
980
|
+
const newContent = {
|
|
981
|
+
...existingMessage.content,
|
|
982
|
+
...updatableFields.content,
|
|
983
|
+
// Deep merge metadata if it exists on both
|
|
984
|
+
...existingMessage.content?.metadata && updatableFields.content.metadata ? {
|
|
985
|
+
metadata: {
|
|
986
|
+
...existingMessage.content.metadata,
|
|
987
|
+
...updatableFields.content.metadata
|
|
988
|
+
}
|
|
989
|
+
} : {}
|
|
990
|
+
};
|
|
991
|
+
updateDoc.content = JSON.stringify(newContent);
|
|
992
|
+
delete updatableFields.content;
|
|
993
|
+
}
|
|
994
|
+
for (const key in updatableFields) {
|
|
995
|
+
if (Object.prototype.hasOwnProperty.call(updatableFields, key)) {
|
|
996
|
+
const dbKey = key === "threadId" ? "thread_id" : key;
|
|
997
|
+
let value = updatableFields[key];
|
|
998
|
+
if (typeof value === "object" && value !== null) {
|
|
999
|
+
value = JSON.stringify(value);
|
|
1000
|
+
}
|
|
1001
|
+
updateDoc[dbKey] = value;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
if (Object.keys(updateDoc).length > 0) {
|
|
1005
|
+
bulkOps.push({
|
|
1006
|
+
updateOne: {
|
|
1007
|
+
filter: { id },
|
|
1008
|
+
update: { $set: updateDoc }
|
|
1009
|
+
}
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
if (bulkOps.length > 0) {
|
|
1014
|
+
await collection.bulkWrite(bulkOps);
|
|
1015
|
+
}
|
|
1016
|
+
if (threadIdsToUpdate.size > 0) {
|
|
1017
|
+
const threadsCollection = await this.operations.getCollection(storage.TABLE_THREADS);
|
|
1018
|
+
await threadsCollection.updateMany(
|
|
1019
|
+
{ id: { $in: Array.from(threadIdsToUpdate) } },
|
|
1020
|
+
{ $set: { updatedAt: /* @__PURE__ */ new Date() } }
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
const updatedMessages = await collection.find({ id: { $in: messageIds } }).toArray();
|
|
1024
|
+
return updatedMessages.map((row) => this.parseRow(row));
|
|
1025
|
+
}
|
|
1026
|
+
async getResourceById({ resourceId }) {
|
|
1027
|
+
try {
|
|
1028
|
+
const collection = await this.operations.getCollection(storage.TABLE_RESOURCES);
|
|
1029
|
+
const result = await collection.findOne({ id: resourceId });
|
|
1030
|
+
if (!result) {
|
|
1031
|
+
return null;
|
|
1032
|
+
}
|
|
1033
|
+
return {
|
|
1034
|
+
id: result.id,
|
|
1035
|
+
workingMemory: result.workingMemory || "",
|
|
1036
|
+
metadata: typeof result.metadata === "string" ? storage.safelyParseJSON(result.metadata) : result.metadata,
|
|
1037
|
+
createdAt: formatDateForMongoDB(result.createdAt),
|
|
1038
|
+
updatedAt: formatDateForMongoDB(result.updatedAt)
|
|
1039
|
+
};
|
|
1040
|
+
} catch (error$1) {
|
|
1041
|
+
throw new error.MastraError(
|
|
1042
|
+
{
|
|
1043
|
+
id: "STORAGE_MONGODB_STORE_GET_RESOURCE_BY_ID_FAILED",
|
|
1044
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1045
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1046
|
+
details: { resourceId }
|
|
1047
|
+
},
|
|
1048
|
+
error$1
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
async saveResource({ resource }) {
|
|
1053
|
+
try {
|
|
1054
|
+
const collection = await this.operations.getCollection(storage.TABLE_RESOURCES);
|
|
1055
|
+
await collection.updateOne(
|
|
1056
|
+
{ id: resource.id },
|
|
1057
|
+
{
|
|
1058
|
+
$set: {
|
|
1059
|
+
...resource,
|
|
1060
|
+
metadata: JSON.stringify(resource.metadata)
|
|
1061
|
+
}
|
|
1062
|
+
},
|
|
1063
|
+
{ upsert: true }
|
|
1064
|
+
);
|
|
1065
|
+
return resource;
|
|
1066
|
+
} catch (error$1) {
|
|
1067
|
+
throw new error.MastraError(
|
|
1068
|
+
{
|
|
1069
|
+
id: "STORAGE_MONGODB_STORE_SAVE_RESOURCE_FAILED",
|
|
1070
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1071
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1072
|
+
details: { resourceId: resource.id }
|
|
1073
|
+
},
|
|
1074
|
+
error$1
|
|
1075
|
+
);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
async updateResource({
|
|
1079
|
+
resourceId,
|
|
1080
|
+
workingMemory,
|
|
1081
|
+
metadata
|
|
1082
|
+
}) {
|
|
1083
|
+
try {
|
|
1084
|
+
const existingResource = await this.getResourceById({ resourceId });
|
|
1085
|
+
if (!existingResource) {
|
|
1086
|
+
const newResource = {
|
|
1087
|
+
id: resourceId,
|
|
1088
|
+
workingMemory: workingMemory || "",
|
|
1089
|
+
metadata: metadata || {},
|
|
1090
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
1091
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
1092
|
+
};
|
|
1093
|
+
return this.saveResource({ resource: newResource });
|
|
1094
|
+
}
|
|
1095
|
+
const updatedResource = {
|
|
1096
|
+
...existingResource,
|
|
1097
|
+
workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
|
|
1098
|
+
metadata: metadata ? { ...existingResource.metadata, ...metadata } : existingResource.metadata,
|
|
1099
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
1100
|
+
};
|
|
1101
|
+
const collection = await this.operations.getCollection(storage.TABLE_RESOURCES);
|
|
1102
|
+
const updateDoc = { updatedAt: updatedResource.updatedAt };
|
|
1103
|
+
if (workingMemory !== void 0) {
|
|
1104
|
+
updateDoc.workingMemory = workingMemory;
|
|
1105
|
+
}
|
|
1106
|
+
if (metadata) {
|
|
1107
|
+
updateDoc.metadata = JSON.stringify(updatedResource.metadata);
|
|
1108
|
+
}
|
|
1109
|
+
await collection.updateOne({ id: resourceId }, { $set: updateDoc });
|
|
1110
|
+
return updatedResource;
|
|
1111
|
+
} catch (error$1) {
|
|
1112
|
+
throw new error.MastraError(
|
|
1113
|
+
{
|
|
1114
|
+
id: "STORAGE_MONGODB_STORE_UPDATE_RESOURCE_FAILED",
|
|
1115
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1116
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1117
|
+
details: { resourceId }
|
|
1118
|
+
},
|
|
1119
|
+
error$1
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
async getThreadById({ threadId }) {
|
|
1124
|
+
try {
|
|
1125
|
+
const collection = await this.operations.getCollection(storage.TABLE_THREADS);
|
|
1126
|
+
const result = await collection.findOne({ id: threadId });
|
|
1127
|
+
if (!result) {
|
|
1128
|
+
return null;
|
|
1129
|
+
}
|
|
1130
|
+
return {
|
|
1131
|
+
...result,
|
|
1132
|
+
metadata: typeof result.metadata === "string" ? storage.safelyParseJSON(result.metadata) : result.metadata
|
|
1133
|
+
};
|
|
1134
|
+
} catch (error$1) {
|
|
1135
|
+
throw new error.MastraError(
|
|
1136
|
+
{
|
|
1137
|
+
id: "STORAGE_MONGODB_STORE_GET_THREAD_BY_ID_FAILED",
|
|
1138
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1139
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1140
|
+
details: { threadId }
|
|
1141
|
+
},
|
|
1142
|
+
error$1
|
|
1143
|
+
);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
async listThreadsByResourceId(args) {
|
|
1147
|
+
try {
|
|
1148
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
1149
|
+
if (page < 0) {
|
|
1150
|
+
throw new error.MastraError(
|
|
1151
|
+
{
|
|
1152
|
+
id: "STORAGE_MONGODB_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
1153
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1154
|
+
category: error.ErrorCategory.USER,
|
|
1155
|
+
details: { page }
|
|
1156
|
+
},
|
|
1157
|
+
new Error("page must be >= 0")
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1161
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1162
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1163
|
+
const collection = await this.operations.getCollection(storage.TABLE_THREADS);
|
|
1164
|
+
const query = { resourceId };
|
|
1165
|
+
const total = await collection.countDocuments(query);
|
|
1166
|
+
if (perPage === 0) {
|
|
1167
|
+
return {
|
|
1168
|
+
threads: [],
|
|
1169
|
+
total,
|
|
1170
|
+
page,
|
|
1171
|
+
perPage: perPageForResponse,
|
|
1172
|
+
hasMore: offset < total
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
1176
|
+
let cursor = collection.find(query).sort({ [field]: sortOrder }).skip(offset);
|
|
1177
|
+
if (perPageInput !== false) {
|
|
1178
|
+
cursor = cursor.limit(perPage);
|
|
1179
|
+
}
|
|
1180
|
+
const threads = await cursor.toArray();
|
|
1181
|
+
return {
|
|
1182
|
+
threads: threads.map((thread) => ({
|
|
1183
|
+
id: thread.id,
|
|
1184
|
+
title: thread.title,
|
|
1185
|
+
resourceId: thread.resourceId,
|
|
1186
|
+
createdAt: formatDateForMongoDB(thread.createdAt),
|
|
1187
|
+
updatedAt: formatDateForMongoDB(thread.updatedAt),
|
|
1188
|
+
metadata: thread.metadata || {}
|
|
1189
|
+
})),
|
|
1190
|
+
total,
|
|
1191
|
+
page,
|
|
1192
|
+
perPage: perPageForResponse,
|
|
1193
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
1194
|
+
};
|
|
1195
|
+
} catch (error$1) {
|
|
1196
|
+
throw new error.MastraError(
|
|
1197
|
+
{
|
|
1198
|
+
id: "MONGODB_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
1199
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1200
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1201
|
+
details: { resourceId: args.resourceId }
|
|
1202
|
+
},
|
|
1203
|
+
error$1
|
|
1204
|
+
);
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
async saveThread({ thread }) {
|
|
1208
|
+
try {
|
|
1209
|
+
const collection = await this.operations.getCollection(storage.TABLE_THREADS);
|
|
1210
|
+
await collection.updateOne(
|
|
1211
|
+
{ id: thread.id },
|
|
1212
|
+
{
|
|
1213
|
+
$set: {
|
|
1214
|
+
...thread,
|
|
1215
|
+
metadata: thread.metadata
|
|
1216
|
+
}
|
|
1217
|
+
},
|
|
1218
|
+
{ upsert: true }
|
|
1219
|
+
);
|
|
1220
|
+
return thread;
|
|
1221
|
+
} catch (error$1) {
|
|
1222
|
+
throw new error.MastraError(
|
|
1223
|
+
{
|
|
1224
|
+
id: "STORAGE_MONGODB_STORE_SAVE_THREAD_FAILED",
|
|
1225
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1226
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1227
|
+
details: { threadId: thread.id }
|
|
1228
|
+
},
|
|
1229
|
+
error$1
|
|
1230
|
+
);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
async updateThread({
|
|
1234
|
+
id,
|
|
1235
|
+
title,
|
|
1236
|
+
metadata
|
|
1237
|
+
}) {
|
|
1238
|
+
const thread = await this.getThreadById({ threadId: id });
|
|
1239
|
+
if (!thread) {
|
|
1240
|
+
throw new error.MastraError({
|
|
1241
|
+
id: "STORAGE_MONGODB_STORE_UPDATE_THREAD_NOT_FOUND",
|
|
1242
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1243
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1244
|
+
details: { threadId: id, status: 404 },
|
|
1245
|
+
text: `Thread ${id} not found`
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1248
|
+
const updatedThread = {
|
|
1249
|
+
...thread,
|
|
1250
|
+
title,
|
|
1251
|
+
metadata: {
|
|
1252
|
+
...thread.metadata,
|
|
1253
|
+
...metadata
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
try {
|
|
1257
|
+
const collection = await this.operations.getCollection(storage.TABLE_THREADS);
|
|
1258
|
+
await collection.updateOne(
|
|
1259
|
+
{ id },
|
|
1260
|
+
{
|
|
1261
|
+
$set: {
|
|
1262
|
+
title,
|
|
1263
|
+
metadata: updatedThread.metadata
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
);
|
|
1267
|
+
} catch (error$1) {
|
|
1268
|
+
throw new error.MastraError(
|
|
1269
|
+
{
|
|
1270
|
+
id: "STORAGE_MONGODB_STORE_UPDATE_THREAD_FAILED",
|
|
1271
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1272
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1273
|
+
details: { threadId: id }
|
|
1274
|
+
},
|
|
1275
|
+
error$1
|
|
1276
|
+
);
|
|
1277
|
+
}
|
|
1278
|
+
return updatedThread;
|
|
1279
|
+
}
|
|
1280
|
+
async deleteThread({ threadId }) {
|
|
1281
|
+
try {
|
|
1282
|
+
const collectionMessages = await this.operations.getCollection(storage.TABLE_MESSAGES);
|
|
1283
|
+
await collectionMessages.deleteMany({ thread_id: threadId });
|
|
1284
|
+
const collectionThreads = await this.operations.getCollection(storage.TABLE_THREADS);
|
|
1285
|
+
await collectionThreads.deleteOne({ id: threadId });
|
|
1286
|
+
} catch (error$1) {
|
|
1287
|
+
throw new error.MastraError(
|
|
1288
|
+
{
|
|
1289
|
+
id: "STORAGE_MONGODB_STORE_DELETE_THREAD_FAILED",
|
|
1290
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1291
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1292
|
+
details: { threadId }
|
|
1293
|
+
},
|
|
1294
|
+
error$1
|
|
1295
|
+
);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
};
|
|
1299
|
+
var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
1300
|
+
operations;
|
|
1301
|
+
constructor({ operations }) {
|
|
1302
|
+
super();
|
|
1303
|
+
this.operations = operations;
|
|
1304
|
+
}
|
|
1305
|
+
get tracingStrategy() {
|
|
1306
|
+
return {
|
|
1307
|
+
preferred: "batch-with-updates",
|
|
1308
|
+
supported: ["batch-with-updates", "insert-only"]
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
async createSpan(span) {
|
|
1312
|
+
try {
|
|
1313
|
+
const startedAt = span.startedAt instanceof Date ? span.startedAt.toISOString() : span.startedAt;
|
|
1314
|
+
const endedAt = span.endedAt instanceof Date ? span.endedAt.toISOString() : span.endedAt;
|
|
1315
|
+
const record = {
|
|
1316
|
+
...span,
|
|
1317
|
+
startedAt,
|
|
1318
|
+
endedAt,
|
|
1319
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1320
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1321
|
+
};
|
|
1322
|
+
return this.operations.insert({ tableName: storage.TABLE_SPANS, record });
|
|
1323
|
+
} catch (error$1) {
|
|
1324
|
+
throw new error.MastraError(
|
|
1325
|
+
{
|
|
1326
|
+
id: "MONGODB_STORE_CREATE_SPAN_FAILED",
|
|
1327
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1328
|
+
category: error.ErrorCategory.USER,
|
|
1329
|
+
details: {
|
|
1330
|
+
spanId: span.spanId,
|
|
1331
|
+
traceId: span.traceId,
|
|
1332
|
+
spanType: span.spanType,
|
|
1333
|
+
spanName: span.name
|
|
1334
|
+
}
|
|
1335
|
+
},
|
|
1336
|
+
error$1
|
|
1337
|
+
);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
async getTrace(traceId) {
|
|
1341
|
+
try {
|
|
1342
|
+
const collection = await this.operations.getCollection(storage.TABLE_SPANS);
|
|
1343
|
+
const spans = await collection.find({ traceId }).sort({ startedAt: -1 }).toArray();
|
|
1344
|
+
if (!spans || spans.length === 0) {
|
|
1345
|
+
return null;
|
|
1346
|
+
}
|
|
1347
|
+
return {
|
|
1348
|
+
traceId,
|
|
1349
|
+
spans: spans.map((span) => this.transformSpanFromMongo(span))
|
|
1350
|
+
};
|
|
1351
|
+
} catch (error$1) {
|
|
1352
|
+
throw new error.MastraError(
|
|
1353
|
+
{
|
|
1354
|
+
id: "MONGODB_STORE_GET_TRACE_FAILED",
|
|
1355
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1356
|
+
category: error.ErrorCategory.USER,
|
|
1357
|
+
details: {
|
|
1358
|
+
traceId
|
|
1359
|
+
}
|
|
1360
|
+
},
|
|
1361
|
+
error$1
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
async updateSpan({
|
|
1366
|
+
spanId,
|
|
1367
|
+
traceId,
|
|
1368
|
+
updates
|
|
1369
|
+
}) {
|
|
1370
|
+
try {
|
|
1371
|
+
const data = { ...updates };
|
|
1372
|
+
if (data.endedAt instanceof Date) {
|
|
1373
|
+
data.endedAt = data.endedAt.toISOString();
|
|
1374
|
+
}
|
|
1375
|
+
if (data.startedAt instanceof Date) {
|
|
1376
|
+
data.startedAt = data.startedAt.toISOString();
|
|
1377
|
+
}
|
|
1378
|
+
const updateData = {
|
|
1379
|
+
...data,
|
|
1380
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1381
|
+
};
|
|
1382
|
+
await this.operations.update({
|
|
1383
|
+
tableName: storage.TABLE_SPANS,
|
|
1384
|
+
keys: { spanId, traceId },
|
|
1385
|
+
data: updateData
|
|
1386
|
+
});
|
|
1387
|
+
} catch (error$1) {
|
|
1388
|
+
throw new error.MastraError(
|
|
1389
|
+
{
|
|
1390
|
+
id: "MONGODB_STORE_UPDATE_SPAN_FAILED",
|
|
1391
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1392
|
+
category: error.ErrorCategory.USER,
|
|
1393
|
+
details: {
|
|
1394
|
+
spanId,
|
|
1395
|
+
traceId
|
|
1396
|
+
}
|
|
1397
|
+
},
|
|
1398
|
+
error$1
|
|
1399
|
+
);
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
async getTracesPaginated({
|
|
1403
|
+
filters,
|
|
1404
|
+
pagination
|
|
1405
|
+
}) {
|
|
1406
|
+
const page = pagination?.page ?? 0;
|
|
1407
|
+
const perPage = pagination?.perPage ?? 10;
|
|
1408
|
+
const { entityId, entityType, ...actualFilters } = filters || {};
|
|
1409
|
+
try {
|
|
1410
|
+
const collection = await this.operations.getCollection(storage.TABLE_SPANS);
|
|
1411
|
+
const mongoFilter = {
|
|
1412
|
+
parentSpanId: null,
|
|
1413
|
+
// Only get root spans for traces
|
|
1414
|
+
...actualFilters
|
|
1415
|
+
};
|
|
1416
|
+
if (pagination?.dateRange) {
|
|
1417
|
+
const dateFilter = {};
|
|
1418
|
+
if (pagination.dateRange.start) {
|
|
1419
|
+
dateFilter.$gte = pagination.dateRange.start instanceof Date ? pagination.dateRange.start.toISOString() : pagination.dateRange.start;
|
|
1420
|
+
}
|
|
1421
|
+
if (pagination.dateRange.end) {
|
|
1422
|
+
dateFilter.$lte = pagination.dateRange.end instanceof Date ? pagination.dateRange.end.toISOString() : pagination.dateRange.end;
|
|
1423
|
+
}
|
|
1424
|
+
if (Object.keys(dateFilter).length > 0) {
|
|
1425
|
+
mongoFilter.startedAt = dateFilter;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
if (entityId && entityType) {
|
|
1429
|
+
let name = "";
|
|
1430
|
+
if (entityType === "workflow") {
|
|
1431
|
+
name = `workflow run: '${entityId}'`;
|
|
1432
|
+
} else if (entityType === "agent") {
|
|
1433
|
+
name = `agent run: '${entityId}'`;
|
|
1434
|
+
} else {
|
|
1435
|
+
const error$1 = new error.MastraError({
|
|
1436
|
+
id: "MONGODB_STORE_GET_TRACES_PAGINATED_FAILED",
|
|
1437
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1438
|
+
category: error.ErrorCategory.USER,
|
|
1439
|
+
details: {
|
|
1440
|
+
entityType
|
|
1441
|
+
},
|
|
1442
|
+
text: `Cannot filter by entity type: ${entityType}`
|
|
1443
|
+
});
|
|
1444
|
+
throw error$1;
|
|
1445
|
+
}
|
|
1446
|
+
mongoFilter.name = name;
|
|
1447
|
+
}
|
|
1448
|
+
const count = await collection.countDocuments(mongoFilter);
|
|
1449
|
+
if (count === 0) {
|
|
1450
|
+
return {
|
|
1451
|
+
pagination: {
|
|
1452
|
+
total: 0,
|
|
1453
|
+
page,
|
|
1454
|
+
perPage,
|
|
1455
|
+
hasMore: false
|
|
1456
|
+
},
|
|
1457
|
+
spans: []
|
|
1458
|
+
};
|
|
1459
|
+
}
|
|
1460
|
+
const spans = await collection.find(mongoFilter).sort({ startedAt: -1 }).skip(page * perPage).limit(perPage).toArray();
|
|
1461
|
+
return {
|
|
1462
|
+
pagination: {
|
|
1463
|
+
total: count,
|
|
1464
|
+
page,
|
|
1465
|
+
perPage,
|
|
1466
|
+
hasMore: spans.length === perPage
|
|
1467
|
+
},
|
|
1468
|
+
spans: spans.map((span) => this.transformSpanFromMongo(span))
|
|
1469
|
+
};
|
|
1470
|
+
} catch (error$1) {
|
|
1471
|
+
throw new error.MastraError(
|
|
1472
|
+
{
|
|
1473
|
+
id: "MONGODB_STORE_GET_TRACES_PAGINATED_FAILED",
|
|
1474
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1475
|
+
category: error.ErrorCategory.USER
|
|
1476
|
+
},
|
|
1477
|
+
error$1
|
|
1478
|
+
);
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
async batchCreateSpans(args) {
|
|
1482
|
+
try {
|
|
1483
|
+
const records = args.records.map((record) => {
|
|
1484
|
+
const startedAt = record.startedAt instanceof Date ? record.startedAt.toISOString() : record.startedAt;
|
|
1485
|
+
const endedAt = record.endedAt instanceof Date ? record.endedAt.toISOString() : record.endedAt;
|
|
1486
|
+
return {
|
|
1487
|
+
...record,
|
|
1488
|
+
startedAt,
|
|
1489
|
+
endedAt,
|
|
1490
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1491
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1492
|
+
};
|
|
1493
|
+
});
|
|
1494
|
+
return this.operations.batchInsert({
|
|
1495
|
+
tableName: storage.TABLE_SPANS,
|
|
1496
|
+
records
|
|
1497
|
+
});
|
|
1498
|
+
} catch (error$1) {
|
|
1499
|
+
throw new error.MastraError(
|
|
1500
|
+
{
|
|
1501
|
+
id: "MONGODB_STORE_BATCH_CREATE_SPANS_FAILED",
|
|
1502
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1503
|
+
category: error.ErrorCategory.USER
|
|
1504
|
+
},
|
|
1505
|
+
error$1
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
async batchUpdateSpans(args) {
|
|
1510
|
+
try {
|
|
1511
|
+
return this.operations.batchUpdate({
|
|
1512
|
+
tableName: storage.TABLE_SPANS,
|
|
1513
|
+
updates: args.records.map((record) => {
|
|
1514
|
+
const data = { ...record.updates };
|
|
1515
|
+
if (data.endedAt instanceof Date) {
|
|
1516
|
+
data.endedAt = data.endedAt.toISOString();
|
|
1517
|
+
}
|
|
1518
|
+
if (data.startedAt instanceof Date) {
|
|
1519
|
+
data.startedAt = data.startedAt.toISOString();
|
|
1520
|
+
}
|
|
1521
|
+
const updateData = {
|
|
1522
|
+
...data,
|
|
1523
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1524
|
+
};
|
|
1525
|
+
return {
|
|
1526
|
+
keys: { spanId: record.spanId, traceId: record.traceId },
|
|
1527
|
+
data: updateData
|
|
1528
|
+
};
|
|
1529
|
+
})
|
|
1530
|
+
});
|
|
1531
|
+
} catch (error$1) {
|
|
1532
|
+
throw new error.MastraError(
|
|
1533
|
+
{
|
|
1534
|
+
id: "MONGODB_STORE_BATCH_UPDATE_SPANS_FAILED",
|
|
1535
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1536
|
+
category: error.ErrorCategory.USER
|
|
1537
|
+
},
|
|
1538
|
+
error$1
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
async batchDeleteTraces(args) {
|
|
1543
|
+
try {
|
|
1544
|
+
const collection = await this.operations.getCollection(storage.TABLE_SPANS);
|
|
1545
|
+
await collection.deleteMany({
|
|
1546
|
+
traceId: { $in: args.traceIds }
|
|
1547
|
+
});
|
|
1548
|
+
} catch (error$1) {
|
|
1549
|
+
throw new error.MastraError(
|
|
1550
|
+
{
|
|
1551
|
+
id: "MONGODB_STORE_BATCH_DELETE_TRACES_FAILED",
|
|
1552
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1553
|
+
category: error.ErrorCategory.USER
|
|
1554
|
+
},
|
|
1555
|
+
error$1
|
|
1556
|
+
);
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
/**
|
|
1560
|
+
* Transform MongoDB document to SpanRecord format
|
|
1561
|
+
*/
|
|
1562
|
+
transformSpanFromMongo(doc) {
|
|
1563
|
+
const { _id, ...span } = doc;
|
|
1564
|
+
if (span.startedAt && typeof span.startedAt === "string") {
|
|
1565
|
+
span.startedAt = span.startedAt;
|
|
1566
|
+
}
|
|
1567
|
+
if (span.endedAt && typeof span.endedAt === "string") {
|
|
1568
|
+
span.endedAt = span.endedAt;
|
|
1569
|
+
}
|
|
1570
|
+
if (span.createdAt && typeof span.createdAt === "string") {
|
|
1571
|
+
span.createdAt = new Date(span.createdAt);
|
|
1572
|
+
}
|
|
1573
|
+
if (span.updatedAt && typeof span.updatedAt === "string") {
|
|
1574
|
+
span.updatedAt = new Date(span.updatedAt);
|
|
1575
|
+
}
|
|
1576
|
+
return span;
|
|
1577
|
+
}
|
|
1578
|
+
};
|
|
1579
|
+
var StoreOperationsMongoDB = class extends storage.StoreOperations {
|
|
1580
|
+
#connector;
|
|
1581
|
+
constructor(config) {
|
|
1582
|
+
super();
|
|
1583
|
+
this.#connector = config.connector;
|
|
1584
|
+
}
|
|
1585
|
+
async getCollection(collectionName) {
|
|
1586
|
+
return this.#connector.getCollection(collectionName);
|
|
1587
|
+
}
|
|
1588
|
+
async hasColumn(_table, _column) {
|
|
1589
|
+
return true;
|
|
1590
|
+
}
|
|
1591
|
+
async createTable() {
|
|
1592
|
+
}
|
|
1593
|
+
async alterTable(_args) {
|
|
1594
|
+
}
|
|
1595
|
+
async clearTable({ tableName }) {
|
|
1596
|
+
try {
|
|
1597
|
+
const collection = await this.getCollection(tableName);
|
|
1598
|
+
await collection.deleteMany({});
|
|
1599
|
+
} catch (error$1) {
|
|
1600
|
+
const mastraError = new error.MastraError(
|
|
1601
|
+
{
|
|
1602
|
+
id: "STORAGE_MONGODB_STORE_CLEAR_TABLE_FAILED",
|
|
1603
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1604
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1605
|
+
details: { tableName }
|
|
1606
|
+
},
|
|
1607
|
+
error$1
|
|
1608
|
+
);
|
|
1609
|
+
this.logger.error(mastraError.message);
|
|
1610
|
+
this.logger?.trackException(mastraError);
|
|
1611
|
+
throw mastraError;
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
async dropTable({ tableName }) {
|
|
1615
|
+
try {
|
|
1616
|
+
const collection = await this.getCollection(tableName);
|
|
1617
|
+
await collection.drop();
|
|
1618
|
+
} catch (error$1) {
|
|
1619
|
+
if (error$1 instanceof Error && error$1.message.includes("ns not found")) {
|
|
1620
|
+
return;
|
|
1621
|
+
}
|
|
1622
|
+
throw new error.MastraError(
|
|
1623
|
+
{
|
|
1624
|
+
id: "MONGODB_STORE_DROP_TABLE_FAILED",
|
|
1625
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1626
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1627
|
+
details: { tableName }
|
|
1628
|
+
},
|
|
1629
|
+
error$1
|
|
1630
|
+
);
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
processJsonbFields(tableName, record) {
|
|
1634
|
+
const schema = storage.TABLE_SCHEMAS[tableName];
|
|
1635
|
+
if (!schema) {
|
|
1636
|
+
return record;
|
|
1637
|
+
}
|
|
1638
|
+
return Object.fromEntries(
|
|
1639
|
+
Object.entries(schema).map(([key, value]) => {
|
|
1640
|
+
if (value.type === "jsonb" && record[key] && typeof record[key] === "string") {
|
|
1641
|
+
return [key, storage.safelyParseJSON(record[key])];
|
|
1642
|
+
}
|
|
1643
|
+
return [key, record[key]];
|
|
1644
|
+
})
|
|
1645
|
+
);
|
|
1646
|
+
}
|
|
1647
|
+
async insert({ tableName, record }) {
|
|
1648
|
+
try {
|
|
1649
|
+
const collection = await this.getCollection(tableName);
|
|
1650
|
+
const recordToInsert = this.processJsonbFields(tableName, record);
|
|
1651
|
+
await collection.insertOne(recordToInsert);
|
|
1652
|
+
} catch (error$1) {
|
|
1653
|
+
const mastraError = new error.MastraError(
|
|
1654
|
+
{
|
|
1655
|
+
id: "STORAGE_MONGODB_STORE_INSERT_FAILED",
|
|
1656
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1657
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1658
|
+
details: { tableName }
|
|
1659
|
+
},
|
|
1660
|
+
error$1
|
|
1661
|
+
);
|
|
1662
|
+
this.logger.error(mastraError.message);
|
|
1663
|
+
this.logger?.trackException(mastraError);
|
|
1664
|
+
throw mastraError;
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
async batchInsert({ tableName, records }) {
|
|
1668
|
+
if (!records.length) {
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
try {
|
|
1672
|
+
const collection = await this.getCollection(tableName);
|
|
1673
|
+
const processedRecords = records.map((record) => this.processJsonbFields(tableName, record));
|
|
1674
|
+
await collection.insertMany(processedRecords);
|
|
1675
|
+
} catch (error$1) {
|
|
1676
|
+
throw new error.MastraError(
|
|
1677
|
+
{
|
|
1678
|
+
id: "STORAGE_MONGODB_STORE_BATCH_INSERT_FAILED",
|
|
1679
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1680
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1681
|
+
details: { tableName }
|
|
1682
|
+
},
|
|
1683
|
+
error$1
|
|
1684
|
+
);
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
async load({ tableName, keys }) {
|
|
1688
|
+
this.logger.info(`Loading ${tableName} with keys ${JSON.stringify(keys)}`);
|
|
1689
|
+
try {
|
|
1690
|
+
const collection = await this.getCollection(tableName);
|
|
1691
|
+
return await collection.find(keys).toArray();
|
|
1692
|
+
} catch (error$1) {
|
|
1693
|
+
throw new error.MastraError(
|
|
1694
|
+
{
|
|
1695
|
+
id: "STORAGE_MONGODB_STORE_LOAD_FAILED",
|
|
1696
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1697
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1698
|
+
details: { tableName }
|
|
1699
|
+
},
|
|
1700
|
+
error$1
|
|
1701
|
+
);
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
async update({
|
|
1705
|
+
tableName,
|
|
1706
|
+
keys,
|
|
1707
|
+
data
|
|
1708
|
+
}) {
|
|
1709
|
+
try {
|
|
1710
|
+
const collection = await this.getCollection(tableName);
|
|
1711
|
+
const processedData = this.processJsonbFields(tableName, data);
|
|
1712
|
+
const cleanData = Object.fromEntries(Object.entries(processedData).filter(([_, value]) => value !== void 0));
|
|
1713
|
+
await collection.updateOne(keys, { $set: cleanData });
|
|
1714
|
+
} catch (error$1) {
|
|
1715
|
+
throw new error.MastraError(
|
|
1716
|
+
{
|
|
1717
|
+
id: "STORAGE_MONGODB_STORE_UPDATE_FAILED",
|
|
1718
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1719
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1720
|
+
details: { tableName }
|
|
1721
|
+
},
|
|
1722
|
+
error$1
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
async batchUpdate({
|
|
1727
|
+
tableName,
|
|
1728
|
+
updates
|
|
1729
|
+
}) {
|
|
1730
|
+
if (!updates.length) {
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
try {
|
|
1734
|
+
const collection = await this.getCollection(tableName);
|
|
1735
|
+
const bulkOps = updates.map(({ keys, data }) => {
|
|
1736
|
+
const processedData = this.processJsonbFields(tableName, data);
|
|
1737
|
+
const cleanData = Object.fromEntries(Object.entries(processedData).filter(([_, value]) => value !== void 0));
|
|
1738
|
+
return {
|
|
1739
|
+
updateOne: {
|
|
1740
|
+
filter: keys,
|
|
1741
|
+
update: { $set: cleanData }
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
1744
|
+
});
|
|
1745
|
+
await collection.bulkWrite(bulkOps);
|
|
1746
|
+
} catch (error$1) {
|
|
1747
|
+
throw new error.MastraError(
|
|
1748
|
+
{
|
|
1749
|
+
id: "STORAGE_MONGODB_STORE_BATCH_UPDATE_FAILED",
|
|
1750
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1751
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1752
|
+
details: { tableName }
|
|
1753
|
+
},
|
|
1754
|
+
error$1
|
|
1755
|
+
);
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
};
|
|
1759
|
+
function transformScoreRow(row) {
|
|
1760
|
+
let scorerValue = null;
|
|
1761
|
+
if (row.scorer) {
|
|
1762
|
+
try {
|
|
1763
|
+
scorerValue = typeof row.scorer === "string" ? storage.safelyParseJSON(row.scorer) : row.scorer;
|
|
1764
|
+
} catch (e) {
|
|
1765
|
+
console.warn("Failed to parse scorer:", e);
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
let preprocessStepResultValue = null;
|
|
1769
|
+
if (row.preprocessStepResult) {
|
|
1770
|
+
try {
|
|
1771
|
+
preprocessStepResultValue = typeof row.preprocessStepResult === "string" ? storage.safelyParseJSON(row.preprocessStepResult) : row.preprocessStepResult;
|
|
1772
|
+
} catch (e) {
|
|
1773
|
+
console.warn("Failed to parse preprocessStepResult:", e);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
let analyzeStepResultValue = null;
|
|
1777
|
+
if (row.analyzeStepResult) {
|
|
1778
|
+
try {
|
|
1779
|
+
analyzeStepResultValue = typeof row.analyzeStepResult === "string" ? storage.safelyParseJSON(row.analyzeStepResult) : row.analyzeStepResult;
|
|
1780
|
+
} catch (e) {
|
|
1781
|
+
console.warn("Failed to parse analyzeStepResult:", e);
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
let inputValue = null;
|
|
1785
|
+
if (row.input) {
|
|
1786
|
+
try {
|
|
1787
|
+
inputValue = typeof row.input === "string" ? storage.safelyParseJSON(row.input) : row.input;
|
|
1788
|
+
} catch (e) {
|
|
1789
|
+
console.warn("Failed to parse input:", e);
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
let outputValue = null;
|
|
1793
|
+
if (row.output) {
|
|
1794
|
+
try {
|
|
1795
|
+
outputValue = typeof row.output === "string" ? storage.safelyParseJSON(row.output) : row.output;
|
|
1796
|
+
} catch (e) {
|
|
1797
|
+
console.warn("Failed to parse output:", e);
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
let entityValue = null;
|
|
1801
|
+
if (row.entity) {
|
|
1802
|
+
try {
|
|
1803
|
+
entityValue = typeof row.entity === "string" ? storage.safelyParseJSON(row.entity) : row.entity;
|
|
1804
|
+
} catch (e) {
|
|
1805
|
+
console.warn("Failed to parse entity:", e);
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
let requestContextValue = null;
|
|
1809
|
+
if (row.requestContext) {
|
|
1810
|
+
try {
|
|
1811
|
+
requestContextValue = typeof row.requestContext === "string" ? storage.safelyParseJSON(row.requestContext) : row.requestContext;
|
|
1812
|
+
} catch (e) {
|
|
1813
|
+
console.warn("Failed to parse requestContext:", e);
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
let metadataValue = null;
|
|
1817
|
+
if (row.metadata) {
|
|
1818
|
+
try {
|
|
1819
|
+
metadataValue = typeof row.metadata === "string" ? storage.safelyParseJSON(row.metadata) : row.metadata;
|
|
1820
|
+
} catch (e) {
|
|
1821
|
+
console.warn("Failed to parse metadata:", e);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
return {
|
|
1825
|
+
id: row.id,
|
|
1826
|
+
entityId: row.entityId,
|
|
1827
|
+
entityType: row.entityType,
|
|
1828
|
+
scorerId: row.scorerId,
|
|
1829
|
+
traceId: row.traceId,
|
|
1830
|
+
spanId: row.spanId,
|
|
1831
|
+
runId: row.runId,
|
|
1832
|
+
scorer: scorerValue,
|
|
1833
|
+
preprocessStepResult: preprocessStepResultValue,
|
|
1834
|
+
preprocessPrompt: row.preprocessPrompt,
|
|
1835
|
+
analyzeStepResult: analyzeStepResultValue,
|
|
1836
|
+
generateScorePrompt: row.generateScorePrompt,
|
|
1837
|
+
score: row.score,
|
|
1838
|
+
analyzePrompt: row.analyzePrompt,
|
|
1839
|
+
reasonPrompt: row.reasonPrompt,
|
|
1840
|
+
metadata: metadataValue,
|
|
1841
|
+
input: inputValue,
|
|
1842
|
+
output: outputValue,
|
|
1843
|
+
additionalContext: row.additionalContext,
|
|
1844
|
+
requestContext: requestContextValue,
|
|
1845
|
+
entity: entityValue,
|
|
1846
|
+
source: row.source,
|
|
1847
|
+
resourceId: row.resourceId,
|
|
1848
|
+
threadId: row.threadId,
|
|
1849
|
+
createdAt: new Date(row.createdAt),
|
|
1850
|
+
updatedAt: new Date(row.updatedAt)
|
|
1851
|
+
};
|
|
1852
|
+
}
|
|
1853
|
+
var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
1854
|
+
operations;
|
|
1855
|
+
constructor({ operations }) {
|
|
1856
|
+
super();
|
|
1857
|
+
this.operations = operations;
|
|
1858
|
+
}
|
|
1859
|
+
async getScoreById({ id }) {
|
|
1860
|
+
try {
|
|
1861
|
+
const collection = await this.operations.getCollection(storage.TABLE_SCORERS);
|
|
1862
|
+
const document = await collection.findOne({ id });
|
|
1863
|
+
if (!document) {
|
|
1864
|
+
return null;
|
|
1865
|
+
}
|
|
1866
|
+
return transformScoreRow(document);
|
|
1867
|
+
} catch (error$1) {
|
|
1868
|
+
throw new error.MastraError(
|
|
1869
|
+
{
|
|
1870
|
+
id: "STORAGE_MONGODB_STORE_GET_SCORE_BY_ID_FAILED",
|
|
1871
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1872
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1873
|
+
details: { id }
|
|
1874
|
+
},
|
|
1875
|
+
error$1
|
|
1876
|
+
);
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
async saveScore(score) {
|
|
1880
|
+
let validatedScore;
|
|
1881
|
+
try {
|
|
1882
|
+
validatedScore = evals.saveScorePayloadSchema.parse(score);
|
|
1883
|
+
} catch (error$1) {
|
|
1884
|
+
throw new error.MastraError(
|
|
1885
|
+
{
|
|
1886
|
+
id: "STORAGE_MONGODB_STORE_SAVE_SCORE_VALIDATION_FAILED",
|
|
1887
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1888
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1889
|
+
},
|
|
1890
|
+
error$1
|
|
1891
|
+
);
|
|
1892
|
+
}
|
|
1893
|
+
try {
|
|
1894
|
+
const now = /* @__PURE__ */ new Date();
|
|
1895
|
+
const scoreId = `score-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
1896
|
+
const scoreData = {
|
|
1897
|
+
id: scoreId,
|
|
1898
|
+
entityId: validatedScore.entityId,
|
|
1899
|
+
entityType: validatedScore.entityType,
|
|
1900
|
+
scorerId: validatedScore.scorerId,
|
|
1901
|
+
traceId: validatedScore.traceId || "",
|
|
1902
|
+
spanId: validatedScore.spanId || "",
|
|
1903
|
+
runId: validatedScore.runId,
|
|
1904
|
+
scorer: typeof validatedScore.scorer === "string" ? storage.safelyParseJSON(validatedScore.scorer) : validatedScore.scorer,
|
|
1905
|
+
preprocessStepResult: typeof validatedScore.preprocessStepResult === "string" ? storage.safelyParseJSON(validatedScore.preprocessStepResult) : validatedScore.preprocessStepResult,
|
|
1906
|
+
analyzeStepResult: typeof validatedScore.analyzeStepResult === "string" ? storage.safelyParseJSON(validatedScore.analyzeStepResult) : validatedScore.analyzeStepResult,
|
|
1907
|
+
score: validatedScore.score,
|
|
1908
|
+
reason: validatedScore.reason,
|
|
1909
|
+
preprocessPrompt: validatedScore.preprocessPrompt,
|
|
1910
|
+
generateScorePrompt: validatedScore.generateScorePrompt,
|
|
1911
|
+
generateReasonPrompt: validatedScore.generateReasonPrompt,
|
|
1912
|
+
analyzePrompt: validatedScore.analyzePrompt,
|
|
1913
|
+
input: typeof validatedScore.input === "string" ? storage.safelyParseJSON(validatedScore.input) : validatedScore.input,
|
|
1914
|
+
output: typeof validatedScore.output === "string" ? storage.safelyParseJSON(validatedScore.output) : validatedScore.output,
|
|
1915
|
+
additionalContext: validatedScore.additionalContext,
|
|
1916
|
+
requestContext: typeof validatedScore.requestContext === "string" ? storage.safelyParseJSON(validatedScore.requestContext) : validatedScore.requestContext,
|
|
1917
|
+
entity: typeof validatedScore.entity === "string" ? storage.safelyParseJSON(validatedScore.entity) : validatedScore.entity,
|
|
1918
|
+
source: validatedScore.source,
|
|
1919
|
+
resourceId: validatedScore.resourceId || "",
|
|
1920
|
+
threadId: validatedScore.threadId || "",
|
|
1921
|
+
createdAt: now,
|
|
1922
|
+
updatedAt: now
|
|
1923
|
+
};
|
|
1924
|
+
const collection = await this.operations.getCollection(storage.TABLE_SCORERS);
|
|
1925
|
+
await collection.insertOne(scoreData);
|
|
1926
|
+
const savedScore = {
|
|
1927
|
+
...score,
|
|
1928
|
+
id: scoreId,
|
|
1929
|
+
createdAt: now,
|
|
1930
|
+
updatedAt: now
|
|
1931
|
+
};
|
|
1932
|
+
return { score: savedScore };
|
|
1933
|
+
} catch (error$1) {
|
|
1934
|
+
throw new error.MastraError(
|
|
1935
|
+
{
|
|
1936
|
+
id: "STORAGE_MONGODB_STORE_SAVE_SCORE_FAILED",
|
|
1937
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1938
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1939
|
+
details: { scorerId: score.scorerId, runId: score.runId }
|
|
1940
|
+
},
|
|
1941
|
+
error$1
|
|
1942
|
+
);
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
async listScoresByScorerId({
|
|
1946
|
+
scorerId,
|
|
1947
|
+
pagination,
|
|
1948
|
+
entityId,
|
|
1949
|
+
entityType,
|
|
1950
|
+
source
|
|
1951
|
+
}) {
|
|
1952
|
+
try {
|
|
1953
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1954
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1955
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1956
|
+
const query = { scorerId };
|
|
1957
|
+
if (entityId) {
|
|
1958
|
+
query.entityId = entityId;
|
|
1959
|
+
}
|
|
1960
|
+
if (entityType) {
|
|
1961
|
+
query.entityType = entityType;
|
|
1962
|
+
}
|
|
1963
|
+
if (source) {
|
|
1964
|
+
query.source = source;
|
|
1965
|
+
}
|
|
1966
|
+
const collection = await this.operations.getCollection(storage.TABLE_SCORERS);
|
|
1967
|
+
const total = await collection.countDocuments(query);
|
|
1968
|
+
if (total === 0) {
|
|
1969
|
+
return {
|
|
1970
|
+
scores: [],
|
|
1971
|
+
pagination: {
|
|
1972
|
+
total: 0,
|
|
1973
|
+
page,
|
|
1974
|
+
perPage: perPageInput,
|
|
1975
|
+
hasMore: false
|
|
1976
|
+
}
|
|
1977
|
+
};
|
|
1978
|
+
}
|
|
1979
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1980
|
+
let cursor = collection.find(query).sort({ createdAt: "desc" }).skip(start);
|
|
1981
|
+
if (perPageInput !== false) {
|
|
1982
|
+
cursor = cursor.limit(perPage);
|
|
1983
|
+
}
|
|
1984
|
+
const documents = await cursor.toArray();
|
|
1985
|
+
const scores = documents.map((row) => transformScoreRow(row));
|
|
1986
|
+
return {
|
|
1987
|
+
scores,
|
|
1988
|
+
pagination: {
|
|
1989
|
+
total,
|
|
1990
|
+
page,
|
|
1991
|
+
perPage: perPageForResponse,
|
|
1992
|
+
hasMore: end < total
|
|
1993
|
+
}
|
|
1994
|
+
};
|
|
1995
|
+
} catch (error$1) {
|
|
1996
|
+
throw new error.MastraError(
|
|
1997
|
+
{
|
|
1998
|
+
id: "STORAGE_MONGODB_STORE_GET_SCORES_BY_SCORER_ID_FAILED",
|
|
1999
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2000
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2001
|
+
details: { scorerId, page: pagination.page, perPage: pagination.perPage }
|
|
2002
|
+
},
|
|
2003
|
+
error$1
|
|
2004
|
+
);
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
async listScoresByRunId({
|
|
2008
|
+
runId,
|
|
2009
|
+
pagination
|
|
2010
|
+
}) {
|
|
2011
|
+
try {
|
|
2012
|
+
const { page, perPage: perPageInput } = pagination;
|
|
2013
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
2014
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2015
|
+
const collection = await this.operations.getCollection(storage.TABLE_SCORERS);
|
|
2016
|
+
const total = await collection.countDocuments({ runId });
|
|
2017
|
+
if (total === 0) {
|
|
2018
|
+
return {
|
|
2019
|
+
scores: [],
|
|
2020
|
+
pagination: {
|
|
2021
|
+
total: 0,
|
|
2022
|
+
page,
|
|
2023
|
+
perPage: perPageInput,
|
|
2024
|
+
hasMore: false
|
|
2025
|
+
}
|
|
2026
|
+
};
|
|
2027
|
+
}
|
|
2028
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
2029
|
+
let cursor = collection.find({ runId }).sort({ createdAt: "desc" }).skip(start);
|
|
2030
|
+
if (perPageInput !== false) {
|
|
2031
|
+
cursor = cursor.limit(perPage);
|
|
2032
|
+
}
|
|
2033
|
+
const documents = await cursor.toArray();
|
|
2034
|
+
const scores = documents.map((row) => transformScoreRow(row));
|
|
2035
|
+
return {
|
|
2036
|
+
scores,
|
|
2037
|
+
pagination: {
|
|
2038
|
+
total,
|
|
2039
|
+
page,
|
|
2040
|
+
perPage: perPageForResponse,
|
|
2041
|
+
hasMore: end < total
|
|
2042
|
+
}
|
|
2043
|
+
};
|
|
2044
|
+
} catch (error$1) {
|
|
2045
|
+
throw new error.MastraError(
|
|
2046
|
+
{
|
|
2047
|
+
id: "STORAGE_MONGODB_STORE_GET_SCORES_BY_RUN_ID_FAILED",
|
|
2048
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2049
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2050
|
+
details: { runId, page: pagination.page, perPage: pagination.perPage }
|
|
2051
|
+
},
|
|
2052
|
+
error$1
|
|
2053
|
+
);
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
async listScoresByEntityId({
|
|
2057
|
+
entityId,
|
|
2058
|
+
entityType,
|
|
2059
|
+
pagination
|
|
2060
|
+
}) {
|
|
2061
|
+
try {
|
|
2062
|
+
const { page, perPage: perPageInput } = pagination;
|
|
2063
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
2064
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2065
|
+
const collection = await this.operations.getCollection(storage.TABLE_SCORERS);
|
|
2066
|
+
const total = await collection.countDocuments({ entityId, entityType });
|
|
2067
|
+
if (total === 0) {
|
|
2068
|
+
return {
|
|
2069
|
+
scores: [],
|
|
2070
|
+
pagination: {
|
|
2071
|
+
total: 0,
|
|
2072
|
+
page,
|
|
2073
|
+
perPage: perPageInput,
|
|
2074
|
+
hasMore: false
|
|
2075
|
+
}
|
|
2076
|
+
};
|
|
2077
|
+
}
|
|
2078
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
2079
|
+
let cursor = collection.find({ entityId, entityType }).sort({ createdAt: "desc" }).skip(start);
|
|
2080
|
+
if (perPageInput !== false) {
|
|
2081
|
+
cursor = cursor.limit(perPage);
|
|
2082
|
+
}
|
|
2083
|
+
const documents = await cursor.toArray();
|
|
2084
|
+
const scores = documents.map((row) => transformScoreRow(row));
|
|
2085
|
+
return {
|
|
2086
|
+
scores,
|
|
2087
|
+
pagination: {
|
|
2088
|
+
total,
|
|
2089
|
+
page,
|
|
2090
|
+
perPage: perPageForResponse,
|
|
2091
|
+
hasMore: end < total
|
|
2092
|
+
}
|
|
2093
|
+
};
|
|
2094
|
+
} catch (error$1) {
|
|
2095
|
+
throw new error.MastraError(
|
|
2096
|
+
{
|
|
2097
|
+
id: "STORAGE_MONGODB_STORE_GET_SCORES_BY_ENTITY_ID_FAILED",
|
|
2098
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2099
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2100
|
+
details: { entityId, entityType, page: pagination.page, perPage: pagination.perPage }
|
|
2101
|
+
},
|
|
2102
|
+
error$1
|
|
2103
|
+
);
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
async listScoresBySpan({
|
|
2107
|
+
traceId,
|
|
2108
|
+
spanId,
|
|
2109
|
+
pagination
|
|
2110
|
+
}) {
|
|
2111
|
+
try {
|
|
2112
|
+
const { page, perPage: perPageInput } = pagination;
|
|
2113
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
2114
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2115
|
+
const query = { traceId, spanId };
|
|
2116
|
+
const collection = await this.operations.getCollection(storage.TABLE_SCORERS);
|
|
2117
|
+
const total = await collection.countDocuments(query);
|
|
2118
|
+
if (total === 0) {
|
|
2119
|
+
return {
|
|
2120
|
+
scores: [],
|
|
2121
|
+
pagination: {
|
|
2122
|
+
total: 0,
|
|
2123
|
+
page,
|
|
2124
|
+
perPage: perPageInput,
|
|
2125
|
+
hasMore: false
|
|
2126
|
+
}
|
|
2127
|
+
};
|
|
2128
|
+
}
|
|
2129
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
2130
|
+
let cursor = collection.find(query).sort({ createdAt: "desc" }).skip(start);
|
|
2131
|
+
if (perPageInput !== false) {
|
|
2132
|
+
cursor = cursor.limit(perPage);
|
|
2133
|
+
}
|
|
2134
|
+
const documents = await cursor.toArray();
|
|
2135
|
+
const scores = documents.map((row) => transformScoreRow(row));
|
|
2136
|
+
return {
|
|
2137
|
+
scores,
|
|
2138
|
+
pagination: {
|
|
2139
|
+
total,
|
|
2140
|
+
page,
|
|
2141
|
+
perPage: perPageForResponse,
|
|
2142
|
+
hasMore: end < total
|
|
2143
|
+
}
|
|
2144
|
+
};
|
|
2145
|
+
} catch (error$1) {
|
|
2146
|
+
throw new error.MastraError(
|
|
2147
|
+
{
|
|
2148
|
+
id: "STORAGE_MONGODB_STORE_GET_SCORES_BY_SPAN_FAILED",
|
|
2149
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2150
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2151
|
+
details: { traceId, spanId, page: pagination.page, perPage: pagination.perPage }
|
|
2152
|
+
},
|
|
2153
|
+
error$1
|
|
2154
|
+
);
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
};
|
|
2158
|
+
var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
2159
|
+
operations;
|
|
2160
|
+
constructor({ operations }) {
|
|
2161
|
+
super();
|
|
2162
|
+
this.operations = operations;
|
|
2163
|
+
}
|
|
2164
|
+
updateWorkflowResults({
|
|
2165
|
+
// workflowName,
|
|
2166
|
+
// runId,
|
|
2167
|
+
// stepId,
|
|
2168
|
+
// result,
|
|
2169
|
+
// requestContext,
|
|
2170
|
+
}) {
|
|
2171
|
+
throw new Error("Method not implemented.");
|
|
2172
|
+
}
|
|
2173
|
+
updateWorkflowState({
|
|
2174
|
+
// workflowName,
|
|
2175
|
+
// runId,
|
|
2176
|
+
// opts,
|
|
2177
|
+
}) {
|
|
2178
|
+
throw new Error("Method not implemented.");
|
|
2179
|
+
}
|
|
2180
|
+
async persistWorkflowSnapshot({
|
|
2181
|
+
workflowName,
|
|
2182
|
+
runId,
|
|
2183
|
+
resourceId,
|
|
2184
|
+
snapshot
|
|
2185
|
+
}) {
|
|
2186
|
+
try {
|
|
2187
|
+
const collection = await this.operations.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2188
|
+
await collection.updateOne(
|
|
2189
|
+
{ workflow_name: workflowName, run_id: runId },
|
|
2190
|
+
{
|
|
2191
|
+
$set: {
|
|
2192
|
+
workflow_name: workflowName,
|
|
2193
|
+
run_id: runId,
|
|
2194
|
+
resourceId,
|
|
2195
|
+
snapshot,
|
|
2196
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
2197
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
2198
|
+
}
|
|
2199
|
+
},
|
|
2200
|
+
{ upsert: true }
|
|
2201
|
+
);
|
|
2202
|
+
} catch (error$1) {
|
|
2203
|
+
throw new error.MastraError(
|
|
2204
|
+
{
|
|
2205
|
+
id: "STORAGE_MONGODB_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
|
|
2206
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2207
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2208
|
+
details: { workflowName, runId }
|
|
2209
|
+
},
|
|
2210
|
+
error$1
|
|
2211
|
+
);
|
|
2212
|
+
}
|
|
2213
|
+
}
|
|
2214
|
+
async loadWorkflowSnapshot({
|
|
2215
|
+
workflowName,
|
|
2216
|
+
runId
|
|
2217
|
+
}) {
|
|
2218
|
+
try {
|
|
2219
|
+
const result = await this.operations.load({
|
|
2220
|
+
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
2221
|
+
keys: {
|
|
2222
|
+
workflow_name: workflowName,
|
|
2223
|
+
run_id: runId
|
|
2224
|
+
}
|
|
2225
|
+
});
|
|
2226
|
+
if (!result?.length) {
|
|
2227
|
+
return null;
|
|
2228
|
+
}
|
|
2229
|
+
return typeof result[0].snapshot === "string" ? storage.safelyParseJSON(result[0].snapshot) : result[0].snapshot;
|
|
2230
|
+
} catch (error$1) {
|
|
2231
|
+
throw new error.MastraError(
|
|
2232
|
+
{
|
|
2233
|
+
id: "STORAGE_MONGODB_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
|
|
2234
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2235
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2236
|
+
details: { workflowName, runId }
|
|
2237
|
+
},
|
|
2238
|
+
error$1
|
|
2239
|
+
);
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
async listWorkflowRuns(args) {
|
|
2243
|
+
const options = args || {};
|
|
2244
|
+
try {
|
|
2245
|
+
const query = {};
|
|
2246
|
+
if (options.workflowName) {
|
|
2247
|
+
query["workflow_name"] = options.workflowName;
|
|
2248
|
+
}
|
|
2249
|
+
if (options.status) {
|
|
2250
|
+
query["snapshot.status"] = options.status;
|
|
2251
|
+
}
|
|
2252
|
+
if (options.fromDate) {
|
|
2253
|
+
query["createdAt"] = { $gte: options.fromDate };
|
|
2254
|
+
}
|
|
2255
|
+
if (options.toDate) {
|
|
2256
|
+
if (query["createdAt"]) {
|
|
2257
|
+
query["createdAt"].$lte = options.toDate;
|
|
2258
|
+
} else {
|
|
2259
|
+
query["createdAt"] = { $lte: options.toDate };
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
if (options.resourceId) {
|
|
2263
|
+
query["resourceId"] = options.resourceId;
|
|
2264
|
+
}
|
|
2265
|
+
const collection = await this.operations.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2266
|
+
let total = 0;
|
|
2267
|
+
let cursor = collection.find(query).sort({ createdAt: -1 });
|
|
2268
|
+
if (options.page !== void 0 && typeof options.perPage === "number") {
|
|
2269
|
+
if (options.page < 0) {
|
|
2270
|
+
throw new error.MastraError(
|
|
2271
|
+
{
|
|
2272
|
+
id: "STORAGE_MONGODB_INVALID_PAGE",
|
|
2273
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2274
|
+
category: error.ErrorCategory.USER,
|
|
2275
|
+
details: { page: options.page }
|
|
2276
|
+
},
|
|
2277
|
+
new Error("page must be >= 0")
|
|
2278
|
+
);
|
|
2279
|
+
}
|
|
2280
|
+
total = await collection.countDocuments(query);
|
|
2281
|
+
const normalizedPerPage = storage.normalizePerPage(options.perPage, Number.MAX_SAFE_INTEGER);
|
|
2282
|
+
if (normalizedPerPage === 0) {
|
|
2283
|
+
return { runs: [], total };
|
|
2284
|
+
}
|
|
2285
|
+
const offset = options.page * normalizedPerPage;
|
|
2286
|
+
cursor = cursor.skip(offset);
|
|
2287
|
+
cursor = cursor.limit(Math.min(normalizedPerPage, 2147483647));
|
|
2288
|
+
}
|
|
2289
|
+
const results = await cursor.toArray();
|
|
2290
|
+
const runs = results.map((row) => this.parseWorkflowRun(row));
|
|
2291
|
+
return {
|
|
2292
|
+
runs,
|
|
2293
|
+
total: total || runs.length
|
|
2294
|
+
};
|
|
2295
|
+
} catch (error$1) {
|
|
2296
|
+
throw new error.MastraError(
|
|
2297
|
+
{
|
|
2298
|
+
id: "STORAGE_MONGODB_STORE_LIST_WORKFLOW_RUNS_FAILED",
|
|
2299
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2300
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2301
|
+
details: { workflowName: options.workflowName || "unknown" }
|
|
2302
|
+
},
|
|
2303
|
+
error$1
|
|
2304
|
+
);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
async getWorkflowRunById(args) {
|
|
2308
|
+
try {
|
|
2309
|
+
const query = {};
|
|
2310
|
+
if (args.runId) {
|
|
2311
|
+
query["run_id"] = args.runId;
|
|
2312
|
+
}
|
|
2313
|
+
if (args.workflowName) {
|
|
2314
|
+
query["workflow_name"] = args.workflowName;
|
|
2315
|
+
}
|
|
2316
|
+
const collection = await this.operations.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2317
|
+
const result = await collection.findOne(query);
|
|
2318
|
+
if (!result) {
|
|
2319
|
+
return null;
|
|
2320
|
+
}
|
|
2321
|
+
return this.parseWorkflowRun(result);
|
|
2322
|
+
} catch (error$1) {
|
|
2323
|
+
throw new error.MastraError(
|
|
2324
|
+
{
|
|
2325
|
+
id: "STORAGE_MONGODB_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED",
|
|
2326
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2327
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2328
|
+
details: { runId: args.runId }
|
|
2329
|
+
},
|
|
2330
|
+
error$1
|
|
2331
|
+
);
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
parseWorkflowRun(row) {
|
|
2335
|
+
let parsedSnapshot = row.snapshot;
|
|
2336
|
+
if (typeof parsedSnapshot === "string") {
|
|
2337
|
+
try {
|
|
2338
|
+
parsedSnapshot = typeof row.snapshot === "string" ? storage.safelyParseJSON(row.snapshot) : row.snapshot;
|
|
2339
|
+
} catch (e) {
|
|
2340
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
return {
|
|
2344
|
+
workflowName: row.workflow_name,
|
|
2345
|
+
runId: row.run_id,
|
|
2346
|
+
snapshot: parsedSnapshot,
|
|
2347
|
+
createdAt: new Date(row.createdAt),
|
|
2348
|
+
updatedAt: new Date(row.updatedAt),
|
|
2349
|
+
resourceId: row.resourceId
|
|
2350
|
+
};
|
|
2351
|
+
}
|
|
2352
|
+
};
|
|
2353
|
+
|
|
2354
|
+
// src/storage/index.ts
|
|
2355
|
+
var loadConnector = (config) => {
|
|
2356
|
+
try {
|
|
2357
|
+
if ("connectorHandler" in config) {
|
|
2358
|
+
return MongoDBConnector.fromConnectionHandler(config.connectorHandler);
|
|
2359
|
+
}
|
|
2360
|
+
} catch (error$1) {
|
|
2361
|
+
throw new error.MastraError(
|
|
2362
|
+
{
|
|
2363
|
+
id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
|
|
2364
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2365
|
+
category: error.ErrorCategory.USER,
|
|
2366
|
+
details: { connectionHandler: true }
|
|
2367
|
+
},
|
|
2368
|
+
error$1
|
|
2369
|
+
);
|
|
2370
|
+
}
|
|
2371
|
+
try {
|
|
2372
|
+
return MongoDBConnector.fromDatabaseConfig({
|
|
2373
|
+
id: config.id,
|
|
2374
|
+
options: config.options,
|
|
2375
|
+
url: config.url,
|
|
2376
|
+
dbName: config.dbName
|
|
2377
|
+
});
|
|
2378
|
+
} catch (error$1) {
|
|
2379
|
+
throw new error.MastraError(
|
|
2380
|
+
{
|
|
2381
|
+
id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
|
|
2382
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2383
|
+
category: error.ErrorCategory.USER,
|
|
2384
|
+
details: { url: config?.url, dbName: config?.dbName }
|
|
2385
|
+
},
|
|
2386
|
+
error$1
|
|
2387
|
+
);
|
|
2388
|
+
}
|
|
2389
|
+
};
|
|
2390
|
+
var MongoDBStore = class extends storage.MastraStorage {
|
|
2391
|
+
#connector;
|
|
2392
|
+
stores;
|
|
2393
|
+
get supports() {
|
|
2394
|
+
return {
|
|
2395
|
+
selectByIncludeResourceScope: true,
|
|
2396
|
+
resourceWorkingMemory: true,
|
|
2397
|
+
hasColumn: false,
|
|
2398
|
+
createTable: false,
|
|
2399
|
+
deleteMessages: false,
|
|
2400
|
+
listScoresBySpan: true
|
|
2401
|
+
};
|
|
2402
|
+
}
|
|
2403
|
+
constructor(config) {
|
|
2404
|
+
super({ id: config.id, name: "MongoDBStore" });
|
|
2405
|
+
this.stores = {};
|
|
2406
|
+
this.#connector = loadConnector(config);
|
|
2407
|
+
const operations = new StoreOperationsMongoDB({
|
|
2408
|
+
connector: this.#connector
|
|
2409
|
+
});
|
|
2410
|
+
const memory = new MemoryStorageMongoDB({
|
|
2411
|
+
operations
|
|
2412
|
+
});
|
|
2413
|
+
const scores = new ScoresStorageMongoDB({
|
|
2414
|
+
operations
|
|
2415
|
+
});
|
|
2416
|
+
const workflows = new WorkflowsStorageMongoDB({
|
|
2417
|
+
operations
|
|
2418
|
+
});
|
|
2419
|
+
const observability = new ObservabilityMongoDB({
|
|
2420
|
+
operations
|
|
2421
|
+
});
|
|
2422
|
+
this.stores = {
|
|
2423
|
+
operations,
|
|
2424
|
+
memory,
|
|
2425
|
+
scores,
|
|
2426
|
+
workflows,
|
|
2427
|
+
observability
|
|
2428
|
+
};
|
|
2429
|
+
}
|
|
2430
|
+
async createTable({
|
|
2431
|
+
tableName,
|
|
2432
|
+
schema
|
|
2433
|
+
}) {
|
|
2434
|
+
return this.stores.operations.createTable({ tableName, schema });
|
|
2435
|
+
}
|
|
2436
|
+
async alterTable(_args) {
|
|
2437
|
+
return this.stores.operations.alterTable(_args);
|
|
2438
|
+
}
|
|
2439
|
+
async dropTable({ tableName }) {
|
|
2440
|
+
return this.stores.operations.dropTable({ tableName });
|
|
2441
|
+
}
|
|
2442
|
+
async clearTable({ tableName }) {
|
|
2443
|
+
return this.stores.operations.clearTable({ tableName });
|
|
2444
|
+
}
|
|
2445
|
+
async insert({ tableName, record }) {
|
|
2446
|
+
return this.stores.operations.insert({ tableName, record });
|
|
2447
|
+
}
|
|
2448
|
+
async batchInsert({ tableName, records }) {
|
|
2449
|
+
return this.stores.operations.batchInsert({ tableName, records });
|
|
2450
|
+
}
|
|
2451
|
+
async load({ tableName, keys }) {
|
|
2452
|
+
return this.stores.operations.load({ tableName, keys });
|
|
2453
|
+
}
|
|
2454
|
+
async getThreadById({ threadId }) {
|
|
2455
|
+
return this.stores.memory.getThreadById({ threadId });
|
|
2456
|
+
}
|
|
2457
|
+
async saveThread({ thread }) {
|
|
2458
|
+
return this.stores.memory.saveThread({ thread });
|
|
2459
|
+
}
|
|
2460
|
+
async updateThread({
|
|
2461
|
+
id,
|
|
2462
|
+
title,
|
|
2463
|
+
metadata
|
|
2464
|
+
}) {
|
|
2465
|
+
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2466
|
+
}
|
|
2467
|
+
async deleteThread({ threadId }) {
|
|
2468
|
+
return this.stores.memory.deleteThread({ threadId });
|
|
2469
|
+
}
|
|
2470
|
+
async listMessagesById({ messageIds }) {
|
|
2471
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2472
|
+
}
|
|
2473
|
+
async saveMessages(args) {
|
|
2474
|
+
return this.stores.memory.saveMessages(args);
|
|
2475
|
+
}
|
|
2476
|
+
async updateMessages(_args) {
|
|
2477
|
+
return this.stores.memory.updateMessages(_args);
|
|
2478
|
+
}
|
|
2479
|
+
async listWorkflowRuns(args) {
|
|
2480
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2481
|
+
}
|
|
2482
|
+
async updateWorkflowResults({
|
|
2483
|
+
workflowName,
|
|
2484
|
+
runId,
|
|
2485
|
+
stepId,
|
|
2486
|
+
result,
|
|
2487
|
+
requestContext
|
|
2488
|
+
}) {
|
|
2489
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2490
|
+
}
|
|
2491
|
+
async updateWorkflowState({
|
|
2492
|
+
workflowName,
|
|
2493
|
+
runId,
|
|
2494
|
+
opts
|
|
2495
|
+
}) {
|
|
2496
|
+
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2497
|
+
}
|
|
2498
|
+
async persistWorkflowSnapshot({
|
|
2499
|
+
workflowName,
|
|
2500
|
+
runId,
|
|
2501
|
+
resourceId,
|
|
2502
|
+
snapshot
|
|
2503
|
+
}) {
|
|
2504
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2505
|
+
}
|
|
2506
|
+
async loadWorkflowSnapshot({
|
|
2507
|
+
workflowName,
|
|
2508
|
+
runId
|
|
2509
|
+
}) {
|
|
2510
|
+
return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
2511
|
+
}
|
|
2512
|
+
async getWorkflowRunById({
|
|
2513
|
+
runId,
|
|
2514
|
+
workflowName
|
|
2515
|
+
}) {
|
|
2516
|
+
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2517
|
+
}
|
|
2518
|
+
async close() {
|
|
2519
|
+
try {
|
|
2520
|
+
await this.#connector.close();
|
|
2521
|
+
} catch (error$1) {
|
|
2522
|
+
throw new error.MastraError(
|
|
2523
|
+
{
|
|
2524
|
+
id: "STORAGE_MONGODB_STORE_CLOSE_FAILED",
|
|
2525
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2526
|
+
category: error.ErrorCategory.USER
|
|
2527
|
+
},
|
|
2528
|
+
error$1
|
|
2529
|
+
);
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
/**
|
|
2533
|
+
* SCORERS
|
|
2534
|
+
*/
|
|
2535
|
+
async getScoreById({ id }) {
|
|
2536
|
+
return this.stores.scores.getScoreById({ id });
|
|
2537
|
+
}
|
|
2538
|
+
async saveScore(score) {
|
|
2539
|
+
return this.stores.scores.saveScore(score);
|
|
2540
|
+
}
|
|
2541
|
+
async listScoresByRunId({
|
|
2542
|
+
runId,
|
|
2543
|
+
pagination
|
|
2544
|
+
}) {
|
|
2545
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2546
|
+
}
|
|
2547
|
+
async listScoresByEntityId({
|
|
2548
|
+
entityId,
|
|
2549
|
+
entityType,
|
|
2550
|
+
pagination
|
|
2551
|
+
}) {
|
|
2552
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2553
|
+
}
|
|
2554
|
+
async listScoresByScorerId({
|
|
2555
|
+
scorerId,
|
|
2556
|
+
pagination,
|
|
2557
|
+
entityId,
|
|
2558
|
+
entityType,
|
|
2559
|
+
source
|
|
2560
|
+
}) {
|
|
2561
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2562
|
+
}
|
|
2563
|
+
async listScoresBySpan({
|
|
2564
|
+
traceId,
|
|
2565
|
+
spanId,
|
|
2566
|
+
pagination
|
|
2567
|
+
}) {
|
|
2568
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2569
|
+
}
|
|
2570
|
+
/**
|
|
2571
|
+
* RESOURCES
|
|
2572
|
+
*/
|
|
2573
|
+
async getResourceById({ resourceId }) {
|
|
2574
|
+
return this.stores.memory.getResourceById({ resourceId });
|
|
2575
|
+
}
|
|
2576
|
+
async saveResource({ resource }) {
|
|
2577
|
+
return this.stores.memory.saveResource({ resource });
|
|
2578
|
+
}
|
|
2579
|
+
async updateResource({
|
|
2580
|
+
resourceId,
|
|
2581
|
+
workingMemory,
|
|
2582
|
+
metadata
|
|
2583
|
+
}) {
|
|
2584
|
+
return this.stores.memory.updateResource({
|
|
2585
|
+
resourceId,
|
|
2586
|
+
workingMemory,
|
|
2587
|
+
metadata
|
|
2588
|
+
});
|
|
2589
|
+
}
|
|
2590
|
+
/**
|
|
2591
|
+
* Tracing/Observability
|
|
2592
|
+
*/
|
|
2593
|
+
async createSpan(span) {
|
|
2594
|
+
if (!this.stores.observability) {
|
|
2595
|
+
throw new error.MastraError({
|
|
2596
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2597
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2598
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2599
|
+
text: "Observability storage is not initialized"
|
|
2600
|
+
});
|
|
2601
|
+
}
|
|
2602
|
+
return this.stores.observability.createSpan(span);
|
|
2603
|
+
}
|
|
2604
|
+
async updateSpan({
|
|
2605
|
+
spanId,
|
|
2606
|
+
traceId,
|
|
2607
|
+
updates
|
|
2608
|
+
}) {
|
|
2609
|
+
if (!this.stores.observability) {
|
|
2610
|
+
throw new error.MastraError({
|
|
2611
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2612
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2613
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2614
|
+
text: "Observability storage is not initialized"
|
|
2615
|
+
});
|
|
2616
|
+
}
|
|
2617
|
+
return this.stores.observability.updateSpan({ spanId, traceId, updates });
|
|
2618
|
+
}
|
|
2619
|
+
async getTrace(traceId) {
|
|
2620
|
+
if (!this.stores.observability) {
|
|
2621
|
+
throw new error.MastraError({
|
|
2622
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2623
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2624
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2625
|
+
text: "Observability storage is not initialized"
|
|
2626
|
+
});
|
|
2627
|
+
}
|
|
2628
|
+
return this.stores.observability.getTrace(traceId);
|
|
2629
|
+
}
|
|
2630
|
+
async getTracesPaginated(args) {
|
|
2631
|
+
if (!this.stores.observability) {
|
|
2632
|
+
throw new error.MastraError({
|
|
2633
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2634
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2635
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2636
|
+
text: "Observability storage is not initialized"
|
|
2637
|
+
});
|
|
2638
|
+
}
|
|
2639
|
+
return this.stores.observability.getTracesPaginated(args);
|
|
2640
|
+
}
|
|
2641
|
+
async batchCreateSpans(args) {
|
|
2642
|
+
if (!this.stores.observability) {
|
|
2643
|
+
throw new error.MastraError({
|
|
2644
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2645
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2646
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2647
|
+
text: "Observability storage is not initialized"
|
|
2648
|
+
});
|
|
2649
|
+
}
|
|
2650
|
+
return this.stores.observability.batchCreateSpans(args);
|
|
2651
|
+
}
|
|
2652
|
+
async batchUpdateSpans(args) {
|
|
2653
|
+
if (!this.stores.observability) {
|
|
2654
|
+
throw new error.MastraError({
|
|
2655
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2656
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2657
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2658
|
+
text: "Observability storage is not initialized"
|
|
2659
|
+
});
|
|
2660
|
+
}
|
|
2661
|
+
return this.stores.observability.batchUpdateSpans(args);
|
|
2662
|
+
}
|
|
2663
|
+
async batchDeleteTraces(args) {
|
|
2664
|
+
if (!this.stores.observability) {
|
|
2665
|
+
throw new error.MastraError({
|
|
2666
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2667
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2668
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2669
|
+
text: "Observability storage is not initialized"
|
|
2670
|
+
});
|
|
2671
|
+
}
|
|
2672
|
+
return this.stores.observability.batchDeleteTraces(args);
|
|
2673
|
+
}
|
|
359
2674
|
};
|
|
360
2675
|
|
|
361
2676
|
// src/vector/prompt.ts
|
|
@@ -454,4 +2769,7 @@ Example Complex Query:
|
|
|
454
2769
|
}`;
|
|
455
2770
|
|
|
456
2771
|
exports.MONGODB_PROMPT = MONGODB_PROMPT;
|
|
2772
|
+
exports.MongoDBStore = MongoDBStore;
|
|
457
2773
|
exports.MongoDBVector = MongoDBVector;
|
|
2774
|
+
//# sourceMappingURL=index.cjs.map
|
|
2775
|
+
//# sourceMappingURL=index.cjs.map
|