@mastra/cloudflare-d1 0.13.8 → 1.0.0-beta.1
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 +198 -14
- package/README.md +10 -5
- package/dist/index.cjs +230 -592
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +230 -592
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +13 -44
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +5 -5
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +3 -10
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +23 -88
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +14 -16
- 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,9 +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');
|
|
8
|
-
var
|
|
7
|
+
var utils = require('@mastra/core/utils');
|
|
8
|
+
var evals = require('@mastra/core/evals');
|
|
9
9
|
|
|
10
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
|
|
@@ -248,16 +248,6 @@ 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
|
-
}
|
|
261
251
|
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
262
252
|
try {
|
|
263
253
|
return JSON.parse(value);
|
|
@@ -268,155 +258,7 @@ function deserializeValue(value, type) {
|
|
|
268
258
|
return value;
|
|
269
259
|
}
|
|
270
260
|
|
|
271
|
-
// src/storage/domains/
|
|
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
|
-
};
|
|
261
|
+
// src/storage/domains/memory/index.ts
|
|
420
262
|
var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
421
263
|
operations;
|
|
422
264
|
constructor({ operations }) {
|
|
@@ -564,39 +406,22 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
564
406
|
return null;
|
|
565
407
|
}
|
|
566
408
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
try {
|
|
573
|
-
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
574
|
-
const { sql, params } = query.build();
|
|
575
|
-
const results = await this.operations.executeQuery({ sql, params });
|
|
576
|
-
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
577
|
-
...thread,
|
|
578
|
-
createdAt: storage.ensureDate(thread.createdAt),
|
|
579
|
-
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
580
|
-
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
581
|
-
}));
|
|
582
|
-
} catch (error$1) {
|
|
583
|
-
const mastraError = new error.MastraError(
|
|
409
|
+
async listThreadsByResourceId(args) {
|
|
410
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
411
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
412
|
+
if (page < 0) {
|
|
413
|
+
throw new error.MastraError(
|
|
584
414
|
{
|
|
585
|
-
id: "
|
|
415
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
586
416
|
domain: error.ErrorDomain.STORAGE,
|
|
587
|
-
category: error.ErrorCategory.
|
|
588
|
-
|
|
589
|
-
details: { resourceId }
|
|
417
|
+
category: error.ErrorCategory.USER,
|
|
418
|
+
details: { page }
|
|
590
419
|
},
|
|
591
|
-
|
|
420
|
+
new Error("page must be >= 0")
|
|
592
421
|
);
|
|
593
|
-
this.logger?.error(mastraError.toString());
|
|
594
|
-
this.logger?.trackException(mastraError);
|
|
595
|
-
return [];
|
|
596
422
|
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
const { resourceId, page, perPage } = args;
|
|
423
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
424
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
600
425
|
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
601
426
|
const mapRowToStorageThreadType = (row) => ({
|
|
602
427
|
...row,
|
|
@@ -608,20 +433,21 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
608
433
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
609
434
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
610
435
|
const total = Number(countResult?.[0]?.count ?? 0);
|
|
611
|
-
const
|
|
436
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
437
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy(field, direction).limit(limitValue).offset(offset);
|
|
612
438
|
const results = await this.operations.executeQuery(selectQuery.build());
|
|
613
439
|
const threads = results.map(mapRowToStorageThreadType);
|
|
614
440
|
return {
|
|
615
441
|
threads,
|
|
616
442
|
total,
|
|
617
443
|
page,
|
|
618
|
-
perPage,
|
|
619
|
-
hasMore:
|
|
444
|
+
perPage: perPageForResponse,
|
|
445
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
620
446
|
};
|
|
621
447
|
} catch (error$1) {
|
|
622
448
|
const mastraError = new error.MastraError(
|
|
623
449
|
{
|
|
624
|
-
id: "
|
|
450
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_THREADS_BY_RESOURCE_ID_ERROR",
|
|
625
451
|
domain: error.ErrorDomain.STORAGE,
|
|
626
452
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
627
453
|
text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
@@ -635,7 +461,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
635
461
|
threads: [],
|
|
636
462
|
total: 0,
|
|
637
463
|
page,
|
|
638
|
-
perPage,
|
|
464
|
+
perPage: perPageForResponse,
|
|
639
465
|
hasMore: false
|
|
640
466
|
};
|
|
641
467
|
}
|
|
@@ -745,8 +571,8 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
745
571
|
}
|
|
746
572
|
}
|
|
747
573
|
async saveMessages(args) {
|
|
748
|
-
const { messages
|
|
749
|
-
if (messages.length === 0) return [];
|
|
574
|
+
const { messages } = args;
|
|
575
|
+
if (messages.length === 0) return { messages: [] };
|
|
750
576
|
try {
|
|
751
577
|
const now = /* @__PURE__ */ new Date();
|
|
752
578
|
const threadId = messages[0]?.threadId;
|
|
@@ -794,8 +620,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
794
620
|
]);
|
|
795
621
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
796
622
|
const list = new agent.MessageList().add(messages, "memory");
|
|
797
|
-
|
|
798
|
-
return list.get.all.v1();
|
|
623
|
+
return { messages: list.get.all.db() };
|
|
799
624
|
} catch (error$1) {
|
|
800
625
|
throw new error.MastraError(
|
|
801
626
|
{
|
|
@@ -808,9 +633,8 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
808
633
|
);
|
|
809
634
|
}
|
|
810
635
|
}
|
|
811
|
-
async _getIncludedMessages(threadId,
|
|
636
|
+
async _getIncludedMessages(threadId, include) {
|
|
812
637
|
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
813
|
-
const include = selectBy?.include;
|
|
814
638
|
if (!include) return null;
|
|
815
639
|
const unionQueries = [];
|
|
816
640
|
const params = [];
|
|
@@ -866,74 +690,8 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
866
690
|
});
|
|
867
691
|
return processedMessages;
|
|
868
692
|
}
|
|
869
|
-
async
|
|
870
|
-
|
|
871
|
-
resourceId,
|
|
872
|
-
selectBy,
|
|
873
|
-
format
|
|
874
|
-
}) {
|
|
875
|
-
try {
|
|
876
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
877
|
-
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
878
|
-
const limit = storage.resolveMessageLimit({
|
|
879
|
-
last: selectBy?.last,
|
|
880
|
-
defaultLimit: 40
|
|
881
|
-
});
|
|
882
|
-
const include = selectBy?.include || [];
|
|
883
|
-
const messages = [];
|
|
884
|
-
if (include.length) {
|
|
885
|
-
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
886
|
-
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
887
|
-
}
|
|
888
|
-
const excludeIds = messages.map((m) => m.id);
|
|
889
|
-
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
890
|
-
if (excludeIds.length > 0) {
|
|
891
|
-
query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
|
|
892
|
-
}
|
|
893
|
-
query.orderBy("createdAt", "DESC").limit(limit);
|
|
894
|
-
const { sql, params } = query.build();
|
|
895
|
-
const result = await this.operations.executeQuery({ sql, params });
|
|
896
|
-
if (Array.isArray(result)) messages.push(...result);
|
|
897
|
-
messages.sort((a, b) => {
|
|
898
|
-
const aRecord = a;
|
|
899
|
-
const bRecord = b;
|
|
900
|
-
const timeA = new Date(aRecord.createdAt).getTime();
|
|
901
|
-
const timeB = new Date(bRecord.createdAt).getTime();
|
|
902
|
-
return timeA - timeB;
|
|
903
|
-
});
|
|
904
|
-
const processedMessages = messages.map((message) => {
|
|
905
|
-
const processedMsg = {};
|
|
906
|
-
for (const [key, value] of Object.entries(message)) {
|
|
907
|
-
if (key === `type` && value === `v2`) continue;
|
|
908
|
-
processedMsg[key] = deserializeValue(value);
|
|
909
|
-
}
|
|
910
|
-
return processedMsg;
|
|
911
|
-
});
|
|
912
|
-
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
913
|
-
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
914
|
-
if (format === `v2`) return list.get.all.v2();
|
|
915
|
-
return list.get.all.v1();
|
|
916
|
-
} catch (error$1) {
|
|
917
|
-
const mastraError = new error.MastraError(
|
|
918
|
-
{
|
|
919
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
|
|
920
|
-
domain: error.ErrorDomain.STORAGE,
|
|
921
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
922
|
-
text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
923
|
-
details: { threadId, resourceId: resourceId ?? "" }
|
|
924
|
-
},
|
|
925
|
-
error$1
|
|
926
|
-
);
|
|
927
|
-
this.logger?.error(mastraError.toString());
|
|
928
|
-
this.logger?.trackException(mastraError);
|
|
929
|
-
throw mastraError;
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
async getMessagesById({
|
|
933
|
-
messageIds,
|
|
934
|
-
format
|
|
935
|
-
}) {
|
|
936
|
-
if (messageIds.length === 0) return [];
|
|
693
|
+
async listMessagesById({ messageIds }) {
|
|
694
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
937
695
|
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
938
696
|
const messages = [];
|
|
939
697
|
try {
|
|
@@ -952,12 +710,11 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
952
710
|
});
|
|
953
711
|
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
954
712
|
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
955
|
-
|
|
956
|
-
return list.get.all.v2();
|
|
713
|
+
return { messages: list.get.all.db() };
|
|
957
714
|
} catch (error$1) {
|
|
958
715
|
const mastraError = new error.MastraError(
|
|
959
716
|
{
|
|
960
|
-
id: "
|
|
717
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_BY_ID_ERROR",
|
|
961
718
|
domain: error.ErrorDomain.STORAGE,
|
|
962
719
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
963
720
|
text: `Failed to retrieve messages by ID: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
@@ -970,118 +727,157 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
970
727
|
throw mastraError;
|
|
971
728
|
}
|
|
972
729
|
}
|
|
973
|
-
async
|
|
974
|
-
threadId,
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
730
|
+
async listMessages(args) {
|
|
731
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
732
|
+
if (!threadId.trim()) {
|
|
733
|
+
throw new error.MastraError(
|
|
734
|
+
{
|
|
735
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
736
|
+
domain: error.ErrorDomain.STORAGE,
|
|
737
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
738
|
+
details: { threadId }
|
|
739
|
+
},
|
|
740
|
+
new Error("threadId must be a non-empty string")
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
if (page < 0) {
|
|
744
|
+
throw new error.MastraError(
|
|
745
|
+
{
|
|
746
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_PAGE",
|
|
747
|
+
domain: error.ErrorDomain.STORAGE,
|
|
748
|
+
category: error.ErrorCategory.USER,
|
|
749
|
+
details: { page }
|
|
750
|
+
},
|
|
751
|
+
new Error("page must be >= 0")
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
755
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
984
756
|
try {
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
757
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
758
|
+
let query = `
|
|
759
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
760
|
+
FROM ${fullTableName}
|
|
761
|
+
WHERE thread_id = ?
|
|
762
|
+
`;
|
|
763
|
+
const queryParams = [threadId];
|
|
764
|
+
if (resourceId) {
|
|
765
|
+
query += ` AND resourceId = ?`;
|
|
766
|
+
queryParams.push(resourceId);
|
|
989
767
|
}
|
|
990
|
-
const
|
|
991
|
-
if (
|
|
992
|
-
|
|
768
|
+
const dateRange = filter?.dateRange;
|
|
769
|
+
if (dateRange?.start) {
|
|
770
|
+
const startDate = dateRange.start instanceof Date ? storage.serializeDate(dateRange.start) : storage.serializeDate(new Date(dateRange.start));
|
|
771
|
+
query += ` AND createdAt >= ?`;
|
|
772
|
+
queryParams.push(startDate);
|
|
993
773
|
}
|
|
994
|
-
if (
|
|
995
|
-
|
|
774
|
+
if (dateRange?.end) {
|
|
775
|
+
const endDate = dateRange.end instanceof Date ? storage.serializeDate(dateRange.end) : storage.serializeDate(new Date(dateRange.end));
|
|
776
|
+
query += ` AND createdAt <= ?`;
|
|
777
|
+
queryParams.push(endDate);
|
|
996
778
|
}
|
|
997
|
-
const
|
|
779
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
780
|
+
query += ` ORDER BY "${field}" ${direction}`;
|
|
781
|
+
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
782
|
+
query += ` LIMIT ? OFFSET ?`;
|
|
783
|
+
queryParams.push(perPage, offset);
|
|
784
|
+
}
|
|
785
|
+
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
786
|
+
const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
|
|
787
|
+
const processedMsg = {};
|
|
788
|
+
for (const [key, value] of Object.entries(message)) {
|
|
789
|
+
if (key === `type` && value === `v2`) continue;
|
|
790
|
+
processedMsg[key] = deserializeValue(value);
|
|
791
|
+
}
|
|
792
|
+
return processedMsg;
|
|
793
|
+
});
|
|
794
|
+
const paginatedCount = paginatedMessages.length;
|
|
795
|
+
let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
|
|
796
|
+
const countParams = [threadId];
|
|
797
|
+
if (resourceId) {
|
|
798
|
+
countQuery += ` AND resourceId = ?`;
|
|
799
|
+
countParams.push(resourceId);
|
|
800
|
+
}
|
|
801
|
+
if (dateRange?.start) {
|
|
802
|
+
const startDate = dateRange.start instanceof Date ? storage.serializeDate(dateRange.start) : storage.serializeDate(new Date(dateRange.start));
|
|
803
|
+
countQuery += ` AND createdAt >= ?`;
|
|
804
|
+
countParams.push(startDate);
|
|
805
|
+
}
|
|
806
|
+
if (dateRange?.end) {
|
|
807
|
+
const endDate = dateRange.end instanceof Date ? storage.serializeDate(dateRange.end) : storage.serializeDate(new Date(dateRange.end));
|
|
808
|
+
countQuery += ` AND createdAt <= ?`;
|
|
809
|
+
countParams.push(endDate);
|
|
810
|
+
}
|
|
811
|
+
const countResult = await this.operations.executeQuery({ sql: countQuery, params: countParams });
|
|
998
812
|
const total = Number(countResult[0]?.count ?? 0);
|
|
999
|
-
if (total === 0 &&
|
|
813
|
+
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
|
|
1000
814
|
return {
|
|
1001
815
|
messages: [],
|
|
1002
816
|
total: 0,
|
|
1003
817
|
page,
|
|
1004
|
-
perPage,
|
|
818
|
+
perPage: perPageForResponse,
|
|
1005
819
|
hasMore: false
|
|
1006
820
|
};
|
|
1007
821
|
}
|
|
1008
|
-
const
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
1022
|
-
query = `
|
|
1023
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1024
|
-
FROM ${fullTableName}
|
|
1025
|
-
WHERE thread_id = ?
|
|
1026
|
-
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1027
|
-
${toDate ? "AND createdAt <= ?" : ""}
|
|
1028
|
-
${excludeCondition}
|
|
1029
|
-
ORDER BY createdAt DESC
|
|
1030
|
-
LIMIT ?
|
|
1031
|
-
`;
|
|
1032
|
-
queryParams.push(selectBy.last);
|
|
1033
|
-
} else {
|
|
1034
|
-
query = `
|
|
1035
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1036
|
-
FROM ${fullTableName}
|
|
1037
|
-
WHERE thread_id = ?
|
|
1038
|
-
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1039
|
-
${toDate ? "AND createdAt <= ?" : ""}
|
|
1040
|
-
${excludeCondition}
|
|
1041
|
-
ORDER BY createdAt DESC
|
|
1042
|
-
LIMIT ? OFFSET ?
|
|
1043
|
-
`;
|
|
1044
|
-
queryParams.push(perPage, page * perPage);
|
|
822
|
+
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
823
|
+
let includeMessages = [];
|
|
824
|
+
if (include && include.length > 0) {
|
|
825
|
+
const includeResult = await this._getIncludedMessages(threadId, include);
|
|
826
|
+
if (Array.isArray(includeResult)) {
|
|
827
|
+
includeMessages = includeResult;
|
|
828
|
+
for (const includeMsg of includeMessages) {
|
|
829
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
830
|
+
paginatedMessages.push(includeMsg);
|
|
831
|
+
messageIds.add(includeMsg.id);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
}
|
|
1045
835
|
}
|
|
1046
|
-
const
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
836
|
+
const list = new agent.MessageList().add(paginatedMessages, "memory");
|
|
837
|
+
let finalMessages = list.get.all.db();
|
|
838
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
839
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
840
|
+
const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
841
|
+
const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
842
|
+
if (aValue === bValue) {
|
|
843
|
+
return a.id.localeCompare(b.id);
|
|
1052
844
|
}
|
|
1053
|
-
|
|
845
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
846
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
847
|
+
}
|
|
848
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
1054
849
|
});
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
1059
|
-
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
850
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
851
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
852
|
+
const hasMore = perPageInput === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
|
|
1060
853
|
return {
|
|
1061
|
-
messages,
|
|
854
|
+
messages: finalMessages,
|
|
1062
855
|
total,
|
|
1063
856
|
page,
|
|
1064
|
-
perPage,
|
|
1065
|
-
hasMore
|
|
857
|
+
perPage: perPageForResponse,
|
|
858
|
+
hasMore
|
|
1066
859
|
};
|
|
1067
860
|
} catch (error$1) {
|
|
1068
861
|
const mastraError = new error.MastraError(
|
|
1069
862
|
{
|
|
1070
|
-
id: "
|
|
863
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
|
|
1071
864
|
domain: error.ErrorDomain.STORAGE,
|
|
1072
865
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
1073
|
-
text: `Failed to
|
|
1074
|
-
details: {
|
|
866
|
+
text: `Failed to list messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
867
|
+
details: {
|
|
868
|
+
threadId,
|
|
869
|
+
resourceId: resourceId ?? ""
|
|
870
|
+
}
|
|
1075
871
|
},
|
|
1076
872
|
error$1
|
|
1077
873
|
);
|
|
1078
|
-
this.logger?.error(mastraError.toString());
|
|
1079
|
-
this.logger?.trackException(mastraError);
|
|
874
|
+
this.logger?.error?.(mastraError.toString());
|
|
875
|
+
this.logger?.trackException?.(mastraError);
|
|
1080
876
|
return {
|
|
1081
877
|
messages: [],
|
|
1082
878
|
total: 0,
|
|
1083
879
|
page,
|
|
1084
|
-
perPage,
|
|
880
|
+
perPage: perPageForResponse,
|
|
1085
881
|
hasMore: false
|
|
1086
882
|
};
|
|
1087
883
|
}
|
|
@@ -1582,7 +1378,7 @@ function transformScoreRow(row) {
|
|
|
1582
1378
|
deserialized.analyzeStepResult = storage.safelyParseJSON(row.analyzeStepResult);
|
|
1583
1379
|
deserialized.metadata = storage.safelyParseJSON(row.metadata);
|
|
1584
1380
|
deserialized.additionalContext = storage.safelyParseJSON(row.additionalContext);
|
|
1585
|
-
deserialized.
|
|
1381
|
+
deserialized.requestContext = storage.safelyParseJSON(row.requestContext);
|
|
1586
1382
|
deserialized.entity = storage.safelyParseJSON(row.entity);
|
|
1587
1383
|
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1588
1384
|
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
@@ -1618,7 +1414,7 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1618
1414
|
async saveScore(score) {
|
|
1619
1415
|
let parsedScore;
|
|
1620
1416
|
try {
|
|
1621
|
-
parsedScore =
|
|
1417
|
+
parsedScore = evals.saveScorePayloadSchema.parse(score);
|
|
1622
1418
|
} catch (error$1) {
|
|
1623
1419
|
throw new error.MastraError(
|
|
1624
1420
|
{
|
|
@@ -1666,7 +1462,7 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1666
1462
|
);
|
|
1667
1463
|
}
|
|
1668
1464
|
}
|
|
1669
|
-
async
|
|
1465
|
+
async listScoresByScorerId({
|
|
1670
1466
|
scorerId,
|
|
1671
1467
|
entityId,
|
|
1672
1468
|
entityType,
|
|
@@ -1674,6 +1470,9 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1674
1470
|
pagination
|
|
1675
1471
|
}) {
|
|
1676
1472
|
try {
|
|
1473
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1474
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1475
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1677
1476
|
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1678
1477
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1679
1478
|
if (entityId) {
|
|
@@ -1691,13 +1490,15 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1691
1490
|
return {
|
|
1692
1491
|
pagination: {
|
|
1693
1492
|
total: 0,
|
|
1694
|
-
page
|
|
1695
|
-
perPage:
|
|
1493
|
+
page,
|
|
1494
|
+
perPage: perPageForResponse,
|
|
1696
1495
|
hasMore: false
|
|
1697
1496
|
},
|
|
1698
1497
|
scores: []
|
|
1699
1498
|
};
|
|
1700
1499
|
}
|
|
1500
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1501
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1701
1502
|
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
|
|
1702
1503
|
if (entityId) {
|
|
1703
1504
|
selectQuery.andWhere("entityId = ?", entityId);
|
|
@@ -1708,16 +1509,16 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1708
1509
|
if (source) {
|
|
1709
1510
|
selectQuery.andWhere("source = ?", source);
|
|
1710
1511
|
}
|
|
1711
|
-
selectQuery.limit(
|
|
1512
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1712
1513
|
const { sql, params } = selectQuery.build();
|
|
1713
1514
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1714
1515
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1715
1516
|
return {
|
|
1716
1517
|
pagination: {
|
|
1717
1518
|
total,
|
|
1718
|
-
page
|
|
1719
|
-
perPage:
|
|
1720
|
-
hasMore:
|
|
1519
|
+
page,
|
|
1520
|
+
perPage: perPageForResponse,
|
|
1521
|
+
hasMore: end < total
|
|
1721
1522
|
},
|
|
1722
1523
|
scores
|
|
1723
1524
|
};
|
|
@@ -1732,11 +1533,14 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1732
1533
|
);
|
|
1733
1534
|
}
|
|
1734
1535
|
}
|
|
1735
|
-
async
|
|
1536
|
+
async listScoresByRunId({
|
|
1736
1537
|
runId,
|
|
1737
1538
|
pagination
|
|
1738
1539
|
}) {
|
|
1739
1540
|
try {
|
|
1541
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1542
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1543
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1740
1544
|
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1741
1545
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1742
1546
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1745,23 +1549,25 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1745
1549
|
return {
|
|
1746
1550
|
pagination: {
|
|
1747
1551
|
total: 0,
|
|
1748
|
-
page
|
|
1749
|
-
perPage:
|
|
1552
|
+
page,
|
|
1553
|
+
perPage: perPageForResponse,
|
|
1750
1554
|
hasMore: false
|
|
1751
1555
|
},
|
|
1752
1556
|
scores: []
|
|
1753
1557
|
};
|
|
1754
1558
|
}
|
|
1755
|
-
const
|
|
1559
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1560
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1561
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(limitValue).offset(start);
|
|
1756
1562
|
const { sql, params } = selectQuery.build();
|
|
1757
1563
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1758
1564
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1759
1565
|
return {
|
|
1760
1566
|
pagination: {
|
|
1761
1567
|
total,
|
|
1762
|
-
page
|
|
1763
|
-
perPage:
|
|
1764
|
-
hasMore:
|
|
1568
|
+
page,
|
|
1569
|
+
perPage: perPageForResponse,
|
|
1570
|
+
hasMore: end < total
|
|
1765
1571
|
},
|
|
1766
1572
|
scores
|
|
1767
1573
|
};
|
|
@@ -1776,12 +1582,15 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1776
1582
|
);
|
|
1777
1583
|
}
|
|
1778
1584
|
}
|
|
1779
|
-
async
|
|
1585
|
+
async listScoresByEntityId({
|
|
1780
1586
|
entityId,
|
|
1781
1587
|
entityType,
|
|
1782
1588
|
pagination
|
|
1783
1589
|
}) {
|
|
1784
1590
|
try {
|
|
1591
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1592
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1593
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1785
1594
|
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1786
1595
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1787
1596
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1790,23 +1599,25 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1790
1599
|
return {
|
|
1791
1600
|
pagination: {
|
|
1792
1601
|
total: 0,
|
|
1793
|
-
page
|
|
1794
|
-
perPage:
|
|
1602
|
+
page,
|
|
1603
|
+
perPage: perPageForResponse,
|
|
1795
1604
|
hasMore: false
|
|
1796
1605
|
},
|
|
1797
1606
|
scores: []
|
|
1798
1607
|
};
|
|
1799
1608
|
}
|
|
1800
|
-
const
|
|
1609
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1610
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1611
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(limitValue).offset(start);
|
|
1801
1612
|
const { sql, params } = selectQuery.build();
|
|
1802
1613
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1803
1614
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1804
1615
|
return {
|
|
1805
1616
|
pagination: {
|
|
1806
1617
|
total,
|
|
1807
|
-
page
|
|
1808
|
-
perPage:
|
|
1809
|
-
hasMore:
|
|
1618
|
+
page,
|
|
1619
|
+
perPage: perPageForResponse,
|
|
1620
|
+
hasMore: end < total
|
|
1810
1621
|
},
|
|
1811
1622
|
scores
|
|
1812
1623
|
};
|
|
@@ -1821,12 +1632,15 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1821
1632
|
);
|
|
1822
1633
|
}
|
|
1823
1634
|
}
|
|
1824
|
-
async
|
|
1635
|
+
async listScoresBySpan({
|
|
1825
1636
|
traceId,
|
|
1826
1637
|
spanId,
|
|
1827
1638
|
pagination
|
|
1828
1639
|
}) {
|
|
1829
1640
|
try {
|
|
1641
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1642
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1643
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1830
1644
|
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1831
1645
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1832
1646
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1835,25 +1649,25 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1835
1649
|
return {
|
|
1836
1650
|
pagination: {
|
|
1837
1651
|
total: 0,
|
|
1838
|
-
page
|
|
1839
|
-
perPage:
|
|
1652
|
+
page,
|
|
1653
|
+
perPage: perPageForResponse,
|
|
1840
1654
|
hasMore: false
|
|
1841
1655
|
},
|
|
1842
1656
|
scores: []
|
|
1843
1657
|
};
|
|
1844
1658
|
}
|
|
1845
|
-
const
|
|
1846
|
-
const
|
|
1659
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1660
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1661
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limitValue).offset(start);
|
|
1847
1662
|
const { sql, params } = selectQuery.build();
|
|
1848
1663
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1849
|
-
const
|
|
1850
|
-
const scores = rows.slice(0, pagination.perPage).map(transformScoreRow);
|
|
1664
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1851
1665
|
return {
|
|
1852
1666
|
pagination: {
|
|
1853
1667
|
total,
|
|
1854
|
-
page
|
|
1855
|
-
perPage:
|
|
1856
|
-
hasMore:
|
|
1668
|
+
page,
|
|
1669
|
+
perPage: perPageForResponse,
|
|
1670
|
+
hasMore: end < total
|
|
1857
1671
|
},
|
|
1858
1672
|
scores
|
|
1859
1673
|
};
|
|
@@ -1869,128 +1683,6 @@ var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
|
1869
1683
|
}
|
|
1870
1684
|
}
|
|
1871
1685
|
};
|
|
1872
|
-
function isArrayOfRecords2(value) {
|
|
1873
|
-
return value && Array.isArray(value) && value.length > 0;
|
|
1874
|
-
}
|
|
1875
|
-
var TracesStorageD1 = class extends storage.TracesStorage {
|
|
1876
|
-
operations;
|
|
1877
|
-
constructor({ operations }) {
|
|
1878
|
-
super();
|
|
1879
|
-
this.operations = operations;
|
|
1880
|
-
}
|
|
1881
|
-
async getTraces(args) {
|
|
1882
|
-
const paginatedArgs = {
|
|
1883
|
-
name: args.name,
|
|
1884
|
-
scope: args.scope,
|
|
1885
|
-
page: args.page,
|
|
1886
|
-
perPage: args.perPage,
|
|
1887
|
-
attributes: args.attributes,
|
|
1888
|
-
filters: args.filters,
|
|
1889
|
-
dateRange: args.fromDate || args.toDate ? {
|
|
1890
|
-
start: args.fromDate,
|
|
1891
|
-
end: args.toDate
|
|
1892
|
-
} : void 0
|
|
1893
|
-
};
|
|
1894
|
-
try {
|
|
1895
|
-
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1896
|
-
return result.traces;
|
|
1897
|
-
} catch (error$1) {
|
|
1898
|
-
throw new error.MastraError(
|
|
1899
|
-
{
|
|
1900
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1901
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1902
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1903
|
-
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1904
|
-
details: {
|
|
1905
|
-
name: args.name ?? "",
|
|
1906
|
-
scope: args.scope ?? ""
|
|
1907
|
-
}
|
|
1908
|
-
},
|
|
1909
|
-
error$1
|
|
1910
|
-
);
|
|
1911
|
-
}
|
|
1912
|
-
}
|
|
1913
|
-
async getTracesPaginated(args) {
|
|
1914
|
-
const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
|
|
1915
|
-
const fromDate = dateRange?.start;
|
|
1916
|
-
const toDate = dateRange?.end;
|
|
1917
|
-
const fullTableName = this.operations.getTableName(storage.TABLE_TRACES);
|
|
1918
|
-
try {
|
|
1919
|
-
const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
|
|
1920
|
-
const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
|
|
1921
|
-
if (name) {
|
|
1922
|
-
dataQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1923
|
-
countQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1924
|
-
}
|
|
1925
|
-
if (scope) {
|
|
1926
|
-
dataQuery.andWhere("scope = ?", scope);
|
|
1927
|
-
countQuery.andWhere("scope = ?", scope);
|
|
1928
|
-
}
|
|
1929
|
-
if (attributes && Object.keys(attributes).length > 0) {
|
|
1930
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
1931
|
-
dataQuery.jsonLike("attributes", key, value);
|
|
1932
|
-
countQuery.jsonLike("attributes", key, value);
|
|
1933
|
-
}
|
|
1934
|
-
}
|
|
1935
|
-
if (fromDate) {
|
|
1936
|
-
const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
|
|
1937
|
-
dataQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1938
|
-
countQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1939
|
-
}
|
|
1940
|
-
if (toDate) {
|
|
1941
|
-
const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
|
|
1942
|
-
dataQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1943
|
-
countQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1944
|
-
}
|
|
1945
|
-
const allDataResult = await this.operations.executeQuery(
|
|
1946
|
-
createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
|
|
1947
|
-
);
|
|
1948
|
-
console.info("allDataResult", allDataResult);
|
|
1949
|
-
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1950
|
-
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1951
|
-
dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
|
|
1952
|
-
const results = await this.operations.executeQuery(dataQuery.build());
|
|
1953
|
-
const traces = isArrayOfRecords2(results) ? results.map(
|
|
1954
|
-
(trace) => ({
|
|
1955
|
-
...trace,
|
|
1956
|
-
attributes: deserializeValue(trace.attributes, "jsonb"),
|
|
1957
|
-
status: deserializeValue(trace.status, "jsonb"),
|
|
1958
|
-
events: deserializeValue(trace.events, "jsonb"),
|
|
1959
|
-
links: deserializeValue(trace.links, "jsonb"),
|
|
1960
|
-
other: deserializeValue(trace.other, "jsonb")
|
|
1961
|
-
})
|
|
1962
|
-
) : [];
|
|
1963
|
-
return {
|
|
1964
|
-
traces,
|
|
1965
|
-
total,
|
|
1966
|
-
page,
|
|
1967
|
-
perPage,
|
|
1968
|
-
hasMore: page * perPage + traces.length < total
|
|
1969
|
-
};
|
|
1970
|
-
} catch (error$1) {
|
|
1971
|
-
const mastraError = new error.MastraError(
|
|
1972
|
-
{
|
|
1973
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
|
|
1974
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1975
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1976
|
-
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1977
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1978
|
-
},
|
|
1979
|
-
error$1
|
|
1980
|
-
);
|
|
1981
|
-
this.logger?.error(mastraError.toString());
|
|
1982
|
-
this.logger?.trackException(mastraError);
|
|
1983
|
-
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1986
|
-
async batchTraceInsert({ records }) {
|
|
1987
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1988
|
-
await this.operations.batchInsert({
|
|
1989
|
-
tableName: storage.TABLE_TRACES,
|
|
1990
|
-
records
|
|
1991
|
-
});
|
|
1992
|
-
}
|
|
1993
|
-
};
|
|
1994
1686
|
var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
1995
1687
|
operations;
|
|
1996
1688
|
constructor({ operations }) {
|
|
@@ -2002,7 +1694,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
2002
1694
|
// runId,
|
|
2003
1695
|
// stepId,
|
|
2004
1696
|
// result,
|
|
2005
|
-
//
|
|
1697
|
+
// requestContext,
|
|
2006
1698
|
}) {
|
|
2007
1699
|
throw new Error("Method not implemented.");
|
|
2008
1700
|
}
|
|
@@ -2106,19 +1798,24 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
2106
1798
|
resourceId: row.resourceId
|
|
2107
1799
|
};
|
|
2108
1800
|
}
|
|
2109
|
-
async
|
|
1801
|
+
async listWorkflowRuns({
|
|
2110
1802
|
workflowName,
|
|
2111
1803
|
fromDate,
|
|
2112
1804
|
toDate,
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
resourceId
|
|
1805
|
+
page,
|
|
1806
|
+
perPage,
|
|
1807
|
+
resourceId,
|
|
1808
|
+
status
|
|
2116
1809
|
} = {}) {
|
|
2117
1810
|
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2118
1811
|
try {
|
|
2119
1812
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
2120
1813
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
2121
1814
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
1815
|
+
if (status) {
|
|
1816
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1817
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1818
|
+
}
|
|
2122
1819
|
if (resourceId) {
|
|
2123
1820
|
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
2124
1821
|
if (hasResourceId) {
|
|
@@ -2137,11 +1834,14 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
2137
1834
|
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2138
1835
|
}
|
|
2139
1836
|
builder.orderBy("createdAt", "DESC");
|
|
2140
|
-
if (typeof
|
|
2141
|
-
|
|
1837
|
+
if (typeof perPage === "number" && typeof page === "number") {
|
|
1838
|
+
const offset = page * perPage;
|
|
1839
|
+
builder.limit(perPage);
|
|
1840
|
+
builder.offset(offset);
|
|
1841
|
+
}
|
|
2142
1842
|
const { sql, params } = builder.build();
|
|
2143
1843
|
let total = 0;
|
|
2144
|
-
if (
|
|
1844
|
+
if (perPage !== void 0 && page !== void 0) {
|
|
2145
1845
|
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
2146
1846
|
const countResult = await this.operations.executeQuery({
|
|
2147
1847
|
sql: countSql,
|
|
@@ -2156,7 +1856,7 @@ var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
|
2156
1856
|
} catch (error$1) {
|
|
2157
1857
|
throw new error.MastraError(
|
|
2158
1858
|
{
|
|
2159
|
-
id: "
|
|
1859
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_WORKFLOW_RUNS_ERROR",
|
|
2160
1860
|
domain: error.ErrorDomain.STORAGE,
|
|
2161
1861
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
2162
1862
|
text: `Failed to retrieve workflow runs: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
@@ -2218,7 +1918,7 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2218
1918
|
*/
|
|
2219
1919
|
constructor(config) {
|
|
2220
1920
|
try {
|
|
2221
|
-
super({ name: "D1" });
|
|
1921
|
+
super({ id: config.id, name: "D1" });
|
|
2222
1922
|
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
2223
1923
|
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
2224
1924
|
}
|
|
@@ -2272,12 +1972,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2272
1972
|
const scores = new ScoresStorageD1({
|
|
2273
1973
|
operations
|
|
2274
1974
|
});
|
|
2275
|
-
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2276
|
-
operations
|
|
2277
|
-
});
|
|
2278
|
-
const traces = new TracesStorageD1({
|
|
2279
|
-
operations
|
|
2280
|
-
});
|
|
2281
1975
|
const workflows = new WorkflowsStorageD1({
|
|
2282
1976
|
operations
|
|
2283
1977
|
});
|
|
@@ -2287,8 +1981,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2287
1981
|
this.stores = {
|
|
2288
1982
|
operations,
|
|
2289
1983
|
scores,
|
|
2290
|
-
legacyEvals,
|
|
2291
|
-
traces,
|
|
2292
1984
|
workflows,
|
|
2293
1985
|
memory
|
|
2294
1986
|
};
|
|
@@ -2300,7 +1992,7 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2300
1992
|
hasColumn: true,
|
|
2301
1993
|
createTable: true,
|
|
2302
1994
|
deleteMessages: false,
|
|
2303
|
-
|
|
1995
|
+
listScoresBySpan: true
|
|
2304
1996
|
};
|
|
2305
1997
|
}
|
|
2306
1998
|
async createTable({
|
|
@@ -2340,15 +2032,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2340
2032
|
async getThreadById({ threadId }) {
|
|
2341
2033
|
return this.stores.memory.getThreadById({ threadId });
|
|
2342
2034
|
}
|
|
2343
|
-
/**
|
|
2344
|
-
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
2345
|
-
*/
|
|
2346
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2347
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2348
|
-
}
|
|
2349
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2350
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2351
|
-
}
|
|
2352
2035
|
async saveThread({ thread }) {
|
|
2353
2036
|
return this.stores.memory.saveThread({ thread });
|
|
2354
2037
|
}
|
|
@@ -2365,34 +2048,14 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2365
2048
|
async saveMessages(args) {
|
|
2366
2049
|
return this.stores.memory.saveMessages(args);
|
|
2367
2050
|
}
|
|
2368
|
-
async getMessages({
|
|
2369
|
-
threadId,
|
|
2370
|
-
selectBy,
|
|
2371
|
-
format
|
|
2372
|
-
}) {
|
|
2373
|
-
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2374
|
-
}
|
|
2375
|
-
async getMessagesById({
|
|
2376
|
-
messageIds,
|
|
2377
|
-
format
|
|
2378
|
-
}) {
|
|
2379
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2380
|
-
}
|
|
2381
|
-
async getMessagesPaginated({
|
|
2382
|
-
threadId,
|
|
2383
|
-
selectBy,
|
|
2384
|
-
format
|
|
2385
|
-
}) {
|
|
2386
|
-
return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
|
|
2387
|
-
}
|
|
2388
2051
|
async updateWorkflowResults({
|
|
2389
2052
|
workflowName,
|
|
2390
2053
|
runId,
|
|
2391
2054
|
stepId,
|
|
2392
2055
|
result,
|
|
2393
|
-
|
|
2056
|
+
requestContext
|
|
2394
2057
|
}) {
|
|
2395
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2058
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2396
2059
|
}
|
|
2397
2060
|
async updateWorkflowState({
|
|
2398
2061
|
workflowName,
|
|
@@ -2412,15 +2075,8 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2412
2075
|
async loadWorkflowSnapshot(params) {
|
|
2413
2076
|
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2414
2077
|
}
|
|
2415
|
-
async
|
|
2416
|
-
|
|
2417
|
-
fromDate,
|
|
2418
|
-
toDate,
|
|
2419
|
-
limit,
|
|
2420
|
-
offset,
|
|
2421
|
-
resourceId
|
|
2422
|
-
} = {}) {
|
|
2423
|
-
return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2078
|
+
async listWorkflowRuns(args = {}) {
|
|
2079
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2424
2080
|
}
|
|
2425
2081
|
async getWorkflowRunById({
|
|
2426
2082
|
runId,
|
|
@@ -2436,24 +2092,6 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2436
2092
|
async batchInsert({ tableName, records }) {
|
|
2437
2093
|
return this.stores.operations.batchInsert({ tableName, records });
|
|
2438
2094
|
}
|
|
2439
|
-
/**
|
|
2440
|
-
* @deprecated use getTracesPaginated instead
|
|
2441
|
-
*/
|
|
2442
|
-
async getTraces(args) {
|
|
2443
|
-
return this.stores.traces.getTraces(args);
|
|
2444
|
-
}
|
|
2445
|
-
async getTracesPaginated(args) {
|
|
2446
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2447
|
-
}
|
|
2448
|
-
/**
|
|
2449
|
-
* @deprecated use getEvals instead
|
|
2450
|
-
*/
|
|
2451
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2452
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2453
|
-
}
|
|
2454
|
-
async getEvals(options) {
|
|
2455
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2456
|
-
}
|
|
2457
2095
|
async updateMessages(_args) {
|
|
2458
2096
|
return this.stores.memory.updateMessages(_args);
|
|
2459
2097
|
}
|
|
@@ -2476,38 +2114,38 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
2476
2114
|
async saveScore(_score) {
|
|
2477
2115
|
return this.stores.scores.saveScore(_score);
|
|
2478
2116
|
}
|
|
2479
|
-
async
|
|
2117
|
+
async listScoresByRunId({
|
|
2480
2118
|
runId: _runId,
|
|
2481
2119
|
pagination: _pagination
|
|
2482
2120
|
}) {
|
|
2483
|
-
return this.stores.scores.
|
|
2121
|
+
return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2484
2122
|
}
|
|
2485
|
-
async
|
|
2123
|
+
async listScoresByEntityId({
|
|
2486
2124
|
entityId: _entityId,
|
|
2487
2125
|
entityType: _entityType,
|
|
2488
2126
|
pagination: _pagination
|
|
2489
2127
|
}) {
|
|
2490
|
-
return this.stores.scores.
|
|
2128
|
+
return this.stores.scores.listScoresByEntityId({
|
|
2491
2129
|
entityId: _entityId,
|
|
2492
2130
|
entityType: _entityType,
|
|
2493
2131
|
pagination: _pagination
|
|
2494
2132
|
});
|
|
2495
2133
|
}
|
|
2496
|
-
async
|
|
2134
|
+
async listScoresByScorerId({
|
|
2497
2135
|
scorerId,
|
|
2498
2136
|
pagination,
|
|
2499
2137
|
entityId,
|
|
2500
2138
|
entityType,
|
|
2501
2139
|
source
|
|
2502
2140
|
}) {
|
|
2503
|
-
return this.stores.scores.
|
|
2141
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2504
2142
|
}
|
|
2505
|
-
async
|
|
2143
|
+
async listScoresBySpan({
|
|
2506
2144
|
traceId,
|
|
2507
2145
|
spanId,
|
|
2508
2146
|
pagination
|
|
2509
2147
|
}) {
|
|
2510
|
-
return this.stores.scores.
|
|
2148
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2511
2149
|
}
|
|
2512
2150
|
/**
|
|
2513
2151
|
* Close the database connection
|