@mastra/cloudflare 0.13.2 → 1.0.0-beta.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/CHANGELOG.md +221 -15
- package/README.md +37 -15
- package/dist/index.cjs +300 -550
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +300 -550
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +13 -36
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/operations/index.d.ts +0 -1
- package/dist/storage/domains/operations/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +5 -5
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +3 -10
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +23 -79
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/types.d.ts +9 -6
- package/dist/storage/types.d.ts.map +1 -1
- package/package.json +8 -12
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -21
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -18
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,110 +1,10 @@
|
|
|
1
1
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
|
-
import { MastraStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT,
|
|
2
|
+
import { MastraStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT, TABLE_SCORERS, StoreOperations, TABLE_TRACES, WorkflowsStorage, ensureDate, normalizePerPage, MemoryStorage, calculatePagination, serializeDate, TABLE_RESOURCES, ScoresStorage, safelyParseJSON } from '@mastra/core/storage';
|
|
3
3
|
import Cloudflare from 'cloudflare';
|
|
4
4
|
import { MessageList } from '@mastra/core/agent';
|
|
5
|
-
import { saveScorePayloadSchema } from '@mastra/core/
|
|
5
|
+
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
6
6
|
|
|
7
7
|
// src/storage/index.ts
|
|
8
|
-
var LegacyEvalsStorageCloudflare = class extends LegacyEvalsStorage {
|
|
9
|
-
operations;
|
|
10
|
-
constructor({ operations }) {
|
|
11
|
-
super();
|
|
12
|
-
this.operations = operations;
|
|
13
|
-
}
|
|
14
|
-
async getEvalsByAgentName(agentName, type) {
|
|
15
|
-
try {
|
|
16
|
-
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
17
|
-
const keyObjs = await this.operations.listKV(TABLE_EVALS, { prefix: `${prefix}${TABLE_EVALS}` });
|
|
18
|
-
const evals = [];
|
|
19
|
-
for (const { name: key } of keyObjs) {
|
|
20
|
-
const data = await this.operations.getKV(TABLE_EVALS, key);
|
|
21
|
-
if (!data) continue;
|
|
22
|
-
if (data.agent_name !== agentName) continue;
|
|
23
|
-
if (type) {
|
|
24
|
-
const isTest = data.test_info !== null && data.test_info !== void 0;
|
|
25
|
-
const evalType = isTest ? "test" : "live";
|
|
26
|
-
if (evalType !== type) continue;
|
|
27
|
-
}
|
|
28
|
-
const mappedData = {
|
|
29
|
-
...data,
|
|
30
|
-
runId: data.run_id,
|
|
31
|
-
testInfo: data.test_info
|
|
32
|
-
};
|
|
33
|
-
evals.push(mappedData);
|
|
34
|
-
}
|
|
35
|
-
evals.sort((a, b) => {
|
|
36
|
-
const aTime = new Date(a.createdAt || 0).getTime();
|
|
37
|
-
const bTime = new Date(b.createdAt || 0).getTime();
|
|
38
|
-
return bTime - aTime;
|
|
39
|
-
});
|
|
40
|
-
return evals;
|
|
41
|
-
} catch (error) {
|
|
42
|
-
throw new MastraError(
|
|
43
|
-
{
|
|
44
|
-
id: "CLOUDFLARE_STORAGE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
45
|
-
domain: ErrorDomain.STORAGE,
|
|
46
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
47
|
-
text: "Failed to get evals by agent name"
|
|
48
|
-
},
|
|
49
|
-
error
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
async getEvals(options) {
|
|
54
|
-
try {
|
|
55
|
-
const { agentName, type, page = 0, perPage = 100, dateRange } = options;
|
|
56
|
-
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
57
|
-
const keyObjs = await this.operations.listKV(TABLE_EVALS, { prefix: `${prefix}${TABLE_EVALS}` });
|
|
58
|
-
const evals = [];
|
|
59
|
-
for (const { name: key } of keyObjs) {
|
|
60
|
-
const data = await this.operations.getKV(TABLE_EVALS, key);
|
|
61
|
-
if (!data) continue;
|
|
62
|
-
if (agentName && data.agent_name !== agentName) continue;
|
|
63
|
-
if (type) {
|
|
64
|
-
const isTest = data.test_info !== null && data.test_info !== void 0;
|
|
65
|
-
const evalType = isTest ? "test" : "live";
|
|
66
|
-
if (evalType !== type) continue;
|
|
67
|
-
}
|
|
68
|
-
if (dateRange?.start || dateRange?.end) {
|
|
69
|
-
const evalDate = new Date(data.createdAt || data.created_at || 0);
|
|
70
|
-
if (dateRange.start && evalDate < dateRange.start) continue;
|
|
71
|
-
if (dateRange.end && evalDate > dateRange.end) continue;
|
|
72
|
-
}
|
|
73
|
-
const mappedData = {
|
|
74
|
-
...data,
|
|
75
|
-
runId: data.run_id,
|
|
76
|
-
testInfo: data.test_info
|
|
77
|
-
};
|
|
78
|
-
evals.push(mappedData);
|
|
79
|
-
}
|
|
80
|
-
evals.sort((a, b) => {
|
|
81
|
-
const aTime = new Date(a.createdAt || 0).getTime();
|
|
82
|
-
const bTime = new Date(b.createdAt || 0).getTime();
|
|
83
|
-
return bTime - aTime;
|
|
84
|
-
});
|
|
85
|
-
const start = page * perPage;
|
|
86
|
-
const end = start + perPage;
|
|
87
|
-
const paginatedEvals = evals.slice(start, end);
|
|
88
|
-
return {
|
|
89
|
-
page,
|
|
90
|
-
perPage,
|
|
91
|
-
total: evals.length,
|
|
92
|
-
hasMore: start + perPage < evals.length,
|
|
93
|
-
evals: paginatedEvals
|
|
94
|
-
};
|
|
95
|
-
} catch (error) {
|
|
96
|
-
throw new MastraError(
|
|
97
|
-
{
|
|
98
|
-
id: "CLOUDFLARE_STORAGE_GET_EVALS_FAILED",
|
|
99
|
-
domain: ErrorDomain.STORAGE,
|
|
100
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
101
|
-
text: "Failed to get evals"
|
|
102
|
-
},
|
|
103
|
-
error
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
8
|
var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
109
9
|
operations;
|
|
110
10
|
constructor({ operations }) {
|
|
@@ -142,61 +42,23 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
142
42
|
return null;
|
|
143
43
|
}
|
|
144
44
|
}
|
|
145
|
-
async
|
|
45
|
+
async listThreadsByResourceId(args) {
|
|
146
46
|
try {
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const mastraError = new MastraError(
|
|
163
|
-
{
|
|
164
|
-
id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
165
|
-
domain: ErrorDomain.STORAGE,
|
|
166
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
167
|
-
details: {
|
|
168
|
-
resourceId
|
|
169
|
-
}
|
|
170
|
-
},
|
|
171
|
-
error
|
|
172
|
-
);
|
|
173
|
-
this.logger?.trackException(mastraError);
|
|
174
|
-
this.logger?.error(mastraError.toString());
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
})
|
|
178
|
-
);
|
|
179
|
-
return threads.filter((thread) => thread !== null);
|
|
180
|
-
} catch (error) {
|
|
181
|
-
const mastraError = new MastraError(
|
|
182
|
-
{
|
|
183
|
-
id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
184
|
-
domain: ErrorDomain.STORAGE,
|
|
185
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
186
|
-
details: {
|
|
187
|
-
resourceId
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
error
|
|
191
|
-
);
|
|
192
|
-
this.logger?.trackException(mastraError);
|
|
193
|
-
this.logger?.error(mastraError.toString());
|
|
194
|
-
return [];
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
198
|
-
try {
|
|
199
|
-
const { resourceId, page = 0, perPage = 100 } = args;
|
|
47
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
48
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
49
|
+
if (page < 0) {
|
|
50
|
+
throw new MastraError(
|
|
51
|
+
{
|
|
52
|
+
id: "STORAGE_CLOUDFLARE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
53
|
+
domain: ErrorDomain.STORAGE,
|
|
54
|
+
category: ErrorCategory.USER,
|
|
55
|
+
details: { page }
|
|
56
|
+
},
|
|
57
|
+
new Error("page must be >= 0")
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
61
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
200
62
|
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
201
63
|
const keyObjs = await this.operations.listKV(TABLE_THREADS, { prefix: `${prefix}${TABLE_THREADS}` });
|
|
202
64
|
const threads = [];
|
|
@@ -207,24 +69,23 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
207
69
|
threads.push(data);
|
|
208
70
|
}
|
|
209
71
|
threads.sort((a, b) => {
|
|
210
|
-
const aTime = new Date(a
|
|
211
|
-
const bTime = new Date(b
|
|
212
|
-
return bTime - aTime;
|
|
72
|
+
const aTime = new Date(a[field] || 0).getTime();
|
|
73
|
+
const bTime = new Date(b[field] || 0).getTime();
|
|
74
|
+
return direction === "ASC" ? aTime - bTime : bTime - aTime;
|
|
213
75
|
});
|
|
214
|
-
const
|
|
215
|
-
const
|
|
216
|
-
const paginatedThreads = threads.slice(start, end);
|
|
76
|
+
const end = perPageInput === false ? threads.length : offset + perPage;
|
|
77
|
+
const paginatedThreads = threads.slice(offset, end);
|
|
217
78
|
return {
|
|
218
79
|
page,
|
|
219
|
-
perPage,
|
|
80
|
+
perPage: perPageForResponse,
|
|
220
81
|
total: threads.length,
|
|
221
|
-
hasMore:
|
|
82
|
+
hasMore: perPageInput === false ? false : offset + perPage < threads.length,
|
|
222
83
|
threads: paginatedThreads
|
|
223
84
|
};
|
|
224
85
|
} catch (error) {
|
|
225
86
|
throw new MastraError(
|
|
226
87
|
{
|
|
227
|
-
id: "
|
|
88
|
+
id: "CLOUDFLARE_STORAGE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
228
89
|
domain: ErrorDomain.STORAGE,
|
|
229
90
|
category: ErrorCategory.THIRD_PARTY,
|
|
230
91
|
text: "Failed to get threads by resource ID with pagination"
|
|
@@ -440,8 +301,8 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
440
301
|
}
|
|
441
302
|
}
|
|
442
303
|
async saveMessages(args) {
|
|
443
|
-
const { messages
|
|
444
|
-
if (!Array.isArray(messages) || messages.length === 0) return [];
|
|
304
|
+
const { messages } = args;
|
|
305
|
+
if (!Array.isArray(messages) || messages.length === 0) return { messages: [] };
|
|
445
306
|
try {
|
|
446
307
|
const validatedMessages = messages.map((message, index) => {
|
|
447
308
|
const errors = [];
|
|
@@ -534,8 +395,7 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
534
395
|
({ _index, ...message }) => ({ ...message, type: message.type !== "v2" ? message.type : void 0 })
|
|
535
396
|
);
|
|
536
397
|
const list = new MessageList().add(prepared, "memory");
|
|
537
|
-
|
|
538
|
-
return list.get.all.v1();
|
|
398
|
+
return { messages: list.get.all.db() };
|
|
539
399
|
} catch (error) {
|
|
540
400
|
throw new MastraError(
|
|
541
401
|
{
|
|
@@ -644,96 +504,8 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
644
504
|
);
|
|
645
505
|
return messages.filter((msg) => msg !== null);
|
|
646
506
|
}
|
|
647
|
-
async
|
|
648
|
-
|
|
649
|
-
resourceId,
|
|
650
|
-
selectBy,
|
|
651
|
-
format
|
|
652
|
-
}) {
|
|
653
|
-
console.info(`getMessages called with format: ${format}, threadId: ${threadId}`);
|
|
654
|
-
const actualFormat = format || "v1";
|
|
655
|
-
console.info(`Using format: ${actualFormat}`);
|
|
656
|
-
const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
657
|
-
const messageIds = /* @__PURE__ */ new Set();
|
|
658
|
-
if (limit === 0 && !selectBy?.include?.length) return [];
|
|
659
|
-
try {
|
|
660
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
661
|
-
await Promise.all([
|
|
662
|
-
selectBy?.include?.length ? this.getIncludedMessagesWithContext(threadId, selectBy.include, messageIds) : Promise.resolve(),
|
|
663
|
-
limit > 0 ? this.getRecentMessages(threadId, limit, messageIds) : Promise.resolve()
|
|
664
|
-
]);
|
|
665
|
-
const targetThreadId = selectBy?.include?.length ? void 0 : threadId;
|
|
666
|
-
const messages = await this.fetchAndParseMessagesFromMultipleThreads(
|
|
667
|
-
Array.from(messageIds),
|
|
668
|
-
selectBy?.include,
|
|
669
|
-
targetThreadId
|
|
670
|
-
);
|
|
671
|
-
if (!messages.length) return [];
|
|
672
|
-
try {
|
|
673
|
-
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
674
|
-
const messageOrder = await this.getFullOrder(threadMessagesKey);
|
|
675
|
-
const orderMap = new Map(messageOrder.map((id, index) => [id, index]));
|
|
676
|
-
messages.sort((a, b) => {
|
|
677
|
-
const indexA = orderMap.get(a.id);
|
|
678
|
-
const indexB = orderMap.get(b.id);
|
|
679
|
-
if (indexA !== void 0 && indexB !== void 0) return orderMap.get(a.id) - orderMap.get(b.id);
|
|
680
|
-
return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
|
|
681
|
-
});
|
|
682
|
-
} catch (error) {
|
|
683
|
-
const mastraError = new MastraError(
|
|
684
|
-
{
|
|
685
|
-
id: "CLOUDFLARE_STORAGE_SORT_MESSAGES_FAILED",
|
|
686
|
-
domain: ErrorDomain.STORAGE,
|
|
687
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
688
|
-
text: `Error sorting messages for thread ${threadId} falling back to creation time`,
|
|
689
|
-
details: {
|
|
690
|
-
threadId
|
|
691
|
-
}
|
|
692
|
-
},
|
|
693
|
-
error
|
|
694
|
-
);
|
|
695
|
-
this.logger?.trackException(mastraError);
|
|
696
|
-
this.logger?.error(mastraError.toString());
|
|
697
|
-
messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
698
|
-
}
|
|
699
|
-
const prepared = messages.map(({ _index, ...message }) => ({
|
|
700
|
-
...message,
|
|
701
|
-
type: message.type === `v2` ? void 0 : message.type,
|
|
702
|
-
createdAt: ensureDate(message.createdAt)
|
|
703
|
-
}));
|
|
704
|
-
if (actualFormat === `v1`) {
|
|
705
|
-
console.info(`Processing ${prepared.length} messages for v1 format - returning directly without MessageList`);
|
|
706
|
-
return prepared.map((msg) => ({
|
|
707
|
-
...msg,
|
|
708
|
-
createdAt: new Date(msg.createdAt)
|
|
709
|
-
}));
|
|
710
|
-
}
|
|
711
|
-
const list = new MessageList({ threadId, resourceId }).add(prepared, "memory");
|
|
712
|
-
return list.get.all.v2();
|
|
713
|
-
} catch (error) {
|
|
714
|
-
const mastraError = new MastraError(
|
|
715
|
-
{
|
|
716
|
-
id: "CLOUDFLARE_STORAGE_GET_MESSAGES_FAILED",
|
|
717
|
-
domain: ErrorDomain.STORAGE,
|
|
718
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
719
|
-
text: `Error retrieving messages for thread ${threadId}`,
|
|
720
|
-
details: {
|
|
721
|
-
threadId,
|
|
722
|
-
resourceId: resourceId ?? ""
|
|
723
|
-
}
|
|
724
|
-
},
|
|
725
|
-
error
|
|
726
|
-
);
|
|
727
|
-
this.logger?.trackException(mastraError);
|
|
728
|
-
this.logger?.error(mastraError.toString());
|
|
729
|
-
return [];
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
async getMessagesById({
|
|
733
|
-
messageIds,
|
|
734
|
-
format
|
|
735
|
-
}) {
|
|
736
|
-
if (messageIds.length === 0) return [];
|
|
507
|
+
async listMessagesById({ messageIds }) {
|
|
508
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
737
509
|
try {
|
|
738
510
|
const messages = (await Promise.all(messageIds.map((id) => this.findMessageInAnyThread(id)))).filter(
|
|
739
511
|
(result) => !!result
|
|
@@ -744,12 +516,11 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
744
516
|
createdAt: ensureDate(message.createdAt)
|
|
745
517
|
}));
|
|
746
518
|
const list = new MessageList().add(prepared, "memory");
|
|
747
|
-
|
|
748
|
-
return list.get.all.v2();
|
|
519
|
+
return { messages: list.get.all.db() };
|
|
749
520
|
} catch (error) {
|
|
750
521
|
const mastraError = new MastraError(
|
|
751
522
|
{
|
|
752
|
-
id: "
|
|
523
|
+
id: "CLOUDFLARE_STORAGE_LIST_MESSAGES_BY_ID_FAILED",
|
|
753
524
|
domain: ErrorDomain.STORAGE,
|
|
754
525
|
category: ErrorCategory.THIRD_PARTY,
|
|
755
526
|
text: `Error retrieving messages by ID`,
|
|
@@ -761,42 +532,202 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
761
532
|
);
|
|
762
533
|
this.logger?.trackException(mastraError);
|
|
763
534
|
this.logger?.error(mastraError.toString());
|
|
764
|
-
return [];
|
|
535
|
+
return { messages: [] };
|
|
765
536
|
}
|
|
766
537
|
}
|
|
767
|
-
async
|
|
768
|
-
const { threadId, resourceId,
|
|
769
|
-
|
|
538
|
+
async listMessages(args) {
|
|
539
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
540
|
+
if (!threadId.trim()) {
|
|
541
|
+
throw new MastraError(
|
|
542
|
+
{
|
|
543
|
+
id: "STORAGE_CLOUDFLARE_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
544
|
+
domain: ErrorDomain.STORAGE,
|
|
545
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
546
|
+
details: { threadId }
|
|
547
|
+
},
|
|
548
|
+
new Error("threadId must be a non-empty string")
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
552
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
770
553
|
try {
|
|
771
|
-
if (
|
|
772
|
-
|
|
554
|
+
if (page < 0) {
|
|
555
|
+
throw new MastraError(
|
|
556
|
+
{
|
|
557
|
+
id: "STORAGE_CLOUDFLARE_LIST_MESSAGES_INVALID_PAGE",
|
|
558
|
+
domain: ErrorDomain.STORAGE,
|
|
559
|
+
category: ErrorCategory.USER,
|
|
560
|
+
details: { page }
|
|
561
|
+
},
|
|
562
|
+
new Error("page must be >= 0")
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
566
|
+
const messageIds = /* @__PURE__ */ new Set();
|
|
567
|
+
const hasFilters = !!resourceId || !!filter?.dateRange;
|
|
568
|
+
if (hasFilters || perPage === Number.MAX_SAFE_INTEGER) {
|
|
569
|
+
try {
|
|
570
|
+
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
571
|
+
const allIds = await this.getFullOrder(threadMessagesKey);
|
|
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);
|
|
598
|
+
}
|
|
599
|
+
const messages = await this.fetchAndParseMessagesFromMultipleThreads(
|
|
600
|
+
Array.from(messageIds),
|
|
601
|
+
include,
|
|
602
|
+
include && include.length > 0 ? void 0 : threadId
|
|
603
|
+
);
|
|
773
604
|
let filteredMessages = messages;
|
|
774
|
-
if (
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
605
|
+
if (resourceId) {
|
|
606
|
+
filteredMessages = filteredMessages.filter((msg) => msg.resourceId === resourceId);
|
|
607
|
+
}
|
|
608
|
+
const dateRange = filter?.dateRange;
|
|
609
|
+
if (dateRange) {
|
|
610
|
+
filteredMessages = filteredMessages.filter((msg) => {
|
|
611
|
+
const messageDate = new Date(msg.createdAt);
|
|
612
|
+
if (dateRange.start && messageDate < new Date(dateRange.start)) return false;
|
|
613
|
+
if (dateRange.end && messageDate > new Date(dateRange.end)) return false;
|
|
780
614
|
return true;
|
|
781
615
|
});
|
|
782
616
|
}
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
617
|
+
let total;
|
|
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
|
+
}
|
|
629
|
+
if (perPage === 0 && (!include || include.length === 0)) {
|
|
630
|
+
return {
|
|
631
|
+
messages: [],
|
|
632
|
+
total,
|
|
633
|
+
page,
|
|
634
|
+
perPage: perPageForResponse,
|
|
635
|
+
hasMore: offset < total
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
if (hasFilters && perPage !== Number.MAX_SAFE_INTEGER && perPage > 0) {
|
|
639
|
+
if (direction === "ASC") {
|
|
640
|
+
filteredMessages = filteredMessages.slice(offset, offset + perPage);
|
|
641
|
+
} else {
|
|
642
|
+
const start = Math.max(filteredMessages.length - offset - perPage, 0);
|
|
643
|
+
const end = filteredMessages.length - offset;
|
|
644
|
+
filteredMessages = filteredMessages.slice(start, end);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
const paginatedCount = hasFilters && perPage !== Number.MAX_SAFE_INTEGER && perPage > 0 ? filteredMessages.length : filteredMessages.length;
|
|
648
|
+
try {
|
|
649
|
+
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
650
|
+
const messageOrder = await this.getFullOrder(threadMessagesKey);
|
|
651
|
+
const orderMap = new Map(messageOrder.map((id, index) => [id, index]));
|
|
652
|
+
filteredMessages.sort((a, b) => {
|
|
653
|
+
const indexA = orderMap.get(a.id);
|
|
654
|
+
const indexB = orderMap.get(b.id);
|
|
655
|
+
if (indexA !== void 0 && indexB !== void 0) {
|
|
656
|
+
return direction === "ASC" ? indexA - indexB : indexB - indexA;
|
|
657
|
+
}
|
|
658
|
+
const timeA = new Date(a.createdAt).getTime();
|
|
659
|
+
const timeB = new Date(b.createdAt).getTime();
|
|
660
|
+
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
661
|
+
if (timeDiff === 0) {
|
|
662
|
+
return a.id.localeCompare(b.id);
|
|
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
|
+
});
|
|
676
|
+
}
|
|
677
|
+
if (total === 0 && filteredMessages.length === 0 && (!include || include.length === 0)) {
|
|
678
|
+
return {
|
|
679
|
+
messages: [],
|
|
680
|
+
total: 0,
|
|
681
|
+
page,
|
|
682
|
+
perPage: perPageForResponse,
|
|
683
|
+
hasMore: false
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
const prepared = filteredMessages.map(({ _index, ...message }) => ({
|
|
687
|
+
...message,
|
|
688
|
+
type: message.type !== "v2" ? message.type : void 0,
|
|
689
|
+
createdAt: ensureDate(message.createdAt)
|
|
690
|
+
}));
|
|
691
|
+
const list = new MessageList({ threadId, resourceId }).add(prepared, "memory");
|
|
692
|
+
let finalMessages = list.get.all.db();
|
|
693
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
694
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
695
|
+
const aVal = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
696
|
+
const bVal = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
697
|
+
if (aVal == null && bVal == null) return a.id.localeCompare(b.id);
|
|
698
|
+
if (aVal == null) return 1;
|
|
699
|
+
if (bVal == null) return -1;
|
|
700
|
+
if (typeof aVal === "number" && typeof bVal === "number") {
|
|
701
|
+
const cmp2 = direction === "ASC" ? aVal - bVal : bVal - aVal;
|
|
702
|
+
return cmp2 !== 0 ? cmp2 : a.id.localeCompare(b.id);
|
|
703
|
+
}
|
|
704
|
+
const cmp = direction === "ASC" ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal));
|
|
705
|
+
return cmp !== 0 ? cmp : a.id.localeCompare(b.id);
|
|
706
|
+
});
|
|
707
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
708
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
709
|
+
let hasMore;
|
|
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
|
+
}
|
|
786
717
|
return {
|
|
718
|
+
messages: finalMessages,
|
|
719
|
+
total,
|
|
787
720
|
page,
|
|
788
|
-
perPage,
|
|
789
|
-
|
|
790
|
-
hasMore: start + perPage < filteredMessages.length,
|
|
791
|
-
messages: paginatedMessages
|
|
721
|
+
perPage: perPageForResponse,
|
|
722
|
+
hasMore
|
|
792
723
|
};
|
|
793
724
|
} catch (error) {
|
|
794
725
|
const mastraError = new MastraError(
|
|
795
726
|
{
|
|
796
|
-
id: "
|
|
727
|
+
id: "CLOUDFLARE_STORAGE_LIST_MESSAGES_FAILED",
|
|
797
728
|
domain: ErrorDomain.STORAGE,
|
|
798
729
|
category: ErrorCategory.THIRD_PARTY,
|
|
799
|
-
text:
|
|
730
|
+
text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
800
731
|
details: {
|
|
801
732
|
threadId,
|
|
802
733
|
resourceId: resourceId ?? ""
|
|
@@ -804,9 +735,15 @@ var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
|
804
735
|
},
|
|
805
736
|
error
|
|
806
737
|
);
|
|
807
|
-
this.logger?.trackException?.(mastraError);
|
|
808
738
|
this.logger?.error?.(mastraError.toString());
|
|
809
|
-
|
|
739
|
+
this.logger?.trackException?.(mastraError);
|
|
740
|
+
return {
|
|
741
|
+
messages: [],
|
|
742
|
+
total: 0,
|
|
743
|
+
page,
|
|
744
|
+
perPage: perPageForResponse,
|
|
745
|
+
hasMore: false
|
|
746
|
+
};
|
|
810
747
|
}
|
|
811
748
|
}
|
|
812
749
|
async updateMessages(args) {
|
|
@@ -1092,10 +1029,6 @@ var StoreOperationsCloudflare = class extends StoreOperations {
|
|
|
1092
1029
|
case TABLE_TRACES:
|
|
1093
1030
|
if (!record.id) throw new Error("Trace ID is required");
|
|
1094
1031
|
return `${prefix}${tableName}:${record.id}`;
|
|
1095
|
-
case TABLE_EVALS:
|
|
1096
|
-
const evalId = record.id || record.run_id;
|
|
1097
|
-
if (!evalId) throw new Error("Eval ID or run_id is required");
|
|
1098
|
-
return `${prefix}${tableName}:${evalId}`;
|
|
1099
1032
|
case TABLE_SCORERS:
|
|
1100
1033
|
if (!record.id) throw new Error("Score ID is required");
|
|
1101
1034
|
return `${prefix}${tableName}:${record.id}`;
|
|
@@ -1335,11 +1268,6 @@ var StoreOperationsCloudflare = class extends StoreOperations {
|
|
|
1335
1268
|
throw new Error("Trace record missing required fields");
|
|
1336
1269
|
}
|
|
1337
1270
|
break;
|
|
1338
|
-
case TABLE_EVALS:
|
|
1339
|
-
if (!("agent_name" in recordTyped) || !("run_id" in recordTyped)) {
|
|
1340
|
-
throw new Error("Eval record missing required fields");
|
|
1341
|
-
}
|
|
1342
|
-
break;
|
|
1343
1271
|
case TABLE_SCORERS:
|
|
1344
1272
|
if (!("id" in recordTyped) || !("scorerId" in recordTyped)) {
|
|
1345
1273
|
throw new Error("Score record missing required fields");
|
|
@@ -1357,12 +1285,7 @@ var StoreOperationsCloudflare = class extends StoreOperations {
|
|
|
1357
1285
|
async insert({ tableName, record }) {
|
|
1358
1286
|
try {
|
|
1359
1287
|
const key = this.getKey(tableName, record);
|
|
1360
|
-
const processedRecord = {
|
|
1361
|
-
...record,
|
|
1362
|
-
createdAt: record.createdAt ? serializeDate(record.createdAt) : void 0,
|
|
1363
|
-
updatedAt: record.updatedAt ? serializeDate(record.updatedAt) : void 0,
|
|
1364
|
-
metadata: record.metadata ? JSON.stringify(record.metadata) : ""
|
|
1365
|
-
};
|
|
1288
|
+
const processedRecord = { ...record };
|
|
1366
1289
|
await this.validateRecord(processedRecord, tableName);
|
|
1367
1290
|
await this.putKV({ tableName, key, value: processedRecord });
|
|
1368
1291
|
} catch (error) {
|
|
@@ -1379,22 +1302,12 @@ var StoreOperationsCloudflare = class extends StoreOperations {
|
|
|
1379
1302
|
);
|
|
1380
1303
|
}
|
|
1381
1304
|
}
|
|
1382
|
-
ensureMetadata(metadata) {
|
|
1383
|
-
if (!metadata) return {};
|
|
1384
|
-
return typeof metadata === "string" ? JSON.parse(metadata) : metadata;
|
|
1385
|
-
}
|
|
1386
1305
|
async load({ tableName, keys }) {
|
|
1387
1306
|
try {
|
|
1388
1307
|
const key = this.getKey(tableName, keys);
|
|
1389
1308
|
const data = await this.getKV(tableName, key);
|
|
1390
1309
|
if (!data) return null;
|
|
1391
|
-
|
|
1392
|
-
...data,
|
|
1393
|
-
createdAt: ensureDate(data.createdAt),
|
|
1394
|
-
updatedAt: ensureDate(data.updatedAt),
|
|
1395
|
-
metadata: this.ensureMetadata(data.metadata)
|
|
1396
|
-
};
|
|
1397
|
-
return processed;
|
|
1310
|
+
return data;
|
|
1398
1311
|
} catch (error) {
|
|
1399
1312
|
const mastraError = new MastraError(
|
|
1400
1313
|
{
|
|
@@ -1418,13 +1331,7 @@ var StoreOperationsCloudflare = class extends StoreOperations {
|
|
|
1418
1331
|
await Promise.all(
|
|
1419
1332
|
input.records.map(async (record) => {
|
|
1420
1333
|
const key = this.getKey(input.tableName, record);
|
|
1421
|
-
|
|
1422
|
-
...record,
|
|
1423
|
-
createdAt: record.createdAt ? serializeDate(record.createdAt) : void 0,
|
|
1424
|
-
updatedAt: record.updatedAt ? serializeDate(record.updatedAt) : void 0,
|
|
1425
|
-
metadata: record.metadata ? JSON.stringify(record.metadata) : void 0
|
|
1426
|
-
};
|
|
1427
|
-
await this.putKV({ tableName: input.tableName, key, value: processedRecord });
|
|
1334
|
+
await this.putKV({ tableName: input.tableName, key, value: record });
|
|
1428
1335
|
})
|
|
1429
1336
|
);
|
|
1430
1337
|
} catch (error) {
|
|
@@ -1582,7 +1489,7 @@ function transformScoreRow(row) {
|
|
|
1582
1489
|
deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
|
|
1583
1490
|
deserialized.metadata = safelyParseJSON(row.metadata);
|
|
1584
1491
|
deserialized.additionalContext = safelyParseJSON(row.additionalContext);
|
|
1585
|
-
deserialized.
|
|
1492
|
+
deserialized.requestContext = safelyParseJSON(row.requestContext);
|
|
1586
1493
|
deserialized.entity = safelyParseJSON(row.entity);
|
|
1587
1494
|
return deserialized;
|
|
1588
1495
|
}
|
|
@@ -1668,7 +1575,7 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1668
1575
|
throw mastraError;
|
|
1669
1576
|
}
|
|
1670
1577
|
}
|
|
1671
|
-
async
|
|
1578
|
+
async listScoresByScorerId({
|
|
1672
1579
|
scorerId,
|
|
1673
1580
|
entityId,
|
|
1674
1581
|
entityType,
|
|
@@ -1698,15 +1605,17 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1698
1605
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1699
1606
|
return dateB - dateA;
|
|
1700
1607
|
});
|
|
1608
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1609
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1610
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1701
1611
|
const total = scores.length;
|
|
1702
|
-
const
|
|
1703
|
-
const end = start + pagination.perPage;
|
|
1612
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1704
1613
|
const pagedScores = scores.slice(start, end);
|
|
1705
1614
|
return {
|
|
1706
1615
|
pagination: {
|
|
1707
1616
|
total,
|
|
1708
|
-
page
|
|
1709
|
-
perPage:
|
|
1617
|
+
page,
|
|
1618
|
+
perPage: perPageForResponse,
|
|
1710
1619
|
hasMore: end < total
|
|
1711
1620
|
},
|
|
1712
1621
|
scores: pagedScores
|
|
@@ -1726,7 +1635,7 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1726
1635
|
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1727
1636
|
}
|
|
1728
1637
|
}
|
|
1729
|
-
async
|
|
1638
|
+
async listScoresByRunId({
|
|
1730
1639
|
runId,
|
|
1731
1640
|
pagination
|
|
1732
1641
|
}) {
|
|
@@ -1744,15 +1653,17 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1744
1653
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1745
1654
|
return dateB - dateA;
|
|
1746
1655
|
});
|
|
1656
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1657
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1658
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1747
1659
|
const total = scores.length;
|
|
1748
|
-
const
|
|
1749
|
-
const end = start + pagination.perPage;
|
|
1660
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1750
1661
|
const pagedScores = scores.slice(start, end);
|
|
1751
1662
|
return {
|
|
1752
1663
|
pagination: {
|
|
1753
1664
|
total,
|
|
1754
|
-
page
|
|
1755
|
-
perPage:
|
|
1665
|
+
page,
|
|
1666
|
+
perPage: perPageForResponse,
|
|
1756
1667
|
hasMore: end < total
|
|
1757
1668
|
},
|
|
1758
1669
|
scores: pagedScores
|
|
@@ -1772,7 +1683,7 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1772
1683
|
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1773
1684
|
}
|
|
1774
1685
|
}
|
|
1775
|
-
async
|
|
1686
|
+
async listScoresByEntityId({
|
|
1776
1687
|
entityId,
|
|
1777
1688
|
entityType,
|
|
1778
1689
|
pagination
|
|
@@ -1791,15 +1702,17 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1791
1702
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1792
1703
|
return dateB - dateA;
|
|
1793
1704
|
});
|
|
1705
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1706
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1707
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1794
1708
|
const total = scores.length;
|
|
1795
|
-
const
|
|
1796
|
-
const end = start + pagination.perPage;
|
|
1709
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1797
1710
|
const pagedScores = scores.slice(start, end);
|
|
1798
1711
|
return {
|
|
1799
1712
|
pagination: {
|
|
1800
1713
|
total,
|
|
1801
|
-
page
|
|
1802
|
-
perPage:
|
|
1714
|
+
page,
|
|
1715
|
+
perPage: perPageForResponse,
|
|
1803
1716
|
hasMore: end < total
|
|
1804
1717
|
},
|
|
1805
1718
|
scores: pagedScores
|
|
@@ -1819,7 +1732,7 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1819
1732
|
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1820
1733
|
}
|
|
1821
1734
|
}
|
|
1822
|
-
async
|
|
1735
|
+
async listScoresBySpan({
|
|
1823
1736
|
traceId,
|
|
1824
1737
|
spanId,
|
|
1825
1738
|
pagination
|
|
@@ -1838,21 +1751,23 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1838
1751
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1839
1752
|
return dateB - dateA;
|
|
1840
1753
|
});
|
|
1754
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1755
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1756
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1841
1757
|
const total = scores.length;
|
|
1842
|
-
const
|
|
1843
|
-
const end = start + pagination.perPage;
|
|
1758
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1844
1759
|
const pagedScores = scores.slice(start, end);
|
|
1845
1760
|
return {
|
|
1846
1761
|
pagination: {
|
|
1847
1762
|
total,
|
|
1848
|
-
page
|
|
1849
|
-
perPage:
|
|
1763
|
+
page,
|
|
1764
|
+
perPage: perPageForResponse,
|
|
1850
1765
|
hasMore: end < total
|
|
1851
1766
|
},
|
|
1852
1767
|
scores: pagedScores
|
|
1853
1768
|
};
|
|
1854
1769
|
} catch (error) {
|
|
1855
|
-
|
|
1770
|
+
const mastraError = new MastraError(
|
|
1856
1771
|
{
|
|
1857
1772
|
id: "CLOUDFLARE_STORAGE_SCORES_GET_SCORES_BY_SPAN_FAILED",
|
|
1858
1773
|
domain: ErrorDomain.STORAGE,
|
|
@@ -1861,129 +1776,12 @@ var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
|
1861
1776
|
},
|
|
1862
1777
|
error
|
|
1863
1778
|
);
|
|
1779
|
+
this.logger?.trackException(mastraError);
|
|
1780
|
+
this.logger?.error(mastraError.toString());
|
|
1781
|
+
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1864
1782
|
}
|
|
1865
1783
|
}
|
|
1866
1784
|
};
|
|
1867
|
-
var TracesStorageCloudflare = class extends TracesStorage {
|
|
1868
|
-
operations;
|
|
1869
|
-
constructor({ operations }) {
|
|
1870
|
-
super();
|
|
1871
|
-
this.operations = operations;
|
|
1872
|
-
}
|
|
1873
|
-
async getTraces(args) {
|
|
1874
|
-
const paginatedArgs = {
|
|
1875
|
-
name: args.name,
|
|
1876
|
-
scope: args.scope,
|
|
1877
|
-
page: args.page,
|
|
1878
|
-
perPage: args.perPage,
|
|
1879
|
-
attributes: args.attributes,
|
|
1880
|
-
filters: args.filters,
|
|
1881
|
-
dateRange: args.fromDate || args.toDate ? {
|
|
1882
|
-
start: args.fromDate,
|
|
1883
|
-
end: args.toDate
|
|
1884
|
-
} : void 0
|
|
1885
|
-
};
|
|
1886
|
-
try {
|
|
1887
|
-
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1888
|
-
return result.traces;
|
|
1889
|
-
} catch (error) {
|
|
1890
|
-
throw new MastraError(
|
|
1891
|
-
{
|
|
1892
|
-
id: "CLOUDFLARE_STORAGE_GET_TRACES_ERROR",
|
|
1893
|
-
domain: ErrorDomain.STORAGE,
|
|
1894
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1895
|
-
text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
|
|
1896
|
-
details: {
|
|
1897
|
-
name: args.name ?? "",
|
|
1898
|
-
scope: args.scope ?? ""
|
|
1899
|
-
}
|
|
1900
|
-
},
|
|
1901
|
-
error
|
|
1902
|
-
);
|
|
1903
|
-
}
|
|
1904
|
-
}
|
|
1905
|
-
async getTracesPaginated(args) {
|
|
1906
|
-
try {
|
|
1907
|
-
const { name, scope, attributes, filters, page = 0, perPage = 100, dateRange } = args;
|
|
1908
|
-
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
1909
|
-
const keyObjs = await this.operations.listKV(TABLE_TRACES, { prefix: `${prefix}${TABLE_TRACES}` });
|
|
1910
|
-
const traces = [];
|
|
1911
|
-
for (const { name: key } of keyObjs) {
|
|
1912
|
-
try {
|
|
1913
|
-
const data = await this.operations.getKV(TABLE_TRACES, key);
|
|
1914
|
-
if (!data) continue;
|
|
1915
|
-
if (name && data.name !== name) continue;
|
|
1916
|
-
if (scope && data.scope !== scope) continue;
|
|
1917
|
-
if (attributes) {
|
|
1918
|
-
const dataAttributes = data.attributes || {};
|
|
1919
|
-
let shouldSkip = false;
|
|
1920
|
-
for (const [key2, value] of Object.entries(attributes)) {
|
|
1921
|
-
if (dataAttributes[key2] !== value) {
|
|
1922
|
-
shouldSkip = true;
|
|
1923
|
-
break;
|
|
1924
|
-
}
|
|
1925
|
-
}
|
|
1926
|
-
if (shouldSkip) continue;
|
|
1927
|
-
}
|
|
1928
|
-
if (dateRange?.start || dateRange?.end) {
|
|
1929
|
-
const traceDate = new Date(data.createdAt || 0);
|
|
1930
|
-
if (dateRange.start && traceDate < dateRange.start) continue;
|
|
1931
|
-
if (dateRange.end && traceDate > dateRange.end) continue;
|
|
1932
|
-
}
|
|
1933
|
-
if (filters) {
|
|
1934
|
-
let shouldSkip = false;
|
|
1935
|
-
for (const [key2, value] of Object.entries(filters)) {
|
|
1936
|
-
if (data[key2] !== value) {
|
|
1937
|
-
shouldSkip = true;
|
|
1938
|
-
break;
|
|
1939
|
-
}
|
|
1940
|
-
}
|
|
1941
|
-
if (shouldSkip) continue;
|
|
1942
|
-
}
|
|
1943
|
-
traces.push(data);
|
|
1944
|
-
} catch (err) {
|
|
1945
|
-
this.logger.error("Failed to parse trace:", { key, error: err });
|
|
1946
|
-
}
|
|
1947
|
-
}
|
|
1948
|
-
traces.sort((a, b) => {
|
|
1949
|
-
const aTime = new Date(a.createdAt || 0).getTime();
|
|
1950
|
-
const bTime = new Date(b.createdAt || 0).getTime();
|
|
1951
|
-
return bTime - aTime;
|
|
1952
|
-
});
|
|
1953
|
-
const total = traces.length;
|
|
1954
|
-
const start = page * perPage;
|
|
1955
|
-
const end = start + perPage;
|
|
1956
|
-
const pagedTraces = traces.slice(start, end);
|
|
1957
|
-
return {
|
|
1958
|
-
traces: pagedTraces,
|
|
1959
|
-
total,
|
|
1960
|
-
page,
|
|
1961
|
-
perPage,
|
|
1962
|
-
hasMore: end < total
|
|
1963
|
-
};
|
|
1964
|
-
} catch (error) {
|
|
1965
|
-
const mastraError = new MastraError(
|
|
1966
|
-
{
|
|
1967
|
-
id: "CLOUDFLARE_STORAGE_GET_TRACES_PAGINATED_FAILED",
|
|
1968
|
-
domain: ErrorDomain.STORAGE,
|
|
1969
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1970
|
-
text: "Error getting traces with pagination"
|
|
1971
|
-
},
|
|
1972
|
-
error
|
|
1973
|
-
);
|
|
1974
|
-
this.logger.trackException?.(mastraError);
|
|
1975
|
-
this.logger.error(mastraError.toString());
|
|
1976
|
-
return { traces: [], total: 0, page: 0, perPage: 100, hasMore: false };
|
|
1977
|
-
}
|
|
1978
|
-
}
|
|
1979
|
-
async batchTraceInsert({ records }) {
|
|
1980
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1981
|
-
await this.operations.batchInsert({
|
|
1982
|
-
tableName: TABLE_TRACES,
|
|
1983
|
-
records
|
|
1984
|
-
});
|
|
1985
|
-
}
|
|
1986
|
-
};
|
|
1987
1785
|
var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
1988
1786
|
operations;
|
|
1989
1787
|
constructor({ operations }) {
|
|
@@ -2001,7 +1799,7 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
|
2001
1799
|
// runId,
|
|
2002
1800
|
// stepId,
|
|
2003
1801
|
// result,
|
|
2004
|
-
//
|
|
1802
|
+
// requestContext,
|
|
2005
1803
|
}) {
|
|
2006
1804
|
throw new Error("Method not implemented.");
|
|
2007
1805
|
}
|
|
@@ -2101,15 +1899,28 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
|
2101
1899
|
if (resourceId) key += `:${resourceId}`;
|
|
2102
1900
|
return key;
|
|
2103
1901
|
}
|
|
2104
|
-
async
|
|
1902
|
+
async listWorkflowRuns({
|
|
2105
1903
|
workflowName,
|
|
2106
|
-
|
|
2107
|
-
|
|
1904
|
+
page = 0,
|
|
1905
|
+
perPage = 20,
|
|
2108
1906
|
resourceId,
|
|
2109
1907
|
fromDate,
|
|
2110
1908
|
toDate
|
|
2111
1909
|
} = {}) {
|
|
2112
1910
|
try {
|
|
1911
|
+
if (page < 0 || !Number.isInteger(page)) {
|
|
1912
|
+
throw new MastraError(
|
|
1913
|
+
{
|
|
1914
|
+
id: "CLOUDFLARE_STORE_INVALID_PAGE",
|
|
1915
|
+
domain: ErrorDomain.STORAGE,
|
|
1916
|
+
category: ErrorCategory.USER,
|
|
1917
|
+
details: { page }
|
|
1918
|
+
},
|
|
1919
|
+
new Error("page must be a non-negative integer")
|
|
1920
|
+
);
|
|
1921
|
+
}
|
|
1922
|
+
const normalizedPerPage = normalizePerPage(perPage, 20);
|
|
1923
|
+
const offset = page * normalizedPerPage;
|
|
2113
1924
|
const prefix = this.buildWorkflowSnapshotPrefix({ workflowName });
|
|
2114
1925
|
const keyObjs = await this.operations.listKV(TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
2115
1926
|
const runs = [];
|
|
@@ -2146,7 +1957,7 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
|
2146
1957
|
const bDate = b.createdAt ? new Date(b.createdAt).getTime() : 0;
|
|
2147
1958
|
return bDate - aDate;
|
|
2148
1959
|
});
|
|
2149
|
-
const pagedRuns = runs.slice(offset, offset +
|
|
1960
|
+
const pagedRuns = runs.slice(offset, offset + normalizedPerPage);
|
|
2150
1961
|
return {
|
|
2151
1962
|
runs: pagedRuns,
|
|
2152
1963
|
total: runs.length
|
|
@@ -2154,7 +1965,7 @@ var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
|
2154
1965
|
} catch (error) {
|
|
2155
1966
|
const mastraError = new MastraError(
|
|
2156
1967
|
{
|
|
2157
|
-
id: "
|
|
1968
|
+
id: "CLOUDFLARE_STORAGE_LIST_WORKFLOW_RUNS_FAILED",
|
|
2158
1969
|
domain: ErrorDomain.STORAGE,
|
|
2159
1970
|
category: ErrorCategory.THIRD_PARTY
|
|
2160
1971
|
},
|
|
@@ -2228,14 +2039,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2228
2039
|
if (!config.bindings) {
|
|
2229
2040
|
throw new Error("KV bindings are required when using Workers Binding API");
|
|
2230
2041
|
}
|
|
2231
|
-
const requiredTables = [
|
|
2232
|
-
TABLE_THREADS,
|
|
2233
|
-
TABLE_MESSAGES,
|
|
2234
|
-
TABLE_WORKFLOW_SNAPSHOT,
|
|
2235
|
-
TABLE_EVALS,
|
|
2236
|
-
TABLE_SCORERS,
|
|
2237
|
-
TABLE_TRACES
|
|
2238
|
-
];
|
|
2042
|
+
const requiredTables = [TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT, TABLE_SCORERS];
|
|
2239
2043
|
for (const table of requiredTables) {
|
|
2240
2044
|
if (!(table in config.bindings)) {
|
|
2241
2045
|
throw new Error(`Missing KV binding for table: ${table}`);
|
|
@@ -2255,11 +2059,13 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2255
2059
|
}
|
|
2256
2060
|
get supports() {
|
|
2257
2061
|
const supports = super.supports;
|
|
2258
|
-
supports.
|
|
2062
|
+
supports.listScoresBySpan = true;
|
|
2063
|
+
supports.resourceWorkingMemory = true;
|
|
2064
|
+
supports.selectByIncludeResourceScope = true;
|
|
2259
2065
|
return supports;
|
|
2260
2066
|
}
|
|
2261
2067
|
constructor(config) {
|
|
2262
|
-
super({ name: "Cloudflare" });
|
|
2068
|
+
super({ id: config.id, name: "Cloudflare" });
|
|
2263
2069
|
try {
|
|
2264
2070
|
if (isWorkersConfig(config)) {
|
|
2265
2071
|
this.validateWorkersConfig(config);
|
|
@@ -2281,15 +2087,9 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2281
2087
|
namespacePrefix: this.namespacePrefix,
|
|
2282
2088
|
bindings: this.bindings
|
|
2283
2089
|
});
|
|
2284
|
-
const legacyEvals = new LegacyEvalsStorageCloudflare({
|
|
2285
|
-
operations
|
|
2286
|
-
});
|
|
2287
2090
|
const workflows = new WorkflowsStorageCloudflare({
|
|
2288
2091
|
operations
|
|
2289
2092
|
});
|
|
2290
|
-
const traces = new TracesStorageCloudflare({
|
|
2291
|
-
operations
|
|
2292
|
-
});
|
|
2293
2093
|
const memory = new MemoryStorageCloudflare({
|
|
2294
2094
|
operations
|
|
2295
2095
|
});
|
|
@@ -2298,9 +2098,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2298
2098
|
});
|
|
2299
2099
|
this.stores = {
|
|
2300
2100
|
operations,
|
|
2301
|
-
legacyEvals,
|
|
2302
2101
|
workflows,
|
|
2303
|
-
traces,
|
|
2304
2102
|
memory,
|
|
2305
2103
|
scores
|
|
2306
2104
|
};
|
|
@@ -2342,9 +2140,6 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2342
2140
|
async getThreadById({ threadId }) {
|
|
2343
2141
|
return this.stores.memory.getThreadById({ threadId });
|
|
2344
2142
|
}
|
|
2345
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2346
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2347
|
-
}
|
|
2348
2143
|
async saveThread({ thread }) {
|
|
2349
2144
|
return this.stores.memory.saveThread({ thread });
|
|
2350
2145
|
}
|
|
@@ -2361,22 +2156,14 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2361
2156
|
async saveMessages(args) {
|
|
2362
2157
|
return this.stores.memory.saveMessages(args);
|
|
2363
2158
|
}
|
|
2364
|
-
async getMessages({
|
|
2365
|
-
threadId,
|
|
2366
|
-
resourceId,
|
|
2367
|
-
selectBy,
|
|
2368
|
-
format
|
|
2369
|
-
}) {
|
|
2370
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format });
|
|
2371
|
-
}
|
|
2372
2159
|
async updateWorkflowResults({
|
|
2373
2160
|
workflowName,
|
|
2374
2161
|
runId,
|
|
2375
2162
|
stepId,
|
|
2376
2163
|
result,
|
|
2377
|
-
|
|
2164
|
+
requestContext
|
|
2378
2165
|
}) {
|
|
2379
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2166
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2380
2167
|
}
|
|
2381
2168
|
async updateWorkflowState({
|
|
2382
2169
|
workflowName,
|
|
@@ -2385,11 +2172,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2385
2172
|
}) {
|
|
2386
2173
|
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2387
2174
|
}
|
|
2388
|
-
async
|
|
2389
|
-
messageIds
|
|
2390
|
-
format
|
|
2391
|
-
}) {
|
|
2392
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2175
|
+
async listMessagesById({ messageIds }) {
|
|
2176
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2393
2177
|
}
|
|
2394
2178
|
async persistWorkflowSnapshot(params) {
|
|
2395
2179
|
return this.stores.workflows.persistWorkflowSnapshot(params);
|
|
@@ -2400,43 +2184,18 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2400
2184
|
async batchInsert(input) {
|
|
2401
2185
|
return this.stores.operations.batchInsert(input);
|
|
2402
2186
|
}
|
|
2403
|
-
async
|
|
2404
|
-
name,
|
|
2405
|
-
scope,
|
|
2406
|
-
page = 0,
|
|
2407
|
-
perPage = 100,
|
|
2408
|
-
attributes,
|
|
2409
|
-
fromDate,
|
|
2410
|
-
toDate
|
|
2411
|
-
}) {
|
|
2412
|
-
return this.stores.traces.getTraces({
|
|
2413
|
-
name,
|
|
2414
|
-
scope,
|
|
2415
|
-
page,
|
|
2416
|
-
perPage,
|
|
2417
|
-
attributes,
|
|
2418
|
-
fromDate,
|
|
2419
|
-
toDate
|
|
2420
|
-
});
|
|
2421
|
-
}
|
|
2422
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2423
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2424
|
-
}
|
|
2425
|
-
async getEvals(options) {
|
|
2426
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2427
|
-
}
|
|
2428
|
-
async getWorkflowRuns({
|
|
2187
|
+
async listWorkflowRuns({
|
|
2429
2188
|
workflowName,
|
|
2430
|
-
|
|
2431
|
-
|
|
2189
|
+
perPage = 20,
|
|
2190
|
+
page = 0,
|
|
2432
2191
|
resourceId,
|
|
2433
2192
|
fromDate,
|
|
2434
2193
|
toDate
|
|
2435
2194
|
} = {}) {
|
|
2436
|
-
return this.stores.workflows.
|
|
2195
|
+
return this.stores.workflows.listWorkflowRuns({
|
|
2437
2196
|
workflowName,
|
|
2438
|
-
|
|
2439
|
-
|
|
2197
|
+
perPage,
|
|
2198
|
+
page,
|
|
2440
2199
|
resourceId,
|
|
2441
2200
|
fromDate,
|
|
2442
2201
|
toDate
|
|
@@ -2448,15 +2207,6 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2448
2207
|
}) {
|
|
2449
2208
|
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2450
2209
|
}
|
|
2451
|
-
async getTracesPaginated(args) {
|
|
2452
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2453
|
-
}
|
|
2454
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2455
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2456
|
-
}
|
|
2457
|
-
async getMessagesPaginated(args) {
|
|
2458
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2459
|
-
}
|
|
2460
2210
|
async updateMessages(args) {
|
|
2461
2211
|
return this.stores.memory.updateMessages(args);
|
|
2462
2212
|
}
|
|
@@ -2466,34 +2216,34 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
2466
2216
|
async saveScore(score) {
|
|
2467
2217
|
return this.stores.scores.saveScore(score);
|
|
2468
2218
|
}
|
|
2469
|
-
async
|
|
2219
|
+
async listScoresByRunId({
|
|
2470
2220
|
runId,
|
|
2471
2221
|
pagination
|
|
2472
2222
|
}) {
|
|
2473
|
-
return this.stores.scores.
|
|
2223
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2474
2224
|
}
|
|
2475
|
-
async
|
|
2225
|
+
async listScoresByEntityId({
|
|
2476
2226
|
entityId,
|
|
2477
2227
|
entityType,
|
|
2478
2228
|
pagination
|
|
2479
2229
|
}) {
|
|
2480
|
-
return this.stores.scores.
|
|
2230
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2481
2231
|
}
|
|
2482
|
-
async
|
|
2232
|
+
async listScoresByScorerId({
|
|
2483
2233
|
scorerId,
|
|
2484
2234
|
entityId,
|
|
2485
2235
|
entityType,
|
|
2486
2236
|
source,
|
|
2487
2237
|
pagination
|
|
2488
2238
|
}) {
|
|
2489
|
-
return this.stores.scores.
|
|
2239
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, entityId, entityType, source, pagination });
|
|
2490
2240
|
}
|
|
2491
|
-
async
|
|
2241
|
+
async listScoresBySpan({
|
|
2492
2242
|
traceId,
|
|
2493
2243
|
spanId,
|
|
2494
2244
|
pagination
|
|
2495
2245
|
}) {
|
|
2496
|
-
return this.stores.scores.
|
|
2246
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2497
2247
|
}
|
|
2498
2248
|
async getResourceById({ resourceId }) {
|
|
2499
2249
|
return this.stores.memory.getResourceById({ resourceId });
|