@mastra/mongodb 0.14.6 → 0.14.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/index.cjs +479 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +480 -36
- package/dist/index.js.map +1 -1
- package/dist/storage/connectors/MongoDBConnector.d.ts.map +1 -1
- 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 +12 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +41 -11
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +13 -6
- package/dist/storage/MongoDBConnector.d.ts +0 -23
- package/dist/storage/MongoDBConnector.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -3,11 +3,15 @@ import { MastraVector } from '@mastra/core/vector';
|
|
|
3
3
|
import { MongoClient } from 'mongodb';
|
|
4
4
|
import { v4 } from 'uuid';
|
|
5
5
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
6
|
-
import { MastraStorage, StoreOperations, TABLE_SCHEMAS, safelyParseJSON, MemoryStorage, TABLE_MESSAGES, resolveMessageLimit, TABLE_THREADS, TABLE_RESOURCES, TracesStorage, TABLE_TRACES, LegacyEvalsStorage, TABLE_EVALS, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';
|
|
6
|
+
import { MastraStorage, StoreOperations, TABLE_SCHEMAS, safelyParseJSON, MemoryStorage, TABLE_MESSAGES, resolveMessageLimit, TABLE_THREADS, TABLE_RESOURCES, TracesStorage, TABLE_TRACES, LegacyEvalsStorage, TABLE_EVALS, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ObservabilityStorage, TABLE_AI_SPANS } from '@mastra/core/storage';
|
|
7
7
|
import { MessageList } from '@mastra/core/agent';
|
|
8
8
|
import { saveScorePayloadSchema } from '@mastra/core/scores';
|
|
9
9
|
|
|
10
10
|
// src/vector/index.ts
|
|
11
|
+
|
|
12
|
+
// package.json
|
|
13
|
+
var package_default = {
|
|
14
|
+
version: "0.14.7"};
|
|
11
15
|
var MongoDBFilterTranslator = class extends BaseFilterTranslator {
|
|
12
16
|
getSupportedOperators() {
|
|
13
17
|
return {
|
|
@@ -112,7 +116,14 @@ var MongoDBVector = class extends MastraVector {
|
|
|
112
116
|
};
|
|
113
117
|
constructor({ uri, dbName, options }) {
|
|
114
118
|
super();
|
|
115
|
-
|
|
119
|
+
const client = new MongoClient(uri, {
|
|
120
|
+
...options,
|
|
121
|
+
driverInfo: {
|
|
122
|
+
name: "mastra-vector",
|
|
123
|
+
version: package_default.version
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
this.client = client;
|
|
116
127
|
this.db = this.client.db(dbName);
|
|
117
128
|
this.collections = /* @__PURE__ */ new Map();
|
|
118
129
|
}
|
|
@@ -632,8 +643,15 @@ var MongoDBConnector = class _MongoDBConnector {
|
|
|
632
643
|
"MongoDBStore: dbName must be provided and cannot be empty. Passing an empty string may cause fallback to local MongoDB defaults."
|
|
633
644
|
);
|
|
634
645
|
}
|
|
646
|
+
const client = new MongoClient(config.url, {
|
|
647
|
+
...config.options,
|
|
648
|
+
driverInfo: {
|
|
649
|
+
name: "mastra-storage",
|
|
650
|
+
version: package_default.version
|
|
651
|
+
}
|
|
652
|
+
});
|
|
635
653
|
return new _MongoDBConnector({
|
|
636
|
-
client
|
|
654
|
+
client,
|
|
637
655
|
dbName: config.dbName,
|
|
638
656
|
handler: void 0
|
|
639
657
|
});
|
|
@@ -1426,6 +1444,286 @@ var MemoryStorageMongoDB = class extends MemoryStorage {
|
|
|
1426
1444
|
}
|
|
1427
1445
|
}
|
|
1428
1446
|
};
|
|
1447
|
+
var ObservabilityMongoDB = class extends ObservabilityStorage {
|
|
1448
|
+
operations;
|
|
1449
|
+
constructor({ operations }) {
|
|
1450
|
+
super();
|
|
1451
|
+
this.operations = operations;
|
|
1452
|
+
}
|
|
1453
|
+
get aiTracingStrategy() {
|
|
1454
|
+
return {
|
|
1455
|
+
preferred: "batch-with-updates",
|
|
1456
|
+
supported: ["batch-with-updates", "insert-only"]
|
|
1457
|
+
};
|
|
1458
|
+
}
|
|
1459
|
+
async createAISpan(span) {
|
|
1460
|
+
try {
|
|
1461
|
+
const startedAt = span.startedAt instanceof Date ? span.startedAt.toISOString() : span.startedAt;
|
|
1462
|
+
const endedAt = span.endedAt instanceof Date ? span.endedAt.toISOString() : span.endedAt;
|
|
1463
|
+
const record = {
|
|
1464
|
+
...span,
|
|
1465
|
+
startedAt,
|
|
1466
|
+
endedAt,
|
|
1467
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1468
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1469
|
+
};
|
|
1470
|
+
return this.operations.insert({ tableName: TABLE_AI_SPANS, record });
|
|
1471
|
+
} catch (error) {
|
|
1472
|
+
throw new MastraError(
|
|
1473
|
+
{
|
|
1474
|
+
id: "MONGODB_STORE_CREATE_AI_SPAN_FAILED",
|
|
1475
|
+
domain: ErrorDomain.STORAGE,
|
|
1476
|
+
category: ErrorCategory.USER,
|
|
1477
|
+
details: {
|
|
1478
|
+
spanId: span.spanId,
|
|
1479
|
+
traceId: span.traceId,
|
|
1480
|
+
spanType: span.spanType,
|
|
1481
|
+
spanName: span.name
|
|
1482
|
+
}
|
|
1483
|
+
},
|
|
1484
|
+
error
|
|
1485
|
+
);
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
async getAITrace(traceId) {
|
|
1489
|
+
try {
|
|
1490
|
+
const collection = await this.operations.getCollection(TABLE_AI_SPANS);
|
|
1491
|
+
const spans = await collection.find({ traceId }).sort({ startedAt: -1 }).toArray();
|
|
1492
|
+
if (!spans || spans.length === 0) {
|
|
1493
|
+
return null;
|
|
1494
|
+
}
|
|
1495
|
+
return {
|
|
1496
|
+
traceId,
|
|
1497
|
+
spans: spans.map((span) => this.transformSpanFromMongo(span))
|
|
1498
|
+
};
|
|
1499
|
+
} catch (error) {
|
|
1500
|
+
throw new MastraError(
|
|
1501
|
+
{
|
|
1502
|
+
id: "MONGODB_STORE_GET_AI_TRACE_FAILED",
|
|
1503
|
+
domain: ErrorDomain.STORAGE,
|
|
1504
|
+
category: ErrorCategory.USER,
|
|
1505
|
+
details: {
|
|
1506
|
+
traceId
|
|
1507
|
+
}
|
|
1508
|
+
},
|
|
1509
|
+
error
|
|
1510
|
+
);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
async updateAISpan({
|
|
1514
|
+
spanId,
|
|
1515
|
+
traceId,
|
|
1516
|
+
updates
|
|
1517
|
+
}) {
|
|
1518
|
+
try {
|
|
1519
|
+
const data = { ...updates };
|
|
1520
|
+
if (data.endedAt instanceof Date) {
|
|
1521
|
+
data.endedAt = data.endedAt.toISOString();
|
|
1522
|
+
}
|
|
1523
|
+
if (data.startedAt instanceof Date) {
|
|
1524
|
+
data.startedAt = data.startedAt.toISOString();
|
|
1525
|
+
}
|
|
1526
|
+
const updateData = {
|
|
1527
|
+
...data,
|
|
1528
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1529
|
+
};
|
|
1530
|
+
await this.operations.update({
|
|
1531
|
+
tableName: TABLE_AI_SPANS,
|
|
1532
|
+
keys: { spanId, traceId },
|
|
1533
|
+
data: updateData
|
|
1534
|
+
});
|
|
1535
|
+
} catch (error) {
|
|
1536
|
+
throw new MastraError(
|
|
1537
|
+
{
|
|
1538
|
+
id: "MONGODB_STORE_UPDATE_AI_SPAN_FAILED",
|
|
1539
|
+
domain: ErrorDomain.STORAGE,
|
|
1540
|
+
category: ErrorCategory.USER,
|
|
1541
|
+
details: {
|
|
1542
|
+
spanId,
|
|
1543
|
+
traceId
|
|
1544
|
+
}
|
|
1545
|
+
},
|
|
1546
|
+
error
|
|
1547
|
+
);
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
async getAITracesPaginated({
|
|
1551
|
+
filters,
|
|
1552
|
+
pagination
|
|
1553
|
+
}) {
|
|
1554
|
+
const page = pagination?.page ?? 0;
|
|
1555
|
+
const perPage = pagination?.perPage ?? 10;
|
|
1556
|
+
const { entityId, entityType, ...actualFilters } = filters || {};
|
|
1557
|
+
try {
|
|
1558
|
+
const collection = await this.operations.getCollection(TABLE_AI_SPANS);
|
|
1559
|
+
const mongoFilter = {
|
|
1560
|
+
parentSpanId: null,
|
|
1561
|
+
// Only get root spans for traces
|
|
1562
|
+
...actualFilters
|
|
1563
|
+
};
|
|
1564
|
+
if (pagination?.dateRange) {
|
|
1565
|
+
const dateFilter = {};
|
|
1566
|
+
if (pagination.dateRange.start) {
|
|
1567
|
+
dateFilter.$gte = pagination.dateRange.start instanceof Date ? pagination.dateRange.start.toISOString() : pagination.dateRange.start;
|
|
1568
|
+
}
|
|
1569
|
+
if (pagination.dateRange.end) {
|
|
1570
|
+
dateFilter.$lte = pagination.dateRange.end instanceof Date ? pagination.dateRange.end.toISOString() : pagination.dateRange.end;
|
|
1571
|
+
}
|
|
1572
|
+
if (Object.keys(dateFilter).length > 0) {
|
|
1573
|
+
mongoFilter.startedAt = dateFilter;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
if (entityId && entityType) {
|
|
1577
|
+
let name = "";
|
|
1578
|
+
if (entityType === "workflow") {
|
|
1579
|
+
name = `workflow run: '${entityId}'`;
|
|
1580
|
+
} else if (entityType === "agent") {
|
|
1581
|
+
name = `agent run: '${entityId}'`;
|
|
1582
|
+
} else {
|
|
1583
|
+
const error = new MastraError({
|
|
1584
|
+
id: "MONGODB_STORE_GET_AI_TRACES_PAGINATED_FAILED",
|
|
1585
|
+
domain: ErrorDomain.STORAGE,
|
|
1586
|
+
category: ErrorCategory.USER,
|
|
1587
|
+
details: {
|
|
1588
|
+
entityType
|
|
1589
|
+
},
|
|
1590
|
+
text: `Cannot filter by entity type: ${entityType}`
|
|
1591
|
+
});
|
|
1592
|
+
throw error;
|
|
1593
|
+
}
|
|
1594
|
+
mongoFilter.name = name;
|
|
1595
|
+
}
|
|
1596
|
+
const count = await collection.countDocuments(mongoFilter);
|
|
1597
|
+
if (count === 0) {
|
|
1598
|
+
return {
|
|
1599
|
+
pagination: {
|
|
1600
|
+
total: 0,
|
|
1601
|
+
page,
|
|
1602
|
+
perPage,
|
|
1603
|
+
hasMore: false
|
|
1604
|
+
},
|
|
1605
|
+
spans: []
|
|
1606
|
+
};
|
|
1607
|
+
}
|
|
1608
|
+
const spans = await collection.find(mongoFilter).sort({ startedAt: -1 }).skip(page * perPage).limit(perPage).toArray();
|
|
1609
|
+
return {
|
|
1610
|
+
pagination: {
|
|
1611
|
+
total: count,
|
|
1612
|
+
page,
|
|
1613
|
+
perPage,
|
|
1614
|
+
hasMore: spans.length === perPage
|
|
1615
|
+
},
|
|
1616
|
+
spans: spans.map((span) => this.transformSpanFromMongo(span))
|
|
1617
|
+
};
|
|
1618
|
+
} catch (error) {
|
|
1619
|
+
throw new MastraError(
|
|
1620
|
+
{
|
|
1621
|
+
id: "MONGODB_STORE_GET_AI_TRACES_PAGINATED_FAILED",
|
|
1622
|
+
domain: ErrorDomain.STORAGE,
|
|
1623
|
+
category: ErrorCategory.USER
|
|
1624
|
+
},
|
|
1625
|
+
error
|
|
1626
|
+
);
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
async batchCreateAISpans(args) {
|
|
1630
|
+
try {
|
|
1631
|
+
const records = args.records.map((record) => {
|
|
1632
|
+
const startedAt = record.startedAt instanceof Date ? record.startedAt.toISOString() : record.startedAt;
|
|
1633
|
+
const endedAt = record.endedAt instanceof Date ? record.endedAt.toISOString() : record.endedAt;
|
|
1634
|
+
return {
|
|
1635
|
+
...record,
|
|
1636
|
+
startedAt,
|
|
1637
|
+
endedAt,
|
|
1638
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1639
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1640
|
+
};
|
|
1641
|
+
});
|
|
1642
|
+
return this.operations.batchInsert({
|
|
1643
|
+
tableName: TABLE_AI_SPANS,
|
|
1644
|
+
records
|
|
1645
|
+
});
|
|
1646
|
+
} catch (error) {
|
|
1647
|
+
throw new MastraError(
|
|
1648
|
+
{
|
|
1649
|
+
id: "MONGODB_STORE_BATCH_CREATE_AI_SPANS_FAILED",
|
|
1650
|
+
domain: ErrorDomain.STORAGE,
|
|
1651
|
+
category: ErrorCategory.USER
|
|
1652
|
+
},
|
|
1653
|
+
error
|
|
1654
|
+
);
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
async batchUpdateAISpans(args) {
|
|
1658
|
+
try {
|
|
1659
|
+
return this.operations.batchUpdate({
|
|
1660
|
+
tableName: TABLE_AI_SPANS,
|
|
1661
|
+
updates: args.records.map((record) => {
|
|
1662
|
+
const data = { ...record.updates };
|
|
1663
|
+
if (data.endedAt instanceof Date) {
|
|
1664
|
+
data.endedAt = data.endedAt.toISOString();
|
|
1665
|
+
}
|
|
1666
|
+
if (data.startedAt instanceof Date) {
|
|
1667
|
+
data.startedAt = data.startedAt.toISOString();
|
|
1668
|
+
}
|
|
1669
|
+
const updateData = {
|
|
1670
|
+
...data,
|
|
1671
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1672
|
+
};
|
|
1673
|
+
return {
|
|
1674
|
+
keys: { spanId: record.spanId, traceId: record.traceId },
|
|
1675
|
+
data: updateData
|
|
1676
|
+
};
|
|
1677
|
+
})
|
|
1678
|
+
});
|
|
1679
|
+
} catch (error) {
|
|
1680
|
+
throw new MastraError(
|
|
1681
|
+
{
|
|
1682
|
+
id: "MONGODB_STORE_BATCH_UPDATE_AI_SPANS_FAILED",
|
|
1683
|
+
domain: ErrorDomain.STORAGE,
|
|
1684
|
+
category: ErrorCategory.USER
|
|
1685
|
+
},
|
|
1686
|
+
error
|
|
1687
|
+
);
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
async batchDeleteAITraces(args) {
|
|
1691
|
+
try {
|
|
1692
|
+
const collection = await this.operations.getCollection(TABLE_AI_SPANS);
|
|
1693
|
+
await collection.deleteMany({
|
|
1694
|
+
traceId: { $in: args.traceIds }
|
|
1695
|
+
});
|
|
1696
|
+
} catch (error) {
|
|
1697
|
+
throw new MastraError(
|
|
1698
|
+
{
|
|
1699
|
+
id: "MONGODB_STORE_BATCH_DELETE_AI_TRACES_FAILED",
|
|
1700
|
+
domain: ErrorDomain.STORAGE,
|
|
1701
|
+
category: ErrorCategory.USER
|
|
1702
|
+
},
|
|
1703
|
+
error
|
|
1704
|
+
);
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Transform MongoDB document to AISpanRecord format
|
|
1709
|
+
*/
|
|
1710
|
+
transformSpanFromMongo(doc) {
|
|
1711
|
+
const { _id, ...span } = doc;
|
|
1712
|
+
if (span.startedAt && typeof span.startedAt === "string") {
|
|
1713
|
+
span.startedAt = span.startedAt;
|
|
1714
|
+
}
|
|
1715
|
+
if (span.endedAt && typeof span.endedAt === "string") {
|
|
1716
|
+
span.endedAt = span.endedAt;
|
|
1717
|
+
}
|
|
1718
|
+
if (span.createdAt && typeof span.createdAt === "string") {
|
|
1719
|
+
span.createdAt = new Date(span.createdAt);
|
|
1720
|
+
}
|
|
1721
|
+
if (span.updatedAt && typeof span.updatedAt === "string") {
|
|
1722
|
+
span.updatedAt = new Date(span.updatedAt);
|
|
1723
|
+
}
|
|
1724
|
+
return span;
|
|
1725
|
+
}
|
|
1726
|
+
};
|
|
1429
1727
|
var StoreOperationsMongoDB = class extends StoreOperations {
|
|
1430
1728
|
#connector;
|
|
1431
1729
|
constructor(config) {
|
|
@@ -1447,19 +1745,18 @@ var StoreOperationsMongoDB = class extends StoreOperations {
|
|
|
1447
1745
|
const collection = await this.getCollection(tableName);
|
|
1448
1746
|
await collection.deleteMany({});
|
|
1449
1747
|
} catch (error) {
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
}
|
|
1748
|
+
const mastraError = new MastraError(
|
|
1749
|
+
{
|
|
1750
|
+
id: "STORAGE_MONGODB_STORE_CLEAR_TABLE_FAILED",
|
|
1751
|
+
domain: ErrorDomain.STORAGE,
|
|
1752
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1753
|
+
details: { tableName }
|
|
1754
|
+
},
|
|
1755
|
+
error
|
|
1756
|
+
);
|
|
1757
|
+
this.logger.error(mastraError.message);
|
|
1758
|
+
this.logger?.trackException(mastraError);
|
|
1759
|
+
throw mastraError;
|
|
1463
1760
|
}
|
|
1464
1761
|
}
|
|
1465
1762
|
async dropTable({ tableName }) {
|
|
@@ -1483,6 +1780,9 @@ var StoreOperationsMongoDB = class extends StoreOperations {
|
|
|
1483
1780
|
}
|
|
1484
1781
|
processJsonbFields(tableName, record) {
|
|
1485
1782
|
const schema = TABLE_SCHEMAS[tableName];
|
|
1783
|
+
if (!schema) {
|
|
1784
|
+
return record;
|
|
1785
|
+
}
|
|
1486
1786
|
return Object.fromEntries(
|
|
1487
1787
|
Object.entries(schema).map(([key, value]) => {
|
|
1488
1788
|
if (value.type === "jsonb" && record[key] && typeof record[key] === "string") {
|
|
@@ -1498,19 +1798,18 @@ var StoreOperationsMongoDB = class extends StoreOperations {
|
|
|
1498
1798
|
const recordToInsert = this.processJsonbFields(tableName, record);
|
|
1499
1799
|
await collection.insertOne(recordToInsert);
|
|
1500
1800
|
} catch (error) {
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
}
|
|
1801
|
+
const mastraError = new MastraError(
|
|
1802
|
+
{
|
|
1803
|
+
id: "STORAGE_MONGODB_STORE_INSERT_FAILED",
|
|
1804
|
+
domain: ErrorDomain.STORAGE,
|
|
1805
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1806
|
+
details: { tableName }
|
|
1807
|
+
},
|
|
1808
|
+
error
|
|
1809
|
+
);
|
|
1810
|
+
this.logger.error(mastraError.message);
|
|
1811
|
+
this.logger?.trackException(mastraError);
|
|
1812
|
+
throw mastraError;
|
|
1514
1813
|
}
|
|
1515
1814
|
}
|
|
1516
1815
|
async batchInsert({ tableName, records }) {
|
|
@@ -1550,6 +1849,60 @@ var StoreOperationsMongoDB = class extends StoreOperations {
|
|
|
1550
1849
|
);
|
|
1551
1850
|
}
|
|
1552
1851
|
}
|
|
1852
|
+
async update({
|
|
1853
|
+
tableName,
|
|
1854
|
+
keys,
|
|
1855
|
+
data
|
|
1856
|
+
}) {
|
|
1857
|
+
try {
|
|
1858
|
+
const collection = await this.getCollection(tableName);
|
|
1859
|
+
const processedData = this.processJsonbFields(tableName, data);
|
|
1860
|
+
const cleanData = Object.fromEntries(Object.entries(processedData).filter(([_, value]) => value !== void 0));
|
|
1861
|
+
await collection.updateOne(keys, { $set: cleanData });
|
|
1862
|
+
} catch (error) {
|
|
1863
|
+
throw new MastraError(
|
|
1864
|
+
{
|
|
1865
|
+
id: "STORAGE_MONGODB_STORE_UPDATE_FAILED",
|
|
1866
|
+
domain: ErrorDomain.STORAGE,
|
|
1867
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1868
|
+
details: { tableName }
|
|
1869
|
+
},
|
|
1870
|
+
error
|
|
1871
|
+
);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
async batchUpdate({
|
|
1875
|
+
tableName,
|
|
1876
|
+
updates
|
|
1877
|
+
}) {
|
|
1878
|
+
if (!updates.length) {
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
try {
|
|
1882
|
+
const collection = await this.getCollection(tableName);
|
|
1883
|
+
const bulkOps = updates.map(({ keys, data }) => {
|
|
1884
|
+
const processedData = this.processJsonbFields(tableName, data);
|
|
1885
|
+
const cleanData = Object.fromEntries(Object.entries(processedData).filter(([_, value]) => value !== void 0));
|
|
1886
|
+
return {
|
|
1887
|
+
updateOne: {
|
|
1888
|
+
filter: keys,
|
|
1889
|
+
update: { $set: cleanData }
|
|
1890
|
+
}
|
|
1891
|
+
};
|
|
1892
|
+
});
|
|
1893
|
+
await collection.bulkWrite(bulkOps);
|
|
1894
|
+
} catch (error) {
|
|
1895
|
+
throw new MastraError(
|
|
1896
|
+
{
|
|
1897
|
+
id: "STORAGE_MONGODB_STORE_BATCH_UPDATE_FAILED",
|
|
1898
|
+
domain: ErrorDomain.STORAGE,
|
|
1899
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1900
|
+
details: { tableName }
|
|
1901
|
+
},
|
|
1902
|
+
error
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1553
1906
|
};
|
|
1554
1907
|
function transformScoreRow(row) {
|
|
1555
1908
|
let scorerValue = null;
|
|
@@ -2289,13 +2642,17 @@ var MongoDBStore = class extends MastraStorage {
|
|
|
2289
2642
|
const workflows = new WorkflowsStorageMongoDB({
|
|
2290
2643
|
operations
|
|
2291
2644
|
});
|
|
2645
|
+
const observability = new ObservabilityMongoDB({
|
|
2646
|
+
operations
|
|
2647
|
+
});
|
|
2292
2648
|
this.stores = {
|
|
2293
2649
|
operations,
|
|
2294
2650
|
memory,
|
|
2295
2651
|
traces,
|
|
2296
2652
|
legacyEvals,
|
|
2297
2653
|
scores,
|
|
2298
|
-
workflows
|
|
2654
|
+
workflows,
|
|
2655
|
+
observability
|
|
2299
2656
|
};
|
|
2300
2657
|
}
|
|
2301
2658
|
async createTable({
|
|
@@ -2328,6 +2685,9 @@ var MongoDBStore = class extends MastraStorage {
|
|
|
2328
2685
|
async getThreadsByResourceId({ resourceId }) {
|
|
2329
2686
|
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2330
2687
|
}
|
|
2688
|
+
async getThreadsByResourceIdPaginated(_args) {
|
|
2689
|
+
return this.stores.memory.getThreadsByResourceIdPaginated(_args);
|
|
2690
|
+
}
|
|
2331
2691
|
async saveThread({ thread }) {
|
|
2332
2692
|
return this.stores.memory.saveThread({ thread });
|
|
2333
2693
|
}
|
|
@@ -2348,6 +2708,9 @@ var MongoDBStore = class extends MastraStorage {
|
|
|
2348
2708
|
}) {
|
|
2349
2709
|
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2350
2710
|
}
|
|
2711
|
+
async getMessagesPaginated(_args) {
|
|
2712
|
+
return this.stores.memory.getMessagesPaginated(_args);
|
|
2713
|
+
}
|
|
2351
2714
|
async getMessagesById({
|
|
2352
2715
|
messageIds,
|
|
2353
2716
|
format
|
|
@@ -2357,12 +2720,6 @@ var MongoDBStore = class extends MastraStorage {
|
|
|
2357
2720
|
async saveMessages(args) {
|
|
2358
2721
|
return this.stores.memory.saveMessages(args);
|
|
2359
2722
|
}
|
|
2360
|
-
async getThreadsByResourceIdPaginated(_args) {
|
|
2361
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(_args);
|
|
2362
|
-
}
|
|
2363
|
-
async getMessagesPaginated(_args) {
|
|
2364
|
-
return this.stores.memory.getMessagesPaginated(_args);
|
|
2365
|
-
}
|
|
2366
2723
|
async updateMessages(_args) {
|
|
2367
2724
|
return this.stores.memory.updateMessages(_args);
|
|
2368
2725
|
}
|
|
@@ -2372,6 +2729,9 @@ var MongoDBStore = class extends MastraStorage {
|
|
|
2372
2729
|
async getTracesPaginated(args) {
|
|
2373
2730
|
return this.stores.traces.getTracesPaginated(args);
|
|
2374
2731
|
}
|
|
2732
|
+
async batchTraceInsert({ records }) {
|
|
2733
|
+
return this.stores.traces.batchTraceInsert({ records });
|
|
2734
|
+
}
|
|
2375
2735
|
async getWorkflowRuns(args) {
|
|
2376
2736
|
return this.stores.workflows.getWorkflowRuns(args);
|
|
2377
2737
|
}
|
|
@@ -2489,6 +2849,90 @@ var MongoDBStore = class extends MastraStorage {
|
|
|
2489
2849
|
metadata
|
|
2490
2850
|
});
|
|
2491
2851
|
}
|
|
2852
|
+
/**
|
|
2853
|
+
* AI Tracing/Observability
|
|
2854
|
+
*/
|
|
2855
|
+
async createAISpan(span) {
|
|
2856
|
+
if (!this.stores.observability) {
|
|
2857
|
+
throw new MastraError({
|
|
2858
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2859
|
+
domain: ErrorDomain.STORAGE,
|
|
2860
|
+
category: ErrorCategory.SYSTEM,
|
|
2861
|
+
text: "Observability storage is not initialized"
|
|
2862
|
+
});
|
|
2863
|
+
}
|
|
2864
|
+
return this.stores.observability.createAISpan(span);
|
|
2865
|
+
}
|
|
2866
|
+
async updateAISpan({
|
|
2867
|
+
spanId,
|
|
2868
|
+
traceId,
|
|
2869
|
+
updates
|
|
2870
|
+
}) {
|
|
2871
|
+
if (!this.stores.observability) {
|
|
2872
|
+
throw new MastraError({
|
|
2873
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2874
|
+
domain: ErrorDomain.STORAGE,
|
|
2875
|
+
category: ErrorCategory.SYSTEM,
|
|
2876
|
+
text: "Observability storage is not initialized"
|
|
2877
|
+
});
|
|
2878
|
+
}
|
|
2879
|
+
return this.stores.observability.updateAISpan({ spanId, traceId, updates });
|
|
2880
|
+
}
|
|
2881
|
+
async getAITrace(traceId) {
|
|
2882
|
+
if (!this.stores.observability) {
|
|
2883
|
+
throw new MastraError({
|
|
2884
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2885
|
+
domain: ErrorDomain.STORAGE,
|
|
2886
|
+
category: ErrorCategory.SYSTEM,
|
|
2887
|
+
text: "Observability storage is not initialized"
|
|
2888
|
+
});
|
|
2889
|
+
}
|
|
2890
|
+
return this.stores.observability.getAITrace(traceId);
|
|
2891
|
+
}
|
|
2892
|
+
async getAITracesPaginated(args) {
|
|
2893
|
+
if (!this.stores.observability) {
|
|
2894
|
+
throw new MastraError({
|
|
2895
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2896
|
+
domain: ErrorDomain.STORAGE,
|
|
2897
|
+
category: ErrorCategory.SYSTEM,
|
|
2898
|
+
text: "Observability storage is not initialized"
|
|
2899
|
+
});
|
|
2900
|
+
}
|
|
2901
|
+
return this.stores.observability.getAITracesPaginated(args);
|
|
2902
|
+
}
|
|
2903
|
+
async batchCreateAISpans(args) {
|
|
2904
|
+
if (!this.stores.observability) {
|
|
2905
|
+
throw new MastraError({
|
|
2906
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2907
|
+
domain: ErrorDomain.STORAGE,
|
|
2908
|
+
category: ErrorCategory.SYSTEM,
|
|
2909
|
+
text: "Observability storage is not initialized"
|
|
2910
|
+
});
|
|
2911
|
+
}
|
|
2912
|
+
return this.stores.observability.batchCreateAISpans(args);
|
|
2913
|
+
}
|
|
2914
|
+
async batchUpdateAISpans(args) {
|
|
2915
|
+
if (!this.stores.observability) {
|
|
2916
|
+
throw new MastraError({
|
|
2917
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2918
|
+
domain: ErrorDomain.STORAGE,
|
|
2919
|
+
category: ErrorCategory.SYSTEM,
|
|
2920
|
+
text: "Observability storage is not initialized"
|
|
2921
|
+
});
|
|
2922
|
+
}
|
|
2923
|
+
return this.stores.observability.batchUpdateAISpans(args);
|
|
2924
|
+
}
|
|
2925
|
+
async batchDeleteAITraces(args) {
|
|
2926
|
+
if (!this.stores.observability) {
|
|
2927
|
+
throw new MastraError({
|
|
2928
|
+
id: "MONGODB_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
2929
|
+
domain: ErrorDomain.STORAGE,
|
|
2930
|
+
category: ErrorCategory.SYSTEM,
|
|
2931
|
+
text: "Observability storage is not initialized"
|
|
2932
|
+
});
|
|
2933
|
+
}
|
|
2934
|
+
return this.stores.observability.batchDeleteAITraces(args);
|
|
2935
|
+
}
|
|
2492
2936
|
};
|
|
2493
2937
|
|
|
2494
2938
|
// src/vector/prompt.ts
|