@mastra/cloudflare-d1 0.0.0-pg-pool-options-20250428183821 → 0.0.0-rag-chunk-extract-llm-option-20250926183645
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 +1140 -0
- package/LICENSE.md +11 -42
- package/README.md +15 -0
- package/dist/index.cjs +2146 -567
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2124 -545
- package/dist/index.js.map +1 -0
- package/dist/storage/domains/legacy-evals/index.d.ts +20 -0
- package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +90 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/operations/index.d.ts +72 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +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 -313
- package/dist/_tsup-dts-rollup.d.ts +0 -313
- 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) {
|
|
@@ -68,6 +77,10 @@ var SqlBuilder = class {
|
|
|
68
77
|
this.params.push(count);
|
|
69
78
|
return this;
|
|
70
79
|
}
|
|
80
|
+
count() {
|
|
81
|
+
this.sql += "SELECT COUNT(*) AS count";
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
71
84
|
/**
|
|
72
85
|
* Insert a row, or update specific columns on conflict (upsert).
|
|
73
86
|
* @param table Table name
|
|
@@ -77,27 +90,33 @@ var SqlBuilder = class {
|
|
|
77
90
|
* @param updateMap Object mapping columns to update to their new value (e.g. { name: 'excluded.name' })
|
|
78
91
|
*/
|
|
79
92
|
insert(table, columns, values, conflictColumns, updateMap) {
|
|
80
|
-
const
|
|
93
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
94
|
+
const parsedColumns = columns.map((col) => parseSqlIdentifier(col, "column name"));
|
|
95
|
+
const placeholders = parsedColumns.map(() => "?").join(", ");
|
|
81
96
|
if (conflictColumns && updateMap) {
|
|
97
|
+
const parsedConflictColumns = conflictColumns.map((col) => parseSqlIdentifier(col, "column name"));
|
|
82
98
|
const updateClause = Object.entries(updateMap).map(([col, expr]) => `${col} = ${expr}`).join(", ");
|
|
83
|
-
this.sql = `INSERT INTO ${
|
|
99
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${parsedConflictColumns.join(", ")}) DO UPDATE SET ${updateClause}`;
|
|
84
100
|
this.params.push(...values);
|
|
85
101
|
return this;
|
|
86
102
|
}
|
|
87
|
-
this.sql = `INSERT INTO ${
|
|
103
|
+
this.sql = `INSERT INTO ${parsedTableName} (${parsedColumns.join(", ")}) VALUES (${placeholders})`;
|
|
88
104
|
this.params.push(...values);
|
|
89
105
|
return this;
|
|
90
106
|
}
|
|
91
107
|
// Update operations
|
|
92
108
|
update(table, columns, values) {
|
|
93
|
-
const
|
|
94
|
-
|
|
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}`;
|
|
95
113
|
this.params.push(...values);
|
|
96
114
|
return this;
|
|
97
115
|
}
|
|
98
116
|
// Delete operations
|
|
99
117
|
delete(table) {
|
|
100
|
-
|
|
118
|
+
const parsedTableName = parseSqlIdentifier(table, "table name");
|
|
119
|
+
this.sql = `DELETE FROM ${parsedTableName}`;
|
|
101
120
|
return this;
|
|
102
121
|
}
|
|
103
122
|
/**
|
|
@@ -108,9 +127,16 @@ var SqlBuilder = class {
|
|
|
108
127
|
* @returns The builder instance
|
|
109
128
|
*/
|
|
110
129
|
createTable(table, columnDefinitions, tableConstraints) {
|
|
111
|
-
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(", ");
|
|
112
138
|
const constraints = tableConstraints && tableConstraints.length > 0 ? ", " + tableConstraints.join(", ") : "";
|
|
113
|
-
this.sql = `CREATE TABLE IF NOT EXISTS ${
|
|
139
|
+
this.sql = `CREATE TABLE IF NOT EXISTS ${parsedTableName} (${columns}${constraints})`;
|
|
114
140
|
return this;
|
|
115
141
|
}
|
|
116
142
|
/**
|
|
@@ -133,13 +159,10 @@ var SqlBuilder = class {
|
|
|
133
159
|
* @returns The builder instance
|
|
134
160
|
*/
|
|
135
161
|
createIndex(indexName, tableName, columnName, indexType = "") {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
raw(sql, ...params) {
|
|
141
|
-
this.sql = sql;
|
|
142
|
-
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})`;
|
|
143
166
|
return this;
|
|
144
167
|
}
|
|
145
168
|
/**
|
|
@@ -149,11 +172,12 @@ var SqlBuilder = class {
|
|
|
149
172
|
* @param exact If true, will not add % wildcards
|
|
150
173
|
*/
|
|
151
174
|
like(column, value, exact = false) {
|
|
175
|
+
const parsedColumnName = parseSqlIdentifier(column, "column name");
|
|
152
176
|
const likeValue = exact ? value : `%${value}%`;
|
|
153
177
|
if (this.whereAdded) {
|
|
154
|
-
this.sql += ` AND ${
|
|
178
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
155
179
|
} else {
|
|
156
|
-
this.sql += ` WHERE ${
|
|
180
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
157
181
|
this.whereAdded = true;
|
|
158
182
|
}
|
|
159
183
|
this.params.push(likeValue);
|
|
@@ -166,11 +190,13 @@ var SqlBuilder = class {
|
|
|
166
190
|
* @param value The value to match
|
|
167
191
|
*/
|
|
168
192
|
jsonLike(column, key, value) {
|
|
169
|
-
const
|
|
193
|
+
const parsedColumnName = parseSqlIdentifier(column, "column name");
|
|
194
|
+
const parsedKey = parseSqlIdentifier(key, "key name");
|
|
195
|
+
const jsonPattern = `%"${parsedKey}":"${value}"%`;
|
|
170
196
|
if (this.whereAdded) {
|
|
171
|
-
this.sql += ` AND ${
|
|
197
|
+
this.sql += ` AND ${parsedColumnName} LIKE ?`;
|
|
172
198
|
} else {
|
|
173
|
-
this.sql += ` WHERE ${
|
|
199
|
+
this.sql += ` WHERE ${parsedColumnName} LIKE ?`;
|
|
174
200
|
this.whereAdded = true;
|
|
175
201
|
}
|
|
176
202
|
this.params.push(jsonPattern);
|
|
@@ -200,331 +226,311 @@ var SqlBuilder = class {
|
|
|
200
226
|
function createSqlBuilder() {
|
|
201
227
|
return new SqlBuilder();
|
|
202
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
|
+
}
|
|
203
238
|
|
|
204
|
-
// src/storage/
|
|
239
|
+
// src/storage/domains/utils.ts
|
|
205
240
|
function isArrayOfRecords(value) {
|
|
206
241
|
return value && Array.isArray(value) && value.length > 0;
|
|
207
242
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
binding;
|
|
213
|
-
// D1Database binding
|
|
214
|
-
tablePrefix;
|
|
215
|
-
/**
|
|
216
|
-
* Creates a new D1Store instance
|
|
217
|
-
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
218
|
-
*/
|
|
219
|
-
constructor(config) {
|
|
220
|
-
super({ name: "D1" });
|
|
221
|
-
this.tablePrefix = config.tablePrefix || "";
|
|
222
|
-
if ("binding" in config) {
|
|
223
|
-
if (!config.binding) {
|
|
224
|
-
throw new Error("D1 binding is required when using Workers Binding API");
|
|
225
|
-
}
|
|
226
|
-
this.binding = config.binding;
|
|
227
|
-
this.logger.info("Using D1 Workers Binding API");
|
|
228
|
-
} else {
|
|
229
|
-
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
230
|
-
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
231
|
-
}
|
|
232
|
-
this.accountId = config.accountId;
|
|
233
|
-
this.databaseId = config.databaseId;
|
|
234
|
-
this.client = new Cloudflare({
|
|
235
|
-
apiToken: config.apiToken
|
|
236
|
-
});
|
|
237
|
-
this.logger.info("Using D1 REST API");
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
// Helper method to get the full table name with prefix
|
|
241
|
-
getTableName(tableName) {
|
|
242
|
-
return `${this.tablePrefix}${tableName}`;
|
|
243
|
-
}
|
|
244
|
-
formatSqlParams(params) {
|
|
245
|
-
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
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);
|
|
246
247
|
}
|
|
247
|
-
|
|
248
|
-
async createIndexIfNotExists(tableName, columnName, indexType = "") {
|
|
249
|
-
const fullTableName = this.getTableName(tableName);
|
|
250
|
-
const indexName = `idx_${tableName}_${columnName}`;
|
|
248
|
+
if (type === "jsonb" && typeof value === "string") {
|
|
251
249
|
try {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
sql: checkSql,
|
|
256
|
-
params: checkParams,
|
|
257
|
-
first: true
|
|
258
|
-
});
|
|
259
|
-
if (!indexExists) {
|
|
260
|
-
const createQuery = createSqlBuilder().createIndex(indexName, fullTableName, columnName, indexType);
|
|
261
|
-
const { sql: createSql, params: createParams } = createQuery.build();
|
|
262
|
-
await this.executeQuery({ sql: createSql, params: createParams });
|
|
263
|
-
this.logger.debug(`Created index ${indexName} on ${fullTableName}(${columnName})`);
|
|
264
|
-
}
|
|
265
|
-
} catch (error) {
|
|
266
|
-
this.logger.error(`Error creating index on ${fullTableName}(${columnName}):`, {
|
|
267
|
-
message: error instanceof Error ? error.message : String(error)
|
|
268
|
-
});
|
|
250
|
+
return JSON.parse(value);
|
|
251
|
+
} catch {
|
|
252
|
+
return value;
|
|
269
253
|
}
|
|
270
254
|
}
|
|
271
|
-
|
|
272
|
-
sql,
|
|
273
|
-
params = [],
|
|
274
|
-
first = false
|
|
275
|
-
}) {
|
|
276
|
-
if (!this.binding) {
|
|
277
|
-
throw new Error("Workers binding is not configured");
|
|
278
|
-
}
|
|
255
|
+
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
279
256
|
try {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (formattedParams.length > 0) {
|
|
284
|
-
if (first) {
|
|
285
|
-
result = await statement.bind(...formattedParams).first();
|
|
286
|
-
if (!result) return null;
|
|
287
|
-
return result;
|
|
288
|
-
} else {
|
|
289
|
-
result = await statement.bind(...formattedParams).all();
|
|
290
|
-
const results = result.results || [];
|
|
291
|
-
if (result.meta) {
|
|
292
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
293
|
-
}
|
|
294
|
-
return results;
|
|
295
|
-
}
|
|
296
|
-
} else {
|
|
297
|
-
if (first) {
|
|
298
|
-
result = await statement.first();
|
|
299
|
-
if (!result) return null;
|
|
300
|
-
return result;
|
|
301
|
-
} else {
|
|
302
|
-
result = await statement.all();
|
|
303
|
-
const results = result.results || [];
|
|
304
|
-
if (result.meta) {
|
|
305
|
-
this.logger.debug("Query metadata", { meta: result.meta });
|
|
306
|
-
}
|
|
307
|
-
return results;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
} catch (workerError) {
|
|
311
|
-
this.logger.error("Workers Binding API error", {
|
|
312
|
-
message: workerError instanceof Error ? workerError.message : String(workerError),
|
|
313
|
-
sql
|
|
314
|
-
});
|
|
315
|
-
throw new Error(`D1 Workers API error: ${workerError.message}`);
|
|
257
|
+
return JSON.parse(value);
|
|
258
|
+
} catch {
|
|
259
|
+
return value;
|
|
316
260
|
}
|
|
317
261
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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);
|
|
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));
|
|
325
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();
|
|
326
299
|
try {
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
300
|
+
const countResult = await this.operations.executeQuery({
|
|
301
|
+
sql: countSql,
|
|
302
|
+
params: countParams,
|
|
303
|
+
first: true
|
|
331
304
|
});
|
|
332
|
-
const
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
+
};
|
|
338
315
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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
|
+
};
|
|
344
344
|
});
|
|
345
|
-
|
|
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
|
+
);
|
|
346
364
|
}
|
|
347
365
|
}
|
|
348
366
|
/**
|
|
349
|
-
*
|
|
350
|
-
* @param options Query options including SQL, parameters, and whether to return only the first result
|
|
351
|
-
* @returns Query results as an array or a single object if first=true
|
|
367
|
+
* @deprecated use getEvals instead
|
|
352
368
|
*/
|
|
353
|
-
async
|
|
354
|
-
const
|
|
369
|
+
async getEvalsByAgentName(agentName, type) {
|
|
370
|
+
const fullTableName = this.operations.getTableName(TABLE_EVALS);
|
|
355
371
|
try {
|
|
356
|
-
|
|
357
|
-
if (
|
|
358
|
-
|
|
359
|
-
} else if (
|
|
360
|
-
|
|
361
|
-
} else {
|
|
362
|
-
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)");
|
|
363
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
|
+
}) : [];
|
|
364
397
|
} catch (error) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
return "TEXT";
|
|
379
|
-
case "timestamp":
|
|
380
|
-
return "TIMESTAMP";
|
|
381
|
-
case "integer":
|
|
382
|
-
return "INTEGER";
|
|
383
|
-
case "bigint":
|
|
384
|
-
return "INTEGER";
|
|
385
|
-
// SQLite doesn't have a separate BIGINT type
|
|
386
|
-
case "jsonb":
|
|
387
|
-
return "TEXT";
|
|
388
|
-
// Store JSON as TEXT in SQLite
|
|
389
|
-
default:
|
|
390
|
-
return "TEXT";
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
ensureDate(date) {
|
|
394
|
-
if (!date) return void 0;
|
|
395
|
-
return date instanceof Date ? date : new Date(date);
|
|
396
|
-
}
|
|
397
|
-
serializeDate(date) {
|
|
398
|
-
if (!date) return void 0;
|
|
399
|
-
const dateObj = this.ensureDate(date);
|
|
400
|
-
return dateObj?.toISOString();
|
|
401
|
-
}
|
|
402
|
-
// Helper to serialize objects to JSON strings
|
|
403
|
-
serializeValue(value) {
|
|
404
|
-
if (value === null || value === void 0) return null;
|
|
405
|
-
if (value instanceof Date) {
|
|
406
|
-
return this.serializeDate(value);
|
|
407
|
-
}
|
|
408
|
-
if (typeof value === "object") {
|
|
409
|
-
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 [];
|
|
410
411
|
}
|
|
411
|
-
return value;
|
|
412
412
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
if (type === "jsonb" && typeof value === "string") {
|
|
420
|
-
try {
|
|
421
|
-
return JSON.parse(value);
|
|
422
|
-
} catch {
|
|
423
|
-
return value;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
427
|
-
try {
|
|
428
|
-
return JSON.parse(value);
|
|
429
|
-
} catch {
|
|
430
|
-
return value;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
return value;
|
|
413
|
+
};
|
|
414
|
+
var MemoryStorageD1 = class extends MemoryStorage {
|
|
415
|
+
operations;
|
|
416
|
+
constructor({ operations }) {
|
|
417
|
+
super();
|
|
418
|
+
this.operations = operations;
|
|
434
419
|
}
|
|
435
|
-
async
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const fullTableName = this.getTableName(tableName);
|
|
440
|
-
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
441
|
-
const type = this.getSqlType(colDef.type);
|
|
442
|
-
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
443
|
-
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
444
|
-
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 }
|
|
445
424
|
});
|
|
446
|
-
|
|
447
|
-
if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
|
|
448
|
-
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
449
|
-
}
|
|
450
|
-
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
451
|
-
const { sql, params } = query.build();
|
|
452
|
-
try {
|
|
453
|
-
await this.executeQuery({ sql, params });
|
|
454
|
-
this.logger.debug(`Created table ${fullTableName}`);
|
|
455
|
-
} catch (error) {
|
|
456
|
-
this.logger.error(`Error creating table ${fullTableName}:`, {
|
|
457
|
-
message: error instanceof Error ? error.message : String(error)
|
|
458
|
-
});
|
|
459
|
-
throw new Error(`Failed to create table ${fullTableName}: ${error}`);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
async clearTable({ tableName }) {
|
|
463
|
-
const fullTableName = this.getTableName(tableName);
|
|
425
|
+
if (!resource) return null;
|
|
464
426
|
try {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
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
|
+
};
|
|
469
433
|
} catch (error) {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
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;
|
|
480
447
|
}
|
|
481
|
-
return processedRecord;
|
|
482
448
|
}
|
|
483
|
-
async
|
|
484
|
-
const fullTableName = this.getTableName(
|
|
485
|
-
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);
|
|
486
459
|
const columns = Object.keys(processedRecord);
|
|
487
460
|
const values = Object.values(processedRecord);
|
|
488
|
-
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);
|
|
489
468
|
const { sql, params } = query.build();
|
|
490
469
|
try {
|
|
491
|
-
await this.executeQuery({ sql, params });
|
|
470
|
+
await this.operations.executeQuery({ sql, params });
|
|
471
|
+
return resource;
|
|
492
472
|
} catch (error) {
|
|
493
|
-
|
|
494
|
-
|
|
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
|
+
);
|
|
495
483
|
}
|
|
496
484
|
}
|
|
497
|
-
async
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
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 });
|
|
508
500
|
}
|
|
509
|
-
|
|
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);
|
|
510
515
|
const { sql, params } = query.build();
|
|
511
516
|
try {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
const processedResult = {};
|
|
515
|
-
for (const [key, value] of Object.entries(result)) {
|
|
516
|
-
processedResult[key] = this.deserializeValue(value);
|
|
517
|
-
}
|
|
518
|
-
return processedResult;
|
|
517
|
+
await this.operations.executeQuery({ sql, params });
|
|
518
|
+
return updatedResource;
|
|
519
519
|
} catch (error) {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
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
|
+
);
|
|
524
530
|
}
|
|
525
531
|
}
|
|
526
532
|
async getThreadById({ threadId }) {
|
|
527
|
-
const thread = await this.load({
|
|
533
|
+
const thread = await this.operations.load({
|
|
528
534
|
tableName: TABLE_THREADS,
|
|
529
535
|
keys: { id: threadId }
|
|
530
536
|
});
|
|
@@ -532,47 +538,113 @@ var D1Store = class extends MastraStorage {
|
|
|
532
538
|
try {
|
|
533
539
|
return {
|
|
534
540
|
...thread,
|
|
535
|
-
createdAt:
|
|
536
|
-
updatedAt:
|
|
541
|
+
createdAt: ensureDate(thread.createdAt),
|
|
542
|
+
updatedAt: ensureDate(thread.updatedAt),
|
|
537
543
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
538
544
|
};
|
|
539
545
|
} catch (error) {
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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);
|
|
543
558
|
return null;
|
|
544
559
|
}
|
|
545
560
|
}
|
|
561
|
+
/**
|
|
562
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
563
|
+
*/
|
|
546
564
|
async getThreadsByResourceId({ resourceId }) {
|
|
547
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
565
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
548
566
|
try {
|
|
549
567
|
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
550
568
|
const { sql, params } = query.build();
|
|
551
|
-
const results = await this.executeQuery({ sql, params });
|
|
569
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
552
570
|
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
553
571
|
...thread,
|
|
554
|
-
createdAt:
|
|
555
|
-
updatedAt:
|
|
572
|
+
createdAt: ensureDate(thread.createdAt),
|
|
573
|
+
updatedAt: ensureDate(thread.updatedAt),
|
|
556
574
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
557
575
|
}));
|
|
558
576
|
} catch (error) {
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
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);
|
|
562
589
|
return [];
|
|
563
590
|
}
|
|
564
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
|
+
}
|
|
565
637
|
async saveThread({ thread }) {
|
|
566
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
638
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
567
639
|
const threadToSave = {
|
|
568
640
|
id: thread.id,
|
|
569
641
|
resourceId: thread.resourceId,
|
|
570
642
|
title: thread.title,
|
|
571
643
|
metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
|
|
572
|
-
createdAt: thread.createdAt,
|
|
573
|
-
updatedAt: thread.updatedAt
|
|
644
|
+
createdAt: thread.createdAt.toISOString(),
|
|
645
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
574
646
|
};
|
|
575
|
-
const processedRecord = await this.processRecord(threadToSave);
|
|
647
|
+
const processedRecord = await this.operations.processRecord(threadToSave);
|
|
576
648
|
const columns = Object.keys(processedRecord);
|
|
577
649
|
const values = Object.values(processedRecord);
|
|
578
650
|
const updateMap = {
|
|
@@ -585,11 +657,19 @@ var D1Store = class extends MastraStorage {
|
|
|
585
657
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
586
658
|
const { sql, params } = query.build();
|
|
587
659
|
try {
|
|
588
|
-
await this.executeQuery({ sql, params });
|
|
660
|
+
await this.operations.executeQuery({ sql, params });
|
|
589
661
|
return thread;
|
|
590
662
|
} catch (error) {
|
|
591
|
-
|
|
592
|
-
|
|
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
|
+
);
|
|
593
673
|
}
|
|
594
674
|
}
|
|
595
675
|
async updateThread({
|
|
@@ -598,20 +678,21 @@ var D1Store = class extends MastraStorage {
|
|
|
598
678
|
metadata
|
|
599
679
|
}) {
|
|
600
680
|
const thread = await this.getThreadById({ threadId: id });
|
|
601
|
-
if (!thread) {
|
|
602
|
-
throw new Error(`Thread ${id} not found`);
|
|
603
|
-
}
|
|
604
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
605
|
-
const mergedMetadata = {
|
|
606
|
-
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
607
|
-
...metadata
|
|
608
|
-
};
|
|
609
|
-
const columns = ["title", "metadata", "updatedAt"];
|
|
610
|
-
const values = [title, JSON.stringify(mergedMetadata), (/* @__PURE__ */ new Date()).toISOString()];
|
|
611
|
-
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
612
|
-
const { sql, params } = query.build();
|
|
613
681
|
try {
|
|
614
|
-
|
|
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 });
|
|
615
696
|
return {
|
|
616
697
|
...thread,
|
|
617
698
|
title,
|
|
@@ -619,40 +700,64 @@ var D1Store = class extends MastraStorage {
|
|
|
619
700
|
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
620
701
|
...metadata
|
|
621
702
|
},
|
|
622
|
-
updatedAt
|
|
703
|
+
updatedAt
|
|
623
704
|
};
|
|
624
705
|
} catch (error) {
|
|
625
|
-
|
|
626
|
-
|
|
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
|
+
);
|
|
627
716
|
}
|
|
628
717
|
}
|
|
629
718
|
async deleteThread({ threadId }) {
|
|
630
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
719
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
631
720
|
try {
|
|
632
721
|
const deleteThreadQuery = createSqlBuilder().delete(fullTableName).where("id = ?", threadId);
|
|
633
722
|
const { sql: threadSql, params: threadParams } = deleteThreadQuery.build();
|
|
634
|
-
await this.executeQuery({ sql: threadSql, params: threadParams });
|
|
635
|
-
const messagesTableName = this.getTableName(TABLE_MESSAGES);
|
|
723
|
+
await this.operations.executeQuery({ sql: threadSql, params: threadParams });
|
|
724
|
+
const messagesTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
636
725
|
const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
|
|
637
726
|
const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
|
|
638
|
-
await this.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
727
|
+
await this.operations.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
639
728
|
} catch (error) {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
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
|
+
);
|
|
644
739
|
}
|
|
645
740
|
}
|
|
646
|
-
|
|
647
|
-
|
|
741
|
+
async saveMessages(args) {
|
|
742
|
+
const { messages, format = "v1" } = args;
|
|
648
743
|
if (messages.length === 0) return [];
|
|
649
744
|
try {
|
|
650
745
|
const now = /* @__PURE__ */ new Date();
|
|
746
|
+
const threadId = messages[0]?.threadId;
|
|
651
747
|
for (const [i, message] of messages.entries()) {
|
|
652
748
|
if (!message.id) throw new Error(`Message at index ${i} missing id`);
|
|
653
|
-
if (!message.threadId)
|
|
654
|
-
|
|
655
|
-
|
|
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
|
+
}
|
|
656
761
|
const thread = await this.getThreadById({ threadId: message.threadId });
|
|
657
762
|
if (!thread) {
|
|
658
763
|
throw new Error(`Thread ${message.threadId} not found`);
|
|
@@ -666,122 +771,1268 @@ var D1Store = class extends MastraStorage {
|
|
|
666
771
|
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
667
772
|
createdAt: createdAt.toISOString(),
|
|
668
773
|
role: message.role,
|
|
669
|
-
type: message.type
|
|
774
|
+
type: message.type || "v2",
|
|
775
|
+
resourceId: message.resourceId
|
|
670
776
|
};
|
|
671
777
|
});
|
|
672
|
-
await
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
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
|
+
]);
|
|
676
789
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
677
|
-
|
|
790
|
+
const list = new MessageList().add(messages, "memory");
|
|
791
|
+
if (format === `v2`) return list.get.all.v2();
|
|
792
|
+
return list.get.all.v1();
|
|
678
793
|
} catch (error) {
|
|
679
|
-
|
|
680
|
-
|
|
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
|
+
);
|
|
681
803
|
}
|
|
682
804
|
}
|
|
683
|
-
async
|
|
684
|
-
|
|
685
|
-
const
|
|
686
|
-
|
|
687
|
-
const
|
|
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);
|
|
880
|
+
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
881
|
+
}
|
|
882
|
+
const excludeIds = messages.map((m) => m.id);
|
|
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);
|
|
888
|
+
const { sql, params } = query.build();
|
|
889
|
+
const result = await this.operations.executeQuery({ sql, params });
|
|
890
|
+
if (Array.isArray(result)) messages.push(...result);
|
|
891
|
+
messages.sort((a, b) => {
|
|
892
|
+
const aRecord = a;
|
|
893
|
+
const bRecord = b;
|
|
894
|
+
const timeA = new Date(aRecord.createdAt).getTime();
|
|
895
|
+
const timeB = new Date(bRecord.createdAt).getTime();
|
|
896
|
+
return timeA - timeB;
|
|
897
|
+
});
|
|
898
|
+
const processedMessages = messages.map((message) => {
|
|
899
|
+
const processedMsg = {};
|
|
900
|
+
for (const [key, value] of Object.entries(message)) {
|
|
901
|
+
if (key === `type` && value === `v2`) continue;
|
|
902
|
+
processedMsg[key] = deserializeValue(value);
|
|
903
|
+
}
|
|
904
|
+
return processedMsg;
|
|
905
|
+
});
|
|
906
|
+
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
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);
|
|
688
1912
|
try {
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
WITH ordered_messages AS (
|
|
695
|
-
SELECT
|
|
696
|
-
*,
|
|
697
|
-
ROW_NUMBER() OVER (ORDER BY createdAt DESC) AS row_num
|
|
698
|
-
FROM ${fullTableName}
|
|
699
|
-
WHERE thread_id = ?
|
|
700
|
-
)
|
|
701
|
-
SELECT
|
|
702
|
-
m.id,
|
|
703
|
-
m.content,
|
|
704
|
-
m.role,
|
|
705
|
-
m.type,
|
|
706
|
-
m.createdAt,
|
|
707
|
-
m.thread_id AS "threadId"
|
|
708
|
-
FROM ordered_messages m
|
|
709
|
-
WHERE m.id IN (${includeIds.map(() => "?").join(",")})
|
|
710
|
-
OR EXISTS (
|
|
711
|
-
SELECT 1 FROM ordered_messages target
|
|
712
|
-
WHERE target.id IN (${includeIds.map(() => "?").join(",")})
|
|
713
|
-
AND (
|
|
714
|
-
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
715
|
-
OR
|
|
716
|
-
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
717
|
-
)
|
|
718
|
-
)
|
|
719
|
-
ORDER BY m.createdAt DESC
|
|
720
|
-
`;
|
|
721
|
-
const params2 = [
|
|
722
|
-
threadId,
|
|
723
|
-
...includeIds,
|
|
724
|
-
// for m.id IN (...)
|
|
725
|
-
...includeIds,
|
|
726
|
-
// for target.id IN (...)
|
|
727
|
-
prevMax,
|
|
728
|
-
nextMax
|
|
729
|
-
];
|
|
730
|
-
const includeResult = await this.executeQuery({ sql: sql2, params: params2 });
|
|
731
|
-
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
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}%`);
|
|
732
1918
|
}
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
if (
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
const timeA = new Date(aRecord.createdAt).getTime();
|
|
742
|
-
const timeB = new Date(bRecord.createdAt).getTime();
|
|
743
|
-
return timeA - timeB;
|
|
744
|
-
});
|
|
745
|
-
const processedMessages = messages.map((message) => {
|
|
746
|
-
const processedMsg = {};
|
|
747
|
-
for (const [key, value] of Object.entries(message)) {
|
|
748
|
-
processedMsg[key] = this.deserializeValue(value);
|
|
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);
|
|
749
1927
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
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
|
+
};
|
|
754
1964
|
} catch (error) {
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
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 };
|
|
760
1978
|
}
|
|
761
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
|
+
}
|
|
762
2010
|
async persistWorkflowSnapshot({
|
|
763
2011
|
workflowName,
|
|
764
2012
|
runId,
|
|
2013
|
+
resourceId,
|
|
765
2014
|
snapshot
|
|
766
2015
|
}) {
|
|
767
|
-
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
2016
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
768
2017
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
769
|
-
const currentSnapshot = await this.load({
|
|
2018
|
+
const currentSnapshot = await this.operations.load({
|
|
770
2019
|
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
771
2020
|
keys: { workflow_name: workflowName, run_id: runId }
|
|
772
2021
|
});
|
|
773
2022
|
const persisting = currentSnapshot ? {
|
|
774
2023
|
...currentSnapshot,
|
|
2024
|
+
resourceId,
|
|
775
2025
|
snapshot: JSON.stringify(snapshot),
|
|
776
2026
|
updatedAt: now
|
|
777
2027
|
} : {
|
|
778
2028
|
workflow_name: workflowName,
|
|
779
2029
|
run_id: runId,
|
|
2030
|
+
resourceId,
|
|
780
2031
|
snapshot,
|
|
781
2032
|
createdAt: now,
|
|
782
2033
|
updatedAt: now
|
|
783
2034
|
};
|
|
784
|
-
const processedRecord = await this.processRecord(persisting);
|
|
2035
|
+
const processedRecord = await this.operations.processRecord(persisting);
|
|
785
2036
|
const columns = Object.keys(processedRecord);
|
|
786
2037
|
const values = Object.values(processedRecord);
|
|
787
2038
|
const updateMap = {
|
|
@@ -792,139 +2043,465 @@ var D1Store = class extends MastraStorage {
|
|
|
792
2043
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
793
2044
|
const { sql, params } = query.build();
|
|
794
2045
|
try {
|
|
795
|
-
await this.executeQuery({ sql, params });
|
|
2046
|
+
await this.operations.executeQuery({ sql, params });
|
|
796
2047
|
} catch (error) {
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
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
|
+
);
|
|
801
2058
|
}
|
|
802
2059
|
}
|
|
803
2060
|
async loadWorkflowSnapshot(params) {
|
|
804
2061
|
const { workflowName, runId } = params;
|
|
805
2062
|
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
2063
|
+
try {
|
|
2064
|
+
const d = await this.operations.load({
|
|
2065
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
2066
|
+
keys: {
|
|
2067
|
+
workflow_name: workflowName,
|
|
2068
|
+
run_id: runId
|
|
2069
|
+
}
|
|
2070
|
+
});
|
|
2071
|
+
return d ? d.snapshot : null;
|
|
2072
|
+
} catch (error) {
|
|
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
|
+
);
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
parseWorkflowRun(row) {
|
|
2086
|
+
let parsedSnapshot = row.snapshot;
|
|
2087
|
+
if (typeof parsedSnapshot === "string") {
|
|
2088
|
+
try {
|
|
2089
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
2090
|
+
} catch (e) {
|
|
2091
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
811
2092
|
}
|
|
812
|
-
}
|
|
813
|
-
return
|
|
2093
|
+
}
|
|
2094
|
+
return {
|
|
2095
|
+
workflowName: row.workflow_name,
|
|
2096
|
+
runId: row.run_id,
|
|
2097
|
+
snapshot: parsedSnapshot,
|
|
2098
|
+
createdAt: ensureDate(row.createdAt),
|
|
2099
|
+
updatedAt: ensureDate(row.updatedAt),
|
|
2100
|
+
resourceId: row.resourceId
|
|
2101
|
+
};
|
|
814
2102
|
}
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
2103
|
+
async getWorkflowRuns({
|
|
2104
|
+
workflowName,
|
|
2105
|
+
fromDate,
|
|
2106
|
+
toDate,
|
|
2107
|
+
limit,
|
|
2108
|
+
offset,
|
|
2109
|
+
resourceId
|
|
2110
|
+
} = {}) {
|
|
2111
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
823
2112
|
try {
|
|
824
|
-
const
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
const value = typeof col === "string" ? record[col] : null;
|
|
835
|
-
return this.serializeValue(value);
|
|
836
|
-
});
|
|
837
|
-
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
838
|
-
const { sql, params } = query.build();
|
|
839
|
-
await this.executeQuery({ sql, params });
|
|
840
|
-
}
|
|
2113
|
+
const builder = createSqlBuilder().select().from(fullTableName);
|
|
2114
|
+
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
2115
|
+
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
2116
|
+
if (resourceId) {
|
|
2117
|
+
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
2118
|
+
if (hasResourceId) {
|
|
2119
|
+
builder.whereAnd("resourceId = ?", resourceId);
|
|
2120
|
+
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
2121
|
+
} else {
|
|
2122
|
+
console.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
|
|
841
2123
|
}
|
|
842
|
-
this.logger.debug(
|
|
843
|
-
`Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
|
|
844
|
-
);
|
|
845
2124
|
}
|
|
846
|
-
|
|
2125
|
+
if (fromDate) {
|
|
2126
|
+
builder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
2127
|
+
countBuilder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
2128
|
+
}
|
|
2129
|
+
if (toDate) {
|
|
2130
|
+
builder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2131
|
+
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
2132
|
+
}
|
|
2133
|
+
builder.orderBy("createdAt", "DESC");
|
|
2134
|
+
if (typeof limit === "number") builder.limit(limit);
|
|
2135
|
+
if (typeof offset === "number") builder.offset(offset);
|
|
2136
|
+
const { sql, params } = builder.build();
|
|
2137
|
+
let total = 0;
|
|
2138
|
+
if (limit !== void 0 && offset !== void 0) {
|
|
2139
|
+
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
2140
|
+
const countResult = await this.operations.executeQuery({
|
|
2141
|
+
sql: countSql,
|
|
2142
|
+
params: countParams,
|
|
2143
|
+
first: true
|
|
2144
|
+
});
|
|
2145
|
+
total = Number(countResult?.count ?? 0);
|
|
2146
|
+
}
|
|
2147
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
2148
|
+
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
2149
|
+
return { runs, total: total || runs.length };
|
|
847
2150
|
} catch (error) {
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
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
|
+
);
|
|
852
2164
|
}
|
|
853
2165
|
}
|
|
854
|
-
async
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
page,
|
|
858
|
-
perPage,
|
|
859
|
-
attributes
|
|
2166
|
+
async getWorkflowRunById({
|
|
2167
|
+
runId,
|
|
2168
|
+
workflowName
|
|
860
2169
|
}) {
|
|
861
|
-
const fullTableName = this.getTableName(
|
|
2170
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
862
2171
|
try {
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
2172
|
+
const conditions = [];
|
|
2173
|
+
const params = [];
|
|
2174
|
+
if (runId) {
|
|
2175
|
+
conditions.push("run_id = ?");
|
|
2176
|
+
params.push(runId);
|
|
866
2177
|
}
|
|
867
|
-
if (
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
if (attributes && Object.keys(attributes).length > 0) {
|
|
871
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
872
|
-
query.jsonLike("attributes", key, value);
|
|
873
|
-
}
|
|
2178
|
+
if (workflowName) {
|
|
2179
|
+
conditions.push("workflow_name = ?");
|
|
2180
|
+
params.push(workflowName);
|
|
874
2181
|
}
|
|
875
|
-
|
|
876
|
-
const
|
|
877
|
-
const
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
2182
|
+
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
2183
|
+
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
2184
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
2185
|
+
if (!result) return null;
|
|
2186
|
+
return this.parseWorkflowRun(result);
|
|
2187
|
+
} catch (error) {
|
|
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
|
+
);
|
|
889
2198
|
}
|
|
890
2199
|
}
|
|
891
|
-
|
|
892
|
-
|
|
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) {
|
|
893
2214
|
try {
|
|
894
|
-
|
|
895
|
-
if (
|
|
896
|
-
|
|
897
|
-
} else if (type === "live") {
|
|
898
|
-
query = query.andWhere("(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)");
|
|
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.");
|
|
899
2218
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
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
|
+
}
|
|
917
2247
|
};
|
|
918
|
-
|
|
2248
|
+
this.logger.info("Using D1 REST API");
|
|
2249
|
+
}
|
|
919
2250
|
} catch (error) {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
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
|
+
);
|
|
924
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
|
+
};
|
|
925
2289
|
}
|
|
926
|
-
|
|
927
|
-
|
|
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 });
|
|
928
2505
|
}
|
|
929
2506
|
/**
|
|
930
2507
|
* Close the database connection
|
|
@@ -936,3 +2513,5 @@ var D1Store = class extends MastraStorage {
|
|
|
936
2513
|
};
|
|
937
2514
|
|
|
938
2515
|
export { D1Store };
|
|
2516
|
+
//# sourceMappingURL=index.js.map
|
|
2517
|
+
//# sourceMappingURL=index.js.map
|