@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.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
|
-
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, resolveMessageLimit,
|
|
2
|
+
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, LegacyEvalsStorage, TABLE_EVALS, serializeDate, TracesStorage, TABLE_TRACES, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, resolveMessageLimit, safelyParseJSON } from '@mastra/core/storage';
|
|
3
3
|
import Cloudflare from 'cloudflare';
|
|
4
|
-
import { MessageList } from '@mastra/core/agent';
|
|
5
4
|
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
5
|
+
import { MessageList } from '@mastra/core/agent';
|
|
6
6
|
import { saveScorePayloadSchema } from '@mastra/core/scores';
|
|
7
7
|
|
|
8
8
|
// src/storage/index.ts
|
|
@@ -242,6 +242,16 @@ function isArrayOfRecords(value) {
|
|
|
242
242
|
}
|
|
243
243
|
function deserializeValue(value, type) {
|
|
244
244
|
if (value === null || value === void 0) return null;
|
|
245
|
+
if (type === "date" && typeof value === "string") {
|
|
246
|
+
return new Date(value);
|
|
247
|
+
}
|
|
248
|
+
if (type === "jsonb" && typeof value === "string") {
|
|
249
|
+
try {
|
|
250
|
+
return JSON.parse(value);
|
|
251
|
+
} catch {
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
245
255
|
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
246
256
|
try {
|
|
247
257
|
return JSON.parse(value);
|
|
@@ -252,7 +262,155 @@ function deserializeValue(value, type) {
|
|
|
252
262
|
return value;
|
|
253
263
|
}
|
|
254
264
|
|
|
255
|
-
// src/storage/domains/
|
|
265
|
+
// src/storage/domains/legacy-evals/index.ts
|
|
266
|
+
var LegacyEvalsStorageD1 = class extends LegacyEvalsStorage {
|
|
267
|
+
operations;
|
|
268
|
+
constructor({ operations }) {
|
|
269
|
+
super();
|
|
270
|
+
this.operations = operations;
|
|
271
|
+
}
|
|
272
|
+
async getEvals(options) {
|
|
273
|
+
const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
|
|
274
|
+
const fullTableName = this.operations.getTableName(TABLE_EVALS);
|
|
275
|
+
const conditions = [];
|
|
276
|
+
const queryParams = [];
|
|
277
|
+
if (agentName) {
|
|
278
|
+
conditions.push(`agent_name = ?`);
|
|
279
|
+
queryParams.push(agentName);
|
|
280
|
+
}
|
|
281
|
+
if (type === "test") {
|
|
282
|
+
conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
|
|
283
|
+
} else if (type === "live") {
|
|
284
|
+
conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
|
|
285
|
+
}
|
|
286
|
+
if (dateRange?.start) {
|
|
287
|
+
conditions.push(`created_at >= ?`);
|
|
288
|
+
queryParams.push(serializeDate(dateRange.start));
|
|
289
|
+
}
|
|
290
|
+
if (dateRange?.end) {
|
|
291
|
+
conditions.push(`created_at <= ?`);
|
|
292
|
+
queryParams.push(serializeDate(dateRange.end));
|
|
293
|
+
}
|
|
294
|
+
const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
|
|
295
|
+
if (conditions.length > 0) {
|
|
296
|
+
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
297
|
+
}
|
|
298
|
+
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
299
|
+
try {
|
|
300
|
+
const countResult = await this.operations.executeQuery({
|
|
301
|
+
sql: countSql,
|
|
302
|
+
params: countParams,
|
|
303
|
+
first: true
|
|
304
|
+
});
|
|
305
|
+
const total = Number(countResult?.count || 0);
|
|
306
|
+
const currentOffset = page * perPage;
|
|
307
|
+
if (total === 0) {
|
|
308
|
+
return {
|
|
309
|
+
evals: [],
|
|
310
|
+
total: 0,
|
|
311
|
+
page,
|
|
312
|
+
perPage,
|
|
313
|
+
hasMore: false
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
317
|
+
if (conditions.length > 0) {
|
|
318
|
+
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
319
|
+
}
|
|
320
|
+
dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
|
|
321
|
+
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
322
|
+
const rows = await this.operations.executeQuery({
|
|
323
|
+
sql: dataSql,
|
|
324
|
+
params: dataParams
|
|
325
|
+
});
|
|
326
|
+
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
327
|
+
const result = deserializeValue(row.result);
|
|
328
|
+
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
329
|
+
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
330
|
+
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
input: row.input,
|
|
334
|
+
output: row.output,
|
|
335
|
+
result,
|
|
336
|
+
agentName: row.agent_name,
|
|
337
|
+
metricName: row.metric_name,
|
|
338
|
+
instructions: row.instructions,
|
|
339
|
+
testInfo,
|
|
340
|
+
globalRunId: row.global_run_id,
|
|
341
|
+
runId: row.run_id,
|
|
342
|
+
createdAt: row.created_at
|
|
343
|
+
};
|
|
344
|
+
});
|
|
345
|
+
const hasMore = currentOffset + evals.length < total;
|
|
346
|
+
return {
|
|
347
|
+
evals,
|
|
348
|
+
total,
|
|
349
|
+
page,
|
|
350
|
+
perPage,
|
|
351
|
+
hasMore
|
|
352
|
+
};
|
|
353
|
+
} catch (error) {
|
|
354
|
+
throw new MastraError(
|
|
355
|
+
{
|
|
356
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
357
|
+
domain: ErrorDomain.STORAGE,
|
|
358
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
359
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
360
|
+
details: { agentName: agentName ?? "", type: type ?? "" }
|
|
361
|
+
},
|
|
362
|
+
error
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* @deprecated use getEvals instead
|
|
368
|
+
*/
|
|
369
|
+
async getEvalsByAgentName(agentName, type) {
|
|
370
|
+
const fullTableName = this.operations.getTableName(TABLE_EVALS);
|
|
371
|
+
try {
|
|
372
|
+
let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
|
|
373
|
+
if (type === "test") {
|
|
374
|
+
query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
|
|
375
|
+
} else if (type === "live") {
|
|
376
|
+
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
377
|
+
}
|
|
378
|
+
query.orderBy("created_at", "DESC");
|
|
379
|
+
const { sql, params } = query.build();
|
|
380
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
381
|
+
return isArrayOfRecords(results) ? results.map((row) => {
|
|
382
|
+
const result = deserializeValue(row.result);
|
|
383
|
+
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
384
|
+
return {
|
|
385
|
+
input: row.input || "",
|
|
386
|
+
output: row.output || "",
|
|
387
|
+
result,
|
|
388
|
+
agentName: row.agent_name || "",
|
|
389
|
+
metricName: row.metric_name || "",
|
|
390
|
+
instructions: row.instructions || "",
|
|
391
|
+
runId: row.run_id || "",
|
|
392
|
+
globalRunId: row.global_run_id || "",
|
|
393
|
+
createdAt: row.created_at || "",
|
|
394
|
+
testInfo
|
|
395
|
+
};
|
|
396
|
+
}) : [];
|
|
397
|
+
} catch (error) {
|
|
398
|
+
const mastraError = new MastraError(
|
|
399
|
+
{
|
|
400
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
401
|
+
domain: ErrorDomain.STORAGE,
|
|
402
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
403
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
404
|
+
details: { agentName }
|
|
405
|
+
},
|
|
406
|
+
error
|
|
407
|
+
);
|
|
408
|
+
this.logger?.error(mastraError.toString());
|
|
409
|
+
this.logger?.trackException(mastraError);
|
|
410
|
+
return [];
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
};
|
|
256
414
|
var MemoryStorageD1 = class extends MemoryStorage {
|
|
257
415
|
operations;
|
|
258
416
|
constructor({ operations }) {
|
|
@@ -765,7 +923,10 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
765
923
|
throw mastraError;
|
|
766
924
|
}
|
|
767
925
|
}
|
|
768
|
-
async
|
|
926
|
+
async getMessagesById({
|
|
927
|
+
messageIds,
|
|
928
|
+
format
|
|
929
|
+
}) {
|
|
769
930
|
if (messageIds.length === 0) return [];
|
|
770
931
|
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
771
932
|
const messages = [];
|
|
@@ -785,6 +946,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
785
946
|
});
|
|
786
947
|
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
787
948
|
const list = new MessageList().add(processedMessages, "memory");
|
|
949
|
+
if (format === `v1`) return list.get.all.v1();
|
|
788
950
|
return list.get.all.v2();
|
|
789
951
|
} catch (error) {
|
|
790
952
|
const mastraError = new MastraError(
|
|
@@ -802,169 +964,6 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
802
964
|
throw mastraError;
|
|
803
965
|
}
|
|
804
966
|
}
|
|
805
|
-
async listMessages(args) {
|
|
806
|
-
const { threadId, resourceId, include, filter, limit, offset = 0, orderBy } = args;
|
|
807
|
-
if (!threadId.trim()) {
|
|
808
|
-
throw new MastraError(
|
|
809
|
-
{
|
|
810
|
-
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
811
|
-
domain: ErrorDomain.STORAGE,
|
|
812
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
813
|
-
details: { threadId }
|
|
814
|
-
},
|
|
815
|
-
new Error("threadId must be a non-empty string")
|
|
816
|
-
);
|
|
817
|
-
}
|
|
818
|
-
try {
|
|
819
|
-
let perPage = 40;
|
|
820
|
-
if (limit !== void 0) {
|
|
821
|
-
if (limit === false) {
|
|
822
|
-
perPage = Number.MAX_SAFE_INTEGER;
|
|
823
|
-
} else if (limit === 0) {
|
|
824
|
-
perPage = 0;
|
|
825
|
-
} else if (typeof limit === "number" && limit > 0) {
|
|
826
|
-
perPage = limit;
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
const page = perPage === 0 ? 0 : Math.floor(offset / perPage);
|
|
830
|
-
const sortField = orderBy?.field || "createdAt";
|
|
831
|
-
const sortDirection = orderBy?.direction || "DESC";
|
|
832
|
-
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
833
|
-
let query = `
|
|
834
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
835
|
-
FROM ${fullTableName}
|
|
836
|
-
WHERE thread_id = ?
|
|
837
|
-
`;
|
|
838
|
-
const queryParams = [threadId];
|
|
839
|
-
if (resourceId) {
|
|
840
|
-
query += ` AND resourceId = ?`;
|
|
841
|
-
queryParams.push(resourceId);
|
|
842
|
-
}
|
|
843
|
-
const dateRange = filter?.dateRange;
|
|
844
|
-
if (dateRange?.start) {
|
|
845
|
-
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
846
|
-
query += ` AND createdAt >= ?`;
|
|
847
|
-
queryParams.push(startDate);
|
|
848
|
-
}
|
|
849
|
-
if (dateRange?.end) {
|
|
850
|
-
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
851
|
-
query += ` AND createdAt <= ?`;
|
|
852
|
-
queryParams.push(endDate);
|
|
853
|
-
}
|
|
854
|
-
const orderByField = sortField === "createdAt" ? "createdAt" : `"${sortField}"`;
|
|
855
|
-
const orderByDirection = sortDirection === "ASC" ? "ASC" : "DESC";
|
|
856
|
-
query += ` ORDER BY ${orderByField} ${orderByDirection}`;
|
|
857
|
-
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
858
|
-
query += ` LIMIT ? OFFSET ?`;
|
|
859
|
-
queryParams.push(perPage, offset);
|
|
860
|
-
}
|
|
861
|
-
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
862
|
-
const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
|
|
863
|
-
const processedMsg = {};
|
|
864
|
-
for (const [key, value] of Object.entries(message)) {
|
|
865
|
-
if (key === `type` && value === `v2`) continue;
|
|
866
|
-
processedMsg[key] = deserializeValue(value);
|
|
867
|
-
}
|
|
868
|
-
return processedMsg;
|
|
869
|
-
});
|
|
870
|
-
const paginatedCount = paginatedMessages.length;
|
|
871
|
-
let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
|
|
872
|
-
const countParams = [threadId];
|
|
873
|
-
if (resourceId) {
|
|
874
|
-
countQuery += ` AND resourceId = ?`;
|
|
875
|
-
countParams.push(resourceId);
|
|
876
|
-
}
|
|
877
|
-
if (dateRange?.start) {
|
|
878
|
-
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
879
|
-
countQuery += ` AND createdAt >= ?`;
|
|
880
|
-
countParams.push(startDate);
|
|
881
|
-
}
|
|
882
|
-
if (dateRange?.end) {
|
|
883
|
-
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
884
|
-
countQuery += ` AND createdAt <= ?`;
|
|
885
|
-
countParams.push(endDate);
|
|
886
|
-
}
|
|
887
|
-
const countResult = await this.operations.executeQuery({ sql: countQuery, params: countParams });
|
|
888
|
-
const total = Number(countResult[0]?.count ?? 0);
|
|
889
|
-
if (total === 0 && paginatedCount === 0) {
|
|
890
|
-
return {
|
|
891
|
-
messages: [],
|
|
892
|
-
total: 0,
|
|
893
|
-
page,
|
|
894
|
-
perPage,
|
|
895
|
-
hasMore: false
|
|
896
|
-
};
|
|
897
|
-
}
|
|
898
|
-
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
899
|
-
let includeMessages = [];
|
|
900
|
-
if (include && include.length > 0) {
|
|
901
|
-
const selectBy = { include };
|
|
902
|
-
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
903
|
-
if (Array.isArray(includeResult)) {
|
|
904
|
-
includeMessages = includeResult;
|
|
905
|
-
for (const includeMsg of includeMessages) {
|
|
906
|
-
if (!messageIds.has(includeMsg.id)) {
|
|
907
|
-
paginatedMessages.push(includeMsg);
|
|
908
|
-
messageIds.add(includeMsg.id);
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
const list = new MessageList().add(paginatedMessages, "memory");
|
|
914
|
-
let finalMessages = list.get.all.v2();
|
|
915
|
-
finalMessages = finalMessages.sort((a, b) => {
|
|
916
|
-
const aValue = sortField === "createdAt" ? new Date(a.createdAt).getTime() : a[sortField];
|
|
917
|
-
const bValue = sortField === "createdAt" ? new Date(b.createdAt).getTime() : b[sortField];
|
|
918
|
-
if (aValue === bValue) {
|
|
919
|
-
return a.id.localeCompare(b.id);
|
|
920
|
-
}
|
|
921
|
-
return sortDirection === "ASC" ? aValue - bValue : bValue - aValue;
|
|
922
|
-
});
|
|
923
|
-
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
924
|
-
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
925
|
-
const hasMore = limit === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
|
|
926
|
-
return {
|
|
927
|
-
messages: finalMessages,
|
|
928
|
-
total,
|
|
929
|
-
page,
|
|
930
|
-
perPage,
|
|
931
|
-
hasMore
|
|
932
|
-
};
|
|
933
|
-
} catch (error) {
|
|
934
|
-
const mastraError = new MastraError(
|
|
935
|
-
{
|
|
936
|
-
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
|
|
937
|
-
domain: ErrorDomain.STORAGE,
|
|
938
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
939
|
-
text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
940
|
-
details: {
|
|
941
|
-
threadId,
|
|
942
|
-
resourceId: resourceId ?? ""
|
|
943
|
-
}
|
|
944
|
-
},
|
|
945
|
-
error
|
|
946
|
-
);
|
|
947
|
-
this.logger?.error?.(mastraError.toString());
|
|
948
|
-
this.logger?.trackException?.(mastraError);
|
|
949
|
-
return {
|
|
950
|
-
messages: [],
|
|
951
|
-
total: 0,
|
|
952
|
-
page: Math.floor(offset / (limit === false ? Number.MAX_SAFE_INTEGER : limit || 40)),
|
|
953
|
-
perPage: limit === false ? Number.MAX_SAFE_INTEGER : limit || 40,
|
|
954
|
-
hasMore: false
|
|
955
|
-
};
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
/**
|
|
959
|
-
* @todo When migrating from getThreadsByResourceIdPaginated to this method,
|
|
960
|
-
* implement orderBy and sortDirection support for full sorting capabilities
|
|
961
|
-
*/
|
|
962
|
-
async listThreadsByResourceId(args) {
|
|
963
|
-
const { resourceId, limit, offset } = args;
|
|
964
|
-
const page = Math.floor(offset / limit);
|
|
965
|
-
const perPage = limit;
|
|
966
|
-
return this.getThreadsByResourceIdPaginated({ resourceId, page, perPage });
|
|
967
|
-
}
|
|
968
967
|
async getMessagesPaginated({
|
|
969
968
|
threadId,
|
|
970
969
|
resourceId,
|
|
@@ -1047,17 +1046,16 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
1047
1046
|
}
|
|
1048
1047
|
return processedMsg;
|
|
1049
1048
|
});
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
1049
|
+
const allMessages = [...messages, ...processedMessages];
|
|
1050
|
+
const list = new MessageList().add(allMessages, "memory");
|
|
1051
|
+
let finalMessages = format === `v2` ? list.get.all.v2() : list.get.all.v1();
|
|
1052
|
+
finalMessages = finalMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
1055
1053
|
return {
|
|
1056
|
-
messages,
|
|
1054
|
+
messages: finalMessages,
|
|
1057
1055
|
total,
|
|
1058
1056
|
page,
|
|
1059
1057
|
perPage,
|
|
1060
|
-
hasMore: selectBy?.last ? false : page * perPage +
|
|
1058
|
+
hasMore: selectBy?.last ? false : page * perPage + processedMessages.length < total
|
|
1061
1059
|
};
|
|
1062
1060
|
} catch (error) {
|
|
1063
1061
|
const mastraError = new MastraError(
|
|
@@ -1570,15 +1568,15 @@ var StoreOperationsD1 = class extends StoreOperations {
|
|
|
1570
1568
|
};
|
|
1571
1569
|
function transformScoreRow(row) {
|
|
1572
1570
|
const deserialized = { ...row };
|
|
1573
|
-
deserialized.input = safelyParseJSON(row.input);
|
|
1574
|
-
deserialized.output = safelyParseJSON(row.output);
|
|
1575
|
-
deserialized.scorer = safelyParseJSON(row.scorer);
|
|
1576
|
-
deserialized.preprocessStepResult = safelyParseJSON(row.preprocessStepResult);
|
|
1577
|
-
deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
|
|
1578
|
-
deserialized.metadata = safelyParseJSON(row.metadata);
|
|
1579
|
-
deserialized.additionalContext = safelyParseJSON(row.additionalContext);
|
|
1580
|
-
deserialized.
|
|
1581
|
-
deserialized.entity = safelyParseJSON(row.entity);
|
|
1571
|
+
deserialized.input = row.input ? safelyParseJSON(row.input) : void 0;
|
|
1572
|
+
deserialized.output = row.output ? safelyParseJSON(row.output) : void 0;
|
|
1573
|
+
deserialized.scorer = row.scorer ? safelyParseJSON(row.scorer) : void 0;
|
|
1574
|
+
deserialized.preprocessStepResult = row.preprocessStepResult ? safelyParseJSON(row.preprocessStepResult) : void 0;
|
|
1575
|
+
deserialized.analyzeStepResult = row.analyzeStepResult ? safelyParseJSON(row.analyzeStepResult) : void 0;
|
|
1576
|
+
deserialized.metadata = row.metadata ? safelyParseJSON(row.metadata) : void 0;
|
|
1577
|
+
deserialized.additionalContext = row.additionalContext ? safelyParseJSON(row.additionalContext) : void 0;
|
|
1578
|
+
deserialized.runtimeContext = row.runtimeContext ? safelyParseJSON(row.runtimeContext) : void 0;
|
|
1579
|
+
deserialized.entity = row.entity ? safelyParseJSON(row.entity) : void 0;
|
|
1582
1580
|
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1583
1581
|
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
1584
1582
|
return deserialized;
|
|
@@ -1864,6 +1862,128 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1864
1862
|
}
|
|
1865
1863
|
}
|
|
1866
1864
|
};
|
|
1865
|
+
function isArrayOfRecords2(value) {
|
|
1866
|
+
return value && Array.isArray(value) && value.length > 0;
|
|
1867
|
+
}
|
|
1868
|
+
var TracesStorageD1 = class extends TracesStorage {
|
|
1869
|
+
operations;
|
|
1870
|
+
constructor({ operations }) {
|
|
1871
|
+
super();
|
|
1872
|
+
this.operations = operations;
|
|
1873
|
+
}
|
|
1874
|
+
async getTraces(args) {
|
|
1875
|
+
const paginatedArgs = {
|
|
1876
|
+
name: args.name,
|
|
1877
|
+
scope: args.scope,
|
|
1878
|
+
page: args.page,
|
|
1879
|
+
perPage: args.perPage,
|
|
1880
|
+
attributes: args.attributes,
|
|
1881
|
+
filters: args.filters,
|
|
1882
|
+
dateRange: args.fromDate || args.toDate ? {
|
|
1883
|
+
start: args.fromDate,
|
|
1884
|
+
end: args.toDate
|
|
1885
|
+
} : void 0
|
|
1886
|
+
};
|
|
1887
|
+
try {
|
|
1888
|
+
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1889
|
+
return result.traces;
|
|
1890
|
+
} catch (error) {
|
|
1891
|
+
throw new MastraError(
|
|
1892
|
+
{
|
|
1893
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1894
|
+
domain: ErrorDomain.STORAGE,
|
|
1895
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1896
|
+
text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
|
|
1897
|
+
details: {
|
|
1898
|
+
name: args.name ?? "",
|
|
1899
|
+
scope: args.scope ?? ""
|
|
1900
|
+
}
|
|
1901
|
+
},
|
|
1902
|
+
error
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
async getTracesPaginated(args) {
|
|
1907
|
+
const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
|
|
1908
|
+
const fromDate = dateRange?.start;
|
|
1909
|
+
const toDate = dateRange?.end;
|
|
1910
|
+
const fullTableName = this.operations.getTableName(TABLE_TRACES);
|
|
1911
|
+
try {
|
|
1912
|
+
const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
|
|
1913
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
|
|
1914
|
+
if (name) {
|
|
1915
|
+
dataQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1916
|
+
countQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1917
|
+
}
|
|
1918
|
+
if (scope) {
|
|
1919
|
+
dataQuery.andWhere("scope = ?", scope);
|
|
1920
|
+
countQuery.andWhere("scope = ?", scope);
|
|
1921
|
+
}
|
|
1922
|
+
if (attributes && Object.keys(attributes).length > 0) {
|
|
1923
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
1924
|
+
dataQuery.jsonLike("attributes", key, value);
|
|
1925
|
+
countQuery.jsonLike("attributes", key, value);
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
if (fromDate) {
|
|
1929
|
+
const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
|
|
1930
|
+
dataQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1931
|
+
countQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1932
|
+
}
|
|
1933
|
+
if (toDate) {
|
|
1934
|
+
const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
|
|
1935
|
+
dataQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1936
|
+
countQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1937
|
+
}
|
|
1938
|
+
const allDataResult = await this.operations.executeQuery(
|
|
1939
|
+
createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
|
|
1940
|
+
);
|
|
1941
|
+
console.info("allDataResult", allDataResult);
|
|
1942
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1943
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1944
|
+
dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
|
|
1945
|
+
const results = await this.operations.executeQuery(dataQuery.build());
|
|
1946
|
+
const traces = isArrayOfRecords2(results) ? results.map(
|
|
1947
|
+
(trace) => ({
|
|
1948
|
+
...trace,
|
|
1949
|
+
attributes: deserializeValue(trace.attributes, "jsonb"),
|
|
1950
|
+
status: deserializeValue(trace.status, "jsonb"),
|
|
1951
|
+
events: deserializeValue(trace.events, "jsonb"),
|
|
1952
|
+
links: deserializeValue(trace.links, "jsonb"),
|
|
1953
|
+
other: deserializeValue(trace.other, "jsonb")
|
|
1954
|
+
})
|
|
1955
|
+
) : [];
|
|
1956
|
+
return {
|
|
1957
|
+
traces,
|
|
1958
|
+
total,
|
|
1959
|
+
page,
|
|
1960
|
+
perPage,
|
|
1961
|
+
hasMore: page * perPage + traces.length < total
|
|
1962
|
+
};
|
|
1963
|
+
} catch (error) {
|
|
1964
|
+
const mastraError = new MastraError(
|
|
1965
|
+
{
|
|
1966
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
|
|
1967
|
+
domain: ErrorDomain.STORAGE,
|
|
1968
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1969
|
+
text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
|
|
1970
|
+
details: { name: name ?? "", scope: scope ?? "" }
|
|
1971
|
+
},
|
|
1972
|
+
error
|
|
1973
|
+
);
|
|
1974
|
+
this.logger?.error(mastraError.toString());
|
|
1975
|
+
this.logger?.trackException(mastraError);
|
|
1976
|
+
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
async batchTraceInsert({ records }) {
|
|
1980
|
+
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1981
|
+
await this.operations.batchInsert({
|
|
1982
|
+
tableName: TABLE_TRACES,
|
|
1983
|
+
records
|
|
1984
|
+
});
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1867
1987
|
var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
1868
1988
|
operations;
|
|
1869
1989
|
constructor({ operations }) {
|
|
@@ -1875,7 +1995,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
1875
1995
|
// runId,
|
|
1876
1996
|
// stepId,
|
|
1877
1997
|
// result,
|
|
1878
|
-
//
|
|
1998
|
+
// runtimeContext,
|
|
1879
1999
|
}) {
|
|
1880
2000
|
throw new Error("Method not implemented.");
|
|
1881
2001
|
}
|
|
@@ -1979,19 +2099,24 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
1979
2099
|
resourceId: row.resourceId
|
|
1980
2100
|
};
|
|
1981
2101
|
}
|
|
1982
|
-
async
|
|
2102
|
+
async getWorkflowRuns({
|
|
1983
2103
|
workflowName,
|
|
1984
2104
|
fromDate,
|
|
1985
2105
|
toDate,
|
|
1986
2106
|
limit,
|
|
1987
2107
|
offset,
|
|
1988
|
-
resourceId
|
|
2108
|
+
resourceId,
|
|
2109
|
+
status
|
|
1989
2110
|
} = {}) {
|
|
1990
2111
|
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1991
2112
|
try {
|
|
1992
2113
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
1993
2114
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
1994
2115
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
2116
|
+
if (status) {
|
|
2117
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
2118
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
2119
|
+
}
|
|
1995
2120
|
if (resourceId) {
|
|
1996
2121
|
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
1997
2122
|
if (hasResourceId) {
|
|
@@ -2145,6 +2270,12 @@ var D1Store = class extends MastraStorage {
|
|
|
2145
2270
|
const scores = new ScoresStorageD1({
|
|
2146
2271
|
operations
|
|
2147
2272
|
});
|
|
2273
|
+
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2274
|
+
operations
|
|
2275
|
+
});
|
|
2276
|
+
const traces = new TracesStorageD1({
|
|
2277
|
+
operations
|
|
2278
|
+
});
|
|
2148
2279
|
const workflows = new WorkflowsStorageD1({
|
|
2149
2280
|
operations
|
|
2150
2281
|
});
|
|
@@ -2154,6 +2285,8 @@ var D1Store = class extends MastraStorage {
|
|
|
2154
2285
|
this.stores = {
|
|
2155
2286
|
operations,
|
|
2156
2287
|
scores,
|
|
2288
|
+
legacyEvals,
|
|
2289
|
+
traces,
|
|
2157
2290
|
workflows,
|
|
2158
2291
|
memory
|
|
2159
2292
|
};
|
|
@@ -2237,6 +2370,12 @@ var D1Store = class extends MastraStorage {
|
|
|
2237
2370
|
}) {
|
|
2238
2371
|
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2239
2372
|
}
|
|
2373
|
+
async getMessagesById({
|
|
2374
|
+
messageIds,
|
|
2375
|
+
format
|
|
2376
|
+
}) {
|
|
2377
|
+
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2378
|
+
}
|
|
2240
2379
|
async getMessagesPaginated({
|
|
2241
2380
|
threadId,
|
|
2242
2381
|
selectBy,
|
|
@@ -2249,9 +2388,9 @@ var D1Store = class extends MastraStorage {
|
|
|
2249
2388
|
runId,
|
|
2250
2389
|
stepId,
|
|
2251
2390
|
result,
|
|
2252
|
-
|
|
2391
|
+
runtimeContext
|
|
2253
2392
|
}) {
|
|
2254
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2393
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
|
|
2255
2394
|
}
|
|
2256
2395
|
async updateWorkflowState({
|
|
2257
2396
|
workflowName,
|
|
@@ -2271,15 +2410,8 @@ var D1Store = class extends MastraStorage {
|
|
|
2271
2410
|
async loadWorkflowSnapshot(params) {
|
|
2272
2411
|
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2273
2412
|
}
|
|
2274
|
-
async
|
|
2275
|
-
|
|
2276
|
-
fromDate,
|
|
2277
|
-
toDate,
|
|
2278
|
-
limit,
|
|
2279
|
-
offset,
|
|
2280
|
-
resourceId
|
|
2281
|
-
} = {}) {
|
|
2282
|
-
return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2413
|
+
async getWorkflowRuns(args = {}) {
|
|
2414
|
+
return this.stores.workflows.getWorkflowRuns(args);
|
|
2283
2415
|
}
|
|
2284
2416
|
async getWorkflowRunById({
|
|
2285
2417
|
runId,
|
|
@@ -2295,6 +2427,24 @@ var D1Store = class extends MastraStorage {
|
|
|
2295
2427
|
async batchInsert({ tableName, records }) {
|
|
2296
2428
|
return this.stores.operations.batchInsert({ tableName, records });
|
|
2297
2429
|
}
|
|
2430
|
+
/**
|
|
2431
|
+
* @deprecated use getTracesPaginated instead
|
|
2432
|
+
*/
|
|
2433
|
+
async getTraces(args) {
|
|
2434
|
+
return this.stores.traces.getTraces(args);
|
|
2435
|
+
}
|
|
2436
|
+
async getTracesPaginated(args) {
|
|
2437
|
+
return this.stores.traces.getTracesPaginated(args);
|
|
2438
|
+
}
|
|
2439
|
+
/**
|
|
2440
|
+
* @deprecated use getEvals instead
|
|
2441
|
+
*/
|
|
2442
|
+
async getEvalsByAgentName(agentName, type) {
|
|
2443
|
+
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2444
|
+
}
|
|
2445
|
+
async getEvals(options) {
|
|
2446
|
+
return this.stores.legacyEvals.getEvals(options);
|
|
2447
|
+
}
|
|
2298
2448
|
async updateMessages(_args) {
|
|
2299
2449
|
return this.stores.memory.updateMessages(_args);
|
|
2300
2450
|
}
|