@mastra/cloudflare-d1 0.0.0-share-agent-metadata-with-cloud-20250718123411 → 0.0.0-span-scorring-test-20251124132129
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 +1632 -0
- package/README.md +10 -5
- package/dist/index.cjs +1410 -904
- 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 +1411 -905
- package/dist/index.js.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +59 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -0
- package/dist/storage/domains/operations/index.d.ts +72 -0
- package/dist/storage/domains/operations/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +60 -0
- package/dist/storage/domains/scores/index.d.ts.map +1 -0
- package/dist/storage/domains/utils.d.ts +3 -0
- package/dist/storage/domains/utils.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +45 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +230 -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 +31 -15
- package/dist/_tsup-dts-rollup.d.cts +0 -422
- package/dist/_tsup-dts-rollup.d.ts +0 -422
- package/dist/index.d.cts +0 -7
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { MessageList } from '@mastra/core/agent';
|
|
2
1
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
3
|
-
import { MastraStorage,
|
|
2
|
+
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, serializeDate, safelyParseJSON } from '@mastra/core/storage';
|
|
4
3
|
import Cloudflare from 'cloudflare';
|
|
4
|
+
import { MessageList } from '@mastra/core/agent';
|
|
5
5
|
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
6
|
+
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
6
7
|
|
|
7
8
|
// src/storage/index.ts
|
|
8
9
|
var SqlBuilder = class {
|
|
@@ -235,487 +236,212 @@ function parseSelectIdentifier(column) {
|
|
|
235
236
|
return column;
|
|
236
237
|
}
|
|
237
238
|
|
|
238
|
-
// src/storage/
|
|
239
|
+
// src/storage/domains/utils.ts
|
|
239
240
|
function isArrayOfRecords(value) {
|
|
240
241
|
return value && Array.isArray(value) && value.length > 0;
|
|
241
242
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
// D1Database binding
|
|
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) {
|
|
243
|
+
function deserializeValue(value, type) {
|
|
244
|
+
if (value === null || value === void 0) return null;
|
|
245
|
+
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
252
246
|
try {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
sql,
|
|
282
|
-
params
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
this.logger.info("Using D1 REST API");
|
|
287
|
-
}
|
|
247
|
+
return JSON.parse(value);
|
|
248
|
+
} catch {
|
|
249
|
+
return value;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/storage/domains/memory/index.ts
|
|
256
|
+
var MemoryStorageD1 = class extends MemoryStorage {
|
|
257
|
+
operations;
|
|
258
|
+
constructor({ operations }) {
|
|
259
|
+
super();
|
|
260
|
+
this.operations = operations;
|
|
261
|
+
}
|
|
262
|
+
async getResourceById({ resourceId }) {
|
|
263
|
+
const resource = await this.operations.load({
|
|
264
|
+
tableName: TABLE_RESOURCES,
|
|
265
|
+
keys: { id: resourceId }
|
|
266
|
+
});
|
|
267
|
+
if (!resource) return null;
|
|
268
|
+
try {
|
|
269
|
+
return {
|
|
270
|
+
...resource,
|
|
271
|
+
createdAt: ensureDate(resource.createdAt),
|
|
272
|
+
updatedAt: ensureDate(resource.updatedAt),
|
|
273
|
+
metadata: typeof resource.metadata === "string" ? JSON.parse(resource.metadata || "{}") : resource.metadata
|
|
274
|
+
};
|
|
288
275
|
} catch (error) {
|
|
289
|
-
|
|
276
|
+
const mastraError = new MastraError(
|
|
290
277
|
{
|
|
291
|
-
id: "
|
|
278
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_RESOURCE_BY_ID_ERROR",
|
|
292
279
|
domain: ErrorDomain.STORAGE,
|
|
293
|
-
category: ErrorCategory.
|
|
294
|
-
text:
|
|
280
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
281
|
+
text: `Error processing resource ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
282
|
+
details: { resourceId }
|
|
295
283
|
},
|
|
296
284
|
error
|
|
297
285
|
);
|
|
286
|
+
this.logger?.error(mastraError.toString());
|
|
287
|
+
this.logger?.trackException(mastraError);
|
|
288
|
+
return null;
|
|
298
289
|
}
|
|
299
290
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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
|
-
}
|
|
382
|
-
}
|
|
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;
|
|
390
|
-
try {
|
|
391
|
-
this.logger.debug("Executing SQL query", { sql, params, first });
|
|
392
|
-
if (this.binding) {
|
|
393
|
-
return this.executeWorkersBindingQuery({ sql, params, first });
|
|
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}`);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
// Helper to get existing table columns
|
|
410
|
-
async getTableColumns(tableName) {
|
|
411
|
-
try {
|
|
412
|
-
const sql = `PRAGMA table_info(${tableName})`;
|
|
413
|
-
const result = await this.executeQuery({ sql, params: [] });
|
|
414
|
-
if (!result || !Array.isArray(result)) {
|
|
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 [];
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
// Helper to serialize objects to JSON strings
|
|
429
|
-
serializeValue(value) {
|
|
430
|
-
if (value === null || value === void 0) return null;
|
|
431
|
-
if (value instanceof Date) {
|
|
432
|
-
return this.serializeDate(value);
|
|
433
|
-
}
|
|
434
|
-
if (typeof value === "object") {
|
|
435
|
-
return JSON.stringify(value);
|
|
436
|
-
}
|
|
437
|
-
return value;
|
|
438
|
-
}
|
|
439
|
-
// Helper to deserialize JSON strings to objects
|
|
440
|
-
deserializeValue(value, type) {
|
|
441
|
-
if (value === null || value === void 0) return null;
|
|
442
|
-
if (type === "date" && typeof value === "string") {
|
|
443
|
-
return new Date(value);
|
|
444
|
-
}
|
|
445
|
-
if (type === "jsonb" && typeof value === "string") {
|
|
446
|
-
try {
|
|
447
|
-
return JSON.parse(value);
|
|
448
|
-
} catch {
|
|
449
|
-
return value;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
|
|
453
|
-
try {
|
|
454
|
-
return JSON.parse(value);
|
|
455
|
-
} catch {
|
|
456
|
-
return value;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
return value;
|
|
460
|
-
}
|
|
461
|
-
getSqlType(type) {
|
|
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);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
async createTable({
|
|
474
|
-
tableName,
|
|
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)");
|
|
487
|
-
}
|
|
291
|
+
async saveResource({ resource }) {
|
|
292
|
+
const fullTableName = this.operations.getTableName(TABLE_RESOURCES);
|
|
293
|
+
const resourceToSave = {
|
|
294
|
+
id: resource.id,
|
|
295
|
+
workingMemory: resource.workingMemory,
|
|
296
|
+
metadata: resource.metadata ? JSON.stringify(resource.metadata) : null,
|
|
297
|
+
createdAt: resource.createdAt,
|
|
298
|
+
updatedAt: resource.updatedAt
|
|
299
|
+
};
|
|
300
|
+
const processedRecord = await this.operations.processRecord(resourceToSave);
|
|
301
|
+
const columns = Object.keys(processedRecord);
|
|
302
|
+
const values = Object.values(processedRecord);
|
|
303
|
+
const updateMap = {
|
|
304
|
+
workingMemory: "excluded.workingMemory",
|
|
305
|
+
metadata: "excluded.metadata",
|
|
306
|
+
createdAt: "excluded.createdAt",
|
|
307
|
+
updatedAt: "excluded.updatedAt"
|
|
308
|
+
};
|
|
309
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
310
|
+
const { sql, params } = query.build();
|
|
488
311
|
try {
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
await this.executeQuery({ sql, params });
|
|
492
|
-
this.logger.debug(`Created table ${fullTableName}`);
|
|
312
|
+
await this.operations.executeQuery({ sql, params });
|
|
313
|
+
return resource;
|
|
493
314
|
} catch (error) {
|
|
494
|
-
this.logger.error(`Error creating table ${fullTableName}:`, {
|
|
495
|
-
message: error instanceof Error ? error.message : String(error)
|
|
496
|
-
});
|
|
497
315
|
throw new MastraError(
|
|
498
316
|
{
|
|
499
|
-
id: "
|
|
317
|
+
id: "CLOUDFLARE_D1_STORAGE_SAVE_RESOURCE_ERROR",
|
|
500
318
|
domain: ErrorDomain.STORAGE,
|
|
501
319
|
category: ErrorCategory.THIRD_PARTY,
|
|
502
|
-
text: `Failed to
|
|
503
|
-
details: {
|
|
320
|
+
text: `Failed to save resource to ${fullTableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
321
|
+
details: { resourceId: resource.id }
|
|
504
322
|
},
|
|
505
323
|
error
|
|
506
324
|
);
|
|
507
325
|
}
|
|
508
326
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
* @param ifNotExists Array of column names to add if they don't exist
|
|
514
|
-
*/
|
|
515
|
-
async alterTable({
|
|
516
|
-
tableName,
|
|
517
|
-
schema,
|
|
518
|
-
ifNotExists
|
|
327
|
+
async updateResource({
|
|
328
|
+
resourceId,
|
|
329
|
+
workingMemory,
|
|
330
|
+
metadata
|
|
519
331
|
}) {
|
|
520
|
-
const
|
|
332
|
+
const existingResource = await this.getResourceById({ resourceId });
|
|
333
|
+
if (!existingResource) {
|
|
334
|
+
const newResource = {
|
|
335
|
+
id: resourceId,
|
|
336
|
+
workingMemory,
|
|
337
|
+
metadata: metadata || {},
|
|
338
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
339
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
340
|
+
};
|
|
341
|
+
return this.saveResource({ resource: newResource });
|
|
342
|
+
}
|
|
343
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
344
|
+
const updatedResource = {
|
|
345
|
+
...existingResource,
|
|
346
|
+
workingMemory: workingMemory !== void 0 ? workingMemory : existingResource.workingMemory,
|
|
347
|
+
metadata: {
|
|
348
|
+
...existingResource.metadata,
|
|
349
|
+
...metadata
|
|
350
|
+
},
|
|
351
|
+
updatedAt
|
|
352
|
+
};
|
|
353
|
+
const fullTableName = this.operations.getTableName(TABLE_RESOURCES);
|
|
354
|
+
const columns = ["workingMemory", "metadata", "updatedAt"];
|
|
355
|
+
const values = [updatedResource.workingMemory, JSON.stringify(updatedResource.metadata), updatedAt.toISOString()];
|
|
356
|
+
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", resourceId);
|
|
357
|
+
const { sql, params } = query.build();
|
|
521
358
|
try {
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
for (const columnName of ifNotExists) {
|
|
525
|
-
if (!existingColumnNames.has(columnName.toLowerCase()) && schema[columnName]) {
|
|
526
|
-
const columnDef = schema[columnName];
|
|
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
|
-
}
|
|
534
|
-
}
|
|
359
|
+
await this.operations.executeQuery({ sql, params });
|
|
360
|
+
return updatedResource;
|
|
535
361
|
} catch (error) {
|
|
536
362
|
throw new MastraError(
|
|
537
363
|
{
|
|
538
|
-
id: "
|
|
364
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_RESOURCE_ERROR",
|
|
539
365
|
domain: ErrorDomain.STORAGE,
|
|
540
366
|
category: ErrorCategory.THIRD_PARTY,
|
|
541
|
-
text: `Failed to
|
|
542
|
-
details: {
|
|
367
|
+
text: `Failed to update resource ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
368
|
+
details: { resourceId }
|
|
543
369
|
},
|
|
544
370
|
error
|
|
545
371
|
);
|
|
546
372
|
}
|
|
547
373
|
}
|
|
548
|
-
async
|
|
549
|
-
const
|
|
374
|
+
async getThreadById({ threadId }) {
|
|
375
|
+
const thread = await this.operations.load({
|
|
376
|
+
tableName: TABLE_THREADS,
|
|
377
|
+
keys: { id: threadId }
|
|
378
|
+
});
|
|
379
|
+
if (!thread) return null;
|
|
550
380
|
try {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
381
|
+
return {
|
|
382
|
+
...thread,
|
|
383
|
+
createdAt: ensureDate(thread.createdAt),
|
|
384
|
+
updatedAt: ensureDate(thread.updatedAt),
|
|
385
|
+
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
386
|
+
};
|
|
555
387
|
} catch (error) {
|
|
556
|
-
|
|
388
|
+
const mastraError = new MastraError(
|
|
557
389
|
{
|
|
558
|
-
id: "
|
|
390
|
+
id: "CLOUDFLARE_D1_STORAGE_GET_THREAD_BY_ID_ERROR",
|
|
559
391
|
domain: ErrorDomain.STORAGE,
|
|
560
392
|
category: ErrorCategory.THIRD_PARTY,
|
|
561
|
-
text: `
|
|
562
|
-
details: {
|
|
393
|
+
text: `Error processing thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
394
|
+
details: { threadId }
|
|
563
395
|
},
|
|
564
396
|
error
|
|
565
397
|
);
|
|
398
|
+
this.logger?.error(mastraError.toString());
|
|
399
|
+
this.logger?.trackException(mastraError);
|
|
400
|
+
return null;
|
|
566
401
|
}
|
|
567
402
|
}
|
|
568
|
-
async
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
}
|
|
573
|
-
return processedRecord;
|
|
574
|
-
}
|
|
575
|
-
async insert({ tableName, record }) {
|
|
576
|
-
const fullTableName = this.getTableName(tableName);
|
|
577
|
-
const processedRecord = await this.processRecord(record);
|
|
578
|
-
const columns = Object.keys(processedRecord);
|
|
579
|
-
const values = Object.values(processedRecord);
|
|
580
|
-
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
581
|
-
const { sql, params } = query.build();
|
|
582
|
-
try {
|
|
583
|
-
await this.executeQuery({ sql, params });
|
|
584
|
-
} catch (error) {
|
|
585
|
-
throw new MastraError(
|
|
586
|
-
{
|
|
587
|
-
id: "CLOUDFLARE_D1_STORAGE_INSERT_ERROR",
|
|
588
|
-
domain: ErrorDomain.STORAGE,
|
|
589
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
590
|
-
text: `Failed to insert into ${fullTableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
591
|
-
details: { tableName }
|
|
592
|
-
},
|
|
593
|
-
error
|
|
594
|
-
);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
async load({ tableName, keys }) {
|
|
598
|
-
const fullTableName = this.getTableName(tableName);
|
|
599
|
-
const query = createSqlBuilder().select("*").from(fullTableName);
|
|
600
|
-
let firstKey = true;
|
|
601
|
-
for (const [key, value] of Object.entries(keys)) {
|
|
602
|
-
if (firstKey) {
|
|
603
|
-
query.where(`${key} = ?`, value);
|
|
604
|
-
firstKey = false;
|
|
605
|
-
} else {
|
|
606
|
-
query.andWhere(`${key} = ?`, value);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
query.limit(1);
|
|
610
|
-
const { sql, params } = query.build();
|
|
611
|
-
try {
|
|
612
|
-
const result = await this.executeQuery({ sql, params, first: true });
|
|
613
|
-
if (!result) return null;
|
|
614
|
-
const processedResult = {};
|
|
615
|
-
for (const [key, value] of Object.entries(result)) {
|
|
616
|
-
processedResult[key] = this.deserializeValue(value);
|
|
617
|
-
}
|
|
618
|
-
return processedResult;
|
|
619
|
-
} catch (error) {
|
|
403
|
+
async listThreadsByResourceId(args) {
|
|
404
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
405
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
406
|
+
if (page < 0) {
|
|
620
407
|
throw new MastraError(
|
|
621
408
|
{
|
|
622
|
-
id: "
|
|
623
|
-
domain: ErrorDomain.STORAGE,
|
|
624
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
625
|
-
text: `Failed to load from ${fullTableName}: ${error instanceof Error ? error.message : String(error)}`,
|
|
626
|
-
details: { tableName }
|
|
627
|
-
},
|
|
628
|
-
error
|
|
629
|
-
);
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
async getThreadById({ threadId }) {
|
|
633
|
-
const thread = await this.load({
|
|
634
|
-
tableName: TABLE_THREADS,
|
|
635
|
-
keys: { id: threadId }
|
|
636
|
-
});
|
|
637
|
-
if (!thread) return null;
|
|
638
|
-
try {
|
|
639
|
-
return {
|
|
640
|
-
...thread,
|
|
641
|
-
createdAt: this.ensureDate(thread.createdAt),
|
|
642
|
-
updatedAt: this.ensureDate(thread.updatedAt),
|
|
643
|
-
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
644
|
-
};
|
|
645
|
-
} catch (error) {
|
|
646
|
-
const mastraError = new MastraError(
|
|
647
|
-
{
|
|
648
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_THREAD_BY_ID_ERROR",
|
|
649
|
-
domain: ErrorDomain.STORAGE,
|
|
650
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
651
|
-
text: `Error processing thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
652
|
-
details: { threadId }
|
|
653
|
-
},
|
|
654
|
-
error
|
|
655
|
-
);
|
|
656
|
-
this.logger?.error(mastraError.toString());
|
|
657
|
-
this.logger?.trackException(mastraError);
|
|
658
|
-
return null;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
/**
|
|
662
|
-
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
663
|
-
*/
|
|
664
|
-
async getThreadsByResourceId({ resourceId }) {
|
|
665
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
666
|
-
try {
|
|
667
|
-
const query = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId);
|
|
668
|
-
const { sql, params } = query.build();
|
|
669
|
-
const results = await this.executeQuery({ sql, params });
|
|
670
|
-
return (isArrayOfRecords(results) ? results : []).map((thread) => ({
|
|
671
|
-
...thread,
|
|
672
|
-
createdAt: this.ensureDate(thread.createdAt),
|
|
673
|
-
updatedAt: this.ensureDate(thread.updatedAt),
|
|
674
|
-
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata || "{}") : thread.metadata || {}
|
|
675
|
-
}));
|
|
676
|
-
} catch (error) {
|
|
677
|
-
const mastraError = new MastraError(
|
|
678
|
-
{
|
|
679
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_THREADS_BY_RESOURCE_ID_ERROR",
|
|
409
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
|
|
680
410
|
domain: ErrorDomain.STORAGE,
|
|
681
|
-
category: ErrorCategory.
|
|
682
|
-
|
|
683
|
-
details: { resourceId }
|
|
411
|
+
category: ErrorCategory.USER,
|
|
412
|
+
details: { page }
|
|
684
413
|
},
|
|
685
|
-
|
|
414
|
+
new Error("page must be >= 0")
|
|
686
415
|
);
|
|
687
|
-
this.logger?.error(mastraError.toString());
|
|
688
|
-
this.logger?.trackException(mastraError);
|
|
689
|
-
return [];
|
|
690
416
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
const
|
|
694
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
417
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
418
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
419
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
695
420
|
const mapRowToStorageThreadType = (row) => ({
|
|
696
421
|
...row,
|
|
697
|
-
createdAt:
|
|
698
|
-
updatedAt:
|
|
422
|
+
createdAt: ensureDate(row.createdAt),
|
|
423
|
+
updatedAt: ensureDate(row.updatedAt),
|
|
699
424
|
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata || "{}") : row.metadata || {}
|
|
700
425
|
});
|
|
701
426
|
try {
|
|
702
427
|
const countQuery = createSqlBuilder().count().from(fullTableName).where("resourceId = ?", resourceId);
|
|
703
|
-
const countResult = await this.executeQuery(countQuery.build());
|
|
428
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
704
429
|
const total = Number(countResult?.[0]?.count ?? 0);
|
|
705
|
-
const
|
|
706
|
-
const
|
|
430
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
431
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("resourceId = ?", resourceId).orderBy(field, direction).limit(limitValue).offset(offset);
|
|
432
|
+
const results = await this.operations.executeQuery(selectQuery.build());
|
|
707
433
|
const threads = results.map(mapRowToStorageThreadType);
|
|
708
434
|
return {
|
|
709
435
|
threads,
|
|
710
436
|
total,
|
|
711
437
|
page,
|
|
712
|
-
perPage,
|
|
713
|
-
hasMore:
|
|
438
|
+
perPage: perPageForResponse,
|
|
439
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
714
440
|
};
|
|
715
441
|
} catch (error) {
|
|
716
442
|
const mastraError = new MastraError(
|
|
717
443
|
{
|
|
718
|
-
id: "
|
|
444
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_THREADS_BY_RESOURCE_ID_ERROR",
|
|
719
445
|
domain: ErrorDomain.STORAGE,
|
|
720
446
|
category: ErrorCategory.THIRD_PARTY,
|
|
721
447
|
text: `Error getting threads by resourceId ${resourceId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -729,22 +455,22 @@ var D1Store = class extends MastraStorage {
|
|
|
729
455
|
threads: [],
|
|
730
456
|
total: 0,
|
|
731
457
|
page,
|
|
732
|
-
perPage,
|
|
458
|
+
perPage: perPageForResponse,
|
|
733
459
|
hasMore: false
|
|
734
460
|
};
|
|
735
461
|
}
|
|
736
462
|
}
|
|
737
463
|
async saveThread({ thread }) {
|
|
738
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
464
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
739
465
|
const threadToSave = {
|
|
740
466
|
id: thread.id,
|
|
741
467
|
resourceId: thread.resourceId,
|
|
742
468
|
title: thread.title,
|
|
743
469
|
metadata: thread.metadata ? JSON.stringify(thread.metadata) : null,
|
|
744
|
-
createdAt: thread.createdAt,
|
|
745
|
-
updatedAt: thread.updatedAt
|
|
470
|
+
createdAt: thread.createdAt.toISOString(),
|
|
471
|
+
updatedAt: thread.updatedAt.toISOString()
|
|
746
472
|
};
|
|
747
|
-
const processedRecord = await this.processRecord(threadToSave);
|
|
473
|
+
const processedRecord = await this.operations.processRecord(threadToSave);
|
|
748
474
|
const columns = Object.keys(processedRecord);
|
|
749
475
|
const values = Object.values(processedRecord);
|
|
750
476
|
const updateMap = {
|
|
@@ -757,7 +483,7 @@ var D1Store = class extends MastraStorage {
|
|
|
757
483
|
const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], updateMap);
|
|
758
484
|
const { sql, params } = query.build();
|
|
759
485
|
try {
|
|
760
|
-
await this.executeQuery({ sql, params });
|
|
486
|
+
await this.operations.executeQuery({ sql, params });
|
|
761
487
|
return thread;
|
|
762
488
|
} catch (error) {
|
|
763
489
|
throw new MastraError(
|
|
@@ -782,16 +508,17 @@ var D1Store = class extends MastraStorage {
|
|
|
782
508
|
if (!thread) {
|
|
783
509
|
throw new Error(`Thread ${id} not found`);
|
|
784
510
|
}
|
|
785
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
511
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
786
512
|
const mergedMetadata = {
|
|
787
513
|
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
788
514
|
...metadata
|
|
789
515
|
};
|
|
516
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
790
517
|
const columns = ["title", "metadata", "updatedAt"];
|
|
791
|
-
const values = [title, JSON.stringify(mergedMetadata),
|
|
518
|
+
const values = [title, JSON.stringify(mergedMetadata), updatedAt.toISOString()];
|
|
792
519
|
const query = createSqlBuilder().update(fullTableName, columns, values).where("id = ?", id);
|
|
793
520
|
const { sql, params } = query.build();
|
|
794
|
-
await this.executeQuery({ sql, params });
|
|
521
|
+
await this.operations.executeQuery({ sql, params });
|
|
795
522
|
return {
|
|
796
523
|
...thread,
|
|
797
524
|
title,
|
|
@@ -799,7 +526,7 @@ var D1Store = class extends MastraStorage {
|
|
|
799
526
|
...typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
800
527
|
...metadata
|
|
801
528
|
},
|
|
802
|
-
updatedAt
|
|
529
|
+
updatedAt
|
|
803
530
|
};
|
|
804
531
|
} catch (error) {
|
|
805
532
|
throw new MastraError(
|
|
@@ -815,15 +542,15 @@ var D1Store = class extends MastraStorage {
|
|
|
815
542
|
}
|
|
816
543
|
}
|
|
817
544
|
async deleteThread({ threadId }) {
|
|
818
|
-
const fullTableName = this.getTableName(TABLE_THREADS);
|
|
545
|
+
const fullTableName = this.operations.getTableName(TABLE_THREADS);
|
|
819
546
|
try {
|
|
820
547
|
const deleteThreadQuery = createSqlBuilder().delete(fullTableName).where("id = ?", threadId);
|
|
821
548
|
const { sql: threadSql, params: threadParams } = deleteThreadQuery.build();
|
|
822
|
-
await this.executeQuery({ sql: threadSql, params: threadParams });
|
|
823
|
-
const messagesTableName = this.getTableName(TABLE_MESSAGES);
|
|
549
|
+
await this.operations.executeQuery({ sql: threadSql, params: threadParams });
|
|
550
|
+
const messagesTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
824
551
|
const deleteMessagesQuery = createSqlBuilder().delete(messagesTableName).where("thread_id = ?", threadId);
|
|
825
552
|
const { sql: messagesSql, params: messagesParams } = deleteMessagesQuery.build();
|
|
826
|
-
await this.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
553
|
+
await this.operations.executeQuery({ sql: messagesSql, params: messagesParams });
|
|
827
554
|
} catch (error) {
|
|
828
555
|
throw new MastraError(
|
|
829
556
|
{
|
|
@@ -838,8 +565,8 @@ var D1Store = class extends MastraStorage {
|
|
|
838
565
|
}
|
|
839
566
|
}
|
|
840
567
|
async saveMessages(args) {
|
|
841
|
-
const { messages
|
|
842
|
-
if (messages.length === 0) return [];
|
|
568
|
+
const { messages } = args;
|
|
569
|
+
if (messages.length === 0) return { messages: [] };
|
|
843
570
|
try {
|
|
844
571
|
const now = /* @__PURE__ */ new Date();
|
|
845
572
|
const threadId = messages[0]?.threadId;
|
|
@@ -854,6 +581,9 @@ var D1Store = class extends MastraStorage {
|
|
|
854
581
|
if (!message.role) {
|
|
855
582
|
throw new Error(`Message at index ${i} missing role`);
|
|
856
583
|
}
|
|
584
|
+
if (!message.resourceId) {
|
|
585
|
+
throw new Error(`Message at index ${i} missing resourceId`);
|
|
586
|
+
}
|
|
857
587
|
const thread = await this.getThreadById({ threadId: message.threadId });
|
|
858
588
|
if (!thread) {
|
|
859
589
|
throw new Error(`Thread ${message.threadId} not found`);
|
|
@@ -872,20 +602,19 @@ var D1Store = class extends MastraStorage {
|
|
|
872
602
|
};
|
|
873
603
|
});
|
|
874
604
|
await Promise.all([
|
|
875
|
-
this.batchUpsert({
|
|
605
|
+
this.operations.batchUpsert({
|
|
876
606
|
tableName: TABLE_MESSAGES,
|
|
877
607
|
records: messagesToInsert
|
|
878
608
|
}),
|
|
879
609
|
// Update thread's updatedAt timestamp
|
|
880
|
-
this.executeQuery({
|
|
881
|
-
sql: `UPDATE ${this.getTableName(TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
|
|
610
|
+
this.operations.executeQuery({
|
|
611
|
+
sql: `UPDATE ${this.operations.getTableName(TABLE_THREADS)} SET updatedAt = ? WHERE id = ?`,
|
|
882
612
|
params: [now.toISOString(), threadId]
|
|
883
613
|
})
|
|
884
614
|
]);
|
|
885
615
|
this.logger.debug(`Saved ${messages.length} messages`);
|
|
886
616
|
const list = new MessageList().add(messages, "memory");
|
|
887
|
-
|
|
888
|
-
return list.get.all.v1();
|
|
617
|
+
return { messages: list.get.all.db() };
|
|
889
618
|
} catch (error) {
|
|
890
619
|
throw new MastraError(
|
|
891
620
|
{
|
|
@@ -898,105 +627,92 @@ var D1Store = class extends MastraStorage {
|
|
|
898
627
|
);
|
|
899
628
|
}
|
|
900
629
|
}
|
|
901
|
-
async _getIncludedMessages(threadId,
|
|
902
|
-
|
|
630
|
+
async _getIncludedMessages(threadId, include) {
|
|
631
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
903
632
|
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
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
defaultLimit: 40
|
|
633
|
+
const unionQueries = [];
|
|
634
|
+
const params = [];
|
|
635
|
+
let paramIdx = 1;
|
|
636
|
+
for (const inc of include) {
|
|
637
|
+
const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
|
|
638
|
+
const searchId = inc.threadId || threadId;
|
|
639
|
+
unionQueries.push(`
|
|
640
|
+
SELECT * FROM (
|
|
641
|
+
WITH ordered_messages AS (
|
|
642
|
+
SELECT
|
|
643
|
+
*,
|
|
644
|
+
ROW_NUMBER() OVER (ORDER BY createdAt ASC) AS row_num
|
|
645
|
+
FROM ${this.operations.getTableName(TABLE_MESSAGES)}
|
|
646
|
+
WHERE thread_id = ?
|
|
647
|
+
)
|
|
648
|
+
SELECT
|
|
649
|
+
m.id,
|
|
650
|
+
m.content,
|
|
651
|
+
m.role,
|
|
652
|
+
m.type,
|
|
653
|
+
m.createdAt,
|
|
654
|
+
m.thread_id AS threadId,
|
|
655
|
+
m.resourceId
|
|
656
|
+
FROM ordered_messages m
|
|
657
|
+
WHERE m.id = ?
|
|
658
|
+
OR EXISTS (
|
|
659
|
+
SELECT 1 FROM ordered_messages target
|
|
660
|
+
WHERE target.id = ?
|
|
661
|
+
AND (
|
|
662
|
+
(m.row_num <= target.row_num + ? AND m.row_num > target.row_num)
|
|
663
|
+
OR
|
|
664
|
+
(m.row_num >= target.row_num - ? AND m.row_num < target.row_num)
|
|
665
|
+
)
|
|
666
|
+
)
|
|
667
|
+
) AS query_${paramIdx}
|
|
668
|
+
`);
|
|
669
|
+
params.push(searchId, id, id, withNextMessages, withPreviousMessages);
|
|
670
|
+
paramIdx++;
|
|
671
|
+
}
|
|
672
|
+
const finalQuery = unionQueries.join(" UNION ALL ") + " ORDER BY createdAt ASC";
|
|
673
|
+
const messages = await this.operations.executeQuery({ sql: finalQuery, params });
|
|
674
|
+
if (!Array.isArray(messages)) {
|
|
675
|
+
return [];
|
|
676
|
+
}
|
|
677
|
+
const processedMessages = messages.map((message) => {
|
|
678
|
+
const processedMsg = {};
|
|
679
|
+
for (const [key, value] of Object.entries(message)) {
|
|
680
|
+
if (key === `type` && value === `v2`) continue;
|
|
681
|
+
processedMsg[key] = deserializeValue(value);
|
|
682
|
+
}
|
|
683
|
+
return processedMsg;
|
|
956
684
|
});
|
|
957
|
-
|
|
685
|
+
return processedMessages;
|
|
686
|
+
}
|
|
687
|
+
async listMessagesById({ messageIds }) {
|
|
688
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
689
|
+
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
958
690
|
const messages = [];
|
|
959
691
|
try {
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
963
|
-
}
|
|
964
|
-
const excludeIds = messages.map((m) => m.id);
|
|
965
|
-
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
966
|
-
if (excludeIds.length > 0) {
|
|
967
|
-
query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
|
|
968
|
-
}
|
|
969
|
-
query.orderBy("createdAt", "DESC").limit(limit);
|
|
692
|
+
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId", "resourceId"]).from(fullTableName).where(`id in (${messageIds.map(() => "?").join(",")})`, ...messageIds);
|
|
693
|
+
query.orderBy("createdAt", "DESC");
|
|
970
694
|
const { sql, params } = query.build();
|
|
971
|
-
const result = await this.executeQuery({ sql, params });
|
|
695
|
+
const result = await this.operations.executeQuery({ sql, params });
|
|
972
696
|
if (Array.isArray(result)) messages.push(...result);
|
|
973
|
-
messages.sort((a, b) => {
|
|
974
|
-
const aRecord = a;
|
|
975
|
-
const bRecord = b;
|
|
976
|
-
const timeA = new Date(aRecord.createdAt).getTime();
|
|
977
|
-
const timeB = new Date(bRecord.createdAt).getTime();
|
|
978
|
-
return timeA - timeB;
|
|
979
|
-
});
|
|
980
697
|
const processedMessages = messages.map((message) => {
|
|
981
698
|
const processedMsg = {};
|
|
982
699
|
for (const [key, value] of Object.entries(message)) {
|
|
983
700
|
if (key === `type` && value === `v2`) continue;
|
|
984
|
-
processedMsg[key] =
|
|
701
|
+
processedMsg[key] = deserializeValue(value);
|
|
985
702
|
}
|
|
986
703
|
return processedMsg;
|
|
987
704
|
});
|
|
988
|
-
this.logger.debug(`Retrieved ${messages.length} messages
|
|
705
|
+
this.logger.debug(`Retrieved ${messages.length} messages`);
|
|
989
706
|
const list = new MessageList().add(processedMessages, "memory");
|
|
990
|
-
|
|
991
|
-
return list.get.all.v1();
|
|
707
|
+
return { messages: list.get.all.db() };
|
|
992
708
|
} catch (error) {
|
|
993
709
|
const mastraError = new MastraError(
|
|
994
710
|
{
|
|
995
|
-
id: "
|
|
711
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_BY_ID_ERROR",
|
|
996
712
|
domain: ErrorDomain.STORAGE,
|
|
997
713
|
category: ErrorCategory.THIRD_PARTY,
|
|
998
|
-
text: `Failed to retrieve messages
|
|
999
|
-
details: {
|
|
714
|
+
text: `Failed to retrieve messages by ID: ${error instanceof Error ? error.message : String(error)}`,
|
|
715
|
+
details: { messageIds: JSON.stringify(messageIds) }
|
|
1000
716
|
},
|
|
1001
717
|
error
|
|
1002
718
|
);
|
|
@@ -1005,195 +721,601 @@ var D1Store = class extends MastraStorage {
|
|
|
1005
721
|
throw mastraError;
|
|
1006
722
|
}
|
|
1007
723
|
}
|
|
1008
|
-
async
|
|
1009
|
-
threadId,
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
724
|
+
async listMessages(args) {
|
|
725
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
726
|
+
if (!threadId.trim()) {
|
|
727
|
+
throw new MastraError(
|
|
728
|
+
{
|
|
729
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
730
|
+
domain: ErrorDomain.STORAGE,
|
|
731
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
732
|
+
details: { threadId }
|
|
733
|
+
},
|
|
734
|
+
new Error("threadId must be a non-empty string")
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
if (page < 0) {
|
|
738
|
+
throw new MastraError(
|
|
739
|
+
{
|
|
740
|
+
id: "STORAGE_CLOUDFLARE_D1_LIST_MESSAGES_INVALID_PAGE",
|
|
741
|
+
domain: ErrorDomain.STORAGE,
|
|
742
|
+
category: ErrorCategory.USER,
|
|
743
|
+
details: { page }
|
|
744
|
+
},
|
|
745
|
+
new Error("page must be >= 0")
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
749
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1017
750
|
try {
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
751
|
+
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
752
|
+
let query = `
|
|
753
|
+
SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
|
|
754
|
+
FROM ${fullTableName}
|
|
755
|
+
WHERE thread_id = ?
|
|
756
|
+
`;
|
|
757
|
+
const queryParams = [threadId];
|
|
758
|
+
if (resourceId) {
|
|
759
|
+
query += ` AND resourceId = ?`;
|
|
760
|
+
queryParams.push(resourceId);
|
|
1021
761
|
}
|
|
1022
|
-
const
|
|
1023
|
-
if (
|
|
1024
|
-
|
|
762
|
+
const dateRange = filter?.dateRange;
|
|
763
|
+
if (dateRange?.start) {
|
|
764
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
765
|
+
query += ` AND createdAt >= ?`;
|
|
766
|
+
queryParams.push(startDate);
|
|
1025
767
|
}
|
|
1026
|
-
if (
|
|
1027
|
-
|
|
768
|
+
if (dateRange?.end) {
|
|
769
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
770
|
+
query += ` AND createdAt <= ?`;
|
|
771
|
+
queryParams.push(endDate);
|
|
772
|
+
}
|
|
773
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
774
|
+
query += ` ORDER BY "${field}" ${direction}`;
|
|
775
|
+
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
776
|
+
query += ` LIMIT ? OFFSET ?`;
|
|
777
|
+
queryParams.push(perPage, offset);
|
|
778
|
+
}
|
|
779
|
+
const results = await this.operations.executeQuery({ sql: query, params: queryParams });
|
|
780
|
+
const paginatedMessages = (isArrayOfRecords(results) ? results : []).map((message) => {
|
|
781
|
+
const processedMsg = {};
|
|
782
|
+
for (const [key, value] of Object.entries(message)) {
|
|
783
|
+
if (key === `type` && value === `v2`) continue;
|
|
784
|
+
processedMsg[key] = deserializeValue(value);
|
|
785
|
+
}
|
|
786
|
+
return processedMsg;
|
|
787
|
+
});
|
|
788
|
+
const paginatedCount = paginatedMessages.length;
|
|
789
|
+
let countQuery = `SELECT count() as count FROM ${fullTableName} WHERE thread_id = ?`;
|
|
790
|
+
const countParams = [threadId];
|
|
791
|
+
if (resourceId) {
|
|
792
|
+
countQuery += ` AND resourceId = ?`;
|
|
793
|
+
countParams.push(resourceId);
|
|
794
|
+
}
|
|
795
|
+
if (dateRange?.start) {
|
|
796
|
+
const startDate = dateRange.start instanceof Date ? serializeDate(dateRange.start) : serializeDate(new Date(dateRange.start));
|
|
797
|
+
countQuery += ` AND createdAt >= ?`;
|
|
798
|
+
countParams.push(startDate);
|
|
1028
799
|
}
|
|
1029
|
-
|
|
800
|
+
if (dateRange?.end) {
|
|
801
|
+
const endDate = dateRange.end instanceof Date ? serializeDate(dateRange.end) : serializeDate(new Date(dateRange.end));
|
|
802
|
+
countQuery += ` AND createdAt <= ?`;
|
|
803
|
+
countParams.push(endDate);
|
|
804
|
+
}
|
|
805
|
+
const countResult = await this.operations.executeQuery({ sql: countQuery, params: countParams });
|
|
1030
806
|
const total = Number(countResult[0]?.count ?? 0);
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
807
|
+
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
|
|
808
|
+
return {
|
|
809
|
+
messages: [],
|
|
810
|
+
total: 0,
|
|
811
|
+
page,
|
|
812
|
+
perPage: perPageForResponse,
|
|
813
|
+
hasMore: false
|
|
814
|
+
};
|
|
1034
815
|
}
|
|
1035
|
-
|
|
1036
|
-
|
|
816
|
+
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
817
|
+
let includeMessages = [];
|
|
818
|
+
if (include && include.length > 0) {
|
|
819
|
+
const includeResult = await this._getIncludedMessages(threadId, include);
|
|
820
|
+
if (Array.isArray(includeResult)) {
|
|
821
|
+
includeMessages = includeResult;
|
|
822
|
+
for (const includeMsg of includeMessages) {
|
|
823
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
824
|
+
paginatedMessages.push(includeMsg);
|
|
825
|
+
messageIds.add(includeMsg.id);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
1037
829
|
}
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
830
|
+
const list = new MessageList().add(paginatedMessages, "memory");
|
|
831
|
+
let finalMessages = list.get.all.db();
|
|
832
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
833
|
+
const isDateField = field === "createdAt" || field === "updatedAt";
|
|
834
|
+
const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
|
|
835
|
+
const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
|
|
836
|
+
if (aValue === bValue) {
|
|
837
|
+
return a.id.localeCompare(b.id);
|
|
838
|
+
}
|
|
839
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
840
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
841
|
+
}
|
|
842
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
843
|
+
});
|
|
844
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
845
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
846
|
+
const hasMore = perPageInput === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
|
|
1042
847
|
return {
|
|
1043
|
-
messages,
|
|
848
|
+
messages: finalMessages,
|
|
1044
849
|
total,
|
|
1045
850
|
page,
|
|
1046
|
-
perPage,
|
|
1047
|
-
hasMore
|
|
851
|
+
perPage: perPageForResponse,
|
|
852
|
+
hasMore
|
|
1048
853
|
};
|
|
1049
854
|
} catch (error) {
|
|
1050
855
|
const mastraError = new MastraError(
|
|
1051
856
|
{
|
|
1052
|
-
id: "
|
|
857
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_MESSAGES_ERROR",
|
|
1053
858
|
domain: ErrorDomain.STORAGE,
|
|
1054
859
|
category: ErrorCategory.THIRD_PARTY,
|
|
1055
|
-
text: `Failed to
|
|
1056
|
-
details: {
|
|
860
|
+
text: `Failed to list messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
861
|
+
details: {
|
|
862
|
+
threadId,
|
|
863
|
+
resourceId: resourceId ?? ""
|
|
864
|
+
}
|
|
1057
865
|
},
|
|
1058
866
|
error
|
|
1059
867
|
);
|
|
1060
|
-
this.logger?.error(mastraError.toString());
|
|
1061
|
-
this.logger?.trackException(mastraError);
|
|
868
|
+
this.logger?.error?.(mastraError.toString());
|
|
869
|
+
this.logger?.trackException?.(mastraError);
|
|
1062
870
|
return {
|
|
1063
871
|
messages: [],
|
|
1064
872
|
total: 0,
|
|
1065
873
|
page,
|
|
1066
|
-
perPage,
|
|
874
|
+
perPage: perPageForResponse,
|
|
1067
875
|
hasMore: false
|
|
1068
876
|
};
|
|
1069
877
|
}
|
|
1070
878
|
}
|
|
1071
|
-
async
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1077
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1078
|
-
const currentSnapshot = await this.load({
|
|
1079
|
-
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
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();
|
|
1103
|
-
try {
|
|
1104
|
-
await this.executeQuery({ sql, params });
|
|
1105
|
-
} catch (error) {
|
|
1106
|
-
throw new MastraError(
|
|
1107
|
-
{
|
|
1108
|
-
id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
|
|
1109
|
-
domain: ErrorDomain.STORAGE,
|
|
1110
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1111
|
-
text: `Failed to persist workflow snapshot: ${error instanceof Error ? error.message : String(error)}`,
|
|
1112
|
-
details: { workflowName, runId }
|
|
1113
|
-
},
|
|
1114
|
-
error
|
|
1115
|
-
);
|
|
879
|
+
async updateMessages(args) {
|
|
880
|
+
const { messages } = args;
|
|
881
|
+
this.logger.debug("Updating messages", { count: messages.length });
|
|
882
|
+
if (!messages.length) {
|
|
883
|
+
return [];
|
|
1116
884
|
}
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
const
|
|
1120
|
-
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
885
|
+
const messageIds = messages.map((m) => m.id);
|
|
886
|
+
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
887
|
+
const threadsTableName = this.operations.getTableName(TABLE_THREADS);
|
|
1121
888
|
try {
|
|
1122
|
-
const
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
889
|
+
const placeholders = messageIds.map(() => "?").join(",");
|
|
890
|
+
const selectQuery = `SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId FROM ${fullTableName} WHERE id IN (${placeholders})`;
|
|
891
|
+
const existingMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
892
|
+
if (existingMessages.length === 0) {
|
|
893
|
+
return [];
|
|
894
|
+
}
|
|
895
|
+
const parsedExistingMessages = existingMessages.map((msg) => {
|
|
896
|
+
if (typeof msg.content === "string") {
|
|
897
|
+
try {
|
|
898
|
+
msg.content = JSON.parse(msg.content);
|
|
899
|
+
} catch {
|
|
900
|
+
}
|
|
1127
901
|
}
|
|
902
|
+
return msg;
|
|
1128
903
|
});
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1166
|
-
const { sql, params } = query.build();
|
|
1167
|
-
await this.executeQuery({ sql, params });
|
|
904
|
+
const threadIdsToUpdate = /* @__PURE__ */ new Set();
|
|
905
|
+
const updateQueries = [];
|
|
906
|
+
for (const existingMessage of parsedExistingMessages) {
|
|
907
|
+
const updatePayload = messages.find((m) => m.id === existingMessage.id);
|
|
908
|
+
if (!updatePayload) continue;
|
|
909
|
+
const { id, ...fieldsToUpdate } = updatePayload;
|
|
910
|
+
if (Object.keys(fieldsToUpdate).length === 0) continue;
|
|
911
|
+
threadIdsToUpdate.add(existingMessage.threadId);
|
|
912
|
+
if ("threadId" in updatePayload && updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
|
|
913
|
+
threadIdsToUpdate.add(updatePayload.threadId);
|
|
914
|
+
}
|
|
915
|
+
const setClauses = [];
|
|
916
|
+
const values = [];
|
|
917
|
+
const updatableFields = { ...fieldsToUpdate };
|
|
918
|
+
if (updatableFields.content) {
|
|
919
|
+
const existingContent = existingMessage.content || {};
|
|
920
|
+
const newContent = {
|
|
921
|
+
...existingContent,
|
|
922
|
+
...updatableFields.content,
|
|
923
|
+
// Deep merge metadata if it exists on both
|
|
924
|
+
...existingContent?.metadata && updatableFields.content.metadata ? {
|
|
925
|
+
metadata: {
|
|
926
|
+
...existingContent.metadata,
|
|
927
|
+
...updatableFields.content.metadata
|
|
928
|
+
}
|
|
929
|
+
} : {}
|
|
930
|
+
};
|
|
931
|
+
setClauses.push(`content = ?`);
|
|
932
|
+
values.push(JSON.stringify(newContent));
|
|
933
|
+
delete updatableFields.content;
|
|
934
|
+
}
|
|
935
|
+
for (const key in updatableFields) {
|
|
936
|
+
if (Object.prototype.hasOwnProperty.call(updatableFields, key)) {
|
|
937
|
+
const dbColumn = key === "threadId" ? "thread_id" : key;
|
|
938
|
+
setClauses.push(`${dbColumn} = ?`);
|
|
939
|
+
values.push(updatableFields[key]);
|
|
1168
940
|
}
|
|
1169
941
|
}
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
942
|
+
if (setClauses.length > 0) {
|
|
943
|
+
values.push(id);
|
|
944
|
+
const updateQuery = `UPDATE ${fullTableName} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
945
|
+
updateQueries.push({ sql: updateQuery, params: values });
|
|
946
|
+
}
|
|
1173
947
|
}
|
|
1174
|
-
|
|
948
|
+
for (const query of updateQueries) {
|
|
949
|
+
await this.operations.executeQuery(query);
|
|
950
|
+
}
|
|
951
|
+
if (threadIdsToUpdate.size > 0) {
|
|
952
|
+
const threadPlaceholders = Array.from(threadIdsToUpdate).map(() => "?").join(",");
|
|
953
|
+
const threadUpdateQuery = `UPDATE ${threadsTableName} SET updatedAt = ? WHERE id IN (${threadPlaceholders})`;
|
|
954
|
+
const threadUpdateParams = [(/* @__PURE__ */ new Date()).toISOString(), ...Array.from(threadIdsToUpdate)];
|
|
955
|
+
await this.operations.executeQuery({ sql: threadUpdateQuery, params: threadUpdateParams });
|
|
956
|
+
}
|
|
957
|
+
const updatedMessages = await this.operations.executeQuery({ sql: selectQuery, params: messageIds });
|
|
958
|
+
return updatedMessages.map((message) => {
|
|
959
|
+
if (typeof message.content === "string") {
|
|
960
|
+
try {
|
|
961
|
+
message.content = JSON.parse(message.content);
|
|
962
|
+
} catch {
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
return message;
|
|
966
|
+
});
|
|
1175
967
|
} catch (error) {
|
|
1176
968
|
throw new MastraError(
|
|
1177
969
|
{
|
|
1178
|
-
id: "
|
|
970
|
+
id: "CLOUDFLARE_D1_STORAGE_UPDATE_MESSAGES_FAILED",
|
|
1179
971
|
domain: ErrorDomain.STORAGE,
|
|
1180
972
|
category: ErrorCategory.THIRD_PARTY,
|
|
1181
|
-
|
|
1182
|
-
details: { tableName }
|
|
973
|
+
details: { count: messages.length }
|
|
1183
974
|
},
|
|
1184
975
|
error
|
|
1185
976
|
);
|
|
1186
977
|
}
|
|
1187
978
|
}
|
|
1188
|
-
|
|
1189
|
-
|
|
979
|
+
};
|
|
980
|
+
var StoreOperationsD1 = class extends StoreOperations {
|
|
981
|
+
client;
|
|
982
|
+
binding;
|
|
983
|
+
tablePrefix;
|
|
984
|
+
constructor(config) {
|
|
985
|
+
super();
|
|
986
|
+
this.client = config.client;
|
|
987
|
+
this.binding = config.binding;
|
|
988
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
989
|
+
}
|
|
990
|
+
async hasColumn(table, column) {
|
|
991
|
+
const fullTableName = table.startsWith(this.tablePrefix) ? table : `${this.tablePrefix}${table}`;
|
|
992
|
+
const sql = `PRAGMA table_info(${fullTableName});`;
|
|
993
|
+
const result = await this.executeQuery({ sql, params: [] });
|
|
994
|
+
if (!result || !Array.isArray(result)) return false;
|
|
995
|
+
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
996
|
+
}
|
|
997
|
+
getTableName(tableName) {
|
|
998
|
+
return `${this.tablePrefix}${tableName}`;
|
|
999
|
+
}
|
|
1000
|
+
formatSqlParams(params) {
|
|
1001
|
+
return params.map((p) => p === void 0 || p === null ? null : p);
|
|
1002
|
+
}
|
|
1003
|
+
async executeWorkersBindingQuery({
|
|
1004
|
+
sql,
|
|
1005
|
+
params = [],
|
|
1006
|
+
first = false
|
|
1007
|
+
}) {
|
|
1008
|
+
if (!this.binding) {
|
|
1009
|
+
throw new Error("Workers binding is not configured");
|
|
1010
|
+
}
|
|
1011
|
+
try {
|
|
1012
|
+
const statement = this.binding.prepare(sql);
|
|
1013
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1014
|
+
let result;
|
|
1015
|
+
if (formattedParams.length > 0) {
|
|
1016
|
+
if (first) {
|
|
1017
|
+
result = await statement.bind(...formattedParams).first();
|
|
1018
|
+
if (!result) return null;
|
|
1019
|
+
return result;
|
|
1020
|
+
} else {
|
|
1021
|
+
result = await statement.bind(...formattedParams).all();
|
|
1022
|
+
const results = result.results || [];
|
|
1023
|
+
return results;
|
|
1024
|
+
}
|
|
1025
|
+
} else {
|
|
1026
|
+
if (first) {
|
|
1027
|
+
result = await statement.first();
|
|
1028
|
+
if (!result) return null;
|
|
1029
|
+
return result;
|
|
1030
|
+
} else {
|
|
1031
|
+
result = await statement.all();
|
|
1032
|
+
const results = result.results || [];
|
|
1033
|
+
return results;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
} catch (error) {
|
|
1037
|
+
throw new MastraError(
|
|
1038
|
+
{
|
|
1039
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_WORKERS_BINDING_QUERY_FAILED",
|
|
1040
|
+
domain: ErrorDomain.STORAGE,
|
|
1041
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1042
|
+
details: { sql }
|
|
1043
|
+
},
|
|
1044
|
+
error
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
async executeRestQuery({
|
|
1049
|
+
sql,
|
|
1050
|
+
params = [],
|
|
1051
|
+
first = false
|
|
1052
|
+
}) {
|
|
1053
|
+
if (!this.client) {
|
|
1054
|
+
throw new Error("D1 client is not configured");
|
|
1055
|
+
}
|
|
1056
|
+
try {
|
|
1057
|
+
const formattedParams = this.formatSqlParams(params);
|
|
1058
|
+
const response = await this.client.query({
|
|
1059
|
+
sql,
|
|
1060
|
+
params: formattedParams
|
|
1061
|
+
});
|
|
1062
|
+
const result = response.result || [];
|
|
1063
|
+
const results = result.flatMap((r) => r.results || []);
|
|
1064
|
+
if (first) {
|
|
1065
|
+
return results[0] || null;
|
|
1066
|
+
}
|
|
1067
|
+
return results;
|
|
1068
|
+
} catch (error) {
|
|
1069
|
+
throw new MastraError(
|
|
1070
|
+
{
|
|
1071
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_REST_QUERY_FAILED",
|
|
1072
|
+
domain: ErrorDomain.STORAGE,
|
|
1073
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1074
|
+
details: { sql }
|
|
1075
|
+
},
|
|
1076
|
+
error
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
async executeQuery(options) {
|
|
1081
|
+
if (this.binding) {
|
|
1082
|
+
return this.executeWorkersBindingQuery(options);
|
|
1083
|
+
} else if (this.client) {
|
|
1084
|
+
return this.executeRestQuery(options);
|
|
1085
|
+
} else {
|
|
1086
|
+
throw new Error("Neither binding nor client is configured");
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
async getTableColumns(tableName) {
|
|
1090
|
+
try {
|
|
1091
|
+
const sql = `PRAGMA table_info(${tableName})`;
|
|
1092
|
+
const result = await this.executeQuery({ sql });
|
|
1093
|
+
if (!result || !Array.isArray(result)) {
|
|
1094
|
+
return [];
|
|
1095
|
+
}
|
|
1096
|
+
return result.map((row) => ({
|
|
1097
|
+
name: row.name,
|
|
1098
|
+
type: row.type
|
|
1099
|
+
}));
|
|
1100
|
+
} catch (error) {
|
|
1101
|
+
this.logger.warn(`Failed to get table columns for ${tableName}:`, error);
|
|
1102
|
+
return [];
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
serializeValue(value) {
|
|
1106
|
+
if (value === null || value === void 0) {
|
|
1107
|
+
return null;
|
|
1108
|
+
}
|
|
1109
|
+
if (value instanceof Date) {
|
|
1110
|
+
return value.toISOString();
|
|
1111
|
+
}
|
|
1112
|
+
if (typeof value === "object") {
|
|
1113
|
+
return JSON.stringify(value);
|
|
1114
|
+
}
|
|
1115
|
+
return value;
|
|
1116
|
+
}
|
|
1117
|
+
getSqlType(type) {
|
|
1118
|
+
switch (type) {
|
|
1119
|
+
case "bigint":
|
|
1120
|
+
return "INTEGER";
|
|
1121
|
+
// SQLite uses INTEGER for all integer sizes
|
|
1122
|
+
case "jsonb":
|
|
1123
|
+
return "TEXT";
|
|
1124
|
+
// Store JSON as TEXT in SQLite
|
|
1125
|
+
default:
|
|
1126
|
+
return super.getSqlType(type);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
async createTable({
|
|
1130
|
+
tableName,
|
|
1131
|
+
schema
|
|
1132
|
+
}) {
|
|
1133
|
+
try {
|
|
1134
|
+
const fullTableName = this.getTableName(tableName);
|
|
1135
|
+
const columnDefinitions = Object.entries(schema).map(([colName, colDef]) => {
|
|
1136
|
+
const type = this.getSqlType(colDef.type);
|
|
1137
|
+
const nullable = colDef.nullable === false ? "NOT NULL" : "";
|
|
1138
|
+
const primaryKey = colDef.primaryKey ? "PRIMARY KEY" : "";
|
|
1139
|
+
return `${colName} ${type} ${nullable} ${primaryKey}`.trim();
|
|
1140
|
+
});
|
|
1141
|
+
const tableConstraints = [];
|
|
1142
|
+
if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
|
|
1143
|
+
tableConstraints.push("UNIQUE (workflow_name, run_id)");
|
|
1144
|
+
}
|
|
1145
|
+
const query = createSqlBuilder().createTable(fullTableName, columnDefinitions, tableConstraints);
|
|
1146
|
+
const { sql, params } = query.build();
|
|
1147
|
+
await this.executeQuery({ sql, params });
|
|
1148
|
+
this.logger.debug(`Created table ${fullTableName}`);
|
|
1149
|
+
} catch (error) {
|
|
1150
|
+
throw new MastraError(
|
|
1151
|
+
{
|
|
1152
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CREATE_TABLE_FAILED",
|
|
1153
|
+
domain: ErrorDomain.STORAGE,
|
|
1154
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1155
|
+
details: { tableName }
|
|
1156
|
+
},
|
|
1157
|
+
error
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
async clearTable({ tableName }) {
|
|
1162
|
+
try {
|
|
1163
|
+
const fullTableName = this.getTableName(tableName);
|
|
1164
|
+
const query = createSqlBuilder().delete(fullTableName);
|
|
1165
|
+
const { sql, params } = query.build();
|
|
1166
|
+
await this.executeQuery({ sql, params });
|
|
1167
|
+
this.logger.debug(`Cleared table ${fullTableName}`);
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
throw new MastraError(
|
|
1170
|
+
{
|
|
1171
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_CLEAR_TABLE_FAILED",
|
|
1172
|
+
domain: ErrorDomain.STORAGE,
|
|
1173
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1174
|
+
details: { tableName }
|
|
1175
|
+
},
|
|
1176
|
+
error
|
|
1177
|
+
);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
async dropTable({ tableName }) {
|
|
1181
|
+
try {
|
|
1182
|
+
const fullTableName = this.getTableName(tableName);
|
|
1183
|
+
const sql = `DROP TABLE IF EXISTS ${fullTableName}`;
|
|
1184
|
+
await this.executeQuery({ sql });
|
|
1185
|
+
this.logger.debug(`Dropped table ${fullTableName}`);
|
|
1186
|
+
} catch (error) {
|
|
1187
|
+
throw new MastraError(
|
|
1188
|
+
{
|
|
1189
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_DROP_TABLE_FAILED",
|
|
1190
|
+
domain: ErrorDomain.STORAGE,
|
|
1191
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1192
|
+
details: { tableName }
|
|
1193
|
+
},
|
|
1194
|
+
error
|
|
1195
|
+
);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
async alterTable(args) {
|
|
1199
|
+
try {
|
|
1200
|
+
const fullTableName = this.getTableName(args.tableName);
|
|
1201
|
+
const existingColumns = await this.getTableColumns(fullTableName);
|
|
1202
|
+
const existingColumnNames = new Set(existingColumns.map((col) => col.name));
|
|
1203
|
+
for (const [columnName, column] of Object.entries(args.schema)) {
|
|
1204
|
+
if (!existingColumnNames.has(columnName) && args.ifNotExists.includes(columnName)) {
|
|
1205
|
+
const sqlType = this.getSqlType(column.type);
|
|
1206
|
+
const defaultValue = this.getDefaultValue(column.type);
|
|
1207
|
+
const sql = `ALTER TABLE ${fullTableName} ADD COLUMN ${columnName} ${sqlType} ${defaultValue}`;
|
|
1208
|
+
await this.executeQuery({ sql });
|
|
1209
|
+
this.logger.debug(`Added column ${columnName} to table ${fullTableName}`);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
} catch (error) {
|
|
1213
|
+
throw new MastraError(
|
|
1214
|
+
{
|
|
1215
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_ALTER_TABLE_FAILED",
|
|
1216
|
+
domain: ErrorDomain.STORAGE,
|
|
1217
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1218
|
+
details: { tableName: args.tableName }
|
|
1219
|
+
},
|
|
1220
|
+
error
|
|
1221
|
+
);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
async insert({ tableName, record }) {
|
|
1225
|
+
try {
|
|
1226
|
+
const fullTableName = this.getTableName(tableName);
|
|
1227
|
+
const processedRecord = await this.processRecord(record);
|
|
1228
|
+
const columns = Object.keys(processedRecord);
|
|
1229
|
+
const values = Object.values(processedRecord);
|
|
1230
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1231
|
+
const { sql, params } = query.build();
|
|
1232
|
+
await this.executeQuery({ sql, params });
|
|
1233
|
+
} catch (error) {
|
|
1234
|
+
throw new MastraError(
|
|
1235
|
+
{
|
|
1236
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_INSERT_FAILED",
|
|
1237
|
+
domain: ErrorDomain.STORAGE,
|
|
1238
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1239
|
+
details: { tableName }
|
|
1240
|
+
},
|
|
1241
|
+
error
|
|
1242
|
+
);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
async batchInsert({ tableName, records }) {
|
|
1246
|
+
try {
|
|
1247
|
+
if (records.length === 0) return;
|
|
1248
|
+
const fullTableName = this.getTableName(tableName);
|
|
1249
|
+
const processedRecords = await Promise.all(records.map((record) => this.processRecord(record)));
|
|
1250
|
+
const columns = Object.keys(processedRecords[0] || {});
|
|
1251
|
+
for (const record of processedRecords) {
|
|
1252
|
+
const values = Object.values(record);
|
|
1253
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1254
|
+
const { sql, params } = query.build();
|
|
1255
|
+
await this.executeQuery({ sql, params });
|
|
1256
|
+
}
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
throw new MastraError(
|
|
1259
|
+
{
|
|
1260
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_BATCH_INSERT_FAILED",
|
|
1261
|
+
domain: ErrorDomain.STORAGE,
|
|
1262
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1263
|
+
details: { tableName }
|
|
1264
|
+
},
|
|
1265
|
+
error
|
|
1266
|
+
);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
async load({ tableName, keys }) {
|
|
1270
|
+
try {
|
|
1271
|
+
const fullTableName = this.getTableName(tableName);
|
|
1272
|
+
const query = createSqlBuilder().select("*").from(fullTableName);
|
|
1273
|
+
let firstKey = true;
|
|
1274
|
+
for (const [key, value] of Object.entries(keys)) {
|
|
1275
|
+
if (firstKey) {
|
|
1276
|
+
query.where(`${key} = ?`, value);
|
|
1277
|
+
firstKey = false;
|
|
1278
|
+
} else {
|
|
1279
|
+
query.andWhere(`${key} = ?`, value);
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
query.orderBy("createdAt", "DESC");
|
|
1283
|
+
query.limit(1);
|
|
1284
|
+
const { sql, params } = query.build();
|
|
1285
|
+
const result = await this.executeQuery({ sql, params, first: true });
|
|
1286
|
+
if (!result) {
|
|
1287
|
+
return null;
|
|
1288
|
+
}
|
|
1289
|
+
const deserializedResult = {};
|
|
1290
|
+
for (const [key, value] of Object.entries(result)) {
|
|
1291
|
+
deserializedResult[key] = deserializeValue(value);
|
|
1292
|
+
}
|
|
1293
|
+
return deserializedResult;
|
|
1294
|
+
} catch (error) {
|
|
1295
|
+
throw new MastraError(
|
|
1296
|
+
{
|
|
1297
|
+
id: "CLOUDFLARE_D1_STORE_OPERATIONS_LOAD_FAILED",
|
|
1298
|
+
domain: ErrorDomain.STORAGE,
|
|
1299
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1300
|
+
details: { tableName }
|
|
1301
|
+
},
|
|
1302
|
+
error
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
async processRecord(record) {
|
|
1307
|
+
const processed = {};
|
|
1308
|
+
for (const [key, value] of Object.entries(record)) {
|
|
1309
|
+
processed[key] = this.serializeValue(value);
|
|
1310
|
+
}
|
|
1311
|
+
return processed;
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* Upsert multiple records in a batch operation
|
|
1190
1315
|
* @param tableName The table to insert into
|
|
1191
1316
|
* @param records The records to insert
|
|
1192
1317
|
*/
|
|
1193
|
-
async batchUpsert({
|
|
1194
|
-
tableName,
|
|
1195
|
-
records
|
|
1196
|
-
}) {
|
|
1318
|
+
async batchUpsert({ tableName, records }) {
|
|
1197
1319
|
if (records.length === 0) return;
|
|
1198
1320
|
const fullTableName = this.getTableName(tableName);
|
|
1199
1321
|
try {
|
|
@@ -1240,273 +1362,413 @@ var D1Store = class extends MastraStorage {
|
|
|
1240
1362
|
);
|
|
1241
1363
|
}
|
|
1242
1364
|
}
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1365
|
+
};
|
|
1366
|
+
function transformScoreRow(row) {
|
|
1367
|
+
const deserialized = { ...row };
|
|
1368
|
+
deserialized.input = safelyParseJSON(row.input);
|
|
1369
|
+
deserialized.output = safelyParseJSON(row.output);
|
|
1370
|
+
deserialized.scorer = safelyParseJSON(row.scorer);
|
|
1371
|
+
deserialized.preprocessStepResult = safelyParseJSON(row.preprocessStepResult);
|
|
1372
|
+
deserialized.analyzeStepResult = safelyParseJSON(row.analyzeStepResult);
|
|
1373
|
+
deserialized.metadata = safelyParseJSON(row.metadata);
|
|
1374
|
+
deserialized.additionalContext = safelyParseJSON(row.additionalContext);
|
|
1375
|
+
deserialized.requestContext = safelyParseJSON(row.requestContext);
|
|
1376
|
+
deserialized.entity = safelyParseJSON(row.entity);
|
|
1377
|
+
deserialized.createdAt = row.createdAtZ || row.createdAt;
|
|
1378
|
+
deserialized.updatedAt = row.updatedAtZ || row.updatedAt;
|
|
1379
|
+
return deserialized;
|
|
1380
|
+
}
|
|
1381
|
+
var ScoresStorageD1 = class extends ScoresStorage {
|
|
1382
|
+
operations;
|
|
1383
|
+
constructor({ operations }) {
|
|
1384
|
+
super();
|
|
1385
|
+
this.operations = operations;
|
|
1386
|
+
}
|
|
1387
|
+
async getScoreById({ id }) {
|
|
1256
1388
|
try {
|
|
1257
|
-
const
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
}
|
|
1261
|
-
if (
|
|
1262
|
-
|
|
1389
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1390
|
+
const query = createSqlBuilder().select("*").from(fullTableName).where("id = ?", id);
|
|
1391
|
+
const { sql, params } = query.build();
|
|
1392
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1393
|
+
if (!result) {
|
|
1394
|
+
return null;
|
|
1263
1395
|
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1396
|
+
return transformScoreRow(result);
|
|
1397
|
+
} catch (error) {
|
|
1398
|
+
throw new MastraError(
|
|
1399
|
+
{
|
|
1400
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORE_BY_ID_FAILED",
|
|
1401
|
+
domain: ErrorDomain.STORAGE,
|
|
1402
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1403
|
+
},
|
|
1404
|
+
error
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
async saveScore(score) {
|
|
1409
|
+
let parsedScore;
|
|
1410
|
+
try {
|
|
1411
|
+
parsedScore = saveScorePayloadSchema.parse(score);
|
|
1412
|
+
} catch (error) {
|
|
1413
|
+
throw new MastraError(
|
|
1414
|
+
{
|
|
1415
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
|
|
1416
|
+
domain: ErrorDomain.STORAGE,
|
|
1417
|
+
category: ErrorCategory.USER,
|
|
1418
|
+
details: { scoreId: score.id }
|
|
1419
|
+
},
|
|
1420
|
+
error
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1423
|
+
try {
|
|
1424
|
+
const id = crypto.randomUUID();
|
|
1425
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1426
|
+
const serializedRecord = {};
|
|
1427
|
+
for (const [key, value] of Object.entries(parsedScore)) {
|
|
1428
|
+
if (value !== null && value !== void 0) {
|
|
1429
|
+
if (typeof value === "object") {
|
|
1430
|
+
serializedRecord[key] = JSON.stringify(value);
|
|
1431
|
+
} else {
|
|
1432
|
+
serializedRecord[key] = value;
|
|
1433
|
+
}
|
|
1434
|
+
} else {
|
|
1435
|
+
serializedRecord[key] = null;
|
|
1267
1436
|
}
|
|
1268
1437
|
}
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
query.orderBy("startTime", "DESC").limit(perPage).offset(page * perPage);
|
|
1438
|
+
serializedRecord.id = id;
|
|
1439
|
+
serializedRecord.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1440
|
+
serializedRecord.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1441
|
+
const columns = Object.keys(serializedRecord);
|
|
1442
|
+
const values = Object.values(serializedRecord);
|
|
1443
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values);
|
|
1276
1444
|
const { sql, params } = query.build();
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
...trace,
|
|
1281
|
-
attributes: this.deserializeValue(trace.attributes, "jsonb"),
|
|
1282
|
-
status: this.deserializeValue(trace.status, "jsonb"),
|
|
1283
|
-
events: this.deserializeValue(trace.events, "jsonb"),
|
|
1284
|
-
links: this.deserializeValue(trace.links, "jsonb"),
|
|
1285
|
-
other: this.deserializeValue(trace.other, "jsonb")
|
|
1286
|
-
})
|
|
1287
|
-
) : [];
|
|
1445
|
+
await this.operations.executeQuery({ sql, params });
|
|
1446
|
+
const scoreFromDb = await this.getScoreById({ id });
|
|
1447
|
+
return { score: scoreFromDb };
|
|
1288
1448
|
} catch (error) {
|
|
1289
|
-
|
|
1449
|
+
throw new MastraError(
|
|
1290
1450
|
{
|
|
1291
|
-
id: "
|
|
1451
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_SAVE_SCORE_FAILED",
|
|
1292
1452
|
domain: ErrorDomain.STORAGE,
|
|
1293
|
-
category: ErrorCategory.THIRD_PARTY
|
|
1294
|
-
text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
|
|
1295
|
-
details: {
|
|
1296
|
-
name: name ?? "",
|
|
1297
|
-
scope: scope ?? ""
|
|
1298
|
-
}
|
|
1453
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1299
1454
|
},
|
|
1300
1455
|
error
|
|
1301
1456
|
);
|
|
1302
|
-
this.logger?.error(mastraError.toString());
|
|
1303
|
-
this.logger?.trackException(mastraError);
|
|
1304
|
-
return [];
|
|
1305
1457
|
}
|
|
1306
1458
|
}
|
|
1307
|
-
async
|
|
1308
|
-
|
|
1309
|
-
|
|
1459
|
+
async listScoresByScorerId({
|
|
1460
|
+
scorerId,
|
|
1461
|
+
entityId,
|
|
1462
|
+
entityType,
|
|
1463
|
+
source,
|
|
1464
|
+
pagination
|
|
1465
|
+
}) {
|
|
1310
1466
|
try {
|
|
1311
|
-
const
|
|
1312
|
-
const
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1467
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1468
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1469
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1470
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1471
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("scorerId = ?", scorerId);
|
|
1472
|
+
if (entityId) {
|
|
1473
|
+
countQuery.andWhere("entityId = ?", entityId);
|
|
1316
1474
|
}
|
|
1317
|
-
if (
|
|
1318
|
-
|
|
1319
|
-
countQuery.andWhere("scope = ?", scope);
|
|
1475
|
+
if (entityType) {
|
|
1476
|
+
countQuery.andWhere("entityType = ?", entityType);
|
|
1320
1477
|
}
|
|
1321
|
-
if (
|
|
1322
|
-
|
|
1323
|
-
dataQuery.jsonLike("attributes", key, value);
|
|
1324
|
-
countQuery.jsonLike("attributes", key, value);
|
|
1325
|
-
}
|
|
1478
|
+
if (source) {
|
|
1479
|
+
countQuery.andWhere("source = ?", source);
|
|
1326
1480
|
}
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1481
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1482
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1483
|
+
if (total === 0) {
|
|
1484
|
+
return {
|
|
1485
|
+
pagination: {
|
|
1486
|
+
total: 0,
|
|
1487
|
+
page,
|
|
1488
|
+
perPage: perPageForResponse,
|
|
1489
|
+
hasMore: false
|
|
1490
|
+
},
|
|
1491
|
+
scores: []
|
|
1492
|
+
};
|
|
1331
1493
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1494
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1495
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1496
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("scorerId = ?", scorerId);
|
|
1497
|
+
if (entityId) {
|
|
1498
|
+
selectQuery.andWhere("entityId = ?", entityId);
|
|
1336
1499
|
}
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
links: this.deserializeValue(trace.links, "jsonb"),
|
|
1348
|
-
other: this.deserializeValue(trace.other, "jsonb")
|
|
1349
|
-
})
|
|
1350
|
-
) : [];
|
|
1500
|
+
if (entityType) {
|
|
1501
|
+
selectQuery.andWhere("entityType = ?", entityType);
|
|
1502
|
+
}
|
|
1503
|
+
if (source) {
|
|
1504
|
+
selectQuery.andWhere("source = ?", source);
|
|
1505
|
+
}
|
|
1506
|
+
selectQuery.limit(limitValue).offset(start);
|
|
1507
|
+
const { sql, params } = selectQuery.build();
|
|
1508
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1509
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1351
1510
|
return {
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1511
|
+
pagination: {
|
|
1512
|
+
total,
|
|
1513
|
+
page,
|
|
1514
|
+
perPage: perPageForResponse,
|
|
1515
|
+
hasMore: end < total
|
|
1516
|
+
},
|
|
1517
|
+
scores
|
|
1518
|
+
};
|
|
1519
|
+
} catch (error) {
|
|
1520
|
+
throw new MastraError(
|
|
1521
|
+
{
|
|
1522
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SCORER_ID_FAILED",
|
|
1523
|
+
domain: ErrorDomain.STORAGE,
|
|
1524
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1525
|
+
},
|
|
1526
|
+
error
|
|
1527
|
+
);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
async listScoresByRunId({
|
|
1531
|
+
runId,
|
|
1532
|
+
pagination
|
|
1533
|
+
}) {
|
|
1534
|
+
try {
|
|
1535
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1536
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1537
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1538
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1539
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
|
|
1540
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1541
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1542
|
+
if (total === 0) {
|
|
1543
|
+
return {
|
|
1544
|
+
pagination: {
|
|
1545
|
+
total: 0,
|
|
1546
|
+
page,
|
|
1547
|
+
perPage: perPageForResponse,
|
|
1548
|
+
hasMore: false
|
|
1549
|
+
},
|
|
1550
|
+
scores: []
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1554
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1555
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(limitValue).offset(start);
|
|
1556
|
+
const { sql, params } = selectQuery.build();
|
|
1557
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1558
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1559
|
+
return {
|
|
1560
|
+
pagination: {
|
|
1561
|
+
total,
|
|
1562
|
+
page,
|
|
1563
|
+
perPage: perPageForResponse,
|
|
1564
|
+
hasMore: end < total
|
|
1565
|
+
},
|
|
1566
|
+
scores
|
|
1567
|
+
};
|
|
1568
|
+
} catch (error) {
|
|
1569
|
+
throw new MastraError(
|
|
1570
|
+
{
|
|
1571
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_RUN_ID_FAILED",
|
|
1572
|
+
domain: ErrorDomain.STORAGE,
|
|
1573
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1574
|
+
},
|
|
1575
|
+
error
|
|
1576
|
+
);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
async listScoresByEntityId({
|
|
1580
|
+
entityId,
|
|
1581
|
+
entityType,
|
|
1582
|
+
pagination
|
|
1583
|
+
}) {
|
|
1584
|
+
try {
|
|
1585
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1586
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1587
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1588
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1589
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
|
|
1590
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1591
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1592
|
+
if (total === 0) {
|
|
1593
|
+
return {
|
|
1594
|
+
pagination: {
|
|
1595
|
+
total: 0,
|
|
1596
|
+
page,
|
|
1597
|
+
perPage: perPageForResponse,
|
|
1598
|
+
hasMore: false
|
|
1599
|
+
},
|
|
1600
|
+
scores: []
|
|
1601
|
+
};
|
|
1602
|
+
}
|
|
1603
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1604
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1605
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(limitValue).offset(start);
|
|
1606
|
+
const { sql, params } = selectQuery.build();
|
|
1607
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1608
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1609
|
+
return {
|
|
1610
|
+
pagination: {
|
|
1611
|
+
total,
|
|
1612
|
+
page,
|
|
1613
|
+
perPage: perPageForResponse,
|
|
1614
|
+
hasMore: end < total
|
|
1615
|
+
},
|
|
1616
|
+
scores
|
|
1617
|
+
};
|
|
1618
|
+
} catch (error) {
|
|
1619
|
+
throw new MastraError(
|
|
1620
|
+
{
|
|
1621
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_ENTITY_ID_FAILED",
|
|
1622
|
+
domain: ErrorDomain.STORAGE,
|
|
1623
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1624
|
+
},
|
|
1625
|
+
error
|
|
1626
|
+
);
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
async listScoresBySpan({
|
|
1630
|
+
traceId,
|
|
1631
|
+
spanId,
|
|
1632
|
+
pagination
|
|
1633
|
+
}) {
|
|
1634
|
+
try {
|
|
1635
|
+
const { page, perPage: perPageInput } = pagination;
|
|
1636
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1637
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1638
|
+
const fullTableName = this.operations.getTableName(TABLE_SCORERS);
|
|
1639
|
+
const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
|
|
1640
|
+
const countResult = await this.operations.executeQuery(countQuery.build());
|
|
1641
|
+
const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
|
|
1642
|
+
if (total === 0) {
|
|
1643
|
+
return {
|
|
1644
|
+
pagination: {
|
|
1645
|
+
total: 0,
|
|
1646
|
+
page,
|
|
1647
|
+
perPage: perPageForResponse,
|
|
1648
|
+
hasMore: false
|
|
1649
|
+
},
|
|
1650
|
+
scores: []
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1653
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
1654
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1655
|
+
const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limitValue).offset(start);
|
|
1656
|
+
const { sql, params } = selectQuery.build();
|
|
1657
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1658
|
+
const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
|
|
1659
|
+
return {
|
|
1660
|
+
pagination: {
|
|
1661
|
+
total,
|
|
1662
|
+
page,
|
|
1663
|
+
perPage: perPageForResponse,
|
|
1664
|
+
hasMore: end < total
|
|
1665
|
+
},
|
|
1666
|
+
scores
|
|
1357
1667
|
};
|
|
1358
1668
|
} catch (error) {
|
|
1359
|
-
|
|
1669
|
+
throw new MastraError(
|
|
1360
1670
|
{
|
|
1361
|
-
id: "
|
|
1671
|
+
id: "CLOUDFLARE_D1_STORE_SCORES_GET_SCORES_BY_SPAN_FAILED",
|
|
1362
1672
|
domain: ErrorDomain.STORAGE,
|
|
1363
|
-
category: ErrorCategory.THIRD_PARTY
|
|
1364
|
-
text: `Failed to retrieve traces: ${error instanceof Error ? error.message : String(error)}`,
|
|
1365
|
-
details: { name: name ?? "", scope: scope ?? "" }
|
|
1673
|
+
category: ErrorCategory.THIRD_PARTY
|
|
1366
1674
|
},
|
|
1367
1675
|
error
|
|
1368
1676
|
);
|
|
1369
|
-
this.logger?.error(mastraError.toString());
|
|
1370
|
-
this.logger?.trackException(mastraError);
|
|
1371
|
-
return { traces: [], total: 0, page, perPage, hasMore: false };
|
|
1372
1677
|
}
|
|
1373
1678
|
}
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1679
|
+
};
|
|
1680
|
+
var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
1681
|
+
operations;
|
|
1682
|
+
constructor({ operations }) {
|
|
1683
|
+
super();
|
|
1684
|
+
this.operations = operations;
|
|
1685
|
+
}
|
|
1686
|
+
updateWorkflowResults({
|
|
1687
|
+
// workflowName,
|
|
1688
|
+
// runId,
|
|
1689
|
+
// stepId,
|
|
1690
|
+
// result,
|
|
1691
|
+
// requestContext,
|
|
1692
|
+
}) {
|
|
1693
|
+
throw new Error("Method not implemented.");
|
|
1694
|
+
}
|
|
1695
|
+
updateWorkflowState({
|
|
1696
|
+
// workflowName,
|
|
1697
|
+
// runId,
|
|
1698
|
+
// opts,
|
|
1699
|
+
}) {
|
|
1700
|
+
throw new Error("Method not implemented.");
|
|
1701
|
+
}
|
|
1702
|
+
async persistWorkflowSnapshot({
|
|
1703
|
+
workflowName,
|
|
1704
|
+
runId,
|
|
1705
|
+
resourceId,
|
|
1706
|
+
snapshot
|
|
1707
|
+
}) {
|
|
1708
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1709
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1710
|
+
const currentSnapshot = await this.operations.load({
|
|
1711
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
1712
|
+
keys: { workflow_name: workflowName, run_id: runId }
|
|
1713
|
+
});
|
|
1714
|
+
const persisting = currentSnapshot ? {
|
|
1715
|
+
...currentSnapshot,
|
|
1716
|
+
resourceId,
|
|
1717
|
+
snapshot: JSON.stringify(snapshot),
|
|
1718
|
+
updatedAt: now
|
|
1719
|
+
} : {
|
|
1720
|
+
workflow_name: workflowName,
|
|
1721
|
+
run_id: runId,
|
|
1722
|
+
resourceId,
|
|
1723
|
+
snapshot,
|
|
1724
|
+
createdAt: now,
|
|
1725
|
+
updatedAt: now
|
|
1726
|
+
};
|
|
1727
|
+
const processedRecord = await this.operations.processRecord(persisting);
|
|
1728
|
+
const columns = Object.keys(processedRecord);
|
|
1729
|
+
const values = Object.values(processedRecord);
|
|
1730
|
+
const updateMap = {
|
|
1731
|
+
snapshot: "excluded.snapshot",
|
|
1732
|
+
updatedAt: "excluded.updatedAt"
|
|
1733
|
+
};
|
|
1734
|
+
this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
|
|
1735
|
+
const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap);
|
|
1736
|
+
const { sql, params } = query.build();
|
|
1379
1737
|
try {
|
|
1380
|
-
|
|
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
|
-
}) : [];
|
|
1738
|
+
await this.operations.executeQuery({ sql, params });
|
|
1405
1739
|
} catch (error) {
|
|
1406
|
-
|
|
1740
|
+
throw new MastraError(
|
|
1407
1741
|
{
|
|
1408
|
-
id: "
|
|
1742
|
+
id: "CLOUDFLARE_D1_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_ERROR",
|
|
1409
1743
|
domain: ErrorDomain.STORAGE,
|
|
1410
1744
|
category: ErrorCategory.THIRD_PARTY,
|
|
1411
|
-
text: `Failed to
|
|
1412
|
-
details: {
|
|
1745
|
+
text: `Failed to persist workflow snapshot: ${error instanceof Error ? error.message : String(error)}`,
|
|
1746
|
+
details: { workflowName, runId }
|
|
1413
1747
|
},
|
|
1414
1748
|
error
|
|
1415
1749
|
);
|
|
1416
|
-
this.logger?.error(mastraError.toString());
|
|
1417
|
-
this.logger?.trackException(mastraError);
|
|
1418
|
-
return [];
|
|
1419
1750
|
}
|
|
1420
1751
|
}
|
|
1421
|
-
async
|
|
1422
|
-
const {
|
|
1423
|
-
|
|
1424
|
-
const conditions = [];
|
|
1425
|
-
const queryParams = [];
|
|
1426
|
-
if (agentName) {
|
|
1427
|
-
conditions.push(`agent_name = ?`);
|
|
1428
|
-
queryParams.push(agentName);
|
|
1429
|
-
}
|
|
1430
|
-
if (type === "test") {
|
|
1431
|
-
conditions.push(`(test_info IS NOT NULL AND json_extract(test_info, '$.testPath') IS NOT NULL)`);
|
|
1432
|
-
} else if (type === "live") {
|
|
1433
|
-
conditions.push(`(test_info IS NULL OR json_extract(test_info, '$.testPath') IS NULL)`);
|
|
1434
|
-
}
|
|
1435
|
-
if (fromDate) {
|
|
1436
|
-
conditions.push(`createdAt >= ?`);
|
|
1437
|
-
queryParams.push(this.serializeDate(fromDate));
|
|
1438
|
-
}
|
|
1439
|
-
if (toDate) {
|
|
1440
|
-
conditions.push(`createdAt <= ?`);
|
|
1441
|
-
queryParams.push(this.serializeDate(toDate));
|
|
1442
|
-
}
|
|
1443
|
-
const countQueryBuilder = createSqlBuilder().count().from(fullTableName);
|
|
1444
|
-
if (conditions.length > 0) {
|
|
1445
|
-
countQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
1446
|
-
}
|
|
1447
|
-
const { sql: countSql, params: countParams } = countQueryBuilder.build();
|
|
1752
|
+
async loadWorkflowSnapshot(params) {
|
|
1753
|
+
const { workflowName, runId } = params;
|
|
1754
|
+
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
|
|
1448
1755
|
try {
|
|
1449
|
-
const
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
const total = Number(countResult?.count || 0);
|
|
1455
|
-
const currentOffset = page * perPage;
|
|
1456
|
-
if (total === 0) {
|
|
1457
|
-
return {
|
|
1458
|
-
evals: [],
|
|
1459
|
-
total: 0,
|
|
1460
|
-
page,
|
|
1461
|
-
perPage,
|
|
1462
|
-
hasMore: false
|
|
1463
|
-
};
|
|
1464
|
-
}
|
|
1465
|
-
const dataQueryBuilder = createSqlBuilder().select("*").from(fullTableName);
|
|
1466
|
-
if (conditions.length > 0) {
|
|
1467
|
-
dataQueryBuilder.where(conditions.join(" AND "), ...queryParams);
|
|
1468
|
-
}
|
|
1469
|
-
dataQueryBuilder.orderBy("createdAt", "DESC").limit(perPage).offset(currentOffset);
|
|
1470
|
-
const { sql: dataSql, params: dataParams } = dataQueryBuilder.build();
|
|
1471
|
-
const rows = await this.executeQuery({
|
|
1472
|
-
sql: dataSql,
|
|
1473
|
-
params: dataParams
|
|
1474
|
-
});
|
|
1475
|
-
const evals = (isArrayOfRecords(rows) ? rows : []).map((row) => {
|
|
1476
|
-
const result = this.deserializeValue(row.result);
|
|
1477
|
-
const testInfo = row.test_info ? this.deserializeValue(row.test_info) : void 0;
|
|
1478
|
-
if (!result || typeof result !== "object" || !("score" in result)) {
|
|
1479
|
-
throw new Error(`Invalid MetricResult format: ${JSON.stringify(result)}`);
|
|
1756
|
+
const d = await this.operations.load({
|
|
1757
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
1758
|
+
keys: {
|
|
1759
|
+
workflow_name: workflowName,
|
|
1760
|
+
run_id: runId
|
|
1480
1761
|
}
|
|
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
1762
|
});
|
|
1494
|
-
|
|
1495
|
-
return {
|
|
1496
|
-
evals,
|
|
1497
|
-
total,
|
|
1498
|
-
page,
|
|
1499
|
-
perPage,
|
|
1500
|
-
hasMore
|
|
1501
|
-
};
|
|
1763
|
+
return d ? d.snapshot : null;
|
|
1502
1764
|
} catch (error) {
|
|
1503
1765
|
throw new MastraError(
|
|
1504
1766
|
{
|
|
1505
|
-
id: "
|
|
1767
|
+
id: "CLOUDFLARE_D1_STORAGE_LOAD_WORKFLOW_SNAPSHOT_ERROR",
|
|
1506
1768
|
domain: ErrorDomain.STORAGE,
|
|
1507
1769
|
category: ErrorCategory.THIRD_PARTY,
|
|
1508
|
-
text: `Failed to
|
|
1509
|
-
details: {
|
|
1770
|
+
text: `Failed to load workflow snapshot: ${error instanceof Error ? error.message : String(error)}`,
|
|
1771
|
+
details: { workflowName, runId }
|
|
1510
1772
|
},
|
|
1511
1773
|
error
|
|
1512
1774
|
);
|
|
@@ -1525,32 +1787,31 @@ var D1Store = class extends MastraStorage {
|
|
|
1525
1787
|
workflowName: row.workflow_name,
|
|
1526
1788
|
runId: row.run_id,
|
|
1527
1789
|
snapshot: parsedSnapshot,
|
|
1528
|
-
createdAt:
|
|
1529
|
-
updatedAt:
|
|
1790
|
+
createdAt: ensureDate(row.createdAt),
|
|
1791
|
+
updatedAt: ensureDate(row.updatedAt),
|
|
1530
1792
|
resourceId: row.resourceId
|
|
1531
1793
|
};
|
|
1532
1794
|
}
|
|
1533
|
-
async
|
|
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
|
-
async getWorkflowRuns({
|
|
1795
|
+
async listWorkflowRuns({
|
|
1540
1796
|
workflowName,
|
|
1541
1797
|
fromDate,
|
|
1542
1798
|
toDate,
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
resourceId
|
|
1799
|
+
page,
|
|
1800
|
+
perPage,
|
|
1801
|
+
resourceId,
|
|
1802
|
+
status
|
|
1546
1803
|
} = {}) {
|
|
1547
|
-
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1804
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1548
1805
|
try {
|
|
1549
1806
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
1550
1807
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
1551
1808
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
1809
|
+
if (status) {
|
|
1810
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1811
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1812
|
+
}
|
|
1552
1813
|
if (resourceId) {
|
|
1553
|
-
const hasResourceId = await this.hasColumn(fullTableName, "resourceId");
|
|
1814
|
+
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
1554
1815
|
if (hasResourceId) {
|
|
1555
1816
|
builder.whereAnd("resourceId = ?", resourceId);
|
|
1556
1817
|
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
@@ -1567,26 +1828,29 @@ var D1Store = class extends MastraStorage {
|
|
|
1567
1828
|
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
1568
1829
|
}
|
|
1569
1830
|
builder.orderBy("createdAt", "DESC");
|
|
1570
|
-
if (typeof
|
|
1571
|
-
|
|
1831
|
+
if (typeof perPage === "number" && typeof page === "number") {
|
|
1832
|
+
const offset = page * perPage;
|
|
1833
|
+
builder.limit(perPage);
|
|
1834
|
+
builder.offset(offset);
|
|
1835
|
+
}
|
|
1572
1836
|
const { sql, params } = builder.build();
|
|
1573
1837
|
let total = 0;
|
|
1574
|
-
if (
|
|
1838
|
+
if (perPage !== void 0 && page !== void 0) {
|
|
1575
1839
|
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
1576
|
-
const countResult = await this.executeQuery({
|
|
1840
|
+
const countResult = await this.operations.executeQuery({
|
|
1577
1841
|
sql: countSql,
|
|
1578
1842
|
params: countParams,
|
|
1579
1843
|
first: true
|
|
1580
1844
|
});
|
|
1581
1845
|
total = Number(countResult?.count ?? 0);
|
|
1582
1846
|
}
|
|
1583
|
-
const results = await this.executeQuery({ sql, params });
|
|
1847
|
+
const results = await this.operations.executeQuery({ sql, params });
|
|
1584
1848
|
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
1585
1849
|
return { runs, total: total || runs.length };
|
|
1586
1850
|
} catch (error) {
|
|
1587
1851
|
throw new MastraError(
|
|
1588
1852
|
{
|
|
1589
|
-
id: "
|
|
1853
|
+
id: "CLOUDFLARE_D1_STORAGE_LIST_WORKFLOW_RUNS_ERROR",
|
|
1590
1854
|
domain: ErrorDomain.STORAGE,
|
|
1591
1855
|
category: ErrorCategory.THIRD_PARTY,
|
|
1592
1856
|
text: `Failed to retrieve workflow runs: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -1603,7 +1867,7 @@ var D1Store = class extends MastraStorage {
|
|
|
1603
1867
|
runId,
|
|
1604
1868
|
workflowName
|
|
1605
1869
|
}) {
|
|
1606
|
-
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1870
|
+
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1607
1871
|
try {
|
|
1608
1872
|
const conditions = [];
|
|
1609
1873
|
const params = [];
|
|
@@ -1617,7 +1881,7 @@ var D1Store = class extends MastraStorage {
|
|
|
1617
1881
|
}
|
|
1618
1882
|
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
1619
1883
|
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
1620
|
-
const result = await this.executeQuery({ sql, params, first: true });
|
|
1884
|
+
const result = await this.operations.executeQuery({ sql, params, first: true });
|
|
1621
1885
|
if (!result) return null;
|
|
1622
1886
|
return this.parseWorkflowRun(result);
|
|
1623
1887
|
} catch (error) {
|
|
@@ -1633,6 +1897,250 @@ var D1Store = class extends MastraStorage {
|
|
|
1633
1897
|
);
|
|
1634
1898
|
}
|
|
1635
1899
|
}
|
|
1900
|
+
};
|
|
1901
|
+
|
|
1902
|
+
// src/storage/index.ts
|
|
1903
|
+
var D1Store = class extends MastraStorage {
|
|
1904
|
+
client;
|
|
1905
|
+
binding;
|
|
1906
|
+
// D1Database binding
|
|
1907
|
+
tablePrefix;
|
|
1908
|
+
stores;
|
|
1909
|
+
/**
|
|
1910
|
+
* Creates a new D1Store instance
|
|
1911
|
+
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
1912
|
+
*/
|
|
1913
|
+
constructor(config) {
|
|
1914
|
+
try {
|
|
1915
|
+
super({ id: config.id, name: "D1" });
|
|
1916
|
+
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
1917
|
+
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
1918
|
+
}
|
|
1919
|
+
this.tablePrefix = config.tablePrefix || "";
|
|
1920
|
+
if ("binding" in config) {
|
|
1921
|
+
if (!config.binding) {
|
|
1922
|
+
throw new Error("D1 binding is required when using Workers Binding API");
|
|
1923
|
+
}
|
|
1924
|
+
this.binding = config.binding;
|
|
1925
|
+
this.logger.info("Using D1 Workers Binding API");
|
|
1926
|
+
} else if ("client" in config) {
|
|
1927
|
+
if (!config.client) {
|
|
1928
|
+
throw new Error("D1 client is required when using D1ClientConfig");
|
|
1929
|
+
}
|
|
1930
|
+
this.client = config.client;
|
|
1931
|
+
this.logger.info("Using D1 Client");
|
|
1932
|
+
} else {
|
|
1933
|
+
if (!config.accountId || !config.databaseId || !config.apiToken) {
|
|
1934
|
+
throw new Error("accountId, databaseId, and apiToken are required when using REST API");
|
|
1935
|
+
}
|
|
1936
|
+
const cfClient = new Cloudflare({
|
|
1937
|
+
apiToken: config.apiToken
|
|
1938
|
+
});
|
|
1939
|
+
this.client = {
|
|
1940
|
+
query: ({ sql, params }) => {
|
|
1941
|
+
return cfClient.d1.database.query(config.databaseId, {
|
|
1942
|
+
account_id: config.accountId,
|
|
1943
|
+
sql,
|
|
1944
|
+
params
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
};
|
|
1948
|
+
this.logger.info("Using D1 REST API");
|
|
1949
|
+
}
|
|
1950
|
+
} catch (error) {
|
|
1951
|
+
throw new MastraError(
|
|
1952
|
+
{
|
|
1953
|
+
id: "CLOUDFLARE_D1_STORAGE_INITIALIZATION_ERROR",
|
|
1954
|
+
domain: ErrorDomain.STORAGE,
|
|
1955
|
+
category: ErrorCategory.SYSTEM,
|
|
1956
|
+
text: "Error initializing D1Store"
|
|
1957
|
+
},
|
|
1958
|
+
error
|
|
1959
|
+
);
|
|
1960
|
+
}
|
|
1961
|
+
const operations = new StoreOperationsD1({
|
|
1962
|
+
client: this.client,
|
|
1963
|
+
binding: this.binding,
|
|
1964
|
+
tablePrefix: this.tablePrefix
|
|
1965
|
+
});
|
|
1966
|
+
const scores = new ScoresStorageD1({
|
|
1967
|
+
operations
|
|
1968
|
+
});
|
|
1969
|
+
const workflows = new WorkflowsStorageD1({
|
|
1970
|
+
operations
|
|
1971
|
+
});
|
|
1972
|
+
const memory = new MemoryStorageD1({
|
|
1973
|
+
operations
|
|
1974
|
+
});
|
|
1975
|
+
this.stores = {
|
|
1976
|
+
operations,
|
|
1977
|
+
scores,
|
|
1978
|
+
workflows,
|
|
1979
|
+
memory
|
|
1980
|
+
};
|
|
1981
|
+
}
|
|
1982
|
+
get supports() {
|
|
1983
|
+
return {
|
|
1984
|
+
selectByIncludeResourceScope: true,
|
|
1985
|
+
resourceWorkingMemory: true,
|
|
1986
|
+
hasColumn: true,
|
|
1987
|
+
createTable: true,
|
|
1988
|
+
deleteMessages: false,
|
|
1989
|
+
listScoresBySpan: true
|
|
1990
|
+
};
|
|
1991
|
+
}
|
|
1992
|
+
async createTable({
|
|
1993
|
+
tableName,
|
|
1994
|
+
schema
|
|
1995
|
+
}) {
|
|
1996
|
+
return this.stores.operations.createTable({ tableName, schema });
|
|
1997
|
+
}
|
|
1998
|
+
/**
|
|
1999
|
+
* Alters table schema to add columns if they don't exist
|
|
2000
|
+
* @param tableName Name of the table
|
|
2001
|
+
* @param schema Schema of the table
|
|
2002
|
+
* @param ifNotExists Array of column names to add if they don't exist
|
|
2003
|
+
*/
|
|
2004
|
+
async alterTable({
|
|
2005
|
+
tableName,
|
|
2006
|
+
schema,
|
|
2007
|
+
ifNotExists
|
|
2008
|
+
}) {
|
|
2009
|
+
return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
|
|
2010
|
+
}
|
|
2011
|
+
async clearTable({ tableName }) {
|
|
2012
|
+
return this.stores.operations.clearTable({ tableName });
|
|
2013
|
+
}
|
|
2014
|
+
async dropTable({ tableName }) {
|
|
2015
|
+
return this.stores.operations.dropTable({ tableName });
|
|
2016
|
+
}
|
|
2017
|
+
async hasColumn(table, column) {
|
|
2018
|
+
return this.stores.operations.hasColumn(table, column);
|
|
2019
|
+
}
|
|
2020
|
+
async insert({ tableName, record }) {
|
|
2021
|
+
return this.stores.operations.insert({ tableName, record });
|
|
2022
|
+
}
|
|
2023
|
+
async load({ tableName, keys }) {
|
|
2024
|
+
return this.stores.operations.load({ tableName, keys });
|
|
2025
|
+
}
|
|
2026
|
+
async getThreadById({ threadId }) {
|
|
2027
|
+
return this.stores.memory.getThreadById({ threadId });
|
|
2028
|
+
}
|
|
2029
|
+
async saveThread({ thread }) {
|
|
2030
|
+
return this.stores.memory.saveThread({ thread });
|
|
2031
|
+
}
|
|
2032
|
+
async updateThread({
|
|
2033
|
+
id,
|
|
2034
|
+
title,
|
|
2035
|
+
metadata
|
|
2036
|
+
}) {
|
|
2037
|
+
return this.stores.memory.updateThread({ id, title, metadata });
|
|
2038
|
+
}
|
|
2039
|
+
async deleteThread({ threadId }) {
|
|
2040
|
+
return this.stores.memory.deleteThread({ threadId });
|
|
2041
|
+
}
|
|
2042
|
+
async saveMessages(args) {
|
|
2043
|
+
return this.stores.memory.saveMessages(args);
|
|
2044
|
+
}
|
|
2045
|
+
async updateWorkflowResults({
|
|
2046
|
+
workflowName,
|
|
2047
|
+
runId,
|
|
2048
|
+
stepId,
|
|
2049
|
+
result,
|
|
2050
|
+
requestContext
|
|
2051
|
+
}) {
|
|
2052
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
2053
|
+
}
|
|
2054
|
+
async updateWorkflowState({
|
|
2055
|
+
workflowName,
|
|
2056
|
+
runId,
|
|
2057
|
+
opts
|
|
2058
|
+
}) {
|
|
2059
|
+
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
2060
|
+
}
|
|
2061
|
+
async persistWorkflowSnapshot({
|
|
2062
|
+
workflowName,
|
|
2063
|
+
runId,
|
|
2064
|
+
resourceId,
|
|
2065
|
+
snapshot
|
|
2066
|
+
}) {
|
|
2067
|
+
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
|
|
2068
|
+
}
|
|
2069
|
+
async loadWorkflowSnapshot(params) {
|
|
2070
|
+
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2071
|
+
}
|
|
2072
|
+
async listWorkflowRuns(args = {}) {
|
|
2073
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2074
|
+
}
|
|
2075
|
+
async getWorkflowRunById({
|
|
2076
|
+
runId,
|
|
2077
|
+
workflowName
|
|
2078
|
+
}) {
|
|
2079
|
+
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
2080
|
+
}
|
|
2081
|
+
/**
|
|
2082
|
+
* Insert multiple records in a batch operation
|
|
2083
|
+
* @param tableName The table to insert into
|
|
2084
|
+
* @param records The records to insert
|
|
2085
|
+
*/
|
|
2086
|
+
async batchInsert({ tableName, records }) {
|
|
2087
|
+
return this.stores.operations.batchInsert({ tableName, records });
|
|
2088
|
+
}
|
|
2089
|
+
async updateMessages(_args) {
|
|
2090
|
+
return this.stores.memory.updateMessages(_args);
|
|
2091
|
+
}
|
|
2092
|
+
async getResourceById({ resourceId }) {
|
|
2093
|
+
return this.stores.memory.getResourceById({ resourceId });
|
|
2094
|
+
}
|
|
2095
|
+
async saveResource({ resource }) {
|
|
2096
|
+
return this.stores.memory.saveResource({ resource });
|
|
2097
|
+
}
|
|
2098
|
+
async updateResource({
|
|
2099
|
+
resourceId,
|
|
2100
|
+
workingMemory,
|
|
2101
|
+
metadata
|
|
2102
|
+
}) {
|
|
2103
|
+
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
2104
|
+
}
|
|
2105
|
+
async getScoreById({ id: _id }) {
|
|
2106
|
+
return this.stores.scores.getScoreById({ id: _id });
|
|
2107
|
+
}
|
|
2108
|
+
async saveScore(_score) {
|
|
2109
|
+
return this.stores.scores.saveScore(_score);
|
|
2110
|
+
}
|
|
2111
|
+
async listScoresByRunId({
|
|
2112
|
+
runId: _runId,
|
|
2113
|
+
pagination: _pagination
|
|
2114
|
+
}) {
|
|
2115
|
+
return this.stores.scores.listScoresByRunId({ runId: _runId, pagination: _pagination });
|
|
2116
|
+
}
|
|
2117
|
+
async listScoresByEntityId({
|
|
2118
|
+
entityId: _entityId,
|
|
2119
|
+
entityType: _entityType,
|
|
2120
|
+
pagination: _pagination
|
|
2121
|
+
}) {
|
|
2122
|
+
return this.stores.scores.listScoresByEntityId({
|
|
2123
|
+
entityId: _entityId,
|
|
2124
|
+
entityType: _entityType,
|
|
2125
|
+
pagination: _pagination
|
|
2126
|
+
});
|
|
2127
|
+
}
|
|
2128
|
+
async listScoresByScorerId({
|
|
2129
|
+
scorerId,
|
|
2130
|
+
pagination,
|
|
2131
|
+
entityId,
|
|
2132
|
+
entityType,
|
|
2133
|
+
source
|
|
2134
|
+
}) {
|
|
2135
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
2136
|
+
}
|
|
2137
|
+
async listScoresBySpan({
|
|
2138
|
+
traceId,
|
|
2139
|
+
spanId,
|
|
2140
|
+
pagination
|
|
2141
|
+
}) {
|
|
2142
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
2143
|
+
}
|
|
1636
2144
|
/**
|
|
1637
2145
|
* Close the database connection
|
|
1638
2146
|
* No explicit cleanup needed for D1 in either REST or Workers Binding mode
|
|
@@ -1640,10 +2148,8 @@ var D1Store = class extends MastraStorage {
|
|
|
1640
2148
|
async close() {
|
|
1641
2149
|
this.logger.debug("Closing D1 connection");
|
|
1642
2150
|
}
|
|
1643
|
-
async updateMessages(_args) {
|
|
1644
|
-
this.logger.error("updateMessages is not yet implemented in CloudflareD1Store");
|
|
1645
|
-
throw new Error("Method not implemented");
|
|
1646
|
-
}
|
|
1647
2151
|
};
|
|
1648
2152
|
|
|
1649
2153
|
export { D1Store };
|
|
2154
|
+
//# sourceMappingURL=index.js.map
|
|
2155
|
+
//# sourceMappingURL=index.js.map
|