@mastra/cloudflare 0.13.2-alpha.0 → 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 +225 -10
- 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.cjs
CHANGED
|
@@ -4,113 +4,13 @@ var error = require('@mastra/core/error');
|
|
|
4
4
|
var storage = require('@mastra/core/storage');
|
|
5
5
|
var Cloudflare = require('cloudflare');
|
|
6
6
|
var agent = require('@mastra/core/agent');
|
|
7
|
-
var
|
|
7
|
+
var evals = require('@mastra/core/evals');
|
|
8
8
|
|
|
9
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
|
|
11
11
|
var Cloudflare__default = /*#__PURE__*/_interopDefault(Cloudflare);
|
|
12
12
|
|
|
13
13
|
// src/storage/index.ts
|
|
14
|
-
var LegacyEvalsStorageCloudflare = class extends storage.LegacyEvalsStorage {
|
|
15
|
-
operations;
|
|
16
|
-
constructor({ operations }) {
|
|
17
|
-
super();
|
|
18
|
-
this.operations = operations;
|
|
19
|
-
}
|
|
20
|
-
async getEvalsByAgentName(agentName, type) {
|
|
21
|
-
try {
|
|
22
|
-
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
23
|
-
const keyObjs = await this.operations.listKV(storage.TABLE_EVALS, { prefix: `${prefix}${storage.TABLE_EVALS}` });
|
|
24
|
-
const evals = [];
|
|
25
|
-
for (const { name: key } of keyObjs) {
|
|
26
|
-
const data = await this.operations.getKV(storage.TABLE_EVALS, key);
|
|
27
|
-
if (!data) continue;
|
|
28
|
-
if (data.agent_name !== agentName) continue;
|
|
29
|
-
if (type) {
|
|
30
|
-
const isTest = data.test_info !== null && data.test_info !== void 0;
|
|
31
|
-
const evalType = isTest ? "test" : "live";
|
|
32
|
-
if (evalType !== type) continue;
|
|
33
|
-
}
|
|
34
|
-
const mappedData = {
|
|
35
|
-
...data,
|
|
36
|
-
runId: data.run_id,
|
|
37
|
-
testInfo: data.test_info
|
|
38
|
-
};
|
|
39
|
-
evals.push(mappedData);
|
|
40
|
-
}
|
|
41
|
-
evals.sort((a, b) => {
|
|
42
|
-
const aTime = new Date(a.createdAt || 0).getTime();
|
|
43
|
-
const bTime = new Date(b.createdAt || 0).getTime();
|
|
44
|
-
return bTime - aTime;
|
|
45
|
-
});
|
|
46
|
-
return evals;
|
|
47
|
-
} catch (error$1) {
|
|
48
|
-
throw new error.MastraError(
|
|
49
|
-
{
|
|
50
|
-
id: "CLOUDFLARE_STORAGE_GET_EVALS_BY_AGENT_NAME_FAILED",
|
|
51
|
-
domain: error.ErrorDomain.STORAGE,
|
|
52
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
53
|
-
text: "Failed to get evals by agent name"
|
|
54
|
-
},
|
|
55
|
-
error$1
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async getEvals(options) {
|
|
60
|
-
try {
|
|
61
|
-
const { agentName, type, page = 0, perPage = 100, dateRange } = options;
|
|
62
|
-
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
63
|
-
const keyObjs = await this.operations.listKV(storage.TABLE_EVALS, { prefix: `${prefix}${storage.TABLE_EVALS}` });
|
|
64
|
-
const evals = [];
|
|
65
|
-
for (const { name: key } of keyObjs) {
|
|
66
|
-
const data = await this.operations.getKV(storage.TABLE_EVALS, key);
|
|
67
|
-
if (!data) continue;
|
|
68
|
-
if (agentName && data.agent_name !== agentName) continue;
|
|
69
|
-
if (type) {
|
|
70
|
-
const isTest = data.test_info !== null && data.test_info !== void 0;
|
|
71
|
-
const evalType = isTest ? "test" : "live";
|
|
72
|
-
if (evalType !== type) continue;
|
|
73
|
-
}
|
|
74
|
-
if (dateRange?.start || dateRange?.end) {
|
|
75
|
-
const evalDate = new Date(data.createdAt || data.created_at || 0);
|
|
76
|
-
if (dateRange.start && evalDate < dateRange.start) continue;
|
|
77
|
-
if (dateRange.end && evalDate > dateRange.end) continue;
|
|
78
|
-
}
|
|
79
|
-
const mappedData = {
|
|
80
|
-
...data,
|
|
81
|
-
runId: data.run_id,
|
|
82
|
-
testInfo: data.test_info
|
|
83
|
-
};
|
|
84
|
-
evals.push(mappedData);
|
|
85
|
-
}
|
|
86
|
-
evals.sort((a, b) => {
|
|
87
|
-
const aTime = new Date(a.createdAt || 0).getTime();
|
|
88
|
-
const bTime = new Date(b.createdAt || 0).getTime();
|
|
89
|
-
return bTime - aTime;
|
|
90
|
-
});
|
|
91
|
-
const start = page * perPage;
|
|
92
|
-
const end = start + perPage;
|
|
93
|
-
const paginatedEvals = evals.slice(start, end);
|
|
94
|
-
return {
|
|
95
|
-
page,
|
|
96
|
-
perPage,
|
|
97
|
-
total: evals.length,
|
|
98
|
-
hasMore: start + perPage < evals.length,
|
|
99
|
-
evals: paginatedEvals
|
|
100
|
-
};
|
|
101
|
-
} catch (error$1) {
|
|
102
|
-
throw new error.MastraError(
|
|
103
|
-
{
|
|
104
|
-
id: "CLOUDFLARE_STORAGE_GET_EVALS_FAILED",
|
|
105
|
-
domain: error.ErrorDomain.STORAGE,
|
|
106
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
107
|
-
text: "Failed to get evals"
|
|
108
|
-
},
|
|
109
|
-
error$1
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
14
|
var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
115
15
|
operations;
|
|
116
16
|
constructor({ operations }) {
|
|
@@ -148,61 +48,23 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
148
48
|
return null;
|
|
149
49
|
}
|
|
150
50
|
}
|
|
151
|
-
async
|
|
51
|
+
async listThreadsByResourceId(args) {
|
|
152
52
|
try {
|
|
153
|
-
const
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const mastraError = new error.MastraError(
|
|
169
|
-
{
|
|
170
|
-
id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
171
|
-
domain: error.ErrorDomain.STORAGE,
|
|
172
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
173
|
-
details: {
|
|
174
|
-
resourceId
|
|
175
|
-
}
|
|
176
|
-
},
|
|
177
|
-
error$1
|
|
178
|
-
);
|
|
179
|
-
this.logger?.trackException(mastraError);
|
|
180
|
-
this.logger?.error(mastraError.toString());
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
})
|
|
184
|
-
);
|
|
185
|
-
return threads.filter((thread) => thread !== null);
|
|
186
|
-
} catch (error$1) {
|
|
187
|
-
const mastraError = new error.MastraError(
|
|
188
|
-
{
|
|
189
|
-
id: "CLOUDFLARE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
|
|
190
|
-
domain: error.ErrorDomain.STORAGE,
|
|
191
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
192
|
-
details: {
|
|
193
|
-
resourceId
|
|
194
|
-
}
|
|
195
|
-
},
|
|
196
|
-
error$1
|
|
197
|
-
);
|
|
198
|
-
this.logger?.trackException(mastraError);
|
|
199
|
-
this.logger?.error(mastraError.toString());
|
|
200
|
-
return [];
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
204
|
-
try {
|
|
205
|
-
const { resourceId, page = 0, perPage = 100 } = args;
|
|
53
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
54
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
55
|
+
if (page < 0) {
|
|
56
|
+
throw new error.MastraError(
|
|
57
|
+
{
|
|
58
|
+
id: "STORAGE_CLOUDFLARE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
59
|
+
domain: error.ErrorDomain.STORAGE,
|
|
60
|
+
category: error.ErrorCategory.USER,
|
|
61
|
+
details: { page }
|
|
62
|
+
},
|
|
63
|
+
new Error("page must be >= 0")
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
67
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
206
68
|
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
207
69
|
const keyObjs = await this.operations.listKV(storage.TABLE_THREADS, { prefix: `${prefix}${storage.TABLE_THREADS}` });
|
|
208
70
|
const threads = [];
|
|
@@ -213,24 +75,23 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
213
75
|
threads.push(data);
|
|
214
76
|
}
|
|
215
77
|
threads.sort((a, b) => {
|
|
216
|
-
const aTime = new Date(a
|
|
217
|
-
const bTime = new Date(b
|
|
218
|
-
return bTime - aTime;
|
|
78
|
+
const aTime = new Date(a[field] || 0).getTime();
|
|
79
|
+
const bTime = new Date(b[field] || 0).getTime();
|
|
80
|
+
return direction === "ASC" ? aTime - bTime : bTime - aTime;
|
|
219
81
|
});
|
|
220
|
-
const
|
|
221
|
-
const
|
|
222
|
-
const paginatedThreads = threads.slice(start, end);
|
|
82
|
+
const end = perPageInput === false ? threads.length : offset + perPage;
|
|
83
|
+
const paginatedThreads = threads.slice(offset, end);
|
|
223
84
|
return {
|
|
224
85
|
page,
|
|
225
|
-
perPage,
|
|
86
|
+
perPage: perPageForResponse,
|
|
226
87
|
total: threads.length,
|
|
227
|
-
hasMore:
|
|
88
|
+
hasMore: perPageInput === false ? false : offset + perPage < threads.length,
|
|
228
89
|
threads: paginatedThreads
|
|
229
90
|
};
|
|
230
91
|
} catch (error$1) {
|
|
231
92
|
throw new error.MastraError(
|
|
232
93
|
{
|
|
233
|
-
id: "
|
|
94
|
+
id: "CLOUDFLARE_STORAGE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
234
95
|
domain: error.ErrorDomain.STORAGE,
|
|
235
96
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
236
97
|
text: "Failed to get threads by resource ID with pagination"
|
|
@@ -446,8 +307,8 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
446
307
|
}
|
|
447
308
|
}
|
|
448
309
|
async saveMessages(args) {
|
|
449
|
-
const { messages
|
|
450
|
-
if (!Array.isArray(messages) || messages.length === 0) return [];
|
|
310
|
+
const { messages } = args;
|
|
311
|
+
if (!Array.isArray(messages) || messages.length === 0) return { messages: [] };
|
|
451
312
|
try {
|
|
452
313
|
const validatedMessages = messages.map((message, index) => {
|
|
453
314
|
const errors = [];
|
|
@@ -540,8 +401,7 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
540
401
|
({ _index, ...message }) => ({ ...message, type: message.type !== "v2" ? message.type : void 0 })
|
|
541
402
|
);
|
|
542
403
|
const list = new agent.MessageList().add(prepared, "memory");
|
|
543
|
-
|
|
544
|
-
return list.get.all.v1();
|
|
404
|
+
return { messages: list.get.all.db() };
|
|
545
405
|
} catch (error$1) {
|
|
546
406
|
throw new error.MastraError(
|
|
547
407
|
{
|
|
@@ -650,96 +510,8 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
650
510
|
);
|
|
651
511
|
return messages.filter((msg) => msg !== null);
|
|
652
512
|
}
|
|
653
|
-
async
|
|
654
|
-
|
|
655
|
-
resourceId,
|
|
656
|
-
selectBy,
|
|
657
|
-
format
|
|
658
|
-
}) {
|
|
659
|
-
console.info(`getMessages called with format: ${format}, threadId: ${threadId}`);
|
|
660
|
-
const actualFormat = format || "v1";
|
|
661
|
-
console.info(`Using format: ${actualFormat}`);
|
|
662
|
-
const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
663
|
-
const messageIds = /* @__PURE__ */ new Set();
|
|
664
|
-
if (limit === 0 && !selectBy?.include?.length) return [];
|
|
665
|
-
try {
|
|
666
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
667
|
-
await Promise.all([
|
|
668
|
-
selectBy?.include?.length ? this.getIncludedMessagesWithContext(threadId, selectBy.include, messageIds) : Promise.resolve(),
|
|
669
|
-
limit > 0 ? this.getRecentMessages(threadId, limit, messageIds) : Promise.resolve()
|
|
670
|
-
]);
|
|
671
|
-
const targetThreadId = selectBy?.include?.length ? void 0 : threadId;
|
|
672
|
-
const messages = await this.fetchAndParseMessagesFromMultipleThreads(
|
|
673
|
-
Array.from(messageIds),
|
|
674
|
-
selectBy?.include,
|
|
675
|
-
targetThreadId
|
|
676
|
-
);
|
|
677
|
-
if (!messages.length) return [];
|
|
678
|
-
try {
|
|
679
|
-
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
680
|
-
const messageOrder = await this.getFullOrder(threadMessagesKey);
|
|
681
|
-
const orderMap = new Map(messageOrder.map((id, index) => [id, index]));
|
|
682
|
-
messages.sort((a, b) => {
|
|
683
|
-
const indexA = orderMap.get(a.id);
|
|
684
|
-
const indexB = orderMap.get(b.id);
|
|
685
|
-
if (indexA !== void 0 && indexB !== void 0) return orderMap.get(a.id) - orderMap.get(b.id);
|
|
686
|
-
return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
|
|
687
|
-
});
|
|
688
|
-
} catch (error$1) {
|
|
689
|
-
const mastraError = new error.MastraError(
|
|
690
|
-
{
|
|
691
|
-
id: "CLOUDFLARE_STORAGE_SORT_MESSAGES_FAILED",
|
|
692
|
-
domain: error.ErrorDomain.STORAGE,
|
|
693
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
694
|
-
text: `Error sorting messages for thread ${threadId} falling back to creation time`,
|
|
695
|
-
details: {
|
|
696
|
-
threadId
|
|
697
|
-
}
|
|
698
|
-
},
|
|
699
|
-
error$1
|
|
700
|
-
);
|
|
701
|
-
this.logger?.trackException(mastraError);
|
|
702
|
-
this.logger?.error(mastraError.toString());
|
|
703
|
-
messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
704
|
-
}
|
|
705
|
-
const prepared = messages.map(({ _index, ...message }) => ({
|
|
706
|
-
...message,
|
|
707
|
-
type: message.type === `v2` ? void 0 : message.type,
|
|
708
|
-
createdAt: storage.ensureDate(message.createdAt)
|
|
709
|
-
}));
|
|
710
|
-
if (actualFormat === `v1`) {
|
|
711
|
-
console.info(`Processing ${prepared.length} messages for v1 format - returning directly without MessageList`);
|
|
712
|
-
return prepared.map((msg) => ({
|
|
713
|
-
...msg,
|
|
714
|
-
createdAt: new Date(msg.createdAt)
|
|
715
|
-
}));
|
|
716
|
-
}
|
|
717
|
-
const list = new agent.MessageList({ threadId, resourceId }).add(prepared, "memory");
|
|
718
|
-
return list.get.all.v2();
|
|
719
|
-
} catch (error$1) {
|
|
720
|
-
const mastraError = new error.MastraError(
|
|
721
|
-
{
|
|
722
|
-
id: "CLOUDFLARE_STORAGE_GET_MESSAGES_FAILED",
|
|
723
|
-
domain: error.ErrorDomain.STORAGE,
|
|
724
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
725
|
-
text: `Error retrieving messages for thread ${threadId}`,
|
|
726
|
-
details: {
|
|
727
|
-
threadId,
|
|
728
|
-
resourceId: resourceId ?? ""
|
|
729
|
-
}
|
|
730
|
-
},
|
|
731
|
-
error$1
|
|
732
|
-
);
|
|
733
|
-
this.logger?.trackException(mastraError);
|
|
734
|
-
this.logger?.error(mastraError.toString());
|
|
735
|
-
return [];
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
async getMessagesById({
|
|
739
|
-
messageIds,
|
|
740
|
-
format
|
|
741
|
-
}) {
|
|
742
|
-
if (messageIds.length === 0) return [];
|
|
513
|
+
async listMessagesById({ messageIds }) {
|
|
514
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
743
515
|
try {
|
|
744
516
|
const messages = (await Promise.all(messageIds.map((id) => this.findMessageInAnyThread(id)))).filter(
|
|
745
517
|
(result) => !!result
|
|
@@ -750,12 +522,11 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
750
522
|
createdAt: storage.ensureDate(message.createdAt)
|
|
751
523
|
}));
|
|
752
524
|
const list = new agent.MessageList().add(prepared, "memory");
|
|
753
|
-
|
|
754
|
-
return list.get.all.v2();
|
|
525
|
+
return { messages: list.get.all.db() };
|
|
755
526
|
} catch (error$1) {
|
|
756
527
|
const mastraError = new error.MastraError(
|
|
757
528
|
{
|
|
758
|
-
id: "
|
|
529
|
+
id: "CLOUDFLARE_STORAGE_LIST_MESSAGES_BY_ID_FAILED",
|
|
759
530
|
domain: error.ErrorDomain.STORAGE,
|
|
760
531
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
761
532
|
text: `Error retrieving messages by ID`,
|
|
@@ -767,42 +538,202 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
767
538
|
);
|
|
768
539
|
this.logger?.trackException(mastraError);
|
|
769
540
|
this.logger?.error(mastraError.toString());
|
|
770
|
-
return [];
|
|
541
|
+
return { messages: [] };
|
|
771
542
|
}
|
|
772
543
|
}
|
|
773
|
-
async
|
|
774
|
-
const { threadId, resourceId,
|
|
775
|
-
|
|
544
|
+
async listMessages(args) {
|
|
545
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
546
|
+
if (!threadId.trim()) {
|
|
547
|
+
throw new error.MastraError(
|
|
548
|
+
{
|
|
549
|
+
id: "STORAGE_CLOUDFLARE_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
550
|
+
domain: error.ErrorDomain.STORAGE,
|
|
551
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
552
|
+
details: { threadId }
|
|
553
|
+
},
|
|
554
|
+
new Error("threadId must be a non-empty string")
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
558
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
776
559
|
try {
|
|
777
|
-
if (
|
|
778
|
-
|
|
560
|
+
if (page < 0) {
|
|
561
|
+
throw new error.MastraError(
|
|
562
|
+
{
|
|
563
|
+
id: "STORAGE_CLOUDFLARE_LIST_MESSAGES_INVALID_PAGE",
|
|
564
|
+
domain: error.ErrorDomain.STORAGE,
|
|
565
|
+
category: error.ErrorCategory.USER,
|
|
566
|
+
details: { page }
|
|
567
|
+
},
|
|
568
|
+
new Error("page must be >= 0")
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
572
|
+
const messageIds = /* @__PURE__ */ new Set();
|
|
573
|
+
const hasFilters = !!resourceId || !!filter?.dateRange;
|
|
574
|
+
if (hasFilters || perPage === Number.MAX_SAFE_INTEGER) {
|
|
575
|
+
try {
|
|
576
|
+
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
577
|
+
const allIds = await this.getFullOrder(threadMessagesKey);
|
|
578
|
+
allIds.forEach((id) => messageIds.add(id));
|
|
579
|
+
} catch {
|
|
580
|
+
}
|
|
581
|
+
} else {
|
|
582
|
+
if (perPage > 0) {
|
|
583
|
+
try {
|
|
584
|
+
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
585
|
+
const fullOrder = await this.getFullOrder(threadMessagesKey);
|
|
586
|
+
const totalMessages = fullOrder.length;
|
|
587
|
+
let start;
|
|
588
|
+
let end;
|
|
589
|
+
if (direction === "ASC") {
|
|
590
|
+
start = offset;
|
|
591
|
+
end = Math.min(offset + perPage - 1, totalMessages - 1);
|
|
592
|
+
} else {
|
|
593
|
+
start = Math.max(totalMessages - offset - perPage, 0);
|
|
594
|
+
end = totalMessages - offset - 1;
|
|
595
|
+
}
|
|
596
|
+
const paginatedIds = await this.getRange(threadMessagesKey, start, end);
|
|
597
|
+
paginatedIds.forEach((id) => messageIds.add(id));
|
|
598
|
+
} catch {
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
if (include && include.length > 0) {
|
|
603
|
+
await this.getIncludedMessagesWithContext(threadId, include, messageIds);
|
|
604
|
+
}
|
|
605
|
+
const messages = await this.fetchAndParseMessagesFromMultipleThreads(
|
|
606
|
+
Array.from(messageIds),
|
|
607
|
+
include,
|
|
608
|
+
include && include.length > 0 ? void 0 : threadId
|
|
609
|
+
);
|
|
779
610
|
let filteredMessages = messages;
|
|
780
|
-
if (
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
611
|
+
if (resourceId) {
|
|
612
|
+
filteredMessages = filteredMessages.filter((msg) => msg.resourceId === resourceId);
|
|
613
|
+
}
|
|
614
|
+
const dateRange = filter?.dateRange;
|
|
615
|
+
if (dateRange) {
|
|
616
|
+
filteredMessages = filteredMessages.filter((msg) => {
|
|
617
|
+
const messageDate = new Date(msg.createdAt);
|
|
618
|
+
if (dateRange.start && messageDate < new Date(dateRange.start)) return false;
|
|
619
|
+
if (dateRange.end && messageDate > new Date(dateRange.end)) return false;
|
|
786
620
|
return true;
|
|
787
621
|
});
|
|
788
622
|
}
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
623
|
+
let total;
|
|
624
|
+
if (hasFilters) {
|
|
625
|
+
total = filteredMessages.length;
|
|
626
|
+
} else {
|
|
627
|
+
try {
|
|
628
|
+
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
629
|
+
const fullOrder = await this.getFullOrder(threadMessagesKey);
|
|
630
|
+
total = fullOrder.length;
|
|
631
|
+
} catch {
|
|
632
|
+
total = filteredMessages.length;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
if (perPage === 0 && (!include || include.length === 0)) {
|
|
636
|
+
return {
|
|
637
|
+
messages: [],
|
|
638
|
+
total,
|
|
639
|
+
page,
|
|
640
|
+
perPage: perPageForResponse,
|
|
641
|
+
hasMore: offset < total
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
if (hasFilters && perPage !== Number.MAX_SAFE_INTEGER && perPage > 0) {
|
|
645
|
+
if (direction === "ASC") {
|
|
646
|
+
filteredMessages = filteredMessages.slice(offset, offset + perPage);
|
|
647
|
+
} else {
|
|
648
|
+
const start = Math.max(filteredMessages.length - offset - perPage, 0);
|
|
649
|
+
const end = filteredMessages.length - offset;
|
|
650
|
+
filteredMessages = filteredMessages.slice(start, end);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
const paginatedCount = hasFilters && perPage !== Number.MAX_SAFE_INTEGER && perPage > 0 ? filteredMessages.length : filteredMessages.length;
|
|
654
|
+
try {
|
|
655
|
+
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
656
|
+
const messageOrder = await this.getFullOrder(threadMessagesKey);
|
|
657
|
+
const orderMap = new Map(messageOrder.map((id, index) => [id, index]));
|
|
658
|
+
filteredMessages.sort((a, b) => {
|
|
659
|
+
const indexA = orderMap.get(a.id);
|
|
660
|
+
const indexB = orderMap.get(b.id);
|
|
661
|
+
if (indexA !== void 0 && indexB !== void 0) {
|
|
662
|
+
return direction === "ASC" ? indexA - indexB : indexB - indexA;
|
|
663
|
+
}
|
|
664
|
+
const timeA = new Date(a.createdAt).getTime();
|
|
665
|
+
const timeB = new Date(b.createdAt).getTime();
|
|
666
|
+
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
667
|
+
if (timeDiff === 0) {
|
|
668
|
+
return a.id.localeCompare(b.id);
|
|
669
|
+
}
|
|
670
|
+
return timeDiff;
|
|
671
|
+
});
|
|
672
|
+
} catch {
|
|
673
|
+
filteredMessages.sort((a, b) => {
|
|
674
|
+
const timeA = new Date(a.createdAt).getTime();
|
|
675
|
+
const timeB = new Date(b.createdAt).getTime();
|
|
676
|
+
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
677
|
+
if (timeDiff === 0) {
|
|
678
|
+
return a.id.localeCompare(b.id);
|
|
679
|
+
}
|
|
680
|
+
return timeDiff;
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
if (total === 0 && filteredMessages.length === 0 && (!include || include.length === 0)) {
|
|
684
|
+
return {
|
|
685
|
+
messages: [],
|
|
686
|
+
total: 0,
|
|
687
|
+
page,
|
|
688
|
+
perPage: perPageForResponse,
|
|
689
|
+
hasMore: false
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
const prepared = filteredMessages.map(({ _index, ...message }) => ({
|
|
693
|
+
...message,
|
|
694
|
+
type: message.type !== "v2" ? message.type : void 0,
|
|
695
|
+
createdAt: storage.ensureDate(message.createdAt)
|
|
696
|
+
}));
|
|
697
|
+
const list = new agent.MessageList({ threadId, resourceId }).add(prepared, "memory");
|
|
698
|
+
let finalMessages = list.get.all.db();
|
|
699
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
700
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
701
|
+
const aVal = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
702
|
+
const bVal = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
703
|
+
if (aVal == null && bVal == null) return a.id.localeCompare(b.id);
|
|
704
|
+
if (aVal == null) return 1;
|
|
705
|
+
if (bVal == null) return -1;
|
|
706
|
+
if (typeof aVal === "number" && typeof bVal === "number") {
|
|
707
|
+
const cmp2 = direction === "ASC" ? aVal - bVal : bVal - aVal;
|
|
708
|
+
return cmp2 !== 0 ? cmp2 : a.id.localeCompare(b.id);
|
|
709
|
+
}
|
|
710
|
+
const cmp = direction === "ASC" ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal));
|
|
711
|
+
return cmp !== 0 ? cmp : a.id.localeCompare(b.id);
|
|
712
|
+
});
|
|
713
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
714
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
715
|
+
let hasMore;
|
|
716
|
+
if (perPageInput === false || allThreadMessagesReturned) {
|
|
717
|
+
hasMore = false;
|
|
718
|
+
} else if (direction === "ASC") {
|
|
719
|
+
hasMore = offset + paginatedCount < total;
|
|
720
|
+
} else {
|
|
721
|
+
hasMore = total - offset - perPage > 0;
|
|
722
|
+
}
|
|
792
723
|
return {
|
|
724
|
+
messages: finalMessages,
|
|
725
|
+
total,
|
|
793
726
|
page,
|
|
794
|
-
perPage,
|
|
795
|
-
|
|
796
|
-
hasMore: start + perPage < filteredMessages.length,
|
|
797
|
-
messages: paginatedMessages
|
|
727
|
+
perPage: perPageForResponse,
|
|
728
|
+
hasMore
|
|
798
729
|
};
|
|
799
730
|
} catch (error$1) {
|
|
800
731
|
const mastraError = new error.MastraError(
|
|
801
732
|
{
|
|
802
|
-
id: "
|
|
733
|
+
id: "CLOUDFLARE_STORAGE_LIST_MESSAGES_FAILED",
|
|
803
734
|
domain: error.ErrorDomain.STORAGE,
|
|
804
735
|
category: error.ErrorCategory.THIRD_PARTY,
|
|
805
|
-
text:
|
|
736
|
+
text: `Failed to list messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
806
737
|
details: {
|
|
807
738
|
threadId,
|
|
808
739
|
resourceId: resourceId ?? ""
|
|
@@ -810,9 +741,15 @@ var MemoryStorageCloudflare = class extends storage.MemoryStorage {
|
|
|
810
741
|
},
|
|
811
742
|
error$1
|
|
812
743
|
);
|
|
813
|
-
this.logger?.trackException?.(mastraError);
|
|
814
744
|
this.logger?.error?.(mastraError.toString());
|
|
815
|
-
|
|
745
|
+
this.logger?.trackException?.(mastraError);
|
|
746
|
+
return {
|
|
747
|
+
messages: [],
|
|
748
|
+
total: 0,
|
|
749
|
+
page,
|
|
750
|
+
perPage: perPageForResponse,
|
|
751
|
+
hasMore: false
|
|
752
|
+
};
|
|
816
753
|
}
|
|
817
754
|
}
|
|
818
755
|
async updateMessages(args) {
|
|
@@ -1098,10 +1035,6 @@ var StoreOperationsCloudflare = class extends storage.StoreOperations {
|
|
|
1098
1035
|
case storage.TABLE_TRACES:
|
|
1099
1036
|
if (!record.id) throw new Error("Trace ID is required");
|
|
1100
1037
|
return `${prefix}${tableName}:${record.id}`;
|
|
1101
|
-
case storage.TABLE_EVALS:
|
|
1102
|
-
const evalId = record.id || record.run_id;
|
|
1103
|
-
if (!evalId) throw new Error("Eval ID or run_id is required");
|
|
1104
|
-
return `${prefix}${tableName}:${evalId}`;
|
|
1105
1038
|
case storage.TABLE_SCORERS:
|
|
1106
1039
|
if (!record.id) throw new Error("Score ID is required");
|
|
1107
1040
|
return `${prefix}${tableName}:${record.id}`;
|
|
@@ -1341,11 +1274,6 @@ var StoreOperationsCloudflare = class extends storage.StoreOperations {
|
|
|
1341
1274
|
throw new Error("Trace record missing required fields");
|
|
1342
1275
|
}
|
|
1343
1276
|
break;
|
|
1344
|
-
case storage.TABLE_EVALS:
|
|
1345
|
-
if (!("agent_name" in recordTyped) || !("run_id" in recordTyped)) {
|
|
1346
|
-
throw new Error("Eval record missing required fields");
|
|
1347
|
-
}
|
|
1348
|
-
break;
|
|
1349
1277
|
case storage.TABLE_SCORERS:
|
|
1350
1278
|
if (!("id" in recordTyped) || !("scorerId" in recordTyped)) {
|
|
1351
1279
|
throw new Error("Score record missing required fields");
|
|
@@ -1363,12 +1291,7 @@ var StoreOperationsCloudflare = class extends storage.StoreOperations {
|
|
|
1363
1291
|
async insert({ tableName, record }) {
|
|
1364
1292
|
try {
|
|
1365
1293
|
const key = this.getKey(tableName, record);
|
|
1366
|
-
const processedRecord = {
|
|
1367
|
-
...record,
|
|
1368
|
-
createdAt: record.createdAt ? storage.serializeDate(record.createdAt) : void 0,
|
|
1369
|
-
updatedAt: record.updatedAt ? storage.serializeDate(record.updatedAt) : void 0,
|
|
1370
|
-
metadata: record.metadata ? JSON.stringify(record.metadata) : ""
|
|
1371
|
-
};
|
|
1294
|
+
const processedRecord = { ...record };
|
|
1372
1295
|
await this.validateRecord(processedRecord, tableName);
|
|
1373
1296
|
await this.putKV({ tableName, key, value: processedRecord });
|
|
1374
1297
|
} catch (error$1) {
|
|
@@ -1385,22 +1308,12 @@ var StoreOperationsCloudflare = class extends storage.StoreOperations {
|
|
|
1385
1308
|
);
|
|
1386
1309
|
}
|
|
1387
1310
|
}
|
|
1388
|
-
ensureMetadata(metadata) {
|
|
1389
|
-
if (!metadata) return {};
|
|
1390
|
-
return typeof metadata === "string" ? JSON.parse(metadata) : metadata;
|
|
1391
|
-
}
|
|
1392
1311
|
async load({ tableName, keys }) {
|
|
1393
1312
|
try {
|
|
1394
1313
|
const key = this.getKey(tableName, keys);
|
|
1395
1314
|
const data = await this.getKV(tableName, key);
|
|
1396
1315
|
if (!data) return null;
|
|
1397
|
-
|
|
1398
|
-
...data,
|
|
1399
|
-
createdAt: storage.ensureDate(data.createdAt),
|
|
1400
|
-
updatedAt: storage.ensureDate(data.updatedAt),
|
|
1401
|
-
metadata: this.ensureMetadata(data.metadata)
|
|
1402
|
-
};
|
|
1403
|
-
return processed;
|
|
1316
|
+
return data;
|
|
1404
1317
|
} catch (error$1) {
|
|
1405
1318
|
const mastraError = new error.MastraError(
|
|
1406
1319
|
{
|
|
@@ -1424,13 +1337,7 @@ var StoreOperationsCloudflare = class extends storage.StoreOperations {
|
|
|
1424
1337
|
await Promise.all(
|
|
1425
1338
|
input.records.map(async (record) => {
|
|
1426
1339
|
const key = this.getKey(input.tableName, record);
|
|
1427
|
-
|
|
1428
|
-
...record,
|
|
1429
|
-
createdAt: record.createdAt ? storage.serializeDate(record.createdAt) : void 0,
|
|
1430
|
-
updatedAt: record.updatedAt ? storage.serializeDate(record.updatedAt) : void 0,
|
|
1431
|
-
metadata: record.metadata ? JSON.stringify(record.metadata) : void 0
|
|
1432
|
-
};
|
|
1433
|
-
await this.putKV({ tableName: input.tableName, key, value: processedRecord });
|
|
1340
|
+
await this.putKV({ tableName: input.tableName, key, value: record });
|
|
1434
1341
|
})
|
|
1435
1342
|
);
|
|
1436
1343
|
} catch (error$1) {
|
|
@@ -1588,7 +1495,7 @@ function transformScoreRow(row) {
|
|
|
1588
1495
|
deserialized.analyzeStepResult = storage.safelyParseJSON(row.analyzeStepResult);
|
|
1589
1496
|
deserialized.metadata = storage.safelyParseJSON(row.metadata);
|
|
1590
1497
|
deserialized.additionalContext = storage.safelyParseJSON(row.additionalContext);
|
|
1591
|
-
deserialized.
|
|
1498
|
+
deserialized.requestContext = storage.safelyParseJSON(row.requestContext);
|
|
1592
1499
|
deserialized.entity = storage.safelyParseJSON(row.entity);
|
|
1593
1500
|
return deserialized;
|
|
1594
1501
|
}
|
|
@@ -1623,7 +1530,7 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1623
1530
|
async saveScore(score) {
|
|
1624
1531
|
let parsedScore;
|
|
1625
1532
|
try {
|
|
1626
|
-
parsedScore =
|
|
1533
|
+
parsedScore = evals.saveScorePayloadSchema.parse(score);
|
|
1627
1534
|
} catch (error$1) {
|
|
1628
1535
|
throw new error.MastraError(
|
|
1629
1536
|
{
|
|
@@ -1674,7 +1581,7 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1674
1581
|
throw mastraError;
|
|
1675
1582
|
}
|
|
1676
1583
|
}
|
|
1677
|
-
async
|
|
1584
|
+
async listScoresByScorerId({
|
|
1678
1585
|
scorerId,
|
|
1679
1586
|
entityId,
|
|
1680
1587
|
entityType,
|
|
@@ -1704,15 +1611,17 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1704
1611
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1705
1612
|
return dateB - dateA;
|
|
1706
1613
|
});
|
|
1614
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1615
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1616
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1707
1617
|
const total = scores.length;
|
|
1708
|
-
const
|
|
1709
|
-
const end = start + pagination.perPage;
|
|
1618
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1710
1619
|
const pagedScores = scores.slice(start, end);
|
|
1711
1620
|
return {
|
|
1712
1621
|
pagination: {
|
|
1713
1622
|
total,
|
|
1714
|
-
page
|
|
1715
|
-
perPage:
|
|
1623
|
+
page,
|
|
1624
|
+
perPage: perPageForResponse,
|
|
1716
1625
|
hasMore: end < total
|
|
1717
1626
|
},
|
|
1718
1627
|
scores: pagedScores
|
|
@@ -1732,7 +1641,7 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1732
1641
|
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1733
1642
|
}
|
|
1734
1643
|
}
|
|
1735
|
-
async
|
|
1644
|
+
async listScoresByRunId({
|
|
1736
1645
|
runId,
|
|
1737
1646
|
pagination
|
|
1738
1647
|
}) {
|
|
@@ -1750,15 +1659,17 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1750
1659
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1751
1660
|
return dateB - dateA;
|
|
1752
1661
|
});
|
|
1662
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1663
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1664
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1753
1665
|
const total = scores.length;
|
|
1754
|
-
const
|
|
1755
|
-
const end = start + pagination.perPage;
|
|
1666
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1756
1667
|
const pagedScores = scores.slice(start, end);
|
|
1757
1668
|
return {
|
|
1758
1669
|
pagination: {
|
|
1759
1670
|
total,
|
|
1760
|
-
page
|
|
1761
|
-
perPage:
|
|
1671
|
+
page,
|
|
1672
|
+
perPage: perPageForResponse,
|
|
1762
1673
|
hasMore: end < total
|
|
1763
1674
|
},
|
|
1764
1675
|
scores: pagedScores
|
|
@@ -1778,7 +1689,7 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1778
1689
|
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1779
1690
|
}
|
|
1780
1691
|
}
|
|
1781
|
-
async
|
|
1692
|
+
async listScoresByEntityId({
|
|
1782
1693
|
entityId,
|
|
1783
1694
|
entityType,
|
|
1784
1695
|
pagination
|
|
@@ -1797,15 +1708,17 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1797
1708
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1798
1709
|
return dateB - dateA;
|
|
1799
1710
|
});
|
|
1711
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1712
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1713
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1800
1714
|
const total = scores.length;
|
|
1801
|
-
const
|
|
1802
|
-
const end = start + pagination.perPage;
|
|
1715
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1803
1716
|
const pagedScores = scores.slice(start, end);
|
|
1804
1717
|
return {
|
|
1805
1718
|
pagination: {
|
|
1806
1719
|
total,
|
|
1807
|
-
page
|
|
1808
|
-
perPage:
|
|
1720
|
+
page,
|
|
1721
|
+
perPage: perPageForResponse,
|
|
1809
1722
|
hasMore: end < total
|
|
1810
1723
|
},
|
|
1811
1724
|
scores: pagedScores
|
|
@@ -1825,7 +1738,7 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1825
1738
|
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1826
1739
|
}
|
|
1827
1740
|
}
|
|
1828
|
-
async
|
|
1741
|
+
async listScoresBySpan({
|
|
1829
1742
|
traceId,
|
|
1830
1743
|
spanId,
|
|
1831
1744
|
pagination
|
|
@@ -1844,21 +1757,23 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1844
1757
|
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1845
1758
|
return dateB - dateA;
|
|
1846
1759
|
});
|
|
1760
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1761
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
1762
|
+
const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1847
1763
|
const total = scores.length;
|
|
1848
|
-
const
|
|
1849
|
-
const end = start + pagination.perPage;
|
|
1764
|
+
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1850
1765
|
const pagedScores = scores.slice(start, end);
|
|
1851
1766
|
return {
|
|
1852
1767
|
pagination: {
|
|
1853
1768
|
total,
|
|
1854
|
-
page
|
|
1855
|
-
perPage:
|
|
1769
|
+
page,
|
|
1770
|
+
perPage: perPageForResponse,
|
|
1856
1771
|
hasMore: end < total
|
|
1857
1772
|
},
|
|
1858
1773
|
scores: pagedScores
|
|
1859
1774
|
};
|
|
1860
1775
|
} catch (error$1) {
|
|
1861
|
-
|
|
1776
|
+
const mastraError = new error.MastraError(
|
|
1862
1777
|
{
|
|
1863
1778
|
id: "CLOUDFLARE_STORAGE_SCORES_GET_SCORES_BY_SPAN_FAILED",
|
|
1864
1779
|
domain: error.ErrorDomain.STORAGE,
|
|
@@ -1867,129 +1782,12 @@ var ScoresStorageCloudflare = class extends storage.ScoresStorage {
|
|
|
1867
1782
|
},
|
|
1868
1783
|
error$1
|
|
1869
1784
|
);
|
|
1785
|
+
this.logger?.trackException(mastraError);
|
|
1786
|
+
this.logger?.error(mastraError.toString());
|
|
1787
|
+
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1870
1788
|
}
|
|
1871
1789
|
}
|
|
1872
1790
|
};
|
|
1873
|
-
var TracesStorageCloudflare = class extends storage.TracesStorage {
|
|
1874
|
-
operations;
|
|
1875
|
-
constructor({ operations }) {
|
|
1876
|
-
super();
|
|
1877
|
-
this.operations = operations;
|
|
1878
|
-
}
|
|
1879
|
-
async getTraces(args) {
|
|
1880
|
-
const paginatedArgs = {
|
|
1881
|
-
name: args.name,
|
|
1882
|
-
scope: args.scope,
|
|
1883
|
-
page: args.page,
|
|
1884
|
-
perPage: args.perPage,
|
|
1885
|
-
attributes: args.attributes,
|
|
1886
|
-
filters: args.filters,
|
|
1887
|
-
dateRange: args.fromDate || args.toDate ? {
|
|
1888
|
-
start: args.fromDate,
|
|
1889
|
-
end: args.toDate
|
|
1890
|
-
} : void 0
|
|
1891
|
-
};
|
|
1892
|
-
try {
|
|
1893
|
-
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1894
|
-
return result.traces;
|
|
1895
|
-
} catch (error$1) {
|
|
1896
|
-
throw new error.MastraError(
|
|
1897
|
-
{
|
|
1898
|
-
id: "CLOUDFLARE_STORAGE_GET_TRACES_ERROR",
|
|
1899
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1900
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1901
|
-
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1902
|
-
details: {
|
|
1903
|
-
name: args.name ?? "",
|
|
1904
|
-
scope: args.scope ?? ""
|
|
1905
|
-
}
|
|
1906
|
-
},
|
|
1907
|
-
error$1
|
|
1908
|
-
);
|
|
1909
|
-
}
|
|
1910
|
-
}
|
|
1911
|
-
async getTracesPaginated(args) {
|
|
1912
|
-
try {
|
|
1913
|
-
const { name, scope, attributes, filters, page = 0, perPage = 100, dateRange } = args;
|
|
1914
|
-
const prefix = this.operations.namespacePrefix ? `${this.operations.namespacePrefix}:` : "";
|
|
1915
|
-
const keyObjs = await this.operations.listKV(storage.TABLE_TRACES, { prefix: `${prefix}${storage.TABLE_TRACES}` });
|
|
1916
|
-
const traces = [];
|
|
1917
|
-
for (const { name: key } of keyObjs) {
|
|
1918
|
-
try {
|
|
1919
|
-
const data = await this.operations.getKV(storage.TABLE_TRACES, key);
|
|
1920
|
-
if (!data) continue;
|
|
1921
|
-
if (name && data.name !== name) continue;
|
|
1922
|
-
if (scope && data.scope !== scope) continue;
|
|
1923
|
-
if (attributes) {
|
|
1924
|
-
const dataAttributes = data.attributes || {};
|
|
1925
|
-
let shouldSkip = false;
|
|
1926
|
-
for (const [key2, value] of Object.entries(attributes)) {
|
|
1927
|
-
if (dataAttributes[key2] !== value) {
|
|
1928
|
-
shouldSkip = true;
|
|
1929
|
-
break;
|
|
1930
|
-
}
|
|
1931
|
-
}
|
|
1932
|
-
if (shouldSkip) continue;
|
|
1933
|
-
}
|
|
1934
|
-
if (dateRange?.start || dateRange?.end) {
|
|
1935
|
-
const traceDate = new Date(data.createdAt || 0);
|
|
1936
|
-
if (dateRange.start && traceDate < dateRange.start) continue;
|
|
1937
|
-
if (dateRange.end && traceDate > dateRange.end) continue;
|
|
1938
|
-
}
|
|
1939
|
-
if (filters) {
|
|
1940
|
-
let shouldSkip = false;
|
|
1941
|
-
for (const [key2, value] of Object.entries(filters)) {
|
|
1942
|
-
if (data[key2] !== value) {
|
|
1943
|
-
shouldSkip = true;
|
|
1944
|
-
break;
|
|
1945
|
-
}
|
|
1946
|
-
}
|
|
1947
|
-
if (shouldSkip) continue;
|
|
1948
|
-
}
|
|
1949
|
-
traces.push(data);
|
|
1950
|
-
} catch (err) {
|
|
1951
|
-
this.logger.error("Failed to parse trace:", { key, error: err });
|
|
1952
|
-
}
|
|
1953
|
-
}
|
|
1954
|
-
traces.sort((a, b) => {
|
|
1955
|
-
const aTime = new Date(a.createdAt || 0).getTime();
|
|
1956
|
-
const bTime = new Date(b.createdAt || 0).getTime();
|
|
1957
|
-
return bTime - aTime;
|
|
1958
|
-
});
|
|
1959
|
-
const total = traces.length;
|
|
1960
|
-
const start = page * perPage;
|
|
1961
|
-
const end = start + perPage;
|
|
1962
|
-
const pagedTraces = traces.slice(start, end);
|
|
1963
|
-
return {
|
|
1964
|
-
traces: pagedTraces,
|
|
1965
|
-
total,
|
|
1966
|
-
page,
|
|
1967
|
-
perPage,
|
|
1968
|
-
hasMore: end < total
|
|
1969
|
-
};
|
|
1970
|
-
} catch (error$1) {
|
|
1971
|
-
const mastraError = new error.MastraError(
|
|
1972
|
-
{
|
|
1973
|
-
id: "CLOUDFLARE_STORAGE_GET_TRACES_PAGINATED_FAILED",
|
|
1974
|
-
domain: error.ErrorDomain.STORAGE,
|
|
1975
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
1976
|
-
text: "Error getting traces with pagination"
|
|
1977
|
-
},
|
|
1978
|
-
error$1
|
|
1979
|
-
);
|
|
1980
|
-
this.logger.trackException?.(mastraError);
|
|
1981
|
-
this.logger.error(mastraError.toString());
|
|
1982
|
-
return { traces: [], total: 0, page: 0, perPage: 100, hasMore: false };
|
|
1983
|
-
}
|
|
1984
|
-
}
|
|
1985
|
-
async batchTraceInsert({ records }) {
|
|
1986
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1987
|
-
await this.operations.batchInsert({
|
|
1988
|
-
tableName: storage.TABLE_TRACES,
|
|
1989
|
-
records
|
|
1990
|
-
});
|
|
1991
|
-
}
|
|
1992
|
-
};
|
|
1993
1791
|
var WorkflowsStorageCloudflare = class extends storage.WorkflowsStorage {
|
|
1994
1792
|
operations;
|
|
1995
1793
|
constructor({ operations }) {
|
|
@@ -2007,7 +1805,7 @@ var WorkflowsStorageCloudflare = class extends storage.WorkflowsStorage {
|
|
|
2007
1805
|
// runId,
|
|
2008
1806
|
// stepId,
|
|
2009
1807
|
// result,
|
|
2010
|
-
//
|
|
1808
|
+
// requestContext,
|
|
2011
1809
|
}) {
|
|
2012
1810
|
throw new Error("Method not implemented.");
|
|
2013
1811
|
}
|
|
@@ -2107,15 +1905,28 @@ var WorkflowsStorageCloudflare = class extends storage.WorkflowsStorage {
|
|
|
2107
1905
|
if (resourceId) key += `:${resourceId}`;
|
|
2108
1906
|
return key;
|
|
2109
1907
|
}
|
|
2110
|
-
async
|
|
1908
|
+
async listWorkflowRuns({
|
|
2111
1909
|
workflowName,
|
|
2112
|
-
|
|
2113
|
-
|
|
1910
|
+
page = 0,
|
|
1911
|
+
perPage = 20,
|
|
2114
1912
|
resourceId,
|
|
2115
1913
|
fromDate,
|
|
2116
1914
|
toDate
|
|
2117
1915
|
} = {}) {
|
|
2118
1916
|
try {
|
|
1917
|
+
if (page < 0 || !Number.isInteger(page)) {
|
|
1918
|
+
throw new error.MastraError(
|
|
1919
|
+
{
|
|
1920
|
+
id: "CLOUDFLARE_STORE_INVALID_PAGE",
|
|
1921
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1922
|
+
category: error.ErrorCategory.USER,
|
|
1923
|
+
details: { page }
|
|
1924
|
+
},
|
|
1925
|
+
new Error("page must be a non-negative integer")
|
|
1926
|
+
);
|
|
1927
|
+
}
|
|
1928
|
+
const normalizedPerPage = storage.normalizePerPage(perPage, 20);
|
|
1929
|
+
const offset = page * normalizedPerPage;
|
|
2119
1930
|
const prefix = this.buildWorkflowSnapshotPrefix({ workflowName });
|
|
2120
1931
|
const keyObjs = await this.operations.listKV(storage.TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
2121
1932
|
const runs = [];
|
|
@@ -2152,7 +1963,7 @@ var WorkflowsStorageCloudflare = class extends storage.WorkflowsStorage {
|
|
|
2152
1963
|
const bDate = b.createdAt ? new Date(b.createdAt).getTime() : 0;
|
|
2153
1964
|
return bDate - aDate;
|
|
2154
1965
|
});
|
|
2155
|
-
const pagedRuns = runs.slice(offset, offset +
|
|
1966
|
+
const pagedRuns = runs.slice(offset, offset + normalizedPerPage);
|
|
2156
1967
|
return {
|
|
2157
1968
|
runs: pagedRuns,
|
|
2158
1969
|
total: runs.length
|
|
@@ -2160,7 +1971,7 @@ var WorkflowsStorageCloudflare = class extends storage.WorkflowsStorage {
|
|
|
2160
1971
|
} catch (error$1) {
|
|
2161
1972
|
const mastraError = new error.MastraError(
|
|
2162
1973
|
{
|
|
2163
|
-
id: "
|
|
1974
|
+
id: "CLOUDFLARE_STORAGE_LIST_WORKFLOW_RUNS_FAILED",
|
|
2164
1975
|
domain: error.ErrorDomain.STORAGE,
|
|
2165
1976
|
category: error.ErrorCategory.THIRD_PARTY
|
|
2166
1977
|
},
|
|
@@ -2234,14 +2045,7 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2234
2045
|
if (!config.bindings) {
|
|
2235
2046
|
throw new Error("KV bindings are required when using Workers Binding API");
|
|
2236
2047
|
}
|
|
2237
|
-
const requiredTables = [
|
|
2238
|
-
storage.TABLE_THREADS,
|
|
2239
|
-
storage.TABLE_MESSAGES,
|
|
2240
|
-
storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
2241
|
-
storage.TABLE_EVALS,
|
|
2242
|
-
storage.TABLE_SCORERS,
|
|
2243
|
-
storage.TABLE_TRACES
|
|
2244
|
-
];
|
|
2048
|
+
const requiredTables = [storage.TABLE_THREADS, storage.TABLE_MESSAGES, storage.TABLE_WORKFLOW_SNAPSHOT, storage.TABLE_SCORERS];
|
|
2245
2049
|
for (const table of requiredTables) {
|
|
2246
2050
|
if (!(table in config.bindings)) {
|
|
2247
2051
|
throw new Error(`Missing KV binding for table: ${table}`);
|
|
@@ -2261,11 +2065,13 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2261
2065
|
}
|
|
2262
2066
|
get supports() {
|
|
2263
2067
|
const supports = super.supports;
|
|
2264
|
-
supports.
|
|
2068
|
+
supports.listScoresBySpan = true;
|
|
2069
|
+
supports.resourceWorkingMemory = true;
|
|
2070
|
+
supports.selectByIncludeResourceScope = true;
|
|
2265
2071
|
return supports;
|
|
2266
2072
|
}
|
|
2267
2073
|
constructor(config) {
|
|
2268
|
-
super({ name: "Cloudflare" });
|
|
2074
|
+
super({ id: config.id, name: "Cloudflare" });
|
|
2269
2075
|
try {
|
|
2270
2076
|
if (isWorkersConfig(config)) {
|
|
2271
2077
|
this.validateWorkersConfig(config);
|
|
@@ -2287,15 +2093,9 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2287
2093
|
namespacePrefix: this.namespacePrefix,
|
|
2288
2094
|
bindings: this.bindings
|
|
2289
2095
|
});
|
|
2290
|
-
const legacyEvals = new LegacyEvalsStorageCloudflare({
|
|
2291
|
-
operations
|
|
2292
|
-
});
|
|
2293
2096
|
const workflows = new WorkflowsStorageCloudflare({
|
|
2294
2097
|
operations
|
|
2295
2098
|
});
|
|
2296
|
-
const traces = new TracesStorageCloudflare({
|
|
2297
|
-
operations
|
|
2298
|
-
});
|
|
2299
2099
|
const memory = new MemoryStorageCloudflare({
|
|
2300
2100
|
operations
|
|
2301
2101
|
});
|
|
@@ -2304,9 +2104,7 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2304
2104
|
});
|
|
2305
2105
|
this.stores = {
|
|
2306
2106
|
operations,
|
|
2307
|
-
legacyEvals,
|
|
2308
2107
|
workflows,
|
|
2309
|
-
traces,
|
|
2310
2108
|
memory,
|
|
2311
2109
|
scores
|
|
2312
2110
|
};
|
|
@@ -2348,9 +2146,6 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2348
2146
|
async getThreadById({ threadId }) {
|
|
2349
2147
|
return this.stores.memory.getThreadById({ threadId });
|
|
2350
2148
|
}
|
|
2351
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
2352
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2353
|
-
}
|
|
2354
2149
|
async saveThread({ thread }) {
|
|
2355
2150
|
return this.stores.memory.saveThread({ thread });
|
|
2356
2151
|
}
|
|
@@ -2367,22 +2162,14 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2367
2162
|
async saveMessages(args) {
|
|
2368
2163
|
return this.stores.memory.saveMessages(args);
|
|
2369
2164
|
}
|
|
2370
|
-
async getMessages({
|
|
2371
|
-
threadId,
|
|
2372
|
-
resourceId,
|
|
2373
|
-
selectBy,
|
|
2374
|
-
format
|
|
2375
|
-
}) {
|
|
2376
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format });
|
|
2377
|
-
}
|
|
2378
2165
|
async updateWorkflowResults({
|
|
2379
2166
|
workflowName,
|
|
2380
2167
|
runId,
|
|
2381
2168
|
stepId,
|
|
2382
2169
|
result,
|
|
2383
|
-
|
|
2170
|
+
requestContext
|
|
2384
2171
|
}) {
|
|
2385
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
2172
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2386
2173
|
}
|
|
2387
2174
|
async updateWorkflowState({
|
|
2388
2175
|
workflowName,
|
|
@@ -2391,11 +2178,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2391
2178
|
}) {
|
|
2392
2179
|
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2393
2180
|
}
|
|
2394
|
-
async
|
|
2395
|
-
messageIds
|
|
2396
|
-
format
|
|
2397
|
-
}) {
|
|
2398
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2181
|
+
async listMessagesById({ messageIds }) {
|
|
2182
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
2399
2183
|
}
|
|
2400
2184
|
async persistWorkflowSnapshot(params) {
|
|
2401
2185
|
return this.stores.workflows.persistWorkflowSnapshot(params);
|
|
@@ -2406,43 +2190,18 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2406
2190
|
async batchInsert(input) {
|
|
2407
2191
|
return this.stores.operations.batchInsert(input);
|
|
2408
2192
|
}
|
|
2409
|
-
async
|
|
2410
|
-
name,
|
|
2411
|
-
scope,
|
|
2412
|
-
page = 0,
|
|
2413
|
-
perPage = 100,
|
|
2414
|
-
attributes,
|
|
2415
|
-
fromDate,
|
|
2416
|
-
toDate
|
|
2417
|
-
}) {
|
|
2418
|
-
return this.stores.traces.getTraces({
|
|
2419
|
-
name,
|
|
2420
|
-
scope,
|
|
2421
|
-
page,
|
|
2422
|
-
perPage,
|
|
2423
|
-
attributes,
|
|
2424
|
-
fromDate,
|
|
2425
|
-
toDate
|
|
2426
|
-
});
|
|
2427
|
-
}
|
|
2428
|
-
async getEvalsByAgentName(agentName, type) {
|
|
2429
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2430
|
-
}
|
|
2431
|
-
async getEvals(options) {
|
|
2432
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
2433
|
-
}
|
|
2434
|
-
async getWorkflowRuns({
|
|
2193
|
+
async listWorkflowRuns({
|
|
2435
2194
|
workflowName,
|
|
2436
|
-
|
|
2437
|
-
|
|
2195
|
+
perPage = 20,
|
|
2196
|
+
page = 0,
|
|
2438
2197
|
resourceId,
|
|
2439
2198
|
fromDate,
|
|
2440
2199
|
toDate
|
|
2441
2200
|
} = {}) {
|
|
2442
|
-
return this.stores.workflows.
|
|
2201
|
+
return this.stores.workflows.listWorkflowRuns({
|
|
2443
2202
|
workflowName,
|
|
2444
|
-
|
|
2445
|
-
|
|
2203
|
+
perPage,
|
|
2204
|
+
page,
|
|
2446
2205
|
resourceId,
|
|
2447
2206
|
fromDate,
|
|
2448
2207
|
toDate
|
|
@@ -2454,15 +2213,6 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2454
2213
|
}) {
|
|
2455
2214
|
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2456
2215
|
}
|
|
2457
|
-
async getTracesPaginated(args) {
|
|
2458
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
2459
|
-
}
|
|
2460
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
2461
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2462
|
-
}
|
|
2463
|
-
async getMessagesPaginated(args) {
|
|
2464
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
2465
|
-
}
|
|
2466
2216
|
async updateMessages(args) {
|
|
2467
2217
|
return this.stores.memory.updateMessages(args);
|
|
2468
2218
|
}
|
|
@@ -2472,34 +2222,34 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
2472
2222
|
async saveScore(score) {
|
|
2473
2223
|
return this.stores.scores.saveScore(score);
|
|
2474
2224
|
}
|
|
2475
|
-
async
|
|
2225
|
+
async listScoresByRunId({
|
|
2476
2226
|
runId,
|
|
2477
2227
|
pagination
|
|
2478
2228
|
}) {
|
|
2479
|
-
return this.stores.scores.
|
|
2229
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
2480
2230
|
}
|
|
2481
|
-
async
|
|
2231
|
+
async listScoresByEntityId({
|
|
2482
2232
|
entityId,
|
|
2483
2233
|
entityType,
|
|
2484
2234
|
pagination
|
|
2485
2235
|
}) {
|
|
2486
|
-
return this.stores.scores.
|
|
2236
|
+
return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
|
|
2487
2237
|
}
|
|
2488
|
-
async
|
|
2238
|
+
async listScoresByScorerId({
|
|
2489
2239
|
scorerId,
|
|
2490
2240
|
entityId,
|
|
2491
2241
|
entityType,
|
|
2492
2242
|
source,
|
|
2493
2243
|
pagination
|
|
2494
2244
|
}) {
|
|
2495
|
-
return this.stores.scores.
|
|
2245
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, entityId, entityType, source, pagination });
|
|
2496
2246
|
}
|
|
2497
|
-
async
|
|
2247
|
+
async listScoresBySpan({
|
|
2498
2248
|
traceId,
|
|
2499
2249
|
spanId,
|
|
2500
2250
|
pagination
|
|
2501
2251
|
}) {
|
|
2502
|
-
return this.stores.scores.
|
|
2252
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2503
2253
|
}
|
|
2504
2254
|
async getResourceById({ resourceId }) {
|
|
2505
2255
|
return this.stores.memory.getResourceById({ resourceId });
|