@mastra/mssql 0.0.0-mssql-store-20250804200341 → 0.0.0-pgvector-index-fix-20250905222058

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/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './storage';
1
+ export * from './storage/index.js';
2
2
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -461,6 +461,7 @@ var MemoryMSSQL = class extends MemoryStorage {
461
461
  selectBy,
462
462
  orderByStatement
463
463
  }) {
464
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
464
465
  const include = selectBy?.include;
465
466
  if (!include) return null;
466
467
  const unionQueries = [];
@@ -532,11 +533,12 @@ var MemoryMSSQL = class extends MemoryStorage {
532
533
  return dedupedRows;
533
534
  }
534
535
  async getMessages(args) {
535
- const { threadId, format, selectBy } = args;
536
+ const { threadId, resourceId, format, selectBy } = args;
536
537
  const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId, resourceId`;
537
538
  const orderByStatement = `ORDER BY [seq_id] DESC`;
538
539
  const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
539
540
  try {
541
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
540
542
  let rows = [];
541
543
  const include = selectBy?.include || [];
542
544
  if (include?.length) {
@@ -574,7 +576,8 @@ var MemoryMSSQL = class extends MemoryStorage {
574
576
  domain: ErrorDomain.STORAGE,
575
577
  category: ErrorCategory.THIRD_PARTY,
576
578
  details: {
577
- threadId
579
+ threadId,
580
+ resourceId: resourceId ?? ""
578
581
  }
579
582
  },
580
583
  error
@@ -584,30 +587,65 @@ var MemoryMSSQL = class extends MemoryStorage {
584
587
  return [];
585
588
  }
586
589
  }
587
- async getMessagesPaginated(args) {
588
- const { threadId, selectBy } = args;
589
- const { page = 0, perPage: perPageInput } = selectBy?.pagination || {};
590
+ async getMessagesById({
591
+ messageIds,
592
+ format
593
+ }) {
594
+ if (messageIds.length === 0) return [];
595
+ const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId, resourceId`;
590
596
  const orderByStatement = `ORDER BY [seq_id] DESC`;
591
- if (selectBy?.include?.length) {
592
- await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
597
+ try {
598
+ let rows = [];
599
+ let query = `${selectStatement} FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} WHERE [id] IN (${messageIds.map((_, i) => `@id${i}`).join(", ")})`;
600
+ const request = this.pool.request();
601
+ messageIds.forEach((id, i) => request.input(`id${i}`, id));
602
+ query += ` ${orderByStatement}`;
603
+ const result = await request.query(query);
604
+ const remainingRows = result.recordset || [];
605
+ rows.push(...remainingRows);
606
+ rows.sort((a, b) => {
607
+ const timeDiff = a.seq_id - b.seq_id;
608
+ return timeDiff;
609
+ });
610
+ rows = rows.map(({ seq_id, ...rest }) => rest);
611
+ if (format === `v1`) return this._parseAndFormatMessages(rows, format);
612
+ return this._parseAndFormatMessages(rows, `v2`);
613
+ } catch (error) {
614
+ const mastraError = new MastraError(
615
+ {
616
+ id: "MASTRA_STORAGE_MSSQL_STORE_GET_MESSAGES_BY_ID_FAILED",
617
+ domain: ErrorDomain.STORAGE,
618
+ category: ErrorCategory.THIRD_PARTY,
619
+ details: {
620
+ messageIds: JSON.stringify(messageIds)
621
+ }
622
+ },
623
+ error
624
+ );
625
+ this.logger?.error?.(mastraError.toString());
626
+ this.logger?.trackException(mastraError);
627
+ return [];
593
628
  }
629
+ }
630
+ async getMessagesPaginated(args) {
631
+ const { threadId, resourceId, format, selectBy } = args;
632
+ const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
594
633
  try {
595
- const { threadId: threadId2, format, selectBy: selectBy2 } = args;
596
- const { page: page2 = 0, perPage: perPageInput2, dateRange } = selectBy2?.pagination || {};
634
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
597
635
  const fromDate = dateRange?.start;
598
636
  const toDate = dateRange?.end;
599
637
  const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId, resourceId`;
600
- const orderByStatement2 = `ORDER BY [seq_id] DESC`;
601
- let messages2 = [];
602
- if (selectBy2?.include?.length) {
603
- const includeMessages = await this._getIncludedMessages({ threadId: threadId2, selectBy: selectBy2, orderByStatement: orderByStatement2 });
604
- if (includeMessages) messages2.push(...includeMessages);
638
+ const orderByStatement = `ORDER BY [seq_id] DESC`;
639
+ let messages = [];
640
+ if (selectBy?.include?.length) {
641
+ const includeMessages = await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
642
+ if (includeMessages) messages.push(...includeMessages);
605
643
  }
606
- const perPage = perPageInput2 !== void 0 ? perPageInput2 : resolveMessageLimit({ last: selectBy2?.last, defaultLimit: 40 });
607
- const currentOffset = page2 * perPage;
644
+ const perPage = perPageInput !== void 0 ? perPageInput : resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
645
+ const currentOffset = page * perPage;
608
646
  const conditions = ["[thread_id] = @threadId"];
609
647
  const request = this.pool.request();
610
- request.input("threadId", threadId2);
648
+ request.input("threadId", threadId);
611
649
  if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
612
650
  conditions.push("[createdAt] >= @fromDate");
613
651
  request.input("fromDate", fromDate.toISOString());
@@ -620,35 +658,35 @@ var MemoryMSSQL = class extends MemoryStorage {
620
658
  const countQuery = `SELECT COUNT(*) as total FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} ${whereClause}`;
621
659
  const countResult = await request.query(countQuery);
622
660
  const total = parseInt(countResult.recordset[0]?.total, 10) || 0;
623
- if (total === 0 && messages2.length > 0) {
624
- const parsedIncluded = this._parseAndFormatMessages(messages2, format);
661
+ if (total === 0 && messages.length > 0) {
662
+ const parsedIncluded = this._parseAndFormatMessages(messages, format);
625
663
  return {
626
664
  messages: parsedIncluded,
627
665
  total: parsedIncluded.length,
628
- page: page2,
666
+ page,
629
667
  perPage,
630
668
  hasMore: false
631
669
  };
632
670
  }
633
- const excludeIds = messages2.map((m) => m.id);
671
+ const excludeIds = messages.map((m) => m.id);
634
672
  if (excludeIds.length > 0) {
635
673
  const excludeParams = excludeIds.map((_, idx) => `@id${idx}`);
636
674
  conditions.push(`id NOT IN (${excludeParams.join(", ")})`);
637
675
  excludeIds.forEach((id, idx) => request.input(`id${idx}`, id));
638
676
  }
639
677
  const finalWhereClause = `WHERE ${conditions.join(" AND ")}`;
640
- const dataQuery = `${selectStatement} FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} ${finalWhereClause} ${orderByStatement2} OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
678
+ const dataQuery = `${selectStatement} FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} ${finalWhereClause} ${orderByStatement} OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
641
679
  request.input("offset", currentOffset);
642
680
  request.input("limit", perPage);
643
681
  const rowsResult = await request.query(dataQuery);
644
682
  const rows = rowsResult.recordset || [];
645
683
  rows.sort((a, b) => a.seq_id - b.seq_id);
646
- messages2.push(...rows);
647
- const parsed = this._parseAndFormatMessages(messages2, format);
684
+ messages.push(...rows);
685
+ const parsed = this._parseAndFormatMessages(messages, format);
648
686
  return {
649
687
  messages: parsed,
650
688
  total: total + excludeIds.length,
651
- page: page2,
689
+ page,
652
690
  perPage,
653
691
  hasMore: currentOffset + rows.length < total
654
692
  };
@@ -660,6 +698,7 @@ var MemoryMSSQL = class extends MemoryStorage {
660
698
  category: ErrorCategory.THIRD_PARTY,
661
699
  details: {
662
700
  threadId,
701
+ resourceId: resourceId ?? "",
663
702
  page
664
703
  }
665
704
  },
@@ -1373,18 +1412,25 @@ ${columns}
1373
1412
  }
1374
1413
  }
1375
1414
  };
1376
- function transformScoreRow(row) {
1377
- let input = void 0;
1378
- if (row.input) {
1379
- try {
1380
- input = JSON.parse(row.input);
1381
- } catch {
1382
- input = row.input;
1383
- }
1415
+ function parseJSON(jsonString) {
1416
+ try {
1417
+ return JSON.parse(jsonString);
1418
+ } catch {
1419
+ return jsonString;
1384
1420
  }
1421
+ }
1422
+ function transformScoreRow(row) {
1385
1423
  return {
1386
1424
  ...row,
1387
- input,
1425
+ input: parseJSON(row.input),
1426
+ scorer: parseJSON(row.scorer),
1427
+ preprocessStepResult: parseJSON(row.preprocessStepResult),
1428
+ analyzeStepResult: parseJSON(row.analyzeStepResult),
1429
+ metadata: parseJSON(row.metadata),
1430
+ output: parseJSON(row.output),
1431
+ additionalContext: parseJSON(row.additionalContext),
1432
+ runtimeContext: parseJSON(row.runtimeContext),
1433
+ entity: parseJSON(row.entity),
1388
1434
  createdAt: row.createdAt,
1389
1435
  updatedAt: row.updatedAt
1390
1436
  };
@@ -1429,13 +1475,32 @@ var ScoresMSSQL = class extends ScoresStorage {
1429
1475
  async saveScore(score) {
1430
1476
  try {
1431
1477
  const scoreId = crypto.randomUUID();
1432
- const { input, ...rest } = score;
1478
+ const {
1479
+ scorer,
1480
+ preprocessStepResult,
1481
+ analyzeStepResult,
1482
+ metadata,
1483
+ input,
1484
+ output,
1485
+ additionalContext,
1486
+ runtimeContext,
1487
+ entity,
1488
+ ...rest
1489
+ } = score;
1433
1490
  await this.operations.insert({
1434
1491
  tableName: TABLE_SCORERS,
1435
1492
  record: {
1436
1493
  id: scoreId,
1437
1494
  ...rest,
1438
- input: JSON.stringify(input),
1495
+ input: JSON.stringify(input) || "",
1496
+ output: JSON.stringify(output) || "",
1497
+ preprocessStepResult: preprocessStepResult ? JSON.stringify(preprocessStepResult) : null,
1498
+ analyzeStepResult: analyzeStepResult ? JSON.stringify(analyzeStepResult) : null,
1499
+ metadata: metadata ? JSON.stringify(metadata) : null,
1500
+ additionalContext: additionalContext ? JSON.stringify(additionalContext) : null,
1501
+ runtimeContext: runtimeContext ? JSON.stringify(runtimeContext) : null,
1502
+ entity: entity ? JSON.stringify(entity) : null,
1503
+ scorer: scorer ? JSON.stringify(scorer) : null,
1439
1504
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
1440
1505
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1441
1506
  }
@@ -1806,6 +1871,22 @@ var WorkflowsMSSQL = class extends WorkflowsStorage {
1806
1871
  this.operations = operations;
1807
1872
  this.schema = schema;
1808
1873
  }
1874
+ updateWorkflowResults({
1875
+ // workflowName,
1876
+ // runId,
1877
+ // stepId,
1878
+ // result,
1879
+ // runtimeContext,
1880
+ }) {
1881
+ throw new Error("Method not implemented.");
1882
+ }
1883
+ updateWorkflowState({
1884
+ // workflowName,
1885
+ // runId,
1886
+ // opts,
1887
+ }) {
1888
+ throw new Error("Method not implemented.");
1889
+ }
1809
1890
  async persistWorkflowSnapshot({
1810
1891
  workflowName,
1811
1892
  runId,
@@ -2157,6 +2238,12 @@ var MSSQLStore = class extends MastraStorage {
2157
2238
  async getMessages(args) {
2158
2239
  return this.stores.memory.getMessages(args);
2159
2240
  }
2241
+ async getMessagesById({
2242
+ messageIds,
2243
+ format
2244
+ }) {
2245
+ return this.stores.memory.getMessagesById({ messageIds, format });
2246
+ }
2160
2247
  async getMessagesPaginated(args) {
2161
2248
  return this.stores.memory.getMessagesPaginated(args);
2162
2249
  }
@@ -2187,6 +2274,22 @@ var MSSQLStore = class extends MastraStorage {
2187
2274
  /**
2188
2275
  * Workflows
2189
2276
  */
2277
+ async updateWorkflowResults({
2278
+ workflowName,
2279
+ runId,
2280
+ stepId,
2281
+ result,
2282
+ runtimeContext
2283
+ }) {
2284
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2285
+ }
2286
+ async updateWorkflowState({
2287
+ workflowName,
2288
+ runId,
2289
+ opts
2290
+ }) {
2291
+ return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
2292
+ }
2190
2293
  async persistWorkflowSnapshot({
2191
2294
  workflowName,
2192
2295
  runId,