@mastra/cloudflare-d1 0.0.0-remove-cloud-span-transform-20250425214156 → 0.0.0-roamin-openaivoice-speak-options-passing-20250926163614
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 +1136 -0
- package/LICENSE.md +11 -42
- package/README.md +15 -0
- package/dist/index.cjs +2078 -567
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2056 -545
- package/dist/index.js.map +1 -0
- package/dist/storage/domains/legacy-evals/index.d.ts +20 -0
- package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +90 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/operations/index.d.ts +72 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +52 -0
- package/dist/storage/domains/scores/index.d.ts.map +1 -0
- package/dist/storage/domains/traces/index.d.ts +18 -0
- package/dist/storage/domains/traces/index.d.ts.map +1 -0
- package/dist/storage/domains/utils.d.ts +3 -0
- package/dist/storage/domains/utils.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +52 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +286 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/sql-builder.d.ts +128 -0
- package/dist/storage/sql-builder.d.ts.map +1 -0
- package/dist/storage/test-utils.d.ts +19 -0
- package/dist/storage/test-utils.d.ts.map +1 -0
- package/package.json +32 -17
- package/dist/_tsup-dts-rollup.d.cts +0 -313
- package/dist/_tsup-dts-rollup.d.ts +0 -313
- package/dist/index.d.cts +0 -5
package/dist/index.cjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var error = require('@mastra/core/error');
|
|
3
4
|
var storage = require('@mastra/core/storage');
|
|
4
5
|
var Cloudflare = require('cloudflare');
|
|
6
|
+
var utils = require('@mastra/core/utils');
|
|
7
|
+
var agent = require('@mastra/core/agent');
|
|
5
8
|
|
|
6
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
10
|
|
|
8
11
|
var Cloudflare__default = /*#__PURE__*/_interopDefault(Cloudflare);
|
|
9
12
|
|
|
10
13
|
// src/storage/index.ts
|
|
11
|
-
|
|
12
|
-
// src/storage/sql-builder.ts
|
|
13
14
|
var SqlBuilder = class {
|
|
14
15
|
sql = "";
|
|
15
16
|
params = [];
|
|
@@ -19,12 +20,15 @@ var SqlBuilder = class {
|
|
|
19
20
|
if (!columns || Array.isArray(columns) && columns.length === 0) {
|
|
20
21
|
this.sql = "SELECT *";
|
|
21
22
|
} else {
|
|
22
|
-
|
|
23
|
+
const cols = Array.isArray(columns) ? columns : [columns];
|
|
24
|
+
const parsedCols = cols.map((col) => parseSelectIdentifier(col));
|
|
25
|
+
this.sql = `SELECT ${parsedCols.join(", ")}`;
|
|
23
26
|
}
|
|
24
27
|
return this;
|
|
25
28
|
}
|
|
26
29
|
from(table) {
|
|
27
|
-
|
|
30
|
+
const parsedTableName = utils.parseSqlIdentifier(table, "table name");
|
|
31
|
+
this.sql += ` FROM ${parsedTableName}`;
|
|
28
32
|
return this;
|
|
29
33
|
}
|
|
30
34
|
/**
|
|
@@ -61,7 +65,11 @@ var SqlBuilder = class {
|
|
|
61
65
|
return this;
|
|
62
66
|
}
|
|
63
67
|
orderBy(column, direction = "ASC") {
|
|
64
|
-
|
|
68
|
+
const parsedColumn = utils.parseSqlIdentifier(column, "column name");
|
|
69
|
+
if (!["ASC", "DESC"].includes(direction)) {
|
|
70
|
+
throw new Error(`Invalid sort direction: ${direction}`);
|
|
71
|
+
}
|
|
72
|
+
this.sql += ` ORDER BY ${parsedColumn} ${direction}`;
|
|
65
73
|
return this;
|
|
66
74
|
}
|
|
67
75
|
limit(count) {
|
|
@@ -74,6 +82,10 @@ var SqlBuilder = class {
|
|
|
74
82
|
this.params.push(count);
|
|
75
83
|
return this;
|
|
76
84
|
}
|
|
85
|
+
count() {
|
|
86
|
+
this.sql += "SELECT COUNT(*) AS count";
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
77
89
|
/**
|
|
78
90
|
* Insert a row, or update specific columns on conflict (upsert).
|
|
79
91
|
* @param table Table name
|
|
@@ -83,27 +95,33 @@ var SqlBuilder = class {
|
|
|
83
95
|
* @param updateMap Object mapping columns to update to their new value (e.g. { name: 'excluded.name' })
|
|
84
96
|
*/
|
|
85
97
|
insert(table, columns, values, conflictColumns, updateMap) {
|
|
86
|
-
const
|
|
98
|
+
const parsedTableName = utils.parseSqlIdentifier(table, "table name");
|
|
99
|
+
const parsedColumns = columns.map((col) => utils.parseSqlIdentifier(col, "column name"));
|
|
100
|
+
const placeholders = parsedColumns.map(() => "?").join(", ");
|
|
87
101
|
if (conflictColumns && updateMap) {
|
|
102
|
+
const parsedConflictColumns = conflictColumns.map((col) => utils.parseSqlIdentifier(col, "column name"));
|
|
88
103
|
const updateClause = Object.entries(updateMap).map(([col, expr]) => `${col} = ${expr}`).join(", ");
|
|
89
|
-
this.sql = `INSERT INTO ${
|
|
104
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${parsedConflictColumns.join(", ")}) DO UPDATE SET ${updateClause}`;
|
|
90
105
|
this.params.push(...values);
|
|
91
106
|
return this;
|
|
92
107
|
}
|
|
93
|
-
this.sql = `INSERT INTO ${
|
|
108
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders})`;
|
|
94
109
|
this.params.push(...values);
|
|
95
110
|
return this;
|
|
96
111
|
}
|
|
97
112
|
// Update operations
|
|
98
113
|
update(table, columns, values) {
|
|
99
|
-
const
|
|
100
|
-
|
|
114
|
+
const parsedTableName = utils.parseSqlIdentifier(table, "table name");
|
|
115
|
+
const parsedColumns = columns.map((col) => utils.parseSqlIdentifier(col, "column name"));
|
|
116
|
+
const setClause = parsedColumns.map((col) => `${col} = ?`).join(", ");
|
|
117
|
+
this.sql = `UPDATE ${parsedTableName} SET ${setClause}`;
|
|
101
118
|
this.params.push(...values);
|
|
102
119
|
return this;
|
|
103
120
|
}
|
|
104
121
|
// Delete operations
|
|
105
122
|
delete(table) {
|
|
106
|
-
|
|
123
|
+
const parsedTableName = utils.parseSqlIdentifier(table, "table name");
|
|
124
|
+
this.sql = `DELETE FROM ${parsedTableName}`;
|
|
107
125
|
return this;
|
|
108
126
|
}
|
|
109
127
|
/**
|
|
@@ -114,9 +132,16 @@ var SqlBuilder = class {
|
|
|
114
132
|
* @returns The builder instance
|
|
115
133
|
*/
|
|
116
134
|
createTable(table, columnDefinitions, tableConstraints) {
|
|
117
|
-
const
|
|
135
|
+
const parsedTableName = utils.parseSqlIdentifier(table, "table name");
|
|
136
|
+
const parsedColumnDefinitions = columnDefinitions.map((def) => {
|
|
137
|
+
const colName = def.split(/\s+/)[0];
|
|
138
|
+
if (!colName) throw new Error("Empty column name in definition");
|
|
139
|
+
utils.parseSqlIdentifier(colName, "column name");
|
|
140
|
+
return def;
|
|
141
|
+
});
|
|
142
|
+
const columns = parsedColumnDefinitions.join(", ");
|
|
118
143
|
const constraints = tableConstraints && tableConstraints.length > 0 ? ", " + tableConstraints.join(", ") : "";
|
|
119
|
-
this.sql = `CREATE TABLE IF NOT EXISTS ${
|
|
144
|
+
this.sql = `CREATE TABLE IF NOT EXISTS ${parsedTableName} (${columns}${constraints})`;
|
|
120
145
|
return this;
|
|
121
146
|
}
|
|
122
147
|
/**
|
|
@@ -139,13 +164,10 @@ var SqlBuilder = class {
|
|
|
139
164
|
* @returns The builder instance
|
|
140
165
|
*/
|
|
141
166
|
createIndex(indexName, tableName, columnName, indexType = "") {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
raw(sql, ...params) {
|
|
147
|
-
this.sql = sql;
|
|
148
|
-
this.params.push(...params);
|
|
167
|
+
const parsedIndexName = utils.parseSqlIdentifier(indexName, "index name");
|
|
168
|
+
const parsedTableName = utils.parseSqlIdentifier(tableName, "table name");
|
|
169
|
+
const parsedColumnName = utils.parseSqlIdentifier(columnName, "column name");
|
|
170
|
+
this.sql = `CREATE ${indexType ? indexType + " " : ""}INDEX IF NOT EXISTS ${parsedIndexName} ON ${parsedTableName}(${parsedColumnName})`;
|
|
149
171
|
return this;
|
|
150
172
|
}
|
|
151
173
|
/**
|
|
@@ -155,11 +177,12 @@ var SqlBuilder = class {
|
|
|
155
177
|
* @param exact If true, will not add % wildcards
|
|
156
178
|
*/
|
|
157
179
|
like(column, value, exact = false) {
|
|
180
|
+
const parsedColumnName = utils.parseSqlIdentifier(column, "column name");
|
|
158
181
|
const likeValue = exact ? value : `%${value}%`;
|
|
159
182
|
if (this.whereAdded) {
|
|
160
|
-
this.sql += ` AND ${
|
|
183
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
161
184
|
} else {
|
|
162
|
-
this.sql += ` WHERE ${
|
|
185
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
163
186
|
this.whereAdded = true;
|
|
164
187
|
}
|
|
165
188
|
this.params.push(likeValue);
|
|
@@ -172,11 +195,13 @@ var SqlBuilder = class {
|
|
|
172
195
|
* @param value The value to match
|
|
173
196
|
*/
|
|
174
197
|
jsonLike(column, key, value) {
|
|
175
|
-
const
|
|
198
|
+
const parsedColumnName = utils.parseSqlIdentifier(column, "column name");
|
|
199
|
+
const parsedKey = utils.parseSqlIdentifier(key, "key name");
|
|
200
|
+
const jsonPattern = `%"${parsedKey}":"${value}"%`;
|
|
176
201
|
if (this.whereAdded) {
|
|
177
|
-
this.sql += ` AND ${
|
|
202
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
178
203
|
} else {
|
|
179
|
-
this.sql += ` WHERE ${
|
|
204
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
180
205
|
this.whereAdded = true;
|
|
181
206
|
}
|
|
182
207
|
this.params.push(jsonPattern);
|
|
@@ -206,331 +231,311 @@ var SqlBuilder = class {
|
|
|
206
231
|
function createSqlBuilder() {
|
|
207
232
|
return new SqlBuilder();
|
|
208
233
|
}
|
|
234
|
+
var SQL_IDENTIFIER_PATTERN = /^[a-zA-Z0-9_]+(\s+AS\s+[a-zA-Z0-9_]+)?$/;
|
|
235
|
+
function parseSelectIdentifier(column) {
|
|
236
|
+
if (column !== "*" && !SQL_IDENTIFIER_PATTERN.test(column)) {
|
|
237
|
+
throw new Error(
|
|
238
|
+
`Invalid column name: "${column}". Must be "*" or a valid identifier (letters, numbers, underscores), optionally with "AS alias".`
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
return column;
|
|
242
|
+
}
|
|
209
243
|
|
|
210
|
-
// src/storage/
|
|
244
|
+
// src/storage/domains/utils.ts
|
|
211
245
|
function isArrayOfRecords(value) {
|
|
212
246
|
return value && Array.isArray(value) && value.length > 0;
|
|
213
247
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
binding;
|
|
219
|
-
// D1Database binding
|
|
220
|
-
tablePrefix;
|
|
221
|
-
/**
|
|
222
|
-
* Creates a new D1Store instance
|
|
223
|
-
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
224
|
-
*/
|
|
225
|
-
constructor(config) {
|
|
226
|
-
super({ name: "D1" });
|
|
227
|
-
this.tablePrefix = config.tablePrefix || "";
|
|
228
|
-
if ("binding" in config) {
|
|
229
|
-
if (!config.binding) {
|
|
230
|
-
throw new Error("D1 binding is required when using Workers Binding API");
|
|
231
|
-
}
|
|
232
|
-
this.binding = config.binding;
|
|
233
|
-
this.logger.info("Using D1 Workers Binding API");
|
|
234
|
-
} else {
|
|
235
|
-
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
236
|
-
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
237
|
-
}
|
|
238
|
-
this.accountId = config.accountId;
|
|
239
|
-
this.databaseId = config.databaseId;
|
|
240
|
-
this.client = new Cloudflare__default.default({
|
|
241
|
-
apiToken: config.apiToken
|
|
242
|
-
});
|
|
243
|
-
this.logger.info("Using D1 REST API");
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
// Helper method to get the full table name with prefix
|
|
247
|
-
getTableName(tableName) {
|
|
248
|
-
return `${this.tablePrefix}${tableName}`;
|
|
249
|
-
}
|
|
250
|
-
formatSqlParams(params) {
|
|
251
|
-
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
248
|
+
function deserializeValue(value, type) {
|
|
249
|
+
if (value === null || value === void 0) return null;
|
|
250
|
+
if (type === "date" && typeof value === "string") {
|
|
251
|
+
return new Date(value);
|
|
252
252
|
}
|
|
253
|
-
|
|
254
|
-
async createIndexIfNotExists(tableName, columnName, indexType = "") {
|
|
255
|
-
const fullTableName = this.getTableName(tableName);
|
|
256
|
-
const indexName = `idx_${tableName}_${columnName}`;
|
|
253
|
+
if (type === "jsonb" && typeof value === "string") {
|
|
257
254
|
try {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
sql: checkSql,
|
|
262
|
-
params: checkParams,
|
|
263
|
-
first: true
|
|
264
|
-
});
|
|
265
|
-
if (!indexExists) {
|
|
266
|
-
const createQuery = createSqlBuilder().createIndex(indexName, fullTableName, columnName, indexType);
|
|
267
|
-
const { sql: createSql, params: createParams } = createQuery.build();
|
|
268
|
-
await this.executeQuery({ sql: createSql, params: createParams });
|
|
269
|
-
this.logger.debug(`Created index ${indexName} on ${fullTableName}(${columnName})`);
|
|
270
|
-
}
|
|
271
|
-
} catch (error) {
|
|
272
|
-
this.logger.error(`Error creating index on ${fullTableName}(${columnName}):`, {
|
|
273
|
-
message: error instanceof Error ? error.message : String(error)
|
|
274
|
-
});
|
|
255
|
+
return JSON.parse(value);
|
|
256
|
+
} catch {
|
|
257
|
+
return value;
|
|
275
258
|
}
|
|
276
259
|
}
|
|
277
|
-
|
|
278
|
-
sql,
|
|
279
|
-
params = [],
|
|
280
|
-
first = false
|
|
281
|
-
}) {
|
|
282
|
-
if (!this.binding) {
|
|
283
|
-
throw new Error("Workers binding is not configured");
|
|
284
|
-
}
|
|
260
|
+
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
285
261
|
try {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (formattedParams.length > 0) {
|
|
290
|
-
if (first) {
|
|
291
|
-
result = await statement.bind(...formattedParams).first();
|
|
292
|
-
if (!result) return null;
|
|
293
|
-
return result;
|
|
294
|
-
} else {
|
|
295
|
-
result = await statement.bind(...formattedParams).all();
|
|
296
|
-
const results = result.results || [];
|
|
297
|
-
if (result.meta) {
|
|
298
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
299
|
-
}
|
|
300
|
-
return results;
|
|
301
|
-
}
|
|
302
|
-
} else {
|
|
303
|
-
if (first) {
|
|
304
|
-
result = await statement.first();
|
|
305
|
-
if (!result) return null;
|
|
306
|
-
return result;
|
|
307
|
-
} else {
|
|
308
|
-
result = await statement.all();
|
|
309
|
-
const results = result.results || [];
|
|
310
|
-
if (result.meta) {
|
|
311
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
312
|
-
}
|
|
313
|
-
return results;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
} catch (workerError) {
|
|
317
|
-
this.logger.error("Workers Binding API error", {
|
|
318
|
-
message: workerError instanceof Error ? workerError.message : String(workerError),
|
|
319
|
-
sql
|
|
320
|
-
});
|
|
321
|
-
throw new Error(`D1 Workers API error: ${workerError.message}`);
|
|
262
|
+
return JSON.parse(value);
|
|
263
|
+
} catch {
|
|
264
|
+
return value;
|
|
322
265
|
}
|
|
323
266
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
267
|
+
return value;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// src/storage/domains/legacy-evals/index.ts
|
|
271
|
+
var LegacyEvalsStorageD1 = class extends storage.LegacyEvalsStorage {
|
|
272
|
+
operations;
|
|
273
|
+
constructor({ operations }) {
|
|
274
|
+
super();
|
|
275
|
+
this.operations = operations;
|
|
276
|
+
}
|
|
277
|
+
async getEvals(options) {
|
|
278
|
+
const { agentName, type, page = 0, perPage = 40, dateRange } = options || {};
|
|
279
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
|
|
280
|
+
const conditions = [];
|
|
281
|
+
const queryParams = [];
|
|
282
|
+
if (agentName) {
|
|
283
|
+
conditions.push(`agent_name = ?`);
|
|
284
|
+
queryParams.push(agentName);
|
|
285
|
+
}
|
|
286
|
+
if (type === "test") {
|
|
287
|
+
conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
|
|
288
|
+
} else if (type === "live") {
|
|
289
|
+
conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
|
|
290
|
+
}
|
|
291
|
+
if (dateRange?.start) {
|
|
292
|
+
conditions.push(`created_at >= ?`);
|
|
293
|
+
queryParams.push(storage.serializeDate(dateRange.start));
|
|
331
294
|
}
|
|
295
|
+
if (dateRange?.end) {
|
|
296
|
+
conditions.push(`created_at <= ?`);
|
|
297
|
+
queryParams.push(storage.serializeDate(dateRange.end));
|
|
298
|
+
}
|
|
299
|
+
const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
|
|
300
|
+
if (conditions.length > 0) {
|
|
301
|
+
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
302
|
+
}
|
|
303
|
+
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
332
304
|
try {
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
305
|
+
const countResult = await this.operations.executeQuery({
|
|
306
|
+
sql: countSql,
|
|
307
|
+
params: countParams,
|
|
308
|
+
first: true
|
|
337
309
|
});
|
|
338
|
-
const
|
|
339
|
-
const
|
|
340
|
-
if (
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
310
|
+
const total = Number(countResult?.count || 0);
|
|
311
|
+
const currentOffset = page * perPage;
|
|
312
|
+
if (total === 0) {
|
|
313
|
+
return {
|
|
314
|
+
evals: [],
|
|
315
|
+
total: 0,
|
|
316
|
+
page,
|
|
317
|
+
perPage,
|
|
318
|
+
hasMore: false
|
|
319
|
+
};
|
|
344
320
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
321
|
+
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
322
|
+
if (conditions.length > 0) {
|
|
323
|
+
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
324
|
+
}
|
|
325
|
+
dataQueryBuilder.orderBy("created_at", "DESC").limit(perPage).offset(currentOffset);
|
|
326
|
+
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
327
|
+
const rows = await this.operations.executeQuery({
|
|
328
|
+
sql: dataSql,
|
|
329
|
+
params: dataParams
|
|
330
|
+
});
|
|
331
|
+
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
332
|
+
const result = deserializeValue(row.result);
|
|
333
|
+
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
334
|
+
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
335
|
+
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
input: row.input,
|
|
339
|
+
output: row.output,
|
|
340
|
+
result,
|
|
341
|
+
agentName: row.agent_name,
|
|
342
|
+
metricName: row.metric_name,
|
|
343
|
+
instructions: row.instructions,
|
|
344
|
+
testInfo,
|
|
345
|
+
globalRunId: row.global_run_id,
|
|
346
|
+
runId: row.run_id,
|
|
347
|
+
createdAt: row.created_at
|
|
348
|
+
};
|
|
350
349
|
});
|
|
351
|
-
|
|
350
|
+
const hasMore = currentOffset + evals.length < total;
|
|
351
|
+
return {
|
|
352
|
+
evals,
|
|
353
|
+
total,
|
|
354
|
+
page,
|
|
355
|
+
perPage,
|
|
356
|
+
hasMore
|
|
357
|
+
};
|
|
358
|
+
} catch (error$1) {
|
|
359
|
+
throw new error.MastraError(
|
|
360
|
+
{
|
|
361
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
362
|
+
domain: error.ErrorDomain.STORAGE,
|
|
363
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
364
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
365
|
+
details: { agentName: agentName ?? "", type: type ?? "" }
|
|
366
|
+
},
|
|
367
|
+
error$1
|
|
368
|
+
);
|
|
352
369
|
}
|
|
353
370
|
}
|
|
354
371
|
/**
|
|
355
|
-
*
|
|
356
|
-
* @param options Query options including SQL, parameters, and whether to return only the first result
|
|
357
|
-
* @returns Query results as an array or a single object if first=true
|
|
372
|
+
* @deprecated use getEvals instead
|
|
358
373
|
*/
|
|
359
|
-
async
|
|
360
|
-
const
|
|
374
|
+
async getEvalsByAgentName(agentName, type) {
|
|
375
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
|
|
361
376
|
try {
|
|
362
|
-
|
|
363
|
-
if (
|
|
364
|
-
|
|
365
|
-
} else if (
|
|
366
|
-
|
|
367
|
-
} else {
|
|
368
|
-
throw new Error("No valid D1 configuration provided");
|
|
377
|
+
let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
|
|
378
|
+
if (type === "test") {
|
|
379
|
+
query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
|
|
380
|
+
} else if (type === "live") {
|
|
381
|
+
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
369
382
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
serializeDate(date) {
|
|
404
|
-
if (!date) return void 0;
|
|
405
|
-
const dateObj = this.ensureDate(date);
|
|
406
|
-
return dateObj?.toISOString();
|
|
407
|
-
}
|
|
408
|
-
// Helper to serialize objects to JSON strings
|
|
409
|
-
serializeValue(value) {
|
|
410
|
-
if (value === null || value === void 0) return null;
|
|
411
|
-
if (value instanceof Date) {
|
|
412
|
-
return this.serializeDate(value);
|
|
413
|
-
}
|
|
414
|
-
if (typeof value === "object") {
|
|
415
|
-
return JSON.stringify(value);
|
|
383
|
+
query.orderBy("created_at", "DESC");
|
|
384
|
+
const { sql, params } = query.build();
|
|
385
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
386
|
+
return isArrayOfRecords(results) ? results.map((row) => {
|
|
387
|
+
const result = deserializeValue(row.result);
|
|
388
|
+
const testInfo = row.test_info ? deserializeValue(row.test_info) : void 0;
|
|
389
|
+
return {
|
|
390
|
+
input: row.input || "",
|
|
391
|
+
output: row.output || "",
|
|
392
|
+
result,
|
|
393
|
+
agentName: row.agent_name || "",
|
|
394
|
+
metricName: row.metric_name || "",
|
|
395
|
+
instructions: row.instructions || "",
|
|
396
|
+
runId: row.run_id || "",
|
|
397
|
+
globalRunId: row.global_run_id || "",
|
|
398
|
+
createdAt: row.created_at || "",
|
|
399
|
+
testInfo
|
|
400
|
+
};
|
|
401
|
+
}) : [];
|
|
402
|
+
} catch (error$1) {
|
|
403
|
+
const mastraError = new error.MastraError(
|
|
404
|
+
{
|
|
405
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_EVALS_ERROR",
|
|
406
|
+
domain: error.ErrorDomain.STORAGE,
|
|
407
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
408
|
+
text: `Failed to retrieve evals for agent ${agentName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
409
|
+
details: { agentName }
|
|
410
|
+
},
|
|
411
|
+
error$1
|
|
412
|
+
);
|
|
413
|
+
this.logger?.error(mastraError.toString());
|
|
414
|
+
this.logger?.trackException(mastraError);
|
|
415
|
+
return [];
|
|
416
416
|
}
|
|
417
|
-
return value;
|
|
418
417
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
if (type === "jsonb" && typeof value === "string") {
|
|
426
|
-
try {
|
|
427
|
-
return JSON.parse(value);
|
|
428
|
-
} catch {
|
|
429
|
-
return value;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
433
|
-
try {
|
|
434
|
-
return JSON.parse(value);
|
|
435
|
-
} catch {
|
|
436
|
-
return value;
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
return value;
|
|
418
|
+
};
|
|
419
|
+
var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
420
|
+
operations;
|
|
421
|
+
constructor({ operations }) {
|
|
422
|
+
super();
|
|
423
|
+
this.operations = operations;
|
|
440
424
|
}
|
|
441
|
-
async
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
const fullTableName = this.getTableName(tableName);
|
|
446
|
-
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
447
|
-
const type = this.getSqlType(colDef.type);
|
|
448
|
-
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
449
|
-
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
450
|
-
return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
|
|
425
|
+
async getResourceById({ resourceId }) {
|
|
426
|
+
const resource = await this.operations.load({
|
|
427
|
+
tableName: storage.TABLE_RESOURCES,
|
|
428
|
+
keys: { id: resourceId }
|
|
451
429
|
});
|
|
452
|
-
|
|
453
|
-
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
454
|
-
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
455
|
-
}
|
|
456
|
-
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
457
|
-
const { sql, params } = query.build();
|
|
458
|
-
try {
|
|
459
|
-
await this.executeQuery({ sql, params });
|
|
460
|
-
this.logger.debug(`Created table ${fullTableName}`);
|
|
461
|
-
} catch (error) {
|
|
462
|
-
this.logger.error(`Error creating table ${fullTableName}:`, {
|
|
463
|
-
message: error instanceof Error ? error.message : String(error)
|
|
464
|
-
});
|
|
465
|
-
throw new Error(`Failed to create table ${fullTableName}: ${error}`);
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
async clearTable({ tableName }) {
|
|
469
|
-
const fullTableName = this.getTableName(tableName);
|
|
430
|
+
if (!resource) return null;
|
|
470
431
|
try {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
432
|
+
return {
|
|
433
|
+
...resource,
|
|
434
|
+
createdAt: storage.ensureDate(resource.createdAt),
|
|
435
|
+
updatedAt: storage.ensureDate(resource.updatedAt),
|
|
436
|
+
metadata: typeof resource.metadata === "string" ? JSON.parse(resource.metadata || "{}") : resource.metadata
|
|
437
|
+
};
|
|
438
|
+
} catch (error$1) {
|
|
439
|
+
const mastraError = new error.MastraError(
|
|
440
|
+
{
|
|
441
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_RESOURCE_BY_ID_ERROR",
|
|
442
|
+
domain: error.ErrorDomain.STORAGE,
|
|
443
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
444
|
+
text: `Error processing resource ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
445
|
+
details: { resourceId }
|
|
446
|
+
},
|
|
447
|
+
error$1
|
|
448
|
+
);
|
|
449
|
+
this.logger?.error(mastraError.toString());
|
|
450
|
+
this.logger?.trackException(mastraError);
|
|
451
|
+
return null;
|
|
486
452
|
}
|
|
487
|
-
return processedRecord;
|
|
488
453
|
}
|
|
489
|
-
async
|
|
490
|
-
const fullTableName = this.getTableName(
|
|
491
|
-
const
|
|
454
|
+
async saveResource({ resource }) {
|
|
455
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_RESOURCES);
|
|
456
|
+
const resourceToSave = {
|
|
457
|
+
id: resource.id,
|
|
458
|
+
workingMemory: resource.workingMemory,
|
|
459
|
+
metadata: resource.metadata ? JSON.stringify(resource.metadata) : null,
|
|
460
|
+
createdAt: resource.createdAt,
|
|
461
|
+
updatedAt: resource.updatedAt
|
|
462
|
+
};
|
|
463
|
+
const processedRecord = await this.operations.processRecord(resourceToSave);
|
|
492
464
|
const columns = Object.keys(processedRecord);
|
|
493
465
|
const values = Object.values(processedRecord);
|
|
494
|
-
const
|
|
466
|
+
const updateMap = {
|
|
467
|
+
workingMemory: "excluded.workingMemory",
|
|
468
|
+
metadata: "excluded.metadata",
|
|
469
|
+
createdAt: "excluded.createdAt",
|
|
470
|
+
updatedAt: "excluded.updatedAt"
|
|
471
|
+
};
|
|
472
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
495
473
|
const { sql, params } = query.build();
|
|
496
474
|
try {
|
|
497
|
-
await this.executeQuery({ sql, params });
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
throw new
|
|
475
|
+
await this.operations.executeQuery({ sql, params });
|
|
476
|
+
return resource;
|
|
477
|
+
} catch (error$1) {
|
|
478
|
+
throw new error.MastraError(
|
|
479
|
+
{
|
|
480
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_RESOURCE_ERROR",
|
|
481
|
+
domain: error.ErrorDomain.STORAGE,
|
|
482
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
483
|
+
text: `Failed to save resource to ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
484
|
+
details: { resourceId: resource.id }
|
|
485
|
+
},
|
|
486
|
+
error$1
|
|
487
|
+
);
|
|
501
488
|
}
|
|
502
489
|
}
|
|
503
|
-
async
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
490
|
+
async updateResource({
|
|
491
|
+
resourceId,
|
|
492
|
+
workingMemory,
|
|
493
|
+
metadata
|
|
494
|
+
}) {
|
|
495
|
+
const existingResource = await this.getResourceById({ resourceId });
|
|
496
|
+
if (!existingResource) {
|
|
497
|
+
const newResource = {
|
|
498
|
+
id: resourceId,
|
|
499
|
+
workingMemory,
|
|
500
|
+
metadata: metadata || {},
|
|
501
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
502
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
503
|
+
};
|
|
504
|
+
return this.saveResource({ resource: newResource });
|
|
514
505
|
}
|
|
515
|
-
|
|
506
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
507
|
+
const updatedResource = {
|
|
508
|
+
...existingResource,
|
|
509
|
+
workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
|
|
510
|
+
metadata: {
|
|
511
|
+
...existingResource.metadata,
|
|
512
|
+
...metadata
|
|
513
|
+
},
|
|
514
|
+
updatedAt
|
|
515
|
+
};
|
|
516
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_RESOURCES);
|
|
517
|
+
const columns = ["workingMemory", "metadata", "updatedAt"];
|
|
518
|
+
const values = [updatedResource.workingMemory, JSON.stringify(updatedResource.metadata), updatedAt.toISOString()];
|
|
519
|
+
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", resourceId);
|
|
516
520
|
const { sql, params } = query.build();
|
|
517
521
|
try {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
522
|
+
await this.operations.executeQuery({ sql, params });
|
|
523
|
+
return updatedResource;
|
|
524
|
+
} catch (error$1) {
|
|
525
|
+
throw new error.MastraError(
|
|
526
|
+
{
|
|
527
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_RESOURCE_ERROR",
|
|
528
|
+
domain: error.ErrorDomain.STORAGE,
|
|
529
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
530
|
+
text: `Failed to update resource ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
531
|
+
details: { resourceId }
|
|
532
|
+
},
|
|
533
|
+
error$1
|
|
534
|
+
);
|
|
530
535
|
}
|
|
531
536
|
}
|
|
532
537
|
async getThreadById({ threadId }) {
|
|
533
|
-
const thread = await this.load({
|
|
538
|
+
const thread = await this.operations.load({
|
|
534
539
|
tableName: storage.TABLE_THREADS,
|
|
535
540
|
keys: { id: threadId }
|
|
536
541
|
});
|
|
@@ -538,47 +543,113 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
538
543
|
try {
|
|
539
544
|
return {
|
|
540
545
|
...thread,
|
|
541
|
-
createdAt:
|
|
542
|
-
updatedAt:
|
|
546
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
547
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
543
548
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
544
549
|
};
|
|
545
|
-
} catch (error) {
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
550
|
+
} catch (error$1) {
|
|
551
|
+
const mastraError = new error.MastraError(
|
|
552
|
+
{
|
|
553
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREAD_BY_ID_ERROR",
|
|
554
|
+
domain: error.ErrorDomain.STORAGE,
|
|
555
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
556
|
+
text: `Error processing thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
557
|
+
details: { threadId }
|
|
558
|
+
},
|
|
559
|
+
error$1
|
|
560
|
+
);
|
|
561
|
+
this.logger?.error(mastraError.toString());
|
|
562
|
+
this.logger?.trackException(mastraError);
|
|
549
563
|
return null;
|
|
550
564
|
}
|
|
551
565
|
}
|
|
566
|
+
/**
|
|
567
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
568
|
+
*/
|
|
552
569
|
async getThreadsByResourceId({ resourceId }) {
|
|
553
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
570
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
554
571
|
try {
|
|
555
572
|
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
556
573
|
const { sql, params } = query.build();
|
|
557
|
-
const results = await this.executeQuery({ sql, params });
|
|
574
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
558
575
|
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
559
576
|
...thread,
|
|
560
|
-
createdAt:
|
|
561
|
-
updatedAt:
|
|
577
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
578
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
562
579
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
563
580
|
}));
|
|
564
|
-
} catch (error) {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
581
|
+
} catch (error$1) {
|
|
582
|
+
const mastraError = new error.MastraError(
|
|
583
|
+
{
|
|
584
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_ERROR",
|
|
585
|
+
domain: error.ErrorDomain.STORAGE,
|
|
586
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
587
|
+
text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
588
|
+
details: { resourceId }
|
|
589
|
+
},
|
|
590
|
+
error$1
|
|
591
|
+
);
|
|
592
|
+
this.logger?.error(mastraError.toString());
|
|
593
|
+
this.logger?.trackException(mastraError);
|
|
568
594
|
return [];
|
|
569
595
|
}
|
|
570
596
|
}
|
|
597
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
598
|
+
const { resourceId, page, perPage } = args;
|
|
599
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
600
|
+
const mapRowToStorageThreadType = (row) => ({
|
|
601
|
+
...row,
|
|
602
|
+
createdAt: storage.ensureDate(row.createdAt),
|
|
603
|
+
updatedAt: storage.ensureDate(row.updatedAt),
|
|
604
|
+
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
|
|
605
|
+
});
|
|
606
|
+
try {
|
|
607
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
608
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
609
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
610
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
|
|
611
|
+
const results = await this.operations.executeQuery(selectQuery.build());
|
|
612
|
+
const threads = results.map(mapRowToStorageThreadType);
|
|
613
|
+
return {
|
|
614
|
+
threads,
|
|
615
|
+
total,
|
|
616
|
+
page,
|
|
617
|
+
perPage,
|
|
618
|
+
hasMore: page * perPage + threads.length < total
|
|
619
|
+
};
|
|
620
|
+
} catch (error$1) {
|
|
621
|
+
const mastraError = new error.MastraError(
|
|
622
|
+
{
|
|
623
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_ERROR",
|
|
624
|
+
domain: error.ErrorDomain.STORAGE,
|
|
625
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
626
|
+
text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
627
|
+
details: { resourceId }
|
|
628
|
+
},
|
|
629
|
+
error$1
|
|
630
|
+
);
|
|
631
|
+
this.logger?.error(mastraError.toString());
|
|
632
|
+
this.logger?.trackException(mastraError);
|
|
633
|
+
return {
|
|
634
|
+
threads: [],
|
|
635
|
+
total: 0,
|
|
636
|
+
page,
|
|
637
|
+
perPage,
|
|
638
|
+
hasMore: false
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
}
|
|
571
642
|
async saveThread({ thread }) {
|
|
572
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
643
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
573
644
|
const threadToSave = {
|
|
574
645
|
id: thread.id,
|
|
575
646
|
resourceId: thread.resourceId,
|
|
576
647
|
title: thread.title,
|
|
577
648
|
metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
|
|
578
|
-
createdAt: thread.createdAt,
|
|
579
|
-
updatedAt: thread.updatedAt
|
|
649
|
+
createdAt: thread.createdAt.toISOString(),
|
|
650
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
580
651
|
};
|
|
581
|
-
const processedRecord = await this.processRecord(threadToSave);
|
|
652
|
+
const processedRecord = await this.operations.processRecord(threadToSave);
|
|
582
653
|
const columns = Object.keys(processedRecord);
|
|
583
654
|
const values = Object.values(processedRecord);
|
|
584
655
|
const updateMap = {
|
|
@@ -591,11 +662,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
591
662
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
592
663
|
const { sql, params } = query.build();
|
|
593
664
|
try {
|
|
594
|
-
await this.executeQuery({ sql, params });
|
|
665
|
+
await this.operations.executeQuery({ sql, params });
|
|
595
666
|
return thread;
|
|
596
|
-
} catch (error) {
|
|
597
|
-
|
|
598
|
-
|
|
667
|
+
} catch (error$1) {
|
|
668
|
+
throw new error.MastraError(
|
|
669
|
+
{
|
|
670
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_THREAD_ERROR",
|
|
671
|
+
domain: error.ErrorDomain.STORAGE,
|
|
672
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
673
|
+
text: `Failed to save thread to ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
674
|
+
details: { threadId: thread.id }
|
|
675
|
+
},
|
|
676
|
+
error$1
|
|
677
|
+
);
|
|
599
678
|
}
|
|
600
679
|
}
|
|
601
680
|
async updateThread({
|
|
@@ -604,20 +683,21 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
604
683
|
metadata
|
|
605
684
|
}) {
|
|
606
685
|
const thread = await this.getThreadById({ threadId: id });
|
|
607
|
-
if (!thread) {
|
|
608
|
-
throw new Error(`Thread ${id} not found`);
|
|
609
|
-
}
|
|
610
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
611
|
-
const mergedMetadata = {
|
|
612
|
-
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
613
|
-
...metadata
|
|
614
|
-
};
|
|
615
|
-
const columns = ["title", "metadata", "updatedAt"];
|
|
616
|
-
const values = [title, JSON.stringify(mergedMetadata), (/* @__PURE__ */ new Date()).toISOString()];
|
|
617
|
-
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
618
|
-
const { sql, params } = query.build();
|
|
619
686
|
try {
|
|
620
|
-
|
|
687
|
+
if (!thread) {
|
|
688
|
+
throw new Error(`Thread ${id} not found`);
|
|
689
|
+
}
|
|
690
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
691
|
+
const mergedMetadata = {
|
|
692
|
+
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
693
|
+
...metadata
|
|
694
|
+
};
|
|
695
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
696
|
+
const columns = ["title", "metadata", "updatedAt"];
|
|
697
|
+
const values = [title, JSON.stringify(mergedMetadata), updatedAt.toISOString()];
|
|
698
|
+
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
699
|
+
const { sql, params } = query.build();
|
|
700
|
+
await this.operations.executeQuery({ sql, params });
|
|
621
701
|
return {
|
|
622
702
|
...thread,
|
|
623
703
|
title,
|
|
@@ -625,40 +705,64 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
625
705
|
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
626
706
|
...metadata
|
|
627
707
|
},
|
|
628
|
-
updatedAt
|
|
708
|
+
updatedAt
|
|
629
709
|
};
|
|
630
|
-
} catch (error) {
|
|
631
|
-
|
|
632
|
-
|
|
710
|
+
} catch (error$1) {
|
|
711
|
+
throw new error.MastraError(
|
|
712
|
+
{
|
|
713
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_THREAD_ERROR",
|
|
714
|
+
domain: error.ErrorDomain.STORAGE,
|
|
715
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
716
|
+
text: `Failed to update thread ${id}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
717
|
+
details: { threadId: id }
|
|
718
|
+
},
|
|
719
|
+
error$1
|
|
720
|
+
);
|
|
633
721
|
}
|
|
634
722
|
}
|
|
635
723
|
async deleteThread({ threadId }) {
|
|
636
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
724
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
637
725
|
try {
|
|
638
726
|
const deleteThreadQuery = createSqlBuilder().delete(fullTableName).where("id = ?", threadId);
|
|
639
727
|
const { sql: threadSql, params: threadParams } = deleteThreadQuery.build();
|
|
640
|
-
await this.executeQuery({ sql: threadSql, params: threadParams });
|
|
641
|
-
const messagesTableName = this.getTableName(storage.TABLE_MESSAGES);
|
|
728
|
+
await this.operations.executeQuery({ sql: threadSql, params: threadParams });
|
|
729
|
+
const messagesTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
642
730
|
const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
|
|
643
731
|
const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
|
|
644
|
-
await this.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
645
|
-
} catch (error) {
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
732
|
+
await this.operations.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
733
|
+
} catch (error$1) {
|
|
734
|
+
throw new error.MastraError(
|
|
735
|
+
{
|
|
736
|
+
id: "CLOUDFLARE_D1_STORAGE_DELETE_THREAD_ERROR",
|
|
737
|
+
domain: error.ErrorDomain.STORAGE,
|
|
738
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
739
|
+
text: `Failed to delete thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
740
|
+
details: { threadId }
|
|
741
|
+
},
|
|
742
|
+
error$1
|
|
743
|
+
);
|
|
650
744
|
}
|
|
651
745
|
}
|
|
652
|
-
|
|
653
|
-
|
|
746
|
+
async saveMessages(args) {
|
|
747
|
+
const { messages, format = "v1" } = args;
|
|
654
748
|
if (messages.length === 0) return [];
|
|
655
749
|
try {
|
|
656
750
|
const now = /* @__PURE__ */ new Date();
|
|
751
|
+
const threadId = messages[0]?.threadId;
|
|
657
752
|
for (const [i, message] of messages.entries()) {
|
|
658
753
|
if (!message.id) throw new Error(`Message at index ${i} missing id`);
|
|
659
|
-
if (!message.threadId)
|
|
660
|
-
|
|
661
|
-
|
|
754
|
+
if (!message.threadId) {
|
|
755
|
+
throw new Error(`Message at index ${i} missing threadId`);
|
|
756
|
+
}
|
|
757
|
+
if (!message.content) {
|
|
758
|
+
throw new Error(`Message at index ${i} missing content`);
|
|
759
|
+
}
|
|
760
|
+
if (!message.role) {
|
|
761
|
+
throw new Error(`Message at index ${i} missing role`);
|
|
762
|
+
}
|
|
763
|
+
if (!message.resourceId) {
|
|
764
|
+
throw new Error(`Message at index ${i} missing resourceId`);
|
|
765
|
+
}
|
|
662
766
|
const thread = await this.getThreadById({ threadId: message.threadId });
|
|
663
767
|
if (!thread) {
|
|
664
768
|
throw new Error(`Thread ${message.threadId} not found`);
|
|
@@ -672,74 +776,122 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
672
776
|
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
673
777
|
createdAt: createdAt.toISOString(),
|
|
674
778
|
role: message.role,
|
|
675
|
-
type: message.type
|
|
779
|
+
type: message.type || "v2",
|
|
780
|
+
resourceId: message.resourceId
|
|
676
781
|
};
|
|
677
782
|
});
|
|
678
|
-
await
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
783
|
+
await Promise.all([
|
|
784
|
+
this.operations.batchUpsert({
|
|
785
|
+
tableName: storage.TABLE_MESSAGES,
|
|
786
|
+
records: messagesToInsert
|
|
787
|
+
}),
|
|
788
|
+
// Update thread's updatedAt timestamp
|
|
789
|
+
this.operations.executeQuery({
|
|
790
|
+
sql: `UPDATE ${this.operations.getTableName(storage.TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
|
|
791
|
+
params: [now.toISOString(), threadId]
|
|
792
|
+
})
|
|
793
|
+
]);
|
|
682
794
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
795
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
796
|
+
if (format === `v2`) return list.get.all.v2();
|
|
797
|
+
return list.get.all.v1();
|
|
798
|
+
} catch (error$1) {
|
|
799
|
+
throw new error.MastraError(
|
|
800
|
+
{
|
|
801
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_MESSAGES_ERROR",
|
|
802
|
+
domain: error.ErrorDomain.STORAGE,
|
|
803
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
804
|
+
text: `Failed to save messages: ${error$1 instanceof Error ? error$1.message : String(error$1)}`
|
|
805
|
+
},
|
|
806
|
+
error$1
|
|
807
|
+
);
|
|
687
808
|
}
|
|
688
809
|
}
|
|
689
|
-
async
|
|
690
|
-
|
|
691
|
-
const
|
|
692
|
-
|
|
693
|
-
const
|
|
810
|
+
async _getIncludedMessages(threadId, selectBy) {
|
|
811
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
812
|
+
const include = selectBy?.include;
|
|
813
|
+
if (!include) return null;
|
|
814
|
+
const unionQueries = [];
|
|
815
|
+
const params = [];
|
|
816
|
+
let paramIdx = 1;
|
|
817
|
+
for (const inc of include) {
|
|
818
|
+
const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
|
|
819
|
+
const searchId = inc.threadId || threadId;
|
|
820
|
+
unionQueries.push(`
|
|
821
|
+
SELECT * FROM (
|
|
822
|
+
WITH ordered_messages AS (
|
|
823
|
+
SELECT
|
|
824
|
+
*,
|
|
825
|
+
ROW_NUMBER() OVER (ORDER BY createdAt ASC) AS row_num
|
|
826
|
+
FROM ${this.operations.getTableName(storage.TABLE_MESSAGES)}
|
|
827
|
+
WHERE thread_id = ?
|
|
828
|
+
)
|
|
829
|
+
SELECT
|
|
830
|
+
m.id,
|
|
831
|
+
m.content,
|
|
832
|
+
m.role,
|
|
833
|
+
m.type,
|
|
834
|
+
m.createdAt,
|
|
835
|
+
m.thread_id AS threadId,
|
|
836
|
+
m.resourceId
|
|
837
|
+
FROM ordered_messages m
|
|
838
|
+
WHERE m.id = ?
|
|
839
|
+
OR EXISTS (
|
|
840
|
+
SELECT 1 FROM ordered_messages target
|
|
841
|
+
WHERE target.id = ?
|
|
842
|
+
AND (
|
|
843
|
+
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
844
|
+
OR
|
|
845
|
+
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
846
|
+
)
|
|
847
|
+
)
|
|
848
|
+
) AS query_${paramIdx}
|
|
849
|
+
`);
|
|
850
|
+
params.push(searchId, id, id, withNextMessages, withPreviousMessages);
|
|
851
|
+
paramIdx++;
|
|
852
|
+
}
|
|
853
|
+
const finalQuery = unionQueries.join(" UNION ALL ") + " ORDER BY createdAt ASC";
|
|
854
|
+
const messages = await this.operations.executeQuery({ sql: finalQuery, params });
|
|
855
|
+
if (!Array.isArray(messages)) {
|
|
856
|
+
return [];
|
|
857
|
+
}
|
|
858
|
+
const processedMessages = messages.map((message) => {
|
|
859
|
+
const processedMsg = {};
|
|
860
|
+
for (const [key, value] of Object.entries(message)) {
|
|
861
|
+
if (key === `type` && value === `v2`) continue;
|
|
862
|
+
processedMsg[key] = deserializeValue(value);
|
|
863
|
+
}
|
|
864
|
+
return processedMsg;
|
|
865
|
+
});
|
|
866
|
+
return processedMessages;
|
|
867
|
+
}
|
|
868
|
+
async getMessages({
|
|
869
|
+
threadId,
|
|
870
|
+
resourceId,
|
|
871
|
+
selectBy,
|
|
872
|
+
format
|
|
873
|
+
}) {
|
|
694
874
|
try {
|
|
875
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
876
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
877
|
+
const limit = storage.resolveMessageLimit({
|
|
878
|
+
last: selectBy?.last,
|
|
879
|
+
defaultLimit: 40
|
|
880
|
+
});
|
|
881
|
+
const include = selectBy?.include || [];
|
|
882
|
+
const messages = [];
|
|
695
883
|
if (include.length) {
|
|
696
|
-
const
|
|
697
|
-
const nextMax = Math.max(...include.map((i) => i.withNextMessages || 0));
|
|
698
|
-
const includeIds = include.map((i) => i.id);
|
|
699
|
-
const sql2 = `
|
|
700
|
-
WITH ordered_messages AS (
|
|
701
|
-
SELECT
|
|
702
|
-
*,
|
|
703
|
-
ROW_NUMBER() OVER (ORDER BY createdAt DESC) AS row_num
|
|
704
|
-
FROM ${fullTableName}
|
|
705
|
-
WHERE thread_id = ?
|
|
706
|
-
)
|
|
707
|
-
SELECT
|
|
708
|
-
m.id,
|
|
709
|
-
m.content,
|
|
710
|
-
m.role,
|
|
711
|
-
m.type,
|
|
712
|
-
m.createdAt,
|
|
713
|
-
m.thread_id AS "threadId"
|
|
714
|
-
FROM ordered_messages m
|
|
715
|
-
WHERE m.id IN (${includeIds.map(() => "?").join(",")})
|
|
716
|
-
OR EXISTS (
|
|
717
|
-
SELECT 1 FROM ordered_messages target
|
|
718
|
-
WHERE target.id IN (${includeIds.map(() => "?").join(",")})
|
|
719
|
-
AND (
|
|
720
|
-
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
721
|
-
OR
|
|
722
|
-
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
723
|
-
)
|
|
724
|
-
)
|
|
725
|
-
ORDER BY m.createdAt DESC
|
|
726
|
-
`;
|
|
727
|
-
const params2 = [
|
|
728
|
-
threadId,
|
|
729
|
-
...includeIds,
|
|
730
|
-
// for m.id IN (...)
|
|
731
|
-
...includeIds,
|
|
732
|
-
// for target.id IN (...)
|
|
733
|
-
prevMax,
|
|
734
|
-
nextMax
|
|
735
|
-
];
|
|
736
|
-
const includeResult = await this.executeQuery({ sql: sql2, params: params2 });
|
|
884
|
+
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
737
885
|
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
738
886
|
}
|
|
739
887
|
const excludeIds = messages.map((m) => m.id);
|
|
740
|
-
|
|
888
|
+
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
889
|
+
if (excludeIds.length > 0) {
|
|
890
|
+
query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
|
|
891
|
+
}
|
|
892
|
+
query.orderBy("createdAt", "DESC").limit(limit);
|
|
741
893
|
const { sql, params } = query.build();
|
|
742
|
-
const result = await this.executeQuery({ sql, params });
|
|
894
|
+
const result = await this.operations.executeQuery({ sql, params });
|
|
743
895
|
if (Array.isArray(result)) messages.push(...result);
|
|
744
896
|
messages.sort((a, b) => {
|
|
745
897
|
const aRecord = a;
|
|
@@ -751,186 +903,1543 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
751
903
|
const processedMessages = messages.map((message) => {
|
|
752
904
|
const processedMsg = {};
|
|
753
905
|
for (const [key, value] of Object.entries(message)) {
|
|
754
|
-
|
|
906
|
+
if (key === `type` && value === `v2`) continue;
|
|
907
|
+
processedMsg[key] = deserializeValue(value);
|
|
755
908
|
}
|
|
756
909
|
return processedMsg;
|
|
757
910
|
});
|
|
758
911
|
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
912
|
+
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
913
|
+
if (format === `v2`) return list.get.all.v2();
|
|
914
|
+
return list.get.all.v1();
|
|
915
|
+
} catch (error$1) {
|
|
916
|
+
const mastraError = new error.MastraError(
|
|
917
|
+
{
|
|
918
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
|
|
919
|
+
domain: error.ErrorDomain.STORAGE,
|
|
920
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
921
|
+
text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
922
|
+
details: { threadId, resourceId: resourceId ?? "" }
|
|
923
|
+
},
|
|
924
|
+
error$1
|
|
925
|
+
);
|
|
926
|
+
this.logger?.error(mastraError.toString());
|
|
927
|
+
this.logger?.trackException(mastraError);
|
|
928
|
+
throw mastraError;
|
|
766
929
|
}
|
|
767
930
|
}
|
|
768
|
-
async
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
snapshot
|
|
931
|
+
async getMessagesById({
|
|
932
|
+
messageIds,
|
|
933
|
+
format
|
|
772
934
|
}) {
|
|
773
|
-
|
|
774
|
-
const
|
|
775
|
-
const
|
|
776
|
-
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
777
|
-
keys: { workflow_name: workflowName, run_id: runId }
|
|
778
|
-
});
|
|
779
|
-
const persisting = currentSnapshot ? {
|
|
780
|
-
...currentSnapshot,
|
|
781
|
-
snapshot: JSON.stringify(snapshot),
|
|
782
|
-
updatedAt: now
|
|
783
|
-
} : {
|
|
784
|
-
workflow_name: workflowName,
|
|
785
|
-
run_id: runId,
|
|
786
|
-
snapshot,
|
|
787
|
-
createdAt: now,
|
|
788
|
-
updatedAt: now
|
|
789
|
-
};
|
|
790
|
-
const processedRecord = await this.processRecord(persisting);
|
|
791
|
-
const columns = Object.keys(processedRecord);
|
|
792
|
-
const values = Object.values(processedRecord);
|
|
793
|
-
const updateMap = {
|
|
794
|
-
snapshot: "excluded.snapshot",
|
|
795
|
-
updatedAt: "excluded.updatedAt"
|
|
796
|
-
};
|
|
797
|
-
this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
|
|
798
|
-
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
799
|
-
const { sql, params } = query.build();
|
|
935
|
+
if (messageIds.length === 0) return [];
|
|
936
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
937
|
+
const messages = [];
|
|
800
938
|
try {
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
939
|
+
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId", "resourceId"]).from(fullTableName).where(`id in (${messageIds.map(() => "?").join(",")})`, ...messageIds);
|
|
940
|
+
query.orderBy("createdAt", "DESC");
|
|
941
|
+
const { sql, params } = query.build();
|
|
942
|
+
const result = await this.operations.executeQuery({ sql, params });
|
|
943
|
+
if (Array.isArray(result)) messages.push(...result);
|
|
944
|
+
const processedMessages = messages.map((message) => {
|
|
945
|
+
const processedMsg = {};
|
|
946
|
+
for (const [key, value] of Object.entries(message)) {
|
|
947
|
+
if (key === `type` && value === `v2`) continue;
|
|
948
|
+
processedMsg[key] = deserializeValue(value);
|
|
949
|
+
}
|
|
950
|
+
return processedMsg;
|
|
805
951
|
});
|
|
806
|
-
|
|
952
|
+
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
953
|
+
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
954
|
+
if (format === `v1`) return list.get.all.v1();
|
|
955
|
+
return list.get.all.v2();
|
|
956
|
+
} catch (error$1) {
|
|
957
|
+
const mastraError = new error.MastraError(
|
|
958
|
+
{
|
|
959
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_BY_ID_ERROR",
|
|
960
|
+
domain: error.ErrorDomain.STORAGE,
|
|
961
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
962
|
+
text: `Failed to retrieve messages by ID: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
963
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
964
|
+
},
|
|
965
|
+
error$1
|
|
966
|
+
);
|
|
967
|
+
this.logger?.error(mastraError.toString());
|
|
968
|
+
this.logger?.trackException(mastraError);
|
|
969
|
+
throw mastraError;
|
|
807
970
|
}
|
|
808
971
|
}
|
|
809
|
-
async
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
972
|
+
async getMessagesPaginated({
|
|
973
|
+
threadId,
|
|
974
|
+
resourceId,
|
|
975
|
+
selectBy,
|
|
976
|
+
format
|
|
977
|
+
}) {
|
|
978
|
+
const { dateRange, page = 0, perPage: perPageInput } = selectBy?.pagination || {};
|
|
979
|
+
const { start: fromDate, end: toDate } = dateRange || {};
|
|
980
|
+
const perPage = perPageInput !== void 0 ? perPageInput : storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
981
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
982
|
+
const messages = [];
|
|
983
|
+
try {
|
|
984
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
985
|
+
if (selectBy?.include?.length) {
|
|
986
|
+
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
987
|
+
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
817
988
|
}
|
|
818
|
-
|
|
819
|
-
|
|
989
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("thread_id = ?", threadId);
|
|
990
|
+
if (fromDate) {
|
|
991
|
+
countQuery.andWhere("createdAt >= ?", storage.serializeDate(fromDate));
|
|
992
|
+
}
|
|
993
|
+
if (toDate) {
|
|
994
|
+
countQuery.andWhere("createdAt <= ?", storage.serializeDate(toDate));
|
|
995
|
+
}
|
|
996
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
997
|
+
const total = Number(countResult[0]?.count ?? 0);
|
|
998
|
+
if (total === 0 && messages.length === 0) {
|
|
999
|
+
return {
|
|
1000
|
+
messages: [],
|
|
1001
|
+
total: 0,
|
|
1002
|
+
page,
|
|
1003
|
+
perPage,
|
|
1004
|
+
hasMore: false
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
const excludeIds = messages.map((m) => m.id);
|
|
1008
|
+
const excludeCondition = excludeIds.length > 0 ? `AND id NOT IN (${excludeIds.map(() => "?").join(",")})` : "";
|
|
1009
|
+
let query;
|
|
1010
|
+
let queryParams = [threadId];
|
|
1011
|
+
if (fromDate) {
|
|
1012
|
+
queryParams.push(storage.serializeDate(fromDate));
|
|
1013
|
+
}
|
|
1014
|
+
if (toDate) {
|
|
1015
|
+
queryParams.push(storage.serializeDate(toDate));
|
|
1016
|
+
}
|
|
1017
|
+
if (excludeIds.length > 0) {
|
|
1018
|
+
queryParams.push(...excludeIds);
|
|
1019
|
+
}
|
|
1020
|
+
if (selectBy?.last && selectBy.last > 0) {
|
|
1021
|
+
query = `
|
|
1022
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1023
|
+
FROM ${fullTableName}
|
|
1024
|
+
WHERE thread_id = ?
|
|
1025
|
+
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1026
|
+
${toDate ? "AND createdAt <= ?" : ""}
|
|
1027
|
+
${excludeCondition}
|
|
1028
|
+
ORDER BY createdAt DESC
|
|
1029
|
+
LIMIT ?
|
|
1030
|
+
`;
|
|
1031
|
+
queryParams.push(selectBy.last);
|
|
1032
|
+
} else {
|
|
1033
|
+
query = `
|
|
1034
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
1035
|
+
FROM ${fullTableName}
|
|
1036
|
+
WHERE thread_id = ?
|
|
1037
|
+
${fromDate ? "AND createdAt >= ?" : ""}
|
|
1038
|
+
${toDate ? "AND createdAt <= ?" : ""}
|
|
1039
|
+
${excludeCondition}
|
|
1040
|
+
ORDER BY createdAt DESC
|
|
1041
|
+
LIMIT ? OFFSET ?
|
|
1042
|
+
`;
|
|
1043
|
+
queryParams.push(perPage, page * perPage);
|
|
1044
|
+
}
|
|
1045
|
+
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
1046
|
+
const processedMessages = results.map((message) => {
|
|
1047
|
+
const processedMsg = {};
|
|
1048
|
+
for (const [key, value] of Object.entries(message)) {
|
|
1049
|
+
if (key === `type` && value === `v2`) continue;
|
|
1050
|
+
processedMsg[key] = deserializeValue(value);
|
|
1051
|
+
}
|
|
1052
|
+
return processedMsg;
|
|
1053
|
+
});
|
|
1054
|
+
if (selectBy?.last) {
|
|
1055
|
+
processedMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
1056
|
+
}
|
|
1057
|
+
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
1058
|
+
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
1059
|
+
return {
|
|
1060
|
+
messages,
|
|
1061
|
+
total,
|
|
1062
|
+
page,
|
|
1063
|
+
perPage,
|
|
1064
|
+
hasMore: selectBy?.last ? false : page * perPage + messages.length < total
|
|
1065
|
+
};
|
|
1066
|
+
} catch (error$1) {
|
|
1067
|
+
const mastraError = new error.MastraError(
|
|
1068
|
+
{
|
|
1069
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_PAGINATED_ERROR",
|
|
1070
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1071
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1072
|
+
text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1073
|
+
details: { threadId, resourceId: resourceId ?? "" }
|
|
1074
|
+
},
|
|
1075
|
+
error$1
|
|
1076
|
+
);
|
|
1077
|
+
this.logger?.error(mastraError.toString());
|
|
1078
|
+
this.logger?.trackException(mastraError);
|
|
1079
|
+
return {
|
|
1080
|
+
messages: [],
|
|
1081
|
+
total: 0,
|
|
1082
|
+
page,
|
|
1083
|
+
perPage,
|
|
1084
|
+
hasMore: false
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
820
1087
|
}
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
const fullTableName = this.getTableName(
|
|
1088
|
+
async updateMessages(args) {
|
|
1089
|
+
const { messages } = args;
|
|
1090
|
+
this.logger.debug("Updating messages", { count: messages.length });
|
|
1091
|
+
if (!messages.length) {
|
|
1092
|
+
return [];
|
|
1093
|
+
}
|
|
1094
|
+
const messageIds = messages.map((m) => m.id);
|
|
1095
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
1096
|
+
const threadsTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
829
1097
|
try {
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
return this.serializeValue(value);
|
|
842
|
-
});
|
|
843
|
-
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
844
|
-
const { sql, params } = query.build();
|
|
845
|
-
await this.executeQuery({ sql, params });
|
|
1098
|
+
const placeholders = messageIds.map(() => "?").join(",");
|
|
1099
|
+
const selectQuery = `SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId FROM ${fullTableName} WHERE id IN (${placeholders})`;
|
|
1100
|
+
const existingMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
1101
|
+
if (existingMessages.length === 0) {
|
|
1102
|
+
return [];
|
|
1103
|
+
}
|
|
1104
|
+
const parsedExistingMessages = existingMessages.map((msg) => {
|
|
1105
|
+
if (typeof msg.content === "string") {
|
|
1106
|
+
try {
|
|
1107
|
+
msg.content = JSON.parse(msg.content);
|
|
1108
|
+
} catch {
|
|
846
1109
|
}
|
|
847
1110
|
}
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
1111
|
+
return msg;
|
|
1112
|
+
});
|
|
1113
|
+
const threadIdsToUpdate = /* @__PURE__ */ new Set();
|
|
1114
|
+
const updateQueries = [];
|
|
1115
|
+
for (const existingMessage of parsedExistingMessages) {
|
|
1116
|
+
const updatePayload = messages.find((m) => m.id === existingMessage.id);
|
|
1117
|
+
if (!updatePayload) continue;
|
|
1118
|
+
const { id, ...fieldsToUpdate } = updatePayload;
|
|
1119
|
+
if (Object.keys(fieldsToUpdate).length === 0) continue;
|
|
1120
|
+
threadIdsToUpdate.add(existingMessage.threadId);
|
|
1121
|
+
if ("threadId" in updatePayload && updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
|
|
1122
|
+
threadIdsToUpdate.add(updatePayload.threadId);
|
|
1123
|
+
}
|
|
1124
|
+
const setClauses = [];
|
|
1125
|
+
const values = [];
|
|
1126
|
+
const updatableFields = { ...fieldsToUpdate };
|
|
1127
|
+
if (updatableFields.content) {
|
|
1128
|
+
const existingContent = existingMessage.content || {};
|
|
1129
|
+
const newContent = {
|
|
1130
|
+
...existingContent,
|
|
1131
|
+
...updatableFields.content,
|
|
1132
|
+
// Deep merge metadata if it exists on both
|
|
1133
|
+
...existingContent?.metadata && updatableFields.content.metadata ? {
|
|
1134
|
+
metadata: {
|
|
1135
|
+
...existingContent.metadata,
|
|
1136
|
+
...updatableFields.content.metadata
|
|
1137
|
+
}
|
|
1138
|
+
} : {}
|
|
1139
|
+
};
|
|
1140
|
+
setClauses.push(`content = ?`);
|
|
1141
|
+
values.push(JSON.stringify(newContent));
|
|
1142
|
+
delete updatableFields.content;
|
|
1143
|
+
}
|
|
1144
|
+
for (const key in updatableFields) {
|
|
1145
|
+
if (Object.prototype.hasOwnProperty.call(updatableFields, key)) {
|
|
1146
|
+
const dbColumn = key === "threadId" ? "thread_id" : key;
|
|
1147
|
+
setClauses.push(`${dbColumn} = ?`);
|
|
1148
|
+
values.push(updatableFields[key]);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
if (setClauses.length > 0) {
|
|
1152
|
+
values.push(id);
|
|
1153
|
+
const updateQuery = `UPDATE ${fullTableName} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
1154
|
+
updateQueries.push({ sql: updateQuery, params: values });
|
|
1155
|
+
}
|
|
851
1156
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
1157
|
+
for (const query of updateQueries) {
|
|
1158
|
+
await this.operations.executeQuery(query);
|
|
1159
|
+
}
|
|
1160
|
+
if (threadIdsToUpdate.size > 0) {
|
|
1161
|
+
const threadPlaceholders = Array.from(threadIdsToUpdate).map(() => "?").join(",");
|
|
1162
|
+
const threadUpdateQuery = `UPDATE ${threadsTableName} SET updatedAt = ? WHERE id IN (${threadPlaceholders})`;
|
|
1163
|
+
const threadUpdateParams = [(/* @__PURE__ */ new Date()).toISOString(), ...Array.from(threadIdsToUpdate)];
|
|
1164
|
+
await this.operations.executeQuery({ sql: threadUpdateQuery, params: threadUpdateParams });
|
|
1165
|
+
}
|
|
1166
|
+
const updatedMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
1167
|
+
return updatedMessages.map((message) => {
|
|
1168
|
+
if (typeof message.content === "string") {
|
|
1169
|
+
try {
|
|
1170
|
+
message.content = JSON.parse(message.content);
|
|
1171
|
+
} catch {
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
return message;
|
|
856
1175
|
});
|
|
857
|
-
|
|
1176
|
+
} catch (error$1) {
|
|
1177
|
+
throw new error.MastraError(
|
|
1178
|
+
{
|
|
1179
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_MESSAGES_FAILED",
|
|
1180
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1181
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1182
|
+
details: { count: messages.length }
|
|
1183
|
+
},
|
|
1184
|
+
error$1
|
|
1185
|
+
);
|
|
858
1186
|
}
|
|
859
1187
|
}
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
1188
|
+
};
|
|
1189
|
+
var StoreOperationsD1 = class extends storage.StoreOperations {
|
|
1190
|
+
client;
|
|
1191
|
+
binding;
|
|
1192
|
+
tablePrefix;
|
|
1193
|
+
constructor(config) {
|
|
1194
|
+
super();
|
|
1195
|
+
this.client = config.client;
|
|
1196
|
+
this.binding = config.binding;
|
|
1197
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
1198
|
+
}
|
|
1199
|
+
async hasColumn(table, column) {
|
|
1200
|
+
const fullTableName = table.startsWith(this.tablePrefix) ? table : `${this.tablePrefix}${table}`;
|
|
1201
|
+
const sql = `PRAGMA table_info(${fullTableName});`;
|
|
1202
|
+
const result = await this.executeQuery({ sql, params: [] });
|
|
1203
|
+
if (!result || !Array.isArray(result)) return false;
|
|
1204
|
+
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
1205
|
+
}
|
|
1206
|
+
getTableName(tableName) {
|
|
1207
|
+
return `${this.tablePrefix}${tableName}`;
|
|
1208
|
+
}
|
|
1209
|
+
formatSqlParams(params) {
|
|
1210
|
+
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
1211
|
+
}
|
|
1212
|
+
async executeWorkersBindingQuery({
|
|
1213
|
+
sql,
|
|
1214
|
+
params = [],
|
|
1215
|
+
first = false
|
|
866
1216
|
}) {
|
|
867
|
-
|
|
1217
|
+
if (!this.binding) {
|
|
1218
|
+
throw new Error("Workers binding is not configured");
|
|
1219
|
+
}
|
|
868
1220
|
try {
|
|
869
|
-
const
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
1221
|
+
const statement = this.binding.prepare(sql);
|
|
1222
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1223
|
+
let result;
|
|
1224
|
+
if (formattedParams.length > 0) {
|
|
1225
|
+
if (first) {
|
|
1226
|
+
result = await statement.bind(...formattedParams).first();
|
|
1227
|
+
if (!result) return null;
|
|
1228
|
+
return result;
|
|
1229
|
+
} else {
|
|
1230
|
+
result = await statement.bind(...formattedParams).all();
|
|
1231
|
+
const results = result.results || [];
|
|
1232
|
+
return results;
|
|
1233
|
+
}
|
|
1234
|
+
} else {
|
|
1235
|
+
if (first) {
|
|
1236
|
+
result = await statement.first();
|
|
1237
|
+
if (!result) return null;
|
|
1238
|
+
return result;
|
|
1239
|
+
} else {
|
|
1240
|
+
result = await statement.all();
|
|
1241
|
+
const results = result.results || [];
|
|
1242
|
+
return results;
|
|
879
1243
|
}
|
|
880
1244
|
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
})) : [];
|
|
892
|
-
} catch (error) {
|
|
893
|
-
this.logger.error("Error getting traces:", { message: error instanceof Error ? error.message : String(error) });
|
|
894
|
-
return [];
|
|
1245
|
+
} catch (error$1) {
|
|
1246
|
+
throw new error.MastraError(
|
|
1247
|
+
{
|
|
1248
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_WORKERS_BINDING_QUERY_FAILED",
|
|
1249
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1250
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1251
|
+
details: { sql }
|
|
1252
|
+
},
|
|
1253
|
+
error$1
|
|
1254
|
+
);
|
|
895
1255
|
}
|
|
896
1256
|
}
|
|
897
|
-
async
|
|
898
|
-
|
|
1257
|
+
async executeRestQuery({
|
|
1258
|
+
sql,
|
|
1259
|
+
params = [],
|
|
1260
|
+
first = false
|
|
1261
|
+
}) {
|
|
1262
|
+
if (!this.client) {
|
|
1263
|
+
throw new Error("D1 client is not configured");
|
|
1264
|
+
}
|
|
899
1265
|
try {
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
1266
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1267
|
+
const response = await this.client.query({
|
|
1268
|
+
sql,
|
|
1269
|
+
params: formattedParams
|
|
1270
|
+
});
|
|
1271
|
+
const result = response.result || [];
|
|
1272
|
+
const results = result.flatMap((r) => r.results || []);
|
|
1273
|
+
if (first) {
|
|
1274
|
+
return results[0] || null;
|
|
905
1275
|
}
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
1276
|
+
return results;
|
|
1277
|
+
} catch (error$1) {
|
|
1278
|
+
throw new error.MastraError(
|
|
1279
|
+
{
|
|
1280
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_REST_QUERY_FAILED",
|
|
1281
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1282
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1283
|
+
details: { sql }
|
|
1284
|
+
},
|
|
1285
|
+
error$1
|
|
1286
|
+
);
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
async executeQuery(options) {
|
|
1290
|
+
if (this.binding) {
|
|
1291
|
+
return this.executeWorkersBindingQuery(options);
|
|
1292
|
+
} else if (this.client) {
|
|
1293
|
+
return this.executeRestQuery(options);
|
|
1294
|
+
} else {
|
|
1295
|
+
throw new Error("Neither binding nor client is configured");
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
async getTableColumns(tableName) {
|
|
1299
|
+
try {
|
|
1300
|
+
const sql = `PRAGMA table_info(${tableName})`;
|
|
1301
|
+
const result = await this.executeQuery({ sql });
|
|
1302
|
+
if (!result || !Array.isArray(result)) {
|
|
1303
|
+
return [];
|
|
1304
|
+
}
|
|
1305
|
+
return result.map((row) => ({
|
|
1306
|
+
name: row.name,
|
|
1307
|
+
type: row.type
|
|
1308
|
+
}));
|
|
925
1309
|
} catch (error) {
|
|
926
|
-
this.logger.
|
|
927
|
-
message: error instanceof Error ? error.message : String(error)
|
|
928
|
-
});
|
|
1310
|
+
this.logger.warn(`Failed to get table columns for ${tableName}:`, error);
|
|
929
1311
|
return [];
|
|
930
1312
|
}
|
|
931
1313
|
}
|
|
932
|
-
|
|
933
|
-
|
|
1314
|
+
serializeValue(value) {
|
|
1315
|
+
if (value === null || value === void 0) {
|
|
1316
|
+
return null;
|
|
1317
|
+
}
|
|
1318
|
+
if (value instanceof Date) {
|
|
1319
|
+
return value.toISOString();
|
|
1320
|
+
}
|
|
1321
|
+
if (typeof value === "object") {
|
|
1322
|
+
return JSON.stringify(value);
|
|
1323
|
+
}
|
|
1324
|
+
return value;
|
|
1325
|
+
}
|
|
1326
|
+
getSqlType(type) {
|
|
1327
|
+
switch (type) {
|
|
1328
|
+
case "bigint":
|
|
1329
|
+
return "INTEGER";
|
|
1330
|
+
// SQLite uses INTEGER for all integer sizes
|
|
1331
|
+
case "jsonb":
|
|
1332
|
+
return "TEXT";
|
|
1333
|
+
// Store JSON as TEXT in SQLite
|
|
1334
|
+
default:
|
|
1335
|
+
return super.getSqlType(type);
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
async createTable({
|
|
1339
|
+
tableName,
|
|
1340
|
+
schema
|
|
1341
|
+
}) {
|
|
1342
|
+
try {
|
|
1343
|
+
const fullTableName = this.getTableName(tableName);
|
|
1344
|
+
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
1345
|
+
const type = this.getSqlType(colDef.type);
|
|
1346
|
+
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
1347
|
+
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
1348
|
+
return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
|
|
1349
|
+
});
|
|
1350
|
+
const tableConstraints = [];
|
|
1351
|
+
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
1352
|
+
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
1353
|
+
}
|
|
1354
|
+
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
1355
|
+
const { sql, params } = query.build();
|
|
1356
|
+
await this.executeQuery({ sql, params });
|
|
1357
|
+
this.logger.debug(`Created table ${fullTableName}`);
|
|
1358
|
+
} catch (error$1) {
|
|
1359
|
+
throw new error.MastraError(
|
|
1360
|
+
{
|
|
1361
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CREATE_TABLE_FAILED",
|
|
1362
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1363
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1364
|
+
details: { tableName }
|
|
1365
|
+
},
|
|
1366
|
+
error$1
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
async clearTable({ tableName }) {
|
|
1371
|
+
try {
|
|
1372
|
+
const fullTableName = this.getTableName(tableName);
|
|
1373
|
+
const query = createSqlBuilder().delete(fullTableName);
|
|
1374
|
+
const { sql, params } = query.build();
|
|
1375
|
+
await this.executeQuery({ sql, params });
|
|
1376
|
+
this.logger.debug(`Cleared table ${fullTableName}`);
|
|
1377
|
+
} catch (error$1) {
|
|
1378
|
+
throw new error.MastraError(
|
|
1379
|
+
{
|
|
1380
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CLEAR_TABLE_FAILED",
|
|
1381
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1382
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1383
|
+
details: { tableName }
|
|
1384
|
+
},
|
|
1385
|
+
error$1
|
|
1386
|
+
);
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
async dropTable({ tableName }) {
|
|
1390
|
+
try {
|
|
1391
|
+
const fullTableName = this.getTableName(tableName);
|
|
1392
|
+
const sql = `DROP TABLE IF EXISTS ${fullTableName}`;
|
|
1393
|
+
await this.executeQuery({ sql });
|
|
1394
|
+
this.logger.debug(`Dropped table ${fullTableName}`);
|
|
1395
|
+
} catch (error$1) {
|
|
1396
|
+
throw new error.MastraError(
|
|
1397
|
+
{
|
|
1398
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_DROP_TABLE_FAILED",
|
|
1399
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1400
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1401
|
+
details: { tableName }
|
|
1402
|
+
},
|
|
1403
|
+
error$1
|
|
1404
|
+
);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
async alterTable(args) {
|
|
1408
|
+
try {
|
|
1409
|
+
const fullTableName = this.getTableName(args.tableName);
|
|
1410
|
+
const existingColumns = await this.getTableColumns(fullTableName);
|
|
1411
|
+
const existingColumnNames = new Set(existingColumns.map((col) => col.name));
|
|
1412
|
+
for (const [columnName, column] of Object.entries(args.schema)) {
|
|
1413
|
+
if (!existingColumnNames.has(columnName) && args.ifNotExists.includes(columnName)) {
|
|
1414
|
+
const sqlType = this.getSqlType(column.type);
|
|
1415
|
+
const defaultValue = this.getDefaultValue(column.type);
|
|
1416
|
+
const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${sqlType} ${defaultValue}`;
|
|
1417
|
+
await this.executeQuery({ sql });
|
|
1418
|
+
this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
} catch (error$1) {
|
|
1422
|
+
throw new error.MastraError(
|
|
1423
|
+
{
|
|
1424
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_ALTER_TABLE_FAILED",
|
|
1425
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1426
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1427
|
+
details: { tableName: args.tableName }
|
|
1428
|
+
},
|
|
1429
|
+
error$1
|
|
1430
|
+
);
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
async insert({ tableName, record }) {
|
|
1434
|
+
try {
|
|
1435
|
+
const fullTableName = this.getTableName(tableName);
|
|
1436
|
+
const processedRecord = await this.processRecord(record);
|
|
1437
|
+
const columns = Object.keys(processedRecord);
|
|
1438
|
+
const values = Object.values(processedRecord);
|
|
1439
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1440
|
+
const { sql, params } = query.build();
|
|
1441
|
+
await this.executeQuery({ sql, params });
|
|
1442
|
+
} catch (error$1) {
|
|
1443
|
+
throw new error.MastraError(
|
|
1444
|
+
{
|
|
1445
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_INSERT_FAILED",
|
|
1446
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1447
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1448
|
+
details: { tableName }
|
|
1449
|
+
},
|
|
1450
|
+
error$1
|
|
1451
|
+
);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
async batchInsert({ tableName, records }) {
|
|
1455
|
+
try {
|
|
1456
|
+
if (records.length === 0) return;
|
|
1457
|
+
const fullTableName = this.getTableName(tableName);
|
|
1458
|
+
const processedRecords = await Promise.all(records.map((record) => this.processRecord(record)));
|
|
1459
|
+
const columns = Object.keys(processedRecords[0] || {});
|
|
1460
|
+
for (const record of processedRecords) {
|
|
1461
|
+
const values = Object.values(record);
|
|
1462
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1463
|
+
const { sql, params } = query.build();
|
|
1464
|
+
await this.executeQuery({ sql, params });
|
|
1465
|
+
}
|
|
1466
|
+
} catch (error$1) {
|
|
1467
|
+
throw new error.MastraError(
|
|
1468
|
+
{
|
|
1469
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_BATCH_INSERT_FAILED",
|
|
1470
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1471
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1472
|
+
details: { tableName }
|
|
1473
|
+
},
|
|
1474
|
+
error$1
|
|
1475
|
+
);
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
async load({ tableName, keys }) {
|
|
1479
|
+
try {
|
|
1480
|
+
const fullTableName = this.getTableName(tableName);
|
|
1481
|
+
const query = createSqlBuilder().select("*").from(fullTableName);
|
|
1482
|
+
let firstKey = true;
|
|
1483
|
+
for (const [key, value] of Object.entries(keys)) {
|
|
1484
|
+
if (firstKey) {
|
|
1485
|
+
query.where(`${key} = ?`, value);
|
|
1486
|
+
firstKey = false;
|
|
1487
|
+
} else {
|
|
1488
|
+
query.andWhere(`${key} = ?`, value);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
query.orderBy("createdAt", "DESC");
|
|
1492
|
+
query.limit(1);
|
|
1493
|
+
const { sql, params } = query.build();
|
|
1494
|
+
const result = await this.executeQuery({ sql, params, first: true });
|
|
1495
|
+
if (!result) {
|
|
1496
|
+
return null;
|
|
1497
|
+
}
|
|
1498
|
+
const deserializedResult = {};
|
|
1499
|
+
for (const [key, value] of Object.entries(result)) {
|
|
1500
|
+
deserializedResult[key] = deserializeValue(value);
|
|
1501
|
+
}
|
|
1502
|
+
return deserializedResult;
|
|
1503
|
+
} catch (error$1) {
|
|
1504
|
+
throw new error.MastraError(
|
|
1505
|
+
{
|
|
1506
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_LOAD_FAILED",
|
|
1507
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1508
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1509
|
+
details: { tableName }
|
|
1510
|
+
},
|
|
1511
|
+
error$1
|
|
1512
|
+
);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
async processRecord(record) {
|
|
1516
|
+
const processed = {};
|
|
1517
|
+
for (const [key, value] of Object.entries(record)) {
|
|
1518
|
+
processed[key] = this.serializeValue(value);
|
|
1519
|
+
}
|
|
1520
|
+
return processed;
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Upsert multiple records in a batch operation
|
|
1524
|
+
* @param tableName The table to insert into
|
|
1525
|
+
* @param records The records to insert
|
|
1526
|
+
*/
|
|
1527
|
+
async batchUpsert({ tableName, records }) {
|
|
1528
|
+
if (records.length === 0) return;
|
|
1529
|
+
const fullTableName = this.getTableName(tableName);
|
|
1530
|
+
try {
|
|
1531
|
+
const batchSize = 50;
|
|
1532
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
1533
|
+
const batch = records.slice(i, i + batchSize);
|
|
1534
|
+
const recordsToInsert = batch;
|
|
1535
|
+
if (recordsToInsert.length > 0) {
|
|
1536
|
+
const firstRecord = recordsToInsert[0];
|
|
1537
|
+
const columns = Object.keys(firstRecord || {});
|
|
1538
|
+
for (const record of recordsToInsert) {
|
|
1539
|
+
const values = columns.map((col) => {
|
|
1540
|
+
if (!record) return null;
|
|
1541
|
+
const value = typeof col === "string" ? record[col] : null;
|
|
1542
|
+
return this.serializeValue(value);
|
|
1543
|
+
});
|
|
1544
|
+
const recordToUpsert = columns.reduce(
|
|
1545
|
+
(acc, col) => {
|
|
1546
|
+
if (col !== "createdAt") acc[col] = `excluded.${col}`;
|
|
1547
|
+
return acc;
|
|
1548
|
+
},
|
|
1549
|
+
{}
|
|
1550
|
+
);
|
|
1551
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], recordToUpsert);
|
|
1552
|
+
const { sql, params } = query.build();
|
|
1553
|
+
await this.executeQuery({ sql, params });
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
this.logger.debug(
|
|
1557
|
+
`Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
|
|
1558
|
+
);
|
|
1559
|
+
}
|
|
1560
|
+
this.logger.debug(`Successfully batch upserted ${records.length} records into ${tableName}`);
|
|
1561
|
+
} catch (error$1) {
|
|
1562
|
+
throw new error.MastraError(
|
|
1563
|
+
{
|
|
1564
|
+
id: "CLOUDFLARE_D1_STORAGE_BATCH_UPSERT_ERROR",
|
|
1565
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1566
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1567
|
+
text: `Failed to batch upsert into ${tableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1568
|
+
details: { tableName }
|
|
1569
|
+
},
|
|
1570
|
+
error$1
|
|
1571
|
+
);
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
};
|
|
1575
|
+
function transformScoreRow(row) {
|
|
1576
|
+
const deserialized = { ...row };
|
|
1577
|
+
deserialized.input = storage.safelyParseJSON(row.input);
|
|
1578
|
+
deserialized.output = storage.safelyParseJSON(row.output);
|
|
1579
|
+
deserialized.scorer = storage.safelyParseJSON(row.scorer);
|
|
1580
|
+
deserialized.preprocessStepResult = storage.safelyParseJSON(row.preprocessStepResult);
|
|
1581
|
+
deserialized.analyzeStepResult = storage.safelyParseJSON(row.analyzeStepResult);
|
|
1582
|
+
deserialized.metadata = storage.safelyParseJSON(row.metadata);
|
|
1583
|
+
deserialized.additionalContext = storage.safelyParseJSON(row.additionalContext);
|
|
1584
|
+
deserialized.runtimeContext = storage.safelyParseJSON(row.runtimeContext);
|
|
1585
|
+
deserialized.entity = storage.safelyParseJSON(row.entity);
|
|
1586
|
+
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1587
|
+
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
1588
|
+
return deserialized;
|
|
1589
|
+
}
|
|
1590
|
+
var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
1591
|
+
operations;
|
|
1592
|
+
constructor({ operations }) {
|
|
1593
|
+
super();
|
|
1594
|
+
this.operations = operations;
|
|
1595
|
+
}
|
|
1596
|
+
async getScoreById({ id }) {
|
|
1597
|
+
try {
|
|
1598
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1599
|
+
const query = createSqlBuilder().select("*").from(fullTableName).where("id = ?", id);
|
|
1600
|
+
const { sql, params } = query.build();
|
|
1601
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1602
|
+
if (!result) {
|
|
1603
|
+
return null;
|
|
1604
|
+
}
|
|
1605
|
+
return transformScoreRow(result);
|
|
1606
|
+
} catch (error$1) {
|
|
1607
|
+
throw new error.MastraError(
|
|
1608
|
+
{
|
|
1609
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORE_BY_ID_FAILED",
|
|
1610
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1611
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1612
|
+
},
|
|
1613
|
+
error$1
|
|
1614
|
+
);
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
async saveScore(score) {
|
|
1618
|
+
try {
|
|
1619
|
+
const id = crypto.randomUUID();
|
|
1620
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1621
|
+
const { input, ...rest } = score;
|
|
1622
|
+
const serializedRecord = {};
|
|
1623
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
1624
|
+
if (value !== null && value !== void 0) {
|
|
1625
|
+
if (typeof value === "object") {
|
|
1626
|
+
serializedRecord[key] = JSON.stringify(value);
|
|
1627
|
+
} else {
|
|
1628
|
+
serializedRecord[key] = value;
|
|
1629
|
+
}
|
|
1630
|
+
} else {
|
|
1631
|
+
serializedRecord[key] = null;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
serializedRecord.id = id;
|
|
1635
|
+
serializedRecord.input = JSON.stringify(input);
|
|
1636
|
+
serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1637
|
+
serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1638
|
+
const columns = Object.keys(serializedRecord);
|
|
1639
|
+
const values = Object.values(serializedRecord);
|
|
1640
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1641
|
+
const { sql, params } = query.build();
|
|
1642
|
+
await this.operations.executeQuery({ sql, params });
|
|
1643
|
+
const scoreFromDb = await this.getScoreById({ id });
|
|
1644
|
+
return { score: scoreFromDb };
|
|
1645
|
+
} catch (error$1) {
|
|
1646
|
+
throw new error.MastraError(
|
|
1647
|
+
{
|
|
1648
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED",
|
|
1649
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1650
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1651
|
+
},
|
|
1652
|
+
error$1
|
|
1653
|
+
);
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
async getScoresByScorerId({
|
|
1657
|
+
scorerId,
|
|
1658
|
+
entityId,
|
|
1659
|
+
entityType,
|
|
1660
|
+
source,
|
|
1661
|
+
pagination
|
|
1662
|
+
}) {
|
|
1663
|
+
try {
|
|
1664
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1665
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1666
|
+
if (entityId) {
|
|
1667
|
+
countQuery.andWhere("entityId = ?", entityId);
|
|
1668
|
+
}
|
|
1669
|
+
if (entityType) {
|
|
1670
|
+
countQuery.andWhere("entityType = ?", entityType);
|
|
1671
|
+
}
|
|
1672
|
+
if (source) {
|
|
1673
|
+
countQuery.andWhere("source = ?", source);
|
|
1674
|
+
}
|
|
1675
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1676
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1677
|
+
if (total === 0) {
|
|
1678
|
+
return {
|
|
1679
|
+
pagination: {
|
|
1680
|
+
total: 0,
|
|
1681
|
+
page: pagination.page,
|
|
1682
|
+
perPage: pagination.perPage,
|
|
1683
|
+
hasMore: false
|
|
1684
|
+
},
|
|
1685
|
+
scores: []
|
|
1686
|
+
};
|
|
1687
|
+
}
|
|
1688
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
|
|
1689
|
+
if (entityId) {
|
|
1690
|
+
selectQuery.andWhere("entityId = ?", entityId);
|
|
1691
|
+
}
|
|
1692
|
+
if (entityType) {
|
|
1693
|
+
selectQuery.andWhere("entityType = ?", entityType);
|
|
1694
|
+
}
|
|
1695
|
+
if (source) {
|
|
1696
|
+
selectQuery.andWhere("source = ?", source);
|
|
1697
|
+
}
|
|
1698
|
+
selectQuery.limit(pagination.perPage).offset(pagination.page * pagination.perPage);
|
|
1699
|
+
const { sql, params } = selectQuery.build();
|
|
1700
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1701
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1702
|
+
return {
|
|
1703
|
+
pagination: {
|
|
1704
|
+
total,
|
|
1705
|
+
page: pagination.page,
|
|
1706
|
+
perPage: pagination.perPage,
|
|
1707
|
+
hasMore: total > (pagination.page + 1) * pagination.perPage
|
|
1708
|
+
},
|
|
1709
|
+
scores
|
|
1710
|
+
};
|
|
1711
|
+
} catch (error$1) {
|
|
1712
|
+
throw new error.MastraError(
|
|
1713
|
+
{
|
|
1714
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SCORER_ID_FAILED",
|
|
1715
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1716
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1717
|
+
},
|
|
1718
|
+
error$1
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
async getScoresByRunId({
|
|
1723
|
+
runId,
|
|
1724
|
+
pagination
|
|
1725
|
+
}) {
|
|
1726
|
+
try {
|
|
1727
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1728
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1729
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1730
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1731
|
+
if (total === 0) {
|
|
1732
|
+
return {
|
|
1733
|
+
pagination: {
|
|
1734
|
+
total: 0,
|
|
1735
|
+
page: pagination.page,
|
|
1736
|
+
perPage: pagination.perPage,
|
|
1737
|
+
hasMore: false
|
|
1738
|
+
},
|
|
1739
|
+
scores: []
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
|
|
1743
|
+
const { sql, params } = selectQuery.build();
|
|
1744
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1745
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1746
|
+
return {
|
|
1747
|
+
pagination: {
|
|
1748
|
+
total,
|
|
1749
|
+
page: pagination.page,
|
|
1750
|
+
perPage: pagination.perPage,
|
|
1751
|
+
hasMore: total > (pagination.page + 1) * pagination.perPage
|
|
1752
|
+
},
|
|
1753
|
+
scores
|
|
1754
|
+
};
|
|
1755
|
+
} catch (error$1) {
|
|
1756
|
+
throw new error.MastraError(
|
|
1757
|
+
{
|
|
1758
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_RUN_ID_FAILED",
|
|
1759
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1760
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1761
|
+
},
|
|
1762
|
+
error$1
|
|
1763
|
+
);
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
async getScoresByEntityId({
|
|
1767
|
+
entityId,
|
|
1768
|
+
entityType,
|
|
1769
|
+
pagination
|
|
1770
|
+
}) {
|
|
1771
|
+
try {
|
|
1772
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1773
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1774
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1775
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1776
|
+
if (total === 0) {
|
|
1777
|
+
return {
|
|
1778
|
+
pagination: {
|
|
1779
|
+
total: 0,
|
|
1780
|
+
page: pagination.page,
|
|
1781
|
+
perPage: pagination.perPage,
|
|
1782
|
+
hasMore: false
|
|
1783
|
+
},
|
|
1784
|
+
scores: []
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
|
|
1788
|
+
const { sql, params } = selectQuery.build();
|
|
1789
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1790
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1791
|
+
return {
|
|
1792
|
+
pagination: {
|
|
1793
|
+
total,
|
|
1794
|
+
page: pagination.page,
|
|
1795
|
+
perPage: pagination.perPage,
|
|
1796
|
+
hasMore: total > (pagination.page + 1) * pagination.perPage
|
|
1797
|
+
},
|
|
1798
|
+
scores
|
|
1799
|
+
};
|
|
1800
|
+
} catch (error$1) {
|
|
1801
|
+
throw new error.MastraError(
|
|
1802
|
+
{
|
|
1803
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_ENTITY_ID_FAILED",
|
|
1804
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1805
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1806
|
+
},
|
|
1807
|
+
error$1
|
|
1808
|
+
);
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
};
|
|
1812
|
+
function isArrayOfRecords2(value) {
|
|
1813
|
+
return value && Array.isArray(value) && value.length > 0;
|
|
1814
|
+
}
|
|
1815
|
+
var TracesStorageD1 = class extends storage.TracesStorage {
|
|
1816
|
+
operations;
|
|
1817
|
+
constructor({ operations }) {
|
|
1818
|
+
super();
|
|
1819
|
+
this.operations = operations;
|
|
1820
|
+
}
|
|
1821
|
+
async getTraces(args) {
|
|
1822
|
+
const paginatedArgs = {
|
|
1823
|
+
name: args.name,
|
|
1824
|
+
scope: args.scope,
|
|
1825
|
+
page: args.page,
|
|
1826
|
+
perPage: args.perPage,
|
|
1827
|
+
attributes: args.attributes,
|
|
1828
|
+
filters: args.filters,
|
|
1829
|
+
dateRange: args.fromDate || args.toDate ? {
|
|
1830
|
+
start: args.fromDate,
|
|
1831
|
+
end: args.toDate
|
|
1832
|
+
} : void 0
|
|
1833
|
+
};
|
|
1834
|
+
try {
|
|
1835
|
+
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1836
|
+
return result.traces;
|
|
1837
|
+
} catch (error$1) {
|
|
1838
|
+
throw new error.MastraError(
|
|
1839
|
+
{
|
|
1840
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1841
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1842
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1843
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1844
|
+
details: {
|
|
1845
|
+
name: args.name ?? "",
|
|
1846
|
+
scope: args.scope ?? ""
|
|
1847
|
+
}
|
|
1848
|
+
},
|
|
1849
|
+
error$1
|
|
1850
|
+
);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
async getTracesPaginated(args) {
|
|
1854
|
+
const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
|
|
1855
|
+
const fromDate = dateRange?.start;
|
|
1856
|
+
const toDate = dateRange?.end;
|
|
1857
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_TRACES);
|
|
1858
|
+
try {
|
|
1859
|
+
const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
|
|
1860
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
|
|
1861
|
+
if (name) {
|
|
1862
|
+
dataQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1863
|
+
countQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1864
|
+
}
|
|
1865
|
+
if (scope) {
|
|
1866
|
+
dataQuery.andWhere("scope = ?", scope);
|
|
1867
|
+
countQuery.andWhere("scope = ?", scope);
|
|
1868
|
+
}
|
|
1869
|
+
if (attributes && Object.keys(attributes).length > 0) {
|
|
1870
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
1871
|
+
dataQuery.jsonLike("attributes", key, value);
|
|
1872
|
+
countQuery.jsonLike("attributes", key, value);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
if (fromDate) {
|
|
1876
|
+
const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
|
|
1877
|
+
dataQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1878
|
+
countQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1879
|
+
}
|
|
1880
|
+
if (toDate) {
|
|
1881
|
+
const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
|
|
1882
|
+
dataQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1883
|
+
countQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1884
|
+
}
|
|
1885
|
+
const allDataResult = await this.operations.executeQuery(
|
|
1886
|
+
createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
|
|
1887
|
+
);
|
|
1888
|
+
console.info("allDataResult", allDataResult);
|
|
1889
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1890
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1891
|
+
dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
|
|
1892
|
+
const results = await this.operations.executeQuery(dataQuery.build());
|
|
1893
|
+
const traces = isArrayOfRecords2(results) ? results.map(
|
|
1894
|
+
(trace) => ({
|
|
1895
|
+
...trace,
|
|
1896
|
+
attributes: deserializeValue(trace.attributes, "jsonb"),
|
|
1897
|
+
status: deserializeValue(trace.status, "jsonb"),
|
|
1898
|
+
events: deserializeValue(trace.events, "jsonb"),
|
|
1899
|
+
links: deserializeValue(trace.links, "jsonb"),
|
|
1900
|
+
other: deserializeValue(trace.other, "jsonb")
|
|
1901
|
+
})
|
|
1902
|
+
) : [];
|
|
1903
|
+
return {
|
|
1904
|
+
traces,
|
|
1905
|
+
total,
|
|
1906
|
+
page,
|
|
1907
|
+
perPage,
|
|
1908
|
+
hasMore: page * perPage + traces.length < total
|
|
1909
|
+
};
|
|
1910
|
+
} catch (error$1) {
|
|
1911
|
+
const mastraError = new error.MastraError(
|
|
1912
|
+
{
|
|
1913
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
|
|
1914
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1915
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1916
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1917
|
+
details: { name: name ?? "", scope: scope ?? "" }
|
|
1918
|
+
},
|
|
1919
|
+
error$1
|
|
1920
|
+
);
|
|
1921
|
+
this.logger?.error(mastraError.toString());
|
|
1922
|
+
this.logger?.trackException(mastraError);
|
|
1923
|
+
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
async batchTraceInsert({ records }) {
|
|
1927
|
+
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1928
|
+
await this.operations.batchInsert({
|
|
1929
|
+
tableName: storage.TABLE_TRACES,
|
|
1930
|
+
records
|
|
1931
|
+
});
|
|
1932
|
+
}
|
|
1933
|
+
};
|
|
1934
|
+
var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
1935
|
+
operations;
|
|
1936
|
+
constructor({ operations }) {
|
|
1937
|
+
super();
|
|
1938
|
+
this.operations = operations;
|
|
1939
|
+
}
|
|
1940
|
+
updateWorkflowResults({
|
|
1941
|
+
// workflowName,
|
|
1942
|
+
// runId,
|
|
1943
|
+
// stepId,
|
|
1944
|
+
// result,
|
|
1945
|
+
// runtimeContext,
|
|
1946
|
+
}) {
|
|
1947
|
+
throw new Error("Method not implemented.");
|
|
1948
|
+
}
|
|
1949
|
+
updateWorkflowState({
|
|
1950
|
+
// workflowName,
|
|
1951
|
+
// runId,
|
|
1952
|
+
// opts,
|
|
1953
|
+
}) {
|
|
1954
|
+
throw new Error("Method not implemented.");
|
|
1955
|
+
}
|
|
1956
|
+
async persistWorkflowSnapshot({
|
|
1957
|
+
workflowName,
|
|
1958
|
+
runId,
|
|
1959
|
+
resourceId,
|
|
1960
|
+
snapshot
|
|
1961
|
+
}) {
|
|
1962
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1963
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1964
|
+
const currentSnapshot = await this.operations.load({
|
|
1965
|
+
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
1966
|
+
keys: { workflow_name: workflowName, run_id: runId }
|
|
1967
|
+
});
|
|
1968
|
+
const persisting = currentSnapshot ? {
|
|
1969
|
+
...currentSnapshot,
|
|
1970
|
+
resourceId,
|
|
1971
|
+
snapshot: JSON.stringify(snapshot),
|
|
1972
|
+
updatedAt: now
|
|
1973
|
+
} : {
|
|
1974
|
+
workflow_name: workflowName,
|
|
1975
|
+
run_id: runId,
|
|
1976
|
+
resourceId,
|
|
1977
|
+
snapshot,
|
|
1978
|
+
createdAt: now,
|
|
1979
|
+
updatedAt: now
|
|
1980
|
+
};
|
|
1981
|
+
const processedRecord = await this.operations.processRecord(persisting);
|
|
1982
|
+
const columns = Object.keys(processedRecord);
|
|
1983
|
+
const values = Object.values(processedRecord);
|
|
1984
|
+
const updateMap = {
|
|
1985
|
+
snapshot: "excluded.snapshot",
|
|
1986
|
+
updatedAt: "excluded.updatedAt"
|
|
1987
|
+
};
|
|
1988
|
+
this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
|
|
1989
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
1990
|
+
const { sql, params } = query.build();
|
|
1991
|
+
try {
|
|
1992
|
+
await this.operations.executeQuery({ sql, params });
|
|
1993
|
+
} catch (error$1) {
|
|
1994
|
+
throw new error.MastraError(
|
|
1995
|
+
{
|
|
1996
|
+
id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
|
|
1997
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1998
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1999
|
+
text: `Failed to persist workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2000
|
+
details: { workflowName, runId }
|
|
2001
|
+
},
|
|
2002
|
+
error$1
|
|
2003
|
+
);
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
async loadWorkflowSnapshot(params) {
|
|
2007
|
+
const { workflowName, runId } = params;
|
|
2008
|
+
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
2009
|
+
try {
|
|
2010
|
+
const d = await this.operations.load({
|
|
2011
|
+
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
2012
|
+
keys: {
|
|
2013
|
+
workflow_name: workflowName,
|
|
2014
|
+
run_id: runId
|
|
2015
|
+
}
|
|
2016
|
+
});
|
|
2017
|
+
return d ? d.snapshot : null;
|
|
2018
|
+
} catch (error$1) {
|
|
2019
|
+
throw new error.MastraError(
|
|
2020
|
+
{
|
|
2021
|
+
id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
|
|
2022
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2023
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2024
|
+
text: `Failed to load workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2025
|
+
details: { workflowName, runId }
|
|
2026
|
+
},
|
|
2027
|
+
error$1
|
|
2028
|
+
);
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
parseWorkflowRun(row) {
|
|
2032
|
+
let parsedSnapshot = row.snapshot;
|
|
2033
|
+
if (typeof parsedSnapshot === "string") {
|
|
2034
|
+
try {
|
|
2035
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
2036
|
+
} catch (e) {
|
|
2037
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
return {
|
|
2041
|
+
workflowName: row.workflow_name,
|
|
2042
|
+
runId: row.run_id,
|
|
2043
|
+
snapshot: parsedSnapshot,
|
|
2044
|
+
createdAt: storage.ensureDate(row.createdAt),
|
|
2045
|
+
updatedAt: storage.ensureDate(row.updatedAt),
|
|
2046
|
+
resourceId: row.resourceId
|
|
2047
|
+
};
|
|
2048
|
+
}
|
|
2049
|
+
async getWorkflowRuns({
|
|
2050
|
+
workflowName,
|
|
2051
|
+
fromDate,
|
|
2052
|
+
toDate,
|
|
2053
|
+
limit,
|
|
2054
|
+
offset,
|
|
2055
|
+
resourceId
|
|
2056
|
+
} = {}) {
|
|
2057
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2058
|
+
try {
|
|
2059
|
+
const builder = createSqlBuilder().select().from(fullTableName);
|
|
2060
|
+
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
2061
|
+
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
2062
|
+
if (resourceId) {
|
|
2063
|
+
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
2064
|
+
if (hasResourceId) {
|
|
2065
|
+
builder.whereAnd("resourceId = ?", resourceId);
|
|
2066
|
+
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
2067
|
+
} else {
|
|
2068
|
+
console.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
if (fromDate) {
|
|
2072
|
+
builder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
2073
|
+
countBuilder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
2074
|
+
}
|
|
2075
|
+
if (toDate) {
|
|
2076
|
+
builder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2077
|
+
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2078
|
+
}
|
|
2079
|
+
builder.orderBy("createdAt", "DESC");
|
|
2080
|
+
if (typeof limit === "number") builder.limit(limit);
|
|
2081
|
+
if (typeof offset === "number") builder.offset(offset);
|
|
2082
|
+
const { sql, params } = builder.build();
|
|
2083
|
+
let total = 0;
|
|
2084
|
+
if (limit !== void 0 && offset !== void 0) {
|
|
2085
|
+
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
2086
|
+
const countResult = await this.operations.executeQuery({
|
|
2087
|
+
sql: countSql,
|
|
2088
|
+
params: countParams,
|
|
2089
|
+
first: true
|
|
2090
|
+
});
|
|
2091
|
+
total = Number(countResult?.count ?? 0);
|
|
2092
|
+
}
|
|
2093
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
2094
|
+
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
2095
|
+
return { runs, total: total || runs.length };
|
|
2096
|
+
} catch (error$1) {
|
|
2097
|
+
throw new error.MastraError(
|
|
2098
|
+
{
|
|
2099
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUNS_ERROR",
|
|
2100
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2101
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2102
|
+
text: `Failed to retrieve workflow runs: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2103
|
+
details: {
|
|
2104
|
+
workflowName: workflowName ?? "",
|
|
2105
|
+
resourceId: resourceId ?? ""
|
|
2106
|
+
}
|
|
2107
|
+
},
|
|
2108
|
+
error$1
|
|
2109
|
+
);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
async getWorkflowRunById({
|
|
2113
|
+
runId,
|
|
2114
|
+
workflowName
|
|
2115
|
+
}) {
|
|
2116
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2117
|
+
try {
|
|
2118
|
+
const conditions = [];
|
|
2119
|
+
const params = [];
|
|
2120
|
+
if (runId) {
|
|
2121
|
+
conditions.push("run_id = ?");
|
|
2122
|
+
params.push(runId);
|
|
2123
|
+
}
|
|
2124
|
+
if (workflowName) {
|
|
2125
|
+
conditions.push("workflow_name = ?");
|
|
2126
|
+
params.push(workflowName);
|
|
2127
|
+
}
|
|
2128
|
+
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
2129
|
+
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
2130
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
2131
|
+
if (!result) return null;
|
|
2132
|
+
return this.parseWorkflowRun(result);
|
|
2133
|
+
} catch (error$1) {
|
|
2134
|
+
throw new error.MastraError(
|
|
2135
|
+
{
|
|
2136
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUN_BY_ID_ERROR",
|
|
2137
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2138
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2139
|
+
text: `Failed to retrieve workflow run by ID: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2140
|
+
details: { runId, workflowName: workflowName ?? "" }
|
|
2141
|
+
},
|
|
2142
|
+
error$1
|
|
2143
|
+
);
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
};
|
|
2147
|
+
|
|
2148
|
+
// src/storage/index.ts
|
|
2149
|
+
var D1Store = class extends storage.MastraStorage {
|
|
2150
|
+
client;
|
|
2151
|
+
binding;
|
|
2152
|
+
// D1Database binding
|
|
2153
|
+
tablePrefix;
|
|
2154
|
+
stores;
|
|
2155
|
+
/**
|
|
2156
|
+
* Creates a new D1Store instance
|
|
2157
|
+
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
2158
|
+
*/
|
|
2159
|
+
constructor(config) {
|
|
2160
|
+
try {
|
|
2161
|
+
super({ name: "D1" });
|
|
2162
|
+
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
2163
|
+
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
2164
|
+
}
|
|
2165
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
2166
|
+
if ("binding" in config) {
|
|
2167
|
+
if (!config.binding) {
|
|
2168
|
+
throw new Error("D1 binding is required when using Workers Binding API");
|
|
2169
|
+
}
|
|
2170
|
+
this.binding = config.binding;
|
|
2171
|
+
this.logger.info("Using D1 Workers Binding API");
|
|
2172
|
+
} else if ("client" in config) {
|
|
2173
|
+
if (!config.client) {
|
|
2174
|
+
throw new Error("D1 client is required when using D1ClientConfig");
|
|
2175
|
+
}
|
|
2176
|
+
this.client = config.client;
|
|
2177
|
+
this.logger.info("Using D1 Client");
|
|
2178
|
+
} else {
|
|
2179
|
+
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
2180
|
+
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
2181
|
+
}
|
|
2182
|
+
const cfClient = new Cloudflare__default.default({
|
|
2183
|
+
apiToken: config.apiToken
|
|
2184
|
+
});
|
|
2185
|
+
this.client = {
|
|
2186
|
+
query: ({ sql, params }) => {
|
|
2187
|
+
return cfClient.d1.database.query(config.databaseId, {
|
|
2188
|
+
account_id: config.accountId,
|
|
2189
|
+
sql,
|
|
2190
|
+
params
|
|
2191
|
+
});
|
|
2192
|
+
}
|
|
2193
|
+
};
|
|
2194
|
+
this.logger.info("Using D1 REST API");
|
|
2195
|
+
}
|
|
2196
|
+
} catch (error$1) {
|
|
2197
|
+
throw new error.MastraError(
|
|
2198
|
+
{
|
|
2199
|
+
id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
|
|
2200
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2201
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2202
|
+
text: "Error initializing D1Store"
|
|
2203
|
+
},
|
|
2204
|
+
error$1
|
|
2205
|
+
);
|
|
2206
|
+
}
|
|
2207
|
+
const operations = new StoreOperationsD1({
|
|
2208
|
+
client: this.client,
|
|
2209
|
+
binding: this.binding,
|
|
2210
|
+
tablePrefix: this.tablePrefix
|
|
2211
|
+
});
|
|
2212
|
+
const scores = new ScoresStorageD1({
|
|
2213
|
+
operations
|
|
2214
|
+
});
|
|
2215
|
+
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2216
|
+
operations
|
|
2217
|
+
});
|
|
2218
|
+
const traces = new TracesStorageD1({
|
|
2219
|
+
operations
|
|
2220
|
+
});
|
|
2221
|
+
const workflows = new WorkflowsStorageD1({
|
|
2222
|
+
operations
|
|
2223
|
+
});
|
|
2224
|
+
const memory = new MemoryStorageD1({
|
|
2225
|
+
operations
|
|
2226
|
+
});
|
|
2227
|
+
this.stores = {
|
|
2228
|
+
operations,
|
|
2229
|
+
scores,
|
|
2230
|
+
legacyEvals,
|
|
2231
|
+
traces,
|
|
2232
|
+
workflows,
|
|
2233
|
+
memory
|
|
2234
|
+
};
|
|
2235
|
+
}
|
|
2236
|
+
get supports() {
|
|
2237
|
+
return {
|
|
2238
|
+
selectByIncludeResourceScope: true,
|
|
2239
|
+
resourceWorkingMemory: true,
|
|
2240
|
+
hasColumn: true,
|
|
2241
|
+
createTable: true,
|
|
2242
|
+
deleteMessages: false
|
|
2243
|
+
};
|
|
2244
|
+
}
|
|
2245
|
+
async createTable({
|
|
2246
|
+
tableName,
|
|
2247
|
+
schema
|
|
2248
|
+
}) {
|
|
2249
|
+
return this.stores.operations.createTable({ tableName, schema });
|
|
2250
|
+
}
|
|
2251
|
+
/**
|
|
2252
|
+
* Alters table schema to add columns if they don't exist
|
|
2253
|
+
* @param tableName Name of the table
|
|
2254
|
+
* @param schema Schema of the table
|
|
2255
|
+
* @param ifNotExists Array of column names to add if they don't exist
|
|
2256
|
+
*/
|
|
2257
|
+
async alterTable({
|
|
2258
|
+
tableName,
|
|
2259
|
+
schema,
|
|
2260
|
+
ifNotExists
|
|
2261
|
+
}) {
|
|
2262
|
+
return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
|
|
2263
|
+
}
|
|
2264
|
+
async clearTable({ tableName }) {
|
|
2265
|
+
return this.stores.operations.clearTable({ tableName });
|
|
2266
|
+
}
|
|
2267
|
+
async dropTable({ tableName }) {
|
|
2268
|
+
return this.stores.operations.dropTable({ tableName });
|
|
2269
|
+
}
|
|
2270
|
+
async hasColumn(table, column) {
|
|
2271
|
+
return this.stores.operations.hasColumn(table, column);
|
|
2272
|
+
}
|
|
2273
|
+
async insert({ tableName, record }) {
|
|
2274
|
+
return this.stores.operations.insert({ tableName, record });
|
|
2275
|
+
}
|
|
2276
|
+
async load({ tableName, keys }) {
|
|
2277
|
+
return this.stores.operations.load({ tableName, keys });
|
|
2278
|
+
}
|
|
2279
|
+
async getThreadById({ threadId }) {
|
|
2280
|
+
return this.stores.memory.getThreadById({ threadId });
|
|
2281
|
+
}
|
|
2282
|
+
/**
|
|
2283
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
2284
|
+
*/
|
|
2285
|
+
async getThreadsByResourceId({ resourceId }) {
|
|
2286
|
+
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2287
|
+
}
|
|
2288
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
2289
|
+
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2290
|
+
}
|
|
2291
|
+
async saveThread({ thread }) {
|
|
2292
|
+
return this.stores.memory.saveThread({ thread });
|
|
2293
|
+
}
|
|
2294
|
+
async updateThread({
|
|
2295
|
+
id,
|
|
2296
|
+
title,
|
|
2297
|
+
metadata
|
|
2298
|
+
}) {
|
|
2299
|
+
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2300
|
+
}
|
|
2301
|
+
async deleteThread({ threadId }) {
|
|
2302
|
+
return this.stores.memory.deleteThread({ threadId });
|
|
2303
|
+
}
|
|
2304
|
+
async saveMessages(args) {
|
|
2305
|
+
return this.stores.memory.saveMessages(args);
|
|
2306
|
+
}
|
|
2307
|
+
async getMessages({
|
|
2308
|
+
threadId,
|
|
2309
|
+
selectBy,
|
|
2310
|
+
format
|
|
2311
|
+
}) {
|
|
2312
|
+
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2313
|
+
}
|
|
2314
|
+
async getMessagesById({
|
|
2315
|
+
messageIds,
|
|
2316
|
+
format
|
|
2317
|
+
}) {
|
|
2318
|
+
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2319
|
+
}
|
|
2320
|
+
async getMessagesPaginated({
|
|
2321
|
+
threadId,
|
|
2322
|
+
selectBy,
|
|
2323
|
+
format
|
|
2324
|
+
}) {
|
|
2325
|
+
return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
|
|
2326
|
+
}
|
|
2327
|
+
async updateWorkflowResults({
|
|
2328
|
+
workflowName,
|
|
2329
|
+
runId,
|
|
2330
|
+
stepId,
|
|
2331
|
+
result,
|
|
2332
|
+
runtimeContext
|
|
2333
|
+
}) {
|
|
2334
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
|
|
2335
|
+
}
|
|
2336
|
+
async updateWorkflowState({
|
|
2337
|
+
workflowName,
|
|
2338
|
+
runId,
|
|
2339
|
+
opts
|
|
2340
|
+
}) {
|
|
2341
|
+
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2342
|
+
}
|
|
2343
|
+
async persistWorkflowSnapshot({
|
|
2344
|
+
workflowName,
|
|
2345
|
+
runId,
|
|
2346
|
+
resourceId,
|
|
2347
|
+
snapshot
|
|
2348
|
+
}) {
|
|
2349
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2350
|
+
}
|
|
2351
|
+
async loadWorkflowSnapshot(params) {
|
|
2352
|
+
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2353
|
+
}
|
|
2354
|
+
async getWorkflowRuns({
|
|
2355
|
+
workflowName,
|
|
2356
|
+
fromDate,
|
|
2357
|
+
toDate,
|
|
2358
|
+
limit,
|
|
2359
|
+
offset,
|
|
2360
|
+
resourceId
|
|
2361
|
+
} = {}) {
|
|
2362
|
+
return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2363
|
+
}
|
|
2364
|
+
async getWorkflowRunById({
|
|
2365
|
+
runId,
|
|
2366
|
+
workflowName
|
|
2367
|
+
}) {
|
|
2368
|
+
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2369
|
+
}
|
|
2370
|
+
/**
|
|
2371
|
+
* Insert multiple records in a batch operation
|
|
2372
|
+
* @param tableName The table to insert into
|
|
2373
|
+
* @param records The records to insert
|
|
2374
|
+
*/
|
|
2375
|
+
async batchInsert({ tableName, records }) {
|
|
2376
|
+
return this.stores.operations.batchInsert({ tableName, records });
|
|
2377
|
+
}
|
|
2378
|
+
/**
|
|
2379
|
+
* @deprecated use getTracesPaginated instead
|
|
2380
|
+
*/
|
|
2381
|
+
async getTraces(args) {
|
|
2382
|
+
return this.stores.traces.getTraces(args);
|
|
2383
|
+
}
|
|
2384
|
+
async getTracesPaginated(args) {
|
|
2385
|
+
return this.stores.traces.getTracesPaginated(args);
|
|
2386
|
+
}
|
|
2387
|
+
/**
|
|
2388
|
+
* @deprecated use getEvals instead
|
|
2389
|
+
*/
|
|
2390
|
+
async getEvalsByAgentName(agentName, type) {
|
|
2391
|
+
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2392
|
+
}
|
|
2393
|
+
async getEvals(options) {
|
|
2394
|
+
return this.stores.legacyEvals.getEvals(options);
|
|
2395
|
+
}
|
|
2396
|
+
async updateMessages(_args) {
|
|
2397
|
+
return this.stores.memory.updateMessages(_args);
|
|
2398
|
+
}
|
|
2399
|
+
async getResourceById({ resourceId }) {
|
|
2400
|
+
return this.stores.memory.getResourceById({ resourceId });
|
|
2401
|
+
}
|
|
2402
|
+
async saveResource({ resource }) {
|
|
2403
|
+
return this.stores.memory.saveResource({ resource });
|
|
2404
|
+
}
|
|
2405
|
+
async updateResource({
|
|
2406
|
+
resourceId,
|
|
2407
|
+
workingMemory,
|
|
2408
|
+
metadata
|
|
2409
|
+
}) {
|
|
2410
|
+
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
2411
|
+
}
|
|
2412
|
+
async getScoreById({ id: _id }) {
|
|
2413
|
+
return this.stores.scores.getScoreById({ id: _id });
|
|
2414
|
+
}
|
|
2415
|
+
async saveScore(_score) {
|
|
2416
|
+
return this.stores.scores.saveScore(_score);
|
|
2417
|
+
}
|
|
2418
|
+
async getScoresByRunId({
|
|
2419
|
+
runId: _runId,
|
|
2420
|
+
pagination: _pagination
|
|
2421
|
+
}) {
|
|
2422
|
+
return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2423
|
+
}
|
|
2424
|
+
async getScoresByEntityId({
|
|
2425
|
+
entityId: _entityId,
|
|
2426
|
+
entityType: _entityType,
|
|
2427
|
+
pagination: _pagination
|
|
2428
|
+
}) {
|
|
2429
|
+
return this.stores.scores.getScoresByEntityId({
|
|
2430
|
+
entityId: _entityId,
|
|
2431
|
+
entityType: _entityType,
|
|
2432
|
+
pagination: _pagination
|
|
2433
|
+
});
|
|
2434
|
+
}
|
|
2435
|
+
async getScoresByScorerId({
|
|
2436
|
+
scorerId,
|
|
2437
|
+
pagination,
|
|
2438
|
+
entityId,
|
|
2439
|
+
entityType,
|
|
2440
|
+
source
|
|
2441
|
+
}) {
|
|
2442
|
+
return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
934
2443
|
}
|
|
935
2444
|
/**
|
|
936
2445
|
* Close the database connection
|
|
@@ -942,3 +2451,5 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
942
2451
|
};
|
|
943
2452
|
|
|
944
2453
|
exports.D1Store = D1Store;
|
|
2454
|
+
//# sourceMappingURL=index.cjs.map
|
|
2455
|
+
//# sourceMappingURL=index.cjs.map
|