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