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