@mastra/memory 0.0.2-alpha.8 → 0.1.0-alpha.65
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 +440 -0
- package/LICENSE +44 -0
- package/dist/index.d.ts +44 -2
- package/dist/index.js +117 -6
- package/package.json +22 -15
- package/src/index.test.ts +7 -0
- package/src/index.ts +170 -2
- package/vitest.config.ts +8 -0
- package/dist/kv/upstash.d.ts +0 -71
- package/dist/memory.cjs.development.js +0 -1579
- package/dist/memory.cjs.development.js.map +0 -1
- package/dist/memory.cjs.production.min.js +0 -2
- package/dist/memory.cjs.production.min.js.map +0 -1
- package/dist/memory.esm.js +0 -1574
- package/dist/memory.esm.js.map +0 -1
- package/dist/postgres/index.d.ts +0 -59
- package/jest.config.ts +0 -19
- package/src/kv/upstash.test.ts +0 -253
- package/src/kv/upstash.ts +0 -298
- package/src/postgres/index.test.ts +0 -68
- package/src/postgres/index.ts +0 -492
|
@@ -1,1579 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var core = require('@mastra/core');
|
|
6
|
-
var crypto = require('crypto');
|
|
7
|
-
var pg = require('pg');
|
|
8
|
-
var redis = require('@upstash/redis');
|
|
9
|
-
|
|
10
|
-
function _arrayLikeToArray(r, a) {
|
|
11
|
-
(null == a || a > r.length) && (a = r.length);
|
|
12
|
-
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
13
|
-
return n;
|
|
14
|
-
}
|
|
15
|
-
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
16
|
-
try {
|
|
17
|
-
var i = n[a](c),
|
|
18
|
-
u = i.value;
|
|
19
|
-
} catch (n) {
|
|
20
|
-
return void e(n);
|
|
21
|
-
}
|
|
22
|
-
i.done ? t(u) : Promise.resolve(u).then(r, o);
|
|
23
|
-
}
|
|
24
|
-
function _asyncToGenerator(n) {
|
|
25
|
-
return function () {
|
|
26
|
-
var t = this,
|
|
27
|
-
e = arguments;
|
|
28
|
-
return new Promise(function (r, o) {
|
|
29
|
-
var a = n.apply(t, e);
|
|
30
|
-
function _next(n) {
|
|
31
|
-
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
|
|
32
|
-
}
|
|
33
|
-
function _throw(n) {
|
|
34
|
-
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
|
|
35
|
-
}
|
|
36
|
-
_next(void 0);
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function _createForOfIteratorHelperLoose(r, e) {
|
|
41
|
-
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
42
|
-
if (t) return (t = t.call(r)).next.bind(t);
|
|
43
|
-
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
|
|
44
|
-
t && (r = t);
|
|
45
|
-
var o = 0;
|
|
46
|
-
return function () {
|
|
47
|
-
return o >= r.length ? {
|
|
48
|
-
done: !0
|
|
49
|
-
} : {
|
|
50
|
-
done: !1,
|
|
51
|
-
value: r[o++]
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
56
|
-
}
|
|
57
|
-
function _extends() {
|
|
58
|
-
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
59
|
-
for (var e = 1; e < arguments.length; e++) {
|
|
60
|
-
var t = arguments[e];
|
|
61
|
-
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
62
|
-
}
|
|
63
|
-
return n;
|
|
64
|
-
}, _extends.apply(null, arguments);
|
|
65
|
-
}
|
|
66
|
-
function _inheritsLoose(t, o) {
|
|
67
|
-
t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
|
|
68
|
-
}
|
|
69
|
-
function _regeneratorRuntime() {
|
|
70
|
-
_regeneratorRuntime = function () {
|
|
71
|
-
return e;
|
|
72
|
-
};
|
|
73
|
-
var t,
|
|
74
|
-
e = {},
|
|
75
|
-
r = Object.prototype,
|
|
76
|
-
n = r.hasOwnProperty,
|
|
77
|
-
o = Object.defineProperty || function (t, e, r) {
|
|
78
|
-
t[e] = r.value;
|
|
79
|
-
},
|
|
80
|
-
i = "function" == typeof Symbol ? Symbol : {},
|
|
81
|
-
a = i.iterator || "@@iterator",
|
|
82
|
-
c = i.asyncIterator || "@@asyncIterator",
|
|
83
|
-
u = i.toStringTag || "@@toStringTag";
|
|
84
|
-
function define(t, e, r) {
|
|
85
|
-
return Object.defineProperty(t, e, {
|
|
86
|
-
value: r,
|
|
87
|
-
enumerable: !0,
|
|
88
|
-
configurable: !0,
|
|
89
|
-
writable: !0
|
|
90
|
-
}), t[e];
|
|
91
|
-
}
|
|
92
|
-
try {
|
|
93
|
-
define({}, "");
|
|
94
|
-
} catch (t) {
|
|
95
|
-
define = function (t, e, r) {
|
|
96
|
-
return t[e] = r;
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
function wrap(t, e, r, n) {
|
|
100
|
-
var i = e && e.prototype instanceof Generator ? e : Generator,
|
|
101
|
-
a = Object.create(i.prototype),
|
|
102
|
-
c = new Context(n || []);
|
|
103
|
-
return o(a, "_invoke", {
|
|
104
|
-
value: makeInvokeMethod(t, r, c)
|
|
105
|
-
}), a;
|
|
106
|
-
}
|
|
107
|
-
function tryCatch(t, e, r) {
|
|
108
|
-
try {
|
|
109
|
-
return {
|
|
110
|
-
type: "normal",
|
|
111
|
-
arg: t.call(e, r)
|
|
112
|
-
};
|
|
113
|
-
} catch (t) {
|
|
114
|
-
return {
|
|
115
|
-
type: "throw",
|
|
116
|
-
arg: t
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
e.wrap = wrap;
|
|
121
|
-
var h = "suspendedStart",
|
|
122
|
-
l = "suspendedYield",
|
|
123
|
-
f = "executing",
|
|
124
|
-
s = "completed",
|
|
125
|
-
y = {};
|
|
126
|
-
function Generator() {}
|
|
127
|
-
function GeneratorFunction() {}
|
|
128
|
-
function GeneratorFunctionPrototype() {}
|
|
129
|
-
var p = {};
|
|
130
|
-
define(p, a, function () {
|
|
131
|
-
return this;
|
|
132
|
-
});
|
|
133
|
-
var d = Object.getPrototypeOf,
|
|
134
|
-
v = d && d(d(values([])));
|
|
135
|
-
v && v !== r && n.call(v, a) && (p = v);
|
|
136
|
-
var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
|
|
137
|
-
function defineIteratorMethods(t) {
|
|
138
|
-
["next", "throw", "return"].forEach(function (e) {
|
|
139
|
-
define(t, e, function (t) {
|
|
140
|
-
return this._invoke(e, t);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
function AsyncIterator(t, e) {
|
|
145
|
-
function invoke(r, o, i, a) {
|
|
146
|
-
var c = tryCatch(t[r], t, o);
|
|
147
|
-
if ("throw" !== c.type) {
|
|
148
|
-
var u = c.arg,
|
|
149
|
-
h = u.value;
|
|
150
|
-
return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
|
|
151
|
-
invoke("next", t, i, a);
|
|
152
|
-
}, function (t) {
|
|
153
|
-
invoke("throw", t, i, a);
|
|
154
|
-
}) : e.resolve(h).then(function (t) {
|
|
155
|
-
u.value = t, i(u);
|
|
156
|
-
}, function (t) {
|
|
157
|
-
return invoke("throw", t, i, a);
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
a(c.arg);
|
|
161
|
-
}
|
|
162
|
-
var r;
|
|
163
|
-
o(this, "_invoke", {
|
|
164
|
-
value: function (t, n) {
|
|
165
|
-
function callInvokeWithMethodAndArg() {
|
|
166
|
-
return new e(function (e, r) {
|
|
167
|
-
invoke(t, n, e, r);
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
function makeInvokeMethod(e, r, n) {
|
|
175
|
-
var o = h;
|
|
176
|
-
return function (i, a) {
|
|
177
|
-
if (o === f) throw Error("Generator is already running");
|
|
178
|
-
if (o === s) {
|
|
179
|
-
if ("throw" === i) throw a;
|
|
180
|
-
return {
|
|
181
|
-
value: t,
|
|
182
|
-
done: !0
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
for (n.method = i, n.arg = a;;) {
|
|
186
|
-
var c = n.delegate;
|
|
187
|
-
if (c) {
|
|
188
|
-
var u = maybeInvokeDelegate(c, n);
|
|
189
|
-
if (u) {
|
|
190
|
-
if (u === y) continue;
|
|
191
|
-
return u;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
|
|
195
|
-
if (o === h) throw o = s, n.arg;
|
|
196
|
-
n.dispatchException(n.arg);
|
|
197
|
-
} else "return" === n.method && n.abrupt("return", n.arg);
|
|
198
|
-
o = f;
|
|
199
|
-
var p = tryCatch(e, r, n);
|
|
200
|
-
if ("normal" === p.type) {
|
|
201
|
-
if (o = n.done ? s : l, p.arg === y) continue;
|
|
202
|
-
return {
|
|
203
|
-
value: p.arg,
|
|
204
|
-
done: n.done
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
"throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
function maybeInvokeDelegate(e, r) {
|
|
212
|
-
var n = r.method,
|
|
213
|
-
o = e.iterator[n];
|
|
214
|
-
if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
|
|
215
|
-
var i = tryCatch(o, e.iterator, r.arg);
|
|
216
|
-
if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
|
|
217
|
-
var a = i.arg;
|
|
218
|
-
return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
|
|
219
|
-
}
|
|
220
|
-
function pushTryEntry(t) {
|
|
221
|
-
var e = {
|
|
222
|
-
tryLoc: t[0]
|
|
223
|
-
};
|
|
224
|
-
1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
|
|
225
|
-
}
|
|
226
|
-
function resetTryEntry(t) {
|
|
227
|
-
var e = t.completion || {};
|
|
228
|
-
e.type = "normal", delete e.arg, t.completion = e;
|
|
229
|
-
}
|
|
230
|
-
function Context(t) {
|
|
231
|
-
this.tryEntries = [{
|
|
232
|
-
tryLoc: "root"
|
|
233
|
-
}], t.forEach(pushTryEntry, this), this.reset(!0);
|
|
234
|
-
}
|
|
235
|
-
function values(e) {
|
|
236
|
-
if (e || "" === e) {
|
|
237
|
-
var r = e[a];
|
|
238
|
-
if (r) return r.call(e);
|
|
239
|
-
if ("function" == typeof e.next) return e;
|
|
240
|
-
if (!isNaN(e.length)) {
|
|
241
|
-
var o = -1,
|
|
242
|
-
i = function next() {
|
|
243
|
-
for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
|
|
244
|
-
return next.value = t, next.done = !0, next;
|
|
245
|
-
};
|
|
246
|
-
return i.next = i;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
throw new TypeError(typeof e + " is not iterable");
|
|
250
|
-
}
|
|
251
|
-
return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
|
|
252
|
-
value: GeneratorFunctionPrototype,
|
|
253
|
-
configurable: !0
|
|
254
|
-
}), o(GeneratorFunctionPrototype, "constructor", {
|
|
255
|
-
value: GeneratorFunction,
|
|
256
|
-
configurable: !0
|
|
257
|
-
}), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
|
|
258
|
-
var e = "function" == typeof t && t.constructor;
|
|
259
|
-
return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
|
|
260
|
-
}, e.mark = function (t) {
|
|
261
|
-
return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
|
|
262
|
-
}, e.awrap = function (t) {
|
|
263
|
-
return {
|
|
264
|
-
__await: t
|
|
265
|
-
};
|
|
266
|
-
}, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
|
|
267
|
-
return this;
|
|
268
|
-
}), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
|
|
269
|
-
void 0 === i && (i = Promise);
|
|
270
|
-
var a = new AsyncIterator(wrap(t, r, n, o), i);
|
|
271
|
-
return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
|
|
272
|
-
return t.done ? t.value : a.next();
|
|
273
|
-
});
|
|
274
|
-
}, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
|
|
275
|
-
return this;
|
|
276
|
-
}), define(g, "toString", function () {
|
|
277
|
-
return "[object Generator]";
|
|
278
|
-
}), e.keys = function (t) {
|
|
279
|
-
var e = Object(t),
|
|
280
|
-
r = [];
|
|
281
|
-
for (var n in e) r.push(n);
|
|
282
|
-
return r.reverse(), function next() {
|
|
283
|
-
for (; r.length;) {
|
|
284
|
-
var t = r.pop();
|
|
285
|
-
if (t in e) return next.value = t, next.done = !1, next;
|
|
286
|
-
}
|
|
287
|
-
return next.done = !0, next;
|
|
288
|
-
};
|
|
289
|
-
}, e.values = values, Context.prototype = {
|
|
290
|
-
constructor: Context,
|
|
291
|
-
reset: function (e) {
|
|
292
|
-
if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
|
|
293
|
-
},
|
|
294
|
-
stop: function () {
|
|
295
|
-
this.done = !0;
|
|
296
|
-
var t = this.tryEntries[0].completion;
|
|
297
|
-
if ("throw" === t.type) throw t.arg;
|
|
298
|
-
return this.rval;
|
|
299
|
-
},
|
|
300
|
-
dispatchException: function (e) {
|
|
301
|
-
if (this.done) throw e;
|
|
302
|
-
var r = this;
|
|
303
|
-
function handle(n, o) {
|
|
304
|
-
return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
|
|
305
|
-
}
|
|
306
|
-
for (var o = this.tryEntries.length - 1; o >= 0; --o) {
|
|
307
|
-
var i = this.tryEntries[o],
|
|
308
|
-
a = i.completion;
|
|
309
|
-
if ("root" === i.tryLoc) return handle("end");
|
|
310
|
-
if (i.tryLoc <= this.prev) {
|
|
311
|
-
var c = n.call(i, "catchLoc"),
|
|
312
|
-
u = n.call(i, "finallyLoc");
|
|
313
|
-
if (c && u) {
|
|
314
|
-
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
315
|
-
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
316
|
-
} else if (c) {
|
|
317
|
-
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
318
|
-
} else {
|
|
319
|
-
if (!u) throw Error("try statement without catch or finally");
|
|
320
|
-
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
},
|
|
325
|
-
abrupt: function (t, e) {
|
|
326
|
-
for (var r = this.tryEntries.length - 1; r >= 0; --r) {
|
|
327
|
-
var o = this.tryEntries[r];
|
|
328
|
-
if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
|
|
329
|
-
var i = o;
|
|
330
|
-
break;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
|
|
334
|
-
var a = i ? i.completion : {};
|
|
335
|
-
return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
|
|
336
|
-
},
|
|
337
|
-
complete: function (t, e) {
|
|
338
|
-
if ("throw" === t.type) throw t.arg;
|
|
339
|
-
return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
|
|
340
|
-
},
|
|
341
|
-
finish: function (t) {
|
|
342
|
-
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
343
|
-
var r = this.tryEntries[e];
|
|
344
|
-
if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
|
|
345
|
-
}
|
|
346
|
-
},
|
|
347
|
-
catch: function (t) {
|
|
348
|
-
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
349
|
-
var r = this.tryEntries[e];
|
|
350
|
-
if (r.tryLoc === t) {
|
|
351
|
-
var n = r.completion;
|
|
352
|
-
if ("throw" === n.type) {
|
|
353
|
-
var o = n.arg;
|
|
354
|
-
resetTryEntry(r);
|
|
355
|
-
}
|
|
356
|
-
return o;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
throw Error("illegal catch attempt");
|
|
360
|
-
},
|
|
361
|
-
delegateYield: function (e, r, n) {
|
|
362
|
-
return this.delegate = {
|
|
363
|
-
iterator: values(e),
|
|
364
|
-
resultName: r,
|
|
365
|
-
nextLoc: n
|
|
366
|
-
}, "next" === this.method && (this.arg = t), y;
|
|
367
|
-
}
|
|
368
|
-
}, e;
|
|
369
|
-
}
|
|
370
|
-
function _setPrototypeOf(t, e) {
|
|
371
|
-
return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
|
|
372
|
-
return t.__proto__ = e, t;
|
|
373
|
-
}, _setPrototypeOf(t, e);
|
|
374
|
-
}
|
|
375
|
-
function _unsupportedIterableToArray(r, a) {
|
|
376
|
-
if (r) {
|
|
377
|
-
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
378
|
-
var t = {}.toString.call(r).slice(8, -1);
|
|
379
|
-
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
var Pool = pg.Pool;
|
|
384
|
-
var PgMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
385
|
-
function PgMemory(config) {
|
|
386
|
-
var _this;
|
|
387
|
-
_this = _MastraMemory.call(this) || this;
|
|
388
|
-
_this.pool = void 0;
|
|
389
|
-
_this.hasTables = false;
|
|
390
|
-
_this.pool = new Pool({
|
|
391
|
-
connectionString: config.connectionString
|
|
392
|
-
});
|
|
393
|
-
_this.MAX_CONTEXT_TOKENS = config.maxTokens;
|
|
394
|
-
return _this;
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Threads
|
|
398
|
-
*/
|
|
399
|
-
_inheritsLoose(PgMemory, _MastraMemory);
|
|
400
|
-
var _proto = PgMemory.prototype;
|
|
401
|
-
_proto.getThreadById =
|
|
402
|
-
/*#__PURE__*/
|
|
403
|
-
function () {
|
|
404
|
-
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
405
|
-
var threadId, client, result;
|
|
406
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
407
|
-
while (1) switch (_context.prev = _context.next) {
|
|
408
|
-
case 0:
|
|
409
|
-
threadId = _ref.threadId;
|
|
410
|
-
_context.next = 3;
|
|
411
|
-
return this.ensureTablesExist();
|
|
412
|
-
case 3:
|
|
413
|
-
_context.next = 5;
|
|
414
|
-
return this.pool.connect();
|
|
415
|
-
case 5:
|
|
416
|
-
client = _context.sent;
|
|
417
|
-
_context.prev = 6;
|
|
418
|
-
_context.next = 9;
|
|
419
|
-
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]);
|
|
420
|
-
case 9:
|
|
421
|
-
result = _context.sent;
|
|
422
|
-
return _context.abrupt("return", result.rows[0] || null);
|
|
423
|
-
case 11:
|
|
424
|
-
_context.prev = 11;
|
|
425
|
-
client.release();
|
|
426
|
-
return _context.finish(11);
|
|
427
|
-
case 14:
|
|
428
|
-
case "end":
|
|
429
|
-
return _context.stop();
|
|
430
|
-
}
|
|
431
|
-
}, _callee, this, [[6,, 11, 14]]);
|
|
432
|
-
}));
|
|
433
|
-
function getThreadById(_x) {
|
|
434
|
-
return _getThreadById.apply(this, arguments);
|
|
435
|
-
}
|
|
436
|
-
return getThreadById;
|
|
437
|
-
}();
|
|
438
|
-
_proto.getThreadsByResourceId = /*#__PURE__*/function () {
|
|
439
|
-
var _getThreadsByResourceId = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref2) {
|
|
440
|
-
var resourceid, client, result;
|
|
441
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
442
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
443
|
-
case 0:
|
|
444
|
-
resourceid = _ref2.resourceid;
|
|
445
|
-
_context2.next = 3;
|
|
446
|
-
return this.ensureTablesExist();
|
|
447
|
-
case 3:
|
|
448
|
-
_context2.next = 5;
|
|
449
|
-
return this.pool.connect();
|
|
450
|
-
case 5:
|
|
451
|
-
client = _context2.sent;
|
|
452
|
-
_context2.prev = 6;
|
|
453
|
-
_context2.next = 9;
|
|
454
|
-
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]);
|
|
455
|
-
case 9:
|
|
456
|
-
result = _context2.sent;
|
|
457
|
-
return _context2.abrupt("return", result.rows);
|
|
458
|
-
case 11:
|
|
459
|
-
_context2.prev = 11;
|
|
460
|
-
client.release();
|
|
461
|
-
return _context2.finish(11);
|
|
462
|
-
case 14:
|
|
463
|
-
case "end":
|
|
464
|
-
return _context2.stop();
|
|
465
|
-
}
|
|
466
|
-
}, _callee2, this, [[6,, 11, 14]]);
|
|
467
|
-
}));
|
|
468
|
-
function getThreadsByResourceId(_x2) {
|
|
469
|
-
return _getThreadsByResourceId.apply(this, arguments);
|
|
470
|
-
}
|
|
471
|
-
return getThreadsByResourceId;
|
|
472
|
-
}();
|
|
473
|
-
_proto.saveThread = /*#__PURE__*/function () {
|
|
474
|
-
var _saveThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref3) {
|
|
475
|
-
var thread, client, _result$rows, id, title, createdAt, updatedAt, resourceid, metadata, result;
|
|
476
|
-
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
477
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
478
|
-
case 0:
|
|
479
|
-
thread = _ref3.thread;
|
|
480
|
-
_context3.next = 3;
|
|
481
|
-
return this.ensureTablesExist();
|
|
482
|
-
case 3:
|
|
483
|
-
_context3.next = 5;
|
|
484
|
-
return this.pool.connect();
|
|
485
|
-
case 5:
|
|
486
|
-
client = _context3.sent;
|
|
487
|
-
_context3.prev = 6;
|
|
488
|
-
id = thread.id, title = thread.title, createdAt = thread.createdAt, updatedAt = thread.updatedAt, resourceid = thread.resourceid, metadata = thread.metadata;
|
|
489
|
-
_context3.next = 10;
|
|
490
|
-
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)]);
|
|
491
|
-
case 10:
|
|
492
|
-
result = _context3.sent;
|
|
493
|
-
return _context3.abrupt("return", result == null || (_result$rows = result.rows) == null ? void 0 : _result$rows[0]);
|
|
494
|
-
case 12:
|
|
495
|
-
_context3.prev = 12;
|
|
496
|
-
client.release();
|
|
497
|
-
return _context3.finish(12);
|
|
498
|
-
case 15:
|
|
499
|
-
case "end":
|
|
500
|
-
return _context3.stop();
|
|
501
|
-
}
|
|
502
|
-
}, _callee3, this, [[6,, 12, 15]]);
|
|
503
|
-
}));
|
|
504
|
-
function saveThread(_x3) {
|
|
505
|
-
return _saveThread.apply(this, arguments);
|
|
506
|
-
}
|
|
507
|
-
return saveThread;
|
|
508
|
-
}();
|
|
509
|
-
_proto.updateThread = /*#__PURE__*/function () {
|
|
510
|
-
var _updateThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(id, title, metadata) {
|
|
511
|
-
var client, _result$rows2, result;
|
|
512
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
513
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
514
|
-
case 0:
|
|
515
|
-
_context4.next = 2;
|
|
516
|
-
return this.pool.connect();
|
|
517
|
-
case 2:
|
|
518
|
-
client = _context4.sent;
|
|
519
|
-
_context4.prev = 3;
|
|
520
|
-
_context4.next = 6;
|
|
521
|
-
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]);
|
|
522
|
-
case 6:
|
|
523
|
-
result = _context4.sent;
|
|
524
|
-
return _context4.abrupt("return", result == null || (_result$rows2 = result.rows) == null ? void 0 : _result$rows2[0]);
|
|
525
|
-
case 8:
|
|
526
|
-
_context4.prev = 8;
|
|
527
|
-
client.release();
|
|
528
|
-
return _context4.finish(8);
|
|
529
|
-
case 11:
|
|
530
|
-
case "end":
|
|
531
|
-
return _context4.stop();
|
|
532
|
-
}
|
|
533
|
-
}, _callee4, this, [[3,, 8, 11]]);
|
|
534
|
-
}));
|
|
535
|
-
function updateThread(_x4, _x5, _x6) {
|
|
536
|
-
return _updateThread.apply(this, arguments);
|
|
537
|
-
}
|
|
538
|
-
return updateThread;
|
|
539
|
-
}();
|
|
540
|
-
_proto.deleteThread = /*#__PURE__*/function () {
|
|
541
|
-
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(id) {
|
|
542
|
-
var client;
|
|
543
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
544
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
545
|
-
case 0:
|
|
546
|
-
_context5.next = 2;
|
|
547
|
-
return this.pool.connect();
|
|
548
|
-
case 2:
|
|
549
|
-
client = _context5.sent;
|
|
550
|
-
_context5.prev = 3;
|
|
551
|
-
_context5.next = 6;
|
|
552
|
-
return client.query("\n DELETE FROM mastra_messages\n WHERE thread_id = $1\n ", [id]);
|
|
553
|
-
case 6:
|
|
554
|
-
_context5.next = 8;
|
|
555
|
-
return client.query("\n DELETE FROM mastra_threads\n WHERE id = $1\n ", [id]);
|
|
556
|
-
case 8:
|
|
557
|
-
_context5.prev = 8;
|
|
558
|
-
client.release();
|
|
559
|
-
return _context5.finish(8);
|
|
560
|
-
case 11:
|
|
561
|
-
case "end":
|
|
562
|
-
return _context5.stop();
|
|
563
|
-
}
|
|
564
|
-
}, _callee5, this, [[3,, 8, 11]]);
|
|
565
|
-
}));
|
|
566
|
-
function deleteThread(_x7) {
|
|
567
|
-
return _deleteThread.apply(this, arguments);
|
|
568
|
-
}
|
|
569
|
-
return deleteThread;
|
|
570
|
-
}()
|
|
571
|
-
/**
|
|
572
|
-
* Tool Cache
|
|
573
|
-
*/
|
|
574
|
-
;
|
|
575
|
-
_proto.validateToolCallArgs =
|
|
576
|
-
/*#__PURE__*/
|
|
577
|
-
function () {
|
|
578
|
-
var _validateToolCallArgs = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref4) {
|
|
579
|
-
var hashedArgs, client, toolArgsResult;
|
|
580
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
581
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
582
|
-
case 0:
|
|
583
|
-
hashedArgs = _ref4.hashedArgs;
|
|
584
|
-
_context6.next = 3;
|
|
585
|
-
return this.ensureTablesExist();
|
|
586
|
-
case 3:
|
|
587
|
-
_context6.next = 5;
|
|
588
|
-
return this.pool.connect();
|
|
589
|
-
case 5:
|
|
590
|
-
client = _context6.sent;
|
|
591
|
-
_context6.prev = 6;
|
|
592
|
-
_context6.next = 9;
|
|
593
|
-
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()]);
|
|
594
|
-
case 9:
|
|
595
|
-
toolArgsResult = _context6.sent;
|
|
596
|
-
return _context6.abrupt("return", toolArgsResult.rows.length > 0);
|
|
597
|
-
case 13:
|
|
598
|
-
_context6.prev = 13;
|
|
599
|
-
_context6.t0 = _context6["catch"](6);
|
|
600
|
-
console.log('error checking if valid arg exists====', _context6.t0);
|
|
601
|
-
return _context6.abrupt("return", false);
|
|
602
|
-
case 17:
|
|
603
|
-
_context6.prev = 17;
|
|
604
|
-
client.release();
|
|
605
|
-
return _context6.finish(17);
|
|
606
|
-
case 20:
|
|
607
|
-
case "end":
|
|
608
|
-
return _context6.stop();
|
|
609
|
-
}
|
|
610
|
-
}, _callee6, this, [[6, 13, 17, 20]]);
|
|
611
|
-
}));
|
|
612
|
-
function validateToolCallArgs(_x8) {
|
|
613
|
-
return _validateToolCallArgs.apply(this, arguments);
|
|
614
|
-
}
|
|
615
|
-
return validateToolCallArgs;
|
|
616
|
-
}();
|
|
617
|
-
_proto.getToolResult = /*#__PURE__*/function () {
|
|
618
|
-
var _getToolResult = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref5) {
|
|
619
|
-
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;
|
|
620
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
621
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
622
|
-
case 0:
|
|
623
|
-
threadId = _ref5.threadId, toolArgs = _ref5.toolArgs, toolName = _ref5.toolName;
|
|
624
|
-
_context7.next = 3;
|
|
625
|
-
return this.ensureTablesExist();
|
|
626
|
-
case 3:
|
|
627
|
-
console.log('checking for cached tool result====', JSON.stringify(toolArgs, null, 2));
|
|
628
|
-
_context7.next = 6;
|
|
629
|
-
return this.pool.connect();
|
|
630
|
-
case 6:
|
|
631
|
-
client = _context7.sent;
|
|
632
|
-
_context7.prev = 7;
|
|
633
|
-
hashedToolArgs = crypto.createHash('sha256').update(JSON.stringify({
|
|
634
|
-
args: toolArgs,
|
|
635
|
-
threadId: threadId,
|
|
636
|
-
toolName: toolName
|
|
637
|
-
})).digest('hex');
|
|
638
|
-
_context7.next = 11;
|
|
639
|
-
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()]);
|
|
640
|
-
case 11:
|
|
641
|
-
toolArgsResult = _context7.sent;
|
|
642
|
-
if (!(toolArgsResult.rows.length > 0)) {
|
|
643
|
-
_context7.next = 28;
|
|
644
|
-
break;
|
|
645
|
-
}
|
|
646
|
-
toolCallArgs = JSON.parse((_toolArgsResult$rows$ = toolArgsResult.rows[0]) == null ? void 0 : _toolArgsResult$rows$.tool_call_args);
|
|
647
|
-
toolCallIds = JSON.parse((_toolArgsResult$rows$2 = toolArgsResult.rows[0]) == null ? void 0 : _toolArgsResult$rows$2.tool_call_ids);
|
|
648
|
-
createdAt = (_toolArgsResult$rows$3 = toolArgsResult.rows[0]) == null ? void 0 : _toolArgsResult$rows$3.created_at;
|
|
649
|
-
toolCallArgsIndex = toolCallArgs.findIndex(function (arg) {
|
|
650
|
-
return arg === hashedToolArgs;
|
|
651
|
-
});
|
|
652
|
-
correspondingToolCallId = toolCallIds[toolCallArgsIndex];
|
|
653
|
-
_context7.next = 20;
|
|
654
|
-
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()]);
|
|
655
|
-
case 20:
|
|
656
|
-
toolResult = _context7.sent;
|
|
657
|
-
if (!(toolResult.rows.length === 0)) {
|
|
658
|
-
_context7.next = 24;
|
|
659
|
-
break;
|
|
660
|
-
}
|
|
661
|
-
console.log('no tool result found');
|
|
662
|
-
return _context7.abrupt("return", null);
|
|
663
|
-
case 24:
|
|
664
|
-
toolResultContent = JSON.parse((_toolResult$rows$ = toolResult.rows[0]) == null ? void 0 : _toolResult$rows$.content);
|
|
665
|
-
requiredToolResult = toolResultContent.find(function (part) {
|
|
666
|
-
return part.toolCallId === correspondingToolCallId;
|
|
667
|
-
});
|
|
668
|
-
if (!requiredToolResult) {
|
|
669
|
-
_context7.next = 28;
|
|
670
|
-
break;
|
|
671
|
-
}
|
|
672
|
-
return _context7.abrupt("return", requiredToolResult.result);
|
|
673
|
-
case 28:
|
|
674
|
-
return _context7.abrupt("return", null);
|
|
675
|
-
case 31:
|
|
676
|
-
_context7.prev = 31;
|
|
677
|
-
_context7.t0 = _context7["catch"](7);
|
|
678
|
-
console.log('error getting cached tool result====', _context7.t0);
|
|
679
|
-
return _context7.abrupt("return", null);
|
|
680
|
-
case 35:
|
|
681
|
-
_context7.prev = 35;
|
|
682
|
-
client.release();
|
|
683
|
-
return _context7.finish(35);
|
|
684
|
-
case 38:
|
|
685
|
-
case "end":
|
|
686
|
-
return _context7.stop();
|
|
687
|
-
}
|
|
688
|
-
}, _callee7, this, [[7, 31, 35, 38]]);
|
|
689
|
-
}));
|
|
690
|
-
function getToolResult(_x9) {
|
|
691
|
-
return _getToolResult.apply(this, arguments);
|
|
692
|
-
}
|
|
693
|
-
return getToolResult;
|
|
694
|
-
}();
|
|
695
|
-
_proto.getContextWindow = /*#__PURE__*/function () {
|
|
696
|
-
var _getContextWindow = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref6) {
|
|
697
|
-
var threadId, startDate, endDate, _ref6$format, format, client, _result, result;
|
|
698
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
699
|
-
while (1) switch (_context8.prev = _context8.next) {
|
|
700
|
-
case 0:
|
|
701
|
-
threadId = _ref6.threadId, startDate = _ref6.startDate, endDate = _ref6.endDate, _ref6$format = _ref6.format, format = _ref6$format === void 0 ? 'raw' : _ref6$format;
|
|
702
|
-
_context8.next = 3;
|
|
703
|
-
return this.ensureTablesExist();
|
|
704
|
-
case 3:
|
|
705
|
-
_context8.next = 5;
|
|
706
|
-
return this.pool.connect();
|
|
707
|
-
case 5:
|
|
708
|
-
client = _context8.sent;
|
|
709
|
-
_context8.prev = 6;
|
|
710
|
-
if (!this.MAX_CONTEXT_TOKENS) {
|
|
711
|
-
_context8.next = 13;
|
|
712
|
-
break;
|
|
713
|
-
}
|
|
714
|
-
_context8.next = 10;
|
|
715
|
-
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]);
|
|
716
|
-
case 10:
|
|
717
|
-
_result = _context8.sent;
|
|
718
|
-
console.log('Format', format);
|
|
719
|
-
return _context8.abrupt("return", this.parseMessages(_result.rows));
|
|
720
|
-
case 13:
|
|
721
|
-
_context8.next = 15;
|
|
722
|
-
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]);
|
|
723
|
-
case 15:
|
|
724
|
-
result = _context8.sent;
|
|
725
|
-
console.log('Format', format);
|
|
726
|
-
return _context8.abrupt("return", this.parseMessages(result.rows));
|
|
727
|
-
case 20:
|
|
728
|
-
_context8.prev = 20;
|
|
729
|
-
_context8.t0 = _context8["catch"](6);
|
|
730
|
-
console.log('error getting context window====', _context8.t0);
|
|
731
|
-
return _context8.abrupt("return", []);
|
|
732
|
-
case 24:
|
|
733
|
-
_context8.prev = 24;
|
|
734
|
-
client.release();
|
|
735
|
-
return _context8.finish(24);
|
|
736
|
-
case 27:
|
|
737
|
-
case "end":
|
|
738
|
-
return _context8.stop();
|
|
739
|
-
}
|
|
740
|
-
}, _callee8, this, [[6, 20, 24, 27]]);
|
|
741
|
-
}));
|
|
742
|
-
function getContextWindow(_x10) {
|
|
743
|
-
return _getContextWindow.apply(this, arguments);
|
|
744
|
-
}
|
|
745
|
-
return getContextWindow;
|
|
746
|
-
}()
|
|
747
|
-
/**
|
|
748
|
-
* Messages
|
|
749
|
-
*/
|
|
750
|
-
;
|
|
751
|
-
_proto.getMessages =
|
|
752
|
-
/*#__PURE__*/
|
|
753
|
-
function () {
|
|
754
|
-
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_ref7) {
|
|
755
|
-
var threadId, client, result, messages, uiMessages;
|
|
756
|
-
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
757
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
758
|
-
case 0:
|
|
759
|
-
threadId = _ref7.threadId;
|
|
760
|
-
_context9.next = 3;
|
|
761
|
-
return this.ensureTablesExist();
|
|
762
|
-
case 3:
|
|
763
|
-
_context9.next = 5;
|
|
764
|
-
return this.pool.connect();
|
|
765
|
-
case 5:
|
|
766
|
-
client = _context9.sent;
|
|
767
|
-
_context9.prev = 6;
|
|
768
|
-
_context9.next = 9;
|
|
769
|
-
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]);
|
|
770
|
-
case 9:
|
|
771
|
-
result = _context9.sent;
|
|
772
|
-
messages = this.parseMessages(result.rows);
|
|
773
|
-
uiMessages = this.convertToUIMessages(messages);
|
|
774
|
-
return _context9.abrupt("return", {
|
|
775
|
-
messages: messages,
|
|
776
|
-
uiMessages: uiMessages
|
|
777
|
-
});
|
|
778
|
-
case 13:
|
|
779
|
-
_context9.prev = 13;
|
|
780
|
-
client.release();
|
|
781
|
-
return _context9.finish(13);
|
|
782
|
-
case 16:
|
|
783
|
-
case "end":
|
|
784
|
-
return _context9.stop();
|
|
785
|
-
}
|
|
786
|
-
}, _callee9, this, [[6,, 13, 16]]);
|
|
787
|
-
}));
|
|
788
|
-
function getMessages(_x11) {
|
|
789
|
-
return _getMessages.apply(this, arguments);
|
|
790
|
-
}
|
|
791
|
-
return getMessages;
|
|
792
|
-
}();
|
|
793
|
-
_proto.saveMessages = /*#__PURE__*/function () {
|
|
794
|
-
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(_ref8) {
|
|
795
|
-
var _this2 = this;
|
|
796
|
-
var messages, client, _loop, _iterator, _step;
|
|
797
|
-
return _regeneratorRuntime().wrap(function _callee10$(_context11) {
|
|
798
|
-
while (1) switch (_context11.prev = _context11.next) {
|
|
799
|
-
case 0:
|
|
800
|
-
messages = _ref8.messages;
|
|
801
|
-
_context11.next = 3;
|
|
802
|
-
return this.ensureTablesExist();
|
|
803
|
-
case 3:
|
|
804
|
-
_context11.next = 5;
|
|
805
|
-
return this.pool.connect();
|
|
806
|
-
case 5:
|
|
807
|
-
client = _context11.sent;
|
|
808
|
-
_context11.prev = 6;
|
|
809
|
-
_context11.next = 9;
|
|
810
|
-
return client.query('BEGIN');
|
|
811
|
-
case 9:
|
|
812
|
-
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
813
|
-
var message, id, content, role, createdAt, threadId, toolCallIds, toolCallArgs, type, toolNames, tokens, _content$, contentMssg, hashedToolCallArgs, validArgExists, i, isValid, toolCallArgsExpireAt;
|
|
814
|
-
return _regeneratorRuntime().wrap(function _loop$(_context10) {
|
|
815
|
-
while (1) switch (_context10.prev = _context10.next) {
|
|
816
|
-
case 0:
|
|
817
|
-
message = _step.value;
|
|
818
|
-
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;
|
|
819
|
-
tokens = null;
|
|
820
|
-
if (type === 'text') {
|
|
821
|
-
contentMssg = role === 'assistant' ? ((_content$ = content[0]) == null ? void 0 : _content$.text) || '' : content;
|
|
822
|
-
tokens = _this2.estimateTokens(contentMssg);
|
|
823
|
-
}
|
|
824
|
-
// Hash the toolCallArgs if they exist
|
|
825
|
-
hashedToolCallArgs = toolCallArgs ? toolCallArgs.map(function (args, index) {
|
|
826
|
-
return crypto.createHash('sha256').update(JSON.stringify({
|
|
827
|
-
args: args,
|
|
828
|
-
threadId: threadId,
|
|
829
|
-
toolName: toolNames == null ? void 0 : toolNames[index]
|
|
830
|
-
})).digest('hex');
|
|
831
|
-
}) : null;
|
|
832
|
-
validArgExists = false;
|
|
833
|
-
if (!(hashedToolCallArgs != null && hashedToolCallArgs.length)) {
|
|
834
|
-
_context10.next = 19;
|
|
835
|
-
break;
|
|
836
|
-
}
|
|
837
|
-
// Check all args sequentially
|
|
838
|
-
validArgExists = true; // Start true and set to false if any check fails
|
|
839
|
-
i = 0;
|
|
840
|
-
case 9:
|
|
841
|
-
if (!(i < hashedToolCallArgs.length)) {
|
|
842
|
-
_context10.next = 19;
|
|
843
|
-
break;
|
|
844
|
-
}
|
|
845
|
-
_context10.next = 12;
|
|
846
|
-
return _this2.validateToolCallArgs({
|
|
847
|
-
hashedArgs: hashedToolCallArgs[i]
|
|
848
|
-
});
|
|
849
|
-
case 12:
|
|
850
|
-
isValid = _context10.sent;
|
|
851
|
-
if (isValid) {
|
|
852
|
-
_context10.next = 16;
|
|
853
|
-
break;
|
|
854
|
-
}
|
|
855
|
-
validArgExists = false;
|
|
856
|
-
return _context10.abrupt("break", 19);
|
|
857
|
-
case 16:
|
|
858
|
-
i++;
|
|
859
|
-
_context10.next = 9;
|
|
860
|
-
break;
|
|
861
|
-
case 19:
|
|
862
|
-
toolCallArgsExpireAt = !toolCallArgs ? null : validArgExists ? createdAt : new Date(createdAt.getTime() + 5 * 60 * 1000); // 5 minutes
|
|
863
|
-
_context10.next = 22;
|
|
864
|
-
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()]);
|
|
865
|
-
case 22:
|
|
866
|
-
case "end":
|
|
867
|
-
return _context10.stop();
|
|
868
|
-
}
|
|
869
|
-
}, _loop);
|
|
870
|
-
});
|
|
871
|
-
_iterator = _createForOfIteratorHelperLoose(messages);
|
|
872
|
-
case 11:
|
|
873
|
-
if ((_step = _iterator()).done) {
|
|
874
|
-
_context11.next = 15;
|
|
875
|
-
break;
|
|
876
|
-
}
|
|
877
|
-
return _context11.delegateYield(_loop(), "t0", 13);
|
|
878
|
-
case 13:
|
|
879
|
-
_context11.next = 11;
|
|
880
|
-
break;
|
|
881
|
-
case 15:
|
|
882
|
-
_context11.next = 17;
|
|
883
|
-
return client.query('COMMIT');
|
|
884
|
-
case 17:
|
|
885
|
-
return _context11.abrupt("return", messages);
|
|
886
|
-
case 20:
|
|
887
|
-
_context11.prev = 20;
|
|
888
|
-
_context11.t1 = _context11["catch"](6);
|
|
889
|
-
_context11.next = 24;
|
|
890
|
-
return client.query('ROLLBACK');
|
|
891
|
-
case 24:
|
|
892
|
-
throw _context11.t1;
|
|
893
|
-
case 25:
|
|
894
|
-
_context11.prev = 25;
|
|
895
|
-
client.release();
|
|
896
|
-
return _context11.finish(25);
|
|
897
|
-
case 28:
|
|
898
|
-
case "end":
|
|
899
|
-
return _context11.stop();
|
|
900
|
-
}
|
|
901
|
-
}, _callee10, this, [[6, 20, 25, 28]]);
|
|
902
|
-
}));
|
|
903
|
-
function saveMessages(_x12) {
|
|
904
|
-
return _saveMessages.apply(this, arguments);
|
|
905
|
-
}
|
|
906
|
-
return saveMessages;
|
|
907
|
-
}();
|
|
908
|
-
_proto.deleteMessage = /*#__PURE__*/function () {
|
|
909
|
-
var _deleteMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(id) {
|
|
910
|
-
var client;
|
|
911
|
-
return _regeneratorRuntime().wrap(function _callee11$(_context12) {
|
|
912
|
-
while (1) switch (_context12.prev = _context12.next) {
|
|
913
|
-
case 0:
|
|
914
|
-
_context12.next = 2;
|
|
915
|
-
return this.pool.connect();
|
|
916
|
-
case 2:
|
|
917
|
-
client = _context12.sent;
|
|
918
|
-
_context12.prev = 3;
|
|
919
|
-
_context12.next = 6;
|
|
920
|
-
return client.query("\n DELETE FROM mastra_messages\n WHERE id = $1\n ", [id]);
|
|
921
|
-
case 6:
|
|
922
|
-
_context12.prev = 6;
|
|
923
|
-
client.release();
|
|
924
|
-
return _context12.finish(6);
|
|
925
|
-
case 9:
|
|
926
|
-
case "end":
|
|
927
|
-
return _context12.stop();
|
|
928
|
-
}
|
|
929
|
-
}, _callee11, this, [[3,, 6, 9]]);
|
|
930
|
-
}));
|
|
931
|
-
function deleteMessage(_x13) {
|
|
932
|
-
return _deleteMessage.apply(this, arguments);
|
|
933
|
-
}
|
|
934
|
-
return deleteMessage;
|
|
935
|
-
}()
|
|
936
|
-
/**
|
|
937
|
-
* Table Management
|
|
938
|
-
*/
|
|
939
|
-
;
|
|
940
|
-
_proto.drop =
|
|
941
|
-
/*#__PURE__*/
|
|
942
|
-
function () {
|
|
943
|
-
var _drop = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
|
|
944
|
-
var client;
|
|
945
|
-
return _regeneratorRuntime().wrap(function _callee12$(_context13) {
|
|
946
|
-
while (1) switch (_context13.prev = _context13.next) {
|
|
947
|
-
case 0:
|
|
948
|
-
_context13.next = 2;
|
|
949
|
-
return this.pool.connect();
|
|
950
|
-
case 2:
|
|
951
|
-
client = _context13.sent;
|
|
952
|
-
_context13.next = 5;
|
|
953
|
-
return client.query('DELETE FROM mastra_messages');
|
|
954
|
-
case 5:
|
|
955
|
-
_context13.next = 7;
|
|
956
|
-
return client.query('DELETE FROM mastra_threads');
|
|
957
|
-
case 7:
|
|
958
|
-
client.release();
|
|
959
|
-
_context13.next = 10;
|
|
960
|
-
return this.pool.end();
|
|
961
|
-
case 10:
|
|
962
|
-
case "end":
|
|
963
|
-
return _context13.stop();
|
|
964
|
-
}
|
|
965
|
-
}, _callee12, this);
|
|
966
|
-
}));
|
|
967
|
-
function drop() {
|
|
968
|
-
return _drop.apply(this, arguments);
|
|
969
|
-
}
|
|
970
|
-
return drop;
|
|
971
|
-
}();
|
|
972
|
-
_proto.ensureTablesExist = /*#__PURE__*/function () {
|
|
973
|
-
var _ensureTablesExist = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
|
|
974
|
-
var client, _threadsResult$rows, _messagesResult$rows, threadsResult, messagesResult;
|
|
975
|
-
return _regeneratorRuntime().wrap(function _callee13$(_context14) {
|
|
976
|
-
while (1) switch (_context14.prev = _context14.next) {
|
|
977
|
-
case 0:
|
|
978
|
-
if (!this.hasTables) {
|
|
979
|
-
_context14.next = 2;
|
|
980
|
-
break;
|
|
981
|
-
}
|
|
982
|
-
return _context14.abrupt("return");
|
|
983
|
-
case 2:
|
|
984
|
-
_context14.next = 4;
|
|
985
|
-
return this.pool.connect();
|
|
986
|
-
case 4:
|
|
987
|
-
client = _context14.sent;
|
|
988
|
-
_context14.prev = 5;
|
|
989
|
-
_context14.next = 8;
|
|
990
|
-
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_threads'\n );\n ");
|
|
991
|
-
case 8:
|
|
992
|
-
threadsResult = _context14.sent;
|
|
993
|
-
if (threadsResult != null && (_threadsResult$rows = threadsResult.rows) != null && (_threadsResult$rows = _threadsResult$rows[0]) != null && _threadsResult$rows.exists) {
|
|
994
|
-
_context14.next = 12;
|
|
995
|
-
break;
|
|
996
|
-
}
|
|
997
|
-
_context14.next = 12;
|
|
998
|
-
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 ");
|
|
999
|
-
case 12:
|
|
1000
|
-
_context14.next = 14;
|
|
1001
|
-
return client.query("\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_name = 'mastra_messages'\n );\n ");
|
|
1002
|
-
case 14:
|
|
1003
|
-
messagesResult = _context14.sent;
|
|
1004
|
-
if (messagesResult != null && (_messagesResult$rows = messagesResult.rows) != null && (_messagesResult$rows = _messagesResult$rows[0]) != null && _messagesResult$rows.exists) {
|
|
1005
|
-
_context14.next = 18;
|
|
1006
|
-
break;
|
|
1007
|
-
}
|
|
1008
|
-
_context14.next = 18;
|
|
1009
|
-
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 ");
|
|
1010
|
-
case 18:
|
|
1011
|
-
_context14.prev = 18;
|
|
1012
|
-
client.release();
|
|
1013
|
-
this.hasTables = true;
|
|
1014
|
-
return _context14.finish(18);
|
|
1015
|
-
case 22:
|
|
1016
|
-
case "end":
|
|
1017
|
-
return _context14.stop();
|
|
1018
|
-
}
|
|
1019
|
-
}, _callee13, this, [[5,, 18, 22]]);
|
|
1020
|
-
}));
|
|
1021
|
-
function ensureTablesExist() {
|
|
1022
|
-
return _ensureTablesExist.apply(this, arguments);
|
|
1023
|
-
}
|
|
1024
|
-
return ensureTablesExist;
|
|
1025
|
-
}();
|
|
1026
|
-
return PgMemory;
|
|
1027
|
-
}(core.MastraMemory);
|
|
1028
|
-
|
|
1029
|
-
var UpstashKVMemory = /*#__PURE__*/function (_MastraMemory) {
|
|
1030
|
-
function UpstashKVMemory(config) {
|
|
1031
|
-
var _this;
|
|
1032
|
-
_this = _MastraMemory.call(this) || this;
|
|
1033
|
-
_this.prefix = void 0;
|
|
1034
|
-
_this.kv = void 0;
|
|
1035
|
-
_this.prefix = config.prefix || 'mastra';
|
|
1036
|
-
_this.MAX_CONTEXT_TOKENS = config.maxTokens;
|
|
1037
|
-
_this.kv = new redis.Redis({
|
|
1038
|
-
url: config.url,
|
|
1039
|
-
token: config.token
|
|
1040
|
-
});
|
|
1041
|
-
return _this;
|
|
1042
|
-
}
|
|
1043
|
-
_inheritsLoose(UpstashKVMemory, _MastraMemory);
|
|
1044
|
-
var _proto = UpstashKVMemory.prototype;
|
|
1045
|
-
_proto.getThreadKey = function getThreadKey(threadId) {
|
|
1046
|
-
return this.prefix + ":thread:" + threadId;
|
|
1047
|
-
};
|
|
1048
|
-
_proto.getMessagesKey = function getMessagesKey(threadId) {
|
|
1049
|
-
return this.prefix + ":messages:" + threadId;
|
|
1050
|
-
};
|
|
1051
|
-
_proto.getToolCacheKey = function getToolCacheKey(hashedArgs) {
|
|
1052
|
-
return this.prefix + ":tool:" + hashedArgs;
|
|
1053
|
-
};
|
|
1054
|
-
_proto.getThreadById = /*#__PURE__*/function () {
|
|
1055
|
-
var _getThreadById = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
1056
|
-
var threadId, thread;
|
|
1057
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1058
|
-
while (1) switch (_context.prev = _context.next) {
|
|
1059
|
-
case 0:
|
|
1060
|
-
threadId = _ref.threadId;
|
|
1061
|
-
_context.next = 3;
|
|
1062
|
-
return this.kv.get(this.getThreadKey(threadId));
|
|
1063
|
-
case 3:
|
|
1064
|
-
thread = _context.sent;
|
|
1065
|
-
return _context.abrupt("return", thread ? this.parseThread(thread) : null);
|
|
1066
|
-
case 5:
|
|
1067
|
-
case "end":
|
|
1068
|
-
return _context.stop();
|
|
1069
|
-
}
|
|
1070
|
-
}, _callee, this);
|
|
1071
|
-
}));
|
|
1072
|
-
function getThreadById(_x) {
|
|
1073
|
-
return _getThreadById.apply(this, arguments);
|
|
1074
|
-
}
|
|
1075
|
-
return getThreadById;
|
|
1076
|
-
}();
|
|
1077
|
-
_proto.getThreadsByResourceId = /*#__PURE__*/function () {
|
|
1078
|
-
var _getThreadsByResourceId = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref2) {
|
|
1079
|
-
var _this2 = this;
|
|
1080
|
-
var resourceid, pattern, keys, threads;
|
|
1081
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
1082
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
1083
|
-
case 0:
|
|
1084
|
-
resourceid = _ref2.resourceid;
|
|
1085
|
-
pattern = this.prefix + ":thread:*";
|
|
1086
|
-
_context2.next = 4;
|
|
1087
|
-
return this.kv.keys(pattern);
|
|
1088
|
-
case 4:
|
|
1089
|
-
keys = _context2.sent;
|
|
1090
|
-
_context2.next = 7;
|
|
1091
|
-
return Promise.all(keys.map(function (key) {
|
|
1092
|
-
return _this2.kv.get(key);
|
|
1093
|
-
}));
|
|
1094
|
-
case 7:
|
|
1095
|
-
threads = _context2.sent;
|
|
1096
|
-
return _context2.abrupt("return", threads.filter(function (thread) {
|
|
1097
|
-
return (thread == null ? void 0 : thread.resourceid) === resourceid;
|
|
1098
|
-
}).map(function (thread) {
|
|
1099
|
-
return _this2.parseThread(thread);
|
|
1100
|
-
}));
|
|
1101
|
-
case 9:
|
|
1102
|
-
case "end":
|
|
1103
|
-
return _context2.stop();
|
|
1104
|
-
}
|
|
1105
|
-
}, _callee2, this);
|
|
1106
|
-
}));
|
|
1107
|
-
function getThreadsByResourceId(_x2) {
|
|
1108
|
-
return _getThreadsByResourceId.apply(this, arguments);
|
|
1109
|
-
}
|
|
1110
|
-
return getThreadsByResourceId;
|
|
1111
|
-
}();
|
|
1112
|
-
_proto.saveThread = /*#__PURE__*/function () {
|
|
1113
|
-
var _saveThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref3) {
|
|
1114
|
-
var thread, key, serializedThread;
|
|
1115
|
-
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
1116
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
1117
|
-
case 0:
|
|
1118
|
-
thread = _ref3.thread;
|
|
1119
|
-
key = this.getThreadKey(thread.id);
|
|
1120
|
-
serializedThread = _extends({}, thread, {
|
|
1121
|
-
createdAt: thread.createdAt.toISOString(),
|
|
1122
|
-
updatedAt: thread.updatedAt.toISOString()
|
|
1123
|
-
});
|
|
1124
|
-
_context3.next = 5;
|
|
1125
|
-
return this.kv.set(key, serializedThread);
|
|
1126
|
-
case 5:
|
|
1127
|
-
return _context3.abrupt("return", thread);
|
|
1128
|
-
case 6:
|
|
1129
|
-
case "end":
|
|
1130
|
-
return _context3.stop();
|
|
1131
|
-
}
|
|
1132
|
-
}, _callee3, this);
|
|
1133
|
-
}));
|
|
1134
|
-
function saveThread(_x3) {
|
|
1135
|
-
return _saveThread.apply(this, arguments);
|
|
1136
|
-
}
|
|
1137
|
-
return saveThread;
|
|
1138
|
-
}();
|
|
1139
|
-
_proto.updateThread = /*#__PURE__*/function () {
|
|
1140
|
-
var _updateThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(id, title, metadata) {
|
|
1141
|
-
var key, thread, updatedThread;
|
|
1142
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
1143
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
1144
|
-
case 0:
|
|
1145
|
-
key = this.getThreadKey(id);
|
|
1146
|
-
_context4.next = 3;
|
|
1147
|
-
return this.kv.get(key);
|
|
1148
|
-
case 3:
|
|
1149
|
-
thread = _context4.sent;
|
|
1150
|
-
if (thread) {
|
|
1151
|
-
_context4.next = 6;
|
|
1152
|
-
break;
|
|
1153
|
-
}
|
|
1154
|
-
throw new Error("Thread " + id + " not found");
|
|
1155
|
-
case 6:
|
|
1156
|
-
updatedThread = _extends({}, thread, {
|
|
1157
|
-
title: title,
|
|
1158
|
-
metadata: metadata,
|
|
1159
|
-
updatedAt: new Date().toISOString()
|
|
1160
|
-
});
|
|
1161
|
-
_context4.next = 9;
|
|
1162
|
-
return this.kv.set(key, updatedThread);
|
|
1163
|
-
case 9:
|
|
1164
|
-
return _context4.abrupt("return", this.parseThread(updatedThread));
|
|
1165
|
-
case 10:
|
|
1166
|
-
case "end":
|
|
1167
|
-
return _context4.stop();
|
|
1168
|
-
}
|
|
1169
|
-
}, _callee4, this);
|
|
1170
|
-
}));
|
|
1171
|
-
function updateThread(_x4, _x5, _x6) {
|
|
1172
|
-
return _updateThread.apply(this, arguments);
|
|
1173
|
-
}
|
|
1174
|
-
return updateThread;
|
|
1175
|
-
}();
|
|
1176
|
-
_proto.deleteThread = /*#__PURE__*/function () {
|
|
1177
|
-
var _deleteThread = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(id) {
|
|
1178
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
1179
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
1180
|
-
case 0:
|
|
1181
|
-
_context5.next = 2;
|
|
1182
|
-
return this.kv.del(this.getThreadKey(id));
|
|
1183
|
-
case 2:
|
|
1184
|
-
_context5.next = 4;
|
|
1185
|
-
return this.kv.del(this.getMessagesKey(id));
|
|
1186
|
-
case 4:
|
|
1187
|
-
case "end":
|
|
1188
|
-
return _context5.stop();
|
|
1189
|
-
}
|
|
1190
|
-
}, _callee5, this);
|
|
1191
|
-
}));
|
|
1192
|
-
function deleteThread(_x7) {
|
|
1193
|
-
return _deleteThread.apply(this, arguments);
|
|
1194
|
-
}
|
|
1195
|
-
return deleteThread;
|
|
1196
|
-
}()
|
|
1197
|
-
/**
|
|
1198
|
-
* Tool Cache
|
|
1199
|
-
*/
|
|
1200
|
-
;
|
|
1201
|
-
_proto.validateToolCallArgs =
|
|
1202
|
-
/*#__PURE__*/
|
|
1203
|
-
function () {
|
|
1204
|
-
var _validateToolCallArgs = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref4) {
|
|
1205
|
-
var hashedArgs, cacheKey, cached;
|
|
1206
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
1207
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
1208
|
-
case 0:
|
|
1209
|
-
hashedArgs = _ref4.hashedArgs;
|
|
1210
|
-
cacheKey = this.getToolCacheKey(hashedArgs);
|
|
1211
|
-
_context6.next = 4;
|
|
1212
|
-
return this.kv.get(cacheKey);
|
|
1213
|
-
case 4:
|
|
1214
|
-
cached = _context6.sent;
|
|
1215
|
-
return _context6.abrupt("return", !!cached && new Date(cached.expireAt) > new Date());
|
|
1216
|
-
case 6:
|
|
1217
|
-
case "end":
|
|
1218
|
-
return _context6.stop();
|
|
1219
|
-
}
|
|
1220
|
-
}, _callee6, this);
|
|
1221
|
-
}));
|
|
1222
|
-
function validateToolCallArgs(_x8) {
|
|
1223
|
-
return _validateToolCallArgs.apply(this, arguments);
|
|
1224
|
-
}
|
|
1225
|
-
return validateToolCallArgs;
|
|
1226
|
-
}();
|
|
1227
|
-
_proto.getToolResult = /*#__PURE__*/function () {
|
|
1228
|
-
var _getToolResult = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref5) {
|
|
1229
|
-
var threadId, toolArgs, toolName, hashedToolArgs, cacheKey, cached;
|
|
1230
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
1231
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
1232
|
-
case 0:
|
|
1233
|
-
threadId = _ref5.threadId, toolArgs = _ref5.toolArgs, toolName = _ref5.toolName;
|
|
1234
|
-
hashedToolArgs = crypto.createHash('sha256').update(JSON.stringify({
|
|
1235
|
-
args: toolArgs,
|
|
1236
|
-
threadId: threadId,
|
|
1237
|
-
toolName: toolName
|
|
1238
|
-
})).digest('hex');
|
|
1239
|
-
cacheKey = this.getToolCacheKey(hashedToolArgs);
|
|
1240
|
-
_context7.next = 5;
|
|
1241
|
-
return this.kv.get(cacheKey);
|
|
1242
|
-
case 5:
|
|
1243
|
-
cached = _context7.sent;
|
|
1244
|
-
if (!(cached && new Date(cached.expireAt) > new Date())) {
|
|
1245
|
-
_context7.next = 8;
|
|
1246
|
-
break;
|
|
1247
|
-
}
|
|
1248
|
-
return _context7.abrupt("return", cached.result || null);
|
|
1249
|
-
case 8:
|
|
1250
|
-
return _context7.abrupt("return", null);
|
|
1251
|
-
case 9:
|
|
1252
|
-
case "end":
|
|
1253
|
-
return _context7.stop();
|
|
1254
|
-
}
|
|
1255
|
-
}, _callee7, this);
|
|
1256
|
-
}));
|
|
1257
|
-
function getToolResult(_x9) {
|
|
1258
|
-
return _getToolResult.apply(this, arguments);
|
|
1259
|
-
}
|
|
1260
|
-
return getToolResult;
|
|
1261
|
-
}();
|
|
1262
|
-
_proto.getContextWindow = /*#__PURE__*/function () {
|
|
1263
|
-
var _getContextWindow = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref6) {
|
|
1264
|
-
var threadId, startDate, endDate, _ref6$format, format, messagesKey, messages, filteredMessages, totalTokens, messagesWithinTokenLimit, _iterator, _step, _message$content$, message, content, tokens;
|
|
1265
|
-
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
1266
|
-
while (1) switch (_context8.prev = _context8.next) {
|
|
1267
|
-
case 0:
|
|
1268
|
-
threadId = _ref6.threadId, startDate = _ref6.startDate, endDate = _ref6.endDate, _ref6$format = _ref6.format, format = _ref6$format === void 0 ? 'raw' : _ref6$format;
|
|
1269
|
-
messagesKey = this.getMessagesKey(threadId);
|
|
1270
|
-
_context8.next = 4;
|
|
1271
|
-
return this.kv.lrange(messagesKey, 0, -1);
|
|
1272
|
-
case 4:
|
|
1273
|
-
messages = _context8.sent;
|
|
1274
|
-
filteredMessages = messages.filter(function (msg) {
|
|
1275
|
-
return msg.type === 'text';
|
|
1276
|
-
});
|
|
1277
|
-
if (startDate) {
|
|
1278
|
-
filteredMessages = filteredMessages.filter(function (msg) {
|
|
1279
|
-
return new Date(msg.createdAt) >= startDate;
|
|
1280
|
-
});
|
|
1281
|
-
}
|
|
1282
|
-
if (endDate) {
|
|
1283
|
-
filteredMessages = filteredMessages.filter(function (msg) {
|
|
1284
|
-
return new Date(msg.createdAt) <= endDate;
|
|
1285
|
-
});
|
|
1286
|
-
}
|
|
1287
|
-
if (!this.MAX_CONTEXT_TOKENS) {
|
|
1288
|
-
_context8.next = 24;
|
|
1289
|
-
break;
|
|
1290
|
-
}
|
|
1291
|
-
totalTokens = 0;
|
|
1292
|
-
messagesWithinTokenLimit = []; // Process messages from newest to oldest
|
|
1293
|
-
_iterator = _createForOfIteratorHelperLoose(filteredMessages.reverse());
|
|
1294
|
-
case 12:
|
|
1295
|
-
if ((_step = _iterator()).done) {
|
|
1296
|
-
_context8.next = 22;
|
|
1297
|
-
break;
|
|
1298
|
-
}
|
|
1299
|
-
message = _step.value;
|
|
1300
|
-
content = message.role === 'assistant' ? ((_message$content$ = message.content[0]) == null ? void 0 : _message$content$.text) || '' : message.content; // Use a more aggressive token estimation
|
|
1301
|
-
// Roughly estimate 1 token per 4 characters
|
|
1302
|
-
tokens = Math.ceil(content.length / 4); // Check if adding this message would exceed the token limit
|
|
1303
|
-
if (!(totalTokens + tokens > this.MAX_CONTEXT_TOKENS)) {
|
|
1304
|
-
_context8.next = 18;
|
|
1305
|
-
break;
|
|
1306
|
-
}
|
|
1307
|
-
return _context8.abrupt("break", 22);
|
|
1308
|
-
case 18:
|
|
1309
|
-
totalTokens += tokens;
|
|
1310
|
-
messagesWithinTokenLimit.unshift(_extends({}, message, {
|
|
1311
|
-
tokens: tokens
|
|
1312
|
-
}));
|
|
1313
|
-
case 20:
|
|
1314
|
-
_context8.next = 12;
|
|
1315
|
-
break;
|
|
1316
|
-
case 22:
|
|
1317
|
-
console.log('Format:', format);
|
|
1318
|
-
// Return messages in chronological order
|
|
1319
|
-
return _context8.abrupt("return", this.parseMessages(messagesWithinTokenLimit));
|
|
1320
|
-
case 24:
|
|
1321
|
-
return _context8.abrupt("return", this.parseMessages(filteredMessages));
|
|
1322
|
-
case 25:
|
|
1323
|
-
case "end":
|
|
1324
|
-
return _context8.stop();
|
|
1325
|
-
}
|
|
1326
|
-
}, _callee8, this);
|
|
1327
|
-
}));
|
|
1328
|
-
function getContextWindow(_x10) {
|
|
1329
|
-
return _getContextWindow.apply(this, arguments);
|
|
1330
|
-
}
|
|
1331
|
-
return getContextWindow;
|
|
1332
|
-
}()
|
|
1333
|
-
/**
|
|
1334
|
-
* Messages
|
|
1335
|
-
*/
|
|
1336
|
-
;
|
|
1337
|
-
_proto.getMessages =
|
|
1338
|
-
/*#__PURE__*/
|
|
1339
|
-
function () {
|
|
1340
|
-
var _getMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_ref7) {
|
|
1341
|
-
var threadId, messagesKey, messages, parsedMessages, uiMessages;
|
|
1342
|
-
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
1343
|
-
while (1) switch (_context9.prev = _context9.next) {
|
|
1344
|
-
case 0:
|
|
1345
|
-
threadId = _ref7.threadId;
|
|
1346
|
-
messagesKey = this.getMessagesKey(threadId);
|
|
1347
|
-
_context9.next = 4;
|
|
1348
|
-
return this.kv.lrange(messagesKey, 0, -1);
|
|
1349
|
-
case 4:
|
|
1350
|
-
messages = _context9.sent;
|
|
1351
|
-
parsedMessages = this.parseMessages(messages);
|
|
1352
|
-
uiMessages = this.convertToUIMessages(parsedMessages);
|
|
1353
|
-
return _context9.abrupt("return", {
|
|
1354
|
-
messages: parsedMessages,
|
|
1355
|
-
uiMessages: uiMessages
|
|
1356
|
-
});
|
|
1357
|
-
case 8:
|
|
1358
|
-
case "end":
|
|
1359
|
-
return _context9.stop();
|
|
1360
|
-
}
|
|
1361
|
-
}, _callee9, this);
|
|
1362
|
-
}));
|
|
1363
|
-
function getMessages(_x11) {
|
|
1364
|
-
return _getMessages.apply(this, arguments);
|
|
1365
|
-
}
|
|
1366
|
-
return getMessages;
|
|
1367
|
-
}();
|
|
1368
|
-
_proto.saveMessages = /*#__PURE__*/function () {
|
|
1369
|
-
var _saveMessages = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(_ref8) {
|
|
1370
|
-
var _this3 = this;
|
|
1371
|
-
var messages, processedMessages, _loop, _iterator2, _step2;
|
|
1372
|
-
return _regeneratorRuntime().wrap(function _callee10$(_context11) {
|
|
1373
|
-
while (1) switch (_context11.prev = _context11.next) {
|
|
1374
|
-
case 0:
|
|
1375
|
-
messages = _ref8.messages;
|
|
1376
|
-
processedMessages = [];
|
|
1377
|
-
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
1378
|
-
var message, threadId, toolCallArgs, toolNames, createdAt, messagesKey, processedMessage, _message$content$2, content, hashedToolCallArgs, validArgExists, _iterator3, _step3, hashedArg, isValid, expireAt, _iterator4, _step4, _hashedArg, cacheKey;
|
|
1379
|
-
return _regeneratorRuntime().wrap(function _loop$(_context10) {
|
|
1380
|
-
while (1) switch (_context10.prev = _context10.next) {
|
|
1381
|
-
case 0:
|
|
1382
|
-
message = _step2.value;
|
|
1383
|
-
threadId = message.threadId, toolCallArgs = message.toolCallArgs, toolNames = message.toolNames, createdAt = message.createdAt;
|
|
1384
|
-
messagesKey = _this3.getMessagesKey(threadId);
|
|
1385
|
-
processedMessage = _extends({}, message);
|
|
1386
|
-
if (message.type === 'text') {
|
|
1387
|
-
content = message.role === 'assistant' ? ((_message$content$2 = message.content[0]) == null ? void 0 : _message$content$2.text) || '' : message.content;
|
|
1388
|
-
processedMessage.tokens = _this3.estimateTokens(content);
|
|
1389
|
-
}
|
|
1390
|
-
if (!(toolCallArgs != null && toolCallArgs.length)) {
|
|
1391
|
-
_context10.next = 28;
|
|
1392
|
-
break;
|
|
1393
|
-
}
|
|
1394
|
-
hashedToolCallArgs = toolCallArgs.map(function (args, index) {
|
|
1395
|
-
return crypto.createHash('sha256').update(JSON.stringify({
|
|
1396
|
-
args: args,
|
|
1397
|
-
threadId: threadId,
|
|
1398
|
-
toolName: toolNames == null ? void 0 : toolNames[index]
|
|
1399
|
-
})).digest('hex');
|
|
1400
|
-
});
|
|
1401
|
-
validArgExists = true;
|
|
1402
|
-
_iterator3 = _createForOfIteratorHelperLoose(hashedToolCallArgs);
|
|
1403
|
-
case 9:
|
|
1404
|
-
if ((_step3 = _iterator3()).done) {
|
|
1405
|
-
_context10.next = 19;
|
|
1406
|
-
break;
|
|
1407
|
-
}
|
|
1408
|
-
hashedArg = _step3.value;
|
|
1409
|
-
_context10.next = 13;
|
|
1410
|
-
return _this3.validateToolCallArgs({
|
|
1411
|
-
hashedArgs: hashedArg
|
|
1412
|
-
});
|
|
1413
|
-
case 13:
|
|
1414
|
-
isValid = _context10.sent;
|
|
1415
|
-
if (isValid) {
|
|
1416
|
-
_context10.next = 17;
|
|
1417
|
-
break;
|
|
1418
|
-
}
|
|
1419
|
-
validArgExists = false;
|
|
1420
|
-
return _context10.abrupt("break", 19);
|
|
1421
|
-
case 17:
|
|
1422
|
-
_context10.next = 9;
|
|
1423
|
-
break;
|
|
1424
|
-
case 19:
|
|
1425
|
-
expireAt = validArgExists ? createdAt : new Date(createdAt.getTime() + 5 * 60 * 1000); // 5 minutes
|
|
1426
|
-
_iterator4 = _createForOfIteratorHelperLoose(hashedToolCallArgs);
|
|
1427
|
-
case 21:
|
|
1428
|
-
if ((_step4 = _iterator4()).done) {
|
|
1429
|
-
_context10.next = 28;
|
|
1430
|
-
break;
|
|
1431
|
-
}
|
|
1432
|
-
_hashedArg = _step4.value;
|
|
1433
|
-
cacheKey = _this3.getToolCacheKey(_hashedArg);
|
|
1434
|
-
_context10.next = 26;
|
|
1435
|
-
return _this3.kv.set(cacheKey, {
|
|
1436
|
-
expireAt: expireAt.toISOString()
|
|
1437
|
-
});
|
|
1438
|
-
case 26:
|
|
1439
|
-
_context10.next = 21;
|
|
1440
|
-
break;
|
|
1441
|
-
case 28:
|
|
1442
|
-
_context10.next = 30;
|
|
1443
|
-
return _this3.kv.rpush(messagesKey, processedMessage);
|
|
1444
|
-
case 30:
|
|
1445
|
-
processedMessages.push(processedMessage);
|
|
1446
|
-
case 31:
|
|
1447
|
-
case "end":
|
|
1448
|
-
return _context10.stop();
|
|
1449
|
-
}
|
|
1450
|
-
}, _loop);
|
|
1451
|
-
});
|
|
1452
|
-
_iterator2 = _createForOfIteratorHelperLoose(messages);
|
|
1453
|
-
case 4:
|
|
1454
|
-
if ((_step2 = _iterator2()).done) {
|
|
1455
|
-
_context11.next = 8;
|
|
1456
|
-
break;
|
|
1457
|
-
}
|
|
1458
|
-
return _context11.delegateYield(_loop(), "t0", 6);
|
|
1459
|
-
case 6:
|
|
1460
|
-
_context11.next = 4;
|
|
1461
|
-
break;
|
|
1462
|
-
case 8:
|
|
1463
|
-
return _context11.abrupt("return", processedMessages);
|
|
1464
|
-
case 9:
|
|
1465
|
-
case "end":
|
|
1466
|
-
return _context11.stop();
|
|
1467
|
-
}
|
|
1468
|
-
}, _callee10);
|
|
1469
|
-
}));
|
|
1470
|
-
function saveMessages(_x12) {
|
|
1471
|
-
return _saveMessages.apply(this, arguments);
|
|
1472
|
-
}
|
|
1473
|
-
return saveMessages;
|
|
1474
|
-
}();
|
|
1475
|
-
_proto.deleteMessage = /*#__PURE__*/function () {
|
|
1476
|
-
var _deleteMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(id) {
|
|
1477
|
-
var pattern, keys, _iterator5, _step5, key, messages, filteredMessages, _this$kv;
|
|
1478
|
-
return _regeneratorRuntime().wrap(function _callee11$(_context12) {
|
|
1479
|
-
while (1) switch (_context12.prev = _context12.next) {
|
|
1480
|
-
case 0:
|
|
1481
|
-
pattern = this.prefix + ":messages:*";
|
|
1482
|
-
_context12.next = 3;
|
|
1483
|
-
return this.kv.keys(pattern);
|
|
1484
|
-
case 3:
|
|
1485
|
-
keys = _context12.sent;
|
|
1486
|
-
_iterator5 = _createForOfIteratorHelperLoose(keys);
|
|
1487
|
-
case 5:
|
|
1488
|
-
if ((_step5 = _iterator5()).done) {
|
|
1489
|
-
_context12.next = 19;
|
|
1490
|
-
break;
|
|
1491
|
-
}
|
|
1492
|
-
key = _step5.value;
|
|
1493
|
-
_context12.next = 9;
|
|
1494
|
-
return this.kv.lrange(key, 0, -1);
|
|
1495
|
-
case 9:
|
|
1496
|
-
messages = _context12.sent;
|
|
1497
|
-
filteredMessages = messages.filter(function (msg) {
|
|
1498
|
-
return msg.id !== id;
|
|
1499
|
-
});
|
|
1500
|
-
if (!(messages.length !== filteredMessages.length)) {
|
|
1501
|
-
_context12.next = 17;
|
|
1502
|
-
break;
|
|
1503
|
-
}
|
|
1504
|
-
_context12.next = 14;
|
|
1505
|
-
return this.kv.del(key);
|
|
1506
|
-
case 14:
|
|
1507
|
-
if (!(filteredMessages.length > 0)) {
|
|
1508
|
-
_context12.next = 17;
|
|
1509
|
-
break;
|
|
1510
|
-
}
|
|
1511
|
-
_context12.next = 17;
|
|
1512
|
-
return (_this$kv = this.kv).rpush.apply(_this$kv, [key].concat(filteredMessages));
|
|
1513
|
-
case 17:
|
|
1514
|
-
_context12.next = 5;
|
|
1515
|
-
break;
|
|
1516
|
-
case 19:
|
|
1517
|
-
case "end":
|
|
1518
|
-
return _context12.stop();
|
|
1519
|
-
}
|
|
1520
|
-
}, _callee11, this);
|
|
1521
|
-
}));
|
|
1522
|
-
function deleteMessage(_x13) {
|
|
1523
|
-
return _deleteMessage.apply(this, arguments);
|
|
1524
|
-
}
|
|
1525
|
-
return deleteMessage;
|
|
1526
|
-
}()
|
|
1527
|
-
/**
|
|
1528
|
-
* Cleanup
|
|
1529
|
-
*/
|
|
1530
|
-
;
|
|
1531
|
-
_proto.drop =
|
|
1532
|
-
/*#__PURE__*/
|
|
1533
|
-
function () {
|
|
1534
|
-
var _drop = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
|
|
1535
|
-
var pattern, keys, _this$kv2;
|
|
1536
|
-
return _regeneratorRuntime().wrap(function _callee12$(_context13) {
|
|
1537
|
-
while (1) switch (_context13.prev = _context13.next) {
|
|
1538
|
-
case 0:
|
|
1539
|
-
pattern = this.prefix + ":*";
|
|
1540
|
-
_context13.next = 3;
|
|
1541
|
-
return this.kv.keys(pattern);
|
|
1542
|
-
case 3:
|
|
1543
|
-
keys = _context13.sent;
|
|
1544
|
-
if (!(keys.length > 0)) {
|
|
1545
|
-
_context13.next = 7;
|
|
1546
|
-
break;
|
|
1547
|
-
}
|
|
1548
|
-
_context13.next = 7;
|
|
1549
|
-
return (_this$kv2 = this.kv).del.apply(_this$kv2, keys);
|
|
1550
|
-
case 7:
|
|
1551
|
-
case "end":
|
|
1552
|
-
return _context13.stop();
|
|
1553
|
-
}
|
|
1554
|
-
}, _callee12, this);
|
|
1555
|
-
}));
|
|
1556
|
-
function drop() {
|
|
1557
|
-
return _drop.apply(this, arguments);
|
|
1558
|
-
}
|
|
1559
|
-
return drop;
|
|
1560
|
-
}();
|
|
1561
|
-
_proto.parseThread = function parseThread(thread) {
|
|
1562
|
-
return _extends({}, thread, {
|
|
1563
|
-
createdAt: new Date(thread.createdAt),
|
|
1564
|
-
updatedAt: new Date(thread.updatedAt)
|
|
1565
|
-
});
|
|
1566
|
-
};
|
|
1567
|
-
_proto.parseMessages = function parseMessages(messages) {
|
|
1568
|
-
return messages.map(function (message) {
|
|
1569
|
-
return _extends({}, message, {
|
|
1570
|
-
createdAt: new Date(message.createdAt)
|
|
1571
|
-
});
|
|
1572
|
-
});
|
|
1573
|
-
};
|
|
1574
|
-
return UpstashKVMemory;
|
|
1575
|
-
}(core.MastraMemory);
|
|
1576
|
-
|
|
1577
|
-
exports.PgMemory = PgMemory;
|
|
1578
|
-
exports.UpstashKVMemory = UpstashKVMemory;
|
|
1579
|
-
//# sourceMappingURL=memory.cjs.development.js.map
|