@mastra/memory 1.36.0-alpha.0 → 1.36.0-alpha.1

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.
@@ -33294,22 +33294,32 @@ var Memory = class Memory extends _mastra_core_memory.MastraMemory {
33294
33294
  if (resourceScope && !resourceId && config?.semanticRecall && vectorSearchString) throw new Error("Memory error: Resource-scoped semantic recall is enabled but no resourceId was provided. Either provide a resourceId or explicitly set semanticRecall.scope to 'thread'.");
33295
33295
  let usage;
33296
33296
  if (config?.semanticRecall && vectorSearchString && this.vector) {
33297
- const result = await this.embedMessageContent(vectorSearchString);
33298
- usage = result.usage;
33299
- const { embeddings, dimension } = result;
33300
- const { indexName } = await this.createEmbeddingIndex(dimension, config);
33301
- await Promise.all(embeddings.map(async (embedding) => {
33302
- if (typeof this.vector === `undefined`) throw new Error(`Tried to query vector index ${indexName} but this Memory instance doesn't have an attached vector db.`);
33303
- const scopeFilter = resourceScope ? { resource_id: resourceId } : { thread_id: threadId };
33304
- const userFilter = typeof config.semanticRecall === "object" ? config.semanticRecall.filter : void 0;
33305
- const combinedFilter = userFilter ? { $and: [scopeFilter, userFilter] } : scopeFilter;
33297
+ const scopeFilter = resourceScope ? { resource_id: resourceId } : { thread_id: threadId };
33298
+ const userFilter = typeof config.semanticRecall === "object" ? config.semanticRecall.filter : void 0;
33299
+ const combinedFilter = userFilter ? { $and: [scopeFilter, userFilter] } : scopeFilter;
33300
+ if (this.isSelfEmbedding) {
33301
+ const { indexName } = await this.createEmbeddingIndex(void 0, config);
33306
33302
  vectorResults.push(...await this.vector.query({
33307
33303
  indexName,
33308
- queryVector: embedding,
33304
+ queryText: vectorSearchString,
33309
33305
  topK: vectorConfig.topK,
33310
33306
  filter: combinedFilter
33311
33307
  }));
33312
- }));
33308
+ } else {
33309
+ const result = await this.embedMessageContent(vectorSearchString);
33310
+ usage = result.usage;
33311
+ const { embeddings, dimension } = result;
33312
+ const { indexName } = await this.createEmbeddingIndex(dimension, config);
33313
+ await Promise.all(embeddings.map(async (embedding) => {
33314
+ if (typeof this.vector === `undefined`) throw new Error(`Tried to query vector index ${indexName} but this Memory instance doesn't have an attached vector db.`);
33315
+ vectorResults.push(...await this.vector.query({
33316
+ indexName,
33317
+ queryVector: embedding,
33318
+ topK: vectorConfig.topK,
33319
+ filter: combinedFilter
33320
+ }));
33321
+ }));
33322
+ }
33313
33323
  }
33314
33324
  const threshold = (typeof config.semanticRecall === "object" ? config.semanticRecall : void 0)?.threshold;
33315
33325
  const filteredVectorResults = threshold !== void 0 ? vectorResults.filter((r) => r.score >= threshold) : vectorResults;
@@ -33467,11 +33477,13 @@ var Memory = class Memory extends _mastra_core_memory.MastraMemory {
33467
33477
  if (this.vector) this.trackVectorCleanup(this.deleteThreadVectors(threadId));
33468
33478
  }
33469
33479
  /**
33470
- * Prefix shared by every message index. The index for the default embedding
33471
- * dimension is named with the bare prefix; other dimensions add a suffix.
33480
+ * Prefix shared by every message index. The index for the default embedding dimension is named
33481
+ * with the bare prefix; other dimensions add a dimension suffix, and a store that embeds the
33482
+ * text itself adds its own. Built here rather than read from `getEmbeddingIndexName()`, which
33483
+ * answers for one configuration and so would not match indexes written under another.
33472
33484
  */
33473
33485
  get messageIndexPrefix() {
33474
- return this.getEmbeddingIndexName();
33486
+ return `memory${this.vector?.indexSeparator ?? "_"}messages`;
33475
33487
  }
33476
33488
  /**
33477
33489
  * Prefix shared by every observation index. Each observation index adds a dimension suffix.
@@ -33756,13 +33768,24 @@ ${workingMemory}`;
33756
33768
  if (joined) textForEmbedding = joined;
33757
33769
  }
33758
33770
  if (!textForEmbedding) return;
33759
- const result = await this.embedMessageContent(textForEmbedding);
33760
- dimension = result.dimension;
33761
- if (result.usage?.tokens) totalTokens += result.usage.tokens;
33771
+ let embeddings = [];
33772
+ let documents;
33773
+ let chunks;
33774
+ if (this.isSelfEmbedding) {
33775
+ chunks = this.chunkText(textForEmbedding);
33776
+ documents = chunks;
33777
+ } else {
33778
+ const result = await this.embedMessageContent(textForEmbedding);
33779
+ dimension = result.dimension;
33780
+ if (result.usage?.tokens) totalTokens += result.usage.tokens;
33781
+ embeddings = result.embeddings;
33782
+ chunks = result.chunks;
33783
+ }
33762
33784
  const threadMetadata = message.threadId ? threadMetadataMap.get(message.threadId) || {} : {};
33763
33785
  embeddingData.push({
33764
- embeddings: result.embeddings,
33765
- metadata: result.chunks.map(() => ({
33786
+ embeddings,
33787
+ documents,
33788
+ metadata: chunks.map(() => ({
33766
33789
  ...threadMetadata,
33767
33790
  message_id: message.id,
33768
33791
  thread_id: message.threadId,
@@ -33773,16 +33796,23 @@ ${workingMemory}`;
33773
33796
  }))
33774
33797
  });
33775
33798
  }));
33776
- if (embeddingData.length > 0 && dimension !== void 0) {
33799
+ if (embeddingData.length > 0 && (this.isSelfEmbedding || dimension !== void 0)) {
33777
33800
  if (typeof this.vector === `undefined`) throw new Error(`Tried to upsert embeddings but this Memory instance doesn't have an attached vector db.`);
33778
33801
  const { indexName } = await this.createEmbeddingIndex(dimension, config);
33779
33802
  const allVectors = [];
33780
33803
  const allMetadata = [];
33804
+ const allDocuments = [];
33781
33805
  for (const data of embeddingData) {
33782
33806
  allVectors.push(...data.embeddings);
33807
+ if (data.documents) allDocuments.push(...data.documents);
33783
33808
  allMetadata.push(...data.metadata);
33784
33809
  }
33785
- await this.vector.upsert({
33810
+ if (this.isSelfEmbedding) await this.vector.upsert({
33811
+ indexName,
33812
+ documents: allDocuments,
33813
+ metadata: allMetadata
33814
+ });
33815
+ else await this.vector.upsert({
33786
33816
  indexName,
33787
33817
  vectors: allVectors,
33788
33818
  metadata: allMetadata
@@ -34567,11 +34597,22 @@ Notes:
34567
34597
  }
34568
34598
  }
34569
34599
  if (textForEmbedding) {
34570
- const result = await this.embedMessageContent(textForEmbedding);
34571
- dimension = result.dimension;
34600
+ let embeddings = [];
34601
+ let documents;
34602
+ let chunks;
34603
+ if (this.isSelfEmbedding) {
34604
+ chunks = this.chunkText(textForEmbedding);
34605
+ documents = chunks;
34606
+ } else {
34607
+ const result = await this.embedMessageContent(textForEmbedding);
34608
+ dimension = result.dimension;
34609
+ embeddings = result.embeddings;
34610
+ chunks = result.chunks;
34611
+ }
34572
34612
  embeddingData.push({
34573
- embeddings: result.embeddings,
34574
- metadata: result.chunks.map(() => ({
34613
+ embeddings,
34614
+ documents,
34615
+ metadata: chunks.map(() => ({
34575
34616
  message_id: message.id,
34576
34617
  thread_id: existingMessage.threadId,
34577
34618
  resource_id: existingMessage.resourceId,
@@ -34606,15 +34647,22 @@ Notes:
34606
34647
  } catch {
34607
34648
  this.logger.debug("Failed to clean up old vectors during message update");
34608
34649
  }
34609
- if (embeddingData.length > 0 && dimension !== void 0) {
34650
+ if (embeddingData.length > 0 && (this.isSelfEmbedding || dimension !== void 0)) {
34610
34651
  const { indexName } = await this.createEmbeddingIndex(dimension, config);
34611
34652
  const allVectors = [];
34612
34653
  const allMetadata = [];
34654
+ const allDocuments = [];
34613
34655
  for (const data of embeddingData) {
34614
34656
  allVectors.push(...data.embeddings);
34657
+ if (data.documents) allDocuments.push(...data.documents);
34615
34658
  allMetadata.push(...data.metadata);
34616
34659
  }
34617
- await this.vector.upsert({
34660
+ if (this.isSelfEmbedding) await this.vector.upsert({
34661
+ indexName,
34662
+ documents: allDocuments,
34663
+ metadata: allMetadata
34664
+ });
34665
+ else await this.vector.upsert({
34618
34666
  indexName,
34619
34667
  vectors: allVectors,
34620
34668
  metadata: allMetadata
@@ -34801,7 +34849,7 @@ Notes:
34801
34849
  await this.rollbackCopiedThread(memoryStore, result.thread, "OM clone", false, priorDestinationResourceWm);
34802
34850
  throw error;
34803
34851
  }
34804
- if (this.vector && this.embedder && config.semanticRecall) try {
34852
+ if (this.vector && (this.embedder || this.isSelfEmbedding) && config.semanticRecall) try {
34805
34853
  await this.embedCopiedMessagesInBatches(memoryStore, result, config);
34806
34854
  } catch (error) {
34807
34855
  await this.rollbackCopiedThread(memoryStore, result.thread, "embedding", true, priorDestinationResourceWm);
@@ -34870,7 +34918,7 @@ Notes:
34870
34918
  async updateThreadResourceId({ threadId, resourceId, memoryConfig }) {
34871
34919
  const memoryStore = await this.getMemoryStore();
34872
34920
  const config = this.getMergedThreadConfig(memoryConfig);
34873
- const migratesVectors = Boolean(this.vector && this.embedder && config.semanticRecall);
34921
+ const migratesVectors = Boolean(this.vector && (this.embedder || this.isSelfEmbedding) && config.semanticRecall);
34874
34922
  if (!migratesVectors) {
34875
34923
  const existing = await memoryStore.getThreadById({ threadId });
34876
34924
  if (existing && existing.resourceId === resourceId) return existing;
@@ -34976,7 +35024,7 @@ Notes:
34976
35024
  * This is similar to the embedding logic in saveMessages but operates on already-saved messages.
34977
35025
  */
34978
35026
  async embedClonedMessages(messages, config) {
34979
- if (!this.vector || !this.embedder) return;
35027
+ if (!this.vector || !this.embedder && !this.isSelfEmbedding) return;
34980
35028
  const embeddingData = [];
34981
35029
  let dimension;
34982
35030
  await Promise.all(messages.map(async (message) => {
@@ -34987,11 +35035,22 @@ Notes:
34987
35035
  if (joined) textForEmbedding = joined;
34988
35036
  }
34989
35037
  if (!textForEmbedding) return;
34990
- const result = await this.embedMessageContent(textForEmbedding);
34991
- dimension = result.dimension;
35038
+ let embeddings = [];
35039
+ let documents;
35040
+ let chunks;
35041
+ if (this.isSelfEmbedding) {
35042
+ chunks = this.chunkText(textForEmbedding);
35043
+ documents = chunks;
35044
+ } else {
35045
+ const result = await this.embedMessageContent(textForEmbedding);
35046
+ dimension = result.dimension;
35047
+ embeddings = result.embeddings;
35048
+ chunks = result.chunks;
35049
+ }
34992
35050
  embeddingData.push({
34993
- embeddings: result.embeddings,
34994
- metadata: result.chunks.map(() => ({
35051
+ embeddings,
35052
+ documents,
35053
+ metadata: chunks.map(() => ({
34995
35054
  message_id: message.id,
34996
35055
  thread_id: message.threadId,
34997
35056
  resource_id: message.resourceId,
@@ -35001,15 +35060,22 @@ Notes:
35001
35060
  }))
35002
35061
  });
35003
35062
  }));
35004
- if (embeddingData.length > 0 && dimension !== void 0) {
35063
+ if (embeddingData.length > 0 && (this.isSelfEmbedding || dimension !== void 0)) {
35005
35064
  const { indexName } = await this.createEmbeddingIndex(dimension, config);
35006
35065
  const allVectors = [];
35007
35066
  const allMetadata = [];
35067
+ const allDocuments = [];
35008
35068
  for (const data of embeddingData) {
35009
35069
  allVectors.push(...data.embeddings);
35070
+ if (data.documents) allDocuments.push(...data.documents);
35010
35071
  allMetadata.push(...data.metadata);
35011
35072
  }
35012
- await this.vector.upsert({
35073
+ if (this.isSelfEmbedding) await this.vector.upsert({
35074
+ indexName,
35075
+ documents: allDocuments,
35076
+ metadata: allMetadata
35077
+ });
35078
+ else await this.vector.upsert({
35013
35079
  indexName,
35014
35080
  vectors: allVectors,
35015
35081
  metadata: allMetadata
@@ -35498,4 +35564,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
35498
35564
  }
35499
35565
  });
35500
35566
 
35501
- //# sourceMappingURL=src-DhCg-LqR.cjs.map
35567
+ //# sourceMappingURL=src-Ch_8ufgu.cjs.map