@mastra/libsql 1.20.0-alpha.0 → 1.20.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/dist/index.js CHANGED
@@ -6721,14 +6721,21 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
6721
6721
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
6722
6722
  });
6723
6723
  }
6724
- async _getIncludedMessages({ include }) {
6724
+ /**
6725
+ * Fetches included messages by ID, discovering their thread automatically.
6726
+ * This handles cross-thread includes where the include item doesn't specify a threadId.
6727
+ * When a resourceId is given, both the target lookup and the surrounding window stay
6728
+ * inside that resource, so an include never leaks another resource's messages.
6729
+ */
6730
+ async _getIncludedMessages({ include, resourceId }) {
6725
6731
  if (!include || include.length === 0) return null;
6726
6732
  const targetIds = include.map((inc) => inc.id).filter(Boolean);
6727
6733
  if (targetIds.length === 0) return null;
6734
+ const resourceCondition = resourceId ? ` AND "resourceId" = ?` : "";
6728
6735
  const idPlaceholders = targetIds.map(() => "?").join(", ");
6729
6736
  const targetResult = await this.#client.execute({
6730
- sql: `SELECT id, thread_id, "createdAt" FROM "${TABLE_MESSAGES}" WHERE id IN (${idPlaceholders})`,
6731
- args: targetIds
6737
+ sql: `SELECT id, thread_id, "createdAt" FROM "${TABLE_MESSAGES}" WHERE id IN (${idPlaceholders})${resourceCondition}`,
6738
+ args: resourceId ? [...targetIds, resourceId] : targetIds
6732
6739
  });
6733
6740
  if (!targetResult.rows || targetResult.rows.length === 0) return null;
6734
6741
  const targetMap = new Map(targetResult.rows.map((r) => [r.id, {
@@ -6745,21 +6752,25 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
6745
6752
  SELECT id, content, role, type, "createdAt", thread_id, "resourceId"
6746
6753
  FROM "${TABLE_MESSAGES}"
6747
6754
  WHERE thread_id = ?
6748
- AND "createdAt" <= ?
6755
+ AND "createdAt" <= ?${resourceCondition}
6749
6756
  ORDER BY "createdAt" DESC, id DESC
6750
6757
  LIMIT ?
6751
6758
  )`);
6752
- params.push(target.threadId, target.createdAt, withPreviousMessages + 1);
6759
+ params.push(target.threadId, target.createdAt);
6760
+ if (resourceId) params.push(resourceId);
6761
+ params.push(withPreviousMessages + 1);
6753
6762
  if (withNextMessages > 0) {
6754
6763
  unionQueries.push(`SELECT * FROM (
6755
6764
  SELECT id, content, role, type, "createdAt", thread_id, "resourceId"
6756
6765
  FROM "${TABLE_MESSAGES}"
6757
6766
  WHERE thread_id = ?
6758
- AND "createdAt" > ?
6767
+ AND "createdAt" > ?${resourceCondition}
6759
6768
  ORDER BY "createdAt" ASC, id ASC
6760
6769
  LIMIT ?
6761
6770
  )`);
6762
- params.push(target.threadId, target.createdAt, withNextMessages);
6771
+ params.push(target.threadId, target.createdAt);
6772
+ if (resourceId) params.push(resourceId);
6773
+ params.push(withNextMessages);
6763
6774
  }
6764
6775
  }
6765
6776
  if (unionQueries.length === 0) return null;
@@ -6855,7 +6866,10 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
6855
6866
  hasMore: false
6856
6867
  };
6857
6868
  if (perPage === 0 && include && include.length > 0) {
6858
- const includeMessages = await this._getIncludedMessages({ include });
6869
+ const includeMessages = await this._getIncludedMessages({
6870
+ include,
6871
+ resourceId
6872
+ });
6859
6873
  if (!includeMessages || includeMessages.length === 0) return {
6860
6874
  messages: [],
6861
6875
  total: 0,
@@ -6896,7 +6910,10 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
6896
6910
  };
6897
6911
  const messageIds = new Set(messages.map((m) => m.id));
6898
6912
  if (include && include.length > 0) {
6899
- const includeMessages = await this._getIncludedMessages({ include });
6913
+ const includeMessages = await this._getIncludedMessages({
6914
+ include,
6915
+ resourceId
6916
+ });
6900
6917
  if (includeMessages) {
6901
6918
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
6902
6919
  messages.push(includeMsg);
@@ -6975,7 +6992,10 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
6975
6992
  hasMore: false
6976
6993
  };
6977
6994
  if (perPage === 0 && include && include.length > 0) {
6978
- const includeMessages = await this._getIncludedMessages({ include });
6995
+ const includeMessages = await this._getIncludedMessages({
6996
+ include,
6997
+ resourceId
6998
+ });
6979
6999
  if (!includeMessages || includeMessages.length === 0) return {
6980
7000
  messages: [],
6981
7001
  total: 0,
@@ -7015,7 +7035,10 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
7015
7035
  };
7016
7036
  const messageIds = new Set(messages.map((m) => m.id));
7017
7037
  if (include && include.length > 0) {
7018
- const includeMessages = await this._getIncludedMessages({ include });
7038
+ const includeMessages = await this._getIncludedMessages({
7039
+ include,
7040
+ resourceId
7041
+ });
7019
7042
  if (includeMessages) {
7020
7043
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
7021
7044
  messages.push(includeMsg);
@@ -7426,7 +7449,7 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
7426
7449
  const now = /* @__PURE__ */ new Date();
7427
7450
  const updatedThread = {
7428
7451
  ...thread,
7429
- title,
7452
+ title: title ?? thread.title,
7430
7453
  metadata: {
7431
7454
  ...thread.metadata,
7432
7455
  ...metadata
@@ -7435,9 +7458,9 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
7435
7458
  };
7436
7459
  try {
7437
7460
  await this.#client.execute({
7438
- sql: `UPDATE ${TABLE_THREADS} SET title = ?, metadata = jsonb(?), updatedAt = ? WHERE id = ?`,
7461
+ sql: `UPDATE ${TABLE_THREADS} SET title = COALESCE(?, title), metadata = jsonb(?), updatedAt = ? WHERE id = ?`,
7439
7462
  args: [
7440
- title,
7463
+ title ?? null,
7441
7464
  JSON.stringify(updatedThread.metadata),
7442
7465
  now.toISOString(),
7443
7466
  id