@mastra/memory 0.0.2-alpha.1 → 0.0.2-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/kv/upstash.d.ts +71 -0
- package/dist/memory.cjs.development.js +937 -448
- 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 +937 -449
- package/dist/memory.esm.js.map +1 -1
- package/dist/postgres/index.d.ts +28 -18
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/kv/upstash.test.ts +253 -0
- package/src/kv/upstash.ts +298 -0
- package/src/postgres/index.ts +175 -257
package/dist/memory.esm.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { MastraMemory } from '@mastra/core';
|
|
2
2
|
import crypto from 'crypto';
|
|
3
3
|
import pg from 'pg';
|
|
4
|
+
import { Redis } from '@upstash/redis';
|
|
4
5
|
|
|
5
6
|
function _arrayLikeToArray(r, a) {
|
|
6
7
|
(null == a || a > r.length) && (a = r.length);
|
|
@@ -381,215 +382,129 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
381
382
|
var _this;
|
|
382
383
|
_this = _MastraMemory.call(this) || this;
|
|
383
384
|
_this.pool = void 0;
|
|
384
|
-
_this.
|
|
385
|
+
_this.hasTables = false;
|
|
385
386
|
_this.pool = new Pool({
|
|
386
387
|
connectionString: config.connectionString
|
|
387
388
|
});
|
|
388
389
|
_this.MAX_CONTEXT_TOKENS = config.maxTokens;
|
|
389
390
|
return _this;
|
|
390
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Threads
|
|
394
|
+
*/
|
|
391
395
|
_inheritsLoose(PgMemory, _MastraMemory);
|
|
392
396
|
var _proto = PgMemory.prototype;
|
|
393
|
-
_proto.
|
|
394
|
-
|
|
395
|
-
|
|
397
|
+
_proto.getThreadById =
|
|
398
|
+
/*#__PURE__*/
|
|
399
|
+
function () {
|
|
400
|
+
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
401
|
+
var threadId, client, result;
|
|
396
402
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
397
403
|
while (1) switch (_context.prev = _context.next) {
|
|
398
404
|
case 0:
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
405
|
+
threadId = _ref.threadId;
|
|
406
|
+
_context.next = 3;
|
|
407
|
+
return this.ensureTablesExist();
|
|
408
|
+
case 3:
|
|
403
409
|
_context.next = 5;
|
|
404
|
-
return
|
|
410
|
+
return this.pool.connect();
|
|
405
411
|
case 5:
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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;
|
|
409
421
|
client.release();
|
|
410
|
-
_context.
|
|
411
|
-
|
|
412
|
-
case 10:
|
|
422
|
+
return _context.finish(11);
|
|
423
|
+
case 14:
|
|
413
424
|
case "end":
|
|
414
425
|
return _context.stop();
|
|
415
426
|
}
|
|
416
|
-
}, _callee, this);
|
|
427
|
+
}, _callee, this, [[6,, 11, 14]]);
|
|
417
428
|
}));
|
|
418
|
-
function
|
|
419
|
-
return
|
|
420
|
-
}
|
|
421
|
-
return drop;
|
|
422
|
-
}() // Simplified token estimation
|
|
423
|
-
;
|
|
424
|
-
_proto.estimateTokens = function estimateTokens(text) {
|
|
425
|
-
return Math.ceil(text.split(' ').length * 1.3);
|
|
426
|
-
};
|
|
427
|
-
_proto.processMessages = function processMessages(messages) {
|
|
428
|
-
return messages.map(function (mssg) {
|
|
429
|
-
return _extends({}, mssg, {
|
|
430
|
-
content: typeof mssg.content === 'string' ? JSON.parse(mssg.content) : mssg.content
|
|
431
|
-
});
|
|
432
|
-
});
|
|
433
|
-
};
|
|
434
|
-
_proto.convertToUIMessages = function convertToUIMessages(messages) {
|
|
435
|
-
function addToolMessageToChat(_ref) {
|
|
436
|
-
var toolMessage = _ref.toolMessage,
|
|
437
|
-
messages = _ref.messages,
|
|
438
|
-
toolResultContents = _ref.toolResultContents;
|
|
439
|
-
var chatMessages = messages.map(function (message) {
|
|
440
|
-
if (message.toolInvocations) {
|
|
441
|
-
return _extends({}, message, {
|
|
442
|
-
toolInvocations: message.toolInvocations.map(function (toolInvocation) {
|
|
443
|
-
var toolResult = toolMessage.content.find(function (tool) {
|
|
444
|
-
return tool.toolCallId === toolInvocation.toolCallId;
|
|
445
|
-
});
|
|
446
|
-
if (toolResult) {
|
|
447
|
-
return _extends({}, toolInvocation, {
|
|
448
|
-
state: 'result',
|
|
449
|
-
result: toolResult.result
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
return toolInvocation;
|
|
453
|
-
})
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
return message;
|
|
457
|
-
});
|
|
458
|
-
var resultContents = [].concat(toolResultContents, toolMessage.content);
|
|
459
|
-
return {
|
|
460
|
-
chatMessages: chatMessages,
|
|
461
|
-
toolResultContents: resultContents
|
|
462
|
-
};
|
|
429
|
+
function getThreadById(_x) {
|
|
430
|
+
return _getThreadById.apply(this, arguments);
|
|
463
431
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
toolResultContents: obj.toolResultContents
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
var textContent = '';
|
|
473
|
-
var toolInvocations = [];
|
|
474
|
-
if (typeof message.content === 'string') {
|
|
475
|
-
textContent = message.content;
|
|
476
|
-
} else if (Array.isArray(message.content)) {
|
|
477
|
-
var _loop = function _loop() {
|
|
478
|
-
var content = _step.value;
|
|
479
|
-
if (content.type === 'text') {
|
|
480
|
-
textContent += content.text;
|
|
481
|
-
} else if (content.type === 'tool-call') {
|
|
482
|
-
var toolResult = obj.toolResultContents.find(function (tool) {
|
|
483
|
-
return tool.toolCallId === content.toolCallId;
|
|
484
|
-
});
|
|
485
|
-
toolInvocations.push({
|
|
486
|
-
state: toolResult ? 'result' : 'call',
|
|
487
|
-
toolCallId: content.toolCallId,
|
|
488
|
-
toolName: content.toolName,
|
|
489
|
-
args: content.args,
|
|
490
|
-
result: toolResult == null ? void 0 : toolResult.result
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
for (var _iterator = _createForOfIteratorHelperLoose(message.content), _step; !(_step = _iterator()).done;) {
|
|
495
|
-
_loop();
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
obj.chatMessages.push({
|
|
499
|
-
id: message.id,
|
|
500
|
-
role: message.role,
|
|
501
|
-
content: textContent,
|
|
502
|
-
toolInvocations: toolInvocations
|
|
503
|
-
});
|
|
504
|
-
return obj;
|
|
505
|
-
}, {
|
|
506
|
-
chatMessages: [],
|
|
507
|
-
toolResultContents: []
|
|
508
|
-
}),
|
|
509
|
-
chatMessages = _messages$reduce.chatMessages;
|
|
510
|
-
return chatMessages;
|
|
511
|
-
};
|
|
512
|
-
_proto.ensureTablesExist = /*#__PURE__*/function () {
|
|
513
|
-
var _ensureTablesExist = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
514
|
-
var client, _threadsResult$rows, _messagesResult$rows, threadsResult, messagesResult;
|
|
432
|
+
return getThreadById;
|
|
433
|
+
}();
|
|
434
|
+
_proto.getThreadsByResourceId = /*#__PURE__*/function () {
|
|
435
|
+
var _getThreadsByResourceId = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref2) {
|
|
436
|
+
var resourceid, client, result;
|
|
515
437
|
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
516
438
|
while (1) switch (_context2.prev = _context2.next) {
|
|
517
439
|
case 0:
|
|
518
|
-
|
|
440
|
+
resourceid = _ref2.resourceid;
|
|
441
|
+
_context2.next = 3;
|
|
442
|
+
return this.ensureTablesExist();
|
|
443
|
+
case 3:
|
|
444
|
+
_context2.next = 5;
|
|
519
445
|
return this.pool.connect();
|
|
520
|
-
case
|
|
446
|
+
case 5:
|
|
521
447
|
client = _context2.sent;
|
|
522
|
-
_context2.prev =
|
|
523
|
-
_context2.next =
|
|
524
|
-
return client.query("\n SELECT
|
|
525
|
-
case
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
531
|
-
_context2.next = 10;
|
|
532
|
-
return client.query("\n CREATE TABLE IF NOT EXISTS mastra_threads (\n id UUID PRIMARY KEY,\n resourceid TEXT,\n title TEXT,\n created_at TIMESTAMP WITH TIME ZONE NOT NULL,\n updated_at TIMESTAMP WITH TIME ZONE NOT NULL,\n metadata JSONB\n );\n ");
|
|
533
|
-
case 10:
|
|
534
|
-
_context2.next = 12;
|
|
535
|
-
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_messages'\n );\n ");
|
|
536
|
-
case 12:
|
|
537
|
-
messagesResult = _context2.sent;
|
|
538
|
-
if (messagesResult != null && (_messagesResult$rows = messagesResult.rows) != null && (_messagesResult$rows = _messagesResult$rows[0]) != null && _messagesResult$rows.exists) {
|
|
539
|
-
_context2.next = 16;
|
|
540
|
-
break;
|
|
541
|
-
}
|
|
542
|
-
_context2.next = 16;
|
|
543
|
-
return client.query("\n CREATE TABLE IF NOT EXISTS mastra_messages (\n id UUID PRIMARY KEY,\n content TEXT NOT NULL,\n role VARCHAR(20) NOT NULL,\n created_at TIMESTAMP WITH TIME ZONE NOT NULL,\n tool_call_ids TEXT DEFAULT NULL,\n tool_call_args TEXT DEFAULT NULL,\n tool_call_args_expire_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,\n type VARCHAR(20) NOT NULL,\n tokens INTEGER DEFAULT NULL,\n thread_id UUID NOT NULL,\n FOREIGN KEY (thread_id) REFERENCES mastra_threads(id)\n );\n ");
|
|
544
|
-
case 16:
|
|
545
|
-
_context2.prev = 16;
|
|
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;
|
|
546
456
|
client.release();
|
|
547
|
-
return _context2.finish(
|
|
548
|
-
case
|
|
457
|
+
return _context2.finish(11);
|
|
458
|
+
case 14:
|
|
549
459
|
case "end":
|
|
550
460
|
return _context2.stop();
|
|
551
461
|
}
|
|
552
|
-
}, _callee2, this, [[
|
|
462
|
+
}, _callee2, this, [[6,, 11, 14]]);
|
|
553
463
|
}));
|
|
554
|
-
function
|
|
555
|
-
return
|
|
464
|
+
function getThreadsByResourceId(_x2) {
|
|
465
|
+
return _getThreadsByResourceId.apply(this, arguments);
|
|
556
466
|
}
|
|
557
|
-
return
|
|
467
|
+
return getThreadsByResourceId;
|
|
558
468
|
}();
|
|
559
|
-
_proto.
|
|
560
|
-
var
|
|
561
|
-
var client, _result$rows, result;
|
|
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;
|
|
562
472
|
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
563
473
|
while (1) switch (_context3.prev = _context3.next) {
|
|
564
474
|
case 0:
|
|
565
|
-
|
|
475
|
+
thread = _ref3.thread;
|
|
476
|
+
_context3.next = 3;
|
|
477
|
+
return this.ensureTablesExist();
|
|
478
|
+
case 3:
|
|
479
|
+
_context3.next = 5;
|
|
566
480
|
return this.pool.connect();
|
|
567
|
-
case
|
|
481
|
+
case 5:
|
|
568
482
|
client = _context3.sent;
|
|
569
|
-
_context3.prev =
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
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:
|
|
573
488
|
result = _context3.sent;
|
|
574
489
|
return _context3.abrupt("return", result == null || (_result$rows = result.rows) == null ? void 0 : _result$rows[0]);
|
|
575
|
-
case
|
|
576
|
-
_context3.prev =
|
|
490
|
+
case 12:
|
|
491
|
+
_context3.prev = 12;
|
|
577
492
|
client.release();
|
|
578
|
-
return _context3.finish(
|
|
579
|
-
case
|
|
493
|
+
return _context3.finish(12);
|
|
494
|
+
case 15:
|
|
580
495
|
case "end":
|
|
581
496
|
return _context3.stop();
|
|
582
497
|
}
|
|
583
|
-
}, _callee3, this, [[
|
|
498
|
+
}, _callee3, this, [[6,, 12, 15]]);
|
|
584
499
|
}));
|
|
585
|
-
function
|
|
586
|
-
return
|
|
500
|
+
function saveThread(_x3) {
|
|
501
|
+
return _saveThread.apply(this, arguments);
|
|
587
502
|
}
|
|
588
|
-
return
|
|
503
|
+
return saveThread;
|
|
589
504
|
}();
|
|
590
|
-
_proto.
|
|
591
|
-
var
|
|
592
|
-
var client;
|
|
505
|
+
_proto.updateThread = /*#__PURE__*/function () {
|
|
506
|
+
var _updateThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(id, title, metadata) {
|
|
507
|
+
var client, _result$rows2, result;
|
|
593
508
|
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
594
509
|
while (1) switch (_context4.prev = _context4.next) {
|
|
595
510
|
case 0:
|
|
@@ -599,10 +514,10 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
599
514
|
client = _context4.sent;
|
|
600
515
|
_context4.prev = 3;
|
|
601
516
|
_context4.next = 6;
|
|
602
|
-
return client.query("\n
|
|
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]);
|
|
603
518
|
case 6:
|
|
604
|
-
|
|
605
|
-
return
|
|
519
|
+
result = _context4.sent;
|
|
520
|
+
return _context4.abrupt("return", result == null || (_result$rows2 = result.rows) == null ? void 0 : _result$rows2[0]);
|
|
606
521
|
case 8:
|
|
607
522
|
_context4.prev = 8;
|
|
608
523
|
client.release();
|
|
@@ -613,13 +528,13 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
613
528
|
}
|
|
614
529
|
}, _callee4, this, [[3,, 8, 11]]);
|
|
615
530
|
}));
|
|
616
|
-
function
|
|
617
|
-
return
|
|
531
|
+
function updateThread(_x4, _x5, _x6) {
|
|
532
|
+
return _updateThread.apply(this, arguments);
|
|
618
533
|
}
|
|
619
|
-
return
|
|
534
|
+
return updateThread;
|
|
620
535
|
}();
|
|
621
|
-
_proto.
|
|
622
|
-
var
|
|
536
|
+
_proto.deleteThread = /*#__PURE__*/function () {
|
|
537
|
+
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(id) {
|
|
623
538
|
var client;
|
|
624
539
|
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
625
540
|
while (1) switch (_context5.prev = _context5.next) {
|
|
@@ -630,197 +545,99 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
630
545
|
client = _context5.sent;
|
|
631
546
|
_context5.prev = 3;
|
|
632
547
|
_context5.next = 6;
|
|
633
|
-
return client.query("\n DELETE FROM mastra_messages\n WHERE
|
|
548
|
+
return client.query("\n DELETE FROM mastra_messages\n WHERE thread_id = $1\n ", [id]);
|
|
634
549
|
case 6:
|
|
635
|
-
_context5.
|
|
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;
|
|
636
554
|
client.release();
|
|
637
|
-
return _context5.finish(
|
|
638
|
-
case
|
|
555
|
+
return _context5.finish(8);
|
|
556
|
+
case 11:
|
|
639
557
|
case "end":
|
|
640
558
|
return _context5.stop();
|
|
641
559
|
}
|
|
642
|
-
}, _callee5, this, [[3,,
|
|
560
|
+
}, _callee5, this, [[3,, 8, 11]]);
|
|
643
561
|
}));
|
|
644
|
-
function
|
|
645
|
-
return
|
|
562
|
+
function deleteThread(_x7) {
|
|
563
|
+
return _deleteThread.apply(this, arguments);
|
|
646
564
|
}
|
|
647
|
-
return
|
|
648
|
-
}()
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
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;
|
|
652
576
|
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
653
577
|
while (1) switch (_context6.prev = _context6.next) {
|
|
654
578
|
case 0:
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
_context6.next = 4;
|
|
658
|
-
return this.ensureTablesExist();
|
|
659
|
-
case 4:
|
|
660
|
-
_context6.next = 6;
|
|
661
|
-
return this.pool.connect();
|
|
662
|
-
case 6:
|
|
663
|
-
client = _context6.sent;
|
|
664
|
-
_context6.prev = 7;
|
|
665
|
-
_context6.next = 10;
|
|
666
|
-
return client.query("\n SELECT id, title, created_at AS createdAt, updated_at AS updatedAt, resourceid, metadata\n FROM mastra_threads\n WHERE id = $1\n ", [threadId]);
|
|
667
|
-
case 10:
|
|
668
|
-
result = _context6.sent;
|
|
669
|
-
return _context6.abrupt("return", result.rows[0] || null);
|
|
670
|
-
case 12:
|
|
671
|
-
_context6.prev = 12;
|
|
672
|
-
client.release();
|
|
673
|
-
return _context6.finish(12);
|
|
674
|
-
case 15:
|
|
675
|
-
case "end":
|
|
676
|
-
return _context6.stop();
|
|
677
|
-
}
|
|
678
|
-
}, _callee6, this, [[7,, 12, 15]]);
|
|
679
|
-
}));
|
|
680
|
-
function getThreadById(_x6) {
|
|
681
|
-
return _getThreadById.apply(this, arguments);
|
|
682
|
-
}
|
|
683
|
-
return getThreadById;
|
|
684
|
-
}();
|
|
685
|
-
_proto.getThreadsByResourceId = /*#__PURE__*/function () {
|
|
686
|
-
var _getThreadsByResourceId = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref3) {
|
|
687
|
-
var resourceid, client, result;
|
|
688
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
689
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
690
|
-
case 0:
|
|
691
|
-
resourceid = _ref3.resourceid;
|
|
692
|
-
_context7.next = 3;
|
|
693
|
-
return this.ensureTablesExist();
|
|
694
|
-
case 3:
|
|
695
|
-
_context7.next = 5;
|
|
696
|
-
return this.pool.connect();
|
|
697
|
-
case 5:
|
|
698
|
-
client = _context7.sent;
|
|
699
|
-
_context7.prev = 6;
|
|
700
|
-
_context7.next = 9;
|
|
701
|
-
return client.query("\n SELECT id, title, resourceid, created_at AS createdAt, updated_at AS updatedAt, metadata\n FROM mastra_threads\n WHERE resourceid = $1\n ", [resourceid]);
|
|
702
|
-
case 9:
|
|
703
|
-
result = _context7.sent;
|
|
704
|
-
return _context7.abrupt("return", result.rows);
|
|
705
|
-
case 11:
|
|
706
|
-
_context7.prev = 11;
|
|
707
|
-
client.release();
|
|
708
|
-
return _context7.finish(11);
|
|
709
|
-
case 14:
|
|
710
|
-
case "end":
|
|
711
|
-
return _context7.stop();
|
|
712
|
-
}
|
|
713
|
-
}, _callee7, this, [[6,, 11, 14]]);
|
|
714
|
-
}));
|
|
715
|
-
function getThreadsByResourceId(_x7) {
|
|
716
|
-
return _getThreadsByResourceId.apply(this, arguments);
|
|
717
|
-
}
|
|
718
|
-
return getThreadsByResourceId;
|
|
719
|
-
}();
|
|
720
|
-
_proto.saveThread = /*#__PURE__*/function () {
|
|
721
|
-
var _saveThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref4) {
|
|
722
|
-
var thread, client, _result$rows2, id, title, createdAt, updatedAt, resourceid, metadata, result;
|
|
723
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
724
|
-
while (1) switch (_context8.prev = _context8.next) {
|
|
725
|
-
case 0:
|
|
726
|
-
thread = _ref4.thread;
|
|
727
|
-
_context8.next = 3;
|
|
728
|
-
return this.ensureTablesExist();
|
|
729
|
-
case 3:
|
|
730
|
-
_context8.next = 5;
|
|
731
|
-
return this.pool.connect();
|
|
732
|
-
case 5:
|
|
733
|
-
client = _context8.sent;
|
|
734
|
-
_context8.prev = 6;
|
|
735
|
-
id = thread.id, title = thread.title, createdAt = thread.createdAt, updatedAt = thread.updatedAt, resourceid = thread.resourceid, metadata = thread.metadata;
|
|
736
|
-
_context8.next = 10;
|
|
737
|
-
return client.query("\n INSERT INTO mastra_threads (id, title, created_at, updated_at, resourceid, metadata)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (id) DO UPDATE SET title = $2, updated_at = $4, resourceid = $5, metadata = $6\n RETURNING id, title, created_at AS createdAt, updated_at AS updatedAt, resourceid, metadata\n ", [id, title, createdAt, updatedAt, resourceid, JSON.stringify(metadata)]);
|
|
738
|
-
case 10:
|
|
739
|
-
result = _context8.sent;
|
|
740
|
-
return _context8.abrupt("return", result == null || (_result$rows2 = result.rows) == null ? void 0 : _result$rows2[0]);
|
|
741
|
-
case 12:
|
|
742
|
-
_context8.prev = 12;
|
|
743
|
-
client.release();
|
|
744
|
-
return _context8.finish(12);
|
|
745
|
-
case 15:
|
|
746
|
-
case "end":
|
|
747
|
-
return _context8.stop();
|
|
748
|
-
}
|
|
749
|
-
}, _callee8, this, [[6,, 12, 15]]);
|
|
750
|
-
}));
|
|
751
|
-
function saveThread(_x8) {
|
|
752
|
-
return _saveThread.apply(this, arguments);
|
|
753
|
-
}
|
|
754
|
-
return saveThread;
|
|
755
|
-
}();
|
|
756
|
-
_proto.checkIfValidArgExists = /*#__PURE__*/function () {
|
|
757
|
-
var _checkIfValidArgExists = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_ref5) {
|
|
758
|
-
var hashedToolCallArgs, client, toolArgsResult;
|
|
759
|
-
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
760
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
761
|
-
case 0:
|
|
762
|
-
hashedToolCallArgs = _ref5.hashedToolCallArgs;
|
|
763
|
-
_context9.next = 3;
|
|
579
|
+
hashedArgs = _ref4.hashedArgs;
|
|
580
|
+
_context6.next = 3;
|
|
764
581
|
return this.ensureTablesExist();
|
|
765
582
|
case 3:
|
|
766
|
-
|
|
583
|
+
_context6.next = 5;
|
|
767
584
|
return this.pool.connect();
|
|
768
585
|
case 5:
|
|
769
|
-
client =
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
return client.query(" SELECT tool_call_ids as toolCallIds, \n tool_call_args as toolCallArgs,\n created_at AS createdAt\n FROM mastra_messages\n WHERE tool_call_args::jsonb @> $1\n AND tool_call_args_expire_at > $2\n ORDER BY created_at ASC\n LIMIT 1", [JSON.stringify([
|
|
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()]);
|
|
773
590
|
case 9:
|
|
774
|
-
toolArgsResult =
|
|
775
|
-
return
|
|
591
|
+
toolArgsResult = _context6.sent;
|
|
592
|
+
return _context6.abrupt("return", toolArgsResult.rows.length > 0);
|
|
776
593
|
case 13:
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
console.log('error checking if valid arg exists====',
|
|
780
|
-
return
|
|
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);
|
|
781
598
|
case 17:
|
|
782
|
-
|
|
599
|
+
_context6.prev = 17;
|
|
783
600
|
client.release();
|
|
784
|
-
return
|
|
601
|
+
return _context6.finish(17);
|
|
785
602
|
case 20:
|
|
786
603
|
case "end":
|
|
787
|
-
return
|
|
604
|
+
return _context6.stop();
|
|
788
605
|
}
|
|
789
|
-
},
|
|
606
|
+
}, _callee6, this, [[6, 13, 17, 20]]);
|
|
790
607
|
}));
|
|
791
|
-
function
|
|
792
|
-
return
|
|
608
|
+
function validateToolCallArgs(_x8) {
|
|
609
|
+
return _validateToolCallArgs.apply(this, arguments);
|
|
793
610
|
}
|
|
794
|
-
return
|
|
611
|
+
return validateToolCallArgs;
|
|
795
612
|
}();
|
|
796
|
-
_proto.
|
|
797
|
-
var
|
|
613
|
+
_proto.getToolResult = /*#__PURE__*/function () {
|
|
614
|
+
var _getToolResult = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref5) {
|
|
798
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;
|
|
799
|
-
return _regeneratorRuntime().wrap(function
|
|
800
|
-
while (1) switch (
|
|
616
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
617
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
801
618
|
case 0:
|
|
802
|
-
threadId =
|
|
803
|
-
|
|
619
|
+
threadId = _ref5.threadId, toolArgs = _ref5.toolArgs, toolName = _ref5.toolName;
|
|
620
|
+
_context7.next = 3;
|
|
804
621
|
return this.ensureTablesExist();
|
|
805
622
|
case 3:
|
|
806
623
|
console.log('checking for cached tool result====', JSON.stringify(toolArgs, null, 2));
|
|
807
|
-
|
|
624
|
+
_context7.next = 6;
|
|
808
625
|
return this.pool.connect();
|
|
809
626
|
case 6:
|
|
810
|
-
client =
|
|
811
|
-
|
|
627
|
+
client = _context7.sent;
|
|
628
|
+
_context7.prev = 7;
|
|
812
629
|
hashedToolArgs = crypto.createHash('sha256').update(JSON.stringify({
|
|
813
630
|
args: toolArgs,
|
|
814
631
|
threadId: threadId,
|
|
815
632
|
toolName: toolName
|
|
816
633
|
})).digest('hex');
|
|
817
634
|
console.log('hashedToolArgs====', hashedToolArgs);
|
|
818
|
-
|
|
635
|
+
_context7.next = 12;
|
|
819
636
|
return client.query("SELECT tool_call_ids, \n tool_call_args,\n created_at\n FROM mastra_messages\n WHERE tool_call_args::jsonb @> $1\n AND tool_call_args_expire_at > $2\n ORDER BY created_at ASC\n LIMIT 1", [JSON.stringify([hashedToolArgs]), new Date().toISOString()]);
|
|
820
637
|
case 12:
|
|
821
|
-
toolArgsResult =
|
|
638
|
+
toolArgsResult = _context7.sent;
|
|
822
639
|
if (!(toolArgsResult.rows.length > 0)) {
|
|
823
|
-
|
|
640
|
+
_context7.next = 36;
|
|
824
641
|
break;
|
|
825
642
|
}
|
|
826
643
|
console.log('toolArgsResult====', JSON.stringify(toolArgsResult.rows[0], null, 2));
|
|
@@ -838,17 +655,17 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
838
655
|
correspondingToolCallId: correspondingToolCallId,
|
|
839
656
|
toolCallArgsIndex: toolCallArgsIndex
|
|
840
657
|
});
|
|
841
|
-
|
|
658
|
+
_context7.next = 26;
|
|
842
659
|
return client.query("SELECT content \n FROM mastra_messages \n WHERE thread_id = $1\n AND tool_call_ids ILIKE $2\n AND type = 'tool-result'\n AND created_at = $3\n LIMIT 1", [threadId, "%" + correspondingToolCallId + "%", new Date(createdAt).toISOString()]);
|
|
843
660
|
case 26:
|
|
844
|
-
toolResult =
|
|
661
|
+
toolResult = _context7.sent;
|
|
845
662
|
console.log('called toolResult');
|
|
846
663
|
if (!(toolResult.rows.length === 0)) {
|
|
847
|
-
|
|
664
|
+
_context7.next = 31;
|
|
848
665
|
break;
|
|
849
666
|
}
|
|
850
667
|
console.log('no tool result found');
|
|
851
|
-
return
|
|
668
|
+
return _context7.abrupt("return", null);
|
|
852
669
|
case 31:
|
|
853
670
|
toolResultContent = JSON.parse((_toolResult$rows$ = toolResult.rows[0]) == null ? void 0 : _toolResult$rows$.content);
|
|
854
671
|
requiredToolResult = toolResultContent.find(function (part) {
|
|
@@ -856,110 +673,155 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
856
673
|
});
|
|
857
674
|
console.log('requiredToolResult====', JSON.stringify(requiredToolResult, null, 2));
|
|
858
675
|
if (!requiredToolResult) {
|
|
859
|
-
|
|
676
|
+
_context7.next = 36;
|
|
860
677
|
break;
|
|
861
678
|
}
|
|
862
|
-
return
|
|
679
|
+
return _context7.abrupt("return", requiredToolResult.result);
|
|
863
680
|
case 36:
|
|
864
|
-
return
|
|
681
|
+
return _context7.abrupt("return", null);
|
|
865
682
|
case 39:
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
console.log('error getting cached tool result====',
|
|
869
|
-
return
|
|
683
|
+
_context7.prev = 39;
|
|
684
|
+
_context7.t0 = _context7["catch"](7);
|
|
685
|
+
console.log('error getting cached tool result====', _context7.t0);
|
|
686
|
+
return _context7.abrupt("return", null);
|
|
870
687
|
case 43:
|
|
871
|
-
|
|
688
|
+
_context7.prev = 43;
|
|
872
689
|
client.release();
|
|
873
|
-
return
|
|
690
|
+
return _context7.finish(43);
|
|
874
691
|
case 46:
|
|
875
692
|
case "end":
|
|
876
|
-
return
|
|
693
|
+
return _context7.stop();
|
|
877
694
|
}
|
|
878
|
-
},
|
|
695
|
+
}, _callee7, this, [[7, 39, 43, 46]]);
|
|
879
696
|
}));
|
|
880
|
-
function
|
|
881
|
-
return
|
|
697
|
+
function getToolResult(_x9) {
|
|
698
|
+
return _getToolResult.apply(this, arguments);
|
|
882
699
|
}
|
|
883
|
-
return
|
|
700
|
+
return getToolResult;
|
|
884
701
|
}();
|
|
885
702
|
_proto.getContextWindow = /*#__PURE__*/function () {
|
|
886
|
-
var _getContextWindow = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function
|
|
887
|
-
var threadId, startDate, endDate, client, _result, result;
|
|
888
|
-
return _regeneratorRuntime().wrap(function
|
|
889
|
-
while (1) switch (
|
|
703
|
+
var _getContextWindow = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref6) {
|
|
704
|
+
var threadId, startDate, endDate, _ref6$format, format, client, _result, result;
|
|
705
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
706
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
890
707
|
case 0:
|
|
891
|
-
threadId =
|
|
892
|
-
|
|
708
|
+
threadId = _ref6.threadId, startDate = _ref6.startDate, endDate = _ref6.endDate, _ref6$format = _ref6.format, format = _ref6$format === void 0 ? 'raw' : _ref6$format;
|
|
709
|
+
_context8.next = 3;
|
|
893
710
|
return this.ensureTablesExist();
|
|
894
711
|
case 3:
|
|
895
|
-
|
|
896
|
-
_context11.next = 6;
|
|
712
|
+
_context8.next = 5;
|
|
897
713
|
return this.pool.connect();
|
|
898
|
-
case
|
|
899
|
-
client =
|
|
900
|
-
|
|
714
|
+
case 5:
|
|
715
|
+
client = _context8.sent;
|
|
716
|
+
_context8.prev = 6;
|
|
901
717
|
if (!this.MAX_CONTEXT_TOKENS) {
|
|
902
|
-
|
|
718
|
+
_context8.next = 13;
|
|
903
719
|
break;
|
|
904
720
|
}
|
|
905
|
-
|
|
721
|
+
_context8.next = 10;
|
|
906
722
|
return client.query("WITH RankedMessages AS (\n SELECT *,\n SUM(tokens) OVER (ORDER BY created_at DESC) as running_total\n FROM mastra_messages\n WHERE thread_id = $1\n AND type = 'text'\n " + (startDate ? "AND created_at >= '" + startDate.toISOString() + "'" : '') + "\n " + (endDate ? "AND created_at <= '" + endDate.toISOString() + "'" : '') + "\n ORDER BY created_at DESC\n )\n SELECT id, \n content, \n role, \n type,\n created_at AS createdAt, \n thread_id AS threadId\n FROM RankedMessages\n WHERE running_total <= $2\n ORDER BY created_at ASC", [threadId, this.MAX_CONTEXT_TOKENS]);
|
|
907
|
-
case
|
|
908
|
-
_result =
|
|
909
|
-
console.log('
|
|
910
|
-
return
|
|
911
|
-
case
|
|
912
|
-
|
|
723
|
+
case 10:
|
|
724
|
+
_result = _context8.sent;
|
|
725
|
+
console.log('Format', format);
|
|
726
|
+
return _context8.abrupt("return", this.parseMessages(_result.rows));
|
|
727
|
+
case 13:
|
|
728
|
+
_context8.next = 15;
|
|
913
729
|
return client.query("SELECT id, \n content, \n role, \n type,\n created_at AS createdAt, \n thread_id AS threadId\n FROM mastra_messages\n WHERE thread_id = $1\n AND type = 'text'\n " + (startDate ? "AND created_at >= '" + startDate.toISOString() + "'" : '') + "\n " + (endDate ? "AND created_at <= '" + endDate.toISOString() + "'" : '') + "\n ORDER BY created_at ASC", [threadId]);
|
|
914
|
-
case
|
|
915
|
-
result =
|
|
916
|
-
console.log('
|
|
917
|
-
return
|
|
918
|
-
case
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
console.log('error getting context window====',
|
|
922
|
-
return
|
|
923
|
-
case
|
|
924
|
-
|
|
730
|
+
case 15:
|
|
731
|
+
result = _context8.sent;
|
|
732
|
+
console.log('Format', format);
|
|
733
|
+
return _context8.abrupt("return", this.parseMessages(result.rows));
|
|
734
|
+
case 20:
|
|
735
|
+
_context8.prev = 20;
|
|
736
|
+
_context8.t0 = _context8["catch"](6);
|
|
737
|
+
console.log('error getting context window====', _context8.t0);
|
|
738
|
+
return _context8.abrupt("return", []);
|
|
739
|
+
case 24:
|
|
740
|
+
_context8.prev = 24;
|
|
925
741
|
client.release();
|
|
926
|
-
return
|
|
927
|
-
case
|
|
742
|
+
return _context8.finish(24);
|
|
743
|
+
case 27:
|
|
928
744
|
case "end":
|
|
929
|
-
return
|
|
745
|
+
return _context8.stop();
|
|
930
746
|
}
|
|
931
|
-
},
|
|
747
|
+
}, _callee8, this, [[6, 20, 24, 27]]);
|
|
932
748
|
}));
|
|
933
|
-
function getContextWindow(
|
|
749
|
+
function getContextWindow(_x10) {
|
|
934
750
|
return _getContextWindow.apply(this, arguments);
|
|
935
751
|
}
|
|
936
752
|
return getContextWindow;
|
|
937
|
-
}()
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
753
|
+
}()
|
|
754
|
+
/**
|
|
755
|
+
* Messages
|
|
756
|
+
*/
|
|
757
|
+
;
|
|
758
|
+
_proto.getMessages =
|
|
759
|
+
/*#__PURE__*/
|
|
760
|
+
function () {
|
|
761
|
+
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_ref7) {
|
|
762
|
+
var threadId, client, result, messages, uiMessages;
|
|
763
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
764
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
944
765
|
case 0:
|
|
945
|
-
|
|
946
|
-
|
|
766
|
+
threadId = _ref7.threadId;
|
|
767
|
+
_context9.next = 3;
|
|
947
768
|
return this.ensureTablesExist();
|
|
948
769
|
case 3:
|
|
949
|
-
|
|
770
|
+
_context9.next = 5;
|
|
950
771
|
return this.pool.connect();
|
|
951
772
|
case 5:
|
|
952
|
-
client =
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
return client.query(
|
|
956
|
-
case 9:
|
|
957
|
-
|
|
773
|
+
client = _context9.sent;
|
|
774
|
+
_context9.prev = 6;
|
|
775
|
+
_context9.next = 9;
|
|
776
|
+
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]);
|
|
777
|
+
case 9:
|
|
778
|
+
result = _context9.sent;
|
|
779
|
+
messages = this.parseMessages(result.rows);
|
|
780
|
+
uiMessages = this.convertToUIMessages(messages);
|
|
781
|
+
return _context9.abrupt("return", {
|
|
782
|
+
messages: messages,
|
|
783
|
+
uiMessages: uiMessages
|
|
784
|
+
});
|
|
785
|
+
case 13:
|
|
786
|
+
_context9.prev = 13;
|
|
787
|
+
client.release();
|
|
788
|
+
return _context9.finish(13);
|
|
789
|
+
case 16:
|
|
790
|
+
case "end":
|
|
791
|
+
return _context9.stop();
|
|
792
|
+
}
|
|
793
|
+
}, _callee9, this, [[6,, 13, 16]]);
|
|
794
|
+
}));
|
|
795
|
+
function getMessages(_x11) {
|
|
796
|
+
return _getMessages.apply(this, arguments);
|
|
797
|
+
}
|
|
798
|
+
return getMessages;
|
|
799
|
+
}();
|
|
800
|
+
_proto.saveMessages = /*#__PURE__*/function () {
|
|
801
|
+
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(_ref8) {
|
|
802
|
+
var _this2 = this;
|
|
803
|
+
var messages, client, _loop, _iterator, _step;
|
|
804
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context11) {
|
|
805
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
806
|
+
case 0:
|
|
807
|
+
messages = _ref8.messages;
|
|
808
|
+
_context11.next = 3;
|
|
809
|
+
return this.ensureTablesExist();
|
|
810
|
+
case 3:
|
|
811
|
+
_context11.next = 5;
|
|
812
|
+
return this.pool.connect();
|
|
813
|
+
case 5:
|
|
814
|
+
client = _context11.sent;
|
|
815
|
+
_context11.prev = 6;
|
|
816
|
+
_context11.next = 9;
|
|
817
|
+
return client.query('BEGIN');
|
|
818
|
+
case 9:
|
|
819
|
+
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
958
820
|
var message, id, content, role, createdAt, threadId, toolCallIds, toolCallArgs, type, toolNames, tokens, _content$, contentMssg, hashedToolCallArgs, validArgExists, i, isValid, toolCallArgsExpireAt;
|
|
959
|
-
return _regeneratorRuntime().wrap(function
|
|
960
|
-
while (1) switch (
|
|
821
|
+
return _regeneratorRuntime().wrap(function _loop$(_context10) {
|
|
822
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
961
823
|
case 0:
|
|
962
|
-
message =
|
|
824
|
+
message = _step.value;
|
|
963
825
|
console.log('saving message====', JSON.stringify(message, null, 2));
|
|
964
826
|
id = message.id, content = message.content, role = message.role, createdAt = message.createdAt, threadId = message.threadId, toolCallIds = message.toolCallIds, toolCallArgs = message.toolCallArgs, type = message.type, toolNames = message.toolNames;
|
|
965
827
|
tokens = null;
|
|
@@ -977,7 +839,7 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
977
839
|
}) : null;
|
|
978
840
|
validArgExists = false;
|
|
979
841
|
if (!(hashedToolCallArgs != null && hashedToolCallArgs.length)) {
|
|
980
|
-
|
|
842
|
+
_context10.next = 20;
|
|
981
843
|
break;
|
|
982
844
|
}
|
|
983
845
|
// Check all args sequentially
|
|
@@ -985,116 +847,742 @@ var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
|
985
847
|
i = 0;
|
|
986
848
|
case 10:
|
|
987
849
|
if (!(i < hashedToolCallArgs.length)) {
|
|
988
|
-
|
|
850
|
+
_context10.next = 20;
|
|
989
851
|
break;
|
|
990
852
|
}
|
|
991
|
-
|
|
992
|
-
return _this2.
|
|
993
|
-
|
|
853
|
+
_context10.next = 13;
|
|
854
|
+
return _this2.validateToolCallArgs({
|
|
855
|
+
hashedArgs: hashedToolCallArgs[i]
|
|
994
856
|
});
|
|
995
857
|
case 13:
|
|
996
|
-
isValid =
|
|
858
|
+
isValid = _context10.sent;
|
|
997
859
|
if (isValid) {
|
|
998
|
-
|
|
860
|
+
_context10.next = 17;
|
|
999
861
|
break;
|
|
1000
862
|
}
|
|
1001
863
|
validArgExists = false;
|
|
1002
|
-
return
|
|
864
|
+
return _context10.abrupt("break", 20);
|
|
1003
865
|
case 17:
|
|
1004
866
|
i++;
|
|
1005
|
-
|
|
867
|
+
_context10.next = 10;
|
|
1006
868
|
break;
|
|
1007
869
|
case 20:
|
|
1008
870
|
toolCallArgsExpireAt = !toolCallArgs ? null : validArgExists ? createdAt : new Date(createdAt.getTime() + 5 * 60 * 1000); // 5 minutes
|
|
1009
871
|
console.log('just before query');
|
|
1010
|
-
|
|
872
|
+
_context10.next = 24;
|
|
1011
873
|
return client.query("\n INSERT INTO mastra_messages (id, content, role, created_at, thread_id, tool_call_ids, tool_call_args, type, tokens, tool_call_args_expire_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ", [id, JSON.stringify(content), role, createdAt.toISOString(), threadId, JSON.stringify(toolCallIds), JSON.stringify(hashedToolCallArgs), type, tokens, toolCallArgsExpireAt == null ? void 0 : toolCallArgsExpireAt.toISOString()]);
|
|
1012
874
|
case 24:
|
|
1013
875
|
case "end":
|
|
1014
|
-
return
|
|
876
|
+
return _context10.stop();
|
|
1015
877
|
}
|
|
1016
|
-
},
|
|
878
|
+
}, _loop);
|
|
1017
879
|
});
|
|
1018
|
-
|
|
880
|
+
_iterator = _createForOfIteratorHelperLoose(messages);
|
|
1019
881
|
case 11:
|
|
1020
|
-
if ((
|
|
1021
|
-
|
|
882
|
+
if ((_step = _iterator()).done) {
|
|
883
|
+
_context11.next = 15;
|
|
1022
884
|
break;
|
|
1023
885
|
}
|
|
1024
|
-
return
|
|
886
|
+
return _context11.delegateYield(_loop(), "t0", 13);
|
|
1025
887
|
case 13:
|
|
1026
|
-
|
|
888
|
+
_context11.next = 11;
|
|
1027
889
|
break;
|
|
1028
890
|
case 15:
|
|
1029
891
|
console.log('just after query');
|
|
1030
|
-
|
|
892
|
+
_context11.next = 18;
|
|
1031
893
|
return client.query('COMMIT');
|
|
1032
894
|
case 18:
|
|
1033
|
-
return
|
|
895
|
+
return _context11.abrupt("return", messages);
|
|
1034
896
|
case 21:
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
897
|
+
_context11.prev = 21;
|
|
898
|
+
_context11.t1 = _context11["catch"](6);
|
|
899
|
+
_context11.next = 25;
|
|
1038
900
|
return client.query('ROLLBACK');
|
|
1039
901
|
case 25:
|
|
1040
|
-
throw
|
|
902
|
+
throw _context11.t1;
|
|
1041
903
|
case 26:
|
|
1042
|
-
|
|
904
|
+
_context11.prev = 26;
|
|
1043
905
|
client.release();
|
|
1044
|
-
return
|
|
906
|
+
return _context11.finish(26);
|
|
1045
907
|
case 29:
|
|
1046
908
|
case "end":
|
|
1047
|
-
return
|
|
909
|
+
return _context11.stop();
|
|
1048
910
|
}
|
|
1049
|
-
},
|
|
911
|
+
}, _callee10, this, [[6, 21, 26, 29]]);
|
|
1050
912
|
}));
|
|
1051
913
|
function saveMessages(_x12) {
|
|
1052
914
|
return _saveMessages.apply(this, arguments);
|
|
1053
915
|
}
|
|
1054
916
|
return saveMessages;
|
|
1055
917
|
}();
|
|
1056
|
-
_proto.
|
|
1057
|
-
var
|
|
1058
|
-
var
|
|
918
|
+
_proto.deleteMessage = /*#__PURE__*/function () {
|
|
919
|
+
var _deleteMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(id) {
|
|
920
|
+
var client;
|
|
921
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context12) {
|
|
922
|
+
while (1) switch (_context12.prev = _context12.next) {
|
|
923
|
+
case 0:
|
|
924
|
+
_context12.next = 2;
|
|
925
|
+
return this.pool.connect();
|
|
926
|
+
case 2:
|
|
927
|
+
client = _context12.sent;
|
|
928
|
+
_context12.prev = 3;
|
|
929
|
+
_context12.next = 6;
|
|
930
|
+
return client.query("\n DELETE FROM mastra_messages\n WHERE id = $1\n ", [id]);
|
|
931
|
+
case 6:
|
|
932
|
+
_context12.prev = 6;
|
|
933
|
+
client.release();
|
|
934
|
+
return _context12.finish(6);
|
|
935
|
+
case 9:
|
|
936
|
+
case "end":
|
|
937
|
+
return _context12.stop();
|
|
938
|
+
}
|
|
939
|
+
}, _callee11, this, [[3,, 6, 9]]);
|
|
940
|
+
}));
|
|
941
|
+
function deleteMessage(_x13) {
|
|
942
|
+
return _deleteMessage.apply(this, arguments);
|
|
943
|
+
}
|
|
944
|
+
return deleteMessage;
|
|
945
|
+
}()
|
|
946
|
+
/**
|
|
947
|
+
* Table Management
|
|
948
|
+
*/
|
|
949
|
+
;
|
|
950
|
+
_proto.drop =
|
|
951
|
+
/*#__PURE__*/
|
|
952
|
+
function () {
|
|
953
|
+
var _drop = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
|
|
954
|
+
var client;
|
|
955
|
+
return _regeneratorRuntime().wrap(function _callee12$(_context13) {
|
|
956
|
+
while (1) switch (_context13.prev = _context13.next) {
|
|
957
|
+
case 0:
|
|
958
|
+
_context13.next = 2;
|
|
959
|
+
return this.pool.connect();
|
|
960
|
+
case 2:
|
|
961
|
+
client = _context13.sent;
|
|
962
|
+
_context13.next = 5;
|
|
963
|
+
return client.query('DELETE FROM mastra_messages');
|
|
964
|
+
case 5:
|
|
965
|
+
_context13.next = 7;
|
|
966
|
+
return client.query('DELETE FROM mastra_threads');
|
|
967
|
+
case 7:
|
|
968
|
+
client.release();
|
|
969
|
+
_context13.next = 10;
|
|
970
|
+
return this.pool.end();
|
|
971
|
+
case 10:
|
|
972
|
+
case "end":
|
|
973
|
+
return _context13.stop();
|
|
974
|
+
}
|
|
975
|
+
}, _callee12, this);
|
|
976
|
+
}));
|
|
977
|
+
function drop() {
|
|
978
|
+
return _drop.apply(this, arguments);
|
|
979
|
+
}
|
|
980
|
+
return drop;
|
|
981
|
+
}();
|
|
982
|
+
_proto.ensureTablesExist = /*#__PURE__*/function () {
|
|
983
|
+
var _ensureTablesExist = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
|
|
984
|
+
var client, _threadsResult$rows, _messagesResult$rows, threadsResult, messagesResult;
|
|
1059
985
|
return _regeneratorRuntime().wrap(function _callee13$(_context14) {
|
|
1060
986
|
while (1) switch (_context14.prev = _context14.next) {
|
|
1061
987
|
case 0:
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
_context14.
|
|
988
|
+
if (!this.hasTables) {
|
|
989
|
+
_context14.next = 2;
|
|
990
|
+
break;
|
|
991
|
+
}
|
|
992
|
+
return _context14.abrupt("return");
|
|
993
|
+
case 2:
|
|
994
|
+
_context14.next = 4;
|
|
1067
995
|
return this.pool.connect();
|
|
1068
|
-
case
|
|
996
|
+
case 4:
|
|
1069
997
|
client = _context14.sent;
|
|
1070
|
-
_context14.prev =
|
|
1071
|
-
_context14.next =
|
|
1072
|
-
return client.query("\n SELECT
|
|
998
|
+
_context14.prev = 5;
|
|
999
|
+
_context14.next = 8;
|
|
1000
|
+
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_threads'\n );\n ");
|
|
1001
|
+
case 8:
|
|
1002
|
+
threadsResult = _context14.sent;
|
|
1003
|
+
if (threadsResult != null && (_threadsResult$rows = threadsResult.rows) != null && (_threadsResult$rows = _threadsResult$rows[0]) != null && _threadsResult$rows.exists) {
|
|
1004
|
+
_context14.next = 12;
|
|
1005
|
+
break;
|
|
1006
|
+
}
|
|
1007
|
+
_context14.next = 12;
|
|
1008
|
+
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 ");
|
|
1009
|
+
case 12:
|
|
1010
|
+
_context14.next = 14;
|
|
1011
|
+
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_messages'\n );\n ");
|
|
1012
|
+
case 14:
|
|
1013
|
+
messagesResult = _context14.sent;
|
|
1014
|
+
if (messagesResult != null && (_messagesResult$rows = messagesResult.rows) != null && (_messagesResult$rows = _messagesResult$rows[0]) != null && _messagesResult$rows.exists) {
|
|
1015
|
+
_context14.next = 18;
|
|
1016
|
+
break;
|
|
1017
|
+
}
|
|
1018
|
+
_context14.next = 18;
|
|
1019
|
+
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 ");
|
|
1020
|
+
case 18:
|
|
1021
|
+
_context14.prev = 18;
|
|
1022
|
+
client.release();
|
|
1023
|
+
this.hasTables = true;
|
|
1024
|
+
return _context14.finish(18);
|
|
1025
|
+
case 22:
|
|
1026
|
+
case "end":
|
|
1027
|
+
return _context14.stop();
|
|
1028
|
+
}
|
|
1029
|
+
}, _callee13, this, [[5,, 18, 22]]);
|
|
1030
|
+
}));
|
|
1031
|
+
function ensureTablesExist() {
|
|
1032
|
+
return _ensureTablesExist.apply(this, arguments);
|
|
1033
|
+
}
|
|
1034
|
+
return ensureTablesExist;
|
|
1035
|
+
}();
|
|
1036
|
+
return PgMemory;
|
|
1037
|
+
}(MastraMemory);
|
|
1038
|
+
|
|
1039
|
+
var UpstashKVMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
1040
|
+
function UpstashKVMemory(config) {
|
|
1041
|
+
var _this;
|
|
1042
|
+
_this = _MastraMemory.call(this) || this;
|
|
1043
|
+
_this.prefix = void 0;
|
|
1044
|
+
_this.kv = void 0;
|
|
1045
|
+
_this.prefix = config.prefix || 'mastra';
|
|
1046
|
+
_this.MAX_CONTEXT_TOKENS = config.maxTokens;
|
|
1047
|
+
_this.kv = new Redis({
|
|
1048
|
+
url: config.url,
|
|
1049
|
+
token: config.token
|
|
1050
|
+
});
|
|
1051
|
+
return _this;
|
|
1052
|
+
}
|
|
1053
|
+
_inheritsLoose(UpstashKVMemory, _MastraMemory);
|
|
1054
|
+
var _proto = UpstashKVMemory.prototype;
|
|
1055
|
+
_proto.getThreadKey = function getThreadKey(threadId) {
|
|
1056
|
+
return this.prefix + ":thread:" + threadId;
|
|
1057
|
+
};
|
|
1058
|
+
_proto.getMessagesKey = function getMessagesKey(threadId) {
|
|
1059
|
+
return this.prefix + ":messages:" + threadId;
|
|
1060
|
+
};
|
|
1061
|
+
_proto.getToolCacheKey = function getToolCacheKey(hashedArgs) {
|
|
1062
|
+
return this.prefix + ":tool:" + hashedArgs;
|
|
1063
|
+
};
|
|
1064
|
+
_proto.getThreadById = /*#__PURE__*/function () {
|
|
1065
|
+
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
1066
|
+
var threadId, thread;
|
|
1067
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1068
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1069
|
+
case 0:
|
|
1070
|
+
threadId = _ref.threadId;
|
|
1071
|
+
_context.next = 3;
|
|
1072
|
+
return this.kv.get(this.getThreadKey(threadId));
|
|
1073
|
+
case 3:
|
|
1074
|
+
thread = _context.sent;
|
|
1075
|
+
return _context.abrupt("return", thread ? this.parseThread(thread) : null);
|
|
1076
|
+
case 5:
|
|
1077
|
+
case "end":
|
|
1078
|
+
return _context.stop();
|
|
1079
|
+
}
|
|
1080
|
+
}, _callee, this);
|
|
1081
|
+
}));
|
|
1082
|
+
function getThreadById(_x) {
|
|
1083
|
+
return _getThreadById.apply(this, arguments);
|
|
1084
|
+
}
|
|
1085
|
+
return getThreadById;
|
|
1086
|
+
}();
|
|
1087
|
+
_proto.getThreadsByResourceId = /*#__PURE__*/function () {
|
|
1088
|
+
var _getThreadsByResourceId = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref2) {
|
|
1089
|
+
var _this2 = this;
|
|
1090
|
+
var resourceid, pattern, keys, threads;
|
|
1091
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
1092
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
1093
|
+
case 0:
|
|
1094
|
+
resourceid = _ref2.resourceid;
|
|
1095
|
+
pattern = this.prefix + ":thread:*";
|
|
1096
|
+
_context2.next = 4;
|
|
1097
|
+
return this.kv.keys(pattern);
|
|
1098
|
+
case 4:
|
|
1099
|
+
keys = _context2.sent;
|
|
1100
|
+
_context2.next = 7;
|
|
1101
|
+
return Promise.all(keys.map(function (key) {
|
|
1102
|
+
return _this2.kv.get(key);
|
|
1103
|
+
}));
|
|
1104
|
+
case 7:
|
|
1105
|
+
threads = _context2.sent;
|
|
1106
|
+
return _context2.abrupt("return", threads.filter(function (thread) {
|
|
1107
|
+
return (thread == null ? void 0 : thread.resourceid) === resourceid;
|
|
1108
|
+
}).map(function (thread) {
|
|
1109
|
+
return _this2.parseThread(thread);
|
|
1110
|
+
}));
|
|
1073
1111
|
case 9:
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1112
|
+
case "end":
|
|
1113
|
+
return _context2.stop();
|
|
1114
|
+
}
|
|
1115
|
+
}, _callee2, this);
|
|
1116
|
+
}));
|
|
1117
|
+
function getThreadsByResourceId(_x2) {
|
|
1118
|
+
return _getThreadsByResourceId.apply(this, arguments);
|
|
1119
|
+
}
|
|
1120
|
+
return getThreadsByResourceId;
|
|
1121
|
+
}();
|
|
1122
|
+
_proto.saveThread = /*#__PURE__*/function () {
|
|
1123
|
+
var _saveThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref3) {
|
|
1124
|
+
var thread, key, serializedThread;
|
|
1125
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
1126
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
1127
|
+
case 0:
|
|
1128
|
+
thread = _ref3.thread;
|
|
1129
|
+
key = this.getThreadKey(thread.id);
|
|
1130
|
+
serializedThread = _extends({}, thread, {
|
|
1131
|
+
createdAt: thread.createdAt.toISOString(),
|
|
1132
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
1133
|
+
});
|
|
1134
|
+
_context3.next = 5;
|
|
1135
|
+
return this.kv.set(key, serializedThread);
|
|
1136
|
+
case 5:
|
|
1137
|
+
return _context3.abrupt("return", thread);
|
|
1138
|
+
case 6:
|
|
1139
|
+
case "end":
|
|
1140
|
+
return _context3.stop();
|
|
1141
|
+
}
|
|
1142
|
+
}, _callee3, this);
|
|
1143
|
+
}));
|
|
1144
|
+
function saveThread(_x3) {
|
|
1145
|
+
return _saveThread.apply(this, arguments);
|
|
1146
|
+
}
|
|
1147
|
+
return saveThread;
|
|
1148
|
+
}();
|
|
1149
|
+
_proto.updateThread = /*#__PURE__*/function () {
|
|
1150
|
+
var _updateThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(id, title, metadata) {
|
|
1151
|
+
var key, thread, updatedThread;
|
|
1152
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
1153
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
1154
|
+
case 0:
|
|
1155
|
+
key = this.getThreadKey(id);
|
|
1156
|
+
_context4.next = 3;
|
|
1157
|
+
return this.kv.get(key);
|
|
1158
|
+
case 3:
|
|
1159
|
+
thread = _context4.sent;
|
|
1160
|
+
if (thread) {
|
|
1161
|
+
_context4.next = 6;
|
|
1162
|
+
break;
|
|
1163
|
+
}
|
|
1164
|
+
throw new Error("Thread " + id + " not found");
|
|
1165
|
+
case 6:
|
|
1166
|
+
updatedThread = _extends({}, thread, {
|
|
1167
|
+
title: title,
|
|
1168
|
+
metadata: metadata,
|
|
1169
|
+
updatedAt: new Date().toISOString()
|
|
1170
|
+
});
|
|
1171
|
+
_context4.next = 9;
|
|
1172
|
+
return this.kv.set(key, updatedThread);
|
|
1173
|
+
case 9:
|
|
1174
|
+
return _context4.abrupt("return", this.parseThread(updatedThread));
|
|
1175
|
+
case 10:
|
|
1176
|
+
case "end":
|
|
1177
|
+
return _context4.stop();
|
|
1178
|
+
}
|
|
1179
|
+
}, _callee4, this);
|
|
1180
|
+
}));
|
|
1181
|
+
function updateThread(_x4, _x5, _x6) {
|
|
1182
|
+
return _updateThread.apply(this, arguments);
|
|
1183
|
+
}
|
|
1184
|
+
return updateThread;
|
|
1185
|
+
}();
|
|
1186
|
+
_proto.deleteThread = /*#__PURE__*/function () {
|
|
1187
|
+
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(id) {
|
|
1188
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
1189
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
1190
|
+
case 0:
|
|
1191
|
+
_context5.next = 2;
|
|
1192
|
+
return this.kv.del(this.getThreadKey(id));
|
|
1193
|
+
case 2:
|
|
1194
|
+
_context5.next = 4;
|
|
1195
|
+
return this.kv.del(this.getMessagesKey(id));
|
|
1196
|
+
case 4:
|
|
1197
|
+
case "end":
|
|
1198
|
+
return _context5.stop();
|
|
1199
|
+
}
|
|
1200
|
+
}, _callee5, this);
|
|
1201
|
+
}));
|
|
1202
|
+
function deleteThread(_x7) {
|
|
1203
|
+
return _deleteThread.apply(this, arguments);
|
|
1204
|
+
}
|
|
1205
|
+
return deleteThread;
|
|
1206
|
+
}()
|
|
1207
|
+
/**
|
|
1208
|
+
* Tool Cache
|
|
1209
|
+
*/
|
|
1210
|
+
;
|
|
1211
|
+
_proto.validateToolCallArgs =
|
|
1212
|
+
/*#__PURE__*/
|
|
1213
|
+
function () {
|
|
1214
|
+
var _validateToolCallArgs = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref4) {
|
|
1215
|
+
var hashedArgs, cacheKey, cached;
|
|
1216
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
1217
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
1218
|
+
case 0:
|
|
1219
|
+
hashedArgs = _ref4.hashedArgs;
|
|
1220
|
+
cacheKey = this.getToolCacheKey(hashedArgs);
|
|
1221
|
+
_context6.next = 4;
|
|
1222
|
+
return this.kv.get(cacheKey);
|
|
1223
|
+
case 4:
|
|
1224
|
+
cached = _context6.sent;
|
|
1225
|
+
return _context6.abrupt("return", !!cached && new Date(cached.expireAt) > new Date());
|
|
1226
|
+
case 6:
|
|
1227
|
+
case "end":
|
|
1228
|
+
return _context6.stop();
|
|
1229
|
+
}
|
|
1230
|
+
}, _callee6, this);
|
|
1231
|
+
}));
|
|
1232
|
+
function validateToolCallArgs(_x8) {
|
|
1233
|
+
return _validateToolCallArgs.apply(this, arguments);
|
|
1234
|
+
}
|
|
1235
|
+
return validateToolCallArgs;
|
|
1236
|
+
}();
|
|
1237
|
+
_proto.getToolResult = /*#__PURE__*/function () {
|
|
1238
|
+
var _getToolResult = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref5) {
|
|
1239
|
+
var threadId, toolArgs, toolName, hashedToolArgs, cacheKey, cached;
|
|
1240
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
1241
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
1242
|
+
case 0:
|
|
1243
|
+
threadId = _ref5.threadId, toolArgs = _ref5.toolArgs, toolName = _ref5.toolName;
|
|
1244
|
+
hashedToolArgs = crypto.createHash('sha256').update(JSON.stringify({
|
|
1245
|
+
args: toolArgs,
|
|
1246
|
+
threadId: threadId,
|
|
1247
|
+
toolName: toolName
|
|
1248
|
+
})).digest('hex');
|
|
1249
|
+
cacheKey = this.getToolCacheKey(hashedToolArgs);
|
|
1250
|
+
_context7.next = 5;
|
|
1251
|
+
return this.kv.get(cacheKey);
|
|
1252
|
+
case 5:
|
|
1253
|
+
cached = _context7.sent;
|
|
1254
|
+
if (!(cached && new Date(cached.expireAt) > new Date())) {
|
|
1255
|
+
_context7.next = 8;
|
|
1256
|
+
break;
|
|
1257
|
+
}
|
|
1258
|
+
return _context7.abrupt("return", cached.result || null);
|
|
1259
|
+
case 8:
|
|
1260
|
+
return _context7.abrupt("return", null);
|
|
1261
|
+
case 9:
|
|
1262
|
+
case "end":
|
|
1263
|
+
return _context7.stop();
|
|
1264
|
+
}
|
|
1265
|
+
}, _callee7, this);
|
|
1266
|
+
}));
|
|
1267
|
+
function getToolResult(_x9) {
|
|
1268
|
+
return _getToolResult.apply(this, arguments);
|
|
1269
|
+
}
|
|
1270
|
+
return getToolResult;
|
|
1271
|
+
}();
|
|
1272
|
+
_proto.getContextWindow = /*#__PURE__*/function () {
|
|
1273
|
+
var _getContextWindow = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref6) {
|
|
1274
|
+
var threadId, startDate, endDate, _ref6$format, format, messagesKey, messages, filteredMessages, totalTokens, messagesWithinTokenLimit, _iterator, _step, _message$content$, message, content, tokens;
|
|
1275
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
1276
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
1277
|
+
case 0:
|
|
1278
|
+
threadId = _ref6.threadId, startDate = _ref6.startDate, endDate = _ref6.endDate, _ref6$format = _ref6.format, format = _ref6$format === void 0 ? 'raw' : _ref6$format;
|
|
1279
|
+
messagesKey = this.getMessagesKey(threadId);
|
|
1280
|
+
_context8.next = 4;
|
|
1281
|
+
return this.kv.lrange(messagesKey, 0, -1);
|
|
1282
|
+
case 4:
|
|
1283
|
+
messages = _context8.sent;
|
|
1284
|
+
filteredMessages = messages.filter(function (msg) {
|
|
1285
|
+
return msg.type === 'text';
|
|
1286
|
+
});
|
|
1287
|
+
if (startDate) {
|
|
1288
|
+
filteredMessages = filteredMessages.filter(function (msg) {
|
|
1289
|
+
return new Date(msg.createdAt) >= startDate;
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
if (endDate) {
|
|
1293
|
+
filteredMessages = filteredMessages.filter(function (msg) {
|
|
1294
|
+
return new Date(msg.createdAt) <= endDate;
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
if (!this.MAX_CONTEXT_TOKENS) {
|
|
1298
|
+
_context8.next = 24;
|
|
1299
|
+
break;
|
|
1300
|
+
}
|
|
1301
|
+
totalTokens = 0;
|
|
1302
|
+
messagesWithinTokenLimit = []; // Process messages from newest to oldest
|
|
1303
|
+
_iterator = _createForOfIteratorHelperLoose(filteredMessages.reverse());
|
|
1304
|
+
case 12:
|
|
1305
|
+
if ((_step = _iterator()).done) {
|
|
1306
|
+
_context8.next = 22;
|
|
1307
|
+
break;
|
|
1308
|
+
}
|
|
1309
|
+
message = _step.value;
|
|
1310
|
+
content = message.role === 'assistant' ? ((_message$content$ = message.content[0]) == null ? void 0 : _message$content$.text) || '' : message.content; // Use a more aggressive token estimation
|
|
1311
|
+
// Roughly estimate 1 token per 4 characters
|
|
1312
|
+
tokens = Math.ceil(content.length / 4); // Check if adding this message would exceed the token limit
|
|
1313
|
+
if (!(totalTokens + tokens > this.MAX_CONTEXT_TOKENS)) {
|
|
1314
|
+
_context8.next = 18;
|
|
1315
|
+
break;
|
|
1316
|
+
}
|
|
1317
|
+
return _context8.abrupt("break", 22);
|
|
1318
|
+
case 18:
|
|
1319
|
+
totalTokens += tokens;
|
|
1320
|
+
messagesWithinTokenLimit.unshift(_extends({}, message, {
|
|
1321
|
+
tokens: tokens
|
|
1322
|
+
}));
|
|
1323
|
+
case 20:
|
|
1324
|
+
_context8.next = 12;
|
|
1325
|
+
break;
|
|
1326
|
+
case 22:
|
|
1327
|
+
console.log('Format:', format);
|
|
1328
|
+
// Return messages in chronological order
|
|
1329
|
+
return _context8.abrupt("return", this.parseMessages(messagesWithinTokenLimit));
|
|
1330
|
+
case 24:
|
|
1331
|
+
return _context8.abrupt("return", this.parseMessages(filteredMessages));
|
|
1332
|
+
case 25:
|
|
1333
|
+
case "end":
|
|
1334
|
+
return _context8.stop();
|
|
1335
|
+
}
|
|
1336
|
+
}, _callee8, this);
|
|
1337
|
+
}));
|
|
1338
|
+
function getContextWindow(_x10) {
|
|
1339
|
+
return _getContextWindow.apply(this, arguments);
|
|
1340
|
+
}
|
|
1341
|
+
return getContextWindow;
|
|
1342
|
+
}()
|
|
1343
|
+
/**
|
|
1344
|
+
* Messages
|
|
1345
|
+
*/
|
|
1346
|
+
;
|
|
1347
|
+
_proto.getMessages =
|
|
1348
|
+
/*#__PURE__*/
|
|
1349
|
+
function () {
|
|
1350
|
+
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_ref7) {
|
|
1351
|
+
var threadId, messagesKey, messages, parsedMessages, uiMessages;
|
|
1352
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
1353
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
1354
|
+
case 0:
|
|
1355
|
+
threadId = _ref7.threadId;
|
|
1356
|
+
messagesKey = this.getMessagesKey(threadId);
|
|
1357
|
+
_context9.next = 4;
|
|
1358
|
+
return this.kv.lrange(messagesKey, 0, -1);
|
|
1359
|
+
case 4:
|
|
1360
|
+
messages = _context9.sent;
|
|
1361
|
+
parsedMessages = this.parseMessages(messages);
|
|
1362
|
+
uiMessages = this.convertToUIMessages(parsedMessages);
|
|
1363
|
+
return _context9.abrupt("return", {
|
|
1364
|
+
messages: parsedMessages,
|
|
1079
1365
|
uiMessages: uiMessages
|
|
1080
1366
|
});
|
|
1081
|
-
case
|
|
1082
|
-
_context14.prev = 13;
|
|
1083
|
-
client.release();
|
|
1084
|
-
return _context14.finish(13);
|
|
1085
|
-
case 16:
|
|
1367
|
+
case 8:
|
|
1086
1368
|
case "end":
|
|
1087
|
-
return
|
|
1369
|
+
return _context9.stop();
|
|
1088
1370
|
}
|
|
1089
|
-
},
|
|
1371
|
+
}, _callee9, this);
|
|
1090
1372
|
}));
|
|
1091
|
-
function getMessages(
|
|
1373
|
+
function getMessages(_x11) {
|
|
1092
1374
|
return _getMessages.apply(this, arguments);
|
|
1093
1375
|
}
|
|
1094
1376
|
return getMessages;
|
|
1095
1377
|
}();
|
|
1096
|
-
|
|
1378
|
+
_proto.saveMessages = /*#__PURE__*/function () {
|
|
1379
|
+
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(_ref8) {
|
|
1380
|
+
var _this3 = this;
|
|
1381
|
+
var messages, processedMessages, _loop, _iterator2, _step2;
|
|
1382
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context11) {
|
|
1383
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
1384
|
+
case 0:
|
|
1385
|
+
messages = _ref8.messages;
|
|
1386
|
+
processedMessages = [];
|
|
1387
|
+
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
1388
|
+
var message, threadId, toolCallArgs, toolNames, createdAt, messagesKey, processedMessage, _message$content$2, content, hashedToolCallArgs, validArgExists, _iterator3, _step3, hashedArg, isValid, expireAt, _iterator4, _step4, _hashedArg, cacheKey;
|
|
1389
|
+
return _regeneratorRuntime().wrap(function _loop$(_context10) {
|
|
1390
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
1391
|
+
case 0:
|
|
1392
|
+
message = _step2.value;
|
|
1393
|
+
threadId = message.threadId, toolCallArgs = message.toolCallArgs, toolNames = message.toolNames, createdAt = message.createdAt;
|
|
1394
|
+
messagesKey = _this3.getMessagesKey(threadId);
|
|
1395
|
+
processedMessage = _extends({}, message);
|
|
1396
|
+
if (message.type === 'text') {
|
|
1397
|
+
content = message.role === 'assistant' ? ((_message$content$2 = message.content[0]) == null ? void 0 : _message$content$2.text) || '' : message.content;
|
|
1398
|
+
processedMessage.tokens = _this3.estimateTokens(content);
|
|
1399
|
+
}
|
|
1400
|
+
if (!(toolCallArgs != null && toolCallArgs.length)) {
|
|
1401
|
+
_context10.next = 28;
|
|
1402
|
+
break;
|
|
1403
|
+
}
|
|
1404
|
+
hashedToolCallArgs = toolCallArgs.map(function (args, index) {
|
|
1405
|
+
return crypto.createHash('sha256').update(JSON.stringify({
|
|
1406
|
+
args: args,
|
|
1407
|
+
threadId: threadId,
|
|
1408
|
+
toolName: toolNames == null ? void 0 : toolNames[index]
|
|
1409
|
+
})).digest('hex');
|
|
1410
|
+
});
|
|
1411
|
+
validArgExists = true;
|
|
1412
|
+
_iterator3 = _createForOfIteratorHelperLoose(hashedToolCallArgs);
|
|
1413
|
+
case 9:
|
|
1414
|
+
if ((_step3 = _iterator3()).done) {
|
|
1415
|
+
_context10.next = 19;
|
|
1416
|
+
break;
|
|
1417
|
+
}
|
|
1418
|
+
hashedArg = _step3.value;
|
|
1419
|
+
_context10.next = 13;
|
|
1420
|
+
return _this3.validateToolCallArgs({
|
|
1421
|
+
hashedArgs: hashedArg
|
|
1422
|
+
});
|
|
1423
|
+
case 13:
|
|
1424
|
+
isValid = _context10.sent;
|
|
1425
|
+
if (isValid) {
|
|
1426
|
+
_context10.next = 17;
|
|
1427
|
+
break;
|
|
1428
|
+
}
|
|
1429
|
+
validArgExists = false;
|
|
1430
|
+
return _context10.abrupt("break", 19);
|
|
1431
|
+
case 17:
|
|
1432
|
+
_context10.next = 9;
|
|
1433
|
+
break;
|
|
1434
|
+
case 19:
|
|
1435
|
+
expireAt = validArgExists ? createdAt : new Date(createdAt.getTime() + 5 * 60 * 1000); // 5 minutes
|
|
1436
|
+
_iterator4 = _createForOfIteratorHelperLoose(hashedToolCallArgs);
|
|
1437
|
+
case 21:
|
|
1438
|
+
if ((_step4 = _iterator4()).done) {
|
|
1439
|
+
_context10.next = 28;
|
|
1440
|
+
break;
|
|
1441
|
+
}
|
|
1442
|
+
_hashedArg = _step4.value;
|
|
1443
|
+
cacheKey = _this3.getToolCacheKey(_hashedArg);
|
|
1444
|
+
_context10.next = 26;
|
|
1445
|
+
return _this3.kv.set(cacheKey, {
|
|
1446
|
+
expireAt: expireAt.toISOString()
|
|
1447
|
+
});
|
|
1448
|
+
case 26:
|
|
1449
|
+
_context10.next = 21;
|
|
1450
|
+
break;
|
|
1451
|
+
case 28:
|
|
1452
|
+
_context10.next = 30;
|
|
1453
|
+
return _this3.kv.rpush(messagesKey, processedMessage);
|
|
1454
|
+
case 30:
|
|
1455
|
+
processedMessages.push(processedMessage);
|
|
1456
|
+
case 31:
|
|
1457
|
+
case "end":
|
|
1458
|
+
return _context10.stop();
|
|
1459
|
+
}
|
|
1460
|
+
}, _loop);
|
|
1461
|
+
});
|
|
1462
|
+
_iterator2 = _createForOfIteratorHelperLoose(messages);
|
|
1463
|
+
case 4:
|
|
1464
|
+
if ((_step2 = _iterator2()).done) {
|
|
1465
|
+
_context11.next = 8;
|
|
1466
|
+
break;
|
|
1467
|
+
}
|
|
1468
|
+
return _context11.delegateYield(_loop(), "t0", 6);
|
|
1469
|
+
case 6:
|
|
1470
|
+
_context11.next = 4;
|
|
1471
|
+
break;
|
|
1472
|
+
case 8:
|
|
1473
|
+
return _context11.abrupt("return", processedMessages);
|
|
1474
|
+
case 9:
|
|
1475
|
+
case "end":
|
|
1476
|
+
return _context11.stop();
|
|
1477
|
+
}
|
|
1478
|
+
}, _callee10);
|
|
1479
|
+
}));
|
|
1480
|
+
function saveMessages(_x12) {
|
|
1481
|
+
return _saveMessages.apply(this, arguments);
|
|
1482
|
+
}
|
|
1483
|
+
return saveMessages;
|
|
1484
|
+
}();
|
|
1485
|
+
_proto.deleteMessage = /*#__PURE__*/function () {
|
|
1486
|
+
var _deleteMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(id) {
|
|
1487
|
+
var pattern, keys, _iterator5, _step5, key, messages, filteredMessages, _this$kv;
|
|
1488
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context12) {
|
|
1489
|
+
while (1) switch (_context12.prev = _context12.next) {
|
|
1490
|
+
case 0:
|
|
1491
|
+
pattern = this.prefix + ":messages:*";
|
|
1492
|
+
_context12.next = 3;
|
|
1493
|
+
return this.kv.keys(pattern);
|
|
1494
|
+
case 3:
|
|
1495
|
+
keys = _context12.sent;
|
|
1496
|
+
_iterator5 = _createForOfIteratorHelperLoose(keys);
|
|
1497
|
+
case 5:
|
|
1498
|
+
if ((_step5 = _iterator5()).done) {
|
|
1499
|
+
_context12.next = 19;
|
|
1500
|
+
break;
|
|
1501
|
+
}
|
|
1502
|
+
key = _step5.value;
|
|
1503
|
+
_context12.next = 9;
|
|
1504
|
+
return this.kv.lrange(key, 0, -1);
|
|
1505
|
+
case 9:
|
|
1506
|
+
messages = _context12.sent;
|
|
1507
|
+
filteredMessages = messages.filter(function (msg) {
|
|
1508
|
+
return msg.id !== id;
|
|
1509
|
+
});
|
|
1510
|
+
if (!(messages.length !== filteredMessages.length)) {
|
|
1511
|
+
_context12.next = 17;
|
|
1512
|
+
break;
|
|
1513
|
+
}
|
|
1514
|
+
_context12.next = 14;
|
|
1515
|
+
return this.kv.del(key);
|
|
1516
|
+
case 14:
|
|
1517
|
+
if (!(filteredMessages.length > 0)) {
|
|
1518
|
+
_context12.next = 17;
|
|
1519
|
+
break;
|
|
1520
|
+
}
|
|
1521
|
+
_context12.next = 17;
|
|
1522
|
+
return (_this$kv = this.kv).rpush.apply(_this$kv, [key].concat(filteredMessages));
|
|
1523
|
+
case 17:
|
|
1524
|
+
_context12.next = 5;
|
|
1525
|
+
break;
|
|
1526
|
+
case 19:
|
|
1527
|
+
case "end":
|
|
1528
|
+
return _context12.stop();
|
|
1529
|
+
}
|
|
1530
|
+
}, _callee11, this);
|
|
1531
|
+
}));
|
|
1532
|
+
function deleteMessage(_x13) {
|
|
1533
|
+
return _deleteMessage.apply(this, arguments);
|
|
1534
|
+
}
|
|
1535
|
+
return deleteMessage;
|
|
1536
|
+
}()
|
|
1537
|
+
/**
|
|
1538
|
+
* Cleanup
|
|
1539
|
+
*/
|
|
1540
|
+
;
|
|
1541
|
+
_proto.drop =
|
|
1542
|
+
/*#__PURE__*/
|
|
1543
|
+
function () {
|
|
1544
|
+
var _drop = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
|
|
1545
|
+
var pattern, keys, _this$kv2;
|
|
1546
|
+
return _regeneratorRuntime().wrap(function _callee12$(_context13) {
|
|
1547
|
+
while (1) switch (_context13.prev = _context13.next) {
|
|
1548
|
+
case 0:
|
|
1549
|
+
pattern = this.prefix + ":*";
|
|
1550
|
+
_context13.next = 3;
|
|
1551
|
+
return this.kv.keys(pattern);
|
|
1552
|
+
case 3:
|
|
1553
|
+
keys = _context13.sent;
|
|
1554
|
+
if (!(keys.length > 0)) {
|
|
1555
|
+
_context13.next = 7;
|
|
1556
|
+
break;
|
|
1557
|
+
}
|
|
1558
|
+
_context13.next = 7;
|
|
1559
|
+
return (_this$kv2 = this.kv).del.apply(_this$kv2, keys);
|
|
1560
|
+
case 7:
|
|
1561
|
+
case "end":
|
|
1562
|
+
return _context13.stop();
|
|
1563
|
+
}
|
|
1564
|
+
}, _callee12, this);
|
|
1565
|
+
}));
|
|
1566
|
+
function drop() {
|
|
1567
|
+
return _drop.apply(this, arguments);
|
|
1568
|
+
}
|
|
1569
|
+
return drop;
|
|
1570
|
+
}();
|
|
1571
|
+
_proto.parseThread = function parseThread(thread) {
|
|
1572
|
+
return _extends({}, thread, {
|
|
1573
|
+
createdAt: new Date(thread.createdAt),
|
|
1574
|
+
updatedAt: new Date(thread.updatedAt)
|
|
1575
|
+
});
|
|
1576
|
+
};
|
|
1577
|
+
_proto.parseMessages = function parseMessages(messages) {
|
|
1578
|
+
return messages.map(function (message) {
|
|
1579
|
+
return _extends({}, message, {
|
|
1580
|
+
createdAt: new Date(message.createdAt)
|
|
1581
|
+
});
|
|
1582
|
+
});
|
|
1583
|
+
};
|
|
1584
|
+
return UpstashKVMemory;
|
|
1097
1585
|
}(MastraMemory);
|
|
1098
1586
|
|
|
1099
|
-
export { PgMemory };
|
|
1587
|
+
export { PgMemory, UpstashKVMemory };
|
|
1100
1588
|
//# sourceMappingURL=memory.esm.js.map
|