@mastra/mssql 0.0.0-share-agent-metadata-with-cloud-20250718110128 → 0.0.0-taofeeq-fix-tool-call-showing-after-message-20250806162745

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +197 -3
  2. package/dist/index.cjs +1575 -1037
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.ts +2 -4
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +1575 -1037
  7. package/dist/index.js.map +1 -0
  8. package/dist/storage/domains/legacy-evals/index.d.ts +20 -0
  9. package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
  10. package/dist/storage/domains/memory/index.d.ts +90 -0
  11. package/dist/storage/domains/memory/index.d.ts.map +1 -0
  12. package/dist/storage/domains/operations/index.d.ts +51 -0
  13. package/dist/storage/domains/operations/index.d.ts.map +1 -0
  14. package/dist/storage/domains/scores/index.d.ts +46 -0
  15. package/dist/storage/domains/scores/index.d.ts.map +1 -0
  16. package/dist/storage/domains/traces/index.d.ts +37 -0
  17. package/dist/storage/domains/traces/index.d.ts.map +1 -0
  18. package/dist/storage/domains/utils.d.ts +6 -0
  19. package/dist/storage/domains/utils.d.ts.map +1 -0
  20. package/dist/storage/domains/workflows/index.d.ts +36 -0
  21. package/dist/storage/domains/workflows/index.d.ts.map +1 -0
  22. package/dist/{_tsup-dts-rollup.d.cts → storage/index.d.ts} +104 -102
  23. package/dist/storage/index.d.ts.map +1 -0
  24. package/docker-compose.yaml +1 -1
  25. package/package.json +10 -9
  26. package/src/storage/domains/legacy-evals/index.ts +175 -0
  27. package/src/storage/domains/memory/index.ts +1024 -0
  28. package/src/storage/domains/operations/index.ts +401 -0
  29. package/src/storage/domains/scores/index.ts +316 -0
  30. package/src/storage/domains/traces/index.ts +212 -0
  31. package/src/storage/domains/utils.ts +12 -0
  32. package/src/storage/domains/workflows/index.ts +259 -0
  33. package/src/storage/index.test.ts +2211 -2222
  34. package/src/storage/index.ts +171 -1767
  35. package/tsconfig.build.json +9 -0
  36. package/tsconfig.json +1 -1
  37. package/tsup.config.ts +17 -0
  38. package/dist/_tsup-dts-rollup.d.ts +0 -213
  39. package/dist/index.d.cts +0 -4
@@ -1,18 +1,9 @@
1
- import { MessageList } from '@mastra/core/agent';
2
1
  import type { MastraMessageContentV2, MastraMessageV2 } from '@mastra/core/agent';
3
2
  export type MastraMessageV2WithTypedContent = Omit<MastraMessageV2, 'content'> & { content: MastraMessageContentV2 };
4
3
  import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
5
- import type { MetricResult } from '@mastra/core/eval';
6
4
  import type { MastraMessageV1, StorageThreadType } from '@mastra/core/memory';
