@mastra/cloudflare 0.0.0-vnextAgentNetwork-20250602084555 → 0.0.0-vnextAgentNetwork-20250602134426
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_tsup-dts-rollup.d.cts +20 -8
- package/dist/_tsup-dts-rollup.d.ts +20 -8
- package/dist/index.cjs +46 -9
- package/dist/index.js +46 -9
- package/package.json +4 -4
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { EvalRow } from '@mastra/core/storage';
|
|
2
2
|
import type { KVNamespace as KVNamespace_2 } from '@cloudflare/workers-types';
|
|
3
3
|
import { KVNamespaceListKey as KVNamespaceListKey_2 } from '@cloudflare/workers-types';
|
|
4
|
+
import type { MastraMessageV1 } from '@mastra/core/memory';
|
|
5
|
+
import type { MastraMessageV2 } from '@mastra/core/agent';
|
|
6
|
+
import type { MastraMessageV2 as MastraMessageV2_2 } from '@mastra/core/memory';
|
|
7
|
+
import type { MastraMessageV2 as MastraMessageV2_3 } from '@mastra/core';
|
|
4
8
|
import { MastraStorage } from '@mastra/core/storage';
|
|
5
|
-
import type { MessageType } from '@mastra/core/memory';
|
|
6
|
-
import type { MessageType as MessageType_2 } from '@mastra/core';
|
|
7
9
|
import type { StorageColumn } from '@mastra/core/storage';
|
|
8
10
|
import type { StorageGetMessagesArg } from '@mastra/core/storage';
|
|
9
11
|
import type { StorageThreadType } from '@mastra/core/memory';
|
|
@@ -144,10 +146,20 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
144
146
|
}): Promise<void>;
|
|
145
147
|
private getMessageKey;
|
|
146
148
|
private getThreadMessagesKey;
|
|
147
|
-
saveMessages(
|
|
148
|
-
messages:
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
saveMessages(args: {
|
|
150
|
+
messages: MastraMessageV1[];
|
|
151
|
+
format?: undefined | 'v1';
|
|
152
|
+
}): Promise<MastraMessageV1[]>;
|
|
153
|
+
saveMessages(args: {
|
|
154
|
+
messages: MastraMessageV2[];
|
|
155
|
+
format: 'v2';
|
|
156
|
+
}): Promise<MastraMessageV2[]>;
|
|
157
|
+
getMessages(args: StorageGetMessagesArg & {
|
|
158
|
+
format?: 'v1';
|
|
159
|
+
}): Promise<MastraMessageV1[]>;
|
|
160
|
+
getMessages(args: StorageGetMessagesArg & {
|
|
161
|
+
format: 'v2';
|
|
162
|
+
}): Promise<MastraMessageV2[]>;
|
|
151
163
|
private validateWorkflowParams;
|
|
152
164
|
private validateWorkflowState;
|
|
153
165
|
private normalizeSteps;
|
|
@@ -216,7 +228,7 @@ export declare interface CloudflareWorkersConfig {
|
|
|
216
228
|
keyPrefix?: string;
|
|
217
229
|
}
|
|
218
230
|
|
|
219
|
-
export declare const createSampleMessage: (threadId: string) =>
|
|
231
|
+
export declare const createSampleMessage: (threadId: string, parts?: MastraMessageV2_3["content"]["parts"]) => MastraMessageV2_3;
|
|
220
232
|
|
|
221
233
|
export declare const createSampleThread: () => {
|
|
222
234
|
id: string;
|
|
@@ -278,7 +290,7 @@ export declare type ListOptions = {
|
|
|
278
290
|
|
|
279
291
|
export declare type RecordTypes = {
|
|
280
292
|
[TABLE_THREADS]: StorageThreadType;
|
|
281
|
-
[TABLE_MESSAGES]:
|
|
293
|
+
[TABLE_MESSAGES]: MastraMessageV2_2;
|
|
282
294
|
[TABLE_WORKFLOW_SNAPSHOT]: WorkflowRunState;
|
|
283
295
|
[TABLE_EVALS]: EvalRow;
|
|
284
296
|
[TABLE_TRACES]: any;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { EvalRow } from '@mastra/core/storage';
|
|
2
2
|
import type { KVNamespace as KVNamespace_2 } from '@cloudflare/workers-types';
|
|
3
3
|
import { KVNamespaceListKey as KVNamespaceListKey_2 } from '@cloudflare/workers-types';
|
|
4
|
+
import type { MastraMessageV1 } from '@mastra/core/memory';
|
|
5
|
+
import type { MastraMessageV2 } from '@mastra/core/agent';
|
|
6
|
+
import type { MastraMessageV2 as MastraMessageV2_2 } from '@mastra/core/memory';
|
|
7
|
+
import type { MastraMessageV2 as MastraMessageV2_3 } from '@mastra/core';
|
|
4
8
|
import { MastraStorage } from '@mastra/core/storage';
|
|
5
|
-
import type { MessageType } from '@mastra/core/memory';
|
|
6
|
-
import type { MessageType as MessageType_2 } from '@mastra/core';
|
|
7
9
|
import type { StorageColumn } from '@mastra/core/storage';
|
|
8
10
|
import type { StorageGetMessagesArg } from '@mastra/core/storage';
|
|
9
11
|
import type { StorageThreadType } from '@mastra/core/memory';
|
|
@@ -144,10 +146,20 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
144
146
|
}): Promise<void>;
|
|
145
147
|
private getMessageKey;
|
|
146
148
|
private getThreadMessagesKey;
|
|
147
|
-
saveMessages(
|
|
148
|
-
messages:
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
saveMessages(args: {
|
|
150
|
+
messages: MastraMessageV1[];
|
|
151
|
+
format?: undefined | 'v1';
|
|
152
|
+
}): Promise<MastraMessageV1[]>;
|
|
153
|
+
saveMessages(args: {
|
|
154
|
+
messages: MastraMessageV2[];
|
|
155
|
+
format: 'v2';
|
|
156
|
+
}): Promise<MastraMessageV2[]>;
|
|
157
|
+
getMessages(args: StorageGetMessagesArg & {
|
|
158
|
+
format?: 'v1';
|
|
159
|
+
}): Promise<MastraMessageV1[]>;
|
|
160
|
+
getMessages(args: StorageGetMessagesArg & {
|
|
161
|
+
format: 'v2';
|
|
162
|
+
}): Promise<MastraMessageV2[]>;
|
|
151
163
|
private validateWorkflowParams;
|
|
152
164
|
private validateWorkflowState;
|
|
153
165
|
private normalizeSteps;
|
|
@@ -216,7 +228,7 @@ export declare interface CloudflareWorkersConfig {
|
|
|
216
228
|
keyPrefix?: string;
|
|
217
229
|
}
|
|
218
230
|
|
|
219
|
-
export declare const createSampleMessage: (threadId: string) =>
|
|
231
|
+
export declare const createSampleMessage: (threadId: string, parts?: MastraMessageV2_3["content"]["parts"]) => MastraMessageV2_3;
|
|
220
232
|
|
|
221
233
|
export declare const createSampleThread: () => {
|
|
222
234
|
id: string;
|
|
@@ -278,7 +290,7 @@ export declare type ListOptions = {
|
|
|
278
290
|
|
|
279
291
|
export declare type RecordTypes = {
|
|
280
292
|
[TABLE_THREADS]: StorageThreadType;
|
|
281
|
-
[TABLE_MESSAGES]:
|
|
293
|
+
[TABLE_MESSAGES]: MastraMessageV2_2;
|
|
282
294
|
[TABLE_WORKFLOW_SNAPSHOT]: WorkflowRunState;
|
|
283
295
|
[TABLE_EVALS]: EvalRow;
|
|
284
296
|
[TABLE_TRACES]: any;
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var agent = require('@mastra/core/agent');
|
|
3
4
|
var storage = require('@mastra/core/storage');
|
|
4
5
|
var Cloudflare = require('cloudflare');
|
|
5
6
|
|
|
@@ -86,7 +87,25 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
86
87
|
}))
|
|
87
88
|
};
|
|
88
89
|
}
|
|
89
|
-
|
|
90
|
+
let allNamespaces = [];
|
|
91
|
+
let currentPage = 1;
|
|
92
|
+
const perPage = 50;
|
|
93
|
+
let morePagesExist = true;
|
|
94
|
+
while (morePagesExist) {
|
|
95
|
+
const response = await this.client.kv.namespaces.list({
|
|
96
|
+
account_id: this.accountId,
|
|
97
|
+
page: currentPage,
|
|
98
|
+
per_page: perPage
|
|
99
|
+
});
|
|
100
|
+
if (response.result) {
|
|
101
|
+
allNamespaces = allNamespaces.concat(response.result);
|
|
102
|
+
}
|
|
103
|
+
morePagesExist = response.result ? response.result.length === perPage : false;
|
|
104
|
+
if (morePagesExist) {
|
|
105
|
+
currentPage++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { result: allNamespaces };
|
|
90
109
|
}
|
|
91
110
|
async getNamespaceValue(tableName, key) {
|
|
92
111
|
try {
|
|
@@ -794,7 +813,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
794
813
|
throw error;
|
|
795
814
|
}
|
|
796
815
|
}
|
|
797
|
-
async saveMessages(
|
|
816
|
+
async saveMessages(args) {
|
|
817
|
+
const { messages, format = "v1" } = args;
|
|
798
818
|
if (!Array.isArray(messages) || messages.length === 0) return [];
|
|
799
819
|
try {
|
|
800
820
|
const validatedMessages = messages.map((message, index) => {
|
|
@@ -810,15 +830,17 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
810
830
|
return {
|
|
811
831
|
...message,
|
|
812
832
|
createdAt: this.ensureDate(message.createdAt),
|
|
813
|
-
type: message.type || "
|
|
833
|
+
type: message.type || "v2",
|
|
814
834
|
_index: index
|
|
815
835
|
};
|
|
816
836
|
});
|
|
817
837
|
const messagesByThread = validatedMessages.reduce((acc, message) => {
|
|
818
|
-
if (!acc.has(message.threadId)) {
|
|
838
|
+
if (message.threadId && !acc.has(message.threadId)) {
|
|
819
839
|
acc.set(message.threadId, []);
|
|
820
840
|
}
|
|
821
|
-
|
|
841
|
+
if (message.threadId) {
|
|
842
|
+
acc.get(message.threadId).push(message);
|
|
843
|
+
}
|
|
822
844
|
return acc;
|
|
823
845
|
}, /* @__PURE__ */ new Map());
|
|
824
846
|
await Promise.all(
|
|
@@ -830,7 +852,7 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
830
852
|
}
|
|
831
853
|
await Promise.all(
|
|
832
854
|
threadMessages.map(async (message) => {
|
|
833
|
-
const key =
|
|
855
|
+
const key = this.getMessageKey(threadId, message.id);
|
|
834
856
|
const { _index, ...cleanMessage } = message;
|
|
835
857
|
const serializedMessage = {
|
|
836
858
|
...cleanMessage,
|
|
@@ -849,14 +871,24 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
849
871
|
}
|
|
850
872
|
})
|
|
851
873
|
);
|
|
852
|
-
|
|
874
|
+
const prepared = validatedMessages.map(
|
|
875
|
+
({ _index, ...message }) => ({ ...message, type: message.type !== "v2" ? message.type : void 0 })
|
|
876
|
+
);
|
|
877
|
+
const list = new agent.MessageList().add(prepared, "memory");
|
|
878
|
+
if (format === `v2`) return list.get.all.v2();
|
|
879
|
+
return list.get.all.v1();
|
|
853
880
|
} catch (error) {
|
|
854
881
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
855
882
|
this.logger.error(`Error saving messages: ${errorMessage}`);
|
|
856
883
|
throw error;
|
|
857
884
|
}
|
|
858
885
|
}
|
|
859
|
-
async getMessages({
|
|
886
|
+
async getMessages({
|
|
887
|
+
threadId,
|
|
888
|
+
resourceId,
|
|
889
|
+
selectBy,
|
|
890
|
+
format
|
|
891
|
+
}) {
|
|
860
892
|
if (!threadId) throw new Error("threadId is required");
|
|
861
893
|
let limit = 40;
|
|
862
894
|
if (typeof selectBy?.last === "number") {
|
|
@@ -888,10 +920,14 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
888
920
|
this.logger.warn(`Error sorting messages, falling back to creation time: ${errorMessage}`);
|
|
889
921
|
messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
890
922
|
}
|
|
891
|
-
|
|
923
|
+
const prepared = messages.map(({ _index, ...message }) => ({
|
|
892
924
|
...message,
|
|
925
|
+
type: message.type === `v2` ? void 0 : message.type,
|
|
893
926
|
createdAt: this.ensureDate(message.createdAt)
|
|
894
927
|
}));
|
|
928
|
+
const list = new agent.MessageList({ threadId, resourceId }).add(prepared, "memory");
|
|
929
|
+
if (format === `v1`) return list.get.all.v1();
|
|
930
|
+
return list.get.all.v2();
|
|
895
931
|
} catch (error) {
|
|
896
932
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
897
933
|
this.logger.error(`Error retrieving messages for thread ${threadId}: ${errorMessage}`);
|
|
@@ -925,6 +961,7 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
925
961
|
runId: data.runId,
|
|
926
962
|
value: data.value,
|
|
927
963
|
context: data.context,
|
|
964
|
+
serializedStepGraph: data.serializedStepGraph,
|
|
928
965
|
suspendedPaths: data.suspendedPaths || {},
|
|
929
966
|
activePaths: data.activePaths || [],
|
|
930
967
|
timestamp: data.timestamp || Date.now()
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MessageList } from '@mastra/core/agent';
|
|
1
2
|
import { MastraStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT, TABLE_EVALS, TABLE_TRACES } from '@mastra/core/storage';
|
|
2
3
|
import Cloudflare from 'cloudflare';
|
|
3
4
|
|
|
@@ -80,7 +81,25 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
80
81
|
}))
|
|
81
82
|
};
|
|
82
83
|
}
|
|
83
|
-
|
|
84
|
+
let allNamespaces = [];
|
|
85
|
+
let currentPage = 1;
|
|
86
|
+
const perPage = 50;
|
|
87
|
+
let morePagesExist = true;
|
|
88
|
+
while (morePagesExist) {
|
|
89
|
+
const response = await this.client.kv.namespaces.list({
|
|
90
|
+
account_id: this.accountId,
|
|
91
|
+
page: currentPage,
|
|
92
|
+
per_page: perPage
|
|
93
|
+
});
|
|
94
|
+
if (response.result) {
|
|
95
|
+
allNamespaces = allNamespaces.concat(response.result);
|
|
96
|
+
}
|
|
97
|
+
morePagesExist = response.result ? response.result.length === perPage : false;
|
|
98
|
+
if (morePagesExist) {
|
|
99
|
+
currentPage++;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { result: allNamespaces };
|
|
84
103
|
}
|
|
85
104
|
async getNamespaceValue(tableName, key) {
|
|
86
105
|
try {
|
|
@@ -788,7 +807,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
788
807
|
throw error;
|
|
789
808
|
}
|
|
790
809
|
}
|
|
791
|
-
async saveMessages(
|
|
810
|
+
async saveMessages(args) {
|
|
811
|
+
const { messages, format = "v1" } = args;
|
|
792
812
|
if (!Array.isArray(messages) || messages.length === 0) return [];
|
|
793
813
|
try {
|
|
794
814
|
const validatedMessages = messages.map((message, index) => {
|
|
@@ -804,15 +824,17 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
804
824
|
return {
|
|
805
825
|
...message,
|
|
806
826
|
createdAt: this.ensureDate(message.createdAt),
|
|
807
|
-
type: message.type || "
|
|
827
|
+
type: message.type || "v2",
|
|
808
828
|
_index: index
|
|
809
829
|
};
|
|
810
830
|
});
|
|
811
831
|
const messagesByThread = validatedMessages.reduce((acc, message) => {
|
|
812
|
-
if (!acc.has(message.threadId)) {
|
|
832
|
+
if (message.threadId && !acc.has(message.threadId)) {
|
|
813
833
|
acc.set(message.threadId, []);
|
|
814
834
|
}
|
|
815
|
-
|
|
835
|
+
if (message.threadId) {
|
|
836
|
+
acc.get(message.threadId).push(message);
|
|
837
|
+
}
|
|
816
838
|
return acc;
|
|
817
839
|
}, /* @__PURE__ */ new Map());
|
|
818
840
|
await Promise.all(
|
|
@@ -824,7 +846,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
824
846
|
}
|
|
825
847
|
await Promise.all(
|
|
826
848
|
threadMessages.map(async (message) => {
|
|
827
|
-
const key =
|
|
849
|
+
const key = this.getMessageKey(threadId, message.id);
|
|
828
850
|
const { _index, ...cleanMessage } = message;
|
|
829
851
|
const serializedMessage = {
|
|
830
852
|
...cleanMessage,
|
|
@@ -843,14 +865,24 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
843
865
|
}
|
|
844
866
|
})
|
|
845
867
|
);
|
|
846
|
-
|
|
868
|
+
const prepared = validatedMessages.map(
|
|
869
|
+
({ _index, ...message }) => ({ ...message, type: message.type !== "v2" ? message.type : void 0 })
|
|
870
|
+
);
|
|
871
|
+
const list = new MessageList().add(prepared, "memory");
|
|
872
|
+
if (format === `v2`) return list.get.all.v2();
|
|
873
|
+
return list.get.all.v1();
|
|
847
874
|
} catch (error) {
|
|
848
875
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
849
876
|
this.logger.error(`Error saving messages: ${errorMessage}`);
|
|
850
877
|
throw error;
|
|
851
878
|
}
|
|
852
879
|
}
|
|
853
|
-
async getMessages({
|
|
880
|
+
async getMessages({
|
|
881
|
+
threadId,
|
|
882
|
+
resourceId,
|
|
883
|
+
selectBy,
|
|
884
|
+
format
|
|
885
|
+
}) {
|
|
854
886
|
if (!threadId) throw new Error("threadId is required");
|
|
855
887
|
let limit = 40;
|
|
856
888
|
if (typeof selectBy?.last === "number") {
|
|
@@ -882,10 +914,14 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
882
914
|
this.logger.warn(`Error sorting messages, falling back to creation time: ${errorMessage}`);
|
|
883
915
|
messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
884
916
|
}
|
|
885
|
-
|
|
917
|
+
const prepared = messages.map(({ _index, ...message }) => ({
|
|
886
918
|
...message,
|
|
919
|
+
type: message.type === `v2` ? void 0 : message.type,
|
|
887
920
|
createdAt: this.ensureDate(message.createdAt)
|
|
888
921
|
}));
|
|
922
|
+
const list = new MessageList({ threadId, resourceId }).add(prepared, "memory");
|
|
923
|
+
if (format === `v1`) return list.get.all.v1();
|
|
924
|
+
return list.get.all.v2();
|
|
889
925
|
} catch (error) {
|
|
890
926
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
891
927
|
this.logger.error(`Error retrieving messages for thread ${threadId}: ${errorMessage}`);
|
|
@@ -919,6 +955,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
919
955
|
runId: data.runId,
|
|
920
956
|
value: data.value,
|
|
921
957
|
context: data.context,
|
|
958
|
+
serializedStepGraph: data.serializedStepGraph,
|
|
922
959
|
suspendedPaths: data.suspendedPaths || {},
|
|
923
960
|
activePaths: data.activePaths || [],
|
|
924
961
|
timestamp: data.timestamp || Date.now()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/cloudflare",
|
|
3
|
-
"version": "0.0.0-vnextAgentNetwork-
|
|
3
|
+
"version": "0.0.0-vnextAgentNetwork-20250602134426",
|
|
4
4
|
"description": "Cloudflare provider for Mastra - includes db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"tsup": "^8.4.0",
|
|
36
36
|
"typescript": "^5.8.2",
|
|
37
37
|
"vitest": "^3.1.2",
|
|
38
|
-
"@internal/lint": "0.0.0-vnextAgentNetwork-
|
|
39
|
-
"@mastra/core": "0.0.0-vnextAgentNetwork-
|
|
38
|
+
"@internal/lint": "0.0.0-vnextAgentNetwork-20250602134426",
|
|
39
|
+
"@mastra/core": "0.0.0-vnextAgentNetwork-20250602134426"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@mastra/core": "^0.10.0"
|
|
42
|
+
"@mastra/core": "^0.10.0-alpha.0"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|