@mastra/memory 0.0.1 → 0.0.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/dist/index.d.ts +0 -2
- package/dist/memory.cjs.development.js +599 -1864
- package/dist/memory.cjs.development.js.map +1 -1
- package/dist/memory.cjs.production.min.js +1 -1
- package/dist/memory.cjs.production.min.js.map +1 -1
- package/dist/memory.esm.js +600 -1861
- package/dist/memory.esm.js.map +1 -1
- package/dist/postgres/index.d.ts +40 -7
- package/package.json +3 -2
- package/src/index.ts +0 -2
- package/src/postgres/index.test.ts +20 -12
- package/src/postgres/index.ts +391 -56
- package/dist/cloudflare/index.d.ts +0 -18
- package/dist/cloudflare/kv.d.ts +0 -53
- package/dist/redis/index.d.ts +0 -22
- package/dist/redis/providers.d.ts +0 -26
- package/dist/redis/types.d.ts +0 -17
- package/src/cloudflare/index.test.ts +0 -230
- package/src/cloudflare/index.ts +0 -169
- package/src/cloudflare/kv.ts +0 -139
- package/src/redis/index.test.ts +0 -245
- package/src/redis/index.ts +0 -189
- package/src/redis/providers.ts +0 -191
- package/src/redis/types.ts +0 -18
package/dist/memory.esm.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { MastraMemory } from '@mastra/core';
|
|
2
|
-
import
|
|
2
|
+
import crypto from 'crypto';
|
|
3
3
|
import pg from 'pg';
|
|
4
|
-
import { Redis } from '@upstash/redis';
|
|
5
|
-
import { createClient } from 'redis';
|
|
6
4
|
|
|
7
5
|
function _arrayLikeToArray(r, a) {
|
|
8
6
|
(null == a || a > r.length) && (a = r.length);
|
|
@@ -377,1985 +375,726 @@ function _unsupportedIterableToArray(r, a) {
|
|
|
377
375
|
}
|
|
378
376
|
}
|
|
379
377
|
|
|
380
|
-
var
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
390
|
-
case 0:
|
|
391
|
-
return _context5.abrupt("return", this.namespace.get(key, 'json'));
|
|
392
|
-
case 1:
|
|
393
|
-
case "end":
|
|
394
|
-
return _context5.stop();
|
|
395
|
-
}
|
|
396
|
-
}, _callee5, this);
|
|
397
|
-
}));
|
|
398
|
-
function get(_x7) {
|
|
399
|
-
return _get2.apply(this, arguments);
|
|
400
|
-
}
|
|
401
|
-
return get;
|
|
402
|
-
}();
|
|
403
|
-
_proto2.set = /*#__PURE__*/function () {
|
|
404
|
-
var _set = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(key, value) {
|
|
405
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
406
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
407
|
-
case 0:
|
|
408
|
-
_context6.next = 2;
|
|
409
|
-
return this.namespace.put(key, JSON.stringify(value));
|
|
410
|
-
case 2:
|
|
411
|
-
case "end":
|
|
412
|
-
return _context6.stop();
|
|
413
|
-
}
|
|
414
|
-
}, _callee6, this);
|
|
415
|
-
}));
|
|
416
|
-
function set(_x8, _x9) {
|
|
417
|
-
return _set.apply(this, arguments);
|
|
418
|
-
}
|
|
419
|
-
return set;
|
|
420
|
-
}();
|
|
421
|
-
_proto2.del = /*#__PURE__*/function () {
|
|
422
|
-
var _del = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(key) {
|
|
423
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
424
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
425
|
-
case 0:
|
|
426
|
-
_context7.next = 2;
|
|
427
|
-
return this.namespace["delete"](key);
|
|
428
|
-
case 2:
|
|
429
|
-
case "end":
|
|
430
|
-
return _context7.stop();
|
|
431
|
-
}
|
|
432
|
-
}, _callee7, this);
|
|
433
|
-
}));
|
|
434
|
-
function del(_x10) {
|
|
435
|
-
return _del.apply(this, arguments);
|
|
436
|
-
}
|
|
437
|
-
return del;
|
|
438
|
-
}();
|
|
439
|
-
_proto2.sadd = /*#__PURE__*/function () {
|
|
440
|
-
var _sadd = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(key, value) {
|
|
441
|
-
var set;
|
|
442
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
443
|
-
while (1) switch (_context8.prev = _context8.next) {
|
|
444
|
-
case 0:
|
|
445
|
-
_context8.next = 2;
|
|
446
|
-
return this.get(key);
|
|
447
|
-
case 2:
|
|
448
|
-
_context8.t0 = _context8.sent;
|
|
449
|
-
if (_context8.t0) {
|
|
450
|
-
_context8.next = 5;
|
|
451
|
-
break;
|
|
452
|
-
}
|
|
453
|
-
_context8.t0 = [];
|
|
454
|
-
case 5:
|
|
455
|
-
set = _context8.t0;
|
|
456
|
-
if (set.includes(value)) {
|
|
457
|
-
_context8.next = 11;
|
|
458
|
-
break;
|
|
459
|
-
}
|
|
460
|
-
set.push(value);
|
|
461
|
-
_context8.next = 10;
|
|
462
|
-
return this.set(key, set);
|
|
463
|
-
case 10:
|
|
464
|
-
return _context8.abrupt("return", 1);
|
|
465
|
-
case 11:
|
|
466
|
-
return _context8.abrupt("return", 0);
|
|
467
|
-
case 12:
|
|
468
|
-
case "end":
|
|
469
|
-
return _context8.stop();
|
|
470
|
-
}
|
|
471
|
-
}, _callee8, this);
|
|
472
|
-
}));
|
|
473
|
-
function sadd(_x11, _x12) {
|
|
474
|
-
return _sadd.apply(this, arguments);
|
|
475
|
-
}
|
|
476
|
-
return sadd;
|
|
477
|
-
}();
|
|
478
|
-
_proto2.srem = /*#__PURE__*/function () {
|
|
479
|
-
var _srem = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(key, value) {
|
|
480
|
-
var set, index;
|
|
481
|
-
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
482
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
483
|
-
case 0:
|
|
484
|
-
_context9.next = 2;
|
|
485
|
-
return this.get(key);
|
|
486
|
-
case 2:
|
|
487
|
-
_context9.t0 = _context9.sent;
|
|
488
|
-
if (_context9.t0) {
|
|
489
|
-
_context9.next = 5;
|
|
490
|
-
break;
|
|
491
|
-
}
|
|
492
|
-
_context9.t0 = [];
|
|
493
|
-
case 5:
|
|
494
|
-
set = _context9.t0;
|
|
495
|
-
index = set.indexOf(value);
|
|
496
|
-
if (!(index !== -1)) {
|
|
497
|
-
_context9.next = 12;
|
|
498
|
-
break;
|
|
499
|
-
}
|
|
500
|
-
set.splice(index, 1);
|
|
501
|
-
_context9.next = 11;
|
|
502
|
-
return this.set(key, set);
|
|
503
|
-
case 11:
|
|
504
|
-
return _context9.abrupt("return", 1);
|
|
505
|
-
case 12:
|
|
506
|
-
return _context9.abrupt("return", 0);
|
|
507
|
-
case 13:
|
|
508
|
-
case "end":
|
|
509
|
-
return _context9.stop();
|
|
510
|
-
}
|
|
511
|
-
}, _callee9, this);
|
|
512
|
-
}));
|
|
513
|
-
function srem(_x13, _x14) {
|
|
514
|
-
return _srem.apply(this, arguments);
|
|
515
|
-
}
|
|
516
|
-
return srem;
|
|
517
|
-
}();
|
|
518
|
-
_proto2.smembers = /*#__PURE__*/function () {
|
|
519
|
-
var _smembers = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(key) {
|
|
520
|
-
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
521
|
-
while (1) switch (_context10.prev = _context10.next) {
|
|
522
|
-
case 0:
|
|
523
|
-
_context10.next = 2;
|
|
524
|
-
return this.get(key);
|
|
525
|
-
case 2:
|
|
526
|
-
_context10.t0 = _context10.sent;
|
|
527
|
-
if (_context10.t0) {
|
|
528
|
-
_context10.next = 5;
|
|
529
|
-
break;
|
|
530
|
-
}
|
|
531
|
-
_context10.t0 = [];
|
|
532
|
-
case 5:
|
|
533
|
-
return _context10.abrupt("return", _context10.t0);
|
|
534
|
-
case 6:
|
|
535
|
-
case "end":
|
|
536
|
-
return _context10.stop();
|
|
537
|
-
}
|
|
538
|
-
}, _callee10, this);
|
|
539
|
-
}));
|
|
540
|
-
function smembers(_x15) {
|
|
541
|
-
return _smembers.apply(this, arguments);
|
|
542
|
-
}
|
|
543
|
-
return smembers;
|
|
544
|
-
}();
|
|
545
|
-
_proto2.flushall = /*#__PURE__*/function () {
|
|
546
|
-
var _flushall = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
|
|
547
|
-
var _this = this;
|
|
548
|
-
var cursor, result;
|
|
549
|
-
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
550
|
-
while (1) switch (_context11.prev = _context11.next) {
|
|
551
|
-
case 0:
|
|
552
|
-
_context11.next = 2;
|
|
553
|
-
return this.namespace.list({
|
|
554
|
-
cursor: cursor
|
|
555
|
-
});
|
|
556
|
-
case 2:
|
|
557
|
-
result = _context11.sent;
|
|
558
|
-
_context11.next = 5;
|
|
559
|
-
return Promise.all(result.keys.map(function (key) {
|
|
560
|
-
return _this.namespace["delete"](key.name);
|
|
561
|
-
}));
|
|
562
|
-
case 5:
|
|
563
|
-
cursor = result.cursor;
|
|
564
|
-
case 6:
|
|
565
|
-
if (cursor) {
|
|
566
|
-
_context11.next = 0;
|
|
567
|
-
break;
|
|
568
|
-
}
|
|
569
|
-
case 7:
|
|
570
|
-
case "end":
|
|
571
|
-
return _context11.stop();
|
|
572
|
-
}
|
|
573
|
-
}, _callee11, this);
|
|
574
|
-
}));
|
|
575
|
-
function flushall() {
|
|
576
|
-
return _flushall.apply(this, arguments);
|
|
577
|
-
}
|
|
578
|
-
return flushall;
|
|
579
|
-
}();
|
|
580
|
-
_proto2.getWithVersion = /*#__PURE__*/function () {
|
|
581
|
-
var _getWithVersion = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(key) {
|
|
582
|
-
var versionKey, _yield$Promise$all, data, version;
|
|
583
|
-
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
|
|
584
|
-
while (1) switch (_context12.prev = _context12.next) {
|
|
585
|
-
case 0:
|
|
586
|
-
versionKey = key + ":version";
|
|
587
|
-
_context12.next = 3;
|
|
588
|
-
return Promise.all([this.get(key), this.get(versionKey).then(function (v) {
|
|
589
|
-
return v || 0;
|
|
590
|
-
})]);
|
|
591
|
-
case 3:
|
|
592
|
-
_yield$Promise$all = _context12.sent;
|
|
593
|
-
data = _yield$Promise$all[0];
|
|
594
|
-
version = _yield$Promise$all[1];
|
|
595
|
-
return _context12.abrupt("return", {
|
|
596
|
-
data: data,
|
|
597
|
-
version: version
|
|
598
|
-
});
|
|
599
|
-
case 7:
|
|
600
|
-
case "end":
|
|
601
|
-
return _context12.stop();
|
|
602
|
-
}
|
|
603
|
-
}, _callee12, this);
|
|
604
|
-
}));
|
|
605
|
-
function getWithVersion(_x16) {
|
|
606
|
-
return _getWithVersion.apply(this, arguments);
|
|
607
|
-
}
|
|
608
|
-
return getWithVersion;
|
|
609
|
-
}();
|
|
610
|
-
_proto2.setWithVersion = /*#__PURE__*/function () {
|
|
611
|
-
var _setWithVersion = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13(key, value, expectedVersion) {
|
|
612
|
-
var versionKey, currentVersion;
|
|
613
|
-
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
|
|
614
|
-
while (1) switch (_context13.prev = _context13.next) {
|
|
615
|
-
case 0:
|
|
616
|
-
versionKey = key + ":version";
|
|
617
|
-
_context13.next = 3;
|
|
618
|
-
return this.get(versionKey);
|
|
619
|
-
case 3:
|
|
620
|
-
_context13.t0 = _context13.sent;
|
|
621
|
-
if (_context13.t0) {
|
|
622
|
-
_context13.next = 6;
|
|
623
|
-
break;
|
|
624
|
-
}
|
|
625
|
-
_context13.t0 = 0;
|
|
626
|
-
case 6:
|
|
627
|
-
currentVersion = _context13.t0;
|
|
628
|
-
if (!(currentVersion !== expectedVersion)) {
|
|
629
|
-
_context13.next = 9;
|
|
630
|
-
break;
|
|
631
|
-
}
|
|
632
|
-
return _context13.abrupt("return", false);
|
|
633
|
-
case 9:
|
|
634
|
-
_context13.next = 11;
|
|
635
|
-
return Promise.all([this.set(key, value), this.set(versionKey, expectedVersion + 1)]);
|
|
636
|
-
case 11:
|
|
637
|
-
return _context13.abrupt("return", true);
|
|
638
|
-
case 12:
|
|
639
|
-
case "end":
|
|
640
|
-
return _context13.stop();
|
|
641
|
-
}
|
|
642
|
-
}, _callee13, this);
|
|
643
|
-
}));
|
|
644
|
-
function setWithVersion(_x17, _x18, _x19) {
|
|
645
|
-
return _setWithVersion.apply(this, arguments);
|
|
646
|
-
}
|
|
647
|
-
return setWithVersion;
|
|
648
|
-
}();
|
|
649
|
-
return CloudflareKVProvider;
|
|
650
|
-
}();
|
|
651
|
-
|
|
652
|
-
var CloudflareKVMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
653
|
-
function CloudflareKVMemory(namespace) {
|
|
654
|
-
var _this;
|
|
655
|
-
_this = _MastraMemory.call(this) || this;
|
|
656
|
-
_this.kv = void 0;
|
|
657
|
-
_this.threadPrefix = 'thread:';
|
|
658
|
-
_this.messagePrefix = 'messages:';
|
|
659
|
-
_this.kv = new CloudflareKVProvider(namespace);
|
|
660
|
-
return _this;
|
|
661
|
-
}
|
|
662
|
-
_inheritsLoose(CloudflareKVMemory, _MastraMemory);
|
|
663
|
-
var _proto = CloudflareKVMemory.prototype;
|
|
664
|
-
_proto.getThreadById = /*#__PURE__*/function () {
|
|
665
|
-
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(threadId) {
|
|
666
|
-
var thread;
|
|
667
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
668
|
-
while (1) switch (_context.prev = _context.next) {
|
|
669
|
-
case 0:
|
|
670
|
-
_context.next = 2;
|
|
671
|
-
return this.kv.get("" + this.threadPrefix + threadId);
|
|
672
|
-
case 2:
|
|
673
|
-
thread = _context.sent;
|
|
674
|
-
if (thread && typeof thread.createdAt === 'string') {
|
|
675
|
-
thread.createdAt = new Date(thread.createdAt);
|
|
676
|
-
thread.updatedAt = new Date(thread.updatedAt);
|
|
677
|
-
}
|
|
678
|
-
return _context.abrupt("return", thread);
|
|
679
|
-
case 5:
|
|
680
|
-
case "end":
|
|
681
|
-
return _context.stop();
|
|
682
|
-
}
|
|
683
|
-
}, _callee, this);
|
|
684
|
-
}));
|
|
685
|
-
function getThreadById(_x) {
|
|
686
|
-
return _getThreadById.apply(this, arguments);
|
|
687
|
-
}
|
|
688
|
-
return getThreadById;
|
|
689
|
-
}();
|
|
690
|
-
_proto.saveThread = /*#__PURE__*/function () {
|
|
691
|
-
var _saveThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(thread) {
|
|
692
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
693
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
694
|
-
case 0:
|
|
695
|
-
thread.updatedAt = new Date();
|
|
696
|
-
_context2.next = 3;
|
|
697
|
-
return this.kv.set("" + this.threadPrefix + thread.id, thread);
|
|
698
|
-
case 3:
|
|
699
|
-
_context2.next = 5;
|
|
700
|
-
return this.kv.sadd('threads', thread.id);
|
|
701
|
-
case 5:
|
|
702
|
-
return _context2.abrupt("return", thread);
|
|
703
|
-
case 6:
|
|
704
|
-
case "end":
|
|
705
|
-
return _context2.stop();
|
|
706
|
-
}
|
|
707
|
-
}, _callee2, this);
|
|
708
|
-
}));
|
|
709
|
-
function saveThread(_x2) {
|
|
710
|
-
return _saveThread.apply(this, arguments);
|
|
711
|
-
}
|
|
712
|
-
return saveThread;
|
|
713
|
-
}();
|
|
714
|
-
_proto.retryOperation = /*#__PURE__*/function () {
|
|
715
|
-
var _retryOperation = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(operation, maxRetries) {
|
|
716
|
-
var lastError, _loop, _ret, attempt;
|
|
717
|
-
return _regeneratorRuntime().wrap(function _callee3$(_context4) {
|
|
718
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
719
|
-
case 0:
|
|
720
|
-
if (maxRetries === void 0) {
|
|
721
|
-
maxRetries = 5;
|
|
722
|
-
}
|
|
723
|
-
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop(attempt) {
|
|
724
|
-
return _regeneratorRuntime().wrap(function _loop$(_context3) {
|
|
725
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
726
|
-
case 0:
|
|
727
|
-
_context3.prev = 0;
|
|
728
|
-
_context3.next = 3;
|
|
729
|
-
return operation();
|
|
730
|
-
case 3:
|
|
731
|
-
_context3.t0 = _context3.sent;
|
|
732
|
-
return _context3.abrupt("return", {
|
|
733
|
-
v: _context3.t0
|
|
734
|
-
});
|
|
735
|
-
case 7:
|
|
736
|
-
_context3.prev = 7;
|
|
737
|
-
_context3.t1 = _context3["catch"](0);
|
|
738
|
-
lastError = _context3.t1;
|
|
739
|
-
_context3.next = 12;
|
|
740
|
-
return new Promise(function (resolve) {
|
|
741
|
-
return setTimeout(resolve, Math.random() * 100 * Math.pow(2, attempt));
|
|
742
|
-
});
|
|
743
|
-
case 12:
|
|
744
|
-
case "end":
|
|
745
|
-
return _context3.stop();
|
|
746
|
-
}
|
|
747
|
-
}, _loop, null, [[0, 7]]);
|
|
748
|
-
});
|
|
749
|
-
attempt = 0;
|
|
750
|
-
case 3:
|
|
751
|
-
if (!(attempt < maxRetries)) {
|
|
752
|
-
_context4.next = 11;
|
|
753
|
-
break;
|
|
754
|
-
}
|
|
755
|
-
return _context4.delegateYield(_loop(attempt), "t0", 5);
|
|
756
|
-
case 5:
|
|
757
|
-
_ret = _context4.t0;
|
|
758
|
-
if (!_ret) {
|
|
759
|
-
_context4.next = 8;
|
|
760
|
-
break;
|
|
761
|
-
}
|
|
762
|
-
return _context4.abrupt("return", _ret.v);
|
|
763
|
-
case 8:
|
|
764
|
-
attempt++;
|
|
765
|
-
_context4.next = 3;
|
|
766
|
-
break;
|
|
767
|
-
case 11:
|
|
768
|
-
throw lastError || new Error("Operation failed after " + maxRetries + " attempts");
|
|
769
|
-
case 12:
|
|
770
|
-
case "end":
|
|
771
|
-
return _context4.stop();
|
|
772
|
-
}
|
|
773
|
-
}, _callee3);
|
|
774
|
-
}));
|
|
775
|
-
function retryOperation(_x3, _x4) {
|
|
776
|
-
return _retryOperation.apply(this, arguments);
|
|
777
|
-
}
|
|
778
|
-
return retryOperation;
|
|
779
|
-
}();
|
|
780
|
-
_proto.addMessage = /*#__PURE__*/function () {
|
|
781
|
-
var _addMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(threadId, content, role) {
|
|
782
|
-
var _this2 = this;
|
|
783
|
-
var message;
|
|
784
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context6) {
|
|
785
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
786
|
-
case 0:
|
|
787
|
-
message = {
|
|
788
|
-
id: this.generateId(),
|
|
789
|
-
content: content,
|
|
790
|
-
role: role,
|
|
791
|
-
createdAt: new Date(),
|
|
792
|
-
threadId: threadId
|
|
793
|
-
};
|
|
794
|
-
_context6.next = 3;
|
|
795
|
-
return this.retryOperation(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
796
|
-
var key, existingMessages, messageMap, updatedMessages;
|
|
797
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context5) {
|
|
798
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
799
|
-
case 0:
|
|
800
|
-
key = "" + _this2.messagePrefix + threadId;
|
|
801
|
-
_context5.next = 3;
|
|
802
|
-
return _this2.kv.get(key);
|
|
803
|
-
case 3:
|
|
804
|
-
_context5.t0 = _context5.sent;
|
|
805
|
-
if (_context5.t0) {
|
|
806
|
-
_context5.next = 6;
|
|
807
|
-
break;
|
|
808
|
-
}
|
|
809
|
-
_context5.t0 = [];
|
|
810
|
-
case 6:
|
|
811
|
-
existingMessages = _context5.t0;
|
|
812
|
-
messageMap = new Map();
|
|
813
|
-
existingMessages.forEach(function (msg) {
|
|
814
|
-
messageMap.set(msg.id, _extends({}, msg, {
|
|
815
|
-
createdAt: new Date(msg.createdAt)
|
|
816
|
-
}));
|
|
817
|
-
});
|
|
818
|
-
messageMap.set(message.id, message);
|
|
819
|
-
updatedMessages = Array.from(messageMap.values());
|
|
820
|
-
updatedMessages.sort(function (a, b) {
|
|
821
|
-
var timeCompare = a.createdAt.getTime() - b.createdAt.getTime();
|
|
822
|
-
return timeCompare === 0 ? a.id.localeCompare(b.id) : timeCompare;
|
|
823
|
-
});
|
|
824
|
-
_context5.next = 14;
|
|
825
|
-
return _this2.kv.set(key, updatedMessages);
|
|
826
|
-
case 14:
|
|
827
|
-
case "end":
|
|
828
|
-
return _context5.stop();
|
|
829
|
-
}
|
|
830
|
-
}, _callee4);
|
|
831
|
-
})));
|
|
832
|
-
case 3:
|
|
833
|
-
return _context6.abrupt("return", message);
|
|
834
|
-
case 4:
|
|
835
|
-
case "end":
|
|
836
|
-
return _context6.stop();
|
|
837
|
-
}
|
|
838
|
-
}, _callee5, this);
|
|
839
|
-
}));
|
|
840
|
-
function addMessage(_x5, _x6, _x7) {
|
|
841
|
-
return _addMessage.apply(this, arguments);
|
|
842
|
-
}
|
|
843
|
-
return addMessage;
|
|
844
|
-
}();
|
|
845
|
-
_proto.saveMessages = /*#__PURE__*/function () {
|
|
846
|
-
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(messages) {
|
|
847
|
-
var _this3 = this;
|
|
848
|
-
var messagesByThread, _iterator, _step, message, key;
|
|
849
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context9) {
|
|
850
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
851
|
-
case 0:
|
|
852
|
-
if (messages.length) {
|
|
853
|
-
_context9.next = 2;
|
|
854
|
-
break;
|
|
855
|
-
}
|
|
856
|
-
return _context9.abrupt("return", []);
|
|
857
|
-
case 2:
|
|
858
|
-
messagesByThread = new Map();
|
|
859
|
-
for (_iterator = _createForOfIteratorHelperLoose(messages); !(_step = _iterator()).done;) {
|
|
860
|
-
message = _step.value;
|
|
861
|
-
key = "" + this.messagePrefix + message.threadId;
|
|
862
|
-
if (!messagesByThread.has(key)) {
|
|
863
|
-
messagesByThread.set(key, []);
|
|
864
|
-
}
|
|
865
|
-
messagesByThread.get(key).push(_extends({}, message, {
|
|
866
|
-
createdAt: new Date(message.createdAt)
|
|
867
|
-
}));
|
|
868
|
-
}
|
|
869
|
-
_context9.next = 6;
|
|
870
|
-
return Promise.all(Array.from(messagesByThread.entries()).map(function (_ref2) {
|
|
871
|
-
var key = _ref2[0],
|
|
872
|
-
threadMessages = _ref2[1];
|
|
873
|
-
return _this3.retryOperation(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
874
|
-
var saved, _loop2;
|
|
875
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context8) {
|
|
876
|
-
while (1) switch (_context8.prev = _context8.next) {
|
|
877
|
-
case 0:
|
|
878
|
-
saved = false;
|
|
879
|
-
_loop2 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop2() {
|
|
880
|
-
var _yield$_this3$kv$getW, existingMessages, version, messageMap, updatedMessages;
|
|
881
|
-
return _regeneratorRuntime().wrap(function _loop2$(_context7) {
|
|
882
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
883
|
-
case 0:
|
|
884
|
-
_context7.next = 2;
|
|
885
|
-
return _this3.kv.getWithVersion(key);
|
|
886
|
-
case 2:
|
|
887
|
-
_yield$_this3$kv$getW = _context7.sent;
|
|
888
|
-
existingMessages = _yield$_this3$kv$getW.data;
|
|
889
|
-
version = _yield$_this3$kv$getW.version;
|
|
890
|
-
console.log('Read version:', version, 'Messages:', (existingMessages == null ? void 0 : existingMessages.length) || 0);
|
|
891
|
-
messageMap = new Map();
|
|
892
|
-
(existingMessages || []).forEach(function (msg) {
|
|
893
|
-
messageMap.set(msg.id, _extends({}, msg, {
|
|
894
|
-
createdAt: new Date(msg.createdAt)
|
|
895
|
-
}));
|
|
896
|
-
});
|
|
897
|
-
threadMessages.forEach(function (msg) {
|
|
898
|
-
messageMap.set(msg.id, msg);
|
|
899
|
-
});
|
|
900
|
-
updatedMessages = Array.from(messageMap.values());
|
|
901
|
-
updatedMessages.sort(function (a, b) {
|
|
902
|
-
var timeCompare = a.createdAt.getTime() - b.createdAt.getTime();
|
|
903
|
-
return timeCompare === 0 ? a.id.localeCompare(b.id) : timeCompare;
|
|
904
|
-
});
|
|
905
|
-
_context7.next = 13;
|
|
906
|
-
return _this3.kv.setWithVersion(key, updatedMessages, version);
|
|
907
|
-
case 13:
|
|
908
|
-
saved = _context7.sent;
|
|
909
|
-
console.log('Save attempt with version:', version, 'Success:', saved);
|
|
910
|
-
if (saved) {
|
|
911
|
-
_context7.next = 18;
|
|
912
|
-
break;
|
|
913
|
-
}
|
|
914
|
-
_context7.next = 18;
|
|
915
|
-
return new Promise(function (resolve) {
|
|
916
|
-
return setTimeout(resolve, Math.random() * 50);
|
|
917
|
-
});
|
|
918
|
-
case 18:
|
|
919
|
-
case "end":
|
|
920
|
-
return _context7.stop();
|
|
921
|
-
}
|
|
922
|
-
}, _loop2);
|
|
923
|
-
});
|
|
924
|
-
case 2:
|
|
925
|
-
if (saved) {
|
|
926
|
-
_context8.next = 6;
|
|
927
|
-
break;
|
|
928
|
-
}
|
|
929
|
-
return _context8.delegateYield(_loop2(), "t0", 4);
|
|
930
|
-
case 4:
|
|
931
|
-
_context8.next = 2;
|
|
932
|
-
break;
|
|
933
|
-
case 6:
|
|
934
|
-
case "end":
|
|
935
|
-
return _context8.stop();
|
|
936
|
-
}
|
|
937
|
-
}, _callee6);
|
|
938
|
-
})));
|
|
939
|
-
}));
|
|
940
|
-
case 6:
|
|
941
|
-
return _context9.abrupt("return", messages);
|
|
942
|
-
case 7:
|
|
943
|
-
case "end":
|
|
944
|
-
return _context9.stop();
|
|
945
|
-
}
|
|
946
|
-
}, _callee7, this);
|
|
947
|
-
}));
|
|
948
|
-
function saveMessages(_x8) {
|
|
949
|
-
return _saveMessages.apply(this, arguments);
|
|
950
|
-
}
|
|
951
|
-
return saveMessages;
|
|
952
|
-
}();
|
|
953
|
-
_proto.getMessages = /*#__PURE__*/function () {
|
|
954
|
-
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(threadId) {
|
|
955
|
-
var messages;
|
|
956
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context10) {
|
|
957
|
-
while (1) switch (_context10.prev = _context10.next) {
|
|
958
|
-
case 0:
|
|
959
|
-
_context10.next = 2;
|
|
960
|
-
return this.kv.get("" + this.messagePrefix + threadId);
|
|
961
|
-
case 2:
|
|
962
|
-
_context10.t0 = _context10.sent;
|
|
963
|
-
if (_context10.t0) {
|
|
964
|
-
_context10.next = 5;
|
|
965
|
-
break;
|
|
966
|
-
}
|
|
967
|
-
_context10.t0 = [];
|
|
968
|
-
case 5:
|
|
969
|
-
messages = _context10.t0;
|
|
970
|
-
return _context10.abrupt("return", messages.map(function (msg) {
|
|
971
|
-
return _extends({}, msg, {
|
|
972
|
-
createdAt: new Date(msg.createdAt)
|
|
973
|
-
});
|
|
974
|
-
}));
|
|
975
|
-
case 7:
|
|
976
|
-
case "end":
|
|
977
|
-
return _context10.stop();
|
|
978
|
-
}
|
|
979
|
-
}, _callee8, this);
|
|
980
|
-
}));
|
|
981
|
-
function getMessages(_x9) {
|
|
982
|
-
return _getMessages.apply(this, arguments);
|
|
983
|
-
}
|
|
984
|
-
return getMessages;
|
|
985
|
-
}();
|
|
986
|
-
_proto.getAllThreadIds = /*#__PURE__*/function () {
|
|
987
|
-
var _getAllThreadIds = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
|
|
988
|
-
return _regeneratorRuntime().wrap(function _callee9$(_context11) {
|
|
989
|
-
while (1) switch (_context11.prev = _context11.next) {
|
|
990
|
-
case 0:
|
|
991
|
-
return _context11.abrupt("return", this.kv.smembers('threads'));
|
|
992
|
-
case 1:
|
|
993
|
-
case "end":
|
|
994
|
-
return _context11.stop();
|
|
995
|
-
}
|
|
996
|
-
}, _callee9, this);
|
|
997
|
-
}));
|
|
998
|
-
function getAllThreadIds() {
|
|
999
|
-
return _getAllThreadIds.apply(this, arguments);
|
|
1000
|
-
}
|
|
1001
|
-
return getAllThreadIds;
|
|
1002
|
-
}();
|
|
1003
|
-
_proto.deleteThread = /*#__PURE__*/function () {
|
|
1004
|
-
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(threadId) {
|
|
1005
|
-
return _regeneratorRuntime().wrap(function _callee10$(_context12) {
|
|
1006
|
-
while (1) switch (_context12.prev = _context12.next) {
|
|
1007
|
-
case 0:
|
|
1008
|
-
_context12.next = 2;
|
|
1009
|
-
return Promise.all([this.kv.del("" + this.threadPrefix + threadId), this.kv.del("" + this.messagePrefix + threadId), this.kv.srem('threads', threadId)]);
|
|
1010
|
-
case 2:
|
|
1011
|
-
case "end":
|
|
1012
|
-
return _context12.stop();
|
|
1013
|
-
}
|
|
1014
|
-
}, _callee10, this);
|
|
1015
|
-
}));
|
|
1016
|
-
function deleteThread(_x10) {
|
|
1017
|
-
return _deleteThread.apply(this, arguments);
|
|
1018
|
-
}
|
|
1019
|
-
return deleteThread;
|
|
1020
|
-
}();
|
|
1021
|
-
_proto.getThreads = /*#__PURE__*/function () {
|
|
1022
|
-
var _getThreads = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(threadIds) {
|
|
1023
|
-
var _this4 = this;
|
|
1024
|
-
var threads;
|
|
1025
|
-
return _regeneratorRuntime().wrap(function _callee11$(_context13) {
|
|
1026
|
-
while (1) switch (_context13.prev = _context13.next) {
|
|
1027
|
-
case 0:
|
|
1028
|
-
_context13.next = 2;
|
|
1029
|
-
return Promise.all(threadIds.map(function (id) {
|
|
1030
|
-
return _this4.getThreadById(id);
|
|
1031
|
-
}));
|
|
1032
|
-
case 2:
|
|
1033
|
-
threads = _context13.sent;
|
|
1034
|
-
return _context13.abrupt("return", threads.filter(function (t) {
|
|
1035
|
-
return t !== null;
|
|
1036
|
-
}));
|
|
1037
|
-
case 4:
|
|
1038
|
-
case "end":
|
|
1039
|
-
return _context13.stop();
|
|
1040
|
-
}
|
|
1041
|
-
}, _callee11);
|
|
1042
|
-
}));
|
|
1043
|
-
function getThreads(_x11) {
|
|
1044
|
-
return _getThreads.apply(this, arguments);
|
|
1045
|
-
}
|
|
1046
|
-
return getThreads;
|
|
1047
|
-
}();
|
|
1048
|
-
_proto.cleanup = /*#__PURE__*/function () {
|
|
1049
|
-
var _cleanup = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
|
|
1050
|
-
return _regeneratorRuntime().wrap(function _callee12$(_context14) {
|
|
1051
|
-
while (1) switch (_context14.prev = _context14.next) {
|
|
1052
|
-
case 0:
|
|
1053
|
-
_context14.next = 2;
|
|
1054
|
-
return this.kv.flushall();
|
|
1055
|
-
case 2:
|
|
1056
|
-
case "end":
|
|
1057
|
-
return _context14.stop();
|
|
1058
|
-
}
|
|
1059
|
-
}, _callee12, this);
|
|
1060
|
-
}));
|
|
1061
|
-
function cleanup() {
|
|
1062
|
-
return _cleanup.apply(this, arguments);
|
|
1063
|
-
}
|
|
1064
|
-
return cleanup;
|
|
1065
|
-
}();
|
|
1066
|
-
return CloudflareKVMemory;
|
|
1067
|
-
}(MastraMemory);
|
|
1068
|
-
|
|
1069
|
-
var Pool = pg.Pool;
|
|
1070
|
-
var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
1071
|
-
function PgMemory(connectionString) {
|
|
1072
|
-
var _this;
|
|
1073
|
-
_this = _MastraMemory.call(this) || this;
|
|
1074
|
-
_this.pool = void 0;
|
|
1075
|
-
_this.pool = new Pool({
|
|
1076
|
-
connectionString: connectionString
|
|
1077
|
-
});
|
|
1078
|
-
return _this;
|
|
1079
|
-
}
|
|
1080
|
-
_inheritsLoose(PgMemory, _MastraMemory);
|
|
1081
|
-
var _proto = PgMemory.prototype;
|
|
1082
|
-
_proto.drop = /*#__PURE__*/function () {
|
|
1083
|
-
var _drop = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
1084
|
-
var client;
|
|
1085
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1086
|
-
while (1) switch (_context.prev = _context.next) {
|
|
1087
|
-
case 0:
|
|
1088
|
-
_context.next = 2;
|
|
1089
|
-
return this.pool.connect();
|
|
1090
|
-
case 2:
|
|
1091
|
-
client = _context.sent;
|
|
1092
|
-
_context.next = 5;
|
|
1093
|
-
return client.query('DELETE FROM mastra_messages');
|
|
1094
|
-
case 5:
|
|
1095
|
-
_context.next = 7;
|
|
1096
|
-
return client.query('DELETE FROM mastra_threads');
|
|
1097
|
-
case 7:
|
|
1098
|
-
client.release();
|
|
1099
|
-
_context.next = 10;
|
|
1100
|
-
return this.pool.end();
|
|
1101
|
-
case 10:
|
|
1102
|
-
case "end":
|
|
1103
|
-
return _context.stop();
|
|
1104
|
-
}
|
|
1105
|
-
}, _callee, this);
|
|
1106
|
-
}));
|
|
1107
|
-
function drop() {
|
|
1108
|
-
return _drop.apply(this, arguments);
|
|
1109
|
-
}
|
|
1110
|
-
return drop;
|
|
1111
|
-
}();
|
|
1112
|
-
_proto.ensureTablesExist = /*#__PURE__*/function () {
|
|
1113
|
-
var _ensureTablesExist = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
1114
|
-
var client, _threadsResult$rows, _messagesResult$rows, threadsResult, messagesResult;
|
|
1115
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
1116
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
1117
|
-
case 0:
|
|
1118
|
-
_context2.next = 2;
|
|
1119
|
-
return this.pool.connect();
|
|
1120
|
-
case 2:
|
|
1121
|
-
client = _context2.sent;
|
|
1122
|
-
_context2.prev = 3;
|
|
1123
|
-
_context2.next = 6;
|
|
1124
|
-
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_threads'\n );\n ");
|
|
1125
|
-
case 6:
|
|
1126
|
-
threadsResult = _context2.sent;
|
|
1127
|
-
if (threadsResult != null && (_threadsResult$rows = threadsResult.rows) != null && (_threadsResult$rows = _threadsResult$rows[0]) != null && _threadsResult$rows.exists) {
|
|
1128
|
-
_context2.next = 10;
|
|
1129
|
-
break;
|
|
1130
|
-
}
|
|
1131
|
-
_context2.next = 10;
|
|
1132
|
-
return client.query("\n CREATE TABLE IF NOT EXISTS mastra_threads (\n id UUID PRIMARY KEY,\n title TEXT,\n created_at TIMESTAMP WITH TIME ZONE NOT NULL,\n updated_at TIMESTAMP WITH TIME ZONE NOT NULL,\n metadata JSONB\n );\n ");
|
|
1133
|
-
case 10:
|
|
1134
|
-
_context2.next = 12;
|
|
1135
|
-
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_messages'\n );\n ");
|
|
1136
|
-
case 12:
|
|
1137
|
-
messagesResult = _context2.sent;
|
|
1138
|
-
if (messagesResult != null && (_messagesResult$rows = messagesResult.rows) != null && (_messagesResult$rows = _messagesResult$rows[0]) != null && _messagesResult$rows.exists) {
|
|
1139
|
-
_context2.next = 16;
|
|
1140
|
-
break;
|
|
1141
|
-
}
|
|
1142
|
-
_context2.next = 16;
|
|
1143
|
-
return client.query("\n CREATE TABLE IF NOT EXISTS mastra_messages (\n id UUID PRIMARY KEY,\n content TEXT NOT NULL,\n role VARCHAR(20) NOT NULL,\n created_at TIMESTAMP WITH TIME ZONE NOT NULL,\n thread_id UUID NOT NULL,\n FOREIGN KEY (thread_id) REFERENCES mastra_threads(id)\n );\n ");
|
|
1144
|
-
case 16:
|
|
1145
|
-
_context2.prev = 16;
|
|
1146
|
-
client.release();
|
|
1147
|
-
return _context2.finish(16);
|
|
1148
|
-
case 19:
|
|
1149
|
-
case "end":
|
|
1150
|
-
return _context2.stop();
|
|
1151
|
-
}
|
|
1152
|
-
}, _callee2, this, [[3,, 16, 19]]);
|
|
1153
|
-
}));
|
|
1154
|
-
function ensureTablesExist() {
|
|
1155
|
-
return _ensureTablesExist.apply(this, arguments);
|
|
1156
|
-
}
|
|
1157
|
-
return ensureTablesExist;
|
|
1158
|
-
}();
|
|
1159
|
-
_proto.updateThread = /*#__PURE__*/function () {
|
|
1160
|
-
var _updateThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(id, title, metadata) {
|
|
1161
|
-
var client, _result$rows, result;
|
|
1162
|
-
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
1163
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
1164
|
-
case 0:
|
|
1165
|
-
_context3.next = 2;
|
|
1166
|
-
return this.pool.connect();
|
|
1167
|
-
case 2:
|
|
1168
|
-
client = _context3.sent;
|
|
1169
|
-
_context3.prev = 3;
|
|
1170
|
-
_context3.next = 6;
|
|
1171
|
-
return client.query("\n UPDATE mastra_threads\n SET title = $1, metadata = $2, updated_at = NOW()\n WHERE id = $3\n RETURNING *\n ", [title, JSON.stringify(metadata), id]);
|
|
1172
|
-
case 6:
|
|
1173
|
-
result = _context3.sent;
|
|
1174
|
-
return _context3.abrupt("return", result == null || (_result$rows = result.rows) == null ? void 0 : _result$rows[0]);
|
|
1175
|
-
case 8:
|
|
1176
|
-
_context3.prev = 8;
|
|
1177
|
-
client.release();
|
|
1178
|
-
return _context3.finish(8);
|
|
1179
|
-
case 11:
|
|
1180
|
-
case "end":
|
|
1181
|
-
return _context3.stop();
|
|
1182
|
-
}
|
|
1183
|
-
}, _callee3, this, [[3,, 8, 11]]);
|
|
1184
|
-
}));
|
|
1185
|
-
function updateThread(_x, _x2, _x3) {
|
|
1186
|
-
return _updateThread.apply(this, arguments);
|
|
1187
|
-
}
|
|
1188
|
-
return updateThread;
|
|
1189
|
-
}();
|
|
1190
|
-
_proto.deleteThread = /*#__PURE__*/function () {
|
|
1191
|
-
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(id) {
|
|
1192
|
-
var client;
|
|
1193
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
1194
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
1195
|
-
case 0:
|
|
1196
|
-
_context4.next = 2;
|
|
1197
|
-
return this.pool.connect();
|
|
1198
|
-
case 2:
|
|
1199
|
-
client = _context4.sent;
|
|
1200
|
-
_context4.prev = 3;
|
|
1201
|
-
_context4.next = 6;
|
|
1202
|
-
return client.query("\n DELETE FROM mastra_messages\n WHERE thread_id = $1\n ", [id]);
|
|
1203
|
-
case 6:
|
|
1204
|
-
_context4.next = 8;
|
|
1205
|
-
return client.query("\n DELETE FROM mastra_threads\n WHERE id = $1\n ", [id]);
|
|
1206
|
-
case 8:
|
|
1207
|
-
_context4.prev = 8;
|
|
1208
|
-
client.release();
|
|
1209
|
-
return _context4.finish(8);
|
|
1210
|
-
case 11:
|
|
1211
|
-
case "end":
|
|
1212
|
-
return _context4.stop();
|
|
1213
|
-
}
|
|
1214
|
-
}, _callee4, this, [[3,, 8, 11]]);
|
|
1215
|
-
}));
|
|
1216
|
-
function deleteThread(_x4) {
|
|
1217
|
-
return _deleteThread.apply(this, arguments);
|
|
1218
|
-
}
|
|
1219
|
-
return deleteThread;
|
|
1220
|
-
}();
|
|
1221
|
-
_proto.deleteMessage = /*#__PURE__*/function () {
|
|
1222
|
-
var _deleteMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(id) {
|
|
1223
|
-
var client;
|
|
1224
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
1225
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
1226
|
-
case 0:
|
|
1227
|
-
_context5.next = 2;
|
|
1228
|
-
return this.pool.connect();
|
|
1229
|
-
case 2:
|
|
1230
|
-
client = _context5.sent;
|
|
1231
|
-
_context5.prev = 3;
|
|
1232
|
-
_context5.next = 6;
|
|
1233
|
-
return client.query("\n DELETE FROM mastra_messages\n WHERE id = $1\n ", [id]);
|
|
1234
|
-
case 6:
|
|
1235
|
-
_context5.prev = 6;
|
|
1236
|
-
client.release();
|
|
1237
|
-
return _context5.finish(6);
|
|
1238
|
-
case 9:
|
|
1239
|
-
case "end":
|
|
1240
|
-
return _context5.stop();
|
|
1241
|
-
}
|
|
1242
|
-
}, _callee5, this, [[3,, 6, 9]]);
|
|
1243
|
-
}));
|
|
1244
|
-
function deleteMessage(_x5) {
|
|
1245
|
-
return _deleteMessage.apply(this, arguments);
|
|
1246
|
-
}
|
|
1247
|
-
return deleteMessage;
|
|
1248
|
-
}();
|
|
1249
|
-
_proto.getThreadById = /*#__PURE__*/function () {
|
|
1250
|
-
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(threadId) {
|
|
1251
|
-
var client, result;
|
|
1252
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
1253
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
1254
|
-
case 0:
|
|
1255
|
-
_context6.next = 2;
|
|
1256
|
-
return this.ensureTablesExist();
|
|
1257
|
-
case 2:
|
|
1258
|
-
_context6.next = 4;
|
|
1259
|
-
return this.pool.connect();
|
|
1260
|
-
case 4:
|
|
1261
|
-
client = _context6.sent;
|
|
1262
|
-
_context6.prev = 5;
|
|
1263
|
-
_context6.next = 8;
|
|
1264
|
-
return client.query("\n SELECT id, title, created_at AS createdAt, updated_at AS updatedAt, metadata\n FROM mastra_threads\n WHERE id = $1\n ", [threadId]);
|
|
1265
|
-
case 8:
|
|
1266
|
-
result = _context6.sent;
|
|
1267
|
-
return _context6.abrupt("return", result.rows[0] || null);
|
|
1268
|
-
case 10:
|
|
1269
|
-
_context6.prev = 10;
|
|
1270
|
-
client.release();
|
|
1271
|
-
return _context6.finish(10);
|
|
1272
|
-
case 13:
|
|
1273
|
-
case "end":
|
|
1274
|
-
return _context6.stop();
|
|
1275
|
-
}
|
|
1276
|
-
}, _callee6, this, [[5,, 10, 13]]);
|
|
1277
|
-
}));
|
|
1278
|
-
function getThreadById(_x6) {
|
|
1279
|
-
return _getThreadById.apply(this, arguments);
|
|
1280
|
-
}
|
|
1281
|
-
return getThreadById;
|
|
1282
|
-
}();
|
|
1283
|
-
_proto.saveThread = /*#__PURE__*/function () {
|
|
1284
|
-
var _saveThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(thread) {
|
|
1285
|
-
var client, _result$rows2, id, title, createdAt, updatedAt, metadata, result;
|
|
1286
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
1287
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
1288
|
-
case 0:
|
|
1289
|
-
_context7.next = 2;
|
|
1290
|
-
return this.ensureTablesExist();
|
|
1291
|
-
case 2:
|
|
1292
|
-
_context7.next = 4;
|
|
1293
|
-
return this.pool.connect();
|
|
1294
|
-
case 4:
|
|
1295
|
-
client = _context7.sent;
|
|
1296
|
-
_context7.prev = 5;
|
|
1297
|
-
id = thread.id, title = thread.title, createdAt = thread.createdAt, updatedAt = thread.updatedAt, metadata = thread.metadata;
|
|
1298
|
-
_context7.next = 9;
|
|
1299
|
-
return client.query("\n INSERT INTO mastra_threads (id, title, created_at, updated_at, metadata)\n VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT (id) DO UPDATE SET title = $2, updated_at = $4, metadata = $5\n RETURNING id, title, created_at AS createdAt, updated_at AS updatedAt, metadata\n ", [id, title, createdAt, updatedAt, JSON.stringify(metadata)]);
|
|
1300
|
-
case 9:
|
|
1301
|
-
result = _context7.sent;
|
|
1302
|
-
return _context7.abrupt("return", result == null || (_result$rows2 = result.rows) == null ? void 0 : _result$rows2[0]);
|
|
1303
|
-
case 11:
|
|
1304
|
-
_context7.prev = 11;
|
|
1305
|
-
client.release();
|
|
1306
|
-
return _context7.finish(11);
|
|
1307
|
-
case 14:
|
|
1308
|
-
case "end":
|
|
1309
|
-
return _context7.stop();
|
|
1310
|
-
}
|
|
1311
|
-
}, _callee7, this, [[5,, 11, 14]]);
|
|
1312
|
-
}));
|
|
1313
|
-
function saveThread(_x7) {
|
|
1314
|
-
return _saveThread.apply(this, arguments);
|
|
1315
|
-
}
|
|
1316
|
-
return saveThread;
|
|
1317
|
-
}();
|
|
1318
|
-
_proto.saveMessages = /*#__PURE__*/function () {
|
|
1319
|
-
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(messages) {
|
|
1320
|
-
var client, _iterator, _step, message, id, content, role, createdAt, threadId;
|
|
1321
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
1322
|
-
while (1) switch (_context8.prev = _context8.next) {
|
|
1323
|
-
case 0:
|
|
1324
|
-
_context8.next = 2;
|
|
1325
|
-
return this.ensureTablesExist();
|
|
1326
|
-
case 2:
|
|
1327
|
-
_context8.next = 4;
|
|
1328
|
-
return this.pool.connect();
|
|
1329
|
-
case 4:
|
|
1330
|
-
client = _context8.sent;
|
|
1331
|
-
_context8.prev = 5;
|
|
1332
|
-
_context8.next = 8;
|
|
1333
|
-
return client.query('BEGIN');
|
|
1334
|
-
case 8:
|
|
1335
|
-
_iterator = _createForOfIteratorHelperLoose(messages);
|
|
1336
|
-
case 9:
|
|
1337
|
-
if ((_step = _iterator()).done) {
|
|
1338
|
-
_context8.next = 16;
|
|
1339
|
-
break;
|
|
1340
|
-
}
|
|
1341
|
-
message = _step.value;
|
|
1342
|
-
id = message.id, content = message.content, role = message.role, createdAt = message.createdAt, threadId = message.threadId;
|
|
1343
|
-
_context8.next = 14;
|
|
1344
|
-
return client.query("\n INSERT INTO mastra_messages (id, content, role, created_at, thread_id)\n VALUES ($1, $2, $3, $4, $5)\n ", [id, content, role, createdAt.toISOString(), threadId]);
|
|
1345
|
-
case 14:
|
|
1346
|
-
_context8.next = 9;
|
|
1347
|
-
break;
|
|
1348
|
-
case 16:
|
|
1349
|
-
_context8.next = 18;
|
|
1350
|
-
return client.query('COMMIT');
|
|
1351
|
-
case 18:
|
|
1352
|
-
return _context8.abrupt("return", messages);
|
|
1353
|
-
case 21:
|
|
1354
|
-
_context8.prev = 21;
|
|
1355
|
-
_context8.t0 = _context8["catch"](5);
|
|
1356
|
-
_context8.next = 25;
|
|
1357
|
-
return client.query('ROLLBACK');
|
|
1358
|
-
case 25:
|
|
1359
|
-
throw _context8.t0;
|
|
1360
|
-
case 26:
|
|
1361
|
-
_context8.prev = 26;
|
|
1362
|
-
client.release();
|
|
1363
|
-
return _context8.finish(26);
|
|
1364
|
-
case 29:
|
|
1365
|
-
case "end":
|
|
1366
|
-
return _context8.stop();
|
|
1367
|
-
}
|
|
1368
|
-
}, _callee8, this, [[5, 21, 26, 29]]);
|
|
1369
|
-
}));
|
|
1370
|
-
function saveMessages(_x8) {
|
|
1371
|
-
return _saveMessages.apply(this, arguments);
|
|
1372
|
-
}
|
|
1373
|
-
return saveMessages;
|
|
1374
|
-
}();
|
|
1375
|
-
_proto.getMessages = /*#__PURE__*/function () {
|
|
1376
|
-
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(threadId) {
|
|
1377
|
-
var client, result;
|
|
1378
|
-
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
1379
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
1380
|
-
case 0:
|
|
1381
|
-
_context9.next = 2;
|
|
1382
|
-
return this.ensureTablesExist();
|
|
1383
|
-
case 2:
|
|
1384
|
-
_context9.next = 4;
|
|
1385
|
-
return this.pool.connect();
|
|
1386
|
-
case 4:
|
|
1387
|
-
client = _context9.sent;
|
|
1388
|
-
_context9.prev = 5;
|
|
1389
|
-
_context9.next = 8;
|
|
1390
|
-
return client.query("\n SELECT \n id, \n content, \n role, \n created_at AS createdAt, \n thread_id AS threadId\n FROM mastra_messages\n WHERE thread_id = $1\n ORDER BY created_at ASC\n ", [threadId]);
|
|
1391
|
-
case 8:
|
|
1392
|
-
result = _context9.sent;
|
|
1393
|
-
return _context9.abrupt("return", result.rows);
|
|
1394
|
-
case 10:
|
|
1395
|
-
_context9.prev = 10;
|
|
1396
|
-
client.release();
|
|
1397
|
-
return _context9.finish(10);
|
|
1398
|
-
case 13:
|
|
1399
|
-
case "end":
|
|
1400
|
-
return _context9.stop();
|
|
1401
|
-
}
|
|
1402
|
-
}, _callee9, this, [[5,, 10, 13]]);
|
|
1403
|
-
}));
|
|
1404
|
-
function getMessages(_x9) {
|
|
1405
|
-
return _getMessages.apply(this, arguments);
|
|
1406
|
-
}
|
|
1407
|
-
return getMessages;
|
|
1408
|
-
}();
|
|
1409
|
-
_proto.createThread = /*#__PURE__*/function () {
|
|
1410
|
-
var _createThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(title, metadata) {
|
|
1411
|
-
var id, now, thread;
|
|
1412
|
-
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
1413
|
-
while (1) switch (_context10.prev = _context10.next) {
|
|
1414
|
-
case 0:
|
|
1415
|
-
_context10.next = 2;
|
|
1416
|
-
return this.ensureTablesExist();
|
|
1417
|
-
case 2:
|
|
1418
|
-
id = randomUUID();
|
|
1419
|
-
now = new Date();
|
|
1420
|
-
thread = {
|
|
1421
|
-
id: id,
|
|
1422
|
-
title: title,
|
|
1423
|
-
createdAt: now,
|
|
1424
|
-
updatedAt: now,
|
|
1425
|
-
metadata: metadata
|
|
1426
|
-
};
|
|
1427
|
-
return _context10.abrupt("return", this.saveThread(thread));
|
|
1428
|
-
case 6:
|
|
1429
|
-
case "end":
|
|
1430
|
-
return _context10.stop();
|
|
1431
|
-
}
|
|
1432
|
-
}, _callee10, this);
|
|
1433
|
-
}));
|
|
1434
|
-
function createThread(_x10, _x11) {
|
|
1435
|
-
return _createThread.apply(this, arguments);
|
|
1436
|
-
}
|
|
1437
|
-
return createThread;
|
|
1438
|
-
}();
|
|
1439
|
-
_proto.addMessage = /*#__PURE__*/function () {
|
|
1440
|
-
var _addMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(threadId, content, role) {
|
|
1441
|
-
var thread, id, message, _yield$this$saveMessa, savedMessage;
|
|
1442
|
-
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
1443
|
-
while (1) switch (_context11.prev = _context11.next) {
|
|
1444
|
-
case 0:
|
|
1445
|
-
_context11.next = 2;
|
|
1446
|
-
return this.ensureTablesExist();
|
|
1447
|
-
case 2:
|
|
1448
|
-
_context11.next = 4;
|
|
1449
|
-
return this.getThreadById(threadId);
|
|
1450
|
-
case 4:
|
|
1451
|
-
thread = _context11.sent;
|
|
1452
|
-
if (thread) {
|
|
1453
|
-
_context11.next = 7;
|
|
1454
|
-
break;
|
|
1455
|
-
}
|
|
1456
|
-
throw new Error("Thread with ID " + threadId + " does not exist.");
|
|
1457
|
-
case 7:
|
|
1458
|
-
id = randomUUID();
|
|
1459
|
-
message = {
|
|
1460
|
-
id: id,
|
|
1461
|
-
content: content,
|
|
1462
|
-
role: role,
|
|
1463
|
-
createdAt: new Date(),
|
|
1464
|
-
threadId: threadId
|
|
1465
|
-
};
|
|
1466
|
-
_context11.next = 11;
|
|
1467
|
-
return this.saveMessages([message]);
|
|
1468
|
-
case 11:
|
|
1469
|
-
_yield$this$saveMessa = _context11.sent;
|
|
1470
|
-
savedMessage = _yield$this$saveMessa[0];
|
|
1471
|
-
return _context11.abrupt("return", savedMessage);
|
|
1472
|
-
case 14:
|
|
1473
|
-
case "end":
|
|
1474
|
-
return _context11.stop();
|
|
1475
|
-
}
|
|
1476
|
-
}, _callee11, this);
|
|
1477
|
-
}));
|
|
1478
|
-
function addMessage(_x12, _x13, _x14) {
|
|
1479
|
-
return _addMessage.apply(this, arguments);
|
|
1480
|
-
}
|
|
1481
|
-
return addMessage;
|
|
1482
|
-
}();
|
|
1483
|
-
return PgMemory;
|
|
1484
|
-
}(MastraMemory);
|
|
1485
|
-
|
|
1486
|
-
// Helper functions for date handling
|
|
1487
|
-
function serializeData(data) {
|
|
1488
|
-
if (data === null || data === undefined) return data;
|
|
1489
|
-
if (data instanceof Date) {
|
|
1490
|
-
return {
|
|
1491
|
-
__type: 'Date',
|
|
1492
|
-
value: data.toISOString()
|
|
1493
|
-
};
|
|
1494
|
-
}
|
|
1495
|
-
if (Array.isArray(data)) {
|
|
1496
|
-
return data.map(function (item) {
|
|
1497
|
-
return serializeData(item);
|
|
1498
|
-
});
|
|
1499
|
-
}
|
|
1500
|
-
if (typeof data === 'object') {
|
|
1501
|
-
return Object.keys(data).reduce(function (acc, key) {
|
|
1502
|
-
acc[key] = serializeData(data[key]);
|
|
1503
|
-
return acc;
|
|
1504
|
-
}, {});
|
|
1505
|
-
}
|
|
1506
|
-
return data;
|
|
1507
|
-
}
|
|
1508
|
-
function deserializeData(data) {
|
|
1509
|
-
if (data === null || data === undefined) return data;
|
|
1510
|
-
if (typeof data === 'object' && data.__type === 'Date') {
|
|
1511
|
-
return new Date(data.value);
|
|
1512
|
-
}
|
|
1513
|
-
if (Array.isArray(data)) {
|
|
1514
|
-
return data.map(function (item) {
|
|
1515
|
-
return deserializeData(item);
|
|
1516
|
-
});
|
|
1517
|
-
}
|
|
1518
|
-
if (typeof data === 'object') {
|
|
1519
|
-
return Object.keys(data).reduce(function (acc, key) {
|
|
1520
|
-
acc[key] = deserializeData(data[key]);
|
|
1521
|
-
return acc;
|
|
1522
|
-
}, {});
|
|
1523
|
-
}
|
|
1524
|
-
return data;
|
|
1525
|
-
}
|
|
1526
|
-
// Provider for Upstash/Vercel KV
|
|
1527
|
-
var UpstashProvider = /*#__PURE__*/function () {
|
|
1528
|
-
function UpstashProvider(url, token) {
|
|
1529
|
-
this.client = void 0;
|
|
1530
|
-
this.client = new Redis({
|
|
1531
|
-
url: url,
|
|
1532
|
-
token: token
|
|
378
|
+
var Pool = pg.Pool;
|
|
379
|
+
var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
380
|
+
function PgMemory(config) {
|
|
381
|
+
var _this;
|
|
382
|
+
_this = _MastraMemory.call(this) || this;
|
|
383
|
+
_this.pool = void 0;
|
|
384
|
+
_this.MAX_CONTEXT_TOKENS = void 0;
|
|
385
|
+
_this.pool = new Pool({
|
|
386
|
+
connectionString: config.connectionString
|
|
1533
387
|
});
|
|
388
|
+
_this.MAX_CONTEXT_TOKENS = config.maxTokens;
|
|
389
|
+
return _this;
|
|
1534
390
|
}
|
|
1535
|
-
|
|
1536
|
-
_proto
|
|
1537
|
-
|
|
1538
|
-
|
|
391
|
+
_inheritsLoose(PgMemory, _MastraMemory);
|
|
392
|
+
var _proto = PgMemory.prototype;
|
|
393
|
+
_proto.drop = /*#__PURE__*/function () {
|
|
394
|
+
var _drop = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
395
|
+
var client;
|
|
1539
396
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1540
397
|
while (1) switch (_context.prev = _context.next) {
|
|
1541
398
|
case 0:
|
|
1542
399
|
_context.next = 2;
|
|
1543
|
-
return this.
|
|
400
|
+
return this.pool.connect();
|
|
1544
401
|
case 2:
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
402
|
+
client = _context.sent;
|
|
403
|
+
_context.next = 5;
|
|
404
|
+
return client.query('DELETE FROM mastra_messages');
|
|
405
|
+
case 5:
|
|
406
|
+
_context.next = 7;
|
|
407
|
+
return client.query('DELETE FROM mastra_threads');
|
|
408
|
+
case 7:
|
|
409
|
+
client.release();
|
|
410
|
+
_context.next = 10;
|
|
411
|
+
return this.pool.end();
|
|
412
|
+
case 10:
|
|
1548
413
|
case "end":
|
|
1549
414
|
return _context.stop();
|
|
1550
415
|
}
|
|
1551
416
|
}, _callee, this);
|
|
1552
417
|
}));
|
|
1553
|
-
function
|
|
1554
|
-
return
|
|
418
|
+
function drop() {
|
|
419
|
+
return _drop.apply(this, arguments);
|
|
1555
420
|
}
|
|
1556
|
-
return
|
|
1557
|
-
}()
|
|
1558
|
-
|
|
1559
|
-
|
|
421
|
+
return drop;
|
|
422
|
+
}() // Simplified token estimation
|
|
423
|
+
;
|
|
424
|
+
_proto.estimateTokens = function estimateTokens(text) {
|
|
425
|
+
return Math.ceil(text.split(' ').length * 1.3);
|
|
426
|
+
};
|
|
427
|
+
_proto.processMessages = function processMessages(messages) {
|
|
428
|
+
return messages.map(function (mssg) {
|
|
429
|
+
return _extends({}, mssg, {
|
|
430
|
+
content: typeof mssg.content === 'string' ? JSON.parse(mssg.content) : mssg.content
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
};
|
|
434
|
+
_proto.convertToUIMessages = function convertToUIMessages(messages) {
|
|
435
|
+
function addToolMessageToChat(_ref) {
|
|
436
|
+
var toolMessage = _ref.toolMessage,
|
|
437
|
+
messages = _ref.messages,
|
|
438
|
+
toolResultContents = _ref.toolResultContents;
|
|
439
|
+
var chatMessages = messages.map(function (message) {
|
|
440
|
+
if (message.toolInvocations) {
|
|
441
|
+
return _extends({}, message, {
|
|
442
|
+
toolInvocations: message.toolInvocations.map(function (toolInvocation) {
|
|
443
|
+
var toolResult = toolMessage.content.find(function (tool) {
|
|
444
|
+
return tool.toolCallId === toolInvocation.toolCallId;
|
|
445
|
+
});
|
|
446
|
+
if (toolResult) {
|
|
447
|
+
return _extends({}, toolInvocation, {
|
|
448
|
+
state: 'result',
|
|
449
|
+
result: toolResult.result
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
return toolInvocation;
|
|
453
|
+
})
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
return message;
|
|
457
|
+
});
|
|
458
|
+
var resultContents = [].concat(toolResultContents, toolMessage.content);
|
|
459
|
+
return {
|
|
460
|
+
chatMessages: chatMessages,
|
|
461
|
+
toolResultContents: resultContents
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
var _messages$reduce = messages.reduce(function (obj, message) {
|
|
465
|
+
if (message.role === 'tool') {
|
|
466
|
+
return addToolMessageToChat({
|
|
467
|
+
toolMessage: message,
|
|
468
|
+
messages: obj.chatMessages,
|
|
469
|
+
toolResultContents: obj.toolResultContents
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
var textContent = '';
|
|
473
|
+
var toolInvocations = [];
|
|
474
|
+
if (typeof message.content === 'string') {
|
|
475
|
+
textContent = message.content;
|
|
476
|
+
} else if (Array.isArray(message.content)) {
|
|
477
|
+
var _loop = function _loop() {
|
|
478
|
+
var content = _step.value;
|
|
479
|
+
if (content.type === 'text') {
|
|
480
|
+
textContent += content.text;
|
|
481
|
+
} else if (content.type === 'tool-call') {
|
|
482
|
+
var toolResult = obj.toolResultContents.find(function (tool) {
|
|
483
|
+
return tool.toolCallId === content.toolCallId;
|
|
484
|
+
});
|
|
485
|
+
toolInvocations.push({
|
|
486
|
+
state: toolResult ? 'result' : 'call',
|
|
487
|
+
toolCallId: content.toolCallId,
|
|
488
|
+
toolName: content.toolName,
|
|
489
|
+
args: content.args,
|
|
490
|
+
result: toolResult == null ? void 0 : toolResult.result
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
for (var _iterator = _createForOfIteratorHelperLoose(message.content), _step; !(_step = _iterator()).done;) {
|
|
495
|
+
_loop();
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
obj.chatMessages.push({
|
|
499
|
+
id: message.id,
|
|
500
|
+
role: message.role,
|
|
501
|
+
content: textContent,
|
|
502
|
+
toolInvocations: toolInvocations
|
|
503
|
+
});
|
|
504
|
+
return obj;
|
|
505
|
+
}, {
|
|
506
|
+
chatMessages: [],
|
|
507
|
+
toolResultContents: []
|
|
508
|
+
}),
|
|
509
|
+
chatMessages = _messages$reduce.chatMessages;
|
|
510
|
+
return chatMessages;
|
|
511
|
+
};
|
|
512
|
+
_proto.ensureTablesExist = /*#__PURE__*/function () {
|
|
513
|
+
var _ensureTablesExist = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
514
|
+
var client, _threadsResult$rows, _messagesResult$rows, threadsResult, messagesResult;
|
|
1560
515
|
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
1561
516
|
while (1) switch (_context2.prev = _context2.next) {
|
|
1562
517
|
case 0:
|
|
1563
|
-
|
|
1564
|
-
|
|
518
|
+
_context2.next = 2;
|
|
519
|
+
return this.pool.connect();
|
|
520
|
+
case 2:
|
|
521
|
+
client = _context2.sent;
|
|
522
|
+
_context2.prev = 3;
|
|
523
|
+
_context2.next = 6;
|
|
524
|
+
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_threads'\n );\n ");
|
|
525
|
+
case 6:
|
|
526
|
+
threadsResult = _context2.sent;
|
|
527
|
+
if (threadsResult != null && (_threadsResult$rows = threadsResult.rows) != null && (_threadsResult$rows = _threadsResult$rows[0]) != null && _threadsResult$rows.exists) {
|
|
528
|
+
_context2.next = 10;
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
_context2.next = 10;
|
|
532
|
+
return client.query("\n CREATE TABLE IF NOT EXISTS mastra_threads (\n id UUID PRIMARY KEY,\n resourceid TEXT,\n title TEXT,\n created_at TIMESTAMP WITH TIME ZONE NOT NULL,\n updated_at TIMESTAMP WITH TIME ZONE NOT NULL,\n metadata JSONB\n );\n ");
|
|
533
|
+
case 10:
|
|
534
|
+
_context2.next = 12;
|
|
535
|
+
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_messages'\n );\n ");
|
|
536
|
+
case 12:
|
|
537
|
+
messagesResult = _context2.sent;
|
|
538
|
+
if (messagesResult != null && (_messagesResult$rows = messagesResult.rows) != null && (_messagesResult$rows = _messagesResult$rows[0]) != null && _messagesResult$rows.exists) {
|
|
539
|
+
_context2.next = 16;
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
_context2.next = 16;
|
|
543
|
+
return client.query("\n CREATE TABLE IF NOT EXISTS mastra_messages (\n id UUID PRIMARY KEY,\n content TEXT NOT NULL,\n role VARCHAR(20) NOT NULL,\n created_at TIMESTAMP WITH TIME ZONE NOT NULL,\n tool_call_ids TEXT DEFAULT NULL,\n tool_call_args TEXT DEFAULT NULL,\n tool_call_args_expire_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,\n type VARCHAR(20) NOT NULL,\n tokens INTEGER DEFAULT NULL,\n thread_id UUID NOT NULL,\n FOREIGN KEY (thread_id) REFERENCES mastra_threads(id)\n );\n ");
|
|
544
|
+
case 16:
|
|
545
|
+
_context2.prev = 16;
|
|
546
|
+
client.release();
|
|
547
|
+
return _context2.finish(16);
|
|
548
|
+
case 19:
|
|
1565
549
|
case "end":
|
|
1566
550
|
return _context2.stop();
|
|
1567
551
|
}
|
|
1568
|
-
}, _callee2, this);
|
|
552
|
+
}, _callee2, this, [[3,, 16, 19]]);
|
|
1569
553
|
}));
|
|
1570
|
-
function
|
|
1571
|
-
return
|
|
554
|
+
function ensureTablesExist() {
|
|
555
|
+
return _ensureTablesExist.apply(this, arguments);
|
|
1572
556
|
}
|
|
1573
|
-
return
|
|
557
|
+
return ensureTablesExist;
|
|
1574
558
|
}();
|
|
1575
|
-
_proto.
|
|
1576
|
-
var
|
|
559
|
+
_proto.updateThread = /*#__PURE__*/function () {
|
|
560
|
+
var _updateThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(id, title, metadata) {
|
|
561
|
+
var client, _result$rows, result;
|
|
1577
562
|
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
1578
563
|
while (1) switch (_context3.prev = _context3.next) {
|
|
1579
564
|
case 0:
|
|
1580
|
-
|
|
1581
|
-
|
|
565
|
+
_context3.next = 2;
|
|
566
|
+
return this.pool.connect();
|
|
567
|
+
case 2:
|
|
568
|
+
client = _context3.sent;
|
|
569
|
+
_context3.prev = 3;
|
|
570
|
+
_context3.next = 6;
|
|
571
|
+
return client.query("\n UPDATE mastra_threads\n SET title = $1, metadata = $2, updated_at = NOW()\n WHERE id = $3\n RETURNING *\n ", [title, JSON.stringify(metadata), id]);
|
|
572
|
+
case 6:
|
|
573
|
+
result = _context3.sent;
|
|
574
|
+
return _context3.abrupt("return", result == null || (_result$rows = result.rows) == null ? void 0 : _result$rows[0]);
|
|
575
|
+
case 8:
|
|
576
|
+
_context3.prev = 8;
|
|
577
|
+
client.release();
|
|
578
|
+
return _context3.finish(8);
|
|
579
|
+
case 11:
|
|
1582
580
|
case "end":
|
|
1583
581
|
return _context3.stop();
|
|
1584
582
|
}
|
|
1585
|
-
}, _callee3, this);
|
|
583
|
+
}, _callee3, this, [[3,, 8, 11]]);
|
|
1586
584
|
}));
|
|
1587
|
-
function
|
|
1588
|
-
return
|
|
585
|
+
function updateThread(_x, _x2, _x3) {
|
|
586
|
+
return _updateThread.apply(this, arguments);
|
|
1589
587
|
}
|
|
1590
|
-
return
|
|
588
|
+
return updateThread;
|
|
1591
589
|
}();
|
|
1592
|
-
_proto.
|
|
1593
|
-
var
|
|
590
|
+
_proto.deleteThread = /*#__PURE__*/function () {
|
|
591
|
+
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(id) {
|
|
592
|
+
var client;
|
|
1594
593
|
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
1595
594
|
while (1) switch (_context4.prev = _context4.next) {
|
|
1596
595
|
case 0:
|
|
1597
|
-
|
|
1598
|
-
|
|
596
|
+
_context4.next = 2;
|
|
597
|
+
return this.pool.connect();
|
|
598
|
+
case 2:
|
|
599
|
+
client = _context4.sent;
|
|
600
|
+
_context4.prev = 3;
|
|
601
|
+
_context4.next = 6;
|
|
602
|
+
return client.query("\n DELETE FROM mastra_messages\n WHERE thread_id = $1\n ", [id]);
|
|
603
|
+
case 6:
|
|
604
|
+
_context4.next = 8;
|
|
605
|
+
return client.query("\n DELETE FROM mastra_threads\n WHERE id = $1\n ", [id]);
|
|
606
|
+
case 8:
|
|
607
|
+
_context4.prev = 8;
|
|
608
|
+
client.release();
|
|
609
|
+
return _context4.finish(8);
|
|
610
|
+
case 11:
|
|
1599
611
|
case "end":
|
|
1600
612
|
return _context4.stop();
|
|
1601
613
|
}
|
|
1602
|
-
}, _callee4, this);
|
|
614
|
+
}, _callee4, this, [[3,, 8, 11]]);
|
|
1603
615
|
}));
|
|
1604
|
-
function
|
|
1605
|
-
return
|
|
616
|
+
function deleteThread(_x4) {
|
|
617
|
+
return _deleteThread.apply(this, arguments);
|
|
1606
618
|
}
|
|
1607
|
-
return
|
|
619
|
+
return deleteThread;
|
|
1608
620
|
}();
|
|
1609
|
-
_proto.
|
|
1610
|
-
var
|
|
621
|
+
_proto.deleteMessage = /*#__PURE__*/function () {
|
|
622
|
+
var _deleteMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(id) {
|
|
623
|
+
var client;
|
|
1611
624
|
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
1612
625
|
while (1) switch (_context5.prev = _context5.next) {
|
|
1613
626
|
case 0:
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
case "end":
|
|
1617
|
-
return _context5.stop();
|
|
1618
|
-
}
|
|
1619
|
-
}, _callee5, this);
|
|
1620
|
-
}));
|
|
1621
|
-
function srem(_x7, _x8) {
|
|
1622
|
-
return _srem.apply(this, arguments);
|
|
1623
|
-
}
|
|
1624
|
-
return srem;
|
|
1625
|
-
}();
|
|
1626
|
-
_proto.smembers = /*#__PURE__*/function () {
|
|
1627
|
-
var _smembers = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(key) {
|
|
1628
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
1629
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
1630
|
-
case 0:
|
|
1631
|
-
return _context6.abrupt("return", this.client.smembers(key));
|
|
1632
|
-
case 1:
|
|
1633
|
-
case "end":
|
|
1634
|
-
return _context6.stop();
|
|
1635
|
-
}
|
|
1636
|
-
}, _callee6, this);
|
|
1637
|
-
}));
|
|
1638
|
-
function smembers(_x9) {
|
|
1639
|
-
return _smembers.apply(this, arguments);
|
|
1640
|
-
}
|
|
1641
|
-
return smembers;
|
|
1642
|
-
}();
|
|
1643
|
-
_proto.flushall = /*#__PURE__*/function () {
|
|
1644
|
-
var _flushall = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
|
|
1645
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
1646
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
1647
|
-
case 0:
|
|
1648
|
-
return _context7.abrupt("return", this.client.flushall());
|
|
1649
|
-
case 1:
|
|
1650
|
-
case "end":
|
|
1651
|
-
return _context7.stop();
|
|
1652
|
-
}
|
|
1653
|
-
}, _callee7, this);
|
|
1654
|
-
}));
|
|
1655
|
-
function flushall() {
|
|
1656
|
-
return _flushall.apply(this, arguments);
|
|
1657
|
-
}
|
|
1658
|
-
return flushall;
|
|
1659
|
-
}();
|
|
1660
|
-
_proto.pipeline = function pipeline() {
|
|
1661
|
-
var multi = this.client.multi();
|
|
1662
|
-
var pipeline = {
|
|
1663
|
-
get: function get(key) {
|
|
1664
|
-
multi.get(key);
|
|
1665
|
-
return pipeline;
|
|
1666
|
-
},
|
|
1667
|
-
set: function set(key, value) {
|
|
1668
|
-
multi.set(key, JSON.stringify(serializeData(value)));
|
|
1669
|
-
return pipeline;
|
|
1670
|
-
},
|
|
1671
|
-
del: function del(key) {
|
|
1672
|
-
multi.del(key);
|
|
1673
|
-
return pipeline;
|
|
1674
|
-
},
|
|
1675
|
-
srem: function srem(key, value) {
|
|
1676
|
-
multi.srem(key, value);
|
|
1677
|
-
return pipeline;
|
|
1678
|
-
},
|
|
1679
|
-
exec: function () {
|
|
1680
|
-
var _exec = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
|
|
1681
|
-
var results;
|
|
1682
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
1683
|
-
while (1) switch (_context8.prev = _context8.next) {
|
|
1684
|
-
case 0:
|
|
1685
|
-
_context8.next = 2;
|
|
1686
|
-
return multi.exec();
|
|
1687
|
-
case 2:
|
|
1688
|
-
results = _context8.sent;
|
|
1689
|
-
return _context8.abrupt("return", results.map(function (result) {
|
|
1690
|
-
try {
|
|
1691
|
-
// For get operations that return string data
|
|
1692
|
-
if (typeof result === 'string') {
|
|
1693
|
-
return deserializeData(JSON.parse(result));
|
|
1694
|
-
}
|
|
1695
|
-
// For array results (like from lists/sets)
|
|
1696
|
-
if (Array.isArray(result)) {
|
|
1697
|
-
return result.map(function (item) {
|
|
1698
|
-
try {
|
|
1699
|
-
return typeof item === 'string' ? deserializeData(JSON.parse(item)) : item;
|
|
1700
|
-
} catch (_unused) {
|
|
1701
|
-
return item;
|
|
1702
|
-
}
|
|
1703
|
-
});
|
|
1704
|
-
}
|
|
1705
|
-
return result;
|
|
1706
|
-
} catch (_unused2) {
|
|
1707
|
-
return result;
|
|
1708
|
-
}
|
|
1709
|
-
}));
|
|
1710
|
-
case 4:
|
|
1711
|
-
case "end":
|
|
1712
|
-
return _context8.stop();
|
|
1713
|
-
}
|
|
1714
|
-
}, _callee8);
|
|
1715
|
-
}));
|
|
1716
|
-
function exec() {
|
|
1717
|
-
return _exec.apply(this, arguments);
|
|
1718
|
-
}
|
|
1719
|
-
return exec;
|
|
1720
|
-
}()
|
|
1721
|
-
};
|
|
1722
|
-
return pipeline;
|
|
1723
|
-
};
|
|
1724
|
-
return UpstashProvider;
|
|
1725
|
-
}();
|
|
1726
|
-
var LocalRedisProvider = /*#__PURE__*/function () {
|
|
1727
|
-
function LocalRedisProvider() {
|
|
1728
|
-
this.client = void 0;
|
|
1729
|
-
this.client = createClient({
|
|
1730
|
-
url: 'redis://localhost:6379'
|
|
1731
|
-
});
|
|
1732
|
-
this.client.connect();
|
|
1733
|
-
}
|
|
1734
|
-
var _proto2 = LocalRedisProvider.prototype;
|
|
1735
|
-
_proto2.get = /*#__PURE__*/function () {
|
|
1736
|
-
var _get2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(key) {
|
|
1737
|
-
var data;
|
|
1738
|
-
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
1739
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
1740
|
-
case 0:
|
|
1741
|
-
_context9.next = 2;
|
|
1742
|
-
return this.client.get(key);
|
|
627
|
+
_context5.next = 2;
|
|
628
|
+
return this.pool.connect();
|
|
1743
629
|
case 2:
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
return _get2.apply(this, arguments);
|
|
1754
|
-
}
|
|
1755
|
-
return get;
|
|
1756
|
-
}();
|
|
1757
|
-
_proto2.set = /*#__PURE__*/function () {
|
|
1758
|
-
var _set2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(key, value) {
|
|
1759
|
-
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
1760
|
-
while (1) switch (_context10.prev = _context10.next) {
|
|
1761
|
-
case 0:
|
|
1762
|
-
return _context10.abrupt("return", this.client.set(key, JSON.stringify(serializeData(value))));
|
|
1763
|
-
case 1:
|
|
1764
|
-
case "end":
|
|
1765
|
-
return _context10.stop();
|
|
1766
|
-
}
|
|
1767
|
-
}, _callee10, this);
|
|
1768
|
-
}));
|
|
1769
|
-
function set(_x11, _x12) {
|
|
1770
|
-
return _set2.apply(this, arguments);
|
|
1771
|
-
}
|
|
1772
|
-
return set;
|
|
1773
|
-
}();
|
|
1774
|
-
_proto2.del = /*#__PURE__*/function () {
|
|
1775
|
-
var _del2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(key) {
|
|
1776
|
-
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
1777
|
-
while (1) switch (_context11.prev = _context11.next) {
|
|
1778
|
-
case 0:
|
|
1779
|
-
return _context11.abrupt("return", this.client.del(key));
|
|
1780
|
-
case 1:
|
|
1781
|
-
case "end":
|
|
1782
|
-
return _context11.stop();
|
|
1783
|
-
}
|
|
1784
|
-
}, _callee11, this);
|
|
1785
|
-
}));
|
|
1786
|
-
function del(_x13) {
|
|
1787
|
-
return _del2.apply(this, arguments);
|
|
1788
|
-
}
|
|
1789
|
-
return del;
|
|
1790
|
-
}();
|
|
1791
|
-
_proto2.sadd = /*#__PURE__*/function () {
|
|
1792
|
-
var _sadd2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(key, value) {
|
|
1793
|
-
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
|
|
1794
|
-
while (1) switch (_context12.prev = _context12.next) {
|
|
1795
|
-
case 0:
|
|
1796
|
-
return _context12.abrupt("return", this.client.sAdd(key, value));
|
|
1797
|
-
case 1:
|
|
1798
|
-
case "end":
|
|
1799
|
-
return _context12.stop();
|
|
1800
|
-
}
|
|
1801
|
-
}, _callee12, this);
|
|
1802
|
-
}));
|
|
1803
|
-
function sadd(_x14, _x15) {
|
|
1804
|
-
return _sadd2.apply(this, arguments);
|
|
1805
|
-
}
|
|
1806
|
-
return sadd;
|
|
1807
|
-
}();
|
|
1808
|
-
_proto2.srem = /*#__PURE__*/function () {
|
|
1809
|
-
var _srem2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13(key, value) {
|
|
1810
|
-
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
|
|
1811
|
-
while (1) switch (_context13.prev = _context13.next) {
|
|
1812
|
-
case 0:
|
|
1813
|
-
return _context13.abrupt("return", this.client.sRem(key, value));
|
|
1814
|
-
case 1:
|
|
1815
|
-
case "end":
|
|
1816
|
-
return _context13.stop();
|
|
1817
|
-
}
|
|
1818
|
-
}, _callee13, this);
|
|
1819
|
-
}));
|
|
1820
|
-
function srem(_x16, _x17) {
|
|
1821
|
-
return _srem2.apply(this, arguments);
|
|
1822
|
-
}
|
|
1823
|
-
return srem;
|
|
1824
|
-
}();
|
|
1825
|
-
_proto2.smembers = /*#__PURE__*/function () {
|
|
1826
|
-
var _smembers2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee14(key) {
|
|
1827
|
-
return _regeneratorRuntime().wrap(function _callee14$(_context14) {
|
|
1828
|
-
while (1) switch (_context14.prev = _context14.next) {
|
|
1829
|
-
case 0:
|
|
1830
|
-
return _context14.abrupt("return", this.client.sMembers(key));
|
|
1831
|
-
case 1:
|
|
630
|
+
client = _context5.sent;
|
|
631
|
+
_context5.prev = 3;
|
|
632
|
+
_context5.next = 6;
|
|
633
|
+
return client.query("\n DELETE FROM mastra_messages\n WHERE id = $1\n ", [id]);
|
|
634
|
+
case 6:
|
|
635
|
+
_context5.prev = 6;
|
|
636
|
+
client.release();
|
|
637
|
+
return _context5.finish(6);
|
|
638
|
+
case 9:
|
|
1832
639
|
case "end":
|
|
1833
|
-
return
|
|
640
|
+
return _context5.stop();
|
|
1834
641
|
}
|
|
1835
|
-
},
|
|
642
|
+
}, _callee5, this, [[3,, 6, 9]]);
|
|
1836
643
|
}));
|
|
1837
|
-
function
|
|
1838
|
-
return
|
|
644
|
+
function deleteMessage(_x5) {
|
|
645
|
+
return _deleteMessage.apply(this, arguments);
|
|
1839
646
|
}
|
|
1840
|
-
return
|
|
647
|
+
return deleteMessage;
|
|
1841
648
|
}();
|
|
1842
|
-
|
|
1843
|
-
var
|
|
1844
|
-
|
|
1845
|
-
|
|
649
|
+
_proto.getThreadById = /*#__PURE__*/function () {
|
|
650
|
+
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref2) {
|
|
651
|
+
var threadId, client, result;
|
|
652
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
653
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
1846
654
|
case 0:
|
|
1847
|
-
|
|
1848
|
-
|
|
655
|
+
threadId = _ref2.threadId;
|
|
656
|
+
console.log('getThreadById', threadId);
|
|
657
|
+
_context6.next = 4;
|
|
658
|
+
return this.ensureTablesExist();
|
|
659
|
+
case 4:
|
|
660
|
+
_context6.next = 6;
|
|
661
|
+
return this.pool.connect();
|
|
662
|
+
case 6:
|
|
663
|
+
client = _context6.sent;
|
|
664
|
+
_context6.prev = 7;
|
|
665
|
+
_context6.next = 10;
|
|
666
|
+
return client.query("\n SELECT id, title, created_at AS createdAt, updated_at AS updatedAt, resourceid, metadata\n FROM mastra_threads\n WHERE id = $1\n ", [threadId]);
|
|
667
|
+
case 10:
|
|
668
|
+
result = _context6.sent;
|
|
669
|
+
return _context6.abrupt("return", result.rows[0] || null);
|
|
670
|
+
case 12:
|
|
671
|
+
_context6.prev = 12;
|
|
672
|
+
client.release();
|
|
673
|
+
return _context6.finish(12);
|
|
674
|
+
case 15:
|
|
1849
675
|
case "end":
|
|
1850
|
-
return
|
|
676
|
+
return _context6.stop();
|
|
1851
677
|
}
|
|
1852
|
-
},
|
|
678
|
+
}, _callee6, this, [[7,, 12, 15]]);
|
|
1853
679
|
}));
|
|
1854
|
-
function
|
|
1855
|
-
return
|
|
680
|
+
function getThreadById(_x6) {
|
|
681
|
+
return _getThreadById.apply(this, arguments);
|
|
1856
682
|
}
|
|
1857
|
-
return
|
|
683
|
+
return getThreadById;
|
|
1858
684
|
}();
|
|
1859
|
-
|
|
1860
|
-
var
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
return pipeline;
|
|
1865
|
-
},
|
|
1866
|
-
set: function set(key, value) {
|
|
1867
|
-
multi.set(key, JSON.stringify(value));
|
|
1868
|
-
return pipeline;
|
|
1869
|
-
},
|
|
1870
|
-
del: function del(key) {
|
|
1871
|
-
multi.del(key);
|
|
1872
|
-
return pipeline;
|
|
1873
|
-
},
|
|
1874
|
-
srem: function srem(key, value) {
|
|
1875
|
-
multi.sRem(key, value);
|
|
1876
|
-
return pipeline;
|
|
1877
|
-
},
|
|
1878
|
-
exec: function () {
|
|
1879
|
-
var _exec2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee16() {
|
|
1880
|
-
return _regeneratorRuntime().wrap(function _callee16$(_context16) {
|
|
1881
|
-
while (1) switch (_context16.prev = _context16.next) {
|
|
1882
|
-
case 0:
|
|
1883
|
-
return _context16.abrupt("return", multi.exec());
|
|
1884
|
-
case 1:
|
|
1885
|
-
case "end":
|
|
1886
|
-
return _context16.stop();
|
|
1887
|
-
}
|
|
1888
|
-
}, _callee16);
|
|
1889
|
-
}));
|
|
1890
|
-
function exec() {
|
|
1891
|
-
return _exec2.apply(this, arguments);
|
|
1892
|
-
}
|
|
1893
|
-
return exec;
|
|
1894
|
-
}()
|
|
1895
|
-
};
|
|
1896
|
-
return pipeline;
|
|
1897
|
-
};
|
|
1898
|
-
_proto2.quit = /*#__PURE__*/function () {
|
|
1899
|
-
var _quit = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee17() {
|
|
1900
|
-
return _regeneratorRuntime().wrap(function _callee17$(_context17) {
|
|
1901
|
-
while (1) switch (_context17.prev = _context17.next) {
|
|
685
|
+
_proto.getThreadsByResourceId = /*#__PURE__*/function () {
|
|
686
|
+
var _getThreadsByResourceId = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref3) {
|
|
687
|
+
var resourceid, client, result;
|
|
688
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
689
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
1902
690
|
case 0:
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
691
|
+
resourceid = _ref3.resourceid;
|
|
692
|
+
_context7.next = 3;
|
|
693
|
+
return this.ensureTablesExist();
|
|
694
|
+
case 3:
|
|
695
|
+
_context7.next = 5;
|
|
696
|
+
return this.pool.connect();
|
|
697
|
+
case 5:
|
|
698
|
+
client = _context7.sent;
|
|
699
|
+
_context7.prev = 6;
|
|
700
|
+
_context7.next = 9;
|
|
701
|
+
return client.query("\n SELECT id, title, resourceid, created_at AS createdAt, updated_at AS updatedAt, metadata\n FROM mastra_threads\n WHERE resourceid = $1\n ", [resourceid]);
|
|
702
|
+
case 9:
|
|
703
|
+
result = _context7.sent;
|
|
704
|
+
return _context7.abrupt("return", result.rows);
|
|
705
|
+
case 11:
|
|
706
|
+
_context7.prev = 11;
|
|
707
|
+
client.release();
|
|
708
|
+
return _context7.finish(11);
|
|
709
|
+
case 14:
|
|
1906
710
|
case "end":
|
|
1907
|
-
return
|
|
711
|
+
return _context7.stop();
|
|
1908
712
|
}
|
|
1909
|
-
},
|
|
713
|
+
}, _callee7, this, [[6,, 11, 14]]);
|
|
1910
714
|
}));
|
|
1911
|
-
function
|
|
1912
|
-
return
|
|
715
|
+
function getThreadsByResourceId(_x7) {
|
|
716
|
+
return _getThreadsByResourceId.apply(this, arguments);
|
|
1913
717
|
}
|
|
1914
|
-
return
|
|
718
|
+
return getThreadsByResourceId;
|
|
1915
719
|
}();
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
var _this;
|
|
1922
|
-
_this = _MastraMemory.call(this) || this;
|
|
1923
|
-
_this.redis = void 0;
|
|
1924
|
-
_this.threadPrefix = 'thread:';
|
|
1925
|
-
_this.messagePrefix = 'messages:';
|
|
1926
|
-
_this.lockTimeouts = new Map();
|
|
1927
|
-
_this.redis = redis;
|
|
1928
|
-
return _this;
|
|
1929
|
-
}
|
|
1930
|
-
_inheritsLoose(RedisMemory, _MastraMemory);
|
|
1931
|
-
var _proto = RedisMemory.prototype;
|
|
1932
|
-
_proto.getThreadById = /*#__PURE__*/function () {
|
|
1933
|
-
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(threadId) {
|
|
1934
|
-
var thread;
|
|
1935
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1936
|
-
while (1) switch (_context.prev = _context.next) {
|
|
720
|
+
_proto.saveThread = /*#__PURE__*/function () {
|
|
721
|
+
var _saveThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref4) {
|
|
722
|
+
var thread, client, _result$rows2, id, title, createdAt, updatedAt, resourceid, metadata, result;
|
|
723
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
724
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
1937
725
|
case 0:
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
thread.updatedAt = new Date(thread.updatedAt);
|
|
1945
|
-
}
|
|
1946
|
-
return _context.abrupt("return", thread);
|
|
726
|
+
thread = _ref4.thread;
|
|
727
|
+
_context8.next = 3;
|
|
728
|
+
return this.ensureTablesExist();
|
|
729
|
+
case 3:
|
|
730
|
+
_context8.next = 5;
|
|
731
|
+
return this.pool.connect();
|
|
1947
732
|
case 5:
|
|
733
|
+
client = _context8.sent;
|
|
734
|
+
_context8.prev = 6;
|
|
735
|
+
id = thread.id, title = thread.title, createdAt = thread.createdAt, updatedAt = thread.updatedAt, resourceid = thread.resourceid, metadata = thread.metadata;
|
|
736
|
+
_context8.next = 10;
|
|
737
|
+
return client.query("\n INSERT INTO mastra_threads (id, title, created_at, updated_at, resourceid, metadata)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (id) DO UPDATE SET title = $2, updated_at = $4, resourceid = $5, metadata = $6\n RETURNING id, title, created_at AS createdAt, updated_at AS updatedAt, resourceid, metadata\n ", [id, title, createdAt, updatedAt, resourceid, JSON.stringify(metadata)]);
|
|
738
|
+
case 10:
|
|
739
|
+
result = _context8.sent;
|
|
740
|
+
return _context8.abrupt("return", result == null || (_result$rows2 = result.rows) == null ? void 0 : _result$rows2[0]);
|
|
741
|
+
case 12:
|
|
742
|
+
_context8.prev = 12;
|
|
743
|
+
client.release();
|
|
744
|
+
return _context8.finish(12);
|
|
745
|
+
case 15:
|
|
1948
746
|
case "end":
|
|
1949
|
-
return
|
|
747
|
+
return _context8.stop();
|
|
1950
748
|
}
|
|
1951
|
-
},
|
|
749
|
+
}, _callee8, this, [[6,, 12, 15]]);
|
|
1952
750
|
}));
|
|
1953
|
-
function
|
|
1954
|
-
return
|
|
751
|
+
function saveThread(_x8) {
|
|
752
|
+
return _saveThread.apply(this, arguments);
|
|
1955
753
|
}
|
|
1956
|
-
return
|
|
754
|
+
return saveThread;
|
|
1957
755
|
}();
|
|
1958
|
-
_proto.
|
|
1959
|
-
var
|
|
1960
|
-
|
|
1961
|
-
|
|
756
|
+
_proto.checkIfValidArgExists = /*#__PURE__*/function () {
|
|
757
|
+
var _checkIfValidArgExists = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_ref5) {
|
|
758
|
+
var hashedToolCallArgs, client, toolArgsResult;
|
|
759
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
760
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
1962
761
|
case 0:
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
return this.
|
|
762
|
+
hashedToolCallArgs = _ref5.hashedToolCallArgs;
|
|
763
|
+
_context9.next = 3;
|
|
764
|
+
return this.ensureTablesExist();
|
|
1966
765
|
case 3:
|
|
1967
|
-
|
|
1968
|
-
return this.
|
|
766
|
+
_context9.next = 5;
|
|
767
|
+
return this.pool.connect();
|
|
1969
768
|
case 5:
|
|
1970
|
-
|
|
1971
|
-
|
|
769
|
+
client = _context9.sent;
|
|
770
|
+
_context9.prev = 6;
|
|
771
|
+
_context9.next = 9;
|
|
772
|
+
return client.query(" SELECT tool_call_ids as toolCallIds, \n tool_call_args as toolCallArgs,\n created_at AS createdAt\n FROM mastra_messages\n WHERE tool_call_args::jsonb @> $1\n AND tool_call_args_expire_at > $2\n ORDER BY created_at ASC\n LIMIT 1", [JSON.stringify([hashedToolCallArgs]), new Date().toISOString()]);
|
|
773
|
+
case 9:
|
|
774
|
+
toolArgsResult = _context9.sent;
|
|
775
|
+
return _context9.abrupt("return", toolArgsResult.rows.length > 0);
|
|
776
|
+
case 13:
|
|
777
|
+
_context9.prev = 13;
|
|
778
|
+
_context9.t0 = _context9["catch"](6);
|
|
779
|
+
console.log('error checking if valid arg exists====', _context9.t0);
|
|
780
|
+
return _context9.abrupt("return", false);
|
|
781
|
+
case 17:
|
|
782
|
+
_context9.prev = 17;
|
|
783
|
+
client.release();
|
|
784
|
+
return _context9.finish(17);
|
|
785
|
+
case 20:
|
|
1972
786
|
case "end":
|
|
1973
|
-
return
|
|
787
|
+
return _context9.stop();
|
|
1974
788
|
}
|
|
1975
|
-
},
|
|
789
|
+
}, _callee9, this, [[6, 13, 17, 20]]);
|
|
1976
790
|
}));
|
|
1977
|
-
function
|
|
1978
|
-
return
|
|
791
|
+
function checkIfValidArgExists(_x9) {
|
|
792
|
+
return _checkIfValidArgExists.apply(this, arguments);
|
|
1979
793
|
}
|
|
1980
|
-
return
|
|
794
|
+
return checkIfValidArgExists;
|
|
1981
795
|
}();
|
|
1982
|
-
_proto.
|
|
1983
|
-
var
|
|
1984
|
-
var
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
796
|
+
_proto.getCachedToolResult = /*#__PURE__*/function () {
|
|
797
|
+
var _getCachedToolResult = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(_ref6) {
|
|
798
|
+
var threadId, toolArgs, toolName, client, hashedToolArgs, toolArgsResult, _toolArgsResult$rows$, _toolArgsResult$rows$2, _toolArgsResult$rows$3, _toolResult$rows$, toolCallArgs, toolCallIds, createdAt, toolCallArgsIndex, correspondingToolCallId, toolResult, toolResultContent, requiredToolResult;
|
|
799
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
800
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
1988
801
|
case 0:
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
802
|
+
threadId = _ref6.threadId, toolArgs = _ref6.toolArgs, toolName = _ref6.toolName;
|
|
803
|
+
_context10.next = 3;
|
|
804
|
+
return this.ensureTablesExist();
|
|
805
|
+
case 3:
|
|
806
|
+
console.log('checking for cached tool result====', JSON.stringify(toolArgs, null, 2));
|
|
807
|
+
_context10.next = 6;
|
|
808
|
+
return this.pool.connect();
|
|
809
|
+
case 6:
|
|
810
|
+
client = _context10.sent;
|
|
811
|
+
_context10.prev = 7;
|
|
812
|
+
hashedToolArgs = crypto.createHash('sha256').update(JSON.stringify({
|
|
813
|
+
args: toolArgs,
|
|
814
|
+
threadId: threadId,
|
|
815
|
+
toolName: toolName
|
|
816
|
+
})).digest('hex');
|
|
817
|
+
console.log('hashedToolArgs====', hashedToolArgs);
|
|
818
|
+
_context10.next = 12;
|
|
819
|
+
return client.query("SELECT tool_call_ids, \n tool_call_args,\n created_at\n FROM mastra_messages\n WHERE tool_call_args::jsonb @> $1\n AND tool_call_args_expire_at > $2\n ORDER BY created_at ASC\n LIMIT 1", [JSON.stringify([hashedToolArgs]), new Date().toISOString()]);
|
|
820
|
+
case 12:
|
|
821
|
+
toolArgsResult = _context10.sent;
|
|
822
|
+
if (!(toolArgsResult.rows.length > 0)) {
|
|
823
|
+
_context10.next = 36;
|
|
2005
824
|
break;
|
|
2006
825
|
}
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
826
|
+
console.log('toolArgsResult====', JSON.stringify(toolArgsResult.rows[0], null, 2));
|
|
827
|
+
toolCallArgs = JSON.parse((_toolArgsResult$rows$ = toolArgsResult.rows[0]) == null ? void 0 : _toolArgsResult$rows$.tool_call_args);
|
|
828
|
+
toolCallIds = JSON.parse((_toolArgsResult$rows$2 = toolArgsResult.rows[0]) == null ? void 0 : _toolArgsResult$rows$2.tool_call_ids);
|
|
829
|
+
createdAt = (_toolArgsResult$rows$3 = toolArgsResult.rows[0]) == null ? void 0 : _toolArgsResult$rows$3.created_at;
|
|
830
|
+
console.log('toolCallArgs====', JSON.stringify(toolCallArgs, null, 2));
|
|
831
|
+
console.log('toolCallIds====', JSON.stringify(toolCallIds, null, 2));
|
|
832
|
+
console.log('createdAt====', createdAt);
|
|
833
|
+
toolCallArgsIndex = toolCallArgs.findIndex(function (arg) {
|
|
834
|
+
return arg === hashedToolArgs;
|
|
2012
835
|
});
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
836
|
+
correspondingToolCallId = toolCallIds[toolCallArgsIndex];
|
|
837
|
+
console.log('correspondingToolCallId====', {
|
|
838
|
+
correspondingToolCallId: correspondingToolCallId,
|
|
839
|
+
toolCallArgsIndex: toolCallArgsIndex
|
|
840
|
+
});
|
|
841
|
+
_context10.next = 26;
|
|
842
|
+
return client.query("SELECT content \n FROM mastra_messages \n WHERE thread_id = $1\n AND tool_call_ids ILIKE $2\n AND type = 'tool-result'\n AND created_at = $3\n LIMIT 1", [threadId, "%" + correspondingToolCallId + "%", new Date(createdAt).toISOString()]);
|
|
843
|
+
case 26:
|
|
844
|
+
toolResult = _context10.sent;
|
|
845
|
+
console.log('called toolResult');
|
|
846
|
+
if (!(toolResult.rows.length === 0)) {
|
|
847
|
+
_context10.next = 31;
|
|
2020
848
|
break;
|
|
2021
849
|
}
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
case 3:
|
|
2033
|
-
_context3.next = 7;
|
|
2034
|
-
break;
|
|
2035
|
-
case 5:
|
|
2036
|
-
_context3.prev = 5;
|
|
2037
|
-
_context3.t0 = _context3["catch"](0);
|
|
2038
|
-
case 7:
|
|
2039
|
-
_this2.lockTimeouts["delete"](lockKey);
|
|
2040
|
-
case 8:
|
|
2041
|
-
case "end":
|
|
2042
|
-
return _context3.stop();
|
|
2043
|
-
}
|
|
2044
|
-
}, _callee3, null, [[0, 5]]);
|
|
2045
|
-
})), lockTimeout);
|
|
2046
|
-
this.lockTimeouts.set(lockKey, timeoutId);
|
|
2047
|
-
// Execute operation
|
|
2048
|
-
_context4.next = 22;
|
|
2049
|
-
return operation();
|
|
2050
|
-
case 22:
|
|
2051
|
-
return _context4.abrupt("return", _context4.sent);
|
|
2052
|
-
case 23:
|
|
2053
|
-
_context4.prev = 23;
|
|
2054
|
-
if (timeoutId !== undefined) {
|
|
2055
|
-
clearTimeout(timeoutId);
|
|
2056
|
-
this.lockTimeouts["delete"](lockKey);
|
|
2057
|
-
}
|
|
2058
|
-
if (!locked) {
|
|
2059
|
-
_context4.next = 33;
|
|
850
|
+
console.log('no tool result found');
|
|
851
|
+
return _context10.abrupt("return", null);
|
|
852
|
+
case 31:
|
|
853
|
+
toolResultContent = JSON.parse((_toolResult$rows$ = toolResult.rows[0]) == null ? void 0 : _toolResult$rows$.content);
|
|
854
|
+
requiredToolResult = toolResultContent.find(function (part) {
|
|
855
|
+
return part.toolCallId === correspondingToolCallId;
|
|
856
|
+
});
|
|
857
|
+
console.log('requiredToolResult====', JSON.stringify(requiredToolResult, null, 2));
|
|
858
|
+
if (!requiredToolResult) {
|
|
859
|
+
_context10.next = 36;
|
|
2060
860
|
break;
|
|
2061
861
|
}
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
return
|
|
2065
|
-
case
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
862
|
+
return _context10.abrupt("return", requiredToolResult.result);
|
|
863
|
+
case 36:
|
|
864
|
+
return _context10.abrupt("return", null);
|
|
865
|
+
case 39:
|
|
866
|
+
_context10.prev = 39;
|
|
867
|
+
_context10.t0 = _context10["catch"](7);
|
|
868
|
+
console.log('error getting cached tool result====', _context10.t0);
|
|
869
|
+
return _context10.abrupt("return", null);
|
|
870
|
+
case 43:
|
|
871
|
+
_context10.prev = 43;
|
|
872
|
+
client.release();
|
|
873
|
+
return _context10.finish(43);
|
|
874
|
+
case 46:
|
|
2074
875
|
case "end":
|
|
2075
|
-
return
|
|
876
|
+
return _context10.stop();
|
|
2076
877
|
}
|
|
2077
|
-
},
|
|
878
|
+
}, _callee10, this, [[7, 39, 43, 46]]);
|
|
2078
879
|
}));
|
|
2079
|
-
function
|
|
2080
|
-
return
|
|
880
|
+
function getCachedToolResult(_x10) {
|
|
881
|
+
return _getCachedToolResult.apply(this, arguments);
|
|
2081
882
|
}
|
|
2082
|
-
return
|
|
2083
|
-
}()
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
var _iterator, _step, timeout;
|
|
2090
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
2091
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
883
|
+
return getCachedToolResult;
|
|
884
|
+
}();
|
|
885
|
+
_proto.getContextWindow = /*#__PURE__*/function () {
|
|
886
|
+
var _getContextWindow = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(_ref7) {
|
|
887
|
+
var threadId, startDate, endDate, client, _result, result;
|
|
888
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
889
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
2092
890
|
case 0:
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
891
|
+
threadId = _ref7.threadId, startDate = _ref7.startDate, endDate = _ref7.endDate;
|
|
892
|
+
_context11.next = 3;
|
|
893
|
+
return this.ensureTablesExist();
|
|
894
|
+
case 3:
|
|
895
|
+
console.log('table exists');
|
|
896
|
+
_context11.next = 6;
|
|
897
|
+
return this.pool.connect();
|
|
898
|
+
case 6:
|
|
899
|
+
client = _context11.sent;
|
|
900
|
+
_context11.prev = 7;
|
|
901
|
+
if (!this.MAX_CONTEXT_TOKENS) {
|
|
902
|
+
_context11.next = 14;
|
|
903
|
+
break;
|
|
2097
904
|
}
|
|
2098
|
-
|
|
2099
|
-
|
|
905
|
+
_context11.next = 11;
|
|
906
|
+
return client.query("WITH RankedMessages AS (\n SELECT *,\n SUM(tokens) OVER (ORDER BY created_at DESC) as running_total\n FROM mastra_messages\n WHERE thread_id = $1\n AND type = 'text'\n " + (startDate ? "AND created_at >= '" + startDate.toISOString() + "'" : '') + "\n " + (endDate ? "AND created_at <= '" + endDate.toISOString() + "'" : '') + "\n ORDER BY created_at DESC\n )\n SELECT id, \n content, \n role, \n type,\n created_at AS createdAt, \n thread_id AS threadId\n FROM RankedMessages\n WHERE running_total <= $2\n ORDER BY created_at ASC", [threadId, this.MAX_CONTEXT_TOKENS]);
|
|
907
|
+
case 11:
|
|
908
|
+
_result = _context11.sent;
|
|
909
|
+
console.log('result===', JSON.stringify(_result.rows, null, 2));
|
|
910
|
+
return _context11.abrupt("return", this.processMessages(_result.rows));
|
|
911
|
+
case 14:
|
|
912
|
+
_context11.next = 16;
|
|
913
|
+
return client.query("SELECT id, \n content, \n role, \n type,\n created_at AS createdAt, \n thread_id AS threadId\n FROM mastra_messages\n WHERE thread_id = $1\n AND type = 'text'\n " + (startDate ? "AND created_at >= '" + startDate.toISOString() + "'" : '') + "\n " + (endDate ? "AND created_at <= '" + endDate.toISOString() + "'" : '') + "\n ORDER BY created_at ASC", [threadId]);
|
|
914
|
+
case 16:
|
|
915
|
+
result = _context11.sent;
|
|
916
|
+
console.log('result===', JSON.stringify(result.rows, null, 2));
|
|
917
|
+
return _context11.abrupt("return", this.processMessages(result.rows));
|
|
918
|
+
case 21:
|
|
919
|
+
_context11.prev = 21;
|
|
920
|
+
_context11.t0 = _context11["catch"](7);
|
|
921
|
+
console.log('error getting context window====', _context11.t0);
|
|
922
|
+
return _context11.abrupt("return", []);
|
|
923
|
+
case 25:
|
|
924
|
+
_context11.prev = 25;
|
|
925
|
+
client.release();
|
|
926
|
+
return _context11.finish(25);
|
|
927
|
+
case 28:
|
|
2100
928
|
case "end":
|
|
2101
|
-
return
|
|
929
|
+
return _context11.stop();
|
|
2102
930
|
}
|
|
2103
|
-
},
|
|
931
|
+
}, _callee11, this, [[7, 21, 25, 28]]);
|
|
2104
932
|
}));
|
|
2105
|
-
function
|
|
2106
|
-
return
|
|
933
|
+
function getContextWindow(_x11) {
|
|
934
|
+
return _getContextWindow.apply(this, arguments);
|
|
2107
935
|
}
|
|
2108
|
-
return
|
|
936
|
+
return getContextWindow;
|
|
2109
937
|
}();
|
|
2110
938
|
_proto.saveMessages = /*#__PURE__*/function () {
|
|
2111
|
-
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
2112
|
-
var
|
|
2113
|
-
var
|
|
2114
|
-
return _regeneratorRuntime().wrap(function
|
|
2115
|
-
while (1) switch (
|
|
939
|
+
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(_ref8) {
|
|
940
|
+
var _this2 = this;
|
|
941
|
+
var messages, client, _loop2, _iterator2, _step2;
|
|
942
|
+
return _regeneratorRuntime().wrap(function _callee12$(_context13) {
|
|
943
|
+
while (1) switch (_context13.prev = _context13.next) {
|
|
2116
944
|
case 0:
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
return
|
|
2132
|
-
|
|
2133
|
-
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
2134
|
-
var _Object$entries$_i, key, threadMessages;
|
|
2135
|
-
return _regeneratorRuntime().wrap(function _loop$(_context7) {
|
|
2136
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
945
|
+
messages = _ref8.messages;
|
|
946
|
+
_context13.next = 3;
|
|
947
|
+
return this.ensureTablesExist();
|
|
948
|
+
case 3:
|
|
949
|
+
_context13.next = 5;
|
|
950
|
+
return this.pool.connect();
|
|
951
|
+
case 5:
|
|
952
|
+
client = _context13.sent;
|
|
953
|
+
_context13.prev = 6;
|
|
954
|
+
_context13.next = 9;
|
|
955
|
+
return client.query('BEGIN');
|
|
956
|
+
case 9:
|
|
957
|
+
_loop2 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop2() {
|
|
958
|
+
var message, id, content, role, createdAt, threadId, toolCallIds, toolCallArgs, type, toolNames, tokens, _content$, contentMssg, hashedToolCallArgs, validArgExists, i, isValid, toolCallArgsExpireAt;
|
|
959
|
+
return _regeneratorRuntime().wrap(function _loop2$(_context12) {
|
|
960
|
+
while (1) switch (_context12.prev = _context12.next) {
|
|
2137
961
|
case 0:
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
return _this3.redis.set("" + _this3.threadPrefix + thread.id, thread);
|
|
2190
|
-
case 21:
|
|
2191
|
-
case "end":
|
|
2192
|
-
return _context6.stop();
|
|
2193
|
-
}
|
|
2194
|
-
}, _callee6);
|
|
2195
|
-
})));
|
|
2196
|
-
case 3:
|
|
962
|
+
message = _step2.value;
|
|
963
|
+
console.log('saving message====', JSON.stringify(message, null, 2));
|
|
964
|
+
id = message.id, content = message.content, role = message.role, createdAt = message.createdAt, threadId = message.threadId, toolCallIds = message.toolCallIds, toolCallArgs = message.toolCallArgs, type = message.type, toolNames = message.toolNames;
|
|
965
|
+
tokens = null;
|
|
966
|
+
if (type === 'text') {
|
|
967
|
+
contentMssg = role === 'assistant' ? ((_content$ = content[0]) == null ? void 0 : _content$.text) || '' : content;
|
|
968
|
+
tokens = _this2.estimateTokens(contentMssg);
|
|
969
|
+
}
|
|
970
|
+
// Hash the toolCallArgs if they exist
|
|
971
|
+
hashedToolCallArgs = toolCallArgs ? toolCallArgs.map(function (args, index) {
|
|
972
|
+
return crypto.createHash('sha256').update(JSON.stringify({
|
|
973
|
+
args: args,
|
|
974
|
+
threadId: threadId,
|
|
975
|
+
toolName: toolNames == null ? void 0 : toolNames[index]
|
|
976
|
+
})).digest('hex');
|
|
977
|
+
}) : null;
|
|
978
|
+
validArgExists = false;
|
|
979
|
+
if (!(hashedToolCallArgs != null && hashedToolCallArgs.length)) {
|
|
980
|
+
_context12.next = 20;
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
// Check all args sequentially
|
|
984
|
+
validArgExists = true; // Start true and set to false if any check fails
|
|
985
|
+
i = 0;
|
|
986
|
+
case 10:
|
|
987
|
+
if (!(i < hashedToolCallArgs.length)) {
|
|
988
|
+
_context12.next = 20;
|
|
989
|
+
break;
|
|
990
|
+
}
|
|
991
|
+
_context12.next = 13;
|
|
992
|
+
return _this2.checkIfValidArgExists({
|
|
993
|
+
hashedToolCallArgs: hashedToolCallArgs[i]
|
|
994
|
+
});
|
|
995
|
+
case 13:
|
|
996
|
+
isValid = _context12.sent;
|
|
997
|
+
if (isValid) {
|
|
998
|
+
_context12.next = 17;
|
|
999
|
+
break;
|
|
1000
|
+
}
|
|
1001
|
+
validArgExists = false;
|
|
1002
|
+
return _context12.abrupt("break", 20);
|
|
1003
|
+
case 17:
|
|
1004
|
+
i++;
|
|
1005
|
+
_context12.next = 10;
|
|
1006
|
+
break;
|
|
1007
|
+
case 20:
|
|
1008
|
+
toolCallArgsExpireAt = !toolCallArgs ? null : validArgExists ? createdAt : new Date(createdAt.getTime() + 5 * 60 * 1000); // 5 minutes
|
|
1009
|
+
console.log('just before query');
|
|
1010
|
+
_context12.next = 24;
|
|
1011
|
+
return client.query("\n INSERT INTO mastra_messages (id, content, role, created_at, thread_id, tool_call_ids, tool_call_args, type, tokens, tool_call_args_expire_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ", [id, JSON.stringify(content), role, createdAt.toISOString(), threadId, JSON.stringify(toolCallIds), JSON.stringify(hashedToolCallArgs), type, tokens, toolCallArgsExpireAt == null ? void 0 : toolCallArgsExpireAt.toISOString()]);
|
|
1012
|
+
case 24:
|
|
2197
1013
|
case "end":
|
|
2198
|
-
return
|
|
1014
|
+
return _context12.stop();
|
|
2199
1015
|
}
|
|
2200
|
-
},
|
|
1016
|
+
}, _loop2);
|
|
2201
1017
|
});
|
|
2202
|
-
|
|
2203
|
-
case
|
|
2204
|
-
if (
|
|
2205
|
-
|
|
1018
|
+
_iterator2 = _createForOfIteratorHelperLoose(messages);
|
|
1019
|
+
case 11:
|
|
1020
|
+
if ((_step2 = _iterator2()).done) {
|
|
1021
|
+
_context13.next = 15;
|
|
2206
1022
|
break;
|
|
2207
1023
|
}
|
|
2208
|
-
return
|
|
2209
|
-
case
|
|
2210
|
-
|
|
2211
|
-
_context8.next = 5;
|
|
1024
|
+
return _context13.delegateYield(_loop2(), "t0", 13);
|
|
1025
|
+
case 13:
|
|
1026
|
+
_context13.next = 11;
|
|
2212
1027
|
break;
|
|
2213
|
-
case
|
|
2214
|
-
|
|
2215
|
-
|
|
1028
|
+
case 15:
|
|
1029
|
+
console.log('just after query');
|
|
1030
|
+
_context13.next = 18;
|
|
1031
|
+
return client.query('COMMIT');
|
|
1032
|
+
case 18:
|
|
1033
|
+
return _context13.abrupt("return", messages);
|
|
1034
|
+
case 21:
|
|
1035
|
+
_context13.prev = 21;
|
|
1036
|
+
_context13.t1 = _context13["catch"](6);
|
|
1037
|
+
_context13.next = 25;
|
|
1038
|
+
return client.query('ROLLBACK');
|
|
1039
|
+
case 25:
|
|
1040
|
+
throw _context13.t1;
|
|
1041
|
+
case 26:
|
|
1042
|
+
_context13.prev = 26;
|
|
1043
|
+
client.release();
|
|
1044
|
+
return _context13.finish(26);
|
|
1045
|
+
case 29:
|
|
2216
1046
|
case "end":
|
|
2217
|
-
return
|
|
1047
|
+
return _context13.stop();
|
|
2218
1048
|
}
|
|
2219
|
-
},
|
|
1049
|
+
}, _callee12, this, [[6, 21, 26, 29]]);
|
|
2220
1050
|
}));
|
|
2221
|
-
function saveMessages(
|
|
1051
|
+
function saveMessages(_x12) {
|
|
2222
1052
|
return _saveMessages.apply(this, arguments);
|
|
2223
1053
|
}
|
|
2224
1054
|
return saveMessages;
|
|
2225
1055
|
}();
|
|
2226
|
-
_proto.addMessage = /*#__PURE__*/function () {
|
|
2227
|
-
var _addMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(threadId, content, role) {
|
|
2228
|
-
var message;
|
|
2229
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context9) {
|
|
2230
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
2231
|
-
case 0:
|
|
2232
|
-
message = {
|
|
2233
|
-
id: this.generateId(),
|
|
2234
|
-
content: content,
|
|
2235
|
-
role: role,
|
|
2236
|
-
createdAt: new Date(),
|
|
2237
|
-
threadId: threadId
|
|
2238
|
-
};
|
|
2239
|
-
_context9.next = 3;
|
|
2240
|
-
return this.saveMessages([message]);
|
|
2241
|
-
case 3:
|
|
2242
|
-
return _context9.abrupt("return", message);
|
|
2243
|
-
case 4:
|
|
2244
|
-
case "end":
|
|
2245
|
-
return _context9.stop();
|
|
2246
|
-
}
|
|
2247
|
-
}, _callee8, this);
|
|
2248
|
-
}));
|
|
2249
|
-
function addMessage(_x6, _x7, _x8) {
|
|
2250
|
-
return _addMessage.apply(this, arguments);
|
|
2251
|
-
}
|
|
2252
|
-
return addMessage;
|
|
2253
|
-
}();
|
|
2254
1056
|
_proto.getMessages = /*#__PURE__*/function () {
|
|
2255
|
-
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
2256
|
-
var messages;
|
|
2257
|
-
return _regeneratorRuntime().wrap(function
|
|
2258
|
-
while (1) switch (
|
|
1057
|
+
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13(_ref9) {
|
|
1058
|
+
var threadId, client, result, messages, uiMessages;
|
|
1059
|
+
return _regeneratorRuntime().wrap(function _callee13$(_context14) {
|
|
1060
|
+
while (1) switch (_context14.prev = _context14.next) {
|
|
2259
1061
|
case 0:
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
break;
|
|
2267
|
-
}
|
|
2268
|
-
_context10.t0 = [];
|
|
1062
|
+
threadId = _ref9.threadId;
|
|
1063
|
+
_context14.next = 3;
|
|
1064
|
+
return this.ensureTablesExist();
|
|
1065
|
+
case 3:
|
|
1066
|
+
_context14.next = 5;
|
|
1067
|
+
return this.pool.connect();
|
|
2269
1068
|
case 5:
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
createdAt
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
1069
|
+
client = _context14.sent;
|
|
1070
|
+
_context14.prev = 6;
|
|
1071
|
+
_context14.next = 9;
|
|
1072
|
+
return client.query("\n SELECT \n id, \n content, \n role, \n type,\n created_at AS createdAt, \n thread_id AS threadId\n FROM mastra_messages\n WHERE thread_id = $1\n ORDER BY created_at ASC\n ", [threadId]);
|
|
1073
|
+
case 9:
|
|
1074
|
+
result = _context14.sent;
|
|
1075
|
+
messages = this.processMessages(result.rows);
|
|
1076
|
+
uiMessages = this.convertToUIMessages(messages);
|
|
1077
|
+
return _context14.abrupt("return", {
|
|
1078
|
+
messages: messages,
|
|
1079
|
+
uiMessages: uiMessages
|
|
1080
|
+
});
|
|
1081
|
+
case 13:
|
|
1082
|
+
_context14.prev = 13;
|
|
1083
|
+
client.release();
|
|
1084
|
+
return _context14.finish(13);
|
|
1085
|
+
case 16:
|
|
2277
1086
|
case "end":
|
|
2278
|
-
return
|
|
1087
|
+
return _context14.stop();
|
|
2279
1088
|
}
|
|
2280
|
-
},
|
|
1089
|
+
}, _callee13, this, [[6,, 13, 16]]);
|
|
2281
1090
|
}));
|
|
2282
|
-
function getMessages(
|
|
1091
|
+
function getMessages(_x13) {
|
|
2283
1092
|
return _getMessages.apply(this, arguments);
|
|
2284
1093
|
}
|
|
2285
1094
|
return getMessages;
|
|
2286
1095
|
}();
|
|
2287
|
-
|
|
2288
|
-
var _getAllThreadIds = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10() {
|
|
2289
|
-
return _regeneratorRuntime().wrap(function _callee10$(_context11) {
|
|
2290
|
-
while (1) switch (_context11.prev = _context11.next) {
|
|
2291
|
-
case 0:
|
|
2292
|
-
return _context11.abrupt("return", this.redis.smembers('threads'));
|
|
2293
|
-
case 1:
|
|
2294
|
-
case "end":
|
|
2295
|
-
return _context11.stop();
|
|
2296
|
-
}
|
|
2297
|
-
}, _callee10, this);
|
|
2298
|
-
}));
|
|
2299
|
-
function getAllThreadIds() {
|
|
2300
|
-
return _getAllThreadIds.apply(this, arguments);
|
|
2301
|
-
}
|
|
2302
|
-
return getAllThreadIds;
|
|
2303
|
-
}();
|
|
2304
|
-
_proto.deleteThread = /*#__PURE__*/function () {
|
|
2305
|
-
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(threadId) {
|
|
2306
|
-
var pipeline;
|
|
2307
|
-
return _regeneratorRuntime().wrap(function _callee11$(_context12) {
|
|
2308
|
-
while (1) switch (_context12.prev = _context12.next) {
|
|
2309
|
-
case 0:
|
|
2310
|
-
pipeline = this.redis.pipeline();
|
|
2311
|
-
pipeline.del("" + this.threadPrefix + threadId);
|
|
2312
|
-
pipeline.del("" + this.messagePrefix + threadId);
|
|
2313
|
-
pipeline.srem('threads', threadId);
|
|
2314
|
-
_context12.next = 6;
|
|
2315
|
-
return pipeline.exec();
|
|
2316
|
-
case 6:
|
|
2317
|
-
case "end":
|
|
2318
|
-
return _context12.stop();
|
|
2319
|
-
}
|
|
2320
|
-
}, _callee11, this);
|
|
2321
|
-
}));
|
|
2322
|
-
function deleteThread(_x10) {
|
|
2323
|
-
return _deleteThread.apply(this, arguments);
|
|
2324
|
-
}
|
|
2325
|
-
return deleteThread;
|
|
2326
|
-
}();
|
|
2327
|
-
_proto.getThreads = /*#__PURE__*/function () {
|
|
2328
|
-
var _getThreads = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(threadIds) {
|
|
2329
|
-
var _this4 = this;
|
|
2330
|
-
var threads;
|
|
2331
|
-
return _regeneratorRuntime().wrap(function _callee12$(_context13) {
|
|
2332
|
-
while (1) switch (_context13.prev = _context13.next) {
|
|
2333
|
-
case 0:
|
|
2334
|
-
_context13.next = 2;
|
|
2335
|
-
return Promise.all(threadIds.map(function (id) {
|
|
2336
|
-
return _this4.getThreadById(id);
|
|
2337
|
-
}));
|
|
2338
|
-
case 2:
|
|
2339
|
-
threads = _context13.sent;
|
|
2340
|
-
return _context13.abrupt("return", threads.filter(function (t) {
|
|
2341
|
-
return t !== null;
|
|
2342
|
-
}));
|
|
2343
|
-
case 4:
|
|
2344
|
-
case "end":
|
|
2345
|
-
return _context13.stop();
|
|
2346
|
-
}
|
|
2347
|
-
}, _callee12);
|
|
2348
|
-
}));
|
|
2349
|
-
function getThreads(_x11) {
|
|
2350
|
-
return _getThreads.apply(this, arguments);
|
|
2351
|
-
}
|
|
2352
|
-
return getThreads;
|
|
2353
|
-
}();
|
|
2354
|
-
_proto.generateId = function generateId() {
|
|
2355
|
-
return crypto.randomUUID();
|
|
2356
|
-
};
|
|
2357
|
-
return RedisMemory;
|
|
1096
|
+
return PgMemory;
|
|
2358
1097
|
}(MastraMemory);
|
|
2359
1098
|
|
|
2360
|
-
export {
|
|
1099
|
+
export { PgMemory };
|
|
2361
1100
|
//# sourceMappingURL=memory.esm.js.map
|