@mastra/cloudflare-d1 0.0.0-remove-unused-import-20250909212718 → 0.0.0-remove-unused-model-providers-api-20251030210744
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 +169 -3
- package/dist/index.cjs +238 -319
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +239 -320
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +8 -7
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +8 -0
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +5 -11
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +15 -39
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +9 -9
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -18
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
var error = require('@mastra/core/error');
|
|
4
4
|
var storage = require('@mastra/core/storage');
|
|
5
5
|
var Cloudflare = require('cloudflare');
|
|
6
|
-
var utils = require('@mastra/core/utils');
|
|
7
6
|
var agent = require('@mastra/core/agent');
|
|
7
|
+
var utils = require('@mastra/core/utils');
|
|
8
|
+
var scores = require('@mastra/core/scores');
|
|
8
9
|
|
|
9
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
11
|
|
|
@@ -247,16 +248,6 @@ function isArrayOfRecords(value) {
|
|
|
247
248
|
}
|
|
248
249
|
function deserializeValue(value, type) {
|
|
249
250
|
if (value === null || value === void 0) return null;
|
|
250
|
-
if (type === "date" && typeof value === "string") {
|
|
251
|
-
return new Date(value);
|
|
252
|
-
}
|
|
253
|
-
if (type === "jsonb" && typeof value === "string") {
|
|
254
|
-
try {
|
|
255
|
-
return JSON.parse(value);
|
|
256
|
-
} catch {
|
|
257
|
-
return value;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
251
|
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
261
252
|
try {
|
|
262
253
|
return JSON.parse(value);
|
|
@@ -267,155 +258,7 @@ function deserializeValue(value, type) {
|
|
|
267
258
|
return value;
|
|
268
259
|
}
|
|
269
260
|
|
|
270
|
-
// src/storage/domains/
|
|
271
|
-
var LegacyEvalsStorageD1 = class extends storage.LegacyEvalsStorage {
|
|
272
|
-
operations;
|
|
273
|
-
constructor({ operations }) {
|
|
274
|
-
super();
|
|
275
|
-
this.operations = operations;
|
|
276
|
-
}
|
|
277
|
-
async getEvals(options) {
|
|
278
|
-
const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
|
|
279
|
-
const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
|
|
280
|
-
const conditions = [];
|
|
281
|
-
const queryParams = [];
|
|
282
|
-
if (agentName) {
|
|
283
|
-
conditions.push(`agent_name = ?`);
|
|
284
|
-
queryParams.push(agentName);
|
|
285
|
-
}
|
|
286
|
-
if (type === "test") {
|
|
287
|
-
conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
|
|
288
|
-
} else if (type === "live") {
|
|
289
|
-
conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
|
|
290
|
-
}
|
|
291
|
-
if (dateRange?.start) {
|
|
292
|
-
conditions.push(`created_at >= ?`);
|
|
293
|
-
queryParams.push(storage.serializeDate(dateRange.start));
|
|
294
|
-
}
|
|
295
|
-
if (dateRange?.end) {
|
|
296
|
-
conditions.push(`created_at <= ?`);
|
|
297
|
-
queryParams.push(storage.serializeDate(dateRange.end));
|
|
298
|
-
}
|
|
299
|
-
const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
|
|
300
|
-
if (conditions.length > 0) {
|
|
301
|
-
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
302
|
-
}
|
|
303
|
-
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
304
|
-
try {
|
|
305
|
-
const countResult = await this.operations.executeQuery({
|
|
306
|
-
sql: countSql,
|
|
307
|
-
params: countParams,
|
|
308
|
-
first: true
|
|
309
|
-
});
|
|
310
|
-
const total = Number(countResult?.count || 0);
|
|
311
|
-
const currentOffset = page * perPage;
|
|
312
|
-
if (total === 0) {
|
|
313
|
-
return {
|
|
314
|
-
evals: [],
|
|
315
|
-
total: 0,
|
|
316
|
-
page,
|
|
317
|
-
perPage,
|
|
318
|
-
hasMore: false
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
322
|
-
if (conditions.length > 0) {
|
|
323
|
-
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
324
|
-
}
|
|
325
|
-
dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
|
|
326
|
-
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
327
|
-
const rows = await this.operations.executeQuery({
|
|
328
|
-
sql: dataSql,
|
|
329
|
-
params: dataParams
|
|
330
|
-
});
|
|
331
|
-
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
332
|
-
const result = deserializeValue(row.result);
|
|
333
|
-
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
334
|
-
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
335
|
-
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
336
|
-
}
|
|
337
|
-
return {
|
|
338
|
-
input: row.input,
|
|
339
|
-
output: row.output,
|
|
340
|
-
result,
|
|
341
|
-
agentName: row.agent_name,
|
|
342
|
-
metricName: row.metric_name,
|
|
343
|
-
instructions: row.instructions,
|
|
344
|
-
testInfo,
|
|
345
|
-
globalRunId: row.global_run_id,
|
|
346
|
-
runId: row.run_id,
|
|
347
|
-
createdAt: row.created_at
|
|
348
|
-
};
|
|
349
|
-
});
|
|
350
|
-
const hasMore = currentOffset + evals.length < total;
|
|
351
|
-
return {
|
|
352
|
-
evals,
|
|
353
|
-
total,
|
|
354
|
-
page,
|
|
355
|
-
perPage,
|
|
356
|
-
hasMore
|
|
357
|
-
};
|
|
358
|
-
} catch (error$1) {
|
|
359
|
-
throw new error.MastraError(
|
|
360
|
-
{
|
|
361
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
362
|
-
domain: error.ErrorDomain.STORAGE,
|
|
363
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
364
|
-
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
365
|
-
details: { agentName: agentName ?? "", type: type ?? "" }
|
|
366
|
-
},
|
|
367
|
-
error$1
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* @deprecated use getEvals instead
|
|
373
|
-
*/
|
|
374
|
-
async getEvalsByAgentName(agentName, type) {
|
|
375
|
-
const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
|
|
376
|
-
try {
|
|
377
|
-
let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
|
|
378
|
-
if (type === "test") {
|
|
379
|
-
query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
|
|
380
|
-
} else if (type === "live") {
|
|
381
|
-
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
382
|
-
}
|
|
383
|
-
query.orderBy("created_at", "DESC");
|
|
384
|
-
const { sql, params } = query.build();
|
|
385
|
-
const results = await this.operations.executeQuery({ sql, params });
|
|
386
|
-
return isArrayOfRecords(results) ? results.map((row) => {
|
|
387
|
-
const result = deserializeValue(row.result);
|
|
388
|
-
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
389
|
-
return {
|
|
390
|
-
input: row.input || "",
|
|
391
|
-
output: row.output || "",
|
|
392
|
-
result,
|
|
393
|
-
agentName: row.agent_name || "",
|
|
394
|
-
metricName: row.metric_name || "",
|
|
395
|
-
instructions: row.instructions || "",
|
|
396
|
-
runId: row.run_id || "",
|
|
397
|
-
globalRunId: row.global_run_id || "",
|
|
398
|
-
createdAt: row.created_at || "",
|
|
399
|
-
testInfo
|
|
400
|
-
};
|
|
401
|
-
}) : [];
|
|
402
|
-
} catch (error$1) {
|
|
403
|
-
const mastraError = new error.MastraError(
|
|
404
|
-
{
|
|
405
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
406
|
-
domain: error.ErrorDomain.STORAGE,
|
|
407
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
408
|
-
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
409
|
-
details: { agentName }
|
|
410
|
-
},
|
|
411
|
-
error$1
|
|
412
|
-
);
|
|
413
|
-
this.logger?.error(mastraError.toString());
|
|
414
|
-
this.logger?.trackException(mastraError);
|
|
415
|
-
return [];
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
};
|
|
261
|
+
// src/storage/domains/memory/index.ts
|
|
419
262
|
var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
420
263
|
operations;
|
|
421
264
|
constructor({ operations }) {
|
|
@@ -928,10 +771,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
928
771
|
throw mastraError;
|
|
929
772
|
}
|
|
930
773
|
}
|
|
931
|
-
async
|
|
932
|
-
messageIds,
|
|
933
|
-
format
|
|
934
|
-
}) {
|
|
774
|
+
async listMessagesById({ messageIds }) {
|
|
935
775
|
if (messageIds.length === 0) return [];
|
|
936
776
|
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
937
777
|
const messages = [];
|
|
@@ -951,7 +791,6 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
951
791
|
});
|
|
952
792
|
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
953
793
|
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
954
|
-
if (format === `v1`) return list.get.all.v1();
|
|
955
794
|
return list.get.all.v2();
|
|
956
795
|
} catch (error$1) {
|
|
957
796
|
const mastraError = new error.MastraError(
|
|
@@ -969,6 +808,169 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
969
808
|
throw mastraError;
|
|
970
809
|
}
|
|
971
810
|
}
|
|
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
|
+
}
|
|
972
974
|
async getMessagesPaginated({
|
|
973
975
|
threadId,
|
|
974
976
|
resourceId,
|
|
@@ -1581,7 +1583,7 @@ function transformScoreRow(row) {
|
|
|
1581
1583
|
deserialized.analyzeStepResult = storage.safelyParseJSON(row.analyzeStepResult);
|
|
1582
1584
|
deserialized.metadata = storage.safelyParseJSON(row.metadata);
|
|
1583
1585
|
deserialized.additionalContext = storage.safelyParseJSON(row.additionalContext);
|
|
1584
|
-
deserialized.
|
|
1586
|
+
deserialized.requestContext = storage.safelyParseJSON(row.requestContext);
|
|
1585
1587
|
deserialized.entity = storage.safelyParseJSON(row.entity);
|
|
1586
1588
|
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1587
1589
|
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
@@ -1615,12 +1617,25 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1615
1617
|
}
|
|
1616
1618
|
}
|
|
1617
1619
|
async saveScore(score) {
|
|
1620
|
+
let parsedScore;
|
|
1621
|
+
try {
|
|
1622
|
+
parsedScore = scores.saveScorePayloadSchema.parse(score);
|
|
1623
|
+
} catch (error$1) {
|
|
1624
|
+
throw new error.MastraError(
|
|
1625
|
+
{
|
|
1626
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
|
|
1627
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1628
|
+
category: error.ErrorCategory.USER,
|
|
1629
|
+
details: { scoreId: score.id }
|
|
1630
|
+
},
|
|
1631
|
+
error$1
|
|
1632
|
+
);
|
|
1633
|
+
}
|
|
1618
1634
|
try {
|
|
1619
1635
|
const id = crypto.randomUUID();
|
|
1620
1636
|
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1621
|
-
const { input, ...rest } = score;
|
|
1622
1637
|
const serializedRecord = {};
|
|
1623
|
-
for (const [key, value] of Object.entries(
|
|
1638
|
+
for (const [key, value] of Object.entries(parsedScore)) {
|
|
1624
1639
|
if (value !== null && value !== void 0) {
|
|
1625
1640
|
if (typeof value === "object") {
|
|
1626
1641
|
serializedRecord[key] = JSON.stringify(value);
|
|
@@ -1632,7 +1647,6 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1632
1647
|
}
|
|
1633
1648
|
}
|
|
1634
1649
|
serializedRecord.id = id;
|
|
1635
|
-
serializedRecord.input = JSON.stringify(input);
|
|
1636
1650
|
serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1637
1651
|
serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1638
1652
|
const columns = Object.keys(serializedRecord);
|
|
@@ -1808,128 +1822,53 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1808
1822
|
);
|
|
1809
1823
|
}
|
|
1810
1824
|
}
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
operations;
|
|
1817
|
-
constructor({ operations }) {
|
|
1818
|
-
super();
|
|
1819
|
-
this.operations = operations;
|
|
1820
|
-
}
|
|
1821
|
-
async getTraces(args) {
|
|
1822
|
-
const paginatedArgs = {
|
|
1823
|
-
name: args.name,
|
|
1824
|
-
scope: args.scope,
|
|
1825
|
-
page: args.page,
|
|
1826
|
-
perPage: args.perPage,
|
|
1827
|
-
attributes: args.attributes,
|
|
1828
|
-
filters: args.filters,
|
|
1829
|
-
dateRange: args.fromDate || args.toDate ? {
|
|
1830
|
-
start: args.fromDate,
|
|
1831
|
-
end: args.toDate
|
|
1832
|
-
} : void 0
|
|
1833
|
-
};
|
|
1834
|
-
try {
|
|
1835
|
-
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1836
|
-
return result.traces;
|
|
1837
|
-
} catch (error$1) {
|
|
1838
|
-
throw new error.MastraError(
|
|
1839
|
-
{
|
|
1840
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1841
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1842
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1843
|
-
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1844
|
-
details: {
|
|
1845
|
-
name: args.name ?? "",
|
|
1846
|
-
scope: args.scope ?? ""
|
|
1847
|
-
}
|
|
1848
|
-
},
|
|
1849
|
-
error$1
|
|
1850
|
-
);
|
|
1851
|
-
}
|
|
1852
|
-
}
|
|
1853
|
-
async getTracesPaginated(args) {
|
|
1854
|
-
const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
|
|
1855
|
-
const fromDate = dateRange?.start;
|
|
1856
|
-
const toDate = dateRange?.end;
|
|
1857
|
-
const fullTableName = this.operations.getTableName(storage.TABLE_TRACES);
|
|
1825
|
+
async getScoresBySpan({
|
|
1826
|
+
traceId,
|
|
1827
|
+
spanId,
|
|
1828
|
+
pagination
|
|
1829
|
+
}) {
|
|
1858
1830
|
try {
|
|
1859
|
-
const
|
|
1860
|
-
const countQuery = createSqlBuilder().count().from(fullTableName).where("
|
|
1861
|
-
if (name) {
|
|
1862
|
-
dataQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1863
|
-
countQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1864
|
-
}
|
|
1865
|
-
if (scope) {
|
|
1866
|
-
dataQuery.andWhere("scope = ?", scope);
|
|
1867
|
-
countQuery.andWhere("scope = ?", scope);
|
|
1868
|
-
}
|
|
1869
|
-
if (attributes && Object.keys(attributes).length > 0) {
|
|
1870
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
1871
|
-
dataQuery.jsonLike("attributes", key, value);
|
|
1872
|
-
countQuery.jsonLike("attributes", key, value);
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1875
|
-
if (fromDate) {
|
|
1876
|
-
const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
|
|
1877
|
-
dataQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1878
|
-
countQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1879
|
-
}
|
|
1880
|
-
if (toDate) {
|
|
1881
|
-
const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
|
|
1882
|
-
dataQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1883
|
-
countQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1884
|
-
}
|
|
1885
|
-
const allDataResult = await this.operations.executeQuery(
|
|
1886
|
-
createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
|
|
1887
|
-
);
|
|
1888
|
-
console.log("allDataResult", allDataResult);
|
|
1831
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1832
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1889
1833
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1890
|
-
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1834
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1835
|
+
if (total === 0) {
|
|
1836
|
+
return {
|
|
1837
|
+
pagination: {
|
|
1838
|
+
total: 0,
|
|
1839
|
+
page: pagination.page,
|
|
1840
|
+
perPage: pagination.perPage,
|
|
1841
|
+
hasMore: false
|
|
1842
|
+
},
|
|
1843
|
+
scores: []
|
|
1844
|
+
};
|
|
1845
|
+
}
|
|
1846
|
+
const limit = pagination.perPage + 1;
|
|
1847
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limit).offset(pagination.page * pagination.perPage);
|
|
1848
|
+
const { sql, params } = selectQuery.build();
|
|
1849
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1850
|
+
const rows = Array.isArray(results) ? results : [];
|
|
1851
|
+
const scores = rows.slice(0, pagination.perPage).map(transformScoreRow);
|
|
1903
1852
|
return {
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1853
|
+
pagination: {
|
|
1854
|
+
total,
|
|
1855
|
+
page: pagination.page,
|
|
1856
|
+
perPage: pagination.perPage,
|
|
1857
|
+
hasMore: rows.length > pagination.perPage
|
|
1858
|
+
},
|
|
1859
|
+
scores
|
|
1909
1860
|
};
|
|
1910
1861
|
} catch (error$1) {
|
|
1911
|
-
|
|
1862
|
+
throw new error.MastraError(
|
|
1912
1863
|
{
|
|
1913
|
-
id: "
|
|
1864
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SPAN_FAILED",
|
|
1914
1865
|
domain: error.ErrorDomain.STORAGE,
|
|
1915
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
1916
|
-
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1917
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1866
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1918
1867
|
},
|
|
1919
1868
|
error$1
|
|
1920
1869
|
);
|
|
1921
|
-
this.logger?.error(mastraError.toString());
|
|
1922
|
-
this.logger?.trackException(mastraError);
|
|
1923
|
-
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1924
1870
|
}
|
|
1925
1871
|
}
|
|
1926
|
-
async batchTraceInsert({ records }) {
|
|
1927
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1928
|
-
await this.operations.batchInsert({
|
|
1929
|
-
tableName: storage.TABLE_TRACES,
|
|
1930
|
-
records
|
|
1931
|
-
});
|
|
1932
|
-
}
|
|
1933
1872
|
};
|
|
1934
1873
|
var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
1935
1874
|
operations;
|
|
@@ -1942,7 +1881,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
1942
1881
|
// runId,
|
|
1943
1882
|
// stepId,
|
|
1944
1883
|
// result,
|
|
1945
|
-
//
|
|
1884
|
+
// requestContext,
|
|
1946
1885
|
}) {
|
|
1947
1886
|
throw new Error("Method not implemented.");
|
|
1948
1887
|
}
|
|
@@ -1956,6 +1895,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
1956
1895
|
async persistWorkflowSnapshot({
|
|
1957
1896
|
workflowName,
|
|
1958
1897
|
runId,
|
|
1898
|
+
resourceId,
|
|
1959
1899
|
snapshot
|
|
1960
1900
|
}) {
|
|
1961
1901
|
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
@@ -1966,11 +1906,13 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
1966
1906
|
});
|
|
1967
1907
|
const persisting = currentSnapshot ? {
|
|
1968
1908
|
...currentSnapshot,
|
|
1909
|
+
resourceId,
|
|
1969
1910
|
snapshot: JSON.stringify(snapshot),
|
|
1970
1911
|
updatedAt: now
|
|
1971
1912
|
} : {
|
|
1972
1913
|
workflow_name: workflowName,
|
|
1973
1914
|
run_id: runId,
|
|
1915
|
+
resourceId,
|
|
1974
1916
|
snapshot,
|
|
1975
1917
|
createdAt: now,
|
|
1976
1918
|
updatedAt: now
|
|
@@ -2043,7 +1985,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
2043
1985
|
resourceId: row.resourceId
|
|
2044
1986
|
};
|
|
2045
1987
|
}
|
|
2046
|
-
async
|
|
1988
|
+
async listWorkflowRuns({
|
|
2047
1989
|
workflowName,
|
|
2048
1990
|
fromDate,
|
|
2049
1991
|
toDate,
|
|
@@ -2209,12 +2151,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2209
2151
|
const scores = new ScoresStorageD1({
|
|
2210
2152
|
operations
|
|
2211
2153
|
});
|
|
2212
|
-
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2213
|
-
operations
|
|
2214
|
-
});
|
|
2215
|
-
const traces = new TracesStorageD1({
|
|
2216
|
-
operations
|
|
2217
|
-
});
|
|
2218
2154
|
const workflows = new WorkflowsStorageD1({
|
|
2219
2155
|
operations
|
|
2220
2156
|
});
|
|
@@ -2224,8 +2160,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2224
2160
|
this.stores = {
|
|
2225
2161
|
operations,
|
|
2226
2162
|
scores,
|
|
2227
|
-
legacyEvals,
|
|
2228
|
-
traces,
|
|
2229
2163
|
workflows,
|
|
2230
2164
|
memory
|
|
2231
2165
|
};
|
|
@@ -2236,7 +2170,8 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2236
2170
|
resourceWorkingMemory: true,
|
|
2237
2171
|
hasColumn: true,
|
|
2238
2172
|
createTable: true,
|
|
2239
|
-
deleteMessages: false
|
|
2173
|
+
deleteMessages: false,
|
|
2174
|
+
getScoresBySpan: true
|
|
2240
2175
|
};
|
|
2241
2176
|
}
|
|
2242
2177
|
async createTable({
|
|
@@ -2308,12 +2243,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2308
2243
|
}) {
|
|
2309
2244
|
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2310
2245
|
}
|
|
2311
|
-
async getMessagesById({
|
|
2312
|
-
messageIds,
|
|
2313
|
-
format
|
|
2314
|
-
}) {
|
|
2315
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2316
|
-
}
|
|
2317
2246
|
async getMessagesPaginated({
|
|
2318
2247
|
threadId,
|
|
2319
2248
|
selectBy,
|
|
@@ -2326,9 +2255,9 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2326
2255
|
runId,
|
|
2327
2256
|
stepId,
|
|
2328
2257
|
result,
|
|
2329
|
-
|
|
2258
|
+
requestContext
|
|
2330
2259
|
}) {
|
|
2331
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2260
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2332
2261
|
}
|
|
2333
2262
|
async updateWorkflowState({
|
|
2334
2263
|
workflowName,
|
|
@@ -2340,14 +2269,15 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2340
2269
|
async persistWorkflowSnapshot({
|
|
2341
2270
|
workflowName,
|
|
2342
2271
|
runId,
|
|
2272
|
+
resourceId,
|
|
2343
2273
|
snapshot
|
|
2344
2274
|
}) {
|
|
2345
|
-
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
|
|
2275
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2346
2276
|
}
|
|
2347
2277
|
async loadWorkflowSnapshot(params) {
|
|
2348
2278
|
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2349
2279
|
}
|
|
2350
|
-
async
|
|
2280
|
+
async listWorkflowRuns({
|
|
2351
2281
|
workflowName,
|
|
2352
2282
|
fromDate,
|
|
2353
2283
|
toDate,
|
|
@@ -2355,7 +2285,7 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2355
2285
|
offset,
|
|
2356
2286
|
resourceId
|
|
2357
2287
|
} = {}) {
|
|
2358
|
-
return this.stores.workflows.
|
|
2288
|
+
return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2359
2289
|
}
|
|
2360
2290
|
async getWorkflowRunById({
|
|
2361
2291
|
runId,
|
|
@@ -2371,24 +2301,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2371
2301
|
async batchInsert({ tableName, records }) {
|
|
2372
2302
|
return this.stores.operations.batchInsert({ tableName, records });
|
|
2373
2303
|
}
|
|
2374
|
-
/**
|
|
2375
|
-
* @deprecated use getTracesPaginated instead
|
|
2376
|
-
*/
|
|
2377
|
-
async getTraces(args) {
|
|
2378
|
-
return this.stores.traces.getTraces(args);
|
|
2379
|
-
}
|
|
2380
|
-
async getTracesPaginated(args) {
|
|
2381
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2382
|
-
}
|
|
2383
|
-
/**
|
|
2384
|
-
* @deprecated use getEvals instead
|
|
2385
|
-
*/
|
|
2386
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2387
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2388
|
-
}
|
|
2389
|
-
async getEvals(options) {
|
|
2390
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2391
|
-
}
|
|
2392
2304
|
async updateMessages(_args) {
|
|
2393
2305
|
return this.stores.memory.updateMessages(_args);
|
|
2394
2306
|
}
|
|
@@ -2437,6 +2349,13 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2437
2349
|
}) {
|
|
2438
2350
|
return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2439
2351
|
}
|
|
2352
|
+
async getScoresBySpan({
|
|
2353
|
+
traceId,
|
|
2354
|
+
spanId,
|
|
2355
|
+
pagination
|
|
2356
|
+
}) {
|
|
2357
|
+
return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
|
|
2358
|
+
}
|
|
2440
2359
|
/**
|
|
2441
2360
|
* Close the database connection
|
|
2442
2361
|
* No explicit cleanup needed for D1 in either REST or Workers Binding mode
|