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