@mastra/mssql 0.3.6 → 0.3.7-alpha.0
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 +15 -0
- package/dist/index.cjs +25 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +25 -26
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +3 -3
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
|
|
@@ -625,29 +628,24 @@ var MemoryMSSQL = class extends MemoryStorage {
|
|
|
625
628
|
}
|
|
626
629
|
}
|
|
627
630
|
async getMessagesPaginated(args) {
|
|
628
|
-
const { threadId, selectBy } = args;
|
|
629
|
-
const { page = 0, perPage: perPageInput } = selectBy?.pagination || {};
|
|
630
|
-
const orderByStatement = `ORDER BY [seq_id] DESC`;
|
|
631
|
-
if (selectBy?.include?.length) {
|
|
632
|
-
await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
|
|
633
|
-
}
|
|
631
|
+
const { threadId, resourceId, format, selectBy } = args;
|
|
632
|
+
const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
|
|
634
633
|
try {
|
|
635
|
-
|
|
636
|
-
const { page: page2 = 0, perPage: perPageInput2, dateRange } = selectBy2?.pagination || {};
|
|
634
|
+
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
637
635
|
const fromDate = dateRange?.start;
|
|
638
636
|
const toDate = dateRange?.end;
|
|
639
637
|
const selectStatement = `SELECT seq_id, id, content, role, type, [createdAt], thread_id AS threadId, resourceId`;
|
|
640
|
-
const
|
|
641
|
-
let
|
|
642
|
-
if (
|
|
643
|
-
const includeMessages = await this._getIncludedMessages({ threadId
|
|
644
|
-
if (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);
|
|
645
643
|
}
|
|
646
|
-
const perPage =
|
|
647
|
-
const currentOffset =
|
|
644
|
+
const perPage = perPageInput !== void 0 ? perPageInput : resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
645
|
+
const currentOffset = page * perPage;
|
|
648
646
|
const conditions = ["[thread_id] = @threadId"];
|
|
649
647
|
const request = this.pool.request();
|
|
650
|
-
request.input("threadId",
|
|
648
|
+
request.input("threadId", threadId);
|
|
651
649
|
if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
|
|
652
650
|
conditions.push("[createdAt] >= @fromDate");
|
|
653
651
|
request.input("fromDate", fromDate.toISOString());
|
|
@@ -660,35 +658,35 @@ var MemoryMSSQL = class extends MemoryStorage {
|
|
|
660
658
|
const countQuery = `SELECT COUNT(*) as total FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} ${whereClause}`;
|
|
661
659
|
const countResult = await request.query(countQuery);
|
|
662
660
|
const total = parseInt(countResult.recordset[0]?.total, 10) || 0;
|
|
663
|
-
if (total === 0 &&
|
|
664
|
-
const parsedIncluded = this._parseAndFormatMessages(
|
|
661
|
+
if (total === 0 && messages.length > 0) {
|
|
662
|
+
const parsedIncluded = this._parseAndFormatMessages(messages, format);
|
|
665
663
|
return {
|
|
666
664
|
messages: parsedIncluded,
|
|
667
665
|
total: parsedIncluded.length,
|
|
668
|
-
page
|
|
666
|
+
page,
|
|
669
667
|
perPage,
|
|
670
668
|
hasMore: false
|
|
671
669
|
};
|
|
672
670
|
}
|
|
673
|
-
const excludeIds =
|
|
671
|
+
const excludeIds = messages.map((m) => m.id);
|
|
674
672
|
if (excludeIds.length > 0) {
|
|
675
673
|
const excludeParams = excludeIds.map((_, idx) => `@id${idx}`);
|
|
676
674
|
conditions.push(`id NOT IN (${excludeParams.join(", ")})`);
|
|
677
675
|
excludeIds.forEach((id, idx) => request.input(`id${idx}`, id));
|
|
678
676
|
}
|
|
679
677
|
const finalWhereClause = `WHERE ${conditions.join(" AND ")}`;
|
|
680
|
-
const dataQuery = `${selectStatement} FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} ${finalWhereClause} ${
|
|
678
|
+
const dataQuery = `${selectStatement} FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} ${finalWhereClause} ${orderByStatement} OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
|
|
681
679
|
request.input("offset", currentOffset);
|
|
682
680
|
request.input("limit", perPage);
|
|
683
681
|
const rowsResult = await request.query(dataQuery);
|
|
684
682
|
const rows = rowsResult.recordset || [];
|
|
685
683
|
rows.sort((a, b) => a.seq_id - b.seq_id);
|
|
686
|
-
|
|
687
|
-
const parsed = this._parseAndFormatMessages(
|
|
684
|
+
messages.push(...rows);
|
|
685
|
+
const parsed = this._parseAndFormatMessages(messages, format);
|
|
688
686
|
return {
|
|
689
687
|
messages: parsed,
|
|
690
688
|
total: total + excludeIds.length,
|
|
691
|
-
page
|
|
689
|
+
page,
|
|
692
690
|
perPage,
|
|
693
691
|
hasMore: currentOffset + rows.length < total
|
|
694
692
|
};
|
|
@@ -700,6 +698,7 @@ var MemoryMSSQL = class extends MemoryStorage {
|
|
|
700
698
|
category: ErrorCategory.THIRD_PARTY,
|
|
701
699
|
details: {
|
|
702
700
|
threadId,
|
|
701
|
+
resourceId: resourceId ?? "",
|
|
703
702
|
page
|
|
704
703
|
}
|
|
705
704
|
},
|