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