@mastra/cloudflare 1.2.3 → 1.3.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 +26 -0
- package/dist/chunk-E4MARS3A.cjs +2135 -0
- package/dist/chunk-E4MARS3A.cjs.map +1 -0
- package/dist/chunk-NSFHQATX.js +2128 -0
- package/dist/chunk-NSFHQATX.js.map +1 -0
- package/dist/chunk-O57GFJSB.js +2265 -0
- package/dist/chunk-O57GFJSB.js.map +1 -0
- package/dist/chunk-ZBYNKKG6.cjs +2275 -0
- package/dist/chunk-ZBYNKKG6.cjs.map +1 -0
- package/dist/do/index.cjs +32 -0
- package/dist/do/index.cjs.map +1 -0
- package/dist/do/index.d.ts +91 -0
- package/dist/do/index.d.ts.map +1 -0
- package/dist/do/index.js +3 -0
- package/dist/do/index.js.map +1 -0
- package/dist/do/storage/db/index.d.ts +76 -0
- package/dist/do/storage/db/index.d.ts.map +1 -0
- package/dist/do/storage/domains/memory/index.d.ts +60 -0
- package/dist/do/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/do/storage/domains/scores/index.d.ts +38 -0
- package/dist/do/storage/domains/scores/index.d.ts.map +1 -0
- package/dist/do/storage/domains/utils.d.ts +3 -0
- package/dist/do/storage/domains/utils.d.ts.map +1 -0
- package/dist/do/storage/domains/workflows/index.d.ts +46 -0
- package/dist/do/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/do/storage/sql-builder.d.ts +128 -0
- package/dist/do/storage/sql-builder.d.ts.map +1 -0
- package/dist/docs/SKILL.md +2 -2
- package/dist/docs/assets/SOURCE_MAP.json +56 -2
- package/dist/docs/references/reference-storage-cloudflare.md +79 -8
- package/dist/index.cjs +49 -2269
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2262
- package/dist/index.js.map +1 -1
- package/dist/kv/index.cjs +28 -0
- package/dist/kv/index.cjs.map +1 -0
- package/dist/{storage → kv}/index.d.ts +11 -7
- package/dist/kv/index.d.ts.map +1 -0
- package/dist/kv/index.js +3 -0
- package/dist/kv/index.js.map +1 -0
- package/dist/kv/storage/db/index.d.ts.map +1 -0
- package/dist/kv/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/kv/storage/domains/scores/index.d.ts.map +1 -0
- package/dist/kv/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/kv/storage/test-utils.d.ts.map +1 -0
- package/dist/kv/storage/types.d.ts.map +1 -0
- package/package.json +26 -5
- package/dist/storage/db/index.d.ts.map +0 -1
- package/dist/storage/domains/memory/index.d.ts.map +0 -1
- package/dist/storage/domains/scores/index.d.ts.map +0 -1
- package/dist/storage/domains/workflows/index.d.ts.map +0 -1
- package/dist/storage/index.d.ts.map +0 -1
- package/dist/storage/test-utils.d.ts.map +0 -1
- package/dist/storage/types.d.ts.map +0 -1
- /package/dist/{storage → kv/storage}/db/index.d.ts +0 -0
- /package/dist/{storage → kv/storage}/domains/memory/index.d.ts +0 -0
- /package/dist/{storage → kv/storage}/domains/scores/index.d.ts +0 -0
- /package/dist/{storage → kv/storage}/domains/workflows/index.d.ts +0 -0
- /package/dist/{storage → kv/storage}/test-utils.d.ts +0 -0
- /package/dist/{storage → kv/storage}/types.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -1,2264 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import Cloudflare from 'cloudflare';
|
|
4
|
-
import { MessageList } from '@mastra/core/agent';
|
|
5
|
-
import { MastraBase } from '@mastra/core/base';
|
|
6
|
-
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
7
|
-
|
|
8
|
-
// src/storage/index.ts
|
|
9
|
-
function resolveCloudflareConfig(config) {
|
|
10
|
-
if ("client" in config) {
|
|
11
|
-
return {
|
|
12
|
-
client: config.client,
|
|
13
|
-
accountId: config.accountId,
|
|
14
|
-
namespacePrefix: config.namespacePrefix
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
if ("bindings" in config) {
|
|
18
|
-
return {
|
|
19
|
-
bindings: config.bindings,
|
|
20
|
-
namespacePrefix: config.keyPrefix
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
client: new Cloudflare({ apiToken: config.apiToken }),
|
|
25
|
-
accountId: config.accountId,
|
|
26
|
-
namespacePrefix: config.namespacePrefix
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
var CloudflareKVDB = class extends MastraBase {
|
|
30
|
-
bindings;
|
|
31
|
-
client;
|
|
32
|
-
accountId;
|
|
33
|
-
namespacePrefix;
|
|
34
|
-
constructor(config) {
|
|
35
|
-
super({
|
|
36
|
-
component: "STORAGE",
|
|
37
|
-
name: "CLOUDFLARE_KV_DB"
|
|
38
|
-
});
|
|
39
|
-
this.bindings = config.bindings;
|
|
40
|
-
this.namespacePrefix = config.namespacePrefix || "";
|
|
41
|
-
this.client = config.client;
|
|
42
|
-
this.accountId = config.accountId;
|
|
43
|
-
}
|
|
44
|
-
getBinding(tableName) {
|
|
45
|
-
if (!this.bindings) {
|
|
46
|
-
throw new Error(`Cannot use Workers API binding for ${tableName}: Store initialized with REST API configuration`);
|
|
47
|
-
}
|
|
48
|
-
const binding = this.bindings[tableName];
|
|
49
|
-
if (!binding) throw new Error(`No binding found for namespace ${tableName}`);
|
|
50
|
-
return binding;
|
|
51
|
-
}
|
|
52
|
-
getKey(tableName, record) {
|
|
53
|
-
const prefix = this.namespacePrefix ? `${this.namespacePrefix}:` : "";
|
|
54
|
-
switch (tableName) {
|
|
55
|
-
case TABLE_THREADS:
|
|
56
|
-
if (!record.id) throw new Error("Thread ID is required");
|
|
57
|
-
return `${prefix}${tableName}:${record.id}`;
|
|
58
|
-
case TABLE_MESSAGES:
|
|
59
|
-
if (!record.threadId || !record.id) throw new Error("Thread ID and Message ID are required");
|
|
60
|
-
return `${prefix}${tableName}:${record.threadId}:${record.id}`;
|
|
61
|
-
case TABLE_WORKFLOW_SNAPSHOT:
|
|
62
|
-
if (!record.workflow_name || !record.run_id) {
|
|
63
|
-
throw new Error("Workflow name, and run ID are required");
|
|
64
|
-
}
|
|
65
|
-
let key = `${prefix}${tableName}:${record.workflow_name}:${record.run_id}`;
|
|
66
|
-
if (record.resourceId) {
|
|
67
|
-
key = `${key}:${record.resourceId}`;
|
|
68
|
-
}
|
|
69
|
-
return key;
|
|
70
|
-
case TABLE_TRACES:
|
|
71
|
-
if (!record.id) throw new Error("Trace ID is required");
|
|
72
|
-
return `${prefix}${tableName}:${record.id}`;
|
|
73
|
-
case TABLE_SCORERS:
|
|
74
|
-
if (!record.id) throw new Error("Score ID is required");
|
|
75
|
-
return `${prefix}${tableName}:${record.id}`;
|
|
76
|
-
default:
|
|
77
|
-
throw new Error(`Unsupported table: ${tableName}`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
getSchemaKey(tableName) {
|
|
81
|
-
const prefix = this.namespacePrefix ? `${this.namespacePrefix}:` : "";
|
|
82
|
-
return `${prefix}schema:${tableName}`;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Helper to safely parse data from KV storage
|
|
86
|
-
*/
|
|
87
|
-
safeParse(text) {
|
|
88
|
-
if (!text) return null;
|
|
89
|
-
try {
|
|
90
|
-
const data = JSON.parse(text);
|
|
91
|
-
if (data && typeof data === "object" && "value" in data) {
|
|
92
|
-
if (typeof data.value === "string") {
|
|
93
|
-
try {
|
|
94
|
-
return JSON.parse(data.value);
|
|
95
|
-
} catch {
|
|
96
|
-
return data.value;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
return data;
|
|
102
|
-
} catch (error) {
|
|
103
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
104
|
-
this.logger.error("Failed to parse text:", { message, text });
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
async createNamespaceById(title) {
|
|
109
|
-
if (this.bindings) {
|
|
110
|
-
return {
|
|
111
|
-
id: title,
|
|
112
|
-
title,
|
|
113
|
-
supports_url_encoding: true
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
return await this.client.kv.namespaces.create({
|
|
117
|
-
account_id: this.accountId,
|
|
118
|
-
title
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
async createNamespace(namespaceName) {
|
|
122
|
-
try {
|
|
123
|
-
const response = await this.createNamespaceById(namespaceName);
|
|
124
|
-
return response.id;
|
|
125
|
-
} catch (error) {
|
|
126
|
-
if (error.message && error.message.includes("already exists")) {
|
|
127
|
-
const namespaces = await this.listNamespaces();
|
|
128
|
-
const namespace = namespaces.result.find((ns) => ns.title === namespaceName);
|
|
129
|
-
if (namespace) return namespace.id;
|
|
130
|
-
}
|
|
131
|
-
this.logger.error("Error creating namespace:", error);
|
|
132
|
-
throw new Error(`Failed to create namespace ${namespaceName}: ${error.message}`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
async listNamespaces() {
|
|
136
|
-
if (this.bindings) {
|
|
137
|
-
return {
|
|
138
|
-
result: Object.keys(this.bindings).map((name) => ({
|
|
139
|
-
id: name,
|
|
140
|
-
title: name,
|
|
141
|
-
supports_url_encoding: true
|
|
142
|
-
}))
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
let allNamespaces = [];
|
|
146
|
-
let currentPage = 1;
|
|
147
|
-
const perPage = 50;
|
|
148
|
-
let morePagesExist = true;
|
|
149
|
-
while (morePagesExist) {
|
|
150
|
-
const response = await this.client.kv.namespaces.list({
|
|
151
|
-
account_id: this.accountId,
|
|
152
|
-
page: currentPage,
|
|
153
|
-
per_page: perPage
|
|
154
|
-
});
|
|
155
|
-
if (response.result) {
|
|
156
|
-
allNamespaces = allNamespaces.concat(response.result);
|
|
157
|
-
}
|
|
158
|
-
morePagesExist = response.result ? response.result.length === perPage : false;
|
|
159
|
-
if (morePagesExist) {
|
|
160
|
-
currentPage++;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return { result: allNamespaces };
|
|
164
|
-
}
|
|
165
|
-
async getNamespaceIdByName(namespaceName) {
|
|
166
|
-
try {
|
|
167
|
-
const response = await this.listNamespaces();
|
|
168
|
-
const namespace = response.result.find((ns) => ns.title === namespaceName);
|
|
169
|
-
return namespace ? namespace.id : null;
|
|
170
|
-
} catch (error) {
|
|
171
|
-
this.logger.error(`Failed to get namespace ID for ${namespaceName}:`, error);
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
async getOrCreateNamespaceId(namespaceName) {
|
|
176
|
-
let namespaceId = await this.getNamespaceIdByName(namespaceName);
|
|
177
|
-
if (!namespaceId) {
|
|
178
|
-
namespaceId = await this.createNamespace(namespaceName);
|
|
179
|
-
}
|
|
180
|
-
return namespaceId;
|
|
181
|
-
}
|
|
182
|
-
async getNamespaceId(tableName) {
|
|
183
|
-
const prefix = this.namespacePrefix ? `${this.namespacePrefix}_` : "";
|
|
184
|
-
try {
|
|
185
|
-
return await this.getOrCreateNamespaceId(`${prefix}${tableName}`);
|
|
186
|
-
} catch (error) {
|
|
187
|
-
this.logger.error("Error fetching namespace ID:", error);
|
|
188
|
-
throw new Error(`Failed to fetch namespace ID for table ${tableName}: ${error.message}`);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
async getNamespaceValue(tableName, key) {
|
|
192
|
-
try {
|
|
193
|
-
if (this.bindings) {
|
|
194
|
-
const binding = this.getBinding(tableName);
|
|
195
|
-
const result = await binding.getWithMetadata(key, "text");
|
|
196
|
-
if (!result) return null;
|
|
197
|
-
return JSON.stringify(result);
|
|
198
|
-
} else {
|
|
199
|
-
const namespaceId = await this.getNamespaceId(tableName);
|
|
200
|
-
const response = await this.client.kv.namespaces.values.get(namespaceId, key, {
|
|
201
|
-
account_id: this.accountId
|
|
202
|
-
});
|
|
203
|
-
return await response.text();
|
|
204
|
-
}
|
|
205
|
-
} catch (error) {
|
|
206
|
-
if (error.message && error.message.includes("key not found")) {
|
|
207
|
-
return null;
|
|
208
|
-
}
|
|
209
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
210
|
-
this.logger.error(`Failed to get value for ${tableName} ${key}:`, { message });
|
|
211
|
-
throw error;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
async getKV(tableName, key) {
|
|
215
|
-
try {
|
|
216
|
-
const text = await this.getNamespaceValue(tableName, key);
|
|
217
|
-
return this.safeParse(text);
|
|
218
|
-
} catch (error) {
|
|
219
|
-
this.logger.error(`Failed to get KV value for ${tableName}:${key}:`, error);
|
|
220
|
-
throw new Error(`Failed to get KV value: ${error.message}`);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
async getTableSchema(tableName) {
|
|
224
|
-
try {
|
|
225
|
-
const schemaKey = this.getSchemaKey(tableName);
|
|
226
|
-
return await this.getKV(tableName, schemaKey);
|
|
227
|
-
} catch (error) {
|
|
228
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
229
|
-
this.logger.error(`Failed to get schema for ${tableName}:`, { message });
|
|
230
|
-
return null;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
validateColumnValue(value, column) {
|
|
234
|
-
if (value === void 0 || value === null) {
|
|
235
|
-
return column.nullable ?? false;
|
|
236
|
-
}
|
|
237
|
-
switch (column.type) {
|
|
238
|
-
case "text":
|
|
239
|
-
case "uuid":
|
|
240
|
-
return typeof value === "string";
|
|
241
|
-
case "integer":
|
|
242
|
-
case "bigint":
|
|
243
|
-
return typeof value === "number";
|
|
244
|
-
case "timestamp":
|
|
245
|
-
return value instanceof Date || typeof value === "string" && !isNaN(Date.parse(value));
|
|
246
|
-
case "jsonb":
|
|
247
|
-
if (typeof value !== "object") return false;
|
|
248
|
-
try {
|
|
249
|
-
JSON.stringify(value);
|
|
250
|
-
return true;
|
|
251
|
-
} catch {
|
|
252
|
-
return false;
|
|
253
|
-
}
|
|
254
|
-
default:
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
async validateAgainstSchema(record, schema) {
|
|
259
|
-
try {
|
|
260
|
-
if (!schema || typeof schema !== "object" || schema.value === null) {
|
|
261
|
-
throw new Error("Invalid schema format");
|
|
262
|
-
}
|
|
263
|
-
for (const [columnName, column] of Object.entries(schema)) {
|
|
264
|
-
const value = record[columnName];
|
|
265
|
-
if (column.primaryKey && (value === void 0 || value === null)) {
|
|
266
|
-
throw new Error(`Missing primary key value for column ${columnName}`);
|
|
267
|
-
}
|
|
268
|
-
if (!this.validateColumnValue(value, column)) {
|
|
269
|
-
const valueType = value === null ? "null" : typeof value;
|
|
270
|
-
throw new Error(`Invalid value for column ${columnName}: expected ${column.type}, got ${valueType}`);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
} catch (error) {
|
|
274
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
275
|
-
this.logger.error(`Error validating record against schema:`, { message, record, schema });
|
|
276
|
-
throw error;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
async validateRecord(record, tableName) {
|
|
280
|
-
try {
|
|
281
|
-
if (!record || typeof record !== "object") {
|
|
282
|
-
throw new Error("Record must be an object");
|
|
283
|
-
}
|
|
284
|
-
const recordTyped = record;
|
|
285
|
-
const schema = await this.getTableSchema(tableName);
|
|
286
|
-
if (schema) {
|
|
287
|
-
await this.validateAgainstSchema(recordTyped, schema);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
switch (tableName) {
|
|
291
|
-
case TABLE_THREADS:
|
|
292
|
-
if (!("id" in recordTyped) || !("resourceId" in recordTyped) || !("title" in recordTyped)) {
|
|
293
|
-
throw new Error("Thread record missing required fields");
|
|
294
|
-
}
|
|
295
|
-
break;
|
|
296
|
-
case TABLE_MESSAGES:
|
|
297
|
-
if (!("id" in recordTyped) || !("threadId" in recordTyped) || !("content" in recordTyped) || !("role" in recordTyped)) {
|
|
298
|
-
throw new Error("Message record missing required fields");
|
|
299
|
-
}
|
|
300
|
-
break;
|
|
301
|
-
case TABLE_WORKFLOW_SNAPSHOT:
|
|
302
|
-
if (!("workflow_name" in recordTyped) || !("run_id" in recordTyped)) {
|
|
303
|
-
throw new Error("Workflow record missing required fields");
|
|
304
|
-
}
|
|
305
|
-
break;
|
|
306
|
-
case TABLE_TRACES:
|
|
307
|
-
if (!("id" in recordTyped)) {
|
|
308
|
-
throw new Error("Trace record missing required fields");
|
|
309
|
-
}
|
|
310
|
-
break;
|
|
311
|
-
case TABLE_SCORERS:
|
|
312
|
-
if (!("id" in recordTyped) || !("scorerId" in recordTyped)) {
|
|
313
|
-
throw new Error("Score record missing required fields");
|
|
314
|
-
}
|
|
315
|
-
break;
|
|
316
|
-
default:
|
|
317
|
-
throw new Error(`Unknown table type: ${tableName}`);
|
|
318
|
-
}
|
|
319
|
-
} catch (error) {
|
|
320
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
321
|
-
this.logger.error(`Failed to validate record for ${tableName}:`, { message, record });
|
|
322
|
-
throw error;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
async insert({ tableName, record }) {
|
|
326
|
-
try {
|
|
327
|
-
const key = this.getKey(tableName, record);
|
|
328
|
-
const processedRecord = { ...record };
|
|
329
|
-
await this.validateRecord(processedRecord, tableName);
|
|
330
|
-
await this.putKV({ tableName, key, value: processedRecord });
|
|
331
|
-
} catch (error) {
|
|
332
|
-
throw new MastraError(
|
|
333
|
-
{
|
|
334
|
-
id: createStorageErrorId("CLOUDFLARE", "INSERT", "FAILED"),
|
|
335
|
-
domain: ErrorDomain.STORAGE,
|
|
336
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
337
|
-
details: {
|
|
338
|
-
tableName
|
|
339
|
-
}
|
|
340
|
-
},
|
|
341
|
-
error
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
async load({ tableName, keys }) {
|
|
346
|
-
try {
|
|
347
|
-
const key = this.getKey(tableName, keys);
|
|
348
|
-
const data = await this.getKV(tableName, key);
|
|
349
|
-
if (!data) return null;
|
|
350
|
-
return data;
|
|
351
|
-
} catch (error) {
|
|
352
|
-
const mastraError = new MastraError(
|
|
353
|
-
{
|
|
354
|
-
id: createStorageErrorId("CLOUDFLARE", "LOAD", "FAILED"),
|
|
355
|
-
domain: ErrorDomain.STORAGE,
|
|
356
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
357
|
-
details: {
|
|
358
|
-
tableName
|
|
359
|
-
}
|
|
360
|
-
},
|
|
361
|
-
error
|
|
362
|
-
);
|
|
363
|
-
this.logger?.trackException(mastraError);
|
|
364
|
-
this.logger?.error(mastraError.toString());
|
|
365
|
-
return null;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
async batchInsert(input) {
|
|
369
|
-
if (!input.records || input.records.length === 0) return;
|
|
370
|
-
try {
|
|
371
|
-
await Promise.all(
|
|
372
|
-
input.records.map(async (record) => {
|
|
373
|
-
const key = this.getKey(input.tableName, record);
|
|
374
|
-
await this.putKV({ tableName: input.tableName, key, value: record });
|
|
375
|
-
})
|
|
376
|
-
);
|
|
377
|
-
} catch (error) {
|
|
378
|
-
throw new MastraError(
|
|
379
|
-
{
|
|
380
|
-
id: createStorageErrorId("CLOUDFLARE", "BATCH_INSERT", "FAILED"),
|
|
381
|
-
domain: ErrorDomain.STORAGE,
|
|
382
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
383
|
-
text: `Error in batch insert for table ${input.tableName}`,
|
|
384
|
-
details: {
|
|
385
|
-
tableName: input.tableName
|
|
386
|
-
}
|
|
387
|
-
},
|
|
388
|
-
error
|
|
389
|
-
);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
safeSerialize(data) {
|
|
393
|
-
return typeof data === "string" ? data : JSON.stringify(data);
|
|
394
|
-
}
|
|
395
|
-
async putNamespaceValue({
|
|
396
|
-
tableName,
|
|
397
|
-
key,
|
|
398
|
-
value,
|
|
399
|
-
metadata
|
|
400
|
-
}) {
|
|
401
|
-
try {
|
|
402
|
-
const serializedValue = this.safeSerialize(value);
|
|
403
|
-
const serializedMetadata = metadata ? this.safeSerialize(metadata) : "";
|
|
404
|
-
if (this.bindings) {
|
|
405
|
-
const binding = this.getBinding(tableName);
|
|
406
|
-
await binding.put(key, serializedValue, { metadata: serializedMetadata });
|
|
407
|
-
} else {
|
|
408
|
-
const namespaceId = await this.getNamespaceId(tableName);
|
|
409
|
-
await this.client.kv.namespaces.values.update(namespaceId, key, {
|
|
410
|
-
account_id: this.accountId,
|
|
411
|
-
value: serializedValue,
|
|
412
|
-
metadata: serializedMetadata
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
} catch (error) {
|
|
416
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
417
|
-
this.logger.error(`Failed to put value for ${tableName} ${key}:`, { message });
|
|
418
|
-
throw error;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
async putKV({
|
|
422
|
-
tableName,
|
|
423
|
-
key,
|
|
424
|
-
value,
|
|
425
|
-
metadata
|
|
426
|
-
}) {
|
|
427
|
-
try {
|
|
428
|
-
await this.putNamespaceValue({ tableName, key, value, metadata });
|
|
429
|
-
} catch (error) {
|
|
430
|
-
this.logger.error(`Failed to put KV value for ${tableName}:${key}:`, error);
|
|
431
|
-
throw new Error(`Failed to put KV value: ${error.message}`);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
async createTable({
|
|
435
|
-
tableName,
|
|
436
|
-
schema
|
|
437
|
-
}) {
|
|
438
|
-
try {
|
|
439
|
-
const schemaKey = this.getSchemaKey(tableName);
|
|
440
|
-
const metadata = {
|
|
441
|
-
type: "table_schema",
|
|
442
|
-
tableName,
|
|
443
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
444
|
-
};
|
|
445
|
-
await this.putKV({ tableName, key: schemaKey, value: schema, metadata });
|
|
446
|
-
} catch (error) {
|
|
447
|
-
throw new MastraError(
|
|
448
|
-
{
|
|
449
|
-
id: createStorageErrorId("CLOUDFLARE", "CREATE_TABLE", "FAILED"),
|
|
450
|
-
domain: ErrorDomain.STORAGE,
|
|
451
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
452
|
-
details: {
|
|
453
|
-
tableName
|
|
454
|
-
}
|
|
455
|
-
},
|
|
456
|
-
error
|
|
457
|
-
);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
async clearTable({ tableName }) {
|
|
461
|
-
try {
|
|
462
|
-
const keys = await this.listKV(tableName);
|
|
463
|
-
if (keys.length > 0) {
|
|
464
|
-
await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
|
|
465
|
-
}
|
|
466
|
-
} catch (error) {
|
|
467
|
-
throw new MastraError(
|
|
468
|
-
{
|
|
469
|
-
id: createStorageErrorId("CLOUDFLARE", "CLEAR_TABLE", "FAILED"),
|
|
470
|
-
domain: ErrorDomain.STORAGE,
|
|
471
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
472
|
-
details: {
|
|
473
|
-
tableName
|
|
474
|
-
}
|
|
475
|
-
},
|
|
476
|
-
error
|
|
477
|
-
);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
async dropTable({ tableName }) {
|
|
481
|
-
try {
|
|
482
|
-
const keys = await this.listKV(tableName);
|
|
483
|
-
if (keys.length > 0) {
|
|
484
|
-
await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
|
|
485
|
-
}
|
|
486
|
-
} catch (error) {
|
|
487
|
-
throw new MastraError(
|
|
488
|
-
{
|
|
489
|
-
id: createStorageErrorId("CLOUDFLARE", "DROP_TABLE", "FAILED"),
|
|
490
|
-
domain: ErrorDomain.STORAGE,
|
|
491
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
492
|
-
details: {
|
|
493
|
-
tableName
|
|
494
|
-
}
|
|
495
|
-
},
|
|
496
|
-
error
|
|
497
|
-
);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
async listNamespaceKeys(tableName, options) {
|
|
501
|
-
try {
|
|
502
|
-
if (this.bindings) {
|
|
503
|
-
const binding = this.getBinding(tableName);
|
|
504
|
-
const response = await binding.list({
|
|
505
|
-
limit: options?.limit || 1e3,
|
|
506
|
-
prefix: options?.prefix
|
|
507
|
-
});
|
|
508
|
-
return response.keys;
|
|
509
|
-
} else {
|
|
510
|
-
const namespaceId = await this.getNamespaceId(tableName);
|
|
511
|
-
const response = await this.client.kv.namespaces.keys.list(namespaceId, {
|
|
512
|
-
account_id: this.accountId,
|
|
513
|
-
limit: options?.limit || 1e3,
|
|
514
|
-
prefix: options?.prefix
|
|
515
|
-
});
|
|
516
|
-
return response.result;
|
|
517
|
-
}
|
|
518
|
-
} catch (error) {
|
|
519
|
-
throw new MastraError(
|
|
520
|
-
{
|
|
521
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_NAMESPACE_KEYS", "FAILED"),
|
|
522
|
-
domain: ErrorDomain.STORAGE,
|
|
523
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
524
|
-
details: {
|
|
525
|
-
tableName
|
|
526
|
-
}
|
|
527
|
-
},
|
|
528
|
-
error
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
async deleteNamespaceValue(tableName, key) {
|
|
533
|
-
if (this.bindings) {
|
|
534
|
-
const binding = this.getBinding(tableName);
|
|
535
|
-
await binding.delete(key);
|
|
536
|
-
} else {
|
|
537
|
-
const namespaceId = await this.getNamespaceId(tableName);
|
|
538
|
-
await this.client.kv.namespaces.values.delete(namespaceId, key, {
|
|
539
|
-
account_id: this.accountId
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
async deleteKV(tableName, key) {
|
|
544
|
-
try {
|
|
545
|
-
await this.deleteNamespaceValue(tableName, key);
|
|
546
|
-
} catch (error) {
|
|
547
|
-
this.logger.error(`Failed to delete KV value for ${tableName}:${key}:`, error);
|
|
548
|
-
throw new Error(`Failed to delete KV value: ${error.message}`);
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
async listKV(tableName, options) {
|
|
552
|
-
try {
|
|
553
|
-
return await this.listNamespaceKeys(tableName, options);
|
|
554
|
-
} catch (error) {
|
|
555
|
-
this.logger.error(`Failed to list KV for ${tableName}:`, error);
|
|
556
|
-
throw new Error(`Failed to list KV: ${error.message}`);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
};
|
|
560
|
-
|
|
561
|
-
// src/storage/domains/memory/index.ts
|
|
562
|
-
var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
563
|
-
#db;
|
|
564
|
-
constructor(config) {
|
|
565
|
-
super();
|
|
566
|
-
this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
|
|
567
|
-
}
|
|
568
|
-
async init() {
|
|
569
|
-
}
|
|
570
|
-
async dangerouslyClearAll() {
|
|
571
|
-
await this.#db.clearTable({ tableName: TABLE_MESSAGES });
|
|
572
|
-
await this.#db.clearTable({ tableName: TABLE_THREADS });
|
|
573
|
-
await this.#db.clearTable({ tableName: TABLE_RESOURCES });
|
|
574
|
-
}
|
|
575
|
-
ensureMetadata(metadata) {
|
|
576
|
-
if (!metadata) return void 0;
|
|
577
|
-
return typeof metadata === "string" ? JSON.parse(metadata) : metadata;
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Summarizes message content without exposing raw data (for logging).
|
|
581
|
-
* Returns type, length, and keys only to prevent PII leakage.
|
|
582
|
-
*/
|
|
583
|
-
summarizeMessageContent(content) {
|
|
584
|
-
if (!content) return { type: "undefined" };
|
|
585
|
-
if (typeof content === "string") return { type: "string", length: content.length };
|
|
586
|
-
if (Array.isArray(content)) return { type: "array", length: content.length };
|
|
587
|
-
if (typeof content === "object") return { type: "object", keys: Object.keys(content) };
|
|
588
|
-
return { type: typeof content };
|
|
589
|
-
}
|
|
590
|
-
async getThreadById({ threadId }) {
|
|
591
|
-
const thread = await this.#db.load({ tableName: TABLE_THREADS, keys: { id: threadId } });
|
|
592
|
-
if (!thread) return null;
|
|
593
|
-
try {
|
|
594
|
-
return {
|
|
595
|
-
...thread,
|
|
596
|
-
createdAt: ensureDate(thread.createdAt),
|
|
597
|
-
updatedAt: ensureDate(thread.updatedAt),
|
|
598
|
-
metadata: this.ensureMetadata(thread.metadata)
|
|
599
|
-
};
|
|
600
|
-
} catch (error) {
|
|
601
|
-
const mastraError = new MastraError(
|
|
602
|
-
{
|
|
603
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_THREAD_BY_ID", "FAILED"),
|
|
604
|
-
domain: ErrorDomain.STORAGE,
|
|
605
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
606
|
-
details: {
|
|
607
|
-
threadId
|
|
608
|
-
}
|
|
609
|
-
},
|
|
610
|
-
error
|
|
611
|
-
);
|
|
612
|
-
this.logger?.trackException(mastraError);
|
|
613
|
-
this.logger?.error(mastraError.toString());
|
|
614
|
-
return null;
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
async listThreads(args) {
|
|
618
|
-
const { page = 0, perPage: perPageInput, orderBy, filter } = args;
|
|
619
|
-
try {
|
|
620
|
-
this.validatePaginationInput(page, perPageInput ?? 100);
|
|
621
|
-
} catch (error) {
|
|
622
|
-
throw new MastraError(
|
|
623
|
-
{
|
|
624
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_THREADS", "INVALID_PAGE"),
|
|
625
|
-
domain: ErrorDomain.STORAGE,
|
|
626
|
-
category: ErrorCategory.USER,
|
|
627
|
-
details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
|
|
628
|
-
},
|
|
629
|
-
error instanceof Error ? error : new Error("Invalid pagination parameters")
|
|
630
|
-
);
|
|
631
|
-
}
|
|
632
|
-
const perPage = normalizePerPage(perPageInput, 100);
|
|
633
|
-
try {
|
|
634
|
-
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
635
|
-
const { field, direction } = this.parseOrderBy(orderBy);
|
|
636
|
-
const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
|
|
637
|
-
const keyObjs = await this.#db.listKV(TABLE_THREADS, { prefix: `${prefix}${TABLE_THREADS}` });
|
|
638
|
-
const threads = [];
|
|
639
|
-
for (const { name: key } of keyObjs) {
|
|
640
|
-
const data = await this.#db.getKV(TABLE_THREADS, key);
|
|
641
|
-
if (!data) continue;
|
|
642
|
-
if (filter?.resourceId && data.resourceId !== filter.resourceId) {
|
|
643
|
-
continue;
|
|
644
|
-
}
|
|
645
|
-
if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
|
|
646
|
-
const metadata = this.ensureMetadata(data.metadata);
|
|
647
|
-
if (!metadata) continue;
|
|
648
|
-
const matches = Object.entries(filter.metadata).every(([key2, value]) => metadata[key2] === value);
|
|
649
|
-
if (!matches) continue;
|
|
650
|
-
}
|
|
651
|
-
threads.push(data);
|
|
652
|
-
}
|
|
653
|
-
threads.sort((a, b) => {
|
|
654
|
-
const aTime = new Date(a[field] || 0).getTime();
|
|
655
|
-
const bTime = new Date(b[field] || 0).getTime();
|
|
656
|
-
return direction === "ASC" ? aTime - bTime : bTime - aTime;
|
|
657
|
-
});
|
|
658
|
-
const end = perPageInput === false ? threads.length : offset + perPage;
|
|
659
|
-
const paginatedThreads = threads.slice(offset, end);
|
|
660
|
-
return {
|
|
661
|
-
page,
|
|
662
|
-
perPage: perPageForResponse,
|
|
663
|
-
total: threads.length,
|
|
664
|
-
hasMore: perPageInput === false ? false : offset + perPage < threads.length,
|
|
665
|
-
threads: paginatedThreads
|
|
666
|
-
};
|
|
667
|
-
} catch (error) {
|
|
668
|
-
throw new MastraError(
|
|
669
|
-
{
|
|
670
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_THREADS", "FAILED"),
|
|
671
|
-
domain: ErrorDomain.STORAGE,
|
|
672
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
673
|
-
text: "Failed to list threads with filters"
|
|
674
|
-
},
|
|
675
|
-
error
|
|
676
|
-
);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
async saveThread({ thread }) {
|
|
680
|
-
try {
|
|
681
|
-
await this.#db.insert({ tableName: TABLE_THREADS, record: thread });
|
|
682
|
-
return thread;
|
|
683
|
-
} catch (error) {
|
|
684
|
-
throw new MastraError(
|
|
685
|
-
{
|
|
686
|
-
id: createStorageErrorId("CLOUDFLARE", "SAVE_THREAD", "FAILED"),
|
|
687
|
-
domain: ErrorDomain.STORAGE,
|
|
688
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
689
|
-
details: {
|
|
690
|
-
threadId: thread.id
|
|
691
|
-
}
|
|
692
|
-
},
|
|
693
|
-
error
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
async updateThread({
|
|
698
|
-
id,
|
|
699
|
-
title,
|
|
700
|
-
metadata
|
|
701
|
-
}) {
|
|
702
|
-
try {
|
|
703
|
-
const thread = await this.getThreadById({ threadId: id });
|
|
704
|
-
if (!thread) {
|
|
705
|
-
throw new Error(`Thread ${id} not found`);
|
|
706
|
-
}
|
|
707
|
-
const updatedThread = {
|
|
708
|
-
...thread,
|
|
709
|
-
title,
|
|
710
|
-
metadata: this.ensureMetadata({
|
|
711
|
-
...thread.metadata ?? {},
|
|
712
|
-
...metadata
|
|
713
|
-
}),
|
|
714
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
715
|
-
};
|
|
716
|
-
await this.#db.insert({ tableName: TABLE_THREADS, record: updatedThread });
|
|
717
|
-
return updatedThread;
|
|
718
|
-
} catch (error) {
|
|
719
|
-
throw new MastraError(
|
|
720
|
-
{
|
|
721
|
-
id: createStorageErrorId("CLOUDFLARE", "UPDATE_THREAD", "FAILED"),
|
|
722
|
-
domain: ErrorDomain.STORAGE,
|
|
723
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
724
|
-
details: {
|
|
725
|
-
threadId: id,
|
|
726
|
-
title
|
|
727
|
-
}
|
|
728
|
-
},
|
|
729
|
-
error
|
|
730
|
-
);
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
getMessageKey(threadId, messageId) {
|
|
734
|
-
try {
|
|
735
|
-
return this.#db.getKey(TABLE_MESSAGES, { threadId, id: messageId });
|
|
736
|
-
} catch (error) {
|
|
737
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
738
|
-
this.logger?.error(`Error getting message key for thread ${threadId} and message ${messageId}:`, { message });
|
|
739
|
-
throw error;
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
getThreadMessagesKey(threadId) {
|
|
743
|
-
try {
|
|
744
|
-
return this.#db.getKey(TABLE_MESSAGES, { threadId, id: "messages" });
|
|
745
|
-
} catch (error) {
|
|
746
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
747
|
-
this.logger?.error(`Error getting thread messages key for thread ${threadId}:`, { message });
|
|
748
|
-
throw error;
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
async deleteThread({ threadId }) {
|
|
752
|
-
try {
|
|
753
|
-
const thread = await this.getThreadById({ threadId });
|
|
754
|
-
if (!thread) {
|
|
755
|
-
throw new Error(`Thread ${threadId} not found`);
|
|
756
|
-
}
|
|
757
|
-
const messageKeys = await this.#db.listKV(TABLE_MESSAGES);
|
|
758
|
-
const threadMessageKeys = messageKeys.filter((key) => key.name.includes(`${TABLE_MESSAGES}:${threadId}:`));
|
|
759
|
-
await Promise.all([
|
|
760
|
-
// Delete message order
|
|
761
|
-
this.#db.deleteKV(TABLE_MESSAGES, this.getThreadMessagesKey(threadId)),
|
|
762
|
-
// Delete all messages
|
|
763
|
-
...threadMessageKeys.map((key) => this.#db.deleteKV(TABLE_MESSAGES, key.name)),
|
|
764
|
-
// Delete thread
|
|
765
|
-
this.#db.deleteKV(TABLE_THREADS, this.#db.getKey(TABLE_THREADS, { id: threadId }))
|
|
766
|
-
]);
|
|
767
|
-
} catch (error) {
|
|
768
|
-
throw new MastraError(
|
|
769
|
-
{
|
|
770
|
-
id: createStorageErrorId("CLOUDFLARE", "DELETE_THREAD", "FAILED"),
|
|
771
|
-
domain: ErrorDomain.STORAGE,
|
|
772
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
773
|
-
details: {
|
|
774
|
-
threadId
|
|
775
|
-
}
|
|
776
|
-
},
|
|
777
|
-
error
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
/**
|
|
782
|
-
* Searches all threads in the KV store to find a message by its ID.
|
|
783
|
-
*
|
|
784
|
-
* **Performance Warning**: This method sequentially scans all threads to locate
|
|
785
|
-
* the message. For stores with many threads, this can result in significant
|
|
786
|
-
* latency and API calls. When possible, callers should provide the `threadId`
|
|
787
|
-
* directly to avoid this full scan.
|
|
788
|
-
*
|
|
789
|
-
* @param messageId - The globally unique message ID to search for
|
|
790
|
-
* @returns The message with its threadId if found, null otherwise
|
|
791
|
-
*/
|
|
792
|
-
async findMessageInAnyThread(messageId) {
|
|
793
|
-
try {
|
|
794
|
-
const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
|
|
795
|
-
const threadKeys = await this.#db.listKV(TABLE_THREADS, { prefix: `${prefix}${TABLE_THREADS}` });
|
|
796
|
-
for (const { name: threadKey } of threadKeys) {
|
|
797
|
-
const threadId = threadKey.split(":").pop();
|
|
798
|
-
if (!threadId || threadId === "messages") continue;
|
|
799
|
-
const messageKey = this.getMessageKey(threadId, messageId);
|
|
800
|
-
const message = await this.#db.getKV(TABLE_MESSAGES, messageKey);
|
|
801
|
-
if (message) {
|
|
802
|
-
return { ...message, threadId };
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
return null;
|
|
806
|
-
} catch (error) {
|
|
807
|
-
this.logger?.error(`Error finding message ${messageId} in any thread:`, error);
|
|
808
|
-
return null;
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* Queue for serializing sorted order updates.
|
|
813
|
-
* Updates the sorted order for a given key. This operation is eventually consistent.
|
|
814
|
-
*/
|
|
815
|
-
updateQueue = /* @__PURE__ */ new Map();
|
|
816
|
-
async updateSorting(threadMessages) {
|
|
817
|
-
return threadMessages.map((msg) => ({
|
|
818
|
-
message: msg,
|
|
819
|
-
// Use _index if available, otherwise timestamp, matching Upstash
|
|
820
|
-
score: msg._index !== void 0 ? msg._index : msg.createdAt.getTime()
|
|
821
|
-
})).sort((a, b) => a.score - b.score).map((item) => ({
|
|
822
|
-
id: item.message.id,
|
|
823
|
-
score: item.score
|
|
824
|
-
}));
|
|
825
|
-
}
|
|
826
|
-
/**
|
|
827
|
-
* Updates the sorted order for a given key. This operation is eventually consistent.
|
|
828
|
-
* Note: Operations on the same orderKey are serialized using a queue to prevent
|
|
829
|
-
* concurrent updates from conflicting with each other.
|
|
830
|
-
*/
|
|
831
|
-
async updateSortedMessages(orderKey, newEntries) {
|
|
832
|
-
const currentPromise = this.updateQueue.get(orderKey) || Promise.resolve();
|
|
833
|
-
const nextPromise = currentPromise.then(async () => {
|
|
834
|
-
try {
|
|
835
|
-
const currentOrder = await this.getSortedMessages(orderKey);
|
|
836
|
-
const orderMap = new Map(currentOrder.map((entry) => [entry.id, entry]));
|
|
837
|
-
for (const entry of newEntries) {
|
|
838
|
-
orderMap.set(entry.id, entry);
|
|
839
|
-
}
|
|
840
|
-
const updatedOrder = Array.from(orderMap.values()).sort((a, b) => a.score - b.score);
|
|
841
|
-
await this.#db.putKV({
|
|
842
|
-
tableName: TABLE_MESSAGES,
|
|
843
|
-
key: orderKey,
|
|
844
|
-
value: JSON.stringify(updatedOrder)
|
|
845
|
-
});
|
|
846
|
-
} catch (error) {
|
|
847
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
848
|
-
this.logger?.error(`Error updating sorted order for key ${orderKey}:`, { message });
|
|
849
|
-
throw error;
|
|
850
|
-
} finally {
|
|
851
|
-
if (this.updateQueue.get(orderKey) === nextPromise) {
|
|
852
|
-
this.updateQueue.delete(orderKey);
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
});
|
|
856
|
-
this.updateQueue.set(orderKey, nextPromise);
|
|
857
|
-
return nextPromise;
|
|
858
|
-
}
|
|
859
|
-
async getSortedMessages(orderKey) {
|
|
860
|
-
const raw = await this.#db.getKV(TABLE_MESSAGES, orderKey);
|
|
861
|
-
if (!raw) return [];
|
|
862
|
-
try {
|
|
863
|
-
const arr = JSON.parse(typeof raw === "string" ? raw : JSON.stringify(raw));
|
|
864
|
-
return Array.isArray(arr) ? arr : [];
|
|
865
|
-
} catch (e) {
|
|
866
|
-
this.logger?.error(`Error parsing order data for key ${orderKey}:`, { e });
|
|
867
|
-
return [];
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
async migrateMessage(messageId, fromThreadId, toThreadId) {
|
|
871
|
-
try {
|
|
872
|
-
const oldMessageKey = this.getMessageKey(fromThreadId, messageId);
|
|
873
|
-
const message = await this.#db.getKV(TABLE_MESSAGES, oldMessageKey);
|
|
874
|
-
if (!message) return;
|
|
875
|
-
const updatedMessage = {
|
|
876
|
-
...message,
|
|
877
|
-
threadId: toThreadId
|
|
878
|
-
};
|
|
879
|
-
const newMessageKey = this.getMessageKey(toThreadId, messageId);
|
|
880
|
-
await this.#db.putKV({ tableName: TABLE_MESSAGES, key: newMessageKey, value: updatedMessage });
|
|
881
|
-
const oldOrderKey = this.getThreadMessagesKey(fromThreadId);
|
|
882
|
-
const oldEntries = await this.getSortedMessages(oldOrderKey);
|
|
883
|
-
const filteredEntries = oldEntries.filter((entry) => entry.id !== messageId);
|
|
884
|
-
await this.updateSortedMessages(oldOrderKey, filteredEntries);
|
|
885
|
-
const newOrderKey = this.getThreadMessagesKey(toThreadId);
|
|
886
|
-
const newEntries = await this.getSortedMessages(newOrderKey);
|
|
887
|
-
const newEntry = { id: messageId, score: Date.now() };
|
|
888
|
-
newEntries.push(newEntry);
|
|
889
|
-
await this.updateSortedMessages(newOrderKey, newEntries);
|
|
890
|
-
await this.#db.deleteKV(TABLE_MESSAGES, oldMessageKey);
|
|
891
|
-
} catch (error) {
|
|
892
|
-
this.logger?.error(`Error migrating message ${messageId} from ${fromThreadId} to ${toThreadId}:`, error);
|
|
893
|
-
throw error;
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
async saveMessages(args) {
|
|
897
|
-
const { messages } = args;
|
|
898
|
-
if (!Array.isArray(messages) || messages.length === 0) return { messages: [] };
|
|
899
|
-
try {
|
|
900
|
-
const validatedMessages = messages.map((message, index) => {
|
|
901
|
-
const errors = [];
|
|
902
|
-
if (!message.id) errors.push("id is required");
|
|
903
|
-
if (!message.threadId) errors.push("threadId is required");
|
|
904
|
-
if (!message.content) errors.push("content is required");
|
|
905
|
-
if (!message.role) errors.push("role is required");
|
|
906
|
-
if (!message.createdAt) errors.push("createdAt is required");
|
|
907
|
-
if (message.resourceId === null || message.resourceId === void 0) errors.push("resourceId is required");
|
|
908
|
-
if (errors.length > 0) {
|
|
909
|
-
throw new Error(`Invalid message at index ${index}: ${errors.join(", ")}`);
|
|
910
|
-
}
|
|
911
|
-
return {
|
|
912
|
-
...message,
|
|
913
|
-
createdAt: ensureDate(message.createdAt),
|
|
914
|
-
type: message.type || "v2",
|
|
915
|
-
_index: index
|
|
916
|
-
};
|
|
917
|
-
}).filter((m) => !!m);
|
|
918
|
-
const messageMigrationTasks = [];
|
|
919
|
-
for (const message of validatedMessages) {
|
|
920
|
-
const existingMessage = await this.findMessageInAnyThread(message.id);
|
|
921
|
-
this.logger?.debug(
|
|
922
|
-
`Checking message ${message.id}: existing=${existingMessage?.threadId}, new=${message.threadId}`
|
|
923
|
-
);
|
|
924
|
-
if (existingMessage && existingMessage.threadId && existingMessage.threadId !== message.threadId) {
|
|
925
|
-
this.logger?.debug(`Migrating message ${message.id} from ${existingMessage.threadId} to ${message.threadId}`);
|
|
926
|
-
messageMigrationTasks.push(this.migrateMessage(message.id, existingMessage.threadId, message.threadId));
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
await Promise.all(messageMigrationTasks);
|
|
930
|
-
const messagesByThread = validatedMessages.reduce((acc, message) => {
|
|
931
|
-
if (message.threadId && !acc.has(message.threadId)) {
|
|
932
|
-
acc.set(message.threadId, []);
|
|
933
|
-
}
|
|
934
|
-
if (message.threadId) {
|
|
935
|
-
acc.get(message.threadId).push(message);
|
|
936
|
-
}
|
|
937
|
-
return acc;
|
|
938
|
-
}, /* @__PURE__ */ new Map());
|
|
939
|
-
await Promise.all(
|
|
940
|
-
Array.from(messagesByThread.entries()).map(async ([threadId, threadMessages]) => {
|
|
941
|
-
try {
|
|
942
|
-
const thread = await this.getThreadById({ threadId });
|
|
943
|
-
if (!thread) {
|
|
944
|
-
throw new Error(`Thread ${threadId} not found`);
|
|
945
|
-
}
|
|
946
|
-
await Promise.all(
|
|
947
|
-
threadMessages.map(async (message) => {
|
|
948
|
-
const key = this.getMessageKey(threadId, message.id);
|
|
949
|
-
const { _index, ...cleanMessage } = message;
|
|
950
|
-
const serializedMessage = {
|
|
951
|
-
...cleanMessage,
|
|
952
|
-
createdAt: serializeDate(cleanMessage.createdAt)
|
|
953
|
-
};
|
|
954
|
-
this.logger?.debug(`Saving message ${message.id}`, {
|
|
955
|
-
contentSummary: this.summarizeMessageContent(serializedMessage.content)
|
|
956
|
-
});
|
|
957
|
-
await this.#db.putKV({ tableName: TABLE_MESSAGES, key, value: serializedMessage });
|
|
958
|
-
})
|
|
959
|
-
);
|
|
960
|
-
const orderKey = this.getThreadMessagesKey(threadId);
|
|
961
|
-
const entries = await this.updateSorting(threadMessages);
|
|
962
|
-
await this.updateSortedMessages(orderKey, entries);
|
|
963
|
-
const updatedThread = {
|
|
964
|
-
...thread,
|
|
965
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
966
|
-
};
|
|
967
|
-
await this.#db.putKV({
|
|
968
|
-
tableName: TABLE_THREADS,
|
|
969
|
-
key: this.#db.getKey(TABLE_THREADS, { id: threadId }),
|
|
970
|
-
value: updatedThread
|
|
971
|
-
});
|
|
972
|
-
} catch (error) {
|
|
973
|
-
throw new MastraError(
|
|
974
|
-
{
|
|
975
|
-
id: createStorageErrorId("CLOUDFLARE", "SAVE_MESSAGES", "FAILED"),
|
|
976
|
-
domain: ErrorDomain.STORAGE,
|
|
977
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
978
|
-
details: {
|
|
979
|
-
threadId
|
|
980
|
-
}
|
|
981
|
-
},
|
|
982
|
-
error
|
|
983
|
-
);
|
|
984
|
-
}
|
|
985
|
-
})
|
|
986
|
-
);
|
|
987
|
-
const prepared = validatedMessages.map(
|
|
988
|
-
({ _index, ...message }) => ({ ...message, type: message.type !== "v2" ? message.type : void 0 })
|
|
989
|
-
);
|
|
990
|
-
const list = new MessageList().add(prepared, "memory");
|
|
991
|
-
return { messages: list.get.all.db() };
|
|
992
|
-
} catch (error) {
|
|
993
|
-
throw new MastraError(
|
|
994
|
-
{
|
|
995
|
-
id: createStorageErrorId("CLOUDFLARE", "SAVE_MESSAGES", "FAILED"),
|
|
996
|
-
domain: ErrorDomain.STORAGE,
|
|
997
|
-
category: ErrorCategory.THIRD_PARTY
|
|
998
|
-
},
|
|
999
|
-
error
|
|
1000
|
-
);
|
|
1001
|
-
}
|
|
1002
|
-
}
|
|
1003
|
-
async getRank(orderKey, id) {
|
|
1004
|
-
const order = await this.getSortedMessages(orderKey);
|
|
1005
|
-
const index = order.findIndex((item) => item.id === id);
|
|
1006
|
-
return index >= 0 ? index : null;
|
|
1007
|
-
}
|
|
1008
|
-
async getRange(orderKey, start, end) {
|
|
1009
|
-
const order = await this.getSortedMessages(orderKey);
|
|
1010
|
-
const actualStart = start < 0 ? Math.max(0, order.length + start) : start;
|
|
1011
|
-
const actualEnd = end < 0 ? order.length + end : Math.min(end, order.length - 1);
|
|
1012
|
-
const sliced = order.slice(actualStart, actualEnd + 1);
|
|
1013
|
-
return sliced.map((item) => item.id);
|
|
1014
|
-
}
|
|
1015
|
-
async getLastN(orderKey, n) {
|
|
1016
|
-
return this.getRange(orderKey, -n, -1);
|
|
1017
|
-
}
|
|
1018
|
-
async getFullOrder(orderKey) {
|
|
1019
|
-
return this.getRange(orderKey, 0, -1);
|
|
1020
|
-
}
|
|
1021
|
-
/**
|
|
1022
|
-
* Retrieves messages specified in the include array along with their surrounding context.
|
|
1023
|
-
*
|
|
1024
|
-
* **Performance Note**: When `threadId` is not provided in an include entry, this method
|
|
1025
|
-
* must call `findMessageInAnyThread` which sequentially scans all threads in the KV store.
|
|
1026
|
-
* For optimal performance, callers should provide `threadId` in include entries when known.
|
|
1027
|
-
*
|
|
1028
|
-
* @param include - Array of message IDs to include, optionally with context windows
|
|
1029
|
-
* @param messageIds - Set to accumulate the message IDs that should be fetched
|
|
1030
|
-
*/
|
|
1031
|
-
_sortMessages(messages, field, direction) {
|
|
1032
|
-
return messages.sort((a, b) => {
|
|
1033
|
-
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
1034
|
-
const aVal = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
1035
|
-
const bVal = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
1036
|
-
if (aVal == null && bVal == null) return a.id.localeCompare(b.id);
|
|
1037
|
-
if (aVal == null) return 1;
|
|
1038
|
-
if (bVal == null) return -1;
|
|
1039
|
-
if (typeof aVal === "number" && typeof bVal === "number") {
|
|
1040
|
-
const cmp2 = direction === "ASC" ? aVal - bVal : bVal - aVal;
|
|
1041
|
-
return cmp2 !== 0 ? cmp2 : a.id.localeCompare(b.id);
|
|
1042
|
-
}
|
|
1043
|
-
const cmp = direction === "ASC" ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal));
|
|
1044
|
-
return cmp !== 0 ? cmp : a.id.localeCompare(b.id);
|
|
1045
|
-
});
|
|
1046
|
-
}
|
|
1047
|
-
async getIncludedMessagesWithContext(include, messageIds) {
|
|
1048
|
-
await Promise.all(
|
|
1049
|
-
include.map(async (item) => {
|
|
1050
|
-
let targetThreadId = item.threadId;
|
|
1051
|
-
if (!targetThreadId) {
|
|
1052
|
-
const foundMessage = await this.findMessageInAnyThread(item.id);
|
|
1053
|
-
if (!foundMessage) return;
|
|
1054
|
-
targetThreadId = foundMessage.threadId;
|
|
1055
|
-
}
|
|
1056
|
-
if (!targetThreadId) return;
|
|
1057
|
-
const threadMessagesKey = this.getThreadMessagesKey(targetThreadId);
|
|
1058
|
-
messageIds.add(item.id);
|
|
1059
|
-
if (!item.withPreviousMessages && !item.withNextMessages) return;
|
|
1060
|
-
const rank = await this.getRank(threadMessagesKey, item.id);
|
|
1061
|
-
if (rank === null) return;
|
|
1062
|
-
if (item.withPreviousMessages) {
|
|
1063
|
-
const prevIds = await this.getRange(
|
|
1064
|
-
threadMessagesKey,
|
|
1065
|
-
Math.max(0, rank - item.withPreviousMessages),
|
|
1066
|
-
rank - 1
|
|
1067
|
-
);
|
|
1068
|
-
prevIds.forEach((id) => messageIds.add(id));
|
|
1069
|
-
}
|
|
1070
|
-
if (item.withNextMessages) {
|
|
1071
|
-
const nextIds = await this.getRange(threadMessagesKey, rank + 1, rank + item.withNextMessages);
|
|
1072
|
-
nextIds.forEach((id) => messageIds.add(id));
|
|
1073
|
-
}
|
|
1074
|
-
})
|
|
1075
|
-
);
|
|
1076
|
-
}
|
|
1077
|
-
async getRecentMessages(threadId, limit, messageIds) {
|
|
1078
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
1079
|
-
if (limit <= 0) return;
|
|
1080
|
-
try {
|
|
1081
|
-
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
1082
|
-
const latestIds = await this.getLastN(threadMessagesKey, limit);
|
|
1083
|
-
latestIds.forEach((id) => messageIds.add(id));
|
|
1084
|
-
} catch {
|
|
1085
|
-
this.logger?.debug(`No message order found for thread ${threadId}, skipping latest messages`);
|
|
1086
|
-
}
|
|
1087
|
-
}
|
|
1088
|
-
/**
|
|
1089
|
-
* Fetches and parses messages from one or more threads.
|
|
1090
|
-
*
|
|
1091
|
-
* **Performance Note**: When neither `include` entries with `threadId` nor `targetThreadId`
|
|
1092
|
-
* are provided, this method falls back to `findMessageInAnyThread` which scans all threads.
|
|
1093
|
-
* For optimal performance, provide `threadId` in include entries or specify `targetThreadId`.
|
|
1094
|
-
*/
|
|
1095
|
-
async fetchAndParseMessagesFromMultipleThreads(messageIds, include, targetThreadId) {
|
|
1096
|
-
const messageIdToThreadId = /* @__PURE__ */ new Map();
|
|
1097
|
-
if (include) {
|
|
1098
|
-
for (const item of include) {
|
|
1099
|
-
if (item.threadId) {
|
|
1100
|
-
messageIdToThreadId.set(item.id, item.threadId);
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
const messages = await Promise.all(
|
|
1105
|
-
messageIds.map(async (id) => {
|
|
1106
|
-
try {
|
|
1107
|
-
let threadId = messageIdToThreadId.get(id);
|
|
1108
|
-
if (!threadId) {
|
|
1109
|
-
if (targetThreadId) {
|
|
1110
|
-
threadId = targetThreadId;
|
|
1111
|
-
} else {
|
|
1112
|
-
const foundMessage = await this.findMessageInAnyThread(id);
|
|
1113
|
-
if (foundMessage) {
|
|
1114
|
-
threadId = foundMessage.threadId;
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
if (!threadId) return null;
|
|
1119
|
-
const key = this.getMessageKey(threadId, id);
|
|
1120
|
-
const data = await this.#db.getKV(TABLE_MESSAGES, key);
|
|
1121
|
-
if (!data) return null;
|
|
1122
|
-
const parsed = typeof data === "string" ? JSON.parse(data) : data;
|
|
1123
|
-
this.logger?.debug(`Retrieved message ${id} from thread ${threadId}`, {
|
|
1124
|
-
contentSummary: this.summarizeMessageContent(parsed.content)
|
|
1125
|
-
});
|
|
1126
|
-
return parsed;
|
|
1127
|
-
} catch (error) {
|
|
1128
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1129
|
-
this.logger?.error(`Error retrieving message ${id}:`, { message });
|
|
1130
|
-
return null;
|
|
1131
|
-
}
|
|
1132
|
-
})
|
|
1133
|
-
);
|
|
1134
|
-
return messages.filter((msg) => msg !== null);
|
|
1135
|
-
}
|
|
1136
|
-
/**
|
|
1137
|
-
* Retrieves messages by their IDs.
|
|
1138
|
-
*
|
|
1139
|
-
* **Performance Warning**: This method calls `findMessageInAnyThread` for each message ID,
|
|
1140
|
-
* which scans all threads in the KV store. For large numbers of messages or threads,
|
|
1141
|
-
* this can result in significant latency. Consider using `listMessages` with specific
|
|
1142
|
-
* thread IDs when the thread context is known.
|
|
1143
|
-
*/
|
|
1144
|
-
async listMessagesById({ messageIds }) {
|
|
1145
|
-
if (messageIds.length === 0) return { messages: [] };
|
|
1146
|
-
try {
|
|
1147
|
-
const messages = (await Promise.all(messageIds.map((id) => this.findMessageInAnyThread(id)))).filter(
|
|
1148
|
-
(result) => !!result
|
|
1149
|
-
);
|
|
1150
|
-
const prepared = messages.map(({ _index, ...message }) => ({
|
|
1151
|
-
...message,
|
|
1152
|
-
...message.type !== `v2` && { type: message.type },
|
|
1153
|
-
createdAt: ensureDate(message.createdAt)
|
|
1154
|
-
}));
|
|
1155
|
-
const list = new MessageList().add(prepared, "memory");
|
|
1156
|
-
return { messages: list.get.all.db() };
|
|
1157
|
-
} catch (error) {
|
|
1158
|
-
const mastraError = new MastraError(
|
|
1159
|
-
{
|
|
1160
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES_BY_ID", "FAILED"),
|
|
1161
|
-
domain: ErrorDomain.STORAGE,
|
|
1162
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1163
|
-
text: `Error retrieving messages by ID`,
|
|
1164
|
-
details: {
|
|
1165
|
-
messageIds: JSON.stringify(messageIds)
|
|
1166
|
-
}
|
|
1167
|
-
},
|
|
1168
|
-
error
|
|
1169
|
-
);
|
|
1170
|
-
this.logger?.trackException(mastraError);
|
|
1171
|
-
this.logger?.error(mastraError.toString());
|
|
1172
|
-
return { messages: [] };
|
|
1173
|
-
}
|
|
1174
|
-
}
|
|
1175
|
-
async listMessages(args) {
|
|
1176
|
-
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
1177
|
-
const threadIds = Array.isArray(threadId) ? threadId : [threadId];
|
|
1178
|
-
const isValidThreadId = (id) => typeof id === "string" && id.trim().length > 0;
|
|
1179
|
-
if (threadIds.length === 0 || threadIds.some((id) => !isValidThreadId(id))) {
|
|
1180
|
-
throw new MastraError(
|
|
1181
|
-
{
|
|
1182
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES", "INVALID_THREAD_ID"),
|
|
1183
|
-
domain: ErrorDomain.STORAGE,
|
|
1184
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1185
|
-
details: { threadId: Array.isArray(threadId) ? JSON.stringify(threadId) : String(threadId) }
|
|
1186
|
-
},
|
|
1187
|
-
new Error("threadId must be a non-empty string or array of non-empty strings")
|
|
1188
|
-
);
|
|
1189
|
-
}
|
|
1190
|
-
const perPage = normalizePerPage(perPageInput, 40);
|
|
1191
|
-
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1192
|
-
try {
|
|
1193
|
-
if (page < 0) {
|
|
1194
|
-
throw new MastraError(
|
|
1195
|
-
{
|
|
1196
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES", "INVALID_PAGE"),
|
|
1197
|
-
domain: ErrorDomain.STORAGE,
|
|
1198
|
-
category: ErrorCategory.USER,
|
|
1199
|
-
details: { page }
|
|
1200
|
-
},
|
|
1201
|
-
new Error("page must be >= 0")
|
|
1202
|
-
);
|
|
1203
|
-
}
|
|
1204
|
-
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
1205
|
-
if (perPage === 0 && include && include.length > 0) {
|
|
1206
|
-
const includedMessageIds = /* @__PURE__ */ new Set();
|
|
1207
|
-
await this.getIncludedMessagesWithContext(include, includedMessageIds);
|
|
1208
|
-
const includedMessages2 = await this.fetchAndParseMessagesFromMultipleThreads(
|
|
1209
|
-
Array.from(includedMessageIds),
|
|
1210
|
-
include,
|
|
1211
|
-
void 0
|
|
1212
|
-
);
|
|
1213
|
-
const list2 = new MessageList().add(includedMessages2, "memory");
|
|
1214
|
-
return {
|
|
1215
|
-
messages: this._sortMessages(list2.get.all.db(), field, direction),
|
|
1216
|
-
total: 0,
|
|
1217
|
-
page,
|
|
1218
|
-
perPage: perPageForResponse,
|
|
1219
|
-
hasMore: false
|
|
1220
|
-
};
|
|
1221
|
-
}
|
|
1222
|
-
if (perPage === 0 && (!include || include.length === 0)) {
|
|
1223
|
-
return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
|
|
1224
|
-
}
|
|
1225
|
-
const threadMessageIds = /* @__PURE__ */ new Set();
|
|
1226
|
-
for (const tid of threadIds) {
|
|
1227
|
-
try {
|
|
1228
|
-
const threadMessagesKey = this.getThreadMessagesKey(tid);
|
|
1229
|
-
const allIds = await this.getFullOrder(threadMessagesKey);
|
|
1230
|
-
allIds.forEach((id) => threadMessageIds.add(id));
|
|
1231
|
-
} catch {
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
const threadMessages = await this.fetchAndParseMessagesFromMultipleThreads(
|
|
1235
|
-
Array.from(threadMessageIds),
|
|
1236
|
-
void 0,
|
|
1237
|
-
threadIds.length === 1 ? threadIds[0] : void 0
|
|
1238
|
-
);
|
|
1239
|
-
let filteredThreadMessages = threadMessages;
|
|
1240
|
-
if (resourceId) {
|
|
1241
|
-
filteredThreadMessages = filteredThreadMessages.filter((msg) => msg.resourceId === resourceId);
|
|
1242
|
-
}
|
|
1243
|
-
filteredThreadMessages = filterByDateRange(
|
|
1244
|
-
filteredThreadMessages,
|
|
1245
|
-
(msg) => new Date(msg.createdAt),
|
|
1246
|
-
filter?.dateRange
|
|
1247
|
-
);
|
|
1248
|
-
const total = filteredThreadMessages.length;
|
|
1249
|
-
filteredThreadMessages.sort((a, b) => {
|
|
1250
|
-
const timeA = new Date(a.createdAt).getTime();
|
|
1251
|
-
const timeB = new Date(b.createdAt).getTime();
|
|
1252
|
-
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
1253
|
-
if (timeDiff === 0) {
|
|
1254
|
-
return a.id.localeCompare(b.id);
|
|
1255
|
-
}
|
|
1256
|
-
return timeDiff;
|
|
1257
|
-
});
|
|
1258
|
-
let paginatedMessages;
|
|
1259
|
-
if (perPage === 0) {
|
|
1260
|
-
paginatedMessages = [];
|
|
1261
|
-
} else if (perPage === Number.MAX_SAFE_INTEGER) {
|
|
1262
|
-
paginatedMessages = filteredThreadMessages;
|
|
1263
|
-
} else {
|
|
1264
|
-
paginatedMessages = filteredThreadMessages.slice(offset, offset + perPage);
|
|
1265
|
-
}
|
|
1266
|
-
let includedMessages = [];
|
|
1267
|
-
if (include && include.length > 0) {
|
|
1268
|
-
const includedMessageIds = /* @__PURE__ */ new Set();
|
|
1269
|
-
await this.getIncludedMessagesWithContext(include, includedMessageIds);
|
|
1270
|
-
const paginatedIds = new Set(paginatedMessages.map((m) => m.id));
|
|
1271
|
-
const idsToFetch = Array.from(includedMessageIds).filter((id) => !paginatedIds.has(id));
|
|
1272
|
-
if (idsToFetch.length > 0) {
|
|
1273
|
-
includedMessages = await this.fetchAndParseMessagesFromMultipleThreads(idsToFetch, include, void 0);
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
const seenIds = /* @__PURE__ */ new Set();
|
|
1277
|
-
const allMessages = [];
|
|
1278
|
-
for (const msg of paginatedMessages) {
|
|
1279
|
-
if (!seenIds.has(msg.id)) {
|
|
1280
|
-
allMessages.push(msg);
|
|
1281
|
-
seenIds.add(msg.id);
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1284
|
-
for (const msg of includedMessages) {
|
|
1285
|
-
if (!seenIds.has(msg.id)) {
|
|
1286
|
-
allMessages.push(msg);
|
|
1287
|
-
seenIds.add(msg.id);
|
|
1288
|
-
}
|
|
1289
|
-
}
|
|
1290
|
-
allMessages.sort((a, b) => {
|
|
1291
|
-
const timeA = new Date(a.createdAt).getTime();
|
|
1292
|
-
const timeB = new Date(b.createdAt).getTime();
|
|
1293
|
-
const timeDiff = direction === "ASC" ? timeA - timeB : timeB - timeA;
|
|
1294
|
-
if (timeDiff === 0) {
|
|
1295
|
-
return a.id.localeCompare(b.id);
|
|
1296
|
-
}
|
|
1297
|
-
return timeDiff;
|
|
1298
|
-
});
|
|
1299
|
-
let filteredMessages = allMessages;
|
|
1300
|
-
const paginatedCount = paginatedMessages.length;
|
|
1301
|
-
if (total === 0 && filteredMessages.length === 0 && (!include || include.length === 0)) {
|
|
1302
|
-
return {
|
|
1303
|
-
messages: [],
|
|
1304
|
-
total: 0,
|
|
1305
|
-
page,
|
|
1306
|
-
perPage: perPageForResponse,
|
|
1307
|
-
hasMore: false
|
|
1308
|
-
};
|
|
1309
|
-
}
|
|
1310
|
-
const prepared = filteredMessages.map(({ _index, ...message }) => ({
|
|
1311
|
-
...message,
|
|
1312
|
-
type: message.type !== "v2" ? message.type : void 0,
|
|
1313
|
-
createdAt: ensureDate(message.createdAt)
|
|
1314
|
-
}));
|
|
1315
|
-
const primaryThreadId = Array.isArray(threadId) ? threadId[0] : threadId;
|
|
1316
|
-
const list = new MessageList({ threadId: primaryThreadId, resourceId }).add(
|
|
1317
|
-
prepared,
|
|
1318
|
-
"memory"
|
|
1319
|
-
);
|
|
1320
|
-
const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
|
|
1321
|
-
const threadIdSet = new Set(threadIds);
|
|
1322
|
-
const returnedThreadMessageIds = new Set(
|
|
1323
|
-
finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
|
|
1324
|
-
);
|
|
1325
|
-
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
1326
|
-
const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + paginatedCount < total;
|
|
1327
|
-
return {
|
|
1328
|
-
messages: finalMessages,
|
|
1329
|
-
total,
|
|
1330
|
-
page,
|
|
1331
|
-
perPage: perPageForResponse,
|
|
1332
|
-
hasMore
|
|
1333
|
-
};
|
|
1334
|
-
} catch (error) {
|
|
1335
|
-
const mastraError = new MastraError(
|
|
1336
|
-
{
|
|
1337
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_MESSAGES", "FAILED"),
|
|
1338
|
-
domain: ErrorDomain.STORAGE,
|
|
1339
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1340
|
-
text: `Failed to list messages for thread ${Array.isArray(threadId) ? threadId.join(",") : threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1341
|
-
details: {
|
|
1342
|
-
threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
|
|
1343
|
-
resourceId: resourceId ?? ""
|
|
1344
|
-
}
|
|
1345
|
-
},
|
|
1346
|
-
error
|
|
1347
|
-
);
|
|
1348
|
-
this.logger?.error?.(mastraError.toString());
|
|
1349
|
-
this.logger?.trackException?.(mastraError);
|
|
1350
|
-
return {
|
|
1351
|
-
messages: [],
|
|
1352
|
-
total: 0,
|
|
1353
|
-
page,
|
|
1354
|
-
perPage: perPageForResponse,
|
|
1355
|
-
hasMore: false
|
|
1356
|
-
};
|
|
1357
|
-
}
|
|
1358
|
-
}
|
|
1359
|
-
async updateMessages(args) {
|
|
1360
|
-
try {
|
|
1361
|
-
const { messages } = args;
|
|
1362
|
-
const updatedMessages = [];
|
|
1363
|
-
for (const messageUpdate of messages) {
|
|
1364
|
-
const { id, content, ...otherFields } = messageUpdate;
|
|
1365
|
-
const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
|
|
1366
|
-
const keyObjs = await this.#db.listKV(TABLE_MESSAGES, { prefix: `${prefix}${TABLE_MESSAGES}` });
|
|
1367
|
-
let existingMessage = null;
|
|
1368
|
-
let messageKey = "";
|
|
1369
|
-
for (const { name: key } of keyObjs) {
|
|
1370
|
-
const data = await this.#db.getKV(TABLE_MESSAGES, key);
|
|
1371
|
-
if (data && data.id === id) {
|
|
1372
|
-
existingMessage = data;
|
|
1373
|
-
messageKey = key;
|
|
1374
|
-
break;
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
if (!existingMessage) {
|
|
1378
|
-
continue;
|
|
1379
|
-
}
|
|
1380
|
-
const updatedMessage = {
|
|
1381
|
-
...existingMessage,
|
|
1382
|
-
...otherFields,
|
|
1383
|
-
id
|
|
1384
|
-
};
|
|
1385
|
-
if (content) {
|
|
1386
|
-
if (content.metadata !== void 0) {
|
|
1387
|
-
updatedMessage.content = {
|
|
1388
|
-
...updatedMessage.content,
|
|
1389
|
-
metadata: {
|
|
1390
|
-
...updatedMessage.content?.metadata,
|
|
1391
|
-
...content.metadata
|
|
1392
|
-
}
|
|
1393
|
-
};
|
|
1394
|
-
}
|
|
1395
|
-
if (content.content !== void 0) {
|
|
1396
|
-
updatedMessage.content = {
|
|
1397
|
-
...updatedMessage.content,
|
|
1398
|
-
content: content.content
|
|
1399
|
-
};
|
|
1400
|
-
}
|
|
1401
|
-
}
|
|
1402
|
-
if ("threadId" in messageUpdate && messageUpdate.threadId && messageUpdate.threadId !== existingMessage.threadId) {
|
|
1403
|
-
await this.#db.deleteKV(TABLE_MESSAGES, messageKey);
|
|
1404
|
-
updatedMessage.threadId = messageUpdate.threadId;
|
|
1405
|
-
const newMessageKey = this.getMessageKey(messageUpdate.threadId, id);
|
|
1406
|
-
await this.#db.putKV({
|
|
1407
|
-
tableName: TABLE_MESSAGES,
|
|
1408
|
-
key: newMessageKey,
|
|
1409
|
-
value: updatedMessage
|
|
1410
|
-
});
|
|
1411
|
-
if (existingMessage.threadId) {
|
|
1412
|
-
const sourceOrderKey = this.getThreadMessagesKey(existingMessage.threadId);
|
|
1413
|
-
const sourceEntries = await this.getSortedMessages(sourceOrderKey);
|
|
1414
|
-
const filteredEntries = sourceEntries.filter((entry) => entry.id !== id);
|
|
1415
|
-
await this.updateSortedMessages(sourceOrderKey, filteredEntries);
|
|
1416
|
-
}
|
|
1417
|
-
const destOrderKey = this.getThreadMessagesKey(messageUpdate.threadId);
|
|
1418
|
-
const destEntries = await this.getSortedMessages(destOrderKey);
|
|
1419
|
-
const newEntry = { id, score: Date.now() };
|
|
1420
|
-
destEntries.push(newEntry);
|
|
1421
|
-
await this.updateSortedMessages(destOrderKey, destEntries);
|
|
1422
|
-
} else {
|
|
1423
|
-
await this.#db.putKV({
|
|
1424
|
-
tableName: TABLE_MESSAGES,
|
|
1425
|
-
key: messageKey,
|
|
1426
|
-
value: updatedMessage
|
|
1427
|
-
});
|
|
1428
|
-
}
|
|
1429
|
-
const threadsToUpdate = /* @__PURE__ */ new Set();
|
|
1430
|
-
if (updatedMessage.threadId) {
|
|
1431
|
-
threadsToUpdate.add(updatedMessage.threadId);
|
|
1432
|
-
}
|
|
1433
|
-
if ("threadId" in messageUpdate && messageUpdate.threadId && messageUpdate.threadId !== existingMessage.threadId) {
|
|
1434
|
-
if (existingMessage.threadId) {
|
|
1435
|
-
threadsToUpdate.add(existingMessage.threadId);
|
|
1436
|
-
}
|
|
1437
|
-
threadsToUpdate.add(messageUpdate.threadId);
|
|
1438
|
-
}
|
|
1439
|
-
for (const threadId of threadsToUpdate) {
|
|
1440
|
-
const thread = await this.getThreadById({ threadId });
|
|
1441
|
-
if (thread) {
|
|
1442
|
-
const updatedThread = {
|
|
1443
|
-
...thread,
|
|
1444
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1445
|
-
};
|
|
1446
|
-
await this.#db.putKV({
|
|
1447
|
-
tableName: TABLE_THREADS,
|
|
1448
|
-
key: this.#db.getKey(TABLE_THREADS, { id: threadId }),
|
|
1449
|
-
value: updatedThread
|
|
1450
|
-
});
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
|
-
updatedMessages.push(updatedMessage);
|
|
1454
|
-
}
|
|
1455
|
-
return updatedMessages;
|
|
1456
|
-
} catch (error) {
|
|
1457
|
-
throw new MastraError(
|
|
1458
|
-
{
|
|
1459
|
-
id: createStorageErrorId("CLOUDFLARE", "UPDATE_MESSAGES", "FAILED"),
|
|
1460
|
-
domain: ErrorDomain.STORAGE,
|
|
1461
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1462
|
-
text: "Failed to update messages"
|
|
1463
|
-
},
|
|
1464
|
-
error
|
|
1465
|
-
);
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
async getResourceById({ resourceId }) {
|
|
1469
|
-
try {
|
|
1470
|
-
const data = await this.#db.getKV(TABLE_RESOURCES, resourceId);
|
|
1471
|
-
if (!data) return null;
|
|
1472
|
-
const resource = typeof data === "string" ? JSON.parse(data) : data;
|
|
1473
|
-
return {
|
|
1474
|
-
...resource,
|
|
1475
|
-
createdAt: ensureDate(resource.createdAt),
|
|
1476
|
-
updatedAt: ensureDate(resource.updatedAt),
|
|
1477
|
-
metadata: this.ensureMetadata(resource.metadata)
|
|
1478
|
-
};
|
|
1479
|
-
} catch (error) {
|
|
1480
|
-
const mastraError = new MastraError(
|
|
1481
|
-
{
|
|
1482
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_RESOURCE_BY_ID", "FAILED"),
|
|
1483
|
-
domain: ErrorDomain.STORAGE,
|
|
1484
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1485
|
-
details: {
|
|
1486
|
-
resourceId
|
|
1487
|
-
}
|
|
1488
|
-
},
|
|
1489
|
-
error
|
|
1490
|
-
);
|
|
1491
|
-
this.logger?.trackException(mastraError);
|
|
1492
|
-
this.logger?.error(mastraError.toString());
|
|
1493
|
-
return null;
|
|
1494
|
-
}
|
|
1495
|
-
}
|
|
1496
|
-
async saveResource({ resource }) {
|
|
1497
|
-
try {
|
|
1498
|
-
const resourceToSave = {
|
|
1499
|
-
...resource,
|
|
1500
|
-
metadata: resource.metadata ? JSON.stringify(resource.metadata) : null
|
|
1501
|
-
};
|
|
1502
|
-
await this.#db.putKV({
|
|
1503
|
-
tableName: TABLE_RESOURCES,
|
|
1504
|
-
key: resource.id,
|
|
1505
|
-
value: resourceToSave
|
|
1506
|
-
});
|
|
1507
|
-
return resource;
|
|
1508
|
-
} catch (error) {
|
|
1509
|
-
throw new MastraError(
|
|
1510
|
-
{
|
|
1511
|
-
id: createStorageErrorId("CLOUDFLARE", "SAVE_RESOURCE", "FAILED"),
|
|
1512
|
-
domain: ErrorDomain.STORAGE,
|
|
1513
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1514
|
-
details: {
|
|
1515
|
-
resourceId: resource.id
|
|
1516
|
-
}
|
|
1517
|
-
},
|
|
1518
|
-
error
|
|
1519
|
-
);
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
|
-
async updateResource({
|
|
1523
|
-
resourceId,
|
|
1524
|
-
workingMemory,
|
|
1525
|
-
metadata
|
|
1526
|
-
}) {
|
|
1527
|
-
const existingResource = await this.getResourceById({ resourceId });
|
|
1528
|
-
if (!existingResource) {
|
|
1529
|
-
const newResource = {
|
|
1530
|
-
id: resourceId,
|
|
1531
|
-
workingMemory,
|
|
1532
|
-
metadata: metadata || {},
|
|
1533
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
1534
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1535
|
-
};
|
|
1536
|
-
return this.saveResource({ resource: newResource });
|
|
1537
|
-
}
|
|
1538
|
-
const updatedAt = /* @__PURE__ */ new Date();
|
|
1539
|
-
const updatedResource = {
|
|
1540
|
-
...existingResource,
|
|
1541
|
-
workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
|
|
1542
|
-
metadata: {
|
|
1543
|
-
...existingResource.metadata,
|
|
1544
|
-
...metadata
|
|
1545
|
-
},
|
|
1546
|
-
updatedAt
|
|
1547
|
-
};
|
|
1548
|
-
return this.saveResource({ resource: updatedResource });
|
|
1549
|
-
}
|
|
1550
|
-
async deleteMessages(messageIds) {
|
|
1551
|
-
if (messageIds.length === 0) return;
|
|
1552
|
-
try {
|
|
1553
|
-
const threadIds = /* @__PURE__ */ new Set();
|
|
1554
|
-
for (const messageId of messageIds) {
|
|
1555
|
-
const message = await this.findMessageInAnyThread(messageId);
|
|
1556
|
-
if (message?.threadId) {
|
|
1557
|
-
threadIds.add(message.threadId);
|
|
1558
|
-
const messageKey = this.getMessageKey(message.threadId, messageId);
|
|
1559
|
-
await this.#db.deleteKV(TABLE_MESSAGES, messageKey);
|
|
1560
|
-
const orderKey = this.getThreadMessagesKey(message.threadId);
|
|
1561
|
-
const entries = await this.getSortedMessages(orderKey);
|
|
1562
|
-
const filteredEntries = entries.filter((entry) => entry.id !== messageId);
|
|
1563
|
-
if (filteredEntries.length > 0) {
|
|
1564
|
-
await this.#db.putKV({
|
|
1565
|
-
tableName: TABLE_MESSAGES,
|
|
1566
|
-
key: orderKey,
|
|
1567
|
-
value: JSON.stringify(filteredEntries)
|
|
1568
|
-
});
|
|
1569
|
-
} else {
|
|
1570
|
-
await this.#db.deleteKV(TABLE_MESSAGES, orderKey);
|
|
1571
|
-
}
|
|
1572
|
-
}
|
|
1573
|
-
}
|
|
1574
|
-
for (const threadId of threadIds) {
|
|
1575
|
-
const thread = await this.getThreadById({ threadId });
|
|
1576
|
-
if (thread) {
|
|
1577
|
-
const updatedThread = {
|
|
1578
|
-
...thread,
|
|
1579
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1580
|
-
};
|
|
1581
|
-
await this.#db.putKV({
|
|
1582
|
-
tableName: TABLE_THREADS,
|
|
1583
|
-
key: this.#db.getKey(TABLE_THREADS, { id: threadId }),
|
|
1584
|
-
value: updatedThread
|
|
1585
|
-
});
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
} catch (error) {
|
|
1589
|
-
throw new MastraError(
|
|
1590
|
-
{
|
|
1591
|
-
id: createStorageErrorId("CLOUDFLARE", "DELETE_MESSAGES", "FAILED"),
|
|
1592
|
-
domain: ErrorDomain.STORAGE,
|
|
1593
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1594
|
-
details: { messageIds: JSON.stringify(messageIds) }
|
|
1595
|
-
},
|
|
1596
|
-
error
|
|
1597
|
-
);
|
|
1598
|
-
}
|
|
1599
|
-
}
|
|
1600
|
-
};
|
|
1601
|
-
function transformScoreRow(row) {
|
|
1602
|
-
return transformScoreRow$1(row);
|
|
1603
|
-
}
|
|
1604
|
-
var ScoresStorageCloudflare = class extends ScoresStorage {
|
|
1605
|
-
#db;
|
|
1606
|
-
constructor(config) {
|
|
1607
|
-
super();
|
|
1608
|
-
this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
|
|
1609
|
-
}
|
|
1610
|
-
async init() {
|
|
1611
|
-
}
|
|
1612
|
-
async dangerouslyClearAll() {
|
|
1613
|
-
await this.#db.clearTable({ tableName: TABLE_SCORERS });
|
|
1614
|
-
}
|
|
1615
|
-
async getScoreById({ id }) {
|
|
1616
|
-
try {
|
|
1617
|
-
const score = await this.#db.getKV(TABLE_SCORERS, id);
|
|
1618
|
-
if (!score) {
|
|
1619
|
-
return null;
|
|
1620
|
-
}
|
|
1621
|
-
return transformScoreRow(score);
|
|
1622
|
-
} catch (error) {
|
|
1623
|
-
const mastraError = new MastraError(
|
|
1624
|
-
{
|
|
1625
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_SCORE_BY_ID", "FAILED"),
|
|
1626
|
-
domain: ErrorDomain.STORAGE,
|
|
1627
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1628
|
-
text: `Failed to get score by id: ${id}`
|
|
1629
|
-
},
|
|
1630
|
-
error
|
|
1631
|
-
);
|
|
1632
|
-
this.logger?.trackException(mastraError);
|
|
1633
|
-
this.logger?.error(mastraError.toString());
|
|
1634
|
-
return null;
|
|
1635
|
-
}
|
|
1636
|
-
}
|
|
1637
|
-
async saveScore(score) {
|
|
1638
|
-
let parsedScore;
|
|
1639
|
-
try {
|
|
1640
|
-
parsedScore = saveScorePayloadSchema.parse(score);
|
|
1641
|
-
} catch (error) {
|
|
1642
|
-
throw new MastraError(
|
|
1643
|
-
{
|
|
1644
|
-
id: createStorageErrorId("CLOUDFLARE", "SAVE_SCORE", "VALIDATION_FAILED"),
|
|
1645
|
-
domain: ErrorDomain.STORAGE,
|
|
1646
|
-
category: ErrorCategory.USER,
|
|
1647
|
-
details: {
|
|
1648
|
-
scorer: typeof score.scorer?.id === "string" ? score.scorer.id : String(score.scorer?.id ?? "unknown"),
|
|
1649
|
-
entityId: score.entityId ?? "unknown",
|
|
1650
|
-
entityType: score.entityType ?? "unknown",
|
|
1651
|
-
traceId: score.traceId ?? "",
|
|
1652
|
-
spanId: score.spanId ?? ""
|
|
1653
|
-
}
|
|
1654
|
-
},
|
|
1655
|
-
error
|
|
1656
|
-
);
|
|
1657
|
-
}
|
|
1658
|
-
const id = crypto.randomUUID();
|
|
1659
|
-
try {
|
|
1660
|
-
const serializedRecord = {};
|
|
1661
|
-
for (const [key, value] of Object.entries(parsedScore)) {
|
|
1662
|
-
if (value !== null && value !== void 0) {
|
|
1663
|
-
if (typeof value === "object") {
|
|
1664
|
-
serializedRecord[key] = JSON.stringify(value);
|
|
1665
|
-
} else {
|
|
1666
|
-
serializedRecord[key] = value;
|
|
1667
|
-
}
|
|
1668
|
-
} else {
|
|
1669
|
-
serializedRecord[key] = null;
|
|
1670
|
-
}
|
|
1671
|
-
}
|
|
1672
|
-
const now = /* @__PURE__ */ new Date();
|
|
1673
|
-
serializedRecord.id = id;
|
|
1674
|
-
serializedRecord.createdAt = now.toISOString();
|
|
1675
|
-
serializedRecord.updatedAt = now.toISOString();
|
|
1676
|
-
await this.#db.putKV({
|
|
1677
|
-
tableName: TABLE_SCORERS,
|
|
1678
|
-
key: id,
|
|
1679
|
-
value: serializedRecord
|
|
1680
|
-
});
|
|
1681
|
-
return { score: { ...parsedScore, id, createdAt: now, updatedAt: now } };
|
|
1682
|
-
} catch (error) {
|
|
1683
|
-
const mastraError = new MastraError(
|
|
1684
|
-
{
|
|
1685
|
-
id: createStorageErrorId("CLOUDFLARE", "SAVE_SCORE", "FAILED"),
|
|
1686
|
-
domain: ErrorDomain.STORAGE,
|
|
1687
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1688
|
-
details: { id }
|
|
1689
|
-
},
|
|
1690
|
-
error
|
|
1691
|
-
);
|
|
1692
|
-
this.logger?.trackException(mastraError);
|
|
1693
|
-
this.logger?.error(mastraError.toString());
|
|
1694
|
-
throw mastraError;
|
|
1695
|
-
}
|
|
1696
|
-
}
|
|
1697
|
-
async listScoresByScorerId({
|
|
1698
|
-
scorerId,
|
|
1699
|
-
entityId,
|
|
1700
|
-
entityType,
|
|
1701
|
-
source,
|
|
1702
|
-
pagination
|
|
1703
|
-
}) {
|
|
1704
|
-
try {
|
|
1705
|
-
const keys = await this.#db.listKV(TABLE_SCORERS);
|
|
1706
|
-
const scores = [];
|
|
1707
|
-
for (const { name: key } of keys) {
|
|
1708
|
-
const score = await this.#db.getKV(TABLE_SCORERS, key);
|
|
1709
|
-
if (entityId && score.entityId !== entityId) {
|
|
1710
|
-
continue;
|
|
1711
|
-
}
|
|
1712
|
-
if (entityType && score.entityType !== entityType) {
|
|
1713
|
-
continue;
|
|
1714
|
-
}
|
|
1715
|
-
if (source && score.source !== source) {
|
|
1716
|
-
continue;
|
|
1717
|
-
}
|
|
1718
|
-
if (score && score.scorerId === scorerId) {
|
|
1719
|
-
scores.push(transformScoreRow(score));
|
|
1720
|
-
}
|
|
1721
|
-
}
|
|
1722
|
-
scores.sort((a, b) => {
|
|
1723
|
-
const dateA = new Date(a.createdAt || 0).getTime();
|
|
1724
|
-
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1725
|
-
return dateB - dateA;
|
|
1726
|
-
});
|
|
1727
|
-
const { page, perPage: perPageInput } = pagination;
|
|
1728
|
-
const perPage = normalizePerPage(perPageInput, 100);
|
|
1729
|
-
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1730
|
-
const total = scores.length;
|
|
1731
|
-
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1732
|
-
const pagedScores = scores.slice(start, end);
|
|
1733
|
-
return {
|
|
1734
|
-
pagination: {
|
|
1735
|
-
total,
|
|
1736
|
-
page,
|
|
1737
|
-
perPage: perPageForResponse,
|
|
1738
|
-
hasMore: end < total
|
|
1739
|
-
},
|
|
1740
|
-
scores: pagedScores
|
|
1741
|
-
};
|
|
1742
|
-
} catch (error) {
|
|
1743
|
-
const mastraError = new MastraError(
|
|
1744
|
-
{
|
|
1745
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_SCORER_ID", "FAILED"),
|
|
1746
|
-
domain: ErrorDomain.STORAGE,
|
|
1747
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1748
|
-
text: `Failed to get scores by scorer id: ${scorerId}`
|
|
1749
|
-
},
|
|
1750
|
-
error
|
|
1751
|
-
);
|
|
1752
|
-
this.logger?.trackException(mastraError);
|
|
1753
|
-
this.logger?.error(mastraError.toString());
|
|
1754
|
-
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1755
|
-
}
|
|
1756
|
-
}
|
|
1757
|
-
async listScoresByRunId({
|
|
1758
|
-
runId,
|
|
1759
|
-
pagination
|
|
1760
|
-
}) {
|
|
1761
|
-
try {
|
|
1762
|
-
const keys = await this.#db.listKV(TABLE_SCORERS);
|
|
1763
|
-
const scores = [];
|
|
1764
|
-
for (const { name: key } of keys) {
|
|
1765
|
-
const score = await this.#db.getKV(TABLE_SCORERS, key);
|
|
1766
|
-
if (score && score.runId === runId) {
|
|
1767
|
-
scores.push(transformScoreRow(score));
|
|
1768
|
-
}
|
|
1769
|
-
}
|
|
1770
|
-
scores.sort((a, b) => {
|
|
1771
|
-
const dateA = new Date(a.createdAt || 0).getTime();
|
|
1772
|
-
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1773
|
-
return dateB - dateA;
|
|
1774
|
-
});
|
|
1775
|
-
const { page, perPage: perPageInput } = pagination;
|
|
1776
|
-
const perPage = normalizePerPage(perPageInput, 100);
|
|
1777
|
-
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1778
|
-
const total = scores.length;
|
|
1779
|
-
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1780
|
-
const pagedScores = scores.slice(start, end);
|
|
1781
|
-
return {
|
|
1782
|
-
pagination: {
|
|
1783
|
-
total,
|
|
1784
|
-
page,
|
|
1785
|
-
perPage: perPageForResponse,
|
|
1786
|
-
hasMore: end < total
|
|
1787
|
-
},
|
|
1788
|
-
scores: pagedScores
|
|
1789
|
-
};
|
|
1790
|
-
} catch (error) {
|
|
1791
|
-
const mastraError = new MastraError(
|
|
1792
|
-
{
|
|
1793
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_RUN_ID", "FAILED"),
|
|
1794
|
-
domain: ErrorDomain.STORAGE,
|
|
1795
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1796
|
-
text: `Failed to get scores by run id: ${runId}`
|
|
1797
|
-
},
|
|
1798
|
-
error
|
|
1799
|
-
);
|
|
1800
|
-
this.logger?.trackException(mastraError);
|
|
1801
|
-
this.logger?.error(mastraError.toString());
|
|
1802
|
-
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1803
|
-
}
|
|
1804
|
-
}
|
|
1805
|
-
async listScoresByEntityId({
|
|
1806
|
-
entityId,
|
|
1807
|
-
entityType,
|
|
1808
|
-
pagination
|
|
1809
|
-
}) {
|
|
1810
|
-
try {
|
|
1811
|
-
const keys = await this.#db.listKV(TABLE_SCORERS);
|
|
1812
|
-
const scores = [];
|
|
1813
|
-
for (const { name: key } of keys) {
|
|
1814
|
-
const score = await this.#db.getKV(TABLE_SCORERS, key);
|
|
1815
|
-
if (score && score.entityId === entityId && score.entityType === entityType) {
|
|
1816
|
-
scores.push(transformScoreRow(score));
|
|
1817
|
-
}
|
|
1818
|
-
}
|
|
1819
|
-
scores.sort((a, b) => {
|
|
1820
|
-
const dateA = new Date(a.createdAt || 0).getTime();
|
|
1821
|
-
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1822
|
-
return dateB - dateA;
|
|
1823
|
-
});
|
|
1824
|
-
const { page, perPage: perPageInput } = pagination;
|
|
1825
|
-
const perPage = normalizePerPage(perPageInput, 100);
|
|
1826
|
-
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1827
|
-
const total = scores.length;
|
|
1828
|
-
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1829
|
-
const pagedScores = scores.slice(start, end);
|
|
1830
|
-
return {
|
|
1831
|
-
pagination: {
|
|
1832
|
-
total,
|
|
1833
|
-
page,
|
|
1834
|
-
perPage: perPageForResponse,
|
|
1835
|
-
hasMore: end < total
|
|
1836
|
-
},
|
|
1837
|
-
scores: pagedScores
|
|
1838
|
-
};
|
|
1839
|
-
} catch (error) {
|
|
1840
|
-
const mastraError = new MastraError(
|
|
1841
|
-
{
|
|
1842
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_ENTITY_ID", "FAILED"),
|
|
1843
|
-
domain: ErrorDomain.STORAGE,
|
|
1844
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1845
|
-
text: `Failed to get scores by entity id: ${entityId}, type: ${entityType}`
|
|
1846
|
-
},
|
|
1847
|
-
error
|
|
1848
|
-
);
|
|
1849
|
-
this.logger?.trackException(mastraError);
|
|
1850
|
-
this.logger?.error(mastraError.toString());
|
|
1851
|
-
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1852
|
-
}
|
|
1853
|
-
}
|
|
1854
|
-
async listScoresBySpan({
|
|
1855
|
-
traceId,
|
|
1856
|
-
spanId,
|
|
1857
|
-
pagination
|
|
1858
|
-
}) {
|
|
1859
|
-
try {
|
|
1860
|
-
const keys = await this.#db.listKV(TABLE_SCORERS);
|
|
1861
|
-
const scores = [];
|
|
1862
|
-
for (const { name: key } of keys) {
|
|
1863
|
-
const score = await this.#db.getKV(TABLE_SCORERS, key);
|
|
1864
|
-
if (score && score.traceId === traceId && score.spanId === spanId) {
|
|
1865
|
-
scores.push(transformScoreRow(score));
|
|
1866
|
-
}
|
|
1867
|
-
}
|
|
1868
|
-
scores.sort((a, b) => {
|
|
1869
|
-
const dateA = new Date(a.createdAt || 0).getTime();
|
|
1870
|
-
const dateB = new Date(b.createdAt || 0).getTime();
|
|
1871
|
-
return dateB - dateA;
|
|
1872
|
-
});
|
|
1873
|
-
const { page, perPage: perPageInput } = pagination;
|
|
1874
|
-
const perPage = normalizePerPage(perPageInput, 100);
|
|
1875
|
-
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1876
|
-
const total = scores.length;
|
|
1877
|
-
const end = perPageInput === false ? scores.length : start + perPage;
|
|
1878
|
-
const pagedScores = scores.slice(start, end);
|
|
1879
|
-
return {
|
|
1880
|
-
pagination: {
|
|
1881
|
-
total,
|
|
1882
|
-
page,
|
|
1883
|
-
perPage: perPageForResponse,
|
|
1884
|
-
hasMore: end < total
|
|
1885
|
-
},
|
|
1886
|
-
scores: pagedScores
|
|
1887
|
-
};
|
|
1888
|
-
} catch (error) {
|
|
1889
|
-
const mastraError = new MastraError(
|
|
1890
|
-
{
|
|
1891
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_SCORES_BY_SPAN", "FAILED"),
|
|
1892
|
-
domain: ErrorDomain.STORAGE,
|
|
1893
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1894
|
-
text: `Failed to get scores by span: traceId=${traceId}, spanId=${spanId}`
|
|
1895
|
-
},
|
|
1896
|
-
error
|
|
1897
|
-
);
|
|
1898
|
-
this.logger?.trackException(mastraError);
|
|
1899
|
-
this.logger?.error(mastraError.toString());
|
|
1900
|
-
return { pagination: { total: 0, page: 0, perPage: 100, hasMore: false }, scores: [] };
|
|
1901
|
-
}
|
|
1902
|
-
}
|
|
1903
|
-
};
|
|
1904
|
-
var WorkflowsStorageCloudflare = class extends WorkflowsStorage {
|
|
1905
|
-
#db;
|
|
1906
|
-
constructor(config) {
|
|
1907
|
-
super();
|
|
1908
|
-
this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
|
|
1909
|
-
}
|
|
1910
|
-
supportsConcurrentUpdates() {
|
|
1911
|
-
return false;
|
|
1912
|
-
}
|
|
1913
|
-
async init() {
|
|
1914
|
-
}
|
|
1915
|
-
async dangerouslyClearAll() {
|
|
1916
|
-
await this.#db.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT });
|
|
1917
|
-
}
|
|
1918
|
-
validateWorkflowParams(params) {
|
|
1919
|
-
const { workflowName, runId } = params;
|
|
1920
|
-
if (!workflowName || !runId) {
|
|
1921
|
-
throw new Error("Invalid workflow snapshot parameters");
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
async updateWorkflowResults(_args) {
|
|
1925
|
-
throw new Error(
|
|
1926
|
-
"updateWorkflowResults is not implemented for Cloudflare KV storage. Cloudflare KV is eventually-consistent and does not support atomic read-modify-write operations needed for concurrent workflow updates."
|
|
1927
|
-
);
|
|
1928
|
-
}
|
|
1929
|
-
async updateWorkflowState(_args) {
|
|
1930
|
-
throw new Error(
|
|
1931
|
-
"updateWorkflowState is not implemented for Cloudflare KV storage. Cloudflare KV is eventually-consistent and does not support atomic read-modify-write operations needed for concurrent workflow updates."
|
|
1932
|
-
);
|
|
1933
|
-
}
|
|
1934
|
-
async persistWorkflowSnapshot(params) {
|
|
1935
|
-
try {
|
|
1936
|
-
const { workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;
|
|
1937
|
-
const now = /* @__PURE__ */ new Date();
|
|
1938
|
-
const existingKey = this.#db.getKey(TABLE_WORKFLOW_SNAPSHOT, { workflow_name: workflowName, run_id: runId });
|
|
1939
|
-
const existing = await this.#db.getKV(TABLE_WORKFLOW_SNAPSHOT, existingKey);
|
|
1940
|
-
await this.#db.putKV({
|
|
1941
|
-
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
1942
|
-
key: existingKey,
|
|
1943
|
-
value: {
|
|
1944
|
-
workflow_name: workflowName,
|
|
1945
|
-
run_id: runId,
|
|
1946
|
-
resourceId,
|
|
1947
|
-
snapshot: JSON.stringify(snapshot),
|
|
1948
|
-
createdAt: existing?.createdAt ?? createdAt ?? now,
|
|
1949
|
-
updatedAt: updatedAt ?? now
|
|
1950
|
-
}
|
|
1951
|
-
});
|
|
1952
|
-
} catch (error) {
|
|
1953
|
-
throw new MastraError(
|
|
1954
|
-
{
|
|
1955
|
-
id: createStorageErrorId("CLOUDFLARE", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
|
|
1956
|
-
domain: ErrorDomain.STORAGE,
|
|
1957
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1958
|
-
text: `Error persisting workflow snapshot for workflow ${params.workflowName}, run ${params.runId}`,
|
|
1959
|
-
details: {
|
|
1960
|
-
workflowName: params.workflowName,
|
|
1961
|
-
runId: params.runId
|
|
1962
|
-
}
|
|
1963
|
-
},
|
|
1964
|
-
error
|
|
1965
|
-
);
|
|
1966
|
-
}
|
|
1967
|
-
}
|
|
1968
|
-
async loadWorkflowSnapshot(params) {
|
|
1969
|
-
try {
|
|
1970
|
-
this.validateWorkflowParams(params);
|
|
1971
|
-
const { workflowName, runId } = params;
|
|
1972
|
-
const key = this.#db.getKey(TABLE_WORKFLOW_SNAPSHOT, { workflow_name: workflowName, run_id: runId });
|
|
1973
|
-
const data = await this.#db.getKV(TABLE_WORKFLOW_SNAPSHOT, key);
|
|
1974
|
-
if (!data) return null;
|
|
1975
|
-
const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
|
|
1976
|
-
return snapshotData;
|
|
1977
|
-
} catch (error) {
|
|
1978
|
-
const mastraError = new MastraError(
|
|
1979
|
-
{
|
|
1980
|
-
id: createStorageErrorId("CLOUDFLARE", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
|
|
1981
|
-
domain: ErrorDomain.STORAGE,
|
|
1982
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1983
|
-
text: `Error loading workflow snapshot for workflow ${params.workflowName}, run ${params.runId}`,
|
|
1984
|
-
details: {
|
|
1985
|
-
workflowName: params.workflowName,
|
|
1986
|
-
runId: params.runId
|
|
1987
|
-
}
|
|
1988
|
-
},
|
|
1989
|
-
error
|
|
1990
|
-
);
|
|
1991
|
-
this.logger.trackException?.(mastraError);
|
|
1992
|
-
this.logger.error(mastraError.toString());
|
|
1993
|
-
return null;
|
|
1994
|
-
}
|
|
1995
|
-
}
|
|
1996
|
-
parseWorkflowRun(row) {
|
|
1997
|
-
let parsedSnapshot = row.snapshot;
|
|
1998
|
-
if (typeof parsedSnapshot === "string") {
|
|
1999
|
-
try {
|
|
2000
|
-
parsedSnapshot = JSON.parse(row.snapshot);
|
|
2001
|
-
} catch (e) {
|
|
2002
|
-
this.logger.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
2003
|
-
}
|
|
2004
|
-
}
|
|
2005
|
-
return {
|
|
2006
|
-
workflowName: row.workflow_name,
|
|
2007
|
-
runId: row.run_id,
|
|
2008
|
-
snapshot: parsedSnapshot,
|
|
2009
|
-
createdAt: ensureDate(row.createdAt),
|
|
2010
|
-
updatedAt: ensureDate(row.updatedAt),
|
|
2011
|
-
resourceId: row.resourceId
|
|
2012
|
-
};
|
|
2013
|
-
}
|
|
2014
|
-
buildWorkflowSnapshotPrefix({
|
|
2015
|
-
workflowName,
|
|
2016
|
-
runId,
|
|
2017
|
-
resourceId
|
|
2018
|
-
}) {
|
|
2019
|
-
const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
|
|
2020
|
-
let key = `${prefix}${TABLE_WORKFLOW_SNAPSHOT}`;
|
|
2021
|
-
if (workflowName) key += `:${workflowName}`;
|
|
2022
|
-
if (runId) key += `:${runId}`;
|
|
2023
|
-
if (resourceId) key += `:${resourceId}`;
|
|
2024
|
-
return key;
|
|
2025
|
-
}
|
|
2026
|
-
async listWorkflowRuns({
|
|
2027
|
-
workflowName,
|
|
2028
|
-
page = 0,
|
|
2029
|
-
perPage = 20,
|
|
2030
|
-
resourceId,
|
|
2031
|
-
fromDate,
|
|
2032
|
-
toDate,
|
|
2033
|
-
status
|
|
2034
|
-
} = {}) {
|
|
2035
|
-
try {
|
|
2036
|
-
if (page < 0 || !Number.isInteger(page)) {
|
|
2037
|
-
throw new MastraError(
|
|
2038
|
-
{
|
|
2039
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_WORKFLOW_RUNS", "INVALID_PAGE"),
|
|
2040
|
-
domain: ErrorDomain.STORAGE,
|
|
2041
|
-
category: ErrorCategory.USER,
|
|
2042
|
-
details: { page }
|
|
2043
|
-
},
|
|
2044
|
-
new Error("page must be a non-negative integer")
|
|
2045
|
-
);
|
|
2046
|
-
}
|
|
2047
|
-
const normalizedPerPage = normalizePerPage(perPage, 20);
|
|
2048
|
-
const offset = page * normalizedPerPage;
|
|
2049
|
-
const prefix = this.buildWorkflowSnapshotPrefix({ workflowName });
|
|
2050
|
-
const keyObjs = await this.#db.listKV(TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
2051
|
-
const runs = [];
|
|
2052
|
-
for (const { name: key } of keyObjs) {
|
|
2053
|
-
const parts = key.split(":");
|
|
2054
|
-
const idx = parts.indexOf(TABLE_WORKFLOW_SNAPSHOT);
|
|
2055
|
-
if (idx === -1 || parts.length < idx + 3) continue;
|
|
2056
|
-
const wfName = parts[idx + 1];
|
|
2057
|
-
const keyResourceId = parts.length > idx + 3 ? parts[idx + 3] : void 0;
|
|
2058
|
-
if (workflowName && wfName !== workflowName) continue;
|
|
2059
|
-
const data = await this.#db.getKV(TABLE_WORKFLOW_SNAPSHOT, key);
|
|
2060
|
-
if (!data) continue;
|
|
2061
|
-
try {
|
|
2062
|
-
const effectiveResourceId = keyResourceId || data.resourceId;
|
|
2063
|
-
if (resourceId && effectiveResourceId !== resourceId) continue;
|
|
2064
|
-
const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
|
|
2065
|
-
if (status && snapshotData.status !== status) continue;
|
|
2066
|
-
const createdAt = ensureDate(data.createdAt);
|
|
2067
|
-
if (fromDate && createdAt && createdAt < fromDate) continue;
|
|
2068
|
-
if (toDate && createdAt && createdAt > toDate) continue;
|
|
2069
|
-
const run = this.parseWorkflowRun({
|
|
2070
|
-
...data,
|
|
2071
|
-
workflow_name: wfName,
|
|
2072
|
-
resourceId: effectiveResourceId,
|
|
2073
|
-
snapshot: snapshotData
|
|
2074
|
-
});
|
|
2075
|
-
runs.push(run);
|
|
2076
|
-
} catch (err) {
|
|
2077
|
-
this.logger.error("Failed to parse workflow snapshot:", { key, error: err });
|
|
2078
|
-
}
|
|
2079
|
-
}
|
|
2080
|
-
runs.sort((a, b) => {
|
|
2081
|
-
const aDate = a.createdAt ? new Date(a.createdAt).getTime() : 0;
|
|
2082
|
-
const bDate = b.createdAt ? new Date(b.createdAt).getTime() : 0;
|
|
2083
|
-
return bDate - aDate;
|
|
2084
|
-
});
|
|
2085
|
-
const pagedRuns = runs.slice(offset, offset + normalizedPerPage);
|
|
2086
|
-
return {
|
|
2087
|
-
runs: pagedRuns,
|
|
2088
|
-
total: runs.length
|
|
2089
|
-
};
|
|
2090
|
-
} catch (error) {
|
|
2091
|
-
const mastraError = new MastraError(
|
|
2092
|
-
{
|
|
2093
|
-
id: createStorageErrorId("CLOUDFLARE", "LIST_WORKFLOW_RUNS", "FAILED"),
|
|
2094
|
-
domain: ErrorDomain.STORAGE,
|
|
2095
|
-
category: ErrorCategory.THIRD_PARTY
|
|
2096
|
-
},
|
|
2097
|
-
error
|
|
2098
|
-
);
|
|
2099
|
-
this.logger.trackException?.(mastraError);
|
|
2100
|
-
this.logger.error(mastraError.toString());
|
|
2101
|
-
return { runs: [], total: 0 };
|
|
2102
|
-
}
|
|
2103
|
-
}
|
|
2104
|
-
async getWorkflowRunById({
|
|
2105
|
-
runId,
|
|
2106
|
-
workflowName
|
|
2107
|
-
}) {
|
|
2108
|
-
try {
|
|
2109
|
-
if (!runId || !workflowName) {
|
|
2110
|
-
throw new Error("runId, workflowName, are required");
|
|
2111
|
-
}
|
|
2112
|
-
const prefix = this.buildWorkflowSnapshotPrefix({ workflowName, runId });
|
|
2113
|
-
const keyObjs = await this.#db.listKV(TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
2114
|
-
if (!keyObjs.length) return null;
|
|
2115
|
-
const exactKey = keyObjs.find((k) => {
|
|
2116
|
-
const parts = k.name.split(":");
|
|
2117
|
-
const idx = parts.indexOf(TABLE_WORKFLOW_SNAPSHOT);
|
|
2118
|
-
if (idx === -1 || parts.length < idx + 3) return false;
|
|
2119
|
-
const wfName = parts[idx + 1];
|
|
2120
|
-
const rId = parts[idx + 2];
|
|
2121
|
-
return wfName === workflowName && rId === runId;
|
|
2122
|
-
});
|
|
2123
|
-
if (!exactKey) return null;
|
|
2124
|
-
const data = await this.#db.getKV(TABLE_WORKFLOW_SNAPSHOT, exactKey.name);
|
|
2125
|
-
if (!data) return null;
|
|
2126
|
-
const snapshotData = typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
|
|
2127
|
-
return this.parseWorkflowRun({ ...data, snapshot: snapshotData });
|
|
2128
|
-
} catch (error) {
|
|
2129
|
-
const mastraError = new MastraError(
|
|
2130
|
-
{
|
|
2131
|
-
id: createStorageErrorId("CLOUDFLARE", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
|
|
2132
|
-
domain: ErrorDomain.STORAGE,
|
|
2133
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
2134
|
-
details: {
|
|
2135
|
-
workflowName,
|
|
2136
|
-
runId
|
|
2137
|
-
}
|
|
2138
|
-
},
|
|
2139
|
-
error
|
|
2140
|
-
);
|
|
2141
|
-
this.logger.trackException?.(mastraError);
|
|
2142
|
-
this.logger.error(mastraError.toString());
|
|
2143
|
-
return null;
|
|
2144
|
-
}
|
|
2145
|
-
}
|
|
2146
|
-
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
2147
|
-
try {
|
|
2148
|
-
if (!runId || !workflowName) {
|
|
2149
|
-
throw new Error("runId and workflowName are required");
|
|
2150
|
-
}
|
|
2151
|
-
const key = this.#db.getKey(TABLE_WORKFLOW_SNAPSHOT, { workflow_name: workflowName, run_id: runId });
|
|
2152
|
-
await this.#db.deleteKV(TABLE_WORKFLOW_SNAPSHOT, key);
|
|
2153
|
-
} catch (error) {
|
|
2154
|
-
throw new MastraError(
|
|
2155
|
-
{
|
|
2156
|
-
id: createStorageErrorId("CLOUDFLARE", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
|
|
2157
|
-
domain: ErrorDomain.STORAGE,
|
|
2158
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
2159
|
-
details: {
|
|
2160
|
-
workflowName,
|
|
2161
|
-
runId
|
|
2162
|
-
}
|
|
2163
|
-
},
|
|
2164
|
-
error
|
|
2165
|
-
);
|
|
2166
|
-
}
|
|
2167
|
-
}
|
|
2168
|
-
};
|
|
2169
|
-
|
|
2170
|
-
// src/storage/types.ts
|
|
2171
|
-
function isWorkersConfig(config) {
|
|
2172
|
-
return "bindings" in config;
|
|
2173
|
-
}
|
|
2174
|
-
|
|
2175
|
-
// src/storage/index.ts
|
|
2176
|
-
var CloudflareStore = class extends MastraCompositeStore {
|
|
2177
|
-
stores;
|
|
2178
|
-
client;
|
|
2179
|
-
accountId;
|
|
2180
|
-
namespacePrefix;
|
|
2181
|
-
bindings;
|
|
2182
|
-
validateWorkersConfig(config) {
|
|
2183
|
-
if (!isWorkersConfig(config)) {
|
|
2184
|
-
throw new Error("Invalid Workers API configuration");
|
|
2185
|
-
}
|
|
2186
|
-
if (!config.bindings) {
|
|
2187
|
-
throw new Error("KV bindings are required when using Workers Binding API");
|
|
2188
|
-
}
|
|
2189
|
-
const requiredTables = [TABLE_THREADS, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT, TABLE_SCORERS];
|
|
2190
|
-
for (const table of requiredTables) {
|
|
2191
|
-
if (!(table in config.bindings)) {
|
|
2192
|
-
throw new Error(`Missing KV binding for table: ${table}`);
|
|
2193
|
-
}
|
|
2194
|
-
}
|
|
2195
|
-
}
|
|
2196
|
-
validateRestConfig(config) {
|
|
2197
|
-
if (isWorkersConfig(config)) {
|
|
2198
|
-
throw new Error("Invalid REST API configuration");
|
|
2199
|
-
}
|
|
2200
|
-
if (!config.accountId?.trim()) {
|
|
2201
|
-
throw new Error("accountId is required for REST API");
|
|
2202
|
-
}
|
|
2203
|
-
if (!config.apiToken?.trim()) {
|
|
2204
|
-
throw new Error("apiToken is required for REST API");
|
|
2205
|
-
}
|
|
2206
|
-
}
|
|
2207
|
-
constructor(config) {
|
|
2208
|
-
super({ id: config.id, name: "Cloudflare", disableInit: config.disableInit });
|
|
2209
|
-
try {
|
|
2210
|
-
let workflows;
|
|
2211
|
-
let memory;
|
|
2212
|
-
let scores;
|
|
2213
|
-
if (isWorkersConfig(config)) {
|
|
2214
|
-
this.validateWorkersConfig(config);
|
|
2215
|
-
this.bindings = config.bindings;
|
|
2216
|
-
this.namespacePrefix = config.keyPrefix?.trim() || "";
|
|
2217
|
-
this.logger.info("Using Cloudflare KV Workers Binding API");
|
|
2218
|
-
const domainConfig = {
|
|
2219
|
-
bindings: this.bindings,
|
|
2220
|
-
keyPrefix: this.namespacePrefix
|
|
2221
|
-
};
|
|
2222
|
-
workflows = new WorkflowsStorageCloudflare(domainConfig);
|
|
2223
|
-
memory = new MemoryStorageCloudflare(domainConfig);
|
|
2224
|
-
scores = new ScoresStorageCloudflare(domainConfig);
|
|
2225
|
-
} else {
|
|
2226
|
-
this.validateRestConfig(config);
|
|
2227
|
-
this.accountId = config.accountId.trim();
|
|
2228
|
-
this.namespacePrefix = config.namespacePrefix?.trim() || "";
|
|
2229
|
-
this.client = new Cloudflare({
|
|
2230
|
-
apiToken: config.apiToken.trim()
|
|
2231
|
-
});
|
|
2232
|
-
this.logger.info("Using Cloudflare KV REST API");
|
|
2233
|
-
const domainConfig = {
|
|
2234
|
-
client: this.client,
|
|
2235
|
-
accountId: this.accountId,
|
|
2236
|
-
namespacePrefix: this.namespacePrefix
|
|
2237
|
-
};
|
|
2238
|
-
workflows = new WorkflowsStorageCloudflare(domainConfig);
|
|
2239
|
-
memory = new MemoryStorageCloudflare(domainConfig);
|
|
2240
|
-
scores = new ScoresStorageCloudflare(domainConfig);
|
|
2241
|
-
}
|
|
2242
|
-
this.stores = {
|
|
2243
|
-
workflows,
|
|
2244
|
-
memory,
|
|
2245
|
-
scores
|
|
2246
|
-
};
|
|
2247
|
-
} catch (error) {
|
|
2248
|
-
throw new MastraError(
|
|
2249
|
-
{
|
|
2250
|
-
id: createStorageErrorId("CLOUDFLARE", "INIT", "FAILED"),
|
|
2251
|
-
domain: ErrorDomain.STORAGE,
|
|
2252
|
-
category: ErrorCategory.THIRD_PARTY
|
|
2253
|
-
},
|
|
2254
|
-
error
|
|
2255
|
-
);
|
|
2256
|
-
}
|
|
2257
|
-
}
|
|
2258
|
-
async close() {
|
|
2259
|
-
}
|
|
2260
|
-
};
|
|
2261
|
-
|
|
2262
|
-
export { CloudflareStore, MemoryStorageCloudflare, ScoresStorageCloudflare, WorkflowsStorageCloudflare };
|
|
1
|
+
export { CloudflareDOStorage, DODB, DOStore, MemoryStorageDO, ScoresStorageDO, WorkflowsStorageDO } from './chunk-NSFHQATX.js';
|
|
2
|
+
export { CloudflareKVStorage, CloudflareStore, MemoryStorageCloudflare, ScoresStorageCloudflare, WorkflowsStorageCloudflare } from './chunk-O57GFJSB.js';
|
|
2263
3
|
//# sourceMappingURL=index.js.map
|
|
2264
4
|
//# sourceMappingURL=index.js.map
|