@mastra/cloudflare-d1 0.0.0-trigger-playground-ui-package-20250506151043 → 0.0.0-update-stores-peerDeps-20250723031338
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/LICENSE.md +11 -42
- package/README.md +15 -0
- package/dist/_tsup-dts-rollup.d.cts +452 -77
- package/dist/_tsup-dts-rollup.d.ts +452 -77
- package/dist/index.cjs +1868 -587
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1860 -579
- package/package.json +17 -13
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,380 +231,426 @@ 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
|
+
};
|
|
320
|
+
}
|
|
321
|
+
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
322
|
+
if (conditions.length > 0) {
|
|
323
|
+
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
348
324
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
this.
|
|
352
|
-
|
|
353
|
-
|
|
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
|
});
|
|
542
542
|
if (!thread) return null;
|
|
543
|
+
console.log("thread", thread);
|
|
543
544
|
try {
|
|
544
545
|
return {
|
|
545
546
|
...thread,
|
|
546
|
-
createdAt:
|
|
547
|
-
updatedAt:
|
|
547
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
548
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
548
549
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
549
550
|
};
|
|
550
|
-
} catch (error) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
551
|
+
} catch (error$1) {
|
|
552
|
+
const mastraError = new error.MastraError(
|
|
553
|
+
{
|
|
554
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREAD_BY_ID_ERROR",
|
|
555
|
+
domain: error.ErrorDomain.STORAGE,
|
|
556
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
557
|
+
text: `Error processing thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
558
|
+
details: { threadId }
|
|
559
|
+
},
|
|
560
|
+
error$1
|
|
561
|
+
);
|
|
562
|
+
this.logger?.error(mastraError.toString());
|
|
563
|
+
this.logger?.trackException(mastraError);
|
|
554
564
|
return null;
|
|
555
565
|
}
|
|
556
566
|
}
|
|
567
|
+
/**
|
|
568
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
569
|
+
*/
|
|
557
570
|
async getThreadsByResourceId({ resourceId }) {
|
|
558
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
571
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
559
572
|
try {
|
|
560
573
|
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
561
574
|
const { sql, params } = query.build();
|
|
562
|
-
const results = await this.executeQuery({ sql, params });
|
|
575
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
563
576
|
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
564
577
|
...thread,
|
|
565
|
-
createdAt:
|
|
566
|
-
updatedAt:
|
|
578
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
579
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
567
580
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
568
581
|
}));
|
|
569
|
-
} catch (error) {
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
582
|
+
} catch (error$1) {
|
|
583
|
+
const mastraError = new error.MastraError(
|
|
584
|
+
{
|
|
585
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_ERROR",
|
|
586
|
+
domain: error.ErrorDomain.STORAGE,
|
|
587
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
588
|
+
text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
589
|
+
details: { resourceId }
|
|
590
|
+
},
|
|
591
|
+
error$1
|
|
592
|
+
);
|
|
593
|
+
this.logger?.error(mastraError.toString());
|
|
594
|
+
this.logger?.trackException(mastraError);
|
|
573
595
|
return [];
|
|
574
596
|
}
|
|
575
597
|
}
|
|
598
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
599
|
+
const { resourceId, page, perPage } = args;
|
|
600
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
601
|
+
const mapRowToStorageThreadType = (row) => ({
|
|
602
|
+
...row,
|
|
603
|
+
createdAt: storage.ensureDate(row.createdAt),
|
|
604
|
+
updatedAt: storage.ensureDate(row.updatedAt),
|
|
605
|
+
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
|
|
606
|
+
});
|
|
607
|
+
try {
|
|
608
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
609
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
610
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
611
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy("createdAt", "DESC").limit(perPage).offset(page * perPage);
|
|
612
|
+
const results = await this.operations.executeQuery(selectQuery.build());
|
|
613
|
+
const threads = results.map(mapRowToStorageThreadType);
|
|
614
|
+
return {
|
|
615
|
+
threads,
|
|
616
|
+
total,
|
|
617
|
+
page,
|
|
618
|
+
perPage,
|
|
619
|
+
hasMore: page * perPage + threads.length < total
|
|
620
|
+
};
|
|
621
|
+
} catch (error$1) {
|
|
622
|
+
const mastraError = new error.MastraError(
|
|
623
|
+
{
|
|
624
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_ERROR",
|
|
625
|
+
domain: error.ErrorDomain.STORAGE,
|
|
626
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
627
|
+
text: `Error getting threads by resourceId ${resourceId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
628
|
+
details: { resourceId }
|
|
629
|
+
},
|
|
630
|
+
error$1
|
|
631
|
+
);
|
|
632
|
+
this.logger?.error(mastraError.toString());
|
|
633
|
+
this.logger?.trackException(mastraError);
|
|
634
|
+
return {
|
|
635
|
+
threads: [],
|
|
636
|
+
total: 0,
|
|
637
|
+
page,
|
|
638
|
+
perPage,
|
|
639
|
+
hasMore: false
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
}
|
|
576
643
|
async saveThread({ thread }) {
|
|
577
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
644
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
578
645
|
const threadToSave = {
|
|
579
646
|
id: thread.id,
|
|
580
647
|
resourceId: thread.resourceId,
|
|
581
648
|
title: thread.title,
|
|
582
649
|
metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
|
|
583
|
-
createdAt: thread.createdAt,
|
|
584
|
-
updatedAt: thread.updatedAt
|
|
650
|
+
createdAt: thread.createdAt.toISOString(),
|
|
651
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
585
652
|
};
|
|
586
|
-
const processedRecord = await this.processRecord(threadToSave);
|
|
653
|
+
const processedRecord = await this.operations.processRecord(threadToSave);
|
|
587
654
|
const columns = Object.keys(processedRecord);
|
|
588
655
|
const values = Object.values(processedRecord);
|
|
589
656
|
const updateMap = {
|
|
@@ -596,12 +663,19 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
596
663
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
597
664
|
const { sql, params } = query.build();
|
|
598
665
|
try {
|
|
599
|
-
await this.executeQuery({ sql, params });
|
|
666
|
+
await this.operations.executeQuery({ sql, params });
|
|
600
667
|
return thread;
|
|
601
|
-
} catch (error) {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
668
|
+
} catch (error$1) {
|
|
669
|
+
throw new error.MastraError(
|
|
670
|
+
{
|
|
671
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_THREAD_ERROR",
|
|
672
|
+
domain: error.ErrorDomain.STORAGE,
|
|
673
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
674
|
+
text: `Failed to save thread to ${fullTableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
675
|
+
details: { threadId: thread.id }
|
|
676
|
+
},
|
|
677
|
+
error$1
|
|
678
|
+
);
|
|
605
679
|
}
|
|
606
680
|
}
|
|
607
681
|
async updateThread({
|
|
@@ -610,20 +684,21 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
610
684
|
metadata
|
|
611
685
|
}) {
|
|
612
686
|
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
687
|
try {
|
|
626
|
-
|
|
688
|
+
if (!thread) {
|
|
689
|
+
throw new Error(`Thread ${id} not found`);
|
|
690
|
+
}
|
|
691
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
692
|
+
const mergedMetadata = {
|
|
693
|
+
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
694
|
+
...metadata
|
|
695
|
+
};
|
|
696
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
697
|
+
const columns = ["title", "metadata", "updatedAt"];
|
|
698
|
+
const values = [title, JSON.stringify(mergedMetadata), updatedAt.toISOString()];
|
|
699
|
+
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
700
|
+
const { sql, params } = query.build();
|
|
701
|
+
await this.operations.executeQuery({ sql, params });
|
|
627
702
|
return {
|
|
628
703
|
...thread,
|
|
629
704
|
title,
|
|
@@ -631,41 +706,64 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
631
706
|
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
632
707
|
...metadata
|
|
633
708
|
},
|
|
634
|
-
updatedAt
|
|
709
|
+
updatedAt
|
|
635
710
|
};
|
|
636
|
-
} catch (error) {
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
711
|
+
} catch (error$1) {
|
|
712
|
+
throw new error.MastraError(
|
|
713
|
+
{
|
|
714
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_THREAD_ERROR",
|
|
715
|
+
domain: error.ErrorDomain.STORAGE,
|
|
716
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
717
|
+
text: `Failed to update thread ${id}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
718
|
+
details: { threadId: id }
|
|
719
|
+
},
|
|
720
|
+
error$1
|
|
721
|
+
);
|
|
640
722
|
}
|
|
641
723
|
}
|
|
642
724
|
async deleteThread({ threadId }) {
|
|
643
|
-
const fullTableName = this.getTableName(storage.TABLE_THREADS);
|
|
725
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
644
726
|
try {
|
|
645
727
|
const deleteThreadQuery = createSqlBuilder().delete(fullTableName).where("id = ?", threadId);
|
|
646
728
|
const { sql: threadSql, params: threadParams } = deleteThreadQuery.build();
|
|
647
|
-
await this.executeQuery({ sql: threadSql, params: threadParams });
|
|
648
|
-
const messagesTableName = this.getTableName(storage.TABLE_MESSAGES);
|
|
729
|
+
await this.operations.executeQuery({ sql: threadSql, params: threadParams });
|
|
730
|
+
const messagesTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
649
731
|
const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
|
|
650
732
|
const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
|
|
651
|
-
await this.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
652
|
-
} catch (error) {
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
733
|
+
await this.operations.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
734
|
+
} catch (error$1) {
|
|
735
|
+
throw new error.MastraError(
|
|
736
|
+
{
|
|
737
|
+
id: "CLOUDFLARE_D1_STORAGE_DELETE_THREAD_ERROR",
|
|
738
|
+
domain: error.ErrorDomain.STORAGE,
|
|
739
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
740
|
+
text: `Failed to delete thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
741
|
+
details: { threadId }
|
|
742
|
+
},
|
|
743
|
+
error$1
|
|
744
|
+
);
|
|
657
745
|
}
|
|
658
746
|
}
|
|
659
|
-
|
|
660
|
-
|
|
747
|
+
async saveMessages(args) {
|
|
748
|
+
const { messages, format = "v1" } = args;
|
|
661
749
|
if (messages.length === 0) return [];
|
|
662
750
|
try {
|
|
663
751
|
const now = /* @__PURE__ */ new Date();
|
|
752
|
+
const threadId = messages[0]?.threadId;
|
|
664
753
|
for (const [i, message] of messages.entries()) {
|
|
665
754
|
if (!message.id) throw new Error(`Message at index ${i} missing id`);
|
|
666
|
-
if (!message.threadId)
|
|
667
|
-
|
|
668
|
-
|
|
755
|
+
if (!message.threadId) {
|
|
756
|
+
throw new Error(`Message at index ${i} missing threadId`);
|
|
757
|
+
}
|
|
758
|
+
if (!message.content) {
|
|
759
|
+
throw new Error(`Message at index ${i} missing content`);
|
|
760
|
+
}
|
|
761
|
+
if (!message.role) {
|
|
762
|
+
throw new Error(`Message at index ${i} missing role`);
|
|
763
|
+
}
|
|
764
|
+
if (!message.resourceId) {
|
|
765
|
+
throw new Error(`Message at index ${i} missing resourceId`);
|
|
766
|
+
}
|
|
669
767
|
const thread = await this.getThreadById({ threadId: message.threadId });
|
|
670
768
|
if (!thread) {
|
|
671
769
|
throw new Error(`Thread ${message.threadId} not found`);
|
|
@@ -679,74 +777,119 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
679
777
|
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
680
778
|
createdAt: createdAt.toISOString(),
|
|
681
779
|
role: message.role,
|
|
682
|
-
type: message.type
|
|
780
|
+
type: message.type || "v2",
|
|
781
|
+
resourceId: message.resourceId
|
|
683
782
|
};
|
|
684
783
|
});
|
|
685
|
-
await
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
784
|
+
await Promise.all([
|
|
785
|
+
this.operations.batchUpsert({
|
|
786
|
+
tableName: storage.TABLE_MESSAGES,
|
|
787
|
+
records: messagesToInsert
|
|
788
|
+
}),
|
|
789
|
+
// Update thread's updatedAt timestamp
|
|
790
|
+
this.operations.executeQuery({
|
|
791
|
+
sql: `UPDATE ${this.operations.getTableName(storage.TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
|
|
792
|
+
params: [now.toISOString(), threadId]
|
|
793
|
+
})
|
|
794
|
+
]);
|
|
689
795
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
796
|
+
const list = new agent.MessageList().add(messages, "memory");
|
|
797
|
+
if (format === `v2`) return list.get.all.v2();
|
|
798
|
+
return list.get.all.v1();
|
|
799
|
+
} catch (error$1) {
|
|
800
|
+
throw new error.MastraError(
|
|
801
|
+
{
|
|
802
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_MESSAGES_ERROR",
|
|
803
|
+
domain: error.ErrorDomain.STORAGE,
|
|
804
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
805
|
+
text: `Failed to save messages: ${error$1 instanceof Error ? error$1.message : String(error$1)}`
|
|
806
|
+
},
|
|
807
|
+
error$1
|
|
808
|
+
);
|
|
694
809
|
}
|
|
695
810
|
}
|
|
696
|
-
async
|
|
697
|
-
const
|
|
698
|
-
|
|
811
|
+
async _getIncludedMessages(threadId, selectBy) {
|
|
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
|
+
selectBy,
|
|
871
|
+
format
|
|
872
|
+
}) {
|
|
873
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
874
|
+
const limit = storage.resolveMessageLimit({
|
|
875
|
+
last: selectBy?.last,
|
|
876
|
+
defaultLimit: 40
|
|
877
|
+
});
|
|
699
878
|
const include = selectBy?.include || [];
|
|
700
879
|
const messages = [];
|
|
701
880
|
try {
|
|
702
881
|
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 });
|
|
882
|
+
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
744
883
|
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
745
884
|
}
|
|
746
885
|
const excludeIds = messages.map((m) => m.id);
|
|
747
|
-
|
|
886
|
+
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
887
|
+
if (excludeIds.length > 0) {
|
|
888
|
+
query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
|
|
889
|
+
}
|
|
890
|
+
query.orderBy("createdAt", "DESC").limit(limit);
|
|
748
891
|
const { sql, params } = query.build();
|
|
749
|
-
const result = await this.executeQuery({ sql, params });
|
|
892
|
+
const result = await this.operations.executeQuery({ sql, params });
|
|
750
893
|
if (Array.isArray(result)) messages.push(...result);
|
|
751
894
|
messages.sort((a, b) => {
|
|
752
895
|
const aRecord = a;
|
|
@@ -758,28 +901,982 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
758
901
|
const processedMessages = messages.map((message) => {
|
|
759
902
|
const processedMsg = {};
|
|
760
903
|
for (const [key, value] of Object.entries(message)) {
|
|
761
|
-
|
|
904
|
+
if (key === `type` && value === `v2`) continue;
|
|
905
|
+
processedMsg[key] = deserializeValue(value);
|
|
906
|
+
}
|
|
907
|
+
return processedMsg;
|
|
908
|
+
});
|
|
909
|
+
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
910
|
+
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
911
|
+
if (format === `v2`) return list.get.all.v2();
|
|
912
|
+
return list.get.all.v1();
|
|
913
|
+
} catch (error$1) {
|
|
914
|
+
const mastraError = new error.MastraError(
|
|
915
|
+
{
|
|
916
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
|
|
917
|
+
domain: error.ErrorDomain.STORAGE,
|
|
918
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
919
|
+
text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
920
|
+
details: { threadId }
|
|
921
|
+
},
|
|
922
|
+
error$1
|
|
923
|
+
);
|
|
924
|
+
this.logger?.error(mastraError.toString());
|
|
925
|
+
this.logger?.trackException(mastraError);
|
|
926
|
+
throw mastraError;
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
async getMessagesPaginated({
|
|
930
|
+
threadId,
|
|
931
|
+
selectBy,
|
|
932
|
+
format
|
|
933
|
+
}) {
|
|
934
|
+
const { dateRange, page = 0, perPage: perPageInput } = selectBy?.pagination || {};
|
|
935
|
+
const { start: fromDate, end: toDate } = dateRange || {};
|
|
936
|
+
const perPage = perPageInput !== void 0 ? perPageInput : storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
937
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
938
|
+
const messages = [];
|
|
939
|
+
try {
|
|
940
|
+
if (selectBy?.include?.length) {
|
|
941
|
+
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
942
|
+
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
943
|
+
}
|
|
944
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("thread_id = ?", threadId);
|
|
945
|
+
if (fromDate) {
|
|
946
|
+
countQuery.andWhere("createdAt >= ?", storage.serializeDate(fromDate));
|
|
947
|
+
}
|
|
948
|
+
if (toDate) {
|
|
949
|
+
countQuery.andWhere("createdAt <= ?", storage.serializeDate(toDate));
|
|
950
|
+
}
|
|
951
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
952
|
+
const total = Number(countResult[0]?.count ?? 0);
|
|
953
|
+
if (total === 0 && messages.length === 0) {
|
|
954
|
+
return {
|
|
955
|
+
messages: [],
|
|
956
|
+
total: 0,
|
|
957
|
+
page,
|
|
958
|
+
perPage,
|
|
959
|
+
hasMore: false
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
const excludeIds = messages.map((m) => m.id);
|
|
963
|
+
const excludeCondition = excludeIds.length > 0 ? `AND id NOT IN (${excludeIds.map(() => "?").join(",")})` : "";
|
|
964
|
+
let query;
|
|
965
|
+
let queryParams = [threadId];
|
|
966
|
+
if (fromDate) {
|
|
967
|
+
queryParams.push(storage.serializeDate(fromDate));
|
|
968
|
+
}
|
|
969
|
+
if (toDate) {
|
|
970
|
+
queryParams.push(storage.serializeDate(toDate));
|
|
971
|
+
}
|
|
972
|
+
if (excludeIds.length > 0) {
|
|
973
|
+
queryParams.push(...excludeIds);
|
|
974
|
+
}
|
|
975
|
+
if (selectBy?.last && selectBy.last > 0) {
|
|
976
|
+
query = `
|
|
977
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
978
|
+
FROM ${fullTableName}
|
|
979
|
+
WHERE thread_id = ?
|
|
980
|
+
${fromDate ? "AND createdAt >= ?" : ""}
|
|
981
|
+
${toDate ? "AND createdAt <= ?" : ""}
|
|
982
|
+
${excludeCondition}
|
|
983
|
+
ORDER BY createdAt DESC
|
|
984
|
+
LIMIT ?
|
|
985
|
+
`;
|
|
986
|
+
queryParams.push(selectBy.last);
|
|
987
|
+
} else {
|
|
988
|
+
query = `
|
|
989
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
990
|
+
FROM ${fullTableName}
|
|
991
|
+
WHERE thread_id = ?
|
|
992
|
+
${fromDate ? "AND createdAt >= ?" : ""}
|
|
993
|
+
${toDate ? "AND createdAt <= ?" : ""}
|
|
994
|
+
${excludeCondition}
|
|
995
|
+
ORDER BY createdAt DESC
|
|
996
|
+
LIMIT ? OFFSET ?
|
|
997
|
+
`;
|
|
998
|
+
queryParams.push(perPage, page * perPage);
|
|
999
|
+
}
|
|
1000
|
+
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
1001
|
+
const processedMessages = results.map((message) => {
|
|
1002
|
+
const processedMsg = {};
|
|
1003
|
+
for (const [key, value] of Object.entries(message)) {
|
|
1004
|
+
if (key === `type` && value === `v2`) continue;
|
|
1005
|
+
processedMsg[key] = deserializeValue(value);
|
|
1006
|
+
}
|
|
1007
|
+
return processedMsg;
|
|
1008
|
+
});
|
|
1009
|
+
if (selectBy?.last) {
|
|
1010
|
+
processedMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
1011
|
+
}
|
|
1012
|
+
const list = new agent.MessageList().add(processedMessages, "memory");
|
|
1013
|
+
messages.push(...format === `v2` ? list.get.all.v2() : list.get.all.v1());
|
|
1014
|
+
return {
|
|
1015
|
+
messages,
|
|
1016
|
+
total,
|
|
1017
|
+
page,
|
|
1018
|
+
perPage,
|
|
1019
|
+
hasMore: selectBy?.last ? false : page * perPage + messages.length < total
|
|
1020
|
+
};
|
|
1021
|
+
} catch (error$1) {
|
|
1022
|
+
const mastraError = new error.MastraError(
|
|
1023
|
+
{
|
|
1024
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_PAGINATED_ERROR",
|
|
1025
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1026
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1027
|
+
text: `Failed to retrieve messages for thread ${threadId}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1028
|
+
details: { threadId }
|
|
1029
|
+
},
|
|
1030
|
+
error$1
|
|
1031
|
+
);
|
|
1032
|
+
this.logger?.error(mastraError.toString());
|
|
1033
|
+
this.logger?.trackException(mastraError);
|
|
1034
|
+
return {
|
|
1035
|
+
messages: [],
|
|
1036
|
+
total: 0,
|
|
1037
|
+
page,
|
|
1038
|
+
perPage,
|
|
1039
|
+
hasMore: false
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
async updateMessages(args) {
|
|
1044
|
+
const { messages } = args;
|
|
1045
|
+
this.logger.debug("Updating messages", { count: messages.length });
|
|
1046
|
+
if (!messages.length) {
|
|
1047
|
+
return [];
|
|
1048
|
+
}
|
|
1049
|
+
const messageIds = messages.map((m) => m.id);
|
|
1050
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_MESSAGES);
|
|
1051
|
+
const threadsTableName = this.operations.getTableName(storage.TABLE_THREADS);
|
|
1052
|
+
try {
|
|
1053
|
+
const placeholders = messageIds.map(() => "?").join(",");
|
|
1054
|
+
const selectQuery = `SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId FROM ${fullTableName} WHERE id IN (${placeholders})`;
|
|
1055
|
+
const existingMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
1056
|
+
if (existingMessages.length === 0) {
|
|
1057
|
+
return [];
|
|
1058
|
+
}
|
|
1059
|
+
const parsedExistingMessages = existingMessages.map((msg) => {
|
|
1060
|
+
if (typeof msg.content === "string") {
|
|
1061
|
+
try {
|
|
1062
|
+
msg.content = JSON.parse(msg.content);
|
|
1063
|
+
} catch {
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
return msg;
|
|
1067
|
+
});
|
|
1068
|
+
const threadIdsToUpdate = /* @__PURE__ */ new Set();
|
|
1069
|
+
const updateQueries = [];
|
|
1070
|
+
for (const existingMessage of parsedExistingMessages) {
|
|
1071
|
+
const updatePayload = messages.find((m) => m.id === existingMessage.id);
|
|
1072
|
+
if (!updatePayload) continue;
|
|
1073
|
+
const { id, ...fieldsToUpdate } = updatePayload;
|
|
1074
|
+
if (Object.keys(fieldsToUpdate).length === 0) continue;
|
|
1075
|
+
threadIdsToUpdate.add(existingMessage.threadId);
|
|
1076
|
+
if ("threadId" in updatePayload && updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
|
|
1077
|
+
threadIdsToUpdate.add(updatePayload.threadId);
|
|
1078
|
+
}
|
|
1079
|
+
const setClauses = [];
|
|
1080
|
+
const values = [];
|
|
1081
|
+
const updatableFields = { ...fieldsToUpdate };
|
|
1082
|
+
if (updatableFields.content) {
|
|
1083
|
+
const existingContent = existingMessage.content || {};
|
|
1084
|
+
const newContent = {
|
|
1085
|
+
...existingContent,
|
|
1086
|
+
...updatableFields.content,
|
|
1087
|
+
// Deep merge metadata if it exists on both
|
|
1088
|
+
...existingContent?.metadata && updatableFields.content.metadata ? {
|
|
1089
|
+
metadata: {
|
|
1090
|
+
...existingContent.metadata,
|
|
1091
|
+
...updatableFields.content.metadata
|
|
1092
|
+
}
|
|
1093
|
+
} : {}
|
|
1094
|
+
};
|
|
1095
|
+
setClauses.push(`content = ?`);
|
|
1096
|
+
values.push(JSON.stringify(newContent));
|
|
1097
|
+
delete updatableFields.content;
|
|
1098
|
+
}
|
|
1099
|
+
for (const key in updatableFields) {
|
|
1100
|
+
if (Object.prototype.hasOwnProperty.call(updatableFields, key)) {
|
|
1101
|
+
const dbColumn = key === "threadId" ? "thread_id" : key;
|
|
1102
|
+
setClauses.push(`${dbColumn} = ?`);
|
|
1103
|
+
values.push(updatableFields[key]);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
if (setClauses.length > 0) {
|
|
1107
|
+
values.push(id);
|
|
1108
|
+
const updateQuery = `UPDATE ${fullTableName} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
1109
|
+
updateQueries.push({ sql: updateQuery, params: values });
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
for (const query of updateQueries) {
|
|
1113
|
+
await this.operations.executeQuery(query);
|
|
1114
|
+
}
|
|
1115
|
+
if (threadIdsToUpdate.size > 0) {
|
|
1116
|
+
const threadPlaceholders = Array.from(threadIdsToUpdate).map(() => "?").join(",");
|
|
1117
|
+
const threadUpdateQuery = `UPDATE ${threadsTableName} SET updatedAt = ? WHERE id IN (${threadPlaceholders})`;
|
|
1118
|
+
const threadUpdateParams = [(/* @__PURE__ */ new Date()).toISOString(), ...Array.from(threadIdsToUpdate)];
|
|
1119
|
+
await this.operations.executeQuery({ sql: threadUpdateQuery, params: threadUpdateParams });
|
|
1120
|
+
}
|
|
1121
|
+
const updatedMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
1122
|
+
return updatedMessages.map((message) => {
|
|
1123
|
+
if (typeof message.content === "string") {
|
|
1124
|
+
try {
|
|
1125
|
+
message.content = JSON.parse(message.content);
|
|
1126
|
+
} catch {
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return message;
|
|
1130
|
+
});
|
|
1131
|
+
} catch (error$1) {
|
|
1132
|
+
throw new error.MastraError(
|
|
1133
|
+
{
|
|
1134
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_MESSAGES_FAILED",
|
|
1135
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1136
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1137
|
+
details: { count: messages.length }
|
|
1138
|
+
},
|
|
1139
|
+
error$1
|
|
1140
|
+
);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
var StoreOperationsD1 = class extends storage.StoreOperations {
|
|
1145
|
+
client;
|
|
1146
|
+
binding;
|
|
1147
|
+
tablePrefix;
|
|
1148
|
+
constructor(config) {
|
|
1149
|
+
super();
|
|
1150
|
+
this.client = config.client;
|
|
1151
|
+
this.binding = config.binding;
|
|
1152
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
1153
|
+
}
|
|
1154
|
+
async hasColumn(table, column) {
|
|
1155
|
+
const fullTableName = table.startsWith(this.tablePrefix) ? table : `${this.tablePrefix}${table}`;
|
|
1156
|
+
const sql = `PRAGMA table_info(${fullTableName});`;
|
|
1157
|
+
const result = await this.executeQuery({ sql, params: [] });
|
|
1158
|
+
if (!result || !Array.isArray(result)) return false;
|
|
1159
|
+
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
1160
|
+
}
|
|
1161
|
+
getTableName(tableName) {
|
|
1162
|
+
return `${this.tablePrefix}${tableName}`;
|
|
1163
|
+
}
|
|
1164
|
+
formatSqlParams(params) {
|
|
1165
|
+
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
1166
|
+
}
|
|
1167
|
+
async executeWorkersBindingQuery({
|
|
1168
|
+
sql,
|
|
1169
|
+
params = [],
|
|
1170
|
+
first = false
|
|
1171
|
+
}) {
|
|
1172
|
+
if (!this.binding) {
|
|
1173
|
+
throw new Error("Workers binding is not configured");
|
|
1174
|
+
}
|
|
1175
|
+
try {
|
|
1176
|
+
const statement = this.binding.prepare(sql);
|
|
1177
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1178
|
+
let result;
|
|
1179
|
+
if (formattedParams.length > 0) {
|
|
1180
|
+
if (first) {
|
|
1181
|
+
result = await statement.bind(...formattedParams).first();
|
|
1182
|
+
if (!result) return null;
|
|
1183
|
+
return result;
|
|
1184
|
+
} else {
|
|
1185
|
+
result = await statement.bind(...formattedParams).all();
|
|
1186
|
+
const results = result.results || [];
|
|
1187
|
+
return results;
|
|
1188
|
+
}
|
|
1189
|
+
} else {
|
|
1190
|
+
if (first) {
|
|
1191
|
+
result = await statement.first();
|
|
1192
|
+
if (!result) return null;
|
|
1193
|
+
return result;
|
|
1194
|
+
} else {
|
|
1195
|
+
result = await statement.all();
|
|
1196
|
+
const results = result.results || [];
|
|
1197
|
+
return results;
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
} catch (error$1) {
|
|
1201
|
+
throw new error.MastraError(
|
|
1202
|
+
{
|
|
1203
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_WORKERS_BINDING_QUERY_FAILED",
|
|
1204
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1205
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1206
|
+
details: { sql }
|
|
1207
|
+
},
|
|
1208
|
+
error$1
|
|
1209
|
+
);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
async executeRestQuery({
|
|
1213
|
+
sql,
|
|
1214
|
+
params = [],
|
|
1215
|
+
first = false
|
|
1216
|
+
}) {
|
|
1217
|
+
if (!this.client) {
|
|
1218
|
+
throw new Error("D1 client is not configured");
|
|
1219
|
+
}
|
|
1220
|
+
try {
|
|
1221
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1222
|
+
const response = await this.client.query({
|
|
1223
|
+
sql,
|
|
1224
|
+
params: formattedParams
|
|
1225
|
+
});
|
|
1226
|
+
if (!response.result) {
|
|
1227
|
+
return first ? null : [];
|
|
1228
|
+
}
|
|
1229
|
+
if (first) {
|
|
1230
|
+
return response.result[0] || null;
|
|
1231
|
+
}
|
|
1232
|
+
return response.result;
|
|
1233
|
+
} catch (error$1) {
|
|
1234
|
+
throw new error.MastraError(
|
|
1235
|
+
{
|
|
1236
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_REST_QUERY_FAILED",
|
|
1237
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1238
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1239
|
+
details: { sql }
|
|
1240
|
+
},
|
|
1241
|
+
error$1
|
|
1242
|
+
);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
async executeQuery(options) {
|
|
1246
|
+
if (this.binding) {
|
|
1247
|
+
return this.executeWorkersBindingQuery(options);
|
|
1248
|
+
} else if (this.client) {
|
|
1249
|
+
return this.executeRestQuery(options);
|
|
1250
|
+
} else {
|
|
1251
|
+
throw new Error("Neither binding nor client is configured");
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
async getTableColumns(tableName) {
|
|
1255
|
+
try {
|
|
1256
|
+
const sql = `PRAGMA table_info(${tableName})`;
|
|
1257
|
+
const result = await this.executeQuery({ sql });
|
|
1258
|
+
if (!result || !Array.isArray(result)) {
|
|
1259
|
+
return [];
|
|
1260
|
+
}
|
|
1261
|
+
return result.map((row) => ({
|
|
1262
|
+
name: row.name,
|
|
1263
|
+
type: row.type
|
|
1264
|
+
}));
|
|
1265
|
+
} catch (error) {
|
|
1266
|
+
this.logger.warn(`Failed to get table columns for ${tableName}:`, error);
|
|
1267
|
+
return [];
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
serializeValue(value) {
|
|
1271
|
+
if (value === null || value === void 0) {
|
|
1272
|
+
return null;
|
|
1273
|
+
}
|
|
1274
|
+
if (value instanceof Date) {
|
|
1275
|
+
return value.toISOString();
|
|
1276
|
+
}
|
|
1277
|
+
if (typeof value === "object") {
|
|
1278
|
+
return JSON.stringify(value);
|
|
1279
|
+
}
|
|
1280
|
+
return value;
|
|
1281
|
+
}
|
|
1282
|
+
getSqlType(type) {
|
|
1283
|
+
switch (type) {
|
|
1284
|
+
case "bigint":
|
|
1285
|
+
return "INTEGER";
|
|
1286
|
+
// SQLite uses INTEGER for all integer sizes
|
|
1287
|
+
case "jsonb":
|
|
1288
|
+
return "TEXT";
|
|
1289
|
+
// Store JSON as TEXT in SQLite
|
|
1290
|
+
default:
|
|
1291
|
+
return super.getSqlType(type);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
async createTable({
|
|
1295
|
+
tableName,
|
|
1296
|
+
schema
|
|
1297
|
+
}) {
|
|
1298
|
+
try {
|
|
1299
|
+
const fullTableName = this.getTableName(tableName);
|
|
1300
|
+
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
1301
|
+
const type = this.getSqlType(colDef.type);
|
|
1302
|
+
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
1303
|
+
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
1304
|
+
return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
|
|
1305
|
+
});
|
|
1306
|
+
const tableConstraints = [];
|
|
1307
|
+
if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
|
|
1308
|
+
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
1309
|
+
}
|
|
1310
|
+
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
1311
|
+
const { sql, params } = query.build();
|
|
1312
|
+
await this.executeQuery({ sql, params });
|
|
1313
|
+
this.logger.debug(`Created table ${fullTableName}`);
|
|
1314
|
+
} catch (error$1) {
|
|
1315
|
+
throw new error.MastraError(
|
|
1316
|
+
{
|
|
1317
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CREATE_TABLE_FAILED",
|
|
1318
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1319
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1320
|
+
details: { tableName }
|
|
1321
|
+
},
|
|
1322
|
+
error$1
|
|
1323
|
+
);
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
async clearTable({ tableName }) {
|
|
1327
|
+
try {
|
|
1328
|
+
const fullTableName = this.getTableName(tableName);
|
|
1329
|
+
const query = createSqlBuilder().delete(fullTableName);
|
|
1330
|
+
const { sql, params } = query.build();
|
|
1331
|
+
await this.executeQuery({ sql, params });
|
|
1332
|
+
this.logger.debug(`Cleared table ${fullTableName}`);
|
|
1333
|
+
} catch (error$1) {
|
|
1334
|
+
throw new error.MastraError(
|
|
1335
|
+
{
|
|
1336
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CLEAR_TABLE_FAILED",
|
|
1337
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1338
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1339
|
+
details: { tableName }
|
|
1340
|
+
},
|
|
1341
|
+
error$1
|
|
1342
|
+
);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
async dropTable({ tableName }) {
|
|
1346
|
+
try {
|
|
1347
|
+
const fullTableName = this.getTableName(tableName);
|
|
1348
|
+
const sql = `DROP TABLE IF EXISTS ${fullTableName}`;
|
|
1349
|
+
await this.executeQuery({ sql });
|
|
1350
|
+
this.logger.debug(`Dropped table ${fullTableName}`);
|
|
1351
|
+
} catch (error$1) {
|
|
1352
|
+
throw new error.MastraError(
|
|
1353
|
+
{
|
|
1354
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_DROP_TABLE_FAILED",
|
|
1355
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1356
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1357
|
+
details: { tableName }
|
|
1358
|
+
},
|
|
1359
|
+
error$1
|
|
1360
|
+
);
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
async alterTable(args) {
|
|
1364
|
+
try {
|
|
1365
|
+
const fullTableName = this.getTableName(args.tableName);
|
|
1366
|
+
const existingColumns = await this.getTableColumns(fullTableName);
|
|
1367
|
+
const existingColumnNames = new Set(existingColumns.map((col) => col.name));
|
|
1368
|
+
for (const [columnName, column] of Object.entries(args.schema)) {
|
|
1369
|
+
if (!existingColumnNames.has(columnName) && args.ifNotExists.includes(columnName)) {
|
|
1370
|
+
const sqlType = this.getSqlType(column.type);
|
|
1371
|
+
const defaultValue = this.getDefaultValue(column.type);
|
|
1372
|
+
const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${sqlType} ${defaultValue}`;
|
|
1373
|
+
await this.executeQuery({ sql });
|
|
1374
|
+
this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
} catch (error$1) {
|
|
1378
|
+
throw new error.MastraError(
|
|
1379
|
+
{
|
|
1380
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_ALTER_TABLE_FAILED",
|
|
1381
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1382
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1383
|
+
details: { tableName: args.tableName }
|
|
1384
|
+
},
|
|
1385
|
+
error$1
|
|
1386
|
+
);
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
async insert({ tableName, record }) {
|
|
1390
|
+
try {
|
|
1391
|
+
const fullTableName = this.getTableName(tableName);
|
|
1392
|
+
const processedRecord = await this.processRecord(record);
|
|
1393
|
+
const columns = Object.keys(processedRecord);
|
|
1394
|
+
const values = Object.values(processedRecord);
|
|
1395
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1396
|
+
const { sql, params } = query.build();
|
|
1397
|
+
await this.executeQuery({ sql, params });
|
|
1398
|
+
} catch (error$1) {
|
|
1399
|
+
throw new error.MastraError(
|
|
1400
|
+
{
|
|
1401
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_INSERT_FAILED",
|
|
1402
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1403
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1404
|
+
details: { tableName }
|
|
1405
|
+
},
|
|
1406
|
+
error$1
|
|
1407
|
+
);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
async batchInsert({ tableName, records }) {
|
|
1411
|
+
try {
|
|
1412
|
+
if (records.length === 0) return;
|
|
1413
|
+
const fullTableName = this.getTableName(tableName);
|
|
1414
|
+
const processedRecords = await Promise.all(records.map((record) => this.processRecord(record)));
|
|
1415
|
+
const columns = Object.keys(processedRecords[0] || {});
|
|
1416
|
+
for (const record of processedRecords) {
|
|
1417
|
+
const values = Object.values(record);
|
|
1418
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1419
|
+
const { sql, params } = query.build();
|
|
1420
|
+
await this.executeQuery({ sql, params });
|
|
1421
|
+
}
|
|
1422
|
+
} catch (error$1) {
|
|
1423
|
+
throw new error.MastraError(
|
|
1424
|
+
{
|
|
1425
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_BATCH_INSERT_FAILED",
|
|
1426
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1427
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1428
|
+
details: { tableName }
|
|
1429
|
+
},
|
|
1430
|
+
error$1
|
|
1431
|
+
);
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
async load({ tableName, keys }) {
|
|
1435
|
+
try {
|
|
1436
|
+
const fullTableName = this.getTableName(tableName);
|
|
1437
|
+
const query = createSqlBuilder().select("*").from(fullTableName);
|
|
1438
|
+
let firstKey = true;
|
|
1439
|
+
for (const [key, value] of Object.entries(keys)) {
|
|
1440
|
+
if (firstKey) {
|
|
1441
|
+
query.where(`${key} = ?`, value);
|
|
1442
|
+
firstKey = false;
|
|
1443
|
+
} else {
|
|
1444
|
+
query.andWhere(`${key} = ?`, value);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
query.limit(1);
|
|
1448
|
+
const { sql, params } = query.build();
|
|
1449
|
+
const result = await this.executeQuery({ sql, params, first: true });
|
|
1450
|
+
if (!result) {
|
|
1451
|
+
return null;
|
|
1452
|
+
}
|
|
1453
|
+
const deserializedResult = {};
|
|
1454
|
+
for (const [key, value] of Object.entries(result)) {
|
|
1455
|
+
deserializedResult[key] = deserializeValue(value);
|
|
1456
|
+
}
|
|
1457
|
+
return deserializedResult;
|
|
1458
|
+
} catch (error$1) {
|
|
1459
|
+
throw new error.MastraError(
|
|
1460
|
+
{
|
|
1461
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_LOAD_FAILED",
|
|
1462
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1463
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1464
|
+
details: { tableName }
|
|
1465
|
+
},
|
|
1466
|
+
error$1
|
|
1467
|
+
);
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
async processRecord(record) {
|
|
1471
|
+
const processed = {};
|
|
1472
|
+
for (const [key, value] of Object.entries(record)) {
|
|
1473
|
+
processed[key] = this.serializeValue(value);
|
|
1474
|
+
}
|
|
1475
|
+
return processed;
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* Upsert multiple records in a batch operation
|
|
1479
|
+
* @param tableName The table to insert into
|
|
1480
|
+
* @param records The records to insert
|
|
1481
|
+
*/
|
|
1482
|
+
async batchUpsert({ tableName, records }) {
|
|
1483
|
+
if (records.length === 0) return;
|
|
1484
|
+
const fullTableName = this.getTableName(tableName);
|
|
1485
|
+
try {
|
|
1486
|
+
const batchSize = 50;
|
|
1487
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
1488
|
+
const batch = records.slice(i, i + batchSize);
|
|
1489
|
+
const recordsToInsert = batch;
|
|
1490
|
+
if (recordsToInsert.length > 0) {
|
|
1491
|
+
const firstRecord = recordsToInsert[0];
|
|
1492
|
+
const columns = Object.keys(firstRecord || {});
|
|
1493
|
+
for (const record of recordsToInsert) {
|
|
1494
|
+
const values = columns.map((col) => {
|
|
1495
|
+
if (!record) return null;
|
|
1496
|
+
const value = typeof col === "string" ? record[col] : null;
|
|
1497
|
+
return this.serializeValue(value);
|
|
1498
|
+
});
|
|
1499
|
+
const recordToUpsert = columns.reduce(
|
|
1500
|
+
(acc, col) => {
|
|
1501
|
+
if (col !== "createdAt") acc[col] = `excluded.${col}`;
|
|
1502
|
+
return acc;
|
|
1503
|
+
},
|
|
1504
|
+
{}
|
|
1505
|
+
);
|
|
1506
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], recordToUpsert);
|
|
1507
|
+
const { sql, params } = query.build();
|
|
1508
|
+
await this.executeQuery({ sql, params });
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
this.logger.debug(
|
|
1512
|
+
`Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
|
|
1513
|
+
);
|
|
1514
|
+
}
|
|
1515
|
+
this.logger.debug(`Successfully batch upserted ${records.length} records into ${tableName}`);
|
|
1516
|
+
} catch (error$1) {
|
|
1517
|
+
throw new error.MastraError(
|
|
1518
|
+
{
|
|
1519
|
+
id: "CLOUDFLARE_D1_STORAGE_BATCH_UPSERT_ERROR",
|
|
1520
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1521
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1522
|
+
text: `Failed to batch upsert into ${tableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1523
|
+
details: { tableName }
|
|
1524
|
+
},
|
|
1525
|
+
error$1
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
};
|
|
1530
|
+
function transformScoreRow(row) {
|
|
1531
|
+
let input = void 0;
|
|
1532
|
+
if (row.input) {
|
|
1533
|
+
try {
|
|
1534
|
+
input = JSON.parse(row.input);
|
|
1535
|
+
} catch {
|
|
1536
|
+
input = row.input;
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
return {
|
|
1540
|
+
...row,
|
|
1541
|
+
input,
|
|
1542
|
+
createdAt: row.createdAtZ || row.createdAt,
|
|
1543
|
+
updatedAt: row.updatedAtZ || row.updatedAt
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
var ScoresStorageD1 = class extends storage.ScoresStorage {
|
|
1547
|
+
operations;
|
|
1548
|
+
constructor({ operations }) {
|
|
1549
|
+
super();
|
|
1550
|
+
this.operations = operations;
|
|
1551
|
+
}
|
|
1552
|
+
async getScoreById({ id }) {
|
|
1553
|
+
try {
|
|
1554
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1555
|
+
const query = createSqlBuilder().select("*").from(fullTableName).where("id = ?", id);
|
|
1556
|
+
const { sql, params } = query.build();
|
|
1557
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1558
|
+
if (!result) {
|
|
1559
|
+
return null;
|
|
1560
|
+
}
|
|
1561
|
+
return transformScoreRow(result);
|
|
1562
|
+
} catch (error$1) {
|
|
1563
|
+
throw new error.MastraError(
|
|
1564
|
+
{
|
|
1565
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORE_BY_ID_FAILED",
|
|
1566
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1567
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1568
|
+
},
|
|
1569
|
+
error$1
|
|
1570
|
+
);
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
async saveScore(score) {
|
|
1574
|
+
try {
|
|
1575
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1576
|
+
const { input, ...rest } = score;
|
|
1577
|
+
const serializedRecord = {};
|
|
1578
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
1579
|
+
if (value !== null && value !== void 0) {
|
|
1580
|
+
if (typeof value === "object") {
|
|
1581
|
+
serializedRecord[key] = JSON.stringify(value);
|
|
1582
|
+
} else {
|
|
1583
|
+
serializedRecord[key] = value;
|
|
1584
|
+
}
|
|
1585
|
+
} else {
|
|
1586
|
+
serializedRecord[key] = null;
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
serializedRecord.input = JSON.stringify(input);
|
|
1590
|
+
serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1591
|
+
serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1592
|
+
const columns = Object.keys(serializedRecord);
|
|
1593
|
+
const values = Object.values(serializedRecord);
|
|
1594
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1595
|
+
const { sql, params } = query.build();
|
|
1596
|
+
await this.operations.executeQuery({ sql, params });
|
|
1597
|
+
const scoreFromDb = await this.getScoreById({ id: score.id });
|
|
1598
|
+
return { score: scoreFromDb };
|
|
1599
|
+
} catch (error$1) {
|
|
1600
|
+
throw new error.MastraError(
|
|
1601
|
+
{
|
|
1602
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED",
|
|
1603
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1604
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1605
|
+
},
|
|
1606
|
+
error$1
|
|
1607
|
+
);
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
async getScoresByScorerId({
|
|
1611
|
+
scorerId,
|
|
1612
|
+
pagination
|
|
1613
|
+
}) {
|
|
1614
|
+
try {
|
|
1615
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1616
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1617
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1618
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1619
|
+
if (total === 0) {
|
|
1620
|
+
return {
|
|
1621
|
+
pagination: {
|
|
1622
|
+
total: 0,
|
|
1623
|
+
page: pagination.page,
|
|
1624
|
+
perPage: pagination.perPage,
|
|
1625
|
+
hasMore: false
|
|
1626
|
+
},
|
|
1627
|
+
scores: []
|
|
1628
|
+
};
|
|
1629
|
+
}
|
|
1630
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
|
|
1631
|
+
const { sql, params } = selectQuery.build();
|
|
1632
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1633
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1634
|
+
return {
|
|
1635
|
+
pagination: {
|
|
1636
|
+
total,
|
|
1637
|
+
page: pagination.page,
|
|
1638
|
+
perPage: pagination.perPage,
|
|
1639
|
+
hasMore: total > (pagination.page + 1) * pagination.perPage
|
|
1640
|
+
},
|
|
1641
|
+
scores
|
|
1642
|
+
};
|
|
1643
|
+
} catch (error$1) {
|
|
1644
|
+
throw new error.MastraError(
|
|
1645
|
+
{
|
|
1646
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SCORER_ID_FAILED",
|
|
1647
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1648
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1649
|
+
},
|
|
1650
|
+
error$1
|
|
1651
|
+
);
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
async getScoresByRunId({
|
|
1655
|
+
runId,
|
|
1656
|
+
pagination
|
|
1657
|
+
}) {
|
|
1658
|
+
try {
|
|
1659
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1660
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1661
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1662
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1663
|
+
if (total === 0) {
|
|
1664
|
+
return {
|
|
1665
|
+
pagination: {
|
|
1666
|
+
total: 0,
|
|
1667
|
+
page: pagination.page,
|
|
1668
|
+
perPage: pagination.perPage,
|
|
1669
|
+
hasMore: false
|
|
1670
|
+
},
|
|
1671
|
+
scores: []
|
|
1672
|
+
};
|
|
1673
|
+
}
|
|
1674
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
|
|
1675
|
+
const { sql, params } = selectQuery.build();
|
|
1676
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1677
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1678
|
+
return {
|
|
1679
|
+
pagination: {
|
|
1680
|
+
total,
|
|
1681
|
+
page: pagination.page,
|
|
1682
|
+
perPage: pagination.perPage,
|
|
1683
|
+
hasMore: total > (pagination.page + 1) * pagination.perPage
|
|
1684
|
+
},
|
|
1685
|
+
scores
|
|
1686
|
+
};
|
|
1687
|
+
} catch (error$1) {
|
|
1688
|
+
throw new error.MastraError(
|
|
1689
|
+
{
|
|
1690
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_RUN_ID_FAILED",
|
|
1691
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1692
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1693
|
+
},
|
|
1694
|
+
error$1
|
|
1695
|
+
);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
async getScoresByEntityId({
|
|
1699
|
+
entityId,
|
|
1700
|
+
entityType,
|
|
1701
|
+
pagination
|
|
1702
|
+
}) {
|
|
1703
|
+
try {
|
|
1704
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_SCORERS);
|
|
1705
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1706
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1707
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1708
|
+
if (total === 0) {
|
|
1709
|
+
return {
|
|
1710
|
+
pagination: {
|
|
1711
|
+
total: 0,
|
|
1712
|
+
page: pagination.page,
|
|
1713
|
+
perPage: pagination.perPage,
|
|
1714
|
+
hasMore: false
|
|
1715
|
+
},
|
|
1716
|
+
scores: []
|
|
1717
|
+
};
|
|
1718
|
+
}
|
|
1719
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(pagination.perPage).offset(pagination.page * pagination.perPage);
|
|
1720
|
+
const { sql, params } = selectQuery.build();
|
|
1721
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1722
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1723
|
+
return {
|
|
1724
|
+
pagination: {
|
|
1725
|
+
total,
|
|
1726
|
+
page: pagination.page,
|
|
1727
|
+
perPage: pagination.perPage,
|
|
1728
|
+
hasMore: total > (pagination.page + 1) * pagination.perPage
|
|
1729
|
+
},
|
|
1730
|
+
scores
|
|
1731
|
+
};
|
|
1732
|
+
} catch (error$1) {
|
|
1733
|
+
throw new error.MastraError(
|
|
1734
|
+
{
|
|
1735
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_ENTITY_ID_FAILED",
|
|
1736
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1737
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
1738
|
+
},
|
|
1739
|
+
error$1
|
|
1740
|
+
);
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
1744
|
+
function isArrayOfRecords2(value) {
|
|
1745
|
+
return value && Array.isArray(value) && value.length > 0;
|
|
1746
|
+
}
|
|
1747
|
+
var TracesStorageD1 = class extends storage.TracesStorage {
|
|
1748
|
+
operations;
|
|
1749
|
+
constructor({ operations }) {
|
|
1750
|
+
super();
|
|
1751
|
+
this.operations = operations;
|
|
1752
|
+
}
|
|
1753
|
+
async getTraces(args) {
|
|
1754
|
+
const paginatedArgs = {
|
|
1755
|
+
name: args.name,
|
|
1756
|
+
scope: args.scope,
|
|
1757
|
+
page: args.page,
|
|
1758
|
+
perPage: args.perPage,
|
|
1759
|
+
attributes: args.attributes,
|
|
1760
|
+
filters: args.filters,
|
|
1761
|
+
dateRange: args.fromDate || args.toDate ? {
|
|
1762
|
+
start: args.fromDate,
|
|
1763
|
+
end: args.toDate
|
|
1764
|
+
} : void 0
|
|
1765
|
+
};
|
|
1766
|
+
try {
|
|
1767
|
+
const result = await this.getTracesPaginated(paginatedArgs);
|
|
1768
|
+
return result.traces;
|
|
1769
|
+
} catch (error$1) {
|
|
1770
|
+
throw new error.MastraError(
|
|
1771
|
+
{
|
|
1772
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_ERROR",
|
|
1773
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1774
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1775
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1776
|
+
details: {
|
|
1777
|
+
name: args.name ?? "",
|
|
1778
|
+
scope: args.scope ?? ""
|
|
1779
|
+
}
|
|
1780
|
+
},
|
|
1781
|
+
error$1
|
|
1782
|
+
);
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
async getTracesPaginated(args) {
|
|
1786
|
+
const { name, scope, page = 0, perPage = 100, attributes, dateRange } = args;
|
|
1787
|
+
const fromDate = dateRange?.start;
|
|
1788
|
+
const toDate = dateRange?.end;
|
|
1789
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_TRACES);
|
|
1790
|
+
try {
|
|
1791
|
+
const dataQuery = createSqlBuilder().select("*").from(fullTableName).where("1=1");
|
|
1792
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("1=1");
|
|
1793
|
+
if (name) {
|
|
1794
|
+
dataQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1795
|
+
countQuery.andWhere("name LIKE ?", `%${name}%`);
|
|
1796
|
+
}
|
|
1797
|
+
if (scope) {
|
|
1798
|
+
dataQuery.andWhere("scope = ?", scope);
|
|
1799
|
+
countQuery.andWhere("scope = ?", scope);
|
|
1800
|
+
}
|
|
1801
|
+
if (attributes && Object.keys(attributes).length > 0) {
|
|
1802
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
1803
|
+
dataQuery.jsonLike("attributes", key, value);
|
|
1804
|
+
countQuery.jsonLike("attributes", key, value);
|
|
762
1805
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
1806
|
+
}
|
|
1807
|
+
if (fromDate) {
|
|
1808
|
+
const fromDateStr = fromDate instanceof Date ? fromDate.toISOString() : fromDate;
|
|
1809
|
+
dataQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1810
|
+
countQuery.andWhere("createdAt >= ?", fromDateStr);
|
|
1811
|
+
}
|
|
1812
|
+
if (toDate) {
|
|
1813
|
+
const toDateStr = toDate instanceof Date ? toDate.toISOString() : toDate;
|
|
1814
|
+
dataQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1815
|
+
countQuery.andWhere("createdAt <= ?", toDateStr);
|
|
1816
|
+
}
|
|
1817
|
+
const allDataResult = await this.operations.executeQuery(
|
|
1818
|
+
createSqlBuilder().select("*").from(fullTableName).where("1=1").build()
|
|
1819
|
+
);
|
|
1820
|
+
console.log("allDataResult", allDataResult);
|
|
1821
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1822
|
+
const total = Number(countResult?.[0]?.count ?? 0);
|
|
1823
|
+
dataQuery.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
|
|
1824
|
+
const results = await this.operations.executeQuery(dataQuery.build());
|
|
1825
|
+
const traces = isArrayOfRecords2(results) ? results.map(
|
|
1826
|
+
(trace) => ({
|
|
1827
|
+
...trace,
|
|
1828
|
+
attributes: deserializeValue(trace.attributes, "jsonb"),
|
|
1829
|
+
status: deserializeValue(trace.status, "jsonb"),
|
|
1830
|
+
events: deserializeValue(trace.events, "jsonb"),
|
|
1831
|
+
links: deserializeValue(trace.links, "jsonb"),
|
|
1832
|
+
other: deserializeValue(trace.other, "jsonb")
|
|
1833
|
+
})
|
|
1834
|
+
) : [];
|
|
1835
|
+
return {
|
|
1836
|
+
traces,
|
|
1837
|
+
total,
|
|
1838
|
+
page,
|
|
1839
|
+
perPage,
|
|
1840
|
+
hasMore: page * perPage + traces.length < total
|
|
1841
|
+
};
|
|
1842
|
+
} catch (error$1) {
|
|
1843
|
+
const mastraError = new error.MastraError(
|
|
1844
|
+
{
|
|
1845
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_TRACES_PAGINATED_ERROR",
|
|
1846
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1847
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1848
|
+
text: `Failed to retrieve traces: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1849
|
+
details: { name: name ?? "", scope: scope ?? "" }
|
|
1850
|
+
},
|
|
1851
|
+
error$1
|
|
1852
|
+
);
|
|
1853
|
+
this.logger?.error(mastraError.toString());
|
|
1854
|
+
this.logger?.trackException(mastraError);
|
|
1855
|
+
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
773
1856
|
}
|
|
774
1857
|
}
|
|
1858
|
+
async batchTraceInsert({ records }) {
|
|
1859
|
+
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
1860
|
+
await this.operations.batchInsert({
|
|
1861
|
+
tableName: storage.TABLE_TRACES,
|
|
1862
|
+
records
|
|
1863
|
+
});
|
|
1864
|
+
}
|
|
1865
|
+
};
|
|
1866
|
+
var WorkflowsStorageD1 = class extends storage.WorkflowsStorage {
|
|
1867
|
+
operations;
|
|
1868
|
+
constructor({ operations }) {
|
|
1869
|
+
super();
|
|
1870
|
+
this.operations = operations;
|
|
1871
|
+
}
|
|
775
1872
|
async persistWorkflowSnapshot({
|
|
776
1873
|
workflowName,
|
|
777
1874
|
runId,
|
|
778
1875
|
snapshot
|
|
779
1876
|
}) {
|
|
780
|
-
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1877
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
781
1878
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
782
|
-
const currentSnapshot = await this.load({
|
|
1879
|
+
const currentSnapshot = await this.operations.load({
|
|
783
1880
|
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
784
1881
|
keys: { workflow_name: workflowName, run_id: runId }
|
|
785
1882
|
});
|
|
@@ -794,7 +1891,7 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
794
1891
|
createdAt: now,
|
|
795
1892
|
updatedAt: now
|
|
796
1893
|
};
|
|
797
|
-
const processedRecord = await this.processRecord(persisting);
|
|
1894
|
+
const processedRecord = await this.operations.processRecord(persisting);
|
|
798
1895
|
const columns = Object.keys(processedRecord);
|
|
799
1896
|
const values = Object.values(processedRecord);
|
|
800
1897
|
const updateMap = {
|
|
@@ -805,143 +1902,43 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
805
1902
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
806
1903
|
const { sql, params } = query.build();
|
|
807
1904
|
try {
|
|
808
|
-
await this.executeQuery({ sql, params });
|
|
809
|
-
} catch (error) {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
1905
|
+
await this.operations.executeQuery({ sql, params });
|
|
1906
|
+
} catch (error$1) {
|
|
1907
|
+
throw new error.MastraError(
|
|
1908
|
+
{
|
|
1909
|
+
id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
|
|
1910
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1911
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1912
|
+
text: `Failed to persist workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1913
|
+
details: { workflowName, runId }
|
|
1914
|
+
},
|
|
1915
|
+
error$1
|
|
1916
|
+
);
|
|
814
1917
|
}
|
|
815
1918
|
}
|
|
816
1919
|
async loadWorkflowSnapshot(params) {
|
|
817
1920
|
const { workflowName, runId } = params;
|
|
818
1921
|
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
|
-
try {
|
|
837
|
-
const batchSize = 50;
|
|
838
|
-
for (let i = 0; i < records.length; i += batchSize) {
|
|
839
|
-
const batch = records.slice(i, i + batchSize);
|
|
840
|
-
const recordsToInsert = batch;
|
|
841
|
-
if (recordsToInsert.length > 0) {
|
|
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
1922
|
try {
|
|
878
|
-
const
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
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);
|
|
1923
|
+
const d = await this.operations.load({
|
|
1924
|
+
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
1925
|
+
keys: {
|
|
1926
|
+
workflow_name: workflowName,
|
|
1927
|
+
run_id: runId
|
|
888
1928
|
}
|
|
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
1929
|
});
|
|
944
|
-
return
|
|
1930
|
+
return d ? d.snapshot : null;
|
|
1931
|
+
} catch (error$1) {
|
|
1932
|
+
throw new error.MastraError(
|
|
1933
|
+
{
|
|
1934
|
+
id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
|
|
1935
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1936
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1937
|
+
text: `Failed to load workflow snapshot: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
1938
|
+
details: { workflowName, runId }
|
|
1939
|
+
},
|
|
1940
|
+
error$1
|
|
1941
|
+
);
|
|
945
1942
|
}
|
|
946
1943
|
}
|
|
947
1944
|
parseWorkflowRun(row) {
|
|
@@ -957,17 +1954,11 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
957
1954
|
workflowName: row.workflow_name,
|
|
958
1955
|
runId: row.run_id,
|
|
959
1956
|
snapshot: parsedSnapshot,
|
|
960
|
-
createdAt:
|
|
961
|
-
updatedAt:
|
|
1957
|
+
createdAt: storage.ensureDate(row.createdAt),
|
|
1958
|
+
updatedAt: storage.ensureDate(row.updatedAt),
|
|
962
1959
|
resourceId: row.resourceId
|
|
963
1960
|
};
|
|
964
1961
|
}
|
|
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
1962
|
async getWorkflowRuns({
|
|
972
1963
|
workflowName,
|
|
973
1964
|
fromDate,
|
|
@@ -976,13 +1967,13 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
976
1967
|
offset,
|
|
977
1968
|
resourceId
|
|
978
1969
|
} = {}) {
|
|
979
|
-
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1970
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
980
1971
|
try {
|
|
981
1972
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
982
1973
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
983
1974
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
984
1975
|
if (resourceId) {
|
|
985
|
-
const hasResourceId = await this.hasColumn(fullTableName, "resourceId");
|
|
1976
|
+
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
986
1977
|
if (hasResourceId) {
|
|
987
1978
|
builder.whereAnd("resourceId = ?", resourceId);
|
|
988
1979
|
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
@@ -1005,24 +1996,37 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1005
1996
|
let total = 0;
|
|
1006
1997
|
if (limit !== void 0 && offset !== void 0) {
|
|
1007
1998
|
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
1008
|
-
const countResult = await this.executeQuery({
|
|
1999
|
+
const countResult = await this.operations.executeQuery({
|
|
2000
|
+
sql: countSql,
|
|
2001
|
+
params: countParams,
|
|
2002
|
+
first: true
|
|
2003
|
+
});
|
|
1009
2004
|
total = Number(countResult?.count ?? 0);
|
|
1010
2005
|
}
|
|
1011
|
-
const results = await this.executeQuery({ sql, params });
|
|
2006
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1012
2007
|
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
1013
2008
|
return { runs, total: total || runs.length };
|
|
1014
|
-
} catch (error) {
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
2009
|
+
} catch (error$1) {
|
|
2010
|
+
throw new error.MastraError(
|
|
2011
|
+
{
|
|
2012
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUNS_ERROR",
|
|
2013
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2014
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2015
|
+
text: `Failed to retrieve workflow runs: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2016
|
+
details: {
|
|
2017
|
+
workflowName: workflowName ?? "",
|
|
2018
|
+
resourceId: resourceId ?? ""
|
|
2019
|
+
}
|
|
2020
|
+
},
|
|
2021
|
+
error$1
|
|
2022
|
+
);
|
|
1019
2023
|
}
|
|
1020
2024
|
}
|
|
1021
2025
|
async getWorkflowRunById({
|
|
1022
2026
|
runId,
|
|
1023
2027
|
workflowName
|
|
1024
2028
|
}) {
|
|
1025
|
-
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
2029
|
+
const fullTableName = this.operations.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1026
2030
|
try {
|
|
1027
2031
|
const conditions = [];
|
|
1028
2032
|
const params = [];
|
|
@@ -1036,15 +2040,292 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
1036
2040
|
}
|
|
1037
2041
|
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
1038
2042
|
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
1039
|
-
const result = await this.executeQuery({ sql, params, first: true });
|
|
2043
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1040
2044
|
if (!result) return null;
|
|
1041
2045
|
return this.parseWorkflowRun(result);
|
|
1042
|
-
} catch (error) {
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
2046
|
+
} catch (error$1) {
|
|
2047
|
+
throw new error.MastraError(
|
|
2048
|
+
{
|
|
2049
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_WORKFLOW_RUN_BY_ID_ERROR",
|
|
2050
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2051
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
2052
|
+
text: `Failed to retrieve workflow run by ID: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
|
|
2053
|
+
details: { runId, workflowName: workflowName ?? "" }
|
|
2054
|
+
},
|
|
2055
|
+
error$1
|
|
2056
|
+
);
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
};
|
|
2060
|
+
|
|
2061
|
+
// src/storage/index.ts
|
|
2062
|
+
var D1Store = class extends storage.MastraStorage {
|
|
2063
|
+
client;
|
|
2064
|
+
binding;
|
|
2065
|
+
// D1Database binding
|
|
2066
|
+
tablePrefix;
|
|
2067
|
+
stores;
|
|
2068
|
+
/**
|
|
2069
|
+
* Creates a new D1Store instance
|
|
2070
|
+
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
2071
|
+
*/
|
|
2072
|
+
constructor(config) {
|
|
2073
|
+
try {
|
|
2074
|
+
super({ name: "D1" });
|
|
2075
|
+
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
2076
|
+
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
2077
|
+
}
|
|
2078
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
2079
|
+
if ("binding" in config) {
|
|
2080
|
+
if (!config.binding) {
|
|
2081
|
+
throw new Error("D1 binding is required when using Workers Binding API");
|
|
2082
|
+
}
|
|
2083
|
+
this.binding = config.binding;
|
|
2084
|
+
this.logger.info("Using D1 Workers Binding API");
|
|
2085
|
+
} else if ("client" in config) {
|
|
2086
|
+
if (!config.client) {
|
|
2087
|
+
throw new Error("D1 client is required when using D1ClientConfig");
|
|
2088
|
+
}
|
|
2089
|
+
this.client = config.client;
|
|
2090
|
+
this.logger.info("Using D1 Client");
|
|
2091
|
+
} else {
|
|
2092
|
+
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
2093
|
+
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
2094
|
+
}
|
|
2095
|
+
const cfClient = new Cloudflare__default.default({
|
|
2096
|
+
apiToken: config.apiToken
|
|
2097
|
+
});
|
|
2098
|
+
this.client = {
|
|
2099
|
+
query: ({ sql, params }) => {
|
|
2100
|
+
return cfClient.d1.database.query(config.databaseId, {
|
|
2101
|
+
account_id: config.accountId,
|
|
2102
|
+
sql,
|
|
2103
|
+
params
|
|
2104
|
+
});
|
|
2105
|
+
}
|
|
2106
|
+
};
|
|
2107
|
+
this.logger.info("Using D1 REST API");
|
|
2108
|
+
}
|
|
2109
|
+
} catch (error$1) {
|
|
2110
|
+
throw new error.MastraError(
|
|
2111
|
+
{
|
|
2112
|
+
id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
|
|
2113
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2114
|
+
category: error.ErrorCategory.SYSTEM,
|
|
2115
|
+
text: "Error initializing D1Store"
|
|
2116
|
+
},
|
|
2117
|
+
error$1
|
|
2118
|
+
);
|
|
1047
2119
|
}
|
|
2120
|
+
const operations = new StoreOperationsD1({
|
|
2121
|
+
client: this.client,
|
|
2122
|
+
binding: this.binding,
|
|
2123
|
+
tablePrefix: this.tablePrefix
|
|
2124
|
+
});
|
|
2125
|
+
const scores = new ScoresStorageD1({
|
|
2126
|
+
operations
|
|
2127
|
+
});
|
|
2128
|
+
const legacyEvals = new LegacyEvalsStorageD1({
|
|
2129
|
+
operations
|
|
2130
|
+
});
|
|
2131
|
+
const traces = new TracesStorageD1({
|
|
2132
|
+
operations
|
|
2133
|
+
});
|
|
2134
|
+
const workflows = new WorkflowsStorageD1({
|
|
2135
|
+
operations
|
|
2136
|
+
});
|
|
2137
|
+
const memory = new MemoryStorageD1({
|
|
2138
|
+
operations
|
|
2139
|
+
});
|
|
2140
|
+
this.stores = {
|
|
2141
|
+
operations,
|
|
2142
|
+
scores,
|
|
2143
|
+
legacyEvals,
|
|
2144
|
+
traces,
|
|
2145
|
+
workflows,
|
|
2146
|
+
memory
|
|
2147
|
+
};
|
|
2148
|
+
}
|
|
2149
|
+
get supports() {
|
|
2150
|
+
return {
|
|
2151
|
+
selectByIncludeResourceScope: true,
|
|
2152
|
+
resourceWorkingMemory: true,
|
|
2153
|
+
hasColumn: true,
|
|
2154
|
+
createTable: true
|
|
2155
|
+
};
|
|
2156
|
+
}
|
|
2157
|
+
async createTable({
|
|
2158
|
+
tableName,
|
|
2159
|
+
schema
|
|
2160
|
+
}) {
|
|
2161
|
+
return this.stores.operations.createTable({ tableName, schema });
|
|
2162
|
+
}
|
|
2163
|
+
/**
|
|
2164
|
+
* Alters table schema to add columns if they don't exist
|
|
2165
|
+
* @param tableName Name of the table
|
|
2166
|
+
* @param schema Schema of the table
|
|
2167
|
+
* @param ifNotExists Array of column names to add if they don't exist
|
|
2168
|
+
*/
|
|
2169
|
+
async alterTable({
|
|
2170
|
+
tableName,
|
|
2171
|
+
schema,
|
|
2172
|
+
ifNotExists
|
|
2173
|
+
}) {
|
|
2174
|
+
return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
|
|
2175
|
+
}
|
|
2176
|
+
async clearTable({ tableName }) {
|
|
2177
|
+
return this.stores.operations.clearTable({ tableName });
|
|
2178
|
+
}
|
|
2179
|
+
async dropTable({ tableName }) {
|
|
2180
|
+
return this.stores.operations.dropTable({ tableName });
|
|
2181
|
+
}
|
|
2182
|
+
async hasColumn(table, column) {
|
|
2183
|
+
return this.stores.operations.hasColumn(table, column);
|
|
2184
|
+
}
|
|
2185
|
+
async insert({ tableName, record }) {
|
|
2186
|
+
return this.stores.operations.insert({ tableName, record });
|
|
2187
|
+
}
|
|
2188
|
+
async load({ tableName, keys }) {
|
|
2189
|
+
return this.stores.operations.load({ tableName, keys });
|
|
2190
|
+
}
|
|
2191
|
+
async getThreadById({ threadId }) {
|
|
2192
|
+
return this.stores.memory.getThreadById({ threadId });
|
|
2193
|
+
}
|
|
2194
|
+
/**
|
|
2195
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
2196
|
+
*/
|
|
2197
|
+
async getThreadsByResourceId({ resourceId }) {
|
|
2198
|
+
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
2199
|
+
}
|
|
2200
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
2201
|
+
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
2202
|
+
}
|
|
2203
|
+
async saveThread({ thread }) {
|
|
2204
|
+
return this.stores.memory.saveThread({ thread });
|
|
2205
|
+
}
|
|
2206
|
+
async updateThread({
|
|
2207
|
+
id,
|
|
2208
|
+
title,
|
|
2209
|
+
metadata
|
|
2210
|
+
}) {
|
|
2211
|
+
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2212
|
+
}
|
|
2213
|
+
async deleteThread({ threadId }) {
|
|
2214
|
+
return this.stores.memory.deleteThread({ threadId });
|
|
2215
|
+
}
|
|
2216
|
+
async saveMessages(args) {
|
|
2217
|
+
return this.stores.memory.saveMessages(args);
|
|
2218
|
+
}
|
|
2219
|
+
async getMessages({
|
|
2220
|
+
threadId,
|
|
2221
|
+
selectBy,
|
|
2222
|
+
format
|
|
2223
|
+
}) {
|
|
2224
|
+
return this.stores.memory.getMessages({ threadId, selectBy, format });
|
|
2225
|
+
}
|
|
2226
|
+
async getMessagesPaginated({
|
|
2227
|
+
threadId,
|
|
2228
|
+
selectBy,
|
|
2229
|
+
format
|
|
2230
|
+
}) {
|
|
2231
|
+
return this.stores.memory.getMessagesPaginated({ threadId, selectBy, format });
|
|
2232
|
+
}
|
|
2233
|
+
async persistWorkflowSnapshot({
|
|
2234
|
+
workflowName,
|
|
2235
|
+
runId,
|
|
2236
|
+
snapshot
|
|
2237
|
+
}) {
|
|
2238
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
|
|
2239
|
+
}
|
|
2240
|
+
async loadWorkflowSnapshot(params) {
|
|
2241
|
+
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2242
|
+
}
|
|
2243
|
+
async getWorkflowRuns({
|
|
2244
|
+
workflowName,
|
|
2245
|
+
fromDate,
|
|
2246
|
+
toDate,
|
|
2247
|
+
limit,
|
|
2248
|
+
offset,
|
|
2249
|
+
resourceId
|
|
2250
|
+
} = {}) {
|
|
2251
|
+
return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
2252
|
+
}
|
|
2253
|
+
async getWorkflowRunById({
|
|
2254
|
+
runId,
|
|
2255
|
+
workflowName
|
|
2256
|
+
}) {
|
|
2257
|
+
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2258
|
+
}
|
|
2259
|
+
/**
|
|
2260
|
+
* Insert multiple records in a batch operation
|
|
2261
|
+
* @param tableName The table to insert into
|
|
2262
|
+
* @param records The records to insert
|
|
2263
|
+
*/
|
|
2264
|
+
async batchInsert({ tableName, records }) {
|
|
2265
|
+
return this.stores.operations.batchInsert({ tableName, records });
|
|
2266
|
+
}
|
|
2267
|
+
/**
|
|
2268
|
+
* @deprecated use getTracesPaginated instead
|
|
2269
|
+
*/
|
|
2270
|
+
async getTraces(args) {
|
|
2271
|
+
return this.stores.traces.getTraces(args);
|
|
2272
|
+
}
|
|
2273
|
+
async getTracesPaginated(args) {
|
|
2274
|
+
return this.stores.traces.getTracesPaginated(args);
|
|
2275
|
+
}
|
|
2276
|
+
/**
|
|
2277
|
+
* @deprecated use getEvals instead
|
|
2278
|
+
*/
|
|
2279
|
+
async getEvalsByAgentName(agentName, type) {
|
|
2280
|
+
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
2281
|
+
}
|
|
2282
|
+
async getEvals(options) {
|
|
2283
|
+
return this.stores.legacyEvals.getEvals(options);
|
|
2284
|
+
}
|
|
2285
|
+
async updateMessages(_args) {
|
|
2286
|
+
return this.stores.memory.updateMessages(_args);
|
|
2287
|
+
}
|
|
2288
|
+
async getResourceById({ resourceId }) {
|
|
2289
|
+
return this.stores.memory.getResourceById({ resourceId });
|
|
2290
|
+
}
|
|
2291
|
+
async saveResource({ resource }) {
|
|
2292
|
+
return this.stores.memory.saveResource({ resource });
|
|
2293
|
+
}
|
|
2294
|
+
async updateResource({
|
|
2295
|
+
resourceId,
|
|
2296
|
+
workingMemory,
|
|
2297
|
+
metadata
|
|
2298
|
+
}) {
|
|
2299
|
+
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
2300
|
+
}
|
|
2301
|
+
async getScoreById({ id: _id }) {
|
|
2302
|
+
return this.stores.scores.getScoreById({ id: _id });
|
|
2303
|
+
}
|
|
2304
|
+
async saveScore(_score) {
|
|
2305
|
+
return this.stores.scores.saveScore(_score);
|
|
2306
|
+
}
|
|
2307
|
+
async getScoresByRunId({
|
|
2308
|
+
runId: _runId,
|
|
2309
|
+
pagination: _pagination
|
|
2310
|
+
}) {
|
|
2311
|
+
return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2312
|
+
}
|
|
2313
|
+
async getScoresByEntityId({
|
|
2314
|
+
entityId: _entityId,
|
|
2315
|
+
entityType: _entityType,
|
|
2316
|
+
pagination: _pagination
|
|
2317
|
+
}) {
|
|
2318
|
+
return this.stores.scores.getScoresByEntityId({
|
|
2319
|
+
entityId: _entityId,
|
|
2320
|
+
entityType: _entityType,
|
|
2321
|
+
pagination: _pagination
|
|
2322
|
+
});
|
|
2323
|
+
}
|
|
2324
|
+
async getScoresByScorerId({
|
|
2325
|
+
scorerId: _scorerId,
|
|
2326
|
+
pagination: _pagination
|
|
2327
|
+
}) {
|
|
2328
|
+
return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
|
|
1048
2329
|
}
|
|
1049
2330
|
/**
|
|
1050
2331
|
* Close the database connection
|