@mastra/mysql 0.7.0-alpha.0 → 0.7.0-alpha.2
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 +28 -0
- package/dist/index.cjs +47 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +47 -17
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +23 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -4921,9 +4921,17 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
4921
4921
|
if (row.type && row.type !== "v2") message.type = row.type;
|
|
4922
4922
|
return message;
|
|
4923
4923
|
}
|
|
4924
|
-
|
|
4925
|
-
|
|
4926
|
-
|
|
4924
|
+
/**
|
|
4925
|
+
* Loads a thread's messages in chronological order.
|
|
4926
|
+
*
|
|
4927
|
+
* @param threadId - Thread to read.
|
|
4928
|
+
* @param limit - Optional cap on the number of rows.
|
|
4929
|
+
* @param resourceId - When set, returns only the rows owned by that resource.
|
|
4930
|
+
*/
|
|
4931
|
+
async fetchMessagesForThread(threadId, limit, resourceId) {
|
|
4932
|
+
const resourceCondition = resourceId ? ` AND ${quoteIdentifier("resourceId", "column name")} = ?` : "";
|
|
4933
|
+
let sql = `SELECT id, thread_id, content, role, type, createdAt, resourceId FROM ${formatTableName(TABLE_MESSAGES)} WHERE ${quoteIdentifier("thread_id", "column name")} = ?${resourceCondition} ORDER BY ${quoteIdentifier("createdAt", "column name")} ASC`;
|
|
4934
|
+
const params = resourceId ? [threadId, resourceId] : [threadId];
|
|
4927
4935
|
if (limit && limit > 0) {
|
|
4928
4936
|
sql += ` LIMIT ?`;
|
|
4929
4937
|
params.push(limit);
|
|
@@ -4934,15 +4942,20 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
4934
4942
|
/**
|
|
4935
4943
|
* Fetches included messages by ID, discovering their thread automatically.
|
|
4936
4944
|
* This handles cross-thread includes where the include item doesn't specify a threadId.
|
|
4945
|
+
*
|
|
4946
|
+
* @param include - Message ids to pin, each with an optional before/after window.
|
|
4947
|
+
* @param resourceId - When set, restricts both the pinned messages and their context
|
|
4948
|
+
* to that resource so an id from another resource returns nothing.
|
|
4937
4949
|
*/
|
|
4938
|
-
async _getIncludedMessages({ include }) {
|
|
4950
|
+
async _getIncludedMessages({ include, resourceId }) {
|
|
4939
4951
|
if (!include || include.length === 0) return null;
|
|
4940
4952
|
const tableName = formatTableName(TABLE_MESSAGES);
|
|
4941
4953
|
const selectColumns = `id, thread_id, content, role, type, createdAt, resourceId`;
|
|
4954
|
+
const resourceCondition = resourceId ? ` AND m.${quoteIdentifier("resourceId", "column name")} = ?` : "";
|
|
4942
4955
|
const targetIds = include.map((inc) => inc.id).filter(Boolean);
|
|
4943
4956
|
if (targetIds.length === 0) return null;
|
|
4944
4957
|
const idPlaceholders = targetIds.map(() => "?").join(", ");
|
|
4945
|
-
const [targetRows] = await this.pool.execute(`SELECT id, thread_id, createdAt FROM ${tableName} WHERE id IN (${idPlaceholders})`, targetIds);
|
|
4958
|
+
const [targetRows] = await this.pool.execute(`SELECT id, thread_id, createdAt FROM ${tableName} WHERE id IN (${idPlaceholders})${resourceId ? ` AND ${quoteIdentifier("resourceId", "column name")} = ?` : ""}`, resourceId ? [...targetIds, resourceId] : targetIds);
|
|
4946
4959
|
if (!targetRows || targetRows.length === 0) return null;
|
|
4947
4960
|
const targetMap = new Map(targetRows.map((r) => [r.id, {
|
|
4948
4961
|
threadId: r.thread_id,
|
|
@@ -4960,21 +4973,23 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
4960
4973
|
SELECT ${selectColumns}
|
|
4961
4974
|
FROM ${tableName} m
|
|
4962
4975
|
WHERE m.thread_id = ?
|
|
4963
|
-
AND m.createdAt <=
|
|
4976
|
+
AND m.createdAt <= ?${resourceCondition}
|
|
4964
4977
|
ORDER BY m.createdAt DESC, m.id DESC
|
|
4965
4978
|
LIMIT ${prevLimit}
|
|
4966
4979
|
)`);
|
|
4967
4980
|
params.push(target.threadId, target.createdAt);
|
|
4981
|
+
if (resourceId) params.push(resourceId);
|
|
4968
4982
|
if (nextLimit > 0) {
|
|
4969
4983
|
unionQueries.push(`(
|
|
4970
4984
|
SELECT ${selectColumns}
|
|
4971
4985
|
FROM ${tableName} m
|
|
4972
4986
|
WHERE m.thread_id = ?
|
|
4973
|
-
AND m.createdAt >
|
|
4987
|
+
AND m.createdAt > ?${resourceCondition}
|
|
4974
4988
|
ORDER BY m.createdAt ASC, m.id ASC
|
|
4975
4989
|
LIMIT ${nextLimit}
|
|
4976
4990
|
)`);
|
|
4977
4991
|
params.push(target.threadId, target.createdAt);
|
|
4992
|
+
if (resourceId) params.push(resourceId);
|
|
4978
4993
|
}
|
|
4979
4994
|
}
|
|
4980
4995
|
if (unionQueries.length === 0) return null;
|
|
@@ -4982,7 +4997,17 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
4982
4997
|
const [rows] = await this.pool.execute(finalQuery, params);
|
|
4983
4998
|
return rows;
|
|
4984
4999
|
}
|
|
4985
|
-
|
|
5000
|
+
/**
|
|
5001
|
+
* Resolves include items against loaded threads, adding each pinned message and its
|
|
5002
|
+
* before/after window.
|
|
5003
|
+
*
|
|
5004
|
+
* @param threadId - Thread used when an include item names no thread of its own.
|
|
5005
|
+
* @param include - Message ids to pin, each with an optional before/after window.
|
|
5006
|
+
* @param messagesByThread - Cache of thread snapshots, reused and filled as threads load.
|
|
5007
|
+
* @param resourceId - When set, restricts both the pinned messages and their context
|
|
5008
|
+
* to that resource so an id from another resource returns nothing.
|
|
5009
|
+
*/
|
|
5010
|
+
async collectIncludeMessages({ threadId, include, messagesByThread, resourceId }) {
|
|
4986
5011
|
if (!include?.length) return [];
|
|
4987
5012
|
const includeMessages = [];
|
|
4988
5013
|
const seenIds = /* @__PURE__ */ new Set();
|
|
@@ -4990,18 +5015,18 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
4990
5015
|
const resolvedThreadIds = /* @__PURE__ */ new Map();
|
|
4991
5016
|
if (unresolvedIds.length > 0) {
|
|
4992
5017
|
const placeholders = unresolvedIds.map(() => "?").join(", ");
|
|
4993
|
-
const [rows] = await this.pool.execute(`SELECT id, thread_id FROM ${formatTableName(TABLE_MESSAGES)} WHERE id IN (${placeholders})`, unresolvedIds);
|
|
5018
|
+
const [rows] = await this.pool.execute(`SELECT id, thread_id FROM ${formatTableName(TABLE_MESSAGES)} WHERE id IN (${placeholders})${resourceId ? ` AND ${quoteIdentifier("resourceId", "column name")} = ?` : ""}`, resourceId ? [...unresolvedIds, resourceId] : unresolvedIds);
|
|
4994
5019
|
for (const row of rows) resolvedThreadIds.set(row.id, row.thread_id);
|
|
4995
5020
|
}
|
|
4996
5021
|
for (const inc of include) {
|
|
4997
5022
|
const targetThreadId = inc.threadId ?? resolvedThreadIds.get(inc.id) ?? threadId;
|
|
4998
5023
|
let threadMessages = messagesByThread.get(targetThreadId);
|
|
4999
5024
|
if (!threadMessages) {
|
|
5000
|
-
threadMessages = (await this.fetchMessagesForThread(targetThreadId)).map((row) => this.mapMessage(row));
|
|
5025
|
+
threadMessages = (await this.fetchMessagesForThread(targetThreadId, void 0, resourceId)).map((row) => this.mapMessage(row));
|
|
5001
5026
|
messagesByThread.set(targetThreadId, threadMessages);
|
|
5002
5027
|
}
|
|
5003
5028
|
if (!threadMessages.some((message) => message.id === inc.id) || (inc.withPreviousMessages ?? 0) > 0 || (inc.withNextMessages ?? 0) > 0 || threadMessages.length < (inc.withNextMessages ?? 0) + (inc.withPreviousMessages ?? 0) + 1) {
|
|
5004
|
-
threadMessages = (await this.fetchMessagesForThread(targetThreadId)).map((row) => this.mapMessage(row));
|
|
5029
|
+
threadMessages = (await this.fetchMessagesForThread(targetThreadId, void 0, resourceId)).map((row) => this.mapMessage(row));
|
|
5005
5030
|
messagesByThread.set(targetThreadId, threadMessages);
|
|
5006
5031
|
}
|
|
5007
5032
|
const targetIndex = threadMessages.findIndex((msg) => msg.id === inc.id);
|
|
@@ -5185,14 +5210,14 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
5185
5210
|
tableName: TABLE_THREADS,
|
|
5186
5211
|
keys: { id },
|
|
5187
5212
|
data: {
|
|
5188
|
-
title,
|
|
5213
|
+
title: title ?? existing.title,
|
|
5189
5214
|
metadata: JSON.stringify(mergedMetadata),
|
|
5190
5215
|
updatedAt
|
|
5191
5216
|
}
|
|
5192
5217
|
});
|
|
5193
5218
|
return {
|
|
5194
5219
|
...existing,
|
|
5195
|
-
title,
|
|
5220
|
+
title: title ?? existing.title,
|
|
5196
5221
|
metadata: mergedMetadata,
|
|
5197
5222
|
updatedAt
|
|
5198
5223
|
};
|
|
@@ -5595,7 +5620,10 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
5595
5620
|
hasMore: false
|
|
5596
5621
|
};
|
|
5597
5622
|
if (perPage === 0 && include && include.length > 0) {
|
|
5598
|
-
const includeRows = await this._getIncludedMessages({
|
|
5623
|
+
const includeRows = await this._getIncludedMessages({
|
|
5624
|
+
include,
|
|
5625
|
+
resourceId
|
|
5626
|
+
});
|
|
5599
5627
|
if (!includeRows || includeRows.length === 0) return {
|
|
5600
5628
|
messages: [],
|
|
5601
5629
|
total: 0,
|
|
@@ -5633,7 +5661,8 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
5633
5661
|
const includeMessages = await this.collectIncludeMessages({
|
|
5634
5662
|
threadId: primaryThreadId,
|
|
5635
5663
|
include,
|
|
5636
|
-
messagesByThread
|
|
5664
|
+
messagesByThread,
|
|
5665
|
+
resourceId
|
|
5637
5666
|
});
|
|
5638
5667
|
const combinedMap = /* @__PURE__ */ new Map();
|
|
5639
5668
|
for (const msg of paginatedMain) combinedMap.set(msg.id, msg);
|
|
@@ -5721,7 +5750,7 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
5721
5750
|
if (include && include.length > 0) {
|
|
5722
5751
|
const validInclude = (await Promise.all(include.map(async (inc) => {
|
|
5723
5752
|
if (inc.threadId) return inc;
|
|
5724
|
-
const [msgRows] = await this.pool.execute(`SELECT thread_id FROM ${tableName} WHERE id = ? LIMIT 1`, [inc.id]);
|
|
5753
|
+
const [msgRows] = await this.pool.execute(`SELECT thread_id FROM ${tableName} WHERE id = ? AND ${quoteIdentifier("resourceId", "column name")} = ? LIMIT 1`, [inc.id, resourceId]);
|
|
5725
5754
|
const threadId = msgRows?.[0]?.thread_id;
|
|
5726
5755
|
return threadId ? {
|
|
5727
5756
|
...inc,
|
|
@@ -5733,7 +5762,8 @@ var MemoryMySQL = class MemoryMySQL extends MemoryStorage {
|
|
|
5733
5762
|
const includeMessages = await this.collectIncludeMessages({
|
|
5734
5763
|
threadId: validInclude[0].threadId,
|
|
5735
5764
|
include: validInclude,
|
|
5736
|
-
messagesByThread
|
|
5765
|
+
messagesByThread,
|
|
5766
|
+
resourceId
|
|
5737
5767
|
});
|
|
5738
5768
|
for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
|
|
5739
5769
|
messages.push(includeMsg);
|