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