@mastra/dynamodb 0.0.0-share-agent-metadata-with-cloud-20250718110128 → 0.0.0-sidebar-window-undefined-fix-20251029233656
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 +1074 -0
- package/README.md +0 -4
- package/dist/entities/eval.d.ts +102 -0
- package/dist/entities/eval.d.ts.map +1 -0
- package/dist/entities/index.d.ts +761 -0
- package/dist/entities/index.d.ts.map +1 -0
- package/dist/entities/message.d.ts +100 -0
- package/dist/entities/message.d.ts.map +1 -0
- package/dist/entities/resource.d.ts +54 -0
- package/dist/entities/resource.d.ts.map +1 -0
- package/dist/entities/score.d.ts +244 -0
- package/dist/entities/score.d.ts.map +1 -0
- package/dist/entities/thread.d.ts +69 -0
- package/dist/entities/thread.d.ts.map +1 -0
- package/dist/entities/trace.d.ts +127 -0
- package/dist/entities/trace.d.ts.map +1 -0
- package/dist/entities/utils.d.ts +21 -0
- package/dist/entities/utils.d.ts.map +1 -0
- package/dist/entities/workflow-snapshot.d.ts +74 -0
- package/dist/entities/workflow-snapshot.d.ts.map +1 -0
- package/dist/index.cjs +1978 -578
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1979 -579
- package/dist/index.js.map +1 -0
- package/dist/storage/domains/legacy-evals/index.d.ts +19 -0
- package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +89 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/operations/index.d.ts +69 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -0
- package/dist/storage/domains/score/index.d.ts +51 -0
- package/dist/storage/domains/score/index.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +51 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +244 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/package.json +24 -14
- package/dist/_tsup-dts-rollup.d.cts +0 -1160
- package/dist/_tsup-dts-rollup.d.ts +0 -1160
- package/dist/index.d.cts +0 -2
- package/src/entities/eval.ts +0 -102
- package/src/entities/index.ts +0 -23
- package/src/entities/message.ts +0 -143
- package/src/entities/thread.ts +0 -66
- package/src/entities/trace.ts +0 -129
- package/src/entities/utils.ts +0 -51
- package/src/entities/workflow-snapshot.ts +0 -56
- package/src/index.ts +0 -1
- package/src/storage/docker-compose.yml +0 -16
- package/src/storage/index.test.ts +0 -1483
- package/src/storage/index.ts +0 -1383
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { DynamoDBClient, DescribeTableCommand } from '@aws-sdk/client-dynamodb';
|
|
2
2
|
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
|
|
3
|
-
import { MessageList } from '@mastra/core/agent';
|
|
4
3
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
5
|
-
import { MastraStorage, TABLE_TRACES, TABLE_EVALS, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
|
|
4
|
+
import { MastraStorage, StoreOperations, WorkflowsStorage, MemoryStorage, resolveMessageLimit, ScoresStorage, LegacyEvalsStorage, TABLE_AI_SPANS, TABLE_RESOURCES, TABLE_TRACES, TABLE_SCORERS, TABLE_EVALS, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
|
|
6
5
|
import { Entity, Service } from 'electrodb';
|
|
6
|
+
import { MessageList } from '@mastra/core/agent';
|
|
7
|
+
import { saveScorePayloadSchema } from '@mastra/core/scores';
|
|
7
8
|
|
|
8
9
|
// src/storage/index.ts
|
|
9
10
|
|
|
@@ -294,9 +295,9 @@ var messageEntity = new Entity({
|
|
|
294
295
|
}
|
|
295
296
|
}
|
|
296
297
|
});
|
|
297
|
-
var
|
|
298
|
+
var resourceEntity = new Entity({
|
|
298
299
|
model: {
|
|
299
|
-
entity: "
|
|
300
|
+
entity: "resource",
|
|
300
301
|
version: "1",
|
|
301
302
|
service: "mastra"
|
|
302
303
|
},
|
|
@@ -310,25 +311,21 @@ var threadEntity = new Entity({
|
|
|
310
311
|
type: "string",
|
|
311
312
|
required: true
|
|
312
313
|
},
|
|
313
|
-
|
|
314
|
-
type: "string",
|
|
315
|
-
required: true
|
|
316
|
-
},
|
|
317
|
-
title: {
|
|
314
|
+
workingMemory: {
|
|
318
315
|
type: "string",
|
|
319
|
-
required:
|
|
316
|
+
required: false
|
|
320
317
|
},
|
|
321
318
|
metadata: {
|
|
322
319
|
type: "string",
|
|
323
320
|
required: false,
|
|
324
|
-
// Stringify
|
|
321
|
+
// Stringify content object on set if it's not already a string
|
|
325
322
|
set: (value) => {
|
|
326
323
|
if (value && typeof value !== "string") {
|
|
327
324
|
return JSON.stringify(value);
|
|
328
325
|
}
|
|
329
326
|
return value;
|
|
330
327
|
},
|
|
331
|
-
// Parse JSON string to object on get
|
|
328
|
+
// Parse JSON string to object on get ONLY if it looks like JSON
|
|
332
329
|
get: (value) => {
|
|
333
330
|
if (value && typeof value === "string") {
|
|
334
331
|
try {
|
|
@@ -346,18 +343,13 @@ var threadEntity = new Entity({
|
|
|
346
343
|
indexes: {
|
|
347
344
|
primary: {
|
|
348
345
|
pk: { field: "pk", composite: ["entity", "id"] },
|
|
349
|
-
sk: { field: "sk", composite: ["
|
|
350
|
-
},
|
|
351
|
-
byResource: {
|
|
352
|
-
index: "gsi1",
|
|
353
|
-
pk: { field: "gsi1pk", composite: ["entity", "resourceId"] },
|
|
354
|
-
sk: { field: "gsi1sk", composite: ["createdAt"] }
|
|
346
|
+
sk: { field: "sk", composite: ["entity"] }
|
|
355
347
|
}
|
|
356
348
|
}
|
|
357
349
|
});
|
|
358
|
-
var
|
|
350
|
+
var scoreEntity = new Entity({
|
|
359
351
|
model: {
|
|
360
|
-
entity: "
|
|
352
|
+
entity: "score",
|
|
361
353
|
version: "1",
|
|
362
354
|
service: "mastra"
|
|
363
355
|
},
|
|
@@ -371,123 +363,315 @@ var traceEntity = new Entity({
|
|
|
371
363
|
type: "string",
|
|
372
364
|
required: true
|
|
373
365
|
},
|
|
374
|
-
|
|
366
|
+
scorerId: {
|
|
367
|
+
type: "string",
|
|
368
|
+
required: true
|
|
369
|
+
},
|
|
370
|
+
traceId: {
|
|
375
371
|
type: "string",
|
|
376
372
|
required: false
|
|
377
373
|
},
|
|
378
|
-
|
|
374
|
+
spanId: {
|
|
379
375
|
type: "string",
|
|
380
|
-
required:
|
|
376
|
+
required: false
|
|
381
377
|
},
|
|
382
|
-
|
|
378
|
+
runId: {
|
|
383
379
|
type: "string",
|
|
384
380
|
required: true
|
|
385
381
|
},
|
|
386
|
-
|
|
382
|
+
scorer: {
|
|
387
383
|
type: "string",
|
|
388
|
-
required: true
|
|
384
|
+
required: true,
|
|
385
|
+
set: (value) => {
|
|
386
|
+
if (value && typeof value !== "string") {
|
|
387
|
+
return JSON.stringify(value);
|
|
388
|
+
}
|
|
389
|
+
return value;
|
|
390
|
+
},
|
|
391
|
+
get: (value) => {
|
|
392
|
+
if (value && typeof value === "string") {
|
|
393
|
+
try {
|
|
394
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
395
|
+
return JSON.parse(value);
|
|
396
|
+
}
|
|
397
|
+
} catch {
|
|
398
|
+
return value;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return value;
|
|
402
|
+
}
|
|
389
403
|
},
|
|
390
|
-
|
|
404
|
+
extractStepResult: {
|
|
405
|
+
type: "string",
|
|
406
|
+
required: false,
|
|
407
|
+
set: (value) => {
|
|
408
|
+
if (value && typeof value !== "string") {
|
|
409
|
+
return JSON.stringify(value);
|
|
410
|
+
}
|
|
411
|
+
return value;
|
|
412
|
+
},
|
|
413
|
+
get: (value) => {
|
|
414
|
+
if (value && typeof value === "string") {
|
|
415
|
+
try {
|
|
416
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
417
|
+
return JSON.parse(value);
|
|
418
|
+
}
|
|
419
|
+
} catch {
|
|
420
|
+
return value;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return value;
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
preprocessStepResult: {
|
|
427
|
+
type: "string",
|
|
428
|
+
required: false,
|
|
429
|
+
set: (value) => {
|
|
430
|
+
if (value && typeof value !== "string") {
|
|
431
|
+
return JSON.stringify(value);
|
|
432
|
+
}
|
|
433
|
+
return value;
|
|
434
|
+
},
|
|
435
|
+
get: (value) => {
|
|
436
|
+
if (value && typeof value === "string") {
|
|
437
|
+
try {
|
|
438
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
439
|
+
return JSON.parse(value);
|
|
440
|
+
}
|
|
441
|
+
} catch {
|
|
442
|
+
return value;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return value;
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
analyzeStepResult: {
|
|
449
|
+
type: "string",
|
|
450
|
+
required: false,
|
|
451
|
+
set: (value) => {
|
|
452
|
+
if (value && typeof value !== "string") {
|
|
453
|
+
return JSON.stringify(value);
|
|
454
|
+
}
|
|
455
|
+
return value;
|
|
456
|
+
},
|
|
457
|
+
get: (value) => {
|
|
458
|
+
if (value && typeof value === "string") {
|
|
459
|
+
try {
|
|
460
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
461
|
+
return JSON.parse(value);
|
|
462
|
+
}
|
|
463
|
+
} catch {
|
|
464
|
+
return value;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return value;
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
score: {
|
|
391
471
|
type: "number",
|
|
392
472
|
required: true
|
|
393
473
|
},
|
|
394
|
-
|
|
474
|
+
reason: {
|
|
395
475
|
type: "string",
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
476
|
+
required: false
|
|
477
|
+
},
|
|
478
|
+
extractPrompt: {
|
|
479
|
+
type: "string",
|
|
480
|
+
required: false
|
|
481
|
+
},
|
|
482
|
+
analyzePrompt: {
|
|
483
|
+
type: "string",
|
|
484
|
+
required: false
|
|
485
|
+
},
|
|
486
|
+
// Deprecated in favor of generateReasonPrompt
|
|
487
|
+
reasonPrompt: {
|
|
488
|
+
type: "string",
|
|
489
|
+
required: false
|
|
490
|
+
},
|
|
491
|
+
generateScorePrompt: {
|
|
492
|
+
type: "string",
|
|
493
|
+
required: false
|
|
494
|
+
},
|
|
495
|
+
generateReasonPrompt: {
|
|
496
|
+
type: "string",
|
|
497
|
+
required: false
|
|
498
|
+
},
|
|
499
|
+
input: {
|
|
500
|
+
type: "string",
|
|
501
|
+
required: true,
|
|
399
502
|
set: (value) => {
|
|
400
503
|
if (value && typeof value !== "string") {
|
|
401
504
|
return JSON.stringify(value);
|
|
402
505
|
}
|
|
403
506
|
return value;
|
|
404
507
|
},
|
|
405
|
-
// Parse JSON string to object on get
|
|
406
508
|
get: (value) => {
|
|
407
|
-
|
|
509
|
+
if (value && typeof value === "string") {
|
|
510
|
+
try {
|
|
511
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
512
|
+
return JSON.parse(value);
|
|
513
|
+
}
|
|
514
|
+
} catch {
|
|
515
|
+
return value;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return value;
|
|
408
519
|
}
|
|
409
520
|
},
|
|
410
|
-
|
|
521
|
+
output: {
|
|
522
|
+
type: "string",
|
|
523
|
+
required: true,
|
|
524
|
+
set: (value) => {
|
|
525
|
+
if (value && typeof value !== "string") {
|
|
526
|
+
return JSON.stringify(value);
|
|
527
|
+
}
|
|
528
|
+
return value;
|
|
529
|
+
},
|
|
530
|
+
get: (value) => {
|
|
531
|
+
if (value && typeof value === "string") {
|
|
532
|
+
try {
|
|
533
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
534
|
+
return JSON.parse(value);
|
|
535
|
+
}
|
|
536
|
+
} catch {
|
|
537
|
+
return value;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return value;
|
|
541
|
+
}
|
|
542
|
+
},
|
|
543
|
+
additionalContext: {
|
|
411
544
|
type: "string",
|
|
412
|
-
// JSON stringified
|
|
413
545
|
required: false,
|
|
414
|
-
// Stringify object on set
|
|
415
546
|
set: (value) => {
|
|
416
547
|
if (value && typeof value !== "string") {
|
|
417
548
|
return JSON.stringify(value);
|
|
418
549
|
}
|
|
419
550
|
return value;
|
|
420
551
|
},
|
|
421
|
-
// Parse JSON string to object on get
|
|
422
552
|
get: (value) => {
|
|
553
|
+
if (value && typeof value === "string") {
|
|
554
|
+
try {
|
|
555
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
556
|
+
return JSON.parse(value);
|
|
557
|
+
}
|
|
558
|
+
} catch {
|
|
559
|
+
return value;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
423
562
|
return value;
|
|
424
563
|
}
|
|
425
564
|
},
|
|
426
|
-
|
|
565
|
+
runtimeContext: {
|
|
427
566
|
type: "string",
|
|
428
|
-
// JSON stringified
|
|
429
567
|
required: false,
|
|
430
|
-
// Stringify object on set
|
|
431
568
|
set: (value) => {
|
|
432
569
|
if (value && typeof value !== "string") {
|
|
433
570
|
return JSON.stringify(value);
|
|
434
571
|
}
|
|
435
572
|
return value;
|
|
436
573
|
},
|
|
437
|
-
// Parse JSON string to object on get
|
|
438
574
|
get: (value) => {
|
|
575
|
+
if (value && typeof value === "string") {
|
|
576
|
+
try {
|
|
577
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
578
|
+
return JSON.parse(value);
|
|
579
|
+
}
|
|
580
|
+
} catch {
|
|
581
|
+
return value;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
439
584
|
return value;
|
|
440
585
|
}
|
|
441
586
|
},
|
|
442
|
-
|
|
587
|
+
entityType: {
|
|
588
|
+
type: "string",
|
|
589
|
+
required: false
|
|
590
|
+
},
|
|
591
|
+
entityData: {
|
|
443
592
|
type: "string",
|
|
444
|
-
// JSON stringified
|
|
445
593
|
required: false,
|
|
446
|
-
// Stringify object on set
|
|
447
594
|
set: (value) => {
|
|
448
595
|
if (value && typeof value !== "string") {
|
|
449
596
|
return JSON.stringify(value);
|
|
450
597
|
}
|
|
451
598
|
return value;
|
|
452
599
|
},
|
|
453
|
-
// Parse JSON string to object on get
|
|
454
600
|
get: (value) => {
|
|
601
|
+
if (value && typeof value === "string") {
|
|
602
|
+
try {
|
|
603
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
604
|
+
return JSON.parse(value);
|
|
605
|
+
}
|
|
606
|
+
} catch {
|
|
607
|
+
return value;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
455
610
|
return value;
|
|
456
611
|
}
|
|
457
612
|
},
|
|
458
|
-
|
|
613
|
+
entityId: {
|
|
459
614
|
type: "string",
|
|
460
615
|
required: false
|
|
461
616
|
},
|
|
462
|
-
|
|
463
|
-
type: "
|
|
617
|
+
source: {
|
|
618
|
+
type: "string",
|
|
464
619
|
required: true
|
|
465
620
|
},
|
|
466
|
-
|
|
467
|
-
type: "
|
|
468
|
-
required:
|
|
621
|
+
resourceId: {
|
|
622
|
+
type: "string",
|
|
623
|
+
required: false
|
|
624
|
+
},
|
|
625
|
+
threadId: {
|
|
626
|
+
type: "string",
|
|
627
|
+
required: false
|
|
469
628
|
}
|
|
470
629
|
},
|
|
471
630
|
indexes: {
|
|
472
631
|
primary: {
|
|
473
632
|
pk: { field: "pk", composite: ["entity", "id"] },
|
|
474
|
-
sk: { field: "sk", composite: [] }
|
|
633
|
+
sk: { field: "sk", composite: ["entity"] }
|
|
475
634
|
},
|
|
476
|
-
|
|
635
|
+
byScorer: {
|
|
477
636
|
index: "gsi1",
|
|
478
|
-
pk: { field: "gsi1pk", composite: ["entity", "
|
|
479
|
-
sk: { field: "gsi1sk", composite: ["
|
|
637
|
+
pk: { field: "gsi1pk", composite: ["entity", "scorerId"] },
|
|
638
|
+
sk: { field: "gsi1sk", composite: ["createdAt"] }
|
|
480
639
|
},
|
|
481
|
-
|
|
640
|
+
byRun: {
|
|
482
641
|
index: "gsi2",
|
|
483
|
-
pk: { field: "gsi2pk", composite: ["entity", "
|
|
484
|
-
sk: { field: "gsi2sk", composite: ["
|
|
642
|
+
pk: { field: "gsi2pk", composite: ["entity", "runId"] },
|
|
643
|
+
sk: { field: "gsi2sk", composite: ["createdAt"] }
|
|
644
|
+
},
|
|
645
|
+
byTrace: {
|
|
646
|
+
index: "gsi3",
|
|
647
|
+
pk: { field: "gsi3pk", composite: ["entity", "traceId"] },
|
|
648
|
+
sk: { field: "gsi3sk", composite: ["createdAt"] }
|
|
649
|
+
},
|
|
650
|
+
byEntityData: {
|
|
651
|
+
index: "gsi4",
|
|
652
|
+
pk: { field: "gsi4pk", composite: ["entity", "entityId"] },
|
|
653
|
+
sk: { field: "gsi4sk", composite: ["createdAt"] }
|
|
654
|
+
},
|
|
655
|
+
byResource: {
|
|
656
|
+
index: "gsi5",
|
|
657
|
+
pk: { field: "gsi5pk", composite: ["entity", "resourceId"] },
|
|
658
|
+
sk: { field: "gsi5sk", composite: ["createdAt"] }
|
|
659
|
+
},
|
|
660
|
+
byThread: {
|
|
661
|
+
index: "gsi6",
|
|
662
|
+
pk: { field: "gsi6pk", composite: ["entity", "threadId"] },
|
|
663
|
+
sk: { field: "gsi6sk", composite: ["createdAt"] }
|
|
664
|
+
},
|
|
665
|
+
bySpan: {
|
|
666
|
+
index: "gsi7",
|
|
667
|
+
pk: { field: "gsi7pk", composite: ["entity", "traceId", "spanId"] },
|
|
668
|
+
sk: { field: "gsi7sk", composite: ["createdAt"] }
|
|
485
669
|
}
|
|
486
670
|
}
|
|
487
671
|
});
|
|
488
|
-
var
|
|
672
|
+
var threadEntity = new Entity({
|
|
489
673
|
model: {
|
|
490
|
-
entity: "
|
|
674
|
+
entity: "thread",
|
|
491
675
|
version: "1",
|
|
492
676
|
service: "mastra"
|
|
493
677
|
},
|
|
@@ -497,11 +681,202 @@ var workflowSnapshotEntity = new Entity({
|
|
|
497
681
|
required: true
|
|
498
682
|
},
|
|
499
683
|
...baseAttributes,
|
|
500
|
-
|
|
684
|
+
id: {
|
|
501
685
|
type: "string",
|
|
502
686
|
required: true
|
|
503
687
|
},
|
|
504
|
-
|
|
688
|
+
resourceId: {
|
|
689
|
+
type: "string",
|
|
690
|
+
required: true
|
|
691
|
+
},
|
|
692
|
+
title: {
|
|
693
|
+
type: "string",
|
|
694
|
+
required: true
|
|
695
|
+
},
|
|
696
|
+
metadata: {
|
|
697
|
+
type: "string",
|
|
698
|
+
required: false,
|
|
699
|
+
// Stringify metadata object on set if it's not already a string
|
|
700
|
+
set: (value) => {
|
|
701
|
+
if (value && typeof value !== "string") {
|
|
702
|
+
return JSON.stringify(value);
|
|
703
|
+
}
|
|
704
|
+
return value;
|
|
705
|
+
},
|
|
706
|
+
// Parse JSON string to object on get
|
|
707
|
+
get: (value) => {
|
|
708
|
+
if (value && typeof value === "string") {
|
|
709
|
+
try {
|
|
710
|
+
if (value.startsWith("{") || value.startsWith("[")) {
|
|
711
|
+
return JSON.parse(value);
|
|
712
|
+
}
|
|
713
|
+
} catch {
|
|
714
|
+
return value;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return value;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
indexes: {
|
|
722
|
+
primary: {
|
|
723
|
+
pk: { field: "pk", composite: ["entity", "id"] },
|
|
724
|
+
sk: { field: "sk", composite: ["id"] }
|
|
725
|
+
},
|
|
726
|
+
byResource: {
|
|
727
|
+
index: "gsi1",
|
|
728
|
+
pk: { field: "gsi1pk", composite: ["entity", "resourceId"] },
|
|
729
|
+
sk: { field: "gsi1sk", composite: ["createdAt"] }
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
var traceEntity = new Entity({
|
|
734
|
+
model: {
|
|
735
|
+
entity: "trace",
|
|
736
|
+
version: "1",
|
|
737
|
+
service: "mastra"
|
|
738
|
+
},
|
|
739
|
+
attributes: {
|
|
740
|
+
entity: {
|
|
741
|
+
type: "string",
|
|
742
|
+
required: true
|
|
743
|
+
},
|
|
744
|
+
...baseAttributes,
|
|
745
|
+
id: {
|
|
746
|
+
type: "string",
|
|
747
|
+
required: true
|
|
748
|
+
},
|
|
749
|
+
parentSpanId: {
|
|
750
|
+
type: "string",
|
|
751
|
+
required: false
|
|
752
|
+
},
|
|
753
|
+
name: {
|
|
754
|
+
type: "string",
|
|
755
|
+
required: true
|
|
756
|
+
},
|
|
757
|
+
traceId: {
|
|
758
|
+
type: "string",
|
|
759
|
+
required: true
|
|
760
|
+
},
|
|
761
|
+
scope: {
|
|
762
|
+
type: "string",
|
|
763
|
+
required: true
|
|
764
|
+
},
|
|
765
|
+
kind: {
|
|
766
|
+
type: "number",
|
|
767
|
+
required: true
|
|
768
|
+
},
|
|
769
|
+
attributes: {
|
|
770
|
+
type: "string",
|
|
771
|
+
// JSON stringified
|
|
772
|
+
required: false,
|
|
773
|
+
// Stringify object on set
|
|
774
|
+
set: (value) => {
|
|
775
|
+
if (value && typeof value !== "string") {
|
|
776
|
+
return JSON.stringify(value);
|
|
777
|
+
}
|
|
778
|
+
return value;
|
|
779
|
+
},
|
|
780
|
+
// Parse JSON string to object on get
|
|
781
|
+
get: (value) => {
|
|
782
|
+
return value ? JSON.parse(value) : value;
|
|
783
|
+
}
|
|
784
|
+
},
|
|
785
|
+
status: {
|
|
786
|
+
type: "string",
|
|
787
|
+
// JSON stringified
|
|
788
|
+
required: false,
|
|
789
|
+
// Stringify object on set
|
|
790
|
+
set: (value) => {
|
|
791
|
+
if (value && typeof value !== "string") {
|
|
792
|
+
return JSON.stringify(value);
|
|
793
|
+
}
|
|
794
|
+
return value;
|
|
795
|
+
},
|
|
796
|
+
// Parse JSON string to object on get
|
|
797
|
+
get: (value) => {
|
|
798
|
+
return value;
|
|
799
|
+
}
|
|
800
|
+
},
|
|
801
|
+
events: {
|
|
802
|
+
type: "string",
|
|
803
|
+
// JSON stringified
|
|
804
|
+
required: false,
|
|
805
|
+
// Stringify object on set
|
|
806
|
+
set: (value) => {
|
|
807
|
+
if (value && typeof value !== "string") {
|
|
808
|
+
return JSON.stringify(value);
|
|
809
|
+
}
|
|
810
|
+
return value;
|
|
811
|
+
},
|
|
812
|
+
// Parse JSON string to object on get
|
|
813
|
+
get: (value) => {
|
|
814
|
+
return value;
|
|
815
|
+
}
|
|
816
|
+
},
|
|
817
|
+
links: {
|
|
818
|
+
type: "string",
|
|
819
|
+
// JSON stringified
|
|
820
|
+
required: false,
|
|
821
|
+
// Stringify object on set
|
|
822
|
+
set: (value) => {
|
|
823
|
+
if (value && typeof value !== "string") {
|
|
824
|
+
return JSON.stringify(value);
|
|
825
|
+
}
|
|
826
|
+
return value;
|
|
827
|
+
},
|
|
828
|
+
// Parse JSON string to object on get
|
|
829
|
+
get: (value) => {
|
|
830
|
+
return value;
|
|
831
|
+
}
|
|
832
|
+
},
|
|
833
|
+
other: {
|
|
834
|
+
type: "string",
|
|
835
|
+
required: false
|
|
836
|
+
},
|
|
837
|
+
startTime: {
|
|
838
|
+
type: "number",
|
|
839
|
+
required: true
|
|
840
|
+
},
|
|
841
|
+
endTime: {
|
|
842
|
+
type: "number",
|
|
843
|
+
required: true
|
|
844
|
+
}
|
|
845
|
+
},
|
|
846
|
+
indexes: {
|
|
847
|
+
primary: {
|
|
848
|
+
pk: { field: "pk", composite: ["entity", "id"] },
|
|
849
|
+
sk: { field: "sk", composite: [] }
|
|
850
|
+
},
|
|
851
|
+
byName: {
|
|
852
|
+
index: "gsi1",
|
|
853
|
+
pk: { field: "gsi1pk", composite: ["entity", "name"] },
|
|
854
|
+
sk: { field: "gsi1sk", composite: ["startTime"] }
|
|
855
|
+
},
|
|
856
|
+
byScope: {
|
|
857
|
+
index: "gsi2",
|
|
858
|
+
pk: { field: "gsi2pk", composite: ["entity", "scope"] },
|
|
859
|
+
sk: { field: "gsi2sk", composite: ["startTime"] }
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
});
|
|
863
|
+
var workflowSnapshotEntity = new Entity({
|
|
864
|
+
model: {
|
|
865
|
+
entity: "workflow_snapshot",
|
|
866
|
+
version: "1",
|
|
867
|
+
service: "mastra"
|
|
868
|
+
},
|
|
869
|
+
attributes: {
|
|
870
|
+
entity: {
|
|
871
|
+
type: "string",
|
|
872
|
+
required: true
|
|
873
|
+
},
|
|
874
|
+
...baseAttributes,
|
|
875
|
+
workflow_name: {
|
|
876
|
+
type: "string",
|
|
877
|
+
required: true
|
|
878
|
+
},
|
|
879
|
+
run_id: {
|
|
505
880
|
type: "string",
|
|
506
881
|
required: true
|
|
507
882
|
},
|
|
@@ -514,118 +889,1020 @@ var workflowSnapshotEntity = new Entity({
|
|
|
514
889
|
if (value && typeof value !== "string") {
|
|
515
890
|
return JSON.stringify(value);
|
|
516
891
|
}
|
|
517
|
-
return value;
|
|
518
|
-
},
|
|
519
|
-
// Parse JSON string to object on get
|
|
520
|
-
get: (value) => {
|
|
521
|
-
return value ? JSON.parse(value) : value;
|
|
892
|
+
return value;
|
|
893
|
+
},
|
|
894
|
+
// Parse JSON string to object on get
|
|
895
|
+
get: (value) => {
|
|
896
|
+
return value ? JSON.parse(value) : value;
|
|
897
|
+
}
|
|
898
|
+
},
|
|
899
|
+
resourceId: {
|
|
900
|
+
type: "string",
|
|
901
|
+
required: false
|
|
902
|
+
}
|
|
903
|
+
},
|
|
904
|
+
indexes: {
|
|
905
|
+
primary: {
|
|
906
|
+
pk: { field: "pk", composite: ["entity", "workflow_name"] },
|
|
907
|
+
sk: { field: "sk", composite: ["run_id"] }
|
|
908
|
+
},
|
|
909
|
+
// GSI to allow querying by run_id efficiently without knowing the workflow_name
|
|
910
|
+
gsi2: {
|
|
911
|
+
index: "gsi2",
|
|
912
|
+
pk: { field: "gsi2pk", composite: ["entity", "run_id"] },
|
|
913
|
+
sk: { field: "gsi2sk", composite: ["workflow_name"] }
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
// src/entities/index.ts
|
|
919
|
+
function getElectroDbService(client, tableName) {
|
|
920
|
+
return new Service(
|
|
921
|
+
{
|
|
922
|
+
thread: threadEntity,
|
|
923
|
+
message: messageEntity,
|
|
924
|
+
eval: evalEntity,
|
|
925
|
+
trace: traceEntity,
|
|
926
|
+
workflow_snapshot: workflowSnapshotEntity,
|
|
927
|
+
resource: resourceEntity,
|
|
928
|
+
score: scoreEntity
|
|
929
|
+
},
|
|
930
|
+
{
|
|
931
|
+
client,
|
|
932
|
+
table: tableName
|
|
933
|
+
}
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
var LegacyEvalsDynamoDB = class extends LegacyEvalsStorage {
|
|
937
|
+
service;
|
|
938
|
+
tableName;
|
|
939
|
+
constructor({ service, tableName }) {
|
|
940
|
+
super();
|
|
941
|
+
this.service = service;
|
|
942
|
+
this.tableName = tableName;
|
|
943
|
+
}
|
|
944
|
+
// Eval operations
|
|
945
|
+
async getEvalsByAgentName(agentName, type) {
|
|
946
|
+
this.logger.debug("Getting evals for agent", { agentName, type });
|
|
947
|
+
try {
|
|
948
|
+
const query = this.service.entities.eval.query.byAgent({ entity: "eval", agent_name: agentName });
|
|
949
|
+
const results = await query.go({ order: "desc", limit: 100 });
|
|
950
|
+
if (!results.data.length) {
|
|
951
|
+
return [];
|
|
952
|
+
}
|
|
953
|
+
let filteredData = results.data;
|
|
954
|
+
if (type) {
|
|
955
|
+
filteredData = filteredData.filter((evalRecord) => {
|
|
956
|
+
try {
|
|
957
|
+
const testInfo = evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0;
|
|
958
|
+
if (type === "test" && !testInfo) {
|
|
959
|
+
return false;
|
|
960
|
+
}
|
|
961
|
+
if (type === "live" && testInfo) {
|
|
962
|
+
return false;
|
|
963
|
+
}
|
|
964
|
+
} catch (e) {
|
|
965
|
+
this.logger.warn("Failed to parse test_info during filtering", { record: evalRecord, error: e });
|
|
966
|
+
}
|
|
967
|
+
return true;
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
return filteredData.map((evalRecord) => {
|
|
971
|
+
try {
|
|
972
|
+
return {
|
|
973
|
+
input: evalRecord.input,
|
|
974
|
+
output: evalRecord.output,
|
|
975
|
+
// Safely parse result and test_info
|
|
976
|
+
result: evalRecord.result && typeof evalRecord.result === "string" ? JSON.parse(evalRecord.result) : void 0,
|
|
977
|
+
agentName: evalRecord.agent_name,
|
|
978
|
+
createdAt: evalRecord.created_at,
|
|
979
|
+
// Keep as string from DDB?
|
|
980
|
+
metricName: evalRecord.metric_name,
|
|
981
|
+
instructions: evalRecord.instructions,
|
|
982
|
+
runId: evalRecord.run_id,
|
|
983
|
+
globalRunId: evalRecord.global_run_id,
|
|
984
|
+
testInfo: evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0
|
|
985
|
+
};
|
|
986
|
+
} catch (parseError) {
|
|
987
|
+
this.logger.error("Failed to parse eval record", { record: evalRecord, error: parseError });
|
|
988
|
+
return {
|
|
989
|
+
agentName: evalRecord.agent_name,
|
|
990
|
+
createdAt: evalRecord.created_at,
|
|
991
|
+
runId: evalRecord.run_id,
|
|
992
|
+
globalRunId: evalRecord.global_run_id
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
} catch (error) {
|
|
997
|
+
throw new MastraError(
|
|
998
|
+
{
|
|
999
|
+
id: "STORAGE_DYNAMODB_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
1000
|
+
domain: ErrorDomain.STORAGE,
|
|
1001
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1002
|
+
details: { agentName }
|
|
1003
|
+
},
|
|
1004
|
+
error
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
async getEvals(options = {}) {
|
|
1009
|
+
const { agentName, type, page = 0, perPage = 100, dateRange } = options;
|
|
1010
|
+
this.logger.debug("Getting evals with pagination", { agentName, type, page, perPage, dateRange });
|
|
1011
|
+
try {
|
|
1012
|
+
let query;
|
|
1013
|
+
if (agentName) {
|
|
1014
|
+
query = this.service.entities.eval.query.byAgent({ entity: "eval", agent_name: agentName });
|
|
1015
|
+
} else {
|
|
1016
|
+
query = this.service.entities.eval.query.byEntity({ entity: "eval" });
|
|
1017
|
+
}
|
|
1018
|
+
const results = await query.go({
|
|
1019
|
+
order: "desc",
|
|
1020
|
+
pages: "all"
|
|
1021
|
+
// Get all pages to apply filtering and pagination
|
|
1022
|
+
});
|
|
1023
|
+
if (!results.data.length) {
|
|
1024
|
+
return {
|
|
1025
|
+
evals: [],
|
|
1026
|
+
total: 0,
|
|
1027
|
+
page,
|
|
1028
|
+
perPage,
|
|
1029
|
+
hasMore: false
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
let filteredData = results.data;
|
|
1033
|
+
if (type) {
|
|
1034
|
+
filteredData = filteredData.filter((evalRecord) => {
|
|
1035
|
+
try {
|
|
1036
|
+
const testInfo = evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0;
|
|
1037
|
+
if (type === "test" && !testInfo) {
|
|
1038
|
+
return false;
|
|
1039
|
+
}
|
|
1040
|
+
if (type === "live" && testInfo) {
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
} catch (e) {
|
|
1044
|
+
this.logger.warn("Failed to parse test_info during filtering", { record: evalRecord, error: e });
|
|
1045
|
+
}
|
|
1046
|
+
return true;
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
if (dateRange) {
|
|
1050
|
+
const fromDate = dateRange.start;
|
|
1051
|
+
const toDate = dateRange.end;
|
|
1052
|
+
filteredData = filteredData.filter((evalRecord) => {
|
|
1053
|
+
const recordDate = new Date(evalRecord.created_at);
|
|
1054
|
+
if (fromDate && recordDate < fromDate) {
|
|
1055
|
+
return false;
|
|
1056
|
+
}
|
|
1057
|
+
if (toDate && recordDate > toDate) {
|
|
1058
|
+
return false;
|
|
1059
|
+
}
|
|
1060
|
+
return true;
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
const total = filteredData.length;
|
|
1064
|
+
const start = page * perPage;
|
|
1065
|
+
const end = start + perPage;
|
|
1066
|
+
const paginatedData = filteredData.slice(start, end);
|
|
1067
|
+
const evals = paginatedData.map((evalRecord) => {
|
|
1068
|
+
try {
|
|
1069
|
+
return {
|
|
1070
|
+
input: evalRecord.input,
|
|
1071
|
+
output: evalRecord.output,
|
|
1072
|
+
result: evalRecord.result && typeof evalRecord.result === "string" ? JSON.parse(evalRecord.result) : void 0,
|
|
1073
|
+
agentName: evalRecord.agent_name,
|
|
1074
|
+
createdAt: evalRecord.created_at,
|
|
1075
|
+
metricName: evalRecord.metric_name,
|
|
1076
|
+
instructions: evalRecord.instructions,
|
|
1077
|
+
runId: evalRecord.run_id,
|
|
1078
|
+
globalRunId: evalRecord.global_run_id,
|
|
1079
|
+
testInfo: evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0
|
|
1080
|
+
};
|
|
1081
|
+
} catch (parseError) {
|
|
1082
|
+
this.logger.error("Failed to parse eval record", { record: evalRecord, error: parseError });
|
|
1083
|
+
return {
|
|
1084
|
+
agentName: evalRecord.agent_name,
|
|
1085
|
+
createdAt: evalRecord.created_at,
|
|
1086
|
+
runId: evalRecord.run_id,
|
|
1087
|
+
globalRunId: evalRecord.global_run_id
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
const hasMore = end < total;
|
|
1092
|
+
return {
|
|
1093
|
+
evals,
|
|
1094
|
+
total,
|
|
1095
|
+
page,
|
|
1096
|
+
perPage,
|
|
1097
|
+
hasMore
|
|
1098
|
+
};
|
|
1099
|
+
} catch (error) {
|
|
1100
|
+
throw new MastraError(
|
|
1101
|
+
{
|
|
1102
|
+
id: "STORAGE_DYNAMODB_STORE_GET_EVALS_FAILED",
|
|
1103
|
+
domain: ErrorDomain.STORAGE,
|
|
1104
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1105
|
+
details: {
|
|
1106
|
+
agentName: agentName || "all",
|
|
1107
|
+
type: type || "all",
|
|
1108
|
+
page,
|
|
1109
|
+
perPage
|
|
1110
|
+
}
|
|
1111
|
+
},
|
|
1112
|
+
error
|
|
1113
|
+
);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
};
|
|
1117
|
+
var MemoryStorageDynamoDB = class extends MemoryStorage {
|
|
1118
|
+
service;
|
|
1119
|
+
constructor({ service }) {
|
|
1120
|
+
super();
|
|
1121
|
+
this.service = service;
|
|
1122
|
+
}
|
|
1123
|
+
// Helper function to parse message data (handle JSON fields)
|
|
1124
|
+
parseMessageData(data) {
|
|
1125
|
+
return {
|
|
1126
|
+
...data,
|
|
1127
|
+
// Ensure dates are Date objects if needed (ElectroDB might return strings)
|
|
1128
|
+
createdAt: data.createdAt ? new Date(data.createdAt) : void 0,
|
|
1129
|
+
updatedAt: data.updatedAt ? new Date(data.updatedAt) : void 0
|
|
1130
|
+
// Other fields like content, toolCallArgs etc. are assumed to be correctly
|
|
1131
|
+
// transformed by the ElectroDB entity getters.
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
// Helper function to transform and sort threads
|
|
1135
|
+
transformAndSortThreads(rawThreads, orderBy, sortDirection) {
|
|
1136
|
+
return rawThreads.map((data) => ({
|
|
1137
|
+
...data,
|
|
1138
|
+
// Convert date strings back to Date objects for consistency
|
|
1139
|
+
createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
|
|
1140
|
+
updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
|
|
1141
|
+
})).sort((a, b) => {
|
|
1142
|
+
const fieldA = orderBy === "createdAt" ? a.createdAt : a.updatedAt;
|
|
1143
|
+
const fieldB = orderBy === "createdAt" ? b.createdAt : b.updatedAt;
|
|
1144
|
+
const comparison = fieldA.getTime() - fieldB.getTime();
|
|
1145
|
+
return sortDirection === "DESC" ? -comparison : comparison;
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
async getThreadById({ threadId }) {
|
|
1149
|
+
this.logger.debug("Getting thread by ID", { threadId });
|
|
1150
|
+
try {
|
|
1151
|
+
const result = await this.service.entities.thread.get({ entity: "thread", id: threadId }).go();
|
|
1152
|
+
if (!result.data) {
|
|
1153
|
+
return null;
|
|
1154
|
+
}
|
|
1155
|
+
const data = result.data;
|
|
1156
|
+
return {
|
|
1157
|
+
...data,
|
|
1158
|
+
// Convert date strings back to Date objects for consistency
|
|
1159
|
+
createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
|
|
1160
|
+
updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
|
|
1161
|
+
// metadata: data.metadata ? JSON.parse(data.metadata) : undefined, // REMOVED by AI
|
|
1162
|
+
// metadata is already transformed by the entity's getter
|
|
1163
|
+
};
|
|
1164
|
+
} catch (error) {
|
|
1165
|
+
throw new MastraError(
|
|
1166
|
+
{
|
|
1167
|
+
id: "STORAGE_DYNAMODB_STORE_GET_THREAD_BY_ID_FAILED",
|
|
1168
|
+
domain: ErrorDomain.STORAGE,
|
|
1169
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1170
|
+
details: { threadId }
|
|
1171
|
+
},
|
|
1172
|
+
error
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* @deprecated use getThreadsByResourceIdPaginated instead for paginated results.
|
|
1178
|
+
*/
|
|
1179
|
+
async getThreadsByResourceId(args) {
|
|
1180
|
+
const resourceId = args.resourceId;
|
|
1181
|
+
const orderBy = this.castThreadOrderBy(args.orderBy);
|
|
1182
|
+
const sortDirection = this.castThreadSortDirection(args.sortDirection);
|
|
1183
|
+
this.logger.debug("Getting threads by resource ID", { resourceId, orderBy, sortDirection });
|
|
1184
|
+
try {
|
|
1185
|
+
const result = await this.service.entities.thread.query.byResource({ entity: "thread", resourceId }).go();
|
|
1186
|
+
if (!result.data.length) {
|
|
1187
|
+
return [];
|
|
1188
|
+
}
|
|
1189
|
+
return this.transformAndSortThreads(result.data, orderBy, sortDirection);
|
|
1190
|
+
} catch (error) {
|
|
1191
|
+
throw new MastraError(
|
|
1192
|
+
{
|
|
1193
|
+
id: "STORAGE_DYNAMODB_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
1194
|
+
domain: ErrorDomain.STORAGE,
|
|
1195
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1196
|
+
details: { resourceId }
|
|
1197
|
+
},
|
|
1198
|
+
error
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
async saveThread({ thread }) {
|
|
1203
|
+
this.logger.debug("Saving thread", { threadId: thread.id });
|
|
1204
|
+
const now = /* @__PURE__ */ new Date();
|
|
1205
|
+
const threadData = {
|
|
1206
|
+
entity: "thread",
|
|
1207
|
+
id: thread.id,
|
|
1208
|
+
resourceId: thread.resourceId,
|
|
1209
|
+
title: thread.title || `Thread ${thread.id}`,
|
|
1210
|
+
createdAt: thread.createdAt?.toISOString() || now.toISOString(),
|
|
1211
|
+
updatedAt: thread.updatedAt?.toISOString() || now.toISOString(),
|
|
1212
|
+
metadata: thread.metadata ? JSON.stringify(thread.metadata) : void 0
|
|
1213
|
+
};
|
|
1214
|
+
try {
|
|
1215
|
+
await this.service.entities.thread.upsert(threadData).go();
|
|
1216
|
+
return {
|
|
1217
|
+
id: thread.id,
|
|
1218
|
+
resourceId: thread.resourceId,
|
|
1219
|
+
title: threadData.title,
|
|
1220
|
+
createdAt: thread.createdAt || now,
|
|
1221
|
+
updatedAt: now,
|
|
1222
|
+
metadata: thread.metadata
|
|
1223
|
+
};
|
|
1224
|
+
} catch (error) {
|
|
1225
|
+
throw new MastraError(
|
|
1226
|
+
{
|
|
1227
|
+
id: "STORAGE_DYNAMODB_STORE_SAVE_THREAD_FAILED",
|
|
1228
|
+
domain: ErrorDomain.STORAGE,
|
|
1229
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1230
|
+
details: { threadId: thread.id }
|
|
1231
|
+
},
|
|
1232
|
+
error
|
|
1233
|
+
);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
async updateThread({
|
|
1237
|
+
id,
|
|
1238
|
+
title,
|
|
1239
|
+
metadata
|
|
1240
|
+
}) {
|
|
1241
|
+
this.logger.debug("Updating thread", { threadId: id });
|
|
1242
|
+
try {
|
|
1243
|
+
const existingThread = await this.getThreadById({ threadId: id });
|
|
1244
|
+
if (!existingThread) {
|
|
1245
|
+
throw new Error(`Thread not found: ${id}`);
|
|
1246
|
+
}
|
|
1247
|
+
const now = /* @__PURE__ */ new Date();
|
|
1248
|
+
const updateData = {
|
|
1249
|
+
updatedAt: now.toISOString()
|
|
1250
|
+
};
|
|
1251
|
+
if (title) {
|
|
1252
|
+
updateData.title = title;
|
|
1253
|
+
}
|
|
1254
|
+
if (metadata) {
|
|
1255
|
+
const existingMetadata = existingThread.metadata ? typeof existingThread.metadata === "string" ? JSON.parse(existingThread.metadata) : existingThread.metadata : {};
|
|
1256
|
+
const mergedMetadata = { ...existingMetadata, ...metadata };
|
|
1257
|
+
updateData.metadata = JSON.stringify(mergedMetadata);
|
|
1258
|
+
}
|
|
1259
|
+
await this.service.entities.thread.update({ entity: "thread", id }).set(updateData).go();
|
|
1260
|
+
return {
|
|
1261
|
+
...existingThread,
|
|
1262
|
+
title: title || existingThread.title,
|
|
1263
|
+
metadata: metadata ? { ...existingThread.metadata, ...metadata } : existingThread.metadata,
|
|
1264
|
+
updatedAt: now
|
|
1265
|
+
};
|
|
1266
|
+
} catch (error) {
|
|
1267
|
+
throw new MastraError(
|
|
1268
|
+
{
|
|
1269
|
+
id: "STORAGE_DYNAMODB_STORE_UPDATE_THREAD_FAILED",
|
|
1270
|
+
domain: ErrorDomain.STORAGE,
|
|
1271
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1272
|
+
details: { threadId: id }
|
|
1273
|
+
},
|
|
1274
|
+
error
|
|
1275
|
+
);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
async deleteThread({ threadId }) {
|
|
1279
|
+
this.logger.debug("Deleting thread", { threadId });
|
|
1280
|
+
try {
|
|
1281
|
+
const messages = await this.getMessages({ threadId });
|
|
1282
|
+
if (messages.length > 0) {
|
|
1283
|
+
const batchSize = 25;
|
|
1284
|
+
for (let i = 0; i < messages.length; i += batchSize) {
|
|
1285
|
+
const batch = messages.slice(i, i + batchSize);
|
|
1286
|
+
await Promise.all(
|
|
1287
|
+
batch.map(
|
|
1288
|
+
(message) => this.service.entities.message.delete({
|
|
1289
|
+
entity: "message",
|
|
1290
|
+
id: message.id,
|
|
1291
|
+
threadId: message.threadId
|
|
1292
|
+
}).go()
|
|
1293
|
+
)
|
|
1294
|
+
);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
await this.service.entities.thread.delete({ entity: "thread", id: threadId }).go();
|
|
1298
|
+
} catch (error) {
|
|
1299
|
+
throw new MastraError(
|
|
1300
|
+
{
|
|
1301
|
+
id: "STORAGE_DYNAMODB_STORE_DELETE_THREAD_FAILED",
|
|
1302
|
+
domain: ErrorDomain.STORAGE,
|
|
1303
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1304
|
+
details: { threadId }
|
|
1305
|
+
},
|
|
1306
|
+
error
|
|
1307
|
+
);
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
async getMessages({
|
|
1311
|
+
threadId,
|
|
1312
|
+
resourceId,
|
|
1313
|
+
selectBy,
|
|
1314
|
+
format
|
|
1315
|
+
}) {
|
|
1316
|
+
this.logger.debug("Getting messages", { threadId, selectBy });
|
|
1317
|
+
try {
|
|
1318
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
1319
|
+
const messages = [];
|
|
1320
|
+
const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
1321
|
+
if (selectBy?.include?.length) {
|
|
1322
|
+
const includeMessages = await this._getIncludedMessages(threadId, selectBy);
|
|
1323
|
+
if (includeMessages) {
|
|
1324
|
+
messages.push(...includeMessages);
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
if (limit !== 0) {
|
|
1328
|
+
const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
|
|
1329
|
+
let results;
|
|
1330
|
+
if (limit !== Number.MAX_SAFE_INTEGER && limit > 0) {
|
|
1331
|
+
results = await query.go({ limit, order: "desc" });
|
|
1332
|
+
results.data = results.data.reverse();
|
|
1333
|
+
} else {
|
|
1334
|
+
results = await query.go();
|
|
1335
|
+
}
|
|
1336
|
+
let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg);
|
|
1337
|
+
allThreadMessages.sort((a, b) => {
|
|
1338
|
+
const timeA = a.createdAt.getTime();
|
|
1339
|
+
const timeB = b.createdAt.getTime();
|
|
1340
|
+
if (timeA === timeB) {
|
|
1341
|
+
return a.id.localeCompare(b.id);
|
|
1342
|
+
}
|
|
1343
|
+
return timeA - timeB;
|
|
1344
|
+
});
|
|
1345
|
+
messages.push(...allThreadMessages);
|
|
1346
|
+
}
|
|
1347
|
+
messages.sort((a, b) => {
|
|
1348
|
+
const timeA = a.createdAt.getTime();
|
|
1349
|
+
const timeB = b.createdAt.getTime();
|
|
1350
|
+
if (timeA === timeB) {
|
|
1351
|
+
return a.id.localeCompare(b.id);
|
|
1352
|
+
}
|
|
1353
|
+
return timeA - timeB;
|
|
1354
|
+
});
|
|
1355
|
+
const uniqueMessages = messages.filter(
|
|
1356
|
+
(message, index, self) => index === self.findIndex((m) => m.id === message.id)
|
|
1357
|
+
);
|
|
1358
|
+
const list = new MessageList({ threadId, resourceId }).add(uniqueMessages, "memory");
|
|
1359
|
+
if (format === `v2`) return list.get.all.v2();
|
|
1360
|
+
return list.get.all.v1();
|
|
1361
|
+
} catch (error) {
|
|
1362
|
+
throw new MastraError(
|
|
1363
|
+
{
|
|
1364
|
+
id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_FAILED",
|
|
1365
|
+
domain: ErrorDomain.STORAGE,
|
|
1366
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1367
|
+
details: { threadId, resourceId: resourceId ?? "" }
|
|
1368
|
+
},
|
|
1369
|
+
error
|
|
1370
|
+
);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
async getMessagesById({
|
|
1374
|
+
messageIds,
|
|
1375
|
+
format
|
|
1376
|
+
}) {
|
|
1377
|
+
this.logger.debug("Getting messages by ID", { messageIds });
|
|
1378
|
+
if (messageIds.length === 0) return [];
|
|
1379
|
+
try {
|
|
1380
|
+
const results = await Promise.all(
|
|
1381
|
+
messageIds.map((id) => this.service.entities.message.query.primary({ entity: "message", id }).go())
|
|
1382
|
+
);
|
|
1383
|
+
const data = results.map((result) => result.data).flat(1);
|
|
1384
|
+
let parsedMessages = data.map((data2) => this.parseMessageData(data2)).filter((msg) => "content" in msg);
|
|
1385
|
+
const uniqueMessages = parsedMessages.filter(
|
|
1386
|
+
(message, index, self) => index === self.findIndex((m) => m.id === message.id)
|
|
1387
|
+
);
|
|
1388
|
+
const list = new MessageList().add(uniqueMessages, "memory");
|
|
1389
|
+
if (format === `v1`) return list.get.all.v1();
|
|
1390
|
+
return list.get.all.v2();
|
|
1391
|
+
} catch (error) {
|
|
1392
|
+
throw new MastraError(
|
|
1393
|
+
{
|
|
1394
|
+
id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_BY_ID_FAILED",
|
|
1395
|
+
domain: ErrorDomain.STORAGE,
|
|
1396
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1397
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
1398
|
+
},
|
|
1399
|
+
error
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
async saveMessages(args) {
|
|
1404
|
+
const { messages, format = "v1" } = args;
|
|
1405
|
+
this.logger.debug("Saving messages", { count: messages.length });
|
|
1406
|
+
if (!messages.length) {
|
|
1407
|
+
return [];
|
|
1408
|
+
}
|
|
1409
|
+
const threadId = messages[0]?.threadId;
|
|
1410
|
+
if (!threadId) {
|
|
1411
|
+
throw new Error("Thread ID is required");
|
|
1412
|
+
}
|
|
1413
|
+
const messagesToSave = messages.map((msg) => {
|
|
1414
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1415
|
+
return {
|
|
1416
|
+
entity: "message",
|
|
1417
|
+
// Add entity type
|
|
1418
|
+
id: msg.id,
|
|
1419
|
+
threadId: msg.threadId,
|
|
1420
|
+
role: msg.role,
|
|
1421
|
+
type: msg.type,
|
|
1422
|
+
resourceId: msg.resourceId,
|
|
1423
|
+
// Ensure complex fields are stringified if not handled by attribute setters
|
|
1424
|
+
content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content),
|
|
1425
|
+
toolCallArgs: `toolCallArgs` in msg && msg.toolCallArgs ? JSON.stringify(msg.toolCallArgs) : void 0,
|
|
1426
|
+
toolCallIds: `toolCallIds` in msg && msg.toolCallIds ? JSON.stringify(msg.toolCallIds) : void 0,
|
|
1427
|
+
toolNames: `toolNames` in msg && msg.toolNames ? JSON.stringify(msg.toolNames) : void 0,
|
|
1428
|
+
createdAt: msg.createdAt instanceof Date ? msg.createdAt.toISOString() : msg.createdAt || now,
|
|
1429
|
+
updatedAt: now
|
|
1430
|
+
// Add updatedAt
|
|
1431
|
+
};
|
|
1432
|
+
});
|
|
1433
|
+
try {
|
|
1434
|
+
const savedMessageIds = [];
|
|
1435
|
+
for (const messageData of messagesToSave) {
|
|
1436
|
+
if (!messageData.entity) {
|
|
1437
|
+
this.logger.error("Missing entity property in message data for create", { messageData });
|
|
1438
|
+
throw new Error("Internal error: Missing entity property during saveMessages");
|
|
1439
|
+
}
|
|
1440
|
+
try {
|
|
1441
|
+
await this.service.entities.message.put(messageData).go();
|
|
1442
|
+
savedMessageIds.push(messageData.id);
|
|
1443
|
+
} catch (error) {
|
|
1444
|
+
for (const savedId of savedMessageIds) {
|
|
1445
|
+
try {
|
|
1446
|
+
await this.service.entities.message.delete({ entity: "message", id: savedId }).go();
|
|
1447
|
+
} catch (rollbackError) {
|
|
1448
|
+
this.logger.error("Failed to rollback message during save error", {
|
|
1449
|
+
messageId: savedId,
|
|
1450
|
+
error: rollbackError
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
throw error;
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
await this.service.entities.thread.update({ entity: "thread", id: threadId }).set({
|
|
1458
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1459
|
+
}).go();
|
|
1460
|
+
const list = new MessageList().add(messages, "memory");
|
|
1461
|
+
if (format === `v1`) return list.get.all.v1();
|
|
1462
|
+
return list.get.all.v2();
|
|
1463
|
+
} catch (error) {
|
|
1464
|
+
throw new MastraError(
|
|
1465
|
+
{
|
|
1466
|
+
id: "STORAGE_DYNAMODB_STORE_SAVE_MESSAGES_FAILED",
|
|
1467
|
+
domain: ErrorDomain.STORAGE,
|
|
1468
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1469
|
+
details: { count: messages.length }
|
|
1470
|
+
},
|
|
1471
|
+
error
|
|
1472
|
+
);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
1476
|
+
const { resourceId, page = 0, perPage = 100 } = args;
|
|
1477
|
+
const orderBy = this.castThreadOrderBy(args.orderBy);
|
|
1478
|
+
const sortDirection = this.castThreadSortDirection(args.sortDirection);
|
|
1479
|
+
this.logger.debug("Getting threads by resource ID with pagination", {
|
|
1480
|
+
resourceId,
|
|
1481
|
+
page,
|
|
1482
|
+
perPage,
|
|
1483
|
+
orderBy,
|
|
1484
|
+
sortDirection
|
|
1485
|
+
});
|
|
1486
|
+
try {
|
|
1487
|
+
const query = this.service.entities.thread.query.byResource({ entity: "thread", resourceId });
|
|
1488
|
+
const results = await query.go();
|
|
1489
|
+
const allThreads = this.transformAndSortThreads(results.data, orderBy, sortDirection);
|
|
1490
|
+
const startIndex = page * perPage;
|
|
1491
|
+
const endIndex = startIndex + perPage;
|
|
1492
|
+
const paginatedThreads = allThreads.slice(startIndex, endIndex);
|
|
1493
|
+
const total = allThreads.length;
|
|
1494
|
+
const hasMore = endIndex < total;
|
|
1495
|
+
return {
|
|
1496
|
+
threads: paginatedThreads,
|
|
1497
|
+
total,
|
|
1498
|
+
page,
|
|
1499
|
+
perPage,
|
|
1500
|
+
hasMore
|
|
1501
|
+
};
|
|
1502
|
+
} catch (error) {
|
|
1503
|
+
throw new MastraError(
|
|
1504
|
+
{
|
|
1505
|
+
id: "STORAGE_DYNAMODB_STORE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
|
|
1506
|
+
domain: ErrorDomain.STORAGE,
|
|
1507
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1508
|
+
details: { resourceId, page, perPage }
|
|
1509
|
+
},
|
|
1510
|
+
error
|
|
1511
|
+
);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
async getMessagesPaginated(args) {
|
|
1515
|
+
const { threadId, resourceId, selectBy, format = "v1" } = args;
|
|
1516
|
+
const { page = 0, perPage = 40, dateRange } = selectBy?.pagination || {};
|
|
1517
|
+
const fromDate = dateRange?.start;
|
|
1518
|
+
const toDate = dateRange?.end;
|
|
1519
|
+
const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
1520
|
+
this.logger.debug("Getting messages with pagination", { threadId, page, perPage, fromDate, toDate, limit });
|
|
1521
|
+
try {
|
|
1522
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
1523
|
+
let messages = [];
|
|
1524
|
+
if (selectBy?.include?.length) {
|
|
1525
|
+
const includeMessages = await this._getIncludedMessages(threadId, selectBy);
|
|
1526
|
+
if (includeMessages) {
|
|
1527
|
+
messages.push(...includeMessages);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
if (limit !== 0) {
|
|
1531
|
+
const query = this.service.entities.message.query.byThread({ entity: "message", threadId });
|
|
1532
|
+
let results;
|
|
1533
|
+
if (limit !== Number.MAX_SAFE_INTEGER && limit > 0) {
|
|
1534
|
+
results = await query.go({ limit, order: "desc" });
|
|
1535
|
+
results.data = results.data.reverse();
|
|
1536
|
+
} else {
|
|
1537
|
+
results = await query.go();
|
|
1538
|
+
}
|
|
1539
|
+
let allThreadMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg);
|
|
1540
|
+
allThreadMessages.sort((a, b) => {
|
|
1541
|
+
const timeA = a.createdAt.getTime();
|
|
1542
|
+
const timeB = b.createdAt.getTime();
|
|
1543
|
+
if (timeA === timeB) {
|
|
1544
|
+
return a.id.localeCompare(b.id);
|
|
1545
|
+
}
|
|
1546
|
+
return timeA - timeB;
|
|
1547
|
+
});
|
|
1548
|
+
const excludeIds = messages.map((m) => m.id);
|
|
1549
|
+
if (excludeIds.length > 0) {
|
|
1550
|
+
allThreadMessages = allThreadMessages.filter((msg) => !excludeIds.includes(msg.id));
|
|
1551
|
+
}
|
|
1552
|
+
messages.push(...allThreadMessages);
|
|
1553
|
+
}
|
|
1554
|
+
messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
1555
|
+
if (fromDate || toDate) {
|
|
1556
|
+
messages = messages.filter((msg) => {
|
|
1557
|
+
const createdAt = new Date(msg.createdAt).getTime();
|
|
1558
|
+
if (fromDate && createdAt < new Date(fromDate).getTime()) return false;
|
|
1559
|
+
if (toDate && createdAt > new Date(toDate).getTime()) return false;
|
|
1560
|
+
return true;
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
const total = messages.length;
|
|
1564
|
+
const start = page * perPage;
|
|
1565
|
+
const end = start + perPage;
|
|
1566
|
+
const paginatedMessages = messages.slice(start, end);
|
|
1567
|
+
const hasMore = end < total;
|
|
1568
|
+
const list = new MessageList({ threadId, resourceId }).add(paginatedMessages, "memory");
|
|
1569
|
+
const finalMessages = format === "v2" ? list.get.all.v2() : list.get.all.v1();
|
|
1570
|
+
return {
|
|
1571
|
+
messages: finalMessages,
|
|
1572
|
+
total,
|
|
1573
|
+
page,
|
|
1574
|
+
perPage,
|
|
1575
|
+
hasMore
|
|
1576
|
+
};
|
|
1577
|
+
} catch (error) {
|
|
1578
|
+
const mastraError = new MastraError(
|
|
1579
|
+
{
|
|
1580
|
+
id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_PAGINATED_FAILED",
|
|
1581
|
+
domain: ErrorDomain.STORAGE,
|
|
1582
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1583
|
+
details: { threadId, resourceId: resourceId ?? "" }
|
|
1584
|
+
},
|
|
1585
|
+
error
|
|
1586
|
+
);
|
|
1587
|
+
this.logger?.trackException?.(mastraError);
|
|
1588
|
+
this.logger?.error?.(mastraError.toString());
|
|
1589
|
+
return { messages: [], total: 0, page, perPage, hasMore: false };
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
// Helper method to get included messages with context
|
|
1593
|
+
async _getIncludedMessages(threadId, selectBy) {
|
|
1594
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
1595
|
+
if (!selectBy?.include?.length) {
|
|
1596
|
+
return [];
|
|
1597
|
+
}
|
|
1598
|
+
const includeMessages = [];
|
|
1599
|
+
for (const includeItem of selectBy.include) {
|
|
1600
|
+
try {
|
|
1601
|
+
const { id, threadId: targetThreadId, withPreviousMessages = 0, withNextMessages = 0 } = includeItem;
|
|
1602
|
+
const searchThreadId = targetThreadId || threadId;
|
|
1603
|
+
this.logger.debug("Getting included messages for", {
|
|
1604
|
+
id,
|
|
1605
|
+
targetThreadId,
|
|
1606
|
+
searchThreadId,
|
|
1607
|
+
withPreviousMessages,
|
|
1608
|
+
withNextMessages
|
|
1609
|
+
});
|
|
1610
|
+
const query = this.service.entities.message.query.byThread({ entity: "message", threadId: searchThreadId });
|
|
1611
|
+
const results = await query.go();
|
|
1612
|
+
const allMessages = results.data.map((data) => this.parseMessageData(data)).filter((msg) => "content" in msg && typeof msg.content === "object");
|
|
1613
|
+
this.logger.debug("Found messages in thread", {
|
|
1614
|
+
threadId: searchThreadId,
|
|
1615
|
+
messageCount: allMessages.length,
|
|
1616
|
+
messageIds: allMessages.map((m) => m.id)
|
|
1617
|
+
});
|
|
1618
|
+
allMessages.sort((a, b) => {
|
|
1619
|
+
const timeA = a.createdAt.getTime();
|
|
1620
|
+
const timeB = b.createdAt.getTime();
|
|
1621
|
+
if (timeA === timeB) {
|
|
1622
|
+
return a.id.localeCompare(b.id);
|
|
1623
|
+
}
|
|
1624
|
+
return timeA - timeB;
|
|
1625
|
+
});
|
|
1626
|
+
const targetIndex = allMessages.findIndex((msg) => msg.id === id);
|
|
1627
|
+
if (targetIndex === -1) {
|
|
1628
|
+
this.logger.warn("Target message not found", { id, threadId: searchThreadId });
|
|
1629
|
+
continue;
|
|
1630
|
+
}
|
|
1631
|
+
this.logger.debug("Found target message at index", { id, targetIndex, totalMessages: allMessages.length });
|
|
1632
|
+
const startIndex = Math.max(0, targetIndex - withPreviousMessages);
|
|
1633
|
+
const endIndex = Math.min(allMessages.length, targetIndex + withNextMessages + 1);
|
|
1634
|
+
const contextMessages = allMessages.slice(startIndex, endIndex);
|
|
1635
|
+
this.logger.debug("Context messages", {
|
|
1636
|
+
startIndex,
|
|
1637
|
+
endIndex,
|
|
1638
|
+
contextCount: contextMessages.length,
|
|
1639
|
+
contextIds: contextMessages.map((m) => m.id)
|
|
1640
|
+
});
|
|
1641
|
+
includeMessages.push(...contextMessages);
|
|
1642
|
+
} catch (error) {
|
|
1643
|
+
this.logger.warn("Failed to get included message", { messageId: includeItem.id, error });
|
|
522
1644
|
}
|
|
523
|
-
},
|
|
524
|
-
resourceId: {
|
|
525
|
-
type: "string",
|
|
526
|
-
required: false
|
|
527
|
-
}
|
|
528
|
-
},
|
|
529
|
-
indexes: {
|
|
530
|
-
primary: {
|
|
531
|
-
pk: { field: "pk", composite: ["entity", "workflow_name"] },
|
|
532
|
-
sk: { field: "sk", composite: ["run_id"] }
|
|
533
|
-
},
|
|
534
|
-
// GSI to allow querying by run_id efficiently without knowing the workflow_name
|
|
535
|
-
gsi2: {
|
|
536
|
-
index: "gsi2",
|
|
537
|
-
pk: { field: "gsi2pk", composite: ["entity", "run_id"] },
|
|
538
|
-
sk: { field: "gsi2sk", composite: ["workflow_name"] }
|
|
539
1645
|
}
|
|
1646
|
+
this.logger.debug("Total included messages", {
|
|
1647
|
+
count: includeMessages.length,
|
|
1648
|
+
ids: includeMessages.map((m) => m.id)
|
|
1649
|
+
});
|
|
1650
|
+
return includeMessages;
|
|
540
1651
|
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
{
|
|
547
|
-
thread: threadEntity,
|
|
548
|
-
message: messageEntity,
|
|
549
|
-
eval: evalEntity,
|
|
550
|
-
trace: traceEntity,
|
|
551
|
-
workflowSnapshot: workflowSnapshotEntity
|
|
552
|
-
},
|
|
553
|
-
{
|
|
554
|
-
client,
|
|
555
|
-
table: tableName
|
|
1652
|
+
async updateMessages(args) {
|
|
1653
|
+
const { messages } = args;
|
|
1654
|
+
this.logger.debug("Updating messages", { count: messages.length });
|
|
1655
|
+
if (!messages.length) {
|
|
1656
|
+
return [];
|
|
556
1657
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
// src/storage/index.ts
|
|
561
|
-
var DynamoDBStore = class extends MastraStorage {
|
|
562
|
-
tableName;
|
|
563
|
-
client;
|
|
564
|
-
service;
|
|
565
|
-
hasInitialized = null;
|
|
566
|
-
constructor({ name, config }) {
|
|
567
|
-
super({ name });
|
|
1658
|
+
const updatedMessages = [];
|
|
1659
|
+
const affectedThreadIds = /* @__PURE__ */ new Set();
|
|
568
1660
|
try {
|
|
569
|
-
|
|
570
|
-
|
|
1661
|
+
for (const updateData of messages) {
|
|
1662
|
+
const { id, ...updates } = updateData;
|
|
1663
|
+
const existingMessage = await this.service.entities.message.get({ entity: "message", id }).go();
|
|
1664
|
+
if (!existingMessage.data) {
|
|
1665
|
+
this.logger.warn("Message not found for update", { id });
|
|
1666
|
+
continue;
|
|
1667
|
+
}
|
|
1668
|
+
const existingMsg = this.parseMessageData(existingMessage.data);
|
|
1669
|
+
const originalThreadId = existingMsg.threadId;
|
|
1670
|
+
affectedThreadIds.add(originalThreadId);
|
|
1671
|
+
const updatePayload = {
|
|
1672
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1673
|
+
};
|
|
1674
|
+
if ("role" in updates && updates.role !== void 0) updatePayload.role = updates.role;
|
|
1675
|
+
if ("type" in updates && updates.type !== void 0) updatePayload.type = updates.type;
|
|
1676
|
+
if ("resourceId" in updates && updates.resourceId !== void 0) updatePayload.resourceId = updates.resourceId;
|
|
1677
|
+
if ("threadId" in updates && updates.threadId !== void 0 && updates.threadId !== null) {
|
|
1678
|
+
updatePayload.threadId = updates.threadId;
|
|
1679
|
+
affectedThreadIds.add(updates.threadId);
|
|
1680
|
+
}
|
|
1681
|
+
if (updates.content) {
|
|
1682
|
+
const existingContent = existingMsg.content;
|
|
1683
|
+
let newContent = { ...existingContent };
|
|
1684
|
+
if (updates.content.metadata !== void 0) {
|
|
1685
|
+
newContent.metadata = {
|
|
1686
|
+
...existingContent.metadata || {},
|
|
1687
|
+
...updates.content.metadata || {}
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1690
|
+
if (updates.content.content !== void 0) {
|
|
1691
|
+
newContent.content = updates.content.content;
|
|
1692
|
+
}
|
|
1693
|
+
if ("parts" in updates.content && updates.content.parts !== void 0) {
|
|
1694
|
+
newContent.parts = updates.content.parts;
|
|
1695
|
+
}
|
|
1696
|
+
updatePayload.content = JSON.stringify(newContent);
|
|
1697
|
+
}
|
|
1698
|
+
await this.service.entities.message.update({ entity: "message", id }).set(updatePayload).go();
|
|
1699
|
+
const updatedMessage = await this.service.entities.message.get({ entity: "message", id }).go();
|
|
1700
|
+
if (updatedMessage.data) {
|
|
1701
|
+
updatedMessages.push(this.parseMessageData(updatedMessage.data));
|
|
1702
|
+
}
|
|
571
1703
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
);
|
|
1704
|
+
for (const threadId of affectedThreadIds) {
|
|
1705
|
+
await this.service.entities.thread.update({ entity: "thread", id: threadId }).set({
|
|
1706
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1707
|
+
}).go();
|
|
576
1708
|
}
|
|
577
|
-
|
|
578
|
-
region: config.region || "us-east-1",
|
|
579
|
-
endpoint: config.endpoint,
|
|
580
|
-
credentials: config.credentials
|
|
581
|
-
});
|
|
582
|
-
this.tableName = config.tableName;
|
|
583
|
-
this.client = DynamoDBDocumentClient.from(dynamoClient);
|
|
584
|
-
this.service = getElectroDbService(this.client, this.tableName);
|
|
1709
|
+
return updatedMessages;
|
|
585
1710
|
} catch (error) {
|
|
586
1711
|
throw new MastraError(
|
|
587
1712
|
{
|
|
588
|
-
id: "
|
|
1713
|
+
id: "STORAGE_DYNAMODB_STORE_UPDATE_MESSAGES_FAILED",
|
|
589
1714
|
domain: ErrorDomain.STORAGE,
|
|
590
|
-
category: ErrorCategory.
|
|
1715
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1716
|
+
details: { count: messages.length }
|
|
591
1717
|
},
|
|
592
1718
|
error
|
|
593
1719
|
);
|
|
594
1720
|
}
|
|
595
1721
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
* It assumes the table is created and managed externally via CDK/CloudFormation.
|
|
599
|
-
*
|
|
600
|
-
* This implementation only validates that the required table exists and is accessible.
|
|
601
|
-
* No table creation is attempted - we simply check if we can access the table.
|
|
602
|
-
*/
|
|
603
|
-
async createTable({ tableName }) {
|
|
604
|
-
this.logger.debug("Validating access to externally managed table", { tableName, physicalTable: this.tableName });
|
|
1722
|
+
async getResourceById({ resourceId }) {
|
|
1723
|
+
this.logger.debug("Getting resource by ID", { resourceId });
|
|
605
1724
|
try {
|
|
606
|
-
const
|
|
607
|
-
if (!
|
|
608
|
-
|
|
609
|
-
`Table ${this.tableName} does not exist or is not accessible. It should be created via CDK/CloudFormation.`
|
|
610
|
-
);
|
|
611
|
-
throw new Error(
|
|
612
|
-
`Table ${this.tableName} does not exist or is not accessible. Ensure it's created via CDK/CloudFormation before using this store.`
|
|
613
|
-
);
|
|
1725
|
+
const result = await this.service.entities.resource.get({ entity: "resource", id: resourceId }).go();
|
|
1726
|
+
if (!result.data) {
|
|
1727
|
+
return null;
|
|
614
1728
|
}
|
|
615
|
-
|
|
1729
|
+
const data = result.data;
|
|
1730
|
+
return {
|
|
1731
|
+
...data,
|
|
1732
|
+
// Convert date strings back to Date objects for consistency
|
|
1733
|
+
createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
|
|
1734
|
+
updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt,
|
|
1735
|
+
// Ensure workingMemory is always returned as a string, regardless of automatic parsing
|
|
1736
|
+
workingMemory: typeof data.workingMemory === "object" ? JSON.stringify(data.workingMemory) : data.workingMemory
|
|
1737
|
+
// metadata is already transformed by the entity's getter
|
|
1738
|
+
};
|
|
616
1739
|
} catch (error) {
|
|
617
|
-
this.logger.error("Error validating table access", { tableName: this.tableName, error });
|
|
618
1740
|
throw new MastraError(
|
|
619
1741
|
{
|
|
620
|
-
id: "
|
|
1742
|
+
id: "STORAGE_DYNAMODB_STORE_GET_RESOURCE_BY_ID_FAILED",
|
|
621
1743
|
domain: ErrorDomain.STORAGE,
|
|
622
1744
|
category: ErrorCategory.THIRD_PARTY,
|
|
623
|
-
details: {
|
|
1745
|
+
details: { resourceId }
|
|
1746
|
+
},
|
|
1747
|
+
error
|
|
1748
|
+
);
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
async saveResource({ resource }) {
|
|
1752
|
+
this.logger.debug("Saving resource", { resourceId: resource.id });
|
|
1753
|
+
const now = /* @__PURE__ */ new Date();
|
|
1754
|
+
const resourceData = {
|
|
1755
|
+
entity: "resource",
|
|
1756
|
+
id: resource.id,
|
|
1757
|
+
workingMemory: resource.workingMemory,
|
|
1758
|
+
metadata: resource.metadata ? JSON.stringify(resource.metadata) : void 0,
|
|
1759
|
+
createdAt: resource.createdAt?.toISOString() || now.toISOString(),
|
|
1760
|
+
updatedAt: now.toISOString()
|
|
1761
|
+
};
|
|
1762
|
+
try {
|
|
1763
|
+
await this.service.entities.resource.upsert(resourceData).go();
|
|
1764
|
+
return {
|
|
1765
|
+
id: resource.id,
|
|
1766
|
+
workingMemory: resource.workingMemory,
|
|
1767
|
+
metadata: resource.metadata,
|
|
1768
|
+
createdAt: resource.createdAt || now,
|
|
1769
|
+
updatedAt: now
|
|
1770
|
+
};
|
|
1771
|
+
} catch (error) {
|
|
1772
|
+
throw new MastraError(
|
|
1773
|
+
{
|
|
1774
|
+
id: "STORAGE_DYNAMODB_STORE_SAVE_RESOURCE_FAILED",
|
|
1775
|
+
domain: ErrorDomain.STORAGE,
|
|
1776
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1777
|
+
details: { resourceId: resource.id }
|
|
1778
|
+
},
|
|
1779
|
+
error
|
|
1780
|
+
);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
async updateResource({
|
|
1784
|
+
resourceId,
|
|
1785
|
+
workingMemory,
|
|
1786
|
+
metadata
|
|
1787
|
+
}) {
|
|
1788
|
+
this.logger.debug("Updating resource", { resourceId });
|
|
1789
|
+
try {
|
|
1790
|
+
const existingResource = await this.getResourceById({ resourceId });
|
|
1791
|
+
if (!existingResource) {
|
|
1792
|
+
const newResource = {
|
|
1793
|
+
id: resourceId,
|
|
1794
|
+
workingMemory,
|
|
1795
|
+
metadata: metadata || {},
|
|
1796
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
1797
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
1798
|
+
};
|
|
1799
|
+
return this.saveResource({ resource: newResource });
|
|
1800
|
+
}
|
|
1801
|
+
const now = /* @__PURE__ */ new Date();
|
|
1802
|
+
const updateData = {
|
|
1803
|
+
updatedAt: now.toISOString()
|
|
1804
|
+
};
|
|
1805
|
+
if (workingMemory !== void 0) {
|
|
1806
|
+
updateData.workingMemory = workingMemory;
|
|
1807
|
+
}
|
|
1808
|
+
if (metadata) {
|
|
1809
|
+
const existingMetadata = existingResource.metadata || {};
|
|
1810
|
+
const mergedMetadata = { ...existingMetadata, ...metadata };
|
|
1811
|
+
updateData.metadata = JSON.stringify(mergedMetadata);
|
|
1812
|
+
}
|
|
1813
|
+
await this.service.entities.resource.update({ entity: "resource", id: resourceId }).set(updateData).go();
|
|
1814
|
+
return {
|
|
1815
|
+
...existingResource,
|
|
1816
|
+
workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
|
|
1817
|
+
metadata: metadata ? { ...existingResource.metadata, ...metadata } : existingResource.metadata,
|
|
1818
|
+
updatedAt: now
|
|
1819
|
+
};
|
|
1820
|
+
} catch (error) {
|
|
1821
|
+
throw new MastraError(
|
|
1822
|
+
{
|
|
1823
|
+
id: "STORAGE_DYNAMODB_STORE_UPDATE_RESOURCE_FAILED",
|
|
1824
|
+
domain: ErrorDomain.STORAGE,
|
|
1825
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1826
|
+
details: { resourceId }
|
|
624
1827
|
},
|
|
625
1828
|
error
|
|
626
1829
|
);
|
|
627
1830
|
}
|
|
628
1831
|
}
|
|
1832
|
+
};
|
|
1833
|
+
var StoreOperationsDynamoDB = class extends StoreOperations {
|
|
1834
|
+
client;
|
|
1835
|
+
tableName;
|
|
1836
|
+
service;
|
|
1837
|
+
constructor({
|
|
1838
|
+
service,
|
|
1839
|
+
tableName,
|
|
1840
|
+
client
|
|
1841
|
+
}) {
|
|
1842
|
+
super();
|
|
1843
|
+
this.service = service;
|
|
1844
|
+
this.client = client;
|
|
1845
|
+
this.tableName = tableName;
|
|
1846
|
+
}
|
|
1847
|
+
async hasColumn() {
|
|
1848
|
+
return true;
|
|
1849
|
+
}
|
|
1850
|
+
async dropTable() {
|
|
1851
|
+
}
|
|
1852
|
+
// Helper methods for entity/table mapping
|
|
1853
|
+
getEntityNameForTable(tableName) {
|
|
1854
|
+
const mapping = {
|
|
1855
|
+
[TABLE_THREADS]: "thread",
|
|
1856
|
+
[TABLE_MESSAGES]: "message",
|
|
1857
|
+
[TABLE_WORKFLOW_SNAPSHOT]: "workflow_snapshot",
|
|
1858
|
+
[TABLE_EVALS]: "eval",
|
|
1859
|
+
[TABLE_SCORERS]: "score",
|
|
1860
|
+
[TABLE_TRACES]: "trace",
|
|
1861
|
+
[TABLE_RESOURCES]: "resource",
|
|
1862
|
+
[TABLE_AI_SPANS]: "ai_span"
|
|
1863
|
+
};
|
|
1864
|
+
return mapping[tableName] || null;
|
|
1865
|
+
}
|
|
1866
|
+
/**
|
|
1867
|
+
* Pre-processes a record to ensure Date objects are converted to ISO strings
|
|
1868
|
+
* This is necessary because ElectroDB validation happens before setters are applied
|
|
1869
|
+
*/
|
|
1870
|
+
preprocessRecord(record) {
|
|
1871
|
+
const processed = { ...record };
|
|
1872
|
+
if (processed.createdAt instanceof Date) {
|
|
1873
|
+
processed.createdAt = processed.createdAt.toISOString();
|
|
1874
|
+
}
|
|
1875
|
+
if (processed.updatedAt instanceof Date) {
|
|
1876
|
+
processed.updatedAt = processed.updatedAt.toISOString();
|
|
1877
|
+
}
|
|
1878
|
+
if (processed.created_at instanceof Date) {
|
|
1879
|
+
processed.created_at = processed.created_at.toISOString();
|
|
1880
|
+
}
|
|
1881
|
+
if (processed.result && typeof processed.result === "object") {
|
|
1882
|
+
processed.result = JSON.stringify(processed.result);
|
|
1883
|
+
}
|
|
1884
|
+
if (processed.test_info && typeof processed.test_info === "object") {
|
|
1885
|
+
processed.test_info = JSON.stringify(processed.test_info);
|
|
1886
|
+
} else if (processed.test_info === void 0 || processed.test_info === null) {
|
|
1887
|
+
delete processed.test_info;
|
|
1888
|
+
}
|
|
1889
|
+
if (processed.snapshot && typeof processed.snapshot === "object") {
|
|
1890
|
+
processed.snapshot = JSON.stringify(processed.snapshot);
|
|
1891
|
+
}
|
|
1892
|
+
if (processed.attributes && typeof processed.attributes === "object") {
|
|
1893
|
+
processed.attributes = JSON.stringify(processed.attributes);
|
|
1894
|
+
}
|
|
1895
|
+
if (processed.status && typeof processed.status === "object") {
|
|
1896
|
+
processed.status = JSON.stringify(processed.status);
|
|
1897
|
+
}
|
|
1898
|
+
if (processed.events && typeof processed.events === "object") {
|
|
1899
|
+
processed.events = JSON.stringify(processed.events);
|
|
1900
|
+
}
|
|
1901
|
+
if (processed.links && typeof processed.links === "object") {
|
|
1902
|
+
processed.links = JSON.stringify(processed.links);
|
|
1903
|
+
}
|
|
1904
|
+
return processed;
|
|
1905
|
+
}
|
|
629
1906
|
/**
|
|
630
1907
|
* Validates that the required DynamoDB table exists and is accessible.
|
|
631
1908
|
* This does not check the table structure - it assumes the table
|
|
@@ -654,61 +1931,64 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
654
1931
|
}
|
|
655
1932
|
}
|
|
656
1933
|
/**
|
|
657
|
-
*
|
|
658
|
-
*
|
|
659
|
-
*
|
|
1934
|
+
* This method is modified for DynamoDB with ElectroDB single-table design.
|
|
1935
|
+
* It assumes the table is created and managed externally via CDK/CloudFormation.
|
|
1936
|
+
*
|
|
1937
|
+
* This implementation only validates that the required table exists and is accessible.
|
|
1938
|
+
* No table creation is attempted - we simply check if we can access the table.
|
|
660
1939
|
*/
|
|
661
|
-
async
|
|
662
|
-
|
|
663
|
-
this.hasInitialized = this._performInitializationAndStore();
|
|
664
|
-
}
|
|
1940
|
+
async createTable({ tableName }) {
|
|
1941
|
+
this.logger.debug("Validating access to externally managed table", { tableName, physicalTable: this.tableName });
|
|
665
1942
|
try {
|
|
666
|
-
await this.
|
|
1943
|
+
const tableExists = await this.validateTableExists();
|
|
1944
|
+
if (!tableExists) {
|
|
1945
|
+
this.logger.error(
|
|
1946
|
+
`Table ${this.tableName} does not exist or is not accessible. It should be created via CDK/CloudFormation.`
|
|
1947
|
+
);
|
|
1948
|
+
throw new Error(
|
|
1949
|
+
`Table ${this.tableName} does not exist or is not accessible. Ensure it's created via CDK/CloudFormation before using this store.`
|
|
1950
|
+
);
|
|
1951
|
+
}
|
|
1952
|
+
this.logger.debug(`Table ${this.tableName} exists and is accessible`);
|
|
667
1953
|
} catch (error) {
|
|
1954
|
+
this.logger.error("Error validating table access", { tableName: this.tableName, error });
|
|
668
1955
|
throw new MastraError(
|
|
669
1956
|
{
|
|
670
|
-
id: "
|
|
1957
|
+
id: "STORAGE_DYNAMODB_STORE_VALIDATE_TABLE_ACCESS_FAILED",
|
|
671
1958
|
domain: ErrorDomain.STORAGE,
|
|
672
1959
|
category: ErrorCategory.THIRD_PARTY,
|
|
673
1960
|
details: { tableName: this.tableName }
|
|
674
1961
|
},
|
|
675
1962
|
error
|
|
676
|
-
);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
/**
|
|
680
|
-
* Performs the actual table validation and stores the promise.
|
|
681
|
-
* Handles resetting the stored promise on failure to allow retries.
|
|
682
|
-
*/
|
|
683
|
-
_performInitializationAndStore() {
|
|
684
|
-
return this.validateTableExists().then((exists) => {
|
|
685
|
-
if (!exists) {
|
|
686
|
-
throw new Error(
|
|
687
|
-
`Table ${this.tableName} does not exist or is not accessible. Ensure it's created via CDK/CloudFormation before using this store.`
|
|
688
|
-
);
|
|
689
|
-
}
|
|
690
|
-
return true;
|
|
691
|
-
}).catch((err) => {
|
|
692
|
-
this.hasInitialized = null;
|
|
693
|
-
throw err;
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
/**
|
|
697
|
-
* Pre-processes a record to ensure Date objects are converted to ISO strings
|
|
698
|
-
* This is necessary because ElectroDB validation happens before setters are applied
|
|
699
|
-
*/
|
|
700
|
-
preprocessRecord(record) {
|
|
701
|
-
const processed = { ...record };
|
|
702
|
-
if (processed.createdAt instanceof Date) {
|
|
703
|
-
processed.createdAt = processed.createdAt.toISOString();
|
|
1963
|
+
);
|
|
704
1964
|
}
|
|
705
|
-
|
|
706
|
-
|
|
1965
|
+
}
|
|
1966
|
+
async insert({ tableName, record }) {
|
|
1967
|
+
this.logger.debug("DynamoDB insert called", { tableName });
|
|
1968
|
+
const entityName = this.getEntityNameForTable(tableName);
|
|
1969
|
+
if (!entityName || !this.service.entities[entityName]) {
|
|
1970
|
+
throw new MastraError({
|
|
1971
|
+
id: "STORAGE_DYNAMODB_STORE_INSERT_INVALID_ARGS",
|
|
1972
|
+
domain: ErrorDomain.STORAGE,
|
|
1973
|
+
category: ErrorCategory.USER,
|
|
1974
|
+
text: "No entity defined for tableName",
|
|
1975
|
+
details: { tableName }
|
|
1976
|
+
});
|
|
707
1977
|
}
|
|
708
|
-
|
|
709
|
-
|
|
1978
|
+
try {
|
|
1979
|
+
const dataToSave = { entity: entityName, ...this.preprocessRecord(record) };
|
|
1980
|
+
await this.service.entities[entityName].create(dataToSave).go();
|
|
1981
|
+
} catch (error) {
|
|
1982
|
+
throw new MastraError(
|
|
1983
|
+
{
|
|
1984
|
+
id: "STORAGE_DYNAMODB_STORE_INSERT_FAILED",
|
|
1985
|
+
domain: ErrorDomain.STORAGE,
|
|
1986
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1987
|
+
details: { tableName }
|
|
1988
|
+
},
|
|
1989
|
+
error
|
|
1990
|
+
);
|
|
710
1991
|
}
|
|
711
|
-
return processed;
|
|
712
1992
|
}
|
|
713
1993
|
async alterTable(_args) {
|
|
714
1994
|
}
|
|
@@ -745,10 +2025,10 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
745
2025
|
if (!item.id) throw new Error(`Missing required key 'id' for entity 'message'`);
|
|
746
2026
|
key.id = item.id;
|
|
747
2027
|
break;
|
|
748
|
-
case "
|
|
2028
|
+
case "workflow_snapshot":
|
|
749
2029
|
if (!item.workflow_name)
|
|
750
|
-
throw new Error(`Missing required key 'workflow_name' for entity '
|
|
751
|
-
if (!item.run_id) throw new Error(`Missing required key 'run_id' for entity '
|
|
2030
|
+
throw new Error(`Missing required key 'workflow_name' for entity 'workflow_snapshot'`);
|
|
2031
|
+
if (!item.run_id) throw new Error(`Missing required key 'run_id' for entity 'workflow_snapshot'`);
|
|
752
2032
|
key.workflow_name = item.workflow_name;
|
|
753
2033
|
key.run_id = item.run_id;
|
|
754
2034
|
break;
|
|
@@ -760,6 +2040,14 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
760
2040
|
if (!item.id) throw new Error(`Missing required key 'id' for entity 'trace'`);
|
|
761
2041
|
key.id = item.id;
|
|
762
2042
|
break;
|
|
2043
|
+
case "score":
|
|
2044
|
+
if (!item.id) throw new Error(`Missing required key 'id' for entity 'score'`);
|
|
2045
|
+
key.id = item.id;
|
|
2046
|
+
break;
|
|
2047
|
+
case "resource":
|
|
2048
|
+
if (!item.id) throw new Error(`Missing required key 'id' for entity 'resource'`);
|
|
2049
|
+
key.id = item.id;
|
|
2050
|
+
break;
|
|
763
2051
|
default:
|
|
764
2052
|
this.logger.warn(`Unknown entity type encountered during clearTable: ${entityName}`);
|
|
765
2053
|
throw new Error(`Cannot construct delete key for unknown entity type: ${entityName}`);
|
|
@@ -784,46 +2072,10 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
784
2072
|
);
|
|
785
2073
|
}
|
|
786
2074
|
}
|
|
787
|
-
/**
|
|
788
|
-
* Insert a record into the specified "table" (entity)
|
|
789
|
-
*/
|
|
790
|
-
async insert({
|
|
791
|
-
tableName,
|
|
792
|
-
record
|
|
793
|
-
}) {
|
|
794
|
-
this.logger.debug("DynamoDB insert called", { tableName });
|
|
795
|
-
const entityName = this.getEntityNameForTable(tableName);
|
|
796
|
-
if (!entityName || !this.service.entities[entityName]) {
|
|
797
|
-
throw new MastraError({
|
|
798
|
-
id: "STORAGE_DYNAMODB_STORE_INSERT_INVALID_ARGS",
|
|
799
|
-
domain: ErrorDomain.STORAGE,
|
|
800
|
-
category: ErrorCategory.USER,
|
|
801
|
-
text: "No entity defined for tableName",
|
|
802
|
-
details: { tableName }
|
|
803
|
-
});
|
|
804
|
-
}
|
|
805
|
-
try {
|
|
806
|
-
const dataToSave = { entity: entityName, ...this.preprocessRecord(record) };
|
|
807
|
-
await this.service.entities[entityName].create(dataToSave).go();
|
|
808
|
-
} catch (error) {
|
|
809
|
-
throw new MastraError(
|
|
810
|
-
{
|
|
811
|
-
id: "STORAGE_DYNAMODB_STORE_INSERT_FAILED",
|
|
812
|
-
domain: ErrorDomain.STORAGE,
|
|
813
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
814
|
-
details: { tableName }
|
|
815
|
-
},
|
|
816
|
-
error
|
|
817
|
-
);
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
2075
|
/**
|
|
821
2076
|
* Insert multiple records as a batch
|
|
822
2077
|
*/
|
|
823
|
-
async batchInsert({
|
|
824
|
-
tableName,
|
|
825
|
-
records
|
|
826
|
-
}) {
|
|
2078
|
+
async batchInsert({ tableName, records }) {
|
|
827
2079
|
this.logger.debug("DynamoDB batchInsert called", { tableName, count: records.length });
|
|
828
2080
|
const entityName = this.getEntityNameForTable(tableName);
|
|
829
2081
|
if (!entityName || !this.service.entities[entityName]) {
|
|
@@ -868,10 +2120,7 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
868
2120
|
/**
|
|
869
2121
|
* Load a record by its keys
|
|
870
2122
|
*/
|
|
871
|
-
async load({
|
|
872
|
-
tableName,
|
|
873
|
-
keys
|
|
874
|
-
}) {
|
|
2123
|
+
async load({ tableName, keys }) {
|
|
875
2124
|
this.logger.debug("DynamoDB load called", { tableName, keys });
|
|
876
2125
|
const entityName = this.getEntityNameForTable(tableName);
|
|
877
2126
|
if (!entityName || !this.service.entities[entityName]) {
|
|
@@ -903,344 +2152,316 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
903
2152
|
);
|
|
904
2153
|
}
|
|
905
2154
|
}
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
2155
|
+
};
|
|
2156
|
+
var ScoresStorageDynamoDB = class extends ScoresStorage {
|
|
2157
|
+
service;
|
|
2158
|
+
constructor({ service }) {
|
|
2159
|
+
super();
|
|
2160
|
+
this.service = service;
|
|
2161
|
+
}
|
|
2162
|
+
// Helper function to parse score data (handle JSON fields)
|
|
2163
|
+
parseScoreData(data) {
|
|
2164
|
+
return {
|
|
2165
|
+
...data,
|
|
2166
|
+
// Convert date strings back to Date objects for consistency
|
|
2167
|
+
createdAt: data.createdAt ? new Date(data.createdAt) : /* @__PURE__ */ new Date(),
|
|
2168
|
+
updatedAt: data.updatedAt ? new Date(data.updatedAt) : /* @__PURE__ */ new Date()
|
|
2169
|
+
// JSON fields are already transformed by the entity's getters
|
|
2170
|
+
};
|
|
2171
|
+
}
|
|
2172
|
+
async getScoreById({ id }) {
|
|
2173
|
+
this.logger.debug("Getting score by ID", { id });
|
|
909
2174
|
try {
|
|
910
|
-
const result = await this.service.entities.
|
|
2175
|
+
const result = await this.service.entities.score.get({ entity: "score", id }).go();
|
|
911
2176
|
if (!result.data) {
|
|
912
2177
|
return null;
|
|
913
2178
|
}
|
|
914
|
-
|
|
915
|
-
return {
|
|
916
|
-
...data,
|
|
917
|
-
// Convert date strings back to Date objects for consistency
|
|
918
|
-
createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
|
|
919
|
-
updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
|
|
920
|
-
// metadata: data.metadata ? JSON.parse(data.metadata) : undefined, // REMOVED by AI
|
|
921
|
-
// metadata is already transformed by the entity's getter
|
|
922
|
-
};
|
|
2179
|
+
return this.parseScoreData(result.data);
|
|
923
2180
|
} catch (error) {
|
|
924
2181
|
throw new MastraError(
|
|
925
2182
|
{
|
|
926
|
-
id: "
|
|
2183
|
+
id: "STORAGE_DYNAMODB_STORE_GET_SCORE_BY_ID_FAILED",
|
|
927
2184
|
domain: ErrorDomain.STORAGE,
|
|
928
2185
|
category: ErrorCategory.THIRD_PARTY,
|
|
929
|
-
details: {
|
|
2186
|
+
details: { id }
|
|
930
2187
|
},
|
|
931
2188
|
error
|
|
932
2189
|
);
|
|
933
2190
|
}
|
|
934
2191
|
}
|
|
935
|
-
async
|
|
936
|
-
|
|
2192
|
+
async saveScore(score) {
|
|
2193
|
+
let validatedScore;
|
|
937
2194
|
try {
|
|
938
|
-
|
|
939
|
-
if (!result.data.length) {
|
|
940
|
-
return [];
|
|
941
|
-
}
|
|
942
|
-
return result.data.map((data) => ({
|
|
943
|
-
...data,
|
|
944
|
-
// Convert date strings back to Date objects for consistency
|
|
945
|
-
createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
|
|
946
|
-
updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
|
|
947
|
-
// metadata: data.metadata ? JSON.parse(data.metadata) : undefined, // REMOVED by AI
|
|
948
|
-
// metadata is already transformed by the entity's getter
|
|
949
|
-
}));
|
|
2195
|
+
validatedScore = saveScorePayloadSchema.parse(score);
|
|
950
2196
|
} catch (error) {
|
|
951
2197
|
throw new MastraError(
|
|
952
2198
|
{
|
|
953
|
-
id: "
|
|
2199
|
+
id: "STORAGE_DYNAMODB_STORE_SAVE_SCORE_FAILED",
|
|
954
2200
|
domain: ErrorDomain.STORAGE,
|
|
955
|
-
category: ErrorCategory.THIRD_PARTY
|
|
956
|
-
details: { resourceId }
|
|
2201
|
+
category: ErrorCategory.THIRD_PARTY
|
|
957
2202
|
},
|
|
958
2203
|
error
|
|
959
2204
|
);
|
|
960
2205
|
}
|
|
961
|
-
}
|
|
962
|
-
async saveThread({ thread }) {
|
|
963
|
-
this.logger.debug("Saving thread", { threadId: thread.id });
|
|
964
2206
|
const now = /* @__PURE__ */ new Date();
|
|
965
|
-
const
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
2207
|
+
const scoreId = `score-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
2208
|
+
const scoreData = {
|
|
2209
|
+
entity: "score",
|
|
2210
|
+
id: scoreId,
|
|
2211
|
+
scorerId: validatedScore.scorerId,
|
|
2212
|
+
traceId: validatedScore.traceId || "",
|
|
2213
|
+
spanId: validatedScore.spanId || "",
|
|
2214
|
+
runId: validatedScore.runId,
|
|
2215
|
+
scorer: typeof validatedScore.scorer === "string" ? validatedScore.scorer : JSON.stringify(validatedScore.scorer),
|
|
2216
|
+
preprocessStepResult: typeof validatedScore.preprocessStepResult === "string" ? validatedScore.preprocessStepResult : JSON.stringify(validatedScore.preprocessStepResult),
|
|
2217
|
+
analyzeStepResult: typeof validatedScore.analyzeStepResult === "string" ? validatedScore.analyzeStepResult : JSON.stringify(validatedScore.analyzeStepResult),
|
|
2218
|
+
score: validatedScore.score,
|
|
2219
|
+
reason: validatedScore.reason,
|
|
2220
|
+
preprocessPrompt: validatedScore.preprocessPrompt,
|
|
2221
|
+
generateScorePrompt: validatedScore.generateScorePrompt,
|
|
2222
|
+
generateReasonPrompt: validatedScore.generateReasonPrompt,
|
|
2223
|
+
analyzePrompt: validatedScore.analyzePrompt,
|
|
2224
|
+
input: typeof validatedScore.input === "string" ? validatedScore.input : JSON.stringify(validatedScore.input),
|
|
2225
|
+
output: typeof validatedScore.output === "string" ? validatedScore.output : JSON.stringify(validatedScore.output),
|
|
2226
|
+
additionalContext: typeof validatedScore.additionalContext === "string" ? validatedScore.additionalContext : JSON.stringify(validatedScore.additionalContext),
|
|
2227
|
+
runtimeContext: typeof validatedScore.runtimeContext === "string" ? validatedScore.runtimeContext : JSON.stringify(validatedScore.runtimeContext),
|
|
2228
|
+
entityType: validatedScore.entityType,
|
|
2229
|
+
entityData: typeof validatedScore.entity === "string" ? validatedScore.entity : JSON.stringify(validatedScore.entity),
|
|
2230
|
+
entityId: validatedScore.entityId,
|
|
2231
|
+
source: validatedScore.source,
|
|
2232
|
+
resourceId: validatedScore.resourceId || "",
|
|
2233
|
+
threadId: validatedScore.threadId || "",
|
|
2234
|
+
createdAt: now.toISOString(),
|
|
2235
|
+
updatedAt: now.toISOString()
|
|
973
2236
|
};
|
|
974
2237
|
try {
|
|
975
|
-
await this.service.entities.
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
updatedAt: now,
|
|
982
|
-
metadata: thread.metadata
|
|
2238
|
+
await this.service.entities.score.upsert(scoreData).go();
|
|
2239
|
+
const savedScore = {
|
|
2240
|
+
...score,
|
|
2241
|
+
id: scoreId,
|
|
2242
|
+
createdAt: now,
|
|
2243
|
+
updatedAt: now
|
|
983
2244
|
};
|
|
2245
|
+
return { score: savedScore };
|
|
984
2246
|
} catch (error) {
|
|
985
2247
|
throw new MastraError(
|
|
986
2248
|
{
|
|
987
|
-
id: "
|
|
2249
|
+
id: "STORAGE_DYNAMODB_STORE_SAVE_SCORE_FAILED",
|
|
988
2250
|
domain: ErrorDomain.STORAGE,
|
|
989
2251
|
category: ErrorCategory.THIRD_PARTY,
|
|
990
|
-
details: {
|
|
2252
|
+
details: { scorerId: score.scorerId, runId: score.runId }
|
|
991
2253
|
},
|
|
992
2254
|
error
|
|
993
2255
|
);
|
|
994
2256
|
}
|
|
995
2257
|
}
|
|
996
|
-
async
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
2258
|
+
async getScoresByScorerId({
|
|
2259
|
+
scorerId,
|
|
2260
|
+
pagination,
|
|
2261
|
+
entityId,
|
|
2262
|
+
entityType,
|
|
2263
|
+
source
|
|
1000
2264
|
}) {
|
|
1001
|
-
this.logger.debug("Updating thread", { threadId: id });
|
|
1002
2265
|
try {
|
|
1003
|
-
const
|
|
1004
|
-
|
|
1005
|
-
|
|
2266
|
+
const query = this.service.entities.score.query.byScorer({ entity: "score", scorerId });
|
|
2267
|
+
const results = await query.go();
|
|
2268
|
+
let allScores = results.data.map((data) => this.parseScoreData(data));
|
|
2269
|
+
if (entityId) {
|
|
2270
|
+
allScores = allScores.filter((score) => score.entityId === entityId);
|
|
1006
2271
|
}
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
updatedAt: now.toISOString()
|
|
1010
|
-
};
|
|
1011
|
-
if (title) {
|
|
1012
|
-
updateData.title = title;
|
|
2272
|
+
if (entityType) {
|
|
2273
|
+
allScores = allScores.filter((score) => score.entityType === entityType);
|
|
1013
2274
|
}
|
|
1014
|
-
if (
|
|
1015
|
-
|
|
2275
|
+
if (source) {
|
|
2276
|
+
allScores = allScores.filter((score) => score.source === source);
|
|
1016
2277
|
}
|
|
1017
|
-
|
|
2278
|
+
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2279
|
+
const startIndex = pagination.page * pagination.perPage;
|
|
2280
|
+
const endIndex = startIndex + pagination.perPage;
|
|
2281
|
+
const paginatedScores = allScores.slice(startIndex, endIndex);
|
|
2282
|
+
const total = allScores.length;
|
|
2283
|
+
const hasMore = endIndex < total;
|
|
1018
2284
|
return {
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
2285
|
+
scores: paginatedScores,
|
|
2286
|
+
pagination: {
|
|
2287
|
+
total,
|
|
2288
|
+
page: pagination.page,
|
|
2289
|
+
perPage: pagination.perPage,
|
|
2290
|
+
hasMore
|
|
2291
|
+
}
|
|
1023
2292
|
};
|
|
1024
2293
|
} catch (error) {
|
|
1025
2294
|
throw new MastraError(
|
|
1026
2295
|
{
|
|
1027
|
-
id: "
|
|
2296
|
+
id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_SCORER_ID_FAILED",
|
|
1028
2297
|
domain: ErrorDomain.STORAGE,
|
|
1029
2298
|
category: ErrorCategory.THIRD_PARTY,
|
|
1030
|
-
details: {
|
|
2299
|
+
details: {
|
|
2300
|
+
scorerId: scorerId || "",
|
|
2301
|
+
entityId: entityId || "",
|
|
2302
|
+
entityType: entityType || "",
|
|
2303
|
+
source: source || "",
|
|
2304
|
+
page: pagination.page,
|
|
2305
|
+
perPage: pagination.perPage
|
|
2306
|
+
}
|
|
1031
2307
|
},
|
|
1032
2308
|
error
|
|
1033
2309
|
);
|
|
1034
2310
|
}
|
|
1035
2311
|
}
|
|
1036
|
-
async
|
|
1037
|
-
|
|
2312
|
+
async getScoresByRunId({
|
|
2313
|
+
runId,
|
|
2314
|
+
pagination
|
|
2315
|
+
}) {
|
|
2316
|
+
this.logger.debug("Getting scores by run ID", { runId, pagination });
|
|
1038
2317
|
try {
|
|
1039
|
-
|
|
2318
|
+
const query = this.service.entities.score.query.byRun({ entity: "score", runId });
|
|
2319
|
+
const results = await query.go();
|
|
2320
|
+
const allScores = results.data.map((data) => this.parseScoreData(data));
|
|
2321
|
+
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2322
|
+
const startIndex = pagination.page * pagination.perPage;
|
|
2323
|
+
const endIndex = startIndex + pagination.perPage;
|
|
2324
|
+
const paginatedScores = allScores.slice(startIndex, endIndex);
|
|
2325
|
+
const total = allScores.length;
|
|
2326
|
+
const hasMore = endIndex < total;
|
|
2327
|
+
return {
|
|
2328
|
+
scores: paginatedScores,
|
|
2329
|
+
pagination: {
|
|
2330
|
+
total,
|
|
2331
|
+
page: pagination.page,
|
|
2332
|
+
perPage: pagination.perPage,
|
|
2333
|
+
hasMore
|
|
2334
|
+
}
|
|
2335
|
+
};
|
|
1040
2336
|
} catch (error) {
|
|
1041
2337
|
throw new MastraError(
|
|
1042
2338
|
{
|
|
1043
|
-
id: "
|
|
2339
|
+
id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_RUN_ID_FAILED",
|
|
1044
2340
|
domain: ErrorDomain.STORAGE,
|
|
1045
2341
|
category: ErrorCategory.THIRD_PARTY,
|
|
1046
|
-
details: {
|
|
2342
|
+
details: { runId, page: pagination.page, perPage: pagination.perPage }
|
|
1047
2343
|
},
|
|
1048
2344
|
error
|
|
1049
2345
|
);
|
|
1050
2346
|
}
|
|
1051
2347
|
}
|
|
1052
|
-
async
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
format
|
|
2348
|
+
async getScoresByEntityId({
|
|
2349
|
+
entityId,
|
|
2350
|
+
entityType,
|
|
2351
|
+
pagination
|
|
1057
2352
|
}) {
|
|
1058
|
-
this.logger.debug("Getting
|
|
2353
|
+
this.logger.debug("Getting scores by entity ID", { entityId, entityType, pagination });
|
|
1059
2354
|
try {
|
|
1060
|
-
const query = this.service.entities.
|
|
1061
|
-
const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
|
|
1062
|
-
if (limit !== Number.MAX_SAFE_INTEGER) {
|
|
1063
|
-
const results2 = await query.go({ limit, order: "desc" });
|
|
1064
|
-
const list2 = new MessageList({ threadId, resourceId }).add(
|
|
1065
|
-
results2.data.map((data) => this.parseMessageData(data)),
|
|
1066
|
-
"memory"
|
|
1067
|
-
);
|
|
1068
|
-
if (format === `v2`) return list2.get.all.v2();
|
|
1069
|
-
return list2.get.all.v1();
|
|
1070
|
-
}
|
|
2355
|
+
const query = this.service.entities.score.query.byEntityData({ entity: "score", entityId });
|
|
1071
2356
|
const results = await query.go();
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
{
|
|
1081
|
-
id: "STORAGE_DYNAMODB_STORE_GET_MESSAGES_FAILED",
|
|
1082
|
-
domain: ErrorDomain.STORAGE,
|
|
1083
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1084
|
-
details: { threadId }
|
|
1085
|
-
},
|
|
1086
|
-
error
|
|
1087
|
-
);
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
async saveMessages(args) {
|
|
1091
|
-
const { messages, format = "v1" } = args;
|
|
1092
|
-
this.logger.debug("Saving messages", { count: messages.length });
|
|
1093
|
-
if (!messages.length) {
|
|
1094
|
-
return [];
|
|
1095
|
-
}
|
|
1096
|
-
const threadId = messages[0]?.threadId;
|
|
1097
|
-
if (!threadId) {
|
|
1098
|
-
throw new Error("Thread ID is required");
|
|
1099
|
-
}
|
|
1100
|
-
const messagesToSave = messages.map((msg) => {
|
|
1101
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2357
|
+
let allScores = results.data.map((data) => this.parseScoreData(data));
|
|
2358
|
+
allScores = allScores.filter((score) => score.entityType === entityType);
|
|
2359
|
+
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2360
|
+
const startIndex = pagination.page * pagination.perPage;
|
|
2361
|
+
const endIndex = startIndex + pagination.perPage;
|
|
2362
|
+
const paginatedScores = allScores.slice(startIndex, endIndex);
|
|
2363
|
+
const total = allScores.length;
|
|
2364
|
+
const hasMore = endIndex < total;
|
|
1102
2365
|
return {
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
// Ensure complex fields are stringified if not handled by attribute setters
|
|
1111
|
-
content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content),
|
|
1112
|
-
toolCallArgs: `toolCallArgs` in msg && msg.toolCallArgs ? JSON.stringify(msg.toolCallArgs) : void 0,
|
|
1113
|
-
toolCallIds: `toolCallIds` in msg && msg.toolCallIds ? JSON.stringify(msg.toolCallIds) : void 0,
|
|
1114
|
-
toolNames: `toolNames` in msg && msg.toolNames ? JSON.stringify(msg.toolNames) : void 0,
|
|
1115
|
-
createdAt: msg.createdAt instanceof Date ? msg.createdAt.toISOString() : msg.createdAt || now,
|
|
1116
|
-
updatedAt: now
|
|
1117
|
-
// Add updatedAt
|
|
2366
|
+
scores: paginatedScores,
|
|
2367
|
+
pagination: {
|
|
2368
|
+
total,
|
|
2369
|
+
page: pagination.page,
|
|
2370
|
+
perPage: pagination.perPage,
|
|
2371
|
+
hasMore
|
|
2372
|
+
}
|
|
1118
2373
|
};
|
|
1119
|
-
});
|
|
1120
|
-
try {
|
|
1121
|
-
const batchSize = 25;
|
|
1122
|
-
const batches = [];
|
|
1123
|
-
for (let i = 0; i < messagesToSave.length; i += batchSize) {
|
|
1124
|
-
const batch = messagesToSave.slice(i, i + batchSize);
|
|
1125
|
-
batches.push(batch);
|
|
1126
|
-
}
|
|
1127
|
-
await Promise.all([
|
|
1128
|
-
// Process message batches
|
|
1129
|
-
...batches.map(async (batch) => {
|
|
1130
|
-
for (const messageData of batch) {
|
|
1131
|
-
if (!messageData.entity) {
|
|
1132
|
-
this.logger.error("Missing entity property in message data for create", { messageData });
|
|
1133
|
-
throw new Error("Internal error: Missing entity property during saveMessages");
|
|
1134
|
-
}
|
|
1135
|
-
await this.service.entities.message.put(messageData).go();
|
|
1136
|
-
}
|
|
1137
|
-
}),
|
|
1138
|
-
// Update thread's updatedAt timestamp
|
|
1139
|
-
this.service.entities.thread.update({ entity: "thread", id: threadId }).set({
|
|
1140
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1141
|
-
}).go()
|
|
1142
|
-
]);
|
|
1143
|
-
const list = new MessageList().add(messages, "memory");
|
|
1144
|
-
if (format === `v1`) return list.get.all.v1();
|
|
1145
|
-
return list.get.all.v2();
|
|
1146
2374
|
} catch (error) {
|
|
1147
2375
|
throw new MastraError(
|
|
1148
2376
|
{
|
|
1149
|
-
id: "
|
|
2377
|
+
id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_ENTITY_ID_FAILED",
|
|
1150
2378
|
domain: ErrorDomain.STORAGE,
|
|
1151
2379
|
category: ErrorCategory.THIRD_PARTY,
|
|
1152
|
-
details: {
|
|
1153
|
-
},
|
|
1154
|
-
error
|
|
1155
|
-
);
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
// Helper function to parse message data (handle JSON fields)
|
|
1159
|
-
parseMessageData(data) {
|
|
1160
|
-
return {
|
|
1161
|
-
...data,
|
|
1162
|
-
// Ensure dates are Date objects if needed (ElectroDB might return strings)
|
|
1163
|
-
createdAt: data.createdAt ? new Date(data.createdAt) : void 0,
|
|
1164
|
-
updatedAt: data.updatedAt ? new Date(data.updatedAt) : void 0
|
|
1165
|
-
// Other fields like content, toolCallArgs etc. are assumed to be correctly
|
|
1166
|
-
// transformed by the ElectroDB entity getters.
|
|
1167
|
-
};
|
|
1168
|
-
}
|
|
1169
|
-
// Trace operations
|
|
1170
|
-
async getTraces(args) {
|
|
1171
|
-
const { name, scope, page, perPage } = args;
|
|
1172
|
-
this.logger.debug("Getting traces", { name, scope, page, perPage });
|
|
1173
|
-
try {
|
|
1174
|
-
let query;
|
|
1175
|
-
if (name) {
|
|
1176
|
-
query = this.service.entities.trace.query.byName({ entity: "trace", name });
|
|
1177
|
-
} else if (scope) {
|
|
1178
|
-
query = this.service.entities.trace.query.byScope({ entity: "trace", scope });
|
|
1179
|
-
} else {
|
|
1180
|
-
this.logger.warn("Performing a scan operation on traces - consider using a more specific query");
|
|
1181
|
-
query = this.service.entities.trace.scan;
|
|
1182
|
-
}
|
|
1183
|
-
let items = [];
|
|
1184
|
-
let cursor = null;
|
|
1185
|
-
let pagesFetched = 0;
|
|
1186
|
-
const startPage = page > 0 ? page : 1;
|
|
1187
|
-
do {
|
|
1188
|
-
const results = await query.go({ cursor, limit: perPage });
|
|
1189
|
-
pagesFetched++;
|
|
1190
|
-
if (pagesFetched === startPage) {
|
|
1191
|
-
items = results.data;
|
|
1192
|
-
break;
|
|
1193
|
-
}
|
|
1194
|
-
cursor = results.cursor;
|
|
1195
|
-
if (!cursor && results.data.length > 0 && pagesFetched < startPage) {
|
|
1196
|
-
break;
|
|
1197
|
-
}
|
|
1198
|
-
} while (cursor && pagesFetched < startPage);
|
|
1199
|
-
return items;
|
|
1200
|
-
} catch (error) {
|
|
1201
|
-
throw new MastraError(
|
|
1202
|
-
{
|
|
1203
|
-
id: "STORAGE_DYNAMODB_STORE_GET_TRACES_FAILED",
|
|
1204
|
-
domain: ErrorDomain.STORAGE,
|
|
1205
|
-
category: ErrorCategory.THIRD_PARTY
|
|
2380
|
+
details: { entityId, entityType, page: pagination.page, perPage: pagination.perPage }
|
|
1206
2381
|
},
|
|
1207
2382
|
error
|
|
1208
2383
|
);
|
|
1209
2384
|
}
|
|
1210
2385
|
}
|
|
1211
|
-
async
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
2386
|
+
async getScoresBySpan({
|
|
2387
|
+
traceId,
|
|
2388
|
+
spanId,
|
|
2389
|
+
pagination
|
|
2390
|
+
}) {
|
|
2391
|
+
this.logger.debug("Getting scores by span", { traceId, spanId, pagination });
|
|
1216
2392
|
try {
|
|
1217
|
-
const
|
|
1218
|
-
await
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
2393
|
+
const query = this.service.entities.score.query.bySpan({ entity: "score", traceId, spanId });
|
|
2394
|
+
const results = await query.go();
|
|
2395
|
+
const allScores = results.data.map((data) => this.parseScoreData(data));
|
|
2396
|
+
allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2397
|
+
const startIndex = pagination.page * pagination.perPage;
|
|
2398
|
+
const endIndex = startIndex + pagination.perPage;
|
|
2399
|
+
const paginatedScores = allScores.slice(startIndex, endIndex);
|
|
2400
|
+
const total = allScores.length;
|
|
2401
|
+
const hasMore = endIndex < total;
|
|
2402
|
+
return {
|
|
2403
|
+
scores: paginatedScores,
|
|
2404
|
+
pagination: {
|
|
2405
|
+
total,
|
|
2406
|
+
page: pagination.page,
|
|
2407
|
+
perPage: pagination.perPage,
|
|
2408
|
+
hasMore
|
|
2409
|
+
}
|
|
2410
|
+
};
|
|
1223
2411
|
} catch (error) {
|
|
1224
2412
|
throw new MastraError(
|
|
1225
2413
|
{
|
|
1226
|
-
id: "
|
|
2414
|
+
id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_SPAN_FAILED",
|
|
1227
2415
|
domain: ErrorDomain.STORAGE,
|
|
1228
2416
|
category: ErrorCategory.THIRD_PARTY,
|
|
1229
|
-
details: {
|
|
2417
|
+
details: { traceId, spanId, page: pagination.page, perPage: pagination.perPage }
|
|
1230
2418
|
},
|
|
1231
2419
|
error
|
|
1232
2420
|
);
|
|
1233
2421
|
}
|
|
1234
2422
|
}
|
|
2423
|
+
};
|
|
2424
|
+
function formatWorkflowRun(snapshotData) {
|
|
2425
|
+
return {
|
|
2426
|
+
workflowName: snapshotData.workflow_name,
|
|
2427
|
+
runId: snapshotData.run_id,
|
|
2428
|
+
snapshot: snapshotData.snapshot,
|
|
2429
|
+
createdAt: new Date(snapshotData.createdAt),
|
|
2430
|
+
updatedAt: new Date(snapshotData.updatedAt),
|
|
2431
|
+
resourceId: snapshotData.resourceId
|
|
2432
|
+
};
|
|
2433
|
+
}
|
|
2434
|
+
var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
|
|
2435
|
+
service;
|
|
2436
|
+
constructor({ service }) {
|
|
2437
|
+
super();
|
|
2438
|
+
this.service = service;
|
|
2439
|
+
}
|
|
2440
|
+
updateWorkflowResults({
|
|
2441
|
+
// workflowName,
|
|
2442
|
+
// runId,
|
|
2443
|
+
// stepId,
|
|
2444
|
+
// result,
|
|
2445
|
+
// runtimeContext,
|
|
2446
|
+
}) {
|
|
2447
|
+
throw new Error("Method not implemented.");
|
|
2448
|
+
}
|
|
2449
|
+
updateWorkflowState({
|
|
2450
|
+
// workflowName,
|
|
2451
|
+
// runId,
|
|
2452
|
+
// opts,
|
|
2453
|
+
}) {
|
|
2454
|
+
throw new Error("Method not implemented.");
|
|
2455
|
+
}
|
|
1235
2456
|
// Workflow operations
|
|
1236
2457
|
async persistWorkflowSnapshot({
|
|
1237
2458
|
workflowName,
|
|
1238
2459
|
runId,
|
|
2460
|
+
resourceId,
|
|
1239
2461
|
snapshot
|
|
1240
2462
|
}) {
|
|
1241
2463
|
this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
|
|
1242
2464
|
try {
|
|
1243
|
-
const resourceId = "resourceId" in snapshot ? snapshot.resourceId : void 0;
|
|
1244
2465
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1245
2466
|
const data = {
|
|
1246
2467
|
entity: "workflow_snapshot",
|
|
@@ -1253,7 +2474,7 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1253
2474
|
updatedAt: now,
|
|
1254
2475
|
resourceId
|
|
1255
2476
|
};
|
|
1256
|
-
await this.service.entities.
|
|
2477
|
+
await this.service.entities.workflow_snapshot.upsert(data).go();
|
|
1257
2478
|
} catch (error) {
|
|
1258
2479
|
throw new MastraError(
|
|
1259
2480
|
{
|
|
@@ -1272,7 +2493,7 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1272
2493
|
}) {
|
|
1273
2494
|
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
1274
2495
|
try {
|
|
1275
|
-
const result = await this.service.entities.
|
|
2496
|
+
const result = await this.service.entities.workflow_snapshot.get({
|
|
1276
2497
|
entity: "workflow_snapshot",
|
|
1277
2498
|
// Add entity type
|
|
1278
2499
|
workflow_name: workflowName,
|
|
@@ -1301,14 +2522,14 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1301
2522
|
const offset = args?.offset || 0;
|
|
1302
2523
|
let query;
|
|
1303
2524
|
if (args?.workflowName) {
|
|
1304
|
-
query = this.service.entities.
|
|
2525
|
+
query = this.service.entities.workflow_snapshot.query.primary({
|
|
1305
2526
|
entity: "workflow_snapshot",
|
|
1306
2527
|
// Add entity type
|
|
1307
2528
|
workflow_name: args.workflowName
|
|
1308
2529
|
});
|
|
1309
2530
|
} else {
|
|
1310
2531
|
this.logger.warn("Performing a scan operation on workflow snapshots - consider using a more specific query");
|
|
1311
|
-
query = this.service.entities.
|
|
2532
|
+
query = this.service.entities.workflow_snapshot.scan;
|
|
1312
2533
|
}
|
|
1313
2534
|
const allMatchingSnapshots = [];
|
|
1314
2535
|
let cursor = null;
|
|
@@ -1346,7 +2567,7 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1346
2567
|
}
|
|
1347
2568
|
const total = allMatchingSnapshots.length;
|
|
1348
2569
|
const paginatedData = allMatchingSnapshots.slice(offset, offset + limit);
|
|
1349
|
-
const runs = paginatedData.map((snapshot) =>
|
|
2570
|
+
const runs = paginatedData.map((snapshot) => formatWorkflowRun(snapshot));
|
|
1350
2571
|
return {
|
|
1351
2572
|
runs,
|
|
1352
2573
|
total
|
|
@@ -1369,7 +2590,7 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1369
2590
|
try {
|
|
1370
2591
|
if (workflowName) {
|
|
1371
2592
|
this.logger.debug("WorkflowName provided, using direct GET operation.");
|
|
1372
|
-
const result2 = await this.service.entities.
|
|
2593
|
+
const result2 = await this.service.entities.workflow_snapshot.get({
|
|
1373
2594
|
entity: "workflow_snapshot",
|
|
1374
2595
|
// Entity type for PK
|
|
1375
2596
|
workflow_name: workflowName,
|
|
@@ -1391,7 +2612,7 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1391
2612
|
this.logger.debug(
|
|
1392
2613
|
'WorkflowName not provided. Attempting to find workflow run by runId using GSI. Ensure GSI (e.g., "byRunId") is defined on the workflowSnapshot entity with run_id as its key and provisioned in DynamoDB.'
|
|
1393
2614
|
);
|
|
1394
|
-
const result = await this.service.entities.
|
|
2615
|
+
const result = await this.service.entities.workflow_snapshot.query.gsi2({ entity: "workflow_snapshot", run_id: runId }).go();
|
|
1395
2616
|
const matchingRunDbItem = result.data && result.data.length > 0 ? result.data[0] : null;
|
|
1396
2617
|
if (!matchingRunDbItem) {
|
|
1397
2618
|
return null;
|
|
@@ -1417,121 +2638,260 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1417
2638
|
);
|
|
1418
2639
|
}
|
|
1419
2640
|
}
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
2641
|
+
};
|
|
2642
|
+
|
|
2643
|
+
// src/storage/index.ts
|
|
2644
|
+
var DynamoDBStore = class extends MastraStorage {
|
|
2645
|
+
tableName;
|
|
2646
|
+
client;
|
|
2647
|
+
service;
|
|
2648
|
+
hasInitialized = null;
|
|
2649
|
+
stores;
|
|
2650
|
+
constructor({ name, config }) {
|
|
2651
|
+
super({ name });
|
|
2652
|
+
try {
|
|
2653
|
+
if (!config.tableName || typeof config.tableName !== "string" || config.tableName.trim() === "") {
|
|
2654
|
+
throw new Error("DynamoDBStore: config.tableName must be provided and cannot be empty.");
|
|
2655
|
+
}
|
|
2656
|
+
if (!/^[a-zA-Z0-9_.-]{3,255}$/.test(config.tableName)) {
|
|
2657
|
+
throw new Error(
|
|
2658
|
+
`DynamoDBStore: config.tableName "${config.tableName}" contains invalid characters or is not between 3 and 255 characters long.`
|
|
2659
|
+
);
|
|
2660
|
+
}
|
|
2661
|
+
const dynamoClient = new DynamoDBClient({
|
|
2662
|
+
region: config.region || "us-east-1",
|
|
2663
|
+
endpoint: config.endpoint,
|
|
2664
|
+
credentials: config.credentials
|
|
2665
|
+
});
|
|
2666
|
+
this.tableName = config.tableName;
|
|
2667
|
+
this.client = DynamoDBDocumentClient.from(dynamoClient);
|
|
2668
|
+
this.service = getElectroDbService(this.client, this.tableName);
|
|
2669
|
+
const operations = new StoreOperationsDynamoDB({
|
|
2670
|
+
service: this.service,
|
|
2671
|
+
tableName: this.tableName,
|
|
2672
|
+
client: this.client
|
|
2673
|
+
});
|
|
2674
|
+
const workflows = new WorkflowStorageDynamoDB({ service: this.service });
|
|
2675
|
+
const memory = new MemoryStorageDynamoDB({ service: this.service });
|
|
2676
|
+
const scores = new ScoresStorageDynamoDB({ service: this.service });
|
|
2677
|
+
this.stores = {
|
|
2678
|
+
operations,
|
|
2679
|
+
legacyEvals: new LegacyEvalsDynamoDB({ service: this.service, tableName: this.tableName }),
|
|
2680
|
+
workflows,
|
|
2681
|
+
memory,
|
|
2682
|
+
scores
|
|
2683
|
+
};
|
|
2684
|
+
} catch (error) {
|
|
2685
|
+
throw new MastraError(
|
|
2686
|
+
{
|
|
2687
|
+
id: "STORAGE_DYNAMODB_STORE_CONSTRUCTOR_FAILED",
|
|
2688
|
+
domain: ErrorDomain.STORAGE,
|
|
2689
|
+
category: ErrorCategory.USER
|
|
2690
|
+
},
|
|
2691
|
+
error
|
|
2692
|
+
);
|
|
2693
|
+
}
|
|
1430
2694
|
}
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
2695
|
+
get supports() {
|
|
2696
|
+
return {
|
|
2697
|
+
selectByIncludeResourceScope: true,
|
|
2698
|
+
resourceWorkingMemory: true,
|
|
2699
|
+
hasColumn: false,
|
|
2700
|
+
createTable: false,
|
|
2701
|
+
deleteMessages: false,
|
|
2702
|
+
getScoresBySpan: true
|
|
1439
2703
|
};
|
|
1440
|
-
return mapping[tableName] || null;
|
|
1441
2704
|
}
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
2705
|
+
/**
|
|
2706
|
+
* Validates that the required DynamoDB table exists and is accessible.
|
|
2707
|
+
* This does not check the table structure - it assumes the table
|
|
2708
|
+
* was created with the correct structure via CDK/CloudFormation.
|
|
2709
|
+
*/
|
|
2710
|
+
async validateTableExists() {
|
|
1445
2711
|
try {
|
|
1446
|
-
const
|
|
1447
|
-
|
|
1448
|
-
if (!results.data.length) {
|
|
1449
|
-
return [];
|
|
1450
|
-
}
|
|
1451
|
-
let filteredData = results.data;
|
|
1452
|
-
if (type) {
|
|
1453
|
-
filteredData = filteredData.filter((evalRecord) => {
|
|
1454
|
-
try {
|
|
1455
|
-
const testInfo = evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0;
|
|
1456
|
-
if (type === "test" && !testInfo) {
|
|
1457
|
-
return false;
|
|
1458
|
-
}
|
|
1459
|
-
if (type === "live" && testInfo) {
|
|
1460
|
-
return false;
|
|
1461
|
-
}
|
|
1462
|
-
} catch (e) {
|
|
1463
|
-
this.logger.warn("Failed to parse test_info during filtering", { record: evalRecord, error: e });
|
|
1464
|
-
}
|
|
1465
|
-
return true;
|
|
1466
|
-
});
|
|
1467
|
-
}
|
|
1468
|
-
return filteredData.map((evalRecord) => {
|
|
1469
|
-
try {
|
|
1470
|
-
return {
|
|
1471
|
-
input: evalRecord.input,
|
|
1472
|
-
output: evalRecord.output,
|
|
1473
|
-
// Safely parse result and test_info
|
|
1474
|
-
result: evalRecord.result && typeof evalRecord.result === "string" ? JSON.parse(evalRecord.result) : void 0,
|
|
1475
|
-
agentName: evalRecord.agent_name,
|
|
1476
|
-
createdAt: evalRecord.created_at,
|
|
1477
|
-
// Keep as string from DDB?
|
|
1478
|
-
metricName: evalRecord.metric_name,
|
|
1479
|
-
instructions: evalRecord.instructions,
|
|
1480
|
-
runId: evalRecord.run_id,
|
|
1481
|
-
globalRunId: evalRecord.global_run_id,
|
|
1482
|
-
testInfo: evalRecord.test_info && typeof evalRecord.test_info === "string" ? JSON.parse(evalRecord.test_info) : void 0
|
|
1483
|
-
};
|
|
1484
|
-
} catch (parseError) {
|
|
1485
|
-
this.logger.error("Failed to parse eval record", { record: evalRecord, error: parseError });
|
|
1486
|
-
return {
|
|
1487
|
-
agentName: evalRecord.agent_name,
|
|
1488
|
-
createdAt: evalRecord.created_at,
|
|
1489
|
-
runId: evalRecord.run_id,
|
|
1490
|
-
globalRunId: evalRecord.global_run_id
|
|
1491
|
-
};
|
|
1492
|
-
}
|
|
2712
|
+
const command = new DescribeTableCommand({
|
|
2713
|
+
TableName: this.tableName
|
|
1493
2714
|
});
|
|
2715
|
+
await this.client.send(command);
|
|
2716
|
+
return true;
|
|
1494
2717
|
} catch (error) {
|
|
2718
|
+
if (error.name === "ResourceNotFoundException") {
|
|
2719
|
+
return false;
|
|
2720
|
+
}
|
|
1495
2721
|
throw new MastraError(
|
|
1496
2722
|
{
|
|
1497
|
-
id: "
|
|
2723
|
+
id: "STORAGE_DYNAMODB_STORE_VALIDATE_TABLE_EXISTS_FAILED",
|
|
1498
2724
|
domain: ErrorDomain.STORAGE,
|
|
1499
2725
|
category: ErrorCategory.THIRD_PARTY,
|
|
1500
|
-
details: {
|
|
2726
|
+
details: { tableName: this.tableName }
|
|
1501
2727
|
},
|
|
1502
2728
|
error
|
|
1503
2729
|
);
|
|
1504
2730
|
}
|
|
1505
2731
|
}
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
2732
|
+
/**
|
|
2733
|
+
* Initialize storage, validating the externally managed table is accessible.
|
|
2734
|
+
* For the single-table design, we only validate once that we can access
|
|
2735
|
+
* the table that was created via CDK/CloudFormation.
|
|
2736
|
+
*/
|
|
2737
|
+
async init() {
|
|
2738
|
+
if (this.hasInitialized === null) {
|
|
2739
|
+
this.hasInitialized = this._performInitializationAndStore();
|
|
2740
|
+
}
|
|
2741
|
+
try {
|
|
2742
|
+
await this.hasInitialized;
|
|
2743
|
+
} catch (error) {
|
|
2744
|
+
throw new MastraError(
|
|
2745
|
+
{
|
|
2746
|
+
id: "STORAGE_DYNAMODB_STORE_INIT_FAILED",
|
|
2747
|
+
domain: ErrorDomain.STORAGE,
|
|
2748
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
2749
|
+
details: { tableName: this.tableName }
|
|
2750
|
+
},
|
|
2751
|
+
error
|
|
2752
|
+
);
|
|
2753
|
+
}
|
|
1515
2754
|
}
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
2755
|
+
/**
|
|
2756
|
+
* Performs the actual table validation and stores the promise.
|
|
2757
|
+
* Handles resetting the stored promise on failure to allow retries.
|
|
2758
|
+
*/
|
|
2759
|
+
_performInitializationAndStore() {
|
|
2760
|
+
return this.validateTableExists().then((exists) => {
|
|
2761
|
+
if (!exists) {
|
|
2762
|
+
throw new Error(
|
|
2763
|
+
`Table ${this.tableName} does not exist or is not accessible. Ensure it's created via CDK/CloudFormation before using this store.`
|
|
2764
|
+
);
|
|
2765
|
+
}
|
|
2766
|
+
return true;
|
|
2767
|
+
}).catch((err) => {
|
|
2768
|
+
this.hasInitialized = null;
|
|
2769
|
+
throw err;
|
|
2770
|
+
});
|
|
1525
2771
|
}
|
|
1526
|
-
async
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
2772
|
+
async createTable({ tableName, schema }) {
|
|
2773
|
+
return this.stores.operations.createTable({ tableName, schema });
|
|
2774
|
+
}
|
|
2775
|
+
async alterTable(_args) {
|
|
2776
|
+
return this.stores.operations.alterTable(_args);
|
|
2777
|
+
}
|
|
2778
|
+
async clearTable({ tableName }) {
|
|
2779
|
+
return this.stores.operations.clearTable({ tableName });
|
|
2780
|
+
}
|
|
2781
|
+
async dropTable({ tableName }) {
|
|
2782
|
+
return this.stores.operations.dropTable({ tableName });
|
|
2783
|
+
}
|
|
2784
|
+
async insert({ tableName, record }) {
|
|
2785
|
+
return this.stores.operations.insert({ tableName, record });
|
|
2786
|
+
}
|
|
2787
|
+
async batchInsert({ tableName, records }) {
|
|
2788
|
+
return this.stores.operations.batchInsert({ tableName, records });
|
|
2789
|
+
}
|
|
2790
|
+
async load({ tableName, keys }) {
|
|
2791
|
+
return this.stores.operations.load({ tableName, keys });
|
|
2792
|
+
}
|
|
2793
|
+
// Thread operations
|
|
2794
|
+
async getThreadById({ threadId }) {
|
|
2795
|
+
return this.stores.memory.getThreadById({ threadId });
|
|
2796
|
+
}
|
|
2797
|
+
async getThreadsByResourceId(args) {
|
|
2798
|
+
return this.stores.memory.getThreadsByResourceId(args);
|
|
2799
|
+
}
|
|
2800
|
+
async saveThread({ thread }) {
|
|
2801
|
+
return this.stores.memory.saveThread({ thread });
|
|
2802
|
+
}
|
|
2803
|
+
async updateThread({
|
|
2804
|
+
id,
|
|
2805
|
+
title,
|
|
2806
|
+
metadata
|
|
2807
|
+
}) {
|
|
2808
|
+
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2809
|
+
}
|
|
2810
|
+
async deleteThread({ threadId }) {
|
|
2811
|
+
return this.stores.memory.deleteThread({ threadId });
|
|
2812
|
+
}
|
|
2813
|
+
async getMessages({
|
|
2814
|
+
threadId,
|
|
2815
|
+
resourceId,
|
|
2816
|
+
selectBy,
|
|
2817
|
+
format
|
|
2818
|
+
}) {
|
|
2819
|
+
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format });
|
|
2820
|
+
}
|
|
2821
|
+
async getMessagesById({
|
|
2822
|
+
messageIds,
|
|
2823
|
+
format
|
|
2824
|
+
}) {
|
|
2825
|
+
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2826
|
+
}
|
|
2827
|
+
async saveMessages(args) {
|
|
2828
|
+
return this.stores.memory.saveMessages(args);
|
|
2829
|
+
}
|
|
2830
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
2831
|
+
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2832
|
+
}
|
|
2833
|
+
async getMessagesPaginated(args) {
|
|
2834
|
+
return this.stores.memory.getMessagesPaginated(args);
|
|
2835
|
+
}
|
|
2836
|
+
async updateMessages(_args) {
|
|
2837
|
+
return this.stores.memory.updateMessages(_args);
|
|
2838
|
+
}
|
|
2839
|
+
// Workflow operations
|
|
2840
|
+
async updateWorkflowResults({
|
|
2841
|
+
workflowName,
|
|
2842
|
+
runId,
|
|
2843
|
+
stepId,
|
|
2844
|
+
result,
|
|
2845
|
+
runtimeContext
|
|
2846
|
+
}) {
|
|
2847
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
|
|
2848
|
+
}
|
|
2849
|
+
async updateWorkflowState({
|
|
2850
|
+
workflowName,
|
|
2851
|
+
runId,
|
|
2852
|
+
opts
|
|
2853
|
+
}) {
|
|
2854
|
+
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2855
|
+
}
|
|
2856
|
+
async persistWorkflowSnapshot({
|
|
2857
|
+
workflowName,
|
|
2858
|
+
runId,
|
|
2859
|
+
resourceId,
|
|
2860
|
+
snapshot
|
|
2861
|
+
}) {
|
|
2862
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2863
|
+
}
|
|
2864
|
+
async loadWorkflowSnapshot({
|
|
2865
|
+
workflowName,
|
|
2866
|
+
runId
|
|
2867
|
+
}) {
|
|
2868
|
+
return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
2869
|
+
}
|
|
2870
|
+
async getWorkflowRuns(args) {
|
|
2871
|
+
return this.stores.workflows.getWorkflowRuns(args);
|
|
2872
|
+
}
|
|
2873
|
+
async getWorkflowRunById(args) {
|
|
2874
|
+
return this.stores.workflows.getWorkflowRunById(args);
|
|
2875
|
+
}
|
|
2876
|
+
async getResourceById({ resourceId }) {
|
|
2877
|
+
return this.stores.memory.getResourceById({ resourceId });
|
|
2878
|
+
}
|
|
2879
|
+
async saveResource({ resource }) {
|
|
2880
|
+
return this.stores.memory.saveResource({ resource });
|
|
2881
|
+
}
|
|
2882
|
+
async updateResource({
|
|
2883
|
+
resourceId,
|
|
2884
|
+
workingMemory,
|
|
2885
|
+
metadata
|
|
2886
|
+
}) {
|
|
2887
|
+
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
2888
|
+
}
|
|
2889
|
+
// Eval operations
|
|
2890
|
+
async getEvalsByAgentName(agentName, type) {
|
|
2891
|
+
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2892
|
+
}
|
|
2893
|
+
async getEvals(options) {
|
|
2894
|
+
return this.stores.legacyEvals.getEvals(options);
|
|
1535
2895
|
}
|
|
1536
2896
|
/**
|
|
1537
2897
|
* Closes the DynamoDB client connection and cleans up resources.
|
|
@@ -1553,10 +2913,50 @@ var DynamoDBStore = class extends MastraStorage {
|
|
|
1553
2913
|
);
|
|
1554
2914
|
}
|
|
1555
2915
|
}
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
2916
|
+
/**
|
|
2917
|
+
* SCORERS - Not implemented
|
|
2918
|
+
*/
|
|
2919
|
+
async getScoreById({ id: _id }) {
|
|
2920
|
+
return this.stores.scores.getScoreById({ id: _id });
|
|
2921
|
+
}
|
|
2922
|
+
async saveScore(_score) {
|
|
2923
|
+
return this.stores.scores.saveScore(_score);
|
|
2924
|
+
}
|
|
2925
|
+
async getScoresByRunId({
|
|
2926
|
+
runId: _runId,
|
|
2927
|
+
pagination: _pagination
|
|
2928
|
+
}) {
|
|
2929
|
+
return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2930
|
+
}
|
|
2931
|
+
async getScoresByEntityId({
|
|
2932
|
+
entityId: _entityId,
|
|
2933
|
+
entityType: _entityType,
|
|
2934
|
+
pagination: _pagination
|
|
2935
|
+
}) {
|
|
2936
|
+
return this.stores.scores.getScoresByEntityId({
|
|
2937
|
+
entityId: _entityId,
|
|
2938
|
+
entityType: _entityType,
|
|
2939
|
+
pagination: _pagination
|
|
2940
|
+
});
|
|
2941
|
+
}
|
|
2942
|
+
async getScoresByScorerId({
|
|
2943
|
+
scorerId,
|
|
2944
|
+
source,
|
|
2945
|
+
entityId,
|
|
2946
|
+
entityType,
|
|
2947
|
+
pagination
|
|
2948
|
+
}) {
|
|
2949
|
+
return this.stores.scores.getScoresByScorerId({ scorerId, source, entityId, entityType, pagination });
|
|
2950
|
+
}
|
|
2951
|
+
async getScoresBySpan({
|
|
2952
|
+
traceId,
|
|
2953
|
+
spanId,
|
|
2954
|
+
pagination
|
|
2955
|
+
}) {
|
|
2956
|
+
return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
|
|
1559
2957
|
}
|
|
1560
2958
|
};
|
|
1561
2959
|
|
|
1562
2960
|
export { DynamoDBStore };
|
|
2961
|
+
//# sourceMappingURL=index.js.map
|
|
2962
|
+
//# sourceMappingURL=index.js.map
|