@mastra/dynamodb 0.0.0-playground-studio-cloud-20251031080052 → 0.0.0-refactor-agent-information-for-recomposable-ui-20251112151814
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 +397 -3
- package/dist/entities/index.d.ts +1 -1
- package/dist/entities/score.d.ts +1 -1
- package/dist/index.cjs +255 -467
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +255 -467
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +13 -41
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/operations/index.d.ts.map +1 -1
- package/dist/storage/domains/score/index.d.ts +5 -5
- package/dist/storage/domains/score/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +3 -10
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +22 -62
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +8 -5
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -19
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -6,7 +6,7 @@ var error = require('@mastra/core/error');
|
|
|
6
6
|
var storage = require('@mastra/core/storage');
|
|
7
7
|
var electrodb = require('electrodb');
|
|
8
8
|
var agent = require('@mastra/core/agent');
|
|
9
|
-
var
|
|
9
|
+
var evals = require('@mastra/core/evals');
|
|
10
10
|
|
|
11
11
|
// src/storage/index.ts
|
|
12
12
|
|
|
@@ -564,7 +564,7 @@ var scoreEntity = new electrodb.Entity({
|
|
|
564
564
|
return value;
|
|
565
565
|
}
|
|
566
566
|
},
|
|
567
|
-
|
|
567
|
+
requestContext: {
|
|
568
568
|
type: "string",
|
|
569
569
|
required: false,
|
|
570
570
|
set: (value) => {
|
|
@@ -935,187 +935,6 @@ function getElectroDbService(client, tableName) {
|
|
|
935
935
|
}
|
|
936
936
|
);
|
|
937
937
|
}
|
|
938
|
-
var LegacyEvalsDynamoDB = class extends storage.LegacyEvalsStorage {
|
|
939
|
-
service;
|
|
940
|
-
tableName;
|
|
941
|
-
constructor({ service, tableName }) {
|
|
942
|
-
super();
|
|
943
|
-
this.service = service;
|
|
944
|
-
this.tableName = tableName;
|
|
945
|
-
}
|
|
946
|
-
// Eval operations
|
|
947
|
-
async getEvalsByAgentName(agentName, type) {
|
|
948
|
-
this.logger.debug("Getting evals for agent", { agentName, type });
|
|
949
|
-
try {
|
|
950
|
-
const query = this.service.entities.eval.query.byAgent({ entity: "eval", agent_name: agentName });
|
|
951
|
-
const results = await query.go({ order: "desc", limit: 100 });
|
|
952
|
-
if (!results.data.length) {
|
|
953
|
-
return [];
|
|
954
|
-
}
|
|
955
|
-
let filteredData = results.data;
|
|
956
|
-
if (type) {
|
|
957
|
-
filteredData = filteredData.filter((evalRecord) => {
|
|
958
|
-
try {
|
|
959
|
-
const testInfo = evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0;
|
|
960
|
-
if (type === "test" && !testInfo) {
|
|
961
|
-
return false;
|
|
962
|
-
}
|
|
963
|
-
if (type === "live" && testInfo) {
|
|
964
|
-
return false;
|
|
965
|
-
}
|
|
966
|
-
} catch (e) {
|
|
967
|
-
this.logger.warn("Failed to parse test_info during filtering", { record: evalRecord, error: e });
|
|
968
|
-
}
|
|
969
|
-
return true;
|
|
970
|
-
});
|
|
971
|
-
}
|
|
972
|
-
return filteredData.map((evalRecord) => {
|
|
973
|
-
try {
|
|
974
|
-
return {
|
|
975
|
-
input: evalRecord.input,
|
|
976
|
-
output: evalRecord.output,
|
|
977
|
-
// Safely parse result and test_info
|
|
978
|
-
result: evalRecord.result && typeof evalRecord.result === "string" ? JSON.parse(evalRecord.result) : void 0,
|
|
979
|
-
agentName: evalRecord.agent_name,
|
|
980
|
-
createdAt: evalRecord.created_at,
|
|
981
|
-
// Keep as string from DDB?
|
|
982
|
-
metricName: evalRecord.metric_name,
|
|
983
|
-
instructions: evalRecord.instructions,
|
|
984
|
-
runId: evalRecord.run_id,
|
|
985
|
-
globalRunId: evalRecord.global_run_id,
|
|
986
|
-
testInfo: evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0
|
|
987
|
-
};
|
|
988
|
-
} catch (parseError) {
|
|
989
|
-
this.logger.error("Failed to parse eval record", { record: evalRecord, error: parseError });
|
|
990
|
-
return {
|
|
991
|
-
agentName: evalRecord.agent_name,
|
|
992
|
-
createdAt: evalRecord.created_at,
|
|
993
|
-
runId: evalRecord.run_id,
|
|
994
|
-
globalRunId: evalRecord.global_run_id
|
|
995
|
-
};
|
|
996
|
-
}
|
|
997
|
-
});
|
|
998
|
-
} catch (error$1) {
|
|
999
|
-
throw new error.MastraError(
|
|
1000
|
-
{
|
|
1001
|
-
id: "STORAGE_DYNAMODB_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
1002
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1003
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1004
|
-
details: { agentName }
|
|
1005
|
-
},
|
|
1006
|
-
error$1
|
|
1007
|
-
);
|
|
1008
|
-
}
|
|
1009
|
-
}
|
|
1010
|
-
async getEvals(options = {}) {
|
|
1011
|
-
const { agentName, type, page = 0, perPage = 100, dateRange } = options;
|
|
1012
|
-
this.logger.debug("Getting evals with pagination", { agentName, type, page, perPage, dateRange });
|
|
1013
|
-
try {
|
|
1014
|
-
let query;
|
|
1015
|
-
if (agentName) {
|
|
1016
|
-
query = this.service.entities.eval.query.byAgent({ entity: "eval", agent_name: agentName });
|
|
1017
|
-
} else {
|
|
1018
|
-
query = this.service.entities.eval.query.byEntity({ entity: "eval" });
|
|
1019
|
-
}
|
|
1020
|
-
const results = await query.go({
|
|
1021
|
-
order: "desc",
|
|
1022
|
-
pages: "all"
|
|
1023
|
-
// Get all pages to apply filtering and pagination
|
|
1024
|
-
});
|
|
1025
|
-
if (!results.data.length) {
|
|
1026
|
-
return {
|
|
1027
|
-
evals: [],
|
|
1028
|
-
total: 0,
|
|
1029
|
-
page,
|
|
1030
|
-
perPage,
|
|
1031
|
-
hasMore: false
|
|
1032
|
-
};
|
|
1033
|
-
}
|
|
1034
|
-
let filteredData = results.data;
|
|
1035
|
-
if (type) {
|
|
1036
|
-
filteredData = filteredData.filter((evalRecord) => {
|
|
1037
|
-
try {
|
|
1038
|
-
const testInfo = evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0;
|
|
1039
|
-
if (type === "test" && !testInfo) {
|
|
1040
|
-
return false;
|
|
1041
|
-
}
|
|
1042
|
-
if (type === "live" && testInfo) {
|
|
1043
|
-
return false;
|
|
1044
|
-
}
|
|
1045
|
-
} catch (e) {
|
|
1046
|
-
this.logger.warn("Failed to parse test_info during filtering", { record: evalRecord, error: e });
|
|
1047
|
-
}
|
|
1048
|
-
return true;
|
|
1049
|
-
});
|
|
1050
|
-
}
|
|
1051
|
-
if (dateRange) {
|
|
1052
|
-
const fromDate = dateRange.start;
|
|
1053
|
-
const toDate = dateRange.end;
|
|
1054
|
-
filteredData = filteredData.filter((evalRecord) => {
|
|
1055
|
-
const recordDate = new Date(evalRecord.created_at);
|
|
1056
|
-
if (fromDate && recordDate < fromDate) {
|
|
1057
|
-
return false;
|
|
1058
|
-
}
|
|
1059
|
-
if (toDate && recordDate > toDate) {
|
|
1060
|
-
return false;
|
|
1061
|
-
}
|
|
1062
|
-
return true;
|
|
1063
|
-
});
|
|
1064
|
-
}
|
|
1065
|
-
const total = filteredData.length;
|
|
1066
|
-
const start = page * perPage;
|
|
1067
|
-
const end = start + perPage;
|
|
1068
|
-
const paginatedData = filteredData.slice(start, end);
|
|
1069
|
-
const evals = paginatedData.map((evalRecord) => {
|
|
1070
|
-
try {
|
|
1071
|
-
return {
|
|
1072
|
-
input: evalRecord.input,
|
|
1073
|
-
output: evalRecord.output,
|
|
1074
|
-
result: evalRecord.result && typeof evalRecord.result === "string" ? JSON.parse(evalRecord.result) : void 0,
|
|
1075
|
-
agentName: evalRecord.agent_name,
|
|
1076
|
-
createdAt: evalRecord.created_at,
|
|
1077
|
-
metricName: evalRecord.metric_name,
|
|
1078
|
-
instructions: evalRecord.instructions,
|
|
1079
|
-
runId: evalRecord.run_id,
|
|
1080
|
-
globalRunId: evalRecord.global_run_id,
|
|
1081
|
-
testInfo: evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0
|
|
1082
|
-
};
|
|
1083
|
-
} catch (parseError) {
|
|
1084
|
-
this.logger.error("Failed to parse eval record", { record: evalRecord, error: parseError });
|
|
1085
|
-
return {
|
|
1086
|
-
agentName: evalRecord.agent_name,
|
|
1087
|
-
createdAt: evalRecord.created_at,
|
|
1088
|
-
runId: evalRecord.run_id,
|
|
1089
|
-
globalRunId: evalRecord.global_run_id
|
|
1090
|
-
};
|
|
1091
|
-
}
|
|
1092
|
-
});
|
|
1093
|
-
const hasMore = end < total;
|
|
1094
|
-
return {
|
|
1095
|
-
evals,
|
|
1096
|
-
total,
|
|
1097
|
-
page,
|
|
1098
|
-
perPage,
|
|
1099
|
-
hasMore
|
|
1100
|
-
};
|
|
1101
|
-
} catch (error$1) {
|
|
1102
|
-
throw new error.MastraError(
|
|
1103
|
-
{
|
|
1104
|
-
id: "STORAGE_DYNAMODB_STORE_GET_EVALS_FAILED",
|
|
1105
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1106
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1107
|
-
details: {
|
|
1108
|
-
agentName: agentName || "all",
|
|
1109
|
-
type: type || "all",
|
|
1110
|
-
page,
|
|
1111
|
-
perPage
|
|
1112
|
-
}
|
|
1113
|
-
},
|
|
1114
|
-
error$1
|
|
1115
|
-
);
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
};
|
|
1119
938
|
var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
1120
939
|
service;
|
|
1121
940
|
constructor({ service }) {
|
|
@@ -1134,17 +953,17 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1134
953
|
};
|
|
1135
954
|
}
|
|
1136
955
|
// Helper function to transform and sort threads
|
|
1137
|
-
transformAndSortThreads(rawThreads,
|
|
956
|
+
transformAndSortThreads(rawThreads, field, direction) {
|
|
1138
957
|
return rawThreads.map((data) => ({
|
|
1139
958
|
...data,
|
|
1140
959
|
// Convert date strings back to Date objects for consistency
|
|
1141
960
|
createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
|
|
1142
961
|
updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
|
|
1143
962
|
})).sort((a, b) => {
|
|
1144
|
-
const fieldA =
|
|
1145
|
-
const fieldB =
|
|
963
|
+
const fieldA = field === "createdAt" ? a.createdAt : a.updatedAt;
|
|
964
|
+
const fieldB = field === "createdAt" ? b.createdAt : b.updatedAt;
|
|
1146
965
|
const comparison = fieldA.getTime() - fieldB.getTime();
|
|
1147
|
-
return
|
|
966
|
+
return direction === "DESC" ? -comparison : comparison;
|
|
1148
967
|
});
|
|
1149
968
|
}
|
|
1150
969
|
async getThreadById({ threadId }) {
|
|
@@ -1175,32 +994,6 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1175
994
|
);
|
|
1176
995
|
}
|
|
1177
996
|
}
|
|
1178
|
-
/**
|
|
1179
|
-
* @deprecated use getThreadsByResourceIdPaginated instead for paginated results.
|
|
1180
|
-
*/
|
|
1181
|
-
async getThreadsByResourceId(args) {
|
|
1182
|
-
const resourceId = args.resourceId;
|
|
1183
|
-
const orderBy = this.castThreadOrderBy(args.orderBy);
|
|
1184
|
-
const sortDirection = this.castThreadSortDirection(args.sortDirection);
|
|
1185
|
-
this.logger.debug("Getting threads by resource ID", { resourceId, orderBy, sortDirection });
|
|
1186
|
-
try {
|
|
1187
|
-
const result = await this.service.entities.thread.query.byResource({ entity: "thread", resourceId }).go();
|
|
1188
|
-
if (!result.data.length) {
|
|
1189
|
-
return [];
|
|
1190
|
-
}
|
|
1191
|
-
return this.transformAndSortThreads(result.data, orderBy, sortDirection);
|
|
1192
|
-
} catch (error$1) {
|
|
1193
|
-
throw new error.MastraError(
|
|
1194
|
-
{
|
|
1195
|
-
id: "STORAGE_DYNAMODB_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
1196
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1197
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1198
|
-
details: { resourceId }
|
|
1199
|
-
},
|
|
1200
|
-
error$1
|
|
1201
|
-
);
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
997
|
async saveThread({ thread }) {
|
|
1205
998
|
this.logger.debug("Saving thread", { threadId: thread.id });
|
|
1206
999
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -1220,7 +1013,7 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1220
1013
|
resourceId: thread.resourceId,
|
|
1221
1014
|
title: threadData.title,
|
|
1222
1015
|
createdAt: thread.createdAt || now,
|
|
1223
|
-
updatedAt: now,
|
|
1016
|
+
updatedAt: thread.updatedAt || now,
|
|
1224
1017
|
metadata: thread.metadata
|
|
1225
1018
|
};
|
|
1226
1019
|
} catch (error$1) {
|
|
@@ -1280,7 +1073,7 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1280
1073
|
async deleteThread({ threadId }) {
|
|
1281
1074
|
this.logger.debug("Deleting thread", { threadId });
|
|
1282
1075
|
try {
|
|
1283
|
-
const messages = await this.
|
|
1076
|
+
const { messages } = await this.listMessages({ threadId, perPage: false });
|
|
1284
1077
|
if (messages.length > 0) {
|
|
1285
1078
|
const batchSize = 25;
|
|
1286
1079
|
for (let i = 0; i < messages.length; i += batchSize) {
|
|
@@ -1309,104 +1102,175 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1309
1102
|
);
|
|
1310
1103
|
}
|
|
1311
1104
|
}
|
|
1312
|
-
async
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
selectBy,
|
|
1316
|
-
format
|
|
1317
|
-
}) {
|
|
1318
|
-
this.logger.debug("Getting messages", { threadId, selectBy });
|
|
1105
|
+
async listMessagesById({ messageIds }) {
|
|
1106
|
+
this.logger.debug("Getting messages by ID", { messageIds });
|
|
1107
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
1319
1108
|
try {
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
messages.push(...includeMessages);
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
if (limit !== 0) {
|
|
1330
|
-
const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
|
|
1331
|
-
let results;
|
|
1332
|
-
if (limit !== Number.MAX_SAFE_INTEGER && limit > 0) {
|
|
1333
|
-
results = await query.go({ limit, order: "desc" });
|
|
1334
|
-
results.data = results.data.reverse();
|
|
1335
|
-
} else {
|
|
1336
|
-
results = await query.go();
|
|
1337
|
-
}
|
|
1338
|
-
let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg);
|
|
1339
|
-
allThreadMessages.sort((a, b) => {
|
|
1340
|
-
const timeA = a.createdAt.getTime();
|
|
1341
|
-
const timeB = b.createdAt.getTime();
|
|
1342
|
-
if (timeA === timeB) {
|
|
1343
|
-
return a.id.localeCompare(b.id);
|
|
1344
|
-
}
|
|
1345
|
-
return timeA - timeB;
|
|
1346
|
-
});
|
|
1347
|
-
messages.push(...allThreadMessages);
|
|
1348
|
-
}
|
|
1349
|
-
messages.sort((a, b) => {
|
|
1350
|
-
const timeA = a.createdAt.getTime();
|
|
1351
|
-
const timeB = b.createdAt.getTime();
|
|
1352
|
-
if (timeA === timeB) {
|
|
1353
|
-
return a.id.localeCompare(b.id);
|
|
1354
|
-
}
|
|
1355
|
-
return timeA - timeB;
|
|
1356
|
-
});
|
|
1357
|
-
const uniqueMessages = messages.filter(
|
|
1109
|
+
const results = await Promise.all(
|
|
1110
|
+
messageIds.map((id) => this.service.entities.message.query.primary({ entity: "message", id }).go())
|
|
1111
|
+
);
|
|
1112
|
+
const data = results.map((result) => result.data).flat(1);
|
|
1113
|
+
let parsedMessages = data.map((data2) => this.parseMessageData(data2)).filter((msg) => "content" in msg);
|
|
1114
|
+
const uniqueMessages = parsedMessages.filter(
|
|
1358
1115
|
(message, index, self) => index === self.findIndex((m) => m.id === message.id)
|
|
1359
1116
|
);
|
|
1360
|
-
const list = new agent.MessageList(
|
|
1361
|
-
|
|
1362
|
-
return list.get.all.v1();
|
|
1117
|
+
const list = new agent.MessageList().add(uniqueMessages, "memory");
|
|
1118
|
+
return { messages: list.get.all.db() };
|
|
1363
1119
|
} catch (error$1) {
|
|
1364
1120
|
throw new error.MastraError(
|
|
1365
1121
|
{
|
|
1366
|
-
id: "
|
|
1122
|
+
id: "STORAGE_DYNAMODB_STORE_LIST_MESSAGES_BY_ID_FAILED",
|
|
1367
1123
|
domain: error.ErrorDomain.STORAGE,
|
|
1368
1124
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1369
|
-
details: {
|
|
1125
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
1370
1126
|
},
|
|
1371
1127
|
error$1
|
|
1372
1128
|
);
|
|
1373
1129
|
}
|
|
1374
1130
|
}
|
|
1375
|
-
async
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
let parsedMessages = data.map((data2) => this.parseMessageData(data2)).filter((msg) => "content" in msg);
|
|
1387
|
-
const uniqueMessages = parsedMessages.filter(
|
|
1388
|
-
(message, index, self) => index === self.findIndex((m) => m.id === message.id)
|
|
1131
|
+
async listMessages(args) {
|
|
1132
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
1133
|
+
if (!threadId.trim()) {
|
|
1134
|
+
throw new error.MastraError(
|
|
1135
|
+
{
|
|
1136
|
+
id: "STORAGE_DYNAMODB_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
1137
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1138
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1139
|
+
details: { threadId }
|
|
1140
|
+
},
|
|
1141
|
+
new Error("threadId must be a non-empty string")
|
|
1389
1142
|
);
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1143
|
+
}
|
|
1144
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
1145
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1146
|
+
try {
|
|
1147
|
+
if (page < 0) {
|
|
1148
|
+
throw new error.MastraError(
|
|
1149
|
+
{
|
|
1150
|
+
id: "STORAGE_DYNAMODB_LIST_MESSAGES_INVALID_PAGE",
|
|
1151
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1152
|
+
category: error.ErrorCategory.USER,
|
|
1153
|
+
details: { page }
|
|
1154
|
+
},
|
|
1155
|
+
new Error("page must be >= 0")
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
1158
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
1159
|
+
this.logger.debug("Getting messages with listMessages", {
|
|
1160
|
+
threadId,
|
|
1161
|
+
resourceId,
|
|
1162
|
+
perPageInput,
|
|
1163
|
+
offset,
|
|
1164
|
+
perPage,
|
|
1165
|
+
page,
|
|
1166
|
+
field,
|
|
1167
|
+
direction
|
|
1168
|
+
});
|
|
1169
|
+
const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
|
|
1170
|
+
const results = await query.go();
|
|
1171
|
+
let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg && typeof msg.content === "object");
|
|
1172
|
+
if (resourceId) {
|
|
1173
|
+
allThreadMessages = allThreadMessages.filter((msg) => msg.resourceId === resourceId);
|
|
1174
|
+
}
|
|
1175
|
+
if (filter?.dateRange) {
|
|
1176
|
+
const dateRange = filter.dateRange;
|
|
1177
|
+
allThreadMessages = allThreadMessages.filter((msg) => {
|
|
1178
|
+
const createdAt = new Date(msg.createdAt).getTime();
|
|
1179
|
+
if (dateRange.start) {
|
|
1180
|
+
const startTime = dateRange.start instanceof Date ? dateRange.start.getTime() : new Date(dateRange.start).getTime();
|
|
1181
|
+
if (createdAt < startTime) return false;
|
|
1182
|
+
}
|
|
1183
|
+
if (dateRange.end) {
|
|
1184
|
+
const endTime = dateRange.end instanceof Date ? dateRange.end.getTime() : new Date(dateRange.end).getTime();
|
|
1185
|
+
if (createdAt > endTime) return false;
|
|
1186
|
+
}
|
|
1187
|
+
return true;
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1190
|
+
allThreadMessages.sort((a, b) => {
|
|
1191
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
1192
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
1193
|
+
if (aValue === bValue) {
|
|
1194
|
+
return a.id.localeCompare(b.id);
|
|
1195
|
+
}
|
|
1196
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
1197
|
+
});
|
|
1198
|
+
const total = allThreadMessages.length;
|
|
1199
|
+
const paginatedMessages = allThreadMessages.slice(offset, offset + perPage);
|
|
1200
|
+
const paginatedCount = paginatedMessages.length;
|
|
1201
|
+
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
|
|
1202
|
+
return {
|
|
1203
|
+
messages: [],
|
|
1204
|
+
total: 0,
|
|
1205
|
+
page,
|
|
1206
|
+
perPage: perPageForResponse,
|
|
1207
|
+
hasMore: false
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
1211
|
+
let includeMessages = [];
|
|
1212
|
+
if (include && include.length > 0) {
|
|
1213
|
+
const selectBy = { include };
|
|
1214
|
+
includeMessages = await this._getIncludedMessages(threadId, selectBy);
|
|
1215
|
+
for (const includeMsg of includeMessages) {
|
|
1216
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
1217
|
+
paginatedMessages.push(includeMsg);
|
|
1218
|
+
messageIds.add(includeMsg.id);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
const list = new agent.MessageList().add(paginatedMessages, "memory");
|
|
1223
|
+
let finalMessages = list.get.all.db();
|
|
1224
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
1225
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
1226
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
1227
|
+
if (aValue === bValue) {
|
|
1228
|
+
return a.id.localeCompare(b.id);
|
|
1229
|
+
}
|
|
1230
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
1231
|
+
});
|
|
1232
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
1233
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
1234
|
+
let hasMore = false;
|
|
1235
|
+
if (perPageInput !== false && !allThreadMessagesReturned) {
|
|
1236
|
+
hasMore = offset + paginatedCount < total;
|
|
1237
|
+
}
|
|
1238
|
+
return {
|
|
1239
|
+
messages: finalMessages,
|
|
1240
|
+
total,
|
|
1241
|
+
page,
|
|
1242
|
+
perPage: perPageForResponse,
|
|
1243
|
+
hasMore
|
|
1244
|
+
};
|
|
1393
1245
|
} catch (error$1) {
|
|
1394
|
-
|
|
1246
|
+
const mastraError = new error.MastraError(
|
|
1395
1247
|
{
|
|
1396
|
-
id: "
|
|
1248
|
+
id: "STORAGE_DYNAMODB_STORE_LIST_MESSAGES_FAILED",
|
|
1397
1249
|
domain: error.ErrorDomain.STORAGE,
|
|
1398
1250
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1399
|
-
details: {
|
|
1251
|
+
details: {
|
|
1252
|
+
threadId,
|
|
1253
|
+
resourceId: resourceId ?? ""
|
|
1254
|
+
}
|
|
1400
1255
|
},
|
|
1401
1256
|
error$1
|
|
1402
1257
|
);
|
|
1258
|
+
this.logger?.error?.(mastraError.toString());
|
|
1259
|
+
this.logger?.trackException?.(mastraError);
|
|
1260
|
+
return {
|
|
1261
|
+
messages: [],
|
|
1262
|
+
total: 0,
|
|
1263
|
+
page,
|
|
1264
|
+
perPage: perPageForResponse,
|
|
1265
|
+
hasMore: false
|
|
1266
|
+
};
|
|
1403
1267
|
}
|
|
1404
1268
|
}
|
|
1405
1269
|
async saveMessages(args) {
|
|
1406
|
-
const { messages
|
|
1270
|
+
const { messages } = args;
|
|
1407
1271
|
this.logger.debug("Saving messages", { count: messages.length });
|
|
1408
1272
|
if (!messages.length) {
|
|
1409
|
-
return [];
|
|
1273
|
+
return { messages: [] };
|
|
1410
1274
|
}
|
|
1411
1275
|
const threadId = messages[0]?.threadId;
|
|
1412
1276
|
if (!threadId) {
|
|
@@ -1460,8 +1324,7 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1460
1324
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1461
1325
|
}).go();
|
|
1462
1326
|
const list = new agent.MessageList().add(messages, "memory");
|
|
1463
|
-
|
|
1464
|
-
return list.get.all.v2();
|
|
1327
|
+
return { messages: list.get.all.db() };
|
|
1465
1328
|
} catch (error$1) {
|
|
1466
1329
|
throw new error.MastraError(
|
|
1467
1330
|
{
|
|
@@ -1474,37 +1337,48 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1474
1337
|
);
|
|
1475
1338
|
}
|
|
1476
1339
|
}
|
|
1477
|
-
async
|
|
1478
|
-
const { resourceId, page = 0, perPage
|
|
1479
|
-
const
|
|
1480
|
-
|
|
1340
|
+
async listThreadsByResourceId(args) {
|
|
1341
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
1342
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1343
|
+
if (page < 0) {
|
|
1344
|
+
throw new error.MastraError(
|
|
1345
|
+
{
|
|
1346
|
+
id: "STORAGE_DYNAMODB_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
1347
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1348
|
+
category: error.ErrorCategory.USER,
|
|
1349
|
+
details: { page }
|
|
1350
|
+
},
|
|
1351
|
+
new Error("page must be >= 0")
|
|
1352
|
+
);
|
|
1353
|
+
}
|
|
1354
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1355
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1481
1356
|
this.logger.debug("Getting threads by resource ID with pagination", {
|
|
1482
1357
|
resourceId,
|
|
1483
1358
|
page,
|
|
1484
1359
|
perPage,
|
|
1485
|
-
|
|
1486
|
-
|
|
1360
|
+
field,
|
|
1361
|
+
direction
|
|
1487
1362
|
});
|
|
1488
1363
|
try {
|
|
1489
1364
|
const query = this.service.entities.thread.query.byResource({ entity: "thread", resourceId });
|
|
1490
1365
|
const results = await query.go();
|
|
1491
|
-
const allThreads = this.transformAndSortThreads(results.data,
|
|
1492
|
-
const
|
|
1493
|
-
const
|
|
1494
|
-
const paginatedThreads = allThreads.slice(startIndex, endIndex);
|
|
1366
|
+
const allThreads = this.transformAndSortThreads(results.data, field, direction);
|
|
1367
|
+
const endIndex = offset + perPage;
|
|
1368
|
+
const paginatedThreads = allThreads.slice(offset, endIndex);
|
|
1495
1369
|
const total = allThreads.length;
|
|
1496
|
-
const hasMore =
|
|
1370
|
+
const hasMore = offset + perPage < total;
|
|
1497
1371
|
return {
|
|
1498
1372
|
threads: paginatedThreads,
|
|
1499
1373
|
total,
|
|
1500
1374
|
page,
|
|
1501
|
-
perPage,
|
|
1375
|
+
perPage: perPageForResponse,
|
|
1502
1376
|
hasMore
|
|
1503
1377
|
};
|
|
1504
1378
|
} catch (error$1) {
|
|
1505
1379
|
throw new error.MastraError(
|
|
1506
1380
|
{
|
|
1507
|
-
id: "
|
|
1381
|
+
id: "DYNAMODB_STORAGE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
1508
1382
|
domain: error.ErrorDomain.STORAGE,
|
|
1509
1383
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1510
1384
|
details: { resourceId, page, perPage }
|
|
@@ -1513,84 +1387,6 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
|
|
|
1513
1387
|
);
|
|
1514
1388
|
}
|
|
1515
1389
|
}
|
|
1516
|
-
async getMessagesPaginated(args) {
|
|
1517
|
-
const { threadId, resourceId, selectBy, format = "v1" } = args;
|
|
1518
|
-
const { page = 0, perPage = 40, dateRange } = selectBy?.pagination || {};
|
|
1519
|
-
const fromDate = dateRange?.start;
|
|
1520
|
-
const toDate = dateRange?.end;
|
|
1521
|
-
const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
1522
|
-
this.logger.debug("Getting messages with pagination", { threadId, page, perPage, fromDate, toDate, limit });
|
|
1523
|
-
try {
|
|
1524
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
1525
|
-
let messages = [];
|
|
1526
|
-
if (selectBy?.include?.length) {
|
|
1527
|
-
const includeMessages = await this._getIncludedMessages(threadId, selectBy);
|
|
1528
|
-
if (includeMessages) {
|
|
1529
|
-
messages.push(...includeMessages);
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
|
-
if (limit !== 0) {
|
|
1533
|
-
const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
|
|
1534
|
-
let results;
|
|
1535
|
-
if (limit !== Number.MAX_SAFE_INTEGER && limit > 0) {
|
|
1536
|
-
results = await query.go({ limit, order: "desc" });
|
|
1537
|
-
results.data = results.data.reverse();
|
|
1538
|
-
} else {
|
|
1539
|
-
results = await query.go();
|
|
1540
|
-
}
|
|
1541
|
-
let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg);
|
|
1542
|
-
allThreadMessages.sort((a, b) => {
|
|
1543
|
-
const timeA = a.createdAt.getTime();
|
|
1544
|
-
const timeB = b.createdAt.getTime();
|
|
1545
|
-
if (timeA === timeB) {
|
|
1546
|
-
return a.id.localeCompare(b.id);
|
|
1547
|
-
}
|
|
1548
|
-
return timeA - timeB;
|
|
1549
|
-
});
|
|
1550
|
-
const excludeIds = messages.map((m) => m.id);
|
|
1551
|
-
if (excludeIds.length > 0) {
|
|
1552
|
-
allThreadMessages = allThreadMessages.filter((msg) => !excludeIds.includes(msg.id));
|
|
1553
|
-
}
|
|
1554
|
-
messages.push(...allThreadMessages);
|
|
1555
|
-
}
|
|
1556
|
-
messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
1557
|
-
if (fromDate || toDate) {
|
|
1558
|
-
messages = messages.filter((msg) => {
|
|
1559
|
-
const createdAt = new Date(msg.createdAt).getTime();
|
|
1560
|
-
if (fromDate && createdAt < new Date(fromDate).getTime()) return false;
|
|
1561
|
-
if (toDate && createdAt > new Date(toDate).getTime()) return false;
|
|
1562
|
-
return true;
|
|
1563
|
-
});
|
|
1564
|
-
}
|
|
1565
|
-
const total = messages.length;
|
|
1566
|
-
const start = page * perPage;
|
|
1567
|
-
const end = start + perPage;
|
|
1568
|
-
const paginatedMessages = messages.slice(start, end);
|
|
1569
|
-
const hasMore = end < total;
|
|
1570
|
-
const list = new agent.MessageList({ threadId, resourceId }).add(paginatedMessages, "memory");
|
|
1571
|
-
const finalMessages = format === "v2" ? list.get.all.v2() : list.get.all.v1();
|
|
1572
|
-
return {
|
|
1573
|
-
messages: finalMessages,
|
|
1574
|
-
total,
|
|
1575
|
-
page,
|
|
1576
|
-
perPage,
|
|
1577
|
-
hasMore
|
|
1578
|
-
};
|
|
1579
|
-
} catch (error$1) {
|
|
1580
|
-
const mastraError = new error.MastraError(
|
|
1581
|
-
{
|
|
1582
|
-
id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_PAGINATED_FAILED",
|
|
1583
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1584
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1585
|
-
details: { threadId, resourceId: resourceId ?? "" }
|
|
1586
|
-
},
|
|
1587
|
-
error$1
|
|
1588
|
-
);
|
|
1589
|
-
this.logger?.trackException?.(mastraError);
|
|
1590
|
-
this.logger?.error?.(mastraError.toString());
|
|
1591
|
-
return { messages: [], total: 0, page, perPage, hasMore: false };
|
|
1592
|
-
}
|
|
1593
|
-
}
|
|
1594
1390
|
// Helper method to get included messages with context
|
|
1595
1391
|
async _getIncludedMessages(threadId, selectBy) {
|
|
1596
1392
|
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
@@ -1857,11 +1653,10 @@ var StoreOperationsDynamoDB = class extends storage.StoreOperations {
|
|
|
1857
1653
|
[storage.TABLE_THREADS]: "thread",
|
|
1858
1654
|
[storage.TABLE_MESSAGES]: "message",
|
|
1859
1655
|
[storage.TABLE_WORKFLOW_SNAPSHOT]: "workflow_snapshot",
|
|
1860
|
-
[storage.TABLE_EVALS]: "eval",
|
|
1861
1656
|
[storage.TABLE_SCORERS]: "score",
|
|
1862
1657
|
[storage.TABLE_TRACES]: "trace",
|
|
1863
1658
|
[storage.TABLE_RESOURCES]: "resource",
|
|
1864
|
-
[storage.
|
|
1659
|
+
[storage.TABLE_SPANS]: "ai_span"
|
|
1865
1660
|
};
|
|
1866
1661
|
return mapping[tableName] || null;
|
|
1867
1662
|
}
|
|
@@ -2194,7 +1989,7 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2194
1989
|
async saveScore(score) {
|
|
2195
1990
|
let validatedScore;
|
|
2196
1991
|
try {
|
|
2197
|
-
validatedScore =
|
|
1992
|
+
validatedScore = evals.saveScorePayloadSchema.parse(score);
|
|
2198
1993
|
} catch (error$1) {
|
|
2199
1994
|
throw new error.MastraError(
|
|
2200
1995
|
{
|
|
@@ -2226,7 +2021,7 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2226
2021
|
input: typeof validatedScore.input === "string" ? validatedScore.input : JSON.stringify(validatedScore.input),
|
|
2227
2022
|
output: typeof validatedScore.output === "string" ? validatedScore.output : JSON.stringify(validatedScore.output),
|
|
2228
2023
|
additionalContext: typeof validatedScore.additionalContext === "string" ? validatedScore.additionalContext : JSON.stringify(validatedScore.additionalContext),
|
|
2229
|
-
|
|
2024
|
+
requestContext: typeof validatedScore.requestContext === "string" ? validatedScore.requestContext : JSON.stringify(validatedScore.requestContext),
|
|
2230
2025
|
entityType: validatedScore.entityType,
|
|
2231
2026
|
entityData: typeof validatedScore.entity === "string" ? validatedScore.entity : JSON.stringify(validatedScore.entity),
|
|
2232
2027
|
entityId: validatedScore.entityId,
|
|
@@ -2257,7 +2052,7 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2257
2052
|
);
|
|
2258
2053
|
}
|
|
2259
2054
|
}
|
|
2260
|
-
async
|
|
2055
|
+
async listScoresByScorerId({
|
|
2261
2056
|
scorerId,
|
|
2262
2057
|
pagination,
|
|
2263
2058
|
entityId,
|
|
@@ -2278,18 +2073,19 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2278
2073
|
allScores = allScores.filter((score) => score.source === source);
|
|
2279
2074
|
}
|
|
2280
2075
|
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2281
|
-
const
|
|
2282
|
-
const
|
|
2283
|
-
const
|
|
2076
|
+
const { page, perPage: perPageInput } = pagination;
|
|
2077
|
+
const perPage = storage.normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
|
|
2078
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2284
2079
|
const total = allScores.length;
|
|
2285
|
-
const
|
|
2080
|
+
const end = perPageInput === false ? allScores.length : start + perPage;
|
|
2081
|
+
const paginatedScores = allScores.slice(start, end);
|
|
2286
2082
|
return {
|
|
2287
2083
|
scores: paginatedScores,
|
|
2288
2084
|
pagination: {
|
|
2289
2085
|
total,
|
|
2290
|
-
page
|
|
2291
|
-
perPage:
|
|
2292
|
-
hasMore
|
|
2086
|
+
page,
|
|
2087
|
+
perPage: perPageForResponse,
|
|
2088
|
+
hasMore: end < total
|
|
2293
2089
|
}
|
|
2294
2090
|
};
|
|
2295
2091
|
} catch (error$1) {
|
|
@@ -2311,7 +2107,7 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2311
2107
|
);
|
|
2312
2108
|
}
|
|
2313
2109
|
}
|
|
2314
|
-
async
|
|
2110
|
+
async listScoresByRunId({
|
|
2315
2111
|
runId,
|
|
2316
2112
|
pagination
|
|
2317
2113
|
}) {
|
|
@@ -2321,18 +2117,19 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2321
2117
|
const results = await query.go();
|
|
2322
2118
|
const allScores = results.data.map((data) => this.parseScoreData(data));
|
|
2323
2119
|
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2324
|
-
const
|
|
2325
|
-
const
|
|
2326
|
-
const
|
|
2120
|
+
const { page, perPage: perPageInput } = pagination;
|
|
2121
|
+
const perPage = storage.normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
|
|
2122
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2327
2123
|
const total = allScores.length;
|
|
2328
|
-
const
|
|
2124
|
+
const end = perPageInput === false ? allScores.length : start + perPage;
|
|
2125
|
+
const paginatedScores = allScores.slice(start, end);
|
|
2329
2126
|
return {
|
|
2330
2127
|
scores: paginatedScores,
|
|
2331
2128
|
pagination: {
|
|
2332
2129
|
total,
|
|
2333
|
-
page
|
|
2334
|
-
perPage:
|
|
2335
|
-
hasMore
|
|
2130
|
+
page,
|
|
2131
|
+
perPage: perPageForResponse,
|
|
2132
|
+
hasMore: end < total
|
|
2336
2133
|
}
|
|
2337
2134
|
};
|
|
2338
2135
|
} catch (error$1) {
|
|
@@ -2347,7 +2144,7 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2347
2144
|
);
|
|
2348
2145
|
}
|
|
2349
2146
|
}
|
|
2350
|
-
async
|
|
2147
|
+
async listScoresByEntityId({
|
|
2351
2148
|
entityId,
|
|
2352
2149
|
entityType,
|
|
2353
2150
|
pagination
|
|
@@ -2359,18 +2156,19 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2359
2156
|
let allScores = results.data.map((data) => this.parseScoreData(data));
|
|
2360
2157
|
allScores = allScores.filter((score) => score.entityType === entityType);
|
|
2361
2158
|
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2362
|
-
const
|
|
2363
|
-
const
|
|
2364
|
-
const
|
|
2159
|
+
const { page, perPage: perPageInput } = pagination;
|
|
2160
|
+
const perPage = storage.normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
|
|
2161
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2365
2162
|
const total = allScores.length;
|
|
2366
|
-
const
|
|
2163
|
+
const end = perPageInput === false ? allScores.length : start + perPage;
|
|
2164
|
+
const paginatedScores = allScores.slice(start, end);
|
|
2367
2165
|
return {
|
|
2368
2166
|
scores: paginatedScores,
|
|
2369
2167
|
pagination: {
|
|
2370
2168
|
total,
|
|
2371
|
-
page
|
|
2372
|
-
perPage:
|
|
2373
|
-
hasMore
|
|
2169
|
+
page,
|
|
2170
|
+
perPage: perPageForResponse,
|
|
2171
|
+
hasMore: end < total
|
|
2374
2172
|
}
|
|
2375
2173
|
};
|
|
2376
2174
|
} catch (error$1) {
|
|
@@ -2385,7 +2183,7 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2385
2183
|
);
|
|
2386
2184
|
}
|
|
2387
2185
|
}
|
|
2388
|
-
async
|
|
2186
|
+
async listScoresBySpan({
|
|
2389
2187
|
traceId,
|
|
2390
2188
|
spanId,
|
|
2391
2189
|
pagination
|
|
@@ -2396,18 +2194,19 @@ var ScoresStorageDynamoDB = class extends storage.ScoresStorage {
|
|
|
2396
2194
|
const results = await query.go();
|
|
2397
2195
|
const allScores = results.data.map((data) => this.parseScoreData(data));
|
|
2398
2196
|
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2399
|
-
const
|
|
2400
|
-
const
|
|
2401
|
-
const
|
|
2197
|
+
const { page, perPage: perPageInput } = pagination;
|
|
2198
|
+
const perPage = storage.normalizePerPage(perPageInput, Number.MAX_SAFE_INTEGER);
|
|
2199
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2402
2200
|
const total = allScores.length;
|
|
2403
|
-
const
|
|
2201
|
+
const end = perPageInput === false ? allScores.length : start + perPage;
|
|
2202
|
+
const paginatedScores = allScores.slice(start, end);
|
|
2404
2203
|
return {
|
|
2405
2204
|
scores: paginatedScores,
|
|
2406
2205
|
pagination: {
|
|
2407
2206
|
total,
|
|
2408
|
-
page
|
|
2409
|
-
perPage:
|
|
2410
|
-
hasMore
|
|
2207
|
+
page,
|
|
2208
|
+
perPage: perPageForResponse,
|
|
2209
|
+
hasMore: end < total
|
|
2411
2210
|
}
|
|
2412
2211
|
};
|
|
2413
2212
|
} catch (error$1) {
|
|
@@ -2444,7 +2243,7 @@ var WorkflowStorageDynamoDB = class extends storage.WorkflowsStorage {
|
|
|
2444
2243
|
// runId,
|
|
2445
2244
|
// stepId,
|
|
2446
2245
|
// result,
|
|
2447
|
-
//
|
|
2246
|
+
// requestContext,
|
|
2448
2247
|
}) {
|
|
2449
2248
|
throw new Error("Method not implemented.");
|
|
2450
2249
|
}
|
|
@@ -2471,7 +2270,6 @@ var WorkflowStorageDynamoDB = class extends storage.WorkflowsStorage {
|
|
|
2471
2270
|
workflow_name: workflowName,
|
|
2472
2271
|
run_id: runId,
|
|
2473
2272
|
snapshot: JSON.stringify(snapshot),
|
|
2474
|
-
// Stringify the snapshot object
|
|
2475
2273
|
createdAt: now,
|
|
2476
2274
|
updatedAt: now,
|
|
2477
2275
|
resourceId
|
|
@@ -2517,11 +2315,24 @@ var WorkflowStorageDynamoDB = class extends storage.WorkflowsStorage {
|
|
|
2517
2315
|
);
|
|
2518
2316
|
}
|
|
2519
2317
|
}
|
|
2520
|
-
async
|
|
2318
|
+
async listWorkflowRuns(args) {
|
|
2521
2319
|
this.logger.debug("Getting workflow runs", { args });
|
|
2522
2320
|
try {
|
|
2523
|
-
const
|
|
2524
|
-
const
|
|
2321
|
+
const perPage = args?.perPage !== void 0 ? args.perPage : 10;
|
|
2322
|
+
const page = args?.page !== void 0 ? args.page : 0;
|
|
2323
|
+
if (page < 0) {
|
|
2324
|
+
throw new error.MastraError(
|
|
2325
|
+
{
|
|
2326
|
+
id: "DYNAMODB_STORE_INVALID_PAGE",
|
|
2327
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2328
|
+
category: error.ErrorCategory.USER,
|
|
2329
|
+
details: { page }
|
|
2330
|
+
},
|
|
2331
|
+
new Error("page must be >= 0")
|
|
2332
|
+
);
|
|
2333
|
+
}
|
|
2334
|
+
const normalizedPerPage = storage.normalizePerPage(perPage, 10);
|
|
2335
|
+
const offset = page * normalizedPerPage;
|
|
2525
2336
|
let query;
|
|
2526
2337
|
if (args?.workflowName) {
|
|
2527
2338
|
query = this.service.entities.workflow_snapshot.query.primary({
|
|
@@ -2543,6 +2354,11 @@ var WorkflowStorageDynamoDB = class extends storage.WorkflowsStorage {
|
|
|
2543
2354
|
});
|
|
2544
2355
|
if (pageResults.data && pageResults.data.length > 0) {
|
|
2545
2356
|
let pageFilteredData = pageResults.data;
|
|
2357
|
+
if (args?.status) {
|
|
2358
|
+
pageFilteredData = pageFilteredData.filter((snapshot) => {
|
|
2359
|
+
return snapshot.snapshot.status === args.status;
|
|
2360
|
+
});
|
|
2361
|
+
}
|
|
2546
2362
|
if (args?.fromDate || args?.toDate) {
|
|
2547
2363
|
pageFilteredData = pageFilteredData.filter((snapshot) => {
|
|
2548
2364
|
const createdAt = new Date(snapshot.createdAt);
|
|
@@ -2568,7 +2384,7 @@ var WorkflowStorageDynamoDB = class extends storage.WorkflowsStorage {
|
|
|
2568
2384
|
return { runs: [], total: 0 };
|
|
2569
2385
|
}
|
|
2570
2386
|
const total = allMatchingSnapshots.length;
|
|
2571
|
-
const paginatedData = allMatchingSnapshots.slice(offset, offset +
|
|
2387
|
+
const paginatedData = allMatchingSnapshots.slice(offset, offset + normalizedPerPage);
|
|
2572
2388
|
const runs = paginatedData.map((snapshot) => formatWorkflowRun(snapshot));
|
|
2573
2389
|
return {
|
|
2574
2390
|
runs,
|
|
@@ -2577,7 +2393,7 @@ var WorkflowStorageDynamoDB = class extends storage.WorkflowsStorage {
|
|
|
2577
2393
|
} catch (error$1) {
|
|
2578
2394
|
throw new error.MastraError(
|
|
2579
2395
|
{
|
|
2580
|
-
id: "
|
|
2396
|
+
id: "STORAGE_DYNAMODB_STORE_LIST_WORKFLOW_RUNS_FAILED",
|
|
2581
2397
|
domain: error.ErrorDomain.STORAGE,
|
|
2582
2398
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2583
2399
|
details: { workflowName: args?.workflowName || "", resourceId: args?.resourceId || "" }
|
|
@@ -2650,7 +2466,7 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2650
2466
|
hasInitialized = null;
|
|
2651
2467
|
stores;
|
|
2652
2468
|
constructor({ name, config }) {
|
|
2653
|
-
super({ name });
|
|
2469
|
+
super({ id: config.id, name });
|
|
2654
2470
|
try {
|
|
2655
2471
|
if (!config.tableName || typeof config.tableName !== "string" || config.tableName.trim() === "") {
|
|
2656
2472
|
throw new Error("DynamoDBStore: config.tableName must be provided and cannot be empty.");
|
|
@@ -2678,7 +2494,6 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2678
2494
|
const scores = new ScoresStorageDynamoDB({ service: this.service });
|
|
2679
2495
|
this.stores = {
|
|
2680
2496
|
operations,
|
|
2681
|
-
legacyEvals: new LegacyEvalsDynamoDB({ service: this.service, tableName: this.tableName }),
|
|
2682
2497
|
workflows,
|
|
2683
2498
|
memory,
|
|
2684
2499
|
scores
|
|
@@ -2701,7 +2516,7 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2701
2516
|
hasColumn: false,
|
|
2702
2517
|
createTable: false,
|
|
2703
2518
|
deleteMessages: false,
|
|
2704
|
-
|
|
2519
|
+
listScoresBySpan: true
|
|
2705
2520
|
};
|
|
2706
2521
|
}
|
|
2707
2522
|
/**
|
|
@@ -2796,9 +2611,6 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2796
2611
|
async getThreadById({ threadId }) {
|
|
2797
2612
|
return this.stores.memory.getThreadById({ threadId });
|
|
2798
2613
|
}
|
|
2799
|
-
async getThreadsByResourceId(args) {
|
|
2800
|
-
return this.stores.memory.getThreadsByResourceId(args);
|
|
2801
|
-
}
|
|
2802
2614
|
async saveThread({ thread }) {
|
|
2803
2615
|
return this.stores.memory.saveThread({ thread });
|
|
2804
2616
|
}
|
|
@@ -2812,29 +2624,12 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2812
2624
|
async deleteThread({ threadId }) {
|
|
2813
2625
|
return this.stores.memory.deleteThread({ threadId });
|
|
2814
2626
|
}
|
|
2815
|
-
async
|
|
2816
|
-
|
|
2817
|
-
resourceId,
|
|
2818
|
-
selectBy,
|
|
2819
|
-
format
|
|
2820
|
-
}) {
|
|
2821
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format });
|
|
2822
|
-
}
|
|
2823
|
-
async getMessagesById({
|
|
2824
|
-
messageIds,
|
|
2825
|
-
format
|
|
2826
|
-
}) {
|
|
2827
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2627
|
+
async listMessagesById(args) {
|
|
2628
|
+
return this.stores.memory.listMessagesById(args);
|
|
2828
2629
|
}
|
|
2829
2630
|
async saveMessages(args) {
|
|
2830
2631
|
return this.stores.memory.saveMessages(args);
|
|
2831
2632
|
}
|
|
2832
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2833
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2834
|
-
}
|
|
2835
|
-
async getMessagesPaginated(args) {
|
|
2836
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2837
|
-
}
|
|
2838
2633
|
async updateMessages(_args) {
|
|
2839
2634
|
return this.stores.memory.updateMessages(_args);
|
|
2840
2635
|
}
|
|
@@ -2844,9 +2639,9 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2844
2639
|
runId,
|
|
2845
2640
|
stepId,
|
|
2846
2641
|
result,
|
|
2847
|
-
|
|
2642
|
+
requestContext
|
|
2848
2643
|
}) {
|
|
2849
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2644
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2850
2645
|
}
|
|
2851
2646
|
async updateWorkflowState({
|
|
2852
2647
|
workflowName,
|
|
@@ -2869,8 +2664,8 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2869
2664
|
}) {
|
|
2870
2665
|
return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
2871
2666
|
}
|
|
2872
|
-
async
|
|
2873
|
-
return this.stores.workflows.
|
|
2667
|
+
async listWorkflowRuns(args) {
|
|
2668
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2874
2669
|
}
|
|
2875
2670
|
async getWorkflowRunById(args) {
|
|
2876
2671
|
return this.stores.workflows.getWorkflowRunById(args);
|
|
@@ -2888,13 +2683,6 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2888
2683
|
}) {
|
|
2889
2684
|
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
2890
2685
|
}
|
|
2891
|
-
// Eval operations
|
|
2892
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2893
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2894
|
-
}
|
|
2895
|
-
async getEvals(options) {
|
|
2896
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2897
|
-
}
|
|
2898
2686
|
/**
|
|
2899
2687
|
* Closes the DynamoDB client connection and cleans up resources.
|
|
2900
2688
|
* Should be called when the store is no longer needed, e.g., at the end of tests or application shutdown.
|
|
@@ -2924,38 +2712,38 @@ var DynamoDBStore = class extends storage.MastraStorage {
|
|
|
2924
2712
|
async saveScore(_score) {
|
|
2925
2713
|
return this.stores.scores.saveScore(_score);
|
|
2926
2714
|
}
|
|
2927
|
-
async
|
|
2715
|
+
async listScoresByRunId({
|
|
2928
2716
|
runId: _runId,
|
|
2929
2717
|
pagination: _pagination
|
|
2930
2718
|
}) {
|
|
2931
|
-
return this.stores.scores.
|
|
2719
|
+
return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2932
2720
|
}
|
|
2933
|
-
async
|
|
2721
|
+
async listScoresByEntityId({
|
|
2934
2722
|
entityId: _entityId,
|
|
2935
2723
|
entityType: _entityType,
|
|
2936
2724
|
pagination: _pagination
|
|
2937
2725
|
}) {
|
|
2938
|
-
return this.stores.scores.
|
|
2726
|
+
return this.stores.scores.listScoresByEntityId({
|
|
2939
2727
|
entityId: _entityId,
|
|
2940
2728
|
entityType: _entityType,
|
|
2941
2729
|
pagination: _pagination
|
|
2942
2730
|
});
|
|
2943
2731
|
}
|
|
2944
|
-
async
|
|
2732
|
+
async listScoresByScorerId({
|
|
2945
2733
|
scorerId,
|
|
2946
2734
|
source,
|
|
2947
2735
|
entityId,
|
|
2948
2736
|
entityType,
|
|
2949
2737
|
pagination
|
|
2950
2738
|
}) {
|
|
2951
|
-
return this.stores.scores.
|
|
2739
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, source, entityId, entityType, pagination });
|
|
2952
2740
|
}
|
|
2953
|
-
async
|
|
2741
|
+
async listScoresBySpan({
|
|
2954
2742
|
traceId,
|
|
2955
2743
|
spanId,
|
|
2956
2744
|
pagination
|
|
2957
2745
|
}) {
|
|
2958
|
-
return this.stores.scores.
|
|
2746
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2959
2747
|
}
|
|
2960
2748
|
};
|
|
2961
2749
|
|