7
- import {
8
- MastraStorage,
9
- TABLE_MESSAGES,
10
- TABLE_THREADS,
11
- TABLE_TRACES,
12
- TABLE_RESOURCES,
13
- TABLE_WORKFLOW_SNAPSHOT,
14
- TABLE_EVALS,
15
- } from '@mastra/core/storage';
5
+ import type { ScoreRowData } from '@mastra/core/scores';
6
+ import { MastraStorage } from '@mastra/core/storage';
16
7
  import type {
17
8
  EvalRow,
18
9
  PaginationInfo,
@@ -23,10 +14,21 @@ import type {
23
14
  WorkflowRun,
24
15
  WorkflowRuns,
25
16
  PaginationArgs,
17
+ StoragePagination,
18
+ ThreadSortOptions,
19
+ StorageDomains,
20
+ StorageGetTracesArg,
21
+ StorageGetTracesPaginatedArg,
26
22
  } from '@mastra/core/storage';
27
- import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
23
+ import type { Trace } from '@mastra/core/telemetry';
28
24
  import type { WorkflowRunState } from '@mastra/core/workflows';
29
25
  import sql from 'mssql';
26
+ import { LegacyEvalsMSSQL } from './domains/legacy-evals';
27
+ import { MemoryMSSQL } from './domains/memory';
28
+ import { StoreOperationsMSSQL } from './domains/operations';
29
+ import { ScoresMSSQL } from './domains/scores';
30
+ import { TracesMSSQL } from './domains/traces';
31
+ import { WorkflowsMSSQL } from './domains/workflows';
30
32
 
31
33
  export type MSSQLConfigType = {
32
34
  schemaName?: string;
@@ -49,9 +51,8 @@ export type MSSQLConfig = MSSQLConfigType;
49
51
  export class MSSQLStore extends MastraStorage {
50
52
  public pool: sql.ConnectionPool;
51
53
  private schema?: string;
52
- private setupSchemaPromise: Promise<void> | null = null;
53
- private schemaSetupComplete: boolean | undefined = undefined;
54
54
  private isConnected: Promise<boolean> | null = null;
55
+ stores: StorageDomains;
55
56
 
56
57
  constructor(config: MSSQLConfigType) {
57
58
  super({ name: 'MSSQLStore' });
@@ -73,7 +74,7 @@ export class MSSQLStore extends MastraStorage {
73
74
  }
74
75
  }
75
76
 
76
- this.schema = config.schemaName;
77
+ this.schema = config.schemaName || 'dbo';
77
78
  this.pool =
78
79
  'connectionString' in config
79
80
  ? new sql.ConnectionPool(config.connectionString)
@@ -85,6 +86,22 @@ export class MSSQLStore extends MastraStorage {
85
86
  port: config.port,
86
87
  options: config.options || { encrypt: true, trustServerCertificate: true },
87
88
  });
89
+
90
+ const legacyEvals = new LegacyEvalsMSSQL({ pool: this.pool, schema: this.schema });
91
+ const operations = new StoreOperationsMSSQL({ pool: this.pool, schemaName: this.schema });
92
+ const scores = new ScoresMSSQL({ pool: this.pool, operations, schema: this.schema });
93
+ const traces = new TracesMSSQL({ pool: this.pool, operations, schema: this.schema });
94
+ const workflows = new WorkflowsMSSQL({ pool: this.pool, operations, schema: this.schema });
95
+ const memory = new MemoryMSSQL({ pool: this.pool, schema: this.schema, operations });
96
+
97
+ this.stores = {
98
+ operations,
99
+ scores,
100
+ traces,
101
+ workflows,
102
+ legacyEvals,
103
+ memory,
104
+ };
88
105
  } catch (e) {
89
106
  throw new MastraError(
90
107
  {
@@ -129,343 +146,46 @@ export class MSSQLStore extends MastraStorage {
129
146
  public get supports(): {
130
147
  selectByIncludeResourceScope: boolean;
131
148
  resourceWorkingMemory: boolean;
149
+ hasColumn: boolean;
150
+ createTable: boolean;
151
+ deleteMessages: boolean;
132
152
  } {
133
153
  return {
134
154
  selectByIncludeResourceScope: true,
135
155
  resourceWorkingMemory: true,
136
- };
137
- }
138
-
139
- private getTableName(indexName: string) {
140
- const parsedIndexName = parseSqlIdentifier(indexName, 'index name');
141
- const quotedIndexName = `[${parsedIndexName}]`;
142
- const quotedSchemaName = this.getSchemaName();
143
- return quotedSchemaName ? `${quotedSchemaName}.${quotedIndexName}` : quotedIndexName;
144
- }
145
-
146
- private getSchemaName() {
147
- return this.schema ? `[${parseSqlIdentifier(this.schema, 'schema name')}]` : undefined;
148
- }
149
-
150
- private transformEvalRow(row: Record<string, any>): EvalRow {
151
- let testInfoValue = null,
152
- resultValue = null;
153
- if (row.test_info) {
154
- try {
155
- testInfoValue = typeof row.test_info === 'string' ? JSON.parse(row.test_info) : row.test_info;
156
- } catch {}
157
- }
158
- if (row.test_info) {
159
- try {
160
- resultValue = typeof row.result === 'string' ? JSON.parse(row.result) : row.result;
161
- } catch {}
162
- }
163
- return {
164
- agentName: row.agent_name as string,
165
- input: row.input as string,
166
- output: row.output as string,
167
- result: resultValue as MetricResult,
168
- metricName: row.metric_name as string,
169
- instructions: row.instructions as string,
170
- testInfo: testInfoValue,
171
- globalRunId: row.global_run_id as string,
172
- runId: row.run_id as string,
173
- createdAt: row.created_at as string,
156
+ hasColumn: true,
157
+ createTable: true,
158
+ deleteMessages: true,
174
159
  };
175
160
  }
176
161
 
177
162
  /** @deprecated use getEvals instead */
178
163
  async getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
179
- try {
180
- let query = `SELECT * FROM ${this.getTableName(TABLE_EVALS)} WHERE agent_name = @p1`;
181
- if (type === 'test') {
182
- query += " AND test_info IS NOT NULL AND JSON_VALUE(test_info, '$.testPath') IS NOT NULL";
183
- } else if (type === 'live') {
184
- query += " AND (test_info IS NULL OR JSON_VALUE(test_info, '$.testPath') IS NULL)";
185
- }
186
- query += ' ORDER BY created_at DESC';
187
-
188
- const request = this.pool.request();
189
- request.input('p1', agentName);
190
- const result = await request.query(query);
191
- const rows = result.recordset;
192
- return typeof this.transformEvalRow === 'function'
193
- ? (rows?.map((row: any) => this.transformEvalRow(row)) ?? [])
194
- : (rows ?? []);
195
- } catch (error: any) {
196
- if (error && error.number === 208 && error.message && error.message.includes('Invalid object name')) {
197
- return [];
198
- }
199
- console.error('Failed to get evals for the specified agent: ' + error?.message);
200
- throw error;
201
- }
202
- }
203
-
204
- async batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record<string, any>[] }): Promise<void> {
205
- const transaction = this.pool.transaction();
206
- try {
207
- await transaction.begin();
208
- for (const record of records) {
209
- await this.insert({ tableName, record });
210
- }
211
- await transaction.commit();
212
- } catch (error) {
213
- await transaction.rollback();
214
- throw new MastraError(
215
- {
216
- id: 'MASTRA_STORAGE_MSSQL_STORE_BATCH_INSERT_FAILED',
217
- domain: ErrorDomain.STORAGE,
218
- category: ErrorCategory.THIRD_PARTY,
219
- details: {
220
- tableName,
221
- numberOfRecords: records.length,
222
- },
223
- },
224
- error,
225
- );
226
- }
164
+ return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
227
165
  }
228
166
 
229
- /** @deprecated use getTracesPaginated instead*/
230
- public async getTraces(args: {
231
- name?: string;
232
- scope?: string;
233
- attributes?: Record<string, string>;
234
- filters?: Record<string, any>;
235
- page: number;
236
- perPage?: number;
237
- fromDate?: Date;
238
- toDate?: Date;
239
- }): Promise<any[]> {
240
- if (args.fromDate || args.toDate) {
241
- (args as any).dateRange = {
242
- start: args.fromDate,
243
- end: args.toDate,
244
- };
245
- }
246
- const result = await this.getTracesPaginated(args);
247
- return result.traces;
167
+ async getEvals(
168
+ options: {
169
+ agentName?: string;
170
+ type?: 'test' | 'live';
171
+ } & PaginationArgs = {},
172
+ ): Promise<PaginationInfo & { evals: EvalRow[] }> {
173
+ return this.stores.legacyEvals.getEvals(options);
248
174
  }
249
175
 
250
- public async getTracesPaginated(
251
- args: {
252
- name?: string;
253
- scope?: string;
254
- attributes?: Record<string, string>;
255
- filters?: Record<string, any>;
256
- } & PaginationArgs,
257
- ): Promise<
258
- PaginationInfo & {
259
- traces: any[];
260
- }
261
- > {
262
- const { name, scope, page = 0, perPage: perPageInput, attributes, filters, dateRange } = args;
263
- const fromDate = dateRange?.start;
264
- const toDate = dateRange?.end;
265
-
266
- const perPage = perPageInput !== undefined ? perPageInput : 100;
267
- const currentOffset = page * perPage;
268
-
269
- const paramMap: Record<string, any> = {};
270
- const conditions: string[] = [];
271
- let paramIndex = 1;
272
-
273
- if (name) {
274
- const paramName = `p${paramIndex++}`;
275
- conditions.push(`[name] LIKE @${paramName}`);
276
- paramMap[paramName] = `${name}%`;
277
- }
278
- if (scope) {
279
- const paramName = `p${paramIndex++}`;
280
- conditions.push(`[scope] = @${paramName}`);
281
- paramMap[paramName] = scope;
282
- }
283
- if (attributes) {
284
- Object.entries(attributes).forEach(([key, value]) => {
285
- const parsedKey = parseFieldKey(key);
286
- const paramName = `p${paramIndex++}`;
287
- conditions.push(`JSON_VALUE([attributes], '$.${parsedKey}') = @${paramName}`);
288
- paramMap[paramName] = value;
289
- });
290
- }
291
- if (filters) {
292
- Object.entries(filters).forEach(([key, value]) => {
293
- const parsedKey = parseFieldKey(key);
294
- const paramName = `p${paramIndex++}`;
295
- conditions.push(`[${parsedKey}] = @${paramName}`);
296
- paramMap[paramName] = value;
297
- });
298
- }
299
- if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
300
- const paramName = `p${paramIndex++}`;
301
- conditions.push(`[createdAt] >= @${paramName}`);
302
- paramMap[paramName] = fromDate.toISOString();
303
- }
304
- if (toDate instanceof Date && !isNaN(toDate.getTime())) {
305
- const paramName = `p${paramIndex++}`;
306
- conditions.push(`[createdAt] <= @${paramName}`);
307
- paramMap[paramName] = toDate.toISOString();
308
- }
309
-
310
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
311
-
312
- const countQuery = `SELECT COUNT(*) as total FROM ${this.getTableName(TABLE_TRACES)} ${whereClause}`;
313
- let total = 0;
314
- try {
315
- const countRequest = this.pool.request();
316
- Object.entries(paramMap).forEach(([key, value]) => {
317
- if (value instanceof Date) {
318
- countRequest.input(key, sql.DateTime, value);
319
- } else {
320
- countRequest.input(key, value);
321
- }
322
- });
323
- const countResult = await countRequest.query(countQuery);
324
- total = parseInt(countResult.recordset[0].total, 10);
325
- } catch (error) {
326
- throw new MastraError(
327
- {
328
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_TRACES_PAGINATED_FAILED_TO_RETRIEVE_TOTAL_COUNT',
329
- domain: ErrorDomain.STORAGE,
330
- category: ErrorCategory.THIRD_PARTY,
331
- details: {
332
- name: args.name ?? '',
333
- scope: args.scope ?? '',
334
- },
335
- },
336
- error,
337
- );
338
- }
339
-
340
- if (total === 0) {
341
- return {
342
- traces: [],
343
- total: 0,
344
- page,
345
- perPage,
346
- hasMore: false,
347
- };
348
- }
349
-
350
- const dataQuery = `SELECT * FROM ${this.getTableName(TABLE_TRACES)} ${whereClause} ORDER BY [seq_id] DESC OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
351
- const dataRequest = this.pool.request();
352
- Object.entries(paramMap).forEach(([key, value]) => {
353
- if (value instanceof Date) {
354
- dataRequest.input(key, sql.DateTime, value);
355
- } else {
356
- dataRequest.input(key, value);
357
- }
358
- });
359
- dataRequest.input('offset', currentOffset);
360
- dataRequest.input('limit', perPage);
361
-
362
- try {
363
- const rowsResult = await dataRequest.query(dataQuery);
364
- const rows = rowsResult.recordset;
365
- const traces = rows.map(row => ({
366
- id: row.id,
367
- parentSpanId: row.parentSpanId,
368
- traceId: row.traceId,
369
- name: row.name,
370
- scope: row.scope,
371
- kind: row.kind,
372
- status: JSON.parse(row.status),
373
- events: JSON.parse(row.events),
374
- links: JSON.parse(row.links),
375
- attributes: JSON.parse(row.attributes),
376
- startTime: row.startTime,
377
- endTime: row.endTime,
378
- other: row.other,
379
- createdAt: row.createdAt,
380
- }));
381
-
382
- return {
383
- traces,
384
- total,
385
- page,
386
- perPage,
387
- hasMore: currentOffset + traces.length < total,
388
- };
389
- } catch (error) {
390
- throw new MastraError(
391
- {
392
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_TRACES_PAGINATED_FAILED_TO_RETRIEVE_TRACES',
393
- domain: ErrorDomain.STORAGE,
394
- category: ErrorCategory.THIRD_PARTY,
395
- details: {
396
- name: args.name ?? '',
397
- scope: args.scope ?? '',
398
- },
399
- },
400
- error,
401
- );
402
- }
176
+ /**
177
+ * @deprecated use getTracesPaginated instead
178
+ */
179
+ public async getTraces(args: StorageGetTracesArg): Promise<Trace[]> {
180
+ return this.stores.traces.getTraces(args);
403
181
  }
404
182
 
405
- private async setupSchema() {
406
- if (!this.schema || this.schemaSetupComplete) {
407
- return;
408
- }
409
-
410
- if (!this.setupSchemaPromise) {
411
- this.setupSchemaPromise = (async () => {
412
- try {
413
- const checkRequest = this.pool.request();
414
- checkRequest.input('schemaName', this.schema);
415
- const checkResult = await checkRequest.query(`
416
- SELECT 1 AS found FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = @schemaName
417
- `);
418
- const schemaExists = Array.isArray(checkResult.recordset) && checkResult.recordset.length > 0;
419
-
420
- if (!schemaExists) {
421
- try {
422
- await this.pool.request().query(`CREATE SCHEMA [${this.schema}]`);
423
- this.logger?.info?.(`Schema "${this.schema}" created successfully`);
424
- } catch (error) {
425
- this.logger?.error?.(`Failed to create schema "${this.schema}"`, { error });
426
- throw new Error(
427
- `Unable to create schema "${this.schema}". This requires CREATE privilege on the database. ` +
428
- `Either create the schema manually or grant CREATE privilege to the user.`,
429
- );
430
- }
431
- }
432
-
433
- this.schemaSetupComplete = true;
434
- this.logger?.debug?.(`Schema "${this.schema}" is ready for use`);
435
- } catch (error) {
436
- this.schemaSetupComplete = undefined;
437
- this.setupSchemaPromise = null;
438
- throw error;
439
- } finally {
440
- this.setupSchemaPromise = null;
441
- }
442
- })();
443
- }
444
-
445
- await this.setupSchemaPromise;
183
+ public async getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & { traces: Trace[] }> {
184
+ return this.stores.traces.getTracesPaginated(args);
446
185
  }
447
186
 
448
- protected getSqlType(type: StorageColumn['type'], isPrimaryKey = false): string {
449
- switch (type) {
450
- case 'text':
451
- return isPrimaryKey ? 'NVARCHAR(255)' : 'NVARCHAR(MAX)';
452
- case 'timestamp':
453
- return 'DATETIME2(7)';
454
- case 'uuid':
455
- return 'UNIQUEIDENTIFIER';
456
- case 'jsonb':
457
- return 'NVARCHAR(MAX)';
458
- case 'integer':
459
- return 'INT';
460
- case 'bigint':
461
- return 'BIGINT';
462
- default:
463
- throw new MastraError({
464
- id: 'MASTRA_STORAGE_MSSQL_STORE_TYPE_NOT_SUPPORTED',
465
- domain: ErrorDomain.STORAGE,
466
- category: ErrorCategory.THIRD_PARTY,
467
- });
468
- }
187
+ async batchTraceInsert({ records }: { records: Record<string, any>[] }): Promise<void> {
188
+ return this.stores.traces.batchTraceInsert({ records });
469
189
  }
470
190
 
471
191
  async createTable({
@@ -475,88 +195,7 @@ export class MSSQLStore extends MastraStorage {
475
195
  tableName: TABLE_NAMES;
476
196
  schema: Record<string, StorageColumn>;
477
197
  }): Promise<void> {
478
- try {
479
- const uniqueConstraintColumns = tableName === TABLE_WORKFLOW_SNAPSHOT ? ['workflow_name', 'run_id'] : [];
480
-
481
- const columns = Object.entries(schema)
482
- .map(([name, def]) => {
483
- const parsedName = parseSqlIdentifier(name, 'column name');
484
- const constraints = [];
485
- if (def.primaryKey) constraints.push('PRIMARY KEY');
486
- if (!def.nullable) constraints.push('NOT NULL');
487
- const isIndexed = !!def.primaryKey || uniqueConstraintColumns.includes(name);
488
- return `[${parsedName}] ${this.getSqlType(def.type, isIndexed)} ${constraints.join(' ')}`.trim();
489
- })
490
- .join(',\n');
491
-
492
- if (this.schema) {
493
- await this.setupSchema();
494
- }
495
-
496
- const checkTableRequest = this.pool.request();
497
- checkTableRequest.input('tableName', this.getTableName(tableName).replace(/[[\]]/g, '').split('.').pop());
498
- const checkTableSql = `SELECT 1 AS found FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = @tableName`;
499
- checkTableRequest.input('schema', this.schema || 'dbo');
500
- const checkTableResult = await checkTableRequest.query(checkTableSql);
501
- const tableExists = Array.isArray(checkTableResult.recordset) && checkTableResult.recordset.length > 0;
502
-
503
- if (!tableExists) {
504
- const createSql = `CREATE TABLE ${this.getTableName(tableName)} (\n${columns}\n)`;
505
- await this.pool.request().query(createSql);
506
- }
507
-
508
- const columnCheckSql = `
509
- SELECT 1 AS found
510
- FROM INFORMATION_SCHEMA.COLUMNS
511
- WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = @tableName AND COLUMN_NAME = 'seq_id'
512
- `;
513
- const checkColumnRequest = this.pool.request();
514
- checkColumnRequest.input('schema', this.schema || 'dbo');
515
- checkColumnRequest.input('tableName', this.getTableName(tableName).replace(/[[\]]/g, '').split('.').pop());
516
- const columnResult = await checkColumnRequest.query(columnCheckSql);
517
- const columnExists = Array.isArray(columnResult.recordset) && columnResult.recordset.length > 0;
518
-
519
- if (!columnExists) {
520
- const alterSql = `ALTER TABLE ${this.getTableName(tableName)} ADD seq_id BIGINT IDENTITY(1,1)`;
521
- await this.pool.request().query(alterSql);
522
- }
523
-
524
- if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
525
- const constraintName = 'mastra_workflow_snapshot_workflow_name_run_id_key';
526
- const checkConstraintSql = `SELECT 1 AS found FROM sys.key_constraints WHERE name = @constraintName`;
527
- const checkConstraintRequest = this.pool.request();
528
- checkConstraintRequest.input('constraintName', constraintName);
529
- const constraintResult = await checkConstraintRequest.query(checkConstraintSql);
530
- const constraintExists = Array.isArray(constraintResult.recordset) && constraintResult.recordset.length > 0;
531
- if (!constraintExists) {
532
- const addConstraintSql = `ALTER TABLE ${this.getTableName(tableName)} ADD CONSTRAINT ${constraintName} UNIQUE ([workflow_name], [run_id])`;
533
- await this.pool.request().query(addConstraintSql);
534
- }
535
- }
536
- } catch (error) {
537
- throw new MastraError(
538
- {
539
- id: 'MASTRA_STORAGE_MSSQL_STORE_CREATE_TABLE_FAILED',
540
- domain: ErrorDomain.STORAGE,
541
- category: ErrorCategory.THIRD_PARTY,
542
- details: {
543
- tableName,
544
- },
545
- },
546
- error,
547
- );
548
- }
549
- }
550
-
551
- protected getDefaultValue(type: StorageColumn['type']): string {
552
- switch (type) {
553
- case 'timestamp':
554
- return 'DEFAULT SYSDATETIMEOFFSET()';
555
- case 'jsonb':
556
- return "DEFAULT N'{}'";
557
- default:
558
- return super.getDefaultValue(type);
559
- }
198
+ return this.stores.operations.createTable({ tableName, schema });
560
199
  }
561
200
 
562
201
  async alterTable({
@@ -568,321 +207,58 @@ export class MSSQLStore extends MastraStorage {
568
207
  schema: Record<string, StorageColumn>;
569
208
  ifNotExists: string[];
570
209
  }): Promise<void> {
571
- const fullTableName = this.getTableName(tableName);
572
- try {
573
- for (const columnName of ifNotExists) {
574
- if (schema[columnName]) {
575
- const columnCheckRequest = this.pool.request();
576
- columnCheckRequest.input('tableName', fullTableName.replace(/[[\]]/g, '').split('.').pop());
577
- columnCheckRequest.input('columnName', columnName);
578
- columnCheckRequest.input('schema', this.schema || 'dbo');
579
- const checkSql = `SELECT 1 AS found FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = @tableName AND COLUMN_NAME = @columnName`;
580
- const checkResult = await columnCheckRequest.query(checkSql);
581
- const columnExists = Array.isArray(checkResult.recordset) && checkResult.recordset.length > 0;
582
- if (!columnExists) {
583
- const columnDef = schema[columnName];
584
- const sqlType = this.getSqlType(columnDef.type);
585
- const nullable = columnDef.nullable === false ? 'NOT NULL' : '';
586
- const defaultValue = columnDef.nullable === false ? this.getDefaultValue(columnDef.type) : '';
587
- const parsedColumnName = parseSqlIdentifier(columnName, 'column name');
588
- const alterSql =
589
- `ALTER TABLE ${fullTableName} ADD [${parsedColumnName}] ${sqlType} ${nullable} ${defaultValue}`.trim();
590
- await this.pool.request().query(alterSql);
591
- this.logger?.debug?.(`Ensured column ${parsedColumnName} exists in table ${fullTableName}`);
592
- }
593
- }
594
- }
595
- } catch (error) {
596
- throw new MastraError(
597
- {
598
- id: 'MASTRA_STORAGE_MSSQL_STORE_ALTER_TABLE_FAILED',
599
- domain: ErrorDomain.STORAGE,
600
- category: ErrorCategory.THIRD_PARTY,
601
- details: {
602
- tableName,
603
- },
604
- },
605
- error,
606
- );
607
- }
210
+ return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
608
211
  }
609
212
 
610
213
  async clearTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
611
- const fullTableName = this.getTableName(tableName);
612
- try {
613
- const fkQuery = `
614
- SELECT
615
- OBJECT_SCHEMA_NAME(fk.parent_object_id) AS schema_name,
616
- OBJECT_NAME(fk.parent_object_id) AS table_name
617
- FROM sys.foreign_keys fk
618
- WHERE fk.referenced_object_id = OBJECT_ID(@fullTableName)
619
- `;
620
- const fkResult = await this.pool.request().input('fullTableName', fullTableName).query(fkQuery);
621
- const childTables: { schema_name: string; table_name: string }[] = fkResult.recordset || [];
622
- for (const child of childTables) {
623
- const childTableName = this.schema ? `[${child.schema_name}].[${child.table_name}]` : `[${child.table_name}]`;
624
- await this.clearTable({ tableName: childTableName as TABLE_NAMES });
625
- }
626
- await this.pool.request().query(`TRUNCATE TABLE ${fullTableName}`);
627
- } catch (error) {
628
- throw new MastraError(
629
- {
630
- id: 'MASTRA_STORAGE_MSSQL_STORE_CLEAR_TABLE_FAILED',
631
- domain: ErrorDomain.STORAGE,
632
- category: ErrorCategory.THIRD_PARTY,
633
- details: {
634
- tableName,
635
- },
636
- },
637
- error,
638
- );
639
- }
214
+ return this.stores.operations.clearTable({ tableName });
215
+ }
216
+
217
+ async dropTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
218
+ return this.stores.operations.dropTable({ tableName });
640
219
  }
641
220
 
642
221
  async insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record<string, any> }): Promise<void> {
643
- try {
644
- const columns = Object.keys(record).map(col => parseSqlIdentifier(col, 'column name'));
645
- const values = Object.values(record);
646
- const paramNames = values.map((_, i) => `@param${i}`);
647
- const insertSql = `INSERT INTO ${this.getTableName(tableName)} (${columns.map(c => `[${c}]`).join(', ')}) VALUES (${paramNames.join(', ')})`;
648
- const request = this.pool.request();
649
- values.forEach((value, i) => {
650
- if (value instanceof Date) {
651
- request.input(`param${i}`, sql.DateTime2, value);
652
- } else if (typeof value === 'object' && value !== null) {
653
- request.input(`param${i}`, JSON.stringify(value));
654
- } else {
655
- request.input(`param${i}`, value);
656
- }
657
- });
222
+ return this.stores.operations.insert({ tableName, record });
223
+ }
658
224
 
659
- await request.query(insertSql);
660
- } catch (error) {
661
- throw new MastraError(
662
- {
663
- id: 'MASTRA_STORAGE_MSSQL_STORE_INSERT_FAILED',
664
- domain: ErrorDomain.STORAGE,
665
- category: ErrorCategory.THIRD_PARTY,
666
- details: {
667
- tableName,
668
- },
669
- },
670
- error,
671
- );
672
- }
225
+ async batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record<string, any>[] }): Promise<void> {
226
+ return this.stores.operations.batchInsert({ tableName, records });
673
227
  }
674
228
 
675
229
  async load<R>({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, string> }): Promise<R | null> {
676
- try {
677
- const keyEntries = Object.entries(keys).map(([key, value]) => [parseSqlIdentifier(key, 'column name'), value]);
678
- const conditions = keyEntries.map(([key], i) => `[${key}] = @param${i}`).join(' AND ');
679
- const values = keyEntries.map(([_, value]) => value);
680
- const sql = `SELECT * FROM ${this.getTableName(tableName)} WHERE ${conditions}`;
681
- const request = this.pool.request();
682
- values.forEach((value, i) => {
683
- request.input(`param${i}`, value);
684
- });
685
- const resultSet = await request.query(sql);
686
- const result = resultSet.recordset[0] || null;
687
- if (!result) {
688
- return null;
689
- }
690
- if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
691
- const snapshot = result as any;
692
- if (typeof snapshot.snapshot === 'string') {
693
- snapshot.snapshot = JSON.parse(snapshot.snapshot);
694
- }
695
- return snapshot;
696
- }
697
- return result;
698
- } catch (error) {
699
- throw new MastraError(
700
- {
701
- id: 'MASTRA_STORAGE_MSSQL_STORE_LOAD_FAILED',
702
- domain: ErrorDomain.STORAGE,
703
- category: ErrorCategory.THIRD_PARTY,
704
- details: {
705
- tableName,
706
- },
707
- },
708
- error,
709
- );
710
- }
230
+ return this.stores.operations.load({ tableName, keys });
711
231
  }
712
232
 
233
+ /**
234
+ * Memory
235
+ */
236
+
713
237
  async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
714
- try {
715
- const sql = `SELECT
716
- id,
717
- [resourceId],
718
- title,
719
- metadata,
720
- [createdAt],
721
- [updatedAt]
722
- FROM ${this.getTableName(TABLE_THREADS)}
723
- WHERE id = @threadId`;
724
- const request = this.pool.request();
725
- request.input('threadId', threadId);
726
- const resultSet = await request.query(sql);
727
- const thread = resultSet.recordset[0] || null;
728
- if (!thread) {
729
- return null;
730
- }
731
- return {
732
- ...thread,
733
- metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,
734
- createdAt: thread.createdAt,
735
- updatedAt: thread.updatedAt,
736
- };
737
- } catch (error) {
738
- throw new MastraError(
739
- {
740
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_THREAD_BY_ID_FAILED',
741
- domain: ErrorDomain.STORAGE,
742
- category: ErrorCategory.THIRD_PARTY,
743
- details: {
744
- threadId,
745
- },
746
- },
747
- error,
748
- );
749
- }
238
+ return this.stores.memory.getThreadById({ threadId });
239
+ }
240
+
241
+ /**
242
+ * @deprecated use getThreadsByResourceIdPaginated instead
243
+ */
244
+ public async getThreadsByResourceId(args: { resourceId: string } & ThreadSortOptions): Promise<StorageThreadType[]> {
245
+ return this.stores.memory.getThreadsByResourceId(args);
750
246
  }
751
247
 
752
248
  public async getThreadsByResourceIdPaginated(
753
249
  args: {
754
250
  resourceId: string;
755
- } & PaginationArgs,
251
+ page: number;
252
+ perPage: number;
253
+ } & ThreadSortOptions,
756
254
  ): Promise<PaginationInfo & { threads: StorageThreadType[] }> {
757
- const { resourceId, page = 0, perPage: perPageInput } = args;
758
- try {
759
- const perPage = perPageInput !== undefined ? perPageInput : 100;
760
- const currentOffset = page * perPage;
761
- const baseQuery = `FROM ${this.getTableName(TABLE_THREADS)} WHERE [resourceId] = @resourceId`;
762
-
763
- const countQuery = `SELECT COUNT(*) as count ${baseQuery}`;
764
- const countRequest = this.pool.request();
765
- countRequest.input('resourceId', resourceId);
766
- const countResult = await countRequest.query(countQuery);
767
- const total = parseInt(countResult.recordset[0]?.count ?? '0', 10);
768
-
769
- if (total === 0) {
770
- return {
771
- threads: [],
772
- total: 0,
773
- page,
774
- perPage,
775
- hasMore: false,
776
- };
777
- }
778
-
779
- const dataQuery = `SELECT id, [resourceId], title, metadata, [createdAt], [updatedAt] ${baseQuery} ORDER BY [seq_id] DESC OFFSET @offset ROWS FETCH NEXT @perPage ROWS ONLY`;
780
- const dataRequest = this.pool.request();
781
- dataRequest.input('resourceId', resourceId);
782
- dataRequest.input('perPage', perPage);
783
- dataRequest.input('offset', currentOffset);
784
- const rowsResult = await dataRequest.query(dataQuery);
785
- const rows = rowsResult.recordset || [];
786
- const threads = rows.map(thread => ({
787
- ...thread,
788
- metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,
789
- createdAt: thread.createdAt,
790
- updatedAt: thread.updatedAt,
791
- }));
792
-
793
- return {
794
- threads,
795
- total,
796
- page,
797
- perPage,
798
- hasMore: currentOffset + threads.length < total,
799
- };
800
- } catch (error) {
801
- const mastraError = new MastraError(
802
- {
803
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED',
804
- domain: ErrorDomain.STORAGE,
805
- category: ErrorCategory.THIRD_PARTY,
806
- details: {
807
- resourceId,
808
- page,
809
- },
810
- },
811
- error,
812
- );
813
- this.logger?.error?.(mastraError.toString());
814
- this.logger?.trackException?.(mastraError);
815
- return { threads: [], total: 0, page, perPage: perPageInput || 100, hasMore: false };
816
- }
817
- }
818
-
819
- public async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
820
- try {
821
- const table = this.getTableName(TABLE_THREADS);
822
- const mergeSql = `MERGE INTO ${table} WITH (HOLDLOCK) AS target
823
- USING (SELECT @id AS id) AS source
824
- ON (target.id = source.id)
825
- WHEN MATCHED THEN
826
- UPDATE SET
827
- [resourceId] = @resourceId,
828
- title = @title,
829
- metadata = @metadata,
830
- [createdAt] = @createdAt,
831
- [updatedAt] = @updatedAt
832
- WHEN NOT MATCHED THEN
833
- INSERT (id, [resourceId], title, metadata, [createdAt], [updatedAt])
834
- VALUES (@id, @resourceId, @title, @metadata, @createdAt, @updatedAt);`;
835
- const req = this.pool.request();
836
- req.input('id', thread.id);
837
- req.input('resourceId', thread.resourceId);
838
- req.input('title', thread.title);
839
- req.input('metadata', thread.metadata ? JSON.stringify(thread.metadata) : null);
840
- req.input('createdAt', thread.createdAt);
841
- req.input('updatedAt', thread.updatedAt);
842
- await req.query(mergeSql);
843
- return thread;
844
- } catch (error) {
845
- throw new MastraError(
846
- {
847
- id: 'MASTRA_STORAGE_MSSQL_STORE_SAVE_THREAD_FAILED',
848
- domain: ErrorDomain.STORAGE,
849
- category: ErrorCategory.THIRD_PARTY,
850
- details: {
851
- threadId: thread.id,
852
- },
853
- },
854
- error,
855
- );
856
- }
255
+ return this.stores.memory.getThreadsByResourceIdPaginated(args);
857
256
  }
858
257
 
859
- /**
860
- * @deprecated use getThreadsByResourceIdPaginated instead
861
- */
862
- public async getThreadsByResourceId(args: { resourceId: string }): Promise<StorageThreadType[]> {
863
- const { resourceId } = args;
864
- try {
865
- const baseQuery = `FROM ${this.getTableName(TABLE_THREADS)} WHERE [resourceId] = @resourceId`;
866
- const dataQuery = `SELECT id, [resourceId], title, metadata, [createdAt], [updatedAt] ${baseQuery} ORDER BY [seq_id] DESC`;
867
- const request = this.pool.request();
868
- request.input('resourceId', resourceId);
869
- const resultSet = await request.query(dataQuery);
870
- const rows = resultSet.recordset || [];
871
- return rows.map(thread => ({
872
- ...thread,
873
- metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,
874
- createdAt: thread.createdAt,
875
- updatedAt: thread.updatedAt,
876
- }));
877
- } catch (error) {
878
- this.logger?.error?.(`Error getting threads for resource ${resourceId}:`, error);
879
- return [];
880
- }
258
+ async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
259
+ return this.stores.memory.saveThread({ thread });
881
260
  }
882
261
 
883
- /**
884
- * Updates a thread's title and metadata, merging with existing metadata. Returns the updated thread.
885
- */
886
262
  async updateThread({
887
263
  id,
888
264
  title,
@@ -892,195 +268,11 @@ export class MSSQLStore extends MastraStorage {
892
268
  title: string;
893
269
  metadata: Record<string, unknown>;
894
270
  }): Promise<StorageThreadType> {
895
- const existingThread = await this.getThreadById({ threadId: id });
896
- if (!existingThread) {
897
- throw new MastraError({
898
- id: 'MASTRA_STORAGE_MSSQL_STORE_UPDATE_THREAD_FAILED',
899
- domain: ErrorDomain.STORAGE,
900
- category: ErrorCategory.USER,
901
- text: `Thread ${id} not found`,
902
- details: {
903
- threadId: id,
904
- title,
905
- },
906
- });
907
- }
908
-
909
- const mergedMetadata = {
910
- ...existingThread.metadata,
911
- ...metadata,
912
- };
913
-
914
- try {
915
- const table = this.getTableName(TABLE_THREADS);
916
- const sql = `UPDATE ${table}
917
- SET title = @title,
918
- metadata = @metadata,
919
- [updatedAt] = @updatedAt
920
- OUTPUT INSERTED.*
921
- WHERE id = @id`;
922
- const req = this.pool.request();
923
- req.input('id', id);
924
- req.input('title', title);
925
- req.input('metadata', JSON.stringify(mergedMetadata));
926
- req.input('updatedAt', new Date().toISOString());
927
- const result = await req.query(sql);
928
- let thread = result.recordset && result.recordset[0];
929
- if (thread && 'seq_id' in thread) {
930
- const { seq_id, ...rest } = thread;
931
- thread = rest;
932
- }
933
- if (!thread) {
934
- throw new MastraError({
935
- id: 'MASTRA_STORAGE_MSSQL_STORE_UPDATE_THREAD_FAILED',
936
- domain: ErrorDomain.STORAGE,
937
- category: ErrorCategory.USER,
938
- text: `Thread ${id} not found after update`,
939
- details: {
940
- threadId: id,
941
- title,
942
- },
943
- });
944
- }
945
- return {
946
- ...thread,
947
- metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,
948
- createdAt: thread.createdAt,
949
- updatedAt: thread.updatedAt,
950
- };
951
- } catch (error) {
952
- throw new MastraError(
953
- {
954
- id: 'MASTRA_STORAGE_MSSQL_STORE_UPDATE_THREAD_FAILED',
955
- domain: ErrorDomain.STORAGE,
956
- category: ErrorCategory.THIRD_PARTY,
957
- details: {
958
- threadId: id,
959
- title,
960
- },
961
- },
962
- error,
963
- );
964
- }
271
+ return this.stores.memory.updateThread({ id, title, metadata });
965
272
  }
966
273
 
967
274
  async deleteThread({ threadId }: { threadId: string }): Promise<void> {
968
- const messagesTable = this.getTableName(TABLE_MESSAGES);
969
- const threadsTable = this.getTableName(TABLE_THREADS);
970
- const deleteMessagesSql = `DELETE FROM ${messagesTable} WHERE [thread_id] = @threadId`;
971
- const deleteThreadSql = `DELETE FROM ${threadsTable} WHERE id = @threadId`;
972
- const tx = this.pool.transaction();
973
- try {
974
- await tx.begin();
975
- const req = tx.request();
976
- req.input('threadId', threadId);
977
- await req.query(deleteMessagesSql);
978
- await req.query(deleteThreadSql);
979
- await tx.commit();
980
- } catch (error) {
981
- await tx.rollback().catch(() => {});
982
- throw new MastraError(
983
- {
984
- id: 'MASTRA_STORAGE_MSSQL_STORE_DELETE_THREAD_FAILED',
985
- domain: ErrorDomain.STORAGE,
986
- category: ErrorCategory.THIRD_PARTY,
987
- details: {
988
- threadId,
989
- },
990
- },
991
- error,
992
- );
993
- }
994
- }
995
-
996
- private async _getIncludedMessages({
997
- threadId,
998
- selectBy,
999
- orderByStatement,
1000
- }: {
1001
- threadId: string;
1002
- selectBy: StorageGetMessagesArg['selectBy'];
1003
- orderByStatement: string;
1004
- }) {
1005
- const include = selectBy?.include;
1006
- if (!include) return null;
1007
-
1008
- const unionQueries: string[] = [];
1009
- const paramValues: any[] = [];
1010
- let paramIdx = 1;
1011
- const paramNames: string[] = [];
1012
-
1013
- for (const inc of include) {
1014
- const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
1015
- const searchId = inc.threadId || threadId;
1016
-
1017
- const pThreadId = `@p${paramIdx}`;
1018
- const pId = `@p${paramIdx + 1}`;
1019
- const pPrev = `@p${paramIdx + 2}`;
1020
- const pNext = `@p${paramIdx + 3}`;
1021
-
1022
- unionQueries.push(
1023
- `
1024
- SELECT
1025
- m.id,
1026
- m.content,
1027
- m.role,
1028
- m.type,
1029
- m.[createdAt],
1030
- m.thread_id AS threadId,
1031
- m.[resourceId],
1032
- m.seq_id
1033
- FROM (
1034
- SELECT *, ROW_NUMBER() OVER (${orderByStatement}) as row_num
1035
- FROM ${this.getTableName(TABLE_MESSAGES)}
1036
- WHERE [thread_id] = ${pThreadId}
1037
- ) AS m
1038
- WHERE m.id = ${pId}
1039
- OR EXISTS (
1040
- SELECT 1
1041
- FROM (
1042
- SELECT *, ROW_NUMBER() OVER (${orderByStatement}) as row_num
1043
- FROM ${this.getTableName(TABLE_MESSAGES)}
1044
- WHERE [thread_id] = ${pThreadId}
1045
- ) AS target
1046
- WHERE target.id = ${pId}
1047
- AND (
1048
- (m.row_num <= target.row_num + ${pPrev} AND m.row_num > target.row_num)
1049
- OR
1050
- (m.row_num >= target.row_num - ${pNext} AND m.row_num < target.row_num)
1051
- )
1052
- )
1053
- `,
1054
- );
1055
-
1056
- paramValues.push(searchId, id, withPreviousMessages, withNextMessages);
1057
- paramNames.push(`p${paramIdx}`, `p${paramIdx + 1}`, `p${paramIdx + 2}`, `p${paramIdx + 3}`);
1058
- paramIdx += 4;
1059
- }
1060
-
1061
- const finalQuery = `
1062
- SELECT * FROM (
1063
- ${unionQueries.join(' UNION ALL ')}
1064
- ) AS union_result
1065
- ORDER BY [seq_id] ASC
1066
- `;
1067
-
1068
- const req = this.pool.request();
1069
- for (let i = 0; i < paramValues.length; ++i) {
1070
- req.input(paramNames[i] as string, paramValues[i]);
1071
- }
1072
-
1073
- const result = await req.query(finalQuery);
1074
- const includedRows = result.recordset || [];
1075
-
1076
- const seen = new Set<string>();
1077
- const dedupedRows = includedRows.filter((row: any) => {
1078
- if (seen.has(row.id)) return false;
1079
- seen.add(row.id);
1080
- return true;
1081
- });
1082
-
1083
- return dedupedRows;
275
+ return this.stores.memory.deleteThread({ threadId });
1084
276
  }
1085
277
 
1086
278
  /**
@@ -1093,87 +285,7 @@ export class MSSQLStore extends MastraStorage {
1093
285
  format?: 'v1' | 'v2';
1094
286
  },
1095
287
  ): Promise<MastraMessageV1[] | MastraMessageV2[]> {
1096
- const { threadId, format, selectBy } = args;
1097
- const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId`;
1098
- const orderByStatement = `ORDER BY [seq_id] DESC`;
1099
- const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
1100
- try {
1101
- let rows: any[] = [];
1102
- const include = selectBy?.include || [];
1103
- if (include?.length) {
1104
- const includeMessages = await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
1105
- if (includeMessages) {
1106
- rows.push(...includeMessages);
1107
- }
1108
- }
1109
- const excludeIds = rows.map(m => m.id).filter(Boolean);
1110
-
1111
- let query = `${selectStatement} FROM ${this.getTableName(TABLE_MESSAGES)} WHERE [thread_id] = @threadId`;
1112
- const request = this.pool.request();
1113
- request.input('threadId', threadId);
1114
-
1115
- if (excludeIds.length > 0) {
1116
- const excludeParams = excludeIds.map((_, idx) => `@id${idx}`);
1117
- query += ` AND id NOT IN (${excludeParams.join(', ')})`;
1118
- excludeIds.forEach((id, idx) => {
1119
- request.input(`id${idx}`, id);
1120
- });
1121
- }
1122
-
1123
- query += ` ${orderByStatement} OFFSET 0 ROWS FETCH NEXT @limit ROWS ONLY`;
1124
- request.input('limit', limit);
1125
- const result = await request.query(query);
1126
- const remainingRows = result.recordset || [];
1127
- rows.push(...remainingRows);
1128
- rows.sort((a, b) => {
1129
- const timeDiff = a.seq_id - b.seq_id;
1130
- return timeDiff;
1131
- });
1132
- rows = rows.map(({ seq_id, ...rest }) => rest);
1133
- const fetchedMessages = (rows || []).map(message => {
1134
- if (typeof message.content === 'string') {
1135
- try {
1136
- message.content = JSON.parse(message.content);
1137
- } catch {}
1138
- }
1139
-
1140
- if (format === 'v1') {
1141
- if (Array.isArray(message.content)) {
1142
- } else if (typeof message.content === 'object' && message.content && Array.isArray(message.content.parts)) {
1143
- message.content = message.content.parts;
1144
- } else {
1145
- message.content = [{ type: 'text', text: '' }];
1146
- }
1147
- } else {
1148
- if (typeof message.content !== 'object' || !message.content || !('parts' in message.content)) {
1149
- message.content = { format: 2, parts: [{ type: 'text', text: '' }] };
1150
- }
1151
- }
1152
- if (message.type === 'v2') delete message.type;
1153
- return message as MastraMessageV1;
1154
- });
1155
- return format === 'v2'
1156
- ? fetchedMessages.map(
1157
- m =>
1158
- ({ ...m, content: m.content || { format: 2, parts: [{ type: 'text', text: '' }] } }) as MastraMessageV2,
1159
- )
1160
- : fetchedMessages;
1161
- } catch (error) {
1162
- const mastraError = new MastraError(
1163
- {
1164
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_MESSAGES_FAILED',
1165
- domain: ErrorDomain.STORAGE,
1166
- category: ErrorCategory.THIRD_PARTY,
1167
- details: {
1168
- threadId,
1169
- },
1170
- },
1171
- error,
1172
- );
1173
- this.logger?.error?.(mastraError.toString());
1174
- this.logger?.trackException(mastraError);
1175
- return [];
1176
- }
288
+ return this.stores.memory.getMessages(args);
1177
289
  }
1178
290
 
1179
291
  public async getMessagesPaginated(
@@ -1181,246 +293,58 @@ export class MSSQLStore extends MastraStorage {
1181
293
  format?: 'v1' | 'v2';
1182
294
  },
1183
295
  ): Promise<PaginationInfo & { messages: MastraMessageV1[] | MastraMessageV2[] }> {
1184
- const { threadId, selectBy } = args;
1185
- const { page = 0, perPage: perPageInput } = selectBy?.pagination || {};
1186
- const orderByStatement = `ORDER BY [seq_id] DESC`;
1187
- let messages: any[] = [];
1188
- if (selectBy?.include?.length) {
1189
- const includeMessages = await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
1190
- if (includeMessages) {
1191
- messages.push(...includeMessages);
1192
- }
1193
- }
1194
- try {
1195
- const { threadId, format, selectBy } = args;
1196
- const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
1197
- const fromDate = dateRange?.start;
1198
- const toDate = dateRange?.end;
1199
-
1200
- const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId`;
1201
- const orderByStatement = `ORDER BY [seq_id] DESC`;
1202
-
1203
- let messages: any[] = [];
1204
-
1205
- if (selectBy?.include?.length) {
1206
- const includeMessages = await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
1207
- if (includeMessages) messages.push(...includeMessages);
1208
- }
1209
-
1210
- const perPage =
1211
- perPageInput !== undefined
1212
- ? perPageInput
1213
- : this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
1214
- const currentOffset = page * perPage;
1215
-
1216
- const conditions: string[] = ['[thread_id] = @threadId'];
1217
- const request = this.pool.request();
1218
- request.input('threadId', threadId);
1219
-
1220
- if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
1221
- conditions.push('[createdAt] >= @fromDate');
1222
- request.input('fromDate', fromDate.toISOString());
1223
- }
1224
- if (toDate instanceof Date && !isNaN(toDate.getTime())) {
1225
- conditions.push('[createdAt] <= @toDate');
1226
- request.input('toDate', toDate.toISOString());
1227
- }
1228
-
1229
- const whereClause = `WHERE ${conditions.join(' AND ')}`;
1230
- const countQuery = `SELECT COUNT(*) as total FROM ${this.getTableName(TABLE_MESSAGES)} ${whereClause}`;
1231
- const countResult = await request.query(countQuery);
1232
- const total = parseInt(countResult.recordset[0]?.total, 10) || 0;
1233
-
1234
- if (total === 0 && messages.length > 0) {
1235
- const parsedIncluded = this._parseAndFormatMessages(messages, format);
1236
- return {
1237
- messages: parsedIncluded,
1238
- total: parsedIncluded.length,
1239
- page,
1240
- perPage,
1241
- hasMore: false,
1242
- };
1243
- }
1244
-
1245
- const excludeIds = messages.map(m => m.id);
1246
- if (excludeIds.length > 0) {
1247
- const excludeParams = excludeIds.map((_, idx) => `@id${idx}`);
1248
- conditions.push(`id NOT IN (${excludeParams.join(', ')})`);
1249
- excludeIds.forEach((id, idx) => request.input(`id${idx}`, id));
1250
- }
1251
-
1252
- const finalWhereClause = `WHERE ${conditions.join(' AND ')}`;
1253
- const dataQuery = `${selectStatement} FROM ${this.getTableName(TABLE_MESSAGES)} ${finalWhereClause} ${orderByStatement} OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
1254
-
1255
- request.input('offset', currentOffset);
1256
- request.input('limit', perPage);
296
+ return this.stores.memory.getMessagesPaginated(args);
297
+ }
1257
298
 
1258
- const rowsResult = await request.query(dataQuery);
1259
- const rows = rowsResult.recordset || [];
1260
- rows.sort((a, b) => a.seq_id - b.seq_id);
1261
- messages.push(...rows);
299
+ async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
300
+ async saveMessages(args: { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[]>;
301
+ async saveMessages(
302
+ args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraMessageV2[]; format: 'v2' },
303
+ ): Promise<MastraMessageV2[] | MastraMessageV1[]> {
304
+ return this.stores.memory.saveMessages(args);
305
+ }
1262
306
 
1263
- const parsed = this._parseAndFormatMessages(messages, format);
1264
- return {
1265
- messages: parsed,
1266
- total: total + excludeIds.length,
1267
- page,
1268
- perPage,
1269
- hasMore: currentOffset + rows.length < total,
307
+ async updateMessages({
308
+ messages,
309
+ }: {
310
+ messages: (Partial<Omit<MastraMessageV2, 'createdAt'>> & {
311
+ id: string;
312
+ content?: {
313
+ metadata?: MastraMessageContentV2['metadata'];
314
+ content?: MastraMessageContentV2['content'];
1270
315
  };
1271
- } catch (error) {
1272
- const mastraError = new MastraError(
1273
- {
1274
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_MESSAGES_PAGINATED_FAILED',
1275
- domain: ErrorDomain.STORAGE,
1276
- category: ErrorCategory.THIRD_PARTY,
1277
- details: {
1278
- threadId,
1279
- page,
1280
- },
1281
- },
1282
- error,
1283
- );
1284
- this.logger?.error?.(mastraError.toString());
1285
- this.logger?.trackException(mastraError);
1286
- return { messages: [], total: 0, page, perPage: perPageInput || 40, hasMore: false };
1287
- }
316
+ })[];
317
+ }): Promise<MastraMessageV2[]> {
318
+ return this.stores.memory.updateMessages({ messages });
1288
319
  }
1289
320
 
1290
- private _parseAndFormatMessages(messages: any[], format?: 'v1' | 'v2') {
1291
- const parsedMessages = messages.map(message => {
1292
- let parsed = message;
1293
- if (typeof parsed.content === 'string') {
1294
- try {
1295
- parsed = { ...parsed, content: JSON.parse(parsed.content) };
1296
- } catch {}
1297
- }
1298
-
1299
- if (format === 'v1') {
1300
- if (Array.isArray(parsed.content)) {
1301
- } else if (parsed.content?.parts) {
1302
- parsed.content = parsed.content.parts;
1303
- } else {
1304
- parsed.content = [{ type: 'text', text: '' }];
1305
- }
1306
- } else {
1307
- if (!parsed.content?.parts) {
1308
- parsed = { ...parsed, content: { format: 2, parts: [{ type: 'text', text: '' }] } };
1309
- }
1310
- }
321
+ async deleteMessages(messageIds: string[]): Promise<void> {
322
+ return this.stores.memory.deleteMessages(messageIds);
323
+ }
1311
324
 
1312
- return parsed;
1313
- });
325
+ async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {
326
+ return this.stores.memory.getResourceById({ resourceId });
327
+ }
1314
328
 
1315
- const list = new MessageList().add(parsedMessages, 'memory');
1316
- return format === 'v2' ? list.get.all.v2() : list.get.all.v1();
329
+ async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {
330
+ return this.stores.memory.saveResource({ resource });
1317
331
  }
1318
332
 
1319
- async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
1320
- async saveMessages(args: { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[]>;
1321
- async saveMessages({
1322
- messages,
1323
- format,
1324
- }:
1325
- | { messages: MastraMessageV1[]; format?: undefined | 'v1' }
1326
- | { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[] | MastraMessageV1[]> {
1327
- if (messages.length === 0) return messages;
1328
- const threadId = messages[0]?.threadId;
1329
- if (!threadId) {
1330
- throw new MastraError({
1331
- id: 'MASTRA_STORAGE_MSSQL_STORE_SAVE_MESSAGES_FAILED',
1332
- domain: ErrorDomain.STORAGE,
1333
- category: ErrorCategory.THIRD_PARTY,
1334
- text: `Thread ID is required`,
1335
- });
1336
- }
1337
- const thread = await this.getThreadById({ threadId });
1338
- if (!thread) {
1339
- throw new MastraError({
1340
- id: 'MASTRA_STORAGE_MSSQL_STORE_SAVE_MESSAGES_FAILED',
1341
- domain: ErrorDomain.STORAGE,
1342
- category: ErrorCategory.THIRD_PARTY,
1343
- text: `Thread ${threadId} not found`,
1344
- details: { threadId },
1345
- });
1346
- }
1347
- const tableMessages = this.getTableName(TABLE_MESSAGES);
1348
- const tableThreads = this.getTableName(TABLE_THREADS);
1349
- try {
1350
- const transaction = this.pool.transaction();
1351
- await transaction.begin();
1352
- try {
1353
- for (const message of messages) {
1354
- if (!message.threadId) {
1355
- throw new Error(
1356
- `Expected to find a threadId for message, but couldn't find one. An unexpected error has occurred.`,
1357
- );
1358
- }
1359
- if (!message.resourceId) {
1360
- throw new Error(
1361
- `Expected to find a resourceId for message, but couldn't find one. An unexpected error has occurred.`,
1362
- );
1363
- }
1364
- const request = transaction.request();
1365
- request.input('id', message.id);
1366
- request.input('thread_id', message.threadId);
1367
- request.input(
1368
- 'content',
1369
- typeof message.content === 'string' ? message.content : JSON.stringify(message.content),
1370
- );
1371
- request.input('createdAt', message.createdAt.toISOString() || new Date().toISOString());
1372
- request.input('role', message.role);
1373
- request.input('type', message.type || 'v2');
1374
- request.input('resourceId', message.resourceId);
1375
- const mergeSql = `MERGE INTO ${tableMessages} AS target
1376
- USING (SELECT @id AS id) AS src
1377
- ON target.id = src.id
1378
- WHEN MATCHED THEN UPDATE SET
1379
- thread_id = @thread_id,
1380
- content = @content,
1381
- [createdAt] = @createdAt,
1382
- role = @role,
1383
- type = @type,
1384
- resourceId = @resourceId
1385
- WHEN NOT MATCHED THEN INSERT (id, thread_id, content, [createdAt], role, type, resourceId)
1386
- VALUES (@id, @thread_id, @content, @createdAt, @role, @type, @resourceId);`;
1387
- await request.query(mergeSql);
1388
- }
1389
- const threadReq = transaction.request();
1390
- threadReq.input('updatedAt', new Date().toISOString());
1391
- threadReq.input('id', threadId);
1392
- await threadReq.query(`UPDATE ${tableThreads} SET [updatedAt] = @updatedAt WHERE id = @id`);
1393
- await transaction.commit();
1394
- } catch (error) {
1395
- await transaction.rollback();
1396
- throw error;
1397
- }
1398
- const messagesWithParsedContent = messages.map(message => {
1399
- if (typeof message.content === 'string') {
1400
- try {
1401
- return { ...message, content: JSON.parse(message.content) };
1402
- } catch {
1403
- return message;
1404
- }
1405
- }
1406
- return message;
1407
- });
1408
- const list = new MessageList().add(messagesWithParsedContent, 'memory');
1409
- if (format === 'v2') return list.get.all.v2();
1410
- return list.get.all.v1();
1411
- } catch (error) {
1412
- throw new MastraError(
1413
- {
1414
- id: 'MASTRA_STORAGE_MSSQL_STORE_SAVE_MESSAGES_FAILED',
1415
- domain: ErrorDomain.STORAGE,
1416
- category: ErrorCategory.THIRD_PARTY,
1417
- details: { threadId },
1418
- },
1419
- error,
1420
- );
1421
- }
333
+ async updateResource({
334
+ resourceId,
335
+ workingMemory,
336
+ metadata,
337
+ }: {
338
+ resourceId: string;
339
+ workingMemory?: string;
340
+ metadata?: Record<string, unknown>;
341
+ }): Promise<StorageResourceType> {
342
+ return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
1422
343
  }
1423
344
 
345
+ /**
346
+ * Workflows
347
+ */
1424
348
  async persistWorkflowSnapshot({
1425
349
  workflowName,
1426
350
  runId,
@@ -1430,38 +354,7 @@ export class MSSQLStore extends MastraStorage {
1430
354
  runId: string;
1431
355
  snapshot: WorkflowRunState;
1432
356
  }): Promise<void> {
1433
- const table = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
1434
- const now = new Date().toISOString();
1435
- try {
1436
- const request = this.pool.request();
1437
- request.input('workflow_name', workflowName);
1438
- request.input('run_id', runId);
1439
- request.input('snapshot', JSON.stringify(snapshot));
1440
- request.input('createdAt', now);
1441
- request.input('updatedAt', now);
1442
- const mergeSql = `MERGE INTO ${table} AS target
1443
- USING (SELECT @workflow_name AS workflow_name, @run_id AS run_id) AS src
1444
- ON target.workflow_name = src.workflow_name AND target.run_id = src.run_id
1445
- WHEN MATCHED THEN UPDATE SET
1446
- snapshot = @snapshot,
1447
- [updatedAt] = @updatedAt
1448
- WHEN NOT MATCHED THEN INSERT (workflow_name, run_id, snapshot, [createdAt], [updatedAt])
1449
- VALUES (@workflow_name, @run_id, @snapshot, @createdAt, @updatedAt);`;
1450
- await request.query(mergeSql);
1451
- } catch (error) {
1452
- throw new MastraError(
1453
- {
1454
- id: 'MASTRA_STORAGE_MSSQL_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED',
1455
- domain: ErrorDomain.STORAGE,
1456
- category: ErrorCategory.THIRD_PARTY,
1457
- details: {
1458
- workflowName,
1459
- runId,
1460
- },
1461
- },
1462
- error,
1463
- );
1464
- }
357
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
1465
358
  }
1466
359
 
1467
360
  async loadWorkflowSnapshot({
@@ -1471,64 +364,7 @@ export class MSSQLStore extends MastraStorage {
1471
364
  workflowName: string;
1472
365
  runId: string;
1473
366
  }): Promise<WorkflowRunState | null> {
1474
- try {
1475
- const result = await this.load({
1476
- tableName: TABLE_WORKFLOW_SNAPSHOT,
1477
- keys: {
1478
- workflow_name: workflowName,
1479
- run_id: runId,
1480
- },
1481
- });
1482
- if (!result) {
1483
- return null;
1484
- }
1485
- return (result as any).snapshot;
1486
- } catch (error) {
1487
- throw new MastraError(
1488
- {
1489
- id: 'MASTRA_STORAGE_MSSQL_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED',
1490
- domain: ErrorDomain.STORAGE,
1491
- category: ErrorCategory.THIRD_PARTY,
1492
- details: {
1493
- workflowName,
1494
- runId,
1495
- },
1496
- },
1497
- error,
1498
- );
1499
- }
1500
- }
1501
-
1502
- private async hasColumn(table: string, column: string): Promise<boolean> {
1503
- const schema = this.schema || 'dbo';
1504
- const request = this.pool.request();
1505
- request.input('schema', schema);
1506
- request.input('table', table);
1507
- request.input('column', column);
1508
- request.input('columnLower', column.toLowerCase());
1509
- const result = await request.query(
1510
- `SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = @table AND (COLUMN_NAME = @column OR COLUMN_NAME = @columnLower)`,
1511
- );
1512
- return result.recordset.length > 0;
1513
- }
1514
-
1515
- private parseWorkflowRun(row: any): WorkflowRun {
1516
- let parsedSnapshot: WorkflowRunState | string = row.snapshot as string;
1517
- if (typeof parsedSnapshot === 'string') {
1518
- try {
1519
- parsedSnapshot = JSON.parse(row.snapshot as string) as WorkflowRunState;
1520
- } catch (e) {
1521
- console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1522
- }
1523
- }
1524
- return {
1525
- workflowName: row.workflow_name,
1526
- runId: row.run_id,
1527
- snapshot: parsedSnapshot,
1528
- createdAt: row.createdAt,
1529
- updatedAt: row.updatedAt,
1530
- resourceId: row.resourceId,
1531
- };
367
+ return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
1532
368
  }
1533
369
 
1534
370
  async getWorkflowRuns({
@@ -1546,75 +382,7 @@ export class MSSQLStore extends MastraStorage {
1546
382
  offset?: number;
1547
383
  resourceId?: string;
1548
384
  } = {}): Promise<WorkflowRuns> {
1549
- try {
1550
- const conditions: string[] = [];
1551
- const paramMap: Record<string, any> = {};
1552
-
1553
- if (workflowName) {
1554
- conditions.push(`[workflow_name] = @workflowName`);
1555
- paramMap['workflowName'] = workflowName;
1556
- }
1557
-
1558
- if (resourceId) {
1559
- const hasResourceId = await this.hasColumn(TABLE_WORKFLOW_SNAPSHOT, 'resourceId');
1560
- if (hasResourceId) {
1561
- conditions.push(`[resourceId] = @resourceId`);
1562
- paramMap['resourceId'] = resourceId;
1563
- } else {
1564
- console.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
1565
- }
1566
- }
1567
-
1568
- if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
1569
- conditions.push(`[createdAt] >= @fromDate`);
1570
- paramMap[`fromDate`] = fromDate.toISOString();
1571
- }
1572
-
1573
- if (toDate instanceof Date && !isNaN(toDate.getTime())) {
1574
- conditions.push(`[createdAt] <= @toDate`);
1575
- paramMap[`toDate`] = toDate.toISOString();
1576
- }
1577
-
1578
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
1579
- let total = 0;
1580
- const tableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
1581
- const request = this.pool.request();
1582
- Object.entries(paramMap).forEach(([key, value]) => {
1583
- if (value instanceof Date) {
1584
- request.input(key, sql.DateTime, value);
1585
- } else {
1586
- request.input(key, value);
1587
- }
1588
- });
1589
-
1590
- if (limit !== undefined && offset !== undefined) {
1591
- const countQuery = `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`;
1592
- const countResult = await request.query(countQuery);
1593
- total = Number(countResult.recordset[0]?.count || 0);
1594
- }
1595
-
1596
- let query = `SELECT * FROM ${tableName} ${whereClause} ORDER BY [seq_id] DESC`;
1597
- if (limit !== undefined && offset !== undefined) {
1598
- query += ` OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
1599
- request.input('limit', limit);
1600
- request.input('offset', offset);
1601
- }
1602
- const result = await request.query(query);
1603
- const runs = (result.recordset || []).map(row => this.parseWorkflowRun(row));
1604
- return { runs, total: total || runs.length };
1605
- } catch (error) {
1606
- throw new MastraError(
1607
- {
1608
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_WORKFLOW_RUNS_FAILED',
1609
- domain: ErrorDomain.STORAGE,
1610
- category: ErrorCategory.THIRD_PARTY,
1611
- details: {
1612
- workflowName: workflowName || 'all',
1613
- },
1614
- },
1615
- error,
1616
- );
1617
- }
385
+ return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
1618
386
  }
1619
387
 
1620
388
  async getWorkflowRunById({
@@ -1624,421 +392,57 @@ export class MSSQLStore extends MastraStorage {
1624
392
  runId: string;
1625
393
  workflowName?: string;
1626
394
  }): Promise<WorkflowRun | null> {
1627
- try {
1628
- const conditions: string[] = [];
1629
- const paramMap: Record<string, any> = {};
1630
-
1631
- if (runId) {
1632
- conditions.push(`[run_id] = @runId`);
1633
- paramMap['runId'] = runId;
1634
- }
1635
-
1636
- if (workflowName) {
1637
- conditions.push(`[workflow_name] = @workflowName`);
1638
- paramMap['workflowName'] = workflowName;
1639
- }
1640
-
1641
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
1642
- const tableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
1643
- const query = `SELECT * FROM ${tableName} ${whereClause}`;
1644
- const request = this.pool.request();
1645
- Object.entries(paramMap).forEach(([key, value]) => request.input(key, value));
1646
- const result = await request.query(query);
1647
-
1648
- if (!result.recordset || result.recordset.length === 0) {
1649
- return null;
1650
- }
1651
-
1652
- return this.parseWorkflowRun(result.recordset[0]);
1653
- } catch (error) {
1654
- throw new MastraError(
1655
- {
1656
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED',
1657
- domain: ErrorDomain.STORAGE,
1658
- category: ErrorCategory.THIRD_PARTY,
1659
- details: {
1660
- runId,
1661
- workflowName: workflowName || '',
1662
- },
1663
- },
1664
- error,
1665
- );
1666
- }
395
+ return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
1667
396
  }
1668
397
 
1669
- async updateMessages({
1670
- messages,
1671
- }: {
1672
- messages: (Partial<Omit<MastraMessageV2, 'createdAt'>> & {
1673
- id: string;
1674
- content?: {
1675
- metadata?: MastraMessageContentV2['metadata'];
1676
- content?: MastraMessageContentV2['content'];
1677
- };
1678
- })[];
1679
- }): Promise<MastraMessageV2[]> {
1680
- if (!messages || messages.length === 0) {
1681
- return [];
1682
- }
1683
-
1684
- const messageIds = messages.map(m => m.id);
1685
- const idParams = messageIds.map((_, i) => `@id${i}`).join(', ');
1686
- let selectQuery = `SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId FROM ${this.getTableName(TABLE_MESSAGES)}`;
1687
- if (idParams.length > 0) {
1688
- selectQuery += ` WHERE id IN (${idParams})`;
1689
- } else {
1690
- return [];
1691
- }
1692
- const selectReq = this.pool.request();
1693
- messageIds.forEach((id, i) => selectReq.input(`id${i}`, id));
1694
- const existingMessagesDb = (await selectReq.query(selectQuery)).recordset;
1695
- if (!existingMessagesDb || existingMessagesDb.length === 0) {
1696
- return [];
1697
- }
1698
-
1699
- const existingMessages: MastraMessageV2[] = existingMessagesDb.map(msg => {
1700
- if (typeof msg.content === 'string') {
1701
- try {
1702
- msg.content = JSON.parse(msg.content);
1703
- } catch {}
1704
- }
1705
- return msg as MastraMessageV2;
1706
- });
1707
-
1708
- const threadIdsToUpdate = new Set<string>();
1709
- const transaction = this.pool.transaction();
1710
-
1711
- try {
1712
- await transaction.begin();
1713
- for (const existingMessage of existingMessages) {
1714
- const updatePayload = messages.find(m => m.id === existingMessage.id);
1715
- if (!updatePayload) continue;
1716
- const { id, ...fieldsToUpdate } = updatePayload;
1717
- if (Object.keys(fieldsToUpdate).length === 0) continue;
1718
- threadIdsToUpdate.add(existingMessage.threadId!);
1719
- if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {
1720
- threadIdsToUpdate.add(updatePayload.threadId);
1721
- }
1722
- const setClauses: string[] = [];
1723
- const req = transaction.request();
1724
- req.input('id', id);
1725
- const columnMapping: Record<string, string> = { threadId: 'thread_id' };
1726
- const updatableFields = { ...fieldsToUpdate };
1727
- if (updatableFields.content) {
1728
- const newContent = {
1729
- ...existingMessage.content,
1730
- ...updatableFields.content,
1731
- ...(existingMessage.content?.metadata && updatableFields.content.metadata
1732
- ? { metadata: { ...existingMessage.content.metadata, ...updatableFields.content.metadata } }
1733
- : {}),
1734
- };
1735
- setClauses.push(`content = @content`);
1736
- req.input('content', JSON.stringify(newContent));
1737
- delete updatableFields.content;
1738
- }
1739
- for (const key in updatableFields) {
1740
- if (Object.prototype.hasOwnProperty.call(updatableFields, key)) {
1741
- const dbColumn = columnMapping[key] || key;
1742
- setClauses.push(`[${dbColumn}] = @${dbColumn}`);
1743
- req.input(dbColumn, updatableFields[key as keyof typeof updatableFields]);
1744
- }
1745
- }
1746
- if (setClauses.length > 0) {
1747
- const updateSql = `UPDATE ${this.getTableName(TABLE_MESSAGES)} SET ${setClauses.join(', ')} WHERE id = @id`;
1748
- await req.query(updateSql);
1749
- }
1750
- }
1751
- if (threadIdsToUpdate.size > 0) {
1752
- const threadIdParams = Array.from(threadIdsToUpdate)
1753
- .map((_, i) => `@tid${i}`)
1754
- .join(', ');
1755
- const threadReq = transaction.request();
1756
- Array.from(threadIdsToUpdate).forEach((tid, i) => threadReq.input(`tid${i}`, tid));
1757
- threadReq.input('updatedAt', new Date().toISOString());
1758
- const threadSql = `UPDATE ${this.getTableName(TABLE_THREADS)} SET updatedAt = @updatedAt WHERE id IN (${threadIdParams})`;
1759
- await threadReq.query(threadSql);
1760
- }
1761
- await transaction.commit();
1762
- } catch (error) {
1763
- await transaction.rollback();
1764
- throw new MastraError(
1765
- {
1766
- id: 'MASTRA_STORAGE_MSSQL_UPDATE_MESSAGES_FAILED',
1767
- domain: ErrorDomain.STORAGE,
1768
- category: ErrorCategory.THIRD_PARTY,
1769
- },
1770
- error,
1771
- );
1772
- }
1773
-
1774
- const refetchReq = this.pool.request();
1775
- messageIds.forEach((id, i) => refetchReq.input(`id${i}`, id));
1776
- const updatedMessages = (await refetchReq.query(selectQuery)).recordset;
1777
- return (updatedMessages || []).map(message => {
1778
- if (typeof message.content === 'string') {
1779
- try {
1780
- message.content = JSON.parse(message.content);
1781
- } catch {}
1782
- }
1783
- return message;
1784
- });
398
+ async close(): Promise<void> {
399
+ await this.pool.close();
1785
400
  }
1786
401
 
1787
- async close() {
1788
- if (this.pool) {
1789
- try {
1790
- if (this.pool.connected) {
1791
- await this.pool.close();
1792
- } else if (this.pool.connecting) {
1793
- await this.pool.connect();
1794
- await this.pool.close();
1795
- }
1796
- } catch (err: any) {
1797
- if (err.message && err.message.includes('Cannot close a pool while it is connecting')) {
1798
- } else {
1799
- throw err;
1800
- }
1801
- }
1802
- }
402
+ /**
403
+ * Scorers
404
+ */
405
+ async getScoreById({ id: _id }: { id: string }): Promise<ScoreRowData | null> {
406
+ return this.stores.scores.getScoreById({ id: _id });
1803
407
  }
1804
408
 
1805
- async getEvals(
1806
- options: {
1807
- agentName?: string;
1808
- type?: 'test' | 'live';
1809
- } & PaginationArgs = {},
1810
- ): Promise<PaginationInfo & { evals: EvalRow[] }> {
1811
- const { agentName, type, page = 0, perPage = 100, dateRange } = options;
1812
- const fromDate = dateRange?.start;
1813
- const toDate = dateRange?.end;
1814
-
1815
- const where: string[] = [];
1816
- const params: Record<string, any> = {};
1817
-
1818
- if (agentName) {
1819
- where.push('agent_name = @agentName');
1820
- params['agentName'] = agentName;
1821
- }
1822
-
1823
- if (type === 'test') {
1824
- where.push("test_info IS NOT NULL AND JSON_VALUE(test_info, '$.testPath') IS NOT NULL");
1825
- } else if (type === 'live') {
1826
- where.push("(test_info IS NULL OR JSON_VALUE(test_info, '$.testPath') IS NULL)");
1827
- }
1828
-
1829
- if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
1830
- where.push(`[created_at] >= @fromDate`);
1831
- params[`fromDate`] = fromDate.toISOString();
1832
- }
1833
-
1834
- if (toDate instanceof Date && !isNaN(toDate.getTime())) {
1835
- where.push(`[created_at] <= @toDate`);
1836
- params[`toDate`] = toDate.toISOString();
1837
- }
1838
-
1839
- const whereClause = where.length > 0 ? `WHERE ${where.join(' AND ')}` : '';
1840
- const tableName = this.getTableName(TABLE_EVALS);
1841
- const offset = page * perPage;
1842
-
1843
- const countQuery = `SELECT COUNT(*) as total FROM ${tableName} ${whereClause}`;
1844
- const dataQuery = `SELECT * FROM ${tableName} ${whereClause} ORDER BY seq_id DESC OFFSET @offset ROWS FETCH NEXT @perPage ROWS ONLY`;
1845
-
1846
- try {
1847
- const countReq = this.pool.request();
1848
- Object.entries(params).forEach(([key, value]) => {
1849
- if (value instanceof Date) {
1850
- countReq.input(key, sql.DateTime, value);
1851
- } else {
1852
- countReq.input(key, value);
1853
- }
1854
- });
1855
- const countResult = await countReq.query(countQuery);
1856
- const total = countResult.recordset[0]?.total || 0;
1857
-
1858
- if (total === 0) {
1859
- return {
1860
- evals: [],
1861
- total: 0,
1862
- page,
1863
- perPage,
1864
- hasMore: false,
1865
- };
1866
- }
1867
-
1868
- const req = this.pool.request();
1869
- Object.entries(params).forEach(([key, value]) => {
1870
- if (value instanceof Date) {
1871
- req.input(key, sql.DateTime, value);
1872
- } else {
1873
- req.input(key, value);
1874
- }
1875
- });
1876
- req.input('offset', offset);
1877
- req.input('perPage', perPage);
1878
-
1879
- const result = await req.query(dataQuery);
1880
- const rows = result.recordset;
1881
-
1882
- return {
1883
- evals: rows?.map(row => this.transformEvalRow(row)) ?? [],
1884
- total,
1885
- page,
1886
- perPage,
1887
- hasMore: offset + (rows?.length ?? 0) < total,
1888
- };
1889
- } catch (error) {
1890
- const mastraError = new MastraError(
1891
- {
1892
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_EVALS_FAILED',
1893
- domain: ErrorDomain.STORAGE,
1894
- category: ErrorCategory.THIRD_PARTY,
1895
- details: {
1896
- agentName: agentName || 'all',
1897
- type: type || 'all',
1898
- page,
1899
- perPage,
1900
- },
1901
- },
1902
- error,
1903
- );
1904
- this.logger?.error?.(mastraError.toString());
1905
- this.logger?.trackException(mastraError);
1906
- throw mastraError;
1907
- }
409
+ async getScoresByScorerId({
410
+ scorerId: _scorerId,
411
+ pagination: _pagination,
412
+ }: {
413
+ scorerId: string;
414
+ pagination: StoragePagination;
415
+ }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
416
+ return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
1908
417
  }
1909
418
 
1910
- async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {
1911
- const tableName = this.getTableName(TABLE_RESOURCES);
1912
- try {
1913
- const req = this.pool.request();
1914
- req.input('id', resource.id);
1915
- req.input('workingMemory', resource.workingMemory);
1916
- req.input('metadata', JSON.stringify(resource.metadata));
1917
- req.input('createdAt', resource.createdAt.toISOString());
1918
- req.input('updatedAt', resource.updatedAt.toISOString());
1919
-
1920
- await req.query(
1921
- `INSERT INTO ${tableName} (id, workingMemory, metadata, createdAt, updatedAt) VALUES (@id, @workingMemory, @metadata, @createdAt, @updatedAt)`,
1922
- );
1923
-
1924
- return resource;
1925
- } catch (error) {
1926
- const mastraError = new MastraError(
1927
- {
1928
- id: 'MASTRA_STORAGE_MSSQL_SAVE_RESOURCE_FAILED',
1929
- domain: ErrorDomain.STORAGE,
1930
- category: ErrorCategory.THIRD_PARTY,
1931
- details: { resourceId: resource.id },
1932
- },
1933
- error,
1934
- );
1935
- this.logger?.error?.(mastraError.toString());
1936
- this.logger?.trackException(mastraError);
1937
- throw mastraError;
1938
- }
419
+ async saveScore(_score: ScoreRowData): Promise<{ score: ScoreRowData }> {
420
+ return this.stores.scores.saveScore(_score);
1939
421
  }
1940
422
 
1941
- async updateResource({
1942
- resourceId,
1943
- workingMemory,
1944
- metadata,
423
+ async getScoresByRunId({
424
+ runId: _runId,
425
+ pagination: _pagination,
1945
426
  }: {
1946
- resourceId: string;
1947
- workingMemory?: string;
1948
- metadata?: Record<string, unknown>;
1949
- }): Promise<StorageResourceType> {
1950
- try {
1951
- const existingResource = await this.getResourceById({ resourceId });
1952
-
1953
- if (!existingResource) {
1954
- const newResource: StorageResourceType = {
1955
- id: resourceId,
1956
- workingMemory,
1957
- metadata: metadata || {},
1958
- createdAt: new Date(),
1959
- updatedAt: new Date(),
1960
- };
1961
- return this.saveResource({ resource: newResource });
1962
- }
1963
-
1964
- const updatedResource = {
1965
- ...existingResource,
1966
- workingMemory: workingMemory !== undefined ? workingMemory : existingResource.workingMemory,
1967
- metadata: {
1968
- ...existingResource.metadata,
1969
- ...metadata,
1970
- },
1971
- updatedAt: new Date(),
1972
- };
1973
-
1974
- const tableName = this.getTableName(TABLE_RESOURCES);
1975
- const updates: string[] = [];
1976
- const req = this.pool.request();
1977
-
1978
- if (workingMemory !== undefined) {
1979
- updates.push('workingMemory = @workingMemory');
1980
- req.input('workingMemory', workingMemory);
1981
- }
1982
-
1983
- if (metadata) {
1984
- updates.push('metadata = @metadata');
1985
- req.input('metadata', JSON.stringify(updatedResource.metadata));
1986
- }
1987
-
1988
- updates.push('updatedAt = @updatedAt');
1989
- req.input('updatedAt', updatedResource.updatedAt.toISOString());
1990
-
1991
- req.input('id', resourceId);
1992
-
1993
- await req.query(`UPDATE ${tableName} SET ${updates.join(', ')} WHERE id = @id`);
1994
-
1995
- return updatedResource;
1996
- } catch (error) {
1997
- const mastraError = new MastraError(
1998
- {
1999
- id: 'MASTRA_STORAGE_MSSQL_UPDATE_RESOURCE_FAILED',
2000
- domain: ErrorDomain.STORAGE,
2001
- category: ErrorCategory.THIRD_PARTY,
2002
- details: { resourceId },
2003
- },
2004
- error,
2005
- );
2006
- this.logger?.error?.(mastraError.toString());
2007
- this.logger?.trackException(mastraError);
2008
- throw mastraError;
2009
- }
427
+ runId: string;
428
+ pagination: StoragePagination;
429
+ }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
430
+ return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
2010
431
  }
2011
432
 
2012
- async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {
2013
- const tableName = this.getTableName(TABLE_RESOURCES);
2014
- try {
2015
- const req = this.pool.request();
2016
- req.input('resourceId', resourceId);
2017
- const result = (await req.query(`SELECT * FROM ${tableName} WHERE id = @resourceId`)).recordset[0];
2018
-
2019
- if (!result) {
2020
- return null;
2021
- }
2022
-
2023
- return {
2024
- ...result,
2025
- workingMemory:
2026
- typeof result.workingMemory === 'object' ? JSON.stringify(result.workingMemory) : result.workingMemory,
2027
- metadata: typeof result.metadata === 'string' ? JSON.parse(result.metadata) : result.metadata,
2028
- };
2029
- } catch (error) {
2030
- const mastraError = new MastraError(
2031
- {
2032
- id: 'MASTRA_STORAGE_MSSQL_GET_RESOURCE_BY_ID_FAILED',
2033
- domain: ErrorDomain.STORAGE,
2034
- category: ErrorCategory.THIRD_PARTY,
2035
- details: { resourceId },
2036
- },
2037
- error,
2038
- );
2039
- this.logger?.error?.(mastraError.toString());
2040
- this.logger?.trackException(mastraError);
2041
- throw mastraError;
2042
- }
433
+ async getScoresByEntityId({
434
+ entityId: _entityId,
435
+ entityType: _entityType,
436
+ pagination: _pagination,
437
+ }: {
438
+ pagination: StoragePagination;
439
+ entityId: string;
440
+ entityType: string;
441
+ }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
442
+ return this.stores.scores.getScoresByEntityId({
443
+ entityId: _entityId,
444
+ entityType: _entityType,
445
+ pagination: _pagination,
446
+ });
2043
447
  }
2044
448
  }