@mastra/cloudflare 1.0.0-beta.0 → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/dist/index.cjs +98 -116
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +98 -116
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +5 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +1 -1
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +1 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/package.json +10 -8
package/dist/index.js
CHANGED
|
@@ -15,6 +15,17 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
15
15
|
if (!metadata) return void 0;
|
|
16
16
|
return typeof metadata === "string" ? JSON.parse(metadata) : metadata;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Summarizes message content without exposing raw data (for logging).
|
|
20
|
+
* Returns type, length, and keys only to prevent PII leakage.
|
|
21
|
+
*/
|
|
22
|
+
summarizeMessageContent(content) {
|
|
23
|
+
if (!content) return { type: "undefined" };
|
|
24
|
+
if (typeof content === "string") return { type: "string", length: content.length };
|
|
25
|
+
if (Array.isArray(content)) return { type: "array", length: content.length };
|
|
26
|
+
if (typeof content === "object") return { type: "object", keys: Object.keys(content) };
|
|
27
|
+
return { type: typeof content };
|
|
28
|
+
}
|
|
18
29
|
async getThreadById({ threadId }) {
|
|
19
30
|
const thread = await this.operations.load({ tableName: TABLE_THREADS, keys: { id: threadId } });
|
|
20
31
|
if (!thread) return null;
|
|
@@ -153,7 +164,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
153
164
|
return this.operations.getKey(TABLE_MESSAGES, { threadId, id: messageId });
|
|
154
165
|
} catch (error) {
|
|
155
166
|
const message = error instanceof Error ? error.message : String(error);
|
|
156
|
-
this.logger
|
|
167
|
+
this.logger?.error(`Error getting message key for thread ${threadId} and message ${messageId}:`, { message });
|
|
157
168
|
throw error;
|
|
158
169
|
}
|
|
159
170
|
}
|
|
@@ -162,7 +173,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
162
173
|
return this.operations.getKey(TABLE_MESSAGES, { threadId, id: "messages" });
|
|
163
174
|
} catch (error) {
|
|
164
175
|
const message = error instanceof Error ? error.message : String(error);
|
|
165
|
-
this.logger
|
|
176
|
+
this.logger?.error(`Error getting thread messages key for thread ${threadId}:`, { message });
|
|
166
177
|
throw error;
|
|
167
178
|
}
|
|
168
179
|
}
|
|
@@ -252,7 +263,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
252
263
|
});
|
|
253
264
|
} catch (error) {
|
|
254
265
|
const message = error instanceof Error ? error.message : String(error);
|
|
255
|
-
this.logger
|
|
266
|
+
this.logger?.error(`Error updating sorted order for key ${orderKey}:`, { message });
|
|
256
267
|
throw error;
|
|
257
268
|
} finally {
|
|
258
269
|
if (this.updateQueue.get(orderKey) === nextPromise) {
|
|
@@ -270,7 +281,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
270
281
|
const arr = JSON.parse(typeof raw === "string" ? raw : JSON.stringify(raw));
|
|
271
282
|
return Array.isArray(arr) ? arr : [];
|
|
272
283
|
} catch (e) {
|
|
273
|
-
this.logger
|
|
284
|
+
this.logger?.error(`Error parsing order data for key ${orderKey}:`, { e });
|
|
274
285
|
return [];
|
|
275
286
|
}
|
|
276
287
|
}
|
|
@@ -325,9 +336,11 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
325
336
|
const messageMigrationTasks = [];
|
|
326
337
|
for (const message of validatedMessages) {
|
|
327
338
|
const existingMessage = await this.findMessageInAnyThread(message.id);
|
|
328
|
-
|
|
339
|
+
this.logger?.debug(
|
|
340
|
+
`Checking message ${message.id}: existing=${existingMessage?.threadId}, new=${message.threadId}`
|
|
341
|
+
);
|
|
329
342
|
if (existingMessage && existingMessage.threadId && existingMessage.threadId !== message.threadId) {
|
|
330
|
-
|
|
343
|
+
this.logger?.debug(`Migrating message ${message.id} from ${existingMessage.threadId} to ${message.threadId}`);
|
|
331
344
|
messageMigrationTasks.push(this.migrateMessage(message.id, existingMessage.threadId, message.threadId));
|
|
332
345
|
}
|
|
333
346
|
}
|
|
@@ -356,10 +369,8 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
356
369
|
...cleanMessage,
|
|
357
370
|
createdAt: serializeDate(cleanMessage.createdAt)
|
|
358
371
|
};
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
contentType: typeof serializedMessage.content,
|
|
362
|
-
isArray: Array.isArray(serializedMessage.content)
|
|
372
|
+
this.logger?.debug(`Saving message ${message.id}`, {
|
|
373
|
+
contentSummary: this.summarizeMessageContent(serializedMessage.content)
|
|
363
374
|
});
|
|
364
375
|
await this.operations.putKV({ tableName: TABLE_MESSAGES, key, value: serializedMessage });
|
|
365
376
|
})
|
|
@@ -458,7 +469,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
458
469
|
const latestIds = await this.getLastN(threadMessagesKey, limit);
|
|
459
470
|
latestIds.forEach((id) => messageIds.add(id));
|
|
460
471
|
} catch {
|
|
461
|
-
|
|
472
|
+
this.logger?.debug(`No message order found for thread ${threadId}, skipping latest messages`);
|
|
462
473
|
}
|
|
463
474
|
}
|
|
464
475
|
async fetchAndParseMessagesFromMultipleThreads(messageIds, include, targetThreadId) {
|
|
@@ -489,15 +500,13 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
489
500
|
const data = await this.operations.getKV(TABLE_MESSAGES, key);
|
|
490
501
|
if (!data) return null;
|
|
491
502
|
const parsed = typeof data === "string" ? JSON.parse(data) : data;
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
contentType: typeof parsed.content,
|
|
495
|
-
isArray: Array.isArray(parsed.content)
|
|
503
|
+
this.logger?.debug(`Retrieved message ${id} from thread ${threadId}`, {
|
|
504
|
+
contentSummary: this.summarizeMessageContent(parsed.content)
|
|
496
505
|
});
|
|
497
506
|
return parsed;
|
|
498
507
|
} catch (error) {
|
|
499
508
|
const message = error instanceof Error ? error.message : String(error);
|
|
500
|
-
this.logger
|
|
509
|
+
this.logger?.error(`Error retrieving message ${id}:`, { message });
|
|
501
510
|
return null;
|
|
502
511
|
}
|
|
503
512
|
})
|
|
@@ -563,69 +572,32 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
563
572
|
);
|
|
564
573
|
}
|
|
565
574
|
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
allIds.forEach((id) => messageIds.add(id));
|
|
573
|
-
} catch {
|
|
574
|
-
}
|
|
575
|
-
} else {
|
|
576
|
-
if (perPage > 0) {
|
|
577
|
-
try {
|
|
578
|
-
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
579
|
-
const fullOrder = await this.getFullOrder(threadMessagesKey);
|
|
580
|
-
const totalMessages = fullOrder.length;
|
|
581
|
-
let start;
|
|
582
|
-
let end;
|
|
583
|
-
if (direction === "ASC") {
|
|
584
|
-
start = offset;
|
|
585
|
-
end = Math.min(offset + perPage - 1, totalMessages - 1);
|
|
586
|
-
} else {
|
|
587
|
-
start = Math.max(totalMessages - offset - perPage, 0);
|
|
588
|
-
end = totalMessages - offset - 1;
|
|
589
|
-
}
|
|
590
|
-
const paginatedIds = await this.getRange(threadMessagesKey, start, end);
|
|
591
|
-
paginatedIds.forEach((id) => messageIds.add(id));
|
|
592
|
-
} catch {
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
if (include && include.length > 0) {
|
|
597
|
-
await this.getIncludedMessagesWithContext(threadId, include, messageIds);
|
|
575
|
+
const threadMessageIds = /* @__PURE__ */ new Set();
|
|
576
|
+
try {
|
|
577
|
+
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
578
|
+
const allIds = await this.getFullOrder(threadMessagesKey);
|
|
579
|
+
allIds.forEach((id) => threadMessageIds.add(id));
|
|
580
|
+
} catch {
|
|
598
581
|
}
|
|
599
|
-
const
|
|
600
|
-
Array.from(
|
|
601
|
-
|
|
602
|
-
|
|
582
|
+
const threadMessages = await this.fetchAndParseMessagesFromMultipleThreads(
|
|
583
|
+
Array.from(threadMessageIds),
|
|
584
|
+
void 0,
|
|
585
|
+
threadId
|
|
603
586
|
);
|
|
604
|
-
let
|
|
587
|
+
let filteredThreadMessages = threadMessages;
|
|
605
588
|
if (resourceId) {
|
|
606
|
-
|
|
589
|
+
filteredThreadMessages = filteredThreadMessages.filter((msg) => msg.resourceId === resourceId);
|
|
607
590
|
}
|
|
608
591
|
const dateRange = filter?.dateRange;
|
|
609
592
|
if (dateRange) {
|
|
610
|
-
|
|
593
|
+
filteredThreadMessages = filteredThreadMessages.filter((msg) => {
|
|
611
594
|
const messageDate = new Date(msg.createdAt);
|
|
612
595
|
if (dateRange.start && messageDate < new Date(dateRange.start)) return false;
|
|
613
596
|
if (dateRange.end && messageDate > new Date(dateRange.end)) return false;
|
|
614
597
|
return true;
|
|
615
598
|
});
|
|
616
599
|
}
|
|
617
|
-
|
|
618
|
-
if (hasFilters) {
|
|
619
|
-
total = filteredMessages.length;
|
|
620
|
-
} else {
|
|
621
|
-
try {
|
|
622
|
-
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
623
|
-
const fullOrder = await this.getFullOrder(threadMessagesKey);
|
|
624
|
-
total = fullOrder.length;
|
|
625
|
-
} catch {
|
|
626
|
-
total = filteredMessages.length;
|
|
627
|
-
}
|
|
628
|
-
}
|
|
600
|
+
const total = filteredThreadMessages.length;
|
|
629
601
|
if (perPage === 0 && (!include || include.length === 0)) {
|
|
630
602
|
return {
|
|
631
603
|
messages: [],
|
|
@@ -635,45 +607,58 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
635
607
|
hasMore: offset < total
|
|
636
608
|
};
|
|
637
609
|
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
filteredMessages = filteredMessages.slice(start, end);
|
|
610
|
+
filteredThreadMessages.sort((a, b) => {
|
|
611
|
+
const timeA = new Date(a.createdAt).getTime();
|
|
612
|
+
const timeB = new Date(b.createdAt).getTime();
|
|
613
|
+
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
614
|
+
if (timeDiff === 0) {
|
|
615
|
+
return a.id.localeCompare(b.id);
|
|
645
616
|
}
|
|
617
|
+
return timeDiff;
|
|
618
|
+
});
|
|
619
|
+
let paginatedMessages;
|
|
620
|
+
if (perPage === 0) {
|
|
621
|
+
paginatedMessages = [];
|
|
622
|
+
} else if (perPage === Number.MAX_SAFE_INTEGER) {
|
|
623
|
+
paginatedMessages = filteredThreadMessages;
|
|
624
|
+
} else {
|
|
625
|
+
paginatedMessages = filteredThreadMessages.slice(offset, offset + perPage);
|
|
646
626
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
const
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
return timeDiff;
|
|
665
|
-
});
|
|
666
|
-
} catch {
|
|
667
|
-
filteredMessages.sort((a, b) => {
|
|
668
|
-
const timeA = new Date(a.createdAt).getTime();
|
|
669
|
-
const timeB = new Date(b.createdAt).getTime();
|
|
670
|
-
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
671
|
-
if (timeDiff === 0) {
|
|
672
|
-
return a.id.localeCompare(b.id);
|
|
673
|
-
}
|
|
674
|
-
return timeDiff;
|
|
675
|
-
});
|
|
627
|
+
let includedMessages = [];
|
|
628
|
+
if (include && include.length > 0) {
|
|
629
|
+
const includedMessageIds = /* @__PURE__ */ new Set();
|
|
630
|
+
await this.getIncludedMessagesWithContext(threadId, include, includedMessageIds);
|
|
631
|
+
const paginatedIds = new Set(paginatedMessages.map((m) => m.id));
|
|
632
|
+
const idsToFetch = Array.from(includedMessageIds).filter((id) => !paginatedIds.has(id));
|
|
633
|
+
if (idsToFetch.length > 0) {
|
|
634
|
+
includedMessages = await this.fetchAndParseMessagesFromMultipleThreads(idsToFetch, include, void 0);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
const seenIds = /* @__PURE__ */ new Set();
|
|
638
|
+
const allMessages = [];
|
|
639
|
+
for (const msg of paginatedMessages) {
|
|
640
|
+
if (!seenIds.has(msg.id)) {
|
|
641
|
+
allMessages.push(msg);
|
|
642
|
+
seenIds.add(msg.id);
|
|
643
|
+
}
|
|
676
644
|
}
|
|
645
|
+
for (const msg of includedMessages) {
|
|
646
|
+
if (!seenIds.has(msg.id)) {
|
|
647
|
+
allMessages.push(msg);
|
|
648
|
+
seenIds.add(msg.id);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
allMessages.sort((a, b) => {
|
|
652
|
+
const timeA = new Date(a.createdAt).getTime();
|
|
653
|
+
const timeB = new Date(b.createdAt).getTime();
|
|
654
|
+
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
655
|
+
if (timeDiff === 0) {
|
|
656
|
+
return a.id.localeCompare(b.id);
|
|
657
|
+
}
|
|
658
|
+
return timeDiff;
|
|
659
|
+
});
|
|
660
|
+
let filteredMessages = allMessages;
|
|
661
|
+
const paginatedCount = paginatedMessages.length;
|
|
677
662
|
if (total === 0 && filteredMessages.length === 0 && (!include || include.length === 0)) {
|
|
678
663
|
return {
|
|
679
664
|
messages: [],
|
|
@@ -706,14 +691,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
706
691
|
});
|
|
707
692
|
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
708
693
|
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
709
|
-
|
|
710
|
-
if (perPageInput === false || allThreadMessagesReturned) {
|
|
711
|
-
hasMore = false;
|
|
712
|
-
} else if (direction === "ASC") {
|
|
713
|
-
hasMore = offset + paginatedCount < total;
|
|
714
|
-
} else {
|
|
715
|
-
hasMore = total - offset - perPage > 0;
|
|
716
|
-
}
|
|
694
|
+
const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + paginatedCount < total;
|
|
717
695
|
return {
|
|
718
696
|
messages: finalMessages,
|
|
719
697
|
total,
|
|
@@ -1820,7 +1798,7 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
|
1820
1798
|
workflow_name: workflowName,
|
|
1821
1799
|
run_id: runId,
|
|
1822
1800
|
resourceId,
|
|
1823
|
-
snapshot:
|
|
1801
|
+
snapshot: JSON.stringify(snapshot),
|
|
1824
1802
|
createdAt: /* @__PURE__ */ new Date(),
|
|
1825
1803
|
updatedAt: /* @__PURE__ */ new Date()
|
|
1826
1804
|
}
|
|
@@ -1905,7 +1883,8 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
|
1905
1883
|
perPage = 20,
|
|
1906
1884
|
resourceId,
|
|
1907
1885
|
fromDate,
|
|
1908
|
-
toDate
|
|
1886
|
+
toDate,
|
|
1887
|
+
status
|
|
1909
1888
|
} = {}) {
|
|
1910
1889
|
try {
|
|
1911
1890
|
if (page < 0 || !Number.isInteger(page)) {
|
|
@@ -1936,10 +1915,11 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
|
1936
1915
|
if (!data) continue;
|
|
1937
1916
|
try {
|
|
1938
1917
|
if (resourceId && !keyResourceId) continue;
|
|
1918
|
+
const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
|
|
1919
|
+
if (status && snapshotData.status !== status) continue;
|
|
1939
1920
|
const createdAt = ensureDate(data.createdAt);
|
|
1940
1921
|
if (fromDate && createdAt && createdAt < fromDate) continue;
|
|
1941
1922
|
if (toDate && createdAt && createdAt > toDate) continue;
|
|
1942
|
-
const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
|
|
1943
1923
|
const resourceIdToUse = keyResourceId || data.resourceId;
|
|
1944
1924
|
const run = this.parseWorkflowRun({
|
|
1945
1925
|
...data,
|
|
@@ -2190,7 +2170,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2190
2170
|
page = 0,
|
|
2191
2171
|
resourceId,
|
|
2192
2172
|
fromDate,
|
|
2193
|
-
toDate
|
|
2173
|
+
toDate,
|
|
2174
|
+
status
|
|
2194
2175
|
} = {}) {
|
|
2195
2176
|
return this.stores.workflows.listWorkflowRuns({
|
|
2196
2177
|
workflowName,
|
|
@@ -2198,7 +2179,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2198
2179
|
page,
|
|
2199
2180
|
resourceId,
|
|
2200
2181
|
fromDate,
|
|
2201
|
-
toDate
|
|
2182
|
+
toDate,
|
|
2183
|
+
status
|
|
2202
2184
|
});
|
|
2203
2185
|
}
|
|
2204
2186
|
async getWorkflowRunById({
|