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