@mastra/cloudflare-d1 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-safe-stringify-telemetry-20251205024938
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 +45 -13
- package/dist/index.cjs +346 -196
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +347 -197
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/legacy-evals/index.d.ts +20 -0
- package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +7 -8
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/traces/index.d.ts +18 -0
- package/dist/storage/domains/traces/index.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +3 -3
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +38 -11
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +13 -6
package/dist/index.cjs
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
var error = require('@mastra/core/error');
|
|
4
4
|
var storage = require('@mastra/core/storage');
|
|
5
5
|
var Cloudflare = require('cloudflare');
|
|
6
|
-
var agent = require('@mastra/core/agent');
|
|
7
6
|
var utils = require('@mastra/core/utils');
|
|
7
|
+
var agent = require('@mastra/core/agent');
|
|
8
8
|
var scores = require('@mastra/core/scores');
|
|
9
9
|
|
|
10
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -248,6 +248,16 @@ function isArrayOfRecords(value) {
|
|
|
248
248
|
}
|
|
249
249
|
function deserializeValue(value, type) {
|
|
250
250
|
if (value === null || value === void 0) return null;
|
|
251
|
+
if (type === "date" && typeof value === "string") {
|
|
252
|
+
return new Date(value);
|
|
253
|
+
}
|
|
254
|
+
if (type === "jsonb" && typeof value === "string") {
|
|
255
|
+
try {
|
|
256
|
+
return JSON.parse(value);
|
|
257
|
+
} catch {
|
|
258
|
+
return value;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
251
261
|
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
252
262
|
try {
|
|
253
263
|
return JSON.parse(value);
|
|
@@ -258,7 +268,155 @@ function deserializeValue(value, type) {
|
|
|
258
268
|
return value;
|
|
259
269
|
}
|
|
260
270
|
|
|
261
|
-
// src/storage/domains/
|
|
271
|
+
// src/storage/domains/legacy-evals/index.ts
|
|
272
|
+
var LegacyEvalsStorageD1 = class extends storage.LegacyEvalsStorage {
|
|
273
|
+
operations;
|
|
274
|
+
constructor({ operations }) {
|
|
275
|
+
super();
|
|
276
|
+
this.operations = operations;
|
|
277
|
+
}
|
|
278
|
+
async getEvals(options) {
|
|
279
|
+
const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
|
|
280
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
|
|
281
|
+
const conditions = [];
|
|
282
|
+
const queryParams = [];
|
|
283
|
+
if (agentName) {
|
|
284
|
+
conditions.push(`agent_name = ?`);
|
|
285
|
+
queryParams.push(agentName);
|
|
286
|
+
}
|
|
287
|
+
if (type === "test") {
|
|
288
|
+
conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
|
|
289
|
+
} else if (type === "live") {
|
|
290
|
+
conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
|
|
291
|
+
}
|
|
292
|
+
if (dateRange?.start) {
|
|
293
|
+
conditions.push(`created_at >= ?`);
|
|
294
|
+
queryParams.push(storage.serializeDate(dateRange.start));
|
|
295
|
+
}
|
|
296
|
+
if (dateRange?.end) {
|
|
297
|
+
conditions.push(`created_at <= ?`);
|
|
298
|
+
queryParams.push(storage.serializeDate(dateRange.end));
|
|
299
|
+
}
|
|
300
|
+
const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
|
|
301
|
+
if (conditions.length > 0) {
|
|
302
|
+
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
303
|
+
}
|
|
304
|
+
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
305
|
+
try {
|
|
306
|
+
const countResult = await this.operations.executeQuery({
|
|
307
|
+
sql: countSql,
|
|
308
|
+
params: countParams,
|
|
309
|
+
first: true
|
|
310
|
+
});
|
|
311
|
+
const total = Number(countResult?.count || 0);
|
|
312
|
+
const currentOffset = page * perPage;
|
|
313
|
+
if (total === 0) {
|
|
314
|
+
return {
|
|
315
|
+
evals: [],
|
|
316
|
+
total: 0,
|
|
317
|
+
page,
|
|
318
|
+
perPage,
|
|
319
|
+
hasMore: false
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
323
|
+
if (conditions.length > 0) {
|
|
324
|
+
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
325
|
+
}
|
|
326
|
+
dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
|
|
327
|
+
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
328
|
+
const rows = await this.operations.executeQuery({
|
|
329
|
+
sql: dataSql,
|
|
330
|
+
params: dataParams
|
|
331
|
+
});
|
|
332
|
+
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
333
|
+
const result = deserializeValue(row.result);
|
|
334
|
+
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
335
|
+
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
336
|
+
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
input: row.input,
|
|
340
|
+
output: row.output,
|
|
341
|
+
result,
|
|
342
|
+
agentName: row.agent_name,
|
|
343
|
+
metricName: row.metric_name,
|
|
344
|
+
instructions: row.instructions,
|
|
345
|
+
testInfo,
|
|
346
|
+
globalRunId: row.global_run_id,
|
|
347
|
+
runId: row.run_id,
|
|
348
|
+
createdAt: row.created_at
|
|
349
|
+
};
|
|
350
|
+
});
|
|
351
|
+
const hasMore = currentOffset + evals.length < total;
|
|
352
|
+
return {
|
|
353
|
+
evals,
|
|
354
|
+
total,
|
|
355
|
+
page,
|
|
356
|
+
perPage,
|
|
357
|
+
hasMore
|
|
358
|
+
};
|
|
359
|
+
} catch (error$1) {
|
|
360
|
+
throw new error.MastraError(
|
|
361
|
+
{
|
|
362
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
363
|
+
domain: error.ErrorDomain.STORAGE,
|
|
364
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
365
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
366
|
+
details: { agentName: agentName ?? "", type: type ?? "" }
|
|
367
|
+
},
|
|
368
|
+
error$1
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* @deprecated use getEvals instead
|
|
374
|
+
*/
|
|
375
|
+
async getEvalsByAgentName(agentName, type) {
|
|
376
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
|
|
377
|
+
try {
|
|
378
|
+
let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
|
|
379
|
+
if (type === "test") {
|
|
380
|
+
query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
|
|
381
|
+
} else if (type === "live") {
|
|
382
|
+
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
383
|
+
}
|
|
384
|
+
query.orderBy("created_at", "DESC");
|
|
385
|
+
const { sql, params } = query.build();
|
|
386
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
387
|
+
return isArrayOfRecords(results) ? results.map((row) => {
|
|
388
|
+
const result = deserializeValue(row.result);
|
|
389
|
+
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
390
|
+
return {
|
|
391
|
+
input: row.input || "",
|
|
392
|
+
output: row.output || "",
|
|
393
|
+
result,
|
|
394
|
+
agentName: row.agent_name || "",
|
|
395
|
+
metricName: row.metric_name || "",
|
|
396
|
+
instructions: row.instructions || "",
|
|
397
|
+
runId: row.run_id || "",
|
|
398
|
+
globalRunId: row.global_run_id || "",
|
|
399
|
+
createdAt: row.created_at || "",
|
|
400
|
+
testInfo
|
|
401
|
+
};
|
|
402
|
+
}) : [];
|
|
403
|
+
} catch (error$1) {
|
|
404
|
+
const mastraError = new error.MastraError(
|
|
405
|
+
{
|
|
406
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
407
|
+
domain: error.ErrorDomain.STORAGE,
|
|
408
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
409
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
410
|
+
details: { agentName }
|
|
411
|
+
},
|
|
412
|
+
error$1
|
|
413
|
+
);
|
|
414
|
+
this.logger?.error(mastraError.toString());
|
|
415
|
+
this.logger?.trackException(mastraError);
|
|
416
|
+
return [];
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
};
|
|
262
420
|
var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
263
421
|
operations;
|
|
264
422
|
constructor({ operations }) {
|
|
@@ -771,7 +929,10 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
771
929
|
throw mastraError;
|
|
772
930
|
}
|
|
773
931
|
}
|
|
774
|
-
async
|
|
932
|
+
async getMessagesById({
|
|
933
|
+
messageIds,
|
|
934
|
+
format
|
|
935
|
+
}) {
|
|
775
936
|
if (messageIds.length === 0) return [];
|
|
776
937
|
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
777
938
|
const messages = [];
|
|
@@ -791,6 +952,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
791
952
|
});
|
|
792
953
|
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
793
954
|
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
955
|
+
if (format === `v1`) return list.get.all.v1();
|
|
794
956
|
return list.get.all.v2();
|
|
795
957
|
} catch (error$1) {
|
|
796
958
|
const mastraError = new error.MastraError(
|
|
@@ -808,169 +970,6 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
808
970
|
throw mastraError;
|
|
809
971
|
}
|
|
810
972
|
}
|
|
811
|
-
async listMessages(args) {
|
|
812
|
-
const { threadId, resourceId, include, filter, limit, offset = 0, orderBy } = args;
|
|
813
|
-
if (!threadId.trim()) {
|
|
814
|
-
throw new error.MastraError(
|
|
815
|
-
{
|
|
816
|
-
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
817
|
-
domain: error.ErrorDomain.STORAGE,
|
|
818
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
819
|
-
details: { threadId }
|
|
820
|
-
},
|
|
821
|
-
new Error("threadId must be a non-empty string")
|
|
822
|
-
);
|
|
823
|
-
}
|
|
824
|
-
try {
|
|
825
|
-
let perPage = 40;
|
|
826
|
-
if (limit !== void 0) {
|
|
827
|
-
if (limit === false) {
|
|
828
|
-
perPage = Number.MAX_SAFE_INTEGER;
|
|
829
|
-
} else if (limit === 0) {
|
|
830
|
-
perPage = 0;
|
|
831
|
-
} else if (typeof limit === "number" && limit > 0) {
|
|
832
|
-
perPage = limit;
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
const page = perPage === 0 ? 0 : Math.floor(offset / perPage);
|
|
836
|
-
const sortField = orderBy?.field || "createdAt";
|
|
837
|
-
const sortDirection = orderBy?.direction || "DESC";
|
|
838
|
-
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
839
|
-
let query = `
|
|
840
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
841
|
-
FROM ${fullTableName}
|
|
842
|
-
WHERE thread_id = ?
|
|
843
|
-
`;
|
|
844
|
-
const queryParams = [threadId];
|
|
845
|
-
if (resourceId) {
|
|
846
|
-
query += ` AND resourceId = ?`;
|
|
847
|
-
queryParams.push(resourceId);
|
|
848
|
-
}
|
|
849
|
-
const dateRange = filter?.dateRange;
|
|
850
|
-
if (dateRange?.start) {
|
|
851
|
-
const startDate = dateRange.start instanceof Date ? storage.serializeDate(dateRange.start) : storage.serializeDate(new Date(dateRange.start));
|
|
852
|
-
query += ` AND createdAt >= ?`;
|
|
853
|
-
queryParams.push(startDate);
|
|
854
|
-
}
|
|
855
|
-
if (dateRange?.end) {
|
|
856
|
-
const endDate = dateRange.end instanceof Date ? storage.serializeDate(dateRange.end) : storage.serializeDate(new Date(dateRange.end));
|
|
857
|
-
query += ` AND createdAt <= ?`;
|
|
858
|
-
queryParams.push(endDate);
|
|
859
|
-
}
|
|
860
|
-
const orderByField = sortField === "createdAt" ? "createdAt" : `"${sortField}"`;
|
|
861
|
-
const orderByDirection = sortDirection === "ASC" ? "ASC" : "DESC";
|
|
862
|
-
query += ` ORDER BY ${orderByField} ${orderByDirection}`;
|
|
863
|
-
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
864
|
-
query += ` LIMIT ? OFFSET ?`;
|
|
865
|
-
queryParams.push(perPage, offset);
|
|
866
|
-
}
|
|
867
|
-
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
868
|
-
const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
|
|
869
|
-
const processedMsg = {};
|
|
870
|
-
for (const [key, value] of Object.entries(message)) {
|
|
871
|
-
if (key === `type` && value === `v2`) continue;
|
|
872
|
-
processedMsg[key] = deserializeValue(value);
|
|
873
|
-
}
|
|
874
|
-
return processedMsg;
|
|
875
|
-
});
|
|
876
|
-
const paginatedCount = paginatedMessages.length;
|
|
877
|
-
let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
|
|
878
|
-
const countParams = [threadId];
|
|
879
|
-
if (resourceId) {
|
|
880
|
-
countQuery += ` AND resourceId = ?`;
|
|
881
|
-
countParams.push(resourceId);
|
|
882
|
-
}
|
|
883
|
-
if (dateRange?.start) {
|
|
884
|
-
const startDate = dateRange.start instanceof Date ? storage.serializeDate(dateRange.start) : storage.serializeDate(new Date(dateRange.start));
|
|
885
|
-
countQuery += ` AND createdAt >= ?`;
|
|
886
|
-
countParams.push(startDate);
|
|
887
|
-
}
|
|
888
|
-
if (dateRange?.end) {
|
|
889
|
-
const endDate = dateRange.end instanceof Date ? storage.serializeDate(dateRange.end) : storage.serializeDate(new Date(dateRange.end));
|
|
890
|
-
countQuery += ` AND createdAt <= ?`;
|
|
891
|
-
countParams.push(endDate);
|
|
892
|
-
}
|
|
893
|
-
const countResult = await this.operations.executeQuery({ sql: countQuery, params: countParams });
|
|
894
|
-
const total = Number(countResult[0]?.count ?? 0);
|
|
895
|
-
if (total === 0 && paginatedCount === 0) {
|
|
896
|
-
return {
|
|
897
|
-
messages: [],
|
|
898
|
-
total: 0,
|
|
899
|
-
page,
|
|
900
|
-
perPage,
|
|
901
|
-
hasMore: false
|
|
902
|
-
};
|
|
903
|
-
}
|
|
904
|
-
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
905
|
-
let includeMessages = [];
|
|
906
|
-
if (include && include.length > 0) {
|
|
907
|
-
const selectBy = { include };
|
|
908
|
-
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
909
|
-
if (Array.isArray(includeResult)) {
|
|
910
|
-
includeMessages = includeResult;
|
|
911
|
-
for (const includeMsg of includeMessages) {
|
|
912
|
-
if (!messageIds.has(includeMsg.id)) {
|
|
913
|
-
paginatedMessages.push(includeMsg);
|
|
914
|
-
messageIds.add(includeMsg.id);
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
const list = new agent.MessageList().add(paginatedMessages, "memory");
|
|
920
|
-
let finalMessages = list.get.all.v2();
|
|
921
|
-
finalMessages = finalMessages.sort((a, b) => {
|
|
922
|
-
const aValue = sortField === "createdAt" ? new Date(a.createdAt).getTime() : a[sortField];
|
|
923
|
-
const bValue = sortField === "createdAt" ? new Date(b.createdAt).getTime() : b[sortField];
|
|
924
|
-
if (aValue === bValue) {
|
|
925
|
-
return a.id.localeCompare(b.id);
|
|
926
|
-
}
|
|
927
|
-
return sortDirection === "ASC" ? aValue - bValue : bValue - aValue;
|
|
928
|
-
});
|
|
929
|
-
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
930
|
-
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
931
|
-
const hasMore = limit === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
|
|
932
|
-
return {
|
|
933
|
-
messages: finalMessages,
|
|
934
|
-
total,
|
|
935
|
-
page,
|
|
936
|
-
perPage,
|
|
937
|
-
hasMore
|
|
938
|
-
};
|
|
939
|
-
} catch (error$1) {
|
|
940
|
-
const mastraError = new error.MastraError(
|
|
941
|
-
{
|
|
942
|
-
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
|
|
943
|
-
domain: error.ErrorDomain.STORAGE,
|
|
944
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
945
|
-
text: `Failed to list messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
946
|
-
details: {
|
|
947
|
-
threadId,
|
|
948
|
-
resourceId: resourceId ?? ""
|
|
949
|
-
}
|
|
950
|
-
},
|
|
951
|
-
error$1
|
|
952
|
-
);
|
|
953
|
-
this.logger?.error?.(mastraError.toString());
|
|
954
|
-
this.logger?.trackException?.(mastraError);
|
|
955
|
-
return {
|
|
956
|
-
messages: [],
|
|
957
|
-
total: 0,
|
|
958
|
-
page: Math.floor(offset / (limit === false ? Number.MAX_SAFE_INTEGER : limit || 40)),
|
|
959
|
-
perPage: limit === false ? Number.MAX_SAFE_INTEGER : limit || 40,
|
|
960
|
-
hasMore: false
|
|
961
|
-
};
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
/**
|
|
965
|
-
* @todo When migrating from getThreadsByResourceIdPaginated to this method,
|
|
966
|
-
* implement orderBy and sortDirection support for full sorting capabilities
|
|
967
|
-
*/
|
|
968
|
-
async listThreadsByResourceId(args) {
|
|
969
|
-
const { resourceId, limit, offset } = args;
|
|
970
|
-
const page = Math.floor(offset / limit);
|
|
971
|
-
const perPage = limit;
|
|
972
|
-
return this.getThreadsByResourceIdPaginated({ resourceId, page, perPage });
|
|
973
|
-
}
|
|
974
973
|
async getMessagesPaginated({
|
|
975
974
|
threadId,
|
|
976
975
|
resourceId,
|
|
@@ -1053,17 +1052,16 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
1053
1052
|
}
|
|
1054
1053
|
return processedMsg;
|
|
1055
1054
|
});
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
1055
|
+
const allMessages = [...messages, ...processedMessages];
|
|
1056
|
+
const list = new agent.MessageList().add(allMessages, "memory");
|
|
1057
|
+
let finalMessages = format === `v2` ? list.get.all.v2() : list.get.all.v1();
|
|
1058
|
+
finalMessages = finalMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
1061
1059
|
return {
|
|
1062
|
-
messages,
|
|
1060
|
+
messages: finalMessages,
|
|
1063
1061
|
total,
|
|
1064
1062
|
page,
|
|
1065
1063
|
perPage,
|
|
1066
|
-
hasMore: selectBy?.last ? false : page * perPage +
|
|
1064
|
+
hasMore: selectBy?.last ? false : page * perPage + processedMessages.length < total
|
|
1067
1065
|
};
|
|
1068
1066
|
} catch (error$1) {
|
|
1069
1067
|
const mastraError = new error.MastraError(
|
|
@@ -1576,15 +1574,15 @@ var StoreOperationsD1 = class extends storage.StoreOperations {
|
|
|
1576
1574
|
};
|
|
1577
1575
|
function transformScoreRow(row) {
|
|
1578
1576
|
const deserialized = { ...row };
|
|
1579
|
-
deserialized.input = storage.safelyParseJSON(row.input);
|
|
1580
|
-
deserialized.output = storage.safelyParseJSON(row.output);
|
|
1581
|
-
deserialized.scorer = storage.safelyParseJSON(row.scorer);
|
|
1582
|
-
deserialized.preprocessStepResult = storage.safelyParseJSON(row.preprocessStepResult);
|
|
1583
|
-
deserialized.analyzeStepResult = storage.safelyParseJSON(row.analyzeStepResult);
|
|
1584
|
-
deserialized.metadata = storage.safelyParseJSON(row.metadata);
|
|
1585
|
-
deserialized.additionalContext = storage.safelyParseJSON(row.additionalContext);
|
|
1586
|
-
deserialized.
|
|
1587
|
-
deserialized.entity = storage.safelyParseJSON(row.entity);
|
|
1577
|
+
deserialized.input = row.input ? storage.safelyParseJSON(row.input) : void 0;
|
|
1578
|
+
deserialized.output = row.output ? storage.safelyParseJSON(row.output) : void 0;
|
|
1579
|
+
deserialized.scorer = row.scorer ? storage.safelyParseJSON(row.scorer) : void 0;
|
|
1580
|
+
deserialized.preprocessStepResult = row.preprocessStepResult ? storage.safelyParseJSON(row.preprocessStepResult) : void 0;
|
|
1581
|
+
deserialized.analyzeStepResult = row.analyzeStepResult ? storage.safelyParseJSON(row.analyzeStepResult) : void 0;
|
|
1582
|
+
deserialized.metadata = row.metadata ? storage.safelyParseJSON(row.metadata) : void 0;
|
|
1583
|
+
deserialized.additionalContext = row.additionalContext ? storage.safelyParseJSON(row.additionalContext) : void 0;
|
|
1584
|
+
deserialized.runtimeContext = row.runtimeContext ? storage.safelyParseJSON(row.runtimeContext) : void 0;
|
|
1585
|
+
deserialized.entity = row.entity ? storage.safelyParseJSON(row.entity) : void 0;
|
|
1588
1586
|
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1589
1587
|
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
1590
1588
|
return deserialized;
|
|
@@ -1870,6 +1868,128 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1870
1868
|
}
|
|
1871
1869
|
}
|
|
1872
1870
|
};
|
|
1871
|
+
function isArrayOfRecords2(value) {
|
|
1872
|
+
return value && Array.isArray(value) && value.length > 0;
|
|
1873
|
+
}
|
|
1874
|
+
var TracesStorageD1 = class extends storage.TracesStorage {
|
|
1875
|
+
operations;
|
|
1876
|
+
constructor({ operations }) {
|
|
1877
|
+
super();
|
|
1878
|
+
this.operations = operations;
|
|
1879
|
+
}
|
|
1880
|
+
async getTraces(args) {
|
|
1881
|
+
const paginatedArgs = {
|
|
1882
|
+
name: args.name,
|
|
1883
|
+
scope: args.scope,
|
|
1884
|
+
page: args.page,
|
|
1885
|
+
perPage: args.perPage,
|
|
1886
|
+
attributes: args.attributes,
|
|
1887
|
+
filters: args.filters,
|
|
1888
|
+
dateRange: args.fromDate || args.toDate ? {
|
|
1889
|
+
start: args.fromDate,
|
|
1890
|
+
end: args.toDate
|
|
1891
|
+
} : void 0
|
|
1892
|
+
};
|
|
1893
|
+
try {
|
|
1894
|
+
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1895
|
+
return result.traces;
|
|
1896
|
+
} catch (error$1) {
|
|
1897
|
+
throw new error.MastraError(
|
|
1898
|
+
{
|
|
1899
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1900
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1901
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1902
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1903
|
+
details: {
|
|
1904
|
+
name: args.name ?? "",
|
|
1905
|
+
scope: args.scope ?? ""
|
|
1906
|
+
}
|
|
1907
|
+
},
|
|
1908
|
+
error$1
|
|
1909
|
+
);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
async getTracesPaginated(args) {
|
|
1913
|
+
const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
|
|
1914
|
+
const fromDate = dateRange?.start;
|
|
1915
|
+
const toDate = dateRange?.end;
|
|
1916
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_TRACES);
|
|
1917
|
+
try {
|
|
1918
|
+
const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
|
|
1919
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
|
|
1920
|
+
if (name) {
|
|
1921
|
+
dataQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1922
|
+
countQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1923
|
+
}
|
|
1924
|
+
if (scope) {
|
|
1925
|
+
dataQuery.andWhere("scope = ?", scope);
|
|
1926
|
+
countQuery.andWhere("scope = ?", scope);
|
|
1927
|
+
}
|
|
1928
|
+
if (attributes && Object.keys(attributes).length > 0) {
|
|
1929
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
1930
|
+
dataQuery.jsonLike("attributes", key, value);
|
|
1931
|
+
countQuery.jsonLike("attributes", key, value);
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
if (fromDate) {
|
|
1935
|
+
const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
|
|
1936
|
+
dataQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1937
|
+
countQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1938
|
+
}
|
|
1939
|
+
if (toDate) {
|
|
1940
|
+
const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
|
|
1941
|
+
dataQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1942
|
+
countQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1943
|
+
}
|
|
1944
|
+
const allDataResult = await this.operations.executeQuery(
|
|
1945
|
+
createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
|
|
1946
|
+
);
|
|
1947
|
+
console.info("allDataResult", allDataResult);
|
|
1948
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1949
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1950
|
+
dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
|
|
1951
|
+
const results = await this.operations.executeQuery(dataQuery.build());
|
|
1952
|
+
const traces = isArrayOfRecords2(results) ? results.map(
|
|
1953
|
+
(trace) => ({
|
|
1954
|
+
...trace,
|
|
1955
|
+
attributes: deserializeValue(trace.attributes, "jsonb"),
|
|
1956
|
+
status: deserializeValue(trace.status, "jsonb"),
|
|
1957
|
+
events: deserializeValue(trace.events, "jsonb"),
|
|
1958
|
+
links: deserializeValue(trace.links, "jsonb"),
|
|
1959
|
+
other: deserializeValue(trace.other, "jsonb")
|
|
1960
|
+
})
|
|
1961
|
+
) : [];
|
|
1962
|
+
return {
|
|
1963
|
+
traces,
|
|
1964
|
+
total,
|
|
1965
|
+
page,
|
|
1966
|
+
perPage,
|
|
1967
|
+
hasMore: page * perPage + traces.length < total
|
|
1968
|
+
};
|
|
1969
|
+
} catch (error$1) {
|
|
1970
|
+
const mastraError = new error.MastraError(
|
|
1971
|
+
{
|
|
1972
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
|
|
1973
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1974
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1975
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1976
|
+
details: { name: name ?? "", scope: scope ?? "" }
|
|
1977
|
+
},
|
|
1978
|
+
error$1
|
|
1979
|
+
);
|
|
1980
|
+
this.logger?.error(mastraError.toString());
|
|
1981
|
+
this.logger?.trackException(mastraError);
|
|
1982
|
+
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
async batchTraceInsert({ records }) {
|
|
1986
|
+
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1987
|
+
await this.operations.batchInsert({
|
|
1988
|
+
tableName: storage.TABLE_TRACES,
|
|
1989
|
+
records
|
|
1990
|
+
});
|
|
1991
|
+
}
|
|
1992
|
+
};
|
|
1873
1993
|
var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
1874
1994
|
operations;
|
|
1875
1995
|
constructor({ operations }) {
|
|
@@ -1881,7 +2001,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
1881
2001
|
// runId,
|
|
1882
2002
|
// stepId,
|
|
1883
2003
|
// result,
|
|
1884
|
-
//
|
|
2004
|
+
// runtimeContext,
|
|
1885
2005
|
}) {
|
|
1886
2006
|
throw new Error("Method not implemented.");
|
|
1887
2007
|
}
|
|
@@ -1985,19 +2105,24 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
1985
2105
|
resourceId: row.resourceId
|
|
1986
2106
|
};
|
|
1987
2107
|
}
|
|
1988
|
-
async
|
|
2108
|
+
async getWorkflowRuns({
|
|
1989
2109
|
workflowName,
|
|
1990
2110
|
fromDate,
|
|
1991
2111
|
toDate,
|
|
1992
2112
|
limit,
|
|
1993
2113
|
offset,
|
|
1994
|
-
resourceId
|
|
2114
|
+
resourceId,
|
|
2115
|
+
status
|
|
1995
2116
|
} = {}) {
|
|
1996
2117
|
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1997
2118
|
try {
|
|
1998
2119
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
1999
2120
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
2000
2121
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
2122
|
+
if (status) {
|
|
2123
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
2124
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
2125
|
+
}
|
|
2001
2126
|
if (resourceId) {
|
|
2002
2127
|
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
2003
2128
|
if (hasResourceId) {
|
|
@@ -2151,6 +2276,12 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2151
2276
|
const scores = new ScoresStorageD1({
|
|
2152
2277
|
operations
|
|
2153
2278
|
});
|
|
2279
|
+
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2280
|
+
operations
|
|
2281
|
+
});
|
|
2282
|
+
const traces = new TracesStorageD1({
|
|
2283
|
+
operations
|
|
2284
|
+
});
|
|
2154
2285
|
const workflows = new WorkflowsStorageD1({
|
|
2155
2286
|
operations
|
|
2156
2287
|
});
|
|
@@ -2160,6 +2291,8 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2160
2291
|
this.stores = {
|
|
2161
2292
|
operations,
|
|
2162
2293
|
scores,
|
|
2294
|
+
legacyEvals,
|
|
2295
|
+
traces,
|
|
2163
2296
|
workflows,
|
|
2164
2297
|
memory
|
|
2165
2298
|
};
|
|
@@ -2243,6 +2376,12 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2243
2376
|
}) {
|
|
2244
2377
|
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2245
2378
|
}
|
|
2379
|
+
async getMessagesById({
|
|
2380
|
+
messageIds,
|
|
2381
|
+
format
|
|
2382
|
+
}) {
|
|
2383
|
+
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2384
|
+
}
|
|
2246
2385
|
async getMessagesPaginated({
|
|
2247
2386
|
threadId,
|
|
2248
2387
|
selectBy,
|
|
@@ -2255,9 +2394,9 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2255
2394
|
runId,
|
|
2256
2395
|
stepId,
|
|
2257
2396
|
result,
|
|
2258
|
-
|
|
2397
|
+
runtimeContext
|
|
2259
2398
|
}) {
|
|
2260
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2399
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
|
|
2261
2400
|
}
|
|
2262
2401
|
async updateWorkflowState({
|
|
2263
2402
|
workflowName,
|
|
@@ -2277,15 +2416,8 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2277
2416
|
async loadWorkflowSnapshot(params) {
|
|
2278
2417
|
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2279
2418
|
}
|
|
2280
|
-
async
|
|
2281
|
-
|
|
2282
|
-
fromDate,
|
|
2283
|
-
toDate,
|
|
2284
|
-
limit,
|
|
2285
|
-
offset,
|
|
2286
|
-
resourceId
|
|
2287
|
-
} = {}) {
|
|
2288
|
-
return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2419
|
+
async getWorkflowRuns(args = {}) {
|
|
2420
|
+
return this.stores.workflows.getWorkflowRuns(args);
|
|
2289
2421
|
}
|
|
2290
2422
|
async getWorkflowRunById({
|
|
2291
2423
|
runId,
|
|
@@ -2301,6 +2433,24 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2301
2433
|
async batchInsert({ tableName, records }) {
|
|
2302
2434
|
return this.stores.operations.batchInsert({ tableName, records });
|
|
2303
2435
|
}
|
|
2436
|
+
/**
|
|
2437
|
+
* @deprecated use getTracesPaginated instead
|
|
2438
|
+
*/
|
|
2439
|
+
async getTraces(args) {
|
|
2440
|
+
return this.stores.traces.getTraces(args);
|
|
2441
|
+
}
|
|
2442
|
+
async getTracesPaginated(args) {
|
|
2443
|
+
return this.stores.traces.getTracesPaginated(args);
|
|
2444
|
+
}
|
|
2445
|
+
/**
|
|
2446
|
+
* @deprecated use getEvals instead
|
|
2447
|
+
*/
|
|
2448
|
+
async getEvalsByAgentName(agentName, type) {
|
|
2449
|
+
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2450
|
+
}
|
|
2451
|
+
async getEvals(options) {
|
|
2452
|
+
return this.stores.legacyEvals.getEvals(options);
|
|
2453
|
+
}
|
|
2304
2454
|
async updateMessages(_args) {
|
|
2305
2455
|
return this.stores.memory.updateMessages(_args);
|
|
2306
2456
|
}
|