@mastra/cloudflare-d1 0.0.0-sidebar-window-undefined-fix-20251029233656 → 0.0.0-span-scorring-test-20251124132129
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 +403 -3
- package/README.md +10 -5
- package/dist/index.cjs +230 -447
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +230 -447
- 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 -72
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +14 -9
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,9 +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';
|
|
6
|
-
import {
|
|
5
|
+
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
6
|
+
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
7
7
|
|
|
8
8
|
// src/storage/index.ts
|
|
9
9
|
var SqlBuilder = class {
|
|
@@ -252,155 +252,7 @@ function deserializeValue(value, type) {
|
|
|
252
252
|
return value;
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
-
// src/storage/domains/
|
|
256
|
-
var LegacyEvalsStorageD1 = class extends LegacyEvalsStorage {
|
|
257
|
-
operations;
|
|
258
|
-
constructor({ operations }) {
|
|
259
|
-
super();
|
|
260
|
-
this.operations = operations;
|
|
261
|
-
}
|
|
262
|
-
async getEvals(options) {
|
|
263
|
-
const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
|
|
264
|
-
const fullTableName = this.operations.getTableName(TABLE_EVALS);
|
|
265
|
-
const conditions = [];
|
|
266
|
-
const queryParams = [];
|
|
267
|
-
if (agentName) {
|
|
268
|
-
conditions.push(`agent_name = ?`);
|
|
269
|
-
queryParams.push(agentName);
|
|
270
|
-
}
|
|
271
|
-
if (type === "test") {
|
|
272
|
-
conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
|
|
273
|
-
} else if (type === "live") {
|
|
274
|
-
conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
|
|
275
|
-
}
|
|
276
|
-
if (dateRange?.start) {
|
|
277
|
-
conditions.push(`created_at >= ?`);
|
|
278
|
-
queryParams.push(serializeDate(dateRange.start));
|
|
279
|
-
}
|
|
280
|
-
if (dateRange?.end) {
|
|
281
|
-
conditions.push(`created_at <= ?`);
|
|
282
|
-
queryParams.push(serializeDate(dateRange.end));
|
|
283
|
-
}
|
|
284
|
-
const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
|
|
285
|
-
if (conditions.length > 0) {
|
|
286
|
-
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
287
|
-
}
|
|
288
|
-
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
289
|
-
try {
|
|
290
|
-
const countResult = await this.operations.executeQuery({
|
|
291
|
-
sql: countSql,
|
|
292
|
-
params: countParams,
|
|
293
|
-
first: true
|
|
294
|
-
});
|
|
295
|
-
const total = Number(countResult?.count || 0);
|
|
296
|
-
const currentOffset = page * perPage;
|
|
297
|
-
if (total === 0) {
|
|
298
|
-
return {
|
|
299
|
-
evals: [],
|
|
300
|
-
total: 0,
|
|
301
|
-
page,
|
|
302
|
-
perPage,
|
|
303
|
-
hasMore: false
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
307
|
-
if (conditions.length > 0) {
|
|
308
|
-
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
309
|
-
}
|
|
310
|
-
dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
|
|
311
|
-
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
312
|
-
const rows = await this.operations.executeQuery({
|
|
313
|
-
sql: dataSql,
|
|
314
|
-
params: dataParams
|
|
315
|
-
});
|
|
316
|
-
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
317
|
-
const result = deserializeValue(row.result);
|
|
318
|
-
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
319
|
-
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
320
|
-
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
321
|
-
}
|
|
322
|
-
return {
|
|
323
|
-
input: row.input,
|
|
324
|
-
output: row.output,
|
|
325
|
-
result,
|
|
326
|
-
agentName: row.agent_name,
|
|
327
|
-
metricName: row.metric_name,
|
|
328
|
-
instructions: row.instructions,
|
|
329
|
-
testInfo,
|
|
330
|
-
globalRunId: row.global_run_id,
|
|
331
|
-
runId: row.run_id,
|
|
332
|
-
createdAt: row.created_at
|
|
333
|
-
};
|
|
334
|
-
});
|
|
335
|
-
const hasMore = currentOffset + evals.length < total;
|
|
336
|
-
return {
|
|
337
|
-
evals,
|
|
338
|
-
total,
|
|
339
|
-
page,
|
|
340
|
-
perPage,
|
|
341
|
-
hasMore
|
|
342
|
-
};
|
|
343
|
-
} catch (error) {
|
|
344
|
-
throw new MastraError(
|
|
345
|
-
{
|
|
346
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
347
|
-
domain: ErrorDomain.STORAGE,
|
|
348
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
349
|
-
text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
350
|
-
details: { agentName: agentName ?? "", type: type ?? "" }
|
|
351
|
-
},
|
|
352
|
-
error
|
|
353
|
-
);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* @deprecated use getEvals instead
|
|
358
|
-
*/
|
|
359
|
-
async getEvalsByAgentName(agentName, type) {
|
|
360
|
-
const fullTableName = this.operations.getTableName(TABLE_EVALS);
|
|
361
|
-
try {
|
|
362
|
-
let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
|
|
363
|
-
if (type === "test") {
|
|
364
|
-
query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
|
|
365
|
-
} else if (type === "live") {
|
|
366
|
-
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
367
|
-
}
|
|
368
|
-
query.orderBy("created_at", "DESC");
|
|
369
|
-
const { sql, params } = query.build();
|
|
370
|
-
const results = await this.operations.executeQuery({ sql, params });
|
|
371
|
-
return isArrayOfRecords(results) ? results.map((row) => {
|
|
372
|
-
const result = deserializeValue(row.result);
|
|
373
|
-
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
374
|
-
return {
|
|
375
|
-
input: row.input || "",
|
|
376
|
-
output: row.output || "",
|
|
377
|
-
result,
|
|
378
|
-
agentName: row.agent_name || "",
|
|
379
|
-
metricName: row.metric_name || "",
|
|
380
|
-
instructions: row.instructions || "",
|
|
381
|
-
runId: row.run_id || "",
|
|
382
|
-
globalRunId: row.global_run_id || "",
|
|
383
|
-
createdAt: row.created_at || "",
|
|
384
|
-
testInfo
|
|
385
|
-
};
|
|
386
|
-
}) : [];
|
|
387
|
-
} catch (error) {
|
|
388
|
-
const mastraError = new MastraError(
|
|
389
|
-
{
|
|
390
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
391
|
-
domain: ErrorDomain.STORAGE,
|
|
392
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
393
|
-
text: `Failed to retrieve evals for agent ${agentName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
394
|
-
details: { agentName }
|
|
395
|
-
},
|
|
396
|
-
error
|
|
397
|
-
);
|
|
398
|
-
this.logger?.error(mastraError.toString());
|
|
399
|
-
this.logger?.trackException(mastraError);
|
|
400
|
-
return [];
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
};
|
|
255
|
+
// src/storage/domains/memory/index.ts
|
|
404
256
|
var MemoryStorageD1 = class extends MemoryStorage {
|
|
405
257
|
operations;
|
|
406
258
|
constructor({ operations }) {
|
|
@@ -548,39 +400,22 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
548
400
|
return null;
|
|
549
401
|
}
|
|
550
402
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
try {
|
|
557
|
-
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
558
|
-
const { sql, params } = query.build();
|
|
559
|
-
const results = await this.operations.executeQuery({ sql, params });
|
|
560
|
-
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
561
|
-
...thread,
|
|
562
|
-
createdAt: ensureDate(thread.createdAt),
|
|
563
|
-
updatedAt: ensureDate(thread.updatedAt),
|
|
564
|
-
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
565
|
-
}));
|
|
566
|
-
} catch (error) {
|
|
567
|
-
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(
|
|
568
408
|
{
|
|
569
|
-
id: "
|
|
409
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
570
410
|
domain: ErrorDomain.STORAGE,
|
|
571
|
-
category: ErrorCategory.
|
|
572
|
-
|
|
573
|
-
details: { resourceId }
|
|
411
|
+
category: ErrorCategory.USER,
|
|
412
|
+
details: { page }
|
|
574
413
|
},
|
|
575
|
-
|
|
414
|
+
new Error("page must be >= 0")
|
|
576
415
|
);
|
|
577
|
-
this.logger?.error(mastraError.toString());
|
|
578
|
-
this.logger?.trackException(mastraError);
|
|
579
|
-
return [];
|
|
580
416
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
const { resourceId, page, perPage } = args;
|
|
417
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
418
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
584
419
|
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
585
420
|
const mapRowToStorageThreadType = (row) => ({
|
|
586
421
|
...row,
|
|
@@ -592,20 +427,21 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
592
427
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
593
428
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
594
429
|
const total = Number(countResult?.[0]?.count ?? 0);
|
|
595
|
-
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);
|
|
596
432
|
const results = await this.operations.executeQuery(selectQuery.build());
|
|
597
433
|
const threads = results.map(mapRowToStorageThreadType);
|
|
598
434
|
return {
|
|
599
435
|
threads,
|
|
600
436
|
total,
|
|
601
437
|
page,
|
|
602
|
-
perPage,
|
|
603
|
-
hasMore:
|
|
438
|
+
perPage: perPageForResponse,
|
|
439
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
604
440
|
};
|
|
605
441
|
} catch (error) {
|
|
606
442
|
const mastraError = new MastraError(
|
|
607
443
|
{
|
|
608
|
-
id: "
|
|
444
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_THREADS_BY_RESOURCE_ID_ERROR",
|
|
609
445
|
domain: ErrorDomain.STORAGE,
|
|
610
446
|
category: ErrorCategory.THIRD_PARTY,
|
|
611
447
|
text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -619,7 +455,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
619
455
|
threads: [],
|
|
620
456
|
total: 0,
|
|
621
457
|
page,
|
|
622
|
-
perPage,
|
|
458
|
+
perPage: perPageForResponse,
|
|
623
459
|
hasMore: false
|
|
624
460
|
};
|
|
625
461
|
}
|
|
@@ -729,8 +565,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
729
565
|
}
|
|
730
566
|
}
|
|
731
567
|
async saveMessages(args) {
|
|
732
|
-
const { messages
|
|
733
|
-
if (messages.length === 0) return [];
|
|
568
|
+
const { messages } = args;
|
|
569
|
+
if (messages.length === 0) return { messages: [] };
|
|
734
570
|
try {
|
|
735
571
|
const now = /* @__PURE__ */ new Date();
|
|
736
572
|
const threadId = messages[0]?.threadId;
|
|
@@ -778,8 +614,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
778
614
|
]);
|
|
779
615
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
780
616
|
const list = new MessageList().add(messages, "memory");
|
|
781
|
-
|
|
782
|
-
return list.get.all.v1();
|
|
617
|
+
return { messages: list.get.all.db() };
|
|
783
618
|
} catch (error) {
|
|
784
619
|
throw new MastraError(
|
|
785
620
|
{
|
|
@@ -792,9 +627,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
792
627
|
);
|
|
793
628
|
}
|
|
794
629
|
}
|
|
795
|
-
async _getIncludedMessages(threadId,
|
|
630
|
+
async _getIncludedMessages(threadId, include) {
|
|
796
631
|
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
797
|
-
const include = selectBy?.include;
|
|
798
632
|
if (!include) return null;
|
|
799
633
|
const unionQueries = [];
|
|
800
634
|
const params = [];
|
|
@@ -850,74 +684,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
850
684
|
});
|
|
851
685
|
return processedMessages;
|
|
852
686
|
}
|
|
853
|
-
async
|
|
854
|
-
|
|
855
|
-
resourceId,
|
|
856
|
-
selectBy,
|
|
857
|
-
format
|
|
858
|
-
}) {
|
|
859
|
-
try {
|
|
860
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
861
|
-
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
862
|
-
const limit = resolveMessageLimit({
|
|
863
|
-
last: selectBy?.last,
|
|
864
|
-
defaultLimit: 40
|
|
865
|
-
});
|
|
866
|
-
const include = selectBy?.include || [];
|
|
867
|
-
const messages = [];
|
|
868
|
-
if (include.length) {
|
|
869
|
-
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
870
|
-
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
871
|
-
}
|
|
872
|
-
const excludeIds = messages.map((m) => m.id);
|
|
873
|
-
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
874
|
-
if (excludeIds.length > 0) {
|
|
875
|
-
query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
|
|
876
|
-
}
|
|
877
|
-
query.orderBy("createdAt", "DESC").limit(limit);
|
|
878
|
-
const { sql, params } = query.build();
|
|
879
|
-
const result = await this.operations.executeQuery({ sql, params });
|
|
880
|
-
if (Array.isArray(result)) messages.push(...result);
|
|
881
|
-
messages.sort((a, b) => {
|
|
882
|
-
const aRecord = a;
|
|
883
|
-
const bRecord = b;
|
|
884
|
-
const timeA = new Date(aRecord.createdAt).getTime();
|
|
885
|
-
const timeB = new Date(bRecord.createdAt).getTime();
|
|
886
|
-
return timeA - timeB;
|
|
887
|
-
});
|
|
888
|
-
const processedMessages = messages.map((message) => {
|
|
889
|
-
const processedMsg = {};
|
|
890
|
-
for (const [key, value] of Object.entries(message)) {
|
|
891
|
-
if (key === `type` && value === `v2`) continue;
|
|
892
|
-
processedMsg[key] = deserializeValue(value);
|
|
893
|
-
}
|
|
894
|
-
return processedMsg;
|
|
895
|
-
});
|
|
896
|
-
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
897
|
-
const list = new MessageList().add(processedMessages, "memory");
|
|
898
|
-
if (format === `v2`) return list.get.all.v2();
|
|
899
|
-
return list.get.all.v1();
|
|
900
|
-
} catch (error) {
|
|
901
|
-
const mastraError = new MastraError(
|
|
902
|
-
{
|
|
903
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
|
|
904
|
-
domain: ErrorDomain.STORAGE,
|
|
905
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
906
|
-
text: `Failed to retrieve messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
907
|
-
details: { threadId, resourceId: resourceId ?? "" }
|
|
908
|
-
},
|
|
909
|
-
error
|
|
910
|
-
);
|
|
911
|
-
this.logger?.error(mastraError.toString());
|
|
912
|
-
this.logger?.trackException(mastraError);
|
|
913
|
-
throw mastraError;
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
async getMessagesById({
|
|
917
|
-
messageIds,
|
|
918
|
-
format
|
|
919
|
-
}) {
|
|
920
|
-
if (messageIds.length === 0) return [];
|
|
687
|
+
async listMessagesById({ messageIds }) {
|
|
688
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
921
689
|
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
922
690
|
const messages = [];
|
|
923
691
|
try {
|
|
@@ -936,12 +704,11 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
936
704
|
});
|
|
937
705
|
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
938
706
|
const list = new MessageList().add(processedMessages, "memory");
|
|
939
|
-
|
|
940
|
-
return list.get.all.v2();
|
|
707
|
+
return { messages: list.get.all.db() };
|
|
941
708
|
} catch (error) {
|
|
942
709
|
const mastraError = new MastraError(
|
|
943
710
|
{
|
|
944
|
-
id: "
|
|
711
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_BY_ID_ERROR",
|
|
945
712
|
domain: ErrorDomain.STORAGE,
|
|
946
713
|
category: ErrorCategory.THIRD_PARTY,
|
|
947
714
|
text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -954,118 +721,157 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
954
721
|
throw mastraError;
|
|
955
722
|
}
|
|
956
723
|
}
|
|
957
|
-
async
|
|
958
|
-
threadId,
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
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);
|
|
968
750
|
try {
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
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);
|
|
973
761
|
}
|
|
974
|
-
const
|
|
975
|
-
if (
|
|
976
|
-
|
|
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);
|
|
977
767
|
}
|
|
978
|
-
if (
|
|
979
|
-
|
|
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);
|
|
980
772
|
}
|
|
981
|
-
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 });
|
|
982
806
|
const total = Number(countResult[0]?.count ?? 0);
|
|
983
|
-
if (total === 0 &&
|
|
807
|
+
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
|
|
984
808
|
return {
|
|
985
809
|
messages: [],
|
|
986
810
|
total: 0,
|
|
987
811
|
page,
|
|
988
|
-
perPage,
|
|
812
|
+
perPage: perPageForResponse,
|
|
989
813
|
hasMore: false
|
|
990
814
|
};
|
|
991
815
|
}
|
|
992
|
-
const
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
if (selectBy?.last && selectBy.last > 0) {
|
|
1006
|
-
query = `
|
|
1007
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1008
|
-
FROM ${fullTableName}
|
|
1009
|
-
WHERE thread_id = ?
|
|
1010
|
-
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1011
|
-
${toDate ? "AND createdAt <= ?" : ""}
|
|
1012
|
-
${excludeCondition}
|
|
1013
|
-
ORDER BY createdAt DESC
|
|
1014
|
-
LIMIT ?
|
|
1015
|
-
`;
|
|
1016
|
-
queryParams.push(selectBy.last);
|
|
1017
|
-
} else {
|
|
1018
|
-
query = `
|
|
1019
|
-
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1020
|
-
FROM ${fullTableName}
|
|
1021
|
-
WHERE thread_id = ?
|
|
1022
|
-
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1023
|
-
${toDate ? "AND createdAt <= ?" : ""}
|
|
1024
|
-
${excludeCondition}
|
|
1025
|
-
ORDER BY createdAt DESC
|
|
1026
|
-
LIMIT ? OFFSET ?
|
|
1027
|
-
`;
|
|
1028
|
-
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
|
+
}
|
|
1029
829
|
}
|
|
1030
|
-
const
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
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);
|
|
1036
838
|
}
|
|
1037
|
-
|
|
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));
|
|
1038
843
|
});
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
const list = new MessageList().add(processedMessages, "memory");
|
|
1043
|
-
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;
|
|
1044
847
|
return {
|
|
1045
|
-
messages,
|
|
848
|
+
messages: finalMessages,
|
|
1046
849
|
total,
|
|
1047
850
|
page,
|
|
1048
|
-
perPage,
|
|
1049
|
-
hasMore
|
|
851
|
+
perPage: perPageForResponse,
|
|
852
|
+
hasMore
|
|
1050
853
|
};
|
|
1051
854
|
} catch (error) {
|
|
1052
855
|
const mastraError = new MastraError(
|
|
1053
856
|
{
|
|
1054
|
-
id: "
|
|
857
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
|
|
1055
858
|
domain: ErrorDomain.STORAGE,
|
|
1056
859
|
category: ErrorCategory.THIRD_PARTY,
|
|
1057
|
-
text: `Failed to
|
|
1058
|
-
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
|
+
}
|
|
1059
865
|
},
|
|
1060
866
|
error
|
|
1061
867
|
);
|
|
1062
|
-
this.logger?.error(mastraError.toString());
|
|
1063
|
-
this.logger?.trackException(mastraError);
|
|
868
|
+
this.logger?.error?.(mastraError.toString());
|
|
869
|
+
this.logger?.trackException?.(mastraError);
|
|
1064
870
|
return {
|
|
1065
871
|
messages: [],
|
|
1066
872
|
total: 0,
|
|
1067
873
|
page,
|
|
1068
|
-
perPage,
|
|
874
|
+
perPage: perPageForResponse,
|
|
1069
875
|
hasMore: false
|
|
1070
876
|
};
|
|
1071
877
|
}
|
|
@@ -1566,7 +1372,7 @@ function transformScoreRow(row) {
|
|
|
1566
1372
|
deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
|
|
1567
1373
|
deserialized.metadata = safelyParseJSON(row.metadata);
|
|
1568
1374
|
deserialized.additionalContext = safelyParseJSON(row.additionalContext);
|
|
1569
|
-
deserialized.
|
|
1375
|
+
deserialized.requestContext = safelyParseJSON(row.requestContext);
|
|
1570
1376
|
deserialized.entity = safelyParseJSON(row.entity);
|
|
1571
1377
|
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1572
1378
|
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
@@ -1650,7 +1456,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1650
1456
|
);
|
|
1651
1457
|
}
|
|
1652
1458
|
}
|
|
1653
|
-
async
|
|
1459
|
+
async listScoresByScorerId({
|
|
1654
1460
|
scorerId,
|
|
1655
1461
|
entityId,
|
|
1656
1462
|
entityType,
|
|
@@ -1658,6 +1464,9 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1658
1464
|
pagination
|
|
1659
1465
|
}) {
|
|
1660
1466
|
try {
|
|
1467
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1468
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1469
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1661
1470
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1662
1471
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1663
1472
|
if (entityId) {
|
|
@@ -1675,13 +1484,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1675
1484
|
return {
|
|
1676
1485
|
pagination: {
|
|
1677
1486
|
total: 0,
|
|
1678
|
-
page
|
|
1679
|
-
perPage:
|
|
1487
|
+
page,
|
|
1488
|
+
perPage: perPageForResponse,
|
|
1680
1489
|
hasMore: false
|
|
1681
1490
|
},
|
|
1682
1491
|
scores: []
|
|
1683
1492
|
};
|
|
1684
1493
|
}
|
|
1494
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1495
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1685
1496
|
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
|
|
1686
1497
|
if (entityId) {
|
|
1687
1498
|
selectQuery.andWhere("entityId = ?", entityId);
|
|
@@ -1692,16 +1503,16 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1692
1503
|
if (source) {
|
|
1693
1504
|
selectQuery.andWhere("source = ?", source);
|
|
1694
1505
|
}
|
|
1695
|
-
selectQuery.limit(
|
|
1506
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1696
1507
|
const { sql, params } = selectQuery.build();
|
|
1697
1508
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1698
1509
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1699
1510
|
return {
|
|
1700
1511
|
pagination: {
|
|
1701
1512
|
total,
|
|
1702
|
-
page
|
|
1703
|
-
perPage:
|
|
1704
|
-
hasMore:
|
|
1513
|
+
page,
|
|
1514
|
+
perPage: perPageForResponse,
|
|
1515
|
+
hasMore: end < total
|
|
1705
1516
|
},
|
|
1706
1517
|
scores
|
|
1707
1518
|
};
|
|
@@ -1716,11 +1527,14 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1716
1527
|
);
|
|
1717
1528
|
}
|
|
1718
1529
|
}
|
|
1719
|
-
async
|
|
1530
|
+
async listScoresByRunId({
|
|
1720
1531
|
runId,
|
|
1721
1532
|
pagination
|
|
1722
1533
|
}) {
|
|
1723
1534
|
try {
|
|
1535
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1536
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1537
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1724
1538
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1725
1539
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1726
1540
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1729,23 +1543,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1729
1543
|
return {
|
|
1730
1544
|
pagination: {
|
|
1731
1545
|
total: 0,
|
|
1732
|
-
page
|
|
1733
|
-
perPage:
|
|
1546
|
+
page,
|
|
1547
|
+
perPage: perPageForResponse,
|
|
1734
1548
|
hasMore: false
|
|
1735
1549
|
},
|
|
1736
1550
|
scores: []
|
|
1737
1551
|
};
|
|
1738
1552
|
}
|
|
1739
|
-
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);
|
|
1740
1556
|
const { sql, params } = selectQuery.build();
|
|
1741
1557
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1742
1558
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1743
1559
|
return {
|
|
1744
1560
|
pagination: {
|
|
1745
1561
|
total,
|
|
1746
|
-
page
|
|
1747
|
-
perPage:
|
|
1748
|
-
hasMore:
|
|
1562
|
+
page,
|
|
1563
|
+
perPage: perPageForResponse,
|
|
1564
|
+
hasMore: end < total
|
|
1749
1565
|
},
|
|
1750
1566
|
scores
|
|
1751
1567
|
};
|
|
@@ -1760,12 +1576,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1760
1576
|
);
|
|
1761
1577
|
}
|
|
1762
1578
|
}
|
|
1763
|
-
async
|
|
1579
|
+
async listScoresByEntityId({
|
|
1764
1580
|
entityId,
|
|
1765
1581
|
entityType,
|
|
1766
1582
|
pagination
|
|
1767
1583
|
}) {
|
|
1768
1584
|
try {
|
|
1585
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1586
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1587
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1769
1588
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1770
1589
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1771
1590
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1774,23 +1593,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1774
1593
|
return {
|
|
1775
1594
|
pagination: {
|
|
1776
1595
|
total: 0,
|
|
1777
|
-
page
|
|
1778
|
-
perPage:
|
|
1596
|
+
page,
|
|
1597
|
+
perPage: perPageForResponse,
|
|
1779
1598
|
hasMore: false
|
|
1780
1599
|
},
|
|
1781
1600
|
scores: []
|
|
1782
1601
|
};
|
|
1783
1602
|
}
|
|
1784
|
-
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);
|
|
1785
1606
|
const { sql, params } = selectQuery.build();
|
|
1786
1607
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1787
1608
|
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1788
1609
|
return {
|
|
1789
1610
|
pagination: {
|
|
1790
1611
|
total,
|
|
1791
|
-
page
|
|
1792
|
-
perPage:
|
|
1793
|
-
hasMore:
|
|
1612
|
+
page,
|
|
1613
|
+
perPage: perPageForResponse,
|
|
1614
|
+
hasMore: end < total
|
|
1794
1615
|
},
|
|
1795
1616
|
scores
|
|
1796
1617
|
};
|
|
@@ -1805,12 +1626,15 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1805
1626
|
);
|
|
1806
1627
|
}
|
|
1807
1628
|
}
|
|
1808
|
-
async
|
|
1629
|
+
async listScoresBySpan({
|
|
1809
1630
|
traceId,
|
|
1810
1631
|
spanId,
|
|
1811
1632
|
pagination
|
|
1812
1633
|
}) {
|
|
1813
1634
|
try {
|
|
1635
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1636
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1637
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1814
1638
|
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1815
1639
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1816
1640
|
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
@@ -1819,25 +1643,25 @@ var ScoresStorageD1 = class extends ScoresStorage {
|
|
|
1819
1643
|
return {
|
|
1820
1644
|
pagination: {
|
|
1821
1645
|
total: 0,
|
|
1822
|
-
page
|
|
1823
|
-
perPage:
|
|
1646
|
+
page,
|
|
1647
|
+
perPage: perPageForResponse,
|
|
1824
1648
|
hasMore: false
|
|
1825
1649
|
},
|
|
1826
1650
|
scores: []
|
|
1827
1651
|
};
|
|
1828
1652
|
}
|
|
1829
|
-
const
|
|
1830
|
-
const
|
|
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);
|
|
1831
1656
|
const { sql, params } = selectQuery.build();
|
|
1832
1657
|
const results = await this.operations.executeQuery({ sql, params });
|
|
1833
|
-
const
|
|
1834
|
-
const scores = rows.slice(0, pagination.perPage).map(transformScoreRow);
|
|
1658
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1835
1659
|
return {
|
|
1836
1660
|
pagination: {
|
|
1837
1661
|
total,
|
|
1838
|
-
page
|
|
1839
|
-
perPage:
|
|
1840
|
-
hasMore:
|
|
1662
|
+
page,
|
|
1663
|
+
perPage: perPageForResponse,
|
|
1664
|
+
hasMore: end < total
|
|
1841
1665
|
},
|
|
1842
1666
|
scores
|
|
1843
1667
|
};
|
|
@@ -1864,7 +1688,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
1864
1688
|
// runId,
|
|
1865
1689
|
// stepId,
|
|
1866
1690
|
// result,
|
|
1867
|
-
//
|
|
1691
|
+
// requestContext,
|
|
1868
1692
|
}) {
|
|
1869
1693
|
throw new Error("Method not implemented.");
|
|
1870
1694
|
}
|
|
@@ -1968,19 +1792,24 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
1968
1792
|
resourceId: row.resourceId
|
|
1969
1793
|
};
|
|
1970
1794
|
}
|
|
1971
|
-
async
|
|
1795
|
+
async listWorkflowRuns({
|
|
1972
1796
|
workflowName,
|
|
1973
1797
|
fromDate,
|
|
1974
1798
|
toDate,
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
resourceId
|
|
1799
|
+
page,
|
|
1800
|
+
perPage,
|
|
1801
|
+
resourceId,
|
|
1802
|
+
status
|
|
1978
1803
|
} = {}) {
|
|
1979
1804
|
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1980
1805
|
try {
|
|
1981
1806
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
1982
1807
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
1983
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
|
+
}
|
|
1984
1813
|
if (resourceId) {
|
|
1985
1814
|
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
1986
1815
|
if (hasResourceId) {
|
|
@@ -1999,11 +1828,14 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
1999
1828
|
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2000
1829
|
}
|
|
2001
1830
|
builder.orderBy("createdAt", "DESC");
|
|
2002
|
-
if (typeof
|
|
2003
|
-
|
|
1831
|
+
if (typeof perPage === "number" && typeof page === "number") {
|
|
1832
|
+
const offset = page * perPage;
|
|
1833
|
+
builder.limit(perPage);
|
|
1834
|
+
builder.offset(offset);
|
|
1835
|
+
}
|
|
2004
1836
|
const { sql, params } = builder.build();
|
|
2005
1837
|
let total = 0;
|
|
2006
|
-
if (
|
|
1838
|
+
if (perPage !== void 0 && page !== void 0) {
|
|
2007
1839
|
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
2008
1840
|
const countResult = await this.operations.executeQuery({
|
|
2009
1841
|
sql: countSql,
|
|
@@ -2018,7 +1850,7 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
2018
1850
|
} catch (error) {
|
|
2019
1851
|
throw new MastraError(
|
|
2020
1852
|
{
|
|
2021
|
-
id: "
|
|
1853
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_WORKFLOW_RUNS_ERROR",
|
|
2022
1854
|
domain: ErrorDomain.STORAGE,
|
|
2023
1855
|
category: ErrorCategory.THIRD_PARTY,
|
|
2024
1856
|
text: `Failed to retrieve workflow runs: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -2080,7 +1912,7 @@ var D1Store = class extends MastraStorage {
|
|
|
2080
1912
|
*/
|
|
2081
1913
|
constructor(config) {
|
|
2082
1914
|
try {
|
|
2083
|
-
super({ name: "D1" });
|
|
1915
|
+
super({ id: config.id, name: "D1" });
|
|
2084
1916
|
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
2085
1917
|
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
2086
1918
|
}
|
|
@@ -2134,9 +1966,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2134
1966
|
const scores = new ScoresStorageD1({
|
|
2135
1967
|
operations
|
|
2136
1968
|
});
|
|
2137
|
-
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2138
|
-
operations
|
|
2139
|
-
});
|
|
2140
1969
|
const workflows = new WorkflowsStorageD1({
|
|
2141
1970
|
operations
|
|
2142
1971
|
});
|
|
@@ -2146,7 +1975,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2146
1975
|
this.stores = {
|
|
2147
1976
|
operations,
|
|
2148
1977
|
scores,
|
|
2149
|
-
legacyEvals,
|
|
2150
1978
|
workflows,
|
|
2151
1979
|
memory
|
|
2152
1980
|
};
|
|
@@ -2158,7 +1986,7 @@ var D1Store = class extends MastraStorage {
|
|
|
2158
1986
|
hasColumn: true,
|
|
2159
1987
|
createTable: true,
|
|
2160
1988
|
deleteMessages: false,
|
|
2161
|
-
|
|
1989
|
+
listScoresBySpan: true
|
|
2162
1990
|
};
|
|
2163
1991
|
}
|
|
2164
1992
|
async createTable({
|
|
@@ -2198,15 +2026,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2198
2026
|
async getThreadById({ threadId }) {
|
|
2199
2027
|
return this.stores.memory.getThreadById({ threadId });
|
|
2200
2028
|
}
|
|
2201
|
-
/**
|
|
2202
|
-
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
2203
|
-
*/
|
|
2204
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2205
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2206
|
-
}
|
|
2207
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2208
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2209
|
-
}
|
|
2210
2029
|
async saveThread({ thread }) {
|
|
2211
2030
|
return this.stores.memory.saveThread({ thread });
|
|
2212
2031
|
}
|
|
@@ -2223,34 +2042,14 @@ var D1Store = class extends MastraStorage {
|
|
|
2223
2042
|
async saveMessages(args) {
|
|
2224
2043
|
return this.stores.memory.saveMessages(args);
|
|
2225
2044
|
}
|
|
2226
|
-
async getMessages({
|
|
2227
|
-
threadId,
|
|
2228
|
-
selectBy,
|
|
2229
|
-
format
|
|
2230
|
-
}) {
|
|
2231
|
-
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2232
|
-
}
|
|
2233
|
-
async getMessagesById({
|
|
2234
|
-
messageIds,
|
|
2235
|
-
format
|
|
2236
|
-
}) {
|
|
2237
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2238
|
-
}
|
|
2239
|
-
async getMessagesPaginated({
|
|
2240
|
-
threadId,
|
|
2241
|
-
selectBy,
|
|
2242
|
-
format
|
|
2243
|
-
}) {
|
|
2244
|
-
return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
|
|
2245
|
-
}
|
|
2246
2045
|
async updateWorkflowResults({
|
|
2247
2046
|
workflowName,
|
|
2248
2047
|
runId,
|
|
2249
2048
|
stepId,
|
|
2250
2049
|
result,
|
|
2251
|
-
|
|
2050
|
+
requestContext
|
|
2252
2051
|
}) {
|
|
2253
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2052
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2254
2053
|
}
|
|
2255
2054
|
async updateWorkflowState({
|
|
2256
2055
|
workflowName,
|
|
@@ -2270,15 +2069,8 @@ var D1Store = class extends MastraStorage {
|
|
|
2270
2069
|
async loadWorkflowSnapshot(params) {
|
|
2271
2070
|
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2272
2071
|
}
|
|
2273
|
-
async
|
|
2274
|
-
|
|
2275
|
-
fromDate,
|
|
2276
|
-
toDate,
|
|
2277
|
-
limit,
|
|
2278
|
-
offset,
|
|
2279
|
-
resourceId
|
|
2280
|
-
} = {}) {
|
|
2281
|
-
return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2072
|
+
async listWorkflowRuns(args = {}) {
|
|
2073
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2282
2074
|
}
|
|
2283
2075
|
async getWorkflowRunById({
|
|
2284
2076
|
runId,
|
|
@@ -2294,15 +2086,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2294
2086
|
async batchInsert({ tableName, records }) {
|
|
2295
2087
|
return this.stores.operations.batchInsert({ tableName, records });
|
|
2296
2088
|
}
|
|
2297
|
-
/**
|
|
2298
|
-
* @deprecated use getEvals instead
|
|
2299
|
-
*/
|
|
2300
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2301
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2302
|
-
}
|
|
2303
|
-
async getEvals(options) {
|
|
2304
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2305
|
-
}
|
|
2306
2089
|
async updateMessages(_args) {
|
|
2307
2090
|
return this.stores.memory.updateMessages(_args);
|
|
2308
2091
|
}
|
|
@@ -2325,38 +2108,38 @@ var D1Store = class extends MastraStorage {
|
|
|
2325
2108
|
async saveScore(_score) {
|
|
2326
2109
|
return this.stores.scores.saveScore(_score);
|
|
2327
2110
|
}
|
|
2328
|
-
async
|
|
2111
|
+
async listScoresByRunId({
|
|
2329
2112
|
runId: _runId,
|
|
2330
2113
|
pagination: _pagination
|
|
2331
2114
|
}) {
|
|
2332
|
-
return this.stores.scores.
|
|
2115
|
+
return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2333
2116
|
}
|
|
2334
|
-
async
|
|
2117
|
+
async listScoresByEntityId({
|
|
2335
2118
|
entityId: _entityId,
|
|
2336
2119
|
entityType: _entityType,
|
|
2337
2120
|
pagination: _pagination
|
|
2338
2121
|
}) {
|
|
2339
|
-
return this.stores.scores.
|
|
2122
|
+
return this.stores.scores.listScoresByEntityId({
|
|
2340
2123
|
entityId: _entityId,
|
|
2341
2124
|
entityType: _entityType,
|
|
2342
2125
|
pagination: _pagination
|
|
2343
2126
|
});
|
|
2344
2127
|
}
|
|
2345
|
-
async
|
|
2128
|
+
async listScoresByScorerId({
|
|
2346
2129
|
scorerId,
|
|
2347
2130
|
pagination,
|
|
2348
2131
|
entityId,
|
|
2349
2132
|
entityType,
|
|
2350
2133
|
source
|
|
2351
2134
|
}) {
|
|
2352
|
-
return this.stores.scores.
|
|
2135
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2353
2136
|
}
|
|
2354
|
-
async
|
|
2137
|
+
async listScoresBySpan({
|
|
2355
2138
|
traceId,
|
|
2356
2139
|
spanId,
|
|
2357
2140
|
pagination
|
|
2358
2141
|
}) {
|
|
2359
|
-
return this.stores.scores.
|
|
2142
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2360
2143
|
}
|
|
2361
2144
|
/**
|
|
2362
2145
|
* Close the database connection
|