@mastra/mongodb 1.0.0-beta.7 → 1.0.0-beta.8
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 +218 -0
- package/dist/index.cjs +971 -615
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +972 -616
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +10 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/domains/agents/index.d.ts +36 -0
- package/dist/storage/domains/agents/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +21 -5
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +28 -28
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +24 -23
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +23 -6
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +24 -186
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/types.d.ts +67 -1
- package/dist/storage/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/storage/domains/operations/index.d.ts +0 -50
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -13,7 +13,7 @@ var evals = require('@mastra/core/evals');
|
|
|
13
13
|
|
|
14
14
|
// package.json
|
|
15
15
|
var package_default = {
|
|
16
|
-
version: "1.0.0-beta.
|
|
16
|
+
version: "1.0.0-beta.8"};
|
|
17
17
|
var MongoDBFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
18
18
|
getSupportedOperators() {
|
|
19
19
|
return {
|
|
@@ -838,16 +838,401 @@ var MongoDBConnector = class _MongoDBConnector {
|
|
|
838
838
|
}
|
|
839
839
|
}
|
|
840
840
|
};
|
|
841
|
+
|
|
842
|
+
// src/storage/db/index.ts
|
|
843
|
+
function resolveMongoDBConfig(config) {
|
|
844
|
+
if ("connector" in config) {
|
|
845
|
+
return config.connector;
|
|
846
|
+
}
|
|
847
|
+
if ("connectorHandler" in config) {
|
|
848
|
+
try {
|
|
849
|
+
return MongoDBConnector.fromConnectionHandler(config.connectorHandler);
|
|
850
|
+
} catch (error$1) {
|
|
851
|
+
throw new error.MastraError(
|
|
852
|
+
{
|
|
853
|
+
id: storage.createStorageErrorId("MONGODB", "CONSTRUCTOR", "FAILED"),
|
|
854
|
+
domain: error.ErrorDomain.STORAGE,
|
|
855
|
+
category: error.ErrorCategory.USER,
|
|
856
|
+
details: { connectionHandler: true }
|
|
857
|
+
},
|
|
858
|
+
error$1
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
try {
|
|
863
|
+
return MongoDBConnector.fromDatabaseConfig({
|
|
864
|
+
id: "id" in config ? config.id : "domain",
|
|
865
|
+
options: config.options,
|
|
866
|
+
url: config.url,
|
|
867
|
+
dbName: config.dbName
|
|
868
|
+
});
|
|
869
|
+
} catch (error$1) {
|
|
870
|
+
throw new error.MastraError(
|
|
871
|
+
{
|
|
872
|
+
id: storage.createStorageErrorId("MONGODB", "CONSTRUCTOR", "FAILED"),
|
|
873
|
+
domain: error.ErrorDomain.STORAGE,
|
|
874
|
+
category: error.ErrorCategory.USER,
|
|
875
|
+
details: { url: config?.url, dbName: config?.dbName }
|
|
876
|
+
},
|
|
877
|
+
error$1
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
var MongoDBAgentsStorage = class _MongoDBAgentsStorage extends storage.AgentsStorage {
|
|
882
|
+
#connector;
|
|
883
|
+
#skipDefaultIndexes;
|
|
884
|
+
#indexes;
|
|
885
|
+
/** Collections managed by this domain */
|
|
886
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_AGENTS];
|
|
887
|
+
constructor(config) {
|
|
888
|
+
super();
|
|
889
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
890
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
891
|
+
this.#indexes = config.indexes?.filter(
|
|
892
|
+
(idx) => _MongoDBAgentsStorage.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
async getCollection(name) {
|
|
896
|
+
return this.#connector.getCollection(name);
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Returns default index definitions for the agents domain collections.
|
|
900
|
+
* These indexes optimize common query patterns for agent lookups.
|
|
901
|
+
*/
|
|
902
|
+
getDefaultIndexDefinitions() {
|
|
903
|
+
return [
|
|
904
|
+
{ collection: storage.TABLE_AGENTS, keys: { id: 1 }, options: { unique: true } },
|
|
905
|
+
{ collection: storage.TABLE_AGENTS, keys: { createdAt: -1 } },
|
|
906
|
+
{ collection: storage.TABLE_AGENTS, keys: { updatedAt: -1 } }
|
|
907
|
+
];
|
|
908
|
+
}
|
|
909
|
+
async createDefaultIndexes() {
|
|
910
|
+
if (this.#skipDefaultIndexes) {
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
914
|
+
try {
|
|
915
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
916
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
917
|
+
} catch (error) {
|
|
918
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Creates custom user-defined indexes for this domain's collections.
|
|
924
|
+
*/
|
|
925
|
+
async createCustomIndexes() {
|
|
926
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
for (const indexDef of this.#indexes) {
|
|
930
|
+
try {
|
|
931
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
932
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
933
|
+
} catch (error) {
|
|
934
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
async init() {
|
|
939
|
+
await this.createDefaultIndexes();
|
|
940
|
+
await this.createCustomIndexes();
|
|
941
|
+
}
|
|
942
|
+
async dangerouslyClearAll() {
|
|
943
|
+
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
944
|
+
await collection.deleteMany({});
|
|
945
|
+
}
|
|
946
|
+
async getAgentById({ id }) {
|
|
947
|
+
try {
|
|
948
|
+
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
949
|
+
const result = await collection.findOne({ id });
|
|
950
|
+
if (!result) {
|
|
951
|
+
return null;
|
|
952
|
+
}
|
|
953
|
+
return this.transformAgent(result);
|
|
954
|
+
} catch (error$1) {
|
|
955
|
+
throw new error.MastraError(
|
|
956
|
+
{
|
|
957
|
+
id: storage.createStorageErrorId("MONGODB", "GET_AGENT_BY_ID", "FAILED"),
|
|
958
|
+
domain: error.ErrorDomain.STORAGE,
|
|
959
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
960
|
+
details: { id }
|
|
961
|
+
},
|
|
962
|
+
error$1
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
async createAgent({ agent }) {
|
|
967
|
+
try {
|
|
968
|
+
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
969
|
+
const existing = await collection.findOne({ id: agent.id });
|
|
970
|
+
if (existing) {
|
|
971
|
+
throw new error.MastraError({
|
|
972
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_AGENT", "ALREADY_EXISTS"),
|
|
973
|
+
domain: error.ErrorDomain.STORAGE,
|
|
974
|
+
category: error.ErrorCategory.USER,
|
|
975
|
+
details: { id: agent.id },
|
|
976
|
+
text: `Agent with id ${agent.id} already exists`
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
const now = /* @__PURE__ */ new Date();
|
|
980
|
+
const newAgent = {
|
|
981
|
+
...agent,
|
|
982
|
+
createdAt: now,
|
|
983
|
+
updatedAt: now
|
|
984
|
+
};
|
|
985
|
+
await collection.insertOne(this.serializeAgent(newAgent));
|
|
986
|
+
return newAgent;
|
|
987
|
+
} catch (error$1) {
|
|
988
|
+
if (error$1 instanceof error.MastraError) {
|
|
989
|
+
throw error$1;
|
|
990
|
+
}
|
|
991
|
+
throw new error.MastraError(
|
|
992
|
+
{
|
|
993
|
+
id: storage.createStorageErrorId("MONGODB", "CREATE_AGENT", "FAILED"),
|
|
994
|
+
domain: error.ErrorDomain.STORAGE,
|
|
995
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
996
|
+
details: { id: agent.id }
|
|
997
|
+
},
|
|
998
|
+
error$1
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
async updateAgent({ id, ...updates }) {
|
|
1003
|
+
try {
|
|
1004
|
+
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
1005
|
+
const existingAgent = await collection.findOne({ id });
|
|
1006
|
+
if (!existingAgent) {
|
|
1007
|
+
throw new error.MastraError({
|
|
1008
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_AGENT", "NOT_FOUND"),
|
|
1009
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1010
|
+
category: error.ErrorCategory.USER,
|
|
1011
|
+
details: { id },
|
|
1012
|
+
text: `Agent with id ${id} not found`
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
const updateDoc = {
|
|
1016
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
1017
|
+
};
|
|
1018
|
+
if (updates.name !== void 0) updateDoc.name = updates.name;
|
|
1019
|
+
if (updates.description !== void 0) updateDoc.description = updates.description;
|
|
1020
|
+
if (updates.instructions !== void 0) updateDoc.instructions = updates.instructions;
|
|
1021
|
+
if (updates.model !== void 0) updateDoc.model = updates.model;
|
|
1022
|
+
if (updates.tools !== void 0) updateDoc.tools = updates.tools;
|
|
1023
|
+
if (updates.defaultOptions !== void 0) updateDoc.defaultOptions = updates.defaultOptions;
|
|
1024
|
+
if (updates.workflows !== void 0) updateDoc.workflows = updates.workflows;
|
|
1025
|
+
if (updates.agents !== void 0) updateDoc.agents = updates.agents;
|
|
1026
|
+
if (updates.inputProcessors !== void 0) updateDoc.inputProcessors = updates.inputProcessors;
|
|
1027
|
+
if (updates.outputProcessors !== void 0) updateDoc.outputProcessors = updates.outputProcessors;
|
|
1028
|
+
if (updates.memory !== void 0) updateDoc.memory = updates.memory;
|
|
1029
|
+
if (updates.scorers !== void 0) updateDoc.scorers = updates.scorers;
|
|
1030
|
+
if (updates.metadata !== void 0) {
|
|
1031
|
+
const existingMetadata = existingAgent.metadata || {};
|
|
1032
|
+
updateDoc.metadata = { ...existingMetadata, ...updates.metadata };
|
|
1033
|
+
}
|
|
1034
|
+
await collection.updateOne({ id }, { $set: updateDoc });
|
|
1035
|
+
const updatedAgent = await collection.findOne({ id });
|
|
1036
|
+
if (!updatedAgent) {
|
|
1037
|
+
throw new error.MastraError({
|
|
1038
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_AGENT", "NOT_FOUND_AFTER_UPDATE"),
|
|
1039
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1040
|
+
category: error.ErrorCategory.SYSTEM,
|
|
1041
|
+
text: `Agent with id ${id} was deleted during update`,
|
|
1042
|
+
details: { id }
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
return this.transformAgent(updatedAgent);
|
|
1046
|
+
} catch (error$1) {
|
|
1047
|
+
if (error$1 instanceof error.MastraError) {
|
|
1048
|
+
throw error$1;
|
|
1049
|
+
}
|
|
1050
|
+
throw new error.MastraError(
|
|
1051
|
+
{
|
|
1052
|
+
id: storage.createStorageErrorId("MONGODB", "UPDATE_AGENT", "FAILED"),
|
|
1053
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1054
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1055
|
+
details: { id }
|
|
1056
|
+
},
|
|
1057
|
+
error$1
|
|
1058
|
+
);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
async deleteAgent({ id }) {
|
|
1062
|
+
try {
|
|
1063
|
+
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
1064
|
+
await collection.deleteOne({ id });
|
|
1065
|
+
} catch (error$1) {
|
|
1066
|
+
throw new error.MastraError(
|
|
1067
|
+
{
|
|
1068
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_AGENT", "FAILED"),
|
|
1069
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1070
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1071
|
+
details: { id }
|
|
1072
|
+
},
|
|
1073
|
+
error$1
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
async listAgents(args) {
|
|
1078
|
+
try {
|
|
1079
|
+
const { page = 0, perPage: perPageInput, orderBy } = args || {};
|
|
1080
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1081
|
+
if (page < 0) {
|
|
1082
|
+
throw new error.MastraError(
|
|
1083
|
+
{
|
|
1084
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_AGENTS", "INVALID_PAGE"),
|
|
1085
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1086
|
+
category: error.ErrorCategory.USER,
|
|
1087
|
+
details: { page }
|
|
1088
|
+
},
|
|
1089
|
+
new Error("page must be >= 0")
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1093
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1094
|
+
const collection = await this.getCollection(storage.TABLE_AGENTS);
|
|
1095
|
+
const total = await collection.countDocuments({});
|
|
1096
|
+
if (total === 0 || perPage === 0) {
|
|
1097
|
+
return {
|
|
1098
|
+
agents: [],
|
|
1099
|
+
total,
|
|
1100
|
+
page,
|
|
1101
|
+
perPage: perPageForResponse,
|
|
1102
|
+
hasMore: false
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
1106
|
+
let cursor = collection.find({}).sort({ [field]: sortOrder }).skip(offset);
|
|
1107
|
+
if (perPageInput !== false) {
|
|
1108
|
+
cursor = cursor.limit(perPage);
|
|
1109
|
+
}
|
|
1110
|
+
const results = await cursor.toArray();
|
|
1111
|
+
const agents = results.map((doc) => this.transformAgent(doc));
|
|
1112
|
+
return {
|
|
1113
|
+
agents,
|
|
1114
|
+
total,
|
|
1115
|
+
page,
|
|
1116
|
+
perPage: perPageForResponse,
|
|
1117
|
+
hasMore: perPageInput !== false && offset + perPage < total
|
|
1118
|
+
};
|
|
1119
|
+
} catch (error$1) {
|
|
1120
|
+
if (error$1 instanceof error.MastraError) {
|
|
1121
|
+
throw error$1;
|
|
1122
|
+
}
|
|
1123
|
+
throw new error.MastraError(
|
|
1124
|
+
{
|
|
1125
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_AGENTS", "FAILED"),
|
|
1126
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1127
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1128
|
+
},
|
|
1129
|
+
error$1
|
|
1130
|
+
);
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
transformAgent(doc) {
|
|
1134
|
+
const { _id, ...agent } = doc;
|
|
1135
|
+
return {
|
|
1136
|
+
...agent,
|
|
1137
|
+
createdAt: agent.createdAt instanceof Date ? agent.createdAt : new Date(agent.createdAt),
|
|
1138
|
+
updatedAt: agent.updatedAt instanceof Date ? agent.updatedAt : new Date(agent.updatedAt)
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
serializeAgent(agent) {
|
|
1142
|
+
return {
|
|
1143
|
+
...agent
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
841
1147
|
function formatDateForMongoDB(date) {
|
|
842
1148
|
return typeof date === "string" ? new Date(date) : date;
|
|
843
1149
|
}
|
|
844
1150
|
|
|
845
1151
|
// src/storage/domains/memory/index.ts
|
|
846
|
-
var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
847
|
-
|
|
848
|
-
|
|
1152
|
+
var MemoryStorageMongoDB = class _MemoryStorageMongoDB extends storage.MemoryStorage {
|
|
1153
|
+
#connector;
|
|
1154
|
+
#skipDefaultIndexes;
|
|
1155
|
+
#indexes;
|
|
1156
|
+
/** Collections managed by this domain */
|
|
1157
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_THREADS, storage.TABLE_MESSAGES, storage.TABLE_RESOURCES];
|
|
1158
|
+
constructor(config) {
|
|
849
1159
|
super();
|
|
850
|
-
this
|
|
1160
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
1161
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
1162
|
+
this.#indexes = config.indexes?.filter(
|
|
1163
|
+
(idx) => _MemoryStorageMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
1164
|
+
);
|
|
1165
|
+
}
|
|
1166
|
+
async getCollection(name) {
|
|
1167
|
+
return this.#connector.getCollection(name);
|
|
1168
|
+
}
|
|
1169
|
+
async init() {
|
|
1170
|
+
await this.createDefaultIndexes();
|
|
1171
|
+
await this.createCustomIndexes();
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Returns default index definitions for the memory domain collections.
|
|
1175
|
+
*/
|
|
1176
|
+
getDefaultIndexDefinitions() {
|
|
1177
|
+
return [
|
|
1178
|
+
// Threads collection indexes
|
|
1179
|
+
{ collection: storage.TABLE_THREADS, keys: { id: 1 }, options: { unique: true } },
|
|
1180
|
+
{ collection: storage.TABLE_THREADS, keys: { resourceId: 1 } },
|
|
1181
|
+
{ collection: storage.TABLE_THREADS, keys: { createdAt: -1 } },
|
|
1182
|
+
{ collection: storage.TABLE_THREADS, keys: { updatedAt: -1 } },
|
|
1183
|
+
// Messages collection indexes
|
|
1184
|
+
{ collection: storage.TABLE_MESSAGES, keys: { id: 1 }, options: { unique: true } },
|
|
1185
|
+
{ collection: storage.TABLE_MESSAGES, keys: { thread_id: 1 } },
|
|
1186
|
+
{ collection: storage.TABLE_MESSAGES, keys: { resourceId: 1 } },
|
|
1187
|
+
{ collection: storage.TABLE_MESSAGES, keys: { createdAt: -1 } },
|
|
1188
|
+
{ collection: storage.TABLE_MESSAGES, keys: { thread_id: 1, createdAt: 1 } },
|
|
1189
|
+
// Resources collection indexes
|
|
1190
|
+
{ collection: storage.TABLE_RESOURCES, keys: { id: 1 }, options: { unique: true } },
|
|
1191
|
+
{ collection: storage.TABLE_RESOURCES, keys: { createdAt: -1 } },
|
|
1192
|
+
{ collection: storage.TABLE_RESOURCES, keys: { updatedAt: -1 } }
|
|
1193
|
+
];
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Creates default indexes for optimal query performance.
|
|
1197
|
+
*/
|
|
1198
|
+
async createDefaultIndexes() {
|
|
1199
|
+
if (this.#skipDefaultIndexes) {
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
1203
|
+
try {
|
|
1204
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
1205
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
1206
|
+
} catch (error) {
|
|
1207
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Creates custom user-defined indexes for this domain's collections.
|
|
1213
|
+
*/
|
|
1214
|
+
async createCustomIndexes() {
|
|
1215
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
for (const indexDef of this.#indexes) {
|
|
1219
|
+
try {
|
|
1220
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
1221
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
1222
|
+
} catch (error) {
|
|
1223
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
async dangerouslyClearAll() {
|
|
1228
|
+
const threadsCollection = await this.getCollection(storage.TABLE_THREADS);
|
|
1229
|
+
const messagesCollection = await this.getCollection(storage.TABLE_MESSAGES);
|
|
1230
|
+
const resourcesCollection = await this.getCollection(storage.TABLE_RESOURCES);
|
|
1231
|
+
await Promise.all([
|
|
1232
|
+
threadsCollection.deleteMany({}),
|
|
1233
|
+
messagesCollection.deleteMany({}),
|
|
1234
|
+
resourcesCollection.deleteMany({})
|
|
1235
|
+
]);
|
|
851
1236
|
}
|
|
852
1237
|
parseRow(row) {
|
|
853
1238
|
let content = row.content;
|
|
@@ -870,7 +1255,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
870
1255
|
}
|
|
871
1256
|
async _getIncludedMessages({ include }) {
|
|
872
1257
|
if (!include || include.length === 0) return null;
|
|
873
|
-
const collection = await this.
|
|
1258
|
+
const collection = await this.getCollection(storage.TABLE_MESSAGES);
|
|
874
1259
|
const includedMessages = [];
|
|
875
1260
|
for (const inc of include) {
|
|
876
1261
|
const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
|
|
@@ -897,7 +1282,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
897
1282
|
async listMessagesById({ messageIds }) {
|
|
898
1283
|
if (messageIds.length === 0) return { messages: [] };
|
|
899
1284
|
try {
|
|
900
|
-
const collection = await this.
|
|
1285
|
+
const collection = await this.getCollection(storage.TABLE_MESSAGES);
|
|
901
1286
|
const rawMessages = await collection.find({ id: { $in: messageIds } }).sort({ createdAt: -1 }).toArray();
|
|
902
1287
|
const list = new agent.MessageList().add(
|
|
903
1288
|
rawMessages.map(this.parseRow),
|
|
@@ -946,7 +1331,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
946
1331
|
try {
|
|
947
1332
|
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
948
1333
|
const sortOrder = direction === "ASC" ? 1 : -1;
|
|
949
|
-
const collection = await this.
|
|
1334
|
+
const collection = await this.getCollection(storage.TABLE_MESSAGES);
|
|
950
1335
|
const query = { thread_id: threadIds.length === 1 ? threadIds[0] : { $in: threadIds } };
|
|
951
1336
|
if (resourceId) {
|
|
952
1337
|
query.resourceId = resourceId;
|
|
@@ -1044,8 +1429,8 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1044
1429
|
if (!threadId) {
|
|
1045
1430
|
throw new Error("Thread ID is required");
|
|
1046
1431
|
}
|
|
1047
|
-
const collection = await this.
|
|
1048
|
-
const threadsCollection = await this.
|
|
1432
|
+
const collection = await this.getCollection(storage.TABLE_MESSAGES);
|
|
1433
|
+
const threadsCollection = await this.getCollection(storage.TABLE_THREADS);
|
|
1049
1434
|
const messagesToInsert = messages.map((message) => {
|
|
1050
1435
|
const time = message.createdAt || /* @__PURE__ */ new Date();
|
|
1051
1436
|
if (!message.threadId) {
|
|
@@ -1100,7 +1485,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1100
1485
|
return [];
|
|
1101
1486
|
}
|
|
1102
1487
|
const messageIds = messages.map((m) => m.id);
|
|
1103
|
-
const collection = await this.
|
|
1488
|
+
const collection = await this.getCollection(storage.TABLE_MESSAGES);
|
|
1104
1489
|
const existingMessages = await collection.find({ id: { $in: messageIds } }).toArray();
|
|
1105
1490
|
const existingMessagesParsed = existingMessages.map((msg) => this.parseRow(msg));
|
|
1106
1491
|
if (existingMessagesParsed.length === 0) {
|
|
@@ -1157,7 +1542,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1157
1542
|
await collection.bulkWrite(bulkOps);
|
|
1158
1543
|
}
|
|
1159
1544
|
if (threadIdsToUpdate.size > 0) {
|
|
1160
|
-
const threadsCollection = await this.
|
|
1545
|
+
const threadsCollection = await this.getCollection(storage.TABLE_THREADS);
|
|
1161
1546
|
await threadsCollection.updateMany(
|
|
1162
1547
|
{ id: { $in: Array.from(threadIdsToUpdate) } },
|
|
1163
1548
|
{ $set: { updatedAt: /* @__PURE__ */ new Date() } }
|
|
@@ -1168,7 +1553,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1168
1553
|
}
|
|
1169
1554
|
async getResourceById({ resourceId }) {
|
|
1170
1555
|
try {
|
|
1171
|
-
const collection = await this.
|
|
1556
|
+
const collection = await this.getCollection(storage.TABLE_RESOURCES);
|
|
1172
1557
|
const result = await collection.findOne({ id: resourceId });
|
|
1173
1558
|
if (!result) {
|
|
1174
1559
|
return null;
|
|
@@ -1194,7 +1579,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1194
1579
|
}
|
|
1195
1580
|
async saveResource({ resource }) {
|
|
1196
1581
|
try {
|
|
1197
|
-
const collection = await this.
|
|
1582
|
+
const collection = await this.getCollection(storage.TABLE_RESOURCES);
|
|
1198
1583
|
await collection.updateOne(
|
|
1199
1584
|
{ id: resource.id },
|
|
1200
1585
|
{
|
|
@@ -1241,7 +1626,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1241
1626
|
metadata: metadata ? { ...existingResource.metadata, ...metadata } : existingResource.metadata,
|
|
1242
1627
|
updatedAt: /* @__PURE__ */ new Date()
|
|
1243
1628
|
};
|
|
1244
|
-
const collection = await this.
|
|
1629
|
+
const collection = await this.getCollection(storage.TABLE_RESOURCES);
|
|
1245
1630
|
const updateDoc = { updatedAt: updatedResource.updatedAt };
|
|
1246
1631
|
if (workingMemory !== void 0) {
|
|
1247
1632
|
updateDoc.workingMemory = workingMemory;
|
|
@@ -1265,7 +1650,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1265
1650
|
}
|
|
1266
1651
|
async getThreadById({ threadId }) {
|
|
1267
1652
|
try {
|
|
1268
|
-
const collection = await this.
|
|
1653
|
+
const collection = await this.getCollection(storage.TABLE_THREADS);
|
|
1269
1654
|
const result = await collection.findOne({ id: threadId });
|
|
1270
1655
|
if (!result) {
|
|
1271
1656
|
return null;
|
|
@@ -1303,7 +1688,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1303
1688
|
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1304
1689
|
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1305
1690
|
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1306
|
-
const collection = await this.
|
|
1691
|
+
const collection = await this.getCollection(storage.TABLE_THREADS);
|
|
1307
1692
|
const query = { resourceId };
|
|
1308
1693
|
const total = await collection.countDocuments(query);
|
|
1309
1694
|
if (perPage === 0) {
|
|
@@ -1349,7 +1734,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1349
1734
|
}
|
|
1350
1735
|
async saveThread({ thread }) {
|
|
1351
1736
|
try {
|
|
1352
|
-
const collection = await this.
|
|
1737
|
+
const collection = await this.getCollection(storage.TABLE_THREADS);
|
|
1353
1738
|
await collection.updateOne(
|
|
1354
1739
|
{ id: thread.id },
|
|
1355
1740
|
{
|
|
@@ -1397,7 +1782,7 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1397
1782
|
}
|
|
1398
1783
|
};
|
|
1399
1784
|
try {
|
|
1400
|
-
const collection = await this.
|
|
1785
|
+
const collection = await this.getCollection(storage.TABLE_THREADS);
|
|
1401
1786
|
await collection.updateOne(
|
|
1402
1787
|
{ id },
|
|
1403
1788
|
{
|
|
@@ -1422,9 +1807,9 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1422
1807
|
}
|
|
1423
1808
|
async deleteThread({ threadId }) {
|
|
1424
1809
|
try {
|
|
1425
|
-
const collectionMessages = await this.
|
|
1810
|
+
const collectionMessages = await this.getCollection(storage.TABLE_MESSAGES);
|
|
1426
1811
|
await collectionMessages.deleteMany({ thread_id: threadId });
|
|
1427
|
-
const collectionThreads = await this.
|
|
1812
|
+
const collectionThreads = await this.getCollection(storage.TABLE_THREADS);
|
|
1428
1813
|
await collectionThreads.deleteOne({ id: threadId });
|
|
1429
1814
|
} catch (error$1) {
|
|
1430
1815
|
throw new error.MastraError(
|
|
@@ -1438,12 +1823,97 @@ var MemoryStorageMongoDB = class extends storage.MemoryStorage {
|
|
|
1438
1823
|
);
|
|
1439
1824
|
}
|
|
1440
1825
|
}
|
|
1826
|
+
async deleteMessages(messageIds) {
|
|
1827
|
+
if (messageIds.length === 0) return;
|
|
1828
|
+
try {
|
|
1829
|
+
const messagesCollection = await this.getCollection(storage.TABLE_MESSAGES);
|
|
1830
|
+
const threadsCollection = await this.getCollection(storage.TABLE_THREADS);
|
|
1831
|
+
const messagesToDelete = await messagesCollection.find({ id: { $in: messageIds } }).toArray();
|
|
1832
|
+
const threadIds = [...new Set(messagesToDelete.map((m) => m.thread_id))];
|
|
1833
|
+
await messagesCollection.deleteMany({ id: { $in: messageIds } });
|
|
1834
|
+
if (threadIds.length > 0) {
|
|
1835
|
+
await threadsCollection.updateMany({ id: { $in: threadIds } }, { $set: { updatedAt: /* @__PURE__ */ new Date() } });
|
|
1836
|
+
}
|
|
1837
|
+
} catch (error$1) {
|
|
1838
|
+
throw new error.MastraError(
|
|
1839
|
+
{
|
|
1840
|
+
id: storage.createStorageErrorId("MONGODB", "DELETE_MESSAGES", "FAILED"),
|
|
1841
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1842
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1843
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
1844
|
+
},
|
|
1845
|
+
error$1
|
|
1846
|
+
);
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1441
1849
|
};
|
|
1442
|
-
var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
1443
|
-
|
|
1444
|
-
|
|
1850
|
+
var ObservabilityMongoDB = class _ObservabilityMongoDB extends storage.ObservabilityStorage {
|
|
1851
|
+
#connector;
|
|
1852
|
+
#skipDefaultIndexes;
|
|
1853
|
+
#indexes;
|
|
1854
|
+
/** Collections managed by this domain */
|
|
1855
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_SPANS];
|
|
1856
|
+
constructor(config) {
|
|
1445
1857
|
super();
|
|
1446
|
-
this
|
|
1858
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
1859
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
1860
|
+
this.#indexes = config.indexes?.filter(
|
|
1861
|
+
(idx) => _ObservabilityMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
1862
|
+
);
|
|
1863
|
+
}
|
|
1864
|
+
async getCollection(name) {
|
|
1865
|
+
return this.#connector.getCollection(name);
|
|
1866
|
+
}
|
|
1867
|
+
/**
|
|
1868
|
+
* Returns default index definitions for the observability domain collections.
|
|
1869
|
+
* These indexes optimize common query patterns for span and trace lookups.
|
|
1870
|
+
*/
|
|
1871
|
+
getDefaultIndexDefinitions() {
|
|
1872
|
+
return [
|
|
1873
|
+
{ collection: storage.TABLE_SPANS, keys: { spanId: 1, traceId: 1 }, options: { unique: true } },
|
|
1874
|
+
{ collection: storage.TABLE_SPANS, keys: { traceId: 1 } },
|
|
1875
|
+
{ collection: storage.TABLE_SPANS, keys: { parentSpanId: 1 } },
|
|
1876
|
+
{ collection: storage.TABLE_SPANS, keys: { startedAt: -1 } },
|
|
1877
|
+
{ collection: storage.TABLE_SPANS, keys: { spanType: 1 } },
|
|
1878
|
+
{ collection: storage.TABLE_SPANS, keys: { name: 1 } }
|
|
1879
|
+
];
|
|
1880
|
+
}
|
|
1881
|
+
async createDefaultIndexes() {
|
|
1882
|
+
if (this.#skipDefaultIndexes) {
|
|
1883
|
+
return;
|
|
1884
|
+
}
|
|
1885
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
1886
|
+
try {
|
|
1887
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
1888
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
1889
|
+
} catch (error) {
|
|
1890
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
/**
|
|
1895
|
+
* Creates custom user-defined indexes for this domain's collections.
|
|
1896
|
+
*/
|
|
1897
|
+
async createCustomIndexes() {
|
|
1898
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
1899
|
+
return;
|
|
1900
|
+
}
|
|
1901
|
+
for (const indexDef of this.#indexes) {
|
|
1902
|
+
try {
|
|
1903
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
1904
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
1905
|
+
} catch (error) {
|
|
1906
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
async init() {
|
|
1911
|
+
await this.createDefaultIndexes();
|
|
1912
|
+
await this.createCustomIndexes();
|
|
1913
|
+
}
|
|
1914
|
+
async dangerouslyClearAll() {
|
|
1915
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
1916
|
+
await collection.deleteMany({});
|
|
1447
1917
|
}
|
|
1448
1918
|
get tracingStrategy() {
|
|
1449
1919
|
return {
|
|
@@ -1451,7 +1921,8 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1451
1921
|
supported: ["batch-with-updates", "insert-only"]
|
|
1452
1922
|
};
|
|
1453
1923
|
}
|
|
1454
|
-
async createSpan(
|
|
1924
|
+
async createSpan(args) {
|
|
1925
|
+
const { span } = args;
|
|
1455
1926
|
try {
|
|
1456
1927
|
const startedAt = span.startedAt instanceof Date ? span.startedAt.toISOString() : span.startedAt;
|
|
1457
1928
|
const endedAt = span.endedAt instanceof Date ? span.endedAt.toISOString() : span.endedAt;
|
|
@@ -1462,7 +1933,8 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1462
1933
|
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1463
1934
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1464
1935
|
};
|
|
1465
|
-
|
|
1936
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
1937
|
+
await collection.insertOne(record);
|
|
1466
1938
|
} catch (error$1) {
|
|
1467
1939
|
throw new error.MastraError(
|
|
1468
1940
|
{
|
|
@@ -1473,60 +1945,101 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1473
1945
|
spanId: span.spanId,
|
|
1474
1946
|
traceId: span.traceId,
|
|
1475
1947
|
spanType: span.spanType,
|
|
1476
|
-
|
|
1948
|
+
name: span.name
|
|
1477
1949
|
}
|
|
1478
1950
|
},
|
|
1479
1951
|
error$1
|
|
1480
1952
|
);
|
|
1481
1953
|
}
|
|
1482
1954
|
}
|
|
1483
|
-
async
|
|
1955
|
+
async getSpan(args) {
|
|
1956
|
+
const { traceId, spanId } = args;
|
|
1484
1957
|
try {
|
|
1485
|
-
const collection = await this.
|
|
1486
|
-
const
|
|
1487
|
-
if (!
|
|
1958
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
1959
|
+
const span = await collection.findOne({ traceId, spanId });
|
|
1960
|
+
if (!span) {
|
|
1488
1961
|
return null;
|
|
1489
1962
|
}
|
|
1490
1963
|
return {
|
|
1491
|
-
|
|
1492
|
-
spans: spans.map((span) => this.transformSpanFromMongo(span))
|
|
1964
|
+
span: this.transformSpanFromMongo(span)
|
|
1493
1965
|
};
|
|
1494
1966
|
} catch (error$1) {
|
|
1495
1967
|
throw new error.MastraError(
|
|
1496
1968
|
{
|
|
1497
|
-
id: storage.createStorageErrorId("MONGODB", "
|
|
1969
|
+
id: storage.createStorageErrorId("MONGODB", "GET_SPAN", "FAILED"),
|
|
1498
1970
|
domain: error.ErrorDomain.STORAGE,
|
|
1499
1971
|
category: error.ErrorCategory.USER,
|
|
1500
|
-
details: {
|
|
1501
|
-
traceId
|
|
1502
|
-
}
|
|
1972
|
+
details: { traceId, spanId }
|
|
1503
1973
|
},
|
|
1504
1974
|
error$1
|
|
1505
1975
|
);
|
|
1506
1976
|
}
|
|
1507
1977
|
}
|
|
1508
|
-
async
|
|
1509
|
-
|
|
1510
|
-
traceId,
|
|
1511
|
-
updates
|
|
1512
|
-
}) {
|
|
1978
|
+
async getRootSpan(args) {
|
|
1979
|
+
const { traceId } = args;
|
|
1513
1980
|
try {
|
|
1514
|
-
const
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1981
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
1982
|
+
const span = await collection.findOne({ traceId, parentSpanId: null });
|
|
1983
|
+
if (!span) {
|
|
1984
|
+
return null;
|
|
1985
|
+
}
|
|
1986
|
+
return {
|
|
1987
|
+
span: this.transformSpanFromMongo(span)
|
|
1988
|
+
};
|
|
1989
|
+
} catch (error$1) {
|
|
1990
|
+
throw new error.MastraError(
|
|
1991
|
+
{
|
|
1992
|
+
id: storage.createStorageErrorId("MONGODB", "GET_ROOT_SPAN", "FAILED"),
|
|
1993
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1994
|
+
category: error.ErrorCategory.USER,
|
|
1995
|
+
details: { traceId }
|
|
1996
|
+
},
|
|
1997
|
+
error$1
|
|
1998
|
+
);
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
async getTrace(args) {
|
|
2002
|
+
const { traceId } = args;
|
|
2003
|
+
try {
|
|
2004
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
2005
|
+
const spans = await collection.find({ traceId }).sort({ startedAt: 1 }).toArray();
|
|
2006
|
+
if (!spans || spans.length === 0) {
|
|
2007
|
+
return null;
|
|
2008
|
+
}
|
|
2009
|
+
return {
|
|
2010
|
+
traceId,
|
|
2011
|
+
spans: spans.map((span) => this.transformSpanFromMongo(span))
|
|
2012
|
+
};
|
|
2013
|
+
} catch (error$1) {
|
|
2014
|
+
throw new error.MastraError(
|
|
2015
|
+
{
|
|
2016
|
+
id: storage.createStorageErrorId("MONGODB", "GET_TRACE", "FAILED"),
|
|
2017
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2018
|
+
category: error.ErrorCategory.USER,
|
|
2019
|
+
details: {
|
|
2020
|
+
traceId
|
|
2021
|
+
}
|
|
2022
|
+
},
|
|
2023
|
+
error$1
|
|
2024
|
+
);
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
async updateSpan(args) {
|
|
2028
|
+
const { traceId, spanId, updates } = args;
|
|
2029
|
+
try {
|
|
2030
|
+
const data = { ...updates };
|
|
2031
|
+
if (data.endedAt instanceof Date) {
|
|
2032
|
+
data.endedAt = data.endedAt.toISOString();
|
|
2033
|
+
}
|
|
2034
|
+
if (data.startedAt instanceof Date) {
|
|
2035
|
+
data.startedAt = data.startedAt.toISOString();
|
|
1520
2036
|
}
|
|
1521
2037
|
const updateData = {
|
|
1522
2038
|
...data,
|
|
1523
2039
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1524
2040
|
};
|
|
1525
|
-
await this.
|
|
1526
|
-
|
|
1527
|
-
keys: { spanId, traceId },
|
|
1528
|
-
data: updateData
|
|
1529
|
-
});
|
|
2041
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
2042
|
+
await collection.updateOne({ spanId, traceId }, { $set: updateData });
|
|
1530
2043
|
} catch (error$1) {
|
|
1531
2044
|
throw new error.MastraError(
|
|
1532
2045
|
{
|
|
@@ -1542,51 +2055,189 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1542
2055
|
);
|
|
1543
2056
|
}
|
|
1544
2057
|
}
|
|
1545
|
-
async
|
|
1546
|
-
filters,
|
|
1547
|
-
pagination
|
|
1548
|
-
}) {
|
|
1549
|
-
const page = pagination?.page ?? 0;
|
|
1550
|
-
const perPage = pagination?.perPage ?? 10;
|
|
1551
|
-
const { entityId, entityType, ...actualFilters } = filters || {};
|
|
2058
|
+
async listTraces(args) {
|
|
2059
|
+
const { filters, pagination, orderBy } = storage.listTracesArgsSchema.parse(args);
|
|
2060
|
+
const { page, perPage } = pagination;
|
|
1552
2061
|
try {
|
|
1553
|
-
const collection = await this.
|
|
2062
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
1554
2063
|
const mongoFilter = {
|
|
1555
|
-
parentSpanId: null
|
|
2064
|
+
parentSpanId: null
|
|
1556
2065
|
// Only get root spans for traces
|
|
1557
|
-
...actualFilters
|
|
1558
2066
|
};
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
if (
|
|
1562
|
-
|
|
2067
|
+
const andConditions = [];
|
|
2068
|
+
if (filters) {
|
|
2069
|
+
if (filters.startedAt) {
|
|
2070
|
+
const startedAtFilter = {};
|
|
2071
|
+
if (filters.startedAt.start) {
|
|
2072
|
+
startedAtFilter.$gte = filters.startedAt.start.toISOString();
|
|
2073
|
+
}
|
|
2074
|
+
if (filters.startedAt.end) {
|
|
2075
|
+
startedAtFilter.$lte = filters.startedAt.end.toISOString();
|
|
2076
|
+
}
|
|
2077
|
+
if (Object.keys(startedAtFilter).length > 0) {
|
|
2078
|
+
mongoFilter.startedAt = startedAtFilter;
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
if (filters.endedAt) {
|
|
2082
|
+
const endedAtFilter = {};
|
|
2083
|
+
if (filters.endedAt.start) {
|
|
2084
|
+
endedAtFilter.$gte = filters.endedAt.start.toISOString();
|
|
2085
|
+
}
|
|
2086
|
+
if (filters.endedAt.end) {
|
|
2087
|
+
endedAtFilter.$lte = filters.endedAt.end.toISOString();
|
|
2088
|
+
}
|
|
2089
|
+
if (Object.keys(endedAtFilter).length > 0) {
|
|
2090
|
+
andConditions.push({ endedAt: endedAtFilter });
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
if (filters.spanType !== void 0) {
|
|
2094
|
+
mongoFilter.spanType = filters.spanType;
|
|
2095
|
+
}
|
|
2096
|
+
if (filters.entityType !== void 0) {
|
|
2097
|
+
mongoFilter.entityType = filters.entityType;
|
|
2098
|
+
}
|
|
2099
|
+
if (filters.entityId !== void 0) {
|
|
2100
|
+
mongoFilter.entityId = filters.entityId;
|
|
2101
|
+
}
|
|
2102
|
+
if (filters.entityName !== void 0) {
|
|
2103
|
+
mongoFilter.entityName = filters.entityName;
|
|
1563
2104
|
}
|
|
1564
|
-
if (
|
|
1565
|
-
|
|
2105
|
+
if (filters.userId !== void 0) {
|
|
2106
|
+
mongoFilter.userId = filters.userId;
|
|
1566
2107
|
}
|
|
1567
|
-
if (
|
|
1568
|
-
mongoFilter.
|
|
2108
|
+
if (filters.organizationId !== void 0) {
|
|
2109
|
+
mongoFilter.organizationId = filters.organizationId;
|
|
2110
|
+
}
|
|
2111
|
+
if (filters.resourceId !== void 0) {
|
|
2112
|
+
mongoFilter.resourceId = filters.resourceId;
|
|
2113
|
+
}
|
|
2114
|
+
if (filters.runId !== void 0) {
|
|
2115
|
+
mongoFilter.runId = filters.runId;
|
|
2116
|
+
}
|
|
2117
|
+
if (filters.sessionId !== void 0) {
|
|
2118
|
+
mongoFilter.sessionId = filters.sessionId;
|
|
2119
|
+
}
|
|
2120
|
+
if (filters.threadId !== void 0) {
|
|
2121
|
+
mongoFilter.threadId = filters.threadId;
|
|
2122
|
+
}
|
|
2123
|
+
if (filters.requestId !== void 0) {
|
|
2124
|
+
mongoFilter.requestId = filters.requestId;
|
|
2125
|
+
}
|
|
2126
|
+
if (filters.environment !== void 0) {
|
|
2127
|
+
mongoFilter.environment = filters.environment;
|
|
2128
|
+
}
|
|
2129
|
+
if (filters.source !== void 0) {
|
|
2130
|
+
mongoFilter.source = filters.source;
|
|
2131
|
+
}
|
|
2132
|
+
if (filters.serviceName !== void 0) {
|
|
2133
|
+
mongoFilter.serviceName = filters.serviceName;
|
|
2134
|
+
}
|
|
2135
|
+
if (filters.scope != null) {
|
|
2136
|
+
for (const [key, value] of Object.entries(filters.scope)) {
|
|
2137
|
+
mongoFilter[`scope.${key}`] = value;
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
if (filters.metadata != null) {
|
|
2141
|
+
for (const [key, value] of Object.entries(filters.metadata)) {
|
|
2142
|
+
mongoFilter[`metadata.${key}`] = value;
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
if (filters.tags != null && filters.tags.length > 0) {
|
|
2146
|
+
mongoFilter.tags = { $all: filters.tags };
|
|
2147
|
+
}
|
|
2148
|
+
if (filters.status !== void 0) {
|
|
2149
|
+
switch (filters.status) {
|
|
2150
|
+
case storage.TraceStatus.ERROR:
|
|
2151
|
+
andConditions.push({ error: { $exists: true, $ne: null } });
|
|
2152
|
+
break;
|
|
2153
|
+
case storage.TraceStatus.RUNNING:
|
|
2154
|
+
andConditions.push({ endedAt: null, error: null });
|
|
2155
|
+
break;
|
|
2156
|
+
case storage.TraceStatus.SUCCESS:
|
|
2157
|
+
andConditions.push({ endedAt: { $exists: true, $ne: null }, error: null });
|
|
2158
|
+
break;
|
|
2159
|
+
}
|
|
1569
2160
|
}
|
|
1570
2161
|
}
|
|
1571
|
-
if (
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
2162
|
+
if (andConditions.length) {
|
|
2163
|
+
mongoFilter.$and = andConditions;
|
|
2164
|
+
}
|
|
2165
|
+
const sortField = orderBy.field;
|
|
2166
|
+
const sortDirection = orderBy.direction === "ASC" ? 1 : -1;
|
|
2167
|
+
if (filters?.hasChildError !== void 0) {
|
|
2168
|
+
const pipeline = [
|
|
2169
|
+
{ $match: mongoFilter },
|
|
2170
|
+
// Lookup child spans with errors for this trace
|
|
2171
|
+
{
|
|
2172
|
+
$lookup: {
|
|
2173
|
+
from: storage.TABLE_SPANS,
|
|
2174
|
+
let: { traceId: "$traceId" },
|
|
2175
|
+
pipeline: [
|
|
2176
|
+
{
|
|
2177
|
+
$match: {
|
|
2178
|
+
$expr: { $eq: ["$traceId", "$$traceId"] },
|
|
2179
|
+
error: { $exists: true, $ne: null }
|
|
2180
|
+
}
|
|
2181
|
+
},
|
|
2182
|
+
{ $limit: 1 }
|
|
2183
|
+
// Only need to know if at least one exists
|
|
2184
|
+
],
|
|
2185
|
+
as: "_errorSpans"
|
|
2186
|
+
}
|
|
2187
|
+
},
|
|
2188
|
+
// Filter based on whether error spans exist
|
|
2189
|
+
{
|
|
2190
|
+
$match: filters.hasChildError ? { _errorSpans: { $ne: [] } } : { _errorSpans: { $eq: [] } }
|
|
2191
|
+
// No children with errors
|
|
2192
|
+
}
|
|
2193
|
+
];
|
|
2194
|
+
const countResult = await collection.aggregate([...pipeline, { $count: "total" }]).toArray();
|
|
2195
|
+
const count2 = countResult[0]?.total || 0;
|
|
2196
|
+
if (count2 === 0) {
|
|
2197
|
+
return {
|
|
2198
|
+
pagination: {
|
|
2199
|
+
total: 0,
|
|
2200
|
+
page,
|
|
2201
|
+
perPage,
|
|
2202
|
+
hasMore: false
|
|
1584
2203
|
},
|
|
1585
|
-
|
|
1586
|
-
}
|
|
1587
|
-
throw error$1;
|
|
2204
|
+
spans: []
|
|
2205
|
+
};
|
|
1588
2206
|
}
|
|
1589
|
-
|
|
2207
|
+
let aggregationPipeline;
|
|
2208
|
+
if (sortField === "endedAt") {
|
|
2209
|
+
const nullSortValue = sortDirection === -1 ? 0 : 1;
|
|
2210
|
+
aggregationPipeline = [
|
|
2211
|
+
...pipeline,
|
|
2212
|
+
{
|
|
2213
|
+
$addFields: {
|
|
2214
|
+
_endedAtNull: { $cond: [{ $eq: ["$endedAt", null] }, nullSortValue, sortDirection === -1 ? 1 : 0] }
|
|
2215
|
+
}
|
|
2216
|
+
},
|
|
2217
|
+
{ $sort: { _endedAtNull: 1, [sortField]: sortDirection } },
|
|
2218
|
+
{ $skip: page * perPage },
|
|
2219
|
+
{ $limit: perPage },
|
|
2220
|
+
{ $project: { _errorSpans: 0, _endedAtNull: 0 } }
|
|
2221
|
+
];
|
|
2222
|
+
} else {
|
|
2223
|
+
aggregationPipeline = [
|
|
2224
|
+
...pipeline,
|
|
2225
|
+
{ $sort: { [sortField]: sortDirection } },
|
|
2226
|
+
{ $skip: page * perPage },
|
|
2227
|
+
{ $limit: perPage },
|
|
2228
|
+
{ $project: { _errorSpans: 0 } }
|
|
2229
|
+
];
|
|
2230
|
+
}
|
|
2231
|
+
const spans2 = await collection.aggregate(aggregationPipeline).toArray();
|
|
2232
|
+
return {
|
|
2233
|
+
pagination: {
|
|
2234
|
+
total: count2,
|
|
2235
|
+
page,
|
|
2236
|
+
perPage,
|
|
2237
|
+
hasMore: (page + 1) * perPage < count2
|
|
2238
|
+
},
|
|
2239
|
+
spans: spans2.map((span) => this.transformSpanFromMongo(span))
|
|
2240
|
+
};
|
|
1590
2241
|
}
|
|
1591
2242
|
const count = await collection.countDocuments(mongoFilter);
|
|
1592
2243
|
if (count === 0) {
|
|
@@ -1600,20 +2251,37 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1600
2251
|
spans: []
|
|
1601
2252
|
};
|
|
1602
2253
|
}
|
|
1603
|
-
|
|
2254
|
+
let spans;
|
|
2255
|
+
if (sortField === "endedAt") {
|
|
2256
|
+
const nullSortValue = sortDirection === -1 ? 0 : 1;
|
|
2257
|
+
spans = await collection.aggregate([
|
|
2258
|
+
{ $match: mongoFilter },
|
|
2259
|
+
{
|
|
2260
|
+
$addFields: {
|
|
2261
|
+
_endedAtNull: { $cond: [{ $eq: ["$endedAt", null] }, nullSortValue, sortDirection === -1 ? 1 : 0] }
|
|
2262
|
+
}
|
|
2263
|
+
},
|
|
2264
|
+
{ $sort: { _endedAtNull: 1, [sortField]: sortDirection } },
|
|
2265
|
+
{ $skip: page * perPage },
|
|
2266
|
+
{ $limit: perPage },
|
|
2267
|
+
{ $project: { _endedAtNull: 0 } }
|
|
2268
|
+
]).toArray();
|
|
2269
|
+
} else {
|
|
2270
|
+
spans = await collection.find(mongoFilter).sort({ [sortField]: sortDirection }).skip(page * perPage).limit(perPage).toArray();
|
|
2271
|
+
}
|
|
1604
2272
|
return {
|
|
1605
2273
|
pagination: {
|
|
1606
2274
|
total: count,
|
|
1607
2275
|
page,
|
|
1608
2276
|
perPage,
|
|
1609
|
-
hasMore:
|
|
2277
|
+
hasMore: (page + 1) * perPage < count
|
|
1610
2278
|
},
|
|
1611
2279
|
spans: spans.map((span) => this.transformSpanFromMongo(span))
|
|
1612
2280
|
};
|
|
1613
2281
|
} catch (error$1) {
|
|
1614
2282
|
throw new error.MastraError(
|
|
1615
2283
|
{
|
|
1616
|
-
id: storage.createStorageErrorId("MONGODB", "
|
|
2284
|
+
id: storage.createStorageErrorId("MONGODB", "LIST_TRACES", "FAILED"),
|
|
1617
2285
|
domain: error.ErrorDomain.STORAGE,
|
|
1618
2286
|
category: error.ErrorCategory.USER
|
|
1619
2287
|
},
|
|
@@ -1634,10 +2302,10 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1634
2302
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1635
2303
|
};
|
|
1636
2304
|
});
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
records
|
|
1640
|
-
}
|
|
2305
|
+
if (records.length > 0) {
|
|
2306
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
2307
|
+
await collection.insertMany(records);
|
|
2308
|
+
}
|
|
1641
2309
|
} catch (error$1) {
|
|
1642
2310
|
throw new error.MastraError(
|
|
1643
2311
|
{
|
|
@@ -1651,26 +2319,30 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1651
2319
|
}
|
|
1652
2320
|
async batchUpdateSpans(args) {
|
|
1653
2321
|
try {
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
2322
|
+
if (args.records.length === 0) {
|
|
2323
|
+
return;
|
|
2324
|
+
}
|
|
2325
|
+
const bulkOps = args.records.map((record) => {
|
|
2326
|
+
const data = { ...record.updates };
|
|
2327
|
+
if (data.endedAt instanceof Date) {
|
|
2328
|
+
data.endedAt = data.endedAt.toISOString();
|
|
2329
|
+
}
|
|
2330
|
+
if (data.startedAt instanceof Date) {
|
|
2331
|
+
data.startedAt = data.startedAt.toISOString();
|
|
2332
|
+
}
|
|
2333
|
+
const updateData = {
|
|
2334
|
+
...data,
|
|
2335
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2336
|
+
};
|
|
2337
|
+
return {
|
|
2338
|
+
updateOne: {
|
|
2339
|
+
filter: { spanId: record.spanId, traceId: record.traceId },
|
|
2340
|
+
update: { $set: updateData }
|
|
1663
2341
|
}
|
|
1664
|
-
|
|
1665
|
-
...data,
|
|
1666
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1667
|
-
};
|
|
1668
|
-
return {
|
|
1669
|
-
keys: { spanId: record.spanId, traceId: record.traceId },
|
|
1670
|
-
data: updateData
|
|
1671
|
-
};
|
|
1672
|
-
})
|
|
2342
|
+
};
|
|
1673
2343
|
});
|
|
2344
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
2345
|
+
await collection.bulkWrite(bulkOps);
|
|
1674
2346
|
} catch (error$1) {
|
|
1675
2347
|
throw new error.MastraError(
|
|
1676
2348
|
{
|
|
@@ -1684,7 +2356,7 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1684
2356
|
}
|
|
1685
2357
|
async batchDeleteTraces(args) {
|
|
1686
2358
|
try {
|
|
1687
|
-
const collection = await this.
|
|
2359
|
+
const collection = await this.getCollection(storage.TABLE_SPANS);
|
|
1688
2360
|
await collection.deleteMany({
|
|
1689
2361
|
traceId: { $in: args.traceIds }
|
|
1690
2362
|
});
|
|
@@ -1704,215 +2376,98 @@ var ObservabilityMongoDB = class extends storage.ObservabilityStorage {
|
|
|
1704
2376
|
*/
|
|
1705
2377
|
transformSpanFromMongo(doc) {
|
|
1706
2378
|
const { _id, ...span } = doc;
|
|
1707
|
-
if (span.startedAt && typeof span.startedAt === "string") {
|
|
1708
|
-
span.startedAt = span.startedAt;
|
|
1709
|
-
}
|
|
1710
|
-
if (span.endedAt && typeof span.endedAt === "string") {
|
|
1711
|
-
span.endedAt = span.endedAt;
|
|
1712
|
-
}
|
|
1713
2379
|
if (span.createdAt && typeof span.createdAt === "string") {
|
|
1714
2380
|
span.createdAt = new Date(span.createdAt);
|
|
1715
2381
|
}
|
|
1716
2382
|
if (span.updatedAt && typeof span.updatedAt === "string") {
|
|
1717
2383
|
span.updatedAt = new Date(span.updatedAt);
|
|
1718
2384
|
}
|
|
2385
|
+
if (span.startedAt && typeof span.startedAt === "string") {
|
|
2386
|
+
span.startedAt = new Date(span.startedAt);
|
|
2387
|
+
}
|
|
2388
|
+
if (span.endedAt && typeof span.endedAt === "string") {
|
|
2389
|
+
span.endedAt = new Date(span.endedAt);
|
|
2390
|
+
}
|
|
1719
2391
|
return span;
|
|
1720
2392
|
}
|
|
1721
2393
|
};
|
|
1722
|
-
|
|
2394
|
+
function transformScoreRow(row) {
|
|
2395
|
+
return storage.transformScoreRow(row, {
|
|
2396
|
+
convertTimestamps: true
|
|
2397
|
+
});
|
|
2398
|
+
}
|
|
2399
|
+
var ScoresStorageMongoDB = class _ScoresStorageMongoDB extends storage.ScoresStorage {
|
|
1723
2400
|
#connector;
|
|
2401
|
+
#skipDefaultIndexes;
|
|
2402
|
+
#indexes;
|
|
2403
|
+
/** Collections managed by this domain */
|
|
2404
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_SCORERS];
|
|
1724
2405
|
constructor(config) {
|
|
1725
2406
|
super();
|
|
1726
|
-
this.#connector = config
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
}
|
|
1731
|
-
async hasColumn(_table, _column) {
|
|
1732
|
-
return true;
|
|
1733
|
-
}
|
|
1734
|
-
async createTable() {
|
|
1735
|
-
}
|
|
1736
|
-
async alterTable(_args) {
|
|
1737
|
-
}
|
|
1738
|
-
async clearTable({ tableName }) {
|
|
1739
|
-
try {
|
|
1740
|
-
const collection = await this.getCollection(tableName);
|
|
1741
|
-
await collection.deleteMany({});
|
|
1742
|
-
} catch (error$1) {
|
|
1743
|
-
const mastraError = new error.MastraError(
|
|
1744
|
-
{
|
|
1745
|
-
id: storage.createStorageErrorId("MONGODB", "CLEAR_TABLE", "FAILED"),
|
|
1746
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1747
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1748
|
-
details: { tableName }
|
|
1749
|
-
},
|
|
1750
|
-
error$1
|
|
1751
|
-
);
|
|
1752
|
-
this.logger.error(mastraError.message);
|
|
1753
|
-
this.logger?.trackException(mastraError);
|
|
1754
|
-
throw mastraError;
|
|
1755
|
-
}
|
|
1756
|
-
}
|
|
1757
|
-
async dropTable({ tableName }) {
|
|
1758
|
-
try {
|
|
1759
|
-
const collection = await this.getCollection(tableName);
|
|
1760
|
-
await collection.drop();
|
|
1761
|
-
} catch (error$1) {
|
|
1762
|
-
if (error$1 instanceof Error && error$1.message.includes("ns not found")) {
|
|
1763
|
-
return;
|
|
1764
|
-
}
|
|
1765
|
-
throw new error.MastraError(
|
|
1766
|
-
{
|
|
1767
|
-
id: storage.createStorageErrorId("MONGODB", "DROP_TABLE", "FAILED"),
|
|
1768
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1769
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1770
|
-
details: { tableName }
|
|
1771
|
-
},
|
|
1772
|
-
error$1
|
|
1773
|
-
);
|
|
1774
|
-
}
|
|
1775
|
-
}
|
|
1776
|
-
processJsonbFields(tableName, record) {
|
|
1777
|
-
const schema = storage.TABLE_SCHEMAS[tableName];
|
|
1778
|
-
if (!schema) {
|
|
1779
|
-
return record;
|
|
1780
|
-
}
|
|
1781
|
-
return Object.fromEntries(
|
|
1782
|
-
Object.entries(schema).map(([key, value]) => {
|
|
1783
|
-
if (value.type === "jsonb" && record[key] && typeof record[key] === "string") {
|
|
1784
|
-
return [key, storage.safelyParseJSON(record[key])];
|
|
1785
|
-
}
|
|
1786
|
-
return [key, record[key]];
|
|
1787
|
-
})
|
|
2407
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
2408
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
2409
|
+
this.#indexes = config.indexes?.filter(
|
|
2410
|
+
(idx) => _ScoresStorageMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
1788
2411
|
);
|
|
1789
2412
|
}
|
|
1790
|
-
async
|
|
1791
|
-
|
|
1792
|
-
const collection = await this.getCollection(tableName);
|
|
1793
|
-
const recordToInsert = this.processJsonbFields(tableName, record);
|
|
1794
|
-
await collection.insertOne(recordToInsert);
|
|
1795
|
-
} catch (error$1) {
|
|
1796
|
-
const mastraError = new error.MastraError(
|
|
1797
|
-
{
|
|
1798
|
-
id: storage.createStorageErrorId("MONGODB", "INSERT", "FAILED"),
|
|
1799
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1800
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1801
|
-
details: { tableName }
|
|
1802
|
-
},
|
|
1803
|
-
error$1
|
|
1804
|
-
);
|
|
1805
|
-
this.logger.error(mastraError.message);
|
|
1806
|
-
this.logger?.trackException(mastraError);
|
|
1807
|
-
throw mastraError;
|
|
1808
|
-
}
|
|
2413
|
+
async getCollection(name) {
|
|
2414
|
+
return this.#connector.getCollection(name);
|
|
1809
2415
|
}
|
|
1810
|
-
|
|
1811
|
-
|
|
2416
|
+
/**
|
|
2417
|
+
* Returns default index definitions for the scores domain collections.
|
|
2418
|
+
* These indexes optimize common query patterns for score lookups.
|
|
2419
|
+
*/
|
|
2420
|
+
getDefaultIndexDefinitions() {
|
|
2421
|
+
return [
|
|
2422
|
+
{ collection: storage.TABLE_SCORERS, keys: { id: 1 }, options: { unique: true } },
|
|
2423
|
+
{ collection: storage.TABLE_SCORERS, keys: { scorerId: 1 } },
|
|
2424
|
+
{ collection: storage.TABLE_SCORERS, keys: { runId: 1 } },
|
|
2425
|
+
{ collection: storage.TABLE_SCORERS, keys: { entityId: 1, entityType: 1 } },
|
|
2426
|
+
{ collection: storage.TABLE_SCORERS, keys: { traceId: 1, spanId: 1 } },
|
|
2427
|
+
{ collection: storage.TABLE_SCORERS, keys: { createdAt: -1 } },
|
|
2428
|
+
{ collection: storage.TABLE_SCORERS, keys: { source: 1 } }
|
|
2429
|
+
];
|
|
2430
|
+
}
|
|
2431
|
+
async createDefaultIndexes() {
|
|
2432
|
+
if (this.#skipDefaultIndexes) {
|
|
1812
2433
|
return;
|
|
1813
2434
|
}
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
id: storage.createStorageErrorId("MONGODB", "BATCH_INSERT", "FAILED"),
|
|
1822
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1823
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1824
|
-
details: { tableName }
|
|
1825
|
-
},
|
|
1826
|
-
error$1
|
|
1827
|
-
);
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
async load({ tableName, keys }) {
|
|
1831
|
-
this.logger.info(`Loading ${tableName} with keys ${JSON.stringify(keys)}`);
|
|
1832
|
-
try {
|
|
1833
|
-
const collection = await this.getCollection(tableName);
|
|
1834
|
-
return await collection.find(keys).toArray();
|
|
1835
|
-
} catch (error$1) {
|
|
1836
|
-
throw new error.MastraError(
|
|
1837
|
-
{
|
|
1838
|
-
id: storage.createStorageErrorId("MONGODB", "LOAD", "FAILED"),
|
|
1839
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1840
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1841
|
-
details: { tableName }
|
|
1842
|
-
},
|
|
1843
|
-
error$1
|
|
1844
|
-
);
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1847
|
-
async update({
|
|
1848
|
-
tableName,
|
|
1849
|
-
keys,
|
|
1850
|
-
data
|
|
1851
|
-
}) {
|
|
1852
|
-
try {
|
|
1853
|
-
const collection = await this.getCollection(tableName);
|
|
1854
|
-
const processedData = this.processJsonbFields(tableName, data);
|
|
1855
|
-
const cleanData = Object.fromEntries(Object.entries(processedData).filter(([_, value]) => value !== void 0));
|
|
1856
|
-
await collection.updateOne(keys, { $set: cleanData });
|
|
1857
|
-
} catch (error$1) {
|
|
1858
|
-
throw new error.MastraError(
|
|
1859
|
-
{
|
|
1860
|
-
id: storage.createStorageErrorId("MONGODB", "UPDATE", "FAILED"),
|
|
1861
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1862
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1863
|
-
details: { tableName }
|
|
1864
|
-
},
|
|
1865
|
-
error$1
|
|
1866
|
-
);
|
|
2435
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
2436
|
+
try {
|
|
2437
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
2438
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
2439
|
+
} catch (error) {
|
|
2440
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
2441
|
+
}
|
|
1867
2442
|
}
|
|
1868
2443
|
}
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
if (!
|
|
2444
|
+
/**
|
|
2445
|
+
* Creates custom user-defined indexes for this domain's collections.
|
|
2446
|
+
*/
|
|
2447
|
+
async createCustomIndexes() {
|
|
2448
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
1874
2449
|
return;
|
|
1875
2450
|
}
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
filter: keys,
|
|
1884
|
-
update: { $set: cleanData }
|
|
1885
|
-
}
|
|
1886
|
-
};
|
|
1887
|
-
});
|
|
1888
|
-
await collection.bulkWrite(bulkOps);
|
|
1889
|
-
} catch (error$1) {
|
|
1890
|
-
throw new error.MastraError(
|
|
1891
|
-
{
|
|
1892
|
-
id: storage.createStorageErrorId("MONGODB", "BATCH_UPDATE", "FAILED"),
|
|
1893
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1894
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1895
|
-
details: { tableName }
|
|
1896
|
-
},
|
|
1897
|
-
error$1
|
|
1898
|
-
);
|
|
2451
|
+
for (const indexDef of this.#indexes) {
|
|
2452
|
+
try {
|
|
2453
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
2454
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
2455
|
+
} catch (error) {
|
|
2456
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
2457
|
+
}
|
|
1899
2458
|
}
|
|
1900
2459
|
}
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
operations;
|
|
1909
|
-
constructor({ operations }) {
|
|
1910
|
-
super();
|
|
1911
|
-
this.operations = operations;
|
|
2460
|
+
async init() {
|
|
2461
|
+
await this.createDefaultIndexes();
|
|
2462
|
+
await this.createCustomIndexes();
|
|
2463
|
+
}
|
|
2464
|
+
async dangerouslyClearAll() {
|
|
2465
|
+
const collection = await this.getCollection(storage.TABLE_SCORERS);
|
|
2466
|
+
await collection.deleteMany({});
|
|
1912
2467
|
}
|
|
1913
2468
|
async getScoreById({ id }) {
|
|
1914
2469
|
try {
|
|
1915
|
-
const collection = await this.
|
|
2470
|
+
const collection = await this.getCollection(storage.TABLE_SCORERS);
|
|
1916
2471
|
const document = await collection.findOne({ id });
|
|
1917
2472
|
if (!document) {
|
|
1918
2473
|
return null;
|
|
@@ -1941,7 +2496,7 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
|
1941
2496
|
domain: error.ErrorDomain.STORAGE,
|
|
1942
2497
|
category: error.ErrorCategory.USER,
|
|
1943
2498
|
details: {
|
|
1944
|
-
scorer: score.scorer?.id ?? "unknown",
|
|
2499
|
+
scorer: typeof score.scorer?.id === "string" ? score.scorer.id : String(score.scorer?.id ?? "unknown"),
|
|
1945
2500
|
entityId: score.entityId ?? "unknown",
|
|
1946
2501
|
entityType: score.entityType ?? "unknown",
|
|
1947
2502
|
traceId: score.traceId ?? "",
|
|
@@ -1976,7 +2531,7 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
|
1976
2531
|
createdAt,
|
|
1977
2532
|
updatedAt
|
|
1978
2533
|
};
|
|
1979
|
-
const collection = await this.
|
|
2534
|
+
const collection = await this.getCollection(storage.TABLE_SCORERS);
|
|
1980
2535
|
await collection.insertOne(dataToSave);
|
|
1981
2536
|
return { score: dataToSave };
|
|
1982
2537
|
} catch (error$1) {
|
|
@@ -2012,7 +2567,7 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
|
2012
2567
|
if (source) {
|
|
2013
2568
|
query.source = source;
|
|
2014
2569
|
}
|
|
2015
|
-
const collection = await this.
|
|
2570
|
+
const collection = await this.getCollection(storage.TABLE_SCORERS);
|
|
2016
2571
|
const total = await collection.countDocuments(query);
|
|
2017
2572
|
if (total === 0) {
|
|
2018
2573
|
return {
|
|
@@ -2061,7 +2616,7 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
|
2061
2616
|
const { page, perPage: perPageInput } = pagination;
|
|
2062
2617
|
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
2063
2618
|
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2064
|
-
const collection = await this.
|
|
2619
|
+
const collection = await this.getCollection(storage.TABLE_SCORERS);
|
|
2065
2620
|
const total = await collection.countDocuments({ runId });
|
|
2066
2621
|
if (total === 0) {
|
|
2067
2622
|
return {
|
|
@@ -2111,7 +2666,7 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
|
2111
2666
|
const { page, perPage: perPageInput } = pagination;
|
|
2112
2667
|
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
2113
2668
|
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2114
|
-
const collection = await this.
|
|
2669
|
+
const collection = await this.getCollection(storage.TABLE_SCORERS);
|
|
2115
2670
|
const total = await collection.countDocuments({ entityId, entityType });
|
|
2116
2671
|
if (total === 0) {
|
|
2117
2672
|
return {
|
|
@@ -2162,7 +2717,7 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
|
2162
2717
|
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
2163
2718
|
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2164
2719
|
const query = { traceId, spanId };
|
|
2165
|
-
const collection = await this.
|
|
2720
|
+
const collection = await this.getCollection(storage.TABLE_SCORERS);
|
|
2166
2721
|
const total = await collection.countDocuments(query);
|
|
2167
2722
|
if (total === 0) {
|
|
2168
2723
|
return {
|
|
@@ -2204,11 +2759,75 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
|
|
|
2204
2759
|
}
|
|
2205
2760
|
}
|
|
2206
2761
|
};
|
|
2207
|
-
var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
2208
|
-
|
|
2209
|
-
|
|
2762
|
+
var WorkflowsStorageMongoDB = class _WorkflowsStorageMongoDB extends storage.WorkflowsStorage {
|
|
2763
|
+
#connector;
|
|
2764
|
+
#skipDefaultIndexes;
|
|
2765
|
+
#indexes;
|
|
2766
|
+
/** Collections managed by this domain */
|
|
2767
|
+
static MANAGED_COLLECTIONS = [storage.TABLE_WORKFLOW_SNAPSHOT];
|
|
2768
|
+
constructor(config) {
|
|
2210
2769
|
super();
|
|
2211
|
-
this
|
|
2770
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
2771
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
2772
|
+
this.#indexes = config.indexes?.filter(
|
|
2773
|
+
(idx) => _WorkflowsStorageMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
2774
|
+
);
|
|
2775
|
+
}
|
|
2776
|
+
async getCollection(name) {
|
|
2777
|
+
return this.#connector.getCollection(name);
|
|
2778
|
+
}
|
|
2779
|
+
async init() {
|
|
2780
|
+
await this.createDefaultIndexes();
|
|
2781
|
+
await this.createCustomIndexes();
|
|
2782
|
+
}
|
|
2783
|
+
/**
|
|
2784
|
+
* Returns default index definitions for the workflows domain collections.
|
|
2785
|
+
*/
|
|
2786
|
+
getDefaultIndexDefinitions() {
|
|
2787
|
+
return [
|
|
2788
|
+
{ collection: storage.TABLE_WORKFLOW_SNAPSHOT, keys: { workflow_name: 1, run_id: 1 }, options: { unique: true } },
|
|
2789
|
+
{ collection: storage.TABLE_WORKFLOW_SNAPSHOT, keys: { run_id: 1 } },
|
|
2790
|
+
{ collection: storage.TABLE_WORKFLOW_SNAPSHOT, keys: { workflow_name: 1 } },
|
|
2791
|
+
{ collection: storage.TABLE_WORKFLOW_SNAPSHOT, keys: { resourceId: 1 } },
|
|
2792
|
+
{ collection: storage.TABLE_WORKFLOW_SNAPSHOT, keys: { createdAt: -1 } },
|
|
2793
|
+
{ collection: storage.TABLE_WORKFLOW_SNAPSHOT, keys: { "snapshot.status": 1 } }
|
|
2794
|
+
];
|
|
2795
|
+
}
|
|
2796
|
+
/**
|
|
2797
|
+
* Creates default indexes for optimal query performance.
|
|
2798
|
+
*/
|
|
2799
|
+
async createDefaultIndexes() {
|
|
2800
|
+
if (this.#skipDefaultIndexes) {
|
|
2801
|
+
return;
|
|
2802
|
+
}
|
|
2803
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
2804
|
+
try {
|
|
2805
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
2806
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
2807
|
+
} catch (error) {
|
|
2808
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
}
|
|
2812
|
+
/**
|
|
2813
|
+
* Creates custom user-defined indexes for this domain's collections.
|
|
2814
|
+
*/
|
|
2815
|
+
async createCustomIndexes() {
|
|
2816
|
+
if (!this.#indexes || this.#indexes.length === 0) {
|
|
2817
|
+
return;
|
|
2818
|
+
}
|
|
2819
|
+
for (const indexDef of this.#indexes) {
|
|
2820
|
+
try {
|
|
2821
|
+
const collection = await this.getCollection(indexDef.collection);
|
|
2822
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
2823
|
+
} catch (error) {
|
|
2824
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
async dangerouslyClearAll() {
|
|
2829
|
+
const collection = await this.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2830
|
+
await collection.deleteMany({});
|
|
2212
2831
|
}
|
|
2213
2832
|
updateWorkflowResults({
|
|
2214
2833
|
// workflowName,
|
|
@@ -2230,10 +2849,13 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
|
2230
2849
|
workflowName,
|
|
2231
2850
|
runId,
|
|
2232
2851
|
resourceId,
|
|
2233
|
-
snapshot
|
|
2852
|
+
snapshot,
|
|
2853
|
+
createdAt,
|
|
2854
|
+
updatedAt
|
|
2234
2855
|
}) {
|
|
2235
2856
|
try {
|
|
2236
|
-
const
|
|
2857
|
+
const now = /* @__PURE__ */ new Date();
|
|
2858
|
+
const collection = await this.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2237
2859
|
await collection.updateOne(
|
|
2238
2860
|
{ workflow_name: workflowName, run_id: runId },
|
|
2239
2861
|
{
|
|
@@ -2242,8 +2864,10 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
|
2242
2864
|
run_id: runId,
|
|
2243
2865
|
resourceId,
|
|
2244
2866
|
snapshot,
|
|
2245
|
-
|
|
2246
|
-
|
|
2867
|
+
updatedAt: updatedAt ?? now
|
|
2868
|
+
},
|
|
2869
|
+
$setOnInsert: {
|
|
2870
|
+
createdAt: createdAt ?? now
|
|
2247
2871
|
}
|
|
2248
2872
|
},
|
|
2249
2873
|
{ upsert: true }
|
|
@@ -2265,17 +2889,15 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
|
2265
2889
|
runId
|
|
2266
2890
|
}) {
|
|
2267
2891
|
try {
|
|
2268
|
-
const
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
run_id: runId
|
|
2273
|
-
}
|
|
2892
|
+
const collection = await this.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2893
|
+
const result = await collection.findOne({
|
|
2894
|
+
workflow_name: workflowName,
|
|
2895
|
+
run_id: runId
|
|
2274
2896
|
});
|
|
2275
|
-
if (!result
|
|
2897
|
+
if (!result) {
|
|
2276
2898
|
return null;
|
|
2277
2899
|
}
|
|
2278
|
-
return typeof result
|
|
2900
|
+
return typeof result.snapshot === "string" ? storage.safelyParseJSON(result.snapshot) : result.snapshot;
|
|
2279
2901
|
} catch (error$1) {
|
|
2280
2902
|
throw new error.MastraError(
|
|
2281
2903
|
{
|
|
@@ -2311,7 +2933,7 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
|
2311
2933
|
if (options.resourceId) {
|
|
2312
2934
|
query["resourceId"] = options.resourceId;
|
|
2313
2935
|
}
|
|
2314
|
-
const collection = await this.
|
|
2936
|
+
const collection = await this.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2315
2937
|
let total = 0;
|
|
2316
2938
|
let cursor = collection.find(query).sort({ createdAt: -1 });
|
|
2317
2939
|
if (options.page !== void 0 && typeof options.perPage === "number") {
|
|
@@ -2362,7 +2984,7 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
|
2362
2984
|
if (args.workflowName) {
|
|
2363
2985
|
query["workflow_name"] = args.workflowName;
|
|
2364
2986
|
}
|
|
2365
|
-
const collection = await this.
|
|
2987
|
+
const collection = await this.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2366
2988
|
const result = await collection.findOne(query);
|
|
2367
2989
|
if (!result) {
|
|
2368
2990
|
return null;
|
|
@@ -2382,7 +3004,7 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
|
2382
3004
|
}
|
|
2383
3005
|
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
2384
3006
|
try {
|
|
2385
|
-
const collection = await this.
|
|
3007
|
+
const collection = await this.getCollection(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2386
3008
|
await collection.deleteOne({ workflow_name: workflowName, run_id: runId });
|
|
2387
3009
|
} catch (error$1) {
|
|
2388
3010
|
throw new error.MastraError(
|
|
@@ -2409,49 +3031,14 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
|
|
|
2409
3031
|
workflowName: row.workflow_name,
|
|
2410
3032
|
runId: row.run_id,
|
|
2411
3033
|
snapshot: parsedSnapshot,
|
|
2412
|
-
createdAt: new Date(row.createdAt),
|
|
2413
|
-
updatedAt: new Date(row.updatedAt),
|
|
3034
|
+
createdAt: row.createdAt ? new Date(row.createdAt) : /* @__PURE__ */ new Date(),
|
|
3035
|
+
updatedAt: row.updatedAt ? new Date(row.updatedAt) : /* @__PURE__ */ new Date(),
|
|
2414
3036
|
resourceId: row.resourceId
|
|
2415
3037
|
};
|
|
2416
3038
|
}
|
|
2417
3039
|
};
|
|
2418
3040
|
|
|
2419
3041
|
// src/storage/index.ts
|
|
2420
|
-
var loadConnector = (config) => {
|
|
2421
|
-
try {
|
|
2422
|
-
if ("connectorHandler" in config) {
|
|
2423
|
-
return MongoDBConnector.fromConnectionHandler(config.connectorHandler);
|
|
2424
|
-
}
|
|
2425
|
-
} catch (error$1) {
|
|
2426
|
-
throw new error.MastraError(
|
|
2427
|
-
{
|
|
2428
|
-
id: storage.createStorageErrorId("MONGODB", "CONSTRUCTOR", "FAILED"),
|
|
2429
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2430
|
-
category: error.ErrorCategory.USER,
|
|
2431
|
-
details: { connectionHandler: true }
|
|
2432
|
-
},
|
|
2433
|
-
error$1
|
|
2434
|
-
);
|
|
2435
|
-
}
|
|
2436
|
-
try {
|
|
2437
|
-
return MongoDBConnector.fromDatabaseConfig({
|
|
2438
|
-
id: config.id,
|
|
2439
|
-
options: config.options,
|
|
2440
|
-
url: config.url,
|
|
2441
|
-
dbName: config.dbName
|
|
2442
|
-
});
|
|
2443
|
-
} catch (error$1) {
|
|
2444
|
-
throw new error.MastraError(
|
|
2445
|
-
{
|
|
2446
|
-
id: storage.createStorageErrorId("MONGODB", "CONSTRUCTOR", "FAILED"),
|
|
2447
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2448
|
-
category: error.ErrorCategory.USER,
|
|
2449
|
-
details: { url: config?.url, dbName: config?.dbName }
|
|
2450
|
-
},
|
|
2451
|
-
error$1
|
|
2452
|
-
);
|
|
2453
|
-
}
|
|
2454
|
-
};
|
|
2455
3042
|
var MongoDBStore = class extends storage.MastraStorage {
|
|
2456
3043
|
#connector;
|
|
2457
3044
|
stores;
|
|
@@ -2461,128 +3048,39 @@ var MongoDBStore = class extends storage.MastraStorage {
|
|
|
2461
3048
|
resourceWorkingMemory: true,
|
|
2462
3049
|
hasColumn: false,
|
|
2463
3050
|
createTable: false,
|
|
2464
|
-
deleteMessages:
|
|
2465
|
-
|
|
3051
|
+
deleteMessages: true,
|
|
3052
|
+
observability: true,
|
|
3053
|
+
indexManagement: false,
|
|
3054
|
+
listScoresBySpan: true,
|
|
3055
|
+
agents: true
|
|
2466
3056
|
};
|
|
2467
3057
|
}
|
|
2468
3058
|
constructor(config) {
|
|
2469
3059
|
super({ id: config.id, name: "MongoDBStore", disableInit: config.disableInit });
|
|
2470
|
-
this
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
const
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
const workflows = new WorkflowsStorageMongoDB({
|
|
2482
|
-
operations
|
|
2483
|
-
});
|
|
2484
|
-
const observability = new ObservabilityMongoDB({
|
|
2485
|
-
operations
|
|
2486
|
-
});
|
|
3060
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
3061
|
+
const domainConfig = {
|
|
3062
|
+
connector: this.#connector,
|
|
3063
|
+
skipDefaultIndexes: config.skipDefaultIndexes,
|
|
3064
|
+
indexes: config.indexes
|
|
3065
|
+
};
|
|
3066
|
+
const memory = new MemoryStorageMongoDB(domainConfig);
|
|
3067
|
+
const scores = new ScoresStorageMongoDB(domainConfig);
|
|
3068
|
+
const workflows = new WorkflowsStorageMongoDB(domainConfig);
|
|
3069
|
+
const observability = new ObservabilityMongoDB(domainConfig);
|
|
3070
|
+
const agents = new MongoDBAgentsStorage(domainConfig);
|
|
2487
3071
|
this.stores = {
|
|
2488
|
-
operations,
|
|
2489
3072
|
memory,
|
|
2490
3073
|
scores,
|
|
2491
3074
|
workflows,
|
|
2492
|
-
observability
|
|
3075
|
+
observability,
|
|
3076
|
+
agents
|
|
2493
3077
|
};
|
|
2494
3078
|
}
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
}
|
|
2501
|
-
async alterTable(_args) {
|
|
2502
|
-
return this.stores.operations.alterTable(_args);
|
|
2503
|
-
}
|
|
2504
|
-
async dropTable({ tableName }) {
|
|
2505
|
-
return this.stores.operations.dropTable({ tableName });
|
|
2506
|
-
}
|
|
2507
|
-
async clearTable({ tableName }) {
|
|
2508
|
-
return this.stores.operations.clearTable({ tableName });
|
|
2509
|
-
}
|
|
2510
|
-
async insert({ tableName, record }) {
|
|
2511
|
-
return this.stores.operations.insert({ tableName, record });
|
|
2512
|
-
}
|
|
2513
|
-
async batchInsert({ tableName, records }) {
|
|
2514
|
-
return this.stores.operations.batchInsert({ tableName, records });
|
|
2515
|
-
}
|
|
2516
|
-
async load({ tableName, keys }) {
|
|
2517
|
-
return this.stores.operations.load({ tableName, keys });
|
|
2518
|
-
}
|
|
2519
|
-
async getThreadById({ threadId }) {
|
|
2520
|
-
return this.stores.memory.getThreadById({ threadId });
|
|
2521
|
-
}
|
|
2522
|
-
async saveThread({ thread }) {
|
|
2523
|
-
return this.stores.memory.saveThread({ thread });
|
|
2524
|
-
}
|
|
2525
|
-
async updateThread({
|
|
2526
|
-
id,
|
|
2527
|
-
title,
|
|
2528
|
-
metadata
|
|
2529
|
-
}) {
|
|
2530
|
-
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2531
|
-
}
|
|
2532
|
-
async deleteThread({ threadId }) {
|
|
2533
|
-
return this.stores.memory.deleteThread({ threadId });
|
|
2534
|
-
}
|
|
2535
|
-
async listMessagesById({ messageIds }) {
|
|
2536
|
-
return this.stores.memory.listMessagesById({ messageIds });
|
|
2537
|
-
}
|
|
2538
|
-
async saveMessages(args) {
|
|
2539
|
-
return this.stores.memory.saveMessages(args);
|
|
2540
|
-
}
|
|
2541
|
-
async updateMessages(_args) {
|
|
2542
|
-
return this.stores.memory.updateMessages(_args);
|
|
2543
|
-
}
|
|
2544
|
-
async listWorkflowRuns(args) {
|
|
2545
|
-
return this.stores.workflows.listWorkflowRuns(args);
|
|
2546
|
-
}
|
|
2547
|
-
async updateWorkflowResults({
|
|
2548
|
-
workflowName,
|
|
2549
|
-
runId,
|
|
2550
|
-
stepId,
|
|
2551
|
-
result,
|
|
2552
|
-
requestContext
|
|
2553
|
-
}) {
|
|
2554
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2555
|
-
}
|
|
2556
|
-
async updateWorkflowState({
|
|
2557
|
-
workflowName,
|
|
2558
|
-
runId,
|
|
2559
|
-
opts
|
|
2560
|
-
}) {
|
|
2561
|
-
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2562
|
-
}
|
|
2563
|
-
async persistWorkflowSnapshot({
|
|
2564
|
-
workflowName,
|
|
2565
|
-
runId,
|
|
2566
|
-
resourceId,
|
|
2567
|
-
snapshot
|
|
2568
|
-
}) {
|
|
2569
|
-
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2570
|
-
}
|
|
2571
|
-
async loadWorkflowSnapshot({
|
|
2572
|
-
workflowName,
|
|
2573
|
-
runId
|
|
2574
|
-
}) {
|
|
2575
|
-
return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
2576
|
-
}
|
|
2577
|
-
async getWorkflowRunById({
|
|
2578
|
-
runId,
|
|
2579
|
-
workflowName
|
|
2580
|
-
}) {
|
|
2581
|
-
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2582
|
-
}
|
|
2583
|
-
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
2584
|
-
return this.stores.workflows.deleteWorkflowRunById({ runId, workflowName });
|
|
2585
|
-
}
|
|
3079
|
+
/**
|
|
3080
|
+
* Closes the MongoDB client connection.
|
|
3081
|
+
*
|
|
3082
|
+
* This will close the MongoDB client, including pre-configured clients.
|
|
3083
|
+
*/
|
|
2586
3084
|
async close() {
|
|
2587
3085
|
try {
|
|
2588
3086
|
await this.#connector.close();
|
|
@@ -2591,154 +3089,12 @@ var MongoDBStore = class extends storage.MastraStorage {
|
|
|
2591
3089
|
{
|
|
2592
3090
|
id: storage.createStorageErrorId("MONGODB", "CLOSE", "FAILED"),
|
|
2593
3091
|
domain: error.ErrorDomain.STORAGE,
|
|
2594
|
-
category: error.ErrorCategory.
|
|
3092
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
2595
3093
|
},
|
|
2596
3094
|
error$1
|
|
2597
3095
|
);
|
|
2598
3096
|
}
|
|
2599
3097
|
}
|
|
2600
|
-
/**
|
|
2601
|
-
* SCORERS
|
|
2602
|
-
*/
|
|
2603
|
-
async getScoreById({ id }) {
|
|
2604
|
-
return this.stores.scores.getScoreById({ id });
|
|
2605
|
-
}
|
|
2606
|
-
async saveScore(score) {
|
|
2607
|
-
return this.stores.scores.saveScore(score);
|
|
2608
|
-
}
|
|
2609
|
-
async listScoresByRunId({
|
|
2610
|
-
runId,
|
|
2611
|
-
pagination
|
|
2612
|
-
}) {
|
|
2613
|
-
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2614
|
-
}
|
|
2615
|
-
async listScoresByEntityId({
|
|
2616
|
-
entityId,
|
|
2617
|
-
entityType,
|
|
2618
|
-
pagination
|
|
2619
|
-
}) {
|
|
2620
|
-
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2621
|
-
}
|
|
2622
|
-
async listScoresByScorerId({
|
|
2623
|
-
scorerId,
|
|
2624
|
-
pagination,
|
|
2625
|
-
entityId,
|
|
2626
|
-
entityType,
|
|
2627
|
-
source
|
|
2628
|
-
}) {
|
|
2629
|
-
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2630
|
-
}
|
|
2631
|
-
async listScoresBySpan({
|
|
2632
|
-
traceId,
|
|
2633
|
-
spanId,
|
|
2634
|
-
pagination
|
|
2635
|
-
}) {
|
|
2636
|
-
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2637
|
-
}
|
|
2638
|
-
/**
|
|
2639
|
-
* RESOURCES
|
|
2640
|
-
*/
|
|
2641
|
-
async getResourceById({ resourceId }) {
|
|
2642
|
-
return this.stores.memory.getResourceById({ resourceId });
|
|
2643
|
-
}
|
|
2644
|
-
async saveResource({ resource }) {
|
|
2645
|
-
return this.stores.memory.saveResource({ resource });
|
|
2646
|
-
}
|
|
2647
|
-
async updateResource({
|
|
2648
|
-
resourceId,
|
|
2649
|
-
workingMemory,
|
|
2650
|
-
metadata
|
|
2651
|
-
}) {
|
|
2652
|
-
return this.stores.memory.updateResource({
|
|
2653
|
-
resourceId,
|
|
2654
|
-
workingMemory,
|
|
2655
|
-
metadata
|
|
2656
|
-
});
|
|
2657
|
-
}
|
|
2658
|
-
/**
|
|
2659
|
-
* Tracing/Observability
|
|
2660
|
-
*/
|
|
2661
|
-
async createSpan(span) {
|
|
2662
|
-
if (!this.stores.observability) {
|
|
2663
|
-
throw new error.MastraError({
|
|
2664
|
-
id: storage.createStorageErrorId("MONGODB", "OBSERVABILITY", "NOT_INITIALIZED"),
|
|
2665
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2666
|
-
category: error.ErrorCategory.SYSTEM,
|
|
2667
|
-
text: "Observability storage is not initialized"
|
|
2668
|
-
});
|
|
2669
|
-
}
|
|
2670
|
-
return this.stores.observability.createSpan(span);
|
|
2671
|
-
}
|
|
2672
|
-
async updateSpan({
|
|
2673
|
-
spanId,
|
|
2674
|
-
traceId,
|
|
2675
|
-
updates
|
|
2676
|
-
}) {
|
|
2677
|
-
if (!this.stores.observability) {
|
|
2678
|
-
throw new error.MastraError({
|
|
2679
|
-
id: storage.createStorageErrorId("MONGODB", "OBSERVABILITY", "NOT_INITIALIZED"),
|
|
2680
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2681
|
-
category: error.ErrorCategory.SYSTEM,
|
|
2682
|
-
text: "Observability storage is not initialized"
|
|
2683
|
-
});
|
|
2684
|
-
}
|
|
2685
|
-
return this.stores.observability.updateSpan({ spanId, traceId, updates });
|
|
2686
|
-
}
|
|
2687
|
-
async getTrace(traceId) {
|
|
2688
|
-
if (!this.stores.observability) {
|
|
2689
|
-
throw new error.MastraError({
|
|
2690
|
-
id: storage.createStorageErrorId("MONGODB", "OBSERVABILITY", "NOT_INITIALIZED"),
|
|
2691
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2692
|
-
category: error.ErrorCategory.SYSTEM,
|
|
2693
|
-
text: "Observability storage is not initialized"
|
|
2694
|
-
});
|
|
2695
|
-
}
|
|
2696
|
-
return this.stores.observability.getTrace(traceId);
|
|
2697
|
-
}
|
|
2698
|
-
async getTracesPaginated(args) {
|
|
2699
|
-
if (!this.stores.observability) {
|
|
2700
|
-
throw new error.MastraError({
|
|
2701
|
-
id: storage.createStorageErrorId("MONGODB", "OBSERVABILITY", "NOT_INITIALIZED"),
|
|
2702
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2703
|
-
category: error.ErrorCategory.SYSTEM,
|
|
2704
|
-
text: "Observability storage is not initialized"
|
|
2705
|
-
});
|
|
2706
|
-
}
|
|
2707
|
-
return this.stores.observability.getTracesPaginated(args);
|
|
2708
|
-
}
|
|
2709
|
-
async batchCreateSpans(args) {
|
|
2710
|
-
if (!this.stores.observability) {
|
|
2711
|
-
throw new error.MastraError({
|
|
2712
|
-
id: storage.createStorageErrorId("MONGODB", "OBSERVABILITY", "NOT_INITIALIZED"),
|
|
2713
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2714
|
-
category: error.ErrorCategory.SYSTEM,
|
|
2715
|
-
text: "Observability storage is not initialized"
|
|
2716
|
-
});
|
|
2717
|
-
}
|
|
2718
|
-
return this.stores.observability.batchCreateSpans(args);
|
|
2719
|
-
}
|
|
2720
|
-
async batchUpdateSpans(args) {
|
|
2721
|
-
if (!this.stores.observability) {
|
|
2722
|
-
throw new error.MastraError({
|
|
2723
|
-
id: storage.createStorageErrorId("MONGODB", "OBSERVABILITY", "NOT_INITIALIZED"),
|
|
2724
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2725
|
-
category: error.ErrorCategory.SYSTEM,
|
|
2726
|
-
text: "Observability storage is not initialized"
|
|
2727
|
-
});
|
|
2728
|
-
}
|
|
2729
|
-
return this.stores.observability.batchUpdateSpans(args);
|
|
2730
|
-
}
|
|
2731
|
-
async batchDeleteTraces(args) {
|
|
2732
|
-
if (!this.stores.observability) {
|
|
2733
|
-
throw new error.MastraError({
|
|
2734
|
-
id: storage.createStorageErrorId("MONGODB", "OBSERVABILITY", "NOT_INITIALIZED"),
|
|
2735
|
-
domain: error.ErrorDomain.STORAGE,
|
|
2736
|
-
category: error.ErrorCategory.SYSTEM,
|
|
2737
|
-
text: "Observability storage is not initialized"
|
|
2738
|
-
});
|
|
2739
|
-
}
|
|
2740
|
-
return this.stores.observability.batchDeleteTraces(args);
|
|
2741
|
-
}
|
|
2742
3098
|
};
|
|
2743
3099
|
|
|
2744
3100
|
// src/vector/prompt.ts
|