@mastra/cloudflare-d1 0.0.0-toolOptionTypes-20250917085558 → 0.0.0-trace-timeline-update-20251121092347
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 +535 -3
- package/README.md +10 -5
- package/dist/index.cjs +275 -569
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +276 -570
- 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 +12 -4
- 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 +30 -86
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +16 -11
- 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.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
|
-
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS,
|
|
2
|
+
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, serializeDate, safelyParseJSON } from '@mastra/core/storage';
|
|
3
3
|
import Cloudflare from 'cloudflare';
|
|
4
|
-
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
5
4
|
import { MessageList } from '@mastra/core/agent';
|
|
5
|
+
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
6
|
+
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
6
7
|
|
|
7
8
|
// src/storage/index.ts
|
|
8
9
|
var SqlBuilder = class {
|
|
@@ -241,16 +242,6 @@ function isArrayOfRecords(value) {
|
|
|
241
242
|
}
|
|
242
243
|
function deserializeValue(value, type) {
|
|
243
244
|
if (value === null || value === void 0) return null;
|
|
244
|
-
if (type === "date" && typeof value === "string") {
|
|
245
|
-
return new Date(value);
|
|
246
|
-
}
|
|
247
|
-
if (type === "jsonb" && typeof value === "string") {
|
|
248
|
-
try {
|
|
249
|
-
return JSON.parse(value);
|
|
250
|
-
} catch {
|
|
251
|
-
return value;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
245
|
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
255
246
|
try {
|
|
256
247
|
return JSON.parse(value);
|
|
@@ -261,155 +252,7 @@ function deserializeValue(value, type) {
|
|
|
261
252
|
return value;
|
|
262
253
|
}
|
|
263
254
|
|
|
264
|
-
// src/storage/domains/
|
|
265
|
-
var LegacyEvalsStorageD1 = class extends LegacyEvalsStorage {
|
|
266
|
-
operations;
|
|
267
|
-
constructor({ operations }) {
|
|
268
|
-
super();
|
|
269
|
-
this.operations = operations;
|
|
270
|
-
}
|
|
271
|
-
async getEvals(options) {
|
|
272
|
-
const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
|
|
273
|
-
const fullTableName = this.operations.getTableName(TABLE_EVALS);
|
|
274
|
-
const conditions = [];
|
|
275
|
-
const queryParams = [];
|
|
276
|
-
if (agentName) {
|
|
277
|
-
conditions.push(`agent_name = ?`);
|
|
278
|
-
queryParams.push(agentName);
|
|
279
|
-
}
|
|
280
|
-
if (type === "test") {
|
|
281
|
-
conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
|
|
282
|
-
} else if (type === "live") {
|
|
283
|
-
conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
|
|
284
|
-
}
|
|
285
|
-
if (dateRange?.start) {
|
|
286
|
-
conditions.push(`created_at >= ?`);
|
|
287
|
-
queryParams.push(serializeDate(dateRange.start));
|
|
288
|
-
}
|
|
289
|
-
if (dateRange?.end) {
|
|
290
|
-
conditions.push(`created_at <= ?`);
|
|
291
|
-
queryParams.push(serializeDate(dateRange.end));
|
|
292
|
-
}
|
|
293
|
-
const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
|
|
294
|
-
if (conditions.length > 0) {
|
|
295
|
-
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
296
|
-
}
|
|
297
|
-
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
298
|
-
try {
|
|
299
|
-
const countResult = await this.operations.executeQuery({
|
|
300
|
-
sql: countSql,
|
|
301
|
-
params: countParams,
|
|
302
|
-
first: true
|
|
303
|
-
});
|
|
304
|
-
const total = Number(countResult?.count || 0);
|
|
305
|
-
const currentOffset = page * perPage;
|
|
306
|
-
if (total === 0) {
|
|
307
|
-
return {
|
|
308
|
-
evals: [],
|
|
309
|
-
total: 0,
|
|
310
|
-
page,
|
|
311
|
-
perPage,
|
|
312
|
-
hasMore: false
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
316
|
-
if (conditions.length > 0) {
|
|
317
|
-
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
318
|
-
}
|
|
319
|
-
dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
|
|
320
|
-
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
321
|
-
const rows = await this.operations.executeQuery({
|
|
322
|
-
sql: dataSql,
|
|
323
|
-
params: dataParams
|
|
324
|
-
});
|
|
325
|
-
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
326
|
-
const result = deserializeValue(row.result);
|
|
327
|
-
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
328
|
-
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
329
|
-
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
330
|
-
}
|
|
331
|
-
return {
|
|
332
|
-
input: row.input,
|
|
333
|
-
output: row.output,
|
|
334
|
-
result,
|
|
335
|
-
agentName: row.agent_name,
|
|
336
|
-
metricName: row.metric_name,
|
|
337
|
-
instructions: row.instructions,
|
|
338
|
-
testInfo,
|
|
339
|
-
globalRunId: row.global_run_id,
|
|
340
|
-
runId: row.run_id,
|
|
341
|
-
createdAt: row.created_at
|
|
342
|
-
};
|
|
343
|
-
});
|
|
344
|
-
const hasMore = currentOffset + evals.length < total;
|
|
345
|
-
return {
|
|
346
|
-
evals,
|
|
347
|
-
total,
|
|
348
|
-
page,
|
|
349
|
-
perPage,
|
|
350
|
-
hasMore
|
|
351
|
-
};
|
|
352
|
-
} catch (error) {
|
|
353
|
-
throw new MastraError(
|
|
354
|
-
{
|
|
355
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
356
|
-
domain: ErrorDomain.STORAGE,
|
|
357
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
358
|
-
text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
359
|
-
details: { agentName: agentName ?? "", type: type ?? "" }
|
|
360
|
-
},
|
|
361
|
-
error
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* @deprecated use getEvals instead
|
|
367
|
-
*/
|
|
368
|
-
async getEvalsByAgentName(agentName, type) {
|
|
369
|
-
const fullTableName = this.operations.getTableName(TABLE_EVALS);
|
|
370
|
-
try {
|
|
371
|
-
let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
|
|
372
|
-
if (type === "test") {
|
|
373
|
-
query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
|
|
374
|
-
} else if (type === "live") {
|
|
375
|
-
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
376
|
-
}
|
|
377
|
-
query.orderBy("created_at", "DESC");
|
|
378
|
-
const { sql, params } = query.build();
|
|
379
|
-
const results = await this.operations.executeQuery({ sql, params });
|
|
380
|
-
return isArrayOfRecords(results) ? results.map((row) => {
|
|
381
|
-
const result = deserializeValue(row.result);
|
|
382
|
-
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
383
|
-
return {
|
|
384
|
-
input: row.input || "",
|
|
385
|
-
output: row.output || "",
|
|
386
|
-
result,
|
|
387
|
-
agentName: row.agent_name || "",
|
|
388
|
-
metricName: row.metric_name || "",
|
|
389
|
-
instructions: row.instructions || "",
|
|
390
|
-
runId: row.run_id || "",
|
|
391
|
-
globalRunId: row.global_run_id || "",
|
|
392
|
-
createdAt: row.created_at || "",
|
|
393
|
-
testInfo
|
|
394
|
-
};
|
|
395
|
-
}) : [];
|
|
396
|
-
} catch (error) {
|
|
397
|
-
const mastraError = new MastraError(
|
|
398
|
-
{
|
|
399
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
400
|
-
domain: ErrorDomain.STORAGE,
|
|
401
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
402
|
-
text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
403
|
-
details: { agentName }
|
|
404
|
-
},
|
|
405
|
-
error
|
|
406
|
-
);
|
|
407
|
-
this.logger?.error(mastraError.toString());
|
|
408
|
-
this.logger?.trackException(mastraError);
|
|
409
|
-
return [];
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
};
|
|
255
|
+
// src/storage/domains/memory/index.ts
|
|
413
256
|
var MemoryStorageD1 = class extends MemoryStorage {
|
|
414
257
|
operations;
|
|
415
258
|
constructor({ operations }) {
|
|
@@ -557,39 +400,22 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
557
400
|
return null;
|
|
558
401
|
}
|
|
559
402
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
try {
|
|
566
|
-
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
567
|
-
const { sql, params } = query.build();
|
|
568
|
-
const results = await this.operations.executeQuery({ sql, params });
|
|
569
|
-
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
570
|
-
...thread,
|
|
571
|
-
createdAt: ensureDate(thread.createdAt),
|
|
572
|
-
updatedAt: ensureDate(thread.updatedAt),
|
|
573
|
-
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
574
|
-
}));
|
|
575
|
-
} catch (error) {
|
|
576
|
-
const mastraError = new MastraError(
|
|
403
|
+
async listThreadsByResourceId(args) {
|
|
404
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
405
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
406
|
+
if (page < 0) {
|
|
407
|
+
throw new MastraError(
|
|
577
408
|
{
|
|
578
|
-
id: "
|
|
409
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
579
410
|
domain: ErrorDomain.STORAGE,
|
|
580
|
-
category: ErrorCategory.
|
|
581
|
-
|
|
582
|
-
details: { resourceId }
|
|
411
|
+
category: ErrorCategory.USER,
|
|
412
|
+
details: { page }
|
|
583
413
|
},
|
|
584
|
-
|
|
414
|
+
new Error("page must be >= 0")
|
|
585
415
|
);
|
|
586
|
-
this.logger?.error(mastraError.toString());
|
|
587
|
-
this.logger?.trackException(mastraError);
|
|
588
|
-
return [];
|
|
589
416
|
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
const { resourceId, page, perPage } = args;
|
|
417
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
418
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
593
419
|
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
594
420
|
const mapRowToStorageThreadType = (row) => ({
|
|
595
421
|
...row,
|
|
@@ -601,20 +427,21 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
601
427
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
602
428
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
603
429
|
const total = Number(countResult?.[0]?.count ?? 0);
|
|
604
|
-
const
|
|
430
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
431
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy(field, direction).limit(limitValue).offset(offset);
|
|
605
432
|
const results = await this.operations.executeQuery(selectQuery.build());
|
|
606
433
|
const threads = results.map(mapRowToStorageThreadType);
|
|
607
434
|
return {
|
|
608
435
|
threads,
|
|
609
436
|
total,
|
|
610
437
|
page,
|
|
611
|
-
perPage,
|
|
612
|
-
hasMore:
|
|
438
|
+
perPage: perPageForResponse,
|
|
439
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
613
440
|
};
|
|
614
441
|
} catch (error) {
|
|
615
442
|
const mastraError = new MastraError(
|
|
616
443
|
{
|
|
617
|
-
id: "
|
|
444
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_THREADS_BY_RESOURCE_ID_ERROR",
|
|
618
445
|
domain: ErrorDomain.STORAGE,
|
|
619
446
|
category: ErrorCategory.THIRD_PARTY,
|
|
620
447
|
text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -628,7 +455,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
628
455
|
threads: [],
|
|
629
456
|
total: 0,
|
|
630
457
|
page,
|
|
631
|
-
perPage,
|
|
458
|
+
perPage: perPageForResponse,
|
|
632
459
|
hasMore: false
|
|
633
460
|
};
|
|
634
461
|
}
|
|
@@ -738,8 +565,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
738
565
|
}
|
|
739
566
|
}
|
|
740
567
|
async saveMessages(args) {
|
|
741
|
-
const { messages
|
|
742
|
-
if (messages.length === 0) return [];
|
|
568
|
+
const { messages } = args;
|
|
569
|
+
if (messages.length === 0) return { messages: [] };
|
|
743
570
|
try {
|
|
744
571
|
const now = /* @__PURE__ */ new Date();
|
|
745
572
|
const threadId = messages[0]?.threadId;
|
|
@@ -787,8 +614,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
787
614
|
]);
|
|
788
615
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
789
616
|
const list = new MessageList().add(messages, "memory");
|
|
790
|
-
|
|
791
|
-
return list.get.all.v1();
|
|
617
|
+
return { messages: list.get.all.db() };
|
|
792
618
|
} catch (error) {
|
|
793
619
|
throw new MastraError(
|
|
794
620
|
{
|
|
@@ -801,9 +627,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
801
627
|
);
|
|
802
628
|
}
|
|
803
629
|
}
|
|
804
|
-
async _getIncludedMessages(threadId,
|
|
630
|
+
async _getIncludedMessages(threadId, include) {
|
|
805
631
|
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
806
|
-
const include = selectBy?.include;
|
|
807
632
|
if (!include) return null;
|
|
808
633
|
const unionQueries = [];
|
|
809
634
|
const params = [];
|
|
@@ -859,74 +684,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
859
684
|
});
|
|
860
685
|
return processedMessages;
|
|
861
686
|
}
|
|
862
|
-
async
|
|
863
|
-
|
|
864
|
-
resourceId,
|
|
865
|
-
selectBy,
|
|
866
|
-
format
|
|
867
|
-
}) {
|
|
868
|
-
try {
|
|
869
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
870
|
-
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
871
|
-
const limit = resolveMessageLimit({
|
|
872
|
-
last: selectBy?.last,
|
|
873
|
-
defaultLimit: 40
|
|
874
|
-
});
|
|
875
|
-
const include = selectBy?.include || [];
|
|
876
|
-
const messages = [];
|
|
877
|
-
if (include.length) {
|
|
878
|
-
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
879
|
-
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
880
|
-
}
|
|
881
|
-
const excludeIds = messages.map((m) => m.id);
|
|
882
|
-
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
883
|
-
if (excludeIds.length > 0) {
|
|
884
|
-
query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
|
|
885
|
-
}
|
|
886
|
-
query.orderBy("createdAt", "DESC").limit(limit);
|
|
887
|
-
const { sql, params } = query.build();
|
|
888
|
-
const result = await this.operations.executeQuery({ sql, params });
|
|
889
|
-
if (Array.isArray(result)) messages.push(...result);
|
|
890
|
-
messages.sort((a, b) => {
|
|
891
|
-
const aRecord = a;
|
|
892
|
-
const bRecord = b;
|
|
893
|
-
const timeA = new Date(aRecord.createdAt).getTime();
|
|
894
|
-
const timeB = new Date(bRecord.createdAt).getTime();
|
|
895
|
-
return timeA - timeB;
|
|
896
|
-
});
|
|
897
|
-
const processedMessages = messages.map((message) => {
|
|
898
|
-
const processedMsg = {};
|
|
899
|
-
for (const [key, value] of Object.entries(message)) {
|
|
900
|
-
if (key === `type` && value === `v2`) continue;
|
|
901
|
-
processedMsg[key] = deserializeValue(value);
|
|
902
|
-
}
|
|
903
|
-
return processedMsg;
|
|
904
|
-
});
|
|
905
|
-
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
906
|
-
const list = new MessageList().add(processedMessages, "memory");
|
|
907
|
-
if (format === `v2`) return list.get.all.v2();
|
|
908
|
-
return list.get.all.v1();
|
|
909
|
-
} catch (error) {
|
|
910
|
-
const mastraError = new MastraError(
|
|
911
|
-
{
|
|
912
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
|
|
913
|
-
domain: ErrorDomain.STORAGE,
|
|
914
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
915
|
-
text: `Failed to retrieve messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
916
|
-
details: { threadId, resourceId: resourceId ?? "" }
|
|
917
|
-
},
|
|
918
|
-
error
|
|
919
|
-
);
|
|
920
|
-
this.logger?.error(mastraError.toString());
|
|
921
|
-
this.logger?.trackException(mastraError);
|
|
922
|
-
throw mastraError;
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
async getMessagesById({
|
|
926
|
-
messageIds,
|
|
927
|
-
format
|
|
928
|
-
}) {
|
|
929
|
-
if (messageIds.length === 0) return [];
|
|
687
|
+
async listMessagesById({ messageIds }) {
|
|
688
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
930
689
|
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
931
690
|
const messages = [];
|
|
932
691
|
try {
|
|
@@ -945,12 +704,11 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
945
704
|
});
|
|
946
705
|
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
947
706
|
const list = new MessageList().add(processedMessages, "memory");
|
|
948
|
-
|
|
949
|
-
return list.get.all.v2();
|
|
707
|
+
return { messages: list.get.all.db() };
|
|
950
708
|
} catch (error) {
|
|
951
709
|
const mastraError = new MastraError(
|
|
952
710
|
{
|
|
953
|
-
id: "
|
|
711
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_BY_ID_ERROR",
|
|
954
712
|
domain: ErrorDomain.STORAGE,
|
|
955
713
|
category: ErrorCategory.THIRD_PARTY,
|
|
956
714
|
text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -963,118 +721,157 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
963
721
|
throw mastraError;
|
|
964
722
|
}
|
|
965
723
|
}
|
|
966
|
-
async
|
|
967
|
-
threadId,
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
724
|
+
async listMessages(args) {
|
|
725
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
726
|
+
if (!threadId.trim()) {
|
|
727
|
+
throw new MastraError(
|
|
728
|
+
{
|
|
729
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
730
|
+
domain: ErrorDomain.STORAGE,
|
|
731
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
732
|
+
details: { threadId }
|
|
733
|
+
},
|
|
734
|
+
new Error("threadId must be a non-empty string")
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
if (page < 0) {
|
|
738
|
+
throw new MastraError(
|
|
739
|
+
{
|
|
740
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_PAGE",
|
|
741
|
+
domain: ErrorDomain.STORAGE,
|
|
742
|
+
category: ErrorCategory.USER,
|
|
743
|
+
details: { page }
|
|
744
|
+
},
|
|
745
|
+
new Error("page must be >= 0")
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
749
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
977
750
|
try {
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
751
|
+
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
752
|
+
let query = `
|
|
753
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
754
|
+
FROM ${fullTableName}
|
|
755
|
+
WHERE thread_id = ?
|
|
756
|
+
`;
|
|
757
|
+
const queryParams = [threadId];
|
|
758
|
+
if (resourceId) {
|
|
759
|
+
query += ` AND resourceId = ?`;
|
|
760
|
+
queryParams.push(resourceId);
|
|
982
761
|
}
|
|
983
|
-
const
|
|
984
|
-
if (
|
|
985
|
-
|
|
762
|
+
const dateRange = filter?.dateRange;
|
|
763
|
+
if (dateRange?.start) {
|
|
764
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
765
|
+
query += ` AND createdAt >= ?`;
|
|
766
|
+
queryParams.push(startDate);
|
|
986
767
|
}
|
|
987
|
-
if (
|
|
988
|
-
|
|
768
|
+
if (dateRange?.end) {
|
|
769
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
770
|
+
query += ` AND createdAt <= ?`;
|
|
771
|
+
queryParams.push(endDate);
|
|
989
772
|
}
|
|
990
|
-
const
|
|
773
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
774
|
+
query += ` ORDER BY "${field}" ${direction}`;
|
|
775
|
+
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
776
|
+
query += ` LIMIT ? OFFSET ?`;
|
|
777
|
+
queryParams.push(perPage, offset);
|
|
778
|
+
}
|
|
779
|
+
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
780
|
+
const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
|
|
781
|
+
const processedMsg = {};
|
|
782
|
+
for (const [key, value] of Object.entries(message)) {
|
|
783
|
+
if (key === `type` && value === `v2`) continue;
|
|
784
|
+
processedMsg[key] = deserializeValue(value);
|
|
785
|
+
}
|
|
786
|
+
return processedMsg;
|
|
787
|
+
});
|
|
788
|
+
const paginatedCount = paginatedMessages.length;
|
|
789
|
+
let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
|
|
790
|
+
const countParams = [threadId];
|
|
791
|
+
if (resourceId) {
|
|
792
|
+
countQuery += ` AND resourceId = ?`;
|
|
793
|
+
countParams.push(resourceId);
|
|
794
|
+
}
|
|
795
|
+
if (dateRange?.start) {
|
|
796
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
797
|
+
countQuery += ` AND createdAt >= ?`;
|
|
798
|
+
countParams.push(startDate);
|
|
799
|
+
}
|
|
800
|
+
if (dateRange?.end) {
|
|
801
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
802
|
+
countQuery += ` AND createdAt <= ?`;
|
|
803
|
+
countParams.push(endDate);
|
|
804
|
+
}
|
|
805
|
+
const countResult = await this.operations.executeQuery({ sql: countQuery, params: countParams });
|
|
991
806
|
const total = Number(countResult[0]?.count ?? 0);
|
|
992
|
-
if (total === 0 &&
|
|
807
|
+
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
|
|
993
808
|
return {
|
|
994
809
|
messages: [],
|
|
995
810
|
total: 0,
|
|
996
811
|
page,
|
|
997
|
-
perPage,
|
|
812
|
+
perPage: perPageForResponse,
|
|
998
813
|
hasMore: false
|
|
999
814
|
};
|
|
1000
815
|
}
|
|
1001
|
-
const
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
1015
|
-
query = `
|
|
1016
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1017
|
-
FROM ${fullTableName}
|
|
1018
|
-
WHERE thread_id = ?
|
|
1019
|
-
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1020
|
-
${toDate ? "AND createdAt <= ?" : ""}
|
|
1021
|
-
${excludeCondition}
|
|
1022
|
-
ORDER BY createdAt DESC
|
|
1023
|
-
LIMIT ?
|
|
1024
|
-
`;
|
|
1025
|
-
queryParams.push(selectBy.last);
|
|
1026
|
-
} else {
|
|
1027
|
-
query = `
|
|
1028
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1029
|
-
FROM ${fullTableName}
|
|
1030
|
-
WHERE thread_id = ?
|
|
1031
|
-
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1032
|
-
${toDate ? "AND createdAt <= ?" : ""}
|
|
1033
|
-
${excludeCondition}
|
|
1034
|
-
ORDER BY createdAt DESC
|
|
1035
|
-
LIMIT ? OFFSET ?
|
|
1036
|
-
`;
|
|
1037
|
-
queryParams.push(perPage, page * perPage);
|
|
816
|
+
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
817
|
+
let includeMessages = [];
|
|
818
|
+
if (include && include.length > 0) {
|
|
819
|
+
const includeResult = await this._getIncludedMessages(threadId, include);
|
|
820
|
+
if (Array.isArray(includeResult)) {
|
|
821
|
+
includeMessages = includeResult;
|
|
822
|
+
for (const includeMsg of includeMessages) {
|
|
823
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
824
|
+
paginatedMessages.push(includeMsg);
|
|
825
|
+
messageIds.add(includeMsg.id);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
1038
829
|
}
|
|
1039
|
-
const
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
830
|
+
const list = new MessageList().add(paginatedMessages, "memory");
|
|
831
|
+
let finalMessages = list.get.all.db();
|
|
832
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
833
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
834
|
+
const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
835
|
+
const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
836
|
+
if (aValue === bValue) {
|
|
837
|
+
return a.id.localeCompare(b.id);
|
|
1045
838
|
}
|
|
1046
|
-
|
|
839
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
840
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
841
|
+
}
|
|
842
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
1047
843
|
});
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
const list = new MessageList().add(processedMessages, "memory");
|
|
1052
|
-
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
844
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
845
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
846
|
+
const hasMore = perPageInput === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
|
|
1053
847
|
return {
|
|
1054
|
-
messages,
|
|
848
|
+
messages: finalMessages,
|
|
1055
849
|
total,
|
|
1056
850
|
page,
|
|
1057
|
-
perPage,
|
|
1058
|
-
hasMore
|
|
851
|
+
perPage: perPageForResponse,
|
|
852
|
+
hasMore
|
|
1059
853
|
};
|
|
1060
854
|
} catch (error) {
|
|
1061
855
|
const mastraError = new MastraError(
|
|
1062
856
|
{
|
|
1063
|
-
id: "
|
|
857
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
|
|
1064
858
|
domain: ErrorDomain.STORAGE,
|
|
1065
859
|
category: ErrorCategory.THIRD_PARTY,
|
|
1066
|
-
text: `Failed to
|
|
1067
|
-
details: {
|
|
860
|
+
text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
861
|
+
details: {
|
|
862
|
+
threadId,
|
|
863
|
+
resourceId: resourceId ?? ""
|
|
864
|
+
}
|
|
1068
865
|
},
|
|
1069
866
|
error
|
|
1070
867
|
);
|
|
1071
|
-
this.logger?.error(mastraError.toString());
|
|
1072
|
-
this.logger?.trackException(mastraError);
|
|
868
|
+
this.logger?.error?.(mastraError.toString());
|
|
869
|
+
this.logger?.trackException?.(mastraError);
|
|
1073
870
|
return {
|
|
1074
871
|
messages: [],
|
|
1075
872
|
total: 0,
|
|
1076
873
|
page,
|
|
1077
|
-
perPage,
|
|
874
|
+
perPage: perPageForResponse,
|
|
1078
875
|
hasMore: false
|
|
1079
876
|
};
|
|
1080
877
|
}
|
|
@@ -1575,7 +1372,7 @@ function transformScoreRow(row) {
|
|
|
1575
1372
|
deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
|
|
1576
1373
|
deserialized.metadata = safelyParseJSON(row.metadata);
|
|
1577
1374
|
deserialized.additionalContext = safelyParseJSON(row.additionalContext);
|
|
1578
|
-
deserialized.
|
|
1375
|
+
deserialized.requestContext = safelyParseJSON(row.requestContext);
|
|
1579
1376
|
deserialized.entity = safelyParseJSON(row.entity);
|
|
1580
1377
|
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1581
1378
|
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
@@ -1609,12 +1406,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1609
1406
|
}
|
|
1610
1407
|
}
|
|
1611
1408
|
async saveScore(score) {
|
|
1409
|
+
let parsedScore;
|
|
1410
|
+
try {
|
|
1411
|
+
parsedScore = saveScorePayloadSchema.parse(score);
|
|
1412
|
+
} catch (error) {
|
|
1413
|
+
throw new MastraError(
|
|
1414
|
+
{
|
|
1415
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
|
|
1416
|
+
domain: ErrorDomain.STORAGE,
|
|
1417
|
+
category: ErrorCategory.USER,
|
|
1418
|
+
details: { scoreId: score.id }
|
|
1419
|
+
},
|
|
1420
|
+
error
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1612
1423
|
try {
|
|
1613
1424
|
const id = crypto.randomUUID();
|
|
1614
1425
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1615
|
-
const { input, ...rest } = score;
|
|
1616
1426
|
const serializedRecord = {};
|
|
1617
|
-
for (const [key, value] of Object.entries(
|
|
1427
|
+
for (const [key, value] of Object.entries(parsedScore)) {
|
|
1618
1428
|
if (value !== null && value !== void 0) {
|
|
1619
1429
|
if (typeof value === "object") {
|
|
1620
1430
|
serializedRecord[key] = JSON.stringify(value);
|
|
@@ -1626,7 +1436,6 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1626
1436
|
}
|
|
1627
1437
|
}
|
|
1628
1438
|
serializedRecord.id = id;
|
|
1629
|
-
serializedRecord.input = JSON.stringify(input);
|
|
1630
1439
|
serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1631
1440
|
serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1632
1441
|
const columns = Object.keys(serializedRecord);
|
|
@@ -1647,7 +1456,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1647
1456
|
);
|
|
1648
1457
|
}
|
|
1649
1458
|
}
|
|
1650
|
-
async
|
|
1459
|
+
async listScoresByScorerId({
|
|
1651
1460
|
scorerId,
|
|
1652
1461
|
entityId,
|
|
1653
1462
|
entityType,
|
|
@@ -1655,6 +1464,9 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1655
1464
|
pagination
|
|
1656
1465
|
}) {
|
|
1657
1466
|
try {
|
|
1467
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1468
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1469
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1658
1470
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1659
1471
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1660
1472
|
if (entityId) {
|
|
@@ -1672,13 +1484,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1672
1484
|
return {
|
|
1673
1485
|
pagination: {
|
|
1674
1486
|
total: 0,
|
|
1675
|
-
page
|
|
1676
|
-
perPage:
|
|
1487
|
+
page,
|
|
1488
|
+
perPage: perPageForResponse,
|
|
1677
1489
|
hasMore: false
|
|
1678
1490
|
},
|
|
1679
1491
|
scores: []
|
|
1680
1492
|
};
|
|
1681
1493
|
}
|
|
1494
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1495
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1682
1496
|
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
|
|
1683
1497
|
if (entityId) {
|
|
1684
1498
|
selectQuery.andWhere("entityId = ?", entityId);
|
|
@@ -1689,16 +1503,16 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1689
1503
|
if (source) {
|
|
1690
1504
|
selectQuery.andWhere("source = ?", source);
|
|
1691
1505
|
}
|
|
1692
|
-
selectQuery.limit(
|
|
1506
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1693
1507
|
const { sql, params } = selectQuery.build();
|
|
1694
1508
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1695
1509
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1696
1510
|
return {
|
|
1697
1511
|
pagination: {
|
|
1698
1512
|
total,
|
|
1699
|
-
page
|
|
1700
|
-
perPage:
|
|
1701
|
-
hasMore:
|
|
1513
|
+
page,
|
|
1514
|
+
perPage: perPageForResponse,
|
|
1515
|
+
hasMore: end < total
|
|
1702
1516
|
},
|
|
1703
1517
|
scores
|
|
1704
1518
|
};
|
|
@@ -1713,11 +1527,14 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1713
1527
|
);
|
|
1714
1528
|
}
|
|
1715
1529
|
}
|
|
1716
|
-
async
|
|
1530
|
+
async listScoresByRunId({
|
|
1717
1531
|
runId,
|
|
1718
1532
|
pagination
|
|
1719
1533
|
}) {
|
|
1720
1534
|
try {
|
|
1535
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1536
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1537
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1721
1538
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1722
1539
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1723
1540
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1726,23 +1543,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1726
1543
|
return {
|
|
1727
1544
|
pagination: {
|
|
1728
1545
|
total: 0,
|
|
1729
|
-
page
|
|
1730
|
-
perPage:
|
|
1546
|
+
page,
|
|
1547
|
+
perPage: perPageForResponse,
|
|
1731
1548
|
hasMore: false
|
|
1732
1549
|
},
|
|
1733
1550
|
scores: []
|
|
1734
1551
|
};
|
|
1735
1552
|
}
|
|
1736
|
-
const
|
|
1553
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1554
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1555
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(limitValue).offset(start);
|
|
1737
1556
|
const { sql, params } = selectQuery.build();
|
|
1738
1557
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1739
1558
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1740
1559
|
return {
|
|
1741
1560
|
pagination: {
|
|
1742
1561
|
total,
|
|
1743
|
-
page
|
|
1744
|
-
perPage:
|
|
1745
|
-
hasMore:
|
|
1562
|
+
page,
|
|
1563
|
+
perPage: perPageForResponse,
|
|
1564
|
+
hasMore: end < total
|
|
1746
1565
|
},
|
|
1747
1566
|
scores
|
|
1748
1567
|
};
|
|
@@ -1757,12 +1576,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1757
1576
|
);
|
|
1758
1577
|
}
|
|
1759
1578
|
}
|
|
1760
|
-
async
|
|
1579
|
+
async listScoresByEntityId({
|
|
1761
1580
|
entityId,
|
|
1762
1581
|
entityType,
|
|
1763
1582
|
pagination
|
|
1764
1583
|
}) {
|
|
1765
1584
|
try {
|
|
1585
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1586
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1587
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1766
1588
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1767
1589
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1768
1590
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1771,23 +1593,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1771
1593
|
return {
|
|
1772
1594
|
pagination: {
|
|
1773
1595
|
total: 0,
|
|
1774
|
-
page
|
|
1775
|
-
perPage:
|
|
1596
|
+
page,
|
|
1597
|
+
perPage: perPageForResponse,
|
|
1776
1598
|
hasMore: false
|
|
1777
1599
|
},
|
|
1778
1600
|
scores: []
|
|
1779
1601
|
};
|
|
1780
1602
|
}
|
|
1781
|
-
const
|
|
1603
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1604
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1605
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(limitValue).offset(start);
|
|
1782
1606
|
const { sql, params } = selectQuery.build();
|
|
1783
1607
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1784
1608
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1785
1609
|
return {
|
|
1786
1610
|
pagination: {
|
|
1787
1611
|
total,
|
|
1788
|
-
page
|
|
1789
|
-
perPage:
|
|
1790
|
-
hasMore:
|
|
1612
|
+
page,
|
|
1613
|
+
perPage: perPageForResponse,
|
|
1614
|
+
hasMore: end < total
|
|
1791
1615
|
},
|
|
1792
1616
|
scores
|
|
1793
1617
|
};
|
|
@@ -1802,128 +1626,56 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1802
1626
|
);
|
|
1803
1627
|
}
|
|
1804
1628
|
}
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
operations;
|
|
1811
|
-
constructor({ operations }) {
|
|
1812
|
-
super();
|
|
1813
|
-
this.operations = operations;
|
|
1814
|
-
}
|
|
1815
|
-
async getTraces(args) {
|
|
1816
|
-
const paginatedArgs = {
|
|
1817
|
-
name: args.name,
|
|
1818
|
-
scope: args.scope,
|
|
1819
|
-
page: args.page,
|
|
1820
|
-
perPage: args.perPage,
|
|
1821
|
-
attributes: args.attributes,
|
|
1822
|
-
filters: args.filters,
|
|
1823
|
-
dateRange: args.fromDate || args.toDate ? {
|
|
1824
|
-
start: args.fromDate,
|
|
1825
|
-
end: args.toDate
|
|
1826
|
-
} : void 0
|
|
1827
|
-
};
|
|
1828
|
-
try {
|
|
1829
|
-
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1830
|
-
return result.traces;
|
|
1831
|
-
} catch (error) {
|
|
1832
|
-
throw new MastraError(
|
|
1833
|
-
{
|
|
1834
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1835
|
-
domain: ErrorDomain.STORAGE,
|
|
1836
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1837
|
-
text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
|
|
1838
|
-
details: {
|
|
1839
|
-
name: args.name ?? "",
|
|
1840
|
-
scope: args.scope ?? ""
|
|
1841
|
-
}
|
|
1842
|
-
},
|
|
1843
|
-
error
|
|
1844
|
-
);
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1847
|
-
async getTracesPaginated(args) {
|
|
1848
|
-
const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
|
|
1849
|
-
const fromDate = dateRange?.start;
|
|
1850
|
-
const toDate = dateRange?.end;
|
|
1851
|
-
const fullTableName = this.operations.getTableName(TABLE_TRACES);
|
|
1629
|
+
async listScoresBySpan({
|
|
1630
|
+
traceId,
|
|
1631
|
+
spanId,
|
|
1632
|
+
pagination
|
|
1633
|
+
}) {
|
|
1852
1634
|
try {
|
|
1853
|
-
const
|
|
1854
|
-
const
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
}
|
|
1859
|
-
if (scope) {
|
|
1860
|
-
dataQuery.andWhere("scope = ?", scope);
|
|
1861
|
-
countQuery.andWhere("scope = ?", scope);
|
|
1862
|
-
}
|
|
1863
|
-
if (attributes && Object.keys(attributes).length > 0) {
|
|
1864
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
1865
|
-
dataQuery.jsonLike("attributes", key, value);
|
|
1866
|
-
countQuery.jsonLike("attributes", key, value);
|
|
1867
|
-
}
|
|
1868
|
-
}
|
|
1869
|
-
if (fromDate) {
|
|
1870
|
-
const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
|
|
1871
|
-
dataQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1872
|
-
countQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1873
|
-
}
|
|
1874
|
-
if (toDate) {
|
|
1875
|
-
const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
|
|
1876
|
-
dataQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1877
|
-
countQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1878
|
-
}
|
|
1879
|
-
const allDataResult = await this.operations.executeQuery(
|
|
1880
|
-
createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
|
|
1881
|
-
);
|
|
1882
|
-
console.log("allDataResult", allDataResult);
|
|
1635
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1636
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1637
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1638
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1639
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1883
1640
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1884
|
-
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1641
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1642
|
+
if (total === 0) {
|
|
1643
|
+
return {
|
|
1644
|
+
pagination: {
|
|
1645
|
+
total: 0,
|
|
1646
|
+
page,
|
|
1647
|
+
perPage: perPageForResponse,
|
|
1648
|
+
hasMore: false
|
|
1649
|
+
},
|
|
1650
|
+
scores: []
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1653
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1654
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1655
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limitValue).offset(start);
|
|
1656
|
+
const { sql, params } = selectQuery.build();
|
|
1657
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1658
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1897
1659
|
return {
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1660
|
+
pagination: {
|
|
1661
|
+
total,
|
|
1662
|
+
page,
|
|
1663
|
+
perPage: perPageForResponse,
|
|
1664
|
+
hasMore: end < total
|
|
1665
|
+
},
|
|
1666
|
+
scores
|
|
1903
1667
|
};
|
|
1904
1668
|
} catch (error) {
|
|
1905
|
-
|
|
1669
|
+
throw new MastraError(
|
|
1906
1670
|
{
|
|
1907
|
-
id: "
|
|
1671
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SPAN_FAILED",
|
|
1908
1672
|
domain: ErrorDomain.STORAGE,
|
|
1909
|
-
category: ErrorCategory.THIRD_PARTY
|
|
1910
|
-
text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
|
|
1911
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1673
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1912
1674
|
},
|
|
1913
1675
|
error
|
|
1914
1676
|
);
|
|
1915
|
-
this.logger?.error(mastraError.toString());
|
|
1916
|
-
this.logger?.trackException(mastraError);
|
|
1917
|
-
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1918
1677
|
}
|
|
1919
1678
|
}
|
|
1920
|
-
async batchTraceInsert({ records }) {
|
|
1921
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1922
|
-
await this.operations.batchInsert({
|
|
1923
|
-
tableName: TABLE_TRACES,
|
|
1924
|
-
records
|
|
1925
|
-
});
|
|
1926
|
-
}
|
|
1927
1679
|
};
|
|
1928
1680
|
var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
1929
1681
|
operations;
|
|
@@ -1936,7 +1688,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
1936
1688
|
// runId,
|
|
1937
1689
|
// stepId,
|
|
1938
1690
|
// result,
|
|
1939
|
-
//
|
|
1691
|
+
// requestContext,
|
|
1940
1692
|
}) {
|
|
1941
1693
|
throw new Error("Method not implemented.");
|
|
1942
1694
|
}
|
|
@@ -2040,19 +1792,24 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
2040
1792
|
resourceId: row.resourceId
|
|
2041
1793
|
};
|
|
2042
1794
|
}
|
|
2043
|
-
async
|
|
1795
|
+
async listWorkflowRuns({
|
|
2044
1796
|
workflowName,
|
|
2045
1797
|
fromDate,
|
|
2046
1798
|
toDate,
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
resourceId
|
|
1799
|
+
page,
|
|
1800
|
+
perPage,
|
|
1801
|
+
resourceId,
|
|
1802
|
+
status
|
|
2050
1803
|
} = {}) {
|
|
2051
1804
|
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
2052
1805
|
try {
|
|
2053
1806
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
2054
1807
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
2055
1808
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
1809
|
+
if (status) {
|
|
1810
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1811
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1812
|
+
}
|
|
2056
1813
|
if (resourceId) {
|
|
2057
1814
|
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
2058
1815
|
if (hasResourceId) {
|
|
@@ -2071,11 +1828,14 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
2071
1828
|
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2072
1829
|
}
|
|
2073
1830
|
builder.orderBy("createdAt", "DESC");
|
|
2074
|
-
if (typeof
|
|
2075
|
-
|
|
1831
|
+
if (typeof perPage === "number" && typeof page === "number") {
|
|
1832
|
+
const offset = page * perPage;
|
|
1833
|
+
builder.limit(perPage);
|
|
1834
|
+
builder.offset(offset);
|
|
1835
|
+
}
|
|
2076
1836
|
const { sql, params } = builder.build();
|
|
2077
1837
|
let total = 0;
|
|
2078
|
-
if (
|
|
1838
|
+
if (perPage !== void 0 && page !== void 0) {
|
|
2079
1839
|
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
2080
1840
|
const countResult = await this.operations.executeQuery({
|
|
2081
1841
|
sql: countSql,
|
|
@@ -2090,7 +1850,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
2090
1850
|
} catch (error) {
|
|
2091
1851
|
throw new MastraError(
|
|
2092
1852
|
{
|
|
2093
|
-
id: "
|
|
1853
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_WORKFLOW_RUNS_ERROR",
|
|
2094
1854
|
domain: ErrorDomain.STORAGE,
|
|
2095
1855
|
category: ErrorCategory.THIRD_PARTY,
|
|
2096
1856
|
text: `Failed to retrieve workflow runs: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -2152,7 +1912,7 @@ var D1Store = class extends MastraStorage {
|
|
|
2152
1912
|
*/
|
|
2153
1913
|
constructor(config) {
|
|
2154
1914
|
try {
|
|
2155
|
-
super({ name: "D1" });
|
|
1915
|
+
super({ id: config.id, name: "D1" });
|
|
2156
1916
|
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
2157
1917
|
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
2158
1918
|
}
|
|
@@ -2206,12 +1966,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2206
1966
|
const scores = new ScoresStorageD1({
|
|
2207
1967
|
operations
|
|
2208
1968
|
});
|
|
2209
|
-
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2210
|
-
operations
|
|
2211
|
-
});
|
|
2212
|
-
const traces = new TracesStorageD1({
|
|
2213
|
-
operations
|
|
2214
|
-
});
|
|
2215
1969
|
const workflows = new WorkflowsStorageD1({
|
|
2216
1970
|
operations
|
|
2217
1971
|
});
|
|
@@ -2221,8 +1975,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2221
1975
|
this.stores = {
|
|
2222
1976
|
operations,
|
|
2223
1977
|
scores,
|
|
2224
|
-
legacyEvals,
|
|
2225
|
-
traces,
|
|
2226
1978
|
workflows,
|
|
2227
1979
|
memory
|
|
2228
1980
|
};
|
|
@@ -2233,7 +1985,8 @@ var D1Store = class extends MastraStorage {
|
|
|
2233
1985
|
resourceWorkingMemory: true,
|
|
2234
1986
|
hasColumn: true,
|
|
2235
1987
|
createTable: true,
|
|
2236
|
-
deleteMessages: false
|
|
1988
|
+
deleteMessages: false,
|
|
1989
|
+
listScoresBySpan: true
|
|
2237
1990
|
};
|
|
2238
1991
|
}
|
|
2239
1992
|
async createTable({
|
|
@@ -2273,15 +2026,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2273
2026
|
async getThreadById({ threadId }) {
|
|
2274
2027
|
return this.stores.memory.getThreadById({ threadId });
|
|
2275
2028
|
}
|
|
2276
|
-
/**
|
|
2277
|
-
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
2278
|
-
*/
|
|
2279
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2280
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2281
|
-
}
|
|
2282
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2283
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2284
|
-
}
|
|
2285
2029
|
async saveThread({ thread }) {
|
|
2286
2030
|
return this.stores.memory.saveThread({ thread });
|
|
2287
2031
|
}
|
|
@@ -2298,34 +2042,14 @@ var D1Store = class extends MastraStorage {
|
|
|
2298
2042
|
async saveMessages(args) {
|
|
2299
2043
|
return this.stores.memory.saveMessages(args);
|
|
2300
2044
|
}
|
|
2301
|
-
async getMessages({
|
|
2302
|
-
threadId,
|
|
2303
|
-
selectBy,
|
|
2304
|
-
format
|
|
2305
|
-
}) {
|
|
2306
|
-
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2307
|
-
}
|
|
2308
|
-
async getMessagesById({
|
|
2309
|
-
messageIds,
|
|
2310
|
-
format
|
|
2311
|
-
}) {
|
|
2312
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2313
|
-
}
|
|
2314
|
-
async getMessagesPaginated({
|
|
2315
|
-
threadId,
|
|
2316
|
-
selectBy,
|
|
2317
|
-
format
|
|
2318
|
-
}) {
|
|
2319
|
-
return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
|
|
2320
|
-
}
|
|
2321
2045
|
async updateWorkflowResults({
|
|
2322
2046
|
workflowName,
|
|
2323
2047
|
runId,
|
|
2324
2048
|
stepId,
|
|
2325
2049
|
result,
|
|
2326
|
-
|
|
2050
|
+
requestContext
|
|
2327
2051
|
}) {
|
|
2328
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2052
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2329
2053
|
}
|
|
2330
2054
|
async updateWorkflowState({
|
|
2331
2055
|
workflowName,
|
|
@@ -2345,15 +2069,8 @@ var D1Store = class extends MastraStorage {
|
|
|
2345
2069
|
async loadWorkflowSnapshot(params) {
|
|
2346
2070
|
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2347
2071
|
}
|
|
2348
|
-
async
|
|
2349
|
-
|
|
2350
|
-
fromDate,
|
|
2351
|
-
toDate,
|
|
2352
|
-
limit,
|
|
2353
|
-
offset,
|
|
2354
|
-
resourceId
|
|
2355
|
-
} = {}) {
|
|
2356
|
-
return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2072
|
+
async listWorkflowRuns(args = {}) {
|
|
2073
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2357
2074
|
}
|
|
2358
2075
|
async getWorkflowRunById({
|
|
2359
2076
|
runId,
|
|
@@ -2369,24 +2086,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2369
2086
|
async batchInsert({ tableName, records }) {
|
|
2370
2087
|
return this.stores.operations.batchInsert({ tableName, records });
|
|
2371
2088
|
}
|
|
2372
|
-
/**
|
|
2373
|
-
* @deprecated use getTracesPaginated instead
|
|
2374
|
-
*/
|
|
2375
|
-
async getTraces(args) {
|
|
2376
|
-
return this.stores.traces.getTraces(args);
|
|
2377
|
-
}
|
|
2378
|
-
async getTracesPaginated(args) {
|
|
2379
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2380
|
-
}
|
|
2381
|
-
/**
|
|
2382
|
-
* @deprecated use getEvals instead
|
|
2383
|
-
*/
|
|
2384
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2385
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2386
|
-
}
|
|
2387
|
-
async getEvals(options) {
|
|
2388
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2389
|
-
}
|
|
2390
2089
|
async updateMessages(_args) {
|
|
2391
2090
|
return this.stores.memory.updateMessages(_args);
|
|
2392
2091
|
}
|
|
@@ -2409,31 +2108,38 @@ var D1Store = class extends MastraStorage {
|
|
|
2409
2108
|
async saveScore(_score) {
|
|
2410
2109
|
return this.stores.scores.saveScore(_score);
|
|
2411
2110
|
}
|
|
2412
|
-
async
|
|
2111
|
+
async listScoresByRunId({
|
|
2413
2112
|
runId: _runId,
|
|
2414
2113
|
pagination: _pagination
|
|
2415
2114
|
}) {
|
|
2416
|
-
return this.stores.scores.
|
|
2115
|
+
return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2417
2116
|
}
|
|
2418
|
-
async
|
|
2117
|
+
async listScoresByEntityId({
|
|
2419
2118
|
entityId: _entityId,
|
|
2420
2119
|
entityType: _entityType,
|
|
2421
2120
|
pagination: _pagination
|
|
2422
2121
|
}) {
|
|
2423
|
-
return this.stores.scores.
|
|
2122
|
+
return this.stores.scores.listScoresByEntityId({
|
|
2424
2123
|
entityId: _entityId,
|
|
2425
2124
|
entityType: _entityType,
|
|
2426
2125
|
pagination: _pagination
|
|
2427
2126
|
});
|
|
2428
2127
|
}
|
|
2429
|
-
async
|
|
2128
|
+
async listScoresByScorerId({
|
|
2430
2129
|
scorerId,
|
|
2431
2130
|
pagination,
|
|
2432
2131
|
entityId,
|
|
2433
2132
|
entityType,
|
|
2434
2133
|
source
|
|
2435
2134
|
}) {
|
|
2436
|
-
return this.stores.scores.
|
|
2135
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2136
|
+
}
|
|
2137
|
+
async listScoresBySpan({
|
|
2138
|
+
traceId,
|
|
2139
|
+
spanId,
|
|
2140
|
+
pagination
|
|
2141
|
+
}) {
|
|
2142
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2437
2143
|
}
|
|
2438
2144
|
/**
|
|
2439
2145
|
* Close the database connection
|