@mastra/cloudflare 1.5.1 → 1.6.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 +52 -6
- package/dist/do/index.cjs +9 -36
- package/dist/do/index.js +2 -3
- package/dist/do/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/do/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/do-BDZtdsky.js +2272 -0
- package/dist/do-BDZtdsky.js.map +1 -0
- package/dist/do-BkCQKqjz.cjs +2313 -0
- package/dist/do-BkCQKqjz.cjs.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +2 -56
- package/dist/index.cjs +14 -53
- package/dist/index.js +3 -4
- package/dist/kv/index.cjs +8 -32
- package/dist/kv/index.js +2 -3
- package/dist/kv/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/kv-CzXjeoeQ.js +2137 -0
- package/dist/kv-CzXjeoeQ.js.map +1 -0
- package/dist/kv-DAbAqe-v.cjs +2195 -0
- package/dist/kv-DAbAqe-v.cjs.map +1 -0
- package/package.json +18 -17
- package/dist/chunk-CD6XB3H3.js +0 -2421
- package/dist/chunk-CD6XB3H3.js.map +0 -1
- package/dist/chunk-D7CPZ5NS.cjs +0 -2432
- package/dist/chunk-D7CPZ5NS.cjs.map +0 -1
- package/dist/chunk-OMTTR37Z.js +0 -2442
- package/dist/chunk-OMTTR37Z.js.map +0 -1
- package/dist/chunk-UQ4IOLSC.cjs +0 -2450
- package/dist/chunk-UQ4IOLSC.cjs.map +0 -1
- package/dist/do/index.cjs.map +0 -1
- package/dist/do/index.js.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/kv/index.cjs.map +0 -1
- package/dist/kv/index.js.map +0 -1
|
@@ -0,0 +1,2272 @@
|
|
|
1
|
+
import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
|
|
2
|
+
import { BackgroundTasksStorage, MastraCompositeStore, MemoryStorage, ScoresStorage, TABLE_BACKGROUND_TASKS, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, WorkflowsStorage, calculatePagination, createStorageErrorId, ensureDate, getDefaultValue, getSqlType, normalizePerPage, serializeDate, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
|
|
3
|
+
import { MastraBase } from "@mastra/core/base";
|
|
4
|
+
import { MessageList } from "@mastra/core/agent";
|
|
5
|
+
import { saveScorePayloadSchema } from "@mastra/core/evals";
|
|
6
|
+
import { parseSqlIdentifier } from "@mastra/core/utils";
|
|
7
|
+
//#region src/do/storage/domains/utils.ts
|
|
8
|
+
function isArrayOfRecords(value) {
|
|
9
|
+
return value !== null && value !== void 0 && Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
function deserializeValue(value, type) {
|
|
12
|
+
if (value === null || value === void 0) return null;
|
|
13
|
+
if (type === "date" && typeof value === "string") return new Date(value);
|
|
14
|
+
if (type === "jsonb" && typeof value === "string") try {
|
|
15
|
+
return JSON.parse(value);
|
|
16
|
+
} catch {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) try {
|
|
20
|
+
return JSON.parse(value);
|
|
21
|
+
} catch {
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/do/storage/sql-builder.ts
|
|
28
|
+
/**
|
|
29
|
+
* SQL Builder class for constructing type-safe SQL queries
|
|
30
|
+
* This helps create maintainable and secure SQL queries with proper parameter handling
|
|
31
|
+
*/
|
|
32
|
+
var SqlBuilder = class {
|
|
33
|
+
sql = "";
|
|
34
|
+
params = [];
|
|
35
|
+
whereAdded = false;
|
|
36
|
+
select(columns) {
|
|
37
|
+
if (!columns || Array.isArray(columns) && columns.length === 0) this.sql = "SELECT *";
|
|
38
|
+
else {
|
|
39
|
+
const parsedCols = (Array.isArray(columns) ? columns : [columns]).map((col) => parseSelectIdentifier(col));
|
|
40
|
+
this.sql = `SELECT ${parsedCols.join(", ")}`;
|
|
41
|
+
}
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
from(table) {
|
|
45
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
46
|
+
this.sql += ` FROM ${parsedTableName}`;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Add a WHERE clause to the query
|
|
51
|
+
* @param condition The condition to add
|
|
52
|
+
* @param params Parameters to bind to the condition
|
|
53
|
+
*/
|
|
54
|
+
where(condition, ...params) {
|
|
55
|
+
this.sql += ` WHERE ${condition}`;
|
|
56
|
+
this.params.push(...params);
|
|
57
|
+
this.whereAdded = true;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Add a WHERE clause if it hasn't been added yet, otherwise add an AND clause
|
|
62
|
+
* @param condition The condition to add
|
|
63
|
+
* @param params Parameters to bind to the condition
|
|
64
|
+
*/
|
|
65
|
+
whereAnd(condition, ...params) {
|
|
66
|
+
if (this.whereAdded) return this.andWhere(condition, ...params);
|
|
67
|
+
else return this.where(condition, ...params);
|
|
68
|
+
}
|
|
69
|
+
andWhere(condition, ...params) {
|
|
70
|
+
this.sql += ` AND ${condition}`;
|
|
71
|
+
this.params.push(...params);
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
orWhere(condition, ...params) {
|
|
75
|
+
this.sql += ` OR ${condition}`;
|
|
76
|
+
this.params.push(...params);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
orderBy(column, direction = "ASC") {
|
|
80
|
+
const parsedColumn = parseSqlIdentifier(column, "column name");
|
|
81
|
+
if (!["ASC", "DESC"].includes(direction)) throw new Error(`Invalid sort direction: ${direction}`);
|
|
82
|
+
this.sql += ` ORDER BY ${parsedColumn} ${direction}`;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
limit(count) {
|
|
86
|
+
this.sql += ` LIMIT ?`;
|
|
87
|
+
this.params.push(count);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
offset(count) {
|
|
91
|
+
this.sql += ` OFFSET ?`;
|
|
92
|
+
this.params.push(count);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
count() {
|
|
96
|
+
this.sql += "SELECT COUNT(*) AS count";
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Insert a row, or update specific columns on conflict (upsert).
|
|
101
|
+
* @param table Table name
|
|
102
|
+
* @param columns Columns to insert
|
|
103
|
+
* @param values Values to insert
|
|
104
|
+
* @param conflictColumns Columns to check for conflict (usually PK or UNIQUE)
|
|
105
|
+
* @param updateMap Object mapping columns to update to their new value (e.g. { name: 'excluded.name' })
|
|
106
|
+
*/
|
|
107
|
+
insert(table, columns, values, conflictColumns, updateMap) {
|
|
108
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
109
|
+
const parsedColumns = columns.map((col) => parseSqlIdentifier(col, "column name"));
|
|
110
|
+
const placeholders = parsedColumns.map(() => "?").join(", ");
|
|
111
|
+
if (conflictColumns && updateMap) {
|
|
112
|
+
const parsedConflictColumns = conflictColumns.map((col) => parseSqlIdentifier(col, "column name"));
|
|
113
|
+
const excludedPattern = /^excluded\.[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
114
|
+
const coalescePattern = /^COALESCE\(excluded\.[a-zA-Z_][a-zA-Z0-9_]*,\s*[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]*\)$/;
|
|
115
|
+
const updateClause = Object.entries(updateMap).map(([col, expr]) => {
|
|
116
|
+
const parsedCol = parseSqlIdentifier(col, "update column name");
|
|
117
|
+
if (!excludedPattern.test(expr) && !coalescePattern.test(expr)) throw new Error(`Invalid update expression for column ${col}: must be 'excluded.<column>' or 'COALESCE(excluded.<column>, <table>.<column>)' pattern`);
|
|
118
|
+
return `${parsedCol} = ${expr}`;
|
|
119
|
+
}).join(", ");
|
|
120
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${parsedConflictColumns.join(", ")}) DO UPDATE SET ${updateClause}`;
|
|
121
|
+
this.params.push(...values);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders})`;
|
|
125
|
+
this.params.push(...values);
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
update(table, columns, values) {
|
|
129
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
130
|
+
const setClause = columns.map((col) => parseSqlIdentifier(col, "column name")).map((col) => `${col} = ?`).join(", ");
|
|
131
|
+
this.sql = `UPDATE ${parsedTableName} SET ${setClause}`;
|
|
132
|
+
this.params.push(...values);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
delete(table) {
|
|
136
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
137
|
+
this.sql = `DELETE FROM ${parsedTableName}`;
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Create a table if it doesn't exist
|
|
142
|
+
* @param table The table name
|
|
143
|
+
* @param columnDefinitions The column definitions as an array of strings
|
|
144
|
+
* @param tableConstraints Optional constraints for the table
|
|
145
|
+
* @returns The builder instance
|
|
146
|
+
*/
|
|
147
|
+
createTable(table, columnDefinitions, tableConstraints) {
|
|
148
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
149
|
+
const columns = columnDefinitions.map((def) => {
|
|
150
|
+
const colName = def.split(/\s+/)[0];
|
|
151
|
+
if (!colName) throw new Error("Empty column name in definition");
|
|
152
|
+
parseSqlIdentifier(colName, "column name");
|
|
153
|
+
return def;
|
|
154
|
+
}).join(", ");
|
|
155
|
+
let constraints = "";
|
|
156
|
+
if (tableConstraints && tableConstraints.length > 0) {
|
|
157
|
+
const constraintPattern = /^(PRIMARY\s+KEY|UNIQUE|FOREIGN\s+KEY|CHECK)\s*\([a-zA-Z0-9_,\s]+\)$/i;
|
|
158
|
+
for (const constraint of tableConstraints) if (!constraintPattern.test(constraint.trim())) throw new Error(`Invalid table constraint: ${constraint}`);
|
|
159
|
+
constraints = ", " + tableConstraints.join(", ");
|
|
160
|
+
}
|
|
161
|
+
this.sql = `CREATE TABLE IF NOT EXISTS ${parsedTableName} (${columns}${constraints})`;
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Check if an index exists in the database
|
|
166
|
+
* @param indexName The name of the index to check
|
|
167
|
+
* @param tableName The table the index is on
|
|
168
|
+
* @returns The builder instance
|
|
169
|
+
*/
|
|
170
|
+
checkIndexExists(indexName, tableName) {
|
|
171
|
+
this.sql = `SELECT name FROM sqlite_master WHERE type='index' AND name=? AND tbl_name=?`;
|
|
172
|
+
this.params.push(indexName, tableName);
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Create an index if it doesn't exist
|
|
177
|
+
* @param indexName The name of the index to create
|
|
178
|
+
* @param tableName The table to create the index on
|
|
179
|
+
* @param columnName The column to index
|
|
180
|
+
* @param indexType Optional index type (e.g., 'UNIQUE')
|
|
181
|
+
* @returns The builder instance
|
|
182
|
+
*/
|
|
183
|
+
createIndex(indexName, tableName, columnName, indexType = "") {
|
|
184
|
+
const parsedIndexName = parseSqlIdentifier(indexName, "index name");
|
|
185
|
+
const parsedTableName = parseSqlIdentifier(tableName, "table name");
|
|
186
|
+
const parsedColumnName = parseSqlIdentifier(columnName, "column name");
|
|
187
|
+
const allowedIndexTypes = ["", "UNIQUE"];
|
|
188
|
+
const normalizedIndexType = indexType.toUpperCase().trim();
|
|
189
|
+
if (!allowedIndexTypes.includes(normalizedIndexType)) throw new Error(`Invalid index type: ${indexType}. Allowed values: ${allowedIndexTypes.filter((t) => t).join(", ")}`);
|
|
190
|
+
this.sql = `CREATE ${normalizedIndexType ? normalizedIndexType + " " : ""}INDEX IF NOT EXISTS ${parsedIndexName} ON ${parsedTableName}(${parsedColumnName})`;
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Add a LIKE condition to the query
|
|
195
|
+
* @param column The column to check
|
|
196
|
+
* @param value The value to match (will be wrapped with % for LIKE)
|
|
197
|
+
* @param exact If true, will not add % wildcards
|
|
198
|
+
*/
|
|
199
|
+
like(column, value, exact = false) {
|
|
200
|
+
const parsedColumnName = parseSqlIdentifier(column, "column name");
|
|
201
|
+
const likeValue = exact ? value : `%${value}%`;
|
|
202
|
+
if (this.whereAdded) this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
203
|
+
else {
|
|
204
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
205
|
+
this.whereAdded = true;
|
|
206
|
+
}
|
|
207
|
+
this.params.push(likeValue);
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Add a JSON LIKE condition for searching in JSON fields
|
|
212
|
+
* @param column The JSON column to search in
|
|
213
|
+
* @param key The JSON key to match
|
|
214
|
+
* @param value The value to match
|
|
215
|
+
*/
|
|
216
|
+
jsonLike(column, key, value) {
|
|
217
|
+
const parsedColumnName = parseSqlIdentifier(column, "column name");
|
|
218
|
+
const jsonPattern = `%"${parseSqlIdentifier(key, "key name")}":"${value}"%`;
|
|
219
|
+
if (this.whereAdded) this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
220
|
+
else {
|
|
221
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
222
|
+
this.whereAdded = true;
|
|
223
|
+
}
|
|
224
|
+
this.params.push(jsonPattern);
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Get the built query
|
|
229
|
+
* @returns Object containing the SQL string and parameters array
|
|
230
|
+
*/
|
|
231
|
+
build() {
|
|
232
|
+
return {
|
|
233
|
+
sql: this.sql,
|
|
234
|
+
params: this.params
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Reset the builder for reuse
|
|
239
|
+
* @returns The reset builder instance
|
|
240
|
+
*/
|
|
241
|
+
reset() {
|
|
242
|
+
this.sql = "";
|
|
243
|
+
this.params = [];
|
|
244
|
+
this.whereAdded = false;
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
function createSqlBuilder() {
|
|
249
|
+
return new SqlBuilder();
|
|
250
|
+
}
|
|
251
|
+
const SQL_IDENTIFIER_PATTERN = /^[a-zA-Z0-9_]+(\s+AS\s+[a-zA-Z0-9_]+)?$/;
|
|
252
|
+
/**
|
|
253
|
+
* Parses and returns a valid SQL SELECT column identifier.
|
|
254
|
+
* Allows a single identifier (letters, numbers, underscores), or '*', optionally with 'AS alias'.
|
|
255
|
+
*
|
|
256
|
+
* @param column - The column identifier string to parse.
|
|
257
|
+
* @returns The validated column identifier as a branded type.
|
|
258
|
+
* @throws {Error} If invalid.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* const col = parseSelectIdentifier('user_id'); // Ok
|
|
262
|
+
* parseSelectIdentifier('user_id AS uid'); // Ok
|
|
263
|
+
* parseSelectIdentifier('*'); // Ok
|
|
264
|
+
* parseSelectIdentifier('user id'); // Throws error
|
|
265
|
+
*/
|
|
266
|
+
function parseSelectIdentifier(column) {
|
|
267
|
+
if (column !== "*" && !SQL_IDENTIFIER_PATTERN.test(column)) throw new Error(`Invalid column name: "${column}". Must be "*" or a valid identifier (letters, numbers, underscores), optionally with "AS alias".`);
|
|
268
|
+
return column;
|
|
269
|
+
}
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/do/storage/db/index.ts
|
|
272
|
+
var DODB = class extends MastraBase {
|
|
273
|
+
sql;
|
|
274
|
+
tablePrefix;
|
|
275
|
+
constructor(config) {
|
|
276
|
+
super({
|
|
277
|
+
component: "STORAGE",
|
|
278
|
+
name: "DO_DB"
|
|
279
|
+
});
|
|
280
|
+
this.sql = config.sql;
|
|
281
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
282
|
+
}
|
|
283
|
+
async hasColumn(table, column) {
|
|
284
|
+
const identifierPattern = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
285
|
+
if (!identifierPattern.test(table)) throw new Error(`Invalid table name: ${table}`);
|
|
286
|
+
if (!identifierPattern.test(column)) throw new Error(`Invalid column name: ${column}`);
|
|
287
|
+
if (this.tablePrefix && !identifierPattern.test(this.tablePrefix)) throw new Error(`Invalid table prefix: ${this.tablePrefix}`);
|
|
288
|
+
const sql = `PRAGMA table_info(${table.startsWith(this.tablePrefix) ? table : `${this.tablePrefix}${table}`});`;
|
|
289
|
+
const result = await this.executeQuery({
|
|
290
|
+
sql,
|
|
291
|
+
params: []
|
|
292
|
+
});
|
|
293
|
+
if (!result || !Array.isArray(result)) return false;
|
|
294
|
+
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
295
|
+
}
|
|
296
|
+
getTableName(tableName) {
|
|
297
|
+
return `${this.tablePrefix}${tableName}`;
|
|
298
|
+
}
|
|
299
|
+
formatSqlParams(params) {
|
|
300
|
+
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Execute a SQL query using Durable Objects SqlStorage.
|
|
304
|
+
* SqlStorage.exec() is synchronous but we wrap in Promise for interface compatibility.
|
|
305
|
+
*/
|
|
306
|
+
async executeQuery(options) {
|
|
307
|
+
const { sql, params = [], first = false } = options;
|
|
308
|
+
try {
|
|
309
|
+
const formattedParams = this.formatSqlParams(params);
|
|
310
|
+
const cursor = this.sql.exec(sql, ...formattedParams);
|
|
311
|
+
if (first) return cursor.toArray()[0] || null;
|
|
312
|
+
return cursor.toArray();
|
|
313
|
+
} catch (error) {
|
|
314
|
+
throw new MastraError({
|
|
315
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "QUERY", "FAILED"),
|
|
316
|
+
domain: ErrorDomain.STORAGE,
|
|
317
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
318
|
+
details: { sql }
|
|
319
|
+
}, error);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
async getTableColumns(tableName) {
|
|
323
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(tableName)) {
|
|
324
|
+
this.logger.warn(`Invalid table name in getTableColumns: ${tableName}`);
|
|
325
|
+
return [];
|
|
326
|
+
}
|
|
327
|
+
try {
|
|
328
|
+
const sql = `PRAGMA table_info(${tableName})`;
|
|
329
|
+
const result = await this.executeQuery({ sql });
|
|
330
|
+
if (!result || !Array.isArray(result)) return [];
|
|
331
|
+
return result.map((row) => ({
|
|
332
|
+
name: row.name,
|
|
333
|
+
type: row.type
|
|
334
|
+
}));
|
|
335
|
+
} catch (error) {
|
|
336
|
+
this.logger.warn(`Failed to get table columns for ${tableName}:`, error);
|
|
337
|
+
return [];
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
serializeValue(value) {
|
|
341
|
+
if (value === null || value === void 0) return null;
|
|
342
|
+
if (value instanceof Date) return value.toISOString();
|
|
343
|
+
if (typeof value === "object") return JSON.stringify(value);
|
|
344
|
+
return value;
|
|
345
|
+
}
|
|
346
|
+
getSqlType(type) {
|
|
347
|
+
switch (type) {
|
|
348
|
+
case "bigint": return "INTEGER";
|
|
349
|
+
case "jsonb": return "TEXT";
|
|
350
|
+
case "boolean": return "INTEGER";
|
|
351
|
+
default: return getSqlType(type);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
getDefaultValue(type) {
|
|
355
|
+
return getDefaultValue(type);
|
|
356
|
+
}
|
|
357
|
+
async createTable({ tableName, schema }) {
|
|
358
|
+
try {
|
|
359
|
+
const fullTableName = this.getTableName(tableName);
|
|
360
|
+
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
361
|
+
return `${colName} ${this.getSqlType(colDef.type)} ${colDef.nullable === false ? "NOT NULL" : ""} ${colDef.primaryKey ? "PRIMARY KEY" : ""}`.trim();
|
|
362
|
+
});
|
|
363
|
+
const tableConstraints = [];
|
|
364
|
+
if (tableName === TABLE_WORKFLOW_SNAPSHOT) tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
365
|
+
const { sql, params } = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints).build();
|
|
366
|
+
await this.executeQuery({
|
|
367
|
+
sql,
|
|
368
|
+
params
|
|
369
|
+
});
|
|
370
|
+
this.logger.debug(`Created table ${fullTableName}`);
|
|
371
|
+
} catch (error) {
|
|
372
|
+
throw new MastraError({
|
|
373
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "CREATE_TABLE", "FAILED"),
|
|
374
|
+
domain: ErrorDomain.STORAGE,
|
|
375
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
376
|
+
details: { tableName }
|
|
377
|
+
}, error);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async clearTable({ tableName }) {
|
|
381
|
+
try {
|
|
382
|
+
const fullTableName = this.getTableName(tableName);
|
|
383
|
+
const { sql, params } = createSqlBuilder().delete(fullTableName).build();
|
|
384
|
+
await this.executeQuery({
|
|
385
|
+
sql,
|
|
386
|
+
params
|
|
387
|
+
});
|
|
388
|
+
this.logger.debug(`Cleared table ${fullTableName}`);
|
|
389
|
+
} catch (error) {
|
|
390
|
+
throw new MastraError({
|
|
391
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "CLEAR_TABLE", "FAILED"),
|
|
392
|
+
domain: ErrorDomain.STORAGE,
|
|
393
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
394
|
+
details: { tableName }
|
|
395
|
+
}, error);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
async dropTable({ tableName }) {
|
|
399
|
+
try {
|
|
400
|
+
const fullTableName = this.getTableName(tableName);
|
|
401
|
+
const sql = `DROP TABLE IF EXISTS ${fullTableName}`;
|
|
402
|
+
await this.executeQuery({ sql });
|
|
403
|
+
this.logger.debug(`Dropped table ${fullTableName}`);
|
|
404
|
+
} catch (error) {
|
|
405
|
+
throw new MastraError({
|
|
406
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "DROP_TABLE", "FAILED"),
|
|
407
|
+
domain: ErrorDomain.STORAGE,
|
|
408
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
409
|
+
details: { tableName }
|
|
410
|
+
}, error);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
async alterTable(args) {
|
|
414
|
+
const identifierPattern = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
415
|
+
try {
|
|
416
|
+
const fullTableName = this.getTableName(args.tableName);
|
|
417
|
+
if (!identifierPattern.test(fullTableName)) throw new Error(`Invalid table name: ${fullTableName}`);
|
|
418
|
+
const existingColumns = await this.getTableColumns(fullTableName);
|
|
419
|
+
const existingColumnNames = new Set(existingColumns.map((col) => col.name));
|
|
420
|
+
for (const [columnName, column] of Object.entries(args.schema)) {
|
|
421
|
+
if (!identifierPattern.test(columnName)) throw new Error(`Invalid column name: ${columnName}`);
|
|
422
|
+
if (!existingColumnNames.has(columnName) && args.ifNotExists.includes(columnName)) {
|
|
423
|
+
const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${this.getSqlType(column.type)} ${this.getDefaultValue(column.type)}`;
|
|
424
|
+
await this.executeQuery({ sql });
|
|
425
|
+
this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
} catch (error) {
|
|
429
|
+
throw new MastraError({
|
|
430
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "ALTER_TABLE", "FAILED"),
|
|
431
|
+
domain: ErrorDomain.STORAGE,
|
|
432
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
433
|
+
details: { tableName: args.tableName }
|
|
434
|
+
}, error);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
async insert({ tableName, record }) {
|
|
438
|
+
try {
|
|
439
|
+
const fullTableName = this.getTableName(tableName);
|
|
440
|
+
const processedRecord = await this.processRecord(record);
|
|
441
|
+
const columns = Object.keys(processedRecord);
|
|
442
|
+
const values = Object.values(processedRecord);
|
|
443
|
+
const { sql, params } = createSqlBuilder().insert(fullTableName, columns, values).build();
|
|
444
|
+
await this.executeQuery({
|
|
445
|
+
sql,
|
|
446
|
+
params
|
|
447
|
+
});
|
|
448
|
+
} catch (error) {
|
|
449
|
+
throw new MastraError({
|
|
450
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "INSERT", "FAILED"),
|
|
451
|
+
domain: ErrorDomain.STORAGE,
|
|
452
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
453
|
+
details: { tableName }
|
|
454
|
+
}, error);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
async batchInsert({ tableName, records }) {
|
|
458
|
+
try {
|
|
459
|
+
if (records.length === 0) return;
|
|
460
|
+
const fullTableName = this.getTableName(tableName);
|
|
461
|
+
const processedRecords = await Promise.all(records.map((record) => this.processRecord(record)));
|
|
462
|
+
const columns = Object.keys(processedRecords[0] || {});
|
|
463
|
+
for (const record of processedRecords) {
|
|
464
|
+
const values = Object.values(record);
|
|
465
|
+
const { sql, params } = createSqlBuilder().insert(fullTableName, columns, values).build();
|
|
466
|
+
await this.executeQuery({
|
|
467
|
+
sql,
|
|
468
|
+
params
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
} catch (error) {
|
|
472
|
+
throw new MastraError({
|
|
473
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BATCH_INSERT", "FAILED"),
|
|
474
|
+
domain: ErrorDomain.STORAGE,
|
|
475
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
476
|
+
details: { tableName }
|
|
477
|
+
}, error);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
async load({ tableName, keys, orderBy }) {
|
|
481
|
+
try {
|
|
482
|
+
const fullTableName = this.getTableName(tableName);
|
|
483
|
+
const query = createSqlBuilder().select("*").from(fullTableName);
|
|
484
|
+
let firstKey = true;
|
|
485
|
+
for (const [key, value] of Object.entries(keys)) if (firstKey) {
|
|
486
|
+
query.where(`${key} = ?`, value);
|
|
487
|
+
firstKey = false;
|
|
488
|
+
} else query.andWhere(`${key} = ?`, value);
|
|
489
|
+
if (orderBy) query.orderBy(orderBy.column, orderBy.direction);
|
|
490
|
+
query.limit(1);
|
|
491
|
+
const { sql, params } = query.build();
|
|
492
|
+
const result = await this.executeQuery({
|
|
493
|
+
sql,
|
|
494
|
+
params,
|
|
495
|
+
first: true
|
|
496
|
+
});
|
|
497
|
+
if (!result) return null;
|
|
498
|
+
const deserializedResult = {};
|
|
499
|
+
for (const [key, value] of Object.entries(result)) deserializedResult[key] = deserializeValue(value);
|
|
500
|
+
return deserializedResult;
|
|
501
|
+
} catch (error) {
|
|
502
|
+
throw new MastraError({
|
|
503
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LOAD", "FAILED"),
|
|
504
|
+
domain: ErrorDomain.STORAGE,
|
|
505
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
506
|
+
details: { tableName }
|
|
507
|
+
}, error);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
async processRecord(record) {
|
|
511
|
+
const processed = {};
|
|
512
|
+
for (const [key, value] of Object.entries(record)) processed[key] = this.serializeValue(value);
|
|
513
|
+
return processed;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Upsert multiple records in a batch operation
|
|
517
|
+
* @param tableName The table to insert into
|
|
518
|
+
* @param records The records to insert
|
|
519
|
+
* @param conflictKeys The columns to use for conflict detection (defaults to ['id'])
|
|
520
|
+
*/
|
|
521
|
+
async batchUpsert({ tableName, records, conflictKeys = ["id"] }) {
|
|
522
|
+
if (records.length === 0) return;
|
|
523
|
+
const fullTableName = this.getTableName(tableName);
|
|
524
|
+
try {
|
|
525
|
+
const batchSize = 50;
|
|
526
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
527
|
+
const recordsToInsert = records.slice(i, i + batchSize);
|
|
528
|
+
if (recordsToInsert.length > 0) {
|
|
529
|
+
const firstRecord = recordsToInsert[0];
|
|
530
|
+
const columns = Object.keys(firstRecord || {});
|
|
531
|
+
for (const record of recordsToInsert) {
|
|
532
|
+
const values = columns.map((col) => {
|
|
533
|
+
if (!record) return null;
|
|
534
|
+
const value = typeof col === "string" ? record[col] : null;
|
|
535
|
+
return this.serializeValue(value);
|
|
536
|
+
});
|
|
537
|
+
const recordToUpsert = columns.reduce((acc, col) => {
|
|
538
|
+
if (col !== "createdAt" && !conflictKeys.includes(col)) acc[col] = `excluded.${col}`;
|
|
539
|
+
return acc;
|
|
540
|
+
}, {});
|
|
541
|
+
const { sql, params } = createSqlBuilder().insert(fullTableName, columns, values, conflictKeys, recordToUpsert).build();
|
|
542
|
+
await this.executeQuery({
|
|
543
|
+
sql,
|
|
544
|
+
params
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
this.logger.debug(`Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`);
|
|
549
|
+
}
|
|
550
|
+
this.logger.debug(`Successfully batch upserted ${records.length} records into ${tableName}`);
|
|
551
|
+
} catch (error) {
|
|
552
|
+
throw new MastraError({
|
|
553
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BATCH_UPSERT", "FAILED"),
|
|
554
|
+
domain: ErrorDomain.STORAGE,
|
|
555
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
556
|
+
text: `Failed to batch upsert into ${tableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
557
|
+
details: { tableName }
|
|
558
|
+
}, error);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
//#endregion
|
|
563
|
+
//#region src/do/storage/domains/background-tasks/index.ts
|
|
564
|
+
function serializeJson(v) {
|
|
565
|
+
if (typeof v === "object" && v != null) return JSON.stringify(v);
|
|
566
|
+
return v ?? void 0;
|
|
567
|
+
}
|
|
568
|
+
function rowToTask(row) {
|
|
569
|
+
const parseJson = (v) => {
|
|
570
|
+
if (v == null) return void 0;
|
|
571
|
+
if (typeof v === "string") try {
|
|
572
|
+
return JSON.parse(v);
|
|
573
|
+
} catch {
|
|
574
|
+
return v;
|
|
575
|
+
}
|
|
576
|
+
return v;
|
|
577
|
+
};
|
|
578
|
+
const asDate = (v) => v ? new Date(String(v)) : void 0;
|
|
579
|
+
return {
|
|
580
|
+
id: String(row.id),
|
|
581
|
+
status: String(row.status),
|
|
582
|
+
toolName: String(row.tool_name),
|
|
583
|
+
toolCallId: String(row.tool_call_id),
|
|
584
|
+
args: parseJson(row.args) ?? {},
|
|
585
|
+
agentId: String(row.agent_id),
|
|
586
|
+
threadId: row.thread_id != null ? String(row.thread_id) : void 0,
|
|
587
|
+
resourceId: row.resource_id != null ? String(row.resource_id) : void 0,
|
|
588
|
+
runId: String(row.run_id),
|
|
589
|
+
result: parseJson(row.result),
|
|
590
|
+
error: parseJson(row.error),
|
|
591
|
+
suspendPayload: parseJson(row.suspend_payload),
|
|
592
|
+
retryCount: Number(row.retry_count ?? 0),
|
|
593
|
+
maxRetries: Number(row.max_retries ?? 0),
|
|
594
|
+
timeoutMs: Number(row.timeout_ms ?? 3e5),
|
|
595
|
+
createdAt: asDate(row.createdAt) ?? /* @__PURE__ */ new Date(),
|
|
596
|
+
startedAt: asDate(row.startedAt),
|
|
597
|
+
suspendedAt: asDate(row.suspendedAt),
|
|
598
|
+
completedAt: asDate(row.completedAt)
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
function dateColumnName(col) {
|
|
602
|
+
return col;
|
|
603
|
+
}
|
|
604
|
+
var BackgroundTasksStorageDO = class extends BackgroundTasksStorage {
|
|
605
|
+
#db;
|
|
606
|
+
constructor(config) {
|
|
607
|
+
super();
|
|
608
|
+
this.#db = new DODB(config);
|
|
609
|
+
}
|
|
610
|
+
async init() {
|
|
611
|
+
await this.#db.createTable({
|
|
612
|
+
tableName: TABLE_BACKGROUND_TASKS,
|
|
613
|
+
schema: TABLE_SCHEMAS[TABLE_BACKGROUND_TASKS]
|
|
614
|
+
});
|
|
615
|
+
await this.#db.alterTable({
|
|
616
|
+
tableName: TABLE_BACKGROUND_TASKS,
|
|
617
|
+
schema: TABLE_SCHEMAS[TABLE_BACKGROUND_TASKS],
|
|
618
|
+
ifNotExists: ["suspend_payload", "suspendedAt"]
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
async dangerouslyClearAll() {
|
|
622
|
+
await this.#db.clearTable({ tableName: TABLE_BACKGROUND_TASKS });
|
|
623
|
+
}
|
|
624
|
+
async createTask(task) {
|
|
625
|
+
try {
|
|
626
|
+
await this.#db.insert({
|
|
627
|
+
tableName: TABLE_BACKGROUND_TASKS,
|
|
628
|
+
record: {
|
|
629
|
+
id: task.id,
|
|
630
|
+
tool_call_id: task.toolCallId,
|
|
631
|
+
tool_name: task.toolName,
|
|
632
|
+
agent_id: task.agentId,
|
|
633
|
+
thread_id: task.threadId ?? null,
|
|
634
|
+
resource_id: task.resourceId ?? null,
|
|
635
|
+
run_id: task.runId,
|
|
636
|
+
status: task.status,
|
|
637
|
+
args: serializeJson(task.args),
|
|
638
|
+
result: serializeJson(task.result),
|
|
639
|
+
error: serializeJson(task.error),
|
|
640
|
+
suspend_payload: serializeJson(task.suspendPayload),
|
|
641
|
+
retry_count: task.retryCount,
|
|
642
|
+
max_retries: task.maxRetries,
|
|
643
|
+
timeout_ms: task.timeoutMs,
|
|
644
|
+
createdAt: task.createdAt.toISOString(),
|
|
645
|
+
startedAt: task.startedAt?.toISOString() ?? null,
|
|
646
|
+
suspendedAt: task.suspendedAt?.toISOString() ?? null,
|
|
647
|
+
completedAt: task.completedAt?.toISOString() ?? null
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
} catch (error) {
|
|
651
|
+
throw new MastraError({
|
|
652
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_CREATE", "FAILED"),
|
|
653
|
+
domain: ErrorDomain.STORAGE,
|
|
654
|
+
category: ErrorCategory.THIRD_PARTY
|
|
655
|
+
}, error);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
async updateTask(taskId, update) {
|
|
659
|
+
const columns = [];
|
|
660
|
+
const values = [];
|
|
661
|
+
if ("status" in update) {
|
|
662
|
+
columns.push("status");
|
|
663
|
+
values.push(update.status);
|
|
664
|
+
}
|
|
665
|
+
if ("result" in update) {
|
|
666
|
+
columns.push("result");
|
|
667
|
+
values.push(serializeJson(update.result));
|
|
668
|
+
}
|
|
669
|
+
if ("error" in update) {
|
|
670
|
+
columns.push("error");
|
|
671
|
+
values.push(serializeJson(update.error));
|
|
672
|
+
}
|
|
673
|
+
if ("suspendPayload" in update) {
|
|
674
|
+
columns.push("suspend_payload");
|
|
675
|
+
values.push(serializeJson(update.suspendPayload));
|
|
676
|
+
}
|
|
677
|
+
if ("retryCount" in update) {
|
|
678
|
+
columns.push("retry_count");
|
|
679
|
+
values.push(update.retryCount);
|
|
680
|
+
}
|
|
681
|
+
if ("startedAt" in update) {
|
|
682
|
+
columns.push("startedAt");
|
|
683
|
+
values.push(update.startedAt?.toISOString() ?? null);
|
|
684
|
+
}
|
|
685
|
+
if ("suspendedAt" in update) {
|
|
686
|
+
columns.push("suspendedAt");
|
|
687
|
+
values.push(update.suspendedAt?.toISOString() ?? null);
|
|
688
|
+
}
|
|
689
|
+
if ("completedAt" in update) {
|
|
690
|
+
columns.push("completedAt");
|
|
691
|
+
values.push(update.completedAt?.toISOString() ?? null);
|
|
692
|
+
}
|
|
693
|
+
if (columns.length === 0) return;
|
|
694
|
+
try {
|
|
695
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
696
|
+
const { sql, params } = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", taskId).build();
|
|
697
|
+
await this.#db.executeQuery({
|
|
698
|
+
sql,
|
|
699
|
+
params
|
|
700
|
+
});
|
|
701
|
+
} catch (error) {
|
|
702
|
+
throw new MastraError({
|
|
703
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_UPDATE", "FAILED"),
|
|
704
|
+
domain: ErrorDomain.STORAGE,
|
|
705
|
+
category: ErrorCategory.THIRD_PARTY
|
|
706
|
+
}, error);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
async getTask(taskId) {
|
|
710
|
+
try {
|
|
711
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
712
|
+
const { sql, params } = createSqlBuilder().select("*").from(fullTableName).where("id = ?", taskId).build();
|
|
713
|
+
const result = await this.#db.executeQuery({
|
|
714
|
+
sql,
|
|
715
|
+
params,
|
|
716
|
+
first: true
|
|
717
|
+
});
|
|
718
|
+
if (!result) return null;
|
|
719
|
+
const deserialized = {};
|
|
720
|
+
for (const [k, v] of Object.entries(result)) deserialized[k] = deserializeValue(v);
|
|
721
|
+
return rowToTask(deserialized);
|
|
722
|
+
} catch (error) {
|
|
723
|
+
throw new MastraError({
|
|
724
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_GET", "FAILED"),
|
|
725
|
+
domain: ErrorDomain.STORAGE,
|
|
726
|
+
category: ErrorCategory.THIRD_PARTY
|
|
727
|
+
}, error);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
async listTasks(filter) {
|
|
731
|
+
try {
|
|
732
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
733
|
+
const applyConditions = (builder) => {
|
|
734
|
+
if (filter.status) {
|
|
735
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
736
|
+
const placeholders = statuses.map(() => "?").join(", ");
|
|
737
|
+
builder.whereAnd(`status IN (${placeholders})`, ...statuses);
|
|
738
|
+
}
|
|
739
|
+
if (filter.agentId) builder.whereAnd("agent_id = ?", filter.agentId);
|
|
740
|
+
if (filter.threadId) builder.whereAnd("thread_id = ?", filter.threadId);
|
|
741
|
+
if (filter.resourceId) builder.whereAnd("resource_id = ?", filter.resourceId);
|
|
742
|
+
if (filter.toolName) builder.whereAnd("tool_name = ?", filter.toolName);
|
|
743
|
+
if (filter.toolCallId) builder.whereAnd("tool_call_id = ?", filter.toolCallId);
|
|
744
|
+
if (filter.runId) builder.whereAnd("run_id = ?", filter.runId);
|
|
745
|
+
const dateCol = dateColumnName(filter.dateFilterBy ?? "createdAt");
|
|
746
|
+
if (filter.fromDate) builder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
|
|
747
|
+
if (filter.toDate) builder.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
|
|
748
|
+
};
|
|
749
|
+
const countQuery = createSqlBuilder().count().from(fullTableName);
|
|
750
|
+
applyConditions(countQuery);
|
|
751
|
+
const { sql: countSql, params: countParams } = countQuery.build();
|
|
752
|
+
const countResult = await this.#db.executeQuery({
|
|
753
|
+
sql: countSql,
|
|
754
|
+
params: countParams,
|
|
755
|
+
first: true
|
|
756
|
+
});
|
|
757
|
+
const total = Number(countResult?.count ?? 0);
|
|
758
|
+
const dataQuery = createSqlBuilder().select("*").from(fullTableName);
|
|
759
|
+
applyConditions(dataQuery);
|
|
760
|
+
const orderBy = dateColumnName(filter.orderBy ?? "createdAt");
|
|
761
|
+
const direction = filter.orderDirection === "desc" ? "DESC" : "ASC";
|
|
762
|
+
dataQuery.orderBy(orderBy, direction);
|
|
763
|
+
if (filter.perPage != null) {
|
|
764
|
+
dataQuery.limit(filter.perPage);
|
|
765
|
+
if (filter.page != null) dataQuery.offset(filter.page * filter.perPage);
|
|
766
|
+
}
|
|
767
|
+
const { sql, params } = dataQuery.build();
|
|
768
|
+
return {
|
|
769
|
+
tasks: (await this.#db.executeQuery({
|
|
770
|
+
sql,
|
|
771
|
+
params
|
|
772
|
+
}) ?? []).map((row) => {
|
|
773
|
+
const deserialized = {};
|
|
774
|
+
for (const [k, v] of Object.entries(row)) deserialized[k] = deserializeValue(v);
|
|
775
|
+
return rowToTask(deserialized);
|
|
776
|
+
}),
|
|
777
|
+
total
|
|
778
|
+
};
|
|
779
|
+
} catch (error) {
|
|
780
|
+
throw new MastraError({
|
|
781
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_LIST", "FAILED"),
|
|
782
|
+
domain: ErrorDomain.STORAGE,
|
|
783
|
+
category: ErrorCategory.THIRD_PARTY
|
|
784
|
+
}, error);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
async deleteTask(taskId) {
|
|
788
|
+
try {
|
|
789
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
790
|
+
const { sql, params } = createSqlBuilder().delete(fullTableName).where("id = ?", taskId).build();
|
|
791
|
+
await this.#db.executeQuery({
|
|
792
|
+
sql,
|
|
793
|
+
params
|
|
794
|
+
});
|
|
795
|
+
} catch (error) {
|
|
796
|
+
throw new MastraError({
|
|
797
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_DELETE", "FAILED"),
|
|
798
|
+
domain: ErrorDomain.STORAGE,
|
|
799
|
+
category: ErrorCategory.THIRD_PARTY
|
|
800
|
+
}, error);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
async deleteTasks(filter) {
|
|
804
|
+
try {
|
|
805
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
806
|
+
const query = createSqlBuilder().delete(fullTableName);
|
|
807
|
+
if (filter.status) {
|
|
808
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
809
|
+
const placeholders = statuses.map(() => "?").join(", ");
|
|
810
|
+
query.whereAnd(`status IN (${placeholders})`, ...statuses);
|
|
811
|
+
}
|
|
812
|
+
if (filter.agentId) query.whereAnd("agent_id = ?", filter.agentId);
|
|
813
|
+
if (filter.threadId) query.whereAnd("thread_id = ?", filter.threadId);
|
|
814
|
+
if (filter.resourceId) query.whereAnd("resource_id = ?", filter.resourceId);
|
|
815
|
+
if (filter.toolName) query.whereAnd("tool_name = ?", filter.toolName);
|
|
816
|
+
if (filter.toolCallId) query.whereAnd("tool_call_id = ?", filter.toolCallId);
|
|
817
|
+
if (filter.runId) query.whereAnd("run_id = ?", filter.runId);
|
|
818
|
+
const dateCol = dateColumnName(filter.dateFilterBy ?? "createdAt");
|
|
819
|
+
if (filter.fromDate) query.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
|
|
820
|
+
if (filter.toDate) query.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
|
|
821
|
+
const { sql, params } = query.build();
|
|
822
|
+
await this.#db.executeQuery({
|
|
823
|
+
sql,
|
|
824
|
+
params
|
|
825
|
+
});
|
|
826
|
+
} catch (error) {
|
|
827
|
+
throw new MastraError({
|
|
828
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "BACKGROUND_TASKS_DELETE_MANY", "FAILED"),
|
|
829
|
+
domain: ErrorDomain.STORAGE,
|
|
830
|
+
category: ErrorCategory.THIRD_PARTY
|
|
831
|
+
}, error);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
async getRunningCount() {
|
|
835
|
+
const { total } = await this.listTasks({ status: "running" });
|
|
836
|
+
return total;
|
|
837
|
+
}
|
|
838
|
+
async getRunningCountByAgent(agentId) {
|
|
839
|
+
const { total } = await this.listTasks({
|
|
840
|
+
status: "running",
|
|
841
|
+
agentId
|
|
842
|
+
});
|
|
843
|
+
return total;
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
//#endregion
|
|
847
|
+
//#region src/do/storage/domains/memory/index.ts
|
|
848
|
+
function addSqliteMetadataFilter(conditions, params, metadataFilter) {
|
|
849
|
+
if (!metadataFilter) return;
|
|
850
|
+
for (const [key, value] of Object.entries(metadataFilter)) {
|
|
851
|
+
const path = `$.metadata.${key}`;
|
|
852
|
+
conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) IS NOT NULL ELSE 0 END`);
|
|
853
|
+
params.push(path);
|
|
854
|
+
addSqliteMetadataValuePredicate(conditions, params, path, value);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
function addSqliteMetadataValuePredicate(conditions, params, path, value) {
|
|
858
|
+
if (value === null) {
|
|
859
|
+
conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) = 'null' ELSE 0 END`);
|
|
860
|
+
params.push(path);
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
if (typeof value === "string") {
|
|
864
|
+
conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) = 'text' AND json_extract(content, ?) = ? ELSE 0 END`);
|
|
865
|
+
params.push(path, path, value);
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
if (typeof value === "number") {
|
|
869
|
+
conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) IN ('integer', 'real') AND json_extract(content, ?) = ? ELSE 0 END`);
|
|
870
|
+
params.push(path, path, value);
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) = ? AND json_extract(content, ?) = ? ELSE 0 END`);
|
|
874
|
+
params.push(path, value ? "true" : "false", path, value ? 1 : 0);
|
|
875
|
+
}
|
|
876
|
+
var MemoryStorageDO = class extends MemoryStorage {
|
|
877
|
+
#db;
|
|
878
|
+
constructor(config) {
|
|
879
|
+
super();
|
|
880
|
+
this.#db = new DODB(config);
|
|
881
|
+
}
|
|
882
|
+
async init() {
|
|
883
|
+
await this.#db.createTable({
|
|
884
|
+
tableName: TABLE_THREADS,
|
|
885
|
+
schema: TABLE_SCHEMAS[TABLE_THREADS]
|
|
886
|
+
});
|
|
887
|
+
await this.#db.createTable({
|
|
888
|
+
tableName: TABLE_MESSAGES,
|
|
889
|
+
schema: TABLE_SCHEMAS[TABLE_MESSAGES]
|
|
890
|
+
});
|
|
891
|
+
await this.#db.createTable({
|
|
892
|
+
tableName: TABLE_RESOURCES,
|
|
893
|
+
schema: TABLE_SCHEMAS[TABLE_RESOURCES]
|
|
894
|
+
});
|
|
895
|
+
await this.#db.alterTable({
|
|
896
|
+
tableName: TABLE_MESSAGES,
|
|
897
|
+
schema: TABLE_SCHEMAS[TABLE_MESSAGES],
|
|
898
|
+
ifNotExists: ["resourceId"]
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
async dangerouslyClearAll() {
|
|
902
|
+
await this.#db.clearTable({ tableName: TABLE_MESSAGES });
|
|
903
|
+
await this.#db.clearTable({ tableName: TABLE_THREADS });
|
|
904
|
+
await this.#db.clearTable({ tableName: TABLE_RESOURCES });
|
|
905
|
+
}
|
|
906
|
+
async getResourceById({ resourceId }) {
|
|
907
|
+
const resource = await this.#db.load({
|
|
908
|
+
tableName: TABLE_RESOURCES,
|
|
909
|
+
keys: { id: resourceId }
|
|
910
|
+
});
|
|
911
|
+
if (!resource) return null;
|
|
912
|
+
try {
|
|
913
|
+
return {
|
|
914
|
+
...resource,
|
|
915
|
+
createdAt: ensureDate(resource.createdAt),
|
|
916
|
+
updatedAt: ensureDate(resource.updatedAt),
|
|
917
|
+
metadata: typeof resource.metadata === "string" ? JSON.parse(resource.metadata || "{}") : resource.metadata
|
|
918
|
+
};
|
|
919
|
+
} catch (error) {
|
|
920
|
+
const mastraError = new MastraError({
|
|
921
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_RESOURCE_BY_ID", "FAILED"),
|
|
922
|
+
domain: ErrorDomain.STORAGE,
|
|
923
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
924
|
+
text: `Error processing resource ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
925
|
+
details: { resourceId }
|
|
926
|
+
}, error);
|
|
927
|
+
this.logger?.error(mastraError.toString());
|
|
928
|
+
this.logger?.trackException(mastraError);
|
|
929
|
+
return null;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
async saveResource({ resource }) {
|
|
933
|
+
const fullTableName = this.#db.getTableName(TABLE_RESOURCES);
|
|
934
|
+
const resourceToSave = {
|
|
935
|
+
id: resource.id,
|
|
936
|
+
workingMemory: resource.workingMemory,
|
|
937
|
+
metadata: resource.metadata ? JSON.stringify(resource.metadata) : null,
|
|
938
|
+
createdAt: resource.createdAt,
|
|
939
|
+
updatedAt: resource.updatedAt
|
|
940
|
+
};
|
|
941
|
+
const processedRecord = await this.#db.processRecord(resourceToSave);
|
|
942
|
+
const columns = Object.keys(processedRecord);
|
|
943
|
+
const values = Object.values(processedRecord);
|
|
944
|
+
const { sql, params } = createSqlBuilder().insert(fullTableName, columns, values, ["id"], {
|
|
945
|
+
workingMemory: "excluded.workingMemory",
|
|
946
|
+
metadata: "excluded.metadata",
|
|
947
|
+
createdAt: "excluded.createdAt",
|
|
948
|
+
updatedAt: "excluded.updatedAt"
|
|
949
|
+
}).build();
|
|
950
|
+
try {
|
|
951
|
+
await this.#db.executeQuery({
|
|
952
|
+
sql,
|
|
953
|
+
params
|
|
954
|
+
});
|
|
955
|
+
return resource;
|
|
956
|
+
} catch (error) {
|
|
957
|
+
throw new MastraError({
|
|
958
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "SAVE_RESOURCE", "FAILED"),
|
|
959
|
+
domain: ErrorDomain.STORAGE,
|
|
960
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
961
|
+
text: `Failed to save resource to ${fullTableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
962
|
+
details: { resourceId: resource.id }
|
|
963
|
+
}, error);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
async updateResource({ resourceId, workingMemory, metadata }) {
|
|
967
|
+
const existingResource = await this.getResourceById({ resourceId });
|
|
968
|
+
if (!existingResource) {
|
|
969
|
+
const newResource = {
|
|
970
|
+
id: resourceId,
|
|
971
|
+
workingMemory,
|
|
972
|
+
metadata: metadata || {},
|
|
973
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
974
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
975
|
+
};
|
|
976
|
+
return this.saveResource({ resource: newResource });
|
|
977
|
+
}
|
|
978
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
979
|
+
const updatedResource = {
|
|
980
|
+
...existingResource,
|
|
981
|
+
workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
|
|
982
|
+
metadata: {
|
|
983
|
+
...existingResource.metadata,
|
|
984
|
+
...metadata
|
|
985
|
+
},
|
|
986
|
+
updatedAt
|
|
987
|
+
};
|
|
988
|
+
const fullTableName = this.#db.getTableName(TABLE_RESOURCES);
|
|
989
|
+
const columns = [
|
|
990
|
+
"workingMemory",
|
|
991
|
+
"metadata",
|
|
992
|
+
"updatedAt"
|
|
993
|
+
];
|
|
994
|
+
const values = [
|
|
995
|
+
updatedResource.workingMemory,
|
|
996
|
+
JSON.stringify(updatedResource.metadata),
|
|
997
|
+
updatedAt.toISOString()
|
|
998
|
+
];
|
|
999
|
+
const { sql, params } = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", resourceId).build();
|
|
1000
|
+
try {
|
|
1001
|
+
await this.#db.executeQuery({
|
|
1002
|
+
sql,
|
|
1003
|
+
params
|
|
1004
|
+
});
|
|
1005
|
+
return updatedResource;
|
|
1006
|
+
} catch (error) {
|
|
1007
|
+
throw new MastraError({
|
|
1008
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "UPDATE_RESOURCE", "FAILED"),
|
|
1009
|
+
domain: ErrorDomain.STORAGE,
|
|
1010
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1011
|
+
text: `Failed to update resource ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1012
|
+
details: { resourceId }
|
|
1013
|
+
}, error);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
async getThreadById({ threadId, resourceId }) {
|
|
1017
|
+
const thread = await this.#db.load({
|
|
1018
|
+
tableName: TABLE_THREADS,
|
|
1019
|
+
keys: { id: threadId }
|
|
1020
|
+
});
|
|
1021
|
+
if (!thread || resourceId !== void 0 && thread.resourceId !== resourceId) return null;
|
|
1022
|
+
try {
|
|
1023
|
+
return {
|
|
1024
|
+
...thread,
|
|
1025
|
+
createdAt: ensureDate(thread.createdAt),
|
|
1026
|
+
updatedAt: ensureDate(thread.updatedAt),
|
|
1027
|
+
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
1028
|
+
};
|
|
1029
|
+
} catch (error) {
|
|
1030
|
+
const mastraError = new MastraError({
|
|
1031
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_THREAD_BY_ID", "FAILED"),
|
|
1032
|
+
domain: ErrorDomain.STORAGE,
|
|
1033
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1034
|
+
text: `Error processing thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1035
|
+
details: { threadId }
|
|
1036
|
+
}, error);
|
|
1037
|
+
this.logger?.error(mastraError.toString());
|
|
1038
|
+
this.logger?.trackException(mastraError);
|
|
1039
|
+
return null;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
async listThreads(args) {
|
|
1043
|
+
const { page = 0, perPage: perPageInput, orderBy, filter } = args;
|
|
1044
|
+
try {
|
|
1045
|
+
this.validatePaginationInput(page, perPageInput ?? 100);
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
throw new MastraError({
|
|
1048
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_THREADS", "INVALID_PAGE"),
|
|
1049
|
+
domain: ErrorDomain.STORAGE,
|
|
1050
|
+
category: ErrorCategory.USER,
|
|
1051
|
+
details: {
|
|
1052
|
+
page,
|
|
1053
|
+
...perPageInput !== void 0 && { perPage: perPageInput }
|
|
1054
|
+
}
|
|
1055
|
+
}, error instanceof Error ? error : /* @__PURE__ */ new Error("Invalid pagination parameters"));
|
|
1056
|
+
}
|
|
1057
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1058
|
+
try {
|
|
1059
|
+
this.validateMetadataKeys(filter?.metadata);
|
|
1060
|
+
} catch (error) {
|
|
1061
|
+
throw new MastraError({
|
|
1062
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_THREADS", "INVALID_METADATA_KEY"),
|
|
1063
|
+
domain: ErrorDomain.STORAGE,
|
|
1064
|
+
category: ErrorCategory.USER,
|
|
1065
|
+
details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
|
|
1066
|
+
}, error instanceof Error ? error : /* @__PURE__ */ new Error("Invalid metadata key"));
|
|
1067
|
+
}
|
|
1068
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1069
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1070
|
+
const fullTableName = this.#db.getTableName(TABLE_THREADS);
|
|
1071
|
+
const mapRowToStorageThreadType = (row) => ({
|
|
1072
|
+
...row,
|
|
1073
|
+
createdAt: ensureDate(row.createdAt),
|
|
1074
|
+
updatedAt: ensureDate(row.updatedAt),
|
|
1075
|
+
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
|
|
1076
|
+
});
|
|
1077
|
+
try {
|
|
1078
|
+
let countQuery = createSqlBuilder().count().from(fullTableName);
|
|
1079
|
+
let selectQuery = createSqlBuilder().select("*").from(fullTableName);
|
|
1080
|
+
if (filter?.resourceId) {
|
|
1081
|
+
countQuery = countQuery.whereAnd("resourceId = ?", filter.resourceId);
|
|
1082
|
+
selectQuery = selectQuery.whereAnd("resourceId = ?", filter.resourceId);
|
|
1083
|
+
}
|
|
1084
|
+
if (filter?.metadata && Object.keys(filter.metadata).length > 0) for (const [key, value] of Object.entries(filter.metadata)) {
|
|
1085
|
+
if (value !== null && typeof value === "object") throw new MastraError({
|
|
1086
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_THREADS", "INVALID_METADATA_VALUE"),
|
|
1087
|
+
domain: ErrorDomain.STORAGE,
|
|
1088
|
+
category: ErrorCategory.USER,
|
|
1089
|
+
text: `Metadata filter value for key "${key}" must be a scalar type (string, number, boolean, or null), got ${Array.isArray(value) ? "array" : "object"}`,
|
|
1090
|
+
details: {
|
|
1091
|
+
key,
|
|
1092
|
+
valueType: Array.isArray(value) ? "array" : "object"
|
|
1093
|
+
}
|
|
1094
|
+
}, /* @__PURE__ */ new Error("Invalid metadata filter value type"));
|
|
1095
|
+
if (value === null) {
|
|
1096
|
+
const condition = `json_extract(metadata, '$.${key}') IS NULL`;
|
|
1097
|
+
countQuery = countQuery.whereAnd(condition);
|
|
1098
|
+
selectQuery = selectQuery.whereAnd(condition);
|
|
1099
|
+
} else {
|
|
1100
|
+
const condition = `json_extract(metadata, '$.${key}') = ?`;
|
|
1101
|
+
const filterValue = value;
|
|
1102
|
+
countQuery = countQuery.whereAnd(condition, filterValue);
|
|
1103
|
+
selectQuery = selectQuery.whereAnd(condition, filterValue);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
const countResult = await this.#db.executeQuery(countQuery.build());
|
|
1107
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1108
|
+
if (total === 0) return {
|
|
1109
|
+
threads: [],
|
|
1110
|
+
total: 0,
|
|
1111
|
+
page,
|
|
1112
|
+
perPage: perPageForResponse,
|
|
1113
|
+
hasMore: false
|
|
1114
|
+
};
|
|
1115
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1116
|
+
selectQuery = selectQuery.orderBy(field, direction).limit(limitValue).offset(offset);
|
|
1117
|
+
return {
|
|
1118
|
+
threads: (await this.#db.executeQuery(selectQuery.build())).map(mapRowToStorageThreadType),
|
|
1119
|
+
total,
|
|
1120
|
+
page,
|
|
1121
|
+
perPage: perPageForResponse,
|
|
1122
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
1123
|
+
};
|
|
1124
|
+
} catch (error) {
|
|
1125
|
+
if (error instanceof MastraError && error.category === ErrorCategory.USER) throw error;
|
|
1126
|
+
const mastraError = new MastraError({
|
|
1127
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_THREADS", "FAILED"),
|
|
1128
|
+
domain: ErrorDomain.STORAGE,
|
|
1129
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1130
|
+
text: `Error listing threads: ${error instanceof Error ? error.message : String(error)}`,
|
|
1131
|
+
details: {
|
|
1132
|
+
...filter?.resourceId && { resourceId: filter.resourceId },
|
|
1133
|
+
hasMetadataFilter: !!filter?.metadata
|
|
1134
|
+
}
|
|
1135
|
+
}, error);
|
|
1136
|
+
this.logger?.error(mastraError.toString());
|
|
1137
|
+
this.logger?.trackException(mastraError);
|
|
1138
|
+
return {
|
|
1139
|
+
threads: [],
|
|
1140
|
+
total: 0,
|
|
1141
|
+
page,
|
|
1142
|
+
perPage: perPageForResponse,
|
|
1143
|
+
hasMore: false
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
async saveThread({ thread }) {
|
|
1148
|
+
const fullTableName = this.#db.getTableName(TABLE_THREADS);
|
|
1149
|
+
const threadToSave = {
|
|
1150
|
+
id: thread.id,
|
|
1151
|
+
resourceId: thread.resourceId,
|
|
1152
|
+
title: thread.title,
|
|
1153
|
+
metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
|
|
1154
|
+
createdAt: thread.createdAt.toISOString(),
|
|
1155
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
1156
|
+
};
|
|
1157
|
+
const processedRecord = await this.#db.processRecord(threadToSave);
|
|
1158
|
+
const columns = Object.keys(processedRecord);
|
|
1159
|
+
const values = Object.values(processedRecord);
|
|
1160
|
+
const { sql, params } = createSqlBuilder().insert(fullTableName, columns, values, ["id"], {
|
|
1161
|
+
resourceId: "excluded.resourceId",
|
|
1162
|
+
title: "excluded.title",
|
|
1163
|
+
metadata: "excluded.metadata",
|
|
1164
|
+
createdAt: "excluded.createdAt",
|
|
1165
|
+
updatedAt: "excluded.updatedAt"
|
|
1166
|
+
}).build();
|
|
1167
|
+
try {
|
|
1168
|
+
await this.#db.executeQuery({
|
|
1169
|
+
sql,
|
|
1170
|
+
params
|
|
1171
|
+
});
|
|
1172
|
+
return thread;
|
|
1173
|
+
} catch (error) {
|
|
1174
|
+
throw new MastraError({
|
|
1175
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "SAVE_THREAD", "FAILED"),
|
|
1176
|
+
domain: ErrorDomain.STORAGE,
|
|
1177
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1178
|
+
text: `Failed to save thread to ${fullTableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1179
|
+
details: { threadId: thread.id }
|
|
1180
|
+
}, error);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
async updateThread({ id, title, metadata }) {
|
|
1184
|
+
const thread = await this.getThreadById({ threadId: id });
|
|
1185
|
+
try {
|
|
1186
|
+
if (!thread) throw new Error(`Thread ${id} not found`);
|
|
1187
|
+
const fullTableName = this.#db.getTableName(TABLE_THREADS);
|
|
1188
|
+
const mergedMetadata = {
|
|
1189
|
+
...thread.metadata,
|
|
1190
|
+
...metadata
|
|
1191
|
+
};
|
|
1192
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
1193
|
+
const columns = [
|
|
1194
|
+
"title",
|
|
1195
|
+
"metadata",
|
|
1196
|
+
"updatedAt"
|
|
1197
|
+
];
|
|
1198
|
+
const values = [
|
|
1199
|
+
title,
|
|
1200
|
+
JSON.stringify(mergedMetadata),
|
|
1201
|
+
updatedAt.toISOString()
|
|
1202
|
+
];
|
|
1203
|
+
const { sql, params } = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id).build();
|
|
1204
|
+
await this.#db.executeQuery({
|
|
1205
|
+
sql,
|
|
1206
|
+
params
|
|
1207
|
+
});
|
|
1208
|
+
return {
|
|
1209
|
+
...thread,
|
|
1210
|
+
title,
|
|
1211
|
+
metadata: mergedMetadata,
|
|
1212
|
+
updatedAt
|
|
1213
|
+
};
|
|
1214
|
+
} catch (error) {
|
|
1215
|
+
throw new MastraError({
|
|
1216
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "UPDATE_THREAD", "FAILED"),
|
|
1217
|
+
domain: ErrorDomain.STORAGE,
|
|
1218
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1219
|
+
text: `Failed to update thread ${id}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1220
|
+
details: { threadId: id }
|
|
1221
|
+
}, error);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
async deleteThread({ threadId }) {
|
|
1225
|
+
const fullTableName = this.#db.getTableName(TABLE_THREADS);
|
|
1226
|
+
try {
|
|
1227
|
+
const messagesTableName = this.#db.getTableName(TABLE_MESSAGES);
|
|
1228
|
+
const { sql: messagesSql, params: messagesParams } = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId).build();
|
|
1229
|
+
await this.#db.executeQuery({
|
|
1230
|
+
sql: messagesSql,
|
|
1231
|
+
params: messagesParams
|
|
1232
|
+
});
|
|
1233
|
+
const { sql: threadSql, params: threadParams } = createSqlBuilder().delete(fullTableName).where("id = ?", threadId).build();
|
|
1234
|
+
await this.#db.executeQuery({
|
|
1235
|
+
sql: threadSql,
|
|
1236
|
+
params: threadParams
|
|
1237
|
+
});
|
|
1238
|
+
} catch (error) {
|
|
1239
|
+
throw new MastraError({
|
|
1240
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "DELETE_THREAD", "FAILED"),
|
|
1241
|
+
domain: ErrorDomain.STORAGE,
|
|
1242
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1243
|
+
text: `Failed to delete thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1244
|
+
details: { threadId }
|
|
1245
|
+
}, error);
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
async saveMessages(args) {
|
|
1249
|
+
const { messages } = args;
|
|
1250
|
+
if (messages.length === 0) return { messages: [] };
|
|
1251
|
+
try {
|
|
1252
|
+
const now = /* @__PURE__ */ new Date();
|
|
1253
|
+
for (const [i, message] of messages.entries()) {
|
|
1254
|
+
if (!message.id) throw new Error(`Message at index ${i} missing id`);
|
|
1255
|
+
if (!message.threadId) throw new Error(`Message at index ${i} missing threadId`);
|
|
1256
|
+
if (!message.content) throw new Error(`Message at index ${i} missing content`);
|
|
1257
|
+
if (!message.role) throw new Error(`Message at index ${i} missing role`);
|
|
1258
|
+
if (!message.resourceId) throw new Error(`Message at index ${i} missing resourceId`);
|
|
1259
|
+
}
|
|
1260
|
+
const uniqueThreadIds = [...new Set(messages.map((m) => m.threadId))];
|
|
1261
|
+
const threads = await Promise.all(uniqueThreadIds.map((id) => this.getThreadById({ threadId: id })));
|
|
1262
|
+
const missingThreadId = uniqueThreadIds.find((id, i) => !threads[i]);
|
|
1263
|
+
if (missingThreadId) throw new Error(`Thread ${missingThreadId} not found`);
|
|
1264
|
+
const messagesToInsert = messages.map((message) => {
|
|
1265
|
+
const createdAt = message.createdAt ? new Date(message.createdAt) : now;
|
|
1266
|
+
return {
|
|
1267
|
+
id: message.id,
|
|
1268
|
+
thread_id: message.threadId,
|
|
1269
|
+
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
1270
|
+
createdAt: createdAt.toISOString(),
|
|
1271
|
+
role: message.role,
|
|
1272
|
+
type: message.type || "v2",
|
|
1273
|
+
resourceId: message.resourceId
|
|
1274
|
+
};
|
|
1275
|
+
});
|
|
1276
|
+
await Promise.all([this.#db.batchUpsert({
|
|
1277
|
+
tableName: TABLE_MESSAGES,
|
|
1278
|
+
records: messagesToInsert
|
|
1279
|
+
}), ...uniqueThreadIds.map((tid) => this.#db.executeQuery({
|
|
1280
|
+
sql: `UPDATE ${this.#db.getTableName(TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
|
|
1281
|
+
params: [now.toISOString(), tid]
|
|
1282
|
+
}))]);
|
|
1283
|
+
this.logger.debug(`Saved ${messages.length} messages`);
|
|
1284
|
+
return { messages: new MessageList().add(messages, "memory").get.all.db() };
|
|
1285
|
+
} catch (error) {
|
|
1286
|
+
throw new MastraError({
|
|
1287
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "SAVE_MESSAGES", "FAILED"),
|
|
1288
|
+
domain: ErrorDomain.STORAGE,
|
|
1289
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1290
|
+
text: `Failed to save messages: ${error instanceof Error ? error.message : String(error)}`
|
|
1291
|
+
}, error);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
async _getIncludedMessages(include) {
|
|
1295
|
+
if (!include || include.length === 0) return null;
|
|
1296
|
+
const unionQueries = [];
|
|
1297
|
+
const params = [];
|
|
1298
|
+
let paramIdx = 1;
|
|
1299
|
+
const tableName = this.#db.getTableName(TABLE_MESSAGES);
|
|
1300
|
+
for (const inc of include) {
|
|
1301
|
+
const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
|
|
1302
|
+
unionQueries.push(`
|
|
1303
|
+
SELECT * FROM (
|
|
1304
|
+
WITH target_thread AS (
|
|
1305
|
+
SELECT thread_id FROM ${tableName} WHERE id = ?
|
|
1306
|
+
),
|
|
1307
|
+
ordered_messages AS (
|
|
1308
|
+
SELECT
|
|
1309
|
+
*,
|
|
1310
|
+
ROW_NUMBER() OVER (ORDER BY createdAt ASC) AS row_num
|
|
1311
|
+
FROM ${tableName}
|
|
1312
|
+
WHERE thread_id = (SELECT thread_id FROM target_thread)
|
|
1313
|
+
)
|
|
1314
|
+
SELECT
|
|
1315
|
+
m.id,
|
|
1316
|
+
m.content,
|
|
1317
|
+
m.role,
|
|
1318
|
+
m.type,
|
|
1319
|
+
m.createdAt,
|
|
1320
|
+
m.thread_id AS threadId,
|
|
1321
|
+
m.resourceId
|
|
1322
|
+
FROM ordered_messages m
|
|
1323
|
+
WHERE m.id = ?
|
|
1324
|
+
OR EXISTS (
|
|
1325
|
+
SELECT 1 FROM ordered_messages target
|
|
1326
|
+
WHERE target.id = ?
|
|
1327
|
+
AND (
|
|
1328
|
+
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
1329
|
+
OR
|
|
1330
|
+
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
1331
|
+
)
|
|
1332
|
+
)
|
|
1333
|
+
) AS query_${paramIdx}
|
|
1334
|
+
`);
|
|
1335
|
+
params.push(id, id, id, withNextMessages, withPreviousMessages);
|
|
1336
|
+
paramIdx++;
|
|
1337
|
+
}
|
|
1338
|
+
const finalQuery = unionQueries.join(" UNION ALL ") + " ORDER BY createdAt ASC";
|
|
1339
|
+
const messages = await this.#db.executeQuery({
|
|
1340
|
+
sql: finalQuery,
|
|
1341
|
+
params
|
|
1342
|
+
});
|
|
1343
|
+
if (!Array.isArray(messages)) return [];
|
|
1344
|
+
return messages.map((message) => {
|
|
1345
|
+
const processedMsg = {};
|
|
1346
|
+
for (const [key, value] of Object.entries(message)) {
|
|
1347
|
+
if (key === `type` && value === `v2`) continue;
|
|
1348
|
+
processedMsg[key] = deserializeValue(value);
|
|
1349
|
+
}
|
|
1350
|
+
return processedMsg;
|
|
1351
|
+
});
|
|
1352
|
+
}
|
|
1353
|
+
async listMessagesById({ messageIds }) {
|
|
1354
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
1355
|
+
const fullTableName = this.#db.getTableName(TABLE_MESSAGES);
|
|
1356
|
+
const messages = [];
|
|
1357
|
+
try {
|
|
1358
|
+
const query = createSqlBuilder().select([
|
|
1359
|
+
"id",
|
|
1360
|
+
"content",
|
|
1361
|
+
"role",
|
|
1362
|
+
"type",
|
|
1363
|
+
"createdAt",
|
|
1364
|
+
"thread_id AS threadId",
|
|
1365
|
+
"resourceId"
|
|
1366
|
+
]).from(fullTableName).where(`id in (${messageIds.map(() => "?").join(",")})`, ...messageIds);
|
|
1367
|
+
query.orderBy("createdAt", "DESC");
|
|
1368
|
+
const { sql, params } = query.build();
|
|
1369
|
+
const result = await this.#db.executeQuery({
|
|
1370
|
+
sql,
|
|
1371
|
+
params
|
|
1372
|
+
});
|
|
1373
|
+
if (Array.isArray(result)) messages.push(...result);
|
|
1374
|
+
const processedMessages = messages.map((message) => {
|
|
1375
|
+
const processedMsg = {};
|
|
1376
|
+
for (const [key, value] of Object.entries(message)) {
|
|
1377
|
+
if (key === `type` && value === `v2`) continue;
|
|
1378
|
+
processedMsg[key] = deserializeValue(value);
|
|
1379
|
+
}
|
|
1380
|
+
return processedMsg;
|
|
1381
|
+
});
|
|
1382
|
+
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
1383
|
+
return { messages: new MessageList().add(processedMessages, "memory").get.all.db() };
|
|
1384
|
+
} catch (error) {
|
|
1385
|
+
const mastraError = new MastraError({
|
|
1386
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_MESSAGES_BY_ID", "FAILED"),
|
|
1387
|
+
domain: ErrorDomain.STORAGE,
|
|
1388
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1389
|
+
text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
1390
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
1391
|
+
}, error);
|
|
1392
|
+
this.logger?.error?.(mastraError.toString());
|
|
1393
|
+
this.logger?.trackException?.(mastraError);
|
|
1394
|
+
throw mastraError;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
async listMessages(args) {
|
|
1398
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
1399
|
+
const threadIds = Array.isArray(threadId) ? threadId : [threadId];
|
|
1400
|
+
if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) throw new MastraError({
|
|
1401
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_MESSAGES", "INVALID_THREAD_ID"),
|
|
1402
|
+
domain: ErrorDomain.STORAGE,
|
|
1403
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1404
|
+
details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
|
|
1405
|
+
}, /* @__PURE__ */ new Error("threadId must be a non-empty string or array of non-empty strings"));
|
|
1406
|
+
if (page < 0) throw new MastraError({
|
|
1407
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_MESSAGES", "INVALID_PAGE"),
|
|
1408
|
+
domain: ErrorDomain.STORAGE,
|
|
1409
|
+
category: ErrorCategory.USER,
|
|
1410
|
+
details: { page }
|
|
1411
|
+
}, /* @__PURE__ */ new Error("page must be >= 0"));
|
|
1412
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
1413
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1414
|
+
const metadataFilter = validateStorageMetadataFilter(filter?.metadata);
|
|
1415
|
+
try {
|
|
1416
|
+
const fullTableName = this.#db.getTableName(TABLE_MESSAGES);
|
|
1417
|
+
let query = `
|
|
1418
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1419
|
+
FROM ${fullTableName}
|
|
1420
|
+
WHERE thread_id IN (${threadIds.map(() => "?").join(", ")})
|
|
1421
|
+
`;
|
|
1422
|
+
const queryParams = [...threadIds];
|
|
1423
|
+
if (resourceId) {
|
|
1424
|
+
query += ` AND resourceId = ?`;
|
|
1425
|
+
queryParams.push(resourceId);
|
|
1426
|
+
}
|
|
1427
|
+
const dateRange = filter?.dateRange;
|
|
1428
|
+
if (dateRange?.start) {
|
|
1429
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
1430
|
+
const startOp = dateRange.startExclusive ? ">" : ">=";
|
|
1431
|
+
query += ` AND createdAt ${startOp} ?`;
|
|
1432
|
+
queryParams.push(startDate);
|
|
1433
|
+
}
|
|
1434
|
+
if (dateRange?.end) {
|
|
1435
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
1436
|
+
const endOp = dateRange.endExclusive ? "<" : "<=";
|
|
1437
|
+
query += ` AND createdAt ${endOp} ?`;
|
|
1438
|
+
queryParams.push(endDate);
|
|
1439
|
+
}
|
|
1440
|
+
const metadataConditions = [];
|
|
1441
|
+
addSqliteMetadataFilter(metadataConditions, queryParams, metadataFilter);
|
|
1442
|
+
if (metadataConditions.length > 0) query += ` AND ${metadataConditions.join(" AND ")}`;
|
|
1443
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
1444
|
+
query += ` ORDER BY "${field}" ${direction}`;
|
|
1445
|
+
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
1446
|
+
query += ` LIMIT ? OFFSET ?`;
|
|
1447
|
+
queryParams.push(perPage, offset);
|
|
1448
|
+
}
|
|
1449
|
+
const results = await this.#db.executeQuery({
|
|
1450
|
+
sql: query,
|
|
1451
|
+
params: queryParams
|
|
1452
|
+
});
|
|
1453
|
+
const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
|
|
1454
|
+
const processedMsg = {};
|
|
1455
|
+
for (const [key, value] of Object.entries(message)) {
|
|
1456
|
+
if (key === `type` && value === `v2`) continue;
|
|
1457
|
+
processedMsg[key] = deserializeValue(value);
|
|
1458
|
+
}
|
|
1459
|
+
return processedMsg;
|
|
1460
|
+
});
|
|
1461
|
+
const paginatedCount = paginatedMessages.length;
|
|
1462
|
+
let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
|
|
1463
|
+
const countParams = [threadId];
|
|
1464
|
+
if (resourceId) {
|
|
1465
|
+
countQuery += ` AND resourceId = ?`;
|
|
1466
|
+
countParams.push(resourceId);
|
|
1467
|
+
}
|
|
1468
|
+
if (dateRange?.start) {
|
|
1469
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
1470
|
+
const startOp = dateRange.startExclusive ? ">" : ">=";
|
|
1471
|
+
countQuery += ` AND createdAt ${startOp} ?`;
|
|
1472
|
+
countParams.push(startDate);
|
|
1473
|
+
}
|
|
1474
|
+
if (dateRange?.end) {
|
|
1475
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
1476
|
+
const endOp = dateRange.endExclusive ? "<" : "<=";
|
|
1477
|
+
countQuery += ` AND createdAt ${endOp} ?`;
|
|
1478
|
+
countParams.push(endDate);
|
|
1479
|
+
}
|
|
1480
|
+
const countMetadataConditions = [];
|
|
1481
|
+
addSqliteMetadataFilter(countMetadataConditions, countParams, metadataFilter);
|
|
1482
|
+
if (countMetadataConditions.length > 0) countQuery += ` AND ${countMetadataConditions.join(" AND ")}`;
|
|
1483
|
+
const countResult = await this.#db.executeQuery({
|
|
1484
|
+
sql: countQuery,
|
|
1485
|
+
params: countParams
|
|
1486
|
+
});
|
|
1487
|
+
const total = Number(countResult[0]?.count ?? 0);
|
|
1488
|
+
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) return {
|
|
1489
|
+
messages: [],
|
|
1490
|
+
total: 0,
|
|
1491
|
+
page,
|
|
1492
|
+
perPage: perPageForResponse,
|
|
1493
|
+
hasMore: false
|
|
1494
|
+
};
|
|
1495
|
+
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
1496
|
+
let includeMessages = [];
|
|
1497
|
+
if (include && include.length > 0) {
|
|
1498
|
+
const includeResult = await this._getIncludedMessages(include);
|
|
1499
|
+
if (Array.isArray(includeResult)) {
|
|
1500
|
+
includeMessages = includeResult;
|
|
1501
|
+
for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
|
|
1502
|
+
paginatedMessages.push(includeMsg);
|
|
1503
|
+
messageIds.add(includeMsg.id);
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
let finalMessages = new MessageList().add(paginatedMessages, "memory").get.all.db();
|
|
1508
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
1509
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
1510
|
+
const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
1511
|
+
const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
1512
|
+
if (aValue === bValue) return a.id.localeCompare(b.id);
|
|
1513
|
+
if (typeof aValue === "number" && typeof bValue === "number") return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
1514
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
1515
|
+
});
|
|
1516
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
1517
|
+
const hasMore = perPageInput !== false && (metadataFilter || returnedThreadMessageIds.size < total) && offset + paginatedCount < total;
|
|
1518
|
+
return {
|
|
1519
|
+
messages: finalMessages,
|
|
1520
|
+
total,
|
|
1521
|
+
page,
|
|
1522
|
+
perPage: perPageForResponse,
|
|
1523
|
+
hasMore
|
|
1524
|
+
};
|
|
1525
|
+
} catch (error) {
|
|
1526
|
+
const mastraError = new MastraError({
|
|
1527
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_MESSAGES", "FAILED"),
|
|
1528
|
+
domain: ErrorDomain.STORAGE,
|
|
1529
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1530
|
+
text: `Failed to list messages for thread ${Array.isArray(threadId) ? threadId.join(",") : threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1531
|
+
details: {
|
|
1532
|
+
threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
|
|
1533
|
+
resourceId: resourceId ?? ""
|
|
1534
|
+
}
|
|
1535
|
+
}, error);
|
|
1536
|
+
this.logger?.error?.(mastraError.toString());
|
|
1537
|
+
this.logger?.trackException?.(mastraError);
|
|
1538
|
+
return {
|
|
1539
|
+
messages: [],
|
|
1540
|
+
total: 0,
|
|
1541
|
+
page,
|
|
1542
|
+
perPage: perPageForResponse,
|
|
1543
|
+
hasMore: false
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
async updateMessages(args) {
|
|
1548
|
+
const { messages } = args;
|
|
1549
|
+
this.logger.debug("Updating messages", { count: messages.length });
|
|
1550
|
+
if (!messages.length) return [];
|
|
1551
|
+
const messageIds = messages.map((m) => m.id);
|
|
1552
|
+
const fullTableName = this.#db.getTableName(TABLE_MESSAGES);
|
|
1553
|
+
const threadsTableName = this.#db.getTableName(TABLE_THREADS);
|
|
1554
|
+
try {
|
|
1555
|
+
const selectQuery = `SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId FROM ${fullTableName} WHERE id IN (${messageIds.map(() => "?").join(",")})`;
|
|
1556
|
+
const existingMessages = await this.#db.executeQuery({
|
|
1557
|
+
sql: selectQuery,
|
|
1558
|
+
params: messageIds
|
|
1559
|
+
});
|
|
1560
|
+
if (existingMessages.length === 0) return [];
|
|
1561
|
+
const parsedExistingMessages = existingMessages.map((msg) => {
|
|
1562
|
+
let parsedContent = msg.content;
|
|
1563
|
+
if (typeof msg.content === "string") try {
|
|
1564
|
+
parsedContent = JSON.parse(msg.content);
|
|
1565
|
+
} catch {}
|
|
1566
|
+
return {
|
|
1567
|
+
...msg,
|
|
1568
|
+
content: parsedContent
|
|
1569
|
+
};
|
|
1570
|
+
});
|
|
1571
|
+
const existingMessagesMap = new Map(parsedExistingMessages.map((msg) => [msg.id, msg]));
|
|
1572
|
+
const updatedMessages = [];
|
|
1573
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1574
|
+
for (const update of messages) {
|
|
1575
|
+
const existing = existingMessagesMap.get(update.id);
|
|
1576
|
+
if (!existing) continue;
|
|
1577
|
+
let mergedContent = existing.content;
|
|
1578
|
+
if (update.content) if (typeof mergedContent === "object" && mergedContent !== null) mergedContent = {
|
|
1579
|
+
...mergedContent,
|
|
1580
|
+
...update.content,
|
|
1581
|
+
metadata: {
|
|
1582
|
+
...mergedContent.metadata,
|
|
1583
|
+
...update.content.metadata
|
|
1584
|
+
}
|
|
1585
|
+
};
|
|
1586
|
+
else mergedContent = update.content;
|
|
1587
|
+
updatedMessages.push({
|
|
1588
|
+
...existing,
|
|
1589
|
+
...update,
|
|
1590
|
+
content: mergedContent
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
for (const msg of updatedMessages) {
|
|
1594
|
+
const contentStr = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
|
|
1595
|
+
const { sql, params } = createSqlBuilder().update(fullTableName, [
|
|
1596
|
+
"content",
|
|
1597
|
+
"role",
|
|
1598
|
+
"type"
|
|
1599
|
+
], [
|
|
1600
|
+
contentStr,
|
|
1601
|
+
msg.role,
|
|
1602
|
+
msg.type
|
|
1603
|
+
]).where("id = ?", msg.id).build();
|
|
1604
|
+
await this.#db.executeQuery({
|
|
1605
|
+
sql,
|
|
1606
|
+
params
|
|
1607
|
+
});
|
|
1608
|
+
}
|
|
1609
|
+
const threadIds = [...new Set(updatedMessages.map((m) => m.threadId))];
|
|
1610
|
+
for (const tid of threadIds) await this.#db.executeQuery({
|
|
1611
|
+
sql: `UPDATE ${threadsTableName} SET updatedAt = ? WHERE id = ?`,
|
|
1612
|
+
params: [now, tid]
|
|
1613
|
+
});
|
|
1614
|
+
return new MessageList().add(updatedMessages, "memory").get.all.db();
|
|
1615
|
+
} catch (error) {
|
|
1616
|
+
throw new MastraError({
|
|
1617
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "UPDATE_MESSAGES", "FAILED"),
|
|
1618
|
+
domain: ErrorDomain.STORAGE,
|
|
1619
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1620
|
+
text: `Failed to update messages: ${error instanceof Error ? error.message : String(error)}`,
|
|
1621
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
1622
|
+
}, error);
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
async deleteMessages(messageIds) {
|
|
1626
|
+
if (messageIds.length === 0) return;
|
|
1627
|
+
const fullTableName = this.#db.getTableName(TABLE_MESSAGES);
|
|
1628
|
+
try {
|
|
1629
|
+
const sql = `DELETE FROM ${fullTableName} WHERE id IN (${messageIds.map(() => "?").join(",")})`;
|
|
1630
|
+
await this.#db.executeQuery({
|
|
1631
|
+
sql,
|
|
1632
|
+
params: messageIds
|
|
1633
|
+
});
|
|
1634
|
+
this.logger.debug(`Deleted ${messageIds.length} messages`);
|
|
1635
|
+
} catch (error) {
|
|
1636
|
+
throw new MastraError({
|
|
1637
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "DELETE_MESSAGES", "FAILED"),
|
|
1638
|
+
domain: ErrorDomain.STORAGE,
|
|
1639
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1640
|
+
text: `Failed to delete messages: ${error instanceof Error ? error.message : String(error)}`,
|
|
1641
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
1642
|
+
}, error);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
};
|
|
1646
|
+
//#endregion
|
|
1647
|
+
//#region src/do/storage/domains/scores/index.ts
|
|
1648
|
+
/** Appends multi-tenant scope conditions to a SQL builder query (no-op when no filters). */
|
|
1649
|
+
function applyTenancyFilters(query, filters) {
|
|
1650
|
+
if (filters?.organizationId !== void 0) query.andWhere("organizationId = ?", filters.organizationId);
|
|
1651
|
+
if (filters?.projectId !== void 0) query.andWhere("projectId = ?", filters.projectId);
|
|
1652
|
+
}
|
|
1653
|
+
/**
|
|
1654
|
+
* Durable Objects-specific score row transformation.
|
|
1655
|
+
* Uses Z-suffix timestamps (createdAtZ, updatedAtZ) when available.
|
|
1656
|
+
*/
|
|
1657
|
+
function transformScoreRow$1(row) {
|
|
1658
|
+
return transformScoreRow(row, { preferredTimestampFields: {
|
|
1659
|
+
createdAt: "createdAtZ",
|
|
1660
|
+
updatedAt: "updatedAtZ"
|
|
1661
|
+
} });
|
|
1662
|
+
}
|
|
1663
|
+
var ScoresStorageDO = class extends ScoresStorage {
|
|
1664
|
+
#db;
|
|
1665
|
+
constructor(config) {
|
|
1666
|
+
super();
|
|
1667
|
+
this.#db = new DODB(config);
|
|
1668
|
+
}
|
|
1669
|
+
async init() {
|
|
1670
|
+
await this.#db.createTable({
|
|
1671
|
+
tableName: TABLE_SCORERS,
|
|
1672
|
+
schema: TABLE_SCHEMAS[TABLE_SCORERS]
|
|
1673
|
+
});
|
|
1674
|
+
}
|
|
1675
|
+
async dangerouslyClearAll() {
|
|
1676
|
+
await this.#db.clearTable({ tableName: TABLE_SCORERS });
|
|
1677
|
+
}
|
|
1678
|
+
async getScoreById({ id }) {
|
|
1679
|
+
try {
|
|
1680
|
+
const fullTableName = this.#db.getTableName(TABLE_SCORERS);
|
|
1681
|
+
const { sql, params } = createSqlBuilder().select("*").from(fullTableName).where("id = ?", id).build();
|
|
1682
|
+
const result = await this.#db.executeQuery({
|
|
1683
|
+
sql,
|
|
1684
|
+
params,
|
|
1685
|
+
first: true
|
|
1686
|
+
});
|
|
1687
|
+
if (!result) return null;
|
|
1688
|
+
return transformScoreRow$1(result);
|
|
1689
|
+
} catch (error) {
|
|
1690
|
+
throw new MastraError({
|
|
1691
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_SCORE_BY_ID", "FAILED"),
|
|
1692
|
+
domain: ErrorDomain.STORAGE,
|
|
1693
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1694
|
+
}, error);
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
async saveScore(score) {
|
|
1698
|
+
let parsedScore;
|
|
1699
|
+
try {
|
|
1700
|
+
parsedScore = saveScorePayloadSchema.parse(score);
|
|
1701
|
+
} catch (error) {
|
|
1702
|
+
const safeScore = score && typeof score === "object" ? score : {};
|
|
1703
|
+
const safeScorer = safeScore.scorer && typeof safeScore.scorer === "object" ? safeScore.scorer : {};
|
|
1704
|
+
throw new MastraError({
|
|
1705
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "SAVE_SCORE", "VALIDATION_FAILED"),
|
|
1706
|
+
domain: ErrorDomain.STORAGE,
|
|
1707
|
+
category: ErrorCategory.USER,
|
|
1708
|
+
details: {
|
|
1709
|
+
scorer: typeof safeScorer.id === "string" ? safeScorer.id : String(safeScorer.id ?? "unknown"),
|
|
1710
|
+
entityId: safeScore.entityId ?? "unknown",
|
|
1711
|
+
entityType: safeScore.entityType ?? "unknown",
|
|
1712
|
+
traceId: safeScore.traceId ?? "",
|
|
1713
|
+
spanId: safeScore.spanId ?? ""
|
|
1714
|
+
}
|
|
1715
|
+
}, error);
|
|
1716
|
+
}
|
|
1717
|
+
const id = crypto.randomUUID();
|
|
1718
|
+
try {
|
|
1719
|
+
const fullTableName = this.#db.getTableName(TABLE_SCORERS);
|
|
1720
|
+
const serializedRecord = {};
|
|
1721
|
+
for (const [key, value] of Object.entries(parsedScore)) if (value !== null && value !== void 0) if (typeof value === "object") serializedRecord[key] = JSON.stringify(value);
|
|
1722
|
+
else serializedRecord[key] = value;
|
|
1723
|
+
else serializedRecord[key] = null;
|
|
1724
|
+
const now = /* @__PURE__ */ new Date();
|
|
1725
|
+
serializedRecord.id = id;
|
|
1726
|
+
serializedRecord.createdAt = now.toISOString();
|
|
1727
|
+
serializedRecord.updatedAt = now.toISOString();
|
|
1728
|
+
const columns = Object.keys(serializedRecord);
|
|
1729
|
+
const values = Object.values(serializedRecord);
|
|
1730
|
+
const { sql, params } = createSqlBuilder().insert(fullTableName, columns, values).build();
|
|
1731
|
+
await this.#db.executeQuery({
|
|
1732
|
+
sql,
|
|
1733
|
+
params
|
|
1734
|
+
});
|
|
1735
|
+
return { score: {
|
|
1736
|
+
...parsedScore,
|
|
1737
|
+
id,
|
|
1738
|
+
createdAt: now,
|
|
1739
|
+
updatedAt: now
|
|
1740
|
+
} };
|
|
1741
|
+
} catch (error) {
|
|
1742
|
+
throw new MastraError({
|
|
1743
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "SAVE_SCORE", "FAILED"),
|
|
1744
|
+
domain: ErrorDomain.STORAGE,
|
|
1745
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1746
|
+
details: { id }
|
|
1747
|
+
}, error);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
async listScoresByScorerId({ scorerId, entityId, entityType, source, pagination, filters }) {
|
|
1751
|
+
try {
|
|
1752
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1753
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1754
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1755
|
+
const fullTableName = this.#db.getTableName(TABLE_SCORERS);
|
|
1756
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1757
|
+
if (entityId) countQuery.andWhere("entityId = ?", entityId);
|
|
1758
|
+
if (entityType) countQuery.andWhere("entityType = ?", entityType);
|
|
1759
|
+
if (source) countQuery.andWhere("source = ?", source);
|
|
1760
|
+
applyTenancyFilters(countQuery, filters);
|
|
1761
|
+
const countResult = await this.#db.executeQuery(countQuery.build());
|
|
1762
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1763
|
+
if (total === 0) return {
|
|
1764
|
+
pagination: {
|
|
1765
|
+
total: 0,
|
|
1766
|
+
page,
|
|
1767
|
+
perPage: perPageForResponse,
|
|
1768
|
+
hasMore: false
|
|
1769
|
+
},
|
|
1770
|
+
scores: []
|
|
1771
|
+
};
|
|
1772
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1773
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1774
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
|
|
1775
|
+
if (entityId) selectQuery.andWhere("entityId = ?", entityId);
|
|
1776
|
+
if (entityType) selectQuery.andWhere("entityType = ?", entityType);
|
|
1777
|
+
if (source) selectQuery.andWhere("source = ?", source);
|
|
1778
|
+
applyTenancyFilters(selectQuery, filters);
|
|
1779
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1780
|
+
const { sql, params } = selectQuery.build();
|
|
1781
|
+
const results = await this.#db.executeQuery({
|
|
1782
|
+
sql,
|
|
1783
|
+
params
|
|
1784
|
+
});
|
|
1785
|
+
const scores = Array.isArray(results) ? results.map((r) => transformScoreRow$1(r)) : [];
|
|
1786
|
+
return {
|
|
1787
|
+
pagination: {
|
|
1788
|
+
total,
|
|
1789
|
+
page,
|
|
1790
|
+
perPage: perPageForResponse,
|
|
1791
|
+
hasMore: end < total
|
|
1792
|
+
},
|
|
1793
|
+
scores
|
|
1794
|
+
};
|
|
1795
|
+
} catch (error) {
|
|
1796
|
+
throw new MastraError({
|
|
1797
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_SCORES_BY_SCORER_ID", "FAILED"),
|
|
1798
|
+
domain: ErrorDomain.STORAGE,
|
|
1799
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1800
|
+
}, error);
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
async listScoresByRunId({ runId, pagination, filters }) {
|
|
1804
|
+
try {
|
|
1805
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1806
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1807
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1808
|
+
const fullTableName = this.#db.getTableName(TABLE_SCORERS);
|
|
1809
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1810
|
+
applyTenancyFilters(countQuery, filters);
|
|
1811
|
+
const countResult = await this.#db.executeQuery(countQuery.build());
|
|
1812
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1813
|
+
if (total === 0) return {
|
|
1814
|
+
pagination: {
|
|
1815
|
+
total: 0,
|
|
1816
|
+
page,
|
|
1817
|
+
perPage: perPageForResponse,
|
|
1818
|
+
hasMore: false
|
|
1819
|
+
},
|
|
1820
|
+
scores: []
|
|
1821
|
+
};
|
|
1822
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1823
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1824
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId);
|
|
1825
|
+
applyTenancyFilters(selectQuery, filters);
|
|
1826
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1827
|
+
const { sql, params } = selectQuery.build();
|
|
1828
|
+
const results = await this.#db.executeQuery({
|
|
1829
|
+
sql,
|
|
1830
|
+
params
|
|
1831
|
+
});
|
|
1832
|
+
const scores = Array.isArray(results) ? results.map((r) => transformScoreRow$1(r)) : [];
|
|
1833
|
+
return {
|
|
1834
|
+
pagination: {
|
|
1835
|
+
total,
|
|
1836
|
+
page,
|
|
1837
|
+
perPage: perPageForResponse,
|
|
1838
|
+
hasMore: end < total
|
|
1839
|
+
},
|
|
1840
|
+
scores
|
|
1841
|
+
};
|
|
1842
|
+
} catch (error) {
|
|
1843
|
+
throw new MastraError({
|
|
1844
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_SCORES_BY_RUN_ID", "FAILED"),
|
|
1845
|
+
domain: ErrorDomain.STORAGE,
|
|
1846
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1847
|
+
}, error);
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
async listScoresByEntityId({ entityId, entityType, pagination, filters }) {
|
|
1851
|
+
try {
|
|
1852
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1853
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1854
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1855
|
+
const fullTableName = this.#db.getTableName(TABLE_SCORERS);
|
|
1856
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1857
|
+
applyTenancyFilters(countQuery, filters);
|
|
1858
|
+
const countResult = await this.#db.executeQuery(countQuery.build());
|
|
1859
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1860
|
+
if (total === 0) return {
|
|
1861
|
+
pagination: {
|
|
1862
|
+
total: 0,
|
|
1863
|
+
page,
|
|
1864
|
+
perPage: perPageForResponse,
|
|
1865
|
+
hasMore: false
|
|
1866
|
+
},
|
|
1867
|
+
scores: []
|
|
1868
|
+
};
|
|
1869
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1870
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1871
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1872
|
+
applyTenancyFilters(selectQuery, filters);
|
|
1873
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1874
|
+
const { sql, params } = selectQuery.build();
|
|
1875
|
+
const results = await this.#db.executeQuery({
|
|
1876
|
+
sql,
|
|
1877
|
+
params
|
|
1878
|
+
});
|
|
1879
|
+
const scores = Array.isArray(results) ? results.map((r) => transformScoreRow$1(r)) : [];
|
|
1880
|
+
return {
|
|
1881
|
+
pagination: {
|
|
1882
|
+
total,
|
|
1883
|
+
page,
|
|
1884
|
+
perPage: perPageForResponse,
|
|
1885
|
+
hasMore: end < total
|
|
1886
|
+
},
|
|
1887
|
+
scores
|
|
1888
|
+
};
|
|
1889
|
+
} catch (error) {
|
|
1890
|
+
throw new MastraError({
|
|
1891
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_SCORES_BY_ENTITY_ID", "FAILED"),
|
|
1892
|
+
domain: ErrorDomain.STORAGE,
|
|
1893
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1894
|
+
}, error);
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
async listScoresBySpan({ traceId, spanId, pagination, filters }) {
|
|
1898
|
+
try {
|
|
1899
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1900
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1901
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1902
|
+
const fullTableName = this.#db.getTableName(TABLE_SCORERS);
|
|
1903
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1904
|
+
applyTenancyFilters(countQuery, filters);
|
|
1905
|
+
const countResult = await this.#db.executeQuery(countQuery.build());
|
|
1906
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1907
|
+
if (total === 0) return {
|
|
1908
|
+
pagination: {
|
|
1909
|
+
total: 0,
|
|
1910
|
+
page,
|
|
1911
|
+
perPage: perPageForResponse,
|
|
1912
|
+
hasMore: false
|
|
1913
|
+
},
|
|
1914
|
+
scores: []
|
|
1915
|
+
};
|
|
1916
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1917
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1918
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1919
|
+
applyTenancyFilters(selectQuery, filters);
|
|
1920
|
+
selectQuery.orderBy("createdAt", "DESC").limit(limitValue).offset(start);
|
|
1921
|
+
const { sql, params } = selectQuery.build();
|
|
1922
|
+
const results = await this.#db.executeQuery({
|
|
1923
|
+
sql,
|
|
1924
|
+
params
|
|
1925
|
+
});
|
|
1926
|
+
const scores = Array.isArray(results) ? results.map((r) => transformScoreRow$1(r)) : [];
|
|
1927
|
+
return {
|
|
1928
|
+
pagination: {
|
|
1929
|
+
total,
|
|
1930
|
+
page,
|
|
1931
|
+
perPage: perPageForResponse,
|
|
1932
|
+
hasMore: end < total
|
|
1933
|
+
},
|
|
1934
|
+
scores
|
|
1935
|
+
};
|
|
1936
|
+
} catch (error) {
|
|
1937
|
+
throw new MastraError({
|
|
1938
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_SCORES_BY_SPAN", "FAILED"),
|
|
1939
|
+
domain: ErrorDomain.STORAGE,
|
|
1940
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1941
|
+
}, error);
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
};
|
|
1945
|
+
//#endregion
|
|
1946
|
+
//#region src/do/storage/domains/workflows/index.ts
|
|
1947
|
+
var WorkflowsStorageDO = class extends WorkflowsStorage {
|
|
1948
|
+
#db;
|
|
1949
|
+
constructor(config) {
|
|
1950
|
+
super();
|
|
1951
|
+
this.#db = new DODB(config);
|
|
1952
|
+
}
|
|
1953
|
+
supportsConcurrentUpdates() {
|
|
1954
|
+
return false;
|
|
1955
|
+
}
|
|
1956
|
+
async init() {
|
|
1957
|
+
await this.#db.createTable({
|
|
1958
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
1959
|
+
schema: TABLE_SCHEMAS[TABLE_WORKFLOW_SNAPSHOT]
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
async dangerouslyClearAll() {
|
|
1963
|
+
await this.#db.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT });
|
|
1964
|
+
}
|
|
1965
|
+
updateWorkflowResults({}) {
|
|
1966
|
+
throw new Error("Method not implemented.");
|
|
1967
|
+
}
|
|
1968
|
+
updateWorkflowState({}) {
|
|
1969
|
+
throw new Error("Method not implemented.");
|
|
1970
|
+
}
|
|
1971
|
+
async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt }) {
|
|
1972
|
+
const fullTableName = this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1973
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1974
|
+
const currentSnapshot = await this.#db.load({
|
|
1975
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
1976
|
+
keys: {
|
|
1977
|
+
workflow_name: workflowName,
|
|
1978
|
+
run_id: runId
|
|
1979
|
+
}
|
|
1980
|
+
});
|
|
1981
|
+
const persisting = currentSnapshot ? {
|
|
1982
|
+
...currentSnapshot,
|
|
1983
|
+
resourceId,
|
|
1984
|
+
snapshot: JSON.stringify(snapshot),
|
|
1985
|
+
updatedAt: updatedAt ? updatedAt.toISOString() : now
|
|
1986
|
+
} : {
|
|
1987
|
+
workflow_name: workflowName,
|
|
1988
|
+
run_id: runId,
|
|
1989
|
+
resourceId,
|
|
1990
|
+
snapshot: JSON.stringify(snapshot),
|
|
1991
|
+
createdAt: createdAt ? createdAt.toISOString() : now,
|
|
1992
|
+
updatedAt: updatedAt ? updatedAt.toISOString() : now
|
|
1993
|
+
};
|
|
1994
|
+
const processedRecord = await this.#db.processRecord(persisting);
|
|
1995
|
+
const columns = Object.keys(processedRecord);
|
|
1996
|
+
const values = Object.values(processedRecord);
|
|
1997
|
+
const updateMap = {
|
|
1998
|
+
snapshot: "excluded.snapshot",
|
|
1999
|
+
updatedAt: "excluded.updatedAt",
|
|
2000
|
+
resourceId: `COALESCE(excluded.resourceId, ${fullTableName}.resourceId)`
|
|
2001
|
+
};
|
|
2002
|
+
this.logger.debug("Persisting workflow snapshot", {
|
|
2003
|
+
workflowName,
|
|
2004
|
+
runId
|
|
2005
|
+
});
|
|
2006
|
+
const { sql, params } = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap).build();
|
|
2007
|
+
try {
|
|
2008
|
+
await this.#db.executeQuery({
|
|
2009
|
+
sql,
|
|
2010
|
+
params
|
|
2011
|
+
});
|
|
2012
|
+
} catch (error) {
|
|
2013
|
+
throw new MastraError({
|
|
2014
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
|
|
2015
|
+
domain: ErrorDomain.STORAGE,
|
|
2016
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
2017
|
+
text: `Failed to persist workflow snapshot: ${error instanceof Error ? error.message : String(error)}`,
|
|
2018
|
+
details: {
|
|
2019
|
+
workflowName,
|
|
2020
|
+
runId
|
|
2021
|
+
}
|
|
2022
|
+
}, error);
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
async loadWorkflowSnapshot(params) {
|
|
2026
|
+
const { workflowName, runId } = params;
|
|
2027
|
+
this.logger.debug("Loading workflow snapshot", {
|
|
2028
|
+
workflowName,
|
|
2029
|
+
runId
|
|
2030
|
+
});
|
|
2031
|
+
try {
|
|
2032
|
+
const d = await this.#db.load({
|
|
2033
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
2034
|
+
keys: {
|
|
2035
|
+
workflow_name: workflowName,
|
|
2036
|
+
run_id: runId
|
|
2037
|
+
}
|
|
2038
|
+
});
|
|
2039
|
+
return d ? d.snapshot : null;
|
|
2040
|
+
} catch (error) {
|
|
2041
|
+
throw new MastraError({
|
|
2042
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
|
|
2043
|
+
domain: ErrorDomain.STORAGE,
|
|
2044
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
2045
|
+
text: `Failed to load workflow snapshot: ${error instanceof Error ? error.message : String(error)}`,
|
|
2046
|
+
details: {
|
|
2047
|
+
workflowName,
|
|
2048
|
+
runId
|
|
2049
|
+
}
|
|
2050
|
+
}, error);
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
parseWorkflowRun(row) {
|
|
2054
|
+
let parsedSnapshot = row.snapshot;
|
|
2055
|
+
if (typeof parsedSnapshot === "string") try {
|
|
2056
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
2057
|
+
} catch (e) {
|
|
2058
|
+
this.logger.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
2059
|
+
}
|
|
2060
|
+
return {
|
|
2061
|
+
workflowName: row.workflow_name,
|
|
2062
|
+
runId: row.run_id,
|
|
2063
|
+
snapshot: parsedSnapshot,
|
|
2064
|
+
createdAt: ensureDate(row.createdAt),
|
|
2065
|
+
updatedAt: ensureDate(row.updatedAt),
|
|
2066
|
+
resourceId: row.resourceId
|
|
2067
|
+
};
|
|
2068
|
+
}
|
|
2069
|
+
async listWorkflowRuns({ workflowName, fromDate, toDate, page, perPage, resourceId, status } = {}) {
|
|
2070
|
+
const fullTableName = this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
2071
|
+
try {
|
|
2072
|
+
const builder = createSqlBuilder().select().from(fullTableName);
|
|
2073
|
+
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
2074
|
+
if (workflowName) {
|
|
2075
|
+
builder.whereAnd("workflow_name = ?", workflowName);
|
|
2076
|
+
countBuilder.whereAnd("workflow_name = ?", workflowName);
|
|
2077
|
+
}
|
|
2078
|
+
if (status) {
|
|
2079
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
2080
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
2081
|
+
}
|
|
2082
|
+
if (resourceId) if (await this.#db.hasColumn(fullTableName, "resourceId")) {
|
|
2083
|
+
builder.whereAnd("resourceId = ?", resourceId);
|
|
2084
|
+
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
2085
|
+
} else this.logger.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
|
|
2086
|
+
if (fromDate) {
|
|
2087
|
+
builder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
2088
|
+
countBuilder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
2089
|
+
}
|
|
2090
|
+
if (toDate) {
|
|
2091
|
+
builder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2092
|
+
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2093
|
+
}
|
|
2094
|
+
builder.orderBy("createdAt", "DESC");
|
|
2095
|
+
if (typeof perPage === "number" && typeof page === "number") {
|
|
2096
|
+
const offset = page * perPage;
|
|
2097
|
+
builder.limit(perPage);
|
|
2098
|
+
builder.offset(offset);
|
|
2099
|
+
}
|
|
2100
|
+
const { sql, params } = builder.build();
|
|
2101
|
+
let total = 0;
|
|
2102
|
+
if (perPage !== void 0 && page !== void 0) {
|
|
2103
|
+
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
2104
|
+
const countResult = await this.#db.executeQuery({
|
|
2105
|
+
sql: countSql,
|
|
2106
|
+
params: countParams,
|
|
2107
|
+
first: true
|
|
2108
|
+
});
|
|
2109
|
+
total = Number(countResult?.count ?? 0);
|
|
2110
|
+
}
|
|
2111
|
+
const results = await this.#db.executeQuery({
|
|
2112
|
+
sql,
|
|
2113
|
+
params
|
|
2114
|
+
});
|
|
2115
|
+
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
2116
|
+
return {
|
|
2117
|
+
runs,
|
|
2118
|
+
total: total || runs.length
|
|
2119
|
+
};
|
|
2120
|
+
} catch (error) {
|
|
2121
|
+
throw new MastraError({
|
|
2122
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "LIST_WORKFLOW_RUNS", "FAILED"),
|
|
2123
|
+
domain: ErrorDomain.STORAGE,
|
|
2124
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
2125
|
+
text: `Failed to retrieve workflow runs: ${error instanceof Error ? error.message : String(error)}`,
|
|
2126
|
+
details: {
|
|
2127
|
+
workflowName: workflowName ?? "",
|
|
2128
|
+
resourceId: resourceId ?? ""
|
|
2129
|
+
}
|
|
2130
|
+
}, error);
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
async getWorkflowRunById({ runId, workflowName }) {
|
|
2134
|
+
const fullTableName = this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
2135
|
+
try {
|
|
2136
|
+
const conditions = [];
|
|
2137
|
+
const params = [];
|
|
2138
|
+
if (runId) {
|
|
2139
|
+
conditions.push("run_id = ?");
|
|
2140
|
+
params.push(runId);
|
|
2141
|
+
}
|
|
2142
|
+
if (workflowName) {
|
|
2143
|
+
conditions.push("workflow_name = ?");
|
|
2144
|
+
params.push(workflowName);
|
|
2145
|
+
}
|
|
2146
|
+
const sql = `SELECT * FROM ${fullTableName} ${conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : ""} ORDER BY createdAt DESC LIMIT 1`;
|
|
2147
|
+
const result = await this.#db.executeQuery({
|
|
2148
|
+
sql,
|
|
2149
|
+
params,
|
|
2150
|
+
first: true
|
|
2151
|
+
});
|
|
2152
|
+
if (!result) return null;
|
|
2153
|
+
return this.parseWorkflowRun(result);
|
|
2154
|
+
} catch (error) {
|
|
2155
|
+
throw new MastraError({
|
|
2156
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
|
|
2157
|
+
domain: ErrorDomain.STORAGE,
|
|
2158
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
2159
|
+
text: `Failed to retrieve workflow run by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
2160
|
+
details: {
|
|
2161
|
+
runId,
|
|
2162
|
+
workflowName: workflowName ?? ""
|
|
2163
|
+
}
|
|
2164
|
+
}, error);
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
2168
|
+
const fullTableName = this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
2169
|
+
try {
|
|
2170
|
+
const sql = `DELETE FROM ${fullTableName} WHERE workflow_name = ? AND run_id = ?`;
|
|
2171
|
+
const params = [workflowName, runId];
|
|
2172
|
+
await this.#db.executeQuery({
|
|
2173
|
+
sql,
|
|
2174
|
+
params
|
|
2175
|
+
});
|
|
2176
|
+
} catch (error) {
|
|
2177
|
+
throw new MastraError({
|
|
2178
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
|
|
2179
|
+
domain: ErrorDomain.STORAGE,
|
|
2180
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
2181
|
+
text: `Failed to delete workflow run by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
2182
|
+
details: {
|
|
2183
|
+
runId,
|
|
2184
|
+
workflowName
|
|
2185
|
+
}
|
|
2186
|
+
}, error);
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
};
|
|
2190
|
+
//#endregion
|
|
2191
|
+
//#region src/do/index.ts
|
|
2192
|
+
/**
|
|
2193
|
+
* Cloudflare Durable Objects storage adapter for Mastra.
|
|
2194
|
+
*
|
|
2195
|
+
* Uses the synchronous SqlStorage API available in Durable Objects
|
|
2196
|
+
* to provide thread-based memory storage, workflow persistence, and scoring.
|
|
2197
|
+
*
|
|
2198
|
+
* Access domain-specific storage via `getStore()`:
|
|
2199
|
+
*
|
|
2200
|
+
* @example
|
|
2201
|
+
* ```typescript
|
|
2202
|
+
* import { DurableObject } from "cloudflare:workers";
|
|
2203
|
+
* import { CloudflareDOStorage } from "@mastra/cloudflare/do";
|
|
2204
|
+
*
|
|
2205
|
+
* class AgentDurableObject extends DurableObject<Env> {
|
|
2206
|
+
* private storage: CloudflareDOStorage;
|
|
2207
|
+
*
|
|
2208
|
+
* constructor(ctx: DurableObjectState, env: Env) {
|
|
2209
|
+
* super(ctx, env);
|
|
2210
|
+
* this.storage = new CloudflareDOStorage({
|
|
2211
|
+
* sql: ctx.storage.sql,
|
|
2212
|
+
* tablePrefix: 'mastra_'
|
|
2213
|
+
* });
|
|
2214
|
+
* }
|
|
2215
|
+
*
|
|
2216
|
+
* async run() {
|
|
2217
|
+
* const memory = await this.storage.getStore('memory');
|
|
2218
|
+
* await memory?.saveThread({ thread: { id: 'thread-1', ... } });
|
|
2219
|
+
* }
|
|
2220
|
+
* }
|
|
2221
|
+
* ```
|
|
2222
|
+
*/
|
|
2223
|
+
var CloudflareDOStorage = class extends MastraCompositeStore {
|
|
2224
|
+
stores;
|
|
2225
|
+
/**
|
|
2226
|
+
* Creates a new CloudflareDOStorage instance
|
|
2227
|
+
* @param config Configuration for Durable Objects SqlStorage access
|
|
2228
|
+
*/
|
|
2229
|
+
constructor(config) {
|
|
2230
|
+
try {
|
|
2231
|
+
super({
|
|
2232
|
+
id: "do-store",
|
|
2233
|
+
name: "DO",
|
|
2234
|
+
disableInit: config.disableInit
|
|
2235
|
+
});
|
|
2236
|
+
if (config.tablePrefix && !/^[A-Za-z_][A-Za-z0-9_]*$/.test(config.tablePrefix)) throw new Error("Invalid tablePrefix: must start with a letter or underscore and contain only letters, numbers, and underscores.");
|
|
2237
|
+
const domainConfig = {
|
|
2238
|
+
sql: config.sql,
|
|
2239
|
+
tablePrefix: config.tablePrefix
|
|
2240
|
+
};
|
|
2241
|
+
this.stores = {
|
|
2242
|
+
memory: new MemoryStorageDO(domainConfig),
|
|
2243
|
+
workflows: new WorkflowsStorageDO(domainConfig),
|
|
2244
|
+
scores: new ScoresStorageDO(domainConfig),
|
|
2245
|
+
backgroundTasks: new BackgroundTasksStorageDO(domainConfig)
|
|
2246
|
+
};
|
|
2247
|
+
this.logger.info("Using Durable Objects SqlStorage");
|
|
2248
|
+
} catch (error) {
|
|
2249
|
+
throw new MastraError({
|
|
2250
|
+
id: createStorageErrorId("CLOUDFLARE_DO", "INITIALIZATION", "FAILED"),
|
|
2251
|
+
domain: ErrorDomain.STORAGE,
|
|
2252
|
+
category: ErrorCategory.SYSTEM,
|
|
2253
|
+
text: "Error initializing CloudflareDOStorage"
|
|
2254
|
+
}, error);
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
/**
|
|
2258
|
+
* Close the database connection
|
|
2259
|
+
* No explicit cleanup needed for DO storage
|
|
2260
|
+
*/
|
|
2261
|
+
async close() {
|
|
2262
|
+
this.logger.debug("Closing DO connection");
|
|
2263
|
+
}
|
|
2264
|
+
};
|
|
2265
|
+
/**
|
|
2266
|
+
* @deprecated Use CloudflareDOStorage instead
|
|
2267
|
+
*/
|
|
2268
|
+
const DOStore = CloudflareDOStorage;
|
|
2269
|
+
//#endregion
|
|
2270
|
+
export { MemoryStorageDO as a, ScoresStorageDO as i, DOStore as n, BackgroundTasksStorageDO as o, WorkflowsStorageDO as r, DODB as s, CloudflareDOStorage as t };
|
|
2271
|
+
|
|
2272
|
+
//# sourceMappingURL=do-BDZtdsky.js.map
|