@mastra/cloudflare-d1 0.0.0-redis-cloud-transporter-20250508203756 → 0.0.0-remove-unused-import-20250909212718
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 +1078 -0
- package/LICENSE.md +11 -42
- package/README.md +15 -0
- package/dist/index.cjs +1979 -586
- 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 +1967 -574
- 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 +51 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +285 -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 -331
- package/dist/_tsup-dts-rollup.d.ts +0 -331
- 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) {
|
|
@@ -87,27 +95,33 @@ var SqlBuilder = class {
|
|
|
87
95
|
* @param updateMap Object mapping columns to update to their new value (e.g. { name: 'excluded.name' })
|
|
88
96
|
*/
|
|
89
97
|
insert(table, columns, values, conflictColumns, updateMap) {
|
|
90
|
-
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(", ");
|
|
91
101
|
if (conflictColumns && updateMap) {
|
|
102
|
+
const parsedConflictColumns = conflictColumns.map((col) => utils.parseSqlIdentifier(col, "column name"));
|
|
92
103
|
const updateClause = Object.entries(updateMap).map(([col, expr]) => `${col} = ${expr}`).join(", ");
|
|
93
|
-
this.sql = `INSERT INTO ${
|
|
104
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${parsedConflictColumns.join(", ")}) DO UPDATE SET ${updateClause}`;
|
|
94
105
|
this.params.push(...values);
|
|
95
106
|
return this;
|
|
96
107
|
}
|
|
97
|
-
this.sql = `INSERT INTO ${
|
|
108
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders})`;
|
|
98
109
|
this.params.push(...values);
|
|
99
110
|
return this;
|
|
100
111
|
}
|
|
101
112
|
// Update operations
|
|
102
113
|
update(table, columns, values) {
|
|
103
|
-
const
|
|
104
|
-
|
|
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}`;
|
|
105
118
|
this.params.push(...values);
|
|
106
119
|
return this;
|
|
107
120
|
}
|
|
108
121
|
// Delete operations
|
|
109
122
|
delete(table) {
|
|
110
|
-
|
|
123
|
+
const parsedTableName = utils.parseSqlIdentifier(table, "table name");
|
|
124
|
+
this.sql = `DELETE FROM ${parsedTableName}`;
|
|
111
125
|
return this;
|
|
112
126
|
}
|
|
113
127
|
/**
|
|
@@ -118,9 +132,16 @@ var SqlBuilder = class {
|
|
|
118
132
|
* @returns The builder instance
|
|
119
133
|
*/
|
|
120
134
|
createTable(table, columnDefinitions, tableConstraints) {
|
|
121
|
-
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(", ");
|
|
122
143
|
const constraints = tableConstraints && tableConstraints.length > 0 ? ", " + tableConstraints.join(", ") : "";
|
|
123
|
-
this.sql = `CREATE TABLE IF NOT EXISTS ${
|
|
144
|
+
this.sql = `CREATE TABLE IF NOT EXISTS ${parsedTableName} (${columns}${constraints})`;
|
|
124
145
|
return this;
|
|
125
146
|
}
|
|
126
147
|
/**
|
|
@@ -143,13 +164,10 @@ var SqlBuilder = class {
|
|
|
143
164
|
* @returns The builder instance
|
|
144
165
|
*/
|
|
145
166
|
createIndex(indexName, tableName, columnName, indexType = "") {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
raw(sql, ...params) {
|
|
151
|
-
this.sql = sql;
|
|
152
|
-
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})`;
|
|
153
171
|
return this;
|
|
154
172
|
}
|
|
155
173
|
/**
|
|
@@ -159,11 +177,12 @@ var SqlBuilder = class {
|
|
|
159
177
|
* @param exact If true, will not add % wildcards
|
|
160
178
|
*/
|
|
161
179
|
like(column, value, exact = false) {
|
|
180
|
+
const parsedColumnName = utils.parseSqlIdentifier(column, "column name");
|
|
162
181
|
const likeValue = exact ? value : `%${value}%`;
|
|
163
182
|
if (this.whereAdded) {
|
|
164
|
-
this.sql += ` AND ${
|
|
183
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
165
184
|
} else {
|
|
166
|
-
this.sql += ` WHERE ${
|
|
185
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
167
186
|
this.whereAdded = true;
|
|
168
187
|
}
|
|
169
188
|
this.params.push(likeValue);
|
|
@@ -176,11 +195,13 @@ var SqlBuilder = class {
|
|
|
176
195
|
* @param value The value to match
|
|
177
196
|
*/
|
|
178
197
|
jsonLike(column, key, value) {
|
|
179
|
-
const
|
|
198
|
+
const parsedColumnName = utils.parseSqlIdentifier(column, "column name");
|
|
199
|
+
const parsedKey = utils.parseSqlIdentifier(key, "key name");
|
|
200
|
+
const jsonPattern = `%"${parsedKey}":"${value}"%`;
|
|
180
201
|
if (this.whereAdded) {
|
|
181
|
-
this.sql += ` AND ${
|
|
202
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
182
203
|
} else {
|
|
183
|
-
this.sql += ` WHERE ${
|
|
204
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
184
205
|
this.whereAdded = true;
|
|
185
206
|
}
|
|
186
207
|
this.params.push(jsonPattern);
|
|
@@ -210,332 +231,311 @@ var SqlBuilder = class {
|
|
|
210
231
|
function createSqlBuilder() {
|
|
211
232
|
return new SqlBuilder();
|
|
212
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
|
+
}
|
|
213
243
|
|
|
214
|
-
// src/storage/
|
|
244
|
+
// src/storage/domains/utils.ts
|
|
215
245
|
function isArrayOfRecords(value) {
|
|
216
246
|
return value && Array.isArray(value) && value.length > 0;
|
|
217
247
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
binding;
|
|
223
|
-
// D1Database binding
|
|
224
|
-
tablePrefix;
|
|
225
|
-
/**
|
|
226
|
-
* Creates a new D1Store instance
|
|
227
|
-
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
228
|
-
*/
|
|
229
|
-
constructor(config) {
|
|
230
|
-
super({ name: "D1" });
|
|
231
|
-
this.tablePrefix = config.tablePrefix || "";
|
|
232
|
-
if ("binding" in config) {
|
|
233
|
-
if (!config.binding) {
|
|
234
|
-
throw new Error("D1 binding is required when using Workers Binding API");
|
|
235
|
-
}
|
|
236
|
-
this.binding = config.binding;
|
|
237
|
-
this.logger.info("Using D1 Workers Binding API");
|
|
238
|
-
} else {
|
|
239
|
-
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
240
|
-
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
241
|
-
}
|
|
242
|
-
this.accountId = config.accountId;
|
|
243
|
-
this.databaseId = config.databaseId;
|
|
244
|
-
this.client = new Cloudflare__default.default({
|
|
245
|
-
apiToken: config.apiToken
|
|
246
|
-
});
|
|
247
|
-
this.logger.info("Using D1 REST API");
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
// Helper method to get the full table name with prefix
|
|
251
|
-
getTableName(tableName) {
|
|
252
|
-
return `${this.tablePrefix}${tableName}`;
|
|
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);
|
|
253
252
|
}
|
|
254
|
-
|
|
255
|
-
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
256
|
-
}
|
|
257
|
-
// Helper method to create SQL indexes for better query performance
|
|
258
|
-
async createIndexIfNotExists(tableName, columnName, indexType = "") {
|
|
259
|
-
const fullTableName = this.getTableName(tableName);
|
|
260
|
-
const indexName = `idx_${tableName}_${columnName}`;
|
|
253
|
+
if (type === "jsonb" && typeof value === "string") {
|
|
261
254
|
try {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
sql: checkSql,
|
|
266
|
-
params: checkParams,
|
|
267
|
-
first: true
|
|
268
|
-
});
|
|
269
|
-
if (!indexExists) {
|
|
270
|
-
const createQuery = createSqlBuilder().createIndex(indexName, fullTableName, columnName, indexType);
|
|
271
|
-
const { sql: createSql, params: createParams } = createQuery.build();
|
|
272
|
-
await this.executeQuery({ sql: createSql, params: createParams });
|
|
273
|
-
this.logger.debug(`Created index ${indexName} on ${fullTableName}(${columnName})`);
|
|
274
|
-
}
|
|
275
|
-
} catch (error) {
|
|
276
|
-
this.logger.error(`Error creating index on ${fullTableName}(${columnName}):`, {
|
|
277
|
-
message: error instanceof Error ? error.message : String(error)
|
|
278
|
-
});
|
|
255
|
+
return JSON.parse(value);
|
|
256
|
+
} catch {
|
|
257
|
+
return value;
|
|
279
258
|
}
|
|
280
259
|
}
|
|
281
|
-
|
|
282
|
-
sql,
|
|
283
|
-
params = [],
|
|
284
|
-
first = false
|
|
285
|
-
}) {
|
|
286
|
-
if (!this.binding) {
|
|
287
|
-
throw new Error("Workers binding is not configured");
|
|
288
|
-
}
|
|
260
|
+
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
289
261
|
try {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (formattedParams.length > 0) {
|
|
294
|
-
if (first) {
|
|
295
|
-
result = await statement.bind(...formattedParams).first();
|
|
296
|
-
if (!result) return null;
|
|
297
|
-
return result;
|
|
298
|
-
} else {
|
|
299
|
-
result = await statement.bind(...formattedParams).all();
|
|
300
|
-
const results = result.results || [];
|
|
301
|
-
if (result.meta) {
|
|
302
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
303
|
-
}
|
|
304
|
-
return results;
|
|
305
|
-
}
|
|
306
|
-
} else {
|
|
307
|
-
if (first) {
|
|
308
|
-
result = await statement.first();
|
|
309
|
-
if (!result) return null;
|
|
310
|
-
return result;
|
|
311
|
-
} else {
|
|
312
|
-
result = await statement.all();
|
|
313
|
-
const results = result.results || [];
|
|
314
|
-
if (result.meta) {
|
|
315
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
316
|
-
}
|
|
317
|
-
return results;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
} catch (workerError) {
|
|
321
|
-
this.logger.error("Workers Binding API error", {
|
|
322
|
-
message: workerError instanceof Error ? workerError.message : String(workerError),
|
|
323
|
-
sql
|
|
324
|
-
});
|
|
325
|
-
throw new Error(`D1 Workers API error: ${workerError.message}`);
|
|
262
|
+
return JSON.parse(value);
|
|
263
|
+
} catch {
|
|
264
|
+
return value;
|
|
326
265
|
}
|
|
327
266
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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);
|
|
335
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));
|
|
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();
|
|
336
304
|
try {
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
305
|
+
const countResult = await this.operations.executeQuery({
|
|
306
|
+
sql: countSql,
|
|
307
|
+
params: countParams,
|
|
308
|
+
first: true
|
|
341
309
|
});
|
|
342
|
-
const
|
|
343
|
-
const
|
|
344
|
-
if (
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
|
+
};
|
|
348
320
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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
|
+
};
|
|
354
349
|
});
|
|
355
|
-
|
|
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
|
+
);
|
|
356
369
|
}
|
|
357
370
|
}
|
|
358
371
|
/**
|
|
359
|
-
*
|
|
360
|
-
* @param options Query options including SQL, parameters, and whether to return only the first result
|
|
361
|
-
* @returns Query results as an array or a single object if first=true
|
|
372
|
+
* @deprecated use getEvals instead
|
|
362
373
|
*/
|
|
363
|
-
async
|
|
364
|
-
const
|
|
374
|
+
async getEvalsByAgentName(agentName, type) {
|
|
375
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_EVALS);
|
|
365
376
|
try {
|
|
366
|
-
|
|
367
|
-
if (
|
|
368
|
-
|
|
369
|
-
} else if (
|
|
370
|
-
|
|
371
|
-
} else {
|
|
372
|
-
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)");
|
|
373
382
|
}
|
|
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
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
serializeDate(date) {
|
|
408
|
-
if (!date) return void 0;
|
|
409
|
-
const dateObj = this.ensureDate(date);
|
|
410
|
-
return dateObj?.toISOString();
|
|
411
|
-
}
|
|
412
|
-
// Helper to serialize objects to JSON strings
|
|
413
|
-
serializeValue(value) {
|
|
414
|
-
if (value === null || value === void 0) return null;
|
|
415
|
-
if (value instanceof Date) {
|
|
416
|
-
return this.serializeDate(value);
|
|
417
|
-
}
|
|
418
|
-
if (typeof value === "object") {
|
|
419
|
-
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 [];
|
|
420
416
|
}
|
|
421
|
-
return value;
|
|
422
417
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
if (type === "jsonb" && typeof value === "string") {
|
|
430
|
-
try {
|
|
431
|
-
return JSON.parse(value);
|
|
432
|
-
} catch {
|
|
433
|
-
return value;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
437
|
-
try {
|
|
438
|
-
return JSON.parse(value);
|
|
439
|
-
} catch {
|
|
440
|
-
return value;
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
return value;
|
|
418
|
+
};
|
|
419
|
+
var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
420
|
+
operations;
|
|
421
|
+
constructor({ operations }) {
|
|
422
|
+
super();
|
|
423
|
+
this.operations = operations;
|
|
444
424
|
}
|
|
445
|
-
async
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
const fullTableName = this.getTableName(tableName);
|
|
450
|
-
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
451
|
-
const type = this.getSqlType(colDef.type);
|
|
452
|
-
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
453
|
-
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
454
|
-
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 }
|
|
455
429
|
});
|
|
456
|
-
|
|
457
|
-
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
458
|
-
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
459
|
-
}
|
|
460
|
-
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
461
|
-
const { sql, params } = query.build();
|
|
462
|
-
try {
|
|
463
|
-
await this.executeQuery({ sql, params });
|
|
464
|
-
this.logger.debug(`Created table ${fullTableName}`);
|
|
465
|
-
} catch (error) {
|
|
466
|
-
this.logger.error(`Error creating table ${fullTableName}:`, {
|
|
467
|
-
message: error instanceof Error ? error.message : String(error)
|
|
468
|
-
});
|
|
469
|
-
throw new Error(`Failed to create table ${fullTableName}: ${error}`);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
async clearTable({ tableName }) {
|
|
473
|
-
const fullTableName = this.getTableName(tableName);
|
|
430
|
+
if (!resource) return null;
|
|
474
431
|
try {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
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;
|
|
490
452
|
}
|
|
491
|
-
return processedRecord;
|
|
492
453
|
}
|
|
493
|
-
async
|
|
494
|
-
const fullTableName = this.getTableName(
|
|
495
|
-
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);
|
|
496
464
|
const columns = Object.keys(processedRecord);
|
|
497
465
|
const values = Object.values(processedRecord);
|
|
498
|
-
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);
|
|
499
473
|
const { sql, params } = query.build();
|
|
500
474
|
try {
|
|
501
|
-
await this.executeQuery({ sql, params });
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
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
|
+
);
|
|
506
488
|
}
|
|
507
489
|
}
|
|
508
|
-
async
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
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 });
|
|
519
505
|
}
|
|
520
|
-
|
|
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);
|
|
521
520
|
const { sql, params } = query.build();
|
|
522
521
|
try {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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
|
+
);
|
|
535
535
|
}
|
|
536
536
|
}
|
|
537
537
|
async getThreadById({ threadId }) {
|
|
538
|
-
const thread = await this.load({
|
|
538
|
+
const thread = await this.operations.load({
|
|
539
539
|
tableName: storage.TABLE_THREADS,
|
|
540
540
|
keys: { id: threadId }
|
|
541
541
|
});
|
|
@@ -543,47 +543,113 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
543
543
|
try {
|
|
544
544
|
return {
|
|
545
545
|
...thread,
|
|
546
|
-
createdAt:
|
|
547
|
-
updatedAt:
|
|
546
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
547
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
548
548
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
549
549
|
};
|
|
550
|
-
} catch (error) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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);
|
|
554
563
|
return null;
|
|
555
564
|
}
|
|
556
565
|
}
|
|
566
|
+
/**
|
|
567
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
568
|
+
*/
|
|
557
569
|
async getThreadsByResourceId({ resourceId }) {
|
|
558
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
570
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
559
571
|
try {
|
|
560
572
|
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
561
573
|
const { sql, params } = query.build();
|
|
562
|
-
const results = await this.executeQuery({ sql, params });
|
|
574
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
563
575
|
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
564
576
|
...thread,
|
|
565
|
-
createdAt:
|
|
566
|
-
updatedAt:
|
|
577
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
578
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
567
579
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
568
580
|
}));
|
|
569
|
-
} catch (error) {
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
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);
|
|
573
594
|
return [];
|
|
574
595
|
}
|
|
575
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
|
+
}
|
|
576
642
|
async saveThread({ thread }) {
|
|
577
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
643
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
578
644
|
const threadToSave = {
|
|
579
645
|
id: thread.id,
|
|
580
646
|
resourceId: thread.resourceId,
|
|
581
647
|
title: thread.title,
|
|
582
648
|
metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
|
|
583
|
-
createdAt: thread.createdAt,
|
|
584
|
-
updatedAt: thread.updatedAt
|
|
649
|
+
createdAt: thread.createdAt.toISOString(),
|
|
650
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
585
651
|
};
|
|
586
|
-
const processedRecord = await this.processRecord(threadToSave);
|
|
652
|
+
const processedRecord = await this.operations.processRecord(threadToSave);
|
|
587
653
|
const columns = Object.keys(processedRecord);
|
|
588
654
|
const values = Object.values(processedRecord);
|
|
589
655
|
const updateMap = {
|
|
@@ -596,12 +662,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
596
662
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
597
663
|
const { sql, params } = query.build();
|
|
598
664
|
try {
|
|
599
|
-
await this.executeQuery({ sql, params });
|
|
665
|
+
await this.operations.executeQuery({ sql, params });
|
|
600
666
|
return thread;
|
|
601
|
-
} catch (error) {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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
|
+
);
|
|
605
678
|
}
|
|
606
679
|
}
|
|
607
680
|
async updateThread({
|
|
@@ -610,20 +683,21 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
610
683
|
metadata
|
|
611
684
|
}) {
|
|
612
685
|
const thread = await this.getThreadById({ threadId: id });
|
|
613
|
-
if (!thread) {
|
|
614
|
-
throw new Error(`Thread ${id} not found`);
|
|
615
|
-
}
|
|
616
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
617
|
-
const mergedMetadata = {
|
|
618
|
-
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
619
|
-
...metadata
|
|
620
|
-
};
|
|
621
|
-
const columns = ["title", "metadata", "updatedAt"];
|
|
622
|
-
const values = [title, JSON.stringify(mergedMetadata), (/* @__PURE__ */ new Date()).toISOString()];
|
|
623
|
-
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
624
|
-
const { sql, params } = query.build();
|
|
625
686
|
try {
|
|
626
|
-
|
|
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 });
|
|
627
701
|
return {
|
|
628
702
|
...thread,
|
|
629
703
|
title,
|
|
@@ -631,43 +705,66 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
631
705
|
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
632
706
|
...metadata
|
|
633
707
|
},
|
|
634
|
-
updatedAt
|
|
708
|
+
updatedAt
|
|
635
709
|
};
|
|
636
|
-
} catch (error) {
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
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
|
+
);
|
|
640
721
|
}
|
|
641
722
|
}
|
|
642
723
|
async deleteThread({ threadId }) {
|
|
643
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
724
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
644
725
|
try {
|
|
645
726
|
const deleteThreadQuery = createSqlBuilder().delete(fullTableName).where("id = ?", threadId);
|
|
646
727
|
const { sql: threadSql, params: threadParams } = deleteThreadQuery.build();
|
|
647
|
-
await this.executeQuery({ sql: threadSql, params: threadParams });
|
|
648
|
-
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);
|
|
649
730
|
const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
|
|
650
731
|
const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
|
|
651
|
-
await this.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
652
|
-
} catch (error) {
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
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
|
+
);
|
|
657
744
|
}
|
|
658
745
|
}
|
|
659
|
-
|
|
660
|
-
|
|
746
|
+
async saveMessages(args) {
|
|
747
|
+
const { messages, format = "v1" } = args;
|
|
661
748
|
if (messages.length === 0) return [];
|
|
662
749
|
try {
|
|
663
750
|
const now = /* @__PURE__ */ new Date();
|
|
751
|
+
const threadId = messages[0]?.threadId;
|
|
664
752
|
for (const [i, message] of messages.entries()) {
|
|
665
753
|
if (!message.id) throw new Error(`Message at index ${i} missing id`);
|
|
666
|
-
if (!message.threadId)
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
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
|
+
}
|
|
766
|
+
const thread = await this.getThreadById({ threadId: message.threadId });
|
|
767
|
+
if (!thread) {
|
|
671
768
|
throw new Error(`Thread ${message.threadId} not found`);
|
|
672
769
|
}
|
|
673
770
|
}
|
|
@@ -679,74 +776,122 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
679
776
|
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
680
777
|
createdAt: createdAt.toISOString(),
|
|
681
778
|
role: message.role,
|
|
682
|
-
type: message.type
|
|
779
|
+
type: message.type || "v2",
|
|
780
|
+
resourceId: message.resourceId
|
|
683
781
|
};
|
|
684
782
|
});
|
|
685
|
-
await
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
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
|
+
]);
|
|
689
794
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
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
|
+
);
|
|
694
808
|
}
|
|
695
809
|
}
|
|
696
|
-
async
|
|
697
|
-
|
|
698
|
-
const
|
|
699
|
-
|
|
700
|
-
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
|
+
}) {
|
|
701
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 = [];
|
|
702
883
|
if (include.length) {
|
|
703
|
-
const
|
|
704
|
-
const nextMax = Math.max(...include.map((i) => i.withNextMessages || 0));
|
|
705
|
-
const includeIds = include.map((i) => i.id);
|
|
706
|
-
const sql2 = `
|
|
707
|
-
WITH ordered_messages AS (
|
|
708
|
-
SELECT
|
|
709
|
-
*,
|
|
710
|
-
ROW_NUMBER() OVER (ORDER BY createdAt DESC) AS row_num
|
|
711
|
-
FROM ${fullTableName}
|
|
712
|
-
WHERE thread_id = ?
|
|
713
|
-
)
|
|
714
|
-
SELECT
|
|
715
|
-
m.id,
|
|
716
|
-
m.content,
|
|
717
|
-
m.role,
|
|
718
|
-
m.type,
|
|
719
|
-
m.createdAt,
|
|
720
|
-
m.thread_id AS "threadId"
|
|
721
|
-
FROM ordered_messages m
|
|
722
|
-
WHERE m.id IN (${includeIds.map(() => "?").join(",")})
|
|
723
|
-
OR EXISTS (
|
|
724
|
-
SELECT 1 FROM ordered_messages target
|
|
725
|
-
WHERE target.id IN (${includeIds.map(() => "?").join(",")})
|
|
726
|
-
AND (
|
|
727
|
-
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
728
|
-
OR
|
|
729
|
-
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
730
|
-
)
|
|
731
|
-
)
|
|
732
|
-
ORDER BY m.createdAt DESC
|
|
733
|
-
`;
|
|
734
|
-
const params2 = [
|
|
735
|
-
threadId,
|
|
736
|
-
...includeIds,
|
|
737
|
-
// for m.id IN (...)
|
|
738
|
-
...includeIds,
|
|
739
|
-
// for target.id IN (...)
|
|
740
|
-
prevMax,
|
|
741
|
-
nextMax
|
|
742
|
-
];
|
|
743
|
-
const includeResult = await this.executeQuery({ sql: sql2, params: params2 });
|
|
884
|
+
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
744
885
|
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
745
886
|
}
|
|
746
887
|
const excludeIds = messages.map((m) => m.id);
|
|
747
|
-
|
|
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);
|
|
748
893
|
const { sql, params } = query.build();
|
|
749
|
-
const result = await this.executeQuery({ sql, params });
|
|
894
|
+
const result = await this.operations.executeQuery({ sql, params });
|
|
750
895
|
if (Array.isArray(result)) messages.push(...result);
|
|
751
896
|
messages.sort((a, b) => {
|
|
752
897
|
const aRecord = a;
|
|
@@ -758,28 +903,1064 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
758
903
|
const processedMessages = messages.map((message) => {
|
|
759
904
|
const processedMsg = {};
|
|
760
905
|
for (const [key, value] of Object.entries(message)) {
|
|
761
|
-
|
|
906
|
+
if (key === `type` && value === `v2`) continue;
|
|
907
|
+
processedMsg[key] = deserializeValue(value);
|
|
762
908
|
}
|
|
763
909
|
return processedMsg;
|
|
764
910
|
});
|
|
765
911
|
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
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;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
async getMessagesById({
|
|
932
|
+
messageIds,
|
|
933
|
+
format
|
|
934
|
+
}) {
|
|
935
|
+
if (messageIds.length === 0) return [];
|
|
936
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
937
|
+
const messages = [];
|
|
938
|
+
try {
|
|
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;
|
|
771
951
|
});
|
|
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;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
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);
|
|
988
|
+
}
|
|
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
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
async updateMessages(args) {
|
|
1089
|
+
const { messages } = args;
|
|
1090
|
+
this.logger.debug("Updating messages", { count: messages.length });
|
|
1091
|
+
if (!messages.length) {
|
|
772
1092
|
return [];
|
|
773
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);
|
|
1097
|
+
try {
|
|
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 {
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
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
|
+
}
|
|
1156
|
+
}
|
|
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;
|
|
1175
|
+
});
|
|
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
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
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
|
|
1216
|
+
}) {
|
|
1217
|
+
if (!this.binding) {
|
|
1218
|
+
throw new Error("Workers binding is not configured");
|
|
1219
|
+
}
|
|
1220
|
+
try {
|
|
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;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
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
|
+
);
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
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
|
+
}
|
|
1265
|
+
try {
|
|
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;
|
|
1275
|
+
}
|
|
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
|
+
}));
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
this.logger.warn(`Failed to get table columns for ${tableName}:`, error);
|
|
1311
|
+
return [];
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
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.log("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.");
|
|
774
1955
|
}
|
|
775
1956
|
async persistWorkflowSnapshot({
|
|
776
1957
|
workflowName,
|
|
777
1958
|
runId,
|
|
778
1959
|
snapshot
|
|
779
1960
|
}) {
|
|
780
|
-
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1961
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
781
1962
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
782
|
-
const currentSnapshot = await this.load({
|
|
1963
|
+
const currentSnapshot = await this.operations.load({
|
|
783
1964
|
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
784
1965
|
keys: { workflow_name: workflowName, run_id: runId }
|
|
785
1966
|
});
|
|
@@ -794,7 +1975,7 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
794
1975
|
createdAt: now,
|
|
795
1976
|
updatedAt: now
|
|
796
1977
|
};
|
|
797
|
-
const processedRecord = await this.processRecord(persisting);
|
|
1978
|
+
const processedRecord = await this.operations.processRecord(persisting);
|
|
798
1979
|
const columns = Object.keys(processedRecord);
|
|
799
1980
|
const values = Object.values(processedRecord);
|
|
800
1981
|
const updateMap = {
|
|
@@ -805,143 +1986,43 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
805
1986
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
806
1987
|
const { sql, params } = query.build();
|
|
807
1988
|
try {
|
|
808
|
-
await this.executeQuery({ sql, params });
|
|
809
|
-
} catch (error) {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
1989
|
+
await this.operations.executeQuery({ sql, params });
|
|
1990
|
+
} catch (error$1) {
|
|
1991
|
+
throw new error.MastraError(
|
|
1992
|
+
{
|
|
1993
|
+
id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
|
|
1994
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1995
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1996
|
+
text: `Failed to persist workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1997
|
+
details: { workflowName, runId }
|
|
1998
|
+
},
|
|
1999
|
+
error$1
|
|
2000
|
+
);
|
|
814
2001
|
}
|
|
815
2002
|
}
|
|
816
2003
|
async loadWorkflowSnapshot(params) {
|
|
817
2004
|
const { workflowName, runId } = params;
|
|
818
2005
|
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
819
|
-
const d = await this.load({
|
|
820
|
-
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
821
|
-
keys: {
|
|
822
|
-
workflow_name: workflowName,
|
|
823
|
-
run_id: runId
|
|
824
|
-
}
|
|
825
|
-
});
|
|
826
|
-
return d ? d.snapshot : null;
|
|
827
|
-
}
|
|
828
|
-
/**
|
|
829
|
-
* Insert multiple records in a batch operation
|
|
830
|
-
* @param tableName The table to insert into
|
|
831
|
-
* @param records The records to insert
|
|
832
|
-
*/
|
|
833
|
-
async batchInsert({ tableName, records }) {
|
|
834
|
-
if (records.length === 0) return;
|
|
835
|
-
const fullTableName = this.getTableName(tableName);
|
|
836
2006
|
try {
|
|
837
|
-
const
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
const firstRecord = recordsToInsert[0];
|
|
843
|
-
const columns = Object.keys(firstRecord || {});
|
|
844
|
-
for (const record of recordsToInsert) {
|
|
845
|
-
const values = columns.map((col) => {
|
|
846
|
-
if (!record) return null;
|
|
847
|
-
const value = typeof col === "string" ? record[col] : null;
|
|
848
|
-
return this.serializeValue(value);
|
|
849
|
-
});
|
|
850
|
-
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
851
|
-
const { sql, params } = query.build();
|
|
852
|
-
await this.executeQuery({ sql, params });
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
this.logger.debug(
|
|
856
|
-
`Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
|
|
857
|
-
);
|
|
858
|
-
}
|
|
859
|
-
this.logger.debug(`Successfully batch inserted ${records.length} records into ${tableName}`);
|
|
860
|
-
} catch (error) {
|
|
861
|
-
this.logger.error(`Error batch inserting into ${tableName}:`, {
|
|
862
|
-
message: error instanceof Error ? error.message : String(error)
|
|
863
|
-
});
|
|
864
|
-
throw new Error(`Failed to batch insert into ${tableName}: ${error}`);
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
async getTraces({
|
|
868
|
-
name,
|
|
869
|
-
scope,
|
|
870
|
-
page,
|
|
871
|
-
perPage,
|
|
872
|
-
attributes,
|
|
873
|
-
fromDate,
|
|
874
|
-
toDate
|
|
875
|
-
}) {
|
|
876
|
-
const fullTableName = this.getTableName(storage.TABLE_TRACES);
|
|
877
|
-
try {
|
|
878
|
-
const query = createSqlBuilder().select("*").from(fullTableName).where("1=1");
|
|
879
|
-
if (name) {
|
|
880
|
-
query.andWhere("name LIKE ?", `%${name}%`);
|
|
881
|
-
}
|
|
882
|
-
if (scope) {
|
|
883
|
-
query.andWhere("scope = ?", scope);
|
|
884
|
-
}
|
|
885
|
-
if (attributes && Object.keys(attributes).length > 0) {
|
|
886
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
887
|
-
query.jsonLike("attributes", key, value);
|
|
2007
|
+
const d = await this.operations.load({
|
|
2008
|
+
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
2009
|
+
keys: {
|
|
2010
|
+
workflow_name: workflowName,
|
|
2011
|
+
run_id: runId
|
|
888
2012
|
}
|
|
889
|
-
}
|
|
890
|
-
if (fromDate) {
|
|
891
|
-
query.andWhere("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
892
|
-
}
|
|
893
|
-
if (toDate) {
|
|
894
|
-
query.andWhere("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
895
|
-
}
|
|
896
|
-
query.orderBy("startTime", "DESC").limit(perPage).offset((page - 1) * perPage);
|
|
897
|
-
const { sql, params } = query.build();
|
|
898
|
-
const results = await this.executeQuery({ sql, params });
|
|
899
|
-
return isArrayOfRecords(results) ? results.map((trace) => ({
|
|
900
|
-
...trace,
|
|
901
|
-
attributes: this.deserializeValue(trace.attributes, "jsonb"),
|
|
902
|
-
status: this.deserializeValue(trace.status, "jsonb"),
|
|
903
|
-
events: this.deserializeValue(trace.events, "jsonb"),
|
|
904
|
-
links: this.deserializeValue(trace.links, "jsonb"),
|
|
905
|
-
other: this.deserializeValue(trace.other, "jsonb")
|
|
906
|
-
})) : [];
|
|
907
|
-
} catch (error) {
|
|
908
|
-
this.logger.error("Error getting traces:", { message: error instanceof Error ? error.message : String(error) });
|
|
909
|
-
return [];
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
async getEvalsByAgentName(agentName, type) {
|
|
913
|
-
const fullTableName = this.getTableName(storage.TABLE_EVALS);
|
|
914
|
-
try {
|
|
915
|
-
let query = createSqlBuilder().select("*").from(fullTableName).where("agent_name = ?", agentName);
|
|
916
|
-
if (type === "test") {
|
|
917
|
-
query = query.andWhere("test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL");
|
|
918
|
-
} else if (type === "live") {
|
|
919
|
-
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
920
|
-
}
|
|
921
|
-
query.orderBy("created_at", "DESC");
|
|
922
|
-
const { sql, params } = query.build();
|
|
923
|
-
const results = await this.executeQuery({ sql, params });
|
|
924
|
-
return isArrayOfRecords(results) ? results.map((row) => {
|
|
925
|
-
const result = this.deserializeValue(row.result);
|
|
926
|
-
const testInfo = row.test_info ? this.deserializeValue(row.test_info) : void 0;
|
|
927
|
-
return {
|
|
928
|
-
input: row.input || "",
|
|
929
|
-
output: row.output || "",
|
|
930
|
-
result,
|
|
931
|
-
agentName: row.agent_name || "",
|
|
932
|
-
metricName: row.metric_name || "",
|
|
933
|
-
instructions: row.instructions || "",
|
|
934
|
-
runId: row.run_id || "",
|
|
935
|
-
globalRunId: row.global_run_id || "",
|
|
936
|
-
createdAt: row.created_at || "",
|
|
937
|
-
testInfo
|
|
938
|
-
};
|
|
939
|
-
}) : [];
|
|
940
|
-
} catch (error) {
|
|
941
|
-
this.logger.error(`Error getting evals for agent ${agentName}:`, {
|
|
942
|
-
message: error instanceof Error ? error.message : String(error)
|
|
943
2013
|
});
|
|
944
|
-
return
|
|
2014
|
+
return d ? d.snapshot : null;
|
|
2015
|
+
} catch (error$1) {
|
|
2016
|
+
throw new error.MastraError(
|
|
2017
|
+
{
|
|
2018
|
+
id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
|
|
2019
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2020
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2021
|
+
text: `Failed to load workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2022
|
+
details: { workflowName, runId }
|
|
2023
|
+
},
|
|
2024
|
+
error$1
|
|
2025
|
+
);
|
|
945
2026
|
}
|
|
946
2027
|
}
|
|
947
2028
|
parseWorkflowRun(row) {
|
|
@@ -957,17 +2038,11 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
957
2038
|
workflowName: row.workflow_name,
|
|
958
2039
|
runId: row.run_id,
|
|
959
2040
|
snapshot: parsedSnapshot,
|
|
960
|
-
createdAt:
|
|
961
|
-
updatedAt:
|
|
2041
|
+
createdAt: storage.ensureDate(row.createdAt),
|
|
2042
|
+
updatedAt: storage.ensureDate(row.updatedAt),
|
|
962
2043
|
resourceId: row.resourceId
|
|
963
2044
|
};
|
|
964
2045
|
}
|
|
965
|
-
async hasColumn(table, column) {
|
|
966
|
-
const sql = `PRAGMA table_info(${table});`;
|
|
967
|
-
const result = await this.executeQuery({ sql, params: [] });
|
|
968
|
-
if (!result || !Array.isArray(result)) return false;
|
|
969
|
-
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
970
|
-
}
|
|
971
2046
|
async getWorkflowRuns({
|
|
972
2047
|
workflowName,
|
|
973
2048
|
fromDate,
|
|
@@ -976,13 +2051,13 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
976
2051
|
offset,
|
|
977
2052
|
resourceId
|
|
978
2053
|
} = {}) {
|
|
979
|
-
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2054
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
980
2055
|
try {
|
|
981
2056
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
982
2057
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
983
2058
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
984
2059
|
if (resourceId) {
|
|
985
|
-
const hasResourceId = await this.hasColumn(fullTableName, "resourceId");
|
|
2060
|
+
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
986
2061
|
if (hasResourceId) {
|
|
987
2062
|
builder.whereAnd("resourceId = ?", resourceId);
|
|
988
2063
|
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
@@ -1005,24 +2080,37 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1005
2080
|
let total = 0;
|
|
1006
2081
|
if (limit !== void 0 && offset !== void 0) {
|
|
1007
2082
|
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
1008
|
-
const countResult = await this.executeQuery({
|
|
2083
|
+
const countResult = await this.operations.executeQuery({
|
|
2084
|
+
sql: countSql,
|
|
2085
|
+
params: countParams,
|
|
2086
|
+
first: true
|
|
2087
|
+
});
|
|
1009
2088
|
total = Number(countResult?.count ?? 0);
|
|
1010
2089
|
}
|
|
1011
|
-
const results = await this.executeQuery({ sql, params });
|
|
2090
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1012
2091
|
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
1013
2092
|
return { runs, total: total || runs.length };
|
|
1014
|
-
} catch (error) {
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
2093
|
+
} catch (error$1) {
|
|
2094
|
+
throw new error.MastraError(
|
|
2095
|
+
{
|
|
2096
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUNS_ERROR",
|
|
2097
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2098
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2099
|
+
text: `Failed to retrieve workflow runs: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2100
|
+
details: {
|
|
2101
|
+
workflowName: workflowName ?? "",
|
|
2102
|
+
resourceId: resourceId ?? ""
|
|
2103
|
+
}
|
|
2104
|
+
},
|
|
2105
|
+
error$1
|
|
2106
|
+
);
|
|
1019
2107
|
}
|
|
1020
2108
|
}
|
|
1021
2109
|
async getWorkflowRunById({
|
|
1022
2110
|
runId,
|
|
1023
2111
|
workflowName
|
|
1024
2112
|
}) {
|
|
1025
|
-
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2113
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1026
2114
|
try {
|
|
1027
2115
|
const conditions = [];
|
|
1028
2116
|
const params = [];
|
|
@@ -1036,15 +2124,318 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1036
2124
|
}
|
|
1037
2125
|
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
1038
2126
|
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
1039
|
-
const result = await this.executeQuery({ sql, params, first: true });
|
|
2127
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1040
2128
|
if (!result) return null;
|
|
1041
2129
|
return this.parseWorkflowRun(result);
|
|
1042
|
-
} catch (error) {
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
2130
|
+
} catch (error$1) {
|
|
2131
|
+
throw new error.MastraError(
|
|
2132
|
+
{
|
|
2133
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUN_BY_ID_ERROR",
|
|
2134
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2135
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2136
|
+
text: `Failed to retrieve workflow run by ID: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2137
|
+
details: { runId, workflowName: workflowName ?? "" }
|
|
2138
|
+
},
|
|
2139
|
+
error$1
|
|
2140
|
+
);
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
};
|
|
2144
|
+
|
|
2145
|
+
// src/storage/index.ts
|
|
2146
|
+
var D1Store = class extends storage.MastraStorage {
|
|
2147
|
+
client;
|
|
2148
|
+
binding;
|
|
2149
|
+
// D1Database binding
|
|
2150
|
+
tablePrefix;
|
|
2151
|
+
stores;
|
|
2152
|
+
/**
|
|
2153
|
+
* Creates a new D1Store instance
|
|
2154
|
+
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
2155
|
+
*/
|
|
2156
|
+
constructor(config) {
|
|
2157
|
+
try {
|
|
2158
|
+
super({ name: "D1" });
|
|
2159
|
+
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
2160
|
+
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
2161
|
+
}
|
|
2162
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
2163
|
+
if ("binding" in config) {
|
|
2164
|
+
if (!config.binding) {
|
|
2165
|
+
throw new Error("D1 binding is required when using Workers Binding API");
|
|
2166
|
+
}
|
|
2167
|
+
this.binding = config.binding;
|
|
2168
|
+
this.logger.info("Using D1 Workers Binding API");
|
|
2169
|
+
} else if ("client" in config) {
|
|
2170
|
+
if (!config.client) {
|
|
2171
|
+
throw new Error("D1 client is required when using D1ClientConfig");
|
|
2172
|
+
}
|
|
2173
|
+
this.client = config.client;
|
|
2174
|
+
this.logger.info("Using D1 Client");
|
|
2175
|
+
} else {
|
|
2176
|
+
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
2177
|
+
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
2178
|
+
}
|
|
2179
|
+
const cfClient = new Cloudflare__default.default({
|
|
2180
|
+
apiToken: config.apiToken
|
|
2181
|
+
});
|
|
2182
|
+
this.client = {
|
|
2183
|
+
query: ({ sql, params }) => {
|
|
2184
|
+
return cfClient.d1.database.query(config.databaseId, {
|
|
2185
|
+
account_id: config.accountId,
|
|
2186
|
+
sql,
|
|
2187
|
+
params
|
|
2188
|
+
});
|
|
2189
|
+
}
|
|
2190
|
+
};
|
|
2191
|
+
this.logger.info("Using D1 REST API");
|
|
2192
|
+
}
|
|
2193
|
+
} catch (error$1) {
|
|
2194
|
+
throw new error.MastraError(
|
|
2195
|
+
{
|
|
2196
|
+
id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
|
|
2197
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2198
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2199
|
+
text: "Error initializing D1Store"
|
|
2200
|
+
},
|
|
2201
|
+
error$1
|
|
2202
|
+
);
|
|
1047
2203
|
}
|
|
2204
|
+
const operations = new StoreOperationsD1({
|
|
2205
|
+
client: this.client,
|
|
2206
|
+
binding: this.binding,
|
|
2207
|
+
tablePrefix: this.tablePrefix
|
|
2208
|
+
});
|
|
2209
|
+
const scores = new ScoresStorageD1({
|
|
2210
|
+
operations
|
|
2211
|
+
});
|
|
2212
|
+
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2213
|
+
operations
|
|
2214
|
+
});
|
|
2215
|
+
const traces = new TracesStorageD1({
|
|
2216
|
+
operations
|
|
2217
|
+
});
|
|
2218
|
+
const workflows = new WorkflowsStorageD1({
|
|
2219
|
+
operations
|
|
2220
|
+
});
|
|
2221
|
+
const memory = new MemoryStorageD1({
|
|
2222
|
+
operations
|
|
2223
|
+
});
|
|
2224
|
+
this.stores = {
|
|
2225
|
+
operations,
|
|
2226
|
+
scores,
|
|
2227
|
+
legacyEvals,
|
|
2228
|
+
traces,
|
|
2229
|
+
workflows,
|
|
2230
|
+
memory
|
|
2231
|
+
};
|
|
2232
|
+
}
|
|
2233
|
+
get supports() {
|
|
2234
|
+
return {
|
|
2235
|
+
selectByIncludeResourceScope: true,
|
|
2236
|
+
resourceWorkingMemory: true,
|
|
2237
|
+
hasColumn: true,
|
|
2238
|
+
createTable: true,
|
|
2239
|
+
deleteMessages: false
|
|
2240
|
+
};
|
|
2241
|
+
}
|
|
2242
|
+
async createTable({
|
|
2243
|
+
tableName,
|
|
2244
|
+
schema
|
|
2245
|
+
}) {
|
|
2246
|
+
return this.stores.operations.createTable({ tableName, schema });
|
|
2247
|
+
}
|
|
2248
|
+
/**
|
|
2249
|
+
* Alters table schema to add columns if they don't exist
|
|
2250
|
+
* @param tableName Name of the table
|
|
2251
|
+
* @param schema Schema of the table
|
|
2252
|
+
* @param ifNotExists Array of column names to add if they don't exist
|
|
2253
|
+
*/
|
|
2254
|
+
async alterTable({
|
|
2255
|
+
tableName,
|
|
2256
|
+
schema,
|
|
2257
|
+
ifNotExists
|
|
2258
|
+
}) {
|
|
2259
|
+
return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
|
|
2260
|
+
}
|
|
2261
|
+
async clearTable({ tableName }) {
|
|
2262
|
+
return this.stores.operations.clearTable({ tableName });
|
|
2263
|
+
}
|
|
2264
|
+
async dropTable({ tableName }) {
|
|
2265
|
+
return this.stores.operations.dropTable({ tableName });
|
|
2266
|
+
}
|
|
2267
|
+
async hasColumn(table, column) {
|
|
2268
|
+
return this.stores.operations.hasColumn(table, column);
|
|
2269
|
+
}
|
|
2270
|
+
async insert({ tableName, record }) {
|
|
2271
|
+
return this.stores.operations.insert({ tableName, record });
|
|
2272
|
+
}
|
|
2273
|
+
async load({ tableName, keys }) {
|
|
2274
|
+
return this.stores.operations.load({ tableName, keys });
|
|
2275
|
+
}
|
|
2276
|
+
async getThreadById({ threadId }) {
|
|
2277
|
+
return this.stores.memory.getThreadById({ threadId });
|
|
2278
|
+
}
|
|
2279
|
+
/**
|
|
2280
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
2281
|
+
*/
|
|
2282
|
+
async getThreadsByResourceId({ resourceId }) {
|
|
2283
|
+
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2284
|
+
}
|
|
2285
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
2286
|
+
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2287
|
+
}
|
|
2288
|
+
async saveThread({ thread }) {
|
|
2289
|
+
return this.stores.memory.saveThread({ thread });
|
|
2290
|
+
}
|
|
2291
|
+
async updateThread({
|
|
2292
|
+
id,
|
|
2293
|
+
title,
|
|
2294
|
+
metadata
|
|
2295
|
+
}) {
|
|
2296
|
+
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2297
|
+
}
|
|
2298
|
+
async deleteThread({ threadId }) {
|
|
2299
|
+
return this.stores.memory.deleteThread({ threadId });
|
|
2300
|
+
}
|
|
2301
|
+
async saveMessages(args) {
|
|
2302
|
+
return this.stores.memory.saveMessages(args);
|
|
2303
|
+
}
|
|
2304
|
+
async getMessages({
|
|
2305
|
+
threadId,
|
|
2306
|
+
selectBy,
|
|
2307
|
+
format
|
|
2308
|
+
}) {
|
|
2309
|
+
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2310
|
+
}
|
|
2311
|
+
async getMessagesById({
|
|
2312
|
+
messageIds,
|
|
2313
|
+
format
|
|
2314
|
+
}) {
|
|
2315
|
+
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
2316
|
+
}
|
|
2317
|
+
async getMessagesPaginated({
|
|
2318
|
+
threadId,
|
|
2319
|
+
selectBy,
|
|
2320
|
+
format
|
|
2321
|
+
}) {
|
|
2322
|
+
return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
|
|
2323
|
+
}
|
|
2324
|
+
async updateWorkflowResults({
|
|
2325
|
+
workflowName,
|
|
2326
|
+
runId,
|
|
2327
|
+
stepId,
|
|
2328
|
+
result,
|
|
2329
|
+
runtimeContext
|
|
2330
|
+
}) {
|
|
2331
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
|
|
2332
|
+
}
|
|
2333
|
+
async updateWorkflowState({
|
|
2334
|
+
workflowName,
|
|
2335
|
+
runId,
|
|
2336
|
+
opts
|
|
2337
|
+
}) {
|
|
2338
|
+
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2339
|
+
}
|
|
2340
|
+
async persistWorkflowSnapshot({
|
|
2341
|
+
workflowName,
|
|
2342
|
+
runId,
|
|
2343
|
+
snapshot
|
|
2344
|
+
}) {
|
|
2345
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
|
|
2346
|
+
}
|
|
2347
|
+
async loadWorkflowSnapshot(params) {
|
|
2348
|
+
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2349
|
+
}
|
|
2350
|
+
async getWorkflowRuns({
|
|
2351
|
+
workflowName,
|
|
2352
|
+
fromDate,
|
|
2353
|
+
toDate,
|
|
2354
|
+
limit,
|
|
2355
|
+
offset,
|
|
2356
|
+
resourceId
|
|
2357
|
+
} = {}) {
|
|
2358
|
+
return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2359
|
+
}
|
|
2360
|
+
async getWorkflowRunById({
|
|
2361
|
+
runId,
|
|
2362
|
+
workflowName
|
|
2363
|
+
}) {
|
|
2364
|
+
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2365
|
+
}
|
|
2366
|
+
/**
|
|
2367
|
+
* Insert multiple records in a batch operation
|
|
2368
|
+
* @param tableName The table to insert into
|
|
2369
|
+
* @param records The records to insert
|
|
2370
|
+
*/
|
|
2371
|
+
async batchInsert({ tableName, records }) {
|
|
2372
|
+
return this.stores.operations.batchInsert({ tableName, records });
|
|
2373
|
+
}
|
|
2374
|
+
/**
|
|
2375
|
+
* @deprecated use getTracesPaginated instead
|
|
2376
|
+
*/
|
|
2377
|
+
async getTraces(args) {
|
|
2378
|
+
return this.stores.traces.getTraces(args);
|
|
2379
|
+
}
|
|
2380
|
+
async getTracesPaginated(args) {
|
|
2381
|
+
return this.stores.traces.getTracesPaginated(args);
|
|
2382
|
+
}
|
|
2383
|
+
/**
|
|
2384
|
+
* @deprecated use getEvals instead
|
|
2385
|
+
*/
|
|
2386
|
+
async getEvalsByAgentName(agentName, type) {
|
|
2387
|
+
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2388
|
+
}
|
|
2389
|
+
async getEvals(options) {
|
|
2390
|
+
return this.stores.legacyEvals.getEvals(options);
|
|
2391
|
+
}
|
|
2392
|
+
async updateMessages(_args) {
|
|
2393
|
+
return this.stores.memory.updateMessages(_args);
|
|
2394
|
+
}
|
|
2395
|
+
async getResourceById({ resourceId }) {
|
|
2396
|
+
return this.stores.memory.getResourceById({ resourceId });
|
|
2397
|
+
}
|
|
2398
|
+
async saveResource({ resource }) {
|
|
2399
|
+
return this.stores.memory.saveResource({ resource });
|
|
2400
|
+
}
|
|
2401
|
+
async updateResource({
|
|
2402
|
+
resourceId,
|
|
2403
|
+
workingMemory,
|
|
2404
|
+
metadata
|
|
2405
|
+
}) {
|
|
2406
|
+
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
2407
|
+
}
|
|
2408
|
+
async getScoreById({ id: _id }) {
|
|
2409
|
+
return this.stores.scores.getScoreById({ id: _id });
|
|
2410
|
+
}
|
|
2411
|
+
async saveScore(_score) {
|
|
2412
|
+
return this.stores.scores.saveScore(_score);
|
|
2413
|
+
}
|
|
2414
|
+
async getScoresByRunId({
|
|
2415
|
+
runId: _runId,
|
|
2416
|
+
pagination: _pagination
|
|
2417
|
+
}) {
|
|
2418
|
+
return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2419
|
+
}
|
|
2420
|
+
async getScoresByEntityId({
|
|
2421
|
+
entityId: _entityId,
|
|
2422
|
+
entityType: _entityType,
|
|
2423
|
+
pagination: _pagination
|
|
2424
|
+
}) {
|
|
2425
|
+
return this.stores.scores.getScoresByEntityId({
|
|
2426
|
+
entityId: _entityId,
|
|
2427
|
+
entityType: _entityType,
|
|
2428
|
+
pagination: _pagination
|
|
2429
|
+
});
|
|
2430
|
+
}
|
|
2431
|
+
async getScoresByScorerId({
|
|
2432
|
+
scorerId,
|
|
2433
|
+
pagination,
|
|
2434
|
+
entityId,
|
|
2435
|
+
entityType,
|
|
2436
|
+
source
|
|
2437
|
+
}) {
|
|
2438
|
+
return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
1048
2439
|
}
|
|
1049
2440
|
/**
|
|
1050
2441
|
* Close the database connection
|
|
@@ -1056,3 +2447,5 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1056
2447
|
};
|
|
1057
2448
|
|
|
1058
2449
|
exports.D1Store = D1Store;
|
|
2450
|
+
//# sourceMappingURL=index.cjs.map
|
|
2451
|
+
//# sourceMappingURL=index.cjs.map
|