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