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