@mastra/redis 1.0.1-alpha.0 → 1.0.2-alpha.0

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/dist/index.js ADDED
@@ -0,0 +1,1795 @@
1
+ import { MessageList } from '@mastra/core/agent';
2
+ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
+ import { MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, jsonValueEquals, filterByDateRange, ScoresStorage, TABLE_SCORERS, transformScoreRow, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraStorage, serializeDate } from '@mastra/core/storage';
4
+ import crypto2 from 'crypto';
5
+ import { saveScorePayloadSchema } from '@mastra/core/evals';
6
+ import { createClient } from 'redis';
7
+
8
+ // src/storage/domains/memory/index.ts
9
+ function getKey(tableName, keys) {
10
+ const keyParts = Object.entries(keys).filter(([_, value]) => value !== void 0).map(([key, value]) => {
11
+ if (value && typeof value === "object") {
12
+ return `${key}:${JSON.stringify(value)}`;
13
+ }
14
+ return `${key}:${value}`;
15
+ });
16
+ return `${tableName}:${keyParts.join(":")}`;
17
+ }
18
+ function processRecord(tableName, record) {
19
+ let key;
20
+ if (tableName === TABLE_MESSAGES) {
21
+ key = getKey(tableName, { threadId: record.threadId, id: record.id });
22
+ } else if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
23
+ key = getKey(tableName, {
24
+ namespace: record.namespace || "workflows",
25
+ workflow_name: record.workflow_name,
26
+ run_id: record.run_id,
27
+ ...record.resourceId ? { resourceId: record.resourceId } : {}
28
+ });
29
+ } else {
30
+ key = getKey(tableName, { id: record.id });
31
+ }
32
+ const processedRecord = {
33
+ ...record,
34
+ createdAt: serializeDate(record.createdAt),
35
+ updatedAt: serializeDate(record.updatedAt)
36
+ };
37
+ return { key, processedRecord };
38
+ }
39
+
40
+ // src/storage/db/index.ts
41
+ var RedisDB = class {
42
+ client;
43
+ constructor({ client }) {
44
+ this.client = client;
45
+ }
46
+ getClient() {
47
+ return this.client;
48
+ }
49
+ async insert({ tableName, record }) {
50
+ const { key, processedRecord } = processRecord(tableName, record);
51
+ try {
52
+ await this.client.set(key, JSON.stringify(processedRecord));
53
+ } catch (error) {
54
+ throw new MastraError(
55
+ {
56
+ id: createStorageErrorId("REDIS", "INSERT", "FAILED"),
57
+ domain: ErrorDomain.STORAGE,
58
+ category: ErrorCategory.THIRD_PARTY,
59
+ details: {
60
+ tableName
61
+ }
62
+ },
63
+ error
64
+ );
65
+ }
66
+ }
67
+ async get({ tableName, keys }) {
68
+ const key = getKey(tableName, keys);
69
+ try {
70
+ const data = await this.client.get(key);
71
+ if (!data) {
72
+ return null;
73
+ }
74
+ return JSON.parse(data);
75
+ } catch (error) {
76
+ throw new MastraError(
77
+ {
78
+ id: createStorageErrorId("REDIS", "LOAD", "FAILED"),
79
+ domain: ErrorDomain.STORAGE,
80
+ category: ErrorCategory.THIRD_PARTY,
81
+ details: {
82
+ tableName
83
+ }
84
+ },
85
+ error
86
+ );
87
+ }
88
+ }
89
+ async scanAndDelete(pattern, batchSize = 1e4) {
90
+ let cursor = "0";
91
+ let totalDeleted = 0;
92
+ do {
93
+ const result = await this.client.scan(cursor, { MATCH: pattern, COUNT: batchSize });
94
+ if (result.keys.length > 0) {
95
+ await this.client.del(result.keys);
96
+ totalDeleted += result.keys.length;
97
+ }
98
+ cursor = result.cursor;
99
+ } while (cursor !== "0");
100
+ return totalDeleted;
101
+ }
102
+ async scanKeys(pattern, batchSize = 1e4) {
103
+ let cursor = "0";
104
+ const keys = [];
105
+ do {
106
+ const result = await this.client.scan(cursor, { MATCH: pattern, COUNT: batchSize });
107
+ keys.push(...result.keys);
108
+ cursor = result.cursor;
109
+ } while (cursor !== "0");
110
+ return keys;
111
+ }
112
+ async deleteData({ tableName }) {
113
+ const pattern = `${tableName}:*`;
114
+ try {
115
+ await this.scanAndDelete(pattern);
116
+ } catch (error) {
117
+ throw new MastraError(
118
+ {
119
+ id: createStorageErrorId("REDIS", "CLEAR_TABLE", "FAILED"),
120
+ domain: ErrorDomain.STORAGE,
121
+ category: ErrorCategory.THIRD_PARTY,
122
+ details: {
123
+ tableName
124
+ }
125
+ },
126
+ error
127
+ );
128
+ }
129
+ }
130
+ };
131
+
132
+ // src/storage/domains/memory/index.ts
133
+ var StoreMemoryRedis = class extends MemoryStorage {
134
+ client;
135
+ db;
136
+ constructor(config) {
137
+ super();
138
+ this.client = config.client;
139
+ this.db = new RedisDB({ client: config.client });
140
+ }
141
+ async dangerouslyClearAll() {
142
+ await this.db.deleteData({ tableName: TABLE_THREADS });
143
+ await this.db.deleteData({ tableName: TABLE_MESSAGES });
144
+ await this.db.deleteData({ tableName: TABLE_RESOURCES });
145
+ await this.db.scanAndDelete("msg-idx:*");
146
+ await this.db.scanAndDelete("thread:*:messages");
147
+ }
148
+ async getThreadById({ threadId }) {
149
+ try {
150
+ const thread = await this.db.get({
151
+ tableName: TABLE_THREADS,
152
+ keys: { id: threadId }
153
+ });
154
+ if (!thread) {
155
+ return null;
156
+ }
157
+ return {
158
+ ...thread,
159
+ createdAt: ensureDate(thread.createdAt),
160
+ updatedAt: ensureDate(thread.updatedAt),
161
+ metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
162
+ };
163
+ } catch (error) {
164
+ throw new MastraError(
165
+ {
166
+ id: createStorageErrorId("REDIS", "GET_THREAD_BY_ID", "FAILED"),
167
+ domain: ErrorDomain.STORAGE,
168
+ category: ErrorCategory.THIRD_PARTY,
169
+ details: {
170
+ threadId
171
+ }
172
+ },
173
+ error
174
+ );
175
+ }
176
+ }
177
+ async listThreadsByResourceId(args) {
178
+ return this.listThreads(args);
179
+ }
180
+ async listThreads(args) {
181
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
182
+ const { field, direction } = this.parseOrderBy(orderBy);
183
+ try {
184
+ this.validatePaginationInput(page, perPageInput ?? 100);
185
+ } catch (error) {
186
+ throw new MastraError(
187
+ {
188
+ id: createStorageErrorId("REDIS", "LIST_THREADS", "INVALID_PAGE"),
189
+ domain: ErrorDomain.STORAGE,
190
+ category: ErrorCategory.USER,
191
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
192
+ },
193
+ error instanceof Error ? error : new Error("Invalid pagination parameters")
194
+ );
195
+ }
196
+ const perPage = normalizePerPage(perPageInput, 100);
197
+ try {
198
+ this.validateMetadataKeys(filter?.metadata);
199
+ } catch (error) {
200
+ throw new MastraError(
201
+ {
202
+ id: createStorageErrorId("REDIS", "LIST_THREADS", "INVALID_METADATA_KEY"),
203
+ domain: ErrorDomain.STORAGE,
204
+ category: ErrorCategory.USER,
205
+ details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
206
+ },
207
+ error instanceof Error ? error : new Error("Invalid metadata key")
208
+ );
209
+ }
210
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
211
+ try {
212
+ let allThreads = [];
213
+ const pattern = `${TABLE_THREADS}:*`;
214
+ const keys = await this.db.scanKeys(pattern);
215
+ if (keys.length === 0) {
216
+ return {
217
+ threads: [],
218
+ total: 0,
219
+ page,
220
+ perPage: perPageForResponse,
221
+ hasMore: false
222
+ };
223
+ }
224
+ const results = await this.client.mGet(keys);
225
+ for (let i = 0; i < results.length; i++) {
226
+ const data = results[i];
227
+ if (!data) {
228
+ continue;
229
+ }
230
+ const thread = JSON.parse(data);
231
+ if (filter?.resourceId && thread.resourceId !== filter.resourceId) {
232
+ continue;
233
+ }
234
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
235
+ const threadMetadata = typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata;
236
+ const matches = Object.entries(filter.metadata).every(
237
+ ([key, value]) => jsonValueEquals(threadMetadata?.[key], value)
238
+ );
239
+ if (!matches) {
240
+ continue;
241
+ }
242
+ }
243
+ allThreads.push({
244
+ ...thread,
245
+ createdAt: ensureDate(thread.createdAt),
246
+ updatedAt: ensureDate(thread.updatedAt),
247
+ metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
248
+ });
249
+ }
250
+ const sortedThreads = this.sortThreads(allThreads, field, direction);
251
+ const total = sortedThreads.length;
252
+ const end = perPageInput === false ? total : offset + perPage;
253
+ const paginatedThreads = sortedThreads.slice(offset, end);
254
+ const hasMore = perPageInput === false ? false : end < total;
255
+ return {
256
+ threads: paginatedThreads,
257
+ total,
258
+ page,
259
+ perPage: perPageForResponse,
260
+ hasMore
261
+ };
262
+ } catch (error) {
263
+ const mastraError = new MastraError(
264
+ {
265
+ id: createStorageErrorId("REDIS", "LIST_THREADS", "FAILED"),
266
+ domain: ErrorDomain.STORAGE,
267
+ category: ErrorCategory.THIRD_PARTY,
268
+ details: {
269
+ ...filter?.resourceId && { resourceId: filter.resourceId },
270
+ hasMetadataFilter: !!filter?.metadata,
271
+ page,
272
+ perPage
273
+ }
274
+ },
275
+ error
276
+ );
277
+ this.logger.trackException(mastraError);
278
+ this.logger.error(mastraError.toString());
279
+ return {
280
+ threads: [],
281
+ total: 0,
282
+ page,
283
+ perPage: perPageForResponse,
284
+ hasMore: false
285
+ };
286
+ }
287
+ }
288
+ async saveThread({ thread }) {
289
+ try {
290
+ await this.db.insert({
291
+ tableName: TABLE_THREADS,
292
+ record: thread
293
+ });
294
+ return thread;
295
+ } catch (error) {
296
+ const mastraError = new MastraError(
297
+ {
298
+ id: createStorageErrorId("REDIS", "SAVE_THREAD", "FAILED"),
299
+ domain: ErrorDomain.STORAGE,
300
+ category: ErrorCategory.THIRD_PARTY,
301
+ details: {
302
+ threadId: thread.id
303
+ }
304
+ },
305
+ error
306
+ );
307
+ this.logger.trackException(mastraError);
308
+ this.logger.error(mastraError.toString());
309
+ throw mastraError;
310
+ }
311
+ }
312
+ async updateThread({
313
+ id,
314
+ title,
315
+ metadata
316
+ }) {
317
+ const thread = await this.getThreadById({ threadId: id });
318
+ if (!thread) {
319
+ throw new MastraError({
320
+ id: createStorageErrorId("REDIS", "UPDATE_THREAD", "FAILED"),
321
+ domain: ErrorDomain.STORAGE,
322
+ category: ErrorCategory.USER,
323
+ text: `Thread ${id} not found`,
324
+ details: {
325
+ threadId: id
326
+ }
327
+ });
328
+ }
329
+ const updatedThread = {
330
+ ...thread,
331
+ title,
332
+ metadata: {
333
+ ...thread.metadata,
334
+ ...metadata
335
+ },
336
+ updatedAt: /* @__PURE__ */ new Date()
337
+ };
338
+ try {
339
+ await this.saveThread({ thread: updatedThread });
340
+ return updatedThread;
341
+ } catch (error) {
342
+ throw new MastraError(
343
+ {
344
+ id: createStorageErrorId("REDIS", "UPDATE_THREAD", "FAILED"),
345
+ domain: ErrorDomain.STORAGE,
346
+ category: ErrorCategory.THIRD_PARTY,
347
+ details: {
348
+ threadId: id
349
+ }
350
+ },
351
+ error
352
+ );
353
+ }
354
+ }
355
+ async deleteThread({ threadId }) {
356
+ const threadKey = getKey(TABLE_THREADS, { id: threadId });
357
+ const threadMessagesKey = getThreadMessagesKey(threadId);
358
+ try {
359
+ const messageIds = await this.client.zRange(threadMessagesKey, 0, -1);
360
+ const multi = this.client.multi();
361
+ multi.del(threadKey);
362
+ multi.del(threadMessagesKey);
363
+ for (const messageId of messageIds) {
364
+ const messageKey = getMessageKey(threadId, messageId);
365
+ multi.del(messageKey);
366
+ multi.del(getMessageIndexKey(messageId));
367
+ }
368
+ await multi.exec();
369
+ await this.db.scanAndDelete(getMessageKey(threadId, "*"));
370
+ } catch (error) {
371
+ throw new MastraError(
372
+ {
373
+ id: createStorageErrorId("REDIS", "DELETE_THREAD", "FAILED"),
374
+ domain: ErrorDomain.STORAGE,
375
+ category: ErrorCategory.THIRD_PARTY,
376
+ details: {
377
+ threadId
378
+ }
379
+ },
380
+ error
381
+ );
382
+ }
383
+ }
384
+ async saveMessages(args) {
385
+ const { messages } = args;
386
+ if (messages.length === 0) {
387
+ return { messages: [] };
388
+ }
389
+ const threadId = messages[0]?.threadId;
390
+ try {
391
+ if (!threadId) {
392
+ throw new Error("Thread ID is required");
393
+ }
394
+ const thread = await this.getThreadById({ threadId });
395
+ if (!thread) {
396
+ throw new Error(`Thread ${threadId} not found`);
397
+ }
398
+ } catch (error) {
399
+ throw new MastraError(
400
+ {
401
+ id: createStorageErrorId("REDIS", "SAVE_MESSAGES", "INVALID_ARGS"),
402
+ domain: ErrorDomain.STORAGE,
403
+ category: ErrorCategory.USER
404
+ },
405
+ error
406
+ );
407
+ }
408
+ const messagesWithIndex = messages.map((message, index) => {
409
+ if (!message.threadId) {
410
+ throw new Error(
411
+ `Expected to find a threadId for message, but couldn't find one. An unexpected error has occurred.`
412
+ );
413
+ }
414
+ if (!message.resourceId) {
415
+ throw new Error(
416
+ `Expected to find a resourceId for message, but couldn't find one. An unexpected error has occurred.`
417
+ );
418
+ }
419
+ return {
420
+ ...message,
421
+ _index: index
422
+ };
423
+ });
424
+ const threadKey = getKey(TABLE_THREADS, { id: threadId });
425
+ const existingThreadData = await this.client.get(threadKey);
426
+ const existingThread = existingThreadData ? JSON.parse(existingThreadData) : null;
427
+ try {
428
+ const batchSize = 1e3;
429
+ const existingThreadIds = await this.client.mGet(
430
+ messagesWithIndex.map((message) => getMessageIndexKey(message.id))
431
+ );
432
+ for (let i = 0; i < messagesWithIndex.length; i += batchSize) {
433
+ const batch = messagesWithIndex.slice(i, i + batchSize);
434
+ const batchExistingThreadIds = existingThreadIds.slice(i, i + batch.length);
435
+ const multi = this.client.multi();
436
+ for (const [batchIndex, message] of batch.entries()) {
437
+ const key = getMessageKey(message.threadId, message.id);
438
+ const score = getMessageScore(message);
439
+ const existingThreadId = batchExistingThreadIds[batchIndex];
440
+ if (existingThreadId && existingThreadId !== message.threadId) {
441
+ const existingMessageKey = getMessageKey(existingThreadId, message.id);
442
+ multi.del(existingMessageKey);
443
+ multi.zRem(getThreadMessagesKey(existingThreadId), message.id);
444
+ }
445
+ multi.set(key, JSON.stringify(message));
446
+ multi.set(getMessageIndexKey(message.id), message.threadId);
447
+ multi.zAdd(getThreadMessagesKey(message.threadId), { score, value: message.id });
448
+ }
449
+ if (i === 0 && existingThread) {
450
+ const updatedThread = {
451
+ ...existingThread,
452
+ updatedAt: /* @__PURE__ */ new Date()
453
+ };
454
+ multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));
455
+ }
456
+ await multi.exec();
457
+ }
458
+ const list = new MessageList().add(messages, "memory");
459
+ return { messages: list.get.all.db() };
460
+ } catch (error) {
461
+ throw new MastraError(
462
+ {
463
+ id: createStorageErrorId("REDIS", "SAVE_MESSAGES", "FAILED"),
464
+ domain: ErrorDomain.STORAGE,
465
+ category: ErrorCategory.THIRD_PARTY,
466
+ details: {
467
+ threadId
468
+ }
469
+ },
470
+ error
471
+ );
472
+ }
473
+ }
474
+ async getThreadIdForMessage(messageId) {
475
+ const indexedThreadId = await this.client.get(getMessageIndexKey(messageId));
476
+ if (indexedThreadId) {
477
+ return indexedThreadId;
478
+ }
479
+ const keys = await this.db.scanKeys(getMessageKey("*", messageId));
480
+ if (keys.length === 0) {
481
+ return null;
482
+ }
483
+ const messageData = await this.client.get(keys[0]);
484
+ if (!messageData) {
485
+ return null;
486
+ }
487
+ const message = JSON.parse(messageData);
488
+ if (message.threadId) {
489
+ await this.client.set(getMessageIndexKey(messageId), message.threadId);
490
+ }
491
+ return message.threadId || null;
492
+ }
493
+ async getIncludedMessages(include) {
494
+ if (!include?.length) {
495
+ return [];
496
+ }
497
+ const messageIds = /* @__PURE__ */ new Set();
498
+ const messageIdToThreadIds = {};
499
+ for (const item of include) {
500
+ const itemThreadId = await this.getThreadIdForMessage(item.id);
501
+ if (!itemThreadId) {
502
+ continue;
503
+ }
504
+ messageIds.add(item.id);
505
+ messageIdToThreadIds[item.id] = itemThreadId;
506
+ const itemThreadMessagesKey = getThreadMessagesKey(itemThreadId);
507
+ const rank = await this.client.zRank(itemThreadMessagesKey, item.id);
508
+ if (rank === null) {
509
+ continue;
510
+ }
511
+ if (item.withPreviousMessages) {
512
+ const start = Math.max(0, rank - item.withPreviousMessages);
513
+ const prevIds = rank === 0 ? [] : await this.client.zRange(itemThreadMessagesKey, start, rank - 1);
514
+ prevIds.forEach((id) => {
515
+ messageIds.add(id);
516
+ messageIdToThreadIds[id] = itemThreadId;
517
+ });
518
+ }
519
+ if (item.withNextMessages) {
520
+ const nextIds = await this.client.zRange(itemThreadMessagesKey, rank + 1, rank + item.withNextMessages);
521
+ nextIds.forEach((id) => {
522
+ messageIds.add(id);
523
+ messageIdToThreadIds[id] = itemThreadId;
524
+ });
525
+ }
526
+ }
527
+ if (messageIds.size === 0) {
528
+ return [];
529
+ }
530
+ const keysToFetch = Array.from(messageIds).map((id) => getMessageKey(messageIdToThreadIds[id], id));
531
+ const results = await this.client.mGet(keysToFetch);
532
+ return results.filter((data) => data !== null).map((data) => JSON.parse(data));
533
+ }
534
+ parseStoredMessage(storedMessage) {
535
+ const defaultMessageContent = { format: 2, parts: [{ type: "text", text: "" }] };
536
+ const { _index, ...rest } = storedMessage;
537
+ return {
538
+ ...rest,
539
+ createdAt: new Date(rest.createdAt),
540
+ content: rest.content || defaultMessageContent
541
+ };
542
+ }
543
+ async listMessagesById({ messageIds }) {
544
+ if (messageIds.length === 0) {
545
+ return { messages: [] };
546
+ }
547
+ try {
548
+ const rawMessages = [];
549
+ const indexKeys = messageIds.map((id) => getMessageIndexKey(id));
550
+ const indexResults = await this.client.mGet(indexKeys);
551
+ const indexedIds = [];
552
+ const unindexedIds = [];
553
+ messageIds.forEach((id, i) => {
554
+ const threadId = indexResults[i];
555
+ if (threadId) {
556
+ indexedIds.push({ messageId: id, threadId });
557
+ return;
558
+ }
559
+ unindexedIds.push(id);
560
+ });
561
+ if (indexedIds.length > 0) {
562
+ const messageKeys = indexedIds.map(({ messageId, threadId }) => getMessageKey(threadId, messageId));
563
+ const messageResults = await this.client.mGet(messageKeys);
564
+ for (const data of messageResults) {
565
+ if (data) {
566
+ rawMessages.push(JSON.parse(data));
567
+ }
568
+ }
569
+ }
570
+ if (unindexedIds.length > 0) {
571
+ const threadKeys = await this.db.scanKeys("thread:*:messages");
572
+ const result = await Promise.all(
573
+ threadKeys.map(async (threadKey) => {
574
+ const threadId = threadKey.split(":")[1];
575
+ if (!threadId) {
576
+ throw new Error(`Failed to parse thread ID from thread key "${threadKey}"`);
577
+ }
578
+ const msgKeys = unindexedIds.map((id) => getMessageKey(threadId, id));
579
+ return this.client.mGet(msgKeys);
580
+ })
581
+ );
582
+ const foundMessages = result.flat(1).filter((data) => !!data).map((data) => JSON.parse(data));
583
+ rawMessages.push(...foundMessages);
584
+ if (foundMessages.length > 0) {
585
+ const multi = this.client.multi();
586
+ foundMessages.forEach((msg) => {
587
+ if (msg.threadId) {
588
+ multi.set(getMessageIndexKey(msg.id), msg.threadId);
589
+ }
590
+ });
591
+ await multi.exec();
592
+ }
593
+ }
594
+ const list = new MessageList().add(rawMessages.map(this.parseStoredMessage), "memory");
595
+ return { messages: list.get.all.db() };
596
+ } catch (error) {
597
+ throw new MastraError(
598
+ {
599
+ id: createStorageErrorId("REDIS", "LIST_MESSAGES_BY_ID", "FAILED"),
600
+ domain: ErrorDomain.STORAGE,
601
+ category: ErrorCategory.THIRD_PARTY,
602
+ details: {
603
+ messageIds: JSON.stringify(messageIds)
604
+ }
605
+ },
606
+ error
607
+ );
608
+ }
609
+ }
610
+ async listMessages(args) {
611
+ const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
612
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
613
+ const threadIdsSet = new Set(threadIds);
614
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
615
+ throw new MastraError(
616
+ {
617
+ id: createStorageErrorId("REDIS", "LIST_MESSAGES", "INVALID_THREAD_ID"),
618
+ domain: ErrorDomain.STORAGE,
619
+ category: ErrorCategory.USER,
620
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
621
+ },
622
+ new Error("threadId must be a non-empty string or array of non-empty strings")
623
+ );
624
+ }
625
+ const perPage = normalizePerPage(perPageInput, 40);
626
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
627
+ try {
628
+ if (page < 0) {
629
+ throw new MastraError(
630
+ {
631
+ id: createStorageErrorId("REDIS", "LIST_MESSAGES", "INVALID_PAGE"),
632
+ domain: ErrorDomain.STORAGE,
633
+ category: ErrorCategory.USER,
634
+ details: { page }
635
+ },
636
+ new Error("page must be >= 0")
637
+ );
638
+ }
639
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
640
+ const getFieldValue = (msg) => {
641
+ if (field === "createdAt") {
642
+ return new Date(msg.createdAt).getTime();
643
+ }
644
+ const value = msg[field];
645
+ if (typeof value === "number") {
646
+ return value;
647
+ }
648
+ if (value instanceof Date) {
649
+ return value.getTime();
650
+ }
651
+ return 0;
652
+ };
653
+ if (perPage === 0 && (!include || include.length === 0)) {
654
+ return {
655
+ messages: [],
656
+ total: 0,
657
+ page,
658
+ perPage: perPageForResponse,
659
+ hasMore: false
660
+ };
661
+ }
662
+ let includedMessages = [];
663
+ if (include && include.length > 0) {
664
+ const included = await this.getIncludedMessages(include);
665
+ includedMessages = included.map(this.parseStoredMessage);
666
+ }
667
+ if (perPage === 0 && include && include.length > 0) {
668
+ const list2 = new MessageList().add(includedMessages, "memory");
669
+ const messages = list2.get.all.db().sort((a, b) => {
670
+ const aValue = getFieldValue(a);
671
+ const bValue = getFieldValue(b);
672
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
673
+ });
674
+ return {
675
+ messages,
676
+ total: 0,
677
+ page,
678
+ perPage: perPageForResponse,
679
+ hasMore: false
680
+ };
681
+ }
682
+ const allMessageIdsWithThreads = [];
683
+ for (const tid of threadIds) {
684
+ const threadMessagesKey = getThreadMessagesKey(tid);
685
+ const msgIds = await this.client.zRange(threadMessagesKey, 0, -1);
686
+ for (const mid of msgIds) {
687
+ allMessageIdsWithThreads.push({ threadId: tid, messageId: mid });
688
+ }
689
+ }
690
+ if (allMessageIdsWithThreads.length === 0) {
691
+ return {
692
+ messages: [],
693
+ total: 0,
694
+ page,
695
+ perPage: perPageForResponse,
696
+ hasMore: false
697
+ };
698
+ }
699
+ const messageKeys = allMessageIdsWithThreads.map(({ threadId: tid, messageId }) => getMessageKey(tid, messageId));
700
+ const results = await this.client.mGet(messageKeys);
701
+ let messagesData = results.filter((data) => data !== null).map((data) => JSON.parse(data)).map(this.parseStoredMessage);
702
+ if (resourceId) {
703
+ messagesData = messagesData.filter((msg) => msg.resourceId === resourceId);
704
+ }
705
+ messagesData = filterByDateRange(
706
+ messagesData,
707
+ (msg) => new Date(msg.createdAt),
708
+ filter?.dateRange
709
+ );
710
+ messagesData.sort((a, b) => {
711
+ const aValue = getFieldValue(a);
712
+ const bValue = getFieldValue(b);
713
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
714
+ });
715
+ const total = messagesData.length;
716
+ const start = offset;
717
+ const end = perPageInput === false ? total : start + perPage;
718
+ const paginatedMessages = messagesData.slice(start, end);
719
+ const messageIdsSet = /* @__PURE__ */ new Set();
720
+ const allMessages = [];
721
+ for (const msg of paginatedMessages) {
722
+ if (messageIdsSet.has(msg.id)) {
723
+ continue;
724
+ }
725
+ allMessages.push(msg);
726
+ messageIdsSet.add(msg.id);
727
+ }
728
+ for (const msg of includedMessages) {
729
+ if (messageIdsSet.has(msg.id)) {
730
+ continue;
731
+ }
732
+ allMessages.push(msg);
733
+ messageIdsSet.add(msg.id);
734
+ }
735
+ const list = new MessageList().add(allMessages, "memory");
736
+ let finalMessages = list.get.all.db();
737
+ finalMessages = finalMessages.sort((a, b) => {
738
+ const aValue = getFieldValue(a);
739
+ const bValue = getFieldValue(b);
740
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
741
+ });
742
+ const returnedThreadMessageIds = new Set(
743
+ finalMessages.filter((m) => {
744
+ return m.threadId && threadIdsSet.has(m.threadId);
745
+ }).map((m) => m.id)
746
+ );
747
+ const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
748
+ const hasMore = perPageInput !== false && !allThreadMessagesReturned && end < total;
749
+ return {
750
+ messages: finalMessages,
751
+ total,
752
+ page,
753
+ perPage: perPageForResponse,
754
+ hasMore
755
+ };
756
+ } catch (error) {
757
+ const mastraError = new MastraError(
758
+ {
759
+ id: createStorageErrorId("REDIS", "LIST_MESSAGES", "FAILED"),
760
+ domain: ErrorDomain.STORAGE,
761
+ category: ErrorCategory.THIRD_PARTY,
762
+ details: {
763
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
764
+ resourceId: resourceId ?? ""
765
+ }
766
+ },
767
+ error
768
+ );
769
+ this.logger.error(mastraError.toString());
770
+ this.logger.trackException(mastraError);
771
+ return {
772
+ messages: [],
773
+ total: 0,
774
+ page,
775
+ perPage: perPageForResponse,
776
+ hasMore: false
777
+ };
778
+ }
779
+ }
780
+ async getResourceById({ resourceId }) {
781
+ try {
782
+ const key = `${TABLE_RESOURCES}:${resourceId}`;
783
+ const data = await this.client.get(key);
784
+ if (!data) {
785
+ return null;
786
+ }
787
+ const resource = JSON.parse(data);
788
+ return {
789
+ ...resource,
790
+ createdAt: new Date(resource.createdAt),
791
+ updatedAt: new Date(resource.updatedAt),
792
+ workingMemory: typeof resource.workingMemory === "object" ? JSON.stringify(resource.workingMemory) : resource.workingMemory,
793
+ metadata: typeof resource.metadata === "string" ? JSON.parse(resource.metadata) : resource.metadata
794
+ };
795
+ } catch (error) {
796
+ this.logger.error("Error getting resource by ID:", error);
797
+ throw error;
798
+ }
799
+ }
800
+ async saveResource({ resource }) {
801
+ try {
802
+ const key = `${TABLE_RESOURCES}:${resource.id}`;
803
+ const serializedResource = {
804
+ ...resource,
805
+ metadata: JSON.stringify(resource.metadata),
806
+ createdAt: resource.createdAt.toISOString(),
807
+ updatedAt: resource.updatedAt.toISOString()
808
+ };
809
+ await this.client.set(key, JSON.stringify(serializedResource));
810
+ return resource;
811
+ } catch (error) {
812
+ this.logger.error("Error saving resource:", error);
813
+ throw error;
814
+ }
815
+ }
816
+ async updateResource({
817
+ resourceId,
818
+ workingMemory,
819
+ metadata
820
+ }) {
821
+ try {
822
+ const existingResource = await this.getResourceById({ resourceId });
823
+ if (!existingResource) {
824
+ const newResource = {
825
+ id: resourceId,
826
+ workingMemory,
827
+ metadata: metadata || {},
828
+ createdAt: /* @__PURE__ */ new Date(),
829
+ updatedAt: /* @__PURE__ */ new Date()
830
+ };
831
+ return this.saveResource({ resource: newResource });
832
+ }
833
+ const updatedResource = {
834
+ ...existingResource,
835
+ workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
836
+ metadata: {
837
+ ...existingResource.metadata,
838
+ ...metadata
839
+ },
840
+ updatedAt: /* @__PURE__ */ new Date()
841
+ };
842
+ await this.saveResource({ resource: updatedResource });
843
+ return updatedResource;
844
+ } catch (error) {
845
+ this.logger.error("Error updating resource:", error);
846
+ throw error;
847
+ }
848
+ }
849
+ async updateMessages(args) {
850
+ const { messages } = args;
851
+ if (messages.length === 0) {
852
+ return [];
853
+ }
854
+ try {
855
+ const messageIds = messages.map((m) => m.id);
856
+ const existingMessages = [];
857
+ const messageIdToKey = {};
858
+ for (const messageId of messageIds) {
859
+ const pattern = getMessageKey("*", messageId);
860
+ const keys = await this.db.scanKeys(pattern);
861
+ for (const key of keys) {
862
+ const data = await this.client.get(key);
863
+ if (!data) {
864
+ continue;
865
+ }
866
+ const message = JSON.parse(data);
867
+ if (message && message.id === messageId) {
868
+ existingMessages.push(message);
869
+ messageIdToKey[messageId] = key;
870
+ break;
871
+ }
872
+ }
873
+ }
874
+ if (existingMessages.length === 0) {
875
+ return [];
876
+ }
877
+ const threadIdsToUpdate = /* @__PURE__ */ new Set();
878
+ const multi = this.client.multi();
879
+ for (const existingMessage of existingMessages) {
880
+ const updatePayload = messages.find((m) => m.id === existingMessage.id);
881
+ if (!updatePayload) {
882
+ continue;
883
+ }
884
+ const { id, ...fieldsToUpdate } = updatePayload;
885
+ if (Object.keys(fieldsToUpdate).length === 0) {
886
+ continue;
887
+ }
888
+ threadIdsToUpdate.add(existingMessage.threadId);
889
+ if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
890
+ threadIdsToUpdate.add(updatePayload.threadId);
891
+ }
892
+ const updatedMessage = { ...existingMessage };
893
+ if (fieldsToUpdate.content) {
894
+ const existingContent = existingMessage.content;
895
+ const newContent = {
896
+ ...existingContent,
897
+ ...fieldsToUpdate.content,
898
+ ...existingContent?.metadata && fieldsToUpdate.content.metadata ? {
899
+ metadata: {
900
+ ...existingContent.metadata,
901
+ ...fieldsToUpdate.content.metadata
902
+ }
903
+ } : {}
904
+ };
905
+ updatedMessage.content = newContent;
906
+ }
907
+ for (const key2 in fieldsToUpdate) {
908
+ if (Object.prototype.hasOwnProperty.call(fieldsToUpdate, key2) && key2 !== "content") {
909
+ updatedMessage[key2] = fieldsToUpdate[key2];
910
+ }
911
+ }
912
+ const key = messageIdToKey[id];
913
+ if (!key) {
914
+ continue;
915
+ }
916
+ if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
917
+ multi.zRem(getThreadMessagesKey(existingMessage.threadId), id);
918
+ multi.del(key);
919
+ const newKey = getMessageKey(updatePayload.threadId, id);
920
+ multi.set(newKey, JSON.stringify(updatedMessage));
921
+ multi.set(getMessageIndexKey(id), updatePayload.threadId);
922
+ const score = getMessageScore(updatedMessage);
923
+ multi.zAdd(getThreadMessagesKey(updatePayload.threadId), { score, value: id });
924
+ messageIdToKey[id] = newKey;
925
+ continue;
926
+ }
927
+ multi.set(key, JSON.stringify(updatedMessage));
928
+ }
929
+ const now = /* @__PURE__ */ new Date();
930
+ for (const threadId of threadIdsToUpdate) {
931
+ if (threadId) {
932
+ const threadKey = getKey(TABLE_THREADS, { id: threadId });
933
+ const existingThreadData = await this.client.get(threadKey);
934
+ if (existingThreadData) {
935
+ const existingThread = JSON.parse(existingThreadData);
936
+ const updatedThread = {
937
+ ...existingThread,
938
+ updatedAt: now
939
+ };
940
+ multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));
941
+ }
942
+ }
943
+ }
944
+ await multi.exec();
945
+ const updatedMessages = [];
946
+ for (const messageId of messageIds) {
947
+ const key = messageIdToKey[messageId];
948
+ if (key) {
949
+ const data = await this.client.get(key);
950
+ if (data) {
951
+ updatedMessages.push(JSON.parse(data));
952
+ }
953
+ }
954
+ }
955
+ return updatedMessages;
956
+ } catch (error) {
957
+ throw new MastraError(
958
+ {
959
+ id: createStorageErrorId("REDIS", "UPDATE_MESSAGES", "FAILED"),
960
+ domain: ErrorDomain.STORAGE,
961
+ category: ErrorCategory.THIRD_PARTY,
962
+ details: {
963
+ messageIds: messages.map((m) => m.id).join(",")
964
+ }
965
+ },
966
+ error
967
+ );
968
+ }
969
+ }
970
+ async deleteMessages(messageIds) {
971
+ if (!messageIds || messageIds.length === 0) {
972
+ return;
973
+ }
974
+ try {
975
+ const threadIds = /* @__PURE__ */ new Set();
976
+ const messageKeys = [];
977
+ const foundMessageIds = [];
978
+ const messageIdToThreadId = /* @__PURE__ */ new Map();
979
+ const indexKeys = messageIds.map((id) => getMessageIndexKey(id));
980
+ const indexResults = await this.client.mGet(indexKeys);
981
+ const indexedMessages = [];
982
+ const unindexedMessageIds = [];
983
+ messageIds.forEach((id, i) => {
984
+ const threadId = indexResults[i];
985
+ if (threadId) {
986
+ indexedMessages.push({ messageId: id, threadId });
987
+ return;
988
+ }
989
+ unindexedMessageIds.push(id);
990
+ });
991
+ for (const { messageId, threadId } of indexedMessages) {
992
+ messageKeys.push(getMessageKey(threadId, messageId));
993
+ foundMessageIds.push(messageId);
994
+ messageIdToThreadId.set(messageId, threadId);
995
+ threadIds.add(threadId);
996
+ }
997
+ for (const messageId of unindexedMessageIds) {
998
+ const pattern = getMessageKey("*", messageId);
999
+ const keys = await this.db.scanKeys(pattern);
1000
+ for (const key of keys) {
1001
+ const data = await this.client.get(key);
1002
+ if (!data) {
1003
+ continue;
1004
+ }
1005
+ const message = JSON.parse(data);
1006
+ if (message && message.id === messageId) {
1007
+ messageKeys.push(key);
1008
+ foundMessageIds.push(messageId);
1009
+ if (message.threadId) {
1010
+ messageIdToThreadId.set(messageId, message.threadId);
1011
+ threadIds.add(message.threadId);
1012
+ }
1013
+ break;
1014
+ }
1015
+ }
1016
+ }
1017
+ if (messageKeys.length === 0) {
1018
+ return;
1019
+ }
1020
+ const multi = this.client.multi();
1021
+ for (const key of messageKeys) {
1022
+ multi.del(key);
1023
+ }
1024
+ for (const messageId of foundMessageIds) {
1025
+ multi.del(getMessageIndexKey(messageId));
1026
+ }
1027
+ if (threadIds.size > 0) {
1028
+ for (const threadId of threadIds) {
1029
+ for (const [msgId, msgThreadId] of messageIdToThreadId) {
1030
+ if (msgThreadId === threadId) {
1031
+ multi.zRem(getThreadMessagesKey(threadId), msgId);
1032
+ }
1033
+ }
1034
+ const threadKey = getKey(TABLE_THREADS, { id: threadId });
1035
+ const threadData = await this.client.get(threadKey);
1036
+ if (!threadData) {
1037
+ continue;
1038
+ }
1039
+ const thread = JSON.parse(threadData);
1040
+ const updatedThread = { ...thread, updatedAt: /* @__PURE__ */ new Date() };
1041
+ multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));
1042
+ }
1043
+ }
1044
+ await multi.exec();
1045
+ } catch (error) {
1046
+ throw new MastraError(
1047
+ {
1048
+ id: createStorageErrorId("REDIS", "DELETE_MESSAGES", "FAILED"),
1049
+ domain: ErrorDomain.STORAGE,
1050
+ category: ErrorCategory.THIRD_PARTY,
1051
+ details: { messageIds: messageIds.join(", ") }
1052
+ },
1053
+ error
1054
+ );
1055
+ }
1056
+ }
1057
+ sortThreads(threads, field, direction) {
1058
+ return threads.sort((a, b) => {
1059
+ const aValue = new Date(a[field]).getTime();
1060
+ const bValue = new Date(b[field]).getTime();
1061
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
1062
+ });
1063
+ }
1064
+ async cloneThread(args) {
1065
+ const { sourceThreadId, newThreadId: providedThreadId, resourceId, title, metadata, options } = args;
1066
+ const sourceThread = await this.getThreadById({ threadId: sourceThreadId });
1067
+ if (!sourceThread) {
1068
+ throw new MastraError({
1069
+ id: createStorageErrorId("REDIS", "CLONE_THREAD", "SOURCE_NOT_FOUND"),
1070
+ domain: ErrorDomain.STORAGE,
1071
+ category: ErrorCategory.USER,
1072
+ text: `Source thread with id ${sourceThreadId} not found`,
1073
+ details: { sourceThreadId }
1074
+ });
1075
+ }
1076
+ const newThreadId = providedThreadId || crypto.randomUUID();
1077
+ const existingThread = await this.getThreadById({ threadId: newThreadId });
1078
+ if (existingThread) {
1079
+ throw new MastraError({
1080
+ id: createStorageErrorId("REDIS", "CLONE_THREAD", "THREAD_EXISTS"),
1081
+ domain: ErrorDomain.STORAGE,
1082
+ category: ErrorCategory.USER,
1083
+ text: `Thread with id ${newThreadId} already exists`,
1084
+ details: { newThreadId }
1085
+ });
1086
+ }
1087
+ try {
1088
+ const threadMessagesKey = getThreadMessagesKey(sourceThreadId);
1089
+ const msgIds = await this.client.zRange(threadMessagesKey, 0, -1);
1090
+ const messageKeys = msgIds.map((mid) => getMessageKey(sourceThreadId, mid));
1091
+ let sourceMessages = [];
1092
+ if (messageKeys.length > 0) {
1093
+ const results = await this.client.mGet(messageKeys);
1094
+ sourceMessages = results.filter((data) => data !== null).map((data) => {
1095
+ const msg = JSON.parse(data);
1096
+ return { ...msg, createdAt: new Date(msg.createdAt) };
1097
+ });
1098
+ }
1099
+ if (options?.messageFilter?.startDate || options?.messageFilter?.endDate) {
1100
+ sourceMessages = filterByDateRange(sourceMessages, (msg) => new Date(msg.createdAt), {
1101
+ start: options.messageFilter?.startDate,
1102
+ end: options.messageFilter?.endDate
1103
+ });
1104
+ }
1105
+ if (options?.messageFilter?.messageIds && options.messageFilter.messageIds.length > 0) {
1106
+ const messageIdSet = new Set(options.messageFilter.messageIds);
1107
+ sourceMessages = sourceMessages.filter((msg) => messageIdSet.has(msg.id));
1108
+ }
1109
+ sourceMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
1110
+ if (options?.messageLimit && options.messageLimit > 0 && sourceMessages.length > options.messageLimit) {
1111
+ sourceMessages = sourceMessages.slice(-options.messageLimit);
1112
+ }
1113
+ const now = /* @__PURE__ */ new Date();
1114
+ const lastMessageId = sourceMessages.length > 0 ? sourceMessages[sourceMessages.length - 1].id : void 0;
1115
+ const cloneMetadata = {
1116
+ sourceThreadId,
1117
+ clonedAt: now,
1118
+ ...lastMessageId && { lastMessageId }
1119
+ };
1120
+ const newThread = {
1121
+ id: newThreadId,
1122
+ resourceId: resourceId || sourceThread.resourceId,
1123
+ title: title || (sourceThread.title ? `Clone of ${sourceThread.title}` : void 0),
1124
+ metadata: { ...metadata, clone: cloneMetadata },
1125
+ createdAt: now,
1126
+ updatedAt: now
1127
+ };
1128
+ const multi = this.client.multi();
1129
+ const threadKey = getKey(TABLE_THREADS, { id: newThreadId });
1130
+ multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, newThread).processedRecord));
1131
+ const clonedMessages = [];
1132
+ const targetResourceId = resourceId || sourceThread.resourceId;
1133
+ const newThreadMessagesKey = getThreadMessagesKey(newThreadId);
1134
+ for (let i = 0; i < sourceMessages.length; i++) {
1135
+ const sourceMsg = sourceMessages[i];
1136
+ const newMessageId = crypto.randomUUID();
1137
+ const { _index, ...restMsg } = sourceMsg;
1138
+ const newMessage = {
1139
+ ...restMsg,
1140
+ id: newMessageId,
1141
+ threadId: newThreadId,
1142
+ resourceId: targetResourceId
1143
+ };
1144
+ const messageKey = getMessageKey(newThreadId, newMessageId);
1145
+ multi.set(messageKey, JSON.stringify(newMessage));
1146
+ multi.set(getMessageIndexKey(newMessageId), newThreadId);
1147
+ const score = getMessageScore({ createdAt: newMessage.createdAt, _index: i });
1148
+ multi.zAdd(newThreadMessagesKey, { score, value: newMessageId });
1149
+ clonedMessages.push(newMessage);
1150
+ }
1151
+ await multi.exec();
1152
+ return {
1153
+ thread: newThread,
1154
+ clonedMessages
1155
+ };
1156
+ } catch (error) {
1157
+ if (error instanceof MastraError) {
1158
+ throw error;
1159
+ }
1160
+ throw new MastraError(
1161
+ {
1162
+ id: createStorageErrorId("REDIS", "CLONE_THREAD", "FAILED"),
1163
+ domain: ErrorDomain.STORAGE,
1164
+ category: ErrorCategory.THIRD_PARTY,
1165
+ details: { sourceThreadId, newThreadId }
1166
+ },
1167
+ error
1168
+ );
1169
+ }
1170
+ }
1171
+ };
1172
+ function getThreadMessagesKey(threadId) {
1173
+ return `thread:${threadId}:messages`;
1174
+ }
1175
+ function getMessageKey(threadId, messageId) {
1176
+ return getKey(TABLE_MESSAGES, { threadId, id: messageId });
1177
+ }
1178
+ function getMessageIndexKey(messageId) {
1179
+ return `msg-idx:${messageId}`;
1180
+ }
1181
+ function getMessageScore(message) {
1182
+ const createdAtScore = new Date(message.createdAt).getTime();
1183
+ const index = typeof message._index === "number" ? message._index : 0;
1184
+ return createdAtScore * 1e3 + index;
1185
+ }
1186
+ var ScoresRedis = class extends ScoresStorage {
1187
+ client;
1188
+ db;
1189
+ constructor(config) {
1190
+ super();
1191
+ this.client = config.client;
1192
+ this.db = new RedisDB({ client: config.client });
1193
+ }
1194
+ async dangerouslyClearAll() {
1195
+ await this.db.deleteData({ tableName: TABLE_SCORERS });
1196
+ }
1197
+ async getScoreById({ id }) {
1198
+ try {
1199
+ const data = await this.db.get({
1200
+ tableName: TABLE_SCORERS,
1201
+ keys: { id }
1202
+ });
1203
+ if (!data) {
1204
+ return null;
1205
+ }
1206
+ return transformScoreRow(data);
1207
+ } catch (error) {
1208
+ throw new MastraError(
1209
+ {
1210
+ id: createStorageErrorId("REDIS", "GET_SCORE_BY_ID", "FAILED"),
1211
+ domain: ErrorDomain.STORAGE,
1212
+ category: ErrorCategory.THIRD_PARTY,
1213
+ details: {
1214
+ ...id && { id }
1215
+ }
1216
+ },
1217
+ error
1218
+ );
1219
+ }
1220
+ }
1221
+ async listScoresByScorerId({
1222
+ scorerId,
1223
+ entityId,
1224
+ entityType,
1225
+ source,
1226
+ pagination = { page: 0, perPage: 20 }
1227
+ }) {
1228
+ return this.fetchAndFilterScores(pagination, (row) => {
1229
+ if (row.scorerId !== scorerId) {
1230
+ return false;
1231
+ }
1232
+ if (entityId && row.entityId !== entityId) {
1233
+ return false;
1234
+ }
1235
+ if (entityType && row.entityType !== entityType) {
1236
+ return false;
1237
+ }
1238
+ if (source && row.source !== source) {
1239
+ return false;
1240
+ }
1241
+ return true;
1242
+ });
1243
+ }
1244
+ async saveScore(score) {
1245
+ let validatedScore;
1246
+ try {
1247
+ validatedScore = saveScorePayloadSchema.parse(score);
1248
+ } catch (error) {
1249
+ throw new MastraError(
1250
+ {
1251
+ id: createStorageErrorId("REDIS", "SAVE_SCORE", "VALIDATION_FAILED"),
1252
+ domain: ErrorDomain.STORAGE,
1253
+ category: ErrorCategory.USER,
1254
+ details: {
1255
+ scorer: typeof score.scorer?.id === "string" ? score.scorer.id : String(score.scorer?.id ?? "unknown"),
1256
+ entityId: score.entityId ?? "unknown",
1257
+ entityType: score.entityType ?? "unknown",
1258
+ traceId: score.traceId ?? "",
1259
+ spanId: score.spanId ?? ""
1260
+ }
1261
+ },
1262
+ error
1263
+ );
1264
+ }
1265
+ const now = /* @__PURE__ */ new Date();
1266
+ const id = crypto2.randomUUID();
1267
+ const scoreWithId = {
1268
+ ...validatedScore,
1269
+ id,
1270
+ createdAt: now,
1271
+ updatedAt: now
1272
+ };
1273
+ const { key, processedRecord } = processRecord(TABLE_SCORERS, scoreWithId);
1274
+ try {
1275
+ await this.client.set(key, JSON.stringify(processedRecord));
1276
+ return { score: { ...validatedScore, id, createdAt: now, updatedAt: now } };
1277
+ } catch (error) {
1278
+ throw new MastraError(
1279
+ {
1280
+ id: createStorageErrorId("REDIS", "SAVE_SCORE", "FAILED"),
1281
+ domain: ErrorDomain.STORAGE,
1282
+ category: ErrorCategory.THIRD_PARTY,
1283
+ details: { id }
1284
+ },
1285
+ error
1286
+ );
1287
+ }
1288
+ }
1289
+ async listScoresByRunId({
1290
+ runId,
1291
+ pagination = { page: 0, perPage: 20 }
1292
+ }) {
1293
+ return this.fetchAndFilterScores(pagination, (row) => row.runId === runId);
1294
+ }
1295
+ async listScoresByEntityId({
1296
+ entityId,
1297
+ entityType,
1298
+ pagination = { page: 0, perPage: 20 }
1299
+ }) {
1300
+ return this.fetchAndFilterScores(pagination, (row) => {
1301
+ if (row.entityId !== entityId) {
1302
+ return false;
1303
+ }
1304
+ if (entityType && row.entityType !== entityType) {
1305
+ return false;
1306
+ }
1307
+ return true;
1308
+ });
1309
+ }
1310
+ async listScoresBySpan({
1311
+ traceId,
1312
+ spanId,
1313
+ pagination = { page: 0, perPage: 20 }
1314
+ }) {
1315
+ return this.fetchAndFilterScores(pagination, (row) => row.traceId === traceId && row.spanId === spanId);
1316
+ }
1317
+ async fetchAndFilterScores(pagination, filterFn) {
1318
+ const { page, perPage: perPageInput } = pagination;
1319
+ const keys = await this.db.scanKeys(`${TABLE_SCORERS}:*`);
1320
+ if (keys.length === 0) {
1321
+ return {
1322
+ scores: [],
1323
+ pagination: { total: 0, page, perPage: perPageInput, hasMore: false }
1324
+ };
1325
+ }
1326
+ const results = await this.client.mGet(keys);
1327
+ const filtered = results.map((data) => {
1328
+ if (!data) {
1329
+ return null;
1330
+ }
1331
+ try {
1332
+ return JSON.parse(data);
1333
+ } catch {
1334
+ return null;
1335
+ }
1336
+ }).filter((row) => !!row && typeof row === "object" && filterFn(row));
1337
+ const total = filtered.length;
1338
+ const perPage = normalizePerPage(perPageInput, 100);
1339
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1340
+ const end = perPageInput === false ? total : start + perPage;
1341
+ const scores = filtered.slice(start, end).map((row) => transformScoreRow(row));
1342
+ return {
1343
+ scores,
1344
+ pagination: {
1345
+ total,
1346
+ page,
1347
+ perPage: perPageForResponse,
1348
+ hasMore: end < total
1349
+ }
1350
+ };
1351
+ }
1352
+ };
1353
+ function parseWorkflowRun(row) {
1354
+ let parsedSnapshot = row.snapshot;
1355
+ if (typeof parsedSnapshot === "string") {
1356
+ try {
1357
+ parsedSnapshot = JSON.parse(row.snapshot);
1358
+ } catch (e) {
1359
+ console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1360
+ }
1361
+ }
1362
+ return {
1363
+ workflowName: row.workflow_name,
1364
+ runId: row.run_id,
1365
+ snapshot: parsedSnapshot,
1366
+ createdAt: ensureDate(row.createdAt),
1367
+ updatedAt: ensureDate(row.updatedAt),
1368
+ resourceId: row.resourceId
1369
+ };
1370
+ }
1371
+ var WorkflowsRedis = class extends WorkflowsStorage {
1372
+ client;
1373
+ db;
1374
+ constructor(config) {
1375
+ super();
1376
+ this.client = config.client;
1377
+ this.db = new RedisDB({ client: config.client });
1378
+ }
1379
+ supportsConcurrentUpdates() {
1380
+ return false;
1381
+ }
1382
+ async dangerouslyClearAll() {
1383
+ await this.db.deleteData({ tableName: TABLE_WORKFLOW_SNAPSHOT });
1384
+ }
1385
+ async updateWorkflowResults({
1386
+ workflowName,
1387
+ runId,
1388
+ stepId,
1389
+ result,
1390
+ requestContext
1391
+ }) {
1392
+ try {
1393
+ const existingRecord = await this.db.get({
1394
+ tableName: TABLE_WORKFLOW_SNAPSHOT,
1395
+ keys: {
1396
+ namespace: "workflows",
1397
+ workflow_name: workflowName,
1398
+ run_id: runId
1399
+ }
1400
+ });
1401
+ const existingSnapshot = existingRecord?.snapshot;
1402
+ let snapshot = existingSnapshot;
1403
+ if (!snapshot) {
1404
+ snapshot = {
1405
+ context: {},
1406
+ activePaths: [],
1407
+ timestamp: Date.now(),
1408
+ suspendedPaths: {},
1409
+ activeStepsPath: {},
1410
+ resumeLabels: {},
1411
+ serializedStepGraph: [],
1412
+ status: "pending",
1413
+ value: {},
1414
+ waitingPaths: {},
1415
+ runId,
1416
+ requestContext: {}
1417
+ };
1418
+ }
1419
+ snapshot.context[stepId] = result;
1420
+ snapshot.requestContext = { ...snapshot.requestContext, ...requestContext };
1421
+ await this.persistWorkflowSnapshot({
1422
+ namespace: "workflows",
1423
+ workflowName,
1424
+ runId,
1425
+ snapshot,
1426
+ createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : void 0
1427
+ });
1428
+ return snapshot.context;
1429
+ } catch (error) {
1430
+ if (error instanceof MastraError) {
1431
+ throw error;
1432
+ }
1433
+ throw new MastraError(
1434
+ {
1435
+ id: createStorageErrorId("REDIS", "UPDATE_WORKFLOW_RESULTS", "FAILED"),
1436
+ domain: ErrorDomain.STORAGE,
1437
+ category: ErrorCategory.THIRD_PARTY,
1438
+ details: { workflowName, runId, stepId }
1439
+ },
1440
+ error
1441
+ );
1442
+ }
1443
+ }
1444
+ async updateWorkflowState({
1445
+ workflowName,
1446
+ runId,
1447
+ opts
1448
+ }) {
1449
+ try {
1450
+ const existingRecord = await this.db.get({
1451
+ tableName: TABLE_WORKFLOW_SNAPSHOT,
1452
+ keys: {
1453
+ namespace: "workflows",
1454
+ workflow_name: workflowName,
1455
+ run_id: runId
1456
+ }
1457
+ });
1458
+ const existingSnapshot = existingRecord?.snapshot;
1459
+ if (!existingSnapshot || !existingSnapshot.context) {
1460
+ return void 0;
1461
+ }
1462
+ const updatedSnapshot = { ...existingSnapshot, ...opts };
1463
+ await this.persistWorkflowSnapshot({
1464
+ namespace: "workflows",
1465
+ workflowName,
1466
+ runId,
1467
+ snapshot: updatedSnapshot,
1468
+ createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : void 0
1469
+ });
1470
+ return updatedSnapshot;
1471
+ } catch (error) {
1472
+ if (error instanceof MastraError) {
1473
+ throw error;
1474
+ }
1475
+ throw new MastraError(
1476
+ {
1477
+ id: createStorageErrorId("REDIS", "UPDATE_WORKFLOW_STATE", "FAILED"),
1478
+ domain: ErrorDomain.STORAGE,
1479
+ category: ErrorCategory.THIRD_PARTY,
1480
+ details: { workflowName, runId }
1481
+ },
1482
+ error
1483
+ );
1484
+ }
1485
+ }
1486
+ async persistWorkflowSnapshot(params) {
1487
+ const { namespace = "workflows", workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;
1488
+ try {
1489
+ let finalCreatedAt = createdAt;
1490
+ if (!finalCreatedAt) {
1491
+ const existing = await this.db.get({
1492
+ tableName: TABLE_WORKFLOW_SNAPSHOT,
1493
+ keys: {
1494
+ namespace,
1495
+ workflow_name: workflowName,
1496
+ run_id: runId
1497
+ }
1498
+ });
1499
+ finalCreatedAt = existing?.createdAt ? ensureDate(existing.createdAt) : /* @__PURE__ */ new Date();
1500
+ }
1501
+ await this.db.insert({
1502
+ tableName: TABLE_WORKFLOW_SNAPSHOT,
1503
+ record: {
1504
+ namespace,
1505
+ workflow_name: workflowName,
1506
+ run_id: runId,
1507
+ resourceId,
1508
+ snapshot,
1509
+ createdAt: finalCreatedAt,
1510
+ updatedAt: updatedAt ?? /* @__PURE__ */ new Date()
1511
+ }
1512
+ });
1513
+ } catch (error) {
1514
+ throw new MastraError(
1515
+ {
1516
+ id: createStorageErrorId("REDIS", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
1517
+ domain: ErrorDomain.STORAGE,
1518
+ category: ErrorCategory.THIRD_PARTY,
1519
+ details: {
1520
+ namespace,
1521
+ workflowName,
1522
+ runId
1523
+ }
1524
+ },
1525
+ error
1526
+ );
1527
+ }
1528
+ }
1529
+ async loadWorkflowSnapshot(params) {
1530
+ const { namespace = "workflows", workflowName, runId } = params;
1531
+ const key = getKey(TABLE_WORKFLOW_SNAPSHOT, {
1532
+ namespace,
1533
+ workflow_name: workflowName,
1534
+ run_id: runId
1535
+ });
1536
+ try {
1537
+ const data = await this.client.get(key);
1538
+ if (!data) {
1539
+ return null;
1540
+ }
1541
+ const parsed = JSON.parse(data);
1542
+ return parsed.snapshot;
1543
+ } catch (error) {
1544
+ throw new MastraError(
1545
+ {
1546
+ id: createStorageErrorId("REDIS", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
1547
+ domain: ErrorDomain.STORAGE,
1548
+ category: ErrorCategory.THIRD_PARTY,
1549
+ details: {
1550
+ namespace,
1551
+ workflowName,
1552
+ runId
1553
+ }
1554
+ },
1555
+ error
1556
+ );
1557
+ }
1558
+ }
1559
+ async getWorkflowRunById({
1560
+ runId,
1561
+ workflowName
1562
+ }) {
1563
+ try {
1564
+ const key = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows", workflow_name: workflowName, run_id: runId }) + "*";
1565
+ const keys = await this.db.scanKeys(key);
1566
+ if (keys.length === 0) {
1567
+ return null;
1568
+ }
1569
+ const results = await this.client.mGet(keys);
1570
+ const workflows = results.filter((data2) => data2 !== null).map(
1571
+ (data2) => JSON.parse(data2)
1572
+ );
1573
+ const data = workflows.find((workflow) => {
1574
+ if (!workflow) {
1575
+ return false;
1576
+ }
1577
+ const runIdMatch = workflow.run_id === runId;
1578
+ if (workflowName) {
1579
+ return runIdMatch && workflow.workflow_name === workflowName;
1580
+ }
1581
+ return runIdMatch;
1582
+ });
1583
+ if (!data) {
1584
+ return null;
1585
+ }
1586
+ return parseWorkflowRun(data);
1587
+ } catch (error) {
1588
+ throw new MastraError(
1589
+ {
1590
+ id: createStorageErrorId("REDIS", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
1591
+ domain: ErrorDomain.STORAGE,
1592
+ category: ErrorCategory.THIRD_PARTY,
1593
+ details: {
1594
+ namespace: "workflows",
1595
+ runId,
1596
+ workflowName: workflowName || ""
1597
+ }
1598
+ },
1599
+ error
1600
+ );
1601
+ }
1602
+ }
1603
+ async deleteWorkflowRunById({ runId, workflowName }) {
1604
+ const key = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows", workflow_name: workflowName, run_id: runId });
1605
+ try {
1606
+ await this.client.del(key);
1607
+ } catch (error) {
1608
+ throw new MastraError(
1609
+ {
1610
+ id: createStorageErrorId("REDIS", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
1611
+ domain: ErrorDomain.STORAGE,
1612
+ category: ErrorCategory.THIRD_PARTY,
1613
+ details: {
1614
+ namespace: "workflows",
1615
+ runId,
1616
+ workflowName
1617
+ }
1618
+ },
1619
+ error
1620
+ );
1621
+ }
1622
+ }
1623
+ async listWorkflowRuns({
1624
+ workflowName,
1625
+ fromDate,
1626
+ toDate,
1627
+ perPage,
1628
+ page,
1629
+ resourceId,
1630
+ status
1631
+ } = {}) {
1632
+ try {
1633
+ if (page !== void 0 && page < 0) {
1634
+ throw new MastraError(
1635
+ {
1636
+ id: createStorageErrorId("REDIS", "LIST_WORKFLOW_RUNS", "INVALID_PAGE"),
1637
+ domain: ErrorDomain.STORAGE,
1638
+ category: ErrorCategory.USER,
1639
+ details: { page }
1640
+ },
1641
+ new Error("page must be >= 0")
1642
+ );
1643
+ }
1644
+ const normalizedFrom = fromDate ? ensureDate(fromDate) : void 0;
1645
+ const normalizedTo = toDate ? ensureDate(toDate) : void 0;
1646
+ let pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows" }) + ":*";
1647
+ if (workflowName && resourceId) {
1648
+ pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, {
1649
+ namespace: "workflows",
1650
+ workflow_name: workflowName,
1651
+ run_id: "*",
1652
+ resourceId
1653
+ });
1654
+ } else if (workflowName) {
1655
+ pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows", workflow_name: workflowName }) + ":*";
1656
+ } else if (resourceId) {
1657
+ pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, {
1658
+ namespace: "workflows",
1659
+ workflow_name: "*",
1660
+ run_id: "*",
1661
+ resourceId
1662
+ });
1663
+ }
1664
+ const keys = await this.db.scanKeys(pattern);
1665
+ if (keys.length === 0) {
1666
+ return { runs: [], total: 0 };
1667
+ }
1668
+ const results = await this.client.mGet(keys);
1669
+ let runs = results.filter((data) => data !== null).map((data) => JSON.parse(data)).filter(
1670
+ (record) => record !== null && record !== void 0 && typeof record === "object" && "workflow_name" in record
1671
+ ).filter((record) => !workflowName || record.workflow_name === workflowName).map((w) => parseWorkflowRun(w)).filter((w) => {
1672
+ if (normalizedFrom && w.createdAt < normalizedFrom) {
1673
+ return false;
1674
+ }
1675
+ if (normalizedTo && w.createdAt > normalizedTo) {
1676
+ return false;
1677
+ }
1678
+ if (status) {
1679
+ let snapshot = w.snapshot;
1680
+ if (typeof snapshot === "string") {
1681
+ try {
1682
+ snapshot = JSON.parse(snapshot);
1683
+ } catch (e) {
1684
+ console.warn(`Failed to parse snapshot for workflow ${w.workflowName}: ${e}`);
1685
+ return false;
1686
+ }
1687
+ }
1688
+ return snapshot.status === status;
1689
+ }
1690
+ return true;
1691
+ }).sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
1692
+ const total = runs.length;
1693
+ if (typeof perPage === "number" && typeof page === "number") {
1694
+ const normalizedPerPage = normalizePerPage(perPage, Number.MAX_SAFE_INTEGER);
1695
+ const offset = page * normalizedPerPage;
1696
+ runs = runs.slice(offset, offset + normalizedPerPage);
1697
+ }
1698
+ return { runs, total };
1699
+ } catch (error) {
1700
+ if (error instanceof MastraError) {
1701
+ throw error;
1702
+ }
1703
+ throw new MastraError(
1704
+ {
1705
+ id: createStorageErrorId("REDIS", "LIST_WORKFLOW_RUNS", "FAILED"),
1706
+ domain: ErrorDomain.STORAGE,
1707
+ category: ErrorCategory.THIRD_PARTY,
1708
+ details: {
1709
+ namespace: "workflows",
1710
+ workflowName: workflowName || "",
1711
+ resourceId: resourceId || ""
1712
+ }
1713
+ },
1714
+ error
1715
+ );
1716
+ }
1717
+ }
1718
+ };
1719
+
1720
+ // src/storage/utils.ts
1721
+ function isClientConfig(config) {
1722
+ return "client" in config;
1723
+ }
1724
+ function isConnectionStringConfig(config) {
1725
+ return "connectionString" in config;
1726
+ }
1727
+
1728
+ // src/storage/store.ts
1729
+ var RedisStore = class extends MastraStorage {
1730
+ client;
1731
+ shouldManageConnection;
1732
+ stores;
1733
+ constructor(config) {
1734
+ super({ id: config.id, name: "Redis", disableInit: config.disableInit });
1735
+ const { client, shouldManageConnection } = this.createClient(config);
1736
+ this.client = client;
1737
+ this.shouldManageConnection = shouldManageConnection;
1738
+ this.stores = {
1739
+ scores: new ScoresRedis({ client: this.client }),
1740
+ workflows: new WorkflowsRedis({ client: this.client }),
1741
+ memory: new StoreMemoryRedis({ client: this.client })
1742
+ };
1743
+ }
1744
+ async init() {
1745
+ if (this.shouldManageConnection && !this.client.isOpen) {
1746
+ await this.client.connect();
1747
+ }
1748
+ await super.init();
1749
+ }
1750
+ getClient() {
1751
+ return this.client;
1752
+ }
1753
+ async close() {
1754
+ if (this.shouldManageConnection && this.client.isOpen) {
1755
+ await this.client.quit();
1756
+ }
1757
+ }
1758
+ createClient(config) {
1759
+ if (isClientConfig(config)) {
1760
+ return { client: config.client, shouldManageConnection: false };
1761
+ }
1762
+ if (isConnectionStringConfig(config)) {
1763
+ if (!config.connectionString?.trim()) {
1764
+ throw new Error("RedisStore: connectionString is required and cannot be empty.");
1765
+ }
1766
+ return {
1767
+ client: createClient({ url: config.connectionString }),
1768
+ shouldManageConnection: true
1769
+ };
1770
+ }
1771
+ if (!config.host?.trim()) {
1772
+ throw new Error("RedisStore: host is required and cannot be empty.");
1773
+ }
1774
+ const url = this.createClientUrl({
1775
+ ...config,
1776
+ db: config.db ?? 0,
1777
+ port: config.port ?? 6379
1778
+ });
1779
+ return {
1780
+ client: createClient({ url }),
1781
+ shouldManageConnection: true
1782
+ };
1783
+ }
1784
+ createClientUrl(config) {
1785
+ const encodedPassword = config.password ? encodeURIComponent(config.password) : null;
1786
+ if (config.password) {
1787
+ return `redis://:${encodedPassword}@${config.host}:${config.port || 6379}/${config.db || 0}`;
1788
+ }
1789
+ return `redis://${config.host}:${config.port || 6379}/${config.db || 0}`;
1790
+ }
1791
+ };
1792
+
1793
+ export { RedisStore, ScoresRedis, StoreMemoryRedis, WorkflowsRedis };
1794
+ //# sourceMappingURL=index.js.map
1795
+ //# sourceMappingURL=index.js.map